2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
12 #include "afs/param.h"
14 #include <afs/param.h>
20 #ifdef RX_KERNEL_TRACE
21 #include "rx_kcommon.h"
23 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
24 #include "afs/sysincludes.h"
29 #if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV)
33 #include <net/net_globals.h>
34 #endif /* AFS_OSF_ENV */
35 #ifdef AFS_LINUX20_ENV
38 #include "netinet/in.h"
39 #if defined(AFS_SGI_ENV)
40 #include "afs/sysincludes.h"
43 #include "afs/afs_args.h"
44 #include "afs/afs_osi.h"
45 #if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
49 #include "afs/sysincludes.h"
52 #undef RXDEBUG /* turn off debugging */
55 #include "rx_kmutex.h"
56 #include "rx/rx_kernel.h"
57 #include "rx/rx_clock.h"
58 #include "rx/rx_queue.h"
60 #include "rx/rx_globals.h"
69 #endif /* AFS_OSF_ENV */
71 # include <sys/types.h>
73 # include <winsock2.h>
74 #else /* !AFS_NT40_ENV */
75 # include <sys/socket.h>
76 # include <sys/file.h>
78 # include <netinet/in.h>
79 # include <sys/stat.h>
80 # include <sys/time.h>
81 #endif /* !AFS_NT40_ENV */
87 # include "rx_clock.h"
88 # include "rx_queue.h"
90 # include "rx_globals.h"
94 /* rxdb_fileID is used to identify the lock location, along with line#. */
95 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
96 #endif /* RX_LOCKS_DB */
97 /* rxi_ReadProc -- internal version.
99 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
102 rxi_ReadProc(struct rx_call *call, char *buf,
105 struct rx_packet *cp = call->currentPacket;
106 struct rx_packet *rp;
110 /* XXXX took out clock_NewTime from here. Was it needed? */
111 requestCount = nbytes;
113 /* Free any packets from the last call to ReadvProc/WritevProc */
114 if (queue_IsNotEmpty(&call->iovq)) {
115 #ifdef RXDEBUG_PACKET
117 #endif /* RXDEBUG_PACKET */
118 rxi_FreePackets(0, &call->iovq);
122 if (call->nLeft == 0) {
123 /* Get next packet */
125 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
129 if (call->mode == RX_MODE_SENDING) {
130 rxi_FlushWrite(call);
134 if (queue_IsNotEmpty(&call->rq)) {
135 /* Check that next packet available is next in sequence */
136 rp = queue_First(&call->rq, rx_packet);
137 if (rp->header.seq == call->rnext) {
139 struct rx_connection *conn = call->conn;
141 rp->flags &= ~RX_PKTFLAG_RQ;
142 #ifdef RXDEBUG_PACKET
144 #endif /* RXDEBUG_PACKET */
146 /* RXS_CheckPacket called to undo RXS_PreparePacket's
147 * work. It may reduce the length of the packet by up
148 * to conn->maxTrailerSize, to reflect the length of the
149 * data + the header. */
151 RXS_CheckPacket(conn->securityObject, call,
153 /* Used to merely shut down the call, but now we
154 * shut down the whole connection since this may
155 * indicate an attempt to hijack it */
157 MUTEX_EXIT(&call->lock);
158 rxi_ConnectionError(conn, error);
159 MUTEX_ENTER(&conn->conn_data_lock);
160 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
161 MUTEX_EXIT(&conn->conn_data_lock);
163 MUTEX_ENTER(&call->lock);
168 cp = call->currentPacket = rp;
169 call->currentPacket->flags |= RX_PKTFLAG_CP;
170 call->curvec = 1; /* 0th vec is always header */
171 /* begin at the beginning [ more or less ], continue
172 * on until the end, then stop. */
174 (char *)cp->wirevec[1].iov_base +
175 call->conn->securityHeaderSize;
177 cp->wirevec[1].iov_len -
178 call->conn->securityHeaderSize;
180 /* Notice that this code works correctly if the data
181 * size is 0 (which it may be--no reply arguments from
182 * server, for example). This relies heavily on the
183 * fact that the code below immediately frees the packet
184 * (no yields, etc.). If it didn't, this would be a
185 * problem because a value of zero for call->nLeft
186 * normally means that there is no read packet */
187 call->nLeft = cp->length;
188 hadd32(call->bytesRcvd, cp->length);
190 /* Send a hard ack for every rxi_HardAckRate+1 packets
191 * consumed. Otherwise schedule an event to send
192 * the hard ack later on.
195 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
196 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
197 rxevent_Cancel(call->delayedAckEvent, call,
198 RX_CALL_REFCOUNT_DELAY);
199 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
201 struct clock when, now;
204 /* Delay to consolidate ack packets */
205 clock_Add(&when, &rx_hardAckDelay);
206 if (!call->delayedAckEvent
207 || clock_Gt(&call->delayedAckEvent->
209 rxevent_Cancel(call->delayedAckEvent,
211 RX_CALL_REFCOUNT_DELAY);
212 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
213 call->delayedAckEvent =
214 rxevent_PostNow(&when, &now,
215 rxi_SendDelayedAck, call,
225 * If we reach this point either we have no packets in the
226 * receive queue or the next packet in the queue is not the
227 * one we are looking for. There is nothing else for us to
228 * do but wait for another packet to arrive.
231 /* Are there ever going to be any more packets? */
232 if (call->flags & RX_CALL_RECEIVE_DONE) {
233 return requestCount - nbytes;
235 /* Wait for in-sequence packet */
236 call->flags |= RX_CALL_READER_WAIT;
238 call->startWait = clock_Sec();
239 while (call->flags & RX_CALL_READER_WAIT) {
240 #ifdef RX_ENABLE_LOCKS
241 CV_WAIT(&call->cv_rq, &call->lock);
243 osi_rxSleep(&call->rq);
246 /* cp is no longer valid since we may have given up the lock */
247 cp = call->currentPacket;
250 #ifdef RX_ENABLE_LOCKS
254 #endif /* RX_ENABLE_LOCKS */
258 /* MTUXXX this should be replaced by some error-recovery code before shipping */
259 /* yes, the following block is allowed to be the ELSE clause (or not) */
260 /* It's possible for call->nLeft to be smaller than any particular
261 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
262 * reflects the size of the buffer. We have to keep track of the
263 * number of bytes read in the length field of the packet struct. On
264 * the final portion of a received packet, it's almost certain that
265 * call->nLeft will be smaller than the final buffer. */
266 while (nbytes && cp) {
267 t = MIN((int)call->curlen, nbytes);
268 t = MIN(t, (int)call->nLeft);
269 memcpy(buf, call->curpos, t);
277 /* out of packet. Get another one. */
278 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
280 cp = call->currentPacket = (struct rx_packet *)0;
281 } else if (!call->curlen) {
282 /* need to get another struct iov */
283 if (++call->curvec >= cp->niovecs) {
284 /* current packet is exhausted, get ready for another */
285 /* don't worry about curvec and stuff, they get set somewhere else */
286 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
288 cp = call->currentPacket = (struct rx_packet *)0;
292 (char *)cp->wirevec[call->curvec].iov_base;
293 call->curlen = cp->wirevec[call->curvec].iov_len;
298 /* user buffer is full, return */
308 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
317 * Free any packets from the last call to ReadvProc/WritevProc.
318 * We do not need the lock because the receiver threads only
319 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
320 * RX_CALL_IOVEC_WAIT is always cleared before returning from
321 * ReadvProc/WritevProc.
323 if (!queue_IsEmpty(&call->iovq)) {
324 #ifdef RXDEBUG_PACKET
326 #endif /* RXDEBUG_PACKET */
327 rxi_FreePackets(0, &call->iovq);
331 * Most common case, all of the data is in the current iovec.
332 * We do not need the lock because this is the only thread that
333 * updates the curlen, curpos, nLeft fields.
335 * We are relying on nLeft being zero unless the call is in receive mode.
337 tcurlen = call->curlen;
338 tnLeft = call->nLeft;
339 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
340 tcurpos = call->curpos;
341 memcpy(buf, tcurpos, nbytes);
342 call->curpos = tcurpos + nbytes;
343 call->curlen = tcurlen - nbytes;
344 call->nLeft = tnLeft - nbytes;
347 /* out of packet. Get another one. */
349 MUTEX_ENTER(&call->lock);
350 rxi_FreePacket(call->currentPacket);
351 call->currentPacket = (struct rx_packet *)0;
352 MUTEX_EXIT(&call->lock);
359 MUTEX_ENTER(&call->lock);
360 bytes = rxi_ReadProc(call, buf, nbytes);
361 MUTEX_EXIT(&call->lock);
366 /* Optimization for unmarshalling 32 bit integers */
368 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
377 * Free any packets from the last call to ReadvProc/WritevProc.
378 * We do not need the lock because the receiver threads only
379 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
380 * RX_CALL_IOVEC_WAIT is always cleared before returning from
381 * ReadvProc/WritevProc.
383 if (!queue_IsEmpty(&call->iovq)) {
384 #ifdef RXDEBUG_PACKET
386 #endif /* RXDEBUG_PACKET */
387 rxi_FreePackets(0, &call->iovq);
391 * Most common case, all of the data is in the current iovec.
392 * We do not need the lock because this is the only thread that
393 * updates the curlen, curpos, nLeft fields.
395 * We are relying on nLeft being zero unless the call is in receive mode.
397 tcurlen = call->curlen;
398 tnLeft = call->nLeft;
399 if (!call->error && tcurlen >= sizeof(afs_int32)
400 && tnLeft >= sizeof(afs_int32)) {
401 tcurpos = call->curpos;
402 memcpy((char *)value, tcurpos, sizeof(afs_int32));
403 call->curpos = tcurpos + sizeof(afs_int32);
404 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
405 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
406 if (!call->nLeft && call->currentPacket != NULL) {
407 /* out of packet. Get another one. */
409 MUTEX_ENTER(&call->lock);
410 rxi_FreePacket(call->currentPacket);
411 call->currentPacket = (struct rx_packet *)0;
412 MUTEX_EXIT(&call->lock);
415 return sizeof(afs_int32);
419 MUTEX_ENTER(&call->lock);
420 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
421 MUTEX_EXIT(&call->lock);
428 * Uses packets in the receive queue to fill in as much of the
429 * current iovec as possible. Does not block if it runs out
430 * of packets to complete the iovec. Return true if an ack packet
431 * was sent, otherwise return false */
433 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
438 struct rx_packet *rp;
439 struct rx_packet *curp;
440 struct iovec *call_iov;
441 struct iovec *cur_iov = NULL;
443 curp = call->currentPacket;
445 cur_iov = &curp->wirevec[call->curvec];
447 call_iov = &call->iov[call->iovNext];
449 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
450 if (call->nLeft == 0) {
451 /* Get next packet */
452 if (queue_IsNotEmpty(&call->rq)) {
453 /* Check that next packet available is next in sequence */
454 rp = queue_First(&call->rq, rx_packet);
455 if (rp->header.seq == call->rnext) {
457 struct rx_connection *conn = call->conn;
459 rp->flags &= ~RX_PKTFLAG_RQ;
460 #ifdef RXDEBUG_PACKET
462 #endif /* RXDEBUG_PACKET */
464 /* RXS_CheckPacket called to undo RXS_PreparePacket's
465 * work. It may reduce the length of the packet by up
466 * to conn->maxTrailerSize, to reflect the length of the
467 * data + the header. */
469 RXS_CheckPacket(conn->securityObject, call, rp))) {
470 /* Used to merely shut down the call, but now we
471 * shut down the whole connection since this may
472 * indicate an attempt to hijack it */
474 MUTEX_EXIT(&call->lock);
475 rxi_ConnectionError(conn, error);
476 MUTEX_ENTER(&conn->conn_data_lock);
477 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
478 MUTEX_EXIT(&conn->conn_data_lock);
480 MUTEX_ENTER(&call->lock);
485 curp = call->currentPacket = rp;
486 call->currentPacket->flags |= RX_PKTFLAG_CP;
487 call->curvec = 1; /* 0th vec is always header */
488 cur_iov = &curp->wirevec[1];
489 /* begin at the beginning [ more or less ], continue
490 * on until the end, then stop. */
492 (char *)curp->wirevec[1].iov_base +
493 call->conn->securityHeaderSize;
495 curp->wirevec[1].iov_len -
496 call->conn->securityHeaderSize;
498 /* Notice that this code works correctly if the data
499 * size is 0 (which it may be--no reply arguments from
500 * server, for example). This relies heavily on the
501 * fact that the code below immediately frees the packet
502 * (no yields, etc.). If it didn't, this would be a
503 * problem because a value of zero for call->nLeft
504 * normally means that there is no read packet */
505 call->nLeft = curp->length;
506 hadd32(call->bytesRcvd, curp->length);
508 /* Send a hard ack for every rxi_HardAckRate+1 packets
509 * consumed. Otherwise schedule an event to send
510 * the hard ack later on.
520 /* It's possible for call->nLeft to be smaller than any particular
521 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
522 * reflects the size of the buffer. We have to keep track of the
523 * number of bytes read in the length field of the packet struct. On
524 * the final portion of a received packet, it's almost certain that
525 * call->nLeft will be smaller than the final buffer. */
526 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
528 t = MIN((int)call->curlen, call->iovNBytes);
529 t = MIN(t, (int)call->nLeft);
530 call_iov->iov_base = call->curpos;
531 call_iov->iov_len = t;
534 call->iovNBytes -= t;
540 /* out of packet. Get another one. */
541 curp->flags &= ~RX_PKTFLAG_CP;
542 curp->flags |= RX_PKTFLAG_IOVQ;
543 queue_Append(&call->iovq, curp);
544 #ifdef RXDEBUG_PACKET
546 #endif /* RXDEBUG_PACKET */
547 curp = call->currentPacket = (struct rx_packet *)0;
548 } else if (!call->curlen) {
549 /* need to get another struct iov */
550 if (++call->curvec >= curp->niovecs) {
551 /* current packet is exhausted, get ready for another */
552 /* don't worry about curvec and stuff, they get set somewhere else */
553 curp->flags &= ~RX_PKTFLAG_CP;
554 curp->flags |= RX_PKTFLAG_IOVQ;
555 queue_Append(&call->iovq, curp);
556 #ifdef RXDEBUG_PACKET
558 #endif /* RXDEBUG_PACKET */
559 curp = call->currentPacket = (struct rx_packet *)0;
563 call->curpos = (char *)cur_iov->iov_base;
564 call->curlen = cur_iov->iov_len;
570 /* If we consumed any packets then check whether we need to
571 * send a hard ack. */
572 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
573 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
574 rxevent_Cancel(call->delayedAckEvent, call,
575 RX_CALL_REFCOUNT_DELAY);
576 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
579 struct clock when, now;
582 /* Delay to consolidate ack packets */
583 clock_Add(&when, &rx_hardAckDelay);
584 if (!call->delayedAckEvent
585 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
586 rxevent_Cancel(call->delayedAckEvent, call,
587 RX_CALL_REFCOUNT_DELAY);
588 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
589 call->delayedAckEvent =
590 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
598 /* rxi_ReadvProc -- internal version.
600 * Fills in an iovec with pointers to the packet buffers. All packets
601 * except the last packet (new current packet) are moved to the iovq
602 * while the application is processing the data.
604 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
607 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
613 requestCount = nbytes;
616 /* Free any packets from the last call to ReadvProc/WritevProc */
617 if (queue_IsNotEmpty(&call->iovq)) {
618 #ifdef RXDEBUG_PACKET
620 #endif /* RXDEBUG_PACKET */
621 rxi_FreePackets(0, &call->iovq);
624 if (call->mode == RX_MODE_SENDING) {
625 rxi_FlushWrite(call);
632 /* Get whatever data is currently available in the receive queue.
633 * If rxi_FillReadVec sends an ack packet then it is possible
634 * that we will receive more data while we drop the call lock
635 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
636 * here to avoid a race with the receive thread if we send
637 * hard acks in rxi_FillReadVec. */
638 call->flags |= RX_CALL_IOVEC_WAIT;
639 call->iovNBytes = nbytes;
640 call->iovMax = maxio;
643 rxi_FillReadVec(call, 0);
645 /* if we need more data then sleep until the receive thread has
646 * filled in the rest. */
647 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
648 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
649 call->flags |= RX_CALL_READER_WAIT;
651 call->startWait = clock_Sec();
652 while (call->flags & RX_CALL_READER_WAIT) {
653 #ifdef RX_ENABLE_LOCKS
654 CV_WAIT(&call->cv_rq, &call->lock);
656 osi_rxSleep(&call->rq);
661 call->flags &= ~RX_CALL_IOVEC_WAIT;
662 #ifdef RX_ENABLE_LOCKS
666 #endif /* RX_ENABLE_LOCKS */
669 *nio = call->iovNext;
670 return nbytes - call->iovNBytes;
674 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
681 MUTEX_ENTER(&call->lock);
682 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
683 MUTEX_EXIT(&call->lock);
688 /* rxi_WriteProc -- internal version.
690 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
693 rxi_WriteProc(struct rx_call *call, char *buf,
696 struct rx_connection *conn = call->conn;
697 struct rx_packet *cp = call->currentPacket;
699 int requestCount = nbytes;
701 /* Free any packets from the last call to ReadvProc/WritevProc */
702 if (queue_IsNotEmpty(&call->iovq)) {
703 #ifdef RXDEBUG_PACKET
705 #endif /* RXDEBUG_PACKET */
706 rxi_FreePackets(0, &call->iovq);
709 if (call->mode != RX_MODE_SENDING) {
710 if ((conn->type == RX_SERVER_CONNECTION)
711 && (call->mode == RX_MODE_RECEIVING)) {
712 call->mode = RX_MODE_SENDING;
714 cp->flags &= ~RX_PKTFLAG_CP;
716 cp = call->currentPacket = (struct rx_packet *)0;
725 /* Loop condition is checked at end, so that a write of 0 bytes
726 * will force a packet to be created--specially for the case where
727 * there are 0 bytes on the stream, but we must send a packet
730 if (call->nFree == 0) {
731 if (!call->error && cp) {
732 /* Clear the current packet now so that if
733 * we are forced to wait and drop the lock
734 * the packet we are planning on using
737 cp->flags &= ~RX_PKTFLAG_CP;
738 call->currentPacket = (struct rx_packet *)0;
739 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
740 /* Wait until TQ_BUSY is reset before adding any
741 * packets to the transmit queue
743 while (call->flags & RX_CALL_TQ_BUSY) {
744 call->flags |= RX_CALL_TQ_WAIT;
745 #ifdef RX_ENABLE_LOCKS
746 CV_WAIT(&call->cv_tq, &call->lock);
747 #else /* RX_ENABLE_LOCKS */
748 osi_rxSleep(&call->tq);
749 #endif /* RX_ENABLE_LOCKS */
751 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
752 clock_NewTime(); /* Bogus: need new time package */
753 /* The 0, below, specifies that it is not the last packet:
754 * there will be others. PrepareSendPacket may
755 * alter the packet length by up to
756 * conn->securityMaxTrailerSize */
757 hadd32(call->bytesSent, cp->length);
758 rxi_PrepareSendPacket(call, cp, 0);
759 cp->flags |= RX_PKTFLAG_TQ;
760 queue_Append(&call->tq, cp);
761 #ifdef RXDEBUG_PACKET
763 #endif /* RXDEBUG_PACKET */
764 cp = (struct rx_packet *)0;
767 flags & (RX_CALL_FAST_RECOVER |
768 RX_CALL_FAST_RECOVER_WAIT))) {
769 rxi_Start(0, call, 0, 0);
772 cp->flags &= ~RX_PKTFLAG_CP;
774 cp = call->currentPacket = (struct rx_packet *)0;
776 /* Wait for transmit window to open up */
778 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
780 call->startWait = clock_Sec();
782 #ifdef RX_ENABLE_LOCKS
783 CV_WAIT(&call->cv_twind, &call->lock);
785 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
786 osi_rxSleep(&call->twind);
790 #ifdef RX_ENABLE_LOCKS
794 #endif /* RX_ENABLE_LOCKS */
796 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
797 cp->flags |= RX_PKTFLAG_CP;
798 call->currentPacket = cp;
799 call->nFree = cp->length;
800 call->curvec = 1; /* 0th vec is always header */
801 /* begin at the beginning [ more or less ], continue
802 * on until the end, then stop. */
804 (char *)cp->wirevec[1].iov_base +
805 call->conn->securityHeaderSize;
807 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
811 cp->flags &= ~RX_PKTFLAG_CP;
813 call->currentPacket = NULL;
819 if (cp && (int)call->nFree < nbytes) {
820 /* Try to extend the current buffer */
823 mud = rx_MaxUserDataSize(call);
826 want = MIN(nbytes - (int)call->nFree, mud - len);
827 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
828 if (cp->length > (unsigned)mud)
830 call->nFree += (cp->length - len);
834 /* If the remaining bytes fit in the buffer, then store them
835 * and return. Don't ship a buffer that's full immediately to
836 * the peer--we don't know if it's the last buffer yet */
842 while (nbytes && call->nFree) {
844 t = MIN((int)call->curlen, nbytes);
845 t = MIN((int)call->nFree, t);
846 memcpy(call->curpos, buf, t);
850 call->curlen -= (u_short)t;
851 call->nFree -= (u_short)t;
854 /* need to get another struct iov */
855 if (++call->curvec >= cp->niovecs) {
856 /* current packet is full, extend or send it */
859 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
860 call->curlen = cp->wirevec[call->curvec].iov_len;
863 } /* while bytes to send and room to send them */
865 /* might be out of space now */
868 } else; /* more data to send, so get another packet and keep going */
871 return requestCount - nbytes;
875 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
884 * Free any packets from the last call to ReadvProc/WritevProc.
885 * We do not need the lock because the receiver threads only
886 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
887 * RX_CALL_IOVEC_WAIT is always cleared before returning from
888 * ReadvProc/WritevProc.
890 if (queue_IsNotEmpty(&call->iovq)) {
891 #ifdef RXDEBUG_PACKET
893 #endif /* RXDEBUG_PACKET */
894 rxi_FreePackets(0, &call->iovq);
898 * Most common case: all of the data fits in the current iovec.
899 * We do not need the lock because this is the only thread that
900 * updates the curlen, curpos, nFree fields.
902 * We are relying on nFree being zero unless the call is in send mode.
904 tcurlen = (int)call->curlen;
905 tnFree = (int)call->nFree;
906 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
907 tcurpos = call->curpos;
908 memcpy(tcurpos, buf, nbytes);
909 call->curpos = tcurpos + nbytes;
910 call->curlen = (u_short)(tcurlen - nbytes);
911 call->nFree = (u_short)(tnFree - nbytes);
916 MUTEX_ENTER(&call->lock);
917 bytes = rxi_WriteProc(call, buf, nbytes);
918 MUTEX_EXIT(&call->lock);
923 /* Optimization for marshalling 32 bit arguments */
925 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
934 * Free any packets from the last call to ReadvProc/WritevProc.
935 * We do not need the lock because the receiver threads only
936 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
937 * RX_CALL_IOVEC_WAIT is always cleared before returning from
938 * ReadvProc/WritevProc.
940 if (queue_IsNotEmpty(&call->iovq)) {
941 #ifdef RXDEBUG_PACKET
943 #endif /* RXDEBUG_PACKET */
944 rxi_FreePackets(0, &call->iovq);
948 * Most common case: all of the data fits in the current iovec.
949 * We do not need the lock because this is the only thread that
950 * updates the curlen, curpos, nFree fields.
952 * We are relying on nFree being zero unless the call is in send mode.
954 tcurlen = call->curlen;
955 tnFree = call->nFree;
956 if (!call->error && tcurlen >= sizeof(afs_int32)
957 && tnFree >= sizeof(afs_int32)) {
958 tcurpos = call->curpos;
959 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
960 *((afs_int32 *) (tcurpos)) = *value;
962 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
964 call->curpos = tcurpos + sizeof(afs_int32);
965 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
966 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
967 return sizeof(afs_int32);
971 MUTEX_ENTER(&call->lock);
972 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
973 MUTEX_EXIT(&call->lock);
978 /* rxi_WritevAlloc -- internal version.
980 * Fill in an iovec to point to data in packet buffers. The application
981 * calls rxi_WritevProc when the buffers are full.
983 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
986 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
989 struct rx_connection *conn = call->conn;
990 struct rx_packet *cp = call->currentPacket;
993 /* Temporary values, real work is done in rxi_WritevProc */
999 requestCount = nbytes;
1002 /* Free any packets from the last call to ReadvProc/WritevProc */
1003 if (queue_IsNotEmpty(&call->iovq)) {
1004 #ifdef RXDEBUG_PACKET
1006 #endif /* RXDEBUG_PACKET */
1007 rxi_FreePackets(0, &call->iovq);
1010 if (call->mode != RX_MODE_SENDING) {
1011 if ((conn->type == RX_SERVER_CONNECTION)
1012 && (call->mode == RX_MODE_RECEIVING)) {
1013 call->mode = RX_MODE_SENDING;
1015 cp->flags &= ~RX_PKTFLAG_CP;
1017 cp = call->currentPacket = (struct rx_packet *)0;
1026 /* Set up the iovec to point to data in packet buffers. */
1027 tnFree = call->nFree;
1028 tcurvec = call->curvec;
1029 tcurpos = call->curpos;
1030 tcurlen = call->curlen;
1035 /* current packet is full, allocate a new one */
1036 cp = rxi_AllocSendPacket(call, nbytes);
1038 /* out of space, return what we have */
1040 return requestCount - nbytes;
1042 cp->flags |= RX_PKTFLAG_IOVQ;
1043 queue_Append(&call->iovq, cp);
1044 #ifdef RXDEBUG_PACKET
1046 #endif /* RXDEBUG_PACKET */
1047 tnFree = cp->length;
1050 (char *)cp->wirevec[1].iov_base +
1051 call->conn->securityHeaderSize;
1052 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1055 if (tnFree < nbytes) {
1056 /* try to extend the current packet */
1059 mud = rx_MaxUserDataSize(call);
1062 want = MIN(nbytes - tnFree, mud - len);
1063 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1064 if (cp->length > (unsigned)mud)
1066 tnFree += (cp->length - len);
1067 if (cp == call->currentPacket) {
1068 call->nFree += (cp->length - len);
1073 /* fill in the next entry in the iovec */
1074 t = MIN(tcurlen, nbytes);
1076 iov[nextio].iov_base = tcurpos;
1077 iov[nextio].iov_len = t;
1085 /* need to get another struct iov */
1086 if (++tcurvec >= cp->niovecs) {
1087 /* current packet is full, extend it or move on to next packet */
1090 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1091 tcurlen = cp->wirevec[tcurvec].iov_len;
1094 } while (nbytes && nextio < maxio);
1096 return requestCount - nbytes;
1100 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1107 MUTEX_ENTER(&call->lock);
1108 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1109 MUTEX_EXIT(&call->lock);
1114 /* rxi_WritevProc -- internal version.
1116 * Send buffers allocated in rxi_WritevAlloc.
1118 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1121 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1123 struct rx_packet *cp = NULL;
1124 struct rx_packet *p, *np;
1127 struct rx_queue tmpq;
1128 #ifdef RXDEBUG_PACKET
1132 requestCount = nbytes;
1135 if (call->mode != RX_MODE_SENDING) {
1136 call->error = RX_PROTOCOL_ERROR;
1138 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1139 /* Wait until TQ_BUSY is reset before trying to move any
1140 * packets to the transmit queue. */
1141 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1142 call->flags |= RX_CALL_TQ_WAIT;
1143 #ifdef RX_ENABLE_LOCKS
1144 CV_WAIT(&call->cv_tq, &call->lock);
1145 #else /* RX_ENABLE_LOCKS */
1146 osi_rxSleep(&call->tq);
1147 #endif /* RX_ENABLE_LOCKS */
1149 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1150 /* cp is no longer valid since we may have given up the lock */
1151 cp = call->currentPacket;
1155 cp->flags &= ~RX_PKTFLAG_CP;
1156 cp->flags |= RX_PKTFLAG_IOVQ;
1157 queue_Prepend(&call->iovq, cp);
1158 #ifdef RXDEBUG_PACKET
1160 #endif /* RXDEBUG_PACKET */
1161 cp = call->currentPacket = (struct rx_packet *)0;
1163 #ifdef RXDEBUG_PACKET
1165 #endif /* RXDEBUG_PACKET */
1166 rxi_FreePackets(0, &call->iovq);
1170 /* Loop through the I/O vector adjusting packet pointers.
1171 * Place full packets back onto the iovq once they are ready
1172 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1173 * the iovec. We put the loop condition at the end to ensure that
1174 * a zero length write will push a short packet. */
1177 #ifdef RXDEBUG_PACKET
1179 #endif /* RXDEBUG_PACKET */
1181 if (call->nFree == 0 && cp) {
1182 clock_NewTime(); /* Bogus: need new time package */
1183 /* The 0, below, specifies that it is not the last packet:
1184 * there will be others. PrepareSendPacket may
1185 * alter the packet length by up to
1186 * conn->securityMaxTrailerSize */
1187 hadd32(call->bytesSent, cp->length);
1188 rxi_PrepareSendPacket(call, cp, 0);
1189 queue_Append(&tmpq, cp);
1190 #ifdef RXDEBUG_PACKET
1192 #endif /* RXDEBUG_PACKET */
1193 cp = call->currentPacket = (struct rx_packet *)0;
1195 /* The head of the iovq is now the current packet */
1197 if (queue_IsEmpty(&call->iovq)) {
1198 call->error = RX_PROTOCOL_ERROR;
1199 #ifdef RXDEBUG_PACKET
1201 #endif /* RXDEBUG_PACKET */
1202 rxi_FreePackets(0, &tmpq);
1205 cp = queue_First(&call->iovq, rx_packet);
1207 cp->flags &= ~RX_PKTFLAG_IOVQ;
1208 #ifdef RXDEBUG_PACKET
1210 #endif /* RXDEBUG_PACKET */
1211 cp->flags |= RX_PKTFLAG_CP;
1212 call->currentPacket = cp;
1213 call->nFree = cp->length;
1216 (char *)cp->wirevec[1].iov_base +
1217 call->conn->securityHeaderSize;
1219 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1224 /* The next iovec should point to the current position */
1225 if (iov[nextio].iov_base != call->curpos
1226 || iov[nextio].iov_len > (int)call->curlen) {
1227 call->error = RX_PROTOCOL_ERROR;
1229 cp->flags &= ~RX_PKTFLAG_CP;
1230 queue_Prepend(&tmpq, cp);
1231 #ifdef RXDEBUG_PACKET
1233 #endif /* RXDEBUG_PACKET */
1234 cp = call->currentPacket = (struct rx_packet *)0;
1236 #ifdef RXDEBUG_PACKET
1238 #endif /* RXDEBUG_PACKET */
1239 rxi_FreePackets(0, &tmpq);
1242 nbytes -= iov[nextio].iov_len;
1243 call->curpos += iov[nextio].iov_len;
1244 call->curlen -= iov[nextio].iov_len;
1245 call->nFree -= iov[nextio].iov_len;
1247 if (call->curlen == 0) {
1248 if (++call->curvec > cp->niovecs) {
1251 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1252 call->curlen = cp->wirevec[call->curvec].iov_len;
1256 } while (nbytes && nextio < nio);
1258 /* Move the packets from the temporary queue onto the transmit queue.
1259 * We may end up with more than call->twind packets on the queue. */
1261 for (queue_Scan(&tmpq, p, np, rx_packet))
1263 p->flags |= RX_PKTFLAG_TQ;
1265 queue_SpliceAppend(&call->tq, &tmpq);
1267 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1268 rxi_Start(0, call, 0, 0);
1271 /* Wait for the length of the transmit queue to fall below call->twind */
1272 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1274 call->startWait = clock_Sec();
1275 #ifdef RX_ENABLE_LOCKS
1276 CV_WAIT(&call->cv_twind, &call->lock);
1278 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1279 osi_rxSleep(&call->twind);
1281 call->startWait = 0;
1283 /* cp is no longer valid since we may have given up the lock */
1284 cp = call->currentPacket;
1288 cp->flags &= ~RX_PKTFLAG_CP;
1290 cp = call->currentPacket = (struct rx_packet *)0;
1295 return requestCount - nbytes;
1299 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1305 MUTEX_ENTER(&call->lock);
1306 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1307 MUTEX_EXIT(&call->lock);
1312 /* Flush any buffered data to the stream, switch to read mode
1313 * (clients) or to EOF mode (servers) */
1315 rxi_FlushWrite(struct rx_call *call)
1317 struct rx_packet *cp = NULL;
1319 /* Free any packets from the last call to ReadvProc/WritevProc */
1320 if (queue_IsNotEmpty(&call->iovq)) {
1321 #ifdef RXDEBUG_PACKET
1323 #endif /* RXDEBUG_PACKET */
1324 rxi_FreePackets(0, &call->iovq);
1327 if (call->mode == RX_MODE_SENDING) {
1330 (call->conn->type ==
1331 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1333 #ifdef RX_KERNEL_TRACE
1335 int glockOwner = ISAFS_GLOCK();
1338 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1339 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1346 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1347 /* Wait until TQ_BUSY is reset before adding any
1348 * packets to the transmit queue
1350 while (call->flags & RX_CALL_TQ_BUSY) {
1351 call->flags |= RX_CALL_TQ_WAIT;
1352 #ifdef RX_ENABLE_LOCKS
1353 CV_WAIT(&call->cv_tq, &call->lock);
1354 #else /* RX_ENABLE_LOCKS */
1355 osi_rxSleep(&call->tq);
1356 #endif /* RX_ENABLE_LOCKS */
1358 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1360 /* cp is no longer valid since we may have given up the lock */
1361 cp = call->currentPacket;
1364 /* cp->length is only supposed to be the user's data */
1365 /* cp->length was already set to (then-current)
1366 * MaxUserDataSize or less. */
1367 cp->flags &= ~RX_PKTFLAG_CP;
1368 cp->length -= call->nFree;
1369 call->currentPacket = (struct rx_packet *)0;
1372 cp = rxi_AllocSendPacket(call, 0);
1374 /* Mode can no longer be MODE_SENDING */
1378 cp->niovecs = 2; /* header + space for rxkad stuff */
1382 /* The 1 specifies that this is the last packet */
1383 hadd32(call->bytesSent, cp->length);
1384 rxi_PrepareSendPacket(call, cp, 1);
1385 cp->flags |= RX_PKTFLAG_TQ;
1386 queue_Append(&call->tq, cp);
1387 #ifdef RXDEBUG_PACKET
1389 #endif /* RXDEBUG_PACKET */
1392 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1393 rxi_Start(0, call, 0, 0);
1398 /* Flush any buffered data to the stream, switch to read mode
1399 * (clients) or to EOF mode (servers) */
1401 rx_FlushWrite(struct rx_call *call)
1405 MUTEX_ENTER(&call->lock);
1406 rxi_FlushWrite(call);
1407 MUTEX_EXIT(&call->lock);