2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Here is the basic algorithm:
39 * First, some design criteria I used:
40 * - I think a false hit is more serious than a false miss
41 * - A false hit for an RPC that has Op(s) that order via seqid# must be
43 * - A valid hit will probably happen a long time after the original reply
44 * and the TCP socket that the original request was received on will no
46 * (The long time delay implies to me that LRU is not appropriate.)
47 * - The mechanism will satisfy the requirements of ordering Ops with seqid#s
48 * in them as well as minimizing the risk of redoing retried non-idempotent
50 * Because it is biased towards avoiding false hits, multiple entries with
51 * the same xid are to be expected, especially for the case of the entry
52 * in the cache being related to a seqid# sequenced Op.
54 * The basic algorithm I'm about to code up:
55 * - Null RPCs bypass the cache and are just done
57 * - key on <xid, NFS version> (as noted above, there can be several
58 * entries with the same key)
59 * When a request arrives:
60 * For all that match key
61 * - if RPC# != OR request_size !=
62 * - not a match with this one
63 * - if NFSv4 and received on same TCP socket OR
64 * received on a TCP connection created before the
66 * - not a match with this one
67 * (V2,3 clients might retry on same TCP socket)
68 * - calculate checksum on first N bytes of NFS XDR
70 * - not a match for this one
71 * If any of the remaining ones that match has a
73 * - not a match (go do RPC, using new cache entry)
75 * - a hit (reply from cache)
77 * - miss (go do RPC, using new cache entry)
79 * During processing of NFSv4 request:
80 * - set a flag when a non-idempotent Op is processed
81 * - when an Op that uses a seqid# (Open,...) is processed
82 * - if same seqid# as referenced entry in cache
83 * - free new cache entry
84 * - reply from referenced cache entry
85 * else if next seqid# in order
86 * - free referenced cache entry
87 * - increment seqid_refcnt on new cache entry
88 * - set pointer from Openowner/Lockowner to
89 * new cache entry (aka reference it)
90 * else if first seqid# in sequence
91 * - increment seqid_refcnt on new cache entry
92 * - set pointer from Openowner/Lockowner to
93 * new cache entry (aka reference it)
95 * At end of RPC processing:
96 * - if seqid_refcnt > 0 OR flagged non-idempotent on new
98 * - save reply in cache entry
99 * - calculate checksum on first N bytes of NFS XDR
101 * - note op and length of XDR request (in bytes)
104 * - free new cache entry
105 * - Send reply (noting info for socket activity check, below)
107 * For cache entries saved above:
108 * - if saved since seqid_refcnt was > 0
109 * - free when seqid_refcnt decrements to 0
110 * (when next one in sequence is processed above, or
111 * when Openowner/Lockowner is discarded)
112 * else { non-idempotent Op(s) }
114 * - some further activity observed on same
116 * (I'm not yet sure how I'm going to do
117 * this. Maybe look at the TCP connection
118 * to see if the send_tcp_sequence# is well
119 * past sent reply OR K additional RPCs
120 * replied on same socket OR?)
122 * - when very old (hours, days, weeks?)
124 * For UDP (v2, 3 only), pretty much the old way:
125 * - key on <xid, NFS version, RPC#, Client host ip#>
126 * (at most one entry for each key)
128 * When a Request arrives:
129 * - if a match with entry via key
130 * - if RPC marked In_progress
131 * - discard request (don't send reply)
134 * - timestamp cache entry
136 * - add entry to cache, marked In_progress
139 * - if RPC# non-idempotent
140 * - mark entry Done (not In_progress)
142 * - timestamp cache entry
147 * Later, entries with saved replies are free'd a short time (few minutes)
148 * after reply sent (timestamp).
149 * Reference: Chet Juszczak, "Improving the Performance and Correctness
150 * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
151 * pages 53-63. San Diego, February 1989.
153 * nfsrc_floodlevel is set to the allowable upper limit for saved replies
154 * for TCP. For V3, a reply won't be saved when the flood level is
155 * hit. For V4, the non-idempotent Op will return NFSERR_RESOURCE in
156 * that case. This level should be set high enough that this almost
160 #include <fs/nfs/nfsport.h>
162 extern struct nfsstats newnfsstats;
164 int nfsrc_floodlevel = NFSRVCACHE_FLOODLEVEL, nfsrc_tcpsavedreplies = 0;
165 #endif /* !APPLEKEXT */
167 static int nfsrc_tcpnonidempotent = 1;
168 static int nfsrc_udphighwater = NFSRVCACHE_UDPHIGHWATER, nfsrc_udpcachesize = 0;
169 static TAILQ_HEAD(, nfsrvcache) nfsrvudplru;
170 static struct nfsrvhashhead nfsrvhashtbl[NFSRVCACHE_HASHSIZE],
171 nfsrvudphashtbl[NFSRVCACHE_HASHSIZE];
173 * and the reverse mapping from generic to Version 2 procedure numbers
175 static int newnfsv2_procid[NFS_V3NPROCS] = {
200 #define NFSRCUDPHASH(xid) \
201 (&nfsrvudphashtbl[((xid) + ((xid) >> 24)) % NFSRVCACHE_HASHSIZE])
202 #define NFSRCHASH(xid) \
203 (&nfsrvhashtbl[((xid) + ((xid) >> 24)) % NFSRVCACHE_HASHSIZE])
206 #define NFSRVCACHE_CHECKLEN 100
208 /* True iff the rpc reply is an nfs status ONLY! */
209 static int nfsv2_repstat[NFS_V3NPROCS] = {
235 * Will NFS want to work over IPv6 someday?
237 #define NETFAMILY(rp) \
238 (((rp)->rc_flag & RC_INETIPV6) ? AF_INET6 : AF_INET)
240 /* local functions */
241 static int nfsrc_getudp(struct nfsrv_descript *nd, struct nfsrvcache *newrp);
242 static int nfsrc_gettcp(struct nfsrv_descript *nd, struct nfsrvcache *newrp);
243 static void nfsrc_lock(struct nfsrvcache *rp);
244 static void nfsrc_unlock(struct nfsrvcache *rp);
245 static void nfsrc_wanted(struct nfsrvcache *rp);
246 static void nfsrc_freecache(struct nfsrvcache *rp);
247 static void nfsrc_trimcache(u_int64_t, struct socket *);
248 static int nfsrc_activesocket(struct nfsrvcache *rp, u_int64_t,
250 static int nfsrc_getlenandcksum(mbuf_t m1, u_int16_t *cksum);
251 static void nfsrc_marksametcpconn(u_int64_t);
254 * Initialize the server request cache list
257 nfsrvd_initcache(void)
260 static int inited = 0;
265 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
266 LIST_INIT(&nfsrvudphashtbl[i]);
267 LIST_INIT(&nfsrvhashtbl[i]);
269 TAILQ_INIT(&nfsrvudplru);
270 nfsrc_tcpsavedreplies = 0;
271 nfsrc_udpcachesize = 0;
272 newnfsstats.srvcache_tcppeak = 0;
273 newnfsstats.srvcache_size = 0;
277 * Get a cache entry for this request. Basically just malloc a new one
278 * and then call nfsrc_getudp() or nfsrc_gettcp() to do the rest.
279 * Call nfsrc_trimcache() to clean up the cache before returning.
282 nfsrvd_getcache(struct nfsrv_descript *nd, struct socket *so)
284 struct nfsrvcache *newrp;
287 if (nd->nd_procnum == NFSPROC_NULL)
288 panic("nfsd cache null");
289 MALLOC(newrp, struct nfsrvcache *, sizeof (struct nfsrvcache),
290 M_NFSRVCACHE, M_WAITOK);
291 NFSBZERO((caddr_t)newrp, sizeof (struct nfsrvcache));
292 if (nd->nd_flag & ND_NFSV4)
293 newrp->rc_flag = RC_NFSV4;
294 else if (nd->nd_flag & ND_NFSV3)
295 newrp->rc_flag = RC_NFSV3;
297 newrp->rc_flag = RC_NFSV2;
298 newrp->rc_xid = nd->nd_retxid;
299 newrp->rc_proc = nd->nd_procnum;
300 newrp->rc_sockref = nd->nd_sockref;
301 newrp->rc_cachetime = nd->nd_tcpconntime;
302 if (nd->nd_flag & ND_SAMETCPCONN)
303 newrp->rc_flag |= RC_SAMETCPCONN;
304 if (nd->nd_nam2 != NULL) {
305 newrp->rc_flag |= RC_UDP;
306 ret = nfsrc_getudp(nd, newrp);
308 ret = nfsrc_gettcp(nd, newrp);
310 nfsrc_trimcache(nd->nd_sockref, so);
317 * - key on <xid, NFS version, RPC#, Client host ip#>
318 * (at most one entry for each key)
321 nfsrc_getudp(struct nfsrv_descript *nd, struct nfsrvcache *newrp)
323 struct nfsrvcache *rp;
324 struct sockaddr_in *saddr;
325 struct sockaddr_in6 *saddr6;
326 struct nfsrvhashhead *hp;
329 hp = NFSRCUDPHASH(newrp->rc_xid);
332 LIST_FOREACH(rp, hp, rc_hash) {
333 if (newrp->rc_xid == rp->rc_xid &&
334 newrp->rc_proc == rp->rc_proc &&
335 (newrp->rc_flag & rp->rc_flag & RC_NFSVERS) &&
336 nfsaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
337 if ((rp->rc_flag & RC_LOCKED) != 0) {
338 rp->rc_flag |= RC_WANTED;
339 (void)mtx_sleep(rp, NFSCACHEMUTEXPTR,
340 (PZERO - 1) | PDROP, "nfsrc", 10 * hz);
343 if (rp->rc_flag == 0)
344 panic("nfs udp cache0");
345 rp->rc_flag |= RC_LOCKED;
346 TAILQ_REMOVE(&nfsrvudplru, rp, rc_lru);
347 TAILQ_INSERT_TAIL(&nfsrvudplru, rp, rc_lru);
348 if (rp->rc_flag & RC_INPROG) {
349 newnfsstats.srvcache_inproghits++;
352 } else if (rp->rc_flag & RC_REPSTATUS) {
356 newnfsstats.srvcache_nonidemdonehits++;
359 *(nd->nd_errp) = rp->rc_status;
361 rp->rc_timestamp = NFSD_MONOSEC +
362 NFSRVCACHE_UDPTIMEOUT;
363 } else if (rp->rc_flag & RC_REPMBUF) {
364 newnfsstats.srvcache_nonidemdonehits++;
366 nd->nd_mreq = m_copym(rp->rc_reply, 0,
369 rp->rc_timestamp = NFSD_MONOSEC +
370 NFSRVCACHE_UDPTIMEOUT;
372 panic("nfs udp cache1");
375 free((caddr_t)newrp, M_NFSRVCACHE);
379 newnfsstats.srvcache_misses++;
380 newnfsstats.srvcache_size++;
381 nfsrc_udpcachesize++;
383 newrp->rc_flag |= RC_INPROG;
384 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
385 if (saddr->sin_family == AF_INET)
386 newrp->rc_inet = saddr->sin_addr.s_addr;
387 else if (saddr->sin_family == AF_INET6) {
388 saddr6 = (struct sockaddr_in6 *)saddr;
389 NFSBCOPY((caddr_t)&saddr6->sin6_addr, (caddr_t)&newrp->rc_inet6,
390 sizeof (struct in6_addr));
391 newrp->rc_flag |= RC_INETIPV6;
393 LIST_INSERT_HEAD(hp, newrp, rc_hash);
394 TAILQ_INSERT_TAIL(&nfsrvudplru, newrp, rc_lru);
405 * Update a request cache entry after the rpc has been done
407 APPLESTATIC struct nfsrvcache *
408 nfsrvd_updatecache(struct nfsrv_descript *nd, struct socket *so)
410 struct nfsrvcache *rp;
411 struct nfsrvcache *retrp = NULL;
416 panic("nfsrvd_updatecache null rp");
420 if (!(rp->rc_flag & RC_INPROG))
421 panic("nfsrvd_updatecache not inprog");
422 rp->rc_flag &= ~RC_INPROG;
423 if (rp->rc_flag & RC_UDP) {
424 TAILQ_REMOVE(&nfsrvudplru, rp, rc_lru);
425 TAILQ_INSERT_TAIL(&nfsrvudplru, rp, rc_lru);
429 * Reply from cache is a special case returned by nfsrv_checkseqid().
431 if (nd->nd_repstat == NFSERR_REPLYFROMCACHE) {
432 newnfsstats.srvcache_nonidemdonehits++;
436 mbuf_freem(nd->nd_mreq);
437 if (!(rp->rc_flag & RC_REPMBUF))
438 panic("reply from cache");
439 nd->nd_mreq = m_copym(rp->rc_reply, 0,
441 rp->rc_timestamp = NFSD_MONOSEC + NFSRVCACHE_TCPTIMEOUT;
447 * If rc_refcnt > 0, save it
448 * For UDP, save it if ND_SAVEREPLY is set
449 * For TCP, save it if ND_SAVEREPLY and nfsrc_tcpnonidempotent is set
451 if (nd->nd_repstat != NFSERR_DONTREPLY &&
452 (rp->rc_refcnt > 0 ||
453 ((nd->nd_flag & ND_SAVEREPLY) && (rp->rc_flag & RC_UDP)) ||
454 ((nd->nd_flag & ND_SAVEREPLY) && !(rp->rc_flag & RC_UDP) &&
455 nfsrc_tcpsavedreplies <= nfsrc_floodlevel &&
456 nfsrc_tcpnonidempotent))) {
457 if (rp->rc_refcnt > 0) {
458 if (!(rp->rc_flag & RC_NFSV4))
459 panic("update_cache refcnt");
460 rp->rc_flag |= RC_REFCNT;
462 if ((nd->nd_flag & ND_NFSV2) &&
463 nfsv2_repstat[newnfsv2_procid[nd->nd_procnum]]) {
464 rp->rc_status = nd->nd_repstat;
465 rp->rc_flag |= RC_REPSTATUS;
468 if (!(rp->rc_flag & RC_UDP)) {
469 nfsrc_tcpsavedreplies++;
470 if (nfsrc_tcpsavedreplies >
471 newnfsstats.srvcache_tcppeak)
472 newnfsstats.srvcache_tcppeak =
473 nfsrc_tcpsavedreplies;
476 m = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAIT);
479 rp->rc_flag |= RC_REPMBUF;
482 if (rp->rc_flag & RC_UDP) {
483 rp->rc_timestamp = NFSD_MONOSEC +
484 NFSRVCACHE_UDPTIMEOUT;
487 rp->rc_timestamp = NFSD_MONOSEC +
488 NFSRVCACHE_TCPTIMEOUT;
489 if (rp->rc_refcnt > 0)
500 nfsrc_trimcache(nd->nd_sockref, so);
506 * Invalidate and, if possible, free an in prog cache entry.
510 nfsrvd_delcache(struct nfsrvcache *rp)
513 if (!(rp->rc_flag & RC_INPROG))
514 panic("nfsrvd_delcache not in prog");
516 rp->rc_flag &= ~RC_INPROG;
517 if (rp->rc_refcnt == 0 && !(rp->rc_flag & RC_LOCKED))
523 * Called after nfsrvd_updatecache() once the reply is sent, to update
524 * the entry for nfsrc_activesocket() and unlock it. The argument is
525 * the pointer returned by nfsrvd_updatecache().
528 nfsrvd_sentcache(struct nfsrvcache *rp, struct socket *so, int err)
532 if (!(rp->rc_flag & RC_LOCKED))
533 panic("nfsrvd_sentcache not locked");
535 if ((so->so_proto->pr_domain->dom_family != AF_INET &&
536 so->so_proto->pr_domain->dom_family != AF_INET6) ||
537 so->so_proto->pr_protocol != IPPROTO_TCP)
538 panic("nfs sent cache");
539 if (nfsrv_getsockseqnum(so, &tmp_seq)) {
541 rp->rc_tcpseq = tmp_seq;
542 rp->rc_flag |= RC_TCPSEQ;
550 * Get a cache entry for TCP
551 * - key on <xid, nfs version>
552 * (allow multiple entries for a given key)
555 nfsrc_gettcp(struct nfsrv_descript *nd, struct nfsrvcache *newrp)
557 struct nfsrvcache *rp, *nextrp;
559 struct nfsrvcache *hitrp;
560 struct nfsrvhashhead *hp, nfsrc_templist;
563 hp = NFSRCHASH(newrp->rc_xid);
564 newrp->rc_reqlen = nfsrc_getlenandcksum(nd->nd_mrep, &newrp->rc_cksum);
568 LIST_INIT(&nfsrc_templist);
570 * Get all the matches and put them on the temp list.
573 while (rp != LIST_END(hp)) {
574 nextrp = LIST_NEXT(rp, rc_hash);
575 if (newrp->rc_xid == rp->rc_xid &&
576 (!(rp->rc_flag & RC_INPROG) ||
577 ((newrp->rc_flag & RC_SAMETCPCONN) &&
578 newrp->rc_sockref == rp->rc_sockref)) &&
579 (newrp->rc_flag & rp->rc_flag & RC_NFSVERS) &&
580 newrp->rc_proc == rp->rc_proc &&
581 ((newrp->rc_flag & RC_NFSV4) &&
582 newrp->rc_sockref != rp->rc_sockref &&
583 newrp->rc_cachetime >= rp->rc_cachetime)
584 && newrp->rc_reqlen == rp->rc_reqlen &&
585 newrp->rc_cksum == rp->rc_cksum) {
586 LIST_REMOVE(rp, rc_hash);
587 LIST_INSERT_HEAD(&nfsrc_templist, rp, rc_hash);
593 * Now, use nfsrc_templist to decide if there is a match.
596 LIST_FOREACH(rp, &nfsrc_templist, rc_hash) {
598 if (rp->rc_refcnt > 0) {
604 * Can be a hit only if one entry left.
605 * Note possible hit entry and put nfsrc_templist back on hash
610 hitrp = rp = LIST_FIRST(&nfsrc_templist);
611 while (rp != LIST_END(&nfsrc_templist)) {
612 nextrp = LIST_NEXT(rp, rc_hash);
613 LIST_REMOVE(rp, rc_hash);
614 LIST_INSERT_HEAD(hp, rp, rc_hash);
617 if (LIST_FIRST(&nfsrc_templist) != LIST_END(&nfsrc_templist))
618 panic("nfs gettcp cache templist");
622 if ((rp->rc_flag & RC_LOCKED) != 0) {
623 rp->rc_flag |= RC_WANTED;
624 (void)mtx_sleep(rp, NFSCACHEMUTEXPTR,
625 (PZERO - 1) | PDROP, "nfsrc", 10 * hz);
628 if (rp->rc_flag == 0)
629 panic("nfs tcp cache0");
630 rp->rc_flag |= RC_LOCKED;
631 if (rp->rc_flag & RC_INPROG) {
632 newnfsstats.srvcache_inproghits++;
634 if (newrp->rc_sockref == rp->rc_sockref)
635 nfsrc_marksametcpconn(rp->rc_sockref);
637 } else if (rp->rc_flag & RC_REPSTATUS) {
641 newnfsstats.srvcache_nonidemdonehits++;
643 if (newrp->rc_sockref == rp->rc_sockref)
644 nfsrc_marksametcpconn(rp->rc_sockref);
647 *(nd->nd_errp) = rp->rc_status;
648 rp->rc_timestamp = NFSD_MONOSEC +
649 NFSRVCACHE_TCPTIMEOUT;
650 } else if (rp->rc_flag & RC_REPMBUF) {
651 newnfsstats.srvcache_nonidemdonehits++;
653 if (newrp->rc_sockref == rp->rc_sockref)
654 nfsrc_marksametcpconn(rp->rc_sockref);
656 nd->nd_mreq = m_copym(rp->rc_reply, 0,
658 rp->rc_timestamp = NFSD_MONOSEC +
659 NFSRVCACHE_TCPTIMEOUT;
661 panic("nfs tcp cache1");
664 free((caddr_t)newrp, M_NFSRVCACHE);
667 newnfsstats.srvcache_misses++;
668 newnfsstats.srvcache_size++;
671 * For TCP, multiple entries for a key are allowed, so don't
672 * chain it into the hash table until done.
674 newrp->rc_cachetime = NFSD_MONOSEC;
675 newrp->rc_flag |= RC_INPROG;
676 LIST_INSERT_HEAD(hp, newrp, rc_hash);
687 * Lock a cache entry.
688 * Also puts a mutex lock on the cache list.
691 nfsrc_lock(struct nfsrvcache *rp)
693 NFSCACHELOCKREQUIRED();
694 while ((rp->rc_flag & RC_LOCKED) != 0) {
695 rp->rc_flag |= RC_WANTED;
696 (void)mtx_sleep(rp, NFSCACHEMUTEXPTR, PZERO - 1,
699 rp->rc_flag |= RC_LOCKED;
703 * Unlock a cache entry.
706 nfsrc_unlock(struct nfsrvcache *rp)
710 rp->rc_flag &= ~RC_LOCKED;
716 * Wakeup anyone wanting entry.
719 nfsrc_wanted(struct nfsrvcache *rp)
721 if (rp->rc_flag & RC_WANTED) {
722 rp->rc_flag &= ~RC_WANTED;
732 nfsrc_freecache(struct nfsrvcache *rp)
735 NFSCACHELOCKREQUIRED();
736 LIST_REMOVE(rp, rc_hash);
737 if (rp->rc_flag & RC_UDP) {
738 TAILQ_REMOVE(&nfsrvudplru, rp, rc_lru);
739 nfsrc_udpcachesize--;
742 if (rp->rc_flag & RC_REPMBUF) {
743 mbuf_freem(rp->rc_reply);
744 if (!(rp->rc_flag & RC_UDP))
745 nfsrc_tcpsavedreplies--;
747 FREE((caddr_t)rp, M_NFSRVCACHE);
748 newnfsstats.srvcache_size--;
752 * Clean out the cache. Called when nfsserver module is unloaded.
755 nfsrvd_cleancache(void)
757 struct nfsrvcache *rp, *nextrp;
761 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
762 LIST_FOREACH_SAFE(rp, &nfsrvhashtbl[i], rc_hash, nextrp) {
766 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
767 LIST_FOREACH_SAFE(rp, &nfsrvudphashtbl[i], rc_hash, nextrp) {
771 newnfsstats.srvcache_size = 0;
772 nfsrc_tcpsavedreplies = 0;
777 * The basic rule is to get rid of entries that are expired.
780 nfsrc_trimcache(u_int64_t sockref, struct socket *so)
782 struct nfsrvcache *rp, *nextrp;
786 TAILQ_FOREACH_SAFE(rp, &nfsrvudplru, rc_lru, nextrp) {
787 if (!(rp->rc_flag & (RC_INPROG|RC_LOCKED|RC_WANTED))
788 && rp->rc_refcnt == 0
789 && ((rp->rc_flag & RC_REFCNT) ||
790 NFSD_MONOSEC > rp->rc_timestamp ||
791 nfsrc_udpcachesize > nfsrc_udphighwater))
794 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
795 LIST_FOREACH_SAFE(rp, &nfsrvhashtbl[i], rc_hash, nextrp) {
796 if (!(rp->rc_flag & (RC_INPROG|RC_LOCKED|RC_WANTED))
797 && rp->rc_refcnt == 0
798 && ((rp->rc_flag & RC_REFCNT) ||
799 NFSD_MONOSEC > rp->rc_timestamp ||
800 nfsrc_activesocket(rp, sockref, so)))
808 * Add a seqid# reference to the cache entry.
811 nfsrvd_refcache(struct nfsrvcache *rp)
815 if (rp->rc_refcnt < 0)
816 panic("nfs cache refcnt");
822 * Dereference a seqid# cache entry.
825 nfsrvd_derefcache(struct nfsrvcache *rp)
829 if (rp->rc_refcnt <= 0)
830 panic("nfs cache derefcnt");
832 if (rp->rc_refcnt == 0 && !(rp->rc_flag & (RC_LOCKED | RC_INPROG)))
838 * Check to see if the socket is active.
839 * Return 1 if the reply has been received/acknowledged by the client,
841 * XXX - Uses tcp internals.
844 nfsrc_activesocket(struct nfsrvcache *rp, u_int64_t cur_sockref,
845 struct socket *cur_so)
849 if (!(rp->rc_flag & RC_TCPSEQ))
852 * If the sockref is the same, it is the same TCP connection.
854 if (cur_sockref == rp->rc_sockref)
855 ret = nfsrv_checksockseqnum(cur_so, rp->rc_tcpseq);
860 * Calculate the length of the mbuf list and a checksum on the first up to
861 * NFSRVCACHE_CHECKLEN bytes.
864 nfsrc_getlenandcksum(mbuf_t m1, u_int16_t *cksum)
874 cklen = (len > NFSRVCACHE_CHECKLEN) ? NFSRVCACHE_CHECKLEN : len;
875 *cksum = in_cksum(m1, cklen);
880 * Mark a TCP connection that is seeing retries. Should never happen for
884 nfsrc_marksametcpconn(u_int64_t sockref)