svc_dg.c revision 1.8 1 /* $NetBSD: svc_dg.c,v 1.8 2002/11/11 22:17:21 thorpej Exp $ */
2
3 /*
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.
10 *
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.
14 *
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.
18 *
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.
22 *
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.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 /*
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 */
35
36 /* #ident "@(#)svc_dg.c 1.17 94/04/24 SMI" */
37
38
39 /*
40 * svc_dg.c, Server side for connectionless RPC.
41 *
42 * Does some caching in the hopes of achieving execute-at-most-once semantics.
43 */
44
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <rpc/rpc.h>
50 #include <assert.h>
51 #include <errno.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #ifdef RPC_CACHE_DEBUG
57 #include <netconfig.h>
58 #include <netdir.h>
59 #endif
60 #include <err.h>
61
62 #include "rpc_internal.h"
63 #include "svc_dg.h"
64
65 #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2))
66 #define rpc_buffer(xprt) ((xprt)->xp_p1)
67
68 #ifdef __weak_alias
69 __weak_alias(svc_dg_create,_svc_dg_create)
70 #endif
71
72 #ifndef MAX
73 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
74 #endif
75
76 static void svc_dg_ops __P((SVCXPRT *));
77 static enum xprt_stat svc_dg_stat __P((SVCXPRT *));
78 static bool_t svc_dg_recv __P((SVCXPRT *, struct rpc_msg *));
79 static bool_t svc_dg_reply __P((SVCXPRT *, struct rpc_msg *));
80 static bool_t svc_dg_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
81 static bool_t svc_dg_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
82 static void svc_dg_destroy __P((SVCXPRT *));
83 static bool_t svc_dg_control __P((SVCXPRT *, const u_int, void *));
84 static int cache_get __P((SVCXPRT *, struct rpc_msg *, char **, size_t *));
85 static void cache_set __P((SVCXPRT *, size_t));
86
87 /*
88 * Usage:
89 * xprt = svc_dg_create(sock, sendsize, recvsize);
90 * Does other connectionless specific initializations.
91 * Once *xprt is initialized, it is registered.
92 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
93 * system defaults are chosen.
94 * The routines returns NULL if a problem occurred.
95 */
96 static const char svc_dg_str[] = "svc_dg_create: %s";
97 static const char svc_dg_err1[] = "could not get transport information";
98 static const char svc_dg_err2[] = " transport does not support data transfer";
99 static const char __no_mem_str[] = "out of memory";
100
101 SVCXPRT *
102 svc_dg_create(fd, sendsize, recvsize)
103 int fd;
104 u_int sendsize;
105 u_int recvsize;
106 {
107 SVCXPRT *xprt;
108 struct svc_dg_data *su = NULL;
109 struct __rpc_sockinfo si;
110 struct sockaddr_storage ss;
111 socklen_t slen;
112
113 if (!__rpc_fd2sockinfo(fd, &si)) {
114 warnx(svc_dg_str, svc_dg_err1);
115 return (NULL);
116 }
117 /*
118 * Find the receive and the send size
119 */
120 sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
121 recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
122 if ((sendsize == 0) || (recvsize == 0)) {
123 warnx(svc_dg_str, svc_dg_err2);
124 return (NULL);
125 }
126
127 xprt = mem_alloc(sizeof (SVCXPRT));
128 if (xprt == NULL)
129 goto freedata;
130 memset(xprt, 0, sizeof (SVCXPRT));
131
132 su = mem_alloc(sizeof (*su));
133 if (su == NULL)
134 goto freedata;
135 su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
136 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
137 goto freedata;
138 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
139 XDR_DECODE);
140 su->su_cache = NULL;
141 xprt->xp_fd = fd;
142 xprt->xp_p2 = (caddr_t)(void *)su;
143 xprt->xp_verf.oa_base = su->su_verfbody;
144 svc_dg_ops(xprt);
145 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
146
147 slen = sizeof ss;
148 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
149 goto freedata;
150 xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
151 xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
152 xprt->xp_ltaddr.len = slen;
153 memcpy(xprt->xp_ltaddr.buf, &ss, slen);
154
155 xprt_register(xprt);
156 return (xprt);
157 freedata:
158 (void) warnx(svc_dg_str, __no_mem_str);
159 if (xprt) {
160 if (su)
161 (void) mem_free(su, sizeof (*su));
162 (void) mem_free(xprt, sizeof (SVCXPRT));
163 }
164 return (NULL);
165 }
166
167 /*ARGSUSED*/
168 static enum xprt_stat
169 svc_dg_stat(xprt)
170 SVCXPRT *xprt;
171 {
172 return (XPRT_IDLE);
173 }
174
175 static bool_t
176 svc_dg_recv(xprt, msg)
177 SVCXPRT *xprt;
178 struct rpc_msg *msg;
179 {
180 struct svc_dg_data *su;
181 XDR *xdrs;
182 char *reply;
183 struct sockaddr_storage ss;
184 socklen_t alen;
185 size_t replylen;
186 ssize_t rlen;
187
188 _DIAGASSERT(xprt != NULL);
189 _DIAGASSERT(msg != NULL);
190
191 su = su_data(xprt);
192 xdrs = &(su->su_xdrs);
193
194 again:
195 alen = sizeof (struct sockaddr_storage);
196 rlen = recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz, 0,
197 (struct sockaddr *)(void *)&ss, &alen);
198 if (rlen == -1 && errno == EINTR)
199 goto again;
200 if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
201 return (FALSE);
202 if (xprt->xp_rtaddr.len < alen) {
203 if (xprt->xp_rtaddr.len != 0)
204 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
205 xprt->xp_rtaddr.buf = mem_alloc(alen);
206 xprt->xp_rtaddr.len = alen;
207 }
208 memcpy(xprt->xp_rtaddr.buf, &ss, alen);
209 #ifdef PORTMAP
210 if (ss.ss_family == AF_INET) {
211 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
212 xprt->xp_addrlen = sizeof (struct sockaddr_in);
213 }
214 #endif
215 xdrs->x_op = XDR_DECODE;
216 XDR_SETPOS(xdrs, 0);
217 if (! xdr_callmsg(xdrs, msg)) {
218 return (FALSE);
219 }
220 su->su_xid = msg->rm_xid;
221 if (su->su_cache != NULL) {
222 if (cache_get(xprt, msg, &reply, &replylen)) {
223 (void)sendto(xprt->xp_fd, reply, replylen, 0,
224 (struct sockaddr *)(void *)&ss, alen);
225 return (FALSE);
226 }
227 }
228 return (TRUE);
229 }
230
231 static bool_t
232 svc_dg_reply(xprt, msg)
233 SVCXPRT *xprt;
234 struct rpc_msg *msg;
235 {
236 struct svc_dg_data *su;
237 XDR *xdrs;
238 bool_t stat = FALSE;
239 size_t slen;
240
241 _DIAGASSERT(xprt != NULL);
242 _DIAGASSERT(msg != NULL);
243
244 su = su_data(xprt);
245 xdrs = &(su->su_xdrs);
246
247 xdrs->x_op = XDR_ENCODE;
248 XDR_SETPOS(xdrs, 0);
249 msg->rm_xid = su->su_xid;
250 if (xdr_replymsg(xdrs, msg)) {
251 slen = XDR_GETPOS(xdrs);
252 if (sendto(xprt->xp_fd, rpc_buffer(xprt), slen, 0,
253 (struct sockaddr *)xprt->xp_rtaddr.buf,
254 (socklen_t)xprt->xp_rtaddr.len) == (ssize_t) slen) {
255 stat = TRUE;
256 if (su->su_cache)
257 cache_set(xprt, slen);
258 }
259 }
260 return (stat);
261 }
262
263 static bool_t
264 svc_dg_getargs(xprt, xdr_args, args_ptr)
265 SVCXPRT *xprt;
266 xdrproc_t xdr_args;
267 caddr_t args_ptr;
268 {
269 return (*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr);
270 }
271
272 static bool_t
273 svc_dg_freeargs(xprt, xdr_args, args_ptr)
274 SVCXPRT *xprt;
275 xdrproc_t xdr_args;
276 caddr_t args_ptr;
277 {
278 XDR *xdrs;
279
280 _DIAGASSERT(xprt != NULL);
281
282 xdrs = &(su_data(xprt)->su_xdrs);
283 xdrs->x_op = XDR_FREE;
284 return (*xdr_args)(xdrs, args_ptr);
285 }
286
287 static void
288 svc_dg_destroy(xprt)
289 SVCXPRT *xprt;
290 {
291 struct svc_dg_data *su;
292
293 _DIAGASSERT(xprt != NULL);
294
295 su = su_data(xprt);
296
297 xprt_unregister(xprt);
298 if (xprt->xp_fd != -1)
299 (void)close(xprt->xp_fd);
300 XDR_DESTROY(&(su->su_xdrs));
301 (void) mem_free(rpc_buffer(xprt), su->su_iosz);
302 (void) mem_free(su, sizeof (*su));
303 if (xprt->xp_rtaddr.buf)
304 (void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
305 if (xprt->xp_ltaddr.buf)
306 (void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
307 if (xprt->xp_tp)
308 (void) free(xprt->xp_tp);
309 (void) mem_free(xprt, sizeof (SVCXPRT));
310 }
311
312 static bool_t
313 /*ARGSUSED*/
314 svc_dg_control(xprt, rq, in)
315 SVCXPRT *xprt;
316 const u_int rq;
317 void *in;
318 {
319 return (FALSE);
320 }
321
322 static void
323 svc_dg_ops(xprt)
324 SVCXPRT *xprt;
325 {
326 static struct xp_ops ops;
327 static struct xp_ops2 ops2;
328 #ifdef __REENT
329 extern mutex_t ops_lock;
330 #endif
331
332 _DIAGASSERT(xprt != NULL);
333
334 /* VARIABLES PROTECTED BY ops_lock: ops */
335
336 mutex_lock(&ops_lock);
337 if (ops.xp_recv == NULL) {
338 ops.xp_recv = svc_dg_recv;
339 ops.xp_stat = svc_dg_stat;
340 ops.xp_getargs = svc_dg_getargs;
341 ops.xp_reply = svc_dg_reply;
342 ops.xp_freeargs = svc_dg_freeargs;
343 ops.xp_destroy = svc_dg_destroy;
344 ops2.xp_control = svc_dg_control;
345 }
346 xprt->xp_ops = &ops;
347 xprt->xp_ops2 = &ops2;
348 mutex_unlock(&ops_lock);
349 }
350
351 /* The CACHING COMPONENT */
352
353 /*
354 * Could have been a separate file, but some part of it depends upon the
355 * private structure of the client handle.
356 *
357 * Fifo cache for cl server
358 * Copies pointers to reply buffers into fifo cache
359 * Buffers are sent again if retransmissions are detected.
360 */
361
362 #define SPARSENESS 4 /* 75% sparse */
363
364 #define ALLOC(type, size) \
365 (type *) mem_alloc((sizeof (type) * (size)))
366
367 #define MEMZERO(addr, type, size) \
368 (void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
369
370 #define FREE(addr, type, size) \
371 mem_free((addr), (sizeof (type) * (size)))
372
373 /*
374 * An entry in the cache
375 */
376 typedef struct cache_node *cache_ptr;
377 struct cache_node {
378 /*
379 * Index into cache is xid, proc, vers, prog and address
380 */
381 u_int32_t cache_xid;
382 rpcproc_t cache_proc;
383 rpcvers_t cache_vers;
384 rpcprog_t cache_prog;
385 struct netbuf cache_addr;
386 /*
387 * The cached reply and length
388 */
389 char *cache_reply;
390 size_t cache_replylen;
391 /*
392 * Next node on the list, if there is a collision
393 */
394 cache_ptr cache_next;
395 };
396
397 /*
398 * The entire cache
399 */
400 struct cl_cache {
401 u_int uc_size; /* size of cache */
402 cache_ptr *uc_entries; /* hash table of entries in cache */
403 cache_ptr *uc_fifo; /* fifo list of entries in cache */
404 u_int uc_nextvictim; /* points to next victim in fifo list */
405 rpcprog_t uc_prog; /* saved program number */
406 rpcvers_t uc_vers; /* saved version number */
407 rpcproc_t uc_proc; /* saved procedure number */
408 };
409
410
411 /*
412 * the hashing function
413 */
414 #define CACHE_LOC(transp, xid) \
415 (xid % (SPARSENESS * ((struct cl_cache *) \
416 su_data(transp)->su_cache)->uc_size))
417
418 #ifdef __REENT
419 extern mutex_t dupreq_lock;
420 #endif
421
422 /*
423 * Enable use of the cache. Returns 1 on success, 0 on failure.
424 * Note: there is no disable.
425 */
426 static const char cache_enable_str[] = "svc_enablecache: %s %s";
427 static const char alloc_err[] = "could not allocate cache ";
428 static const char enable_err[] = "cache already enabled";
429
430 int
431 svc_dg_enablecache(transp, size)
432 SVCXPRT *transp;
433 u_int size;
434 {
435 struct svc_dg_data *su;
436 struct cl_cache *uc;
437
438 _DIAGASSERT(transp != NULL);
439
440 su = su_data(transp);
441
442 mutex_lock(&dupreq_lock);
443 if (su->su_cache != NULL) {
444 (void) warnx(cache_enable_str, enable_err, " ");
445 mutex_unlock(&dupreq_lock);
446 return (0);
447 }
448 uc = ALLOC(struct cl_cache, 1);
449 if (uc == NULL) {
450 warnx(cache_enable_str, alloc_err, " ");
451 mutex_unlock(&dupreq_lock);
452 return (0);
453 }
454 uc->uc_size = size;
455 uc->uc_nextvictim = 0;
456 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
457 if (uc->uc_entries == NULL) {
458 warnx(cache_enable_str, alloc_err, "data");
459 FREE(uc, struct cl_cache, 1);
460 mutex_unlock(&dupreq_lock);
461 return (0);
462 }
463 MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
464 uc->uc_fifo = ALLOC(cache_ptr, size);
465 if (uc->uc_fifo == NULL) {
466 warnx(cache_enable_str, alloc_err, "fifo");
467 FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
468 FREE(uc, struct cl_cache, 1);
469 mutex_unlock(&dupreq_lock);
470 return (0);
471 }
472 MEMZERO(uc->uc_fifo, cache_ptr, size);
473 su->su_cache = (char *)(void *)uc;
474 mutex_unlock(&dupreq_lock);
475 return (1);
476 }
477
478 /*
479 * Set an entry in the cache. It assumes that the uc entry is set from
480 * the earlier call to cache_get() for the same procedure. This will always
481 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
482 * by svc_dg_reply(). All this hoopla because the right RPC parameters are
483 * not available at svc_dg_reply time.
484 */
485
486 static const char cache_set_str[] = "cache_set: %s";
487 static const char cache_set_err1[] = "victim not found";
488 static const char cache_set_err2[] = "victim alloc failed";
489 static const char cache_set_err3[] = "could not allocate new rpc buffer";
490
491 static void
492 cache_set(xprt, replylen)
493 SVCXPRT *xprt;
494 size_t replylen;
495 {
496 cache_ptr victim;
497 cache_ptr *vicp;
498 struct svc_dg_data *su;
499 struct cl_cache *uc;
500 u_int loc;
501 char *newbuf;
502 #ifdef RPC_CACHE_DEBUG
503 struct netconfig *nconf;
504 char *uaddr;
505 #endif
506
507 _DIAGASSERT(xprt != NULL);
508
509 su = su_data(xprt);
510 uc = (struct cl_cache *) su->su_cache;
511
512 mutex_lock(&dupreq_lock);
513 /*
514 * Find space for the new entry, either by
515 * reusing an old entry, or by mallocing a new one
516 */
517 victim = uc->uc_fifo[uc->uc_nextvictim];
518 if (victim != NULL) {
519 loc = CACHE_LOC(xprt, victim->cache_xid);
520 for (vicp = &uc->uc_entries[loc];
521 *vicp != NULL && *vicp != victim;
522 vicp = &(*vicp)->cache_next)
523 ;
524 if (*vicp == NULL) {
525 warnx(cache_set_str, cache_set_err1);
526 mutex_unlock(&dupreq_lock);
527 return;
528 }
529 *vicp = victim->cache_next; /* remove from cache */
530 newbuf = victim->cache_reply;
531 } else {
532 victim = ALLOC(struct cache_node, 1);
533 if (victim == NULL) {
534 warnx(cache_set_str, cache_set_err2);
535 mutex_unlock(&dupreq_lock);
536 return;
537 }
538 newbuf = mem_alloc(su->su_iosz);
539 if (newbuf == NULL) {
540 warnx(cache_set_str, cache_set_err3);
541 FREE(victim, struct cache_node, 1);
542 mutex_unlock(&dupreq_lock);
543 return;
544 }
545 }
546
547 /*
548 * Store it away
549 */
550 #ifdef RPC_CACHE_DEBUG
551 if (nconf = getnetconfigent(xprt->xp_netid)) {
552 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
553 freenetconfigent(nconf);
554 printf(
555 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
556 su->su_xid, uc->uc_prog, uc->uc_vers,
557 uc->uc_proc, uaddr);
558 free(uaddr);
559 }
560 #endif
561 victim->cache_replylen = replylen;
562 victim->cache_reply = rpc_buffer(xprt);
563 rpc_buffer(xprt) = newbuf;
564 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
565 su->su_iosz, XDR_ENCODE);
566 victim->cache_xid = su->su_xid;
567 victim->cache_proc = uc->uc_proc;
568 victim->cache_vers = uc->uc_vers;
569 victim->cache_prog = uc->uc_prog;
570 victim->cache_addr = xprt->xp_rtaddr;
571 victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
572 (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
573 (size_t)xprt->xp_rtaddr.len);
574 loc = CACHE_LOC(xprt, victim->cache_xid);
575 victim->cache_next = uc->uc_entries[loc];
576 uc->uc_entries[loc] = victim;
577 uc->uc_fifo[uc->uc_nextvictim++] = victim;
578 uc->uc_nextvictim %= uc->uc_size;
579 mutex_unlock(&dupreq_lock);
580 }
581
582 /*
583 * Try to get an entry from the cache
584 * return 1 if found, 0 if not found and set the stage for cache_set()
585 */
586 static int
587 cache_get(xprt, msg, replyp, replylenp)
588 SVCXPRT *xprt;
589 struct rpc_msg *msg;
590 char **replyp;
591 size_t *replylenp;
592 {
593 u_int loc;
594 cache_ptr ent;
595 struct svc_dg_data *su;
596 struct cl_cache *uc;
597 #ifdef RPC_CACHE_DEBUG
598 struct netconfig *nconf;
599 char *uaddr;
600 #endif
601
602 _DIAGASSERT(xprt != NULL);
603 _DIAGASSERT(msg != NULL);
604 _DIAGASSERT(replyp != NULL);
605 _DIAGASSERT(replylenp != NULL);
606
607 su = su_data(xprt);
608 uc = (struct cl_cache *) su->su_cache;
609
610 mutex_lock(&dupreq_lock);
611 loc = CACHE_LOC(xprt, su->su_xid);
612 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
613 if (ent->cache_xid == su->su_xid &&
614 ent->cache_proc == msg->rm_call.cb_proc &&
615 ent->cache_vers == msg->rm_call.cb_vers &&
616 ent->cache_prog == msg->rm_call.cb_prog &&
617 ent->cache_addr.len == xprt->xp_rtaddr.len &&
618 (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
619 xprt->xp_rtaddr.len) == 0)) {
620 #ifdef RPC_CACHE_DEBUG
621 if (nconf = getnetconfigent(xprt->xp_netid)) {
622 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
623 freenetconfigent(nconf);
624 printf(
625 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
626 su->su_xid, msg->rm_call.cb_prog,
627 msg->rm_call.cb_vers,
628 msg->rm_call.cb_proc, uaddr);
629 free(uaddr);
630 }
631 #endif
632 *replyp = ent->cache_reply;
633 *replylenp = ent->cache_replylen;
634 mutex_unlock(&dupreq_lock);
635 return (1);
636 }
637 }
638 /*
639 * Failed to find entry
640 * Remember a few things so we can do a set later
641 */
642 uc->uc_proc = msg->rm_call.cb_proc;
643 uc->uc_vers = msg->rm_call.cb_vers;
644 uc->uc_prog = msg->rm_call.cb_prog;
645 mutex_unlock(&dupreq_lock);
646 return (0);
647 }
648