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"
61 #include "rx_globals.h"
62 #include "rx_atomic.h"
63 #include "rx_internal.h"
66 #include "rx_packet.h"
69 /* rxdb_fileID is used to identify the lock location, along with line#. */
70 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
71 #endif /* RX_LOCKS_DB */
73 /* Get the next packet in the receive queue
75 * Dispose of the call's currentPacket, and move the next packet in the
76 * receive queue into the currentPacket field. If the next packet isn't
77 * available, then currentPacket is left NULL.
80 * The RX call to manipulate
82 * 0 on success, an error code on failure
85 * Must be called with the call locked. Unlocks the call if returning
90 rxi_GetNextPacket(struct rx_call *call) {
94 if (call->currentPacket != NULL) {
95 #ifdef RX_TRACK_PACKETS
96 call->currentPacket->flags |= RX_PKTFLAG_CP;
98 rxi_FreePacket(call->currentPacket);
99 call->currentPacket = NULL;
102 if (opr_queue_IsEmpty(&call->rq))
105 /* Check that next packet available is next in sequence */
106 rp = opr_queue_First(&call->rq, struct rx_packet, entry);
107 if (rp->header.seq != call->rnext)
110 opr_queue_Remove(&rp->entry);
111 #ifdef RX_TRACK_PACKETS
112 rp->flags &= ~RX_PKTFLAG_RQ;
114 #ifdef RXDEBUG_PACKET
116 #endif /* RXDEBUG_PACKET */
118 /* RXS_CheckPacket called to undo RXS_PreparePacket's work. It may
119 * reduce the length of the packet by up to conn->maxTrailerSize,
120 * to reflect the length of the data + the header. */
121 if ((error = RXS_CheckPacket(call->conn->securityObject, call, rp))) {
122 /* Used to merely shut down the call, but now we shut down the whole
123 * connection since this may indicate an attempt to hijack it */
125 MUTEX_EXIT(&call->lock);
126 rxi_ConnectionError(call->conn, error);
127 MUTEX_ENTER(&call->conn->conn_data_lock);
128 rp = rxi_SendConnectionAbort(call->conn, rp, 0, 0);
129 MUTEX_EXIT(&call->conn->conn_data_lock);
136 call->currentPacket = rp;
137 #ifdef RX_TRACK_PACKETS
138 call->currentPacket->flags |= RX_PKTFLAG_CP;
140 call->curvec = 1; /* 0th vec is always header */
142 /* begin at the beginning [ more or less ], continue on until the end,
144 call->curpos = (char *)call->currentPacket->wirevec[1].iov_base +
145 call->conn->securityHeaderSize;
146 call->curlen = call->currentPacket->wirevec[1].iov_len -
147 call->conn->securityHeaderSize;
149 call->nLeft = call->currentPacket->length;
150 call->bytesRcvd += call->currentPacket->length;
157 /* rxi_ReadProc -- internal version.
159 * LOCKS USED -- called at netpri
162 rxi_ReadProc(struct rx_call *call, char *buf,
169 /* XXXX took out clock_NewTime from here. Was it needed? */
170 requestCount = nbytes;
172 /* Free any packets from the last call to ReadvProc/WritevProc */
173 if (!opr_queue_IsEmpty(&call->iovq)) {
174 #ifdef RXDEBUG_PACKET
176 #endif /* RXDEBUG_PACKET */
177 rxi_FreePackets(0, &call->iovq);
181 if (call->nLeft == 0) {
182 /* Get next packet */
183 MUTEX_ENTER(&call->lock);
185 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
187 call->mode = RX_MODE_ERROR;
188 MUTEX_EXIT(&call->lock);
191 if (call->mode == RX_MODE_SENDING) {
192 MUTEX_EXIT(&call->lock);
193 rxi_FlushWrite(call);
194 MUTEX_ENTER(&call->lock);
199 code = rxi_GetNextPacket(call);
203 if (call->currentPacket) {
204 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
205 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
206 rxevent_Cancel(&call->delayedAckEvent, call,
207 RX_CALL_REFCOUNT_DELAY);
208 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
210 /* Delay to consolidate ack packets */
211 rxi_PostDelayedAckEvent(call, &rx_hardAckDelay);
218 * If we reach this point either we have no packets in the
219 * receive queue or the next packet in the queue is not the
220 * one we are looking for. There is nothing else for us to
221 * do but wait for another packet to arrive.
224 /* Are there ever going to be any more packets? */
225 if (call->flags & RX_CALL_RECEIVE_DONE) {
226 MUTEX_EXIT(&call->lock);
227 return requestCount - nbytes;
229 /* Wait for in-sequence packet */
230 call->flags |= RX_CALL_READER_WAIT;
232 call->startWait = clock_Sec();
233 while (call->flags & RX_CALL_READER_WAIT) {
234 #ifdef RX_ENABLE_LOCKS
235 CV_WAIT(&call->cv_rq, &call->lock);
237 osi_rxSleep(&call->rq);
242 #ifdef RX_ENABLE_LOCKS
244 MUTEX_EXIT(&call->lock);
247 #endif /* RX_ENABLE_LOCKS */
249 MUTEX_EXIT(&call->lock);
251 /* osi_Assert(cp); */
252 /* MTUXXX this should be replaced by some error-recovery code before shipping */
253 /* yes, the following block is allowed to be the ELSE clause (or not) */
254 /* It's possible for call->nLeft to be smaller than any particular
255 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
256 * reflects the size of the buffer. We have to keep track of the
257 * number of bytes read in the length field of the packet struct. On
258 * the final portion of a received packet, it's almost certain that
259 * call->nLeft will be smaller than the final buffer. */
260 while (nbytes && call->currentPacket) {
261 t = MIN((int)call->curlen, nbytes);
262 t = MIN(t, (int)call->nLeft);
263 memcpy(buf, call->curpos, t);
271 /* out of packet. Get another one. */
272 #ifdef RX_TRACK_PACKETS
273 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
275 rxi_FreePacket(call->currentPacket);
276 call->currentPacket = NULL;
277 } else if (!call->curlen) {
278 /* need to get another struct iov */
279 if (++call->curvec >= call->currentPacket->niovecs) {
280 /* current packet is exhausted, get ready for another */
281 /* don't worry about curvec and stuff, they get set somewhere else */
282 #ifdef RX_TRACK_PACKETS
283 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
285 rxi_FreePacket(call->currentPacket);
286 call->currentPacket = NULL;
290 call->currentPacket->wirevec[call->curvec].iov_base;
292 call->currentPacket->wirevec[call->curvec].iov_len;
297 /* user buffer is full, return */
307 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
312 /* Free any packets from the last call to ReadvProc/WritevProc */
313 if (!opr_queue_IsEmpty(&call->iovq)) {
314 #ifdef RXDEBUG_PACKET
316 #endif /* RXDEBUG_PACKET */
317 rxi_FreePackets(0, &call->iovq);
321 * Most common case, all of the data is in the current iovec.
322 * We are relying on nLeft being zero unless the call is in receive mode.
324 if (!call->error && call->curlen > nbytes && call->nLeft > nbytes) {
325 memcpy(buf, call->curpos, nbytes);
327 call->curpos += nbytes;
328 call->curlen -= nbytes;
329 call->nLeft -= nbytes;
331 if (!call->nLeft && call->currentPacket != NULL) {
332 /* out of packet. Get another one. */
333 rxi_FreePacket(call->currentPacket);
334 call->currentPacket = (struct rx_packet *)0;
340 bytes = rxi_ReadProc(call, buf, nbytes);
345 /* Optimization for unmarshalling 32 bit integers */
347 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
352 /* Free any packets from the last call to ReadvProc/WritevProc */
353 if (!opr_queue_IsEmpty(&call->iovq)) {
354 #ifdef RXDEBUG_PACKET
356 #endif /* RXDEBUG_PACKET */
357 rxi_FreePackets(0, &call->iovq);
361 * Most common case, all of the data is in the current iovec.
362 * We are relying on nLeft being zero unless the call is in receive mode.
364 if (!call->error && call->curlen >= sizeof(afs_int32)
365 && call->nLeft >= sizeof(afs_int32)) {
367 memcpy((char *)value, call->curpos, sizeof(afs_int32));
369 call->curpos += sizeof(afs_int32);
370 call->curlen -= sizeof(afs_int32);
371 call->nLeft -= sizeof(afs_int32);
373 if (!call->nLeft && call->currentPacket != NULL) {
374 /* out of packet. Get another one. */
375 rxi_FreePacket(call->currentPacket);
376 call->currentPacket = (struct rx_packet *)0;
378 return sizeof(afs_int32);
382 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
390 * Uses packets in the receive queue to fill in as much of the
391 * current iovec as possible. Does not block if it runs out
392 * of packets to complete the iovec. Return true if an ack packet
393 * was sent, otherwise return false */
395 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
401 struct iovec *call_iov;
402 struct iovec *cur_iov = NULL;
404 if (call->currentPacket) {
405 cur_iov = &call->currentPacket->wirevec[call->curvec];
407 call_iov = &call->iov[call->iovNext];
409 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
410 if (call->nLeft == 0) {
411 /* Get next packet */
412 code = rxi_GetNextPacket(call);
414 MUTEX_ENTER(&call->lock);
418 if (call->currentPacket) {
419 cur_iov = &call->currentPacket->wirevec[1];
427 /* It's possible for call->nLeft to be smaller than any particular
428 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
429 * reflects the size of the buffer. We have to keep track of the
430 * number of bytes read in the length field of the packet struct. On
431 * the final portion of a received packet, it's almost certain that
432 * call->nLeft will be smaller than the final buffer. */
433 while (call->iovNBytes
434 && call->iovNext < call->iovMax
435 && call->currentPacket) {
437 t = MIN((int)call->curlen, call->iovNBytes);
438 t = MIN(t, (int)call->nLeft);
439 call_iov->iov_base = call->curpos;
440 call_iov->iov_len = t;
443 call->iovNBytes -= t;
449 /* out of packet. Get another one. */
450 #ifdef RX_TRACK_PACKETS
451 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
452 call->currentPacket->flags |= RX_PKTFLAG_IOVQ;
454 opr_queue_Append(&call->iovq, &call->currentPacket->entry);
455 #ifdef RXDEBUG_PACKET
457 #endif /* RXDEBUG_PACKET */
458 call->currentPacket = NULL;
459 } else if (!call->curlen) {
460 /* need to get another struct iov */
461 if (++call->curvec >= call->currentPacket->niovecs) {
462 /* current packet is exhausted, get ready for another */
463 /* don't worry about curvec and stuff, they get set somewhere else */
464 #ifdef RX_TRACK_PACKETS
465 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
466 call->currentPacket->flags |= RX_PKTFLAG_IOVQ;
468 opr_queue_Append(&call->iovq, &call->currentPacket->entry);
469 #ifdef RXDEBUG_PACKET
471 #endif /* RXDEBUG_PACKET */
472 call->currentPacket = NULL;
476 call->curpos = (char *)cur_iov->iov_base;
477 call->curlen = cur_iov->iov_len;
483 /* If we consumed any packets then check whether we need to
484 * send a hard ack. */
485 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
486 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
487 rxevent_Cancel(&call->delayedAckEvent, call,
488 RX_CALL_REFCOUNT_DELAY);
489 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
492 /* Delay to consolidate ack packets */
493 rxi_PostDelayedAckEvent(call, &rx_hardAckDelay);
500 /* rxi_ReadvProc -- internal version.
502 * Fills in an iovec with pointers to the packet buffers. All packets
503 * except the last packet (new current packet) are moved to the iovq
504 * while the application is processing the data.
506 * LOCKS USED -- called at netpri.
509 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
514 /* Free any packets from the last call to ReadvProc/WritevProc */
515 if (!opr_queue_IsEmpty(&call->iovq)) {
516 #ifdef RXDEBUG_PACKET
518 #endif /* RXDEBUG_PACKET */
519 rxi_FreePackets(0, &call->iovq);
522 if (call->mode == RX_MODE_SENDING) {
523 rxi_FlushWrite(call);
526 MUTEX_ENTER(&call->lock);
530 /* Get whatever data is currently available in the receive queue.
531 * If rxi_FillReadVec sends an ack packet then it is possible
532 * that we will receive more data while we drop the call lock
533 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
534 * here to avoid a race with the receive thread if we send
535 * hard acks in rxi_FillReadVec. */
536 call->flags |= RX_CALL_IOVEC_WAIT;
537 call->iovNBytes = nbytes;
538 call->iovMax = maxio;
541 rxi_FillReadVec(call, 0);
543 /* if we need more data then sleep until the receive thread has
544 * filled in the rest. */
545 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
546 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
547 call->flags |= RX_CALL_READER_WAIT;
549 call->startWait = clock_Sec();
550 while (call->flags & RX_CALL_READER_WAIT) {
551 #ifdef RX_ENABLE_LOCKS
552 CV_WAIT(&call->cv_rq, &call->lock);
554 osi_rxSleep(&call->rq);
559 call->flags &= ~RX_CALL_IOVEC_WAIT;
565 *nio = call->iovNext;
566 bytes = nbytes - call->iovNBytes;
567 MUTEX_EXIT(&call->lock);
571 MUTEX_EXIT(&call->lock);
572 call->mode = RX_MODE_ERROR;
577 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
584 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
589 /* rxi_WriteProc -- internal version.
591 * LOCKS USED -- called at netpri
595 rxi_WriteProc(struct rx_call *call, char *buf,
598 struct rx_connection *conn = call->conn;
600 int requestCount = nbytes;
602 /* Free any packets from the last call to ReadvProc/WritevProc */
603 if (!opr_queue_IsEmpty(&call->iovq)) {
604 #ifdef RXDEBUG_PACKET
606 #endif /* RXDEBUG_PACKET */
607 rxi_FreePackets(0, &call->iovq);
610 if (call->mode != RX_MODE_SENDING) {
611 if ((conn->type == RX_SERVER_CONNECTION)
612 && (call->mode == RX_MODE_RECEIVING)) {
613 call->mode = RX_MODE_SENDING;
614 if (call->currentPacket) {
615 #ifdef RX_TRACK_PACKETS
616 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
618 rxi_FreePacket(call->currentPacket);
619 call->currentPacket = NULL;
628 /* Loop condition is checked at end, so that a write of 0 bytes
629 * will force a packet to be created--specially for the case where
630 * there are 0 bytes on the stream, but we must send a packet
633 if (call->nFree == 0) {
634 MUTEX_ENTER(&call->lock);
636 call->mode = RX_MODE_ERROR;
637 if (!call->error && call->currentPacket) {
638 clock_NewTime(); /* Bogus: need new time package */
639 /* The 0, below, specifies that it is not the last packet:
640 * there will be others. PrepareSendPacket may
641 * alter the packet length by up to
642 * conn->securityMaxTrailerSize */
643 call->bytesSent += call->currentPacket->length;
644 rxi_PrepareSendPacket(call, call->currentPacket, 0);
645 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
646 /* PrepareSendPacket drops the call lock */
647 rxi_WaitforTQBusy(call);
648 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
649 #ifdef RX_TRACK_PACKETS
650 call->currentPacket->flags |= RX_PKTFLAG_TQ;
652 opr_queue_Append(&call->tq, &call->currentPacket->entry);
653 #ifdef RXDEBUG_PACKET
655 #endif /* RXDEBUG_PACKET */
656 #ifdef RX_TRACK_PACKETS
657 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
659 call->currentPacket = NULL;
661 /* If the call is in recovery, let it exhaust its current
662 * retransmit queue before forcing it to send new packets
664 if (!(call->flags & (RX_CALL_FAST_RECOVER))) {
667 } else if (call->currentPacket) {
668 #ifdef RX_TRACK_PACKETS
669 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
671 rxi_FreePacket(call->currentPacket);
672 call->currentPacket = NULL;
674 /* Wait for transmit window to open up */
676 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
678 call->startWait = clock_Sec();
680 #ifdef RX_ENABLE_LOCKS
681 CV_WAIT(&call->cv_twind, &call->lock);
683 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
684 osi_rxSleep(&call->twind);
688 #ifdef RX_ENABLE_LOCKS
690 call->mode = RX_MODE_ERROR;
691 MUTEX_EXIT(&call->lock);
694 #endif /* RX_ENABLE_LOCKS */
696 if ((call->currentPacket = rxi_AllocSendPacket(call, nbytes))) {
697 #ifdef RX_TRACK_PACKETS
698 call->currentPacket->flags |= RX_PKTFLAG_CP;
700 call->nFree = call->currentPacket->length;
701 call->curvec = 1; /* 0th vec is always header */
702 /* begin at the beginning [ more or less ], continue
703 * on until the end, then stop. */
705 (char *) call->currentPacket->wirevec[1].iov_base +
706 call->conn->securityHeaderSize;
708 call->currentPacket->wirevec[1].iov_len -
709 call->conn->securityHeaderSize;
712 call->mode = RX_MODE_ERROR;
713 if (call->currentPacket) {
714 #ifdef RX_TRACK_PACKETS
715 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
717 rxi_FreePacket(call->currentPacket);
718 call->currentPacket = NULL;
720 MUTEX_EXIT(&call->lock);
723 MUTEX_EXIT(&call->lock);
726 if (call->currentPacket && (int)call->nFree < nbytes) {
727 /* Try to extend the current buffer */
729 len = call->currentPacket->length;
730 mud = rx_MaxUserDataSize(call);
733 want = MIN(nbytes - (int)call->nFree, mud - len);
734 rxi_AllocDataBuf(call->currentPacket, want,
735 RX_PACKET_CLASS_SEND_CBUF);
736 if (call->currentPacket->length > (unsigned)mud)
737 call->currentPacket->length = mud;
738 call->nFree += (call->currentPacket->length - len);
742 /* If the remaining bytes fit in the buffer, then store them
743 * and return. Don't ship a buffer that's full immediately to
744 * the peer--we don't know if it's the last buffer yet */
746 if (!call->currentPacket) {
750 while (nbytes && call->nFree) {
752 t = MIN((int)call->curlen, nbytes);
753 t = MIN((int)call->nFree, t);
754 memcpy(call->curpos, buf, t);
758 call->curlen -= (u_short)t;
759 call->nFree -= (u_short)t;
762 /* need to get another struct iov */
763 if (++call->curvec >= call->currentPacket->niovecs) {
764 /* current packet is full, extend or send it */
768 call->currentPacket->wirevec[call->curvec].iov_base;
770 call->currentPacket->wirevec[call->curvec].iov_len;
773 } /* while bytes to send and room to send them */
775 /* might be out of space now */
778 } else; /* more data to send, so get another packet and keep going */
781 return requestCount - nbytes;
785 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
793 /* Free any packets from the last call to ReadvProc/WritevProc */
794 if (!opr_queue_IsEmpty(&call->iovq)) {
795 #ifdef RXDEBUG_PACKET
797 #endif /* RXDEBUG_PACKET */
798 rxi_FreePackets(0, &call->iovq);
802 * Most common case: all of the data fits in the current iovec.
803 * We are relying on nFree being zero unless the call is in send mode.
805 tcurlen = (int)call->curlen;
806 tnFree = (int)call->nFree;
807 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
808 tcurpos = call->curpos;
810 memcpy(tcurpos, buf, nbytes);
811 call->curpos = tcurpos + nbytes;
812 call->curlen = (u_short)(tcurlen - nbytes);
813 call->nFree = (u_short)(tnFree - nbytes);
818 bytes = rxi_WriteProc(call, buf, nbytes);
823 /* Optimization for marshalling 32 bit arguments */
825 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
833 if (!opr_queue_IsEmpty(&call->iovq)) {
834 #ifdef RXDEBUG_PACKET
836 #endif /* RXDEBUG_PACKET */
837 rxi_FreePackets(0, &call->iovq);
841 * Most common case: all of the data fits in the current iovec.
842 * We are relying on nFree being zero unless the call is in send mode.
844 tcurlen = call->curlen;
845 tnFree = call->nFree;
846 if (!call->error && tcurlen >= sizeof(afs_int32)
847 && tnFree >= sizeof(afs_int32)) {
848 tcurpos = call->curpos;
850 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
851 *((afs_int32 *) (tcurpos)) = *value;
853 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
855 call->curpos = tcurpos + sizeof(afs_int32);
856 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
857 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
858 return sizeof(afs_int32);
862 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
867 /* rxi_WritevAlloc -- internal version.
869 * Fill in an iovec to point to data in packet buffers. The application
870 * calls rxi_WritevProc when the buffers are full.
872 * LOCKS USED -- called at netpri.
876 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
879 struct rx_connection *conn = call->conn;
880 struct rx_packet *cp;
883 /* Temporary values, real work is done in rxi_WritevProc */
885 unsigned int tcurvec;
889 requestCount = nbytes;
892 /* Free any packets from the last call to ReadvProc/WritevProc */
893 if (!opr_queue_IsEmpty(&call->iovq)) {
894 #ifdef RXDEBUG_PACKET
896 #endif /* RXDEBUG_PACKET */
897 rxi_FreePackets(0, &call->iovq);
900 if (call->mode != RX_MODE_SENDING) {
901 if ((conn->type == RX_SERVER_CONNECTION)
902 && (call->mode == RX_MODE_RECEIVING)) {
903 call->mode = RX_MODE_SENDING;
904 if (call->currentPacket) {
905 #ifdef RX_TRACK_PACKETS
906 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
908 rxi_FreePacket(call->currentPacket);
909 call->currentPacket = NULL;
918 /* Set up the iovec to point to data in packet buffers. */
919 tnFree = call->nFree;
920 tcurvec = call->curvec;
921 tcurpos = call->curpos;
922 tcurlen = call->curlen;
923 cp = call->currentPacket;
928 /* current packet is full, allocate a new one */
929 MUTEX_ENTER(&call->lock);
930 cp = rxi_AllocSendPacket(call, nbytes);
931 MUTEX_EXIT(&call->lock);
933 /* out of space, return what we have */
935 return requestCount - nbytes;
937 #ifdef RX_TRACK_PACKETS
938 cp->flags |= RX_PKTFLAG_IOVQ;
940 opr_queue_Append(&call->iovq, &cp->entry);
941 #ifdef RXDEBUG_PACKET
943 #endif /* RXDEBUG_PACKET */
947 (char *)cp->wirevec[1].iov_base +
948 call->conn->securityHeaderSize;
949 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
952 if (tnFree < nbytes) {
953 /* try to extend the current packet */
956 mud = rx_MaxUserDataSize(call);
959 want = MIN(nbytes - tnFree, mud - len);
960 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
961 if (cp->length > (unsigned)mud)
963 tnFree += (cp->length - len);
964 if (cp == call->currentPacket) {
965 call->nFree += (cp->length - len);
970 /* fill in the next entry in the iovec */
971 t = MIN(tcurlen, nbytes);
973 iov[nextio].iov_base = tcurpos;
974 iov[nextio].iov_len = t;
982 /* need to get another struct iov */
983 if (++tcurvec >= cp->niovecs) {
984 /* current packet is full, extend it or move on to next packet */
987 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
988 tcurlen = cp->wirevec[tcurvec].iov_len;
991 } while (nbytes && nextio < maxio);
993 return requestCount - nbytes;
997 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1004 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1009 /* rxi_WritevProc -- internal version.
1011 * Send buffers allocated in rxi_WritevAlloc.
1013 * LOCKS USED -- called at netpri.
1016 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1018 #ifdef RX_TRACK_PACKETS
1019 struct opr_queue *cursor;
1023 struct opr_queue tmpq;
1024 #ifdef RXDEBUG_PACKET
1028 requestCount = nbytes;
1031 MUTEX_ENTER(&call->lock);
1033 call->mode = RX_MODE_ERROR;
1034 } else if (call->mode != RX_MODE_SENDING) {
1035 call->error = RX_PROTOCOL_ERROR;
1037 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1038 rxi_WaitforTQBusy(call);
1039 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1042 call->mode = RX_MODE_ERROR;
1043 MUTEX_EXIT(&call->lock);
1044 if (call->currentPacket) {
1045 #ifdef RX_TRACK_PACKETS
1046 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
1047 call->currentPacket->flags |= RX_PKTFLAG_IOVQ;
1049 opr_queue_Prepend(&call->iovq, &call->currentPacket->entry);
1050 #ifdef RXDEBUG_PACKET
1052 #endif /* RXDEBUG_PACKET */
1053 call->currentPacket = NULL;
1055 #ifdef RXDEBUG_PACKET
1057 #endif /* RXDEBUG_PACKET */
1058 rxi_FreePackets(0, &call->iovq);
1062 /* Loop through the I/O vector adjusting packet pointers.
1063 * Place full packets back onto the iovq once they are ready
1064 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1065 * the iovec. We put the loop condition at the end to ensure that
1066 * a zero length write will push a short packet. */
1068 opr_queue_Init(&tmpq);
1069 #ifdef RXDEBUG_PACKET
1071 #endif /* RXDEBUG_PACKET */
1073 if (call->nFree == 0 && call->currentPacket) {
1074 clock_NewTime(); /* Bogus: need new time package */
1075 /* The 0, below, specifies that it is not the last packet:
1076 * there will be others. PrepareSendPacket may
1077 * alter the packet length by up to
1078 * conn->securityMaxTrailerSize */
1079 call->bytesSent += call->currentPacket->length;
1080 rxi_PrepareSendPacket(call, call->currentPacket, 0);
1081 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1082 /* PrepareSendPacket drops the call lock */
1083 rxi_WaitforTQBusy(call);
1084 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1085 opr_queue_Append(&tmpq, &call->currentPacket->entry);
1086 #ifdef RXDEBUG_PACKET
1088 #endif /* RXDEBUG_PACKET */
1089 call->currentPacket = NULL;
1091 /* The head of the iovq is now the current packet */
1093 if (opr_queue_IsEmpty(&call->iovq)) {
1094 MUTEX_EXIT(&call->lock);
1095 call->error = RX_PROTOCOL_ERROR;
1096 #ifdef RXDEBUG_PACKET
1098 #endif /* RXDEBUG_PACKET */
1099 rxi_FreePackets(0, &tmpq);
1102 call->currentPacket = opr_queue_First(&call->iovq,
1105 opr_queue_Remove(&call->currentPacket->entry);
1106 #ifdef RX_TRACK_PACKETS
1107 call->currentPacket->flags &= ~RX_PKTFLAG_IOVQ;
1108 call->currentPacket->flags |= RX_PKTFLAG_CP;
1110 #ifdef RXDEBUG_PACKET
1112 #endif /* RXDEBUG_PACKET */
1113 call->nFree = call->currentPacket->length;
1116 (char *) call->currentPacket->wirevec[1].iov_base +
1117 call->conn->securityHeaderSize;
1119 call->currentPacket->wirevec[1].iov_len -
1120 call->conn->securityHeaderSize;
1125 /* The next iovec should point to the current position */
1126 if (iov[nextio].iov_base != call->curpos
1127 || iov[nextio].iov_len > (int)call->curlen) {
1128 call->error = RX_PROTOCOL_ERROR;
1129 MUTEX_EXIT(&call->lock);
1130 if (call->currentPacket) {
1131 #ifdef RX_TRACK_PACKETS
1132 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
1134 opr_queue_Prepend(&tmpq, &call->currentPacket->entry);
1135 #ifdef RXDEBUG_PACKET
1137 #endif /* RXDEBUG_PACKET */
1138 call->currentPacket = NULL;
1140 #ifdef RXDEBUG_PACKET
1142 #endif /* RXDEBUG_PACKET */
1143 rxi_FreePackets(0, &tmpq);
1146 nbytes -= iov[nextio].iov_len;
1147 call->curpos += iov[nextio].iov_len;
1148 call->curlen -= iov[nextio].iov_len;
1149 call->nFree -= iov[nextio].iov_len;
1151 if (call->curlen == 0) {
1152 if (++call->curvec > call->currentPacket->niovecs) {
1156 call->currentPacket->wirevec[call->curvec].iov_base;
1158 call->currentPacket->wirevec[call->curvec].iov_len;
1162 } while (nbytes && nextio < nio);
1164 /* Move the packets from the temporary queue onto the transmit queue.
1165 * We may end up with more than call->twind packets on the queue. */
1167 #ifdef RX_TRACK_PACKETS
1168 for (opr_queue_Scan(&tmpq, cursor))
1170 struct rx_packet *p = opr_queue_Entry(cursor, struct rx_packet, entry);
1171 p->flags |= RX_PKTFLAG_TQ;
1175 call->mode = RX_MODE_ERROR;
1177 opr_queue_SpliceAppend(&call->tq, &tmpq);
1179 /* If the call is in recovery, let it exhaust its current retransmit
1180 * queue before forcing it to send new packets
1182 if (!(call->flags & RX_CALL_FAST_RECOVER)) {
1186 /* Wait for the length of the transmit queue to fall below call->twind */
1187 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1189 call->startWait = clock_Sec();
1190 #ifdef RX_ENABLE_LOCKS
1191 CV_WAIT(&call->cv_twind, &call->lock);
1193 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1194 osi_rxSleep(&call->twind);
1196 call->startWait = 0;
1200 call->mode = RX_MODE_ERROR;
1201 call->currentPacket = NULL;
1202 MUTEX_EXIT(&call->lock);
1203 if (call->currentPacket) {
1204 #ifdef RX_TRACK_PACKETS
1205 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
1207 rxi_FreePacket(call->currentPacket);
1211 MUTEX_EXIT(&call->lock);
1213 return requestCount - nbytes;
1217 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1223 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1228 /* Flush any buffered data to the stream, switch to read mode
1229 * (clients) or to EOF mode (servers)
1231 * LOCKS HELD: called at netpri.
1234 rxi_FlushWrite(struct rx_call *call)
1236 struct rx_packet *cp = NULL;
1238 /* Free any packets from the last call to ReadvProc/WritevProc */
1239 if (!opr_queue_IsEmpty(&call->iovq)) {
1240 #ifdef RXDEBUG_PACKET
1242 #endif /* RXDEBUG_PACKET */
1243 rxi_FreePackets(0, &call->iovq);
1246 if (call->mode == RX_MODE_SENDING) {
1249 (call->conn->type ==
1250 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1252 #ifdef RX_KERNEL_TRACE
1254 int glockOwner = ISAFS_GLOCK();
1257 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1258 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1265 MUTEX_ENTER(&call->lock);
1267 call->mode = RX_MODE_ERROR;
1269 cp = call->currentPacket;
1272 /* cp->length is only supposed to be the user's data */
1273 /* cp->length was already set to (then-current)
1274 * MaxUserDataSize or less. */
1275 #ifdef RX_TRACK_PACKETS
1276 cp->flags &= ~RX_PKTFLAG_CP;
1278 cp->length -= call->nFree;
1279 call->currentPacket = (struct rx_packet *)0;
1282 cp = rxi_AllocSendPacket(call, 0);
1284 /* Mode can no longer be MODE_SENDING */
1288 cp->niovecs = 2; /* header + space for rxkad stuff */
1292 /* The 1 specifies that this is the last packet */
1293 call->bytesSent += cp->length;
1294 rxi_PrepareSendPacket(call, cp, 1);
1295 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1296 /* PrepareSendPacket drops the call lock */
1297 rxi_WaitforTQBusy(call);
1298 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1299 #ifdef RX_TRACK_PACKETS
1300 cp->flags |= RX_PKTFLAG_TQ;
1302 opr_queue_Append(&call->tq, &cp->entry);
1303 #ifdef RXDEBUG_PACKET
1305 #endif /* RXDEBUG_PACKET */
1307 /* If the call is in recovery, let it exhaust its current retransmit
1308 * queue before forcing it to send new packets
1310 if (!(call->flags & RX_CALL_FAST_RECOVER)) {
1313 MUTEX_EXIT(&call->lock);
1317 /* Flush any buffered data to the stream, switch to read mode
1318 * (clients) or to EOF mode (servers) */
1320 rx_FlushWrite(struct rx_call *call)
1324 rxi_FlushWrite(call);