int rxi_WriteProc(struct rx_call *call, char *buf, int nbytes) { struct rx_connection *conn = call->conn; unsigned int t; int requestCount = nbytes; /* Free any packets from the last call to ReadvProc/WritevProc */ if (!opr_queue_IsEmpty(&call->app.iovq)) { #ifdef RXDEBUG_PACKET call->iovqc -= #endif /* RXDEBUG_PACKET */ rxi_FreePackets(0, &call->app.iovq); } if (call->app.mode != RX_MODE_SENDING) { if ((conn->type == RX_SERVER_CONNECTION) && (call->app.mode == RX_MODE_RECEIVING)) { call->app.mode = RX_MODE_SENDING; if (call->app.currentPacket) { #ifdef RX_TRACK_PACKETS call->app.currentPacket->flags &= ~RX_PKTFLAG_CP; #endif rxi_FreePacket(call->app.currentPacket); call->app.currentPacket = NULL; call->app.nLeft = 0; call->app.nFree = 0; } } else { return 0; } } /* Loop condition is checked at end, so that a write of 0 bytes * will force a packet to be created--specially for the case where * there are 0 bytes on the stream, but we must send a packet * anyway. */ do { if (call->app.nFree == 0) { MUTEX_ENTER(&call->lock); if (call->error) call->app.mode = RX_MODE_ERROR; if (!call->error && call->app.currentPacket) { clock_NewTime(); /* Bogus: need new time package */ /* The 0, below, specifies that it is not the last packet: * there will be others. PrepareSendPacket may * alter the packet length by up to * conn->securityMaxTrailerSize */ call->app.bytesSent += call->app.currentPacket->length; rxi_PrepareSendPacket(call, call->app.currentPacket, 0); /* PrepareSendPacket drops the call lock */ rxi_WaitforTQBusy(call); #ifdef RX_TRACK_PACKETS call->app.currentPacket->flags |= RX_PKTFLAG_TQ; #endif opr_queue_Append(&call->tq, &call->app.currentPacket->entry); #ifdef RXDEBUG_PACKET call->tqc++; #endif /* RXDEBUG_PACKET */ #ifdef RX_TRACK_PACKETS call->app.currentPacket->flags &= ~RX_PKTFLAG_CP; #endif call->app.currentPacket = NULL; /* If the call is in recovery, let it exhaust its current * retransmit queue before forcing it to send new packets */ if (!(call->flags & (RX_CALL_FAST_RECOVER))) { rxi_Start(call, 0); } } else if (call->app.currentPacket) { #ifdef RX_TRACK_PACKETS call->app.currentPacket->flags &= ~RX_PKTFLAG_CP; #endif rxi_FreePacket(call->app.currentPacket); call->app.currentPacket = NULL; } /* Wait for transmit window to open up */ while (!call->error && call->tnext + 1 > call->tfirst + (2 * call->twind)) { clock_NewTime(); call->startWait = clock_Sec(); #ifdef RX_ENABLE_LOCKS CV_WAIT(&call->cv_twind, &call->lock); #else call->flags |= RX_CALL_WAIT_WINDOW_ALLOC; osi_rxSleep(&call->twind); #endif call->startWait = 0; #ifdef RX_ENABLE_LOCKS if (call->error) { call->app.mode = RX_MODE_ERROR; MUTEX_EXIT(&call->lock); return 0; } #endif /* RX_ENABLE_LOCKS */ } if ((call->app.currentPacket = rxi_AllocSendPacket(call, nbytes))) { #ifdef RX_TRACK_PACKETS call->app.currentPacket->flags |= RX_PKTFLAG_CP; #endif call->app.nFree = call->app.currentPacket->length; call->app.curvec = 1; /* 0th vec is always header */ /* begin at the beginning [ more or less ], continue * on until the end, then stop. */ call->app.curpos = (char *) call->app.currentPacket->wirevec[1].iov_base + call->conn->securityHeaderSize; call->app.curlen = call->app.currentPacket->wirevec[1].iov_len - call->conn->securityHeaderSize; } if (call->error) { call->app.mode = RX_MODE_ERROR; if (call->app.currentPacket) { #ifdef RX_TRACK_PACKETS call->app.currentPacket->flags &= ~RX_PKTFLAG_CP; #endif rxi_FreePacket(call->app.currentPacket); call->app.currentPacket = NULL; } MUTEX_EXIT(&call->lock); return 0; } MUTEX_EXIT(&call->lock); } if (call->app.currentPacket && (int)call->app.nFree < nbytes) { /* Try to extend the current buffer */ int len, mud; len = call->app.currentPacket->length; mud = rx_MaxUserDataSize(call); if (mud > len) { int want; want = MIN(nbytes - (int)call->app.nFree, mud - len); rxi_AllocDataBuf(call->app.currentPacket, want, RX_PACKET_CLASS_SEND_CBUF); if (call->app.currentPacket->length > (unsigned)mud) call->app.currentPacket->length = mud; call->app.nFree += (call->app.currentPacket->length - len); } } /* If the remaining bytes fit in the buffer, then store them * and return. Don't ship a buffer that's full immediately to * the peer--we don't know if it's the last buffer yet */ if (!call->app.currentPacket) { call->app.nFree = 0; } while (nbytes && call->app.nFree) { t = MIN((int)call->app.curlen, nbytes); t = MIN((int)call->app.nFree, t); memcpy(call->app.curpos, buf, t); buf += t; nbytes -= t; call->app.curpos += t; call->app.curlen -= (u_short)t; call->app.nFree -= (u_short)t; if (!call->app.curlen) { /* need to get another struct iov */ if (++call->app.curvec >= call->app.currentPacket->niovecs) { /* current packet is full, extend or send it */ call->app.nFree = 0; } else { call->app.curpos = call->app.currentPacket->wirevec[call->app.curvec].iov_base; call->app.curlen = call->app.currentPacket->wirevec[call->app.curvec].iov_len; } } } /* while bytes to send and room to send them */ /* might be out of space now */ if (!nbytes) { return requestCount; } else; /* more data to send, so get another packet and keep going */ } while (nbytes); return requestCount - nbytes; }
static int rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio, int nbytes) { struct rx_connection *conn = call->conn; struct rx_packet *cp; int requestCount; int nextio; /* Temporary values, real work is done in rxi_WritevProc */ int tnFree; unsigned int tcurvec; char *tcurpos; int tcurlen; requestCount = nbytes; nextio = 0; /* Free any packets from the last call to ReadvProc/WritevProc */ if (!opr_queue_IsEmpty(&call->app.iovq)) { #ifdef RXDEBUG_PACKET call->iovqc -= #endif /* RXDEBUG_PACKET */ rxi_FreePackets(0, &call->app.iovq); } if (call->app.mode != RX_MODE_SENDING) { if ((conn->type == RX_SERVER_CONNECTION) && (call->app.mode == RX_MODE_RECEIVING)) { call->app.mode = RX_MODE_SENDING; if (call->app.currentPacket) { #ifdef RX_TRACK_PACKETS call->app.currentPacket->flags &= ~RX_PKTFLAG_CP; #endif rxi_FreePacket(call->app.currentPacket); call->app.currentPacket = NULL; call->app.nLeft = 0; call->app.nFree = 0; } } else { return 0; } } /* Set up the iovec to point to data in packet buffers. */ tnFree = call->app.nFree; tcurvec = call->app.curvec; tcurpos = call->app.curpos; tcurlen = call->app.curlen; cp = call->app.currentPacket; do { int t; if (tnFree == 0) { /* current packet is full, allocate a new one */ MUTEX_ENTER(&call->lock); cp = rxi_AllocSendPacket(call, nbytes); MUTEX_EXIT(&call->lock); if (cp == NULL) { /* out of space, return what we have */ *nio = nextio; return requestCount - nbytes; } #ifdef RX_TRACK_PACKETS cp->flags |= RX_PKTFLAG_IOVQ; #endif opr_queue_Append(&call->app.iovq, &cp->entry); #ifdef RXDEBUG_PACKET call->iovqc++; #endif /* RXDEBUG_PACKET */ tnFree = cp->length; tcurvec = 1; tcurpos = (char *)cp->wirevec[1].iov_base + call->conn->securityHeaderSize; tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize; } if (tnFree < nbytes) { /* try to extend the current packet */ int len, mud; len = cp->length; mud = rx_MaxUserDataSize(call); if (mud > len) { int want; want = MIN(nbytes - tnFree, mud - len); rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF); if (cp->length > (unsigned)mud) cp->length = mud; tnFree += (cp->length - len); if (cp == call->app.currentPacket) { call->app.nFree += (cp->length - len); } } } /* fill in the next entry in the iovec */ t = MIN(tcurlen, nbytes); t = MIN(tnFree, t); iov[nextio].iov_base = tcurpos; iov[nextio].iov_len = t; nbytes -= t; tcurpos += t; tcurlen -= t; tnFree -= t; nextio++; if (!tcurlen) { /* need to get another struct iov */ if (++tcurvec >= cp->niovecs) { /* current packet is full, extend it or move on to next packet */ tnFree = 0; } else { tcurpos = (char *)cp->wirevec[tcurvec].iov_base; tcurlen = cp->wirevec[tcurvec].iov_len; } } } while (nbytes && nextio < maxio); *nio = nextio; return requestCount - nbytes; }
/* Flush any buffered data to the stream, switch to read mode * (clients) or to EOF mode (servers) * * LOCKS HELD: called at netpri. */ void rxi_FlushWrite(struct rx_call *call) { struct rx_packet *cp = NULL; /* Free any packets from the last call to ReadvProc/WritevProc */ if (queue_IsNotEmpty(&call->iovq)) { #ifdef RXDEBUG_PACKET call->iovqc -= #endif /* RXDEBUG_PACKET */ rxi_FreePackets(0, &call->iovq); } if (call->mode == RX_MODE_SENDING) { call->mode = (call->conn->type == RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF); #ifdef RX_KERNEL_TRACE { int glockOwner = ISAFS_GLOCK(); if (!glockOwner) AFS_GLOCK(); afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING, __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER, call); if (!glockOwner) AFS_GUNLOCK(); } #endif MUTEX_ENTER(&call->lock); #ifdef AFS_GLOBAL_RXLOCK_KERNEL rxi_WaitforTQBusy(call); #endif /* AFS_GLOBAL_RXLOCK_KERNEL */ if (call->error) call->mode = RX_MODE_ERROR; cp = call->currentPacket; if (cp) { /* cp->length is only supposed to be the user's data */ /* cp->length was already set to (then-current) * MaxUserDataSize or less. */ #ifdef RX_TRACK_PACKETS cp->flags &= ~RX_PKTFLAG_CP; #endif cp->length -= call->nFree; call->currentPacket = (struct rx_packet *)0; call->nFree = 0; } else { cp = rxi_AllocSendPacket(call, 0); if (!cp) { /* Mode can no longer be MODE_SENDING */ return; } cp->length = 0; cp->niovecs = 2; /* header + space for rxkad stuff */ call->nFree = 0; } /* The 1 specifies that this is the last packet */ hadd32(call->bytesSent, cp->length); rxi_PrepareSendPacket(call, cp, 1); #ifdef RX_TRACK_PACKETS cp->flags |= RX_PKTFLAG_TQ; #endif queue_Append(&call->tq, cp); #ifdef RXDEBUG_PACKET call->tqc++; #endif /* RXDEBUG_PACKET */ if (! (call-> flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) { rxi_Start(0, call, 0, 0); } MUTEX_EXIT(&call->lock); } }