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"
61 #include "rx/rx_internal.h"
63 #include "rx/rx_globals.h"
72 #endif /* AFS_OSF_ENV */
74 # include <sys/types.h>
76 # include <winsock2.h>
77 #else /* !AFS_NT40_ENV */
78 # include <sys/socket.h>
79 # include <sys/file.h>
81 # include <netinet/in.h>
82 # include <sys/stat.h>
83 # include <sys/time.h>
84 #endif /* !AFS_NT40_ENV */
90 # include "rx_clock.h"
91 # include "rx_queue.h"
92 # include "rx_internal.h"
94 # include "rx_globals.h"
98 /* rxdb_fileID is used to identify the lock location, along with line#. */
99 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
100 #endif /* RX_LOCKS_DB */
101 /* rxi_ReadProc -- internal version.
103 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
106 rxi_ReadProc(register struct rx_call *call, register char *buf,
109 register struct rx_packet *cp = call->currentPacket;
110 register struct rx_packet *rp;
111 register int requestCount;
112 register unsigned int t;
114 /* XXXX took out clock_NewTime from here. Was it needed? */
115 requestCount = nbytes;
117 /* Free any packets from the last call to ReadvProc/WritevProc */
118 if (queue_IsNotEmpty(&call->iovq)) {
119 rxi_FreePackets(0, &call->iovq);
123 if (call->nLeft == 0) {
124 /* Get next packet */
126 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
130 if (call->mode == RX_MODE_SENDING) {
131 rxi_FlushWrite(call);
135 if (queue_IsNotEmpty(&call->rq)) {
136 /* Check that next packet available is next in sequence */
137 rp = queue_First(&call->rq, rx_packet);
138 if (rp->header.seq == call->rnext) {
140 register struct rx_connection *conn = call->conn;
142 rp->flags &= ~RX_PKTFLAG_RQ;
144 /* RXS_CheckPacket called to undo RXS_PreparePacket's
145 * work. It may reduce the length of the packet by up
146 * to conn->maxTrailerSize, to reflect the length of the
147 * data + the header. */
149 RXS_CheckPacket(conn->securityObject, call,
151 /* Used to merely shut down the call, but now we
152 * shut down the whole connection since this may
153 * indicate an attempt to hijack it */
155 MUTEX_EXIT(&call->lock);
156 rxi_ConnectionError(conn, error);
157 MUTEX_ENTER(&conn->conn_data_lock);
158 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
159 MUTEX_EXIT(&conn->conn_data_lock);
161 MUTEX_ENTER(&call->lock);
166 cp = call->currentPacket = rp;
167 call->currentPacket->flags |= RX_PKTFLAG_CP;
168 call->curvec = 1; /* 0th vec is always header */
169 /* begin at the beginning [ more or less ], continue
170 * on until the end, then stop. */
172 (char *)cp->wirevec[1].iov_base +
173 call->conn->securityHeaderSize;
175 cp->wirevec[1].iov_len -
176 call->conn->securityHeaderSize;
178 /* Notice that this code works correctly if the data
179 * size is 0 (which it may be--no reply arguments from
180 * server, for example). This relies heavily on the
181 * fact that the code below immediately frees the packet
182 * (no yields, etc.). If it didn't, this would be a
183 * problem because a value of zero for call->nLeft
184 * normally means that there is no read packet */
185 call->nLeft = cp->length;
186 hadd32(call->bytesRcvd, cp->length);
188 /* Send a hard ack for every rxi_HardAckRate+1 packets
189 * consumed. Otherwise schedule an event to send
190 * the hard ack later on.
193 if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
194 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
195 rxevent_Cancel(call->delayedAckEvent, call,
196 RX_CALL_REFCOUNT_DELAY);
197 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
199 struct clock when, now;
202 /* Delay to consolidate ack packets */
203 clock_Add(&when, &rx_hardAckDelay);
204 if (!call->delayedAckEvent
205 || clock_Gt(&call->delayedAckEvent->
207 rxevent_Cancel(call->delayedAckEvent,
209 RX_CALL_REFCOUNT_DELAY);
210 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
211 call->delayedAckEvent =
212 rxevent_PostNow(&when, &now,
213 rxi_SendDelayedAck, call,
223 MTUXXX doesn't there need to be an "else" here ???
225 /* Are there ever going to be any more packets? */
226 if (call->flags & RX_CALL_RECEIVE_DONE) {
227 return requestCount - nbytes;
229 /* Wait for in-sequence packet */
230 call->flags |= RX_CALL_READER_WAIT;
232 call->startWait = clock_Sec();
233 while (call->flags & RX_CALL_READER_WAIT) {
234 #ifdef RX_ENABLE_LOCKS
235 CV_WAIT(&call->cv_rq, &call->lock);
237 osi_rxSleep(&call->rq);
242 #ifdef RX_ENABLE_LOCKS
246 #endif /* RX_ENABLE_LOCKS */
250 /* MTUXXX this should be replaced by some error-recovery code before shipping */
251 /* yes, the following block is allowed to be the ELSE clause (or not) */
252 /* It's possible for call->nLeft to be smaller than any particular
253 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
254 * reflects the size of the buffer. We have to keep track of the
255 * number of bytes read in the length field of the packet struct. On
256 * the final portion of a received packet, it's almost certain that
257 * call->nLeft will be smaller than the final buffer. */
258 while (nbytes && cp) {
259 t = MIN((int)call->curlen, nbytes);
260 t = MIN(t, (int)call->nLeft);
261 memcpy(buf, call->curpos, t);
269 /* out of packet. Get another one. */
270 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
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 */
278 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
280 cp = call->currentPacket = (struct rx_packet *)0;
284 (char *)cp->wirevec[call->curvec].iov_base;
285 call->curlen = cp->wirevec[call->curvec].iov_len;
290 /* user buffer is full, return */
300 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
309 * Free any packets from the last call to ReadvProc/WritevProc.
310 * We do not need the lock because the receiver threads only
311 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
312 * RX_CALL_IOVEC_WAIT is always cleared before returning from
313 * ReadvProc/WritevProc.
315 if (!queue_IsEmpty(&call->iovq)) {
316 rxi_FreePackets(0, &call->iovq);
320 * Most common case, all of the data is in the current iovec.
321 * We do not need the lock because this is the only thread that
322 * updates the curlen, curpos, nLeft fields.
324 * We are relying on nLeft being zero unless the call is in receive mode.
326 tcurlen = call->curlen;
327 tnLeft = call->nLeft;
328 if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
329 tcurpos = call->curpos;
330 memcpy(buf, tcurpos, nbytes);
331 call->curpos = tcurpos + nbytes;
332 call->curlen = tcurlen - nbytes;
333 call->nLeft = tnLeft - nbytes;
336 /* out of packet. Get another one. */
338 MUTEX_ENTER(&call->lock);
339 rxi_FreePacket(call->currentPacket);
340 call->currentPacket = (struct rx_packet *)0;
341 MUTEX_EXIT(&call->lock);
348 MUTEX_ENTER(&call->lock);
349 bytes = rxi_ReadProc(call, buf, nbytes);
350 MUTEX_EXIT(&call->lock);
355 /* Optimization for unmarshalling 32 bit integers */
357 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
366 * Free any packets from the last call to ReadvProc/WritevProc.
367 * We do not need the lock because the receiver threads only
368 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
369 * RX_CALL_IOVEC_WAIT is always cleared before returning from
370 * ReadvProc/WritevProc.
372 if (!queue_IsEmpty(&call->iovq)) {
373 rxi_FreePackets(0, &call->iovq);
377 * Most common case, all of the data is in the current iovec.
378 * We do not need the lock because this is the only thread that
379 * updates the curlen, curpos, nLeft fields.
381 * We are relying on nLeft being zero unless the call is in receive mode.
383 tcurlen = call->curlen;
384 tnLeft = call->nLeft;
385 if (!call->error && tcurlen >= sizeof(afs_int32)
386 && tnLeft >= sizeof(afs_int32)) {
387 tcurpos = call->curpos;
388 memcpy((char *)value, tcurpos, sizeof(afs_int32));
389 call->curpos = tcurpos + sizeof(afs_int32);
390 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
391 call->nLeft = (u_short)(tnLeft - sizeof(afs_int32));
392 if (!call->nLeft && call->currentPacket != NULL) {
393 /* out of packet. Get another one. */
395 MUTEX_ENTER(&call->lock);
396 rxi_FreePacket(call->currentPacket);
397 call->currentPacket = (struct rx_packet *)0;
398 MUTEX_EXIT(&call->lock);
401 return sizeof(afs_int32);
405 MUTEX_ENTER(&call->lock);
406 bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
407 MUTEX_EXIT(&call->lock);
414 * Uses packets in the receive queue to fill in as much of the
415 * current iovec as possible. Does not block if it runs out
416 * of packets to complete the iovec. Return true if an ack packet
417 * was sent, otherwise return false */
419 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
423 register unsigned int t;
424 struct rx_packet *rp;
425 struct rx_packet *curp;
426 struct iovec *call_iov;
427 struct iovec *cur_iov = NULL;
429 curp = call->currentPacket;
431 cur_iov = &curp->wirevec[call->curvec];
433 call_iov = &call->iov[call->iovNext];
435 while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
436 if (call->nLeft == 0) {
437 /* Get next packet */
438 if (queue_IsNotEmpty(&call->rq)) {
439 /* Check that next packet available is next in sequence */
440 rp = queue_First(&call->rq, rx_packet);
441 if (rp->header.seq == call->rnext) {
443 register struct rx_connection *conn = call->conn;
445 rp->flags &= ~RX_PKTFLAG_RQ;
447 /* RXS_CheckPacket called to undo RXS_PreparePacket's
448 * work. It may reduce the length of the packet by up
449 * to conn->maxTrailerSize, to reflect the length of the
450 * data + the header. */
452 RXS_CheckPacket(conn->securityObject, call, rp))) {
453 /* Used to merely shut down the call, but now we
454 * shut down the whole connection since this may
455 * indicate an attempt to hijack it */
457 MUTEX_EXIT(&call->lock);
458 rxi_ConnectionError(conn, error);
459 MUTEX_ENTER(&conn->conn_data_lock);
460 rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
461 MUTEX_EXIT(&conn->conn_data_lock);
463 MUTEX_ENTER(&call->lock);
468 curp = call->currentPacket = rp;
469 call->currentPacket->flags |= RX_PKTFLAG_CP;
470 call->curvec = 1; /* 0th vec is always header */
471 cur_iov = &curp->wirevec[1];
472 /* begin at the beginning [ more or less ], continue
473 * on until the end, then stop. */
475 (char *)curp->wirevec[1].iov_base +
476 call->conn->securityHeaderSize;
478 curp->wirevec[1].iov_len -
479 call->conn->securityHeaderSize;
481 /* Notice that this code works correctly if the data
482 * size is 0 (which it may be--no reply arguments from
483 * server, for example). This relies heavily on the
484 * fact that the code below immediately frees the packet
485 * (no yields, etc.). If it didn't, this would be a
486 * problem because a value of zero for call->nLeft
487 * normally means that there is no read packet */
488 call->nLeft = curp->length;
489 hadd32(call->bytesRcvd, curp->length);
491 /* Send a hard ack for every rxi_HardAckRate+1 packets
492 * consumed. Otherwise schedule an event to send
493 * the hard ack later on.
503 /* It's possible for call->nLeft to be smaller than any particular
504 * iov_len. Usually, recvmsg doesn't change the iov_len, since it
505 * reflects the size of the buffer. We have to keep track of the
506 * number of bytes read in the length field of the packet struct. On
507 * the final portion of a received packet, it's almost certain that
508 * call->nLeft will be smaller than the final buffer. */
509 while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
511 t = MIN((int)call->curlen, call->iovNBytes);
512 t = MIN(t, (int)call->nLeft);
513 call_iov->iov_base = call->curpos;
514 call_iov->iov_len = t;
517 call->iovNBytes -= t;
523 /* out of packet. Get another one. */
524 curp->flags &= ~RX_PKTFLAG_CP;
525 curp->flags |= RX_PKTFLAG_IOVQ;
526 queue_Append(&call->iovq, curp);
527 curp = call->currentPacket = (struct rx_packet *)0;
528 } else if (!call->curlen) {
529 /* need to get another struct iov */
530 if (++call->curvec >= curp->niovecs) {
531 /* current packet is exhausted, get ready for another */
532 /* don't worry about curvec and stuff, they get set somewhere else */
533 curp->flags &= ~RX_PKTFLAG_CP;
534 curp->flags |= RX_PKTFLAG_IOVQ;
535 queue_Append(&call->iovq, curp);
536 curp = call->currentPacket = (struct rx_packet *)0;
540 call->curpos = (char *)cur_iov->iov_base;
541 call->curlen = cur_iov->iov_len;
547 /* If we consumed any packets then check whether we need to
548 * send a hard ack. */
549 if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
550 if (call->nHardAcks > (u_short) rxi_HardAckRate) {
551 rxevent_Cancel(call->delayedAckEvent, call,
552 RX_CALL_REFCOUNT_DELAY);
553 rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
556 struct clock when, now;
559 /* Delay to consolidate ack packets */
560 clock_Add(&when, &rx_hardAckDelay);
561 if (!call->delayedAckEvent
562 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
563 rxevent_Cancel(call->delayedAckEvent, call,
564 RX_CALL_REFCOUNT_DELAY);
565 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
566 call->delayedAckEvent =
567 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
575 /* rxi_ReadvProc -- internal version.
577 * Fills in an iovec with pointers to the packet buffers. All packets
578 * except the last packet (new current packet) are moved to the iovq
579 * while the application is processing the data.
581 * LOCKS USED -- called at netpri with rx global lock and call->lock held.
584 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
590 requestCount = nbytes;
593 /* Free any packets from the last call to ReadvProc/WritevProc */
594 if (queue_IsNotEmpty(&call->iovq)) {
595 rxi_FreePackets(0, &call->iovq);
598 if (call->mode == RX_MODE_SENDING) {
599 rxi_FlushWrite(call);
606 /* Get whatever data is currently available in the receive queue.
607 * If rxi_FillReadVec sends an ack packet then it is possible
608 * that we will receive more data while we drop the call lock
609 * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
610 * here to avoid a race with the receive thread if we send
611 * hard acks in rxi_FillReadVec. */
612 call->flags |= RX_CALL_IOVEC_WAIT;
613 call->iovNBytes = nbytes;
614 call->iovMax = maxio;
617 rxi_FillReadVec(call, 0);
619 /* if we need more data then sleep until the receive thread has
620 * filled in the rest. */
621 if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
622 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
623 call->flags |= RX_CALL_READER_WAIT;
625 call->startWait = clock_Sec();
626 while (call->flags & RX_CALL_READER_WAIT) {
627 #ifdef RX_ENABLE_LOCKS
628 CV_WAIT(&call->cv_rq, &call->lock);
630 osi_rxSleep(&call->rq);
635 call->flags &= ~RX_CALL_IOVEC_WAIT;
636 #ifdef RX_ENABLE_LOCKS
640 #endif /* RX_ENABLE_LOCKS */
643 *nio = call->iovNext;
644 return nbytes - call->iovNBytes;
648 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
655 MUTEX_ENTER(&call->lock);
656 bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
657 MUTEX_EXIT(&call->lock);
662 /* rxi_WriteProc -- internal version.
664 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
667 rxi_WriteProc(register struct rx_call *call, register char *buf,
670 struct rx_connection *conn = call->conn;
671 register struct rx_packet *cp = call->currentPacket;
672 register unsigned int t;
673 int requestCount = nbytes;
675 /* Free any packets from the last call to ReadvProc/WritevProc */
676 if (queue_IsNotEmpty(&call->iovq)) {
677 rxi_FreePackets(0, &call->iovq);
680 if (call->mode != RX_MODE_SENDING) {
681 if ((conn->type == RX_SERVER_CONNECTION)
682 && (call->mode == RX_MODE_RECEIVING)) {
683 call->mode = RX_MODE_SENDING;
685 cp->flags &= ~RX_PKTFLAG_CP;
687 cp = call->currentPacket = (struct rx_packet *)0;
696 /* Loop condition is checked at end, so that a write of 0 bytes
697 * will force a packet to be created--specially for the case where
698 * there are 0 bytes on the stream, but we must send a packet
701 if (call->nFree == 0) {
702 if (!call->error && cp) {
703 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
704 /* Wait until TQ_BUSY is reset before adding any
705 * packets to the transmit queue
707 while (call->flags & RX_CALL_TQ_BUSY) {
708 call->flags |= RX_CALL_TQ_WAIT;
709 #ifdef RX_ENABLE_LOCKS
710 CV_WAIT(&call->cv_tq, &call->lock);
711 #else /* RX_ENABLE_LOCKS */
712 osi_rxSleep(&call->tq);
713 #endif /* RX_ENABLE_LOCKS */
715 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
716 clock_NewTime(); /* Bogus: need new time package */
717 /* The 0, below, specifies that it is not the last packet:
718 * there will be others. PrepareSendPacket may
719 * alter the packet length by up to
720 * conn->securityMaxTrailerSize */
721 hadd32(call->bytesSent, cp->length);
722 rxi_PrepareSendPacket(call, cp, 0);
723 cp->flags &= ~RX_PKTFLAG_CP;
724 cp->flags |= RX_PKTFLAG_TQ;
725 queue_Append(&call->tq, cp);
726 cp = call->currentPacket = (struct rx_packet *)0;
729 flags & (RX_CALL_FAST_RECOVER |
730 RX_CALL_FAST_RECOVER_WAIT))) {
731 rxi_Start(0, call, 0, 0);
734 cp->flags &= ~RX_PKTFLAG_CP;
736 cp = call->currentPacket = (struct rx_packet *)0;
738 /* Wait for transmit window to open up */
740 && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
742 call->startWait = clock_Sec();
744 #ifdef RX_ENABLE_LOCKS
745 CV_WAIT(&call->cv_twind, &call->lock);
747 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
748 osi_rxSleep(&call->twind);
752 #ifdef RX_ENABLE_LOCKS
756 #endif /* RX_ENABLE_LOCKS */
758 if ((cp = rxi_AllocSendPacket(call, nbytes))) {
759 cp->flags |= RX_PKTFLAG_CP;
760 call->currentPacket = cp;
761 call->nFree = cp->length;
762 call->curvec = 1; /* 0th vec is always header */
763 /* begin at the beginning [ more or less ], continue
764 * on until the end, then stop. */
766 (char *)cp->wirevec[1].iov_base +
767 call->conn->securityHeaderSize;
769 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
773 cp->flags &= ~RX_PKTFLAG_CP;
775 call->currentPacket = NULL;
781 if (cp && (int)call->nFree < nbytes) {
782 /* Try to extend the current buffer */
783 register int len, mud;
785 mud = rx_MaxUserDataSize(call);
788 want = MIN(nbytes - (int)call->nFree, mud - len);
789 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
790 if (cp->length > (unsigned)mud)
792 call->nFree += (cp->length - len);
796 /* If the remaining bytes fit in the buffer, then store them
797 * and return. Don't ship a buffer that's full immediately to
798 * the peer--we don't know if it's the last buffer yet */
804 while (nbytes && call->nFree) {
806 t = MIN((int)call->curlen, nbytes);
807 t = MIN((int)call->nFree, t);
808 memcpy(call->curpos, buf, t);
812 call->curlen -= (u_short)t;
813 call->nFree -= (u_short)t;
816 /* need to get another struct iov */
817 if (++call->curvec >= cp->niovecs) {
818 /* current packet is full, extend or send it */
821 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
822 call->curlen = cp->wirevec[call->curvec].iov_len;
825 } /* while bytes to send and room to send them */
827 /* might be out of space now */
830 } else; /* more data to send, so get another packet and keep going */
833 return requestCount - nbytes;
837 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
846 * Free any packets from the last call to ReadvProc/WritevProc.
847 * We do not need the lock because the receiver threads only
848 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
849 * RX_CALL_IOVEC_WAIT is always cleared before returning from
850 * ReadvProc/WritevProc.
852 if (queue_IsNotEmpty(&call->iovq)) {
853 rxi_FreePackets(0, &call->iovq);
857 * Most common case: all of the data fits in the current iovec.
858 * We do not need the lock because this is the only thread that
859 * updates the curlen, curpos, nFree fields.
861 * We are relying on nFree being zero unless the call is in send mode.
863 tcurlen = (int)call->curlen;
864 tnFree = (int)call->nFree;
865 if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
866 tcurpos = call->curpos;
867 memcpy(tcurpos, buf, nbytes);
868 call->curpos = tcurpos + nbytes;
869 call->curlen = (u_short)(tcurlen - nbytes);
870 call->nFree = (u_short)(tnFree - nbytes);
875 MUTEX_ENTER(&call->lock);
876 bytes = rxi_WriteProc(call, buf, nbytes);
877 MUTEX_EXIT(&call->lock);
882 /* Optimization for marshalling 32 bit arguments */
884 rx_WriteProc32(register struct rx_call *call, register afs_int32 * value)
893 * Free any packets from the last call to ReadvProc/WritevProc.
894 * We do not need the lock because the receiver threads only
895 * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
896 * RX_CALL_IOVEC_WAIT is always cleared before returning from
897 * ReadvProc/WritevProc.
899 if (queue_IsNotEmpty(&call->iovq)) {
900 rxi_FreePackets(0, &call->iovq);
904 * Most common case: all of the data fits in the current iovec.
905 * We do not need the lock because this is the only thread that
906 * updates the curlen, curpos, nFree fields.
908 * We are relying on nFree being zero unless the call is in send mode.
910 tcurlen = call->curlen;
911 tnFree = call->nFree;
912 if (!call->error && tcurlen >= sizeof(afs_int32)
913 && tnFree >= sizeof(afs_int32)) {
914 tcurpos = call->curpos;
915 if (!((size_t)tcurpos & (sizeof(afs_int32) - 1))) {
916 *((afs_int32 *) (tcurpos)) = *value;
918 memcpy(tcurpos, (char *)value, sizeof(afs_int32));
920 call->curpos = tcurpos + sizeof(afs_int32);
921 call->curlen = (u_short)(tcurlen - sizeof(afs_int32));
922 call->nFree = (u_short)(tnFree - sizeof(afs_int32));
923 return sizeof(afs_int32);
927 MUTEX_ENTER(&call->lock);
928 bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
929 MUTEX_EXIT(&call->lock);
934 /* rxi_WritevAlloc -- internal version.
936 * Fill in an iovec to point to data in packet buffers. The application
937 * calls rxi_WritevProc when the buffers are full.
939 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
942 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
945 struct rx_connection *conn = call->conn;
946 struct rx_packet *cp = call->currentPacket;
949 /* Temporary values, real work is done in rxi_WritevProc */
955 requestCount = nbytes;
958 /* Free any packets from the last call to ReadvProc/WritevProc */
959 if (queue_IsNotEmpty(&call->iovq)) {
960 rxi_FreePackets(0, &call->iovq);
963 if (call->mode != RX_MODE_SENDING) {
964 if ((conn->type == RX_SERVER_CONNECTION)
965 && (call->mode == RX_MODE_RECEIVING)) {
966 call->mode = RX_MODE_SENDING;
968 cp->flags &= ~RX_PKTFLAG_CP;
970 cp = call->currentPacket = (struct rx_packet *)0;
979 /* Set up the iovec to point to data in packet buffers. */
980 tnFree = call->nFree;
981 tcurvec = call->curvec;
982 tcurpos = call->curpos;
983 tcurlen = call->curlen;
985 register unsigned int t;
988 /* current packet is full, allocate a new one */
989 cp = rxi_AllocSendPacket(call, nbytes);
991 /* out of space, return what we have */
993 return requestCount - nbytes;
995 cp->flags |= RX_PKTFLAG_IOVQ;
996 queue_Append(&call->iovq, cp);
1000 (char *)cp->wirevec[1].iov_base +
1001 call->conn->securityHeaderSize;
1002 tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1005 if (tnFree < nbytes) {
1006 /* try to extend the current packet */
1007 register int len, mud;
1009 mud = rx_MaxUserDataSize(call);
1012 want = MIN(nbytes - tnFree, mud - len);
1013 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1014 if (cp->length > (unsigned)mud)
1016 tnFree += (cp->length - len);
1017 if (cp == call->currentPacket) {
1018 call->nFree += (cp->length - len);
1023 /* fill in the next entry in the iovec */
1024 t = MIN(tcurlen, nbytes);
1026 iov[nextio].iov_base = tcurpos;
1027 iov[nextio].iov_len = t;
1035 /* need to get another struct iov */
1036 if (++tcurvec >= cp->niovecs) {
1037 /* current packet is full, extend it or move on to next packet */
1040 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1041 tcurlen = cp->wirevec[tcurvec].iov_len;
1044 } while (nbytes && nextio < maxio);
1046 return requestCount - nbytes;
1050 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1057 MUTEX_ENTER(&call->lock);
1058 bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1059 MUTEX_EXIT(&call->lock);
1064 /* rxi_WritevProc -- internal version.
1066 * Send buffers allocated in rxi_WritevAlloc.
1068 * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1071 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1073 struct rx_packet *cp = call->currentPacket;
1076 struct rx_queue tmpq;
1078 requestCount = nbytes;
1081 if (call->mode != RX_MODE_SENDING) {
1082 call->error = RX_PROTOCOL_ERROR;
1084 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1085 /* Wait until TQ_BUSY is reset before trying to move any
1086 * packets to the transmit queue. */
1087 while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1088 call->flags |= RX_CALL_TQ_WAIT;
1089 #ifdef RX_ENABLE_LOCKS
1090 CV_WAIT(&call->cv_tq, &call->lock);
1091 #else /* RX_ENABLE_LOCKS */
1092 osi_rxSleep(&call->tq);
1093 #endif /* RX_ENABLE_LOCKS */
1095 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1099 cp->flags &= ~RX_PKTFLAG_CP;
1100 cp->flags |= RX_PKTFLAG_IOVQ;
1101 queue_Prepend(&call->iovq, cp);
1102 cp = call->currentPacket = (struct rx_packet *)0;
1104 rxi_FreePackets(0, &call->iovq);
1108 /* Loop through the I/O vector adjusting packet pointers.
1109 * Place full packets back onto the iovq once they are ready
1110 * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1111 * the iovec. We put the loop condition at the end to ensure that
1112 * a zero length write will push a short packet. */
1116 if (call->nFree == 0 && cp) {
1117 clock_NewTime(); /* Bogus: need new time package */
1118 /* The 0, below, specifies that it is not the last packet:
1119 * there will be others. PrepareSendPacket may
1120 * alter the packet length by up to
1121 * conn->securityMaxTrailerSize */
1122 hadd32(call->bytesSent, cp->length);
1123 rxi_PrepareSendPacket(call, cp, 0);
1124 cp->flags |= RX_PKTFLAG_TQ;
1125 queue_Append(&tmpq, cp);
1126 cp = call->currentPacket = (struct rx_packet *)0;
1128 /* The head of the iovq is now the current packet */
1130 if (queue_IsEmpty(&call->iovq)) {
1131 call->error = RX_PROTOCOL_ERROR;
1132 rxi_FreePackets(0, &tmpq);
1135 cp = queue_First(&call->iovq, rx_packet);
1137 cp->flags &= ~RX_PKTFLAG_IOVQ;
1138 cp->flags |= RX_PKTFLAG_CP;
1139 call->currentPacket = cp;
1140 call->nFree = cp->length;
1143 (char *)cp->wirevec[1].iov_base +
1144 call->conn->securityHeaderSize;
1146 cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1151 /* The next iovec should point to the current position */
1152 if (iov[nextio].iov_base != call->curpos
1153 || iov[nextio].iov_len > (int)call->curlen) {
1154 call->error = RX_PROTOCOL_ERROR;
1156 cp->flags &= ~RX_PKTFLAG_CP;
1157 queue_Prepend(&tmpq, cp);
1158 cp = call->currentPacket = (struct rx_packet *)0;
1160 rxi_FreePackets(0, &tmpq);
1163 nbytes -= iov[nextio].iov_len;
1164 call->curpos += iov[nextio].iov_len;
1165 call->curlen -= iov[nextio].iov_len;
1166 call->nFree -= iov[nextio].iov_len;
1168 if (call->curlen == 0) {
1169 if (++call->curvec > cp->niovecs) {
1172 call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1173 call->curlen = cp->wirevec[call->curvec].iov_len;
1177 } while (nbytes && nextio < nio);
1179 /* Move the packets from the temporary queue onto the transmit queue.
1180 * We may end up with more than call->twind packets on the queue. */
1181 queue_SpliceAppend(&call->tq, &tmpq);
1183 if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1184 rxi_Start(0, call, 0, 0);
1187 /* Wait for the length of the transmit queue to fall below call->twind */
1188 while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) {
1190 call->startWait = clock_Sec();
1191 #ifdef RX_ENABLE_LOCKS
1192 CV_WAIT(&call->cv_twind, &call->lock);
1194 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1195 osi_rxSleep(&call->twind);
1197 call->startWait = 0;
1202 cp->flags &= ~RX_PKTFLAG_CP;
1204 cp = call->currentPacket = (struct rx_packet *)0;
1209 return requestCount - nbytes;
1213 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1219 MUTEX_ENTER(&call->lock);
1220 bytes = rxi_WritevProc(call, iov, nio, nbytes);
1221 MUTEX_EXIT(&call->lock);
1226 /* Flush any buffered data to the stream, switch to read mode
1227 * (clients) or to EOF mode (servers) */
1229 rxi_FlushWrite(register struct rx_call *call)
1231 register struct rx_packet *cp = call->currentPacket;
1233 /* Free any packets from the last call to ReadvProc/WritevProc */
1234 if (queue_IsNotEmpty(&call->iovq)) {
1235 rxi_FreePackets(0, &call->iovq);
1238 if (call->mode == RX_MODE_SENDING) {
1241 (call->conn->type ==
1242 RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1244 #ifdef RX_KERNEL_TRACE
1246 int glockOwner = ISAFS_GLOCK();
1249 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1250 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1257 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1258 /* Wait until TQ_BUSY is reset before adding any
1259 * packets to the transmit queue
1261 while (call->flags & RX_CALL_TQ_BUSY) {
1262 call->flags |= RX_CALL_TQ_WAIT;
1263 #ifdef RX_ENABLE_LOCKS
1264 CV_WAIT(&call->cv_tq, &call->lock);
1265 #else /* RX_ENABLE_LOCKS */
1266 osi_rxSleep(&call->tq);
1267 #endif /* RX_ENABLE_LOCKS */
1269 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1272 /* cp->length is only supposed to be the user's data */
1273 /* cp->length was already set to (then-current)
1274 * MaxUserDataSize or less. */
1275 cp->flags &= ~RX_PKTFLAG_CP;
1276 cp->length -= call->nFree;
1277 call->currentPacket = (struct rx_packet *)0;
1280 cp = rxi_AllocSendPacket(call, 0);
1282 /* Mode can no longer be MODE_SENDING */
1286 cp->niovecs = 2; /* header + space for rxkad stuff */
1290 /* The 1 specifies that this is the last packet */
1291 hadd32(call->bytesSent, cp->length);
1292 rxi_PrepareSendPacket(call, cp, 1);
1293 cp->flags |= RX_PKTFLAG_TQ;
1294 queue_Append(&call->tq, cp);
1297 flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1298 rxi_Start(0, call, 0, 0);
1303 /* Flush any buffered data to the stream, switch to read mode
1304 * (clients) or to EOF mode (servers) */
1306 rx_FlushWrite(struct rx_call *call)
1310 MUTEX_ENTER(&call->lock);
1311 rxi_FlushWrite(call);
1312 MUTEX_EXIT(&call->lock);