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>
11 #include <afs/param.h>
15 # ifdef RX_KERNEL_TRACE
16 # include "rx_kcommon.h"
18 # if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
19 # include "afs/sysincludes.h"
24 # if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV)
28 # include <net/net_globals.h>
29 # endif /* AFS_OSF_ENV */
30 # ifdef AFS_LINUX20_ENV
31 # include "h/socket.h"
33 # include "netinet/in.h"
34 # if defined(AFS_SGI_ENV)
35 # include "afs/sysincludes.h"
38 # include "afs/afs_args.h"
39 # if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
43 # include "afs/sysincludes.h"
44 # endif /* !UKERNEL */
47 # undef RXDEBUG /* turn off debugging */
50 # include "afs/afs_osi.h"
51 # include "rx_kmutex.h"
52 # include "rx/rx_kernel.h"
53 # include "afs/lock.h"
61 #include "rx_globals.h"
64 /* rxdb_fileID is used to identify the lock location, along with line#. */
65 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
66 #endif /* RX_LOCKS_DB */
67 /* rxi_ReadProc -- internal version.
69 * LOCKS USED -- called at netpri
72 rxi_ReadProc(struct rx_call *call, char *buf,
75 struct rx_packet *cp = call->currentPacket;
80 /* XXXX took out clock_NewTime from here. Was it needed? */
81 requestCount = nbytes;
83 /* Free any packets from the last call to ReadvProc/WritevProc */
84 if (queue_IsNotEmpty(&call->iovq)) {
87 #endif /* RXDEBUG_PACKET */
88 rxi_FreePackets(0, &call->iovq);
92 if (call->nLeft == 0) {
94 MUTEX_ENTER(&call->lock);
96 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
98 call->mode = RX_MODE_ERROR;
99 MUTEX_EXIT(&call->lock);
102 if (call->mode == RX_MODE_SENDING) {
103 MUTEX_EXIT(&call->lock);
104 rxi_FlushWrite(call);
105 MUTEX_ENTER(&call->lock);
109 if (queue_IsNotEmpty(&call->rq)) {
110 /* Check that next packet available is next in sequence */
111 rp = queue_First(&call->rq, rx_packet);
112 if (rp->header.seq == call->rnext) {
114 struct rx_connection *conn = call->conn;
116 #ifdef RX_TRACK_PACKETS
117 rp->flags &= ~RX_PKTFLAG_RQ;
119 #ifdef RXDEBUG_PACKET
121 #endif /* RXDEBUG_PACKET */
123 /* RXS_CheckPacket called to undo RXS_PreparePacket's
124 * work. It may reduce the length of the packet by up
125 * to conn->maxTrailerSize, to reflect the length of the
126 * data + the header. */
128 RXS_CheckPacket(conn->securityObject, call,
130 /* Used to merely shut down the call, but now we
131 * shut down the whole connection since this may
132 * indicate an attempt to hijack it */
134 MUTEX_EXIT(&call->lock);
135 rxi_ConnectionError(conn, error);
136 MUTEX_ENTER(&conn->conn_data_lock);
137 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
138 MUTEX_EXIT(&conn->conn_data_lock);
144 cp = call->currentPacket = rp;
145 #ifdef RX_TRACK_PACKETS
146 call->currentPacket->flags |= RX_PKTFLAG_CP;
148 call->curvec = 1; /* 0th vec is always header */
149 /* begin at the beginning [ more or less ], continue
150 * on until the end, then stop. */
152 (char *)cp->wirevec[1].iov_base +
153 call->conn->securityHeaderSize;
155 cp->wirevec[1].iov_len -
156 call->conn->securityHeaderSize;
158 /* Notice that this code works correctly if the data
159 * size is 0 (which it may be--no reply arguments from
160 * server, for example). This relies heavily on the
161 * fact that the code below immediately frees the packet
162 * (no yields, etc.). If it didn't, this would be a
163 * problem because a value of zero for call->nLeft
164 * normally means that there is no read packet */
165 call->nLeft = cp->length;
166 hadd32(call->bytesRcvd, cp->length);
168 /* Send a hard ack for every rxi_HardAckRate+1 packets
169 * consumed. Otherwise schedule an event to send
170 * the hard ack later on.
173 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
174 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
175 rxevent_Cancel(call->delayedAckEvent, call,
176 RX_CALL_REFCOUNT_DELAY);
177 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
179 struct clock when, now;
182 /* Delay to consolidate ack packets */
183 clock_Add(&when, &rx_hardAckDelay);
184 if (!call->delayedAckEvent
185 || clock_Gt(&call->delayedAckEvent->
187 rxevent_Cancel(call->delayedAckEvent,
189 RX_CALL_REFCOUNT_DELAY);
190 MUTEX_ENTER(&rx_refcnt_mutex);
191 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
192 MUTEX_EXIT(&rx_refcnt_mutex);
193 call->delayedAckEvent =
194 rxevent_PostNow(&when, &now,
195 rxi_SendDelayedAck, call,
205 * If we reach this point either we have no packets in the
206 * receive queue or the next packet in the queue is not the
207 * one we are looking for. There is nothing else for us to
208 * do but wait for another packet to arrive.
211 /* Are there ever going to be any more packets? */
212 if (call->flags & RX_CALL_RECEIVE_DONE) {
213 MUTEX_EXIT(&call->lock);
214 return requestCount - nbytes;
216 /* Wait for in-sequence packet */
217 call->flags |= RX_CALL_READER_WAIT;
219 call->startWait = clock_Sec();
220 while (call->flags & RX_CALL_READER_WAIT) {
221 #ifdef RX_ENABLE_LOCKS
222 CV_WAIT(&call->cv_rq, &call->lock);
224 osi_rxSleep(&call->rq);
227 cp = call->currentPacket;
230 #ifdef RX_ENABLE_LOCKS
232 MUTEX_EXIT(&call->lock);
235 #endif /* RX_ENABLE_LOCKS */
237 MUTEX_EXIT(&call->lock);
239 /* osi_Assert(cp); */
240 /* MTUXXX this should be replaced by some error-recovery code before shipping */
241 /* yes, the following block is allowed to be the ELSE clause (or not) */
242 /* It's possible for call->nLeft to be smaller than any particular
243 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
244 * reflects the size of the buffer. We have to keep track of the
245 * number of bytes read in the length field of the packet struct. On
246 * the final portion of a received packet, it's almost certain that
247 * call->nLeft will be smaller than the final buffer. */
248 while (nbytes && cp) {
249 t = MIN((int)call->curlen, nbytes);
250 t = MIN(t, (int)call->nLeft);
251 memcpy(buf, call->curpos, t);
259 /* out of packet. Get another one. */
260 #ifdef RX_TRACK_PACKETS
261 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
264 cp = call->currentPacket = (struct rx_packet *)0;
265 } else if (!call->curlen) {
266 /* need to get another struct iov */
267 if (++call->curvec >= cp->niovecs) {
268 /* current packet is exhausted, get ready for another */
269 /* don't worry about curvec and stuff, they get set somewhere else */
270 #ifdef RX_TRACK_PACKETS
271 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
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)
302 /* Free any packets from the last call to ReadvProc/WritevProc */
303 if (!queue_IsEmpty(&call->iovq)) {
304 #ifdef RXDEBUG_PACKET
306 #endif /* RXDEBUG_PACKET */
307 rxi_FreePackets(0, &call->iovq);
311 * Most common case, all of the data is in the current iovec.
312 * We are relying on nLeft being zero unless the call is in receive mode.
314 tcurlen = call->curlen;
315 tnLeft = call->nLeft;
316 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
317 tcurpos = call->curpos;
318 memcpy(buf, tcurpos, nbytes);
320 call->curpos = tcurpos + nbytes;
321 call->curlen = tcurlen - nbytes;
322 call->nLeft = tnLeft - nbytes;
324 if (!call->nLeft && call->currentPacket != NULL) {
325 /* out of packet. Get another one. */
326 rxi_FreePacket(call->currentPacket);
327 call->currentPacket = (struct rx_packet *)0;
333 bytes = rxi_ReadProc(call, buf, nbytes);
338 /* Optimization for unmarshalling 32 bit integers */
340 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
348 /* Free any packets from the last call to ReadvProc/WritevProc */
349 if (!queue_IsEmpty(&call->iovq)) {
350 #ifdef RXDEBUG_PACKET
352 #endif /* RXDEBUG_PACKET */
353 rxi_FreePackets(0, &call->iovq);
357 * Most common case, all of the data is in the current iovec.
358 * We are relying on nLeft being zero unless the call is in receive mode.
360 tcurlen = call->curlen;
361 tnLeft = call->nLeft;
362 if (!call->error && tcurlen >= sizeof(afs_int32)
363 && tnLeft >= sizeof(afs_int32)) {
364 tcurpos = call->curpos;
366 memcpy((char *)value, tcurpos, sizeof(afs_int32));
368 call->curpos = tcurpos + sizeof(afs_int32);
369 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
370 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
371 if (!call->nLeft && call->currentPacket != NULL) {
372 /* out of packet. Get another one. */
373 rxi_FreePacket(call->currentPacket);
374 call->currentPacket = (struct rx_packet *)0;
376 return sizeof(afs_int32);
380 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
388 * Uses packets in the receive queue to fill in as much of the
389 * current iovec as possible. Does not block if it runs out
390 * of packets to complete the iovec. Return true if an ack packet
391 * was sent, otherwise return false */
393 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
398 struct rx_packet *rp;
399 struct rx_packet *curp;
400 struct iovec *call_iov;
401 struct iovec *cur_iov = NULL;
403 curp = call->currentPacket;
405 cur_iov = &curp->wirevec[call->curvec];
407 call_iov = &call->iov[call->iovNext];
409 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
410 if (call->nLeft == 0) {
411 /* Get next packet */
412 if (queue_IsNotEmpty(&call->rq)) {
413 /* Check that next packet available is next in sequence */
414 rp = queue_First(&call->rq, rx_packet);
415 if (rp->header.seq == call->rnext) {
417 struct rx_connection *conn = call->conn;
419 #ifdef RX_TRACK_PACKETS
420 rp->flags &= ~RX_PKTFLAG_RQ;
422 #ifdef RXDEBUG_PACKET
424 #endif /* RXDEBUG_PACKET */
426 /* RXS_CheckPacket called to undo RXS_PreparePacket's
427 * work. It may reduce the length of the packet by up
428 * to conn->maxTrailerSize, to reflect the length of the
429 * data + the header. */
431 RXS_CheckPacket(conn->securityObject, call, rp))) {
432 /* Used to merely shut down the call, but now we
433 * shut down the whole connection since this may
434 * indicate an attempt to hijack it */
436 MUTEX_EXIT(&call->lock);
437 rxi_ConnectionError(conn, error);
438 MUTEX_ENTER(&conn->conn_data_lock);
439 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
440 MUTEX_EXIT(&conn->conn_data_lock);
442 MUTEX_ENTER(&call->lock);
447 curp = call->currentPacket = rp;
448 #ifdef RX_TRACK_PACKETS
449 call->currentPacket->flags |= RX_PKTFLAG_CP;
451 call->curvec = 1; /* 0th vec is always header */
452 cur_iov = &curp->wirevec[1];
453 /* begin at the beginning [ more or less ], continue
454 * on until the end, then stop. */
456 (char *)curp->wirevec[1].iov_base +
457 call->conn->securityHeaderSize;
459 curp->wirevec[1].iov_len -
460 call->conn->securityHeaderSize;
462 /* Notice that this code works correctly if the data
463 * size is 0 (which it may be--no reply arguments from
464 * server, for example). This relies heavily on the
465 * fact that the code below immediately frees the packet
466 * (no yields, etc.). If it didn't, this would be a
467 * problem because a value of zero for call->nLeft
468 * normally means that there is no read packet */
469 call->nLeft = curp->length;
470 hadd32(call->bytesRcvd, curp->length);
472 /* Send a hard ack for every rxi_HardAckRate+1 packets
473 * consumed. Otherwise schedule an event to send
474 * the hard ack later on.
484 /* It's possible for call->nLeft to be smaller than any particular
485 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
486 * reflects the size of the buffer. We have to keep track of the
487 * number of bytes read in the length field of the packet struct. On
488 * the final portion of a received packet, it's almost certain that
489 * call->nLeft will be smaller than the final buffer. */
490 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
492 t = MIN((int)call->curlen, call->iovNBytes);
493 t = MIN(t, (int)call->nLeft);
494 call_iov->iov_base = call->curpos;
495 call_iov->iov_len = t;
498 call->iovNBytes -= t;
504 /* out of packet. Get another one. */
505 #ifdef RX_TRACK_PACKETS
506 curp->flags &= ~RX_PKTFLAG_CP;
507 curp->flags |= RX_PKTFLAG_IOVQ;
509 queue_Append(&call->iovq, curp);
510 #ifdef RXDEBUG_PACKET
512 #endif /* RXDEBUG_PACKET */
513 curp = call->currentPacket = (struct rx_packet *)0;
514 } else if (!call->curlen) {
515 /* need to get another struct iov */
516 if (++call->curvec >= curp->niovecs) {
517 /* current packet is exhausted, get ready for another */
518 /* don't worry about curvec and stuff, they get set somewhere else */
519 #ifdef RX_TRACK_PACKETS
520 curp->flags &= ~RX_PKTFLAG_CP;
521 curp->flags |= RX_PKTFLAG_IOVQ;
523 queue_Append(&call->iovq, curp);
524 #ifdef RXDEBUG_PACKET
526 #endif /* RXDEBUG_PACKET */
527 curp = call->currentPacket = (struct rx_packet *)0;
531 call->curpos = (char *)cur_iov->iov_base;
532 call->curlen = cur_iov->iov_len;
538 /* If we consumed any packets then check whether we need to
539 * send a hard ack. */
540 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
541 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
542 rxevent_Cancel(call->delayedAckEvent, call,
543 RX_CALL_REFCOUNT_DELAY);
544 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
547 struct clock when, now;
550 /* Delay to consolidate ack packets */
551 clock_Add(&when, &rx_hardAckDelay);
552 if (!call->delayedAckEvent
553 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
554 rxevent_Cancel(call->delayedAckEvent, call,
555 RX_CALL_REFCOUNT_DELAY);
556 MUTEX_ENTER(&rx_refcnt_mutex);
557 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
558 MUTEX_EXIT(&rx_refcnt_mutex);
559 call->delayedAckEvent =
560 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
568 /* rxi_ReadvProc -- internal version.
570 * Fills in an iovec with pointers to the packet buffers. All packets
571 * except the last packet (new current packet) are moved to the iovq
572 * while the application is processing the data.
574 * LOCKS USED -- called at netpri.
577 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
582 /* Free any packets from the last call to ReadvProc/WritevProc */
583 if (queue_IsNotEmpty(&call->iovq)) {
584 #ifdef RXDEBUG_PACKET
586 #endif /* RXDEBUG_PACKET */
587 rxi_FreePackets(0, &call->iovq);
590 if (call->mode == RX_MODE_SENDING) {
591 rxi_FlushWrite(call);
594 MUTEX_ENTER(&call->lock);
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;
633 *nio = call->iovNext;
634 bytes = nbytes - call->iovNBytes;
635 MUTEX_EXIT(&call->lock);
639 MUTEX_EXIT(&call->lock);
640 call->mode = RX_MODE_ERROR;
645 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
652 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
657 /* rxi_WriteProc -- internal version.
659 * LOCKS USED -- called at netpri
663 rxi_WriteProc(struct rx_call *call, char *buf,
666 struct rx_connection *conn = call->conn;
667 struct rx_packet *cp = call->currentPacket;
669 int requestCount = nbytes;
671 /* Free any packets from the last call to ReadvProc/WritevProc */
672 if (queue_IsNotEmpty(&call->iovq)) {
673 #ifdef RXDEBUG_PACKET
675 #endif /* RXDEBUG_PACKET */
676 rxi_FreePackets(0, &call->iovq);
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;
684 #ifdef RX_TRACK_PACKETS
685 cp->flags &= ~RX_PKTFLAG_CP;
688 cp = call->currentPacket = (struct rx_packet *)0;
697 /* Loop condition is checked at end, so that a write of 0 bytes
698 * will force a packet to be created--specially for the case where
699 * there are 0 bytes on the stream, but we must send a packet
702 if (call->nFree == 0) {
703 MUTEX_ENTER(&call->lock);
704 cp = call->currentPacket;
706 call->mode = RX_MODE_ERROR;
707 if (!call->error && cp) {
708 /* Clear the current packet now so that if
709 * we are forced to wait and drop the lock
710 * the packet we are planning on using
713 #ifdef RX_TRACK_PACKETS
714 cp->flags &= ~RX_PKTFLAG_CP;
716 call->currentPacket = (struct rx_packet *)0;
717 clock_NewTime(); /* Bogus: need new time package */
718 /* The 0, below, specifies that it is not the last packet:
719 * there will be others. PrepareSendPacket may
720 * alter the packet length by up to
721 * conn->securityMaxTrailerSize */
722 hadd32(call->bytesSent, cp->length);
723 rxi_PrepareSendPacket(call, cp, 0);
724 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
725 /* PrepareSendPacket drops the call lock */
726 rxi_WaitforTQBusy(call);
727 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
728 #ifdef RX_TRACK_PACKETS
729 cp->flags |= RX_PKTFLAG_TQ;
731 queue_Append(&call->tq, cp);
732 #ifdef RXDEBUG_PACKET
734 #endif /* RXDEBUG_PACKET */
735 cp = (struct rx_packet *)0;
738 flags & (RX_CALL_FAST_RECOVER |
739 RX_CALL_FAST_RECOVER_WAIT))) {
740 rxi_Start(0, call, 0, 0);
743 #ifdef RX_TRACK_PACKETS
744 cp->flags &= ~RX_PKTFLAG_CP;
747 cp = call->currentPacket = (struct rx_packet *)0;
749 /* Wait for transmit window to open up */
751 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
753 call->startWait = clock_Sec();
755 #ifdef RX_ENABLE_LOCKS
756 CV_WAIT(&call->cv_twind, &call->lock);
758 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
759 osi_rxSleep(&call->twind);
763 #ifdef RX_ENABLE_LOCKS
765 call->mode = RX_MODE_ERROR;
766 MUTEX_EXIT(&call->lock);
769 #endif /* RX_ENABLE_LOCKS */
771 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
772 #ifdef RX_TRACK_PACKETS
773 cp->flags |= RX_PKTFLAG_CP;
775 call->currentPacket = cp;
776 call->nFree = cp->length;
777 call->curvec = 1; /* 0th vec is always header */
778 /* begin at the beginning [ more or less ], continue
779 * on until the end, then stop. */
781 (char *)cp->wirevec[1].iov_base +
782 call->conn->securityHeaderSize;
784 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
787 call->mode = RX_MODE_ERROR;
789 #ifdef RX_TRACK_PACKETS
790 cp->flags &= ~RX_PKTFLAG_CP;
793 call->currentPacket = NULL;
795 MUTEX_EXIT(&call->lock);
798 MUTEX_EXIT(&call->lock);
801 if (cp && (int)call->nFree < nbytes) {
802 /* Try to extend the current buffer */
805 mud = rx_MaxUserDataSize(call);
808 want = MIN(nbytes - (int)call->nFree, mud - len);
809 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
810 if (cp->length > (unsigned)mud)
812 call->nFree += (cp->length - len);
816 /* If the remaining bytes fit in the buffer, then store them
817 * and return. Don't ship a buffer that's full immediately to
818 * the peer--we don't know if it's the last buffer yet */
824 while (nbytes && call->nFree) {
826 t = MIN((int)call->curlen, nbytes);
827 t = MIN((int)call->nFree, t);
828 memcpy(call->curpos, buf, t);
832 call->curlen -= (u_short)t;
833 call->nFree -= (u_short)t;
836 /* need to get another struct iov */
837 if (++call->curvec >= cp->niovecs) {
838 /* current packet is full, extend or send it */
841 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
842 call->curlen = cp->wirevec[call->curvec].iov_len;
845 } /* while bytes to send and room to send them */
847 /* might be out of space now */
850 } else; /* more data to send, so get another packet and keep going */
853 return requestCount - nbytes;
857 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
865 /* Free any packets from the last call to ReadvProc/WritevProc */
866 if (queue_IsNotEmpty(&call->iovq)) {
867 #ifdef RXDEBUG_PACKET
869 #endif /* RXDEBUG_PACKET */
870 rxi_FreePackets(0, &call->iovq);
874 * Most common case: all of the data fits in the current iovec.
875 * We are relying on nFree being zero unless the call is in send mode.
877 tcurlen = (int)call->curlen;
878 tnFree = (int)call->nFree;
879 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
880 tcurpos = call->curpos;
882 memcpy(tcurpos, buf, nbytes);
883 call->curpos = tcurpos + nbytes;
884 call->curlen = (u_short)(tcurlen - nbytes);
885 call->nFree = (u_short)(tnFree - nbytes);
890 bytes = rxi_WriteProc(call, buf, nbytes);
895 /* Optimization for marshalling 32 bit arguments */
897 rx_WriteProc32(struct rx_call *call, afs_int32 * value)
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;
922 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
923 *((afs_int32 *) (tcurpos)) = *value;
925 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
927 call->curpos = tcurpos + sizeof(afs_int32);
928 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
929 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
930 return sizeof(afs_int32);
934 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
939 /* rxi_WritevAlloc -- internal version.
941 * Fill in an iovec to point to data in packet buffers. The application
942 * calls rxi_WritevProc when the buffers are full.
944 * LOCKS USED -- called at netpri.
948 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
951 struct rx_connection *conn = call->conn;
952 struct rx_packet *cp = call->currentPacket;
955 /* Temporary values, real work is done in rxi_WritevProc */
957 unsigned int tcurvec;
961 requestCount = nbytes;
964 /* Free any packets from the last call to ReadvProc/WritevProc */
965 if (queue_IsNotEmpty(&call->iovq)) {
966 #ifdef RXDEBUG_PACKET
968 #endif /* RXDEBUG_PACKET */
969 rxi_FreePackets(0, &call->iovq);
972 if (call->mode != RX_MODE_SENDING) {
973 if ((conn->type == RX_SERVER_CONNECTION)
974 && (call->mode == RX_MODE_RECEIVING)) {
975 call->mode = RX_MODE_SENDING;
977 #ifdef RX_TRACK_PACKETS
978 cp->flags &= ~RX_PKTFLAG_CP;
981 cp = call->currentPacket = (struct rx_packet *)0;
990 /* Set up the iovec to point to data in packet buffers. */
991 tnFree = call->nFree;
992 tcurvec = call->curvec;
993 tcurpos = call->curpos;
994 tcurlen = call->curlen;
999 /* current packet is full, allocate a new one */
1000 MUTEX_ENTER(&call->lock);
1001 cp = rxi_AllocSendPacket(call, nbytes);
1002 MUTEX_EXIT(&call->lock);
1004 /* out of space, return what we have */
1006 return requestCount - nbytes;
1008 #ifdef RX_TRACK_PACKETS
1009 cp->flags |= RX_PKTFLAG_IOVQ;
1011 queue_Append(&call->iovq, cp);
1012 #ifdef RXDEBUG_PACKET
1014 #endif /* RXDEBUG_PACKET */
1015 tnFree = cp->length;
1018 (char *)cp->wirevec[1].iov_base +
1019 call->conn->securityHeaderSize;
1020 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1023 if (tnFree < nbytes) {
1024 /* try to extend the current packet */
1027 mud = rx_MaxUserDataSize(call);
1030 want = MIN(nbytes - tnFree, mud - len);
1031 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1032 if (cp->length > (unsigned)mud)
1034 tnFree += (cp->length - len);
1035 if (cp == call->currentPacket) {
1036 call->nFree += (cp->length - len);
1041 /* fill in the next entry in the iovec */
1042 t = MIN(tcurlen, nbytes);
1044 iov[nextio].iov_base = tcurpos;
1045 iov[nextio].iov_len = t;
1053 /* need to get another struct iov */
1054 if (++tcurvec >= cp->niovecs) {
1055 /* current packet is full, extend it or move on to next packet */
1058 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1059 tcurlen = cp->wirevec[tcurvec].iov_len;
1062 } while (nbytes && nextio < maxio);
1064 return requestCount - nbytes;
1068 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1075 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1080 /* rxi_WritevProc -- internal version.
1082 * Send buffers allocated in rxi_WritevAlloc.
1084 * LOCKS USED -- called at netpri.
1087 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1089 struct rx_packet *cp = NULL;
1090 #ifdef RX_TRACK_PACKETS
1091 struct rx_packet *p, *np;
1095 struct rx_queue tmpq;
1096 #ifdef RXDEBUG_PACKET
1100 requestCount = nbytes;
1103 MUTEX_ENTER(&call->lock);
1105 call->mode = RX_MODE_ERROR;
1106 } else if (call->mode != RX_MODE_SENDING) {
1107 call->error = RX_PROTOCOL_ERROR;
1109 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1110 rxi_WaitforTQBusy(call);
1111 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1112 cp = call->currentPacket;
1115 call->mode = RX_MODE_ERROR;
1116 MUTEX_EXIT(&call->lock);
1118 #ifdef RX_TRACK_PACKETS
1119 cp->flags &= ~RX_PKTFLAG_CP;
1120 cp->flags |= RX_PKTFLAG_IOVQ;
1122 queue_Prepend(&call->iovq, cp);
1123 #ifdef RXDEBUG_PACKET
1125 #endif /* RXDEBUG_PACKET */
1126 call->currentPacket = (struct rx_packet *)0;
1128 #ifdef RXDEBUG_PACKET
1130 #endif /* RXDEBUG_PACKET */
1131 rxi_FreePackets(0, &call->iovq);
1135 /* Loop through the I/O vector adjusting packet pointers.
1136 * Place full packets back onto the iovq once they are ready
1137 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1138 * the iovec. We put the loop condition at the end to ensure that
1139 * a zero length write will push a short packet. */
1142 #ifdef RXDEBUG_PACKET
1144 #endif /* RXDEBUG_PACKET */
1146 if (call->nFree == 0 && cp) {
1147 clock_NewTime(); /* Bogus: need new time package */
1148 /* The 0, below, specifies that it is not the last packet:
1149 * there will be others. PrepareSendPacket may
1150 * alter the packet length by up to
1151 * conn->securityMaxTrailerSize */
1152 hadd32(call->bytesSent, cp->length);
1153 rxi_PrepareSendPacket(call, cp, 0);
1154 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1155 /* PrepareSendPacket drops the call lock */
1156 rxi_WaitforTQBusy(call);
1157 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1158 queue_Append(&tmpq, cp);
1159 #ifdef RXDEBUG_PACKET
1161 #endif /* RXDEBUG_PACKET */
1162 cp = call->currentPacket = (struct rx_packet *)0;
1164 /* The head of the iovq is now the current packet */
1166 if (queue_IsEmpty(&call->iovq)) {
1167 MUTEX_EXIT(&call->lock);
1168 call->error = RX_PROTOCOL_ERROR;
1169 #ifdef RXDEBUG_PACKET
1171 #endif /* RXDEBUG_PACKET */
1172 rxi_FreePackets(0, &tmpq);
1175 cp = queue_First(&call->iovq, rx_packet);
1177 #ifdef RX_TRACK_PACKETS
1178 cp->flags &= ~RX_PKTFLAG_IOVQ;
1180 #ifdef RXDEBUG_PACKET
1182 #endif /* RXDEBUG_PACKET */
1183 #ifdef RX_TRACK_PACKETS
1184 cp->flags |= RX_PKTFLAG_CP;
1186 call->currentPacket = cp;
1187 call->nFree = cp->length;
1190 (char *)cp->wirevec[1].iov_base +
1191 call->conn->securityHeaderSize;
1193 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1198 /* The next iovec should point to the current position */
1199 if (iov[nextio].iov_base != call->curpos
1200 || iov[nextio].iov_len > (int)call->curlen) {
1201 call->error = RX_PROTOCOL_ERROR;
1202 MUTEX_EXIT(&call->lock);
1204 #ifdef RX_TRACK_PACKETS
1205 cp->flags &= ~RX_PKTFLAG_CP;
1207 queue_Prepend(&tmpq, cp);
1208 #ifdef RXDEBUG_PACKET
1210 #endif /* RXDEBUG_PACKET */
1211 cp = call->currentPacket = (struct rx_packet *)0;
1213 #ifdef RXDEBUG_PACKET
1215 #endif /* RXDEBUG_PACKET */
1216 rxi_FreePackets(0, &tmpq);
1219 nbytes -= iov[nextio].iov_len;
1220 call->curpos += iov[nextio].iov_len;
1221 call->curlen -= iov[nextio].iov_len;
1222 call->nFree -= iov[nextio].iov_len;
1224 if (call->curlen == 0) {
1225 if (++call->curvec > cp->niovecs) {
1228 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1229 call->curlen = cp->wirevec[call->curvec].iov_len;
1233 } while (nbytes && nextio < nio);
1235 /* Move the packets from the temporary queue onto the transmit queue.
1236 * We may end up with more than call->twind packets on the queue. */
1238 #ifdef RX_TRACK_PACKETS
1239 for (queue_Scan(&tmpq, p, np, rx_packet))
1241 p->flags |= RX_PKTFLAG_TQ;
1246 call->mode = RX_MODE_ERROR;
1248 queue_SpliceAppend(&call->tq, &tmpq);
1250 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1251 rxi_Start(0, call, 0, 0);
1254 /* Wait for the length of the transmit queue to fall below call->twind */
1255 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1257 call->startWait = clock_Sec();
1258 #ifdef RX_ENABLE_LOCKS
1259 CV_WAIT(&call->cv_twind, &call->lock);
1261 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1262 osi_rxSleep(&call->twind);
1264 call->startWait = 0;
1267 /* cp is no longer valid since we may have given up the lock */
1268 cp = call->currentPacket;
1271 call->mode = RX_MODE_ERROR;
1272 call->currentPacket = NULL;
1273 MUTEX_EXIT(&call->lock);
1275 #ifdef RX_TRACK_PACKETS
1276 cp->flags &= ~RX_PKTFLAG_CP;
1282 MUTEX_EXIT(&call->lock);
1284 return requestCount - nbytes;
1288 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1294 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1299 /* Flush any buffered data to the stream, switch to read mode
1300 * (clients) or to EOF mode (servers)
1302 * LOCKS HELD: called at netpri.
1305 rxi_FlushWrite(struct rx_call *call)
1307 struct rx_packet *cp = NULL;
1309 /* Free any packets from the last call to ReadvProc/WritevProc */
1310 if (queue_IsNotEmpty(&call->iovq)) {
1311 #ifdef RXDEBUG_PACKET
1313 #endif /* RXDEBUG_PACKET */
1314 rxi_FreePackets(0, &call->iovq);
1317 if (call->mode == RX_MODE_SENDING) {
1320 (call->conn->type ==
1321 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1323 #ifdef RX_KERNEL_TRACE
1325 int glockOwner = ISAFS_GLOCK();
1328 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1329 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1336 MUTEX_ENTER(&call->lock);
1338 call->mode = RX_MODE_ERROR;
1340 cp = call->currentPacket;
1343 /* cp->length is only supposed to be the user's data */
1344 /* cp->length was already set to (then-current)
1345 * MaxUserDataSize or less. */
1346 #ifdef RX_TRACK_PACKETS
1347 cp->flags &= ~RX_PKTFLAG_CP;
1349 cp->length -= call->nFree;
1350 call->currentPacket = (struct rx_packet *)0;
1353 cp = rxi_AllocSendPacket(call, 0);
1355 /* Mode can no longer be MODE_SENDING */
1359 cp->niovecs = 2; /* header + space for rxkad stuff */
1363 /* The 1 specifies that this is the last packet */
1364 hadd32(call->bytesSent, cp->length);
1365 rxi_PrepareSendPacket(call, cp, 1);
1366 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1367 /* PrepareSendPacket drops the call lock */
1368 rxi_WaitforTQBusy(call);
1369 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1370 #ifdef RX_TRACK_PACKETS
1371 cp->flags |= RX_PKTFLAG_TQ;
1373 queue_Append(&call->tq, cp);
1374 #ifdef RXDEBUG_PACKET
1376 #endif /* RXDEBUG_PACKET */
1379 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1380 rxi_Start(0, call, 0, 0);
1382 MUTEX_EXIT(&call->lock);
1386 /* Flush any buffered data to the stream, switch to read mode
1387 * (clients) or to EOF mode (servers) */
1389 rx_FlushWrite(struct rx_call *call)
1393 rxi_FlushWrite(call);