1 /* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 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 #if defined(LIBC_SCCS) && !defined(lint)
37 #ident "@(#)svc_dg.c 1.17 94/04/24 SMI"
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * svc_dg.c, Server side for connectionless RPC.
45 * Does some caching in the hopes of achieving execute-at-most-once semantics.
48 #include "namespace.h"
49 #include "reentrant.h"
50 #include <sys/types.h>
51 #include <sys/socket.h>
53 #include <rpc/svc_dg.h>
60 #ifdef RPC_CACHE_DEBUG
61 #include <netconfig.h>
65 #include "un-namespace.h"
70 #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2))
71 #define rpc_buffer(xprt) ((xprt)->xp_p1)
74 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
77 static void svc_dg_ops(SVCXPRT *);
78 static enum xprt_stat svc_dg_stat(SVCXPRT *);
79 static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *);
80 static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *);
81 static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *);
82 static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *);
83 static void svc_dg_destroy(SVCXPRT *);
84 static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
85 static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *);
86 static void cache_set(SVCXPRT *, size_t);
87 int svc_dg_enablecache(SVCXPRT *, u_int);
91 * xprt = svc_dg_create(sock, sendsize, recvsize);
92 * Does other connectionless specific initializations.
93 * Once *xprt is initialized, it is registered.
94 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
95 * system defaults are chosen.
96 * The routines returns NULL if a problem occurred.
98 static const char svc_dg_str[] = "svc_dg_create: %s";
99 static const char svc_dg_err1[] = "could not get transport information";
100 static const char svc_dg_err2[] = "transport does not support data transfer";
101 static const char svc_dg_err3[] = "getsockname failed";
102 static const char svc_dg_err4[] = "cannot set IP_RECVDSTADDR";
103 static const char __no_mem_str[] = "out of memory";
106 svc_dg_create(fd, sendsize, recvsize)
112 struct svc_dg_data *su = NULL;
113 struct __rpc_sockinfo si;
114 struct sockaddr_storage ss;
117 if (!__rpc_fd2sockinfo(fd, &si)) {
118 warnx(svc_dg_str, svc_dg_err1);
122 * Find the receive and the send size
124 sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
125 recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
126 if ((sendsize == 0) || (recvsize == 0)) {
127 warnx(svc_dg_str, svc_dg_err2);
131 xprt = svc_xprt_alloc();
135 su = mem_alloc(sizeof (*su));
138 su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
139 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
141 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
146 xprt->xp_verf.oa_base = su->su_verfbody;
148 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
151 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
152 warnx(svc_dg_str, svc_dg_err3);
153 goto freedata_nowarn;
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);
160 if (ss.ss_family == AF_INET) {
161 struct sockaddr_in *sin;
162 static const int true_value = 1;
164 sin = (struct sockaddr_in *)(void *)&ss;
165 if (sin->sin_addr.s_addr == INADDR_ANY) {
166 su->su_srcaddr.buf = mem_alloc(sizeof (ss));
167 su->su_srcaddr.maxlen = sizeof (ss);
169 if (_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR,
170 &true_value, sizeof(true_value))) {
171 warnx(svc_dg_str, svc_dg_err4);
172 goto freedata_nowarn;
180 (void) warnx(svc_dg_str, __no_mem_str);
184 (void) mem_free(su, sizeof (*su));
191 static enum xprt_stat
199 svc_dg_recvfrom(int fd, char *buf, int buflen,
200 struct sockaddr *raddr, socklen_t *raddrlen,
201 struct sockaddr *laddr, socklen_t *laddrlen)
204 struct iovec msg_iov[1];
205 struct sockaddr_in *lin = (struct sockaddr_in *)laddr;
207 bool_t have_lin = FALSE;
208 char tmp[CMSG_LEN(sizeof(*lin))];
209 struct cmsghdr *cmsg;
211 memset((char *)&msg, 0, sizeof(msg));
212 msg_iov[0].iov_base = buf;
213 msg_iov[0].iov_len = buflen;
214 msg.msg_iov = msg_iov;
216 msg.msg_namelen = *raddrlen;
217 msg.msg_name = (char *)raddr;
219 msg.msg_control = (caddr_t)tmp;
220 msg.msg_controllen = CMSG_LEN(sizeof(*lin));
222 rlen = _recvmsg(fd, &msg, 0);
224 *raddrlen = msg.msg_namelen;
226 if (rlen == -1 || laddr == NULL ||
227 msg.msg_controllen < sizeof(struct cmsghdr) ||
228 msg.msg_flags & MSG_CTRUNC)
231 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
232 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
233 if (cmsg->cmsg_level == IPPROTO_IP &&
234 cmsg->cmsg_type == IP_RECVDSTADDR) {
236 memcpy(&lin->sin_addr,
237 (struct in_addr *)CMSG_DATA(cmsg),
238 sizeof(struct in_addr));
243 lin->sin_family = AF_INET;
245 *laddrlen = sizeof(struct sockaddr_in);
248 lin->sin_addr.s_addr = INADDR_ANY;
254 svc_dg_recv(xprt, msg)
258 struct svc_dg_data *su = su_data(xprt);
259 XDR *xdrs = &(su->su_xdrs);
261 struct sockaddr_storage ss;
267 alen = sizeof (struct sockaddr_storage);
268 rlen = svc_dg_recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz,
269 (struct sockaddr *)(void *)&ss, &alen,
270 (struct sockaddr *)su->su_srcaddr.buf, &su->su_srcaddr.len);
271 if (rlen == -1 && errno == EINTR)
273 if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
275 if (xprt->xp_rtaddr.len < alen) {
276 if (xprt->xp_rtaddr.len != 0)
277 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
278 xprt->xp_rtaddr.buf = mem_alloc(alen);
279 xprt->xp_rtaddr.len = alen;
281 memcpy(xprt->xp_rtaddr.buf, &ss, alen);
283 if (ss.ss_family == AF_INET) {
284 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
285 xprt->xp_addrlen = sizeof (struct sockaddr_in);
288 xdrs->x_op = XDR_DECODE;
290 if (! xdr_callmsg(xdrs, msg)) {
293 su->su_xid = msg->rm_xid;
294 if (su->su_cache != NULL) {
295 if (cache_get(xprt, msg, &reply, &replylen)) {
296 (void)_sendto(xprt->xp_fd, reply, replylen, 0,
297 (struct sockaddr *)(void *)&ss, alen);
305 svc_dg_sendto(int fd, char *buf, int buflen,
306 const struct sockaddr *raddr, socklen_t raddrlen,
307 const struct sockaddr *laddr, socklen_t laddrlen)
310 struct iovec msg_iov[1];
311 struct sockaddr_in *laddr_in = (struct sockaddr_in *)laddr;
312 struct in_addr *lin = &laddr_in->sin_addr;
313 char tmp[CMSG_SPACE(sizeof(*lin))];
314 struct cmsghdr *cmsg;
316 memset((char *)&msg, 0, sizeof(msg));
317 msg_iov[0].iov_base = buf;
318 msg_iov[0].iov_len = buflen;
319 msg.msg_iov = msg_iov;
321 msg.msg_namelen = raddrlen;
322 msg.msg_name = (char *)raddr;
324 if (laddr != NULL && laddr->sa_family == AF_INET &&
325 lin->s_addr != INADDR_ANY) {
326 msg.msg_control = (caddr_t)tmp;
327 msg.msg_controllen = CMSG_LEN(sizeof(*lin));
328 cmsg = CMSG_FIRSTHDR(&msg);
329 cmsg->cmsg_len = CMSG_LEN(sizeof(*lin));
330 cmsg->cmsg_level = IPPROTO_IP;
331 cmsg->cmsg_type = IP_SENDSRCADDR;
332 memcpy(CMSG_DATA(cmsg), lin, sizeof(*lin));
335 return _sendmsg(fd, &msg, 0);
339 svc_dg_reply(xprt, msg)
343 struct svc_dg_data *su = su_data(xprt);
344 XDR *xdrs = &(su->su_xdrs);
350 xdrs->x_op = XDR_ENCODE;
352 msg->rm_xid = su->su_xid;
353 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
354 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
355 xdr_proc = msg->acpted_rply.ar_results.proc;
356 xdr_where = msg->acpted_rply.ar_results.where;
357 msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
358 msg->acpted_rply.ar_results.where = NULL;
360 if (!xdr_replymsg(xdrs, msg) ||
361 !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where))
364 stat = xdr_replymsg(xdrs, msg);
367 slen = XDR_GETPOS(xdrs);
368 if (svc_dg_sendto(xprt->xp_fd, rpc_buffer(xprt), slen,
369 (struct sockaddr *)xprt->xp_rtaddr.buf,
370 (socklen_t)xprt->xp_rtaddr.len,
371 (struct sockaddr *)su->su_srcaddr.buf,
372 (socklen_t)su->su_srcaddr.len) == (ssize_t) slen) {
375 cache_set(xprt, slen);
382 svc_dg_getargs(xprt, xdr_args, args_ptr)
387 struct svc_dg_data *su;
389 assert(xprt != NULL);
391 return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt),
392 &su->su_xdrs, xdr_args, args_ptr));
396 svc_dg_freeargs(xprt, xdr_args, args_ptr)
401 XDR *xdrs = &(su_data(xprt)->su_xdrs);
403 xdrs->x_op = XDR_FREE;
404 return (*xdr_args)(xdrs, args_ptr);
411 struct svc_dg_data *su = su_data(xprt);
413 xprt_unregister(xprt);
414 if (xprt->xp_fd != -1)
415 (void)_close(xprt->xp_fd);
416 XDR_DESTROY(&(su->su_xdrs));
417 (void) mem_free(rpc_buffer(xprt), su->su_iosz);
418 if (su->su_srcaddr.buf)
419 (void) mem_free(su->su_srcaddr.buf, su->su_srcaddr.maxlen);
420 (void) mem_free(su, sizeof (*su));
421 if (xprt->xp_rtaddr.buf)
422 (void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
423 if (xprt->xp_ltaddr.buf)
424 (void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
426 (void) free(xprt->xp_tp);
432 svc_dg_control(xprt, rq, in)
444 static struct xp_ops ops;
445 static struct xp_ops2 ops2;
447 /* VARIABLES PROTECTED BY ops_lock: ops */
449 mutex_lock(&ops_lock);
450 if (ops.xp_recv == NULL) {
451 ops.xp_recv = svc_dg_recv;
452 ops.xp_stat = svc_dg_stat;
453 ops.xp_getargs = svc_dg_getargs;
454 ops.xp_reply = svc_dg_reply;
455 ops.xp_freeargs = svc_dg_freeargs;
456 ops.xp_destroy = svc_dg_destroy;
457 ops2.xp_control = svc_dg_control;
460 xprt->xp_ops2 = &ops2;
461 mutex_unlock(&ops_lock);
464 /* The CACHING COMPONENT */
467 * Could have been a separate file, but some part of it depends upon the
468 * private structure of the client handle.
470 * Fifo cache for cl server
471 * Copies pointers to reply buffers into fifo cache
472 * Buffers are sent again if retransmissions are detected.
475 #define SPARSENESS 4 /* 75% sparse */
477 #define ALLOC(type, size) \
478 (type *) mem_alloc((sizeof (type) * (size)))
480 #define MEMZERO(addr, type, size) \
481 (void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
483 #define FREE(addr, type, size) \
484 mem_free((addr), (sizeof (type) * (size)))
487 * An entry in the cache
489 typedef struct cache_node *cache_ptr;
492 * Index into cache is xid, proc, vers, prog and address
495 rpcproc_t cache_proc;
496 rpcvers_t cache_vers;
497 rpcprog_t cache_prog;
498 struct netbuf cache_addr;
500 * The cached reply and length
503 size_t cache_replylen;
505 * Next node on the list, if there is a collision
507 cache_ptr cache_next;
514 u_int uc_size; /* size of cache */
515 cache_ptr *uc_entries; /* hash table of entries in cache */
516 cache_ptr *uc_fifo; /* fifo list of entries in cache */
517 u_int uc_nextvictim; /* points to next victim in fifo list */
518 rpcprog_t uc_prog; /* saved program number */
519 rpcvers_t uc_vers; /* saved version number */
520 rpcproc_t uc_proc; /* saved procedure number */
525 * the hashing function
527 #define CACHE_LOC(transp, xid) \
528 (xid % (SPARSENESS * ((struct cl_cache *) \
529 su_data(transp)->su_cache)->uc_size))
532 * Enable use of the cache. Returns 1 on success, 0 on failure.
533 * Note: there is no disable.
535 static const char cache_enable_str[] = "svc_enablecache: %s %s";
536 static const char alloc_err[] = "could not allocate cache ";
537 static const char enable_err[] = "cache already enabled";
540 svc_dg_enablecache(transp, size)
544 struct svc_dg_data *su = su_data(transp);
547 mutex_lock(&dupreq_lock);
548 if (su->su_cache != NULL) {
549 (void) warnx(cache_enable_str, enable_err, " ");
550 mutex_unlock(&dupreq_lock);
553 uc = ALLOC(struct cl_cache, 1);
555 warnx(cache_enable_str, alloc_err, " ");
556 mutex_unlock(&dupreq_lock);
560 uc->uc_nextvictim = 0;
561 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
562 if (uc->uc_entries == NULL) {
563 warnx(cache_enable_str, alloc_err, "data");
564 FREE(uc, struct cl_cache, 1);
565 mutex_unlock(&dupreq_lock);
568 MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
569 uc->uc_fifo = ALLOC(cache_ptr, size);
570 if (uc->uc_fifo == NULL) {
571 warnx(cache_enable_str, alloc_err, "fifo");
572 FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
573 FREE(uc, struct cl_cache, 1);
574 mutex_unlock(&dupreq_lock);
577 MEMZERO(uc->uc_fifo, cache_ptr, size);
578 su->su_cache = (char *)(void *)uc;
579 mutex_unlock(&dupreq_lock);
584 * Set an entry in the cache. It assumes that the uc entry is set from
585 * the earlier call to cache_get() for the same procedure. This will always
586 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
587 * by svc_dg_reply(). All this hoopla because the right RPC parameters are
588 * not available at svc_dg_reply time.
591 static const char cache_set_str[] = "cache_set: %s";
592 static const char cache_set_err1[] = "victim not found";
593 static const char cache_set_err2[] = "victim alloc failed";
594 static const char cache_set_err3[] = "could not allocate new rpc buffer";
597 cache_set(xprt, replylen)
603 struct svc_dg_data *su = su_data(xprt);
604 struct cl_cache *uc = (struct cl_cache *) su->su_cache;
607 #ifdef RPC_CACHE_DEBUG
608 struct netconfig *nconf;
612 mutex_lock(&dupreq_lock);
614 * Find space for the new entry, either by
615 * reusing an old entry, or by mallocing a new one
617 victim = uc->uc_fifo[uc->uc_nextvictim];
618 if (victim != NULL) {
619 loc = CACHE_LOC(xprt, victim->cache_xid);
620 for (vicp = &uc->uc_entries[loc];
621 *vicp != NULL && *vicp != victim;
622 vicp = &(*vicp)->cache_next)
625 warnx(cache_set_str, cache_set_err1);
626 mutex_unlock(&dupreq_lock);
629 *vicp = victim->cache_next; /* remove from cache */
630 newbuf = victim->cache_reply;
632 victim = ALLOC(struct cache_node, 1);
633 if (victim == NULL) {
634 warnx(cache_set_str, cache_set_err2);
635 mutex_unlock(&dupreq_lock);
638 newbuf = mem_alloc(su->su_iosz);
639 if (newbuf == NULL) {
640 warnx(cache_set_str, cache_set_err3);
641 FREE(victim, struct cache_node, 1);
642 mutex_unlock(&dupreq_lock);
650 #ifdef RPC_CACHE_DEBUG
651 if (nconf = getnetconfigent(xprt->xp_netid)) {
652 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
653 freenetconfigent(nconf);
655 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
656 su->su_xid, uc->uc_prog, uc->uc_vers,
661 victim->cache_replylen = replylen;
662 victim->cache_reply = rpc_buffer(xprt);
663 rpc_buffer(xprt) = newbuf;
664 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
665 su->su_iosz, XDR_ENCODE);
666 victim->cache_xid = su->su_xid;
667 victim->cache_proc = uc->uc_proc;
668 victim->cache_vers = uc->uc_vers;
669 victim->cache_prog = uc->uc_prog;
670 victim->cache_addr = xprt->xp_rtaddr;
671 victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
672 (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
673 (size_t)xprt->xp_rtaddr.len);
674 loc = CACHE_LOC(xprt, victim->cache_xid);
675 victim->cache_next = uc->uc_entries[loc];
676 uc->uc_entries[loc] = victim;
677 uc->uc_fifo[uc->uc_nextvictim++] = victim;
678 uc->uc_nextvictim %= uc->uc_size;
679 mutex_unlock(&dupreq_lock);
683 * Try to get an entry from the cache
684 * return 1 if found, 0 if not found and set the stage for cache_set()
687 cache_get(xprt, msg, replyp, replylenp)
695 struct svc_dg_data *su = su_data(xprt);
696 struct cl_cache *uc = (struct cl_cache *) su->su_cache;
697 #ifdef RPC_CACHE_DEBUG
698 struct netconfig *nconf;
702 mutex_lock(&dupreq_lock);
703 loc = CACHE_LOC(xprt, su->su_xid);
704 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
705 if (ent->cache_xid == su->su_xid &&
706 ent->cache_proc == msg->rm_call.cb_proc &&
707 ent->cache_vers == msg->rm_call.cb_vers &&
708 ent->cache_prog == msg->rm_call.cb_prog &&
709 ent->cache_addr.len == xprt->xp_rtaddr.len &&
710 (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
711 xprt->xp_rtaddr.len) == 0)) {
712 #ifdef RPC_CACHE_DEBUG
713 if (nconf = getnetconfigent(xprt->xp_netid)) {
714 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
715 freenetconfigent(nconf);
717 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
718 su->su_xid, msg->rm_call.cb_prog,
719 msg->rm_call.cb_vers,
720 msg->rm_call.cb_proc, uaddr);
724 *replyp = ent->cache_reply;
725 *replylenp = ent->cache_replylen;
726 mutex_unlock(&dupreq_lock);
731 * Failed to find entry
732 * Remember a few things so we can do a set later
734 uc->uc_proc = msg->rm_call.cb_proc;
735 uc->uc_vers = msg->rm_call.cb_vers;
736 uc->uc_prog = msg->rm_call.cb_prog;
737 mutex_unlock(&dupreq_lock);