rxi-newcall-avoid-race-20020115
[openafs.git] / src / rx / rx.c
index ae6e505..c5a68ae 100644 (file)
@@ -9,8 +9,16 @@
 
 /* RX:  Extended Remote Procedure Call */
 
+#include <afsconfig.h>
 #ifdef KERNEL
 #include "../afs/param.h"
+#else
+#include <afs/param.h>
+#endif
+
+RCSID("$Header$");
+
+#ifdef KERNEL
 #include "../afs/sysincludes.h"
 #include "../afs/afsincludes.h"
 #ifndef UKERNEL
@@ -66,7 +74,6 @@ extern afs_int32 afs_termState;
 #endif /* AFS_AIX41_ENV */
 # include "../afsint/rxgen_consts.h"
 #else /* KERNEL */
-# include <afs/param.h>
 # include <sys/types.h>
 # include <errno.h>
 #ifdef AFS_NT40_ENV
@@ -81,6 +88,13 @@ extern afs_int32 afs_termState;
 # include <netinet/in.h>
 # include <sys/time.h>
 #endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#else
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#endif
 # include "rx.h"
 # include "rx_user.h"
 # include "rx_clock.h"
@@ -91,10 +105,6 @@ extern afs_int32 afs_termState;
 # include <afs/rxgen_consts.h>
 #endif /* KERNEL */
 
-#ifdef RXDEBUG
-extern afs_uint32 LWP_ThreadId();
-#endif /* RXDEBUG */
-
 int (*registerProgram)() = 0;
 int (*swapNameProgram)() = 0;
 
@@ -217,8 +227,6 @@ assert(pthread_once(&rx_once_init, rxi_InitPthread)==0);
 #define INIT_PTHREAD_LOCKS
 #endif
 
-extern void rxi_DeleteCachedConnections(void);
-
 
 /* Variables for handling the minProcs implementation.  availProcs gives the
  * number of threads available in the pool at this moment (not counting dudes
@@ -242,11 +250,8 @@ extern void rxi_DeleteCachedConnections(void);
  * to manipulate the queue.
  */
 
-extern void rxi_Delay(int);
-
-static int rxi_ServerThreadSelectingCall;
-
 #ifdef RX_ENABLE_LOCKS
+static int rxi_ServerThreadSelectingCall;
 static afs_kmutex_t rx_rpc_stats;
 void rxi_StartUnlocked();
 #endif
@@ -296,8 +301,6 @@ void osirx_AssertMine(afs_kmutex_t *lockaddr, char *msg);
 #define CLEAR_CALL_QUEUE_LOCK(C)
 #endif /* RX_ENABLE_LOCKS */
 static void rxi_DestroyConnectionNoLock();
-void rxi_DestroyConnection();
-void rxi_CleanupConnection();
 struct rx_serverQueueEntry *rx_waitForPacket = 0;
 
 /* ------------Exported Interfaces------------- */
@@ -360,6 +363,10 @@ int rx_Init(u_int port)
     char *htable, *ptable;
     int tmp_status;
 
+#if defined(AFS_DJGPP_ENV) && !defined(DEBUG)
+    __djgpp_set_quiet_socket(1);
+#endif
+
     SPLVAR;
 
     INIT_PTHREAD_LOCKS
@@ -424,14 +431,14 @@ int rx_Init(u_int port)
     rxi_nCalls = 0;
     rx_connDeadTime = 12;
     rx_tranquil     = 0;       /* reset flag */
-    bzero((char *)&rx_stats, sizeof(struct rx_stats));
+    memset((char *)&rx_stats, 0, sizeof(struct rx_stats));
     htable = (char *)
        osi_Alloc(rx_hashTableSize*sizeof(struct rx_connection *));
     PIN(htable, rx_hashTableSize*sizeof(struct rx_connection *));  /* XXXXX */
-    bzero(htable, rx_hashTableSize*sizeof(struct rx_connection *));
+    memset(htable, 0, rx_hashTableSize*sizeof(struct rx_connection *));
     ptable =  (char *) osi_Alloc(rx_hashTableSize*sizeof(struct rx_peer *));   
     PIN(ptable, rx_hashTableSize*sizeof(struct rx_peer *));      /* XXXXX */
