svc_dg.c revision 1.3 1 /* $NetBSD: svc_dg.c,v 1.3 2000/06/22 11:06:23 fvdl 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 <errno.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #ifdef RPC_CACHE_DEBUG
56 #include <netconfig.h>
57 #include <netdir.h>
58 #endif
59 #include <err.h>
60
61 #include "rpc_com.h"
62 #include "svc_dg.h"
63
64 #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2))
65 #define rpc_buffer(xprt) ((xprt)->xp_p1)
66
67 #ifdef __weak_alias
68 __weak_alias(svc_dg_create,_svc_dg_create)
69 #endif
70
71 #ifndef MAX
72 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
73 #endif
74
75 static void svc_dg_ops __P((SVCXPRT *));
76 static enum xprt_stat svc_dg_stat __P((SVCXPRT *));
77 static bool_t svc_dg_recv __P((SVCXPRT *, struct rpc_msg *));
78 static bool_t svc_dg_reply __P((SVCXPRT *, struct rpc_msg *));
79 static bool_t svc_dg_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
80 static bool_t svc_dg_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
81 static void svc_dg_destroy __P((SVCXPRT *));
82 static bool_t svc_dg_control __P((SVCXPRT *, const u_int, void *));
83 static int cache_get __P((SVCXPRT *, struct rpc_msg *, char **, size_t *));
84 static void cache_set __P((SVCXPRT *, size_t));
85 int svc_dg_enablecache __P((SVCXPRT *, u_int));
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 ((SVCXPRT *)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 ((SVCXPRT *)NULL);
125 }
126
127 xprt = (SVCXPRT *)mem_alloc(sizeof (SVCXPRT));
128 if (xprt == NULL)
129 goto freedata;
130 memset((char *)xprt, 0, sizeof (SVCXPRT));
131
132 su = (struct svc_dg_data *)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) = (char *)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)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 *)&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((char *) su, sizeof (*su));
162 (void) mem_free((char *)xprt, sizeof (SVCXPRT));
163 }
164 return ((SVCXPRT *)NULL);
165 }
166
167 static enum xprt_stat
168 svc_dg_stat(xprt)
169 SVCXPRT *xprt;
170 {
171 return (XPRT_IDLE);
172 }
173
174 static bool_t
175 svc_dg_recv(xprt, msg)
176 register SVCXPRT *xprt;
177 struct rpc_msg *msg;
178 {
179 struct svc_dg_data *su = su_data(xprt);
180 XDR *xdrs = &(su->su_xdrs);
181 char *reply;
182 struct sockaddr_storage ss;
183 socklen_t alen;
184 size_t replylen;
185 int rlen;
186
187 again:
188 alen = sizeof (struct sockaddr_storage);
189 rlen = recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz, 0,
190 (struct sockaddr *)&ss, &alen);
191 if (rlen == -1 && errno == EINTR)
192 goto again;
193 if (rlen == -1 || (rlen < 4 * sizeof (u_int32_t)))
194 return (FALSE);
195 if (xprt->xp_rtaddr.len < alen) {
196 if (xprt->xp_rtaddr.len != 0)
197 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
198 xprt->xp_rtaddr.buf = mem_alloc(alen);
199 xprt->xp_rtaddr.len = alen;
200 }
201 memcpy(xprt->xp_rtaddr.buf, &ss, alen);
202 #ifdef PORTMAP
203 if (ss.ss_family == AF_INET) {
204 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
205 xprt->xp_addrlen = sizeof (struct sockaddr_in);
206 }
207 #endif
208 xdrs->x_op = XDR_DECODE;
209 XDR_SETPOS(xdrs, 0);
210 if (! xdr_callmsg(xdrs, msg)) {
211 return (FALSE);
212 }
213 su->su_xid = msg->rm_xid;
214 if (su->su_cache != NULL) {
215 if (cache_get(xprt, msg, &reply, &replylen)) {
216 (void)sendto(xprt->xp_fd, reply, replylen, 0,
217 (struct sockaddr *)&ss, alen);
218 return (FALSE);
219 }
220 }
221 return (TRUE);
222 }
223
224 static bool_t
225 svc_dg_reply(xprt, msg)
226 register SVCXPRT *xprt;
227 struct rpc_msg *msg;
228 {
229 struct svc_dg_data *su = su_data(xprt);
230 XDR *xdrs = &(su->su_xdrs);
231 bool_t stat = FALSE;
232 size_t slen;
233
234 xdrs->x_op = XDR_ENCODE;
235 XDR_SETPOS(xdrs, 0);
236 msg->rm_xid = su->su_xid;
237 if (xdr_replymsg(xdrs, msg)) {
238 slen = XDR_GETPOS(xdrs);
239 if (sendto(xprt->xp_fd, rpc_buffer(xprt), slen, 0,
240 (struct sockaddr *)xprt->xp_rtaddr.buf,
241 (socklen_t)xprt->xp_rtaddr.len) == slen) {
242 stat = TRUE;
243 if (su->su_cache && slen >= 0)
244 cache_set(xprt, slen);
245 }
246 }
247 return (stat);
248 }
249
250 static bool_t
251 svc_dg_getargs(xprt, xdr_args, args_ptr)
252 SVCXPRT *xprt;
253 xdrproc_t xdr_args;
254 caddr_t args_ptr;
255 {
256 return (*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr);
257 }
258
259 static bool_t
260 svc_dg_freeargs(xprt, xdr_args, args_ptr)
261 SVCXPRT *xprt;
262 xdrproc_t xdr_args;
263 caddr_t args_ptr;
264 {
265 register XDR *xdrs = &(su_data(xprt)->su_xdrs);
266
267 xdrs->x_op = XDR_FREE;
268 return (*xdr_args)(xdrs, args_ptr);
269 }
270
271 static void
272 svc_dg_destroy(xprt)
273 register SVCXPRT *xprt;
274 {
275 register struct svc_dg_data *su = su_data(xprt);
276
277 xprt_unregister(xprt);
278 if (xprt->xp_fd != -1)
279 (void)close(xprt->xp_fd);
280 XDR_DESTROY(&(su->su_xdrs));
281 (void) mem_free(rpc_buffer(xprt), su->su_iosz);
282 (void) mem_free((caddr_t)su, sizeof (*su));
283 if (xprt->xp_rtaddr.buf)
284 (void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
285 if (xprt->xp_ltaddr.buf)
286 (void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
287 if (xprt->xp_tp)
288 (void) free(xprt->xp_tp);
289 (void) mem_free((caddr_t)xprt, sizeof (SVCXPRT));
290 }
291
292 static bool_t
293 svc_dg_control(xprt, rq, in)
294 SVCXPRT *xprt;
295 const u_int rq;
296 void *in;
297 {
298 return (FALSE);
299 }
300
301 static void
302 svc_dg_ops(xprt)
303 SVCXPRT *xprt;
304 {
305 static struct xp_ops ops;
306 static struct xp_ops2 ops2;
307 #ifdef __REENT
308 extern mutex_t ops_lock;
309 #endif
310
311 /* VARIABLES PROTECTED BY ops_lock: ops */
312
313 mutex_lock(&ops_lock);
314 if (ops.xp_recv == NULL) {
315 ops.xp_recv = svc_dg_recv;
316 ops.xp_stat = svc_dg_stat;
317 ops.xp_getargs = svc_dg_getargs;
318 ops.xp_reply = svc_dg_reply;
319 ops.xp_freeargs = svc_dg_freeargs;
320 ops.xp_destroy = svc_dg_destroy;
321 ops2.xp_control = svc_dg_control;
322 }
323 xprt->xp_ops = &ops;
324 xprt->xp_ops2 = &ops2;
325 mutex_unlock(&ops_lock);
326 }
327
328 /* The CACHING COMPONENT */
329
330 /*
331 * Could have been a separate file, but some part of it depends upon the
332 * private structure of the client handle.
333 *
334 * Fifo cache for cl server
335 * Copies pointers to reply buffers into fifo cache
336 * Buffers are sent again if retransmissions are detected.
337 */
338
339 #define SPARSENESS 4 /* 75% sparse */
340
341 #define ALLOC(type, size) \
342 (type *) mem_alloc((unsigned) (sizeof (type) * (size)))
343
344 #define MEMZERO(addr, type, size) \
345 (void) memset((char *) (addr), 0, sizeof (type) * (int) (size))
346
347 #define FREE(addr, type, size) \
348 mem_free((char *) (addr), (sizeof (type) * (size)))
349
350 /*
351 * An entry in the cache
352 */
353 typedef struct cache_node *cache_ptr;
354 struct cache_node {
355 /*
356 * Index into cache is xid, proc, vers, prog and address
357 */
358 u_int32_t cache_xid;
359 rpcproc_t cache_proc;
360 rpcvers_t cache_vers;
361 rpcprog_t cache_prog;
362 struct netbuf cache_addr;
363 /*
364 * The cached reply and length
365 */
366 char *cache_reply;
367 size_t cache_replylen;
368 /*
369 * Next node on the list, if there is a collision
370 */
371 cache_ptr cache_next;
372 };
373
374 /*
375 * The entire cache
376 */
377 struct cl_cache {
378 u_int uc_size; /* size of cache */
379 cache_ptr *uc_entries; /* hash table of entries in cache */
380 cache_ptr *uc_fifo; /* fifo list of entries in cache */
381 u_int uc_nextvictim; /* points to next victim in fifo list */
382 rpcprog_t uc_prog; /* saved program number */
383 rpcvers_t uc_vers; /* saved version number */
384 rpcproc_t uc_proc; /* saved procedure number */
385 };
386
387
388 /*
389 * the hashing function
390 */
391 #define CACHE_LOC(transp, xid) \
392 (xid % (SPARSENESS * ((struct cl_cache *) \
393 su_data(transp)->su_cache)->uc_size))
394
395 #ifdef __REENT
396 extern mutex_t dupreq_lock;
397 #endif
398
399 /*
400 * Enable use of the cache. Returns 1 on success, 0 on failure.
401 * Note: there is no disable.
402 */
403 static const char cache_enable_str[] = "svc_enablecache: %s %s";
404 static const char alloc_err[] = "could not allocate cache ";
405 static const char enable_err[] = "cache already enabled";
406
407 int
408 svc_dg_enablecache(transp, size)
409 SVCXPRT *transp;
410 u_int size;
411 {
412 struct svc_dg_data *su = su_data(transp);
413 struct cl_cache *uc;
414
415 mutex_lock(&dupreq_lock);
416 if (su->su_cache != NULL) {
417 (void) warnx(cache_enable_str, enable_err, " ");
418 mutex_unlock(&dupreq_lock);
419 return (0);
420 }
421 uc = ALLOC(struct cl_cache, 1);
422 if (uc == NULL) {
423 warnx(cache_enable_str, alloc_err, " ");
424 mutex_unlock(&dupreq_lock);
425 return (0);
426 }
427 uc->uc_size = size;
428 uc->uc_nextvictim = 0;
429 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
430 if (uc->uc_entries == NULL) {
431 warnx(cache_enable_str, alloc_err, "data");
432 FREE(uc, struct cl_cache, 1);
433 mutex_unlock(&dupreq_lock);
434 return (0);
435 }
436 MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
437 uc->uc_fifo = ALLOC(cache_ptr, size);
438 if (uc->uc_fifo == NULL) {
439 warnx(cache_enable_str, alloc_err, "fifo");
440 FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
441 FREE(uc, struct cl_cache, 1);
442 mutex_unlock(&dupreq_lock);
443 return (0);
444 }
445 MEMZERO(uc->uc_fifo, cache_ptr, size);
446 su->su_cache = (char *) uc;
447 mutex_unlock(&dupreq_lock);
448 return (1);
449 }
450
451 /*
452 * Set an entry in the cache. It assumes that the uc entry is set from
453 * the earlier call to cache_get() for the same procedure. This will always
454 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
455 * by svc_dg_reply(). All this hoopla because the right RPC parameters are
456 * not available at svc_dg_reply time.
457 */
458
459 static const char cache_set_str[] = "cache_set: %s";
460 static const char cache_set_err1[] = "victim not found";
461 static const char cache_set_err2[] = "victim alloc failed";
462 static const char cache_set_err3[] = "could not allocate new rpc buffer";
463
464 static void
465 cache_set(xprt, replylen)
466 SVCXPRT *xprt;
467 size_t replylen;
468 {
469 register cache_ptr victim;
470 register cache_ptr *vicp;
471 register struct svc_dg_data *su = su_data(xprt);
472 struct cl_cache *uc = (struct cl_cache *) su->su_cache;
473 u_int loc;
474 char *newbuf;
475 #ifdef RPC_CACHE_DEBUG
476 struct netconfig *nconf;
477 char *uaddr;
478 #endif
479
480 mutex_lock(&dupreq_lock);
481 /*
482 * Find space for the new entry, either by
483 * reusing an old entry, or by mallocing a new one
484 */
485 victim = uc->uc_fifo[uc->uc_nextvictim];
486 if (victim != NULL) {
487 loc = CACHE_LOC(xprt, victim->cache_xid);
488 for (vicp = &uc->uc_entries[loc];
489 *vicp != NULL && *vicp != victim;
490 vicp = &(*vicp)->cache_next)
491 ;
492 if (*vicp == NULL) {
493 warnx(cache_set_str, cache_set_err1);
494 mutex_unlock(&dupreq_lock);
495 return;
496 }
497 *vicp = victim->cache_next; /* remove from cache */
498 newbuf = victim->cache_reply;
499 } else {
500 victim = ALLOC(struct cache_node, 1);
501 if (victim == NULL) {
502 warnx(cache_set_str, cache_set_err2);
503 mutex_unlock(&dupreq_lock);
504 return;
505 }
506 newbuf = (char *)mem_alloc(su->su_iosz);
507 if (newbuf == NULL) {
508 warnx(cache_set_str, cache_set_err3);
509 FREE(victim, struct cache_node, 1);
510 mutex_unlock(&dupreq_lock);
511 return;
512 }
513 }
514
515 /*
516 * Store it away
517 */
518 #ifdef RPC_CACHE_DEBUG
519 if (nconf = getnetconfigent(xprt->xp_netid)) {
520 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
521 freenetconfigent(nconf);
522 printf(
523 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
524 su->su_xid, uc->uc_prog, uc->uc_vers,
525 uc->uc_proc, uaddr);
526 free(uaddr);
527 }
528 #endif
529 victim->cache_replylen = replylen;
530 victim->cache_reply = rpc_buffer(xprt);
531 rpc_buffer(xprt) = newbuf;
532 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
533 su->su_iosz, XDR_ENCODE);
534 victim->cache_xid = su->su_xid;
535 victim->cache_proc = uc->uc_proc;
536 victim->cache_vers = uc->uc_vers;
537 victim->cache_prog = uc->uc_prog;
538 victim->cache_addr = xprt->xp_rtaddr;
539 victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
540 (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
541 (int)xprt->xp_rtaddr.len);
542 loc = CACHE_LOC(xprt, victim->cache_xid);
543 victim->cache_next = uc->uc_entries[loc];
544 uc->uc_entries[loc] = victim;
545 uc->uc_fifo[uc->uc_nextvictim++] = victim;
546 uc->uc_nextvictim %= uc->uc_size;
547 mutex_unlock(&dupreq_lock);
548 }
549
550 /*
551 * Try to get an entry from the cache
552 * return 1 if found, 0 if not found and set the stage for cache_set()
553 */
554 static int
555 cache_get(xprt, msg, replyp, replylenp)
556 SVCXPRT *xprt;
557 struct rpc_msg *msg;
558 char **replyp;
559 size_t *replylenp;
560 {
561 u_int loc;
562 register cache_ptr ent;
563 register struct svc_dg_data *su = su_data(xprt);
564 register struct cl_cache *uc = (struct cl_cache *) su->su_cache;
565 #ifdef RPC_CACHE_DEBUG
566 struct netconfig *nconf;
567 char *uaddr;
568 #endif
569
570 mutex_lock(&dupreq_lock);
571 loc = CACHE_LOC(xprt, su->su_xid);
572 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
573 if (ent->cache_xid == su->su_xid &&
574 ent->cache_proc == msg->rm_call.cb_proc &&
575 ent->cache_vers == msg->rm_call.cb_vers &&
576 ent->cache_prog == msg->rm_call.cb_prog &&
577 ent->cache_addr.len == xprt->xp_rtaddr.len &&
578 (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
579 xprt->xp_rtaddr.len) == 0)) {
580 #ifdef RPC_CACHE_DEBUG
581 if (nconf = getnetconfigent(xprt->xp_netid)) {
582 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
583 freenetconfigent(nconf);
584 printf(
585 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
586 su->su_xid, msg->rm_call.cb_prog,
587 msg->rm_call.cb_vers,
588 msg->rm_call.cb_proc, uaddr);
589 free(uaddr);
590 }
591 #endif
592 *replyp = ent->cache_reply;
593 *replylenp = ent->cache_replylen;
594 mutex_unlock(&dupreq_lock);
595 return (1);
596 }
597 }
598 /*
599 * Failed to find entry
600 * Remember a few things so we can do a set later
601 */
602 uc->uc_proc = msg->rm_call.cb_proc;
603 uc->uc_vers = msg->rm_call.cb_vers;
604 uc->uc_prog = msg->rm_call.cb_prog;
605 mutex_unlock(&dupreq_lock);
606 return (0);
607 }
608