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"
67 /* rxdb_fileID is used to identify the lock location, along with line#. */
68 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
69 #endif /* RX_LOCKS_DB */
70 /* rxi_ReadProc -- internal version.
72 * LOCKS USED -- called at netpri
75 rxi_ReadProc(struct rx_call *call, char *buf,
78 struct rx_packet *cp = call->currentPacket;
83 /* XXXX took out clock_NewTime from here. Was it needed? */
84 requestCount = nbytes;
86 /* Free any packets from the last call to ReadvProc/WritevProc */
87 if (queue_IsNotEmpty(&call->iovq)) {
90 #endif /* RXDEBUG_PACKET */
91 rxi_FreePackets(0, &call->iovq);
95 if (call->nLeft == 0) {
97 MUTEX_ENTER(&call->lock);
99 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
101 call->mode = RX_MODE_ERROR;
102 MUTEX_EXIT(&call->lock);
105 if (call->mode == RX_MODE_SENDING) {
106 MUTEX_EXIT(&call->lock);
107 rxi_FlushWrite(call);
108 MUTEX_ENTER(&call->lock);
112 if (queue_IsNotEmpty(&call->rq)) {
113 /* Check that next packet available is next in sequence */
114 rp = queue_First(&call->rq, rx_packet);
115 if (rp->header.seq == call->rnext) {
117 struct rx_connection *conn = call->conn;
119 #ifdef RX_TRACK_PACKETS
120 rp->flags &= ~RX_PKTFLAG_RQ;
122 #ifdef RXDEBUG_PACKET
124 #endif /* RXDEBUG_PACKET */
126 /* RXS_CheckPacket called to undo RXS_PreparePacket's
127 * work. It may reduce the length of the packet by up
128 * to conn->maxTrailerSize, to reflect the length of the
129 * data + the header. */
131 RXS_CheckPacket(conn->securityObject, call,
133 /* Used to merely shut down the call, but now we
134 * shut down the whole connection since this may
135 * indicate an attempt to hijack it */
137 MUTEX_EXIT(&call->lock);
138 rxi_ConnectionError(conn, error);
139 MUTEX_ENTER(&conn->conn_data_lock);
140 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
141 MUTEX_EXIT(&conn->conn_data_lock);
147 cp = call->currentPacket = rp;
148 #ifdef RX_TRACK_PACKETS
149 call->currentPacket->flags |= RX_PKTFLAG_CP;
151 call->curvec = 1; /* 0th vec is always header */
152 /* begin at the beginning [ more or less ], continue
153 * on until the end, then stop. */
155 (char *)cp->wirevec[1].iov_base +
156 call->conn->securityHeaderSize;
158 cp->wirevec[1].iov_len -
159 call->conn->securityHeaderSize;
161 /* Notice that this code works correctly if the data
162 * size is 0 (which it may be--no reply arguments from
163 * server, for example). This relies heavily on the
164 * fact that the code below immediately frees the packet
165 * (no yields, etc.). If it didn't, this would be a
166 * problem because a value of zero for call->nLeft
167 * normally means that there is no read packet */
168 call->nLeft = cp->length;
169 hadd32(call->bytesRcvd, cp->length);
171 /* Send a hard ack for every rxi_HardAckRate+1 packets
172 * consumed. Otherwise schedule an event to send
173 * the hard ack later on.
176 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
177 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
178 rxevent_Cancel(&call->delayedAckEvent, call,
179 RX_CALL_REFCOUNT_DELAY);
180 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
182 /* Delay to consolidate ack packets */
183 rxi_PostDelayedAckEvent(call,
192 * If we reach this point either we have no packets in the
193 * receive queue or the next packet in the queue is not the
194 * one we are looking for. There is nothing else for us to
195 * do but wait for another packet to arrive.
198 /* Are there ever going to be any more packets? */
199 if (call->flags & RX_CALL_RECEIVE_DONE) {
200 MUTEX_EXIT(&call->lock);
201 return requestCount - nbytes;
203 /* Wait for in-sequence packet */
204 call->flags |= RX_CALL_READER_WAIT;
206 call->startWait = clock_Sec();
207 while (call->flags & RX_CALL_READER_WAIT) {
208 #ifdef RX_ENABLE_LOCKS
209 CV_WAIT(&call->cv_rq, &call->lock);
211 osi_rxSleep(&call->rq);
214 cp = call->currentPacket;
217 #ifdef RX_ENABLE_LOCKS
219 MUTEX_EXIT(&call->lock);
222 #endif /* RX_ENABLE_LOCKS */
224 MUTEX_EXIT(&call->lock);
226 /* osi_Assert(cp); */
227 /* MTUXXX this should be replaced by some error-recovery code before shipping */
228 /* yes, the following block is allowed to be the ELSE clause (or not) */
229 /* It's possible for call->nLeft to be smaller than any particular
230 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
231 * reflects the size of the buffer. We have to keep track of the
232 * number of bytes read in the length field of the packet struct. On
233 * the final portion of a received packet, it's almost certain that
234 * call->nLeft will be smaller than the final buffer. */
235 while (nbytes && cp) {
236 t = MIN((int)call->curlen, nbytes);
237 t = MIN(t, (int)call->nLeft);
238 memcpy(buf, call->curpos, t);
246 /* out of packet. Get another one. */
247 #ifdef RX_TRACK_PACKETS
248 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
251 cp = call->currentPacket = (struct rx_packet *)0;
252 } else if (!call->curlen) {
253 /* need to get another struct iov */
254 if (++call->curvec >= cp->niovecs) {
255 /* current packet is exhausted, get ready for another */
256 /* don't worry about curvec and stuff, they get set somewhere else */
257 #ifdef RX_TRACK_PACKETS
258 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
261 cp = call->currentPacket = (struct rx_packet *)0;
265 (char *)cp->wirevec[call->curvec].iov_base;
266 call->curlen = cp->wirevec[call->curvec].iov_len;
271 /* user buffer is full, return */
281 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
289 /* Free any packets from the last call to ReadvProc/WritevProc */
290 if (!queue_IsEmpty(&call->iovq)) {
291 #ifdef RXDEBUG_PACKET
293 #endif /* RXDEBUG_PACKET */
294 rxi_FreePackets(0, &call->iovq);
298 * Most common case, all of the data is in the current iovec.
299 * We are relying on nLeft being zero unless the call is in receive mode.
301 tcurlen = call->curlen;
302 tnLeft = call->nLeft;
303 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
304 tcurpos = call->curpos;
305 memcpy(buf, tcurpos, nbytes);
307 call->curpos = tcurpos + nbytes;
308 call->curlen = tcurlen - nbytes;
309 call->nLeft = tnLeft - nbytes;
311 if (!call->nLeft && call->currentPacket != NULL) {
312 /* out of packet. Get another one. */
313 rxi_FreePacket(call->currentPacket);
314 call->currentPacket = (struct rx_packet *)0;
320 bytes = rxi_ReadProc(call, buf, nbytes);
325 /* Optimization for unmarshalling 32 bit integers */
327 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
335 /* Free any packets from the last call to ReadvProc/WritevProc */
336 if (!queue_IsEmpty(&call->iovq)) {
337 #ifdef RXDEBUG_PACKET
339 #endif /* RXDEBUG_PACKET */
340 rxi_FreePackets(0, &call->iovq);
344 * Most common case, all of the data is in the current iovec.
345 * We are relying on nLeft being zero unless the call is in receive mode.
347 tcurlen = call->curlen;
348 tnLeft = call->nLeft;
349 if (!call->error && tcurlen >= sizeof(afs_int32)
350 && tnLeft >= sizeof(afs_int32)) {
351 tcurpos = call->curpos;
353 memcpy((char *)value, tcurpos, sizeof(afs_int32));
355 call->curpos = tcurpos + sizeof(afs_int32);
356 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
357 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
358 if (!call->nLeft && call->currentPacket != NULL) {
359 /* out of packet. Get another one. */
360 rxi_FreePacket(call->currentPacket);
361 call->currentPacket = (struct rx_packet *)0;
363 return sizeof(afs_int32);
367 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
375 * Uses packets in the receive queue to fill in as much of the
376 * current iovec as possible. Does not block if it runs out
377 * of packets to complete the iovec. Return true if an ack packet
378 * was sent, otherwise return false */
380 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
385 struct rx_packet *rp;
386 struct rx_packet *curp;
387 struct iovec *call_iov;
388 struct iovec *cur_iov = NULL;
390 curp = call->currentPacket;
392 cur_iov = &curp->wirevec[call->curvec];
394 call_iov = &call->iov[call->iovNext];
396 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
397 if (call->nLeft == 0) {
398 /* Get next packet */
399 if (queue_IsNotEmpty(&call->rq)) {
400 /* Check that next packet available is next in sequence */
401 rp = queue_First(&call->rq, rx_packet);
402 if (rp->header.seq == call->rnext) {
404 struct rx_connection *conn = call->conn;
406 #ifdef RX_TRACK_PACKETS
407 rp->flags &= ~RX_PKTFLAG_RQ;
409 #ifdef RXDEBUG_PACKET
411 #endif /* RXDEBUG_PACKET */
413 /* RXS_CheckPacket called to undo RXS_PreparePacket's
414 * work. It may reduce the length of the packet by up
415 * to conn->maxTrailerSize, to reflect the length of the
416 * data + the header. */
418 RXS_CheckPacket(conn->securityObject, call, rp))) {
419 /* Used to merely shut down the call, but now we
420 * shut down the whole connection since this may
421 * indicate an attempt to hijack it */
423 MUTEX_EXIT(&call->lock);
424 rxi_ConnectionError(conn, error);
425 MUTEX_ENTER(&conn->conn_data_lock);
426 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
427 MUTEX_EXIT(&conn->conn_data_lock);
429 MUTEX_ENTER(&call->lock);
434 curp = call->currentPacket = rp;
435 #ifdef RX_TRACK_PACKETS
436 call->currentPacket->flags |= RX_PKTFLAG_CP;
438 call->curvec = 1; /* 0th vec is always header */
439 cur_iov = &curp->wirevec[1];
440 /* begin at the beginning [ more or less ], continue
441 * on until the end, then stop. */
443 (char *)curp->wirevec[1].iov_base +
444 call->conn->securityHeaderSize;
446 curp->wirevec[1].iov_len -
447 call->conn->securityHeaderSize;
449 /* Notice that this code works correctly if the data
450 * size is 0 (which it may be--no reply arguments from
451 * server, for example). This relies heavily on the
452 * fact that the code below immediately frees the packet
453 * (no yields, etc.). If it didn't, this would be a
454 * problem because a value of zero for call->nLeft
455 * normally means that there is no read packet */
456 call->nLeft = curp->length;
457 hadd32(call->bytesRcvd, curp->length);
459 /* Send a hard ack for every rxi_HardAckRate+1 packets
460 * consumed. Otherwise schedule an event to send
461 * the hard ack later on.
471 /* It's possible for call->nLeft to be smaller than any particular
472 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
473 * reflects the size of the buffer. We have to keep track of the
474 * number of bytes read in the length field of the packet struct. On
475 * the final portion of a received packet, it's almost certain that
476 * call->nLeft will be smaller than the final buffer. */
477 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
479 t = MIN((int)call->curlen, call->iovNBytes);
480 t = MIN(t, (int)call->nLeft);
481 call_iov->iov_base = call->curpos;
482 call_iov->iov_len = t;
485 call->iovNBytes -= t;
491 /* out of packet. Get another one. */
492 #ifdef RX_TRACK_PACKETS
493 curp->flags &= ~RX_PKTFLAG_CP;
494 curp->flags |= RX_PKTFLAG_IOVQ;
496 queue_Append(&call->iovq, curp);
497 #ifdef RXDEBUG_PACKET
499 #endif /* RXDEBUG_PACKET */
500 curp = call->currentPacket = (struct rx_packet *)0;
501 } else if (!call->curlen) {
502 /* need to get another struct iov */
503 if (++call->curvec >= curp->niovecs) {
504 /* current packet is exhausted, get ready for another */
505 /* don't worry about curvec and stuff, they get set somewhere else */
506 #ifdef RX_TRACK_PACKETS
507 curp->flags &= ~RX_PKTFLAG_CP;
508 curp->flags |= RX_PKTFLAG_IOVQ;
510 queue_Append(&call->iovq, curp);
511 #ifdef RXDEBUG_PACKET
513 #endif /* RXDEBUG_PACKET */
514 curp = call->currentPacket = (struct rx_packet *)0;
518 call->curpos = (char *)cur_iov->iov_base;
519 call->curlen = cur_iov->iov_len;
525 /* If we consumed any packets then check whether we need to
526 * send a hard ack. */
527 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
528 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
529 rxevent_Cancel(&call->delayedAckEvent, call,
530 RX_CALL_REFCOUNT_DELAY);
531 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
534 /* Delay to consolidate ack packets */
535 rxi_PostDelayedAckEvent(call, &rx_hardAckDelay);
542 /* rxi_ReadvProc -- internal version.
544 * Fills in an iovec with pointers to the packet buffers. All packets
545 * except the last packet (new current packet) are moved to the iovq
546 * while the application is processing the data.
548 * LOCKS USED -- called at netpri.
551 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
556 /* Free any packets from the last call to ReadvProc/WritevProc */
557 if (queue_IsNotEmpty(&call->iovq)) {
558 #ifdef RXDEBUG_PACKET
560 #endif /* RXDEBUG_PACKET */
561 rxi_FreePackets(0, &call->iovq);
564 if (call->mode == RX_MODE_SENDING) {
565 rxi_FlushWrite(call);
568 MUTEX_ENTER(&call->lock);
572 /* Get whatever data is currently available in the receive queue.
573 * If rxi_FillReadVec sends an ack packet then it is possible
574 * that we will receive more data while we drop the call lock
575 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
576 * here to avoid a race with the receive thread if we send
577 * hard acks in rxi_FillReadVec. */
578 call->flags |= RX_CALL_IOVEC_WAIT;
579 call->iovNBytes = nbytes;
580 call->iovMax = maxio;
583 rxi_FillReadVec(call, 0);
585 /* if we need more data then sleep until the receive thread has
586 * filled in the rest. */
587 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
588 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
589 call->flags |= RX_CALL_READER_WAIT;
591 call->startWait = clock_Sec();
592 while (call->flags & RX_CALL_READER_WAIT) {
593 #ifdef RX_ENABLE_LOCKS
594 CV_WAIT(&call->cv_rq, &call->lock);
596 osi_rxSleep(&call->rq);
601 call->flags &= ~RX_CALL_IOVEC_WAIT;
607 *nio = call->iovNext;
608 bytes = nbytes - call->iovNBytes;
609 MUTEX_EXIT(&call->lock);
613 MUTEX_EXIT(&call->lock);
614 call->mode = RX_MODE_ERROR;
619 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
626 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
631 /* rxi_WriteProc -- internal version.
633 * LOCKS USED -- called at netpri
637 rxi_WriteProc(struct rx_call *call, char *buf,
640 struct rx_connection *conn = call->conn;
641 struct rx_packet *cp = call->currentPacket;
643 int requestCount = nbytes;
645 /* Free any packets from the last call to ReadvProc/WritevProc */
646 if (queue_IsNotEmpty(&call->iovq)) {
647 #ifdef RXDEBUG_PACKET
649 #endif /* RXDEBUG_PACKET */
650 rxi_FreePackets(0, &call->iovq);
653 if (call->mode != RX_MODE_SENDING) {
654 if ((conn->type == RX_SERVER_CONNECTION)
655 && (call->mode == RX_MODE_RECEIVING)) {
656 call->mode = RX_MODE_SENDING;
658 #ifdef RX_TRACK_PACKETS
659 cp->flags &= ~RX_PKTFLAG_CP;
662 cp = call->currentPacket = (struct rx_packet *)0;
671 /* Loop condition is checked at end, so that a write of 0 bytes
672 * will force a packet to be created--specially for the case where
673 * there are 0 bytes on the stream, but we must send a packet
676 if (call->nFree == 0) {
677 MUTEX_ENTER(&call->lock);
678 cp = call->currentPacket;
680 call->mode = RX_MODE_ERROR;
681 if (!call->error && cp) {
682 /* Clear the current packet now so that if
683 * we are forced to wait and drop the lock
684 * the packet we are planning on using
687 #ifdef RX_TRACK_PACKETS
688 cp->flags &= ~RX_PKTFLAG_CP;
690 call->currentPacket = (struct rx_packet *)0;
691 clock_NewTime(); /* Bogus: need new time package */
692 /* The 0, below, specifies that it is not the last packet:
693 * there will be others. PrepareSendPacket may
694 * alter the packet length by up to
695 * conn->securityMaxTrailerSize */
696 hadd32(call->bytesSent, cp->length);
697 rxi_PrepareSendPacket(call, cp, 0);
698 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
699 /* PrepareSendPacket drops the call lock */
700 rxi_WaitforTQBusy(call);
701 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
702 #ifdef RX_TRACK_PACKETS
703 cp->flags |= RX_PKTFLAG_TQ;
705 queue_Append(&call->tq, cp);
706 #ifdef RXDEBUG_PACKET
708 #endif /* RXDEBUG_PACKET */
709 cp = (struct rx_packet *)0;
710 /* If the call is in recovery, let it exhaust its current
711 * retransmit queue before forcing it to send new packets
713 if (!(call->flags & (RX_CALL_FAST_RECOVER))) {
717 #ifdef RX_TRACK_PACKETS
718 cp->flags &= ~RX_PKTFLAG_CP;
721 cp = call->currentPacket = (struct rx_packet *)0;
723 /* Wait for transmit window to open up */
725 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
727 call->startWait = clock_Sec();
729 #ifdef RX_ENABLE_LOCKS
730 CV_WAIT(&call->cv_twind, &call->lock);
732 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
733 osi_rxSleep(&call->twind);
737 #ifdef RX_ENABLE_LOCKS
739 call->mode = RX_MODE_ERROR;
740 MUTEX_EXIT(&call->lock);
743 #endif /* RX_ENABLE_LOCKS */
745 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
746 #ifdef RX_TRACK_PACKETS
747 cp->flags |= RX_PKTFLAG_CP;
749 call->currentPacket = cp;
750 call->nFree = cp->length;
751 call->curvec = 1; /* 0th vec is always header */
752 /* begin at the beginning [ more or less ], continue
753 * on until the end, then stop. */
755 (char *)cp->wirevec[1].iov_base +
756 call->conn->securityHeaderSize;
758 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
761 call->mode = RX_MODE_ERROR;
763 #ifdef RX_TRACK_PACKETS
764 cp->flags &= ~RX_PKTFLAG_CP;
767 call->currentPacket = NULL;
769 MUTEX_EXIT(&call->lock);
772 MUTEX_EXIT(&call->lock);
775 if (cp && (int)call->nFree < nbytes) {
776 /* Try to extend the current buffer */
779 mud = rx_MaxUserDataSize(call);
782 want = MIN(nbytes - (int)call->nFree, mud - len);
783 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
784 if (cp->length > (unsigned)mud)
786 call->nFree += (cp->length - len);
790 /* If the remaining bytes fit in the buffer, then store them
791 * and return. Don't ship a buffer that's full immediately to
792 * the peer--we don't know if it's the last buffer yet */
798 while (nbytes && call->nFree) {
800 t = MIN((int)call->curlen, nbytes);
801 t = MIN((int)call->nFree, t);
802 memcpy(call->curpos, buf, t);
806 call->curlen -= (u_short)t;
807 call->nFree -= (u_short)t;
810 /* need to get another struct iov */
811 if (++call->curvec >= cp->niovecs) {
812 /* current packet is full, extend or send it */
815 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
816 call->curlen = cp->wirevec[call->curvec].iov_len;
819 } /* while bytes to send and room to send them */
821 /* might be out of space now */
824 } else; /* more data to send, so get another packet and keep going */
827 return requestCount - nbytes;
831 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
839 /* Free any packets from the last call to ReadvProc/WritevProc */
840 if (queue_IsNotEmpty(&call->iovq)) {
841 #ifdef RXDEBUG_PACKET
843 #endif /* RXDEBUG_PACKET */
844 rxi_FreePackets(0, &call->iovq);
848 * Most common case: all of the data fits in the current iovec.
849 * We are relying on nFree being zero unless the call is in send mode.
851 tcurlen = (int)call->curlen;
852 tnFree = (int)call->nFree;
853 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
854 tcurpos = call->curpos;
856 memcpy(tcurpos, buf, nbytes);
857 call->curpos = tcurpos + nbytes;
858 call->curlen = (u_short)(tcurlen - nbytes);
859 call->nFree = (u_short)(tnFree - nbytes);
864 bytes = rxi_WriteProc(call, buf, nbytes);
869 /* Optimization for marshalling 32 bit arguments */
871 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
879 if (queue_IsNotEmpty(&call->iovq)) {
880 #ifdef RXDEBUG_PACKET
882 #endif /* RXDEBUG_PACKET */
883 rxi_FreePackets(0, &call->iovq);
887 * Most common case: all of the data fits in the current iovec.
888 * We are relying on nFree being zero unless the call is in send mode.
890 tcurlen = call->curlen;
891 tnFree = call->nFree;
892 if (!call->error && tcurlen >= sizeof(afs_int32)
893 && tnFree >= sizeof(afs_int32)) {
894 tcurpos = call->curpos;
896 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
897 *((afs_int32 *) (tcurpos)) = *value;
899 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
901 call->curpos = tcurpos + sizeof(afs_int32);
902 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
903 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
904 return sizeof(afs_int32);
908 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
913 /* rxi_WritevAlloc -- internal version.
915 * Fill in an iovec to point to data in packet buffers. The application
916 * calls rxi_WritevProc when the buffers are full.
918 * LOCKS USED -- called at netpri.
922 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
925 struct rx_connection *conn = call->conn;
926 struct rx_packet *cp = call->currentPacket;
929 /* Temporary values, real work is done in rxi_WritevProc */
931 unsigned int tcurvec;
935 requestCount = nbytes;
938 /* Free any packets from the last call to ReadvProc/WritevProc */
939 if (queue_IsNotEmpty(&call->iovq)) {
940 #ifdef RXDEBUG_PACKET
942 #endif /* RXDEBUG_PACKET */
943 rxi_FreePackets(0, &call->iovq);
946 if (call->mode != RX_MODE_SENDING) {
947 if ((conn->type == RX_SERVER_CONNECTION)
948 && (call->mode == RX_MODE_RECEIVING)) {
949 call->mode = RX_MODE_SENDING;
951 #ifdef RX_TRACK_PACKETS
952 cp->flags &= ~RX_PKTFLAG_CP;
955 cp = call->currentPacket = (struct rx_packet *)0;
964 /* Set up the iovec to point to data in packet buffers. */
965 tnFree = call->nFree;
966 tcurvec = call->curvec;
967 tcurpos = call->curpos;
968 tcurlen = call->curlen;
973 /* current packet is full, allocate a new one */
974 MUTEX_ENTER(&call->lock);
975 cp = rxi_AllocSendPacket(call, nbytes);
976 MUTEX_EXIT(&call->lock);
978 /* out of space, return what we have */
980 return requestCount - nbytes;
982 #ifdef RX_TRACK_PACKETS
983 cp->flags |= RX_PKTFLAG_IOVQ;
985 queue_Append(&call->iovq, cp);
986 #ifdef RXDEBUG_PACKET
988 #endif /* RXDEBUG_PACKET */
992 (char *)cp->wirevec[1].iov_base +
993 call->conn->securityHeaderSize;
994 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
997 if (tnFree < nbytes) {
998 /* try to extend the current packet */
1001 mud = rx_MaxUserDataSize(call);
1004 want = MIN(nbytes - tnFree, mud - len);
1005 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1006 if (cp->length > (unsigned)mud)
1008 tnFree += (cp->length - len);
1009 if (cp == call->currentPacket) {
1010 call->nFree += (cp->length - len);
1015 /* fill in the next entry in the iovec */
1016 t = MIN(tcurlen, nbytes);
1018 iov[nextio].iov_base = tcurpos;
1019 iov[nextio].iov_len = t;
1027 /* need to get another struct iov */
1028 if (++tcurvec >= cp->niovecs) {
1029 /* current packet is full, extend it or move on to next packet */
1032 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1033 tcurlen = cp->wirevec[tcurvec].iov_len;
1036 } while (nbytes && nextio < maxio);
1038 return requestCount - nbytes;
1042 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1049 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1054 /* rxi_WritevProc -- internal version.
1056 * Send buffers allocated in rxi_WritevAlloc.
1058 * LOCKS USED -- called at netpri.
1061 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1063 struct rx_packet *cp = NULL;
1064 #ifdef RX_TRACK_PACKETS
1065 struct rx_packet *p, *np;
1069 struct rx_queue tmpq;
1070 #ifdef RXDEBUG_PACKET
1074 requestCount = nbytes;
1077 MUTEX_ENTER(&call->lock);
1079 call->mode = RX_MODE_ERROR;
1080 } else if (call->mode != RX_MODE_SENDING) {
1081 call->error = RX_PROTOCOL_ERROR;
1083 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1084 rxi_WaitforTQBusy(call);
1085 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1086 cp = call->currentPacket;
1089 call->mode = RX_MODE_ERROR;
1090 MUTEX_EXIT(&call->lock);
1092 #ifdef RX_TRACK_PACKETS
1093 cp->flags &= ~RX_PKTFLAG_CP;
1094 cp->flags |= RX_PKTFLAG_IOVQ;
1096 queue_Prepend(&call->iovq, cp);
1097 #ifdef RXDEBUG_PACKET
1099 #endif /* RXDEBUG_PACKET */
1100 call->currentPacket = (struct rx_packet *)0;
1102 #ifdef RXDEBUG_PACKET
1104 #endif /* RXDEBUG_PACKET */
1105 rxi_FreePackets(0, &call->iovq);
1109 /* Loop through the I/O vector adjusting packet pointers.
1110 * Place full packets back onto the iovq once they are ready
1111 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1112 * the iovec. We put the loop condition at the end to ensure that
1113 * a zero length write will push a short packet. */
1116 #ifdef RXDEBUG_PACKET
1118 #endif /* RXDEBUG_PACKET */
1120 if (call->nFree == 0 && cp) {
1121 clock_NewTime(); /* Bogus: need new time package */
1122 /* The 0, below, specifies that it is not the last packet:
1123 * there will be others. PrepareSendPacket may
1124 * alter the packet length by up to
1125 * conn->securityMaxTrailerSize */
1126 hadd32(call->bytesSent, cp->length);
1127 rxi_PrepareSendPacket(call, cp, 0);
1128 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1129 /* PrepareSendPacket drops the call lock */
1130 rxi_WaitforTQBusy(call);
1131 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1132 queue_Append(&tmpq, cp);
1133 #ifdef RXDEBUG_PACKET
1135 #endif /* RXDEBUG_PACKET */
1136 cp = call->currentPacket = (struct rx_packet *)0;
1138 /* The head of the iovq is now the current packet */
1140 if (queue_IsEmpty(&call->iovq)) {
1141 MUTEX_EXIT(&call->lock);
1142 call->error = RX_PROTOCOL_ERROR;
1143 #ifdef RXDEBUG_PACKET
1145 #endif /* RXDEBUG_PACKET */
1146 rxi_FreePackets(0, &tmpq);
1149 cp = queue_First(&call->iovq, rx_packet);
1151 #ifdef RX_TRACK_PACKETS
1152 cp->flags &= ~RX_PKTFLAG_IOVQ;
1154 #ifdef RXDEBUG_PACKET
1156 #endif /* RXDEBUG_PACKET */
1157 #ifdef RX_TRACK_PACKETS
1158 cp->flags |= RX_PKTFLAG_CP;
1160 call->currentPacket = cp;
1161 call->nFree = cp->length;
1164 (char *)cp->wirevec[1].iov_base +
1165 call->conn->securityHeaderSize;
1167 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1172 /* The next iovec should point to the current position */
1173 if (iov[nextio].iov_base != call->curpos
1174 || iov[nextio].iov_len > (int)call->curlen) {
1175 call->error = RX_PROTOCOL_ERROR;
1176 MUTEX_EXIT(&call->lock);
1178 #ifdef RX_TRACK_PACKETS
1179 cp->flags &= ~RX_PKTFLAG_CP;
1181 queue_Prepend(&tmpq, cp);
1182 #ifdef RXDEBUG_PACKET
1184 #endif /* RXDEBUG_PACKET */
1185 cp = call->currentPacket = (struct rx_packet *)0;
1187 #ifdef RXDEBUG_PACKET
1189 #endif /* RXDEBUG_PACKET */
1190 rxi_FreePackets(0, &tmpq);
1193 nbytes -= iov[nextio].iov_len;
1194 call->curpos += iov[nextio].iov_len;
1195 call->curlen -= iov[nextio].iov_len;
1196 call->nFree -= iov[nextio].iov_len;
1198 if (call->curlen == 0) {
1199 if (++call->curvec > cp->niovecs) {
1202 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1203 call->curlen = cp->wirevec[call->curvec].iov_len;
1207 } while (nbytes && nextio < nio);
1209 /* Move the packets from the temporary queue onto the transmit queue.
1210 * We may end up with more than call->twind packets on the queue. */
1212 #ifdef RX_TRACK_PACKETS
1213 for (queue_Scan(&tmpq, p, np, rx_packet))
1215 p->flags |= RX_PKTFLAG_TQ;
1220 call->mode = RX_MODE_ERROR;
1222 queue_SpliceAppend(&call->tq, &tmpq);
1224 /* If the call is in recovery, let it exhaust its current retransmit
1225 * queue before forcing it to send new packets
1227 if (!(call->flags & RX_CALL_FAST_RECOVER)) {
1231 /* Wait for the length of the transmit queue to fall below call->twind */
1232 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1234 call->startWait = clock_Sec();
1235 #ifdef RX_ENABLE_LOCKS
1236 CV_WAIT(&call->cv_twind, &call->lock);
1238 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1239 osi_rxSleep(&call->twind);
1241 call->startWait = 0;
1244 /* cp is no longer valid since we may have given up the lock */
1245 cp = call->currentPacket;
1248 call->mode = RX_MODE_ERROR;
1249 call->currentPacket = NULL;
1250 MUTEX_EXIT(&call->lock);
1252 #ifdef RX_TRACK_PACKETS
1253 cp->flags &= ~RX_PKTFLAG_CP;
1259 MUTEX_EXIT(&call->lock);
1261 return requestCount - nbytes;
1265 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1271 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1276 /* Flush any buffered data to the stream, switch to read mode
1277 * (clients) or to EOF mode (servers)
1279 * LOCKS HELD: called at netpri.
1282 rxi_FlushWrite(struct rx_call *call)
1284 struct rx_packet *cp = NULL;
1286 /* Free any packets from the last call to ReadvProc/WritevProc */
1287 if (queue_IsNotEmpty(&call->iovq)) {
1288 #ifdef RXDEBUG_PACKET
1290 #endif /* RXDEBUG_PACKET */
1291 rxi_FreePackets(0, &call->iovq);
1294 if (call->mode == RX_MODE_SENDING) {
1297 (call->conn->type ==
1298 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1300 #ifdef RX_KERNEL_TRACE
1302 int glockOwner = ISAFS_GLOCK();
1305 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1306 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1313 MUTEX_ENTER(&call->lock);
1315 call->mode = RX_MODE_ERROR;
1317 cp = call->currentPacket;
1320 /* cp->length is only supposed to be the user's data */
1321 /* cp->length was already set to (then-current)
1322 * MaxUserDataSize or less. */
1323 #ifdef RX_TRACK_PACKETS
1324 cp->flags &= ~RX_PKTFLAG_CP;
1326 cp->length -= call->nFree;
1327 call->currentPacket = (struct rx_packet *)0;
1330 cp = rxi_AllocSendPacket(call, 0);
1332 /* Mode can no longer be MODE_SENDING */
1336 cp->niovecs = 2; /* header + space for rxkad stuff */
1340 /* The 1 specifies that this is the last packet */
1341 hadd32(call->bytesSent, cp->length);
1342 rxi_PrepareSendPacket(call, cp, 1);
1343 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1344 /* PrepareSendPacket drops the call lock */
1345 rxi_WaitforTQBusy(call);
1346 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1347 #ifdef RX_TRACK_PACKETS
1348 cp->flags |= RX_PKTFLAG_TQ;
1350 queue_Append(&call->tq, cp);
1351 #ifdef RXDEBUG_PACKET
1353 #endif /* RXDEBUG_PACKET */
1355 /* If the call is in recovery, let it exhaust its current retransmit
1356 * queue before forcing it to send new packets
1358 if (!(call->flags & RX_CALL_FAST_RECOVER)) {
1361 MUTEX_EXIT(&call->lock);
1365 /* Flush any buffered data to the stream, switch to read mode
1366 * (clients) or to EOF mode (servers) */
1368 rx_FlushWrite(struct rx_call *call)
1372 rxi_FlushWrite(call);