2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* RX: Extended Remote Procedure Call */
12 #include <afsconfig.h>
13 #include <afs/param.h>
16 # include "afs/sysincludes.h"
17 # include "afsincludes.h"
22 # ifdef AFS_LINUX20_ENV
23 # include "h/socket.h"
25 # include "netinet/in.h"
27 # include "netinet/ip6.h"
30 # include "inet/common.h"
32 # include "inet/ip_ire.h"
34 # include "afs/afs_args.h"
35 # include "afs/afs_osi.h"
36 # ifdef RX_KERNEL_TRACE
37 # include "rx_kcommon.h"
39 # if defined(AFS_AIX_ENV)
43 # undef RXDEBUG /* turn off debugging */
45 # if defined(AFS_SGI_ENV)
46 # include "sys/debug.h"
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;
60 # include "sys/lockl.h"
61 # include "sys/lock_def.h"
62 # endif /* AFS_AIX41_ENV */
63 # include "afs/rxgen_consts.h"
68 # include <afs/afsutil.h>
69 # include <WINNT\afsreg.h>
78 #include "rx_atomic.h"
79 #include "rx_globals.h"
81 #include "rx_internal.h"
84 #include <afs/rxgen_consts.h>
87 #ifdef AFS_PTHREAD_ENV
89 int (*registerProgram) (pid_t, char *) = 0;
90 int (*swapNameProgram) (pid_t, const char *, char *) = 0;
93 int (*registerProgram) (PROCESS, char *) = 0;
94 int (*swapNameProgram) (PROCESS, const char *, char *) = 0;
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 *);
103 #ifdef RX_ENABLE_LOCKS
104 static void rxi_SetAcksInTransmitQueue(struct rx_call *call);
107 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
109 rx_atomic_t rxi_start_aborted; /* rxi_start awoke after rxi_Send in error.*/
110 rx_atomic_t rxi_start_in_error;
112 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
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
119 * The lastAckDelay may not exceeed 400ms without causing peers to
120 * unecessarily timeout.
122 struct clock rx_lastAckDelay = {0, 400000};
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
128 * This value has been 100ms in all shipping versions of OpenAFS. Changing it
129 * will require changes to the peer's RTT calculations.
131 struct clock rx_softAckDelay = {0, 100000};
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.
140 static unsigned int rxi_rpc_peer_stat_cnt;
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.
149 static unsigned int rxi_rpc_process_stat_cnt;
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
159 static afs_int32 rxi_busyChannelError = 0;
161 rx_atomic_t rx_nWaiting = RX_ATOMIC_INIT(0);
162 rx_atomic_t rx_nWaited = RX_ATOMIC_INIT(0);
164 #if !defined(offsetof)
165 #include <stddef.h> /* for definition of offsetof() */
168 #ifdef RX_ENABLE_LOCKS
169 afs_kmutex_t rx_atomic_mutex;
172 /* Forward prototypes */
173 static struct rx_call * rxi_NewCall(struct rx_connection *, int);
175 #ifdef AFS_PTHREAD_ENV
178 * Use procedural initialization of mutexes/condition variables
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;
198 extern afs_kcondvar_t rx_event_handler_cond;
199 extern afs_kcondvar_t rx_listener_cond;
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;
207 rxi_InitPthread(void)
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);
228 CV_INIT(&rx_event_handler_cond, "evhand", CV_DEFAULT, 0);
229 CV_INIT(&rx_listener_cond, "rxlisten", CV_DEFAULT, 0);
231 osi_Assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
232 osi_Assert(pthread_key_create(&rx_ts_info_key, NULL) == 0);
234 rxkad_global_stats_init();
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
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,
245 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
247 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
249 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
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 */
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)
259 * The rx_stats_mutex mutex protects the following global variables:
260 * rxi_lowConnRefCount
261 * rxi_lowPeerRefCount
270 * The rx_quota_mutex mutex protects the following global variables:
278 * The rx_freePktQ_lock protects the following global variables:
283 * The rx_packets_mutex mutex protects the following global variables:
291 * The rx_pthread_mutex mutex protects the following global variables:
292 * rxi_fcfs_thread_num
295 #define INIT_PTHREAD_LOCKS
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
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.
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.
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);
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.
331 struct rx_connection *rxLastConn = 0;
333 #ifdef RX_ENABLE_LOCKS
334 /* The locking hierarchy for rx fine grain locking is composed of these
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
346 * serverQueueEntry->lock
347 * rx_peerHashTable_lock - locked under rx_connHashTable_lock
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.
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
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
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;
383 /* ------------Exported Interfaces------------- */
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. */
392 #ifdef AFS_PTHREAD_ENV
394 * This mutex protects the following global variables:
398 #define LOCK_EPOCH MUTEX_ENTER(&epoch_mutex)
399 #define UNLOCK_EPOCH MUTEX_EXIT(&epoch_mutex)
403 #endif /* AFS_PTHREAD_ENV */
406 rx_SetEpoch(afs_uint32 epoch)
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
422 int rxinit_status = 1;
423 #ifdef AFS_PTHREAD_ENV
425 * This mutex protects the following global variables:
429 #define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
430 #define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
433 #define UNLOCK_RX_INIT
437 rx_InitHost(u_int host, u_int port)
444 char *htable, *ptable;
451 if (rxinit_status == 0) {
452 tmp_status = rxinit_status;
454 return tmp_status; /* Already started; return previous error code. */
460 if (afs_winsockInit() < 0)
466 * Initialize anything necessary to provide a non-premptive threading
469 rxi_InitializeThreadSupport();
472 /* Allocate and initialize a socket for client and perhaps server
475 rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
476 if (rx_socket == OSI_NULLSOCKET) {
480 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
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,
494 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
496 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
498 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
500 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
501 #if defined(AFS_HPUX110_ENV)
503 rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
504 #endif /* AFS_HPUX110_ENV */
505 #endif /* RX_ENABLE_LOCKS && KERNEL */
508 rx_connDeadTime = 12;
509 rx_tranquil = 0; /* reset flag */
510 rxi_ResetStatistics();
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 *));
519 /* Malloc up a bunch of packets & buffers */
521 queue_Init(&rx_freePacketQueue);
522 rxi_NeedMorePackets = FALSE;
523 rx_nPackets = 0; /* rx_nPackets is managed by rxi_MorePackets* */
525 /* enforce a minimum number of allocated packets */
526 if (rx_extraPackets < rxi_nSendFrags * rx_maxSendWindow)
527 rx_extraPackets = rxi_nSendFrags * rx_maxSendWindow;
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 */
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);
551 #if defined(KERNEL) && !defined(UKERNEL)
552 /* Really, this should never happen in a real kernel */
555 struct sockaddr_in addr;
557 int addrlen = sizeof(addr);
559 socklen_t addrlen = sizeof(addr);
561 if (getsockname((intptr_t)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
565 rx_port = addr.sin_port;
568 rx_stats.minRtt.sec = 9999999;
570 rx_SetEpoch(tv.tv_sec | 0x80000000);
572 rx_SetEpoch(tv.tv_sec); /* Start time of this package, rxkad
573 * will provide a randomer value. */
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;
584 rx_hardAckDelay.sec = 0;
585 rx_hardAckDelay.usec = 100000; /* 100 milliseconds */
587 rxevent_Init(20, rxi_ReScheduleEvents);
589 /* Initialize various global queues */
590 queue_Init(&rx_idleServerQueue);
591 queue_Init(&rx_incomingCallQueue);
592 queue_Init(&rx_freeCallQueue);
594 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
595 /* Initialize our list of usable IP addresses. */
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) */
606 tmp_status = rxinit_status = 0;
614 return rx_InitHost(htonl(INADDR_ANY), port);
618 * Sets the error generated when a busy call channel is detected.
620 * @param[in] error The error to return for a call on a busy channel.
622 * @pre Neither rx_Init nor rx_InitHost have been called yet
625 rx_SetBusyChannelError(afs_int32 error)
627 osi_Assert(rxinit_status != 0);
628 rxi_busyChannelError = error;
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.
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().
640 QuotaOK(struct rx_service *aservice)
642 /* check if over max quota */
643 if (aservice->nRequestsRunning >= aservice->maxProcs) {
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.
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
658 if (aservice->nRequestsRunning <= aservice->minProcs)
661 MUTEX_EXIT(&rx_quota_mutex);
664 MUTEX_EXIT(&rx_quota_mutex);
670 ReturnToServerPool(struct rx_service *aservice)
672 aservice->nRequestsRunning--;
673 MUTEX_ENTER(&rx_quota_mutex);
674 if (aservice->nRequestsRunning < aservice->minProcs)
677 MUTEX_EXIT(&rx_quota_mutex);
680 #else /* RX_ENABLE_LOCKS */
682 QuotaOK(struct rx_service *aservice)
685 /* under min quota, we're OK */
686 if (aservice->nRequestsRunning < aservice->minProcs)
689 /* check if over max quota */
690 if (aservice->nRequestsRunning >= aservice->maxProcs)
693 /* otherwise, can use only if there are enough to allow everyone
694 * to go to their min quota after this guy starts.
696 MUTEX_ENTER(&rx_quota_mutex);
697 if (rxi_availProcs > rxi_minDeficit)
699 MUTEX_EXIT(&rx_quota_mutex);
702 #endif /* RX_ENABLE_LOCKS */
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. */
709 rxi_StartServerProcs(int nExistingProcs)
711 struct rx_service *service;
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++) {
726 service = rx_services[i];
727 if (service == (struct rx_service *)0)
729 nProcs += service->minProcs;
730 diff = service->maxProcs - service->minProcs;
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);
743 /* This routine is only required on Windows */
745 rx_StartClientThread(void)
747 #ifdef AFS_PTHREAD_ENV
749 pid = pthread_self();
750 #endif /* AFS_PTHREAD_ENV */
752 #endif /* AFS_NT40_ENV */
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
758 rx_StartServer(int donateMe)
760 struct rx_service *service;
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.
771 rxi_StartServerProcs(donateMe);
773 /* count up the # of threads in minProcs, and add set the min deficit to
774 * be that value, too.
776 for (i = 0; i < RX_MAX_SERVICES; i++) {
777 service = rx_services[i];
778 if (service == (struct rx_service *)0)
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.
785 rxi_minDeficit += service->minProcs;
786 MUTEX_EXIT(&rx_quota_mutex);
789 /* Turn on reaping of idle server connections */
790 rxi_ReapConnections(NULL, NULL, NULL);
799 #ifdef AFS_PTHREAD_ENV
801 pid = afs_pointer_to_int(pthread_self());
802 #else /* AFS_PTHREAD_ENV */
804 LWP_CurrentProcess(&pid);
805 #endif /* AFS_PTHREAD_ENV */
807 sprintf(name, "srv_%d", ++nProcs);
809 (*registerProgram) (pid, name);
811 #endif /* AFS_NT40_ENV */
812 rx_ServerProc(NULL); /* Never returns */
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.
818 rxi_FlushLocalPacketsTSFPQ();
819 #endif /* RX_ENABLE_TSFPQ */
823 /* Create a new client connection to the specified service, using the
824 * specified security object to implement the security model for this
826 struct rx_connection *
827 rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
828 struct rx_securityClass *securityObject,
829 int serviceSecurityIndex)
833 struct rx_connection *conn;
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));
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);
852 MUTEX_ENTER(&rx_connHashTable_lock);
853 cid = (rx_nextCid += RX_MAXCALLS);
854 conn->type = RX_CLIENT_CONNECTION;
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;
866 conn->specific = NULL;
867 conn->challengeEvent = NULL;
868 conn->delayedAbortEvent = NULL;
869 conn->abortCount = 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;
877 RXS_NewConnection(securityObject, conn);
879 CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
881 conn->refCount++; /* no lock required since only this thread knows... */
882 conn->next = rx_connHashTable[hashindex];
883 rx_connHashTable[hashindex] = conn;
885 rx_atomic_inc(&rx_stats.nClientConns);
886 MUTEX_EXIT(&rx_connHashTable_lock);
892 * Ensure a connection's timeout values are valid.
894 * @param[in] conn The connection to check
896 * @post conn->secondUntilDead <= conn->idleDeadTime <= conn->hardDeadTime,
897 * unless idleDeadTime and/or hardDeadTime are not set
901 rxi_CheckConnTimeouts(struct rx_connection *conn)
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.
911 conn->secondsUntilDead = MAX(conn->secondsUntilDead, 6);
912 if (conn->idleDeadTime) {
913 conn->idleDeadTime = MAX(conn->idleDeadTime, conn->secondsUntilDead);
915 if (conn->hardDeadTime) {
916 if (conn->idleDeadTime) {
917 conn->hardDeadTime = MAX(conn->idleDeadTime, conn->hardDeadTime);
919 conn->hardDeadTime = MAX(conn->secondsUntilDead, conn->hardDeadTime);
925 rx_SetConnDeadTime(struct rx_connection *conn, int seconds)
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;
935 rx_SetConnHardDeadTime(struct rx_connection *conn, int seconds)
937 conn->hardDeadTime = seconds;
938 rxi_CheckConnTimeouts(conn);
942 rx_SetConnIdleDeadTime(struct rx_connection *conn, int seconds)
944 conn->idleDeadTime = seconds;
945 rxi_CheckConnTimeouts(conn);
948 int rxi_lowPeerRefCount = 0;
949 int rxi_lowConnRefCount = 0;
952 * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
953 * NOTE: must not be called with rx_connHashTable_lock held.
956 rxi_CleanupConnection(struct rx_connection *conn)
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);
963 /* Notify the security module that this connection is being destroyed */
964 RXS_DestroyConnection(conn->securityObject, conn);
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.
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);
982 conn->peer->refCount--;
983 MUTEX_EXIT(&rx_peerHashTable_lock);
987 if (conn->type == RX_SERVER_CONNECTION)
988 rx_atomic_dec(&rx_stats.nServerConns);
990 rx_atomic_dec(&rx_stats.nClientConns);
993 if (conn->specific) {
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;
1000 free(conn->specific);
1002 conn->specific = NULL;
1003 conn->nSpecific = 0;
1004 #endif /* !KERNEL */
1006 MUTEX_DESTROY(&conn->conn_call_lock);
1007 MUTEX_DESTROY(&conn->conn_data_lock);
1008 CV_DESTROY(&conn->conn_call_cv);
1010 rxi_FreeConnection(conn);
1013 /* Destroy the specified connection */
1015 rxi_DestroyConnection(struct rx_connection *conn)
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);
1025 #ifdef RX_ENABLE_LOCKS
1027 MUTEX_EXIT(&rx_connHashTable_lock);
1029 #endif /* RX_ENABLE_LOCKS */
1033 rxi_DestroyConnectionNoLock(struct rx_connection *conn)
1035 struct rx_connection **conn_ptr;
1037 struct rx_packet *packet;
1044 MUTEX_ENTER(&conn->conn_data_lock);
1045 MUTEX_ENTER(&rx_refcnt_mutex);
1046 if (conn->refCount > 0)
1049 if (rx_stats_active) {
1050 MUTEX_ENTER(&rx_stats_mutex);
1051 rxi_lowConnRefCount++;
1052 MUTEX_EXIT(&rx_stats_mutex);
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);
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);
1074 MUTEX_EXIT(&rx_refcnt_mutex);
1075 MUTEX_EXIT(&conn->conn_data_lock);
1077 /* Check for extant references to this connection */
1078 for (i = 0; i < RX_MAXCALLS; i++) {
1079 struct rx_call *call = conn->call[i];
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);
1094 rxi_AckAll(NULL, call, 0);
1097 MUTEX_EXIT(&call->lock);
1101 #ifdef RX_ENABLE_LOCKS
1103 if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
1104 MUTEX_EXIT(&conn->conn_data_lock);
1106 /* Someone is accessing a packet right now. */
1110 #endif /* RX_ENABLE_LOCKS */
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);
1122 if (conn->natKeepAliveEvent) {
1123 rxi_NatKeepAliveOff(conn);
1126 if (conn->delayedAbortEvent) {
1127 rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
1128 packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
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);
1137 /* Remove from connection hash table before proceeding */
1139 &rx_connHashTable[CONN_HASH
1140 (peer->host, peer->port, conn->cid, conn->epoch,
1142 for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
1143 if (*conn_ptr == conn) {
1144 *conn_ptr = conn->next;
1148 /* if the conn that we are destroying was the last connection, then we
1149 * clear rxLastConn as well */
1150 if (rxLastConn == conn)
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);
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;
1170 /* Externally available version */
1172 rx_DestroyConnection(struct rx_connection *conn)
1177 rxi_DestroyConnection(conn);
1182 rx_GetConnection(struct rx_connection *conn)
1187 MUTEX_ENTER(&rx_refcnt_mutex);
1189 MUTEX_EXIT(&rx_refcnt_mutex);
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 */
1197 rxi_WaitforTQBusy(struct rx_call *call) {
1198 while (!call->error && (call->flags & RX_CALL_TQ_BUSY)) {
1199 call->flags |= RX_CALL_TQ_WAIT;
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 */
1208 if (call->tqWaiters == 0) {
1209 call->flags &= ~RX_CALL_TQ_WAIT;
1216 rxi_WakeUpTransmitQueue(struct rx_call *call)
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 */
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.
1240 rx_NewCall(struct rx_connection *conn)
1242 int i, wait, ignoreBusy = 1;
1243 struct rx_call *call;
1244 struct clock queueTime;
1245 afs_uint32 leastBusy = 0;
1249 dpf(("rx_NewCall(conn %"AFS_PTR_FMT")\n", conn));
1252 clock_GetTime(&queueTime);
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.
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.
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);
1275 #ifdef RX_ENABLE_LOCKS
1276 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1280 MUTEX_ENTER(&conn->conn_data_lock);
1281 conn->makeCallWaiters--;
1282 if (conn->makeCallWaiters == 0)
1283 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1286 /* We are now the active thread in rx_NewCall */
1287 conn->flags |= RX_CONN_MAKECALL_ACTIVE;
1288 MUTEX_EXIT(&conn->conn_data_lock);
1293 for (i = 0; i < RX_MAXCALLS; i++) {
1294 call = conn->call[i];
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 */
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];
1311 MUTEX_EXIT(&call->lock);
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.
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))
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
1345 MUTEX_EXIT(&call->lock);
1346 MUTEX_ENTER(&conn->conn_call_lock);
1347 MUTEX_ENTER(&call->lock);
1349 if (call->state == RX_STATE_RESET)
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.
1363 MUTEX_ENTER(&rx_refcnt_mutex);
1364 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
1365 MUTEX_EXIT(&rx_refcnt_mutex);
1368 MUTEX_EXIT(&call->lock);
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];
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);
1388 if (i < RX_MAXCALLS) {
1389 conn->lastBusy[i] = 0;
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
1402 MUTEX_ENTER(&conn->conn_data_lock);
1403 conn->flags |= RX_CONN_MAKECALL_WAITING;
1404 conn->makeCallWaiters++;
1405 MUTEX_EXIT(&conn->conn_data_lock);
1407 #ifdef RX_ENABLE_LOCKS
1408 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
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);
1418 /* Client is initially in send mode */
1419 call->state = RX_STATE_ACTIVE;
1420 call->error = conn->error;
1422 call->mode = RX_MODE_ERROR;
1424 call->mode = RX_MODE_SENDING;
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);
1432 /* Turn on busy protocol. */
1433 rxi_KeepAliveOn(call);
1435 /* Attempt MTU discovery */
1436 rxi_GrowMTUOn(call);
1439 * We are no longer the active thread in rx_NewCall
1441 MUTEX_ENTER(&conn->conn_data_lock);
1442 conn->flags &= ~RX_CONN_MAKECALL_ACTIVE;
1443 MUTEX_EXIT(&conn->conn_data_lock);
1446 * Wake up anyone else who might be giving us a chance to
1447 * run (see code above that avoids resource starvation).
1449 #ifdef RX_ENABLE_LOCKS
1450 CV_BROADCAST(&conn->conn_call_cv);
1454 MUTEX_EXIT(&conn->conn_call_lock);
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");
1460 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1462 MUTEX_EXIT(&call->lock);
1465 dpf(("rx_NewCall(call %"AFS_PTR_FMT")\n", call));
1470 rxi_HasActiveCalls(struct rx_connection *aconn)
1473 struct rx_call *tcall;
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)) {
1491 rxi_GetCallNumberVector(struct rx_connection *aconn,
1492 afs_int32 * aint32s)
1495 struct rx_call *tcall;
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;
1503 aint32s[i] = aconn->callNumber[i];
1510 rxi_SetCallNumberVector(struct rx_connection *aconn,
1511 afs_int32 * aint32s)
1514 struct rx_call *tcall;
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;
1522 aconn->callNumber[i] = aint32s[i];
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
1532 char *serviceName; Name for identification purposes (e.g. the
1533 service name might be used for probing for
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))
1541 osi_socket socket = OSI_NULLSOCKET;
1542 struct rx_service *tservice;
1548 if (serviceId == 0) {
1550 "rx_NewService: service id for service %s is not non-zero.\n",
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",
1565 tservice = rxi_AllocService();
1568 #ifdef RX_ENABLE_LOCKS
1569 MUTEX_INIT(&tservice->svc_data_lock, "svc data lock", MUTEX_DEFAULT, 0);
1572 for (i = 0; i < RX_MAX_SERVICES; i++) {
1573 struct rx_service *service = rx_services[i];
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. */
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);
1585 rxi_FreeService(tservice);
1588 /* Different service, same port: re-use the socket
1589 * which is bound to the same port */
1590 socket = service->socket;
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) {
1599 rxi_FreeService(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 */
1626 rxi_FreeService(tservice);
1627 (osi_Msg "rx_NewService: cannot support > %d services\n",
1632 /* Set configuration options for all of a service's security objects */
1635 rx_SetSecurityConfiguration(struct rx_service *service,
1636 rx_securityConfigVariables type,
1640 for (i = 0; i<service->nSecurityObjects; i++) {
1641 if (service->securityObjects[i]) {
1642 RXS_SetConfiguration(service->securityObjects[i], NULL, type,
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))
1654 return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
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
1663 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1665 struct rx_call *call;
1667 struct rx_service *tservice = NULL;
1674 call = rx_GetCall(threadID, tservice, socketp);
1675 if (socketp && *socketp != OSI_NULLSOCKET) {
1676 /* We are now a listener thread */
1681 /* if server is restarting( typically smooth shutdown) then do not
1682 * allow any new calls.
1685 if (rx_tranquil && (call != NULL)) {
1689 MUTEX_ENTER(&call->lock);
1691 rxi_CallError(call, RX_RESTARTING);
1692 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1694 MUTEX_EXIT(&call->lock);
1698 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1699 #ifdef RX_ENABLE_LOCKS
1701 #endif /* RX_ENABLE_LOCKS */
1702 afs_termState = AFSOP_STOP_AFS;
1703 afs_osi_Wakeup(&afs_termState);
1704 #ifdef RX_ENABLE_LOCKS
1706 #endif /* RX_ENABLE_LOCKS */
1711 tservice = call->conn->service;
1713 if (tservice->beforeProc)
1714 (*tservice->beforeProc) (call);
1716 code = tservice->executeRequestProc(call);
1718 if (tservice->afterProc)
1719 (*tservice->afterProc) (call, code);
1721 rx_EndCall(call, code);
1722 if (rx_stats_active) {
1723 MUTEX_ENTER(&rx_stats_mutex);
1725 MUTEX_EXIT(&rx_stats_mutex);
1732 rx_WakeupServerProcs(void)
1734 struct rx_serverQueueEntry *np, *tqp;
1738 MUTEX_ENTER(&rx_serverPool_lock);
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 */
1754 #endif /* RX_ENABLE_LOCKS */
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 */
1762 #endif /* RX_ENABLE_LOCKS */
1764 MUTEX_EXIT(&rx_serverPool_lock);
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.
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.
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.
1796 /* Sleep until a call arrives. Returns a pointer to the call, ready
1797 * for an rx_Read. */
1798 #ifdef RX_ENABLE_LOCKS
1800 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1802 struct rx_serverQueueEntry *sq;
1803 struct rx_call *call = (struct rx_call *)0;
1804 struct rx_service *service = NULL;
1806 MUTEX_ENTER(&freeSQEList_lock);
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);
1818 MUTEX_ENTER(&rx_serverPool_lock);
1819 if (cur_service != NULL) {
1820 ReturnToServerPool(cur_service);
1823 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1824 struct rx_call *tcall, *ncall, *choice2 = NULL;
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)) {
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;
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) {
1855 || (rp->header.flags & RX_LAST_PACKET)) {
1857 } else if (rxi_2dchoice && !choice2
1858 && !(tcall->flags & RX_CALL_CLEARED)
1859 && (tcall->rprev > rxi_HardAckRate)) {
1869 ReturnToServerPool(service);
1876 MUTEX_EXIT(&rx_serverPool_lock);
1877 MUTEX_ENTER(&call->lock);
1879 if (call->flags & RX_CALL_WAIT_PROC) {
1880 call->flags &= ~RX_CALL_WAIT_PROC;
1881 rx_atomic_dec(&rx_nWaiting);
1884 if (call->state != RX_STATE_PRECALL || call->error) {
1885 MUTEX_EXIT(&call->lock);
1886 MUTEX_ENTER(&rx_serverPool_lock);
1887 ReturnToServerPool(service);
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);
1896 CLEAR_CALL_QUEUE_LOCK(call);
1899 /* If there are no eligible incoming calls, add this process
1900 * to the idle server queue, to wait for one */
1904 *socketp = OSI_NULLSOCKET;
1906 sq->socketp = socketp;
1907 queue_Append(&rx_idleServerQueue, sq);
1908 #ifndef AFS_AIX41_ENV
1909 rx_waitForPacket = sq;
1911 rx_waitingForPacket = sq;
1912 #endif /* AFS_AIX41_ENV */
1914 CV_WAIT(&sq->cv, &rx_serverPool_lock);
1916 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1917 MUTEX_EXIT(&rx_serverPool_lock);
1918 return (struct rx_call *)0;
1921 } while (!(call = sq->newcall)
1922 && !(socketp && *socketp != OSI_NULLSOCKET));
1923 MUTEX_EXIT(&rx_serverPool_lock);
1925 MUTEX_ENTER(&call->lock);
1931 MUTEX_ENTER(&freeSQEList_lock);
1932 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1933 rx_FreeSQEList = sq;
1934 MUTEX_EXIT(&freeSQEList_lock);
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();
1945 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1946 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
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,
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);
1963 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
1968 #else /* RX_ENABLE_LOCKS */
1970 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1972 struct rx_serverQueueEntry *sq;
1973 struct rx_call *call = (struct rx_call *)0, *choice2;
1974 struct rx_service *service = NULL;
1978 MUTEX_ENTER(&freeSQEList_lock);
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);
1989 MUTEX_ENTER(&sq->lock);
1991 if (cur_service != NULL) {
1992 cur_service->nRequestsRunning--;
1993 MUTEX_ENTER(&rx_quota_mutex);
1994 if (cur_service->nRequestsRunning < cur_service->minProcs)
1997 MUTEX_EXIT(&rx_quota_mutex);
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;
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
2029 || (rp->header.flags & RX_LAST_PACKET))) {
2031 } else if (rxi_2dchoice && !choice2
2032 && !(tcall->flags & RX_CALL_CLEARED)
2033 && (tcall->rprev > rxi_HardAckRate)) {
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);
2056 call->flags &= (~RX_CALL_WAIT_PROC);
2057 service->nRequestsRunning++;
2058 /* just started call in minProcs pool, need fewer to maintain
2060 MUTEX_ENTER(&rx_quota_mutex);
2061 if (service->nRequestsRunning <= service->minProcs)
2064 MUTEX_EXIT(&rx_quota_mutex);
2065 rx_atomic_dec(&rx_nWaiting);
2066 /* MUTEX_EXIT(&call->lock); */
2068 /* If there are no eligible incoming calls, add this process
2069 * to the idle server queue, to wait for one */
2072 *socketp = OSI_NULLSOCKET;
2074 sq->socketp = socketp;
2075 queue_Append(&rx_idleServerQueue, sq);
2079 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
2081 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
2082 return (struct rx_call *)0;
2085 } while (!(call = sq->newcall)
2086 && !(socketp && *socketp != OSI_NULLSOCKET));
2088 MUTEX_EXIT(&sq->lock);
2090 MUTEX_ENTER(&freeSQEList_lock);
2091 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
2092 rx_FreeSQEList = sq;
2093 MUTEX_EXIT(&freeSQEList_lock);
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();
2104 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
2105 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
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,
2117 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
2124 #endif /* RX_ENABLE_LOCKS */
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
2139 rx_SetArrivalProc(struct rx_call *call,
2140 void (*proc) (struct rx_call * call,
2143 void * handle, int arg)
2145 call->arrivalProc = proc;
2146 call->arrivalProcHandle = handle;
2147 call->arrivalProcArg = arg;
2150 /* Call is finished (possibly prematurely). Return rc to the peer, if
2151 * appropriate, and return the final error code from the conversation
2155 rx_EndCall(struct rx_call *call, afs_int32 rc)
2157 struct rx_connection *conn = call->conn;
2161 dpf(("rx_EndCall(call %"AFS_PTR_FMT" rc %d error %d abortCode %d)\n",
2162 call, rc, call->error, call->abortCode));
2165 MUTEX_ENTER(&call->lock);
2167 if (rc == 0 && call->error == 0) {
2168 call->abortCode = 0;
2169 call->abortCount = 0;
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
2180 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
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);
2189 if (call->mode == RX_MODE_SENDING) {
2190 MUTEX_EXIT(&call->lock);
2191 rxi_FlushWrite(call);
2192 MUTEX_ENTER(&call->lock);
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;
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);
2205 } else { /* Client connection */
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);
2216 /* If we had an outstanding delayed ack, be nice to the server
2217 * and force-send it now.
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);
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.
2235 MUTEX_EXIT(&call->lock);
2236 MUTEX_ENTER(&conn->conn_call_lock);
2237 MUTEX_ENTER(&call->lock);
2239 if (!(call->flags & RX_CALL_PEER_BUSY)) {
2240 conn->lastBusy[call->channel] = 0;
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);
2253 #ifdef RX_ENABLE_LOCKS
2255 MUTEX_EXIT(&conn->conn_data_lock);
2257 #endif /* RX_ENABLE_LOCKS */
2258 call->state = RX_STATE_DALLY;
2260 error = call->error;
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;
2270 rxi_FreePacket(call->currentPacket);
2271 call->currentPacket = (struct rx_packet *)0;
2274 call->nLeft = call->nFree = call->curlen = 0;
2276 /* Free any packets from the last call to ReadvProc/WritevProc */
2277 #ifdef RXDEBUG_PACKET
2279 #endif /* RXDEBUG_PACKET */
2280 rxi_FreePackets(0, &call->iovq);
2281 MUTEX_EXIT(&call->lock);
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);
2294 * Map errors to the local host's errno.h format.
2296 error = ntoh_syserr_conv(error);
2300 #if !defined(KERNEL)
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. */
2311 struct rx_connection **conn_ptr, **conn_end;
2315 if (rxinit_status == 1) {
2317 return; /* Already shutdown. */
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;
2325 struct rx_connection *conn, *next;
2326 for (conn = *conn_ptr; conn; conn = next) {
2328 if (conn->type == RX_CLIENT_CONNECTION) {
2329 MUTEX_ENTER(&rx_refcnt_mutex);
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 */
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);
2349 MUTEX_EXIT(&rx_connHashTable_lock);
2350 #endif /* RX_ENABLE_LOCKS */
2355 afs_winsockCleanup();
2363 /* if we wakeup packet waiter too often, can get in loop with two
2364 AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2366 rxi_PacketsUnWait(void)
2368 if (!rx_waitingForPackets) {
2372 if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2373 return; /* still over quota */
2376 rx_waitingForPackets = 0;
2377 #ifdef RX_ENABLE_LOCKS
2378 CV_BROADCAST(&rx_waitingForPackets_cv);
2380 osi_rxWakeup(&rx_waitingForPackets);
2386 /* ------------------Internal interfaces------------------------- */
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)
2393 struct rx_service **sp;
2394 for (sp = &rx_services[0]; *sp; sp++) {
2395 if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2401 #ifdef RXDEBUG_PACKET
2402 #ifdef KDUMP_RX_LOCK
2403 static struct rx_call_rx_lock *rx_allCallsp = 0;
2405 static struct rx_call *rx_allCallsp = 0;
2407 #endif /* RXDEBUG_PACKET */
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)
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 */
2421 dpf(("rxi_NewCall(conn %"AFS_PTR_FMT", channel %d)\n", conn, channel));
2423 /* Grab an existing call structure, or allocate a new one.
2424 * Existing call structures are assumed to have been left reset by
2426 MUTEX_ENTER(&rx_freeCallQueue_lock);
2428 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2430 * EXCEPT that the TQ might not yet be cleared out.
2431 * Skip over those with in-use TQs.
2434 for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2435 if (!(cp->flags & RX_CALL_TQ_BUSY)) {
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 */
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);*/
2458 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2459 /* Bind the call to its connection structure */
2461 rxi_ResetCall(call, 1);
2464 call = rxi_Alloc(sizeof(struct rx_call));
2465 #ifdef RXDEBUG_PACKET
2466 call->allNextp = rx_allCallsp;
2467 rx_allCallsp = call;
2469 rx_atomic_inc_and_read(&rx_stats.nCallStructs);
2470 #else /* RXDEBUG_PACKET */
2471 rx_atomic_inc(&rx_stats.nCallStructs);
2472 #endif /* RXDEBUG_PACKET */
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);
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) */
2490 rxi_ResetCall(call, 1);
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
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;
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
2512 * call->lock amd rx_refcnt_mutex are held upon entry.
2513 * haveCTLock is set when called from rxi_ReapConnections.
2516 rxi_FreeCall(struct rx_call *call, int haveCTLock)
2518 int channel = call->channel;
2519 struct rx_connection *conn = call->conn;
2522 if (call->state == RX_STATE_DALLY || call->state == RX_STATE_HOLD)
2523 (*call->callNumber)++;
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.
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;
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.
2544 if (call->flags & RX_CALL_TQ_BUSY)
2545 queue_Prepend(&rx_freeCallQueue, call);
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);
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.
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);
2571 MUTEX_EXIT(&rx_refcnt_mutex);
2572 MUTEX_EXIT(&conn->conn_data_lock);
2573 #ifdef RX_ENABLE_LOCKS
2575 rxi_DestroyConnectionNoLock(conn);
2577 rxi_DestroyConnection(conn);
2578 #else /* RX_ENABLE_LOCKS */
2579 rxi_DestroyConnection(conn);
2580 #endif /* RX_ENABLE_LOCKS */
2582 MUTEX_EXIT(&conn->conn_data_lock);
2584 MUTEX_ENTER(&rx_refcnt_mutex);
2587 rx_atomic_t rxi_Allocsize = RX_ATOMIC_INIT(0);
2588 rx_atomic_t rxi_Alloccnt = RX_ATOMIC_INIT(0);
2591 rxi_Alloc(size_t size)
2595 if (rx_stats_active) {
2596 rx_atomic_add(&rxi_Allocsize, (int) size);
2597 rx_atomic_inc(&rxi_Alloccnt);
2601 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
2602 afs_osi_Alloc_NoSleep(size);
2607 osi_Panic("rxi_Alloc error");
2613 rxi_Free(void *addr, size_t size)
2615 if (rx_stats_active) {
2616 rx_atomic_sub(&rxi_Allocsize, (int) size);
2617 rx_atomic_dec(&rxi_Alloccnt);
2619 osi_Free(addr, size);
2623 rxi_SetPeerMtu(struct rx_peer *peer, afs_uint32 host, afs_uint32 port, int mtu)
2625 struct rx_peer **peer_ptr = NULL, **peer_end = NULL;
2626 struct rx_peer *next = NULL;
2630 MUTEX_ENTER(&rx_peerHashTable_lock);
2632 peer_ptr = &rx_peerHashTable[0];
2633 peer_end = &rx_peerHashTable[rx_hashTableSize];
2636 for ( ; peer_ptr < peer_end; peer_ptr++) {
2639 for ( ; peer; peer = next) {
2641 if (host == peer->host)
2646 hashIndex = PEER_HASH(host, port);
2647 for (peer = rx_peerHashTable[hashIndex]; peer; peer = peer->next) {
2648 if ((peer->host == host) && (peer->port == port))
2653 MUTEX_ENTER(&rx_peerHashTable_lock);
2658 MUTEX_EXIT(&rx_peerHashTable_lock);
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);
2675 MUTEX_ENTER(&rx_peerHashTable_lock);
2677 if (host && !port) {
2679 /* pick up where we left off */
2683 MUTEX_EXIT(&rx_peerHashTable_lock);
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 */
2693 rxi_FindPeer(afs_uint32 host, u_short port,
2694 struct rx_peer *origPeer, int create)
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))
2706 pp = rxi_AllocPeer(); /* This bzero's *pp */
2707 pp->host = host; /* set here or in InitPeerParams is zero */
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);
2723 origPeer->refCount--;
2724 MUTEX_EXIT(&rx_peerHashTable_lock);
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)
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],
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
2762 MUTEX_EXIT(&rx_connHashTable_lock);
2763 return (struct rx_connection *)0;
2765 if (pp->host == host && pp->port == port)
2767 if (type == RX_CLIENT_CONNECTION && pp->port == port)
2769 /* So what happens when it's a callback connection? */
2770 if ( /*type == RX_CLIENT_CONNECTION && */
2771 (conn->epoch & 0x80000000))
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 */
2778 conn = rx_connHashTable[hashindex];
2783 struct rx_service *service;
2784 if (type == RX_CLIENT_CONNECTION) {
2785 MUTEX_EXIT(&rx_connHashTable_lock);
2786 return (struct rx_connection *)0;
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;
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;
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);
2830 MUTEX_ENTER(&rx_refcnt_mutex);
2832 MUTEX_EXIT(&rx_refcnt_mutex);
2834 rxLastConn = conn; /* store this connection as the last conn used */
2835 MUTEX_EXIT(&rx_connHashTable_lock);
2840 * Timeout a call on a busy call channel if appropriate.
2842 * @param[in] call The busy call.
2844 * @pre 'call' is marked as busy (namely,
2845 * call->conn->lastBusy[call->channel] != 0)
2847 * @pre call->lock is held
2848 * @pre rxi_busyChannelError is nonzero
2850 * @note call->lock is dropped and reacquired
2853 rxi_CheckBusy(struct rx_call *call)
2855 struct rx_connection *conn = call->conn;
2856 int channel = call->channel;
2857 int freechannel = 0;
2859 afs_uint32 callNumber = *call->callNumber;
2861 MUTEX_EXIT(&call->lock);
2863 MUTEX_ENTER(&conn->conn_call_lock);
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. */
2869 for (i = 0; i < RX_MAXCALLS && !freechannel; i++) {
2871 /* only look at channels that aren't us */
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) {
2880 if (call->startTime.sec - conn->lastBusy[i] < conn->secondsUntilDead) {
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) {
2891 MUTEX_EXIT(&tcall->lock);
2897 MUTEX_EXIT(&conn->conn_call_lock);
2899 MUTEX_ENTER(&call->lock);
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. */
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. */
2916 rxi_CallError(call, rxi_busyChannelError);
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. */
2928 int (*rx_justReceived) (struct rx_packet *, struct sockaddr_in *) = 0;
2929 int (*rx_almostSent) (struct rx_packet *, struct sockaddr_in *) = 0;
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 */
2938 rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
2939 afs_uint32 host, u_short port, int *tnop,
2940 struct rx_call **newcallp)
2942 struct rx_call *call;
2943 struct rx_connection *conn;
2945 afs_uint32 currentCallNumber;
2951 struct rx_packet *tnp;
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));
2966 if (np->header.type == RX_PACKET_TYPE_VERSION) {
2967 return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
2970 if (np->header.type == RX_PACKET_TYPE_DEBUG) {
2971 return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
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;
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 */
2989 port = addr.sin_port; /* in case fcn changed addr */
2990 host = addr.sin_addr.s_addr;
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;
2998 /* Find the connection (or fabricate one, if we're the server & if
2999 * necessary) associated with this packet */
3001 rxi_FindConnection(socket, host, port, np->header.serviceId,
3002 np->header.cid, np->header.epoch, type,
3003 np->header.securityIndex);
3006 /* If no connection found or fabricated, just ignore the packet.
3007 * (An argument could be made for sending an abort packet for
3012 /* If the connection is in an error state, send an abort packet and ignore
3013 * the incoming packet */
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);
3021 MUTEX_EXIT(&rx_refcnt_mutex);
3022 MUTEX_EXIT(&conn->conn_data_lock);
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);
3036 MUTEX_EXIT(&rx_refcnt_mutex);
3039 case RX_PACKET_TYPE_CHALLENGE:
3040 tnp = rxi_ReceiveChallengePacket(conn, np, 1);
3041 MUTEX_ENTER(&rx_refcnt_mutex);
3043 MUTEX_EXIT(&rx_refcnt_mutex);
3045 case RX_PACKET_TYPE_RESPONSE:
3046 tnp = rxi_ReceiveResponsePacket(conn, np, 1);
3047 MUTEX_ENTER(&rx_refcnt_mutex);
3049 MUTEX_EXIT(&rx_refcnt_mutex);
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);
3057 MUTEX_EXIT(&rx_refcnt_mutex);
3062 /* Should not reach here, unless the peer is broken: send an
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);
3069 MUTEX_EXIT(&rx_refcnt_mutex);
3070 MUTEX_EXIT(&conn->conn_data_lock);
3075 channel = np->header.cid & RX_CHANNELMASK;
3076 call = conn->call[channel];
3077 #ifdef RX_ENABLE_LOCKS
3079 MUTEX_ENTER(&call->lock);
3080 /* Test to see if call struct is still attached to conn. */
3081 if (call != conn->call[channel]) {
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
3098 MUTEX_ENTER(&call->lock);
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.
3105 if (rx_stats_active)
3106 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3107 MUTEX_ENTER(&rx_refcnt_mutex);
3109 MUTEX_EXIT(&rx_refcnt_mutex);
3114 currentCallNumber = conn->callNumber[channel];
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
3122 MUTEX_EXIT(&call->lock);
3124 MUTEX_ENTER(&rx_refcnt_mutex);
3126 MUTEX_EXIT(&rx_refcnt_mutex);
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;
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));
3141 call->state = RX_STATE_PRECALL;
3142 clock_GetTime(&call->queueTime);
3143 hzero(call->bytesSent);
3144 hzero(call->bytesRcvd);
3146 * If the number of queued calls exceeds the overload
3147 * threshold then abort this call.
3149 if ((rx_BusyThreshold > 0) &&
3150 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
3151 struct rx_packet *tp;
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);
3158 MUTEX_EXIT(&rx_refcnt_mutex);
3159 if (rx_stats_active)
3160 rx_atomic_inc(&rx_stats.nBusies);
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
3170 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3171 if (call->state == RX_STATE_ACTIVE) {
3172 rxi_WaitforTQBusy(call);
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.
3179 rxi_CallError(call, call->error);
3180 MUTEX_EXIT(&call->lock);
3181 MUTEX_ENTER(&rx_refcnt_mutex);
3183 MUTEX_EXIT(&rx_refcnt_mutex);
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;
3194 rxi_CallError(call, RX_CALL_DEAD);
3195 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
3197 MUTEX_EXIT(&call->lock);
3198 MUTEX_ENTER(&rx_refcnt_mutex);
3200 MUTEX_EXIT(&rx_refcnt_mutex);
3203 rxi_ResetCall(call, 0);
3204 *call->callNumber = np->header.callNumber;
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));
3212 call->state = RX_STATE_PRECALL;
3213 clock_GetTime(&call->queueTime);
3214 hzero(call->bytesSent);
3215 hzero(call->bytesRcvd);
3217 * If the number of queued calls exceeds the overload
3218 * threshold then abort this call.
3220 if ((rx_BusyThreshold > 0) &&
3221 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
3222 struct rx_packet *tp;
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);
3229 MUTEX_EXIT(&rx_refcnt_mutex);
3230 if (rx_stats_active)
3231 rx_atomic_inc(&rx_stats.nBusies);
3234 rxi_KeepAliveOn(call);
3236 /* Continuing call; do nothing here. */
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
3246 MUTEX_EXIT(&call->lock);
3249 MUTEX_ENTER(&rx_refcnt_mutex);
3251 MUTEX_EXIT(&rx_refcnt_mutex);
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
3262 MUTEX_EXIT(&call->lock);
3265 MUTEX_ENTER(&rx_refcnt_mutex);
3267 MUTEX_EXIT(&rx_refcnt_mutex);
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);
3276 MUTEX_ENTER(&rx_refcnt_mutex);
3278 MUTEX_EXIT(&rx_refcnt_mutex);
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.
3295 if (call->flags & RX_CALL_TQ_BUSY) {
3296 #ifdef RX_ENABLE_LOCKS
3297 rxi_SetAcksInTransmitQueue(call);
3299 MUTEX_ENTER(&rx_refcnt_mutex);
3301 MUTEX_EXIT(&rx_refcnt_mutex);
3302 return np; /* xmitting; drop packet */
3305 rxi_ClearTransmitQueue(call, 0);
3307 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
3308 rxi_ClearTransmitQueue(call, 0);
3309 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
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
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);
3331 MUTEX_EXIT(&rx_refcnt_mutex);
3335 } /* else not a data packet */
3338 osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
3339 /* Set remote user defined status from packet */
3340 call->remoteStatus = np->header.userStatus;
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
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
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);
3358 struct rx_peer *peer;
3360 if (skew > peer->inPacketSkew) {
3361 dpf(("*** In skew changed from %d to %d\n",
3362 peer->inPacketSkew, skew));
3363 peer->inPacketSkew = skew;
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,
3373 case RX_PACKET_TYPE_ACK:
3374 /* Respond immediately to ack packets requesting acknowledgement
3376 if (np->header.flags & RX_REQUEST_ACK) {
3378 (void)rxi_SendCallAbort(call, 0, 1, 0);
3380 (void)rxi_SendAck(call, 0, np->header.serial,
3381 RX_ACK_PING_RESPONSE, 1);
3383 np = rxi_ReceiveAckPacket(call, np, 1);
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);
3395 MUTEX_EXIT(&rx_refcnt_mutex);
3396 return np; /* xmitting; drop packet */
3398 case RX_PACKET_TYPE_BUSY: {
3399 struct clock busyTime;
3401 clock_GetTime(&busyTime);
3403 MUTEX_EXIT(&call->lock);
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);
3412 MUTEX_ENTER(&rx_refcnt_mutex);
3414 MUTEX_EXIT(&rx_refcnt_mutex);
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.
3431 if (call->flags & RX_CALL_TQ_BUSY) {
3432 #ifdef RX_ENABLE_LOCKS
3433 rxi_SetAcksInTransmitQueue(call);
3435 #else /* RX_ENABLE_LOCKS */
3436 MUTEX_EXIT(&call->lock);
3437 MUTEX_ENTER(&rx_refcnt_mutex);
3439 MUTEX_EXIT(&rx_refcnt_mutex);
3440 return np; /* xmitting; drop packet */
3441 #endif /* RX_ENABLE_LOCKS */
3443 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3444 rxi_ClearTransmitQueue(call, 0);
3447 /* Should not reach here, unless the peer is broken: send an abort
3449 rxi_CallError(call, RX_PROTOCOL_ERROR);
3450 np = rxi_SendCallAbort(call, np, 1, 0);
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);
3463 MUTEX_EXIT(&rx_refcnt_mutex);
3467 /* return true if this is an "interesting" connection from the point of view
3468 of someone trying to debug the system */
3470 rxi_IsConnInteresting(struct rx_connection *aconn)
3473 struct rx_call *tcall;
3475 if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
3478 for (i = 0; i < RX_MAXCALLS; i++) {
3479 tcall = aconn->call[i];
3481 if ((tcall->state == RX_STATE_PRECALL)
3482 || (tcall->state == RX_STATE_ACTIVE))
3484 if ((tcall->mode == RX_MODE_SENDING)
3485 || (tcall->mode == RX_MODE_RECEIVING))
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. */
3501 TooLow(struct rx_packet *ap, struct rx_call *acall)
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)))) {
3513 MUTEX_EXIT(&rx_quota_mutex);
3519 rxi_CheckReachEvent(struct rxevent *event, void *arg1, void *arg2)
3521 struct rx_connection *conn = arg1;
3522 struct rx_call *acall = arg2;
3523 struct rx_call *call = acall;
3524 struct clock when, now;
3527 MUTEX_ENTER(&conn->conn_data_lock);
3528 conn->checkReachEvent = NULL;
3529 waiting = conn->flags & RX_CONN_ATTACHWAIT;
3531 MUTEX_ENTER(&rx_refcnt_mutex);
3533 MUTEX_EXIT(&rx_refcnt_mutex);
3535 MUTEX_EXIT(&conn->conn_data_lock);
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) {
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.
3554 conn->flags &= ~RX_CONN_ATTACHWAIT;
3555 MUTEX_EXIT(&conn->conn_data_lock);
3556 MUTEX_EXIT(&conn->conn_call_lock);
3561 MUTEX_ENTER(&call->lock);
3562 rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3564 MUTEX_EXIT(&call->lock);
3566 clock_GetTime(&now);
3568 when.sec += RX_CHECKREACH_TIMEOUT;
3569 MUTEX_ENTER(&conn->conn_data_lock);
3570 if (!conn->checkReachEvent) {
3571 MUTEX_ENTER(&rx_refcnt_mutex);
3573 MUTEX_EXIT(&rx_refcnt_mutex);
3574 conn->checkReachEvent =
3575 rxevent_PostNow(&when, &now, rxi_CheckReachEvent, conn,
3578 MUTEX_EXIT(&conn->conn_data_lock);
3584 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3586 struct rx_service *service = conn->service;
3587 struct rx_peer *peer = conn->peer;
3588 afs_uint32 now, lastReach;
3590 if (service->checkReach == 0)
3594 MUTEX_ENTER(&peer->peer_lock);
3595 lastReach = peer->lastReachTime;
3596 MUTEX_EXIT(&peer->peer_lock);
3597 if (now - lastReach < RX_CHECKREACH_TTL)
3600 MUTEX_ENTER(&conn->conn_data_lock);
3601 if (conn->flags & RX_CONN_ATTACHWAIT) {
3602 MUTEX_EXIT(&conn->conn_data_lock);
3605 conn->flags |= RX_CONN_ATTACHWAIT;
3606 MUTEX_EXIT(&conn->conn_data_lock);
3607 if (!conn->checkReachEvent)
3608 rxi_CheckReachEvent(NULL, conn, call);
3613 /* try to attach call, if authentication is complete */
3615 TryAttach(struct rx_call *acall, osi_socket socket,
3616 int *tnop, struct rx_call **newcallp,
3619 struct rx_connection *conn = acall->conn;
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
3631 rxi_ChallengeOn(acall->conn);
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 */
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)
3646 int ackNeeded = 0; /* 0 means no, otherwise ack_reason */
3651 afs_uint32 serial=0, flags=0;
3653 struct rx_packet *tnp;
3654 struct clock when, now;
3655 if (rx_stats_active)
3656 rx_atomic_inc(&rx_stats.dataPacketsRead);
3659 /* If there are no packet buffers, drop this new packet, unless we can find
3660 * packet buffers from inactive calls */
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));
3672 rxi_ClearReceiveQueue(call);
3673 clock_GetTime(&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);
3684 call->delayedAckEvent =
3685 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3687 /* we've damaged this call already, might as well do it in. */
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.
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 */
3707 seq = np->header.seq;
3708 serial = np->header.serial;
3709 flags = np->header.flags;
3711 /* If the call is in an error state, send an abort message */
3713 return rxi_SendCallAbort(call, np, istack, 0);