Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / TBBT / trace_play / rpc / svc_udp.c
1 #ifndef lint
2 static char sfs_svc_udp_id[] = "@(#)svc_udp.c     2.1     97/10/23";
3 #endif
4 /* @(#)svc_udp.c        2.2 88/07/29 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * svc_udp.c,
57  * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
58  * achieving execute-at-most-once semantics.)
59  *
60  * Copyright (C) 1984, Sun Microsystems, Inc.
61  */
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66 #include <string.h>
67 #include "rpc/rpc.h"
68 #include "rpc/osdep.h"
69 #include <errno.h>
70
71
72 #define rpc_buffer(xprt) ((xprt)->xp_p1)
73
74 static bool_t           svcudp_recv(SVCXPRT *, struct rpc_msg *);
75 static bool_t           svcudp_reply(SVCXPRT *, struct rpc_msg *) ;
76 static enum xprt_stat   svcudp_stat(SVCXPRT *);
77 static bool_t           svcudp_getargs(SVCXPRT *, xdrproc_t, void *);
78 static bool_t           svcudp_freeargs(SVCXPRT *, xdrproc_t, void *);
79 static void             svcudp_destroy(SVCXPRT *);
80
81 static struct xp_ops svcudp_op = {
82         svcudp_recv,
83         svcudp_stat,
84         svcudp_getargs,
85         svcudp_reply,
86         svcudp_freeargs,
87         svcudp_destroy
88 };
89
90 /*
91  * kept in xprt->xp_p2
92  */
93 struct svcudp_data {
94         uint_t   su_iosz;       /* byte size of send.recv buffer */
95         uint32_t su_xid;                /* transaction id */
96         XDR     su_xdrs;        /* XDR handle */
97         char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
98         char *  su_cache;       /* cached data, NULL if no cache */
99 };
100 #define su_data(xprt)   ((struct svcudp_data *)(xprt->xp_p2))
101
102 static void cache_set(SVCXPRT *, uint32_t);
103 static int cache_get(SVCXPRT *, struct rpc_msg *, char **, uint32_t *);
104
105 /*
106  * Usage:
107  *      xprt = svcudp_create(sock);
108  *
109  * If sock<0 then a socket is created, else sock is used.
110  * If the socket, sock is not bound to a port then svcudp_create
111  * binds it to an arbitrary port.  In any (successful) case,
112  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
113  * associated port number.
114  * Once *xprt is initialized, it is registered as a transporter;
115  * see (svc.h, xprt_register).
116  * The routines returns NULL if a problem occurred.
117  */
118 SVCXPRT *
119 svcudp_bufcreate(
120         int sock,
121         uint_t sendsz,
122         uint_t recvsz)
123 {
124         bool_t madesock = FALSE;
125         SVCXPRT *xprt;
126         struct svcudp_data *su;
127         struct sockaddr_in addr;
128 #if defined(AIX)
129         size_t len;
130 #else
131         int len;
132 #endif /* AIX */
133
134         len = sizeof(struct sockaddr_in);
135
136         if (sock == RPC_ANYSOCK) {
137                 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
138                         perror("svcudp_create: socket creation problem");
139                         return ((SVCXPRT *)NULL);
140                 }
141                 madesock = TRUE;
142         }
143         memset((char *)&addr, '\0', sizeof (addr));
144         addr.sin_family = AF_INET;
145         if (bindresvport(sock, &addr)) {
146                 addr.sin_port = 0;
147                 (void)bind(sock, (struct sockaddr *)&addr, len);
148         }
149         if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) {
150                 perror("svcudp_create - cannot getsockname");
151                 if (madesock)
152                         (void)close(sock);
153                 return ((SVCXPRT *)NULL);
154         }
155         xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
156         if (xprt == NULL) {
157                 (void)fprintf(stderr, "svcudp_create: out of memory\n");
158                 return (NULL);
159         }
160         su = (struct svcudp_data *)mem_alloc(sizeof(struct svcudp_data));
161         if (su == NULL) {
162                 (void)fprintf(stderr, "svcudp_create: out of memory\n");
163                 return (NULL);
164         }
165         su->su_iosz = ((((sendsz > recvsz) ? sendsz : recvsz) + 3) / 4) * 4;
166         if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
167                 (void)fprintf(stderr, "svcudp_create: out of memory\n");
168                 return (NULL);
169         }
170         xdrmem_create(
171             &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
172         su->su_cache = NULL;
173         xprt->xp_p2 = (caddr_t)su;
174         xprt->xp_verf.oa_base = su->su_verfbody;
175         xprt->xp_ops = &svcudp_op;
176         xprt->xp_port = ntohs(addr.sin_port);
177         xprt->xp_sock = sock;
178         xprt_register(xprt);
179         return (xprt);
180 }
181
182 SVCXPRT *
183 svcudp_create(
184         int sock)
185 {
186
187         return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
188 }
189
190 /*ARGSUSED*/
191 static enum xprt_stat
192 svcudp_stat(
193         SVCXPRT *xprt)
194 {
195
196         return (XPRT_IDLE); 
197 }
198
199 static bool_t
200 svcudp_recv(
201         SVCXPRT *xprt,
202         struct rpc_msg *msg)
203 {
204         struct svcudp_data *su = su_data(xprt);
205         XDR *xdrs = &(su->su_xdrs);
206         int rlen;
207         char *reply;
208         uint32_t replylen;
209
210     again:
211         xprt->xp_addrlen = sizeof(struct sockaddr_in);
212         rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz,
213             0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
214         if (rlen == -1 && errno == EINTR)
215                 goto again;
216         if (rlen < 4*sizeof(uint32_t))
217                 return (FALSE);
218         xdrs->x_op = XDR_DECODE;
219         XDR_SETPOS(xdrs, 0);
220         if (! xdr_callmsg(xdrs, msg))
221                 return (FALSE);
222         su->su_xid = msg->rm_xid;
223         if (su->su_cache != NULL) {
224                 if (cache_get(xprt, msg, &reply, &replylen)) {
225                         (void) sendto(xprt->xp_sock, reply, (int) replylen, 0,
226                           (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen);
227                         return (TRUE);
228                 }
229         }
230         return (TRUE);
231 }
232
233 static bool_t
234 svcudp_reply(
235         SVCXPRT *xprt, 
236         struct rpc_msg *msg) 
237 {
238         struct svcudp_data *su = su_data(xprt);
239         XDR *xdrs = &(su->su_xdrs);
240         int slen;
241         bool_t stat = FALSE;
242
243         xdrs->x_op = XDR_ENCODE;
244         XDR_SETPOS(xdrs, 0);
245         msg->rm_xid = su->su_xid;
246         if (xdr_replymsg(xdrs, msg)) {
247                 slen = (int)XDR_GETPOS(xdrs);
248                 if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
249                     (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
250                     == slen) {
251                         stat = TRUE;
252                         if (su->su_cache && slen >= 0) {
253                                 cache_set(xprt, (uint32_t) slen);
254                         }
255                 }
256         }
257         return (stat);
258 }
259
260 static bool_t
261 svcudp_getargs(
262         SVCXPRT *xprt,
263         xdrproc_t xdr_args,
264         void *args_ptr)
265 {
266
267         return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
268 }
269
270 static bool_t
271 svcudp_freeargs(
272         SVCXPRT *xprt,
273         xdrproc_t xdr_args,
274         void *args_ptr)
275 {
276         XDR *xdrs = &(su_data(xprt)->su_xdrs);
277
278         xdrs->x_op = XDR_FREE;
279         return ((*xdr_args)(xdrs, args_ptr));
280 }
281
282 static void
283 svcudp_destroy(
284         SVCXPRT *xprt)
285 {
286         struct svcudp_data *su = su_data(xprt);
287
288         xprt_unregister(xprt);
289         (void)close(xprt->xp_sock);
290         XDR_DESTROY(&(su->su_xdrs));
291         mem_free(rpc_buffer(xprt), su->su_iosz);
292         mem_free((caddr_t)su, sizeof(struct svcudp_data));
293         mem_free((caddr_t)xprt, sizeof(SVCXPRT));
294 }
295
296
297 /***********this could be a separate file*********************/
298
299 /*
300  * Fifo cache for udp server
301  * Copies pointers to reply buffers into fifo cache
302  * Buffers are sent again if retransmissions are detected.
303  */
304
305 #define SPARSENESS 4    /* 75% sparse */
306
307 #define CACHE_PERROR(msg)       \
308         (void) fprintf(stderr,"%s\n", msg)
309
310 #define ALLOC(type, size)       \
311         (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
312
313 #define BZERO(addr, type, size)  \
314         memset((void *) addr, '\0', sizeof(type) * (int) (size)) 
315
316 /*
317  * An entry in the cache
318  */
319 typedef struct cache_node *cache_ptr;
320 struct cache_node {
321         /*
322          * Index into cache is xid, proc, vers, prog and address
323          */
324         uint32_t cache_xid;
325         uint32_t cache_proc;
326         uint32_t cache_vers;
327         uint32_t cache_prog;
328         struct sockaddr_in cache_addr;
329         /*
330          * The cached reply and length
331          */
332         char * cache_reply;
333         uint32_t cache_replylen;
334         /*
335          * Next node on the list, if there is a collision
336          */
337         cache_ptr cache_next;   
338 };
339
340
341
342 /*
343  * The entire cache
344  */
345 struct udp_cache {
346         uint32_t uc_size;               /* size of cache */
347         cache_ptr *uc_entries;  /* hash table of entries in cache */
348         cache_ptr *uc_fifo;     /* fifo list of entries in cache */
349         uint32_t uc_nextvictim; /* points to next victim in fifo list */
350         uint32_t uc_prog;               /* saved program number */
351         uint32_t uc_vers;               /* saved version number */
352         uint32_t uc_proc;               /* saved procedure number */
353         struct sockaddr_in uc_addr; /* saved caller's address */
354 };
355
356
357 /*
358  * the hashing function
359  */
360 #define CACHE_LOC(transp, xid)  \
361  (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size)) 
362
363
364 /*
365  * Enable use of the cache. 
366  * Note: there is no disable.
367  */
368 svcudp_enablecache(
369         SVCXPRT *transp,
370         uint32_t size)
371 {
372         struct svcudp_data *su = su_data(transp);
373         struct udp_cache *uc;
374
375         if (su->su_cache != NULL) {
376                 CACHE_PERROR("enablecache: cache already enabled");
377                 return(0);      
378         }
379         uc = ALLOC(struct udp_cache, 1);
380         if (uc == NULL) {
381                 CACHE_PERROR("enablecache: could not allocate cache");
382                 return(0);
383         }
384         uc->uc_size = size;
385         uc->uc_nextvictim = 0;
386         uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
387         if (uc->uc_entries == NULL) {
388                 CACHE_PERROR("enablecache: could not allocate cache data");
389                 return(0);
390         }
391         BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
392         uc->uc_fifo = ALLOC(cache_ptr, size);
393         if (uc->uc_fifo == NULL) {
394                 CACHE_PERROR("enablecache: could not allocate cache fifo");
395                 return(0);
396         }
397         BZERO(uc->uc_fifo, cache_ptr, size);
398         su->su_cache = (char *) uc;
399         return(1);
400 }
401
402
403 /*
404  * Set an entry in the cache
405  */
406 static void
407 cache_set(
408         SVCXPRT *xprt,
409         uint32_t replylen)
410 {
411         cache_ptr victim;       
412         cache_ptr *vicp;
413         struct svcudp_data *su = su_data(xprt);
414         struct udp_cache *uc = (struct udp_cache *) su->su_cache;
415         uint_t loc;
416         char *newbuf;
417
418         /*
419          * Find space for the new entry, either by
420          * reusing an old entry, or by mallocing a new one
421          */
422         victim = uc->uc_fifo[uc->uc_nextvictim];
423         if (victim != NULL) {
424                 loc = CACHE_LOC(xprt, victim->cache_xid);
425                 for (vicp = &uc->uc_entries[loc]; 
426                   *vicp != NULL && *vicp != victim; 
427                   vicp = &(*vicp)->cache_next) 
428                                 ;
429                 if (*vicp == NULL) {
430                         CACHE_PERROR("cache_set: victim not found");
431                         return;
432                 }
433                 *vicp = victim->cache_next;     /* remote from cache */
434                 newbuf = victim->cache_reply;
435         } else {
436                 victim = ALLOC(struct cache_node, 1);
437                 if (victim == NULL) {
438                         CACHE_PERROR("cache_set: victim alloc failed");
439                         return;
440                 }
441                 newbuf = mem_alloc(su->su_iosz);
442                 if (newbuf == NULL) {
443                         CACHE_PERROR("cache_set: could not allocate new rpc_buffer");
444                         return;
445                 }
446         }
447
448         /*
449          * Store it away
450          */
451         victim->cache_replylen = replylen;
452         victim->cache_reply = rpc_buffer(xprt);
453         rpc_buffer(xprt) = newbuf;
454         xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE);
455         victim->cache_xid = su->su_xid;
456         victim->cache_proc = uc->uc_proc;
457         victim->cache_vers = uc->uc_vers;
458         victim->cache_prog = uc->uc_prog;
459         victim->cache_addr = uc->uc_addr;
460         loc = CACHE_LOC(xprt, victim->cache_xid);
461         victim->cache_next = uc->uc_entries[loc];       
462         uc->uc_entries[loc] = victim;
463         uc->uc_fifo[uc->uc_nextvictim++] = victim;
464         uc->uc_nextvictim %= uc->uc_size;
465 }
466
467 /*
468  * Try to get an entry from the cache
469  * return 1 if found, 0 if not found
470  */
471 static int
472 cache_get(
473         SVCXPRT *xprt,
474         struct rpc_msg *msg,
475         char **replyp,
476         uint32_t *replylenp)
477 {
478         uint_t loc;
479         cache_ptr ent;
480         struct svcudp_data *su = su_data(xprt);
481         struct udp_cache *uc = (struct udp_cache *) su->su_cache;
482
483 #       define EQADDR(a1, a2)   (memcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
484
485         loc = CACHE_LOC(xprt, su->su_xid);
486         for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
487                 if (ent->cache_xid == su->su_xid &&
488                   ent->cache_proc == uc->uc_proc &&
489                   ent->cache_vers == uc->uc_vers &&
490                   ent->cache_prog == uc->uc_prog &&
491                   EQADDR(ent->cache_addr, uc->uc_addr)) {
492                         *replyp = ent->cache_reply;
493                         *replylenp = ent->cache_replylen;
494                         return(1);
495                 }
496         }
497         /*
498          * Failed to find entry
499          * Remember a few things so we can do a set later
500          */
501         uc->uc_proc = msg->rm_call.cb_proc;
502         uc->uc_vers = msg->rm_call.cb_vers;
503         uc->uc_prog = msg->rm_call.cb_prog;
504         uc->uc_addr = xprt->xp_raddr;
505         return(0);
506 }
507