1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user. 8 * 9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12 * 13 * Sun RPC is provided with no support and without any obligation on the 14 * part of Sun Microsystems, Inc. to assist in its use, correction, 15 * modification or enhancement. 16 * 17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19 * OR ANY PART THEREOF. 20 * 21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22 * or profits or other special, indirect and consequential damages, even if 23 * Sun has been advised of the possibility of such damages. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 * 29 * @(#)svc_dg.c 1.17 94/04/24 SMI 30 * $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $ 31 * $FreeBSD: src/lib/libc/rpc/svc_dg.c,v 1.8 2006/02/27 22:10:59 deischen Exp $ 32 * $DragonFly$ 33 */ 34 35 /* 36 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 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 <rpc/svc_dg.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 #include "un-namespace.h" 62 63 #include "rpc_com.h" 64 #include "mt_misc.h" 65 66 #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2)) 67 #define rpc_buffer(xprt) ((xprt)->xp_p1) 68 69 #ifndef MAX 70 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 71 #endif 72 73 static void svc_dg_ops(SVCXPRT *); 74 static enum xprt_stat svc_dg_stat(SVCXPRT *); 75 static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *); 76 static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *); 77 static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *); 78 static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *); 79 static void svc_dg_destroy(SVCXPRT *); 80 static bool_t svc_dg_control(SVCXPRT *, const u_int, void *); 81 static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *); 82 static void cache_set(SVCXPRT *, size_t); 83 int svc_dg_enablecache(SVCXPRT *, u_int); 84 85 /* 86 * Usage: 87 * xprt = svc_dg_create(sock, sendsize, recvsize); 88 * Does other connectionless specific initializations. 89 * Once *xprt is initialized, it is registered. 90 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable 91 * system defaults are chosen. 92 * The routines returns NULL if a problem occurred. 93 */ 94 static const char svc_dg_str[] = "svc_dg_create: %s"; 95 static const char svc_dg_err1[] = "could not get transport information"; 96 static const char svc_dg_err2[] = " transport does not support data transfer"; 97 static const char __no_mem_str[] = "out of memory"; 98 99 SVCXPRT * 100 svc_dg_create(int fd, u_int sendsize, u_int recvsize) 101 { 102 SVCXPRT *xprt; 103 struct svc_dg_data *su = NULL; 104 struct __rpc_sockinfo si; 105 struct sockaddr_storage ss; 106 socklen_t slen; 107 108 if (!__rpc_fd2sockinfo(fd, &si)) { 109 warnx(svc_dg_str, svc_dg_err1); 110 return (NULL); 111 } 112 /* 113 * Find the receive and the send size 114 */ 115 sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize); 116 recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize); 117 if ((sendsize == 0) || (recvsize == 0)) { 118 warnx(svc_dg_str, svc_dg_err2); 119 return (NULL); 120 } 121 122 xprt = mem_alloc(sizeof (SVCXPRT)); 123 if (xprt == NULL) 124 goto freedata; 125 memset(xprt, 0, sizeof (SVCXPRT)); 126 127 su = mem_alloc(sizeof (*su)); 128 if (su == NULL) 129 goto freedata; 130 su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4; 131 if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) 132 goto freedata; 133 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, 134 XDR_DECODE); 135 su->su_cache = NULL; 136 xprt->xp_fd = fd; 137 xprt->xp_p2 = su; 138 xprt->xp_verf.oa_base = su->su_verfbody; 139 svc_dg_ops(xprt); 140 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage); 141 142 slen = sizeof ss; 143 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) 144 goto freedata; 145 xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage)); 146 xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage); 147 xprt->xp_ltaddr.len = slen; 148 memcpy(xprt->xp_ltaddr.buf, &ss, slen); 149 150 xprt_register(xprt); 151 return (xprt); 152 freedata: 153 warnx(svc_dg_str, __no_mem_str); 154 if (xprt) { 155 if (su) 156 mem_free(su, sizeof (*su)); 157 mem_free(xprt, sizeof (SVCXPRT)); 158 } 159 return (NULL); 160 } 161 162 /*ARGSUSED*/ 163 static enum xprt_stat 164 svc_dg_stat(SVCXPRT *xprt) 165 { 166 return (XPRT_IDLE); 167 } 168 169 static bool_t 170 svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg) 171 { 172 struct svc_dg_data *su = su_data(xprt); 173 XDR *xdrs = &(su->su_xdrs); 174 char *reply; 175 struct sockaddr_storage ss; 176 socklen_t alen; 177 size_t replylen; 178 ssize_t rlen; 179 180 again: 181 alen = sizeof (struct sockaddr_storage); 182 rlen = _recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz, 0, 183 (struct sockaddr *)(void *)&ss, &alen); 184 if (rlen == -1 && errno == EINTR) 185 goto again; 186 if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t)))) 187 return (FALSE); 188 if (xprt->xp_rtaddr.len < alen) { 189 if (xprt->xp_rtaddr.len != 0) 190 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len); 191 xprt->xp_rtaddr.buf = mem_alloc(alen); 192 xprt->xp_rtaddr.len = alen; 193 } 194 memcpy(xprt->xp_rtaddr.buf, &ss, alen); 195 #ifdef PORTMAP 196 if (ss.ss_family == AF_INET) { 197 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf; 198 xprt->xp_addrlen = sizeof (struct sockaddr_in); 199 } 200 #endif /* PORTMAP */ 201 xdrs->x_op = XDR_DECODE; 202 XDR_SETPOS(xdrs, 0); 203 if (! xdr_callmsg(xdrs, msg)) { 204 return (FALSE); 205 } 206 su->su_xid = msg->rm_xid; 207 if (su->su_cache != NULL) { 208 if (cache_get(xprt, msg, &reply, &replylen)) { 209 _sendto(xprt->xp_fd, reply, replylen, 0, 210 (struct sockaddr *)(void *)&ss, alen); 211 return (FALSE); 212 } 213 } 214 return (TRUE); 215 } 216 217 static bool_t 218 svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg) 219 { 220 struct svc_dg_data *su = su_data(xprt); 221 XDR *xdrs = &(su->su_xdrs); 222 bool_t stat = FALSE; 223 size_t slen; 224 225 xdrs->x_op = XDR_ENCODE; 226 XDR_SETPOS(xdrs, 0); 227 msg->rm_xid = su->su_xid; 228 if (xdr_replymsg(xdrs, msg)) { 229 slen = XDR_GETPOS(xdrs); 230 if (_sendto(xprt->xp_fd, rpc_buffer(xprt), slen, 0, 231 (struct sockaddr *)xprt->xp_rtaddr.buf, 232 (socklen_t)xprt->xp_rtaddr.len) == (ssize_t) slen) { 233 stat = TRUE; 234 if (su->su_cache) 235 cache_set(xprt, slen); 236 } 237 } 238 return (stat); 239 } 240 241 static bool_t 242 svc_dg_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr) 243 { 244 return (*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr); 245 } 246 247 static bool_t 248 svc_dg_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr) 249 { 250 XDR *xdrs = &(su_data(xprt)->su_xdrs); 251 252 xdrs->x_op = XDR_FREE; 253 return (*xdr_args)(xdrs, args_ptr); 254 } 255 256 static void 257 svc_dg_destroy(SVCXPRT *xprt) 258 { 259 struct svc_dg_data *su = su_data(xprt); 260 261 xprt_unregister(xprt); 262 if (xprt->xp_fd != -1) 263 _close(xprt->xp_fd); 264 XDR_DESTROY(&(su->su_xdrs)); 265 mem_free(rpc_buffer(xprt), su->su_iosz); 266 mem_free(su, sizeof (*su)); 267 if (xprt->xp_rtaddr.buf) 268 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen); 269 if (xprt->xp_ltaddr.buf) 270 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen); 271 if (xprt->xp_tp) 272 free(xprt->xp_tp); 273 mem_free(xprt, sizeof (SVCXPRT)); 274 } 275 276 static bool_t 277 /*ARGSUSED*/ 278 svc_dg_control(SVCXPRT *xprt, const u_int rq, void *in) 279 { 280 return (FALSE); 281 } 282 283 static void 284 svc_dg_ops(SVCXPRT *xprt) 285 { 286 static struct xp_ops ops; 287 static struct xp_ops2 ops2; 288 289 /* VARIABLES PROTECTED BY ops_lock: ops */ 290 291 mutex_lock(&ops_lock); 292 if (ops.xp_recv == NULL) { 293 ops.xp_recv = svc_dg_recv; 294 ops.xp_stat = svc_dg_stat; 295 ops.xp_getargs = svc_dg_getargs; 296 ops.xp_reply = svc_dg_reply; 297 ops.xp_freeargs = svc_dg_freeargs; 298 ops.xp_destroy = svc_dg_destroy; 299 ops2.xp_control = svc_dg_control; 300 } 301 xprt->xp_ops = &ops; 302 xprt->xp_ops2 = &ops2; 303 mutex_unlock(&ops_lock); 304 } 305 306 /* The CACHING COMPONENT */ 307 308 /* 309 * Could have been a separate file, but some part of it depends upon the 310 * private structure of the client handle. 311 * 312 * Fifo cache for cl server 313 * Copies pointers to reply buffers into fifo cache 314 * Buffers are sent again if retransmissions are detected. 315 */ 316 317 #define SPARSENESS 4 /* 75% sparse */ 318 319 #define ALLOC(type, size) \ 320 (type *) mem_alloc((sizeof (type) * (size))) 321 322 #define MEMZERO(addr, type, size) \ 323 memset((void *) (addr), 0, sizeof (type) * (int) (size)) 324 325 #define FREE(addr, type, size) \ 326 mem_free((addr), (sizeof (type) * (size))) 327 328 /* 329 * An entry in the cache 330 */ 331 typedef struct cache_node *cache_ptr; 332 struct cache_node { 333 /* 334 * Index into cache is xid, proc, vers, prog and address 335 */ 336 u_int32_t cache_xid; 337 rpcproc_t cache_proc; 338 rpcvers_t cache_vers; 339 rpcprog_t cache_prog; 340 struct netbuf cache_addr; 341 /* 342 * The cached reply and length 343 */ 344 char *cache_reply; 345 size_t cache_replylen; 346 /* 347 * Next node on the list, if there is a collision 348 */ 349 cache_ptr cache_next; 350 }; 351 352 /* 353 * The entire cache 354 */ 355 struct cl_cache { 356 u_int uc_size; /* size of cache */ 357 cache_ptr *uc_entries; /* hash table of entries in cache */ 358 cache_ptr *uc_fifo; /* fifo list of entries in cache */ 359 u_int uc_nextvictim; /* points to next victim in fifo list */ 360 rpcprog_t uc_prog; /* saved program number */ 361 rpcvers_t uc_vers; /* saved version number */ 362 rpcproc_t uc_proc; /* saved procedure number */ 363 }; 364 365 366 /* 367 * the hashing function 368 */ 369 #define CACHE_LOC(transp, xid) \ 370 (xid % (SPARSENESS * ((struct cl_cache *) \ 371 su_data(transp)->su_cache)->uc_size)) 372 373 /* 374 * Enable use of the cache. Returns 1 on success, 0 on failure. 375 * Note: there is no disable. 376 */ 377 static const char cache_enable_str[] = "svc_enablecache: %s %s"; 378 static const char alloc_err[] = "could not allocate cache "; 379 static const char enable_err[] = "cache already enabled"; 380 381 int 382 svc_dg_enablecache(SVCXPRT *transp, u_int size) 383 { 384 struct svc_dg_data *su = su_data(transp); 385 struct cl_cache *uc; 386 387 mutex_lock(&dupreq_lock); 388 if (su->su_cache != NULL) { 389 warnx(cache_enable_str, enable_err, " "); 390 mutex_unlock(&dupreq_lock); 391 return (0); 392 } 393 uc = ALLOC(struct cl_cache, 1); 394 if (uc == NULL) { 395 warnx(cache_enable_str, alloc_err, " "); 396 mutex_unlock(&dupreq_lock); 397 return (0); 398 } 399 uc->uc_size = size; 400 uc->uc_nextvictim = 0; 401 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS); 402 if (uc->uc_entries == NULL) { 403 warnx(cache_enable_str, alloc_err, "data"); 404 FREE(uc, struct cl_cache, 1); 405 mutex_unlock(&dupreq_lock); 406 return (0); 407 } 408 MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS); 409 uc->uc_fifo = ALLOC(cache_ptr, size); 410 if (uc->uc_fifo == NULL) { 411 warnx(cache_enable_str, alloc_err, "fifo"); 412 FREE(uc->uc_entries, cache_ptr, size * SPARSENESS); 413 FREE(uc, struct cl_cache, 1); 414 mutex_unlock(&dupreq_lock); 415 return (0); 416 } 417 MEMZERO(uc->uc_fifo, cache_ptr, size); 418 su->su_cache = (char *)(void *)uc; 419 mutex_unlock(&dupreq_lock); 420 return (1); 421 } 422 423 /* 424 * Set an entry in the cache. It assumes that the uc entry is set from 425 * the earlier call to cache_get() for the same procedure. This will always 426 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called 427 * by svc_dg_reply(). All this hoopla because the right RPC parameters are 428 * not available at svc_dg_reply time. 429 */ 430 431 static const char cache_set_str[] = "cache_set: %s"; 432 static const char cache_set_err1[] = "victim not found"; 433 static const char cache_set_err2[] = "victim alloc failed"; 434 static const char cache_set_err3[] = "could not allocate new rpc buffer"; 435 436 static void 437 cache_set(SVCXPRT *xprt, size_t replylen) 438 { 439 cache_ptr victim; 440 cache_ptr *vicp; 441 struct svc_dg_data *su = su_data(xprt); 442 struct cl_cache *uc = (struct cl_cache *) su->su_cache; 443 u_int loc; 444 char *newbuf; 445 #ifdef RPC_CACHE_DEBUG 446 struct netconfig *nconf; 447 char *uaddr; 448 #endif 449 450 mutex_lock(&dupreq_lock); 451 /* 452 * Find space for the new entry, either by 453 * reusing an old entry, or by mallocing a new one 454 */ 455 victim = uc->uc_fifo[uc->uc_nextvictim]; 456 if (victim != NULL) { 457 loc = CACHE_LOC(xprt, victim->cache_xid); 458 for (vicp = &uc->uc_entries[loc]; 459 *vicp != NULL && *vicp != victim; 460 vicp = &(*vicp)->cache_next) 461 ; 462 if (*vicp == NULL) { 463 warnx(cache_set_str, cache_set_err1); 464 mutex_unlock(&dupreq_lock); 465 return; 466 } 467 *vicp = victim->cache_next; /* remove from cache */ 468 newbuf = victim->cache_reply; 469 } else { 470 victim = ALLOC(struct cache_node, 1); 471 if (victim == NULL) { 472 warnx(cache_set_str, cache_set_err2); 473 mutex_unlock(&dupreq_lock); 474 return; 475 } 476 newbuf = mem_alloc(su->su_iosz); 477 if (newbuf == NULL) { 478 warnx(cache_set_str, cache_set_err3); 479 FREE(victim, struct cache_node, 1); 480 mutex_unlock(&dupreq_lock); 481 return; 482 } 483 } 484 485 /* 486 * Store it away 487 */ 488 #ifdef RPC_CACHE_DEBUG 489 if (nconf = getnetconfigent(xprt->xp_netid)) { 490 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr); 491 freenetconfigent(nconf); 492 printf( 493 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n", 494 su->su_xid, uc->uc_prog, uc->uc_vers, 495 uc->uc_proc, uaddr); 496 free(uaddr); 497 } 498 #endif 499 victim->cache_replylen = replylen; 500 victim->cache_reply = rpc_buffer(xprt); 501 rpc_buffer(xprt) = newbuf; 502 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), 503 su->su_iosz, XDR_ENCODE); 504 victim->cache_xid = su->su_xid; 505 victim->cache_proc = uc->uc_proc; 506 victim->cache_vers = uc->uc_vers; 507 victim->cache_prog = uc->uc_prog; 508 victim->cache_addr = xprt->xp_rtaddr; 509 victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len); 510 memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf, 511 (size_t)xprt->xp_rtaddr.len); 512 loc = CACHE_LOC(xprt, victim->cache_xid); 513 victim->cache_next = uc->uc_entries[loc]; 514 uc->uc_entries[loc] = victim; 515 uc->uc_fifo[uc->uc_nextvictim++] = victim; 516 uc->uc_nextvictim %= uc->uc_size; 517 mutex_unlock(&dupreq_lock); 518 } 519 520 /* 521 * Try to get an entry from the cache 522 * return 1 if found, 0 if not found and set the stage for cache_set() 523 */ 524 static int 525 cache_get(SVCXPRT *xprt, struct rpc_msg *msg, char **replyp, size_t *replylenp) 526 { 527 u_int loc; 528 cache_ptr ent; 529 struct svc_dg_data *su = su_data(xprt); 530 struct cl_cache *uc = (struct cl_cache *) su->su_cache; 531 #ifdef RPC_CACHE_DEBUG 532 struct netconfig *nconf; 533 char *uaddr; 534 #endif 535 536 mutex_lock(&dupreq_lock); 537 loc = CACHE_LOC(xprt, su->su_xid); 538 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) { 539 if (ent->cache_xid == su->su_xid && 540 ent->cache_proc == msg->rm_call.cb_proc && 541 ent->cache_vers == msg->rm_call.cb_vers && 542 ent->cache_prog == msg->rm_call.cb_prog && 543 ent->cache_addr.len == xprt->xp_rtaddr.len && 544 (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf, 545 xprt->xp_rtaddr.len) == 0)) { 546 #ifdef RPC_CACHE_DEBUG 547 if (nconf = getnetconfigent(xprt->xp_netid)) { 548 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr); 549 freenetconfigent(nconf); 550 printf( 551 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n", 552 su->su_xid, msg->rm_call.cb_prog, 553 msg->rm_call.cb_vers, 554 msg->rm_call.cb_proc, uaddr); 555 free(uaddr); 556 } 557 #endif 558 *replyp = ent->cache_reply; 559 *replylenp = ent->cache_replylen; 560 mutex_unlock(&dupreq_lock); 561 return (1); 562 } 563 } 564 /* 565 * Failed to find entry 566 * Remember a few things so we can do a set later 567 */ 568 uc->uc_proc = msg->rm_call.cb_proc; 569 uc->uc_vers = msg->rm_call.cb_vers; 570 uc->uc_prog = msg->rm_call.cb_prog; 571 mutex_unlock(&dupreq_lock); 572 return (0); 573 } 574