1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*0Sstevel@tonic-gate /* All Rights Reserved */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate * The Regents of the University of California
33*0Sstevel@tonic-gate * All Rights Reserved
34*0Sstevel@tonic-gate *
35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate * contributors.
38*0Sstevel@tonic-gate */
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate * svc_udp.c,
44*0Sstevel@tonic-gate * Server side for UDP/IP based RPC. (Does some caching in the hopes of
45*0Sstevel@tonic-gate * achieving execute-at-most-once semantics.)
46*0Sstevel@tonic-gate */
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate #include <rpc/rpc.h>
49*0Sstevel@tonic-gate #include <rpc/clnt_soc.h>
50*0Sstevel@tonic-gate #include <sys/socket.h>
51*0Sstevel@tonic-gate #include <errno.h>
52*0Sstevel@tonic-gate #include <syslog.h>
53*0Sstevel@tonic-gate #include <malloc.h>
54*0Sstevel@tonic-gate #include <stdio.h>
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate #define rpc_buffer(xprt) ((xprt)->xp_p1)
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate static struct xp_ops *svcudp_ops();
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate extern int errno;
62*0Sstevel@tonic-gate extern SVCXPRT *svc_xprt_alloc();
63*0Sstevel@tonic-gate extern void svc_xprt_free();
64*0Sstevel@tonic-gate extern int _socket(int, int, int);
65*0Sstevel@tonic-gate extern int _bind(int, const struct sockaddr *, int);
66*0Sstevel@tonic-gate extern int _getsockname(int, struct sockaddr *, int *);
67*0Sstevel@tonic-gate extern int _listen(int, int);
68*0Sstevel@tonic-gate extern int _accept(int, struct sockaddr *, int *);
69*0Sstevel@tonic-gate extern int bindresvport(int, struct sockaddr_in *);
70*0Sstevel@tonic-gate extern int _recvfrom(int, char *, int, int,
71*0Sstevel@tonic-gate struct sockaddr *, int *);
72*0Sstevel@tonic-gate extern int _sendto(int, const char *, int, int,
73*0Sstevel@tonic-gate const struct sockaddr *, int);
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate static int cache_get(SVCXPRT *, struct rpc_msg *,
76*0Sstevel@tonic-gate char **, uint_t *);
77*0Sstevel@tonic-gate static void cache_set(SVCXPRT *, uint_t);
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate * kept in xprt->xp_p2
81*0Sstevel@tonic-gate */
82*0Sstevel@tonic-gate struct svcudp_data {
83*0Sstevel@tonic-gate u_int su_iosz; /* byte size of send.recv buffer */
84*0Sstevel@tonic-gate uint32_t su_xid; /* transaction id */
85*0Sstevel@tonic-gate XDR su_xdrs; /* XDR handle */
86*0Sstevel@tonic-gate char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */
87*0Sstevel@tonic-gate char * su_cache; /* cached data, NULL if no cache */
88*0Sstevel@tonic-gate };
89*0Sstevel@tonic-gate #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate * Usage:
93*0Sstevel@tonic-gate * xprt = svcudp_create(sock);
94*0Sstevel@tonic-gate *
95*0Sstevel@tonic-gate * If sock<0 then a socket is created, else sock is used.
96*0Sstevel@tonic-gate * If the socket, sock is not bound to a port then svcudp_create
97*0Sstevel@tonic-gate * binds it to an arbitrary port. In any (successful) case,
98*0Sstevel@tonic-gate * xprt->xp_sock is the registered socket number and xprt->xp_port is the
99*0Sstevel@tonic-gate * associated port number.
100*0Sstevel@tonic-gate * Once *xprt is initialized, it is registered as a transporter;
101*0Sstevel@tonic-gate * see (svc.h, xprt_register).
102*0Sstevel@tonic-gate * The routines returns NULL if a problem occurred.
103*0Sstevel@tonic-gate */
104*0Sstevel@tonic-gate SVCXPRT *
svcudp_bufcreate(sock,sendsz,recvsz)105*0Sstevel@tonic-gate svcudp_bufcreate(sock, sendsz, recvsz)
106*0Sstevel@tonic-gate register int sock;
107*0Sstevel@tonic-gate u_int sendsz, recvsz;
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate bool_t madesock = FALSE;
110*0Sstevel@tonic-gate register SVCXPRT *xprt;
111*0Sstevel@tonic-gate register struct svcudp_data *su;
112*0Sstevel@tonic-gate struct sockaddr_in addr;
113*0Sstevel@tonic-gate int len = sizeof (struct sockaddr_in);
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate if (sock == RPC_ANYSOCK) {
116*0Sstevel@tonic-gate if ((sock = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
117*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "svcudp_create: socket",
118*0Sstevel@tonic-gate " creation problem: %m");
119*0Sstevel@tonic-gate return ((SVCXPRT *)NULL);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate madesock = TRUE;
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate memset((char *)&addr, 0, sizeof (addr));
124*0Sstevel@tonic-gate addr.sin_family = AF_INET;
125*0Sstevel@tonic-gate if (bindresvport(sock, &addr)) {
126*0Sstevel@tonic-gate addr.sin_port = 0;
127*0Sstevel@tonic-gate (void) _bind(sock, (struct sockaddr *)&addr, len);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate if (_getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
130*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "svcudp_create -",
131*0Sstevel@tonic-gate " cannot getsockname: %m");
132*0Sstevel@tonic-gate if (madesock)
133*0Sstevel@tonic-gate (void) close(sock);
134*0Sstevel@tonic-gate return ((SVCXPRT *)NULL);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate xprt = svc_xprt_alloc();
137*0Sstevel@tonic-gate if (xprt == NULL) {
138*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "svcudp_create: out of memory");
139*0Sstevel@tonic-gate if (madesock)
140*0Sstevel@tonic-gate (void) close(sock);
141*0Sstevel@tonic-gate return ((SVCXPRT *)NULL);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate su = (struct svcudp_data *)mem_alloc(sizeof (*su));
144*0Sstevel@tonic-gate if (su == NULL) {
145*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "svcudp_create: out of memory");
146*0Sstevel@tonic-gate svc_xprt_free(xprt);
147*0Sstevel@tonic-gate if (madesock)
148*0Sstevel@tonic-gate (void) close(sock);
149*0Sstevel@tonic-gate return ((SVCXPRT *)NULL);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
152*0Sstevel@tonic-gate if ((rpc_buffer(xprt) = (char *)mem_alloc(su->su_iosz)) == NULL) {
153*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "svcudp_create: out of memory");
154*0Sstevel@tonic-gate mem_free((char *) su, sizeof (*su));
155*0Sstevel@tonic-gate svc_xprt_free(xprt);
156*0Sstevel@tonic-gate if (madesock)
157*0Sstevel@tonic-gate (void) close(sock);
158*0Sstevel@tonic-gate return ((SVCXPRT *)NULL);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate xdrmem_create(
161*0Sstevel@tonic-gate &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
162*0Sstevel@tonic-gate su->su_cache = NULL;
163*0Sstevel@tonic-gate xprt->xp_p2 = (caddr_t)su;
164*0Sstevel@tonic-gate xprt->xp_netid = NULL;
165*0Sstevel@tonic-gate xprt->xp_verf.oa_base = su->su_verfbody;
166*0Sstevel@tonic-gate xprt->xp_ops = svcudp_ops();
167*0Sstevel@tonic-gate xprt->xp_port = ntohs(addr.sin_port);
168*0Sstevel@tonic-gate xprt->xp_sock = sock;
169*0Sstevel@tonic-gate xprt->xp_rtaddr.buf = &xprt->xp_raddr[0];
170*0Sstevel@tonic-gate xprt_register(xprt);
171*0Sstevel@tonic-gate return (xprt);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate SVCXPRT *
svcudp_create(sock)175*0Sstevel@tonic-gate svcudp_create(sock)
176*0Sstevel@tonic-gate int sock;
177*0Sstevel@tonic-gate {
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate return (svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate static enum xprt_stat
svcudp_stat(xprt)183*0Sstevel@tonic-gate svcudp_stat(xprt)
184*0Sstevel@tonic-gate SVCXPRT *xprt;
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate return (XPRT_IDLE);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate static bool_t
svcudp_recv(xprt,msg)191*0Sstevel@tonic-gate svcudp_recv(xprt, msg)
192*0Sstevel@tonic-gate register SVCXPRT *xprt;
193*0Sstevel@tonic-gate struct rpc_msg *msg;
194*0Sstevel@tonic-gate {
195*0Sstevel@tonic-gate register struct svcudp_data *su = su_data(xprt);
196*0Sstevel@tonic-gate register XDR *xdrs = &(su->su_xdrs);
197*0Sstevel@tonic-gate register int rlen;
198*0Sstevel@tonic-gate char *reply;
199*0Sstevel@tonic-gate uint_t replylen;
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate again:
202*0Sstevel@tonic-gate xprt->xp_addrlen = sizeof (struct sockaddr_in);
203*0Sstevel@tonic-gate rlen = _recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
204*0Sstevel@tonic-gate 0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
205*0Sstevel@tonic-gate if (rlen == -1 && errno == EINTR)
206*0Sstevel@tonic-gate goto again;
207*0Sstevel@tonic-gate if (rlen < 4*sizeof (uint32_t))
208*0Sstevel@tonic-gate return (FALSE);
209*0Sstevel@tonic-gate xdrs->x_op = XDR_DECODE;
210*0Sstevel@tonic-gate XDR_SETPOS(xdrs, 0);
211*0Sstevel@tonic-gate if (! xdr_callmsg(xdrs, msg))
212*0Sstevel@tonic-gate return (FALSE);
213*0Sstevel@tonic-gate su->su_xid = msg->rm_xid;
214*0Sstevel@tonic-gate if (su->su_cache != NULL) {
215*0Sstevel@tonic-gate if (cache_get(xprt, msg, &reply, &replylen)) {
216*0Sstevel@tonic-gate (void) _sendto(xprt->xp_sock, reply, (int) replylen, 0,
217*0Sstevel@tonic-gate (struct sockaddr *) &xprt->xp_raddr,
218*0Sstevel@tonic-gate xprt->xp_addrlen);
219*0Sstevel@tonic-gate return (TRUE);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate return (TRUE);
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate static bool_t
svcudp_reply(xprt,msg)226*0Sstevel@tonic-gate svcudp_reply(xprt, msg)
227*0Sstevel@tonic-gate register SVCXPRT *xprt;
228*0Sstevel@tonic-gate struct rpc_msg *msg;
229*0Sstevel@tonic-gate {
230*0Sstevel@tonic-gate register struct svcudp_data *su = su_data(xprt);
231*0Sstevel@tonic-gate register XDR *xdrs = &(su->su_xdrs);
232*0Sstevel@tonic-gate register int slen;
233*0Sstevel@tonic-gate register bool_t stat = FALSE;
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gate xdrs->x_op = XDR_ENCODE;
236*0Sstevel@tonic-gate XDR_SETPOS(xdrs, 0);
237*0Sstevel@tonic-gate msg->rm_xid = su->su_xid;
238*0Sstevel@tonic-gate if (xdr_replymsg(xdrs, msg)) {
239*0Sstevel@tonic-gate slen = (int)XDR_GETPOS(xdrs);
240*0Sstevel@tonic-gate if (_sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
241*0Sstevel@tonic-gate (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
242*0Sstevel@tonic-gate == slen) {
243*0Sstevel@tonic-gate stat = TRUE;
244*0Sstevel@tonic-gate if (su->su_cache && slen >= 0) {
245*0Sstevel@tonic-gate (void) cache_set(xprt, (uint_t) slen);
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate }
249*0Sstevel@tonic-gate return (stat);
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate static bool_t
svcudp_getargs(xprt,xdr_args,args_ptr)253*0Sstevel@tonic-gate svcudp_getargs(xprt, xdr_args, args_ptr)
254*0Sstevel@tonic-gate SVCXPRT *xprt;
255*0Sstevel@tonic-gate xdrproc_t xdr_args;
256*0Sstevel@tonic-gate caddr_t args_ptr;
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate static bool_t
svcudp_freeargs(xprt,xdr_args,args_ptr)263*0Sstevel@tonic-gate svcudp_freeargs(xprt, xdr_args, args_ptr)
264*0Sstevel@tonic-gate SVCXPRT *xprt;
265*0Sstevel@tonic-gate xdrproc_t xdr_args;
266*0Sstevel@tonic-gate caddr_t args_ptr;
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate register XDR *xdrs = &(su_data(xprt)->su_xdrs);
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate xdrs->x_op = XDR_FREE;
271*0Sstevel@tonic-gate return ((*xdr_args)(xdrs, args_ptr));
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate static void
svcudp_destroy(xprt)275*0Sstevel@tonic-gate svcudp_destroy(xprt)
276*0Sstevel@tonic-gate register SVCXPRT *xprt;
277*0Sstevel@tonic-gate {
278*0Sstevel@tonic-gate register struct svcudp_data *su = su_data(xprt);
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gate xprt_unregister(xprt);
281*0Sstevel@tonic-gate (void) close(xprt->xp_sock);
282*0Sstevel@tonic-gate XDR_DESTROY(&(su->su_xdrs));
283*0Sstevel@tonic-gate mem_free(rpc_buffer(xprt), su->su_iosz);
284*0Sstevel@tonic-gate mem_free((caddr_t)su, sizeof (struct svcudp_data));
285*0Sstevel@tonic-gate svc_xprt_free(xprt);
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gate /* **********this could be a separate file********************* */
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate /*
292*0Sstevel@tonic-gate * Fifo cache for udp server
293*0Sstevel@tonic-gate * Copies pointers to reply buffers into fifo cache
294*0Sstevel@tonic-gate * Buffers are sent again if retransmissions are detected.
295*0Sstevel@tonic-gate */
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate #define SPARSENESS 4 /* 75% sparse */
298*0Sstevel@tonic-gate
299*0Sstevel@tonic-gate #define ALLOC(type, size) \
300*0Sstevel@tonic-gate (type *) mem_alloc((unsigned) (sizeof (type) * (size)))
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate #define BZERO(addr, type, size) \
303*0Sstevel@tonic-gate memset((char *) (addr), 0, sizeof (type) * (int) (size))
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gate #define FREE(addr, type, size) \
306*0Sstevel@tonic-gate (void) mem_free((char *) (addr), (sizeof (type) * (size)))
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate /*
309*0Sstevel@tonic-gate * An entry in the cache
310*0Sstevel@tonic-gate */
311*0Sstevel@tonic-gate typedef struct cache_node *cache_ptr;
312*0Sstevel@tonic-gate struct cache_node {
313*0Sstevel@tonic-gate /*
314*0Sstevel@tonic-gate * Index into cache is xid, proc, vers, prog and address
315*0Sstevel@tonic-gate */
316*0Sstevel@tonic-gate uint32_t cache_xid;
317*0Sstevel@tonic-gate uint32_t cache_proc;
318*0Sstevel@tonic-gate uint32_t cache_vers;
319*0Sstevel@tonic-gate uint32_t cache_prog;
320*0Sstevel@tonic-gate struct sockaddr_in cache_addr;
321*0Sstevel@tonic-gate /*
322*0Sstevel@tonic-gate * The cached reply and length
323*0Sstevel@tonic-gate */
324*0Sstevel@tonic-gate char * cache_reply;
325*0Sstevel@tonic-gate uint32_t cache_replylen;
326*0Sstevel@tonic-gate /*
327*0Sstevel@tonic-gate * Next node on the list, if there is a collision
328*0Sstevel@tonic-gate */
329*0Sstevel@tonic-gate cache_ptr cache_next;
330*0Sstevel@tonic-gate };
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gate /*
335*0Sstevel@tonic-gate * The entire cache
336*0Sstevel@tonic-gate */
337*0Sstevel@tonic-gate struct udp_cache {
338*0Sstevel@tonic-gate uint32_t uc_size; /* size of cache */
339*0Sstevel@tonic-gate cache_ptr *uc_entries; /* hash table of entries in cache */
340*0Sstevel@tonic-gate cache_ptr *uc_fifo; /* fifo list of entries in cache */
341*0Sstevel@tonic-gate uint32_t uc_nextvictim; /* points to next victim in fifo list */
342*0Sstevel@tonic-gate uint32_t uc_prog; /* saved program number */
343*0Sstevel@tonic-gate uint32_t uc_vers; /* saved version number */
344*0Sstevel@tonic-gate uint32_t uc_proc; /* saved procedure number */
345*0Sstevel@tonic-gate struct sockaddr_in uc_addr; /* saved caller's address */
346*0Sstevel@tonic-gate };
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gate
349*0Sstevel@tonic-gate /*
350*0Sstevel@tonic-gate * the hashing function
351*0Sstevel@tonic-gate */
352*0Sstevel@tonic-gate #define CACHE_LOC(transp, xid) \
353*0Sstevel@tonic-gate (xid % (SPARSENESS*((struct udp_cache *) \
354*0Sstevel@tonic-gate su_data(transp)->su_cache)->uc_size))
355*0Sstevel@tonic-gate
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate /*
358*0Sstevel@tonic-gate * Enable use of the cache.
359*0Sstevel@tonic-gate * Note: there is no disable.
360*0Sstevel@tonic-gate */
361*0Sstevel@tonic-gate int
svcudp_enablecache(transp,size)362*0Sstevel@tonic-gate svcudp_enablecache(transp, size)
363*0Sstevel@tonic-gate SVCXPRT *transp;
364*0Sstevel@tonic-gate uint_t size;
365*0Sstevel@tonic-gate {
366*0Sstevel@tonic-gate struct svcudp_data *su = su_data(transp);
367*0Sstevel@tonic-gate struct udp_cache *uc;
368*0Sstevel@tonic-gate
369*0Sstevel@tonic-gate if (su->su_cache != NULL) {
370*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "enablecache: cache already enabled");
371*0Sstevel@tonic-gate return (0);
372*0Sstevel@tonic-gate }
373*0Sstevel@tonic-gate uc = ALLOC(struct udp_cache, 1);
374*0Sstevel@tonic-gate if (uc == NULL) {
375*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "enablecache: could not allocate cache");
376*0Sstevel@tonic-gate return (0);
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate uc->uc_size = size;
379*0Sstevel@tonic-gate uc->uc_nextvictim = 0;
380*0Sstevel@tonic-gate uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
381*0Sstevel@tonic-gate if (uc->uc_entries == NULL) {
382*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "enablecache: could not",
383*0Sstevel@tonic-gate " allocate cache data");
384*0Sstevel@tonic-gate FREE(uc, struct udp_cache, 1);
385*0Sstevel@tonic-gate return (0);
386*0Sstevel@tonic-gate }
387*0Sstevel@tonic-gate BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
388*0Sstevel@tonic-gate uc->uc_fifo = ALLOC(cache_ptr, size);
389*0Sstevel@tonic-gate if (uc->uc_fifo == NULL) {
390*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "enablecache: could not",
391*0Sstevel@tonic-gate " allocate cache fifo");
392*0Sstevel@tonic-gate FREE((char *)uc->uc_entries, cache_ptr, size * SPARSENESS);
393*0Sstevel@tonic-gate FREE((char *)uc, struct udp_cache, 1);
394*0Sstevel@tonic-gate return (0);
395*0Sstevel@tonic-gate }
396*0Sstevel@tonic-gate BZERO(uc->uc_fifo, cache_ptr, size);
397*0Sstevel@tonic-gate su->su_cache = (char *) uc;
398*0Sstevel@tonic-gate return (1);
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate
401*0Sstevel@tonic-gate
402*0Sstevel@tonic-gate /*
403*0Sstevel@tonic-gate * Set an entry in the cache
404*0Sstevel@tonic-gate */
405*0Sstevel@tonic-gate static void
cache_set(xprt,replylen)406*0Sstevel@tonic-gate cache_set(xprt, replylen)
407*0Sstevel@tonic-gate SVCXPRT *xprt;
408*0Sstevel@tonic-gate uint_t replylen;
409*0Sstevel@tonic-gate {
410*0Sstevel@tonic-gate register cache_ptr victim;
411*0Sstevel@tonic-gate register cache_ptr *vicp;
412*0Sstevel@tonic-gate register struct svcudp_data *su = su_data(xprt);
413*0Sstevel@tonic-gate struct udp_cache *uc = (struct udp_cache *) su->su_cache;
414*0Sstevel@tonic-gate u_int loc;
415*0Sstevel@tonic-gate char *newbuf;
416*0Sstevel@tonic-gate
417*0Sstevel@tonic-gate /*
418*0Sstevel@tonic-gate * Find space for the new entry, either by
419*0Sstevel@tonic-gate * reusing an old entry, or by mallocing a new one
420*0Sstevel@tonic-gate */
421*0Sstevel@tonic-gate victim = uc->uc_fifo[uc->uc_nextvictim];
422*0Sstevel@tonic-gate if (victim != NULL) {
423*0Sstevel@tonic-gate loc = CACHE_LOC(xprt, victim->cache_xid);
424*0Sstevel@tonic-gate for (vicp = &uc->uc_entries[loc];
425*0Sstevel@tonic-gate *vicp != NULL && *vicp != victim;
426*0Sstevel@tonic-gate vicp = &(*vicp)->cache_next)
427*0Sstevel@tonic-gate ;
428*0Sstevel@tonic-gate if (*vicp == NULL) {
429*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "cache_set: victim not found");
430*0Sstevel@tonic-gate return;
431*0Sstevel@tonic-gate }
432*0Sstevel@tonic-gate *vicp = victim->cache_next; /* remote from cache */
433*0Sstevel@tonic-gate newbuf = victim->cache_reply;
434*0Sstevel@tonic-gate } else {
435*0Sstevel@tonic-gate victim = ALLOC(struct cache_node, 1);
436*0Sstevel@tonic-gate if (victim == NULL) {
437*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "cache_set: victim alloc",
438*0Sstevel@tonic-gate " failed");
439*0Sstevel@tonic-gate return;
440*0Sstevel@tonic-gate }
441*0Sstevel@tonic-gate newbuf = (char *)mem_alloc(su->su_iosz);
442*0Sstevel@tonic-gate if (newbuf == NULL) {
443*0Sstevel@tonic-gate (void) syslog(LOG_ERR, "cache_set: could not",
444*0Sstevel@tonic-gate " allocate new rpc_buffer");
445*0Sstevel@tonic-gate FREE(victim, struct cache_node, 1);
446*0Sstevel@tonic-gate return;
447*0Sstevel@tonic-gate }
448*0Sstevel@tonic-gate }
449*0Sstevel@tonic-gate
450*0Sstevel@tonic-gate /*
451*0Sstevel@tonic-gate * Store it away
452*0Sstevel@tonic-gate */
453*0Sstevel@tonic-gate victim->cache_replylen = replylen;
454*0Sstevel@tonic-gate victim->cache_reply = rpc_buffer(xprt);
455*0Sstevel@tonic-gate rpc_buffer(xprt) = newbuf;
456*0Sstevel@tonic-gate xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
457*0Sstevel@tonic-gate su->su_iosz, XDR_ENCODE);
458*0Sstevel@tonic-gate victim->cache_xid = su->su_xid;
459*0Sstevel@tonic-gate victim->cache_proc = uc->uc_proc;
460*0Sstevel@tonic-gate victim->cache_vers = uc->uc_vers;
461*0Sstevel@tonic-gate victim->cache_prog = uc->uc_prog;
462*0Sstevel@tonic-gate victim->cache_addr = uc->uc_addr;
463*0Sstevel@tonic-gate loc = CACHE_LOC(xprt, victim->cache_xid);
464*0Sstevel@tonic-gate victim->cache_next = uc->uc_entries[loc];
465*0Sstevel@tonic-gate uc->uc_entries[loc] = victim;
466*0Sstevel@tonic-gate uc->uc_fifo[uc->uc_nextvictim++] = victim;
467*0Sstevel@tonic-gate uc->uc_nextvictim %= uc->uc_size;
468*0Sstevel@tonic-gate }
469*0Sstevel@tonic-gate
470*0Sstevel@tonic-gate /*
471*0Sstevel@tonic-gate * Try to get an entry from the cache
472*0Sstevel@tonic-gate * return 1 if found, 0 if not found
473*0Sstevel@tonic-gate */
474*0Sstevel@tonic-gate static int
cache_get(xprt,msg,replyp,replylenp)475*0Sstevel@tonic-gate cache_get(xprt, msg, replyp, replylenp)
476*0Sstevel@tonic-gate SVCXPRT *xprt;
477*0Sstevel@tonic-gate struct rpc_msg *msg;
478*0Sstevel@tonic-gate char **replyp;
479*0Sstevel@tonic-gate uint_t *replylenp;
480*0Sstevel@tonic-gate {
481*0Sstevel@tonic-gate u_int loc;
482*0Sstevel@tonic-gate register cache_ptr ent;
483*0Sstevel@tonic-gate register struct svcudp_data *su = su_data(xprt);
484*0Sstevel@tonic-gate register struct udp_cache *uc = (struct udp_cache *) su->su_cache;
485*0Sstevel@tonic-gate
486*0Sstevel@tonic-gate #define EQADDR(a1, a2) \
487*0Sstevel@tonic-gate (memcmp((char *)&a1, (char *)&a2, sizeof (a1)) == 0)
488*0Sstevel@tonic-gate
489*0Sstevel@tonic-gate loc = CACHE_LOC(xprt, su->su_xid);
490*0Sstevel@tonic-gate for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
491*0Sstevel@tonic-gate if (ent->cache_xid == su->su_xid &&
492*0Sstevel@tonic-gate ent->cache_proc == uc->uc_proc &&
493*0Sstevel@tonic-gate ent->cache_vers == uc->uc_vers &&
494*0Sstevel@tonic-gate ent->cache_prog == uc->uc_prog &&
495*0Sstevel@tonic-gate EQADDR(ent->cache_addr, uc->uc_addr)) {
496*0Sstevel@tonic-gate *replyp = ent->cache_reply;
497*0Sstevel@tonic-gate *replylenp = ent->cache_replylen;
498*0Sstevel@tonic-gate return (1);
499*0Sstevel@tonic-gate }
500*0Sstevel@tonic-gate }
501*0Sstevel@tonic-gate /*
502*0Sstevel@tonic-gate * Failed to find entry
503*0Sstevel@tonic-gate * Remember a few things so we can do a set later
504*0Sstevel@tonic-gate */
505*0Sstevel@tonic-gate uc->uc_proc = msg->rm_call.cb_proc;
506*0Sstevel@tonic-gate uc->uc_vers = msg->rm_call.cb_vers;
507*0Sstevel@tonic-gate uc->uc_prog = msg->rm_call.cb_prog;
508*0Sstevel@tonic-gate memcpy((char *)&uc->uc_addr, (char *)&xprt->xp_raddr,
509*0Sstevel@tonic-gate sizeof (struct sockaddr_in));
510*0Sstevel@tonic-gate return (0);
511*0Sstevel@tonic-gate }
512*0Sstevel@tonic-gate
513*0Sstevel@tonic-gate static struct xp_ops *
svcudp_ops()514*0Sstevel@tonic-gate svcudp_ops()
515*0Sstevel@tonic-gate {
516*0Sstevel@tonic-gate static struct xp_ops ops;
517*0Sstevel@tonic-gate
518*0Sstevel@tonic-gate if (ops.xp_recv == NULL) {
519*0Sstevel@tonic-gate ops.xp_recv = svcudp_recv;
520*0Sstevel@tonic-gate ops.xp_stat = svcudp_stat;
521*0Sstevel@tonic-gate ops.xp_getargs = svcudp_getargs;
522*0Sstevel@tonic-gate ops.xp_reply = svcudp_reply;
523*0Sstevel@tonic-gate ops.xp_freeargs = svcudp_freeargs;
524*0Sstevel@tonic-gate ops.xp_destroy = svcudp_destroy;
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate return (&ops);
527*0Sstevel@tonic-gate }
528