2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
36 #include <sys/mutex.h>
37 #include <sys/queue.h>
40 #include <rpc/replay.h>
42 struct replay_cache_entry {
44 struct rpc_msg rce_msg;
45 struct sockaddr_storage rce_addr;
46 struct rpc_msg rce_repmsg;
47 struct mbuf *rce_repbody;
49 TAILQ_ENTRY(replay_cache_entry) rce_link;
50 TAILQ_ENTRY(replay_cache_entry) rce_alllink;
52 TAILQ_HEAD(replay_cache_list, replay_cache_entry);
54 static struct replay_cache_entry *
55 replay_alloc(struct replay_cache *rc, struct rpc_msg *msg,
56 struct sockaddr *addr, int h);
57 static void replay_free(struct replay_cache *rc,
58 struct replay_cache_entry *rce);
59 static void replay_prune(struct replay_cache *rc);
61 #define REPLAY_HASH_SIZE 256
62 #define REPLAY_MAX 1024
65 struct replay_cache_list rc_cache[REPLAY_HASH_SIZE];
66 struct replay_cache_list rc_all;
74 replay_newcache(size_t maxsize)
76 struct replay_cache *rc;
79 rc = malloc(sizeof(*rc), M_RPC, M_WAITOK|M_ZERO);
80 for (i = 0; i < REPLAY_HASH_SIZE; i++)
81 TAILQ_INIT(&rc->rc_cache[i]);
82 TAILQ_INIT(&rc->rc_all);
83 mtx_init(&rc->rc_lock, "rc_lock", NULL, MTX_DEF);
84 rc->rc_maxsize = maxsize;
90 replay_setsize(struct replay_cache *rc, size_t newmaxsize)
93 mtx_lock(&rc->rc_lock);
94 rc->rc_maxsize = newmaxsize;
96 mtx_unlock(&rc->rc_lock);
100 replay_freecache(struct replay_cache *rc)
103 mtx_lock(&rc->rc_lock);
104 while (TAILQ_FIRST(&rc->rc_all))
105 replay_free(rc, TAILQ_FIRST(&rc->rc_all));
106 mtx_destroy(&rc->rc_lock);
110 static struct replay_cache_entry *
111 replay_alloc(struct replay_cache *rc,
112 struct rpc_msg *msg, struct sockaddr *addr, int h)
114 struct replay_cache_entry *rce;
116 mtx_assert(&rc->rc_lock, MA_OWNED);
119 rce = malloc(sizeof(*rce), M_RPC, M_NOWAIT|M_ZERO);
124 bcopy(addr, &rce->rce_addr, addr->sa_len);
126 TAILQ_INSERT_HEAD(&rc->rc_cache[h], rce, rce_link);
127 TAILQ_INSERT_HEAD(&rc->rc_all, rce, rce_alllink);
133 replay_free(struct replay_cache *rc, struct replay_cache_entry *rce)
136 mtx_assert(&rc->rc_lock, MA_OWNED);
139 TAILQ_REMOVE(&rc->rc_cache[rce->rce_hash], rce, rce_link);
140 TAILQ_REMOVE(&rc->rc_all, rce, rce_alllink);
141 if (rce->rce_repbody) {
142 rc->rc_size -= m_length(rce->rce_repbody, NULL);
143 m_freem(rce->rce_repbody);
149 replay_prune(struct replay_cache *rc)
151 struct replay_cache_entry *rce;
153 mtx_assert(&rc->rc_lock, MA_OWNED);
155 if (rc->rc_count < REPLAY_MAX && rc->rc_size <= rc->rc_maxsize)
160 * Try to free an entry. Don't free in-progress entries.
162 TAILQ_FOREACH_REVERSE(rce, &rc->rc_all, replay_cache_list,
164 if (rce->rce_repmsg.rm_xid)
168 replay_free(rc, rce);
169 } while (rce && (rc->rc_count >= REPLAY_MAX
170 || rc->rc_size > rc->rc_maxsize));
174 replay_find(struct replay_cache *rc, struct rpc_msg *msg,
175 struct sockaddr *addr, struct rpc_msg *repmsg, struct mbuf **mp)
177 int h = HASHSTEP(HASHINIT, msg->rm_xid) % REPLAY_HASH_SIZE;
178 struct replay_cache_entry *rce;
180 mtx_lock(&rc->rc_lock);
181 TAILQ_FOREACH(rce, &rc->rc_cache[h], rce_link) {
182 if (rce->rce_msg.rm_xid == msg->rm_xid
183 && rce->rce_msg.rm_call.cb_prog == msg->rm_call.cb_prog
184 && rce->rce_msg.rm_call.cb_vers == msg->rm_call.cb_vers
185 && rce->rce_msg.rm_call.cb_proc == msg->rm_call.cb_proc
186 && rce->rce_addr.ss_len == addr->sa_len
187 && bcmp(&rce->rce_addr, addr, addr->sa_len) == 0) {
188 if (rce->rce_repmsg.rm_xid) {
190 * We have a reply for this
191 * message. Copy it and return. Keep
192 * replay_all LRU sorted
194 TAILQ_REMOVE(&rc->rc_all, rce, rce_alllink);
195 TAILQ_INSERT_HEAD(&rc->rc_all, rce,
197 *repmsg = rce->rce_repmsg;
198 if (rce->rce_repbody) {
199 *mp = m_copym(rce->rce_repbody,
200 0, M_COPYALL, M_NOWAIT);
201 mtx_unlock(&rc->rc_lock);
205 mtx_unlock(&rc->rc_lock);
209 mtx_unlock(&rc->rc_lock);
210 return (RS_INPROGRESS);
217 rce = replay_alloc(rc, msg, addr, h);
219 mtx_unlock(&rc->rc_lock);
228 replay_setreply(struct replay_cache *rc,
229 struct rpc_msg *repmsg, struct sockaddr *addr, struct mbuf *m)
231 int h = HASHSTEP(HASHINIT, repmsg->rm_xid) % REPLAY_HASH_SIZE;
232 struct replay_cache_entry *rce;
235 * Copy the reply before the lock so we can sleep.
238 m = m_copym(m, 0, M_COPYALL, M_WAITOK);
240 mtx_lock(&rc->rc_lock);
241 TAILQ_FOREACH(rce, &rc->rc_cache[h], rce_link) {
242 if (rce->rce_msg.rm_xid == repmsg->rm_xid
243 && rce->rce_addr.ss_len == addr->sa_len
244 && bcmp(&rce->rce_addr, addr, addr->sa_len) == 0) {
249 rce->rce_repmsg = *repmsg;
250 rce->rce_repbody = m;
252 rc->rc_size += m_length(m, NULL);
254 mtx_unlock(&rc->rc_lock);