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