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