1 /* $NetBSD: clnt_dg.c,v 1.4 2000/07/14 08:40:41 fvdl Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #ident "@(#)clnt_dg.c 1.23 94/04/22 SMI"
37 static char sccsid[] = "@(#)clnt_dg.c 1.19 89/03/16 Copyr 1988 Sun Micro";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * Implements a connectionless client side RPC.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/mutex.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
63 #include <rpc/rpc_com.h>
66 #ifdef _FREEFALL_CONFIG
68 * Disable RPC exponential back-off for FreeBSD.org systems.
70 #define RPC_MAX_BACKOFF 1 /* second */
72 #define RPC_MAX_BACKOFF 30 /* seconds */
75 static bool_t time_not_ok(struct timeval *);
76 static enum clnt_stat clnt_dg_call(CLIENT *, struct rpc_callextra *,
77 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
78 static void clnt_dg_geterr(CLIENT *, struct rpc_err *);
79 static bool_t clnt_dg_freeres(CLIENT *, xdrproc_t, void *);
80 static void clnt_dg_abort(CLIENT *);
81 static bool_t clnt_dg_control(CLIENT *, u_int, void *);
82 static void clnt_dg_close(CLIENT *);
83 static void clnt_dg_destroy(CLIENT *);
84 static int clnt_dg_soupcall(struct socket *so, void *arg, int waitflag);
86 static struct clnt_ops clnt_dg_ops = {
87 .cl_call = clnt_dg_call,
88 .cl_abort = clnt_dg_abort,
89 .cl_geterr = clnt_dg_geterr,
90 .cl_freeres = clnt_dg_freeres,
91 .cl_close = clnt_dg_close,
92 .cl_destroy = clnt_dg_destroy,
93 .cl_control = clnt_dg_control
96 static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
99 * A pending RPC request which awaits a reply. Requests which have
100 * received their reply will have cr_xid set to zero and cr_mrep to
101 * the mbuf chain of the reply.
104 TAILQ_ENTRY(cu_request) cr_link;
105 CLIENT *cr_client; /* owner */
106 uint32_t cr_xid; /* XID of request */
107 struct mbuf *cr_mrep; /* reply received by upcall */
108 int cr_error; /* any error from upcall */
109 char cr_verf[MAX_AUTH_BYTES]; /* reply verf */
112 TAILQ_HEAD(cu_request_list, cu_request);
114 #define MCALL_MSG_SIZE 24
117 * This structure is pointed to by the socket buffer's sb_upcallarg
118 * member. It is separate from the client private data to facilitate
119 * multiple clients sharing the same socket. The cs_lock mutex is used
120 * to protect all fields of this structure, the socket's receive
121 * buffer SOCKBUF_LOCK is used to ensure that exactly one of these
122 * structures is installed on the socket.
126 int cs_refs; /* Count of clients */
127 struct cu_request_list cs_pending; /* Requests awaiting replies */
128 int cs_upcallrefs; /* Refcnt of upcalls in prog.*/
131 static void clnt_dg_upcallsdone(struct socket *, struct cu_socket *);
134 * Private data kept per client handle
137 int cu_threads; /* # threads in clnt_vc_call */
138 bool_t cu_closing; /* TRUE if we are closing */
139 bool_t cu_closed; /* TRUE if we are closed */
140 struct socket *cu_socket; /* connection socket */
141 bool_t cu_closeit; /* opened by library */
142 struct sockaddr_storage cu_raddr; /* remote address */
144 struct timeval cu_wait; /* retransmit interval */
145 struct timeval cu_total; /* total time for the call */
146 struct rpc_err cu_error;
148 char cu_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */
150 size_t cu_sendsz; /* send size */
151 size_t cu_recvsz; /* recv size */
153 int cu_connect; /* Use connect(). */
154 int cu_connected; /* Have done connect(). */
155 const char *cu_waitchan;
157 int cu_cwnd; /* congestion window */
158 int cu_sent; /* number of in-flight RPCs */
162 #define CWNDSCALE 256
163 #define MAXCWND (32 * CWNDSCALE)
166 * Connection less client creation returns with client handle parameters.
167 * Default options are set, which the user can change using clnt_control().
168 * fd should be open and bound.
169 * NB: The rpch->cl_auth is initialized to null authentication.
170 * Caller may wish to set this something more useful.
172 * sendsz and recvsz are the maximum allowable packet sizes that can be
173 * sent and received. Normally they are the same, but they can be
174 * changed to improve the program efficiency and buffer allocation.
175 * If they are 0, use the transport default.
177 * If svcaddr is NULL, returns NULL.
182 struct sockaddr *svcaddr, /* servers address */
183 rpcprog_t program, /* program number */
184 rpcvers_t version, /* version number */
185 size_t sendsz, /* buffer recv size */
186 size_t recvsz) /* buffer send size */
188 CLIENT *cl = NULL; /* client handle */
189 struct cu_data *cu = NULL; /* private data */
190 struct cu_socket *cs = NULL;
193 struct rpc_msg call_msg;
194 struct __rpc_sockinfo si;
198 if (svcaddr == NULL) {
199 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
203 if (!__rpc_socket2sockinfo(so, &si)) {
204 rpc_createerr.cf_stat = RPC_TLIERROR;
205 rpc_createerr.cf_error.re_errno = 0;
210 * Find the receive and the send size
212 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
213 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
214 if ((sendsz == 0) || (recvsz == 0)) {
215 rpc_createerr.cf_stat = RPC_TLIERROR; /* XXX */
216 rpc_createerr.cf_error.re_errno = 0;
220 cl = mem_alloc(sizeof (CLIENT));
223 * Should be multiple of 4 for XDR.
225 sendsz = ((sendsz + 3) / 4) * 4;
226 recvsz = ((recvsz + 3) / 4) * 4;
227 cu = mem_alloc(sizeof (*cu));
229 cu->cu_closing = FALSE;
230 cu->cu_closed = FALSE;
231 (void) memcpy(&cu->cu_raddr, svcaddr, (size_t)svcaddr->sa_len);
232 cu->cu_rlen = svcaddr->sa_len;
233 /* Other values can also be set through clnt_control() */
234 cu->cu_wait.tv_sec = 3; /* heuristically chosen */
235 cu->cu_wait.tv_usec = 0;
236 cu->cu_total.tv_sec = -1;
237 cu->cu_total.tv_usec = -1;
238 cu->cu_sendsz = sendsz;
239 cu->cu_recvsz = recvsz;
240 cu->cu_async = FALSE;
241 cu->cu_connect = FALSE;
242 cu->cu_connected = FALSE;
243 cu->cu_waitchan = "rpcrecv";
245 cu->cu_cwnd = MAXCWND / 2;
247 cu->cu_cwnd_wait = FALSE;
248 (void) getmicrotime(&now);
249 cu->cu_xid = __RPC_GETXID(&now);
250 call_msg.rm_xid = cu->cu_xid;
251 call_msg.rm_call.cb_prog = program;
252 call_msg.rm_call.cb_vers = version;
253 xdrmem_create(&xdrs, cu->cu_mcallc, MCALL_MSG_SIZE, XDR_ENCODE);
254 if (! xdr_callhdr(&xdrs, &call_msg)) {
255 rpc_createerr.cf_stat = RPC_CANTENCODEARGS; /* XXX */
256 rpc_createerr.cf_error.re_errno = 0;
259 cu->cu_mcalllen = XDR_GETPOS(&xdrs);
262 * By default, closeit is always FALSE. It is users responsibility
263 * to do a close on it, else the user may use clnt_control
264 * to let clnt_destroy do it for him/her.
266 cu->cu_closeit = FALSE;
268 error = soreserve(so, (u_long)sendsz, (u_long)recvsz);
270 rpc_createerr.cf_stat = RPC_FAILED;
271 rpc_createerr.cf_error.re_errno = error;
276 SOCKBUF_LOCK(&so->so_rcv);
279 if (sb->sb_upcall != clnt_dg_soupcall) {
280 SOCKBUF_UNLOCK(&so->so_rcv);
281 printf("clnt_dg_create(): socket already has an incompatible upcall\n");
284 cs = (struct cu_socket *) sb->sb_upcallarg;
285 mtx_lock(&cs->cs_lock);
287 mtx_unlock(&cs->cs_lock);
290 * We are the first on this socket - allocate the
291 * structure and install it in the socket.
293 SOCKBUF_UNLOCK(&so->so_rcv);
294 cs = mem_alloc(sizeof(*cs));
295 SOCKBUF_LOCK(&so->so_rcv);
298 * We have lost a race with some other client.
300 mem_free(cs, sizeof(*cs));
303 mtx_init(&cs->cs_lock, "cs->cs_lock", NULL, MTX_DEF);
305 cs->cs_upcallrefs = 0;
306 TAILQ_INIT(&cs->cs_pending);
307 soupcall_set(so, SO_RCV, clnt_dg_soupcall, cs);
309 SOCKBUF_UNLOCK(&so->so_rcv);
312 cl->cl_ops = &clnt_dg_ops;
313 cl->cl_private = (caddr_t)(void *)cu;
314 cl->cl_auth = authnone_create();
320 mem_free(cl, sizeof (CLIENT));
322 mem_free(cu, sizeof (*cu));
327 static enum clnt_stat
329 CLIENT *cl, /* client handle */
330 struct rpc_callextra *ext, /* call metadata */
331 rpcproc_t proc, /* procedure number */
332 struct mbuf *args, /* pointer to args */
333 struct mbuf **resultsp, /* pointer to results */
334 struct timeval utimeout) /* seconds to wait before giving up */
336 struct cu_data *cu = (struct cu_data *)cl->cl_private;
337 struct cu_socket *cs;
338 struct rpc_timers *rt;
340 struct rpc_err *errp;
343 struct rpc_msg reply_msg;
345 int retrans; /* number of re-transmits so far */
346 int nrefreshes = 2; /* number of times to refresh cred */
350 int next_sendtime, starttime, rtt, time_waited, tv = 0;
354 struct mbuf *mreq = NULL, *results;
355 struct cu_request *cr;
358 cs = cu->cu_socket->so_rcv.sb_upcallarg;
359 cr = malloc(sizeof(struct cu_request), M_RPC, M_WAITOK);
361 mtx_lock(&cs->cs_lock);
363 if (cu->cu_closing || cu->cu_closed) {
364 mtx_unlock(&cs->cs_lock);
366 return (RPC_CANTSEND);
375 errp = &cu->cu_error;
382 if (cu->cu_total.tv_usec == -1) {
383 tvp = &utimeout; /* use supplied timeout */
385 tvp = &cu->cu_total; /* use default timeout */
387 if (tvp->tv_sec || tvp->tv_usec)
388 timeout = tvtohz(tvp);
392 if (cu->cu_connect && !cu->cu_connected) {
393 mtx_unlock(&cs->cs_lock);
394 error = soconnect(cu->cu_socket,
395 (struct sockaddr *)&cu->cu_raddr, curthread);
396 mtx_lock(&cs->cs_lock);
398 errp->re_errno = error;
399 errp->re_status = stat = RPC_CANTSEND;
402 cu->cu_connected = 1;
404 if (cu->cu_connected) {
408 sa = (struct sockaddr *)&cu->cu_raddr;
413 if (ext && ext->rc_timers) {
416 rt->rt_rtxcur = tvtohz(&cu->cu_wait);
417 retransmit_time = next_sendtime = rt->rt_rtxcur;
420 retransmit_time = next_sendtime = tvtohz(&cu->cu_wait);
426 mtx_assert(&cs->cs_lock, MA_OWNED);
432 mtx_unlock(&cs->cs_lock);
434 MGETHDR(mreq, M_WAIT, MT_DATA);
435 KASSERT(cu->cu_mcalllen <= MHLEN, ("RPC header too big"));
436 bcopy(cu->cu_mcallc, mreq->m_data, cu->cu_mcalllen);
437 mreq->m_len = cu->cu_mcalllen;
440 * The XID is the first thing in the request.
442 *mtod(mreq, uint32_t *) = htonl(xid);
444 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
446 if (cu->cu_async == TRUE && args == NULL)
449 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
450 (! AUTH_MARSHALL(auth, xid, &xdrs,
451 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
452 errp->re_status = stat = RPC_CANTENCODEARGS;
453 mtx_lock(&cs->cs_lock);
456 mreq->m_pkthdr.len = m_length(mreq, NULL);
459 mtx_lock(&cs->cs_lock);
462 * Try to get a place in the congestion window.
464 while (cu->cu_sent >= cu->cu_cwnd) {
465 cu->cu_cwnd_wait = TRUE;
466 error = msleep(&cu->cu_cwnd_wait, &cs->cs_lock,
467 cu->cu_waitflag, "rpccwnd", 0);
469 errp->re_errno = error;
470 errp->re_status = stat = RPC_CANTSEND;
474 cu->cu_sent += CWNDSCALE;
476 TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link);
477 mtx_unlock(&cs->cs_lock);
480 * sosend consumes mreq.
482 error = sosend(cu->cu_socket, sa, NULL, mreq, NULL, 0, curthread);
486 * sub-optimal code appears here because we have
487 * some clock time to spare while the packets are in flight.
488 * (We assume that this is actually only executed once.)
490 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
491 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
492 reply_msg.acpted_rply.ar_verf.oa_length = 0;
493 reply_msg.acpted_rply.ar_results.where = NULL;
494 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
496 mtx_lock(&cs->cs_lock);
498 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
499 errp->re_errno = error;
500 errp->re_status = stat = RPC_CANTSEND;
501 cu->cu_sent -= CWNDSCALE;
502 if (cu->cu_cwnd_wait) {
503 cu->cu_cwnd_wait = FALSE;
504 wakeup(&cu->cu_cwnd_wait);
510 * Check to see if we got an upcall while waiting for the
514 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
515 errp->re_errno = cr->cr_error;
516 errp->re_status = stat = RPC_CANTRECV;
517 cu->cu_sent -= CWNDSCALE;
518 if (cu->cu_cwnd_wait) {
519 cu->cu_cwnd_wait = FALSE;
520 wakeup(&cu->cu_cwnd_wait);
525 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
526 cu->cu_sent -= CWNDSCALE;
527 if (cu->cu_cwnd_wait) {
528 cu->cu_cwnd_wait = FALSE;
529 wakeup(&cu->cu_cwnd_wait);
535 * Hack to provide rpc-based message passing
538 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
539 errp->re_status = stat = RPC_TIMEDOUT;
540 cu->cu_sent -= CWNDSCALE;
541 if (cu->cu_cwnd_wait) {
542 cu->cu_cwnd_wait = FALSE;
543 wakeup(&cu->cu_cwnd_wait);
550 /* Decide how long to wait. */
551 if (next_sendtime < timeout)
558 if (cu->cu_closing || cu->cu_closed) {
560 cr->cr_error = ESHUTDOWN;
562 error = msleep(cr, &cs->cs_lock,
563 cu->cu_waitflag, cu->cu_waitchan, tv);
569 TAILQ_REMOVE(&cs->cs_pending, cr, cr_link);
570 cu->cu_sent -= CWNDSCALE;
571 if (cu->cu_cwnd_wait) {
572 cu->cu_cwnd_wait = FALSE;
573 wakeup(&cu->cu_cwnd_wait);
578 * We were woken up by the upcall. If the
579 * upcall had a receive error, report that,
580 * otherwise we have a reply.
583 errp->re_errno = cr->cr_error;
584 errp->re_status = stat = RPC_CANTRECV;
588 cu->cu_cwnd += (CWNDSCALE * CWNDSCALE
589 + cu->cu_cwnd / 2) / cu->cu_cwnd;
590 if (cu->cu_cwnd > MAXCWND)
591 cu->cu_cwnd = MAXCWND;
595 * Add one to the time since a tick
596 * count of N means that the actual
597 * time taken was somewhere between N
600 rtt = ticks - starttime + 1;
603 * Update our estimate of the round
604 * trip time using roughly the
605 * algorithm described in RFC
606 * 2988. Given an RTT sample R:
608 * RTTVAR = (1-beta) * RTTVAR + beta * |SRTT-R|
609 * SRTT = (1-alpha) * SRTT + alpha * R
611 * where alpha = 0.125 and beta = 0.25.
613 * The initial retransmit timeout is
614 * SRTT + 4*RTTVAR and doubles on each
617 if (rt->rt_srtt == 0) {
619 rt->rt_deviate = rtt / 2;
621 int32_t error = rtt - rt->rt_srtt;
622 rt->rt_srtt += error / 8;
623 error = abs(error) - rt->rt_deviate;
624 rt->rt_deviate += error / 4;
626 rt->rt_rtxcur = rt->rt_srtt + 4*rt->rt_deviate;
633 * The sleep returned an error so our request is still
634 * on the list. If we got EWOULDBLOCK, we may want to
635 * re-send the request.
637 if (error != EWOULDBLOCK) {
638 errp->re_errno = error;
640 errp->re_status = stat = RPC_INTR;
642 errp->re_status = stat = RPC_CANTRECV;
646 time_waited = ticks - starttime;
648 /* Check for timeout. */
649 if (time_waited > timeout) {
650 errp->re_errno = EWOULDBLOCK;
651 errp->re_status = stat = RPC_TIMEDOUT;
655 /* Retransmit if necessary. */
656 if (time_waited >= next_sendtime) {
658 if (cu->cu_cwnd < CWNDSCALE)
659 cu->cu_cwnd = CWNDSCALE;
660 if (ext && ext->rc_feedback) {
661 mtx_unlock(&cs->cs_lock);
663 ext->rc_feedback(FEEDBACK_REXMIT1,
664 proc, ext->rc_feedback_arg);
666 ext->rc_feedback(FEEDBACK_REXMIT2,
667 proc, ext->rc_feedback_arg);
668 mtx_lock(&cs->cs_lock);
670 if (cu->cu_closing || cu->cu_closed) {
671 errp->re_errno = ESHUTDOWN;
672 errp->re_status = stat = RPC_CANTRECV;
676 /* update retransmit_time */
677 if (retransmit_time < RPC_MAX_BACKOFF * hz)
678 retransmit_time = 2 * retransmit_time;
679 next_sendtime += retransmit_time;
682 TAILQ_INSERT_TAIL(&cs->cs_pending, cr, cr_link);
687 * Now decode and validate the response. We need to drop the
688 * lock since xdr_replymsg may end up sleeping in malloc.
690 mtx_unlock(&cs->cs_lock);
692 if (ext && ext->rc_feedback)
693 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
695 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
696 ok = xdr_replymsg(&xdrs, &reply_msg);
700 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
701 (reply_msg.acpted_rply.ar_stat == SUCCESS))
702 errp->re_status = stat = RPC_SUCCESS;
704 stat = _seterr_reply(&reply_msg, &(cu->cu_error));
706 if (errp->re_status == RPC_SUCCESS) {
707 results = xdrmbuf_getall(&xdrs);
708 if (! AUTH_VALIDATE(auth, xid,
709 &reply_msg.acpted_rply.ar_verf,
711 errp->re_status = stat = RPC_AUTHERROR;
712 errp->re_why = AUTH_INVALIDRESP;
714 auth->ah_cred.oa_flavor == RPCSEC_GSS) {
716 * If we retransmitted, its
717 * possible that we will
718 * receive a reply for one of
719 * the earlier transmissions
720 * (which will use an older
721 * RPCSEC_GSS sequence
722 * number). In this case, just
723 * go back and listen for a
724 * new reply. We could keep a
725 * record of all the seq
726 * numbers we have transmitted
727 * so far so that we could
728 * accept a reply for any of
732 mtx_lock(&cs->cs_lock);
733 TAILQ_INSERT_TAIL(&cs->cs_pending,
741 } /* end successful completion */
743 * If unsuccesful AND error is an authentication error
744 * then refresh credentials and try again, else break
746 else if (stat == RPC_AUTHERROR)
747 /* maybe our credentials need to be refreshed ... */
748 if (nrefreshes > 0 &&
749 AUTH_REFRESH(auth, &reply_msg)) {
752 mtx_lock(&cs->cs_lock);
755 /* end of unsuccessful completion */
756 } /* end of valid reply message */
758 errp->re_status = stat = RPC_CANTDECODERES;
762 mtx_lock(&cs->cs_lock);
764 mtx_assert(&cs->cs_lock, MA_OWNED);
769 m_freem(cr->cr_mrep);
775 mtx_unlock(&cs->cs_lock);
777 if (auth && stat != RPC_SUCCESS)
778 AUTH_VALIDATE(auth, xid, NULL, NULL);
786 clnt_dg_geterr(CLIENT *cl, struct rpc_err *errp)
788 struct cu_data *cu = (struct cu_data *)cl->cl_private;
790 *errp = cu->cu_error;
794 clnt_dg_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
799 xdrs.x_op = XDR_FREE;
800 dummy = (*xdr_res)(&xdrs, res_ptr);
807 clnt_dg_abort(CLIENT *h)
812 clnt_dg_control(CLIENT *cl, u_int request, void *info)
814 struct cu_data *cu = (struct cu_data *)cl->cl_private;
815 struct cu_socket *cs;
816 struct sockaddr *addr;
818 cs = cu->cu_socket->so_rcv.sb_upcallarg;
819 mtx_lock(&cs->cs_lock);
823 cu->cu_closeit = TRUE;
824 mtx_unlock(&cs->cs_lock);
826 case CLSET_FD_NCLOSE:
827 cu->cu_closeit = FALSE;
828 mtx_unlock(&cs->cs_lock);
832 /* for other requests which use info */
834 mtx_unlock(&cs->cs_lock);
839 if (time_not_ok((struct timeval *)info)) {
840 mtx_unlock(&cs->cs_lock);
843 cu->cu_total = *(struct timeval *)info;
846 *(struct timeval *)info = cu->cu_total;
848 case CLSET_RETRY_TIMEOUT:
849 if (time_not_ok((struct timeval *)info)) {
850 mtx_unlock(&cs->cs_lock);
853 cu->cu_wait = *(struct timeval *)info;
855 case CLGET_RETRY_TIMEOUT:
856 *(struct timeval *)info = cu->cu_wait;
860 * Slightly different semantics to userland - we use
861 * sockaddr instead of netbuf.
863 memcpy(info, &cu->cu_raddr, cu->cu_raddr.ss_len);
865 case CLSET_SVC_ADDR: /* set to new address */
866 addr = (struct sockaddr *)info;
867 (void) memcpy(&cu->cu_raddr, addr, addr->sa_len);
870 *(uint32_t *)info = cu->cu_xid;
874 /* This will set the xid of the NEXT call */
875 /* decrement by 1 as clnt_dg_call() increments once */
876 cu->cu_xid = *(uint32_t *)info - 1;
881 * This RELIES on the information that, in the call body,
882 * the version number field is the fifth field from the
883 * begining of the RPC header. MUST be changed if the
884 * call_struct is changed
887 ntohl(*(uint32_t *)(void *)(cu->cu_mcallc +
888 4 * BYTES_PER_XDR_UNIT));
892 *(uint32_t *)(void *)(cu->cu_mcallc + 4 * BYTES_PER_XDR_UNIT)
893 = htonl(*(uint32_t *)info);
898 * This RELIES on the information that, in the call body,
899 * the program number field is the fourth field from the
900 * begining of the RPC header. MUST be changed if the
901 * call_struct is changed
904 ntohl(*(uint32_t *)(void *)(cu->cu_mcallc +
905 3 * BYTES_PER_XDR_UNIT));
909 *(uint32_t *)(void *)(cu->cu_mcallc + 3 * BYTES_PER_XDR_UNIT)
910 = htonl(*(uint32_t *)info);
913 cu->cu_async = *(int *)info;
916 cu->cu_connect = *(int *)info;
919 cu->cu_waitchan = (const char *)info;
922 *(const char **) info = cu->cu_waitchan;
924 case CLSET_INTERRUPTIBLE:
926 cu->cu_waitflag = PCATCH;
930 case CLGET_INTERRUPTIBLE:
932 *(int *) info = TRUE;
934 *(int *) info = FALSE;
937 mtx_unlock(&cs->cs_lock);
940 mtx_unlock(&cs->cs_lock);
945 clnt_dg_close(CLIENT *cl)
947 struct cu_data *cu = (struct cu_data *)cl->cl_private;
948 struct cu_socket *cs;
949 struct cu_request *cr;
951 cs = cu->cu_socket->so_rcv.sb_upcallarg;
952 mtx_lock(&cs->cs_lock);
955 mtx_unlock(&cs->cs_lock);
959 if (cu->cu_closing) {
960 while (cu->cu_closing)
961 msleep(cu, &cs->cs_lock, 0, "rpcclose", 0);
962 KASSERT(cu->cu_closed, ("client should be closed"));
963 mtx_unlock(&cs->cs_lock);
968 * Abort any pending requests and wait until everyone
969 * has finished with clnt_vc_call.
971 cu->cu_closing = TRUE;
972 TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
973 if (cr->cr_client == cl) {
975 cr->cr_error = ESHUTDOWN;
980 while (cu->cu_threads)
981 msleep(cu, &cs->cs_lock, 0, "rpcclose", 0);
983 cu->cu_closing = FALSE;
984 cu->cu_closed = TRUE;
986 mtx_unlock(&cs->cs_lock);
991 clnt_dg_destroy(CLIENT *cl)
993 struct cu_data *cu = (struct cu_data *)cl->cl_private;
994 struct cu_socket *cs;
995 struct socket *so = NULL;
996 bool_t lastsocketref;
998 cs = cu->cu_socket->so_rcv.sb_upcallarg;
1001 mtx_lock(&cs->cs_lock);
1004 if (cs->cs_refs == 0) {
1005 mtx_unlock(&cs->cs_lock);
1006 SOCKBUF_LOCK(&cu->cu_socket->so_rcv);
1007 soupcall_clear(cu->cu_socket, SO_RCV);
1008 clnt_dg_upcallsdone(cu->cu_socket, cs);
1009 SOCKBUF_UNLOCK(&cu->cu_socket->so_rcv);
1010 mtx_destroy(&cs->cs_lock);
1011 mem_free(cs, sizeof(*cs));
1012 lastsocketref = TRUE;
1014 mtx_unlock(&cs->cs_lock);
1015 lastsocketref = FALSE;
1018 if (cu->cu_closeit && lastsocketref) {
1020 cu->cu_socket = NULL;
1026 if (cl->cl_netid && cl->cl_netid[0])
1027 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
1028 if (cl->cl_tp && cl->cl_tp[0])
1029 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
1030 mem_free(cu, sizeof (*cu));
1031 mem_free(cl, sizeof (CLIENT));
1035 * Make sure that the time is not garbage. -1 value is allowed.
1038 time_not_ok(struct timeval *t)
1040 return (t->tv_sec < -1 || t->tv_sec > 100000000 ||
1041 t->tv_usec < -1 || t->tv_usec > 1000000);
1045 clnt_dg_soupcall(struct socket *so, void *arg, int waitflag)
1047 struct cu_socket *cs = (struct cu_socket *) arg;
1050 struct mbuf *control;
1051 struct cu_request *cr;
1052 int error, rcvflag, foundreq;
1055 cs->cs_upcallrefs++;
1056 uio.uio_resid = 1000000000;
1057 uio.uio_td = curthread;
1059 SOCKBUF_UNLOCK(&so->so_rcv);
1062 rcvflag = MSG_DONTWAIT;
1063 error = soreceive(so, NULL, &uio, &m, &control, &rcvflag);
1066 SOCKBUF_LOCK(&so->so_rcv);
1068 if (error == EWOULDBLOCK)
1072 * If there was an error, wake up all pending
1076 mtx_lock(&cs->cs_lock);
1077 TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
1079 cr->cr_error = error;
1082 mtx_unlock(&cs->cs_lock);
1087 * The XID is in the first uint32_t of the reply.
1089 if (m->m_len < sizeof(xid) && m_length(m, NULL) < sizeof(xid)) {
1091 * Should never happen.
1097 m_copydata(m, 0, sizeof(xid), (char *)&xid);
1101 * Attempt to match this reply with a pending request.
1103 mtx_lock(&cs->cs_lock);
1105 TAILQ_FOREACH(cr, &cs->cs_pending, cr_link) {
1106 if (cr->cr_xid == xid) {
1108 * This one matches. We leave the
1109 * reply mbuf in cr->cr_mrep. Set the
1110 * XID to zero so that we will ignore
1111 * any duplicated replies that arrive
1112 * before clnt_dg_call removes it from
1123 mtx_unlock(&cs->cs_lock);
1126 * If we didn't find the matching request, just drop
1127 * it - its probably a repeated reply.
1132 cs->cs_upcallrefs--;
1133 if (cs->cs_upcallrefs < 0)
1134 panic("rpcdg upcall refcnt");
1135 if (cs->cs_upcallrefs == 0)
1136 wakeup(&cs->cs_upcallrefs);
1141 * Wait for all upcalls in progress to complete.
1144 clnt_dg_upcallsdone(struct socket *so, struct cu_socket *cs)
1147 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1149 while (cs->cs_upcallrefs > 0)
1150 (void) msleep(&cs->cs_upcallrefs, SOCKBUF_MTX(&so->so_rcv), 0,