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"
60 #endif /* AFS_OSF_ENV */
62 #include "afs/sysincludes.h"
63 #include "afsincludes.h"
66 #include "rx_kmutex.h"
67 #include "rx_kernel.h"
71 #include "rx_globals.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 "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_globals.h"
109 # include "rx_trace.h"
110 # include <afs/rxgen_consts.h>
114 #ifdef AFS_PTHREAD_ENV
116 int (*registerProgram) (pid_t, char *) = 0;
117 int (*swapNameProgram) (pid_t, const char *, char *) = 0;
120 int (*registerProgram) (PROCESS, char *) = 0;
121 int (*swapNameProgram) (PROCESS, const char *, char *) = 0;
125 /* Local static routines */
126 static void rxi_DestroyConnectionNoLock(struct rx_connection *conn);
127 #ifdef RX_ENABLE_LOCKS
128 static void rxi_SetAcksInTransmitQueue(struct rx_call *call);
131 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
133 afs_int32 rxi_start_aborted; /* rxi_start awoke after rxi_Send in error. */
134 afs_int32 rxi_start_in_error;
136 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
139 * rxi_rpc_peer_stat_cnt counts the total number of peer stat structures
140 * currently allocated within rx. This number is used to allocate the
141 * memory required to return the statistics when queried.
144 static unsigned int rxi_rpc_peer_stat_cnt;
147 * rxi_rpc_process_stat_cnt counts the total number of local process stat
148 * structures currently allocated within rx. The number is used to allocate
149 * the memory required to return the statistics when queried.
152 static unsigned int rxi_rpc_process_stat_cnt;
154 #if !defined(offsetof)
155 #include <stddef.h> /* for definition of offsetof() */
158 #ifdef AFS_PTHREAD_ENV
162 * Use procedural initialization of mutexes/condition variables
166 extern afs_kmutex_t rx_stats_mutex;
167 extern afs_kmutex_t rx_waiting_mutex;
168 extern afs_kmutex_t rx_quota_mutex;
169 extern afs_kmutex_t rx_pthread_mutex;
170 extern afs_kmutex_t rx_packets_mutex;
171 extern afs_kmutex_t des_init_mutex;
172 extern afs_kmutex_t des_random_mutex;
173 extern afs_kmutex_t rx_clock_mutex;
174 extern afs_kmutex_t rxi_connCacheMutex;
175 extern afs_kmutex_t rx_event_mutex;
176 extern afs_kmutex_t osi_malloc_mutex;
177 extern afs_kmutex_t event_handler_mutex;
178 extern afs_kmutex_t listener_mutex;
179 extern afs_kmutex_t rx_if_init_mutex;
180 extern afs_kmutex_t rx_if_mutex;
181 extern afs_kmutex_t rxkad_client_uid_mutex;
182 extern afs_kmutex_t rxkad_random_mutex;
184 extern afs_kcondvar_t rx_event_handler_cond;
185 extern afs_kcondvar_t rx_listener_cond;
187 static afs_kmutex_t epoch_mutex;
188 static afs_kmutex_t rx_init_mutex;
189 static afs_kmutex_t rx_debug_mutex;
190 static afs_kmutex_t rx_rpc_stats;
193 rxi_InitPthread(void)
195 MUTEX_INIT(&rx_clock_mutex, "clock", MUTEX_DEFAULT, 0);
196 MUTEX_INIT(&rx_stats_mutex, "stats", MUTEX_DEFAULT, 0);
197 MUTEX_INIT(&rx_waiting_mutex, "waiting", MUTEX_DEFAULT, 0);
198 MUTEX_INIT(&rx_quota_mutex, "quota", MUTEX_DEFAULT, 0);
199 MUTEX_INIT(&rx_pthread_mutex, "pthread", MUTEX_DEFAULT, 0);
200 MUTEX_INIT(&rx_packets_mutex, "packets", MUTEX_DEFAULT, 0);
201 MUTEX_INIT(&epoch_mutex, "epoch", MUTEX_DEFAULT, 0);
202 MUTEX_INIT(&rx_init_mutex, "init", MUTEX_DEFAULT, 0);
203 MUTEX_INIT(&rx_event_mutex, "event", MUTEX_DEFAULT, 0);
204 MUTEX_INIT(&des_init_mutex, "des", MUTEX_DEFAULT, 0);
205 MUTEX_INIT(&des_random_mutex, "random", MUTEX_DEFAULT, 0);
206 MUTEX_INIT(&osi_malloc_mutex, "malloc", MUTEX_DEFAULT, 0);
207 MUTEX_INIT(&event_handler_mutex, "event handler", MUTEX_DEFAULT, 0);
208 MUTEX_INIT(&rxi_connCacheMutex, "conn cache", MUTEX_DEFAULT, 0);
209 MUTEX_INIT(&listener_mutex, "listener", MUTEX_DEFAULT, 0);
210 MUTEX_INIT(&rx_if_init_mutex, "if init", MUTEX_DEFAULT, 0);
211 MUTEX_INIT(&rx_if_mutex, "if", MUTEX_DEFAULT, 0);
212 MUTEX_INIT(&rxkad_client_uid_mutex, "uid", MUTEX_DEFAULT, 0);
213 MUTEX_INIT(&rxkad_random_mutex, "rxkad random", MUTEX_DEFAULT, 0);
214 MUTEX_INIT(&rx_debug_mutex, "debug", MUTEX_DEFAULT, 0);
216 assert(pthread_cond_init
217 (&rx_event_handler_cond, (const pthread_condattr_t *)0) == 0);
218 assert(pthread_cond_init(&rx_listener_cond, (const pthread_condattr_t *)0)
220 assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
221 assert(pthread_key_create(&rx_ts_info_key, NULL) == 0);
223 rxkad_global_stats_init();
225 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
226 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
227 #ifdef RX_ENABLE_LOCKS
230 #endif /* RX_LOCKS_DB */
231 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
232 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
234 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
236 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
238 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
240 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
241 MUTEX_INIT(&rxi_keyCreate_lock, "rxi_keyCreate_lock", MUTEX_DEFAULT, 0);
242 #endif /* RX_ENABLE_LOCKS */
245 pthread_once_t rx_once_init = PTHREAD_ONCE_INIT;
246 #define INIT_PTHREAD_LOCKS \
247 assert(pthread_once(&rx_once_init, rxi_InitPthread)==0)
249 * The rx_stats_mutex mutex protects the following global variables:
250 * rxi_lowConnRefCount
251 * rxi_lowPeerRefCount
260 * The rx_quota_mutex mutex protects the following global variables:
268 * The rx_freePktQ_lock protects the following global variables:
273 * The rx_packets_mutex mutex protects the following global variables:
281 * The rx_pthread_mutex mutex protects the following global variables:
285 #define INIT_PTHREAD_LOCKS
289 /* Variables for handling the minProcs implementation. availProcs gives the
290 * number of threads available in the pool at this moment (not counting dudes
291 * executing right now). totalMin gives the total number of procs required
292 * for handling all minProcs requests. minDeficit is a dynamic variable
293 * tracking the # of procs required to satisfy all of the remaining minProcs
295 * For fine grain locking to work, the quota check and the reservation of
296 * a server thread has to come while rxi_availProcs and rxi_minDeficit
297 * are locked. To this end, the code has been modified under #ifdef
298 * RX_ENABLE_LOCKS so that quota checks and reservation occur at the
299 * same time. A new function, ReturnToServerPool() returns the allocation.
301 * A call can be on several queue's (but only one at a time). When
302 * rxi_ResetCall wants to remove the call from a queue, it has to ensure
303 * that no one else is touching the queue. To this end, we store the address
304 * of the queue lock in the call structure (under the call lock) when we
305 * put the call on a queue, and we clear the call_queue_lock when the
306 * call is removed from a queue (once the call lock has been obtained).
307 * This allows rxi_ResetCall to safely synchronize with others wishing
308 * to manipulate the queue.
311 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
312 static afs_kmutex_t rx_rpc_stats;
313 void rxi_StartUnlocked(struct rxevent *event, void *call,
314 void *arg1, int istack);
317 /* We keep a "last conn pointer" in rxi_FindConnection. The odds are
318 ** pretty good that the next packet coming in is from the same connection
319 ** as the last packet, since we're send multiple packets in a transmit window.
321 struct rx_connection *rxLastConn = 0;
323 #ifdef RX_ENABLE_LOCKS
324 /* The locking hierarchy for rx fine grain locking is composed of these
327 * rx_connHashTable_lock - synchronizes conn creation, rx_connHashTable access
328 * conn_call_lock - used to synchonize rx_EndCall and rx_NewCall
329 * call->lock - locks call data fields.
330 * These are independent of each other:
331 * rx_freeCallQueue_lock
336 * serverQueueEntry->lock
338 * rx_peerHashTable_lock - locked under rx_connHashTable_lock
339 * peer->lock - locks peer data fields.
340 * conn_data_lock - that more than one thread is not updating a conn data
341 * field at the same time.
349 * Do we need a lock to protect the peer field in the conn structure?
350 * conn->peer was previously a constant for all intents and so has no
351 * lock protecting this field. The multihomed client delta introduced
352 * a RX code change : change the peer field in the connection structure
353 * to that remote interface from which the last packet for this
354 * connection was sent out. This may become an issue if further changes
357 #define SET_CALL_QUEUE_LOCK(C, L) (C)->call_queue_lock = (L)
358 #define CLEAR_CALL_QUEUE_LOCK(C) (C)->call_queue_lock = NULL
360 /* rxdb_fileID is used to identify the lock location, along with line#. */
361 static int rxdb_fileID = RXDB_FILE_RX;
362 #endif /* RX_LOCKS_DB */
363 #else /* RX_ENABLE_LOCKS */
364 #define SET_CALL_QUEUE_LOCK(C, L)
365 #define CLEAR_CALL_QUEUE_LOCK(C)
366 #endif /* RX_ENABLE_LOCKS */
367 struct rx_serverQueueEntry *rx_waitForPacket = 0;
368 struct rx_serverQueueEntry *rx_waitingForPacket = 0;
370 /* ------------Exported Interfaces------------- */
372 /* This function allows rxkad to set the epoch to a suitably random number
373 * which rx_NewConnection will use in the future. The principle purpose is to
374 * get rxnull connections to use the same epoch as the rxkad connections do, at
375 * least once the first rxkad connection is established. This is important now
376 * that the host/port addresses aren't used in FindConnection: the uniqueness
377 * of epoch/cid matters and the start time won't do. */
379 #ifdef AFS_PTHREAD_ENV
381 * This mutex protects the following global variables:
385 #define LOCK_EPOCH MUTEX_ENTER(&epoch_mutex)
386 #define UNLOCK_EPOCH MUTEX_EXIT(&epoch_mutex)
390 #endif /* AFS_PTHREAD_ENV */
393 rx_SetEpoch(afs_uint32 epoch)
400 /* Initialize rx. A port number may be mentioned, in which case this
401 * becomes the default port number for any service installed later.
402 * If 0 is provided for the port number, a random port will be chosen
403 * by the kernel. Whether this will ever overlap anything in
404 * /etc/services is anybody's guess... Returns 0 on success, -1 on
409 int rxinit_status = 1;
410 #ifdef AFS_PTHREAD_ENV
412 * This mutex protects the following global variables:
416 #define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
417 #define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
420 #define UNLOCK_RX_INIT
424 rx_InitHost(u_int host, u_int port)
431 char *htable, *ptable;
438 if (rxinit_status == 0) {
439 tmp_status = rxinit_status;
441 return tmp_status; /* Already started; return previous error code. */
447 if (afs_winsockInit() < 0)
453 * Initialize anything necessary to provide a non-premptive threading
456 rxi_InitializeThreadSupport();
459 /* Allocate and initialize a socket for client and perhaps server
462 rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
463 if (rx_socket == OSI_NULLSOCKET) {
467 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
470 #endif /* RX_LOCKS_DB */
471 MUTEX_INIT(&rx_stats_mutex, "rx_stats_mutex", MUTEX_DEFAULT, 0);
472 MUTEX_INIT(&rx_waiting_mutex, "rx_waiting_mutex", MUTEX_DEFAULT, 0);
473 MUTEX_INIT(&rx_quota_mutex, "rx_quota_mutex", MUTEX_DEFAULT, 0);
474 MUTEX_INIT(&rx_pthread_mutex, "rx_pthread_mutex", MUTEX_DEFAULT, 0);
475 MUTEX_INIT(&rx_packets_mutex, "rx_packets_mutex", MUTEX_DEFAULT, 0);
476 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
477 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
478 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
479 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
481 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
483 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
485 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
487 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
488 #if defined(AFS_HPUX110_ENV)
490 rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
491 #endif /* AFS_HPUX110_ENV */
492 #endif /* RX_ENABLE_LOCKS && KERNEL */
495 rx_connDeadTime = 12;
496 rx_tranquil = 0; /* reset flag */
497 memset(&rx_stats, 0, sizeof(struct rx_statistics));
499 osi_Alloc(rx_hashTableSize * sizeof(struct rx_connection *));
500 PIN(htable, rx_hashTableSize * sizeof(struct rx_connection *)); /* XXXXX */
501 memset(htable, 0, rx_hashTableSize * sizeof(struct rx_connection *));
502 ptable = (char *)osi_Alloc(rx_hashTableSize * sizeof(struct rx_peer *));
503 PIN(ptable, rx_hashTableSize * sizeof(struct rx_peer *)); /* XXXXX */
504 memset(ptable, 0, rx_hashTableSize * sizeof(struct rx_peer *));
506 /* Malloc up a bunch of packets & buffers */
508 queue_Init(&rx_freePacketQueue);
509 rxi_NeedMorePackets = FALSE;
510 #ifdef RX_ENABLE_TSFPQ
511 rx_nPackets = 0; /* in TSFPQ version, rx_nPackets is managed by rxi_MorePackets* */
512 rxi_MorePacketsTSFPQ(rx_extraPackets + RX_MAX_QUOTA + 2, RX_TS_FPQ_FLUSH_GLOBAL, 0);
513 #else /* RX_ENABLE_TSFPQ */
514 rx_nPackets = rx_extraPackets + RX_MAX_QUOTA + 2; /* fudge */
515 rxi_MorePackets(rx_nPackets);
516 #endif /* RX_ENABLE_TSFPQ */
523 #if defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
524 tv.tv_sec = clock_now.sec;
525 tv.tv_usec = clock_now.usec;
526 srand((unsigned int)tv.tv_usec);
533 #if defined(KERNEL) && !defined(UKERNEL)
534 /* Really, this should never happen in a real kernel */
537 struct sockaddr_in addr;
539 int addrlen = sizeof(addr);
541 socklen_t addrlen = sizeof(addr);
543 if (getsockname((intptr_t)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
547 rx_port = addr.sin_port;
550 rx_stats.minRtt.sec = 9999999;
552 rx_SetEpoch(tv.tv_sec | 0x80000000);
554 rx_SetEpoch(tv.tv_sec); /* Start time of this package, rxkad
555 * will provide a randomer value. */
557 MUTEX_ENTER(&rx_quota_mutex);
558 rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
559 MUTEX_EXIT(&rx_quota_mutex);
560 /* *Slightly* random start time for the cid. This is just to help
561 * out with the hashing function at the peer */
562 rx_nextCid = ((tv.tv_sec ^ tv.tv_usec) << RX_CIDSHIFT);
563 rx_connHashTable = (struct rx_connection **)htable;
564 rx_peerHashTable = (struct rx_peer **)ptable;
566 rx_lastAckDelay.sec = 0;
567 rx_lastAckDelay.usec = 400000; /* 400 milliseconds */
568 rx_hardAckDelay.sec = 0;
569 rx_hardAckDelay.usec = 100000; /* 100 milliseconds */
570 rx_softAckDelay.sec = 0;
571 rx_softAckDelay.usec = 100000; /* 100 milliseconds */
573 rxevent_Init(20, rxi_ReScheduleEvents);
575 /* Initialize various global queues */
576 queue_Init(&rx_idleServerQueue);
577 queue_Init(&rx_incomingCallQueue);
578 queue_Init(&rx_freeCallQueue);
580 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
581 /* Initialize our list of usable IP addresses. */
585 /* Start listener process (exact function is dependent on the
586 * implementation environment--kernel or user space) */
590 tmp_status = rxinit_status = 0;
598 return rx_InitHost(htonl(INADDR_ANY), port);
601 /* called with unincremented nRequestsRunning to see if it is OK to start
602 * a new thread in this service. Could be "no" for two reasons: over the
603 * max quota, or would prevent others from reaching their min quota.
605 #ifdef RX_ENABLE_LOCKS
606 /* This verion of QuotaOK reserves quota if it's ok while the
607 * rx_serverPool_lock is held. Return quota using ReturnToServerPool().
610 QuotaOK(struct rx_service *aservice)
612 /* check if over max quota */
613 if (aservice->nRequestsRunning >= aservice->maxProcs) {
617 /* under min quota, we're OK */
618 /* otherwise, can use only if there are enough to allow everyone
619 * to go to their min quota after this guy starts.
622 MUTEX_ENTER(&rx_quota_mutex);
623 if ((aservice->nRequestsRunning < aservice->minProcs)
624 || (rxi_availProcs > rxi_minDeficit)) {
625 aservice->nRequestsRunning++;
626 /* just started call in minProcs pool, need fewer to maintain
628 if (aservice->nRequestsRunning <= aservice->minProcs)
631 MUTEX_EXIT(&rx_quota_mutex);
634 MUTEX_EXIT(&rx_quota_mutex);
640 ReturnToServerPool(struct rx_service *aservice)
642 aservice->nRequestsRunning--;
643 MUTEX_ENTER(&rx_quota_mutex);
644 if (aservice->nRequestsRunning < aservice->minProcs)
647 MUTEX_EXIT(&rx_quota_mutex);
650 #else /* RX_ENABLE_LOCKS */
652 QuotaOK(struct rx_service *aservice)
655 /* under min quota, we're OK */
656 if (aservice->nRequestsRunning < aservice->minProcs)
659 /* check if over max quota */
660 if (aservice->nRequestsRunning >= aservice->maxProcs)
663 /* otherwise, can use only if there are enough to allow everyone
664 * to go to their min quota after this guy starts.
666 if (rxi_availProcs > rxi_minDeficit)
670 #endif /* RX_ENABLE_LOCKS */
673 /* Called by rx_StartServer to start up lwp's to service calls.
674 NExistingProcs gives the number of procs already existing, and which
675 therefore needn't be created. */
677 rxi_StartServerProcs(int nExistingProcs)
679 struct rx_service *service;
684 /* For each service, reserve N processes, where N is the "minimum"
685 * number of processes that MUST be able to execute a request in parallel,
686 * at any time, for that process. Also compute the maximum difference
687 * between any service's maximum number of processes that can run
688 * (i.e. the maximum number that ever will be run, and a guarantee
689 * that this number will run if other services aren't running), and its
690 * minimum number. The result is the extra number of processes that
691 * we need in order to provide the latter guarantee */
692 for (i = 0; i < RX_MAX_SERVICES; i++) {
694 service = rx_services[i];
695 if (service == (struct rx_service *)0)
697 nProcs += service->minProcs;
698 diff = service->maxProcs - service->minProcs;
702 nProcs += maxdiff; /* Extra processes needed to allow max number requested to run in any given service, under good conditions */
703 nProcs -= nExistingProcs; /* Subtract the number of procs that were previously created for use as server procs */
704 for (i = 0; i < nProcs; i++) {
705 rxi_StartServerProc(rx_ServerProc, rx_stackSize);
711 /* This routine is only required on Windows */
713 rx_StartClientThread(void)
715 #ifdef AFS_PTHREAD_ENV
717 pid = pthread_self();
718 #endif /* AFS_PTHREAD_ENV */
720 #endif /* AFS_NT40_ENV */
722 /* This routine must be called if any services are exported. If the
723 * donateMe flag is set, the calling process is donated to the server
726 rx_StartServer(int donateMe)
728 struct rx_service *service;
734 /* Start server processes, if necessary (exact function is dependent
735 * on the implementation environment--kernel or user space). DonateMe
736 * will be 1 if there is 1 pre-existing proc, i.e. this one. In this
737 * case, one less new proc will be created rx_StartServerProcs.
739 rxi_StartServerProcs(donateMe);
741 /* count up the # of threads in minProcs, and add set the min deficit to
742 * be that value, too.
744 for (i = 0; i < RX_MAX_SERVICES; i++) {
745 service = rx_services[i];
746 if (service == (struct rx_service *)0)
748 MUTEX_ENTER(&rx_quota_mutex);
749 rxi_totalMin += service->minProcs;
750 /* below works even if a thread is running, since minDeficit would
751 * still have been decremented and later re-incremented.
753 rxi_minDeficit += service->minProcs;
754 MUTEX_EXIT(&rx_quota_mutex);
757 /* Turn on reaping of idle server connections */
758 rxi_ReapConnections(NULL, NULL, NULL);
767 #ifdef AFS_PTHREAD_ENV
769 pid = afs_pointer_to_int(pthread_self());
770 #else /* AFS_PTHREAD_ENV */
772 LWP_CurrentProcess(&pid);
773 #endif /* AFS_PTHREAD_ENV */
775 sprintf(name, "srv_%d", ++nProcs);
777 (*registerProgram) (pid, name);
779 #endif /* AFS_NT40_ENV */
780 rx_ServerProc(NULL); /* Never returns */
782 #ifdef RX_ENABLE_TSFPQ
783 /* no use leaving packets around in this thread's local queue if
784 * it isn't getting donated to the server thread pool.
786 rxi_FlushLocalPacketsTSFPQ();
787 #endif /* RX_ENABLE_TSFPQ */
791 /* Create a new client connection to the specified service, using the
792 * specified security object to implement the security model for this
794 struct rx_connection *
795 rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
796 struct rx_securityClass *securityObject,
797 int serviceSecurityIndex)
801 struct rx_connection *conn;
806 dpf(("rx_NewConnection(host %x, port %u, service %u, securityObject %p, "
807 "serviceSecurityIndex %d)\n",
808 ntohl(shost), ntohs(sport), sservice, securityObject,
809 serviceSecurityIndex));
811 /* Vasilsi said: "NETPRI protects Cid and Alloc", but can this be true in
812 * the case of kmem_alloc? */
813 conn = rxi_AllocConnection();
814 #ifdef RX_ENABLE_LOCKS
815 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
816 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
817 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
820 MUTEX_ENTER(&rx_connHashTable_lock);
821 cid = (rx_nextCid += RX_MAXCALLS);
822 conn->type = RX_CLIENT_CONNECTION;
824 conn->epoch = rx_epoch;
825 conn->peer = rxi_FindPeer(shost, sport, 0, 1);
826 conn->serviceId = sservice;
827 conn->securityObject = securityObject;
828 conn->securityData = (void *) 0;
829 conn->securityIndex = serviceSecurityIndex;
830 rx_SetConnDeadTime(conn, rx_connDeadTime);
831 rx_SetConnSecondsUntilNatPing(conn, 0);
832 conn->ackRate = RX_FAST_ACK_RATE;
834 conn->specific = NULL;
835 conn->challengeEvent = NULL;
836 conn->delayedAbortEvent = NULL;
837 conn->abortCount = 0;
839 for (i = 0; i < RX_MAXCALLS; i++) {
840 conn->twind[i] = rx_initSendWindow;
841 conn->rwind[i] = rx_initReceiveWindow;
844 RXS_NewConnection(securityObject, conn);
846 CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
848 conn->refCount++; /* no lock required since only this thread knows... */
849 conn->next = rx_connHashTable[hashindex];
850 rx_connHashTable[hashindex] = conn;
852 rx_MutexIncrement(rx_stats.nClientConns, rx_stats_mutex);
853 MUTEX_EXIT(&rx_connHashTable_lock);
859 rx_SetConnDeadTime(struct rx_connection *conn, int seconds)
861 /* The idea is to set the dead time to a value that allows several
862 * keepalives to be dropped without timing out the connection. */
863 conn->secondsUntilDead = MAX(seconds, 6);
864 conn->secondsUntilPing = conn->secondsUntilDead / 6;
867 int rxi_lowPeerRefCount = 0;
868 int rxi_lowConnRefCount = 0;
871 * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
872 * NOTE: must not be called with rx_connHashTable_lock held.
875 rxi_CleanupConnection(struct rx_connection *conn)
877 /* Notify the service exporter, if requested, that this connection
878 * is being destroyed */
879 if (conn->type == RX_SERVER_CONNECTION && conn->service->destroyConnProc)
880 (*conn->service->destroyConnProc) (conn);
882 /* Notify the security module that this connection is being destroyed */
883 RXS_DestroyConnection(conn->securityObject, conn);
885 /* If this is the last connection using the rx_peer struct, set its
886 * idle time to now. rxi_ReapConnections will reap it if it's still
887 * idle (refCount == 0) after rx_idlePeerTime (60 seconds) have passed.
889 MUTEX_ENTER(&rx_peerHashTable_lock);
890 if (conn->peer->refCount < 2) {
891 conn->peer->idleWhen = clock_Sec();
892 if (conn->peer->refCount < 1) {
893 conn->peer->refCount = 1;
894 if (rx_stats_active) {
895 MUTEX_ENTER(&rx_stats_mutex);
896 rxi_lowPeerRefCount++;
897 MUTEX_EXIT(&rx_stats_mutex);
901 conn->peer->refCount--;
902 MUTEX_EXIT(&rx_peerHashTable_lock);
906 if (conn->type == RX_SERVER_CONNECTION)
907 rx_MutexDecrement(rx_stats.nServerConns, rx_stats_mutex);
909 rx_MutexDecrement(rx_stats.nClientConns, rx_stats_mutex);
912 if (conn->specific) {
914 for (i = 0; i < conn->nSpecific; i++) {
915 if (conn->specific[i] && rxi_keyCreate_destructor[i])
916 (*rxi_keyCreate_destructor[i]) (conn->specific[i]);
917 conn->specific[i] = NULL;
919 free(conn->specific);
921 conn->specific = NULL;
925 MUTEX_DESTROY(&conn->conn_call_lock);
926 MUTEX_DESTROY(&conn->conn_data_lock);
927 CV_DESTROY(&conn->conn_call_cv);
929 rxi_FreeConnection(conn);
932 /* Destroy the specified connection */
934 rxi_DestroyConnection(struct rx_connection *conn)
936 MUTEX_ENTER(&rx_connHashTable_lock);
937 rxi_DestroyConnectionNoLock(conn);
938 /* conn should be at the head of the cleanup list */
939 if (conn == rx_connCleanup_list) {
940 rx_connCleanup_list = rx_connCleanup_list->next;
941 MUTEX_EXIT(&rx_connHashTable_lock);
942 rxi_CleanupConnection(conn);
944 #ifdef RX_ENABLE_LOCKS
946 MUTEX_EXIT(&rx_connHashTable_lock);
948 #endif /* RX_ENABLE_LOCKS */
952 rxi_DestroyConnectionNoLock(struct rx_connection *conn)
954 struct rx_connection **conn_ptr;
956 struct rx_packet *packet;
963 MUTEX_ENTER(&conn->conn_data_lock);
964 if (conn->refCount > 0)
967 if (rx_stats_active) {
968 MUTEX_ENTER(&rx_stats_mutex);
969 rxi_lowConnRefCount++;
970 MUTEX_EXIT(&rx_stats_mutex);
974 if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
975 /* Busy; wait till the last guy before proceeding */
976 MUTEX_EXIT(&conn->conn_data_lock);
981 /* If the client previously called rx_NewCall, but it is still
982 * waiting, treat this as a running call, and wait to destroy the
983 * connection later when the call completes. */
984 if ((conn->type == RX_CLIENT_CONNECTION)
985 && (conn->flags & RX_CONN_MAKECALL_WAITING)) {
986 conn->flags |= RX_CONN_DESTROY_ME;
987 MUTEX_EXIT(&conn->conn_data_lock);
991 MUTEX_EXIT(&conn->conn_data_lock);
993 /* Check for extant references to this connection */
994 for (i = 0; i < RX_MAXCALLS; i++) {
995 struct rx_call *call = conn->call[i];
998 if (conn->type == RX_CLIENT_CONNECTION) {
999 MUTEX_ENTER(&call->lock);
1000 if (call->delayedAckEvent) {
1001 /* Push the final acknowledgment out now--there
1002 * won't be a subsequent call to acknowledge the
1003 * last reply packets */
1004 rxevent_Cancel(call->delayedAckEvent, call,
1005 RX_CALL_REFCOUNT_DELAY);
1006 if (call->state == RX_STATE_PRECALL
1007 || call->state == RX_STATE_ACTIVE) {
1008 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1010 rxi_AckAll(NULL, call, 0);
1013 MUTEX_EXIT(&call->lock);
1017 #ifdef RX_ENABLE_LOCKS
1019 if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
1020 MUTEX_EXIT(&conn->conn_data_lock);
1022 /* Someone is accessing a packet right now. */
1026 #endif /* RX_ENABLE_LOCKS */
1029 /* Don't destroy the connection if there are any call
1030 * structures still in use */
1031 MUTEX_ENTER(&conn->conn_data_lock);
1032 conn->flags |= RX_CONN_DESTROY_ME;
1033 MUTEX_EXIT(&conn->conn_data_lock);
1038 if (conn->natKeepAliveEvent) {
1039 rxi_NatKeepAliveOff(conn);
1042 if (conn->delayedAbortEvent) {
1043 rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
1044 packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
1046 MUTEX_ENTER(&conn->conn_data_lock);
1047 rxi_SendConnectionAbort(conn, packet, 0, 1);
1048 MUTEX_EXIT(&conn->conn_data_lock);
1049 rxi_FreePacket(packet);
1053 /* Remove from connection hash table before proceeding */
1055 &rx_connHashTable[CONN_HASH
1056 (peer->host, peer->port, conn->cid, conn->epoch,
1058 for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
1059 if (*conn_ptr == conn) {
1060 *conn_ptr = conn->next;
1064 /* if the conn that we are destroying was the last connection, then we
1065 * clear rxLastConn as well */
1066 if (rxLastConn == conn)
1069 /* Make sure the connection is completely reset before deleting it. */
1070 /* get rid of pending events that could zap us later */
1071 if (conn->challengeEvent)
1072 rxevent_Cancel(conn->challengeEvent, (struct rx_call *)0, 0);
1073 if (conn->checkReachEvent)
1074 rxevent_Cancel(conn->checkReachEvent, (struct rx_call *)0, 0);
1075 if (conn->natKeepAliveEvent)
1076 rxevent_Cancel(conn->natKeepAliveEvent, (struct rx_call *)0, 0);
1078 /* Add the connection to the list of destroyed connections that
1079 * need to be cleaned up. This is necessary to avoid deadlocks
1080 * in the routines we call to inform others that this connection is
1081 * being destroyed. */
1082 conn->next = rx_connCleanup_list;
1083 rx_connCleanup_list = conn;
1086 /* Externally available version */
1088 rx_DestroyConnection(struct rx_connection *conn)
1093 rxi_DestroyConnection(conn);
1098 rx_GetConnection(struct rx_connection *conn)
1103 MUTEX_ENTER(&conn->conn_data_lock);
1105 MUTEX_EXIT(&conn->conn_data_lock);
1109 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1110 /* Wait for the transmit queue to no longer be busy.
1111 * requires the call->lock to be held */
1112 static void rxi_WaitforTQBusy(struct rx_call *call) {
1113 while (call->flags & RX_CALL_TQ_BUSY) {
1114 call->flags |= RX_CALL_TQ_WAIT;
1116 #ifdef RX_ENABLE_LOCKS
1117 osirx_AssertMine(&call->lock, "rxi_WaitforTQ lock");
1118 CV_WAIT(&call->cv_tq, &call->lock);
1119 #else /* RX_ENABLE_LOCKS */
1120 osi_rxSleep(&call->tq);
1121 #endif /* RX_ENABLE_LOCKS */
1123 if (call->tqWaiters == 0) {
1124 call->flags &= ~RX_CALL_TQ_WAIT;
1130 /* Start a new rx remote procedure call, on the specified connection.
1131 * If wait is set to 1, wait for a free call channel; otherwise return
1132 * 0. Maxtime gives the maximum number of seconds this call may take,
1133 * after rx_NewCall returns. After this time interval, a call to any
1134 * of rx_SendData, rx_ReadData, etc. will fail with RX_CALL_TIMEOUT.
1135 * For fine grain locking, we hold the conn_call_lock in order to
1136 * to ensure that we don't get signalle after we found a call in an active
1137 * state and before we go to sleep.
1140 rx_NewCall(struct rx_connection *conn)
1143 struct rx_call *call;
1144 struct clock queueTime;
1148 dpf(("rx_NewCall(conn %"AFS_PTR_FMT")\n", conn));
1151 clock_GetTime(&queueTime);
1153 * Check if there are others waiting for a new call.
1154 * If so, let them go first to avoid starving them.
1155 * This is a fairly simple scheme, and might not be
1156 * a complete solution for large numbers of waiters.
1158 * makeCallWaiters keeps track of the number of
1159 * threads waiting to make calls and the
1160 * RX_CONN_MAKECALL_WAITING flag bit is used to
1161 * indicate that there are indeed calls waiting.
1162 * The flag is set when the waiter is incremented.
1163 * It is only cleared when makeCallWaiters is 0.
1164 * This prevents us from accidently destroying the
1165 * connection while it is potentially about to be used.
1167 MUTEX_ENTER(&conn->conn_call_lock);
1168 MUTEX_ENTER(&conn->conn_data_lock);
1169 if (conn->makeCallWaiters) {
1170 conn->makeCallWaiters++;
1171 MUTEX_EXIT(&conn->conn_data_lock);
1173 #ifdef RX_ENABLE_LOCKS
1174 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1178 MUTEX_ENTER(&conn->conn_data_lock);
1179 conn->makeCallWaiters--;
1180 if (conn->makeCallWaiters == 0)
1181 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1183 MUTEX_EXIT(&conn->conn_data_lock);
1186 for (i = 0; i < RX_MAXCALLS; i++) {
1187 call = conn->call[i];
1189 if (call->state == RX_STATE_DALLY) {
1190 MUTEX_ENTER(&call->lock);
1191 if (call->state == RX_STATE_DALLY) {
1192 call->state = RX_STATE_RESET;
1193 MUTEX_EXIT(&conn->conn_call_lock);
1194 rxi_ResetCall(call, 0);
1195 MUTEX_ENTER(&conn->conn_call_lock);
1196 (*call->callNumber)++;
1199 MUTEX_EXIT(&call->lock);
1202 /* rxi_NewCall returns with mutex locked */
1203 call = rxi_NewCall(conn, i);
1207 if (i < RX_MAXCALLS) {
1210 MUTEX_ENTER(&conn->conn_data_lock);
1211 conn->flags |= RX_CONN_MAKECALL_WAITING;
1212 conn->makeCallWaiters++;
1213 MUTEX_EXIT(&conn->conn_data_lock);
1215 #ifdef RX_ENABLE_LOCKS
1216 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1220 MUTEX_ENTER(&conn->conn_data_lock);
1221 conn->makeCallWaiters--;
1222 if (conn->makeCallWaiters == 0)
1223 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1224 MUTEX_EXIT(&conn->conn_data_lock);
1227 * Wake up anyone else who might be giving us a chance to
1228 * run (see code above that avoids resource starvation).
1230 #ifdef RX_ENABLE_LOCKS
1231 CV_BROADCAST(&conn->conn_call_cv);
1236 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1238 /* Client is initially in send mode */
1239 call->state = RX_STATE_ACTIVE;
1240 call->error = conn->error;
1242 call->mode = RX_MODE_ERROR;
1244 call->mode = RX_MODE_SENDING;
1246 /* remember start time for call in case we have hard dead time limit */
1247 call->queueTime = queueTime;
1248 clock_GetTime(&call->startTime);
1249 hzero(call->bytesSent);
1250 hzero(call->bytesRcvd);
1252 /* Turn on busy protocol. */
1253 rxi_KeepAliveOn(call);
1254 MUTEX_EXIT(&conn->conn_call_lock);
1256 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1257 if (call->flags & (RX_CALL_TQ_BUSY | RX_CALL_TQ_CLEARME)) {
1258 osi_Panic("rx_NewCall call about to be used without an empty tq");
1260 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1262 MUTEX_EXIT(&call->lock);
1265 dpf(("rx_NewCall(call %"AFS_PTR_FMT")\n", call));
1270 rxi_HasActiveCalls(struct rx_connection *aconn)
1273 struct rx_call *tcall;
1277 for (i = 0; i < RX_MAXCALLS; i++) {
1278 if ((tcall = aconn->call[i])) {
1279 if ((tcall->state == RX_STATE_ACTIVE)
1280 || (tcall->state == RX_STATE_PRECALL)) {
1291 rxi_GetCallNumberVector(struct rx_connection *aconn,
1292 afs_int32 * aint32s)
1295 struct rx_call *tcall;
1299 for (i = 0; i < RX_MAXCALLS; i++) {
1300 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1301 aint32s[i] = aconn->callNumber[i] + 1;
1303 aint32s[i] = aconn->callNumber[i];
1310 rxi_SetCallNumberVector(struct rx_connection *aconn,
1311 afs_int32 * aint32s)
1314 struct rx_call *tcall;
1318 for (i = 0; i < RX_MAXCALLS; i++) {
1319 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1320 aconn->callNumber[i] = aint32s[i] - 1;
1322 aconn->callNumber[i] = aint32s[i];
1328 /* Advertise a new service. A service is named locally by a UDP port
1329 * number plus a 16-bit service id. Returns (struct rx_service *) 0
1332 char *serviceName; Name for identification purposes (e.g. the
1333 service name might be used for probing for
1336 rx_NewServiceHost(afs_uint32 host, u_short port, u_short serviceId,
1337 char *serviceName, struct rx_securityClass **securityObjects,
1338 int nSecurityObjects,
1339 afs_int32(*serviceProc) (struct rx_call * acall))
1341 osi_socket socket = OSI_NULLSOCKET;
1342 struct rx_service *tservice;
1348 if (serviceId == 0) {
1350 "rx_NewService: service id for service %s is not non-zero.\n",
1357 "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",
1365 tservice = rxi_AllocService();
1367 for (i = 0; i < RX_MAX_SERVICES; i++) {
1368 struct rx_service *service = rx_services[i];
1370 if (port == service->servicePort && host == service->serviceHost) {
1371 if (service->serviceId == serviceId) {
1372 /* The identical service has already been
1373 * installed; if the caller was intending to
1374 * change the security classes used by this
1375 * service, he/she loses. */
1377 "rx_NewService: tried to install service %s with service id %d, which is already in use for service %s\n",
1378 serviceName, serviceId, service->serviceName);
1380 rxi_FreeService(tservice);
1383 /* Different service, same port: re-use the socket
1384 * which is bound to the same port */
1385 socket = service->socket;
1388 if (socket == OSI_NULLSOCKET) {
1389 /* If we don't already have a socket (from another
1390 * service on same port) get a new one */
1391 socket = rxi_GetHostUDPSocket(host, port);
1392 if (socket == OSI_NULLSOCKET) {
1394 rxi_FreeService(tservice);
1399 service->socket = socket;
1400 service->serviceHost = host;
1401 service->servicePort = port;
1402 service->serviceId = serviceId;
1403 service->serviceName = serviceName;
1404 service->nSecurityObjects = nSecurityObjects;
1405 service->securityObjects = securityObjects;
1406 service->minProcs = 0;
1407 service->maxProcs = 1;
1408 service->idleDeadTime = 60;
1409 service->idleDeadErr = 0;
1410 service->connDeadTime = rx_connDeadTime;
1411 service->executeRequestProc = serviceProc;
1412 service->checkReach = 0;
1413 rx_services[i] = service; /* not visible until now */
1419 rxi_FreeService(tservice);
1420 (osi_Msg "rx_NewService: cannot support > %d services\n",
1425 /* Set configuration options for all of a service's security objects */
1428 rx_SetSecurityConfiguration(struct rx_service *service,
1429 rx_securityConfigVariables type,
1433 for (i = 0; i<service->nSecurityObjects; i++) {
1434 if (service->securityObjects[i]) {
1435 RXS_SetConfiguration(service->securityObjects[i], NULL, type,
1443 rx_NewService(u_short port, u_short serviceId, char *serviceName,
1444 struct rx_securityClass **securityObjects, int nSecurityObjects,
1445 afs_int32(*serviceProc) (struct rx_call * acall))
1447 return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
1450 /* Generic request processing loop. This routine should be called
1451 * by the implementation dependent rx_ServerProc. If socketp is
1452 * non-null, it will be set to the file descriptor that this thread
1453 * is now listening on. If socketp is null, this routine will never
1456 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1458 struct rx_call *call;
1460 struct rx_service *tservice = NULL;
1467 call = rx_GetCall(threadID, tservice, socketp);
1468 if (socketp && *socketp != OSI_NULLSOCKET) {
1469 /* We are now a listener thread */
1474 /* if server is restarting( typically smooth shutdown) then do not
1475 * allow any new calls.
1478 if (rx_tranquil && (call != NULL)) {
1482 MUTEX_ENTER(&call->lock);
1484 rxi_CallError(call, RX_RESTARTING);
1485 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1487 MUTEX_EXIT(&call->lock);
1491 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1492 #ifdef RX_ENABLE_LOCKS
1494 #endif /* RX_ENABLE_LOCKS */
1495 afs_termState = AFSOP_STOP_AFS;
1496 afs_osi_Wakeup(&afs_termState);
1497 #ifdef RX_ENABLE_LOCKS
1499 #endif /* RX_ENABLE_LOCKS */
1504 tservice = call->conn->service;
1506 if (tservice->beforeProc)
1507 (*tservice->beforeProc) (call);
1509 code = call->conn->service->executeRequestProc(call);
1511 if (tservice->afterProc)
1512 (*tservice->afterProc) (call, code);
1514 rx_EndCall(call, code);
1515 if (rx_stats_active) {
1516 MUTEX_ENTER(&rx_stats_mutex);
1518 MUTEX_EXIT(&rx_stats_mutex);
1525 rx_WakeupServerProcs(void)
1527 struct rx_serverQueueEntry *np, *tqp;
1531 MUTEX_ENTER(&rx_serverPool_lock);
1533 #ifdef RX_ENABLE_LOCKS
1534 if (rx_waitForPacket)
1535 CV_BROADCAST(&rx_waitForPacket->cv);
1536 #else /* RX_ENABLE_LOCKS */
1537 if (rx_waitForPacket)
1538 osi_rxWakeup(rx_waitForPacket);
1539 #endif /* RX_ENABLE_LOCKS */
1540 MUTEX_ENTER(&freeSQEList_lock);
1541 for (np = rx_FreeSQEList; np; np = tqp) {
1542 tqp = *(struct rx_serverQueueEntry **)np;
1543 #ifdef RX_ENABLE_LOCKS
1544 CV_BROADCAST(&np->cv);
1545 #else /* RX_ENABLE_LOCKS */
1547 #endif /* RX_ENABLE_LOCKS */
1549 MUTEX_EXIT(&freeSQEList_lock);
1550 for (queue_Scan(&rx_idleServerQueue, np, tqp, rx_serverQueueEntry)) {
1551 #ifdef RX_ENABLE_LOCKS
1552 CV_BROADCAST(&np->cv);
1553 #else /* RX_ENABLE_LOCKS */
1555 #endif /* RX_ENABLE_LOCKS */
1557 MUTEX_EXIT(&rx_serverPool_lock);
1562 * One thing that seems to happen is that all the server threads get
1563 * tied up on some empty or slow call, and then a whole bunch of calls
1564 * arrive at once, using up the packet pool, so now there are more
1565 * empty calls. The most critical resources here are server threads
1566 * and the free packet pool. The "doreclaim" code seems to help in
1567 * general. I think that eventually we arrive in this state: there
1568 * are lots of pending calls which do have all their packets present,
1569 * so they won't be reclaimed, are multi-packet calls, so they won't
1570 * be scheduled until later, and thus are tying up most of the free
1571 * packet pool for a very long time.
1573 * 1. schedule multi-packet calls if all the packets are present.
1574 * Probably CPU-bound operation, useful to return packets to pool.
1575 * Do what if there is a full window, but the last packet isn't here?
1576 * 3. preserve one thread which *only* runs "best" calls, otherwise
1577 * it sleeps and waits for that type of call.
1578 * 4. Don't necessarily reserve a whole window for each thread. In fact,
1579 * the current dataquota business is badly broken. The quota isn't adjusted
1580 * to reflect how many packets are presently queued for a running call.
1581 * So, when we schedule a queued call with a full window of packets queued
1582 * up for it, that *should* free up a window full of packets for other 2d-class
1583 * calls to be able to use from the packet pool. But it doesn't.
1585 * NB. Most of the time, this code doesn't run -- since idle server threads
1586 * sit on the idle server queue and are assigned by "...ReceivePacket" as soon
1587 * as a new call arrives.
1589 /* Sleep until a call arrives. Returns a pointer to the call, ready
1590 * for an rx_Read. */
1591 #ifdef RX_ENABLE_LOCKS
1593 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1595 struct rx_serverQueueEntry *sq;
1596 struct rx_call *call = (struct rx_call *)0;
1597 struct rx_service *service = NULL;
1600 MUTEX_ENTER(&freeSQEList_lock);
1602 if ((sq = rx_FreeSQEList)) {
1603 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1604 MUTEX_EXIT(&freeSQEList_lock);
1605 } else { /* otherwise allocate a new one and return that */
1606 MUTEX_EXIT(&freeSQEList_lock);
1607 sq = (struct rx_serverQueueEntry *)
1608 rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1609 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1610 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1613 MUTEX_ENTER(&rx_serverPool_lock);
1614 if (cur_service != NULL) {
1615 ReturnToServerPool(cur_service);
1618 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1619 struct rx_call *tcall, *ncall, *choice2 = NULL;
1621 /* Scan for eligible incoming calls. A call is not eligible
1622 * if the maximum number of calls for its service type are
1623 * already executing */
1624 /* One thread will process calls FCFS (to prevent starvation),
1625 * while the other threads may run ahead looking for calls which
1626 * have all their input data available immediately. This helps
1627 * keep threads from blocking, waiting for data from the client. */
1628 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1629 service = tcall->conn->service;
1630 if (!QuotaOK(service)) {
1633 MUTEX_ENTER(&rx_pthread_mutex);
1634 if (tno == rxi_fcfs_thread_num
1635 || !tcall->queue_item_header.next) {
1636 MUTEX_EXIT(&rx_pthread_mutex);
1637 /* If we're the fcfs thread , then we'll just use
1638 * this call. If we haven't been able to find an optimal
1639 * choice, and we're at the end of the list, then use a
1640 * 2d choice if one has been identified. Otherwise... */
1641 call = (choice2 ? choice2 : tcall);
1642 service = call->conn->service;
1644 MUTEX_EXIT(&rx_pthread_mutex);
1645 if (!queue_IsEmpty(&tcall->rq)) {
1646 struct rx_packet *rp;
1647 rp = queue_First(&tcall->rq, rx_packet);
1648 if (rp->header.seq == 1) {
1650 || (rp->header.flags & RX_LAST_PACKET)) {
1652 } else if (rxi_2dchoice && !choice2
1653 && !(tcall->flags & RX_CALL_CLEARED)
1654 && (tcall->rprev > rxi_HardAckRate)) {
1664 ReturnToServerPool(service);
1671 MUTEX_EXIT(&rx_serverPool_lock);
1672 MUTEX_ENTER(&call->lock);
1674 if (call->flags & RX_CALL_WAIT_PROC) {
1675 call->flags &= ~RX_CALL_WAIT_PROC;
1676 MUTEX_ENTER(&rx_waiting_mutex);
1678 MUTEX_EXIT(&rx_waiting_mutex);
1681 if (call->state != RX_STATE_PRECALL || call->error) {
1682 MUTEX_EXIT(&call->lock);
1683 MUTEX_ENTER(&rx_serverPool_lock);
1684 ReturnToServerPool(service);
1689 if (queue_IsEmpty(&call->rq)
1690 || queue_First(&call->rq, rx_packet)->header.seq != 1)
1691 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1693 CLEAR_CALL_QUEUE_LOCK(call);
1696 /* If there are no eligible incoming calls, add this process
1697 * to the idle server queue, to wait for one */
1701 *socketp = OSI_NULLSOCKET;
1703 sq->socketp = socketp;
1704 queue_Append(&rx_idleServerQueue, sq);
1705 #ifndef AFS_AIX41_ENV
1706 rx_waitForPacket = sq;
1708 rx_waitingForPacket = sq;
1709 #endif /* AFS_AIX41_ENV */
1711 CV_WAIT(&sq->cv, &rx_serverPool_lock);
1713 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1714 MUTEX_EXIT(&rx_serverPool_lock);
1715 return (struct rx_call *)0;
1718 } while (!(call = sq->newcall)
1719 && !(socketp && *socketp != OSI_NULLSOCKET));
1720 MUTEX_EXIT(&rx_serverPool_lock);
1722 MUTEX_ENTER(&call->lock);
1728 MUTEX_ENTER(&freeSQEList_lock);
1729 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1730 rx_FreeSQEList = sq;
1731 MUTEX_EXIT(&freeSQEList_lock);
1734 clock_GetTime(&call->startTime);
1735 call->state = RX_STATE_ACTIVE;
1736 call->mode = RX_MODE_RECEIVING;
1737 #ifdef RX_KERNEL_TRACE
1738 if (ICL_SETACTIVE(afs_iclSetp)) {
1739 int glockOwner = ISAFS_GLOCK();
1742 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1743 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1750 rxi_calltrace(RX_CALL_START, call);
1751 dpf(("rx_GetCall(port=%d, service=%d) ==> call %"AFS_PTR_FMT"\n",
1752 call->conn->service->servicePort, call->conn->service->serviceId,
1755 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1756 MUTEX_EXIT(&call->lock);
1758 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
1763 #else /* RX_ENABLE_LOCKS */
1765 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1767 struct rx_serverQueueEntry *sq;
1768 struct rx_call *call = (struct rx_call *)0, *choice2;
1769 struct rx_service *service = NULL;
1773 MUTEX_ENTER(&freeSQEList_lock);
1775 if ((sq = rx_FreeSQEList)) {
1776 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1777 MUTEX_EXIT(&freeSQEList_lock);
1778 } else { /* otherwise allocate a new one and return that */
1779 MUTEX_EXIT(&freeSQEList_lock);
1780 sq = (struct rx_serverQueueEntry *)
1781 rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1782 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1783 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1785 MUTEX_ENTER(&sq->lock);
1787 if (cur_service != NULL) {
1788 cur_service->nRequestsRunning--;
1789 if (cur_service->nRequestsRunning < cur_service->minProcs)
1793 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1794 struct rx_call *tcall, *ncall;
1795 /* Scan for eligible incoming calls. A call is not eligible
1796 * if the maximum number of calls for its service type are
1797 * already executing */
1798 /* One thread will process calls FCFS (to prevent starvation),
1799 * while the other threads may run ahead looking for calls which
1800 * have all their input data available immediately. This helps
1801 * keep threads from blocking, waiting for data from the client. */
1802 choice2 = (struct rx_call *)0;
1803 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1804 service = tcall->conn->service;
1805 if (QuotaOK(service)) {
1806 MUTEX_ENTER(&rx_pthread_mutex);
1807 if (tno == rxi_fcfs_thread_num
1808 || !tcall->queue_item_header.next) {
1809 MUTEX_EXIT(&rx_pthread_mutex);
1810 /* If we're the fcfs thread, then we'll just use
1811 * this call. If we haven't been able to find an optimal
1812 * choice, and we're at the end of the list, then use a
1813 * 2d choice if one has been identified. Otherwise... */
1814 call = (choice2 ? choice2 : tcall);
1815 service = call->conn->service;
1817 MUTEX_EXIT(&rx_pthread_mutex);
1818 if (!queue_IsEmpty(&tcall->rq)) {
1819 struct rx_packet *rp;
1820 rp = queue_First(&tcall->rq, rx_packet);
1821 if (rp->header.seq == 1
1823 || (rp->header.flags & RX_LAST_PACKET))) {
1825 } else if (rxi_2dchoice && !choice2
1826 && !(tcall->flags & RX_CALL_CLEARED)
1827 && (tcall->rprev > rxi_HardAckRate)) {
1841 /* we can't schedule a call if there's no data!!! */
1842 /* send an ack if there's no data, if we're missing the
1843 * first packet, or we're missing something between first
1844 * and last -- there's a "hole" in the incoming data. */
1845 if (queue_IsEmpty(&call->rq)
1846 || queue_First(&call->rq, rx_packet)->header.seq != 1
1847 || call->rprev != queue_Last(&call->rq, rx_packet)->header.seq)
1848 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1850 call->flags &= (~RX_CALL_WAIT_PROC);
1851 service->nRequestsRunning++;
1852 /* just started call in minProcs pool, need fewer to maintain
1854 if (service->nRequestsRunning <= service->minProcs)
1858 /* MUTEX_EXIT(&call->lock); */
1860 /* If there are no eligible incoming calls, add this process
1861 * to the idle server queue, to wait for one */
1864 *socketp = OSI_NULLSOCKET;
1866 sq->socketp = socketp;
1867 queue_Append(&rx_idleServerQueue, sq);
1871 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1873 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
1874 return (struct rx_call *)0;
1877 } while (!(call = sq->newcall)
1878 && !(socketp && *socketp != OSI_NULLSOCKET));
1880 MUTEX_EXIT(&sq->lock);
1882 MUTEX_ENTER(&freeSQEList_lock);
1883 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1884 rx_FreeSQEList = sq;
1885 MUTEX_EXIT(&freeSQEList_lock);
1888 clock_GetTime(&call->startTime);
1889 call->state = RX_STATE_ACTIVE;
1890 call->mode = RX_MODE_RECEIVING;
1891 #ifdef RX_KERNEL_TRACE
1892 if (ICL_SETACTIVE(afs_iclSetp)) {
1893 int glockOwner = ISAFS_GLOCK();
1896 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1897 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1904 rxi_calltrace(RX_CALL_START, call);
1905 dpf(("rx_GetCall(port=%d, service=%d) ==> call %p\n",
1906 call->conn->service->servicePort, call->conn->service->serviceId,
1909 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
1916 #endif /* RX_ENABLE_LOCKS */
1920 /* Establish a procedure to be called when a packet arrives for a
1921 * call. This routine will be called at most once after each call,
1922 * and will also be called if there is an error condition on the or
1923 * the call is complete. Used by multi rx to build a selection
1924 * function which determines which of several calls is likely to be a
1925 * good one to read from.
1926 * NOTE: the way this is currently implemented it is probably only a
1927 * good idea to (1) use it immediately after a newcall (clients only)
1928 * and (2) only use it once. Other uses currently void your warranty
1931 rx_SetArrivalProc(struct rx_call *call,
1932 void (*proc) (struct rx_call * call,
1935 void * handle, int arg)
1937 call->arrivalProc = proc;
1938 call->arrivalProcHandle = handle;
1939 call->arrivalProcArg = arg;
1942 /* Call is finished (possibly prematurely). Return rc to the peer, if
1943 * appropriate, and return the final error code from the conversation
1947 rx_EndCall(struct rx_call *call, afs_int32 rc)
1949 struct rx_connection *conn = call->conn;
1950 struct rx_service *service;
1954 dpf(("rx_EndCall(call %"AFS_PTR_FMT" rc %d error %d abortCode %d)\n",
1955 call, rc, call->error, call->abortCode));
1958 MUTEX_ENTER(&call->lock);
1960 if (rc == 0 && call->error == 0) {
1961 call->abortCode = 0;
1962 call->abortCount = 0;
1965 call->arrivalProc = (void (*)())0;
1966 if (rc && call->error == 0) {
1967 rxi_CallError(call, rc);
1968 /* Send an abort message to the peer if this error code has
1969 * only just been set. If it was set previously, assume the
1970 * peer has already been sent the error code or will request it
1972 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1974 if (conn->type == RX_SERVER_CONNECTION) {
1975 /* Make sure reply or at least dummy reply is sent */
1976 if (call->mode == RX_MODE_RECEIVING) {
1977 rxi_WriteProc(call, 0, 0);
1979 if (call->mode == RX_MODE_SENDING) {
1980 rxi_FlushWrite(call);
1982 service = conn->service;
1983 rxi_calltrace(RX_CALL_END, call);
1984 /* Call goes to hold state until reply packets are acknowledged */
1985 if (call->tfirst + call->nSoftAcked < call->tnext) {
1986 call->state = RX_STATE_HOLD;
1988 call->state = RX_STATE_DALLY;
1989 rxi_ClearTransmitQueue(call, 0);
1990 rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
1991 rxevent_Cancel(call->keepAliveEvent, call,
1992 RX_CALL_REFCOUNT_ALIVE);
1994 } else { /* Client connection */
1996 /* Make sure server receives input packets, in the case where
1997 * no reply arguments are expected */
1998 if ((call->mode == RX_MODE_SENDING)
1999 || (call->mode == RX_MODE_RECEIVING && call->rnext == 1)) {
2000 (void)rxi_ReadProc(call, &dummy, 1);
2003 /* If we had an outstanding delayed ack, be nice to the server
2004 * and force-send it now.
2006 if (call->delayedAckEvent) {
2007 rxevent_Cancel(call->delayedAckEvent, call,
2008 RX_CALL_REFCOUNT_DELAY);
2009 call->delayedAckEvent = NULL;
2010 rxi_SendDelayedAck(NULL, call, NULL);
2013 /* We need to release the call lock since it's lower than the
2014 * conn_call_lock and we don't want to hold the conn_call_lock
2015 * over the rx_ReadProc call. The conn_call_lock needs to be held
2016 * here for the case where rx_NewCall is perusing the calls on
2017 * the connection structure. We don't want to signal until
2018 * rx_NewCall is in a stable state. Otherwise, rx_NewCall may
2019 * have checked this call, found it active and by the time it
2020 * goes to sleep, will have missed the signal.
2022 MUTEX_ENTER(&conn->conn_data_lock);
2023 conn->flags |= RX_CONN_BUSY;
2024 if (conn->flags & RX_CONN_MAKECALL_WAITING) {
2025 MUTEX_EXIT(&conn->conn_data_lock);
2026 #ifdef RX_ENABLE_LOCKS
2027 CV_BROADCAST(&conn->conn_call_cv);
2032 #ifdef RX_ENABLE_LOCKS
2034 MUTEX_EXIT(&conn->conn_data_lock);
2036 #endif /* RX_ENABLE_LOCKS */
2037 call->state = RX_STATE_DALLY;
2039 error = call->error;
2041 /* currentPacket, nLeft, and NFree must be zeroed here, because
2042 * ResetCall cannot: ResetCall may be called at splnet(), in the
2043 * kernel version, and may interrupt the macros rx_Read or
2044 * rx_Write, which run at normal priority for efficiency. */
2045 if (call->currentPacket) {
2046 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
2047 rxi_FreePacket(call->currentPacket);
2048 call->currentPacket = (struct rx_packet *)0;
2051 call->nLeft = call->nFree = call->curlen = 0;
2053 /* Free any packets from the last call to ReadvProc/WritevProc */
2054 #ifdef RXDEBUG_PACKET
2056 #endif /* RXDEBUG_PACKET */
2057 rxi_FreePackets(0, &call->iovq);
2059 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
2060 MUTEX_EXIT(&call->lock);
2061 if (conn->type == RX_CLIENT_CONNECTION) {
2062 conn->flags &= ~RX_CONN_BUSY;
2066 * Map errors to the local host's errno.h format.
2068 error = ntoh_syserr_conv(error);
2072 #if !defined(KERNEL)
2074 /* Call this routine when shutting down a server or client (especially
2075 * clients). This will allow Rx to gracefully garbage collect server
2076 * connections, and reduce the number of retries that a server might
2077 * make to a dead client.
2078 * This is not quite right, since some calls may still be ongoing and
2079 * we can't lock them to destroy them. */
2083 struct rx_connection **conn_ptr, **conn_end;
2087 if (rxinit_status == 1) {
2089 return; /* Already shutdown. */
2091 rxi_DeleteCachedConnections();
2092 if (rx_connHashTable) {
2093 MUTEX_ENTER(&rx_connHashTable_lock);
2094 for (conn_ptr = &rx_connHashTable[0], conn_end =
2095 &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
2097 struct rx_connection *conn, *next;
2098 for (conn = *conn_ptr; conn; conn = next) {
2100 if (conn->type == RX_CLIENT_CONNECTION) {
2101 /* MUTEX_ENTER(&conn->conn_data_lock); when used in kernel */
2103 /* MUTEX_EXIT(&conn->conn_data_lock); when used in kernel */
2104 #ifdef RX_ENABLE_LOCKS
2105 rxi_DestroyConnectionNoLock(conn);
2106 #else /* RX_ENABLE_LOCKS */
2107 rxi_DestroyConnection(conn);
2108 #endif /* RX_ENABLE_LOCKS */
2112 #ifdef RX_ENABLE_LOCKS
2113 while (rx_connCleanup_list) {
2114 struct rx_connection *conn;
2115 conn = rx_connCleanup_list;
2116 rx_connCleanup_list = rx_connCleanup_list->next;
2117 MUTEX_EXIT(&rx_connHashTable_lock);
2118 rxi_CleanupConnection(conn);
2119 MUTEX_ENTER(&rx_connHashTable_lock);
2121 MUTEX_EXIT(&rx_connHashTable_lock);
2122 #endif /* RX_ENABLE_LOCKS */
2127 afs_winsockCleanup();
2135 /* if we wakeup packet waiter too often, can get in loop with two
2136 AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2138 rxi_PacketsUnWait(void)
2140 if (!rx_waitingForPackets) {
2144 if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2145 return; /* still over quota */
2148 rx_waitingForPackets = 0;
2149 #ifdef RX_ENABLE_LOCKS
2150 CV_BROADCAST(&rx_waitingForPackets_cv);
2152 osi_rxWakeup(&rx_waitingForPackets);
2158 /* ------------------Internal interfaces------------------------- */
2160 /* Return this process's service structure for the
2161 * specified socket and service */
2163 rxi_FindService(osi_socket socket, u_short serviceId)
2165 struct rx_service **sp;
2166 for (sp = &rx_services[0]; *sp; sp++) {
2167 if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2173 #ifdef RXDEBUG_PACKET
2174 #ifdef KDUMP_RX_LOCK
2175 static struct rx_call_rx_lock *rx_allCallsp = 0;
2177 static struct rx_call *rx_allCallsp = 0;
2179 #endif /* RXDEBUG_PACKET */
2181 /* Allocate a call structure, for the indicated channel of the
2182 * supplied connection. The mode and state of the call must be set by
2183 * the caller. Returns the call with mutex locked. */
2185 rxi_NewCall(struct rx_connection *conn, int channel)
2187 struct rx_call *call;
2188 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2189 struct rx_call *cp; /* Call pointer temp */
2190 struct rx_call *nxp; /* Next call pointer, for queue_Scan */
2191 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2193 dpf(("rxi_NewCall(conn %"AFS_PTR_FMT", channel %d)\n", conn, channel));
2195 /* Grab an existing call structure, or allocate a new one.
2196 * Existing call structures are assumed to have been left reset by
2198 MUTEX_ENTER(&rx_freeCallQueue_lock);
2200 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2202 * EXCEPT that the TQ might not yet be cleared out.
2203 * Skip over those with in-use TQs.
2206 for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2207 if (!(cp->flags & RX_CALL_TQ_BUSY)) {
2213 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2214 if (queue_IsNotEmpty(&rx_freeCallQueue)) {
2215 call = queue_First(&rx_freeCallQueue, rx_call);
2216 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2218 if (rx_stats_active)
2219 rx_MutexDecrement(rx_stats.nFreeCallStructs, rx_stats_mutex);
2220 MUTEX_EXIT(&rx_freeCallQueue_lock);
2221 MUTEX_ENTER(&call->lock);
2222 CLEAR_CALL_QUEUE_LOCK(call);
2223 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2224 /* Now, if TQ wasn't cleared earlier, do it now. */
2225 rxi_WaitforTQBusy(call);
2226 if (call->flags & RX_CALL_TQ_CLEARME) {
2227 rxi_ClearTransmitQueue(call, 1);
2228 /*queue_Init(&call->tq);*/
2230 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2231 /* Bind the call to its connection structure */
2233 rxi_ResetCall(call, 1);
2236 call = (struct rx_call *)rxi_Alloc(sizeof(struct rx_call));
2237 #ifdef RXDEBUG_PACKET
2238 call->allNextp = rx_allCallsp;
2239 rx_allCallsp = call;
2241 #endif /* RXDEBUG_PACKET */
2242 rx_MutexIncrement(rx_stats.nCallStructs, rx_stats_mutex);
2244 MUTEX_EXIT(&rx_freeCallQueue_lock);
2245 MUTEX_INIT(&call->lock, "call lock", MUTEX_DEFAULT, NULL);
2246 MUTEX_ENTER(&call->lock);
2247 CV_INIT(&call->cv_twind, "call twind", CV_DEFAULT, 0);
2248 CV_INIT(&call->cv_rq, "call rq", CV_DEFAULT, 0);
2249 CV_INIT(&call->cv_tq, "call tq", CV_DEFAULT, 0);
2251 /* Initialize once-only items */
2252 queue_Init(&call->tq);
2253 queue_Init(&call->rq);
2254 queue_Init(&call->iovq);
2255 #ifdef RXDEBUG_PACKET
2256 call->rqc = call->tqc = call->iovqc = 0;
2257 #endif /* RXDEBUG_PACKET */
2258 /* Bind the call to its connection structure (prereq for reset) */
2260 rxi_ResetCall(call, 1);
2262 call->channel = channel;
2263 call->callNumber = &conn->callNumber[channel];
2264 call->rwind = conn->rwind[channel];
2265 call->twind = conn->twind[channel];
2266 /* Note that the next expected call number is retained (in
2267 * conn->callNumber[i]), even if we reallocate the call structure
2269 conn->call[channel] = call;
2270 /* if the channel's never been used (== 0), we should start at 1, otherwise
2271 * the call number is valid from the last time this channel was used */
2272 if (*call->callNumber == 0)
2273 *call->callNumber = 1;
2278 /* A call has been inactive long enough that so we can throw away
2279 * state, including the call structure, which is placed on the call
2281 * Call is locked upon entry.
2282 * haveCTLock set if called from rxi_ReapConnections
2284 #ifdef RX_ENABLE_LOCKS
2286 rxi_FreeCall(struct rx_call *call, int haveCTLock)
2287 #else /* RX_ENABLE_LOCKS */
2289 rxi_FreeCall(struct rx_call *call)
2290 #endif /* RX_ENABLE_LOCKS */
2292 int channel = call->channel;
2293 struct rx_connection *conn = call->conn;
2296 if (call->state == RX_STATE_DALLY || call->state == RX_STATE_HOLD)
2297 (*call->callNumber)++;
2298 rxi_ResetCall(call, 0);
2299 call->conn->call[channel] = (struct rx_call *)0;
2301 MUTEX_ENTER(&rx_freeCallQueue_lock);
2302 SET_CALL_QUEUE_LOCK(call, &rx_freeCallQueue_lock);
2303 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2304 /* A call may be free even though its transmit queue is still in use.
2305 * Since we search the call list from head to tail, put busy calls at
2306 * the head of the list, and idle calls at the tail.
2308 if (call->flags & RX_CALL_TQ_BUSY)
2309 queue_Prepend(&rx_freeCallQueue, call);
2311 queue_Append(&rx_freeCallQueue, call);
2312 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2313 queue_Append(&rx_freeCallQueue, call);
2314 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2315 if (rx_stats_active)
2316 rx_MutexIncrement(rx_stats.nFreeCallStructs, rx_stats_mutex);
2317 MUTEX_EXIT(&rx_freeCallQueue_lock);
2319 /* Destroy the connection if it was previously slated for
2320 * destruction, i.e. the Rx client code previously called
2321 * rx_DestroyConnection (client connections), or
2322 * rxi_ReapConnections called the same routine (server
2323 * connections). Only do this, however, if there are no
2324 * outstanding calls. Note that for fine grain locking, there appears
2325 * to be a deadlock in that rxi_FreeCall has a call locked and
2326 * DestroyConnectionNoLock locks each call in the conn. But note a
2327 * few lines up where we have removed this call from the conn.
2328 * If someone else destroys a connection, they either have no
2329 * call lock held or are going through this section of code.
2331 if (conn->flags & RX_CONN_DESTROY_ME && !(conn->flags & RX_CONN_MAKECALL_WAITING)) {
2332 MUTEX_ENTER(&conn->conn_data_lock);
2334 MUTEX_EXIT(&conn->conn_data_lock);
2335 #ifdef RX_ENABLE_LOCKS
2337 rxi_DestroyConnectionNoLock(conn);
2339 rxi_DestroyConnection(conn);
2340 #else /* RX_ENABLE_LOCKS */
2341 rxi_DestroyConnection(conn);
2342 #endif /* RX_ENABLE_LOCKS */
2346 afs_int32 rxi_Alloccnt = 0, rxi_Allocsize = 0;
2348 rxi_Alloc(size_t size)
2352 if (rx_stats_active)
2353 rx_MutexAdd1Increment2(rxi_Allocsize, (afs_int32)size, rxi_Alloccnt, rx_stats_mutex);
2356 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
2357 afs_osi_Alloc_NoSleep(size);
2362 osi_Panic("rxi_Alloc error");
2368 rxi_Free(void *addr, size_t size)
2370 if (rx_stats_active)
2371 rx_MutexAdd1Decrement2(rxi_Allocsize, -(afs_int32)size, rxi_Alloccnt, rx_stats_mutex);
2372 osi_Free(addr, size);
2376 rxi_SetPeerMtu(afs_uint32 host, afs_uint32 port, int mtu)
2378 struct rx_peer **peer_ptr, **peer_end;
2381 MUTEX_ENTER(&rx_peerHashTable_lock);
2383 for (peer_ptr = &rx_peerHashTable[0], peer_end =
2384 &rx_peerHashTable[rx_hashTableSize]; peer_ptr < peer_end;
2386 struct rx_peer *peer, *next;
2387 for (peer = *peer_ptr; peer; peer = next) {
2389 if (host == peer->host) {
2390 MUTEX_ENTER(&peer->peer_lock);
2391 peer->ifMTU=MIN(mtu, peer->ifMTU);
2392 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2393 MUTEX_EXIT(&peer->peer_lock);
2398 struct rx_peer *peer;
2399 hashIndex = PEER_HASH(host, port);
2400 for (peer = rx_peerHashTable[hashIndex]; peer; peer = peer->next) {
2401 if ((peer->host == host) && (peer->port == port)) {
2402 MUTEX_ENTER(&peer->peer_lock);
2403 peer->ifMTU=MIN(mtu, peer->ifMTU);
2404 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2405 MUTEX_EXIT(&peer->peer_lock);
2409 MUTEX_EXIT(&rx_peerHashTable_lock);
2412 /* Find the peer process represented by the supplied (host,port)
2413 * combination. If there is no appropriate active peer structure, a
2414 * new one will be allocated and initialized
2415 * The origPeer, if set, is a pointer to a peer structure on which the
2416 * refcount will be be decremented. This is used to replace the peer
2417 * structure hanging off a connection structure */
2419 rxi_FindPeer(afs_uint32 host, u_short port,
2420 struct rx_peer *origPeer, int create)
2424 hashIndex = PEER_HASH(host, port);
2425 MUTEX_ENTER(&rx_peerHashTable_lock);
2426 for (pp = rx_peerHashTable[hashIndex]; pp; pp = pp->next) {
2427 if ((pp->host == host) && (pp->port == port))
2432 pp = rxi_AllocPeer(); /* This bzero's *pp */
2433 pp->host = host; /* set here or in InitPeerParams is zero */
2435 MUTEX_INIT(&pp->peer_lock, "peer_lock", MUTEX_DEFAULT, 0);
2436 queue_Init(&pp->congestionQueue);
2437 queue_Init(&pp->rpcStats);
2438 pp->next = rx_peerHashTable[hashIndex];
2439 rx_peerHashTable[hashIndex] = pp;
2440 rxi_InitPeerParams(pp);
2441 if (rx_stats_active)
2442 rx_MutexIncrement(rx_stats.nPeerStructs, rx_stats_mutex);
2449 origPeer->refCount--;
2450 MUTEX_EXIT(&rx_peerHashTable_lock);
2455 /* Find the connection at (host, port) started at epoch, and with the
2456 * given connection id. Creates the server connection if necessary.
2457 * The type specifies whether a client connection or a server
2458 * connection is desired. In both cases, (host, port) specify the
2459 * peer's (host, pair) pair. Client connections are not made
2460 * automatically by this routine. The parameter socket gives the
2461 * socket descriptor on which the packet was received. This is used,
2462 * in the case of server connections, to check that *new* connections
2463 * come via a valid (port, serviceId). Finally, the securityIndex
2464 * parameter must match the existing index for the connection. If a
2465 * server connection is created, it will be created using the supplied
2466 * index, if the index is valid for this service */
2467 struct rx_connection *
2468 rxi_FindConnection(osi_socket socket, afs_int32 host,
2469 u_short port, u_short serviceId, afs_uint32 cid,
2470 afs_uint32 epoch, int type, u_int securityIndex)
2472 int hashindex, flag, i;
2473 struct rx_connection *conn;
2474 hashindex = CONN_HASH(host, port, cid, epoch, type);
2475 MUTEX_ENTER(&rx_connHashTable_lock);
2476 rxLastConn ? (conn = rxLastConn, flag = 0) : (conn =
2477 rx_connHashTable[hashindex],
2480 if ((conn->type == type) && ((cid & RX_CIDMASK) == conn->cid)
2481 && (epoch == conn->epoch)) {
2482 struct rx_peer *pp = conn->peer;
2483 if (securityIndex != conn->securityIndex) {
2484 /* this isn't supposed to happen, but someone could forge a packet
2485 * like this, and there seems to be some CM bug that makes this
2486 * happen from time to time -- in which case, the fileserver
2488 MUTEX_EXIT(&rx_connHashTable_lock);
2489 return (struct rx_connection *)0;
2491 if (pp->host == host && pp->port == port)
2493 if (type == RX_CLIENT_CONNECTION && pp->port == port)
2495 /* So what happens when it's a callback connection? */
2496 if ( /*type == RX_CLIENT_CONNECTION && */
2497 (conn->epoch & 0x80000000))
2501 /* the connection rxLastConn that was used the last time is not the
2502 ** one we are looking for now. Hence, start searching in the hash */
2504 conn = rx_connHashTable[hashindex];
2509 struct rx_service *service;
2510 if (type == RX_CLIENT_CONNECTION) {
2511 MUTEX_EXIT(&rx_connHashTable_lock);
2512 return (struct rx_connection *)0;
2514 service = rxi_FindService(socket, serviceId);
2515 if (!service || (securityIndex >= service->nSecurityObjects)
2516 || (service->securityObjects[securityIndex] == 0)) {
2517 MUTEX_EXIT(&rx_connHashTable_lock);
2518 return (struct rx_connection *)0;
2520 conn = rxi_AllocConnection(); /* This bzero's the connection */
2521 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
2522 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
2523 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
2524 conn->next = rx_connHashTable[hashindex];
2525 rx_connHashTable[hashindex] = conn;
2526 conn->peer = rxi_FindPeer(host, port, 0, 1);
2527 conn->type = RX_SERVER_CONNECTION;
2528 conn->lastSendTime = clock_Sec(); /* don't GC immediately */
2529 conn->epoch = epoch;
2530 conn->cid = cid & RX_CIDMASK;
2531 /* conn->serial = conn->lastSerial = 0; */
2532 /* conn->timeout = 0; */
2533 conn->ackRate = RX_FAST_ACK_RATE;
2534 conn->service = service;
2535 conn->serviceId = serviceId;
2536 conn->securityIndex = securityIndex;
2537 conn->securityObject = service->securityObjects[securityIndex];
2538 conn->nSpecific = 0;
2539 conn->specific = NULL;
2540 rx_SetConnDeadTime(conn, service->connDeadTime);
2541 rx_SetConnIdleDeadTime(conn, service->idleDeadTime);
2542 rx_SetServerConnIdleDeadErr(conn, service->idleDeadErr);
2543 for (i = 0; i < RX_MAXCALLS; i++) {
2544 conn->twind[i] = rx_initSendWindow;
2545 conn->rwind[i] = rx_initReceiveWindow;
2547 /* Notify security object of the new connection */
2548 RXS_NewConnection(conn->securityObject, conn);
2549 /* XXXX Connection timeout? */
2550 if (service->newConnProc)
2551 (*service->newConnProc) (conn);
2552 if (rx_stats_active)
2553 rx_MutexIncrement(rx_stats.nServerConns, rx_stats_mutex);
2556 MUTEX_ENTER(&conn->conn_data_lock);
2558 MUTEX_EXIT(&conn->conn_data_lock);
2560 rxLastConn = conn; /* store this connection as the last conn used */
2561 MUTEX_EXIT(&rx_connHashTable_lock);
2565 /* There are two packet tracing routines available for testing and monitoring
2566 * Rx. One is called just after every packet is received and the other is
2567 * called just before every packet is sent. Received packets, have had their
2568 * headers decoded, and packets to be sent have not yet had their headers
2569 * encoded. Both take two parameters: a pointer to the packet and a sockaddr
2570 * containing the network address. Both can be modified. The return value, if
2571 * non-zero, indicates that the packet should be dropped. */
2573 int (*rx_justReceived) (struct rx_packet *, struct sockaddr_in *) = 0;
2574 int (*rx_almostSent) (struct rx_packet *, struct sockaddr_in *) = 0;
2576 /* A packet has been received off the interface. Np is the packet, socket is
2577 * the socket number it was received from (useful in determining which service
2578 * this packet corresponds to), and (host, port) reflect the host,port of the
2579 * sender. This call returns the packet to the caller if it is finished with
2580 * it, rather than de-allocating it, just as a small performance hack */
2583 rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
2584 afs_uint32 host, u_short port, int *tnop,
2585 struct rx_call **newcallp)
2587 struct rx_call *call;
2588 struct rx_connection *conn;
2590 afs_uint32 currentCallNumber;
2596 struct rx_packet *tnp;
2599 /* We don't print out the packet until now because (1) the time may not be
2600 * accurate enough until now in the lwp implementation (rx_Listener only gets
2601 * the time after the packet is read) and (2) from a protocol point of view,
2602 * this is the first time the packet has been seen */
2603 packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
2604 ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
2605 dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %"AFS_PTR_FMT,
2606 np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
2607 np->header.epoch, np->header.cid, np->header.callNumber,
2608 np->header.seq, np->header.flags, np));
2611 if (np->header.type == RX_PACKET_TYPE_VERSION) {
2612 return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
2615 if (np->header.type == RX_PACKET_TYPE_DEBUG) {
2616 return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
2619 /* If an input tracer function is defined, call it with the packet and
2620 * network address. Note this function may modify its arguments. */
2621 if (rx_justReceived) {
2622 struct sockaddr_in addr;
2624 addr.sin_family = AF_INET;
2625 addr.sin_port = port;
2626 addr.sin_addr.s_addr = host;
2627 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
2628 addr.sin_len = sizeof(addr);
2629 #endif /* AFS_OSF_ENV */
2630 drop = (*rx_justReceived) (np, &addr);
2631 /* drop packet if return value is non-zero */
2634 port = addr.sin_port; /* in case fcn changed addr */
2635 host = addr.sin_addr.s_addr;
2639 /* If packet was not sent by the client, then *we* must be the client */
2640 type = ((np->header.flags & RX_CLIENT_INITIATED) != RX_CLIENT_INITIATED)
2641 ? RX_CLIENT_CONNECTION : RX_SERVER_CONNECTION;
2643 /* Find the connection (or fabricate one, if we're the server & if
2644 * necessary) associated with this packet */
2646 rxi_FindConnection(socket, host, port, np->header.serviceId,
2647 np->header.cid, np->header.epoch, type,
2648 np->header.securityIndex);
2651 /* If no connection found or fabricated, just ignore the packet.
2652 * (An argument could be made for sending an abort packet for
2657 MUTEX_ENTER(&conn->conn_data_lock);
2658 if (conn->maxSerial < np->header.serial)
2659 conn->maxSerial = np->header.serial;
2660 MUTEX_EXIT(&conn->conn_data_lock);
2662 /* If the connection is in an error state, send an abort packet and ignore
2663 * the incoming packet */
2665 /* Don't respond to an abort packet--we don't want loops! */
2666 MUTEX_ENTER(&conn->conn_data_lock);
2667 if (np->header.type != RX_PACKET_TYPE_ABORT)
2668 np = rxi_SendConnectionAbort(conn, np, 1, 0);
2670 MUTEX_EXIT(&conn->conn_data_lock);
2674 /* Check for connection-only requests (i.e. not call specific). */
2675 if (np->header.callNumber == 0) {
2676 switch (np->header.type) {
2677 case RX_PACKET_TYPE_ABORT: {
2678 /* What if the supplied error is zero? */
2679 afs_int32 errcode = ntohl(rx_GetInt32(np, 0));
2680 dpf(("rxi_ReceivePacket ABORT rx_GetInt32 = %d", errcode));
2681 rxi_ConnectionError(conn, errcode);
2682 MUTEX_ENTER(&conn->conn_data_lock);
2684 MUTEX_EXIT(&conn->conn_data_lock);
2687 case RX_PACKET_TYPE_CHALLENGE:
2688 tnp = rxi_ReceiveChallengePacket(conn, np, 1);
2689 MUTEX_ENTER(&conn->conn_data_lock);
2691 MUTEX_EXIT(&conn->conn_data_lock);
2693 case RX_PACKET_TYPE_RESPONSE:
2694 tnp = rxi_ReceiveResponsePacket(conn, np, 1);
2695 MUTEX_ENTER(&conn->conn_data_lock);
2697 MUTEX_EXIT(&conn->conn_data_lock);
2699 case RX_PACKET_TYPE_PARAMS:
2700 case RX_PACKET_TYPE_PARAMS + 1:
2701 case RX_PACKET_TYPE_PARAMS + 2:
2702 /* ignore these packet types for now */
2703 MUTEX_ENTER(&conn->conn_data_lock);
2705 MUTEX_EXIT(&conn->conn_data_lock);
2710 /* Should not reach here, unless the peer is broken: send an
2712 rxi_ConnectionError(conn, RX_PROTOCOL_ERROR);
2713 MUTEX_ENTER(&conn->conn_data_lock);
2714 tnp = rxi_SendConnectionAbort(conn, np, 1, 0);
2716 MUTEX_EXIT(&conn->conn_data_lock);
2721 channel = np->header.cid & RX_CHANNELMASK;
2722 call = conn->call[channel];
2723 #ifdef RX_ENABLE_LOCKS
2725 MUTEX_ENTER(&call->lock);
2726 /* Test to see if call struct is still attached to conn. */
2727 if (call != conn->call[channel]) {
2729 MUTEX_EXIT(&call->lock);
2730 if (type == RX_SERVER_CONNECTION) {
2731 call = conn->call[channel];
2732 /* If we started with no call attached and there is one now,
2733 * another thread is also running this routine and has gotten
2734 * the connection channel. We should drop this packet in the tests
2735 * below. If there was a call on this connection and it's now
2736 * gone, then we'll be making a new call below.
2737 * If there was previously a call and it's now different then
2738 * the old call was freed and another thread running this routine
2739 * has created a call on this channel. One of these two threads
2740 * has a packet for the old call and the code below handles those
2744 MUTEX_ENTER(&call->lock);
2746 /* This packet can't be for this call. If the new call address is
2747 * 0 then no call is running on this channel. If there is a call
2748 * then, since this is a client connection we're getting data for
2749 * it must be for the previous call.
2751 if (rx_stats_active)
2752 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2753 MUTEX_ENTER(&conn->conn_data_lock);
2755 MUTEX_EXIT(&conn->conn_data_lock);
2760 currentCallNumber = conn->callNumber[channel];
2762 if (type == RX_SERVER_CONNECTION) { /* We're the server */
2763 if (np->header.callNumber < currentCallNumber) {
2764 if (rx_stats_active)
2765 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2766 #ifdef RX_ENABLE_LOCKS
2768 MUTEX_EXIT(&call->lock);
2770 MUTEX_ENTER(&conn->conn_data_lock);
2772 MUTEX_EXIT(&conn->conn_data_lock);
2776 MUTEX_ENTER(&conn->conn_call_lock);
2777 call = rxi_NewCall(conn, channel);
2778 MUTEX_EXIT(&conn->conn_call_lock);
2779 *call->callNumber = np->header.callNumber;
2781 if (np->header.callNumber == 0)
2782 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%.06d len %d",
2783 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
2784 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
2785 np->header.flags, np, np->retryTime.sec, np->retryTime.usec / 1000, np->length));
2787 call->state = RX_STATE_PRECALL;
2788 clock_GetTime(&call->queueTime);
2789 hzero(call->bytesSent);
2790 hzero(call->bytesRcvd);
2792 * If the number of queued calls exceeds the overload
2793 * threshold then abort this call.
2795 if ((rx_BusyThreshold > 0) && (rx_nWaiting > rx_BusyThreshold)) {
2796 struct rx_packet *tp;
2798 rxi_CallError(call, rx_BusyError);
2799 tp = rxi_SendCallAbort(call, np, 1, 0);
2800 MUTEX_EXIT(&call->lock);
2801 MUTEX_ENTER(&conn->conn_data_lock);
2803 MUTEX_EXIT(&conn->conn_data_lock);
2804 if (rx_stats_active)
2805 rx_MutexIncrement(rx_stats.nBusies, rx_stats_mutex);
2808 rxi_KeepAliveOn(call);
2809 } else if (np->header.callNumber != currentCallNumber) {
2810 /* Wait until the transmit queue is idle before deciding
2811 * whether to reset the current call. Chances are that the
2812 * call will be in ether DALLY or HOLD state once the TQ_BUSY
2815 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2816 while ((call->state == RX_STATE_ACTIVE)
2817 && (call->flags & RX_CALL_TQ_BUSY)) {
2818 call->flags |= RX_CALL_TQ_WAIT;
2820 #ifdef RX_ENABLE_LOCKS
2821 osirx_AssertMine(&call->lock, "rxi_Start lock3");
2822 CV_WAIT(&call->cv_tq, &call->lock);
2823 #else /* RX_ENABLE_LOCKS */
2824 osi_rxSleep(&call->tq);
2825 #endif /* RX_ENABLE_LOCKS */
2827 if (call->tqWaiters == 0)
2828 call->flags &= ~RX_CALL_TQ_WAIT;
2830 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2831 /* If the new call cannot be taken right now send a busy and set
2832 * the error condition in this call, so that it terminates as
2833 * quickly as possible */
2834 if (call->state == RX_STATE_ACTIVE) {
2835 struct rx_packet *tp;
2837 rxi_CallError(call, RX_CALL_DEAD);
2838 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
2840 MUTEX_EXIT(&call->lock);
2841 MUTEX_ENTER(&conn->conn_data_lock);
2843 MUTEX_EXIT(&conn->conn_data_lock);
2846 rxi_ResetCall(call, 0);
2847 *call->callNumber = np->header.callNumber;
2849 if (np->header.callNumber == 0)
2850 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" resend %d.%06d len %d",
2851 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
2852 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
2853 np->header.flags, np, np->retryTime.sec, np->retryTime.usec, np->length));
2855 call->state = RX_STATE_PRECALL;
2856 clock_GetTime(&call->queueTime);
2857 hzero(call->bytesSent);
2858 hzero(call->bytesRcvd);
2860 * If the number of queued calls exceeds the overload
2861 * threshold then abort this call.
2863 if ((rx_BusyThreshold > 0) && (rx_nWaiting > rx_BusyThreshold)) {
2864 struct rx_packet *tp;
2866 rxi_CallError(call, rx_BusyError);
2867 tp = rxi_SendCallAbort(call, np, 1, 0);
2868 MUTEX_EXIT(&call->lock);
2869 MUTEX_ENTER(&conn->conn_data_lock);
2871 MUTEX_EXIT(&conn->conn_data_lock);
2872 if (rx_stats_active)
2873 rx_MutexIncrement(rx_stats.nBusies, rx_stats_mutex);
2876 rxi_KeepAliveOn(call);
2878 /* Continuing call; do nothing here. */
2880 } else { /* we're the client */
2881 /* Ignore all incoming acknowledgements for calls in DALLY state */
2882 if (call && (call->state == RX_STATE_DALLY)
2883 && (np->header.type == RX_PACKET_TYPE_ACK)) {
2884 if (rx_stats_active)
2885 rx_MutexIncrement(rx_stats.ignorePacketDally, rx_stats_mutex);
2886 #ifdef RX_ENABLE_LOCKS
2888 MUTEX_EXIT(&call->lock);
2891 MUTEX_ENTER(&conn->conn_data_lock);
2893 MUTEX_EXIT(&conn->conn_data_lock);
2897 /* Ignore anything that's not relevant to the current call. If there
2898 * isn't a current call, then no packet is relevant. */
2899 if (!call || (np->header.callNumber != currentCallNumber)) {
2900 if (rx_stats_active)
2901 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2902 #ifdef RX_ENABLE_LOCKS
2904 MUTEX_EXIT(&call->lock);
2907 MUTEX_ENTER(&conn->conn_data_lock);
2909 MUTEX_EXIT(&conn->conn_data_lock);
2912 /* If the service security object index stamped in the packet does not
2913 * match the connection's security index, ignore the packet */
2914 if (np->header.securityIndex != conn->securityIndex) {
2915 #ifdef RX_ENABLE_LOCKS
2916 MUTEX_EXIT(&call->lock);
2918 MUTEX_ENTER(&conn->conn_data_lock);
2920 MUTEX_EXIT(&conn->conn_data_lock);
2924 /* If we're receiving the response, then all transmit packets are
2925 * implicitly acknowledged. Get rid of them. */
2926 if (np->header.type == RX_PACKET_TYPE_DATA) {
2927 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2928 /* XXX Hack. Because we must release the global rx lock when
2929 * sending packets (osi_NetSend) we drop all acks while we're
2930 * traversing the tq in rxi_Start sending packets out because
2931 * packets may move to the freePacketQueue as result of being here!
2932 * So we drop these packets until we're safely out of the
2933 * traversing. Really ugly!
2934 * For fine grain RX locking, we set the acked field in the
2935 * packets and let rxi_Start remove them from the transmit queue.
2937 if (call->flags & RX_CALL_TQ_BUSY) {
2938 #ifdef RX_ENABLE_LOCKS
2939 rxi_SetAcksInTransmitQueue(call);
2942 return np; /* xmitting; drop packet */
2945 rxi_ClearTransmitQueue(call, 0);
2947 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2948 rxi_ClearTransmitQueue(call, 0);
2949 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2951 if (np->header.type == RX_PACKET_TYPE_ACK) {
2952 /* now check to see if this is an ack packet acknowledging that the
2953 * server actually *lost* some hard-acked data. If this happens we
2954 * ignore this packet, as it may indicate that the server restarted in
2955 * the middle of a call. It is also possible that this is an old ack
2956 * packet. We don't abort the connection in this case, because this
2957 * *might* just be an old ack packet. The right way to detect a server
2958 * restart in the midst of a call is to notice that the server epoch
2960 /* XXX I'm not sure this is exactly right, since tfirst **IS**
2961 * XXX unacknowledged. I think that this is off-by-one, but
2962 * XXX I don't dare change it just yet, since it will
2963 * XXX interact badly with the server-restart detection
2964 * XXX code in receiveackpacket. */
2965 if (ntohl(rx_GetInt32(np, FIRSTACKOFFSET)) < call->tfirst) {
2966 if (rx_stats_active)
2967 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2968 MUTEX_EXIT(&call->lock);
2969 MUTEX_ENTER(&conn->conn_data_lock);
2971 MUTEX_EXIT(&conn->conn_data_lock);
2975 } /* else not a data packet */
2978 osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
2979 /* Set remote user defined status from packet */
2980 call->remoteStatus = np->header.userStatus;
2982 /* Note the gap between the expected next packet and the actual
2983 * packet that arrived, when the new packet has a smaller serial number
2984 * than expected. Rioses frequently reorder packets all by themselves,
2985 * so this will be quite important with very large window sizes.
2986 * Skew is checked against 0 here to avoid any dependence on the type of
2987 * inPacketSkew (which may be unsigned). In C, -1 > (unsigned) 0 is always
2989 * The inPacketSkew should be a smoothed running value, not just a maximum. MTUXXX
2990 * see CalculateRoundTripTime for an example of how to keep smoothed values.
2991 * I think using a beta of 1/8 is probably appropriate. 93.04.21
2993 MUTEX_ENTER(&conn->conn_data_lock);
2994 skew = conn->lastSerial - np->header.serial;
2995 conn->lastSerial = np->header.serial;
2996 MUTEX_EXIT(&conn->conn_data_lock);
2998 struct rx_peer *peer;
3000 if (skew > peer->inPacketSkew) {
3001 dpf(("*** In skew changed from %d to %d\n",
3002 peer->inPacketSkew, skew));
3003 peer->inPacketSkew = skew;
3007 /* Now do packet type-specific processing */
3008 switch (np->header.type) {
3009 case RX_PACKET_TYPE_DATA:
3010 np = rxi_ReceiveDataPacket(call, np, 1, socket, host, port, tnop,
3013 case RX_PACKET_TYPE_ACK:
3014 /* Respond immediately to ack packets requesting acknowledgement
3016 if (np->header.flags & RX_REQUEST_ACK) {
3018 (void)rxi_SendCallAbort(call, 0, 1, 0);
3020 (void)rxi_SendAck(call, 0, np->header.serial,
3021 RX_ACK_PING_RESPONSE, 1);
3023 np = rxi_ReceiveAckPacket(call, np, 1);
3025 case RX_PACKET_TYPE_ABORT: {
3026 /* An abort packet: reset the call, passing the error up to the user. */
3027 /* What if error is zero? */
3028 /* What if the error is -1? the application will treat it as a timeout. */
3029 afs_int32 errdata = ntohl(*(afs_int32 *) rx_DataOf(np));
3030 dpf(("rxi_ReceivePacket ABORT rx_DataOf = %d", errdata));
3031 rxi_CallError(call, errdata);
3032 MUTEX_EXIT(&call->lock);
3033 MUTEX_ENTER(&conn->conn_data_lock);
3035 MUTEX_EXIT(&conn->conn_data_lock);
3036 return np; /* xmitting; drop packet */
3038 case RX_PACKET_TYPE_BUSY:
3041 case RX_PACKET_TYPE_ACKALL:
3042 /* All packets acknowledged, so we can drop all packets previously
3043 * readied for sending */
3044 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3045 /* XXX Hack. We because we can't release the global rx lock when
3046 * sending packets (osi_NetSend) we drop all ack pkts while we're
3047 * traversing the tq in rxi_Start sending packets out because
3048 * packets may move to the freePacketQueue as result of being
3049 * here! So we drop these packets until we're safely out of the
3050 * traversing. Really ugly!
3051 * For fine grain RX locking, we set the acked field in the packets
3052 * and let rxi_Start remove the packets from the transmit queue.
3054 if (call->flags & RX_CALL_TQ_BUSY) {
3055 #ifdef RX_ENABLE_LOCKS
3056 rxi_SetAcksInTransmitQueue(call);
3058 #else /* RX_ENABLE_LOCKS */
3059 MUTEX_EXIT(&call->lock);
3060 MUTEX_ENTER(&conn->conn_data_lock);
3062 MUTEX_EXIT(&conn->conn_data_lock);
3063 return np; /* xmitting; drop packet */
3064 #endif /* RX_ENABLE_LOCKS */
3066 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3067 rxi_ClearTransmitQueue(call, 0);
3068 rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
3071 /* Should not reach here, unless the peer is broken: send an abort
3073 rxi_CallError(call, RX_PROTOCOL_ERROR);
3074 np = rxi_SendCallAbort(call, np, 1, 0);
3077 /* Note when this last legitimate packet was received, for keep-alive
3078 * processing. Note, we delay getting the time until now in the hope that
3079 * the packet will be delivered to the user before any get time is required
3080 * (if not, then the time won't actually be re-evaluated here). */
3081 call->lastReceiveTime = clock_Sec();
3082 MUTEX_EXIT(&call->lock);
3083 MUTEX_ENTER(&conn->conn_data_lock);
3085 MUTEX_EXIT(&conn->conn_data_lock);
3089 /* return true if this is an "interesting" connection from the point of view
3090 of someone trying to debug the system */
3092 rxi_IsConnInteresting(struct rx_connection *aconn)
3095 struct rx_call *tcall;
3097 if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
3099 for (i = 0; i < RX_MAXCALLS; i++) {
3100 tcall = aconn->call[i];
3102 if ((tcall->state == RX_STATE_PRECALL)
3103 || (tcall->state == RX_STATE_ACTIVE))
3105 if ((tcall->mode == RX_MODE_SENDING)
3106 || (tcall->mode == RX_MODE_RECEIVING))
3114 /* if this is one of the last few packets AND it wouldn't be used by the
3115 receiving call to immediately satisfy a read request, then drop it on
3116 the floor, since accepting it might prevent a lock-holding thread from
3117 making progress in its reading. If a call has been cleared while in
3118 the precall state then ignore all subsequent packets until the call
3119 is assigned to a thread. */
3122 TooLow(struct rx_packet *ap, struct rx_call *acall)
3126 MUTEX_ENTER(&rx_quota_mutex);
3127 if (((ap->header.seq != 1) && (acall->flags & RX_CALL_CLEARED)
3128 && (acall->state == RX_STATE_PRECALL))
3129 || ((rx_nFreePackets < rxi_dataQuota + 2)
3130 && !((ap->header.seq < acall->rnext + rx_initSendWindow)
3131 && (acall->flags & RX_CALL_READER_WAIT)))) {
3134 MUTEX_EXIT(&rx_quota_mutex);
3140 rxi_CheckReachEvent(struct rxevent *event, void *arg1, void *arg2)
3142 struct rx_connection *conn = arg1;
3143 struct rx_call *acall = arg2;
3144 struct rx_call *call = acall;
3145 struct clock when, now;
3148 MUTEX_ENTER(&conn->conn_data_lock);
3149 conn->checkReachEvent = NULL;
3150 waiting = conn->flags & RX_CONN_ATTACHWAIT;
3153 MUTEX_EXIT(&conn->conn_data_lock);
3157 MUTEX_ENTER(&conn->conn_call_lock);
3158 MUTEX_ENTER(&conn->conn_data_lock);
3159 for (i = 0; i < RX_MAXCALLS; i++) {
3160 struct rx_call *tc = conn->call[i];
3161 if (tc && tc->state == RX_STATE_PRECALL) {
3167 /* Indicate that rxi_CheckReachEvent is no longer running by
3168 * clearing the flag. Must be atomic under conn_data_lock to
3169 * avoid a new call slipping by: rxi_CheckConnReach holds
3170 * conn_data_lock while checking RX_CONN_ATTACHWAIT.
3172 conn->flags &= ~RX_CONN_ATTACHWAIT;
3173 MUTEX_EXIT(&conn->conn_data_lock);
3174 MUTEX_EXIT(&conn->conn_call_lock);
3179 MUTEX_ENTER(&call->lock);
3180 rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3182 MUTEX_EXIT(&call->lock);
3184 clock_GetTime(&now);
3186 when.sec += RX_CHECKREACH_TIMEOUT;
3187 MUTEX_ENTER(&conn->conn_data_lock);
3188 if (!conn->checkReachEvent) {
3190 conn->checkReachEvent =
3191 rxevent_PostNow(&when, &now, rxi_CheckReachEvent, conn,
3194 MUTEX_EXIT(&conn->conn_data_lock);
3200 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3202 struct rx_service *service = conn->service;
3203 struct rx_peer *peer = conn->peer;
3204 afs_uint32 now, lastReach;
3206 if (service->checkReach == 0)
3210 MUTEX_ENTER(&peer->peer_lock);
3211 lastReach = peer->lastReachTime;
3212 MUTEX_EXIT(&peer->peer_lock);
3213 if (now - lastReach < RX_CHECKREACH_TTL)
3216 MUTEX_ENTER(&conn->conn_data_lock);
3217 if (conn->flags & RX_CONN_ATTACHWAIT) {
3218 MUTEX_EXIT(&conn->conn_data_lock);
3221 conn->flags |= RX_CONN_ATTACHWAIT;
3222 MUTEX_EXIT(&conn->conn_data_lock);
3223 if (!conn->checkReachEvent)
3224 rxi_CheckReachEvent(NULL, conn, call);
3229 /* try to attach call, if authentication is complete */
3231 TryAttach(struct rx_call *acall, osi_socket socket,
3232 int *tnop, struct rx_call **newcallp,
3235 struct rx_connection *conn = acall->conn;
3237 if (conn->type == RX_SERVER_CONNECTION
3238 && acall->state == RX_STATE_PRECALL) {
3239 /* Don't attach until we have any req'd. authentication. */
3240 if (RXS_CheckAuthentication(conn->securityObject, conn) == 0) {
3241 if (reachOverride || rxi_CheckConnReach(conn, acall) == 0)
3242 rxi_AttachServerProc(acall, socket, tnop, newcallp);
3243 /* Note: this does not necessarily succeed; there
3244 * may not any proc available
3247 rxi_ChallengeOn(acall->conn);
3252 /* A data packet has been received off the interface. This packet is
3253 * appropriate to the call (the call is in the right state, etc.). This
3254 * routine can return a packet to the caller, for re-use */
3257 rxi_ReceiveDataPacket(struct rx_call *call,
3258 struct rx_packet *np, int istack,
3259 osi_socket socket, afs_uint32 host, u_short port,
3260 int *tnop, struct rx_call **newcallp)
3262 int ackNeeded = 0; /* 0 means no, otherwise ack_reason */
3267 afs_uint32 serial=0, flags=0;
3269 struct rx_packet *tnp;
3270 struct clock when, now;
3271 if (rx_stats_active)
3272 rx_MutexIncrement(rx_stats.dataPacketsRead, rx_stats_mutex);
3275 /* If there are no packet buffers, drop this new packet, unless we can find
3276 * packet buffers from inactive calls */
3278 && (rxi_OverQuota(RX_PACKET_CLASS_RECEIVE) || TooLow(np, call))) {
3279 MUTEX_ENTER(&rx_freePktQ_lock);
3280 rxi_NeedMorePackets = TRUE;
3281 MUTEX_EXIT(&rx_freePktQ_lock);
3282 if (rx_stats_active)
3283 rx_MutexIncrement(rx_stats.noPacketBuffersOnRead, rx_stats_mutex);
3284 call->rprev = np->header.serial;
3285 rxi_calltrace(RX_TRACE_DROP, call);
3286 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - quota problems", np));
3288 rxi_ClearReceiveQueue(call);
3289 clock_GetTime(&now);
3291 clock_Add(&when, &rx_softAckDelay);
3292 if (!call->delayedAckEvent
3293 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3294 rxevent_Cancel(call->delayedAckEvent, call,
3295 RX_CALL_REFCOUNT_DELAY);
3296 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3297 call->delayedAckEvent =
3298 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3300 /* we've damaged this call already, might as well do it in. */
3306 * New in AFS 3.5, if the RX_JUMBO_PACKET flag is set then this
3307 * packet is one of several packets transmitted as a single
3308 * datagram. Do not send any soft or hard acks until all packets
3309 * in a jumbogram have been processed. Send negative acks right away.
3311 for (isFirst = 1, tnp = NULL; isFirst || tnp; isFirst = 0) {
3312 /* tnp is non-null when there are more packets in the
3313 * current jumbo gram */
3320 seq = np->header.seq;
3321 serial = np->header.serial;
3322 flags = np->header.flags;
3324 /* If the call is in an error state, send an abort message */
3326 return rxi_SendCallAbort(call, np, istack, 0);
3328 /* The RX_JUMBO_PACKET is set in all but the last packet in each
3329 * AFS 3.5 jumbogram. */
3330 if (flags & RX_JUMBO_PACKET) {
3331 tnp = rxi_SplitJumboPacket(np, host, port, isFirst);
3336 if (np->header.spare != 0) {
3337 MUTEX_ENTER(&call->conn->conn_data_lock);
3338 call->conn->flags |= RX_CONN_USING_PACKET_CKSUM;
3339 MUTEX_EXIT(&call->conn->conn_data_lock);
3342 /* The usual case is that this is the expected next packet */
3343 if (seq == call->rnext) {
3345 /* Check to make sure it is not a duplicate of one already queued */
3346 if (queue_IsNotEmpty(&call->rq)
3347 && queue_First(&call->rq, rx_packet)->header.seq == seq) {
3348 if (rx_stats_active)
3349 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3350 dpf(("packet %"AFS_PTR_FMT" dropped on receipt - duplicate", np));
3351 rxevent_Cancel(call->delayedAckEvent, call,
3352 RX_CALL_REFCOUNT_DELAY);
3353 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3359 /* It's the next packet. Stick it on the receive queue
3360 * for this call. Set newPackets to make sure we wake
3361 * the reader once all packets have been processed */
3362 np->flags |= RX_PKTFLAG_RQ;
3363 queue_Prepend(&call->rq, np);
3364 #ifdef RXDEBUG_PACKET
3366 #endif /* RXDEBUG_PACKET */
3368 np = NULL; /* We can't use this anymore */
3371 /* If an ack is requested then set a flag to make sure we
3372 * send an acknowledgement for this packet */
3373 if (flags & RX_REQUEST_ACK) {
3374 ackNeeded = RX_ACK_REQUESTED;
3377 /* Keep track of whether we have received the last packet */
3378 if (flags & RX_LAST_PACKET) {
3379 call->flags |= RX_CALL_HAVE_LAST;
3383 /* Check whether we have all of the packets for this call */
3384 if (call->flags & RX_CALL_HAVE_LAST) {
3385 afs_uint32 tseq; /* temporary sequence number */
3386 struct rx_packet *tp; /* Temporary packet pointer */
3387 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3389 for (tseq = seq, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3390 if (tseq != tp->header.seq)
3392 if (tp->header.flags & RX_LAST_PACKET) {
3393 call->flags |= RX_CALL_RECEIVE_DONE;
3400 /* Provide asynchronous notification for those who want it
3401 * (e.g. multi rx) */
3402 if (call->arrivalProc) {
3403 (*call->arrivalProc) (call, call->arrivalProcHandle,
3404 call->arrivalProcArg);
3405 call->arrivalProc = (void (*)())0;
3408 /* Update last packet received */
3411 /* If there is no server process serving this call, grab
3412 * one, if available. We only need to do this once. If a
3413 * server thread is available, this thread becomes a server
3414 * thread and the server thread becomes a listener thread. */
3416 TryAttach(call, socket, tnop, newcallp, 0);
3419 /* This is not the expected next packet. */
3421 /* Determine whether this is a new or old packet, and if it's
3422 * a new one, whether it fits into the current receive window.
3423 * Also figure out whether the packet was delivered in sequence.
3424 * We use the prev variable to determine whether the new packet
3425 * is the successor of its immediate predecessor in the
3426 * receive queue, and the missing flag to determine whether
3427 * any of this packets predecessors are missing. */
3429 afs_uint32 prev; /* "Previous packet" sequence number */
3430 struct rx_packet *tp; /* Temporary packet pointer */
3431 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3432 int missing; /* Are any predecessors missing? */
3434 /* If the new packet's sequence number has been sent to the
3435 * application already, then this is a duplicate */
3436 if (seq < call->rnext) {
3437 if (rx_stats_active)
3438 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3439 rxevent_Cancel(call->delayedAckEvent, call,
3440 RX_CALL_REFCOUNT_DELAY);
3441 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3447 /* If the sequence number is greater than what can be
3448 * accomodated by the current window, then send a negative
3449 * acknowledge and drop the packet */
3450 if ((call->rnext + call->rwind) <= seq) {
3451 rxevent_Cancel(call->delayedAckEvent, call,
3452 RX_CALL_REFCOUNT_DELAY);
3453 np = rxi_SendAck(call, np, serial, RX_ACK_EXCEEDS_WINDOW,
3460 /* Look for the packet in the queue of old received packets */
3461 for (prev = call->rnext - 1, missing =
3462 0, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3463 /*Check for duplicate packet */
3464 if (seq == tp->header.seq) {
3465 if (rx_stats_active)
3466 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3467 rxevent_Cancel(call->delayedAckEvent, call,
3468 RX_CALL_REFCOUNT_DELAY);
3469 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE,
3475 /* If we find a higher sequence packet, break out and
3476 * insert the new packet here. */
3477 if (seq < tp->header.seq)
3479 /* Check for missing packet */
3480 if (tp->header.seq != prev + 1) {
3484 prev = tp->header.seq;
3487 /* Keep track of whether we have received the last packet. */
3488 if (flags & RX_LAST_PACKET) {
3489 call->flags |= RX_CALL_HAVE_LAST;
3492 /* It's within the window: add it to the the receive queue.
3493 * tp is left by the previous loop either pointing at the
3494 * packet before which to insert the new packet, or at the
3495 * queue head if the queue is empty or the packet should be
3497 np->flags |= RX_PKTFLAG_RQ;
3498 #ifdef RXDEBUG_PACKET
3500 #endif /* RXDEBUG_PACKET */
3501 queue_InsertBefore(tp, np);
3505 /* Check whether we have all of the packets for this call */
3506 if ((call->flags & RX_CALL_HAVE_LAST)
3507 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
3508 afs_uint32 tseq; /* temporary sequence number */
3511 call->rnext, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3512 if (tseq != tp->header.seq)
3514 if (tp->header.flags & RX_LAST_PACKET) {
3515 call->flags |= RX_CALL_RECEIVE_DONE;
3522 /* We need to send an ack of the packet is out of sequence,
3523 * or if an ack was requested by the peer. */
3524 if (seq != prev + 1 || missing) {
3525 ackNeeded = RX_ACK_OUT_OF_SEQUENCE;
3526 } else if (flags & RX_REQUEST_ACK) {
3527 ackNeeded = RX_ACK_REQUESTED;
3530 /* Acknowledge the last packet for each call */
3531 if (flags & RX_LAST_PACKET) {
3542 * If the receiver is waiting for an iovec, fill the iovec
3543 * using the data from the receive queue */
3544 if (call->flags & RX_CALL_IOVEC_WAIT) {
3545 didHardAck = rxi_FillReadVec(call, serial);
3546 /* the call may have been aborted */
3555 /* Wakeup the reader if any */
3556 if ((call->flags & RX_CALL_READER_WAIT)
3557 && (!(call->flags & RX_CALL_IOVEC_WAIT) || !(call->iovNBytes)
3558 || (call->iovNext >= call->iovMax)
3559 || (call->flags & RX_CALL_RECEIVE_DONE))) {
3560 call->flags &= ~RX_CALL_READER_WAIT;
3561 #ifdef RX_ENABLE_LOCKS
3562 CV_BROADCAST(&call->cv_rq);
3564 osi_rxWakeup(&call->rq);
3570 * Send an ack when requested by the peer, or once every
3571 * rxi_SoftAckRate packets until the last packet has been
3572 * received. Always send a soft ack for the last packet in
3573 * the server's reply. */
3575 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3576 np = rxi_SendAck(call, np, serial, ackNeeded, istack);
3577 } else if (call->nSoftAcks > (u_short) rxi_SoftAckRate) {
3578 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3579 np = rxi_SendAck(call, np, serial, RX_ACK_IDLE, istack);
3580 } else if (call->nSoftAcks) {
3581 clock_GetTime(&now);
3583 if (haveLast && !(flags & RX_CLIENT_INITIATED)) {
3584 clock_Add(&when, &rx_lastAckDelay);
3586 clock_Add(&when, &rx_softAckDelay);
3588 if (!call->delayedAckEvent
3589 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3590 rxevent_Cancel(call->delayedAckEvent, call,
3591 RX_CALL_REFCOUNT_DELAY);
3592 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3593 call->delayedAckEvent =
3594 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3596 } else if (call->flags & RX_CALL_RECEIVE_DONE) {
3597 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3604 static void rxi_ComputeRate();
3608 rxi_UpdatePeerReach(struct rx_connection *conn, struct rx_call *acall)
3610 struct rx_peer *peer = conn->peer;
3612 MUTEX_ENTER(&peer->peer_lock);
3613 peer->lastReachTime = clock_Sec();
3614 MUTEX_EXIT(&peer->peer_lock);
3616 MUTEX_ENTER(&conn->conn_data_lock);
3617 if (conn->flags & RX_CONN_ATTACHWAIT) {
3620 conn->flags &= ~RX_CONN_ATTACHWAIT;
3621 MUTEX_EXIT(&conn->conn_data_lock);
3623 for (i = 0; i < RX_MAXCALLS; i++) {
3624 struct rx_call *call = conn->call[i];
3627 MUTEX_ENTER(&call->lock);
3628 /* tnop can be null if newcallp is null */
3629 TryAttach(call, (osi_socket) - 1, NULL, NULL, 1);
3631 MUTEX_EXIT(&call->lock);
3635 MUTEX_EXIT(&conn->conn_data_lock);
3638 #if defined(RXDEBUG) && defined(AFS_NT40_ENV)
3640 rx_ack_reason(int reason)
3643 case RX_ACK_REQUESTED:
3645 case RX_ACK_DUPLICATE:
3647 case RX_ACK_OUT_OF_SEQUENCE:
3649 case RX_ACK_EXCEEDS_WINDOW:
3651 case RX_ACK_NOSPACE:
3655 case RX_ACK_PING_RESPONSE:
3668 /* rxi_ComputePeerNetStats
3670 * Called exclusively by rxi_ReceiveAckPacket to compute network link
3671 * estimates (like RTT and throughput) based on ack packets. Caller
3672 * must ensure that the packet in question is the right one (i.e.
3673 * serial number matches).
3676 rxi_ComputePeerNetStats(struct rx_call *call, struct rx_packet *p,
3677 struct rx_ackPacket *ap, struct rx_packet *np)
3679 struct rx_peer *peer = call->conn->peer;
3681 /* Use RTT if not delayed by client and
3682 * ignore packets that were retransmitted. */
3683 if (!(p->flags & RX_PKTFLAG_ACKED) &&
3684 ap->reason != RX_ACK_DELAY &&
3685 clock_Eq(&p->timeSent, &p->firstSent))
3686 rxi_ComputeRoundTripTime(p, &p->timeSent, peer);
3688 rxi_ComputeRate(peer, call, p, np, ap->reason);
3692 /* The real smarts of the whole thing. */
3694 rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
3697 struct rx_ackPacket *ap;
3699 struct rx_packet *tp;
3700 struct rx_packet *nxp; /* Next packet pointer for queue_Scan */
3701 struct rx_connection *conn = call->conn;
3702 struct rx_peer *peer = conn->peer;