-    bzero(ptable, rx_hashTableSize*sizeof(struct rx_peer *));
+    memset(ptable, 0, rx_hashTableSize*sizeof(struct rx_peer *));
 
     /* Malloc up a bunch of packets & buffers */
     rx_nFreePackets = 0;
@@ -562,7 +569,7 @@ register struct rx_service *aservice;
 }
 
 #else /* RX_ENABLE_LOCKS */
-static QuotaOK(aservice)
+static int QuotaOK(aservice)
 register struct rx_service *aservice; {
     int rc=0;
     /* under min quota, we're OK */
@@ -621,7 +628,7 @@ void rxi_StartServerProcs(nExistingProcs)
 void rx_StartServer(donateMe)
 {
     register struct rx_service *service;
-    register int i, nProcs;
+    register int i, nProcs=0;
     SPLVAR;
     clock_NewTime();
 
@@ -658,14 +665,13 @@ void rx_StartServer(donateMe)
     if (donateMe) {
 #ifndef AFS_NT40_ENV
 #ifndef KERNEL
-       int code;
         char name[32];
 #ifdef AFS_PTHREAD_ENV
         pid_t pid;
-        pid = pthread_self();
+        pid = (pid_t) pthread_self();
 #else /* AFS_PTHREAD_ENV */
         PROCESS pid;
-        code = LWP_CurrentProcess(&pid);
+        LWP_CurrentProcess(&pid);
 #endif /* AFS_PTHREAD_ENV */
 
         sprintf(name,"srv_%d", ++nProcs);
@@ -859,7 +865,7 @@ static void rxi_DestroyConnectionNoLock(conn)
        MUTEX_EXIT(&rx_stats_mutex);
     }
 
-    if (conn->refCount > 0) {
+    if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
        /* Busy; wait till the last guy before proceeding */
        MUTEX_EXIT(&conn->conn_data_lock);
        USERPRI;
@@ -891,7 +897,12 @@ static void rxi_DestroyConnectionNoLock(conn)
                     * last reply packets */
                    rxevent_Cancel(call->delayedAckEvent, call,
                                   RX_CALL_REFCOUNT_DELAY);
-                   rxi_AckAll((struct rxevent *)0, call, 0);
+                   if (call->state == RX_STATE_PRECALL ||
+                       call->state == RX_STATE_ACTIVE) {
+                       rxi_SendDelayedAck(call->delayedAckEvent, call, 0);
+                   } else {
+                       rxi_AckAll((struct rxevent *)0, call, 0);
+                   }
                }
                MUTEX_EXIT(&call->lock);
            }
@@ -995,6 +1006,21 @@ struct rx_call *rx_NewCall(conn)
     clock_GetTime(&queueTime);
     AFS_RXGLOCK();
     MUTEX_ENTER(&conn->conn_call_lock);
+
+    /*
+     * Check if there are others waiting for a new call.
+     * If so, let them go first to avoid starving them.
+     * This is a fairly simple scheme, and might not be
+     * a complete solution for large numbers of waiters.
+     */
+    if (conn->makeCallWaiters) {
+#ifdef RX_ENABLE_LOCKS
+       CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
+#else
+       osi_rxSleep(conn);
+#endif
+    }
+
     for (;;) {
        for (i=0; i<RX_MAXCALLS; i++) {
            call = conn->call[i];
@@ -1009,7 +1035,6 @@ struct rx_call *rx_NewCall(conn)
            }
            else {
                call = rxi_NewCall(conn, i);
-               MUTEX_ENTER(&call->lock);
                break;
            }
        }
@@ -1019,12 +1044,24 @@ struct rx_call *rx_NewCall(conn)
        MUTEX_ENTER(&conn->conn_data_lock);
        conn->flags |= RX_CONN_MAKECALL_WAITING;
        MUTEX_EXIT(&conn->conn_data_lock);
+
+       conn->makeCallWaiters++;
 #ifdef RX_ENABLE_LOCKS
        CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
 #else
        osi_rxSleep(conn);
 #endif
+       conn->makeCallWaiters--;
     }
+    /*
+     * Wake up anyone else who might be giving us a chance to
+     * run (see code above that avoids resource starvation).
+     */
+#ifdef RX_ENABLE_LOCKS
+    CV_BROADCAST(&conn->conn_call_cv);
+#else
+    osi_rxWakeup(conn);
+#endif
 
     CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
 
