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);
196 clock_GetTime(&when);
197 /* Delay to consolidate ack packets */
198 clock_Add(&when, &rx_hardAckDelay);
199 if (!call->delayedAckEvent
200 || clock_Gt(&call->delayedAckEvent->
202 rxevent_Cancel(call->delayedAckEvent,
204 RX_CALL_REFCOUNT_DELAY);
205 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
206 call->delayedAckEvent =
208 rxi_SendDelayedAck, call,
218 MTUXXX doesn't there need to be an "else" here ???
220 /* Are there ever going to be any more packets? */
221 if (call->flags & RX_CALL_RECEIVE_DONE) {
222 return requestCount - nbytes;
224 /* Wait for in-sequence packet */
225 call->flags |= RX_CALL_READER_WAIT;
227 call->startWait = clock_Sec();
228 while (call->flags & RX_CALL_READER_WAIT) {
229 #ifdef RX_ENABLE_LOCKS
230 CV_WAIT(&call->cv_rq, &call->lock);
232 osi_rxSleep(&call->rq);
237 #ifdef RX_ENABLE_LOCKS
241 #endif /* RX_ENABLE_LOCKS */
245 /* MTUXXX this should be replaced by some error-recovery code before shipping */
246 /* yes, the following block is allowed to be the ELSE clause (or not) */
247 /* It's possible for call->nLeft to be smaller than any particular
248 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
249 * reflects the size of the buffer. We have to keep track of the
250 * number of bytes read in the length field of the packet struct. On
251 * the final portion of a received packet, it's almost certain that
252 * call->nLeft will be smaller than the final buffer. */
253 while (nbytes && cp) {
254 t = MIN((int)call->curlen, nbytes);
255 t = MIN(t, (int)call->nLeft);
256 memcpy(buf, call->curpos, t);
264 /* out of packet. Get another one. */
266 cp = call->currentPacket = (struct rx_packet *)0;
267 } else if (!call->curlen) {
268 /* need to get another struct iov */
269 if (++call->curvec >= cp->niovecs) {
270 /* current packet is exhausted, get ready for another */
271 /* don't worry about curvec and stuff, they get set somewhere else */
273 cp = call->currentPacket = (struct rx_packet *)0;
277 (char *)cp->wirevec[call->curvec].iov_base;
278 call->curlen = cp->wirevec[call->curvec].iov_len;
283 /* user buffer is full, return */
293 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
302 * Free any packets from the last call to ReadvProc/WritevProc.
303 * We do not need the lock because the receiver threads only
304 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
305 * RX_CALL_IOVEC_WAIT is always cleared before returning from
306 * ReadvProc/WritevProc.
308 if (!queue_IsEmpty(&call->iovq)) {
309 rxi_FreePackets(0, &call->iovq);
313 * Most common case, all of the data is in the current iovec.
314 * We do not need the lock because this is the only thread that
315 * updates the curlen, curpos, nLeft fields.
317 * We are relying on nLeft being zero unless the call is in receive mode.
319 tcurlen = call->curlen;
320 tnLeft = call->nLeft;
321 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
322 tcurpos = call->curpos;
323 memcpy(buf, tcurpos, nbytes);
324 call->curpos = tcurpos + nbytes;
325 call->curlen = tcurlen - nbytes;
326 call->nLeft = tnLeft - nbytes;
331 MUTEX_ENTER(&call->lock);
332 bytes = rxi_ReadProc(call, buf, nbytes);
333 MUTEX_EXIT(&call->lock);
338 /* Optimization for unmarshalling 32 bit integers */
340 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 rxi_FreePackets(0, &call->iovq);
360 * Most common case, all of the data is in the current iovec.
361 * We do not need the lock because this is the only thread that
362 * updates the curlen, curpos, nLeft fields.
364 * We are relying on nLeft being zero unless the call is in receive mode.
366 tcurlen = call->curlen;
367 tnLeft = call->nLeft;
368 if (!call->error && tcurlen >= sizeof(afs_int32)
369 && tnLeft >= sizeof(afs_int32)) {
370 tcurpos = call->curpos;
371 memcpy((char *)value, tcurpos, sizeof(afs_int32));
372 call->curpos = tcurpos + sizeof(afs_int32);
373 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
374 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
375 return sizeof(afs_int32);
379 MUTEX_ENTER(&call->lock);
380 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
381 MUTEX_EXIT(&call->lock);
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)
397 register unsigned int t;
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 register struct rx_connection *conn = call->conn;
420 /* RXS_CheckPacket called to undo RXS_PreparePacket's
421 * work. It may reduce the length of the packet by up
422 * to conn->maxTrailerSize, to reflect the length of the
423 * data + the header. */
425 RXS_CheckPacket(conn->securityObject, call, rp))) {
426 /* Used to merely shut down the call, but now we
427 * shut down the whole connection since this may
428 * indicate an attempt to hijack it */
430 MUTEX_EXIT(&call->lock);
431 rxi_ConnectionError(conn, error);
432 MUTEX_ENTER(&conn->conn_data_lock);
433 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
434 MUTEX_EXIT(&conn->conn_data_lock);
436 MUTEX_ENTER(&call->lock);
441 curp = call->currentPacket = rp;
442 call->curvec = 1; /* 0th vec is always header */
443 cur_iov = &curp->wirevec[1];
444 /* begin at the beginning [ more or less ], continue
445 * on until the end, then stop. */
447 (char *)curp->wirevec[1].iov_base +
448 call->conn->securityHeaderSize;
450 curp->wirevec[1].iov_len -
451 call->conn->securityHeaderSize;
453 /* Notice that this code works correctly if the data
454 * size is 0 (which it may be--no reply arguments from
455 * server, for example). This relies heavily on the
456 * fact that the code below immediately frees the packet
457 * (no yields, etc.). If it didn't, this would be a
458 * problem because a value of zero for call->nLeft
459 * normally means that there is no read packet */
460 call->nLeft = curp->length;
461 hadd32(call->bytesRcvd, curp->length);
463 /* Send a hard ack for every rxi_HardAckRate+1 packets
464 * consumed. Otherwise schedule an event to send
465 * the hard ack later on.
475 /* It's possible for call->nLeft to be smaller than any particular
476 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
477 * reflects the size of the buffer. We have to keep track of the
478 * number of bytes read in the length field of the packet struct. On
479 * the final portion of a received packet, it's almost certain that
480 * call->nLeft will be smaller than the final buffer. */
481 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
483 t = MIN((int)call->curlen, call->iovNBytes);
484 t = MIN(t, (int)call->nLeft);
485 call_iov->iov_base = call->curpos;
486 call_iov->iov_len = t;
489 call->iovNBytes -= t;
495 /* out of packet. Get another one. */
496 queue_Append(&call->iovq, curp);
497 curp = call->currentPacket = (struct rx_packet *)0;
498 } else if (!call->curlen) {
499 /* need to get another struct iov */
500 if (++call->curvec >= curp->niovecs) {
501 /* current packet is exhausted, get ready for another */
502 /* don't worry about curvec and stuff, they get set somewhere else */
503 queue_Append(&call->iovq, curp);
504 curp = call->currentPacket = (struct rx_packet *)0;
508 call->curpos = (char *)cur_iov->iov_base;
509 call->curlen = cur_iov->iov_len;
515 /* If we consumed any packets then check whether we need to
516 * send a hard ack. */
517 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
518 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
519 rxevent_Cancel(call->delayedAckEvent, call,
520 RX_CALL_REFCOUNT_DELAY);
521 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
525 clock_GetTime(&when);
526 /* Delay to consolidate ack packets */
527 clock_Add(&when, &rx_hardAckDelay);
528 if (!call->delayedAckEvent
529 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
530 rxevent_Cancel(call->delayedAckEvent, call,
531 RX_CALL_REFCOUNT_DELAY);
532 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
533 call->delayedAckEvent =
534 rxevent_Post(&when, rxi_SendDelayedAck, call, 0);
542 /* rxi_ReadvProc -- internal version.
544 * Fills in an iovec with pointers to the packet buffers. All packets
545 * except the last packet (new current packet) are moved to the iovq
546 * while the application is processing the data.
548 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
551 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
554 struct rx_packet *rp;
558 requestCount = nbytes;
561 /* Free any packets from the last call to ReadvProc/WritevProc */
562 if (queue_IsNotEmpty(&call->iovq)) {
563 rxi_FreePackets(0, &call->iovq);
566 if (call->mode == RX_MODE_SENDING) {
567 rxi_FlushWrite(call);
574 /* Get whatever data is currently available in the receive queue.
575 * If rxi_FillReadVec sends an ack packet then it is possible
576 * that we will receive more data while we drop the call lock
577 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
578 * here to avoid a race with the receive thread if we send
579 * hard acks in rxi_FillReadVec. */
580 call->flags |= RX_CALL_IOVEC_WAIT;
581 call->iovNBytes = nbytes;
582 call->iovMax = maxio;
585 rxi_FillReadVec(call, 0);
587 /* if we need more data then sleep until the receive thread has
588 * filled in the rest. */
589 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
590 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
591 call->flags |= RX_CALL_READER_WAIT;
593 call->startWait = clock_Sec();
594 while (call->flags & RX_CALL_READER_WAIT) {
595 #ifdef RX_ENABLE_LOCKS
596 CV_WAIT(&call->cv_rq, &call->lock);
598 osi_rxSleep(&call->rq);
603 call->flags &= ~RX_CALL_IOVEC_WAIT;
604 #ifdef RX_ENABLE_LOCKS
608 #endif /* RX_ENABLE_LOCKS */
611 *nio = call->iovNext;
612 return nbytes - call->iovNBytes;
616 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
623 MUTEX_ENTER(&call->lock);
624 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
625 MUTEX_EXIT(&call->lock);
630 /* rxi_WriteProc -- internal version.
632 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
635 rxi_WriteProc(register struct rx_call *call, register char *buf,
638 struct rx_connection *conn = call->conn;
639 register struct rx_packet *cp = call->currentPacket;
640 register unsigned int t;
641 int requestCount = nbytes;
643 /* Free any packets from the last call to ReadvProc/WritevProc */
644 if (queue_IsNotEmpty(&call->iovq)) {
645 rxi_FreePackets(0, &call->iovq);
648 if (call->mode != RX_MODE_SENDING) {
649 if ((conn->type == RX_SERVER_CONNECTION)
650 && (call->mode == RX_MODE_RECEIVING)) {
651 call->mode = RX_MODE_SENDING;
654 cp = call->currentPacket = (struct rx_packet *)0;
663 /* Loop condition is checked at end, so that a write of 0 bytes
664 * will force a packet to be created--specially for the case where
665 * there are 0 bytes on the stream, but we must send a packet
668 if (call->nFree == 0) {
669 if (!call->error && cp) {
670 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
671 /* Wait until TQ_BUSY is reset before adding any
672 * packets to the transmit queue
674 while (call->flags & RX_CALL_TQ_BUSY) {
675 call->flags |= RX_CALL_TQ_WAIT;
676 #ifdef RX_ENABLE_LOCKS
677 CV_WAIT(&call->cv_tq, &call->lock);
678 #else /* RX_ENABLE_LOCKS */
679 osi_rxSleep(&call->tq);
680 #endif /* RX_ENABLE_LOCKS */
682 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
683 clock_NewTime(); /* Bogus: need new time package */
684 /* The 0, below, specifies that it is not the last packet:
685 * there will be others. PrepareSendPacket may
686 * alter the packet length by up to
687 * conn->securityMaxTrailerSize */
688 hadd32(call->bytesSent, cp->length);
689 rxi_PrepareSendPacket(call, cp, 0);
690 queue_Append(&call->tq, cp);
691 cp = call->currentPacket = NULL;
694 flags & (RX_CALL_FAST_RECOVER |
695 RX_CALL_FAST_RECOVER_WAIT))) {
696 rxi_Start(0, call, 0, 0);
699 /* Wait for transmit window to open up */
701 && call->tnext + 1 > call->tfirst + call->twind) {
703 call->startWait = clock_Sec();
705 #ifdef RX_ENABLE_LOCKS
706 CV_WAIT(&call->cv_twind, &call->lock);
708 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
709 osi_rxSleep(&call->twind);
713 #ifdef RX_ENABLE_LOCKS
717 #endif /* RX_ENABLE_LOCKS */
719 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
720 call->currentPacket = cp;
721 call->nFree = cp->length;
722 call->curvec = 1; /* 0th vec is always header */
723 /* begin at the beginning [ more or less ], continue
724 * on until the end, then stop. */
726 (char *)cp->wirevec[1].iov_base +
727 call->conn->securityHeaderSize;
729 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
734 call->currentPacket = NULL;
740 if (cp && (int)call->nFree < nbytes) {
741 /* Try to extend the current buffer */
742 register int len, mud;
744 mud = rx_MaxUserDataSize(call);
747 want = MIN(nbytes - (int)call->nFree, mud - len);
748 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
749 if (cp->length > (unsigned)mud)
751 call->nFree += (cp->length - len);
755 /* If the remaining bytes fit in the buffer, then store them
756 * and return. Don't ship a buffer that's full immediately to
757 * the peer--we don't know if it's the last buffer yet */
763 while (nbytes && call->nFree) {
765 t = MIN((int)call->curlen, nbytes);
766 t = MIN((int)call->nFree, t);
767 memcpy(call->curpos, buf, t);
771 call->curlen -= (u_short)t;
772 call->nFree -= (u_short)t;
775 /* need to get another struct iov */
776 if (++call->curvec >= cp->niovecs) {
777 /* current packet is full, extend or send it */
780 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
781 call->curlen = cp->wirevec[call->curvec].iov_len;
784 } /* while bytes to send and room to send them */
786 /* might be out of space now */
789 } else; /* more data to send, so get another packet and keep going */
792 return requestCount - nbytes;
796 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
805 * Free any packets from the last call to ReadvProc/WritevProc.
806 * We do not need the lock because the receiver threads only
807 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
808 * RX_CALL_IOVEC_WAIT is always cleared before returning from
809 * ReadvProc/WritevProc.
811 if (queue_IsNotEmpty(&call->iovq)) {
812 rxi_FreePackets(0, &call->iovq);
816 * Most common case: all of the data fits in the current iovec.
817 * We do not need the lock because this is the only thread that
818 * updates the curlen, curpos, nFree fields.
820 * We are relying on nFree being zero unless the call is in send mode.
822 tcurlen = (int)call->curlen;
823 tnFree = (int)call->nFree;
824 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
825 tcurpos = call->curpos;
826 memcpy(tcurpos, buf, nbytes);
827 call->curpos = tcurpos + nbytes;
828 call->curlen = (u_short)(tcurlen - nbytes);
829 call->nFree = (u_short)(tnFree - nbytes);
834 MUTEX_ENTER(&call->lock);
835 bytes = rxi_WriteProc(call, buf, nbytes);
836 MUTEX_EXIT(&call->lock);
841 /* Optimization for marshalling 32 bit arguments */
843 rx_WriteProc32(register struct rx_call *call, register afs_int32 * value)
852 * Free any packets from the last call to ReadvProc/WritevProc.
853 * We do not need the lock because the receiver threads only
854 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
855 * RX_CALL_IOVEC_WAIT is always cleared before returning from
856 * ReadvProc/WritevProc.
858 if (queue_IsNotEmpty(&call->iovq)) {
859 rxi_FreePackets(0, &call->iovq);
863 * Most common case: all of the data fits in the current iovec.
864 * We do not need the lock because this is the only thread that
865 * updates the curlen, curpos, nFree fields.
867 * We are relying on nFree being zero unless the call is in send mode.
869 tcurlen = call->curlen;
870 tnFree = call->nFree;
871 if (!call->error && tcurlen >= sizeof(afs_int32)
872 && tnFree >= sizeof(afs_int32)) {
873 tcurpos = call->curpos;
874 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
875 *((afs_int32 *) (tcurpos)) = *value;
877 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
879 call->curpos = tcurpos + sizeof(afs_int32);
880 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
881 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
882 return sizeof(afs_int32);
886 MUTEX_ENTER(&call->lock);
887 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
888 MUTEX_EXIT(&call->lock);
893 /* rxi_WritevAlloc -- internal version.
895 * Fill in an iovec to point to data in packet buffers. The application
896 * calls rxi_WritevProc when the buffers are full.
898 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
901 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
904 struct rx_connection *conn = call->conn;
905 struct rx_packet *cp = call->currentPacket;
908 /* Temporary values, real work is done in rxi_WritevProc */
914 requestCount = nbytes;
917 /* Free any packets from the last call to ReadvProc/WritevProc */
918 if (queue_IsNotEmpty(&call->iovq)) {
919 rxi_FreePackets(0, &call->iovq);
922 if (call->mode != RX_MODE_SENDING) {
923 if ((conn->type == RX_SERVER_CONNECTION)
924 && (call->mode == RX_MODE_RECEIVING)) {
925 call->mode = RX_MODE_SENDING;
928 cp = call->currentPacket = (struct rx_packet *)0;
937 /* Set up the iovec to point to data in packet buffers. */
938 tnFree = call->nFree;
939 tcurvec = call->curvec;
940 tcurpos = call->curpos;
941 tcurlen = call->curlen;
943 register unsigned int t;
946 /* current packet is full, allocate a new one */
947 cp = rxi_AllocSendPacket(call, nbytes);
949 /* out of space, return what we have */
951 return requestCount - nbytes;
953 queue_Append(&call->iovq, cp);
957 (char *)cp->wirevec[1].iov_base +
958 call->conn->securityHeaderSize;
959 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
962 if (tnFree < nbytes) {
963 /* try to extend the current packet */
964 register int len, mud;
966 mud = rx_MaxUserDataSize(call);
969 want = MIN(nbytes - tnFree, mud - len);
970 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
971 if (cp->length > (unsigned)mud)
973 tnFree += (cp->length - len);
974 if (cp == call->currentPacket) {
975 call->nFree += (cp->length - len);
980 /* fill in the next entry in the iovec */
981 t = MIN(tcurlen, nbytes);
983 iov[nextio].iov_base = tcurpos;
984 iov[nextio].iov_len = t;
992 /* need to get another struct iov */
993 if (++tcurvec >= cp->niovecs) {
994 /* current packet is full, extend it or move on to next packet */
997 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
998 tcurlen = cp->wirevec[tcurvec].iov_len;
1001 } while (nbytes && nextio < maxio);
1003 return requestCount - nbytes;
1007 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1014 MUTEX_ENTER(&call->lock);
1015 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1016 MUTEX_EXIT(&call->lock);
1021 /* rxi_WritevProc -- internal version.
1023 * Send buffers allocated in rxi_WritevAlloc.
1025 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1028 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1030 struct rx_packet *cp = call->currentPacket;
1033 struct rx_queue tmpq;
1035 requestCount = nbytes;
1038 if (call->mode != RX_MODE_SENDING) {
1039 call->error = RX_PROTOCOL_ERROR;
1041 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1042 /* Wait until TQ_BUSY is reset before trying to move any
1043 * packets to the transmit queue. */
1044 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1045 call->flags |= RX_CALL_TQ_WAIT;
1046 #ifdef RX_ENABLE_LOCKS
1047 CV_WAIT(&call->cv_tq, &call->lock);
1048 #else /* RX_ENABLE_LOCKS */
1049 osi_rxSleep(&call->tq);
1050 #endif /* RX_ENABLE_LOCKS */
1052 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1056 queue_Prepend(&call->iovq, cp);
1057 cp = call->currentPacket = NULL;
1059 rxi_FreePackets(0, &call->iovq);
1063 /* Loop through the I/O vector adjusting packet pointers.
1064 * Place full packets back onto the iovq once they are ready
1065 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1066 * the iovec. We put the loop condition at the end to ensure that
1067 * a zero length write will push a short packet. */
1071 if (call->nFree == 0 && cp) {
1072 clock_NewTime(); /* Bogus: need new time package */
1073 /* The 0, below, specifies that it is not the last packet:
1074 * there will be others. PrepareSendPacket may
1075 * alter the packet length by up to
1076 * conn->securityMaxTrailerSize */
1077 hadd32(call->bytesSent, cp->length);
1078 rxi_PrepareSendPacket(call, cp, 0);
1079 queue_Append(&tmpq, cp);
1081 /* The head of the iovq is now the current packet */
1083 if (queue_IsEmpty(&call->iovq)) {
1084 call->error = RX_PROTOCOL_ERROR;
1085 cp = call->currentPacket = NULL;
1086 rxi_FreePackets(0, &tmpq);
1089 cp = queue_First(&call->iovq, rx_packet);
1091 call->currentPacket = cp;
1092 call->nFree = cp->length;
1095 (char *)cp->wirevec[1].iov_base +
1096 call->conn->securityHeaderSize;
1098 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1103 /* The next iovec should point to the current position */
1104 if (iov[nextio].iov_base != call->curpos
1105 || iov[nextio].iov_len > (int)call->curlen) {
1106 call->error = RX_PROTOCOL_ERROR;
1108 queue_Prepend(&tmpq, cp);
1109 call->currentPacket = NULL;
1111 rxi_FreePackets(0, &tmpq);
1114 nbytes -= iov[nextio].iov_len;
1115 call->curpos += iov[nextio].iov_len;
1116 call->curlen -= iov[nextio].iov_len;
1117 call->nFree -= iov[nextio].iov_len;
1119 if (call->curlen == 0) {
1120 if (++call->curvec > cp->niovecs) {
1123 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1124 call->curlen = cp->wirevec[call->curvec].iov_len;
1128 } while (nbytes && nextio < nio);
1130 /* Move the packets from the temporary queue onto the transmit queue.
1131 * We may end up with more than call->twind packets on the queue. */
1132 queue_SpliceAppend(&call->tq, &tmpq);
1134 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1135 rxi_Start(0, call, 0, 0);
1138 /* Wait for the length of the transmit queue to fall below call->twind */
1139 while (!call->error && call->tnext + 1 > call->tfirst + call->twind) {
1141 call->startWait = clock_Sec();
1142 #ifdef RX_ENABLE_LOCKS
1143 CV_WAIT(&call->cv_twind, &call->lock);
1145 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1146 osi_rxSleep(&call->twind);
1148 call->startWait = 0;
1154 cp = call->currentPacket = NULL;
1159 return requestCount - nbytes;
1163 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1169 MUTEX_ENTER(&call->lock);
1170 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1171 MUTEX_EXIT(&call->lock);
1176 /* Flush any buffered data to the stream, switch to read mode
1177 * (clients) or to EOF mode (servers) */
1179 rxi_FlushWrite(register struct rx_call *call)
1181 register struct rx_packet *cp = call->currentPacket;
1183 /* Free any packets from the last call to ReadvProc/WritevProc */
1184 if (queue_IsNotEmpty(&call->iovq)) {
1185 rxi_FreePackets(0, &call->iovq);
1188 if (call->mode == RX_MODE_SENDING) {
1191 (call->conn->type ==
1192 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1194 #ifdef RX_KERNEL_TRACE
1196 int glockOwner = ISAFS_GLOCK();
1199 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1200 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1207 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1208 /* Wait until TQ_BUSY is reset before adding any
1209 * packets to the transmit queue
1211 while (call->flags & RX_CALL_TQ_BUSY) {
1212 call->flags |= RX_CALL_TQ_WAIT;
1213 #ifdef RX_ENABLE_LOCKS
1214 CV_WAIT(&call->cv_tq, &call->lock);
1215 #else /* RX_ENABLE_LOCKS */
1216 osi_rxSleep(&call->tq);
1217 #endif /* RX_ENABLE_LOCKS */
1219 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1222 /* cp->length is only supposed to be the user's data */
1223 /* cp->length was already set to (then-current)
1224 * MaxUserDataSize or less. */
1225 cp->length -= call->nFree;
1226 call->currentPacket = (struct rx_packet *)0;
1229 cp = rxi_AllocSendPacket(call, 0);
1231 /* Mode can no longer be MODE_SENDING */
1235 cp->niovecs = 2; /* header + space for rxkad stuff */
1239 /* The 1 specifies that this is the last packet */
1240 hadd32(call->bytesSent, cp->length);
1241 rxi_PrepareSendPacket(call, cp, 1);
1242 queue_Append(&call->tq, cp);
1245 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1246 rxi_Start(0, call, 0, 0);
1251 /* Flush any buffered data to the stream, switch to read mode
1252 * (clients) or to EOF mode (servers) */
1254 rx_FlushWrite(struct rx_call *call)
1258 MUTEX_ENTER(&call->lock);
1259 rxi_FlushWrite(call);
1260 MUTEX_EXIT(&call->lock);