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>
22 #ifdef RX_KERNEL_TRACE
23 #include "rx_kcommon.h"
25 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
26 #include "afs/sysincludes.h"
32 #include <net/net_globals.h>
33 #endif /* AFS_OSF_ENV */
34 #ifdef AFS_LINUX20_ENV
37 #include "netinet/in.h"
38 #if defined(AFS_SGI_ENV)
39 #include "afs/sysincludes.h"
42 #include "afs/afs_args.h"
43 #include "afs/afs_osi.h"
44 #if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
48 #include "afs/sysincludes.h"
51 #undef RXDEBUG /* turn off debugging */
54 #include "rx_kmutex.h"
55 #include "rx/rx_kernel.h"
56 #include "rx/rx_clock.h"
57 #include "rx/rx_queue.h"
59 #include "rx/rx_globals.h"
68 #endif /* AFS_ALPHA_ENV */
70 # include <sys/types.h>
72 # include <sys/socket.h>
73 # include <sys/file.h>
75 # include <netinet/in.h>
76 # include <sys/stat.h>
77 # include <sys/time.h>
90 # include "rx_clock.h"
91 # include "rx_queue.h"
93 # include "rx_globals.h"
97 /* rxdb_fileID is used to identify the lock location, along with line#. */
98 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
99 #endif /* RX_LOCKS_DB */
100 /* rxi_ReadProc -- internal version.
102 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
105 rxi_ReadProc(register struct rx_call *call, register char *buf,
108 register struct rx_packet *cp = call->currentPacket;
109 register struct rx_packet *rp;
110 register struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
111 register int requestCount;
112 register unsigned int t;
113 /* XXXX took out clock_NewTime from here. Was it needed? */
114 requestCount = nbytes;
116 /* Free any packets from the last call to ReadvProc/WritevProc */
117 if (!queue_IsEmpty(&call->iovq)) {
118 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
125 if (call->nLeft == 0) {
126 /* Get next packet */
128 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
132 if (call->mode == RX_MODE_SENDING) {
133 rxi_FlushWrite(call);
137 if (queue_IsNotEmpty(&call->rq)) {
138 /* Check that next packet available is next in sequence */
139 rp = queue_First(&call->rq, rx_packet);
140 if (rp->header.seq == call->rnext) {
142 register struct rx_connection *conn = call->conn;
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->curvec = 1; /* 0th vec is always header */
169 /* begin at the beginning [ more or less ], continue
170 * on until the end, then stop. */
172 (char *)cp->wirevec[1].iov_base +
173 call->conn->securityHeaderSize;
175 cp->wirevec[1].iov_len -
176 call->conn->securityHeaderSize;
178 /* Notice that this code works correctly if the data
179 * size is 0 (which it may be--no reply arguments from
180 * server, for example). This relies heavily on the
181 * fact that the code below immediately frees the packet
182 * (no yields, etc.). If it didn't, this would be a
183 * problem because a value of zero for call->nLeft
184 * normally means that there is no read packet */
185 call->nLeft = cp->length;
186 hadd32(call->bytesRcvd, cp->length);
188 /* Send a hard ack for every rxi_HardAckRate+1 packets
189 * consumed. Otherwise schedule an event to send
190 * the hard ack later on.
193 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
194 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
195 rxevent_Cancel(call->delayedAckEvent, call,
196 RX_CALL_REFCOUNT_DELAY);
197 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
200 clock_GetTime(&when);
201 /* Delay to consolidate ack packets */
202 clock_Add(&when, &rx_hardAckDelay);
203 if (!call->delayedAckEvent
204 || clock_Gt(&call->delayedAckEvent->
206 rxevent_Cancel(call->delayedAckEvent,
208 RX_CALL_REFCOUNT_DELAY);
209 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
210 call->delayedAckEvent =
212 rxi_SendDelayedAck, call,
222 MTUXXX doesn't there need to be an "else" here ???
224 /* Are there ever going to be any more packets? */
225 if (call->flags & RX_CALL_RECEIVE_DONE) {
226 return requestCount - nbytes;
228 /* Wait for in-sequence packet */
229 call->flags |= RX_CALL_READER_WAIT;
231 call->startWait = clock_Sec();
232 while (call->flags & RX_CALL_READER_WAIT) {
233 #ifdef RX_ENABLE_LOCKS
234 CV_WAIT(&call->cv_rq, &call->lock);
236 osi_rxSleep(&call->rq);
241 #ifdef RX_ENABLE_LOCKS
245 #endif /* RX_ENABLE_LOCKS */
249 /* MTUXXX this should be replaced by some error-recovery code before shipping */
250 /* yes, the following block is allowed to be the ELSE clause (or not) */
251 /* It's possible for call->nLeft to be smaller than any particular
252 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
253 * reflects the size of the buffer. We have to keep track of the
254 * number of bytes read in the length field of the packet struct. On
255 * the final portion of a received packet, it's almost certain that
256 * call->nLeft will be smaller than the final buffer. */
257 while (nbytes && cp) {
258 t = MIN((int)call->curlen, nbytes);
259 t = MIN(t, (int)call->nLeft);
260 memcpy(buf, call->curpos, t);
268 /* out of packet. Get another one. */
270 cp = call->currentPacket = (struct rx_packet *)0;
271 } else if (!call->curlen) {
272 /* need to get another struct iov */
273 if (++call->curvec >= cp->niovecs) {
274 /* current packet is exhausted, get ready for another */
275 /* don't worry about curvec and stuff, they get set somewhere else */
277 cp = call->currentPacket = (struct rx_packet *)0;
281 (char *)cp->wirevec[call->curvec].iov_base;
282 call->curlen = cp->wirevec[call->curvec].iov_len;
287 /* user buffer is full, return */
297 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
306 * Free any packets from the last call to ReadvProc/WritevProc.
307 * We do not need the lock because the receiver threads only
308 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
309 * RX_CALL_IOVEC_WAIT is always cleared before returning from
310 * ReadvProc/WritevProc.
312 if (!queue_IsEmpty(&call->iovq)) {
313 register struct rx_packet *rp;
314 register struct rx_packet *nxp;
315 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
322 * Most common case, all of the data is in the current iovec.
323 * We do not need the lock because this is the only thread that
324 * updates the curlen, curpos, nLeft fields.
326 * We are relying on nLeft being zero unless the call is in receive mode.
328 tcurlen = call->curlen;
329 tnLeft = call->nLeft;
330 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
331 tcurpos = call->curpos;
332 memcpy(buf, tcurpos, nbytes);
333 call->curpos = tcurpos + nbytes;
334 call->curlen = tcurlen - nbytes;
335 call->nLeft = tnLeft - nbytes;
341 MUTEX_ENTER(&call->lock);
342 bytes = rxi_ReadProc(call, buf, nbytes);
343 MUTEX_EXIT(&call->lock);
349 /* Optimization for unmarshalling 32 bit integers */
351 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
360 * Free any packets from the last call to ReadvProc/WritevProc.
361 * We do not need the lock because the receiver threads only
362 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
363 * RX_CALL_IOVEC_WAIT is always cleared before returning from
364 * ReadvProc/WritevProc.
366 if (!queue_IsEmpty(&call->iovq)) {
367 register struct rx_packet *rp;
368 register struct rx_packet *nxp;
369 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
376 * Most common case, all of the data is in the current iovec.
377 * We do not need the lock because this is the only thread that
378 * updates the curlen, curpos, nLeft fields.
380 * We are relying on nLeft being zero unless the call is in receive mode.
382 tcurlen = call->curlen;
383 tnLeft = call->nLeft;
384 if (!call->error && tcurlen > sizeof(afs_int32)
385 && tnLeft > sizeof(afs_int32)) {
386 tcurpos = call->curpos;
387 if (!((long)tcurpos & (sizeof(afs_int32) - 1))) {
388 *value = *((afs_int32 *) (tcurpos));
390 memcpy((char *)value, tcurpos, sizeof(afs_int32));
392 call->curpos = tcurpos + sizeof(afs_int32);
393 call->curlen = tcurlen - sizeof(afs_int32);
394 call->nLeft = tnLeft - sizeof(afs_int32);
395 return sizeof(afs_int32);
400 MUTEX_ENTER(&call->lock);
401 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
402 MUTEX_EXIT(&call->lock);
410 * Uses packets in the receive queue to fill in as much of the
411 * current iovec as possible. Does not block if it runs out
412 * of packets to complete the iovec. Return true if an ack packet
413 * was sent, otherwise return false */
415 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
419 register unsigned int t;
420 struct rx_packet *rp;
421 struct rx_packet *curp;
422 struct iovec *call_iov;
423 struct iovec *cur_iov = NULL;
425 curp = call->currentPacket;
427 cur_iov = &curp->wirevec[call->curvec];
429 call_iov = &call->iov[call->iovNext];
431 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
432 if (call->nLeft == 0) {
433 /* Get next packet */
434 if (queue_IsNotEmpty(&call->rq)) {
435 /* Check that next packet available is next in sequence */
436 rp = queue_First(&call->rq, rx_packet);
437 if (rp->header.seq == call->rnext) {
439 register struct rx_connection *conn = call->conn;
442 /* RXS_CheckPacket called to undo RXS_PreparePacket's
443 * work. It may reduce the length of the packet by up
444 * to conn->maxTrailerSize, to reflect the length of the
445 * data + the header. */
447 RXS_CheckPacket(conn->securityObject, call, rp))) {
448 /* Used to merely shut down the call, but now we
449 * shut down the whole connection since this may
450 * indicate an attempt to hijack it */
452 MUTEX_EXIT(&call->lock);
453 rxi_ConnectionError(conn, error);
454 MUTEX_ENTER(&conn->conn_data_lock);
455 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
456 MUTEX_EXIT(&conn->conn_data_lock);
458 MUTEX_ENTER(&call->lock);
463 curp = call->currentPacket = rp;
464 call->curvec = 1; /* 0th vec is always header */
465 cur_iov = &curp->wirevec[1];
466 /* begin at the beginning [ more or less ], continue
467 * on until the end, then stop. */
469 (char *)curp->wirevec[1].iov_base +
470 call->conn->securityHeaderSize;
472 curp->wirevec[1].iov_len -
473 call->conn->securityHeaderSize;
475 /* Notice that this code works correctly if the data
476 * size is 0 (which it may be--no reply arguments from
477 * server, for example). This relies heavily on the
478 * fact that the code below immediately frees the packet
479 * (no yields, etc.). If it didn't, this would be a
480 * problem because a value of zero for call->nLeft
481 * normally means that there is no read packet */
482 call->nLeft = curp->length;
483 hadd32(call->bytesRcvd, curp->length);
485 /* Send a hard ack for every rxi_HardAckRate+1 packets
486 * consumed. Otherwise schedule an event to send
487 * the hard ack later on.
497 /* It's possible for call->nLeft to be smaller than any particular
498 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
499 * reflects the size of the buffer. We have to keep track of the
500 * number of bytes read in the length field of the packet struct. On
501 * the final portion of a received packet, it's almost certain that
502 * call->nLeft will be smaller than the final buffer. */
503 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
505 t = MIN((int)call->curlen, call->iovNBytes);
506 t = MIN(t, (int)call->nLeft);
507 call_iov->iov_base = call->curpos;
508 call_iov->iov_len = t;
511 call->iovNBytes -= t;
517 /* out of packet. Get another one. */
518 queue_Append(&call->iovq, curp);
519 curp = call->currentPacket = (struct rx_packet *)0;
520 } else if (!call->curlen) {
521 /* need to get another struct iov */
522 if (++call->curvec >= curp->niovecs) {
523 /* current packet is exhausted, get ready for another */
524 /* don't worry about curvec and stuff, they get set somewhere else */
525 queue_Append(&call->iovq, curp);
526 curp = call->currentPacket = (struct rx_packet *)0;
530 call->curpos = (char *)cur_iov->iov_base;
531 call->curlen = cur_iov->iov_len;
537 /* If we consumed any packets then check whether we need to
538 * send a hard ack. */
539 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
540 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
541 rxevent_Cancel(call->delayedAckEvent, call,
542 RX_CALL_REFCOUNT_DELAY);
543 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
547 clock_GetTime(&when);
548 /* Delay to consolidate ack packets */
549 clock_Add(&when, &rx_hardAckDelay);
550 if (!call->delayedAckEvent
551 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
552 rxevent_Cancel(call->delayedAckEvent, call,
553 RX_CALL_REFCOUNT_DELAY);
554 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
555 call->delayedAckEvent =
556 rxevent_Post(&when, rxi_SendDelayedAck, call, 0);
564 /* rxi_ReadvProc -- internal version.
566 * Fills in an iovec with pointers to the packet buffers. All packets
567 * except the last packet (new current packet) are moved to the iovq
568 * while the application is processing the data.
570 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
573 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
576 struct rx_packet *rp;
577 struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
581 requestCount = nbytes;
584 /* Free any packets from the last call to ReadvProc/WritevProc */
585 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
590 if (call->mode == RX_MODE_SENDING) {
591 rxi_FlushWrite(call);
598 /* Get whatever data is currently available in the receive queue.
599 * If rxi_FillReadVec sends an ack packet then it is possible
600 * that we will receive more data while we drop the call lock
601 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
602 * here to avoid a race with the receive thread if we send
603 * hard acks in rxi_FillReadVec. */
604 call->flags |= RX_CALL_IOVEC_WAIT;
605 call->iovNBytes = nbytes;
606 call->iovMax = maxio;
609 rxi_FillReadVec(call, 0);
611 /* if we need more data then sleep until the receive thread has
612 * filled in the rest. */
613 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
614 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
615 call->flags |= RX_CALL_READER_WAIT;
617 call->startWait = clock_Sec();
618 while (call->flags & RX_CALL_READER_WAIT) {
619 #ifdef RX_ENABLE_LOCKS
620 CV_WAIT(&call->cv_rq, &call->lock);
622 osi_rxSleep(&call->rq);
627 call->flags &= ~RX_CALL_IOVEC_WAIT;
628 #ifdef RX_ENABLE_LOCKS
632 #endif /* RX_ENABLE_LOCKS */
635 *nio = call->iovNext;
636 return nbytes - call->iovNBytes;
640 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
648 MUTEX_ENTER(&call->lock);
649 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
650 MUTEX_EXIT(&call->lock);
656 /* rxi_WriteProc -- internal version.
658 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
661 rxi_WriteProc(register struct rx_call *call, register char *buf,
664 struct rx_connection *conn = call->conn;
665 register struct rx_packet *cp = call->currentPacket;
666 register struct rx_packet *tp; /* Temporary packet pointer */
667 register struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
668 register unsigned int t;
669 int requestCount = nbytes;
671 /* Free any packets from the last call to ReadvProc/WritevProc */
672 if (!queue_IsEmpty(&call->iovq)) {
673 for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
679 if (call->mode != RX_MODE_SENDING) {
680 if ((conn->type == RX_SERVER_CONNECTION)
681 && (call->mode == RX_MODE_RECEIVING)) {
682 call->mode = RX_MODE_SENDING;
685 cp = call->currentPacket = (struct rx_packet *)0;
694 /* Loop condition is checked at end, so that a write of 0 bytes
695 * will force a packet to be created--specially for the case where
696 * there are 0 bytes on the stream, but we must send a packet
699 if (call->nFree == 0) {
700 if (!call->error && cp) {
701 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
702 /* Wait until TQ_BUSY is reset before adding any
703 * packets to the transmit queue
705 while (call->flags & RX_CALL_TQ_BUSY) {
706 call->flags |= RX_CALL_TQ_WAIT;
707 #ifdef RX_ENABLE_LOCKS
708 CV_WAIT(&call->cv_tq, &call->lock);
709 #else /* RX_ENABLE_LOCKS */
710 osi_rxSleep(&call->tq);
711 #endif /* RX_ENABLE_LOCKS */
713 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
714 clock_NewTime(); /* Bogus: need new time package */
715 /* The 0, below, specifies that it is not the last packet:
716 * there will be others. PrepareSendPacket may
717 * alter the packet length by up to
718 * conn->securityMaxTrailerSize */
719 hadd32(call->bytesSent, cp->length);
720 rxi_PrepareSendPacket(call, cp, 0);
721 queue_Append(&call->tq, cp);
722 cp = call->currentPacket = NULL;
725 flags & (RX_CALL_FAST_RECOVER |
726 RX_CALL_FAST_RECOVER_WAIT))) {
727 rxi_Start(0, call, 0, 0);
730 /* Wait for transmit window to open up */
732 && call->tnext + 1 > call->tfirst + call->twind) {
734 call->startWait = clock_Sec();
736 #ifdef RX_ENABLE_LOCKS
737 CV_WAIT(&call->cv_twind, &call->lock);
739 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
740 osi_rxSleep(&call->twind);
744 #ifdef RX_ENABLE_LOCKS
748 #endif /* RX_ENABLE_LOCKS */
750 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
751 call->currentPacket = cp;
752 call->nFree = cp->length;
753 call->curvec = 1; /* 0th vec is always header */
754 /* begin at the beginning [ more or less ], continue
755 * on until the end, then stop. */
757 (char *)cp->wirevec[1].iov_base +
758 call->conn->securityHeaderSize;
760 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
765 call->currentPacket = NULL;
771 if (cp && (int)call->nFree < nbytes) {
772 /* Try to extend the current buffer */
773 register int len, mud;
775 mud = rx_MaxUserDataSize(call);
778 want = MIN(nbytes - (int)call->nFree, mud - len);
779 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
780 if (cp->length > (unsigned)mud)
782 call->nFree += (cp->length - len);
786 /* If the remaining bytes fit in the buffer, then store them
787 * and return. Don't ship a buffer that's full immediately to
788 * the peer--we don't know if it's the last buffer yet */
794 while (nbytes && call->nFree) {
796 t = MIN((int)call->curlen, nbytes);
797 t = MIN((int)call->nFree, t);
798 memcpy(call->curpos, buf, t);
806 /* need to get another struct iov */
807 if (++call->curvec >= cp->niovecs) {
808 /* current packet is full, extend or send it */
811 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
812 call->curlen = cp->wirevec[call->curvec].iov_len;
815 } /* while bytes to send and room to send them */
817 /* might be out of space now */
820 } else; /* more data to send, so get another packet and keep going */
823 return requestCount - nbytes;
827 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
836 * Free any packets from the last call to ReadvProc/WritevProc.
837 * We do not need the lock because the receiver threads only
838 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
839 * RX_CALL_IOVEC_WAIT is always cleared before returning from
840 * ReadvProc/WritevProc.
842 if (!queue_IsEmpty(&call->iovq)) {
843 register struct rx_packet *rp;
844 register struct rx_packet *nxp;
845 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
852 * Most common case: all of the data fits in the current iovec.
853 * We do not need the lock because this is the only thread that
854 * updates the curlen, curpos, nFree fields.
856 * We are relying on nFree being zero unless the call is in send mode.
858 tcurlen = (int)call->curlen;
859 tnFree = (int)call->nFree;
860 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
861 tcurpos = call->curpos;
862 memcpy(tcurpos, buf, nbytes);
863 call->curpos = tcurpos + nbytes;
864 call->curlen = tcurlen - nbytes;
865 call->nFree = tnFree - nbytes;
871 MUTEX_ENTER(&call->lock);
872 bytes = rxi_WriteProc(call, buf, nbytes);
873 MUTEX_EXIT(&call->lock);
879 /* Optimization for marshalling 32 bit arguments */
881 rx_WriteProc32(register struct rx_call *call, register afs_int32 * value)
890 * Free any packets from the last call to ReadvProc/WritevProc.
891 * We do not need the lock because the receiver threads only
892 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
893 * RX_CALL_IOVEC_WAIT is always cleared before returning from
894 * ReadvProc/WritevProc.
896 if (!queue_IsEmpty(&call->iovq)) {
897 register struct rx_packet *rp;
898 register struct rx_packet *nxp;
899 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
906 * Most common case: all of the data fits in the current iovec.
907 * We do not need the lock because this is the only thread that
908 * updates the curlen, curpos, nFree fields.
910 * We are relying on nFree being zero unless the call is in send mode.
912 tcurlen = (int)call->curlen;
913 tnFree = (int)call->nFree;
914 if (!call->error && tcurlen >= sizeof(afs_int32)
915 && tnFree >= sizeof(afs_int32)) {
916 tcurpos = call->curpos;
917 if (!((long)tcurpos & (sizeof(afs_int32) - 1))) {
918 *((afs_int32 *) (tcurpos)) = *value;
920 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
922 call->curpos = tcurpos + sizeof(afs_int32);
923 call->curlen = tcurlen - sizeof(afs_int32);
924 call->nFree = tnFree - sizeof(afs_int32);
925 return sizeof(afs_int32);
930 MUTEX_ENTER(&call->lock);
931 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
932 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;
951 struct rx_packet *tp; /* temporary packet pointer */
952 struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
955 /* Temporary values, real work is done in rxi_WritevProc */
961 requestCount = nbytes;
964 /* Free any packets from the last call to ReadvProc/WritevProc */
965 for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
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;
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;
991 register unsigned int t;
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 queue_Append(&call->iovq, cp);
1002 tnFree = cp->length;
1005 (char *)cp->wirevec[1].iov_base +
1006 call->conn->securityHeaderSize;
1007 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1010 if (tnFree < nbytes) {
1011 /* try to extend the current packet */
1012 register int len, mud;
1014 mud = rx_MaxUserDataSize(call);
1017 want = MIN(nbytes - tnFree, mud - len);
1018 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1019 if (cp->length > (unsigned)mud)
1021 tnFree += (cp->length - len);
1022 if (cp == call->currentPacket) {
1023 call->nFree += (cp->length - len);
1028 /* fill in the next entry in the iovec */
1029 t = MIN(tcurlen, nbytes);
1031 iov[nextio].iov_base = tcurpos;
1032 iov[nextio].iov_len = t;
1040 /* need to get another struct iov */
1041 if (++tcurvec >= cp->niovecs) {
1042 /* current packet is full, extend it or move on to next packet */
1045 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1046 tcurlen = cp->wirevec[tcurvec].iov_len;
1049 } while (nbytes && nextio < maxio);
1051 return requestCount - nbytes;
1055 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1063 MUTEX_ENTER(&call->lock);
1064 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1065 MUTEX_EXIT(&call->lock);
1071 /* rxi_WritevProc -- internal version.
1073 * Send buffers allocated in rxi_WritevAlloc.
1075 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1078 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1080 struct rx_packet *cp = call->currentPacket;
1081 register struct rx_packet *tp; /* Temporary packet pointer */
1082 register struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
1085 struct rx_queue tmpq;
1087 requestCount = nbytes;
1090 if (call->mode != RX_MODE_SENDING) {
1091 call->error = RX_PROTOCOL_ERROR;
1093 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1094 /* Wait until TQ_BUSY is reset before trying to move any
1095 * packets to the transmit queue. */
1096 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1097 call->flags |= RX_CALL_TQ_WAIT;
1098 #ifdef RX_ENABLE_LOCKS
1099 CV_WAIT(&call->cv_tq, &call->lock);
1100 #else /* RX_ENABLE_LOCKS */
1101 osi_rxSleep(&call->tq);
1102 #endif /* RX_ENABLE_LOCKS */
1104 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1107 for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
1113 cp = call->currentPacket = NULL;
1118 /* Loop through the I/O vector adjusting packet pointers.
1119 * Place full packets back onto the iovq once they are ready
1120 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1121 * the iovec. We put the loop condition at the end to ensure that
1122 * a zero length write will push a short packet. */
1126 if (call->nFree == 0 && cp) {
1127 clock_NewTime(); /* Bogus: need new time package */
1128 /* The 0, below, specifies that it is not the last packet:
1129 * there will be others. PrepareSendPacket may
1130 * alter the packet length by up to
1131 * conn->securityMaxTrailerSize */
1132 hadd32(call->bytesSent, cp->length);
1133 rxi_PrepareSendPacket(call, cp, 0);
1134 queue_Append(&tmpq, cp);
1136 /* The head of the iovq is now the current packet */
1138 if (queue_IsEmpty(&call->iovq)) {
1139 call->error = RX_PROTOCOL_ERROR;
1140 cp = call->currentPacket = NULL;
1141 for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1147 cp = queue_First(&call->iovq, rx_packet);
1149 call->currentPacket = cp;
1150 call->nFree = cp->length;
1153 (char *)cp->wirevec[1].iov_base +
1154 call->conn->securityHeaderSize;
1156 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1161 /* The next iovec should point to the current position */
1162 if (iov[nextio].iov_base != call->curpos
1163 || iov[nextio].iov_len > (int)call->curlen) {
1164 call->error = RX_PROTOCOL_ERROR;
1165 for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1171 call->currentPacket = NULL;
1175 nbytes -= iov[nextio].iov_len;
1176 call->curpos += iov[nextio].iov_len;
1177 call->curlen -= iov[nextio].iov_len;
1178 call->nFree -= iov[nextio].iov_len;
1180 if (call->curlen == 0) {
1181 if (++call->curvec > cp->niovecs) {
1184 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1185 call->curlen = cp->wirevec[call->curvec].iov_len;
1189 } while (nbytes && nextio < nio);
1191 /* Move the packets from the temporary queue onto the transmit queue.
1192 * We may end up with more than call->twind packets on the queue. */
1193 for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1195 queue_Append(&call->tq, tp);
1198 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1199 rxi_Start(0, call, 0, 0);
1202 /* Wait for the length of the transmit queue to fall below call->twind */
1203 while (!call->error && call->tnext + 1 > call->tfirst + call->twind) {
1205 call->startWait = clock_Sec();
1206 #ifdef RX_ENABLE_LOCKS
1207 CV_WAIT(&call->cv_twind, &call->lock);
1209 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1210 osi_rxSleep(&call->twind);
1212 call->startWait = 0;
1218 cp = call->currentPacket = NULL;
1223 return requestCount - nbytes;
1227 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1234 MUTEX_ENTER(&call->lock);
1235 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1236 MUTEX_EXIT(&call->lock);
1242 /* Flush any buffered data to the stream, switch to read mode
1243 * (clients) or to EOF mode (servers) */
1245 rxi_FlushWrite(register struct rx_call *call)
1247 register struct rx_packet *cp = call->currentPacket;
1248 register struct rx_packet *tp; /* Temporary packet pointer */
1249 register struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
1251 /* Free any packets from the last call to ReadvProc/WritevProc */
1252 for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
1257 if (call->mode == RX_MODE_SENDING) {
1260 (call->conn->type ==
1261 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1263 #ifdef RX_KERNEL_TRACE
1265 int glockOwner = ISAFS_GLOCK();
1268 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1269 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1276 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1277 /* Wait until TQ_BUSY is reset before adding any
1278 * packets to the transmit queue
1280 while (call->flags & RX_CALL_TQ_BUSY) {
1281 call->flags |= RX_CALL_TQ_WAIT;
1282 #ifdef RX_ENABLE_LOCKS
1283 CV_WAIT(&call->cv_tq, &call->lock);
1284 #else /* RX_ENABLE_LOCKS */
1285 osi_rxSleep(&call->tq);
1286 #endif /* RX_ENABLE_LOCKS */
1288 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1291 /* cp->length is only supposed to be the user's data */
1292 /* cp->length was already set to (then-current)
1293 * MaxUserDataSize or less. */
1294 cp->length -= call->nFree;
1295 call->currentPacket = (struct rx_packet *)0;
1298 cp = rxi_AllocSendPacket(call, 0);
1300 /* Mode can no longer be MODE_SENDING */
1304 cp->niovecs = 2; /* header + space for rxkad stuff */
1308 /* The 1 specifies that this is the last packet */
1309 hadd32(call->bytesSent, cp->length);
1310 rxi_PrepareSendPacket(call, cp, 1);
1311 queue_Append(&call->tq, cp);
1314 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1315 rxi_Start(0, call, 0, 0);
1320 /* Flush any buffered data to the stream, switch to read mode
1321 * (clients) or to EOF mode (servers) */
1323 rx_FlushWrite(struct rx_call *call)
1328 MUTEX_ENTER(&call->lock);
1329 rxi_FlushWrite(call);
1330 MUTEX_EXIT(&call->lock);