@@ -1069,6 +1106,7 @@ struct rx_call *rx_NewCall(conn)
     return call;
 }
 
+int
 rxi_HasActiveCalls(aconn)
 register struct rx_connection *aconn; {
     register int i;
@@ -1077,7 +1115,7 @@ register struct rx_connection *aconn; {
 
     NETPRI;
     for(i=0; i<RX_MAXCALLS; i++) {
-      if (tcall = aconn->call[i]) {
+      if ((tcall = aconn->call[i])) {
        if ((tcall->state == RX_STATE_ACTIVE) 
            || (tcall->state == RX_STATE_PRECALL)) {
          USERPRI;
@@ -1089,6 +1127,7 @@ register struct rx_connection *aconn; {
     return 0;
 }
 
+int
 rxi_GetCallNumberVector(aconn, aint32s)
 register struct rx_connection *aconn;
 register afs_int32 *aint32s; {
@@ -1107,6 +1146,7 @@ register afs_int32 *aint32s; {
     return 0;
 }
 
+int
 rxi_SetCallNumberVector(aconn, aint32s)
 register struct rx_connection *aconn;
 register afs_int32 *aint32s; {
@@ -1372,12 +1412,12 @@ osi_socket *socketp;
 {
     struct rx_serverQueueEntry *sq;
     register struct rx_call *call = (struct rx_call *) 0, *choice2;
-    struct rx_service *service;
+    struct rx_service *service = NULL;
     SPLVAR;
 
     MUTEX_ENTER(&freeSQEList_lock);
 
-    if (sq = rx_FreeSQEList) {
+    if ((sq = rx_FreeSQEList)) {
        rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
        MUTEX_EXIT(&freeSQEList_lock);
     } else {    /* otherwise allocate a new one and return that */
@@ -1528,14 +1568,14 @@ rx_GetCall(tno, cur_service, socketp)
 {
     struct rx_serverQueueEntry *sq;
     register struct rx_call *call = (struct rx_call *) 0, *choice2;
-    struct rx_service *service;
+    struct rx_service *service = NULL;
     SPLVAR;
 
     NETPRI;
     AFS_RXGLOCK();
     MUTEX_ENTER(&freeSQEList_lock);
 
-    if (sq = rx_FreeSQEList) {
+    if ((sq = rx_FreeSQEList)) {
        rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
        MUTEX_EXIT(&freeSQEList_lock);
     } else {    /* otherwise allocate a new one and return that */
@@ -1759,6 +1799,7 @@ afs_int32 rx_EndCall(call, rc)
        MUTEX_ENTER(&conn->conn_call_lock);
        MUTEX_ENTER(&call->lock);
        MUTEX_ENTER(&conn->conn_data_lock);
+       conn->flags |= RX_CONN_BUSY;
        if (conn->flags & RX_CONN_MAKECALL_WAITING) {
            conn->flags &= (~RX_CONN_MAKECALL_WAITING);
            MUTEX_EXIT(&conn->conn_data_lock);
@@ -1797,8 +1838,10 @@ afs_int32 rx_EndCall(call, rc)
 
     CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
     MUTEX_EXIT(&call->lock);
-    if (conn->type == RX_CLIENT_CONNECTION)
+    if (conn->type == RX_CLIENT_CONNECTION) {
        MUTEX_EXIT(&conn->conn_call_lock);
+       conn->flags &= ~RX_CONN_BUSY;
+    }
     AFS_RXGUNLOCK();
     USERPRI;
     /*
@@ -1906,7 +1949,7 @@ struct rx_service *rxi_FindService(socket, serviceId)
 
 /* Allocate a call structure, for the indicated channel of the
  * supplied connection.  The mode and state of the call must be set by
- * the caller. */
+ * the caller. Returns the call with mutex locked. */
 struct rx_call *rxi_NewCall(conn, channel)
     register struct rx_connection *conn;
     register int channel;
@@ -1988,7 +2031,6 @@ struct rx_call *rxi_NewCall(conn, channel)
        the call number is valid from the last time this channel was used */
     if (*call->callNumber == 0) *call->callNumber = 1;
 
-    MUTEX_EXIT(&call->lock);
     return call;
 }
 
@@ -2091,7 +2133,7 @@ register size_t size;
     p = (char *) osi_Alloc(size);
 #endif
     if (!p) osi_Panic("rxi_Alloc error");
-    bzero(p, size);
+    memset(p, 0, size);
     return p;
 }
 
@@ -2514,7 +2556,6 @@ struct rx_packet *rxi_ReceivePacket(np, socket, host, port, tnop, newcallp)
        }
        if (!call) {
            call = rxi_NewCall(conn, channel);
-           MUTEX_ENTER(&call->lock);
            *call->callNumber = np->header.callNumber;
            call->state = RX_STATE_PRECALL;
            clock_GetTime(&call->queueTime);
@@ -3216,7 +3257,6 @@ struct rx_packet *rxi_ReceiveAckPacket(call, np, istack)
     afs_uint32 serial;
     /* because there are CM's that are bogus, sending weird values for this. */
     afs_uint32 skew = 0;
-    int needRxStart = 0;
     int nbytes;
     int missing;
     int acked;
@@ -3253,8 +3293,9 @@ struct rx_packet *rxi_ReceiveAckPacket(call, np, istack)
     if (rx_Log) {
       fprintf( rx_Log, 
              "RACK: reason %x previous %u seq %u serial %u skew %d first %u",
-             ap->reason, ntohl(ap->previousPacket), np->header.seq, serial, 
-             skew, ntohl(ap->firstPacket));
+             ap->reason, ntohl(ap->previousPacket), 
+             (unsigned int) np->header.seq, (unsigned int) serial, 
+             (unsigned int) skew, ntohl(ap->firstPacket));
        if (nAcks) {
            int offset;
            for (offset = 0; offset < nAcks; offset++) 
@@ -3314,12 +3355,12 @@ struct rx_packet *rxi_ReceiveAckPacket(call, np, istack)
      * set the ack bits in the packets and have rxi_Start remove the packets
      * when it's done transmitting.
      */
-       if (!tp->acked) {
+       if (!(tp->flags & RX_PKTFLAG_ACKED)) {
            newAckCount++;
        }
        if (call->flags & RX_CALL_TQ_BUSY) {
 #ifdef RX_ENABLE_LOCKS
-           tp->acked = 1;
+           tp->flags |= RX_PKTFLAG_ACKED;
            call->flags |= RX_CALL_TQ_SOME_ACKED;
 #else /* RX_ENABLE_LOCKS */
            break;
@@ -3388,17 +3429,17 @@ struct rx_packet *rxi_ReceiveAckPacket(call, np, istack)
         * out of sequence. */
        if (tp->header.seq < first) {
            /* Implicit ack information */
-           if (!tp->acked) {
+           if (!(tp->flags & RX_PKTFLAG_ACKED)) {
                newAckCount++;
            }
-           tp->acked = 1;
+           tp->flags |= RX_PKTFLAG_ACKED;
        }
        else if (tp->header.seq < first + nAcks) {
            /* Explicit ack information:  set it in the packet appropriately */
            if (ap->acks[tp->header.seq - first] == RX_ACK_TYPE_ACK) {
-               if (!tp->acked) {
+               if (!(tp->flags & RX_PKTFLAG_ACKED)) {
                    newAckCount++;
-                   tp->acked = 1;
+                   tp->flags |= RX_PKTFLAG_ACKED;
                }
                if (missing) {
                    nNacked++;
@@ -3406,12 +3447,12 @@ struct rx_packet *rxi_ReceiveAckPacket(call, np, istack)
                    call->nSoftAcked++;
                }
            } else {
-               tp->acked = 0;
+               tp->flags &= ~RX_PKTFLAG_ACKED;
                missing = 1;
            }
        }
        else {
-           tp->acked = 0;
+           tp->flags &= ~RX_PKTFLAG_ACKED;
            missing = 1;
        }
 
@@ -3420,7 +3461,7 @@ struct rx_packet *rxi_ReceiveAckPacket(call, np, istack)
         * ie, this should readjust the retransmit timer for all outstanding 
         * packets...  So we don't just retransmit when we should know better*/
 
-       if (!tp->acked && !clock_IsZero(&tp->retryTime)) {
+       if (!(tp->flags & RX_PKTFLAG_ACKED) && !clock_IsZero(&tp->retryTime)) {
          tp->retryTime = tp->timeSent;
          clock_Add(&tp->retryTime, &peer->timeout);
          /* shift by eight because one quarter-sec ~ 256 milliseconds */
@@ -3448,7 +3489,7 @@ struct rx_packet *rxi_ReceiveAckPacket(call, np, istack)
 
     /* if the ack packet has a receivelen field hanging off it,
      * update our state */
-    if ( np->length >= rx_AckDataSize(ap->nAcks) +sizeof(afs_int32)) {
+    if ( np->length >= rx_AckDataSize(ap->nAcks) + 2*sizeof(afs_int32)) {
       afs_uint32 tSize;
 
       /* If the ack packet has a "recommended" size that is less than 
@@ -3613,10 +3654,10 @@ struct rx_packet *rxi_ReceiveAckPacket(call, np, istack)
         * so we will retransmit as soon as the window permits*/
        for(acked = 0, queue_ScanBackwards(&call->tq, tp, nxp, rx_packet)) {
            if (acked) {
-               if (!tp->acked) {
+               if (!(tp->flags & RX_PKTFLAG_ACKED)) {
                    clock_Zero(&tp->retryTime);
                }
-           } else if (tp->acked) {
+           } else if (tp->flags & RX_PKTFLAG_ACKED) {
                acked = 1;
            }
        }
@@ -3926,7 +3967,7 @@ static void rxi_SetAcksInTransmitQueue(call)
      for (queue_Scan(&call->tq, p, tp, rx_packet)) {
         if (!p) 
             break;
-        p->acked = 1;
+        p->flags |= RX_PKTFLAG_ACKED;
         someAcked = 1;
      }
      if (someAcked) {
@@ -3963,7 +4004,7 @@ void rxi_ClearTransmitQueue(call, force)
        for (queue_Scan(&call->tq, p, tp, rx_packet)) {
          if (!p) 
             break;
-         p->acked = 1;
+         p->flags |= RX_PKTFLAG_ACKED;
          someAcked = 1;
        }
        if (someAcked) {
@@ -4440,8 +4481,8 @@ struct rx_packet *rxi_SendAck(call, optionalPacket, seq, serial, pflags, reason,
 #ifdef RXDEBUG
     if (rx_Log) {
        fprintf(rx_Log, "SACK: reason %x previous %u seq %u first %u",
-               ap->reason, ntohl(ap->previousPacket), p->header.seq, 
-               ntohl(ap->firstPacket));
+               ap->reason, ntohl(ap->previousPacket), 
+               (unsigned int) p->header.seq, ntohl(ap->firstPacket));
        if (ap->nAcks) {
            for (offset = 0; offset < ap->nAcks; offset++) 
                putc(ap->acks[offset] == RX_ACK_TYPE_NACK? '-' : '*', rx_Log);
@@ -4616,7 +4657,7 @@ static void rxi_SendXmitList(call, list, len, istack, now, retryTime, resending)
        /* Does the current packet force us to flush the current list? */
        if (cnt > 0
            && (list[i]->header.serial
-               || list[i]->acked
+               || (list[i]->flags & RX_PKTFLAG_ACKED)
                || list[i]->length > RX_JUMBOBUFFERSIZE)) {
            if (lastCnt > 0) {
                rxi_SendList(call, lastP, lastCnt, istack, 1, now, retryTime, resending);
@@ -4632,7 +4673,7 @@ static void rxi_SendXmitList(call, list, len, istack, now, retryTime, resending)
        }
        /* Add the current packet to the list if it hasn't been acked.
         * Otherwise adjust the list pointer to skip the current packet.  */
-       if (!list[i]->acked) {
+       if (!(list[i]->flags & RX_PKTFLAG_ACKED)) {
            cnt++;
            /* Do we need to flush the list? */
            if (cnt >= (int)peer->maxDgramPackets
@@ -4672,7 +4713,7 @@ static void rxi_SendXmitList(call, list, len, istack, now, retryTime, resending)
         * an acked packet. Since we always send retransmissions
         * in a separate packet, we only need to check the first
         * packet in the list */
-       if (cnt > 0 && !listP[0]->acked) {
+       if (cnt > 0 && !(listP[0]->flags & RX_PKTFLAG_ACKED)) {
            morePackets = 1;
        }
        if (lastCnt > 0) {
@@ -4777,7 +4818,7 @@ void rxi_Start(event, call, istack)
         * than recovery rates.
         */
        for(queue_Scan(&call->tq, p, nxp, rx_packet)) {
-           if (!p->acked) {
+           if (!(p->flags & RX_PKTFLAG_ACKED)) {
                clock_Zero(&p->retryTime);
            }
        }
@@ -4840,14 +4881,14 @@ void rxi_Start(event, call, istack)
             /* Only send one packet during fast recovery */
             break;
          }
-         if ((p->header.flags == RX_FREE_PACKET) ||
+         if ((p->flags & RX_PKTFLAG_FREE) ||
              (!queue_IsEnd(&call->tq, nxp)
-              && (nxp->header.flags == RX_FREE_PACKET)) ||
+              && (nxp->flags & RX_PKTFLAG_FREE)) ||
              (p == (struct rx_packet *)&rx_freePacketQueue) ||
              (nxp == (struct rx_packet *)&rx_freePacketQueue)) {
              osi_Panic("rxi_Start: xmit queue clobbered");
          }
-         if (p->acked) {
+         if (p->flags & RX_PKTFLAG_ACKED) {
            MUTEX_ENTER(&rx_stats_mutex);
            rx_stats.ignoreAckedPacket++;
            MUTEX_EXIT(&rx_stats_mutex);
@@ -4930,7 +4971,7 @@ void rxi_Start(event, call, istack)
             * the transmit queue.
             */
            for (missing = 0, queue_Scan(&call->tq, p, nxp, rx_packet)) {
-               if (p->header.seq < call->tfirst && p->acked) {
+               if (p->header.seq < call->tfirst && (p->flags & RX_PKTFLAG_ACKED)) {
                    queue_Remove(p);
                    rxi_FreePacket(p);
                }
@@ -4966,7 +5007,7 @@ void rxi_Start(event, call, istack)
                break;
            }
 
-           if (!p->acked && !clock_IsZero(&p->retryTime)) {
+           if (!(p->flags & RX_PKTFLAG_ACKED) && !clock_IsZero(&p->retryTime)) {
                haveEvent = 1;
                retryTime = p->retryTime;
                break;
@@ -5306,7 +5347,6 @@ void rxi_ComputeRoundTripTime(p, sentp, peer)
        struct timeval temptime;
 #endif
       register int rtt_timeout;
-      static char id[]="@(#)adaptive RTO";
 
 #if defined(AFS_ALPHA_LINUX20_ENV) && defined(AFS_PTHREAD_ENV) && !defined(KERNEL)
       /* yet again. This was the worst Heisenbug of the port - stroucki */
@@ -5736,7 +5776,7 @@ rxi_DebugPrint(format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
 {
     struct clock now;
     clock_GetTime(&now);
-    fprintf(rx_Log, " %u.%.3u:", now.sec, now.usec/1000);
+    fprintf(rx_Log, " %u.%.3u:", (unsigned int) now.sec, (unsigned int) now.usec/1000);
     fprintf(rx_Log, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15);
     putc('\n', rx_Log);
 }
@@ -5765,9 +5805,8 @@ void rx_PrintTheseStats (file, s, size, freePackets, version)
     }
 
     fprintf(file,
-           "rx stats: free packets %d, "
-           "allocs %d, ",
-           freePackets,
+           "rx stats: free packets %d, allocs %d, ",
+           (int) freePackets,
            s->packetRequests);
 
     if (version >= RX_DEBUGI_VERSION_W_NEWPACKETTYPES) {
@@ -5847,7 +5886,7 @@ void rx_PrintTheseStats (file, s, size, freePackets, version)
            "   \t(these should be small) sendFailed %d, "
            "fatalErrors %d\n", 
            s->netSendFailures,
-           s->fatalErrors);
+           (int) s->fatalErrors);
 
     if (s->nRttSamples) {
        fprintf(file,
@@ -5899,10 +5938,10 @@ struct rx_peer *peer;
            "Burst size %d, "
            "burst wait %u.%d.\n",
            ntohl(peer->host),
-           peer->port,
-           peer->burstSize,
-           peer->burstWait.sec,
-           peer->burstWait.usec);
+           (int) peer->port,
+           (int) peer->burstSize,
+           (int) peer->burstWait.sec,
+           (int) peer->burstWait.usec);
 
     fprintf(file,
            "   Rtt %d, "
@@ -5910,8 +5949,8 @@ struct rx_peer *peer;
            "total sent %d, "
            "resent %d\n",
            peer->rtt,
-           peer->timeout.sec,
-           peer->timeout.usec,
+           (int) peer->timeout.sec,
+           (int) peer->timeout.usec,
            peer->nSent,
            peer->reSends);
 
@@ -5920,8 +5959,8 @@ struct rx_peer *peer;
            "max in packet skew %d, "
            "max out packet skew %d\n",
            peer->ifMTU,
-           peer->inPacketSkew,
-           peer->outPacketSkew);
+           (int) peer->inPacketSkew,
+           (int) peer->outPacketSkew);
 }
 
 #ifdef AFS_PTHREAD_ENV
@@ -5978,8 +6017,8 @@ static int MakeDebugCall(
        theader.flags = RX_CLIENT_INITIATED | RX_LAST_PACKET;
        theader.serviceId = 0;
        
-       bcopy(&theader, tbuffer, sizeof(theader));
-       bcopy(inputData, tp, inputLength);
+       memcpy(tbuffer, &theader, sizeof(theader));
+       memcpy(tp, inputData, inputLength);
        code = sendto(socket, tbuffer, inputLength+sizeof(struct rx_header), 0,
                      (struct sockaddr *) &taddr, sizeof(struct sockaddr_in));
        
@@ -5995,7 +6034,7 @@ static int MakeDebugCall(
            code = recvfrom(socket, tbuffer, sizeof(tbuffer), 0,
                            (struct sockaddr *) &faddr, &faddrLen);
 
-           bcopy(tbuffer, &theader, sizeof(struct rx_header));
+           memcpy(&theader, tbuffer, sizeof(struct rx_header));
            if (counter == ntohl(theader.callNumber)) break;
        }
 
@@ -6004,7 +6043,7 @@ static int MakeDebugCall(
     }
     code -= sizeof(struct rx_header);
     if (code > outputLength) code = outputLength;
-    bcopy(tp, outputData, code);
+    memcpy(outputData, tp, code);
     return code;
 }
 
@@ -6241,7 +6280,6 @@ afs_int32 rx_GetServerPeers(
 {
     struct rx_debugIn in;
     afs_int32 rc = 0;
-    int i;
 
     /*
      * supportedValues is currently unused, but added to allow future
@@ -6399,7 +6437,7 @@ void shutdown_rx(void)
 
     MUTEX_ENTER(&freeSQEList_lock);
 
-    while (np = rx_FreeSQEList) {
+    while ((np = rx_FreeSQEList)) {
        rx_FreeSQEList = *(struct rx_serverQueueEntry **)np;
        MUTEX_DESTROY(&np->lock);
        rxi_Free(np, sizeof(*np));
@@ -6596,7 +6634,8 @@ static int rxi_AddRpcStat(
      * queue.
      */
 
-    if ((rpc_stat == NULL) ||
+    if (queue_IsEnd(stats, rpc_stat) ||
+       (rpc_stat == NULL) ||
        (rpc_stat->stats[0].interfaceId != rxInterface) ||
        (rpc_stat->stats[0].remote_is_server != isServer)) {
        int i;
@@ -6897,9 +6936,6 @@ int rx_RetrieveProcessRPCStats(
        ptr = *stats = (afs_uint32 *) rxi_Alloc(space);
 
        if (ptr != NULL) {
-           register struct rx_peer *pp;
-           int i;
-           int num_copied = 0;
            rx_interface_stat_p rpc_stat, nrpc_stat;
 
 
@@ -7002,8 +7038,6 @@ int rx_RetrievePeerRPCStats(
        ptr = *stats = (afs_uint32 *) rxi_Alloc(space);
 
        if (ptr != NULL) {
-           int i;
-           int num_copied = 0;
            rx_interface_stat_p rpc_stat, nrpc_stat;
            char *fix_offset;