rx: Remove incorrect backoff code
[openafs.git] / src / rx / rx.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 /* RX:  Extended Remote Procedure Call */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 #ifdef KERNEL
16 # include "afs/sysincludes.h"
17 # include "afsincludes.h"
18 # ifndef UKERNEL
19 #  include "h/types.h"
20 #  include "h/time.h"
21 #  include "h/stat.h"
22 #  ifdef AFS_LINUX20_ENV
23 #   include "h/socket.h"
24 #  endif
25 #  include "netinet/in.h"
26 #  ifdef AFS_SUN58_ENV
27 #   include "netinet/ip6.h"
28 #  endif
29 #  ifdef AFS_SUN57_ENV
30 #   include "inet/common.h"
31 #   include "inet/ip.h"
32 #   include "inet/ip_ire.h"
33 #  endif
34 #  include "afs/afs_args.h"
35 #  include "afs/afs_osi.h"
36 #  ifdef RX_KERNEL_TRACE
37 #   include "rx_kcommon.h"
38 #  endif
39 #  if   defined(AFS_AIX_ENV)
40 #   include "h/systm.h"
41 #  endif
42 #  ifdef RXDEBUG
43 #   undef RXDEBUG                       /* turn off debugging */
44 #  endif /* RXDEBUG */
45 #  if defined(AFS_SGI_ENV)
46 #   include "sys/debug.h"
47 #  endif
48 # else /* !UKERNEL */
49 #  include "afs/sysincludes.h"
50 #  include "afsincludes.h"
51 # endif /* !UKERNEL */
52 # include "afs/lock.h"
53 # include "rx_kmutex.h"
54 # include "rx_kernel.h"
55 # define        AFSOP_STOP_RXCALLBACK   210     /* Stop CALLBACK process */
56 # define        AFSOP_STOP_AFS          211     /* Stop AFS process */
57 # define        AFSOP_STOP_BKG          212     /* Stop BKG process */
58 extern afs_int32 afs_termState;
59 # ifdef AFS_AIX41_ENV
60 #  include "sys/lockl.h"
61 #  include "sys/lock_def.h"
62 # endif /* AFS_AIX41_ENV */
63 # include "afs/rxgen_consts.h"
64 #else /* KERNEL */
65 # include <roken.h>
66
67 # ifdef AFS_NT40_ENV
68 #  include <afs/afsutil.h>
69 #  include <WINNT\afsreg.h>
70 # endif
71
72 # include "rx_user.h"
73 #endif /* KERNEL */
74
75 #include "rx.h"
76 #include "rx_clock.h"
77 #include "rx_queue.h"
78 #include "rx_atomic.h"
79 #include "rx_globals.h"
80 #include "rx_trace.h"
81 #include "rx_internal.h"
82 #include "rx_stats.h"
83
84 #include <afs/rxgen_consts.h>
85
86 #ifndef KERNEL
87 #ifdef AFS_PTHREAD_ENV
88 #ifndef AFS_NT40_ENV
89 int (*registerProgram) (pid_t, char *) = 0;
90 int (*swapNameProgram) (pid_t, const char *, char *) = 0;
91 #endif
92 #else
93 int (*registerProgram) (PROCESS, char *) = 0;
94 int (*swapNameProgram) (PROCESS, const char *, char *) = 0;
95 #endif
96 #endif
97
98 /* Local static routines */
99 static void rxi_DestroyConnectionNoLock(struct rx_connection *conn);
100 static void rxi_ComputeRoundTripTime(struct rx_packet *, struct rx_ackPacket *,
101                                      struct rx_peer *, struct clock *);
102
103 #ifdef RX_ENABLE_LOCKS
104 static void rxi_SetAcksInTransmitQueue(struct rx_call *call);
105 #endif
106
107 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
108 struct rx_tq_debug {
109     rx_atomic_t rxi_start_aborted; /* rxi_start awoke after rxi_Send in error.*/
110     rx_atomic_t rxi_start_in_error;
111 } rx_tq_debug;
112 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
113
114 /* Constant delay time before sending an acknowledge of the last packet
115  * received.  This is to avoid sending an extra acknowledge when the
116  * client is about to make another call, anyway, or the server is
117  * about to respond.
118  *
119  * The lastAckDelay may not exceeed 400ms without causing peers to
120  * unecessarily timeout.
121  */
122 struct clock rx_lastAckDelay = {0, 400000};
123
124 /* Constant delay time before sending a soft ack when none was requested.
125  * This is to make sure we send soft acks before the sender times out,
126  * Normally we wait and send a hard ack when the receiver consumes the packet
127  *
128  * This value has been 100ms in all shipping versions of OpenAFS. Changing it
129  * will require changes to the peer's RTT calculations.
130  */
131 struct clock rx_softAckDelay = {0, 100000};
132
133 /*
134  * rxi_rpc_peer_stat_cnt counts the total number of peer stat structures
135  * currently allocated within rx.  This number is used to allocate the
136  * memory required to return the statistics when queried.
137  * Protected by the rx_rpc_stats mutex.
138  */
139
140 static unsigned int rxi_rpc_peer_stat_cnt;
141
142 /*
143  * rxi_rpc_process_stat_cnt counts the total number of local process stat
144  * structures currently allocated within rx.  The number is used to allocate
145  * the memory required to return the statistics when queried.
146  * Protected by the rx_rpc_stats mutex.
147  */
148
149 static unsigned int rxi_rpc_process_stat_cnt;
150
151 /*
152  * rxi_busyChannelError is the error to return to the application when a call
153  * channel appears busy (inferred from the receipt of RX_PACKET_TYPE_BUSY
154  * packets on the channel), and there are other call channels in the
155  * connection that are not busy. If 0, we do not return errors upon receiving
156  * busy packets; we just keep trying on the same call channel until we hit a
157  * timeout.
158  */
159 static afs_int32 rxi_busyChannelError = 0;
160
161 rx_atomic_t rx_nWaiting = RX_ATOMIC_INIT(0);
162 rx_atomic_t rx_nWaited = RX_ATOMIC_INIT(0);
163
164 #if !defined(offsetof)
165 #include <stddef.h>             /* for definition of offsetof() */
166 #endif
167
168 #ifdef RX_ENABLE_LOCKS
169 afs_kmutex_t rx_atomic_mutex;
170 #endif
171
172 /* Forward prototypes */
173 static struct rx_call * rxi_NewCall(struct rx_connection *, int);
174
175 #ifdef AFS_PTHREAD_ENV
176
177 /*
178  * Use procedural initialization of mutexes/condition variables
179  * to ease NT porting
180  */
181
182 extern afs_kmutex_t rx_quota_mutex;
183 extern afs_kmutex_t rx_pthread_mutex;
184 extern afs_kmutex_t rx_packets_mutex;
185 extern afs_kmutex_t rx_refcnt_mutex;
186 extern afs_kmutex_t des_init_mutex;
187 extern afs_kmutex_t des_random_mutex;
188 extern afs_kmutex_t rx_clock_mutex;
189 extern afs_kmutex_t rxi_connCacheMutex;
190 extern afs_kmutex_t rx_event_mutex;
191 extern afs_kmutex_t event_handler_mutex;
192 extern afs_kmutex_t listener_mutex;
193 extern afs_kmutex_t rx_if_init_mutex;
194 extern afs_kmutex_t rx_if_mutex;
195 extern afs_kmutex_t rxkad_client_uid_mutex;
196 extern afs_kmutex_t rxkad_random_mutex;
197
198 extern afs_kcondvar_t rx_event_handler_cond;
199 extern afs_kcondvar_t rx_listener_cond;
200
201 static afs_kmutex_t epoch_mutex;
202 static afs_kmutex_t rx_init_mutex;
203 static afs_kmutex_t rx_debug_mutex;
204 static afs_kmutex_t rx_rpc_stats;
205
206 static void
207 rxi_InitPthread(void)
208 {
209     MUTEX_INIT(&rx_clock_mutex, "clock", MUTEX_DEFAULT, 0);
210     MUTEX_INIT(&rx_stats_mutex, "stats", MUTEX_DEFAULT, 0);
211     MUTEX_INIT(&rx_atomic_mutex, "atomic", MUTEX_DEFAULT, 0);
212     MUTEX_INIT(&rx_quota_mutex, "quota", MUTEX_DEFAULT, 0);
213     MUTEX_INIT(&rx_pthread_mutex, "pthread", MUTEX_DEFAULT, 0);
214     MUTEX_INIT(&rx_packets_mutex, "packets", MUTEX_DEFAULT, 0);
215     MUTEX_INIT(&rx_refcnt_mutex, "refcnts", MUTEX_DEFAULT, 0);
216     MUTEX_INIT(&epoch_mutex, "epoch", MUTEX_DEFAULT, 0);
217     MUTEX_INIT(&rx_init_mutex, "init", MUTEX_DEFAULT, 0);
218     MUTEX_INIT(&rx_event_mutex, "event", MUTEX_DEFAULT, 0);
219     MUTEX_INIT(&event_handler_mutex, "event handler", MUTEX_DEFAULT, 0);
220     MUTEX_INIT(&rxi_connCacheMutex, "conn cache", MUTEX_DEFAULT, 0);
221     MUTEX_INIT(&listener_mutex, "listener", MUTEX_DEFAULT, 0);
222     MUTEX_INIT(&rx_if_init_mutex, "if init", MUTEX_DEFAULT, 0);
223     MUTEX_INIT(&rx_if_mutex, "if", MUTEX_DEFAULT, 0);
224     MUTEX_INIT(&rxkad_client_uid_mutex, "uid", MUTEX_DEFAULT, 0);
225     MUTEX_INIT(&rxkad_random_mutex, "rxkad random", MUTEX_DEFAULT, 0);
226     MUTEX_INIT(&rx_debug_mutex, "debug", MUTEX_DEFAULT, 0);
227
228     CV_INIT(&rx_event_handler_cond, "evhand", CV_DEFAULT, 0);
229     CV_INIT(&rx_listener_cond, "rxlisten", CV_DEFAULT, 0);
230
231     osi_Assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
232     osi_Assert(pthread_key_create(&rx_ts_info_key, NULL) == 0);
233
234     rxkad_global_stats_init();
235
236     MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
237     MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
238 #ifdef  RX_ENABLE_LOCKS
239 #ifdef RX_LOCKS_DB
240     rxdb_init();
241 #endif /* RX_LOCKS_DB */
242     MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
243     MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
244                0);
245     CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
246             0);
247     MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
248                0);
249     MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
250                0);
251     MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
252     MUTEX_INIT(&rxi_keyCreate_lock, "rxi_keyCreate_lock", MUTEX_DEFAULT, 0);
253 #endif /* RX_ENABLE_LOCKS */
254 }
255
256 pthread_once_t rx_once_init = PTHREAD_ONCE_INIT;
257 #define INIT_PTHREAD_LOCKS osi_Assert(pthread_once(&rx_once_init, rxi_InitPthread)==0)
258 /*
259  * The rx_stats_mutex mutex protects the following global variables:
260  * rxi_lowConnRefCount
261  * rxi_lowPeerRefCount
262  * rxi_nCalls
263  * rxi_Alloccnt
264  * rxi_Allocsize
265  * rx_tq_debug
266  * rx_stats
267  */
268
269 /*
270  * The rx_quota_mutex mutex protects the following global variables:
271  * rxi_dataQuota
272  * rxi_minDeficit
273  * rxi_availProcs
274  * rxi_totalMin
275  */
276
277 /*
278  * The rx_freePktQ_lock protects the following global variables:
279  * rx_nFreePackets
280  */
281
282 /*
283  * The rx_packets_mutex mutex protects the following global variables:
284  * rx_nPackets
285  * rx_TSFPQLocalMax
286  * rx_TSFPQGlobSize
287  * rx_TSFPQMaxProcs
288  */
289
290 /*
291  * The rx_pthread_mutex mutex protects the following global variables:
292  * rxi_fcfs_thread_num
293  */
294 #else
295 #define INIT_PTHREAD_LOCKS
296 #endif
297
298
299 /* Variables for handling the minProcs implementation.  availProcs gives the
300  * number of threads available in the pool at this moment (not counting dudes
301  * executing right now).  totalMin gives the total number of procs required
302  * for handling all minProcs requests.  minDeficit is a dynamic variable
303  * tracking the # of procs required to satisfy all of the remaining minProcs
304  * demands.
305  * For fine grain locking to work, the quota check and the reservation of
306  * a server thread has to come while rxi_availProcs and rxi_minDeficit
307  * are locked. To this end, the code has been modified under #ifdef
308  * RX_ENABLE_LOCKS so that quota checks and reservation occur at the
309  * same time. A new function, ReturnToServerPool() returns the allocation.
310  *
311  * A call can be on several queue's (but only one at a time). When
312  * rxi_ResetCall wants to remove the call from a queue, it has to ensure
313  * that no one else is touching the queue. To this end, we store the address
314  * of the queue lock in the call structure (under the call lock) when we
315  * put the call on a queue, and we clear the call_queue_lock when the
316  * call is removed from a queue (once the call lock has been obtained).
317  * This allows rxi_ResetCall to safely synchronize with others wishing
318  * to manipulate the queue.
319  */
320
321 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
322 static afs_kmutex_t rx_rpc_stats;
323 static void rxi_StartUnlocked(struct rxevent *event, void *call,
324                        void *arg1, int istack);
325 #endif
326
327 /* We keep a "last conn pointer" in rxi_FindConnection. The odds are
328 ** pretty good that the next packet coming in is from the same connection
329 ** as the last packet, since we're send multiple packets in a transmit window.
330 */
331 struct rx_connection *rxLastConn = 0;
332
333 #ifdef RX_ENABLE_LOCKS
334 /* The locking hierarchy for rx fine grain locking is composed of these
335  * tiers:
336  *
337  * rx_connHashTable_lock - synchronizes conn creation, rx_connHashTable access
338  * conn_call_lock - used to synchonize rx_EndCall and rx_NewCall
339  * call->lock - locks call data fields.
340  * These are independent of each other:
341  *      rx_freeCallQueue_lock
342  *      rxi_keyCreate_lock
343  * rx_serverPool_lock
344  * freeSQEList_lock
345  *
346  * serverQueueEntry->lock
347  * rx_peerHashTable_lock - locked under rx_connHashTable_lock
348  * rx_rpc_stats
349  * peer->lock - locks peer data fields.
350  * conn_data_lock - that more than one thread is not updating a conn data
351  *                  field at the same time.
352  * rx_freePktQ_lock
353  *
354  * lowest level:
355  *      multi_handle->lock
356  *      rxevent_lock
357  *      rx_packets_mutex
358  *      rx_stats_mutex
359  *      rx_refcnt_mutex
360  *      rx_atomic_mutex
361  *
362  * Do we need a lock to protect the peer field in the conn structure?
363  *      conn->peer was previously a constant for all intents and so has no
364  *      lock protecting this field. The multihomed client delta introduced
365  *      a RX code change : change the peer field in the connection structure
366  *      to that remote interface from which the last packet for this
367  *      connection was sent out. This may become an issue if further changes
368  *      are made.
369  */
370 #define SET_CALL_QUEUE_LOCK(C, L) (C)->call_queue_lock = (L)
371 #define CLEAR_CALL_QUEUE_LOCK(C) (C)->call_queue_lock = NULL
372 #ifdef RX_LOCKS_DB
373 /* rxdb_fileID is used to identify the lock location, along with line#. */
374 static int rxdb_fileID = RXDB_FILE_RX;
375 #endif /* RX_LOCKS_DB */
376 #else /* RX_ENABLE_LOCKS */
377 #define SET_CALL_QUEUE_LOCK(C, L)
378 #define CLEAR_CALL_QUEUE_LOCK(C)
379 #endif /* RX_ENABLE_LOCKS */
380 struct rx_serverQueueEntry *rx_waitForPacket = 0;
381 struct rx_serverQueueEntry *rx_waitingForPacket = 0;
382
383 /* ------------Exported Interfaces------------- */
384
385 /* This function allows rxkad to set the epoch to a suitably random number
386  * which rx_NewConnection will use in the future.  The principle purpose is to
387  * get rxnull connections to use the same epoch as the rxkad connections do, at
388  * least once the first rxkad connection is established.  This is important now
389  * that the host/port addresses aren't used in FindConnection: the uniqueness
390  * of epoch/cid matters and the start time won't do. */
391
392 #ifdef AFS_PTHREAD_ENV
393 /*
394  * This mutex protects the following global variables:
395  * rx_epoch
396  */
397
398 #define LOCK_EPOCH MUTEX_ENTER(&epoch_mutex)
399 #define UNLOCK_EPOCH MUTEX_EXIT(&epoch_mutex)
400 #else
401 #define LOCK_EPOCH
402 #define UNLOCK_EPOCH
403 #endif /* AFS_PTHREAD_ENV */
404
405 void
406 rx_SetEpoch(afs_uint32 epoch)
407 {
408     LOCK_EPOCH;
409     rx_epoch = epoch;
410     UNLOCK_EPOCH;
411 }
412
413 /* Initialize rx.  A port number may be mentioned, in which case this
414  * becomes the default port number for any service installed later.
415  * If 0 is provided for the port number, a random port will be chosen
416  * by the kernel.  Whether this will ever overlap anything in
417  * /etc/services is anybody's guess...  Returns 0 on success, -1 on
418  * error. */
419 #ifndef AFS_NT40_ENV
420 static
421 #endif
422 int rxinit_status = 1;
423 #ifdef AFS_PTHREAD_ENV
424 /*
425  * This mutex protects the following global variables:
426  * rxinit_status
427  */
428
429 #define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
430 #define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
431 #else
432 #define LOCK_RX_INIT
433 #define UNLOCK_RX_INIT
434 #endif
435
436 int
437 rx_InitHost(u_int host, u_int port)
438 {
439 #ifdef KERNEL
440     osi_timeval_t tv;
441 #else /* KERNEL */
442     struct timeval tv;
443 #endif /* KERNEL */
444     char *htable, *ptable;
445     int tmp_status;
446
447     SPLVAR;
448
449     INIT_PTHREAD_LOCKS;
450     LOCK_RX_INIT;
451     if (rxinit_status == 0) {
452         tmp_status = rxinit_status;
453         UNLOCK_RX_INIT;
454         return tmp_status;      /* Already started; return previous error code. */
455     }
456 #ifdef RXDEBUG
457     rxi_DebugInit();
458 #endif
459 #ifdef AFS_NT40_ENV
460     if (afs_winsockInit() < 0)
461         return -1;
462 #endif
463
464 #ifndef KERNEL
465     /*
466      * Initialize anything necessary to provide a non-premptive threading
467      * environment.
468      */
469     rxi_InitializeThreadSupport();
470 #endif
471
472     /* Allocate and initialize a socket for client and perhaps server
473      * connections. */
474
475     rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
476     if (rx_socket == OSI_NULLSOCKET) {
477         UNLOCK_RX_INIT;
478         return RX_ADDRINUSE;
479     }
480 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
481 #ifdef RX_LOCKS_DB
482     rxdb_init();
483 #endif /* RX_LOCKS_DB */
484     MUTEX_INIT(&rx_stats_mutex, "rx_stats_mutex", MUTEX_DEFAULT, 0);
485     MUTEX_INIT(&rx_quota_mutex, "rx_quota_mutex", MUTEX_DEFAULT, 0);
486     MUTEX_INIT(&rx_pthread_mutex, "rx_pthread_mutex", MUTEX_DEFAULT, 0);
487     MUTEX_INIT(&rx_packets_mutex, "rx_packets_mutex", MUTEX_DEFAULT, 0);
488     MUTEX_INIT(&rx_refcnt_mutex, "rx_refcnt_mutex", MUTEX_DEFAULT, 0);
489     MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
490     MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
491     MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
492     MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
493                0);
494     CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
495             0);
496     MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
497                0);
498     MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
499                0);
500     MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
501 #if defined(AFS_HPUX110_ENV)
502     if (!uniprocessor)
503         rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
504 #endif /* AFS_HPUX110_ENV */
505 #endif /* RX_ENABLE_LOCKS && KERNEL */
506
507     rxi_nCalls = 0;
508     rx_connDeadTime = 12;
509     rx_tranquil = 0;            /* reset flag */
510     rxi_ResetStatistics();
511     htable = (char *)
512         osi_Alloc(rx_hashTableSize * sizeof(struct rx_connection *));
513     PIN(htable, rx_hashTableSize * sizeof(struct rx_connection *));     /* XXXXX */
514     memset(htable, 0, rx_hashTableSize * sizeof(struct rx_connection *));
515     ptable = (char *)osi_Alloc(rx_hashTableSize * sizeof(struct rx_peer *));
516     PIN(ptable, rx_hashTableSize * sizeof(struct rx_peer *));   /* XXXXX */
517     memset(ptable, 0, rx_hashTableSize * sizeof(struct rx_peer *));
518
519     /* Malloc up a bunch of packets & buffers */
520     rx_nFreePackets = 0;
521     queue_Init(&rx_freePacketQueue);
522     rxi_NeedMorePackets = FALSE;
523     rx_nPackets = 0;    /* rx_nPackets is managed by rxi_MorePackets* */
524
525     /* enforce a minimum number of allocated packets */
526     if (rx_extraPackets < rxi_nSendFrags * rx_maxSendWindow)
527         rx_extraPackets = rxi_nSendFrags * rx_maxSendWindow;
528
529     /* allocate the initial free packet pool */
530 #ifdef RX_ENABLE_TSFPQ
531     rxi_MorePacketsTSFPQ(rx_extraPackets + RX_MAX_QUOTA + 2, RX_TS_FPQ_FLUSH_GLOBAL, 0);
532 #else /* RX_ENABLE_TSFPQ */
533     rxi_MorePackets(rx_extraPackets + RX_MAX_QUOTA + 2);        /* fudge */
534 #endif /* RX_ENABLE_TSFPQ */
535     rx_CheckPackets();
536
537     NETPRI;
538
539     clock_Init();
540
541 #if defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
542     tv.tv_sec = clock_now.sec;
543     tv.tv_usec = clock_now.usec;
544     srand((unsigned int)tv.tv_usec);
545 #else
546     osi_GetTime(&tv);
547 #endif
548     if (port) {
549         rx_port = port;
550     } else {
551 #if defined(KERNEL) && !defined(UKERNEL)
552         /* Really, this should never happen in a real kernel */
553         rx_port = 0;
554 #else
555         struct sockaddr_in addr;
556 #ifdef AFS_NT40_ENV
557         int addrlen = sizeof(addr);
558 #else
559         socklen_t addrlen = sizeof(addr);
560 #endif
561         if (getsockname((intptr_t)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
562             rx_Finalize();
563             return -1;
564         }
565         rx_port = addr.sin_port;
566 #endif
567     }
568     rx_stats.minRtt.sec = 9999999;
569 #ifdef  KERNEL
570     rx_SetEpoch(tv.tv_sec | 0x80000000);
571 #else
572     rx_SetEpoch(tv.tv_sec);     /* Start time of this package, rxkad
573                                  * will provide a randomer value. */
574 #endif
575     MUTEX_ENTER(&rx_quota_mutex);
576     rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
577     MUTEX_EXIT(&rx_quota_mutex);
578     /* *Slightly* random start time for the cid.  This is just to help
579      * out with the hashing function at the peer */
580     rx_nextCid = ((tv.tv_sec ^ tv.tv_usec) << RX_CIDSHIFT);
581     rx_connHashTable = (struct rx_connection **)htable;
582     rx_peerHashTable = (struct rx_peer **)ptable;
583
584     rx_hardAckDelay.sec = 0;
585     rx_hardAckDelay.usec = 100000;      /* 100 milliseconds */
586
587     rxevent_Init(20, rxi_ReScheduleEvents);
588
589     /* Initialize various global queues */
590     queue_Init(&rx_idleServerQueue);
591     queue_Init(&rx_incomingCallQueue);
592     queue_Init(&rx_freeCallQueue);
593
594 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
595     /* Initialize our list of usable IP addresses. */
596     rx_GetIFInfo();
597 #endif
598
599 #if defined(RXK_LISTENER_ENV) || !defined(KERNEL)
600     /* Start listener process (exact function is dependent on the
601      * implementation environment--kernel or user space) */
602     rxi_StartListener();
603 #endif
604
605     USERPRI;
606     tmp_status = rxinit_status = 0;
607     UNLOCK_RX_INIT;
608     return tmp_status;
609 }
610
611 int
612 rx_Init(u_int port)
613 {
614     return rx_InitHost(htonl(INADDR_ANY), port);
615 }
616
617 /**
618  * Sets the error generated when a busy call channel is detected.
619  *
620  * @param[in] error The error to return for a call on a busy channel.
621  *
622  * @pre Neither rx_Init nor rx_InitHost have been called yet
623  */
624 void
625 rx_SetBusyChannelError(afs_int32 error)
626 {
627     osi_Assert(rxinit_status != 0);
628     rxi_busyChannelError = error;
629 }
630
631 /* called with unincremented nRequestsRunning to see if it is OK to start
632  * a new thread in this service.  Could be "no" for two reasons: over the
633  * max quota, or would prevent others from reaching their min quota.
634  */
635 #ifdef RX_ENABLE_LOCKS
636 /* This verion of QuotaOK reserves quota if it's ok while the
637  * rx_serverPool_lock is held.  Return quota using ReturnToServerPool().
638  */
639 static int
640 QuotaOK(struct rx_service *aservice)
641 {
642     /* check if over max quota */
643     if (aservice->nRequestsRunning >= aservice->maxProcs) {
644         return 0;
645     }
646
647     /* under min quota, we're OK */
648     /* otherwise, can use only if there are enough to allow everyone
649      * to go to their min quota after this guy starts.
650      */
651
652     MUTEX_ENTER(&rx_quota_mutex);
653     if ((aservice->nRequestsRunning < aservice->minProcs)
654         || (rxi_availProcs > rxi_minDeficit)) {
655         aservice->nRequestsRunning++;
656         /* just started call in minProcs pool, need fewer to maintain
657          * guarantee */
658         if (aservice->nRequestsRunning <= aservice->minProcs)
659             rxi_minDeficit--;
660         rxi_availProcs--;
661         MUTEX_EXIT(&rx_quota_mutex);
662         return 1;
663     }
664     MUTEX_EXIT(&rx_quota_mutex);
665
666     return 0;
667 }
668
669 static void
670 ReturnToServerPool(struct rx_service *aservice)
671 {
672     aservice->nRequestsRunning--;
673     MUTEX_ENTER(&rx_quota_mutex);
674     if (aservice->nRequestsRunning < aservice->minProcs)
675         rxi_minDeficit++;
676     rxi_availProcs++;
677     MUTEX_EXIT(&rx_quota_mutex);
678 }
679
680 #else /* RX_ENABLE_LOCKS */
681 static int
682 QuotaOK(struct rx_service *aservice)
683 {
684     int rc = 0;
685     /* under min quota, we're OK */
686     if (aservice->nRequestsRunning < aservice->minProcs)
687         return 1;
688
689     /* check if over max quota */
690     if (aservice->nRequestsRunning >= aservice->maxProcs)
691         return 0;
692
693     /* otherwise, can use only if there are enough to allow everyone
694      * to go to their min quota after this guy starts.
695      */
696     MUTEX_ENTER(&rx_quota_mutex);
697     if (rxi_availProcs > rxi_minDeficit)
698         rc = 1;
699     MUTEX_EXIT(&rx_quota_mutex);
700     return rc;
701 }
702 #endif /* RX_ENABLE_LOCKS */
703
704 #ifndef KERNEL
705 /* Called by rx_StartServer to start up lwp's to service calls.
706    NExistingProcs gives the number of procs already existing, and which
707    therefore needn't be created. */
708 static void
709 rxi_StartServerProcs(int nExistingProcs)
710 {
711     struct rx_service *service;
712     int i;
713     int maxdiff = 0;
714     int nProcs = 0;
715
716     /* For each service, reserve N processes, where N is the "minimum"
717      * number of processes that MUST be able to execute a request in parallel,
718      * at any time, for that process.  Also compute the maximum difference
719      * between any service's maximum number of processes that can run
720      * (i.e. the maximum number that ever will be run, and a guarantee
721      * that this number will run if other services aren't running), and its
722      * minimum number.  The result is the extra number of processes that
723      * we need in order to provide the latter guarantee */
724     for (i = 0; i < RX_MAX_SERVICES; i++) {
725         int diff;
726         service = rx_services[i];
727         if (service == (struct rx_service *)0)
728             break;
729         nProcs += service->minProcs;
730         diff = service->maxProcs - service->minProcs;
731         if (diff > maxdiff)
732             maxdiff = diff;
733     }
734     nProcs += maxdiff;          /* Extra processes needed to allow max number requested to run in any given service, under good conditions */
735     nProcs -= nExistingProcs;   /* Subtract the number of procs that were previously created for use as server procs */
736     for (i = 0; i < nProcs; i++) {
737         rxi_StartServerProc(rx_ServerProc, rx_stackSize);
738     }
739 }
740 #endif /* KERNEL */
741
742 #ifdef AFS_NT40_ENV
743 /* This routine is only required on Windows */
744 void
745 rx_StartClientThread(void)
746 {
747 #ifdef AFS_PTHREAD_ENV
748     pthread_t pid;
749     pid = pthread_self();
750 #endif /* AFS_PTHREAD_ENV */
751 }
752 #endif /* AFS_NT40_ENV */
753
754 /* This routine must be called if any services are exported.  If the
755  * donateMe flag is set, the calling process is donated to the server
756  * process pool */
757 void
758 rx_StartServer(int donateMe)
759 {
760     struct rx_service *service;
761     int i;
762     SPLVAR;
763     clock_NewTime();
764
765     NETPRI;
766     /* Start server processes, if necessary (exact function is dependent
767      * on the implementation environment--kernel or user space).  DonateMe
768      * will be 1 if there is 1 pre-existing proc, i.e. this one.  In this
769      * case, one less new proc will be created rx_StartServerProcs.
770      */
771     rxi_StartServerProcs(donateMe);
772
773     /* count up the # of threads in minProcs, and add set the min deficit to
774      * be that value, too.
775      */
776     for (i = 0; i < RX_MAX_SERVICES; i++) {
777         service = rx_services[i];
778         if (service == (struct rx_service *)0)
779             break;
780         MUTEX_ENTER(&rx_quota_mutex);
781         rxi_totalMin += service->minProcs;
782         /* below works even if a thread is running, since minDeficit would
783          * still have been decremented and later re-incremented.
784          */
785         rxi_minDeficit += service->minProcs;
786         MUTEX_EXIT(&rx_quota_mutex);
787     }
788
789     /* Turn on reaping of idle server connections */
790     rxi_ReapConnections(NULL, NULL, NULL);
791
792     USERPRI;
793
794     if (donateMe) {
795 #ifndef AFS_NT40_ENV
796 #ifndef KERNEL
797         char name[32];
798         static int nProcs;
799 #ifdef AFS_PTHREAD_ENV
800         pid_t pid;
801         pid = afs_pointer_to_int(pthread_self());
802 #else /* AFS_PTHREAD_ENV */
803         PROCESS pid;
804         LWP_CurrentProcess(&pid);
805 #endif /* AFS_PTHREAD_ENV */
806
807         sprintf(name, "srv_%d", ++nProcs);
808         if (registerProgram)
809             (*registerProgram) (pid, name);
810 #endif /* KERNEL */
811 #endif /* AFS_NT40_ENV */
812         rx_ServerProc(NULL);    /* Never returns */
813     }
814 #ifdef RX_ENABLE_TSFPQ
815     /* no use leaving packets around in this thread's local queue if
816      * it isn't getting donated to the server thread pool.
817      */
818     rxi_FlushLocalPacketsTSFPQ();
819 #endif /* RX_ENABLE_TSFPQ */
820     return;
821 }
822
823 /* Create a new client connection to the specified service, using the
824  * specified security object to implement the security model for this
825  * connection. */
826 struct rx_connection *
827 rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
828                  struct rx_securityClass *securityObject,
829                  int serviceSecurityIndex)
830 {
831     int hashindex, i;
832     afs_int32 cid;
833     struct rx_connection *conn;
834
835     SPLVAR;
836
837     clock_NewTime();
838     dpf(("rx_NewConnection(host %x, port %u, service %u, securityObject %p, "
839          "serviceSecurityIndex %d)\n",
840          ntohl(shost), ntohs(sport), sservice, securityObject,
841          serviceSecurityIndex));
842
843     /* Vasilsi said: "NETPRI protects Cid and Alloc", but can this be true in
844      * the case of kmem_alloc? */
845     conn = rxi_AllocConnection();
846 #ifdef  RX_ENABLE_LOCKS
847     MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
848     MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
849     CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
850 #endif
851     NETPRI;
852     MUTEX_ENTER(&rx_connHashTable_lock);
853     cid = (rx_nextCid += RX_MAXCALLS);
854     conn->type = RX_CLIENT_CONNECTION;
855     conn->cid = cid;
856     conn->epoch = rx_epoch;
857     conn->peer = rxi_FindPeer(shost, sport, 0, 1);
858     conn->serviceId = sservice;
859     conn->securityObject = securityObject;
860     conn->securityData = (void *) 0;
861     conn->securityIndex = serviceSecurityIndex;
862     rx_SetConnDeadTime(conn, rx_connDeadTime);
863     rx_SetConnSecondsUntilNatPing(conn, 0);
864     conn->ackRate = RX_FAST_ACK_RATE;
865     conn->nSpecific = 0;
866     conn->specific = NULL;
867     conn->challengeEvent = NULL;
868     conn->delayedAbortEvent = NULL;
869     conn->abortCount = 0;
870     conn->error = 0;
871     for (i = 0; i < RX_MAXCALLS; i++) {
872         conn->twind[i] = rx_initSendWindow;
873         conn->rwind[i] = rx_initReceiveWindow;
874         conn->lastBusy[i] = 0;
875     }
876
877     RXS_NewConnection(securityObject, conn);
878     hashindex =
879         CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
880
881     conn->refCount++;           /* no lock required since only this thread knows... */
882     conn->next = rx_connHashTable[hashindex];
883     rx_connHashTable[hashindex] = conn;
884     if (rx_stats_active)
885         rx_atomic_inc(&rx_stats.nClientConns);
886     MUTEX_EXIT(&rx_connHashTable_lock);
887     USERPRI;
888     return conn;
889 }
890
891 /**
892  * Ensure a connection's timeout values are valid.
893  *
894  * @param[in] conn The connection to check
895  *
896  * @post conn->secondUntilDead <= conn->idleDeadTime <= conn->hardDeadTime,
897  *       unless idleDeadTime and/or hardDeadTime are not set
898  * @internal
899  */
900 static void
901 rxi_CheckConnTimeouts(struct rx_connection *conn)
902 {
903     /* a connection's timeouts must have the relationship
904      * deadTime <= idleDeadTime <= hardDeadTime. Otherwise, for example, a
905      * total loss of network to a peer may cause an idle timeout instead of a
906      * dead timeout, simply because the idle timeout gets hit first. Also set
907      * a minimum deadTime of 6, just to ensure it doesn't get set too low. */
908     /* this logic is slightly complicated by the fact that
909      * idleDeadTime/hardDeadTime may not be set at all, but it's not too bad.
910      */
911     conn->secondsUntilDead = MAX(conn->secondsUntilDead, 6);
912     if (conn->idleDeadTime) {
913         conn->idleDeadTime = MAX(conn->idleDeadTime, conn->secondsUntilDead);
914     }
915     if (conn->hardDeadTime) {
916         if (conn->idleDeadTime) {
917             conn->hardDeadTime = MAX(conn->idleDeadTime, conn->hardDeadTime);
918         } else {
919             conn->hardDeadTime = MAX(conn->secondsUntilDead, conn->hardDeadTime);
920         }
921     }
922 }
923
924 void
925 rx_SetConnDeadTime(struct rx_connection *conn, int seconds)
926 {
927     /* The idea is to set the dead time to a value that allows several
928      * keepalives to be dropped without timing out the connection. */
929     conn->secondsUntilDead = seconds;
930     rxi_CheckConnTimeouts(conn);
931     conn->secondsUntilPing = conn->secondsUntilDead / 6;
932 }
933
934 void
935 rx_SetConnHardDeadTime(struct rx_connection *conn, int seconds)
936 {
937     conn->hardDeadTime = seconds;
938     rxi_CheckConnTimeouts(conn);
939 }
940
941 void
942 rx_SetConnIdleDeadTime(struct rx_connection *conn, int seconds)
943 {
944     conn->idleDeadTime = seconds;
945     rxi_CheckConnTimeouts(conn);
946 }
947
948 int rxi_lowPeerRefCount = 0;
949 int rxi_lowConnRefCount = 0;
950
951 /*
952  * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
953  * NOTE: must not be called with rx_connHashTable_lock held.
954  */
955 static void
956 rxi_CleanupConnection(struct rx_connection *conn)
957 {
958     /* Notify the service exporter, if requested, that this connection
959      * is being destroyed */
960     if (conn->type == RX_SERVER_CONNECTION && conn->service->destroyConnProc)
961         (*conn->service->destroyConnProc) (conn);
962
963     /* Notify the security module that this connection is being destroyed */
964     RXS_DestroyConnection(conn->securityObject, conn);
965
966     /* If this is the last connection using the rx_peer struct, set its
967      * idle time to now. rxi_ReapConnections will reap it if it's still
968      * idle (refCount == 0) after rx_idlePeerTime (60 seconds) have passed.
969      */
970     MUTEX_ENTER(&rx_peerHashTable_lock);
971     if (conn->peer->refCount < 2) {
972         conn->peer->idleWhen = clock_Sec();
973         if (conn->peer->refCount < 1) {
974             conn->peer->refCount = 1;
975             if (rx_stats_active) {
976                 MUTEX_ENTER(&rx_stats_mutex);
977                 rxi_lowPeerRefCount++;
978                 MUTEX_EXIT(&rx_stats_mutex);
979             }
980         }
981     }
982     conn->peer->refCount--;
983     MUTEX_EXIT(&rx_peerHashTable_lock);
984
985     if (rx_stats_active)
986     {
987         if (conn->type == RX_SERVER_CONNECTION)
988             rx_atomic_dec(&rx_stats.nServerConns);
989         else
990             rx_atomic_dec(&rx_stats.nClientConns);
991     }
992 #ifndef KERNEL
993     if (conn->specific) {
994         int i;
995         for (i = 0; i < conn->nSpecific; i++) {
996             if (conn->specific[i] && rxi_keyCreate_destructor[i])
997                 (*rxi_keyCreate_destructor[i]) (conn->specific[i]);
998             conn->specific[i] = NULL;
999         }
1000         free(conn->specific);
1001     }
1002     conn->specific = NULL;
1003     conn->nSpecific = 0;
1004 #endif /* !KERNEL */
1005
1006     MUTEX_DESTROY(&conn->conn_call_lock);
1007     MUTEX_DESTROY(&conn->conn_data_lock);
1008     CV_DESTROY(&conn->conn_call_cv);
1009
1010     rxi_FreeConnection(conn);
1011 }
1012
1013 /* Destroy the specified connection */
1014 void
1015 rxi_DestroyConnection(struct rx_connection *conn)
1016 {
1017     MUTEX_ENTER(&rx_connHashTable_lock);
1018     rxi_DestroyConnectionNoLock(conn);
1019     /* conn should be at the head of the cleanup list */
1020     if (conn == rx_connCleanup_list) {
1021         rx_connCleanup_list = rx_connCleanup_list->next;
1022         MUTEX_EXIT(&rx_connHashTable_lock);
1023         rxi_CleanupConnection(conn);
1024     }
1025 #ifdef RX_ENABLE_LOCKS
1026     else {
1027         MUTEX_EXIT(&rx_connHashTable_lock);
1028     }
1029 #endif /* RX_ENABLE_LOCKS */
1030 }
1031
1032 static void
1033 rxi_DestroyConnectionNoLock(struct rx_connection *conn)
1034 {
1035     struct rx_connection **conn_ptr;
1036     int havecalls = 0;
1037     struct rx_packet *packet;
1038     int i;
1039     SPLVAR;
1040
1041     clock_NewTime();
1042
1043     NETPRI;
1044     MUTEX_ENTER(&conn->conn_data_lock);
1045     MUTEX_ENTER(&rx_refcnt_mutex);
1046     if (conn->refCount > 0)
1047         conn->refCount--;
1048     else {
1049         if (rx_stats_active) {
1050             MUTEX_ENTER(&rx_stats_mutex);
1051             rxi_lowConnRefCount++;
1052             MUTEX_EXIT(&rx_stats_mutex);
1053         }
1054     }
1055
1056     if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
1057         /* Busy; wait till the last guy before proceeding */
1058         MUTEX_EXIT(&rx_refcnt_mutex);
1059         MUTEX_EXIT(&conn->conn_data_lock);
1060         USERPRI;
1061         return;
1062     }
1063
1064     /* If the client previously called rx_NewCall, but it is still
1065      * waiting, treat this as a running call, and wait to destroy the
1066      * connection later when the call completes. */
1067     if ((conn->type == RX_CLIENT_CONNECTION)
1068         && (conn->flags & (RX_CONN_MAKECALL_WAITING|RX_CONN_MAKECALL_ACTIVE))) {
1069         conn->flags |= RX_CONN_DESTROY_ME;
1070         MUTEX_EXIT(&conn->conn_data_lock);
1071         USERPRI;
1072         return;
1073     }
1074     MUTEX_EXIT(&rx_refcnt_mutex);
1075     MUTEX_EXIT(&conn->conn_data_lock);
1076
1077     /* Check for extant references to this connection */
1078     for (i = 0; i < RX_MAXCALLS; i++) {
1079         struct rx_call *call = conn->call[i];
1080         if (call) {
1081             havecalls = 1;
1082             if (conn->type == RX_CLIENT_CONNECTION) {
1083                 MUTEX_ENTER(&call->lock);
1084                 if (call->delayedAckEvent) {
1085                     /* Push the final acknowledgment out now--there
1086                      * won't be a subsequent call to acknowledge the
1087                      * last reply packets */
1088                     rxevent_Cancel(call->delayedAckEvent, call,
1089                                    RX_CALL_REFCOUNT_DELAY);
1090                     if (call->state == RX_STATE_PRECALL
1091                         || call->state == RX_STATE_ACTIVE) {
1092                         rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1093                     } else {
1094                         rxi_AckAll(NULL, call, 0);
1095                     }
1096                 }
1097                 MUTEX_EXIT(&call->lock);
1098             }
1099         }
1100     }
1101 #ifdef RX_ENABLE_LOCKS
1102     if (!havecalls) {
1103         if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
1104             MUTEX_EXIT(&conn->conn_data_lock);
1105         } else {
1106             /* Someone is accessing a packet right now. */
1107             havecalls = 1;
1108         }
1109     }
1110 #endif /* RX_ENABLE_LOCKS */
1111
1112     if (havecalls) {
1113         /* Don't destroy the connection if there are any call
1114          * structures still in use */
1115         MUTEX_ENTER(&conn->conn_data_lock);
1116         conn->flags |= RX_CONN_DESTROY_ME;
1117         MUTEX_EXIT(&conn->conn_data_lock);
1118         USERPRI;
1119         return;
1120     }
1121
1122     if (conn->natKeepAliveEvent) {
1123         rxi_NatKeepAliveOff(conn);
1124     }
1125
1126     if (conn->delayedAbortEvent) {
1127         rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
1128         packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
1129         if (packet) {
1130             MUTEX_ENTER(&conn->conn_data_lock);
1131             rxi_SendConnectionAbort(conn, packet, 0, 1);
1132             MUTEX_EXIT(&conn->conn_data_lock);
1133             rxi_FreePacket(packet);
1134         }
1135     }
1136
1137     /* Remove from connection hash table before proceeding */
1138     conn_ptr =
1139         &rx_connHashTable[CONN_HASH
1140                           (peer->host, peer->port, conn->cid, conn->epoch,
1141                            conn->type)];
1142     for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
1143         if (*conn_ptr == conn) {
1144             *conn_ptr = conn->next;
1145             break;
1146         }
1147     }
1148     /* if the conn that we are destroying was the last connection, then we
1149      * clear rxLastConn as well */
1150     if (rxLastConn == conn)
1151         rxLastConn = 0;
1152
1153     /* Make sure the connection is completely reset before deleting it. */
1154     /* get rid of pending events that could zap us later */
1155     if (conn->challengeEvent)
1156         rxevent_Cancel(conn->challengeEvent, (struct rx_call *)0, 0);
1157     if (conn->checkReachEvent)
1158         rxevent_Cancel(conn->checkReachEvent, (struct rx_call *)0, 0);
1159     if (conn->natKeepAliveEvent)
1160         rxevent_Cancel(conn->natKeepAliveEvent, (struct rx_call *)0, 0);
1161
1162     /* Add the connection to the list of destroyed connections that
1163      * need to be cleaned up. This is necessary to avoid deadlocks
1164      * in the routines we call to inform others that this connection is
1165      * being destroyed. */
1166     conn->next = rx_connCleanup_list;
1167     rx_connCleanup_list = conn;
1168 }
1169
1170 /* Externally available version */
1171 void
1172 rx_DestroyConnection(struct rx_connection *conn)
1173 {
1174     SPLVAR;
1175
1176     NETPRI;
1177     rxi_DestroyConnection(conn);
1178     USERPRI;
1179 }
1180
1181 void
1182 rx_GetConnection(struct rx_connection *conn)
1183 {
1184     SPLVAR;
1185
1186     NETPRI;
1187     MUTEX_ENTER(&rx_refcnt_mutex);
1188     conn->refCount++;
1189     MUTEX_EXIT(&rx_refcnt_mutex);
1190     USERPRI;
1191 }
1192
1193 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
1194 /* Wait for the transmit queue to no longer be busy.
1195  * requires the call->lock to be held */
1196 void
1197 rxi_WaitforTQBusy(struct rx_call *call) {
1198     while (!call->error && (call->flags & RX_CALL_TQ_BUSY)) {
1199         call->flags |= RX_CALL_TQ_WAIT;
1200         call->tqWaiters++;
1201 #ifdef RX_ENABLE_LOCKS
1202         osirx_AssertMine(&call->lock, "rxi_WaitforTQ lock");
1203         CV_WAIT(&call->cv_tq, &call->lock);
1204 #else /* RX_ENABLE_LOCKS */
1205         osi_rxSleep(&call->tq);
1206 #endif /* RX_ENABLE_LOCKS */
1207         call->tqWaiters--;
1208         if (call->tqWaiters == 0) {
1209             call->flags &= ~RX_CALL_TQ_WAIT;
1210         }
1211     }
1212 }
1213 #endif
1214
1215 static void
1216 rxi_WakeUpTransmitQueue(struct rx_call *call)
1217 {
1218     if (call->tqWaiters || (call->flags & RX_CALL_TQ_WAIT)) {
1219         dpf(("call %"AFS_PTR_FMT" has %d waiters and flags %d\n",
1220              call, call->tqWaiters, call->flags));
1221 #ifdef RX_ENABLE_LOCKS
1222         osirx_AssertMine(&call->lock, "rxi_Start start");
1223         CV_BROADCAST(&call->cv_tq);
1224 #else /* RX_ENABLE_LOCKS */
1225         osi_rxWakeup(&call->tq);
1226 #endif /* RX_ENABLE_LOCKS */
1227     }
1228 }
1229
1230 /* Start a new rx remote procedure call, on the specified connection.
1231  * If wait is set to 1, wait for a free call channel; otherwise return
1232  * 0.  Maxtime gives the maximum number of seconds this call may take,
1233  * after rx_NewCall returns.  After this time interval, a call to any
1234  * of rx_SendData, rx_ReadData, etc. will fail with RX_CALL_TIMEOUT.
1235  * For fine grain locking, we hold the conn_call_lock in order to
1236  * to ensure that we don't get signalle after we found a call in an active
1237  * state and before we go to sleep.
1238  */
1239 struct rx_call *
1240 rx_NewCall(struct rx_connection *conn)
1241 {
1242     int i, wait, ignoreBusy = 1;
1243     struct rx_call *call;
1244     struct clock queueTime;
1245     afs_uint32 leastBusy = 0;
1246     SPLVAR;
1247
1248     clock_NewTime();
1249     dpf(("rx_NewCall(conn %"AFS_PTR_FMT")\n", conn));
1250
1251     NETPRI;
1252     clock_GetTime(&queueTime);
1253     /*
1254      * Check if there are others waiting for a new call.
1255      * If so, let them go first to avoid starving them.
1256      * This is a fairly simple scheme, and might not be
1257      * a complete solution for large numbers of waiters.
1258      *
1259      * makeCallWaiters keeps track of the number of
1260      * threads waiting to make calls and the
1261      * RX_CONN_MAKECALL_WAITING flag bit is used to
1262      * indicate that there are indeed calls waiting.
1263      * The flag is set when the waiter is incremented.
1264      * It is only cleared when makeCallWaiters is 0.
1265      * This prevents us from accidently destroying the
1266      * connection while it is potentially about to be used.
1267      */
1268     MUTEX_ENTER(&conn->conn_call_lock);
1269     MUTEX_ENTER(&conn->conn_data_lock);
1270     while (conn->flags & RX_CONN_MAKECALL_ACTIVE) {
1271         conn->flags |= RX_CONN_MAKECALL_WAITING;
1272         conn->makeCallWaiters++;
1273         MUTEX_EXIT(&conn->conn_data_lock);
1274
1275 #ifdef  RX_ENABLE_LOCKS
1276         CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1277 #else
1278         osi_rxSleep(conn);
1279 #endif
1280         MUTEX_ENTER(&conn->conn_data_lock);
1281         conn->makeCallWaiters--;
1282         if (conn->makeCallWaiters == 0)
1283             conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1284     }
1285
1286     /* We are now the active thread in rx_NewCall */
1287     conn->flags |= RX_CONN_MAKECALL_ACTIVE;
1288     MUTEX_EXIT(&conn->conn_data_lock);
1289
1290     for (;;) {
1291         wait = 1;
1292
1293         for (i = 0; i < RX_MAXCALLS; i++) {
1294             call = conn->call[i];
1295             if (call) {
1296                 if (!ignoreBusy && conn->lastBusy[i] != leastBusy) {
1297                     /* we're not ignoring busy call slots; only look at the
1298                      * call slot that is the "least" busy */
1299                     continue;
1300                 }
1301
1302                 if (call->state == RX_STATE_DALLY) {
1303                     MUTEX_ENTER(&call->lock);
1304                     if (call->state == RX_STATE_DALLY) {
1305                         if (ignoreBusy && conn->lastBusy[i]) {
1306                             /* if we're ignoring busy call slots, skip any ones that
1307                              * have lastBusy set */
1308                             if (leastBusy == 0 || conn->lastBusy[i] < leastBusy) {
1309                                 leastBusy = conn->lastBusy[i];
1310                             }
1311                             MUTEX_EXIT(&call->lock);
1312                             continue;
1313                         }
1314
1315                         /*
1316                          * We are setting the state to RX_STATE_RESET to
1317                          * ensure that no one else will attempt to use this
1318                          * call once we drop the conn->conn_call_lock and
1319                          * call->lock.  We must drop the conn->conn_call_lock
1320                          * before calling rxi_ResetCall because the process
1321                          * of clearing the transmit queue can block for an
1322                          * extended period of time.  If we block while holding
1323                          * the conn->conn_call_lock, then all rx_EndCall
1324                          * processing will block as well.  This has a detrimental
1325                          * effect on overall system performance.
1326                          */
1327                         call->state = RX_STATE_RESET;
1328                         MUTEX_EXIT(&conn->conn_call_lock);
1329                         MUTEX_ENTER(&rx_refcnt_mutex);
1330                         CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1331                         MUTEX_EXIT(&rx_refcnt_mutex);
1332                         rxi_ResetCall(call, 0);
1333                         (*call->callNumber)++;
1334                         if (MUTEX_TRYENTER(&conn->conn_call_lock))
1335                             break;
1336
1337                         /*
1338                          * If we failed to be able to safely obtain the
1339                          * conn->conn_call_lock we will have to drop the
1340                          * call->lock to avoid a deadlock.  When the call->lock
1341                          * is released the state of the call can change.  If it
1342                          * is no longer RX_STATE_RESET then some other thread is
1343                          * using the call.
1344                          */
1345                         MUTEX_EXIT(&call->lock);
1346                         MUTEX_ENTER(&conn->conn_call_lock);
1347                         MUTEX_ENTER(&call->lock);
1348
1349                         if (call->state == RX_STATE_RESET)
1350                             break;
1351
1352                         /*
1353                          * If we get here it means that after dropping
1354                          * the conn->conn_call_lock and call->lock that
1355                          * the call is no longer ours.  If we can't find
1356                          * a free call in the remaining slots we should
1357                          * not go immediately to RX_CONN_MAKECALL_WAITING
1358                          * because by dropping the conn->conn_call_lock
1359                          * we have given up synchronization with rx_EndCall.
1360                          * Instead, cycle through one more time to see if
1361                          * we can find a call that can call our own.
1362                          */
1363                         MUTEX_ENTER(&rx_refcnt_mutex);
1364                         CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
1365                         MUTEX_EXIT(&rx_refcnt_mutex);
1366                         wait = 0;
1367                     }
1368                     MUTEX_EXIT(&call->lock);
1369                 }
1370             } else {
1371                 if (ignoreBusy && conn->lastBusy[i]) {
1372                     /* if we're ignoring busy call slots, skip any ones that
1373                      * have lastBusy set */
1374                     if (leastBusy == 0 || conn->lastBusy[i] < leastBusy) {
1375                         leastBusy = conn->lastBusy[i];
1376                     }
1377                     continue;
1378                 }
1379
1380                 /* rxi_NewCall returns with mutex locked */
1381                 call = rxi_NewCall(conn, i);
1382                 MUTEX_ENTER(&rx_refcnt_mutex);
1383                 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1384                 MUTEX_EXIT(&rx_refcnt_mutex);
1385                 break;
1386             }
1387         }
1388         if (i < RX_MAXCALLS) {
1389             conn->lastBusy[i] = 0;
1390             break;
1391         }
1392         if (!wait)
1393             continue;
1394         if (leastBusy && ignoreBusy) {
1395             /* we didn't find a useable call slot, but we did see at least one
1396              * 'busy' slot; look again and only use a slot with the 'least
1397              * busy time */
1398             ignoreBusy = 0;
1399             continue;
1400         }
1401
1402         MUTEX_ENTER(&conn->conn_data_lock);
1403         conn->flags |= RX_CONN_MAKECALL_WAITING;
1404         conn->makeCallWaiters++;
1405         MUTEX_EXIT(&conn->conn_data_lock);
1406
1407 #ifdef  RX_ENABLE_LOCKS
1408         CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1409 #else
1410         osi_rxSleep(conn);
1411 #endif
1412         MUTEX_ENTER(&conn->conn_data_lock);
1413         conn->makeCallWaiters--;
1414         if (conn->makeCallWaiters == 0)
1415             conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1416         MUTEX_EXIT(&conn->conn_data_lock);
1417     }
1418     /* Client is initially in send mode */
1419     call->state = RX_STATE_ACTIVE;
1420     call->error = conn->error;
1421     if (call->error)
1422         call->mode = RX_MODE_ERROR;
1423     else
1424         call->mode = RX_MODE_SENDING;
1425
1426     /* remember start time for call in case we have hard dead time limit */
1427     call->queueTime = queueTime;
1428     clock_GetTime(&call->startTime);
1429     hzero(call->bytesSent);
1430     hzero(call->bytesRcvd);
1431
1432     /* Turn on busy protocol. */
1433     rxi_KeepAliveOn(call);
1434
1435     /* Attempt MTU discovery */
1436     rxi_GrowMTUOn(call);
1437
1438     /*
1439      * We are no longer the active thread in rx_NewCall
1440      */
1441     MUTEX_ENTER(&conn->conn_data_lock);
1442     conn->flags &= ~RX_CONN_MAKECALL_ACTIVE;
1443     MUTEX_EXIT(&conn->conn_data_lock);
1444
1445     /*
1446      * Wake up anyone else who might be giving us a chance to
1447      * run (see code above that avoids resource starvation).
1448      */
1449 #ifdef  RX_ENABLE_LOCKS
1450     CV_BROADCAST(&conn->conn_call_cv);
1451 #else
1452     osi_rxWakeup(conn);
1453 #endif
1454     MUTEX_EXIT(&conn->conn_call_lock);
1455
1456 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
1457     if (call->flags & (RX_CALL_TQ_BUSY | RX_CALL_TQ_CLEARME)) {
1458         osi_Panic("rx_NewCall call about to be used without an empty tq");
1459     }
1460 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1461
1462     MUTEX_EXIT(&call->lock);
1463     USERPRI;
1464
1465     dpf(("rx_NewCall(call %"AFS_PTR_FMT")\n", call));
1466     return call;
1467 }
1468
1469 static int
1470 rxi_HasActiveCalls(struct rx_connection *aconn)
1471 {
1472     int i;
1473     struct rx_call *tcall;
1474     SPLVAR;
1475
1476     NETPRI;
1477     for (i = 0; i < RX_MAXCALLS; i++) {
1478         if ((tcall = aconn->call[i])) {
1479             if ((tcall->state == RX_STATE_ACTIVE)
1480                 || (tcall->state == RX_STATE_PRECALL)) {
1481                 USERPRI;
1482                 return 1;
1483             }
1484         }
1485     }
1486     USERPRI;
1487     return 0;
1488 }
1489
1490 int
1491 rxi_GetCallNumberVector(struct rx_connection *aconn,
1492                         afs_int32 * aint32s)
1493 {
1494     int i;
1495     struct rx_call *tcall;
1496     SPLVAR;
1497
1498     NETPRI;
1499     for (i = 0; i < RX_MAXCALLS; i++) {
1500         if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1501             aint32s[i] = aconn->callNumber[i] + 1;
1502         else
1503             aint32s[i] = aconn->callNumber[i];
1504     }
1505     USERPRI;
1506     return 0;
1507 }
1508
1509 int
1510 rxi_SetCallNumberVector(struct rx_connection *aconn,
1511                         afs_int32 * aint32s)
1512 {
1513     int i;
1514     struct rx_call *tcall;
1515     SPLVAR;
1516
1517     NETPRI;
1518     for (i = 0; i < RX_MAXCALLS; i++) {
1519         if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1520             aconn->callNumber[i] = aint32s[i] - 1;
1521         else
1522             aconn->callNumber[i] = aint32s[i];
1523     }
1524     USERPRI;
1525     return 0;
1526 }
1527
1528 /* Advertise a new service.  A service is named locally by a UDP port
1529  * number plus a 16-bit service id.  Returns (struct rx_service *) 0
1530  * on a failure.
1531  *
1532      char *serviceName;  Name for identification purposes (e.g. the
1533                          service name might be used for probing for
1534                          statistics) */
1535 struct rx_service *
1536 rx_NewServiceHost(afs_uint32 host, u_short port, u_short serviceId,
1537                   char *serviceName, struct rx_securityClass **securityObjects,
1538                   int nSecurityObjects,
1539                   afs_int32(*serviceProc) (struct rx_call * acall))
1540 {
1541     osi_socket socket = OSI_NULLSOCKET;
1542     struct rx_service *tservice;
1543     int i;
1544     SPLVAR;
1545
1546     clock_NewTime();
1547
1548     if (serviceId == 0) {
1549         (osi_Msg
1550          "rx_NewService:  service id for service %s is not non-zero.\n",
1551          serviceName);
1552         return 0;
1553     }
1554     if (port == 0) {
1555         if (rx_port == 0) {
1556             (osi_Msg
1557              "rx_NewService: A non-zero port must be specified on this call if a non-zero port was not provided at Rx initialization (service %s).\n",
1558              serviceName);
1559             return 0;
1560         }
1561         port = rx_port;
1562         socket = rx_socket;
1563     }
1564
1565     tservice = rxi_AllocService();
1566     NETPRI;
1567
1568 #ifdef  RX_ENABLE_LOCKS
1569     MUTEX_INIT(&tservice->svc_data_lock, "svc data lock", MUTEX_DEFAULT, 0);
1570 #endif
1571
1572     for (i = 0; i < RX_MAX_SERVICES; i++) {
1573         struct rx_service *service = rx_services[i];
1574         if (service) {
1575             if (port == service->servicePort && host == service->serviceHost) {
1576                 if (service->serviceId == serviceId) {
1577                     /* The identical service has already been
1578                      * installed; if the caller was intending to
1579                      * change the security classes used by this
1580                      * service, he/she loses. */
1581                     (osi_Msg
1582                      "rx_NewService: tried to install service %s with service id %d, which is already in use for service %s\n",
1583                      serviceName, serviceId, service->serviceName);
1584                     USERPRI;
1585                     rxi_FreeService(tservice);
1586                     return service;
1587                 }
1588                 /* Different service, same port: re-use the socket
1589                  * which is bound to the same port */
1590                 socket = service->socket;
1591             }
1592         } else {
1593             if (socket == OSI_NULLSOCKET) {
1594                 /* If we don't already have a socket (from another
1595                  * service on same port) get a new one */
1596                 socket = rxi_GetHostUDPSocket(host, port);
1597                 if (socket == OSI_NULLSOCKET) {
1598                     USERPRI;
1599                     rxi_FreeService(tservice);
1600                     return 0;
1601                 }
1602             }
1603             service = tservice;
1604             service->socket = socket;
1605             service->serviceHost = host;
1606             service->servicePort = port;
1607             service->serviceId = serviceId;
1608             service->serviceName = serviceName;
1609             service->nSecurityObjects = nSecurityObjects;
1610             service->securityObjects = securityObjects;
1611             service->minProcs = 0;
1612             service->maxProcs = 1;
1613             service->idleDeadTime = 60;
1614             service->idleDeadErr = 0;
1615             service->connDeadTime = rx_connDeadTime;
1616             service->executeRequestProc = serviceProc;
1617             service->checkReach = 0;
1618             service->nSpecific = 0;
1619             service->specific = NULL;
1620             rx_services[i] = service;   /* not visible until now */
1621             USERPRI;
1622             return service;
1623         }
1624     }
1625     USERPRI;
1626     rxi_FreeService(tservice);
1627     (osi_Msg "rx_NewService: cannot support > %d services\n",
1628      RX_MAX_SERVICES);
1629     return 0;
1630 }
1631
1632 /* Set configuration options for all of a service's security objects */
1633
1634 afs_int32
1635 rx_SetSecurityConfiguration(struct rx_service *service,
1636                             rx_securityConfigVariables type,
1637                             void *value)
1638 {
1639     int i;
1640     for (i = 0; i<service->nSecurityObjects; i++) {
1641         if (service->securityObjects[i]) {
1642             RXS_SetConfiguration(service->securityObjects[i], NULL, type,
1643                                  value, NULL);
1644         }
1645     }
1646     return 0;
1647 }
1648
1649 struct rx_service *
1650 rx_NewService(u_short port, u_short serviceId, char *serviceName,
1651               struct rx_securityClass **securityObjects, int nSecurityObjects,
1652               afs_int32(*serviceProc) (struct rx_call * acall))
1653 {
1654     return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
1655 }
1656
1657 /* Generic request processing loop. This routine should be called
1658  * by the implementation dependent rx_ServerProc. If socketp is
1659  * non-null, it will be set to the file descriptor that this thread
1660  * is now listening on. If socketp is null, this routine will never
1661  * returns. */
1662 void
1663 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1664 {
1665     struct rx_call *call;
1666     afs_int32 code;
1667     struct rx_service *tservice = NULL;
1668
1669     for (;;) {
1670         if (newcall) {
1671             call = newcall;
1672             newcall = NULL;
1673         } else {
1674             call = rx_GetCall(threadID, tservice, socketp);
1675             if (socketp && *socketp != OSI_NULLSOCKET) {
1676                 /* We are now a listener thread */
1677                 return;
1678             }
1679         }
1680
1681         /* if server is restarting( typically smooth shutdown) then do not
1682          * allow any new calls.
1683          */
1684
1685         if (rx_tranquil && (call != NULL)) {
1686             SPLVAR;
1687
1688             NETPRI;
1689             MUTEX_ENTER(&call->lock);
1690
1691             rxi_CallError(call, RX_RESTARTING);
1692             rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1693
1694             MUTEX_EXIT(&call->lock);
1695             USERPRI;
1696         }
1697 #ifdef  KERNEL
1698         if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1699 #ifdef RX_ENABLE_LOCKS
1700             AFS_GLOCK();
1701 #endif /* RX_ENABLE_LOCKS */
1702             afs_termState = AFSOP_STOP_AFS;
1703             afs_osi_Wakeup(&afs_termState);
1704 #ifdef RX_ENABLE_LOCKS
1705             AFS_GUNLOCK();
1706 #endif /* RX_ENABLE_LOCKS */
1707             return;
1708         }
1709 #endif
1710
1711         tservice = call->conn->service;
1712
1713         if (tservice->beforeProc)
1714             (*tservice->beforeProc) (call);
1715
1716         code = tservice->executeRequestProc(call);
1717
1718         if (tservice->afterProc)
1719             (*tservice->afterProc) (call, code);
1720
1721         rx_EndCall(call, code);
1722         if (rx_stats_active) {
1723             MUTEX_ENTER(&rx_stats_mutex);
1724             rxi_nCalls++;
1725             MUTEX_EXIT(&rx_stats_mutex);
1726         }
1727     }
1728 }
1729
1730
1731 void
1732 rx_WakeupServerProcs(void)
1733 {
1734     struct rx_serverQueueEntry *np, *tqp;
1735     SPLVAR;
1736
1737     NETPRI;
1738     MUTEX_ENTER(&rx_serverPool_lock);
1739
1740 #ifdef RX_ENABLE_LOCKS
1741     if (rx_waitForPacket)
1742         CV_BROADCAST(&rx_waitForPacket->cv);
1743 #else /* RX_ENABLE_LOCKS */
1744     if (rx_waitForPacket)
1745         osi_rxWakeup(rx_waitForPacket);
1746 #endif /* RX_ENABLE_LOCKS */
1747     MUTEX_ENTER(&freeSQEList_lock);
1748     for (np = rx_FreeSQEList; np; np = tqp) {
1749         tqp = *(struct rx_serverQueueEntry **)np;
1750 #ifdef RX_ENABLE_LOCKS
1751         CV_BROADCAST(&np->cv);
1752 #else /* RX_ENABLE_LOCKS */
1753         osi_rxWakeup(np);
1754 #endif /* RX_ENABLE_LOCKS */
1755     }
1756     MUTEX_EXIT(&freeSQEList_lock);
1757     for (queue_Scan(&rx_idleServerQueue, np, tqp, rx_serverQueueEntry)) {
1758 #ifdef RX_ENABLE_LOCKS
1759         CV_BROADCAST(&np->cv);
1760 #else /* RX_ENABLE_LOCKS */
1761         osi_rxWakeup(np);
1762 #endif /* RX_ENABLE_LOCKS */
1763     }
1764     MUTEX_EXIT(&rx_serverPool_lock);
1765     USERPRI;
1766 }
1767
1768 /* meltdown:
1769  * One thing that seems to happen is that all the server threads get
1770  * tied up on some empty or slow call, and then a whole bunch of calls
1771  * arrive at once, using up the packet pool, so now there are more
1772  * empty calls.  The most critical resources here are server threads
1773  * and the free packet pool.  The "doreclaim" code seems to help in
1774  * general.  I think that eventually we arrive in this state: there
1775  * are lots of pending calls which do have all their packets present,
1776  * so they won't be reclaimed, are multi-packet calls, so they won't
1777  * be scheduled until later, and thus are tying up most of the free
1778  * packet pool for a very long time.
1779  * future options:
1780  * 1.  schedule multi-packet calls if all the packets are present.
1781  * Probably CPU-bound operation, useful to return packets to pool.
1782  * Do what if there is a full window, but the last packet isn't here?
1783  * 3.  preserve one thread which *only* runs "best" calls, otherwise
1784  * it sleeps and waits for that type of call.
1785  * 4.  Don't necessarily reserve a whole window for each thread.  In fact,
1786  * the current dataquota business is badly broken.  The quota isn't adjusted
1787  * to reflect how many packets are presently queued for a running call.
1788  * So, when we schedule a queued call with a full window of packets queued
1789  * up for it, that *should* free up a window full of packets for other 2d-class
1790  * calls to be able to use from the packet pool.  But it doesn't.
1791  *
1792  * NB.  Most of the time, this code doesn't run -- since idle server threads
1793  * sit on the idle server queue and are assigned by "...ReceivePacket" as soon
1794  * as a new call arrives.
1795  */
1796 /* Sleep until a call arrives.  Returns a pointer to the call, ready
1797  * for an rx_Read. */
1798 #ifdef RX_ENABLE_LOCKS
1799 struct rx_call *
1800 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1801 {
1802     struct rx_serverQueueEntry *sq;
1803     struct rx_call *call = (struct rx_call *)0;
1804     struct rx_service *service = NULL;
1805
1806     MUTEX_ENTER(&freeSQEList_lock);
1807
1808     if ((sq = rx_FreeSQEList)) {
1809         rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1810         MUTEX_EXIT(&freeSQEList_lock);
1811     } else {                    /* otherwise allocate a new one and return that */
1812         MUTEX_EXIT(&freeSQEList_lock);
1813         sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1814         MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1815         CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1816     }
1817
1818     MUTEX_ENTER(&rx_serverPool_lock);
1819     if (cur_service != NULL) {
1820         ReturnToServerPool(cur_service);
1821     }
1822     while (1) {
1823         if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1824             struct rx_call *tcall, *ncall, *choice2 = NULL;
1825
1826             /* Scan for eligible incoming calls.  A call is not eligible
1827              * if the maximum number of calls for its service type are
1828              * already executing */
1829             /* One thread will process calls FCFS (to prevent starvation),
1830              * while the other threads may run ahead looking for calls which
1831              * have all their input data available immediately.  This helps
1832              * keep threads from blocking, waiting for data from the client. */
1833             for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1834                 service = tcall->conn->service;
1835                 if (!QuotaOK(service)) {
1836                     continue;
1837                 }
1838                 MUTEX_ENTER(&rx_pthread_mutex);
1839                 if (tno == rxi_fcfs_thread_num
1840                     || !tcall->queue_item_header.next) {
1841                     MUTEX_EXIT(&rx_pthread_mutex);
1842                     /* If we're the fcfs thread , then  we'll just use
1843                      * this call. If we haven't been able to find an optimal
1844                      * choice, and we're at the end of the list, then use a
1845                      * 2d choice if one has been identified.  Otherwise... */
1846                     call = (choice2 ? choice2 : tcall);
1847                     service = call->conn->service;
1848                 } else {
1849                     MUTEX_EXIT(&rx_pthread_mutex);
1850                     if (!queue_IsEmpty(&tcall->rq)) {
1851                         struct rx_packet *rp;
1852                         rp = queue_First(&tcall->rq, rx_packet);
1853                         if (rp->header.seq == 1) {
1854                             if (!meltdown_1pkt
1855                                 || (rp->header.flags & RX_LAST_PACKET)) {
1856                                 call = tcall;
1857                             } else if (rxi_2dchoice && !choice2
1858                                        && !(tcall->flags & RX_CALL_CLEARED)
1859                                        && (tcall->rprev > rxi_HardAckRate)) {
1860                                 choice2 = tcall;
1861                             } else
1862                                 rxi_md2cnt++;
1863                         }
1864                     }
1865                 }
1866                 if (call) {
1867                     break;
1868                 } else {
1869                     ReturnToServerPool(service);
1870                 }
1871             }
1872         }
1873
1874         if (call) {
1875             queue_Remove(call);
1876             MUTEX_EXIT(&rx_serverPool_lock);
1877             MUTEX_ENTER(&call->lock);
1878
1879             if (call->flags & RX_CALL_WAIT_PROC) {
1880                 call->flags &= ~RX_CALL_WAIT_PROC;
1881                 rx_atomic_dec(&rx_nWaiting);
1882             }
1883
1884             if (call->state != RX_STATE_PRECALL || call->error) {
1885                 MUTEX_EXIT(&call->lock);
1886                 MUTEX_ENTER(&rx_serverPool_lock);
1887                 ReturnToServerPool(service);
1888                 call = NULL;
1889                 continue;
1890             }
1891
1892             if (queue_IsEmpty(&call->rq)
1893                 || queue_First(&call->rq, rx_packet)->header.seq != 1)
1894                 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1895
1896             CLEAR_CALL_QUEUE_LOCK(call);
1897             break;
1898         } else {
1899             /* If there are no eligible incoming calls, add this process
1900              * to the idle server queue, to wait for one */
1901             sq->newcall = 0;
1902             sq->tno = tno;
1903             if (socketp) {
1904                 *socketp = OSI_NULLSOCKET;
1905             }
1906             sq->socketp = socketp;
1907             queue_Append(&rx_idleServerQueue, sq);
1908 #ifndef AFS_AIX41_ENV
1909             rx_waitForPacket = sq;
1910 #else
1911             rx_waitingForPacket = sq;
1912 #endif /* AFS_AIX41_ENV */
1913             do {
1914                 CV_WAIT(&sq->cv, &rx_serverPool_lock);
1915 #ifdef  KERNEL
1916                 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1917                     MUTEX_EXIT(&rx_serverPool_lock);
1918                     return (struct rx_call *)0;
1919                 }
1920 #endif
1921             } while (!(call = sq->newcall)
1922                      && !(socketp && *socketp != OSI_NULLSOCKET));
1923             MUTEX_EXIT(&rx_serverPool_lock);
1924             if (call) {
1925                 MUTEX_ENTER(&call->lock);
1926             }
1927             break;
1928         }
1929     }
1930
1931     MUTEX_ENTER(&freeSQEList_lock);
1932     *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1933     rx_FreeSQEList = sq;
1934     MUTEX_EXIT(&freeSQEList_lock);
1935
1936     if (call) {
1937         clock_GetTime(&call->startTime);
1938         call->state = RX_STATE_ACTIVE;
1939         call->mode = RX_MODE_RECEIVING;
1940 #ifdef RX_KERNEL_TRACE
1941         if (ICL_SETACTIVE(afs_iclSetp)) {
1942             int glockOwner = ISAFS_GLOCK();
1943             if (!glockOwner)
1944                 AFS_GLOCK();
1945             afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1946                        __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1947                        call);
1948             if (!glockOwner)
1949                 AFS_GUNLOCK();
1950         }
1951 #endif
1952
1953         rxi_calltrace(RX_CALL_START, call);
1954         dpf(("rx_GetCall(port=%d, service=%d) ==> call %"AFS_PTR_FMT"\n",
1955              call->conn->service->servicePort, call->conn->service->serviceId,
1956              call));
1957
1958         MUTEX_EXIT(&call->lock);
1959         MUTEX_ENTER(&rx_refcnt_mutex);
1960         CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1961         MUTEX_EXIT(&rx_refcnt_mutex);
1962     } else {
1963         dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
1964     }
1965
1966     return call;
1967 }
1968 #else /* RX_ENABLE_LOCKS */
1969 struct rx_call *
1970 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1971 {
1972     struct rx_serverQueueEntry *sq;
1973     struct rx_call *call = (struct rx_call *)0, *choice2;
1974     struct rx_service *service = NULL;
1975     SPLVAR;
1976
1977     NETPRI;
1978     MUTEX_ENTER(&freeSQEList_lock);
1979
1980     if ((sq = rx_FreeSQEList)) {
1981         rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1982         MUTEX_EXIT(&freeSQEList_lock);
1983     } else {                    /* otherwise allocate a new one and return that */
1984         MUTEX_EXIT(&freeSQEList_lock);
1985         sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1986         MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1987         CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1988     }
1989     MUTEX_ENTER(&sq->lock);
1990
1991     if (cur_service != NULL) {
1992         cur_service->nRequestsRunning--;
1993         MUTEX_ENTER(&rx_quota_mutex);
1994         if (cur_service->nRequestsRunning < cur_service->minProcs)
1995             rxi_minDeficit++;
1996         rxi_availProcs++;
1997         MUTEX_EXIT(&rx_quota_mutex);
1998     }
1999     if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
2000         struct rx_call *tcall, *ncall;
2001         /* Scan for eligible incoming calls.  A call is not eligible
2002          * if the maximum number of calls for its service type are
2003          * already executing */
2004         /* One thread will process calls FCFS (to prevent starvation),
2005          * while the other threads may run ahead looking for calls which
2006          * have all their input data available immediately.  This helps
2007          * keep threads from blocking, waiting for data from the client. */
2008         choice2 = (struct rx_call *)0;
2009         for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
2010             service = tcall->conn->service;
2011             if (QuotaOK(service)) {
2012                 MUTEX_ENTER(&rx_pthread_mutex);
2013                 if (tno == rxi_fcfs_thread_num
2014                     || !tcall->queue_item_header.next) {
2015                     MUTEX_EXIT(&rx_pthread_mutex);
2016                     /* If we're the fcfs thread, then  we'll just use
2017                      * this call. If we haven't been able to find an optimal
2018                      * choice, and we're at the end of the list, then use a
2019                      * 2d choice if one has been identified.  Otherwise... */
2020                     call = (choice2 ? choice2 : tcall);
2021                     service = call->conn->service;
2022                 } else {
2023                     MUTEX_EXIT(&rx_pthread_mutex);
2024                     if (!queue_IsEmpty(&tcall->rq)) {
2025                         struct rx_packet *rp;
2026                         rp = queue_First(&tcall->rq, rx_packet);
2027                         if (rp->header.seq == 1
2028                             && (!meltdown_1pkt
2029                                 || (rp->header.flags & RX_LAST_PACKET))) {
2030                             call = tcall;
2031                         } else if (rxi_2dchoice && !choice2
2032                                    && !(tcall->flags & RX_CALL_CLEARED)
2033                                    && (tcall->rprev > rxi_HardAckRate)) {
2034                             choice2 = tcall;
2035                         } else
2036                             rxi_md2cnt++;
2037                     }
2038                 }
2039             }
2040             if (call)
2041                 break;
2042         }
2043     }
2044
2045     if (call) {
2046         queue_Remove(call);
2047         /* we can't schedule a call if there's no data!!! */
2048         /* send an ack if there's no data, if we're missing the
2049          * first packet, or we're missing something between first
2050          * and last -- there's a "hole" in the incoming data. */
2051         if (queue_IsEmpty(&call->rq)
2052             || queue_First(&call->rq, rx_packet)->header.seq != 1
2053             || call->rprev != queue_Last(&call->rq, rx_packet)->header.seq)
2054             rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
2055
2056         call->flags &= (~RX_CALL_WAIT_PROC);
2057         service->nRequestsRunning++;
2058         /* just started call in minProcs pool, need fewer to maintain
2059          * guarantee */
2060         MUTEX_ENTER(&rx_quota_mutex);
2061         if (service->nRequestsRunning <= service->minProcs)
2062             rxi_minDeficit--;
2063         rxi_availProcs--;
2064         MUTEX_EXIT(&rx_quota_mutex);
2065         rx_atomic_dec(&rx_nWaiting);
2066         /* MUTEX_EXIT(&call->lock); */
2067     } else {
2068         /* If there are no eligible incoming calls, add this process
2069          * to the idle server queue, to wait for one */
2070         sq->newcall = 0;
2071         if (socketp) {
2072             *socketp = OSI_NULLSOCKET;
2073         }
2074         sq->socketp = socketp;
2075         queue_Append(&rx_idleServerQueue, sq);
2076         do {
2077             osi_rxSleep(sq);
2078 #ifdef  KERNEL
2079             if (afs_termState == AFSOP_STOP_RXCALLBACK) {
2080                 USERPRI;
2081                 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
2082                 return (struct rx_call *)0;
2083             }
2084 #endif
2085         } while (!(call = sq->newcall)
2086                  && !(socketp && *socketp != OSI_NULLSOCKET));
2087     }
2088     MUTEX_EXIT(&sq->lock);
2089
2090     MUTEX_ENTER(&freeSQEList_lock);
2091     *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
2092     rx_FreeSQEList = sq;
2093     MUTEX_EXIT(&freeSQEList_lock);
2094
2095     if (call) {
2096         clock_GetTime(&call->startTime);
2097         call->state = RX_STATE_ACTIVE;
2098         call->mode = RX_MODE_RECEIVING;
2099 #ifdef RX_KERNEL_TRACE
2100         if (ICL_SETACTIVE(afs_iclSetp)) {
2101             int glockOwner = ISAFS_GLOCK();
2102             if (!glockOwner)
2103                 AFS_GLOCK();
2104             afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
2105                        __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2106                        call);
2107             if (!glockOwner)
2108                 AFS_GUNLOCK();
2109         }
2110 #endif
2111
2112         rxi_calltrace(RX_CALL_START, call);
2113         dpf(("rx_GetCall(port=%d, service=%d) ==> call %p\n",
2114              call->conn->service->servicePort, call->conn->service->serviceId,
2115              call));
2116     } else {
2117         dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
2118     }
2119
2120     USERPRI;
2121
2122     return call;
2123 }
2124 #endif /* RX_ENABLE_LOCKS */
2125
2126
2127
2128 /* Establish a procedure to be called when a packet arrives for a
2129  * call.  This routine will be called at most once after each call,
2130  * and will also be called if there is an error condition on the or
2131  * the call is complete.  Used by multi rx to build a selection
2132  * function which determines which of several calls is likely to be a
2133  * good one to read from.
2134  * NOTE: the way this is currently implemented it is probably only a
2135  * good idea to (1) use it immediately after a newcall (clients only)
2136  * and (2) only use it once.  Other uses currently void your warranty
2137  */
2138 void
2139 rx_SetArrivalProc(struct rx_call *call,
2140                   void (*proc) (struct rx_call * call,
2141                                         void * mh,
2142                                         int index),
2143                   void * handle, int arg)
2144 {
2145     call->arrivalProc = proc;
2146     call->arrivalProcHandle = handle;
2147     call->arrivalProcArg = arg;
2148 }
2149
2150 /* Call is finished (possibly prematurely).  Return rc to the peer, if
2151  * appropriate, and return the final error code from the conversation
2152  * to the caller */
2153
2154 afs_int32
2155 rx_EndCall(struct rx_call *call, afs_int32 rc)
2156 {
2157     struct rx_connection *conn = call->conn;
2158     afs_int32 error;
2159     SPLVAR;
2160
2161     dpf(("rx_EndCall(call %"AFS_PTR_FMT" rc %d error %d abortCode %d)\n",
2162           call, rc, call->error, call->abortCode));
2163
2164     NETPRI;
2165     MUTEX_ENTER(&call->lock);
2166
2167     if (rc == 0 && call->error == 0) {
2168         call->abortCode = 0;
2169         call->abortCount = 0;
2170     }
2171
2172     call->arrivalProc = (void (*)())0;
2173     if (rc && call->error == 0) {
2174         rxi_CallError(call, rc);
2175         call->mode = RX_MODE_ERROR;
2176         /* Send an abort message to the peer if this error code has
2177          * only just been set.  If it was set previously, assume the
2178          * peer has already been sent the error code or will request it
2179          */
2180         rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
2181     }
2182     if (conn->type == RX_SERVER_CONNECTION) {
2183         /* Make sure reply or at least dummy reply is sent */
2184         if (call->mode == RX_MODE_RECEIVING) {
2185             MUTEX_EXIT(&call->lock);
2186             rxi_WriteProc(call, 0, 0);
2187             MUTEX_ENTER(&call->lock);
2188         }
2189         if (call->mode == RX_MODE_SENDING) {
2190             MUTEX_EXIT(&call->lock);
2191             rxi_FlushWrite(call);
2192             MUTEX_ENTER(&call->lock);
2193         }
2194         rxi_calltrace(RX_CALL_END, call);
2195         /* Call goes to hold state until reply packets are acknowledged */
2196         if (call->tfirst + call->nSoftAcked < call->tnext) {
2197             call->state = RX_STATE_HOLD;
2198         } else {
2199             call->state = RX_STATE_DALLY;
2200             rxi_ClearTransmitQueue(call, 0);
2201             rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
2202             rxevent_Cancel(call->keepAliveEvent, call,
2203                            RX_CALL_REFCOUNT_ALIVE);
2204         }
2205     } else {                    /* Client connection */
2206         char dummy;
2207         /* Make sure server receives input packets, in the case where
2208          * no reply arguments are expected */
2209         if ((call->mode == RX_MODE_SENDING)
2210             || (call->mode == RX_MODE_RECEIVING && call->rnext == 1)) {
2211             MUTEX_EXIT(&call->lock);
2212             (void)rxi_ReadProc(call, &dummy, 1);
2213             MUTEX_ENTER(&call->lock);
2214         }
2215
2216         /* If we had an outstanding delayed ack, be nice to the server
2217          * and force-send it now.
2218          */
2219         if (call->delayedAckEvent) {
2220             rxevent_Cancel(call->delayedAckEvent, call,
2221                            RX_CALL_REFCOUNT_DELAY);
2222             call->delayedAckEvent = NULL;
2223             rxi_SendDelayedAck(NULL, call, NULL);
2224         }
2225
2226         /* We need to release the call lock since it's lower than the
2227          * conn_call_lock and we don't want to hold the conn_call_lock
2228          * over the rx_ReadProc call. The conn_call_lock needs to be held
2229          * here for the case where rx_NewCall is perusing the calls on
2230          * the connection structure. We don't want to signal until
2231          * rx_NewCall is in a stable state. Otherwise, rx_NewCall may
2232          * have checked this call, found it active and by the time it
2233          * goes to sleep, will have missed the signal.
2234          */
2235         MUTEX_EXIT(&call->lock);
2236         MUTEX_ENTER(&conn->conn_call_lock);
2237         MUTEX_ENTER(&call->lock);
2238
2239         if (!(call->flags & RX_CALL_PEER_BUSY)) {
2240             conn->lastBusy[call->channel] = 0;
2241         }
2242
2243         MUTEX_ENTER(&conn->conn_data_lock);
2244         conn->flags |= RX_CONN_BUSY;
2245         if (conn->flags & RX_CONN_MAKECALL_WAITING) {
2246             MUTEX_EXIT(&conn->conn_data_lock);
2247 #ifdef  RX_ENABLE_LOCKS
2248             CV_BROADCAST(&conn->conn_call_cv);
2249 #else
2250             osi_rxWakeup(conn);
2251 #endif
2252         }
2253 #ifdef RX_ENABLE_LOCKS
2254         else {
2255             MUTEX_EXIT(&conn->conn_data_lock);
2256         }
2257 #endif /* RX_ENABLE_LOCKS */
2258         call->state = RX_STATE_DALLY;
2259     }
2260     error = call->error;
2261
2262     /* currentPacket, nLeft, and NFree must be zeroed here, because
2263      * ResetCall cannot: ResetCall may be called at splnet(), in the
2264      * kernel version, and may interrupt the macros rx_Read or
2265      * rx_Write, which run at normal priority for efficiency. */
2266     if (call->currentPacket) {
2267 #ifdef RX_TRACK_PACKETS
2268         call->currentPacket->flags &= ~RX_PKTFLAG_CP;
2269 #endif
2270         rxi_FreePacket(call->currentPacket);
2271         call->currentPacket = (struct rx_packet *)0;
2272     }
2273
2274     call->nLeft = call->nFree = call->curlen = 0;
2275
2276     /* Free any packets from the last call to ReadvProc/WritevProc */
2277 #ifdef RXDEBUG_PACKET
2278     call->iovqc -=
2279 #endif /* RXDEBUG_PACKET */
2280         rxi_FreePackets(0, &call->iovq);
2281     MUTEX_EXIT(&call->lock);
2282
2283     MUTEX_ENTER(&rx_refcnt_mutex);
2284     CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
2285     MUTEX_EXIT(&rx_refcnt_mutex);
2286     if (conn->type == RX_CLIENT_CONNECTION) {
2287         MUTEX_ENTER(&conn->conn_data_lock);
2288         conn->flags &= ~RX_CONN_BUSY;
2289         MUTEX_EXIT(&conn->conn_data_lock);
2290         MUTEX_EXIT(&conn->conn_call_lock);
2291     }
2292     USERPRI;
2293     /*
2294      * Map errors to the local host's errno.h format.
2295      */
2296     error = ntoh_syserr_conv(error);
2297     return error;
2298 }
2299
2300 #if !defined(KERNEL)
2301
2302 /* Call this routine when shutting down a server or client (especially
2303  * clients).  This will allow Rx to gracefully garbage collect server
2304  * connections, and reduce the number of retries that a server might
2305  * make to a dead client.
2306  * This is not quite right, since some calls may still be ongoing and
2307  * we can't lock them to destroy them. */
2308 void
2309 rx_Finalize(void)
2310 {
2311     struct rx_connection **conn_ptr, **conn_end;
2312
2313     INIT_PTHREAD_LOCKS;
2314     LOCK_RX_INIT;
2315     if (rxinit_status == 1) {
2316         UNLOCK_RX_INIT;
2317         return;                 /* Already shutdown. */
2318     }
2319     rxi_DeleteCachedConnections();
2320     if (rx_connHashTable) {
2321         MUTEX_ENTER(&rx_connHashTable_lock);
2322         for (conn_ptr = &rx_connHashTable[0], conn_end =
2323              &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
2324              conn_ptr++) {
2325             struct rx_connection *conn, *next;
2326             for (conn = *conn_ptr; conn; conn = next) {
2327                 next = conn->next;
2328                 if (conn->type == RX_CLIENT_CONNECTION) {
2329                     MUTEX_ENTER(&rx_refcnt_mutex);
2330                     conn->refCount++;
2331                     MUTEX_EXIT(&rx_refcnt_mutex);
2332 #ifdef RX_ENABLE_LOCKS
2333                     rxi_DestroyConnectionNoLock(conn);
2334 #else /* RX_ENABLE_LOCKS */
2335                     rxi_DestroyConnection(conn);
2336 #endif /* RX_ENABLE_LOCKS */
2337                 }
2338             }
2339         }
2340 #ifdef RX_ENABLE_LOCKS
2341         while (rx_connCleanup_list) {
2342             struct rx_connection *conn;
2343             conn = rx_connCleanup_list;
2344             rx_connCleanup_list = rx_connCleanup_list->next;
2345             MUTEX_EXIT(&rx_connHashTable_lock);
2346             rxi_CleanupConnection(conn);
2347             MUTEX_ENTER(&rx_connHashTable_lock);
2348         }
2349         MUTEX_EXIT(&rx_connHashTable_lock);
2350 #endif /* RX_ENABLE_LOCKS */
2351     }
2352     rxi_flushtrace();
2353
2354 #ifdef AFS_NT40_ENV
2355     afs_winsockCleanup();
2356 #endif
2357
2358     rxinit_status = 1;
2359     UNLOCK_RX_INIT;
2360 }
2361 #endif
2362
2363 /* if we wakeup packet waiter too often, can get in loop with two
2364     AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2365 void
2366 rxi_PacketsUnWait(void)
2367 {
2368     if (!rx_waitingForPackets) {
2369         return;
2370     }
2371 #ifdef KERNEL
2372     if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2373         return;                 /* still over quota */
2374     }
2375 #endif /* KERNEL */
2376     rx_waitingForPackets = 0;
2377 #ifdef  RX_ENABLE_LOCKS
2378     CV_BROADCAST(&rx_waitingForPackets_cv);
2379 #else
2380     osi_rxWakeup(&rx_waitingForPackets);
2381 #endif
2382     return;
2383 }
2384
2385
2386 /* ------------------Internal interfaces------------------------- */
2387
2388 /* Return this process's service structure for the
2389  * specified socket and service */
2390 static struct rx_service *
2391 rxi_FindService(osi_socket socket, u_short serviceId)
2392 {
2393     struct rx_service **sp;
2394     for (sp = &rx_services[0]; *sp; sp++) {
2395         if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2396             return *sp;
2397     }
2398     return 0;
2399 }
2400
2401 #ifdef RXDEBUG_PACKET
2402 #ifdef KDUMP_RX_LOCK
2403 static struct rx_call_rx_lock *rx_allCallsp = 0;
2404 #else
2405 static struct rx_call *rx_allCallsp = 0;
2406 #endif
2407 #endif /* RXDEBUG_PACKET */
2408
2409 /* Allocate a call structure, for the indicated channel of the
2410  * supplied connection.  The mode and state of the call must be set by
2411  * the caller. Returns the call with mutex locked. */
2412 static struct rx_call *
2413 rxi_NewCall(struct rx_connection *conn, int channel)
2414 {
2415     struct rx_call *call;
2416 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
2417     struct rx_call *cp; /* Call pointer temp */
2418     struct rx_call *nxp;        /* Next call pointer, for queue_Scan */
2419 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2420
2421     dpf(("rxi_NewCall(conn %"AFS_PTR_FMT", channel %d)\n", conn, channel));
2422
2423     /* Grab an existing call structure, or allocate a new one.
2424      * Existing call structures are assumed to have been left reset by
2425      * rxi_FreeCall */
2426     MUTEX_ENTER(&rx_freeCallQueue_lock);
2427
2428 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
2429     /*
2430      * EXCEPT that the TQ might not yet be cleared out.
2431      * Skip over those with in-use TQs.
2432      */
2433     call = NULL;
2434     for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2435         if (!(cp->flags & RX_CALL_TQ_BUSY)) {
2436             call = cp;
2437             break;
2438         }
2439     }
2440     if (call) {
2441 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2442     if (queue_IsNotEmpty(&rx_freeCallQueue)) {
2443         call = queue_First(&rx_freeCallQueue, rx_call);
2444 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2445         queue_Remove(call);
2446         if (rx_stats_active)
2447             rx_atomic_dec(&rx_stats.nFreeCallStructs);
2448         MUTEX_EXIT(&rx_freeCallQueue_lock);
2449         MUTEX_ENTER(&call->lock);
2450         CLEAR_CALL_QUEUE_LOCK(call);
2451 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
2452         /* Now, if TQ wasn't cleared earlier, do it now. */
2453         rxi_WaitforTQBusy(call);
2454         if (call->flags & RX_CALL_TQ_CLEARME) {
2455             rxi_ClearTransmitQueue(call, 1);
2456             /*queue_Init(&call->tq);*/
2457         }
2458 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2459         /* Bind the call to its connection structure */
2460         call->conn = conn;
2461         rxi_ResetCall(call, 1);
2462     } else {
2463
2464         call = rxi_Alloc(sizeof(struct rx_call));
2465 #ifdef RXDEBUG_PACKET
2466         call->allNextp = rx_allCallsp;
2467         rx_allCallsp = call;
2468         call->call_id =
2469             rx_atomic_inc_and_read(&rx_stats.nCallStructs);
2470 #else /* RXDEBUG_PACKET */
2471         rx_atomic_inc(&rx_stats.nCallStructs);
2472 #endif /* RXDEBUG_PACKET */
2473
2474         MUTEX_EXIT(&rx_freeCallQueue_lock);
2475         MUTEX_INIT(&call->lock, "call lock", MUTEX_DEFAULT, NULL);
2476         MUTEX_ENTER(&call->lock);
2477         CV_INIT(&call->cv_twind, "call twind", CV_DEFAULT, 0);
2478         CV_INIT(&call->cv_rq, "call rq", CV_DEFAULT, 0);
2479         CV_INIT(&call->cv_tq, "call tq", CV_DEFAULT, 0);
2480
2481         /* Initialize once-only items */
2482         queue_Init(&call->tq);
2483         queue_Init(&call->rq);
2484         queue_Init(&call->iovq);
2485 #ifdef RXDEBUG_PACKET
2486         call->rqc = call->tqc = call->iovqc = 0;
2487 #endif /* RXDEBUG_PACKET */
2488         /* Bind the call to its connection structure (prereq for reset) */
2489         call->conn = conn;
2490         rxi_ResetCall(call, 1);
2491     }
2492     call->channel = channel;
2493     call->callNumber = &conn->callNumber[channel];
2494     call->rwind = conn->rwind[channel];
2495     call->twind = conn->twind[channel];
2496     /* Note that the next expected call number is retained (in
2497      * conn->callNumber[i]), even if we reallocate the call structure
2498      */
2499     conn->call[channel] = call;
2500     /* if the channel's never been used (== 0), we should start at 1, otherwise
2501      * the call number is valid from the last time this channel was used */
2502     if (*call->callNumber == 0)
2503         *call->callNumber = 1;
2504
2505     return call;
2506 }
2507
2508 /* A call has been inactive long enough that so we can throw away
2509  * state, including the call structure, which is placed on the call
2510  * free list.
2511  *
2512  * call->lock amd rx_refcnt_mutex are held upon entry.
2513  * haveCTLock is set when called from rxi_ReapConnections.
2514  */
2515 static void
2516 rxi_FreeCall(struct rx_call *call, int haveCTLock)
2517 {
2518     int channel = call->channel;
2519     struct rx_connection *conn = call->conn;
2520
2521
2522     if (call->state == RX_STATE_DALLY || call->state == RX_STATE_HOLD)
2523         (*call->callNumber)++;
2524     /*
2525      * We are setting the state to RX_STATE_RESET to
2526      * ensure that no one else will attempt to use this
2527      * call once we drop the refcnt lock. We must drop
2528      * the refcnt lock before calling rxi_ResetCall
2529      * because it cannot be held across acquiring the
2530      * freepktQ lock. NewCall does the same.
2531      */
2532     call->state = RX_STATE_RESET;
2533     MUTEX_EXIT(&rx_refcnt_mutex);
2534     rxi_ResetCall(call, 0);
2535     call->conn->call[channel] = (struct rx_call *)0;
2536
2537     MUTEX_ENTER(&rx_freeCallQueue_lock);
2538     SET_CALL_QUEUE_LOCK(call, &rx_freeCallQueue_lock);
2539 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2540     /* A call may be free even though its transmit queue is still in use.
2541      * Since we search the call list from head to tail, put busy calls at
2542      * the head of the list, and idle calls at the tail.
2543      */
2544     if (call->flags & RX_CALL_TQ_BUSY)
2545         queue_Prepend(&rx_freeCallQueue, call);
2546     else
2547         queue_Append(&rx_freeCallQueue, call);
2548 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2549     queue_Append(&rx_freeCallQueue, call);
2550 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2551     if (rx_stats_active)
2552         rx_atomic_inc(&rx_stats.nFreeCallStructs);
2553     MUTEX_EXIT(&rx_freeCallQueue_lock);
2554
2555     /* Destroy the connection if it was previously slated for
2556      * destruction, i.e. the Rx client code previously called
2557      * rx_DestroyConnection (client connections), or
2558      * rxi_ReapConnections called the same routine (server
2559      * connections).  Only do this, however, if there are no
2560      * outstanding calls. Note that for fine grain locking, there appears
2561      * to be a deadlock in that rxi_FreeCall has a call locked and
2562      * DestroyConnectionNoLock locks each call in the conn. But note a
2563      * few lines up where we have removed this call from the conn.
2564      * If someone else destroys a connection, they either have no
2565      * call lock held or are going through this section of code.
2566      */
2567     MUTEX_ENTER(&conn->conn_data_lock);
2568     if (conn->flags & RX_CONN_DESTROY_ME && !(conn->flags & RX_CONN_MAKECALL_WAITING)) {
2569         MUTEX_ENTER(&rx_refcnt_mutex);
2570         conn->refCount++;
2571         MUTEX_EXIT(&rx_refcnt_mutex);
2572         MUTEX_EXIT(&conn->conn_data_lock);
2573 #ifdef RX_ENABLE_LOCKS
2574         if (haveCTLock)
2575             rxi_DestroyConnectionNoLock(conn);
2576         else
2577             rxi_DestroyConnection(conn);
2578 #else /* RX_ENABLE_LOCKS */
2579         rxi_DestroyConnection(conn);
2580 #endif /* RX_ENABLE_LOCKS */
2581     } else {
2582         MUTEX_EXIT(&conn->conn_data_lock);
2583     }
2584     MUTEX_ENTER(&rx_refcnt_mutex);
2585 }
2586
2587 rx_atomic_t rxi_Allocsize = RX_ATOMIC_INIT(0);
2588 rx_atomic_t rxi_Alloccnt = RX_ATOMIC_INIT(0);
2589
2590 void *
2591 rxi_Alloc(size_t size)
2592 {
2593     char *p;
2594
2595     if (rx_stats_active) {
2596         rx_atomic_add(&rxi_Allocsize, (int) size);
2597         rx_atomic_inc(&rxi_Alloccnt);
2598     }
2599
2600 p = (char *)
2601 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
2602   afs_osi_Alloc_NoSleep(size);
2603 #else
2604   osi_Alloc(size);
2605 #endif
2606     if (!p)
2607         osi_Panic("rxi_Alloc error");
2608     memset(p, 0, size);
2609     return p;
2610 }
2611
2612 void
2613 rxi_Free(void *addr, size_t size)
2614 {
2615     if (rx_stats_active) {
2616         rx_atomic_sub(&rxi_Allocsize, (int) size);
2617         rx_atomic_dec(&rxi_Alloccnt);
2618     }
2619     osi_Free(addr, size);
2620 }
2621
2622 void
2623 rxi_SetPeerMtu(struct rx_peer *peer, afs_uint32 host, afs_uint32 port, int mtu)
2624 {
2625     struct rx_peer **peer_ptr = NULL, **peer_end = NULL;
2626     struct rx_peer *next = NULL;
2627     int hashIndex;
2628
2629     if (!peer) {
2630         MUTEX_ENTER(&rx_peerHashTable_lock);
2631         if (port == 0) {
2632             peer_ptr = &rx_peerHashTable[0];
2633             peer_end = &rx_peerHashTable[rx_hashTableSize];
2634             next = NULL;
2635         resume:
2636             for ( ; peer_ptr < peer_end; peer_ptr++) {
2637                 if (!peer)
2638                     peer = *peer_ptr;
2639                 for ( ; peer; peer = next) {
2640                     next = peer->next;
2641                     if (host == peer->host)
2642                         break;
2643                 }
2644             }
2645         } else {
2646             hashIndex = PEER_HASH(host, port);
2647             for (peer = rx_peerHashTable[hashIndex]; peer; peer = peer->next) {
2648                 if ((peer->host == host) && (peer->port == port))
2649                     break;
2650             }
2651         }
2652     } else {
2653         MUTEX_ENTER(&rx_peerHashTable_lock);
2654     }
2655
2656     if (peer) {
2657         peer->refCount++;
2658         MUTEX_EXIT(&rx_peerHashTable_lock);
2659
2660         MUTEX_ENTER(&peer->peer_lock);
2661         /* We don't handle dropping below min, so don't */
2662         mtu = MAX(mtu, RX_MIN_PACKET_SIZE);
2663         peer->ifMTU=MIN(mtu, peer->ifMTU);
2664         peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2665         /* if we tweaked this down, need to tune our peer MTU too */
2666         peer->MTU = MIN(peer->MTU, peer->natMTU);
2667         /* if we discovered a sub-1500 mtu, degrade */
2668         if (peer->ifMTU < OLD_MAX_PACKET_SIZE)
2669             peer->maxDgramPackets = 1;
2670         /* We no longer have valid peer packet information */
2671         if (peer->maxPacketSize-RX_IPUDP_SIZE > peer->ifMTU)
2672             peer->maxPacketSize = 0;
2673         MUTEX_EXIT(&peer->peer_lock);
2674
2675         MUTEX_ENTER(&rx_peerHashTable_lock);
2676         peer->refCount--;
2677         if (host && !port) {
2678             peer = next;
2679             /* pick up where we left off */
2680             goto resume;
2681         }
2682     }
2683     MUTEX_EXIT(&rx_peerHashTable_lock);
2684 }
2685
2686 /* Find the peer process represented by the supplied (host,port)
2687  * combination.  If there is no appropriate active peer structure, a
2688  * new one will be allocated and initialized
2689  * The origPeer, if set, is a pointer to a peer structure on which the
2690  * refcount will be be decremented. This is used to replace the peer
2691  * structure hanging off a connection structure */
2692 struct rx_peer *
2693 rxi_FindPeer(afs_uint32 host, u_short port,
2694              struct rx_peer *origPeer, int create)
2695 {
2696     struct rx_peer *pp;
2697     int hashIndex;
2698     hashIndex = PEER_HASH(host, port);
2699     MUTEX_ENTER(&rx_peerHashTable_lock);
2700     for (pp = rx_peerHashTable[hashIndex]; pp; pp = pp->next) {
2701         if ((pp->host == host) && (pp->port == port))
2702             break;
2703     }
2704     if (!pp) {
2705         if (create) {
2706             pp = rxi_AllocPeer();       /* This bzero's *pp */
2707             pp->host = host;    /* set here or in InitPeerParams is zero */
2708             pp->port = port;
2709             MUTEX_INIT(&pp->peer_lock, "peer_lock", MUTEX_DEFAULT, 0);
2710             queue_Init(&pp->congestionQueue);
2711             queue_Init(&pp->rpcStats);
2712             pp->next = rx_peerHashTable[hashIndex];
2713             rx_peerHashTable[hashIndex] = pp;
2714             rxi_InitPeerParams(pp);
2715             if (rx_stats_active)
2716                 rx_atomic_inc(&rx_stats.nPeerStructs);
2717         }
2718     }
2719     if (pp && create) {
2720         pp->refCount++;
2721     }
2722     if (origPeer)
2723         origPeer->refCount--;
2724     MUTEX_EXIT(&rx_peerHashTable_lock);
2725     return pp;
2726 }
2727
2728
2729 /* Find the connection at (host, port) started at epoch, and with the
2730  * given connection id.  Creates the server connection if necessary.
2731  * The type specifies whether a client connection or a server
2732  * connection is desired.  In both cases, (host, port) specify the
2733  * peer's (host, pair) pair.  Client connections are not made
2734  * automatically by this routine.  The parameter socket gives the
2735  * socket descriptor on which the packet was received.  This is used,
2736  * in the case of server connections, to check that *new* connections
2737  * come via a valid (port, serviceId).  Finally, the securityIndex
2738  * parameter must match the existing index for the connection.  If a
2739  * server connection is created, it will be created using the supplied
2740  * index, if the index is valid for this service */
2741 struct rx_connection *
2742 rxi_FindConnection(osi_socket socket, afs_uint32 host,
2743                    u_short port, u_short serviceId, afs_uint32 cid,
2744                    afs_uint32 epoch, int type, u_int securityIndex)
2745 {
2746     int hashindex, flag, i;
2747     struct rx_connection *conn;
2748     hashindex = CONN_HASH(host, port, cid, epoch, type);
2749     MUTEX_ENTER(&rx_connHashTable_lock);
2750     rxLastConn ? (conn = rxLastConn, flag = 0) : (conn =
2751                                                   rx_connHashTable[hashindex],
2752                                                   flag = 1);
2753     for (; conn;) {
2754         if ((conn->type == type) && ((cid & RX_CIDMASK) == conn->cid)
2755             && (epoch == conn->epoch)) {
2756             struct rx_peer *pp = conn->peer;
2757             if (securityIndex != conn->securityIndex) {
2758                 /* this isn't supposed to happen, but someone could forge a packet
2759                  * like this, and there seems to be some CM bug that makes this
2760                  * happen from time to time -- in which case, the fileserver
2761                  * asserts. */
2762                 MUTEX_EXIT(&rx_connHashTable_lock);
2763                 return (struct rx_connection *)0;
2764             }
2765             if (pp->host == host && pp->port == port)
2766                 break;
2767             if (type == RX_CLIENT_CONNECTION && pp->port == port)
2768                 break;
2769             /* So what happens when it's a callback connection? */
2770             if (                /*type == RX_CLIENT_CONNECTION && */
2771                    (conn->epoch & 0x80000000))
2772                 break;
2773         }
2774         if (!flag) {
2775             /* the connection rxLastConn that was used the last time is not the
2776              ** one we are looking for now. Hence, start searching in the hash */
2777             flag = 1;
2778             conn = rx_connHashTable[hashindex];
2779         } else
2780             conn = conn->next;
2781     }
2782     if (!conn) {
2783         struct rx_service *service;
2784         if (type == RX_CLIENT_CONNECTION) {
2785             MUTEX_EXIT(&rx_connHashTable_lock);
2786             return (struct rx_connection *)0;
2787         }
2788         service = rxi_FindService(socket, serviceId);
2789         if (!service || (securityIndex >= service->nSecurityObjects)
2790             || (service->securityObjects[securityIndex] == 0)) {
2791             MUTEX_EXIT(&rx_connHashTable_lock);
2792             return (struct rx_connection *)0;
2793         }
2794         conn = rxi_AllocConnection();   /* This bzero's the connection */
2795         MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
2796         MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
2797         CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
2798         conn->next = rx_connHashTable[hashindex];
2799         rx_connHashTable[hashindex] = conn;
2800         conn->peer = rxi_FindPeer(host, port, 0, 1);
2801         conn->type = RX_SERVER_CONNECTION;
2802         conn->lastSendTime = clock_Sec();       /* don't GC immediately */
2803         conn->epoch = epoch;
2804         conn->cid = cid & RX_CIDMASK;
2805         /* conn->serial = conn->lastSerial = 0; */
2806         /* conn->timeout = 0; */
2807         conn->ackRate = RX_FAST_ACK_RATE;
2808         conn->service = service;
2809         conn->serviceId = serviceId;
2810         conn->securityIndex = securityIndex;
2811         conn->securityObject = service->securityObjects[securityIndex];
2812         conn->nSpecific = 0;
2813         conn->specific = NULL;
2814         rx_SetConnDeadTime(conn, service->connDeadTime);
2815         rx_SetConnIdleDeadTime(conn, service->idleDeadTime);
2816         rx_SetServerConnIdleDeadErr(conn, service->idleDeadErr);
2817         for (i = 0; i < RX_MAXCALLS; i++) {
2818             conn->twind[i] = rx_initSendWindow;
2819             conn->rwind[i] = rx_initReceiveWindow;
2820         }
2821         /* Notify security object of the new connection */
2822         RXS_NewConnection(conn->securityObject, conn);
2823         /* XXXX Connection timeout? */
2824         if (service->newConnProc)
2825             (*service->newConnProc) (conn);
2826         if (rx_stats_active)
2827             rx_atomic_inc(&rx_stats.nServerConns);
2828     }
2829
2830     MUTEX_ENTER(&rx_refcnt_mutex);
2831     conn->refCount++;
2832     MUTEX_EXIT(&rx_refcnt_mutex);
2833
2834     rxLastConn = conn;          /* store this connection as the last conn used */
2835     MUTEX_EXIT(&rx_connHashTable_lock);
2836     return conn;
2837 }
2838
2839 /**
2840  * Timeout a call on a busy call channel if appropriate.
2841  *
2842  * @param[in] call The busy call.
2843  *
2844  * @pre 'call' is marked as busy (namely,
2845  *      call->conn->lastBusy[call->channel] != 0)
2846  *
2847  * @pre call->lock is held
2848  * @pre rxi_busyChannelError is nonzero
2849  *
2850  * @note call->lock is dropped and reacquired
2851  */
2852 static void
2853 rxi_CheckBusy(struct rx_call *call)
2854 {
2855     struct rx_connection *conn = call->conn;
2856     int channel = call->channel;
2857     int freechannel = 0;
2858     int i;
2859     afs_uint32 callNumber = *call->callNumber;
2860
2861     MUTEX_EXIT(&call->lock);
2862
2863     MUTEX_ENTER(&conn->conn_call_lock);
2864
2865     /* Are there any other call slots on this conn that we should try? Look for
2866      * slots that are empty and are either non-busy, or were marked as busy
2867      * longer than conn->secondsUntilDead seconds before this call started. */
2868
2869     for (i = 0; i < RX_MAXCALLS && !freechannel; i++) {
2870         if (i == channel) {
2871             /* only look at channels that aren't us */
2872             continue;
2873         }
2874
2875         if (conn->lastBusy[i]) {
2876             /* if this channel looked busy too recently, don't look at it */
2877             if (conn->lastBusy[i] >= call->startTime.sec) {
2878                 continue;
2879             }
2880             if (call->startTime.sec - conn->lastBusy[i] < conn->secondsUntilDead) {
2881                 continue;
2882             }
2883         }
2884
2885         if (conn->call[i]) {
2886             struct rx_call *tcall = conn->call[i];
2887             MUTEX_ENTER(&tcall->lock);
2888             if (tcall->state == RX_STATE_DALLY) {
2889                 freechannel = 1;
2890             }
2891             MUTEX_EXIT(&tcall->lock);
2892         } else {
2893             freechannel = 1;
2894         }
2895     }
2896
2897     MUTEX_EXIT(&conn->conn_call_lock);
2898
2899     MUTEX_ENTER(&call->lock);
2900
2901     /* Since the call->lock and conn->conn_call_lock have been released it is
2902      * possible that (1) the call may no longer be busy and/or (2) the call may
2903      * have been reused by another waiting thread. Therefore, we must confirm
2904      * that the call state has not changed when deciding whether or not to
2905      * force this application thread to retry by forcing a Timeout error. */
2906
2907     if (freechannel && *call->callNumber == callNumber &&
2908         (call->flags & RX_CALL_PEER_BUSY)) {
2909         /* Since 'freechannel' is set, there exists another channel in this
2910          * rx_conn that the application thread might be able to use. We know
2911          * that we have the correct call since callNumber is unchanged, and we
2912          * know that the call is still busy. So, set the call error state to
2913          * rxi_busyChannelError so the application can retry the request,
2914          * presumably on a less-busy call channel. */
2915
2916         rxi_CallError(call, rxi_busyChannelError);
2917     }
2918 }
2919
2920 /* There are two packet tracing routines available for testing and monitoring
2921  * Rx.  One is called just after every packet is received and the other is
2922  * called just before every packet is sent.  Received packets, have had their
2923  * headers decoded, and packets to be sent have not yet had their headers
2924  * encoded.  Both take two parameters: a pointer to the packet and a sockaddr
2925  * containing the network address.  Both can be modified.  The return value, if
2926  * non-zero, indicates that the packet should be dropped.  */
2927
2928 int (*rx_justReceived) (struct rx_packet *, struct sockaddr_in *) = 0;
2929 int (*rx_almostSent) (struct rx_packet *, struct sockaddr_in *) = 0;
2930
2931 /* A packet has been received off the interface.  Np is the packet, socket is
2932  * the socket number it was received from (useful in determining which service
2933  * this packet corresponds to), and (host, port) reflect the host,port of the
2934  * sender.  This call returns the packet to the caller if it is finished with
2935  * it, rather than de-allocating it, just as a small performance hack */
2936
2937 struct rx_packet *
2938 rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
2939                   afs_uint32 host, u_short port, int *tnop,
2940                   struct rx_call **newcallp)
2941 {
2942     struct rx_call *call;
2943     struct rx_connection *conn;
2944     int channel;
2945     afs_uint32 currentCallNumber;
2946     int type;
2947     int skew;
2948 #ifdef RXDEBUG
2949     char *packetType;
2950 #endif
2951     struct rx_packet *tnp;
2952
2953 #ifdef RXDEBUG
2954 /* We don't print out the packet until now because (1) the time may not be
2955  * accurate enough until now in the lwp implementation (rx_Listener only gets
2956  * the time after the packet is read) and (2) from a protocol point of view,
2957  * this is the first time the packet has been seen */
2958     packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
2959         ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
2960     dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %"AFS_PTR_FMT"\n",
2961          np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
2962          np->header.epoch, np->header.cid, np->header.callNumber,
2963          np->header.seq, np->header.flags, np));
2964 #endif
2965
2966     if (np->header.type == RX_PACKET_TYPE_VERSION) {
2967         return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
2968     }
2969
2970     if (np->header.type == RX_PACKET_TYPE_DEBUG) {
2971         return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
2972     }
2973 #ifdef RXDEBUG
2974     /* If an input tracer function is defined, call it with the packet and
2975      * network address.  Note this function may modify its arguments. */
2976     if (rx_justReceived) {
2977         struct sockaddr_in addr;
2978         int drop;
2979         addr.sin_family = AF_INET;
2980         addr.sin_port = port;
2981         addr.sin_addr.s_addr = host;
2982 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
2983         addr.sin_len = sizeof(addr);
2984 #endif /* AFS_OSF_ENV */
2985         drop = (*rx_justReceived) (np, &addr);
2986         /* drop packet if return value is non-zero */
2987         if (drop)
2988             return np;
2989         port = addr.sin_port;   /* in case fcn changed addr */
2990         host = addr.sin_addr.s_addr;
2991     }
2992 #endif
2993
2994     /* If packet was not sent by the client, then *we* must be the client */
2995     type = ((np->header.flags & RX_CLIENT_INITIATED) != RX_CLIENT_INITIATED)
2996         ? RX_CLIENT_CONNECTION : RX_SERVER_CONNECTION;
2997
2998     /* Find the connection (or fabricate one, if we're the server & if
2999      * necessary) associated with this packet */
3000     conn =
3001         rxi_FindConnection(socket, host, port, np->header.serviceId,
3002                            np->header.cid, np->header.epoch, type,
3003                            np->header.securityIndex);
3004
3005     if (!conn) {
3006         /* If no connection found or fabricated, just ignore the packet.
3007          * (An argument could be made for sending an abort packet for
3008          * the conn) */
3009         return np;
3010     }
3011
3012     /* If the connection is in an error state, send an abort packet and ignore
3013      * the incoming packet */
3014     if (conn->error) {
3015         /* Don't respond to an abort packet--we don't want loops! */
3016         MUTEX_ENTER(&conn->conn_data_lock);
3017         if (np->header.type != RX_PACKET_TYPE_ABORT)
3018             np = rxi_SendConnectionAbort(conn, np, 1, 0);
3019         MUTEX_ENTER(&rx_refcnt_mutex);
3020         conn->refCount--;
3021         MUTEX_EXIT(&rx_refcnt_mutex);
3022         MUTEX_EXIT(&conn->conn_data_lock);
3023         return np;
3024     }
3025
3026     /* Check for connection-only requests (i.e. not call specific). */
3027     if (np->header.callNumber == 0) {
3028         switch (np->header.type) {
3029         case RX_PACKET_TYPE_ABORT: {
3030             /* What if the supplied error is zero? */
3031             afs_int32 errcode = ntohl(rx_GetInt32(np, 0));
3032             dpf(("rxi_ReceivePacket ABORT rx_GetInt32 = %d\n", errcode));
3033             rxi_ConnectionError(conn, errcode);
3034             MUTEX_ENTER(&rx_refcnt_mutex);
3035             conn->refCount--;
3036             MUTEX_EXIT(&rx_refcnt_mutex);
3037             return np;
3038         }
3039         case RX_PACKET_TYPE_CHALLENGE:
3040             tnp = rxi_ReceiveChallengePacket(conn, np, 1);
3041             MUTEX_ENTER(&rx_refcnt_mutex);
3042             conn->refCount--;
3043             MUTEX_EXIT(&rx_refcnt_mutex);
3044             return tnp;
3045         case RX_PACKET_TYPE_RESPONSE:
3046             tnp = rxi_ReceiveResponsePacket(conn, np, 1);
3047             MUTEX_ENTER(&rx_refcnt_mutex);
3048             conn->refCount--;
3049             MUTEX_EXIT(&rx_refcnt_mutex);
3050             return tnp;
3051         case RX_PACKET_TYPE_PARAMS:
3052         case RX_PACKET_TYPE_PARAMS + 1:
3053         case RX_PACKET_TYPE_PARAMS + 2:
3054             /* ignore these packet types for now */
3055             MUTEX_ENTER(&rx_refcnt_mutex);
3056             conn->refCount--;
3057             MUTEX_EXIT(&rx_refcnt_mutex);
3058             return np;
3059
3060
3061         default:
3062             /* Should not reach here, unless the peer is broken: send an
3063              * abort packet */
3064             rxi_ConnectionError(conn, RX_PROTOCOL_ERROR);
3065             MUTEX_ENTER(&conn->conn_data_lock);
3066             tnp = rxi_SendConnectionAbort(conn, np, 1, 0);
3067             MUTEX_ENTER(&rx_refcnt_mutex);
3068             conn->refCount--;
3069             MUTEX_EXIT(&rx_refcnt_mutex);
3070             MUTEX_EXIT(&conn->conn_data_lock);
3071             return tnp;
3072         }
3073     }
3074
3075     channel = np->header.cid & RX_CHANNELMASK;
3076     call = conn->call[channel];
3077 #ifdef  RX_ENABLE_LOCKS
3078     if (call)
3079         MUTEX_ENTER(&call->lock);
3080     /* Test to see if call struct is still attached to conn. */
3081     if (call != conn->call[channel]) {
3082         if (call)
3083             MUTEX_EXIT(&call->lock);
3084         if (type == RX_SERVER_CONNECTION) {
3085             call = conn->call[channel];
3086             /* If we started with no call attached and there is one now,
3087              * another thread is also running this routine and has gotten
3088              * the connection channel. We should drop this packet in the tests
3089              * below. If there was a call on this connection and it's now
3090              * gone, then we'll be making a new call below.
3091              * If there was previously a call and it's now different then
3092              * the old call was freed and another thread running this routine
3093              * has created a call on this channel. One of these two threads
3094              * has a packet for the old call and the code below handles those
3095              * cases.
3096              */
3097             if (call)
3098                 MUTEX_ENTER(&call->lock);
3099         } else {
3100             /* This packet can't be for this call. If the new call address is
3101              * 0 then no call is running on this channel. If there is a call
3102              * then, since this is a client connection we're getting data for
3103              * it must be for the previous call.
3104              */
3105             if (rx_stats_active)
3106                 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3107             MUTEX_ENTER(&rx_refcnt_mutex);
3108             conn->refCount--;
3109             MUTEX_EXIT(&rx_refcnt_mutex);
3110             return np;
3111         }
3112     }
3113 #endif
3114     currentCallNumber = conn->callNumber[channel];
3115
3116     if (type == RX_SERVER_CONNECTION) { /* We're the server */
3117         if (np->header.callNumber < currentCallNumber) {
3118             if (rx_stats_active)
3119                 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3120 #ifdef  RX_ENABLE_LOCKS
3121             if (call)
3122                 MUTEX_EXIT(&call->lock);
3123 #endif
3124             MUTEX_ENTER(&rx_refcnt_mutex);
3125             conn->refCount--;
3126             MUTEX_EXIT(&rx_refcnt_mutex);
3127             return np;
3128         }
3129         if (!call) {
3130             MUTEX_ENTER(&conn->conn_call_lock);
3131             call = rxi_NewCall(conn, channel);
3132             MUTEX_EXIT(&conn->conn_call_lock);
3133             *call->callNumber = np->header.callNumber;
3134 #ifdef RXDEBUG
3135             if (np->header.callNumber == 0)
3136                 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%.06d len %d\n",
3137                       np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
3138                       np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
3139                       np->header.flags, np, np->retryTime.sec, np->retryTime.usec / 1000, np->length));
3140 #endif
3141             call->state = RX_STATE_PRECALL;
3142             clock_GetTime(&call->queueTime);
3143             hzero(call->bytesSent);
3144             hzero(call->bytesRcvd);
3145             /*
3146              * If the number of queued calls exceeds the overload
3147              * threshold then abort this call.
3148              */
3149             if ((rx_BusyThreshold > 0) &&
3150                 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
3151                 struct rx_packet *tp;
3152
3153                 rxi_CallError(call, rx_BusyError);
3154                 tp = rxi_SendCallAbort(call, np, 1, 0);
3155                 MUTEX_EXIT(&call->lock);
3156                 MUTEX_ENTER(&rx_refcnt_mutex);
3157                 conn->refCount--;
3158                 MUTEX_EXIT(&rx_refcnt_mutex);
3159                 if (rx_stats_active)
3160                     rx_atomic_inc(&rx_stats.nBusies);
3161                 return tp;
3162             }
3163             rxi_KeepAliveOn(call);
3164         } else if (np->header.callNumber != currentCallNumber) {
3165             /* Wait until the transmit queue is idle before deciding
3166              * whether to reset the current call. Chances are that the
3167              * call will be in ether DALLY or HOLD state once the TQ_BUSY
3168              * flag is cleared.
3169              */
3170 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3171             if (call->state == RX_STATE_ACTIVE) {
3172                 rxi_WaitforTQBusy(call);
3173                 /*
3174                  * If we entered error state while waiting,
3175                  * must call rxi_CallError to permit rxi_ResetCall
3176                  * to processed when the tqWaiter count hits zero.
3177                  */
3178                 if (call->error) {
3179                     rxi_CallError(call, call->error);
3180                     MUTEX_EXIT(&call->lock);
3181                     MUTEX_ENTER(&rx_refcnt_mutex);
3182                     conn->refCount--;
3183                     MUTEX_EXIT(&rx_refcnt_mutex);
3184                     return np;
3185                 }
3186             }
3187 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3188             /* If the new call cannot be taken right now send a busy and set
3189              * the error condition in this call, so that it terminates as
3190              * quickly as possible */
3191             if (call->state == RX_STATE_ACTIVE) {
3192                 struct rx_packet *tp;
3193
3194                 rxi_CallError(call, RX_CALL_DEAD);
3195                 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
3196                                      NULL, 0, 1);
3197                 MUTEX_EXIT(&call->lock);
3198                 MUTEX_ENTER(&rx_refcnt_mutex);
3199                 conn->refCount--;
3200                 MUTEX_EXIT(&rx_refcnt_mutex);
3201                 return tp;
3202             }
3203             rxi_ResetCall(call, 0);
3204             *call->callNumber = np->header.callNumber;
3205 #ifdef RXDEBUG
3206             if (np->header.callNumber == 0)
3207                 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%06d len %d\n",
3208                       np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
3209                       np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
3210                       np->header.flags, np, np->retryTime.sec, np->retryTime.usec, np->length));
3211 #endif
3212             call->state = RX_STATE_PRECALL;
3213             clock_GetTime(&call->queueTime);
3214             hzero(call->bytesSent);
3215             hzero(call->bytesRcvd);
3216             /*
3217              * If the number of queued calls exceeds the overload
3218              * threshold then abort this call.
3219              */
3220             if ((rx_BusyThreshold > 0) &&
3221                 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
3222                 struct rx_packet *tp;
3223
3224                 rxi_CallError(call, rx_BusyError);
3225                 tp = rxi_SendCallAbort(call, np, 1, 0);
3226                 MUTEX_EXIT(&call->lock);
3227                 MUTEX_ENTER(&rx_refcnt_mutex);
3228                 conn->refCount--;
3229                 MUTEX_EXIT(&rx_refcnt_mutex);
3230                 if (rx_stats_active)
3231                     rx_atomic_inc(&rx_stats.nBusies);
3232                 return tp;
3233             }
3234             rxi_KeepAliveOn(call);
3235         } else {
3236             /* Continuing call; do nothing here. */
3237         }
3238     } else {                    /* we're the client */
3239         /* Ignore all incoming acknowledgements for calls in DALLY state */
3240         if (call && (call->state == RX_STATE_DALLY)
3241             && (np->header.type == RX_PACKET_TYPE_ACK)) {
3242             if (rx_stats_active)
3243                 rx_atomic_inc(&rx_stats.ignorePacketDally);
3244 #ifdef  RX_ENABLE_LOCKS
3245             if (call) {
3246                 MUTEX_EXIT(&call->lock);
3247             }
3248 #endif
3249             MUTEX_ENTER(&rx_refcnt_mutex);
3250             conn->refCount--;
3251             MUTEX_EXIT(&rx_refcnt_mutex);
3252             return np;
3253         }
3254
3255         /* Ignore anything that's not relevant to the current call.  If there
3256          * isn't a current call, then no packet is relevant. */
3257         if (!call || (np->header.callNumber != currentCallNumber)) {
3258             if (rx_stats_active)
3259                 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3260 #ifdef  RX_ENABLE_LOCKS
3261             if (call) {
3262                 MUTEX_EXIT(&call->lock);
3263             }
3264 #endif
3265             MUTEX_ENTER(&rx_refcnt_mutex);
3266             conn->refCount--;
3267             MUTEX_EXIT(&rx_refcnt_mutex);
3268             return np;
3269         }
3270         /* If the service security object index stamped in the packet does not
3271          * match the connection's security index, ignore the packet */
3272         if (np->header.securityIndex != conn->securityIndex) {
3273 #ifdef  RX_ENABLE_LOCKS
3274             MUTEX_EXIT(&call->lock);
3275 #endif
3276             MUTEX_ENTER(&rx_refcnt_mutex);
3277             conn->refCount--;
3278             MUTEX_EXIT(&rx_refcnt_mutex);
3279             return np;
3280         }
3281
3282         /* If we're receiving the response, then all transmit packets are
3283          * implicitly acknowledged.  Get rid of them. */
3284         if (np->header.type == RX_PACKET_TYPE_DATA) {
3285 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
3286             /* XXX Hack. Because we must release the global rx lock when
3287              * sending packets (osi_NetSend) we drop all acks while we're
3288              * traversing the tq in rxi_Start sending packets out because
3289              * packets may move to the freePacketQueue as result of being here!
3290              * So we drop these packets until we're safely out of the
3291              * traversing. Really ugly!
3292              * For fine grain RX locking, we set the acked field in the
3293              * packets and let rxi_Start remove them from the transmit queue.
3294              */
3295             if (call->flags & RX_CALL_TQ_BUSY) {
3296 #ifdef  RX_ENABLE_LOCKS
3297                 rxi_SetAcksInTransmitQueue(call);
3298 #else
3299                 MUTEX_ENTER(&rx_refcnt_mutex);
3300                 conn->refCount--;
3301                 MUTEX_EXIT(&rx_refcnt_mutex);
3302                 return np;      /* xmitting; drop packet */
3303 #endif
3304             } else {
3305                 rxi_ClearTransmitQueue(call, 0);
3306             }
3307 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
3308             rxi_ClearTransmitQueue(call, 0);
3309 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3310         } else {
3311             if (np->header.type == RX_PACKET_TYPE_ACK) {
3312                 /* now check to see if this is an ack packet acknowledging that the
3313                  * server actually *lost* some hard-acked data.  If this happens we
3314                  * ignore this packet, as it may indicate that the server restarted in
3315                  * the middle of a call.  It is also possible that this is an old ack
3316                  * packet.  We don't abort the connection in this case, because this
3317                  * *might* just be an old ack packet.  The right way to detect a server
3318                  * restart in the midst of a call is to notice that the server epoch
3319                  * changed, btw.  */
3320                 /* XXX I'm not sure this is exactly right, since tfirst **IS**
3321                  * XXX unacknowledged.  I think that this is off-by-one, but
3322                  * XXX I don't dare change it just yet, since it will
3323                  * XXX interact badly with the server-restart detection
3324                  * XXX code in receiveackpacket.  */
3325                 if (ntohl(rx_GetInt32(np, FIRSTACKOFFSET)) < call->tfirst) {
3326                     if (rx_stats_active)
3327                         rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3328                     MUTEX_EXIT(&call->lock);
3329                     MUTEX_ENTER(&rx_refcnt_mutex);
3330                     conn->refCount--;
3331                     MUTEX_EXIT(&rx_refcnt_mutex);
3332                     return np;
3333                 }
3334             }
3335         }                       /* else not a data packet */
3336     }
3337
3338     osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
3339     /* Set remote user defined status from packet */
3340     call->remoteStatus = np->header.userStatus;
3341
3342     /* Note the gap between the expected next packet and the actual
3343      * packet that arrived, when the new packet has a smaller serial number
3344      * than expected.  Rioses frequently reorder packets all by themselves,
3345      * so this will be quite important with very large window sizes.
3346      * Skew is checked against 0 here to avoid any dependence on the type of
3347      * inPacketSkew (which may be unsigned).  In C, -1 > (unsigned) 0 is always
3348      * true!
3349      * The inPacketSkew should be a smoothed running value, not just a maximum.  MTUXXX
3350      * see CalculateRoundTripTime for an example of how to keep smoothed values.
3351      * I think using a beta of 1/8 is probably appropriate.  93.04.21
3352      */
3353     MUTEX_ENTER(&conn->conn_data_lock);
3354     skew = conn->lastSerial - np->header.serial;
3355     conn->lastSerial = np->header.serial;
3356     MUTEX_EXIT(&conn->conn_data_lock);
3357     if (skew > 0) {
3358         struct rx_peer *peer;
3359         peer = conn->peer;
3360         if (skew > peer->inPacketSkew) {
3361             dpf(("*** In skew changed from %d to %d\n",
3362                   peer->inPacketSkew, skew));
3363             peer->inPacketSkew = skew;
3364         }
3365     }
3366
3367     /* Now do packet type-specific processing */
3368     switch (np->header.type) {
3369     case RX_PACKET_TYPE_DATA:
3370         np = rxi_ReceiveDataPacket(call, np, 1, socket, host, port, tnop,
3371                                    newcallp);
3372         break;
3373     case RX_PACKET_TYPE_ACK:
3374         /* Respond immediately to ack packets requesting acknowledgement
3375          * (ping packets) */
3376         if (np->header.flags & RX_REQUEST_ACK) {
3377             if (call->error)
3378                 (void)rxi_SendCallAbort(call, 0, 1, 0);
3379             else
3380                 (void)rxi_SendAck(call, 0, np->header.serial,
3381                                   RX_ACK_PING_RESPONSE, 1);
3382         }
3383         np = rxi_ReceiveAckPacket(call, np, 1);
3384         break;
3385     case RX_PACKET_TYPE_ABORT: {
3386         /* An abort packet: reset the call, passing the error up to the user. */
3387         /* What if error is zero? */
3388         /* What if the error is -1? the application will treat it as a timeout. */
3389         afs_int32 errdata = ntohl(*(afs_int32 *) rx_DataOf(np));
3390         dpf(("rxi_ReceivePacket ABORT rx_DataOf = %d\n", errdata));
3391         rxi_CallError(call, errdata);
3392         MUTEX_EXIT(&call->lock);
3393         MUTEX_ENTER(&rx_refcnt_mutex);
3394         conn->refCount--;
3395         MUTEX_EXIT(&rx_refcnt_mutex);
3396         return np;              /* xmitting; drop packet */
3397     }
3398     case RX_PACKET_TYPE_BUSY: {
3399         struct clock busyTime;
3400         clock_NewTime();
3401         clock_GetTime(&busyTime);
3402
3403         MUTEX_EXIT(&call->lock);
3404
3405         MUTEX_ENTER(&conn->conn_call_lock);
3406         MUTEX_ENTER(&call->lock);
3407         conn->lastBusy[call->channel] = busyTime.sec;
3408         call->flags |= RX_CALL_PEER_BUSY;
3409         MUTEX_EXIT(&call->lock);
3410         MUTEX_EXIT(&conn->conn_call_lock);
3411
3412         MUTEX_ENTER(&rx_refcnt_mutex);
3413         conn->refCount--;
3414         MUTEX_EXIT(&rx_refcnt_mutex);
3415         return np;
3416     }
3417
3418     case RX_PACKET_TYPE_ACKALL:
3419         /* All packets acknowledged, so we can drop all packets previously
3420          * readied for sending */
3421 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
3422         /* XXX Hack. We because we can't release the global rx lock when
3423          * sending packets (osi_NetSend) we drop all ack pkts while we're
3424          * traversing the tq in rxi_Start sending packets out because
3425          * packets may move to the freePacketQueue as result of being
3426          * here! So we drop these packets until we're safely out of the
3427          * traversing. Really ugly!
3428          * For fine grain RX locking, we set the acked field in the packets
3429          * and let rxi_Start remove the packets from the transmit queue.
3430          */
3431         if (call->flags & RX_CALL_TQ_BUSY) {
3432 #ifdef  RX_ENABLE_LOCKS
3433             rxi_SetAcksInTransmitQueue(call);
3434             break;
3435 #else /* RX_ENABLE_LOCKS */
3436             MUTEX_EXIT(&call->lock);
3437             MUTEX_ENTER(&rx_refcnt_mutex);
3438             conn->refCount--;
3439             MUTEX_EXIT(&rx_refcnt_mutex);
3440             return np;          /* xmitting; drop packet */
3441 #endif /* RX_ENABLE_LOCKS */
3442         }
3443 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3444         rxi_ClearTransmitQueue(call, 0);
3445         break;
3446     default:
3447         /* Should not reach here, unless the peer is broken: send an abort
3448          * packet */
3449         rxi_CallError(call, RX_PROTOCOL_ERROR);
3450         np = rxi_SendCallAbort(call, np, 1, 0);
3451         break;
3452     };
3453     /* Note when this last legitimate packet was received, for keep-alive
3454      * processing.  Note, we delay getting the time until now in the hope that
3455      * the packet will be delivered to the user before any get time is required
3456      * (if not, then the time won't actually be re-evaluated here). */
3457     call->lastReceiveTime = clock_Sec();
3458     /* we've received a legit packet, so the channel is not busy */
3459     call->flags &= ~RX_CALL_PEER_BUSY;
3460     MUTEX_EXIT(&call->lock);
3461     MUTEX_ENTER(&rx_refcnt_mutex);
3462     conn->refCount--;
3463     MUTEX_EXIT(&rx_refcnt_mutex);
3464     return np;
3465 }
3466
3467 /* return true if this is an "interesting" connection from the point of view
3468     of someone trying to debug the system */
3469 int
3470 rxi_IsConnInteresting(struct rx_connection *aconn)
3471 {
3472     int i;
3473     struct rx_call *tcall;
3474
3475     if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
3476         return 1;
3477
3478     for (i = 0; i < RX_MAXCALLS; i++) {
3479         tcall = aconn->call[i];
3480         if (tcall) {
3481             if ((tcall->state == RX_STATE_PRECALL)
3482                 || (tcall->state == RX_STATE_ACTIVE))
3483                 return 1;
3484             if ((tcall->mode == RX_MODE_SENDING)
3485                 || (tcall->mode == RX_MODE_RECEIVING))
3486                 return 1;
3487         }
3488     }
3489     return 0;
3490 }
3491
3492 #ifdef KERNEL
3493 /* if this is one of the last few packets AND it wouldn't be used by the
3494    receiving call to immediately satisfy a read request, then drop it on
3495    the floor, since accepting it might prevent a lock-holding thread from
3496    making progress in its reading. If a call has been cleared while in
3497    the precall state then ignore all subsequent packets until the call
3498    is assigned to a thread. */
3499
3500 static int
3501 TooLow(struct rx_packet *ap, struct rx_call *acall)
3502 {
3503     int rc = 0;
3504
3505     MUTEX_ENTER(&rx_quota_mutex);
3506     if (((ap->header.seq != 1) && (acall->flags & RX_CALL_CLEARED)
3507          && (acall->state == RX_STATE_PRECALL))
3508         || ((rx_nFreePackets < rxi_dataQuota + 2)
3509             && !((ap->header.seq < acall->rnext + rx_initSendWindow)
3510                  && (acall->flags & RX_CALL_READER_WAIT)))) {
3511         rc = 1;
3512     }
3513     MUTEX_EXIT(&rx_quota_mutex);
3514     return rc;
3515 }
3516 #endif /* KERNEL */
3517
3518 static void
3519 rxi_CheckReachEvent(struct rxevent *event, void *arg1, void *arg2)
3520 {
3521     struct rx_connection *conn = arg1;
3522     struct rx_call *acall = arg2;
3523     struct rx_call *call = acall;
3524     struct clock when, now;
3525     int i, waiting;
3526
3527     MUTEX_ENTER(&conn->conn_data_lock);
3528     conn->checkReachEvent = NULL;
3529     waiting = conn->flags & RX_CONN_ATTACHWAIT;
3530     if (event) {
3531         MUTEX_ENTER(&rx_refcnt_mutex);
3532         conn->refCount--;
3533         MUTEX_EXIT(&rx_refcnt_mutex);
3534     }
3535     MUTEX_EXIT(&conn->conn_data_lock);
3536
3537     if (waiting) {
3538         if (!call) {
3539             MUTEX_ENTER(&conn->conn_call_lock);
3540             MUTEX_ENTER(&conn->conn_data_lock);
3541             for (i = 0; i < RX_MAXCALLS; i++) {
3542                 struct rx_call *tc = conn->call[i];
3543                 if (tc && tc->state == RX_STATE_PRECALL) {
3544                     call = tc;
3545                     break;
3546                 }
3547             }
3548             if (!call)
3549                 /* Indicate that rxi_CheckReachEvent is no longer running by
3550                  * clearing the flag.  Must be atomic under conn_data_lock to
3551                  * avoid a new call slipping by: rxi_CheckConnReach holds
3552                  * conn_data_lock while checking RX_CONN_ATTACHWAIT.
3553                  */
3554                 conn->flags &= ~RX_CONN_ATTACHWAIT;
3555             MUTEX_EXIT(&conn->conn_data_lock);
3556             MUTEX_EXIT(&conn->conn_call_lock);
3557         }
3558
3559         if (call) {
3560             if (call != acall)
3561                 MUTEX_ENTER(&call->lock);
3562             rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3563             if (call != acall)
3564                 MUTEX_EXIT(&call->lock);
3565
3566             clock_GetTime(&now);
3567             when = now;
3568             when.sec += RX_CHECKREACH_TIMEOUT;
3569             MUTEX_ENTER(&conn->conn_data_lock);
3570             if (!conn->checkReachEvent) {
3571                 MUTEX_ENTER(&rx_refcnt_mutex);
3572                 conn->refCount++;
3573                 MUTEX_EXIT(&rx_refcnt_mutex);
3574                 conn->checkReachEvent =
3575                     rxevent_PostNow(&when, &now, rxi_CheckReachEvent, conn,
3576                                     NULL);
3577             }
3578             MUTEX_EXIT(&conn->conn_data_lock);
3579         }
3580     }
3581 }
3582
3583 static int
3584 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3585 {
3586     struct rx_service *service = conn->service;
3587     struct rx_peer *peer = conn->peer;
3588     afs_uint32 now, lastReach;
3589
3590     if (service->checkReach == 0)
3591         return 0;
3592
3593     now = clock_Sec();
3594     MUTEX_ENTER(&peer->peer_lock);
3595     lastReach = peer->lastReachTime;
3596     MUTEX_EXIT(&peer->peer_lock);
3597     if (now - lastReach < RX_CHECKREACH_TTL)
3598         return 0;
3599
3600     MUTEX_ENTER(&conn->conn_data_lock);
3601     if (conn->flags & RX_CONN_ATTACHWAIT) {
3602         MUTEX_EXIT(&conn->conn_data_lock);
3603         return 1;
3604     }
3605     conn->flags |= RX_CONN_ATTACHWAIT;
3606     MUTEX_EXIT(&conn->conn_data_lock);
3607     if (!conn->checkReachEvent)
3608         rxi_CheckReachEvent(NULL, conn, call);
3609
3610     return 1;
3611 }
3612
3613 /* try to attach call, if authentication is complete */
3614 static void
3615 TryAttach(struct rx_call *acall, osi_socket socket,
3616           int *tnop, struct rx_call **newcallp,
3617           int reachOverride)
3618 {
3619     struct rx_connection *conn = acall->conn;
3620
3621     if (conn->type == RX_SERVER_CONNECTION
3622         && acall->state == RX_STATE_PRECALL) {
3623         /* Don't attach until we have any req'd. authentication. */
3624         if (RXS_CheckAuthentication(conn->securityObject, conn) == 0) {
3625             if (reachOverride || rxi_CheckConnReach(conn, acall) == 0)
3626                 rxi_AttachServerProc(acall, socket, tnop, newcallp);
3627             /* Note:  this does not necessarily succeed; there
3628              * may not any proc available
3629              */
3630         } else {
3631             rxi_ChallengeOn(acall->conn);
3632         }
3633     }
3634 }
3635
3636 /* A data packet has been received off the interface.  This packet is
3637  * appropriate to the call (the call is in the right state, etc.).  This
3638  * routine can return a packet to the caller, for re-use */
3639
3640 struct rx_packet *
3641 rxi_ReceiveDataPacket(struct rx_call *call,
3642                       struct rx_packet *np, int istack,
3643                       osi_socket socket, afs_uint32 host, u_short port,
3644                       int *tnop, struct rx_call **newcallp)
3645 {
3646     int ackNeeded = 0;          /* 0 means no, otherwise ack_reason */
3647     int newPackets = 0;
3648     int didHardAck = 0;
3649     int haveLast = 0;
3650     afs_uint32 seq;
3651     afs_uint32 serial=0, flags=0;
3652     int isFirst;
3653     struct rx_packet *tnp;
3654     struct clock when, now;
3655     if (rx_stats_active)
3656         rx_atomic_inc(&rx_stats.dataPacketsRead);
3657
3658 #ifdef KERNEL
3659     /* If there are no packet buffers, drop this new packet, unless we can find
3660      * packet buffers from inactive calls */
3661     if (!call->error
3662         && (rxi_OverQuota(RX_PACKET_CLASS_RECEIVE) || TooLow(np, call))) {
3663         MUTEX_ENTER(&rx_freePktQ_lock);
3664         rxi_NeedMorePackets = TRUE;
3665         MUTEX_EXIT(&rx_freePktQ_lock);
3666         if (rx_stats_active)
3667             rx_atomic_inc(&rx_stats.noPacketBuffersOnRead);
3668         call->rprev = np->header.serial;
3669         rxi_calltrace(RX_TRACE_DROP, call);
3670         dpf(("packet %"AFS_PTR_FMT" dropped on receipt - quota problems\n", np));
3671         if (rxi_doreclaim)
3672             rxi_ClearReceiveQueue(call);
3673         clock_GetTime(&now);
3674         when = now;
3675         clock_Add(&when, &rx_softAckDelay);
3676         if (!call->delayedAckEvent
3677             || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3678             rxevent_Cancel(call->delayedAckEvent, call,
3679                            RX_CALL_REFCOUNT_DELAY);
3680             MUTEX_ENTER(&rx_refcnt_mutex);
3681             CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3682             MUTEX_EXIT(&rx_refcnt_mutex);
3683
3684             call->delayedAckEvent =
3685                 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3686         }
3687         /* we've damaged this call already, might as well do it in. */
3688         return np;
3689     }
3690 #endif /* KERNEL */
3691
3692     /*
3693      * New in AFS 3.5, if the RX_JUMBO_PACKET flag is set then this
3694      * packet is one of several packets transmitted as a single
3695      * datagram. Do not send any soft or hard acks until all packets
3696      * in a jumbogram have been processed. Send negative acks right away.
3697      */
3698     for (isFirst = 1, tnp = NULL; isFirst || tnp; isFirst = 0) {
3699         /* tnp is non-null when there are more packets in the
3700          * current jumbo gram */
3701         if (tnp) {
3702             if (np)
3703                 rxi_FreePacket(np);
3704             np = tnp;
3705         }
3706
3707         seq = np->header.seq;
3708         serial = np->header.serial;
3709         flags = np->header.flags;
3710
3711         /* If the call is in an error state, send an abort message */
3712         if (call->error)
3713             return rxi_SendCallAbort(call, np, istack, 0);
3714
3715         /* The RX_JUMBO_PACKET is set in all but the last packet in each
3716          * AFS 3.5 jumbogram. */
3717         if (flags & RX_JUMBO_PACKET) {
3718             tnp = rxi_SplitJumboPacket(np, host, port, isFirst);
3719         } else {
3720             tnp = NULL;
3721         }
3722
3723         if (np->header.spare != 0) {
3724             MUTEX_ENTER(&call->conn->conn_data_lock);
3725             call->conn->flags |= RX_CONN_USING_PACKET_CKSUM;
3726             MUTEX_EXIT(&call->conn->conn_data_lock);
3727         }
3728
3729         /* The usual case is that this is the expected next packet */
3730         if (seq == call->rnext) {
3731
3732             /* Check to make sure it is not a duplicate of one already queued */
3733             if (queue_IsNotEmpty(&call->rq)
3734                 && queue_First(&call->rq, rx_packet)->header.seq == seq) {
3735                 if (rx_stats_active)
3736                     rx_atomic_inc(&rx_stats.dupPacketsRead);
3737                 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - duplicate\n", np));
3738                 rxevent_Cancel(call->delayedAckEvent, call,
3739                                RX_CALL_REFCOUNT_DELAY);
3740                 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3741                 ackNeeded = 0;
3742                 call->rprev = seq;
3743                 continue;
3744             }
3745
3746             /* It's the next packet. Stick it on the receive queue
3747              * for this call. Set newPackets to make sure we wake
3748              * the reader once all packets have been processed */
3749 #ifdef RX_TRACK_PACKETS
3750             np->flags |= RX_PKTFLAG_RQ;
3751 #endif
3752             queue_Prepend(&call->rq, np);
3753 #ifdef RXDEBUG_PACKET
3754             call->rqc++;
3755 #endif /* RXDEBUG_PACKET */
3756             call->nSoftAcks++;
3757             np = NULL;          /* We can't use this anymore */
3758             newPackets = 1;
3759
3760             /* If an ack is requested then set a flag to make sure we
3761              * send an acknowledgement for this packet */
3762             if (flags & RX_REQUEST_ACK) {
3763                 ackNeeded = RX_ACK_REQUESTED;
3764             }
3765
3766             /* Keep track of whether we have received the last packet */
3767             if (flags & RX_LAST_PACKET) {
3768                 call->flags |= RX_CALL_HAVE_LAST;
3769                 haveLast = 1;
3770             }
3771
3772             /* Check whether we have all of the packets for this call */
3773             if (call->flags & RX_CALL_HAVE_LAST) {
3774                 afs_uint32 tseq;        /* temporary sequence number */
3775                 struct rx_packet *tp;   /* Temporary packet pointer */
3776                 struct rx_packet *nxp;  /* Next pointer, for queue_Scan */
3777
3778                 for (tseq = seq, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3779                     if (tseq != tp->header.seq)
3780                         break;
3781                     if (tp->header.flags & RX_LAST_PACKET) {
3782                         call->flags |= RX_CALL_RECEIVE_DONE;
3783                         break;
3784                     }
3785                     tseq++;
3786                 }
3787             }
3788
3789             /* Provide asynchronous notification for those who want it
3790              * (e.g. multi rx) */
3791             if (call->arrivalProc) {
3792                 (*call->arrivalProc) (call, call->arrivalProcHandle,
3793                                       call->arrivalProcArg);
3794                 call->arrivalProc = (void (*)())0;
3795             }
3796
3797             /* Update last packet received */
3798             call->rprev = seq;
3799
3800             /* If there is no server process serving this call, grab
3801              * one, if available. We only need to do this once. If a
3802              * server thread is available, this thread becomes a server
3803              * thread and the server thread becomes a listener thread. */
3804             if (isFirst) {
3805                 TryAttach(call, socket, tnop, newcallp, 0);
3806             }
3807         }
3808         /* This is not the expected next packet. */
3809         else {
3810             /* Determine whether this is a new or old packet, and if it's
3811              * a new one, whether it fits into the current receive window.
3812              * Also figure out whether the packet was delivered in sequence.
3813              * We use the prev variable to determine whether the new packet
3814              * is the successor of its immediate predecessor in the
3815              * receive queue, and the missing flag to determine whether
3816              * any of this packets predecessors are missing.  */
3817
3818             afs_uint32 prev;    /* "Previous packet" sequence number */
3819             struct rx_packet *tp;       /* Temporary packet pointer */
3820             struct rx_packet *nxp;      /* Next pointer, for queue_Scan */
3821             int missing;        /* Are any predecessors missing? */
3822
3823             /* If the new packet's sequence number has been sent to the
3824              * application already, then this is a duplicate */
3825             if (seq < call->rnext) {
3826                 if (rx_stats_active)
3827                     rx_atomic_inc(&rx_stats.dupPacketsRead);
3828                 rxevent_Cancel(call->delayedAckEvent, call,
3829                                RX_CALL_REFCOUNT_DELAY);
3830                 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3831                 ackNeeded = 0;
3832                 call->rprev = seq;
3833                 continue;
3834             }
3835
3836             /* If the sequence number is greater than what can be
3837              * accomodated by the current window, then send a negative
3838              * acknowledge and drop the packet */
3839             if ((call->rnext + call->rwind) <= seq) {
3840                 rxevent_Cancel(call->delayedAckEvent, call,
3841                                RX_CALL_REFCOUNT_DELAY);
3842                 np = rxi_SendAck(call, np, serial, RX_ACK_EXCEEDS_WINDOW,
3843                                  istack);
3844                 ackNeeded = 0;
3845                 call->rprev = seq;
3846                 continue;
3847             }
3848
3849             /* Look for the packet in the queue of old received packets */
3850             for (prev = call->rnext - 1, missing =
3851                  0, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3852                 /*Check for duplicate packet */
3853                 if (seq == tp->header.seq) {
3854                     if (rx_stats_active)
3855                         rx_atomic_inc(&rx_stats.dupPacketsRead);
3856                     rxevent_Cancel(call->delayedAckEvent, call,
3857                                    RX_CALL_REFCOUNT_DELAY);
3858                     np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE,
3859                                      istack);
3860                     ackNeeded = 0;
3861                     call->rprev = seq;
3862                     goto nextloop;
3863                 }
3864                 /* If we find a higher sequence packet, break out and
3865                  * insert the new packet here. */
3866                 if (seq < tp->header.seq)
3867                     break;
3868                 /* Check for missing packet */
3869                 if (tp->header.seq != prev + 1) {
3870                     missing = 1;
3871                 }
3872
3873                 prev = tp->header.seq;
3874             }
3875
3876             /* Keep track of whether we have received the last packet. */
3877             if (flags & RX_LAST_PACKET) {
3878                 call->flags |= RX_CALL_HAVE_LAST;
3879             }
3880
3881             /* It's within the window: add it to the the receive queue.
3882              * tp is left by the previous loop either pointing at the
3883              * packet before which to insert the new packet, or at the
3884              * queue head if the queue is empty or the packet should be
3885              * appended. */
3886 #ifdef RX_TRACK_PACKETS
3887             np->flags |= RX_PKTFLAG_RQ;
3888 #endif
3889 #ifdef RXDEBUG_PACKET
3890             call->rqc++;
3891 #endif /* RXDEBUG_PACKET */
3892             queue_InsertBefore(tp, np);
3893             call->nSoftAcks++;
3894             np = NULL;
3895
3896             /* Check whether we have all of the packets for this call */
3897             if ((call->flags & RX_CALL_HAVE_LAST)
3898                 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
3899                 afs_uint32 tseq;        /* temporary sequence number */
3900
3901                 for (tseq =
3902                      call->rnext, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3903                     if (tseq != tp->header.seq)
3904                         break;
3905                     if (tp->header.flags & RX_LAST_PACKET) {
3906                         call->flags |= RX_CALL_RECEIVE_DONE;
3907                         break;
3908                     }
3909                     tseq++;
3910                 }
3911             }
3912
3913             /* We need to send an ack of the packet is out of sequence,
3914              * or if an ack was requested by the peer. */
3915             if (seq != prev + 1 || missing) {
3916                 ackNeeded = RX_ACK_OUT_OF_SEQUENCE;
3917             } else if (flags & RX_REQUEST_ACK) {
3918                 ackNeeded = RX_ACK_REQUESTED;
3919             }
3920
3921             /* Acknowledge the last packet for each call */
3922             if (flags & RX_LAST_PACKET) {
3923                 haveLast = 1;
3924             }
3925
3926             call->rprev = seq;
3927         }
3928       nextloop:;
3929     }
3930
3931     if (newPackets) {
3932         /*
3933          * If the receiver is waiting for an iovec, fill the iovec
3934          * using the data from the receive queue */
3935         if (call->flags & RX_CALL_IOVEC_WAIT) {
3936             didHardAck = rxi_FillReadVec(call, serial);
3937             /* the call may have been aborted */
3938             if (call->error) {
3939                 return NULL;
3940             }
3941             if (didHardAck) {
3942                 ackNeeded = 0;
3943             }
3944         }
3945
3946         /* Wakeup the reader if any */
3947         if ((call->flags & RX_CALL_READER_WAIT)
3948             && (!(call->flags & RX_CALL_IOVEC_WAIT) || !(call->iovNBytes)
3949                 || (call->iovNext >= call->iovMax)
3950                 || (call->flags & RX_CALL_RECEIVE_DONE))) {
3951             call->flags &= ~RX_CALL_READER_WAIT;
3952 #ifdef  RX_ENABLE_LOCKS
3953             CV_BROADCAST(&call->cv_rq);
3954 #else
3955             osi_rxWakeup(&call->rq);
3956 #endif
3957         }
3958     }
3959
3960     /*
3961      * Send an ack when requested by the peer, or once every
3962      * rxi_SoftAckRate packets until the last packet has been
3963      * received. Always send a soft ack for the last packet in
3964      * the server's reply.
3965      *
3966      * If there was more than one packet received for the call
3967      * and we have received all of them, immediately send an
3968      * RX_PACKET_TYPE_ACKALL packet so that the peer can empty
3969      * its packet transmit queue and cancel all resend events.
3970      *
3971      * When there is only one packet in the call there is a
3972      * chance that we can race with Ping ACKs sent as part of
3973      * connection establishment if the udp packets are delivered
3974      * out of order.  When the race occurs, a two second delay
3975      * will occur while waiting for a new Ping ACK to be sent.
3976      */
3977     if (!isFirst && (call->flags & RX_CALL_RECEIVE_DONE)) {
3978         rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3979         rxi_AckAll(NULL, call, 0);
3980     } else if (ackNeeded) {
3981         rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3982         np = rxi_SendAck(call, np, serial, ackNeeded, istack);
3983     } else if (call->nSoftAcks > (u_short) rxi_SoftAckRate) {
3984         rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3985         np = rxi_SendAck(call, np, serial, RX_ACK_IDLE, istack);
3986     } else if (call->nSoftAcks) {
3987         clock_GetTime(&now);
3988         when = now;
3989         if (haveLast && !(flags & RX_CLIENT_INITIATED)) {
3990             clock_Add(&when, &rx_lastAckDelay);
3991         } else {
3992             clock_Add(&when, &rx_softAckDelay);
3993         }
3994         if (!call->delayedAckEvent
3995             || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3996             rxevent_Cancel(call->delayedAckEvent, call,
3997                            RX_CALL_REFCOUNT_DELAY);
3998             MUTEX_ENTER(&rx_refcnt_mutex);
3999             CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
4000             MUTEX_EXIT(&rx_refcnt_mutex);
4001             call->delayedAckEvent =
4002                 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
4003         }
4004     }
4005
4006     return np;
4007 }
4008
4009 #ifdef  ADAPT_WINDOW
4010 static void rxi_ComputeRate();
4011 #endif
4012
4013 static void
4014 rxi_UpdatePeerReach(struct rx_connection *conn, struct rx_call *acall)
4015 {
4016     struct rx_peer *peer = conn->peer;
4017
4018     MUTEX_ENTER(&peer->peer_lock);
4019     peer->lastReachTime = clock_Sec();
4020     MUTEX_EXIT(&peer->peer_lock);
4021
4022     MUTEX_ENTER(&conn->conn_data_lock);
4023     if (conn->flags & RX_CONN_ATTACHWAIT) {
4024         int i;
4025
4026         conn->flags &= ~RX_CONN_ATTACHWAIT;
4027         MUTEX_EXIT(&conn->conn_data_lock);
4028
4029         for (i = 0; i < RX_MAXCALLS; i++) {
4030             struct rx_call *call = conn->call[i];
4031             if (call) {
4032                 if (call != acall)
4033                     MUTEX_ENTER(&call->lock);
4034                 /* tnop can be null if newcallp is null */
4035                 TryAttach(call, (osi_socket) - 1, NULL, NULL, 1);
4036                 if (call != acall)
4037                     MUTEX_EXIT(&call->lock);
4038             }
4039         }
4040     } else
4041         MUTEX_EXIT(&conn->conn_data_lock);
4042 }
4043
4044 #if defined(RXDEBUG) && defined(AFS_NT40_ENV)
4045 static const char *
4046 rx_ack_reason(int reason)
4047 {
4048     switch (reason) {
4049     case RX_ACK_REQUESTED:
4050         return "requested";
4051     case RX_ACK_DUPLICATE:
4052         return "duplicate";
4053     case RX_ACK_OUT_OF_SEQUENCE:
4054         return "sequence";
4055     case RX_ACK_EXCEEDS_WINDOW:
4056         return "window";
4057     case RX_ACK_NOSPACE:
4058         return "nospace";
4059     case RX_ACK_PING:
4060         return "ping";
4061     case RX_ACK_PING_RESPONSE:
4062         return "response";
4063     case RX_ACK_DELAY:
4064         return "delay";
4065     case RX_ACK_IDLE:
4066         return "idle";
4067     default:
4068         return "unknown!!";
4069     }
4070 }
4071 #endif
4072
4073
4074 /* The real smarts of the whole thing.  */
4075 struct rx_packet *
4076 rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
4077                      int istack)
4078 {
4079     struct rx_ackPacket *ap;
4080     int nAcks;
4081     struct rx_packet *tp;
4082     struct rx_packet *nxp;      /* Next packet pointer for queue_Scan */
4083     struct rx_connection *conn = call->conn;
4084     struct rx_peer *peer = conn->peer;
4085     struct clock now;           /* Current time, for RTT calculations */
4086     afs_uint32 first;
4087     afs_uint32 prev;
4088     afs_uint32 serial;
4089     /* because there are CM's that are bogus, sending weird values for this. */
4090     afs_uint32 skew = 0;
4091     int nbytes;
4092     int missing;
4093     int acked;
4094     int nNacked = 0;
4095     int newAckCount = 0;
4096     int maxDgramPackets = 0;    /* Set if peer supports AFS 3.5 jumbo datagrams */
4097     int pktsize = 0;            /* Set if we need to update the peer mtu */
4098     int conn_data_locked = 0;
4099
4100     if (rx_stats_active)
4101         rx_atomic_inc(&rx_stats.ackPacketsRead);
4102     ap = (struct rx_ackPacket *)rx_DataOf(np);
4103     nbytes = rx_Contiguous(np) - (int)((ap->acks) - (u_char *) ap);
4104     if (nbytes < 0)
4105         return np;              /* truncated ack packet */
4106
4107     /* depends on ack packet struct */
4108     nAcks = MIN((unsigned)nbytes, (unsigned)ap->nAcks);
4109     first = ntohl(ap->firstPacket);
4110     prev = ntohl(ap->previousPacket);
4111     serial = ntohl(ap->serial);
4112     /* temporarily disabled -- needs to degrade over time
4113      * skew = ntohs(ap->maxSkew); */
4114
4115     /* Ignore ack packets received out of order */
4116     if (first < call->tfirst ||
4117         (first == call->tfirst && prev < call->tprev)) {
4118         return np;
4119     }
4120
4121     call->tprev = prev;
4122
4123     if (np->header.flags & RX_SLOW_START_OK) {
4124         call->flags |= RX_CALL_SLOW_START_OK;
4125     }
4126
4127     if (ap->reason == RX_ACK_PING_RESPONSE)
4128         rxi_UpdatePeerReach(conn, call);
4129
4130     if (conn->lastPacketSizeSeq) {
4131         MUTEX_ENTER(&conn->conn_data_lock);
4132         conn_data_locked = 1;
4133         if ((first > conn->lastPacketSizeSeq) && (conn->lastPacketSize)) {
4134             pktsize = conn->lastPacketSize;
4135             conn->lastPacketSize = conn->lastPacketSizeSeq = 0;
4136         }
4137     }
4138     if ((ap->reason == RX_ACK_PING_RESPONSE) && (conn->lastPingSizeSer)) {
4139         if (!conn_data_locked) {
4140             MUTEX_ENTER(&conn->conn_data_lock);
4141             conn_data_locked = 1;
4142         }
4143         if ((conn->lastPingSizeSer == serial) && (conn->lastPingSize)) {
4144             /* process mtu ping ack */
4145             pktsize = conn->lastPingSize;
4146             conn->lastPingSizeSer = conn->lastPingSize = 0;
4147         }
4148     }
4149
4150     if (conn_data_locked) {
4151         MUTEX_EXIT(&conn->conn_data_lock);
4152         conn_data_locked = 0;
4153     }
4154 #ifdef RXDEBUG
4155 #ifdef AFS_NT40_ENV
4156     if (rxdebug_active) {
4157         char msg[512];
4158         size_t len;
4159
4160         len = _snprintf(msg, sizeof(msg),
4161                         "tid[%d] RACK: reason %s serial %u previous %u seq %u skew %d first %u acks %u space %u ",
4162                          GetCurrentThreadId(), rx_ack_reason(ap->reason),
4163                          ntohl(ap->serial), ntohl(ap->previousPacket),
4164                          (unsigned int)np->header.seq, (unsigned int)skew,
4165                          ntohl(ap->firstPacket), ap->nAcks, ntohs(ap->bufferSpace) );
4166         if (nAcks) {
4167             int offset;
4168
4169             for (offset = 0; offset < nAcks && len < sizeof(msg); offset++)
4170                 msg[len++] = (ap->acks[offset] == RX_ACK_TYPE_NACK ? '-' : '*');
4171         }
4172         msg[len++]='\n';
4173         msg[len] = '\0';
4174         OutputDebugString(msg);
4175     }
4176 #else /* AFS_NT40_ENV */
4177     if (rx_Log) {
4178         fprintf(rx_Log,
4179                 "RACK: reason %x previous %u seq %u serial %u skew %d first %u",
4180                 ap->reason, ntohl(ap->previousPacket),
4181                 (unsigned int)np->header.seq, (unsigned int)serial,
4182                 (unsigned int)skew, ntohl(ap->firstPacket));
4183         if (nAcks) {
4184             int offset;
4185             for (offset = 0; offset < nAcks; offset++)
4186                 putc(ap->acks[offset] == RX_ACK_TYPE_NACK ? '-' : '*',
4187                      rx_Log);
4188         }
4189         putc('\n', rx_Log);
4190     }
4191 #endif /* AFS_NT40_ENV */
4192 #endif
4193
4194     MUTEX_ENTER(&peer->peer_lock);
4195     if (pktsize) {
4196         /*
4197          * Start somewhere. Can't assume we can send what we can receive,
4198          * but we are clearly receiving.
4199          */
4200         if (!peer->maxPacketSize)
4201             peer->maxPacketSize = RX_MIN_PACKET_SIZE+RX_IPUDP_SIZE;
4202
4203         if (pktsize > peer->maxPacketSize) {
4204             peer->maxPacketSize = pktsize;
4205             if ((pktsize-RX_IPUDP_SIZE > peer->ifMTU)) {
4206                 peer->ifMTU=pktsize-RX_IPUDP_SIZE;
4207                 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
4208                 rxi_ScheduleGrowMTUEvent(call, 1);
4209             }
4210         }
4211     }
4212
4213     /* Update the outgoing packet skew value to the latest value of
4214      * the peer's incoming packet skew value.  The ack packet, of
4215      * course, could arrive out of order, but that won't affect things
4216      * much */
4217     peer->outPacketSkew = skew;
4218
4219
4220     clock_GetTime(&now);
4221
4222     /* The transmit queue splits into 4 sections.
4223      *
4224      * The first section is packets which have now been acknowledged
4225      * by a window size change in the ack. These have reached the
4226      * application layer, and may be discarded. These are packets
4227      * with sequence numbers < ap->firstPacket.
4228      *
4229      * The second section is packets which have sequence numbers in
4230      * the range ap->firstPacket to ap->firstPacket + ap->nAcks. The
4231      * contents of the packet's ack array determines whether these
4232      * packets are acknowledged or not.
4233      *
4234      * The third section is packets which fall above the range
4235      * addressed in the ack packet. These have not yet been received
4236      * by the peer.
4237      *
4238      * The four section is packets which have not yet been transmitted.
4239      * These packets will have a retryTime of 0.
4240      */
4241
4242     /* First section - implicitly acknowledged packets that can be
4243      * disposed of
4244      */
4245
4246     tp = queue_First(&call->tq, rx_packet);
4247     while(!queue_IsEnd(&call->tq, tp) && tp->header.seq < first) {
4248         struct rx_packet *next;
4249
4250         next = queue_Next(tp, rx_packet);
4251         call->tfirst = tp->header.seq + 1;
4252
4253         if (!(tp->flags & RX_PKTFLAG_ACKED)) {
4254             newAckCount++;
4255             rxi_ComputeRoundTripTime(tp, ap, call->conn->peer, &now);
4256         }
4257
4258 #ifdef ADAPT_WINDOW
4259         rxi_ComputeRate(call->conn->peer, call, p, np, ap->reason);
4260 #endif
4261
4262 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
4263         /* XXX Hack. Because we have to release the global rx lock when sending
4264          * packets (osi_NetSend) we drop all acks while we're traversing the tq
4265          * in rxi_Start sending packets out because packets may move to the
4266          * freePacketQueue as result of being here! So we drop these packets until
4267          * we're safely out of the traversing. Really ugly!
4268          * To make it even uglier, if we're using fine grain locking, we can
4269          * set the ack bits in the packets and have rxi_Start remove the packets
4270          * when it's done transmitting.
4271          */
4272         if (call->flags & RX_CALL_TQ_BUSY) {
4273 #ifdef RX_ENABLE_LOCKS
4274             tp->flags |= RX_PKTFLAG_ACKED;
4275             call->flags |= RX_CALL_TQ_SOME_ACKED;
4276 #else /* RX_ENABLE_LOCKS */
4277             break;
4278 #endif /* RX_ENABLE_LOCKS */
4279         } else
4280 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
4281         {
4282             queue_Remove(tp);
4283 #ifdef RX_TRACK_PACKETS
4284             tp->flags &= ~RX_PKTFLAG_TQ;
4285 #endif
4286 #ifdef RXDEBUG_PACKET
4287             call->tqc--;
4288 #endif /* RXDEBUG_PACKET */
4289             rxi_FreePacket(tp); /* rxi_FreePacket mustn't wake up anyone, preemptively. */
4290         }
4291         tp = next;
4292     }
4293
4294 #ifdef ADAPT_WINDOW
4295     /* Give rate detector a chance to respond to ping requests */
4296     if (ap->reason == RX_ACK_PING_RESPONSE) {
4297         rxi_ComputeRate(peer, call, 0, np, ap->reason);
4298     }
4299 #endif
4300
4301     /* N.B. we don't turn off any timers here.  They'll go away by themselves, anyway */
4302
4303     /* Second section of the queue - packets for which we are receiving
4304      * soft ACKs
4305      *
4306      * Go through the explicit acks/nacks and record the results in
4307      * the waiting packets.  These are packets that can't be released
4308      * yet, even with a positive acknowledge.  This positive
4309      * acknowledge only means the packet has been received by the
4310      * peer, not that it will be retained long enough to be sent to
4311      * the peer's upper level.  In addition, reset the transmit timers
4312      * of any missing packets (those packets that must be missing
4313      * because this packet was out of sequence) */
4314
4315     call->nSoftAcked = 0;
4316     missing = 0;
4317     while (!queue_IsEnd(&call->tq, tp) && tp->header.seq < first + nAcks) {
4318         /* Set the acknowledge flag per packet based on the
4319          * information in the ack packet. An acknowlegded packet can
4320          * be downgraded when the server has discarded a packet it
4321          * soacked previously, or when an ack packet is received
4322          * out of sequence. */
4323         if (ap->acks[tp->header.seq - first] == RX_ACK_TYPE_ACK) {
4324             if (!(tp->flags & RX_PKTFLAG_ACKED)) {
4325                 newAckCount++;
4326                 tp->flags |= RX_PKTFLAG_ACKED;
4327
4328                 rxi_ComputeRoundTripTime(tp, ap, call->conn->peer, &now);
4329 #ifdef ADAPT_WINDOW
4330                 rxi_ComputeRate(call->conn->peer, call, tp, np, ap->reason);
4331 #endif
4332             }
4333             if (missing) {
4334                 nNacked++;
4335             } else {
4336                 call->nSoftAcked++;
4337             }
4338         } else /* RX_ACK_TYPE_NACK */ {
4339             tp->flags &= ~RX_PKTFLAG_ACKED;
4340             missing = 1;
4341         }
4342
4343         /* If packet isn't yet acked, and it has been transmitted at least
4344          * once, reset retransmit time using latest timeout
4345          * ie, this should readjust the retransmit timer for all outstanding
4346          * packets...  So we don't just retransmit when we should know better*/
4347
4348         if (!(tp->flags & RX_PKTFLAG_ACKED) && !clock_IsZero(&tp->retryTime)) {
4349             tp->retryTime = tp->timeSent;
4350             clock_Add(&tp->retryTime, &peer->timeout);
4351             /* shift by eight because one quarter-sec ~ 256 milliseconds */
4352             clock_Addmsec(&(tp->retryTime), ((afs_uint32) tp->backoff) << 8);
4353         }
4354
4355         tp = queue_Next(tp, rx_packet);
4356     }
4357
4358     /* The third case, packets which the ack packet tells us
4359      * nothing about at all. We just need to adjust the retryTime to match
4360      * any new timeouts that have been calculated for this peer.
4361      * We use the fact that we send in order to terminate this loop as soon as
4362      * we find a packet that has not been sent.
4363      */
4364
4365     while (!queue_IsEnd(&call->tq, tp) && !clock_IsZero(&tp->retryTime)) {
4366         tp->retryTime = tp->timeSent;
4367         clock_Add(&tp->retryTime, &peer->timeout);
4368         clock_Addmsec(&tp->retryTime, ((afs_uint32) tp->backoff) << 8);
4369         tp = queue_Next(tp, rx_packet);
4370     }
4371
4372     /* The fourth set of packets - those which have yet to be transmitted,
4373      * we don't care about at all here */
4374
4375     /* If the window has been extended by this acknowledge packet,
4376      * then wakeup a sender waiting in alloc for window space, or try
4377      * sending packets now, if he's been sitting on packets due to
4378      * lack of window space */
4379     if (call->tnext < (call->tfirst + call->twind)) {
4380 #ifdef  RX_ENABLE_LOCKS
4381         CV_SIGNAL(&call->cv_twind);
4382 #else
4383         if (call->flags & RX_CALL_WAIT_WINDOW_ALLOC) {
4384             call->flags &= ~RX_CALL_WAIT_WINDOW_ALLOC;
4385             osi_rxWakeup(&call->twind);
4386         }
4387 #endif
4388         if (call->flags & RX_CALL_WAIT_WINDOW_SEND) {
4389             call->flags &= ~RX_CALL_WAIT_WINDOW_SEND;
4390         }
4391     }
4392
4393     /* if the ack packet has a receivelen field hanging off it,
4394      * update our state */
4395     if (np->length >= rx_AckDataSize(ap->nAcks) + 2 * sizeof(afs_int32)) {
4396         afs_uint32 tSize;
4397
4398         /* If the ack packet has a "recommended" size that is less than
4399          * what I am using now, reduce my size to match */
4400         rx_packetread(np, rx_AckDataSize(ap->nAcks) + (int)sizeof(afs_int32),
4401                       (int)sizeof(afs_int32), &tSize);
4402         tSize = (afs_uint32) ntohl(tSize);
4403         peer->natMTU = rxi_AdjustIfMTU(MIN(tSize, peer->ifMTU));
4404
4405         /* Get the maximum packet size to send to this peer */
4406         rx_packetread(np, rx_AckDataSize(ap->nAcks), (int)sizeof(afs_int32),
4407                       &tSize);
4408         tSize = (afs_uint32) ntohl(tSize);
4409         tSize = (afs_uint32) MIN(tSize, rx_MyMaxSendSize);
4410         tSize = rxi_AdjustMaxMTU(peer->natMTU, tSize);
4411
4412         /* sanity check - peer might have restarted with different params.
4413          * If peer says "send less", dammit, send less...  Peer should never
4414          * be unable to accept packets of the size that prior AFS versions would
4415          * send without asking.  */
4416         if (peer->maxMTU != tSize) {
4417             if (peer->maxMTU > tSize) /* possible cong., maxMTU decreased */
4418                 peer->congestSeq++;
4419             peer->maxMTU = tSize;
4420             peer->MTU = MIN(tSize, peer->MTU);
4421             call->MTU = MIN(call->MTU, tSize);
4422         }
4423
4424         if (np->length == rx_AckDataSize(ap->nAcks) + 3 * sizeof(afs_int32)) {
4425             /* AFS 3.4a */
4426             rx_packetread(np,
4427                           rx_AckDataSize(ap->nAcks) + 2 * (int)sizeof(afs_int32),
4428                           (int)sizeof(afs_int32), &tSize);
4429             tSize = (afs_uint32) ntohl(tSize);  /* peer's receive window, if it's */
4430             if (tSize < call->twind) {  /* smaller than our send */
4431                 call->twind = tSize;    /* window, we must send less... */
4432                 call->ssthresh = MIN(call->twind, call->ssthresh);
4433                 call->conn->twind[call->channel] = call->twind;
4434             }
4435
4436             /* Only send jumbograms to 3.4a fileservers. 3.3a RX gets the
4437              * network MTU confused with the loopback MTU. Calculate the
4438              * maximum MTU here for use in the slow start code below.
4439              */
4440             /* Did peer restart with older RX version? */
4441             if (peer->maxDgramPackets > 1) {
4442                 peer->maxDgramPackets = 1;
4443             }
4444         } else if (np->length >=
4445                    rx_AckDataSize(ap->nAcks) + 4 * sizeof(afs_int32)) {
4446             /* AFS 3.5 */
4447             rx_packetread(np,
4448                           rx_AckDataSize(ap->nAcks) + 2 * (int)sizeof(afs_int32),
4449                           sizeof(afs_int32), &tSize);
4450             tSize = (afs_uint32) ntohl(tSize);
4451             /*
4452              * As of AFS 3.5 we set the send window to match the receive window.
4453              */
4454             if (tSize < call->twind) {
4455                 call->twind = tSize;
4456                 call->conn->twind[call->channel] = call->twind;
4457                 call->ssthresh = MIN(call->twind, call->ssthresh);
4458             } else if (tSize > call->twind) {
4459                 call->twind = tSize;
4460                 call->conn->twind[call->channel] = call->twind;
4461             }
4462
4463             /*
4464              * As of AFS 3.5, a jumbogram is more than one fixed size
4465              * packet transmitted in a single UDP datagram. If the remote
4466              * MTU is smaller than our local MTU then never send a datagram
4467              * larger than the natural MTU.
4468              */
4469             rx_packetread(np,
4470                           rx_AckDataSize(ap->nAcks) + 3 * (int)sizeof(afs_int32),
4471                           (int)sizeof(afs_int32), &tSize);
4472             maxDgramPackets = (afs_uint32) ntohl(tSize);
4473             maxDgramPackets = MIN(maxDgramPackets, rxi_nDgramPackets);
4474             maxDgramPackets =
4475                 MIN(maxDgramPackets, (int)(peer->ifDgramPackets));
4476             if (maxDgramPackets > 1) {
4477                 peer->maxDgramPackets = maxDgramPackets;
4478                 call->MTU = RX_JUMBOBUFFERSIZE + RX_HEADER_SIZE;
4479             } else {
4480                 peer->maxDgramPackets = 1;
4481                 call->MTU = peer->natMTU;
4482             }
4483         } else if (peer->maxDgramPackets > 1) {
4484             /* Restarted with lower version of RX */
4485             peer->maxDgramPackets = 1;
4486         }
4487     } else if (peer->maxDgramPackets > 1
4488                || peer->maxMTU != OLD_MAX_PACKET_SIZE) {
4489         /* Restarted with lower version of RX */
4490         peer->maxMTU = OLD_MAX_PACKET_SIZE;
4491         peer->natMTU = OLD_MAX_PACKET_SIZE;
4492         peer->MTU = OLD_MAX_PACKET_SIZE;
4493         peer->maxDgramPackets = 1;
4494         peer->nDgramPackets = 1;
4495         peer->congestSeq++;
4496         call->MTU = OLD_MAX_PACKET_SIZE;
4497     }
4498
4499     if (nNacked) {
4500         /*
4501          * Calculate how many datagrams were successfully received after
4502          * the first missing packet and adjust the negative ack counter
4503          * accordingly.
4504          */
4505         call->nAcks = 0;
4506         call->nNacks++;
4507         nNacked = (nNacked + call->nDgramPackets - 1) / call->nDgramPackets;
4508         if (call->nNacks < nNacked) {
4509             call->nNacks = nNacked;
4510         }
4511     } else {
4512         call->nAcks += newAckCount;
4513         call->nNacks = 0;
4514     }
4515
4516     if (call->flags & RX_CALL_FAST_RECOVER) {
4517         if (nNacked) {
4518             call->cwind = MIN((int)(call->cwind + 1), rx_maxSendWindow);
4519         } else {
4520             call->flags &= ~RX_CALL_FAST_RECOVER;
4521             call->cwind = call->nextCwind;
4522             call->nextCwind = 0;
4523             call->nAcks = 0;
4524         }
4525         call->nCwindAcks = 0;
4526     } else if (nNacked && call->nNacks >= (u_short) rx_nackThreshold) {
4527         /* Three negative acks in a row trigger congestion recovery */
4528 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
4529         MUTEX_EXIT(&peer->peer_lock);
4530         if (call->flags & RX_CALL_FAST_RECOVER_WAIT) {
4531             /* someone else is waiting to start recovery */
4532             return np;
4533         }
4534         call->flags |= RX_CALL_FAST_RECOVER_WAIT;
4535         rxi_WaitforTQBusy(call);
4536         MUTEX_ENTER(&peer->peer_lock);
4537 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
4538         call->flags &= ~RX_CALL_FAST_RECOVER_WAIT;
4539         call->flags |= RX_CALL_FAST_RECOVER;
4540         call->ssthresh = MAX(4, MIN((int)call->cwind, (int)call->twind)) >> 1;
4541         call->cwind =
4542             MIN((int)(call->ssthresh + rx_nackThreshold), rx_maxSendWindow);
4543         call->nDgramPackets = MAX(2, (int)call->nDgramPackets) >> 1;
4544         call->nextCwind = call->ssthresh;
4545         call->nAcks = 0;
4546         call->nNacks = 0;
4547         peer->MTU = call->MTU;
4548         peer->cwind = call->nextCwind;
4549         peer->nDgramPackets = call->nDgramPackets;
4550         peer->congestSeq++;
4551         call->congestSeq = peer->congestSeq;
4552         /* Reset the resend times on the packets that were nacked
4553          * so we will retransmit as soon as the window permits*/
4554         for (acked = 0, queue_ScanBackwards(&call->tq, tp, nxp, rx_packet)) {
4555             if (acked) {
4556                 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
4557                     clock_Zero(&tp->retryTime);
4558                 }
4559             } else if (tp->flags & RX_PKTFLAG_ACKED) {
4560                 acked = 1;
4561             }
4562         }
4563     } else {
4564         /* If cwind is smaller than ssthresh, then increase
4565          * the window one packet for each ack we receive (exponential
4566          * growth).
4567          * If cwind is greater than or equal to ssthresh then increase
4568          * the congestion window by one packet for each cwind acks we
4569          * receive (linear growth).  */
4570         if (call->cwind < call->ssthresh) {
4571             call->cwind =
4572                 MIN((int)call->ssthresh, (int)(call->cwind + newAckCount));
4573             call->nCwindAcks = 0;
4574         } else {
4575             call->nCwindAcks += newAckCount;
4576             if (call->nCwindAcks >= call->cwind) {
4577                 call->nCwindAcks = 0;
4578                 call->cwind = MIN((int)(call->cwind + 1), rx_maxSendWindow);
4579             }
4580         }
4581         /*
4582          * If we have received several acknowledgements in a row then
4583          * it is time to increase the size of our datagrams
4584          */
4585         if ((int)call->nAcks > rx_nDgramThreshold) {
4586             if (peer->maxDgramPackets > 1) {
4587                 if (call->nDgramPackets < peer->maxDgramPackets) {
4588                     call->nDgramPackets++;
4589                 }
4590                 call->MTU = RX_HEADER_SIZE + RX_JUMBOBUFFERSIZE;
4591             } else if (call->MTU < peer->maxMTU) {
4592                 /* don't upgrade if we can't handle it */
4593                 if ((call->nDgramPackets == 1) && (call->MTU >= peer->ifMTU))
4594                     call->MTU = peer->ifMTU;
4595                 else {
4596                     call->MTU += peer->natMTU;
4597                     call->MTU = MIN(call->MTU, peer->maxMTU);
4598                 }
4599             }
4600             call->nAcks = 0;
4601         }
4602     }
4603
4604     MUTEX_EXIT(&peer->peer_lock);       /* rxi_Start will lock peer. */
4605
4606     /* Servers need to hold the call until all response packets have
4607      * been acknowledged. Soft acks are good enough since clients
4608      * are not allowed to clear their receive queues. */
4609     if (call->state == RX_STATE_HOLD
4610         && call->tfirst + call->nSoftAcked >= call->tnext) {
4611         call->state = RX_STATE_DALLY;
4612         rxi_ClearTransmitQueue(call, 0);
4613         rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
4614     } else if (!queue_IsEmpty(&call->tq)) {
4615         rxi_Start(0, call, 0, istack);
4616     }
4617     return np;
4618 }
4619
4620 /* Received a response to a challenge packet */
4621 struct rx_packet *
4622 rxi_ReceiveResponsePacket(struct rx_connection *conn,
4623                           struct rx_packet *np, int istack)
4624 {
4625     int error;
4626
4627     /* Ignore the packet if we're the client */
4628     if (conn->type == RX_CLIENT_CONNECTION)
4629         return np;
4630
4631     /* If already authenticated, ignore the packet (it's probably a retry) */
4632     if (RXS_CheckAuthentication(conn->securityObject, conn) == 0)
4633         return np;
4634
4635     /* Otherwise, have the security object evaluate the response packet */
4636     error = RXS_CheckResponse(conn->securityObject, conn, np);
4637     if (error) {
4638         /* If the response is invalid, reset the connection, sending
4639          * an abort to the peer */
4640 #ifndef KERNEL
4641         rxi_Delay(1);
4642 #endif
4643         rxi_ConnectionError(conn, error);
4644         MUTEX_ENTER(&conn->conn_data_lock);
4645         np = rxi_SendConnectionAbort(conn, np, istack, 0);
4646         MUTEX_EXIT(&conn->conn_data_lock);
4647         return np;
4648     } else {
4649         /* If the response is valid, any calls waiting to attach
4650          * servers can now do so */
4651         int i;
4652
4653         for (i = 0; i < RX_MAXCALLS; i++) {
4654             struct rx_call *call = conn->call[i];
4655             if (call) {
4656                 MUTEX_ENTER(&call->lock);
4657                 if (call->state == RX_STATE_PRECALL)
4658                     rxi_AttachServerProc(call, (osi_socket) - 1, NULL, NULL);
4659                 /* tnop can be null if newcallp is null */
4660                 MUTEX_EXIT(&call->lock);
4661             }
4662         }
4663
4664         /* Update the peer reachability information, just in case
4665          * some calls went into attach-wait while we were waiting
4666          * for authentication..
4667          */
4668         rxi_UpdatePeerReach(conn, NULL);
4669     }
4670     return np;
4671 }
4672
4673 /* A client has received an authentication challenge: the security
4674  * object is asked to cough up a respectable response packet to send
4675  * back to the server.  The server is responsible for retrying the
4676  * challenge if it fails to get a response. */
4677
4678 struct rx_packet *
4679 rxi_ReceiveChallengePacket(struct rx_connection *conn,
4680                            struct rx_packet *np, int istack)
4681 {
4682     int error;
4683
4684     /* Ignore the challenge if we're the server */
4685     if (conn->type == RX_SERVER_CONNECTION)
4686         return np;
4687
4688     /* Ignore the challenge if the connection is otherwise idle; someone's
4689      * trying to use us as an oracle. */
4690     if (!rxi_HasActiveCalls(conn))
4691         return np;
4692
4693     /* Send the security object the challenge packet.  It is expected to fill
4694      * in the response. */
4695     error = RXS_GetResponse(conn->securityObject, conn, np);
4696
4697     /* If the security object is unable to return a valid response, reset the
4698      * connection and send an abort to the peer.  Otherwise send the response
4699      * packet to the peer connection. */
4700     if (error) {
4701         rxi_ConnectionError(conn, error);
4702         MUTEX_ENTER(&conn->conn_data_lock);
4703         np = rxi_SendConnectionAbort(conn, np, istack, 0);
4704         MUTEX_EXIT(&conn->conn_data_lock);
4705     } else {
4706         np = rxi_SendSpecial((struct rx_call *)0, conn, np,
4707                              RX_PACKET_TYPE_RESPONSE, NULL, -1, istack);
4708     }
4709     return np;
4710 }
4711
4712
4713 /* Find an available server process to service the current request in
4714  * the given call structure.  If one isn't available, queue up this
4715  * call so it eventually gets one */
4716 void
4717 rxi_AttachServerProc(struct rx_call *call,
4718                      osi_socket socket, int *tnop,
4719                      struct rx_call **newcallp)
4720 {
4721     struct rx_serverQueueEntry *sq;
4722     struct rx_service *service = call->conn->service;
4723     int haveQuota = 0;
4724
4725     /* May already be attached */
4726     if (call->state == RX_STATE_ACTIVE)
4727         return;
4728
4729     MUTEX_ENTER(&rx_serverPool_lock);
4730
4731     haveQuota = QuotaOK(service);
4732     if ((!haveQuota) || queue_IsEmpty(&rx_idleServerQueue)) {
4733         /* If there are no processes available to service this call,
4734          * put the call on the incoming call queue (unless it's
4735          * already on the queue).
4736          */
4737 #ifdef RX_ENABLE_LOCKS
4738         if (haveQuota)
4739             ReturnToServerPool(service);
4740 #endif /* RX_ENABLE_LOCKS */
4741
4742         if (!(call->flags & RX_CALL_WAIT_PROC)) {
4743             call->flags |= RX_CALL_WAIT_PROC;
4744             rx_atomic_inc(&rx_nWaiting);
4745             rx_atomic_inc(&rx_nWaited);
4746             rxi_calltrace(RX_CALL_ARRIVAL, call);
4747             SET_CALL_QUEUE_LOCK(call, &rx_serverPool_lock);
4748             queue_Append(&rx_incomingCallQueue, call);
4749         }
4750     } else {
4751         sq = queue_First(&rx_idleServerQueue, rx_serverQueueEntry);
4752
4753         /* If hot threads are enabled, and both newcallp and sq->socketp
4754          * are non-null, then this thread will process the call, and the
4755          * idle server thread will start listening on this threads socket.
4756          */
4757         queue_Remove(sq);
4758         if (rx_enable_hot_thread && newcallp && sq->socketp) {
4759             *newcallp = call;
4760             *tnop = sq->tno;
4761             *sq->socketp = socket;
4762             clock_GetTime(&call->startTime);
4763             MUTEX_ENTER(&rx_refcnt_mutex);
4764             CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
4765             MUTEX_EXIT(&rx_refcnt_mutex);
4766         } else {
4767             sq->newcall = call;
4768         }
4769         if (call->flags & RX_CALL_WAIT_PROC) {
4770             /* Conservative:  I don't think this should happen */
4771             call->flags &= ~RX_CALL_WAIT_PROC;
4772             if (queue_IsOnQueue(call)) {
4773                 queue_Remove(call);
4774
4775                 rx_atomic_dec(&rx_nWaiting);
4776             }
4777         }
4778         call->state = RX_STATE_ACTIVE;
4779         call->mode = RX_MODE_RECEIVING;
4780 #ifdef RX_KERNEL_TRACE
4781         {
4782             int glockOwner = ISAFS_GLOCK();
4783             if (!glockOwner)
4784                 AFS_GLOCK();
4785             afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
4786                        __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
4787                        call);
4788             if (!glockOwner)
4789                 AFS_GUNLOCK();
4790         }
4791 #endif
4792         if (call->flags & RX_CALL_CLEARED) {
4793             /* send an ack now to start the packet flow up again */
4794             call->flags &= ~RX_CALL_CLEARED;
4795             rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
4796         }
4797 #ifdef  RX_ENABLE_LOCKS
4798         CV_SIGNAL(&sq->cv);
4799 #else
4800         service->nRequestsRunning++;
4801         MUTEX_ENTER(&rx_quota_mutex);
4802         if (service->nRequestsRunning <= service->minProcs)
4803             rxi_minDeficit--;
4804         rxi_availProcs--;
4805         MUTEX_EXIT(&rx_quota_mutex);
4806         osi_rxWakeup(sq);
4807 #endif
4808     }
4809     MUTEX_EXIT(&rx_serverPool_lock);
4810 }
4811
4812 /* Delay the sending of an acknowledge event for a short while, while
4813  * a new call is being prepared (in the case of a client) or a reply
4814  * is being prepared (in the case of a server).  Rather than sending
4815  * an ack packet, an ACKALL packet is sent. */
4816 void
4817 rxi_AckAll(struct rxevent *event, struct rx_call *call, char *dummy)
4818 {
4819 #ifdef RX_ENABLE_LOCKS
4820     if (event) {
4821         MUTEX_ENTER(&call->lock);
4822         call->delayedAckEvent = NULL;
4823         MUTEX_ENTER(&rx_refcnt_mutex);
4824         CALL_RELE(call, RX_CALL_REFCOUNT_ACKALL);
4825         MUTEX_EXIT(&rx_refcnt_mutex);
4826     }
4827     rxi_SendSpecial(call, call->conn, (struct rx_packet *)0,
4828                     RX_PACKET_TYPE_ACKALL, NULL, 0, 0);
4829     call->flags |= RX_CALL_ACKALL_SENT;
4830     if (event)
4831         MUTEX_EXIT(&call->lock);
4832 #else /* RX_ENABLE_LOCKS */
4833     if (event)
4834         call->delayedAckEvent = NULL;
4835     rxi_SendSpecial(call, call->conn, (struct rx_packet *)0,
4836                     RX_PACKET_TYPE_ACKALL, NULL, 0, 0);
4837     call->flags |= RX_CALL_ACKALL_SENT;
4838 #endif /* RX_ENABLE_LOCKS */
4839 }
4840
4841 void
4842 rxi_SendDelayedAck(struct rxevent *event, void *arg1, void *unused)
4843 {
4844     struct rx_call *call = arg1;
4845 #ifdef RX_ENABLE_LOCKS
4846     if (event) {
4847         MUTEX_ENTER(&call->lock);
4848         if (event == call->delayedAckEvent)
4849             call->delayedAckEvent = NULL;
4850         MUTEX_ENTER(&rx_refcnt_mutex);
4851         CALL_RELE(call, RX_CALL_REFCOUNT_DELAY);
4852         MUTEX_EXIT(&rx_refcnt_mutex);
4853     }
4854     (void)rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
4855     if (event)
4856         MUTEX_EXIT(&call->lock);
4857 #else /* RX_ENABLE_LOCKS */
4858     if (event)
4859         call->delayedAckEvent = NULL;
4860     (void)rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
4861 #endif /* RX_ENABLE_LOCKS */
4862 }
4863
4864
4865 #ifdef RX_ENABLE_LOCKS
4866 /* Set ack in all packets in transmit queue. rxi_Start will deal with
4867  * clearing them out.
4868  */
4869 static void
4870 rxi_SetAcksInTransmitQueue(struct rx_call *call)
4871 {
4872     struct rx_packet *p, *tp;
4873     int someAcked = 0;
4874
4875     for (queue_Scan(&call->tq, p, tp, rx_packet)) {
4876         p->flags |= RX_PKTFLAG_ACKED;
4877         someAcked = 1;
4878     }
4879     if (someAcked) {
4880         call->flags |= RX_CALL_TQ_CLEARME;
4881         call->flags |= RX_CALL_TQ_SOME_ACKED;
4882     }
4883
4884     rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
4885     call->tfirst = call->tnext;
4886     call->nSoftAcked = 0;
4887
4888     if (call->flags & RX_CALL_FAST_RECOVER) {
4889         call->flags &= ~RX_CALL_FAST_RECOVER;
4890         call->cwind = call->nextCwind;
4891         call->nextCwind = 0;
4892     }
4893
4894     CV_SIGNAL(&call->cv_twind);
4895 }
4896 #endif /* RX_ENABLE_LOCKS */
4897
4898 /* Clear out the transmit queue for the current call (all packets have
4899  * been received by peer) */
4900 void
4901 rxi_ClearTransmitQueue(struct rx_call *call, int force)
4902 {
4903 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
4904     struct rx_packet *p, *tp;
4905
4906     if (!force && (call->flags & RX_CALL_TQ_BUSY)) {
4907         int someAcked = 0;
4908         for (queue_Scan(&call->tq, p, tp, rx_packet)) {
4909             p->flags |= RX_PKTFLAG_ACKED;
4910             someAcked = 1;
4911         }
4912         if (someAcked) {
4913             call->flags |= RX_CALL_TQ_CLEARME;
4914             call->flags |= RX_CALL_TQ_SOME_ACKED;
4915         }
4916     } else {
4917 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
4918 #ifdef RXDEBUG_PACKET
4919         call->tqc -=
4920 #endif /* RXDEBUG_PACKET */
4921             rxi_FreePackets(0, &call->tq);
4922         rxi_WakeUpTransmitQueue(call);
4923 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
4924         call->flags &= ~RX_CALL_TQ_CLEARME;
4925     }
4926 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
4927
4928     rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
4929     call->tfirst = call->tnext; /* implicitly acknowledge all data already sent */
4930     call->nSoftAcked = 0;
4931
4932     if (call->flags & RX_CALL_FAST_RECOVER) {
4933         call->flags &= ~RX_CALL_FAST_RECOVER;
4934         call->cwind = call->nextCwind;
4935     }
4936 #ifdef  RX_ENABLE_LOCKS
4937     CV_SIGNAL(&call->cv_twind);
4938 #else
4939     osi_rxWakeup(&call->twind);
4940 #endif
4941 }
4942
4943 void
4944 rxi_ClearReceiveQueue(struct rx_call *call)
4945 {
4946     if (queue_IsNotEmpty(&call->rq)) {
4947         u_short count;
4948
4949         count = rxi_FreePackets(0, &call->rq);
4950         rx_packetReclaims += count;
4951 #ifdef RXDEBUG_PACKET
4952         call->rqc -= count;
4953         if ( call->rqc != 0 )
4954             dpf(("rxi_ClearReceiveQueue call %"AFS_PTR_FMT" rqc %u != 0\n", call, call->rqc));
4955 #endif
4956         call->flags &= ~(RX_CALL_RECEIVE_DONE | RX_CALL_HAVE_LAST);
4957     }
4958     if (call->state == RX_STATE_PRECALL) {
4959         call->flags |= RX_CALL_CLEARED;
4960     }
4961 }
4962
4963 /* Send an abort packet for the specified call */
4964 struct rx_packet *
4965 rxi_SendCallAbort(struct rx_call *call, struct rx_packet *packet,
4966                   int istack, int force)
4967 {
4968     afs_int32 error;
4969     struct clock when, now;
4970
4971     if (!call->error)
4972         return packet;
4973
4974     /* Clients should never delay abort messages */
4975     if (rx_IsClientConn(call->conn))
4976         force = 1;
4977
4978     if (call->abortCode != call->error) {
4979         call->abortCode = call->error;
4980         call->abortCount = 0;
4981     }
4982
4983     if (force || rxi_callAbortThreshhold == 0
4984         || call->abortCount < rxi_callAbortThreshhold) {
4985         if (call->delayedAbortEvent) {
4986             rxevent_Cancel(call->delayedAbortEvent, call,
4987                            RX_CALL_REFCOUNT_ABORT);
4988         }
4989         error = htonl(call->error);
4990         call->abortCount++;
4991         packet =
4992             rxi_SendSpecial(call, call->conn, packet, RX_PACKET_TYPE_ABORT,
4993                             (char *)&error, sizeof(error), istack);
4994     } else if (!call->delayedAbortEvent) {
4995         clock_GetTime(&now);
4996         when = now;
4997         clock_Addmsec(&when, rxi_callAbortDelay);
4998         MUTEX_ENTER(&rx_refcnt_mutex);
4999         CALL_HOLD(call, RX_CALL_REFCOUNT_ABORT);
5000         MUTEX_EXIT(&rx_refcnt_mutex);
5001         call->delayedAbortEvent =
5002             rxevent_PostNow(&when, &now, rxi_SendDelayedCallAbort, call, 0);
5003     }
5004     return packet;
5005 }
5006
5007 /* Send an abort packet for the specified connection.  Packet is an
5008  * optional pointer to a packet that can be used to send the abort.
5009  * Once the number of abort messages reaches the threshhold, an
5010  * event is scheduled to send the abort. Setting the force flag
5011  * overrides sending delayed abort messages.
5012  *
5013  * NOTE: Called with conn_data_lock held. conn_data_lock is dropped
5014  *       to send the abort packet.
5015  */
5016 struct rx_packet *
5017 rxi_SendConnectionAbort(struct rx_connection *conn,
5018                         struct rx_packet *packet, int istack, int force)
5019 {
5020     afs_int32 error;
5021     struct clock when, now;
5022
5023     if (!conn->error)
5024         return packet;
5025
5026     /* Clients should never delay abort messages */
5027     if (rx_IsClientConn(conn))
5028         force = 1;
5029
5030     if (force || rxi_connAbortThreshhold == 0
5031         || conn->abortCount < rxi_connAbortThreshhold) {
5032         if (conn->delayedAbortEvent) {
5033             rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
5034         }
5035         error = htonl(conn->error);
5036         conn->abortCount++;
5037         MUTEX_EXIT(&conn->conn_data_lock);
5038         packet =
5039             rxi_SendSpecial((struct rx_call *)0, conn, packet,
5040                             RX_PACKET_TYPE_ABORT, (char *)&error,
5041                             sizeof(error), istack);
5042         MUTEX_ENTER(&conn->conn_data_lock);
5043     } else if (!conn->delayedAbortEvent) {
5044         clock_GetTime(&now);
5045         when = now;
5046         clock_Addmsec(&when, rxi_connAbortDelay);
5047         conn->delayedAbortEvent =
5048             rxevent_PostNow(&when, &now, rxi_SendDelayedConnAbort, conn, 0);
5049     }
5050     return packet;
5051 }
5052
5053 /* Associate an error all of the calls owned by a connection.  Called
5054  * with error non-zero.  This is only for really fatal things, like
5055  * bad authentication responses.  The connection itself is set in
5056  * error at this point, so that future packets received will be
5057  * rejected. */
5058 void
5059 rxi_ConnectionError(struct rx_connection *conn,
5060                     afs_int32 error)
5061 {
5062     if (error) {
5063         int i;
5064
5065         dpf(("rxi_ConnectionError conn %"AFS_PTR_FMT" error %d\n", conn, error));
5066
5067         MUTEX_ENTER(&conn->conn_data_lock);
5068         if (conn->challengeEvent)
5069             rxevent_Cancel(conn->challengeEvent, (struct rx_call *)0, 0);
5070         if (conn->natKeepAliveEvent)
5071             rxevent_Cancel(conn->natKeepAliveEvent, (struct rx_call *)0, 0);
5072         if (conn->checkReachEvent) {
5073             rxevent_Cancel(conn->checkReachEvent, (struct rx_call *)0, 0);
5074             conn->checkReachEvent = 0;
5075             conn->flags &= ~RX_CONN_ATTACHWAIT;
5076             MUTEX_ENTER(&rx_refcnt_mutex);
5077             conn->refCount--;
5078             MUTEX_EXIT(&rx_refcnt_mutex);
5079         }
5080         MUTEX_EXIT(&conn->conn_data_lock);
5081         for (i = 0; i < RX_MAXCALLS; i++) {
5082             struct rx_call *call = conn->call[i];
5083             if (call) {
5084                 MUTEX_ENTER(&call->lock);
5085                 rxi_CallError(call, error);
5086                 MUTEX_EXIT(&call->lock);
5087             }
5088         }
5089         conn->error = error;
5090         if (rx_stats_active)
5091             rx_atomic_inc(&rx_stats.fatalErrors);
5092     }
5093 }
5094
5095 /**
5096  * Interrupt an in-progress call with the specified error and wakeup waiters.
5097  *
5098  * @param[in] call  The call to interrupt
5099  * @param[in] error  The error code to send to the peer
5100  */
5101 void
5102 rx_InterruptCall(struct rx_call *call, afs_int32 error)
5103 {
5104     MUTEX_ENTER(&call->lock);
5105     rxi_CallError(call, error);
5106     rxi_SendCallAbort(call, NULL, 0, 1);
5107     MUTEX_EXIT(&call->lock);
5108 }
5109
5110 void
5111 rxi_CallError(struct rx_call *call, afs_int32 error)
5112 {
5113 #ifdef DEBUG
5114     osirx_AssertMine(&call->lock, "rxi_CallError");
5115 #endif
5116     dpf(("rxi_CallError call %"AFS_PTR_FMT" error %d call->error %d\n", call, error, call->error));
5117     if (call->error)
5118         error = call->error;
5119
5120 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
5121     if (!((call->flags & RX_CALL_TQ_BUSY) || (call->tqWaiters > 0))) {
5122         rxi_ResetCall(call, 0);
5123     }
5124 #else
5125     rxi_ResetCall(call, 0);
5126 #endif
5127     call->error = error;
5128 }
5129
5130 /* Reset various fields in a call structure, and wakeup waiting
5131  * processes.  Some fields aren't changed: state & mode are not
5132  * touched (these must be set by the caller), and bufptr, nLeft, and
5133  * nFree are not reset, since these fields are manipulated by
5134  * unprotected macros, and may only be reset by non-interrupting code.
5135  */
5136 #ifdef ADAPT_WINDOW
5137 /* this code requires that call->conn be set properly as a pre-condition. */
5138 #endif /* ADAPT_WINDOW */
5139
5140 void
5141 rxi_ResetCall(struct rx_call *call, int newcall)
5142 {
5143     int flags;
5144     struct rx_peer *peer;
5145     struct rx_packet *packet;
5146 #ifdef DEBUG
5147     osirx_AssertMine(&call->lock, "rxi_ResetCall");
5148 #endif
5149     dpf(("rxi_ResetCall(call %"AFS_PTR_FMT", newcall %d)\n", call, newcall));
5150
5151     /* Notify anyone who is waiting for asynchronous packet arrival */
5152     if (call->arrivalProc) {
5153         (*call->arrivalProc) (call, call->arrivalProcHandle,
5154                               call->arrivalProcArg);
5155         call->arrivalProc = (void (*)())0;
5156     }
5157
5158     if (call->growMTUEvent)
5159         rxevent_Cancel(call->growMTUEvent, call,
5160                        RX_CALL_REFCOUNT_ALIVE);
5161
5162     if (call->delayedAbortEvent) {
5163         rxevent_Cancel(call->delayedAbortEvent, call, RX_CALL_REFCOUNT_ABORT);
5164         packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
5165         if (packet) {
5166             rxi_SendCallAbort(call, packet, 0, 1);
5167             rxi_FreePacket(packet);
5168         }
5169     }
5170
5171     /*
5172      * Update the peer with the congestion information in this call
5173      * so other calls on this connection can pick up where this call
5174      * left off. If the congestion sequence numbers don't match then
5175      * another call experienced a retransmission.
5176      */
5177     peer = call->conn->peer;
5178     MUTEX_ENTER(&peer->peer_lock);
5179     if (!newcall) {
5180         if (call->congestSeq == peer->congestSeq) {
5181             peer->cwind = MAX(peer->cwind, call->cwind);
5182             peer->MTU = MAX(peer->MTU, call->MTU);
5183             peer->nDgramPackets =
5184                 MAX(peer->nDgramPackets, call->nDgramPackets);
5185         }
5186     } else {
5187         call->abortCode = 0;
5188         call->abortCount = 0;
5189     }
5190     if (peer->maxDgramPackets > 1) {
5191         call->MTU = RX_HEADER_SIZE + RX_JUMBOBUFFERSIZE;
5192     } else {
5193         call->MTU = peer->MTU;
5194     }
5195     call->cwind = MIN((int)peer->cwind, (int)peer->nDgramPackets);
5196     call->ssthresh = rx_maxSendWindow;
5197     call->nDgramPackets = peer->nDgramPackets;
5198     call->congestSeq = peer->congestSeq;
5199     MUTEX_EXIT(&peer->peer_lock);
5200
5201     flags = call->flags;
5202 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
5203     rxi_WaitforTQBusy(call);
5204 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
5205
5206     rxi_ClearTransmitQueue(call, 1);
5207     if (call->tqWaiters || (flags & RX_CALL_TQ_WAIT)) {
5208         dpf(("rcall %"AFS_PTR_FMT" has %d waiters and flags %d\n", call, call->tqWaiters, call->flags));
5209     }
5210     call->flags = 0;
5211
5212     if ((flags & RX_CALL_PEER_BUSY)) {
5213         /* The call channel is still busy; resetting the call doesn't change
5214          * that */
5215         call->flags |= RX_CALL_PEER_BUSY;
5216     }
5217
5218     rxi_ClearReceiveQueue(call);
5219     /* why init the queue if you just emptied it? queue_Init(&call->rq); */
5220
5221
5222     call->error = 0;
5223     call->twind = call->conn->twind[call->channel];
5224     call->rwind = call->conn->rwind[call->channel];
5225     call->nSoftAcked = 0;
5226     call->nextCwind = 0;
5227     call->nAcks = 0;
5228     call->nNacks = 0;
5229     call->nCwindAcks = 0;
5230     call->nSoftAcks = 0;
5231     call->nHardAcks = 0;
5232
5233     call->tfirst = call->rnext = call->tnext = 1;
5234     call->tprev = 0;
5235     call->rprev = 0;
5236     call->lastAcked = 0;
5237     call->localStatus = call->remoteStatus = 0;
5238
5239     if (flags & RX_CALL_READER_WAIT) {
5240 #ifdef  RX_ENABLE_LOCKS
5241         CV_BROADCAST(&call->cv_rq);
5242 #else
5243         osi_rxWakeup(&call->rq);
5244 #endif
5245     }
5246     if (flags & RX_CALL_WAIT_PACKETS) {
5247         MUTEX_ENTER(&rx_freePktQ_lock);
5248         rxi_PacketsUnWait();    /* XXX */
5249         MUTEX_EXIT(&rx_freePktQ_lock);
5250     }
5251 #ifdef  RX_ENABLE_LOCKS
5252     CV_SIGNAL(&call->cv_twind);
5253 #else
5254     if (flags & RX_CALL_WAIT_WINDOW_ALLOC)
5255         osi_rxWakeup(&call->twind);
5256 #endif
5257
5258 #ifdef RX_ENABLE_LOCKS
5259     /* The following ensures that we don't mess with any queue while some
5260      * other thread might also be doing so. The call_queue_lock field is
5261      * is only modified under the call lock. If the call is in the process
5262      * of being removed from a queue, the call is not locked until the
5263      * the queue lock is dropped and only then is the call_queue_lock field
5264      * zero'd out. So it's safe to lock the queue if call_queue_lock is set.
5265      * Note that any other routine which removes a call from a queue has to
5266      * obtain the queue lock before examing the queue and removing the call.
5267      */
5268     if (call->call_queue_lock) {
5269         MUTEX_ENTER(call->call_queue_lock);
5270         if (queue_IsOnQueue(call)) {
5271             queue_Remove(call);
5272             if (flags & RX_CALL_WAIT_PROC) {
5273                 rx_atomic_dec(&rx_nWaiting);
5274             }
5275         }
5276         MUTEX_EXIT(call->call_queue_lock);
5277         CLEAR_CALL_QUEUE_LOCK(call);
5278     }
5279 #else /* RX_ENABLE_LOCKS */
5280     if (queue_IsOnQueue(call)) {
5281         queue_Remove(call);
5282         if (flags & RX_CALL_WAIT_PROC)
5283             rx_atomic_dec(&rx_nWaiting);
5284     }
5285 #endif /* RX_ENABLE_LOCKS */
5286
5287     rxi_KeepAliveOff(call);
5288     rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
5289 }
5290
5291 /* Send an acknowledge for the indicated packet (seq,serial) of the
5292  * indicated call, for the indicated reason (reason).  This
5293  * acknowledge will specifically acknowledge receiving the packet, and
5294  * will also specify which other packets for this call have been
5295  * received.  This routine returns the packet that was used to the
5296  * caller.  The caller is responsible for freeing it or re-using it.
5297  * This acknowledgement also returns the highest sequence number
5298  * actually read out by the higher level to the sender; the sender
5299  * promises to keep around packets that have not been read by the
5300  * higher level yet (unless, of course, the sender decides to abort
5301  * the call altogether).  Any of p, seq, serial, pflags, or reason may
5302  * be set to zero without ill effect.  That is, if they are zero, they
5303  * will not convey any information.
5304  * NOW there is a trailer field, after the ack where it will safely be
5305  * ignored by mundanes, which indicates the maximum size packet this
5306  * host can swallow.  */
5307 /*
5308     struct rx_packet *optionalPacket;  use to send ack (or null)
5309     int seq;                     Sequence number of the packet we are acking
5310     int serial;                  Serial number of the packet
5311     int pflags;                  Flags field from packet header
5312     int reason;                  Reason an acknowledge was prompted
5313 */
5314
5315 struct rx_packet *
5316 rxi_SendAck(struct rx_call *call,
5317             struct rx_packet *optionalPacket, int serial, int reason,
5318             int istack)
5319 {
5320     struct rx_ackPacket *ap;
5321     struct rx_packet *rqp;
5322     struct rx_packet *nxp;      /* For queue_Scan */
5323     struct rx_packet *p;
5324     u_char offset;
5325     afs_int32 templ;
5326     afs_uint32 padbytes = 0;
5327 #ifdef RX_ENABLE_TSFPQ
5328     struct rx_ts_info_t * rx_ts_info;
5329 #endif
5330
5331     /*
5332      * Open the receive window once a thread starts reading packets
5333      */
5334     if (call->rnext > 1) {
5335         call->conn->rwind[call->channel] = call->rwind = rx_maxReceiveWindow;
5336     }
5337
5338     /* Don't attempt to grow MTU if this is a critical ping */
5339     if (reason == RX_ACK_MTU) {
5340         /* keep track of per-call attempts, if we're over max, do in small
5341          * otherwise in larger? set a size to increment by, decrease
5342          * on failure, here?
5343          */
5344         if (call->conn->peer->maxPacketSize &&
5345             (call->conn->peer->maxPacketSize < OLD_MAX_PACKET_SIZE
5346              +RX_IPUDP_SIZE))
5347             padbytes = call->conn->peer->maxPacketSize+16;
5348         else
5349             padbytes = call->conn->peer->maxMTU + 128;
5350
5351         /* do always try a minimum size ping */
5352         padbytes = MAX(padbytes, RX_MIN_PACKET_SIZE+RX_IPUDP_SIZE+4);
5353
5354         /* subtract the ack payload */
5355         padbytes -= (rx_AckDataSize(call->rwind) + 4 * sizeof(afs_int32));
5356         reason = RX_ACK_PING;
5357     }
5358
5359     call->nHardAcks = 0;
5360     call->nSoftAcks = 0;
5361     if (call->rnext > call->lastAcked)
5362         call->lastAcked = call->rnext;
5363     p = optionalPacket;
5364
5365     if (p) {
5366         rx_computelen(p, p->length);    /* reset length, you never know */
5367     } /* where that's been...         */
5368 #ifdef RX_ENABLE_TSFPQ
5369     else {
5370         RX_TS_INFO_GET(rx_ts_info);
5371         if ((p = rx_ts_info->local_special_packet)) {
5372             rx_computelen(p, p->length);
5373         } else if ((p = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL))) {
5374             rx_ts_info->local_special_packet = p;
5375         } else { /* We won't send the ack, but don't panic. */
5376             return optionalPacket;
5377         }
5378     }
5379 #else
5380     else if (!(p = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL))) {
5381         /* We won't send the ack, but don't panic. */
5382         return optionalPacket;
5383     }
5384 #endif
5385
5386     templ = padbytes +
5387         rx_AckDataSize(call->rwind) + 4 * sizeof(afs_int32) -
5388         rx_GetDataSize(p);
5389     if (templ > 0) {
5390         if (rxi_AllocDataBuf(p, templ, RX_PACKET_CLASS_SPECIAL) > 0) {
5391 #ifndef RX_ENABLE_TSFPQ
5392             if (!optionalPacket)
5393                 rxi_FreePacket(p);
5394 #endif
5395             return optionalPacket;
5396         }
5397         templ = rx_AckDataSize(call->rwind) + 2 * sizeof(afs_int32);
5398         if (rx_Contiguous(p) < templ) {
5399 #ifndef RX_ENABLE_TSFPQ
5400             if (!optionalPacket)
5401                 rxi_FreePacket(p);
5402 #endif
5403             return optionalPacket;
5404         }
5405     }
5406
5407
5408     /* MTUXXX failing to send an ack is very serious.  We should */
5409     /* try as hard as possible to send even a partial ack; it's */
5410     /* better than nothing. */
5411     ap = (struct rx_ackPacket *)rx_DataOf(p);
5412     ap->bufferSpace = htonl(0); /* Something should go here, sometime */
5413     ap->reason = reason;
5414
5415     /* The skew computation used to be bogus, I think it's better now. */
5416     /* We should start paying attention to skew.    XXX  */
5417     ap->serial = htonl(serial);
5418     ap->maxSkew = 0;            /* used to be peer->inPacketSkew */
5419
5420     /*
5421      * First packet not yet forwarded to reader. When ACKALL has been
5422      * sent the peer has been told that all received packets will be
5423      * delivered to the reader.  The value 'rnext' is used internally
5424      * to refer to the next packet in the receive queue that must be
5425      * delivered to the reader.  From the perspective of the peer it
5426      * already has so report the last sequence number plus one if there
5427      * are packets in the receive queue awaiting processing.
5428      */
5429     if ((call->flags & RX_CALL_ACKALL_SENT) &&
5430         !queue_IsEmpty(&call->rq)) {
5431         ap->firstPacket = htonl(queue_Last(&call->rq, rx_packet)->header.seq + 1);
5432     } else
5433         ap->firstPacket = htonl(call->rnext);
5434
5435     ap->previousPacket = htonl(call->rprev);    /* Previous packet received */
5436
5437     /* No fear of running out of ack packet here because there can only be at most
5438      * one window full of unacknowledged packets.  The window size must be constrained
5439      * to be less than the maximum ack size, of course.  Also, an ack should always
5440      * fit into a single packet -- it should not ever be fragmented.  */
5441     for (offset = 0, queue_Scan(&call->rq, rqp, nxp, rx_packet)) {
5442         if (!rqp || !call->rq.next
5443             || (rqp->header.seq > (call->rnext + call->rwind))) {
5444 #ifndef RX_ENABLE_TSFPQ
5445             if (!optionalPacket)
5446                 rxi_FreePacket(p);
5447 #endif
5448             rxi_CallError(call, RX_CALL_DEAD);
5449             return optionalPacket;
5450         }
5451
5452         while (rqp->header.seq > call->rnext + offset)
5453             ap->acks[offset++] = RX_ACK_TYPE_NACK;
5454         ap->acks[offset++] = RX_ACK_TYPE_ACK;
5455
5456         if ((offset > (u_char) rx_maxReceiveWindow) || (offset > call->rwind)) {
5457 #ifndef RX_ENABLE_TSFPQ
5458             if (!optionalPacket)
5459                 rxi_FreePacket(p);
5460 #endif
5461             rxi_CallError(call, RX_CALL_DEAD);
5462             return optionalPacket;
5463         }
5464     }
5465
5466     ap->nAcks = offset;
5467     p->length = rx_AckDataSize(offset) + 4 * sizeof(afs_int32);
5468
5469     /* these are new for AFS 3.3 */
5470     templ = rxi_AdjustMaxMTU(call->conn->peer->ifMTU, rx_maxReceiveSize);
5471     templ = htonl(templ);
5472     rx_packetwrite(p, rx_AckDataSize(offset), sizeof(afs_int32), &templ);
5473     templ = htonl(call->conn->peer->ifMTU);
5474     rx_packetwrite(p, rx_AckDataSize(offset) + sizeof(afs_int32),
5475                    sizeof(afs_int32), &templ);
5476
5477     /* new for AFS 3.4 */
5478     templ = htonl(call->rwind);
5479     rx_packetwrite(p, rx_AckDataSize(offset) + 2 * sizeof(afs_int32),
5480                    sizeof(afs_int32), &templ);
5481
5482     /* new for AFS 3.5 */
5483     templ = htonl(call->conn->peer->ifDgramPackets);
5484     rx_packetwrite(p, rx_AckDataSize(offset) + 3 * sizeof(afs_int32),
5485                    sizeof(afs_int32), &templ);
5486
5487     p->header.serviceId = call->conn->serviceId;
5488     p->header.cid = (call->conn->cid | call->channel);
5489     p->header.callNumber = *call->callNumber;
5490     p->header.seq = 0;
5491     p->header.securityIndex = call->conn->securityIndex;
5492     p->header.epoch = call->conn->epoch;
5493     p->header.type = RX_PACKET_TYPE_ACK;
5494     p->header.flags = RX_SLOW_START_OK;
5495     if (reason == RX_ACK_PING) {
5496         p->header.flags |= RX_REQUEST_ACK;
5497 #ifdef ADAPT_WINDOW
5498         clock_GetTime(&call->pingRequestTime);
5499 #endif
5500         if (padbytes) {
5501             p->length = padbytes +
5502                 rx_AckDataSize(call->rwind) + 4 * sizeof(afs_int32);
5503
5504             while (padbytes--)
5505                 /* not fast but we can potentially use this if truncated
5506                  * fragments are delivered to figure out the mtu.
5507                  */
5508                 rx_packetwrite(p, rx_AckDataSize(offset) + 4 *
5509                                sizeof(afs_int32), sizeof(afs_int32),
5510                                &padbytes);
5511         }
5512     }
5513     if (call->conn->type == RX_CLIENT_CONNECTION)
5514         p->header.flags |= RX_CLIENT_INITIATED;
5515
5516 #ifdef RXDEBUG
5517 #ifdef AFS_NT40_ENV
5518     if (rxdebug_active) {
5519         char msg[512];
5520         size_t len;
5521
5522         len = _snprintf(msg, sizeof(msg),
5523                         "tid[%d] SACK: reason %s serial %u previous %u seq %u first %u acks %u space %u ",
5524                          GetCurrentThreadId(), rx_ack_reason(ap->reason),
5525                          ntohl(ap->serial), ntohl(ap->previousPacket),
5526                          (unsigned int)p->header.seq, ntohl(ap->firstPacket),
5527                          ap->nAcks, ntohs(ap->bufferSpace) );
5528         if (ap->nAcks) {
5529             int offset;
5530
5531             for (offset = 0; offset < ap->nAcks && len < sizeof(msg); offset++)
5532                 msg[len++] = (ap->acks[offset] == RX_ACK_TYPE_NACK ? '-' : '*');
5533         }
5534         msg[len++]='\n';
5535         msg[len] = '\0';
5536         OutputDebugString(msg);
5537     }
5538 #else /* AFS_NT40_ENV */
5539     if (rx_Log) {
5540         fprintf(rx_Log, "SACK: reason %x previous %u seq %u first %u ",
5541                 ap->reason, ntohl(ap->previousPacket),
5542                 (unsigned int)p->header.seq, ntohl(ap->firstPacket));
5543         if (ap->nAcks) {
5544             for (offset = 0; offset < ap->nAcks; offset++)
5545                 putc(ap->acks[offset] == RX_ACK_TYPE_NACK ? '-' : '*',
5546                      rx_Log);
5547         }
5548         putc('\n', rx_Log);
5549     }
5550 #endif /* AFS_NT40_ENV */
5551 #endif
5552     {
5553         int i, nbytes = p->length;
5554
5555         for (i = 1; i < p->niovecs; i++) {      /* vec 0 is ALWAYS header */
5556             if (nbytes <= p->wirevec[i].iov_len) {
5557                 int savelen, saven;
5558
5559                 savelen = p->wirevec[i].iov_len;
5560                 saven = p->niovecs;
5561                 p->wirevec[i].iov_len = nbytes;
5562                 p->niovecs = i + 1;
5563                 rxi_Send(call, p, istack);
5564                 p->wirevec[i].iov_len = savelen;
5565                 p->niovecs = saven;
5566                 break;
5567             } else
5568                 nbytes -= p->wirevec[i].iov_len;
5569         }
5570     }
5571     if (rx_stats_active)
5572         rx_atomic_inc(&rx_stats.ackPacketsSent);
5573 #ifndef RX_ENABLE_TSFPQ
5574     if (!optionalPacket)
5575         rxi_FreePacket(p);
5576 #endif
5577     return optionalPacket;      /* Return packet for re-use by caller */
5578 }
5579
5580 struct xmitlist {
5581    struct rx_packet **list;
5582    int len;
5583    int resending;
5584 };
5585
5586 /* Send all of the packets in the list in single datagram */
5587 static void
5588 rxi_SendList(struct rx_call *call, struct xmitlist *xmit,
5589              int istack, int moreFlag)
5590 {
5591     int i;
5592     int requestAck = 0;
5593     int lastPacket = 0;
5594     struct clock now, retryTime;
5595     struct rx_connection *conn = call->conn;
5596     struct rx_peer *peer = conn->peer;
5597
5598     MUTEX_ENTER(&peer->peer_lock);
5599     peer->nSent += xmit->len;
5600     if (xmit->resending)
5601         peer->reSends += xmit->len;
5602     retryTime = peer->timeout;
5603     MUTEX_EXIT(&peer->peer_lock);
5604
5605     if (rx_stats_active) {
5606         if (xmit->resending)
5607             rx_atomic_add(&rx_stats.dataPacketsReSent, xmit->len);
5608         else
5609             rx_atomic_add(&rx_stats.dataPacketsSent, xmit->len);
5610     }
5611
5612     clock_GetTime(&now);
5613     clock_Add(&retryTime, &now);
5614
5615     if (xmit->list[xmit->len - 1]->header.flags & RX_LAST_PACKET) {
5616         lastPacket = 1;
5617     }
5618
5619     /* Set the packet flags and schedule the resend events */
5620     /* Only request an ack for the last packet in the list */
5621     for (i = 0; i < xmit->len; i++) {
5622         struct rx_packet *packet = xmit->list[i];
5623
5624         packet->retryTime = retryTime;
5625         if (packet->header.serial) {
5626             /* Exponentially backoff retry times */
5627             if (packet->backoff < MAXBACKOFF) {
5628                 /* so it can't stay == 0 */
5629                 packet->backoff = (packet->backoff << 1) + 1;
5630             } else
5631                 packet->backoff++;
5632             clock_Addmsec(&(packet->retryTime),
5633                           ((afs_uint32) packet->backoff) << 8);
5634         }
5635
5636         /* Wait a little extra for the ack on the last packet */
5637         if (lastPacket 
5638             && !(packet->header.flags & RX_CLIENT_INITIATED)) {
5639             clock_Addmsec(&(packet->retryTime), 400);
5640         }
5641
5642         /* Record the time sent */
5643         packet->timeSent = now;
5644
5645         /* Ask for an ack on retransmitted packets,  on every other packet
5646          * if the peer doesn't support slow start. Ask for an ack on every
5647          * packet until the congestion window reaches the ack rate. */
5648         if (packet->header.serial) {
5649             requestAck = 1;
5650         } else {
5651             /* improved RTO calculation- not Karn */
5652             packet->firstSent = now;
5653             if (!lastPacket && (call->cwind <= (u_short) (conn->ackRate + 1)
5654                                 || (!(call->flags & RX_CALL_SLOW_START_OK)
5655                                     && (packet->header.seq & 1)))) {
5656                 requestAck = 1;
5657             }
5658         }
5659
5660         /* Tag this packet as not being the last in this group,
5661          * for the receiver's benefit */
5662         if (i < xmit->len - 1 || moreFlag) {
5663             packet->header.flags |= RX_MORE_PACKETS;
5664         }
5665     }
5666
5667     if (requestAck) {
5668         xmit->list[xmit->len - 1]->header.flags |= RX_REQUEST_ACK;
5669     }
5670
5671     /* Since we're about to send a data packet to the peer, it's
5672      * safe to nuke any scheduled end-of-packets ack */
5673     rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
5674
5675     MUTEX_EXIT(&call->lock);
5676     MUTEX_ENTER(&rx_refcnt_mutex);
5677     CALL_HOLD(call, RX_CALL_REFCOUNT_SEND);
5678     MUTEX_EXIT(&rx_refcnt_mutex);
5679     if (xmit->len > 1) {
5680         rxi_SendPacketList(call, conn, xmit->list, xmit->len, istack);
5681     } else {
5682         rxi_SendPacket(call, conn, xmit->list[0], istack);
5683     }
5684     MUTEX_ENTER(&call->lock);
5685     MUTEX_ENTER(&rx_refcnt_mutex);
5686     CALL_RELE(call, RX_CALL_REFCOUNT_SEND);
5687     MUTEX_EXIT(&rx_refcnt_mutex);
5688
5689     /* Update last send time for this call (for keep-alive
5690      * processing), and for the connection (so that we can discover
5691      * idle connections) */
5692     conn->lastSendTime = call->lastSendTime = clock_Sec();
5693     /* Let a set of retransmits trigger an idle timeout */
5694     if (!xmit->resending)
5695         call->lastSendData = call->lastSendTime;
5696 }
5697
5698 /* When sending packets we need to follow these rules:
5699  * 1. Never send more than maxDgramPackets in a jumbogram.
5700  * 2. Never send a packet with more than two iovecs in a jumbogram.
5701  * 3. Never send a retransmitted packet in a jumbogram.
5702  * 4. Never send more than cwind/4 packets in a jumbogram
5703  * We always keep the last list we should have sent so we
5704  * can set the RX_MORE_PACKETS flags correctly.
5705  */
5706
5707 static void
5708 rxi_SendXmitList(struct rx_call *call, struct rx_packet **list, int len,
5709                  int istack)
5710 {
5711     int i;
5712     struct xmitlist working;
5713     struct xmitlist last;
5714
5715     struct rx_peer *peer = call->conn->peer;
5716     int morePackets = 0;
5717
5718     memset(&last, 0, sizeof(struct xmitlist));
5719     working.list = &list[0];
5720     working.len = 0;
5721     working.resending = 0;
5722
5723     for (i = 0; i < len; i++) {
5724         /* Does the current packet force us to flush the current list? */
5725         if (working.len > 0
5726             && (list[i]->header.serial || (list[i]->flags & RX_PKTFLAG_ACKED)
5727                 || list[i]->length > RX_JUMBOBUFFERSIZE)) {
5728
5729             /* This sends the 'last' list and then rolls the current working
5730              * set into the 'last' one, and resets the working set */
5731
5732             if (last.len > 0) {
5733                 rxi_SendList(call, &last, istack, 1);
5734                 /* If the call enters an error state stop sending, or if
5735                  * we entered congestion recovery mode, stop sending */
5736                 if (call->error || (call->flags & RX_CALL_FAST_RECOVER_WAIT))
5737                     return;
5738             }
5739             last = working;
5740             working.len = 0;
5741             working.resending = 0;
5742             working.list = &list[i];
5743         }
5744         /* Add the current packet to the list if it hasn't been acked.
5745          * Otherwise adjust the list pointer to skip the current packet.  */
5746         if (!(list[i]->flags & RX_PKTFLAG_ACKED)) {
5747             working.len++;
5748
5749             if (list[i]->header.serial)
5750                 working.resending = 1;
5751
5752             /* Do we need to flush the list? */
5753             if (working.len >= (int)peer->maxDgramPackets
5754                 || working.len >= (int)call->nDgramPackets 
5755                 || working.len >= (int)call->cwind
5756                 || list[i]->header.serial
5757                 || list[i]->length != RX_JUMBOBUFFERSIZE) {
5758                 if (last.len > 0) {
5759                     rxi_SendList(call, &last, istack, 1);
5760                     /* If the call enters an error state stop sending, or if
5761                      * we entered congestion recovery mode, stop sending */
5762                     if (call->error
5763                         || (call->flags & RX_CALL_FAST_RECOVER_WAIT))
5764                         return;
5765                 }
5766                 last = working;
5767                 working.len = 0;
5768                 working.resending = 0;
5769                 working.list = &list[i + 1];
5770             }
5771         } else {
5772             if (working.len != 0) {
5773                 osi_Panic("rxi_SendList error");
5774             }
5775             working.list = &list[i + 1];
5776         }
5777     }
5778
5779     /* Send the whole list when the call is in receive mode, when
5780      * the call is in eof mode, when we are in fast recovery mode,
5781      * and when we have the last packet */
5782     if ((list[len - 1]->header.flags & RX_LAST_PACKET)
5783         || call->mode == RX_MODE_RECEIVING || call->mode == RX_MODE_EOF
5784         || (call->flags & RX_CALL_FAST_RECOVER)) {
5785         /* Check for the case where the current list contains
5786          * an acked packet. Since we always send retransmissions
5787          * in a separate packet, we only need to check the first
5788          * packet in the list */
5789         if (working.len > 0 && !(working.list[0]->flags & RX_PKTFLAG_ACKED)) {
5790             morePackets = 1;
5791         }
5792         if (last.len > 0) {
5793             rxi_SendList(call, &last, istack, morePackets);
5794             /* If the call enters an error state stop sending, or if
5795              * we entered congestion recovery mode, stop sending */
5796             if (call->error || (call->flags & RX_CALL_FAST_RECOVER_WAIT))
5797                 return;
5798         }
5799         if (morePackets) {
5800             rxi_SendList(call, &working, istack, 0);
5801         }
5802     } else if (last.len > 0) {
5803         rxi_SendList(call, &last, istack, 0);
5804         /* Packets which are in 'working' are not sent by this call */
5805     }
5806 }
5807
5808 #ifdef  RX_ENABLE_LOCKS
5809 /* Call rxi_Start, below, but with the call lock held. */
5810 void
5811 rxi_StartUnlocked(struct rxevent *event,
5812                   void *arg0, void *arg1, int istack)
5813 {
5814     struct rx_call *call = arg0;
5815
5816     MUTEX_ENTER(&call->lock);
5817     rxi_Start(event, call, arg1, istack);
5818     MUTEX_EXIT(&call->lock);
5819 }
5820 #endif /* RX_ENABLE_LOCKS */
5821
5822 /* This routine is called when new packets are readied for
5823  * transmission and when retransmission may be necessary, or when the
5824  * transmission window or burst count are favourable.  This should be
5825  * better optimized for new packets, the usual case, now that we've
5826  * got rid of queues of send packets. XXXXXXXXXXX */
5827 void
5828 rxi_Start(struct rxevent *event,
5829           void *arg0, void *arg1, int istack)
5830 {
5831     struct rx_call *call = arg0;
5832
5833     struct rx_packet *p;
5834     struct rx_packet *nxp;      /* Next pointer for queue_Scan */
5835     struct clock now, usenow, retryTime;
5836     int haveEvent;
5837     int nXmitPackets;
5838     int maxXmitPackets;
5839
5840     /* If rxi_Start is being called as a result of a resend event,
5841      * then make sure that the event pointer is removed from the call
5842      * structure, since there is no longer a per-call retransmission
5843      * event pending. */
5844     if (event && event == call->resendEvent) {
5845         MUTEX_ENTER(&rx_refcnt_mutex);
5846         CALL_RELE(call, RX_CALL_REFCOUNT_RESEND);
5847         MUTEX_EXIT(&rx_refcnt_mutex);
5848         call->resendEvent = NULL;
5849
5850         if (rxi_busyChannelError && (call->flags & RX_CALL_PEER_BUSY)) {
5851             rxi_CheckBusy(call);
5852         }
5853
5854         if (queue_IsEmpty(&call->tq)) {
5855             /* Nothing to do */
5856             return;
5857         }
5858     }
5859
5860     if (call->error) {
5861 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
5862         if (rx_stats_active)
5863             rx_atomic_inc(&rx_tq_debug.rxi_start_in_error);
5864 #endif
5865         return;
5866     }
5867
5868     if (queue_IsNotEmpty(&call->tq)) {  /* If we have anything to send */
5869
5870         clock_GetTime(&now);
5871         usenow = now;
5872
5873         /* Send (or resend) any packets that need it, subject to
5874          * window restrictions and congestion burst control
5875          * restrictions.  Ask for an ack on the last packet sent in
5876          * this burst.  For now, we're relying upon the window being
5877          * considerably bigger than the largest number of packets that
5878          * are typically sent at once by one initial call to
5879          * rxi_Start.  This is probably bogus (perhaps we should ask
5880          * for an ack when we're half way through the current
5881          * window?).  Also, for non file transfer applications, this
5882          * may end up asking for an ack for every packet.  Bogus. XXXX
5883          */
5884         /*
5885          * But check whether we're here recursively, and let the other guy
5886          * do the work.
5887          */
5888 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
5889         if (!(call->flags & RX_CALL_TQ_BUSY)) {
5890             call->flags |= RX_CALL_TQ_BUSY;
5891             do {
5892 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
5893             restart:
5894 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
5895                 call->flags &= ~RX_CALL_NEED_START;
5896 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
5897                 nXmitPackets = 0;
5898                 maxXmitPackets = MIN(call->twind, call->cwind);
5899                 for (queue_Scan(&call->tq, p, nxp, rx_packet)) {
5900                     if (call->flags & RX_CALL_FAST_RECOVER_WAIT) {
5901                         /* We shouldn't be sending packets if a thread is waiting
5902                          * to initiate congestion recovery */
5903                         dpf(("call %d waiting to initiate fast recovery\n",
5904                              *(call->callNumber)));
5905                         break;
5906                     }
5907                     if ((nXmitPackets)
5908                         && (call->flags & RX_CALL_FAST_RECOVER)) {
5909                         /* Only send one packet during fast recovery */
5910                         dpf(("call %d restricted to one packet per send during fast recovery\n",
5911                              *(call->callNumber)));
5912                         break;
5913                     }
5914 #ifdef RX_TRACK_PACKETS
5915                     if ((p->flags & RX_PKTFLAG_FREE)
5916                         || (!queue_IsEnd(&call->tq, nxp)
5917                             && (nxp->flags & RX_PKTFLAG_FREE))
5918                         || (p == (struct rx_packet *)&rx_freePacketQueue)
5919                         || (nxp == (struct rx_packet *)&rx_freePacketQueue)) {
5920                         osi_Panic("rxi_Start: xmit queue clobbered");
5921                     }
5922 #endif
5923                     if (p->flags & RX_PKTFLAG_ACKED) {
5924                         /* Since we may block, don't trust this */
5925                         usenow.sec = usenow.usec = 0;
5926                         if (rx_stats_active)
5927                             rx_atomic_inc(&rx_stats.ignoreAckedPacket);
5928                         continue;       /* Ignore this packet if it has been acknowledged */
5929                     }
5930
5931                     /* Turn off all flags except these ones, which are the same
5932                      * on each transmission */
5933                     p->header.flags &= RX_PRESET_FLAGS;
5934
5935                     if (p->header.seq >=
5936                         call->tfirst + MIN((int)call->twind,
5937                                            (int)(call->nSoftAcked +
5938                                                  call->cwind))) {
5939                         call->flags |= RX_CALL_WAIT_WINDOW_SEND;        /* Wait for transmit window */
5940                         /* Note: if we're waiting for more window space, we can
5941                          * still send retransmits; hence we don't return here, but
5942                          * break out to schedule a retransmit event */
5943                         dpf(("call %d waiting for window (seq %d, twind %d, nSoftAcked %d, cwind %d)\n",
5944                              *(call->callNumber), p->header.seq, call->twind, call->nSoftAcked,
5945                              call->cwind));
5946                         break;
5947                     }
5948
5949                     /* Transmit the packet if it needs to be sent. */
5950                     if (!clock_Lt(&now, &p->retryTime)) {
5951                         if (nXmitPackets == maxXmitPackets) {
5952                             rxi_SendXmitList(call, call->xmitList,
5953                                              nXmitPackets, istack);
5954                             goto restart;
5955                         }
5956                         dpf(("call %d xmit packet %"AFS_PTR_FMT" now %u.%06u retryTime %u.%06u\n",
5957                               *(call->callNumber), p,
5958                               now.sec, now.usec,
5959                               p->retryTime.sec, p->retryTime.usec));
5960                         call->xmitList[nXmitPackets++] = p;
5961                     }
5962                 }
5963
5964                 /* xmitList now hold pointers to all of the packets that are
5965                  * ready to send. Now we loop to send the packets */
5966                 if (nXmitPackets > 0) {
5967                     rxi_SendXmitList(call, call->xmitList, nXmitPackets,
5968                                      istack);
5969                 }
5970
5971 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
5972                 /*
5973                  * TQ references no longer protected by this flag; they must remain
5974                  * protected by the global lock.
5975                  */
5976                 if (call->flags & RX_CALL_FAST_RECOVER_WAIT) {
5977                     call->flags &= ~RX_CALL_TQ_BUSY;
5978                     rxi_WakeUpTransmitQueue(call);
5979                     return;
5980                 }
5981                 if (call->error) {
5982                     /* We went into the error state while sending packets. Now is
5983                      * the time to reset the call. This will also inform the using
5984                      * process that the call is in an error state.
5985                      */
5986                     if (rx_stats_active)
5987                         rx_atomic_inc(&rx_tq_debug.rxi_start_aborted);
5988                     call->flags &= ~RX_CALL_TQ_BUSY;
5989                     rxi_WakeUpTransmitQueue(call);
5990                     rxi_CallError(call, call->error);
5991                     return;
5992                 }
5993 #ifdef RX_ENABLE_LOCKS
5994                 if (call->flags & RX_CALL_TQ_SOME_ACKED) {
5995                     int missing;
5996                     call->flags &= ~RX_CALL_TQ_SOME_ACKED;
5997                     /* Some packets have received acks. If they all have, we can clear
5998                      * the transmit queue.
5999                      */
6000                     for (missing =
6001                          0, queue_Scan(&call->tq, p, nxp, rx_packet)) {
6002                         if (p->header.seq < call->tfirst
6003                             && (p->flags & RX_PKTFLAG_ACKED)) {
6004                             queue_Remove(p);
6005 #ifdef RX_TRACK_PACKETS
6006                             p->flags &= ~RX_PKTFLAG_TQ;
6007 #endif
6008 #ifdef RXDEBUG_PACKET
6009                             call->tqc--;
6010 #endif
6011                             rxi_FreePacket(p);
6012                         } else
6013                             missing = 1;
6014                     }
6015                     if (!missing)
6016                         call->flags |= RX_CALL_TQ_CLEARME;
6017                 }
6018 #endif /* RX_ENABLE_LOCKS */
6019                 /* Don't bother doing retransmits if the TQ is cleared. */
6020                 if (call->flags & RX_CALL_TQ_CLEARME) {
6021                     rxi_ClearTransmitQueue(call, 1);
6022                 } else
6023 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
6024                 {
6025
6026                     /* Always post a resend event, if there is anything in the
6027                      * queue, and resend is possible.  There should be at least
6028                      * one unacknowledged packet in the queue ... otherwise none
6029                      * of these packets should be on the queue in the first place.
6030                      */
6031                     if (call->resendEvent) {
6032                         /* Cancel the existing event and post a new one */
6033                         rxevent_Cancel(call->resendEvent, call,
6034                                        RX_CALL_REFCOUNT_RESEND);
6035                     }
6036
6037                     /* The retry time is the retry time on the first unacknowledged
6038                      * packet inside the current window */
6039                     for (haveEvent =
6040                          0, queue_Scan(&call->tq, p, nxp, rx_packet)) {
6041                         /* Don't set timers for packets outside the window */
6042                         if (p->header.seq >= call->tfirst + call->twind) {
6043                             break;
6044                         }
6045
6046                         if (!(p->flags & RX_PKTFLAG_ACKED)
6047                             && !clock_IsZero(&p->retryTime)) {
6048                             haveEvent = 1;
6049                             retryTime = p->retryTime;
6050                             break;
6051                         }
6052                     }
6053
6054                     /* Post a new event to re-run rxi_Start when retries may be needed */
6055                     if (haveEvent && !(call->flags & RX_CALL_NEED_START)) {
6056 #ifdef RX_ENABLE_LOCKS
6057                         MUTEX_ENTER(&rx_refcnt_mutex);
6058                         CALL_HOLD(call, RX_CALL_REFCOUNT_RESEND);
6059                         MUTEX_EXIT(&rx_refcnt_mutex);
6060                         call->resendEvent =
6061                             rxevent_PostNow2(&retryTime, &usenow,
6062                                              rxi_StartUnlocked,
6063                                              (void *)call, 0, istack);
6064 #else /* RX_ENABLE_LOCKS */
6065                         call->resendEvent =
6066                             rxevent_PostNow2(&retryTime, &usenow, rxi_Start,
6067                                              (void *)call, 0, istack);
6068 #endif /* RX_ENABLE_LOCKS */
6069                     }
6070                 }
6071 #ifdef  AFS_GLOBAL_RXLOCK_KERNEL
6072             } while (call->flags & RX_CALL_NEED_START);
6073             /*
6074              * TQ references no longer protected by this flag; they must remain
6075              * protected by the global lock.
6076              */
6077             call->flags &= ~RX_CALL_TQ_BUSY;
6078             rxi_WakeUpTransmitQueue(call);
6079         } else {
6080             call->flags |= RX_CALL_NEED_START;
6081         }
6082 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
6083     } else {
6084         if (call->resendEvent) {
6085             rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
6086         }
6087     }
6088 }
6089
6090 /* Also adjusts the keep alive parameters for the call, to reflect
6091  * that we have just sent a packet (so keep alives aren't sent
6092  * immediately) */
6093 void
6094 rxi_Send(struct rx_call *call, struct rx_packet *p,
6095          int istack)
6096 {
6097     struct rx_connection *conn = call->conn;
6098
6099     /* Stamp each packet with the user supplied status */
6100     p->header.userStatus = call->localStatus;
6101
6102     /* Allow the security object controlling this call's security to
6103      * make any last-minute changes to the packet */
6104     RXS_SendPacket(conn->securityObject, call, p);
6105
6106     /* Since we're about to send SOME sort of packet to the peer, it's
6107      * safe to nuke any scheduled end-of-packets ack */
6108     rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
6109
6110     /* Actually send the packet, filling in more connection-specific fields */
6111     MUTEX_EXIT(&call->lock);
6112     MUTEX_ENTER(&rx_refcnt_mutex);
6113     CALL_HOLD(call, RX_CALL_REFCOUNT_SEND);
6114     MUTEX_EXIT(&rx_refcnt_mutex);
6115     rxi_SendPacket(call, conn, p, istack);
6116     MUTEX_ENTER(&rx_refcnt_mutex);
6117     CALL_RELE(call, RX_CALL_REFCOUNT_SEND);
6118     MUTEX_EXIT(&rx_refcnt_mutex);
6119     MUTEX_ENTER(&call->lock);
6120
6121     /* Update last send time for this call (for keep-alive
6122      * processing), and for the connection (so that we can discover
6123      * idle connections) */
6124     if ((p->header.type != RX_PACKET_TYPE_ACK) ||
6125         (((struct rx_ackPacket *)rx_DataOf(p))->reason == RX_ACK_PING) ||
6126         (p->length <= (rx_AckDataSize(call->rwind) + 4 * sizeof(afs_int32))))
6127     {
6128         conn->lastSendTime = call->lastSendTime = clock_Sec();
6129         /* Don't count keepalive ping/acks here, so idleness can be tracked. */
6130         if ((p->header.type != RX_PACKET_TYPE_ACK) ||
6131             ((((struct rx_ackPacket *)rx_DataOf(p))->reason != RX_ACK_PING) &&
6132              (((struct rx_ackPacket *)rx_DataOf(p))->reason !=
6133               RX_ACK_PING_RESPONSE)))
6134             call->lastSendData = call->lastSendTime;
6135     }
6136 }
6137
6138 /* Check if a call needs to be destroyed.  Called by keep-alive code to ensure
6139  * that things are fine.  Also called periodically to guarantee that nothing
6140  * falls through the cracks (e.g. (error + dally) connections have keepalive
6141  * turned off.  Returns 0 if conn is well, -1 otherwise.  If otherwise, call
6142  *  may be freed!
6143  * haveCTLock Set if calling from rxi_ReapConnections
6144  */
6145 #ifdef RX_ENABLE_LOCKS
6146 int
6147 rxi_CheckCall(struct rx_call *call, int haveCTLock)
6148 #else /* RX_ENABLE_LOCKS */
6149 int
6150 rxi_CheckCall(struct rx_call *call)
6151 #endif                          /* RX_ENABLE_LOCKS */
6152 {
6153     struct rx_connection *conn = call->conn;
6154     afs_uint32 now;
6155     afs_uint32 deadTime, idleDeadTime = 0, hardDeadTime = 0;
6156     afs_uint32 fudgeFactor;
6157     int cerror = 0;
6158     int newmtu = 0;
6159
6160 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
6161     if (call->flags & RX_CALL_TQ_BUSY) {
6162         /* Call is active and will be reset by rxi_Start if it's
6163          * in an error state.
6164          */
6165         return 0;
6166     }
6167 #endif
6168     /* RTT + 8*MDEV, rounded up to the next second. */
6169     fudgeFactor = (((afs_uint32) conn->peer->rtt >> 3) +
6170                    ((afs_uint32) conn->peer->rtt_dev << 1) + 1023) >> 10;
6171
6172     deadTime = conn->secondsUntilDead + fudgeFactor;
6173     now = clock_Sec();
6174     /* These are computed to the second (+- 1 second).  But that's
6175      * good enough for these values, which should be a significant
6176      * number of seconds. */
6177     if (now > (call->lastReceiveTime + deadTime)) {
6178         if (call->state == RX_STATE_ACTIVE) {
6179 #ifdef ADAPT_PMTU
6180 #if defined(KERNEL) && defined(AFS_SUN57_ENV)
6181             ire_t *ire;
6182 #if defined(AFS_SUN510_ENV) && defined(GLOBAL_NETSTACKID)
6183             netstack_t *ns =  netstack_find_by_stackid(GLOBAL_NETSTACKID);
6184             ip_stack_t *ipst = ns->netstack_ip;
6185 #endif
6186             ire = ire_cache_lookup(conn->peer->host
6187 #if defined(AFS_SUN510_ENV) && defined(ALL_ZONES)
6188                                    , ALL_ZONES
6189 #if defined(AFS_SUN510_ENV) && (defined(ICL_3_ARG) || defined(GLOBAL_NETSTACKID))
6190                                    , NULL
6191 #if defined(AFS_SUN510_ENV) && defined(GLOBAL_NETSTACKID)
6192                                    , ipst
6193 #endif
6194 #endif
6195 #endif
6196                 );
6197
6198             if (ire && ire->ire_max_frag > 0)
6199                 rxi_SetPeerMtu(NULL, conn->peer->host, 0,
6200                                ire->ire_max_frag);
6201 #if defined(GLOBAL_NETSTACKID)
6202             netstack_rele(ns);
6203 #endif
6204 #endif
6205 #endif /* ADAPT_PMTU */
6206             cerror = RX_CALL_DEAD;
6207             goto mtuout;
6208         } else {
6209 #ifdef RX_ENABLE_LOCKS
6210             /* Cancel pending events */
6211             rxevent_Cancel(call->delayedAckEvent, call,
6212                            RX_CALL_REFCOUNT_DELAY);
6213             rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
6214             rxevent_Cancel(call->keepAliveEvent, call,
6215                            RX_CALL_REFCOUNT_ALIVE);
6216             if (call->growMTUEvent)
6217                 rxevent_Cancel(call->growMTUEvent, call,
6218                                RX_CALL_REFCOUNT_ALIVE);
6219             MUTEX_ENTER(&rx_refcnt_mutex);
6220             if (call->refCount == 0) {
6221                 rxi_FreeCall(call, haveCTLock);
6222                 MUTEX_EXIT(&rx_refcnt_mutex);
6223                 return -2;
6224             }
6225             MUTEX_EXIT(&rx_refcnt_mutex);
6226             return -1;
6227 #else /* RX_ENABLE_LOCKS */
6228             rxi_FreeCall(call, 0);
6229             return -2;
6230 #endif /* RX_ENABLE_LOCKS */
6231         }
6232         /* Non-active calls are destroyed if they are not responding
6233          * to pings; active calls are simply flagged in error, so the
6234          * attached process can die reasonably gracefully. */
6235     }
6236
6237     if (conn->idleDeadTime) {
6238         idleDeadTime = conn->idleDeadTime + fudgeFactor;
6239     }
6240
6241     /* see if we have a non-activity timeout */
6242     if (call->startWait && idleDeadTime
6243         && ((call->startWait + idleDeadTime) < now) &&
6244         (call->flags & RX_CALL_READER_WAIT)) {
6245         if (call->state == RX_STATE_ACTIVE) {
6246             cerror = RX_CALL_TIMEOUT;
6247             goto mtuout;
6248         }
6249     }
6250     if (call->lastSendData && idleDeadTime && (conn->idleDeadErr != 0)
6251         && ((call->lastSendData + idleDeadTime) < now)) {
6252         if (call->state == RX_STATE_ACTIVE) {
6253             cerror = conn->idleDeadErr;
6254             goto mtuout;
6255         }
6256     }
6257
6258     if (conn->hardDeadTime) {
6259         hardDeadTime = conn->hardDeadTime + fudgeFactor;
6260     }
6261
6262     /* see if we have a hard timeout */
6263     if (hardDeadTime
6264         && (now > (hardDeadTime + call->startTime.sec))) {
6265         if (call->state == RX_STATE_ACTIVE)
6266             rxi_CallError(call, RX_CALL_TIMEOUT);
6267         return -1;
6268     }
6269     return 0;
6270 mtuout:
6271     if (conn->msgsizeRetryErr && cerror != RX_CALL_TIMEOUT
6272         && call->lastReceiveTime) {
6273         int oldMTU = conn->peer->ifMTU;
6274
6275         /* if we thought we could send more, perhaps things got worse */
6276         if (conn->peer->maxPacketSize > conn->lastPacketSize)
6277             /* maxpacketsize will be cleared in rxi_SetPeerMtu */
6278             newmtu = MAX(conn->peer->maxPacketSize-RX_IPUDP_SIZE,
6279                          conn->lastPacketSize-(128+RX_IPUDP_SIZE));
6280         else
6281             newmtu = conn->lastPacketSize-(128+RX_IPUDP_SIZE);
6282
6283         /* minimum capped in SetPeerMtu */
6284         rxi_SetPeerMtu(conn->peer, 0, 0, newmtu);
6285
6286         /* clean up */
6287         conn->lastPacketSize = 0;
6288
6289         /* needed so ResetCall doesn't clobber us. */
6290         call->MTU = conn->peer->ifMTU;
6291
6292         /* if we never succeeded, let the error pass out as-is */
6293         if (conn->peer->maxPacketSize && oldMTU != conn->peer->ifMTU)
6294             cerror = conn->msgsizeRetryErr;
6295
6296     }
6297     rxi_CallError(call, cerror);
6298     return -1;
6299 }
6300
6301 void
6302 rxi_NatKeepAliveEvent(struct rxevent *event, void *arg1, void *dummy)
6303 {
6304     struct rx_connection *conn = arg1;
6305     struct rx_header theader;
6306     char tbuffer[1 + sizeof(struct rx_header)];
6307     struct sockaddr_in taddr;
6308     char *tp;
6309     char a[1] = { 0 };
6310     struct iovec tmpiov[2];
6311     osi_socket socket =
6312         (conn->type ==
6313          RX_CLIENT_CONNECTION ? rx_socket : conn->service->socket);
6314
6315
6316     tp = &tbuffer[sizeof(struct rx_header)];
6317     taddr.sin_family = AF_INET;
6318     taddr.sin_port = rx_PortOf(rx_PeerOf(conn));
6319     taddr.sin_addr.s_addr = rx_HostOf(rx_PeerOf(conn));
6320 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
6321     taddr.sin_len = sizeof(struct sockaddr_in);
6322 #endif
6323     memset(&theader, 0, sizeof(theader));
6324     theader.epoch = htonl(999);
6325     theader.cid = 0;
6326     theader.callNumber = 0;
6327     theader.seq = 0;
6328     theader.serial = 0;
6329     theader.type = RX_PACKET_TYPE_VERSION;
6330     theader.flags = RX_LAST_PACKET;
6331     theader.serviceId = 0;
6332
6333     memcpy(tbuffer, &theader, sizeof(theader));
6334     memcpy(tp, &a, sizeof(a));
6335     tmpiov[0].iov_base = tbuffer;
6336     tmpiov[0].iov_len = 1 + sizeof(struct rx_header);
6337
6338     osi_NetSend(socket, &taddr, tmpiov, 1, 1 + sizeof(struct rx_header), 1);
6339
6340     MUTEX_ENTER(&conn->conn_data_lock);
6341     MUTEX_ENTER(&rx_refcnt_mutex);
6342     /* Only reschedule ourselves if the connection would not be destroyed */
6343     if (conn->refCount <= 1) {
6344         conn->natKeepAliveEvent = NULL;
6345         MUTEX_EXIT(&rx_refcnt_mutex);
6346         MUTEX_EXIT(&conn->conn_data_lock);
6347         rx_DestroyConnection(conn); /* drop the reference for this */
6348     } else {
6349         conn->refCount--; /* drop the reference for this */
6350         MUTEX_EXIT(&rx_refcnt_mutex);
6351         conn->natKeepAliveEvent = NULL;
6352         rxi_ScheduleNatKeepAliveEvent(conn);
6353         MUTEX_EXIT(&conn->conn_data_lock);
6354     }
6355 }
6356
6357 void
6358 rxi_ScheduleNatKeepAliveEvent(struct rx_connection *conn)
6359 {
6360     if (!conn->natKeepAliveEvent && conn->secondsUntilNatPing) {
6361         struct clock when, now;
6362         clock_GetTime(&now);
6363         when = now;
6364         when.sec += conn->secondsUntilNatPing;
6365         MUTEX_ENTER(&rx_refcnt_mutex);
6366         conn->refCount++; /* hold a reference for this */
6367         MUTEX_EXIT(&rx_refcnt_mutex);
6368         conn->natKeepAliveEvent =
6369             rxevent_PostNow(&when, &now, rxi_NatKeepAliveEvent, conn, 0);
6370     }
6371 }
6372
6373 void
6374 rx_SetConnSecondsUntilNatPing(struct rx_connection *conn, afs_int32 seconds)
6375 {
6376     MUTEX_ENTER(&conn->conn_data_lock);
6377     conn->secondsUntilNatPing = seconds;
6378     if (seconds != 0)
6379         rxi_ScheduleNatKeepAliveEvent(conn);
6380     MUTEX_EXIT(&conn->conn_data_lock);
6381 }
6382
6383 void
6384 rxi_NatKeepAliveOn(struct rx_connection *conn)
6385 {
6386     MUTEX_ENTER(&conn->conn_data_lock);
6387     rxi_ScheduleNatKeepAliveEvent(conn);
6388     MUTEX_EXIT(&conn->conn_data_lock);
6389 }
6390
6391 /* When a call is in progress, this routine is called occasionally to
6392  * make sure that some traffic has arrived (or been sent to) the peer.
6393  * If nothing has arrived in a reasonable amount of time, the call is
6394  * declared dead; if nothing has been sent for a while, we send a
6395  * keep-alive packet (if we're actually trying to keep the call alive)
6396  */
6397 void
6398 rxi_KeepAliveEvent(struct rxevent *event, void *arg1, void *dummy)
6399 {
6400     struct rx_call *call = arg1;
6401     struct rx_connection *conn;
6402     afs_uint32 now;
6403
6404     MUTEX_ENTER(&rx_refcnt_mutex);
6405     CALL_RELE(call, RX_CALL_REFCOUNT_ALIVE);
6406     MUTEX_EXIT(&rx_refcnt_mutex);
6407     MUTEX_ENTER(&call->lock);
6408     if (event == call->keepAliveEvent)
6409         call->keepAliveEvent = NULL;
6410     now = clock_Sec();
6411
6412 #ifdef RX_ENABLE_LOCKS
6413     if (rxi_CheckCall(call, 0)) {
6414         MUTEX_EXIT(&call->lock);
6415         return;
6416     }
6417 #else /* RX_ENABLE_LOCKS */
6418     if (rxi_CheckCall(call))
6419         return;
6420 #endif /* RX_ENABLE_LOCKS */
6421
6422     /* Don't try to keep alive dallying calls */
6423     if (call->state == RX_STATE_DALLY) {
6424         MUTEX_EXIT(&call->lock);
6425         return;
6426     }
6427
6428     conn = call->conn;
6429     if ((now - call->lastSendTime) > conn->secondsUntilPing) {
6430         /* Don't try to send keepalives if there is unacknowledged data */
6431         /* the rexmit code should be good enough, this little hack
6432          * doesn't quite work XXX */
6433         (void)rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
6434     }
6435     rxi_ScheduleKeepAliveEvent(call);
6436     MUTEX_EXIT(&call->lock);
6437 }
6438
6439 /* Does what's on the nameplate. */
6440 void
6441 rxi_GrowMTUEvent(struct rxevent *event, void *arg1, void *dummy)
6442 {
6443     struct rx_call *call = arg1;
6444     struct rx_connection *conn;
6445
6446     MUTEX_ENTER(&rx_refcnt_mutex);
6447     CALL_RELE(call, RX_CALL_REFCOUNT_ALIVE);
6448     MUTEX_EXIT(&rx_refcnt_mutex);
6449     MUTEX_ENTER(&call->lock);
6450
6451     if (event == call->growMTUEvent)
6452         call->growMTUEvent = NULL;
6453
6454 #ifdef RX_ENABLE_LOCKS
6455     if (rxi_CheckCall(call, 0)) {
6456         MUTEX_EXIT(&call->lock);
6457         return;
6458     }
6459 #else /* RX_ENABLE_LOCKS */
6460     if (rxi_CheckCall(call))
6461         return;
6462 #endif /* RX_ENABLE_LOCKS */
6463
6464     /* Don't bother with dallying calls */
6465     if (call->state == RX_STATE_DALLY) {
6466         MUTEX_EXIT(&call->lock);
6467         return;
6468     }
6469
6470     conn = call->conn;
6471
6472     /*
6473      * keep being scheduled, just don't do anything if we're at peak,
6474      * or we're not set up to be properly handled (idle timeout required)
6475      */
6476     if ((conn->peer->maxPacketSize != 0) &&
6477         (conn->peer->natMTU < RX_MAX_PACKET_SIZE) &&
6478         (conn->idleDeadErr))
6479         (void)rxi_SendAck(call, NULL, 0, RX_ACK_MTU, 0);
6480     rxi_ScheduleGrowMTUEvent(call, 0);
6481     MUTEX_EXIT(&call->lock);
6482 }
6483
6484 void
6485 rxi_ScheduleKeepAliveEvent(struct rx_call *call)
6486 {
6487     if (!call->keepAliveEvent) {
6488         struct clock when, now;
6489         clock_GetTime(&now);
6490         when = now;
6491         when.sec += call->conn->secondsUntilPing;
6492         MUTEX_ENTER(&rx_refcnt_mutex);
6493         CALL_HOLD(call, RX_CALL_REFCOUNT_ALIVE);
6494         MUTEX_EXIT(&rx_refcnt_mutex);
6495         call->keepAliveEvent =
6496             rxevent_PostNow(&when, &now, rxi_KeepAliveEvent, call, 0);
6497     }
6498 }
6499
6500 void
6501 rxi_ScheduleGrowMTUEvent(struct rx_call *call, int secs)
6502 {
6503     if (!call->growMTUEvent) {
6504         struct clock when, now;
6505
6506         clock_GetTime(&now);
6507         when = now;
6508         if (!secs) {
6509             if (call->conn->secondsUntilPing)
6510                 secs = (6*call->conn->secondsUntilPing)-1;
6511
6512             if (call->conn->secondsUntilDead)
6513                 secs = MIN(secs, (call->conn->secondsUntilDead-1));
6514         }
6515
6516         when.sec += secs;
6517         MUTEX_ENTER(&rx_refcnt_mutex);
6518         CALL_HOLD(call, RX_CALL_REFCOUNT_ALIVE);
6519         MUTEX_EXIT(&rx_refcnt_mutex);
6520         call->growMTUEvent =
6521             rxevent_PostNow(&when, &now, rxi_GrowMTUEvent, call, 0);
6522     }
6523 }
6524
6525 /* N.B. rxi_KeepAliveOff:  is defined earlier as a macro */
6526 void
6527 rxi_KeepAliveOn(struct rx_call *call)
6528 {
6529     /* Pretend last packet received was received now--i.e. if another
6530      * packet isn't received within the keep alive time, then the call
6531      * will die; Initialize last send time to the current time--even
6532      * if a packet hasn't been sent yet.  This will guarantee that a
6533      * keep-alive is sent within the ping time */
6534     call->lastReceiveTime = call->lastSendTime = clock_Sec();
6535     rxi_ScheduleKeepAliveEvent(call);
6536 }
6537
6538 void
6539 rxi_GrowMTUOn(struct rx_call *call)
6540 {
6541     struct rx_connection *conn = call->conn;
6542     MUTEX_ENTER(&conn->conn_data_lock);
6543     conn->lastPingSizeSer = conn->lastPingSize = 0;
6544     MUTEX_EXIT(&conn->conn_data_lock);
6545     rxi_ScheduleGrowMTUEvent(call, 1);
6546 }
6547
6548 /* This routine is called to send connection abort messages
6549  * that have been delayed to throttle looping clients. */
6550 void
6551 rxi_SendDelayedConnAbort(struct rxevent *event,
6552                          void *arg1, void *unused)
6553 {
6554     struct rx_connection *conn = arg1;
6555
6556     afs_int32 error;
6557     struct rx_packet *packet;
6558
6559     MUTEX_ENTER(&conn->conn_data_lock);
6560     conn->delayedAbortEvent = NULL;
6561     error = htonl(conn->error);
6562     conn->abortCount++;
6563     MUTEX_EXIT(&conn->conn_data_lock);
6564     packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
6565     if (packet) {
6566         packet =
6567             rxi_SendSpecial((struct rx_call *)0, conn, packet,
6568                             RX_PACKET_TYPE_ABORT, (char *)&error,
6569                             sizeof(error), 0);
6570         rxi_FreePacket(packet);
6571     }
6572 }
6573
6574 /* This routine is called to send call abort messages
6575  * that have been delayed to throttle looping clients. */
6576 void
6577 rxi_SendDelayedCallAbort(struct rxevent *event,
6578                          void *arg1, void *dummy)
6579 {
6580     struct rx_call *call = arg1;
6581
6582     afs_int32 error;
6583     struct rx_packet *packet;
6584
6585     MUTEX_ENTER(&call->lock);
6586     call->delayedAbortEvent = NULL;
6587     error = htonl(call->error);
6588     call->abortCount++;
6589     packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
6590     if (packet) {
6591         packet =
6592             rxi_SendSpecial(call, call->conn, packet, RX_PACKET_TYPE_ABORT,
6593                             (char *)&error, sizeof(error), 0);
6594         rxi_FreePacket(packet);
6595     }
6596     MUTEX_EXIT(&call->lock);
6597     MUTEX_ENTER(&rx_refcnt_mutex);
6598     CALL_RELE(call, RX_CALL_REFCOUNT_ABORT);
6599     MUTEX_EXIT(&rx_refcnt_mutex);
6600 }
6601
6602 /* This routine is called periodically (every RX_AUTH_REQUEST_TIMEOUT
6603  * seconds) to ask the client to authenticate itself.  The routine
6604  * issues a challenge to the client, which is obtained from the
6605  * security object associated with the connection */
6606 void
6607 rxi_ChallengeEvent(struct rxevent *event,
6608                    void *arg0, void *arg1, int tries)
6609 {
6610     struct rx_connection *conn = arg0;
6611
6612     conn->challengeEvent = NULL;
6613     if (RXS_CheckAuthentication(conn->securityObject, conn) != 0) {
6614         struct rx_packet *packet;
6615         struct clock when, now;
6616
6617         if (tries <= 0) {
6618             /* We've failed to authenticate for too long.
6619              * Reset any calls waiting for authentication;
6620              * they are all in RX_STATE_PRECALL.
6621              */
6622             int i;
6623
6624             MUTEX_ENTER(&conn->conn_call_lock);
6625             for (i = 0; i < RX_MAXCALLS; i++) {
6626                 struct rx_call *call = conn->call[i];
6627                 if (call) {
6628                     MUTEX_ENTER(&call->lock);
6629                     if (call->state == RX_STATE_PRECALL) {
6630                         rxi_CallError(call, RX_CALL_DEAD);
6631                         rxi_SendCallAbort(call, NULL, 0, 0);
6632                     }
6633                     MUTEX_EXIT(&call->lock);
6634                 }
6635             }
6636             MUTEX_EXIT(&conn->conn_call_lock);
6637             return;
6638         }
6639
6640         packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
6641         if (packet) {
6642             /* If there's no packet available, do this later. */
6643             RXS_GetChallenge(conn->securityObject, conn, packet);
6644             rxi_SendSpecial((struct rx_call *)0, conn, packet,
6645                             RX_PACKET_TYPE_CHALLENGE, NULL, -1, 0);
6646             rxi_FreePacket(packet);
6647         }
6648         clock_GetTime(&now);
6649         when = now;
6650         when.sec += RX_CHALLENGE_TIMEOUT;
6651         conn->challengeEvent =
6652             rxevent_PostNow2(&when, &now, rxi_ChallengeEvent, conn, 0,
6653                          (tries - 1));
6654     }
6655 }
6656
6657 /* Call this routine to start requesting the client to authenticate
6658  * itself.  This will continue until authentication is established,
6659  * the call times out, or an invalid response is returned.  The
6660  * security object associated with the connection is asked to create
6661  * the challenge at this time.  N.B.  rxi_ChallengeOff is a macro,
6662  * defined earlier. */
6663 void
6664 rxi_ChallengeOn(struct rx_connection *conn)
6665 {
6666     if (!conn->challengeEvent) {
6667         RXS_CreateChallenge(conn->securityObject, conn);
6668         rxi_ChallengeEvent(NULL, conn, 0, RX_CHALLENGE_MAXTRIES);
6669     };
6670 }
6671
6672
6673 /* rxi_ComputeRoundTripTime is called with peer locked. */
6674 /* peer may be null */
6675 static void
6676 rxi_ComputeRoundTripTime(struct rx_packet *p,
6677                          struct rx_ackPacket *ack,
6678                          struct rx_peer *peer,
6679                          struct clock *now)
6680 {
6681     struct clock thisRtt, *sentp;
6682     int rtt_timeout;
6683     int serial;
6684
6685     /* If the ACK is delayed, then do nothing */
6686     if (ack->reason == RX_ACK_DELAY)
6687         return;
6688
6689     /* On the wire, jumbograms are a single UDP packet. We shouldn't count
6690      * their RTT multiple times, so only include the RTT of the last packet
6691      * in a jumbogram */
6692     if (p->flags & RX_JUMBO_PACKET)
6693         return;
6694
6695     /* Use the serial number to determine which transmission the ACK is for,
6696      * and set the sent time to match this. If we have no serial number, then
6697      * only use the ACK for RTT calculations if the packet has not been
6698      * retransmitted
6699      */
6700
6701     serial = ntohl(ack->serial);
6702     if (serial) {
6703         if (serial == p->header.serial) {
6704             sentp = &p->timeSent;
6705         } else if (serial == p->firstSerial) {
6706             sentp = &p->firstSent;
6707         } else if (clock_Eq(&p->timeSent, &p->firstSent)) {
6708             sentp = &p->firstSent;
6709         } else
6710             return;
6711     } else {
6712         if (clock_Eq(&p->timeSent, &p->firstSent)) {
6713             sentp = &p->firstSent;
6714         } else
6715             return;
6716     }
6717
6718     thisRtt = *now;
6719
6720     if (clock_Lt(&thisRtt, sentp))
6721         return;                 /* somebody set the clock back, don't count this time. */
6722
6723     clock_Sub(&thisRtt, sentp);
6724     dpf(("rxi_ComputeRoundTripTime(call=%d packet=%"AFS_PTR_FMT" rttp=%d.%06d sec)\n",
6725           p->header.callNumber, p, thisRtt.sec, thisRtt.usec));
6726
6727     if (clock_IsZero(&thisRtt)) {
6728         /*
6729          * The actual round trip time is shorter than the
6730          * clock_GetTime resolution.  It is most likely 1ms or 100ns.
6731          * Since we can't tell which at the moment we will assume 1ms.
6732          */
6733         thisRtt.usec = 1000;
6734     }
6735
6736     if (rx_stats_active) {
6737         MUTEX_ENTER(&rx_stats_mutex);
6738         if (clock_Lt(&thisRtt, &rx_stats.minRtt))
6739             rx_stats.minRtt = thisRtt;
6740         if (clock_Gt(&thisRtt, &rx_stats.maxRtt)) {
6741             if (thisRtt.sec > 60) {
6742                 MUTEX_EXIT(&rx_stats_mutex);
6743                 return;         /* somebody set the clock ahead */
6744             }
6745             rx_stats.maxRtt = thisRtt;
6746         }
6747         clock_Add(&rx_stats.totalRtt, &thisRtt);
6748         rx_atomic_inc(&rx_stats.nRttSamples);
6749         MUTEX_EXIT(&rx_stats_mutex);
6750     }
6751
6752     /* better rtt calculation courtesy of UMich crew (dave,larry,peter,?) */
6753
6754     /* Apply VanJacobson round-trip estimations */
6755     if (peer->rtt) {
6756         int delta;
6757
6758         /*
6759          * srtt (peer->rtt) is in units of one-eighth-milliseconds.
6760          * srtt is stored as fixed point with 3 bits after the binary
6761          * point (i.e., scaled by 8). The following magic is
6762          * equivalent to the smoothing algorithm in rfc793 with an
6763          * alpha of .875 (srtt' = rtt/8 + srtt*7/8 in fixed point).
6764          * srtt'*8 = rtt + srtt*7
6765          * srtt'*8 = srtt*8 + rtt - srtt
6766          * srtt' = srtt + rtt/8 - srtt/8
6767          * srtt' = srtt + (rtt - srtt)/8
6768          */
6769
6770         delta = _8THMSEC(&thisRtt) - peer->rtt;
6771         peer->rtt += (delta >> 3);
6772
6773         /*
6774          * We accumulate a smoothed rtt variance (actually, a smoothed
6775          * mean difference), then set the retransmit timer to smoothed
6776          * rtt + 4 times the smoothed variance (was 2x in van's original
6777          * paper, but 4x works better for me, and apparently for him as
6778          * well).
6779          * rttvar is stored as
6780          * fixed point with 2 bits after the binary point (scaled by
6781          * 4).  The following is equivalent to rfc793 smoothing with
6782          * an alpha of .75 (rttvar' = rttvar*3/4 + |delta| / 4).
6783          *   rttvar'*4 = rttvar*3 + |delta|
6784          *   rttvar'*4 = rttvar*4 + |delta| - rttvar
6785          *   rttvar' = rttvar + |delta|/4 - rttvar/4
6786          *   rttvar' = rttvar + (|delta| - rttvar)/4
6787          * This replaces rfc793's wired-in beta.
6788          * dev*4 = dev*4 + (|actual - expected| - dev)
6789          */
6790
6791         if (delta < 0)
6792             delta = -delta;
6793
6794         delta -= (peer->rtt_dev << 1);
6795         peer->rtt_dev += (delta >> 3);
6796     } else {
6797         /* I don't have a stored RTT so I start with this value.  Since I'm
6798          * probably just starting a call, and will be pushing more data down
6799          * this, I expect congestion to increase rapidly.  So I fudge a
6800          * little, and I set deviance to half the rtt.  In practice,
6801          * deviance tends to approach something a little less than
6802          * half the smoothed rtt. */
6803         peer->rtt = _8THMSEC(&thisRtt) + 8;
6804         peer->rtt_dev = peer->rtt >> 2; /* rtt/2: they're scaled differently */
6805     }
6806     /* the smoothed RTT time is RTT + 4*MDEV
6807      *
6808      * We allow a user specified minimum to be set for this, to allow clamping
6809      * at a minimum value in the same way as TCP. In addition, we have to allow
6810      * for the possibility that this packet is answered by a delayed ACK, so we
6811      * add on a fixed 200ms to account for that timer expiring.
6812      */
6813
6814     rtt_timeout = MAX(((peer->rtt >> 3) + peer->rtt_dev),
6815                       rx_minPeerTimeout) + 200;
6816     clock_Zero(&(peer->timeout));
6817     clock_Addmsec(&(peer->timeout), rtt_timeout);
6818
6819     dpf(("rxi_ComputeRoundTripTime(call=%d packet=%"AFS_PTR_FMT" rtt=%d ms, srtt=%d ms, rtt_dev=%d ms, timeout=%d.%06d sec)\n",
6820           p->header.callNumber, p, MSEC(&thisRtt), peer->rtt >> 3, peer->rtt_dev >> 2, (peer->timeout.sec), (peer->timeout.usec)));
6821 }
6822
6823
6824 /* Find all server connections that have not been active for a long time, and
6825  * toss them */
6826 void
6827 rxi_ReapConnections(struct rxevent *unused, void *unused1, void *unused2)
6828 {
6829     struct clock now, when;
6830     clock_GetTime(&now);
6831
6832     /* Find server connection structures that haven't been used for
6833      * greater than rx_idleConnectionTime */
6834     {
6835         struct rx_connection **conn_ptr, **conn_end;
6836         int i, havecalls = 0;
6837         MUTEX_ENTER(&rx_connHashTable_lock);
6838         for (conn_ptr = &rx_connHashTable[0], conn_end =
6839              &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
6840              conn_ptr++) {
6841             struct rx_connection *conn, *next;
6842             struct rx_call *call;
6843             int result;
6844
6845           rereap:
6846             for (conn = *conn_ptr; conn; conn = next) {
6847                 /* XXX -- Shouldn't the connection be locked? */
6848                 next = conn->next;
6849                 havecalls = 0;
6850                 for (i = 0; i < RX_MAXCALLS; i++) {
6851                     call = conn->call[i];
6852                     if (call) {
6853                         int code;
6854                         havecalls = 1;
6855                         code = MUTEX_TRYENTER(&call->lock);
6856                         if (!code)
6857                             continue;
6858 #ifdef RX_ENABLE_LOCKS
6859                         result = rxi_CheckCall(call, 1);
6860 #else /* RX_ENABLE_LOCKS */
6861                         result = rxi_CheckCall(call);
6862 #endif /* RX_ENABLE_LOCKS */
6863                         MUTEX_EXIT(&call->lock);
6864                         if (result == -2) {
6865                             /* If CheckCall freed the call, it might
6866                              * have destroyed  the connection as well,
6867                              * which screws up the linked lists.
6868                              */
6869                             goto rereap;
6870                         }
6871                     }
6872                 }
6873                 if (conn->type == RX_SERVER_CONNECTION) {
6874                     /* This only actually destroys the connection if
6875                      * there are no outstanding calls */
6876                     MUTEX_ENTER(&conn->conn_data_lock);
6877                     MUTEX_ENTER(&rx_refcnt_mutex);
6878                     if (!havecalls && !conn->refCount
6879                         && ((conn->lastSendTime + rx_idleConnectionTime) <
6880                             now.sec)) {
6881                         conn->refCount++;       /* it will be decr in rx_DestroyConn */
6882                         MUTEX_EXIT(&rx_refcnt_mutex);
6883                         MUTEX_EXIT(&conn->conn_data_lock);
6884 #ifdef RX_ENABLE_LOCKS
6885                         rxi_DestroyConnectionNoLock(conn);
6886 #else /* RX_ENABLE_LOCKS */
6887                         rxi_DestroyConnection(conn);
6888 #endif /* RX_ENABLE_LOCKS */
6889                     }
6890 #ifdef RX_ENABLE_LOCKS
6891                     else {
6892                         MUTEX_EXIT(&rx_refcnt_mutex);
6893                         MUTEX_EXIT(&conn->conn_data_lock);
6894                     }
6895 #endif /* RX_ENABLE_LOCKS */
6896                 }
6897             }
6898         }
6899 #ifdef RX_ENABLE_LOCKS
6900         while (rx_connCleanup_list) {
6901             struct rx_connection *conn;
6902             conn = rx_connCleanup_list;
6903             rx_connCleanup_list = rx_connCleanup_list->next;
6904             MUTEX_EXIT(&rx_connHashTable_lock);
6905             rxi_CleanupConnection(conn);
6906             MUTEX_ENTER(&rx_connHashTable_lock);
6907         }
6908         MUTEX_EXIT(&rx_connHashTable_lock);
6909 #endif /* RX_ENABLE_LOCKS */
6910     }
6911
6912     /* Find any peer structures that haven't been used (haven't had an
6913      * associated connection) for greater than rx_idlePeerTime */
6914     {
6915         struct rx_peer **peer_ptr, **peer_end;
6916         int code;
6917
6918         /*
6919          * Why do we need to hold the rx_peerHashTable_lock across
6920          * the incrementing of peer_ptr since the rx_peerHashTable
6921          * array is not changing?  We don't.
6922          *
6923          * By dropping the lock periodically we can permit other
6924          * activities to be performed while a rxi_ReapConnections
6925          * call is in progress.  The goal of reap connections
6926          * is to clean up quickly without causing large amounts
6927          * of contention.  Therefore, it is important that global
6928          * mutexes not be held for extended periods of time.
6929          */
6930         for (peer_ptr = &rx_peerHashTable[0], peer_end =
6931              &rx_peerHashTable[rx_hashTableSize]; peer_ptr < peer_end;
6932              peer_ptr++) {
6933             struct rx_peer *peer, *next, *prev;
6934
6935             MUTEX_ENTER(&rx_peerHashTable_lock);
6936             for (prev = peer = *peer_ptr; peer; peer = next) {
6937                 next = peer->next;
6938                 code = MUTEX_TRYENTER(&peer->peer_lock);
6939                 if ((code) && (peer->refCount == 0)
6940                     && ((peer->idleWhen + rx_idlePeerTime) < now.sec)) {
6941                     rx_interface_stat_p rpc_stat, nrpc_stat;
6942                     size_t space;
6943
6944                     /*
6945                      * now know that this peer object is one to be
6946                      * removed from the hash table.  Once it is removed
6947                      * it can't be referenced by other threads.
6948                      * Lets remove it first and decrement the struct
6949                      * nPeerStructs count.
6950                      */
6951                     if (peer == *peer_ptr) {
6952                         *peer_ptr = next;
6953                         prev = next;
6954                     } else
6955                         prev->next = next;
6956
6957                     if (rx_stats_active)
6958                         rx_atomic_dec(&rx_stats.nPeerStructs);
6959
6960                     /*
6961                      * Now if we hold references on 'prev' and 'next'
6962                      * we can safely drop the rx_peerHashTable_lock
6963                      * while we destroy this 'peer' object.
6964                      */
6965                     if (next)
6966                         next->refCount++;
6967                     if (prev)
6968                         prev->refCount++;
6969                     MUTEX_EXIT(&rx_peerHashTable_lock);
6970
6971                     MUTEX_EXIT(&peer->peer_lock);
6972                     MUTEX_DESTROY(&peer->peer_lock);
6973                     for (queue_Scan
6974                          (&peer->rpcStats, rpc_stat, nrpc_stat,
6975                           rx_interface_stat)) {
6976                         unsigned int num_funcs;
6977                         if (!rpc_stat)
6978                             break;
6979                         queue_Remove(&rpc_stat->queue_header);
6980                         queue_Remove(&rpc_stat->all_peers);
6981                         num_funcs = rpc_stat->stats[0].func_total;
6982                         space =
6983                             sizeof(rx_interface_stat_t) +
6984                             rpc_stat->stats[0].func_total *
6985                             sizeof(rx_function_entry_v1_t);
6986
6987                         rxi_Free(rpc_stat, space);
6988
6989                         MUTEX_ENTER(&rx_rpc_stats);
6990                         rxi_rpc_peer_stat_cnt -= num_funcs;
6991                         MUTEX_EXIT(&rx_rpc_stats);
6992                     }
6993                     rxi_FreePeer(peer);
6994
6995                     /*
6996                      * Regain the rx_peerHashTable_lock and
6997                      * decrement the reference count on 'prev'
6998                      * and 'next'.
6999                      */
7000                     MUTEX_ENTER(&rx_peerHashTable_lock);
7001                     if (next)
7002                         next->refCount--;
7003                     if (prev)
7004                         prev->refCount--;
7005                 } else {
7006                     if (code) {
7007                         MUTEX_EXIT(&peer->peer_lock);
7008                     }
7009                     prev = peer;
7010                 }
7011             }
7012             MUTEX_EXIT(&rx_peerHashTable_lock);
7013         }
7014     }
7015
7016     /* THIS HACK IS A TEMPORARY HACK.  The idea is that the race condition in
7017      * rxi_AllocSendPacket, if it hits, will be handled at the next conn
7018      * GC, just below.  Really, we shouldn't have to keep moving packets from
7019      * one place to another, but instead ought to always know if we can
7020      * afford to hold onto a packet in its particular use.  */
7021     MUTEX_ENTER(&rx_freePktQ_lock);
7022     if (rx_waitingForPackets) {
7023         rx_waitingForPackets = 0;
7024 #ifdef  RX_ENABLE_LOCKS
7025         CV_BROADCAST(&rx_waitingForPackets_cv);
7026 #else
7027         osi_rxWakeup(&rx_waitingForPackets);
7028 #endif
7029     }
7030     MUTEX_EXIT(&rx_freePktQ_lock);
7031
7032     when = now;
7033     when.sec += RX_REAP_TIME;   /* Check every RX_REAP_TIME seconds */
7034     rxevent_Post(&when, rxi_ReapConnections, 0, 0);
7035 }
7036
7037
7038 /* rxs_Release - This isn't strictly necessary but, since the macro name from
7039  * rx.h is sort of strange this is better.  This is called with a security
7040  * object before it is discarded.  Each connection using a security object has
7041  * its own refcount to the object so it won't actually be freed until the last
7042  * connection is destroyed.
7043  *
7044  * This is the only rxs module call.  A hold could also be written but no one
7045  * needs it. */
7046
7047 int
7048 rxs_Release(struct rx_securityClass *aobj)
7049 {
7050     return RXS_Close(aobj);
7051 }
7052
7053 #ifdef ADAPT_WINDOW
7054 #define RXRATE_PKT_OH   (RX_HEADER_SIZE + RX_IPUDP_SIZE)
7055 #define RXRATE_SMALL_PKT    (RXRATE_PKT_OH + sizeof(struct rx_ackPacket))
7056 #define RXRATE_AVG_SMALL_PKT    (RXRATE_PKT_OH + (sizeof(struct rx_ackPacket)/2))
7057 #define RXRATE_LARGE_PKT    (RXRATE_SMALL_PKT + 256)
7058
7059 /* Adjust our estimate of the transmission rate to this peer, given
7060  * that the packet p was just acked. We can adjust peer->timeout and
7061  * call->twind. Pragmatically, this is called
7062  * only with packets of maximal length.
7063  * Called with peer and call locked.
7064  */
7065
7066 static void
7067 rxi_ComputeRate(struct rx_peer *peer, struct rx_call *call,
7068                 struct rx_packet *p, struct rx_packet *ackp, u_char ackReason)
7069 {
7070     afs_int32 xferSize, xferMs;
7071     afs_int32 minTime;
7072     struct clock newTO;
7073
7074     /* Count down packets */
7075     if (peer->rateFlag > 0)
7076         peer->rateFlag--;
7077     /* Do nothing until we're enabled */
7078     if (peer->rateFlag != 0)
7079         return;
7080     if (!call->conn)
7081         return;
7082
7083     /* Count only when the ack seems legitimate */
7084     switch (ackReason) {
7085     case RX_ACK_REQUESTED:
7086         xferSize =
7087             p->length + RX_HEADER_SIZE + call->conn->securityMaxTrailerSize;
7088         xferMs = peer->rtt;
7089         break;
7090
7091     case RX_ACK_PING_RESPONSE:
7092         if (p)                  /* want the response to ping-request, not data send */
7093             return;
7094         clock_GetTime(&newTO);
7095         if (clock_Gt(&newTO, &call->pingRequestTime)) {
7096             clock_Sub(&newTO, &call->pingRequestTime);
7097             xferMs = (newTO.sec * 1000) + (newTO.usec / 1000);
7098         } else {
7099             return;
7100         }
7101         xferSize = rx_AckDataSize(rx_maxSendWindow) + RX_HEADER_SIZE;
7102         break;
7103
7104     default:
7105         return;
7106     }
7107
7108     dpf(("CONG peer %lx/%u: sample (%s) size %ld, %ld ms (to %d.%06d, rtt %u, ps %u)\n",
7109           ntohl(peer->host), ntohs(peer->port), (ackReason == RX_ACK_REQUESTED ? "dataack" : "pingack"),
7110           xferSize, xferMs, peer->timeout.sec, peer->timeout.usec, peer->smRtt, peer->ifMTU));
7111
7112     /* Track only packets that are big enough. */
7113     if ((p->length + RX_HEADER_SIZE + call->conn->securityMaxTrailerSize) <
7114         peer->ifMTU)
7115         return;
7116
7117     /* absorb RTT data (in milliseconds) for these big packets */
7118     if (peer->smRtt == 0) {
7119         peer->smRtt = xferMs;
7120     } else {
7121         peer->smRtt = ((peer->smRtt * 15) + xferMs + 4) >> 4;
7122         if (!peer->smRtt)
7123             peer->smRtt = 1;
7124     }
7125
7126     if (peer->countDown) {
7127         peer->countDown--;
7128         return;
7129     }
7130     peer->countDown = 10;       /* recalculate only every so often */
7131
7132     /* In practice, we can measure only the RTT for full packets,
7133      * because of the way Rx acks the data that it receives.  (If it's
7134      * smaller than a full packet, it often gets implicitly acked
7135      * either by the call response (from a server) or by the next call
7136      * (from a client), and either case confuses transmission times
7137      * with processing times.)  Therefore, replace the above
7138      * more-sophisticated processing with a simpler version, where the
7139      * smoothed RTT is kept for full-size packets, and the time to
7140      * transmit a windowful of full-size packets is simply RTT *
7141      * windowSize. Again, we take two steps:
7142      - ensure the timeout is large enough for a single packet's RTT;
7143      - ensure that the window is small enough to fit in the desired timeout.*/
7144
7145     /* First, the timeout check. */
7146     minTime = peer->smRtt;
7147     /* Get a reasonable estimate for a timeout period */
7148     minTime += minTime;
7149     newTO.sec = minTime / 1000;
7150     newTO.usec = (minTime - (newTO.sec * 1000)) * 1000;
7151
7152     /* Increase the timeout period so that we can always do at least
7153      * one packet exchange */
7154     if (clock_Gt(&newTO, &peer->timeout)) {
7155
7156         dpf(("CONG peer %lx/%u: timeout %d.%06d ==> %ld.%06d (rtt %u)\n",
7157               ntohl(peer->host), ntohs(peer->port), peer->timeout.sec, peer->timeout.usec,
7158               newTO.sec, newTO.usec, peer->smRtt));
7159
7160         peer->timeout = newTO;
7161     }
7162
7163     /* Now, get an estimate for the transmit window size. */
7164     minTime = peer->timeout.sec * 1000 + (peer->timeout.usec / 1000);
7165     /* Now, convert to the number of full packets that could fit in a
7166      * reasonable fraction of that interval */
7167     minTime /= (peer->smRtt << 1);
7168     minTime = MAX(minTime, rx_minPeerTimeout);
7169     xferSize = minTime;         /* (make a copy) */
7170
7171     /* Now clamp the size to reasonable bounds. */
7172     if (minTime <= 1)
7173         minTime = 1;
7174     else if (minTime > rx_maxSendWindow)
7175         minTime = rx_maxSendWindow;
7176 /*    if (minTime != peer->maxWindow) {
7177       dpf(("CONG peer %lx/%u: windowsize %lu ==> %lu (to %lu.%06lu, rtt %u)\n",
7178              ntohl(peer->host), ntohs(peer->port), peer->maxWindow, minTime,
7179              peer->timeout.sec, peer->timeout.usec, peer->smRtt));
7180       peer->maxWindow = minTime;
7181         elide... call->twind = minTime;
7182     }
7183 */
7184
7185     /* Cut back on the peer timeout if it had earlier grown unreasonably.
7186      * Discern this by calculating the timeout necessary for rx_Window
7187      * packets. */
7188     if ((xferSize > rx_maxSendWindow) && (peer->timeout.sec >= 3)) {
7189         /* calculate estimate for transmission interval in milliseconds */
7190         minTime = rx_maxSendWindow * peer->smRtt;
7191         if (minTime < 1000) {
7192             dpf(("CONG peer %lx/%u: cut TO %d.%06d by 0.5 (rtt %u)\n",
7193                  ntohl(peer->host), ntohs(peer->port), peer->timeout.sec,
7194                  peer->timeout.usec, peer->smRtt));
7195
7196             newTO.sec = 0;      /* cut back on timeout by half a second */
7197             newTO.usec = 500000;
7198             clock_Sub(&peer->timeout, &newTO);
7199         }
7200     }
7201
7202     return;
7203 }                               /* end of rxi_ComputeRate */
7204 #endif /* ADAPT_WINDOW */
7205
7206
7207 void
7208 rxi_DebugInit(void)
7209 {
7210 #ifdef RXDEBUG
7211 #ifdef AFS_NT40_ENV
7212 #define TRACE_OPTION_RX_DEBUG 16
7213     HKEY parmKey;
7214     DWORD dummyLen;
7215     DWORD TraceOption;
7216     long code;
7217
7218     rxdebug_active = 0;
7219
7220     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
7221                          0, KEY_QUERY_VALUE, &parmKey);
7222     if (code != ERROR_SUCCESS)
7223         return;
7224
7225     dummyLen = sizeof(TraceOption);
7226     code = RegQueryValueEx(parmKey, "TraceOption", NULL, NULL,
7227                            (BYTE *) &TraceOption, &dummyLen);
7228     if (code == ERROR_SUCCESS) {
7229         rxdebug_active = (TraceOption & TRACE_OPTION_RX_DEBUG) ? 1 : 0;
7230     }
7231     RegCloseKey (parmKey);
7232 #endif /* AFS_NT40_ENV */
7233 #endif
7234 }
7235
7236 void
7237 rx_DebugOnOff(int on)
7238 {
7239 #ifdef RXDEBUG
7240 #ifdef AFS_NT40_ENV
7241     rxdebug_active = on;
7242 #endif
7243 #endif
7244 }
7245
7246 void
7247 rx_StatsOnOff(int on)
7248 {
7249     rx_stats_active = on;
7250 }
7251
7252
7253 /* Don't call this debugging routine directly; use dpf */
7254 void
7255 rxi_DebugPrint(char *format, ...)
7256 {
7257 #ifdef RXDEBUG
7258     va_list ap;
7259 #ifdef AFS_NT40_ENV
7260     char msg[512];
7261     char tformat[256];
7262     size_t len;
7263
7264     va_start(ap, format);
7265
7266     len = _snprintf(tformat, sizeof(tformat), "tid[%d] %s", GetCurrentThreadId(), format);
7267
7268     if (len > 0) {
7269         len = _vsnprintf(msg, sizeof(msg)-2, tformat, ap);
7270         if (len > 0)
7271             OutputDebugString(msg);
7272     }
7273     va_end(ap);
7274 #else
7275     struct clock now;
7276
7277     va_start(ap, format);
7278
7279     clock_GetTime(&now);
7280     fprintf(rx_Log, " %d.%06d:", (unsigned int)now.sec,
7281             (unsigned int)now.usec);
7282     vfprintf(rx_Log, format, ap);
7283     va_end(ap);
7284 #endif
7285 #endif
7286 }
7287
7288 #ifndef KERNEL
7289 /*
7290  * This function is used to process the rx_stats structure that is local
7291  * to a process as well as an rx_stats structure received from a remote
7292  * process (via rxdebug).  Therefore, it needs to do minimal version
7293  * checking.
7294  */
7295 void
7296 rx_PrintTheseStats(FILE * file, struct rx_statistics *s, int size,
7297                    afs_int32 freePackets, char version)
7298 {
7299     int i;
7300
7301     if (size != sizeof(struct rx_statistics)) {
7302         fprintf(file,
7303                 "Unexpected size of stats structure: was %d, expected %" AFS_SIZET_FMT "\n",
7304                 size, sizeof(struct rx_statistics));
7305     }
7306
7307     fprintf(file, "rx stats: free packets %d, allocs %d, ", (int)freePackets,
7308             s->packetRequests);
7309
7310     if (version >= RX_DEBUGI_VERSION_W_NEWPACKETTYPES) {
7311         fprintf(file, "alloc-failures(rcv %u/%u,send %u/%u,ack %u)\n",
7312                 s->receivePktAllocFailures, s->receiveCbufPktAllocFailures,
7313                 s->sendPktAllocFailures, s->sendCbufPktAllocFailures,
7314                 s->specialPktAllocFailures);
7315     } else {
7316         fprintf(file, "alloc-failures(rcv %u,send %u,ack %u)\n",
7317                 s->receivePktAllocFailures, s->sendPktAllocFailures,
7318                 s->specialPktAllocFailures);
7319     }
7320
7321     fprintf(file,
7322             "   greedy %u, " "bogusReads %u (last from host %x), "
7323             "noPackets %u, " "noBuffers %u, " "selects %u, "
7324             "sendSelects %u\n", s->socketGreedy, s->bogusPacketOnRead,
7325             s->bogusHost, s->noPacketOnRead, s->noPacketBuffersOnRead,
7326             s->selects, s->sendSelects);
7327
7328     fprintf(file, "   packets read: ");
7329     for (i = 0; i < RX_N_PACKET_TYPES; i++) {
7330         fprintf(file, "%s %u ", rx_packetTypes[i], s->packetsRead[i]);
7331     }
7332     fprintf(file, "\n");
7333
7334     fprintf(file,
7335             "   other read counters: data %u, " "ack %u, " "dup %u "
7336             "spurious %u " "dally %u\n", s->dataPacketsRead,
7337             s->ackPacketsRead, s->dupPacketsRead, s->spuriousPacketsRead,
7338             s->ignorePacketDally);
7339
7340     fprintf(file, "   packets sent: ");
7341     for (i = 0; i < RX_N_PACKET_TYPES; i++) {
7342         fprintf(file, "%s %u ", rx_packetTypes[i], s->packetsSent[i]);
7343     }
7344     fprintf(file, "\n");
7345
7346     fprintf(file,
7347             "   other send counters: ack %u, " "data %u (not resends), "
7348             "resends %u, " "pushed %u, " "acked&ignored %u\n",
7349             s->ackPacketsSent, s->dataPacketsSent, s->dataPacketsReSent,
7350             s->dataPacketsPushed, s->ignoreAckedPacket);
7351
7352     fprintf(file,
7353             "   \t(these should be small) sendFailed %u, " "fatalErrors %u\n",
7354             s->netSendFailures, (int)s->fatalErrors);
7355
7356     if (s->nRttSamples) {
7357         fprintf(file, "   Average rtt is %0.3f, with %d samples\n",
7358                 clock_Float(&s->totalRtt) / s->nRttSamples, s->nRttSamples);
7359
7360         fprintf(file, "   Minimum rtt is %0.3f, maximum is %0.3f\n",
7361                 clock_Float(&s->minRtt), clock_Float(&s->maxRtt));
7362     }
7363
7364     fprintf(file,
7365             "   %d server connections, " "%d client connections, "
7366             "%d peer structs, " "%d call structs, " "%d free call structs\n",
7367             s->nServerConns, s->nClientConns, s->nPeerStructs,
7368             s->nCallStructs, s->nFreeCallStructs);
7369
7370 #if     !defined(AFS_PTHREAD_ENV) && !defined(AFS_USE_GETTIMEOFDAY)
7371     fprintf(file, "   %d clock updates\n", clock_nUpdates);
7372 #endif
7373 }
7374
7375 /* for backward compatibility */
7376 void
7377 rx_PrintStats(FILE * file)
7378 {
7379     MUTEX_ENTER(&rx_stats_mutex);
7380     rx_PrintTheseStats(file, (struct rx_statistics *) &rx_stats,
7381                        sizeof(rx_stats), rx_nFreePackets,
7382                        RX_DEBUGI_VERSION);
7383     MUTEX_EXIT(&rx_stats_mutex);
7384 }
7385
7386 void
7387 rx_PrintPeerStats(FILE * file, struct rx_peer *peer)
7388 {
7389     fprintf(file, "Peer %x.%d.  " "Burst size %d, " "burst wait %d.%06d.\n",
7390             ntohl(peer->host), (int)ntohs(peer->port), (int)peer->burstSize,
7391             (int)peer->burstWait.sec, (int)peer->burstWait.usec);
7392
7393     fprintf(file,
7394             "   Rtt %d, " "retry time %u.%06d, " "total sent %d, "
7395             "resent %d\n", peer->rtt, (int)peer->timeout.sec,
7396             (int)peer->timeout.usec, peer->nSent, peer->reSends);
7397
7398     fprintf(file,
7399             "   Packet size %d, " "max in packet skew %d, "
7400             "max out packet skew %d\n", peer->ifMTU, (int)peer->inPacketSkew,
7401             (int)peer->outPacketSkew);
7402 }
7403 #endif
7404
7405 #if defined(AFS_PTHREAD_ENV) && defined(RXDEBUG)
7406 /*
7407  * This mutex protects the following static variables:
7408  * counter
7409  */
7410
7411 #define LOCK_RX_DEBUG MUTEX_ENTER(&rx_debug_mutex)
7412 #define UNLOCK_RX_DEBUG MUTEX_EXIT(&rx_debug_mutex)
7413 #else
7414 #define LOCK_RX_DEBUG
7415 #define UNLOCK_RX_DEBUG
7416 #endif /* AFS_PTHREAD_ENV */
7417
7418 #if defined(RXDEBUG) || defined(MAKEDEBUGCALL)
7419 static int
7420 MakeDebugCall(osi_socket socket, afs_uint32 remoteAddr, afs_uint16 remotePort,
7421               u_char type, void *inputData, size_t inputLength,
7422               void *outputData, size_t outputLength)
7423 {
7424     static afs_int32 counter = 100;
7425     time_t waitTime, waitCount;
7426     struct rx_header theader;
7427     char tbuffer[1500];
7428     afs_int32 code;
7429     struct timeval tv_now, tv_wake, tv_delta;
7430     struct sockaddr_in taddr, faddr;
7431 #ifdef AFS_NT40_ENV
7432     int faddrLen;
7433 #else
7434     socklen_t faddrLen;
7435 #endif
7436     fd_set imask;
7437     char *tp;
7438
7439     waitTime = 1;
7440     waitCount = 5;
7441     LOCK_RX_DEBUG;
7442     counter++;
7443     UNLOCK_RX_DEBUG;
7444     tp = &tbuffer[sizeof(struct rx_header)];
7445     taddr.sin_family = AF_INET;
7446     taddr.sin_port = remotePort;
7447     taddr.sin_addr.s_addr = remoteAddr;
7448 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
7449     taddr.sin_len = sizeof(struct sockaddr_in);
7450 #endif
7451     while (1) {
7452         memset(&theader, 0, sizeof(theader));
7453         theader.epoch = htonl(999);
7454         theader.cid = 0;
7455         theader.callNumber = htonl(counter);
7456         theader.seq = 0;
7457         theader.serial = 0;
7458         theader.type = type;
7459         theader.flags = RX_CLIENT_INITIATED | RX_LAST_PACKET;
7460         theader.serviceId = 0;
7461
7462         memcpy(tbuffer, &theader, sizeof(theader));
7463         memcpy(tp, inputData, inputLength);
7464         code =
7465             sendto(socket, tbuffer, inputLength + sizeof(struct rx_header), 0,
7466                    (struct sockaddr *)&taddr, sizeof(struct sockaddr_in));
7467
7468         /* see if there's a packet available */
7469         gettimeofday(&tv_wake, NULL);
7470         tv_wake.tv_sec += waitTime;
7471         for (;;) {
7472             FD_ZERO(&imask);
7473             FD_SET(socket, &imask);
7474             tv_delta.tv_sec = tv_wake.tv_sec;
7475             tv_delta.tv_usec = tv_wake.tv_usec;
7476             gettimeofday(&tv_now, NULL);
7477
7478             if (tv_delta.tv_usec < tv_now.tv_usec) {
7479                 /* borrow */
7480                 tv_delta.tv_usec += 1000000;
7481                 tv_delta.tv_sec--;
7482             }
7483             tv_delta.tv_usec -= tv_now.tv_usec;
7484
7485             if (tv_delta.tv_sec < tv_now.tv_sec) {
7486                 /* time expired */
7487                 break;
7488             }
7489             tv_delta.tv_sec -= tv_now.tv_sec;
7490
7491 #ifdef AFS_NT40_ENV
7492             code = select(0, &imask, 0, 0, &tv_delta);
7493 #else /* AFS_NT40_ENV */
7494             code = select(socket + 1, &imask, 0, 0, &tv_delta);
7495 #endif /* AFS_NT40_ENV */
7496             if (code == 1 && FD_ISSET(socket, &imask)) {
7497                 /* now receive a packet */
7498                 faddrLen = sizeof(struct sockaddr_in);
7499                 code =
7500                     recvfrom(socket, tbuffer, sizeof(tbuffer), 0,
7501                              (struct sockaddr *)&faddr, &faddrLen);
7502
7503                 if (code > 0) {
7504                     memcpy(&theader, tbuffer, sizeof(struct rx_header));
7505                     if (counter == ntohl(theader.callNumber))
7506                         goto success;
7507                     continue;
7508                 }
7509             }
7510             break;
7511         }
7512
7513         /* see if we've timed out */
7514         if (!--waitCount) {
7515             return -1;
7516         }
7517         waitTime <<= 1;
7518     }
7519
7520  success:
7521     code -= sizeof(struct rx_header);
7522     if (code > outputLength)
7523         code = outputLength;
7524     memcpy(outputData, tp, code);
7525     return code;
7526 }
7527 #endif /* RXDEBUG */
7528
7529 afs_int32
7530 rx_GetServerDebug(osi_socket socket, afs_uint32 remoteAddr,
7531                   afs_uint16 remotePort, struct rx_debugStats * stat,
7532                   afs_uint32 * supportedValues)
7533 {
7534 #if defined(RXDEBUG) || defined(MAKEDEBUGCALL)
7535     afs_int32 rc = 0;
7536     struct rx_debugIn in;
7537
7538     *supportedValues = 0;
7539     in.type = htonl(RX_DEBUGI_GETSTATS);
7540     in.index = 0;
7541
7542     rc = MakeDebugCall(socket, remoteAddr, remotePort, RX_PACKET_TYPE_DEBUG,
7543                        &in, sizeof(in), stat, sizeof(*stat));
7544
7545     /*
7546      * If the call was successful, fixup the version and indicate
7547      * what contents of the stat structure are valid.
7548      * Also do net to host conversion of fields here.
7549      */
7550
7551     if (rc >= 0) {
7552         if (stat->version >= RX_DEBUGI_VERSION_W_SECSTATS) {
7553             *supportedValues |= RX_SERVER_DEBUG_SEC_STATS;
7554         }
7555         if (stat->version >= RX_DEBUGI_VERSION_W_GETALLCONN) {
7556             *supportedValues |= RX_SERVER_DEBUG_ALL_CONN;
7557         }
7558         if (stat->version >= RX_DEBUGI_VERSION_W_RXSTATS) {
7559             *supportedValues |= RX_SERVER_DEBUG_RX_STATS;
7560         }
7561         if (stat->version >= RX_DEBUGI_VERSION_W_WAITERS) {
7562             *supportedValues |= RX_SERVER_DEBUG_WAITER_CNT;
7563         }
7564         if (stat->version >= RX_DEBUGI_VERSION_W_IDLETHREADS) {
7565             *supportedValues |= RX_SERVER_DEBUG_IDLE_THREADS;
7566         }
7567         if (stat->version >= RX_DEBUGI_VERSION_W_NEWPACKETTYPES) {
7568             *supportedValues |= RX_SERVER_DEBUG_NEW_PACKETS;
7569         }
7570         if (stat->version >= RX_DEBUGI_VERSION_W_GETPEER) {
7571             *supportedValues |= RX_SERVER_DEBUG_ALL_PEER;
7572         }
7573         if (stat->version >= RX_DEBUGI_VERSION_W_WAITED) {
7574             *supportedValues |= RX_SERVER_DEBUG_WAITED_CNT;
7575         }
7576         if (stat->version >= RX_DEBUGI_VERSION_W_PACKETS) {
7577             *supportedValues |= RX_SERVER_DEBUG_PACKETS_CNT;
7578         }
7579         stat->nFreePackets = ntohl(stat->nFreePackets);
7580         stat->packetReclaims = ntohl(stat->packetReclaims);
7581         stat->callsExecuted = ntohl(stat->callsExecuted);
7582         stat->nWaiting = ntohl(stat->nWaiting);
7583         stat->idleThreads = ntohl(stat->idleThreads);
7584         stat->nWaited = ntohl(stat->nWaited);
7585         stat->nPackets = ntohl(stat->nPackets);
7586     }
7587 #else
7588     afs_int32 rc = -1;
7589 #endif
7590     return rc;
7591 }
7592
7593 afs_int32
7594 rx_GetServerStats(osi_socket socket, afs_uint32 remoteAddr,
7595                   afs_uint16 remotePort, struct rx_statistics * stat,
7596                   afs_uint32 * supportedValues)
7597 {
7598 #if defined(RXDEBUG) || defined(MAKEDEBUGCALL)
7599     afs_int32 rc = 0;
7600     struct rx_debugIn in;
7601     afs_int32 *lp = (afs_int32 *) stat;
7602     int i;
7603
7604     /*
7605      * supportedValues is currently unused, but added to allow future
7606      * versioning of this function.
7607      */
7608
7609     *supportedValues = 0;
7610     in.type = htonl(RX_DEBUGI_RXSTATS);
7611     in.index = 0;
7612     memset(stat, 0, sizeof(*stat));
7613
7614     rc = MakeDebugCall(socket, remoteAddr, remotePort, RX_PACKET_TYPE_DEBUG,
7615                        &in, sizeof(in), stat, sizeof(*stat));
7616
7617     if (rc >= 0) {
7618
7619         /*
7620          * Do net to host conversion here
7621          */
7622
7623         for (i = 0; i < sizeof(*stat) / sizeof(afs_int32); i++, lp++) {
7624             *lp = ntohl(*lp);
7625         }
7626     }
7627 #else
7628     afs_int32 rc = -1;
7629 #endif
7630     return rc;
7631 }
7632
7633 afs_int32
7634 rx_GetServerVersion(osi_socket socket, afs_uint32 remoteAddr,
7635                     afs_uint16 remotePort, size_t version_length,
7636                     char *version)
7637 {
7638 #if defined(RXDEBUG) || defined(MAKEDEBUGCALL)
7639     char a[1] = { 0 };
7640     return MakeDebugCall(socket, remoteAddr, remotePort,
7641                          RX_PACKET_TYPE_VERSION, a, 1, version,
7642                          version_length);
7643 #else
7644     return -1;
7645 #endif
7646 }
7647
7648 afs_int32
7649 rx_GetServerConnections(osi_socket socket, afs_uint32 remoteAddr,
7650                         afs_uint16 remotePort, afs_int32 * nextConnection,
7651                         int allConnections, afs_uint32 debugSupportedValues,
7652                         struct rx_debugConn * conn,
7653                         afs_uint32 * supportedValues)
7654 {
7655 #if defined(RXDEBUG) || defined(MAKEDEBUGCALL)
7656     afs_int32 rc = 0;
7657     struct rx_debugIn in;
7658     int i;
7659
7660     /*
7661      * supportedValues is currently unused, but added to allow future
7662      * versioning of this function.
7663      */
7664
7665     *supportedValues = 0;
7666     if (allConnections) {
7667         in.type = htonl(RX_DEBUGI_GETALLCONN);
7668     } else {
7669         in.type = htonl(RX_DEBUGI_GETCONN);
7670     }
7671     in.index = htonl(*nextConnection);
7672     memset(conn, 0, sizeof(*conn));
7673
7674     rc = MakeDebugCall(socket, remoteAddr, remotePort, RX_PACKET_TYPE_DEBUG,
7675                        &in, sizeof(in), conn, sizeof(*conn));
7676
7677     if (rc >= 0) {
7678         *nextConnection += 1;
7679
7680         /*
7681          * Convert old connection format to new structure.
7682          */
7683
7684         if (debugSupportedValues & RX_SERVER_DEBUG_OLD_CONN) {
7685             struct rx_debugConn_vL *vL = (struct rx_debugConn_vL *)conn;
7686 #define MOVEvL(a) (conn->a = vL->a)
7687
7688             /* any old or unrecognized version... */
7689             for (i = 0; i < RX_MAXCALLS; i++) {
7690                 MOVEvL(callState[i]);
7691                 MOVEvL(callMode[i]);
7692                 MOVEvL(callFlags[i]);
7693                 MOVEvL(callOther[i]);
7694             }
7695             if (debugSupportedValues & RX_SERVER_DEBUG_SEC_STATS) {
7696                 MOVEvL(secStats.type);
7697                 MOVEvL(secStats.level);
7698                 MOVEvL(secStats.flags);
7699                 MOVEvL(secStats.expires);
7700                 MOVEvL(secStats.packetsReceived);
7701                 MOVEvL(secStats.packetsSent);
7702                 MOVEvL(secStats.bytesReceived);
7703                 MOVEvL(secStats.bytesSent);
7704             }
7705         }
7706
7707         /*
7708          * Do net to host conversion here
7709          * NOTE:
7710          *    I don't convert host or port since we are most likely
7711          *    going to want these in NBO.
7712          */
7713         conn->cid = ntohl(conn->cid);
7714         conn->serial = ntohl(conn->serial);
7715         for (i = 0; i < RX_MAXCALLS; i++) {
7716             conn->callNumber[i] = ntohl(conn->callNumber[i]);
7717         }
7718         conn->error = ntohl(conn->error);
7719         conn->secStats.flags = ntohl(conn->secStats.flags);
7720         conn->secStats.expires = ntohl(conn->secStats.expires);
7721         conn->secStats.packetsReceived =
7722             ntohl(conn->secStats.packetsReceived);
7723         conn->secStats.packetsSent = ntohl(conn->secStats.packetsSent);
7724         conn->secStats.bytesReceived = ntohl(conn->secStats.bytesReceived);
7725         conn->secStats.bytesSent = ntohl(conn->secStats.bytesSent);
7726         conn->epoch = ntohl(conn->epoch);
7727         conn->natMTU = ntohl(conn->natMTU);
7728     }
7729 #else
7730     afs_int32 rc = -1;
7731 #endif
7732     return rc;
7733 }
7734
7735 afs_int32
7736 rx_GetServerPeers(osi_socket socket, afs_uint32 remoteAddr,
7737                   afs_uint16 remotePort, afs_int32 * nextPeer,
7738                   afs_uint32 debugSupportedValues, struct rx_debugPeer * peer,
7739                   afs_uint32 * supportedValues)
7740 {
7741 #if defined(RXDEBUG) || defined(MAKEDEBUGCALL)
7742     afs_int32 rc = 0;
7743     struct rx_debugIn in;
7744
7745     /*
7746      * supportedValues is currently unused, but added to allow future
7747      * versioning of this function.
7748      */
7749
7750     *supportedValues = 0;
7751     in.type = htonl(RX_DEBUGI_GETPEER);
7752     in.index = htonl(*nextPeer);
7753     memset(peer, 0, sizeof(*peer));
7754
7755     rc = MakeDebugCall(socket, remoteAddr, remotePort, RX_PACKET_TYPE_DEBUG,
7756                        &in, sizeof(in), peer, sizeof(*peer));
7757
7758     if (rc >= 0) {
7759         *nextPeer += 1;
7760
7761         /*
7762          * Do net to host conversion here
7763          * NOTE:
7764          *    I don't convert host or port since we are most likely
7765          *    going to want these in NBO.
7766          */
7767         peer->ifMTU = ntohs(peer->ifMTU);
7768         peer->idleWhen = ntohl(peer->idleWhen);
7769         peer->refCount = ntohs(peer->refCount);
7770         peer->burstWait.sec = ntohl(peer->burstWait.sec);
7771         peer->burstWait.usec = ntohl(peer->burstWait.usec);
7772         peer->rtt = ntohl(peer->rtt);
7773         peer->rtt_dev = ntohl(peer->rtt_dev);
7774         peer->timeout.sec = ntohl(peer->timeout.sec);
7775         peer->timeout.usec = ntohl(peer->timeout.usec);
7776         peer->nSent = ntohl(peer->nSent);
7777         peer->reSends = ntohl(peer->reSends);
7778         peer->inPacketSkew = ntohl(peer->inPacketSkew);
7779         peer->outPacketSkew = ntohl(peer->outPacketSkew);
7780         peer->rateFlag = ntohl(peer->rateFlag);
7781         peer->natMTU = ntohs(peer->natMTU);
7782         peer->maxMTU = ntohs(peer->maxMTU);
7783         peer->maxDgramPackets = ntohs(peer->maxDgramPackets);
7784         peer->ifDgramPackets = ntohs(peer->ifDgramPackets);
7785         peer->MTU = ntohs(peer->MTU);
7786         peer->cwind = ntohs(peer->cwind);
7787         peer->nDgramPackets = ntohs(peer->nDgramPackets);
7788         peer->congestSeq = ntohs(peer->congestSeq);
7789         peer->bytesSent.high = ntohl(peer->bytesSent.high);
7790         peer->bytesSent.low = ntohl(peer->bytesSent.low);
7791         peer->bytesReceived.high = ntohl(peer->bytesReceived.high);
7792         peer->bytesReceived.low = ntohl(peer->bytesReceived.low);
7793     }
7794 #else
7795     afs_int32 rc = -1;
7796 #endif
7797     return rc;
7798 }
7799
7800 afs_int32
7801 rx_GetLocalPeers(afs_uint32 peerHost, afs_uint16 peerPort,
7802                 struct rx_debugPeer * peerStats)
7803 {
7804         struct rx_peer *tp;
7805         afs_int32 error = 1; /* default to "did not succeed" */
7806         afs_uint32 hashValue = PEER_HASH(peerHost, peerPort);
7807
7808         MUTEX_ENTER(&rx_peerHashTable_lock);
7809         for(tp = rx_peerHashTable[hashValue];
7810               tp != NULL; tp = tp->next) {
7811                 if (tp->host == peerHost)
7812                         break;
7813         }
7814
7815         if (tp) {
7816                 tp->refCount++;
7817                 MUTEX_EXIT(&rx_peerHashTable_lock);
7818
7819                 error = 0;
7820
7821                 MUTEX_ENTER(&tp->peer_lock);
7822                 peerStats->host = tp->host;
7823                 peerStats->port = tp->port;
7824                 peerStats->ifMTU = tp->ifMTU;
7825                 peerStats->idleWhen = tp->idleWhen;
7826                 peerStats->refCount = tp->refCount;
7827                 peerStats->burstSize = tp->burstSize;
7828                 peerStats->burst = tp->burst;
7829                 peerStats->burstWait.sec = tp->burstWait.sec;
7830                 peerStats->burstWait.usec = tp->burstWait.usec;
7831                 peerStats->rtt = tp->rtt;
7832                 peerStats->rtt_dev = tp->rtt_dev;
7833                 peerStats->timeout.sec = tp->timeout.sec;
7834                 peerStats->timeout.usec = tp->timeout.usec;
7835                 peerStats->nSent = tp->nSent;
7836                 peerStats->reSends = tp->reSends;
7837                 peerStats->inPacketSkew = tp->inPacketSkew;
7838                 peerStats->outPacketSkew = tp->outPacketSkew;
7839                 peerStats->rateFlag = tp->rateFlag;
7840                 peerStats->natMTU = tp->natMTU;
7841                 peerStats->maxMTU = tp->maxMTU;
7842                 peerStats->maxDgramPackets = tp->maxDgramPackets;
7843                 peerStats->ifDgramPackets = tp->ifDgramPackets;
7844                 peerStats->MTU = tp->MTU;
7845                 peerStats->cwind = tp->cwind;
7846                 peerStats->nDgramPackets = tp->nDgramPackets;
7847                 peerStats->congestSeq = tp->congestSeq;
7848                 peerStats->bytesSent.high = tp->bytesSent.high;
7849                 peerStats->bytesSent.low = tp->bytesSent.low;
7850                 peerStats->bytesReceived.high = tp->bytesReceived.high;
7851                 peerStats->bytesReceived.low = tp->bytesReceived.low;
7852                 MUTEX_EXIT(&tp->peer_lock);
7853
7854                 MUTEX_ENTER(&rx_peerHashTable_lock);
7855                 tp->refCount--;
7856         }
7857         MUTEX_EXIT(&rx_peerHashTable_lock);
7858
7859         return error;
7860 }
7861
7862 void
7863 shutdown_rx(void)
7864 {
7865     struct rx_serverQueueEntry *np;
7866     int i, j;
7867 #ifndef KERNEL
7868     struct rx_call *call;
7869     struct rx_serverQueueEntry *sq;
7870 #endif /* KERNEL */
7871
7872     LOCK_RX_INIT;
7873     if (rxinit_status == 1) {
7874         UNLOCK_RX_INIT;
7875         return;                 /* Already shutdown. */
7876     }
7877 #ifndef KERNEL
7878     rx_port = 0;
7879 #ifndef AFS_PTHREAD_ENV
7880     FD_ZERO(&rx_selectMask);
7881 #endif /* AFS_PTHREAD_ENV */
7882     rxi_dataQuota = RX_MAX_QUOTA;
7883 #ifndef AFS_PTHREAD_ENV
7884     rxi_StopListener();
7885 #endif /* AFS_PTHREAD_ENV */
7886     shutdown_rxevent();
7887     rx_SetEpoch(0);
7888 #ifndef AFS_PTHREAD_ENV
7889 #ifndef AFS_USE_GETTIMEOFDAY
7890     clock_UnInit();
7891 #endif /* AFS_USE_GETTIMEOFDAY */
7892 #endif /* AFS_PTHREAD_ENV */
7893
7894     while (!queue_IsEmpty(&rx_freeCallQueue)) {
7895         call = queue_First(&rx_freeCallQueue, rx_call);
7896         queue_Remove(call);
7897         rxi_Free(call, sizeof(struct rx_call));
7898     }
7899
7900     while (!queue_IsEmpty(&rx_idleServerQueue)) {
7901         sq = queue_First(&rx_idleServerQueue, rx_serverQueueEntry);
7902         queue_Remove(sq);
7903     }
7904 #endif /* KERNEL */
7905
7906     {
7907         struct rx_peer **peer_ptr, **peer_end;
7908         for (peer_ptr = &rx_peerHashTable[0], peer_end =
7909              &rx_peerHashTable[rx_hashTableSize]; peer_ptr < peer_end;
7910              peer_ptr++) {
7911             struct rx_peer *peer, *next;
7912
7913             MUTEX_ENTER(&rx_peerHashTable_lock);
7914             for (peer = *peer_ptr; peer; peer = next) {
7915                 rx_interface_stat_p rpc_stat, nrpc_stat;
7916                 size_t space;
7917
7918                 MUTEX_ENTER(&rx_rpc_stats);
7919                 MUTEX_ENTER(&peer->peer_lock);
7920                 for (queue_Scan
7921                      (&peer->rpcStats, rpc_stat, nrpc_stat,
7922                       rx_interface_stat)) {
7923                     unsigned int num_funcs;
7924                     if (!rpc_stat)
7925                         break;
7926                     queue_Remove(&rpc_stat->queue_header);
7927                     queue_Remove(&rpc_stat->all_peers);
7928                     num_funcs = rpc_stat->stats[0].func_total;
7929                     space =
7930                         sizeof(rx_interface_stat_t) +
7931                         rpc_stat->stats[0].func_total *
7932                         sizeof(rx_function_entry_v1_t);
7933
7934                     rxi_Free(rpc_stat, space);
7935
7936                     /* rx_rpc_stats must be held */
7937                     rxi_rpc_peer_stat_cnt -= num_funcs;
7938                 }
7939                 MUTEX_EXIT(&peer->peer_lock);
7940                 MUTEX_EXIT(&rx_rpc_stats);
7941
7942                 next = peer->next;
7943                 rxi_FreePeer(peer);
7944                 if (rx_stats_active)
7945                     rx_atomic_dec(&rx_stats.nPeerStructs);
7946             }
7947             MUTEX_EXIT(&rx_peerHashTable_lock);
7948         }
7949     }
7950     for (i = 0; i < RX_MAX_SERVICES; i++) {
7951         if (rx_services[i])
7952             rxi_Free(rx_services[i], sizeof(*rx_services[i]));
7953     }
7954     for (i = 0; i < rx_hashTableSize; i++) {
7955         struct rx_connection *tc, *ntc;
7956         MUTEX_ENTER(&rx_connHashTable_lock);
7957         for (tc = rx_connHashTable[i]; tc; tc = ntc) {
7958             ntc = tc->next;
7959             for (j = 0; j < RX_MAXCALLS; j++) {
7960                 if (tc->call[j]) {
7961                     rxi_Free(tc->call[j], sizeof(*tc->call[j]));
7962                 }
7963             }
7964             rxi_Free(tc, sizeof(*tc));
7965         }
7966         MUTEX_EXIT(&rx_connHashTable_lock);
7967     }
7968
7969     MUTEX_ENTER(&freeSQEList_lock);
7970
7971     while ((np = rx_FreeSQEList)) {
7972         rx_FreeSQEList = *(struct rx_serverQueueEntry **)np;
7973         MUTEX_DESTROY(&np->lock);
7974         rxi_Free(np, sizeof(*np));
7975     }
7976
7977     MUTEX_EXIT(&freeSQEList_lock);
7978     MUTEX_DESTROY(&freeSQEList_lock);
7979     MUTEX_DESTROY(&rx_freeCallQueue_lock);
7980     MUTEX_DESTROY(&rx_connHashTable_lock);
7981     MUTEX_DESTROY(&rx_peerHashTable_lock);
7982     MUTEX_DESTROY(&rx_serverPool_lock);
7983
7984     osi_Free(rx_connHashTable,
7985              rx_hashTableSize * sizeof(struct rx_connection *));
7986     osi_Free(rx_peerHashTable, rx_hashTableSize * sizeof(struct rx_peer *));
7987
7988     UNPIN(rx_connHashTable,
7989           rx_hashTableSize * sizeof(struct rx_connection *));
7990     UNPIN(rx_peerHashTable, rx_hashTableSize * sizeof(struct rx_peer *));
7991
7992     rxi_FreeAllPackets();
7993
7994     MUTEX_ENTER(&rx_quota_mutex);
7995     rxi_dataQuota = RX_MAX_QUOTA;
7996     rxi_availProcs = rxi_totalMin = rxi_minDeficit = 0;
7997     MUTEX_EXIT(&rx_quota_mutex);
7998     rxinit_status = 1;
7999     UNLOCK_RX_INIT;
8000 }
8001
8002 #ifdef RX_ENABLE_LOCKS
8003 void
8004 osirx_AssertMine(afs_kmutex_t * lockaddr, char *msg)
8005 {
8006     if (!MUTEX_ISMINE(lockaddr))
8007         osi_Panic("Lock not held: %s", msg);
8008 }
8009 #endif /* RX_ENABLE_LOCKS */
8010
8011 #ifndef KERNEL
8012
8013 /*
8014  * Routines to implement connection specific data.
8015  */
8016
8017 int
8018 rx_KeyCreate(rx_destructor_t rtn)
8019 {
8020     int key;
8021     MUTEX_ENTER(&rxi_keyCreate_lock);
8022     key = rxi_keyCreate_counter++;
8023     rxi_keyCreate_destructor = (rx_destructor_t *)
8024         realloc((void *)rxi_keyCreate_destructor,
8025                 (key + 1) * sizeof(rx_destructor_t));
8026     rxi_keyCreate_destructor[key] = rtn;
8027     MUTEX_EXIT(&rxi_keyCreate_lock);
8028     return key;
8029 }
8030
8031 void
8032 rx_SetSpecific(struct rx_connection *conn, int key, void *ptr)
8033 {
8034     int i;
8035     MUTEX_ENTER(&conn->conn_data_lock);
8036     if (!conn->specific) {
8037         conn->specific = (void **)malloc((key + 1) * sizeof(void *));
8038         for (i = 0; i < key; i++)
8039             conn->specific[i] = NULL;
8040         conn->nSpecific = key + 1;
8041         conn->specific[key] = ptr;
8042     } else if (key >= conn->nSpecific) {
8043         conn->specific = (void **)
8044             realloc(conn->specific, (key + 1) * sizeof(void *));
8045         for (i = conn->nSpecific; i < key; i++)
8046             conn->specific[i] = NULL;
8047         conn->nSpecific = key + 1;
8048         conn->specific[key] = ptr;
8049     } else {
8050         if (conn->specific[key] && rxi_keyCreate_destructor[key])
8051             (*rxi_keyCreate_destructor[key]) (conn->specific[key]);
8052         conn->specific[key] = ptr;
8053     }
8054     MUTEX_EXIT(&conn->conn_data_lock);
8055 }
8056
8057 void
8058 rx_SetServiceSpecific(struct rx_service *svc, int key, void *ptr)
8059 {
8060     int i;
8061     MUTEX_ENTER(&svc->svc_data_lock);
8062     if (!svc->specific) {
8063         svc->specific = (void **)malloc((key + 1) * sizeof(void *));
8064         for (i = 0; i < key; i++)
8065             svc->specific[i] = NULL;
8066         svc->nSpecific = key + 1;
8067         svc->specific[key] = ptr;
8068     } else if (key >= svc->nSpecific) {
8069         svc->specific = (void **)
8070             realloc(svc->specific, (key + 1) * sizeof(void *));
8071         for (i = svc->nSpecific; i < key; i++)
8072             svc->specific[i] = NULL;
8073         svc->nSpecific = key + 1;
8074         svc->specific[key] = ptr;
8075     } else {
8076         if (svc->specific[key] && rxi_keyCreate_destructor[key])
8077             (*rxi_keyCreate_destructor[key]) (svc->specific[key]);
8078         svc->specific[key] = ptr;
8079     }
8080     MUTEX_EXIT(&svc->svc_data_lock);
8081 }
8082
8083 void *
8084 rx_GetSpecific(struct rx_connection *conn, int key)
8085 {
8086     void *ptr;
8087     MUTEX_ENTER(&conn->conn_data_lock);
8088     if (key >= conn->nSpecific)
8089         ptr = NULL;
8090     else
8091         ptr = conn->specific[key];
8092     MUTEX_EXIT(&conn->conn_data_lock);
8093     return ptr;
8094 }
8095
8096 void *
8097 rx_GetServiceSpecific(struct rx_service *svc, int key)
8098 {
8099     void *ptr;
8100     MUTEX_ENTER(&svc->svc_data_lock);
8101     if (key >= svc->nSpecific)
8102         ptr = NULL;
8103     else
8104         ptr = svc->specific[key];
8105     MUTEX_EXIT(&svc->svc_data_lock);
8106     return ptr;
8107 }
8108
8109
8110 #endif /* !KERNEL */
8111
8112 /*
8113  * processStats is a queue used to store the statistics for the local
8114  * process.  Its contents are similar to the contents of the rpcStats
8115  * queue on a rx_peer structure, but the actual data stored within
8116  * this queue contains totals across the lifetime of the process (assuming
8117  * the stats have not been reset) - unlike the per peer structures
8118  * which can come and go based upon the peer lifetime.
8119  */
8120
8121 static struct rx_queue processStats = { &processStats, &processStats };
8122
8123 /*
8124  * peerStats is a queue used to store the statistics for all peer structs.
8125  * Its contents are the union of all the peer rpcStats queues.
8126  */
8127
8128 static struct rx_queue peerStats = { &peerStats, &peerStats };
8129
8130 /*
8131  * rxi_monitor_processStats is used to turn process wide stat collection
8132  * on and off
8133  */
8134
8135 static int rxi_monitor_processStats = 0;
8136
8137 /*
8138  * rxi_monitor_peerStats is used to turn per peer stat collection on and off
8139  */
8140
8141 static int rxi_monitor_peerStats = 0;
8142
8143 /*
8144  * rxi_AddRpcStat - given all of the information for a particular rpc
8145  * call, create (if needed) and update the stat totals for the rpc.
8146  *
8147  * PARAMETERS
8148  *
8149  * IN stats - the queue of stats that will be updated with the new value
8150  *
8151  * IN rxInterface - a unique number that identifies the rpc interface
8152  *
8153  * IN currentFunc - the index of the function being invoked
8154  *
8155  * IN totalFunc - the total number of functions in this interface
8156  *
8157  * IN queueTime - the amount of time this function waited for a thread
8158  *
8159  * IN execTime - the amount of time this function invocation took to execute
8160  *
8161  * IN bytesSent - the number bytes sent by this invocation
8162  *
8163  * IN bytesRcvd - the number bytes received by this invocation
8164  *
8165  * IN isServer - if true, this invocation was made to a server
8166  *
8167  * IN remoteHost - the ip address of the remote host
8168  *
8169  * IN remotePort - the port of the remote host
8170  *
8171  * IN addToPeerList - if != 0, add newly created stat to the global peer list
8172  *
8173  * INOUT counter - if a new stats structure is allocated, the counter will
8174  * be updated with the new number of allocated stat structures
8175  *
8176  * RETURN CODES
8177  *
8178  * Returns void.
8179  */
8180
8181 static int
8182 rxi_AddRpcStat(struct rx_queue *stats, afs_uint32 rxInterface,
8183                afs_uint32 currentFunc, afs_uint32 totalFunc,
8184                struct clock *queueTime, struct clock *execTime,
8185                afs_hyper_t * bytesSent, afs_hyper_t * bytesRcvd, int isServer,
8186                afs_uint32 remoteHost, afs_uint32 remotePort,
8187                int addToPeerList, unsigned int *counter)
8188 {
8189     int rc = 0;
8190     rx_interface_stat_p rpc_stat, nrpc_stat;
8191
8192     /*
8193      * See if there's already a structure for this interface
8194      */
8195
8196     for (queue_Scan(stats, rpc_stat, nrpc_stat, rx_interface_stat)) {
8197         if ((rpc_stat->stats[0].interfaceId == rxInterface)
8198             && (rpc_stat->stats[0].remote_is_server == isServer))
8199             break;
8200     }
8201
8202     /*
8203      * Didn't find a match so allocate a new structure and add it to the
8204      * queue.
8205      */
8206
8207     if (queue_IsEnd(stats, rpc_stat) || (rpc_stat == NULL)
8208         || (rpc_stat->stats[0].interfaceId != rxInterface)
8209         || (rpc_stat->stats[0].remote_is_server != isServer)) {
8210         int i;
8211         size_t space;
8212
8213         space =
8214             sizeof(rx_interface_stat_t) +
8215             totalFunc * sizeof(rx_function_entry_v1_t);
8216
8217         rpc_stat = rxi_Alloc(space);
8218         if (rpc_stat == NULL) {
8219             rc = 1;
8220             goto fail;
8221         }
8222         *counter += totalFunc;
8223         for (i = 0; i < totalFunc; i++) {
8224             rpc_stat->stats[i].remote_peer = remoteHost;
8225             rpc_stat->stats[i].remote_port = remotePort;
8226             rpc_stat->stats[i].remote_is_server = isServer;
8227             rpc_stat->stats[i].interfaceId = rxInterface;
8228             rpc_stat->stats[i].func_total = totalFunc;
8229             rpc_stat->stats[i].func_index = i;
8230             hzero(rpc_stat->stats[i].invocations);
8231             hzero(rpc_stat->stats[i].bytes_sent);
8232             hzero(rpc_stat->stats[i].bytes_rcvd);
8233             rpc_stat->stats[i].queue_time_sum.sec = 0;
8234             rpc_stat->stats[i].queue_time_sum.usec = 0;
8235             rpc_stat->stats[i].queue_time_sum_sqr.sec = 0;
8236             rpc_stat->stats[i].queue_time_sum_sqr.usec = 0;
8237             rpc_stat->stats[i].queue_time_min.sec = 9999999;
8238             rpc_stat->stats[i].queue_time_min.usec = 9999999;
8239             rpc_stat->stats[i].queue_time_max.sec = 0;
8240             rpc_stat->stats[i].queue_time_max.usec = 0;
8241             rpc_stat->stats[i].execution_time_sum.sec = 0;
8242             rpc_stat->stats[i].execution_time_sum.usec = 0;
8243             rpc_stat->stats[i].execution_time_sum_sqr.sec = 0;
8244             rpc_stat->stats[i].execution_time_sum_sqr.usec = 0;
8245             rpc_stat->stats[i].execution_time_min.sec = 9999999;
8246             rpc_stat->stats[i].execution_time_min.usec = 9999999;
8247             rpc_stat->stats[i].execution_time_max.sec = 0;
8248             rpc_stat->stats[i].execution_time_max.usec = 0;
8249         }
8250         queue_Prepend(stats, rpc_stat);
8251         if (addToPeerList) {
8252             queue_Prepend(&peerStats, &rpc_stat->all_peers);
8253         }
8254     }
8255
8256     /*
8257      * Increment the stats for this function
8258      */
8259
8260     hadd32(rpc_stat->stats[currentFunc].invocations, 1);
8261     hadd(rpc_stat->stats[currentFunc].bytes_sent, *bytesSent);
8262     hadd(rpc_stat->stats[currentFunc].bytes_rcvd, *bytesRcvd);
8263     clock_Add(&rpc_stat->stats[currentFunc].queue_time_sum, queueTime);
8264     clock_AddSq(&rpc_stat->stats[currentFunc].queue_time_sum_sqr, queueTime);
8265     if (clock_Lt(queueTime, &rpc_stat->stats[currentFunc].queue_time_min)) {
8266         rpc_stat->stats[currentFunc].queue_time_min = *queueTime;
8267     }
8268     if (clock_Gt(queueTime, &rpc_stat->stats[currentFunc].queue_time_max)) {
8269         rpc_stat->stats[currentFunc].queue_time_max = *queueTime;
8270     }
8271     clock_Add(&rpc_stat->stats[currentFunc].execution_time_sum, execTime);
8272     clock_AddSq(&rpc_stat->stats[currentFunc].execution_time_sum_sqr,
8273                 execTime);
8274     if (clock_Lt(execTime, &rpc_stat->stats[currentFunc].execution_time_min)) {
8275         rpc_stat->stats[currentFunc].execution_time_min = *execTime;
8276     }
8277     if (clock_Gt(execTime, &rpc_stat->stats[currentFunc].execution_time_max)) {
8278         rpc_stat->stats[currentFunc].execution_time_max = *execTime;
8279     }
8280
8281   fail:
8282     return rc;
8283 }
8284
8285 /*
8286  * rx_IncrementTimeAndCount - increment the times and count for a particular
8287  * rpc function.
8288  *
8289  * PARAMETERS
8290  *
8291  * IN peer - the peer who invoked the rpc
8292  *
8293  * IN rxInterface - a unique number that identifies the rpc interface
8294  *
8295  * IN currentFunc - the index of the function being invoked
8296  *
8297  * IN totalFunc - the total number of functions in this interface
8298  *
8299  * IN queueTime - the amount of time this function waited for a thread
8300  *
8301  * IN execTime - the amount of time this function invocation took to execute
8302  *
8303  * IN bytesSent - the number bytes sent by this invocation
8304  *
8305  * IN bytesRcvd - the number bytes received by this invocation
8306  *
8307  * IN isServer - if true, this invocation was made to a server
8308  *
8309  * RETURN CODES
8310  *
8311  * Returns void.
8312  */
8313
8314 void
8315 rx_IncrementTimeAndCount(struct rx_peer *peer, afs_uint32 rxInterface,
8316                          afs_uint32 currentFunc, afs_uint32 totalFunc,
8317                          struct clock *queueTime, struct clock *execTime,
8318                          afs_hyper_t * bytesSent, afs_hyper_t * bytesRcvd,
8319                          int isServer)
8320 {
8321
8322     if (!(rxi_monitor_peerStats || rxi_monitor_processStats))
8323         return;
8324
8325     MUTEX_ENTER(&rx_rpc_stats);
8326
8327     if (rxi_monitor_peerStats) {
8328         MUTEX_ENTER(&peer->peer_lock);
8329         rxi_AddRpcStat(&peer->rpcStats, rxInterface, currentFunc, totalFunc,
8330                        queueTime, execTime, bytesSent, bytesRcvd, isServer,
8331                        peer->host, peer->port, 1, &rxi_rpc_peer_stat_cnt);
8332         MUTEX_EXIT(&peer->peer_lock);
8333     }
8334
8335     if (rxi_monitor_processStats) {
8336         rxi_AddRpcStat(&processStats, rxInterface, currentFunc, totalFunc,
8337                        queueTime, execTime, bytesSent, bytesRcvd, isServer,
8338                        0xffffffff, 0xffffffff, 0, &rxi_rpc_process_stat_cnt);
8339     }
8340
8341     MUTEX_EXIT(&rx_rpc_stats);
8342
8343 }
8344
8345 /*
8346  * rx_MarshallProcessRPCStats - marshall an array of rpc statistics
8347  *
8348  * PARAMETERS
8349  *
8350  * IN callerVersion - the rpc stat version of the caller.
8351  *
8352  * IN count - the number of entries to marshall.
8353  *
8354  * IN stats - pointer to stats to be marshalled.
8355  *
8356  * OUT ptr - Where to store the marshalled data.
8357  *
8358  * RETURN CODES
8359  *
8360  * Returns void.
8361  */
8362 void
8363 rx_MarshallProcessRPCStats(afs_uint32 callerVersion, int count,
8364                            rx_function_entry_v1_t * stats, afs_uint32 ** ptrP)
8365 {
8366     int i;
8367     afs_uint32 *ptr;
8368
8369     /*
8370      * We only support the first version
8371      */
8372     for (ptr = *ptrP, i = 0; i < count; i++, stats++) {
8373         *(ptr++) = stats->remote_peer;
8374         *(ptr++) = stats->remote_port;
8375         *(ptr++) = stats->remote_is_server;
8376         *(ptr++) = stats->interfaceId;
8377         *(ptr++) = stats->func_total;
8378         *(ptr++) = stats->func_index;
8379         *(ptr++) = hgethi(stats->invocations);
8380         *(ptr++) = hgetlo(stats->invocations);
8381         *(ptr++) = hgethi(stats->bytes_sent);
8382         *(ptr++) = hgetlo(stats->bytes_sent);
8383         *(ptr++) = hgethi(stats->bytes_rcvd);
8384         *(ptr++) = hgetlo(stats->bytes_rcvd);
8385         *(ptr++) = stats->queue_time_sum.sec;
8386         *(ptr++) = stats->queue_time_sum.usec;
8387         *(ptr++) = stats->queue_time_sum_sqr.sec;
8388         *(ptr++) = stats->queue_time_sum_sqr.usec;
8389         *(ptr++) = stats->queue_time_min.sec;
8390         *(ptr++) = stats->queue_time_min.usec;
8391         *(ptr++) = stats->queue_time_max.sec;
8392         *(ptr++) = stats->queue_time_max.usec;
8393         *(ptr++) = stats->execution_time_sum.sec;
8394         *(ptr++) = stats->execution_time_sum.usec;
8395         *(ptr++) = stats->execution_time_sum_sqr.sec;
8396         *(ptr++) = stats->execution_time_sum_sqr.usec;
8397         *(ptr++) = stats->execution_time_min.sec;
8398         *(ptr++) = stats->execution_time_min.usec;
8399         *(ptr++) = stats->execution_time_max.sec;
8400         *(ptr++) = stats->execution_time_max.usec;
8401     }
8402     *ptrP = ptr;
8403 }
8404
8405 /*
8406  * rx_RetrieveProcessRPCStats - retrieve all of the rpc statistics for
8407  * this process
8408  *
8409  * PARAMETERS
8410  *
8411  * IN callerVersion - the rpc stat version of the caller
8412  *
8413  * OUT myVersion - the rpc stat version of this function
8414  *
8415  * OUT clock_sec - local time seconds
8416  *
8417  * OUT clock_usec - local time microseconds
8418  *
8419  * OUT allocSize - the number of bytes allocated to contain stats
8420  *
8421  * OUT statCount - the number stats retrieved from this process.
8422  *
8423  * OUT stats - the actual stats retrieved from this process.
8424  *
8425  * RETURN CODES
8426  *
8427  * Returns void.  If successful, stats will != NULL.
8428  */
8429
8430 int
8431 rx_RetrieveProcessRPCStats(afs_uint32 callerVersion, afs_uint32 * myVersion,
8432                            afs_uint32 * clock_sec, afs_uint32 * clock_usec,
8433                            size_t * allocSize, afs_uint32 * statCount,
8434                            afs_uint32 ** stats)
8435 {
8436     size_t space = 0;
8437     afs_uint32 *ptr;
8438     struct clock now;
8439     int rc = 0;
8440
8441     *stats = 0;
8442     *allocSize = 0;
8443     *statCount = 0;
8444     *myVersion = RX_STATS_RETRIEVAL_VERSION;
8445
8446     /*
8447      * Check to see if stats are enabled
8448      */
8449
8450     MUTEX_ENTER(&rx_rpc_stats);
8451     if (!rxi_monitor_processStats) {
8452         MUTEX_EXIT(&rx_rpc_stats);
8453         return rc;
8454     }
8455
8456     clock_GetTime(&now);
8457     *clock_sec = now.sec;
8458     *clock_usec = now.usec;
8459
8460     /*
8461      * Allocate the space based upon the caller version
8462      *
8463      * If the client is at an older version than we are,
8464      * we return the statistic data in the older data format, but
8465      * we still return our version number so the client knows we
8466      * are maintaining more data than it can retrieve.
8467      */
8468
8469     if (callerVersion >= RX_STATS_RETRIEVAL_FIRST_EDITION) {
8470         space = rxi_rpc_process_stat_cnt * sizeof(rx_function_entry_v1_t);
8471         *statCount = rxi_rpc_process_stat_cnt;
8472     } else {
8473         /*
8474          * This can't happen yet, but in the future version changes
8475          * can be handled by adding additional code here
8476          */
8477     }
8478
8479     if (space > (size_t) 0) {
8480         *allocSize = space;
8481         ptr = *stats = rxi_Alloc(space);
8482
8483         if (ptr != NULL) {
8484             rx_interface_stat_p rpc_stat, nrpc_stat;
8485
8486
8487             for (queue_Scan
8488                  (&processStats, rpc_stat, nrpc_stat, rx_interface_stat)) {
8489                 /*
8490                  * Copy the data based upon the caller version
8491                  */
8492                 rx_MarshallProcessRPCStats(callerVersion,
8493                                            rpc_stat->stats[0].func_total,
8494                                            rpc_stat->stats, &ptr);
8495             }
8496         } else {
8497             rc = ENOMEM;
8498         }
8499     }
8500     MUTEX_EXIT(&rx_rpc_stats);
8501     return rc;
8502 }
8503
8504 /*
8505  * rx_RetrievePeerRPCStats - retrieve all of the rpc statistics for the peers
8506  *
8507  * PARAMETERS
8508  *
8509  * IN callerVersion - the rpc stat version of the caller
8510  *
8511  * OUT myVersion - the rpc stat version of this function
8512  *
8513  * OUT clock_sec - local time seconds
8514  *
8515  * OUT clock_usec - local time microseconds
8516  *
8517  * OUT allocSize - the number of bytes allocated to contain stats
8518  *
8519  * OUT statCount - the number of stats retrieved from the individual
8520  * peer structures.
8521  *
8522  * OUT stats - the actual stats retrieved from the individual peer structures.
8523  *
8524  * RETURN CODES
8525  *
8526  * Returns void.  If successful, stats will != NULL.
8527  */
8528
8529 int
8530 rx_RetrievePeerRPCStats(afs_uint32 callerVersion, afs_uint32 * myVersion,
8531                         afs_uint32 * clock_sec, afs_uint32 * clock_usec,
8532                         size_t * allocSize, afs_uint32 * statCount,
8533                         afs_uint32 ** stats)
8534 {
8535     size_t space = 0;
8536     afs_uint32 *ptr;
8537     struct clock now;
8538     int rc = 0;
8539
8540     *stats = 0;
8541     *statCount = 0;
8542     *allocSize = 0;
8543     *myVersion = RX_STATS_RETRIEVAL_VERSION;
8544
8545     /*
8546      * Check to see if stats are enabled
8547      */
8548
8549     MUTEX_ENTER(&rx_rpc_stats);
8550     if (!rxi_monitor_peerStats) {
8551         MUTEX_EXIT(&rx_rpc_stats);
8552         return rc;
8553     }
8554
8555     clock_GetTime(&now);
8556     *clock_sec = now.sec;
8557     *clock_usec = now.usec;
8558
8559     /*
8560      * Allocate the space based upon the caller version
8561      *
8562      * If the client is at an older version than we are,
8563      * we return the statistic data in the older data format, but
8564      * we still return our version number so the client knows we
8565      * are maintaining more data than it can retrieve.
8566      */
8567
8568     if (callerVersion >= RX_STATS_RETRIEVAL_FIRST_EDITION) {
8569         space = rxi_rpc_peer_stat_cnt * sizeof(rx_function_entry_v1_t);
8570         *statCount = rxi_rpc_peer_stat_cnt;
8571     } else {
8572         /*
8573          * This can't happen yet, but in the future version changes
8574          * can be handled by adding additional code here
8575          */
8576     }
8577
8578     if (space > (size_t) 0) {
8579         *allocSize = space;
8580         ptr = *stats = rxi_Alloc(space);
8581
8582         if (ptr != NULL) {
8583             rx_interface_stat_p rpc_stat, nrpc_stat;
8584             char *fix_offset;
8585
8586             for (queue_Scan
8587                  (&peerStats, rpc_stat, nrpc_stat, rx_interface_stat)) {
8588                 /*
8589                  * We have to fix the offset of rpc_stat since we are
8590                  * keeping this structure on two rx_queues.  The rx_queue
8591                  * package assumes that the rx_queue member is the first
8592                  * member of the structure.  That is, rx_queue assumes that
8593                  * any one item is only on one queue at a time.  We are
8594                  * breaking that assumption and so we have to do a little
8595                  * math to fix our pointers.
8596                  */
8597
8598                 fix_offset = (char *)rpc_stat;
8599                 fix_offset -= offsetof(rx_interface_stat_t, all_peers);
8600                 rpc_stat = (rx_interface_stat_p) fix_offset;
8601
8602                 /*
8603                  * Copy the data based upon the caller version
8604                  */
8605                 rx_MarshallProcessRPCStats(callerVersion,
8606                                            rpc_stat->stats[0].func_total,
8607                                            rpc_stat->stats, &ptr);
8608             }
8609         } else {
8610             rc = ENOMEM;
8611         }
8612     }
8613     MUTEX_EXIT(&rx_rpc_stats);
8614     return rc;
8615 }
8616
8617 /*
8618  * rx_FreeRPCStats - free memory allocated by
8619  *                   rx_RetrieveProcessRPCStats and rx_RetrievePeerRPCStats
8620  *
8621  * PARAMETERS
8622  *
8623  * IN stats - stats previously returned by rx_RetrieveProcessRPCStats or
8624  * rx_RetrievePeerRPCStats
8625  *
8626  * IN allocSize - the number of bytes in stats.
8627  *
8628  * RETURN CODES
8629  *
8630  * Returns void.
8631  */
8632
8633 void
8634 rx_FreeRPCStats(afs_uint32 * stats, size_t allocSize)
8635 {
8636     rxi_Free(stats, allocSize);
8637 }
8638
8639 /*
8640  * rx_queryProcessRPCStats - see if process rpc stat collection is
8641  * currently enabled.
8642  *
8643  * PARAMETERS
8644  *
8645  * RETURN CODES
8646  *
8647  * Returns 0 if stats are not enabled != 0 otherwise
8648  */
8649
8650 int
8651 rx_queryProcessRPCStats(void)
8652 {
8653     int rc;
8654     MUTEX_ENTER(&rx_rpc_stats);
8655     rc = rxi_monitor_processStats;
8656     MUTEX_EXIT(&rx_rpc_stats);
8657     return rc;
8658 }
8659
8660 /*
8661  * rx_queryPeerRPCStats - see if peer stat collection is currently enabled.
8662  *
8663  * PARAMETERS
8664  *
8665  * RETURN CODES
8666  *
8667  * Returns 0 if stats are not enabled != 0 otherwise
8668  */
8669
8670 int
8671 rx_queryPeerRPCStats(void)
8672 {
8673     int rc;
8674     MUTEX_ENTER(&rx_rpc_stats);
8675     rc = rxi_monitor_peerStats;
8676     MUTEX_EXIT(&rx_rpc_stats);
8677     return rc;
8678 }
8679
8680 /*
8681  * rx_enableProcessRPCStats - begin rpc stat collection for entire process
8682  *
8683  * PARAMETERS
8684  *
8685  * RETURN CODES
8686  *
8687  * Returns void.
8688  */
8689
8690 void
8691 rx_enableProcessRPCStats(void)
8692 {
8693     MUTEX_ENTER(&rx_rpc_stats);
8694     rx_enable_stats = 1;
8695     rxi_monitor_processStats = 1;
8696     MUTEX_EXIT(&rx_rpc_stats);
8697 }
8698
8699 /*
8700  * rx_enablePeerRPCStats - begin rpc stat collection per peer structure
8701  *
8702  * PARAMETERS
8703  *
8704  * RETURN CODES
8705  *
8706  * Returns void.
8707  */
8708
8709 void
8710 rx_enablePeerRPCStats(void)
8711 {
8712     MUTEX_ENTER(&rx_rpc_stats);
8713     rx_enable_stats = 1;
8714     rxi_monitor_peerStats = 1;
8715     MUTEX_EXIT(&rx_rpc_stats);
8716 }
8717
8718 /*
8719  * rx_disableProcessRPCStats - stop rpc stat collection for entire process
8720  *
8721  * PARAMETERS
8722  *
8723  * RETURN CODES
8724  *
8725  * Returns void.
8726  */
8727
8728 void
8729 rx_disableProcessRPCStats(void)
8730 {
8731     rx_interface_stat_p rpc_stat, nrpc_stat;
8732     size_t space;
8733
8734     MUTEX_ENTER(&rx_rpc_stats);
8735
8736     /*
8737      * Turn off process statistics and if peer stats is also off, turn
8738      * off everything
8739      */
8740
8741     rxi_monitor_processStats = 0;
8742     if (rxi_monitor_peerStats == 0) {
8743         rx_enable_stats = 0;
8744     }
8745
8746     for (queue_Scan(&processStats, rpc_stat, nrpc_stat, rx_interface_stat)) {
8747         unsigned int num_funcs = 0;
8748         if (!rpc_stat)
8749             break;
8750         queue_Remove(rpc_stat);
8751         num_funcs = rpc_stat->stats[0].func_total;
8752         space =
8753             sizeof(rx_interface_stat_t) +
8754             rpc_stat->stats[0].func_total * sizeof(rx_function_entry_v1_t);
8755
8756         rxi_Free(rpc_stat, space);
8757         rxi_rpc_process_stat_cnt -= num_funcs;
8758     }
8759     MUTEX_EXIT(&rx_rpc_stats);
8760 }
8761
8762 /*
8763  * rx_disablePeerRPCStats - stop rpc stat collection for peers
8764  *
8765  * PARAMETERS
8766  *
8767  * RETURN CODES
8768  *
8769  * Returns void.
8770  */
8771
8772 void
8773 rx_disablePeerRPCStats(void)
8774 {
8775     struct rx_peer **peer_ptr, **peer_end;
8776     int code;
8777
8778     /*
8779      * Turn off peer statistics and if process stats is also off, turn
8780      * off everything
8781      */
8782
8783     rxi_monitor_peerStats = 0;
8784     if (rxi_monitor_processStats == 0) {
8785         rx_enable_stats = 0;
8786     }
8787
8788     for (peer_ptr = &rx_peerHashTable[0], peer_end =
8789          &rx_peerHashTable[rx_hashTableSize]; peer_ptr < peer_end;
8790          peer_ptr++) {
8791         struct rx_peer *peer, *next, *prev;
8792
8793         MUTEX_ENTER(&rx_peerHashTable_lock);
8794         MUTEX_ENTER(&rx_rpc_stats);
8795         for (prev = peer = *peer_ptr; peer; peer = next) {
8796             next = peer->next;
8797             code = MUTEX_TRYENTER(&peer->peer_lock);
8798             if (code) {
8799                 rx_interface_stat_p rpc_stat, nrpc_stat;
8800                 size_t space;
8801
8802                 if (prev == *peer_ptr) {
8803                     *peer_ptr = next;
8804                     prev = next;
8805                 } else
8806                     prev->next = next;
8807
8808                 if (next)
8809                     next->refCount++;
8810                 if (prev)
8811                     prev->refCount++;
8812                 peer->refCount++;
8813                 MUTEX_EXIT(&rx_peerHashTable_lock);
8814
8815                 for (queue_Scan
8816                      (&peer->rpcStats, rpc_stat, nrpc_stat,
8817                       rx_interface_stat)) {
8818                     unsigned int num_funcs = 0;
8819                     if (!rpc_stat)
8820                         break;
8821                     queue_Remove(&rpc_stat->queue_header);
8822                     queue_Remove(&rpc_stat->all_peers);
8823                     num_funcs = rpc_stat->stats[0].func_total;
8824                     space =
8825                         sizeof(rx_interface_stat_t) +
8826                         rpc_stat->stats[0].func_total *
8827                         sizeof(rx_function_entry_v1_t);
8828
8829                     rxi_Free(rpc_stat, space);
8830                     rxi_rpc_peer_stat_cnt -= num_funcs;
8831                 }
8832                 MUTEX_EXIT(&peer->peer_lock);
8833
8834                 MUTEX_ENTER(&rx_peerHashTable_lock);
8835                 if (next)
8836                     next->refCount--;
8837                 if (prev)
8838                     prev->refCount--;
8839                 peer->refCount--;
8840             } else {
8841                 prev = peer;
8842             }
8843         }
8844         MUTEX_EXIT(&rx_rpc_stats);
8845         MUTEX_EXIT(&rx_peerHashTable_lock);
8846     }
8847 }
8848
8849 /*
8850  * rx_clearProcessRPCStats - clear the contents of the rpc stats according
8851  * to clearFlag
8852  *
8853  * PARAMETERS
8854  *
8855  * IN clearFlag - flag indicating which stats to clear
8856  *
8857  * RETURN CODES
8858  *
8859  * Returns void.
8860  */
8861
8862 void
8863 rx_clearProcessRPCStats(afs_uint32 clearFlag)
8864 {
8865     rx_interface_stat_p rpc_stat, nrpc_stat;
8866
8867     MUTEX_ENTER(&rx_rpc_stats);
8868
8869     for (queue_Scan(&processStats, rpc_stat, nrpc_stat, rx_interface_stat)) {
8870         unsigned int num_funcs = 0, i;
8871         num_funcs = rpc_stat->stats[0].func_total;
8872         for (i = 0; i < num_funcs; i++) {
8873             if (clearFlag & AFS_RX_STATS_CLEAR_INVOCATIONS) {
8874                 hzero(rpc_stat->stats[i].invocations);
8875             }
8876             if (clearFlag & AFS_RX_STATS_CLEAR_BYTES_SENT) {
8877                 hzero(rpc_stat->stats[i].bytes_sent);
8878             }
8879             if (clearFlag & AFS_RX_STATS_CLEAR_BYTES_RCVD) {
8880                 hzero(rpc_stat->stats[i].bytes_rcvd);
8881             }
8882             if (clearFlag & AFS_RX_STATS_CLEAR_QUEUE_TIME_SUM) {
8883                 rpc_stat->stats[i].queue_time_sum.sec = 0;
8884                 rpc_stat->stats[i].queue_time_sum.usec = 0;
8885             }
8886             if (clearFlag & AFS_RX_STATS_CLEAR_QUEUE_TIME_SQUARE) {
8887                 rpc_stat->stats[i].queue_time_sum_sqr.sec = 0;
8888                 rpc_stat->stats[i].queue_time_sum_sqr.usec = 0;
8889             }
8890             if (clearFlag & AFS_RX_STATS_CLEAR_QUEUE_TIME_MIN) {
8891                 rpc_stat->stats[i].queue_time_min.sec = 9999999;
8892                 rpc_stat->stats[i].queue_time_min.usec = 9999999;
8893             }
8894             if (clearFlag & AFS_RX_STATS_CLEAR_QUEUE_TIME_MAX) {
8895                 rpc_stat->stats[i].queue_time_max.sec = 0;
8896                 rpc_stat->stats[i].queue_time_max.usec = 0;
8897             }
8898             if (clearFlag & AFS_RX_STATS_CLEAR_EXEC_TIME_SUM) {
8899                 rpc_stat->stats[i].execution_time_sum.sec = 0;
8900                 rpc_stat->stats[i].execution_time_sum.usec = 0;
8901             }
8902             if (clearFlag & AFS_RX_STATS_CLEAR_EXEC_TIME_SQUARE) {
8903                 rpc_stat->stats[i].execution_time_sum_sqr.sec = 0;
8904                 rpc_stat->stats[i].execution_time_sum_sqr.usec = 0;
8905             }
8906             if (clearFlag & AFS_RX_STATS_CLEAR_EXEC_TIME_MIN) {
8907                 rpc_stat->stats[i].execution_time_min.sec = 9999999;
8908                 rpc_stat->stats[i].execution_time_min.usec = 9999999;
8909             }
8910             if (clearFlag & AFS_RX_STATS_CLEAR_EXEC_TIME_MAX) {
8911                 rpc_stat->stats[i].execution_time_max.sec = 0;
8912                 rpc_stat->stats[i].execution_time_max.usec = 0;
8913             }
8914         }
8915     }
8916
8917     MUTEX_EXIT(&rx_rpc_stats);
8918 }
8919
8920 /*
8921  * rx_clearPeerRPCStats - clear the contents of the rpc stats according
8922  * to clearFlag
8923  *
8924  * PARAMETERS
8925  *
8926  * IN clearFlag - flag indicating which stats to clear
8927  *
8928  * RETURN CODES
8929  *
8930  * Returns void.
8931  */
8932
8933 void
8934 rx_clearPeerRPCStats(afs_uint32 clearFlag)
8935 {
8936     rx_interface_stat_p rpc_stat, nrpc_stat;
8937
8938     MUTEX_ENTER(&rx_rpc_stats);
8939
8940     for (queue_Scan(&peerStats, rpc_stat, nrpc_stat, rx_interface_stat)) {
8941         unsigned int num_funcs = 0, i;
8942         char *fix_offset;
8943         /*
8944          * We have to fix the offset of rpc_stat since we are
8945          * keeping this structure on two rx_queues.  The rx_queue
8946          * package assumes that the rx_queue member is the first
8947          * member of the structure.  That is, rx_queue assumes that
8948          * any one item is only on one queue at a time.  We are
8949          * breaking that assumption and so we have to do a little
8950          * math to fix our pointers.
8951          */
8952
8953         fix_offset = (char *)rpc_stat;
8954         fix_offset -= offsetof(rx_interface_stat_t, all_peers);
8955         rpc_stat = (rx_interface_stat_p) fix_offset;
8956
8957         num_funcs = rpc_stat->stats[0].func_total;
8958         for (i = 0; i < num_funcs; i++) {
8959             if (clearFlag & AFS_RX_STATS_CLEAR_INVOCATIONS) {
8960                 hzero(rpc_stat->stats[i].invocations);
8961             }
8962             if (clearFlag & AFS_RX_STATS_CLEAR_BYTES_SENT) {
8963                 hzero(rpc_stat->stats[i].bytes_sent);
8964             }
8965             if (clearFlag & AFS_RX_STATS_CLEAR_BYTES_RCVD) {
8966                 hzero(rpc_stat->stats[i].bytes_rcvd);
8967             }
8968             if (clearFlag & AFS_RX_STATS_CLEAR_QUEUE_TIME_SUM) {
8969                 rpc_stat->stats[i].queue_time_sum.sec = 0;
8970                 rpc_stat->stats[i].queue_time_sum.usec = 0;
8971             }
8972             if (clearFlag & AFS_RX_STATS_CLEAR_QUEUE_TIME_SQUARE) {
8973                 rpc_stat->stats[i].queue_time_sum_sqr.sec = 0;
8974                 rpc_stat->stats[i].queue_time_sum_sqr.usec = 0;
8975             }
8976             if (clearFlag & AFS_RX_STATS_CLEAR_QUEUE_TIME_MIN) {
8977                 rpc_stat->stats[i].queue_time_min.sec = 9999999;
8978                 rpc_stat->stats[i].queue_time_min.usec = 9999999;
8979             }
8980             if (clearFlag & AFS_RX_STATS_CLEAR_QUEUE_TIME_MAX) {
8981                 rpc_stat->stats[i].queue_time_max.sec = 0;
8982                 rpc_stat->stats[i].queue_time_max.usec = 0;
8983             }
8984             if (clearFlag & AFS_RX_STATS_CLEAR_EXEC_TIME_SUM) {
8985                 rpc_stat->stats[i].execution_time_sum.sec = 0;
8986                 rpc_stat->stats[i].execution_time_sum.usec = 0;
8987             }
8988             if (clearFlag & AFS_RX_STATS_CLEAR_EXEC_TIME_SQUARE) {
8989                 rpc_stat->stats[i].execution_time_sum_sqr.sec = 0;
8990                 rpc_stat->stats[i].execution_time_sum_sqr.usec = 0;
8991             }
8992             if (clearFlag & AFS_RX_STATS_CLEAR_EXEC_TIME_MIN) {
8993                 rpc_stat->stats[i].execution_time_min.sec = 9999999;
8994                 rpc_stat->stats[i].execution_time_min.usec = 9999999;
8995             }
8996             if (clearFlag & AFS_RX_STATS_CLEAR_EXEC_TIME_MAX) {
8997                 rpc_stat->stats[i].execution_time_max.sec = 0;
8998                 rpc_stat->stats[i].execution_time_max.usec = 0;
8999             }
9000         }
9001     }
9002
9003     MUTEX_EXIT(&rx_rpc_stats);
9004 }
9005
9006 /*
9007  * rxi_rxstat_userok points to a routine that returns 1 if the caller
9008  * is authorized to enable/disable/clear RX statistics.
9009  */
9010 static int (*rxi_rxstat_userok) (struct rx_call * call) = NULL;
9011
9012 void
9013 rx_SetRxStatUserOk(int (*proc) (struct rx_call * call))
9014 {
9015     rxi_rxstat_userok = proc;
9016 }
9017
9018 int
9019 rx_RxStatUserOk(struct rx_call *call)
9020 {
9021     if (!rxi_rxstat_userok)
9022         return 0;
9023     return rxi_rxstat_userok(call);
9024 }
9025
9026 #ifdef AFS_NT40_ENV
9027 /*
9028  * DllMain() -- Entry-point function called by the DllMainCRTStartup()
9029  *     function in the MSVC runtime DLL (msvcrt.dll).
9030  *
9031  *     Note: the system serializes calls to this function.
9032  */
9033 BOOL WINAPI
9034 DllMain(HINSTANCE dllInstHandle,        /* instance handle for this DLL module */
9035         DWORD reason,                   /* reason function is being called */
9036         LPVOID reserved)                /* reserved for future use */
9037 {
9038     switch (reason) {
9039     case DLL_PROCESS_ATTACH:
9040         /* library is being attached to a process */
9041         INIT_PTHREAD_LOCKS;
9042         return TRUE;
9043
9044     case DLL_PROCESS_DETACH:
9045         return TRUE;
9046
9047     default:
9048         return FALSE;
9049     }
9050 }
9051 #endif /* AFS_NT40_ENV */
9052
9053 #ifndef KERNEL
9054 int rx_DumpCalls(FILE *outputFile, char *cookie)
9055 {
9056 #ifdef RXDEBUG_PACKET
9057 #ifdef KDUMP_RX_LOCK
9058     struct rx_call_rx_lock *c;
9059 #else
9060     struct rx_call *c;
9061 #endif
9062 #ifdef AFS_NT40_ENV
9063     int zilch;
9064     char output[2048];
9065 #define RXDPRINTF sprintf
9066 #define RXDPRINTOUT output
9067 #else
9068 #define RXDPRINTF fprintf
9069 #define RXDPRINTOUT outputFile
9070 #endif
9071
9072     RXDPRINTF(RXDPRINTOUT, "%s - Start dumping all Rx Calls - count=%u\r\n", cookie, rx_stats.nCallStructs);
9073 #ifdef AFS_NT40_ENV
9074     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
9075 #endif
9076
9077     for (c = rx_allCallsp; c; c = c->allNextp) {
9078         u_short rqc, tqc, iovqc;
9079         struct rx_packet *p, *np;
9080
9081         MUTEX_ENTER(&c->lock);
9082         queue_Count(&c->rq, p, np, rx_packet, rqc);
9083         queue_Count(&c->tq, p, np, rx_packet, tqc);
9084         queue_Count(&c->iovq, p, np, rx_packet, iovqc);
9085
9086         RXDPRINTF(RXDPRINTOUT, "%s - call=0x%p, id=%u, state=%u, mode=%u, conn=%p, epoch=%u, cid=%u, callNum=%u, connFlags=0x%x, flags=0x%x, "
9087                 "rqc=%u,%u, tqc=%u,%u, iovqc=%u,%u, "
9088                 "lstatus=%u, rstatus=%u, error=%d, timeout=%u, "
9089                 "resendEvent=%d, timeoutEvt=%d, keepAliveEvt=%d, delayedAckEvt=%d, delayedAbortEvt=%d, abortCode=%d, abortCount=%d, "
9090                 "lastSendTime=%u, lastRecvTime=%u, lastSendData=%u"
9091 #ifdef RX_ENABLE_LOCKS
9092                 ", refCount=%u"
9093 #endif
9094 #ifdef RX_REFCOUNT_CHECK
9095                 ", refCountBegin=%u, refCountResend=%u, refCountDelay=%u, "
9096                 "refCountAlive=%u, refCountPacket=%u, refCountSend=%u, refCountAckAll=%u, refCountAbort=%u"
9097 #endif
9098                 "\r\n",
9099                 cookie, c, c->call_id, (afs_uint32)c->state, (afs_uint32)c->mode, c->conn, c->conn?c->conn->epoch:0, c->conn?c->conn->cid:0,
9100                 c->callNumber?*c->callNumber:0, c->conn?c->conn->flags:0, c->flags,
9101                 (afs_uint32)c->rqc, (afs_uint32)rqc, (afs_uint32)c->tqc, (afs_uint32)tqc, (afs_uint32)c->iovqc, (afs_uint32)iovqc,
9102                 (afs_uint32)c->localStatus, (afs_uint32)c->remoteStatus, c->error, c->timeout,
9103                 c->resendEvent?1:0, c->timeoutEvent?1:0, c->keepAliveEvent?1:0, c->delayedAckEvent?1:0, c->delayedAbortEvent?1:0,
9104                 c->abortCode, c->abortCount, c->lastSendTime, c->lastReceiveTime, c->lastSendData
9105 #ifdef RX_ENABLE_LOCKS
9106                 , (afs_uint32)c->refCount
9107 #endif
9108 #ifdef RX_REFCOUNT_CHECK
9109                 , c->refCDebug[0],c->refCDebug[1],c->refCDebug[2],c->refCDebug[3],c->refCDebug[4],c->refCDebug[5],c->refCDebug[6],c->refCDebug[7]
9110 #endif
9111                 );
9112         MUTEX_EXIT(&c->lock);
9113
9114 #ifdef AFS_NT40_ENV
9115         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
9116 #endif
9117     }
9118     RXDPRINTF(RXDPRINTOUT, "%s - End dumping all Rx Calls\r\n", cookie);
9119 #ifdef AFS_NT40_ENV
9120     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
9121 #endif
9122 #endif /* RXDEBUG_PACKET */
9123     return 0;
9124 }
9125 #endif