3 * Server side for UDP/IP based RPC. (Does some caching in the hopes of
4 * achieving execute-at-most-once semantics.)
6 * Copyright (c) 2010, Oracle America, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
18 * * Neither the name of the "Oracle America, Inc." nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/socket.h>
50 # include <libio/iolibio.h>
53 #define rpc_buffer(xprt) ((xprt)->xp_p1)
55 #define MAX(a, b) ((a > b) ? a : b)
58 static bool_t
svcudp_recv (SVCXPRT
*, struct rpc_msg
*);
59 static bool_t
svcudp_reply (SVCXPRT
*, struct rpc_msg
*);
60 static enum xprt_stat
svcudp_stat (SVCXPRT
*);
61 static bool_t
svcudp_getargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
62 static bool_t
svcudp_freeargs (SVCXPRT
*, xdrproc_t
, caddr_t
);
63 static void svcudp_destroy (SVCXPRT
*);
65 static const struct xp_ops svcudp_op
=
75 static int cache_get (SVCXPRT
*, struct rpc_msg
*, char **replyp
,
77 static void cache_set (SVCXPRT
*xprt
, u_long replylen
);
84 u_int su_iosz
; /* byte size of send.recv buffer */
85 u_long su_xid
; /* transaction id */
86 XDR su_xdrs
; /* XDR handle */
87 char su_verfbody
[MAX_AUTH_BYTES
]; /* verifier body */
88 char *su_cache
; /* cached data, NULL if no cache */
90 #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2))
94 * xprt = svcudp_create(sock);
96 * If sock<0 then a socket is created, else sock is used.
97 * If the socket, sock is not bound to a port then svcudp_create
98 * binds it to an arbitrary port. In any (successful) case,
99 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
100 * associated port number.
101 * Once *xprt is initialized, it is registered as a transporter;
102 * see (svc.h, xprt_register).
103 * The routines returns NULL if a problem occurred.
106 svcudp_bufcreate (sock
, sendsz
, recvsz
)
108 u_int sendsz
, recvsz
;
110 bool_t madesock
= FALSE
;
112 struct svcudp_data
*su
;
113 struct sockaddr_in addr
;
114 socklen_t len
= sizeof (struct sockaddr_in
);
118 if (sock
== RPC_ANYSOCK
)
120 if ((sock
= __socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0)
122 perror (_("svcudp_create: socket creation problem"));
123 return (SVCXPRT
*) NULL
;
127 __bzero ((char *) &addr
, sizeof (addr
));
128 addr
.sin_family
= AF_INET
;
129 if (bindresvport (sock
, &addr
))
132 (void) __bind (sock
, (struct sockaddr
*) &addr
, len
);
134 if (__getsockname (sock
, (struct sockaddr
*) &addr
, &len
) != 0)
136 perror (_("svcudp_create - cannot getsockname"));
138 (void) __close (sock
);
139 return (SVCXPRT
*) NULL
;
141 xprt
= (SVCXPRT
*) mem_alloc (sizeof (SVCXPRT
));
142 su
= (struct svcudp_data
*) mem_alloc (sizeof (*su
));
143 buf
= mem_alloc (((MAX (sendsz
, recvsz
) + 3) / 4) * 4);
144 if (xprt
== NULL
|| su
== NULL
|| buf
== NULL
)
146 (void) __fxprintf (NULL
, "%s: %s",
147 "svcudp_create", _("out of memory\n"));
148 mem_free (xprt
, sizeof (SVCXPRT
));
149 mem_free (su
, sizeof (*su
));
150 mem_free (buf
, ((MAX (sendsz
, recvsz
) + 3) / 4) * 4);
153 su
->su_iosz
= ((MAX (sendsz
, recvsz
) + 3) / 4) * 4;
154 rpc_buffer (xprt
) = buf
;
155 xdrmem_create (&(su
->su_xdrs
), rpc_buffer (xprt
), su
->su_iosz
, XDR_DECODE
);
157 xprt
->xp_p2
= (caddr_t
) su
;
158 xprt
->xp_verf
.oa_base
= su
->su_verfbody
;
159 xprt
->xp_ops
= &svcudp_op
;
160 xprt
->xp_port
= ntohs (addr
.sin_port
);
161 xprt
->xp_sock
= sock
;
164 if ((sizeof (struct iovec
) + sizeof (struct msghdr
)
165 + sizeof(struct cmsghdr
) + sizeof (struct in_pktinfo
))
166 > sizeof (xprt
->xp_pad
))
168 (void) __fxprintf (NULL
,"%s", _("\
169 svcudp_create: xp_pad is too small for IP_PKTINFO\n"));
173 if (__setsockopt (sock
, SOL_IP
, IP_PKTINFO
, (void *) &pad
,
175 /* Set the padding to all 1s. */
179 /* Clear the padding. */
181 memset (&xprt
->xp_pad
[0], pad
, sizeof (xprt
->xp_pad
));
183 xprt_register (xprt
);
186 #ifdef EXPORT_RPC_SYMBOLS
187 libc_hidden_def (svcudp_bufcreate
)
189 libc_hidden_nolink (svcudp_bufcreate
, GLIBC_2_0
)
196 return svcudp_bufcreate (sock
, UDPMSGSIZE
, UDPMSGSIZE
);
198 #ifdef EXPORT_RPC_SYMBOLS
199 libc_hidden_def (svcudp_create
)
201 libc_hidden_nolink (svcudp_create
, GLIBC_2_0
)
204 static enum xprt_stat
213 svcudp_recv (xprt
, msg
)
217 struct svcudp_data
*su
= su_data (xprt
);
218 XDR
*xdrs
= &(su
->su_xdrs
);
224 /* It is very tricky when you have IP aliases. We want to make sure
225 that we are sending the packet from the IP address where the
226 incoming packet is addressed to. H.J. */
229 struct msghdr
*mesgp
;
233 /* FIXME -- should xp_addrlen be a size_t? */
234 len
= (socklen_t
) sizeof(struct sockaddr_in
);
236 iovp
= (struct iovec
*) &xprt
->xp_pad
[0];
237 mesgp
= (struct msghdr
*) &xprt
->xp_pad
[sizeof (struct iovec
)];
238 if (mesgp
->msg_iovlen
)
240 iovp
->iov_base
= rpc_buffer (xprt
);
241 iovp
->iov_len
= su
->su_iosz
;
242 mesgp
->msg_iov
= iovp
;
243 mesgp
->msg_iovlen
= 1;
244 mesgp
->msg_name
= &(xprt
->xp_raddr
);
245 mesgp
->msg_namelen
= len
;
246 mesgp
->msg_control
= &xprt
->xp_pad
[sizeof (struct iovec
)
247 + sizeof (struct msghdr
)];
248 mesgp
->msg_controllen
= sizeof(xprt
->xp_pad
)
249 - sizeof (struct iovec
) - sizeof (struct msghdr
);
250 rlen
= __recvmsg (xprt
->xp_sock
, mesgp
, 0);
253 struct cmsghdr
*cmsg
;
254 len
= mesgp
->msg_namelen
;
255 cmsg
= CMSG_FIRSTHDR (mesgp
);
257 || CMSG_NXTHDR (mesgp
, cmsg
) != NULL
258 || cmsg
->cmsg_level
!= SOL_IP
259 || cmsg
->cmsg_type
!= IP_PKTINFO
260 || cmsg
->cmsg_len
< (sizeof (struct cmsghdr
)
261 + sizeof (struct in_pktinfo
)))
263 /* Not a simple IP_PKTINFO, ignore it. */
264 mesgp
->msg_control
= NULL
;
265 mesgp
->msg_controllen
= 0;
269 /* It was a simple IP_PKTIFO as we expected, discard the
271 struct in_pktinfo
*pkti
= (struct in_pktinfo
*) CMSG_DATA (cmsg
);
272 pkti
->ipi_ifindex
= 0;
278 rlen
= __recvfrom (xprt
->xp_sock
, rpc_buffer (xprt
),
279 (int) su
->su_iosz
, 0,
280 (struct sockaddr
*) &(xprt
->xp_raddr
), &len
);
281 xprt
->xp_addrlen
= len
;
282 if (rlen
== -1 && errno
== EINTR
)
284 if (rlen
< 16) /* < 4 32-bit ints? */
286 xdrs
->x_op
= XDR_DECODE
;
287 XDR_SETPOS (xdrs
, 0);
288 if (!xdr_callmsg (xdrs
, msg
))
290 su
->su_xid
= msg
->rm_xid
;
291 if (su
->su_cache
!= NULL
)
293 if (cache_get (xprt
, msg
, &reply
, &replylen
))
296 if (mesgp
->msg_iovlen
)
298 iovp
->iov_base
= reply
;
299 iovp
->iov_len
= replylen
;
300 (void) __sendmsg (xprt
->xp_sock
, mesgp
, 0);
304 (void) __sendto (xprt
->xp_sock
, reply
, (int) replylen
, 0,
305 (struct sockaddr
*) &xprt
->xp_raddr
, len
);
313 svcudp_reply (xprt
, msg
)
317 struct svcudp_data
*su
= su_data (xprt
);
318 XDR
*xdrs
= &(su
->su_xdrs
);
323 struct msghdr
*mesgp
;
326 xdrs
->x_op
= XDR_ENCODE
;
327 XDR_SETPOS (xdrs
, 0);
328 msg
->rm_xid
= su
->su_xid
;
329 if (xdr_replymsg (xdrs
, msg
))
331 slen
= (int) XDR_GETPOS (xdrs
);
333 mesgp
= (struct msghdr
*) &xprt
->xp_pad
[sizeof (struct iovec
)];
334 if (mesgp
->msg_iovlen
)
336 iovp
= (struct iovec
*) &xprt
->xp_pad
[0];
337 iovp
->iov_base
= rpc_buffer (xprt
);
338 iovp
->iov_len
= slen
;
339 sent
= __sendmsg (xprt
->xp_sock
, mesgp
, 0);
343 sent
= __sendto (xprt
->xp_sock
, rpc_buffer (xprt
), slen
, 0,
344 (struct sockaddr
*) &(xprt
->xp_raddr
),
349 if (su
->su_cache
&& slen
>= 0)
351 cache_set (xprt
, (u_long
) slen
);
359 svcudp_getargs (xprt
, xdr_args
, args_ptr
)
365 return (*xdr_args
) (&(su_data (xprt
)->su_xdrs
), args_ptr
);
369 svcudp_freeargs (xprt
, xdr_args
, args_ptr
)
374 XDR
*xdrs
= &(su_data (xprt
)->su_xdrs
);
376 xdrs
->x_op
= XDR_FREE
;
377 return (*xdr_args
) (xdrs
, args_ptr
);
381 svcudp_destroy (xprt
)
384 struct svcudp_data
*su
= su_data (xprt
);
386 xprt_unregister (xprt
);
387 (void) __close (xprt
->xp_sock
);
388 XDR_DESTROY (&(su
->su_xdrs
));
389 mem_free (rpc_buffer (xprt
), su
->su_iosz
);
390 mem_free ((caddr_t
) su
, sizeof (struct svcudp_data
));
391 mem_free ((caddr_t
) xprt
, sizeof (SVCXPRT
));
395 /***********this could be a separate file*********************/
398 * Fifo cache for udp server
399 * Copies pointers to reply buffers into fifo cache
400 * Buffers are sent again if retransmissions are detected.
403 #define SPARSENESS 4 /* 75% sparse */
405 #define CACHE_PERROR(msg) \
406 (void) __fxprintf(NULL, "%s\n", msg)
408 #define ALLOC(type, size) \
409 (type *) mem_alloc((unsigned) (sizeof(type) * (size)))
411 #define CALLOC(type, size) \
412 (type *) calloc (sizeof (type), size)
415 * An entry in the cache
417 typedef struct cache_node
*cache_ptr
;
421 * Index into cache is xid, proc, vers, prog and address
427 struct sockaddr_in cache_addr
;
429 * The cached reply and length
432 u_long cache_replylen
;
434 * Next node on the list, if there is a collision
436 cache_ptr cache_next
;
446 u_long uc_size
; /* size of cache */
447 cache_ptr
*uc_entries
; /* hash table of entries in cache */
448 cache_ptr
*uc_fifo
; /* fifo list of entries in cache */
449 u_long uc_nextvictim
; /* points to next victim in fifo list */
450 u_long uc_prog
; /* saved program number */
451 u_long uc_vers
; /* saved version number */
452 u_long uc_proc
; /* saved procedure number */
453 struct sockaddr_in uc_addr
; /* saved caller's address */
458 * the hashing function
460 #define CACHE_LOC(transp, xid) \
461 (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size))
465 * Enable use of the cache.
466 * Note: there is no disable.
469 svcudp_enablecache (SVCXPRT
*transp
, u_long size
)
471 struct svcudp_data
*su
= su_data (transp
);
472 struct udp_cache
*uc
;
474 if (su
->su_cache
!= NULL
)
476 CACHE_PERROR (_("enablecache: cache already enabled"));
479 uc
= ALLOC (struct udp_cache
, 1);
482 CACHE_PERROR (_("enablecache: could not allocate cache"));
486 uc
->uc_nextvictim
= 0;
487 uc
->uc_entries
= CALLOC (cache_ptr
, size
* SPARSENESS
);
488 if (uc
->uc_entries
== NULL
)
490 mem_free (uc
, sizeof (struct udp_cache
));
491 CACHE_PERROR (_("enablecache: could not allocate cache data"));
494 uc
->uc_fifo
= CALLOC (cache_ptr
, size
);
495 if (uc
->uc_fifo
== NULL
)
497 mem_free (uc
->uc_entries
, size
* SPARSENESS
);
498 mem_free (uc
, sizeof (struct udp_cache
));
499 CACHE_PERROR (_("enablecache: could not allocate cache fifo"));
502 su
->su_cache
= (char *) uc
;
505 libc_hidden_nolink (svcudp_enablecache
, GLIBC_2_0
)
509 * Set an entry in the cache
512 cache_set (SVCXPRT
*xprt
, u_long replylen
)
516 struct svcudp_data
*su
= su_data (xprt
);
517 struct udp_cache
*uc
= (struct udp_cache
*) su
->su_cache
;
522 * Find space for the new entry, either by
523 * reusing an old entry, or by mallocing a new one
525 victim
= uc
->uc_fifo
[uc
->uc_nextvictim
];
528 loc
= CACHE_LOC (xprt
, victim
->cache_xid
);
529 for (vicp
= &uc
->uc_entries
[loc
];
530 *vicp
!= NULL
&& *vicp
!= victim
;
531 vicp
= &(*vicp
)->cache_next
)
535 CACHE_PERROR (_("cache_set: victim not found"));
538 *vicp
= victim
->cache_next
; /* remote from cache */
539 newbuf
= victim
->cache_reply
;
543 victim
= ALLOC (struct cache_node
, 1);
546 CACHE_PERROR (_("cache_set: victim alloc failed"));
549 newbuf
= mem_alloc (su
->su_iosz
);
552 mem_free (victim
, sizeof (struct cache_node
));
553 CACHE_PERROR (_("cache_set: could not allocate new rpc_buffer"));
561 victim
->cache_replylen
= replylen
;
562 victim
->cache_reply
= rpc_buffer (xprt
);
563 rpc_buffer (xprt
) = newbuf
;
564 xdrmem_create (&(su
->su_xdrs
), rpc_buffer (xprt
), su
->su_iosz
, XDR_ENCODE
);
565 victim
->cache_xid
= su
->su_xid
;
566 victim
->cache_proc
= uc
->uc_proc
;
567 victim
->cache_vers
= uc
->uc_vers
;
568 victim
->cache_prog
= uc
->uc_prog
;
569 victim
->cache_addr
= uc
->uc_addr
;
570 loc
= CACHE_LOC (xprt
, victim
->cache_xid
);
571 victim
->cache_next
= uc
->uc_entries
[loc
];
572 uc
->uc_entries
[loc
] = victim
;
573 uc
->uc_fifo
[uc
->uc_nextvictim
++] = victim
;
574 uc
->uc_nextvictim
%= uc
->uc_size
;
578 * Try to get an entry from the cache
579 * return 1 if found, 0 if not found
582 cache_get (xprt
, msg
, replyp
, replylenp
)
590 struct svcudp_data
*su
= su_data (xprt
);
591 struct udp_cache
*uc
= (struct udp_cache
*) su
->su_cache
;
593 #define EQADDR(a1, a2) (memcmp((char*)&a1, (char*)&a2, sizeof(a1)) == 0)
595 loc
= CACHE_LOC (xprt
, su
->su_xid
);
596 for (ent
= uc
->uc_entries
[loc
]; ent
!= NULL
; ent
= ent
->cache_next
)
598 if (ent
->cache_xid
== su
->su_xid
&&
599 ent
->cache_proc
== uc
->uc_proc
&&
600 ent
->cache_vers
== uc
->uc_vers
&&
601 ent
->cache_prog
== uc
->uc_prog
&&
602 EQADDR (ent
->cache_addr
, uc
->uc_addr
))
604 *replyp
= ent
->cache_reply
;
605 *replylenp
= ent
->cache_replylen
;
610 * Failed to find entry
611 * Remember a few things so we can do a set later
613 uc
->uc_proc
= msg
->rm_call
.cb_proc
;
614 uc
->uc_vers
= msg
->rm_call
.cb_vers
;
615 uc
->uc_prog
= msg
->rm_call
.cb_prog
;
616 memcpy (&uc
->uc_addr
, &xprt
->xp_raddr
, sizeof (uc
->uc_addr
));