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[];
65 NFSD_VNET_DECLARE(SVCPOOL *, nfsrvd_pool);
67 SYSINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_init, NULL);
68 SYSUNINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_uninit, NULL);
71 fhanew_init(void *foo)
73 struct fha_params *softc;
76 softc = &fhanew_softc;
78 bzero(softc, sizeof(*softc));
80 snprintf(softc->server_name, sizeof(softc->server_name),
83 softc->pool = &NFSD_VNET(nfsrvd_pool);
86 * Initialize the sysctl context list for the fha module.
88 sysctl_ctx_init(&softc->sysctl_ctx);
89 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
90 SYSCTL_STATIC_CHILDREN(_vfs_nfsd), OID_AUTO, "fha",
91 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "NFS File Handle Affinity (FHA)");
92 if (softc->sysctl_tree == NULL) {
93 printf("%s: unable to allocate sysctl tree\n", __func__);
97 for (i = 0; i < FHA_HASH_SIZE; i++)
98 mtx_init(&softc->fha_hash[i].mtx, "fhalock", NULL, MTX_DEF);
101 * Set the default tuning parameters.
103 softc->ctls.enable = FHA_DEF_ENABLE;
104 softc->ctls.read = FHA_DEF_READ;
105 softc->ctls.write = FHA_DEF_WRITE;
106 softc->ctls.bin_shift = FHA_DEF_BIN_SHIFT;
107 softc->ctls.max_nfsds_per_fh = FHA_DEF_MAX_NFSDS_PER_FH;
108 softc->ctls.max_reqs_per_nfsd = FHA_DEF_MAX_REQS_PER_NFSD;
111 * Add sysctls so the user can change the tuning parameters.
113 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
114 OID_AUTO, "enable", CTLFLAG_RWTUN,
115 &softc->ctls.enable, 0, "Enable NFS File Handle Affinity (FHA)");
117 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
118 OID_AUTO, "read", CTLFLAG_RWTUN,
119 &softc->ctls.read, 0, "Enable NFS FHA read locality");
121 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
122 OID_AUTO, "write", CTLFLAG_RWTUN,
123 &softc->ctls.write, 0, "Enable NFS FHA write locality");
125 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
126 OID_AUTO, "bin_shift", CTLFLAG_RWTUN,
127 &softc->ctls.bin_shift, 0,
128 "Maximum locality distance 2^(bin_shift) bytes");
130 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
131 OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RWTUN,
132 &softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
133 "should be working on requests for the same file handle");
135 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
136 OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RWTUN,
137 &softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
138 "single nfsd thread should be working on at any time");
140 SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
141 OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
142 0, 0, fhenew_stats_sysctl, "A", "");
146 fhanew_uninit(void *foo)
148 struct fha_params *softc;
151 softc = &fhanew_softc;
153 sysctl_ctx_free(&softc->sysctl_ctx);
154 for (i = 0; i < FHA_HASH_SIZE; i++)
155 mtx_destroy(&softc->fha_hash[i].mtx);
159 fhanew_get_procnum(rpcproc_t procnum)
161 if (procnum > NFSV2PROC_STATFS)
164 return (newnfs_nfsv3_procid[procnum]);
168 fhanew_get_fh(uint64_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
170 struct nfsrv_descript lnd, *nd;
184 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
185 if ((len = fxdr_unsigned(int, *tl)) <= 0 || len > NFSX_FHMAX) {
195 NFSM_DISSECT_NONBLOCK(buf, uint8_t *, len);
196 for (i = 0; i < len; i++)
197 t ^= ((uint64_t)buf[i] << (i & 7) * 8);
209 fhanew_is_read(rpcproc_t procnum)
211 if (procnum == NFSPROC_READ)
218 fhanew_is_write(rpcproc_t procnum)
220 if (procnum == NFSPROC_WRITE)
227 fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
228 struct fha_info *info)
230 struct nfsrv_descript lnd, *nd;
241 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, 2 * NFSX_UNSIGNED);
242 info->offset = fxdr_hyper(tl);
244 NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
245 info->offset = fxdr_unsigned(uint32_t, *tl);
256 fhanew_no_offset(rpcproc_t procnum)
258 if (procnum == NFSPROC_FSSTAT ||
259 procnum == NFSPROC_FSINFO ||
260 procnum == NFSPROC_PATHCONF ||
261 procnum == NFSPROC_NOOP ||
262 procnum == NFSPROC_NULL)
269 fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info)
273 case NFSPROC_GETATTR:
276 case NFSPROC_READLINK:
278 case NFSPROC_READDIR:
279 case NFSPROC_READDIRPLUS:
281 info->locktype = LK_SHARED;
283 case NFSPROC_SETATTR:
286 case NFSPROC_SYMLINK:
294 case NFSPROC_PATHCONF:
297 info->locktype = LK_EXCLUSIVE;
303 * This just specifies that offsets should obey affinity when within
304 * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
307 fha_extract_info(struct svc_req *req, struct fha_info *i)
311 static u_int64_t random_fh = 0;
313 int v3 = (req->rq_vers == 3);
317 * We start off with a random fh. If we get a reasonable
318 * procnum, we set the fh. If there's a concept of offset
319 * that we're interested in, we set that.
323 i->locktype = LK_EXCLUSIVE;
324 i->read = i->write = 0;
327 * Extract the procnum and convert to v3 form if necessary,
328 * taking care to deal with out-of-range procnums. Caller will
329 * ensure that rq_vers is either 2 or 3.
331 procnum = req->rq_proc;
333 rpcproc_t tmp_procnum;
335 tmp_procnum = fhanew_get_procnum(procnum);
336 if (tmp_procnum == -1)
338 procnum = tmp_procnum;
342 * We do affinity for most. However, we divide a realm of affinity
343 * by file offset so as to allow for concurrent random access. We
344 * only do this for reads today, but this may change when IFS supports
345 * efficient concurrent writes.
347 if (fhanew_no_offset(procnum))
350 i->read = fhanew_is_read(procnum);
351 i->write = fhanew_is_write(procnum);
353 error = newnfs_realign(&req->rq_args, M_NOWAIT);
357 dpos = mtod(md, caddr_t);
359 /* Grab the filehandle. */
360 error = fhanew_get_fh(&i->fh, v3, &md, &dpos);
364 /* Content ourselves with zero offset for all but reads. */
365 if (i->read || i->write)
366 fhanew_get_offset(&md, &dpos, v3, i);
369 fhanew_set_locktype(procnum, i);
372 static struct fha_hash_entry *
373 fha_hash_entry_new(u_int64_t fh)
375 struct fha_hash_entry *e;
377 e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
380 e->num_exclusive = 0;
382 LIST_INIT(&e->threads);
388 fha_hash_entry_destroy(struct fha_hash_entry *e)
391 mtx_assert(e->mtx, MA_OWNED);
392 KASSERT(e->num_rw == 0,
393 ("%d reqs on destroyed fhe %p", e->num_rw, e));
394 KASSERT(e->num_exclusive == 0,
395 ("%d exclusive reqs on destroyed fhe %p", e->num_exclusive, e));
396 KASSERT(e->num_threads == 0,
397 ("%d threads on destroyed fhe %p", e->num_threads, e));
402 fha_hash_entry_remove(struct fha_hash_entry *e)
405 mtx_assert(e->mtx, MA_OWNED);
406 LIST_REMOVE(e, link);
407 fha_hash_entry_destroy(e);
410 static struct fha_hash_entry *
411 fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
413 struct fha_hash_slot *fhs;
414 struct fha_hash_entry *fhe, *new_fhe;
416 fhs = &softc->fha_hash[fh % FHA_HASH_SIZE];
417 new_fhe = fha_hash_entry_new(fh);
418 new_fhe->mtx = &fhs->mtx;
420 LIST_FOREACH(fhe, &fhs->list, link)
425 LIST_INSERT_HEAD(&fhs->list, fhe, link);
427 fha_hash_entry_destroy(new_fhe);
432 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
435 mtx_assert(fhe->mtx, MA_OWNED);
437 LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
442 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
445 mtx_assert(fhe->mtx, MA_OWNED);
446 KASSERT(thread->st_p2 == 0,
447 ("%d reqs on removed thread %p", thread->st_p2, thread));
448 LIST_REMOVE(thread, st_alink);
453 * Account for an ongoing operation associated with this file.
456 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
459 mtx_assert(fhe->mtx, MA_OWNED);
460 if (LK_EXCLUSIVE == locktype)
461 fhe->num_exclusive += count;
463 fhe->num_rw += count;
467 * Get the service thread currently associated with the fhe that is
468 * appropriate to handle this operation.
471 fha_hash_entry_choose_thread(struct fha_params *softc,
472 struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
474 SVCTHREAD *thread, *min_thread = NULL;
475 int req_count, min_count = 0;
476 off_t offset1, offset2;
478 LIST_FOREACH(thread, &fhe->threads, st_alink) {
479 req_count = thread->st_p2;
481 /* If there are any writes in progress, use the first thread. */
482 if (fhe->num_exclusive) {
484 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
485 "fha: %p(%d)w", thread, req_count);
490 /* Check whether we should consider locality. */
491 if ((i->read && !softc->ctls.read) ||
492 (i->write && !softc->ctls.write))
496 * Check for locality, making sure that we won't
497 * exceed our per-thread load limit in the process.
500 offset2 = thread->st_p3;
502 if (((offset1 >= offset2)
503 && ((offset1 - offset2) < (1 << softc->ctls.bin_shift)))
504 || ((offset2 > offset1)
505 && ((offset2 - offset1) < (1 << softc->ctls.bin_shift)))) {
506 if ((softc->ctls.max_reqs_per_nfsd == 0) ||
507 (req_count < softc->ctls.max_reqs_per_nfsd)) {
509 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
510 "fha: %p(%d)r", thread, req_count);
518 * We don't have a locality match, so skip this thread,
519 * but keep track of the most attractive thread in case
520 * we need to come back to it later.
523 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
524 "fha: %p(%d)s off1 %llu off2 %llu", thread,
525 req_count, offset1, offset2);
527 if ((min_thread == NULL) || (req_count < min_count)) {
528 min_count = req_count;
534 * We didn't find a good match yet. See if we can add
535 * a new thread to this file handle entry's thread list.
537 if ((softc->ctls.max_nfsds_per_fh == 0) ||
538 (fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
539 thread = this_thread;
541 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
542 "fha: %p(%d)t", thread, thread->st_p2);
544 fha_hash_entry_add_thread(fhe, thread);
547 * We don't want to use any more threads for this file, so
548 * go back to the most attractive nfsd we're already using.
557 * After getting a request, try to assign it to some thread. Usually we
558 * handle it ourselves.
561 fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req)
563 struct fha_params *softc = &fhanew_softc;
566 struct fha_hash_entry *fhe;
568 /* Check to see whether we're enabled. */
569 if (softc->ctls.enable == 0)
573 * Only do placement if this is an NFS request.
575 if (req->rq_prog != NFS_PROG)
578 if (req->rq_vers != 2 && req->rq_vers != 3)
581 fha_extract_info(req, &i);
584 * We save the offset associated with this request for later
587 fhe = fha_hash_entry_lookup(softc, i.fh);
589 req->rq_p2 = i.locktype;
590 req->rq_p3 = i.offset;
593 * Choose a thread, taking into consideration locality, thread load,
594 * and the number of threads already working on this file.
596 thread = fha_hash_entry_choose_thread(softc, fhe, &i, this_thread);
597 KASSERT(thread, ("fha_assign: NULL thread!"));
598 fha_hash_entry_add_op(fhe, i.locktype, 1);
600 thread->st_p3 = i.offset;
603 * Grab the pool lock here to not let chosen thread go away before
604 * the new request inserted to its queue while we drop fhe lock.
606 mtx_lock(&thread->st_lock);
607 mtx_unlock(fhe->mtx);
612 mtx_lock(&this_thread->st_lock);
613 return (this_thread);
617 * Called when we're done with an operation. The request has already
621 fhanew_nd_complete(SVCTHREAD *thread, struct svc_req *req)
623 struct fha_hash_entry *fhe = req->rq_p1;
627 * This may be called for reqs that didn't go through
628 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
635 fha_hash_entry_add_op(fhe, req->rq_p2, -1);
637 KASSERT(thread->st_p2 >= 0, ("Negative request count %d on %p",
638 thread->st_p2, thread));
639 if (thread->st_p2 == 0) {
640 fha_hash_entry_remove_thread(fhe, thread);
641 if (0 == fhe->num_rw + fhe->num_exclusive)
642 fha_hash_entry_remove(fhe);
648 fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS)
650 struct fha_params *softc = &fhanew_softc;
653 struct fha_hash_entry *fhe;
654 bool_t first, hfirst;
657 sbuf_new(&sb, NULL, 65536, SBUF_FIXEDLEN);
660 sbuf_printf(&sb, "NFSD not running\n");
664 for (i = 0; i < FHA_HASH_SIZE; i++)
665 if (!LIST_EMPTY(&softc->fha_hash[i].list))
668 if (i == FHA_HASH_SIZE) {
669 sbuf_printf(&sb, "No file handle entries.\n");
674 for (; i < FHA_HASH_SIZE; i++) {
675 mtx_lock(&softc->fha_hash[i].mtx);
676 if (LIST_EMPTY(&softc->fha_hash[i].list)) {
677 mtx_unlock(&softc->fha_hash[i].mtx);
680 sbuf_printf(&sb, "%shash %d: {\n", hfirst ? "" : ", ", i);
682 LIST_FOREACH(fhe, &softc->fha_hash[i].list, link) {
683 sbuf_printf(&sb, "%sfhe %p: {\n", first ? " " : ", ",
685 sbuf_printf(&sb, " fh: %ju\n", (uintmax_t) fhe->fh);
686 sbuf_printf(&sb, " num_rw/exclusive: %d/%d\n",
687 fhe->num_rw, fhe->num_exclusive);
688 sbuf_printf(&sb, " num_threads: %d\n",
691 LIST_FOREACH(thread, &fhe->threads, st_alink) {
692 sbuf_printf(&sb, " thread %p offset %ju "
694 thread->st_p3, thread->st_p2);
697 sbuf_printf(&sb, " }");
700 sbuf_printf(&sb, "\n}");
701 mtx_unlock(&softc->fha_hash[i].mtx);
708 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);