rx-thread-id-startup-20030303
[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 #ifdef  KERNEL
13 #include "rx/rx.h"
14 #else /* KERNEL */
15 # include "rx.h"
16 #endif /* KERNEL */
17
18 #ifndef INIT
19 #define INIT(x)
20 #if defined(AFS_NT40_ENV) && defined(AFS_PTHREAD_ENV)
21 #define EXT __declspec(dllimport) extern
22 #else
23 #define EXT extern
24 #endif
25 #endif
26
27 /* Basic socket for client requests; other sockets (for receiving server requests) are in the service structures */
28 EXT osi_socket rx_socket;
29
30 /* The array of installed services.  Null terminated. */
31 EXT struct rx_service *rx_services[RX_MAX_SERVICES+1];
32 #ifdef RX_ENABLE_LOCKS
33 /* Protects nRequestsRunning as well as pool allocation variables. */
34 EXT afs_kmutex_t rx_serverPool_lock;
35 #endif /* RX_ENABLE_LOCKS */
36
37 /* Incoming calls wait on this queue when there are no available server processes */
38 EXT struct rx_queue rx_incomingCallQueue;
39
40 /* Server processes wait on this queue when there are no appropriate calls to process */
41 EXT struct rx_queue rx_idleServerQueue;
42
43 /* Constant delay time before sending an acknowledge of the last packet received.  This is to avoid sending an extra acknowledge when the client is about to make another call, anyway, or the server is about to respond. */
44 EXT struct clock rx_lastAckDelay;
45
46 /* Constant delay time before sending a hard ack if the receiver consumes
47  * a packet while no delayed ack event is scheduled. Ensures that the
48  * sender is able to advance its window when the receiver consumes a packet
49  * after the sender has exhausted its transmit window.
50  */
51 EXT struct clock rx_hardAckDelay;
52
53 /* Constant delay time before sending a soft ack when none was requested.
54  * This is to make sure we send soft acks before the sender times out,
55  * Normally we wait and send a hard ack when the receiver consumes the packet */
56 EXT struct clock rx_softAckDelay;
57
58 /* Variable to allow introduction of network unreliability */
59 #ifdef RXDEBUG
60 EXT int rx_intentionallyDroppedPacketsPer100 INIT(0);   /* Dropped on Send */
61 #endif
62
63 /* extra packets to add to the quota */
64 EXT int rx_extraQuota INIT(0);
65 /* extra packets to alloc (2 windows by deflt) */
66 EXT int rx_extraPackets INIT(32);
67
68 EXT int rx_stackSize INIT(RX_DEFAULT_STACK_SIZE);
69
70 /* Time until an unresponsive connection is declared dead */
71 EXT int rx_connDeadTime INIT(12);
72 /* Set rx default connection dead time; set on both services and connections at creation time */
73 #define rx_SetRxDeadTime(seconds)   (rx_connDeadTime = (seconds))
74
75 /* Time until we toss an idle connection */
76 EXT int rx_idleConnectionTime INIT(700);
77 /* Time until we toss a peer structure, after all connections using are gone */
78 EXT int rx_idlePeerTime INIT(60);
79
80 /* The file server is temporarily salvaging */
81 EXT int rx_tranquil     INIT(0);
82
83 /* UDP rcv buffer size */
84 EXT int rx_UdpBufSize   INIT(64*1024);
85 #define rx_GetMinUdpBufSize()   (64*1024)
86 #define rx_SetUdpBufSize(x)     (((x)>rx_GetMinUdpBufSize()) ? (rx_UdpBufSize = (x)):0)
87
88 /*
89  * Variables to control RX overload management. When the number of calls
90  * waiting for a thread exceed the threshold, new calls are aborted
91  * with the busy error. 
92  */
93 EXT int rx_BusyThreshold INIT(-1);      /* default is disabled */
94 EXT int rx_BusyError INIT(-1);
95
96 /* These definitions should be in one place */
97 #ifdef  AFS_SUN5_ENV
98 #define RX_CBUF_TIME    180     /* Check for packet deficit */
99 #define RX_REAP_TIME    90          /* Check for tossable connections every 90 seconds */
100 #else
101 #define RX_CBUF_TIME    120     /* Check for packet deficit */
102 #define RX_REAP_TIME    60          /* Check for tossable connections every 60 seconds */
103 #endif
104
105 #define RX_FAST_ACK_RATE 1      /* as of 3.4, ask for an ack every 
106                                    other packet. */
107
108 EXT int rx_minWindow INIT(1); 
109 EXT int rx_initReceiveWindow INIT(16); /* how much to accept */
110 EXT int rx_maxReceiveWindow INIT(32);  /* how much to accept */
111 EXT int rx_initSendWindow INIT(8); 
112 EXT int rx_maxSendWindow INIT(32); 
113 EXT int rx_nackThreshold INIT(3);      /* Number NACKS to trigger congestion recovery */
114 EXT int rx_nDgramThreshold INIT(4);    /* Number of packets before increasing
115                                           packets per datagram */
116 #define RX_MAX_FRAGS 4
117 EXT int rxi_nSendFrags INIT(RX_MAX_FRAGS);  /* max fragments in a datagram */
118 EXT int rxi_nRecvFrags INIT(RX_MAX_FRAGS);
119 EXT int rxi_OrphanFragSize INIT(512);
120
121 #define RX_MAX_DGRAM_PACKETS 6 /* max packets per jumbogram */
122
123 EXT int rxi_nDgramPackets INIT(RX_MAX_DGRAM_PACKETS);
124 /* allow n packets between soft acks - must be power of 2 -1, else change
125  * macro below */
126 EXT int rxi_SoftAckRate INIT(RX_FAST_ACK_RATE);  
127 /* consume n packets before sending hard ack, should be larger than above,
128    but not absolutely necessary.  If it's smaller, than fast receivers will
129    send a soft ack, immediately followed by a hard ack. */
130 EXT int rxi_HardAckRate INIT(RX_FAST_ACK_RATE+1);
131
132 /* EXT int rx_maxWindow INIT(15);   Temporary HACK:  transmit/receive window */
133
134 /* If window sizes become very variable (in terms of #packets), be
135  * sure that the sender can get back a hard acks without having to wait for
136  * some kind of timer event first (like a keep-alive, for instance).
137  * It might be kind of tricky, so it might be better to shrink the
138  * window size by reducing the packet size below the "natural" MTU. */
139
140 #define ACKHACK(p,r) { if (((p)->header.seq & (rxi_SoftAckRate))==0) (p)->header.flags |= RX_REQUEST_ACK; } 
141
142 EXT int rx_nPackets INIT(100);  /* obsolete; use rx_extraPackets now */
143
144 /* List of free packets */
145 EXT struct rx_queue rx_freePacketQueue;
146 #ifdef RX_ENABLE_LOCKS
147 EXT afs_kmutex_t rx_freePktQ_lock;
148 #endif
149
150 /* Number of free packets */
151 EXT int rx_nFreePackets INIT(0);
152 EXT int rxi_NeedMorePackets INIT(0);
153 EXT int rx_nWaiting INIT(0);
154 EXT int rx_packetReclaims INIT(0);
155
156 /* largest packet which we can safely receive, initialized to AFS 3.2 value
157  * This is provided for backward compatibility with peers which may be unable
158  * to swallow anything larger. THIS MUST NEVER DECREASE WHILE AN APPLICATION
159  * IS RUNNING! */
160 EXT afs_uint32 rx_maxReceiveSize INIT(OLD_MAX_PACKET_SIZE*RX_MAX_FRAGS + UDP_HDR_SIZE*(RX_MAX_FRAGS-1));
161
162 /* this is the maximum packet size that the user wants us to receive */
163 /* this is set by rxTune if required */
164 EXT afs_uint32 rx_maxReceiveSizeUser INIT(0xffffffff);
165
166 /* rx_MyMaxSendSize is the size of the largest packet we will send,
167  * including the RX header. Just as rx_maxReceiveSize is the
168  * max we will receive, including the rx header.
169  */
170 EXT afs_uint32 rx_MyMaxSendSize INIT(8588);
171
172 /* Maximum size of a jumbo datagram we can receive */
173 EXT afs_uint32 rx_maxJumboRecvSize INIT(RX_MAX_PACKET_SIZE);
174
175 /* need this to permit progs to run on AIX systems */
176 EXT int (*rxi_syscallp) (afs_uint32 a3, afs_uint32 a4, void *a5) INIT(0); 
177
178 /* List of free queue entries */
179 EXT struct rx_serverQueueEntry *rx_FreeSQEList INIT(0);
180 #ifdef  RX_ENABLE_LOCKS
181 EXT afs_kmutex_t freeSQEList_lock;
182 #endif
183
184 /* List of free call structures */
185 EXT struct rx_queue rx_freeCallQueue;
186 #ifdef  RX_ENABLE_LOCKS
187 EXT afs_kmutex_t rx_freeCallQueue_lock;
188 #endif
189 EXT afs_int32 rxi_nCalls INIT(0);
190
191 /* 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. */
192 EXT u_short rx_port;
193
194 #if !defined(KERNEL) && !defined(AFS_PTHREAD_ENV)
195 /* 32-bit select Mask for rx_Listener. */
196 EXT fd_set rx_selectMask;
197 EXT int rx_maxSocketNumber; /* Maximum socket number in the select mask. */
198 /* Minumum socket number in the select mask. */
199 EXT int rx_minSocketNumber INIT(0x7fffffff);
200 #endif
201
202 /* This is actually the minimum number of packets that must remain free,
203     overall, immediately after a packet of the requested class has been
204     allocated.  *WARNING* These must be assigned with a great deal of care.
205     In order, these are receive quota, send quota, special quota, receive
206     continuation quota, and send continuation quota. */
207 #define RX_PACKET_QUOTAS {1, 10, 0, 1, 10}
208 /* value large enough to guarantee that no allocation fails due to RX_PACKET_QUOTAS.
209    Make it a little bigger, just for fun */
210 #define RX_MAX_QUOTA    15      /* part of min packet computation */
211 EXT int rx_packetQuota[RX_N_PACKET_CLASSES] INIT(RX_PACKET_QUOTAS);
212 EXT int meltdown_1pkt INIT(1); /* prefer to schedule single-packet calls */
213 EXT int rxi_doreclaim INIT(1);    /* if discard one packet, discard all */
214 EXT int rxi_md2cnt INIT(0);    /* counter of skipped calls */
215 EXT int rxi_2dchoice INIT(1);    /* keep track of another call to schedule */
216
217 /* quota system: each attached server process must be able to make
218     progress to avoid system deadlock, so we ensure that we can always
219     handle the arrival of the next unacknowledged data packet for an
220     attached call.  rxi_dataQuota gives the max # of packets that must be
221     reserved for active calls for them to be able to make progress, which is
222     essentially enough to queue up a window-full of packets (the first packet
223     may be missing, so these may not get read) + the # of packets the thread
224     may use before reading all of its input (# free must be one more than send
225     packet quota).  Thus, each thread allocates rx_maxReceiveWindow+1 (max
226     queued packets) + an extra for sending data.  The system also reserves
227     RX_MAX_QUOTA (must be more than RX_PACKET_QUOTA[i], which is 10), so that
228     the extra packet can be sent (must be under the system-wide send packet
229     quota to send any packets) */
230 /* # to reserve so that thread with input can still make calls (send packets)
231    without blocking */
232 EXT int rxi_dataQuota INIT(RX_MAX_QUOTA); /* packets to reserve for active threads */
233
234 EXT afs_int32 rxi_availProcs INIT(0);   /* number of threads in the pool */
235 EXT afs_int32 rxi_totalMin INIT(0);             /* Sum(minProcs) forall services */
236 EXT afs_int32 rxi_minDeficit INIT(0);   /* number of procs needed to handle all minProcs */
237
238 EXT int rx_nextCid;         /* Next connection call id */
239 EXT int rx_epoch;           /* Initialization time of rx */
240 #ifdef  RX_ENABLE_LOCKS
241 EXT afs_kcondvar_t rx_waitingForPackets_cv;
242 #endif
243 EXT char rx_waitingForPackets; /* Processes set and wait on this variable when waiting for packet buffers */
244
245 EXT struct rx_stats rx_stats;
246
247 EXT struct rx_peer **rx_peerHashTable;
248 EXT struct rx_connection **rx_connHashTable;
249 EXT struct rx_connection *rx_connCleanup_list INIT(0);
250 EXT afs_uint32 rx_hashTableSize INIT(256);      /* Power of 2 */
251 EXT afs_uint32 rx_hashTableMask INIT(255);      /* One less than rx_hashTableSize */
252 #ifdef RX_ENABLE_LOCKS
253 EXT afs_kmutex_t rx_peerHashTable_lock;
254 EXT afs_kmutex_t rx_connHashTable_lock;
255 #endif /* RX_ENABLE_LOCKS */
256
257 #define CONN_HASH(host, port, cid, epoch, type) ((((cid)>>RX_CIDSHIFT)&rx_hashTableMask))
258
259 #define PEER_HASH(host, port)  ((host ^ port) & rx_hashTableMask)
260
261 /* Forward definitions of internal procedures */
262 #define rxi_ChallengeOff(conn)  rxevent_Cancel((conn)->challengeEvent, (struct rx_call*)0, 0);
263 #define rxi_KeepAliveOff(call) rxevent_Cancel((call)->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE)
264
265 #define rxi_AllocSecurityObject() (struct rx_securityClass *) rxi_Alloc(sizeof(struct rx_securityClass))
266 #define rxi_FreeSecurityObject(obj) rxi_Free(obj, sizeof(struct rx_securityClass))
267 #define rxi_AllocService()      (struct rx_service *) rxi_Alloc(sizeof(struct rx_service))
268 #define rxi_FreeService(obj)    rxi_Free(obj, sizeof(struct rx_service))
269 #define rxi_AllocPeer()         (struct rx_peer *) rxi_Alloc(sizeof(struct rx_peer))
270 #define rxi_FreePeer(peer)      rxi_Free(peer, sizeof(struct rx_peer))
271 #define rxi_AllocConnection()   (struct rx_connection *) rxi_Alloc(sizeof(struct rx_connection))
272 #define rxi_FreeConnection(conn) (rxi_Free(conn, sizeof(struct rx_connection)))
273
274 #ifdef RXDEBUG
275 /* Some debugging stuff */
276 EXT FILE *rx_debugFile; /* Set by the user to a stdio file for debugging output */
277 EXT FILE *rxevent_debugFile;    /* Set to an stdio descriptor for event logging to that file */
278
279 #define rx_Log rx_debugFile
280 #define dpf(args) if (rx_debugFile) rxi_DebugPrint args; else
281 #define rx_Log_event rxevent_debugFile
282
283 EXT char *rx_packetTypes[RX_N_PACKET_TYPES] INIT(RX_PACKET_TYPES); /* Strings defined in rx.h */
284
285 #ifndef KERNEL
286 /*
287  * Counter used to implement connection specific data
288  */
289 EXT int rxi_keyCreate_counter INIT(0);
290 /*
291  * Array of function pointers used to destory connection specific data
292  */
293 EXT rx_destructor_t *rxi_keyCreate_destructor INIT(NULL);
294 #ifdef RX_ENABLE_LOCKS
295 EXT afs_kmutex_t rxi_keyCreate_lock;
296 #endif /* RX_ENABLE_LOCKS */
297 #endif /* !KERNEL */
298
299 #else
300 #define dpf(args) 
301 #endif /* RXDEBUG */
302
303 /*
304  * SERVER ONLY: Threshholds used to throttle error replies to looping
305  * clients. When consecutive calls are aborting with the same error, the
306  * server throttles the client by waiting before sending error messages.
307  * Disabled if abort thresholds are zero.
308  */
309 EXT int rxi_connAbortThreshhold INIT(0);
310 EXT int rxi_connAbortDelay INIT(3000);
311 EXT int rxi_callAbortThreshhold INIT(0);
312 EXT int rxi_callAbortDelay INIT(3000);
313
314 /*
315  * Thread specific thread ID used to implement LWP_Index().
316  */
317
318 #if defined(AFS_PTHREAD_ENV)
319 EXT int rxi_fcfs_thread_num INIT(0);
320 EXT pthread_key_t rx_thread_id_key;
321 /* keep track of pthread numbers - protected by rx_stats_mutex, 
322    except in rx_Init() before mutex exists! */
323 EXT int rxi_pthread_hinum INIT(0);
324 #else
325 #define rxi_fcfs_thread_num (0)
326 #endif
327
328 #if defined(RX_ENABLE_LOCKS)
329 EXT afs_kmutex_t rx_stats_mutex; /* used to activate stats gathering */
330 #endif
331
332 EXT int rx_enable_stats INIT(0);
333
334 /*
335  * Set this flag to enable the listener thread to trade places with an idle
336  * worker thread to move the context switch from listener to worker out of
337  * the request path.
338  */
339 EXT int rx_enable_hot_thread INIT(0);