2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/queue.h>
41 #include <rpc/replay.h>
43 struct replay_cache_entry {
45 struct rpc_msg rce_msg;
46 struct sockaddr_storage rce_addr;
47 struct rpc_msg rce_repmsg;
48 struct mbuf *rce_repbody;
50 TAILQ_ENTRY(replay_cache_entry) rce_link;
51 TAILQ_ENTRY(replay_cache_entry) rce_alllink;
53 TAILQ_HEAD(replay_cache_list, replay_cache_entry);
55 static struct replay_cache_entry *
56 replay_alloc(struct replay_cache *rc, struct rpc_msg *msg,
57 struct sockaddr *addr, int h);
58 static void replay_free(struct replay_cache *rc,
59 struct replay_cache_entry *rce);
60 static void replay_prune(struct replay_cache *rc);
62 #define REPLAY_HASH_SIZE 256
63 #define REPLAY_MAX 1024
66 struct replay_cache_list rc_cache[REPLAY_HASH_SIZE];
67 struct replay_cache_list rc_all;
75 replay_newcache(size_t maxsize)
77 struct replay_cache *rc;
80 rc = malloc(sizeof(*rc), M_RPC, M_WAITOK|M_ZERO);
81 for (i = 0; i < REPLAY_HASH_SIZE; i++)
82 TAILQ_INIT(&rc->rc_cache[i]);
83 TAILQ_INIT(&rc->rc_all);
84 mtx_init(&rc->rc_lock, "rc_lock", NULL, MTX_DEF);
85 rc->rc_maxsize = maxsize;
91 replay_setsize(struct replay_cache *rc, size_t newmaxsize)
94 mtx_lock(&rc->rc_lock);
95 rc->rc_maxsize = newmaxsize;
97 mtx_unlock(&rc->rc_lock);
101 replay_freecache(struct replay_cache *rc)
104 mtx_lock(&rc->rc_lock);
105 while (TAILQ_FIRST(&rc->rc_all))
106 replay_free(rc, TAILQ_FIRST(&rc->rc_all));
107 mtx_destroy(&rc->rc_lock);
111 static struct replay_cache_entry *
112 replay_alloc(struct replay_cache *rc,
113 struct rpc_msg *msg, struct sockaddr *addr, int h)
115 struct replay_cache_entry *rce;
117 mtx_assert(&rc->rc_lock, MA_OWNED);
120 rce = malloc(sizeof(*rce), M_RPC, M_NOWAIT|M_ZERO);
125 bcopy(addr, &rce->rce_addr, addr->sa_len);
127 TAILQ_INSERT_HEAD(&rc->rc_cache[h], rce, rce_link);
128 TAILQ_INSERT_HEAD(&rc->rc_all, rce, rce_alllink);
134 replay_free(struct replay_cache *rc, struct replay_cache_entry *rce)
137 mtx_assert(&rc->rc_lock, MA_OWNED);
140 TAILQ_REMOVE(&rc->rc_cache[rce->rce_hash], rce, rce_link);
141 TAILQ_REMOVE(&rc->rc_all, rce, rce_alllink);
142 if (rce->rce_repbody) {
143 rc->rc_size -= m_length(rce->rce_repbody, NULL);
144 m_freem(rce->rce_repbody);
150 replay_prune(struct replay_cache *rc)
152 struct replay_cache_entry *rce;
154 mtx_assert(&rc->rc_lock, MA_OWNED);
156 if (rc->rc_count < REPLAY_MAX && rc->rc_size <= rc->rc_maxsize)
161 * Try to free an entry. Don't free in-progress entries.
163 TAILQ_FOREACH_REVERSE(rce, &rc->rc_all, replay_cache_list,
165 if (rce->rce_repmsg.rm_xid)
169 replay_free(rc, rce);
170 } while (rce && (rc->rc_count >= REPLAY_MAX
171 || rc->rc_size > rc->rc_maxsize));
175 replay_find(struct replay_cache *rc, struct rpc_msg *msg,
176 struct sockaddr *addr, struct rpc_msg *repmsg, struct mbuf **mp)
178 int h = HASHSTEP(HASHINIT, msg->rm_xid) % REPLAY_HASH_SIZE;
179 struct replay_cache_entry *rce;
181 mtx_lock(&rc->rc_lock);
182 TAILQ_FOREACH(rce, &rc->rc_cache[h], rce_link) {
183 if (rce->rce_msg.rm_xid == msg->rm_xid
184 && rce->rce_msg.rm_call.cb_prog == msg->rm_call.cb_prog
185 && rce->rce_msg.rm_call.cb_vers == msg->rm_call.cb_vers
186 && rce->rce_msg.rm_call.cb_proc == msg->rm_call.cb_proc
187 && rce->rce_addr.ss_len == addr->sa_len
188 && bcmp(&rce->rce_addr, addr, addr->sa_len) == 0) {
189 if (rce->rce_repmsg.rm_xid) {
191 * We have a reply for this
192 * message. Copy it and return. Keep
193 * replay_all LRU sorted
195 TAILQ_REMOVE(&rc->rc_all, rce, rce_alllink);
196 TAILQ_INSERT_HEAD(&rc->rc_all, rce,
198 *repmsg = rce->rce_repmsg;
199 if (rce->rce_repbody) {
200 *mp = m_copym(rce->rce_repbody,
201 0, M_COPYALL, M_NOWAIT);
202 mtx_unlock(&rc->rc_lock);
206 mtx_unlock(&rc->rc_lock);
210 mtx_unlock(&rc->rc_lock);
211 return (RS_INPROGRESS);
218 rce = replay_alloc(rc, msg, addr, h);
220 mtx_unlock(&rc->rc_lock);
229 replay_setreply(struct replay_cache *rc,
230 struct rpc_msg *repmsg, struct sockaddr *addr, struct mbuf *m)
232 int h = HASHSTEP(HASHINIT, repmsg->rm_xid) % REPLAY_HASH_SIZE;
233 struct replay_cache_entry *rce;
236 * Copy the reply before the lock so we can sleep.
239 m = m_copym(m, 0, M_COPYALL, M_WAITOK);
241 mtx_lock(&rc->rc_lock);
242 TAILQ_FOREACH(rce, &rc->rc_cache[h], rce_link) {
243 if (rce->rce_msg.rm_xid == repmsg->rm_xid
244 && rce->rce_addr.ss_len == addr->sa_len
245 && bcmp(&rce->rce_addr, addr, addr->sa_len) == 0) {
250 rce->rce_repmsg = *repmsg;
251 rce->rce_repbody = m;
253 rc->rc_size += m_length(m, NULL);
255 mtx_unlock(&rc->rc_lock);