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"
55 # include <sys/types.h>
58 # include <winsock2.h>
59 # else /* !AFS_NT40_ENV */
60 # include <sys/socket.h>
61 # include <sys/file.h>
63 # include <netinet/in.h>
64 # include <sys/stat.h>
65 # include <sys/time.h>
67 # endif /* !AFS_NT40_ENV */
73 #include "rx_globals.h"
76 /* rxdb_fileID is used to identify the lock location, along with line#. */
77 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
78 #endif /* RX_LOCKS_DB */
79 /* rxi_ReadProc -- internal version.
81 * LOCKS USED -- called at netpri
84 rxi_ReadProc(struct rx_call *call, char *buf,
87 struct rx_packet *cp = call->currentPacket;
92 /* XXXX took out clock_NewTime from here. Was it needed? */
93 requestCount = nbytes;
95 /* Free any packets from the last call to ReadvProc/WritevProc */
96 if (queue_IsNotEmpty(&call->iovq)) {
99 #endif /* RXDEBUG_PACKET */
100 rxi_FreePackets(0, &call->iovq);
104 if (call->nLeft == 0) {
105 /* Get next packet */
106 MUTEX_ENTER(&call->lock);
108 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
110 call->mode = RX_MODE_ERROR;
111 MUTEX_EXIT(&call->lock);
114 if (call->mode == RX_MODE_SENDING) {
115 MUTEX_EXIT(&call->lock);
116 rxi_FlushWrite(call);
117 MUTEX_ENTER(&call->lock);
121 if (queue_IsNotEmpty(&call->rq)) {
122 /* Check that next packet available is next in sequence */
123 rp = queue_First(&call->rq, rx_packet);
124 if (rp->header.seq == call->rnext) {
126 struct rx_connection *conn = call->conn;
128 #ifdef RX_TRACK_PACKETS
129 rp->flags &= ~RX_PKTFLAG_RQ;
131 #ifdef RXDEBUG_PACKET
133 #endif /* RXDEBUG_PACKET */
135 /* RXS_CheckPacket called to undo RXS_PreparePacket's
136 * work. It may reduce the length of the packet by up
137 * to conn->maxTrailerSize, to reflect the length of the
138 * data + the header. */
140 RXS_CheckPacket(conn->securityObject, call,
142 /* Used to merely shut down the call, but now we
143 * shut down the whole connection since this may
144 * indicate an attempt to hijack it */
146 MUTEX_EXIT(&call->lock);
147 rxi_ConnectionError(conn, error);
148 MUTEX_ENTER(&conn->conn_data_lock);
149 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
150 MUTEX_EXIT(&conn->conn_data_lock);
156 cp = call->currentPacket = rp;
157 #ifdef RX_TRACK_PACKETS
158 call->currentPacket->flags |= RX_PKTFLAG_CP;
160 call->curvec = 1; /* 0th vec is always header */
161 /* begin at the beginning [ more or less ], continue
162 * on until the end, then stop. */
164 (char *)cp->wirevec[1].iov_base +
165 call->conn->securityHeaderSize;
167 cp->wirevec[1].iov_len -
168 call->conn->securityHeaderSize;
170 /* Notice that this code works correctly if the data
171 * size is 0 (which it may be--no reply arguments from
172 * server, for example). This relies heavily on the
173 * fact that the code below immediately frees the packet
174 * (no yields, etc.). If it didn't, this would be a
175 * problem because a value of zero for call->nLeft
176 * normally means that there is no read packet */
177 call->nLeft = cp->length;
178 hadd32(call->bytesRcvd, cp->length);
180 /* Send a hard ack for every rxi_HardAckRate+1 packets
181 * consumed. Otherwise schedule an event to send
182 * the hard ack later on.
185 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
186 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
187 rxevent_Cancel(call->delayedAckEvent, call,
188 RX_CALL_REFCOUNT_DELAY);
189 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
191 struct clock when, now;
194 /* Delay to consolidate ack packets */
195 clock_Add(&when, &rx_hardAckDelay);
196 if (!call->delayedAckEvent
197 || clock_Gt(&call->delayedAckEvent->
199 rxevent_Cancel(call->delayedAckEvent,
201 RX_CALL_REFCOUNT_DELAY);
202 MUTEX_ENTER(&rx_refcnt_mutex);
203 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
204 MUTEX_EXIT(&rx_refcnt_mutex);
205 call->delayedAckEvent =
206 rxevent_PostNow(&when, &now,
207 rxi_SendDelayedAck, call,
217 * If we reach this point either we have no packets in the
218 * receive queue or the next packet in the queue is not the
219 * one we are looking for. There is nothing else for us to
220 * do but wait for another packet to arrive.
223 /* Are there ever going to be any more packets? */
224 if (call->flags & RX_CALL_RECEIVE_DONE) {
225 MUTEX_EXIT(&call->lock);
226 return requestCount - nbytes;
228 /* Wait for in-sequence packet */
229 call->flags |= RX_CALL_READER_WAIT;
231 call->startWait = clock_Sec();
232 while (call->flags & RX_CALL_READER_WAIT) {
233 #ifdef RX_ENABLE_LOCKS
234 CV_WAIT(&call->cv_rq, &call->lock);
236 osi_rxSleep(&call->rq);
239 cp = call->currentPacket;
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 && cp) {
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;
276 cp = call->currentPacket = (struct rx_packet *)0;
277 } else if (!call->curlen) {
278 /* need to get another struct iov */
279 if (++call->curvec >= cp->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;
286 cp = call->currentPacket = (struct rx_packet *)0;
290 (char *)cp->wirevec[call->curvec].iov_base;
291 call->curlen = cp->wirevec[call->curvec].iov_len;
296 /* user buffer is full, return */
306 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
314 /* Free any packets from the last call to ReadvProc/WritevProc */
315 if (!queue_IsEmpty(&call->iovq)) {
316 #ifdef RXDEBUG_PACKET
318 #endif /* RXDEBUG_PACKET */
319 rxi_FreePackets(0, &call->iovq);
323 * Most common case, all of the data is in the current iovec.
324 * We are relying on nLeft being zero unless the call is in receive mode.
326 tcurlen = call->curlen;
327 tnLeft = call->nLeft;
328 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
329 tcurpos = call->curpos;
330 memcpy(buf, tcurpos, nbytes);
332 call->curpos = tcurpos + nbytes;
333 call->curlen = tcurlen - nbytes;
334 call->nLeft = tnLeft - nbytes;
336 if (!call->nLeft && call->currentPacket != NULL) {
337 /* out of packet. Get another one. */
338 rxi_FreePacket(call->currentPacket);
339 call->currentPacket = (struct rx_packet *)0;
345 bytes = rxi_ReadProc(call, buf, nbytes);
350 /* Optimization for unmarshalling 32 bit integers */
352 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
360 /* Free any packets from the last call to ReadvProc/WritevProc */
361 if (!queue_IsEmpty(&call->iovq)) {
362 #ifdef RXDEBUG_PACKET
364 #endif /* RXDEBUG_PACKET */
365 rxi_FreePackets(0, &call->iovq);
369 * Most common case, all of the data is in the current iovec.
370 * We are relying on nLeft being zero unless the call is in receive mode.
372 tcurlen = call->curlen;
373 tnLeft = call->nLeft;
374 if (!call->error && tcurlen >= sizeof(afs_int32)
375 && tnLeft >= sizeof(afs_int32)) {
376 tcurpos = call->curpos;
378 memcpy((char *)value, tcurpos, sizeof(afs_int32));
380 call->curpos = tcurpos + sizeof(afs_int32);
381 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
382 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
383 if (!call->nLeft && call->currentPacket != NULL) {
384 /* out of packet. Get another one. */
385 rxi_FreePacket(call->currentPacket);
386 call->currentPacket = (struct rx_packet *)0;
388 return sizeof(afs_int32);
392 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
400 * Uses packets in the receive queue to fill in as much of the
401 * current iovec as possible. Does not block if it runs out
402 * of packets to complete the iovec. Return true if an ack packet
403 * was sent, otherwise return false */
405 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
410 struct rx_packet *rp;
411 struct rx_packet *curp;
412 struct iovec *call_iov;
413 struct iovec *cur_iov = NULL;
415 curp = call->currentPacket;
417 cur_iov = &curp->wirevec[call->curvec];
419 call_iov = &call->iov[call->iovNext];
421 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
422 if (call->nLeft == 0) {
423 /* Get next packet */
424 if (queue_IsNotEmpty(&call->rq)) {
425 /* Check that next packet available is next in sequence */
426 rp = queue_First(&call->rq, rx_packet);
427 if (rp->header.seq == call->rnext) {
429 struct rx_connection *conn = call->conn;
431 #ifdef RX_TRACK_PACKETS
432 rp->flags &= ~RX_PKTFLAG_RQ;
434 #ifdef RXDEBUG_PACKET
436 #endif /* RXDEBUG_PACKET */
438 /* RXS_CheckPacket called to undo RXS_PreparePacket's
439 * work. It may reduce the length of the packet by up
440 * to conn->maxTrailerSize, to reflect the length of the
441 * data + the header. */
443 RXS_CheckPacket(conn->securityObject, call, rp))) {
444 /* Used to merely shut down the call, but now we
445 * shut down the whole connection since this may
446 * indicate an attempt to hijack it */
448 MUTEX_EXIT(&call->lock);
449 rxi_ConnectionError(conn, error);
450 MUTEX_ENTER(&conn->conn_data_lock);
451 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
452 MUTEX_EXIT(&conn->conn_data_lock);
454 MUTEX_ENTER(&call->lock);
459 curp = call->currentPacket = rp;
460 #ifdef RX_TRACK_PACKETS
461 call->currentPacket->flags |= RX_PKTFLAG_CP;
463 call->curvec = 1; /* 0th vec is always header */
464 cur_iov = &curp->wirevec[1];
465 /* begin at the beginning [ more or less ], continue
466 * on until the end, then stop. */
468 (char *)curp->wirevec[1].iov_base +
469 call->conn->securityHeaderSize;
471 curp->wirevec[1].iov_len -
472 call->conn->securityHeaderSize;
474 /* Notice that this code works correctly if the data
475 * size is 0 (which it may be--no reply arguments from
476 * server, for example). This relies heavily on the
477 * fact that the code below immediately frees the packet
478 * (no yields, etc.). If it didn't, this would be a
479 * problem because a value of zero for call->nLeft
480 * normally means that there is no read packet */
481 call->nLeft = curp->length;
482 hadd32(call->bytesRcvd, curp->length);
484 /* Send a hard ack for every rxi_HardAckRate+1 packets
485 * consumed. Otherwise schedule an event to send
486 * the hard ack later on.
496 /* It's possible for call->nLeft to be smaller than any particular
497 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
498 * reflects the size of the buffer. We have to keep track of the
499 * number of bytes read in the length field of the packet struct. On
500 * the final portion of a received packet, it's almost certain that
501 * call->nLeft will be smaller than the final buffer. */
502 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
504 t = MIN((int)call->curlen, call->iovNBytes);
505 t = MIN(t, (int)call->nLeft);
506 call_iov->iov_base = call->curpos;
507 call_iov->iov_len = t;
510 call->iovNBytes -= t;
516 /* out of packet. Get another one. */
517 #ifdef RX_TRACK_PACKETS
518 curp->flags &= ~RX_PKTFLAG_CP;
519 curp->flags |= RX_PKTFLAG_IOVQ;
521 queue_Append(&call->iovq, curp);
522 #ifdef RXDEBUG_PACKET
524 #endif /* RXDEBUG_PACKET */
525 curp = call->currentPacket = (struct rx_packet *)0;
526 } else if (!call->curlen) {
527 /* need to get another struct iov */
528 if (++call->curvec >= curp->niovecs) {
529 /* current packet is exhausted, get ready for another */
530 /* don't worry about curvec and stuff, they get set somewhere else */
531 #ifdef RX_TRACK_PACKETS
532 curp->flags &= ~RX_PKTFLAG_CP;
533 curp->flags |= RX_PKTFLAG_IOVQ;
535 queue_Append(&call->iovq, curp);
536 #ifdef RXDEBUG_PACKET
538 #endif /* RXDEBUG_PACKET */
539 curp = call->currentPacket = (struct rx_packet *)0;
543 call->curpos = (char *)cur_iov->iov_base;
544 call->curlen = cur_iov->iov_len;
550 /* If we consumed any packets then check whether we need to
551 * send a hard ack. */
552 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
553 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
554 rxevent_Cancel(call->delayedAckEvent, call,
555 RX_CALL_REFCOUNT_DELAY);
556 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
559 struct clock when, now;
562 /* Delay to consolidate ack packets */
563 clock_Add(&when, &rx_hardAckDelay);
564 if (!call->delayedAckEvent
565 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
566 rxevent_Cancel(call->delayedAckEvent, call,
567 RX_CALL_REFCOUNT_DELAY);
568 MUTEX_ENTER(&rx_refcnt_mutex);
569 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
570 MUTEX_EXIT(&rx_refcnt_mutex);
571 call->delayedAckEvent =
572 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
580 /* rxi_ReadvProc -- internal version.
582 * Fills in an iovec with pointers to the packet buffers. All packets
583 * except the last packet (new current packet) are moved to the iovq
584 * while the application is processing the data.
586 * LOCKS USED -- called at netpri.
589 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
594 /* Free any packets from the last call to ReadvProc/WritevProc */
595 if (queue_IsNotEmpty(&call->iovq)) {
596 #ifdef RXDEBUG_PACKET
598 #endif /* RXDEBUG_PACKET */
599 rxi_FreePackets(0, &call->iovq);
602 if (call->mode == RX_MODE_SENDING) {
603 rxi_FlushWrite(call);
606 MUTEX_ENTER(&call->lock);
610 /* Get whatever data is currently available in the receive queue.
611 * If rxi_FillReadVec sends an ack packet then it is possible
612 * that we will receive more data while we drop the call lock
613 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
614 * here to avoid a race with the receive thread if we send
615 * hard acks in rxi_FillReadVec. */
616 call->flags |= RX_CALL_IOVEC_WAIT;
617 call->iovNBytes = nbytes;
618 call->iovMax = maxio;
621 rxi_FillReadVec(call, 0);
623 /* if we need more data then sleep until the receive thread has
624 * filled in the rest. */
625 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
626 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
627 call->flags |= RX_CALL_READER_WAIT;
629 call->startWait = clock_Sec();
630 while (call->flags & RX_CALL_READER_WAIT) {
631 #ifdef RX_ENABLE_LOCKS
632 CV_WAIT(&call->cv_rq, &call->lock);
634 osi_rxSleep(&call->rq);
639 call->flags &= ~RX_CALL_IOVEC_WAIT;
645 *nio = call->iovNext;
646 bytes = nbytes - call->iovNBytes;
647 MUTEX_EXIT(&call->lock);
651 MUTEX_EXIT(&call->lock);
652 call->mode = RX_MODE_ERROR;
657 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
664 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
669 /* rxi_WriteProc -- internal version.
671 * LOCKS USED -- called at netpri
675 rxi_WriteProc(struct rx_call *call, char *buf,
678 struct rx_connection *conn = call->conn;
679 struct rx_packet *cp = call->currentPacket;
681 int requestCount = nbytes;
683 /* Free any packets from the last call to ReadvProc/WritevProc */
684 if (queue_IsNotEmpty(&call->iovq)) {
685 #ifdef RXDEBUG_PACKET
687 #endif /* RXDEBUG_PACKET */
688 rxi_FreePackets(0, &call->iovq);
691 if (call->mode != RX_MODE_SENDING) {
692 if ((conn->type == RX_SERVER_CONNECTION)
693 && (call->mode == RX_MODE_RECEIVING)) {
694 call->mode = RX_MODE_SENDING;
696 #ifdef RX_TRACK_PACKETS
697 cp->flags &= ~RX_PKTFLAG_CP;
700 cp = call->currentPacket = (struct rx_packet *)0;
709 /* Loop condition is checked at end, so that a write of 0 bytes
710 * will force a packet to be created--specially for the case where
711 * there are 0 bytes on the stream, but we must send a packet
714 if (call->nFree == 0) {
715 MUTEX_ENTER(&call->lock);
716 cp = call->currentPacket;
718 call->mode = RX_MODE_ERROR;
719 if (!call->error && cp) {
720 /* Clear the current packet now so that if
721 * we are forced to wait and drop the lock
722 * the packet we are planning on using
725 #ifdef RX_TRACK_PACKETS
726 cp->flags &= ~RX_PKTFLAG_CP;
728 call->currentPacket = (struct rx_packet *)0;
729 clock_NewTime(); /* Bogus: need new time package */
730 /* The 0, below, specifies that it is not the last packet:
731 * there will be others. PrepareSendPacket may
732 * alter the packet length by up to
733 * conn->securityMaxTrailerSize */
734 hadd32(call->bytesSent, cp->length);
735 rxi_PrepareSendPacket(call, cp, 0);
736 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
737 /* PrepareSendPacket drops the call lock */
738 rxi_WaitforTQBusy(call);
739 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
740 #ifdef RX_TRACK_PACKETS
741 cp->flags |= RX_PKTFLAG_TQ;
743 queue_Append(&call->tq, cp);
744 #ifdef RXDEBUG_PACKET
746 #endif /* RXDEBUG_PACKET */
747 cp = (struct rx_packet *)0;
750 flags & (RX_CALL_FAST_RECOVER |
751 RX_CALL_FAST_RECOVER_WAIT))) {
752 rxi_Start(0, call, 0, 0);
755 #ifdef RX_TRACK_PACKETS
756 cp->flags &= ~RX_PKTFLAG_CP;
759 cp = call->currentPacket = (struct rx_packet *)0;
761 /* Wait for transmit window to open up */
763 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
765 call->startWait = clock_Sec();
767 #ifdef RX_ENABLE_LOCKS
768 CV_WAIT(&call->cv_twind, &call->lock);
770 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
771 osi_rxSleep(&call->twind);
775 #ifdef RX_ENABLE_LOCKS
777 call->mode = RX_MODE_ERROR;
778 MUTEX_EXIT(&call->lock);
781 #endif /* RX_ENABLE_LOCKS */
783 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
784 #ifdef RX_TRACK_PACKETS
785 cp->flags |= RX_PKTFLAG_CP;
787 call->currentPacket = cp;
788 call->nFree = cp->length;
789 call->curvec = 1; /* 0th vec is always header */
790 /* begin at the beginning [ more or less ], continue
791 * on until the end, then stop. */
793 (char *)cp->wirevec[1].iov_base +
794 call->conn->securityHeaderSize;
796 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
799 call->mode = RX_MODE_ERROR;
801 #ifdef RX_TRACK_PACKETS
802 cp->flags &= ~RX_PKTFLAG_CP;
805 call->currentPacket = NULL;
807 MUTEX_EXIT(&call->lock);
810 MUTEX_EXIT(&call->lock);
813 if (cp && (int)call->nFree < nbytes) {
814 /* Try to extend the current buffer */
817 mud = rx_MaxUserDataSize(call);
820 want = MIN(nbytes - (int)call->nFree, mud - len);
821 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
822 if (cp->length > (unsigned)mud)
824 call->nFree += (cp->length - len);
828 /* If the remaining bytes fit in the buffer, then store them
829 * and return. Don't ship a buffer that's full immediately to
830 * the peer--we don't know if it's the last buffer yet */
836 while (nbytes && call->nFree) {
838 t = MIN((int)call->curlen, nbytes);
839 t = MIN((int)call->nFree, t);
840 memcpy(call->curpos, buf, t);
844 call->curlen -= (u_short)t;
845 call->nFree -= (u_short)t;
848 /* need to get another struct iov */
849 if (++call->curvec >= cp->niovecs) {
850 /* current packet is full, extend or send it */
853 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
854 call->curlen = cp->wirevec[call->curvec].iov_len;
857 } /* while bytes to send and room to send them */
859 /* might be out of space now */
862 } else; /* more data to send, so get another packet and keep going */
865 return requestCount - nbytes;
869 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
877 /* Free any packets from the last call to ReadvProc/WritevProc */
878 if (queue_IsNotEmpty(&call->iovq)) {
879 #ifdef RXDEBUG_PACKET
881 #endif /* RXDEBUG_PACKET */
882 rxi_FreePackets(0, &call->iovq);
886 * Most common case: all of the data fits in the current iovec.
887 * We are relying on nFree being zero unless the call is in send mode.
889 tcurlen = (int)call->curlen;
890 tnFree = (int)call->nFree;
891 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
892 tcurpos = call->curpos;
894 memcpy(tcurpos, buf, nbytes);
895 call->curpos = tcurpos + nbytes;
896 call->curlen = (u_short)(tcurlen - nbytes);
897 call->nFree = (u_short)(tnFree - nbytes);
902 bytes = rxi_WriteProc(call, buf, nbytes);
907 /* Optimization for marshalling 32 bit arguments */
909 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
917 if (queue_IsNotEmpty(&call->iovq)) {
918 #ifdef RXDEBUG_PACKET
920 #endif /* RXDEBUG_PACKET */
921 rxi_FreePackets(0, &call->iovq);
925 * Most common case: all of the data fits in the current iovec.
926 * We are relying on nFree being zero unless the call is in send mode.
928 tcurlen = call->curlen;
929 tnFree = call->nFree;
930 if (!call->error && tcurlen >= sizeof(afs_int32)
931 && tnFree >= sizeof(afs_int32)) {
932 tcurpos = call->curpos;
934 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
935 *((afs_int32 *) (tcurpos)) = *value;
937 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
939 call->curpos = tcurpos + sizeof(afs_int32);
940 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
941 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
942 return sizeof(afs_int32);
946 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
951 /* rxi_WritevAlloc -- internal version.
953 * Fill in an iovec to point to data in packet buffers. The application
954 * calls rxi_WritevProc when the buffers are full.
956 * LOCKS USED -- called at netpri.
960 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
963 struct rx_connection *conn = call->conn;
964 struct rx_packet *cp = call->currentPacket;
967 /* Temporary values, real work is done in rxi_WritevProc */
969 unsigned int tcurvec;
973 requestCount = nbytes;
976 /* Free any packets from the last call to ReadvProc/WritevProc */
977 if (queue_IsNotEmpty(&call->iovq)) {
978 #ifdef RXDEBUG_PACKET
980 #endif /* RXDEBUG_PACKET */
981 rxi_FreePackets(0, &call->iovq);
984 if (call->mode != RX_MODE_SENDING) {
985 if ((conn->type == RX_SERVER_CONNECTION)
986 && (call->mode == RX_MODE_RECEIVING)) {
987 call->mode = RX_MODE_SENDING;
989 #ifdef RX_TRACK_PACKETS
990 cp->flags &= ~RX_PKTFLAG_CP;
993 cp = call->currentPacket = (struct rx_packet *)0;
1002 /* Set up the iovec to point to data in packet buffers. */
1003 tnFree = call->nFree;
1004 tcurvec = call->curvec;
1005 tcurpos = call->curpos;
1006 tcurlen = call->curlen;
1011 /* current packet is full, allocate a new one */
1012 MUTEX_ENTER(&call->lock);
1013 cp = rxi_AllocSendPacket(call, nbytes);
1014 MUTEX_EXIT(&call->lock);
1016 /* out of space, return what we have */
1018 return requestCount - nbytes;
1020 #ifdef RX_TRACK_PACKETS
1021 cp->flags |= RX_PKTFLAG_IOVQ;
1023 queue_Append(&call->iovq, cp);
1024 #ifdef RXDEBUG_PACKET
1026 #endif /* RXDEBUG_PACKET */
1027 tnFree = cp->length;
1030 (char *)cp->wirevec[1].iov_base +
1031 call->conn->securityHeaderSize;
1032 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1035 if (tnFree < nbytes) {
1036 /* try to extend the current packet */
1039 mud = rx_MaxUserDataSize(call);
1042 want = MIN(nbytes - tnFree, mud - len);
1043 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1044 if (cp->length > (unsigned)mud)
1046 tnFree += (cp->length - len);
1047 if (cp == call->currentPacket) {
1048 call->nFree += (cp->length - len);
1053 /* fill in the next entry in the iovec */
1054 t = MIN(tcurlen, nbytes);
1056 iov[nextio].iov_base = tcurpos;
1057 iov[nextio].iov_len = t;
1065 /* need to get another struct iov */
1066 if (++tcurvec >= cp->niovecs) {
1067 /* current packet is full, extend it or move on to next packet */
1070 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1071 tcurlen = cp->wirevec[tcurvec].iov_len;
1074 } while (nbytes && nextio < maxio);
1076 return requestCount - nbytes;
1080 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1087 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1092 /* rxi_WritevProc -- internal version.
1094 * Send buffers allocated in rxi_WritevAlloc.
1096 * LOCKS USED -- called at netpri.
1099 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1101 struct rx_packet *cp = NULL;
1102 #ifdef RX_TRACK_PACKETS
1103 struct rx_packet *p, *np;
1107 struct rx_queue tmpq;
1108 #ifdef RXDEBUG_PACKET
1112 requestCount = nbytes;
1115 MUTEX_ENTER(&call->lock);
1117 call->mode = RX_MODE_ERROR;
1118 } else if (call->mode != RX_MODE_SENDING) {
1119 call->error = RX_PROTOCOL_ERROR;
1121 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1122 rxi_WaitforTQBusy(call);
1123 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1124 cp = call->currentPacket;
1127 call->mode = RX_MODE_ERROR;
1128 MUTEX_EXIT(&call->lock);
1130 #ifdef RX_TRACK_PACKETS
1131 cp->flags &= ~RX_PKTFLAG_CP;
1132 cp->flags |= RX_PKTFLAG_IOVQ;
1134 queue_Prepend(&call->iovq, cp);
1135 #ifdef RXDEBUG_PACKET
1137 #endif /* RXDEBUG_PACKET */
1138 call->currentPacket = (struct rx_packet *)0;
1140 #ifdef RXDEBUG_PACKET
1142 #endif /* RXDEBUG_PACKET */
1143 rxi_FreePackets(0, &call->iovq);
1147 /* Loop through the I/O vector adjusting packet pointers.
1148 * Place full packets back onto the iovq once they are ready
1149 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1150 * the iovec. We put the loop condition at the end to ensure that
1151 * a zero length write will push a short packet. */
1154 #ifdef RXDEBUG_PACKET
1156 #endif /* RXDEBUG_PACKET */
1158 if (call->nFree == 0 && cp) {
1159 clock_NewTime(); /* Bogus: need new time package */
1160 /* The 0, below, specifies that it is not the last packet:
1161 * there will be others. PrepareSendPacket may
1162 * alter the packet length by up to
1163 * conn->securityMaxTrailerSize */
1164 hadd32(call->bytesSent, cp->length);
1165 rxi_PrepareSendPacket(call, cp, 0);
1166 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1167 /* PrepareSendPacket drops the call lock */
1168 rxi_WaitforTQBusy(call);
1169 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1170 queue_Append(&tmpq, cp);
1171 #ifdef RXDEBUG_PACKET
1173 #endif /* RXDEBUG_PACKET */
1174 cp = call->currentPacket = (struct rx_packet *)0;
1176 /* The head of the iovq is now the current packet */
1178 if (queue_IsEmpty(&call->iovq)) {
1179 MUTEX_EXIT(&call->lock);
1180 call->error = RX_PROTOCOL_ERROR;
1181 #ifdef RXDEBUG_PACKET
1183 #endif /* RXDEBUG_PACKET */
1184 rxi_FreePackets(0, &tmpq);
1187 cp = queue_First(&call->iovq, rx_packet);
1189 #ifdef RX_TRACK_PACKETS
1190 cp->flags &= ~RX_PKTFLAG_IOVQ;
1192 #ifdef RXDEBUG_PACKET
1194 #endif /* RXDEBUG_PACKET */
1195 #ifdef RX_TRACK_PACKETS
1196 cp->flags |= RX_PKTFLAG_CP;
1198 call->currentPacket = cp;
1199 call->nFree = cp->length;
1202 (char *)cp->wirevec[1].iov_base +
1203 call->conn->securityHeaderSize;
1205 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1210 /* The next iovec should point to the current position */
1211 if (iov[nextio].iov_base != call->curpos
1212 || iov[nextio].iov_len > (int)call->curlen) {
1213 call->error = RX_PROTOCOL_ERROR;
1214 MUTEX_EXIT(&call->lock);
1216 #ifdef RX_TRACK_PACKETS
1217 cp->flags &= ~RX_PKTFLAG_CP;
1219 queue_Prepend(&tmpq, cp);
1220 #ifdef RXDEBUG_PACKET
1222 #endif /* RXDEBUG_PACKET */
1223 cp = call->currentPacket = (struct rx_packet *)0;
1225 #ifdef RXDEBUG_PACKET
1227 #endif /* RXDEBUG_PACKET */
1228 rxi_FreePackets(0, &tmpq);
1231 nbytes -= iov[nextio].iov_len;
1232 call->curpos += iov[nextio].iov_len;
1233 call->curlen -= iov[nextio].iov_len;
1234 call->nFree -= iov[nextio].iov_len;
1236 if (call->curlen == 0) {
1237 if (++call->curvec > cp->niovecs) {
1240 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1241 call->curlen = cp->wirevec[call->curvec].iov_len;
1245 } while (nbytes && nextio < nio);
1247 /* Move the packets from the temporary queue onto the transmit queue.
1248 * We may end up with more than call->twind packets on the queue. */
1250 #ifdef RX_TRACK_PACKETS
1251 for (queue_Scan(&tmpq, p, np, rx_packet))
1253 p->flags |= RX_PKTFLAG_TQ;
1258 call->mode = RX_MODE_ERROR;
1260 queue_SpliceAppend(&call->tq, &tmpq);
1262 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1263 rxi_Start(0, call, 0, 0);
1266 /* Wait for the length of the transmit queue to fall below call->twind */
1267 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1269 call->startWait = clock_Sec();
1270 #ifdef RX_ENABLE_LOCKS
1271 CV_WAIT(&call->cv_twind, &call->lock);
1273 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1274 osi_rxSleep(&call->twind);
1276 call->startWait = 0;
1279 /* cp is no longer valid since we may have given up the lock */
1280 cp = call->currentPacket;
1283 call->mode = RX_MODE_ERROR;
1284 call->currentPacket = NULL;
1285 MUTEX_EXIT(&call->lock);
1287 #ifdef RX_TRACK_PACKETS
1288 cp->flags &= ~RX_PKTFLAG_CP;
1294 MUTEX_EXIT(&call->lock);
1296 return requestCount - nbytes;
1300 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1306 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1311 /* Flush any buffered data to the stream, switch to read mode
1312 * (clients) or to EOF mode (servers)
1314 * LOCKS HELD: called at netpri.
1317 rxi_FlushWrite(struct rx_call *call)
1319 struct rx_packet *cp = NULL;
1321 /* Free any packets from the last call to ReadvProc/WritevProc */
1322 if (queue_IsNotEmpty(&call->iovq)) {
1323 #ifdef RXDEBUG_PACKET
1325 #endif /* RXDEBUG_PACKET */
1326 rxi_FreePackets(0, &call->iovq);
1329 if (call->mode == RX_MODE_SENDING) {
1332 (call->conn->type ==
1333 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1335 #ifdef RX_KERNEL_TRACE
1337 int glockOwner = ISAFS_GLOCK();
1340 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1341 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1348 MUTEX_ENTER(&call->lock);
1350 call->mode = RX_MODE_ERROR;
1352 cp = call->currentPacket;
1355 /* cp->length is only supposed to be the user's data */
1356 /* cp->length was already set to (then-current)
1357 * MaxUserDataSize or less. */
1358 #ifdef RX_TRACK_PACKETS
1359 cp->flags &= ~RX_PKTFLAG_CP;
1361 cp->length -= call->nFree;
1362 call->currentPacket = (struct rx_packet *)0;
1365 cp = rxi_AllocSendPacket(call, 0);
1367 /* Mode can no longer be MODE_SENDING */
1371 cp->niovecs = 2; /* header + space for rxkad stuff */
1375 /* The 1 specifies that this is the last packet */
1376 hadd32(call->bytesSent, cp->length);
1377 rxi_PrepareSendPacket(call, cp, 1);
1378 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1379 /* PrepareSendPacket drops the call lock */
1380 rxi_WaitforTQBusy(call);
1381 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1382 #ifdef RX_TRACK_PACKETS
1383 cp->flags |= RX_PKTFLAG_TQ;
1385 queue_Append(&call->tq, cp);
1386 #ifdef RXDEBUG_PACKET
1388 #endif /* RXDEBUG_PACKET */
1391 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1392 rxi_Start(0, call, 0, 0);
1394 MUTEX_EXIT(&call->lock);
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 rxi_FlushWrite(call);