1 /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";
34 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
42 * Copyright (C) 1984, Sun Microsystems, Inc.
44 * TCP based RPC supports 'batched calls'.
45 * A sequence of calls may be batched-up in a send buffer. The rpc call
46 * return immediately to the client even though the call was not necessarily
47 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
48 * the rpc timeout value is zero (see clnt.h, rpc).
50 * Clients should NOT casually batch calls that in fact return results; that is,
51 * the server side should be aware that a call is batched and not produce any
52 * return message. Batched calls that produce many result messages can
53 * deadlock (netlock) the client and the server....
55 * Now go hang yourself.
58 #include <sys/param.h>
59 #include <sys/systm.h>
61 #include <sys/malloc.h>
63 #include <sys/mutex.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
70 #include <sys/syslog.h>
76 #include <netinet/tcp.h>
79 #include <rpc/rpc_com.h>
84 struct cmsgcred cmcred;
87 static enum clnt_stat clnt_vc_call(CLIENT *, struct rpc_callextra *,
88 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
89 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
90 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
91 static void clnt_vc_abort(CLIENT *);
92 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
93 static void clnt_vc_close(CLIENT *);
94 static void clnt_vc_destroy(CLIENT *);
95 static bool_t time_not_ok(struct timeval *);
96 static int clnt_vc_soupcall(struct socket *so, void *arg, int waitflag);
98 static struct clnt_ops clnt_vc_ops = {
99 .cl_call = clnt_vc_call,
100 .cl_abort = clnt_vc_abort,
101 .cl_geterr = clnt_vc_geterr,
102 .cl_freeres = clnt_vc_freeres,
103 .cl_close = clnt_vc_close,
104 .cl_destroy = clnt_vc_destroy,
105 .cl_control = clnt_vc_control
108 static void clnt_vc_upcallsdone(struct ct_data *);
110 static const char clnt_vc_errstr[] = "%s : %s";
111 static const char clnt_vc_str[] = "clnt_vc_create";
112 static const char clnt_read_vc_str[] = "read_vc";
113 static const char __no_mem_str[] = "out of memory";
116 * Create a client handle for a connection.
117 * Default options are set, which the user can change using clnt_control()'s.
118 * The rpc/vc package does buffering similar to stdio, so the client
119 * must pick send and receive buffer sizes, 0 => use the default.
120 * NB: fd is copied into a private area.
121 * NB: The rpch->cl_auth is set null authentication. Caller may wish to
122 * set this something more useful.
124 * fd should be an open socket
128 struct socket *so, /* open file descriptor */
129 struct sockaddr *raddr, /* servers address */
130 const rpcprog_t prog, /* program number */
131 const rpcvers_t vers, /* version number */
132 size_t sendsz, /* buffer recv size */
133 size_t recvsz, /* buffer send size */
134 int intrflag) /* interruptible */
136 CLIENT *cl; /* client handle */
137 struct ct_data *ct = NULL; /* client handle */
139 struct rpc_msg call_msg;
140 static uint32_t disrupt;
141 struct __rpc_sockinfo si;
143 int error, interrupted, one = 1, sleep_flag;
147 disrupt = (uint32_t)(long)raddr;
149 cl = (CLIENT *)mem_alloc(sizeof (*cl));
150 ct = (struct ct_data *)mem_alloc(sizeof (*ct));
152 mtx_init(&ct->ct_lock, "ct->ct_lock", NULL, MTX_DEF);
154 ct->ct_closing = FALSE;
155 ct->ct_closed = FALSE;
156 ct->ct_upcallrefs = 0;
158 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
159 error = soconnect(so, raddr, curthread);
164 sleep_flag |= PCATCH;
165 while ((so->so_state & SS_ISCONNECTING)
166 && so->so_error == 0) {
167 error = msleep(&so->so_timeo, SOCK_MTX(so),
168 sleep_flag, "connec", 0);
170 if (error == EINTR || error == ERESTART)
176 error = so->so_error;
182 so->so_state &= ~SS_ISCONNECTING;
183 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
184 rpc_createerr.cf_error.re_errno = error;
189 if (!__rpc_socket2sockinfo(so, &si)) {
193 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
194 bzero(&sopt, sizeof(sopt));
195 sopt.sopt_dir = SOPT_SET;
196 sopt.sopt_level = SOL_SOCKET;
197 sopt.sopt_name = SO_KEEPALIVE;
198 sopt.sopt_val = &one;
199 sopt.sopt_valsize = sizeof(one);
203 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
204 bzero(&sopt, sizeof(sopt));
205 sopt.sopt_dir = SOPT_SET;
206 sopt.sopt_level = IPPROTO_TCP;
207 sopt.sopt_name = TCP_NODELAY;
208 sopt.sopt_val = &one;
209 sopt.sopt_valsize = sizeof(one);
213 ct->ct_closeit = FALSE;
216 * Set up private data struct
219 ct->ct_wait.tv_sec = -1;
220 ct->ct_wait.tv_usec = -1;
221 memcpy(&ct->ct_addr, raddr, raddr->sa_len);
224 * Initialize call message
227 ct->ct_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
228 call_msg.rm_xid = ct->ct_xid;
229 call_msg.rm_direction = CALL;
230 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
231 call_msg.rm_call.cb_prog = (uint32_t)prog;
232 call_msg.rm_call.cb_vers = (uint32_t)vers;
235 * pre-serialize the static part of the call msg and stash it away
237 xdrmem_create(&xdrs, ct->ct_mcallc, MCALL_MSG_SIZE,
239 if (! xdr_callhdr(&xdrs, &call_msg)) {
240 if (ct->ct_closeit) {
241 soclose(ct->ct_socket);
245 ct->ct_mpos = XDR_GETPOS(&xdrs);
247 ct->ct_waitchan = "rpcrecv";
251 * Create a client handle which uses xdrrec for serialization
252 * and authnone for authentication.
254 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
255 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
256 error = soreserve(ct->ct_socket, sendsz, recvsz);
258 if (ct->ct_closeit) {
259 soclose(ct->ct_socket);
264 cl->cl_ops = &clnt_vc_ops;
266 cl->cl_auth = authnone_create();
268 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
269 soupcall_set(ct->ct_socket, SO_RCV, clnt_vc_soupcall, ct);
270 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
272 ct->ct_record = NULL;
273 ct->ct_record_resid = 0;
274 TAILQ_INIT(&ct->ct_pending);
280 mtx_destroy(&ct->ct_lock);
281 mem_free(ct, sizeof (struct ct_data));
284 mem_free(cl, sizeof (CLIENT));
286 return ((CLIENT *)NULL);
289 static enum clnt_stat
291 CLIENT *cl, /* client handle */
292 struct rpc_callextra *ext, /* call metadata */
293 rpcproc_t proc, /* procedure number */
294 struct mbuf *args, /* pointer to args */
295 struct mbuf **resultsp, /* pointer to results */
296 struct timeval utimeout)
298 struct ct_data *ct = (struct ct_data *) cl->cl_private;
300 struct rpc_err *errp;
303 struct rpc_msg reply_msg;
305 int nrefreshes = 2; /* number of times to refresh cred */
306 struct timeval timeout;
308 struct mbuf *mreq = NULL, *results;
309 struct ct_request *cr;
312 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
314 mtx_lock(&ct->ct_lock);
316 if (ct->ct_closing || ct->ct_closed) {
317 mtx_unlock(&ct->ct_lock);
319 return (RPC_CANTSEND);
328 errp = &ct->ct_error;
334 if (ct->ct_wait.tv_usec == -1) {
335 timeout = utimeout; /* use supplied timeout */
337 timeout = ct->ct_wait; /* use default timeout */
341 mtx_assert(&ct->ct_lock, MA_OWNED);
346 mtx_unlock(&ct->ct_lock);
349 * Leave space to pre-pend the record mark.
351 mreq = m_gethdr(M_WAITOK, MT_DATA);
352 mreq->m_data += sizeof(uint32_t);
353 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
354 ("RPC header too big"));
355 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
356 mreq->m_len = ct->ct_mpos;
359 * The XID is the first thing in the request.
361 *mtod(mreq, uint32_t *) = htonl(xid);
363 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
365 errp->re_status = stat = RPC_SUCCESS;
367 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
368 (! AUTH_MARSHALL(auth, xid, &xdrs,
369 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
370 errp->re_status = stat = RPC_CANTENCODEARGS;
371 mtx_lock(&ct->ct_lock);
374 mreq->m_pkthdr.len = m_length(mreq, NULL);
377 * Prepend a record marker containing the packet length.
379 M_PREPEND(mreq, sizeof(uint32_t), M_WAITOK);
380 *mtod(mreq, uint32_t *) =
381 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
384 mtx_lock(&ct->ct_lock);
386 * Check to see if the other end has already started to close down
387 * the connection. The upcall will have set ct_error.re_status
388 * to RPC_CANTRECV if this is the case.
389 * If the other end starts to close down the connection after this
390 * point, it will be detected later when cr_error is checked,
391 * since the request is in the ct_pending queue.
393 if (ct->ct_error.re_status == RPC_CANTRECV) {
394 if (errp != &ct->ct_error) {
395 errp->re_errno = ct->ct_error.re_errno;
396 errp->re_status = RPC_CANTRECV;
401 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
402 mtx_unlock(&ct->ct_lock);
405 * sosend consumes mreq.
407 error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
409 if (error == EMSGSIZE) {
410 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
411 sbwait(&ct->ct_socket->so_snd);
412 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
413 AUTH_VALIDATE(auth, xid, NULL, NULL);
414 mtx_lock(&ct->ct_lock);
415 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
419 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
420 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
421 reply_msg.acpted_rply.ar_verf.oa_length = 0;
422 reply_msg.acpted_rply.ar_results.where = NULL;
423 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
425 mtx_lock(&ct->ct_lock);
427 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
428 errp->re_errno = error;
429 errp->re_status = stat = RPC_CANTSEND;
434 * Check to see if we got an upcall while waiting for the
435 * lock. In both these cases, the request has been removed
436 * from ct->ct_pending.
439 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
440 errp->re_errno = cr->cr_error;
441 errp->re_status = stat = RPC_CANTRECV;
445 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
450 * Hack to provide rpc-based message passing
452 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
453 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
454 errp->re_status = stat = RPC_TIMEDOUT;
458 error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
461 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
465 * The sleep returned an error so our request is still
466 * on the list. Turn the error code into an
467 * appropriate client status.
469 errp->re_errno = error;
480 errp->re_status = stat;
484 * We were woken up by the upcall. If the
485 * upcall had a receive error, report that,
486 * otherwise we have a reply.
489 errp->re_errno = cr->cr_error;
490 errp->re_status = stat = RPC_CANTRECV;
497 * Now decode and validate the response. We need to drop the
498 * lock since xdr_replymsg may end up sleeping in malloc.
500 mtx_unlock(&ct->ct_lock);
502 if (ext && ext->rc_feedback)
503 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
505 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
506 ok = xdr_replymsg(&xdrs, &reply_msg);
510 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
511 (reply_msg.acpted_rply.ar_stat == SUCCESS))
512 errp->re_status = stat = RPC_SUCCESS;
514 stat = _seterr_reply(&reply_msg, errp);
516 if (stat == RPC_SUCCESS) {
517 results = xdrmbuf_getall(&xdrs);
518 if (!AUTH_VALIDATE(auth, xid,
519 &reply_msg.acpted_rply.ar_verf,
521 errp->re_status = stat = RPC_AUTHERROR;
522 errp->re_why = AUTH_INVALIDRESP;
525 ("auth validated but no result"));
528 } /* end successful completion */
530 * If unsuccesful AND error is an authentication error
531 * then refresh credentials and try again, else break
533 else if (stat == RPC_AUTHERROR)
534 /* maybe our credentials need to be refreshed ... */
535 if (nrefreshes > 0 &&
536 AUTH_REFRESH(auth, &reply_msg)) {
539 mtx_lock(&ct->ct_lock);
542 /* end of unsuccessful completion */
543 } /* end of valid reply message */
545 errp->re_status = stat = RPC_CANTDECODERES;
548 mtx_lock(&ct->ct_lock);
550 mtx_assert(&ct->ct_lock, MA_OWNED);
552 KASSERT(stat != RPC_SUCCESS || *resultsp,
553 ("RPC_SUCCESS without reply"));
558 m_freem(cr->cr_mrep);
564 mtx_unlock(&ct->ct_lock);
566 if (auth && stat != RPC_SUCCESS)
567 AUTH_VALIDATE(auth, xid, NULL, NULL);
575 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
577 struct ct_data *ct = (struct ct_data *) cl->cl_private;
579 *errp = ct->ct_error;
583 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
588 xdrs.x_op = XDR_FREE;
589 dummy = (*xdr_res)(&xdrs, res_ptr);
596 clnt_vc_abort(CLIENT *cl)
601 clnt_vc_control(CLIENT *cl, u_int request, void *info)
603 struct ct_data *ct = (struct ct_data *)cl->cl_private;
607 mtx_lock(&ct->ct_lock);
611 ct->ct_closeit = TRUE;
612 mtx_unlock(&ct->ct_lock);
614 case CLSET_FD_NCLOSE:
615 ct->ct_closeit = FALSE;
616 mtx_unlock(&ct->ct_lock);
622 /* for other requests which use info */
624 mtx_unlock(&ct->ct_lock);
629 if (time_not_ok((struct timeval *)info)) {
630 mtx_unlock(&ct->ct_lock);
633 ct->ct_wait = *(struct timeval *)infop;
636 *(struct timeval *)infop = ct->ct_wait;
638 case CLGET_SERVER_ADDR:
639 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
643 * Slightly different semantics to userland - we use
644 * sockaddr instead of netbuf.
646 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
648 case CLSET_SVC_ADDR: /* set to new address */
649 mtx_unlock(&ct->ct_lock);
652 *(uint32_t *)info = ct->ct_xid;
655 /* This will set the xid of the NEXT call */
656 /* decrement by 1 as clnt_vc_call() increments once */
657 ct->ct_xid = *(uint32_t *)info - 1;
661 * This RELIES on the information that, in the call body,
662 * the version number field is the fifth field from the
663 * begining of the RPC header. MUST be changed if the
664 * call_struct is changed
667 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
668 4 * BYTES_PER_XDR_UNIT));
672 *(uint32_t *)(void *)(ct->ct_mcallc +
673 4 * BYTES_PER_XDR_UNIT) =
674 htonl(*(uint32_t *)info);
679 * This RELIES on the information that, in the call body,
680 * the program number field is the fourth field from the
681 * begining of the RPC header. MUST be changed if the
682 * call_struct is changed
685 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
686 3 * BYTES_PER_XDR_UNIT));
690 *(uint32_t *)(void *)(ct->ct_mcallc +
691 3 * BYTES_PER_XDR_UNIT) =
692 htonl(*(uint32_t *)info);
696 ct->ct_waitchan = (const char *)info;
700 *(const char **) info = ct->ct_waitchan;
703 case CLSET_INTERRUPTIBLE:
705 ct->ct_waitflag = PCATCH;
710 case CLGET_INTERRUPTIBLE:
712 *(int *) info = TRUE;
714 *(int *) info = FALSE;
717 case CLSET_BACKCHANNEL:
718 xprt = (SVCXPRT *)info;
719 if (ct->ct_backchannelxprt == NULL) {
721 ct->ct_backchannelxprt = xprt;
726 mtx_unlock(&ct->ct_lock);
730 mtx_unlock(&ct->ct_lock);
735 clnt_vc_close(CLIENT *cl)
737 struct ct_data *ct = (struct ct_data *) cl->cl_private;
738 struct ct_request *cr;
740 mtx_lock(&ct->ct_lock);
743 mtx_unlock(&ct->ct_lock);
747 if (ct->ct_closing) {
748 while (ct->ct_closing)
749 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
750 KASSERT(ct->ct_closed, ("client should be closed"));
751 mtx_unlock(&ct->ct_lock);
756 ct->ct_closing = TRUE;
757 mtx_unlock(&ct->ct_lock);
759 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
760 soupcall_clear(ct->ct_socket, SO_RCV);
761 clnt_vc_upcallsdone(ct);
762 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
765 * Abort any pending requests and wait until everyone
766 * has finished with clnt_vc_call.
768 mtx_lock(&ct->ct_lock);
769 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
771 cr->cr_error = ESHUTDOWN;
775 while (ct->ct_threads)
776 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
779 ct->ct_closing = FALSE;
780 ct->ct_closed = TRUE;
781 mtx_unlock(&ct->ct_lock);
786 clnt_vc_destroy(CLIENT *cl)
788 struct ct_data *ct = (struct ct_data *) cl->cl_private;
789 struct socket *so = NULL;
794 mtx_lock(&ct->ct_lock);
795 xprt = ct->ct_backchannelxprt;
796 ct->ct_backchannelxprt = NULL;
798 mtx_unlock(&ct->ct_lock); /* To avoid a LOR. */
799 sx_xlock(&xprt->xp_lock);
800 mtx_lock(&ct->ct_lock);
802 xprt_unregister(xprt);
806 if (ct->ct_closeit) {
811 mtx_unlock(&ct->ct_lock);
813 sx_xunlock(&xprt->xp_lock);
817 mtx_destroy(&ct->ct_lock);
819 soshutdown(so, SHUT_WR);
822 mem_free(ct, sizeof(struct ct_data));
823 if (cl->cl_netid && cl->cl_netid[0])
824 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
825 if (cl->cl_tp && cl->cl_tp[0])
826 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
827 mem_free(cl, sizeof(CLIENT));
831 * Make sure that the time is not garbage. -1 value is disallowed.
832 * Note this is different from time_not_ok in clnt_dg.c
835 time_not_ok(struct timeval *t)
837 return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
838 t->tv_usec <= -1 || t->tv_usec > 1000000);
842 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
844 struct ct_data *ct = (struct ct_data *) arg;
847 struct ct_request *cr;
848 int error, rcvflag, foundreq;
849 uint32_t xid_plus_direction[2], header;
854 CTASSERT(sizeof(xid_plus_direction) == 2 * sizeof(uint32_t));
856 uio.uio_td = curthread;
859 * If ct_record_resid is zero, we are waiting for a
862 if (ct->ct_record_resid == 0) {
865 * Make sure there is either a whole record
866 * mark in the buffer or there is some other
870 if (so->so_rcv.sb_cc >= sizeof(uint32_t)
871 || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
878 SOCKBUF_UNLOCK(&so->so_rcv);
879 uio.uio_resid = sizeof(uint32_t);
881 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
882 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
883 SOCKBUF_LOCK(&so->so_rcv);
885 if (error == EWOULDBLOCK)
889 * If there was an error, wake up all pending
892 if (error || uio.uio_resid > 0) {
894 mtx_lock(&ct->ct_lock);
897 * We must have got EOF trying
898 * to read from the stream.
902 ct->ct_error.re_status = RPC_CANTRECV;
903 ct->ct_error.re_errno = error;
904 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
905 cr->cr_error = error;
908 mtx_unlock(&ct->ct_lock);
911 m_copydata(m, 0, sizeof(uint32_t), (char *)&header);
912 header = ntohl(header);
913 ct->ct_record = NULL;
914 ct->ct_record_resid = header & 0x7fffffff;
915 ct->ct_record_eor = ((header & 0x80000000) != 0);
919 * Wait until the socket has the whole record
923 if (so->so_rcv.sb_cc >= ct->ct_record_resid
924 || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
932 * We have the record mark. Read as much as
933 * the socket has buffered up to the end of
936 SOCKBUF_UNLOCK(&so->so_rcv);
937 uio.uio_resid = ct->ct_record_resid;
939 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
940 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
941 SOCKBUF_LOCK(&so->so_rcv);
943 if (error == EWOULDBLOCK)
946 if (error || uio.uio_resid == ct->ct_record_resid)
950 * If we have part of the record already,
951 * chain this bit onto the end.
954 m_last(ct->ct_record)->m_next = m;
958 ct->ct_record_resid = uio.uio_resid;
961 * If we have the entire record, see if we can
962 * match it to a request.
964 if (ct->ct_record_resid == 0
965 && ct->ct_record_eor) {
967 * The XID is in the first uint32_t of
968 * the reply and the message direction
971 if (ct->ct_record->m_len <
972 sizeof(xid_plus_direction) &&
973 m_length(ct->ct_record, NULL) <
974 sizeof(xid_plus_direction)) {
975 m_freem(ct->ct_record);
978 m_copydata(ct->ct_record, 0,
979 sizeof(xid_plus_direction),
980 (char *)xid_plus_direction);
981 xid_plus_direction[0] =
982 ntohl(xid_plus_direction[0]);
983 xid_plus_direction[1] =
984 ntohl(xid_plus_direction[1]);
985 /* Check message direction. */
986 if (xid_plus_direction[1] == CALL) {
987 /* This is a backchannel request. */
988 mtx_lock(&ct->ct_lock);
989 xprt = ct->ct_backchannelxprt;
991 mtx_unlock(&ct->ct_lock);
992 /* Just throw it away. */
993 m_freem(ct->ct_record);
994 ct->ct_record = NULL;
996 cd = (struct cf_conn *)
1000 * The requests are chained
1001 * in the m_nextpkt list.
1003 while (m2 != NULL &&
1004 m2->m_nextpkt != NULL)
1005 /* Find end of list. */
1013 ct->ct_record->m_nextpkt =
1015 ct->ct_record = NULL;
1017 mtx_unlock(&ct->ct_lock);
1020 mtx_lock(&ct->ct_lock);
1022 TAILQ_FOREACH(cr, &ct->ct_pending,
1025 xid_plus_direction[0]) {
1031 * the XID to zero so
1032 * that we will ignore
1045 mtx_unlock(&ct->ct_lock);
1048 m_freem(ct->ct_record);
1049 ct->ct_record = NULL;
1054 ct->ct_upcallrefs--;
1055 if (ct->ct_upcallrefs < 0)
1056 panic("rpcvc upcall refcnt");
1057 if (ct->ct_upcallrefs == 0)
1058 wakeup(&ct->ct_upcallrefs);
1063 * Wait for all upcalls in progress to complete.
1066 clnt_vc_upcallsdone(struct ct_data *ct)
1069 SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
1071 while (ct->ct_upcallrefs > 0)
1072 (void) msleep(&ct->ct_upcallrefs,
1073 SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);