1 /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 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 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";
35 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
43 * Copyright (C) 1984, Sun Microsystems, Inc.
45 * TCP based RPC supports 'batched calls'.
46 * A sequence of calls may be batched-up in a send buffer. The rpc call
47 * return immediately to the client even though the call was not necessarily
48 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
49 * the rpc timeout value is zero (see clnt.h, rpc).
51 * Clients should NOT casually batch calls that in fact return results; that is,
52 * the server side should be aware that a call is batched and not produce any
53 * return message. Batched calls that produce many result messages can
54 * deadlock (netlock) the client and the server....
56 * Now go hang yourself.
59 #include <sys/param.h>
60 #include <sys/systm.h>
62 #include <sys/malloc.h>
64 #include <sys/mutex.h>
67 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/syslog.h>
76 #include <netinet/tcp.h>
79 #include <rpc/rpc_com.h>
81 #define MCALL_MSG_SIZE 24
85 struct cmsgcred cmcred;
88 static enum clnt_stat clnt_vc_call(CLIENT *, struct rpc_callextra *,
89 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
90 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
91 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
92 static void clnt_vc_abort(CLIENT *);
93 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
94 static void clnt_vc_close(CLIENT *);
95 static void clnt_vc_destroy(CLIENT *);
96 static bool_t time_not_ok(struct timeval *);
97 static int clnt_vc_soupcall(struct socket *so, void *arg, int waitflag);
99 static struct clnt_ops clnt_vc_ops = {
100 .cl_call = clnt_vc_call,
101 .cl_abort = clnt_vc_abort,
102 .cl_geterr = clnt_vc_geterr,
103 .cl_freeres = clnt_vc_freeres,
104 .cl_close = clnt_vc_close,
105 .cl_destroy = clnt_vc_destroy,
106 .cl_control = clnt_vc_control
110 * A pending RPC request which awaits a reply. Requests which have
111 * received their reply will have cr_xid set to zero and cr_mrep to
112 * the mbuf chain of the reply.
115 TAILQ_ENTRY(ct_request) cr_link;
116 uint32_t cr_xid; /* XID of request */
117 struct mbuf *cr_mrep; /* reply received by upcall */
118 int cr_error; /* any error from upcall */
119 char cr_verf[MAX_AUTH_BYTES]; /* reply verf */
122 TAILQ_HEAD(ct_request_list, ct_request);
126 int ct_threads; /* number of threads in clnt_vc_call */
127 bool_t ct_closing; /* TRUE if we are closing */
128 bool_t ct_closed; /* TRUE if we are closed */
129 struct socket *ct_socket; /* connection socket */
130 bool_t ct_closeit; /* close it on destroy */
131 struct timeval ct_wait; /* wait interval in milliseconds */
132 struct sockaddr_storage ct_addr; /* remote addr */
133 struct rpc_err ct_error;
135 char ct_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */
136 size_t ct_mpos; /* pos after marshal */
137 const char *ct_waitchan;
139 struct mbuf *ct_record; /* current reply record */
140 size_t ct_record_resid; /* how much left of reply to read */
141 bool_t ct_record_eor; /* true if reading last fragment */
142 struct ct_request_list ct_pending;
143 int ct_upcallrefs; /* Ref cnt of upcalls in prog. */
146 static void clnt_vc_upcallsdone(struct ct_data *);
148 static const char clnt_vc_errstr[] = "%s : %s";
149 static const char clnt_vc_str[] = "clnt_vc_create";
150 static const char clnt_read_vc_str[] = "read_vc";
151 static const char __no_mem_str[] = "out of memory";
154 * Create a client handle for a connection.
155 * Default options are set, which the user can change using clnt_control()'s.
156 * The rpc/vc package does buffering similar to stdio, so the client
157 * must pick send and receive buffer sizes, 0 => use the default.
158 * NB: fd is copied into a private area.
159 * NB: The rpch->cl_auth is set null authentication. Caller may wish to
160 * set this something more useful.
162 * fd should be an open socket
166 struct socket *so, /* open file descriptor */
167 struct sockaddr *raddr, /* servers address */
168 const rpcprog_t prog, /* program number */
169 const rpcvers_t vers, /* version number */
170 size_t sendsz, /* buffer recv size */
171 size_t recvsz, /* buffer send size */
172 int intrflag) /* interruptible */
174 CLIENT *cl; /* client handle */
175 struct ct_data *ct = NULL; /* client handle */
177 struct rpc_msg call_msg;
178 static uint32_t disrupt;
179 struct __rpc_sockinfo si;
181 int error, interrupted, one = 1, sleep_flag;
185 disrupt = (uint32_t)(long)raddr;
187 cl = (CLIENT *)mem_alloc(sizeof (*cl));
188 ct = (struct ct_data *)mem_alloc(sizeof (*ct));
190 mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
192 ct->ct_closing = FALSE;
193 ct->ct_closed = FALSE;
194 ct->ct_upcallrefs = 0;
196 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
197 error = soconnect(so, raddr, curthread);
202 sleep_flag |= (PCATCH | PBDRY);
203 while ((so->so_state & SS_ISCONNECTING)
204 && so->so_error == 0) {
205 error = msleep(&so->so_timeo, SOCK_MTX(so),
206 sleep_flag, "connec", 0);
208 if (error == EINTR || error == ERESTART)
214 error = so->so_error;
220 so->so_state &= ~SS_ISCONNECTING;
221 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
222 rpc_createerr.cf_error.re_errno = error;
227 if (!__rpc_socket2sockinfo(so, &si)) {
231 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
232 bzero(&sopt, sizeof(sopt));
233 sopt.sopt_dir = SOPT_SET;
234 sopt.sopt_level = SOL_SOCKET;
235 sopt.sopt_name = SO_KEEPALIVE;
236 sopt.sopt_val = &one;
237 sopt.sopt_valsize = sizeof(one);
241 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
242 bzero(&sopt, sizeof(sopt));
243 sopt.sopt_dir = SOPT_SET;
244 sopt.sopt_level = IPPROTO_TCP;
245 sopt.sopt_name = TCP_NODELAY;
246 sopt.sopt_val = &one;
247 sopt.sopt_valsize = sizeof(one);
251 ct->ct_closeit = FALSE;
254 * Set up private data struct
257 ct->ct_wait.tv_sec = -1;
258 ct->ct_wait.tv_usec = -1;
259 memcpy(&ct->ct_addr, raddr, raddr->sa_len);
262 * Initialize call message
265 ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
266 call_msg.rm_xid = ct->ct_xid;
267 call_msg.rm_direction = CALL;
268 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
269 call_msg.rm_call.cb_prog = (uint32_t)prog;
270 call_msg.rm_call.cb_vers = (uint32_t)vers;
273 * pre-serialize the static part of the call msg and stash it away
275 xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
277 if (! xdr_callhdr(&xdrs, &call_msg)) {
278 if (ct->ct_closeit) {
279 soclose(ct->ct_socket);
283 ct->ct_mpos = XDR_GETPOS(&xdrs);
285 ct->ct_waitchan = "rpcrecv";
289 * Create a client handle which uses xdrrec for serialization
290 * and authnone for authentication.
292 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
293 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
294 error = soreserve(ct->ct_socket, sendsz, recvsz);
296 if (ct->ct_closeit) {
297 soclose(ct->ct_socket);
302 cl->cl_ops = &clnt_vc_ops;
304 cl->cl_auth = authnone_create();
306 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
307 soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
308 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
310 ct->ct_record = NULL;
311 ct->ct_record_resid = 0;
312 TAILQ_INIT(&ct->ct_pending);
318 mtx_destroy(&ct->ct_lock);
319 mem_free(ct, sizeof (struct ct_data));
322 mem_free(cl, sizeof (CLIENT));
324 return ((CLIENT *)NULL);
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)
336 struct ct_data *ct = (struct ct_data *) cl->cl_private;
338 struct rpc_err *errp;
341 struct rpc_msg reply_msg;
343 int nrefreshes = 2; /* number of times to refresh cred */
344 struct timeval timeout;
346 struct mbuf *mreq = NULL, *results;
347 struct ct_request *cr;
350 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
352 mtx_lock(&ct->ct_lock);
354 if (ct->ct_closing || ct->ct_closed) {
355 mtx_unlock(&ct->ct_lock);
357 return (RPC_CANTSEND);
366 errp = &ct->ct_error;
372 if (ct->ct_wait.tv_usec == -1) {
373 timeout = utimeout; /* use supplied timeout */
375 timeout = ct->ct_wait; /* use default timeout */
379 mtx_assert(&ct->ct_lock, MA_OWNED);
384 mtx_unlock(&ct->ct_lock);
387 * Leave space to pre-pend the record mark.
389 MGETHDR(mreq, M_WAIT, MT_DATA);
390 mreq->m_data += sizeof(uint32_t);
391 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
392 ("RPC header too big"));
393 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
394 mreq->m_len = ct->ct_mpos;
397 * The XID is the first thing in the request.
399 *mtod(mreq, uint32_t *) = htonl(xid);
401 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
403 errp->re_status = stat = RPC_SUCCESS;
405 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
406 (! AUTH_MARSHALL(auth, xid, &xdrs,
407 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
408 errp->re_status = stat = RPC_CANTENCODEARGS;
409 mtx_lock(&ct->ct_lock);
412 mreq->m_pkthdr.len = m_length(mreq, NULL);
415 * Prepend a record marker containing the packet length.
417 M_PREPEND(mreq, sizeof(uint32_t), M_WAIT);
418 *mtod(mreq, uint32_t *) =
419 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
422 mtx_lock(&ct->ct_lock);
424 * Check to see if the other end has already started to close down
425 * the connection. The upcall will have set ct_error.re_status
426 * to RPC_CANTRECV if this is the case.
427 * If the other end starts to close down the connection after this
428 * point, it will be detected later when cr_error is checked,
429 * since the request is in the ct_pending queue.
431 if (ct->ct_error.re_status == RPC_CANTRECV) {
432 if (errp != &ct->ct_error) {
433 errp->re_errno = ct->ct_error.re_errno;
434 errp->re_status = RPC_CANTRECV;
439 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
440 mtx_unlock(&ct->ct_lock);
443 * sosend consumes mreq.
445 error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
447 if (error == EMSGSIZE) {
448 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
449 sbwait(&ct->ct_socket->so_snd);
450 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
451 AUTH_VALIDATE(auth, xid, NULL, NULL);
452 mtx_lock(&ct->ct_lock);
453 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
457 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
458 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
459 reply_msg.acpted_rply.ar_verf.oa_length = 0;
460 reply_msg.acpted_rply.ar_results.where = NULL;
461 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
463 mtx_lock(&ct->ct_lock);
465 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
466 errp->re_errno = error;
467 errp->re_status = stat = RPC_CANTSEND;
472 * Check to see if we got an upcall while waiting for the
473 * lock. In both these cases, the request has been removed
474 * from ct->ct_pending.
477 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
478 errp->re_errno = cr->cr_error;
479 errp->re_status = stat = RPC_CANTRECV;
483 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
488 * Hack to provide rpc-based message passing
490 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
491 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
492 errp->re_status = stat = RPC_TIMEDOUT;
496 error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
499 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
503 * The sleep returned an error so our request is still
504 * on the list. Turn the error code into an
505 * appropriate client status.
507 errp->re_errno = error;
519 errp->re_status = stat;
523 * We were woken up by the upcall. If the
524 * upcall had a receive error, report that,
525 * otherwise we have a reply.
528 errp->re_errno = cr->cr_error;
529 errp->re_status = stat = RPC_CANTRECV;
536 * Now decode and validate the response. We need to drop the
537 * lock since xdr_replymsg may end up sleeping in malloc.
539 mtx_unlock(&ct->ct_lock);
541 if (ext && ext->rc_feedback)
542 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
544 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
545 ok = xdr_replymsg(&xdrs, &reply_msg);
549 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
550 (reply_msg.acpted_rply.ar_stat == SUCCESS))
551 errp->re_status = stat = RPC_SUCCESS;
553 stat = _seterr_reply(&reply_msg, errp);
555 if (stat == RPC_SUCCESS) {
556 results = xdrmbuf_getall(&xdrs);
557 if (!AUTH_VALIDATE(auth, xid,
558 &reply_msg.acpted_rply.ar_verf,
560 errp->re_status = stat = RPC_AUTHERROR;
561 errp->re_why = AUTH_INVALIDRESP;
564 ("auth validated but no result"));
567 } /* end successful completion */
569 * If unsuccesful AND error is an authentication error
570 * then refresh credentials and try again, else break
572 else if (stat == RPC_AUTHERROR)
573 /* maybe our credentials need to be refreshed ... */
574 if (nrefreshes > 0 &&
575 AUTH_REFRESH(auth, &reply_msg)) {
578 mtx_lock(&ct->ct_lock);
581 /* end of unsuccessful completion */
582 } /* end of valid reply message */
584 errp->re_status = stat = RPC_CANTDECODERES;
587 mtx_lock(&ct->ct_lock);
589 mtx_assert(&ct->ct_lock, MA_OWNED);
591 KASSERT(stat != RPC_SUCCESS || *resultsp,
592 ("RPC_SUCCESS without reply"));
597 m_freem(cr->cr_mrep);
603 mtx_unlock(&ct->ct_lock);
605 if (auth && stat != RPC_SUCCESS)
606 AUTH_VALIDATE(auth, xid, NULL, NULL);
614 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
616 struct ct_data *ct = (struct ct_data *) cl->cl_private;
618 *errp = ct->ct_error;
622 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
627 xdrs.x_op = XDR_FREE;
628 dummy = (*xdr_res)(&xdrs, res_ptr);
635 clnt_vc_abort(CLIENT *cl)
640 clnt_vc_control(CLIENT *cl, u_int request, void *info)
642 struct ct_data *ct = (struct ct_data *)cl->cl_private;
645 mtx_lock(&ct->ct_lock);
649 ct->ct_closeit = TRUE;
650 mtx_unlock(&ct->ct_lock);
652 case CLSET_FD_NCLOSE:
653 ct->ct_closeit = FALSE;
654 mtx_unlock(&ct->ct_lock);
660 /* for other requests which use info */
662 mtx_unlock(&ct->ct_lock);
667 if (time_not_ok((struct timeval *)info)) {
668 mtx_unlock(&ct->ct_lock);
671 ct->ct_wait = *(struct timeval *)infop;
674 *(struct timeval *)infop = ct->ct_wait;
676 case CLGET_SERVER_ADDR:
677 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
681 * Slightly different semantics to userland - we use
682 * sockaddr instead of netbuf.
684 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
686 case CLSET_SVC_ADDR: /* set to new address */
687 mtx_unlock(&ct->ct_lock);
690 *(uint32_t *)info = ct->ct_xid;
693 /* This will set the xid of the NEXT call */
694 /* decrement by 1 as clnt_vc_call() increments once */
695 ct->ct_xid = *(uint32_t *)info - 1;
699 * This RELIES on the information that, in the call body,
700 * the version number field is the fifth field from the
701 * begining of the RPC header. MUST be changed if the
702 * call_struct is changed
705 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
706 4 * BYTES_PER_XDR_UNIT));
710 *(uint32_t *)(void *)(ct->ct_mcallc +
711 4 * BYTES_PER_XDR_UNIT) =
712 htonl(*(uint32_t *)info);
717 * This RELIES on the information that, in the call body,
718 * the program number field is the fourth field from the
719 * begining of the RPC header. MUST be changed if the
720 * call_struct is changed
723 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
724 3 * BYTES_PER_XDR_UNIT));
728 *(uint32_t *)(void *)(ct->ct_mcallc +
729 3 * BYTES_PER_XDR_UNIT) =
730 htonl(*(uint32_t *)info);
734 ct->ct_waitchan = (const char *)info;
738 *(const char **) info = ct->ct_waitchan;
741 case CLSET_INTERRUPTIBLE:
743 ct->ct_waitflag = PCATCH | PBDRY;
748 case CLGET_INTERRUPTIBLE:
750 *(int *) info = TRUE;
752 *(int *) info = FALSE;
756 mtx_unlock(&ct->ct_lock);
760 mtx_unlock(&ct->ct_lock);
765 clnt_vc_close(CLIENT *cl)
767 struct ct_data *ct = (struct ct_data *) cl->cl_private;
768 struct ct_request *cr;
770 mtx_lock(&ct->ct_lock);
773 mtx_unlock(&ct->ct_lock);
777 if (ct->ct_closing) {
778 while (ct->ct_closing)
779 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
780 KASSERT(ct->ct_closed, ("client should be closed"));
781 mtx_unlock(&ct->ct_lock);
786 ct->ct_closing = TRUE;
787 mtx_unlock(&ct->ct_lock);
789 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
790 soupcall_clear(ct->ct_socket, SO_RCV);
791 clnt_vc_upcallsdone(ct);
792 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
795 * Abort any pending requests and wait until everyone
796 * has finished with clnt_vc_call.
798 mtx_lock(&ct->ct_lock);
799 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
801 cr->cr_error = ESHUTDOWN;
805 while (ct->ct_threads)
806 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
809 ct->ct_closing = FALSE;
810 ct->ct_closed = TRUE;
811 mtx_unlock(&ct->ct_lock);
816 clnt_vc_destroy(CLIENT *cl)
818 struct ct_data *ct = (struct ct_data *) cl->cl_private;
819 struct socket *so = NULL;
823 mtx_lock(&ct->ct_lock);
826 if (ct->ct_closeit) {
831 mtx_unlock(&ct->ct_lock);
833 mtx_destroy(&ct->ct_lock);
835 soshutdown(so, SHUT_WR);
838 mem_free(ct, sizeof(struct ct_data));
839 mem_free(cl, sizeof(CLIENT));
843 * Make sure that the time is not garbage. -1 value is disallowed.
844 * Note this is different from time_not_ok in clnt_dg.c
847 time_not_ok(struct timeval *t)
849 return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
850 t->tv_usec <= -1 || t->tv_usec > 1000000);
854 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
856 struct ct_data *ct = (struct ct_data *) arg;
859 struct ct_request *cr;
860 int error, rcvflag, foundreq;
861 uint32_t xid, header;
865 uio.uio_td = curthread;
868 * If ct_record_resid is zero, we are waiting for a
871 if (ct->ct_record_resid == 0) {
874 * Make sure there is either a whole record
875 * mark in the buffer or there is some other
879 if (so->so_rcv.sb_cc >= sizeof(uint32_t)
880 || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
887 SOCKBUF_UNLOCK(&so->so_rcv);
888 uio.uio_resid = sizeof(uint32_t);
890 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
891 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
892 SOCKBUF_LOCK(&so->so_rcv);
894 if (error == EWOULDBLOCK)
898 * If there was an error, wake up all pending
901 if (error || uio.uio_resid > 0) {
903 mtx_lock(&ct->ct_lock);
906 * We must have got EOF trying
907 * to read from the stream.
911 ct->ct_error.re_status = RPC_CANTRECV;
912 ct->ct_error.re_errno = error;
913 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
914 cr->cr_error = error;
917 mtx_unlock(&ct->ct_lock);
920 m_copydata(m, 0, sizeof(uint32_t), (char *)&header);
921 header = ntohl(header);
922 ct->ct_record = NULL;
923 ct->ct_record_resid = header & 0x7fffffff;
924 ct->ct_record_eor = ((header & 0x80000000) != 0);
928 * Wait until the socket has the whole record
932 if (so->so_rcv.sb_cc >= ct->ct_record_resid
933 || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
941 * We have the record mark. Read as much as
942 * the socket has buffered up to the end of
945 SOCKBUF_UNLOCK(&so->so_rcv);
946 uio.uio_resid = ct->ct_record_resid;
948 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
949 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
950 SOCKBUF_LOCK(&so->so_rcv);
952 if (error == EWOULDBLOCK)
955 if (error || uio.uio_resid == ct->ct_record_resid)
959 * If we have part of the record already,
960 * chain this bit onto the end.
963 m_last(ct->ct_record)->m_next = m;
967 ct->ct_record_resid = uio.uio_resid;
970 * If we have the entire record, see if we can
971 * match it to a request.
973 if (ct->ct_record_resid == 0
974 && ct->ct_record_eor) {
976 * The XID is in the first uint32_t of
979 if (ct->ct_record->m_len < sizeof(xid) &&
980 m_length(ct->ct_record, NULL) <
982 m_freem(ct->ct_record);
985 m_copydata(ct->ct_record, 0, sizeof(xid),
989 mtx_lock(&ct->ct_lock);
991 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
992 if (cr->cr_xid == xid) {
999 * that we will ignore
1004 cr->cr_mrep = ct->ct_record;
1011 mtx_unlock(&ct->ct_lock);
1014 m_freem(ct->ct_record);
1015 ct->ct_record = NULL;
1019 ct->ct_upcallrefs--;
1020 if (ct->ct_upcallrefs < 0)
1021 panic("rpcvc upcall refcnt");
1022 if (ct->ct_upcallrefs == 0)
1023 wakeup(&ct->ct_upcallrefs);
1028 * Wait for all upcalls in progress to complete.
1031 clnt_vc_upcallsdone(struct ct_data *ct)
1034 SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
1036 while (ct->ct_upcallrefs > 0)
1037 (void) msleep(&ct->ct_upcallrefs,
1038 SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);