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"
75 #define AFSOP_STOP_RXCALLBACK 210 /* Stop CALLBACK process */
76 #define AFSOP_STOP_AFS 211 /* Stop AFS process */
77 #define AFSOP_STOP_BKG 212 /* Stop BKG process */
79 extern afs_int32 afs_termState;
81 #include "sys/lockl.h"
82 #include "sys/lock_def.h"
83 #endif /* AFS_AIX41_ENV */
84 # include "afs/rxgen_consts.h"
86 # include <sys/types.h>
96 # include <afs/afsutil.h>
97 # include <WINNT\afsreg.h>
99 # include <sys/socket.h>
100 # include <sys/file.h>
102 # include <sys/stat.h>
103 # include <netinet/in.h>
104 # include <sys/time.h>
107 # include "rx_user.h"
108 # include "rx_clock.h"
109 # include "rx_queue.h"
110 # include "rx_atomic.h"
111 # include "rx_globals.h"
112 # include "rx_trace.h"
113 # include "rx_internal.h"
114 # include "rx_stats.h"
115 # include <afs/rxgen_consts.h>
119 #ifdef AFS_PTHREAD_ENV
121 int (*registerProgram) (pid_t, char *) = 0;
122 int (*swapNameProgram) (pid_t, const char *, char *) = 0;
125 int (*registerProgram) (PROCESS, char *) = 0;
126 int (*swapNameProgram) (PROCESS, const char *, char *) = 0;
130 /* Local static routines */
131 static void rxi_DestroyConnectionNoLock(struct rx_connection *conn);
132 #ifdef RX_ENABLE_LOCKS
133 static void rxi_SetAcksInTransmitQueue(struct rx_call *call);
136 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
138 rx_atomic_t rxi_start_aborted; /* rxi_start awoke after rxi_Send in error.*/
139 rx_atomic_t rxi_start_in_error;
141 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
144 * rxi_rpc_peer_stat_cnt counts the total number of peer stat structures
145 * currently allocated within rx. This number is used to allocate the
146 * memory required to return the statistics when queried.
147 * Protected by the rx_rpc_stats mutex.
150 static unsigned int rxi_rpc_peer_stat_cnt;
153 * rxi_rpc_process_stat_cnt counts the total number of local process stat
154 * structures currently allocated within rx. The number is used to allocate
155 * the memory required to return the statistics when queried.
156 * Protected by the rx_rpc_stats mutex.
159 static unsigned int rxi_rpc_process_stat_cnt;
161 rx_atomic_t rx_nWaiting = RX_ATOMIC_INIT(0);
162 rx_atomic_t rx_nWaited = RX_ATOMIC_INIT(0);
164 #if !defined(offsetof)
165 #include <stddef.h> /* for definition of offsetof() */
168 #ifdef RX_ENABLE_LOCKS
169 afs_kmutex_t rx_atomic_mutex;
172 #ifdef AFS_PTHREAD_ENV
176 * Use procedural initialization of mutexes/condition variables
180 extern afs_kmutex_t rx_quota_mutex;
181 extern afs_kmutex_t rx_pthread_mutex;
182 extern afs_kmutex_t rx_packets_mutex;
183 extern afs_kmutex_t des_init_mutex;
184 extern afs_kmutex_t des_random_mutex;
185 extern afs_kmutex_t rx_clock_mutex;
186 extern afs_kmutex_t rxi_connCacheMutex;
187 extern afs_kmutex_t rx_event_mutex;
188 extern afs_kmutex_t osi_malloc_mutex;
189 extern afs_kmutex_t event_handler_mutex;
190 extern afs_kmutex_t listener_mutex;
191 extern afs_kmutex_t rx_if_init_mutex;
192 extern afs_kmutex_t rx_if_mutex;
193 extern afs_kmutex_t rxkad_client_uid_mutex;
194 extern afs_kmutex_t rxkad_random_mutex;
196 extern afs_kcondvar_t rx_event_handler_cond;
197 extern afs_kcondvar_t rx_listener_cond;
199 static afs_kmutex_t epoch_mutex;
200 static afs_kmutex_t rx_init_mutex;
201 static afs_kmutex_t rx_debug_mutex;
202 static afs_kmutex_t rx_rpc_stats;
205 rxi_InitPthread(void)
207 MUTEX_INIT(&rx_clock_mutex, "clock", MUTEX_DEFAULT, 0);
208 MUTEX_INIT(&rx_stats_mutex, "stats", MUTEX_DEFAULT, 0);
209 MUTEX_INIT(&rx_atomic_mutex, "atomic", MUTEX_DEFAULT, 0);
210 MUTEX_INIT(&rx_quota_mutex, "quota", MUTEX_DEFAULT, 0);
211 MUTEX_INIT(&rx_pthread_mutex, "pthread", MUTEX_DEFAULT, 0);
212 MUTEX_INIT(&rx_packets_mutex, "packets", MUTEX_DEFAULT, 0);
213 MUTEX_INIT(&epoch_mutex, "epoch", MUTEX_DEFAULT, 0);
214 MUTEX_INIT(&rx_init_mutex, "init", MUTEX_DEFAULT, 0);
215 MUTEX_INIT(&rx_event_mutex, "event", MUTEX_DEFAULT, 0);
216 MUTEX_INIT(&des_init_mutex, "des", MUTEX_DEFAULT, 0);
217 MUTEX_INIT(&des_random_mutex, "random", MUTEX_DEFAULT, 0);
218 MUTEX_INIT(&osi_malloc_mutex, "malloc", MUTEX_DEFAULT, 0);
219 MUTEX_INIT(&event_handler_mutex, "event handler", MUTEX_DEFAULT, 0);
220 MUTEX_INIT(&rxi_connCacheMutex, "conn cache", MUTEX_DEFAULT, 0);
221 MUTEX_INIT(&listener_mutex, "listener", MUTEX_DEFAULT, 0);
222 MUTEX_INIT(&rx_if_init_mutex, "if init", MUTEX_DEFAULT, 0);
223 MUTEX_INIT(&rx_if_mutex, "if", MUTEX_DEFAULT, 0);
224 MUTEX_INIT(&rxkad_client_uid_mutex, "uid", MUTEX_DEFAULT, 0);
225 MUTEX_INIT(&rxkad_random_mutex, "rxkad random", MUTEX_DEFAULT, 0);
226 MUTEX_INIT(&rx_debug_mutex, "debug", MUTEX_DEFAULT, 0);
228 assert(pthread_cond_init
229 (&rx_event_handler_cond, (const pthread_condattr_t *)0) == 0);
230 assert(pthread_cond_init(&rx_listener_cond, (const pthread_condattr_t *)0)
232 assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
233 assert(pthread_key_create(&rx_ts_info_key, NULL) == 0);
235 rxkad_global_stats_init();
237 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
238 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
239 #ifdef RX_ENABLE_LOCKS
242 #endif /* RX_LOCKS_DB */
243 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
244 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
246 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
248 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
250 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
252 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
253 MUTEX_INIT(&rxi_keyCreate_lock, "rxi_keyCreate_lock", MUTEX_DEFAULT, 0);
254 #endif /* RX_ENABLE_LOCKS */
257 pthread_once_t rx_once_init = PTHREAD_ONCE_INIT;
258 #define INIT_PTHREAD_LOCKS \
259 assert(pthread_once(&rx_once_init, rxi_InitPthread)==0)
261 * The rx_stats_mutex mutex protects the following global variables:
262 * rxi_lowConnRefCount
263 * rxi_lowPeerRefCount
272 * The rx_quota_mutex mutex protects the following global variables:
280 * The rx_freePktQ_lock protects the following global variables:
285 * The rx_packets_mutex mutex protects the following global variables:
293 * The rx_pthread_mutex mutex protects the following global variables:
294 * rxi_fcfs_thread_num
297 #define INIT_PTHREAD_LOCKS
301 /* Variables for handling the minProcs implementation. availProcs gives the
302 * number of threads available in the pool at this moment (not counting dudes
303 * executing right now). totalMin gives the total number of procs required
304 * for handling all minProcs requests. minDeficit is a dynamic variable
305 * tracking the # of procs required to satisfy all of the remaining minProcs
307 * For fine grain locking to work, the quota check and the reservation of
308 * a server thread has to come while rxi_availProcs and rxi_minDeficit
309 * are locked. To this end, the code has been modified under #ifdef
310 * RX_ENABLE_LOCKS so that quota checks and reservation occur at the
311 * same time. A new function, ReturnToServerPool() returns the allocation.
313 * A call can be on several queue's (but only one at a time). When
314 * rxi_ResetCall wants to remove the call from a queue, it has to ensure
315 * that no one else is touching the queue. To this end, we store the address
316 * of the queue lock in the call structure (under the call lock) when we
317 * put the call on a queue, and we clear the call_queue_lock when the
318 * call is removed from a queue (once the call lock has been obtained).
319 * This allows rxi_ResetCall to safely synchronize with others wishing
320 * to manipulate the queue.
323 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
324 static afs_kmutex_t rx_rpc_stats;
325 void rxi_StartUnlocked(struct rxevent *event, void *call,
326 void *arg1, int istack);
329 /* We keep a "last conn pointer" in rxi_FindConnection. The odds are
330 ** pretty good that the next packet coming in is from the same connection
331 ** as the last packet, since we're send multiple packets in a transmit window.
333 struct rx_connection *rxLastConn = 0;
335 #ifdef RX_ENABLE_LOCKS
336 /* The locking hierarchy for rx fine grain locking is composed of these
339 * rx_connHashTable_lock - synchronizes conn creation, rx_connHashTable access
340 * conn_call_lock - used to synchonize rx_EndCall and rx_NewCall
341 * call->lock - locks call data fields.
342 * These are independent of each other:
343 * rx_freeCallQueue_lock
348 * serverQueueEntry->lock
349 * rx_peerHashTable_lock - locked under rx_connHashTable_lock
351 * peer->lock - locks peer data fields.
352 * conn_data_lock - that more than one thread is not updating a conn data
353 * field at the same time.
362 * Do we need a lock to protect the peer field in the conn structure?
363 * conn->peer was previously a constant for all intents and so has no
364 * lock protecting this field. The multihomed client delta introduced
365 * a RX code change : change the peer field in the connection structure
366 * to that remote interface from which the last packet for this
367 * connection was sent out. This may become an issue if further changes
370 #define SET_CALL_QUEUE_LOCK(C, L) (C)->call_queue_lock = (L)
371 #define CLEAR_CALL_QUEUE_LOCK(C) (C)->call_queue_lock = NULL
373 /* rxdb_fileID is used to identify the lock location, along with line#. */
374 static int rxdb_fileID = RXDB_FILE_RX;
375 #endif /* RX_LOCKS_DB */
376 #else /* RX_ENABLE_LOCKS */
377 #define SET_CALL_QUEUE_LOCK(C, L)
378 #define CLEAR_CALL_QUEUE_LOCK(C)
379 #endif /* RX_ENABLE_LOCKS */
380 struct rx_serverQueueEntry *rx_waitForPacket = 0;
381 struct rx_serverQueueEntry *rx_waitingForPacket = 0;
383 /* ------------Exported Interfaces------------- */
385 /* This function allows rxkad to set the epoch to a suitably random number
386 * which rx_NewConnection will use in the future. The principle purpose is to
387 * get rxnull connections to use the same epoch as the rxkad connections do, at
388 * least once the first rxkad connection is established. This is important now
389 * that the host/port addresses aren't used in FindConnection: the uniqueness
390 * of epoch/cid matters and the start time won't do. */
392 #ifdef AFS_PTHREAD_ENV
394 * This mutex protects the following global variables:
398 #define LOCK_EPOCH MUTEX_ENTER(&epoch_mutex)
399 #define UNLOCK_EPOCH MUTEX_EXIT(&epoch_mutex)
403 #endif /* AFS_PTHREAD_ENV */
406 rx_SetEpoch(afs_uint32 epoch)
413 /* Initialize rx. A port number may be mentioned, in which case this
414 * becomes the default port number for any service installed later.
415 * If 0 is provided for the port number, a random port will be chosen
416 * by the kernel. Whether this will ever overlap anything in
417 * /etc/services is anybody's guess... Returns 0 on success, -1 on
422 int rxinit_status = 1;
423 #ifdef AFS_PTHREAD_ENV
425 * This mutex protects the following global variables:
429 #define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
430 #define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
433 #define UNLOCK_RX_INIT
437 rx_InitHost(u_int host, u_int port)
444 char *htable, *ptable;
451 if (rxinit_status == 0) {
452 tmp_status = rxinit_status;
454 return tmp_status; /* Already started; return previous error code. */
460 if (afs_winsockInit() < 0)
466 * Initialize anything necessary to provide a non-premptive threading
469 rxi_InitializeThreadSupport();
472 /* Allocate and initialize a socket for client and perhaps server
475 rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
476 if (rx_socket == OSI_NULLSOCKET) {
480 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
483 #endif /* RX_LOCKS_DB */
484 MUTEX_INIT(&rx_stats_mutex, "rx_stats_mutex", MUTEX_DEFAULT, 0);
485 MUTEX_INIT(&rx_quota_mutex, "rx_quota_mutex", MUTEX_DEFAULT, 0);
486 MUTEX_INIT(&rx_pthread_mutex, "rx_pthread_mutex", MUTEX_DEFAULT, 0);
487 MUTEX_INIT(&rx_packets_mutex, "rx_packets_mutex", MUTEX_DEFAULT, 0);
488 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
489 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
490 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
491 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
493 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
495 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
497 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
499 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
500 #if defined(AFS_HPUX110_ENV)
502 rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
503 #endif /* AFS_HPUX110_ENV */
504 #endif /* RX_ENABLE_LOCKS && KERNEL */
507 rx_connDeadTime = 12;
508 rx_tranquil = 0; /* reset flag */
509 rxi_ResetStatistics();
511 osi_Alloc(rx_hashTableSize * sizeof(struct rx_connection *));
512 PIN(htable, rx_hashTableSize * sizeof(struct rx_connection *)); /* XXXXX */
513 memset(htable, 0, rx_hashTableSize * sizeof(struct rx_connection *));
514 ptable = (char *)osi_Alloc(rx_hashTableSize * sizeof(struct rx_peer *));
515 PIN(ptable, rx_hashTableSize * sizeof(struct rx_peer *)); /* XXXXX */
516 memset(ptable, 0, rx_hashTableSize * sizeof(struct rx_peer *));
518 /* Malloc up a bunch of packets & buffers */
520 queue_Init(&rx_freePacketQueue);
521 rxi_NeedMorePackets = FALSE;
522 rx_nPackets = 0; /* rx_nPackets is managed by rxi_MorePackets* */
524 /* enforce a minimum number of allocated packets */
525 if (rx_extraPackets < rxi_nSendFrags * rx_maxSendWindow)
526 rx_extraPackets = rxi_nSendFrags * rx_maxSendWindow;
528 /* allocate the initial free packet pool */
529 #ifdef RX_ENABLE_TSFPQ
530 rxi_MorePacketsTSFPQ(rx_extraPackets + RX_MAX_QUOTA + 2, RX_TS_FPQ_FLUSH_GLOBAL, 0);
531 #else /* RX_ENABLE_TSFPQ */
532 rxi_MorePackets(rx_extraPackets + RX_MAX_QUOTA + 2); /* fudge */
533 #endif /* RX_ENABLE_TSFPQ */
540 #if defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
541 tv.tv_sec = clock_now.sec;
542 tv.tv_usec = clock_now.usec;
543 srand((unsigned int)tv.tv_usec);
550 #if defined(KERNEL) && !defined(UKERNEL)
551 /* Really, this should never happen in a real kernel */
554 struct sockaddr_in addr;
556 int addrlen = sizeof(addr);
558 socklen_t addrlen = sizeof(addr);
560 if (getsockname((intptr_t)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
564 rx_port = addr.sin_port;
567 rx_stats.minRtt.sec = 9999999;
569 rx_SetEpoch(tv.tv_sec | 0x80000000);
571 rx_SetEpoch(tv.tv_sec); /* Start time of this package, rxkad
572 * will provide a randomer value. */
574 MUTEX_ENTER(&rx_quota_mutex);
575 rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
576 MUTEX_EXIT(&rx_quota_mutex);
577 /* *Slightly* random start time for the cid. This is just to help
578 * out with the hashing function at the peer */
579 rx_nextCid = ((tv.tv_sec ^ tv.tv_usec) << RX_CIDSHIFT);
580 rx_connHashTable = (struct rx_connection **)htable;
581 rx_peerHashTable = (struct rx_peer **)ptable;
583 rx_lastAckDelay.sec = 0;
584 rx_lastAckDelay.usec = 400000; /* 400 milliseconds */
585 rx_hardAckDelay.sec = 0;
586 rx_hardAckDelay.usec = 100000; /* 100 milliseconds */
587 rx_softAckDelay.sec = 0;
588 rx_softAckDelay.usec = 100000; /* 100 milliseconds */
590 rxevent_Init(20, rxi_ReScheduleEvents);
592 /* Initialize various global queues */
593 queue_Init(&rx_idleServerQueue);
594 queue_Init(&rx_incomingCallQueue);
595 queue_Init(&rx_freeCallQueue);
597 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
598 /* Initialize our list of usable IP addresses. */
602 /* Start listener process (exact function is dependent on the
603 * implementation environment--kernel or user space) */
607 tmp_status = rxinit_status = 0;
615 return rx_InitHost(htonl(INADDR_ANY), port);
618 /* called with unincremented nRequestsRunning to see if it is OK to start
619 * a new thread in this service. Could be "no" for two reasons: over the
620 * max quota, or would prevent others from reaching their min quota.
622 #ifdef RX_ENABLE_LOCKS
623 /* This verion of QuotaOK reserves quota if it's ok while the
624 * rx_serverPool_lock is held. Return quota using ReturnToServerPool().
627 QuotaOK(struct rx_service *aservice)
629 /* check if over max quota */
630 if (aservice->nRequestsRunning >= aservice->maxProcs) {
634 /* under min quota, we're OK */
635 /* otherwise, can use only if there are enough to allow everyone
636 * to go to their min quota after this guy starts.
639 MUTEX_ENTER(&rx_quota_mutex);
640 if ((aservice->nRequestsRunning < aservice->minProcs)
641 || (rxi_availProcs > rxi_minDeficit)) {
642 aservice->nRequestsRunning++;
643 /* just started call in minProcs pool, need fewer to maintain
645 if (aservice->nRequestsRunning <= aservice->minProcs)
648 MUTEX_EXIT(&rx_quota_mutex);
651 MUTEX_EXIT(&rx_quota_mutex);
657 ReturnToServerPool(struct rx_service *aservice)
659 aservice->nRequestsRunning--;
660 MUTEX_ENTER(&rx_quota_mutex);
661 if (aservice->nRequestsRunning < aservice->minProcs)
664 MUTEX_EXIT(&rx_quota_mutex);
667 #else /* RX_ENABLE_LOCKS */
669 QuotaOK(struct rx_service *aservice)
672 /* under min quota, we're OK */
673 if (aservice->nRequestsRunning < aservice->minProcs)
676 /* check if over max quota */
677 if (aservice->nRequestsRunning >= aservice->maxProcs)
680 /* otherwise, can use only if there are enough to allow everyone
681 * to go to their min quota after this guy starts.
683 MUTEX_ENTER(&rx_quota_mutex);
684 if (rxi_availProcs > rxi_minDeficit)
686 MUTEX_EXIT(&rx_quota_mutex);
689 #endif /* RX_ENABLE_LOCKS */
692 /* Called by rx_StartServer to start up lwp's to service calls.
693 NExistingProcs gives the number of procs already existing, and which
694 therefore needn't be created. */
696 rxi_StartServerProcs(int nExistingProcs)
698 struct rx_service *service;
703 /* For each service, reserve N processes, where N is the "minimum"
704 * number of processes that MUST be able to execute a request in parallel,
705 * at any time, for that process. Also compute the maximum difference
706 * between any service's maximum number of processes that can run
707 * (i.e. the maximum number that ever will be run, and a guarantee
708 * that this number will run if other services aren't running), and its
709 * minimum number. The result is the extra number of processes that
710 * we need in order to provide the latter guarantee */
711 for (i = 0; i < RX_MAX_SERVICES; i++) {
713 service = rx_services[i];
714 if (service == (struct rx_service *)0)
716 nProcs += service->minProcs;
717 diff = service->maxProcs - service->minProcs;
721 nProcs += maxdiff; /* Extra processes needed to allow max number requested to run in any given service, under good conditions */
722 nProcs -= nExistingProcs; /* Subtract the number of procs that were previously created for use as server procs */
723 for (i = 0; i < nProcs; i++) {
724 rxi_StartServerProc(rx_ServerProc, rx_stackSize);
730 /* This routine is only required on Windows */
732 rx_StartClientThread(void)
734 #ifdef AFS_PTHREAD_ENV
736 pid = pthread_self();
737 #endif /* AFS_PTHREAD_ENV */
739 #endif /* AFS_NT40_ENV */
741 /* This routine must be called if any services are exported. If the
742 * donateMe flag is set, the calling process is donated to the server
745 rx_StartServer(int donateMe)
747 struct rx_service *service;
753 /* Start server processes, if necessary (exact function is dependent
754 * on the implementation environment--kernel or user space). DonateMe
755 * will be 1 if there is 1 pre-existing proc, i.e. this one. In this
756 * case, one less new proc will be created rx_StartServerProcs.
758 rxi_StartServerProcs(donateMe);
760 /* count up the # of threads in minProcs, and add set the min deficit to
761 * be that value, too.
763 for (i = 0; i < RX_MAX_SERVICES; i++) {
764 service = rx_services[i];
765 if (service == (struct rx_service *)0)
767 MUTEX_ENTER(&rx_quota_mutex);
768 rxi_totalMin += service->minProcs;
769 /* below works even if a thread is running, since minDeficit would
770 * still have been decremented and later re-incremented.
772 rxi_minDeficit += service->minProcs;
773 MUTEX_EXIT(&rx_quota_mutex);
776 /* Turn on reaping of idle server connections */
777 rxi_ReapConnections(NULL, NULL, NULL);
786 #ifdef AFS_PTHREAD_ENV
788 pid = afs_pointer_to_int(pthread_self());
789 #else /* AFS_PTHREAD_ENV */
791 LWP_CurrentProcess(&pid);
792 #endif /* AFS_PTHREAD_ENV */
794 sprintf(name, "srv_%d", ++nProcs);
796 (*registerProgram) (pid, name);
798 #endif /* AFS_NT40_ENV */
799 rx_ServerProc(NULL); /* Never returns */
801 #ifdef RX_ENABLE_TSFPQ
802 /* no use leaving packets around in this thread's local queue if
803 * it isn't getting donated to the server thread pool.
805 rxi_FlushLocalPacketsTSFPQ();
806 #endif /* RX_ENABLE_TSFPQ */
810 /* Create a new client connection to the specified service, using the
811 * specified security object to implement the security model for this
813 struct rx_connection *
814 rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
815 struct rx_securityClass *securityObject,
816 int serviceSecurityIndex)
820 struct rx_connection *conn;
825 dpf(("rx_NewConnection(host %x, port %u, service %u, securityObject %p, "
826 "serviceSecurityIndex %d)\n",
827 ntohl(shost), ntohs(sport), sservice, securityObject,
828 serviceSecurityIndex));
830 /* Vasilsi said: "NETPRI protects Cid and Alloc", but can this be true in
831 * the case of kmem_alloc? */
832 conn = rxi_AllocConnection();
833 #ifdef RX_ENABLE_LOCKS
834 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
835 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
836 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
839 MUTEX_ENTER(&rx_connHashTable_lock);
840 cid = (rx_nextCid += RX_MAXCALLS);
841 conn->type = RX_CLIENT_CONNECTION;
843 conn->epoch = rx_epoch;
844 conn->peer = rxi_FindPeer(shost, sport, 0, 1);
845 conn->serviceId = sservice;
846 conn->securityObject = securityObject;
847 conn->securityData = (void *) 0;
848 conn->securityIndex = serviceSecurityIndex;
849 rx_SetConnDeadTime(conn, rx_connDeadTime);
850 rx_SetConnSecondsUntilNatPing(conn, 0);
851 conn->ackRate = RX_FAST_ACK_RATE;
853 conn->specific = NULL;
854 conn->challengeEvent = NULL;
855 conn->delayedAbortEvent = NULL;
856 conn->abortCount = 0;
858 for (i = 0; i < RX_MAXCALLS; i++) {
859 conn->twind[i] = rx_initSendWindow;
860 conn->rwind[i] = rx_initReceiveWindow;
863 RXS_NewConnection(securityObject, conn);
865 CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
867 conn->refCount++; /* no lock required since only this thread knows... */
868 conn->next = rx_connHashTable[hashindex];
869 rx_connHashTable[hashindex] = conn;
871 rx_atomic_inc(&rx_stats.nClientConns);
872 MUTEX_EXIT(&rx_connHashTable_lock);
878 rx_SetConnDeadTime(struct rx_connection *conn, int seconds)
880 /* The idea is to set the dead time to a value that allows several
881 * keepalives to be dropped without timing out the connection. */
882 conn->secondsUntilDead = MAX(seconds, 6);
883 conn->secondsUntilPing = conn->secondsUntilDead / 6;
886 int rxi_lowPeerRefCount = 0;
887 int rxi_lowConnRefCount = 0;
890 * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
891 * NOTE: must not be called with rx_connHashTable_lock held.
894 rxi_CleanupConnection(struct rx_connection *conn)
896 /* Notify the service exporter, if requested, that this connection
897 * is being destroyed */
898 if (conn->type == RX_SERVER_CONNECTION && conn->service->destroyConnProc)
899 (*conn->service->destroyConnProc) (conn);
901 /* Notify the security module that this connection is being destroyed */
902 RXS_DestroyConnection(conn->securityObject, conn);
904 /* If this is the last connection using the rx_peer struct, set its
905 * idle time to now. rxi_ReapConnections will reap it if it's still
906 * idle (refCount == 0) after rx_idlePeerTime (60 seconds) have passed.
908 MUTEX_ENTER(&rx_peerHashTable_lock);
909 if (conn->peer->refCount < 2) {
910 conn->peer->idleWhen = clock_Sec();
911 if (conn->peer->refCount < 1) {
912 conn->peer->refCount = 1;
913 if (rx_stats_active) {
914 MUTEX_ENTER(&rx_stats_mutex);
915 rxi_lowPeerRefCount++;
916 MUTEX_EXIT(&rx_stats_mutex);
920 conn->peer->refCount--;
921 MUTEX_EXIT(&rx_peerHashTable_lock);
925 if (conn->type == RX_SERVER_CONNECTION)
926 rx_atomic_dec(&rx_stats.nServerConns);
928 rx_atomic_dec(&rx_stats.nClientConns);
931 if (conn->specific) {
933 for (i = 0; i < conn->nSpecific; i++) {
934 if (conn->specific[i] && rxi_keyCreate_destructor[i])
935 (*rxi_keyCreate_destructor[i]) (conn->specific[i]);
936 conn->specific[i] = NULL;
938 free(conn->specific);
940 conn->specific = NULL;
944 MUTEX_DESTROY(&conn->conn_call_lock);
945 MUTEX_DESTROY(&conn->conn_data_lock);
946 CV_DESTROY(&conn->conn_call_cv);
948 rxi_FreeConnection(conn);
951 /* Destroy the specified connection */
953 rxi_DestroyConnection(struct rx_connection *conn)
955 MUTEX_ENTER(&rx_connHashTable_lock);
956 rxi_DestroyConnectionNoLock(conn);
957 /* conn should be at the head of the cleanup list */
958 if (conn == rx_connCleanup_list) {
959 rx_connCleanup_list = rx_connCleanup_list->next;
960 MUTEX_EXIT(&rx_connHashTable_lock);
961 rxi_CleanupConnection(conn);
963 #ifdef RX_ENABLE_LOCKS
965 MUTEX_EXIT(&rx_connHashTable_lock);
967 #endif /* RX_ENABLE_LOCKS */
971 rxi_DestroyConnectionNoLock(struct rx_connection *conn)
973 struct rx_connection **conn_ptr;
975 struct rx_packet *packet;
982 MUTEX_ENTER(&conn->conn_data_lock);
983 if (conn->refCount > 0)
986 if (rx_stats_active) {
987 MUTEX_ENTER(&rx_stats_mutex);
988 rxi_lowConnRefCount++;
989 MUTEX_EXIT(&rx_stats_mutex);
993 if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
994 /* Busy; wait till the last guy before proceeding */
995 MUTEX_EXIT(&conn->conn_data_lock);
1000 /* If the client previously called rx_NewCall, but it is still
1001 * waiting, treat this as a running call, and wait to destroy the
1002 * connection later when the call completes. */
1003 if ((conn->type == RX_CLIENT_CONNECTION)
1004 && (conn->flags & (RX_CONN_MAKECALL_WAITING|RX_CONN_MAKECALL_ACTIVE))) {
1005 conn->flags |= RX_CONN_DESTROY_ME;
1006 MUTEX_EXIT(&conn->conn_data_lock);
1010 MUTEX_EXIT(&conn->conn_data_lock);
1012 /* Check for extant references to this connection */
1013 for (i = 0; i < RX_MAXCALLS; i++) {
1014 struct rx_call *call = conn->call[i];
1017 if (conn->type == RX_CLIENT_CONNECTION) {
1018 MUTEX_ENTER(&call->lock);
1019 if (call->delayedAckEvent) {
1020 /* Push the final acknowledgment out now--there
1021 * won't be a subsequent call to acknowledge the
1022 * last reply packets */
1023 rxevent_Cancel(call->delayedAckEvent, call,
1024 RX_CALL_REFCOUNT_DELAY);
1025 if (call->state == RX_STATE_PRECALL
1026 || call->state == RX_STATE_ACTIVE) {
1027 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1029 rxi_AckAll(NULL, call, 0);
1032 MUTEX_EXIT(&call->lock);
1036 #ifdef RX_ENABLE_LOCKS
1038 if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
1039 MUTEX_EXIT(&conn->conn_data_lock);
1041 /* Someone is accessing a packet right now. */
1045 #endif /* RX_ENABLE_LOCKS */
1048 /* Don't destroy the connection if there are any call
1049 * structures still in use */
1050 MUTEX_ENTER(&conn->conn_data_lock);
1051 conn->flags |= RX_CONN_DESTROY_ME;
1052 MUTEX_EXIT(&conn->conn_data_lock);
1057 if (conn->natKeepAliveEvent) {
1058 rxi_NatKeepAliveOff(conn);
1061 if (conn->delayedAbortEvent) {
1062 rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
1063 packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
1065 MUTEX_ENTER(&conn->conn_data_lock);
1066 rxi_SendConnectionAbort(conn, packet, 0, 1);
1067 MUTEX_EXIT(&conn->conn_data_lock);
1068 rxi_FreePacket(packet);
1072 /* Remove from connection hash table before proceeding */
1074 &rx_connHashTable[CONN_HASH
1075 (peer->host, peer->port, conn->cid, conn->epoch,
1077 for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
1078 if (*conn_ptr == conn) {
1079 *conn_ptr = conn->next;
1083 /* if the conn that we are destroying was the last connection, then we
1084 * clear rxLastConn as well */
1085 if (rxLastConn == conn)
1088 /* Make sure the connection is completely reset before deleting it. */
1089 /* get rid of pending events that could zap us later */
1090 if (conn->challengeEvent)
1091 rxevent_Cancel(conn->challengeEvent, (struct rx_call *)0, 0);
1092 if (conn->checkReachEvent)
1093 rxevent_Cancel(conn->checkReachEvent, (struct rx_call *)0, 0);
1094 if (conn->natKeepAliveEvent)
1095 rxevent_Cancel(conn->natKeepAliveEvent, (struct rx_call *)0, 0);
1097 /* Add the connection to the list of destroyed connections that
1098 * need to be cleaned up. This is necessary to avoid deadlocks
1099 * in the routines we call to inform others that this connection is
1100 * being destroyed. */
1101 conn->next = rx_connCleanup_list;
1102 rx_connCleanup_list = conn;
1105 /* Externally available version */
1107 rx_DestroyConnection(struct rx_connection *conn)
1112 rxi_DestroyConnection(conn);
1117 rx_GetConnection(struct rx_connection *conn)
1122 MUTEX_ENTER(&conn->conn_data_lock);
1124 MUTEX_EXIT(&conn->conn_data_lock);
1128 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1129 /* Wait for the transmit queue to no longer be busy.
1130 * requires the call->lock to be held */
1131 static void rxi_WaitforTQBusy(struct rx_call *call) {
1132 while (call->flags & RX_CALL_TQ_BUSY) {
1133 call->flags |= RX_CALL_TQ_WAIT;
1135 #ifdef RX_ENABLE_LOCKS
1136 osirx_AssertMine(&call->lock, "rxi_WaitforTQ lock");
1137 CV_WAIT(&call->cv_tq, &call->lock);
1138 #else /* RX_ENABLE_LOCKS */
1139 osi_rxSleep(&call->tq);
1140 #endif /* RX_ENABLE_LOCKS */
1142 if (call->tqWaiters == 0) {
1143 call->flags &= ~RX_CALL_TQ_WAIT;
1149 /* Start a new rx remote procedure call, on the specified connection.
1150 * If wait is set to 1, wait for a free call channel; otherwise return
1151 * 0. Maxtime gives the maximum number of seconds this call may take,
1152 * after rx_NewCall returns. After this time interval, a call to any
1153 * of rx_SendData, rx_ReadData, etc. will fail with RX_CALL_TIMEOUT.
1154 * For fine grain locking, we hold the conn_call_lock in order to
1155 * to ensure that we don't get signalle after we found a call in an active
1156 * state and before we go to sleep.
1159 rx_NewCall(struct rx_connection *conn)
1162 struct rx_call *call;
1163 struct clock queueTime;
1167 dpf(("rx_NewCall(conn %"AFS_PTR_FMT")\n", conn));
1170 clock_GetTime(&queueTime);
1172 * Check if there are others waiting for a new call.
1173 * If so, let them go first to avoid starving them.
1174 * This is a fairly simple scheme, and might not be
1175 * a complete solution for large numbers of waiters.
1177 * makeCallWaiters keeps track of the number of
1178 * threads waiting to make calls and the
1179 * RX_CONN_MAKECALL_WAITING flag bit is used to
1180 * indicate that there are indeed calls waiting.
1181 * The flag is set when the waiter is incremented.
1182 * It is only cleared when makeCallWaiters is 0.
1183 * This prevents us from accidently destroying the
1184 * connection while it is potentially about to be used.
1186 MUTEX_ENTER(&conn->conn_call_lock);
1187 MUTEX_ENTER(&conn->conn_data_lock);
1188 while (conn->flags & RX_CONN_MAKECALL_ACTIVE) {
1189 conn->flags |= RX_CONN_MAKECALL_WAITING;
1190 conn->makeCallWaiters++;
1191 MUTEX_EXIT(&conn->conn_data_lock);
1193 #ifdef RX_ENABLE_LOCKS
1194 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1198 MUTEX_ENTER(&conn->conn_data_lock);
1199 conn->makeCallWaiters--;
1200 if (conn->makeCallWaiters == 0)
1201 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1204 /* We are now the active thread in rx_NewCall */
1205 conn->flags |= RX_CONN_MAKECALL_ACTIVE;
1206 MUTEX_EXIT(&conn->conn_data_lock);
1211 for (i = 0; i < RX_MAXCALLS; i++) {
1212 call = conn->call[i];
1214 if (call->state == RX_STATE_DALLY) {
1215 MUTEX_ENTER(&call->lock);
1216 if (call->state == RX_STATE_DALLY) {
1218 * We are setting the state to RX_STATE_RESET to
1219 * ensure that no one else will attempt to use this
1220 * call once we drop the conn->conn_call_lock and
1221 * call->lock. We must drop the conn->conn_call_lock
1222 * before calling rxi_ResetCall because the process
1223 * of clearing the transmit queue can block for an
1224 * extended period of time. If we block while holding
1225 * the conn->conn_call_lock, then all rx_EndCall
1226 * processing will block as well. This has a detrimental
1227 * effect on overall system performance.
1229 call->state = RX_STATE_RESET;
1230 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1231 MUTEX_EXIT(&conn->conn_call_lock);
1232 rxi_ResetCall(call, 0);
1233 (*call->callNumber)++;
1234 if (MUTEX_TRYENTER(&conn->conn_call_lock))
1238 * If we failed to be able to safely obtain the
1239 * conn->conn_call_lock we will have to drop the
1240 * call->lock to avoid a deadlock. When the call->lock
1241 * is released the state of the call can change. If it
1242 * is no longer RX_STATE_RESET then some other thread is
1245 MUTEX_EXIT(&call->lock);
1246 MUTEX_ENTER(&conn->conn_call_lock);
1247 MUTEX_ENTER(&call->lock);
1249 if (call->state == RX_STATE_RESET)
1253 * If we get here it means that after dropping
1254 * the conn->conn_call_lock and call->lock that
1255 * the call is no longer ours. If we can't find
1256 * a free call in the remaining slots we should
1257 * not go immediately to RX_CONN_MAKECALL_WAITING
1258 * because by dropping the conn->conn_call_lock
1259 * we have given up synchronization with rx_EndCall.
1260 * Instead, cycle through one more time to see if
1261 * we can find a call that can call our own.
1263 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
1266 MUTEX_EXIT(&call->lock);
1269 /* rxi_NewCall returns with mutex locked */
1270 call = rxi_NewCall(conn, i);
1271 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1275 if (i < RX_MAXCALLS) {
1281 MUTEX_ENTER(&conn->conn_data_lock);
1282 conn->flags |= RX_CONN_MAKECALL_WAITING;
1283 conn->makeCallWaiters++;
1284 MUTEX_EXIT(&conn->conn_data_lock);
1286 #ifdef RX_ENABLE_LOCKS
1287 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1291 MUTEX_ENTER(&conn->conn_data_lock);
1292 conn->makeCallWaiters--;
1293 if (conn->makeCallWaiters == 0)
1294 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1295 MUTEX_EXIT(&conn->conn_data_lock);
1297 /* Client is initially in send mode */
1298 call->state = RX_STATE_ACTIVE;
1299 call->error = conn->error;
1301 call->mode = RX_MODE_ERROR;
1303 call->mode = RX_MODE_SENDING;
1305 /* remember start time for call in case we have hard dead time limit */
1306 call->queueTime = queueTime;
1307 clock_GetTime(&call->startTime);
1308 hzero(call->bytesSent);
1309 hzero(call->bytesRcvd);
1311 /* Turn on busy protocol. */
1312 rxi_KeepAliveOn(call);
1314 /* Attempt MTU discovery */
1315 rxi_GrowMTUOn(call);
1318 * We are no longer the active thread in rx_NewCall
1320 MUTEX_ENTER(&conn->conn_data_lock);
1321 conn->flags &= ~RX_CONN_MAKECALL_ACTIVE;
1322 MUTEX_EXIT(&conn->conn_data_lock);
1325 * Wake up anyone else who might be giving us a chance to
1326 * run (see code above that avoids resource starvation).
1328 #ifdef RX_ENABLE_LOCKS
1329 CV_BROADCAST(&conn->conn_call_cv);
1333 MUTEX_EXIT(&conn->conn_call_lock);
1335 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1336 if (call->flags & (RX_CALL_TQ_BUSY | RX_CALL_TQ_CLEARME)) {
1337 osi_Panic("rx_NewCall call about to be used without an empty tq");
1339 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1341 MUTEX_EXIT(&call->lock);
1344 dpf(("rx_NewCall(call %"AFS_PTR_FMT")\n", call));
1349 rxi_HasActiveCalls(struct rx_connection *aconn)
1352 struct rx_call *tcall;
1356 for (i = 0; i < RX_MAXCALLS; i++) {
1357 if ((tcall = aconn->call[i])) {
1358 if ((tcall->state == RX_STATE_ACTIVE)
1359 || (tcall->state == RX_STATE_PRECALL)) {
1370 rxi_GetCallNumberVector(struct rx_connection *aconn,
1371 afs_int32 * aint32s)
1374 struct rx_call *tcall;
1378 for (i = 0; i < RX_MAXCALLS; i++) {
1379 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1380 aint32s[i] = aconn->callNumber[i] + 1;
1382 aint32s[i] = aconn->callNumber[i];
1389 rxi_SetCallNumberVector(struct rx_connection *aconn,
1390 afs_int32 * aint32s)
1393 struct rx_call *tcall;
1397 for (i = 0; i < RX_MAXCALLS; i++) {
1398 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1399 aconn->callNumber[i] = aint32s[i] - 1;
1401 aconn->callNumber[i] = aint32s[i];
1407 /* Advertise a new service. A service is named locally by a UDP port
1408 * number plus a 16-bit service id. Returns (struct rx_service *) 0
1411 char *serviceName; Name for identification purposes (e.g. the
1412 service name might be used for probing for
1415 rx_NewServiceHost(afs_uint32 host, u_short port, u_short serviceId,
1416 char *serviceName, struct rx_securityClass **securityObjects,
1417 int nSecurityObjects,
1418 afs_int32(*serviceProc) (struct rx_call * acall))
1420 osi_socket socket = OSI_NULLSOCKET;
1421 struct rx_service *tservice;
1427 if (serviceId == 0) {
1429 "rx_NewService: service id for service %s is not non-zero.\n",
1436 "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",
1444 tservice = rxi_AllocService();
1447 #ifdef RX_ENABLE_LOCKS
1448 MUTEX_INIT(&tservice->svc_data_lock, "svc data lock", MUTEX_DEFAULT, 0);
1451 for (i = 0; i < RX_MAX_SERVICES; i++) {
1452 struct rx_service *service = rx_services[i];
1454 if (port == service->servicePort && host == service->serviceHost) {
1455 if (service->serviceId == serviceId) {
1456 /* The identical service has already been
1457 * installed; if the caller was intending to
1458 * change the security classes used by this
1459 * service, he/she loses. */
1461 "rx_NewService: tried to install service %s with service id %d, which is already in use for service %s\n",
1462 serviceName, serviceId, service->serviceName);
1464 rxi_FreeService(tservice);
1467 /* Different service, same port: re-use the socket
1468 * which is bound to the same port */
1469 socket = service->socket;
1472 if (socket == OSI_NULLSOCKET) {
1473 /* If we don't already have a socket (from another
1474 * service on same port) get a new one */
1475 socket = rxi_GetHostUDPSocket(host, port);
1476 if (socket == OSI_NULLSOCKET) {
1478 rxi_FreeService(tservice);
1483 service->socket = socket;
1484 service->serviceHost = host;
1485 service->servicePort = port;
1486 service->serviceId = serviceId;
1487 service->serviceName = serviceName;
1488 service->nSecurityObjects = nSecurityObjects;
1489 service->securityObjects = securityObjects;
1490 service->minProcs = 0;
1491 service->maxProcs = 1;
1492 service->idleDeadTime = 60;
1493 service->idleDeadErr = 0;
1494 service->connDeadTime = rx_connDeadTime;
1495 service->executeRequestProc = serviceProc;
1496 service->checkReach = 0;
1497 service->nSpecific = 0;
1498 service->specific = NULL;
1499 rx_services[i] = service; /* not visible until now */
1505 rxi_FreeService(tservice);
1506 (osi_Msg "rx_NewService: cannot support > %d services\n",
1511 /* Set configuration options for all of a service's security objects */
1514 rx_SetSecurityConfiguration(struct rx_service *service,
1515 rx_securityConfigVariables type,
1519 for (i = 0; i<service->nSecurityObjects; i++) {
1520 if (service->securityObjects[i]) {
1521 RXS_SetConfiguration(service->securityObjects[i], NULL, type,
1529 rx_NewService(u_short port, u_short serviceId, char *serviceName,
1530 struct rx_securityClass **securityObjects, int nSecurityObjects,
1531 afs_int32(*serviceProc) (struct rx_call * acall))
1533 return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
1536 /* Generic request processing loop. This routine should be called
1537 * by the implementation dependent rx_ServerProc. If socketp is
1538 * non-null, it will be set to the file descriptor that this thread
1539 * is now listening on. If socketp is null, this routine will never
1542 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1544 struct rx_call *call;
1546 struct rx_service *tservice = NULL;
1553 call = rx_GetCall(threadID, tservice, socketp);
1554 if (socketp && *socketp != OSI_NULLSOCKET) {
1555 /* We are now a listener thread */
1560 /* if server is restarting( typically smooth shutdown) then do not
1561 * allow any new calls.
1564 if (rx_tranquil && (call != NULL)) {
1568 MUTEX_ENTER(&call->lock);
1570 rxi_CallError(call, RX_RESTARTING);
1571 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1573 MUTEX_EXIT(&call->lock);
1577 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1578 #ifdef RX_ENABLE_LOCKS
1580 #endif /* RX_ENABLE_LOCKS */
1581 afs_termState = AFSOP_STOP_AFS;
1582 afs_osi_Wakeup(&afs_termState);
1583 #ifdef RX_ENABLE_LOCKS
1585 #endif /* RX_ENABLE_LOCKS */
1590 tservice = call->conn->service;
1592 if (tservice->beforeProc)
1593 (*tservice->beforeProc) (call);
1595 code = tservice->executeRequestProc(call);
1597 if (tservice->afterProc)
1598 (*tservice->afterProc) (call, code);
1600 rx_EndCall(call, code);
1601 if (rx_stats_active) {
1602 MUTEX_ENTER(&rx_stats_mutex);
1604 MUTEX_EXIT(&rx_stats_mutex);
1611 rx_WakeupServerProcs(void)
1613 struct rx_serverQueueEntry *np, *tqp;
1617 MUTEX_ENTER(&rx_serverPool_lock);
1619 #ifdef RX_ENABLE_LOCKS
1620 if (rx_waitForPacket)
1621 CV_BROADCAST(&rx_waitForPacket->cv);
1622 #else /* RX_ENABLE_LOCKS */
1623 if (rx_waitForPacket)
1624 osi_rxWakeup(rx_waitForPacket);
1625 #endif /* RX_ENABLE_LOCKS */
1626 MUTEX_ENTER(&freeSQEList_lock);
1627 for (np = rx_FreeSQEList; np; np = tqp) {
1628 tqp = *(struct rx_serverQueueEntry **)np;
1629 #ifdef RX_ENABLE_LOCKS
1630 CV_BROADCAST(&np->cv);
1631 #else /* RX_ENABLE_LOCKS */
1633 #endif /* RX_ENABLE_LOCKS */
1635 MUTEX_EXIT(&freeSQEList_lock);
1636 for (queue_Scan(&rx_idleServerQueue, np, tqp, rx_serverQueueEntry)) {
1637 #ifdef RX_ENABLE_LOCKS
1638 CV_BROADCAST(&np->cv);
1639 #else /* RX_ENABLE_LOCKS */
1641 #endif /* RX_ENABLE_LOCKS */
1643 MUTEX_EXIT(&rx_serverPool_lock);
1648 * One thing that seems to happen is that all the server threads get
1649 * tied up on some empty or slow call, and then a whole bunch of calls
1650 * arrive at once, using up the packet pool, so now there are more
1651 * empty calls. The most critical resources here are server threads
1652 * and the free packet pool. The "doreclaim" code seems to help in
1653 * general. I think that eventually we arrive in this state: there
1654 * are lots of pending calls which do have all their packets present,
1655 * so they won't be reclaimed, are multi-packet calls, so they won't
1656 * be scheduled until later, and thus are tying up most of the free
1657 * packet pool for a very long time.
1659 * 1. schedule multi-packet calls if all the packets are present.
1660 * Probably CPU-bound operation, useful to return packets to pool.
1661 * Do what if there is a full window, but the last packet isn't here?
1662 * 3. preserve one thread which *only* runs "best" calls, otherwise
1663 * it sleeps and waits for that type of call.
1664 * 4. Don't necessarily reserve a whole window for each thread. In fact,
1665 * the current dataquota business is badly broken. The quota isn't adjusted
1666 * to reflect how many packets are presently queued for a running call.
1667 * So, when we schedule a queued call with a full window of packets queued
1668 * up for it, that *should* free up a window full of packets for other 2d-class
1669 * calls to be able to use from the packet pool. But it doesn't.
1671 * NB. Most of the time, this code doesn't run -- since idle server threads
1672 * sit on the idle server queue and are assigned by "...ReceivePacket" as soon
1673 * as a new call arrives.
1675 /* Sleep until a call arrives. Returns a pointer to the call, ready
1676 * for an rx_Read. */
1677 #ifdef RX_ENABLE_LOCKS
1679 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1681 struct rx_serverQueueEntry *sq;
1682 struct rx_call *call = (struct rx_call *)0;
1683 struct rx_service *service = NULL;
1686 MUTEX_ENTER(&freeSQEList_lock);
1688 if ((sq = rx_FreeSQEList)) {
1689 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1690 MUTEX_EXIT(&freeSQEList_lock);
1691 } else { /* otherwise allocate a new one and return that */
1692 MUTEX_EXIT(&freeSQEList_lock);
1693 sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1694 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1695 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1698 MUTEX_ENTER(&rx_serverPool_lock);
1699 if (cur_service != NULL) {
1700 ReturnToServerPool(cur_service);
1703 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1704 struct rx_call *tcall, *ncall, *choice2 = NULL;
1706 /* Scan for eligible incoming calls. A call is not eligible
1707 * if the maximum number of calls for its service type are
1708 * already executing */
1709 /* One thread will process calls FCFS (to prevent starvation),
1710 * while the other threads may run ahead looking for calls which
1711 * have all their input data available immediately. This helps
1712 * keep threads from blocking, waiting for data from the client. */
1713 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1714 service = tcall->conn->service;
1715 if (!QuotaOK(service)) {
1718 MUTEX_ENTER(&rx_pthread_mutex);
1719 if (tno == rxi_fcfs_thread_num
1720 || !tcall->queue_item_header.next) {
1721 MUTEX_EXIT(&rx_pthread_mutex);
1722 /* If we're the fcfs thread , then we'll just use
1723 * this call. If we haven't been able to find an optimal
1724 * choice, and we're at the end of the list, then use a
1725 * 2d choice if one has been identified. Otherwise... */
1726 call = (choice2 ? choice2 : tcall);
1727 service = call->conn->service;
1729 MUTEX_EXIT(&rx_pthread_mutex);
1730 if (!queue_IsEmpty(&tcall->rq)) {
1731 struct rx_packet *rp;
1732 rp = queue_First(&tcall->rq, rx_packet);
1733 if (rp->header.seq == 1) {
1735 || (rp->header.flags & RX_LAST_PACKET)) {
1737 } else if (rxi_2dchoice && !choice2
1738 && !(tcall->flags & RX_CALL_CLEARED)
1739 && (tcall->rprev > rxi_HardAckRate)) {
1749 ReturnToServerPool(service);
1756 MUTEX_EXIT(&rx_serverPool_lock);
1757 MUTEX_ENTER(&call->lock);
1759 if (call->flags & RX_CALL_WAIT_PROC) {
1760 call->flags &= ~RX_CALL_WAIT_PROC;
1761 rx_atomic_dec(&rx_nWaiting);
1764 if (call->state != RX_STATE_PRECALL || call->error) {
1765 MUTEX_EXIT(&call->lock);
1766 MUTEX_ENTER(&rx_serverPool_lock);
1767 ReturnToServerPool(service);
1772 if (queue_IsEmpty(&call->rq)
1773 || queue_First(&call->rq, rx_packet)->header.seq != 1)
1774 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1776 CLEAR_CALL_QUEUE_LOCK(call);
1779 /* If there are no eligible incoming calls, add this process
1780 * to the idle server queue, to wait for one */
1784 *socketp = OSI_NULLSOCKET;
1786 sq->socketp = socketp;
1787 queue_Append(&rx_idleServerQueue, sq);
1788 #ifndef AFS_AIX41_ENV
1789 rx_waitForPacket = sq;
1791 rx_waitingForPacket = sq;
1792 #endif /* AFS_AIX41_ENV */
1794 CV_WAIT(&sq->cv, &rx_serverPool_lock);
1796 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1797 MUTEX_EXIT(&rx_serverPool_lock);
1798 return (struct rx_call *)0;
1801 } while (!(call = sq->newcall)
1802 && !(socketp && *socketp != OSI_NULLSOCKET));
1803 MUTEX_EXIT(&rx_serverPool_lock);
1805 MUTEX_ENTER(&call->lock);
1811 MUTEX_ENTER(&freeSQEList_lock);
1812 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1813 rx_FreeSQEList = sq;
1814 MUTEX_EXIT(&freeSQEList_lock);
1817 clock_GetTime(&call->startTime);
1818 call->state = RX_STATE_ACTIVE;
1819 call->mode = RX_MODE_RECEIVING;
1820 #ifdef RX_KERNEL_TRACE
1821 if (ICL_SETACTIVE(afs_iclSetp)) {
1822 int glockOwner = ISAFS_GLOCK();
1825 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1826 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1833 rxi_calltrace(RX_CALL_START, call);
1834 dpf(("rx_GetCall(port=%d, service=%d) ==> call %"AFS_PTR_FMT"\n",
1835 call->conn->service->servicePort, call->conn->service->serviceId,
1838 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1839 MUTEX_EXIT(&call->lock);
1841 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
1846 #else /* RX_ENABLE_LOCKS */
1848 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1850 struct rx_serverQueueEntry *sq;
1851 struct rx_call *call = (struct rx_call *)0, *choice2;
1852 struct rx_service *service = NULL;
1856 MUTEX_ENTER(&freeSQEList_lock);
1858 if ((sq = rx_FreeSQEList)) {
1859 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1860 MUTEX_EXIT(&freeSQEList_lock);
1861 } else { /* otherwise allocate a new one and return that */
1862 MUTEX_EXIT(&freeSQEList_lock);
1863 sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1864 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1865 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1867 MUTEX_ENTER(&sq->lock);
1869 if (cur_service != NULL) {
1870 cur_service->nRequestsRunning--;
1871 MUTEX_ENTER(&rx_quota_mutex);
1872 if (cur_service->nRequestsRunning < cur_service->minProcs)
1875 MUTEX_EXIT(&rx_quota_mutex);
1877 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1878 struct rx_call *tcall, *ncall;
1879 /* Scan for eligible incoming calls. A call is not eligible
1880 * if the maximum number of calls for its service type are
1881 * already executing */
1882 /* One thread will process calls FCFS (to prevent starvation),
1883 * while the other threads may run ahead looking for calls which
1884 * have all their input data available immediately. This helps
1885 * keep threads from blocking, waiting for data from the client. */
1886 choice2 = (struct rx_call *)0;
1887 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1888 service = tcall->conn->service;
1889 if (QuotaOK(service)) {
1890 MUTEX_ENTER(&rx_pthread_mutex);
1891 if (tno == rxi_fcfs_thread_num
1892 || !tcall->queue_item_header.next) {
1893 MUTEX_EXIT(&rx_pthread_mutex);
1894 /* If we're the fcfs thread, then we'll just use
1895 * this call. If we haven't been able to find an optimal
1896 * choice, and we're at the end of the list, then use a
1897 * 2d choice if one has been identified. Otherwise... */
1898 call = (choice2 ? choice2 : tcall);
1899 service = call->conn->service;
1901 MUTEX_EXIT(&rx_pthread_mutex);
1902 if (!queue_IsEmpty(&tcall->rq)) {
1903 struct rx_packet *rp;
1904 rp = queue_First(&tcall->rq, rx_packet);
1905 if (rp->header.seq == 1
1907 || (rp->header.flags & RX_LAST_PACKET))) {
1909 } else if (rxi_2dchoice && !choice2
1910 && !(tcall->flags & RX_CALL_CLEARED)
1911 && (tcall->rprev > rxi_HardAckRate)) {
1925 /* we can't schedule a call if there's no data!!! */
1926 /* send an ack if there's no data, if we're missing the
1927 * first packet, or we're missing something between first
1928 * and last -- there's a "hole" in the incoming data. */
1929 if (queue_IsEmpty(&call->rq)
1930 || queue_First(&call->rq, rx_packet)->header.seq != 1
1931 || call->rprev != queue_Last(&call->rq, rx_packet)->header.seq)
1932 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1934 call->flags &= (~RX_CALL_WAIT_PROC);
1935 service->nRequestsRunning++;
1936 /* just started call in minProcs pool, need fewer to maintain
1938 MUTEX_ENTER(&rx_quota_mutex);
1939 if (service->nRequestsRunning <= service->minProcs)
1942 MUTEX_EXIT(&rx_quota_mutex);
1943 rx_atomic_dec(&rx_nWaiting);
1944 /* MUTEX_EXIT(&call->lock); */
1946 /* If there are no eligible incoming calls, add this process
1947 * to the idle server queue, to wait for one */
1950 *socketp = OSI_NULLSOCKET;
1952 sq->socketp = socketp;
1953 queue_Append(&rx_idleServerQueue, sq);
1957 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1959 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
1960 return (struct rx_call *)0;
1963 } while (!(call = sq->newcall)
1964 && !(socketp && *socketp != OSI_NULLSOCKET));
1966 MUTEX_EXIT(&sq->lock);
1968 MUTEX_ENTER(&freeSQEList_lock);
1969 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1970 rx_FreeSQEList = sq;
1971 MUTEX_EXIT(&freeSQEList_lock);
1974 clock_GetTime(&call->startTime);
1975 call->state = RX_STATE_ACTIVE;
1976 call->mode = RX_MODE_RECEIVING;
1977 #ifdef RX_KERNEL_TRACE
1978 if (ICL_SETACTIVE(afs_iclSetp)) {
1979 int glockOwner = ISAFS_GLOCK();
1982 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1983 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1990 rxi_calltrace(RX_CALL_START, call);
1991 dpf(("rx_GetCall(port=%d, service=%d) ==> call %p\n",
1992 call->conn->service->servicePort, call->conn->service->serviceId,
1995 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
2002 #endif /* RX_ENABLE_LOCKS */
2006 /* Establish a procedure to be called when a packet arrives for a
2007 * call. This routine will be called at most once after each call,
2008 * and will also be called if there is an error condition on the or
2009 * the call is complete. Used by multi rx to build a selection
2010 * function which determines which of several calls is likely to be a
2011 * good one to read from.
2012 * NOTE: the way this is currently implemented it is probably only a
2013 * good idea to (1) use it immediately after a newcall (clients only)
2014 * and (2) only use it once. Other uses currently void your warranty
2017 rx_SetArrivalProc(struct rx_call *call,
2018 void (*proc) (struct rx_call * call,
2021 void * handle, int arg)
2023 call->arrivalProc = proc;
2024 call->arrivalProcHandle = handle;
2025 call->arrivalProcArg = arg;
2028 /* Call is finished (possibly prematurely). Return rc to the peer, if
2029 * appropriate, and return the final error code from the conversation
2033 rx_EndCall(struct rx_call *call, afs_int32 rc)
2035 struct rx_connection *conn = call->conn;
2039 dpf(("rx_EndCall(call %"AFS_PTR_FMT" rc %d error %d abortCode %d)\n",
2040 call, rc, call->error, call->abortCode));
2043 MUTEX_ENTER(&call->lock);
2045 if (rc == 0 && call->error == 0) {
2046 call->abortCode = 0;
2047 call->abortCount = 0;
2050 call->arrivalProc = (void (*)())0;
2051 if (rc && call->error == 0) {
2052 rxi_CallError(call, rc);
2053 /* Send an abort message to the peer if this error code has
2054 * only just been set. If it was set previously, assume the
2055 * peer has already been sent the error code or will request it
2057 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
2059 if (conn->type == RX_SERVER_CONNECTION) {
2060 /* Make sure reply or at least dummy reply is sent */
2061 if (call->mode == RX_MODE_RECEIVING) {
2062 rxi_WriteProc(call, 0, 0);
2064 if (call->mode == RX_MODE_SENDING) {
2065 rxi_FlushWrite(call);
2067 rxi_calltrace(RX_CALL_END, call);
2068 /* Call goes to hold state until reply packets are acknowledged */
2069 if (call->tfirst + call->nSoftAcked < call->tnext) {
2070 call->state = RX_STATE_HOLD;
2072 call->state = RX_STATE_DALLY;
2073 rxi_ClearTransmitQueue(call, 0);
2074 rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
2075 rxevent_Cancel(call->keepAliveEvent, call,
2076 RX_CALL_REFCOUNT_ALIVE);
2078 } else { /* Client connection */
2080 /* Make sure server receives input packets, in the case where
2081 * no reply arguments are expected */
2082 if ((call->mode == RX_MODE_SENDING)
2083 || (call->mode == RX_MODE_RECEIVING && call->rnext == 1)) {
2084 (void)rxi_ReadProc(call, &dummy, 1);
2087 /* If we had an outstanding delayed ack, be nice to the server
2088 * and force-send it now.
2090 if (call->delayedAckEvent) {
2091 rxevent_Cancel(call->delayedAckEvent, call,
2092 RX_CALL_REFCOUNT_DELAY);
2093 call->delayedAckEvent = NULL;
2094 rxi_SendDelayedAck(NULL, call, NULL);
2097 /* We need to release the call lock since it's lower than the
2098 * conn_call_lock and we don't want to hold the conn_call_lock
2099 * over the rx_ReadProc call. The conn_call_lock needs to be held
2100 * here for the case where rx_NewCall is perusing the calls on
2101 * the connection structure. We don't want to signal until
2102 * rx_NewCall is in a stable state. Otherwise, rx_NewCall may
2103 * have checked this call, found it active and by the time it
2104 * goes to sleep, will have missed the signal.
2106 MUTEX_EXIT(&call->lock);
2107 MUTEX_ENTER(&conn->conn_call_lock);
2108 MUTEX_ENTER(&call->lock);
2109 MUTEX_ENTER(&conn->conn_data_lock);
2110 conn->flags |= RX_CONN_BUSY;
2111 if (conn->flags & RX_CONN_MAKECALL_WAITING) {
2112 MUTEX_EXIT(&conn->conn_data_lock);
2113 #ifdef RX_ENABLE_LOCKS
2114 CV_BROADCAST(&conn->conn_call_cv);
2119 #ifdef RX_ENABLE_LOCKS
2121 MUTEX_EXIT(&conn->conn_data_lock);
2123 #endif /* RX_ENABLE_LOCKS */
2124 call->state = RX_STATE_DALLY;
2126 error = call->error;
2128 /* currentPacket, nLeft, and NFree must be zeroed here, because
2129 * ResetCall cannot: ResetCall may be called at splnet(), in the
2130 * kernel version, and may interrupt the macros rx_Read or
2131 * rx_Write, which run at normal priority for efficiency. */
2132 if (call->currentPacket) {
2133 #ifdef RX_TRACK_PACKETS
2134 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
2136 rxi_FreePacket(call->currentPacket);
2137 call->currentPacket = (struct rx_packet *)0;
2140 call->nLeft = call->nFree = call->curlen = 0;
2142 /* Free any packets from the last call to ReadvProc/WritevProc */
2143 #ifdef RXDEBUG_PACKET
2145 #endif /* RXDEBUG_PACKET */
2146 rxi_FreePackets(0, &call->iovq);
2148 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
2149 MUTEX_EXIT(&call->lock);
2150 if (conn->type == RX_CLIENT_CONNECTION) {
2151 MUTEX_ENTER(&conn->conn_data_lock);
2152 conn->flags &= ~RX_CONN_BUSY;
2153 MUTEX_EXIT(&conn->conn_data_lock);
2154 MUTEX_EXIT(&conn->conn_call_lock);
2158 * Map errors to the local host's errno.h format.
2160 error = ntoh_syserr_conv(error);
2164 #if !defined(KERNEL)
2166 /* Call this routine when shutting down a server or client (especially
2167 * clients). This will allow Rx to gracefully garbage collect server
2168 * connections, and reduce the number of retries that a server might
2169 * make to a dead client.
2170 * This is not quite right, since some calls may still be ongoing and
2171 * we can't lock them to destroy them. */
2175 struct rx_connection **conn_ptr, **conn_end;
2179 if (rxinit_status == 1) {
2181 return; /* Already shutdown. */
2183 rxi_DeleteCachedConnections();
2184 if (rx_connHashTable) {
2185 MUTEX_ENTER(&rx_connHashTable_lock);
2186 for (conn_ptr = &rx_connHashTable[0], conn_end =
2187 &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
2189 struct rx_connection *conn, *next;
2190 for (conn = *conn_ptr; conn; conn = next) {
2192 if (conn->type == RX_CLIENT_CONNECTION) {
2193 /* MUTEX_ENTER(&conn->conn_data_lock); when used in kernel */
2195 /* MUTEX_EXIT(&conn->conn_data_lock); when used in kernel */
2196 #ifdef RX_ENABLE_LOCKS
2197 rxi_DestroyConnectionNoLock(conn);
2198 #else /* RX_ENABLE_LOCKS */
2199 rxi_DestroyConnection(conn);
2200 #endif /* RX_ENABLE_LOCKS */
2204 #ifdef RX_ENABLE_LOCKS
2205 while (rx_connCleanup_list) {
2206 struct rx_connection *conn;
2207 conn = rx_connCleanup_list;
2208 rx_connCleanup_list = rx_connCleanup_list->next;
2209 MUTEX_EXIT(&rx_connHashTable_lock);
2210 rxi_CleanupConnection(conn);
2211 MUTEX_ENTER(&rx_connHashTable_lock);
2213 MUTEX_EXIT(&rx_connHashTable_lock);
2214 #endif /* RX_ENABLE_LOCKS */
2219 afs_winsockCleanup();
2227 /* if we wakeup packet waiter too often, can get in loop with two
2228 AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2230 rxi_PacketsUnWait(void)
2232 if (!rx_waitingForPackets) {
2236 if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2237 return; /* still over quota */
2240 rx_waitingForPackets = 0;
2241 #ifdef RX_ENABLE_LOCKS
2242 CV_BROADCAST(&rx_waitingForPackets_cv);
2244 osi_rxWakeup(&rx_waitingForPackets);
2250 /* ------------------Internal interfaces------------------------- */
2252 /* Return this process's service structure for the
2253 * specified socket and service */
2255 rxi_FindService(osi_socket socket, u_short serviceId)
2257 struct rx_service **sp;
2258 for (sp = &rx_services[0]; *sp; sp++) {
2259 if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2265 #ifdef RXDEBUG_PACKET
2266 #ifdef KDUMP_RX_LOCK
2267 static struct rx_call_rx_lock *rx_allCallsp = 0;
2269 static struct rx_call *rx_allCallsp = 0;
2271 #endif /* RXDEBUG_PACKET */
2273 /* Allocate a call structure, for the indicated channel of the
2274 * supplied connection. The mode and state of the call must be set by
2275 * the caller. Returns the call with mutex locked. */
2277 rxi_NewCall(struct rx_connection *conn, int channel)
2279 struct rx_call *call;
2280 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2281 struct rx_call *cp; /* Call pointer temp */
2282 struct rx_call *nxp; /* Next call pointer, for queue_Scan */
2283 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2285 dpf(("rxi_NewCall(conn %"AFS_PTR_FMT", channel %d)\n", conn, channel));
2287 /* Grab an existing call structure, or allocate a new one.
2288 * Existing call structures are assumed to have been left reset by
2290 MUTEX_ENTER(&rx_freeCallQueue_lock);
2292 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2294 * EXCEPT that the TQ might not yet be cleared out.
2295 * Skip over those with in-use TQs.
2298 for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2299 if (!(cp->flags & RX_CALL_TQ_BUSY)) {
2305 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2306 if (queue_IsNotEmpty(&rx_freeCallQueue)) {
2307 call = queue_First(&rx_freeCallQueue, rx_call);
2308 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2310 if (rx_stats_active)
2311 rx_atomic_dec(&rx_stats.nFreeCallStructs);
2312 MUTEX_EXIT(&rx_freeCallQueue_lock);
2313 MUTEX_ENTER(&call->lock);
2314 CLEAR_CALL_QUEUE_LOCK(call);
2315 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2316 /* Now, if TQ wasn't cleared earlier, do it now. */
2317 rxi_WaitforTQBusy(call);
2318 if (call->flags & RX_CALL_TQ_CLEARME) {
2319 rxi_ClearTransmitQueue(call, 1);
2320 /*queue_Init(&call->tq);*/
2322 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2323 /* Bind the call to its connection structure */
2325 rxi_ResetCall(call, 1);
2328 call = rxi_Alloc(sizeof(struct rx_call));
2329 #ifdef RXDEBUG_PACKET
2330 call->allNextp = rx_allCallsp;
2331 rx_allCallsp = call;
2333 #endif /* RXDEBUG_PACKET */
2334 if (rx_stats_active)
2335 rx_atomic_inc(&rx_stats.nCallStructs);
2337 MUTEX_EXIT(&rx_freeCallQueue_lock);
2338 MUTEX_INIT(&call->lock, "call lock", MUTEX_DEFAULT, NULL);
2339 MUTEX_ENTER(&call->lock);
2340 CV_INIT(&call->cv_twind, "call twind", CV_DEFAULT, 0);
2341 CV_INIT(&call->cv_rq, "call rq", CV_DEFAULT, 0);
2342 CV_INIT(&call->cv_tq, "call tq", CV_DEFAULT, 0);
2344 /* Initialize once-only items */
2345 queue_Init(&call->tq);
2346 queue_Init(&call->rq);
2347 queue_Init(&call->iovq);
2348 #ifdef RXDEBUG_PACKET
2349 call->rqc = call->tqc = call->iovqc = 0;
2350 #endif /* RXDEBUG_PACKET */
2351 /* Bind the call to its connection structure (prereq for reset) */
2353 rxi_ResetCall(call, 1);
2355 call->channel = channel;
2356 call->callNumber = &conn->callNumber[channel];
2357 call->rwind = conn->rwind[channel];
2358 call->twind = conn->twind[channel];
2359 /* Note that the next expected call number is retained (in
2360 * conn->callNumber[i]), even if we reallocate the call structure
2362 conn->call[channel] = call;
2363 /* if the channel's never been used (== 0), we should start at 1, otherwise
2364 * the call number is valid from the last time this channel was used */
2365 if (*call->callNumber == 0)
2366 *call->callNumber = 1;
2371 /* A call has been inactive long enough that so we can throw away
2372 * state, including the call structure, which is placed on the call
2374 * Call is locked upon entry.
2375 * haveCTLock set if called from rxi_ReapConnections
2377 #ifdef RX_ENABLE_LOCKS
2379 rxi_FreeCall(struct rx_call *call, int haveCTLock)
2380 #else /* RX_ENABLE_LOCKS */
2382 rxi_FreeCall(struct rx_call *call)
2383 #endif /* RX_ENABLE_LOCKS */
2385 int channel = call->channel;
2386 struct rx_connection *conn = call->conn;
2389 if (call->state == RX_STATE_DALLY || call->state == RX_STATE_HOLD)
2390 (*call->callNumber)++;
2391 rxi_ResetCall(call, 0);
2392 call->conn->call[channel] = (struct rx_call *)0;
2394 MUTEX_ENTER(&rx_freeCallQueue_lock);
2395 SET_CALL_QUEUE_LOCK(call, &rx_freeCallQueue_lock);
2396 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2397 /* A call may be free even though its transmit queue is still in use.
2398 * Since we search the call list from head to tail, put busy calls at
2399 * the head of the list, and idle calls at the tail.
2401 if (call->flags & RX_CALL_TQ_BUSY)
2402 queue_Prepend(&rx_freeCallQueue, call);
2404 queue_Append(&rx_freeCallQueue, call);
2405 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2406 queue_Append(&rx_freeCallQueue, call);
2407 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2408 if (rx_stats_active)
2409 rx_atomic_inc(&rx_stats.nFreeCallStructs);
2410 MUTEX_EXIT(&rx_freeCallQueue_lock);
2412 /* Destroy the connection if it was previously slated for
2413 * destruction, i.e. the Rx client code previously called
2414 * rx_DestroyConnection (client connections), or
2415 * rxi_ReapConnections called the same routine (server
2416 * connections). Only do this, however, if there are no
2417 * outstanding calls. Note that for fine grain locking, there appears
2418 * to be a deadlock in that rxi_FreeCall has a call locked and
2419 * DestroyConnectionNoLock locks each call in the conn. But note a
2420 * few lines up where we have removed this call from the conn.
2421 * If someone else destroys a connection, they either have no
2422 * call lock held or are going through this section of code.
2424 MUTEX_ENTER(&conn->conn_data_lock);
2425 if (conn->flags & RX_CONN_DESTROY_ME && !(conn->flags & RX_CONN_MAKECALL_WAITING)) {
2427 MUTEX_EXIT(&conn->conn_data_lock);
2428 #ifdef RX_ENABLE_LOCKS
2430 rxi_DestroyConnectionNoLock(conn);
2432 rxi_DestroyConnection(conn);
2433 #else /* RX_ENABLE_LOCKS */
2434 rxi_DestroyConnection(conn);
2435 #endif /* RX_ENABLE_LOCKS */
2437 MUTEX_EXIT(&conn->conn_data_lock);
2441 rx_atomic_t rxi_Allocsize = RX_ATOMIC_INIT(0);
2442 rx_atomic_t rxi_Alloccnt = RX_ATOMIC_INIT(0);
2445 rxi_Alloc(size_t size)
2449 if (rx_stats_active) {
2450 rx_atomic_add(&rxi_Allocsize, (int) size);
2451 rx_atomic_inc(&rxi_Alloccnt);
2455 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
2456 afs_osi_Alloc_NoSleep(size);
2461 osi_Panic("rxi_Alloc error");
2467 rxi_Free(void *addr, size_t size)
2469 if (rx_stats_active) {
2470 rx_atomic_sub(&rxi_Allocsize, (int) size);
2471 rx_atomic_dec(&rxi_Alloccnt);
2473 osi_Free(addr, size);
2477 rxi_SetPeerMtu(struct rx_peer *peer, afs_uint32 host, afs_uint32 port, int mtu)
2479 struct rx_peer **peer_ptr = NULL, **peer_end = NULL;
2480 struct rx_peer *next = NULL;
2484 MUTEX_ENTER(&rx_peerHashTable_lock);
2486 peer_ptr = &rx_peerHashTable[0];
2487 peer_end = &rx_peerHashTable[rx_hashTableSize];
2490 for ( ; peer_ptr < peer_end; peer_ptr++) {
2493 for ( ; peer; peer = next) {
2495 if (host == peer->host)
2500 hashIndex = PEER_HASH(host, port);
2501 for (peer = rx_peerHashTable[hashIndex]; peer; peer = peer->next) {
2502 if ((peer->host == host) && (peer->port == port))
2507 MUTEX_ENTER(&rx_peerHashTable_lock);
2512 MUTEX_EXIT(&rx_peerHashTable_lock);
2514 MUTEX_ENTER(&peer->peer_lock);
2515 /* We don't handle dropping below min, so don't */
2516 mtu = MAX(mtu, RX_MIN_PACKET_SIZE);
2517 peer->ifMTU=MIN(mtu, peer->ifMTU);
2518 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2519 /* if we tweaked this down, need to tune our peer MTU too */
2520 peer->MTU = MIN(peer->MTU, peer->natMTU);
2521 /* if we discovered a sub-1500 mtu, degrade */
2522 if (peer->ifMTU < OLD_MAX_PACKET_SIZE)
2523 peer->maxDgramPackets = 1;
2524 /* We no longer have valid peer packet information */
2525 if (peer->maxPacketSize-RX_IPUDP_SIZE > peer->ifMTU)
2526 peer->maxPacketSize = 0;
2527 MUTEX_EXIT(&peer->peer_lock);
2529 MUTEX_ENTER(&rx_peerHashTable_lock);
2531 if (host && !port) {
2533 /* pick up where we left off */
2537 MUTEX_EXIT(&rx_peerHashTable_lock);
2540 /* Find the peer process represented by the supplied (host,port)
2541 * combination. If there is no appropriate active peer structure, a
2542 * new one will be allocated and initialized
2543 * The origPeer, if set, is a pointer to a peer structure on which the
2544 * refcount will be be decremented. This is used to replace the peer
2545 * structure hanging off a connection structure */
2547 rxi_FindPeer(afs_uint32 host, u_short port,
2548 struct rx_peer *origPeer, int create)
2552 hashIndex = PEER_HASH(host, port);
2553 MUTEX_ENTER(&rx_peerHashTable_lock);
2554 for (pp = rx_peerHashTable[hashIndex]; pp; pp = pp->next) {
2555 if ((pp->host == host) && (pp->port == port))
2560 pp = rxi_AllocPeer(); /* This bzero's *pp */
2561 pp->host = host; /* set here or in InitPeerParams is zero */
2563 MUTEX_INIT(&pp->peer_lock, "peer_lock", MUTEX_DEFAULT, 0);
2564 queue_Init(&pp->congestionQueue);
2565 queue_Init(&pp->rpcStats);
2566 pp->next = rx_peerHashTable[hashIndex];
2567 rx_peerHashTable[hashIndex] = pp;
2568 rxi_InitPeerParams(pp);
2569 if (rx_stats_active)
2570 rx_atomic_inc(&rx_stats.nPeerStructs);
2577 origPeer->refCount--;
2578 MUTEX_EXIT(&rx_peerHashTable_lock);
2583 /* Find the connection at (host, port) started at epoch, and with the
2584 * given connection id. Creates the server connection if necessary.
2585 * The type specifies whether a client connection or a server
2586 * connection is desired. In both cases, (host, port) specify the
2587 * peer's (host, pair) pair. Client connections are not made
2588 * automatically by this routine. The parameter socket gives the
2589 * socket descriptor on which the packet was received. This is used,
2590 * in the case of server connections, to check that *new* connections
2591 * come via a valid (port, serviceId). Finally, the securityIndex
2592 * parameter must match the existing index for the connection. If a
2593 * server connection is created, it will be created using the supplied
2594 * index, if the index is valid for this service */
2595 struct rx_connection *
2596 rxi_FindConnection(osi_socket socket, afs_uint32 host,
2597 u_short port, u_short serviceId, afs_uint32 cid,
2598 afs_uint32 epoch, int type, u_int securityIndex)
2600 int hashindex, flag, i;
2601 struct rx_connection *conn;
2602 hashindex = CONN_HASH(host, port, cid, epoch, type);
2603 MUTEX_ENTER(&rx_connHashTable_lock);
2604 rxLastConn ? (conn = rxLastConn, flag = 0) : (conn =
2605 rx_connHashTable[hashindex],
2608 if ((conn->type == type) && ((cid & RX_CIDMASK) == conn->cid)
2609 && (epoch == conn->epoch)) {
2610 struct rx_peer *pp = conn->peer;
2611 if (securityIndex != conn->securityIndex) {
2612 /* this isn't supposed to happen, but someone could forge a packet
2613 * like this, and there seems to be some CM bug that makes this
2614 * happen from time to time -- in which case, the fileserver
2616 MUTEX_EXIT(&rx_connHashTable_lock);
2617 return (struct rx_connection *)0;
2619 if (pp->host == host && pp->port == port)
2621 if (type == RX_CLIENT_CONNECTION && pp->port == port)
2623 /* So what happens when it's a callback connection? */
2624 if ( /*type == RX_CLIENT_CONNECTION && */
2625 (conn->epoch & 0x80000000))
2629 /* the connection rxLastConn that was used the last time is not the
2630 ** one we are looking for now. Hence, start searching in the hash */
2632 conn = rx_connHashTable[hashindex];
2637 struct rx_service *service;
2638 if (type == RX_CLIENT_CONNECTION) {
2639 MUTEX_EXIT(&rx_connHashTable_lock);
2640 return (struct rx_connection *)0;
2642 service = rxi_FindService(socket, serviceId);
2643 if (!service || (securityIndex >= service->nSecurityObjects)
2644 || (service->securityObjects[securityIndex] == 0)) {
2645 MUTEX_EXIT(&rx_connHashTable_lock);
2646 return (struct rx_connection *)0;
2648 conn = rxi_AllocConnection(); /* This bzero's the connection */
2649 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
2650 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
2651 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
2652 conn->next = rx_connHashTable[hashindex];
2653 rx_connHashTable[hashindex] = conn;
2654 conn->peer = rxi_FindPeer(host, port, 0, 1);
2655 conn->type = RX_SERVER_CONNECTION;
2656 conn->lastSendTime = clock_Sec(); /* don't GC immediately */
2657 conn->epoch = epoch;
2658 conn->cid = cid & RX_CIDMASK;
2659 /* conn->serial = conn->lastSerial = 0; */
2660 /* conn->timeout = 0; */
2661 conn->ackRate = RX_FAST_ACK_RATE;
2662 conn->service = service;
2663 conn->serviceId = serviceId;
2664 conn->securityIndex = securityIndex;
2665 conn->securityObject = service->securityObjects[securityIndex];
2666 conn->nSpecific = 0;
2667 conn->specific = NULL;
2668 rx_SetConnDeadTime(conn, service->connDeadTime);
2669 rx_SetConnIdleDeadTime(conn, service->idleDeadTime);
2670 rx_SetServerConnIdleDeadErr(conn, service->idleDeadErr);
2671 for (i = 0; i < RX_MAXCALLS; i++) {
2672 conn->twind[i] = rx_initSendWindow;
2673 conn->rwind[i] = rx_initReceiveWindow;
2675 /* Notify security object of the new connection */
2676 RXS_NewConnection(conn->securityObject, conn);
2677 /* XXXX Connection timeout? */
2678 if (service->newConnProc)
2679 (*service->newConnProc) (conn);
2680 if (rx_stats_active)
2681 rx_atomic_inc(&rx_stats.nServerConns);
2684 MUTEX_ENTER(&conn->conn_data_lock);
2686 MUTEX_EXIT(&conn->conn_data_lock);
2688 rxLastConn = conn; /* store this connection as the last conn used */
2689 MUTEX_EXIT(&rx_connHashTable_lock);
2693 /* There are two packet tracing routines available for testing and monitoring
2694 * Rx. One is called just after every packet is received and the other is
2695 * called just before every packet is sent. Received packets, have had their
2696 * headers decoded, and packets to be sent have not yet had their headers
2697 * encoded. Both take two parameters: a pointer to the packet and a sockaddr
2698 * containing the network address. Both can be modified. The return value, if
2699 * non-zero, indicates that the packet should be dropped. */
2701 int (*rx_justReceived) (struct rx_packet *, struct sockaddr_in *) = 0;
2702 int (*rx_almostSent) (struct rx_packet *, struct sockaddr_in *) = 0;
2704 /* A packet has been received off the interface. Np is the packet, socket is
2705 * the socket number it was received from (useful in determining which service
2706 * this packet corresponds to), and (host, port) reflect the host,port of the
2707 * sender. This call returns the packet to the caller if it is finished with
2708 * it, rather than de-allocating it, just as a small performance hack */
2711 rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
2712 afs_uint32 host, u_short port, int *tnop,
2713 struct rx_call **newcallp)
2715 struct rx_call *call;
2716 struct rx_connection *conn;
2718 afs_uint32 currentCallNumber;
2724 struct rx_packet *tnp;
2727 /* We don't print out the packet until now because (1) the time may not be
2728 * accurate enough until now in the lwp implementation (rx_Listener only gets
2729 * the time after the packet is read) and (2) from a protocol point of view,
2730 * this is the first time the packet has been seen */
2731 packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
2732 ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
2733 dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %"AFS_PTR_FMT,
2734 np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
2735 np->header.epoch, np->header.cid, np->header.callNumber,
2736 np->header.seq, np->header.flags, np));
2739 if (np->header.type == RX_PACKET_TYPE_VERSION) {
2740 return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
2743 if (np->header.type == RX_PACKET_TYPE_DEBUG) {
2744 return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
2747 /* If an input tracer function is defined, call it with the packet and
2748 * network address. Note this function may modify its arguments. */
2749 if (rx_justReceived) {
2750 struct sockaddr_in addr;
2752 addr.sin_family = AF_INET;
2753 addr.sin_port = port;
2754 addr.sin_addr.s_addr = host;
2755 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
2756 addr.sin_len = sizeof(addr);
2757 #endif /* AFS_OSF_ENV */
2758 drop = (*rx_justReceived) (np, &addr);
2759 /* drop packet if return value is non-zero */
2762 port = addr.sin_port; /* in case fcn changed addr */
2763 host = addr.sin_addr.s_addr;
2767 /* If packet was not sent by the client, then *we* must be the client */
2768 type = ((np->header.flags & RX_CLIENT_INITIATED) != RX_CLIENT_INITIATED)
2769 ? RX_CLIENT_CONNECTION : RX_SERVER_CONNECTION;
2771 /* Find the connection (or fabricate one, if we're the server & if
2772 * necessary) associated with this packet */
2774 rxi_FindConnection(socket, host, port, np->header.serviceId,
2775 np->header.cid, np->header.epoch, type,
2776 np->header.securityIndex);
2779 /* If no connection found or fabricated, just ignore the packet.
2780 * (An argument could be made for sending an abort packet for
2785 MUTEX_ENTER(&conn->conn_data_lock);
2786 if (conn->maxSerial < np->header.serial)
2787 conn->maxSerial = np->header.serial;
2788 MUTEX_EXIT(&conn->conn_data_lock);
2790 /* If the connection is in an error state, send an abort packet and ignore
2791 * the incoming packet */
2793 /* Don't respond to an abort packet--we don't want loops! */
2794 MUTEX_ENTER(&conn->conn_data_lock);
2795 if (np->header.type != RX_PACKET_TYPE_ABORT)
2796 np = rxi_SendConnectionAbort(conn, np, 1, 0);
2798 MUTEX_EXIT(&conn->conn_data_lock);
2802 /* Check for connection-only requests (i.e. not call specific). */
2803 if (np->header.callNumber == 0) {
2804 switch (np->header.type) {
2805 case RX_PACKET_TYPE_ABORT: {
2806 /* What if the supplied error is zero? */
2807 afs_int32 errcode = ntohl(rx_GetInt32(np, 0));
2808 dpf(("rxi_ReceivePacket ABORT rx_GetInt32 = %d", errcode));
2809 rxi_ConnectionError(conn, errcode);
2810 MUTEX_ENTER(&conn->conn_data_lock);
2812 MUTEX_EXIT(&conn->conn_data_lock);
2815 case RX_PACKET_TYPE_CHALLENGE:
2816 tnp = rxi_ReceiveChallengePacket(conn, np, 1);
2817 MUTEX_ENTER(&conn->conn_data_lock);
2819 MUTEX_EXIT(&conn->conn_data_lock);
2821 case RX_PACKET_TYPE_RESPONSE:
2822 tnp = rxi_ReceiveResponsePacket(conn, np, 1);
2823 MUTEX_ENTER(&conn->conn_data_lock);
2825 MUTEX_EXIT(&conn->conn_data_lock);
2827 case RX_PACKET_TYPE_PARAMS:
2828 case RX_PACKET_TYPE_PARAMS + 1:
2829 case RX_PACKET_TYPE_PARAMS + 2:
2830 /* ignore these packet types for now */
2831 MUTEX_ENTER(&conn->conn_data_lock);
2833 MUTEX_EXIT(&conn->conn_data_lock);
2838 /* Should not reach here, unless the peer is broken: send an
2840 rxi_ConnectionError(conn, RX_PROTOCOL_ERROR);
2841 MUTEX_ENTER(&conn->conn_data_lock);
2842 tnp = rxi_SendConnectionAbort(conn, np, 1, 0);
2844 MUTEX_EXIT(&conn->conn_data_lock);
2849 channel = np->header.cid & RX_CHANNELMASK;
2850 call = conn->call[channel];
2851 #ifdef RX_ENABLE_LOCKS
2853 MUTEX_ENTER(&call->lock);
2854 /* Test to see if call struct is still attached to conn. */
2855 if (call != conn->call[channel]) {
2857 MUTEX_EXIT(&call->lock);
2858 if (type == RX_SERVER_CONNECTION) {
2859 call = conn->call[channel];
2860 /* If we started with no call attached and there is one now,
2861 * another thread is also running this routine and has gotten
2862 * the connection channel. We should drop this packet in the tests
2863 * below. If there was a call on this connection and it's now
2864 * gone, then we'll be making a new call below.
2865 * If there was previously a call and it's now different then
2866 * the old call was freed and another thread running this routine
2867 * has created a call on this channel. One of these two threads
2868 * has a packet for the old call and the code below handles those
2872 MUTEX_ENTER(&call->lock);
2874 /* This packet can't be for this call. If the new call address is
2875 * 0 then no call is running on this channel. If there is a call
2876 * then, since this is a client connection we're getting data for
2877 * it must be for the previous call.
2879 if (rx_stats_active)
2880 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
2881 MUTEX_ENTER(&conn->conn_data_lock);
2883 MUTEX_EXIT(&conn->conn_data_lock);
2888 currentCallNumber = conn->callNumber[channel];
2890 if (type == RX_SERVER_CONNECTION) { /* We're the server */
2891 if (np->header.callNumber < currentCallNumber) {
2892 if (rx_stats_active)
2893 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
2894 #ifdef RX_ENABLE_LOCKS
2896 MUTEX_EXIT(&call->lock);
2898 MUTEX_ENTER(&conn->conn_data_lock);
2900 MUTEX_EXIT(&conn->conn_data_lock);
2904 MUTEX_ENTER(&conn->conn_call_lock);
2905 call = rxi_NewCall(conn, channel);
2906 MUTEX_EXIT(&conn->conn_call_lock);
2907 *call->callNumber = np->header.callNumber;
2909 if (np->header.callNumber == 0)
2910 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%.06d len %d",
2911 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
2912 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
2913 np->header.flags, np, np->retryTime.sec, np->retryTime.usec / 1000, np->length));
2915 call->state = RX_STATE_PRECALL;
2916 clock_GetTime(&call->queueTime);
2917 hzero(call->bytesSent);
2918 hzero(call->bytesRcvd);
2920 * If the number of queued calls exceeds the overload
2921 * threshold then abort this call.
2923 if ((rx_BusyThreshold > 0) &&
2924 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
2925 struct rx_packet *tp;
2927 rxi_CallError(call, rx_BusyError);
2928 tp = rxi_SendCallAbort(call, np, 1, 0);
2929 MUTEX_EXIT(&call->lock);
2930 MUTEX_ENTER(&conn->conn_data_lock);
2932 MUTEX_EXIT(&conn->conn_data_lock);
2933 if (rx_stats_active)
2934 rx_atomic_inc(&rx_stats.nBusies);
2937 rxi_KeepAliveOn(call);
2938 } else if (np->header.callNumber != currentCallNumber) {
2939 /* Wait until the transmit queue is idle before deciding
2940 * whether to reset the current call. Chances are that the
2941 * call will be in ether DALLY or HOLD state once the TQ_BUSY
2944 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2945 while ((call->state == RX_STATE_ACTIVE)
2946 && (call->flags & RX_CALL_TQ_BUSY)) {
2947 call->flags |= RX_CALL_TQ_WAIT;
2949 #ifdef RX_ENABLE_LOCKS
2950 osirx_AssertMine(&call->lock, "rxi_Start lock3");
2951 CV_WAIT(&call->cv_tq, &call->lock);
2952 #else /* RX_ENABLE_LOCKS */
2953 osi_rxSleep(&call->tq);
2954 #endif /* RX_ENABLE_LOCKS */
2956 if (call->tqWaiters == 0)
2957 call->flags &= ~RX_CALL_TQ_WAIT;
2959 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2960 /* If the new call cannot be taken right now send a busy and set
2961 * the error condition in this call, so that it terminates as
2962 * quickly as possible */
2963 if (call->state == RX_STATE_ACTIVE) {
2964 struct rx_packet *tp;
2966 rxi_CallError(call, RX_CALL_DEAD);
2967 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
2969 MUTEX_EXIT(&call->lock);
2970 MUTEX_ENTER(&conn->conn_data_lock);
2972 MUTEX_EXIT(&conn->conn_data_lock);
2975 rxi_ResetCall(call, 0);
2976 *call->callNumber = np->header.callNumber;
2978 if (np->header.callNumber == 0)
2979 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%06d len %d",
2980 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
2981 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
2982 np->header.flags, np, np->retryTime.sec, np->retryTime.usec, np->length));
2984 call->state = RX_STATE_PRECALL;
2985 clock_GetTime(&call->queueTime);
2986 hzero(call->bytesSent);
2987 hzero(call->bytesRcvd);
2989 * If the number of queued calls exceeds the overload
2990 * threshold then abort this call.
2992 if ((rx_BusyThreshold > 0) &&
2993 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
2994 struct rx_packet *tp;
2996 rxi_CallError(call, rx_BusyError);
2997 tp = rxi_SendCallAbort(call, np, 1, 0);
2998 MUTEX_EXIT(&call->lock);
2999 MUTEX_ENTER(&conn->conn_data_lock);
3001 MUTEX_EXIT(&conn->conn_data_lock);
3002 if (rx_stats_active)
3003 rx_atomic_inc(&rx_stats.nBusies);
3006 rxi_KeepAliveOn(call);
3008 /* Continuing call; do nothing here. */
3010 } else { /* we're the client */
3011 /* Ignore all incoming acknowledgements for calls in DALLY state */
3012 if (call && (call->state == RX_STATE_DALLY)
3013 && (np->header.type == RX_PACKET_TYPE_ACK)) {
3014 if (rx_stats_active)
3015 rx_atomic_inc(&rx_stats.ignorePacketDally);
3016 #ifdef RX_ENABLE_LOCKS
3018 MUTEX_EXIT(&call->lock);
3021 MUTEX_ENTER(&conn->conn_data_lock);
3023 MUTEX_EXIT(&conn->conn_data_lock);
3027 /* Ignore anything that's not relevant to the current call. If there
3028 * isn't a current call, then no packet is relevant. */
3029 if (!call || (np->header.callNumber != currentCallNumber)) {
3030 if (rx_stats_active)
3031 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3032 #ifdef RX_ENABLE_LOCKS
3034 MUTEX_EXIT(&call->lock);
3037 MUTEX_ENTER(&conn->conn_data_lock);
3039 MUTEX_EXIT(&conn->conn_data_lock);
3042 /* If the service security object index stamped in the packet does not
3043 * match the connection's security index, ignore the packet */
3044 if (np->header.securityIndex != conn->securityIndex) {
3045 #ifdef RX_ENABLE_LOCKS
3046 MUTEX_EXIT(&call->lock);
3048 MUTEX_ENTER(&conn->conn_data_lock);
3050 MUTEX_EXIT(&conn->conn_data_lock);
3054 /* If we're receiving the response, then all transmit packets are
3055 * implicitly acknowledged. Get rid of them. */
3056 if (np->header.type == RX_PACKET_TYPE_DATA) {
3057 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3058 /* XXX Hack. Because we must release the global rx lock when
3059 * sending packets (osi_NetSend) we drop all acks while we're
3060 * traversing the tq in rxi_Start sending packets out because
3061 * packets may move to the freePacketQueue as result of being here!
3062 * So we drop these packets until we're safely out of the
3063 * traversing. Really ugly!
3064 * For fine grain RX locking, we set the acked field in the
3065 * packets and let rxi_Start remove them from the transmit queue.
3067 if (call->flags & RX_CALL_TQ_BUSY) {
3068 #ifdef RX_ENABLE_LOCKS
3069 rxi_SetAcksInTransmitQueue(call);
3072 return np; /* xmitting; drop packet */
3075 rxi_ClearTransmitQueue(call, 0);
3077 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
3078 rxi_ClearTransmitQueue(call, 0);
3079 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3081 if (np->header.type == RX_PACKET_TYPE_ACK) {
3082 /* now check to see if this is an ack packet acknowledging that the
3083 * server actually *lost* some hard-acked data. If this happens we
3084 * ignore this packet, as it may indicate that the server restarted in
3085 * the middle of a call. It is also possible that this is an old ack
3086 * packet. We don't abort the connection in this case, because this
3087 * *might* just be an old ack packet. The right way to detect a server
3088 * restart in the midst of a call is to notice that the server epoch
3090 /* XXX I'm not sure this is exactly right, since tfirst **IS**
3091 * XXX unacknowledged. I think that this is off-by-one, but
3092 * XXX I don't dare change it just yet, since it will
3093 * XXX interact badly with the server-restart detection
3094 * XXX code in receiveackpacket. */
3095 if (ntohl(rx_GetInt32(np, FIRSTACKOFFSET)) < call->tfirst) {
3096 if (rx_stats_active)
3097 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3098 MUTEX_EXIT(&call->lock);
3099 MUTEX_ENTER(&conn->conn_data_lock);
3101 MUTEX_EXIT(&conn->conn_data_lock);
3105 } /* else not a data packet */
3108 osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
3109 /* Set remote user defined status from packet */
3110 call->remoteStatus = np->header.userStatus;
3112 /* Note the gap between the expected next packet and the actual
3113 * packet that arrived, when the new packet has a smaller serial number
3114 * than expected. Rioses frequently reorder packets all by themselves,
3115 * so this will be quite important with very large window sizes.
3116 * Skew is checked against 0 here to avoid any dependence on the type of
3117 * inPacketSkew (which may be unsigned). In C, -1 > (unsigned) 0 is always
3119 * The inPacketSkew should be a smoothed running value, not just a maximum. MTUXXX
3120 * see CalculateRoundTripTime for an example of how to keep smoothed values.
3121 * I think using a beta of 1/8 is probably appropriate. 93.04.21
3123 MUTEX_ENTER(&conn->conn_data_lock);
3124 skew = conn->lastSerial - np->header.serial;
3125 conn->lastSerial = np->header.serial;
3126 MUTEX_EXIT(&conn->conn_data_lock);
3128 struct rx_peer *peer;
3130 if (skew > peer->inPacketSkew) {
3131 dpf(("*** In skew changed from %d to %d\n",
3132 peer->inPacketSkew, skew));
3133 peer->inPacketSkew = skew;
3137 /* Now do packet type-specific processing */
3138 switch (np->header.type) {
3139 case RX_PACKET_TYPE_DATA:
3140 np = rxi_ReceiveDataPacket(call, np, 1, socket, host, port, tnop,
3143 case RX_PACKET_TYPE_ACK:
3144 /* Respond immediately to ack packets requesting acknowledgement
3146 if (np->header.flags & RX_REQUEST_ACK) {
3148 (void)rxi_SendCallAbort(call, 0, 1, 0);
3150 (void)rxi_SendAck(call, 0, np->header.serial,
3151 RX_ACK_PING_RESPONSE, 1);
3153 np = rxi_ReceiveAckPacket(call, np, 1);
3155 case RX_PACKET_TYPE_ABORT: {
3156 /* An abort packet: reset the call, passing the error up to the user. */
3157 /* What if error is zero? */
3158 /* What if the error is -1? the application will treat it as a timeout. */
3159 afs_int32 errdata = ntohl(*(afs_int32 *) rx_DataOf(np));
3160 dpf(("rxi_ReceivePacket ABORT rx_DataOf = %d", errdata));
3161 rxi_CallError(call, errdata);
3162 MUTEX_EXIT(&call->lock);
3163 MUTEX_ENTER(&conn->conn_data_lock);
3165 MUTEX_EXIT(&conn->conn_data_lock);
3166 return np; /* xmitting; drop packet */
3168 case RX_PACKET_TYPE_BUSY:
3171 case RX_PACKET_TYPE_ACKALL:
3172 /* All packets acknowledged, so we can drop all packets previously
3173 * readied for sending */
3174 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3175 /* XXX Hack. We because we can't release the global rx lock when
3176 * sending packets (osi_NetSend) we drop all ack pkts while we're
3177 * traversing the tq in rxi_Start sending packets out because
3178 * packets may move to the freePacketQueue as result of being
3179 * here! So we drop these packets until we're safely out of the
3180 * traversing. Really ugly!
3181 * For fine grain RX locking, we set the acked field in the packets
3182 * and let rxi_Start remove the packets from the transmit queue.
3184 if (call->flags & RX_CALL_TQ_BUSY) {
3185 #ifdef RX_ENABLE_LOCKS
3186 rxi_SetAcksInTransmitQueue(call);
3188 #else /* RX_ENABLE_LOCKS */
3189 MUTEX_EXIT(&call->lock);
3190 MUTEX_ENTER(&conn->conn_data_lock);
3192 MUTEX_EXIT(&conn->conn_data_lock);
3193 return np; /* xmitting; drop packet */
3194 #endif /* RX_ENABLE_LOCKS */
3196 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3197 rxi_ClearTransmitQueue(call, 0);
3198 rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
3201 /* Should not reach here, unless the peer is broken: send an abort
3203 rxi_CallError(call, RX_PROTOCOL_ERROR);
3204 np = rxi_SendCallAbort(call, np, 1, 0);
3207 /* Note when this last legitimate packet was received, for keep-alive
3208 * processing. Note, we delay getting the time until now in the hope that
3209 * the packet will be delivered to the user before any get time is required
3210 * (if not, then the time won't actually be re-evaluated here). */
3211 call->lastReceiveTime = clock_Sec();
3212 MUTEX_EXIT(&call->lock);
3213 MUTEX_ENTER(&conn->conn_data_lock);
3215 MUTEX_EXIT(&conn->conn_data_lock);
3219 /* return true if this is an "interesting" connection from the point of view
3220 of someone trying to debug the system */
3222 rxi_IsConnInteresting(struct rx_connection *aconn)
3225 struct rx_call *tcall;
3227 if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
3230 for (i = 0; i < RX_MAXCALLS; i++) {
3231 tcall = aconn->call[i];
3233 if ((tcall->state == RX_STATE_PRECALL)
3234 || (tcall->state == RX_STATE_ACTIVE))
3236 if ((tcall->mode == RX_MODE_SENDING)
3237 || (tcall->mode == RX_MODE_RECEIVING))
3245 /* if this is one of the last few packets AND it wouldn't be used by the
3246 receiving call to immediately satisfy a read request, then drop it on
3247 the floor, since accepting it might prevent a lock-holding thread from
3248 making progress in its reading. If a call has been cleared while in
3249 the precall state then ignore all subsequent packets until the call
3250 is assigned to a thread. */
3253 TooLow(struct rx_packet *ap, struct rx_call *acall)
3257 MUTEX_ENTER(&rx_quota_mutex);
3258 if (((ap->header.seq != 1) && (acall->flags & RX_CALL_CLEARED)
3259 && (acall->state == RX_STATE_PRECALL))
3260 || ((rx_nFreePackets < rxi_dataQuota + 2)
3261 && !((ap->header.seq < acall->rnext + rx_initSendWindow)
3262 && (acall->flags & RX_CALL_READER_WAIT)))) {
3265 MUTEX_EXIT(&rx_quota_mutex);
3271 rxi_CheckReachEvent(struct rxevent *event, void *arg1, void *arg2)
3273 struct rx_connection *conn = arg1;
3274 struct rx_call *acall = arg2;
3275 struct rx_call *call = acall;
3276 struct clock when, now;
3279 MUTEX_ENTER(&conn->conn_data_lock);
3280 conn->checkReachEvent = NULL;
3281 waiting = conn->flags & RX_CONN_ATTACHWAIT;
3284 MUTEX_EXIT(&conn->conn_data_lock);
3288 MUTEX_ENTER(&conn->conn_call_lock);
3289 MUTEX_ENTER(&conn->conn_data_lock);
3290 for (i = 0; i < RX_MAXCALLS; i++) {
3291 struct rx_call *tc = conn->call[i];
3292 if (tc && tc->state == RX_STATE_PRECALL) {
3298 /* Indicate that rxi_CheckReachEvent is no longer running by
3299 * clearing the flag. Must be atomic under conn_data_lock to
3300 * avoid a new call slipping by: rxi_CheckConnReach holds
3301 * conn_data_lock while checking RX_CONN_ATTACHWAIT.
3303 conn->flags &= ~RX_CONN_ATTACHWAIT;
3304 MUTEX_EXIT(&conn->conn_data_lock);
3305 MUTEX_EXIT(&conn->conn_call_lock);
3310 MUTEX_ENTER(&call->lock);
3311 rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3313 MUTEX_EXIT(&call->lock);
3315 clock_GetTime(&now);
3317 when.sec += RX_CHECKREACH_TIMEOUT;
3318 MUTEX_ENTER(&conn->conn_data_lock);
3319 if (!conn->checkReachEvent) {
3321 conn->checkReachEvent =
3322 rxevent_PostNow(&when, &now, rxi_CheckReachEvent, conn,
3325 MUTEX_EXIT(&conn->conn_data_lock);
3331 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3333 struct rx_service *service = conn->service;
3334 struct rx_peer *peer = conn->peer;
3335 afs_uint32 now, lastReach;
3337 if (service->checkReach == 0)
3341 MUTEX_ENTER(&peer->peer_lock);
3342 lastReach = peer->lastReachTime;
3343 MUTEX_EXIT(&peer->peer_lock);
3344 if (now - lastReach < RX_CHECKREACH_TTL)
3347 MUTEX_ENTER(&conn->conn_data_lock);
3348 if (conn->flags & RX_CONN_ATTACHWAIT) {
3349 MUTEX_EXIT(&conn->conn_data_lock);
3352 conn->flags |= RX_CONN_ATTACHWAIT;
3353 MUTEX_EXIT(&conn->conn_data_lock);
3354 if (!conn->checkReachEvent)
3355 rxi_CheckReachEvent(NULL, conn, call);
3360 /* try to attach call, if authentication is complete */
3362 TryAttach(struct rx_call *acall, osi_socket socket,
3363 int *tnop, struct rx_call **newcallp,
3366 struct rx_connection *conn = acall->conn;
3368 if (conn->type == RX_SERVER_CONNECTION
3369 && acall->state == RX_STATE_PRECALL) {
3370 /* Don't attach until we have any req'd. authentication. */
3371 if (RXS_CheckAuthentication(conn->securityObject, conn) == 0) {
3372 if (reachOverride || rxi_CheckConnReach(conn, acall) == 0)
3373 rxi_AttachServerProc(acall, socket, tnop, newcallp);
3374 /* Note: this does not necessarily succeed; there
3375 * may not any proc available
3378 rxi_ChallengeOn(acall->conn);
3383 /* A data packet has been received off the interface. This packet is
3384 * appropriate to the call (the call is in the right state, etc.). This
3385 * routine can return a packet to the caller, for re-use */
3388 rxi_ReceiveDataPacket(struct rx_call *call,
3389 struct rx_packet *np, int istack,
3390 osi_socket socket, afs_uint32 host, u_short port,
3391 int *tnop, struct rx_call **newcallp)
3393 int ackNeeded = 0; /* 0 means no, otherwise ack_reason */
3398 afs_uint32 serial=0, flags=0;
3400 struct rx_packet *tnp;
3401 struct clock when, now;
3402 if (rx_stats_active)
3403 rx_atomic_inc(&rx_stats.dataPacketsRead);
3406 /* If there are no packet buffers, drop this new packet, unless we can find
3407 * packet buffers from inactive calls */
3409 && (rxi_OverQuota(RX_PACKET_CLASS_RECEIVE) || TooLow(np, call))) {
3410 MUTEX_ENTER(&rx_freePktQ_lock);
3411 rxi_NeedMorePackets = TRUE;
3412 MUTEX_EXIT(&rx_freePktQ_lock);
3413 if (rx_stats_active)
3414 rx_atomic_inc(&rx_stats.noPacketBuffersOnRead);
3415 call->rprev = np->header.serial;
3416 rxi_calltrace(RX_TRACE_DROP, call);
3417 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - quota problems", np));
3419 rxi_ClearReceiveQueue(call);
3420 clock_GetTime(&now);
3422 clock_Add(&when, &rx_softAckDelay);
3423 if (!call->delayedAckEvent
3424 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3425 rxevent_Cancel(call->delayedAckEvent, call,
3426 RX_CALL_REFCOUNT_DELAY);
3427 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3428 call->delayedAckEvent =
3429 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3431 /* we've damaged this call already, might as well do it in. */
3437 * New in AFS 3.5, if the RX_JUMBO_PACKET flag is set then this
3438 * packet is one of several packets transmitted as a single
3439 * datagram. Do not send any soft or hard acks until all packets
3440 * in a jumbogram have been processed. Send negative acks right away.
3442 for (isFirst = 1, tnp = NULL; isFirst || tnp; isFirst = 0) {
3443 /* tnp is non-null when there are more packets in the
3444 * current jumbo gram */
3451 seq = np->header.seq;
3452 serial = np->header.serial;
3453 flags = np->header.flags;
3455 /* If the call is in an error state, send an abort message */
3457 return rxi_SendCallAbort(call, np, istack, 0);
3459 /* The RX_JUMBO_PACKET is set in all but the last packet in each
3460 * AFS 3.5 jumbogram. */
3461 if (flags & RX_JUMBO_PACKET) {
3462 tnp = rxi_SplitJumboPacket(np, host, port, isFirst);
3467 if (np->header.spare != 0) {
3468 MUTEX_ENTER(&call->conn->conn_data_lock);
3469 call->conn->flags |= RX_CONN_USING_PACKET_CKSUM;
3470 MUTEX_EXIT(&call->conn->conn_data_lock);
3473 /* The usual case is that this is the expected next packet */
3474 if (seq == call->rnext) {
3476 /* Check to make sure it is not a duplicate of one already queued */
3477 if (queue_IsNotEmpty(&call->rq)
3478 && queue_First(&call->rq, rx_packet)->header.seq == seq) {
3479 if (rx_stats_active)
3480 rx_atomic_inc(&rx_stats.dupPacketsRead);
3481 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - duplicate", np));
3482 rxevent_Cancel(call->delayedAckEvent, call,
3483 RX_CALL_REFCOUNT_DELAY);
3484 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3490 /* It's the next packet. Stick it on the receive queue
3491 * for this call. Set newPackets to make sure we wake
3492 * the reader once all packets have been processed */
3493 #ifdef RX_TRACK_PACKETS
3494 np->flags |= RX_PKTFLAG_RQ;
3496 queue_Prepend(&call->rq, np);
3497 #ifdef RXDEBUG_PACKET
3499 #endif /* RXDEBUG_PACKET */
3501 np = NULL; /* We can't use this anymore */
3504 /* If an ack is requested then set a flag to make sure we
3505 * send an acknowledgement for this packet */
3506 if (flags & RX_REQUEST_ACK) {
3507 ackNeeded = RX_ACK_REQUESTED;
3510 /* Keep track of whether we have received the last packet */
3511 if (flags & RX_LAST_PACKET) {
3512 call->flags |= RX_CALL_HAVE_LAST;
3516 /* Check whether we have all of the packets for this call */
3517 if (call->flags & RX_CALL_HAVE_LAST) {
3518 afs_uint32 tseq; /* temporary sequence number */
3519 struct rx_packet *tp; /* Temporary packet pointer */
3520 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3522 for (tseq = seq, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3523 if (tseq != tp->header.seq)
3525 if (tp->header.flags & RX_LAST_PACKET) {
3526 call->flags |= RX_CALL_RECEIVE_DONE;
3533 /* Provide asynchronous notification for those who want it
3534 * (e.g. multi rx) */
3535 if (call->arrivalProc) {
3536 (*call->arrivalProc) (call, call->arrivalProcHandle,
3537 call->arrivalProcArg);
3538 call->arrivalProc = (void (*)())0;
3541 /* Update last packet received */
3544 /* If there is no server process serving this call, grab
3545 * one, if available. We only need to do this once. If a
3546 * server thread is available, this thread becomes a server
3547 * thread and the server thread becomes a listener thread. */
3549 TryAttach(call, socket, tnop, newcallp, 0);
3552 /* This is not the expected next packet. */
3554 /* Determine whether this is a new or old packet, and if it's
3555 * a new one, whether it fits into the current receive window.
3556 * Also figure out whether the packet was delivered in sequence.
3557 * We use the prev variable to determine whether the new packet
3558 * is the successor of its immediate predecessor in the
3559 * receive queue, and the missing flag to determine whether
3560 * any of this packets predecessors are missing. */
3562 afs_uint32 prev; /* "Previous packet" sequence number */
3563 struct rx_packet *tp; /* Temporary packet pointer */
3564 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3565 int missing; /* Are any predecessors missing? */
3567 /* If the new packet's sequence number has been sent to the
3568 * application already, then this is a duplicate */
3569 if (seq < call->rnext) {
3570 if (rx_stats_active)
3571 rx_atomic_inc(&rx_stats.dupPacketsRead);
3572 rxevent_Cancel(call->delayedAckEvent, call,
3573 RX_CALL_REFCOUNT_DELAY);
3574 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3580 /* If the sequence number is greater than what can be
3581 * accomodated by the current window, then send a negative
3582 * acknowledge and drop the packet */
3583 if ((call->rnext + call->rwind) <= seq) {
3584 rxevent_Cancel(call->delayedAckEvent, call,
3585 RX_CALL_REFCOUNT_DELAY);
3586 np = rxi_SendAck(call, np, serial, RX_ACK_EXCEEDS_WINDOW,
3593 /* Look for the packet in the queue of old received packets */
3594 for (prev = call->rnext - 1, missing =
3595 0, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3596 /*Check for duplicate packet */
3597 if (seq == tp->header.seq) {
3598 if (rx_stats_active)
3599 rx_atomic_inc(&rx_stats.dupPacketsRead);
3600 rxevent_Cancel(call->delayedAckEvent, call,
3601 RX_CALL_REFCOUNT_DELAY);
3602 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE,
3608 /* If we find a higher sequence packet, break out and
3609 * insert the new packet here. */
3610 if (seq < tp->header.seq)
3612 /* Check for missing packet */
3613 if (tp->header.seq != prev + 1) {
3617 prev = tp->header.seq;
3620 /* Keep track of whether we have received the last packet. */
3621 if (flags & RX_LAST_PACKET) {
3622 call->flags |= RX_CALL_HAVE_LAST;
3625 /* It's within the window: add it to the the receive queue.
3626 * tp is left by the previous loop either pointing at the
3627 * packet before which to insert the new packet, or at the
3628 * queue head if the queue is empty or the packet should be
3630 #ifdef RX_TRACK_PACKETS
3631 np->flags |= RX_PKTFLAG_RQ;
3633 #ifdef RXDEBUG_PACKET
3635 #endif /* RXDEBUG_PACKET */
3636 queue_InsertBefore(tp, np);
3640 /* Check whether we have all of the packets for this call */
3641 if ((call->flags & RX_CALL_HAVE_LAST)
3642 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
3643 afs_uint32 tseq; /* temporary sequence number */
3646 call->rnext, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3647 if (tseq != tp->header.seq)
3649 if (tp->header.flags & RX_LAST_PACKET) {
3650 call->flags |= RX_CALL_RECEIVE_DONE;
3657 /* We need to send an ack of the packet is out of sequence,
3658 * or if an ack was requested by the peer. */
3659 if (seq != prev + 1 || missing) {
3660 ackNeeded = RX_ACK_OUT_OF_SEQUENCE;
3661 } else if (flags & RX_REQUEST_ACK) {
3662 ackNeeded = RX_ACK_REQUESTED;
3665 /* Acknowledge the last packet for each call */
3666 if (flags & RX_LAST_PACKET) {
3677 * If the receiver is waiting for an iovec, fill the iovec
3678 * using the data from the receive queue */
3679 if (call->flags & RX_CALL_IOVEC_WAIT) {
3680 didHardAck = rxi_FillReadVec(call, serial);
3681 /* the call may have been aborted */
3690 /* Wakeup the reader if any */