18360efbdSAlfred Perlstein /* $NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $ */ 28360efbdSAlfred Perlstein /* $FreeBSD$ */ 38360efbdSAlfred Perlstein 48360efbdSAlfred Perlstein /* 58360efbdSAlfred Perlstein * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 68360efbdSAlfred Perlstein * unrestricted use provided that this legend is included on all tape 78360efbdSAlfred Perlstein * media and as a part of the software program in whole or part. Users 88360efbdSAlfred Perlstein * may copy or modify Sun RPC without charge, but are not authorized 98360efbdSAlfred Perlstein * to license or distribute it to anyone else except as part of a product or 108360efbdSAlfred Perlstein * program developed by the user. 118360efbdSAlfred Perlstein * 128360efbdSAlfred Perlstein * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 138360efbdSAlfred Perlstein * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 148360efbdSAlfred Perlstein * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 158360efbdSAlfred Perlstein * 168360efbdSAlfred Perlstein * Sun RPC is provided with no support and without any obligation on the 178360efbdSAlfred Perlstein * part of Sun Microsystems, Inc. to assist in its use, correction, 188360efbdSAlfred Perlstein * modification or enhancement. 198360efbdSAlfred Perlstein * 208360efbdSAlfred Perlstein * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 218360efbdSAlfred Perlstein * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 228360efbdSAlfred Perlstein * OR ANY PART THEREOF. 238360efbdSAlfred Perlstein * 248360efbdSAlfred Perlstein * In no event will Sun Microsystems, Inc. be liable for any lost revenue 258360efbdSAlfred Perlstein * or profits or other special, indirect and consequential damages, even if 268360efbdSAlfred Perlstein * Sun has been advised of the possibility of such damages. 278360efbdSAlfred Perlstein * 288360efbdSAlfred Perlstein * Sun Microsystems, Inc. 298360efbdSAlfred Perlstein * 2550 Garcia Avenue 308360efbdSAlfred Perlstein * Mountain View, California 94043 318360efbdSAlfred Perlstein */ 328360efbdSAlfred Perlstein 338360efbdSAlfred Perlstein /* 348360efbdSAlfred Perlstein * Copyright (c) 1986-1991 by Sun Microsystems Inc. 358360efbdSAlfred Perlstein */ 368360efbdSAlfred Perlstein 378360efbdSAlfred Perlstein /* #ident "@(#)svc_dg.c 1.17 94/04/24 SMI" */ 388360efbdSAlfred Perlstein 398360efbdSAlfred Perlstein 408360efbdSAlfred Perlstein /* 418360efbdSAlfred Perlstein * svc_dg.c, Server side for connectionless RPC. 428360efbdSAlfred Perlstein * 438360efbdSAlfred Perlstein * Does some caching in the hopes of achieving execute-at-most-once semantics. 448360efbdSAlfred Perlstein */ 458360efbdSAlfred Perlstein 468360efbdSAlfred Perlstein #include "namespace.h" 479f5afc13SIan Dowse #include "reentrant.h" 488360efbdSAlfred Perlstein #include <sys/types.h> 498360efbdSAlfred Perlstein #include <sys/socket.h> 508360efbdSAlfred Perlstein #include <rpc/rpc.h> 518360efbdSAlfred Perlstein #include <rpc/svc_dg.h> 528360efbdSAlfred Perlstein #include <errno.h> 538360efbdSAlfred Perlstein #include <unistd.h> 548360efbdSAlfred Perlstein #include <stdio.h> 558360efbdSAlfred Perlstein #include <stdlib.h> 568360efbdSAlfred Perlstein #include <string.h> 578360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG 588360efbdSAlfred Perlstein #include <netconfig.h> 598360efbdSAlfred Perlstein #include <netdir.h> 608360efbdSAlfred Perlstein #endif 618360efbdSAlfred Perlstein #include <err.h> 628360efbdSAlfred Perlstein #include "un-namespace.h" 638360efbdSAlfred Perlstein 648360efbdSAlfred Perlstein #include "rpc_com.h" 658360efbdSAlfred Perlstein 668360efbdSAlfred Perlstein #define su_data(xprt) ((struct svc_dg_data *)(xprt->xp_p2)) 678360efbdSAlfred Perlstein #define rpc_buffer(xprt) ((xprt)->xp_p1) 688360efbdSAlfred Perlstein 698360efbdSAlfred Perlstein #ifndef MAX 708360efbdSAlfred Perlstein #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 718360efbdSAlfred Perlstein #endif 728360efbdSAlfred Perlstein 73c05ac53bSDavid E. O'Brien static void svc_dg_ops(SVCXPRT *); 74c05ac53bSDavid E. O'Brien static enum xprt_stat svc_dg_stat(SVCXPRT *); 75c05ac53bSDavid E. O'Brien static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *); 76c05ac53bSDavid E. O'Brien static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *); 77c05ac53bSDavid E. O'Brien static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, caddr_t); 78c05ac53bSDavid E. O'Brien static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, caddr_t); 79c05ac53bSDavid E. O'Brien static void svc_dg_destroy(SVCXPRT *); 80c05ac53bSDavid E. O'Brien static bool_t svc_dg_control(SVCXPRT *, const u_int, void *); 81c05ac53bSDavid E. O'Brien static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *); 82c05ac53bSDavid E. O'Brien static void cache_set(SVCXPRT *, size_t); 83c05ac53bSDavid E. O'Brien int svc_dg_enablecache(SVCXPRT *, u_int); 848360efbdSAlfred Perlstein 858360efbdSAlfred Perlstein /* 868360efbdSAlfred Perlstein * Usage: 878360efbdSAlfred Perlstein * xprt = svc_dg_create(sock, sendsize, recvsize); 888360efbdSAlfred Perlstein * Does other connectionless specific initializations. 898360efbdSAlfred Perlstein * Once *xprt is initialized, it is registered. 908360efbdSAlfred Perlstein * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable 918360efbdSAlfred Perlstein * system defaults are chosen. 928360efbdSAlfred Perlstein * The routines returns NULL if a problem occurred. 938360efbdSAlfred Perlstein */ 948360efbdSAlfred Perlstein static const char svc_dg_str[] = "svc_dg_create: %s"; 958360efbdSAlfred Perlstein static const char svc_dg_err1[] = "could not get transport information"; 968360efbdSAlfred Perlstein static const char svc_dg_err2[] = " transport does not support data transfer"; 978360efbdSAlfred Perlstein static const char __no_mem_str[] = "out of memory"; 988360efbdSAlfred Perlstein 998360efbdSAlfred Perlstein SVCXPRT * 1008360efbdSAlfred Perlstein svc_dg_create(fd, sendsize, recvsize) 1018360efbdSAlfred Perlstein int fd; 1028360efbdSAlfred Perlstein u_int sendsize; 1038360efbdSAlfred Perlstein u_int recvsize; 1048360efbdSAlfred Perlstein { 1058360efbdSAlfred Perlstein SVCXPRT *xprt; 1068360efbdSAlfred Perlstein struct svc_dg_data *su = NULL; 1078360efbdSAlfred Perlstein struct __rpc_sockinfo si; 1088360efbdSAlfred Perlstein struct sockaddr_storage ss; 1098360efbdSAlfred Perlstein socklen_t slen; 1108360efbdSAlfred Perlstein 1118360efbdSAlfred Perlstein if (!__rpc_fd2sockinfo(fd, &si)) { 1128360efbdSAlfred Perlstein warnx(svc_dg_str, svc_dg_err1); 1138360efbdSAlfred Perlstein return (NULL); 1148360efbdSAlfred Perlstein } 1158360efbdSAlfred Perlstein /* 1168360efbdSAlfred Perlstein * Find the receive and the send size 1178360efbdSAlfred Perlstein */ 1188360efbdSAlfred Perlstein sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize); 1198360efbdSAlfred Perlstein recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize); 1208360efbdSAlfred Perlstein if ((sendsize == 0) || (recvsize == 0)) { 1218360efbdSAlfred Perlstein warnx(svc_dg_str, svc_dg_err2); 1228360efbdSAlfred Perlstein return (NULL); 1238360efbdSAlfred Perlstein } 1248360efbdSAlfred Perlstein 1258360efbdSAlfred Perlstein xprt = mem_alloc(sizeof (SVCXPRT)); 1268360efbdSAlfred Perlstein if (xprt == NULL) 1278360efbdSAlfred Perlstein goto freedata; 1288360efbdSAlfred Perlstein memset(xprt, 0, sizeof (SVCXPRT)); 1298360efbdSAlfred Perlstein 1308360efbdSAlfred Perlstein su = mem_alloc(sizeof (*su)); 1318360efbdSAlfred Perlstein if (su == NULL) 1328360efbdSAlfred Perlstein goto freedata; 1338360efbdSAlfred Perlstein su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4; 1348360efbdSAlfred Perlstein if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) 1358360efbdSAlfred Perlstein goto freedata; 1368360efbdSAlfred Perlstein xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, 1378360efbdSAlfred Perlstein XDR_DECODE); 1388360efbdSAlfred Perlstein su->su_cache = NULL; 1398360efbdSAlfred Perlstein xprt->xp_fd = fd; 1408360efbdSAlfred Perlstein xprt->xp_p2 = (caddr_t)(void *)su; 1418360efbdSAlfred Perlstein xprt->xp_verf.oa_base = su->su_verfbody; 1428360efbdSAlfred Perlstein svc_dg_ops(xprt); 1438360efbdSAlfred Perlstein xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage); 1448360efbdSAlfred Perlstein 1458360efbdSAlfred Perlstein slen = sizeof ss; 1468360efbdSAlfred Perlstein if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) 1478360efbdSAlfred Perlstein goto freedata; 1488360efbdSAlfred Perlstein xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage)); 1498360efbdSAlfred Perlstein xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage); 1508360efbdSAlfred Perlstein xprt->xp_ltaddr.len = slen; 1518360efbdSAlfred Perlstein memcpy(xprt->xp_ltaddr.buf, &ss, slen); 1528360efbdSAlfred Perlstein 1538360efbdSAlfred Perlstein xprt_register(xprt); 1548360efbdSAlfred Perlstein return (xprt); 1558360efbdSAlfred Perlstein freedata: 1568360efbdSAlfred Perlstein (void) warnx(svc_dg_str, __no_mem_str); 1578360efbdSAlfred Perlstein if (xprt) { 1588360efbdSAlfred Perlstein if (su) 1598360efbdSAlfred Perlstein (void) mem_free(su, sizeof (*su)); 1608360efbdSAlfred Perlstein (void) mem_free(xprt, sizeof (SVCXPRT)); 1618360efbdSAlfred Perlstein } 1628360efbdSAlfred Perlstein return (NULL); 1638360efbdSAlfred Perlstein } 1648360efbdSAlfred Perlstein 1658360efbdSAlfred Perlstein /*ARGSUSED*/ 1668360efbdSAlfred Perlstein static enum xprt_stat 1678360efbdSAlfred Perlstein svc_dg_stat(xprt) 1688360efbdSAlfred Perlstein SVCXPRT *xprt; 1698360efbdSAlfred Perlstein { 1708360efbdSAlfred Perlstein return (XPRT_IDLE); 1718360efbdSAlfred Perlstein } 1728360efbdSAlfred Perlstein 1738360efbdSAlfred Perlstein static bool_t 1748360efbdSAlfred Perlstein svc_dg_recv(xprt, msg) 1758360efbdSAlfred Perlstein SVCXPRT *xprt; 1768360efbdSAlfred Perlstein struct rpc_msg *msg; 1778360efbdSAlfred Perlstein { 1788360efbdSAlfred Perlstein struct svc_dg_data *su = su_data(xprt); 1798360efbdSAlfred Perlstein XDR *xdrs = &(su->su_xdrs); 1808360efbdSAlfred Perlstein char *reply; 1818360efbdSAlfred Perlstein struct sockaddr_storage ss; 1828360efbdSAlfred Perlstein socklen_t alen; 1838360efbdSAlfred Perlstein size_t replylen; 1848360efbdSAlfred Perlstein int rlen; 1858360efbdSAlfred Perlstein 1868360efbdSAlfred Perlstein again: 1878360efbdSAlfred Perlstein alen = sizeof (struct sockaddr_storage); 1888360efbdSAlfred Perlstein rlen = _recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz, 0, 1898360efbdSAlfred Perlstein (struct sockaddr *)(void *)&ss, &alen); 1908360efbdSAlfred Perlstein if (rlen == -1 && errno == EINTR) 1918360efbdSAlfred Perlstein goto again; 1928360efbdSAlfred Perlstein if (rlen == -1 || (rlen < 4 * sizeof (u_int32_t))) 1938360efbdSAlfred Perlstein return (FALSE); 1948360efbdSAlfred Perlstein if (xprt->xp_rtaddr.len < alen) { 1958360efbdSAlfred Perlstein if (xprt->xp_rtaddr.len != 0) 1968360efbdSAlfred Perlstein mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len); 1978360efbdSAlfred Perlstein xprt->xp_rtaddr.buf = mem_alloc(alen); 1988360efbdSAlfred Perlstein xprt->xp_rtaddr.len = alen; 1998360efbdSAlfred Perlstein } 2008360efbdSAlfred Perlstein memcpy(xprt->xp_rtaddr.buf, &ss, alen); 2018360efbdSAlfred Perlstein #ifdef PORTMAP 2028360efbdSAlfred Perlstein if (ss.ss_family == AF_INET) { 2038360efbdSAlfred Perlstein xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf; 2048360efbdSAlfred Perlstein xprt->xp_addrlen = sizeof (struct sockaddr_in); 2058360efbdSAlfred Perlstein } 2068360efbdSAlfred Perlstein #endif /* PORTMAP */ 2078360efbdSAlfred Perlstein xdrs->x_op = XDR_DECODE; 2088360efbdSAlfred Perlstein XDR_SETPOS(xdrs, 0); 2098360efbdSAlfred Perlstein if (! xdr_callmsg(xdrs, msg)) { 2108360efbdSAlfred Perlstein return (FALSE); 2118360efbdSAlfred Perlstein } 2128360efbdSAlfred Perlstein su->su_xid = msg->rm_xid; 2138360efbdSAlfred Perlstein if (su->su_cache != NULL) { 2148360efbdSAlfred Perlstein if (cache_get(xprt, msg, &reply, &replylen)) { 2158360efbdSAlfred Perlstein (void)_sendto(xprt->xp_fd, reply, replylen, 0, 2168360efbdSAlfred Perlstein (struct sockaddr *)(void *)&ss, alen); 2178360efbdSAlfred Perlstein return (FALSE); 2188360efbdSAlfred Perlstein } 2198360efbdSAlfred Perlstein } 2208360efbdSAlfred Perlstein return (TRUE); 2218360efbdSAlfred Perlstein } 2228360efbdSAlfred Perlstein 2238360efbdSAlfred Perlstein static bool_t 2248360efbdSAlfred Perlstein svc_dg_reply(xprt, msg) 2258360efbdSAlfred Perlstein SVCXPRT *xprt; 2268360efbdSAlfred Perlstein struct rpc_msg *msg; 2278360efbdSAlfred Perlstein { 2288360efbdSAlfred Perlstein struct svc_dg_data *su = su_data(xprt); 2298360efbdSAlfred Perlstein XDR *xdrs = &(su->su_xdrs); 2308360efbdSAlfred Perlstein bool_t stat = FALSE; 2318360efbdSAlfred Perlstein size_t slen; 2328360efbdSAlfred Perlstein 2338360efbdSAlfred Perlstein xdrs->x_op = XDR_ENCODE; 2348360efbdSAlfred Perlstein XDR_SETPOS(xdrs, 0); 2358360efbdSAlfred Perlstein msg->rm_xid = su->su_xid; 2368360efbdSAlfred Perlstein if (xdr_replymsg(xdrs, msg)) { 2378360efbdSAlfred Perlstein slen = XDR_GETPOS(xdrs); 2388360efbdSAlfred Perlstein if (_sendto(xprt->xp_fd, rpc_buffer(xprt), slen, 0, 2398360efbdSAlfred Perlstein (struct sockaddr *)xprt->xp_rtaddr.buf, 2408360efbdSAlfred Perlstein (socklen_t)xprt->xp_rtaddr.len) == slen) { 2418360efbdSAlfred Perlstein stat = TRUE; 2428360efbdSAlfred Perlstein if (su->su_cache) 2438360efbdSAlfred Perlstein cache_set(xprt, slen); 2448360efbdSAlfred Perlstein } 2458360efbdSAlfred Perlstein } 2468360efbdSAlfred Perlstein return (stat); 2478360efbdSAlfred Perlstein } 2488360efbdSAlfred Perlstein 2498360efbdSAlfred Perlstein static bool_t 2508360efbdSAlfred Perlstein svc_dg_getargs(xprt, xdr_args, args_ptr) 2518360efbdSAlfred Perlstein SVCXPRT *xprt; 2528360efbdSAlfred Perlstein xdrproc_t xdr_args; 2538360efbdSAlfred Perlstein caddr_t args_ptr; 2548360efbdSAlfred Perlstein { 2558360efbdSAlfred Perlstein return (*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr); 2568360efbdSAlfred Perlstein } 2578360efbdSAlfred Perlstein 2588360efbdSAlfred Perlstein static bool_t 2598360efbdSAlfred Perlstein svc_dg_freeargs(xprt, xdr_args, args_ptr) 2608360efbdSAlfred Perlstein SVCXPRT *xprt; 2618360efbdSAlfred Perlstein xdrproc_t xdr_args; 2628360efbdSAlfred Perlstein caddr_t args_ptr; 2638360efbdSAlfred Perlstein { 2648360efbdSAlfred Perlstein XDR *xdrs = &(su_data(xprt)->su_xdrs); 2658360efbdSAlfred Perlstein 2668360efbdSAlfred Perlstein xdrs->x_op = XDR_FREE; 2678360efbdSAlfred Perlstein return (*xdr_args)(xdrs, args_ptr); 2688360efbdSAlfred Perlstein } 2698360efbdSAlfred Perlstein 2708360efbdSAlfred Perlstein static void 2718360efbdSAlfred Perlstein svc_dg_destroy(xprt) 2728360efbdSAlfred Perlstein SVCXPRT *xprt; 2738360efbdSAlfred Perlstein { 2748360efbdSAlfred Perlstein struct svc_dg_data *su = su_data(xprt); 2758360efbdSAlfred Perlstein 2768360efbdSAlfred Perlstein xprt_unregister(xprt); 2778360efbdSAlfred Perlstein if (xprt->xp_fd != -1) 2788360efbdSAlfred Perlstein (void)_close(xprt->xp_fd); 2798360efbdSAlfred Perlstein XDR_DESTROY(&(su->su_xdrs)); 2808360efbdSAlfred Perlstein (void) mem_free(rpc_buffer(xprt), su->su_iosz); 2818360efbdSAlfred Perlstein (void) mem_free(su, sizeof (*su)); 2828360efbdSAlfred Perlstein if (xprt->xp_rtaddr.buf) 2838360efbdSAlfred Perlstein (void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen); 2848360efbdSAlfred Perlstein if (xprt->xp_ltaddr.buf) 2858360efbdSAlfred Perlstein (void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen); 2868360efbdSAlfred Perlstein if (xprt->xp_tp) 2878360efbdSAlfred Perlstein (void) free(xprt->xp_tp); 2888360efbdSAlfred Perlstein (void) mem_free(xprt, sizeof (SVCXPRT)); 2898360efbdSAlfred Perlstein } 2908360efbdSAlfred Perlstein 2918360efbdSAlfred Perlstein static bool_t 2928360efbdSAlfred Perlstein /*ARGSUSED*/ 2938360efbdSAlfred Perlstein svc_dg_control(xprt, rq, in) 2948360efbdSAlfred Perlstein SVCXPRT *xprt; 2958360efbdSAlfred Perlstein const u_int rq; 2968360efbdSAlfred Perlstein void *in; 2978360efbdSAlfred Perlstein { 2988360efbdSAlfred Perlstein return (FALSE); 2998360efbdSAlfred Perlstein } 3008360efbdSAlfred Perlstein 3018360efbdSAlfred Perlstein static void 3028360efbdSAlfred Perlstein svc_dg_ops(xprt) 3038360efbdSAlfred Perlstein SVCXPRT *xprt; 3048360efbdSAlfred Perlstein { 3058360efbdSAlfred Perlstein static struct xp_ops ops; 3068360efbdSAlfred Perlstein static struct xp_ops2 ops2; 3078360efbdSAlfred Perlstein extern mutex_t ops_lock; 3088360efbdSAlfred Perlstein 3098360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY ops_lock: ops */ 3108360efbdSAlfred Perlstein 3118360efbdSAlfred Perlstein mutex_lock(&ops_lock); 3128360efbdSAlfred Perlstein if (ops.xp_recv == NULL) { 3138360efbdSAlfred Perlstein ops.xp_recv = svc_dg_recv; 3148360efbdSAlfred Perlstein ops.xp_stat = svc_dg_stat; 3158360efbdSAlfred Perlstein ops.xp_getargs = svc_dg_getargs; 3168360efbdSAlfred Perlstein ops.xp_reply = svc_dg_reply; 3178360efbdSAlfred Perlstein ops.xp_freeargs = svc_dg_freeargs; 3188360efbdSAlfred Perlstein ops.xp_destroy = svc_dg_destroy; 3198360efbdSAlfred Perlstein ops2.xp_control = svc_dg_control; 3208360efbdSAlfred Perlstein } 3218360efbdSAlfred Perlstein xprt->xp_ops = &ops; 3228360efbdSAlfred Perlstein xprt->xp_ops2 = &ops2; 3238360efbdSAlfred Perlstein mutex_unlock(&ops_lock); 3248360efbdSAlfred Perlstein } 3258360efbdSAlfred Perlstein 3268360efbdSAlfred Perlstein /* The CACHING COMPONENT */ 3278360efbdSAlfred Perlstein 3288360efbdSAlfred Perlstein /* 3298360efbdSAlfred Perlstein * Could have been a separate file, but some part of it depends upon the 3308360efbdSAlfred Perlstein * private structure of the client handle. 3318360efbdSAlfred Perlstein * 3328360efbdSAlfred Perlstein * Fifo cache for cl server 3338360efbdSAlfred Perlstein * Copies pointers to reply buffers into fifo cache 3348360efbdSAlfred Perlstein * Buffers are sent again if retransmissions are detected. 3358360efbdSAlfred Perlstein */ 3368360efbdSAlfred Perlstein 3378360efbdSAlfred Perlstein #define SPARSENESS 4 /* 75% sparse */ 3388360efbdSAlfred Perlstein 3398360efbdSAlfred Perlstein #define ALLOC(type, size) \ 3408360efbdSAlfred Perlstein (type *) mem_alloc((sizeof (type) * (size))) 3418360efbdSAlfred Perlstein 3428360efbdSAlfred Perlstein #define MEMZERO(addr, type, size) \ 3438360efbdSAlfred Perlstein (void) memset((void *) (addr), 0, sizeof (type) * (int) (size)) 3448360efbdSAlfred Perlstein 3458360efbdSAlfred Perlstein #define FREE(addr, type, size) \ 3468360efbdSAlfred Perlstein mem_free((addr), (sizeof (type) * (size))) 3478360efbdSAlfred Perlstein 3488360efbdSAlfred Perlstein /* 3498360efbdSAlfred Perlstein * An entry in the cache 3508360efbdSAlfred Perlstein */ 3518360efbdSAlfred Perlstein typedef struct cache_node *cache_ptr; 3528360efbdSAlfred Perlstein struct cache_node { 3538360efbdSAlfred Perlstein /* 3548360efbdSAlfred Perlstein * Index into cache is xid, proc, vers, prog and address 3558360efbdSAlfred Perlstein */ 3568360efbdSAlfred Perlstein u_int32_t cache_xid; 3578360efbdSAlfred Perlstein rpcproc_t cache_proc; 3588360efbdSAlfred Perlstein rpcvers_t cache_vers; 3598360efbdSAlfred Perlstein rpcprog_t cache_prog; 3608360efbdSAlfred Perlstein struct netbuf cache_addr; 3618360efbdSAlfred Perlstein /* 3628360efbdSAlfred Perlstein * The cached reply and length 3638360efbdSAlfred Perlstein */ 3648360efbdSAlfred Perlstein char *cache_reply; 3658360efbdSAlfred Perlstein size_t cache_replylen; 3668360efbdSAlfred Perlstein /* 3678360efbdSAlfred Perlstein * Next node on the list, if there is a collision 3688360efbdSAlfred Perlstein */ 3698360efbdSAlfred Perlstein cache_ptr cache_next; 3708360efbdSAlfred Perlstein }; 3718360efbdSAlfred Perlstein 3728360efbdSAlfred Perlstein /* 3738360efbdSAlfred Perlstein * The entire cache 3748360efbdSAlfred Perlstein */ 3758360efbdSAlfred Perlstein struct cl_cache { 3768360efbdSAlfred Perlstein u_int uc_size; /* size of cache */ 3778360efbdSAlfred Perlstein cache_ptr *uc_entries; /* hash table of entries in cache */ 3788360efbdSAlfred Perlstein cache_ptr *uc_fifo; /* fifo list of entries in cache */ 3798360efbdSAlfred Perlstein u_int uc_nextvictim; /* points to next victim in fifo list */ 3808360efbdSAlfred Perlstein rpcprog_t uc_prog; /* saved program number */ 3818360efbdSAlfred Perlstein rpcvers_t uc_vers; /* saved version number */ 3828360efbdSAlfred Perlstein rpcproc_t uc_proc; /* saved procedure number */ 3838360efbdSAlfred Perlstein }; 3848360efbdSAlfred Perlstein 3858360efbdSAlfred Perlstein 3868360efbdSAlfred Perlstein /* 3878360efbdSAlfred Perlstein * the hashing function 3888360efbdSAlfred Perlstein */ 3898360efbdSAlfred Perlstein #define CACHE_LOC(transp, xid) \ 3908360efbdSAlfred Perlstein (xid % (SPARSENESS * ((struct cl_cache *) \ 3918360efbdSAlfred Perlstein su_data(transp)->su_cache)->uc_size)) 3928360efbdSAlfred Perlstein 3938360efbdSAlfred Perlstein extern mutex_t dupreq_lock; 3948360efbdSAlfred Perlstein 3958360efbdSAlfred Perlstein /* 3968360efbdSAlfred Perlstein * Enable use of the cache. Returns 1 on success, 0 on failure. 3978360efbdSAlfred Perlstein * Note: there is no disable. 3988360efbdSAlfred Perlstein */ 3998360efbdSAlfred Perlstein static const char cache_enable_str[] = "svc_enablecache: %s %s"; 4008360efbdSAlfred Perlstein static const char alloc_err[] = "could not allocate cache "; 4018360efbdSAlfred Perlstein static const char enable_err[] = "cache already enabled"; 4028360efbdSAlfred Perlstein 4038360efbdSAlfred Perlstein int 4048360efbdSAlfred Perlstein svc_dg_enablecache(transp, size) 4058360efbdSAlfred Perlstein SVCXPRT *transp; 4068360efbdSAlfred Perlstein u_int size; 4078360efbdSAlfred Perlstein { 4088360efbdSAlfred Perlstein struct svc_dg_data *su = su_data(transp); 4098360efbdSAlfred Perlstein struct cl_cache *uc; 4108360efbdSAlfred Perlstein 4118360efbdSAlfred Perlstein mutex_lock(&dupreq_lock); 4128360efbdSAlfred Perlstein if (su->su_cache != NULL) { 4138360efbdSAlfred Perlstein (void) warnx(cache_enable_str, enable_err, " "); 4148360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 4158360efbdSAlfred Perlstein return (0); 4168360efbdSAlfred Perlstein } 4178360efbdSAlfred Perlstein uc = ALLOC(struct cl_cache, 1); 4188360efbdSAlfred Perlstein if (uc == NULL) { 4198360efbdSAlfred Perlstein warnx(cache_enable_str, alloc_err, " "); 4208360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 4218360efbdSAlfred Perlstein return (0); 4228360efbdSAlfred Perlstein } 4238360efbdSAlfred Perlstein uc->uc_size = size; 4248360efbdSAlfred Perlstein uc->uc_nextvictim = 0; 4258360efbdSAlfred Perlstein uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS); 4268360efbdSAlfred Perlstein if (uc->uc_entries == NULL) { 4278360efbdSAlfred Perlstein warnx(cache_enable_str, alloc_err, "data"); 4288360efbdSAlfred Perlstein FREE(uc, struct cl_cache, 1); 4298360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 4308360efbdSAlfred Perlstein return (0); 4318360efbdSAlfred Perlstein } 4328360efbdSAlfred Perlstein MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS); 4338360efbdSAlfred Perlstein uc->uc_fifo = ALLOC(cache_ptr, size); 4348360efbdSAlfred Perlstein if (uc->uc_fifo == NULL) { 4358360efbdSAlfred Perlstein warnx(cache_enable_str, alloc_err, "fifo"); 4368360efbdSAlfred Perlstein FREE(uc->uc_entries, cache_ptr, size * SPARSENESS); 4378360efbdSAlfred Perlstein FREE(uc, struct cl_cache, 1); 4388360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 4398360efbdSAlfred Perlstein return (0); 4408360efbdSAlfred Perlstein } 4418360efbdSAlfred Perlstein MEMZERO(uc->uc_fifo, cache_ptr, size); 4428360efbdSAlfred Perlstein su->su_cache = (char *)(void *)uc; 4438360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 4448360efbdSAlfred Perlstein return (1); 4458360efbdSAlfred Perlstein } 4468360efbdSAlfred Perlstein 4478360efbdSAlfred Perlstein /* 4488360efbdSAlfred Perlstein * Set an entry in the cache. It assumes that the uc entry is set from 4498360efbdSAlfred Perlstein * the earlier call to cache_get() for the same procedure. This will always 4508360efbdSAlfred Perlstein * happen because cache_get() is calle by svc_dg_recv and cache_set() is called 4518360efbdSAlfred Perlstein * by svc_dg_reply(). All this hoopla because the right RPC parameters are 4528360efbdSAlfred Perlstein * not available at svc_dg_reply time. 4538360efbdSAlfred Perlstein */ 4548360efbdSAlfred Perlstein 4558360efbdSAlfred Perlstein static const char cache_set_str[] = "cache_set: %s"; 4568360efbdSAlfred Perlstein static const char cache_set_err1[] = "victim not found"; 4578360efbdSAlfred Perlstein static const char cache_set_err2[] = "victim alloc failed"; 4588360efbdSAlfred Perlstein static const char cache_set_err3[] = "could not allocate new rpc buffer"; 4598360efbdSAlfred Perlstein 4608360efbdSAlfred Perlstein static void 4618360efbdSAlfred Perlstein cache_set(xprt, replylen) 4628360efbdSAlfred Perlstein SVCXPRT *xprt; 4638360efbdSAlfred Perlstein size_t replylen; 4648360efbdSAlfred Perlstein { 4658360efbdSAlfred Perlstein cache_ptr victim; 4668360efbdSAlfred Perlstein cache_ptr *vicp; 4678360efbdSAlfred Perlstein struct svc_dg_data *su = su_data(xprt); 4688360efbdSAlfred Perlstein struct cl_cache *uc = (struct cl_cache *) su->su_cache; 4698360efbdSAlfred Perlstein u_int loc; 4708360efbdSAlfred Perlstein char *newbuf; 4718360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG 4728360efbdSAlfred Perlstein struct netconfig *nconf; 4738360efbdSAlfred Perlstein char *uaddr; 4748360efbdSAlfred Perlstein #endif 4758360efbdSAlfred Perlstein 4768360efbdSAlfred Perlstein mutex_lock(&dupreq_lock); 4778360efbdSAlfred Perlstein /* 4788360efbdSAlfred Perlstein * Find space for the new entry, either by 4798360efbdSAlfred Perlstein * reusing an old entry, or by mallocing a new one 4808360efbdSAlfred Perlstein */ 4818360efbdSAlfred Perlstein victim = uc->uc_fifo[uc->uc_nextvictim]; 4828360efbdSAlfred Perlstein if (victim != NULL) { 4838360efbdSAlfred Perlstein loc = CACHE_LOC(xprt, victim->cache_xid); 4848360efbdSAlfred Perlstein for (vicp = &uc->uc_entries[loc]; 4858360efbdSAlfred Perlstein *vicp != NULL && *vicp != victim; 4868360efbdSAlfred Perlstein vicp = &(*vicp)->cache_next) 4878360efbdSAlfred Perlstein ; 4888360efbdSAlfred Perlstein if (*vicp == NULL) { 4898360efbdSAlfred Perlstein warnx(cache_set_str, cache_set_err1); 4908360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 4918360efbdSAlfred Perlstein return; 4928360efbdSAlfred Perlstein } 4938360efbdSAlfred Perlstein *vicp = victim->cache_next; /* remove from cache */ 4948360efbdSAlfred Perlstein newbuf = victim->cache_reply; 4958360efbdSAlfred Perlstein } else { 4968360efbdSAlfred Perlstein victim = ALLOC(struct cache_node, 1); 4978360efbdSAlfred Perlstein if (victim == NULL) { 4988360efbdSAlfred Perlstein warnx(cache_set_str, cache_set_err2); 4998360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 5008360efbdSAlfred Perlstein return; 5018360efbdSAlfred Perlstein } 5028360efbdSAlfred Perlstein newbuf = mem_alloc(su->su_iosz); 5038360efbdSAlfred Perlstein if (newbuf == NULL) { 5048360efbdSAlfred Perlstein warnx(cache_set_str, cache_set_err3); 5058360efbdSAlfred Perlstein FREE(victim, struct cache_node, 1); 5068360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 5078360efbdSAlfred Perlstein return; 5088360efbdSAlfred Perlstein } 5098360efbdSAlfred Perlstein } 5108360efbdSAlfred Perlstein 5118360efbdSAlfred Perlstein /* 5128360efbdSAlfred Perlstein * Store it away 5138360efbdSAlfred Perlstein */ 5148360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG 5158360efbdSAlfred Perlstein if (nconf = getnetconfigent(xprt->xp_netid)) { 5168360efbdSAlfred Perlstein uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr); 5178360efbdSAlfred Perlstein freenetconfigent(nconf); 5188360efbdSAlfred Perlstein printf( 5198360efbdSAlfred Perlstein "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n", 5208360efbdSAlfred Perlstein su->su_xid, uc->uc_prog, uc->uc_vers, 5218360efbdSAlfred Perlstein uc->uc_proc, uaddr); 5228360efbdSAlfred Perlstein free(uaddr); 5238360efbdSAlfred Perlstein } 5248360efbdSAlfred Perlstein #endif 5258360efbdSAlfred Perlstein victim->cache_replylen = replylen; 5268360efbdSAlfred Perlstein victim->cache_reply = rpc_buffer(xprt); 5278360efbdSAlfred Perlstein rpc_buffer(xprt) = newbuf; 5288360efbdSAlfred Perlstein xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), 5298360efbdSAlfred Perlstein su->su_iosz, XDR_ENCODE); 5308360efbdSAlfred Perlstein victim->cache_xid = su->su_xid; 5318360efbdSAlfred Perlstein victim->cache_proc = uc->uc_proc; 5328360efbdSAlfred Perlstein victim->cache_vers = uc->uc_vers; 5338360efbdSAlfred Perlstein victim->cache_prog = uc->uc_prog; 5348360efbdSAlfred Perlstein victim->cache_addr = xprt->xp_rtaddr; 5358360efbdSAlfred Perlstein victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len); 5368360efbdSAlfred Perlstein (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf, 5378360efbdSAlfred Perlstein (size_t)xprt->xp_rtaddr.len); 5388360efbdSAlfred Perlstein loc = CACHE_LOC(xprt, victim->cache_xid); 5398360efbdSAlfred Perlstein victim->cache_next = uc->uc_entries[loc]; 5408360efbdSAlfred Perlstein uc->uc_entries[loc] = victim; 5418360efbdSAlfred Perlstein uc->uc_fifo[uc->uc_nextvictim++] = victim; 5428360efbdSAlfred Perlstein uc->uc_nextvictim %= uc->uc_size; 5438360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 5448360efbdSAlfred Perlstein } 5458360efbdSAlfred Perlstein 5468360efbdSAlfred Perlstein /* 5478360efbdSAlfred Perlstein * Try to get an entry from the cache 5488360efbdSAlfred Perlstein * return 1 if found, 0 if not found and set the stage for cache_set() 5498360efbdSAlfred Perlstein */ 5508360efbdSAlfred Perlstein static int 5518360efbdSAlfred Perlstein cache_get(xprt, msg, replyp, replylenp) 5528360efbdSAlfred Perlstein SVCXPRT *xprt; 5538360efbdSAlfred Perlstein struct rpc_msg *msg; 5548360efbdSAlfred Perlstein char **replyp; 5558360efbdSAlfred Perlstein size_t *replylenp; 5568360efbdSAlfred Perlstein { 5578360efbdSAlfred Perlstein u_int loc; 5588360efbdSAlfred Perlstein cache_ptr ent; 5598360efbdSAlfred Perlstein struct svc_dg_data *su = su_data(xprt); 5608360efbdSAlfred Perlstein struct cl_cache *uc = (struct cl_cache *) su->su_cache; 5618360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG 5628360efbdSAlfred Perlstein struct netconfig *nconf; 5638360efbdSAlfred Perlstein char *uaddr; 5648360efbdSAlfred Perlstein #endif 5658360efbdSAlfred Perlstein 5668360efbdSAlfred Perlstein mutex_lock(&dupreq_lock); 5678360efbdSAlfred Perlstein loc = CACHE_LOC(xprt, su->su_xid); 5688360efbdSAlfred Perlstein for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) { 5698360efbdSAlfred Perlstein if (ent->cache_xid == su->su_xid && 5708360efbdSAlfred Perlstein ent->cache_proc == msg->rm_call.cb_proc && 5718360efbdSAlfred Perlstein ent->cache_vers == msg->rm_call.cb_vers && 5728360efbdSAlfred Perlstein ent->cache_prog == msg->rm_call.cb_prog && 5738360efbdSAlfred Perlstein ent->cache_addr.len == xprt->xp_rtaddr.len && 5748360efbdSAlfred Perlstein (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf, 5758360efbdSAlfred Perlstein xprt->xp_rtaddr.len) == 0)) { 5768360efbdSAlfred Perlstein #ifdef RPC_CACHE_DEBUG 5778360efbdSAlfred Perlstein if (nconf = getnetconfigent(xprt->xp_netid)) { 5788360efbdSAlfred Perlstein uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr); 5798360efbdSAlfred Perlstein freenetconfigent(nconf); 5808360efbdSAlfred Perlstein printf( 5818360efbdSAlfred Perlstein "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n", 5828360efbdSAlfred Perlstein su->su_xid, msg->rm_call.cb_prog, 5838360efbdSAlfred Perlstein msg->rm_call.cb_vers, 5848360efbdSAlfred Perlstein msg->rm_call.cb_proc, uaddr); 5858360efbdSAlfred Perlstein free(uaddr); 5868360efbdSAlfred Perlstein } 5878360efbdSAlfred Perlstein #endif 5888360efbdSAlfred Perlstein *replyp = ent->cache_reply; 5898360efbdSAlfred Perlstein *replylenp = ent->cache_replylen; 5908360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 5918360efbdSAlfred Perlstein return (1); 5928360efbdSAlfred Perlstein } 5938360efbdSAlfred Perlstein } 5948360efbdSAlfred Perlstein /* 5958360efbdSAlfred Perlstein * Failed to find entry 5968360efbdSAlfred Perlstein * Remember a few things so we can do a set later 5978360efbdSAlfred Perlstein */ 5988360efbdSAlfred Perlstein uc->uc_proc = msg->rm_call.cb_proc; 5998360efbdSAlfred Perlstein uc->uc_vers = msg->rm_call.cb_vers; 6008360efbdSAlfred Perlstein uc->uc_prog = msg->rm_call.cb_prog; 6018360efbdSAlfred Perlstein mutex_unlock(&dupreq_lock); 6028360efbdSAlfred Perlstein return (0); 6038360efbdSAlfred Perlstein } 604