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"
31 #if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV)
35 #include <net/net_globals.h>
36 #endif /* AFS_OSF_ENV */
37 #ifdef AFS_LINUX20_ENV
40 #include "netinet/in.h"
41 #if defined(AFS_SGI_ENV)
42 #include "afs/sysincludes.h"
45 #include "afs/afs_args.h"
46 #include "afs/afs_osi.h"
47 #if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
51 #include "afs/sysincludes.h"
54 #undef RXDEBUG /* turn off debugging */
57 #include "rx_kmutex.h"
58 #include "rx/rx_kernel.h"
59 #include "rx/rx_clock.h"
60 #include "rx/rx_queue.h"
62 #include "rx/rx_globals.h"
71 #endif /* AFS_OSF_ENV */
73 # include <sys/types.h>
75 # include <winsock2.h>
76 #else /* !AFS_NT40_ENV */
77 # include <sys/socket.h>
78 # include <sys/file.h>
80 # include <netinet/in.h>
81 # include <sys/stat.h>
82 # include <sys/time.h>
83 #endif /* !AFS_NT40_ENV */
89 # include "rx_clock.h"
90 # include "rx_queue.h"
92 # include "rx_globals.h"
96 /* rxdb_fileID is used to identify the lock location, along with line#. */
97 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
98 #endif /* RX_LOCKS_DB */
99 /* rxi_ReadProc -- internal version.
101 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
104 rxi_ReadProc(register struct rx_call *call, register char *buf,
107 register struct rx_packet *cp = call->currentPacket;
108 register struct rx_packet *rp;
109 register int requestCount;
110 register unsigned int t;
112 /* XXXX took out clock_NewTime from here. Was it needed? */
113 requestCount = nbytes;
115 /* Free any packets from the last call to ReadvProc/WritevProc */
116 if (queue_IsNotEmpty(&call->iovq)) {
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 register struct rx_connection *conn = call->conn;
141 /* RXS_CheckPacket called to undo RXS_PreparePacket's
142 * work. It may reduce the length of the packet by up
143 * to conn->maxTrailerSize, to reflect the length of the
144 * data + the header. */
146 RXS_CheckPacket(conn->securityObject, call,
148 /* Used to merely shut down the call, but now we
149 * shut down the whole connection since this may
150 * indicate an attempt to hijack it */
152 MUTEX_EXIT(&call->lock);
153 rxi_ConnectionError(conn, error);
154 MUTEX_ENTER(&conn->conn_data_lock);
155 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
156 MUTEX_EXIT(&conn->conn_data_lock);
158 MUTEX_ENTER(&call->lock);
163 cp = call->currentPacket = rp;
164 call->curvec = 1; /* 0th vec is always header */
165 /* begin at the beginning [ more or less ], continue
166 * on until the end, then stop. */
168 (char *)cp->wirevec[1].iov_base +
169 call->conn->securityHeaderSize;
171 cp->wirevec[1].iov_len -
172 call->conn->securityHeaderSize;
174 /* Notice that this code works correctly if the data
175 * size is 0 (which it may be--no reply arguments from
176 * server, for example). This relies heavily on the
177 * fact that the code below immediately frees the packet
178 * (no yields, etc.). If it didn't, this would be a
179 * problem because a value of zero for call->nLeft
180 * normally means that there is no read packet */
181 call->nLeft = cp->length;
182 hadd32(call->bytesRcvd, cp->length);
184 /* Send a hard ack for every rxi_HardAckRate+1 packets
185 * consumed. Otherwise schedule an event to send
186 * the hard ack later on.
189 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
190 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
191 rxevent_Cancel(call->delayedAckEvent, call,
192 RX_CALL_REFCOUNT_DELAY);
193 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
195 struct clock when, now;
198 /* Delay to consolidate ack packets */
199 clock_Add(&when, &rx_hardAckDelay);
200 if (!call->delayedAckEvent
201 || clock_Gt(&call->delayedAckEvent->
203 rxevent_Cancel(call->delayedAckEvent,
205 RX_CALL_REFCOUNT_DELAY);
206 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
207 call->delayedAckEvent =
208 rxevent_PostNow(&when, &now,
209 rxi_SendDelayedAck, call,
219 MTUXXX doesn't there need to be an "else" here ???
221 /* Are there ever going to be any more packets? */
222 if (call->flags & RX_CALL_RECEIVE_DONE) {
223 return requestCount - nbytes;
225 /* Wait for in-sequence packet */
226 call->flags |= RX_CALL_READER_WAIT;
228 call->startWait = clock_Sec();
229 while (call->flags & RX_CALL_READER_WAIT) {
230 #ifdef RX_ENABLE_LOCKS
231 CV_WAIT(&call->cv_rq, &call->lock);
233 osi_rxSleep(&call->rq);
238 #ifdef RX_ENABLE_LOCKS
242 #endif /* RX_ENABLE_LOCKS */
246 /* MTUXXX this should be replaced by some error-recovery code before shipping */
247 /* yes, the following block is allowed to be the ELSE clause (or not) */
248 /* It's possible for call->nLeft to be smaller than any particular
249 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
250 * reflects the size of the buffer. We have to keep track of the
251 * number of bytes read in the length field of the packet struct. On
252 * the final portion of a received packet, it's almost certain that
253 * call->nLeft will be smaller than the final buffer. */
254 while (nbytes && cp) {
255 t = MIN((int)call->curlen, nbytes);
256 t = MIN(t, (int)call->nLeft);
257 memcpy(buf, call->curpos, t);
265 /* out of packet. Get another one. */
267 cp = call->currentPacket = (struct rx_packet *)0;
268 } else if (!call->curlen) {
269 /* need to get another struct iov */
270 if (++call->curvec >= cp->niovecs) {
271 /* current packet is exhausted, get ready for another */
272 /* don't worry about curvec and stuff, they get set somewhere else */
274 cp = call->currentPacket = (struct rx_packet *)0;
278 (char *)cp->wirevec[call->curvec].iov_base;
279 call->curlen = cp->wirevec[call->curvec].iov_len;
284 /* user buffer is full, return */
294 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
303 * Free any packets from the last call to ReadvProc/WritevProc.
304 * We do not need the lock because the receiver threads only
305 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
306 * RX_CALL_IOVEC_WAIT is always cleared before returning from
307 * ReadvProc/WritevProc.
309 if (!queue_IsEmpty(&call->iovq)) {
310 rxi_FreePackets(0, &call->iovq);
314 * Most common case, all of the data is in the current iovec.
315 * We do not need the lock because this is the only thread that
316 * updates the curlen, curpos, nLeft fields.
318 * We are relying on nLeft being zero unless the call is in receive mode.
320 tcurlen = call->curlen;
321 tnLeft = call->nLeft;
322 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
323 tcurpos = call->curpos;
324 memcpy(buf, tcurpos, nbytes);
325 call->curpos = tcurpos + nbytes;
326 call->curlen = tcurlen - nbytes;
327 call->nLeft = tnLeft - nbytes;
330 /* out of packet. Get another one. */
332 MUTEX_ENTER(&call->lock);
333 rxi_FreePacket(call->currentPacket);
334 call->currentPacket = (struct rx_packet *)0;
335 MUTEX_EXIT(&call->lock);
342 MUTEX_ENTER(&call->lock);
343 bytes = rxi_ReadProc(call, buf, nbytes);
344 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 rxi_FreePackets(0, &call->iovq);
371 * Most common case, all of the data is in the current iovec.
372 * We do not need the lock because this is the only thread that
373 * updates the curlen, curpos, nLeft fields.
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));
387 /* out of packet. Get another one. */
389 MUTEX_ENTER(&call->lock);
390 rxi_FreePacket(call->currentPacket);
391 call->currentPacket = (struct rx_packet *)0;
392 MUTEX_EXIT(&call->lock);
395 return sizeof(afs_int32);
399 MUTEX_ENTER(&call->lock);
400 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
401 MUTEX_EXIT(&call->lock);
408 * Uses packets in the receive queue to fill in as much of the
409 * current iovec as possible. Does not block if it runs out
410 * of packets to complete the iovec. Return true if an ack packet
411 * was sent, otherwise return false */
413 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
417 register unsigned int t;
418 struct rx_packet *rp;
419 struct rx_packet *curp;
420 struct iovec *call_iov;
421 struct iovec *cur_iov = NULL;
423 curp = call->currentPacket;
425 cur_iov = &curp->wirevec[call->curvec];
427 call_iov = &call->iov[call->iovNext];
429 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
430 if (call->nLeft == 0) {
431 /* Get next packet */
432 if (queue_IsNotEmpty(&call->rq)) {
433 /* Check that next packet available is next in sequence */
434 rp = queue_First(&call->rq, rx_packet);
435 if (rp->header.seq == call->rnext) {
437 register struct rx_connection *conn = call->conn;
440 /* RXS_CheckPacket called to undo RXS_PreparePacket's
441 * work. It may reduce the length of the packet by up
442 * to conn->maxTrailerSize, to reflect the length of the
443 * data + the header. */
445 RXS_CheckPacket(conn->securityObject, call, rp))) {
446 /* Used to merely shut down the call, but now we
447 * shut down the whole connection since this may
448 * indicate an attempt to hijack it */
450 MUTEX_EXIT(&call->lock);
451 rxi_ConnectionError(conn, error);
452 MUTEX_ENTER(&conn->conn_data_lock);
453 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
454 MUTEX_EXIT(&conn->conn_data_lock);
456 MUTEX_ENTER(&call->lock);
461 curp = call->currentPacket = rp;
462 call->curvec = 1; /* 0th vec is always header */
463 cur_iov = &curp->wirevec[1];
464 /* begin at the beginning [ more or less ], continue
465 * on until the end, then stop. */
467 (char *)curp->wirevec[1].iov_base +
468 call->conn->securityHeaderSize;
470 curp->wirevec[1].iov_len -
471 call->conn->securityHeaderSize;
473 /* Notice that this code works correctly if the data
474 * size is 0 (which it may be--no reply arguments from
475 * server, for example). This relies heavily on the
476 * fact that the code below immediately frees the packet
477 * (no yields, etc.). If it didn't, this would be a
478 * problem because a value of zero for call->nLeft
479 * normally means that there is no read packet */
480 call->nLeft = curp->length;
481 hadd32(call->bytesRcvd, curp->length);
483 /* Send a hard ack for every rxi_HardAckRate+1 packets
484 * consumed. Otherwise schedule an event to send
485 * the hard ack later on.
495 /* It's possible for call->nLeft to be smaller than any particular
496 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
497 * reflects the size of the buffer. We have to keep track of the
498 * number of bytes read in the length field of the packet struct. On
499 * the final portion of a received packet, it's almost certain that
500 * call->nLeft will be smaller than the final buffer. */
501 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
503 t = MIN((int)call->curlen, call->iovNBytes);
504 t = MIN(t, (int)call->nLeft);
505 call_iov->iov_base = call->curpos;
506 call_iov->iov_len = t;
509 call->iovNBytes -= t;
515 /* out of packet. Get another one. */
516 queue_Append(&call->iovq, curp);
517 curp = call->currentPacket = (struct rx_packet *)0;
518 } else if (!call->curlen) {
519 /* need to get another struct iov */
520 if (++call->curvec >= curp->niovecs) {
521 /* current packet is exhausted, get ready for another */
522 /* don't worry about curvec and stuff, they get set somewhere else */
523 queue_Append(&call->iovq, curp);
524 curp = call->currentPacket = (struct rx_packet *)0;
528 call->curpos = (char *)cur_iov->iov_base;
529 call->curlen = cur_iov->iov_len;
535 /* If we consumed any packets then check whether we need to
536 * send a hard ack. */
537 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
538 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
539 rxevent_Cancel(call->delayedAckEvent, call,
540 RX_CALL_REFCOUNT_DELAY);
541 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
544 struct clock when, now;
547 /* Delay to consolidate ack packets */
548 clock_Add(&when, &rx_hardAckDelay);
549 if (!call->delayedAckEvent
550 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
551 rxevent_Cancel(call->delayedAckEvent, call,
552 RX_CALL_REFCOUNT_DELAY);
553 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
554 call->delayedAckEvent =
555 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
563 /* rxi_ReadvProc -- internal version.
565 * Fills in an iovec with pointers to the packet buffers. All packets
566 * except the last packet (new current packet) are moved to the iovq
567 * while the application is processing the data.
569 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
572 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
578 requestCount = nbytes;
581 /* Free any packets from the last call to ReadvProc/WritevProc */
582 if (queue_IsNotEmpty(&call->iovq)) {
583 rxi_FreePackets(0, &call->iovq);
586 if (call->mode == RX_MODE_SENDING) {
587 rxi_FlushWrite(call);
594 /* Get whatever data is currently available in the receive queue.
595 * If rxi_FillReadVec sends an ack packet then it is possible
596 * that we will receive more data while we drop the call lock
597 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
598 * here to avoid a race with the receive thread if we send
599 * hard acks in rxi_FillReadVec. */
600 call->flags |= RX_CALL_IOVEC_WAIT;
601 call->iovNBytes = nbytes;
602 call->iovMax = maxio;
605 rxi_FillReadVec(call, 0);
607 /* if we need more data then sleep until the receive thread has
608 * filled in the rest. */
609 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
610 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
611 call->flags |= RX_CALL_READER_WAIT;
613 call->startWait = clock_Sec();
614 while (call->flags & RX_CALL_READER_WAIT) {
615 #ifdef RX_ENABLE_LOCKS
616 CV_WAIT(&call->cv_rq, &call->lock);
618 osi_rxSleep(&call->rq);
623 call->flags &= ~RX_CALL_IOVEC_WAIT;
624 #ifdef RX_ENABLE_LOCKS
628 #endif /* RX_ENABLE_LOCKS */
631 *nio = call->iovNext;
632 return nbytes - call->iovNBytes;
636 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
643 MUTEX_ENTER(&call->lock);
644 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
645 MUTEX_EXIT(&call->lock);
650 /* rxi_WriteProc -- internal version.
652 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
655 rxi_WriteProc(register struct rx_call *call, register char *buf,
658 struct rx_connection *conn = call->conn;
659 register struct rx_packet *cp = call->currentPacket;
660 register unsigned int t;
661 int requestCount = nbytes;
663 /* Free any packets from the last call to ReadvProc/WritevProc */
664 if (queue_IsNotEmpty(&call->iovq)) {
665 rxi_FreePackets(0, &call->iovq);
668 if (call->mode != RX_MODE_SENDING) {
669 if ((conn->type == RX_SERVER_CONNECTION)
670 && (call->mode == RX_MODE_RECEIVING)) {
671 call->mode = RX_MODE_SENDING;
674 cp = call->currentPacket = (struct rx_packet *)0;
683 /* Loop condition is checked at end, so that a write of 0 bytes
684 * will force a packet to be created--specially for the case where
685 * there are 0 bytes on the stream, but we must send a packet
688 if (call->nFree == 0) {
689 if (!call->error && cp) {
690 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
691 /* Wait until TQ_BUSY is reset before adding any
692 * packets to the transmit queue
694 while (call->flags & RX_CALL_TQ_BUSY) {
695 call->flags |= RX_CALL_TQ_WAIT;
696 #ifdef RX_ENABLE_LOCKS
697 CV_WAIT(&call->cv_tq, &call->lock);
698 #else /* RX_ENABLE_LOCKS */
699 osi_rxSleep(&call->tq);
700 #endif /* RX_ENABLE_LOCKS */
702 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
703 clock_NewTime(); /* Bogus: need new time package */
704 /* The 0, below, specifies that it is not the last packet:
705 * there will be others. PrepareSendPacket may
706 * alter the packet length by up to
707 * conn->securityMaxTrailerSize */
708 hadd32(call->bytesSent, cp->length);
709 rxi_PrepareSendPacket(call, cp, 0);
710 queue_Append(&call->tq, cp);
711 cp = call->currentPacket = NULL;
714 flags & (RX_CALL_FAST_RECOVER |
715 RX_CALL_FAST_RECOVER_WAIT))) {
716 rxi_Start(0, call, 0, 0);
719 /* Wait for transmit window to open up */
721 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
723 call->startWait = clock_Sec();
725 #ifdef RX_ENABLE_LOCKS
726 CV_WAIT(&call->cv_twind, &call->lock);
728 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
729 osi_rxSleep(&call->twind);
733 #ifdef RX_ENABLE_LOCKS
737 #endif /* RX_ENABLE_LOCKS */
739 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
740 call->currentPacket = cp;
741 call->nFree = cp->length;
742 call->curvec = 1; /* 0th vec is always header */
743 /* begin at the beginning [ more or less ], continue
744 * on until the end, then stop. */
746 (char *)cp->wirevec[1].iov_base +
747 call->conn->securityHeaderSize;
749 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
754 call->currentPacket = NULL;
760 if (cp && (int)call->nFree < nbytes) {
761 /* Try to extend the current buffer */
762 register int len, mud;
764 mud = rx_MaxUserDataSize(call);
767 want = MIN(nbytes - (int)call->nFree, mud - len);
768 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
769 if (cp->length > (unsigned)mud)
771 call->nFree += (cp->length - len);
775 /* If the remaining bytes fit in the buffer, then store them
776 * and return. Don't ship a buffer that's full immediately to
777 * the peer--we don't know if it's the last buffer yet */
783 while (nbytes && call->nFree) {
785 t = MIN((int)call->curlen, nbytes);
786 t = MIN((int)call->nFree, t);
787 memcpy(call->curpos, buf, t);
791 call->curlen -= (u_short)t;
792 call->nFree -= (u_short)t;
795 /* need to get another struct iov */
796 if (++call->curvec >= cp->niovecs) {
797 /* current packet is full, extend or send it */
800 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
801 call->curlen = cp->wirevec[call->curvec].iov_len;
804 } /* while bytes to send and room to send them */
806 /* might be out of space now */
809 } else; /* more data to send, so get another packet and keep going */
812 return requestCount - nbytes;
816 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
825 * Free any packets from the last call to ReadvProc/WritevProc.
826 * We do not need the lock because the receiver threads only
827 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
828 * RX_CALL_IOVEC_WAIT is always cleared before returning from
829 * ReadvProc/WritevProc.
831 if (queue_IsNotEmpty(&call->iovq)) {
832 rxi_FreePackets(0, &call->iovq);
836 * Most common case: all of the data fits in the current iovec.
837 * We do not need the lock because this is the only thread that
838 * updates the curlen, curpos, nFree fields.
840 * We are relying on nFree being zero unless the call is in send mode.
842 tcurlen = (int)call->curlen;
843 tnFree = (int)call->nFree;
844 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
845 tcurpos = call->curpos;
846 memcpy(tcurpos, buf, nbytes);
847 call->curpos = tcurpos + nbytes;
848 call->curlen = (u_short)(tcurlen - nbytes);
849 call->nFree = (u_short)(tnFree - nbytes);
854 MUTEX_ENTER(&call->lock);
855 bytes = rxi_WriteProc(call, buf, nbytes);
856 MUTEX_EXIT(&call->lock);
861 /* Optimization for marshalling 32 bit arguments */
863 rx_WriteProc32(register struct rx_call *call, register afs_int32 * value)
872 * Free any packets from the last call to ReadvProc/WritevProc.
873 * We do not need the lock because the receiver threads only
874 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
875 * RX_CALL_IOVEC_WAIT is always cleared before returning from
876 * ReadvProc/WritevProc.
878 if (queue_IsNotEmpty(&call->iovq)) {
879 rxi_FreePackets(0, &call->iovq);
883 * Most common case: all of the data fits in the current iovec.
884 * We do not need the lock because this is the only thread that
885 * updates the curlen, curpos, nFree fields.
887 * We are relying on nFree being zero unless the call is in send mode.
889 tcurlen = call->curlen;
890 tnFree = call->nFree;
891 if (!call->error && tcurlen >= sizeof(afs_int32)
892 && tnFree >= sizeof(afs_int32)) {
893 tcurpos = call->curpos;
894 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
895 *((afs_int32 *) (tcurpos)) = *value;
897 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
899 call->curpos = tcurpos + sizeof(afs_int32);
900 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
901 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
902 return sizeof(afs_int32);
906 MUTEX_ENTER(&call->lock);
907 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
908 MUTEX_EXIT(&call->lock);
913 /* rxi_WritevAlloc -- internal version.
915 * Fill in an iovec to point to data in packet buffers. The application
916 * calls rxi_WritevProc when the buffers are full.
918 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
921 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
924 struct rx_connection *conn = call->conn;
925 struct rx_packet *cp = call->currentPacket;
928 /* Temporary values, real work is done in rxi_WritevProc */
934 requestCount = nbytes;
937 /* Free any packets from the last call to ReadvProc/WritevProc */
938 if (queue_IsNotEmpty(&call->iovq)) {
939 rxi_FreePackets(0, &call->iovq);
942 if (call->mode != RX_MODE_SENDING) {
943 if ((conn->type == RX_SERVER_CONNECTION)
944 && (call->mode == RX_MODE_RECEIVING)) {
945 call->mode = RX_MODE_SENDING;
948 cp = call->currentPacket = (struct rx_packet *)0;
957 /* Set up the iovec to point to data in packet buffers. */
958 tnFree = call->nFree;
959 tcurvec = call->curvec;
960 tcurpos = call->curpos;
961 tcurlen = call->curlen;
963 register unsigned int t;
966 /* current packet is full, allocate a new one */
967 cp = rxi_AllocSendPacket(call, nbytes);
969 /* out of space, return what we have */
971 return requestCount - nbytes;
973 queue_Append(&call->iovq, cp);
977 (char *)cp->wirevec[1].iov_base +
978 call->conn->securityHeaderSize;
979 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
982 if (tnFree < nbytes) {
983 /* try to extend the current packet */
984 register int len, mud;
986 mud = rx_MaxUserDataSize(call);
989 want = MIN(nbytes - tnFree, mud - len);
990 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
991 if (cp->length > (unsigned)mud)
993 tnFree += (cp->length - len);
994 if (cp == call->currentPacket) {
995 call->nFree += (cp->length - len);
1000 /* fill in the next entry in the iovec */
1001 t = MIN(tcurlen, nbytes);
1003 iov[nextio].iov_base = tcurpos;
1004 iov[nextio].iov_len = t;
1012 /* need to get another struct iov */
1013 if (++tcurvec >= cp->niovecs) {
1014 /* current packet is full, extend it or move on to next packet */
1017 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1018 tcurlen = cp->wirevec[tcurvec].iov_len;
1021 } while (nbytes && nextio < maxio);
1023 return requestCount - nbytes;
1027 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1034 MUTEX_ENTER(&call->lock);
1035 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1036 MUTEX_EXIT(&call->lock);
1041 /* rxi_WritevProc -- internal version.
1043 * Send buffers allocated in rxi_WritevAlloc.
1045 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1048 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1050 struct rx_packet *cp = call->currentPacket;
1053 struct rx_queue tmpq;
1055 requestCount = nbytes;
1058 if (call->mode != RX_MODE_SENDING) {
1059 call->error = RX_PROTOCOL_ERROR;
1061 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1062 /* Wait until TQ_BUSY is reset before trying to move any
1063 * packets to the transmit queue. */
1064 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1065 call->flags |= RX_CALL_TQ_WAIT;
1066 #ifdef RX_ENABLE_LOCKS
1067 CV_WAIT(&call->cv_tq, &call->lock);
1068 #else /* RX_ENABLE_LOCKS */
1069 osi_rxSleep(&call->tq);
1070 #endif /* RX_ENABLE_LOCKS */
1072 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1076 queue_Prepend(&call->iovq, cp);
1077 cp = call->currentPacket = NULL;
1079 rxi_FreePackets(0, &call->iovq);
1083 /* Loop through the I/O vector adjusting packet pointers.
1084 * Place full packets back onto the iovq once they are ready
1085 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1086 * the iovec. We put the loop condition at the end to ensure that
1087 * a zero length write will push a short packet. */
1091 if (call->nFree == 0 && cp) {
1092 clock_NewTime(); /* Bogus: need new time package */
1093 /* The 0, below, specifies that it is not the last packet:
1094 * there will be others. PrepareSendPacket may
1095 * alter the packet length by up to
1096 * conn->securityMaxTrailerSize */
1097 hadd32(call->bytesSent, cp->length);
1098 rxi_PrepareSendPacket(call, cp, 0);
1099 queue_Append(&tmpq, cp);
1101 /* The head of the iovq is now the current packet */
1103 if (queue_IsEmpty(&call->iovq)) {
1104 call->error = RX_PROTOCOL_ERROR;
1105 cp = call->currentPacket = NULL;
1106 rxi_FreePackets(0, &tmpq);
1109 cp = queue_First(&call->iovq, rx_packet);
1111 call->currentPacket = cp;
1112 call->nFree = cp->length;
1115 (char *)cp->wirevec[1].iov_base +
1116 call->conn->securityHeaderSize;
1118 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1123 /* The next iovec should point to the current position */
1124 if (iov[nextio].iov_base != call->curpos
1125 || iov[nextio].iov_len > (int)call->curlen) {
1126 call->error = RX_PROTOCOL_ERROR;
1128 queue_Prepend(&tmpq, cp);
1129 call->currentPacket = NULL;
1131 rxi_FreePackets(0, &tmpq);
1134 nbytes -= iov[nextio].iov_len;
1135 call->curpos += iov[nextio].iov_len;
1136 call->curlen -= iov[nextio].iov_len;
1137 call->nFree -= iov[nextio].iov_len;
1139 if (call->curlen == 0) {
1140 if (++call->curvec > cp->niovecs) {
1143 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1144 call->curlen = cp->wirevec[call->curvec].iov_len;
1148 } while (nbytes && nextio < nio);
1150 /* Move the packets from the temporary queue onto the transmit queue.
1151 * We may end up with more than call->twind packets on the queue. */
1152 queue_SpliceAppend(&call->tq, &tmpq);
1154 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1155 rxi_Start(0, call, 0, 0);
1158 /* Wait for the length of the transmit queue to fall below call->twind */
1159 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1161 call->startWait = clock_Sec();
1162 #ifdef RX_ENABLE_LOCKS
1163 CV_WAIT(&call->cv_twind, &call->lock);
1165 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1166 osi_rxSleep(&call->twind);
1168 call->startWait = 0;
1174 cp = call->currentPacket = NULL;
1179 return requestCount - nbytes;
1183 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1189 MUTEX_ENTER(&call->lock);
1190 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1191 MUTEX_EXIT(&call->lock);
1196 /* Flush any buffered data to the stream, switch to read mode
1197 * (clients) or to EOF mode (servers) */
1199 rxi_FlushWrite(register struct rx_call *call)
1201 register struct rx_packet *cp = call->currentPacket;
1203 /* Free any packets from the last call to ReadvProc/WritevProc */
1204 if (queue_IsNotEmpty(&call->iovq)) {
1205 rxi_FreePackets(0, &call->iovq);
1208 if (call->mode == RX_MODE_SENDING) {
1211 (call->conn->type ==
1212 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1214 #ifdef RX_KERNEL_TRACE
1216 int glockOwner = ISAFS_GLOCK();
1219 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1220 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1227 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1228 /* Wait until TQ_BUSY is reset before adding any
1229 * packets to the transmit queue
1231 while (call->flags & RX_CALL_TQ_BUSY) {
1232 call->flags |= RX_CALL_TQ_WAIT;
1233 #ifdef RX_ENABLE_LOCKS
1234 CV_WAIT(&call->cv_tq, &call->lock);
1235 #else /* RX_ENABLE_LOCKS */
1236 osi_rxSleep(&call->tq);
1237 #endif /* RX_ENABLE_LOCKS */
1239 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1242 /* cp->length is only supposed to be the user's data */
1243 /* cp->length was already set to (then-current)
1244 * MaxUserDataSize or less. */
1245 cp->length -= call->nFree;
1246 call->currentPacket = (struct rx_packet *)0;
1249 cp = rxi_AllocSendPacket(call, 0);
1251 /* Mode can no longer be MODE_SENDING */
1255 cp->niovecs = 2; /* header + space for rxkad stuff */
1259 /* The 1 specifies that this is the last packet */
1260 hadd32(call->bytesSent, cp->length);
1261 rxi_PrepareSendPacket(call, cp, 1);
1262 queue_Append(&call->tq, cp);
1265 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1266 rxi_Start(0, call, 0, 0);
1271 /* Flush any buffered data to the stream, switch to read mode
1272 * (clients) or to EOF mode (servers) */
1274 rx_FlushWrite(struct rx_call *call)
1278 MUTEX_ENTER(&call->lock);
1279 rxi_FlushWrite(call);
1280 MUTEX_EXIT(&call->lock);