2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Copyright (c) 2013 Spectra Logic Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
36 #include <fs/nfs/nfsport.h>
37 #include <fs/nfsserver/nfs_fha_new.h>
41 static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
43 static void fhanew_init(void *foo);
44 static void fhanew_uninit(void *foo);
45 static rpcproc_t fhanew_get_procnum(rpcproc_t procnum);
46 static int fhanew_get_fh(uint64_t *fh, int v3, struct mbuf **md,
48 static int fhanew_is_read(rpcproc_t procnum);
49 static int fhanew_is_write(rpcproc_t procnum);
50 static int fhanew_get_offset(struct mbuf **md, caddr_t *dpos,
51 int v3, struct fha_info *info);
52 static int fhanew_no_offset(rpcproc_t procnum);
53 static void fhanew_set_locktype(rpcproc_t procnum,
54 struct fha_info *info);
55 static int fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS);
56 static void fha_extract_info(struct svc_req *req,
59 static struct fha_params fhanew_softc;
61 SYSCTL_DECL(_vfs_nfsd);
63 extern int newnfs_nfsv3_procid[];
64 extern SVCPOOL *nfsrvd_pool;
66 SYSINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_init, NULL);
67 SYSUNINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_uninit, NULL);
70 fhanew_init(void *foo)
72 struct fha_params *softc;
75 softc = &fhanew_softc;
77 bzero(softc, sizeof(*softc));
79 snprintf(softc->server_name, sizeof(softc->server_name),
82 softc->pool = &nfsrvd_pool;
85 * Initialize the sysctl context list for the fha module.
87 sysctl_ctx_init(&softc->sysctl_ctx);
88 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
89 SYSCTL_STATIC_CHILDREN(_vfs_nfsd), OID_AUTO, "fha",
90 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "NFS File Handle Affinity (FHA)");
91 if (softc->sysctl_tree == NULL) {
92 printf("%s: unable to allocate sysctl tree\n", __func__);
96 for (i = 0; i < FHA_HASH_SIZE; i++)
97 mtx_init(&softc->fha_hash[i].mtx, "fhalock", NULL, MTX_DEF);
100 * Set the default tuning parameters.
102 softc->ctls.enable = FHA_DEF_ENABLE;
103 softc->ctls.read = FHA_DEF_READ;
104 softc->ctls.write = FHA_DEF_WRITE;
105 softc->ctls.bin_shift = FHA_DEF_BIN_SHIFT;
106 softc->ctls.max_nfsds_per_fh = FHA_DEF_MAX_NFSDS_PER_FH;
107 softc->ctls.max_reqs_per_nfsd = FHA_DEF_MAX_REQS_PER_NFSD;
110 * Add sysctls so the user can change the tuning parameters.
112 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
113 OID_AUTO, "enable", CTLFLAG_RWTUN,
114 &softc->ctls.enable, 0, "Enable NFS File Handle Affinity (FHA)");
116 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
117 OID_AUTO, "read", CTLFLAG_RWTUN,
118 &softc->ctls.read, 0, "Enable NFS FHA read locality");
120 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
121 OID_AUTO, "write", CTLFLAG_RWTUN,
122 &softc->ctls.write, 0, "Enable NFS FHA write locality");
124 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
125 OID_AUTO, "bin_shift", CTLFLAG_RWTUN,
126 &softc->ctls.bin_shift, 0,
127 "Maximum locality distance 2^(bin_shift) bytes");
129 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
130 OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RWTUN,
131 &softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
132 "should be working on requests for the same file handle");
134 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
135 OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RWTUN,
136 &softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
137 "single nfsd thread should be working on at any time");
139 SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
140 OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
141 0, 0, fhenew_stats_sysctl, "A", "");
145 fhanew_uninit(void *foo)
147 struct fha_params *softc;
150 softc = &fhanew_softc;
152 sysctl_ctx_free(&softc->sysctl_ctx);
153 for (i = 0; i < FHA_HASH_SIZE; i++)
154 mtx_destroy(&softc->fha_hash[i].mtx);
158 fhanew_get_procnum(rpcproc_t procnum)
160 if (procnum > NFSV2PROC_STATFS)
163 return (newnfs_nfsv3_procid[procnum]);
167 fhanew_get_fh(uint64_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
169 struct nfsrv_descript lnd, *nd;
183 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
184 if ((len = fxdr_unsigned(int, *tl)) <= 0 || len > NFSX_FHMAX) {
194 NFSM_DISSECT_NONBLOCK(buf, uint8_t *, len);
195 for (i = 0; i < len; i++)
196 t ^= ((uint64_t)buf[i] << (i & 7) * 8);
208 fhanew_is_read(rpcproc_t procnum)
210 if (procnum == NFSPROC_READ)
217 fhanew_is_write(rpcproc_t procnum)
219 if (procnum == NFSPROC_WRITE)
226 fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
227 struct fha_info *info)
229 struct nfsrv_descript lnd, *nd;
240 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, 2 * NFSX_UNSIGNED);
241 info->offset = fxdr_hyper(tl);
243 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
244 info->offset = fxdr_unsigned(uint32_t, *tl);
255 fhanew_no_offset(rpcproc_t procnum)
257 if (procnum == NFSPROC_FSSTAT ||
258 procnum == NFSPROC_FSINFO ||
259 procnum == NFSPROC_PATHCONF ||
260 procnum == NFSPROC_NOOP ||
261 procnum == NFSPROC_NULL)
268 fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info)
272 case NFSPROC_GETATTR:
275 case NFSPROC_READLINK:
277 case NFSPROC_READDIR:
278 case NFSPROC_READDIRPLUS:
280 info->locktype = LK_SHARED;
282 case NFSPROC_SETATTR:
285 case NFSPROC_SYMLINK:
293 case NFSPROC_PATHCONF:
296 info->locktype = LK_EXCLUSIVE;
302 * This just specifies that offsets should obey affinity when within
303 * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
306 fha_extract_info(struct svc_req *req, struct fha_info *i)
310 static u_int64_t random_fh = 0;
312 int v3 = (req->rq_vers == 3);
316 * We start off with a random fh. If we get a reasonable
317 * procnum, we set the fh. If there's a concept of offset
318 * that we're interested in, we set that.
322 i->locktype = LK_EXCLUSIVE;
323 i->read = i->write = 0;
326 * Extract the procnum and convert to v3 form if necessary,
327 * taking care to deal with out-of-range procnums. Caller will
328 * ensure that rq_vers is either 2 or 3.
330 procnum = req->rq_proc;
332 rpcproc_t tmp_procnum;
334 tmp_procnum = fhanew_get_procnum(procnum);
335 if (tmp_procnum == -1)
337 procnum = tmp_procnum;
341 * We do affinity for most. However, we divide a realm of affinity
342 * by file offset so as to allow for concurrent random access. We
343 * only do this for reads today, but this may change when IFS supports
344 * efficient concurrent writes.
346 if (fhanew_no_offset(procnum))
349 i->read = fhanew_is_read(procnum);
350 i->write = fhanew_is_write(procnum);
352 error = newnfs_realign(&req->rq_args, M_NOWAIT);
356 dpos = mtod(md, caddr_t);
358 /* Grab the filehandle. */
359 error = fhanew_get_fh(&i->fh, v3, &md, &dpos);
363 /* Content ourselves with zero offset for all but reads. */
364 if (i->read || i->write)
365 fhanew_get_offset(&md, &dpos, v3, i);
368 fhanew_set_locktype(procnum, i);
371 static struct fha_hash_entry *
372 fha_hash_entry_new(u_int64_t fh)
374 struct fha_hash_entry *e;
376 e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
379 e->num_exclusive = 0;
381 LIST_INIT(&e->threads);
387 fha_hash_entry_destroy(struct fha_hash_entry *e)
390 mtx_assert(e->mtx, MA_OWNED);
391 KASSERT(e->num_rw == 0,
392 ("%d reqs on destroyed fhe %p", e->num_rw, e));
393 KASSERT(e->num_exclusive == 0,
394 ("%d exclusive reqs on destroyed fhe %p", e->num_exclusive, e));
395 KASSERT(e->num_threads == 0,
396 ("%d threads on destroyed fhe %p", e->num_threads, e));
401 fha_hash_entry_remove(struct fha_hash_entry *e)
404 mtx_assert(e->mtx, MA_OWNED);
405 LIST_REMOVE(e, link);
406 fha_hash_entry_destroy(e);
409 static struct fha_hash_entry *
410 fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
412 struct fha_hash_slot *fhs;
413 struct fha_hash_entry *fhe, *new_fhe;
415 fhs = &softc->fha_hash[fh % FHA_HASH_SIZE];
416 new_fhe = fha_hash_entry_new(fh);
417 new_fhe->mtx = &fhs->mtx;
419 LIST_FOREACH(fhe, &fhs->list, link)
424 LIST_INSERT_HEAD(&fhs->list, fhe, link);
426 fha_hash_entry_destroy(new_fhe);
431 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
434 mtx_assert(fhe->mtx, MA_OWNED);
436 LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
441 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
444 mtx_assert(fhe->mtx, MA_OWNED);
445 KASSERT(thread->st_p2 == 0,
446 ("%d reqs on removed thread %p", thread->st_p2, thread));
447 LIST_REMOVE(thread, st_alink);
452 * Account for an ongoing operation associated with this file.
455 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
458 mtx_assert(fhe->mtx, MA_OWNED);
459 if (LK_EXCLUSIVE == locktype)
460 fhe->num_exclusive += count;
462 fhe->num_rw += count;
466 * Get the service thread currently associated with the fhe that is
467 * appropriate to handle this operation.
470 fha_hash_entry_choose_thread(struct fha_params *softc,
471 struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
473 SVCTHREAD *thread, *min_thread = NULL;
474 int req_count, min_count = 0;
475 off_t offset1, offset2;
477 LIST_FOREACH(thread, &fhe->threads, st_alink) {
478 req_count = thread->st_p2;
480 /* If there are any writes in progress, use the first thread. */
481 if (fhe->num_exclusive) {
483 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
484 "fha: %p(%d)w", thread, req_count);
489 /* Check whether we should consider locality. */
490 if ((i->read && !softc->ctls.read) ||
491 (i->write && !softc->ctls.write))
495 * Check for locality, making sure that we won't
496 * exceed our per-thread load limit in the process.
499 offset2 = thread->st_p3;
501 if (((offset1 >= offset2)
502 && ((offset1 - offset2) < (1 << softc->ctls.bin_shift)))
503 || ((offset2 > offset1)
504 && ((offset2 - offset1) < (1 << softc->ctls.bin_shift)))) {
505 if ((softc->ctls.max_reqs_per_nfsd == 0) ||
506 (req_count < softc->ctls.max_reqs_per_nfsd)) {
508 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
509 "fha: %p(%d)r", thread, req_count);
517 * We don't have a locality match, so skip this thread,
518 * but keep track of the most attractive thread in case
519 * we need to come back to it later.
522 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
523 "fha: %p(%d)s off1 %llu off2 %llu", thread,
524 req_count, offset1, offset2);
526 if ((min_thread == NULL) || (req_count < min_count)) {
527 min_count = req_count;
533 * We didn't find a good match yet. See if we can add
534 * a new thread to this file handle entry's thread list.
536 if ((softc->ctls.max_nfsds_per_fh == 0) ||
537 (fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
538 thread = this_thread;
540 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
541 "fha: %p(%d)t", thread, thread->st_p2);
543 fha_hash_entry_add_thread(fhe, thread);
546 * We don't want to use any more threads for this file, so
547 * go back to the most attractive nfsd we're already using.
556 * After getting a request, try to assign it to some thread. Usually we
557 * handle it ourselves.
560 fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req)
562 struct fha_params *softc = &fhanew_softc;
565 struct fha_hash_entry *fhe;
567 /* Check to see whether we're enabled. */
568 if (softc->ctls.enable == 0)
572 * Only do placement if this is an NFS request.
574 if (req->rq_prog != NFS_PROG)
577 if (req->rq_vers != 2 && req->rq_vers != 3)
580 fha_extract_info(req, &i);
583 * We save the offset associated with this request for later
586 fhe = fha_hash_entry_lookup(softc, i.fh);
588 req->rq_p2 = i.locktype;
589 req->rq_p3 = i.offset;
592 * Choose a thread, taking into consideration locality, thread load,
593 * and the number of threads already working on this file.
595 thread = fha_hash_entry_choose_thread(softc, fhe, &i, this_thread);
596 KASSERT(thread, ("fha_assign: NULL thread!"));
597 fha_hash_entry_add_op(fhe, i.locktype, 1);
599 thread->st_p3 = i.offset;
602 * Grab the pool lock here to not let chosen thread go away before
603 * the new request inserted to its queue while we drop fhe lock.
605 mtx_lock(&thread->st_lock);
606 mtx_unlock(fhe->mtx);
611 mtx_lock(&this_thread->st_lock);
612 return (this_thread);
616 * Called when we're done with an operation. The request has already
620 fhanew_nd_complete(SVCTHREAD *thread, struct svc_req *req)
622 struct fha_hash_entry *fhe = req->rq_p1;
626 * This may be called for reqs that didn't go through
627 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
634 fha_hash_entry_add_op(fhe, req->rq_p2, -1);
636 KASSERT(thread->st_p2 >= 0, ("Negative request count %d on %p",
637 thread->st_p2, thread));
638 if (thread->st_p2 == 0) {
639 fha_hash_entry_remove_thread(fhe, thread);
640 if (0 == fhe->num_rw + fhe->num_exclusive)
641 fha_hash_entry_remove(fhe);
647 fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS)
649 struct fha_params *softc = &fhanew_softc;
652 struct fha_hash_entry *fhe;
653 bool_t first, hfirst;
656 sbuf_new(&sb, NULL, 65536, SBUF_FIXEDLEN);
659 sbuf_printf(&sb, "NFSD not running\n");
663 for (i = 0; i < FHA_HASH_SIZE; i++)
664 if (!LIST_EMPTY(&softc->fha_hash[i].list))
667 if (i == FHA_HASH_SIZE) {
668 sbuf_printf(&sb, "No file handle entries.\n");
673 for (; i < FHA_HASH_SIZE; i++) {
674 mtx_lock(&softc->fha_hash[i].mtx);
675 if (LIST_EMPTY(&softc->fha_hash[i].list)) {
676 mtx_unlock(&softc->fha_hash[i].mtx);
679 sbuf_printf(&sb, "%shash %d: {\n", hfirst ? "" : ", ", i);
681 LIST_FOREACH(fhe, &softc->fha_hash[i].list, link) {
682 sbuf_printf(&sb, "%sfhe %p: {\n", first ? " " : ", ",
684 sbuf_printf(&sb, " fh: %ju\n", (uintmax_t) fhe->fh);
685 sbuf_printf(&sb, " num_rw/exclusive: %d/%d\n",
686 fhe->num_rw, fhe->num_exclusive);
687 sbuf_printf(&sb, " num_threads: %d\n",
690 LIST_FOREACH(thread, &fhe->threads, st_alink) {
691 sbuf_printf(&sb, " thread %p offset %ju "
693 thread->st_p3, thread->st_p2);
696 sbuf_printf(&sb, " }");
699 sbuf_printf(&sb, "\n}");
700 mtx_unlock(&softc->fha_hash[i].mtx);
707 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);