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>
11 #include <afs/param.h>
15 # ifdef RX_KERNEL_TRACE
16 # include "rx_kcommon.h"
18 # if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
19 # include "afs/sysincludes.h"
24 # if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV)
28 # include <net/net_globals.h>
29 # endif /* AFS_OSF_ENV */
30 # ifdef AFS_LINUX20_ENV
31 # include "h/socket.h"
33 # include "netinet/in.h"
34 # if defined(AFS_SGI_ENV)
35 # include "afs/sysincludes.h"
38 # include "afs/afs_args.h"
39 # if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
43 # include "afs/sysincludes.h"
44 # endif /* !UKERNEL */
47 # undef RXDEBUG /* turn off debugging */
50 # include "afs/afs_osi.h"
51 # include "rx_kmutex.h"
52 # include "rx/rx_kernel.h"
53 # include "afs/lock.h"
62 #include "rx_globals.h"
63 #include "rx_atomic.h"
64 #include "rx_internal.h"
67 #include "rx_packet.h"
70 /* rxdb_fileID is used to identify the lock location, along with line#. */
71 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
72 #endif /* RX_LOCKS_DB */
74 /* Get the next packet in the receive queue
76 * Dispose of the call's currentPacket, and move the next packet in the
77 * receive queue into the currentPacket field. If the next packet isn't
78 * available, then currentPacket is left NULL.
81 * The RX call to manipulate
83 * 0 on success, an error code on failure
86 * Must be called with the call locked. Unlocks the call if returning
91 rxi_GetNextPacket(struct rx_call *call) {
95 if (call->currentPacket != NULL) {
96 #ifdef RX_TRACK_PACKETS
97 call->currentPacket->flags |= RX_PKTFLAG_CP;
99 rxi_FreePacket(call->currentPacket);
100 call->currentPacket = NULL;
103 if (queue_IsEmpty(&call->rq))
106 /* Check that next packet available is next in sequence */
107 rp = queue_First(&call->rq, rx_packet);
108 if (rp->header.seq != call->rnext)
112 #ifdef RX_TRACK_PACKETS
113 rp->flags &= ~RX_PKTFLAG_RQ;
115 #ifdef RXDEBUG_PACKET
117 #endif /* RXDEBUG_PACKET */
119 /* RXS_CheckPacket called to undo RXS_PreparePacket's work. It may
120 * reduce the length of the packet by up to conn->maxTrailerSize,
121 * to reflect the length of the data + the header. */
122 if ((error = RXS_CheckPacket(call->conn->securityObject, call, rp))) {
123 /* Used to merely shut down the call, but now we shut down the whole
124 * connection since this may indicate an attempt to hijack it */
126 MUTEX_EXIT(&call->lock);
127 rxi_ConnectionError(call->conn, error);
128 MUTEX_ENTER(&call->conn->conn_data_lock);
129 rp = rxi_SendConnectionAbort(call->conn, rp, 0, 0);
130 MUTEX_EXIT(&call->conn->conn_data_lock);
137 call->currentPacket = rp;
138 #ifdef RX_TRACK_PACKETS
139 call->currentPacket->flags |= RX_PKTFLAG_CP;
141 call->curvec = 1; /* 0th vec is always header */
143 /* begin at the beginning [ more or less ], continue on until the end,
145 call->curpos = (char *)call->currentPacket->wirevec[1].iov_base +
146 call->conn->securityHeaderSize;
147 call->curlen = call->currentPacket->wirevec[1].iov_len -
148 call->conn->securityHeaderSize;
150 call->nLeft = call->currentPacket->length;
151 call->bytesRcvd += call->currentPacket->length;
158 /* rxi_ReadProc -- internal version.
160 * LOCKS USED -- called at netpri
163 rxi_ReadProc(struct rx_call *call, char *buf,
170 /* XXXX took out clock_NewTime from here. Was it needed? */
171 requestCount = nbytes;
173 /* Free any packets from the last call to ReadvProc/WritevProc */
174 if (queue_IsNotEmpty(&call->iovq)) {
175 #ifdef RXDEBUG_PACKET
177 #endif /* RXDEBUG_PACKET */
178 rxi_FreePackets(0, &call->iovq);
182 if (call->nLeft == 0) {
183 /* Get next packet */
184 MUTEX_ENTER(&call->lock);
186 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
188 call->mode = RX_MODE_ERROR;
189 MUTEX_EXIT(&call->lock);
192 if (call->mode == RX_MODE_SENDING) {
193 MUTEX_EXIT(&call->lock);
194 rxi_FlushWrite(call);
195 MUTEX_ENTER(&call->lock);
200 code = rxi_GetNextPacket(call);
204 if (call->currentPacket) {
205 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
206 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
207 rxevent_Cancel(&call->delayedAckEvent, call,
208 RX_CALL_REFCOUNT_DELAY);
209 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
211 /* Delay to consolidate ack packets */
212 rxi_PostDelayedAckEvent(call, &rx_hardAckDelay);
219 * If we reach this point either we have no packets in the
220 * receive queue or the next packet in the queue is not the
221 * one we are looking for. There is nothing else for us to
222 * do but wait for another packet to arrive.
225 /* Are there ever going to be any more packets? */
226 if (call->flags & RX_CALL_RECEIVE_DONE) {
227 MUTEX_EXIT(&call->lock);
228 return requestCount - nbytes;
230 /* Wait for in-sequence packet */
231 call->flags |= RX_CALL_READER_WAIT;
233 call->startWait = clock_Sec();
234 while (call->flags & RX_CALL_READER_WAIT) {
235 #ifdef RX_ENABLE_LOCKS
236 CV_WAIT(&call->cv_rq, &call->lock);
238 osi_rxSleep(&call->rq);
243 #ifdef RX_ENABLE_LOCKS
245 MUTEX_EXIT(&call->lock);
248 #endif /* RX_ENABLE_LOCKS */
250 MUTEX_EXIT(&call->lock);
252 /* osi_Assert(cp); */
253 /* MTUXXX this should be replaced by some error-recovery code before shipping */
254 /* yes, the following block is allowed to be the ELSE clause (or not) */
255 /* It's possible for call->nLeft to be smaller than any particular
256 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
257 * reflects the size of the buffer. We have to keep track of the
258 * number of bytes read in the length field of the packet struct. On
259 * the final portion of a received packet, it's almost certain that
260 * call->nLeft will be smaller than the final buffer. */
261 while (nbytes && call->currentPacket) {
262 t = MIN((int)call->curlen, nbytes);
263 t = MIN(t, (int)call->nLeft);
264 memcpy(buf, call->curpos, t);
272 /* out of packet. Get another one. */
273 #ifdef RX_TRACK_PACKETS
274 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
276 rxi_FreePacket(call->currentPacket);
277 call->currentPacket = NULL;
278 } else if (!call->curlen) {
279 /* need to get another struct iov */
280 if (++call->curvec >= call->currentPacket->niovecs) {
281 /* current packet is exhausted, get ready for another */
282 /* don't worry about curvec and stuff, they get set somewhere else */
283 #ifdef RX_TRACK_PACKETS
284 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
286 rxi_FreePacket(call->currentPacket);
287 call->currentPacket = NULL;
291 call->currentPacket->wirevec[call->curvec].iov_base;
293 call->currentPacket->wirevec[call->curvec].iov_len;
298 /* user buffer is full, return */
308 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
313 /* Free any packets from the last call to ReadvProc/WritevProc */
314 if (!queue_IsEmpty(&call->iovq)) {
315 #ifdef RXDEBUG_PACKET
317 #endif /* RXDEBUG_PACKET */
318 rxi_FreePackets(0, &call->iovq);
322 * Most common case, all of the data is in the current iovec.
323 * We are relying on nLeft being zero unless the call is in receive mode.
325 if (!call->error && call->curlen > nbytes && call->nLeft > nbytes) {
326 memcpy(buf, call->curpos, nbytes);
328 call->curpos += nbytes;
329 call->curlen -= nbytes;
330 call->nLeft -= nbytes;
332 if (!call->nLeft && call->currentPacket != NULL) {
333 /* out of packet. Get another one. */
334 rxi_FreePacket(call->currentPacket);
335 call->currentPacket = (struct rx_packet *)0;
341 bytes = rxi_ReadProc(call, buf, nbytes);
346 /* Optimization for unmarshalling 32 bit integers */
348 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
353 /* Free any packets from the last call to ReadvProc/WritevProc */
354 if (!queue_IsEmpty(&call->iovq)) {
355 #ifdef RXDEBUG_PACKET
357 #endif /* RXDEBUG_PACKET */
358 rxi_FreePackets(0, &call->iovq);
362 * Most common case, all of the data is in the current iovec.
363 * We are relying on nLeft being zero unless the call is in receive mode.
365 if (!call->error && call->curlen >= sizeof(afs_int32)
366 && call->nLeft >= sizeof(afs_int32)) {
368 memcpy((char *)value, call->curpos, sizeof(afs_int32));
370 call->curpos += sizeof(afs_int32);
371 call->curlen -= sizeof(afs_int32);
372 call->nLeft -= sizeof(afs_int32);
374 if (!call->nLeft && call->currentPacket != NULL) {
375 /* out of packet. Get another one. */
376 rxi_FreePacket(call->currentPacket);
377 call->currentPacket = (struct rx_packet *)0;
379 return sizeof(afs_int32);
383 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
391 * Uses packets in the receive queue to fill in as much of the
392 * current iovec as possible. Does not block if it runs out
393 * of packets to complete the iovec. Return true if an ack packet
394 * was sent, otherwise return false */
396 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
402 struct iovec *call_iov;
403 struct iovec *cur_iov = NULL;
405 if (call->currentPacket) {
406 cur_iov = &call->currentPacket->wirevec[call->curvec];
408 call_iov = &call->iov[call->iovNext];
410 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
411 if (call->nLeft == 0) {
412 /* Get next packet */
413 code = rxi_GetNextPacket(call);
415 MUTEX_ENTER(&call->lock);
419 if (call->currentPacket) {
420 cur_iov = &call->currentPacket->wirevec[1];
428 /* It's possible for call->nLeft to be smaller than any particular
429 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
430 * reflects the size of the buffer. We have to keep track of the
431 * number of bytes read in the length field of the packet struct. On
432 * the final portion of a received packet, it's almost certain that
433 * call->nLeft will be smaller than the final buffer. */
434 while (call->iovNBytes
435 && call->iovNext < call->iovMax
436 && call->currentPacket) {
438 t = MIN((int)call->curlen, call->iovNBytes);
439 t = MIN(t, (int)call->nLeft);
440 call_iov->iov_base = call->curpos;
441 call_iov->iov_len = t;
444 call->iovNBytes -= t;
450 /* out of packet. Get another one. */
451 #ifdef RX_TRACK_PACKETS
452 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
453 call->currentPacket->flags |= RX_PKTFLAG_IOVQ;
455 queue_Append(&call->iovq, call->currentPacket);
456 #ifdef RXDEBUG_PACKET
458 #endif /* RXDEBUG_PACKET */
459 call->currentPacket = NULL;
460 } else if (!call->curlen) {
461 /* need to get another struct iov */
462 if (++call->curvec >= call->currentPacket->niovecs) {
463 /* current packet is exhausted, get ready for another */
464 /* don't worry about curvec and stuff, they get set somewhere else */
465 #ifdef RX_TRACK_PACKETS
466 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
467 call->currentPacket->flags |= RX_PKTFLAG_IOVQ;
469 queue_Append(&call->iovq, call->currentPacket);
470 #ifdef RXDEBUG_PACKET
472 #endif /* RXDEBUG_PACKET */
473 call->currentPacket = NULL;
477 call->curpos = (char *)cur_iov->iov_base;
478 call->curlen = cur_iov->iov_len;
484 /* If we consumed any packets then check whether we need to
485 * send a hard ack. */
486 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
487 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
488 rxevent_Cancel(&call->delayedAckEvent, call,
489 RX_CALL_REFCOUNT_DELAY);
490 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
493 /* Delay to consolidate ack packets */
494 rxi_PostDelayedAckEvent(call, &rx_hardAckDelay);
501 /* rxi_ReadvProc -- internal version.
503 * Fills in an iovec with pointers to the packet buffers. All packets
504 * except the last packet (new current packet) are moved to the iovq
505 * while the application is processing the data.
507 * LOCKS USED -- called at netpri.
510 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
515 /* Free any packets from the last call to ReadvProc/WritevProc */
516 if (queue_IsNotEmpty(&call->iovq)) {
517 #ifdef RXDEBUG_PACKET
519 #endif /* RXDEBUG_PACKET */
520 rxi_FreePackets(0, &call->iovq);
523 if (call->mode == RX_MODE_SENDING) {
524 rxi_FlushWrite(call);
527 MUTEX_ENTER(&call->lock);
531 /* Get whatever data is currently available in the receive queue.
532 * If rxi_FillReadVec sends an ack packet then it is possible
533 * that we will receive more data while we drop the call lock
534 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
535 * here to avoid a race with the receive thread if we send
536 * hard acks in rxi_FillReadVec. */
537 call->flags |= RX_CALL_IOVEC_WAIT;
538 call->iovNBytes = nbytes;
539 call->iovMax = maxio;
542 rxi_FillReadVec(call, 0);
544 /* if we need more data then sleep until the receive thread has
545 * filled in the rest. */
546 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
547 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
548 call->flags |= RX_CALL_READER_WAIT;
550 call->startWait = clock_Sec();
551 while (call->flags & RX_CALL_READER_WAIT) {
552 #ifdef RX_ENABLE_LOCKS
553 CV_WAIT(&call->cv_rq, &call->lock);
555 osi_rxSleep(&call->rq);
560 call->flags &= ~RX_CALL_IOVEC_WAIT;
566 *nio = call->iovNext;
567 bytes = nbytes - call->iovNBytes;
568 MUTEX_EXIT(&call->lock);
572 MUTEX_EXIT(&call->lock);
573 call->mode = RX_MODE_ERROR;
578 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
585 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
590 /* rxi_WriteProc -- internal version.
592 * LOCKS USED -- called at netpri
596 rxi_WriteProc(struct rx_call *call, char *buf,
599 struct rx_connection *conn = call->conn;
601 int requestCount = nbytes;
603 /* Free any packets from the last call to ReadvProc/WritevProc */
604 if (queue_IsNotEmpty(&call->iovq)) {
605 #ifdef RXDEBUG_PACKET
607 #endif /* RXDEBUG_PACKET */
608 rxi_FreePackets(0, &call->iovq);
611 if (call->mode != RX_MODE_SENDING) {
612 if ((conn->type == RX_SERVER_CONNECTION)
613 && (call->mode == RX_MODE_RECEIVING)) {
614 call->mode = RX_MODE_SENDING;
615 if (call->currentPacket) {
616 #ifdef RX_TRACK_PACKETS
617 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
619 rxi_FreePacket(call->currentPacket);
620 call->currentPacket = NULL;
629 /* Loop condition is checked at end, so that a write of 0 bytes
630 * will force a packet to be created--specially for the case where
631 * there are 0 bytes on the stream, but we must send a packet
634 if (call->nFree == 0) {
635 MUTEX_ENTER(&call->lock);
637 call->mode = RX_MODE_ERROR;
638 if (!call->error && call->currentPacket) {
639 clock_NewTime(); /* Bogus: need new time package */
640 /* The 0, below, specifies that it is not the last packet:
641 * there will be others. PrepareSendPacket may
642 * alter the packet length by up to
643 * conn->securityMaxTrailerSize */
644 call->bytesSent += call->currentPacket->length;
645 rxi_PrepareSendPacket(call, call->currentPacket, 0);
646 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
647 /* PrepareSendPacket drops the call lock */
648 rxi_WaitforTQBusy(call);
649 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
650 #ifdef RX_TRACK_PACKETS
651 call->currentPacket->flags |= RX_PKTFLAG_TQ;
653 queue_Append(&call->tq, call->currentPacket);
654 #ifdef RXDEBUG_PACKET
656 #endif /* RXDEBUG_PACKET */
657 #ifdef RX_TRACK_PACKETS
658 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
660 call->currentPacket = NULL;
662 /* If the call is in recovery, let it exhaust its current
663 * retransmit queue before forcing it to send new packets
665 if (!(call->flags & (RX_CALL_FAST_RECOVER))) {
668 } else if (call->currentPacket) {
669 #ifdef RX_TRACK_PACKETS
670 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
672 rxi_FreePacket(call->currentPacket);
673 call->currentPacket = NULL;
675 /* Wait for transmit window to open up */
677 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
679 call->startWait = clock_Sec();
681 #ifdef RX_ENABLE_LOCKS
682 CV_WAIT(&call->cv_twind, &call->lock);
684 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
685 osi_rxSleep(&call->twind);
689 #ifdef RX_ENABLE_LOCKS
691 call->mode = RX_MODE_ERROR;
692 MUTEX_EXIT(&call->lock);
695 #endif /* RX_ENABLE_LOCKS */
697 if ((call->currentPacket = rxi_AllocSendPacket(call, nbytes))) {
698 #ifdef RX_TRACK_PACKETS
699 call->currentPacket->flags |= RX_PKTFLAG_CP;
701 call->nFree = call->currentPacket->length;
702 call->curvec = 1; /* 0th vec is always header */
703 /* begin at the beginning [ more or less ], continue
704 * on until the end, then stop. */
706 (char *) call->currentPacket->wirevec[1].iov_base +
707 call->conn->securityHeaderSize;
709 call->currentPacket->wirevec[1].iov_len -
710 call->conn->securityHeaderSize;
713 call->mode = RX_MODE_ERROR;
714 if (call->currentPacket) {
715 #ifdef RX_TRACK_PACKETS
716 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
718 rxi_FreePacket(call->currentPacket);
719 call->currentPacket = NULL;
721 MUTEX_EXIT(&call->lock);
724 MUTEX_EXIT(&call->lock);
727 if (call->currentPacket && (int)call->nFree < nbytes) {
728 /* Try to extend the current buffer */
730 len = call->currentPacket->length;
731 mud = rx_MaxUserDataSize(call);
734 want = MIN(nbytes - (int)call->nFree, mud - len);
735 rxi_AllocDataBuf(call->currentPacket, want,
736 RX_PACKET_CLASS_SEND_CBUF);
737 if (call->currentPacket->length > (unsigned)mud)
738 call->currentPacket->length = mud;
739 call->nFree += (call->currentPacket->length - len);
743 /* If the remaining bytes fit in the buffer, then store them
744 * and return. Don't ship a buffer that's full immediately to
745 * the peer--we don't know if it's the last buffer yet */
747 if (!call->currentPacket) {
751 while (nbytes && call->nFree) {
753 t = MIN((int)call->curlen, nbytes);
754 t = MIN((int)call->nFree, t);
755 memcpy(call->curpos, buf, t);
759 call->curlen -= (u_short)t;
760 call->nFree -= (u_short)t;
763 /* need to get another struct iov */
764 if (++call->curvec >= call->currentPacket->niovecs) {
765 /* current packet is full, extend or send it */
769 call->currentPacket->wirevec[call->curvec].iov_base;
771 call->currentPacket->wirevec[call->curvec].iov_len;
774 } /* while bytes to send and room to send them */
776 /* might be out of space now */
779 } else; /* more data to send, so get another packet and keep going */
782 return requestCount - nbytes;
786 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
794 /* Free any packets from the last call to ReadvProc/WritevProc */
795 if (queue_IsNotEmpty(&call->iovq)) {
796 #ifdef RXDEBUG_PACKET
798 #endif /* RXDEBUG_PACKET */
799 rxi_FreePackets(0, &call->iovq);
803 * Most common case: all of the data fits in the current iovec.
804 * We are relying on nFree being zero unless the call is in send mode.
806 tcurlen = (int)call->curlen;
807 tnFree = (int)call->nFree;
808 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
809 tcurpos = call->curpos;
811 memcpy(tcurpos, buf, nbytes);
812 call->curpos = tcurpos + nbytes;
813 call->curlen = (u_short)(tcurlen - nbytes);
814 call->nFree = (u_short)(tnFree - nbytes);
819 bytes = rxi_WriteProc(call, buf, nbytes);
824 /* Optimization for marshalling 32 bit arguments */
826 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
834 if (queue_IsNotEmpty(&call->iovq)) {
835 #ifdef RXDEBUG_PACKET
837 #endif /* RXDEBUG_PACKET */
838 rxi_FreePackets(0, &call->iovq);
842 * Most common case: all of the data fits in the current iovec.
843 * We are relying on nFree being zero unless the call is in send mode.
845 tcurlen = call->curlen;
846 tnFree = call->nFree;
847 if (!call->error && tcurlen >= sizeof(afs_int32)
848 && tnFree >= sizeof(afs_int32)) {
849 tcurpos = call->curpos;
851 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
852 *((afs_int32 *) (tcurpos)) = *value;
854 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
856 call->curpos = tcurpos + sizeof(afs_int32);
857 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
858 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
859 return sizeof(afs_int32);
863 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
868 /* rxi_WritevAlloc -- internal version.
870 * Fill in an iovec to point to data in packet buffers. The application
871 * calls rxi_WritevProc when the buffers are full.
873 * LOCKS USED -- called at netpri.
877 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
880 struct rx_connection *conn = call->conn;
881 struct rx_packet *cp;
884 /* Temporary values, real work is done in rxi_WritevProc */
886 unsigned int tcurvec;
890 requestCount = nbytes;
893 /* Free any packets from the last call to ReadvProc/WritevProc */
894 if (queue_IsNotEmpty(&call->iovq)) {
895 #ifdef RXDEBUG_PACKET
897 #endif /* RXDEBUG_PACKET */
898 rxi_FreePackets(0, &call->iovq);
901 if (call->mode != RX_MODE_SENDING) {
902 if ((conn->type == RX_SERVER_CONNECTION)
903 && (call->mode == RX_MODE_RECEIVING)) {
904 call->mode = RX_MODE_SENDING;
905 if (call->currentPacket) {
906 #ifdef RX_TRACK_PACKETS
907 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
909 rxi_FreePacket(call->currentPacket);
910 call->currentPacket = NULL;
919 /* Set up the iovec to point to data in packet buffers. */
920 tnFree = call->nFree;
921 tcurvec = call->curvec;
922 tcurpos = call->curpos;
923 tcurlen = call->curlen;
924 cp = call->currentPacket;
929 /* current packet is full, allocate a new one */
930 MUTEX_ENTER(&call->lock);
931 cp = rxi_AllocSendPacket(call, nbytes);
932 MUTEX_EXIT(&call->lock);
934 /* out of space, return what we have */
936 return requestCount - nbytes;
938 #ifdef RX_TRACK_PACKETS
939 cp->flags |= RX_PKTFLAG_IOVQ;
941 queue_Append(&call->iovq, cp);
942 #ifdef RXDEBUG_PACKET
944 #endif /* RXDEBUG_PACKET */
948 (char *)cp->wirevec[1].iov_base +
949 call->conn->securityHeaderSize;
950 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
953 if (tnFree < nbytes) {
954 /* try to extend the current packet */
957 mud = rx_MaxUserDataSize(call);
960 want = MIN(nbytes - tnFree, mud - len);
961 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
962 if (cp->length > (unsigned)mud)
964 tnFree += (cp->length - len);
965 if (cp == call->currentPacket) {
966 call->nFree += (cp->length - len);
971 /* fill in the next entry in the iovec */
972 t = MIN(tcurlen, nbytes);
974 iov[nextio].iov_base = tcurpos;
975 iov[nextio].iov_len = t;
983 /* need to get another struct iov */
984 if (++tcurvec >= cp->niovecs) {
985 /* current packet is full, extend it or move on to next packet */
988 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
989 tcurlen = cp->wirevec[tcurvec].iov_len;
992 } while (nbytes && nextio < maxio);
994 return requestCount - nbytes;
998 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1005 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1010 /* rxi_WritevProc -- internal version.
1012 * Send buffers allocated in rxi_WritevAlloc.
1014 * LOCKS USED -- called at netpri.
1017 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1019 #ifdef RX_TRACK_PACKETS
1020 struct rx_packet *p, *np;
1024 struct rx_queue tmpq;
1025 #ifdef RXDEBUG_PACKET
1029 requestCount = nbytes;
1032 MUTEX_ENTER(&call->lock);
1034 call->mode = RX_MODE_ERROR;
1035 } else if (call->mode != RX_MODE_SENDING) {
1036 call->error = RX_PROTOCOL_ERROR;
1038 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1039 rxi_WaitforTQBusy(call);
1040 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1043 call->mode = RX_MODE_ERROR;
1044 MUTEX_EXIT(&call->lock);
1045 if (call->currentPacket) {
1046 #ifdef RX_TRACK_PACKETS
1047 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
1048 call->currentPacket->flags |= RX_PKTFLAG_IOVQ;
1050 queue_Prepend(&call->iovq, call->currentPacket);
1051 #ifdef RXDEBUG_PACKET
1053 #endif /* RXDEBUG_PACKET */
1054 call->currentPacket = NULL;
1056 #ifdef RXDEBUG_PACKET
1058 #endif /* RXDEBUG_PACKET */
1059 rxi_FreePackets(0, &call->iovq);
1063 /* Loop through the I/O vector adjusting packet pointers.
1064 * Place full packets back onto the iovq once they are ready
1065 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1066 * the iovec. We put the loop condition at the end to ensure that
1067 * a zero length write will push a short packet. */
1070 #ifdef RXDEBUG_PACKET
1072 #endif /* RXDEBUG_PACKET */
1074 if (call->nFree == 0 && call->currentPacket) {
1075 clock_NewTime(); /* Bogus: need new time package */
1076 /* The 0, below, specifies that it is not the last packet:
1077 * there will be others. PrepareSendPacket may
1078 * alter the packet length by up to
1079 * conn->securityMaxTrailerSize */
1080 call->bytesSent += call->currentPacket->length;
1081 rxi_PrepareSendPacket(call, call->currentPacket, 0);
1082 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1083 /* PrepareSendPacket drops the call lock */
1084 rxi_WaitforTQBusy(call);
1085 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1086 queue_Append(&tmpq, call->currentPacket);
1087 #ifdef RXDEBUG_PACKET
1089 #endif /* RXDEBUG_PACKET */
1090 call->currentPacket = NULL;
1092 /* The head of the iovq is now the current packet */
1094 if (queue_IsEmpty(&call->iovq)) {
1095 MUTEX_EXIT(&call->lock);
1096 call->error = RX_PROTOCOL_ERROR;
1097 #ifdef RXDEBUG_PACKET
1099 #endif /* RXDEBUG_PACKET */
1100 rxi_FreePackets(0, &tmpq);
1103 call->currentPacket = queue_First(&call->iovq, rx_packet);
1104 queue_Remove(call->currentPacket);
1105 #ifdef RX_TRACK_PACKETS
1106 call->currentPacket->flags &= ~RX_PKTFLAG_IOVQ;
1107 call->currentPacket->flags |= RX_PKTFLAG_CP;
1109 #ifdef RXDEBUG_PACKET
1111 #endif /* RXDEBUG_PACKET */
1112 call->nFree = call->currentPacket->length;
1115 (char *) call->currentPacket->wirevec[1].iov_base +
1116 call->conn->securityHeaderSize;
1118 call->currentPacket->wirevec[1].iov_len -
1119 call->conn->securityHeaderSize;
1124 /* The next iovec should point to the current position */
1125 if (iov[nextio].iov_base != call->curpos
1126 || iov[nextio].iov_len > (int)call->curlen) {
1127 call->error = RX_PROTOCOL_ERROR;
1128 MUTEX_EXIT(&call->lock);
1129 if (call->currentPacket) {
1130 #ifdef RX_TRACK_PACKETS
1131 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
1133 queue_Prepend(&tmpq, call->currentPacket);
1134 #ifdef RXDEBUG_PACKET
1136 #endif /* RXDEBUG_PACKET */
1137 call->currentPacket = NULL;
1139 #ifdef RXDEBUG_PACKET
1141 #endif /* RXDEBUG_PACKET */
1142 rxi_FreePackets(0, &tmpq);
1145 nbytes -= iov[nextio].iov_len;
1146 call->curpos += iov[nextio].iov_len;
1147 call->curlen -= iov[nextio].iov_len;
1148 call->nFree -= iov[nextio].iov_len;
1150 if (call->curlen == 0) {
1151 if (++call->curvec > call->currentPacket->niovecs) {
1155 call->currentPacket->wirevec[call->curvec].iov_base;
1157 call->currentPacket->wirevec[call->curvec].iov_len;
1161 } while (nbytes && nextio < nio);
1163 /* Move the packets from the temporary queue onto the transmit queue.
1164 * We may end up with more than call->twind packets on the queue. */
1166 #ifdef RX_TRACK_PACKETS
1167 for (queue_Scan(&tmpq, p, np, rx_packet))
1169 p->flags |= RX_PKTFLAG_TQ;
1174 call->mode = RX_MODE_ERROR;
1176 queue_SpliceAppend(&call->tq, &tmpq);
1178 /* If the call is in recovery, let it exhaust its current retransmit
1179 * queue before forcing it to send new packets
1181 if (!(call->flags & RX_CALL_FAST_RECOVER)) {
1185 /* Wait for the length of the transmit queue to fall below call->twind */
1186 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1188 call->startWait = clock_Sec();
1189 #ifdef RX_ENABLE_LOCKS
1190 CV_WAIT(&call->cv_twind, &call->lock);
1192 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1193 osi_rxSleep(&call->twind);
1195 call->startWait = 0;
1199 call->mode = RX_MODE_ERROR;
1200 call->currentPacket = NULL;
1201 MUTEX_EXIT(&call->lock);
1202 if (call->currentPacket) {
1203 #ifdef RX_TRACK_PACKETS
1204 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
1206 rxi_FreePacket(call->currentPacket);
1210 MUTEX_EXIT(&call->lock);
1212 return requestCount - nbytes;
1216 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1222 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1227 /* Flush any buffered data to the stream, switch to read mode
1228 * (clients) or to EOF mode (servers)
1230 * LOCKS HELD: called at netpri.
1233 rxi_FlushWrite(struct rx_call *call)
1235 struct rx_packet *cp = NULL;
1237 /* Free any packets from the last call to ReadvProc/WritevProc */
1238 if (queue_IsNotEmpty(&call->iovq)) {
1239 #ifdef RXDEBUG_PACKET
1241 #endif /* RXDEBUG_PACKET */
1242 rxi_FreePackets(0, &call->iovq);
1245 if (call->mode == RX_MODE_SENDING) {
1248 (call->conn->type ==
1249 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1251 #ifdef RX_KERNEL_TRACE
1253 int glockOwner = ISAFS_GLOCK();
1256 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1257 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1264 MUTEX_ENTER(&call->lock);
1266 call->mode = RX_MODE_ERROR;
1268 cp = call->currentPacket;
1271 /* cp->length is only supposed to be the user's data */
1272 /* cp->length was already set to (then-current)
1273 * MaxUserDataSize or less. */
1274 #ifdef RX_TRACK_PACKETS
1275 cp->flags &= ~RX_PKTFLAG_CP;
1277 cp->length -= call->nFree;
1278 call->currentPacket = (struct rx_packet *)0;
1281 cp = rxi_AllocSendPacket(call, 0);
1283 /* Mode can no longer be MODE_SENDING */
1287 cp->niovecs = 2; /* header + space for rxkad stuff */
1291 /* The 1 specifies that this is the last packet */
1292 call->bytesSent += cp->length;
1293 rxi_PrepareSendPacket(call, cp, 1);
1294 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1295 /* PrepareSendPacket drops the call lock */
1296 rxi_WaitforTQBusy(call);
1297 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1298 #ifdef RX_TRACK_PACKETS
1299 cp->flags |= RX_PKTFLAG_TQ;
1301 queue_Append(&call->tq, cp);
1302 #ifdef RXDEBUG_PACKET
1304 #endif /* RXDEBUG_PACKET */
1306 /* If the call is in recovery, let it exhaust its current retransmit
1307 * queue before forcing it to send new packets
1309 if (!(call->flags & RX_CALL_FAST_RECOVER)) {
1312 MUTEX_EXIT(&call->lock);
1316 /* Flush any buffered data to the stream, switch to read mode
1317 * (clients) or to EOF mode (servers) */
1319 rx_FlushWrite(struct rx_call *call)
1323 rxi_FlushWrite(call);