svc_dg.c revision 1.5 1 /* $NetBSD: svc_dg.c,v 1.5 2000/12/20 20:52:24 christos 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
86 /*
87 * Usage:
88 * xprt = svc_dg_create(sock, sendsize, recvsize);
89 * Does other connectionless specific initializations.
90 * Once *xprt is initialized, it is registered.
91 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
92 * system defaults are chosen.
93 * The routines returns NULL if a problem occurred.
94 */
95 static const char svc_dg_str[] = "svc_dg_create: %s";
96 static const char svc_dg_err1[] = "could not get transport information";
97 static const char svc_dg_err2[] = " transport does not support data transfer";
98 static const char __no_mem_str[] = "out of memory";
99
100 SVCXPRT *
101 svc_dg_create(fd, sendsize, recvsize)
102 int fd;
103 u_int sendsize;
104 u_int recvsize;
105 {
106 SVCXPRT *xprt;
107 struct svc_dg_data *su = NULL;
108 struct __rpc_sockinfo si;
109 struct sockaddr_storage ss;
110 socklen_t slen;
111
112 if (!__rpc_fd2sockinfo(fd, &si)) {
113 warnx(svc_dg_str, svc_dg_err1);
114 return (NULL);
115 }
116 /*
117 * Find the receive and the send size
118 */
119 sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
120 recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
121 if ((sendsize == 0) || (recvsize == 0)) {
122 warnx(svc_dg_str, svc_dg_err2);
123 return (NULL);
124 }
125
126 xprt = mem_alloc(sizeof (SVCXPRT));
127 if (xprt == NULL)
128 goto freedata;
129 memset(xprt, 0, sizeof (SVCXPRT));
130
131 su = mem_alloc(sizeof (*su));
132 if (su == NULL)
133 goto freedata;
134 su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
135 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
136 goto freedata;
137 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
138 XDR_DECODE);
139 su->su_cache = NULL;
140 xprt->xp_fd = fd;
141 xprt->xp_p2 = (caddr_t)(void *)su;
142 xprt->xp_verf.oa_base = su->su_verfbody;
143 svc_dg_ops(xprt);
144 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
145
146 slen = sizeof ss;
147 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
148 goto freedata;
149 xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
150 xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
151 xprt->xp_ltaddr.len = slen;
152 memcpy(xprt->xp_ltaddr.buf, &ss, slen);
153
154 xprt_register(xprt);
155 return (xprt);
156 freedata:
157 (void) warnx(svc_dg_str, __no_mem_str);
158 if (xprt) {
159 if (su)
160 (void) mem_free(su, sizeof (*su));
161 (void) mem_free(xprt, sizeof (SVCXPRT));
162 }
163 return (NULL);
164 }
165
166 /*ARGSUSED*/
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 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 *)(void *)&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 *)(void *)&ss, alen);
218 return (FALSE);
219 }
220 }
221 return (TRUE);
222 }
223
224 static bool_t
225 svc_dg_reply(xprt, msg)
226 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)
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 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 SVCXPRT *xprt;
274 {
275 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(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(xprt, sizeof (SVCXPRT));
290 }
291
292 static bool_t
293 /*ARGSUSED*/
294 svc_dg_control(xprt, rq, in)
295 SVCXPRT *xprt;
296 const u_int rq;
297 void *in;
298 {
299 return (FALSE);
300 }
301
302 static void
303 svc_dg_ops(xprt)
304 SVCXPRT *xprt;
305 {
306 static struct xp_ops ops;
307 static struct xp_ops2 ops2;
308 #ifdef __REENT
309 extern mutex_t ops_lock;
310 #endif
311
312 /* VARIABLES PROTECTED BY ops_lock: ops */
313
314 mutex_lock(&ops_lock);
315 if (ops.xp_recv == NULL) {
316 ops.xp_recv = svc_dg_recv;
317 ops.xp_stat = svc_dg_stat;
318 ops.xp_getargs = svc_dg_getargs;
319 ops.xp_reply = svc_dg_reply;
320 ops.xp_freeargs = svc_dg_freeargs;
321 ops.xp_destroy = svc_dg_destroy;
322 ops2.xp_control = svc_dg_control;
323 }
324 xprt->xp_ops = &ops;
325 xprt->xp_ops2 = &ops2;
326 mutex_unlock(&ops_lock);
327 }
328
329 /* The CACHING COMPONENT */
330
331 /*
332 * Could have been a separate file, but some part of it depends upon the
333 * private structure of the client handle.
334 *
335 * Fifo cache for cl server
336 * Copies pointers to reply buffers into fifo cache
337 * Buffers are sent again if retransmissions are detected.
338 */
339
340 #define SPARSENESS 4 /* 75% sparse */
341
342 #define ALLOC(type, size) \
343 (type *) mem_alloc((sizeof (type) * (size)))
344
345 #define MEMZERO(addr, type, size) \
346 (void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
347
348 #define FREE(addr, type, size) \
349 mem_free((addr), (sizeof (type) * (size)))
350
351 /*
352 * An entry in the cache
353 */
354 typedef struct cache_node *cache_ptr;
355 struct cache_node {
356 /*
357 * Index into cache is xid, proc, vers, prog and address
358 */
359 u_int32_t cache_xid;
360 rpcproc_t cache_proc;
361 rpcvers_t cache_vers;
362 rpcprog_t cache_prog;
363 struct netbuf cache_addr;
364 /*
365 * The cached reply and length
366 */
367 char *cache_reply;
368 size_t cache_replylen;
369 /*
370 * Next node on the list, if there is a collision
371 */
372 cache_ptr cache_next;
373 };
374
375 /*
376 * The entire cache
377 */
378 struct cl_cache {
379 u_int uc_size; /* size of cache */
380 cache_ptr *uc_entries; /* hash table of entries in cache */
381 cache_ptr *uc_fifo; /* fifo list of entries in cache */
382 u_int uc_nextvictim; /* points to next victim in fifo list */
383 rpcprog_t uc_prog; /* saved program number */
384 rpcvers_t uc_vers; /* saved version number */
385 rpcproc_t uc_proc; /* saved procedure number */
386 };
387
388
389 /*
390 * the hashing function
391 */
392 #define CACHE_LOC(transp, xid) \
393 (xid % (SPARSENESS * ((struct cl_cache *) \
394 su_data(transp)->su_cache)->uc_size))
395
396 #ifdef __REENT
397 extern mutex_t dupreq_lock;
398 #endif
399
400 /*
401 * Enable use of the cache. Returns 1 on success, 0 on failure.
402 * Note: there is no disable.
403 */
404 static const char cache_enable_str[] = "svc_enablecache: %s %s";
405 static const char alloc_err[] = "could not allocate cache ";
406 static const char enable_err[] = "cache already enabled";
407
408 int
409 svc_dg_enablecache(transp, size)
410 SVCXPRT *transp;
411 u_int size;
412 {
413 struct svc_dg_data *su = su_data(transp);
414 struct cl_cache *uc;
415
416 mutex_lock(&dupreq_lock);
417 if (su->su_cache != NULL) {
418 (void) warnx(cache_enable_str, enable_err, " ");
419 mutex_unlock(&dupreq_lock);
420 return (0);
421 }
422 uc = ALLOC(struct cl_cache, 1);
423 if (uc == NULL) {
424 warnx(cache_enable_str, alloc_err, " ");
425 mutex_unlock(&dupreq_lock);
426 return (0);
427 }
428 uc->uc_size = size;
429 uc->uc_nextvictim = 0;
430 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
431 if (uc->uc_entries == NULL) {
432 warnx(cache_enable_str, alloc_err, "data");
433 FREE(uc, struct cl_cache, 1);
434 mutex_unlock(&dupreq_lock);
435 return (0);
436 }
437 MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
438 uc->uc_fifo = ALLOC(cache_ptr, size);
439 if (uc->uc_fifo == NULL) {
440 warnx(cache_enable_str, alloc_err, "fifo");
441 FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
442 FREE(uc, struct cl_cache, 1);
443 mutex_unlock(&dupreq_lock);
444 return (0);
445 }
446 MEMZERO(uc->uc_fifo, cache_ptr, size);
447 su->su_cache = (char *)(void *)uc;
448 mutex_unlock(&dupreq_lock);
449 return (1);
450 }
451
452 /*
453 * Set an entry in the cache. It assumes that the uc entry is set from
454 * the earlier call to cache_get() for the same procedure. This will always
455 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
456 * by svc_dg_reply(). All this hoopla because the right RPC parameters are
457 * not available at svc_dg_reply time.
458 */
459
460 static const char cache_set_str[] = "cache_set: %s";
461 static const char cache_set_err1[] = "victim not found";
462 static const char cache_set_err2[] = "victim alloc failed";
463 static const char cache_set_err3[] = "could not allocate new rpc buffer";
464
465 static void
466 cache_set(xprt, replylen)
467 SVCXPRT *xprt;
468 size_t replylen;
469 {
470 cache_ptr victim;
471 cache_ptr *vicp;
472 struct svc_dg_data *su = su_data(xprt);
473 struct cl_cache *uc = (struct cl_cache *) su->su_cache;
474 u_int loc;
475 char *newbuf;
476 #ifdef RPC_CACHE_DEBUG
477 struct netconfig *nconf;
478 char *uaddr;
479 #endif
480
481 mutex_lock(&dupreq_lock);
482 /*
483 * Find space for the new entry, either by
484 * reusing an old entry, or by mallocing a new one
485 */
486 victim = uc->uc_fifo[uc->uc_nextvictim];
487 if (victim != NULL) {
488 loc = CACHE_LOC(xprt, victim->cache_xid);
489 for (vicp = &uc->uc_entries[loc];
490 *vicp != NULL && *vicp != victim;
491 vicp = &(*vicp)->cache_next)
492 ;
493 if (*vicp == NULL) {
494 warnx(cache_set_str, cache_set_err1);
495 mutex_unlock(&dupreq_lock);
496 return;
497 }
498 *vicp = victim->cache_next; /* remove from cache */
499 newbuf = victim->cache_reply;
500 } else {
501 victim = ALLOC(struct cache_node, 1);
502 if (victim == NULL) {
503 warnx(cache_set_str, cache_set_err2);
504 mutex_unlock(&dupreq_lock);
505 return;
506 }
507 newbuf = mem_alloc(su->su_iosz);
508 if (newbuf == NULL) {
509 warnx(cache_set_str, cache_set_err3);
510 FREE(victim, struct cache_node, 1);
511 mutex_unlock(&dupreq_lock);
512 return;
513 }
514 }
515
516 /*
517 * Store it away
518 */
519 #ifdef RPC_CACHE_DEBUG
520 if (nconf = getnetconfigent(xprt->xp_netid)) {
521 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
522 freenetconfigent(nconf);
523 printf(
524 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
525 su->su_xid, uc->uc_prog, uc->uc_vers,
526 uc->uc_proc, uaddr);
527 free(uaddr);
528 }
529 #endif
530 victim->cache_replylen = replylen;
531 victim->cache_reply = rpc_buffer(xprt);
532 rpc_buffer(xprt) = newbuf;
533 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
534 su->su_iosz, XDR_ENCODE);
535 victim->cache_xid = su->su_xid;
536 victim->cache_proc = uc->uc_proc;
537 victim->cache_vers = uc->uc_vers;
538 victim->cache_prog = uc->uc_prog;
539 victim->cache_addr = xprt->xp_rtaddr;
540 victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
541 (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
542 (size_t)xprt->xp_rtaddr.len);
543 loc = CACHE_LOC(xprt, victim->cache_xid);
544 victim->cache_next = uc->uc_entries[loc];
545 uc->uc_entries[loc] = victim;
546 uc->uc_fifo[uc->uc_nextvictim++] = victim;
547 uc->uc_nextvictim %= uc->uc_size;
548 mutex_unlock(&dupreq_lock);
549 }
550
551 /*
552 * Try to get an entry from the cache
553 * return 1 if found, 0 if not found and set the stage for cache_set()
554 */
555 static int
556 cache_get(xprt, msg, replyp, replylenp)
557 SVCXPRT *xprt;
558 struct rpc_msg *msg;
559 char **replyp;
560 size_t *replylenp;
561 {
562 u_int loc;
563 cache_ptr ent;
564 struct svc_dg_data *su = su_data(xprt);
565 struct cl_cache *uc = (struct cl_cache *) su->su_cache;
566 #ifdef RPC_CACHE_DEBUG
567 struct netconfig *nconf;
568 char *uaddr;
569 #endif
570
571 mutex_lock(&dupreq_lock);
572 loc = CACHE_LOC(xprt, su->su_xid);
573 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
574 if (ent->cache_xid == su->su_xid &&
575 ent->cache_proc == msg->rm_call.cb_proc &&
576 ent->cache_vers == msg->rm_call.cb_vers &&
577 ent->cache_prog == msg->rm_call.cb_prog &&
578 ent->cache_addr.len == xprt->xp_rtaddr.len &&
579 (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
580 xprt->xp_rtaddr.len) == 0)) {
581 #ifdef RPC_CACHE_DEBUG
582 if (nconf = getnetconfigent(xprt->xp_netid)) {
583 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
584 freenetconfigent(nconf);
585 printf(
586 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
587 su->su_xid, msg->rm_call.cb_prog,
588 msg->rm_call.cb_vers,
589 msg->rm_call.cb_proc, uaddr);
590 free(uaddr);
591 }
592 #endif
593 *replyp = ent->cache_reply;
594 *replylenp = ent->cache_replylen;
595 mutex_unlock(&dupreq_lock);
596 return (1);
597 }
598 }
599 /*
600 * Failed to find entry
601 * Remember a few things so we can do a set later
602 */
603 uc->uc_proc = msg->rm_call.cb_proc;
604 uc->uc_vers = msg->rm_call.cb_vers;
605 uc->uc_prog = msg->rm_call.cb_prog;
606 mutex_unlock(&dupreq_lock);
607 return (0);
608 }
609