1a9148abdSDoug Rabson /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
4a9148abdSDoug Rabson * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5a9148abdSDoug Rabson * Authors: Doug Rabson <dfr@rabson.org>
6a9148abdSDoug Rabson * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7a9148abdSDoug Rabson *
8a9148abdSDoug Rabson * Redistribution and use in source and binary forms, with or without
9a9148abdSDoug Rabson * modification, are permitted provided that the following conditions
10a9148abdSDoug Rabson * are met:
11a9148abdSDoug Rabson * 1. Redistributions of source code must retain the above copyright
12a9148abdSDoug Rabson * notice, this list of conditions and the following disclaimer.
13a9148abdSDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
14a9148abdSDoug Rabson * notice, this list of conditions and the following disclaimer in the
15a9148abdSDoug Rabson * documentation and/or other materials provided with the distribution.
16a9148abdSDoug Rabson *
17a9148abdSDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18a9148abdSDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19a9148abdSDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20a9148abdSDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21a9148abdSDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22a9148abdSDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23a9148abdSDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24a9148abdSDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25a9148abdSDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26a9148abdSDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27a9148abdSDoug Rabson * SUCH DAMAGE.
28a9148abdSDoug Rabson */
29a9148abdSDoug Rabson
30a9148abdSDoug Rabson #include <sys/param.h>
31a9148abdSDoug Rabson #include <sys/hash.h>
32a9148abdSDoug Rabson #include <sys/kernel.h>
33a9148abdSDoug Rabson #include <sys/lock.h>
348ec07310SGleb Smirnoff #include <sys/malloc.h>
35a9148abdSDoug Rabson #include <sys/mbuf.h>
36a9148abdSDoug Rabson #include <sys/mutex.h>
37a9148abdSDoug Rabson #include <sys/queue.h>
38a9148abdSDoug Rabson
39a9148abdSDoug Rabson #include <rpc/rpc.h>
40a9148abdSDoug Rabson #include <rpc/replay.h>
41a9148abdSDoug Rabson
42a9148abdSDoug Rabson struct replay_cache_entry {
43a9148abdSDoug Rabson int rce_hash;
44a9148abdSDoug Rabson struct rpc_msg rce_msg;
45a9148abdSDoug Rabson struct sockaddr_storage rce_addr;
46a9148abdSDoug Rabson struct rpc_msg rce_repmsg;
47a9148abdSDoug Rabson struct mbuf *rce_repbody;
48a9148abdSDoug Rabson
49a9148abdSDoug Rabson TAILQ_ENTRY(replay_cache_entry) rce_link;
50a9148abdSDoug Rabson TAILQ_ENTRY(replay_cache_entry) rce_alllink;
51a9148abdSDoug Rabson };
52a9148abdSDoug Rabson TAILQ_HEAD(replay_cache_list, replay_cache_entry);
53a9148abdSDoug Rabson
54a9148abdSDoug Rabson static struct replay_cache_entry *
55a9148abdSDoug Rabson replay_alloc(struct replay_cache *rc, struct rpc_msg *msg,
56a9148abdSDoug Rabson struct sockaddr *addr, int h);
57a9148abdSDoug Rabson static void replay_free(struct replay_cache *rc,
58a9148abdSDoug Rabson struct replay_cache_entry *rce);
59a9148abdSDoug Rabson static void replay_prune(struct replay_cache *rc);
60a9148abdSDoug Rabson
61a9148abdSDoug Rabson #define REPLAY_HASH_SIZE 256
62a9148abdSDoug Rabson #define REPLAY_MAX 1024
63a9148abdSDoug Rabson
64a9148abdSDoug Rabson struct replay_cache {
65a9148abdSDoug Rabson struct replay_cache_list rc_cache[REPLAY_HASH_SIZE];
66a9148abdSDoug Rabson struct replay_cache_list rc_all;
67a9148abdSDoug Rabson struct mtx rc_lock;
68a9148abdSDoug Rabson int rc_count;
69a9148abdSDoug Rabson size_t rc_size;
70a9148abdSDoug Rabson size_t rc_maxsize;
71a9148abdSDoug Rabson };
72a9148abdSDoug Rabson
73a9148abdSDoug Rabson struct replay_cache *
replay_newcache(size_t maxsize)74a9148abdSDoug Rabson replay_newcache(size_t maxsize)
75a9148abdSDoug Rabson {
76a9148abdSDoug Rabson struct replay_cache *rc;
77a9148abdSDoug Rabson int i;
78a9148abdSDoug Rabson
79a9148abdSDoug Rabson rc = malloc(sizeof(*rc), M_RPC, M_WAITOK|M_ZERO);
80a9148abdSDoug Rabson for (i = 0; i < REPLAY_HASH_SIZE; i++)
81a9148abdSDoug Rabson TAILQ_INIT(&rc->rc_cache[i]);
82a9148abdSDoug Rabson TAILQ_INIT(&rc->rc_all);
83a9148abdSDoug Rabson mtx_init(&rc->rc_lock, "rc_lock", NULL, MTX_DEF);
84a9148abdSDoug Rabson rc->rc_maxsize = maxsize;
85a9148abdSDoug Rabson
86a9148abdSDoug Rabson return (rc);
87a9148abdSDoug Rabson }
88a9148abdSDoug Rabson
89a9148abdSDoug Rabson void
replay_setsize(struct replay_cache * rc,size_t newmaxsize)90a9148abdSDoug Rabson replay_setsize(struct replay_cache *rc, size_t newmaxsize)
91a9148abdSDoug Rabson {
92a9148abdSDoug Rabson
93d7dc2db4SRick Macklem mtx_lock(&rc->rc_lock);
94a9148abdSDoug Rabson rc->rc_maxsize = newmaxsize;
95a9148abdSDoug Rabson replay_prune(rc);
96d7dc2db4SRick Macklem mtx_unlock(&rc->rc_lock);
97a9148abdSDoug Rabson }
98a9148abdSDoug Rabson
99a9148abdSDoug Rabson void
replay_freecache(struct replay_cache * rc)100a9148abdSDoug Rabson replay_freecache(struct replay_cache *rc)
101a9148abdSDoug Rabson {
102a9148abdSDoug Rabson
103a9148abdSDoug Rabson mtx_lock(&rc->rc_lock);
104a9148abdSDoug Rabson while (TAILQ_FIRST(&rc->rc_all))
105a9148abdSDoug Rabson replay_free(rc, TAILQ_FIRST(&rc->rc_all));
106a9148abdSDoug Rabson mtx_destroy(&rc->rc_lock);
107a9148abdSDoug Rabson free(rc, M_RPC);
108a9148abdSDoug Rabson }
109a9148abdSDoug Rabson
110a9148abdSDoug Rabson static struct replay_cache_entry *
replay_alloc(struct replay_cache * rc,struct rpc_msg * msg,struct sockaddr * addr,int h)111a9148abdSDoug Rabson replay_alloc(struct replay_cache *rc,
112a9148abdSDoug Rabson struct rpc_msg *msg, struct sockaddr *addr, int h)
113a9148abdSDoug Rabson {
114a9148abdSDoug Rabson struct replay_cache_entry *rce;
115a9148abdSDoug Rabson
1160778b1d1SPawel Jakub Dawidek mtx_assert(&rc->rc_lock, MA_OWNED);
1170778b1d1SPawel Jakub Dawidek
118a9148abdSDoug Rabson rc->rc_count++;
119a9148abdSDoug Rabson rce = malloc(sizeof(*rce), M_RPC, M_NOWAIT|M_ZERO);
1200778b1d1SPawel Jakub Dawidek if (!rce)
1210778b1d1SPawel Jakub Dawidek return (NULL);
122a9148abdSDoug Rabson rce->rce_hash = h;
123a9148abdSDoug Rabson rce->rce_msg = *msg;
124a9148abdSDoug Rabson bcopy(addr, &rce->rce_addr, addr->sa_len);
125a9148abdSDoug Rabson
126a9148abdSDoug Rabson TAILQ_INSERT_HEAD(&rc->rc_cache[h], rce, rce_link);
127a9148abdSDoug Rabson TAILQ_INSERT_HEAD(&rc->rc_all, rce, rce_alllink);
128a9148abdSDoug Rabson
129a9148abdSDoug Rabson return (rce);
130a9148abdSDoug Rabson }
131a9148abdSDoug Rabson
132a9148abdSDoug Rabson static void
replay_free(struct replay_cache * rc,struct replay_cache_entry * rce)133a9148abdSDoug Rabson replay_free(struct replay_cache *rc, struct replay_cache_entry *rce)
134a9148abdSDoug Rabson {
135a9148abdSDoug Rabson
1360778b1d1SPawel Jakub Dawidek mtx_assert(&rc->rc_lock, MA_OWNED);
1370778b1d1SPawel Jakub Dawidek
138a9148abdSDoug Rabson rc->rc_count--;
139a9148abdSDoug Rabson TAILQ_REMOVE(&rc->rc_cache[rce->rce_hash], rce, rce_link);
140a9148abdSDoug Rabson TAILQ_REMOVE(&rc->rc_all, rce, rce_alllink);
141a9148abdSDoug Rabson if (rce->rce_repbody) {
142a9148abdSDoug Rabson rc->rc_size -= m_length(rce->rce_repbody, NULL);
143a9148abdSDoug Rabson m_freem(rce->rce_repbody);
144a9148abdSDoug Rabson }
145a9148abdSDoug Rabson free(rce, M_RPC);
146a9148abdSDoug Rabson }
147a9148abdSDoug Rabson
148a9148abdSDoug Rabson static void
replay_prune(struct replay_cache * rc)149a9148abdSDoug Rabson replay_prune(struct replay_cache *rc)
150a9148abdSDoug Rabson {
151a9148abdSDoug Rabson struct replay_cache_entry *rce;
152a9148abdSDoug Rabson
1530778b1d1SPawel Jakub Dawidek mtx_assert(&rc->rc_lock, MA_OWNED);
1540778b1d1SPawel Jakub Dawidek
1550778b1d1SPawel Jakub Dawidek if (rc->rc_count < REPLAY_MAX && rc->rc_size <= rc->rc_maxsize)
1560778b1d1SPawel Jakub Dawidek return;
1570778b1d1SPawel Jakub Dawidek
158a9148abdSDoug Rabson do {
159a9148abdSDoug Rabson /*
1600778b1d1SPawel Jakub Dawidek * Try to free an entry. Don't free in-progress entries.
161a9148abdSDoug Rabson */
1620778b1d1SPawel Jakub Dawidek TAILQ_FOREACH_REVERSE(rce, &rc->rc_all, replay_cache_list,
1630778b1d1SPawel Jakub Dawidek rce_alllink) {
1640778b1d1SPawel Jakub Dawidek if (rce->rce_repmsg.rm_xid)
165a9148abdSDoug Rabson break;
166a9148abdSDoug Rabson }
1670778b1d1SPawel Jakub Dawidek if (rce)
1680778b1d1SPawel Jakub Dawidek replay_free(rc, rce);
1690778b1d1SPawel Jakub Dawidek } while (rce && (rc->rc_count >= REPLAY_MAX
170a9148abdSDoug Rabson || rc->rc_size > rc->rc_maxsize));
171a9148abdSDoug Rabson }
172a9148abdSDoug Rabson
173a9148abdSDoug Rabson enum replay_state
replay_find(struct replay_cache * rc,struct rpc_msg * msg,struct sockaddr * addr,struct rpc_msg * repmsg,struct mbuf ** mp)174a9148abdSDoug Rabson replay_find(struct replay_cache *rc, struct rpc_msg *msg,
175a9148abdSDoug Rabson struct sockaddr *addr, struct rpc_msg *repmsg, struct mbuf **mp)
176a9148abdSDoug Rabson {
177a9148abdSDoug Rabson int h = HASHSTEP(HASHINIT, msg->rm_xid) % REPLAY_HASH_SIZE;
178a9148abdSDoug Rabson struct replay_cache_entry *rce;
179a9148abdSDoug Rabson
180a9148abdSDoug Rabson mtx_lock(&rc->rc_lock);
181a9148abdSDoug Rabson TAILQ_FOREACH(rce, &rc->rc_cache[h], rce_link) {
182a9148abdSDoug Rabson if (rce->rce_msg.rm_xid == msg->rm_xid
183a9148abdSDoug Rabson && rce->rce_msg.rm_call.cb_prog == msg->rm_call.cb_prog
184a9148abdSDoug Rabson && rce->rce_msg.rm_call.cb_vers == msg->rm_call.cb_vers
185a9148abdSDoug Rabson && rce->rce_msg.rm_call.cb_proc == msg->rm_call.cb_proc
186a9148abdSDoug Rabson && rce->rce_addr.ss_len == addr->sa_len
187a9148abdSDoug Rabson && bcmp(&rce->rce_addr, addr, addr->sa_len) == 0) {
188a9148abdSDoug Rabson if (rce->rce_repmsg.rm_xid) {
189a9148abdSDoug Rabson /*
190a9148abdSDoug Rabson * We have a reply for this
191a9148abdSDoug Rabson * message. Copy it and return. Keep
192a9148abdSDoug Rabson * replay_all LRU sorted
193a9148abdSDoug Rabson */
194a9148abdSDoug Rabson TAILQ_REMOVE(&rc->rc_all, rce, rce_alllink);
195a9148abdSDoug Rabson TAILQ_INSERT_HEAD(&rc->rc_all, rce,
196a9148abdSDoug Rabson rce_alllink);
197a9148abdSDoug Rabson *repmsg = rce->rce_repmsg;
198a9148abdSDoug Rabson if (rce->rce_repbody) {
199a9148abdSDoug Rabson *mp = m_copym(rce->rce_repbody,
200a9148abdSDoug Rabson 0, M_COPYALL, M_NOWAIT);
201a9148abdSDoug Rabson mtx_unlock(&rc->rc_lock);
202a9148abdSDoug Rabson if (!*mp)
203a9148abdSDoug Rabson return (RS_ERROR);
204a9148abdSDoug Rabson } else {
205a9148abdSDoug Rabson mtx_unlock(&rc->rc_lock);
206a9148abdSDoug Rabson }
207a9148abdSDoug Rabson return (RS_DONE);
208a9148abdSDoug Rabson } else {
209a9148abdSDoug Rabson mtx_unlock(&rc->rc_lock);
210a9148abdSDoug Rabson return (RS_INPROGRESS);
211a9148abdSDoug Rabson }
212a9148abdSDoug Rabson }
213a9148abdSDoug Rabson }
214a9148abdSDoug Rabson
215a9148abdSDoug Rabson replay_prune(rc);
216a9148abdSDoug Rabson
217a9148abdSDoug Rabson rce = replay_alloc(rc, msg, addr, h);
218a9148abdSDoug Rabson
219a9148abdSDoug Rabson mtx_unlock(&rc->rc_lock);
220a9148abdSDoug Rabson
221a9148abdSDoug Rabson if (!rce)
222a9148abdSDoug Rabson return (RS_ERROR);
223a9148abdSDoug Rabson else
224a9148abdSDoug Rabson return (RS_NEW);
225a9148abdSDoug Rabson }
226a9148abdSDoug Rabson
227a9148abdSDoug Rabson void
replay_setreply(struct replay_cache * rc,struct rpc_msg * repmsg,struct sockaddr * addr,struct mbuf * m)228a9148abdSDoug Rabson replay_setreply(struct replay_cache *rc,
229a9148abdSDoug Rabson struct rpc_msg *repmsg, struct sockaddr *addr, struct mbuf *m)
230a9148abdSDoug Rabson {
231a9148abdSDoug Rabson int h = HASHSTEP(HASHINIT, repmsg->rm_xid) % REPLAY_HASH_SIZE;
232a9148abdSDoug Rabson struct replay_cache_entry *rce;
233a9148abdSDoug Rabson
234a9148abdSDoug Rabson /*
235a9148abdSDoug Rabson * Copy the reply before the lock so we can sleep.
236a9148abdSDoug Rabson */
237a9148abdSDoug Rabson if (m)
238a9148abdSDoug Rabson m = m_copym(m, 0, M_COPYALL, M_WAITOK);
239a9148abdSDoug Rabson
240a9148abdSDoug Rabson mtx_lock(&rc->rc_lock);
241a9148abdSDoug Rabson TAILQ_FOREACH(rce, &rc->rc_cache[h], rce_link) {
242a9148abdSDoug Rabson if (rce->rce_msg.rm_xid == repmsg->rm_xid
243a9148abdSDoug Rabson && rce->rce_addr.ss_len == addr->sa_len
244a9148abdSDoug Rabson && bcmp(&rce->rce_addr, addr, addr->sa_len) == 0) {
245a9148abdSDoug Rabson break;
246a9148abdSDoug Rabson }
247a9148abdSDoug Rabson }
248a9148abdSDoug Rabson if (rce) {
249a9148abdSDoug Rabson rce->rce_repmsg = *repmsg;
250a9148abdSDoug Rabson rce->rce_repbody = m;
251a9148abdSDoug Rabson if (m)
252a9148abdSDoug Rabson rc->rc_size += m_length(m, NULL);
253a9148abdSDoug Rabson }
254a9148abdSDoug Rabson mtx_unlock(&rc->rc_lock);
255a9148abdSDoug Rabson }
256