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"
69 #endif /* AFS_OSF_ENV */
71 # include <sys/types.h>
73 # include <winsock2.h>
74 #else /* !AFS_NT40_ENV */
75 # include <sys/socket.h>
76 # include <sys/file.h>
78 # include <netinet/in.h>
79 # include <sys/stat.h>
80 # include <sys/time.h>
81 #endif /* !AFS_NT40_ENV */
87 # include "rx_clock.h"
88 # include "rx_queue.h"
90 # include "rx_globals.h"
94 /* rxdb_fileID is used to identify the lock location, along with line#. */
95 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
96 #endif /* RX_LOCKS_DB */
97 /* rxi_ReadProc -- internal version.
99 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
102 rxi_ReadProc(struct rx_call *call, char *buf,
105 struct rx_packet *cp = call->currentPacket;
106 struct rx_packet *rp;
110 /* XXXX took out clock_NewTime from here. Was it needed? */
111 requestCount = nbytes;
113 /* Free any packets from the last call to ReadvProc/WritevProc */
114 if (queue_IsNotEmpty(&call->iovq)) {
115 #ifdef RXDEBUG_PACKET
117 #endif /* RXDEBUG_PACKET */
118 rxi_FreePackets(0, &call->iovq);
122 if (call->nLeft == 0) {
123 /* Get next packet */
125 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
129 if (call->mode == RX_MODE_SENDING) {
130 rxi_FlushWrite(call);
134 if (queue_IsNotEmpty(&call->rq)) {
135 /* Check that next packet available is next in sequence */
136 rp = queue_First(&call->rq, rx_packet);
137 if (rp->header.seq == call->rnext) {
139 struct rx_connection *conn = call->conn;
141 rp->flags &= ~RX_PKTFLAG_RQ;
142 #ifdef RXDEBUG_PACKET
144 #endif /* RXDEBUG_PACKET */
146 /* RXS_CheckPacket called to undo RXS_PreparePacket's
147 * work. It may reduce the length of the packet by up
148 * to conn->maxTrailerSize, to reflect the length of the
149 * data + the header. */
151 RXS_CheckPacket(conn->securityObject, call,
153 /* Used to merely shut down the call, but now we
154 * shut down the whole connection since this may
155 * indicate an attempt to hijack it */
157 MUTEX_EXIT(&call->lock);
158 rxi_ConnectionError(conn, error);
159 MUTEX_ENTER(&conn->conn_data_lock);
160 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
161 MUTEX_EXIT(&conn->conn_data_lock);
163 MUTEX_ENTER(&call->lock);
168 cp = call->currentPacket = rp;
169 call->currentPacket->flags |= RX_PKTFLAG_CP;
170 call->curvec = 1; /* 0th vec is always header */
171 /* begin at the beginning [ more or less ], continue
172 * on until the end, then stop. */
174 (char *)cp->wirevec[1].iov_base +
175 call->conn->securityHeaderSize;
177 cp->wirevec[1].iov_len -
178 call->conn->securityHeaderSize;
180 /* Notice that this code works correctly if the data
181 * size is 0 (which it may be--no reply arguments from
182 * server, for example). This relies heavily on the
183 * fact that the code below immediately frees the packet
184 * (no yields, etc.). If it didn't, this would be a
185 * problem because a value of zero for call->nLeft
186 * normally means that there is no read packet */
187 call->nLeft = cp->length;
188 hadd32(call->bytesRcvd, cp->length);
190 /* Send a hard ack for every rxi_HardAckRate+1 packets
191 * consumed. Otherwise schedule an event to send
192 * the hard ack later on.
195 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
196 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
197 rxevent_Cancel(call->delayedAckEvent, call,
198 RX_CALL_REFCOUNT_DELAY);
199 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
201 struct clock when, now;
204 /* Delay to consolidate ack packets */
205 clock_Add(&when, &rx_hardAckDelay);
206 if (!call->delayedAckEvent
207 || clock_Gt(&call->delayedAckEvent->
209 rxevent_Cancel(call->delayedAckEvent,
211 RX_CALL_REFCOUNT_DELAY);
212 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
213 call->delayedAckEvent =
214 rxevent_PostNow(&when, &now,
215 rxi_SendDelayedAck, call,
225 * If we reach this point either we have no packets in the
226 * receive queue or the next packet in the queue is not the
227 * one we are looking for. There is nothing else for us to
228 * do but wait for another packet to arrive.
231 /* Are there ever going to be any more packets? */
232 if (call->flags & RX_CALL_RECEIVE_DONE) {
233 return requestCount - nbytes;
235 /* Wait for in-sequence packet */
236 call->flags |= RX_CALL_READER_WAIT;
238 call->startWait = clock_Sec();
239 while (call->flags & RX_CALL_READER_WAIT) {
240 #ifdef RX_ENABLE_LOCKS
241 CV_WAIT(&call->cv_rq, &call->lock);
243 osi_rxSleep(&call->rq);
246 /* cp is no longer valid since we may have given up the lock */
247 cp = call->currentPacket;
250 #ifdef RX_ENABLE_LOCKS
254 #endif /* RX_ENABLE_LOCKS */
258 /* MTUXXX this should be replaced by some error-recovery code before shipping */
259 /* yes, the following block is allowed to be the ELSE clause (or not) */
260 /* It's possible for call->nLeft to be smaller than any particular
261 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
262 * reflects the size of the buffer. We have to keep track of the
263 * number of bytes read in the length field of the packet struct. On
264 * the final portion of a received packet, it's almost certain that
265 * call->nLeft will be smaller than the final buffer. */
266 while (nbytes && cp) {
267 t = MIN((int)call->curlen, nbytes);
268 t = MIN(t, (int)call->nLeft);
269 memcpy(buf, call->curpos, t);
277 /* out of packet. Get another one. */
278 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
280 cp = call->currentPacket = (struct rx_packet *)0;
281 } else if (!call->curlen) {
282 /* need to get another struct iov */
283 if (++call->curvec >= cp->niovecs) {
284 /* current packet is exhausted, get ready for another */
285 /* don't worry about curvec and stuff, they get set somewhere else */
286 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
288 cp = call->currentPacket = (struct rx_packet *)0;
292 (char *)cp->wirevec[call->curvec].iov_base;
293 call->curlen = cp->wirevec[call->curvec].iov_len;
298 /* user buffer is full, return */
308 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
316 /* Free any packets from the last call to ReadvProc/WritevProc */
318 MUTEX_ENTER(&call->lock);
319 if (!queue_IsEmpty(&call->iovq)) {
320 #ifdef RXDEBUG_PACKET
322 #endif /* RXDEBUG_PACKET */
323 rxi_FreePackets(0, &call->iovq);
327 * Most common case, all of the data is in the current iovec.
328 * We are relying on nLeft being zero unless the call is in receive mode.
330 tcurlen = call->curlen;
331 tnLeft = call->nLeft;
332 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
333 tcurpos = call->curpos;
334 memcpy(buf, tcurpos, nbytes);
335 call->curpos = tcurpos + nbytes;
336 call->curlen = tcurlen - nbytes;
337 call->nLeft = tnLeft - nbytes;
339 if (!call->nLeft && call->currentPacket != NULL) {
340 /* out of packet. Get another one. */
341 rxi_FreePacket(call->currentPacket);
342 call->currentPacket = (struct rx_packet *)0;
346 bytes = rxi_ReadProc(call, buf, nbytes);
348 MUTEX_EXIT(&call->lock);
353 /* Optimization for unmarshalling 32 bit integers */
355 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
363 /* Free any packets from the last call to ReadvProc/WritevProc */
365 MUTEX_ENTER(&call->lock);
366 if (!queue_IsEmpty(&call->iovq)) {
367 #ifdef RXDEBUG_PACKET
369 #endif /* RXDEBUG_PACKET */
370 rxi_FreePackets(0, &call->iovq);
374 * Most common case, all of the data is in the current iovec.
375 * We are relying on nLeft being zero unless the call is in receive mode.
377 tcurlen = call->curlen;
378 tnLeft = call->nLeft;
379 if (!call->error && tcurlen >= sizeof(afs_int32)
380 && tnLeft >= sizeof(afs_int32)) {
381 tcurpos = call->curpos;
382 memcpy((char *)value, tcurpos, sizeof(afs_int32));
383 call->curpos = tcurpos + sizeof(afs_int32);
384 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
385 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
386 if (!call->nLeft && call->currentPacket != NULL) {
387 /* out of packet. Get another one. */
388 rxi_FreePacket(call->currentPacket);
389 call->currentPacket = (struct rx_packet *)0;
391 bytes = sizeof(afs_int32);
393 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
395 MUTEX_EXIT(&call->lock);
402 * Uses packets in the receive queue to fill in as much of the
403 * current iovec as possible. Does not block if it runs out
404 * of packets to complete the iovec. Return true if an ack packet
405 * was sent, otherwise return false */
407 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
412 struct rx_packet *rp;
413 struct rx_packet *curp;
414 struct iovec *call_iov;
415 struct iovec *cur_iov = NULL;
417 curp = call->currentPacket;
419 cur_iov = &curp->wirevec[call->curvec];
421 call_iov = &call->iov[call->iovNext];
423 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
424 if (call->nLeft == 0) {
425 /* Get next packet */
426 if (queue_IsNotEmpty(&call->rq)) {
427 /* Check that next packet available is next in sequence */
428 rp = queue_First(&call->rq, rx_packet);
429 if (rp->header.seq == call->rnext) {
431 struct rx_connection *conn = call->conn;
433 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 call->currentPacket->flags |= RX_PKTFLAG_CP;
461 call->curvec = 1; /* 0th vec is always header */
462 cur_iov = &curp->wirevec[1];
463 /* begin at the beginning [ more or less ], continue
464 * on until the end, then stop. */
466 (char *)curp->wirevec[1].iov_base +
467 call->conn->securityHeaderSize;
469 curp->wirevec[1].iov_len -
470 call->conn->securityHeaderSize;
472 /* Notice that this code works correctly if the data
473 * size is 0 (which it may be--no reply arguments from
474 * server, for example). This relies heavily on the
475 * fact that the code below immediately frees the packet
476 * (no yields, etc.). If it didn't, this would be a
477 * problem because a value of zero for call->nLeft
478 * normally means that there is no read packet */
479 call->nLeft = curp->length;
480 hadd32(call->bytesRcvd, curp->length);
482 /* Send a hard ack for every rxi_HardAckRate+1 packets
483 * consumed. Otherwise schedule an event to send
484 * the hard ack later on.
494 /* It's possible for call->nLeft to be smaller than any particular
495 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
496 * reflects the size of the buffer. We have to keep track of the
497 * number of bytes read in the length field of the packet struct. On
498 * the final portion of a received packet, it's almost certain that
499 * call->nLeft will be smaller than the final buffer. */
500 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
502 t = MIN((int)call->curlen, call->iovNBytes);
503 t = MIN(t, (int)call->nLeft);
504 call_iov->iov_base = call->curpos;
505 call_iov->iov_len = t;
508 call->iovNBytes -= t;
514 /* out of packet. Get another one. */
515 curp->flags &= ~RX_PKTFLAG_CP;
516 curp->flags |= RX_PKTFLAG_IOVQ;
517 queue_Append(&call->iovq, curp);
518 #ifdef RXDEBUG_PACKET
520 #endif /* RXDEBUG_PACKET */
521 curp = call->currentPacket = (struct rx_packet *)0;
522 } else if (!call->curlen) {
523 /* need to get another struct iov */
524 if (++call->curvec >= curp->niovecs) {
525 /* current packet is exhausted, get ready for another */
526 /* don't worry about curvec and stuff, they get set somewhere else */
527 curp->flags &= ~RX_PKTFLAG_CP;
528 curp->flags |= RX_PKTFLAG_IOVQ;
529 queue_Append(&call->iovq, curp);
530 #ifdef RXDEBUG_PACKET
532 #endif /* RXDEBUG_PACKET */
533 curp = call->currentPacket = (struct rx_packet *)0;
537 call->curpos = (char *)cur_iov->iov_base;
538 call->curlen = cur_iov->iov_len;
544 /* If we consumed any packets then check whether we need to
545 * send a hard ack. */
546 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
547 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
548 rxevent_Cancel(call->delayedAckEvent, call,
549 RX_CALL_REFCOUNT_DELAY);
550 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
553 struct clock when, now;
556 /* Delay to consolidate ack packets */
557 clock_Add(&when, &rx_hardAckDelay);
558 if (!call->delayedAckEvent
559 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
560 rxevent_Cancel(call->delayedAckEvent, call,
561 RX_CALL_REFCOUNT_DELAY);
562 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
563 call->delayedAckEvent =
564 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
572 /* rxi_ReadvProc -- internal version.
574 * Fills in an iovec with pointers to the packet buffers. All packets
575 * except the last packet (new current packet) are moved to the iovq
576 * while the application is processing the data.
578 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
581 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
587 requestCount = nbytes;
590 /* Free any packets from the last call to ReadvProc/WritevProc */
591 if (queue_IsNotEmpty(&call->iovq)) {
592 #ifdef RXDEBUG_PACKET
594 #endif /* RXDEBUG_PACKET */
595 rxi_FreePackets(0, &call->iovq);
598 if (call->mode == RX_MODE_SENDING) {
599 rxi_FlushWrite(call);
606 /* Get whatever data is currently available in the receive queue.
607 * If rxi_FillReadVec sends an ack packet then it is possible
608 * that we will receive more data while we drop the call lock
609 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
610 * here to avoid a race with the receive thread if we send
611 * hard acks in rxi_FillReadVec. */
612 call->flags |= RX_CALL_IOVEC_WAIT;
613 call->iovNBytes = nbytes;
614 call->iovMax = maxio;
617 rxi_FillReadVec(call, 0);
619 /* if we need more data then sleep until the receive thread has
620 * filled in the rest. */
621 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
622 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
623 call->flags |= RX_CALL_READER_WAIT;
625 call->startWait = clock_Sec();
626 while (call->flags & RX_CALL_READER_WAIT) {
627 #ifdef RX_ENABLE_LOCKS
628 CV_WAIT(&call->cv_rq, &call->lock);
630 osi_rxSleep(&call->rq);
635 call->flags &= ~RX_CALL_IOVEC_WAIT;
636 #ifdef RX_ENABLE_LOCKS
640 #endif /* RX_ENABLE_LOCKS */
643 *nio = call->iovNext;
644 return nbytes - call->iovNBytes;
648 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
655 MUTEX_ENTER(&call->lock);
656 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
657 MUTEX_EXIT(&call->lock);
662 /* rxi_WriteProc -- internal version.
664 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
667 rxi_WriteProc(struct rx_call *call, char *buf,
670 struct rx_connection *conn = call->conn;
671 struct rx_packet *cp = call->currentPacket;
673 int requestCount = nbytes;
675 /* Free any packets from the last call to ReadvProc/WritevProc */
676 if (queue_IsNotEmpty(&call->iovq)) {
677 #ifdef RXDEBUG_PACKET
679 #endif /* RXDEBUG_PACKET */
680 rxi_FreePackets(0, &call->iovq);
683 if (call->mode != RX_MODE_SENDING) {
684 if ((conn->type == RX_SERVER_CONNECTION)
685 && (call->mode == RX_MODE_RECEIVING)) {
686 call->mode = RX_MODE_SENDING;
688 cp->flags &= ~RX_PKTFLAG_CP;
690 cp = call->currentPacket = (struct rx_packet *)0;
699 /* Loop condition is checked at end, so that a write of 0 bytes
700 * will force a packet to be created--specially for the case where
701 * there are 0 bytes on the stream, but we must send a packet
704 if (call->nFree == 0) {
705 if (!call->error && cp) {
706 /* Clear the current packet now so that if
707 * we are forced to wait and drop the lock
708 * the packet we are planning on using
711 cp->flags &= ~RX_PKTFLAG_CP;
712 call->currentPacket = (struct rx_packet *)0;
713 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
714 /* Wait until TQ_BUSY is reset before adding any
715 * packets to the transmit queue
717 while (call->flags & RX_CALL_TQ_BUSY) {
718 call->flags |= RX_CALL_TQ_WAIT;
720 #ifdef RX_ENABLE_LOCKS
721 CV_WAIT(&call->cv_tq, &call->lock);
722 #else /* RX_ENABLE_LOCKS */
723 osi_rxSleep(&call->tq);
724 #endif /* RX_ENABLE_LOCKS */
726 if (call->tqWaiters == 0)
727 call->flags &= ~RX_CALL_TQ_WAIT;
729 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
730 clock_NewTime(); /* Bogus: need new time package */
731 /* The 0, below, specifies that it is not the last packet:
732 * there will be others. PrepareSendPacket may
733 * alter the packet length by up to
734 * conn->securityMaxTrailerSize */
735 hadd32(call->bytesSent, cp->length);
736 rxi_PrepareSendPacket(call, cp, 0);
737 cp->flags |= RX_PKTFLAG_TQ;
738 queue_Append(&call->tq, cp);
739 #ifdef RXDEBUG_PACKET
741 #endif /* RXDEBUG_PACKET */
742 cp = (struct rx_packet *)0;
745 flags & (RX_CALL_FAST_RECOVER |
746 RX_CALL_FAST_RECOVER_WAIT))) {
747 rxi_Start(0, call, 0, 0);
750 cp->flags &= ~RX_PKTFLAG_CP;
752 cp = call->currentPacket = (struct rx_packet *)0;
754 /* Wait for transmit window to open up */
756 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
758 call->startWait = clock_Sec();
760 #ifdef RX_ENABLE_LOCKS
761 CV_WAIT(&call->cv_twind, &call->lock);
763 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
764 osi_rxSleep(&call->twind);
768 #ifdef RX_ENABLE_LOCKS
772 #endif /* RX_ENABLE_LOCKS */
774 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
775 cp->flags |= RX_PKTFLAG_CP;
776 call->currentPacket = cp;
777 call->nFree = cp->length;
778 call->curvec = 1; /* 0th vec is always header */
779 /* begin at the beginning [ more or less ], continue
780 * on until the end, then stop. */
782 (char *)cp->wirevec[1].iov_base +
783 call->conn->securityHeaderSize;
785 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
789 cp->flags &= ~RX_PKTFLAG_CP;
791 call->currentPacket = NULL;
797 if (cp && (int)call->nFree < nbytes) {
798 /* Try to extend the current buffer */
801 mud = rx_MaxUserDataSize(call);
804 want = MIN(nbytes - (int)call->nFree, mud - len);
805 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
806 if (cp->length > (unsigned)mud)
808 call->nFree += (cp->length - len);
812 /* If the remaining bytes fit in the buffer, then store them
813 * and return. Don't ship a buffer that's full immediately to
814 * the peer--we don't know if it's the last buffer yet */
820 while (nbytes && call->nFree) {
822 t = MIN((int)call->curlen, nbytes);
823 t = MIN((int)call->nFree, t);
824 memcpy(call->curpos, buf, t);
828 call->curlen -= (u_short)t;
829 call->nFree -= (u_short)t;
832 /* need to get another struct iov */
833 if (++call->curvec >= cp->niovecs) {
834 /* current packet is full, extend or send it */
837 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
838 call->curlen = cp->wirevec[call->curvec].iov_len;
841 } /* while bytes to send and room to send them */
843 /* might be out of space now */
846 } else; /* more data to send, so get another packet and keep going */
849 return requestCount - nbytes;
853 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
861 /* Free any packets from the last call to ReadvProc/WritevProc */
863 MUTEX_ENTER(&call->lock);
864 if (queue_IsNotEmpty(&call->iovq)) {
865 #ifdef RXDEBUG_PACKET
867 #endif /* RXDEBUG_PACKET */
868 rxi_FreePackets(0, &call->iovq);
872 * Most common case: all of the data fits in the current iovec.
873 * We are relying on nFree being zero unless the call is in send mode.
875 tcurlen = (int)call->curlen;
876 tnFree = (int)call->nFree;
877 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
878 tcurpos = call->curpos;
879 memcpy(tcurpos, buf, nbytes);
880 call->curpos = tcurpos + nbytes;
881 call->curlen = (u_short)(tcurlen - nbytes);
882 call->nFree = (u_short)(tnFree - nbytes);
885 bytes = rxi_WriteProc(call, buf, nbytes);
887 MUTEX_EXIT(&call->lock);
892 /* Optimization for marshalling 32 bit arguments */
894 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
902 /* Free any packets from the last call to ReadvProc/WritevProc */
904 MUTEX_ENTER(&call->lock);
905 if (queue_IsNotEmpty(&call->iovq)) {
906 #ifdef RXDEBUG_PACKET
908 #endif /* RXDEBUG_PACKET */
909 rxi_FreePackets(0, &call->iovq);
913 * Most common case: all of the data fits in the current iovec.
914 * We are relying on nFree being zero unless the call is in send mode.
916 tcurlen = call->curlen;
917 tnFree = call->nFree;
918 if (!call->error && tcurlen >= sizeof(afs_int32)
919 && tnFree >= sizeof(afs_int32)) {
920 tcurpos = call->curpos;
921 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
922 *((afs_int32 *) (tcurpos)) = *value;
924 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
926 call->curpos = tcurpos + sizeof(afs_int32);
927 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
928 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
929 bytes = sizeof(afs_int32);
931 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
933 MUTEX_EXIT(&call->lock);
938 /* rxi_WritevAlloc -- internal version.
940 * Fill in an iovec to point to data in packet buffers. The application
941 * calls rxi_WritevProc when the buffers are full.
943 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
946 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
949 struct rx_connection *conn = call->conn;
950 struct rx_packet *cp = call->currentPacket;
953 /* Temporary values, real work is done in rxi_WritevProc */
955 unsigned int tcurvec;
959 requestCount = nbytes;
962 /* Free any packets from the last call to ReadvProc/WritevProc */
963 if (queue_IsNotEmpty(&call->iovq)) {
964 #ifdef RXDEBUG_PACKET
966 #endif /* RXDEBUG_PACKET */
967 rxi_FreePackets(0, &call->iovq);
970 if (call->mode != RX_MODE_SENDING) {
971 if ((conn->type == RX_SERVER_CONNECTION)
972 && (call->mode == RX_MODE_RECEIVING)) {
973 call->mode = RX_MODE_SENDING;
975 cp->flags &= ~RX_PKTFLAG_CP;
977 cp = call->currentPacket = (struct rx_packet *)0;
986 /* Set up the iovec to point to data in packet buffers. */
987 tnFree = call->nFree;
988 tcurvec = call->curvec;
989 tcurpos = call->curpos;
990 tcurlen = call->curlen;
995 /* current packet is full, allocate a new one */
996 cp = rxi_AllocSendPacket(call, nbytes);
998 /* out of space, return what we have */
1000 return requestCount - nbytes;
1002 cp->flags |= RX_PKTFLAG_IOVQ;
1003 queue_Append(&call->iovq, cp);
1004 #ifdef RXDEBUG_PACKET
1006 #endif /* RXDEBUG_PACKET */
1007 tnFree = cp->length;
1010 (char *)cp->wirevec[1].iov_base +
1011 call->conn->securityHeaderSize;
1012 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1015 if (tnFree < nbytes) {
1016 /* try to extend the current packet */
1019 mud = rx_MaxUserDataSize(call);
1022 want = MIN(nbytes - tnFree, mud - len);
1023 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1024 if (cp->length > (unsigned)mud)
1026 tnFree += (cp->length - len);
1027 if (cp == call->currentPacket) {
1028 call->nFree += (cp->length - len);
1033 /* fill in the next entry in the iovec */
1034 t = MIN(tcurlen, nbytes);
1036 iov[nextio].iov_base = tcurpos;
1037 iov[nextio].iov_len = t;
1045 /* need to get another struct iov */
1046 if (++tcurvec >= cp->niovecs) {
1047 /* current packet is full, extend it or move on to next packet */
1050 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1051 tcurlen = cp->wirevec[tcurvec].iov_len;
1054 } while (nbytes && nextio < maxio);
1056 return requestCount - nbytes;
1060 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1067 MUTEX_ENTER(&call->lock);
1068 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1069 MUTEX_EXIT(&call->lock);
1074 /* rxi_WritevProc -- internal version.
1076 * Send buffers allocated in rxi_WritevAlloc.
1078 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1081 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1083 struct rx_packet *cp = NULL;
1084 struct rx_packet *p, *np;
1087 struct rx_queue tmpq;
1088 #ifdef RXDEBUG_PACKET
1092 requestCount = nbytes;
1095 if (call->mode != RX_MODE_SENDING) {
1096 call->error = RX_PROTOCOL_ERROR;
1098 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1099 /* Wait until TQ_BUSY is reset before trying to move any
1100 * packets to the transmit queue. */
1101 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1102 call->flags |= RX_CALL_TQ_WAIT;
1104 #ifdef RX_ENABLE_LOCKS
1105 CV_WAIT(&call->cv_tq, &call->lock);
1106 #else /* RX_ENABLE_LOCKS */
1107 osi_rxSleep(&call->tq);
1108 #endif /* RX_ENABLE_LOCKS */
1110 if (call->tqWaiters == 0)
1111 call->flags &= ~RX_CALL_TQ_WAIT;
1113 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1114 /* cp is no longer valid since we may have given up the lock */
1115 cp = call->currentPacket;
1119 cp->flags &= ~RX_PKTFLAG_CP;
1120 cp->flags |= RX_PKTFLAG_IOVQ;
1121 queue_Prepend(&call->iovq, cp);
1122 #ifdef RXDEBUG_PACKET
1124 #endif /* RXDEBUG_PACKET */
1125 cp = call->currentPacket = (struct rx_packet *)0;
1127 #ifdef RXDEBUG_PACKET
1129 #endif /* RXDEBUG_PACKET */
1130 rxi_FreePackets(0, &call->iovq);
1134 /* Loop through the I/O vector adjusting packet pointers.
1135 * Place full packets back onto the iovq once they are ready
1136 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1137 * the iovec. We put the loop condition at the end to ensure that
1138 * a zero length write will push a short packet. */
1141 #ifdef RXDEBUG_PACKET
1143 #endif /* RXDEBUG_PACKET */
1145 if (call->nFree == 0 && cp) {
1146 clock_NewTime(); /* Bogus: need new time package */
1147 /* The 0, below, specifies that it is not the last packet:
1148 * there will be others. PrepareSendPacket may
1149 * alter the packet length by up to
1150 * conn->securityMaxTrailerSize */
1151 hadd32(call->bytesSent, cp->length);
1152 rxi_PrepareSendPacket(call, cp, 0);
1153 queue_Append(&tmpq, cp);
1154 #ifdef RXDEBUG_PACKET
1156 #endif /* RXDEBUG_PACKET */
1157 cp = call->currentPacket = (struct rx_packet *)0;
1159 /* The head of the iovq is now the current packet */
1161 if (queue_IsEmpty(&call->iovq)) {
1162 call->error = RX_PROTOCOL_ERROR;
1163 #ifdef RXDEBUG_PACKET
1165 #endif /* RXDEBUG_PACKET */
1166 rxi_FreePackets(0, &tmpq);
1169 cp = queue_First(&call->iovq, rx_packet);
1171 cp->flags &= ~RX_PKTFLAG_IOVQ;
1172 #ifdef RXDEBUG_PACKET
1174 #endif /* RXDEBUG_PACKET */
1175 cp->flags |= RX_PKTFLAG_CP;
1176 call->currentPacket = cp;
1177 call->nFree = cp->length;
1180 (char *)cp->wirevec[1].iov_base +
1181 call->conn->securityHeaderSize;
1183 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1188 /* The next iovec should point to the current position */
1189 if (iov[nextio].iov_base != call->curpos
1190 || iov[nextio].iov_len > (int)call->curlen) {
1191 call->error = RX_PROTOCOL_ERROR;
1193 cp->flags &= ~RX_PKTFLAG_CP;
1194 queue_Prepend(&tmpq, cp);
1195 #ifdef RXDEBUG_PACKET
1197 #endif /* RXDEBUG_PACKET */
1198 cp = call->currentPacket = (struct rx_packet *)0;
1200 #ifdef RXDEBUG_PACKET
1202 #endif /* RXDEBUG_PACKET */
1203 rxi_FreePackets(0, &tmpq);
1206 nbytes -= iov[nextio].iov_len;
1207 call->curpos += iov[nextio].iov_len;
1208 call->curlen -= iov[nextio].iov_len;
1209 call->nFree -= iov[nextio].iov_len;
1211 if (call->curlen == 0) {
1212 if (++call->curvec > cp->niovecs) {
1215 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1216 call->curlen = cp->wirevec[call->curvec].iov_len;
1220 } while (nbytes && nextio < nio);
1222 /* Move the packets from the temporary queue onto the transmit queue.
1223 * We may end up with more than call->twind packets on the queue. */
1225 for (queue_Scan(&tmpq, p, np, rx_packet))
1227 p->flags |= RX_PKTFLAG_TQ;
1229 queue_SpliceAppend(&call->tq, &tmpq);
1231 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1232 rxi_Start(0, call, 0, 0);
1235 /* Wait for the length of the transmit queue to fall below call->twind */
1236 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1238 call->startWait = clock_Sec();
1239 #ifdef RX_ENABLE_LOCKS
1240 CV_WAIT(&call->cv_twind, &call->lock);
1242 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1243 osi_rxSleep(&call->twind);
1245 call->startWait = 0;
1247 /* cp is no longer valid since we may have given up the lock */
1248 cp = call->currentPacket;
1252 cp->flags &= ~RX_PKTFLAG_CP;
1254 cp = call->currentPacket = (struct rx_packet *)0;
1259 return requestCount - nbytes;
1263 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1269 MUTEX_ENTER(&call->lock);
1270 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1271 MUTEX_EXIT(&call->lock);
1276 /* Flush any buffered data to the stream, switch to read mode
1277 * (clients) or to EOF mode (servers) */
1279 rxi_FlushWrite(struct rx_call *call)
1281 struct rx_packet *cp = NULL;
1283 /* Free any packets from the last call to ReadvProc/WritevProc */
1284 if (queue_IsNotEmpty(&call->iovq)) {
1285 #ifdef RXDEBUG_PACKET
1287 #endif /* RXDEBUG_PACKET */
1288 rxi_FreePackets(0, &call->iovq);
1291 if (call->mode == RX_MODE_SENDING) {
1294 (call->conn->type ==
1295 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1297 #ifdef RX_KERNEL_TRACE
1299 int glockOwner = ISAFS_GLOCK();
1302 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1303 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1310 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1311 /* Wait until TQ_BUSY is reset before adding any
1312 * packets to the transmit queue
1314 while (call->flags & RX_CALL_TQ_BUSY) {
1315 call->flags |= RX_CALL_TQ_WAIT;
1317 #ifdef RX_ENABLE_LOCKS
1318 CV_WAIT(&call->cv_tq, &call->lock);
1319 #else /* RX_ENABLE_LOCKS */
1320 osi_rxSleep(&call->tq);
1321 #endif /* RX_ENABLE_LOCKS */
1323 if (call->tqWaiters == 0)
1324 call->flags &= ~RX_CALL_TQ_WAIT;
1326 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1328 /* cp is no longer valid since we may have given up the lock */
1329 cp = call->currentPacket;
1332 /* cp->length is only supposed to be the user's data */
1333 /* cp->length was already set to (then-current)
1334 * MaxUserDataSize or less. */
1335 cp->flags &= ~RX_PKTFLAG_CP;
1336 cp->length -= call->nFree;
1337 call->currentPacket = (struct rx_packet *)0;
1340 cp = rxi_AllocSendPacket(call, 0);
1342 /* Mode can no longer be MODE_SENDING */
1346 cp->niovecs = 2; /* header + space for rxkad stuff */
1350 /* The 1 specifies that this is the last packet */
1351 hadd32(call->bytesSent, cp->length);
1352 rxi_PrepareSendPacket(call, cp, 1);
1353 cp->flags |= RX_PKTFLAG_TQ;
1354 queue_Append(&call->tq, cp);
1355 #ifdef RXDEBUG_PACKET
1357 #endif /* RXDEBUG_PACKET */
1360 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1361 rxi_Start(0, call, 0, 0);
1366 /* Flush any buffered data to the stream, switch to read mode
1367 * (clients) or to EOF mode (servers) */
1369 rx_FlushWrite(struct rx_call *call)
1373 MUTEX_ENTER(&call->lock);
1374 rxi_FlushWrite(call);
1375 MUTEX_EXIT(&call->lock);