1 /* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2009, Sun Microsystems, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * - Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * - Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * - Neither the name of Sun Microsystems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 33 */ 34 35 #if defined(LIBC_SCCS) && !defined(lint) 36 #ident "@(#)svc_dg.c 1.17 94/04/24 SMI" 37 #endif 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 /* 42 * svc_dg.c, Server side for connectionless RPC. 43 * 44 * Does some caching in the hopes of achieving execute-at-most-once semantics. 45 */ 46 47 #include "namespace.h" 48 #include "reentrant.h" 49 #include <sys/types.h> 50 #include <sys/socket.h> 51 #include <rpc/rpc.h> 52 #include <rpc/svc_dg.h> 53 #include <assert.h> 54 #include <errno.h> 55 #include <unistd.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #ifdef RPC_CACHE_DEBUG 60 #include <netconfig.h> 61 #include <netdir.h> 62 #endif 63 #include <err.h> 64 #include "un-namespace.h" 65 66 #include "rpc_com.h" 67 #include "mt_misc.h" 68 69 #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2)) 70 #define rpc_buffer(xprt) ((xprt)->xp_p1) 71 72 #ifndef MAX 73 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 74 #endif 75 76 static void svc_dg_ops(SVCXPRT *); 77 static enum xprt_stat svc_dg_stat(SVCXPRT *); 78 static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *); 79 static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *); 80 static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *); 81 static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *); 82 static void svc_dg_destroy(SVCXPRT *); 83 static bool_t svc_dg_control(SVCXPRT *, const u_int, void *); 84 static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *); 85 static void cache_set(SVCXPRT *, size_t); 86 int svc_dg_enablecache(SVCXPRT *, u_int); 87 88 /* 89 * Usage: 90 * xprt = svc_dg_create(sock, sendsize, recvsize); 91 * Does other connectionless specific initializations. 92 * Once *xprt is initialized, it is registered. 93 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable 94 * system defaults are chosen. 95 * The routines returns NULL if a problem occurred. 96 */ 97 static const char svc_dg_str[] = "svc_dg_create: %s"; 98 static const char svc_dg_err1[] = "could not get transport information"; 99 static const char svc_dg_err2[] = "transport does not support data transfer"; 100 static const char svc_dg_err3[] = "getsockname failed"; 101 static const char svc_dg_err4[] = "cannot set IP_RECVDSTADDR"; 102 static const char __no_mem_str[] = "out of memory"; 103 104 SVCXPRT * 105 svc_dg_create(int fd, u_int sendsize, 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 (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 (NULL); 125 } 126 127 xprt = svc_xprt_alloc(); 128 if (xprt == NULL) 129 goto freedata; 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 = 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 warnx(svc_dg_str, svc_dg_err3); 149 goto freedata_nowarn; 150 } 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 if (ss.ss_family == AF_INET) { 157 struct sockaddr_in *sin; 158 static const int true_value = 1; 159 160 sin = (struct sockaddr_in *)(void *)&ss; 161 if (sin->sin_addr.s_addr == INADDR_ANY) { 162 su->su_srcaddr.buf = mem_alloc(sizeof (ss)); 163 su->su_srcaddr.maxlen = sizeof (ss); 164 165 if (_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, 166 &true_value, sizeof(true_value))) { 167 warnx(svc_dg_str, svc_dg_err4); 168 goto freedata_nowarn; 169 } 170 } 171 } 172 173 xprt_register(xprt); 174 return (xprt); 175 freedata: 176 (void) warnx(svc_dg_str, __no_mem_str); 177 freedata_nowarn: 178 if (xprt) { 179 if (su) 180 (void) mem_free(su, sizeof (*su)); 181 svc_xprt_free(xprt); 182 } 183 return (NULL); 184 } 185 186 /*ARGSUSED*/ 187 static enum xprt_stat 188 svc_dg_stat(SVCXPRT *xprt) 189 { 190 return (XPRT_IDLE); 191 } 192 193 static int 194 svc_dg_recvfrom(int fd, char *buf, int buflen, 195 struct sockaddr *raddr, socklen_t *raddrlen, 196 struct sockaddr *laddr, socklen_t *laddrlen) 197 { 198 struct msghdr msg; 199 struct iovec msg_iov[1]; 200 struct sockaddr_in *lin = (struct sockaddr_in *)laddr; 201 int rlen; 202 bool_t have_lin = FALSE; 203 char tmp[CMSG_LEN(sizeof(*lin))]; 204 struct cmsghdr *cmsg; 205 206 memset((char *)&msg, 0, sizeof(msg)); 207 msg_iov[0].iov_base = buf; 208 msg_iov[0].iov_len = buflen; 209 msg.msg_iov = msg_iov; 210 msg.msg_iovlen = 1; 211 msg.msg_namelen = *raddrlen; 212 msg.msg_name = (char *)raddr; 213 if (laddr != NULL) { 214 msg.msg_control = (caddr_t)tmp; 215 msg.msg_controllen = CMSG_LEN(sizeof(*lin)); 216 } 217 rlen = _recvmsg(fd, &msg, 0); 218 if (rlen >= 0) 219 *raddrlen = msg.msg_namelen; 220 221 if (rlen == -1 || laddr == NULL || 222 msg.msg_controllen < sizeof(struct cmsghdr) || 223 msg.msg_flags & MSG_CTRUNC) 224 return rlen; 225 226 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; 227 cmsg = CMSG_NXTHDR(&msg, cmsg)) { 228 if (cmsg->cmsg_level == IPPROTO_IP && 229 cmsg->cmsg_type == IP_RECVDSTADDR) { 230 have_lin = TRUE; 231 memcpy(&lin->sin_addr, 232 (struct in_addr *)CMSG_DATA(cmsg), 233 sizeof(struct in_addr)); 234 break; 235 } 236 } 237 238 lin->sin_family = AF_INET; 239 lin->sin_port = 0; 240 *laddrlen = sizeof(struct sockaddr_in); 241 242 if (!have_lin) 243 lin->sin_addr.s_addr = INADDR_ANY; 244 245 return rlen; 246 } 247 248 static bool_t 249 svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg) 250 { 251 struct svc_dg_data *su = su_data(xprt); 252 XDR *xdrs = &(su->su_xdrs); 253 char *reply; 254 struct sockaddr_storage ss; 255 socklen_t alen; 256 size_t replylen; 257 ssize_t rlen; 258 259 again: 260 alen = sizeof (struct sockaddr_storage); 261 rlen = svc_dg_recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz, 262 (struct sockaddr *)(void *)&ss, &alen, 263 (struct sockaddr *)su->su_srcaddr.buf, &su->su_srcaddr.len); 264 if (rlen == -1 && errno == EINTR) 265 goto again; 266 if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t)))) 267 return (FALSE); 268 if (xprt->xp_rtaddr.len < alen) { 269 if (xprt->xp_rtaddr.len != 0) 270 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len); 271 xprt->xp_rtaddr.buf = mem_alloc(alen); 272 xprt->xp_rtaddr.len = alen; 273 } 274 memcpy(xprt->xp_rtaddr.buf, &ss, alen); 275 #ifdef PORTMAP 276 if (ss.ss_family == AF_INET) { 277 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf; 278 xprt->xp_addrlen = sizeof (struct sockaddr_in); 279 } 280 #endif /* PORTMAP */ 281 xdrs->x_op = XDR_DECODE; 282 XDR_SETPOS(xdrs, 0); 283 if (! xdr_callmsg(xdrs, msg)) { 284 return (FALSE); 285 } 286 su->su_xid = msg->rm_xid; 287 if (su->su_cache != NULL) { 288 if (cache_get(xprt, msg, &reply, &replylen)) { 289 (void)_sendto(xprt->xp_fd, reply, replylen, 0, 290 (struct sockaddr *)(void *)&ss, alen); 291 return (FALSE); 292 } 293 } 294 return (TRUE); 295 } 296 297 static int 298 svc_dg_sendto(int fd, char *buf, int buflen, 299 const struct sockaddr *raddr, socklen_t raddrlen, 300 const struct sockaddr *laddr, socklen_t laddrlen) 301 { 302 struct msghdr msg; 303 struct iovec msg_iov[1]; 304 struct sockaddr_in *laddr_in = (struct sockaddr_in *)laddr; 305 struct in_addr *lin = &laddr_in->sin_addr; 306 char tmp[CMSG_SPACE(sizeof(*lin))]; 307 struct cmsghdr *cmsg; 308 309 memset((char *)&msg, 0, sizeof(msg)); 310 msg_iov[0].iov_base = buf; 311 msg_iov[0].iov_len = buflen; 312 msg.msg_iov = msg_iov; 313 msg.msg_iovlen = 1; 314 msg.msg_namelen = raddrlen; 315 msg.msg_name = (char *)raddr; 316 317 if (laddr != NULL && laddr->sa_family == AF_INET && 318 lin->s_addr != INADDR_ANY) { 319 msg.msg_control = (caddr_t)tmp; 320 msg.msg_controllen = CMSG_LEN(sizeof(*lin)); 321 cmsg = CMSG_FIRSTHDR(&msg); 322 cmsg->cmsg_len = CMSG_LEN(sizeof(*lin)); 323 cmsg->cmsg_level = IPPROTO_IP; 324 cmsg->cmsg_type = IP_SENDSRCADDR; 325 memcpy(CMSG_DATA(cmsg), lin, sizeof(*lin)); 326 } 327 328 return _sendmsg(fd, &msg, 0); 329 } 330 331 static bool_t 332 svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg) 333 { 334 struct svc_dg_data *su = su_data(xprt); 335 XDR *xdrs = &(su->su_xdrs); 336 bool_t stat = TRUE; 337 size_t slen; 338 xdrproc_t xdr_proc; 339 caddr_t xdr_where; 340 341 xdrs->x_op = XDR_ENCODE; 342 XDR_SETPOS(xdrs, 0); 343 msg->rm_xid = su->su_xid; 344 if (msg->rm_reply.rp_stat == MSG_ACCEPTED && 345 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) { 346 xdr_proc = msg->acpted_rply.ar_results.proc; 347 xdr_where = msg->acpted_rply.ar_results.where; 348 msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void; 349 msg->acpted_rply.ar_results.where = NULL; 350 351 if (!xdr_replymsg(xdrs, msg) || 352 !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where)) 353 stat = FALSE; 354 } else { 355 stat = xdr_replymsg(xdrs, msg); 356 } 357 if (stat) { 358 slen = XDR_GETPOS(xdrs); 359 if (svc_dg_sendto(xprt->xp_fd, rpc_buffer(xprt), slen, 360 (struct sockaddr *)xprt->xp_rtaddr.buf, 361 (socklen_t)xprt->xp_rtaddr.len, 362 (struct sockaddr *)su->su_srcaddr.buf, 363 (socklen_t)su->su_srcaddr.len) == (ssize_t) slen) { 364 stat = TRUE; 365 if (su->su_cache) 366 cache_set(xprt, slen); 367 } 368 } 369 return (stat); 370 } 371 372 static bool_t 373 svc_dg_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr) 374 { 375 struct svc_dg_data *su; 376 377 assert(xprt != NULL); 378 su = su_data(xprt); 379 return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt), 380 &su->su_xdrs, xdr_args, args_ptr)); 381 } 382 383 static bool_t 384 svc_dg_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr) 385 { 386 XDR *xdrs = &(su_data(xprt)->su_xdrs); 387 388 xdrs->x_op = XDR_FREE; 389 return (*xdr_args)(xdrs, args_ptr); 390 } 391 392 static void 393 svc_dg_destroy(SVCXPRT *xprt) 394 { 395 struct svc_dg_data *su = su_data(xprt); 396 397 xprt_unregister(xprt); 398 if (xprt->xp_fd != -1) 399 (void)_close(xprt->xp_fd); 400 XDR_DESTROY(&(su->su_xdrs)); 401 (void) mem_free(rpc_buffer(xprt), su->su_iosz); 402 if (su->su_srcaddr.buf) 403 (void) mem_free(su->su_srcaddr.buf, su->su_srcaddr.maxlen); 404 (void) mem_free(su, sizeof (*su)); 405 if (xprt->xp_rtaddr.buf) 406 (void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen); 407 if (xprt->xp_ltaddr.buf) 408 (void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen); 409 if (xprt->xp_tp) 410 (void) free(xprt->xp_tp); 411 svc_xprt_free(xprt); 412 } 413 414 static bool_t 415 /*ARGSUSED*/ 416 svc_dg_control(SVCXPRT *xprt, const u_int rq, void *in) 417 { 418 return (FALSE); 419 } 420 421 static void 422 svc_dg_ops(SVCXPRT *xprt) 423 { 424 static struct xp_ops ops; 425 static struct xp_ops2 ops2; 426 427 /* VARIABLES PROTECTED BY ops_lock: ops */ 428 429 mutex_lock(&ops_lock); 430 if (ops.xp_recv == NULL) { 431 ops.xp_recv = svc_dg_recv; 432 ops.xp_stat = svc_dg_stat; 433 ops.xp_getargs = svc_dg_getargs; 434 ops.xp_reply = svc_dg_reply; 435 ops.xp_freeargs = svc_dg_freeargs; 436 ops.xp_destroy = svc_dg_destroy; 437 ops2.xp_control = svc_dg_control; 438 } 439 xprt->xp_ops = &ops; 440 xprt->xp_ops2 = &ops2; 441 mutex_unlock(&ops_lock); 442 } 443 444 /* The CACHING COMPONENT */ 445 446 /* 447 * Could have been a separate file, but some part of it depends upon the 448 * private structure of the client handle. 449 * 450 * Fifo cache for cl server 451 * Copies pointers to reply buffers into fifo cache 452 * Buffers are sent again if retransmissions are detected. 453 */ 454 455 #define SPARSENESS 4 /* 75% sparse */ 456 457 #define ALLOC(type, size) \ 458 (type *) mem_alloc((sizeof (type) * (size))) 459 460 #define MEMZERO(addr, type, size) \ 461 (void) memset((void *) (addr), 0, sizeof (type) * (int) (size)) 462 463 #define FREE(addr, type, size) \ 464 mem_free((addr), (sizeof (type) * (size))) 465 466 /* 467 * An entry in the cache 468 */ 469 typedef struct cache_node *cache_ptr; 470 struct cache_node { 471 /* 472 * Index into cache is xid, proc, vers, prog and address 473 */ 474 u_int32_t cache_xid; 475 rpcproc_t cache_proc; 476 rpcvers_t cache_vers; 477 rpcprog_t cache_prog; 478 struct netbuf cache_addr; 479 /* 480 * The cached reply and length 481 */ 482 char *cache_reply; 483 size_t cache_replylen; 484 /* 485 * Next node on the list, if there is a collision 486 */ 487 cache_ptr cache_next; 488 }; 489 490 /* 491 * The entire cache 492 */ 493 struct cl_cache { 494 u_int uc_size; /* size of cache */ 495 cache_ptr *uc_entries; /* hash table of entries in cache */ 496 cache_ptr *uc_fifo; /* fifo list of entries in cache */ 497 u_int uc_nextvictim; /* points to next victim in fifo list */ 498 rpcprog_t uc_prog; /* saved program number */ 499 rpcvers_t uc_vers; /* saved version number */ 500 rpcproc_t uc_proc; /* saved procedure number */ 501 }; 502 503 504 /* 505 * the hashing function 506 */ 507 #define CACHE_LOC(transp, xid) \ 508 (xid % (SPARSENESS * ((struct cl_cache *) \ 509 su_data(transp)->su_cache)->uc_size)) 510 511 /* 512 * Enable use of the cache. Returns 1 on success, 0 on failure. 513 * Note: there is no disable. 514 */ 515 static const char cache_enable_str[] = "svc_enablecache: %s %s"; 516 static const char alloc_err[] = "could not allocate cache "; 517 static const char enable_err[] = "cache already enabled"; 518 519 int 520 svc_dg_enablecache(SVCXPRT *transp, u_int size) 521 { 522 struct svc_dg_data *su = su_data(transp); 523 struct cl_cache *uc; 524 525 mutex_lock(&dupreq_lock); 526 if (su->su_cache != NULL) { 527 (void) warnx(cache_enable_str, enable_err, " "); 528 mutex_unlock(&dupreq_lock); 529 return (0); 530 } 531 uc = ALLOC(struct cl_cache, 1); 532 if (uc == NULL) { 533 warnx(cache_enable_str, alloc_err, " "); 534 mutex_unlock(&dupreq_lock); 535 return (0); 536 } 537 uc->uc_size = size; 538 uc->uc_nextvictim = 0; 539 uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS); 540 if (uc->uc_entries == NULL) { 541 warnx(cache_enable_str, alloc_err, "data"); 542 FREE(uc, struct cl_cache, 1); 543 mutex_unlock(&dupreq_lock); 544 return (0); 545 } 546 MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS); 547 uc->uc_fifo = ALLOC(cache_ptr, size); 548 if (uc->uc_fifo == NULL) { 549 warnx(cache_enable_str, alloc_err, "fifo"); 550 FREE(uc->uc_entries, cache_ptr, size * SPARSENESS); 551 FREE(uc, struct cl_cache, 1); 552 mutex_unlock(&dupreq_lock); 553 return (0); 554 } 555 MEMZERO(uc->uc_fifo, cache_ptr, size); 556 su->su_cache = (char *)(void *)uc; 557 mutex_unlock(&dupreq_lock); 558 return (1); 559 } 560 561 /* 562 * Set an entry in the cache. It assumes that the uc entry is set from 563 * the earlier call to cache_get() for the same procedure. This will always 564 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called 565 * by svc_dg_reply(). All this hoopla because the right RPC parameters are 566 * not available at svc_dg_reply time. 567 */ 568 569 static const char cache_set_str[] = "cache_set: %s"; 570 static const char cache_set_err1[] = "victim not found"; 571 static const char cache_set_err2[] = "victim alloc failed"; 572 static const char cache_set_err3[] = "could not allocate new rpc buffer"; 573 574 static void 575 cache_set(SVCXPRT *xprt, size_t replylen) 576 { 577 cache_ptr victim; 578 cache_ptr *vicp; 579 struct svc_dg_data *su = su_data(xprt); 580 struct cl_cache *uc = (struct cl_cache *) su->su_cache; 581 u_int loc; 582 char *newbuf; 583 #ifdef RPC_CACHE_DEBUG 584 struct netconfig *nconf; 585 char *uaddr; 586 #endif 587 588 mutex_lock(&dupreq_lock); 589 /* 590 * Find space for the new entry, either by 591 * reusing an old entry, or by mallocing a new one 592 */ 593 victim = uc->uc_fifo[uc->uc_nextvictim]; 594 if (victim != NULL) { 595 loc = CACHE_LOC(xprt, victim->cache_xid); 596 for (vicp = &uc->uc_entries[loc]; 597 *vicp != NULL && *vicp != victim; 598 vicp = &(*vicp)->cache_next) 599 ; 600 if (*vicp == NULL) { 601 warnx(cache_set_str, cache_set_err1); 602 mutex_unlock(&dupreq_lock); 603 return; 604 } 605 *vicp = victim->cache_next; /* remove from cache */ 606 newbuf = victim->cache_reply; 607 } else { 608 victim = ALLOC(struct cache_node, 1); 609 if (victim == NULL) { 610 warnx(cache_set_str, cache_set_err2); 611 mutex_unlock(&dupreq_lock); 612 return; 613 } 614 newbuf = mem_alloc(su->su_iosz); 615 if (newbuf == NULL) { 616 warnx(cache_set_str, cache_set_err3); 617 FREE(victim, struct cache_node, 1); 618 mutex_unlock(&dupreq_lock); 619 return; 620 } 621 } 622 623 /* 624 * Store it away 625 */ 626 #ifdef RPC_CACHE_DEBUG 627 if (nconf = getnetconfigent(xprt->xp_netid)) { 628 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr); 629 freenetconfigent(nconf); 630 printf( 631 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n", 632 su->su_xid, uc->uc_prog, uc->uc_vers, 633 uc->uc_proc, uaddr); 634 free(uaddr); 635 } 636 #endif 637 victim->cache_replylen = replylen; 638 victim->cache_reply = rpc_buffer(xprt); 639 rpc_buffer(xprt) = newbuf; 640 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), 641 su->su_iosz, XDR_ENCODE); 642 victim->cache_xid = su->su_xid; 643 victim->cache_proc = uc->uc_proc; 644 victim->cache_vers = uc->uc_vers; 645 victim->cache_prog = uc->uc_prog; 646 victim->cache_addr = xprt->xp_rtaddr; 647 victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len); 648 (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf, 649 (size_t)xprt->xp_rtaddr.len); 650 loc = CACHE_LOC(xprt, victim->cache_xid); 651 victim->cache_next = uc->uc_entries[loc]; 652 uc->uc_entries[loc] = victim; 653 uc->uc_fifo[uc->uc_nextvictim++] = victim; 654 uc->uc_nextvictim %= uc->uc_size; 655 mutex_unlock(&dupreq_lock); 656 } 657 658 /* 659 * Try to get an entry from the cache 660 * return 1 if found, 0 if not found and set the stage for cache_set() 661 */ 662 static int 663 cache_get(SVCXPRT *xprt, struct rpc_msg *msg, char **replyp, size_t *replylenp) 664 { 665 u_int loc; 666 cache_ptr ent; 667 struct svc_dg_data *su = su_data(xprt); 668 struct cl_cache *uc = (struct cl_cache *) su->su_cache; 669 #ifdef RPC_CACHE_DEBUG 670 struct netconfig *nconf; 671 char *uaddr; 672 #endif 673 674 mutex_lock(&dupreq_lock); 675 loc = CACHE_LOC(xprt, su->su_xid); 676 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) { 677 if (ent->cache_xid == su->su_xid && 678 ent->cache_proc == msg->rm_call.cb_proc && 679 ent->cache_vers == msg->rm_call.cb_vers && 680 ent->cache_prog == msg->rm_call.cb_prog && 681 ent->cache_addr.len == xprt->xp_rtaddr.len && 682 (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf, 683 xprt->xp_rtaddr.len) == 0)) { 684 #ifdef RPC_CACHE_DEBUG 685 if (nconf = getnetconfigent(xprt->xp_netid)) { 686 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr); 687 freenetconfigent(nconf); 688 printf( 689 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n", 690 su->su_xid, msg->rm_call.cb_prog, 691 msg->rm_call.cb_vers, 692 msg->rm_call.cb_proc, uaddr); 693 free(uaddr); 694 } 695 #endif 696 *replyp = ent->cache_reply; 697 *replylenp = ent->cache_replylen; 698 mutex_unlock(&dupreq_lock); 699 return (1); 700 } 701 } 702 /* 703 * Failed to find entry 704 * Remember a few things so we can do a set later 705 */ 706 uc->uc_proc = msg->rm_call.cb_proc; 707 uc->uc_vers = msg->rm_call.cb_vers; 708 uc->uc_prog = msg->rm_call.cb_prog; 709 mutex_unlock(&dupreq_lock); 710 return (0); 711 } 712