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 #define AFSOP_STOP_RXCALLBACK 210 /* Stop CALLBACK process */
74 #define AFSOP_STOP_AFS 211 /* Stop AFS process */
75 #define AFSOP_STOP_BKG 212 /* Stop BKG process */
77 extern afs_int32 afs_termState;
79 #include "sys/lockl.h"
80 #include "sys/lock_def.h"
81 #endif /* AFS_AIX41_ENV */
82 # include "afs/rxgen_consts.h"
84 # include <sys/types.h>
94 # include <afs/afsutil.h>
95 # include <WINNT\afsreg.h>
97 # include <sys/socket.h>
98 # include <sys/file.h>
100 # include <sys/stat.h>
101 # include <netinet/in.h>
102 # include <sys/time.h>
105 # include "rx_user.h"
106 # include "rx_clock.h"
107 # include "rx_queue.h"
108 # include "rx_atomic.h"
109 # include "rx_globals.h"
110 # include "rx_trace.h"
111 # include <afs/rxgen_consts.h>
115 #ifdef AFS_PTHREAD_ENV
117 int (*registerProgram) (pid_t, char *) = 0;
118 int (*swapNameProgram) (pid_t, const char *, char *) = 0;
121 int (*registerProgram) (PROCESS, char *) = 0;
122 int (*swapNameProgram) (PROCESS, const char *, char *) = 0;
126 /* Local static routines */
127 static void rxi_DestroyConnectionNoLock(struct rx_connection *conn);
128 #ifdef RX_ENABLE_LOCKS
129 static void rxi_SetAcksInTransmitQueue(struct rx_call *call);
132 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
134 afs_int32 rxi_start_aborted; /* rxi_start awoke after rxi_Send in error. */
135 afs_int32 rxi_start_in_error;
137 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
140 * rxi_rpc_peer_stat_cnt counts the total number of peer stat structures
141 * currently allocated within rx. This number is used to allocate the
142 * memory required to return the statistics when queried.
143 * Protected by the rx_rpc_stats mutex.
146 static unsigned int rxi_rpc_peer_stat_cnt;
149 * rxi_rpc_process_stat_cnt counts the total number of local process stat
150 * structures currently allocated within rx. The number is used to allocate
151 * the memory required to return the statistics when queried.
152 * Protected by the rx_rpc_stats mutex.
155 static unsigned int rxi_rpc_process_stat_cnt;
157 #if !defined(offsetof)
158 #include <stddef.h> /* for definition of offsetof() */
161 #ifdef RX_ENABLE_LOCKS
162 afs_kmutex_t rx_atomic_mutex;
165 #ifdef AFS_PTHREAD_ENV
169 * Use procedural initialization of mutexes/condition variables
173 extern afs_kmutex_t rx_stats_mutex;
174 extern afs_kmutex_t rx_waiting_mutex;
175 extern afs_kmutex_t rx_quota_mutex;
176 extern afs_kmutex_t rx_pthread_mutex;
177 extern afs_kmutex_t rx_packets_mutex;
178 extern afs_kmutex_t des_init_mutex;
179 extern afs_kmutex_t des_random_mutex;
180 extern afs_kmutex_t rx_clock_mutex;
181 extern afs_kmutex_t rxi_connCacheMutex;
182 extern afs_kmutex_t rx_event_mutex;
183 extern afs_kmutex_t osi_malloc_mutex;
184 extern afs_kmutex_t event_handler_mutex;
185 extern afs_kmutex_t listener_mutex;
186 extern afs_kmutex_t rx_if_init_mutex;
187 extern afs_kmutex_t rx_if_mutex;
188 extern afs_kmutex_t rxkad_client_uid_mutex;
189 extern afs_kmutex_t rxkad_random_mutex;
191 extern afs_kcondvar_t rx_event_handler_cond;
192 extern afs_kcondvar_t rx_listener_cond;
194 static afs_kmutex_t epoch_mutex;
195 static afs_kmutex_t rx_init_mutex;
196 static afs_kmutex_t rx_debug_mutex;
197 static afs_kmutex_t rx_rpc_stats;
200 rxi_InitPthread(void)
202 MUTEX_INIT(&rx_clock_mutex, "clock", MUTEX_DEFAULT, 0);
203 MUTEX_INIT(&rx_stats_mutex, "stats", MUTEX_DEFAULT, 0);
204 MUTEX_INIT(&rx_waiting_mutex, "waiting", MUTEX_DEFAULT, 0);
205 MUTEX_INIT(&rx_atomic_mutex, "atomic", MUTEX_DEFAULT, 0);
206 MUTEX_INIT(&rx_quota_mutex, "quota", MUTEX_DEFAULT, 0);
207 MUTEX_INIT(&rx_pthread_mutex, "pthread", MUTEX_DEFAULT, 0);
208 MUTEX_INIT(&rx_packets_mutex, "packets", MUTEX_DEFAULT, 0);
209 MUTEX_INIT(&epoch_mutex, "epoch", MUTEX_DEFAULT, 0);
210 MUTEX_INIT(&rx_init_mutex, "init", MUTEX_DEFAULT, 0);
211 MUTEX_INIT(&rx_event_mutex, "event", MUTEX_DEFAULT, 0);
212 MUTEX_INIT(&des_init_mutex, "des", MUTEX_DEFAULT, 0);
213 MUTEX_INIT(&des_random_mutex, "random", MUTEX_DEFAULT, 0);
214 MUTEX_INIT(&osi_malloc_mutex, "malloc", MUTEX_DEFAULT, 0);
215 MUTEX_INIT(&event_handler_mutex, "event handler", MUTEX_DEFAULT, 0);
216 MUTEX_INIT(&rxi_connCacheMutex, "conn cache", MUTEX_DEFAULT, 0);
217 MUTEX_INIT(&listener_mutex, "listener", MUTEX_DEFAULT, 0);
218 MUTEX_INIT(&rx_if_init_mutex, "if init", MUTEX_DEFAULT, 0);
219 MUTEX_INIT(&rx_if_mutex, "if", MUTEX_DEFAULT, 0);
220 MUTEX_INIT(&rxkad_client_uid_mutex, "uid", MUTEX_DEFAULT, 0);
221 MUTEX_INIT(&rxkad_random_mutex, "rxkad random", MUTEX_DEFAULT, 0);
222 MUTEX_INIT(&rx_debug_mutex, "debug", MUTEX_DEFAULT, 0);
224 assert(pthread_cond_init
225 (&rx_event_handler_cond, (const pthread_condattr_t *)0) == 0);
226 assert(pthread_cond_init(&rx_listener_cond, (const pthread_condattr_t *)0)
228 assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
229 assert(pthread_key_create(&rx_ts_info_key, NULL) == 0);
231 rxkad_global_stats_init();
233 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
234 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
235 #ifdef RX_ENABLE_LOCKS
238 #endif /* RX_LOCKS_DB */
239 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
240 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
242 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
244 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
246 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
248 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
249 MUTEX_INIT(&rxi_keyCreate_lock, "rxi_keyCreate_lock", MUTEX_DEFAULT, 0);
250 #endif /* RX_ENABLE_LOCKS */
253 pthread_once_t rx_once_init = PTHREAD_ONCE_INIT;
254 #define INIT_PTHREAD_LOCKS \
255 assert(pthread_once(&rx_once_init, rxi_InitPthread)==0)
257 * The rx_stats_mutex mutex protects the following global variables:
258 * rxi_lowConnRefCount
259 * rxi_lowPeerRefCount
268 * The rx_quota_mutex mutex protects the following global variables:
276 * The rx_freePktQ_lock protects the following global variables:
281 * The rx_packets_mutex mutex protects the following global variables:
289 * The rx_pthread_mutex mutex protects the following global variables:
293 #define INIT_PTHREAD_LOCKS
297 /* Variables for handling the minProcs implementation. availProcs gives the
298 * number of threads available in the pool at this moment (not counting dudes
299 * executing right now). totalMin gives the total number of procs required
300 * for handling all minProcs requests. minDeficit is a dynamic variable
301 * tracking the # of procs required to satisfy all of the remaining minProcs
303 * For fine grain locking to work, the quota check and the reservation of
304 * a server thread has to come while rxi_availProcs and rxi_minDeficit
305 * are locked. To this end, the code has been modified under #ifdef
306 * RX_ENABLE_LOCKS so that quota checks and reservation occur at the
307 * same time. A new function, ReturnToServerPool() returns the allocation.
309 * A call can be on several queue's (but only one at a time). When
310 * rxi_ResetCall wants to remove the call from a queue, it has to ensure
311 * that no one else is touching the queue. To this end, we store the address
312 * of the queue lock in the call structure (under the call lock) when we
313 * put the call on a queue, and we clear the call_queue_lock when the
314 * call is removed from a queue (once the call lock has been obtained).
315 * This allows rxi_ResetCall to safely synchronize with others wishing
316 * to manipulate the queue.
319 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
320 static afs_kmutex_t rx_rpc_stats;
321 void rxi_StartUnlocked(struct rxevent *event, void *call,
322 void *arg1, int istack);
325 /* We keep a "last conn pointer" in rxi_FindConnection. The odds are
326 ** pretty good that the next packet coming in is from the same connection
327 ** as the last packet, since we're send multiple packets in a transmit window.
329 struct rx_connection *rxLastConn = 0;
331 #ifdef RX_ENABLE_LOCKS
332 /* The locking hierarchy for rx fine grain locking is composed of these
335 * rx_connHashTable_lock - synchronizes conn creation, rx_connHashTable access
336 * conn_call_lock - used to synchonize rx_EndCall and rx_NewCall
337 * call->lock - locks call data fields.
338 * These are independent of each other:
339 * rx_freeCallQueue_lock
344 * serverQueueEntry->lock
345 * rx_peerHashTable_lock - locked under rx_connHashTable_lock
347 * peer->lock - locks peer data fields.
348 * conn_data_lock - that more than one thread is not updating a conn data
349 * field at the same time.
358 * Do we need a lock to protect the peer field in the conn structure?
359 * conn->peer was previously a constant for all intents and so has no
360 * lock protecting this field. The multihomed client delta introduced
361 * a RX code change : change the peer field in the connection structure
362 * to that remote interface from which the last packet for this
363 * connection was sent out. This may become an issue if further changes
366 #define SET_CALL_QUEUE_LOCK(C, L) (C)->call_queue_lock = (L)
367 #define CLEAR_CALL_QUEUE_LOCK(C) (C)->call_queue_lock = NULL
369 /* rxdb_fileID is used to identify the lock location, along with line#. */
370 static int rxdb_fileID = RXDB_FILE_RX;
371 #endif /* RX_LOCKS_DB */
372 #else /* RX_ENABLE_LOCKS */
373 #define SET_CALL_QUEUE_LOCK(C, L)
374 #define CLEAR_CALL_QUEUE_LOCK(C)
375 #endif /* RX_ENABLE_LOCKS */
376 struct rx_serverQueueEntry *rx_waitForPacket = 0;
377 struct rx_serverQueueEntry *rx_waitingForPacket = 0;
379 /* ------------Exported Interfaces------------- */
381 /* This function allows rxkad to set the epoch to a suitably random number
382 * which rx_NewConnection will use in the future. The principle purpose is to
383 * get rxnull connections to use the same epoch as the rxkad connections do, at
384 * least once the first rxkad connection is established. This is important now
385 * that the host/port addresses aren't used in FindConnection: the uniqueness
386 * of epoch/cid matters and the start time won't do. */
388 #ifdef AFS_PTHREAD_ENV
390 * This mutex protects the following global variables:
394 #define LOCK_EPOCH MUTEX_ENTER(&epoch_mutex)
395 #define UNLOCK_EPOCH MUTEX_EXIT(&epoch_mutex)
399 #endif /* AFS_PTHREAD_ENV */
402 rx_SetEpoch(afs_uint32 epoch)
409 /* Initialize rx. A port number may be mentioned, in which case this
410 * becomes the default port number for any service installed later.
411 * If 0 is provided for the port number, a random port will be chosen
412 * by the kernel. Whether this will ever overlap anything in
413 * /etc/services is anybody's guess... Returns 0 on success, -1 on
418 int rxinit_status = 1;
419 #ifdef AFS_PTHREAD_ENV
421 * This mutex protects the following global variables:
425 #define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
426 #define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
429 #define UNLOCK_RX_INIT
433 rx_InitHost(u_int host, u_int port)
440 char *htable, *ptable;
447 if (rxinit_status == 0) {
448 tmp_status = rxinit_status;
450 return tmp_status; /* Already started; return previous error code. */
456 if (afs_winsockInit() < 0)
462 * Initialize anything necessary to provide a non-premptive threading
465 rxi_InitializeThreadSupport();
468 /* Allocate and initialize a socket for client and perhaps server
471 rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
472 if (rx_socket == OSI_NULLSOCKET) {
476 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
479 #endif /* RX_LOCKS_DB */
480 MUTEX_INIT(&rx_stats_mutex, "rx_stats_mutex", MUTEX_DEFAULT, 0);
481 MUTEX_INIT(&rx_waiting_mutex, "rx_waiting_mutex", MUTEX_DEFAULT, 0);
482 MUTEX_INIT(&rx_quota_mutex, "rx_quota_mutex", MUTEX_DEFAULT, 0);
483 MUTEX_INIT(&rx_pthread_mutex, "rx_pthread_mutex", MUTEX_DEFAULT, 0);
484 MUTEX_INIT(&rx_packets_mutex, "rx_packets_mutex", MUTEX_DEFAULT, 0);
485 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
486 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
487 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
488 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
490 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
492 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
494 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
496 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
497 #if defined(AFS_HPUX110_ENV)
499 rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
500 #endif /* AFS_HPUX110_ENV */
501 #endif /* RX_ENABLE_LOCKS && KERNEL */
504 rx_connDeadTime = 12;
505 rx_tranquil = 0; /* reset flag */
506 memset(&rx_stats, 0, sizeof(struct rx_statistics));
508 osi_Alloc(rx_hashTableSize * sizeof(struct rx_connection *));
509 PIN(htable, rx_hashTableSize * sizeof(struct rx_connection *)); /* XXXXX */
510 memset(htable, 0, rx_hashTableSize * sizeof(struct rx_connection *));
511 ptable = (char *)osi_Alloc(rx_hashTableSize * sizeof(struct rx_peer *));
512 PIN(ptable, rx_hashTableSize * sizeof(struct rx_peer *)); /* XXXXX */
513 memset(ptable, 0, rx_hashTableSize * sizeof(struct rx_peer *));
515 /* Malloc up a bunch of packets & buffers */
517 queue_Init(&rx_freePacketQueue);
518 rxi_NeedMorePackets = FALSE;
519 rx_nPackets = 0; /* rx_nPackets is managed by rxi_MorePackets* */
521 /* enforce a minimum number of allocated packets */
522 if (rx_extraPackets < rxi_nSendFrags * rx_maxSendWindow)
523 rx_extraPackets = rxi_nSendFrags * rx_maxSendWindow;
525 /* allocate the initial free packet pool */
526 #ifdef RX_ENABLE_TSFPQ
527 rxi_MorePacketsTSFPQ(rx_extraPackets + RX_MAX_QUOTA + 2, RX_TS_FPQ_FLUSH_GLOBAL, 0);
528 #else /* RX_ENABLE_TSFPQ */
529 rxi_MorePackets(rx_extraPackets + RX_MAX_QUOTA + 2); /* fudge */
530 #endif /* RX_ENABLE_TSFPQ */
537 #if defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
538 tv.tv_sec = clock_now.sec;
539 tv.tv_usec = clock_now.usec;
540 srand((unsigned int)tv.tv_usec);
547 #if defined(KERNEL) && !defined(UKERNEL)
548 /* Really, this should never happen in a real kernel */
551 struct sockaddr_in addr;
553 int addrlen = sizeof(addr);
555 socklen_t addrlen = sizeof(addr);
557 if (getsockname((intptr_t)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
561 rx_port = addr.sin_port;
564 rx_stats.minRtt.sec = 9999999;
566 rx_SetEpoch(tv.tv_sec | 0x80000000);
568 rx_SetEpoch(tv.tv_sec); /* Start time of this package, rxkad
569 * will provide a randomer value. */
571 MUTEX_ENTER(&rx_quota_mutex);
572 rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
573 MUTEX_EXIT(&rx_quota_mutex);
574 /* *Slightly* random start time for the cid. This is just to help
575 * out with the hashing function at the peer */
576 rx_nextCid = ((tv.tv_sec ^ tv.tv_usec) << RX_CIDSHIFT);
577 rx_connHashTable = (struct rx_connection **)htable;
578 rx_peerHashTable = (struct rx_peer **)ptable;
580 rx_lastAckDelay.sec = 0;
581 rx_lastAckDelay.usec = 400000; /* 400 milliseconds */
582 rx_hardAckDelay.sec = 0;
583 rx_hardAckDelay.usec = 100000; /* 100 milliseconds */
584 rx_softAckDelay.sec = 0;
585 rx_softAckDelay.usec = 100000; /* 100 milliseconds */
587 rxevent_Init(20, rxi_ReScheduleEvents);
589 /* Initialize various global queues */
590 queue_Init(&rx_idleServerQueue);
591 queue_Init(&rx_incomingCallQueue);
592 queue_Init(&rx_freeCallQueue);
594 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
595 /* Initialize our list of usable IP addresses. */
599 /* Start listener process (exact function is dependent on the
600 * implementation environment--kernel or user space) */
604 tmp_status = rxinit_status = 0;
612 return rx_InitHost(htonl(INADDR_ANY), port);
615 /* called with unincremented nRequestsRunning to see if it is OK to start
616 * a new thread in this service. Could be "no" for two reasons: over the
617 * max quota, or would prevent others from reaching their min quota.
619 #ifdef RX_ENABLE_LOCKS
620 /* This verion of QuotaOK reserves quota if it's ok while the
621 * rx_serverPool_lock is held. Return quota using ReturnToServerPool().
624 QuotaOK(struct rx_service *aservice)
626 /* check if over max quota */
627 if (aservice->nRequestsRunning >= aservice->maxProcs) {
631 /* under min quota, we're OK */
632 /* otherwise, can use only if there are enough to allow everyone
633 * to go to their min quota after this guy starts.
636 MUTEX_ENTER(&rx_quota_mutex);
637 if ((aservice->nRequestsRunning < aservice->minProcs)
638 || (rxi_availProcs > rxi_minDeficit)) {
639 aservice->nRequestsRunning++;
640 /* just started call in minProcs pool, need fewer to maintain
642 if (aservice->nRequestsRunning <= aservice->minProcs)
645 MUTEX_EXIT(&rx_quota_mutex);
648 MUTEX_EXIT(&rx_quota_mutex);
654 ReturnToServerPool(struct rx_service *aservice)
656 aservice->nRequestsRunning--;
657 MUTEX_ENTER(&rx_quota_mutex);
658 if (aservice->nRequestsRunning < aservice->minProcs)
661 MUTEX_EXIT(&rx_quota_mutex);
664 #else /* RX_ENABLE_LOCKS */
666 QuotaOK(struct rx_service *aservice)
669 /* under min quota, we're OK */
670 if (aservice->nRequestsRunning < aservice->minProcs)
673 /* check if over max quota */
674 if (aservice->nRequestsRunning >= aservice->maxProcs)
677 /* otherwise, can use only if there are enough to allow everyone
678 * to go to their min quota after this guy starts.
680 MUTEX_ENTER(&rx_quota_mutex);
681 if (rxi_availProcs > rxi_minDeficit)
683 MUTEX_EXIT(&rx_quota_mutex);
686 #endif /* RX_ENABLE_LOCKS */
689 /* Called by rx_StartServer to start up lwp's to service calls.
690 NExistingProcs gives the number of procs already existing, and which
691 therefore needn't be created. */
693 rxi_StartServerProcs(int nExistingProcs)
695 struct rx_service *service;
700 /* For each service, reserve N processes, where N is the "minimum"
701 * number of processes that MUST be able to execute a request in parallel,
702 * at any time, for that process. Also compute the maximum difference
703 * between any service's maximum number of processes that can run
704 * (i.e. the maximum number that ever will be run, and a guarantee
705 * that this number will run if other services aren't running), and its
706 * minimum number. The result is the extra number of processes that
707 * we need in order to provide the latter guarantee */
708 for (i = 0; i < RX_MAX_SERVICES; i++) {
710 service = rx_services[i];
711 if (service == (struct rx_service *)0)
713 nProcs += service->minProcs;
714 diff = service->maxProcs - service->minProcs;
718 nProcs += maxdiff; /* Extra processes needed to allow max number requested to run in any given service, under good conditions */
719 nProcs -= nExistingProcs; /* Subtract the number of procs that were previously created for use as server procs */
720 for (i = 0; i < nProcs; i++) {
721 rxi_StartServerProc(rx_ServerProc, rx_stackSize);
727 /* This routine is only required on Windows */
729 rx_StartClientThread(void)
731 #ifdef AFS_PTHREAD_ENV
733 pid = pthread_self();
734 #endif /* AFS_PTHREAD_ENV */
736 #endif /* AFS_NT40_ENV */
738 /* This routine must be called if any services are exported. If the
739 * donateMe flag is set, the calling process is donated to the server
742 rx_StartServer(int donateMe)
744 struct rx_service *service;
750 /* Start server processes, if necessary (exact function is dependent
751 * on the implementation environment--kernel or user space). DonateMe
752 * will be 1 if there is 1 pre-existing proc, i.e. this one. In this
753 * case, one less new proc will be created rx_StartServerProcs.
755 rxi_StartServerProcs(donateMe);
757 /* count up the # of threads in minProcs, and add set the min deficit to
758 * be that value, too.
760 for (i = 0; i < RX_MAX_SERVICES; i++) {
761 service = rx_services[i];
762 if (service == (struct rx_service *)0)
764 MUTEX_ENTER(&rx_quota_mutex);
765 rxi_totalMin += service->minProcs;
766 /* below works even if a thread is running, since minDeficit would
767 * still have been decremented and later re-incremented.
769 rxi_minDeficit += service->minProcs;
770 MUTEX_EXIT(&rx_quota_mutex);
773 /* Turn on reaping of idle server connections */
774 rxi_ReapConnections(NULL, NULL, NULL);
783 #ifdef AFS_PTHREAD_ENV
785 pid = afs_pointer_to_int(pthread_self());
786 #else /* AFS_PTHREAD_ENV */
788 LWP_CurrentProcess(&pid);
789 #endif /* AFS_PTHREAD_ENV */
791 sprintf(name, "srv_%d", ++nProcs);
793 (*registerProgram) (pid, name);
795 #endif /* AFS_NT40_ENV */
796 rx_ServerProc(NULL); /* Never returns */
798 #ifdef RX_ENABLE_TSFPQ
799 /* no use leaving packets around in this thread's local queue if
800 * it isn't getting donated to the server thread pool.
802 rxi_FlushLocalPacketsTSFPQ();
803 #endif /* RX_ENABLE_TSFPQ */
807 /* Create a new client connection to the specified service, using the
808 * specified security object to implement the security model for this
810 struct rx_connection *
811 rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
812 struct rx_securityClass *securityObject,
813 int serviceSecurityIndex)
817 struct rx_connection *conn;
822 dpf(("rx_NewConnection(host %x, port %u, service %u, securityObject %p, "
823 "serviceSecurityIndex %d)\n",
824 ntohl(shost), ntohs(sport), sservice, securityObject,
825 serviceSecurityIndex));
827 /* Vasilsi said: "NETPRI protects Cid and Alloc", but can this be true in
828 * the case of kmem_alloc? */
829 conn = rxi_AllocConnection();
830 #ifdef RX_ENABLE_LOCKS
831 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
832 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
833 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
836 MUTEX_ENTER(&rx_connHashTable_lock);
837 cid = (rx_nextCid += RX_MAXCALLS);
838 conn->type = RX_CLIENT_CONNECTION;
840 conn->epoch = rx_epoch;
841 conn->peer = rxi_FindPeer(shost, sport, 0, 1);
842 conn->serviceId = sservice;
843 conn->securityObject = securityObject;
844 conn->securityData = (void *) 0;
845 conn->securityIndex = serviceSecurityIndex;
846 rx_SetConnDeadTime(conn, rx_connDeadTime);
847 rx_SetConnSecondsUntilNatPing(conn, 0);
848 conn->ackRate = RX_FAST_ACK_RATE;
850 conn->specific = NULL;
851 conn->challengeEvent = NULL;
852 conn->delayedAbortEvent = NULL;
853 conn->abortCount = 0;
855 for (i = 0; i < RX_MAXCALLS; i++) {
856 conn->twind[i] = rx_initSendWindow;
857 conn->rwind[i] = rx_initReceiveWindow;
860 RXS_NewConnection(securityObject, conn);
862 CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
864 conn->refCount++; /* no lock required since only this thread knows... */
865 conn->next = rx_connHashTable[hashindex];
866 rx_connHashTable[hashindex] = conn;
868 rx_MutexIncrement(rx_stats.nClientConns, rx_stats_mutex);
869 MUTEX_EXIT(&rx_connHashTable_lock);
875 rx_SetConnDeadTime(struct rx_connection *conn, int seconds)
877 /* The idea is to set the dead time to a value that allows several
878 * keepalives to be dropped without timing out the connection. */
879 conn->secondsUntilDead = MAX(seconds, 6);
880 conn->secondsUntilPing = conn->secondsUntilDead / 6;
883 int rxi_lowPeerRefCount = 0;
884 int rxi_lowConnRefCount = 0;
887 * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
888 * NOTE: must not be called with rx_connHashTable_lock held.
891 rxi_CleanupConnection(struct rx_connection *conn)
893 /* Notify the service exporter, if requested, that this connection
894 * is being destroyed */
895 if (conn->type == RX_SERVER_CONNECTION && conn->service->destroyConnProc)
896 (*conn->service->destroyConnProc) (conn);
898 /* Notify the security module that this connection is being destroyed */
899 RXS_DestroyConnection(conn->securityObject, conn);
901 /* If this is the last connection using the rx_peer struct, set its
902 * idle time to now. rxi_ReapConnections will reap it if it's still
903 * idle (refCount == 0) after rx_idlePeerTime (60 seconds) have passed.
905 MUTEX_ENTER(&rx_peerHashTable_lock);
906 if (conn->peer->refCount < 2) {
907 conn->peer->idleWhen = clock_Sec();
908 if (conn->peer->refCount < 1) {
909 conn->peer->refCount = 1;
910 if (rx_stats_active) {
911 MUTEX_ENTER(&rx_stats_mutex);
912 rxi_lowPeerRefCount++;
913 MUTEX_EXIT(&rx_stats_mutex);
917 conn->peer->refCount--;
918 MUTEX_EXIT(&rx_peerHashTable_lock);
922 if (conn->type == RX_SERVER_CONNECTION)
923 rx_MutexDecrement(rx_stats.nServerConns, rx_stats_mutex);
925 rx_MutexDecrement(rx_stats.nClientConns, rx_stats_mutex);
928 if (conn->specific) {
930 for (i = 0; i < conn->nSpecific; i++) {
931 if (conn->specific[i] && rxi_keyCreate_destructor[i])
932 (*rxi_keyCreate_destructor[i]) (conn->specific[i]);
933 conn->specific[i] = NULL;
935 free(conn->specific);
937 conn->specific = NULL;
941 MUTEX_DESTROY(&conn->conn_call_lock);
942 MUTEX_DESTROY(&conn->conn_data_lock);
943 CV_DESTROY(&conn->conn_call_cv);
945 rxi_FreeConnection(conn);
948 /* Destroy the specified connection */
950 rxi_DestroyConnection(struct rx_connection *conn)
952 MUTEX_ENTER(&rx_connHashTable_lock);
953 rxi_DestroyConnectionNoLock(conn);
954 /* conn should be at the head of the cleanup list */
955 if (conn == rx_connCleanup_list) {
956 rx_connCleanup_list = rx_connCleanup_list->next;
957 MUTEX_EXIT(&rx_connHashTable_lock);
958 rxi_CleanupConnection(conn);
960 #ifdef RX_ENABLE_LOCKS
962 MUTEX_EXIT(&rx_connHashTable_lock);
964 #endif /* RX_ENABLE_LOCKS */
968 rxi_DestroyConnectionNoLock(struct rx_connection *conn)
970 struct rx_connection **conn_ptr;
972 struct rx_packet *packet;
979 MUTEX_ENTER(&conn->conn_data_lock);
980 if (conn->refCount > 0)
983 if (rx_stats_active) {
984 MUTEX_ENTER(&rx_stats_mutex);
985 rxi_lowConnRefCount++;
986 MUTEX_EXIT(&rx_stats_mutex);
990 if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
991 /* Busy; wait till the last guy before proceeding */
992 MUTEX_EXIT(&conn->conn_data_lock);
997 /* If the client previously called rx_NewCall, but it is still
998 * waiting, treat this as a running call, and wait to destroy the
999 * connection later when the call completes. */
1000 if ((conn->type == RX_CLIENT_CONNECTION)
1001 && (conn->flags & (RX_CONN_MAKECALL_WAITING|RX_CONN_MAKECALL_ACTIVE))) {
1002 conn->flags |= RX_CONN_DESTROY_ME;
1003 MUTEX_EXIT(&conn->conn_data_lock);
1007 MUTEX_EXIT(&conn->conn_data_lock);
1009 /* Check for extant references to this connection */
1010 for (i = 0; i < RX_MAXCALLS; i++) {
1011 struct rx_call *call = conn->call[i];
1014 if (conn->type == RX_CLIENT_CONNECTION) {
1015 MUTEX_ENTER(&call->lock);
1016 if (call->delayedAckEvent) {
1017 /* Push the final acknowledgment out now--there
1018 * won't be a subsequent call to acknowledge the
1019 * last reply packets */
1020 rxevent_Cancel(call->delayedAckEvent, call,
1021 RX_CALL_REFCOUNT_DELAY);
1022 if (call->state == RX_STATE_PRECALL
1023 || call->state == RX_STATE_ACTIVE) {
1024 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1026 rxi_AckAll(NULL, call, 0);
1029 MUTEX_EXIT(&call->lock);
1033 #ifdef RX_ENABLE_LOCKS
1035 if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
1036 MUTEX_EXIT(&conn->conn_data_lock);
1038 /* Someone is accessing a packet right now. */
1042 #endif /* RX_ENABLE_LOCKS */
1045 /* Don't destroy the connection if there are any call
1046 * structures still in use */
1047 MUTEX_ENTER(&conn->conn_data_lock);
1048 conn->flags |= RX_CONN_DESTROY_ME;
1049 MUTEX_EXIT(&conn->conn_data_lock);
1054 if (conn->natKeepAliveEvent) {
1055 rxi_NatKeepAliveOff(conn);
1058 if (conn->delayedAbortEvent) {
1059 rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
1060 packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
1062 MUTEX_ENTER(&conn->conn_data_lock);
1063 rxi_SendConnectionAbort(conn, packet, 0, 1);
1064 MUTEX_EXIT(&conn->conn_data_lock);
1065 rxi_FreePacket(packet);
1069 /* Remove from connection hash table before proceeding */
1071 &rx_connHashTable[CONN_HASH
1072 (peer->host, peer->port, conn->cid, conn->epoch,
1074 for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
1075 if (*conn_ptr == conn) {
1076 *conn_ptr = conn->next;
1080 /* if the conn that we are destroying was the last connection, then we
1081 * clear rxLastConn as well */
1082 if (rxLastConn == conn)
1085 /* Make sure the connection is completely reset before deleting it. */
1086 /* get rid of pending events that could zap us later */
1087 if (conn->challengeEvent)
1088 rxevent_Cancel(conn->challengeEvent, (struct rx_call *)0, 0);
1089 if (conn->checkReachEvent)
1090 rxevent_Cancel(conn->checkReachEvent, (struct rx_call *)0, 0);
1091 if (conn->natKeepAliveEvent)
1092 rxevent_Cancel(conn->natKeepAliveEvent, (struct rx_call *)0, 0);
1094 /* Add the connection to the list of destroyed connections that
1095 * need to be cleaned up. This is necessary to avoid deadlocks
1096 * in the routines we call to inform others that this connection is
1097 * being destroyed. */
1098 conn->next = rx_connCleanup_list;
1099 rx_connCleanup_list = conn;
1102 /* Externally available version */
1104 rx_DestroyConnection(struct rx_connection *conn)
1109 rxi_DestroyConnection(conn);
1114 rx_GetConnection(struct rx_connection *conn)
1119 MUTEX_ENTER(&conn->conn_data_lock);
1121 MUTEX_EXIT(&conn->conn_data_lock);
1125 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1126 /* Wait for the transmit queue to no longer be busy.
1127 * requires the call->lock to be held */
1128 static void rxi_WaitforTQBusy(struct rx_call *call) {
1129 while (call->flags & RX_CALL_TQ_BUSY) {
1130 call->flags |= RX_CALL_TQ_WAIT;
1132 #ifdef RX_ENABLE_LOCKS
1133 osirx_AssertMine(&call->lock, "rxi_WaitforTQ lock");
1134 CV_WAIT(&call->cv_tq, &call->lock);
1135 #else /* RX_ENABLE_LOCKS */
1136 osi_rxSleep(&call->tq);
1137 #endif /* RX_ENABLE_LOCKS */
1139 if (call->tqWaiters == 0) {
1140 call->flags &= ~RX_CALL_TQ_WAIT;
1146 /* Start a new rx remote procedure call, on the specified connection.
1147 * If wait is set to 1, wait for a free call channel; otherwise return
1148 * 0. Maxtime gives the maximum number of seconds this call may take,
1149 * after rx_NewCall returns. After this time interval, a call to any
1150 * of rx_SendData, rx_ReadData, etc. will fail with RX_CALL_TIMEOUT.
1151 * For fine grain locking, we hold the conn_call_lock in order to
1152 * to ensure that we don't get signalle after we found a call in an active
1153 * state and before we go to sleep.
1156 rx_NewCall(struct rx_connection *conn)
1159 struct rx_call *call;
1160 struct clock queueTime;
1164 dpf(("rx_NewCall(conn %"AFS_PTR_FMT")\n", conn));
1167 clock_GetTime(&queueTime);
1169 * Check if there are others waiting for a new call.
1170 * If so, let them go first to avoid starving them.
1171 * This is a fairly simple scheme, and might not be
1172 * a complete solution for large numbers of waiters.
1174 * makeCallWaiters keeps track of the number of
1175 * threads waiting to make calls and the
1176 * RX_CONN_MAKECALL_WAITING flag bit is used to
1177 * indicate that there are indeed calls waiting.
1178 * The flag is set when the waiter is incremented.
1179 * It is only cleared when makeCallWaiters is 0.
1180 * This prevents us from accidently destroying the
1181 * connection while it is potentially about to be used.
1183 MUTEX_ENTER(&conn->conn_call_lock);
1184 MUTEX_ENTER(&conn->conn_data_lock);
1185 while (conn->flags & RX_CONN_MAKECALL_ACTIVE) {
1186 conn->flags |= RX_CONN_MAKECALL_WAITING;
1187 conn->makeCallWaiters++;
1188 MUTEX_EXIT(&conn->conn_data_lock);
1190 #ifdef RX_ENABLE_LOCKS
1191 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1195 MUTEX_ENTER(&conn->conn_data_lock);
1196 conn->makeCallWaiters--;
1197 if (conn->makeCallWaiters == 0)
1198 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1201 /* We are now the active thread in rx_NewCall */
1202 conn->flags |= RX_CONN_MAKECALL_ACTIVE;
1203 MUTEX_EXIT(&conn->conn_data_lock);
1208 for (i = 0; i < RX_MAXCALLS; i++) {
1209 call = conn->call[i];
1211 if (call->state == RX_STATE_DALLY) {
1212 MUTEX_ENTER(&call->lock);
1213 if (call->state == RX_STATE_DALLY) {
1215 * We are setting the state to RX_STATE_RESET to
1216 * ensure that no one else will attempt to use this
1217 * call once we drop the conn->conn_call_lock and
1218 * call->lock. We must drop the conn->conn_call_lock
1219 * before calling rxi_ResetCall because the process
1220 * of clearing the transmit queue can block for an
1221 * extended period of time. If we block while holding
1222 * the conn->conn_call_lock, then all rx_EndCall
1223 * processing will block as well. This has a detrimental
1224 * effect on overall system performance.
1226 call->state = RX_STATE_RESET;
1227 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1228 MUTEX_EXIT(&conn->conn_call_lock);
1229 rxi_ResetCall(call, 0);
1230 (*call->callNumber)++;
1231 if (MUTEX_TRYENTER(&conn->conn_call_lock))
1235 * If we failed to be able to safely obtain the
1236 * conn->conn_call_lock we will have to drop the
1237 * call->lock to avoid a deadlock. When the call->lock
1238 * is released the state of the call can change. If it
1239 * is no longer RX_STATE_RESET then some other thread is
1242 MUTEX_EXIT(&call->lock);
1243 MUTEX_ENTER(&conn->conn_call_lock);
1244 MUTEX_ENTER(&call->lock);
1246 if (call->state == RX_STATE_RESET)
1250 * If we get here it means that after dropping
1251 * the conn->conn_call_lock and call->lock that
1252 * the call is no longer ours. If we can't find
1253 * a free call in the remaining slots we should
1254 * not go immediately to RX_CONN_MAKECALL_WAITING
1255 * because by dropping the conn->conn_call_lock
1256 * we have given up synchronization with rx_EndCall.
1257 * Instead, cycle through one more time to see if
1258 * we can find a call that can call our own.
1260 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
1263 MUTEX_EXIT(&call->lock);
1266 /* rxi_NewCall returns with mutex locked */
1267 call = rxi_NewCall(conn, i);
1268 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1272 if (i < RX_MAXCALLS) {
1278 MUTEX_ENTER(&conn->conn_data_lock);
1279 conn->flags |= RX_CONN_MAKECALL_WAITING;
1280 conn->makeCallWaiters++;
1281 MUTEX_EXIT(&conn->conn_data_lock);
1283 #ifdef RX_ENABLE_LOCKS
1284 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1288 MUTEX_ENTER(&conn->conn_data_lock);
1289 conn->makeCallWaiters--;
1290 if (conn->makeCallWaiters == 0)
1291 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1292 MUTEX_EXIT(&conn->conn_data_lock);
1294 /* Client is initially in send mode */
1295 call->state = RX_STATE_ACTIVE;
1296 call->error = conn->error;
1298 call->mode = RX_MODE_ERROR;
1300 call->mode = RX_MODE_SENDING;
1302 /* remember start time for call in case we have hard dead time limit */
1303 call->queueTime = queueTime;
1304 clock_GetTime(&call->startTime);
1305 hzero(call->bytesSent);
1306 hzero(call->bytesRcvd);
1308 /* Turn on busy protocol. */
1309 rxi_KeepAliveOn(call);
1311 /* Attempt MTU discovery */
1312 rxi_GrowMTUOn(call);
1315 * We are no longer the active thread in rx_NewCall
1317 MUTEX_ENTER(&conn->conn_data_lock);
1318 conn->flags &= ~RX_CONN_MAKECALL_ACTIVE;
1319 MUTEX_EXIT(&conn->conn_data_lock);
1322 * Wake up anyone else who might be giving us a chance to
1323 * run (see code above that avoids resource starvation).
1325 #ifdef RX_ENABLE_LOCKS
1326 CV_BROADCAST(&conn->conn_call_cv);
1330 MUTEX_EXIT(&conn->conn_call_lock);
1332 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1333 if (call->flags & (RX_CALL_TQ_BUSY | RX_CALL_TQ_CLEARME)) {
1334 osi_Panic("rx_NewCall call about to be used without an empty tq");
1336 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1338 MUTEX_EXIT(&call->lock);
1341 dpf(("rx_NewCall(call %"AFS_PTR_FMT")\n", call));
1346 rxi_HasActiveCalls(struct rx_connection *aconn)
1349 struct rx_call *tcall;
1353 for (i = 0; i < RX_MAXCALLS; i++) {
1354 if ((tcall = aconn->call[i])) {
1355 if ((tcall->state == RX_STATE_ACTIVE)
1356 || (tcall->state == RX_STATE_PRECALL)) {
1367 rxi_GetCallNumberVector(struct rx_connection *aconn,
1368 afs_int32 * aint32s)
1371 struct rx_call *tcall;
1375 for (i = 0; i < RX_MAXCALLS; i++) {
1376 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1377 aint32s[i] = aconn->callNumber[i] + 1;
1379 aint32s[i] = aconn->callNumber[i];
1386 rxi_SetCallNumberVector(struct rx_connection *aconn,
1387 afs_int32 * aint32s)
1390 struct rx_call *tcall;
1394 for (i = 0; i < RX_MAXCALLS; i++) {
1395 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1396 aconn->callNumber[i] = aint32s[i] - 1;
1398 aconn->callNumber[i] = aint32s[i];
1404 /* Advertise a new service. A service is named locally by a UDP port
1405 * number plus a 16-bit service id. Returns (struct rx_service *) 0
1408 char *serviceName; Name for identification purposes (e.g. the
1409 service name might be used for probing for
1412 rx_NewServiceHost(afs_uint32 host, u_short port, u_short serviceId,
1413 char *serviceName, struct rx_securityClass **securityObjects,
1414 int nSecurityObjects,
1415 afs_int32(*serviceProc) (struct rx_call * acall))
1417 osi_socket socket = OSI_NULLSOCKET;
1418 struct rx_service *tservice;
1424 if (serviceId == 0) {
1426 "rx_NewService: service id for service %s is not non-zero.\n",
1433 "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",
1441 tservice = rxi_AllocService();
1444 #ifdef RX_ENABLE_LOCKS
1445 MUTEX_INIT(&tservice->svc_data_lock, "svc data lock", MUTEX_DEFAULT, 0);
1448 for (i = 0; i < RX_MAX_SERVICES; i++) {
1449 struct rx_service *service = rx_services[i];
1451 if (port == service->servicePort && host == service->serviceHost) {
1452 if (service->serviceId == serviceId) {
1453 /* The identical service has already been
1454 * installed; if the caller was intending to
1455 * change the security classes used by this
1456 * service, he/she loses. */
1458 "rx_NewService: tried to install service %s with service id %d, which is already in use for service %s\n",
1459 serviceName, serviceId, service->serviceName);
1461 rxi_FreeService(tservice);
1464 /* Different service, same port: re-use the socket
1465 * which is bound to the same port */
1466 socket = service->socket;
1469 if (socket == OSI_NULLSOCKET) {
1470 /* If we don't already have a socket (from another
1471 * service on same port) get a new one */
1472 socket = rxi_GetHostUDPSocket(host, port);
1473 if (socket == OSI_NULLSOCKET) {
1475 rxi_FreeService(tservice);
1480 service->socket = socket;
1481 service->serviceHost = host;
1482 service->servicePort = port;
1483 service->serviceId = serviceId;
1484 service->serviceName = serviceName;
1485 service->nSecurityObjects = nSecurityObjects;
1486 service->securityObjects = securityObjects;
1487 service->minProcs = 0;
1488 service->maxProcs = 1;
1489 service->idleDeadTime = 60;
1490 service->idleDeadErr = 0;
1491 service->connDeadTime = rx_connDeadTime;
1492 service->executeRequestProc = serviceProc;
1493 service->checkReach = 0;
1494 service->nSpecific = 0;
1495 service->specific = NULL;
1496 rx_services[i] = service; /* not visible until now */
1502 rxi_FreeService(tservice);
1503 (osi_Msg "rx_NewService: cannot support > %d services\n",
1508 /* Set configuration options for all of a service's security objects */
1511 rx_SetSecurityConfiguration(struct rx_service *service,
1512 rx_securityConfigVariables type,
1516 for (i = 0; i<service->nSecurityObjects; i++) {
1517 if (service->securityObjects[i]) {
1518 RXS_SetConfiguration(service->securityObjects[i], NULL, type,
1526 rx_NewService(u_short port, u_short serviceId, char *serviceName,
1527 struct rx_securityClass **securityObjects, int nSecurityObjects,
1528 afs_int32(*serviceProc) (struct rx_call * acall))
1530 return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
1533 /* Generic request processing loop. This routine should be called
1534 * by the implementation dependent rx_ServerProc. If socketp is
1535 * non-null, it will be set to the file descriptor that this thread
1536 * is now listening on. If socketp is null, this routine will never
1539 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1541 struct rx_call *call;
1543 struct rx_service *tservice = NULL;
1550 call = rx_GetCall(threadID, tservice, socketp);
1551 if (socketp && *socketp != OSI_NULLSOCKET) {
1552 /* We are now a listener thread */
1557 /* if server is restarting( typically smooth shutdown) then do not
1558 * allow any new calls.
1561 if (rx_tranquil && (call != NULL)) {
1565 MUTEX_ENTER(&call->lock);
1567 rxi_CallError(call, RX_RESTARTING);
1568 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1570 MUTEX_EXIT(&call->lock);
1574 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1575 #ifdef RX_ENABLE_LOCKS
1577 #endif /* RX_ENABLE_LOCKS */
1578 afs_termState = AFSOP_STOP_AFS;
1579 afs_osi_Wakeup(&afs_termState);
1580 #ifdef RX_ENABLE_LOCKS
1582 #endif /* RX_ENABLE_LOCKS */
1587 tservice = call->conn->service;
1589 if (tservice->beforeProc)
1590 (*tservice->beforeProc) (call);
1592 code = tservice->executeRequestProc(call);
1594 if (tservice->afterProc)
1595 (*tservice->afterProc) (call, code);
1597 rx_EndCall(call, code);
1598 if (rx_stats_active) {
1599 MUTEX_ENTER(&rx_stats_mutex);
1601 MUTEX_EXIT(&rx_stats_mutex);
1608 rx_WakeupServerProcs(void)
1610 struct rx_serverQueueEntry *np, *tqp;
1614 MUTEX_ENTER(&rx_serverPool_lock);
1616 #ifdef RX_ENABLE_LOCKS
1617 if (rx_waitForPacket)
1618 CV_BROADCAST(&rx_waitForPacket->cv);
1619 #else /* RX_ENABLE_LOCKS */
1620 if (rx_waitForPacket)
1621 osi_rxWakeup(rx_waitForPacket);
1622 #endif /* RX_ENABLE_LOCKS */
1623 MUTEX_ENTER(&freeSQEList_lock);
1624 for (np = rx_FreeSQEList; np; np = tqp) {
1625 tqp = *(struct rx_serverQueueEntry **)np;
1626 #ifdef RX_ENABLE_LOCKS
1627 CV_BROADCAST(&np->cv);
1628 #else /* RX_ENABLE_LOCKS */
1630 #endif /* RX_ENABLE_LOCKS */
1632 MUTEX_EXIT(&freeSQEList_lock);
1633 for (queue_Scan(&rx_idleServerQueue, np, tqp, rx_serverQueueEntry)) {
1634 #ifdef RX_ENABLE_LOCKS
1635 CV_BROADCAST(&np->cv);
1636 #else /* RX_ENABLE_LOCKS */
1638 #endif /* RX_ENABLE_LOCKS */
1640 MUTEX_EXIT(&rx_serverPool_lock);
1645 * One thing that seems to happen is that all the server threads get
1646 * tied up on some empty or slow call, and then a whole bunch of calls
1647 * arrive at once, using up the packet pool, so now there are more
1648 * empty calls. The most critical resources here are server threads
1649 * and the free packet pool. The "doreclaim" code seems to help in
1650 * general. I think that eventually we arrive in this state: there
1651 * are lots of pending calls which do have all their packets present,
1652 * so they won't be reclaimed, are multi-packet calls, so they won't
1653 * be scheduled until later, and thus are tying up most of the free
1654 * packet pool for a very long time.
1656 * 1. schedule multi-packet calls if all the packets are present.
1657 * Probably CPU-bound operation, useful to return packets to pool.
1658 * Do what if there is a full window, but the last packet isn't here?
1659 * 3. preserve one thread which *only* runs "best" calls, otherwise
1660 * it sleeps and waits for that type of call.
1661 * 4. Don't necessarily reserve a whole window for each thread. In fact,
1662 * the current dataquota business is badly broken. The quota isn't adjusted
1663 * to reflect how many packets are presently queued for a running call.
1664 * So, when we schedule a queued call with a full window of packets queued
1665 * up for it, that *should* free up a window full of packets for other 2d-class
1666 * calls to be able to use from the packet pool. But it doesn't.
1668 * NB. Most of the time, this code doesn't run -- since idle server threads
1669 * sit on the idle server queue and are assigned by "...ReceivePacket" as soon
1670 * as a new call arrives.
1672 /* Sleep until a call arrives. Returns a pointer to the call, ready
1673 * for an rx_Read. */
1674 #ifdef RX_ENABLE_LOCKS
1676 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1678 struct rx_serverQueueEntry *sq;
1679 struct rx_call *call = (struct rx_call *)0;
1680 struct rx_service *service = NULL;
1683 MUTEX_ENTER(&freeSQEList_lock);
1685 if ((sq = rx_FreeSQEList)) {
1686 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1687 MUTEX_EXIT(&freeSQEList_lock);
1688 } else { /* otherwise allocate a new one and return that */
1689 MUTEX_EXIT(&freeSQEList_lock);
1690 sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1691 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1692 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1695 MUTEX_ENTER(&rx_serverPool_lock);
1696 if (cur_service != NULL) {
1697 ReturnToServerPool(cur_service);
1700 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1701 struct rx_call *tcall, *ncall, *choice2 = NULL;
1703 /* Scan for eligible incoming calls. A call is not eligible
1704 * if the maximum number of calls for its service type are
1705 * already executing */
1706 /* One thread will process calls FCFS (to prevent starvation),
1707 * while the other threads may run ahead looking for calls which
1708 * have all their input data available immediately. This helps
1709 * keep threads from blocking, waiting for data from the client. */
1710 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1711 service = tcall->conn->service;
1712 if (!QuotaOK(service)) {
1715 MUTEX_ENTER(&rx_pthread_mutex);
1716 if (tno == rxi_fcfs_thread_num
1717 || !tcall->queue_item_header.next) {
1718 MUTEX_EXIT(&rx_pthread_mutex);
1719 /* If we're the fcfs thread , then we'll just use
1720 * this call. If we haven't been able to find an optimal
1721 * choice, and we're at the end of the list, then use a
1722 * 2d choice if one has been identified. Otherwise... */
1723 call = (choice2 ? choice2 : tcall);
1724 service = call->conn->service;
1726 MUTEX_EXIT(&rx_pthread_mutex);
1727 if (!queue_IsEmpty(&tcall->rq)) {
1728 struct rx_packet *rp;
1729 rp = queue_First(&tcall->rq, rx_packet);
1730 if (rp->header.seq == 1) {
1732 || (rp->header.flags & RX_LAST_PACKET)) {
1734 } else if (rxi_2dchoice && !choice2
1735 && !(tcall->flags & RX_CALL_CLEARED)
1736 && (tcall->rprev > rxi_HardAckRate)) {
1746 ReturnToServerPool(service);
1753 MUTEX_EXIT(&rx_serverPool_lock);
1754 MUTEX_ENTER(&call->lock);
1756 if (call->flags & RX_CALL_WAIT_PROC) {
1757 call->flags &= ~RX_CALL_WAIT_PROC;
1758 MUTEX_ENTER(&rx_waiting_mutex);
1760 MUTEX_EXIT(&rx_waiting_mutex);
1763 if (call->state != RX_STATE_PRECALL || call->error) {
1764 MUTEX_EXIT(&call->lock);
1765 MUTEX_ENTER(&rx_serverPool_lock);
1766 ReturnToServerPool(service);
1771 if (queue_IsEmpty(&call->rq)
1772 || queue_First(&call->rq, rx_packet)->header.seq != 1)
1773 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1775 CLEAR_CALL_QUEUE_LOCK(call);
1778 /* If there are no eligible incoming calls, add this process
1779 * to the idle server queue, to wait for one */
1783 *socketp = OSI_NULLSOCKET;
1785 sq->socketp = socketp;
1786 queue_Append(&rx_idleServerQueue, sq);
1787 #ifndef AFS_AIX41_ENV
1788 rx_waitForPacket = sq;
1790 rx_waitingForPacket = sq;
1791 #endif /* AFS_AIX41_ENV */
1793 CV_WAIT(&sq->cv, &rx_serverPool_lock);
1795 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1796 MUTEX_EXIT(&rx_serverPool_lock);
1797 return (struct rx_call *)0;
1800 } while (!(call = sq->newcall)
1801 && !(socketp && *socketp != OSI_NULLSOCKET));
1802 MUTEX_EXIT(&rx_serverPool_lock);
1804 MUTEX_ENTER(&call->lock);
1810 MUTEX_ENTER(&freeSQEList_lock);
1811 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1812 rx_FreeSQEList = sq;
1813 MUTEX_EXIT(&freeSQEList_lock);
1816 clock_GetTime(&call->startTime);
1817 call->state = RX_STATE_ACTIVE;
1818 call->mode = RX_MODE_RECEIVING;
1819 #ifdef RX_KERNEL_TRACE
1820 if (ICL_SETACTIVE(afs_iclSetp)) {
1821 int glockOwner = ISAFS_GLOCK();
1824 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1825 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1832 rxi_calltrace(RX_CALL_START, call);
1833 dpf(("rx_GetCall(port=%d, service=%d) ==> call %"AFS_PTR_FMT"\n",
1834 call->conn->service->servicePort, call->conn->service->serviceId,
1837 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1838 MUTEX_EXIT(&call->lock);
1840 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
1845 #else /* RX_ENABLE_LOCKS */
1847 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1849 struct rx_serverQueueEntry *sq;
1850 struct rx_call *call = (struct rx_call *)0, *choice2;
1851 struct rx_service *service = NULL;
1855 MUTEX_ENTER(&freeSQEList_lock);
1857 if ((sq = rx_FreeSQEList)) {
1858 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1859 MUTEX_EXIT(&freeSQEList_lock);
1860 } else { /* otherwise allocate a new one and return that */
1861 MUTEX_EXIT(&freeSQEList_lock);
1862 sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1863 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1864 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1866 MUTEX_ENTER(&sq->lock);
1868 if (cur_service != NULL) {
1869 cur_service->nRequestsRunning--;
1870 MUTEX_ENTER(&rx_quota_mutex);
1871 if (cur_service->nRequestsRunning < cur_service->minProcs)
1874 MUTEX_EXIT(&rx_quota_mutex);
1876 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1877 struct rx_call *tcall, *ncall;
1878 /* Scan for eligible incoming calls. A call is not eligible
1879 * if the maximum number of calls for its service type are
1880 * already executing */
1881 /* One thread will process calls FCFS (to prevent starvation),
1882 * while the other threads may run ahead looking for calls which
1883 * have all their input data available immediately. This helps
1884 * keep threads from blocking, waiting for data from the client. */
1885 choice2 = (struct rx_call *)0;
1886 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1887 service = tcall->conn->service;
1888 if (QuotaOK(service)) {
1889 MUTEX_ENTER(&rx_pthread_mutex);
1890 if (tno == rxi_fcfs_thread_num
1891 || !tcall->queue_item_header.next) {
1892 MUTEX_EXIT(&rx_pthread_mutex);
1893 /* If we're the fcfs thread, then we'll just use
1894 * this call. If we haven't been able to find an optimal
1895 * choice, and we're at the end of the list, then use a
1896 * 2d choice if one has been identified. Otherwise... */
1897 call = (choice2 ? choice2 : tcall);
1898 service = call->conn->service;
1900 MUTEX_EXIT(&rx_pthread_mutex);
1901 if (!queue_IsEmpty(&tcall->rq)) {
1902 struct rx_packet *rp;
1903 rp = queue_First(&tcall->rq, rx_packet);
1904 if (rp->header.seq == 1
1906 || (rp->header.flags & RX_LAST_PACKET))) {
1908 } else if (rxi_2dchoice && !choice2
1909 && !(tcall->flags & RX_CALL_CLEARED)
1910 && (tcall->rprev > rxi_HardAckRate)) {
1924 /* we can't schedule a call if there's no data!!! */
1925 /* send an ack if there's no data, if we're missing the
1926 * first packet, or we're missing something between first
1927 * and last -- there's a "hole" in the incoming data. */
1928 if (queue_IsEmpty(&call->rq)
1929 || queue_First(&call->rq, rx_packet)->header.seq != 1
1930 || call->rprev != queue_Last(&call->rq, rx_packet)->header.seq)
1931 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1933 call->flags &= (~RX_CALL_WAIT_PROC);
1934 service->nRequestsRunning++;
1935 /* just started call in minProcs pool, need fewer to maintain
1937 MUTEX_ENTER(&rx_quota_mutex);
1938 if (service->nRequestsRunning <= service->minProcs)
1941 MUTEX_EXIT(&rx_quota_mutex);
1943 /* MUTEX_EXIT(&call->lock); */
1945 /* If there are no eligible incoming calls, add this process
1946 * to the idle server queue, to wait for one */
1949 *socketp = OSI_NULLSOCKET;
1951 sq->socketp = socketp;
1952 queue_Append(&rx_idleServerQueue, sq);
1956 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1958 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
1959 return (struct rx_call *)0;
1962 } while (!(call = sq->newcall)
1963 && !(socketp && *socketp != OSI_NULLSOCKET));
1965 MUTEX_EXIT(&sq->lock);
1967 MUTEX_ENTER(&freeSQEList_lock);
1968 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1969 rx_FreeSQEList = sq;
1970 MUTEX_EXIT(&freeSQEList_lock);
1973 clock_GetTime(&call->startTime);
1974 call->state = RX_STATE_ACTIVE;
1975 call->mode = RX_MODE_RECEIVING;
1976 #ifdef RX_KERNEL_TRACE
1977 if (ICL_SETACTIVE(afs_iclSetp)) {
1978 int glockOwner = ISAFS_GLOCK();
1981 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1982 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1989 rxi_calltrace(RX_CALL_START, call);
1990 dpf(("rx_GetCall(port=%d, service=%d) ==> call %p\n",
1991 call->conn->service->servicePort, call->conn->service->serviceId,
1994 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
2001 #endif /* RX_ENABLE_LOCKS */
2005 /* Establish a procedure to be called when a packet arrives for a
2006 * call. This routine will be called at most once after each call,
2007 * and will also be called if there is an error condition on the or
2008 * the call is complete. Used by multi rx to build a selection
2009 * function which determines which of several calls is likely to be a
2010 * good one to read from.
2011 * NOTE: the way this is currently implemented it is probably only a
2012 * good idea to (1) use it immediately after a newcall (clients only)
2013 * and (2) only use it once. Other uses currently void your warranty
2016 rx_SetArrivalProc(struct rx_call *call,
2017 void (*proc) (struct rx_call * call,
2020 void * handle, int arg)
2022 call->arrivalProc = proc;
2023 call->arrivalProcHandle = handle;
2024 call->arrivalProcArg = arg;
2027 /* Call is finished (possibly prematurely). Return rc to the peer, if
2028 * appropriate, and return the final error code from the conversation
2032 rx_EndCall(struct rx_call *call, afs_int32 rc)
2034 struct rx_connection *conn = call->conn;
2038 dpf(("rx_EndCall(call %"AFS_PTR_FMT" rc %d error %d abortCode %d)\n",
2039 call, rc, call->error, call->abortCode));
2042 MUTEX_ENTER(&call->lock);
2044 if (rc == 0 && call->error == 0) {
2045 call->abortCode = 0;
2046 call->abortCount = 0;
2049 call->arrivalProc = (void (*)())0;
2050 if (rc && call->error == 0) {
2051 rxi_CallError(call, rc);
2052 /* Send an abort message to the peer if this error code has
2053 * only just been set. If it was set previously, assume the
2054 * peer has already been sent the error code or will request it
2056 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
2058 if (conn->type == RX_SERVER_CONNECTION) {
2059 /* Make sure reply or at least dummy reply is sent */
2060 if (call->mode == RX_MODE_RECEIVING) {
2061 rxi_WriteProc(call, 0, 0);
2063 if (call->mode == RX_MODE_SENDING) {
2064 rxi_FlushWrite(call);
2066 rxi_calltrace(RX_CALL_END, call);
2067 /* Call goes to hold state until reply packets are acknowledged */
2068 if (call->tfirst + call->nSoftAcked < call->tnext) {
2069 call->state = RX_STATE_HOLD;
2071 call->state = RX_STATE_DALLY;
2072 rxi_ClearTransmitQueue(call, 0);
2073 rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
2074 rxevent_Cancel(call->keepAliveEvent, call,
2075 RX_CALL_REFCOUNT_ALIVE);
2077 } else { /* Client connection */
2079 /* Make sure server receives input packets, in the case where
2080 * no reply arguments are expected */
2081 if ((call->mode == RX_MODE_SENDING)
2082 || (call->mode == RX_MODE_RECEIVING && call->rnext == 1)) {
2083 (void)rxi_ReadProc(call, &dummy, 1);
2086 /* If we had an outstanding delayed ack, be nice to the server
2087 * and force-send it now.
2089 if (call->delayedAckEvent) {
2090 rxevent_Cancel(call->delayedAckEvent, call,
2091 RX_CALL_REFCOUNT_DELAY);
2092 call->delayedAckEvent = NULL;
2093 rxi_SendDelayedAck(NULL, call, NULL);
2096 /* We need to release the call lock since it's lower than the
2097 * conn_call_lock and we don't want to hold the conn_call_lock
2098 * over the rx_ReadProc call. The conn_call_lock needs to be held
2099 * here for the case where rx_NewCall is perusing the calls on
2100 * the connection structure. We don't want to signal until
2101 * rx_NewCall is in a stable state. Otherwise, rx_NewCall may
2102 * have checked this call, found it active and by the time it
2103 * goes to sleep, will have missed the signal.
2105 MUTEX_EXIT(&call->lock);
2106 MUTEX_ENTER(&conn->conn_call_lock);
2107 MUTEX_ENTER(&call->lock);
2108 MUTEX_ENTER(&conn->conn_data_lock);
2109 conn->flags |= RX_CONN_BUSY;
2110 if (conn->flags & RX_CONN_MAKECALL_WAITING) {
2111 MUTEX_EXIT(&conn->conn_data_lock);
2112 #ifdef RX_ENABLE_LOCKS
2113 CV_BROADCAST(&conn->conn_call_cv);
2118 #ifdef RX_ENABLE_LOCKS
2120 MUTEX_EXIT(&conn->conn_data_lock);
2122 #endif /* RX_ENABLE_LOCKS */
2123 call->state = RX_STATE_DALLY;
2125 error = call->error;
2127 /* currentPacket, nLeft, and NFree must be zeroed here, because
2128 * ResetCall cannot: ResetCall may be called at splnet(), in the
2129 * kernel version, and may interrupt the macros rx_Read or
2130 * rx_Write, which run at normal priority for efficiency. */
2131 if (call->currentPacket) {
2132 #ifdef RX_TRACK_PACKETS
2133 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
2135 rxi_FreePacket(call->currentPacket);
2136 call->currentPacket = (struct rx_packet *)0;
2139 call->nLeft = call->nFree = call->curlen = 0;
2141 /* Free any packets from the last call to ReadvProc/WritevProc */
2142 #ifdef RXDEBUG_PACKET
2144 #endif /* RXDEBUG_PACKET */
2145 rxi_FreePackets(0, &call->iovq);
2147 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
2148 MUTEX_EXIT(&call->lock);
2149 if (conn->type == RX_CLIENT_CONNECTION) {
2150 MUTEX_ENTER(&conn->conn_data_lock);
2151 conn->flags &= ~RX_CONN_BUSY;
2152 MUTEX_EXIT(&conn->conn_data_lock);
2153 MUTEX_EXIT(&conn->conn_call_lock);
2157 * Map errors to the local host's errno.h format.
2159 error = ntoh_syserr_conv(error);
2163 #if !defined(KERNEL)
2165 /* Call this routine when shutting down a server or client (especially
2166 * clients). This will allow Rx to gracefully garbage collect server
2167 * connections, and reduce the number of retries that a server might
2168 * make to a dead client.
2169 * This is not quite right, since some calls may still be ongoing and
2170 * we can't lock them to destroy them. */
2174 struct rx_connection **conn_ptr, **conn_end;
2178 if (rxinit_status == 1) {
2180 return; /* Already shutdown. */
2182 rxi_DeleteCachedConnections();
2183 if (rx_connHashTable) {
2184 MUTEX_ENTER(&rx_connHashTable_lock);
2185 for (conn_ptr = &rx_connHashTable[0], conn_end =
2186 &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
2188 struct rx_connection *conn, *next;
2189 for (conn = *conn_ptr; conn; conn = next) {
2191 if (conn->type == RX_CLIENT_CONNECTION) {
2192 /* MUTEX_ENTER(&conn->conn_data_lock); when used in kernel */
2194 /* MUTEX_EXIT(&conn->conn_data_lock); when used in kernel */
2195 #ifdef RX_ENABLE_LOCKS
2196 rxi_DestroyConnectionNoLock(conn);
2197 #else /* RX_ENABLE_LOCKS */
2198 rxi_DestroyConnection(conn);
2199 #endif /* RX_ENABLE_LOCKS */
2203 #ifdef RX_ENABLE_LOCKS
2204 while (rx_connCleanup_list) {
2205 struct rx_connection *conn;
2206 conn = rx_connCleanup_list;
2207 rx_connCleanup_list = rx_connCleanup_list->next;
2208 MUTEX_EXIT(&rx_connHashTable_lock);
2209 rxi_CleanupConnection(conn);
2210 MUTEX_ENTER(&rx_connHashTable_lock);
2212 MUTEX_EXIT(&rx_connHashTable_lock);
2213 #endif /* RX_ENABLE_LOCKS */
2218 afs_winsockCleanup();
2226 /* if we wakeup packet waiter too often, can get in loop with two
2227 AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2229 rxi_PacketsUnWait(void)
2231 if (!rx_waitingForPackets) {
2235 if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2236 return; /* still over quota */
2239 rx_waitingForPackets = 0;
2240 #ifdef RX_ENABLE_LOCKS
2241 CV_BROADCAST(&rx_waitingForPackets_cv);
2243 osi_rxWakeup(&rx_waitingForPackets);
2249 /* ------------------Internal interfaces------------------------- */
2251 /* Return this process's service structure for the
2252 * specified socket and service */
2254 rxi_FindService(osi_socket socket, u_short serviceId)
2256 struct rx_service **sp;
2257 for (sp = &rx_services[0]; *sp; sp++) {
2258 if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2264 #ifdef RXDEBUG_PACKET
2265 #ifdef KDUMP_RX_LOCK
2266 static struct rx_call_rx_lock *rx_allCallsp = 0;
2268 static struct rx_call *rx_allCallsp = 0;
2270 #endif /* RXDEBUG_PACKET */
2272 /* Allocate a call structure, for the indicated channel of the
2273 * supplied connection. The mode and state of the call must be set by
2274 * the caller. Returns the call with mutex locked. */
2276 rxi_NewCall(struct rx_connection *conn, int channel)
2278 struct rx_call *call;
2279 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2280 struct rx_call *cp; /* Call pointer temp */
2281 struct rx_call *nxp; /* Next call pointer, for queue_Scan */
2282 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2284 dpf(("rxi_NewCall(conn %"AFS_PTR_FMT", channel %d)\n", conn, channel));
2286 /* Grab an existing call structure, or allocate a new one.
2287 * Existing call structures are assumed to have been left reset by
2289 MUTEX_ENTER(&rx_freeCallQueue_lock);
2291 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2293 * EXCEPT that the TQ might not yet be cleared out.
2294 * Skip over those with in-use TQs.
2297 for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2298 if (!(cp->flags & RX_CALL_TQ_BUSY)) {
2304 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2305 if (queue_IsNotEmpty(&rx_freeCallQueue)) {
2306 call = queue_First(&rx_freeCallQueue, rx_call);
2307 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2309 if (rx_stats_active)
2310 rx_MutexDecrement(rx_stats.nFreeCallStructs, rx_stats_mutex);
2311 MUTEX_EXIT(&rx_freeCallQueue_lock);
2312 MUTEX_ENTER(&call->lock);
2313 CLEAR_CALL_QUEUE_LOCK(call);
2314 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2315 /* Now, if TQ wasn't cleared earlier, do it now. */
2316 rxi_WaitforTQBusy(call);
2317 if (call->flags & RX_CALL_TQ_CLEARME) {
2318 rxi_ClearTransmitQueue(call, 1);
2319 /*queue_Init(&call->tq);*/
2321 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2322 /* Bind the call to its connection structure */
2324 rxi_ResetCall(call, 1);
2327 call = rxi_Alloc(sizeof(struct rx_call));
2328 #ifdef RXDEBUG_PACKET
2329 call->allNextp = rx_allCallsp;
2330 rx_allCallsp = call;
2332 #endif /* RXDEBUG_PACKET */
2333 rx_MutexIncrement(rx_stats.nCallStructs, rx_stats_mutex);
2335 MUTEX_EXIT(&rx_freeCallQueue_lock);
2336 MUTEX_INIT(&call->lock, "call lock", MUTEX_DEFAULT, NULL);
2337 MUTEX_ENTER(&call->lock);
2338 CV_INIT(&call->cv_twind, "call twind", CV_DEFAULT, 0);
2339 CV_INIT(&call->cv_rq, "call rq", CV_DEFAULT, 0);
2340 CV_INIT(&call->cv_tq, "call tq", CV_DEFAULT, 0);
2342 /* Initialize once-only items */
2343 queue_Init(&call->tq);
2344 queue_Init(&call->rq);
2345 queue_Init(&call->iovq);
2346 #ifdef RXDEBUG_PACKET
2347 call->rqc = call->tqc = call->iovqc = 0;
2348 #endif /* RXDEBUG_PACKET */
2349 /* Bind the call to its connection structure (prereq for reset) */
2351 rxi_ResetCall(call, 1);
2353 call->channel = channel;
2354 call->callNumber = &conn->callNumber[channel];
2355 call->rwind = conn->rwind[channel];
2356 call->twind = conn->twind[channel];
2357 /* Note that the next expected call number is retained (in
2358 * conn->callNumber[i]), even if we reallocate the call structure
2360 conn->call[channel] = call;
2361 /* if the channel's never been used (== 0), we should start at 1, otherwise
2362 * the call number is valid from the last time this channel was used */
2363 if (*call->callNumber == 0)
2364 *call->callNumber = 1;
2369 /* A call has been inactive long enough that so we can throw away
2370 * state, including the call structure, which is placed on the call
2372 * Call is locked upon entry.
2373 * haveCTLock set if called from rxi_ReapConnections
2375 #ifdef RX_ENABLE_LOCKS
2377 rxi_FreeCall(struct rx_call *call, int haveCTLock)
2378 #else /* RX_ENABLE_LOCKS */
2380 rxi_FreeCall(struct rx_call *call)
2381 #endif /* RX_ENABLE_LOCKS */
2383 int channel = call->channel;
2384 struct rx_connection *conn = call->conn;
2387 if (call->state == RX_STATE_DALLY || call->state == RX_STATE_HOLD)
2388 (*call->callNumber)++;
2389 rxi_ResetCall(call, 0);
2390 call->conn->call[channel] = (struct rx_call *)0;
2392 MUTEX_ENTER(&rx_freeCallQueue_lock);
2393 SET_CALL_QUEUE_LOCK(call, &rx_freeCallQueue_lock);
2394 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2395 /* A call may be free even though its transmit queue is still in use.
2396 * Since we search the call list from head to tail, put busy calls at
2397 * the head of the list, and idle calls at the tail.
2399 if (call->flags & RX_CALL_TQ_BUSY)
2400 queue_Prepend(&rx_freeCallQueue, call);
2402 queue_Append(&rx_freeCallQueue, call);
2403 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2404 queue_Append(&rx_freeCallQueue, call);
2405 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2406 if (rx_stats_active)
2407 rx_MutexIncrement(rx_stats.nFreeCallStructs, rx_stats_mutex);
2408 MUTEX_EXIT(&rx_freeCallQueue_lock);
2410 /* Destroy the connection if it was previously slated for
2411 * destruction, i.e. the Rx client code previously called
2412 * rx_DestroyConnection (client connections), or
2413 * rxi_ReapConnections called the same routine (server
2414 * connections). Only do this, however, if there are no
2415 * outstanding calls. Note that for fine grain locking, there appears
2416 * to be a deadlock in that rxi_FreeCall has a call locked and
2417 * DestroyConnectionNoLock locks each call in the conn. But note a
2418 * few lines up where we have removed this call from the conn.
2419 * If someone else destroys a connection, they either have no
2420 * call lock held or are going through this section of code.
2422 MUTEX_ENTER(&conn->conn_data_lock);
2423 if (conn->flags & RX_CONN_DESTROY_ME && !(conn->flags & RX_CONN_MAKECALL_WAITING)) {
2425 MUTEX_EXIT(&conn->conn_data_lock);
2426 #ifdef RX_ENABLE_LOCKS
2428 rxi_DestroyConnectionNoLock(conn);
2430 rxi_DestroyConnection(conn);
2431 #else /* RX_ENABLE_LOCKS */
2432 rxi_DestroyConnection(conn);
2433 #endif /* RX_ENABLE_LOCKS */
2435 MUTEX_EXIT(&conn->conn_data_lock);
2439 afs_int32 rxi_Alloccnt = 0, rxi_Allocsize = 0;
2441 rxi_Alloc(size_t size)
2445 if (rx_stats_active)
2446 rx_MutexAdd1Increment2(rxi_Allocsize, (afs_int32)size, rxi_Alloccnt, rx_stats_mutex);
2449 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
2450 afs_osi_Alloc_NoSleep(size);
2455 osi_Panic("rxi_Alloc error");
2461 rxi_Free(void *addr, size_t size)
2463 if (rx_stats_active)
2464 rx_MutexAdd1Decrement2(rxi_Allocsize, -(afs_int32)size, rxi_Alloccnt, rx_stats_mutex);
2465 osi_Free(addr, size);
2469 rxi_SetPeerMtu(struct rx_peer *peer, afs_uint32 host, afs_uint32 port, int mtu)
2471 struct rx_peer **peer_ptr = NULL, **peer_end = NULL;
2472 struct rx_peer *next = NULL;
2476 MUTEX_ENTER(&rx_peerHashTable_lock);
2478 peer_ptr = &rx_peerHashTable[0];
2479 peer_end = &rx_peerHashTable[rx_hashTableSize];
2482 for ( ; peer_ptr < peer_end; peer_ptr++) {
2485 for ( ; peer; peer = next) {
2487 if (host == peer->host)
2492 hashIndex = PEER_HASH(host, port);
2493 for (peer = rx_peerHashTable[hashIndex]; peer; peer = peer->next) {
2494 if ((peer->host == host) && (peer->port == port))
2499 MUTEX_ENTER(&rx_peerHashTable_lock);
2504 MUTEX_EXIT(&rx_peerHashTable_lock);
2506 MUTEX_ENTER(&peer->peer_lock);
2507 /* We don't handle dropping below min, so don't */
2508 mtu = MAX(mtu, RX_MIN_PACKET_SIZE);
2509 peer->ifMTU=MIN(mtu, peer->ifMTU);
2510 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2511 /* if we tweaked this down, need to tune our peer MTU too */
2512 peer->MTU = MIN(peer->MTU, peer->natMTU);
2513 /* if we discovered a sub-1500 mtu, degrade */
2514 if (peer->ifMTU < OLD_MAX_PACKET_SIZE)
2515 peer->maxDgramPackets = 1;
2516 /* We no longer have valid peer packet information */
2517 if (peer->maxPacketSize-RX_IPUDP_SIZE > peer->ifMTU)
2518 peer->maxPacketSize = 0;
2519 MUTEX_EXIT(&peer->peer_lock);
2521 MUTEX_ENTER(&rx_peerHashTable_lock);
2523 if (host && !port) {
2525 /* pick up where we left off */
2529 MUTEX_EXIT(&rx_peerHashTable_lock);
2532 /* Find the peer process represented by the supplied (host,port)
2533 * combination. If there is no appropriate active peer structure, a
2534 * new one will be allocated and initialized
2535 * The origPeer, if set, is a pointer to a peer structure on which the
2536 * refcount will be be decremented. This is used to replace the peer
2537 * structure hanging off a connection structure */
2539 rxi_FindPeer(afs_uint32 host, u_short port,
2540 struct rx_peer *origPeer, int create)
2544 hashIndex = PEER_HASH(host, port);
2545 MUTEX_ENTER(&rx_peerHashTable_lock);
2546 for (pp = rx_peerHashTable[hashIndex]; pp; pp = pp->next) {
2547 if ((pp->host == host) && (pp->port == port))
2552 pp = rxi_AllocPeer(); /* This bzero's *pp */
2553 pp->host = host; /* set here or in InitPeerParams is zero */
2555 MUTEX_INIT(&pp->peer_lock, "peer_lock", MUTEX_DEFAULT, 0);
2556 queue_Init(&pp->congestionQueue);
2557 queue_Init(&pp->rpcStats);
2558 pp->next = rx_peerHashTable[hashIndex];
2559 rx_peerHashTable[hashIndex] = pp;
2560 rxi_InitPeerParams(pp);
2561 if (rx_stats_active)
2562 rx_MutexIncrement(rx_stats.nPeerStructs, rx_stats_mutex);
2569 origPeer->refCount--;
2570 MUTEX_EXIT(&rx_peerHashTable_lock);
2575 /* Find the connection at (host, port) started at epoch, and with the
2576 * given connection id. Creates the server connection if necessary.
2577 * The type specifies whether a client connection or a server
2578 * connection is desired. In both cases, (host, port) specify the
2579 * peer's (host, pair) pair. Client connections are not made
2580 * automatically by this routine. The parameter socket gives the
2581 * socket descriptor on which the packet was received. This is used,
2582 * in the case of server connections, to check that *new* connections
2583 * come via a valid (port, serviceId). Finally, the securityIndex
2584 * parameter must match the existing index for the connection. If a
2585 * server connection is created, it will be created using the supplied
2586 * index, if the index is valid for this service */
2587 struct rx_connection *
2588 rxi_FindConnection(osi_socket socket, afs_uint32 host,
2589 u_short port, u_short serviceId, afs_uint32 cid,
2590 afs_uint32 epoch, int type, u_int securityIndex)
2592 int hashindex, flag, i;
2593 struct rx_connection *conn;
2594 hashindex = CONN_HASH(host, port, cid, epoch, type);
2595 MUTEX_ENTER(&rx_connHashTable_lock);
2596 rxLastConn ? (conn = rxLastConn, flag = 0) : (conn =
2597 rx_connHashTable[hashindex],
2600 if ((conn->type == type) && ((cid & RX_CIDMASK) == conn->cid)
2601 && (epoch == conn->epoch)) {
2602 struct rx_peer *pp = conn->peer;
2603 if (securityIndex != conn->securityIndex) {
2604 /* this isn't supposed to happen, but someone could forge a packet
2605 * like this, and there seems to be some CM bug that makes this
2606 * happen from time to time -- in which case, the fileserver
2608 MUTEX_EXIT(&rx_connHashTable_lock);
2609 return (struct rx_connection *)0;
2611 if (pp->host == host && pp->port == port)
2613 if (type == RX_CLIENT_CONNECTION && pp->port == port)
2615 /* So what happens when it's a callback connection? */
2616 if ( /*type == RX_CLIENT_CONNECTION && */
2617 (conn->epoch & 0x80000000))
2621 /* the connection rxLastConn that was used the last time is not the
2622 ** one we are looking for now. Hence, start searching in the hash */
2624 conn = rx_connHashTable[hashindex];
2629 struct rx_service *service;
2630 if (type == RX_CLIENT_CONNECTION) {
2631 MUTEX_EXIT(&rx_connHashTable_lock);
2632 return (struct rx_connection *)0;
2634 service = rxi_FindService(socket, serviceId);
2635 if (!service || (securityIndex >= service->nSecurityObjects)
2636 || (service->securityObjects[securityIndex] == 0)) {
2637 MUTEX_EXIT(&rx_connHashTable_lock);
2638 return (struct rx_connection *)0;
2640 conn = rxi_AllocConnection(); /* This bzero's the connection */
2641 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
2642 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
2643 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
2644 conn->next = rx_connHashTable[hashindex];
2645 rx_connHashTable[hashindex] = conn;
2646 conn->peer = rxi_FindPeer(host, port, 0, 1);
2647 conn->type = RX_SERVER_CONNECTION;
2648 conn->lastSendTime = clock_Sec(); /* don't GC immediately */
2649 conn->epoch = epoch;
2650 conn->cid = cid & RX_CIDMASK;
2651 /* conn->serial = conn->lastSerial = 0; */
2652 /* conn->timeout = 0; */
2653 conn->ackRate = RX_FAST_ACK_RATE;
2654 conn->service = service;
2655 conn->serviceId = serviceId;
2656 conn->securityIndex = securityIndex;
2657 conn->securityObject = service->securityObjects[securityIndex];
2658 conn->nSpecific = 0;
2659 conn->specific = NULL;
2660 rx_SetConnDeadTime(conn, service->connDeadTime);
2661 rx_SetConnIdleDeadTime(conn, service->idleDeadTime);
2662 rx_SetServerConnIdleDeadErr(conn, service->idleDeadErr);
2663 for (i = 0; i < RX_MAXCALLS; i++) {
2664 conn->twind[i] = rx_initSendWindow;
2665 conn->rwind[i] = rx_initReceiveWindow;
2667 /* Notify security object of the new connection */
2668 RXS_NewConnection(conn->securityObject, conn);
2669 /* XXXX Connection timeout? */
2670 if (service->newConnProc)
2671 (*service->newConnProc) (conn);
2672 if (rx_stats_active)
2673 rx_MutexIncrement(rx_stats.nServerConns, rx_stats_mutex);
2676 MUTEX_ENTER(&conn->conn_data_lock);
2678 MUTEX_EXIT(&conn->conn_data_lock);
2680 rxLastConn = conn; /* store this connection as the last conn used */
2681 MUTEX_EXIT(&rx_connHashTable_lock);
2685 /* There are two packet tracing routines available for testing and monitoring
2686 * Rx. One is called just after every packet is received and the other is
2687 * called just before every packet is sent. Received packets, have had their
2688 * headers decoded, and packets to be sent have not yet had their headers
2689 * encoded. Both take two parameters: a pointer to the packet and a sockaddr
2690 * containing the network address. Both can be modified. The return value, if
2691 * non-zero, indicates that the packet should be dropped. */
2693 int (*rx_justReceived) (struct rx_packet *, struct sockaddr_in *) = 0;
2694 int (*rx_almostSent) (struct rx_packet *, struct sockaddr_in *) = 0;
2696 /* A packet has been received off the interface. Np is the packet, socket is
2697 * the socket number it was received from (useful in determining which service
2698 * this packet corresponds to), and (host, port) reflect the host,port of the
2699 * sender. This call returns the packet to the caller if it is finished with
2700 * it, rather than de-allocating it, just as a small performance hack */
2703 rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
2704 afs_uint32 host, u_short port, int *tnop,
2705 struct rx_call **newcallp)
2707 struct rx_call *call;
2708 struct rx_connection *conn;
2710 afs_uint32 currentCallNumber;
2716 struct rx_packet *tnp;
2719 /* We don't print out the packet until now because (1) the time may not be
2720 * accurate enough until now in the lwp implementation (rx_Listener only gets
2721 * the time after the packet is read) and (2) from a protocol point of view,
2722 * this is the first time the packet has been seen */
2723 packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
2724 ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
2725 dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %"AFS_PTR_FMT,
2726 np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
2727 np->header.epoch, np->header.cid, np->header.callNumber,
2728 np->header.seq, np->header.flags, np));
2731 if (np->header.type == RX_PACKET_TYPE_VERSION) {
2732 return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
2735 if (np->header.type == RX_PACKET_TYPE_DEBUG) {
2736 return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
2739 /* If an input tracer function is defined, call it with the packet and
2740 * network address. Note this function may modify its arguments. */
2741 if (rx_justReceived) {
2742 struct sockaddr_in addr;
2744 addr.sin_family = AF_INET;
2745 addr.sin_port = port;
2746 addr.sin_addr.s_addr = host;
2747 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
2748 addr.sin_len = sizeof(addr);
2749 #endif /* AFS_OSF_ENV */
2750 drop = (*rx_justReceived) (np, &addr);
2751 /* drop packet if return value is non-zero */
2754 port = addr.sin_port; /* in case fcn changed addr */
2755 host = addr.sin_addr.s_addr;
2759 /* If packet was not sent by the client, then *we* must be the client */
2760 type = ((np->header.flags & RX_CLIENT_INITIATED) != RX_CLIENT_INITIATED)
2761 ? RX_CLIENT_CONNECTION : RX_SERVER_CONNECTION;
2763 /* Find the connection (or fabricate one, if we're the server & if
2764 * necessary) associated with this packet */
2766 rxi_FindConnection(socket, host, port, np->header.serviceId,
2767 np->header.cid, np->header.epoch, type,
2768 np->header.securityIndex);
2771 /* If no connection found or fabricated, just ignore the packet.
2772 * (An argument could be made for sending an abort packet for
2777 MUTEX_ENTER(&conn->conn_data_lock);
2778 if (conn->maxSerial < np->header.serial)
2779 conn->maxSerial = np->header.serial;
2780 MUTEX_EXIT(&conn->conn_data_lock);
2782 /* If the connection is in an error state, send an abort packet and ignore
2783 * the incoming packet */
2785 /* Don't respond to an abort packet--we don't want loops! */
2786 MUTEX_ENTER(&conn->conn_data_lock);
2787 if (np->header.type != RX_PACKET_TYPE_ABORT)
2788 np = rxi_SendConnectionAbort(conn, np, 1, 0);
2790 MUTEX_EXIT(&conn->conn_data_lock);
2794 /* Check for connection-only requests (i.e. not call specific). */
2795 if (np->header.callNumber == 0) {
2796 switch (np->header.type) {
2797 case RX_PACKET_TYPE_ABORT: {
2798 /* What if the supplied error is zero? */
2799 afs_int32 errcode = ntohl(rx_GetInt32(np, 0));
2800 dpf(("rxi_ReceivePacket ABORT rx_GetInt32 = %d", errcode));
2801 rxi_ConnectionError(conn, errcode);
2802 MUTEX_ENTER(&conn->conn_data_lock);
2804 MUTEX_EXIT(&conn->conn_data_lock);
2807 case RX_PACKET_TYPE_CHALLENGE:
2808 tnp = rxi_ReceiveChallengePacket(conn, np, 1);
2809 MUTEX_ENTER(&conn->conn_data_lock);
2811 MUTEX_EXIT(&conn->conn_data_lock);
2813 case RX_PACKET_TYPE_RESPONSE:
2814 tnp = rxi_ReceiveResponsePacket(conn, np, 1);
2815 MUTEX_ENTER(&conn->conn_data_lock);
2817 MUTEX_EXIT(&conn->conn_data_lock);
2819 case RX_PACKET_TYPE_PARAMS:
2820 case RX_PACKET_TYPE_PARAMS + 1:
2821 case RX_PACKET_TYPE_PARAMS + 2:
2822 /* ignore these packet types for now */
2823 MUTEX_ENTER(&conn->conn_data_lock);
2825 MUTEX_EXIT(&conn->conn_data_lock);
2830 /* Should not reach here, unless the peer is broken: send an
2832 rxi_ConnectionError(conn, RX_PROTOCOL_ERROR);
2833 MUTEX_ENTER(&conn->conn_data_lock);
2834 tnp = rxi_SendConnectionAbort(conn, np, 1, 0);
2836 MUTEX_EXIT(&conn->conn_data_lock);
2841 channel = np->header.cid & RX_CHANNELMASK;
2842 call = conn->call[channel];
2843 #ifdef RX_ENABLE_LOCKS
2845 MUTEX_ENTER(&call->lock);
2846 /* Test to see if call struct is still attached to conn. */
2847 if (call != conn->call[channel]) {
2849 MUTEX_EXIT(&call->lock);
2850 if (type == RX_SERVER_CONNECTION) {
2851 call = conn->call[channel];
2852 /* If we started with no call attached and there is one now,
2853 * another thread is also running this routine and has gotten
2854 * the connection channel. We should drop this packet in the tests
2855 * below. If there was a call on this connection and it's now
2856 * gone, then we'll be making a new call below.
2857 * If there was previously a call and it's now different then
2858 * the old call was freed and another thread running this routine
2859 * has created a call on this channel. One of these two threads
2860 * has a packet for the old call and the code below handles those
2864 MUTEX_ENTER(&call->lock);
2866 /* This packet can't be for this call. If the new call address is
2867 * 0 then no call is running on this channel. If there is a call
2868 * then, since this is a client connection we're getting data for
2869 * it must be for the previous call.
2871 if (rx_stats_active)
2872 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2873 MUTEX_ENTER(&conn->conn_data_lock);
2875 MUTEX_EXIT(&conn->conn_data_lock);
2880 currentCallNumber = conn->callNumber[channel];
2882 if (type == RX_SERVER_CONNECTION) { /* We're the server */
2883 if (np->header.callNumber < currentCallNumber) {
2884 if (rx_stats_active)
2885 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2886 #ifdef RX_ENABLE_LOCKS
2888 MUTEX_EXIT(&call->lock);
2890 MUTEX_ENTER(&conn->conn_data_lock);
2892 MUTEX_EXIT(&conn->conn_data_lock);
2896 MUTEX_ENTER(&conn->conn_call_lock);
2897 call = rxi_NewCall(conn, channel);
2898 MUTEX_EXIT(&conn->conn_call_lock);
2899 *call->callNumber = np->header.callNumber;
2901 if (np->header.callNumber == 0)
2902 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%.06d len %d",
2903 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
2904 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
2905 np->header.flags, np, np->retryTime.sec, np->retryTime.usec / 1000, np->length));
2907 call->state = RX_STATE_PRECALL;
2908 clock_GetTime(&call->queueTime);
2909 hzero(call->bytesSent);
2910 hzero(call->bytesRcvd);
2912 * If the number of queued calls exceeds the overload
2913 * threshold then abort this call.
2915 if ((rx_BusyThreshold > 0) && (rx_nWaiting > rx_BusyThreshold)) {
2916 struct rx_packet *tp;
2918 rxi_CallError(call, rx_BusyError);
2919 tp = rxi_SendCallAbort(call, np, 1, 0);
2920 MUTEX_EXIT(&call->lock);
2921 MUTEX_ENTER(&conn->conn_data_lock);
2923 MUTEX_EXIT(&conn->conn_data_lock);
2924 if (rx_stats_active)
2925 rx_MutexIncrement(rx_stats.nBusies, rx_stats_mutex);
2928 rxi_KeepAliveOn(call);
2929 } else if (np->header.callNumber != currentCallNumber) {
2930 /* Wait until the transmit queue is idle before deciding
2931 * whether to reset the current call. Chances are that the
2932 * call will be in ether DALLY or HOLD state once the TQ_BUSY
2935 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2936 while ((call->state == RX_STATE_ACTIVE)
2937 && (call->flags & RX_CALL_TQ_BUSY)) {
2938 call->flags |= RX_CALL_TQ_WAIT;
2940 #ifdef RX_ENABLE_LOCKS
2941 osirx_AssertMine(&call->lock, "rxi_Start lock3");
2942 CV_WAIT(&call->cv_tq, &call->lock);
2943 #else /* RX_ENABLE_LOCKS */
2944 osi_rxSleep(&call->tq);
2945 #endif /* RX_ENABLE_LOCKS */
2947 if (call->tqWaiters == 0)
2948 call->flags &= ~RX_CALL_TQ_WAIT;
2950 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2951 /* If the new call cannot be taken right now send a busy and set
2952 * the error condition in this call, so that it terminates as
2953 * quickly as possible */
2954 if (call->state == RX_STATE_ACTIVE) {
2955 struct rx_packet *tp;
2957 rxi_CallError(call, RX_CALL_DEAD);
2958 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
2960 MUTEX_EXIT(&call->lock);
2961 MUTEX_ENTER(&conn->conn_data_lock);
2963 MUTEX_EXIT(&conn->conn_data_lock);
2966 rxi_ResetCall(call, 0);
2967 *call->callNumber = np->header.callNumber;
2969 if (np->header.callNumber == 0)
2970 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%06d len %d",
2971 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
2972 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
2973 np->header.flags, np, np->retryTime.sec, np->retryTime.usec, np->length));
2975 call->state = RX_STATE_PRECALL;
2976 clock_GetTime(&call->queueTime);
2977 hzero(call->bytesSent);
2978 hzero(call->bytesRcvd);
2980 * If the number of queued calls exceeds the overload
2981 * threshold then abort this call.
2983 if ((rx_BusyThreshold > 0) && (rx_nWaiting > rx_BusyThreshold)) {
2984 struct rx_packet *tp;
2986 rxi_CallError(call, rx_BusyError);
2987 tp = rxi_SendCallAbort(call, np, 1, 0);
2988 MUTEX_EXIT(&call->lock);
2989 MUTEX_ENTER(&conn->conn_data_lock);
2991 MUTEX_EXIT(&conn->conn_data_lock);
2992 if (rx_stats_active)
2993 rx_MutexIncrement(rx_stats.nBusies, rx_stats_mutex);
2996 rxi_KeepAliveOn(call);
2998 /* Continuing call; do nothing here. */
3000 } else { /* we're the client */
3001 /* Ignore all incoming acknowledgements for calls in DALLY state */
3002 if (call && (call->state == RX_STATE_DALLY)
3003 && (np->header.type == RX_PACKET_TYPE_ACK)) {
3004 if (rx_stats_active)
3005 rx_MutexIncrement(rx_stats.ignorePacketDally, rx_stats_mutex);
3006 #ifdef RX_ENABLE_LOCKS
3008 MUTEX_EXIT(&call->lock);
3011 MUTEX_ENTER(&conn->conn_data_lock);
3013 MUTEX_EXIT(&conn->conn_data_lock);
3017 /* Ignore anything that's not relevant to the current call. If there
3018 * isn't a current call, then no packet is relevant. */
3019 if (!call || (np->header.callNumber != currentCallNumber)) {
3020 if (rx_stats_active)
3021 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
3022 #ifdef RX_ENABLE_LOCKS
3024 MUTEX_EXIT(&call->lock);
3027 MUTEX_ENTER(&conn->conn_data_lock);
3029 MUTEX_EXIT(&conn->conn_data_lock);
3032 /* If the service security object index stamped in the packet does not
3033 * match the connection's security index, ignore the packet */
3034 if (np->header.securityIndex != conn->securityIndex) {
3035 #ifdef RX_ENABLE_LOCKS
3036 MUTEX_EXIT(&call->lock);
3038 MUTEX_ENTER(&conn->conn_data_lock);
3040 MUTEX_EXIT(&conn->conn_data_lock);
3044 /* If we're receiving the response, then all transmit packets are
3045 * implicitly acknowledged. Get rid of them. */
3046 if (np->header.type == RX_PACKET_TYPE_DATA) {
3047 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3048 /* XXX Hack. Because we must release the global rx lock when
3049 * sending packets (osi_NetSend) we drop all acks while we're
3050 * traversing the tq in rxi_Start sending packets out because
3051 * packets may move to the freePacketQueue as result of being here!
3052 * So we drop these packets until we're safely out of the
3053 * traversing. Really ugly!
3054 * For fine grain RX locking, we set the acked field in the
3055 * packets and let rxi_Start remove them from the transmit queue.
3057 if (call->flags & RX_CALL_TQ_BUSY) {
3058 #ifdef RX_ENABLE_LOCKS
3059 rxi_SetAcksInTransmitQueue(call);
3062 return np; /* xmitting; drop packet */
3065 rxi_ClearTransmitQueue(call, 0);
3067 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
3068 rxi_ClearTransmitQueue(call, 0);
3069 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3071 if (np->header.type == RX_PACKET_TYPE_ACK) {
3072 /* now check to see if this is an ack packet acknowledging that the
3073 * server actually *lost* some hard-acked data. If this happens we
3074 * ignore this packet, as it may indicate that the server restarted in
3075 * the middle of a call. It is also possible that this is an old ack
3076 * packet. We don't abort the connection in this case, because this
3077 * *might* just be an old ack packet. The right way to detect a server
3078 * restart in the midst of a call is to notice that the server epoch
3080 /* XXX I'm not sure this is exactly right, since tfirst **IS**
3081 * XXX unacknowledged. I think that this is off-by-one, but
3082 * XXX I don't dare change it just yet, since it will
3083 * XXX interact badly with the server-restart detection
3084 * XXX code in receiveackpacket. */
3085 if (ntohl(rx_GetInt32(np, FIRSTACKOFFSET)) < call->tfirst) {
3086 if (rx_stats_active)
3087 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
3088 MUTEX_EXIT(&call->lock);
3089 MUTEX_ENTER(&conn->conn_data_lock);
3091 MUTEX_EXIT(&conn->conn_data_lock);
3095 } /* else not a data packet */
3098 osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
3099 /* Set remote user defined status from packet */
3100 call->remoteStatus = np->header.userStatus;
3102 /* Note the gap between the expected next packet and the actual
3103 * packet that arrived, when the new packet has a smaller serial number
3104 * than expected. Rioses frequently reorder packets all by themselves,
3105 * so this will be quite important with very large window sizes.
3106 * Skew is checked against 0 here to avoid any dependence on the type of
3107 * inPacketSkew (which may be unsigned). In C, -1 > (unsigned) 0 is always
3109 * The inPacketSkew should be a smoothed running value, not just a maximum. MTUXXX
3110 * see CalculateRoundTripTime for an example of how to keep smoothed values.
3111 * I think using a beta of 1/8 is probably appropriate. 93.04.21
3113 MUTEX_ENTER(&conn->conn_data_lock);
3114 skew = conn->lastSerial - np->header.serial;
3115 conn->lastSerial = np->header.serial;
3116 MUTEX_EXIT(&conn->conn_data_lock);
3118 struct rx_peer *peer;
3120 if (skew > peer->inPacketSkew) {
3121 dpf(("*** In skew changed from %d to %d\n",
3122 peer->inPacketSkew, skew));
3123 peer->inPacketSkew = skew;
3127 /* Now do packet type-specific processing */
3128 switch (np->header.type) {
3129 case RX_PACKET_TYPE_DATA:
3130 np = rxi_ReceiveDataPacket(call, np, 1, socket, host, port, tnop,
3133 case RX_PACKET_TYPE_ACK:
3134 /* Respond immediately to ack packets requesting acknowledgement
3136 if (np->header.flags & RX_REQUEST_ACK) {
3138 (void)rxi_SendCallAbort(call, 0, 1, 0);
3140 (void)rxi_SendAck(call, 0, np->header.serial,
3141 RX_ACK_PING_RESPONSE, 1);
3143 np = rxi_ReceiveAckPacket(call, np, 1);
3145 case RX_PACKET_TYPE_ABORT: {
3146 /* An abort packet: reset the call, passing the error up to the user. */
3147 /* What if error is zero? */
3148 /* What if the error is -1? the application will treat it as a timeout. */
3149 afs_int32 errdata = ntohl(*(afs_int32 *) rx_DataOf(np));
3150 dpf(("rxi_ReceivePacket ABORT rx_DataOf = %d", errdata));
3151 rxi_CallError(call, errdata);
3152 MUTEX_EXIT(&call->lock);
3153 MUTEX_ENTER(&conn->conn_data_lock);
3155 MUTEX_EXIT(&conn->conn_data_lock);
3156 return np; /* xmitting; drop packet */
3158 case RX_PACKET_TYPE_BUSY:
3161 case RX_PACKET_TYPE_ACKALL:
3162 /* All packets acknowledged, so we can drop all packets previously
3163 * readied for sending */
3164 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3165 /* XXX Hack. We because we can't release the global rx lock when
3166 * sending packets (osi_NetSend) we drop all ack pkts while we're
3167 * traversing the tq in rxi_Start sending packets out because
3168 * packets may move to the freePacketQueue as result of being
3169 * here! So we drop these packets until we're safely out of the
3170 * traversing. Really ugly!
3171 * For fine grain RX locking, we set the acked field in the packets
3172 * and let rxi_Start remove the packets from the transmit queue.
3174 if (call->flags & RX_CALL_TQ_BUSY) {
3175 #ifdef RX_ENABLE_LOCKS
3176 rxi_SetAcksInTransmitQueue(call);
3178 #else /* RX_ENABLE_LOCKS */
3179 MUTEX_EXIT(&call->lock);
3180 MUTEX_ENTER(&conn->conn_data_lock);
3182 MUTEX_EXIT(&conn->conn_data_lock);
3183 return np; /* xmitting; drop packet */
3184 #endif /* RX_ENABLE_LOCKS */
3186 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3187 rxi_ClearTransmitQueue(call, 0);
3188 rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
3191 /* Should not reach here, unless the peer is broken: send an abort
3193 rxi_CallError(call, RX_PROTOCOL_ERROR);
3194 np = rxi_SendCallAbort(call, np, 1, 0);
3197 /* Note when this last legitimate packet was received, for keep-alive
3198 * processing. Note, we delay getting the time until now in the hope that
3199 * the packet will be delivered to the user before any get time is required
3200 * (if not, then the time won't actually be re-evaluated here). */
3201 call->lastReceiveTime = clock_Sec();
3202 MUTEX_EXIT(&call->lock);
3203 MUTEX_ENTER(&conn->conn_data_lock);
3205 MUTEX_EXIT(&conn->conn_data_lock);
3209 /* return true if this is an "interesting" connection from the point of view
3210 of someone trying to debug the system */
3212 rxi_IsConnInteresting(struct rx_connection *aconn)
3215 struct rx_call *tcall;
3217 if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
3220 for (i = 0; i < RX_MAXCALLS; i++) {
3221 tcall = aconn->call[i];
3223 if ((tcall->state == RX_STATE_PRECALL)
3224 || (tcall->state == RX_STATE_ACTIVE))
3226 if ((tcall->mode == RX_MODE_SENDING)
3227 || (tcall->mode == RX_MODE_RECEIVING))
3235 /* if this is one of the last few packets AND it wouldn't be used by the
3236 receiving call to immediately satisfy a read request, then drop it on
3237 the floor, since accepting it might prevent a lock-holding thread from
3238 making progress in its reading. If a call has been cleared while in
3239 the precall state then ignore all subsequent packets until the call
3240 is assigned to a thread. */
3243 TooLow(struct rx_packet *ap, struct rx_call *acall)
3247 MUTEX_ENTER(&rx_quota_mutex);
3248 if (((ap->header.seq != 1) && (acall->flags & RX_CALL_CLEARED)
3249 && (acall->state == RX_STATE_PRECALL))
3250 || ((rx_nFreePackets < rxi_dataQuota + 2)
3251 && !((ap->header.seq < acall->rnext + rx_initSendWindow)
3252 && (acall->flags & RX_CALL_READER_WAIT)))) {
3255 MUTEX_EXIT(&rx_quota_mutex);
3261 rxi_CheckReachEvent(struct rxevent *event, void *arg1, void *arg2)
3263 struct rx_connection *conn = arg1;
3264 struct rx_call *acall = arg2;
3265 struct rx_call *call = acall;
3266 struct clock when, now;
3269 MUTEX_ENTER(&conn->conn_data_lock);
3270 conn->checkReachEvent = NULL;
3271 waiting = conn->flags & RX_CONN_ATTACHWAIT;
3274 MUTEX_EXIT(&conn->conn_data_lock);
3278 MUTEX_ENTER(&conn->conn_call_lock);
3279 MUTEX_ENTER(&conn->conn_data_lock);
3280 for (i = 0; i < RX_MAXCALLS; i++) {
3281 struct rx_call *tc = conn->call[i];
3282 if (tc && tc->state == RX_STATE_PRECALL) {
3288 /* Indicate that rxi_CheckReachEvent is no longer running by
3289 * clearing the flag. Must be atomic under conn_data_lock to
3290 * avoid a new call slipping by: rxi_CheckConnReach holds
3291 * conn_data_lock while checking RX_CONN_ATTACHWAIT.
3293 conn->flags &= ~RX_CONN_ATTACHWAIT;
3294 MUTEX_EXIT(&conn->conn_data_lock);
3295 MUTEX_EXIT(&conn->conn_call_lock);
3300 MUTEX_ENTER(&call->lock);
3301 rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3303 MUTEX_EXIT(&call->lock);
3305 clock_GetTime(&now);
3307 when.sec += RX_CHECKREACH_TIMEOUT;
3308 MUTEX_ENTER(&conn->conn_data_lock);
3309 if (!conn->checkReachEvent) {
3311 conn->checkReachEvent =
3312 rxevent_PostNow(&when, &now, rxi_CheckReachEvent, conn,
3315 MUTEX_EXIT(&conn->conn_data_lock);
3321 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3323 struct rx_service *service = conn->service;
3324 struct rx_peer *peer = conn->peer;
3325 afs_uint32 now, lastReach;
3327 if (service->checkReach == 0)
3331 MUTEX_ENTER(&peer->peer_lock);
3332 lastReach = peer->lastReachTime;
3333 MUTEX_EXIT(&peer->peer_lock);
3334 if (now - lastReach < RX_CHECKREACH_TTL)
3337 MUTEX_ENTER(&conn->conn_data_lock);
3338 if (conn->flags & RX_CONN_ATTACHWAIT) {
3339 MUTEX_EXIT(&conn->conn_data_lock);
3342 conn->flags |= RX_CONN_ATTACHWAIT;
3343 MUTEX_EXIT(&conn->conn_data_lock);
3344 if (!conn->checkReachEvent)
3345 rxi_CheckReachEvent(NULL, conn, call);
3350 /* try to attach call, if authentication is complete */
3352 TryAttach(struct rx_call *acall, osi_socket socket,
3353 int *tnop, struct rx_call **newcallp,
3356 struct rx_connection *conn = acall->conn;
3358 if (conn->type == RX_SERVER_CONNECTION
3359 && acall->state == RX_STATE_PRECALL) {
3360 /* Don't attach until we have any req'd. authentication. */
3361 if (RXS_CheckAuthentication(conn->securityObject, conn) == 0) {
3362 if (reachOverride || rxi_CheckConnReach(conn, acall) == 0)
3363 rxi_AttachServerProc(acall, socket, tnop, newcallp);
3364 /* Note: this does not necessarily succeed; there
3365 * may not any proc available
3368 rxi_ChallengeOn(acall->conn);
3373 /* A data packet has been received off the interface. This packet is
3374 * appropriate to the call (the call is in the right state, etc.). This
3375 * routine can return a packet to the caller, for re-use */
3378 rxi_ReceiveDataPacket(struct rx_call *call,
3379 struct rx_packet *np, int istack,
3380 osi_socket socket, afs_uint32 host, u_short port,
3381 int *tnop, struct rx_call **newcallp)
3383 int ackNeeded = 0; /* 0 means no, otherwise ack_reason */
3388 afs_uint32 serial=0, flags=0;
3390 struct rx_packet *tnp;
3391 struct clock when, now;
3392 if (rx_stats_active)
3393 rx_MutexIncrement(rx_stats.dataPacketsRead, rx_stats_mutex);
3396 /* If there are no packet buffers, drop this new packet, unless we can find
3397 * packet buffers from inactive calls */
3399 && (rxi_OverQuota(RX_PACKET_CLASS_RECEIVE) || TooLow(np, call))) {
3400 MUTEX_ENTER(&rx_freePktQ_lock);
3401 rxi_NeedMorePackets = TRUE;
3402 MUTEX_EXIT(&rx_freePktQ_lock);
3403 if (rx_stats_active)
3404 rx_MutexIncrement(rx_stats.noPacketBuffersOnRead, rx_stats_mutex);
3405 call->rprev = np->header.serial;
3406 rxi_calltrace(RX_TRACE_DROP, call);
3407 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - quota problems", np));
3409 rxi_ClearReceiveQueue(call);
3410 clock_GetTime(&now);
3412 clock_Add(&when, &rx_softAckDelay);
3413 if (!call->delayedAckEvent
3414 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3415 rxevent_Cancel(call->delayedAckEvent, call,
3416 RX_CALL_REFCOUNT_DELAY);
3417 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3418 call->delayedAckEvent =
3419 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3421 /* we've damaged this call already, might as well do it in. */
3427 * New in AFS 3.5, if the RX_JUMBO_PACKET flag is set then this
3428 * packet is one of several packets transmitted as a single
3429 * datagram. Do not send any soft or hard acks until all packets
3430 * in a jumbogram have been processed. Send negative acks right away.
3432 for (isFirst = 1, tnp = NULL; isFirst || tnp; isFirst = 0) {
3433 /* tnp is non-null when there are more packets in the
3434 * current jumbo gram */
3441 seq = np->header.seq;
3442 serial = np->header.serial;
3443 flags = np->header.flags;
3445 /* If the call is in an error state, send an abort message */
3447 return rxi_SendCallAbort(call, np, istack, 0);
3449 /* The RX_JUMBO_PACKET is set in all but the last packet in each
3450 * AFS 3.5 jumbogram. */
3451 if (flags & RX_JUMBO_PACKET) {
3452 tnp = rxi_SplitJumboPacket(np, host, port, isFirst);
3457 if (np->header.spare != 0) {
3458 MUTEX_ENTER(&call->conn->conn_data_lock);
3459 call->conn->flags |= RX_CONN_USING_PACKET_CKSUM;
3460 MUTEX_EXIT(&call->conn->conn_data_lock);
3463 /* The usual case is that this is the expected next packet */
3464 if (seq == call->rnext) {
3466 /* Check to make sure it is not a duplicate of one already queued */
3467 if (queue_IsNotEmpty(&call->rq)
3468 && queue_First(&call->rq, rx_packet)->header.seq == seq) {
3469 if (rx_stats_active)
3470 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3471 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - duplicate", np));
3472 rxevent_Cancel(call->delayedAckEvent, call,
3473 RX_CALL_REFCOUNT_DELAY);
3474 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3480 /* It's the next packet. Stick it on the receive queue
3481 * for this call. Set newPackets to make sure we wake
3482 * the reader once all packets have been processed */
3483 #ifdef RX_TRACK_PACKETS
3484 np->flags |= RX_PKTFLAG_RQ;
3486 queue_Prepend(&call->rq, np);
3487 #ifdef RXDEBUG_PACKET
3489 #endif /* RXDEBUG_PACKET */
3491 np = NULL; /* We can't use this anymore */
3494 /* If an ack is requested then set a flag to make sure we
3495 * send an acknowledgement for this packet */
3496 if (flags & RX_REQUEST_ACK) {
3497 ackNeeded = RX_ACK_REQUESTED;
3500 /* Keep track of whether we have received the last packet */
3501 if (flags & RX_LAST_PACKET) {
3502 call->flags |= RX_CALL_HAVE_LAST;
3506 /* Check whether we have all of the packets for this call */
3507 if (call->flags & RX_CALL_HAVE_LAST) {
3508 afs_uint32 tseq; /* temporary sequence number */
3509 struct rx_packet *tp; /* Temporary packet pointer */
3510 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3512 for (tseq = seq, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3513 if (tseq != tp->header.seq)
3515 if (tp->header.flags & RX_LAST_PACKET) {
3516 call->flags |= RX_CALL_RECEIVE_DONE;
3523 /* Provide asynchronous notification for those who want it
3524 * (e.g. multi rx) */
3525 if (call->arrivalProc) {
3526 (*call->arrivalProc) (call, call->arrivalProcHandle,
3527 call->arrivalProcArg);
3528 call->arrivalProc = (void (*)())0;
3531 /* Update last packet received */
3534 /* If there is no server process serving this call, grab
3535 * one, if available. We only need to do this once. If a
3536 * server thread is available, this thread becomes a server
3537 * thread and the server thread becomes a listener thread. */
3539 TryAttach(call, socket, tnop, newcallp, 0);
3542 /* This is not the expected next packet. */
3544 /* Determine whether this is a new or old packet, and if it's
3545 * a new one, whether it fits into the current receive window.
3546 * Also figure out whether the packet was delivered in sequence.
3547 * We use the prev variable to determine whether the new packet
3548 * is the successor of its immediate predecessor in the
3549 * receive queue, and the missing flag to determine whether
3550 * any of this packets predecessors are missing. */
3552 afs_uint32 prev; /* "Previous packet" sequence number */
3553 struct rx_packet *tp; /* Temporary packet pointer */
3554 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3555 int missing; /* Are any predecessors missing? */
3557 /* If the new packet's sequence number has been sent to the
3558 * application already, then this is a duplicate */
3559 if (seq < call->rnext) {
3560 if (rx_stats_active)
3561 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3562 rxevent_Cancel(call->delayedAckEvent, call,
3563 RX_CALL_REFCOUNT_DELAY);
3564 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3570 /* If the sequence number is greater than what can be
3571 * accomodated by the current window, then send a negative
3572 * acknowledge and drop the packet */
3573 if ((call->rnext + call->rwind) <= seq) {
3574 rxevent_Cancel(call->delayedAckEvent, call,
3575 RX_CALL_REFCOUNT_DELAY);
3576 np = rxi_SendAck(call, np, serial, RX_ACK_EXCEEDS_WINDOW,
3583 /* Look for the packet in the queue of old received packets */
3584 for (prev = call->rnext - 1, missing =
3585 0, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3586 /*Check for duplicate packet */
3587 if (seq == tp->header.seq) {
3588 if (rx_stats_active)
3589 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3590 rxevent_Cancel(call->delayedAckEvent, call,
3591 RX_CALL_REFCOUNT_DELAY);
3592 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE,
3598 /* If we find a higher sequence packet, break out and
3599 * insert the new packet here. */
3600 if (seq < tp->header.seq)
3602 /* Check for missing packet */
3603 if (tp->header.seq != prev + 1) {
3607 prev = tp->header.seq;
3610 /* Keep track of whether we have received the last packet. */
3611 if (flags & RX_LAST_PACKET) {
3612 call->flags |= RX_CALL_HAVE_LAST;
3615 /* It's within the window: add it to the the receive queue.
3616 * tp is left by the previous loop either pointing at the
3617 * packet before which to insert the new packet, or at the
3618 * queue head if the queue is empty or the packet should be
3620 #ifdef RX_TRACK_PACKETS
3621 np->flags |= RX_PKTFLAG_RQ;
3623 #ifdef RXDEBUG_PACKET
3625 #endif /* RXDEBUG_PACKET */
3626 queue_InsertBefore(tp, np);
3630 /* Check whether we have all of the packets for this call */
3631 if ((call->flags & RX_CALL_HAVE_LAST)
3632 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
3633 afs_uint32 tseq; /* temporary sequence number */
3636 call->rnext, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3637 if (tseq != tp->header.seq)
3639 if (tp->header.flags & RX_LAST_PACKET) {
3640 call->flags |= RX_CALL_RECEIVE_DONE;
3647 /* We need to send an ack of the packet is out of sequence,
3648 * or if an ack was requested by the peer. */
3649 if (seq != prev + 1 || missing) {
3650 ackNeeded = RX_ACK_OUT_OF_SEQUENCE;
3651 } else if (flags & RX_REQUEST_ACK) {
3652 ackNeeded = RX_ACK_REQUESTED;
3655 /* Acknowledge the last packet for each call */
3656 if (flags & RX_LAST_PACKET) {
3667 * If the receiver is waiting for an iovec, fill the iovec
3668 * using the data from the receive queue */
3669 if (call->flags & RX_CALL_IOVEC_WAIT) {
3670 didHardAck = rxi_FillReadVec(call, serial);
3671 /* the call may have been aborted */
3680 /* Wakeup the reader if any */
3681 if ((call->flags & RX_CALL_READER_WAIT)
3682 && (!(call->flags & RX_CALL_IOVEC_WAIT) || !(call->iovNBytes)
3683 || (call->iovNext >= call->iovMax)
3684 || (call->flags & RX_CALL_RECEIVE_DONE))) {
3685 call->flags &= ~RX_CALL_READER_WAIT;