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>
14 #include "afs/param.h"
16 #include <afs/param.h>
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
28 #include <net/net_globals.h>
29 #endif /* AFS_OSF_ENV */
30 #ifdef AFS_LINUX20_ENV
33 #include "netinet/in.h"
35 #include "inet/common.h"
37 #include "inet/ip_ire.h"
39 #include "afs/afs_args.h"
40 #include "afs/afs_osi.h"
41 #ifdef RX_KERNEL_TRACE
42 #include "rx_kcommon.h"
44 #if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
48 #undef RXDEBUG /* turn off debugging */
50 #if defined(AFS_SGI_ENV)
51 #include "sys/debug.h"
59 #endif /* AFS_OSF_ENV */
61 #include "afs/sysincludes.h"
62 #include "afsincludes.h"
65 #include "rx_kmutex.h"
66 #include "rx_kernel.h"
70 #include "rx_globals.h"
72 #include "rx_atomic.h"
73 #include "rx_internal.h"
74 #define AFSOP_STOP_RXCALLBACK 210 /* Stop CALLBACK process */
75 #define AFSOP_STOP_AFS 211 /* Stop AFS process */
76 #define AFSOP_STOP_BKG 212 /* Stop BKG process */
78 extern afs_int32 afs_termState;
80 #include "sys/lockl.h"
81 #include "sys/lock_def.h"
82 #endif /* AFS_AIX41_ENV */
83 # include "afs/rxgen_consts.h"
85 # include <sys/types.h>
95 # include <afs/afsutil.h>
96 # include <WINNT\afsreg.h>
98 # include <sys/socket.h>
99 # include <sys/file.h>
101 # include <sys/stat.h>
102 # include <netinet/in.h>
103 # include <sys/time.h>
106 # include "rx_user.h"
107 # include "rx_clock.h"
108 # include "rx_queue.h"
109 # include "rx_atomic.h"
110 # include "rx_globals.h"
111 # include "rx_trace.h"
112 # include "rx_internal.h"
113 # include <afs/rxgen_consts.h>
117 #ifdef AFS_PTHREAD_ENV
119 int (*registerProgram) (pid_t, char *) = 0;
120 int (*swapNameProgram) (pid_t, const char *, char *) = 0;
123 int (*registerProgram) (PROCESS, char *) = 0;
124 int (*swapNameProgram) (PROCESS, const char *, char *) = 0;
128 /* Local static routines */
129 static void rxi_DestroyConnectionNoLock(struct rx_connection *conn);
130 #ifdef RX_ENABLE_LOCKS
131 static void rxi_SetAcksInTransmitQueue(struct rx_call *call);
134 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
136 afs_int32 rxi_start_aborted; /* rxi_start awoke after rxi_Send in error. */
137 afs_int32 rxi_start_in_error;
139 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
142 * rxi_rpc_peer_stat_cnt counts the total number of peer stat structures
143 * currently allocated within rx. This number is used to allocate the
144 * memory required to return the statistics when queried.
145 * Protected by the rx_rpc_stats mutex.
148 static unsigned int rxi_rpc_peer_stat_cnt;
151 * rxi_rpc_process_stat_cnt counts the total number of local process stat
152 * structures currently allocated within rx. The number is used to allocate
153 * the memory required to return the statistics when queried.
154 * Protected by the rx_rpc_stats mutex.
157 static unsigned int rxi_rpc_process_stat_cnt;
159 rx_atomic_t rx_nWaiting = RX_ATOMIC_INIT(0);
160 rx_atomic_t rx_nWaited = RX_ATOMIC_INIT(0);
162 #if !defined(offsetof)
163 #include <stddef.h> /* for definition of offsetof() */
166 #ifdef RX_ENABLE_LOCKS
167 afs_kmutex_t rx_atomic_mutex;
170 #ifdef AFS_PTHREAD_ENV
174 * Use procedural initialization of mutexes/condition variables
178 extern afs_kmutex_t rx_stats_mutex;
179 extern afs_kmutex_t rx_quota_mutex;
180 extern afs_kmutex_t rx_pthread_mutex;
181 extern afs_kmutex_t rx_packets_mutex;
182 extern afs_kmutex_t des_init_mutex;
183 extern afs_kmutex_t des_random_mutex;
184 extern afs_kmutex_t rx_clock_mutex;
185 extern afs_kmutex_t rxi_connCacheMutex;
186 extern afs_kmutex_t rx_event_mutex;
187 extern afs_kmutex_t osi_malloc_mutex;
188 extern afs_kmutex_t event_handler_mutex;
189 extern afs_kmutex_t listener_mutex;
190 extern afs_kmutex_t rx_if_init_mutex;
191 extern afs_kmutex_t rx_if_mutex;
192 extern afs_kmutex_t rxkad_client_uid_mutex;
193 extern afs_kmutex_t rxkad_random_mutex;
195 extern afs_kcondvar_t rx_event_handler_cond;
196 extern afs_kcondvar_t rx_listener_cond;
198 static afs_kmutex_t epoch_mutex;
199 static afs_kmutex_t rx_init_mutex;
200 static afs_kmutex_t rx_debug_mutex;
201 static afs_kmutex_t rx_rpc_stats;
204 rxi_InitPthread(void)
206 MUTEX_INIT(&rx_clock_mutex, "clock", MUTEX_DEFAULT, 0);
207 MUTEX_INIT(&rx_stats_mutex, "stats", MUTEX_DEFAULT, 0);
208 MUTEX_INIT(&rx_atomic_mutex, "atomic", MUTEX_DEFAULT, 0);
209 MUTEX_INIT(&rx_quota_mutex, "quota", MUTEX_DEFAULT, 0);
210 MUTEX_INIT(&rx_pthread_mutex, "pthread", MUTEX_DEFAULT, 0);
211 MUTEX_INIT(&rx_packets_mutex, "packets", MUTEX_DEFAULT, 0);
212 MUTEX_INIT(&epoch_mutex, "epoch", MUTEX_DEFAULT, 0);
213 MUTEX_INIT(&rx_init_mutex, "init", MUTEX_DEFAULT, 0);
214 MUTEX_INIT(&rx_event_mutex, "event", MUTEX_DEFAULT, 0);
215 MUTEX_INIT(&des_init_mutex, "des", MUTEX_DEFAULT, 0);
216 MUTEX_INIT(&des_random_mutex, "random", MUTEX_DEFAULT, 0);
217 MUTEX_INIT(&osi_malloc_mutex, "malloc", MUTEX_DEFAULT, 0);
218 MUTEX_INIT(&event_handler_mutex, "event handler", MUTEX_DEFAULT, 0);
219 MUTEX_INIT(&rxi_connCacheMutex, "conn cache", MUTEX_DEFAULT, 0);
220 MUTEX_INIT(&listener_mutex, "listener", MUTEX_DEFAULT, 0);
221 MUTEX_INIT(&rx_if_init_mutex, "if init", MUTEX_DEFAULT, 0);
222 MUTEX_INIT(&rx_if_mutex, "if", MUTEX_DEFAULT, 0);
223 MUTEX_INIT(&rxkad_client_uid_mutex, "uid", MUTEX_DEFAULT, 0);
224 MUTEX_INIT(&rxkad_random_mutex, "rxkad random", MUTEX_DEFAULT, 0);
225 MUTEX_INIT(&rx_debug_mutex, "debug", MUTEX_DEFAULT, 0);
227 assert(pthread_cond_init
228 (&rx_event_handler_cond, (const pthread_condattr_t *)0) == 0);
229 assert(pthread_cond_init(&rx_listener_cond, (const pthread_condattr_t *)0)
231 assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
232 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 \
258 assert(pthread_once(&rx_once_init, rxi_InitPthread)==0)
260 * The rx_stats_mutex mutex protects the following global variables:
261 * rxi_lowConnRefCount
262 * rxi_lowPeerRefCount
271 * The rx_quota_mutex mutex protects the following global variables:
279 * The rx_freePktQ_lock protects the following global variables:
284 * The rx_packets_mutex mutex protects the following global variables:
292 * The rx_pthread_mutex mutex protects the following global variables:
293 * rxi_fcfs_thread_num
296 #define INIT_PTHREAD_LOCKS
300 /* Variables for handling the minProcs implementation. availProcs gives the
301 * number of threads available in the pool at this moment (not counting dudes
302 * executing right now). totalMin gives the total number of procs required
303 * for handling all minProcs requests. minDeficit is a dynamic variable
304 * tracking the # of procs required to satisfy all of the remaining minProcs
306 * For fine grain locking to work, the quota check and the reservation of
307 * a server thread has to come while rxi_availProcs and rxi_minDeficit
308 * are locked. To this end, the code has been modified under #ifdef
309 * RX_ENABLE_LOCKS so that quota checks and reservation occur at the
310 * same time. A new function, ReturnToServerPool() returns the allocation.
312 * A call can be on several queue's (but only one at a time). When
313 * rxi_ResetCall wants to remove the call from a queue, it has to ensure
314 * that no one else is touching the queue. To this end, we store the address
315 * of the queue lock in the call structure (under the call lock) when we
316 * put the call on a queue, and we clear the call_queue_lock when the
317 * call is removed from a queue (once the call lock has been obtained).
318 * This allows rxi_ResetCall to safely synchronize with others wishing
319 * to manipulate the queue.
322 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
323 static afs_kmutex_t rx_rpc_stats;
324 void rxi_StartUnlocked(struct rxevent *event, void *call,
325 void *arg1, int istack);
328 /* We keep a "last conn pointer" in rxi_FindConnection. The odds are
329 ** pretty good that the next packet coming in is from the same connection
330 ** as the last packet, since we're send multiple packets in a transmit window.
332 struct rx_connection *rxLastConn = 0;
334 #ifdef RX_ENABLE_LOCKS
335 /* The locking hierarchy for rx fine grain locking is composed of these
338 * rx_connHashTable_lock - synchronizes conn creation, rx_connHashTable access
339 * conn_call_lock - used to synchonize rx_EndCall and rx_NewCall
340 * call->lock - locks call data fields.
341 * These are independent of each other:
342 * rx_freeCallQueue_lock
347 * serverQueueEntry->lock
348 * rx_peerHashTable_lock - locked under rx_connHashTable_lock
350 * peer->lock - locks peer data fields.
351 * conn_data_lock - that more than one thread is not updating a conn data
352 * field at the same time.
361 * Do we need a lock to protect the peer field in the conn structure?
362 * conn->peer was previously a constant for all intents and so has no
363 * lock protecting this field. The multihomed client delta introduced
364 * a RX code change : change the peer field in the connection structure
365 * to that remote interface from which the last packet for this
366 * connection was sent out. This may become an issue if further changes
369 #define SET_CALL_QUEUE_LOCK(C, L) (C)->call_queue_lock = (L)
370 #define CLEAR_CALL_QUEUE_LOCK(C) (C)->call_queue_lock = NULL
372 /* rxdb_fileID is used to identify the lock location, along with line#. */
373 static int rxdb_fileID = RXDB_FILE_RX;
374 #endif /* RX_LOCKS_DB */
375 #else /* RX_ENABLE_LOCKS */
376 #define SET_CALL_QUEUE_LOCK(C, L)
377 #define CLEAR_CALL_QUEUE_LOCK(C)
378 #endif /* RX_ENABLE_LOCKS */
379 struct rx_serverQueueEntry *rx_waitForPacket = 0;
380 struct rx_serverQueueEntry *rx_waitingForPacket = 0;
382 /* ------------Exported Interfaces------------- */
384 /* This function allows rxkad to set the epoch to a suitably random number
385 * which rx_NewConnection will use in the future. The principle purpose is to
386 * get rxnull connections to use the same epoch as the rxkad connections do, at
387 * least once the first rxkad connection is established. This is important now
388 * that the host/port addresses aren't used in FindConnection: the uniqueness
389 * of epoch/cid matters and the start time won't do. */
391 #ifdef AFS_PTHREAD_ENV
393 * This mutex protects the following global variables:
397 #define LOCK_EPOCH MUTEX_ENTER(&epoch_mutex)
398 #define UNLOCK_EPOCH MUTEX_EXIT(&epoch_mutex)
402 #endif /* AFS_PTHREAD_ENV */
405 rx_SetEpoch(afs_uint32 epoch)
412 /* Initialize rx. A port number may be mentioned, in which case this
413 * becomes the default port number for any service installed later.
414 * If 0 is provided for the port number, a random port will be chosen
415 * by the kernel. Whether this will ever overlap anything in
416 * /etc/services is anybody's guess... Returns 0 on success, -1 on
421 int rxinit_status = 1;
422 #ifdef AFS_PTHREAD_ENV
424 * This mutex protects the following global variables:
428 #define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
429 #define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
432 #define UNLOCK_RX_INIT
436 rx_InitHost(u_int host, u_int port)
443 char *htable, *ptable;
450 if (rxinit_status == 0) {
451 tmp_status = rxinit_status;
453 return tmp_status; /* Already started; return previous error code. */
459 if (afs_winsockInit() < 0)
465 * Initialize anything necessary to provide a non-premptive threading
468 rxi_InitializeThreadSupport();
471 /* Allocate and initialize a socket for client and perhaps server
474 rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
475 if (rx_socket == OSI_NULLSOCKET) {
479 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
482 #endif /* RX_LOCKS_DB */
483 MUTEX_INIT(&rx_stats_mutex, "rx_stats_mutex", MUTEX_DEFAULT, 0);
484 MUTEX_INIT(&rx_quota_mutex, "rx_quota_mutex", MUTEX_DEFAULT, 0);
485 MUTEX_INIT(&rx_pthread_mutex, "rx_pthread_mutex", MUTEX_DEFAULT, 0);
486 MUTEX_INIT(&rx_packets_mutex, "rx_packets_mutex", MUTEX_DEFAULT, 0);
487 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
488 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
489 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
490 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
492 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
494 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
496 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
498 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
499 #if defined(AFS_HPUX110_ENV)
501 rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
502 #endif /* AFS_HPUX110_ENV */
503 #endif /* RX_ENABLE_LOCKS && KERNEL */
506 rx_connDeadTime = 12;
507 rx_tranquil = 0; /* reset flag */
508 memset(&rx_stats, 0, sizeof(struct rx_statistics));
510 osi_Alloc(rx_hashTableSize * sizeof(struct rx_connection *));
511 PIN(htable, rx_hashTableSize * sizeof(struct rx_connection *)); /* XXXXX */
512 memset(htable, 0, rx_hashTableSize * sizeof(struct rx_connection *));
513 ptable = (char *)osi_Alloc(rx_hashTableSize * sizeof(struct rx_peer *));
514 PIN(ptable, rx_hashTableSize * sizeof(struct rx_peer *)); /* XXXXX */
515 memset(ptable, 0, rx_hashTableSize * sizeof(struct rx_peer *));
517 /* Malloc up a bunch of packets & buffers */
519 queue_Init(&rx_freePacketQueue);
520 rxi_NeedMorePackets = FALSE;
521 rx_nPackets = 0; /* rx_nPackets is managed by rxi_MorePackets* */
523 /* enforce a minimum number of allocated packets */
524 if (rx_extraPackets < rxi_nSendFrags * rx_maxSendWindow)
525 rx_extraPackets = rxi_nSendFrags * rx_maxSendWindow;
527 /* allocate the initial free packet pool */
528 #ifdef RX_ENABLE_TSFPQ
529 rxi_MorePacketsTSFPQ(rx_extraPackets + RX_MAX_QUOTA + 2, RX_TS_FPQ_FLUSH_GLOBAL, 0);
530 #else /* RX_ENABLE_TSFPQ */
531 rxi_MorePackets(rx_extraPackets + RX_MAX_QUOTA + 2); /* fudge */
532 #endif /* RX_ENABLE_TSFPQ */
539 #if defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
540 tv.tv_sec = clock_now.sec;
541 tv.tv_usec = clock_now.usec;
542 srand((unsigned int)tv.tv_usec);
549 #if defined(KERNEL) && !defined(UKERNEL)
550 /* Really, this should never happen in a real kernel */
553 struct sockaddr_in addr;
555 int addrlen = sizeof(addr);
557 socklen_t addrlen = sizeof(addr);
559 if (getsockname((intptr_t)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
563 rx_port = addr.sin_port;
566 rx_stats.minRtt.sec = 9999999;
568 rx_SetEpoch(tv.tv_sec | 0x80000000);
570 rx_SetEpoch(tv.tv_sec); /* Start time of this package, rxkad
571 * will provide a randomer value. */
573 MUTEX_ENTER(&rx_quota_mutex);
574 rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
575 MUTEX_EXIT(&rx_quota_mutex);
576 /* *Slightly* random start time for the cid. This is just to help
577 * out with the hashing function at the peer */
578 rx_nextCid = ((tv.tv_sec ^ tv.tv_usec) << RX_CIDSHIFT);
579 rx_connHashTable = (struct rx_connection **)htable;
580 rx_peerHashTable = (struct rx_peer **)ptable;
582 rx_lastAckDelay.sec = 0;
583 rx_lastAckDelay.usec = 400000; /* 400 milliseconds */
584 rx_hardAckDelay.sec = 0;
585 rx_hardAckDelay.usec = 100000; /* 100 milliseconds */
586 rx_softAckDelay.sec = 0;
587 rx_softAckDelay.usec = 100000; /* 100 milliseconds */
589 rxevent_Init(20, rxi_ReScheduleEvents);
591 /* Initialize various global queues */
592 queue_Init(&rx_idleServerQueue);
593 queue_Init(&rx_incomingCallQueue);
594 queue_Init(&rx_freeCallQueue);
596 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
597 /* Initialize our list of usable IP addresses. */
601 /* Start listener process (exact function is dependent on the
602 * implementation environment--kernel or user space) */
606 tmp_status = rxinit_status = 0;
614 return rx_InitHost(htonl(INADDR_ANY), port);
617 /* called with unincremented nRequestsRunning to see if it is OK to start
618 * a new thread in this service. Could be "no" for two reasons: over the
619 * max quota, or would prevent others from reaching their min quota.
621 #ifdef RX_ENABLE_LOCKS
622 /* This verion of QuotaOK reserves quota if it's ok while the
623 * rx_serverPool_lock is held. Return quota using ReturnToServerPool().
626 QuotaOK(struct rx_service *aservice)
628 /* check if over max quota */
629 if (aservice->nRequestsRunning >= aservice->maxProcs) {
633 /* under min quota, we're OK */
634 /* otherwise, can use only if there are enough to allow everyone
635 * to go to their min quota after this guy starts.
638 MUTEX_ENTER(&rx_quota_mutex);
639 if ((aservice->nRequestsRunning < aservice->minProcs)
640 || (rxi_availProcs > rxi_minDeficit)) {
641 aservice->nRequestsRunning++;
642 /* just started call in minProcs pool, need fewer to maintain
644 if (aservice->nRequestsRunning <= aservice->minProcs)
647 MUTEX_EXIT(&rx_quota_mutex);
650 MUTEX_EXIT(&rx_quota_mutex);
656 ReturnToServerPool(struct rx_service *aservice)
658 aservice->nRequestsRunning--;
659 MUTEX_ENTER(&rx_quota_mutex);
660 if (aservice->nRequestsRunning < aservice->minProcs)
663 MUTEX_EXIT(&rx_quota_mutex);
666 #else /* RX_ENABLE_LOCKS */
668 QuotaOK(struct rx_service *aservice)
671 /* under min quota, we're OK */
672 if (aservice->nRequestsRunning < aservice->minProcs)
675 /* check if over max quota */
676 if (aservice->nRequestsRunning >= aservice->maxProcs)
679 /* otherwise, can use only if there are enough to allow everyone
680 * to go to their min quota after this guy starts.
682 MUTEX_ENTER(&rx_quota_mutex);
683 if (rxi_availProcs > rxi_minDeficit)
685 MUTEX_EXIT(&rx_quota_mutex);
688 #endif /* RX_ENABLE_LOCKS */
691 /* Called by rx_StartServer to start up lwp's to service calls.
692 NExistingProcs gives the number of procs already existing, and which
693 therefore needn't be created. */
695 rxi_StartServerProcs(int nExistingProcs)
697 struct rx_service *service;
702 /* For each service, reserve N processes, where N is the "minimum"
703 * number of processes that MUST be able to execute a request in parallel,
704 * at any time, for that process. Also compute the maximum difference
705 * between any service's maximum number of processes that can run
706 * (i.e. the maximum number that ever will be run, and a guarantee
707 * that this number will run if other services aren't running), and its
708 * minimum number. The result is the extra number of processes that
709 * we need in order to provide the latter guarantee */
710 for (i = 0; i < RX_MAX_SERVICES; i++) {
712 service = rx_services[i];
713 if (service == (struct rx_service *)0)
715 nProcs += service->minProcs;
716 diff = service->maxProcs - service->minProcs;
720 nProcs += maxdiff; /* Extra processes needed to allow max number requested to run in any given service, under good conditions */
721 nProcs -= nExistingProcs; /* Subtract the number of procs that were previously created for use as server procs */
722 for (i = 0; i < nProcs; i++) {
723 rxi_StartServerProc(rx_ServerProc, rx_stackSize);
729 /* This routine is only required on Windows */
731 rx_StartClientThread(void)
733 #ifdef AFS_PTHREAD_ENV
735 pid = pthread_self();
736 #endif /* AFS_PTHREAD_ENV */
738 #endif /* AFS_NT40_ENV */
740 /* This routine must be called if any services are exported. If the
741 * donateMe flag is set, the calling process is donated to the server
744 rx_StartServer(int donateMe)
746 struct rx_service *service;
752 /* Start server processes, if necessary (exact function is dependent
753 * on the implementation environment--kernel or user space). DonateMe
754 * will be 1 if there is 1 pre-existing proc, i.e. this one. In this
755 * case, one less new proc will be created rx_StartServerProcs.
757 rxi_StartServerProcs(donateMe);
759 /* count up the # of threads in minProcs, and add set the min deficit to
760 * be that value, too.
762 for (i = 0; i < RX_MAX_SERVICES; i++) {
763 service = rx_services[i];
764 if (service == (struct rx_service *)0)
766 MUTEX_ENTER(&rx_quota_mutex);
767 rxi_totalMin += service->minProcs;
768 /* below works even if a thread is running, since minDeficit would
769 * still have been decremented and later re-incremented.
771 rxi_minDeficit += service->minProcs;
772 MUTEX_EXIT(&rx_quota_mutex);
775 /* Turn on reaping of idle server connections */
776 rxi_ReapConnections(NULL, NULL, NULL);
785 #ifdef AFS_PTHREAD_ENV
787 pid = afs_pointer_to_int(pthread_self());
788 #else /* AFS_PTHREAD_ENV */
790 LWP_CurrentProcess(&pid);
791 #endif /* AFS_PTHREAD_ENV */
793 sprintf(name, "srv_%d", ++nProcs);
795 (*registerProgram) (pid, name);
797 #endif /* AFS_NT40_ENV */
798 rx_ServerProc(NULL); /* Never returns */
800 #ifdef RX_ENABLE_TSFPQ
801 /* no use leaving packets around in this thread's local queue if
802 * it isn't getting donated to the server thread pool.
804 rxi_FlushLocalPacketsTSFPQ();
805 #endif /* RX_ENABLE_TSFPQ */
809 /* Create a new client connection to the specified service, using the
810 * specified security object to implement the security model for this
812 struct rx_connection *
813 rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
814 struct rx_securityClass *securityObject,
815 int serviceSecurityIndex)
819 struct rx_connection *conn;
824 dpf(("rx_NewConnection(host %x, port %u, service %u, securityObject %p, "
825 "serviceSecurityIndex %d)\n",
826 ntohl(shost), ntohs(sport), sservice, securityObject,
827 serviceSecurityIndex));
829 /* Vasilsi said: "NETPRI protects Cid and Alloc", but can this be true in
830 * the case of kmem_alloc? */
831 conn = rxi_AllocConnection();
832 #ifdef RX_ENABLE_LOCKS
833 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
834 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
835 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
838 MUTEX_ENTER(&rx_connHashTable_lock);
839 cid = (rx_nextCid += RX_MAXCALLS);
840 conn->type = RX_CLIENT_CONNECTION;
842 conn->epoch = rx_epoch;
843 conn->peer = rxi_FindPeer(shost, sport, 0, 1);
844 conn->serviceId = sservice;
845 conn->securityObject = securityObject;
846 conn->securityData = (void *) 0;
847 conn->securityIndex = serviceSecurityIndex;
848 rx_SetConnDeadTime(conn, rx_connDeadTime);
849 rx_SetConnSecondsUntilNatPing(conn, 0);
850 conn->ackRate = RX_FAST_ACK_RATE;
852 conn->specific = NULL;
853 conn->challengeEvent = NULL;
854 conn->delayedAbortEvent = NULL;
855 conn->abortCount = 0;
857 for (i = 0; i < RX_MAXCALLS; i++) {
858 conn->twind[i] = rx_initSendWindow;
859 conn->rwind[i] = rx_initReceiveWindow;
862 RXS_NewConnection(securityObject, conn);
864 CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
866 conn->refCount++; /* no lock required since only this thread knows... */
867 conn->next = rx_connHashTable[hashindex];
868 rx_connHashTable[hashindex] = conn;
870 rx_MutexIncrement(rx_stats.nClientConns, rx_stats_mutex);
871 MUTEX_EXIT(&rx_connHashTable_lock);
877 rx_SetConnDeadTime(struct rx_connection *conn, int seconds)
879 /* The idea is to set the dead time to a value that allows several
880 * keepalives to be dropped without timing out the connection. */
881 conn->secondsUntilDead = MAX(seconds, 6);
882 conn->secondsUntilPing = conn->secondsUntilDead / 6;
885 int rxi_lowPeerRefCount = 0;
886 int rxi_lowConnRefCount = 0;
889 * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
890 * NOTE: must not be called with rx_connHashTable_lock held.
893 rxi_CleanupConnection(struct rx_connection *conn)
895 /* Notify the service exporter, if requested, that this connection
896 * is being destroyed */
897 if (conn->type == RX_SERVER_CONNECTION && conn->service->destroyConnProc)
898 (*conn->service->destroyConnProc) (conn);
900 /* Notify the security module that this connection is being destroyed */
901 RXS_DestroyConnection(conn->securityObject, conn);
903 /* If this is the last connection using the rx_peer struct, set its
904 * idle time to now. rxi_ReapConnections will reap it if it's still
905 * idle (refCount == 0) after rx_idlePeerTime (60 seconds) have passed.
907 MUTEX_ENTER(&rx_peerHashTable_lock);
908 if (conn->peer->refCount < 2) {
909 conn->peer->idleWhen = clock_Sec();
910 if (conn->peer->refCount < 1) {
911 conn->peer->refCount = 1;
912 if (rx_stats_active) {
913 MUTEX_ENTER(&rx_stats_mutex);
914 rxi_lowPeerRefCount++;
915 MUTEX_EXIT(&rx_stats_mutex);
919 conn->peer->refCount--;
920 MUTEX_EXIT(&rx_peerHashTable_lock);
924 if (conn->type == RX_SERVER_CONNECTION)
925 rx_MutexDecrement(rx_stats.nServerConns, rx_stats_mutex);
927 rx_MutexDecrement(rx_stats.nClientConns, rx_stats_mutex);
930 if (conn->specific) {
932 for (i = 0; i < conn->nSpecific; i++) {
933 if (conn->specific[i] && rxi_keyCreate_destructor[i])
934 (*rxi_keyCreate_destructor[i]) (conn->specific[i]);
935 conn->specific[i] = NULL;
937 free(conn->specific);
939 conn->specific = NULL;
943 MUTEX_DESTROY(&conn->conn_call_lock);
944 MUTEX_DESTROY(&conn->conn_data_lock);
945 CV_DESTROY(&conn->conn_call_cv);
947 rxi_FreeConnection(conn);
950 /* Destroy the specified connection */
952 rxi_DestroyConnection(struct rx_connection *conn)
954 MUTEX_ENTER(&rx_connHashTable_lock);
955 rxi_DestroyConnectionNoLock(conn);
956 /* conn should be at the head of the cleanup list */
957 if (conn == rx_connCleanup_list) {
958 rx_connCleanup_list = rx_connCleanup_list->next;
959 MUTEX_EXIT(&rx_connHashTable_lock);
960 rxi_CleanupConnection(conn);
962 #ifdef RX_ENABLE_LOCKS
964 MUTEX_EXIT(&rx_connHashTable_lock);
966 #endif /* RX_ENABLE_LOCKS */
970 rxi_DestroyConnectionNoLock(struct rx_connection *conn)
972 struct rx_connection **conn_ptr;
974 struct rx_packet *packet;
981 MUTEX_ENTER(&conn->conn_data_lock);
982 if (conn->refCount > 0)
985 if (rx_stats_active) {
986 MUTEX_ENTER(&rx_stats_mutex);
987 rxi_lowConnRefCount++;
988 MUTEX_EXIT(&rx_stats_mutex);
992 if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
993 /* Busy; wait till the last guy before proceeding */
994 MUTEX_EXIT(&conn->conn_data_lock);
999 /* If the client previously called rx_NewCall, but it is still
1000 * waiting, treat this as a running call, and wait to destroy the
1001 * connection later when the call completes. */
1002 if ((conn->type == RX_CLIENT_CONNECTION)
1003 && (conn->flags & (RX_CONN_MAKECALL_WAITING|RX_CONN_MAKECALL_ACTIVE))) {
1004 conn->flags |= RX_CONN_DESTROY_ME;
1005 MUTEX_EXIT(&conn->conn_data_lock);
1009 MUTEX_EXIT(&conn->conn_data_lock);
1011 /* Check for extant references to this connection */
1012 for (i = 0; i < RX_MAXCALLS; i++) {
1013 struct rx_call *call = conn->call[i];
1016 if (conn->type == RX_CLIENT_CONNECTION) {
1017 MUTEX_ENTER(&call->lock);
1018 if (call->delayedAckEvent) {
1019 /* Push the final acknowledgment out now--there
1020 * won't be a subsequent call to acknowledge the
1021 * last reply packets */
1022 rxevent_Cancel(call->delayedAckEvent, call,
1023 RX_CALL_REFCOUNT_DELAY);
1024 if (call->state == RX_STATE_PRECALL
1025 || call->state == RX_STATE_ACTIVE) {
1026 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1028 rxi_AckAll(NULL, call, 0);
1031 MUTEX_EXIT(&call->lock);
1035 #ifdef RX_ENABLE_LOCKS
1037 if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
1038 MUTEX_EXIT(&conn->conn_data_lock);
1040 /* Someone is accessing a packet right now. */
1044 #endif /* RX_ENABLE_LOCKS */
1047 /* Don't destroy the connection if there are any call
1048 * structures still in use */
1049 MUTEX_ENTER(&conn->conn_data_lock);
1050 conn->flags |= RX_CONN_DESTROY_ME;
1051 MUTEX_EXIT(&conn->conn_data_lock);
1056 if (conn->natKeepAliveEvent) {
1057 rxi_NatKeepAliveOff(conn);
1060 if (conn->delayedAbortEvent) {
1061 rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
1062 packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
1064 MUTEX_ENTER(&conn->conn_data_lock);
1065 rxi_SendConnectionAbort(conn, packet, 0, 1);
1066 MUTEX_EXIT(&conn->conn_data_lock);
1067 rxi_FreePacket(packet);
1071 /* Remove from connection hash table before proceeding */
1073 &rx_connHashTable[CONN_HASH
1074 (peer->host, peer->port, conn->cid, conn->epoch,
1076 for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
1077 if (*conn_ptr == conn) {
1078 *conn_ptr = conn->next;
1082 /* if the conn that we are destroying was the last connection, then we
1083 * clear rxLastConn as well */
1084 if (rxLastConn == conn)
1087 /* Make sure the connection is completely reset before deleting it. */
1088 /* get rid of pending events that could zap us later */
1089 if (conn->challengeEvent)
1090 rxevent_Cancel(conn->challengeEvent, (struct rx_call *)0, 0);
1091 if (conn->checkReachEvent)
1092 rxevent_Cancel(conn->checkReachEvent, (struct rx_call *)0, 0);
1093 if (conn->natKeepAliveEvent)
1094 rxevent_Cancel(conn->natKeepAliveEvent, (struct rx_call *)0, 0);
1096 /* Add the connection to the list of destroyed connections that
1097 * need to be cleaned up. This is necessary to avoid deadlocks
1098 * in the routines we call to inform others that this connection is
1099 * being destroyed. */
1100 conn->next = rx_connCleanup_list;
1101 rx_connCleanup_list = conn;
1104 /* Externally available version */
1106 rx_DestroyConnection(struct rx_connection *conn)
1111 rxi_DestroyConnection(conn);
1116 rx_GetConnection(struct rx_connection *conn)
1121 MUTEX_ENTER(&conn->conn_data_lock);
1123 MUTEX_EXIT(&conn->conn_data_lock);
1127 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1128 /* Wait for the transmit queue to no longer be busy.
1129 * requires the call->lock to be held */
1130 static void rxi_WaitforTQBusy(struct rx_call *call) {
1131 while (call->flags & RX_CALL_TQ_BUSY) {
1132 call->flags |= RX_CALL_TQ_WAIT;
1134 #ifdef RX_ENABLE_LOCKS
1135 osirx_AssertMine(&call->lock, "rxi_WaitforTQ lock");
1136 CV_WAIT(&call->cv_tq, &call->lock);
1137 #else /* RX_ENABLE_LOCKS */
1138 osi_rxSleep(&call->tq);
1139 #endif /* RX_ENABLE_LOCKS */
1141 if (call->tqWaiters == 0) {
1142 call->flags &= ~RX_CALL_TQ_WAIT;
1148 /* Start a new rx remote procedure call, on the specified connection.
1149 * If wait is set to 1, wait for a free call channel; otherwise return
1150 * 0. Maxtime gives the maximum number of seconds this call may take,
1151 * after rx_NewCall returns. After this time interval, a call to any
1152 * of rx_SendData, rx_ReadData, etc. will fail with RX_CALL_TIMEOUT.
1153 * For fine grain locking, we hold the conn_call_lock in order to
1154 * to ensure that we don't get signalle after we found a call in an active
1155 * state and before we go to sleep.
1158 rx_NewCall(struct rx_connection *conn)
1161 struct rx_call *call;
1162 struct clock queueTime;
1166 dpf(("rx_NewCall(conn %"AFS_PTR_FMT")\n", conn));
1169 clock_GetTime(&queueTime);
1171 * Check if there are others waiting for a new call.
1172 * If so, let them go first to avoid starving them.
1173 * This is a fairly simple scheme, and might not be
1174 * a complete solution for large numbers of waiters.
1176 * makeCallWaiters keeps track of the number of
1177 * threads waiting to make calls and the
1178 * RX_CONN_MAKECALL_WAITING flag bit is used to
1179 * indicate that there are indeed calls waiting.
1180 * The flag is set when the waiter is incremented.
1181 * It is only cleared when makeCallWaiters is 0.
1182 * This prevents us from accidently destroying the
1183 * connection while it is potentially about to be used.
1185 MUTEX_ENTER(&conn->conn_call_lock);
1186 MUTEX_ENTER(&conn->conn_data_lock);
1187 while (conn->flags & RX_CONN_MAKECALL_ACTIVE) {
1188 conn->flags |= RX_CONN_MAKECALL_WAITING;
1189 conn->makeCallWaiters++;
1190 MUTEX_EXIT(&conn->conn_data_lock);
1192 #ifdef RX_ENABLE_LOCKS
1193 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1197 MUTEX_ENTER(&conn->conn_data_lock);
1198 conn->makeCallWaiters--;
1199 if (conn->makeCallWaiters == 0)
1200 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1203 /* We are now the active thread in rx_NewCall */
1204 conn->flags |= RX_CONN_MAKECALL_ACTIVE;
1205 MUTEX_EXIT(&conn->conn_data_lock);
1210 for (i = 0; i < RX_MAXCALLS; i++) {
1211 call = conn->call[i];
1213 if (call->state == RX_STATE_DALLY) {
1214 MUTEX_ENTER(&call->lock);
1215 if (call->state == RX_STATE_DALLY) {
1217 * We are setting the state to RX_STATE_RESET to
1218 * ensure that no one else will attempt to use this
1219 * call once we drop the conn->conn_call_lock and
1220 * call->lock. We must drop the conn->conn_call_lock
1221 * before calling rxi_ResetCall because the process
1222 * of clearing the transmit queue can block for an
1223 * extended period of time. If we block while holding
1224 * the conn->conn_call_lock, then all rx_EndCall
1225 * processing will block as well. This has a detrimental
1226 * effect on overall system performance.
1228 call->state = RX_STATE_RESET;
1229 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1230 MUTEX_EXIT(&conn->conn_call_lock);
1231 rxi_ResetCall(call, 0);
1232 (*call->callNumber)++;
1233 if (MUTEX_TRYENTER(&conn->conn_call_lock))
1237 * If we failed to be able to safely obtain the
1238 * conn->conn_call_lock we will have to drop the
1239 * call->lock to avoid a deadlock. When the call->lock
1240 * is released the state of the call can change. If it
1241 * is no longer RX_STATE_RESET then some other thread is
1244 MUTEX_EXIT(&call->lock);
1245 MUTEX_ENTER(&conn->conn_call_lock);
1246 MUTEX_ENTER(&call->lock);
1248 if (call->state == RX_STATE_RESET)
1252 * If we get here it means that after dropping
1253 * the conn->conn_call_lock and call->lock that
1254 * the call is no longer ours. If we can't find
1255 * a free call in the remaining slots we should
1256 * not go immediately to RX_CONN_MAKECALL_WAITING
1257 * because by dropping the conn->conn_call_lock
1258 * we have given up synchronization with rx_EndCall.
1259 * Instead, cycle through one more time to see if
1260 * we can find a call that can call our own.
1262 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
1265 MUTEX_EXIT(&call->lock);
1268 /* rxi_NewCall returns with mutex locked */
1269 call = rxi_NewCall(conn, i);
1270 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1274 if (i < RX_MAXCALLS) {
1280 MUTEX_ENTER(&conn->conn_data_lock);
1281 conn->flags |= RX_CONN_MAKECALL_WAITING;
1282 conn->makeCallWaiters++;
1283 MUTEX_EXIT(&conn->conn_data_lock);
1285 #ifdef RX_ENABLE_LOCKS
1286 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1290 MUTEX_ENTER(&conn->conn_data_lock);
1291 conn->makeCallWaiters--;
1292 if (conn->makeCallWaiters == 0)
1293 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1294 MUTEX_EXIT(&conn->conn_data_lock);
1296 /* Client is initially in send mode */
1297 call->state = RX_STATE_ACTIVE;
1298 call->error = conn->error;
1300 call->mode = RX_MODE_ERROR;
1302 call->mode = RX_MODE_SENDING;
1304 /* remember start time for call in case we have hard dead time limit */
1305 call->queueTime = queueTime;
1306 clock_GetTime(&call->startTime);
1307 hzero(call->bytesSent);
1308 hzero(call->bytesRcvd);
1310 /* Turn on busy protocol. */
1311 rxi_KeepAliveOn(call);
1313 /* Attempt MTU discovery */
1314 rxi_GrowMTUOn(call);
1317 * We are no longer the active thread in rx_NewCall
1319 MUTEX_ENTER(&conn->conn_data_lock);
1320 conn->flags &= ~RX_CONN_MAKECALL_ACTIVE;
1321 MUTEX_EXIT(&conn->conn_data_lock);
1324 * Wake up anyone else who might be giving us a chance to
1325 * run (see code above that avoids resource starvation).
1327 #ifdef RX_ENABLE_LOCKS
1328 CV_BROADCAST(&conn->conn_call_cv);
1332 MUTEX_EXIT(&conn->conn_call_lock);
1334 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1335 if (call->flags & (RX_CALL_TQ_BUSY | RX_CALL_TQ_CLEARME)) {
1336 osi_Panic("rx_NewCall call about to be used without an empty tq");
1338 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1340 MUTEX_EXIT(&call->lock);
1343 dpf(("rx_NewCall(call %"AFS_PTR_FMT")\n", call));
1348 rxi_HasActiveCalls(struct rx_connection *aconn)
1351 struct rx_call *tcall;
1355 for (i = 0; i < RX_MAXCALLS; i++) {
1356 if ((tcall = aconn->call[i])) {
1357 if ((tcall->state == RX_STATE_ACTIVE)
1358 || (tcall->state == RX_STATE_PRECALL)) {
1369 rxi_GetCallNumberVector(struct rx_connection *aconn,
1370 afs_int32 * aint32s)
1373 struct rx_call *tcall;
1377 for (i = 0; i < RX_MAXCALLS; i++) {
1378 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1379 aint32s[i] = aconn->callNumber[i] + 1;
1381 aint32s[i] = aconn->callNumber[i];
1388 rxi_SetCallNumberVector(struct rx_connection *aconn,
1389 afs_int32 * aint32s)
1392 struct rx_call *tcall;
1396 for (i = 0; i < RX_MAXCALLS; i++) {
1397 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1398 aconn->callNumber[i] = aint32s[i] - 1;
1400 aconn->callNumber[i] = aint32s[i];
1406 /* Advertise a new service. A service is named locally by a UDP port
1407 * number plus a 16-bit service id. Returns (struct rx_service *) 0
1410 char *serviceName; Name for identification purposes (e.g. the
1411 service name might be used for probing for
1414 rx_NewServiceHost(afs_uint32 host, u_short port, u_short serviceId,
1415 char *serviceName, struct rx_securityClass **securityObjects,
1416 int nSecurityObjects,
1417 afs_int32(*serviceProc) (struct rx_call * acall))
1419 osi_socket socket = OSI_NULLSOCKET;
1420 struct rx_service *tservice;
1426 if (serviceId == 0) {
1428 "rx_NewService: service id for service %s is not non-zero.\n",
1435 "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",
1443 tservice = rxi_AllocService();
1446 #ifdef RX_ENABLE_LOCKS
1447 MUTEX_INIT(&tservice->svc_data_lock, "svc data lock", MUTEX_DEFAULT, 0);
1450 for (i = 0; i < RX_MAX_SERVICES; i++) {
1451 struct rx_service *service = rx_services[i];
1453 if (port == service->servicePort && host == service->serviceHost) {
1454 if (service->serviceId == serviceId) {
1455 /* The identical service has already been
1456 * installed; if the caller was intending to
1457 * change the security classes used by this
1458 * service, he/she loses. */
1460 "rx_NewService: tried to install service %s with service id %d, which is already in use for service %s\n",
1461 serviceName, serviceId, service->serviceName);
1463 rxi_FreeService(tservice);
1466 /* Different service, same port: re-use the socket
1467 * which is bound to the same port */
1468 socket = service->socket;
1471 if (socket == OSI_NULLSOCKET) {
1472 /* If we don't already have a socket (from another
1473 * service on same port) get a new one */
1474 socket = rxi_GetHostUDPSocket(host, port);
1475 if (socket == OSI_NULLSOCKET) {
1477 rxi_FreeService(tservice);
1482 service->socket = socket;
1483 service->serviceHost = host;
1484 service->servicePort = port;
1485 service->serviceId = serviceId;
1486 service->serviceName = serviceName;
1487 service->nSecurityObjects = nSecurityObjects;
1488 service->securityObjects = securityObjects;
1489 service->minProcs = 0;
1490 service->maxProcs = 1;
1491 service->idleDeadTime = 60;
1492 service->idleDeadErr = 0;
1493 service->connDeadTime = rx_connDeadTime;
1494 service->executeRequestProc = serviceProc;
1495 service->checkReach = 0;
1496 service->nSpecific = 0;
1497 service->specific = NULL;
1498 rx_services[i] = service; /* not visible until now */
1504 rxi_FreeService(tservice);
1505 (osi_Msg "rx_NewService: cannot support > %d services\n",
1510 /* Set configuration options for all of a service's security objects */
1513 rx_SetSecurityConfiguration(struct rx_service *service,
1514 rx_securityConfigVariables type,
1518 for (i = 0; i<service->nSecurityObjects; i++) {
1519 if (service->securityObjects[i]) {
1520 RXS_SetConfiguration(service->securityObjects[i], NULL, type,
1528 rx_NewService(u_short port, u_short serviceId, char *serviceName,
1529 struct rx_securityClass **securityObjects, int nSecurityObjects,
1530 afs_int32(*serviceProc) (struct rx_call * acall))
1532 return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
1535 /* Generic request processing loop. This routine should be called
1536 * by the implementation dependent rx_ServerProc. If socketp is
1537 * non-null, it will be set to the file descriptor that this thread
1538 * is now listening on. If socketp is null, this routine will never
1541 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1543 struct rx_call *call;
1545 struct rx_service *tservice = NULL;
1552 call = rx_GetCall(threadID, tservice, socketp);
1553 if (socketp && *socketp != OSI_NULLSOCKET) {
1554 /* We are now a listener thread */
1559 /* if server is restarting( typically smooth shutdown) then do not
1560 * allow any new calls.
1563 if (rx_tranquil && (call != NULL)) {
1567 MUTEX_ENTER(&call->lock);
1569 rxi_CallError(call, RX_RESTARTING);
1570 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1572 MUTEX_EXIT(&call->lock);
1576 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1577 #ifdef RX_ENABLE_LOCKS
1579 #endif /* RX_ENABLE_LOCKS */
1580 afs_termState = AFSOP_STOP_AFS;
1581 afs_osi_Wakeup(&afs_termState);
1582 #ifdef RX_ENABLE_LOCKS
1584 #endif /* RX_ENABLE_LOCKS */
1589 tservice = call->conn->service;
1591 if (tservice->beforeProc)
1592 (*tservice->beforeProc) (call);
1594 code = tservice->executeRequestProc(call);
1596 if (tservice->afterProc)
1597 (*tservice->afterProc) (call, code);
1599 rx_EndCall(call, code);
1600 if (rx_stats_active) {
1601 MUTEX_ENTER(&rx_stats_mutex);
1603 MUTEX_EXIT(&rx_stats_mutex);
1610 rx_WakeupServerProcs(void)
1612 struct rx_serverQueueEntry *np, *tqp;
1616 MUTEX_ENTER(&rx_serverPool_lock);
1618 #ifdef RX_ENABLE_LOCKS
1619 if (rx_waitForPacket)
1620 CV_BROADCAST(&rx_waitForPacket->cv);
1621 #else /* RX_ENABLE_LOCKS */
1622 if (rx_waitForPacket)
1623 osi_rxWakeup(rx_waitForPacket);
1624 #endif /* RX_ENABLE_LOCKS */
1625 MUTEX_ENTER(&freeSQEList_lock);
1626 for (np = rx_FreeSQEList; np; np = tqp) {
1627 tqp = *(struct rx_serverQueueEntry **)np;
1628 #ifdef RX_ENABLE_LOCKS
1629 CV_BROADCAST(&np->cv);
1630 #else /* RX_ENABLE_LOCKS */
1632 #endif /* RX_ENABLE_LOCKS */
1634 MUTEX_EXIT(&freeSQEList_lock);
1635 for (queue_Scan(&rx_idleServerQueue, np, tqp, rx_serverQueueEntry)) {
1636 #ifdef RX_ENABLE_LOCKS
1637 CV_BROADCAST(&np->cv);
1638 #else /* RX_ENABLE_LOCKS */
1640 #endif /* RX_ENABLE_LOCKS */
1642 MUTEX_EXIT(&rx_serverPool_lock);
1647 * One thing that seems to happen is that all the server threads get
1648 * tied up on some empty or slow call, and then a whole bunch of calls
1649 * arrive at once, using up the packet pool, so now there are more
1650 * empty calls. The most critical resources here are server threads
1651 * and the free packet pool. The "doreclaim" code seems to help in
1652 * general. I think that eventually we arrive in this state: there
1653 * are lots of pending calls which do have all their packets present,
1654 * so they won't be reclaimed, are multi-packet calls, so they won't
1655 * be scheduled until later, and thus are tying up most of the free
1656 * packet pool for a very long time.
1658 * 1. schedule multi-packet calls if all the packets are present.
1659 * Probably CPU-bound operation, useful to return packets to pool.
1660 * Do what if there is a full window, but the last packet isn't here?
1661 * 3. preserve one thread which *only* runs "best" calls, otherwise
1662 * it sleeps and waits for that type of call.
1663 * 4. Don't necessarily reserve a whole window for each thread. In fact,
1664 * the current dataquota business is badly broken. The quota isn't adjusted
1665 * to reflect how many packets are presently queued for a running call.
1666 * So, when we schedule a queued call with a full window of packets queued
1667 * up for it, that *should* free up a window full of packets for other 2d-class
1668 * calls to be able to use from the packet pool. But it doesn't.
1670 * NB. Most of the time, this code doesn't run -- since idle server threads
1671 * sit on the idle server queue and are assigned by "...ReceivePacket" as soon
1672 * as a new call arrives.
1674 /* Sleep until a call arrives. Returns a pointer to the call, ready
1675 * for an rx_Read. */
1676 #ifdef RX_ENABLE_LOCKS
1678 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1680 struct rx_serverQueueEntry *sq;
1681 struct rx_call *call = (struct rx_call *)0;
1682 struct rx_service *service = NULL;
1685 MUTEX_ENTER(&freeSQEList_lock);
1687 if ((sq = rx_FreeSQEList)) {
1688 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1689 MUTEX_EXIT(&freeSQEList_lock);
1690 } else { /* otherwise allocate a new one and return that */
1691 MUTEX_EXIT(&freeSQEList_lock);
1692 sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1693 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1694 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1697 MUTEX_ENTER(&rx_serverPool_lock);
1698 if (cur_service != NULL) {
1699 ReturnToServerPool(cur_service);
1702 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1703 struct rx_call *tcall, *ncall, *choice2 = NULL;
1705 /* Scan for eligible incoming calls. A call is not eligible
1706 * if the maximum number of calls for its service type are
1707 * already executing */
1708 /* One thread will process calls FCFS (to prevent starvation),
1709 * while the other threads may run ahead looking for calls which
1710 * have all their input data available immediately. This helps
1711 * keep threads from blocking, waiting for data from the client. */
1712 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1713 service = tcall->conn->service;
1714 if (!QuotaOK(service)) {
1717 MUTEX_ENTER(&rx_pthread_mutex);
1718 if (tno == rxi_fcfs_thread_num
1719 || !tcall->queue_item_header.next) {
1720 MUTEX_EXIT(&rx_pthread_mutex);
1721 /* If we're the fcfs thread , then we'll just use
1722 * this call. If we haven't been able to find an optimal
1723 * choice, and we're at the end of the list, then use a
1724 * 2d choice if one has been identified. Otherwise... */
1725 call = (choice2 ? choice2 : tcall);
1726 service = call->conn->service;
1728 MUTEX_EXIT(&rx_pthread_mutex);
1729 if (!queue_IsEmpty(&tcall->rq)) {
1730 struct rx_packet *rp;
1731 rp = queue_First(&tcall->rq, rx_packet);
1732 if (rp->header.seq == 1) {
1734 || (rp->header.flags & RX_LAST_PACKET)) {
1736 } else if (rxi_2dchoice && !choice2
1737 && !(tcall->flags & RX_CALL_CLEARED)
1738 && (tcall->rprev > rxi_HardAckRate)) {
1748 ReturnToServerPool(service);
1755 MUTEX_EXIT(&rx_serverPool_lock);
1756 MUTEX_ENTER(&call->lock);
1758 if (call->flags & RX_CALL_WAIT_PROC) {
1759 call->flags &= ~RX_CALL_WAIT_PROC;
1760 rx_atomic_dec(&rx_nWaiting);
1763 if (call->state != RX_STATE_PRECALL || call->error) {
1764 MUTEX_EXIT(&call->lock);
1765 MUTEX_ENTER(&rx_serverPool_lock);
1766 ReturnToServerPool(service);
1771 if (queue_IsEmpty(&call->rq)
1772 || queue_First(&call->rq, rx_packet)->header.seq != 1)
1773 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1775 CLEAR_CALL_QUEUE_LOCK(call);
1778 /* If there are no eligible incoming calls, add this process
1779 * to the idle server queue, to wait for one */
1783 *socketp = OSI_NULLSOCKET;
1785 sq->socketp = socketp;
1786 queue_Append(&rx_idleServerQueue, sq);
1787 #ifndef AFS_AIX41_ENV
1788 rx_waitForPacket = sq;
1790 rx_waitingForPacket = sq;
1791 #endif /* AFS_AIX41_ENV */
1793 CV_WAIT(&sq->cv, &rx_serverPool_lock);
1795 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1796 MUTEX_EXIT(&rx_serverPool_lock);
1797 return (struct rx_call *)0;
1800 } while (!(call = sq->newcall)
1801 && !(socketp && *socketp != OSI_NULLSOCKET));
1802 MUTEX_EXIT(&rx_serverPool_lock);
1804 MUTEX_ENTER(&call->lock);
1810 MUTEX_ENTER(&freeSQEList_lock);
1811 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1812 rx_FreeSQEList = sq;
1813 MUTEX_EXIT(&freeSQEList_lock);
1816 clock_GetTime(&call->startTime);
1817 call->state = RX_STATE_ACTIVE;
1818 call->mode = RX_MODE_RECEIVING;
1819 #ifdef RX_KERNEL_TRACE
1820 if (ICL_SETACTIVE(afs_iclSetp)) {
1821 int glockOwner = ISAFS_GLOCK();
1824 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1825 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1832 rxi_calltrace(RX_CALL_START, call);
1833 dpf(("rx_GetCall(port=%d, service=%d) ==> call %"AFS_PTR_FMT"\n",
1834 call->conn->service->servicePort, call->conn->service->serviceId,
1837 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1838 MUTEX_EXIT(&call->lock);
1840 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
1845 #else /* RX_ENABLE_LOCKS */
1847 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1849 struct rx_serverQueueEntry *sq;
1850 struct rx_call *call = (struct rx_call *)0, *choice2;
1851 struct rx_service *service = NULL;
1855 MUTEX_ENTER(&freeSQEList_lock);
1857 if ((sq = rx_FreeSQEList)) {
1858 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1859 MUTEX_EXIT(&freeSQEList_lock);
1860 } else { /* otherwise allocate a new one and return that */
1861 MUTEX_EXIT(&freeSQEList_lock);
1862 sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1863 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1864 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1866 MUTEX_ENTER(&sq->lock);
1868 if (cur_service != NULL) {
1869 cur_service->nRequestsRunning--;
1870 MUTEX_ENTER(&rx_quota_mutex);
1871 if (cur_service->nRequestsRunning < cur_service->minProcs)
1874 MUTEX_EXIT(&rx_quota_mutex);
1876 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1877 struct rx_call *tcall, *ncall;
1878 /* Scan for eligible incoming calls. A call is not eligible
1879 * if the maximum number of calls for its service type are
1880 * already executing */
1881 /* One thread will process calls FCFS (to prevent starvation),
1882 * while the other threads may run ahead looking for calls which
1883 * have all their input data available immediately. This helps
1884 * keep threads from blocking, waiting for data from the client. */
1885 choice2 = (struct rx_call *)0;
1886 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1887 service = tcall->conn->service;
1888 if (QuotaOK(service)) {
1889 MUTEX_ENTER(&rx_pthread_mutex);
1890 if (tno == rxi_fcfs_thread_num
1891 || !tcall->queue_item_header.next) {
1892 MUTEX_EXIT(&rx_pthread_mutex);
1893 /* If we're the fcfs thread, then we'll just use
1894 * this call. If we haven't been able to find an optimal
1895 * choice, and we're at the end of the list, then use a
1896 * 2d choice if one has been identified. Otherwise... */
1897 call = (choice2 ? choice2 : tcall);
1898 service = call->conn->service;
1900 MUTEX_EXIT(&rx_pthread_mutex);
1901 if (!queue_IsEmpty(&tcall->rq)) {
1902 struct rx_packet *rp;
1903 rp = queue_First(&tcall->rq, rx_packet);
1904 if (rp->header.seq == 1
1906 || (rp->header.flags & RX_LAST_PACKET))) {
1908 } else if (rxi_2dchoice && !choice2
1909 && !(tcall->flags & RX_CALL_CLEARED)
1910 && (tcall->rprev > rxi_HardAckRate)) {
1924 /* we can't schedule a call if there's no data!!! */
1925 /* send an ack if there's no data, if we're missing the
1926 * first packet, or we're missing something between first
1927 * and last -- there's a "hole" in the incoming data. */
1928 if (queue_IsEmpty(&call->rq)
1929 || queue_First(&call->rq, rx_packet)->header.seq != 1
1930 || call->rprev != queue_Last(&call->rq, rx_packet)->header.seq)
1931 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1933 call->flags &= (~RX_CALL_WAIT_PROC);
1934 service->nRequestsRunning++;
1935 /* just started call in minProcs pool, need fewer to maintain
1937 MUTEX_ENTER(&rx_quota_mutex);
1938 if (service->nRequestsRunning <= service->minProcs)
1941 MUTEX_EXIT(&rx_quota_mutex);
1942 rx_atomic_dec(&rx_nWaiting);
1943 /* MUTEX_EXIT(&call->lock); */
1945 /* If there are no eligible incoming calls, add this process
1946 * to the idle server queue, to wait for one */
1949 *socketp = OSI_NULLSOCKET;
1951 sq->socketp = socketp;
1952 queue_Append(&rx_idleServerQueue, sq);
1956 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1958 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
1959 return (struct rx_call *)0;
1962 } while (!(call = sq->newcall)
1963 && !(socketp && *socketp != OSI_NULLSOCKET));
1965 MUTEX_EXIT(&sq->lock);
1967 MUTEX_ENTER(&freeSQEList_lock);
1968 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1969 rx_FreeSQEList = sq;
1970 MUTEX_EXIT(&freeSQEList_lock);
1973 clock_GetTime(&call->startTime);
1974 call->state = RX_STATE_ACTIVE;
1975 call->mode = RX_MODE_RECEIVING;
1976 #ifdef RX_KERNEL_TRACE
1977 if (ICL_SETACTIVE(afs_iclSetp)) {
1978 int glockOwner = ISAFS_GLOCK();
1981 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1982 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1989 rxi_calltrace(RX_CALL_START, call);
1990 dpf(("rx_GetCall(port=%d, service=%d) ==> call %p\n",
1991 call->conn->service->servicePort, call->conn->service->serviceId,
1994 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
2001 #endif /* RX_ENABLE_LOCKS */
2005 /* Establish a procedure to be called when a packet arrives for a
2006 * call. This routine will be called at most once after each call,
2007 * and will also be called if there is an error condition on the or
2008 * the call is complete. Used by multi rx to build a selection
2009 * function which determines which of several calls is likely to be a
2010 * good one to read from.
2011 * NOTE: the way this is currently implemented it is probably only a
2012 * good idea to (1) use it immediately after a newcall (clients only)
2013 * and (2) only use it once. Other uses currently void your warranty
2016 rx_SetArrivalProc(struct rx_call *call,
2017 void (*proc) (struct rx_call * call,
2020 void * handle, int arg)
2022 call->arrivalProc = proc;
2023 call->arrivalProcHandle = handle;
2024 call->arrivalProcArg = arg;
2027 /* Call is finished (possibly prematurely). Return rc to the peer, if
2028 * appropriate, and return the final error code from the conversation
2032 rx_EndCall(struct rx_call *call, afs_int32 rc)
2034 struct rx_connection *conn = call->conn;
2038 dpf(("rx_EndCall(call %"AFS_PTR_FMT" rc %d error %d abortCode %d)\n",
2039 call, rc, call->error, call->abortCode));
2042 MUTEX_ENTER(&call->lock);
2044 if (rc == 0 && call->error == 0) {
2045 call->abortCode = 0;
2046 call->abortCount = 0;
2049 call->arrivalProc = (void (*)())0;
2050 if (rc && call->error == 0) {
2051 rxi_CallError(call, rc);
2052 /* Send an abort message to the peer if this error code has
2053 * only just been set. If it was set previously, assume the
2054 * peer has already been sent the error code or will request it
2056 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
2058 if (conn->type == RX_SERVER_CONNECTION) {
2059 /* Make sure reply or at least dummy reply is sent */
2060 if (call->mode == RX_MODE_RECEIVING) {
2061 rxi_WriteProc(call, 0, 0);
2063 if (call->mode == RX_MODE_SENDING) {
2064 rxi_FlushWrite(call);
2066 rxi_calltrace(RX_CALL_END, call);
2067 /* Call goes to hold state until reply packets are acknowledged */
2068 if (call->tfirst + call->nSoftAcked < call->tnext) {
2069 call->state = RX_STATE_HOLD;
2071 call->state = RX_STATE_DALLY;
2072 rxi_ClearTransmitQueue(call, 0);
2073 rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
2074 rxevent_Cancel(call->keepAliveEvent, call,
2075 RX_CALL_REFCOUNT_ALIVE);
2077 } else { /* Client connection */
2079 /* Make sure server receives input packets, in the case where
2080 * no reply arguments are expected */
2081 if ((call->mode == RX_MODE_SENDING)
2082 || (call->mode == RX_MODE_RECEIVING && call->rnext == 1)) {
2083 (void)rxi_ReadProc(call, &dummy, 1);
2086 /* If we had an outstanding delayed ack, be nice to the server
2087 * and force-send it now.
2089 if (call->delayedAckEvent) {
2090 rxevent_Cancel(call->delayedAckEvent, call,
2091 RX_CALL_REFCOUNT_DELAY);
2092 call->delayedAckEvent = NULL;
2093 rxi_SendDelayedAck(NULL, call, NULL);
2096 /* We need to release the call lock since it's lower than the
2097 * conn_call_lock and we don't want to hold the conn_call_lock
2098 * over the rx_ReadProc call. The conn_call_lock needs to be held
2099 * here for the case where rx_NewCall is perusing the calls on
2100 * the connection structure. We don't want to signal until
2101 * rx_NewCall is in a stable state. Otherwise, rx_NewCall may
2102 * have checked this call, found it active and by the time it
2103 * goes to sleep, will have missed the signal.
2105 MUTEX_EXIT(&call->lock);
2106 MUTEX_ENTER(&conn->conn_call_lock);
2107 MUTEX_ENTER(&call->lock);
2108 MUTEX_ENTER(&conn->conn_data_lock);
2109 conn->flags |= RX_CONN_BUSY;
2110 if (conn->flags & RX_CONN_MAKECALL_WAITING) {
2111 MUTEX_EXIT(&conn->conn_data_lock);
2112 #ifdef RX_ENABLE_LOCKS
2113 CV_BROADCAST(&conn->conn_call_cv);
2118 #ifdef RX_ENABLE_LOCKS
2120 MUTEX_EXIT(&conn->conn_data_lock);
2122 #endif /* RX_ENABLE_LOCKS */
2123 call->state = RX_STATE_DALLY;
2125 error = call->error;
2127 /* currentPacket, nLeft, and NFree must be zeroed here, because
2128 * ResetCall cannot: ResetCall may be called at splnet(), in the
2129 * kernel version, and may interrupt the macros rx_Read or
2130 * rx_Write, which run at normal priority for efficiency. */
2131 if (call->currentPacket) {
2132 #ifdef RX_TRACK_PACKETS
2133 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
2135 rxi_FreePacket(call->currentPacket);
2136 call->currentPacket = (struct rx_packet *)0;
2139 call->nLeft = call->nFree = call->curlen = 0;
2141 /* Free any packets from the last call to ReadvProc/WritevProc */
2142 #ifdef RXDEBUG_PACKET
2144 #endif /* RXDEBUG_PACKET */
2145 rxi_FreePackets(0, &call->iovq);
2147 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
2148 MUTEX_EXIT(&call->lock);
2149 if (conn->type == RX_CLIENT_CONNECTION) {
2150 MUTEX_ENTER(&conn->conn_data_lock);
2151 conn->flags &= ~RX_CONN_BUSY;
2152 MUTEX_EXIT(&conn->conn_data_lock);
2153 MUTEX_EXIT(&conn->conn_call_lock);
2157 * Map errors to the local host's errno.h format.
2159 error = ntoh_syserr_conv(error);
2163 #if !defined(KERNEL)
2165 /* Call this routine when shutting down a server or client (especially
2166 * clients). This will allow Rx to gracefully garbage collect server
2167 * connections, and reduce the number of retries that a server might
2168 * make to a dead client.
2169 * This is not quite right, since some calls may still be ongoing and
2170 * we can't lock them to destroy them. */
2174 struct rx_connection **conn_ptr, **conn_end;
2178 if (rxinit_status == 1) {
2180 return; /* Already shutdown. */
2182 rxi_DeleteCachedConnections();
2183 if (rx_connHashTable) {
2184 MUTEX_ENTER(&rx_connHashTable_lock);
2185 for (conn_ptr = &rx_connHashTable[0], conn_end =
2186 &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
2188 struct rx_connection *conn, *next;
2189 for (conn = *conn_ptr; conn; conn = next) {
2191 if (conn->type == RX_CLIENT_CONNECTION) {
2192 /* MUTEX_ENTER(&conn->conn_data_lock); when used in kernel */
2194 /* MUTEX_EXIT(&conn->conn_data_lock); when used in kernel */
2195 #ifdef RX_ENABLE_LOCKS
2196 rxi_DestroyConnectionNoLock(conn);
2197 #else /* RX_ENABLE_LOCKS */
2198 rxi_DestroyConnection(conn);
2199 #endif /* RX_ENABLE_LOCKS */
2203 #ifdef RX_ENABLE_LOCKS
2204 while (rx_connCleanup_list) {
2205 struct rx_connection *conn;
2206 conn = rx_connCleanup_list;
2207 rx_connCleanup_list = rx_connCleanup_list->next;
2208 MUTEX_EXIT(&rx_connHashTable_lock);
2209 rxi_CleanupConnection(conn);
2210 MUTEX_ENTER(&rx_connHashTable_lock);
2212 MUTEX_EXIT(&rx_connHashTable_lock);
2213 #endif /* RX_ENABLE_LOCKS */
2218 afs_winsockCleanup();
2226 /* if we wakeup packet waiter too often, can get in loop with two
2227 AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2229 rxi_PacketsUnWait(void)
2231 if (!rx_waitingForPackets) {
2235 if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2236 return; /* still over quota */
2239 rx_waitingForPackets = 0;
2240 #ifdef RX_ENABLE_LOCKS
2241 CV_BROADCAST(&rx_waitingForPackets_cv);
2243 osi_rxWakeup(&rx_waitingForPackets);
2249 /* ------------------Internal interfaces------------------------- */
2251 /* Return this process's service structure for the
2252 * specified socket and service */
2254 rxi_FindService(osi_socket socket, u_short serviceId)
2256 struct rx_service **sp;
2257 for (sp = &rx_services[0]; *sp; sp++) {
2258 if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2264 #ifdef RXDEBUG_PACKET
2265 #ifdef KDUMP_RX_LOCK
2266 static struct rx_call_rx_lock *rx_allCallsp = 0;
2268 static struct rx_call *rx_allCallsp = 0;
2270 #endif /* RXDEBUG_PACKET */
2272 /* Allocate a call structure, for the indicated channel of the
2273 * supplied connection. The mode and state of the call must be set by
2274 * the caller. Returns the call with mutex locked. */
2276 rxi_NewCall(struct rx_connection *conn, int channel)
2278 struct rx_call *call;
2279 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2280 struct rx_call *cp; /* Call pointer temp */
2281 struct rx_call *nxp; /* Next call pointer, for queue_Scan */
2282 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2284 dpf(("rxi_NewCall(conn %"AFS_PTR_FMT", channel %d)\n", conn, channel));
2286 /* Grab an existing call structure, or allocate a new one.
2287 * Existing call structures are assumed to have been left reset by
2289 MUTEX_ENTER(&rx_freeCallQueue_lock);
2291 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2293 * EXCEPT that the TQ might not yet be cleared out.
2294 * Skip over those with in-use TQs.
2297 for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2298 if (!(cp->flags & RX_CALL_TQ_BUSY)) {
2304 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2305 if (queue_IsNotEmpty(&rx_freeCallQueue)) {
2306 call = queue_First(&rx_freeCallQueue, rx_call);
2307 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2309 if (rx_stats_active)
2310 rx_MutexDecrement(rx_stats.nFreeCallStructs, rx_stats_mutex);
2311 MUTEX_EXIT(&rx_freeCallQueue_lock);
2312 MUTEX_ENTER(&call->lock);
2313 CLEAR_CALL_QUEUE_LOCK(call);
2314 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2315 /* Now, if TQ wasn't cleared earlier, do it now. */
2316 rxi_WaitforTQBusy(call);
2317 if (call->flags & RX_CALL_TQ_CLEARME) {
2318 rxi_ClearTransmitQueue(call, 1);
2319 /*queue_Init(&call->tq);*/
2321 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2322 /* Bind the call to its connection structure */
2324 rxi_ResetCall(call, 1);
2327 call = rxi_Alloc(sizeof(struct rx_call));
2328 #ifdef RXDEBUG_PACKET
2329 call->allNextp = rx_allCallsp;
2330 rx_allCallsp = call;
2332 #endif /* RXDEBUG_PACKET */
2333 rx_MutexIncrement(rx_stats.nCallStructs, rx_stats_mutex);
2335 MUTEX_EXIT(&rx_freeCallQueue_lock);
2336 MUTEX_INIT(&call->lock, "call lock", MUTEX_DEFAULT, NULL);
2337 MUTEX_ENTER(&call->lock);
2338 CV_INIT(&call->cv_twind, "call twind", CV_DEFAULT, 0);
2339 CV_INIT(&call->cv_rq, "call rq", CV_DEFAULT, 0);
2340 CV_INIT(&call->cv_tq, "call tq", CV_DEFAULT, 0);
2342 /* Initialize once-only items */
2343 queue_Init(&call->tq);
2344 queue_Init(&call->rq);
2345 queue_Init(&call->iovq);
2346 #ifdef RXDEBUG_PACKET
2347 call->rqc = call->tqc = call->iovqc = 0;
2348 #endif /* RXDEBUG_PACKET */
2349 /* Bind the call to its connection structure (prereq for reset) */
2351 rxi_ResetCall(call, 1);
2353 call->channel = channel;
2354 call->callNumber = &conn->callNumber[channel];
2355 call->rwind = conn->rwind[channel];
2356 call->twind = conn->twind[channel];
2357 /* Note that the next expected call number is retained (in
2358 * conn->callNumber[i]), even if we reallocate the call structure
2360 conn->call[channel] = call;
2361 /* if the channel's never been used (== 0), we should start at 1, otherwise
2362 * the call number is valid from the last time this channel was used */
2363 if (*call->callNumber == 0)
2364 *call->callNumber = 1;
2369 /* A call has been inactive long enough that so we can throw away
2370 * state, including the call structure, which is placed on the call
2372 * Call is locked upon entry.
2373 * haveCTLock set if called from rxi_ReapConnections
2375 #ifdef RX_ENABLE_LOCKS
2377 rxi_FreeCall(struct rx_call *call, int haveCTLock)
2378 #else /* RX_ENABLE_LOCKS */
2380 rxi_FreeCall(struct rx_call *call)
2381 #endif /* RX_ENABLE_LOCKS */
2383 int channel = call->channel;
2384 struct rx_connection *conn = call->conn;
2387 if (call->state == RX_STATE_DALLY || call->state == RX_STATE_HOLD)
2388 (*call->callNumber)++;
2389 rxi_ResetCall(call, 0);
2390 call->conn->call[channel] = (struct rx_call *)0;
2392 MUTEX_ENTER(&rx_freeCallQueue_lock);
2393 SET_CALL_QUEUE_LOCK(call, &rx_freeCallQueue_lock);
2394 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2395 /* A call may be free even though its transmit queue is still in use.
2396 * Since we search the call list from head to tail, put busy calls at
2397 * the head of the list, and idle calls at the tail.
2399 if (call->flags & RX_CALL_TQ_BUSY)
2400 queue_Prepend(&rx_freeCallQueue, call);
2402 queue_Append(&rx_freeCallQueue, call);
2403 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2404 queue_Append(&rx_freeCallQueue, call);
2405 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2406 if (rx_stats_active)
2407 rx_MutexIncrement(rx_stats.nFreeCallStructs, rx_stats_mutex);
2408 MUTEX_EXIT(&rx_freeCallQueue_lock);
2410 /* Destroy the connection if it was previously slated for
2411 * destruction, i.e. the Rx client code previously called
2412 * rx_DestroyConnection (client connections), or
2413 * rxi_ReapConnections called the same routine (server
2414 * connections). Only do this, however, if there are no
2415 * outstanding calls. Note that for fine grain locking, there appears
2416 * to be a deadlock in that rxi_FreeCall has a call locked and
2417 * DestroyConnectionNoLock locks each call in the conn. But note a
2418 * few lines up where we have removed this call from the conn.
2419 * If someone else destroys a connection, they either have no
2420 * call lock held or are going through this section of code.
2422 MUTEX_ENTER(&conn->conn_data_lock);
2423 if (conn->flags & RX_CONN_DESTROY_ME && !(conn->flags & RX_CONN_MAKECALL_WAITING)) {
2425 MUTEX_EXIT(&conn->conn_data_lock);
2426 #ifdef RX_ENABLE_LOCKS
2428 rxi_DestroyConnectionNoLock(conn);
2430 rxi_DestroyConnection(conn);
2431 #else /* RX_ENABLE_LOCKS */
2432 rxi_DestroyConnection(conn);
2433 #endif /* RX_ENABLE_LOCKS */
2435 MUTEX_EXIT(&conn->conn_data_lock);
2439 rx_atomic_t rxi_Allocsize = RX_ATOMIC_INIT(0);
2440 rx_atomic_t rxi_Alloccnt = RX_ATOMIC_INIT(0);
2443 rxi_Alloc(size_t size)
2447 if (rx_stats_active) {
2448 rx_atomic_add(&rxi_Allocsize, (int) size);
2449 rx_atomic_inc(&rxi_Alloccnt);
2453 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
2454 afs_osi_Alloc_NoSleep(size);
2459 osi_Panic("rxi_Alloc error");
2465 rxi_Free(void *addr, size_t size)
2467 if (rx_stats_active) {
2468 rx_atomic_sub(&rxi_Allocsize, (int) size);
2469 rx_atomic_dec(&rxi_Alloccnt);
2471 osi_Free(addr, size);
2475 rxi_SetPeerMtu(struct rx_peer *peer, afs_uint32 host, afs_uint32 port, int mtu)
2477 struct rx_peer **peer_ptr = NULL, **peer_end = NULL;
2478 struct rx_peer *next = NULL;
2482 MUTEX_ENTER(&rx_peerHashTable_lock);
2484 peer_ptr = &rx_peerHashTable[0];
2485 peer_end = &rx_peerHashTable[rx_hashTableSize];
2488 for ( ; peer_ptr < peer_end; peer_ptr++) {
2491 for ( ; peer; peer = next) {
2493 if (host == peer->host)
2498 hashIndex = PEER_HASH(host, port);
2499 for (peer = rx_peerHashTable[hashIndex]; peer; peer = peer->next) {
2500 if ((peer->host == host) && (peer->port == port))
2505 MUTEX_ENTER(&rx_peerHashTable_lock);
2510 MUTEX_EXIT(&rx_peerHashTable_lock);
2512 MUTEX_ENTER(&peer->peer_lock);
2513 /* We don't handle dropping below min, so don't */
2514 mtu = MAX(mtu, RX_MIN_PACKET_SIZE);
2515 peer->ifMTU=MIN(mtu, peer->ifMTU);
2516 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2517 /* if we tweaked this down, need to tune our peer MTU too */
2518 peer->MTU = MIN(peer->MTU, peer->natMTU);
2519 /* if we discovered a sub-1500 mtu, degrade */
2520 if (peer->ifMTU < OLD_MAX_PACKET_SIZE)
2521 peer->maxDgramPackets = 1;
2522 /* We no longer have valid peer packet information */
2523 if (peer->maxPacketSize-RX_IPUDP_SIZE > peer->ifMTU)
2524 peer->maxPacketSize = 0;
2525 MUTEX_EXIT(&peer->peer_lock);
2527 MUTEX_ENTER(&rx_peerHashTable_lock);
2529 if (host && !port) {
2531 /* pick up where we left off */
2535 MUTEX_EXIT(&rx_peerHashTable_lock);
2538 /* Find the peer process represented by the supplied (host,port)
2539 * combination. If there is no appropriate active peer structure, a
2540 * new one will be allocated and initialized
2541 * The origPeer, if set, is a pointer to a peer structure on which the
2542 * refcount will be be decremented. This is used to replace the peer
2543 * structure hanging off a connection structure */
2545 rxi_FindPeer(afs_uint32 host, u_short port,
2546 struct rx_peer *origPeer, int create)
2550 hashIndex = PEER_HASH(host, port);
2551 MUTEX_ENTER(&rx_peerHashTable_lock);
2552 for (pp = rx_peerHashTable[hashIndex]; pp; pp = pp->next) {
2553 if ((pp->host == host) && (pp->port == port))
2558 pp = rxi_AllocPeer(); /* This bzero's *pp */
2559 pp->host = host; /* set here or in InitPeerParams is zero */
2561 MUTEX_INIT(&pp->peer_lock, "peer_lock", MUTEX_DEFAULT, 0);
2562 queue_Init(&pp->congestionQueue);
2563 queue_Init(&pp->rpcStats);
2564 pp->next = rx_peerHashTable[hashIndex];
2565 rx_peerHashTable[hashIndex] = pp;
2566 rxi_InitPeerParams(pp);
2567 if (rx_stats_active)
2568 rx_MutexIncrement(rx_stats.nPeerStructs, rx_stats_mutex);
2575 origPeer->refCount--;
2576 MUTEX_EXIT(&rx_peerHashTable_lock);
2581 /* Find the connection at (host, port) started at epoch, and with the
2582 * given connection id. Creates the server connection if necessary.
2583 * The type specifies whether a client connection or a server
2584 * connection is desired. In both cases, (host, port) specify the
2585 * peer's (host, pair) pair. Client connections are not made
2586 * automatically by this routine. The parameter socket gives the
2587 * socket descriptor on which the packet was received. This is used,
2588 * in the case of server connections, to check that *new* connections
2589 * come via a valid (port, serviceId). Finally, the securityIndex
2590 * parameter must match the existing index for the connection. If a
2591 * server connection is created, it will be created using the supplied
2592 * index, if the index is valid for this service */
2593 struct rx_connection *
2594 rxi_FindConnection(osi_socket socket, afs_uint32 host,
2595 u_short port, u_short serviceId, afs_uint32 cid,
2596 afs_uint32 epoch, int type, u_int securityIndex)
2598 int hashindex, flag, i;
2599 struct rx_connection *conn;
2600 hashindex = CONN_HASH(host, port, cid, epoch, type);
2601 MUTEX_ENTER(&rx_connHashTable_lock);
2602 rxLastConn ? (conn = rxLastConn, flag = 0) : (conn =
2603 rx_connHashTable[hashindex],
2606 if ((conn->type == type) && ((cid & RX_CIDMASK) == conn->cid)
2607 && (epoch == conn->epoch)) {
2608 struct rx_peer *pp = conn->peer;
2609 if (securityIndex != conn->securityIndex) {
2610 /* this isn't supposed to happen, but someone could forge a packet
2611 * like this, and there seems to be some CM bug that makes this
2612 * happen from time to time -- in which case, the fileserver
2614 MUTEX_EXIT(&rx_connHashTable_lock);
2615 return (struct rx_connection *)0;
2617 if (pp->host == host && pp->port == port)
2619 if (type == RX_CLIENT_CONNECTION && pp->port == port)
2621 /* So what happens when it's a callback connection? */
2622 if ( /*type == RX_CLIENT_CONNECTION && */
2623 (conn->epoch & 0x80000000))
2627 /* the connection rxLastConn that was used the last time is not the
2628 ** one we are looking for now. Hence, start searching in the hash */
2630 conn = rx_connHashTable[hashindex];
2635 struct rx_service *service;
2636 if (type == RX_CLIENT_CONNECTION) {
2637 MUTEX_EXIT(&rx_connHashTable_lock);
2638 return (struct rx_connection *)0;
2640 service = rxi_FindService(socket, serviceId);
2641 if (!service || (securityIndex >= service->nSecurityObjects)
2642 || (service->securityObjects[securityIndex] == 0)) {
2643 MUTEX_EXIT(&rx_connHashTable_lock);
2644 return (struct rx_connection *)0;
2646 conn = rxi_AllocConnection(); /* This bzero's the connection */
2647 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
2648 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
2649 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
2650 conn->next = rx_connHashTable[hashindex];
2651 rx_connHashTable[hashindex] = conn;
2652 conn->peer = rxi_FindPeer(host, port, 0, 1);
2653 conn->type = RX_SERVER_CONNECTION;
2654 conn->lastSendTime = clock_Sec(); /* don't GC immediately */
2655 conn->epoch = epoch;
2656 conn->cid = cid & RX_CIDMASK;
2657 /* conn->serial = conn->lastSerial = 0; */
2658 /* conn->timeout = 0; */
2659 conn->ackRate = RX_FAST_ACK_RATE;
2660 conn->service = service;
2661 conn->serviceId = serviceId;
2662 conn->securityIndex = securityIndex;
2663 conn->securityObject = service->securityObjects[securityIndex];
2664 conn->nSpecific = 0;
2665 conn->specific = NULL;
2666 rx_SetConnDeadTime(conn, service->connDeadTime);
2667 rx_SetConnIdleDeadTime(conn, service->idleDeadTime);
2668 rx_SetServerConnIdleDeadErr(conn, service->idleDeadErr);
2669 for (i = 0; i < RX_MAXCALLS; i++) {
2670 conn->twind[i] = rx_initSendWindow;
2671 conn->rwind[i] = rx_initReceiveWindow;
2673 /* Notify security object of the new connection */
2674 RXS_NewConnection(conn->securityObject, conn);
2675 /* XXXX Connection timeout? */
2676 if (service->newConnProc)
2677 (*service->newConnProc) (conn);
2678 if (rx_stats_active)
2679 rx_MutexIncrement(rx_stats.nServerConns, rx_stats_mutex);
2682 MUTEX_ENTER(&conn->conn_data_lock);
2684 MUTEX_EXIT(&conn->conn_data_lock);
2686 rxLastConn = conn; /* store this connection as the last conn used */
2687 MUTEX_EXIT(&rx_connHashTable_lock);
2691 /* There are two packet tracing routines available for testing and monitoring
2692 * Rx. One is called just after every packet is received and the other is
2693 * called just before every packet is sent. Received packets, have had their
2694 * headers decoded, and packets to be sent have not yet had their headers
2695 * encoded. Both take two parameters: a pointer to the packet and a sockaddr
2696 * containing the network address. Both can be modified. The return value, if
2697 * non-zero, indicates that the packet should be dropped. */
2699 int (*rx_justReceived) (struct rx_packet *, struct sockaddr_in *) = 0;
2700 int (*rx_almostSent) (struct rx_packet *, struct sockaddr_in *) = 0;
2702 /* A packet has been received off the interface. Np is the packet, socket is
2703 * the socket number it was received from (useful in determining which service
2704 * this packet corresponds to), and (host, port) reflect the host,port of the
2705 * sender. This call returns the packet to the caller if it is finished with
2706 * it, rather than de-allocating it, just as a small performance hack */
2709 rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
2710 afs_uint32 host, u_short port, int *tnop,
2711 struct rx_call **newcallp)
2713 struct rx_call *call;
2714 struct rx_connection *conn;
2716 afs_uint32 currentCallNumber;
2722 struct rx_packet *tnp;
2725 /* We don't print out the packet until now because (1) the time may not be
2726 * accurate enough until now in the lwp implementation (rx_Listener only gets
2727 * the time after the packet is read) and (2) from a protocol point of view,
2728 * this is the first time the packet has been seen */
2729 packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
2730 ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
2731 dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %"AFS_PTR_FMT,
2732 np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
2733 np->header.epoch, np->header.cid, np->header.callNumber,
2734 np->header.seq, np->header.flags, np));
2737 if (np->header.type == RX_PACKET_TYPE_VERSION) {
2738 return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
2741 if (np->header.type == RX_PACKET_TYPE_DEBUG) {
2742 return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
2745 /* If an input tracer function is defined, call it with the packet and
2746 * network address. Note this function may modify its arguments. */
2747 if (rx_justReceived) {
2748 struct sockaddr_in addr;
2750 addr.sin_family = AF_INET;
2751 addr.sin_port = port;
2752 addr.sin_addr.s_addr = host;
2753 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
2754 addr.sin_len = sizeof(addr);
2755 #endif /* AFS_OSF_ENV */
2756 drop = (*rx_justReceived) (np, &addr);
2757 /* drop packet if return value is non-zero */
2760 port = addr.sin_port; /* in case fcn changed addr */
2761 host = addr.sin_addr.s_addr;
2765 /* If packet was not sent by the client, then *we* must be the client */
2766 type = ((np->header.flags & RX_CLIENT_INITIATED) != RX_CLIENT_INITIATED)
2767 ? RX_CLIENT_CONNECTION : RX_SERVER_CONNECTION;
2769 /* Find the connection (or fabricate one, if we're the server & if
2770 * necessary) associated with this packet */
2772 rxi_FindConnection(socket, host, port, np->header.serviceId,
2773 np->header.cid, np->header.epoch, type,
2774 np->header.securityIndex);
2777 /* If no connection found or fabricated, just ignore the packet.
2778 * (An argument could be made for sending an abort packet for
2783 MUTEX_ENTER(&conn->conn_data_lock);
2784 if (conn->maxSerial < np->header.serial)
2785 conn->maxSerial = np->header.serial;
2786 MUTEX_EXIT(&conn->conn_data_lock);
2788 /* If the connection is in an error state, send an abort packet and ignore
2789 * the incoming packet */
2791 /* Don't respond to an abort packet--we don't want loops! */
2792 MUTEX_ENTER(&conn->conn_data_lock);
2793 if (np->header.type != RX_PACKET_TYPE_ABORT)
2794 np = rxi_SendConnectionAbort(conn, np, 1, 0);
2796 MUTEX_EXIT(&conn->conn_data_lock);
2800 /* Check for connection-only requests (i.e. not call specific). */
2801 if (np->header.callNumber == 0) {
2802 switch (np->header.type) {
2803 case RX_PACKET_TYPE_ABORT: {
2804 /* What if the supplied error is zero? */
2805 afs_int32 errcode = ntohl(rx_GetInt32(np, 0));
2806 dpf(("rxi_ReceivePacket ABORT rx_GetInt32 = %d", errcode));
2807 rxi_ConnectionError(conn, errcode);
2808 MUTEX_ENTER(&conn->conn_data_lock);
2810 MUTEX_EXIT(&conn->conn_data_lock);
2813 case RX_PACKET_TYPE_CHALLENGE:
2814 tnp = rxi_ReceiveChallengePacket(conn, np, 1);
2815 MUTEX_ENTER(&conn->conn_data_lock);
2817 MUTEX_EXIT(&conn->conn_data_lock);
2819 case RX_PACKET_TYPE_RESPONSE:
2820 tnp = rxi_ReceiveResponsePacket(conn, np, 1);
2821 MUTEX_ENTER(&conn->conn_data_lock);
2823 MUTEX_EXIT(&conn->conn_data_lock);
2825 case RX_PACKET_TYPE_PARAMS:
2826 case RX_PACKET_TYPE_PARAMS + 1:
2827 case RX_PACKET_TYPE_PARAMS + 2:
2828 /* ignore these packet types for now */
2829 MUTEX_ENTER(&conn->conn_data_lock);
2831 MUTEX_EXIT(&conn->conn_data_lock);
2836 /* Should not reach here, unless the peer is broken: send an
2838 rxi_ConnectionError(conn, RX_PROTOCOL_ERROR);
2839 MUTEX_ENTER(&conn->conn_data_lock);
2840 tnp = rxi_SendConnectionAbort(conn, np, 1, 0);
2842 MUTEX_EXIT(&conn->conn_data_lock);
2847 channel = np->header.cid & RX_CHANNELMASK;
2848 call = conn->call[channel];
2849 #ifdef RX_ENABLE_LOCKS
2851 MUTEX_ENTER(&call->lock);
2852 /* Test to see if call struct is still attached to conn. */
2853 if (call != conn->call[channel]) {
2855 MUTEX_EXIT(&call->lock);
2856 if (type == RX_SERVER_CONNECTION) {
2857 call = conn->call[channel];
2858 /* If we started with no call attached and there is one now,
2859 * another thread is also running this routine and has gotten
2860 * the connection channel. We should drop this packet in the tests
2861 * below. If there was a call on this connection and it's now
2862 * gone, then we'll be making a new call below.
2863 * If there was previously a call and it's now different then
2864 * the old call was freed and another thread running this routine
2865 * has created a call on this channel. One of these two threads
2866 * has a packet for the old call and the code below handles those
2870 MUTEX_ENTER(&call->lock);
2872 /* This packet can't be for this call. If the new call address is
2873 * 0 then no call is running on this channel. If there is a call
2874 * then, since this is a client connection we're getting data for
2875 * it must be for the previous call.
2877 if (rx_stats_active)
2878 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2879 MUTEX_ENTER(&conn->conn_data_lock);
2881 MUTEX_EXIT(&conn->conn_data_lock);
2886 currentCallNumber = conn->callNumber[channel];
2888 if (type == RX_SERVER_CONNECTION) { /* We're the server */
2889 if (np->header.callNumber < currentCallNumber) {
2890 if (rx_stats_active)
2891 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2892 #ifdef RX_ENABLE_LOCKS
2894 MUTEX_EXIT(&call->lock);
2896 MUTEX_ENTER(&conn->conn_data_lock);
2898 MUTEX_EXIT(&conn->conn_data_lock);
2902 MUTEX_ENTER(&conn->conn_call_lock);
2903 call = rxi_NewCall(conn, channel);
2904 MUTEX_EXIT(&conn->conn_call_lock);
2905 *call->callNumber = np->header.callNumber;
2907 if (np->header.callNumber == 0)
2908 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%.06d len %d",
2909 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
2910 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
2911 np->header.flags, np, np->retryTime.sec, np->retryTime.usec / 1000, np->length));
2913 call->state = RX_STATE_PRECALL;
2914 clock_GetTime(&call->queueTime);
2915 hzero(call->bytesSent);
2916 hzero(call->bytesRcvd);
2918 * If the number of queued calls exceeds the overload
2919 * threshold then abort this call.
2921 if ((rx_BusyThreshold > 0) &&
2922 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
2923 struct rx_packet *tp;
2925 rxi_CallError(call, rx_BusyError);
2926 tp = rxi_SendCallAbort(call, np, 1, 0);
2927 MUTEX_EXIT(&call->lock);
2928 MUTEX_ENTER(&conn->conn_data_lock);
2930 MUTEX_EXIT(&conn->conn_data_lock);
2931 if (rx_stats_active)
2932 rx_MutexIncrement(rx_stats.nBusies, rx_stats_mutex);
2935 rxi_KeepAliveOn(call);
2936 } else if (np->header.callNumber != currentCallNumber) {
2937 /* Wait until the transmit queue is idle before deciding
2938 * whether to reset the current call. Chances are that the
2939 * call will be in ether DALLY or HOLD state once the TQ_BUSY
2942 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2943 while ((call->state == RX_STATE_ACTIVE)
2944 && (call->flags & RX_CALL_TQ_BUSY)) {
2945 call->flags |= RX_CALL_TQ_WAIT;
2947 #ifdef RX_ENABLE_LOCKS
2948 osirx_AssertMine(&call->lock, "rxi_Start lock3");
2949 CV_WAIT(&call->cv_tq, &call->lock);
2950 #else /* RX_ENABLE_LOCKS */
2951 osi_rxSleep(&call->tq);
2952 #endif /* RX_ENABLE_LOCKS */
2954 if (call->tqWaiters == 0)
2955 call->flags &= ~RX_CALL_TQ_WAIT;
2957 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2958 /* If the new call cannot be taken right now send a busy and set
2959 * the error condition in this call, so that it terminates as
2960 * quickly as possible */
2961 if (call->state == RX_STATE_ACTIVE) {
2962 struct rx_packet *tp;
2964 rxi_CallError(call, RX_CALL_DEAD);
2965 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
2967 MUTEX_EXIT(&call->lock);
2968 MUTEX_ENTER(&conn->conn_data_lock);
2970 MUTEX_EXIT(&conn->conn_data_lock);
2973 rxi_ResetCall(call, 0);
2974 *call->callNumber = np->header.callNumber;
2976 if (np->header.callNumber == 0)
2977 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%06d len %d",
2978 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
2979 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
2980 np->header.flags, np, np->retryTime.sec, np->retryTime.usec, np->length));
2982 call->state = RX_STATE_PRECALL;
2983 clock_GetTime(&call->queueTime);
2984 hzero(call->bytesSent);
2985 hzero(call->bytesRcvd);
2987 * If the number of queued calls exceeds the overload
2988 * threshold then abort this call.
2990 if ((rx_BusyThreshold > 0) &&
2991 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
2992 struct rx_packet *tp;
2994 rxi_CallError(call, rx_BusyError);
2995 tp = rxi_SendCallAbort(call, np, 1, 0);
2996 MUTEX_EXIT(&call->lock);
2997 MUTEX_ENTER(&conn->conn_data_lock);
2999 MUTEX_EXIT(&conn->conn_data_lock);
3000 if (rx_stats_active)
3001 rx_MutexIncrement(rx_stats.nBusies, rx_stats_mutex);
3004 rxi_KeepAliveOn(call);
3006 /* Continuing call; do nothing here. */
3008 } else { /* we're the client */
3009 /* Ignore all incoming acknowledgements for calls in DALLY state */
3010 if (call && (call->state == RX_STATE_DALLY)
3011 && (np->header.type == RX_PACKET_TYPE_ACK)) {
3012 if (rx_stats_active)
3013 rx_MutexIncrement(rx_stats.ignorePacketDally, rx_stats_mutex);
3014 #ifdef RX_ENABLE_LOCKS
3016 MUTEX_EXIT(&call->lock);
3019 MUTEX_ENTER(&conn->conn_data_lock);
3021 MUTEX_EXIT(&conn->conn_data_lock);
3025 /* Ignore anything that's not relevant to the current call. If there
3026 * isn't a current call, then no packet is relevant. */
3027 if (!call || (np->header.callNumber != currentCallNumber)) {
3028 if (rx_stats_active)
3029 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
3030 #ifdef RX_ENABLE_LOCKS
3032 MUTEX_EXIT(&call->lock);
3035 MUTEX_ENTER(&conn->conn_data_lock);
3037 MUTEX_EXIT(&conn->conn_data_lock);
3040 /* If the service security object index stamped in the packet does not
3041 * match the connection's security index, ignore the packet */
3042 if (np->header.securityIndex != conn->securityIndex) {
3043 #ifdef RX_ENABLE_LOCKS
3044 MUTEX_EXIT(&call->lock);
3046 MUTEX_ENTER(&conn->conn_data_lock);
3048 MUTEX_EXIT(&conn->conn_data_lock);
3052 /* If we're receiving the response, then all transmit packets are
3053 * implicitly acknowledged. Get rid of them. */
3054 if (np->header.type == RX_PACKET_TYPE_DATA) {
3055 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3056 /* XXX Hack. Because we must release the global rx lock when
3057 * sending packets (osi_NetSend) we drop all acks while we're
3058 * traversing the tq in rxi_Start sending packets out because
3059 * packets may move to the freePacketQueue as result of being here!
3060 * So we drop these packets until we're safely out of the
3061 * traversing. Really ugly!
3062 * For fine grain RX locking, we set the acked field in the
3063 * packets and let rxi_Start remove them from the transmit queue.
3065 if (call->flags & RX_CALL_TQ_BUSY) {
3066 #ifdef RX_ENABLE_LOCKS
3067 rxi_SetAcksInTransmitQueue(call);
3070 return np; /* xmitting; drop packet */
3073 rxi_ClearTransmitQueue(call, 0);
3075 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
3076 rxi_ClearTransmitQueue(call, 0);
3077 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3079 if (np->header.type == RX_PACKET_TYPE_ACK) {
3080 /* now check to see if this is an ack packet acknowledging that the
3081 * server actually *lost* some hard-acked data. If this happens we
3082 * ignore this packet, as it may indicate that the server restarted in
3083 * the middle of a call. It is also possible that this is an old ack
3084 * packet. We don't abort the connection in this case, because this
3085 * *might* just be an old ack packet. The right way to detect a server
3086 * restart in the midst of a call is to notice that the server epoch
3088 /* XXX I'm not sure this is exactly right, since tfirst **IS**
3089 * XXX unacknowledged. I think that this is off-by-one, but
3090 * XXX I don't dare change it just yet, since it will
3091 * XXX interact badly with the server-restart detection
3092 * XXX code in receiveackpacket. */
3093 if (ntohl(rx_GetInt32(np, FIRSTACKOFFSET)) < call->tfirst) {
3094 if (rx_stats_active)
3095 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
3096 MUTEX_EXIT(&call->lock);
3097 MUTEX_ENTER(&conn->conn_data_lock);
3099 MUTEX_EXIT(&conn->conn_data_lock);
3103 } /* else not a data packet */
3106 osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
3107 /* Set remote user defined status from packet */
3108 call->remoteStatus = np->header.userStatus;
3110 /* Note the gap between the expected next packet and the actual
3111 * packet that arrived, when the new packet has a smaller serial number
3112 * than expected. Rioses frequently reorder packets all by themselves,
3113 * so this will be quite important with very large window sizes.
3114 * Skew is checked against 0 here to avoid any dependence on the type of
3115 * inPacketSkew (which may be unsigned). In C, -1 > (unsigned) 0 is always
3117 * The inPacketSkew should be a smoothed running value, not just a maximum. MTUXXX
3118 * see CalculateRoundTripTime for an example of how to keep smoothed values.
3119 * I think using a beta of 1/8 is probably appropriate. 93.04.21
3121 MUTEX_ENTER(&conn->conn_data_lock);
3122 skew = conn->lastSerial - np->header.serial;
3123 conn->lastSerial = np->header.serial;
3124 MUTEX_EXIT(&conn->conn_data_lock);
3126 struct rx_peer *peer;
3128 if (skew > peer->inPacketSkew) {
3129 dpf(("*** In skew changed from %d to %d\n",
3130 peer->inPacketSkew, skew));
3131 peer->inPacketSkew = skew;
3135 /* Now do packet type-specific processing */
3136 switch (np->header.type) {
3137 case RX_PACKET_TYPE_DATA:
3138 np = rxi_ReceiveDataPacket(call, np, 1, socket, host, port, tnop,
3141 case RX_PACKET_TYPE_ACK:
3142 /* Respond immediately to ack packets requesting acknowledgement
3144 if (np->header.flags & RX_REQUEST_ACK) {
3146 (void)rxi_SendCallAbort(call, 0, 1, 0);
3148 (void)rxi_SendAck(call, 0, np->header.serial,
3149 RX_ACK_PING_RESPONSE, 1);
3151 np = rxi_ReceiveAckPacket(call, np, 1);
3153 case RX_PACKET_TYPE_ABORT: {
3154 /* An abort packet: reset the call, passing the error up to the user. */
3155 /* What if error is zero? */
3156 /* What if the error is -1? the application will treat it as a timeout. */
3157 afs_int32 errdata = ntohl(*(afs_int32 *) rx_DataOf(np));
3158 dpf(("rxi_ReceivePacket ABORT rx_DataOf = %d", errdata));
3159 rxi_CallError(call, errdata);
3160 MUTEX_EXIT(&call->lock);
3161 MUTEX_ENTER(&conn->conn_data_lock);
3163 MUTEX_EXIT(&conn->conn_data_lock);
3164 return np; /* xmitting; drop packet */
3166 case RX_PACKET_TYPE_BUSY:
3169 case RX_PACKET_TYPE_ACKALL:
3170 /* All packets acknowledged, so we can drop all packets previously
3171 * readied for sending */
3172 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3173 /* XXX Hack. We because we can't release the global rx lock when
3174 * sending packets (osi_NetSend) we drop all ack pkts while we're
3175 * traversing the tq in rxi_Start sending packets out because
3176 * packets may move to the freePacketQueue as result of being
3177 * here! So we drop these packets until we're safely out of the
3178 * traversing. Really ugly!
3179 * For fine grain RX locking, we set the acked field in the packets
3180 * and let rxi_Start remove the packets from the transmit queue.
3182 if (call->flags & RX_CALL_TQ_BUSY) {
3183 #ifdef RX_ENABLE_LOCKS
3184 rxi_SetAcksInTransmitQueue(call);
3186 #else /* RX_ENABLE_LOCKS */
3187 MUTEX_EXIT(&call->lock);
3188 MUTEX_ENTER(&conn->conn_data_lock);
3190 MUTEX_EXIT(&conn->conn_data_lock);
3191 return np; /* xmitting; drop packet */
3192 #endif /* RX_ENABLE_LOCKS */
3194 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3195 rxi_ClearTransmitQueue(call, 0);
3196 rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
3199 /* Should not reach here, unless the peer is broken: send an abort
3201 rxi_CallError(call, RX_PROTOCOL_ERROR);
3202 np = rxi_SendCallAbort(call, np, 1, 0);
3205 /* Note when this last legitimate packet was received, for keep-alive
3206 * processing. Note, we delay getting the time until now in the hope that
3207 * the packet will be delivered to the user before any get time is required
3208 * (if not, then the time won't actually be re-evaluated here). */
3209 call->lastReceiveTime = clock_Sec();
3210 MUTEX_EXIT(&call->lock);
3211 MUTEX_ENTER(&conn->conn_data_lock);
3213 MUTEX_EXIT(&conn->conn_data_lock);
3217 /* return true if this is an "interesting" connection from the point of view
3218 of someone trying to debug the system */
3220 rxi_IsConnInteresting(struct rx_connection *aconn)
3223 struct rx_call *tcall;
3225 if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
3228 for (i = 0; i < RX_MAXCALLS; i++) {
3229 tcall = aconn->call[i];
3231 if ((tcall->state == RX_STATE_PRECALL)
3232 || (tcall->state == RX_STATE_ACTIVE))
3234 if ((tcall->mode == RX_MODE_SENDING)
3235 || (tcall->mode == RX_MODE_RECEIVING))
3243 /* if this is one of the last few packets AND it wouldn't be used by the
3244 receiving call to immediately satisfy a read request, then drop it on
3245 the floor, since accepting it might prevent a lock-holding thread from
3246 making progress in its reading. If a call has been cleared while in
3247 the precall state then ignore all subsequent packets until the call
3248 is assigned to a thread. */
3251 TooLow(struct rx_packet *ap, struct rx_call *acall)
3255 MUTEX_ENTER(&rx_quota_mutex);
3256 if (((ap->header.seq != 1) && (acall->flags & RX_CALL_CLEARED)
3257 && (acall->state == RX_STATE_PRECALL))
3258 || ((rx_nFreePackets < rxi_dataQuota + 2)
3259 && !((ap->header.seq < acall->rnext + rx_initSendWindow)
3260 && (acall->flags & RX_CALL_READER_WAIT)))) {
3263 MUTEX_EXIT(&rx_quota_mutex);
3269 rxi_CheckReachEvent(struct rxevent *event, void *arg1, void *arg2)
3271 struct rx_connection *conn = arg1;
3272 struct rx_call *acall = arg2;
3273 struct rx_call *call = acall;
3274 struct clock when, now;
3277 MUTEX_ENTER(&conn->conn_data_lock);
3278 conn->checkReachEvent = NULL;
3279 waiting = conn->flags & RX_CONN_ATTACHWAIT;
3282 MUTEX_EXIT(&conn->conn_data_lock);
3286 MUTEX_ENTER(&conn->conn_call_lock);
3287 MUTEX_ENTER(&conn->conn_data_lock);
3288 for (i = 0; i < RX_MAXCALLS; i++) {
3289 struct rx_call *tc = conn->call[i];
3290 if (tc && tc->state == RX_STATE_PRECALL) {
3296 /* Indicate that rxi_CheckReachEvent is no longer running by
3297 * clearing the flag. Must be atomic under conn_data_lock to
3298 * avoid a new call slipping by: rxi_CheckConnReach holds
3299 * conn_data_lock while checking RX_CONN_ATTACHWAIT.
3301 conn->flags &= ~RX_CONN_ATTACHWAIT;
3302 MUTEX_EXIT(&conn->conn_data_lock);
3303 MUTEX_EXIT(&conn->conn_call_lock);
3308 MUTEX_ENTER(&call->lock);
3309 rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3311 MUTEX_EXIT(&call->lock);
3313 clock_GetTime(&now);
3315 when.sec += RX_CHECKREACH_TIMEOUT;
3316 MUTEX_ENTER(&conn->conn_data_lock);
3317 if (!conn->checkReachEvent) {
3319 conn->checkReachEvent =
3320 rxevent_PostNow(&when, &now, rxi_CheckReachEvent, conn,
3323 MUTEX_EXIT(&conn->conn_data_lock);
3329 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3331 struct rx_service *service = conn->service;
3332 struct rx_peer *peer = conn->peer;
3333 afs_uint32 now, lastReach;
3335 if (service->checkReach == 0)
3339 MUTEX_ENTER(&peer->peer_lock);
3340 lastReach = peer->lastReachTime;
3341 MUTEX_EXIT(&peer->peer_lock);
3342 if (now - lastReach < RX_CHECKREACH_TTL)
3345 MUTEX_ENTER(&conn->conn_data_lock);
3346 if (conn->flags & RX_CONN_ATTACHWAIT) {
3347 MUTEX_EXIT(&conn->conn_data_lock);
3350 conn->flags |= RX_CONN_ATTACHWAIT;
3351 MUTEX_EXIT(&conn->conn_data_lock);
3352 if (!conn->checkReachEvent)
3353 rxi_CheckReachEvent(NULL, conn, call);
3358 /* try to attach call, if authentication is complete */
3360 TryAttach(struct rx_call *acall, osi_socket socket,
3361 int *tnop, struct rx_call **newcallp,
3364 struct rx_connection *conn = acall->conn;
3366 if (conn->type == RX_SERVER_CONNECTION
3367 && acall->state == RX_STATE_PRECALL) {
3368 /* Don't attach until we have any req'd. authentication. */
3369 if (RXS_CheckAuthentication(conn->securityObject, conn) == 0) {
3370 if (reachOverride || rxi_CheckConnReach(conn, acall) == 0)
3371 rxi_AttachServerProc(acall, socket, tnop, newcallp);
3372 /* Note: this does not necessarily succeed; there
3373 * may not any proc available
3376 rxi_ChallengeOn(acall->conn);
3381 /* A data packet has been received off the interface. This packet is
3382 * appropriate to the call (the call is in the right state, etc.). This
3383 * routine can return a packet to the caller, for re-use */
3386 rxi_ReceiveDataPacket(struct rx_call *call,
3387 struct rx_packet *np, int istack,
3388 osi_socket socket, afs_uint32 host, u_short port,
3389 int *tnop, struct rx_call **newcallp)
3391 int ackNeeded = 0; /* 0 means no, otherwise ack_reason */
3396 afs_uint32 serial=0, flags=0;
3398 struct rx_packet *tnp;
3399 struct clock when, now;
3400 if (rx_stats_active)
3401 rx_MutexIncrement(rx_stats.dataPacketsRead, rx_stats_mutex);
3404 /* If there are no packet buffers, drop this new packet, unless we can find
3405 * packet buffers from inactive calls */
3407 && (rxi_OverQuota(RX_PACKET_CLASS_RECEIVE) || TooLow(np, call))) {
3408 MUTEX_ENTER(&rx_freePktQ_lock);
3409 rxi_NeedMorePackets = TRUE;
3410 MUTEX_EXIT(&rx_freePktQ_lock);
3411 if (rx_stats_active)
3412 rx_MutexIncrement(rx_stats.noPacketBuffersOnRead, rx_stats_mutex);
3413 call->rprev = np->header.serial;
3414 rxi_calltrace(RX_TRACE_DROP, call);
3415 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - quota problems", np));
3417 rxi_ClearReceiveQueue(call);
3418 clock_GetTime(&now);
3420 clock_Add(&when, &rx_softAckDelay);
3421 if (!call->delayedAckEvent
3422 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3423 rxevent_Cancel(call->delayedAckEvent, call,
3424 RX_CALL_REFCOUNT_DELAY);
3425 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3426 call->delayedAckEvent =
3427 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3429 /* we've damaged this call already, might as well do it in. */
3435 * New in AFS 3.5, if the RX_JUMBO_PACKET flag is set then this
3436 * packet is one of several packets transmitted as a single
3437 * datagram. Do not send any soft or hard acks until all packets
3438 * in a jumbogram have been processed. Send negative acks right away.
3440 for (isFirst = 1, tnp = NULL; isFirst || tnp; isFirst = 0) {
3441 /* tnp is non-null when there are more packets in the
3442 * current jumbo gram */
3449 seq = np->header.seq;
3450 serial = np->header.serial;
3451 flags = np->header.flags;
3453 /* If the call is in an error state, send an abort message */
3455 return rxi_SendCallAbort(call, np, istack, 0);
3457 /* The RX_JUMBO_PACKET is set in all but the last packet in each
3458 * AFS 3.5 jumbogram. */
3459 if (flags & RX_JUMBO_PACKET) {
3460 tnp = rxi_SplitJumboPacket(np, host, port, isFirst);
3465 if (np->header.spare != 0) {
3466 MUTEX_ENTER(&call->conn->conn_data_lock);
3467 call->conn->flags |= RX_CONN_USING_PACKET_CKSUM;
3468 MUTEX_EXIT(&call->conn->conn_data_lock);
3471 /* The usual case is that this is the expected next packet */
3472 if (seq == call->rnext) {
3474 /* Check to make sure it is not a duplicate of one already queued */
3475 if (queue_IsNotEmpty(&call->rq)
3476 && queue_First(&call->rq, rx_packet)->header.seq == seq) {
3477 if (rx_stats_active)
3478 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3479 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - duplicate", np));
3480 rxevent_Cancel(call->delayedAckEvent, call,
3481 RX_CALL_REFCOUNT_DELAY);
3482 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3488 /* It's the next packet. Stick it on the receive queue
3489 * for this call. Set newPackets to make sure we wake
3490 * the reader once all packets have been processed */
3491 #ifdef RX_TRACK_PACKETS
3492 np->flags |= RX_PKTFLAG_RQ;
3494 queue_Prepend(&call->rq, np);
3495 #ifdef RXDEBUG_PACKET
3497 #endif /* RXDEBUG_PACKET */
3499 np = NULL; /* We can't use this anymore */
3502 /* If an ack is requested then set a flag to make sure we
3503 * send an acknowledgement for this packet */
3504 if (flags & RX_REQUEST_ACK) {
3505 ackNeeded = RX_ACK_REQUESTED;
3508 /* Keep track of whether we have received the last packet */
3509 if (flags & RX_LAST_PACKET) {
3510 call->flags |= RX_CALL_HAVE_LAST;
3514 /* Check whether we have all of the packets for this call */
3515 if (call->flags & RX_CALL_HAVE_LAST) {
3516 afs_uint32 tseq; /* temporary sequence number */
3517 struct rx_packet *tp; /* Temporary packet pointer */
3518 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3520 for (tseq = seq, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3521 if (tseq != tp->header.seq)
3523 if (tp->header.flags & RX_LAST_PACKET) {
3524 call->flags |= RX_CALL_RECEIVE_DONE;
3531 /* Provide asynchronous notification for those who want it
3532 * (e.g. multi rx) */
3533 if (call->arrivalProc) {
3534 (*call->arrivalProc) (call, call->arrivalProcHandle,
3535 call->arrivalProcArg);
3536 call->arrivalProc = (void (*)())0;
3539 /* Update last packet received */
3542 /* If there is no server process serving this call, grab
3543 * one, if available. We only need to do this once. If a
3544 * server thread is available, this thread becomes a server
3545 * thread and the server thread becomes a listener thread. */
3547 TryAttach(call, socket, tnop, newcallp, 0);
3550 /* This is not the expected next packet. */
3552 /* Determine whether this is a new or old packet, and if it's
3553 * a new one, whether it fits into the current receive window.
3554 * Also figure out whether the packet was delivered in sequence.
3555 * We use the prev variable to determine whether the new packet
3556 * is the successor of its immediate predecessor in the
3557 * receive queue, and the missing flag to determine whether
3558 * any of this packets predecessors are missing. */
3560 afs_uint32 prev; /* "Previous packet" sequence number */
3561 struct rx_packet *tp; /* Temporary packet pointer */
3562 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3563 int missing; /* Are any predecessors missing? */
3565 /* If the new packet's sequence number has been sent to the
3566 * application already, then this is a duplicate */
3567 if (seq < call->rnext) {
3568 if (rx_stats_active)
3569 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3570 rxevent_Cancel(call->delayedAckEvent, call,
3571 RX_CALL_REFCOUNT_DELAY);
3572 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3578 /* If the sequence number is greater than what can be
3579 * accomodated by the current window, then send a negative
3580 * acknowledge and drop the packet */
3581 if ((call->rnext + call->rwind) <= seq) {
3582 rxevent_Cancel(call->delayedAckEvent, call,
3583 RX_CALL_REFCOUNT_DELAY);
3584 np = rxi_SendAck(call, np, serial, RX_ACK_EXCEEDS_WINDOW,
3591 /* Look for the packet in the queue of old received packets */
3592 for (prev = call->rnext - 1, missing =
3593 0, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3594 /*Check for duplicate packet */
3595 if (seq == tp->header.seq) {
3596 if (rx_stats_active)
3597 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3598 rxevent_Cancel(call->delayedAckEvent, call,
3599 RX_CALL_REFCOUNT_DELAY);
3600 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE,
3606 /* If we find a higher sequence packet, break out and
3607 * insert the new packet here. */
3608 if (seq < tp->header.seq)
3610 /* Check for missing packet */
3611 if (tp->header.seq != prev + 1) {
3615 prev = tp->header.seq;
3618 /* Keep track of whether we have received the last packet. */
3619 if (flags & RX_LAST_PACKET) {
3620 call->flags |= RX_CALL_HAVE_LAST;
3623 /* It's within the window: add it to the the receive queue.
3624 * tp is left by the previous loop either pointing at the
3625 * packet before which to insert the new packet, or at the
3626 * queue head if the queue is empty or the packet should be
3628 #ifdef RX_TRACK_PACKETS
3629 np->flags |= RX_PKTFLAG_RQ;
3631 #ifdef RXDEBUG_PACKET
3633 #endif /* RXDEBUG_PACKET */
3634 queue_InsertBefore(tp, np);
3638 /* Check whether we have all of the packets for this call */
3639 if ((call->flags & RX_CALL_HAVE_LAST)
3640 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
3641 afs_uint32 tseq; /* temporary sequence number */
3644 call->rnext, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3645 if (tseq != tp->header.seq)
3647 if (tp->header.flags & RX_LAST_PACKET) {
3648 call->flags |= RX_CALL_RECEIVE_DONE;
3655 /* We need to send an ack of the packet is out of sequence,
3656 * or if an ack was requested by the peer. */
3657 if (seq != prev + 1 || missing) {
3658 ackNeeded = RX_ACK_OUT_OF_SEQUENCE;
3659 } else if (flags & RX_REQUEST_ACK) {
3660 ackNeeded = RX_ACK_REQUESTED;
3663 /* Acknowledge the last packet for each call */
3664 if (flags & RX_LAST_PACKET) {
3675 * If the receiver is waiting for an iovec, fill the iovec
3676 * using the data from the receive queue */
3677 if (call->flags & RX_CALL_IOVEC_WAIT) {
3678 didHardAck = rxi_FillReadVec(call, serial);
3679 /* the call may have been aborted */
3688 /* Wakeup the reader if any */
3689 if ((call->flags & RX_CALL_READER_WAIT)
3690 && (!(call->flags & RX_CALL_IOVEC_WAIT) || !(call->iovNBytes)
3691 || (call->iovNext >= call->iovMax)
3692 || (call->flags & RX_CALL_RECEIVE_DONE))) {
3693 call->flags &= ~RX_CALL_READER_WAIT;
3694 #ifdef RX_ENABLE_LOCKS
3695 CV_BROADCAST(&call->cv_rq);
3697 osi_rxWakeup(&call->rq);
3703 * Send an ack when requested by the peer, or once every
3704 * rxi_SoftAckRate packets until the last packet has been
3705 * received. Always send a soft ack for the last packet in
3706 * the server's reply. */
3708 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3709 np = rxi_SendAck(call, np, serial, ackNeeded, istack);
3710 } else if (call->nSoftAcks > (u_short) rxi_SoftAckRate) {
3711 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3712 np = rxi_SendAck(call, np, serial, RX_ACK_IDLE, istack);
3713 } else if (call->nSoftAcks) {
3714 clock_GetTime(&now);
3716 if (haveLast && !(flags & RX_CLIENT_INITIATED)) {
3717 clock_Add(&when, &rx_lastAckDelay);
3719 clock_Add(&when, &rx_softAckDelay);
3721 if (!call->delayedAckEvent
3722 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3723 rxevent_Cancel(call->delayedAckEvent, call,
3724 RX_CALL_REFCOUNT_DELAY);
3725 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3726 call->delayedAckEvent =
3727 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3729 } else if (call->flags & RX_CALL_RECEIVE_DONE) {
3730 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3737 static void rxi_ComputeRate();
3741 rxi_UpdatePeerReach(struct rx_connection *conn, struct rx_call *acall)
3743 struct rx_peer *peer = conn->peer;
3745 MUTEX_ENTER(&peer->peer_lock);
3746 peer->lastReachTime = clock_Sec();
3747 MUTEX_EXIT(&peer->peer_lock);
3749 MUTEX_ENTER(&conn->conn_data_lock);
3750 if (conn->flags & RX_CONN_ATTACHWAIT) {
3753 conn->flags &= ~RX_CONN_ATTACHWAIT;
3754 MUTEX_EXIT(&conn->conn_data_lock);
3756 for (i = 0; i < RX_MAXCALLS; i++) {
3757 struct rx_call *call = conn->call[i];
3760 MUTEX_ENTER(&call->lock);
3761 /* tnop can be null if newcallp is null */
3762 TryAttach(call, (osi_socket) - 1, NULL, NULL, 1);
3764 MUTEX_EXIT(&call->lock);
3768 MUTEX_EXIT(&conn->conn_data_lock);
3771 #if defined(RXDEBUG) && defined(AFS_NT40_ENV)
3773 rx_ack_reason(int reason)
3776 case RX_ACK_REQUESTED:
3778 case RX_ACK_DUPLICATE:
3780 case RX_ACK_OUT_OF_SEQUENCE:
3782 case RX_ACK_EXCEEDS_WINDOW:
3784 case RX_ACK_NOSPACE:
3788 case RX_ACK_PING_RESPONSE:
3801 /* rxi_ComputePeerNetStats
3803 * Called exclusively by rxi_ReceiveAckPacket to compute network link
3804 * estimates (like RTT and throughput) based on ack packets. Caller
3805 * must ensure that the packet in question is the right one (i.e.
3806 * serial number matches).
3809 rxi_ComputePeerNetStats(struct rx_call *call, struct rx_packet *p,
3810 struct rx_ackPacket *ap, struct rx_packet *np)
3812 struct rx_peer *peer = call->conn->peer;
3814 /* Use RTT if not delayed by client and
3815 * ignore packets that were retransmitted. */
3816 if (!(p->flags & RX_PKTFLAG_ACKED) &&
3817 ap->reason != RX_ACK_DELAY &&
3818 clock_Eq(&p->timeSent, &p->firstSent))
3819 rxi_ComputeRoundTripTime(p, &p->timeSent, peer);
3821 rxi_ComputeRate(peer, call, p, np, ap->reason);
3825 /* The real smarts of the whole thing. */
3827 rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
3830 struct rx_ackPacket *ap;
3832 struct rx_packet *tp;
3833 struct rx_packet *nxp; /* Next packet pointer for queue_Scan */
3834 struct rx_connection *conn = call->conn;
3835 struct rx_peer *peer = conn->peer;
3838 /* because there are CM's that are bogus, sending weird values for this. */
3839 afs_uint32 skew = 0;
3844 int newAckCount = 0;
3845 int maxDgramPackets = 0; /* Set if peer supports AFS 3.5 jumbo datagrams */
3846 int pktsize = 0; /* Set if we need to update the peer mtu */
3847 int conn_data_locked = 0;
3849 if (rx_stats_active)
3850 rx_MutexIncrement(rx_stats.ackPacketsRead, rx_stats_mutex);
3851 ap = (struct rx_ackPacket *)rx_DataOf(np);
3852 nbytes = rx_Contiguous(np) - (int)((ap->acks) - (u_char *) ap);
3854 return np; /* truncated ack packet */
3856 /* depends on ack packet struct */
3857 nAcks = MIN((unsigned)nbytes, (unsigned)ap->nAcks);
3858 first = ntohl(ap->firstPacket);
3859 serial = ntohl(ap->serial);
3860 /* temporarily disabled -- needs to degrade over time
3861 * skew = ntohs(ap->maxSkew); */
3863 /* Ignore ack packets received out of order */
3864 if (first < call->tfirst) {
3868 if (np->header.flags & RX_SLOW_START_OK) {
3869 call->flags |= RX_CALL_SLOW_START_OK;
3872 if (ap->reason == RX_ACK_PING_RESPONSE)
3873 rxi_UpdatePeerReach(conn, call);
3875 if (conn->lastPacketSizeSeq) {
3876 MUTEX_ENTER(&conn->conn_data_lock);
3877 conn_data_locked = 1;
3878 if ((first > conn->lastPacketSizeSeq) && (conn->lastPacketSize)) {
3879 pktsize = conn->lastPacketSize;
3880 conn->lastPacketSize = conn->lastPacketSizeSeq = 0;
3883 if ((ap->reason == RX_ACK_PING_RESPONSE) && (conn->lastPingSizeSer)) {
3884 if (!conn_data_locked) {
3885 MUTEX_ENTER(&conn->conn_data_lock);
3886 conn_data_locked = 1;
3888 if ((conn->lastPingSizeSer == serial) && (conn->lastPingSize)) {
3889 /* process mtu ping ack */
3890 pktsize = conn->lastPingSize;
3891 conn->lastPingSizeSer = conn->lastPingSize = 0;
3895 if (conn_data_locked) {
3896 MUTEX_EXIT(&conn->conn_data_lock);
3897 conn_data_locked = 0;
3901 if (rxdebug_active) {
3905 len = _snprintf(msg, sizeof(msg),
3906 "tid[%d] RACK: reason %s serial %u previous %u seq %u skew %d first %u acks %u space %u ",
3907 GetCurrentThreadId(), rx_ack_reason(ap->reason),
3908 ntohl(ap->serial), ntohl(ap->previousPacket),
3909 (unsigned int)np->header.seq, (unsigned int)skew,
3910 ntohl(ap->firstPacket), ap->nAcks, ntohs(ap->bufferSpace) );
3914 for (offset = 0; offset < nAcks && len < sizeof(msg); offset++)
3915 msg[len++] = (ap->acks[offset] == RX_ACK_TYPE_NACK ? '-' : '*');
3919 OutputDebugString(msg);
3921 #else /* AFS_NT40_ENV */
3924 "RACK: reason %x previous %u seq %u serial %u skew %d first %u",
3925 ap->reason, ntohl(ap->previousPacket),
3926 (unsigned int)np->header.seq, (unsigned int)serial,
3927 (unsigned int)skew, ntohl(ap->firstPacket));
3930 for (offset = 0; offset < nAcks; offset++)
3931 putc(ap->acks[offset] == RX_ACK_TYPE_NACK ? '-' : '*',
3936 #endif /* AFS_NT40_ENV */
3939 MUTEX_ENTER(&peer->peer_lock);
3942 * Start somewhere. Can't assume we can send what we can receive,
3943 * but we are clearly receiving.
3945 if (!peer->maxPacketSize)
3946 peer->maxPacketSize = RX_MIN_PACKET_SIZE+RX_IPUDP_SIZE;
3948 if (pktsize > peer->maxPacketSize) {
3949 peer->maxPacketSize = pktsize;
3950 if ((pktsize-RX_IPUDP_SIZE > peer->ifMTU)) {
3951 peer->ifMTU=pktsize-RX_IPUDP_SIZE;
3952 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
3953 rxi_ScheduleGrowMTUEvent(call, 1);
3958 /* Update the outgoing packet skew value to the latest value of
3959 * the peer's incoming packet skew value. The ack packet, of
3960 * course, could arrive out of order, but that won't affect things
3962 peer->outPacketSkew = skew;
3964 /* Check for packets that no longer need to be transmitted, and
3965 * discard them. This only applies to packets positively
3966 * acknowledged as having been sent to the peer's upper level.
3967 * All other packets must be retained. So only packets with
3968 * sequence numbers < ap->firstPacket are candidates. */
3969 for (queue_Scan(&call->tq, tp, nxp, rx_packet)) {
3970 if (tp->header.seq >= first)
3972 call->tfirst = tp->header.seq + 1;
3973 rxi_ComputePeerNetStats(call, tp, ap, np);
3974 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
3977 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3978 /* XXX Hack. Because we have to release the global rx lock when sending
3979 * packets (osi_NetSend) we drop all acks while we're traversing the tq
3980 * in rxi_Start sending packets out because packets may move to the
3981 * freePacketQueue as result of being here! So we drop these packets until
3982 * we're safely out of the traversing. Really ugly!
3983 * To make it even uglier, if we're using fine grain locking, we can
3984 * set the ack bits in the packets and have rxi_Start remove the packets
3985 * when it's done transmitting.
3987 if (call->flags & RX_CALL_TQ_BUSY) {
3988 #ifdef RX_ENABLE_LOCKS
3989 tp->flags |= RX_PKTFLAG_ACKED;
3990 call->flags |= RX_CALL_TQ_SOME_ACKED;
3991 #else /* RX_ENABLE_LOCKS */
3993 #endif /* RX_ENABLE_LOCKS */
3995 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3998 #ifdef RX_TRACK_PACKETS
3999 tp->flags &= ~RX_PKTFLAG_TQ;
4001 #ifdef RXDEBUG_PACKET
4003 #endif /* RXDEBUG_PACKET */
4004 rxi_FreePacket(tp); /* rxi_FreePacket mustn't wake up anyone, preemptively. */
4009 /* Give rate detector a chance to respond to ping requests */
4010 if (ap->reason == RX_ACK_PING_RESPONSE) {
4011 rxi_ComputeRate(peer, call, 0, np, ap->reason);
4015 /* N.B. we don't turn off any timers here. They'll go away by themselves, anyway */
4017 /* Now go through explicit acks/nacks and record the results in
4018 * the waiting packets. These are packets that can't be released
4019 * yet, even with a positive acknowledge. This positive
4020 * acknowledge only means the packet has been received by the
4021 * peer, not that it will be retained long enough to be sent to
4022 * the peer's upper level. In addition, reset the transmit timers
4023 * of any missing packets (those packets that must be missing
4024 * because this packet was out of sequence) */
4026 call->nSoftAcked = 0;
4027 for (missing = 0, queue_Scan(&call->tq, tp, nxp, rx_packet)) {
4028 /* Update round trip time if the ack was stimulated on receipt
4030 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
4031 #ifdef RX_ENABLE_LOCKS
4032 if (tp->header.seq >= first)
4033 #endif /* RX_ENABLE_LOCKS */
4034 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
4035 rxi_ComputePeerNetStats(call, tp, ap, np);
4037 /* Set the acknowledge flag per packet based on the
4038 * information in the ack packet. An acknowlegded packet can
4039 * be downgraded when the server has discarded a packet it
4040 * soacked previously, or when an ack packet is received
4041 * out of sequence. */
4042 if (tp->header.seq < first) {
4043 /* Implicit ack information */
4044 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
4047 tp->flags |= RX_PKTFLAG_ACKED;
4048 } else if (tp->header.seq < first + nAcks) {
4049 /* Explicit ack information: set it in the packet appropriately */
4050 if (ap->acks[tp->header.seq - first] == RX_ACK_TYPE_ACK) {
4051 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
4053 tp->flags |= RX_PKTFLAG_ACKED;
4060 } else /* RX_ACK_TYPE_NACK */ {
4061 tp->flags &= ~RX_PKTFLAG_ACKED;
4065 tp->flags &= ~RX_PKTFLAG_ACKED;
4070 * Following the suggestion of Phil Kern, we back off the peer's
4071 * timeout value for future packets until a successful response
4072 * is received for an initial transmission.
4074 if (missing && !peer->backedOff) {
4075 struct clock c = peer->timeout;
4076 struct clock max_to = {3, 0};
4078 clock_Add(&peer->timeout, &c);
4079 if (clock_Gt(&peer->timeout, &max_to))
4080 peer->timeout = max_to;
4081 peer->backedOff = 1;
4084 /* If packet isn't yet acked, and it has been transmitted at least
4085 * once, reset retransmit time using latest timeout
4086 * ie, this should readjust the retransmit timer for all outstanding
4087 * packets... So we don't just retransmit when we should know better*/
4089 if (!(tp->flags & RX_PKTFLAG_ACKED) && !clock_IsZero(&tp->retryTime)) {
4090 tp->retryTime = tp->timeSent;
4091 clock_Add(&tp->retryTime, &peer->timeout);
4092 /* shift by eight because one quarter-sec ~ 256 milliseconds */
4093 clock_Addmsec(&(tp->retryTime), ((afs_uint32) tp->backoff) << 8);
4097 /* If the window has been extended by this acknowledge packet,
4098 * then wakeup a sender waiting in alloc for window space, or try
4099 * sending packets now, if he's been sitting on packets due to
4100 * lack of window space */
4101 if (call->tnext < (call->tfirst + call->twind)) {
4102 #ifdef RX_ENABLE_LOCKS
4103 CV_SIGNAL(&call->cv_twind);
4105 if (call->flags & RX_CALL_WAIT_WINDOW_ALLOC) {
4106 call->flags &= ~RX_CALL_WAIT_WINDOW_ALLOC;
4107 osi_rxWakeup(&call->twind);
4110 if (call->flags & RX_CALL_WAIT_WINDOW_SEND) {
4111 call->flags &= ~RX_CALL_WAIT_WINDOW_SEND;
4115 /* if the ack packet has a receivelen field hanging off it,
4116 * update our state */
4117 if (np->length >= rx_AckDataSize(ap->nAcks) + 2 * sizeof(afs_int32)) {
4120 /* If the ack packet has a "recommended" size that is less than
4121 * what I am using now, reduce my size to match */
4122 rx_packetread(np, rx_AckDataSize(ap->nAcks) + (int)sizeof(afs_int32),
4123 (int)sizeof(afs_int32), &tSize);
4124 tSize = (afs_uint32) ntohl(tSize);
4125 peer->natMTU = rxi_AdjustIfMTU(MIN(tSize, peer->ifMTU));
4127 /* Get the maximum packet size to send to this peer */
4128 rx_packetread(np, rx_AckDataSize(ap->nAcks), (int)sizeof(afs_int32),
4130 tSize = (afs_uint32) ntohl(tSize);
4131 tSize = (afs_uint32) MIN(tSize, rx_MyMaxSendSize);
4132 tSize = rxi_AdjustMaxMTU(peer->natMTU, tSize);
4134 /* sanity check - peer might have restarted with different params.
4135 * If peer says "send less", dammit, send less... Peer should never
4136 * be unable to accept packets of the size that prior AFS versions would
4137 * send without asking. */
4138 if (peer->maxMTU != tSize) {
4139 if (peer->maxMTU > tSize) /* possible cong., maxMTU decreased */
4141 peer->maxMTU = tSize;
4142 peer->MTU = MIN(tSize, peer->MTU);
4143 call->MTU = MIN(call->MTU, tSize);
4146 if (np->length == rx_AckDataSize(ap->nAcks) + 3 * sizeof(afs_int32)) {
4149 rx_AckDataSize(ap->nAcks) + 2 * (int)sizeof(afs_int32),
4150 (int)sizeof(afs_int32), &tSize);
4151 tSize = (afs_uint32) ntohl(tSize); /* peer's receive window, if it's */
4152 if (tSize < call->twind) { /* smaller than our send */
4153 call->twind = tSize; /* window, we must send less... */
4154 call->ssthresh = MIN(call->twind, call->ssthresh);
4155 call->conn->twind[call->channel] = call->twind;
4158 /* Only send jumbograms to 3.4a fileservers. 3.3a RX gets the
4159 * network MTU confused with the loopback MTU. Calculate the
4160 * maximum MTU here for use in the slow start code below.
4162 /* Did peer restart with older RX version? */
4163 if (peer->maxDgramPackets > 1) {
4164 peer->maxDgramPackets = 1;
4166 } else if (np->length >=
4167 rx_AckDataSize(ap->nAcks) + 4 * sizeof(afs_int32)) {
4170 rx_AckDataSize(ap->nAcks) + 2 * (int)sizeof(afs_int32),
4171 sizeof(afs_int32), &tSize);
4172 tSize = (afs_uint32) ntohl(tSize);
4174 * As of AFS 3.5 we set the send window to match the receive window.
4176 if (tSize < call->twind) {
4177 call->twind = tSize;
4178 call->conn->twind[call->channel] = call->twind;
4179 call->ssthresh = MIN(call->twind, call->ssthresh);
4180 } else if (tSize > call->twind) {
4181 call->twind = tSize;
4182 call->conn->twind[call->channel] = call->twind;
4186 * As of AFS 3.5, a jumbogram is more than one fixed size
4187 * packet transmitted in a single UDP datagram. If the remote
4188 * MTU is smaller than our local MTU then never send a datagram
4189 * larger than the natural MTU.
4192 rx_AckDataSize(ap->nAcks) + 3 * (int)sizeof(afs_int32),
4193 (int)sizeof(afs_int32), &tSize);
4194 maxDgramPackets = (afs_uint32) ntohl(tSize);
4195 maxDgramPackets = MIN(maxDgramPackets, rxi_nDgramPackets);
4197 MIN(maxDgramPackets, (int)(peer->ifDgramPackets));
4198 maxDgramPackets = MIN(maxDgramPackets, tSize);
4199 if (maxDgramPackets > 1) {
4200 peer->maxDgramPackets = maxDgramPackets;
4201 call->MTU = RX_JUMBOBUFFERSIZE + RX_HEADER_SIZE;
4203 peer->maxDgramPackets = 1;
4204 call->MTU = peer->natMTU;
4206 } else if (peer->maxDgramPackets > 1) {
4207 /* Restarted with lower version of RX */
4208 peer->maxDgramPackets = 1;
4210 } else if (peer->maxDgramPackets > 1
4211 || peer->maxMTU != OLD_MAX_PACKET_SIZE) {
4212 /* Restarted with lower version of RX */
4213 peer->maxMTU = OLD_MAX_PACKET_SIZE;
4214 peer->natMTU = OLD_MAX_PACKET_SIZE;
4215 peer->MTU = OLD_MAX_PACKET_SIZE;
4216 peer->maxDgramPackets = 1;
4217 peer->nDgramPackets = 1;
4219 call->MTU = OLD_MAX_PACKET_SIZE;
4224 * Calculate how many datagrams were successfully received after
4225 * the first missing packet and adjust the negative ack counter
4230 nNacked = (nNacked + call->nDgramPackets - 1) / call->nDgramPackets;
4231 if (call->nNacks < nNacked) {
4232 call->nNacks = nNacked;
4235 call->nAcks += newAckCount;
4239 if (call->flags & RX_CALL_FAST_RECOVER) {
4241 call->cwind = MIN((int)(call->cwind + 1), rx_maxSendWindow);
4243 call->flags &= ~RX_CALL_FAST_RECOVER;
4244 call->cwind = call->nextCwind;
4245 call->nextCwind = 0;
4248 call->nCwindAcks = 0;
4249 } else if (nNacked && call->nNacks >= (u_short) rx_nackThreshold) {
4250 /* Three negative acks in a row trigger congestion recovery */
4251 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
4252 MUTEX_EXIT(&peer->peer_lock);
4253 if (call->flags & RX_CALL_FAST_RECOVER_WAIT) {
4254 /* someone else is waiting to start recovery */
4257 call->flags |= RX_CALL_FAST_RECOVER_WAIT;
4258 rxi_WaitforTQBusy(call);
4259 MUTEX_ENTER(&peer->peer_lock);
4260 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
4261 call->flags &= ~RX_CALL_FAST_RECOVER_WAIT;
4262 call->flags |= RX_CALL_FAST_RECOVER;
4263 call->ssthresh = MAX(4, MIN((int)call->cwind, (int)call->twind)) >> 1;
4265 MIN((int)(call->ssthresh + rx_nackThreshold), rx_maxSendWindow);
4266 call->nDgramPackets = MAX(2, (int)call->nDgramPackets) >> 1;
4267 call->nextCwind = call->ssthresh;
4270 peer->MTU = call->MTU;
4271 peer->cwind = call->nextCwind;
4272 peer->nDgramPackets = call->nDgramPackets;
4274 call->congestSeq = peer->congestSeq;
4275 /* Reset the resend times on the packets that were nacked
4276 * so we will retransmit as soon as the window permits*/
4277 for (acked = 0, queue_ScanBackwards(&call->tq, tp, nxp, rx_packet)) {
4279 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
4280 clock_Zero(&tp->retryTime);
4282 } else if (tp->flags & RX_PKTFLAG_ACKED) {
4287 /* If cwind is smaller than ssthresh, then increase
4288 * the window one packet for each ack we receive (exponential
4290 * If cwind is greater than or equal to ssthresh then increase
4291 * the congestion window by one packet for each cwind acks we
4292 * receive (linear growth). */
4293 if (call->cwind < call->ssthresh) {
4295 MIN((int)call->ssthresh, (int)(call->cwind + newAckCount));
4296 call->nCwindAcks = 0;
4298 call->nCwindAcks += newAckCount;
4299 if (call->nCwindAcks >= call->cwind) {
4300 call->nCwindAcks = 0;
4301 call->cwind = MIN((int)(call->cwind + 1), rx_maxSendWindow);
4305 * If we have received several acknowledgements in a row then
4306 * it is time to increase the size of our datagrams
4308 if ((int)call->nAcks > rx_nDgramThreshold) {
4309 if (peer->maxDgramPackets > 1) {
4310 if (call->nDgramPackets < peer->maxDgramPackets) {
4311 call->nDgramPackets++;
4313 call->MTU = RX_HEADER_SIZE + RX_JUMBOBUFFERSIZE;
4314 } else if (call->MTU < peer->maxMTU) {
4315 /* don't upgrade if we can't handle it */
4316 if ((call->nDgramPackets == 1) && (call->MTU >= peer->ifMTU))
4317 call->MTU = peer->ifMTU;
4319 call->MTU += peer->natMTU;
4320 call->MTU = MIN(call->MTU, peer->maxMTU);
4327 MUTEX_EXIT(&peer->peer_lock); /* rxi_Start will lock peer. */
4329 /* Servers need to hold the call until all response packets have
4330 * been acknowledged. Soft acks are good enough since clients
4331 * are not allowed to clear their receive queues. */
4332 if (call->state == RX_STATE_HOLD
4333 && call->tfirst + call->nSoftAcked >= call->tnext) {
4334 call->state = RX_STATE_DALLY;
4335 rxi_ClearTransmitQueue(call, 0);
4336 rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
4337 } else if (!queue_IsEmpty(&call->tq)) {
4338 rxi_Start(0, call, 0, istack);