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 */
95 # include "rx_clock.h"
96 # include "rx_queue.h"
98 # include "rx_globals.h"
102 /* rxdb_fileID is used to identify the lock location, along with line#. */
103 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
104 #endif /* RX_LOCKS_DB */
105 /* rxi_ReadProc -- internal version.
107 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
110 rxi_ReadProc(register struct rx_call *call, register char *buf,
113 register struct rx_packet *cp = call->currentPacket;
114 register struct rx_packet *rp;
115 register int requestCount;
116 register unsigned int t;
118 /* XXXX took out clock_NewTime from here. Was it needed? */
119 requestCount = nbytes;
121 /* Free any packets from the last call to ReadvProc/WritevProc */
122 if (queue_IsNotEmpty(&call->iovq)) {
123 rxi_FreePackets(0, &call->iovq);
127 if (call->nLeft == 0) {
128 /* Get next packet */
130 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
134 if (call->mode == RX_MODE_SENDING) {
135 rxi_FlushWrite(call);
139 if (queue_IsNotEmpty(&call->rq)) {
140 /* Check that next packet available is next in sequence */
141 rp = queue_First(&call->rq, rx_packet);
142 if (rp->header.seq == call->rnext) {
144 register struct rx_connection *conn = call->conn;
147 /* RXS_CheckPacket called to undo RXS_PreparePacket's
148 * work. It may reduce the length of the packet by up
149 * to conn->maxTrailerSize, to reflect the length of the
150 * data + the header. */
152 RXS_CheckPacket(conn->securityObject, call,
154 /* Used to merely shut down the call, but now we
155 * shut down the whole connection since this may
156 * indicate an attempt to hijack it */
158 MUTEX_EXIT(&call->lock);
159 rxi_ConnectionError(conn, error);
160 MUTEX_ENTER(&conn->conn_data_lock);
161 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
162 MUTEX_EXIT(&conn->conn_data_lock);
164 MUTEX_ENTER(&call->lock);
169 cp = call->currentPacket = rp;
170 call->curvec = 1; /* 0th vec is always header */
171 /* begin at the beginning [ more or less ], continue
172 * on until the end, then stop. */
174 (char *)cp->wirevec[1].iov_base +
175 call->conn->securityHeaderSize;
177 cp->wirevec[1].iov_len -
178 call->conn->securityHeaderSize;
180 /* Notice that this code works correctly if the data
181 * size is 0 (which it may be--no reply arguments from
182 * server, for example). This relies heavily on the
183 * fact that the code below immediately frees the packet
184 * (no yields, etc.). If it didn't, this would be a
185 * problem because a value of zero for call->nLeft
186 * normally means that there is no read packet */
187 call->nLeft = cp->length;
188 hadd32(call->bytesRcvd, cp->length);
190 /* Send a hard ack for every rxi_HardAckRate+1 packets
191 * consumed. Otherwise schedule an event to send
192 * the hard ack later on.
195 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
196 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
197 rxevent_Cancel(call->delayedAckEvent, call,
198 RX_CALL_REFCOUNT_DELAY);
199 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
202 clock_GetTime(&when);
203 /* Delay to consolidate ack packets */
204 clock_Add(&when, &rx_hardAckDelay);
205 if (!call->delayedAckEvent
206 || clock_Gt(&call->delayedAckEvent->
208 rxevent_Cancel(call->delayedAckEvent,
210 RX_CALL_REFCOUNT_DELAY);
211 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
212 call->delayedAckEvent =
214 rxi_SendDelayedAck, call,
224 MTUXXX doesn't there need to be an "else" here ???
226 /* Are there ever going to be any more packets? */
227 if (call->flags & RX_CALL_RECEIVE_DONE) {
228 return requestCount - nbytes;
230 /* Wait for in-sequence packet */
231 call->flags |= RX_CALL_READER_WAIT;
233 call->startWait = clock_Sec();
234 while (call->flags & RX_CALL_READER_WAIT) {
235 #ifdef RX_ENABLE_LOCKS
236 CV_WAIT(&call->cv_rq, &call->lock);
238 osi_rxSleep(&call->rq);
243 #ifdef RX_ENABLE_LOCKS
247 #endif /* RX_ENABLE_LOCKS */
251 /* MTUXXX this should be replaced by some error-recovery code before shipping */
252 /* yes, the following block is allowed to be the ELSE clause (or not) */
253 /* It's possible for call->nLeft to be smaller than any particular
254 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
255 * reflects the size of the buffer. We have to keep track of the
256 * number of bytes read in the length field of the packet struct. On
257 * the final portion of a received packet, it's almost certain that
258 * call->nLeft will be smaller than the final buffer. */
259 while (nbytes && cp) {
260 t = MIN((int)call->curlen, nbytes);
261 t = MIN(t, (int)call->nLeft);
262 memcpy(buf, call->curpos, t);
270 /* out of packet. Get another one. */
272 cp = call->currentPacket = (struct rx_packet *)0;
273 } else if (!call->curlen) {
274 /* need to get another struct iov */
275 if (++call->curvec >= cp->niovecs) {
276 /* current packet is exhausted, get ready for another */
277 /* don't worry about curvec and stuff, they get set somewhere else */
279 cp = call->currentPacket = (struct rx_packet *)0;
283 (char *)cp->wirevec[call->curvec].iov_base;
284 call->curlen = cp->wirevec[call->curvec].iov_len;
289 /* user buffer is full, return */
299 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
308 * Free any packets from the last call to ReadvProc/WritevProc.
309 * We do not need the lock because the receiver threads only
310 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
311 * RX_CALL_IOVEC_WAIT is always cleared before returning from
312 * ReadvProc/WritevProc.
314 if (!queue_IsEmpty(&call->iovq)) {
315 rxi_FreePackets(0, &call->iovq);
319 * Most common case, all of the data is in the current iovec.
320 * We do not need the lock because this is the only thread that
321 * updates the curlen, curpos, nLeft fields.
323 * We are relying on nLeft being zero unless the call is in receive mode.
325 tcurlen = call->curlen;
326 tnLeft = call->nLeft;
327 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
328 tcurpos = call->curpos;
329 memcpy(buf, tcurpos, nbytes);
330 call->curpos = tcurpos + nbytes;
331 call->curlen = tcurlen - nbytes;
332 call->nLeft = tnLeft - nbytes;
337 MUTEX_ENTER(&call->lock);
338 bytes = rxi_ReadProc(call, buf, nbytes);
339 MUTEX_EXIT(&call->lock);
344 /* Optimization for unmarshalling 32 bit integers */
346 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
355 * Free any packets from the last call to ReadvProc/WritevProc.
356 * We do not need the lock because the receiver threads only
357 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
358 * RX_CALL_IOVEC_WAIT is always cleared before returning from
359 * ReadvProc/WritevProc.
361 if (!queue_IsEmpty(&call->iovq)) {
362 rxi_FreePackets(0, &call->iovq);
366 * Most common case, all of the data is in the current iovec.
367 * We do not need the lock because this is the only thread that
368 * updates the curlen, curpos, nLeft fields.
370 * We are relying on nLeft being zero unless the call is in receive mode.
372 tcurlen = call->curlen;
373 tnLeft = call->nLeft;
374 if (!call->error && tcurlen >= sizeof(afs_int32)
375 && tnLeft >= sizeof(afs_int32)) {
376 tcurpos = call->curpos;
377 memcpy((char *)value, tcurpos, sizeof(afs_int32));
378 call->curpos = tcurpos + sizeof(afs_int32);
379 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
380 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
381 return sizeof(afs_int32);
385 MUTEX_ENTER(&call->lock);
386 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
387 MUTEX_EXIT(&call->lock);
394 * Uses packets in the receive queue to fill in as much of the
395 * current iovec as possible. Does not block if it runs out
396 * of packets to complete the iovec. Return true if an ack packet
397 * was sent, otherwise return false */
399 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
403 register unsigned int t;
404 struct rx_packet *rp;
405 struct rx_packet *curp;
406 struct iovec *call_iov;
407 struct iovec *cur_iov = NULL;
409 curp = call->currentPacket;
411 cur_iov = &curp->wirevec[call->curvec];
413 call_iov = &call->iov[call->iovNext];
415 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
416 if (call->nLeft == 0) {
417 /* Get next packet */
418 if (queue_IsNotEmpty(&call->rq)) {
419 /* Check that next packet available is next in sequence */
420 rp = queue_First(&call->rq, rx_packet);
421 if (rp->header.seq == call->rnext) {
423 register struct rx_connection *conn = call->conn;
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 call->curvec = 1; /* 0th vec is always header */
449 cur_iov = &curp->wirevec[1];
450 /* begin at the beginning [ more or less ], continue
451 * on until the end, then stop. */
453 (char *)curp->wirevec[1].iov_base +
454 call->conn->securityHeaderSize;
456 curp->wirevec[1].iov_len -
457 call->conn->securityHeaderSize;
459 /* Notice that this code works correctly if the data
460 * size is 0 (which it may be--no reply arguments from
461 * server, for example). This relies heavily on the
462 * fact that the code below immediately frees the packet
463 * (no yields, etc.). If it didn't, this would be a
464 * problem because a value of zero for call->nLeft
465 * normally means that there is no read packet */
466 call->nLeft = curp->length;
467 hadd32(call->bytesRcvd, curp->length);
469 /* Send a hard ack for every rxi_HardAckRate+1 packets
470 * consumed. Otherwise schedule an event to send
471 * the hard ack later on.
481 /* It's possible for call->nLeft to be smaller than any particular
482 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
483 * reflects the size of the buffer. We have to keep track of the
484 * number of bytes read in the length field of the packet struct. On
485 * the final portion of a received packet, it's almost certain that
486 * call->nLeft will be smaller than the final buffer. */
487 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
489 t = MIN((int)call->curlen, call->iovNBytes);
490 t = MIN(t, (int)call->nLeft);
491 call_iov->iov_base = call->curpos;
492 call_iov->iov_len = t;
495 call->iovNBytes -= t;
501 /* out of packet. Get another one. */
502 queue_Append(&call->iovq, curp);
503 curp = call->currentPacket = (struct rx_packet *)0;
504 } else if (!call->curlen) {
505 /* need to get another struct iov */
506 if (++call->curvec >= curp->niovecs) {
507 /* current packet is exhausted, get ready for another */
508 /* don't worry about curvec and stuff, they get set somewhere else */
509 queue_Append(&call->iovq, curp);
510 curp = call->currentPacket = (struct rx_packet *)0;
514 call->curpos = (char *)cur_iov->iov_base;
515 call->curlen = cur_iov->iov_len;
521 /* If we consumed any packets then check whether we need to
522 * send a hard ack. */
523 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
524 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
525 rxevent_Cancel(call->delayedAckEvent, call,
526 RX_CALL_REFCOUNT_DELAY);
527 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
531 clock_GetTime(&when);
532 /* Delay to consolidate ack packets */
533 clock_Add(&when, &rx_hardAckDelay);
534 if (!call->delayedAckEvent
535 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
536 rxevent_Cancel(call->delayedAckEvent, call,
537 RX_CALL_REFCOUNT_DELAY);
538 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
539 call->delayedAckEvent =
540 rxevent_Post(&when, rxi_SendDelayedAck, call, 0);
548 /* rxi_ReadvProc -- internal version.
550 * Fills in an iovec with pointers to the packet buffers. All packets
551 * except the last packet (new current packet) are moved to the iovq
552 * while the application is processing the data.
554 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
557 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
560 struct rx_packet *rp;
564 requestCount = nbytes;
567 /* Free any packets from the last call to ReadvProc/WritevProc */
568 if (queue_IsNotEmpty(&call->iovq)) {
569 rxi_FreePackets(0, &call->iovq);
572 if (call->mode == RX_MODE_SENDING) {
573 rxi_FlushWrite(call);
580 /* Get whatever data is currently available in the receive queue.
581 * If rxi_FillReadVec sends an ack packet then it is possible
582 * that we will receive more data while we drop the call lock
583 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
584 * here to avoid a race with the receive thread if we send
585 * hard acks in rxi_FillReadVec. */
586 call->flags |= RX_CALL_IOVEC_WAIT;
587 call->iovNBytes = nbytes;
588 call->iovMax = maxio;
591 rxi_FillReadVec(call, 0);
593 /* if we need more data then sleep until the receive thread has
594 * filled in the rest. */
595 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
596 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
597 call->flags |= RX_CALL_READER_WAIT;
599 call->startWait = clock_Sec();
600 while (call->flags & RX_CALL_READER_WAIT) {
601 #ifdef RX_ENABLE_LOCKS
602 CV_WAIT(&call->cv_rq, &call->lock);
604 osi_rxSleep(&call->rq);
609 call->flags &= ~RX_CALL_IOVEC_WAIT;
610 #ifdef RX_ENABLE_LOCKS
614 #endif /* RX_ENABLE_LOCKS */
617 *nio = call->iovNext;
618 return nbytes - call->iovNBytes;
622 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
629 MUTEX_ENTER(&call->lock);
630 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
631 MUTEX_EXIT(&call->lock);
636 /* rxi_WriteProc -- internal version.
638 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
641 rxi_WriteProc(register struct rx_call *call, register char *buf,
644 struct rx_connection *conn = call->conn;
645 register struct rx_packet *cp = call->currentPacket;
646 register unsigned int t;
647 int requestCount = nbytes;
649 /* Free any packets from the last call to ReadvProc/WritevProc */
650 if (queue_IsNotEmpty(&call->iovq)) {
651 rxi_FreePackets(0, &call->iovq);
654 if (call->mode != RX_MODE_SENDING) {
655 if ((conn->type == RX_SERVER_CONNECTION)
656 && (call->mode == RX_MODE_RECEIVING)) {
657 call->mode = RX_MODE_SENDING;
660 cp = call->currentPacket = (struct rx_packet *)0;
669 /* Loop condition is checked at end, so that a write of 0 bytes
670 * will force a packet to be created--specially for the case where
671 * there are 0 bytes on the stream, but we must send a packet
674 if (call->nFree == 0) {
675 if (!call->error && cp) {
676 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
677 /* Wait until TQ_BUSY is reset before adding any
678 * packets to the transmit queue
680 while (call->flags & RX_CALL_TQ_BUSY) {
681 call->flags |= RX_CALL_TQ_WAIT;
682 #ifdef RX_ENABLE_LOCKS
683 CV_WAIT(&call->cv_tq, &call->lock);
684 #else /* RX_ENABLE_LOCKS */
685 osi_rxSleep(&call->tq);
686 #endif /* RX_ENABLE_LOCKS */
688 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
689 clock_NewTime(); /* Bogus: need new time package */
690 /* The 0, below, specifies that it is not the last packet:
691 * there will be others. PrepareSendPacket may
692 * alter the packet length by up to
693 * conn->securityMaxTrailerSize */
694 hadd32(call->bytesSent, cp->length);
695 rxi_PrepareSendPacket(call, cp, 0);
696 queue_Append(&call->tq, cp);
697 cp = call->currentPacket = NULL;
700 flags & (RX_CALL_FAST_RECOVER |
701 RX_CALL_FAST_RECOVER_WAIT))) {
702 rxi_Start(0, call, 0, 0);
705 /* Wait for transmit window to open up */
707 && call->tnext + 1 > call->tfirst + call->twind) {
709 call->startWait = clock_Sec();
711 #ifdef RX_ENABLE_LOCKS
712 CV_WAIT(&call->cv_twind, &call->lock);
714 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
715 osi_rxSleep(&call->twind);
719 #ifdef RX_ENABLE_LOCKS
723 #endif /* RX_ENABLE_LOCKS */
725 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
726 call->currentPacket = cp;
727 call->nFree = cp->length;
728 call->curvec = 1; /* 0th vec is always header */
729 /* begin at the beginning [ more or less ], continue
730 * on until the end, then stop. */
732 (char *)cp->wirevec[1].iov_base +
733 call->conn->securityHeaderSize;
735 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
740 call->currentPacket = NULL;
746 if (cp && (int)call->nFree < nbytes) {
747 /* Try to extend the current buffer */
748 register int len, mud;
750 mud = rx_MaxUserDataSize(call);
753 want = MIN(nbytes - (int)call->nFree, mud - len);
754 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
755 if (cp->length > (unsigned)mud)
757 call->nFree += (cp->length - len);
761 /* If the remaining bytes fit in the buffer, then store them
762 * and return. Don't ship a buffer that's full immediately to
763 * the peer--we don't know if it's the last buffer yet */
769 while (nbytes && call->nFree) {
771 t = MIN((int)call->curlen, nbytes);
772 t = MIN((int)call->nFree, t);
773 memcpy(call->curpos, buf, t);
777 call->curlen -= (u_short)t;
778 call->nFree -= (u_short)t;
781 /* need to get another struct iov */
782 if (++call->curvec >= cp->niovecs) {
783 /* current packet is full, extend or send it */
786 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
787 call->curlen = cp->wirevec[call->curvec].iov_len;
790 } /* while bytes to send and room to send them */
792 /* might be out of space now */
795 } else; /* more data to send, so get another packet and keep going */
798 return requestCount - nbytes;
802 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
811 * Free any packets from the last call to ReadvProc/WritevProc.
812 * We do not need the lock because the receiver threads only
813 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
814 * RX_CALL_IOVEC_WAIT is always cleared before returning from
815 * ReadvProc/WritevProc.
817 if (queue_IsNotEmpty(&call->iovq)) {
818 rxi_FreePackets(0, &call->iovq);
822 * Most common case: all of the data fits in the current iovec.
823 * We do not need the lock because this is the only thread that
824 * updates the curlen, curpos, nFree fields.
826 * We are relying on nFree being zero unless the call is in send mode.
828 tcurlen = (int)call->curlen;
829 tnFree = (int)call->nFree;
830 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
831 tcurpos = call->curpos;
832 memcpy(tcurpos, buf, nbytes);
833 call->curpos = tcurpos + nbytes;
834 call->curlen = (u_short)(tcurlen - nbytes);
835 call->nFree = (u_short)(tnFree - nbytes);
840 MUTEX_ENTER(&call->lock);
841 bytes = rxi_WriteProc(call, buf, nbytes);
842 MUTEX_EXIT(&call->lock);
847 /* Optimization for marshalling 32 bit arguments */
849 rx_WriteProc32(register struct rx_call *call, register afs_int32 * value)
858 * Free any packets from the last call to ReadvProc/WritevProc.
859 * We do not need the lock because the receiver threads only
860 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
861 * RX_CALL_IOVEC_WAIT is always cleared before returning from
862 * ReadvProc/WritevProc.
864 if (queue_IsNotEmpty(&call->iovq)) {
865 rxi_FreePackets(0, &call->iovq);
869 * Most common case: all of the data fits in the current iovec.
870 * We do not need the lock because this is the only thread that
871 * updates the curlen, curpos, nFree fields.
873 * We are relying on nFree being zero unless the call is in send mode.
875 tcurlen = call->curlen;
876 tnFree = call->nFree;
877 if (!call->error && tcurlen >= sizeof(afs_int32)
878 && tnFree >= sizeof(afs_int32)) {
879 tcurpos = call->curpos;
880 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
881 *((afs_int32 *) (tcurpos)) = *value;
883 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
885 call->curpos = tcurpos + sizeof(afs_int32);
886 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
887 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
888 return sizeof(afs_int32);
892 MUTEX_ENTER(&call->lock);
893 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
894 MUTEX_EXIT(&call->lock);
899 /* rxi_WritevAlloc -- internal version.
901 * Fill in an iovec to point to data in packet buffers. The application
902 * calls rxi_WritevProc when the buffers are full.
904 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
907 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
910 struct rx_connection *conn = call->conn;
911 struct rx_packet *cp = call->currentPacket;
914 /* Temporary values, real work is done in rxi_WritevProc */
920 requestCount = nbytes;
923 /* Free any packets from the last call to ReadvProc/WritevProc */
924 if (queue_IsNotEmpty(&call->iovq)) {
925 rxi_FreePackets(0, &call->iovq);
928 if (call->mode != RX_MODE_SENDING) {
929 if ((conn->type == RX_SERVER_CONNECTION)
930 && (call->mode == RX_MODE_RECEIVING)) {
931 call->mode = RX_MODE_SENDING;
934 cp = call->currentPacket = (struct rx_packet *)0;
943 /* Set up the iovec to point to data in packet buffers. */
944 tnFree = call->nFree;
945 tcurvec = call->curvec;
946 tcurpos = call->curpos;
947 tcurlen = call->curlen;
949 register unsigned int t;
952 /* current packet is full, allocate a new one */
953 cp = rxi_AllocSendPacket(call, nbytes);
955 /* out of space, return what we have */
957 return requestCount - nbytes;
959 queue_Append(&call->iovq, cp);
963 (char *)cp->wirevec[1].iov_base +
964 call->conn->securityHeaderSize;
965 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
968 if (tnFree < nbytes) {
969 /* try to extend the current packet */
970 register int len, mud;
972 mud = rx_MaxUserDataSize(call);
975 want = MIN(nbytes - tnFree, mud - len);
976 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
977 if (cp->length > (unsigned)mud)
979 tnFree += (cp->length - len);
980 if (cp == call->currentPacket) {
981 call->nFree += (cp->length - len);
986 /* fill in the next entry in the iovec */
987 t = MIN(tcurlen, nbytes);
989 iov[nextio].iov_base = tcurpos;
990 iov[nextio].iov_len = t;
998 /* need to get another struct iov */
999 if (++tcurvec >= cp->niovecs) {
1000 /* current packet is full, extend it or move on to next packet */
1003 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1004 tcurlen = cp->wirevec[tcurvec].iov_len;
1007 } while (nbytes && nextio < maxio);
1009 return requestCount - nbytes;
1013 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1020 MUTEX_ENTER(&call->lock);
1021 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1022 MUTEX_EXIT(&call->lock);
1027 /* rxi_WritevProc -- internal version.
1029 * Send buffers allocated in rxi_WritevAlloc.
1031 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1034 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1036 struct rx_packet *cp = call->currentPacket;
1039 struct rx_queue tmpq;
1041 requestCount = nbytes;
1044 if (call->mode != RX_MODE_SENDING) {
1045 call->error = RX_PROTOCOL_ERROR;
1047 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1048 /* Wait until TQ_BUSY is reset before trying to move any
1049 * packets to the transmit queue. */
1050 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1051 call->flags |= RX_CALL_TQ_WAIT;
1052 #ifdef RX_ENABLE_LOCKS
1053 CV_WAIT(&call->cv_tq, &call->lock);
1054 #else /* RX_ENABLE_LOCKS */
1055 osi_rxSleep(&call->tq);
1056 #endif /* RX_ENABLE_LOCKS */
1058 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1062 queue_Prepend(&call->iovq, cp);
1063 cp = call->currentPacket = NULL;
1065 rxi_FreePackets(0, &call->iovq);
1069 /* Loop through the I/O vector adjusting packet pointers.
1070 * Place full packets back onto the iovq once they are ready
1071 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1072 * the iovec. We put the loop condition at the end to ensure that
1073 * a zero length write will push a short packet. */
1077 if (call->nFree == 0 && cp) {
1078 clock_NewTime(); /* Bogus: need new time package */
1079 /* The 0, below, specifies that it is not the last packet:
1080 * there will be others. PrepareSendPacket may
1081 * alter the packet length by up to
1082 * conn->securityMaxTrailerSize */
1083 hadd32(call->bytesSent, cp->length);
1084 rxi_PrepareSendPacket(call, cp, 0);
1085 queue_Append(&tmpq, cp);
1087 /* The head of the iovq is now the current packet */
1089 if (queue_IsEmpty(&call->iovq)) {
1090 call->error = RX_PROTOCOL_ERROR;
1091 cp = call->currentPacket = NULL;
1092 rxi_FreePackets(0, &tmpq);
1095 cp = queue_First(&call->iovq, rx_packet);
1097 call->currentPacket = cp;
1098 call->nFree = cp->length;
1101 (char *)cp->wirevec[1].iov_base +
1102 call->conn->securityHeaderSize;
1104 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1109 /* The next iovec should point to the current position */
1110 if (iov[nextio].iov_base != call->curpos
1111 || iov[nextio].iov_len > (int)call->curlen) {
1112 call->error = RX_PROTOCOL_ERROR;
1114 queue_Prepend(&tmpq, cp);
1115 call->currentPacket = NULL;
1117 rxi_FreePackets(0, &tmpq);
1120 nbytes -= iov[nextio].iov_len;
1121 call->curpos += iov[nextio].iov_len;
1122 call->curlen -= iov[nextio].iov_len;
1123 call->nFree -= iov[nextio].iov_len;
1125 if (call->curlen == 0) {
1126 if (++call->curvec > cp->niovecs) {
1129 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1130 call->curlen = cp->wirevec[call->curvec].iov_len;
1134 } while (nbytes && nextio < nio);
1136 /* Move the packets from the temporary queue onto the transmit queue.
1137 * We may end up with more than call->twind packets on the queue. */
1138 queue_SpliceAppend(&call->tq, &tmpq);
1140 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1141 rxi_Start(0, call, 0, 0);
1144 /* Wait for the length of the transmit queue to fall below call->twind */
1145 while (!call->error && call->tnext + 1 > call->tfirst + call->twind) {
1147 call->startWait = clock_Sec();
1148 #ifdef RX_ENABLE_LOCKS
1149 CV_WAIT(&call->cv_twind, &call->lock);
1151 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1152 osi_rxSleep(&call->twind);
1154 call->startWait = 0;
1160 cp = call->currentPacket = NULL;
1165 return requestCount - nbytes;
1169 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1175 MUTEX_ENTER(&call->lock);
1176 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1177 MUTEX_EXIT(&call->lock);
1182 /* Flush any buffered data to the stream, switch to read mode
1183 * (clients) or to EOF mode (servers) */
1185 rxi_FlushWrite(register struct rx_call *call)
1187 register struct rx_packet *cp = call->currentPacket;
1189 /* Free any packets from the last call to ReadvProc/WritevProc */
1190 if (queue_IsNotEmpty(&call->iovq)) {
1191 rxi_FreePackets(0, &call->iovq);
1194 if (call->mode == RX_MODE_SENDING) {
1197 (call->conn->type ==
1198 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1200 #ifdef RX_KERNEL_TRACE
1202 int glockOwner = ISAFS_GLOCK();
1205 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1206 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1213 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1214 /* Wait until TQ_BUSY is reset before adding any
1215 * packets to the transmit queue
1217 while (call->flags & RX_CALL_TQ_BUSY) {
1218 call->flags |= RX_CALL_TQ_WAIT;
1219 #ifdef RX_ENABLE_LOCKS
1220 CV_WAIT(&call->cv_tq, &call->lock);
1221 #else /* RX_ENABLE_LOCKS */
1222 osi_rxSleep(&call->tq);
1223 #endif /* RX_ENABLE_LOCKS */
1225 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1228 /* cp->length is only supposed to be the user's data */
1229 /* cp->length was already set to (then-current)
1230 * MaxUserDataSize or less. */
1231 cp->length -= call->nFree;
1232 call->currentPacket = (struct rx_packet *)0;
1235 cp = rxi_AllocSendPacket(call, 0);
1237 /* Mode can no longer be MODE_SENDING */
1241 cp->niovecs = 2; /* header + space for rxkad stuff */
1245 /* The 1 specifies that this is the last packet */
1246 hadd32(call->bytesSent, cp->length);
1247 rxi_PrepareSendPacket(call, cp, 1);
1248 queue_Append(&call->tq, cp);
1251 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1252 rxi_Start(0, call, 0, 0);
1257 /* Flush any buffered data to the stream, switch to read mode
1258 * (clients) or to EOF mode (servers) */
1260 rx_FlushWrite(struct rx_call *call)
1264 MUTEX_ENTER(&call->lock);
1265 rxi_FlushWrite(call);
1266 MUTEX_EXIT(&call->lock);