1 /* $NetBSD: svc_dg.c,v 1.17 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
38 /* #ident "@(#)svc_dg.c 1.17 94/04/24 SMI" */
42 * svc_dg.c, Server side for connectionless RPC.
44 * Does some caching in the hopes of achieving execute-at-most-once semantics.
47 #include <sys/cdefs.h>
48 #if defined(LIBC_SCCS) && !defined(lint)
49 __RCSID("$NetBSD: svc_dg.c,v 1.17 2013/03/11 20:19:29 tron Exp $");
52 #include "namespace.h"
53 #include "reentrant.h"
54 #include <sys/types.h>
55 #include <sys/socket.h>
63 #ifdef RPC_CACHE_DEBUG
64 #include <netconfig.h>
69 #include "svc_fdset.h"
70 #include "rpc_internal.h"
73 #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2))
74 #define rpc_buffer(xprt) ((xprt)->xp_p1)
77 __weak_alias(svc_dg_create
,_svc_dg_create
)
81 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
84 static void svc_dg_ops(SVCXPRT
*);
85 static enum xprt_stat
svc_dg_stat(SVCXPRT
*);
86 static bool_t
svc_dg_recv(SVCXPRT
*, struct rpc_msg
*);
87 static bool_t
svc_dg_reply(SVCXPRT
*, struct rpc_msg
*);
88 static bool_t
svc_dg_getargs(SVCXPRT
*, xdrproc_t
, caddr_t
);
89 static bool_t
svc_dg_freeargs(SVCXPRT
*, xdrproc_t
, caddr_t
);
90 static void svc_dg_destroy(SVCXPRT
*);
91 static bool_t
svc_dg_control(SVCXPRT
*, const u_int
, void *);
92 static int cache_get(SVCXPRT
*, struct rpc_msg
*, char **, size_t *);
93 static void cache_set(SVCXPRT
*, size_t);
97 * xprt = svc_dg_create(sock, sendsize, recvsize);
98 * Does other connectionless specific initializations.
99 * Once *xprt is initialized, it is registered.
100 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
101 * system defaults are chosen.
102 * The routines returns NULL if a problem occurred.
104 static const char svc_dg_str
[] = "svc_dg_create: %s";
105 static const char svc_dg_err1
[] = "could not get transport information";
106 static const char svc_dg_err2
[] = " transport does not support data transfer";
107 static const char __no_mem_str
[] = "out of memory";
110 svc_dg_create(int fd
, u_int sendsize
, u_int recvsize
)
113 struct svc_dg_data
*su
= NULL
;
114 struct __rpc_sockinfo si
;
115 struct sockaddr_storage ss
;
118 if (!__rpc_fd2sockinfo(fd
, &si
)) {
119 warnx(svc_dg_str
, svc_dg_err1
);
123 * Find the receive and the send size
125 sendsize
= __rpc_get_t_size(si
.si_af
, si
.si_proto
, (int)sendsize
);
126 recvsize
= __rpc_get_t_size(si
.si_af
, si
.si_proto
, (int)recvsize
);
127 if ((sendsize
== 0) || (recvsize
== 0)) {
128 warnx(svc_dg_str
, svc_dg_err2
);
132 xprt
= mem_alloc(sizeof (SVCXPRT
));
135 memset(xprt
, 0, sizeof (SVCXPRT
));
137 su
= mem_alloc(sizeof (*su
));
140 su
->su_iosz
= ((MAX(sendsize
, recvsize
) + 3) / 4) * 4;
141 if ((rpc_buffer(xprt
) = malloc(su
->su_iosz
)) == NULL
)
143 _DIAGASSERT(__type_fit(u_int
, su
->su_iosz
));
144 xdrmem_create(&(su
->su_xdrs
), rpc_buffer(xprt
), (u_int
)su
->su_iosz
,
148 xprt
->xp_p2
= (caddr_t
)(void *)su
;
149 xprt
->xp_verf
.oa_base
= su
->su_verfbody
;
151 xprt
->xp_rtaddr
.maxlen
= sizeof (struct sockaddr_storage
);
154 if (getsockname(fd
, (struct sockaddr
*)(void *)&ss
, &slen
) < 0)
156 xprt
->xp_ltaddr
.buf
= mem_alloc(sizeof (struct sockaddr_storage
));
157 xprt
->xp_ltaddr
.maxlen
= sizeof (struct sockaddr_storage
);
158 xprt
->xp_ltaddr
.len
= slen
;
159 memcpy(xprt
->xp_ltaddr
.buf
, &ss
, slen
);
161 if (!xprt_register(xprt
))
166 (void) warnx(svc_dg_str
, __no_mem_str
);
170 (void) mem_free(su
, sizeof (*su
));
171 (void) mem_free(xprt
, sizeof (SVCXPRT
));
177 static enum xprt_stat
178 svc_dg_stat(SVCXPRT
*xprt
)
184 svc_dg_recv(SVCXPRT
*xprt
, struct rpc_msg
*msg
)
186 struct svc_dg_data
*su
;
189 struct sockaddr_storage ss
;
194 _DIAGASSERT(xprt
!= NULL
);
195 _DIAGASSERT(msg
!= NULL
);
198 xdrs
= &(su
->su_xdrs
);
201 alen
= sizeof (struct sockaddr_storage
);
202 rlen
= recvfrom(xprt
->xp_fd
, rpc_buffer(xprt
), su
->su_iosz
, 0,
203 (struct sockaddr
*)(void *)&ss
, &alen
);
204 if (rlen
== -1 && errno
== EINTR
)
206 if (rlen
== -1 || (rlen
< (ssize_t
)(4 * sizeof (u_int32_t
))))
208 if (xprt
->xp_rtaddr
.len
< alen
) {
209 if (xprt
->xp_rtaddr
.len
!= 0)
210 mem_free(xprt
->xp_rtaddr
.buf
, xprt
->xp_rtaddr
.len
);
211 xprt
->xp_rtaddr
.buf
= mem_alloc(alen
);
212 xprt
->xp_rtaddr
.len
= alen
;
214 memcpy(xprt
->xp_rtaddr
.buf
, &ss
, alen
);
216 if (ss
.ss_family
== AF_INET
) {
217 xprt
->xp_raddr
= *(struct sockaddr_in
*)xprt
->xp_rtaddr
.buf
;
218 xprt
->xp_addrlen
= sizeof (struct sockaddr_in
);
221 xdrs
->x_op
= XDR_DECODE
;
223 if (! xdr_callmsg(xdrs
, msg
)) {
226 su
->su_xid
= msg
->rm_xid
;
227 if (su
->su_cache
!= NULL
) {
228 if (cache_get(xprt
, msg
, &reply
, &replylen
)) {
229 (void)sendto(xprt
->xp_fd
, reply
, replylen
, 0,
230 (struct sockaddr
*)(void *)&ss
, alen
);
238 svc_dg_reply(SVCXPRT
*xprt
, struct rpc_msg
*msg
)
240 struct svc_dg_data
*su
;
245 _DIAGASSERT(xprt
!= NULL
);
246 _DIAGASSERT(msg
!= NULL
);
249 xdrs
= &(su
->su_xdrs
);
251 xdrs
->x_op
= XDR_ENCODE
;
253 msg
->rm_xid
= su
->su_xid
;
254 if (xdr_replymsg(xdrs
, msg
)) {
255 slen
= XDR_GETPOS(xdrs
);
256 if (sendto(xprt
->xp_fd
, rpc_buffer(xprt
), slen
, 0,
257 (struct sockaddr
*)xprt
->xp_rtaddr
.buf
,
258 (socklen_t
)xprt
->xp_rtaddr
.len
) == (ssize_t
) slen
) {
261 cache_set(xprt
, slen
);
268 svc_dg_getargs(SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
270 return (*xdr_args
)(&(su_data(xprt
)->su_xdrs
), args_ptr
);
274 svc_dg_freeargs(SVCXPRT
*xprt
, xdrproc_t xdr_args
, caddr_t args_ptr
)
278 _DIAGASSERT(xprt
!= NULL
);
280 xdrs
= &(su_data(xprt
)->su_xdrs
);
281 xdrs
->x_op
= XDR_FREE
;
282 return (*xdr_args
)(xdrs
, args_ptr
);
286 svc_dg_destroy(SVCXPRT
*xprt
)
288 struct svc_dg_data
*su
;
290 _DIAGASSERT(xprt
!= NULL
);
294 xprt_unregister(xprt
);
295 if (xprt
->xp_fd
!= -1)
296 (void)close(xprt
->xp_fd
);
297 XDR_DESTROY(&(su
->su_xdrs
));
298 (void) mem_free(rpc_buffer(xprt
), su
->su_iosz
);
299 (void) mem_free(su
, sizeof (*su
));
300 if (xprt
->xp_rtaddr
.buf
)
301 (void) mem_free(xprt
->xp_rtaddr
.buf
, xprt
->xp_rtaddr
.maxlen
);
302 if (xprt
->xp_ltaddr
.buf
)
303 (void) mem_free(xprt
->xp_ltaddr
.buf
, xprt
->xp_ltaddr
.maxlen
);
305 (void) free(xprt
->xp_tp
);
306 (void) mem_free(xprt
, sizeof (SVCXPRT
));
311 svc_dg_control(SVCXPRT
*xprt
, const u_int rq
, void *in
)
317 svc_dg_ops(SVCXPRT
*xprt
)
319 static struct xp_ops ops
;
320 static struct xp_ops2 ops2
;
322 extern mutex_t ops_lock
;
325 _DIAGASSERT(xprt
!= NULL
);
327 /* VARIABLES PROTECTED BY ops_lock: ops */
329 mutex_lock(&ops_lock
);
330 if (ops
.xp_recv
== NULL
) {
331 ops
.xp_recv
= svc_dg_recv
;
332 ops
.xp_stat
= svc_dg_stat
;
333 ops
.xp_getargs
= svc_dg_getargs
;
334 ops
.xp_reply
= svc_dg_reply
;
335 ops
.xp_freeargs
= svc_dg_freeargs
;
336 ops
.xp_destroy
= svc_dg_destroy
;
337 ops2
.xp_control
= svc_dg_control
;
340 xprt
->xp_ops2
= &ops2
;
341 mutex_unlock(&ops_lock
);
344 /* The CACHING COMPONENT */
347 * Could have been a separate file, but some part of it depends upon the
348 * private structure of the client handle.
350 * Fifo cache for cl server
351 * Copies pointers to reply buffers into fifo cache
352 * Buffers are sent again if retransmissions are detected.
355 #define SPARSENESS 4 /* 75% sparse */
357 #define ALLOC(type, size) \
358 mem_alloc((sizeof (type) * (size)))
360 #define MEMZERO(addr, type, size) \
361 (void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
363 #define FREE(addr, type, size) \
364 mem_free((addr), (sizeof (type) * (size)))
367 * An entry in the cache
369 typedef struct cache_node
*cache_ptr
;
372 * Index into cache is xid, proc, vers, prog and address
375 rpcproc_t cache_proc
;
376 rpcvers_t cache_vers
;
377 rpcprog_t cache_prog
;
378 struct netbuf cache_addr
;
380 * The cached reply and length
383 size_t cache_replylen
;
385 * Next node on the list, if there is a collision
387 cache_ptr cache_next
;
394 u_int uc_size
; /* size of cache */
395 cache_ptr
*uc_entries
; /* hash table of entries in cache */
396 cache_ptr
*uc_fifo
; /* fifo list of entries in cache */
397 u_int uc_nextvictim
; /* points to next victim in fifo list */
398 rpcprog_t uc_prog
; /* saved program number */
399 rpcvers_t uc_vers
; /* saved version number */
400 rpcproc_t uc_proc
; /* saved procedure number */
405 * the hashing function
407 #define CACHE_LOC(transp, xid) \
408 (xid % (SPARSENESS * ((struct cl_cache *) \
409 su_data(transp)->su_cache)->uc_size))
412 extern mutex_t dupreq_lock
;
416 * Enable use of the cache. Returns 1 on success, 0 on failure.
417 * Note: there is no disable.
419 static const char cache_enable_str
[] = "svc_enablecache: %s %s";
420 static const char alloc_err
[] = "could not allocate cache ";
421 static const char enable_err
[] = "cache already enabled";
424 svc_dg_enablecache(SVCXPRT
*transp
, u_int size
)
426 struct svc_dg_data
*su
;
429 _DIAGASSERT(transp
!= NULL
);
431 su
= su_data(transp
);
433 mutex_lock(&dupreq_lock
);
434 if (su
->su_cache
!= NULL
) {
435 (void) warnx(cache_enable_str
, enable_err
, " ");
436 mutex_unlock(&dupreq_lock
);
439 uc
= ALLOC(struct cl_cache
, 1);
441 warnx(cache_enable_str
, alloc_err
, " ");
442 mutex_unlock(&dupreq_lock
);
446 uc
->uc_nextvictim
= 0;
447 uc
->uc_entries
= ALLOC(cache_ptr
, size
* SPARSENESS
);
448 if (uc
->uc_entries
== NULL
) {
449 warnx(cache_enable_str
, alloc_err
, "data");
450 FREE(uc
, struct cl_cache
, 1);
451 mutex_unlock(&dupreq_lock
);
454 MEMZERO(uc
->uc_entries
, cache_ptr
, size
* SPARSENESS
);
455 uc
->uc_fifo
= ALLOC(cache_ptr
, size
);
456 if (uc
->uc_fifo
== NULL
) {
457 warnx(cache_enable_str
, alloc_err
, "fifo");
458 FREE(uc
->uc_entries
, cache_ptr
, size
* SPARSENESS
);
459 FREE(uc
, struct cl_cache
, 1);
460 mutex_unlock(&dupreq_lock
);
463 MEMZERO(uc
->uc_fifo
, cache_ptr
, size
);
464 su
->su_cache
= (char *)(void *)uc
;
465 mutex_unlock(&dupreq_lock
);
470 * Set an entry in the cache. It assumes that the uc entry is set from
471 * the earlier call to cache_get() for the same procedure. This will always
472 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
473 * by svc_dg_reply(). All this hoopla because the right RPC parameters are
474 * not available at svc_dg_reply time.
477 static const char cache_set_str
[] = "cache_set: %s";
478 static const char cache_set_err1
[] = "victim not found";
479 static const char cache_set_err2
[] = "victim alloc failed";
480 static const char cache_set_err3
[] = "could not allocate new rpc buffer";
483 cache_set(SVCXPRT
*xprt
, size_t replylen
)
487 struct svc_dg_data
*su
;
491 #ifdef RPC_CACHE_DEBUG
492 struct netconfig
*nconf
;
496 _DIAGASSERT(xprt
!= NULL
);
499 uc
= (struct cl_cache
*) su
->su_cache
;
501 mutex_lock(&dupreq_lock
);
503 * Find space for the new entry, either by
504 * reusing an old entry, or by mallocing a new one
506 victim
= uc
->uc_fifo
[uc
->uc_nextvictim
];
507 if (victim
!= NULL
) {
508 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
509 for (vicp
= &uc
->uc_entries
[loc
];
510 *vicp
!= NULL
&& *vicp
!= victim
;
511 vicp
= &(*vicp
)->cache_next
)
514 warnx(cache_set_str
, cache_set_err1
);
515 mutex_unlock(&dupreq_lock
);
518 *vicp
= victim
->cache_next
; /* remove from cache */
519 newbuf
= victim
->cache_reply
;
521 victim
= ALLOC(struct cache_node
, 1);
522 if (victim
== NULL
) {
523 warnx(cache_set_str
, cache_set_err2
);
524 mutex_unlock(&dupreq_lock
);
527 newbuf
= mem_alloc(su
->su_iosz
);
528 if (newbuf
== NULL
) {
529 warnx(cache_set_str
, cache_set_err3
);
530 FREE(victim
, struct cache_node
, 1);
531 mutex_unlock(&dupreq_lock
);
539 #ifdef RPC_CACHE_DEBUG
540 if (nconf
= getnetconfigent(xprt
->xp_netid
)) {
541 uaddr
= taddr2uaddr(nconf
, &xprt
->xp_rtaddr
);
542 freenetconfigent(nconf
);
544 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
545 su
->su_xid
, uc
->uc_prog
, uc
->uc_vers
,
550 victim
->cache_replylen
= replylen
;
551 victim
->cache_reply
= rpc_buffer(xprt
);
552 rpc_buffer(xprt
) = newbuf
;
553 _DIAGASSERT(__type_fit(u_int
, su
->su_iosz
));
554 xdrmem_create(&(su
->su_xdrs
), rpc_buffer(xprt
), (u_int
)su
->su_iosz
,
556 victim
->cache_xid
= su
->su_xid
;
557 victim
->cache_proc
= uc
->uc_proc
;
558 victim
->cache_vers
= uc
->uc_vers
;
559 victim
->cache_prog
= uc
->uc_prog
;
560 victim
->cache_addr
= xprt
->xp_rtaddr
;
561 victim
->cache_addr
.buf
= ALLOC(char, xprt
->xp_rtaddr
.len
);
562 (void) memcpy(victim
->cache_addr
.buf
, xprt
->xp_rtaddr
.buf
,
563 (size_t)xprt
->xp_rtaddr
.len
);
564 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
565 victim
->cache_next
= uc
->uc_entries
[loc
];
566 uc
->uc_entries
[loc
] = victim
;
567 uc
->uc_fifo
[uc
->uc_nextvictim
++] = victim
;
568 uc
->uc_nextvictim
%= uc
->uc_size
;
569 mutex_unlock(&dupreq_lock
);
573 * Try to get an entry from the cache
574 * return 1 if found, 0 if not found and set the stage for cache_set()
577 cache_get(SVCXPRT
*xprt
, struct rpc_msg
*msg
, char **replyp
, size_t *replylenp
)
581 struct svc_dg_data
*su
;
583 #ifdef RPC_CACHE_DEBUG
584 struct netconfig
*nconf
;
588 _DIAGASSERT(xprt
!= NULL
);
589 _DIAGASSERT(msg
!= NULL
);
590 _DIAGASSERT(replyp
!= NULL
);
591 _DIAGASSERT(replylenp
!= NULL
);
594 uc
= (struct cl_cache
*) su
->su_cache
;
596 mutex_lock(&dupreq_lock
);
597 loc
= CACHE_LOC(xprt
, su
->su_xid
);
598 for (ent
= uc
->uc_entries
[loc
]; ent
!= NULL
; ent
= ent
->cache_next
) {
599 if (ent
->cache_xid
== su
->su_xid
&&
600 ent
->cache_proc
== msg
->rm_call
.cb_proc
&&
601 ent
->cache_vers
== msg
->rm_call
.cb_vers
&&
602 ent
->cache_prog
== msg
->rm_call
.cb_prog
&&
603 ent
->cache_addr
.len
== xprt
->xp_rtaddr
.len
&&
604 (memcmp(ent
->cache_addr
.buf
, xprt
->xp_rtaddr
.buf
,
605 xprt
->xp_rtaddr
.len
) == 0)) {
606 #ifdef RPC_CACHE_DEBUG
607 if (nconf
= getnetconfigent(xprt
->xp_netid
)) {
608 uaddr
= taddr2uaddr(nconf
, &xprt
->xp_rtaddr
);
609 freenetconfigent(nconf
);
611 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
612 su
->su_xid
, msg
->rm_call
.cb_prog
,
613 msg
->rm_call
.cb_vers
,
614 msg
->rm_call
.cb_proc
, uaddr
);
618 *replyp
= ent
->cache_reply
;
619 *replylenp
= ent
->cache_replylen
;
620 mutex_unlock(&dupreq_lock
);
625 * Failed to find entry
626 * Remember a few things so we can do a set later
628 uc
->uc_proc
= msg
->rm_call
.cb_proc
;
629 uc
->uc_vers
= msg
->rm_call
.cb_vers
;
630 uc
->uc_prog
= msg
->rm_call
.cb_prog
;
631 mutex_unlock(&dupreq_lock
);