1 /* $NetBSD: svc_dg.c,v 1.12 2008/04/25 17:44:44 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 /* #ident "@(#)svc_dg.c 1.17 94/04/24 SMI" */
40 * svc_dg.c, Server side for connectionless RPC.
42 * Does some caching in the hopes of achieving execute-at-most-once semantics.
45 #include <sys/cdefs.h>
46 #if defined(LIBC_SCCS) && !defined(lint)
47 __RCSID("$NetBSD: svc_dg.c,v 1.12 2008/04/25 17:44:44 christos Exp $");
50 #include "namespace.h"
51 #include "reentrant.h"
52 #include <sys/types.h>
53 #include <sys/socket.h>
61 #ifdef RPC_CACHE_DEBUG
62 #include <netconfig.h>
67 #include "rpc_internal.h"
70 #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2))
71 #define rpc_buffer(xprt) ((xprt)->xp_p1)
74 __weak_alias(svc_dg_create
,_svc_dg_create
)
78 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
81 static void svc_dg_ops
__P((SVCXPRT
*));
82 static enum xprt_stat svc_dg_stat
__P((SVCXPRT
*));
83 static bool_t svc_dg_recv
__P((SVCXPRT
*, struct rpc_msg
*));
84 static bool_t svc_dg_reply
__P((SVCXPRT
*, struct rpc_msg
*));
85 static bool_t svc_dg_getargs
__P((SVCXPRT
*, xdrproc_t
, caddr_t
));
86 static bool_t svc_dg_freeargs
__P((SVCXPRT
*, xdrproc_t
, caddr_t
));
87 static void svc_dg_destroy
__P((SVCXPRT
*));
88 static bool_t svc_dg_control
__P((SVCXPRT
*, const u_int
, void *));
89 static int cache_get
__P((SVCXPRT
*, struct rpc_msg
*, char **, size_t *));
90 static void cache_set
__P((SVCXPRT
*, size_t));
94 * xprt = svc_dg_create(sock, sendsize, recvsize);
95 * Does other connectionless specific initializations.
96 * Once *xprt is initialized, it is registered.
97 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
98 * system defaults are chosen.
99 * The routines returns NULL if a problem occurred.
101 static const char svc_dg_str
[] = "svc_dg_create: %s";
102 static const char svc_dg_err1
[] = "could not get transport information";
103 static const char svc_dg_err2
[] = " transport does not support data transfer";
104 static const char __no_mem_str
[] = "out of memory";
107 svc_dg_create(fd
, sendsize
, 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 xdrmem_create(&(su
->su_xdrs
), rpc_buffer(xprt
), su
->su_iosz
,
147 xprt
->xp_p2
= (caddr_t
)(void *)su
;
148 xprt
->xp_verf
.oa_base
= su
->su_verfbody
;
150 xprt
->xp_rtaddr
.maxlen
= sizeof (struct sockaddr_storage
);
153 if (getsockname(fd
, (struct sockaddr
*)(void *)&ss
, &slen
) < 0)
155 xprt
->xp_ltaddr
.buf
= mem_alloc(sizeof (struct sockaddr_storage
));
156 xprt
->xp_ltaddr
.maxlen
= sizeof (struct sockaddr_storage
);
157 xprt
->xp_ltaddr
.len
= slen
;
158 memcpy(xprt
->xp_ltaddr
.buf
, &ss
, slen
);
163 (void) warnx(svc_dg_str
, __no_mem_str
);
166 (void) mem_free(su
, sizeof (*su
));
167 (void) mem_free(xprt
, sizeof (SVCXPRT
));
173 static enum xprt_stat
181 svc_dg_recv(xprt
, msg
)
185 struct svc_dg_data
*su
;
188 struct sockaddr_storage ss
;
193 _DIAGASSERT(xprt
!= NULL
);
194 _DIAGASSERT(msg
!= NULL
);
197 xdrs
= &(su
->su_xdrs
);
200 alen
= sizeof (struct sockaddr_storage
);
201 rlen
= recvfrom(xprt
->xp_fd
, rpc_buffer(xprt
), su
->su_iosz
, 0,
202 (struct sockaddr
*)(void *)&ss
, &alen
);
203 if (rlen
== -1 && errno
== EINTR
)
205 if (rlen
== -1 || (rlen
< (ssize_t
)(4 * sizeof (u_int32_t
))))
207 if (xprt
->xp_rtaddr
.len
< alen
) {
208 if (xprt
->xp_rtaddr
.len
!= 0)
209 mem_free(xprt
->xp_rtaddr
.buf
, xprt
->xp_rtaddr
.len
);
210 xprt
->xp_rtaddr
.buf
= mem_alloc(alen
);
211 xprt
->xp_rtaddr
.len
= alen
;
213 memcpy(xprt
->xp_rtaddr
.buf
, &ss
, alen
);
215 if (ss
.ss_family
== AF_INET
) {
216 xprt
->xp_raddr
= *(struct sockaddr_in
*)xprt
->xp_rtaddr
.buf
;
217 xprt
->xp_addrlen
= sizeof (struct sockaddr_in
);
220 xdrs
->x_op
= XDR_DECODE
;
222 if (! xdr_callmsg(xdrs
, msg
)) {
225 su
->su_xid
= msg
->rm_xid
;
226 if (su
->su_cache
!= NULL
) {
227 if (cache_get(xprt
, msg
, &reply
, &replylen
)) {
228 (void)sendto(xprt
->xp_fd
, reply
, replylen
, 0,
229 (struct sockaddr
*)(void *)&ss
, alen
);
237 svc_dg_reply(xprt
, msg
)
241 struct svc_dg_data
*su
;
246 _DIAGASSERT(xprt
!= NULL
);
247 _DIAGASSERT(msg
!= NULL
);
250 xdrs
= &(su
->su_xdrs
);
252 xdrs
->x_op
= XDR_ENCODE
;
254 msg
->rm_xid
= su
->su_xid
;
255 if (xdr_replymsg(xdrs
, msg
)) {
256 slen
= XDR_GETPOS(xdrs
);
257 if (sendto(xprt
->xp_fd
, rpc_buffer(xprt
), slen
, 0,
258 (struct sockaddr
*)xprt
->xp_rtaddr
.buf
,
259 (socklen_t
)xprt
->xp_rtaddr
.len
) == (ssize_t
) slen
) {
262 cache_set(xprt
, slen
);
269 svc_dg_getargs(xprt
, xdr_args
, args_ptr
)
274 return (*xdr_args
)(&(su_data(xprt
)->su_xdrs
), args_ptr
);
278 svc_dg_freeargs(xprt
, xdr_args
, args_ptr
)
285 _DIAGASSERT(xprt
!= NULL
);
287 xdrs
= &(su_data(xprt
)->su_xdrs
);
288 xdrs
->x_op
= XDR_FREE
;
289 return (*xdr_args
)(xdrs
, args_ptr
);
296 struct svc_dg_data
*su
;
298 _DIAGASSERT(xprt
!= NULL
);
302 xprt_unregister(xprt
);
303 if (xprt
->xp_fd
!= -1)
304 (void)close(xprt
->xp_fd
);
305 XDR_DESTROY(&(su
->su_xdrs
));
306 (void) mem_free(rpc_buffer(xprt
), su
->su_iosz
);
307 (void) mem_free(su
, sizeof (*su
));
308 if (xprt
->xp_rtaddr
.buf
)
309 (void) mem_free(xprt
->xp_rtaddr
.buf
, xprt
->xp_rtaddr
.maxlen
);
310 if (xprt
->xp_ltaddr
.buf
)
311 (void) mem_free(xprt
->xp_ltaddr
.buf
, xprt
->xp_ltaddr
.maxlen
);
313 (void) free(xprt
->xp_tp
);
314 (void) mem_free(xprt
, sizeof (SVCXPRT
));
319 svc_dg_control(xprt
, rq
, in
)
331 static struct xp_ops ops
;
332 static struct xp_ops2 ops2
;
334 extern mutex_t ops_lock
;
337 _DIAGASSERT(xprt
!= NULL
);
339 /* VARIABLES PROTECTED BY ops_lock: ops */
341 mutex_lock(&ops_lock
);
342 if (ops
.xp_recv
== NULL
) {
343 ops
.xp_recv
= svc_dg_recv
;
344 ops
.xp_stat
= svc_dg_stat
;
345 ops
.xp_getargs
= svc_dg_getargs
;
346 ops
.xp_reply
= svc_dg_reply
;
347 ops
.xp_freeargs
= svc_dg_freeargs
;
348 ops
.xp_destroy
= svc_dg_destroy
;
349 ops2
.xp_control
= svc_dg_control
;
352 xprt
->xp_ops2
= &ops2
;
353 mutex_unlock(&ops_lock
);
356 /* The CACHING COMPONENT */
359 * Could have been a separate file, but some part of it depends upon the
360 * private structure of the client handle.
362 * Fifo cache for cl server
363 * Copies pointers to reply buffers into fifo cache
364 * Buffers are sent again if retransmissions are detected.
367 #define SPARSENESS 4 /* 75% sparse */
369 #define ALLOC(type, size) \
370 mem_alloc((sizeof (type) * (size)))
372 #define MEMZERO(addr, type, size) \
373 (void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
375 #define FREE(addr, type, size) \
376 mem_free((addr), (sizeof (type) * (size)))
379 * An entry in the cache
381 typedef struct cache_node
*cache_ptr
;
384 * Index into cache is xid, proc, vers, prog and address
387 rpcproc_t cache_proc
;
388 rpcvers_t cache_vers
;
389 rpcprog_t cache_prog
;
390 struct netbuf cache_addr
;
392 * The cached reply and length
395 size_t cache_replylen
;
397 * Next node on the list, if there is a collision
399 cache_ptr cache_next
;
406 u_int uc_size
; /* size of cache */
407 cache_ptr
*uc_entries
; /* hash table of entries in cache */
408 cache_ptr
*uc_fifo
; /* fifo list of entries in cache */
409 u_int uc_nextvictim
; /* points to next victim in fifo list */
410 rpcprog_t uc_prog
; /* saved program number */
411 rpcvers_t uc_vers
; /* saved version number */
412 rpcproc_t uc_proc
; /* saved procedure number */
417 * the hashing function
419 #define CACHE_LOC(transp, xid) \
420 (xid % (SPARSENESS * ((struct cl_cache *) \
421 su_data(transp)->su_cache)->uc_size))
424 extern mutex_t dupreq_lock
;
428 * Enable use of the cache. Returns 1 on success, 0 on failure.
429 * Note: there is no disable.
431 static const char cache_enable_str
[] = "svc_enablecache: %s %s";
432 static const char alloc_err
[] = "could not allocate cache ";
433 static const char enable_err
[] = "cache already enabled";
436 svc_dg_enablecache(transp
, size
)
440 struct svc_dg_data
*su
;
443 _DIAGASSERT(transp
!= NULL
);
445 su
= su_data(transp
);
447 mutex_lock(&dupreq_lock
);
448 if (su
->su_cache
!= NULL
) {
449 (void) warnx(cache_enable_str
, enable_err
, " ");
450 mutex_unlock(&dupreq_lock
);
453 uc
= ALLOC(struct cl_cache
, 1);
455 warnx(cache_enable_str
, alloc_err
, " ");
456 mutex_unlock(&dupreq_lock
);
460 uc
->uc_nextvictim
= 0;
461 uc
->uc_entries
= ALLOC(cache_ptr
, size
* SPARSENESS
);
462 if (uc
->uc_entries
== NULL
) {
463 warnx(cache_enable_str
, alloc_err
, "data");
464 FREE(uc
, struct cl_cache
, 1);
465 mutex_unlock(&dupreq_lock
);
468 MEMZERO(uc
->uc_entries
, cache_ptr
, size
* SPARSENESS
);
469 uc
->uc_fifo
= ALLOC(cache_ptr
, size
);
470 if (uc
->uc_fifo
== NULL
) {
471 warnx(cache_enable_str
, alloc_err
, "fifo");
472 FREE(uc
->uc_entries
, cache_ptr
, size
* SPARSENESS
);
473 FREE(uc
, struct cl_cache
, 1);
474 mutex_unlock(&dupreq_lock
);
477 MEMZERO(uc
->uc_fifo
, cache_ptr
, size
);
478 su
->su_cache
= (char *)(void *)uc
;
479 mutex_unlock(&dupreq_lock
);
484 * Set an entry in the cache. It assumes that the uc entry is set from
485 * the earlier call to cache_get() for the same procedure. This will always
486 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
487 * by svc_dg_reply(). All this hoopla because the right RPC parameters are
488 * not available at svc_dg_reply time.
491 static const char cache_set_str
[] = "cache_set: %s";
492 static const char cache_set_err1
[] = "victim not found";
493 static const char cache_set_err2
[] = "victim alloc failed";
494 static const char cache_set_err3
[] = "could not allocate new rpc buffer";
497 cache_set(xprt
, replylen
)
503 struct svc_dg_data
*su
;
507 #ifdef RPC_CACHE_DEBUG
508 struct netconfig
*nconf
;
512 _DIAGASSERT(xprt
!= NULL
);
515 uc
= (struct cl_cache
*) su
->su_cache
;
517 mutex_lock(&dupreq_lock
);
519 * Find space for the new entry, either by
520 * reusing an old entry, or by mallocing a new one
522 victim
= uc
->uc_fifo
[uc
->uc_nextvictim
];
523 if (victim
!= NULL
) {
524 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
525 for (vicp
= &uc
->uc_entries
[loc
];
526 *vicp
!= NULL
&& *vicp
!= victim
;
527 vicp
= &(*vicp
)->cache_next
)
530 warnx(cache_set_str
, cache_set_err1
);
531 mutex_unlock(&dupreq_lock
);
534 *vicp
= victim
->cache_next
; /* remove from cache */
535 newbuf
= victim
->cache_reply
;
537 victim
= ALLOC(struct cache_node
, 1);
538 if (victim
== NULL
) {
539 warnx(cache_set_str
, cache_set_err2
);
540 mutex_unlock(&dupreq_lock
);
543 newbuf
= mem_alloc(su
->su_iosz
);
544 if (newbuf
== NULL
) {
545 warnx(cache_set_str
, cache_set_err3
);
546 FREE(victim
, struct cache_node
, 1);
547 mutex_unlock(&dupreq_lock
);
555 #ifdef RPC_CACHE_DEBUG
556 if (nconf
= getnetconfigent(xprt
->xp_netid
)) {
557 uaddr
= taddr2uaddr(nconf
, &xprt
->xp_rtaddr
);
558 freenetconfigent(nconf
);
560 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
561 su
->su_xid
, uc
->uc_prog
, uc
->uc_vers
,
566 victim
->cache_replylen
= replylen
;
567 victim
->cache_reply
= rpc_buffer(xprt
);
568 rpc_buffer(xprt
) = newbuf
;
569 xdrmem_create(&(su
->su_xdrs
), rpc_buffer(xprt
),
570 su
->su_iosz
, XDR_ENCODE
);
571 victim
->cache_xid
= su
->su_xid
;
572 victim
->cache_proc
= uc
->uc_proc
;
573 victim
->cache_vers
= uc
->uc_vers
;
574 victim
->cache_prog
= uc
->uc_prog
;
575 victim
->cache_addr
= xprt
->xp_rtaddr
;
576 victim
->cache_addr
.buf
= ALLOC(char, xprt
->xp_rtaddr
.len
);
577 (void) memcpy(victim
->cache_addr
.buf
, xprt
->xp_rtaddr
.buf
,
578 (size_t)xprt
->xp_rtaddr
.len
);
579 loc
= CACHE_LOC(xprt
, victim
->cache_xid
);
580 victim
->cache_next
= uc
->uc_entries
[loc
];
581 uc
->uc_entries
[loc
] = victim
;
582 uc
->uc_fifo
[uc
->uc_nextvictim
++] = victim
;
583 uc
->uc_nextvictim
%= uc
->uc_size
;
584 mutex_unlock(&dupreq_lock
);
588 * Try to get an entry from the cache
589 * return 1 if found, 0 if not found and set the stage for cache_set()
592 cache_get(xprt
, msg
, replyp
, replylenp
)
600 struct svc_dg_data
*su
;
602 #ifdef RPC_CACHE_DEBUG
603 struct netconfig
*nconf
;
607 _DIAGASSERT(xprt
!= NULL
);
608 _DIAGASSERT(msg
!= NULL
);
609 _DIAGASSERT(replyp
!= NULL
);
610 _DIAGASSERT(replylenp
!= NULL
);
613 uc
= (struct cl_cache
*) su
->su_cache
;
615 mutex_lock(&dupreq_lock
);
616 loc
= CACHE_LOC(xprt
, su
->su_xid
);
617 for (ent
= uc
->uc_entries
[loc
]; ent
!= NULL
; ent
= ent
->cache_next
) {
618 if (ent
->cache_xid
== su
->su_xid
&&
619 ent
->cache_proc
== msg
->rm_call
.cb_proc
&&
620 ent
->cache_vers
== msg
->rm_call
.cb_vers
&&
621 ent
->cache_prog
== msg
->rm_call
.cb_prog
&&
622 ent
->cache_addr
.len
== xprt
->xp_rtaddr
.len
&&
623 (memcmp(ent
->cache_addr
.buf
, xprt
->xp_rtaddr
.buf
,
624 xprt
->xp_rtaddr
.len
) == 0)) {
625 #ifdef RPC_CACHE_DEBUG
626 if (nconf
= getnetconfigent(xprt
->xp_netid
)) {
627 uaddr
= taddr2uaddr(nconf
, &xprt
->xp_rtaddr
);
628 freenetconfigent(nconf
);
630 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
631 su
->su_xid
, msg
->rm_call
.cb_prog
,
632 msg
->rm_call
.cb_vers
,
633 msg
->rm_call
.cb_proc
, uaddr
);
637 *replyp
= ent
->cache_reply
;
638 *replylenp
= ent
->cache_replylen
;
639 mutex_unlock(&dupreq_lock
);
644 * Failed to find entry
645 * Remember a few things so we can do a set later
647 uc
->uc_proc
= msg
->rm_call
.cb_proc
;
648 uc
->uc_vers
= msg
->rm_call
.cb_vers
;
649 uc
->uc_prog
= msg
->rm_call
.cb_prog
;
650 mutex_unlock(&dupreq_lock
);