2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
12 #include "afs/param.h"
14 #include <afs/param.h>
20 #ifdef RX_KERNEL_TRACE
21 #include "rx_kcommon.h"
23 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
24 #include "afs/sysincludes.h"
29 #if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV)
33 #include <net/net_globals.h>
34 #endif /* AFS_OSF_ENV */
35 #ifdef AFS_LINUX20_ENV
38 #include "netinet/in.h"
39 #if defined(AFS_SGI_ENV)
40 #include "afs/sysincludes.h"
43 #include "afs/afs_args.h"
44 #include "afs/afs_osi.h"
45 #if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
49 #include "afs/sysincludes.h"
52 #undef RXDEBUG /* turn off debugging */
55 #include "rx_kmutex.h"
56 #include "rx/rx_kernel.h"
57 #include "rx/rx_clock.h"
58 #include "rx/rx_queue.h"
60 #include "rx/rx_globals.h"
68 #endif /* AFS_OSF_ENV */
70 # include <sys/types.h>
72 # include <winsock2.h>
73 #else /* !AFS_NT40_ENV */
74 # include <sys/socket.h>
75 # include <sys/file.h>
77 # include <netinet/in.h>
78 # include <sys/stat.h>
79 # include <sys/time.h>
80 #endif /* !AFS_NT40_ENV */
86 # include "rx_clock.h"
87 # include "rx_queue.h"
89 # include "rx_globals.h"
93 /* rxdb_fileID is used to identify the lock location, along with line#. */
94 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
95 #endif /* RX_LOCKS_DB */
96 /* rxi_ReadProc -- internal version.
98 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
101 rxi_ReadProc(struct rx_call *call, char *buf,
104 struct rx_packet *cp = call->currentPacket;
105 struct rx_packet *rp;
109 /* XXXX took out clock_NewTime from here. Was it needed? */
110 requestCount = nbytes;
112 /* Free any packets from the last call to ReadvProc/WritevProc */
113 if (queue_IsNotEmpty(&call->iovq)) {
114 #ifdef RXDEBUG_PACKET
116 #endif /* RXDEBUG_PACKET */
117 rxi_FreePackets(0, &call->iovq);
121 if (call->nLeft == 0) {
122 /* Get next packet */
124 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
128 if (call->mode == RX_MODE_SENDING) {
129 rxi_FlushWrite(call);
133 if (queue_IsNotEmpty(&call->rq)) {
134 /* Check that next packet available is next in sequence */
135 rp = queue_First(&call->rq, rx_packet);
136 if (rp->header.seq == call->rnext) {
138 struct rx_connection *conn = call->conn;
140 #ifdef RX_TRACK_PACKETS
141 rp->flags &= ~RX_PKTFLAG_RQ;
143 #ifdef RXDEBUG_PACKET
145 #endif /* RXDEBUG_PACKET */
147 /* RXS_CheckPacket called to undo RXS_PreparePacket's
148 * work. It may reduce the length of the packet by up
149 * to conn->maxTrailerSize, to reflect the length of the
150 * data + the header. */
152 RXS_CheckPacket(conn->securityObject, call,
154 /* Used to merely shut down the call, but now we
155 * shut down the whole connection since this may
156 * indicate an attempt to hijack it */
158 MUTEX_EXIT(&call->lock);
159 rxi_ConnectionError(conn, error);
160 MUTEX_ENTER(&conn->conn_data_lock);
161 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
162 MUTEX_EXIT(&conn->conn_data_lock);
164 MUTEX_ENTER(&call->lock);
169 cp = call->currentPacket = rp;
170 #ifdef RX_TRACK_PACKETS
171 call->currentPacket->flags |= RX_PKTFLAG_CP;
173 call->curvec = 1; /* 0th vec is always header */
174 /* begin at the beginning [ more or less ], continue
175 * on until the end, then stop. */
177 (char *)cp->wirevec[1].iov_base +
178 call->conn->securityHeaderSize;
180 cp->wirevec[1].iov_len -
181 call->conn->securityHeaderSize;
183 /* Notice that this code works correctly if the data
184 * size is 0 (which it may be--no reply arguments from
185 * server, for example). This relies heavily on the
186 * fact that the code below immediately frees the packet
187 * (no yields, etc.). If it didn't, this would be a
188 * problem because a value of zero for call->nLeft
189 * normally means that there is no read packet */
190 call->nLeft = cp->length;
191 hadd32(call->bytesRcvd, cp->length);
193 /* Send a hard ack for every rxi_HardAckRate+1 packets
194 * consumed. Otherwise schedule an event to send
195 * the hard ack later on.
198 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
199 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
200 rxevent_Cancel(call->delayedAckEvent, call,
201 RX_CALL_REFCOUNT_DELAY);
202 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
204 struct clock when, now;
207 /* Delay to consolidate ack packets */
208 clock_Add(&when, &rx_hardAckDelay);
209 if (!call->delayedAckEvent
210 || clock_Gt(&call->delayedAckEvent->
212 rxevent_Cancel(call->delayedAckEvent,
214 RX_CALL_REFCOUNT_DELAY);
215 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
216 call->delayedAckEvent =
217 rxevent_PostNow(&when, &now,
218 rxi_SendDelayedAck, call,
228 * If we reach this point either we have no packets in the
229 * receive queue or the next packet in the queue is not the
230 * one we are looking for. There is nothing else for us to
231 * do but wait for another packet to arrive.
234 /* Are there ever going to be any more packets? */
235 if (call->flags & RX_CALL_RECEIVE_DONE) {
236 return requestCount - nbytes;
238 /* Wait for in-sequence packet */
239 call->flags |= RX_CALL_READER_WAIT;
241 call->startWait = clock_Sec();
242 while (call->flags & RX_CALL_READER_WAIT) {
243 #ifdef RX_ENABLE_LOCKS
244 CV_WAIT(&call->cv_rq, &call->lock);
246 osi_rxSleep(&call->rq);
249 /* cp is no longer valid since we may have given up the lock */
250 cp = call->currentPacket;
253 #ifdef RX_ENABLE_LOCKS
257 #endif /* RX_ENABLE_LOCKS */
261 /* MTUXXX this should be replaced by some error-recovery code before shipping */
262 /* yes, the following block is allowed to be the ELSE clause (or not) */
263 /* It's possible for call->nLeft to be smaller than any particular
264 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
265 * reflects the size of the buffer. We have to keep track of the
266 * number of bytes read in the length field of the packet struct. On
267 * the final portion of a received packet, it's almost certain that
268 * call->nLeft will be smaller than the final buffer. */
269 while (nbytes && cp) {
270 t = MIN((int)call->curlen, nbytes);
271 t = MIN(t, (int)call->nLeft);
272 memcpy(buf, call->curpos, t);
280 /* out of packet. Get another one. */
281 #ifdef RX_TRACK_PACKETS
282 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
285 cp = call->currentPacket = (struct rx_packet *)0;
286 } else if (!call->curlen) {
287 /* need to get another struct iov */
288 if (++call->curvec >= cp->niovecs) {
289 /* current packet is exhausted, get ready for another */
290 /* don't worry about curvec and stuff, they get set somewhere else */
291 #ifdef RX_TRACK_PACKETS
292 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
295 cp = call->currentPacket = (struct rx_packet *)0;
299 (char *)cp->wirevec[call->curvec].iov_base;
300 call->curlen = cp->wirevec[call->curvec].iov_len;
305 /* user buffer is full, return */
315 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
324 /* Free any packets from the last call to ReadvProc/WritevProc */
326 MUTEX_ENTER(&call->lock);
327 if (!queue_IsEmpty(&call->iovq)) {
328 #ifdef RXDEBUG_PACKET
330 #endif /* RXDEBUG_PACKET */
331 rxi_FreePackets(0, &call->iovq);
335 * Most common case, all of the data is in the current iovec.
336 * We are relying on nLeft being zero unless the call is in receive mode.
338 tcurlen = call->curlen;
339 tnLeft = call->nLeft;
340 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
341 tcurpos = call->curpos;
342 MUTEX_EXIT(&call->lock);
346 memcpy(buf, tcurpos, nbytes);
348 call->curpos = tcurpos + nbytes;
349 call->curlen = tcurlen - nbytes;
350 call->nLeft = tnLeft - nbytes;
352 if (!call->nLeft && call->currentPacket != NULL) {
353 /* out of packet. Get another one. */
355 MUTEX_ENTER(&call->lock);
357 rxi_FreePacket(call->currentPacket);
358 call->currentPacket = (struct rx_packet *)0;
362 bytes = rxi_ReadProc(call, buf, nbytes);
365 MUTEX_EXIT(&call->lock);
371 /* Optimization for unmarshalling 32 bit integers */
373 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
382 /* Free any packets from the last call to ReadvProc/WritevProc */
384 MUTEX_ENTER(&call->lock);
385 if (!queue_IsEmpty(&call->iovq)) {
386 #ifdef RXDEBUG_PACKET
388 #endif /* RXDEBUG_PACKET */
389 rxi_FreePackets(0, &call->iovq);
393 * Most common case, all of the data is in the current iovec.
394 * We are relying on nLeft being zero unless the call is in receive mode.
396 tcurlen = call->curlen;
397 tnLeft = call->nLeft;
398 if (!call->error && tcurlen >= sizeof(afs_int32)
399 && tnLeft >= sizeof(afs_int32)) {
400 tcurpos = call->curpos;
401 MUTEX_EXIT(&call->lock);
405 memcpy((char *)value, tcurpos, sizeof(afs_int32));
407 call->curpos = tcurpos + sizeof(afs_int32);
408 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
409 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
410 if (!call->nLeft && call->currentPacket != NULL) {
411 /* out of packet. Get another one. */
413 MUTEX_ENTER(&call->lock);
415 rxi_FreePacket(call->currentPacket);
416 call->currentPacket = (struct rx_packet *)0;
418 bytes = sizeof(afs_int32);
420 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
423 MUTEX_EXIT(&call->lock);
431 * Uses packets in the receive queue to fill in as much of the
432 * current iovec as possible. Does not block if it runs out
433 * of packets to complete the iovec. Return true if an ack packet
434 * was sent, otherwise return false */
436 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
441 struct rx_packet *rp;
442 struct rx_packet *curp;
443 struct iovec *call_iov;
444 struct iovec *cur_iov = NULL;
446 curp = call->currentPacket;
448 cur_iov = &curp->wirevec[call->curvec];
450 call_iov = &call->iov[call->iovNext];
452 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
453 if (call->nLeft == 0) {
454 /* Get next packet */
455 if (queue_IsNotEmpty(&call->rq)) {
456 /* Check that next packet available is next in sequence */
457 rp = queue_First(&call->rq, rx_packet);
458 if (rp->header.seq == call->rnext) {
460 struct rx_connection *conn = call->conn;
462 #ifdef RX_TRACK_PACKETS
463 rp->flags &= ~RX_PKTFLAG_RQ;
465 #ifdef RXDEBUG_PACKET
467 #endif /* RXDEBUG_PACKET */
469 /* RXS_CheckPacket called to undo RXS_PreparePacket's
470 * work. It may reduce the length of the packet by up
471 * to conn->maxTrailerSize, to reflect the length of the
472 * data + the header. */
474 RXS_CheckPacket(conn->securityObject, call, rp))) {
475 /* Used to merely shut down the call, but now we
476 * shut down the whole connection since this may
477 * indicate an attempt to hijack it */
479 MUTEX_EXIT(&call->lock);
480 rxi_ConnectionError(conn, error);
481 MUTEX_ENTER(&conn->conn_data_lock);
482 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
483 MUTEX_EXIT(&conn->conn_data_lock);
485 MUTEX_ENTER(&call->lock);
490 curp = call->currentPacket = rp;
491 #ifdef RX_TRACK_PACKETS
492 call->currentPacket->flags |= RX_PKTFLAG_CP;
494 call->curvec = 1; /* 0th vec is always header */
495 cur_iov = &curp->wirevec[1];
496 /* begin at the beginning [ more or less ], continue
497 * on until the end, then stop. */
499 (char *)curp->wirevec[1].iov_base +
500 call->conn->securityHeaderSize;
502 curp->wirevec[1].iov_len -
503 call->conn->securityHeaderSize;
505 /* Notice that this code works correctly if the data
506 * size is 0 (which it may be--no reply arguments from
507 * server, for example). This relies heavily on the
508 * fact that the code below immediately frees the packet
509 * (no yields, etc.). If it didn't, this would be a
510 * problem because a value of zero for call->nLeft
511 * normally means that there is no read packet */
512 call->nLeft = curp->length;
513 hadd32(call->bytesRcvd, curp->length);
515 /* Send a hard ack for every rxi_HardAckRate+1 packets
516 * consumed. Otherwise schedule an event to send
517 * the hard ack later on.
527 /* It's possible for call->nLeft to be smaller than any particular
528 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
529 * reflects the size of the buffer. We have to keep track of the
530 * number of bytes read in the length field of the packet struct. On
531 * the final portion of a received packet, it's almost certain that
532 * call->nLeft will be smaller than the final buffer. */
533 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
535 t = MIN((int)call->curlen, call->iovNBytes);
536 t = MIN(t, (int)call->nLeft);
537 call_iov->iov_base = call->curpos;
538 call_iov->iov_len = t;
541 call->iovNBytes -= t;
547 /* out of packet. Get another one. */
548 #ifdef RX_TRACK_PACKETS
549 curp->flags &= ~RX_PKTFLAG_CP;
550 curp->flags |= RX_PKTFLAG_IOVQ;
552 queue_Append(&call->iovq, curp);
553 #ifdef RXDEBUG_PACKET
555 #endif /* RXDEBUG_PACKET */
556 curp = call->currentPacket = (struct rx_packet *)0;
557 } else if (!call->curlen) {
558 /* need to get another struct iov */
559 if (++call->curvec >= curp->niovecs) {
560 /* current packet is exhausted, get ready for another */
561 /* don't worry about curvec and stuff, they get set somewhere else */
562 #ifdef RX_TRACK_PACKETS
563 curp->flags &= ~RX_PKTFLAG_CP;
564 curp->flags |= RX_PKTFLAG_IOVQ;
566 queue_Append(&call->iovq, curp);
567 #ifdef RXDEBUG_PACKET
569 #endif /* RXDEBUG_PACKET */
570 curp = call->currentPacket = (struct rx_packet *)0;
574 call->curpos = (char *)cur_iov->iov_base;
575 call->curlen = cur_iov->iov_len;
581 /* If we consumed any packets then check whether we need to
582 * send a hard ack. */
583 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
584 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
585 rxevent_Cancel(call->delayedAckEvent, call,
586 RX_CALL_REFCOUNT_DELAY);
587 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
590 struct clock when, now;
593 /* Delay to consolidate ack packets */
594 clock_Add(&when, &rx_hardAckDelay);
595 if (!call->delayedAckEvent
596 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
597 rxevent_Cancel(call->delayedAckEvent, call,
598 RX_CALL_REFCOUNT_DELAY);
599 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
600 call->delayedAckEvent =
601 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
609 /* rxi_ReadvProc -- internal version.
611 * Fills in an iovec with pointers to the packet buffers. All packets
612 * except the last packet (new current packet) are moved to the iovq
613 * while the application is processing the data.
615 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
618 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
621 /* Free any packets from the last call to ReadvProc/WritevProc */
622 if (queue_IsNotEmpty(&call->iovq)) {
623 #ifdef RXDEBUG_PACKET
625 #endif /* RXDEBUG_PACKET */
626 rxi_FreePackets(0, &call->iovq);
629 if (call->mode == RX_MODE_SENDING) {
630 rxi_FlushWrite(call);
637 /* Get whatever data is currently available in the receive queue.
638 * If rxi_FillReadVec sends an ack packet then it is possible
639 * that we will receive more data while we drop the call lock
640 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
641 * here to avoid a race with the receive thread if we send
642 * hard acks in rxi_FillReadVec. */
643 call->flags |= RX_CALL_IOVEC_WAIT;
644 call->iovNBytes = nbytes;
645 call->iovMax = maxio;
648 rxi_FillReadVec(call, 0);
650 /* if we need more data then sleep until the receive thread has
651 * filled in the rest. */
652 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
653 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
654 call->flags |= RX_CALL_READER_WAIT;
656 call->startWait = clock_Sec();
657 while (call->flags & RX_CALL_READER_WAIT) {
658 #ifdef RX_ENABLE_LOCKS
659 CV_WAIT(&call->cv_rq, &call->lock);
661 osi_rxSleep(&call->rq);
666 call->flags &= ~RX_CALL_IOVEC_WAIT;
667 #ifdef RX_ENABLE_LOCKS
671 #endif /* RX_ENABLE_LOCKS */
674 *nio = call->iovNext;
675 return nbytes - call->iovNBytes;
679 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
686 MUTEX_ENTER(&call->lock);
687 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
688 MUTEX_EXIT(&call->lock);
693 /* rxi_WriteProc -- internal version.
695 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
698 rxi_WriteProc(struct rx_call *call, char *buf,
701 struct rx_connection *conn = call->conn;
702 struct rx_packet *cp = call->currentPacket;
704 int requestCount = nbytes;
706 /* Free any packets from the last call to ReadvProc/WritevProc */
707 if (queue_IsNotEmpty(&call->iovq)) {
708 #ifdef RXDEBUG_PACKET
710 #endif /* RXDEBUG_PACKET */
711 rxi_FreePackets(0, &call->iovq);
714 if (call->mode != RX_MODE_SENDING) {
715 if ((conn->type == RX_SERVER_CONNECTION)
716 && (call->mode == RX_MODE_RECEIVING)) {
717 call->mode = RX_MODE_SENDING;
719 #ifdef RX_TRACK_PACKETS
720 cp->flags &= ~RX_PKTFLAG_CP;
723 cp = call->currentPacket = (struct rx_packet *)0;
732 /* Loop condition is checked at end, so that a write of 0 bytes
733 * will force a packet to be created--specially for the case where
734 * there are 0 bytes on the stream, but we must send a packet
737 if (call->nFree == 0) {
738 if (!call->error && cp) {
739 /* Clear the current packet now so that if
740 * we are forced to wait and drop the lock
741 * the packet we are planning on using
744 #ifdef RX_TRACK_PACKETS
745 cp->flags &= ~RX_PKTFLAG_CP;
747 call->currentPacket = (struct rx_packet *)0;
748 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
749 /* Wait until TQ_BUSY is reset before adding any
750 * packets to the transmit queue
752 while (call->flags & RX_CALL_TQ_BUSY) {
753 call->flags |= RX_CALL_TQ_WAIT;
755 #ifdef RX_ENABLE_LOCKS
756 CV_WAIT(&call->cv_tq, &call->lock);
757 #else /* RX_ENABLE_LOCKS */
758 osi_rxSleep(&call->tq);
759 #endif /* RX_ENABLE_LOCKS */
761 if (call->tqWaiters == 0)
762 call->flags &= ~RX_CALL_TQ_WAIT;
764 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
765 clock_NewTime(); /* Bogus: need new time package */
766 /* The 0, below, specifies that it is not the last packet:
767 * there will be others. PrepareSendPacket may
768 * alter the packet length by up to
769 * conn->securityMaxTrailerSize */
770 hadd32(call->bytesSent, cp->length);
771 rxi_PrepareSendPacket(call, cp, 0);
772 #ifdef RX_TRACK_PACKETS
773 cp->flags |= RX_PKTFLAG_TQ;
775 queue_Append(&call->tq, cp);
776 #ifdef RXDEBUG_PACKET
778 #endif /* RXDEBUG_PACKET */
779 cp = (struct rx_packet *)0;
782 flags & (RX_CALL_FAST_RECOVER |
783 RX_CALL_FAST_RECOVER_WAIT))) {
784 rxi_Start(0, call, 0, 0);
787 #ifdef RX_TRACK_PACKETS
788 cp->flags &= ~RX_PKTFLAG_CP;
791 cp = call->currentPacket = (struct rx_packet *)0;
793 /* Wait for transmit window to open up */
795 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
797 call->startWait = clock_Sec();
799 #ifdef RX_ENABLE_LOCKS
800 CV_WAIT(&call->cv_twind, &call->lock);
802 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
803 osi_rxSleep(&call->twind);
807 #ifdef RX_ENABLE_LOCKS
811 #endif /* RX_ENABLE_LOCKS */
813 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
814 #ifdef RX_TRACK_PACKETS
815 cp->flags |= RX_PKTFLAG_CP;
817 call->currentPacket = cp;
818 call->nFree = cp->length;
819 call->curvec = 1; /* 0th vec is always header */
820 /* begin at the beginning [ more or less ], continue
821 * on until the end, then stop. */
823 (char *)cp->wirevec[1].iov_base +
824 call->conn->securityHeaderSize;
826 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
830 #ifdef RX_TRACK_PACKETS
831 cp->flags &= ~RX_PKTFLAG_CP;
834 call->currentPacket = NULL;
840 if (cp && (int)call->nFree < nbytes) {
841 /* Try to extend the current buffer */
844 mud = rx_MaxUserDataSize(call);
847 want = MIN(nbytes - (int)call->nFree, mud - len);
848 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
849 if (cp->length > (unsigned)mud)
851 call->nFree += (cp->length - len);
855 /* If the remaining bytes fit in the buffer, then store them
856 * and return. Don't ship a buffer that's full immediately to
857 * the peer--we don't know if it's the last buffer yet */
863 while (nbytes && call->nFree) {
865 t = MIN((int)call->curlen, nbytes);
866 t = MIN((int)call->nFree, t);
867 memcpy(call->curpos, buf, t);
871 call->curlen -= (u_short)t;
872 call->nFree -= (u_short)t;
875 /* need to get another struct iov */
876 if (++call->curvec >= cp->niovecs) {
877 /* current packet is full, extend or send it */
880 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
881 call->curlen = cp->wirevec[call->curvec].iov_len;
884 } /* while bytes to send and room to send them */
886 /* might be out of space now */
889 } else; /* more data to send, so get another packet and keep going */
892 return requestCount - nbytes;
896 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
905 /* Free any packets from the last call to ReadvProc/WritevProc */
907 MUTEX_ENTER(&call->lock);
908 if (queue_IsNotEmpty(&call->iovq)) {
909 #ifdef RXDEBUG_PACKET
911 #endif /* RXDEBUG_PACKET */
912 rxi_FreePackets(0, &call->iovq);
916 * Most common case: all of the data fits in the current iovec.
917 * We are relying on nFree being zero unless the call is in send mode.
919 tcurlen = (int)call->curlen;
920 tnFree = (int)call->nFree;
921 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
922 tcurpos = call->curpos;
924 MUTEX_EXIT(&call->lock);
928 memcpy(tcurpos, buf, nbytes);
929 call->curpos = tcurpos + nbytes;
930 call->curlen = (u_short)(tcurlen - nbytes);
931 call->nFree = (u_short)(tnFree - nbytes);
934 bytes = rxi_WriteProc(call, buf, nbytes);
937 MUTEX_EXIT(&call->lock);
943 /* Optimization for marshalling 32 bit arguments */
945 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
954 /* Free any packets from the last call to ReadvProc/WritevProc */
956 MUTEX_ENTER(&call->lock);
957 if (queue_IsNotEmpty(&call->iovq)) {
958 #ifdef RXDEBUG_PACKET
960 #endif /* RXDEBUG_PACKET */
961 rxi_FreePackets(0, &call->iovq);
965 * Most common case: all of the data fits in the current iovec.
966 * We are relying on nFree being zero unless the call is in send mode.
968 tcurlen = call->curlen;
969 tnFree = call->nFree;
970 if (!call->error && tcurlen >= sizeof(afs_int32)
971 && tnFree >= sizeof(afs_int32)) {
972 tcurpos = call->curpos;
974 MUTEX_EXIT(&call->lock);
978 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
979 *((afs_int32 *) (tcurpos)) = *value;
981 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
983 call->curpos = tcurpos + sizeof(afs_int32);
984 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
985 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
986 bytes = sizeof(afs_int32);
988 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
991 MUTEX_EXIT(&call->lock);
997 /* rxi_WritevAlloc -- internal version.
999 * Fill in an iovec to point to data in packet buffers. The application
1000 * calls rxi_WritevProc when the buffers are full.
1002 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1005 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1008 struct rx_connection *conn = call->conn;
1009 struct rx_packet *cp = call->currentPacket;
1012 /* Temporary values, real work is done in rxi_WritevProc */
1014 unsigned int tcurvec;
1018 requestCount = nbytes;
1021 /* Free any packets from the last call to ReadvProc/WritevProc */
1022 if (queue_IsNotEmpty(&call->iovq)) {
1023 #ifdef RXDEBUG_PACKET
1025 #endif /* RXDEBUG_PACKET */
1026 rxi_FreePackets(0, &call->iovq);
1029 if (call->mode != RX_MODE_SENDING) {
1030 if ((conn->type == RX_SERVER_CONNECTION)
1031 && (call->mode == RX_MODE_RECEIVING)) {
1032 call->mode = RX_MODE_SENDING;
1034 #ifdef RX_TRACK_PACKETS
1035 cp->flags &= ~RX_PKTFLAG_CP;
1038 cp = call->currentPacket = (struct rx_packet *)0;
1047 /* Set up the iovec to point to data in packet buffers. */
1048 tnFree = call->nFree;
1049 tcurvec = call->curvec;
1050 tcurpos = call->curpos;
1051 tcurlen = call->curlen;
1056 /* current packet is full, allocate a new one */
1057 cp = rxi_AllocSendPacket(call, nbytes);
1059 /* out of space, return what we have */
1061 return requestCount - nbytes;
1063 #ifdef RX_TRACK_PACKETS
1064 cp->flags |= RX_PKTFLAG_IOVQ;
1066 queue_Append(&call->iovq, cp);
1067 #ifdef RXDEBUG_PACKET
1069 #endif /* RXDEBUG_PACKET */
1070 tnFree = cp->length;
1073 (char *)cp->wirevec[1].iov_base +
1074 call->conn->securityHeaderSize;
1075 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1078 if (tnFree < nbytes) {
1079 /* try to extend the current packet */
1082 mud = rx_MaxUserDataSize(call);
1085 want = MIN(nbytes - tnFree, mud - len);
1086 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1087 if (cp->length > (unsigned)mud)
1089 tnFree += (cp->length - len);
1090 if (cp == call->currentPacket) {
1091 call->nFree += (cp->length - len);
1096 /* fill in the next entry in the iovec */
1097 t = MIN(tcurlen, nbytes);
1099 iov[nextio].iov_base = tcurpos;
1100 iov[nextio].iov_len = t;
1108 /* need to get another struct iov */
1109 if (++tcurvec >= cp->niovecs) {
1110 /* current packet is full, extend it or move on to next packet */
1113 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1114 tcurlen = cp->wirevec[tcurvec].iov_len;
1117 } while (nbytes && nextio < maxio);
1119 return requestCount - nbytes;
1123 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1130 MUTEX_ENTER(&call->lock);
1131 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1132 MUTEX_EXIT(&call->lock);
1137 /* rxi_WritevProc -- internal version.
1139 * Send buffers allocated in rxi_WritevAlloc.
1141 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1144 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1146 struct rx_packet *cp = NULL;
1147 #ifdef RX_TRACK_PACKETS
1148 struct rx_packet *p, *np;
1152 struct rx_queue tmpq;
1153 #ifdef RXDEBUG_PACKET
1157 requestCount = nbytes;
1160 if (call->mode != RX_MODE_SENDING) {
1161 call->error = RX_PROTOCOL_ERROR;
1163 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1164 /* Wait until TQ_BUSY is reset before trying to move any
1165 * packets to the transmit queue. */
1166 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1167 call->flags |= RX_CALL_TQ_WAIT;
1169 #ifdef RX_ENABLE_LOCKS
1170 CV_WAIT(&call->cv_tq, &call->lock);
1171 #else /* RX_ENABLE_LOCKS */
1172 osi_rxSleep(&call->tq);
1173 #endif /* RX_ENABLE_LOCKS */
1175 if (call->tqWaiters == 0)
1176 call->flags &= ~RX_CALL_TQ_WAIT;
1178 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1179 /* cp is no longer valid since we may have given up the lock */
1180 cp = call->currentPacket;
1184 #ifdef RX_TRACK_PACKETS
1185 cp->flags &= ~RX_PKTFLAG_CP;
1186 cp->flags |= RX_PKTFLAG_IOVQ;
1188 queue_Prepend(&call->iovq, cp);
1189 #ifdef RXDEBUG_PACKET
1191 #endif /* RXDEBUG_PACKET */
1192 cp = call->currentPacket = (struct rx_packet *)0;
1194 #ifdef RXDEBUG_PACKET
1196 #endif /* RXDEBUG_PACKET */
1197 rxi_FreePackets(0, &call->iovq);
1201 /* Loop through the I/O vector adjusting packet pointers.
1202 * Place full packets back onto the iovq once they are ready
1203 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1204 * the iovec. We put the loop condition at the end to ensure that
1205 * a zero length write will push a short packet. */
1208 #ifdef RXDEBUG_PACKET
1210 #endif /* RXDEBUG_PACKET */
1212 if (call->nFree == 0 && cp) {
1213 clock_NewTime(); /* Bogus: need new time package */
1214 /* The 0, below, specifies that it is not the last packet:
1215 * there will be others. PrepareSendPacket may
1216 * alter the packet length by up to
1217 * conn->securityMaxTrailerSize */
1218 hadd32(call->bytesSent, cp->length);
1219 rxi_PrepareSendPacket(call, cp, 0);
1220 queue_Append(&tmpq, cp);
1221 #ifdef RXDEBUG_PACKET
1223 #endif /* RXDEBUG_PACKET */
1224 cp = call->currentPacket = (struct rx_packet *)0;
1226 /* The head of the iovq is now the current packet */
1228 if (queue_IsEmpty(&call->iovq)) {
1229 call->error = RX_PROTOCOL_ERROR;
1230 #ifdef RXDEBUG_PACKET
1232 #endif /* RXDEBUG_PACKET */
1233 rxi_FreePackets(0, &tmpq);
1236 cp = queue_First(&call->iovq, rx_packet);
1238 #ifdef RX_TRACK_PACKETS
1239 cp->flags &= ~RX_PKTFLAG_IOVQ;
1241 #ifdef RXDEBUG_PACKET
1243 #endif /* RXDEBUG_PACKET */
1244 #ifdef RX_TRACK_PACKETS
1245 cp->flags |= RX_PKTFLAG_CP;
1247 call->currentPacket = cp;
1248 call->nFree = cp->length;
1251 (char *)cp->wirevec[1].iov_base +
1252 call->conn->securityHeaderSize;
1254 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1259 /* The next iovec should point to the current position */
1260 if (iov[nextio].iov_base != call->curpos
1261 || iov[nextio].iov_len > (int)call->curlen) {
1262 call->error = RX_PROTOCOL_ERROR;
1264 #ifdef RX_TRACK_PACKETS
1265 cp->flags &= ~RX_PKTFLAG_CP;
1267 queue_Prepend(&tmpq, cp);
1268 #ifdef RXDEBUG_PACKET
1270 #endif /* RXDEBUG_PACKET */
1271 cp = call->currentPacket = (struct rx_packet *)0;
1273 #ifdef RXDEBUG_PACKET
1275 #endif /* RXDEBUG_PACKET */
1276 rxi_FreePackets(0, &tmpq);
1279 nbytes -= iov[nextio].iov_len;
1280 call->curpos += iov[nextio].iov_len;
1281 call->curlen -= iov[nextio].iov_len;
1282 call->nFree -= iov[nextio].iov_len;
1284 if (call->curlen == 0) {
1285 if (++call->curvec > cp->niovecs) {
1288 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1289 call->curlen = cp->wirevec[call->curvec].iov_len;
1293 } while (nbytes && nextio < nio);
1295 /* Move the packets from the temporary queue onto the transmit queue.
1296 * We may end up with more than call->twind packets on the queue. */
1298 #ifdef RX_TRACK_PACKETS
1299 for (queue_Scan(&tmpq, p, np, rx_packet))
1301 p->flags |= RX_PKTFLAG_TQ;
1304 queue_SpliceAppend(&call->tq, &tmpq);
1306 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1307 rxi_Start(0, call, 0, 0);
1310 /* Wait for the length of the transmit queue to fall below call->twind */
1311 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1313 call->startWait = clock_Sec();
1314 #ifdef RX_ENABLE_LOCKS
1315 CV_WAIT(&call->cv_twind, &call->lock);
1317 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1318 osi_rxSleep(&call->twind);
1320 call->startWait = 0;
1322 /* cp is no longer valid since we may have given up the lock */
1323 cp = call->currentPacket;
1327 #ifdef RX_TRACK_PACKETS
1328 cp->flags &= ~RX_PKTFLAG_CP;
1331 cp = call->currentPacket = (struct rx_packet *)0;
1336 return requestCount - nbytes;
1340 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1346 MUTEX_ENTER(&call->lock);
1347 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1348 MUTEX_EXIT(&call->lock);
1353 /* Flush any buffered data to the stream, switch to read mode
1354 * (clients) or to EOF mode (servers) */
1356 rxi_FlushWrite(struct rx_call *call)
1358 struct rx_packet *cp = NULL;
1360 /* Free any packets from the last call to ReadvProc/WritevProc */
1361 if (queue_IsNotEmpty(&call->iovq)) {
1362 #ifdef RXDEBUG_PACKET
1364 #endif /* RXDEBUG_PACKET */
1365 rxi_FreePackets(0, &call->iovq);
1368 if (call->mode == RX_MODE_SENDING) {
1371 (call->conn->type ==
1372 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1374 #ifdef RX_KERNEL_TRACE
1376 int glockOwner = ISAFS_GLOCK();
1379 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1380 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1387 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1388 /* Wait until TQ_BUSY is reset before adding any
1389 * packets to the transmit queue
1391 while (call->flags & RX_CALL_TQ_BUSY) {
1392 call->flags |= RX_CALL_TQ_WAIT;
1394 #ifdef RX_ENABLE_LOCKS
1395 CV_WAIT(&call->cv_tq, &call->lock);
1396 #else /* RX_ENABLE_LOCKS */
1397 osi_rxSleep(&call->tq);
1398 #endif /* RX_ENABLE_LOCKS */
1400 if (call->tqWaiters == 0)
1401 call->flags &= ~RX_CALL_TQ_WAIT;
1403 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1405 /* cp is no longer valid since we may have given up the lock */
1406 cp = call->currentPacket;
1409 /* cp->length is only supposed to be the user's data */
1410 /* cp->length was already set to (then-current)
1411 * MaxUserDataSize or less. */
1412 #ifdef RX_TRACK_PACKETS
1413 cp->flags &= ~RX_PKTFLAG_CP;
1415 cp->length -= call->nFree;
1416 call->currentPacket = (struct rx_packet *)0;
1419 cp = rxi_AllocSendPacket(call, 0);
1421 /* Mode can no longer be MODE_SENDING */
1425 cp->niovecs = 2; /* header + space for rxkad stuff */
1429 /* The 1 specifies that this is the last packet */
1430 hadd32(call->bytesSent, cp->length);
1431 rxi_PrepareSendPacket(call, cp, 1);
1432 #ifdef RX_TRACK_PACKETS
1433 cp->flags |= RX_PKTFLAG_TQ;
1435 queue_Append(&call->tq, cp);
1436 #ifdef RXDEBUG_PACKET
1438 #endif /* RXDEBUG_PACKET */
1441 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1442 rxi_Start(0, call, 0, 0);
1447 /* Flush any buffered data to the stream, switch to read mode
1448 * (clients) or to EOF mode (servers) */
1450 rx_FlushWrite(struct rx_call *call)
1454 MUTEX_ENTER(&call->lock);
1455 rxi_FlushWrite(call);
1456 MUTEX_EXIT(&call->lock);