rx: initialize mutexes from globals
[openafs.git] / src / rx / rx_globals.h
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:  Globals for internal use, basically */
11
12 #ifndef AFS_RX_GLOBALS_H
13 #define AFS_RX_GLOBALS_H
14
15
16 #ifdef  KERNEL
17 #include "rx/rx.h"
18 #else /* KERNEL */
19 # include "rx.h"
20 #endif /* KERNEL */
21
22 #ifndef GLOBALSINIT
23 #define GLOBALSINIT(x)
24 #define POSTAMBLE
25 #if defined(AFS_NT40_ENV)
26 #define RX_STATS_INTERLOCKED 1
27 #if defined(AFS_PTHREAD_ENV)
28 #define EXT __declspec(dllimport) extern
29 #else /* AFS_PTHREAD_ENV */
30 #define EXT extern
31 #endif /* AFS_PTHREAD_ENV */
32 #else /* AFS_NT40_ENV */
33 #define EXT extern
34 #endif /* AFS_NT40_ENV */
35 #endif /* !GLOBALSINIT */
36
37 /* Basic socket for client requests; other sockets (for receiving server requests) are in the service structures */
38 EXT osi_socket rx_socket;
39
40 /* The array of installed services.  Null terminated. */
41 EXT struct rx_service *rx_services[RX_MAX_SERVICES + 1];
42 #ifdef RX_ENABLE_LOCKS
43 /* Protects nRequestsRunning as well as pool allocation variables. */
44 EXT afs_kmutex_t rx_serverPool_lock;
45 #endif /* RX_ENABLE_LOCKS */
46
47 /* Incoming calls wait on this queue when there are no available server processes */
48 EXT struct rx_queue rx_incomingCallQueue;
49
50 /* Server processes wait on this queue when there are no appropriate calls to process */
51 EXT struct rx_queue rx_idleServerQueue;
52
53 /* Constant delay time before sending a hard ack if the receiver consumes
54  * a packet while no delayed ack event is scheduled. Ensures that the
55  * sender is able to advance its window when the receiver consumes a packet
56  * after the sender has exhausted its transmit window.
57  */
58 EXT struct clock rx_hardAckDelay;
59
60 #if defined(RXDEBUG) || defined(AFS_NT40_ENV)
61 /* Variable to allow introduction of network unreliability; exported from libafsrpc */
62 EXT int rx_intentionallyDroppedPacketsPer100 GLOBALSINIT(0);    /* Dropped on Send */
63 EXT int rx_intentionallyDroppedOnReadPer100  GLOBALSINIT(0);    /* Dropped on Read */
64 #endif
65
66 /* extra packets to add to the quota */
67 EXT int rx_extraQuota GLOBALSINIT(0);
68 /* extra packets to alloc (2 * maxWindowSize by default) */
69 EXT int rx_extraPackets GLOBALSINIT(256);
70
71 EXT int rx_stackSize GLOBALSINIT(RX_DEFAULT_STACK_SIZE);
72
73 /* Time until an unresponsive connection is declared dead */
74 EXT int rx_connDeadTime GLOBALSINIT(12);
75
76 /* Set rx default connection dead time; set on both services and connections at creation time */
77 #ifdef AFS_NT40_ENV
78 void rx_SetRxDeadTime(int seconds);
79 #else
80 #define rx_SetRxDeadTime(seconds)   (rx_connDeadTime = (seconds))
81 #endif
82
83 /* Time until we toss an idle connection */
84 EXT int rx_idleConnectionTime GLOBALSINIT(700);
85 /* Time until we toss a peer structure, after all connections using are gone */
86 EXT int rx_idlePeerTime GLOBALSINIT(60);
87
88 /* The file server is temporarily salvaging */
89 EXT int rx_tranquil GLOBALSINIT(0);
90
91 /* UDP rcv buffer size */
92 EXT int rx_UdpBufSize GLOBALSINIT(64 * 1024);
93 #ifdef AFS_NT40_ENV
94 int   rx_GetMinUdpBufSize(void);
95 void  rx_SetUdpBufSize(int x);
96 #else
97 #define rx_GetMinUdpBufSize()   (64*1024)
98 #define rx_SetUdpBufSize(x)     (((x)>rx_GetMinUdpBufSize()) ? (rx_UdpBufSize = (x)):0)
99 #endif
100 /*
101  * Variables to control RX overload management. When the number of calls
102  * waiting for a thread exceed the threshold, new calls are aborted
103  * with the busy error.
104  */
105 EXT int rx_BusyThreshold GLOBALSINIT(-1);       /* default is disabled */
106 EXT int rx_BusyError GLOBALSINIT(-1);
107
108 /* These definitions should be in one place */
109 #ifdef  AFS_SUN5_ENV
110 #define RX_CBUF_TIME    180     /* Check for packet deficit */
111 #define RX_REAP_TIME    90      /* Check for tossable connections every 90 seconds */
112 #else
113 #define RX_CBUF_TIME    120     /* Check for packet deficit */
114 #define RX_REAP_TIME    60      /* Check for tossable connections every 60 seconds */
115 #endif
116
117 #define RX_FAST_ACK_RATE 1      /* as of 3.4, ask for an ack every
118                                  * other packet. */
119
120 EXT int rx_minPeerTimeout GLOBALSINIT(20);      /* in milliseconds */
121 EXT int rx_minWindow GLOBALSINIT(1);
122 EXT int rx_maxWindow GLOBALSINIT(RX_MAXACKS);   /* must ack what we receive */
123 EXT int rx_initReceiveWindow GLOBALSINIT(16);   /* how much to accept */
124 EXT int rx_maxReceiveWindow GLOBALSINIT(32);    /* how much to accept */
125 EXT int rx_initSendWindow GLOBALSINIT(16);
126 EXT int rx_maxSendWindow GLOBALSINIT(32);
127 EXT int rx_nackThreshold GLOBALSINIT(3);        /* Number NACKS to trigger congestion recovery */
128 EXT int rx_nDgramThreshold GLOBALSINIT(4);      /* Number of packets before increasing
129                                                  * packets per datagram */
130 #define RX_MAX_FRAGS 4
131 EXT int rxi_nSendFrags GLOBALSINIT(RX_MAX_FRAGS);       /* max fragments in a datagram */
132 EXT int rxi_nRecvFrags GLOBALSINIT(RX_MAX_FRAGS);
133 EXT int rxi_OrphanFragSize GLOBALSINIT(512);
134
135 #define RX_MAX_DGRAM_PACKETS 6  /* max packets per jumbogram */
136
137 EXT int rxi_nDgramPackets GLOBALSINIT(RX_MAX_DGRAM_PACKETS);
138 /* allow n packets between soft acks */
139 EXT int rxi_SoftAckRate GLOBALSINIT(RX_FAST_ACK_RATE);
140 /* consume n packets before sending hard ack, should be larger than above,
141    but not absolutely necessary.  If it's smaller, than fast receivers will
142    send a soft ack, immediately followed by a hard ack. */
143 EXT int rxi_HardAckRate GLOBALSINIT(RX_FAST_ACK_RATE + 1);
144
145 EXT int rx_nPackets GLOBALSINIT(0);     /* preallocate packets with rx_extraPackets */
146
147 /*
148  * pthreads thread-specific rx info support
149  * the rx_ts_info_t struct is meant to support all kinds of
150  * thread-specific rx data:
151  *
152  *  _FPQ member contains a thread-specific free packet queue
153  */
154 #ifdef AFS_PTHREAD_ENV
155 EXT pthread_key_t rx_ts_info_key;
156 typedef struct rx_ts_info_t {
157     struct {
158         struct rx_queue queue;
159         int len;                /* local queue length */
160         int delta;              /* number of new packets alloc'd locally since last sync w/ global queue */
161
162         /* FPQ stats */
163         int checkin_ops;
164         int checkin_xfer;
165         int checkout_ops;
166         int checkout_xfer;
167         int gtol_ops;
168         int gtol_xfer;
169         int ltog_ops;
170         int ltog_xfer;
171         int lalloc_ops;
172         int lalloc_xfer;
173         int galloc_ops;
174         int galloc_xfer;
175     } _FPQ;
176     struct rx_packet * local_special_packet;
177 } rx_ts_info_t;
178 EXT struct rx_ts_info_t * rx_ts_info_init(void);   /* init function for thread-specific data struct */
179 #define RX_TS_INFO_GET(ts_info_p) \
180     do { \
181         ts_info_p = (struct rx_ts_info_t*)pthread_getspecific(rx_ts_info_key); \
182         if (ts_info_p == NULL) { \
183             opr_Verify((ts_info_p = rx_ts_info_init()) != NULL); \
184         } \
185     } while(0)
186 #endif /* AFS_PTHREAD_ENV */
187
188
189 /* List of free packets */
190 /* in pthreads rx, free packet queue is now a two-tiered queueing system
191  * in which the first tier is thread-specific, and the second tier is
192  * a global free packet queue */
193 EXT struct rx_queue rx_freePacketQueue;
194 #ifdef RX_TRACK_PACKETS
195 #define RX_FPQ_MARK_FREE(p) \
196     do { \
197         if ((p)->flags & RX_PKTFLAG_FREE) \
198             osi_Panic("rx packet already free\n"); \
199         (p)->flags |= RX_PKTFLAG_FREE; \
200         (p)->flags &= ~(RX_PKTFLAG_TQ|RX_PKTFLAG_IOVQ|RX_PKTFLAG_RQ|RX_PKTFLAG_CP); \
201         (p)->length = 0; \
202         (p)->niovecs = 0; \
203     } while(0)
204 #define RX_FPQ_MARK_USED(p) \
205     do { \
206         if (!((p)->flags & RX_PKTFLAG_FREE)) \
207             osi_Panic("rx packet not free\n"); \
208         (p)->flags = 0;         /* clear RX_PKTFLAG_FREE, initialize the rest */ \
209         (p)->header.flags = 0; \
210     } while(0)
211 #else
212 #define RX_FPQ_MARK_FREE(p) \
213     do { \
214         (p)->length = 0; \
215         (p)->niovecs = 0; \
216     } while(0)
217 #define RX_FPQ_MARK_USED(p) \
218     do { \
219         (p)->flags = 0;         /* clear RX_PKTFLAG_FREE, initialize the rest */ \
220         (p)->header.flags = 0; \
221     } while(0)
222 #endif
223 #define RX_PACKET_IOV_INIT(p) \
224     do { \
225         (p)->wirevec[0].iov_base = (char *)((p)->wirehead); \
226         (p)->wirevec[0].iov_len = RX_HEADER_SIZE; \
227         (p)->wirevec[1].iov_base = (char *)((p)->localdata); \
228         (p)->wirevec[1].iov_len = RX_FIRSTBUFFERSIZE; \
229     } while(0)
230 #define RX_PACKET_IOV_FULLINIT(p) \
231     do { \
232         (p)->wirevec[0].iov_base = (char *)((p)->wirehead); \
233         (p)->wirevec[0].iov_len = RX_HEADER_SIZE; \
234         (p)->wirevec[1].iov_base = (char *)((p)->localdata); \
235         (p)->wirevec[1].iov_len = RX_FIRSTBUFFERSIZE; \
236         (p)->niovecs = 2; \
237         (p)->length = RX_FIRSTBUFFERSIZE; \
238     } while(0)
239
240 #ifdef RX_ENABLE_LOCKS
241 EXT afs_kmutex_t rx_freePktQ_lock;
242 #endif /* RX_ENABLE_LOCKS */
243
244 #if defined(AFS_PTHREAD_ENV)
245 #define RX_ENABLE_TSFPQ
246 EXT int rx_TSFPQGlobSize GLOBALSINIT(3); /* number of packets to transfer between global and local queues in one op */
247 EXT int rx_TSFPQLocalMax GLOBALSINIT(15); /* max number of packets on local FPQ before returning a glob to the global pool */
248 EXT int rx_TSFPQMaxProcs GLOBALSINIT(0); /* max number of threads expected */
249 #define RX_TS_FPQ_FLUSH_GLOBAL 1
250 #define RX_TS_FPQ_PULL_GLOBAL 1
251 #define RX_TS_FPQ_ALLOW_OVERCOMMIT 1
252 /*
253  * compute the localmax and globsize values from rx_TSFPQMaxProcs and rx_nPackets.
254  * arbitarily set local max so that all threads consume 90% of packets, if all local queues are full.
255  * arbitarily set transfer glob size to 20% of max local packet queue length.
256  * also set minimum values of 15 and 3.  Given the algorithms, the number of buffers allocated
257  * by each call to AllocPacketBufs() will increase indefinitely without a cap on the transfer
258  * glob size.  A cap of 64 is selected because that will produce an allocation of greater than
259  * three times that amount which is greater than half of ncalls * maxReceiveWindow.
260  * Must be called under rx_packets_mutex.
261  */
262 #define RX_TS_FPQ_COMPUTE_LIMITS \
263     do { \
264         int newmax, newglob; \
265         newmax = (rx_nPackets * 9) / (10 * rx_TSFPQMaxProcs); \
266         newmax = (newmax >= 15) ? newmax : 15; \
267         newglob = newmax / 5; \
268         newglob = (newglob >= 3) ? (newglob < 64 ? newglob : 64) : 3; \
269         rx_TSFPQLocalMax = newmax; \
270         rx_TSFPQGlobSize = newglob; \
271     } while(0)
272 /* record the number of packets allocated by this thread
273  * and stored in the thread local queue */
274 #define RX_TS_FPQ_LOCAL_ALLOC(rx_ts_info_p,num_alloc) \
275     do { \
276         (rx_ts_info_p)->_FPQ.lalloc_ops++; \
277         (rx_ts_info_p)->_FPQ.lalloc_xfer += num_alloc; \
278     } while (0)
279 /* record the number of packets allocated by this thread
280  * and stored in the global queue */
281 #define RX_TS_FPQ_GLOBAL_ALLOC(rx_ts_info_p,num_alloc) \
282     do { \
283         (rx_ts_info_p)->_FPQ.galloc_ops++; \
284         (rx_ts_info_p)->_FPQ.galloc_xfer += num_alloc; \
285     } while (0)
286 /* move packets from local (thread-specific) to global free packet queue.
287    rx_freePktQ_lock must be held. default is to reduce the queue size to 40% ofmax */
288 #define RX_TS_FPQ_LTOG(rx_ts_info_p) \
289     do { \
290         int i; \
291         struct rx_packet * p; \
292         int tsize = MIN((rx_ts_info_p)->_FPQ.len, (rx_ts_info_p)->_FPQ.len - rx_TSFPQLocalMax + 3 *  rx_TSFPQGlobSize); \
293         if (tsize <= 0) break; \
294         for (i=0,p=queue_Last(&((rx_ts_info_p)->_FPQ), rx_packet); \
295              i < tsize; i++,p=queue_Prev(p, rx_packet)); \
296         queue_SplitAfterPrepend(&((rx_ts_info_p)->_FPQ),&rx_freePacketQueue,p); \
297         (rx_ts_info_p)->_FPQ.len -= tsize; \
298         rx_nFreePackets += tsize; \
299         (rx_ts_info_p)->_FPQ.ltog_ops++; \
300         (rx_ts_info_p)->_FPQ.ltog_xfer += tsize; \
301         if ((rx_ts_info_p)->_FPQ.delta) { \
302             MUTEX_ENTER(&rx_packets_mutex); \
303             RX_TS_FPQ_COMPUTE_LIMITS; \
304             MUTEX_EXIT(&rx_packets_mutex); \
305            (rx_ts_info_p)->_FPQ.delta = 0; \
306         } \
307     } while(0)
308 /* same as above, except user has direct control over number to transfer */
309 #define RX_TS_FPQ_LTOG2(rx_ts_info_p,num_transfer) \
310     do { \
311         int i; \
312         struct rx_packet * p; \
313         if (num_transfer <= 0) break; \
314         for (i=0,p=queue_Last(&((rx_ts_info_p)->_FPQ), rx_packet); \
315              i < (num_transfer); i++,p=queue_Prev(p, rx_packet)); \
316         queue_SplitAfterPrepend(&((rx_ts_info_p)->_FPQ),&rx_freePacketQueue,p); \
317         (rx_ts_info_p)->_FPQ.len -= (num_transfer); \
318         rx_nFreePackets += (num_transfer); \
319         (rx_ts_info_p)->_FPQ.ltog_ops++; \
320         (rx_ts_info_p)->_FPQ.ltog_xfer += (num_transfer); \
321         if ((rx_ts_info_p)->_FPQ.delta) { \
322             MUTEX_ENTER(&rx_packets_mutex); \
323             RX_TS_FPQ_COMPUTE_LIMITS; \
324             MUTEX_EXIT(&rx_packets_mutex); \
325             (rx_ts_info_p)->_FPQ.delta = 0; \
326         } \
327     } while(0)
328 /* move packets from global to local (thread-specific) free packet queue.
329    rx_freePktQ_lock must be held. */
330 #define RX_TS_FPQ_GTOL(rx_ts_info_p) \
331     do { \
332         int i, tsize; \
333         struct rx_packet * p; \
334         tsize = (rx_TSFPQGlobSize <= rx_nFreePackets) ? \
335                  rx_TSFPQGlobSize : rx_nFreePackets; \
336         for (i=0,p=queue_First(&rx_freePacketQueue, rx_packet); \
337              i < tsize; i++,p=queue_Next(p, rx_packet)); \
338         queue_SplitBeforeAppend(&rx_freePacketQueue,&((rx_ts_info_p)->_FPQ),p); \
339         (rx_ts_info_p)->_FPQ.len += i; \
340         rx_nFreePackets -= i; \
341         (rx_ts_info_p)->_FPQ.gtol_ops++; \
342         (rx_ts_info_p)->_FPQ.gtol_xfer += i; \
343     } while(0)
344 /* same as above, except user has direct control over number to transfer */
345 #define RX_TS_FPQ_GTOL2(rx_ts_info_p,num_transfer) \
346     do { \
347         int i, tsize; \
348         struct rx_packet * p; \
349         tsize = (num_transfer); \
350         if (tsize > rx_nFreePackets) tsize = rx_nFreePackets; \
351         for (i=0,p=queue_First(&rx_freePacketQueue, rx_packet); \
352              i < tsize; i++,p=queue_Next(p, rx_packet)); \
353         queue_SplitBeforeAppend(&rx_freePacketQueue,&((rx_ts_info_p)->_FPQ),p); \
354         (rx_ts_info_p)->_FPQ.len += i; \
355         rx_nFreePackets -= i; \
356         (rx_ts_info_p)->_FPQ.gtol_ops++; \
357         (rx_ts_info_p)->_FPQ.gtol_xfer += i; \
358     } while(0)
359 /* checkout a packet from the thread-specific free packet queue */
360 #define RX_TS_FPQ_CHECKOUT(rx_ts_info_p,p) \
361     do { \
362         (p) = queue_First(&((rx_ts_info_p)->_FPQ), rx_packet); \
363         queue_Remove(p); \
364         RX_FPQ_MARK_USED(p); \
365         (rx_ts_info_p)->_FPQ.len--; \
366         (rx_ts_info_p)->_FPQ.checkout_ops++; \
367         (rx_ts_info_p)->_FPQ.checkout_xfer++; \
368     } while(0)
369 /* checkout multiple packets from the thread-specific free packet queue.
370  * num_transfer must be a variable.
371  */
372 #define RX_TS_FPQ_QCHECKOUT(rx_ts_info_p,num_transfer,q) \
373     do { \
374         int i; \
375         struct rx_packet *p; \
376         if (num_transfer > (rx_ts_info_p)->_FPQ.len) num_transfer = (rx_ts_info_p)->_FPQ.len; \
377         for (i=0, p=queue_First(&((rx_ts_info_p)->_FPQ), rx_packet); \
378              i < num_transfer; \
379              i++, p=queue_Next(p, rx_packet)) { \
380             RX_FPQ_MARK_USED(p); \
381         } \
382         queue_SplitBeforeAppend(&((rx_ts_info_p)->_FPQ),(q),p); \
383         (rx_ts_info_p)->_FPQ.len -= num_transfer; \
384         (rx_ts_info_p)->_FPQ.checkout_ops++; \
385         (rx_ts_info_p)->_FPQ.checkout_xfer += num_transfer; \
386     } while(0)
387 /* check a packet into the thread-specific free packet queue */
388 #define RX_TS_FPQ_CHECKIN(rx_ts_info_p,p) \
389     do { \
390         queue_Prepend(&((rx_ts_info_p)->_FPQ), (p)); \
391         RX_FPQ_MARK_FREE(p); \
392         (rx_ts_info_p)->_FPQ.len++; \
393         (rx_ts_info_p)->_FPQ.checkin_ops++; \
394         (rx_ts_info_p)->_FPQ.checkin_xfer++; \
395     } while(0)
396 /* check multiple packets into the thread-specific free packet queue */
397 /* num_transfer must equal length of (q); it is not a means of checking
398  * in part of (q).  passing num_transfer just saves us instructions
399  * since caller already knows length of (q) for other reasons */
400 #define RX_TS_FPQ_QCHECKIN(rx_ts_info_p,num_transfer,q) \
401     do { \
402         struct rx_packet *p, *np; \
403         for (queue_Scan((q), p, np, rx_packet)) { \
404             RX_FPQ_MARK_FREE(p); \
405         } \
406         queue_SplicePrepend(&((rx_ts_info_p)->_FPQ),(q)); \
407         (rx_ts_info_p)->_FPQ.len += (num_transfer); \
408         (rx_ts_info_p)->_FPQ.checkin_ops++; \
409         (rx_ts_info_p)->_FPQ.checkin_xfer += (num_transfer); \
410     } while(0)
411 #endif /* AFS_PTHREAD_ENV */
412
413 /* Number of free packets */
414 EXT int rx_nFreePackets GLOBALSINIT(0);
415 EXT int rxi_NeedMorePackets GLOBALSINIT(0);
416 EXT int rx_packetReclaims GLOBALSINIT(0);
417
418 /* largest packet which we can safely receive, initialized to AFS 3.2 value
419  * This is provided for backward compatibility with peers which may be unable
420  * to swallow anything larger. THIS MUST NEVER DECREASE WHILE AN APPLICATION
421  * IS RUNNING! */
422 EXT afs_uint32 rx_maxReceiveSize GLOBALSINIT(_OLD_MAX_PACKET_SIZE * RX_MAX_FRAGS +
423                                       UDP_HDR_SIZE * (RX_MAX_FRAGS - 1));
424
425 /* this is the maximum packet size that the user wants us to receive */
426 /* this is set by rxTune if required */
427 EXT afs_uint32 rx_maxReceiveSizeUser GLOBALSINIT(0xffffffff);
428
429 /* rx_MyMaxSendSize is the size of the largest packet we will send,
430  * including the RX header. Just as rx_maxReceiveSize is the
431  * max we will receive, including the rx header.
432  */
433 EXT afs_uint32 rx_MyMaxSendSize GLOBALSINIT(8588);
434
435 /* Maximum size of a jumbo datagram we can receive */
436 EXT afs_uint32 rx_maxJumboRecvSize GLOBALSINIT(RX_MAX_PACKET_SIZE);
437
438 /* need this to permit progs to run on AIX systems */
439 EXT int (*rxi_syscallp) (afs_uint32 a3, afs_uint32 a4, void *a5)GLOBALSINIT(0);
440
441 /* List of free queue entries */
442 EXT struct rx_serverQueueEntry *rx_FreeSQEList GLOBALSINIT(0);
443 #ifdef  RX_ENABLE_LOCKS
444 EXT afs_kmutex_t freeSQEList_lock;
445 #endif
446
447 /* List of free call structures */
448 EXT struct rx_queue rx_freeCallQueue;
449 #ifdef  RX_ENABLE_LOCKS
450 EXT afs_kmutex_t rx_freeCallQueue_lock;
451 #endif
452 EXT afs_int32 rxi_nCalls GLOBALSINIT(0);
453
454 /* Port requested at rx_Init.  If this is zero, the actual port used will be different--but it will only be used for client operations.  If non-zero, server provided services may use the same port. */
455 EXT u_short rx_port;
456
457 #if !defined(KERNEL) && !defined(AFS_PTHREAD_ENV)
458 /* 32-bit select Mask for rx_Listener. */
459 EXT fd_set rx_selectMask;
460 EXT osi_socket rx_maxSocketNumber;      /* Maximum socket number in the select mask. */
461 /* Minumum socket number in the select mask. */
462 EXT osi_socket rx_minSocketNumber GLOBALSINIT(0x7fffffff);
463 #endif
464
465 /* This is actually the minimum number of packets that must remain free,
466     overall, immediately after a packet of the requested class has been
467     allocated.  *WARNING* These must be assigned with a great deal of care.
468     In order, these are receive quota, send quota, special quota, receive
469     continuation quota, and send continuation quota. */
470 #define RX_PACKET_QUOTAS {1, 10, 0, 1, 10}
471 /* value large enough to guarantee that no allocation fails due to RX_PACKET_QUOTAS.
472    Make it a little bigger, just for fun */
473 #define RX_MAX_QUOTA    15      /* part of min packet computation */
474 EXT int rx_packetQuota[RX_N_PACKET_CLASSES] GLOBALSINIT(RX_PACKET_QUOTAS);
475 EXT int meltdown_1pkt GLOBALSINIT(1);   /* prefer to schedule single-packet calls */
476 EXT int rxi_md2cnt GLOBALSINIT(0);      /* counter of skipped calls */
477 EXT int rxi_2dchoice GLOBALSINIT(1);    /* keep track of another call to schedule */
478
479 /* quota system: each attached server process must be able to make
480     progress to avoid system deadlock, so we ensure that we can always
481     handle the arrival of the next unacknowledged data packet for an
482     attached call.  rxi_dataQuota gives the max # of packets that must be
483     reserved for active calls for them to be able to make progress, which is
484     essentially enough to queue up a window-full of packets (the first packet
485     may be missing, so these may not get read) + the # of packets the thread
486     may use before reading all of its input (# free must be one more than send
487     packet quota).  Thus, each thread allocates rx_maxReceiveWindow+1 (max
488     queued packets) + an extra for sending data.  The system also reserves
489     RX_MAX_QUOTA (must be more than RX_PACKET_QUOTA[i], which is 10), so that
490     the extra packet can be sent (must be under the system-wide send packet
491     quota to send any packets) */
492 /* # to reserve so that thread with input can still make calls (send packets)
493    without blocking */
494 EXT int rxi_dataQuota GLOBALSINIT(RX_MAX_QUOTA);        /* packets to reserve for active threads */
495
496 EXT afs_int32 rxi_availProcs GLOBALSINIT(0);    /* number of threads in the pool */
497 EXT afs_int32 rxi_totalMin GLOBALSINIT(0);      /* Sum(minProcs) forall services */
498 EXT afs_int32 rxi_minDeficit GLOBALSINIT(0);    /* number of procs needed to handle all minProcs */
499
500 EXT int rx_nextCid;             /* Next connection call id */
501 EXT int rx_epoch;               /* Initialization time of rx */
502 #ifdef  RX_ENABLE_LOCKS
503 EXT afs_kcondvar_t rx_waitingForPackets_cv;
504 #endif
505 EXT char rx_waitingForPackets;  /* Processes set and wait on this variable when waiting for packet buffers */
506
507 EXT struct rx_peer **rx_peerHashTable;
508 EXT struct rx_connection **rx_connHashTable;
509 EXT struct rx_connection *rx_connCleanup_list GLOBALSINIT(0);
510 EXT afs_uint32 rx_hashTableSize GLOBALSINIT(257);       /* Prime number */
511 #ifdef RX_ENABLE_LOCKS
512 EXT afs_kmutex_t rx_peerHashTable_lock;
513 EXT afs_kmutex_t rx_connHashTable_lock;
514 #endif /* RX_ENABLE_LOCKS */
515
516 #define CONN_HASH(host, port, cid, epoch, type) ((((cid)>>RX_CIDSHIFT)%rx_hashTableSize))
517
518 #define PEER_HASH(host, port)  ((host ^ port) % rx_hashTableSize)
519
520 /* Forward definitions of internal procedures */
521 #define rxi_ChallengeOff(conn)  \
522         rxevent_Cancel(&(conn)->challengeEvent, NULL, 0)
523 #define rxi_KeepAliveOff(call) \
524         rxevent_Cancel(&(call)->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE)
525 #define rxi_NatKeepAliveOff(conn) \
526         rxevent_Cancel(&(conn)->natKeepAliveEvent, NULL, 0)
527
528 #define rxi_AllocSecurityObject() rxi_Alloc(sizeof(struct rx_securityClass))
529 #define rxi_FreeSecurityObject(obj) rxi_Free(obj, sizeof(struct rx_securityClass))
530 #define rxi_AllocService()      rxi_Alloc(sizeof(struct rx_service))
531 #define rxi_FreeService(obj) \
532 do { \
533     MUTEX_DESTROY(&(obj)->svc_data_lock);  \
534     rxi_Free((obj), sizeof(struct rx_service)); \
535 } while (0)
536 #define rxi_AllocPeer()         rxi_Alloc(sizeof(struct rx_peer))
537 #define rxi_FreePeer(peer)      rxi_Free(peer, sizeof(struct rx_peer))
538 #define rxi_AllocConnection()   rxi_Alloc(sizeof(struct rx_connection))
539 #define rxi_FreeConnection(conn) (rxi_Free(conn, sizeof(struct rx_connection)))
540
541 EXT afs_int32 rx_stats_active GLOBALSINIT(1);   /* boolean - rx statistics gathering */
542
543 #ifndef KERNEL
544 /* Some debugging stuff */
545 EXT FILE *rx_debugFile;         /* Set by the user to a stdio file for debugging output */
546 EXT FILE *rxevent_debugFile;    /* Set to an stdio descriptor for event logging to that file */
547 #endif
548
549 #ifdef RXDEBUG
550 # define rx_Log rx_debugFile
551 # ifdef AFS_NT40_ENV
552 EXT int rxdebug_active;
553 #  define dpf(args) do { if (rxdebug_active) rxi_DebugPrint args; } while (0)
554 # else
555 #  ifdef DPF_FSLOG
556 #   include <afs/afsutil.h>
557 #   define dpf(args) FSLog args
558 #  else
559 #   define dpf(args) do { if (rx_debugFile) rxi_DebugPrint args; } while (0)
560 #  endif
561 # endif
562 # define rx_Log_event rxevent_debugFile
563 #else
564 # define dpf(args)
565 #endif /* RXDEBUG */
566
567 EXT char *rx_packetTypes[RX_N_PACKET_TYPES] GLOBALSINIT(RX_PACKET_TYPES);       /* Strings defined in rx.h */
568
569 #ifndef KERNEL
570 /*
571  * Counter used to implement connection specific data
572  */
573 EXT int rxi_keyCreate_counter GLOBALSINIT(0);
574 /*
575  * Array of function pointers used to destory connection specific data
576  */
577 EXT rx_destructor_t *rxi_keyCreate_destructor GLOBALSINIT(NULL);
578 #ifdef RX_ENABLE_LOCKS
579 EXT afs_kmutex_t rxi_keyCreate_lock;
580 #endif /* RX_ENABLE_LOCKS */
581 #endif /* !KERNEL */
582
583 /*
584  * SERVER ONLY: Threshholds used to throttle error replies to looping
585  * clients. When consecutive calls are aborting with the same error, the
586  * server throttles the client by waiting before sending error messages.
587  * Disabled if abort thresholds are zero.
588  */
589 EXT int rxi_connAbortThreshhold GLOBALSINIT(0);
590 EXT int rxi_connAbortDelay GLOBALSINIT(3000);
591 EXT int rxi_callAbortThreshhold GLOBALSINIT(0);
592 EXT int rxi_callAbortDelay GLOBALSINIT(3000);
593
594 /*
595  * Thread specific thread ID used to implement LWP_Index().
596  */
597
598 #if defined(AFS_PTHREAD_ENV)
599 EXT int rxi_fcfs_thread_num GLOBALSINIT(0);
600 EXT pthread_key_t rx_thread_id_key;
601 #else
602 #define rxi_fcfs_thread_num (0)
603 #endif
604
605 #if defined(RX_ENABLE_LOCKS)
606 EXT afs_kmutex_t rx_waiting_mutex POSTAMBLE;    /* used to protect waiting counters */
607 EXT afs_kmutex_t rx_quota_mutex POSTAMBLE;      /* used to protect quota counters */
608 EXT afs_kmutex_t rx_pthread_mutex POSTAMBLE;    /* used to protect pthread counters */
609 EXT afs_kmutex_t rx_packets_mutex POSTAMBLE;    /* used to protect packet counters */
610 EXT afs_kmutex_t rx_refcnt_mutex POSTAMBLE;       /* used to protect conn/call ref counts */
611 #endif
612
613 EXT int rx_enable_stats GLOBALSINIT(0);
614
615 /*
616  * Set this flag to enable the listener thread to trade places with an idle
617  * worker thread to move the context switch from listener to worker out of
618  * the request path.
619  */
620 EXT int rx_enable_hot_thread GLOBALSINIT(0);
621
622 EXT int RX_IPUDP_SIZE GLOBALSINIT(_RX_IPUDP_SIZE);
623 #endif /* AFS_RX_GLOBALS_H */