1 /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";
36 static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
44 * Copyright (C) 1984, Sun Microsystems, Inc.
46 * TCP based RPC supports 'batched calls'.
47 * A sequence of calls may be batched-up in a send buffer. The rpc call
48 * return immediately to the client even though the call was not necessarily
49 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
50 * the rpc timeout value is zero (see clnt.h, rpc).
52 * Clients should NOT casually batch calls that in fact return results; that is,
53 * the server side should be aware that a call is batched and not produce any
54 * return message. Batched calls that produce many result messages can
55 * deadlock (netlock) the client and the server....
57 * Now go hang yourself.
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/mutex.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
73 #include <sys/syslog.h>
79 #include <netinet/tcp.h>
82 #include <rpc/rpc_com.h>
87 struct cmsgcred cmcred;
90 static enum clnt_stat clnt_vc_call(CLIENT *, struct rpc_callextra *,
91 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
92 static void clnt_vc_geterr(CLIENT *, struct rpc_err *);
93 static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *);
94 static void clnt_vc_abort(CLIENT *);
95 static bool_t clnt_vc_control(CLIENT *, u_int, void *);
96 static void clnt_vc_close(CLIENT *);
97 static void clnt_vc_destroy(CLIENT *);
98 static bool_t time_not_ok(struct timeval *);
99 static int clnt_vc_soupcall(struct socket *so, void *arg, int waitflag);
101 static struct clnt_ops clnt_vc_ops = {
102 .cl_call = clnt_vc_call,
103 .cl_abort = clnt_vc_abort,
104 .cl_geterr = clnt_vc_geterr,
105 .cl_freeres = clnt_vc_freeres,
106 .cl_close = clnt_vc_close,
107 .cl_destroy = clnt_vc_destroy,
108 .cl_control = clnt_vc_control
111 static void clnt_vc_upcallsdone(struct ct_data *);
113 static int fake_wchan;
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);
278 mtx_destroy(&ct->ct_lock);
279 mem_free(ct, sizeof (struct ct_data));
280 mem_free(cl, sizeof (CLIENT));
282 return ((CLIENT *)NULL);
285 static enum clnt_stat
287 CLIENT *cl, /* client handle */
288 struct rpc_callextra *ext, /* call metadata */
289 rpcproc_t proc, /* procedure number */
290 struct mbuf *args, /* pointer to args */
291 struct mbuf **resultsp, /* pointer to results */
292 struct timeval utimeout)
294 struct ct_data *ct = (struct ct_data *) cl->cl_private;
296 struct rpc_err *errp;
299 struct rpc_msg reply_msg;
301 int nrefreshes = 2; /* number of times to refresh cred */
302 struct timeval timeout;
304 struct mbuf *mreq = NULL, *results;
305 struct ct_request *cr;
308 cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
310 mtx_lock(&ct->ct_lock);
312 if (ct->ct_closing || ct->ct_closed) {
313 mtx_unlock(&ct->ct_lock);
315 return (RPC_CANTSEND);
324 errp = &ct->ct_error;
330 if (ct->ct_wait.tv_usec == -1) {
331 timeout = utimeout; /* use supplied timeout */
333 timeout = ct->ct_wait; /* use default timeout */
337 * After 15sec of looping, allow it to return RPC_CANTSEND, which will
338 * cause the clnt_reconnect layer to create a new TCP connection.
342 mtx_assert(&ct->ct_lock, MA_OWNED);
343 if (ct->ct_closing || ct->ct_closed) {
346 mtx_unlock(&ct->ct_lock);
348 return (RPC_CANTSEND);
354 mtx_unlock(&ct->ct_lock);
357 * Leave space to pre-pend the record mark.
359 mreq = m_gethdr(M_WAITOK, MT_DATA);
360 mreq->m_data += sizeof(uint32_t);
361 KASSERT(ct->ct_mpos + sizeof(uint32_t) <= MHLEN,
362 ("RPC header too big"));
363 bcopy(ct->ct_mcallc, mreq->m_data, ct->ct_mpos);
364 mreq->m_len = ct->ct_mpos;
367 * The XID is the first thing in the request.
369 *mtod(mreq, uint32_t *) = htonl(xid);
371 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
373 errp->re_status = stat = RPC_SUCCESS;
375 if ((! XDR_PUTINT32(&xdrs, &proc)) ||
376 (! AUTH_MARSHALL(auth, xid, &xdrs,
377 m_copym(args, 0, M_COPYALL, M_WAITOK)))) {
378 errp->re_status = stat = RPC_CANTENCODEARGS;
379 mtx_lock(&ct->ct_lock);
382 mreq->m_pkthdr.len = m_length(mreq, NULL);
385 * Prepend a record marker containing the packet length.
387 M_PREPEND(mreq, sizeof(uint32_t), M_WAITOK);
388 *mtod(mreq, uint32_t *) =
389 htonl(0x80000000 | (mreq->m_pkthdr.len - sizeof(uint32_t)));
392 mtx_lock(&ct->ct_lock);
394 * Check to see if the other end has already started to close down
395 * the connection. The upcall will have set ct_error.re_status
396 * to RPC_CANTRECV if this is the case.
397 * If the other end starts to close down the connection after this
398 * point, it will be detected later when cr_error is checked,
399 * since the request is in the ct_pending queue.
401 if (ct->ct_error.re_status == RPC_CANTRECV) {
402 if (errp != &ct->ct_error) {
403 errp->re_errno = ct->ct_error.re_errno;
404 errp->re_status = RPC_CANTRECV;
409 TAILQ_INSERT_TAIL(&ct->ct_pending, cr, cr_link);
410 mtx_unlock(&ct->ct_lock);
413 * sosend consumes mreq.
415 error = sosend(ct->ct_socket, NULL, NULL, mreq, NULL, 0, curthread);
417 if (error == EMSGSIZE || (error == ERESTART &&
418 (ct->ct_waitflag & PCATCH) == 0 && trycnt-- > 0)) {
419 SOCKBUF_LOCK(&ct->ct_socket->so_snd);
420 sbwait(&ct->ct_socket->so_snd);
421 SOCKBUF_UNLOCK(&ct->ct_socket->so_snd);
422 AUTH_VALIDATE(auth, xid, NULL, NULL);
423 mtx_lock(&ct->ct_lock);
424 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
425 /* Sleep for 1 clock tick before trying the sosend() again. */
426 msleep(&fake_wchan, &ct->ct_lock, 0, "rpclpsnd", 1);
430 reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
431 reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
432 reply_msg.acpted_rply.ar_verf.oa_length = 0;
433 reply_msg.acpted_rply.ar_results.where = NULL;
434 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
436 mtx_lock(&ct->ct_lock);
438 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
439 errp->re_errno = error;
440 errp->re_status = stat = RPC_CANTSEND;
445 * Check to see if we got an upcall while waiting for the
446 * lock. In both these cases, the request has been removed
447 * from ct->ct_pending.
450 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
451 errp->re_errno = cr->cr_error;
452 errp->re_status = stat = RPC_CANTRECV;
456 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
461 * Hack to provide rpc-based message passing
463 if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
464 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
465 errp->re_status = stat = RPC_TIMEDOUT;
469 error = msleep(cr, &ct->ct_lock, ct->ct_waitflag, ct->ct_waitchan,
472 TAILQ_REMOVE(&ct->ct_pending, cr, cr_link);
476 * The sleep returned an error so our request is still
477 * on the list. Turn the error code into an
478 * appropriate client status.
480 errp->re_errno = error;
491 errp->re_status = stat;
495 * We were woken up by the upcall. If the
496 * upcall had a receive error, report that,
497 * otherwise we have a reply.
500 errp->re_errno = cr->cr_error;
501 errp->re_status = stat = RPC_CANTRECV;
508 * Now decode and validate the response. We need to drop the
509 * lock since xdr_replymsg may end up sleeping in malloc.
511 mtx_unlock(&ct->ct_lock);
513 if (ext && ext->rc_feedback)
514 ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
516 xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
517 ok = xdr_replymsg(&xdrs, &reply_msg);
521 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
522 (reply_msg.acpted_rply.ar_stat == SUCCESS))
523 errp->re_status = stat = RPC_SUCCESS;
525 stat = _seterr_reply(&reply_msg, errp);
527 if (stat == RPC_SUCCESS) {
528 results = xdrmbuf_getall(&xdrs);
529 if (!AUTH_VALIDATE(auth, xid,
530 &reply_msg.acpted_rply.ar_verf,
532 errp->re_status = stat = RPC_AUTHERROR;
533 errp->re_why = AUTH_INVALIDRESP;
536 ("auth validated but no result"));
539 } /* end successful completion */
541 * If unsuccessful AND error is an authentication error
542 * then refresh credentials and try again, else break
544 else if (stat == RPC_AUTHERROR)
545 /* maybe our credentials need to be refreshed ... */
546 if (nrefreshes > 0 &&
547 AUTH_REFRESH(auth, &reply_msg)) {
550 mtx_lock(&ct->ct_lock);
553 /* end of unsuccessful completion */
554 } /* end of valid reply message */
556 errp->re_status = stat = RPC_CANTDECODERES;
559 mtx_lock(&ct->ct_lock);
561 mtx_assert(&ct->ct_lock, MA_OWNED);
563 KASSERT(stat != RPC_SUCCESS || *resultsp,
564 ("RPC_SUCCESS without reply"));
569 m_freem(cr->cr_mrep);
575 mtx_unlock(&ct->ct_lock);
577 if (auth && stat != RPC_SUCCESS)
578 AUTH_VALIDATE(auth, xid, NULL, NULL);
586 clnt_vc_geterr(CLIENT *cl, struct rpc_err *errp)
588 struct ct_data *ct = (struct ct_data *) cl->cl_private;
590 *errp = ct->ct_error;
594 clnt_vc_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
599 xdrs.x_op = XDR_FREE;
600 dummy = (*xdr_res)(&xdrs, res_ptr);
607 clnt_vc_abort(CLIENT *cl)
612 clnt_vc_control(CLIENT *cl, u_int request, void *info)
614 struct ct_data *ct = (struct ct_data *)cl->cl_private;
618 mtx_lock(&ct->ct_lock);
622 ct->ct_closeit = TRUE;
623 mtx_unlock(&ct->ct_lock);
625 case CLSET_FD_NCLOSE:
626 ct->ct_closeit = FALSE;
627 mtx_unlock(&ct->ct_lock);
633 /* for other requests which use info */
635 mtx_unlock(&ct->ct_lock);
640 if (time_not_ok((struct timeval *)info)) {
641 mtx_unlock(&ct->ct_lock);
644 ct->ct_wait = *(struct timeval *)infop;
647 *(struct timeval *)infop = ct->ct_wait;
649 case CLGET_SERVER_ADDR:
650 (void) memcpy(info, &ct->ct_addr, (size_t)ct->ct_addr.ss_len);
654 * Slightly different semantics to userland - we use
655 * sockaddr instead of netbuf.
657 memcpy(info, &ct->ct_addr, ct->ct_addr.ss_len);
659 case CLSET_SVC_ADDR: /* set to new address */
660 mtx_unlock(&ct->ct_lock);
663 *(uint32_t *)info = ct->ct_xid;
666 /* This will set the xid of the NEXT call */
667 /* decrement by 1 as clnt_vc_call() increments once */
668 ct->ct_xid = *(uint32_t *)info - 1;
672 * This RELIES on the information that, in the call body,
673 * the version number field is the fifth field from the
674 * beginning of the RPC header. MUST be changed if the
675 * call_struct is changed
678 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
679 4 * BYTES_PER_XDR_UNIT));
683 *(uint32_t *)(void *)(ct->ct_mcallc +
684 4 * BYTES_PER_XDR_UNIT) =
685 htonl(*(uint32_t *)info);
690 * This RELIES on the information that, in the call body,
691 * the program number field is the fourth field from the
692 * beginning of the RPC header. MUST be changed if the
693 * call_struct is changed
696 ntohl(*(uint32_t *)(void *)(ct->ct_mcallc +
697 3 * BYTES_PER_XDR_UNIT));
701 *(uint32_t *)(void *)(ct->ct_mcallc +
702 3 * BYTES_PER_XDR_UNIT) =
703 htonl(*(uint32_t *)info);
707 ct->ct_waitchan = (const char *)info;
711 *(const char **) info = ct->ct_waitchan;
714 case CLSET_INTERRUPTIBLE:
716 ct->ct_waitflag = PCATCH;
721 case CLGET_INTERRUPTIBLE:
723 *(int *) info = TRUE;
725 *(int *) info = FALSE;
728 case CLSET_BACKCHANNEL:
729 xprt = (SVCXPRT *)info;
730 if (ct->ct_backchannelxprt == NULL) {
732 ct->ct_backchannelxprt = xprt;
737 mtx_unlock(&ct->ct_lock);
741 mtx_unlock(&ct->ct_lock);
746 clnt_vc_close(CLIENT *cl)
748 struct ct_data *ct = (struct ct_data *) cl->cl_private;
749 struct ct_request *cr;
751 mtx_lock(&ct->ct_lock);
754 mtx_unlock(&ct->ct_lock);
758 if (ct->ct_closing) {
759 while (ct->ct_closing)
760 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
761 KASSERT(ct->ct_closed, ("client should be closed"));
762 mtx_unlock(&ct->ct_lock);
767 ct->ct_closing = TRUE;
768 mtx_unlock(&ct->ct_lock);
770 SOCKBUF_LOCK(&ct->ct_socket->so_rcv);
771 soupcall_clear(ct->ct_socket, SO_RCV);
772 clnt_vc_upcallsdone(ct);
773 SOCKBUF_UNLOCK(&ct->ct_socket->so_rcv);
776 * Abort any pending requests and wait until everyone
777 * has finished with clnt_vc_call.
779 mtx_lock(&ct->ct_lock);
780 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
782 cr->cr_error = ESHUTDOWN;
786 while (ct->ct_threads)
787 msleep(ct, &ct->ct_lock, 0, "rpcclose", 0);
790 ct->ct_closing = FALSE;
791 ct->ct_closed = TRUE;
792 mtx_unlock(&ct->ct_lock);
797 clnt_vc_destroy(CLIENT *cl)
799 struct ct_data *ct = (struct ct_data *) cl->cl_private;
800 struct socket *so = NULL;
805 mtx_lock(&ct->ct_lock);
806 xprt = ct->ct_backchannelxprt;
807 ct->ct_backchannelxprt = NULL;
809 mtx_unlock(&ct->ct_lock); /* To avoid a LOR. */
810 sx_xlock(&xprt->xp_lock);
811 mtx_lock(&ct->ct_lock);
813 sx_xunlock(&xprt->xp_lock);
817 if (ct->ct_closeit) {
822 mtx_unlock(&ct->ct_lock);
824 mtx_destroy(&ct->ct_lock);
826 soshutdown(so, SHUT_WR);
829 mem_free(ct, sizeof(struct ct_data));
830 if (cl->cl_netid && cl->cl_netid[0])
831 mem_free(cl->cl_netid, strlen(cl->cl_netid) +1);
832 if (cl->cl_tp && cl->cl_tp[0])
833 mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
834 mem_free(cl, sizeof(CLIENT));
838 * Make sure that the time is not garbage. -1 value is disallowed.
839 * Note this is different from time_not_ok in clnt_dg.c
842 time_not_ok(struct timeval *t)
844 return (t->tv_sec <= -1 || t->tv_sec > 100000000 ||
845 t->tv_usec <= -1 || t->tv_usec > 1000000);
849 clnt_vc_soupcall(struct socket *so, void *arg, int waitflag)
851 struct ct_data *ct = (struct ct_data *) arg;
854 struct ct_request *cr;
855 int error, rcvflag, foundreq;
856 uint32_t xid_plus_direction[2], header;
861 CTASSERT(sizeof(xid_plus_direction) == 2 * sizeof(uint32_t));
863 uio.uio_td = curthread;
866 * If ct_record_resid is zero, we are waiting for a
869 if (ct->ct_record_resid == 0) {
872 * Make sure there is either a whole record
873 * mark in the buffer or there is some other
877 if (sbavail(&so->so_rcv) >= sizeof(uint32_t)
878 || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
885 SOCKBUF_UNLOCK(&so->so_rcv);
886 uio.uio_resid = sizeof(uint32_t);
888 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
889 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
890 SOCKBUF_LOCK(&so->so_rcv);
892 if (error == EWOULDBLOCK)
896 * If there was an error, wake up all pending
899 if (error || uio.uio_resid > 0) {
901 mtx_lock(&ct->ct_lock);
904 * We must have got EOF trying
905 * to read from the stream.
909 ct->ct_error.re_status = RPC_CANTRECV;
910 ct->ct_error.re_errno = error;
911 TAILQ_FOREACH(cr, &ct->ct_pending, cr_link) {
912 cr->cr_error = error;
915 mtx_unlock(&ct->ct_lock);
918 m_copydata(m, 0, sizeof(uint32_t), (char *)&header);
919 header = ntohl(header);
920 ct->ct_record = NULL;
921 ct->ct_record_resid = header & 0x7fffffff;
922 ct->ct_record_eor = ((header & 0x80000000) != 0);
926 * Wait until the socket has the whole record
930 if (sbavail(&so->so_rcv) >= ct->ct_record_resid
931 || (so->so_rcv.sb_state & SBS_CANTRCVMORE)
939 * We have the record mark. Read as much as
940 * the socket has buffered up to the end of
943 SOCKBUF_UNLOCK(&so->so_rcv);
944 uio.uio_resid = ct->ct_record_resid;
946 rcvflag = MSG_DONTWAIT | MSG_SOCALLBCK;
947 error = soreceive(so, NULL, &uio, &m, NULL, &rcvflag);
948 SOCKBUF_LOCK(&so->so_rcv);
950 if (error == EWOULDBLOCK)
953 if (error || uio.uio_resid == ct->ct_record_resid)
957 * If we have part of the record already,
958 * chain this bit onto the end.
961 m_last(ct->ct_record)->m_next = m;
965 ct->ct_record_resid = uio.uio_resid;
968 * If we have the entire record, see if we can
969 * match it to a request.
971 if (ct->ct_record_resid == 0
972 && ct->ct_record_eor) {
974 * The XID is in the first uint32_t of
975 * the reply and the message direction
978 if (ct->ct_record->m_len <
979 sizeof(xid_plus_direction) &&
980 m_length(ct->ct_record, NULL) <
981 sizeof(xid_plus_direction)) {
982 m_freem(ct->ct_record);
985 m_copydata(ct->ct_record, 0,
986 sizeof(xid_plus_direction),
987 (char *)xid_plus_direction);
988 xid_plus_direction[0] =
989 ntohl(xid_plus_direction[0]);
990 xid_plus_direction[1] =
991 ntohl(xid_plus_direction[1]);
992 /* Check message direction. */
993 if (xid_plus_direction[1] == CALL) {
994 /* This is a backchannel request. */
995 mtx_lock(&ct->ct_lock);
996 xprt = ct->ct_backchannelxprt;
998 mtx_unlock(&ct->ct_lock);
999 /* Just throw it away. */
1000 m_freem(ct->ct_record);
1001 ct->ct_record = NULL;
1003 cd = (struct cf_conn *)
1007 * The requests are chained
1008 * in the m_nextpkt list.
1010 while (m2 != NULL &&
1011 m2->m_nextpkt != NULL)
1012 /* Find end of list. */
1020 ct->ct_record->m_nextpkt =
1022 ct->ct_record = NULL;
1024 mtx_unlock(&ct->ct_lock);
1027 mtx_lock(&ct->ct_lock);
1029 TAILQ_FOREACH(cr, &ct->ct_pending,
1032 xid_plus_direction[0]) {
1038 * the XID to zero so
1039 * that we will ignore
1052 mtx_unlock(&ct->ct_lock);
1055 m_freem(ct->ct_record);
1056 ct->ct_record = NULL;
1061 ct->ct_upcallrefs--;
1062 if (ct->ct_upcallrefs < 0)
1063 panic("rpcvc upcall refcnt");
1064 if (ct->ct_upcallrefs == 0)
1065 wakeup(&ct->ct_upcallrefs);
1070 * Wait for all upcalls in progress to complete.
1073 clnt_vc_upcallsdone(struct ct_data *ct)
1076 SOCKBUF_LOCK_ASSERT(&ct->ct_socket->so_rcv);
1078 while (ct->ct_upcallrefs > 0)
1079 (void) msleep(&ct->ct_upcallrefs,
1080 SOCKBUF_MTX(&ct->ct_socket->so_rcv), 0, "rpcvcup", 0);