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>
21 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
22 #include "../afs/sysincludes.h"
24 #include "../h/types.h"
25 #include "../h/time.h"
26 #include "../h/stat.h"
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 #include "../afs/afs_osi.h"
40 #if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
41 #include "../h/systm.h"
44 #include "../afs/sysincludes.h"
47 #undef RXDEBUG /* turn off debugging */
50 #include "../rx/rx_kmutex.h"
51 #include "../rx/rx_kernel.h"
52 #include "../rx/rx_clock.h"
53 #include "../rx/rx_queue.h"
55 #include "../rx/rx_globals.h"
56 #include "../afs/lock.h"
57 #include "../afsint/afsint.h"
64 #endif /* AFS_ALPHA_ENV */
66 # include <sys/types.h>
68 # include <sys/socket.h>
69 # include <sys/file.h>
71 # include <netinet/in.h>
72 # include <sys/stat.h>
73 # include <sys/time.h>
86 # include "rx_clock.h"
87 # include "rx_queue.h"
89 # include "rx_globals.h"
93 /* rxdb_fileID is used to identify the lock location, along with line#. */
94 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
95 #endif /* RX_LOCKS_DB */
96 /* rxi_ReadProc -- internal version.
98 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
100 int rxi_ReadProc(register struct rx_call *call, register char *buf,
103 register struct rx_packet *cp = call->currentPacket;
104 register struct rx_packet *rp;
105 register struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
106 register int requestCount;
107 register unsigned int t;
108 /* XXXX took out clock_NewTime from here. Was it needed? */
109 requestCount = nbytes;
111 /* Free any packets from the last call to ReadvProc/WritevProc */
112 if (!queue_IsEmpty(&call->iovq)) {
113 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
120 if (call->nLeft == 0) {
121 /* Get next packet */
123 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
127 if (call->mode == RX_MODE_SENDING) {
128 rxi_FlushWrite(call);
132 if (queue_IsNotEmpty(&call->rq)) {
133 /* Check that next packet available is next in sequence */
134 rp = queue_First(&call->rq, rx_packet);
135 if (rp->header.seq == call->rnext) {
137 register struct rx_connection *conn = call->conn;
140 /* RXS_CheckPacket called to undo RXS_PreparePacket's
141 * work. It may reduce the length of the packet by up
142 * to conn->maxTrailerSize, to reflect the length of the
143 * data + the header. */
144 if ((error = RXS_CheckPacket(conn->securityObject, call, rp))) {
145 /* Used to merely shut down the call, but now we
146 * shut down the whole connection since this may
147 * indicate an attempt to hijack it */
149 MUTEX_EXIT(&call->lock);
150 rxi_ConnectionError(conn, error);
151 MUTEX_ENTER(&conn->conn_data_lock);
152 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
153 MUTEX_EXIT(&conn->conn_data_lock);
155 MUTEX_ENTER(&call->lock);
160 cp = call->currentPacket = rp;
161 call->curvec = 1; /* 0th vec is always header */
162 /* begin at the beginning [ more or less ], continue
163 * on until the end, then stop. */
164 call->curpos = (char *)cp->wirevec[1].iov_base
165 + call->conn->securityHeaderSize;
166 call->curlen = cp->wirevec[1].iov_len
167 - call->conn->securityHeaderSize;
169 /* Notice that this code works correctly if the data
170 * size is 0 (which it may be--no reply arguments from
171 * server, for example). This relies heavily on the
172 * fact that the code below immediately frees the packet
173 * (no yields, etc.). If it didn't, this would be a
174 * problem because a value of zero for call->nLeft
175 * normally means that there is no read packet */
176 call->nLeft = cp->length;
177 hadd32(call->bytesRcvd, cp->length);
179 /* Send a hard ack for every rxi_HardAckRate+1 packets
180 * consumed. Otherwise schedule an event to send
181 * the hard ack later on.
184 if (!(call->flags &RX_CALL_RECEIVE_DONE)) {
185 if (call->nHardAcks > (u_short)rxi_HardAckRate) {
186 rxevent_Cancel(call->delayedAckEvent, call,
187 RX_CALL_REFCOUNT_DELAY);
188 rxi_SendAck(call, 0, 0, 0, 0, RX_ACK_DELAY, 0);
192 clock_GetTime(&when);
193 /* Delay to consolidate ack packets */
194 clock_Add(&when, &rx_hardAckDelay);
195 if (!call->delayedAckEvent ||
196 clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
197 rxevent_Cancel(call->delayedAckEvent, call,
198 RX_CALL_REFCOUNT_DELAY);
199 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
200 call->delayedAckEvent = rxevent_Post(&when,
211 MTUXXX doesn't there need to be an "else" here ???
213 /* Are there ever going to be any more packets? */
214 if (call->flags & RX_CALL_RECEIVE_DONE) {
215 return requestCount - nbytes;
217 /* Wait for in-sequence packet */
218 call->flags |= RX_CALL_READER_WAIT;
220 call->startWait = clock_Sec();
221 while (call->flags & RX_CALL_READER_WAIT) {
222 #ifdef RX_ENABLE_LOCKS
223 CV_WAIT(&call->cv_rq, &call->lock);
225 osi_rxSleep(&call->rq);
230 #ifdef RX_ENABLE_LOCKS
234 #endif /* RX_ENABLE_LOCKS */
237 else /* assert(cp); */ /* MTUXXX this should be replaced by some error-recovery code before shipping */
238 /* yes, the following block is allowed to be the ELSE clause (or not) */
240 /* It's possible for call->nLeft to be smaller than any particular
241 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
242 * reflects the size of the buffer. We have to keep track of the
243 * number of bytes read in the length field of the packet struct. On
244 * the final portion of a received packet, it's almost certain that
245 * call->nLeft will be smaller than the final buffer. */
247 while (nbytes && cp) {
248 t = MIN((int)call->curlen, nbytes);
249 t = MIN(t, (int)call->nLeft);
250 memcpy(buf, call->curpos, t);
258 /* out of packet. Get another one. */
260 cp = call->currentPacket = (struct rx_packet *)0;
262 else if (!call->curlen) {
263 /* need to get another struct iov */
264 if (++call->curvec >= cp->niovecs) {
265 /* current packet is exhausted, get ready for another */
266 /* don't worry about curvec and stuff, they get set somewhere else */
268 cp = call->currentPacket = (struct rx_packet *)0;
272 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
273 call->curlen = cp->wirevec[call->curvec].iov_len;
278 /* user buffer is full, return */
287 int rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
296 * Free any packets from the last call to ReadvProc/WritevProc.
297 * We do not need the lock because the receiver threads only
298 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
299 * RX_CALL_IOVEC_WAIT is always cleared before returning from
300 * ReadvProc/WritevProc.
302 if (!queue_IsEmpty(&call->iovq)) {
303 register struct rx_packet *rp;
304 register struct rx_packet *nxp;
305 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
312 * Most common case, all of the data is in the current iovec.
313 * We do not need the lock because this is the only thread that
314 * updates the curlen, curpos, nLeft fields.
316 * We are relying on nLeft being zero unless the call is in receive mode.
318 tcurlen = call->curlen;
319 tnLeft = call->nLeft;
320 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
321 tcurpos = call->curpos;
322 memcpy(buf, tcurpos, nbytes);
323 call->curpos = tcurpos + nbytes;
324 call->curlen = tcurlen - nbytes;
325 call->nLeft = tnLeft - nbytes;
331 MUTEX_ENTER(&call->lock);
332 bytes = rxi_ReadProc(call, buf, nbytes);
333 MUTEX_EXIT(&call->lock);
339 /* Optimization for unmarshalling 32 bit integers */
340 int rx_ReadProc32(struct rx_call *call, afs_int32 *value)
349 * Free any packets from the last call to ReadvProc/WritevProc.
350 * We do not need the lock because the receiver threads only
351 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
352 * RX_CALL_IOVEC_WAIT is always cleared before returning from
353 * ReadvProc/WritevProc.
355 if (!queue_IsEmpty(&call->iovq)) {
356 register struct rx_packet *rp;
357 register struct rx_packet *nxp;
358 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
365 * Most common case, all of the data is in the current iovec.
366 * We do not need the lock because this is the only thread that
367 * updates the curlen, curpos, nLeft fields.
369 * We are relying on nLeft being zero unless the call is in receive mode.
371 tcurlen = call->curlen;
372 tnLeft = call->nLeft;
373 if (!call->error && tcurlen > sizeof(afs_int32) && tnLeft > sizeof(afs_int32)) {
374 tcurpos = call->curpos;
375 if (!((long)tcurpos & (sizeof(afs_int32)-1))) {
376 *value = *((afs_int32 *)(tcurpos));
378 memcpy((char *)value, tcurpos, sizeof(afs_int32));
380 call->curpos = tcurpos + sizeof(afs_int32);
381 call->curlen = tcurlen - sizeof(afs_int32);
382 call->nLeft = tnLeft - sizeof(afs_int32);
383 return sizeof(afs_int32);
388 MUTEX_ENTER(&call->lock);
389 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
390 MUTEX_EXIT(&call->lock);
398 * Uses packets in the receive queue to fill in as much of the
399 * current iovec as possible. Does not block if it runs out
400 * of packets to complete the iovec. Return true if an ack packet
401 * was sent, otherwise return false */
402 int rxi_FillReadVec(struct rx_call *call, afs_uint32 seq,
403 afs_uint32 serial, afs_uint32 flags)
407 register unsigned int t;
408 struct rx_packet *rp;
409 struct rx_packet *curp;
410 struct iovec *call_iov;
411 struct iovec *cur_iov = NULL;
413 curp = call->currentPacket;
415 cur_iov = &curp->wirevec[call->curvec];
417 call_iov = &call->iov[call->iovNext];
419 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
420 if (call->nLeft == 0) {
421 /* Get next packet */
422 if (queue_IsNotEmpty(&call->rq)) {
423 /* Check that next packet available is next in sequence */
424 rp = queue_First(&call->rq, rx_packet);
425 if (rp->header.seq == call->rnext) {
427 register struct rx_connection *conn = call->conn;
430 /* RXS_CheckPacket called to undo RXS_PreparePacket's
431 * work. It may reduce the length of the packet by up
432 * to conn->maxTrailerSize, to reflect the length of the
433 * data + the header. */
434 if ((error = RXS_CheckPacket(conn->securityObject, call, rp))) {
435 /* Used to merely shut down the call, but now we
436 * shut down the whole connection since this may
437 * indicate an attempt to hijack it */
439 MUTEX_EXIT(&call->lock);
440 rxi_ConnectionError(conn, error);
441 MUTEX_ENTER(&conn->conn_data_lock);
442 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
443 MUTEX_EXIT(&conn->conn_data_lock);
445 MUTEX_ENTER(&call->lock);
450 curp = call->currentPacket = rp;
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. */
455 call->curpos = (char *)curp->wirevec[1].iov_base
456 + call->conn->securityHeaderSize;
457 call->curlen = curp->wirevec[1].iov_len
458 - call->conn->securityHeaderSize;
460 /* Notice that this code works correctly if the data
461 * size is 0 (which it may be--no reply arguments from
462 * server, for example). This relies heavily on the
463 * fact that the code below immediately frees the packet
464 * (no yields, etc.). If it didn't, this would be a
465 * problem because a value of zero for call->nLeft
466 * normally means that there is no read packet */
467 call->nLeft = curp->length;
468 hadd32(call->bytesRcvd, curp->length);
470 /* Send a hard ack for every rxi_HardAckRate+1 packets
471 * consumed. Otherwise schedule an event to send
472 * the hard ack later on.
482 /* It's possible for call->nLeft to be smaller than any particular
483 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
484 * reflects the size of the buffer. We have to keep track of the
485 * number of bytes read in the length field of the packet struct. On
486 * the final portion of a received packet, it's almost certain that
487 * call->nLeft will be smaller than the final buffer. */
488 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
490 t = MIN((int)call->curlen, call->iovNBytes);
491 t = MIN(t, (int)call->nLeft);
492 call_iov->iov_base = call->curpos;
493 call_iov->iov_len = t;
496 call->iovNBytes -= t;
502 /* out of packet. Get another one. */
503 queue_Append(&call->iovq, curp);
504 curp = call->currentPacket = (struct rx_packet *)0;
506 else if (!call->curlen) {
507 /* need to get another struct iov */
508 if (++call->curvec >= curp->niovecs) {
509 /* current packet is exhausted, get ready for another */
510 /* don't worry about curvec and stuff, they get set somewhere else */
511 queue_Append(&call->iovq, curp);
512 curp = call->currentPacket = (struct rx_packet *)0;
517 call->curpos = (char *)cur_iov->iov_base;
518 call->curlen = cur_iov->iov_len;
524 /* If we consumed any packets then check whether we need to
525 * send a hard ack. */
526 if (didConsume && (!(call->flags &RX_CALL_RECEIVE_DONE))) {
527 if (call->nHardAcks > (u_short)rxi_HardAckRate) {
528 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
529 rxi_SendAck(call, 0, seq, serial, flags, RX_ACK_DELAY, 0);
534 clock_GetTime(&when);
535 /* Delay to consolidate ack packets */
536 clock_Add(&when, &rx_hardAckDelay);
537 if (!call->delayedAckEvent ||
538 clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
539 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
540 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
541 call->delayedAckEvent = rxevent_Post(&when, rxi_SendDelayedAck,
550 /* rxi_ReadvProc -- internal version.
552 * Fills in an iovec with pointers to the packet buffers. All packets
553 * except the last packet (new current packet) are moved to the iovq
554 * while the application is processing the data.
556 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
558 int rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio,
559 int maxio, int nbytes)
561 struct rx_packet *rp;
562 struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
566 requestCount = nbytes;
569 /* Free any packets from the last call to ReadvProc/WritevProc */
570 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
575 if (call->mode == RX_MODE_SENDING) {
576 rxi_FlushWrite(call);
583 /* Get whatever data is currently available in the receive queue.
584 * If rxi_FillReadVec sends an ack packet then it is possible
585 * that we will receive more data while we drop the call lock
586 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
587 * here to avoid a race with the receive thread if we send
588 * hard acks in rxi_FillReadVec. */
589 call->flags |= RX_CALL_IOVEC_WAIT;
590 call->iovNBytes = nbytes;
591 call->iovMax = maxio;
594 rxi_FillReadVec(call, 0, 0, 0);
596 /* if we need more data then sleep until the receive thread has
597 * filled in the rest. */
598 if (!call->error && call->iovNBytes &&
599 call->iovNext < call->iovMax &&
600 !(call->flags & RX_CALL_RECEIVE_DONE)) {
601 call->flags |= RX_CALL_READER_WAIT;
603 call->startWait = clock_Sec();
604 while (call->flags & RX_CALL_READER_WAIT) {
605 #ifdef RX_ENABLE_LOCKS
606 CV_WAIT(&call->cv_rq, &call->lock);
608 osi_rxSleep(&call->rq);
613 call->flags &= ~RX_CALL_IOVEC_WAIT;
614 #ifdef RX_ENABLE_LOCKS
618 #endif /* RX_ENABLE_LOCKS */
621 *nio = call->iovNext;
622 return nbytes - call->iovNBytes;
625 int rx_ReadvProc(struct rx_call *call, struct iovec *iov,
626 int *nio, int maxio, int nbytes)
633 MUTEX_ENTER(&call->lock);
634 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
635 MUTEX_EXIT(&call->lock);
641 /* rxi_WriteProc -- internal version.
643 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
645 int rxi_WriteProc(register struct rx_call *call, register char *buf,
648 struct rx_connection *conn = call->conn;
649 register struct rx_packet *cp = call->currentPacket;
650 register struct rx_packet *tp; /* Temporary packet pointer */
651 register struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
652 register unsigned int t;
653 int requestCount = nbytes;
655 /* Free any packets from the last call to ReadvProc/WritevProc */
656 if (!queue_IsEmpty(&call->iovq)) {
657 for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
663 if (call->mode != RX_MODE_SENDING) {
664 if ((conn->type == RX_SERVER_CONNECTION)
665 && (call->mode == RX_MODE_RECEIVING)) {
666 call->mode = RX_MODE_SENDING;
669 cp = call->currentPacket = (struct rx_packet *) 0;
679 /* Loop condition is checked at end, so that a write of 0 bytes
680 * will force a packet to be created--specially for the case where
681 * there are 0 bytes on the stream, but we must send a packet
684 if (call->nFree == 0) {
685 if (!call->error && cp) {
686 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
687 /* Wait until TQ_BUSY is reset before adding any
688 * packets to the transmit queue
690 while (call->flags & RX_CALL_TQ_BUSY) {
691 call->flags |= RX_CALL_TQ_WAIT;
692 #ifdef RX_ENABLE_LOCKS
693 CV_WAIT(&call->cv_tq, &call->lock);
694 #else /* RX_ENABLE_LOCKS */
695 osi_rxSleep(&call->tq);
696 #endif /* RX_ENABLE_LOCKS */
698 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
699 clock_NewTime(); /* Bogus: need new time package */
700 /* The 0, below, specifies that it is not the last packet:
701 * there will be others. PrepareSendPacket may
702 * alter the packet length by up to
703 * conn->securityMaxTrailerSize */
704 hadd32(call->bytesSent, cp->length);
705 rxi_PrepareSendPacket(call, cp, 0);
706 queue_Append(&call->tq, cp);
707 cp = call->currentPacket = NULL;
708 if (!(call->flags & (RX_CALL_FAST_RECOVER|
709 RX_CALL_FAST_RECOVER_WAIT))) {
710 rxi_Start(0, call, 0);
713 /* Wait for transmit window to open up */
714 while (!call->error && call->tnext + 1 > call->tfirst + call->twind) {
716 call->startWait = clock_Sec();
718 #ifdef RX_ENABLE_LOCKS
719 CV_WAIT(&call->cv_twind, &call->lock);
721 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
722 osi_rxSleep(&call->twind);
726 #ifdef RX_ENABLE_LOCKS
730 #endif /* RX_ENABLE_LOCKS */
732 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
733 call->currentPacket = cp;
734 call->nFree = cp->length;
735 call->curvec = 1; /* 0th vec is always header */
736 /* begin at the beginning [ more or less ], continue
737 * on until the end, then stop. */
738 call->curpos = (char *)cp->wirevec[1].iov_base
739 + call->conn->securityHeaderSize;
740 call->curlen = cp->wirevec[1].iov_len
741 - call->conn->securityHeaderSize;
746 call->currentPacket = NULL;
752 if (cp && (int)call->nFree < nbytes) {
753 /* Try to extend the current buffer */
754 register int len, mud;
756 mud = rx_MaxUserDataSize(call);
759 want = MIN(nbytes - (int)call->nFree, mud - len);
760 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
761 if (cp->length > (unsigned)mud)
763 call->nFree += (cp->length - len);
767 /* If the remaining bytes fit in the buffer, then store them
768 * and return. Don't ship a buffer that's full immediately to
769 * the peer--we don't know if it's the last buffer yet */
775 while (nbytes && call->nFree) {
777 t = MIN((int)call->curlen, nbytes);
778 t = MIN((int)call->nFree, t);
779 memcpy(call->curpos, buf, t);
787 /* need to get another struct iov */
788 if (++call->curvec >= cp->niovecs) {
789 /* current packet is full, extend or send it */
792 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
793 call->curlen = cp->wirevec[call->curvec].iov_len;
796 } /* while bytes to send and room to send them */
798 /* might be out of space now */
802 else ; /* more data to send, so get another packet and keep going */
805 return requestCount - nbytes;
808 int rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
817 * Free any packets from the last call to ReadvProc/WritevProc.
818 * We do not need the lock because the receiver threads only
819 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
820 * RX_CALL_IOVEC_WAIT is always cleared before returning from
821 * ReadvProc/WritevProc.
823 if (!queue_IsEmpty(&call->iovq)) {
824 register struct rx_packet *rp;
825 register struct rx_packet *nxp;
826 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
833 * Most common case: all of the data fits in the current iovec.
834 * We do not need the lock because this is the only thread that
835 * updates the curlen, curpos, nFree fields.
837 * We are relying on nFree being zero unless the call is in send mode.
839 tcurlen = (int)call->curlen;
840 tnFree = (int)call->nFree;
841 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
842 tcurpos = call->curpos;
843 memcpy(tcurpos, buf, nbytes);
844 call->curpos = tcurpos + nbytes;
845 call->curlen = tcurlen - nbytes;
846 call->nFree = tnFree - nbytes;
852 MUTEX_ENTER(&call->lock);
853 bytes = rxi_WriteProc(call, buf, nbytes);
854 MUTEX_EXIT(&call->lock);
860 /* Optimization for marshalling 32 bit arguments */
861 int rx_WriteProc32(register struct rx_call *call, register afs_int32 *value)
870 * Free any packets from the last call to ReadvProc/WritevProc.
871 * We do not need the lock because the receiver threads only
872 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
873 * RX_CALL_IOVEC_WAIT is always cleared before returning from
874 * ReadvProc/WritevProc.
876 if (!queue_IsEmpty(&call->iovq)) {
877 register struct rx_packet *rp;
878 register struct rx_packet *nxp;
879 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
886 * Most common case: all of the data fits in the current iovec.
887 * We do not need the lock because this is the only thread that
888 * updates the curlen, curpos, nFree fields.
890 * We are relying on nFree being zero unless the call is in send mode.
892 tcurlen = (int)call->curlen;
893 tnFree = (int)call->nFree;
894 if (!call->error && tcurlen >= sizeof(afs_int32) && tnFree >= sizeof(afs_int32)) {
895 tcurpos = call->curpos;
896 if (!((long)tcurpos & (sizeof(afs_int32)-1))) {
897 *((afs_int32 *)(tcurpos)) = *value;
899 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
901 call->curpos = tcurpos + sizeof(afs_int32);
902 call->curlen = tcurlen - sizeof(afs_int32);
903 call->nFree = tnFree - sizeof(afs_int32);
904 return sizeof(afs_int32);
909 MUTEX_ENTER(&call->lock);
910 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
911 MUTEX_EXIT(&call->lock);
917 /* rxi_WritevAlloc -- internal version.
919 * Fill in an iovec to point to data in packet buffers. The application
920 * calls rxi_WritevProc when the buffers are full.
922 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
924 int rxi_WritevAlloc(struct rx_call *call, struct iovec *iov,
925 int *nio, int maxio, int nbytes)
927 struct rx_connection *conn = call->conn;
928 struct rx_packet *cp = call->currentPacket;
929 struct rx_packet *tp; /* temporary packet pointer */
930 struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
933 /* Temporary values, real work is done in rxi_WritevProc */
939 requestCount = nbytes;
942 /* Free any packets from the last call to ReadvProc/WritevProc */
943 for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
948 if (call->mode != RX_MODE_SENDING) {
949 if ((conn->type == RX_SERVER_CONNECTION)
950 && (call->mode == RX_MODE_RECEIVING)) {
951 call->mode = RX_MODE_SENDING;
954 cp = call->currentPacket = (struct rx_packet *) 0;
964 /* Set up the iovec to point to data in packet buffers. */
965 tnFree = call->nFree;
966 tcurvec = call->curvec;
967 tcurpos = call->curpos;
968 tcurlen = call->curlen;
970 register unsigned int t;
973 /* current packet is full, allocate a new one */
974 cp = rxi_AllocSendPacket(call, nbytes);
976 /* out of space, return what we have */
978 return requestCount - nbytes;
980 queue_Append(&call->iovq, cp);
983 tcurpos = (char *)cp->wirevec[1].iov_base
984 + call->conn->securityHeaderSize;
985 tcurlen = cp->wirevec[1].iov_len
986 - call->conn->securityHeaderSize;
989 if (tnFree < nbytes) {
990 /* try to extend the current packet */
991 register int len, mud;
993 mud = rx_MaxUserDataSize(call);
996 want = MIN(nbytes - tnFree, mud - len);
997 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
998 if (cp->length > (unsigned)mud)
1000 tnFree += (cp->length - len);
1001 if (cp == call->currentPacket) {
1002 call->nFree += (cp->length - len);
1007 /* fill in the next entry in the iovec */
1008 t = MIN(tcurlen, nbytes);
1010 iov[nextio].iov_base = tcurpos;
1011 iov[nextio].iov_len = t;
1019 /* need to get another struct iov */
1020 if (++tcurvec >= cp->niovecs) {
1021 /* current packet is full, extend it or move on to next packet */
1024 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1025 tcurlen = cp->wirevec[tcurvec].iov_len;
1028 } while (nbytes && nextio < maxio);
1030 return requestCount - nbytes;
1033 int rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio,
1034 int maxio, int nbytes)
1041 MUTEX_ENTER(&call->lock);
1042 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1043 MUTEX_EXIT(&call->lock);
1049 int rx_WritevInit(struct rx_call *call)
1055 * Free any packets from the last call to ReadvProc/WritevProc.
1056 * We do not need the lock because the receiver threads only
1057 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
1058 * RX_CALL_IOVEC_WAIT is always cleared before returning from
1059 * ReadvProc/WritevProc.
1061 if (!queue_IsEmpty(&call->iovq)) {
1062 register struct rx_packet *rp;
1063 register struct rx_packet *nxp;
1064 for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
1072 MUTEX_ENTER(&call->lock);
1073 bytes = rxi_WriteProc(call, &bytes, 0);
1074 MUTEX_EXIT(&call->lock);
1080 /* rxi_WritevProc -- internal version.
1082 * Send buffers allocated in rxi_WritevAlloc.
1084 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1086 int rxi_WritevProc(struct rx_call *call, struct iovec *iov,
1087 int nio, int nbytes)
1089 struct rx_packet *cp = call->currentPacket;
1090 register struct rx_packet *tp; /* Temporary packet pointer */
1091 register struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
1094 struct rx_queue tmpq;
1096 requestCount = nbytes;
1099 if (call->mode != RX_MODE_SENDING) {
1100 call->error = RX_PROTOCOL_ERROR;
1103 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1104 /* Wait until TQ_BUSY is reset before trying to move any
1105 * packets to the transmit queue. */
1106 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1107 call->flags |= RX_CALL_TQ_WAIT;
1108 #ifdef RX_ENABLE_LOCKS
1109 CV_WAIT(&call->cv_tq, &call->lock);
1110 #else /* RX_ENABLE_LOCKS */
1111 osi_rxSleep(&call->tq);
1112 #endif /* RX_ENABLE_LOCKS */
1114 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1117 for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
1123 cp = call->currentPacket = NULL;
1128 /* Loop through the I/O vector adjusting packet pointers.
1129 * Place full packets back onto the iovq once they are ready
1130 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1131 * the iovec. We put the loop condition at the end to ensure that
1132 * a zero length write will push a short packet. */
1136 if (call->nFree == 0 && cp) {
1137 clock_NewTime(); /* Bogus: need new time package */
1138 /* The 0, below, specifies that it is not the last packet:
1139 * there will be others. PrepareSendPacket may
1140 * alter the packet length by up to
1141 * conn->securityMaxTrailerSize */
1142 hadd32(call->bytesSent, cp->length);
1143 rxi_PrepareSendPacket(call, cp, 0);
1144 queue_Append(&tmpq, cp);
1146 /* The head of the iovq is now the current packet */
1148 if (queue_IsEmpty(&call->iovq)) {
1149 call->error = RX_PROTOCOL_ERROR;
1150 cp = call->currentPacket = NULL;
1151 for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1157 cp = queue_First(&call->iovq, rx_packet);
1159 call->currentPacket = cp;
1160 call->nFree = cp->length;
1162 call->curpos = (char *)cp->wirevec[1].iov_base
1163 + call->conn->securityHeaderSize;
1164 call->curlen = cp->wirevec[1].iov_len
1165 - call->conn->securityHeaderSize;
1170 /* The next iovec should point to the current position */
1171 if (iov[nextio].iov_base != call->curpos
1172 || iov[nextio].iov_len > (int)call->curlen) {
1173 call->error = RX_PROTOCOL_ERROR;
1174 for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1180 call->currentPacket = NULL;
1184 nbytes -= iov[nextio].iov_len;
1185 call->curpos += iov[nextio].iov_len;
1186 call->curlen -= iov[nextio].iov_len;
1187 call->nFree -= iov[nextio].iov_len;
1189 if (call->curlen == 0) {
1190 if (++call->curvec > cp->niovecs) {
1193 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1194 call->curlen = cp->wirevec[call->curvec].iov_len;
1198 } while (nbytes && nextio < nio);
1200 /* Move the packets from the temporary queue onto the transmit queue.
1201 * We may end up with more than call->twind packets on the queue. */
1202 for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1204 queue_Append(&call->tq, tp);
1207 if (!(call->flags & (RX_CALL_FAST_RECOVER|RX_CALL_FAST_RECOVER_WAIT))) {
1208 rxi_Start(0, call, 0);
1211 /* Wait for the length of the transmit queue to fall below call->twind */
1212 while (!call->error && call->tnext + 1 > call->tfirst + call->twind) {
1214 call->startWait = clock_Sec();
1215 #ifdef RX_ENABLE_LOCKS
1216 CV_WAIT(&call->cv_twind, &call->lock);
1218 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1219 osi_rxSleep(&call->twind);
1221 call->startWait = 0;
1227 cp = call->currentPacket = NULL;
1232 return requestCount - nbytes;
1235 int rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1242 MUTEX_ENTER(&call->lock);
1243 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1244 MUTEX_EXIT(&call->lock);
1250 /* Flush any buffered data to the stream, switch to read mode
1251 * (clients) or to EOF mode (servers) */
1252 void rxi_FlushWrite(register struct rx_call *call)
1254 register struct rx_packet *cp = call->currentPacket;
1255 register struct rx_packet *tp; /* Temporary packet pointer */
1256 register struct rx_packet *nxp; /* Next packet pointer, for queue_Scan */
1258 /* Free any packets from the last call to ReadvProc/WritevProc */
1259 for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
1264 if (call->mode == RX_MODE_SENDING) {
1266 call->mode = (call->conn->type == RX_CLIENT_CONNECTION ?
1267 RX_MODE_RECEIVING: RX_MODE_EOF);
1269 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1270 /* Wait until TQ_BUSY is reset before adding any
1271 * packets to the transmit queue
1273 while (call->flags & RX_CALL_TQ_BUSY) {
1274 call->flags |= RX_CALL_TQ_WAIT;
1275 #ifdef RX_ENABLE_LOCKS
1276 CV_WAIT(&call->cv_tq, &call->lock);
1277 #else /* RX_ENABLE_LOCKS */
1278 osi_rxSleep(&call->tq);
1279 #endif /* RX_ENABLE_LOCKS */
1281 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1284 /* cp->length is only supposed to be the user's data */
1285 /* cp->length was already set to (then-current)
1286 * MaxUserDataSize or less. */
1287 cp->length -= call->nFree;
1288 call->currentPacket = (struct rx_packet *) 0;
1292 cp = rxi_AllocSendPacket(call,0);
1294 /* Mode can no longer be MODE_SENDING */
1298 cp->niovecs = 1; /* just the header */
1302 /* The 1 specifies that this is the last packet */
1303 hadd32(call->bytesSent, cp->length);
1304 rxi_PrepareSendPacket(call, cp, 1);
1305 queue_Append(&call->tq, cp);
1306 if (!(call->flags & (RX_CALL_FAST_RECOVER|
1307 RX_CALL_FAST_RECOVER_WAIT))) {
1308 rxi_Start(0, call, 0);
1313 /* Flush any buffered data to the stream, switch to read mode
1314 * (clients) or to EOF mode (servers) */
1315 void rx_FlushWrite(struct rx_call *call)
1320 MUTEX_ENTER(&call->lock);
1321 rxi_FlushWrite(call);
1322 MUTEX_EXIT(&call->lock);