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 rp->flags &= ~RX_PKTFLAG_RQ;
141 #ifdef RXDEBUG_PACKET
143 #endif /* RXDEBUG_PACKET */
145 /* RXS_CheckPacket called to undo RXS_PreparePacket's
146 * work. It may reduce the length of the packet by up
147 * to conn->maxTrailerSize, to reflect the length of the
148 * data + the header. */
150 RXS_CheckPacket(conn->securityObject, call,
152 /* Used to merely shut down the call, but now we
153 * shut down the whole connection since this may
154 * indicate an attempt to hijack it */
156 MUTEX_EXIT(&call->lock);
157 rxi_ConnectionError(conn, error);
158 MUTEX_ENTER(&conn->conn_data_lock);
159 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
160 MUTEX_EXIT(&conn->conn_data_lock);
162 MUTEX_ENTER(&call->lock);
167 cp = call->currentPacket = rp;
168 call->currentPacket->flags |= RX_PKTFLAG_CP;
169 call->curvec = 1; /* 0th vec is always header */
170 /* begin at the beginning [ more or less ], continue
171 * on until the end, then stop. */
173 (char *)cp->wirevec[1].iov_base +
174 call->conn->securityHeaderSize;
176 cp->wirevec[1].iov_len -
177 call->conn->securityHeaderSize;
179 /* Notice that this code works correctly if the data
180 * size is 0 (which it may be--no reply arguments from
181 * server, for example). This relies heavily on the
182 * fact that the code below immediately frees the packet
183 * (no yields, etc.). If it didn't, this would be a
184 * problem because a value of zero for call->nLeft
185 * normally means that there is no read packet */
186 call->nLeft = cp->length;
187 hadd32(call->bytesRcvd, cp->length);
189 /* Send a hard ack for every rxi_HardAckRate+1 packets
190 * consumed. Otherwise schedule an event to send
191 * the hard ack later on.
194 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
195 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
196 rxevent_Cancel(call->delayedAckEvent, call,
197 RX_CALL_REFCOUNT_DELAY);
198 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
200 struct clock when, now;
203 /* Delay to consolidate ack packets */
204 clock_Add(&when, &rx_hardAckDelay);
205 if (!call->delayedAckEvent
206 || clock_Gt(&call->delayedAckEvent->
208 rxevent_Cancel(call->delayedAckEvent,
210 RX_CALL_REFCOUNT_DELAY);
211 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
212 call->delayedAckEvent =
213 rxevent_PostNow(&when, &now,
214 rxi_SendDelayedAck, call,
224 * If we reach this point either we have no packets in the
225 * receive queue or the next packet in the queue is not the
226 * one we are looking for. There is nothing else for us to
227 * do but wait for another packet to arrive.
230 /* Are there ever going to be any more packets? */
231 if (call->flags & RX_CALL_RECEIVE_DONE) {
232 return requestCount - nbytes;
234 /* Wait for in-sequence packet */
235 call->flags |= RX_CALL_READER_WAIT;
237 call->startWait = clock_Sec();
238 while (call->flags & RX_CALL_READER_WAIT) {
239 #ifdef RX_ENABLE_LOCKS
240 CV_WAIT(&call->cv_rq, &call->lock);
242 osi_rxSleep(&call->rq);
245 /* cp is no longer valid since we may have given up the lock */
246 cp = call->currentPacket;
249 #ifdef RX_ENABLE_LOCKS
253 #endif /* RX_ENABLE_LOCKS */
257 /* MTUXXX this should be replaced by some error-recovery code before shipping */
258 /* yes, the following block is allowed to be the ELSE clause (or not) */
259 /* It's possible for call->nLeft to be smaller than any particular
260 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
261 * reflects the size of the buffer. We have to keep track of the
262 * number of bytes read in the length field of the packet struct. On
263 * the final portion of a received packet, it's almost certain that
264 * call->nLeft will be smaller than the final buffer. */
265 while (nbytes && cp) {
266 t = MIN((int)call->curlen, nbytes);
267 t = MIN(t, (int)call->nLeft);
268 memcpy(buf, call->curpos, t);
276 /* out of packet. Get another one. */
277 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
279 cp = call->currentPacket = (struct rx_packet *)0;
280 } else if (!call->curlen) {
281 /* need to get another struct iov */
282 if (++call->curvec >= cp->niovecs) {
283 /* current packet is exhausted, get ready for another */
284 /* don't worry about curvec and stuff, they get set somewhere else */
285 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
287 cp = call->currentPacket = (struct rx_packet *)0;
291 (char *)cp->wirevec[call->curvec].iov_base;
292 call->curlen = cp->wirevec[call->curvec].iov_len;
297 /* user buffer is full, return */
307 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
315 /* Free any packets from the last call to ReadvProc/WritevProc */
317 MUTEX_ENTER(&call->lock);
318 if (!queue_IsEmpty(&call->iovq)) {
319 #ifdef RXDEBUG_PACKET
321 #endif /* RXDEBUG_PACKET */
322 rxi_FreePackets(0, &call->iovq);
326 * Most common case, all of the data is in the current iovec.
327 * We are relying on nLeft being zero unless the call is in receive mode.
329 tcurlen = call->curlen;
330 tnLeft = call->nLeft;
331 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
332 tcurpos = call->curpos;
333 memcpy(buf, tcurpos, nbytes);
334 call->curpos = tcurpos + nbytes;
335 call->curlen = tcurlen - nbytes;
336 call->nLeft = tnLeft - nbytes;
338 if (!call->nLeft && call->currentPacket != NULL) {
339 /* out of packet. Get another one. */
340 rxi_FreePacket(call->currentPacket);
341 call->currentPacket = (struct rx_packet *)0;
345 bytes = rxi_ReadProc(call, buf, nbytes);
347 MUTEX_EXIT(&call->lock);
352 /* Optimization for unmarshalling 32 bit integers */
354 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
362 /* Free any packets from the last call to ReadvProc/WritevProc */
364 MUTEX_ENTER(&call->lock);
365 if (!queue_IsEmpty(&call->iovq)) {
366 #ifdef RXDEBUG_PACKET
368 #endif /* RXDEBUG_PACKET */
369 rxi_FreePackets(0, &call->iovq);
373 * Most common case, all of the data is in the current iovec.
374 * We are relying on nLeft being zero unless the call is in receive mode.
376 tcurlen = call->curlen;
377 tnLeft = call->nLeft;
378 if (!call->error && tcurlen >= sizeof(afs_int32)
379 && tnLeft >= sizeof(afs_int32)) {
380 tcurpos = call->curpos;
381 memcpy((char *)value, tcurpos, sizeof(afs_int32));
382 call->curpos = tcurpos + sizeof(afs_int32);
383 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
384 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
385 if (!call->nLeft && call->currentPacket != NULL) {
386 /* out of packet. Get another one. */
387 rxi_FreePacket(call->currentPacket);
388 call->currentPacket = (struct rx_packet *)0;
390 bytes = sizeof(afs_int32);
392 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
394 MUTEX_EXIT(&call->lock);
401 * Uses packets in the receive queue to fill in as much of the
402 * current iovec as possible. Does not block if it runs out
403 * of packets to complete the iovec. Return true if an ack packet
404 * was sent, otherwise return false */
406 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
411 struct rx_packet *rp;
412 struct rx_packet *curp;
413 struct iovec *call_iov;
414 struct iovec *cur_iov = NULL;
416 curp = call->currentPacket;
418 cur_iov = &curp->wirevec[call->curvec];
420 call_iov = &call->iov[call->iovNext];
422 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
423 if (call->nLeft == 0) {
424 /* Get next packet */
425 if (queue_IsNotEmpty(&call->rq)) {
426 /* Check that next packet available is next in sequence */
427 rp = queue_First(&call->rq, rx_packet);
428 if (rp->header.seq == call->rnext) {
430 struct rx_connection *conn = call->conn;
432 rp->flags &= ~RX_PKTFLAG_RQ;
433 #ifdef RXDEBUG_PACKET
435 #endif /* RXDEBUG_PACKET */
437 /* RXS_CheckPacket called to undo RXS_PreparePacket's
438 * work. It may reduce the length of the packet by up
439 * to conn->maxTrailerSize, to reflect the length of the
440 * data + the header. */
442 RXS_CheckPacket(conn->securityObject, call, rp))) {
443 /* Used to merely shut down the call, but now we
444 * shut down the whole connection since this may
445 * indicate an attempt to hijack it */
447 MUTEX_EXIT(&call->lock);
448 rxi_ConnectionError(conn, error);
449 MUTEX_ENTER(&conn->conn_data_lock);
450 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
451 MUTEX_EXIT(&conn->conn_data_lock);
453 MUTEX_ENTER(&call->lock);
458 curp = call->currentPacket = rp;
459 call->currentPacket->flags |= RX_PKTFLAG_CP;
460 call->curvec = 1; /* 0th vec is always header */
461 cur_iov = &curp->wirevec[1];
462 /* begin at the beginning [ more or less ], continue
463 * on until the end, then stop. */
465 (char *)curp->wirevec[1].iov_base +
466 call->conn->securityHeaderSize;
468 curp->wirevec[1].iov_len -
469 call->conn->securityHeaderSize;
471 /* Notice that this code works correctly if the data
472 * size is 0 (which it may be--no reply arguments from
473 * server, for example). This relies heavily on the
474 * fact that the code below immediately frees the packet
475 * (no yields, etc.). If it didn't, this would be a
476 * problem because a value of zero for call->nLeft
477 * normally means that there is no read packet */
478 call->nLeft = curp->length;
479 hadd32(call->bytesRcvd, curp->length);
481 /* Send a hard ack for every rxi_HardAckRate+1 packets
482 * consumed. Otherwise schedule an event to send
483 * the hard ack later on.
493 /* It's possible for call->nLeft to be smaller than any particular
494 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
495 * reflects the size of the buffer. We have to keep track of the
496 * number of bytes read in the length field of the packet struct. On
497 * the final portion of a received packet, it's almost certain that
498 * call->nLeft will be smaller than the final buffer. */
499 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
501 t = MIN((int)call->curlen, call->iovNBytes);
502 t = MIN(t, (int)call->nLeft);
503 call_iov->iov_base = call->curpos;
504 call_iov->iov_len = t;
507 call->iovNBytes -= t;
513 /* out of packet. Get another one. */
514 curp->flags &= ~RX_PKTFLAG_CP;
515 curp->flags |= RX_PKTFLAG_IOVQ;
516 queue_Append(&call->iovq, curp);
517 #ifdef RXDEBUG_PACKET
519 #endif /* RXDEBUG_PACKET */
520 curp = call->currentPacket = (struct rx_packet *)0;
521 } else if (!call->curlen) {
522 /* need to get another struct iov */
523 if (++call->curvec >= curp->niovecs) {
524 /* current packet is exhausted, get ready for another */
525 /* don't worry about curvec and stuff, they get set somewhere else */
526 curp->flags &= ~RX_PKTFLAG_CP;
527 curp->flags |= RX_PKTFLAG_IOVQ;
528 queue_Append(&call->iovq, curp);
529 #ifdef RXDEBUG_PACKET
531 #endif /* RXDEBUG_PACKET */
532 curp = call->currentPacket = (struct rx_packet *)0;
536 call->curpos = (char *)cur_iov->iov_base;
537 call->curlen = cur_iov->iov_len;
543 /* If we consumed any packets then check whether we need to
544 * send a hard ack. */
545 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
546 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
547 rxevent_Cancel(call->delayedAckEvent, call,
548 RX_CALL_REFCOUNT_DELAY);
549 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
552 struct clock when, now;
555 /* Delay to consolidate ack packets */
556 clock_Add(&when, &rx_hardAckDelay);
557 if (!call->delayedAckEvent
558 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
559 rxevent_Cancel(call->delayedAckEvent, call,
560 RX_CALL_REFCOUNT_DELAY);
561 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
562 call->delayedAckEvent =
563 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
571 /* rxi_ReadvProc -- internal version.
573 * Fills in an iovec with pointers to the packet buffers. All packets
574 * except the last packet (new current packet) are moved to the iovq
575 * while the application is processing the data.
577 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
580 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
586 requestCount = nbytes;
589 /* Free any packets from the last call to ReadvProc/WritevProc */
590 if (queue_IsNotEmpty(&call->iovq)) {
591 #ifdef RXDEBUG_PACKET
593 #endif /* RXDEBUG_PACKET */
594 rxi_FreePackets(0, &call->iovq);
597 if (call->mode == RX_MODE_SENDING) {
598 rxi_FlushWrite(call);
605 /* Get whatever data is currently available in the receive queue.
606 * If rxi_FillReadVec sends an ack packet then it is possible
607 * that we will receive more data while we drop the call lock
608 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
609 * here to avoid a race with the receive thread if we send
610 * hard acks in rxi_FillReadVec. */
611 call->flags |= RX_CALL_IOVEC_WAIT;
612 call->iovNBytes = nbytes;
613 call->iovMax = maxio;
616 rxi_FillReadVec(call, 0);
618 /* if we need more data then sleep until the receive thread has
619 * filled in the rest. */
620 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
621 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
622 call->flags |= RX_CALL_READER_WAIT;
624 call->startWait = clock_Sec();
625 while (call->flags & RX_CALL_READER_WAIT) {
626 #ifdef RX_ENABLE_LOCKS
627 CV_WAIT(&call->cv_rq, &call->lock);
629 osi_rxSleep(&call->rq);
634 call->flags &= ~RX_CALL_IOVEC_WAIT;
635 #ifdef RX_ENABLE_LOCKS
639 #endif /* RX_ENABLE_LOCKS */
642 *nio = call->iovNext;
643 return nbytes - call->iovNBytes;
647 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
654 MUTEX_ENTER(&call->lock);
655 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
656 MUTEX_EXIT(&call->lock);
661 /* rxi_WriteProc -- internal version.
663 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
666 rxi_WriteProc(struct rx_call *call, char *buf,
669 struct rx_connection *conn = call->conn;
670 struct rx_packet *cp = call->currentPacket;
672 int requestCount = nbytes;
674 /* Free any packets from the last call to ReadvProc/WritevProc */
675 if (queue_IsNotEmpty(&call->iovq)) {
676 #ifdef RXDEBUG_PACKET
678 #endif /* RXDEBUG_PACKET */
679 rxi_FreePackets(0, &call->iovq);
682 if (call->mode != RX_MODE_SENDING) {
683 if ((conn->type == RX_SERVER_CONNECTION)
684 && (call->mode == RX_MODE_RECEIVING)) {
685 call->mode = RX_MODE_SENDING;
687 cp->flags &= ~RX_PKTFLAG_CP;
689 cp = call->currentPacket = (struct rx_packet *)0;
698 /* Loop condition is checked at end, so that a write of 0 bytes
699 * will force a packet to be created--specially for the case where
700 * there are 0 bytes on the stream, but we must send a packet
703 if (call->nFree == 0) {
704 if (!call->error && cp) {
705 /* Clear the current packet now so that if
706 * we are forced to wait and drop the lock
707 * the packet we are planning on using
710 cp->flags &= ~RX_PKTFLAG_CP;
711 call->currentPacket = (struct rx_packet *)0;
712 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
713 /* Wait until TQ_BUSY is reset before adding any
714 * packets to the transmit queue
716 while (call->flags & RX_CALL_TQ_BUSY) {
717 call->flags |= RX_CALL_TQ_WAIT;
719 #ifdef RX_ENABLE_LOCKS
720 CV_WAIT(&call->cv_tq, &call->lock);
721 #else /* RX_ENABLE_LOCKS */
722 osi_rxSleep(&call->tq);
723 #endif /* RX_ENABLE_LOCKS */
725 if (call->tqWaiters == 0)
726 call->flags &= ~RX_CALL_TQ_WAIT;
728 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
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 cp->flags |= RX_PKTFLAG_TQ;
737 queue_Append(&call->tq, cp);
738 #ifdef RXDEBUG_PACKET
740 #endif /* RXDEBUG_PACKET */
741 cp = (struct rx_packet *)0;
744 flags & (RX_CALL_FAST_RECOVER |
745 RX_CALL_FAST_RECOVER_WAIT))) {
746 rxi_Start(0, call, 0, 0);
749 cp->flags &= ~RX_PKTFLAG_CP;
751 cp = call->currentPacket = (struct rx_packet *)0;
753 /* Wait for transmit window to open up */
755 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
757 call->startWait = clock_Sec();
759 #ifdef RX_ENABLE_LOCKS
760 CV_WAIT(&call->cv_twind, &call->lock);
762 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
763 osi_rxSleep(&call->twind);
767 #ifdef RX_ENABLE_LOCKS
771 #endif /* RX_ENABLE_LOCKS */
773 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
774 cp->flags |= RX_PKTFLAG_CP;
775 call->currentPacket = cp;
776 call->nFree = cp->length;
777 call->curvec = 1; /* 0th vec is always header */
778 /* begin at the beginning [ more or less ], continue
779 * on until the end, then stop. */
781 (char *)cp->wirevec[1].iov_base +
782 call->conn->securityHeaderSize;
784 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
788 cp->flags &= ~RX_PKTFLAG_CP;
790 call->currentPacket = NULL;
796 if (cp && (int)call->nFree < nbytes) {
797 /* Try to extend the current buffer */
800 mud = rx_MaxUserDataSize(call);
803 want = MIN(nbytes - (int)call->nFree, mud - len);
804 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
805 if (cp->length > (unsigned)mud)
807 call->nFree += (cp->length - len);
811 /* If the remaining bytes fit in the buffer, then store them
812 * and return. Don't ship a buffer that's full immediately to
813 * the peer--we don't know if it's the last buffer yet */
819 while (nbytes && call->nFree) {
821 t = MIN((int)call->curlen, nbytes);
822 t = MIN((int)call->nFree, t);
823 memcpy(call->curpos, buf, t);
827 call->curlen -= (u_short)t;
828 call->nFree -= (u_short)t;
831 /* need to get another struct iov */
832 if (++call->curvec >= cp->niovecs) {
833 /* current packet is full, extend or send it */
836 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
837 call->curlen = cp->wirevec[call->curvec].iov_len;
840 } /* while bytes to send and room to send them */
842 /* might be out of space now */
845 } else; /* more data to send, so get another packet and keep going */
848 return requestCount - nbytes;
852 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
860 /* Free any packets from the last call to ReadvProc/WritevProc */
862 MUTEX_ENTER(&call->lock);
863 if (queue_IsNotEmpty(&call->iovq)) {
864 #ifdef RXDEBUG_PACKET
866 #endif /* RXDEBUG_PACKET */
867 rxi_FreePackets(0, &call->iovq);
871 * Most common case: all of the data fits in the current iovec.
872 * We are relying on nFree being zero unless the call is in send mode.
874 tcurlen = (int)call->curlen;
875 tnFree = (int)call->nFree;
876 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
877 tcurpos = call->curpos;
878 memcpy(tcurpos, buf, nbytes);
879 call->curpos = tcurpos + nbytes;
880 call->curlen = (u_short)(tcurlen - nbytes);
881 call->nFree = (u_short)(tnFree - nbytes);
884 bytes = rxi_WriteProc(call, buf, nbytes);
886 MUTEX_EXIT(&call->lock);
891 /* Optimization for marshalling 32 bit arguments */
893 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
901 /* Free any packets from the last call to ReadvProc/WritevProc */
903 MUTEX_ENTER(&call->lock);
904 if (queue_IsNotEmpty(&call->iovq)) {
905 #ifdef RXDEBUG_PACKET
907 #endif /* RXDEBUG_PACKET */
908 rxi_FreePackets(0, &call->iovq);
912 * Most common case: all of the data fits in the current iovec.
913 * We are relying on nFree being zero unless the call is in send mode.
915 tcurlen = call->curlen;
916 tnFree = call->nFree;
917 if (!call->error && tcurlen >= sizeof(afs_int32)
918 && tnFree >= sizeof(afs_int32)) {
919 tcurpos = call->curpos;
920 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
921 *((afs_int32 *) (tcurpos)) = *value;
923 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
925 call->curpos = tcurpos + sizeof(afs_int32);
926 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
927 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
928 bytes = sizeof(afs_int32);
930 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
932 MUTEX_EXIT(&call->lock);
937 /* rxi_WritevAlloc -- internal version.
939 * Fill in an iovec to point to data in packet buffers. The application
940 * calls rxi_WritevProc when the buffers are full.
942 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
945 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
948 struct rx_connection *conn = call->conn;
949 struct rx_packet *cp = call->currentPacket;
952 /* Temporary values, real work is done in rxi_WritevProc */
954 unsigned int tcurvec;
958 requestCount = nbytes;
961 /* Free any packets from the last call to ReadvProc/WritevProc */
962 if (queue_IsNotEmpty(&call->iovq)) {
963 #ifdef RXDEBUG_PACKET
965 #endif /* RXDEBUG_PACKET */
966 rxi_FreePackets(0, &call->iovq);
969 if (call->mode != RX_MODE_SENDING) {
970 if ((conn->type == RX_SERVER_CONNECTION)
971 && (call->mode == RX_MODE_RECEIVING)) {
972 call->mode = RX_MODE_SENDING;
974 cp->flags &= ~RX_PKTFLAG_CP;
976 cp = call->currentPacket = (struct rx_packet *)0;
985 /* Set up the iovec to point to data in packet buffers. */
986 tnFree = call->nFree;
987 tcurvec = call->curvec;
988 tcurpos = call->curpos;
989 tcurlen = call->curlen;
994 /* current packet is full, allocate a new one */
995 cp = rxi_AllocSendPacket(call, nbytes);
997 /* out of space, return what we have */
999 return requestCount - nbytes;
1001 cp->flags |= RX_PKTFLAG_IOVQ;
1002 queue_Append(&call->iovq, cp);
1003 #ifdef RXDEBUG_PACKET
1005 #endif /* RXDEBUG_PACKET */
1006 tnFree = cp->length;
1009 (char *)cp->wirevec[1].iov_base +
1010 call->conn->securityHeaderSize;
1011 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1014 if (tnFree < nbytes) {
1015 /* try to extend the current packet */
1018 mud = rx_MaxUserDataSize(call);
1021 want = MIN(nbytes - tnFree, mud - len);
1022 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1023 if (cp->length > (unsigned)mud)
1025 tnFree += (cp->length - len);
1026 if (cp == call->currentPacket) {
1027 call->nFree += (cp->length - len);
1032 /* fill in the next entry in the iovec */
1033 t = MIN(tcurlen, nbytes);
1035 iov[nextio].iov_base = tcurpos;
1036 iov[nextio].iov_len = t;
1044 /* need to get another struct iov */
1045 if (++tcurvec >= cp->niovecs) {
1046 /* current packet is full, extend it or move on to next packet */
1049 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1050 tcurlen = cp->wirevec[tcurvec].iov_len;
1053 } while (nbytes && nextio < maxio);
1055 return requestCount - nbytes;
1059 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1066 MUTEX_ENTER(&call->lock);
1067 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1068 MUTEX_EXIT(&call->lock);
1073 /* rxi_WritevProc -- internal version.
1075 * Send buffers allocated in rxi_WritevAlloc.
1077 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1080 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1082 struct rx_packet *cp = NULL;
1083 struct rx_packet *p, *np;
1086 struct rx_queue tmpq;
1087 #ifdef RXDEBUG_PACKET
1091 requestCount = nbytes;
1094 if (call->mode != RX_MODE_SENDING) {
1095 call->error = RX_PROTOCOL_ERROR;
1097 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1098 /* Wait until TQ_BUSY is reset before trying to move any
1099 * packets to the transmit queue. */
1100 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1101 call->flags |= RX_CALL_TQ_WAIT;
1103 #ifdef RX_ENABLE_LOCKS
1104 CV_WAIT(&call->cv_tq, &call->lock);
1105 #else /* RX_ENABLE_LOCKS */
1106 osi_rxSleep(&call->tq);
1107 #endif /* RX_ENABLE_LOCKS */
1109 if (call->tqWaiters == 0)
1110 call->flags &= ~RX_CALL_TQ_WAIT;
1112 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1113 /* cp is no longer valid since we may have given up the lock */
1114 cp = call->currentPacket;
1118 cp->flags &= ~RX_PKTFLAG_CP;
1119 cp->flags |= RX_PKTFLAG_IOVQ;
1120 queue_Prepend(&call->iovq, cp);
1121 #ifdef RXDEBUG_PACKET
1123 #endif /* RXDEBUG_PACKET */
1124 cp = call->currentPacket = (struct rx_packet *)0;
1126 #ifdef RXDEBUG_PACKET
1128 #endif /* RXDEBUG_PACKET */
1129 rxi_FreePackets(0, &call->iovq);
1133 /* Loop through the I/O vector adjusting packet pointers.
1134 * Place full packets back onto the iovq once they are ready
1135 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1136 * the iovec. We put the loop condition at the end to ensure that
1137 * a zero length write will push a short packet. */
1140 #ifdef RXDEBUG_PACKET
1142 #endif /* RXDEBUG_PACKET */
1144 if (call->nFree == 0 && cp) {
1145 clock_NewTime(); /* Bogus: need new time package */
1146 /* The 0, below, specifies that it is not the last packet:
1147 * there will be others. PrepareSendPacket may
1148 * alter the packet length by up to
1149 * conn->securityMaxTrailerSize */
1150 hadd32(call->bytesSent, cp->length);
1151 rxi_PrepareSendPacket(call, cp, 0);
1152 queue_Append(&tmpq, cp);
1153 #ifdef RXDEBUG_PACKET
1155 #endif /* RXDEBUG_PACKET */
1156 cp = call->currentPacket = (struct rx_packet *)0;
1158 /* The head of the iovq is now the current packet */
1160 if (queue_IsEmpty(&call->iovq)) {
1161 call->error = RX_PROTOCOL_ERROR;
1162 #ifdef RXDEBUG_PACKET
1164 #endif /* RXDEBUG_PACKET */
1165 rxi_FreePackets(0, &tmpq);
1168 cp = queue_First(&call->iovq, rx_packet);
1170 cp->flags &= ~RX_PKTFLAG_IOVQ;
1171 #ifdef RXDEBUG_PACKET
1173 #endif /* RXDEBUG_PACKET */
1174 cp->flags |= RX_PKTFLAG_CP;
1175 call->currentPacket = cp;
1176 call->nFree = cp->length;
1179 (char *)cp->wirevec[1].iov_base +
1180 call->conn->securityHeaderSize;
1182 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1187 /* The next iovec should point to the current position */
1188 if (iov[nextio].iov_base != call->curpos
1189 || iov[nextio].iov_len > (int)call->curlen) {
1190 call->error = RX_PROTOCOL_ERROR;
1192 cp->flags &= ~RX_PKTFLAG_CP;
1193 queue_Prepend(&tmpq, cp);
1194 #ifdef RXDEBUG_PACKET
1196 #endif /* RXDEBUG_PACKET */
1197 cp = call->currentPacket = (struct rx_packet *)0;
1199 #ifdef RXDEBUG_PACKET
1201 #endif /* RXDEBUG_PACKET */
1202 rxi_FreePackets(0, &tmpq);
1205 nbytes -= iov[nextio].iov_len;
1206 call->curpos += iov[nextio].iov_len;
1207 call->curlen -= iov[nextio].iov_len;
1208 call->nFree -= iov[nextio].iov_len;
1210 if (call->curlen == 0) {
1211 if (++call->curvec > cp->niovecs) {
1214 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1215 call->curlen = cp->wirevec[call->curvec].iov_len;
1219 } while (nbytes && nextio < nio);
1221 /* Move the packets from the temporary queue onto the transmit queue.
1222 * We may end up with more than call->twind packets on the queue. */
1224 for (queue_Scan(&tmpq, p, np, rx_packet))
1226 p->flags |= RX_PKTFLAG_TQ;
1228 queue_SpliceAppend(&call->tq, &tmpq);
1230 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1231 rxi_Start(0, call, 0, 0);
1234 /* Wait for the length of the transmit queue to fall below call->twind */
1235 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1237 call->startWait = clock_Sec();
1238 #ifdef RX_ENABLE_LOCKS
1239 CV_WAIT(&call->cv_twind, &call->lock);
1241 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1242 osi_rxSleep(&call->twind);
1244 call->startWait = 0;
1246 /* cp is no longer valid since we may have given up the lock */
1247 cp = call->currentPacket;
1251 cp->flags &= ~RX_PKTFLAG_CP;
1253 cp = call->currentPacket = (struct rx_packet *)0;
1258 return requestCount - nbytes;
1262 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1268 MUTEX_ENTER(&call->lock);
1269 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1270 MUTEX_EXIT(&call->lock);
1275 /* Flush any buffered data to the stream, switch to read mode
1276 * (clients) or to EOF mode (servers) */
1278 rxi_FlushWrite(struct rx_call *call)
1280 struct rx_packet *cp = NULL;
1282 /* Free any packets from the last call to ReadvProc/WritevProc */
1283 if (queue_IsNotEmpty(&call->iovq)) {
1284 #ifdef RXDEBUG_PACKET
1286 #endif /* RXDEBUG_PACKET */
1287 rxi_FreePackets(0, &call->iovq);
1290 if (call->mode == RX_MODE_SENDING) {
1293 (call->conn->type ==
1294 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1296 #ifdef RX_KERNEL_TRACE
1298 int glockOwner = ISAFS_GLOCK();
1301 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1302 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1309 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1310 /* Wait until TQ_BUSY is reset before adding any
1311 * packets to the transmit queue
1313 while (call->flags & RX_CALL_TQ_BUSY) {
1314 call->flags |= RX_CALL_TQ_WAIT;
1316 #ifdef RX_ENABLE_LOCKS
1317 CV_WAIT(&call->cv_tq, &call->lock);
1318 #else /* RX_ENABLE_LOCKS */
1319 osi_rxSleep(&call->tq);
1320 #endif /* RX_ENABLE_LOCKS */
1322 if (call->tqWaiters == 0)
1323 call->flags &= ~RX_CALL_TQ_WAIT;
1325 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1327 /* cp is no longer valid since we may have given up the lock */
1328 cp = call->currentPacket;
1331 /* cp->length is only supposed to be the user's data */
1332 /* cp->length was already set to (then-current)
1333 * MaxUserDataSize or less. */
1334 cp->flags &= ~RX_PKTFLAG_CP;
1335 cp->length -= call->nFree;
1336 call->currentPacket = (struct rx_packet *)0;
1339 cp = rxi_AllocSendPacket(call, 0);
1341 /* Mode can no longer be MODE_SENDING */
1345 cp->niovecs = 2; /* header + space for rxkad stuff */
1349 /* The 1 specifies that this is the last packet */
1350 hadd32(call->bytesSent, cp->length);
1351 rxi_PrepareSendPacket(call, cp, 1);
1352 cp->flags |= RX_PKTFLAG_TQ;
1353 queue_Append(&call->tq, cp);
1354 #ifdef RXDEBUG_PACKET
1356 #endif /* RXDEBUG_PACKET */
1359 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1360 rxi_Start(0, call, 0, 0);
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 MUTEX_ENTER(&call->lock);
1373 rxi_FlushWrite(call);
1374 MUTEX_EXIT(&call->lock);