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>
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
30 #include <net/net_globals.h>
31 #endif /* AFS_OSF_ENV */
32 #ifdef AFS_LINUX20_ENV
35 #include "netinet/in.h"
37 #include "inet/common.h"
39 #include "inet/ip_ire.h"
41 #include "afs/afs_args.h"
42 #include "afs/afs_osi.h"
43 #ifdef RX_KERNEL_TRACE
44 #include "rx_kcommon.h"
46 #if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
50 #undef RXDEBUG /* turn off debugging */
52 #if defined(AFS_SGI_ENV)
53 #include "sys/debug.h"
62 #endif /* AFS_OSF_ENV */
64 #include "afs/sysincludes.h"
65 #include "afsincludes.h"
68 #include "rx_kmutex.h"
69 #include "rx_kernel.h"
73 #include "rx_globals.h"
75 #define AFSOP_STOP_RXCALLBACK 210 /* Stop CALLBACK process */
76 #define AFSOP_STOP_AFS 211 /* Stop AFS process */
77 #define AFSOP_STOP_BKG 212 /* Stop BKG process */
79 extern afs_int32 afs_termState;
81 #include "sys/lockl.h"
82 #include "sys/lock_def.h"
83 #endif /* AFS_AIX41_ENV */
84 # include "rxgen_consts.h"
86 # include <sys/types.h>
93 # include <afs/afsutil.h>
94 # include <WINNT\afsreg.h>
96 # include <sys/socket.h>
97 # include <sys/file.h>
99 # include <sys/stat.h>
100 # include <netinet/in.h>
101 # include <sys/time.h>
104 # include "rx_user.h"
105 # include "rx_clock.h"
106 # include "rx_queue.h"
107 # include "rx_globals.h"
108 # include "rx_trace.h"
109 # include <afs/rxgen_consts.h>
113 #ifdef AFS_PTHREAD_ENV
115 int (*registerProgram) (pid_t, char *) = 0;
116 int (*swapNameProgram) (pid_t, const char *, char *) = 0;
119 int (*registerProgram) (PROCESS, char *) = 0;
120 int (*swapNameProgram) (PROCESS, const char *, char *) = 0;
124 /* Local static routines */
125 static void rxi_DestroyConnectionNoLock(struct rx_connection *conn);
126 #ifdef RX_ENABLE_LOCKS
127 static void rxi_SetAcksInTransmitQueue(struct rx_call *call);
130 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
132 afs_int32 rxi_start_aborted; /* rxi_start awoke after rxi_Send in error. */
133 afs_int32 rxi_start_in_error;
135 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
138 * rxi_rpc_peer_stat_cnt counts the total number of peer stat structures
139 * currently allocated within rx. This number is used to allocate the
140 * memory required to return the statistics when queried.
143 static unsigned int rxi_rpc_peer_stat_cnt;
146 * rxi_rpc_process_stat_cnt counts the total number of local process stat
147 * structures currently allocated within rx. The number is used to allocate
148 * the memory required to return the statistics when queried.
151 static unsigned int rxi_rpc_process_stat_cnt;
153 #if !defined(offsetof)
154 #include <stddef.h> /* for definition of offsetof() */
157 #ifdef AFS_PTHREAD_ENV
161 * Use procedural initialization of mutexes/condition variables
165 extern afs_kmutex_t rx_stats_mutex;
166 extern afs_kmutex_t rx_waiting_mutex;
167 extern afs_kmutex_t rx_quota_mutex;
168 extern afs_kmutex_t rx_pthread_mutex;
169 extern afs_kmutex_t rx_packets_mutex;
170 extern afs_kmutex_t des_init_mutex;
171 extern afs_kmutex_t des_random_mutex;
172 extern afs_kmutex_t rx_clock_mutex;
173 extern afs_kmutex_t rxi_connCacheMutex;
174 extern afs_kmutex_t rx_event_mutex;
175 extern afs_kmutex_t osi_malloc_mutex;
176 extern afs_kmutex_t event_handler_mutex;
177 extern afs_kmutex_t listener_mutex;
178 extern afs_kmutex_t rx_if_init_mutex;
179 extern afs_kmutex_t rx_if_mutex;
180 extern afs_kmutex_t rxkad_client_uid_mutex;
181 extern afs_kmutex_t rxkad_random_mutex;
183 extern afs_kcondvar_t rx_event_handler_cond;
184 extern afs_kcondvar_t rx_listener_cond;
186 static afs_kmutex_t epoch_mutex;
187 static afs_kmutex_t rx_init_mutex;
188 static afs_kmutex_t rx_debug_mutex;
189 static afs_kmutex_t rx_rpc_stats;
192 rxi_InitPthread(void)
194 MUTEX_INIT(&rx_clock_mutex, "clock", MUTEX_DEFAULT, 0);
195 MUTEX_INIT(&rx_stats_mutex, "stats", MUTEX_DEFAULT, 0);
196 MUTEX_INIT(&rx_waiting_mutex, "waiting", MUTEX_DEFAULT, 0);
197 MUTEX_INIT(&rx_quota_mutex, "quota", MUTEX_DEFAULT, 0);
198 MUTEX_INIT(&rx_pthread_mutex, "pthread", MUTEX_DEFAULT, 0);
199 MUTEX_INIT(&rx_packets_mutex, "packets", MUTEX_DEFAULT, 0);
200 MUTEX_INIT(&epoch_mutex, "epoch", MUTEX_DEFAULT, 0);
201 MUTEX_INIT(&rx_init_mutex, "init", MUTEX_DEFAULT, 0);
202 MUTEX_INIT(&rx_event_mutex, "event", MUTEX_DEFAULT, 0);
203 MUTEX_INIT(&des_init_mutex, "des", MUTEX_DEFAULT, 0);
204 MUTEX_INIT(&des_random_mutex, "random", MUTEX_DEFAULT, 0);
205 MUTEX_INIT(&osi_malloc_mutex, "malloc", MUTEX_DEFAULT, 0);
206 MUTEX_INIT(&event_handler_mutex, "event handler", MUTEX_DEFAULT, 0);
207 MUTEX_INIT(&rxi_connCacheMutex, "conn cache", MUTEX_DEFAULT, 0);
208 MUTEX_INIT(&listener_mutex, "listener", MUTEX_DEFAULT, 0);
209 MUTEX_INIT(&rx_if_init_mutex, "if init", MUTEX_DEFAULT, 0);
210 MUTEX_INIT(&rx_if_mutex, "if", MUTEX_DEFAULT, 0);
211 MUTEX_INIT(&rxkad_client_uid_mutex, "uid", MUTEX_DEFAULT, 0);
212 MUTEX_INIT(&rxkad_random_mutex, "rxkad random", MUTEX_DEFAULT, 0);
213 MUTEX_INIT(&rx_debug_mutex, "debug", MUTEX_DEFAULT, 0);
215 assert(pthread_cond_init
216 (&rx_event_handler_cond, (const pthread_condattr_t *)0) == 0);
217 assert(pthread_cond_init(&rx_listener_cond, (const pthread_condattr_t *)0)
219 assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
220 assert(pthread_key_create(&rx_ts_info_key, NULL) == 0);
222 rxkad_global_stats_init();
224 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
225 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
226 #ifdef RX_ENABLE_LOCKS
229 #endif /* RX_LOCKS_DB */
230 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
231 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
233 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
235 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
237 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
239 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
240 MUTEX_INIT(&rxi_keyCreate_lock, "rxi_keyCreate_lock", MUTEX_DEFAULT, 0);
241 #endif /* RX_ENABLE_LOCKS */
244 pthread_once_t rx_once_init = PTHREAD_ONCE_INIT;
245 #define INIT_PTHREAD_LOCKS \
246 assert(pthread_once(&rx_once_init, rxi_InitPthread)==0)
248 * The rx_stats_mutex mutex protects the following global variables:
249 * rxi_lowConnRefCount
250 * rxi_lowPeerRefCount
259 * The rx_quota_mutex mutex protects the following global variables:
267 * The rx_freePktQ_lock protects the following global variables:
272 * The rx_packets_mutex mutex protects the following global variables:
280 * The rx_pthread_mutex mutex protects the following global variables:
284 #define INIT_PTHREAD_LOCKS
288 /* Variables for handling the minProcs implementation. availProcs gives the
289 * number of threads available in the pool at this moment (not counting dudes
290 * executing right now). totalMin gives the total number of procs required
291 * for handling all minProcs requests. minDeficit is a dynamic variable
292 * tracking the # of procs required to satisfy all of the remaining minProcs
294 * For fine grain locking to work, the quota check and the reservation of
295 * a server thread has to come while rxi_availProcs and rxi_minDeficit
296 * are locked. To this end, the code has been modified under #ifdef
297 * RX_ENABLE_LOCKS so that quota checks and reservation occur at the
298 * same time. A new function, ReturnToServerPool() returns the allocation.
300 * A call can be on several queue's (but only one at a time). When
301 * rxi_ResetCall wants to remove the call from a queue, it has to ensure
302 * that no one else is touching the queue. To this end, we store the address
303 * of the queue lock in the call structure (under the call lock) when we
304 * put the call on a queue, and we clear the call_queue_lock when the
305 * call is removed from a queue (once the call lock has been obtained).
306 * This allows rxi_ResetCall to safely synchronize with others wishing
307 * to manipulate the queue.
310 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
311 static afs_kmutex_t rx_rpc_stats;
312 void rxi_StartUnlocked(struct rxevent *event, void *call,
313 void *arg1, int istack);
316 /* We keep a "last conn pointer" in rxi_FindConnection. The odds are
317 ** pretty good that the next packet coming in is from the same connection
318 ** as the last packet, since we're send multiple packets in a transmit window.
320 struct rx_connection *rxLastConn = 0;
322 #ifdef RX_ENABLE_LOCKS
323 /* The locking hierarchy for rx fine grain locking is composed of these
326 * rx_connHashTable_lock - synchronizes conn creation, rx_connHashTable access
327 * conn_call_lock - used to synchonize rx_EndCall and rx_NewCall
328 * call->lock - locks call data fields.
329 * These are independent of each other:
330 * rx_freeCallQueue_lock
335 * serverQueueEntry->lock
337 * rx_peerHashTable_lock - locked under rx_connHashTable_lock
338 * peer->lock - locks peer data fields.
339 * conn_data_lock - that more than one thread is not updating a conn data
340 * field at the same time.
348 * Do we need a lock to protect the peer field in the conn structure?
349 * conn->peer was previously a constant for all intents and so has no
350 * lock protecting this field. The multihomed client delta introduced
351 * a RX code change : change the peer field in the connection structure
352 * to that remote inetrface from which the last packet for this
353 * connection was sent out. This may become an issue if further changes
356 #define SET_CALL_QUEUE_LOCK(C, L) (C)->call_queue_lock = (L)
357 #define CLEAR_CALL_QUEUE_LOCK(C) (C)->call_queue_lock = NULL
359 /* rxdb_fileID is used to identify the lock location, along with line#. */
360 static int rxdb_fileID = RXDB_FILE_RX;
361 #endif /* RX_LOCKS_DB */
362 #else /* RX_ENABLE_LOCKS */
363 #define SET_CALL_QUEUE_LOCK(C, L)
364 #define CLEAR_CALL_QUEUE_LOCK(C)
365 #endif /* RX_ENABLE_LOCKS */
366 struct rx_serverQueueEntry *rx_waitForPacket = 0;
367 struct rx_serverQueueEntry *rx_waitingForPacket = 0;
369 /* ------------Exported Interfaces------------- */
371 /* This function allows rxkad to set the epoch to a suitably random number
372 * which rx_NewConnection will use in the future. The principle purpose is to
373 * get rxnull connections to use the same epoch as the rxkad connections do, at
374 * least once the first rxkad connection is established. This is important now
375 * that the host/port addresses aren't used in FindConnection: the uniqueness
376 * of epoch/cid matters and the start time won't do. */
378 #ifdef AFS_PTHREAD_ENV
380 * This mutex protects the following global variables:
384 #define LOCK_EPOCH MUTEX_ENTER(&epoch_mutex)
385 #define UNLOCK_EPOCH MUTEX_EXIT(&epoch_mutex)
389 #endif /* AFS_PTHREAD_ENV */
392 rx_SetEpoch(afs_uint32 epoch)
399 /* Initialize rx. A port number may be mentioned, in which case this
400 * becomes the default port number for any service installed later.
401 * If 0 is provided for the port number, a random port will be chosen
402 * by the kernel. Whether this will ever overlap anything in
403 * /etc/services is anybody's guess... Returns 0 on success, -1 on
408 int rxinit_status = 1;
409 #ifdef AFS_PTHREAD_ENV
411 * This mutex protects the following global variables:
415 #define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
416 #define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
419 #define UNLOCK_RX_INIT
423 rx_InitHost(u_int host, u_int port)
430 char *htable, *ptable;
437 if (rxinit_status == 0) {
438 tmp_status = rxinit_status;
440 return tmp_status; /* Already started; return previous error code. */
446 if (afs_winsockInit() < 0)
452 * Initialize anything necessary to provide a non-premptive threading
455 rxi_InitializeThreadSupport();
458 /* Allocate and initialize a socket for client and perhaps server
461 rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
462 if (rx_socket == OSI_NULLSOCKET) {
466 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
469 #endif /* RX_LOCKS_DB */
470 MUTEX_INIT(&rx_stats_mutex, "rx_stats_mutex", MUTEX_DEFAULT, 0);
471 MUTEX_INIT(&rx_waiting_mutex, "rx_waiting_mutex", MUTEX_DEFAULT, 0);
472 MUTEX_INIT(&rx_quota_mutex, "rx_quota_mutex", MUTEX_DEFAULT, 0);
473 MUTEX_INIT(&rx_pthread_mutex, "rx_pthread_mutex", MUTEX_DEFAULT, 0);
474 MUTEX_INIT(&rx_packets_mutex, "rx_packets_mutex", MUTEX_DEFAULT, 0);
475 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
476 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
477 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
478 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
480 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
482 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
484 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
486 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
487 #if defined(AFS_HPUX110_ENV)
489 rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
490 #endif /* AFS_HPUX110_ENV */
491 #endif /* RX_ENABLE_LOCKS && KERNEL */
494 rx_connDeadTime = 12;
495 rx_tranquil = 0; /* reset flag */
496 memset((char *)&rx_stats, 0, sizeof(struct rx_statistics));
498 osi_Alloc(rx_hashTableSize * sizeof(struct rx_connection *));
499 PIN(htable, rx_hashTableSize * sizeof(struct rx_connection *)); /* XXXXX */
500 memset(htable, 0, rx_hashTableSize * sizeof(struct rx_connection *));
501 ptable = (char *)osi_Alloc(rx_hashTableSize * sizeof(struct rx_peer *));
502 PIN(ptable, rx_hashTableSize * sizeof(struct rx_peer *)); /* XXXXX */
503 memset(ptable, 0, rx_hashTableSize * sizeof(struct rx_peer *));
505 /* Malloc up a bunch of packets & buffers */
507 queue_Init(&rx_freePacketQueue);
508 rxi_NeedMorePackets = FALSE;
509 #ifdef RX_ENABLE_TSFPQ
510 rx_nPackets = 0; /* in TSFPQ version, rx_nPackets is managed by rxi_MorePackets* */
511 rxi_MorePacketsTSFPQ(rx_extraPackets + RX_MAX_QUOTA + 2, RX_TS_FPQ_FLUSH_GLOBAL, 0);
512 #else /* RX_ENABLE_TSFPQ */
513 rx_nPackets = rx_extraPackets + RX_MAX_QUOTA + 2; /* fudge */
514 rxi_MorePackets(rx_nPackets);
515 #endif /* RX_ENABLE_TSFPQ */
522 #if defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
523 tv.tv_sec = clock_now.sec;
524 tv.tv_usec = clock_now.usec;
525 srand((unsigned int)tv.tv_usec);
532 #if defined(KERNEL) && !defined(UKERNEL)
533 /* Really, this should never happen in a real kernel */
536 struct sockaddr_in addr;
538 int addrlen = sizeof(addr);
540 socklen_t addrlen = sizeof(addr);
542 if (getsockname((int)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
546 rx_port = addr.sin_port;
549 rx_stats.minRtt.sec = 9999999;
551 rx_SetEpoch(tv.tv_sec | 0x80000000);
553 rx_SetEpoch(tv.tv_sec); /* Start time of this package, rxkad
554 * will provide a randomer value. */
556 MUTEX_ENTER(&rx_quota_mutex);
557 rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
558 MUTEX_EXIT(&rx_quota_mutex);
559 /* *Slightly* random start time for the cid. This is just to help
560 * out with the hashing function at the peer */
561 rx_nextCid = ((tv.tv_sec ^ tv.tv_usec) << RX_CIDSHIFT);
562 rx_connHashTable = (struct rx_connection **)htable;
563 rx_peerHashTable = (struct rx_peer **)ptable;
565 rx_lastAckDelay.sec = 0;
566 rx_lastAckDelay.usec = 400000; /* 400 milliseconds */
567 rx_hardAckDelay.sec = 0;
568 rx_hardAckDelay.usec = 100000; /* 100 milliseconds */
569 rx_softAckDelay.sec = 0;
570 rx_softAckDelay.usec = 100000; /* 100 milliseconds */
572 rxevent_Init(20, rxi_ReScheduleEvents);
574 /* Initialize various global queues */
575 queue_Init(&rx_idleServerQueue);
576 queue_Init(&rx_incomingCallQueue);
577 queue_Init(&rx_freeCallQueue);
579 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
580 /* Initialize our list of usable IP addresses. */
584 /* Start listener process (exact function is dependent on the
585 * implementation environment--kernel or user space) */
589 tmp_status = rxinit_status = 0;
597 return rx_InitHost(htonl(INADDR_ANY), port);
600 /* called with unincremented nRequestsRunning to see if it is OK to start
601 * a new thread in this service. Could be "no" for two reasons: over the
602 * max quota, or would prevent others from reaching their min quota.
604 #ifdef RX_ENABLE_LOCKS
605 /* This verion of QuotaOK reserves quota if it's ok while the
606 * rx_serverPool_lock is held. Return quota using ReturnToServerPool().
609 QuotaOK(struct rx_service *aservice)
611 /* check if over max quota */
612 if (aservice->nRequestsRunning >= aservice->maxProcs) {
616 /* under min quota, we're OK */
617 /* otherwise, can use only if there are enough to allow everyone
618 * to go to their min quota after this guy starts.
621 MUTEX_ENTER(&rx_quota_mutex);
622 if ((aservice->nRequestsRunning < aservice->minProcs)
623 || (rxi_availProcs > rxi_minDeficit)) {
624 aservice->nRequestsRunning++;
625 /* just started call in minProcs pool, need fewer to maintain
627 if (aservice->nRequestsRunning <= aservice->minProcs)
630 MUTEX_EXIT(&rx_quota_mutex);
633 MUTEX_EXIT(&rx_quota_mutex);
639 ReturnToServerPool(struct rx_service *aservice)
641 aservice->nRequestsRunning--;
642 MUTEX_ENTER(&rx_quota_mutex);
643 if (aservice->nRequestsRunning < aservice->minProcs)
646 MUTEX_EXIT(&rx_quota_mutex);
649 #else /* RX_ENABLE_LOCKS */
651 QuotaOK(struct rx_service *aservice)
654 /* under min quota, we're OK */
655 if (aservice->nRequestsRunning < aservice->minProcs)
658 /* check if over max quota */
659 if (aservice->nRequestsRunning >= aservice->maxProcs)
662 /* otherwise, can use only if there are enough to allow everyone
663 * to go to their min quota after this guy starts.
665 if (rxi_availProcs > rxi_minDeficit)
669 #endif /* RX_ENABLE_LOCKS */
672 /* Called by rx_StartServer to start up lwp's to service calls.
673 NExistingProcs gives the number of procs already existing, and which
674 therefore needn't be created. */
676 rxi_StartServerProcs(int nExistingProcs)
678 struct rx_service *service;
683 /* For each service, reserve N processes, where N is the "minimum"
684 * number of processes that MUST be able to execute a request in parallel,
685 * at any time, for that process. Also compute the maximum difference
686 * between any service's maximum number of processes that can run
687 * (i.e. the maximum number that ever will be run, and a guarantee
688 * that this number will run if other services aren't running), and its
689 * minimum number. The result is the extra number of processes that
690 * we need in order to provide the latter guarantee */
691 for (i = 0; i < RX_MAX_SERVICES; i++) {
693 service = rx_services[i];
694 if (service == (struct rx_service *)0)
696 nProcs += service->minProcs;
697 diff = service->maxProcs - service->minProcs;
701 nProcs += maxdiff; /* Extra processes needed to allow max number requested to run in any given service, under good conditions */
702 nProcs -= nExistingProcs; /* Subtract the number of procs that were previously created for use as server procs */
703 for (i = 0; i < nProcs; i++) {
704 rxi_StartServerProc(rx_ServerProc, rx_stackSize);
710 /* This routine is only required on Windows */
712 rx_StartClientThread(void)
714 #ifdef AFS_PTHREAD_ENV
716 pid = pthread_self();
717 #endif /* AFS_PTHREAD_ENV */
719 #endif /* AFS_NT40_ENV */
721 /* This routine must be called if any services are exported. If the
722 * donateMe flag is set, the calling process is donated to the server
725 rx_StartServer(int donateMe)
727 struct rx_service *service;
733 /* Start server processes, if necessary (exact function is dependent
734 * on the implementation environment--kernel or user space). DonateMe
735 * will be 1 if there is 1 pre-existing proc, i.e. this one. In this
736 * case, one less new proc will be created rx_StartServerProcs.
738 rxi_StartServerProcs(donateMe);
740 /* count up the # of threads in minProcs, and add set the min deficit to
741 * be that value, too.
743 for (i = 0; i < RX_MAX_SERVICES; i++) {
744 service = rx_services[i];
745 if (service == (struct rx_service *)0)
747 MUTEX_ENTER(&rx_quota_mutex);
748 rxi_totalMin += service->minProcs;
749 /* below works even if a thread is running, since minDeficit would
750 * still have been decremented and later re-incremented.
752 rxi_minDeficit += service->minProcs;
753 MUTEX_EXIT(&rx_quota_mutex);
756 /* Turn on reaping of idle server connections */
757 rxi_ReapConnections(NULL, NULL, NULL);
766 #ifdef AFS_PTHREAD_ENV
768 pid = (pid_t) pthread_self();
769 #else /* AFS_PTHREAD_ENV */
771 LWP_CurrentProcess(&pid);
772 #endif /* AFS_PTHREAD_ENV */
774 sprintf(name, "srv_%d", ++nProcs);
776 (*registerProgram) (pid, name);
778 #endif /* AFS_NT40_ENV */
779 rx_ServerProc(NULL); /* Never returns */
781 #ifdef RX_ENABLE_TSFPQ
782 /* no use leaving packets around in this thread's local queue if
783 * it isn't getting donated to the server thread pool.
785 rxi_FlushLocalPacketsTSFPQ();
786 #endif /* RX_ENABLE_TSFPQ */
790 /* Create a new client connection to the specified service, using the
791 * specified security object to implement the security model for this
793 struct rx_connection *
794 rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
795 struct rx_securityClass *securityObject,
796 int serviceSecurityIndex)
800 struct rx_connection *conn;
805 dpf(("rx_NewConnection(host %x, port %u, service %u, securityObject %x, serviceSecurityIndex %d)\n", ntohl(shost), ntohs(sport), sservice, securityObject, serviceSecurityIndex));
807 /* Vasilsi said: "NETPRI protects Cid and Alloc", but can this be true in
808 * the case of kmem_alloc? */
809 conn = rxi_AllocConnection();
810 #ifdef RX_ENABLE_LOCKS
811 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
812 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
813 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
816 MUTEX_ENTER(&rx_connHashTable_lock);
817 cid = (rx_nextCid += RX_MAXCALLS);
818 conn->type = RX_CLIENT_CONNECTION;
820 conn->epoch = rx_epoch;
821 conn->peer = rxi_FindPeer(shost, sport, 0, 1);
822 conn->serviceId = sservice;
823 conn->securityObject = securityObject;
824 conn->securityData = (void *) 0;
825 conn->securityIndex = serviceSecurityIndex;
826 rx_SetConnDeadTime(conn, rx_connDeadTime);
827 conn->ackRate = RX_FAST_ACK_RATE;
829 conn->specific = NULL;
830 conn->challengeEvent = NULL;
831 conn->delayedAbortEvent = NULL;
832 conn->abortCount = 0;
834 for (i = 0; i < RX_MAXCALLS; i++) {
835 conn->twind[i] = rx_initSendWindow;
836 conn->rwind[i] = rx_initReceiveWindow;
839 RXS_NewConnection(securityObject, conn);
841 CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
843 conn->refCount++; /* no lock required since only this thread knows... */
844 conn->next = rx_connHashTable[hashindex];
845 rx_connHashTable[hashindex] = conn;
847 rx_MutexIncrement(rx_stats.nClientConns, rx_stats_mutex);
848 MUTEX_EXIT(&rx_connHashTable_lock);
854 rx_SetConnDeadTime(struct rx_connection *conn, int seconds)
856 /* The idea is to set the dead time to a value that allows several
857 * keepalives to be dropped without timing out the connection. */
858 conn->secondsUntilDead = MAX(seconds, 6);
859 conn->secondsUntilPing = conn->secondsUntilDead / 6;
862 int rxi_lowPeerRefCount = 0;
863 int rxi_lowConnRefCount = 0;
866 * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
867 * NOTE: must not be called with rx_connHashTable_lock held.
870 rxi_CleanupConnection(struct rx_connection *conn)
872 /* Notify the service exporter, if requested, that this connection
873 * is being destroyed */
874 if (conn->type == RX_SERVER_CONNECTION && conn->service->destroyConnProc)
875 (*conn->service->destroyConnProc) (conn);
877 /* Notify the security module that this connection is being destroyed */
878 RXS_DestroyConnection(conn->securityObject, conn);
880 /* If this is the last connection using the rx_peer struct, set its
881 * idle time to now. rxi_ReapConnections will reap it if it's still
882 * idle (refCount == 0) after rx_idlePeerTime (60 seconds) have passed.
884 MUTEX_ENTER(&rx_peerHashTable_lock);
885 if (conn->peer->refCount < 2) {
886 conn->peer->idleWhen = clock_Sec();
887 if (conn->peer->refCount < 1) {
888 conn->peer->refCount = 1;
889 if (rx_stats_active) {
890 MUTEX_ENTER(&rx_stats_mutex);
891 rxi_lowPeerRefCount++;
892 MUTEX_EXIT(&rx_stats_mutex);
896 conn->peer->refCount--;
897 MUTEX_EXIT(&rx_peerHashTable_lock);
901 if (conn->type == RX_SERVER_CONNECTION)
902 rx_MutexDecrement(rx_stats.nServerConns, rx_stats_mutex);
904 rx_MutexDecrement(rx_stats.nClientConns, rx_stats_mutex);
907 if (conn->specific) {
909 for (i = 0; i < conn->nSpecific; i++) {
910 if (conn->specific[i] && rxi_keyCreate_destructor[i])
911 (*rxi_keyCreate_destructor[i]) (conn->specific[i]);
912 conn->specific[i] = NULL;
914 free(conn->specific);
916 conn->specific = NULL;
920 MUTEX_DESTROY(&conn->conn_call_lock);
921 MUTEX_DESTROY(&conn->conn_data_lock);
922 CV_DESTROY(&conn->conn_call_cv);
924 rxi_FreeConnection(conn);
927 /* Destroy the specified connection */
929 rxi_DestroyConnection(struct rx_connection *conn)
931 MUTEX_ENTER(&rx_connHashTable_lock);
932 rxi_DestroyConnectionNoLock(conn);
933 /* conn should be at the head of the cleanup list */
934 if (conn == rx_connCleanup_list) {
935 rx_connCleanup_list = rx_connCleanup_list->next;
936 MUTEX_EXIT(&rx_connHashTable_lock);
937 rxi_CleanupConnection(conn);
939 #ifdef RX_ENABLE_LOCKS
941 MUTEX_EXIT(&rx_connHashTable_lock);
943 #endif /* RX_ENABLE_LOCKS */
947 rxi_DestroyConnectionNoLock(struct rx_connection *conn)
949 struct rx_connection **conn_ptr;
951 struct rx_packet *packet;
958 MUTEX_ENTER(&conn->conn_data_lock);
959 if (conn->refCount > 0)
962 if (rx_stats_active) {
963 MUTEX_ENTER(&rx_stats_mutex);
964 rxi_lowConnRefCount++;
965 MUTEX_EXIT(&rx_stats_mutex);
969 if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
970 /* Busy; wait till the last guy before proceeding */
971 MUTEX_EXIT(&conn->conn_data_lock);
976 /* If the client previously called rx_NewCall, but it is still
977 * waiting, treat this as a running call, and wait to destroy the
978 * connection later when the call completes. */
979 if ((conn->type == RX_CLIENT_CONNECTION)
980 && (conn->flags & RX_CONN_MAKECALL_WAITING)) {
981 conn->flags |= RX_CONN_DESTROY_ME;
982 MUTEX_EXIT(&conn->conn_data_lock);
986 MUTEX_EXIT(&conn->conn_data_lock);
988 /* Check for extant references to this connection */
989 for (i = 0; i < RX_MAXCALLS; i++) {
990 struct rx_call *call = conn->call[i];
993 if (conn->type == RX_CLIENT_CONNECTION) {
994 MUTEX_ENTER(&call->lock);
995 if (call->delayedAckEvent) {
996 /* Push the final acknowledgment out now--there
997 * won't be a subsequent call to acknowledge the
998 * last reply packets */
999 rxevent_Cancel(call->delayedAckEvent, call,
1000 RX_CALL_REFCOUNT_DELAY);
1001 if (call->state == RX_STATE_PRECALL
1002 || call->state == RX_STATE_ACTIVE) {
1003 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1005 rxi_AckAll(NULL, call, 0);
1008 MUTEX_EXIT(&call->lock);
1012 #ifdef RX_ENABLE_LOCKS
1014 if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
1015 MUTEX_EXIT(&conn->conn_data_lock);
1017 /* Someone is accessing a packet right now. */
1021 #endif /* RX_ENABLE_LOCKS */
1024 /* Don't destroy the connection if there are any call
1025 * structures still in use */
1026 MUTEX_ENTER(&conn->conn_data_lock);
1027 conn->flags |= RX_CONN_DESTROY_ME;
1028 MUTEX_EXIT(&conn->conn_data_lock);
1033 if (conn->delayedAbortEvent) {
1034 rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
1035 packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
1037 MUTEX_ENTER(&conn->conn_data_lock);
1038 rxi_SendConnectionAbort(conn, packet, 0, 1);
1039 MUTEX_EXIT(&conn->conn_data_lock);
1040 rxi_FreePacket(packet);
1044 /* Remove from connection hash table before proceeding */
1046 &rx_connHashTable[CONN_HASH
1047 (peer->host, peer->port, conn->cid, conn->epoch,
1049 for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
1050 if (*conn_ptr == conn) {
1051 *conn_ptr = conn->next;
1055 /* if the conn that we are destroying was the last connection, then we
1056 * clear rxLastConn as well */
1057 if (rxLastConn == conn)
1060 /* Make sure the connection is completely reset before deleting it. */
1061 /* get rid of pending events that could zap us later */
1062 if (conn->challengeEvent)
1063 rxevent_Cancel(conn->challengeEvent, (struct rx_call *)0, 0);
1064 if (conn->checkReachEvent)
1065 rxevent_Cancel(conn->checkReachEvent, (struct rx_call *)0, 0);
1067 /* Add the connection to the list of destroyed connections that
1068 * need to be cleaned up. This is necessary to avoid deadlocks
1069 * in the routines we call to inform others that this connection is
1070 * being destroyed. */
1071 conn->next = rx_connCleanup_list;
1072 rx_connCleanup_list = conn;
1075 /* Externally available version */
1077 rx_DestroyConnection(struct rx_connection *conn)
1082 rxi_DestroyConnection(conn);
1087 rx_GetConnection(struct rx_connection *conn)
1092 MUTEX_ENTER(&conn->conn_data_lock);
1094 MUTEX_EXIT(&conn->conn_data_lock);
1098 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1099 /* Wait for the transmit queue to no longer be busy.
1100 * requires the call->lock to be held */
1101 static void rxi_WaitforTQBusy(struct rx_call *call) {
1102 while (call->flags & RX_CALL_TQ_BUSY) {
1103 call->flags |= RX_CALL_TQ_WAIT;
1105 #ifdef RX_ENABLE_LOCKS
1106 osirx_AssertMine(&call->lock, "rxi_WaitforTQ lock");
1107 CV_WAIT(&call->cv_tq, &call->lock);
1108 #else /* RX_ENABLE_LOCKS */
1109 osi_rxSleep(&call->tq);
1110 #endif /* RX_ENABLE_LOCKS */
1112 if (call->tqWaiters == 0) {
1113 call->flags &= ~RX_CALL_TQ_WAIT;
1119 /* Start a new rx remote procedure call, on the specified connection.
1120 * If wait is set to 1, wait for a free call channel; otherwise return
1121 * 0. Maxtime gives the maximum number of seconds this call may take,
1122 * after rx_NewCall returns. After this time interval, a call to any
1123 * of rx_SendData, rx_ReadData, etc. will fail with RX_CALL_TIMEOUT.
1124 * For fine grain locking, we hold the conn_call_lock in order to
1125 * to ensure that we don't get signalle after we found a call in an active
1126 * state and before we go to sleep.
1129 rx_NewCall(struct rx_connection *conn)
1132 struct rx_call *call;
1133 struct clock queueTime;
1137 dpf(("rx_NewCall(conn %x)\n", conn));
1140 clock_GetTime(&queueTime);
1141 MUTEX_ENTER(&conn->conn_call_lock);
1144 * Check if there are others waiting for a new call.
1145 * If so, let them go first to avoid starving them.
1146 * This is a fairly simple scheme, and might not be
1147 * a complete solution for large numbers of waiters.
1149 * makeCallWaiters keeps track of the number of
1150 * threads waiting to make calls and the
1151 * RX_CONN_MAKECALL_WAITING flag bit is used to
1152 * indicate that there are indeed calls waiting.
1153 * The flag is set when the waiter is incremented.
1154 * It is only cleared in rx_EndCall when
1155 * makeCallWaiters is 0. This prevents us from
1156 * accidently destroying the connection while it
1157 * is potentially about to be used.
1159 MUTEX_ENTER(&conn->conn_data_lock);
1160 if (conn->makeCallWaiters) {
1161 conn->flags |= RX_CONN_MAKECALL_WAITING;
1162 conn->makeCallWaiters++;
1163 MUTEX_EXIT(&conn->conn_data_lock);
1165 #ifdef RX_ENABLE_LOCKS
1166 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1170 MUTEX_ENTER(&conn->conn_data_lock);
1171 conn->makeCallWaiters--;
1173 MUTEX_EXIT(&conn->conn_data_lock);
1176 for (i = 0; i < RX_MAXCALLS; i++) {
1177 call = conn->call[i];
1179 MUTEX_ENTER(&call->lock);
1180 if (call->state == RX_STATE_DALLY) {
1181 rxi_ResetCall(call, 0);
1182 (*call->callNumber)++;
1185 MUTEX_EXIT(&call->lock);
1187 call = rxi_NewCall(conn, i);
1191 if (i < RX_MAXCALLS) {
1194 MUTEX_ENTER(&conn->conn_data_lock);
1195 conn->flags |= RX_CONN_MAKECALL_WAITING;
1196 conn->makeCallWaiters++;
1197 MUTEX_EXIT(&conn->conn_data_lock);
1199 #ifdef RX_ENABLE_LOCKS
1200 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1204 MUTEX_ENTER(&conn->conn_data_lock);
1205 conn->makeCallWaiters--;
1206 MUTEX_EXIT(&conn->conn_data_lock);
1209 * Wake up anyone else who might be giving us a chance to
1210 * run (see code above that avoids resource starvation).
1212 #ifdef RX_ENABLE_LOCKS
1213 CV_BROADCAST(&conn->conn_call_cv);
1218 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1220 /* Client is initially in send mode */
1221 call->state = RX_STATE_ACTIVE;
1222 call->error = conn->error;
1224 call->mode = RX_MODE_ERROR;
1226 call->mode = RX_MODE_SENDING;
1228 /* remember start time for call in case we have hard dead time limit */
1229 call->queueTime = queueTime;
1230 clock_GetTime(&call->startTime);
1231 hzero(call->bytesSent);
1232 hzero(call->bytesRcvd);
1234 /* Turn on busy protocol. */
1235 rxi_KeepAliveOn(call);
1237 MUTEX_EXIT(&call->lock);
1238 MUTEX_EXIT(&conn->conn_call_lock);
1241 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1242 /* Now, if TQ wasn't cleared earlier, do it now. */
1243 MUTEX_ENTER(&call->lock);
1244 rxi_WaitforTQBusy(call);
1245 if (call->flags & RX_CALL_TQ_CLEARME) {
1246 rxi_ClearTransmitQueue(call, 1);
1247 /*queue_Init(&call->tq);*/
1249 MUTEX_EXIT(&call->lock);
1250 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1252 dpf(("rx_NewCall(call %x)\n", call));
1257 rxi_HasActiveCalls(struct rx_connection *aconn)
1260 struct rx_call *tcall;
1264 for (i = 0; i < RX_MAXCALLS; i++) {
1265 if ((tcall = aconn->call[i])) {
1266 if ((tcall->state == RX_STATE_ACTIVE)
1267 || (tcall->state == RX_STATE_PRECALL)) {
1278 rxi_GetCallNumberVector(struct rx_connection *aconn,
1279 afs_int32 * aint32s)
1282 struct rx_call *tcall;
1286 for (i = 0; i < RX_MAXCALLS; i++) {
1287 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1288 aint32s[i] = aconn->callNumber[i] + 1;
1290 aint32s[i] = aconn->callNumber[i];
1297 rxi_SetCallNumberVector(struct rx_connection *aconn,
1298 afs_int32 * aint32s)
1301 struct rx_call *tcall;
1305 for (i = 0; i < RX_MAXCALLS; i++) {
1306 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1307 aconn->callNumber[i] = aint32s[i] - 1;
1309 aconn->callNumber[i] = aint32s[i];
1315 /* Advertise a new service. A service is named locally by a UDP port
1316 * number plus a 16-bit service id. Returns (struct rx_service *) 0
1319 char *serviceName; Name for identification purposes (e.g. the
1320 service name might be used for probing for
1323 rx_NewServiceHost(afs_uint32 host, u_short port, u_short serviceId,
1324 char *serviceName, struct rx_securityClass **securityObjects,
1325 int nSecurityObjects,
1326 afs_int32(*serviceProc) (struct rx_call * acall))
1328 osi_socket socket = OSI_NULLSOCKET;
1329 struct rx_service *tservice;
1335 if (serviceId == 0) {
1337 "rx_NewService: service id for service %s is not non-zero.\n",
1344 "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",
1352 tservice = rxi_AllocService();
1354 for (i = 0; i < RX_MAX_SERVICES; i++) {
1355 struct rx_service *service = rx_services[i];
1357 if (port == service->servicePort && host == service->serviceHost) {
1358 if (service->serviceId == serviceId) {
1359 /* The identical service has already been
1360 * installed; if the caller was intending to
1361 * change the security classes used by this
1362 * service, he/she loses. */
1364 "rx_NewService: tried to install service %s with service id %d, which is already in use for service %s\n",
1365 serviceName, serviceId, service->serviceName);
1367 rxi_FreeService(tservice);
1370 /* Different service, same port: re-use the socket
1371 * which is bound to the same port */
1372 socket = service->socket;
1375 if (socket == OSI_NULLSOCKET) {
1376 /* If we don't already have a socket (from another
1377 * service on same port) get a new one */
1378 socket = rxi_GetHostUDPSocket(host, port);
1379 if (socket == OSI_NULLSOCKET) {
1381 rxi_FreeService(tservice);
1386 service->socket = socket;
1387 service->serviceHost = host;
1388 service->servicePort = port;
1389 service->serviceId = serviceId;
1390 service->serviceName = serviceName;
1391 service->nSecurityObjects = nSecurityObjects;
1392 service->securityObjects = securityObjects;
1393 service->minProcs = 0;
1394 service->maxProcs = 1;
1395 service->idleDeadTime = 60;
1396 service->idleDeadErr = 0;
1397 service->connDeadTime = rx_connDeadTime;
1398 service->executeRequestProc = serviceProc;
1399 service->checkReach = 0;
1400 rx_services[i] = service; /* not visible until now */
1406 rxi_FreeService(tservice);
1407 (osi_Msg "rx_NewService: cannot support > %d services\n",
1412 /* Set configuration options for all of a service's security objects */
1415 rx_SetSecurityConfiguration(struct rx_service *service,
1416 rx_securityConfigVariables type,
1420 for (i = 0; i<service->nSecurityObjects; i++) {
1421 if (service->securityObjects[i]) {
1422 RXS_SetConfiguration(service->securityObjects[i], NULL, type,
1430 rx_NewService(u_short port, u_short serviceId, char *serviceName,
1431 struct rx_securityClass **securityObjects, int nSecurityObjects,
1432 afs_int32(*serviceProc) (struct rx_call * acall))
1434 return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
1437 /* Generic request processing loop. This routine should be called
1438 * by the implementation dependent rx_ServerProc. If socketp is
1439 * non-null, it will be set to the file descriptor that this thread
1440 * is now listening on. If socketp is null, this routine will never
1443 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1445 struct rx_call *call;
1447 struct rx_service *tservice = NULL;
1454 call = rx_GetCall(threadID, tservice, socketp);
1455 if (socketp && *socketp != OSI_NULLSOCKET) {
1456 /* We are now a listener thread */
1461 /* if server is restarting( typically smooth shutdown) then do not
1462 * allow any new calls.
1465 if (rx_tranquil && (call != NULL)) {
1469 MUTEX_ENTER(&call->lock);
1471 rxi_CallError(call, RX_RESTARTING);
1472 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1474 MUTEX_EXIT(&call->lock);
1478 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1479 #ifdef RX_ENABLE_LOCKS
1481 #endif /* RX_ENABLE_LOCKS */
1482 afs_termState = AFSOP_STOP_AFS;
1483 afs_osi_Wakeup(&afs_termState);
1484 #ifdef RX_ENABLE_LOCKS
1486 #endif /* RX_ENABLE_LOCKS */
1491 tservice = call->conn->service;
1493 if (tservice->beforeProc)
1494 (*tservice->beforeProc) (call);
1496 code = call->conn->service->executeRequestProc(call);
1498 if (tservice->afterProc)
1499 (*tservice->afterProc) (call, code);
1501 rx_EndCall(call, code);
1502 if (rx_stats_active) {
1503 MUTEX_ENTER(&rx_stats_mutex);
1505 MUTEX_EXIT(&rx_stats_mutex);
1512 rx_WakeupServerProcs(void)
1514 struct rx_serverQueueEntry *np, *tqp;
1518 MUTEX_ENTER(&rx_serverPool_lock);
1520 #ifdef RX_ENABLE_LOCKS
1521 if (rx_waitForPacket)
1522 CV_BROADCAST(&rx_waitForPacket->cv);
1523 #else /* RX_ENABLE_LOCKS */
1524 if (rx_waitForPacket)
1525 osi_rxWakeup(rx_waitForPacket);
1526 #endif /* RX_ENABLE_LOCKS */
1527 MUTEX_ENTER(&freeSQEList_lock);
1528 for (np = rx_FreeSQEList; np; np = tqp) {
1529 tqp = *(struct rx_serverQueueEntry **)np;
1530 #ifdef RX_ENABLE_LOCKS
1531 CV_BROADCAST(&np->cv);
1532 #else /* RX_ENABLE_LOCKS */
1534 #endif /* RX_ENABLE_LOCKS */
1536 MUTEX_EXIT(&freeSQEList_lock);
1537 for (queue_Scan(&rx_idleServerQueue, np, tqp, rx_serverQueueEntry)) {
1538 #ifdef RX_ENABLE_LOCKS
1539 CV_BROADCAST(&np->cv);
1540 #else /* RX_ENABLE_LOCKS */
1542 #endif /* RX_ENABLE_LOCKS */
1544 MUTEX_EXIT(&rx_serverPool_lock);
1549 * One thing that seems to happen is that all the server threads get
1550 * tied up on some empty or slow call, and then a whole bunch of calls
1551 * arrive at once, using up the packet pool, so now there are more
1552 * empty calls. The most critical resources here are server threads
1553 * and the free packet pool. The "doreclaim" code seems to help in
1554 * general. I think that eventually we arrive in this state: there
1555 * are lots of pending calls which do have all their packets present,
1556 * so they won't be reclaimed, are multi-packet calls, so they won't
1557 * be scheduled until later, and thus are tying up most of the free
1558 * packet pool for a very long time.
1560 * 1. schedule multi-packet calls if all the packets are present.
1561 * Probably CPU-bound operation, useful to return packets to pool.
1562 * Do what if there is a full window, but the last packet isn't here?
1563 * 3. preserve one thread which *only* runs "best" calls, otherwise
1564 * it sleeps and waits for that type of call.
1565 * 4. Don't necessarily reserve a whole window for each thread. In fact,
1566 * the current dataquota business is badly broken. The quota isn't adjusted
1567 * to reflect how many packets are presently queued for a running call.
1568 * So, when we schedule a queued call with a full window of packets queued
1569 * up for it, that *should* free up a window full of packets for other 2d-class
1570 * calls to be able to use from the packet pool. But it doesn't.
1572 * NB. Most of the time, this code doesn't run -- since idle server threads
1573 * sit on the idle server queue and are assigned by "...ReceivePacket" as soon
1574 * as a new call arrives.
1576 /* Sleep until a call arrives. Returns a pointer to the call, ready
1577 * for an rx_Read. */
1578 #ifdef RX_ENABLE_LOCKS
1580 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1582 struct rx_serverQueueEntry *sq;
1583 struct rx_call *call = (struct rx_call *)0;
1584 struct rx_service *service = NULL;
1587 MUTEX_ENTER(&freeSQEList_lock);
1589 if ((sq = rx_FreeSQEList)) {
1590 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1591 MUTEX_EXIT(&freeSQEList_lock);
1592 } else { /* otherwise allocate a new one and return that */
1593 MUTEX_EXIT(&freeSQEList_lock);
1594 sq = (struct rx_serverQueueEntry *)
1595 rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1596 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1597 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1600 MUTEX_ENTER(&rx_serverPool_lock);
1601 if (cur_service != NULL) {
1602 ReturnToServerPool(cur_service);
1605 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1606 struct rx_call *tcall, *ncall, *choice2 = NULL;
1608 /* Scan for eligible incoming calls. A call is not eligible
1609 * if the maximum number of calls for its service type are
1610 * already executing */
1611 /* One thread will process calls FCFS (to prevent starvation),
1612 * while the other threads may run ahead looking for calls which
1613 * have all their input data available immediately. This helps
1614 * keep threads from blocking, waiting for data from the client. */
1615 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1616 service = tcall->conn->service;
1617 if (!QuotaOK(service)) {
1620 MUTEX_ENTER(&rx_pthread_mutex);
1621 if (tno == rxi_fcfs_thread_num
1622 || !tcall->queue_item_header.next) {
1623 MUTEX_EXIT(&rx_pthread_mutex);
1624 /* If we're the fcfs thread , then we'll just use
1625 * this call. If we haven't been able to find an optimal
1626 * choice, and we're at the end of the list, then use a
1627 * 2d choice if one has been identified. Otherwise... */
1628 call = (choice2 ? choice2 : tcall);
1629 service = call->conn->service;
1631 MUTEX_EXIT(&rx_pthread_mutex);
1632 if (!queue_IsEmpty(&tcall->rq)) {
1633 struct rx_packet *rp;
1634 rp = queue_First(&tcall->rq, rx_packet);
1635 if (rp->header.seq == 1) {
1637 || (rp->header.flags & RX_LAST_PACKET)) {
1639 } else if (rxi_2dchoice && !choice2
1640 && !(tcall->flags & RX_CALL_CLEARED)
1641 && (tcall->rprev > rxi_HardAckRate)) {
1651 ReturnToServerPool(service);
1658 MUTEX_EXIT(&rx_serverPool_lock);
1659 MUTEX_ENTER(&call->lock);
1661 if (call->flags & RX_CALL_WAIT_PROC) {
1662 call->flags &= ~RX_CALL_WAIT_PROC;
1663 MUTEX_ENTER(&rx_waiting_mutex);
1665 MUTEX_EXIT(&rx_waiting_mutex);
1668 if (call->state != RX_STATE_PRECALL || call->error) {
1669 MUTEX_EXIT(&call->lock);
1670 MUTEX_ENTER(&rx_serverPool_lock);
1671 ReturnToServerPool(service);
1676 if (queue_IsEmpty(&call->rq)
1677 || queue_First(&call->rq, rx_packet)->header.seq != 1)
1678 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1680 CLEAR_CALL_QUEUE_LOCK(call);
1683 /* If there are no eligible incoming calls, add this process
1684 * to the idle server queue, to wait for one */
1688 *socketp = OSI_NULLSOCKET;
1690 sq->socketp = socketp;
1691 queue_Append(&rx_idleServerQueue, sq);
1692 #ifndef AFS_AIX41_ENV
1693 rx_waitForPacket = sq;
1695 rx_waitingForPacket = sq;
1696 #endif /* AFS_AIX41_ENV */
1698 CV_WAIT(&sq->cv, &rx_serverPool_lock);
1700 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1701 MUTEX_EXIT(&rx_serverPool_lock);
1702 return (struct rx_call *)0;
1705 } while (!(call = sq->newcall)
1706 && !(socketp && *socketp != OSI_NULLSOCKET));
1707 MUTEX_EXIT(&rx_serverPool_lock);
1709 MUTEX_ENTER(&call->lock);
1715 MUTEX_ENTER(&freeSQEList_lock);
1716 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1717 rx_FreeSQEList = sq;
1718 MUTEX_EXIT(&freeSQEList_lock);
1721 clock_GetTime(&call->startTime);
1722 call->state = RX_STATE_ACTIVE;
1723 call->mode = RX_MODE_RECEIVING;
1724 #ifdef RX_KERNEL_TRACE
1725 if (ICL_SETACTIVE(afs_iclSetp)) {
1726 int glockOwner = ISAFS_GLOCK();
1729 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1730 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1737 rxi_calltrace(RX_CALL_START, call);
1738 dpf(("rx_GetCall(port=%d, service=%d) ==> call %x\n",
1739 call->conn->service->servicePort, call->conn->service->serviceId,
1742 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1743 MUTEX_EXIT(&call->lock);
1745 dpf(("rx_GetCall(socketp=0x%x, *socketp=0x%x)\n", socketp, *socketp));
1750 #else /* RX_ENABLE_LOCKS */
1752 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1754 struct rx_serverQueueEntry *sq;
1755 struct rx_call *call = (struct rx_call *)0, *choice2;
1756 struct rx_service *service = NULL;
1760 MUTEX_ENTER(&freeSQEList_lock);
1762 if ((sq = rx_FreeSQEList)) {
1763 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1764 MUTEX_EXIT(&freeSQEList_lock);
1765 } else { /* otherwise allocate a new one and return that */
1766 MUTEX_EXIT(&freeSQEList_lock);
1767 sq = (struct rx_serverQueueEntry *)
1768 rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1769 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1770 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1772 MUTEX_ENTER(&sq->lock);
1774 if (cur_service != NULL) {
1775 cur_service->nRequestsRunning--;
1776 if (cur_service->nRequestsRunning < cur_service->minProcs)
1780 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1781 struct rx_call *tcall, *ncall;
1782 /* Scan for eligible incoming calls. A call is not eligible
1783 * if the maximum number of calls for its service type are
1784 * already executing */
1785 /* One thread will process calls FCFS (to prevent starvation),
1786 * while the other threads may run ahead looking for calls which
1787 * have all their input data available immediately. This helps
1788 * keep threads from blocking, waiting for data from the client. */
1789 choice2 = (struct rx_call *)0;
1790 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1791 service = tcall->conn->service;
1792 if (QuotaOK(service)) {
1793 MUTEX_ENTER(&rx_pthread_mutex);
1794 if (tno == rxi_fcfs_thread_num
1795 || !tcall->queue_item_header.next) {
1796 MUTEX_EXIT(&rx_pthread_mutex);
1797 /* If we're the fcfs thread, then we'll just use
1798 * this call. If we haven't been able to find an optimal
1799 * choice, and we're at the end of the list, then use a
1800 * 2d choice if one has been identified. Otherwise... */
1801 call = (choice2 ? choice2 : tcall);
1802 service = call->conn->service;
1804 MUTEX_EXIT(&rx_pthread_mutex);
1805 if (!queue_IsEmpty(&tcall->rq)) {
1806 struct rx_packet *rp;
1807 rp = queue_First(&tcall->rq, rx_packet);
1808 if (rp->header.seq == 1
1810 || (rp->header.flags & RX_LAST_PACKET))) {
1812 } else if (rxi_2dchoice && !choice2
1813 && !(tcall->flags & RX_CALL_CLEARED)
1814 && (tcall->rprev > rxi_HardAckRate)) {
1828 /* we can't schedule a call if there's no data!!! */
1829 /* send an ack if there's no data, if we're missing the
1830 * first packet, or we're missing something between first
1831 * and last -- there's a "hole" in the incoming data. */
1832 if (queue_IsEmpty(&call->rq)
1833 || queue_First(&call->rq, rx_packet)->header.seq != 1
1834 || call->rprev != queue_Last(&call->rq, rx_packet)->header.seq)
1835 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1837 call->flags &= (~RX_CALL_WAIT_PROC);
1838 service->nRequestsRunning++;
1839 /* just started call in minProcs pool, need fewer to maintain
1841 if (service->nRequestsRunning <= service->minProcs)
1845 /* MUTEX_EXIT(&call->lock); */
1847 /* If there are no eligible incoming calls, add this process
1848 * to the idle server queue, to wait for one */
1851 *socketp = OSI_NULLSOCKET;
1853 sq->socketp = socketp;
1854 queue_Append(&rx_idleServerQueue, sq);
1858 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1860 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
1861 return (struct rx_call *)0;
1864 } while (!(call = sq->newcall)
1865 && !(socketp && *socketp != OSI_NULLSOCKET));
1867 MUTEX_EXIT(&sq->lock);
1869 MUTEX_ENTER(&freeSQEList_lock);
1870 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1871 rx_FreeSQEList = sq;
1872 MUTEX_EXIT(&freeSQEList_lock);
1875 clock_GetTime(&call->startTime);
1876 call->state = RX_STATE_ACTIVE;
1877 call->mode = RX_MODE_RECEIVING;
1878 #ifdef RX_KERNEL_TRACE
1879 if (ICL_SETACTIVE(afs_iclSetp)) {
1880 int glockOwner = ISAFS_GLOCK();
1883 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1884 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1891 rxi_calltrace(RX_CALL_START, call);
1892 dpf(("rx_GetCall(port=%d, service=%d) ==> call %x\n",
1893 call->conn->service->servicePort, call->conn->service->serviceId,
1896 dpf(("rx_GetCall(socketp=0x%x, *socketp=0x%x)\n", socketp, *socketp));
1903 #endif /* RX_ENABLE_LOCKS */
1907 /* Establish a procedure to be called when a packet arrives for a
1908 * call. This routine will be called at most once after each call,
1909 * and will also be called if there is an error condition on the or
1910 * the call is complete. Used by multi rx to build a selection
1911 * function which determines which of several calls is likely to be a
1912 * good one to read from.
1913 * NOTE: the way this is currently implemented it is probably only a
1914 * good idea to (1) use it immediately after a newcall (clients only)
1915 * and (2) only use it once. Other uses currently void your warranty
1918 rx_SetArrivalProc(struct rx_call *call,
1919 void (*proc) (struct rx_call * call,
1922 void * handle, int arg)
1924 call->arrivalProc = proc;
1925 call->arrivalProcHandle = handle;
1926 call->arrivalProcArg = arg;
1929 /* Call is finished (possibly prematurely). Return rc to the peer, if
1930 * appropriate, and return the final error code from the conversation
1934 rx_EndCall(struct rx_call *call, afs_int32 rc)
1936 struct rx_connection *conn = call->conn;
1937 struct rx_service *service;
1943 dpf(("rx_EndCall(call %x rc %d error %d abortCode %d)\n", call, rc, call->error, call->abortCode));
1946 MUTEX_ENTER(&call->lock);
1948 if (rc == 0 && call->error == 0) {
1949 call->abortCode = 0;
1950 call->abortCount = 0;
1953 call->arrivalProc = (void (*)())0;
1954 if (rc && call->error == 0) {
1955 rxi_CallError(call, rc);
1956 /* Send an abort message to the peer if this error code has
1957 * only just been set. If it was set previously, assume the
1958 * peer has already been sent the error code or will request it
1960 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1962 if (conn->type == RX_SERVER_CONNECTION) {
1963 /* Make sure reply or at least dummy reply is sent */
1964 if (call->mode == RX_MODE_RECEIVING) {
1965 rxi_WriteProc(call, 0, 0);
1967 if (call->mode == RX_MODE_SENDING) {
1968 rxi_FlushWrite(call);
1970 service = conn->service;
1971 rxi_calltrace(RX_CALL_END, call);
1972 /* Call goes to hold state until reply packets are acknowledged */
1973 if (call->tfirst + call->nSoftAcked < call->tnext) {
1974 call->state = RX_STATE_HOLD;
1976 call->state = RX_STATE_DALLY;
1977 rxi_ClearTransmitQueue(call, 0);
1978 rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
1979 rxevent_Cancel(call->keepAliveEvent, call,
1980 RX_CALL_REFCOUNT_ALIVE);
1982 } else { /* Client connection */
1984 /* Make sure server receives input packets, in the case where
1985 * no reply arguments are expected */
1986 if ((call->mode == RX_MODE_SENDING)
1987 || (call->mode == RX_MODE_RECEIVING && call->rnext == 1)) {
1988 (void)rxi_ReadProc(call, &dummy, 1);
1991 /* If we had an outstanding delayed ack, be nice to the server
1992 * and force-send it now.
1994 if (call->delayedAckEvent) {
1995 rxevent_Cancel(call->delayedAckEvent, call,
1996 RX_CALL_REFCOUNT_DELAY);
1997 call->delayedAckEvent = NULL;
1998 rxi_SendDelayedAck(NULL, call, NULL);
2001 /* We need to release the call lock since it's lower than the
2002 * conn_call_lock and we don't want to hold the conn_call_lock
2003 * over the rx_ReadProc call. The conn_call_lock needs to be held
2004 * here for the case where rx_NewCall is perusing the calls on
2005 * the connection structure. We don't want to signal until
2006 * rx_NewCall is in a stable state. Otherwise, rx_NewCall may
2007 * have checked this call, found it active and by the time it
2008 * goes to sleep, will have missed the signal.
2010 * Do not clear the RX_CONN_MAKECALL_WAITING flag as long as
2011 * there are threads waiting to use the conn object.
2013 MUTEX_EXIT(&call->lock);
2014 MUTEX_ENTER(&conn->conn_call_lock);
2015 MUTEX_ENTER(&call->lock);
2016 MUTEX_ENTER(&conn->conn_data_lock);
2017 conn->flags |= RX_CONN_BUSY;
2018 if (conn->flags & RX_CONN_MAKECALL_WAITING) {
2019 if (conn->makeCallWaiters == 0)
2020 conn->flags &= (~RX_CONN_MAKECALL_WAITING);
2021 MUTEX_EXIT(&conn->conn_data_lock);
2022 #ifdef RX_ENABLE_LOCKS
2023 CV_BROADCAST(&conn->conn_call_cv);
2028 #ifdef RX_ENABLE_LOCKS
2030 MUTEX_EXIT(&conn->conn_data_lock);
2032 #endif /* RX_ENABLE_LOCKS */
2033 call->state = RX_STATE_DALLY;
2035 error = call->error;
2037 /* currentPacket, nLeft, and NFree must be zeroed here, because
2038 * ResetCall cannot: ResetCall may be called at splnet(), in the
2039 * kernel version, and may interrupt the macros rx_Read or
2040 * rx_Write, which run at normal priority for efficiency. */
2041 if (call->currentPacket) {
2042 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
2043 rxi_FreePacket(call->currentPacket);
2044 call->currentPacket = (struct rx_packet *)0;
2047 call->nLeft = call->nFree = call->curlen = 0;
2049 /* Free any packets from the last call to ReadvProc/WritevProc */
2050 #ifdef RXDEBUG_PACKET
2052 #endif /* RXDEBUG_PACKET */
2053 rxi_FreePackets(0, &call->iovq);
2055 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
2056 MUTEX_EXIT(&call->lock);
2057 if (conn->type == RX_CLIENT_CONNECTION) {
2058 MUTEX_EXIT(&conn->conn_call_lock);
2059 conn->flags &= ~RX_CONN_BUSY;
2063 * Map errors to the local host's errno.h format.
2065 error = ntoh_syserr_conv(error);
2069 #if !defined(KERNEL)
2071 /* Call this routine when shutting down a server or client (especially
2072 * clients). This will allow Rx to gracefully garbage collect server
2073 * connections, and reduce the number of retries that a server might
2074 * make to a dead client.
2075 * This is not quite right, since some calls may still be ongoing and
2076 * we can't lock them to destroy them. */
2080 struct rx_connection **conn_ptr, **conn_end;
2084 if (rxinit_status == 1) {
2086 return; /* Already shutdown. */
2088 rxi_DeleteCachedConnections();
2089 if (rx_connHashTable) {
2090 MUTEX_ENTER(&rx_connHashTable_lock);
2091 for (conn_ptr = &rx_connHashTable[0], conn_end =
2092 &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
2094 struct rx_connection *conn, *next;
2095 for (conn = *conn_ptr; conn; conn = next) {
2097 if (conn->type == RX_CLIENT_CONNECTION) {
2098 /* MUTEX_ENTER(&conn->conn_data_lock); when used in kernel */
2100 /* MUTEX_EXIT(&conn->conn_data_lock); when used in kernel */
2101 #ifdef RX_ENABLE_LOCKS
2102 rxi_DestroyConnectionNoLock(conn);
2103 #else /* RX_ENABLE_LOCKS */
2104 rxi_DestroyConnection(conn);
2105 #endif /* RX_ENABLE_LOCKS */
2109 #ifdef RX_ENABLE_LOCKS
2110 while (rx_connCleanup_list) {
2111 struct rx_connection *conn;
2112 conn = rx_connCleanup_list;
2113 rx_connCleanup_list = rx_connCleanup_list->next;
2114 MUTEX_EXIT(&rx_connHashTable_lock);
2115 rxi_CleanupConnection(conn);
2116 MUTEX_ENTER(&rx_connHashTable_lock);
2118 MUTEX_EXIT(&rx_connHashTable_lock);
2119 #endif /* RX_ENABLE_LOCKS */
2124 afs_winsockCleanup();
2132 /* if we wakeup packet waiter too often, can get in loop with two
2133 AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2135 rxi_PacketsUnWait(void)
2137 if (!rx_waitingForPackets) {
2141 if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2142 return; /* still over quota */
2145 rx_waitingForPackets = 0;
2146 #ifdef RX_ENABLE_LOCKS
2147 CV_BROADCAST(&rx_waitingForPackets_cv);
2149 osi_rxWakeup(&rx_waitingForPackets);
2155 /* ------------------Internal interfaces------------------------- */
2157 /* Return this process's service structure for the
2158 * specified socket and service */
2160 rxi_FindService(osi_socket socket, u_short serviceId)
2162 struct rx_service **sp;
2163 for (sp = &rx_services[0]; *sp; sp++) {
2164 if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2171 #ifdef KDUMP_RX_LOCK
2172 static struct rx_call_rx_lock *rx_allCallsp = 0;
2174 static struct rx_call *rx_allCallsp = 0;
2178 /* Allocate a call structure, for the indicated channel of the
2179 * supplied connection. The mode and state of the call must be set by
2180 * the caller. Returns the call with mutex locked. */
2182 rxi_NewCall(struct rx_connection *conn, int channel)
2184 struct rx_call *call;
2185 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2186 struct rx_call *cp; /* Call pointer temp */
2187 struct rx_call *nxp; /* Next call pointer, for queue_Scan */
2188 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2190 dpf(("rxi_NewCall(conn %x, channel %d)\n", conn, channel));
2192 /* Grab an existing call structure, or allocate a new one.
2193 * Existing call structures are assumed to have been left reset by
2195 MUTEX_ENTER(&rx_freeCallQueue_lock);
2197 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2199 * EXCEPT that the TQ might not yet be cleared out.
2200 * Skip over those with in-use TQs.
2203 for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2204 if (!(cp->flags & RX_CALL_TQ_BUSY)) {
2210 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2211 if (queue_IsNotEmpty(&rx_freeCallQueue)) {
2212 call = queue_First(&rx_freeCallQueue, rx_call);
2213 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2215 if (rx_stats_active)
2216 rx_MutexDecrement(rx_stats.nFreeCallStructs, rx_stats_mutex);
2217 MUTEX_EXIT(&rx_freeCallQueue_lock);
2218 MUTEX_ENTER(&call->lock);
2219 CLEAR_CALL_QUEUE_LOCK(call);
2220 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2221 /* Now, if TQ wasn't cleared earlier, do it now. */
2222 if (call->flags & RX_CALL_TQ_CLEARME) {
2223 rxi_ClearTransmitQueue(call, 1);
2224 /*queue_Init(&call->tq);*/
2226 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2227 /* Bind the call to its connection structure */
2229 rxi_ResetCall(call, 1);
2232 call = (struct rx_call *)rxi_Alloc(sizeof(struct rx_call));
2233 #ifdef RXDEBUG_PACKET
2234 call->allNextp = rx_allCallsp;
2235 rx_allCallsp = call;
2237 #endif /* RXDEBUG_PACKET */
2238 rx_MutexIncrement(rx_stats.nCallStructs, rx_stats_mutex);
2240 MUTEX_EXIT(&rx_freeCallQueue_lock);
2241 MUTEX_INIT(&call->lock, "call lock", MUTEX_DEFAULT, NULL);
2242 MUTEX_ENTER(&call->lock);
2243 CV_INIT(&call->cv_twind, "call twind", CV_DEFAULT, 0);
2244 CV_INIT(&call->cv_rq, "call rq", CV_DEFAULT, 0);
2245 CV_INIT(&call->cv_tq, "call tq", CV_DEFAULT, 0);
2247 /* Initialize once-only items */
2248 queue_Init(&call->tq);
2249 queue_Init(&call->rq);
2250 queue_Init(&call->iovq);
2251 #ifdef RXDEBUG_PACKET
2252 call->rqc = call->tqc = call->iovqc = 0;
2253 #endif /* RXDEBUG_PACKET */
2254 /* Bind the call to its connection structure (prereq for reset) */
2256 rxi_ResetCall(call, 1);
2258 call->channel = channel;
2259 call->callNumber = &conn->callNumber[channel];
2260 call->rwind = conn->rwind[channel];
2261 call->twind = conn->twind[channel];
2262 /* Note that the next expected call number is retained (in
2263 * conn->callNumber[i]), even if we reallocate the call structure
2265 conn->call[channel] = call;
2266 /* if the channel's never been used (== 0), we should start at 1, otherwise
2267 * the call number is valid from the last time this channel was used */
2268 if (*call->callNumber == 0)
2269 *call->callNumber = 1;
2274 /* A call has been inactive long enough that so we can throw away
2275 * state, including the call structure, which is placed on the call
2277 * Call is locked upon entry.
2278 * haveCTLock set if called from rxi_ReapConnections
2280 #ifdef RX_ENABLE_LOCKS
2282 rxi_FreeCall(struct rx_call *call, int haveCTLock)
2283 #else /* RX_ENABLE_LOCKS */
2285 rxi_FreeCall(struct rx_call *call)
2286 #endif /* RX_ENABLE_LOCKS */
2288 int channel = call->channel;
2289 struct rx_connection *conn = call->conn;
2292 if (call->state == RX_STATE_DALLY || call->state == RX_STATE_HOLD)
2293 (*call->callNumber)++;
2294 rxi_ResetCall(call, 0);
2295 call->conn->call[channel] = (struct rx_call *)0;
2297 MUTEX_ENTER(&rx_freeCallQueue_lock);
2298 SET_CALL_QUEUE_LOCK(call, &rx_freeCallQueue_lock);
2299 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2300 /* A call may be free even though its transmit queue is still in use.
2301 * Since we search the call list from head to tail, put busy calls at
2302 * the head of the list, and idle calls at the tail.
2304 if (call->flags & RX_CALL_TQ_BUSY)
2305 queue_Prepend(&rx_freeCallQueue, call);
2307 queue_Append(&rx_freeCallQueue, call);
2308 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2309 queue_Append(&rx_freeCallQueue, call);
2310 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2311 if (rx_stats_active)
2312 rx_MutexIncrement(rx_stats.nFreeCallStructs, rx_stats_mutex);
2313 MUTEX_EXIT(&rx_freeCallQueue_lock);
2315 /* Destroy the connection if it was previously slated for
2316 * destruction, i.e. the Rx client code previously called
2317 * rx_DestroyConnection (client connections), or
2318 * rxi_ReapConnections called the same routine (server
2319 * connections). Only do this, however, if there are no
2320 * outstanding calls. Note that for fine grain locking, there appears
2321 * to be a deadlock in that rxi_FreeCall has a call locked and
2322 * DestroyConnectionNoLock locks each call in the conn. But note a
2323 * few lines up where we have removed this call from the conn.
2324 * If someone else destroys a connection, they either have no
2325 * call lock held or are going through this section of code.
2327 if (conn->flags & RX_CONN_DESTROY_ME && !(conn->flags & RX_CONN_MAKECALL_WAITING)) {
2328 MUTEX_ENTER(&conn->conn_data_lock);
2330 MUTEX_EXIT(&conn->conn_data_lock);
2331 #ifdef RX_ENABLE_LOCKS
2333 rxi_DestroyConnectionNoLock(conn);
2335 rxi_DestroyConnection(conn);
2336 #else /* RX_ENABLE_LOCKS */
2337 rxi_DestroyConnection(conn);
2338 #endif /* RX_ENABLE_LOCKS */
2342 afs_int32 rxi_Alloccnt = 0, rxi_Allocsize = 0;
2344 rxi_Alloc(size_t size)
2348 if (rx_stats_active)
2349 rx_MutexAdd1Increment2(rxi_Allocsize, (afs_int32)size, rxi_Alloccnt, rx_stats_mutex);
2352 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
2353 afs_osi_Alloc_NoSleep(size);
2358 osi_Panic("rxi_Alloc error");
2364 rxi_Free(void *addr, size_t size)
2366 if (rx_stats_active)
2367 rx_MutexAdd1Decrement2(rxi_Allocsize, -(afs_int32)size, rxi_Alloccnt, rx_stats_mutex);
2368 osi_Free(addr, size);
2372 rxi_SetPeerMtu(afs_uint32 host, afs_uint32 port, int mtu)
2374 struct rx_peer **peer_ptr, **peer_end;
2377 MUTEX_ENTER(&rx_peerHashTable_lock);
2379 for (peer_ptr = &rx_peerHashTable[0], peer_end =
2380 &rx_peerHashTable[rx_hashTableSize]; peer_ptr < peer_end;
2382 struct rx_peer *peer, *next;
2383 for (peer = *peer_ptr; peer; peer = next) {
2385 if (host == peer->host) {
2386 MUTEX_ENTER(&peer->peer_lock);
2387 peer->ifMTU=MIN(mtu, peer->ifMTU);
2388 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2389 MUTEX_EXIT(&peer->peer_lock);
2394 struct rx_peer *peer;
2395 hashIndex = PEER_HASH(host, port);
2396 for (peer = rx_peerHashTable[hashIndex]; peer; peer = peer->next) {
2397 if ((peer->host == host) && (peer->port == port)) {
2398 MUTEX_ENTER(&peer->peer_lock);
2399 peer->ifMTU=MIN(mtu, peer->ifMTU);
2400 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2401 MUTEX_EXIT(&peer->peer_lock);
2405 MUTEX_EXIT(&rx_peerHashTable_lock);
2408 /* Find the peer process represented by the supplied (host,port)
2409 * combination. If there is no appropriate active peer structure, a
2410 * new one will be allocated and initialized
2411 * The origPeer, if set, is a pointer to a peer structure on which the
2412 * refcount will be be decremented. This is used to replace the peer
2413 * structure hanging off a connection structure */
2415 rxi_FindPeer(afs_uint32 host, u_short port,
2416 struct rx_peer *origPeer, int create)
2420 hashIndex = PEER_HASH(host, port);
2421 MUTEX_ENTER(&rx_peerHashTable_lock);
2422 for (pp = rx_peerHashTable[hashIndex]; pp; pp = pp->next) {
2423 if ((pp->host == host) && (pp->port == port))
2428 pp = rxi_AllocPeer(); /* This bzero's *pp */
2429 pp->host = host; /* set here or in InitPeerParams is zero */
2431 MUTEX_INIT(&pp->peer_lock, "peer_lock", MUTEX_DEFAULT, 0);
2432 queue_Init(&pp->congestionQueue);
2433 queue_Init(&pp->rpcStats);
2434 pp->next = rx_peerHashTable[hashIndex];
2435 rx_peerHashTable[hashIndex] = pp;
2436 rxi_InitPeerParams(pp);
2437 if (rx_stats_active)
2438 rx_MutexIncrement(rx_stats.nPeerStructs, rx_stats_mutex);
2445 origPeer->refCount--;
2446 MUTEX_EXIT(&rx_peerHashTable_lock);
2451 /* Find the connection at (host, port) started at epoch, and with the
2452 * given connection id. Creates the server connection if necessary.
2453 * The type specifies whether a client connection or a server
2454 * connection is desired. In both cases, (host, port) specify the
2455 * peer's (host, pair) pair. Client connections are not made
2456 * automatically by this routine. The parameter socket gives the
2457 * socket descriptor on which the packet was received. This is used,
2458 * in the case of server connections, to check that *new* connections
2459 * come via a valid (port, serviceId). Finally, the securityIndex
2460 * parameter must match the existing index for the connection. If a
2461 * server connection is created, it will be created using the supplied
2462 * index, if the index is valid for this service */
2463 struct rx_connection *
2464 rxi_FindConnection(osi_socket socket, afs_int32 host,
2465 u_short port, u_short serviceId, afs_uint32 cid,
2466 afs_uint32 epoch, int type, u_int securityIndex)
2468 int hashindex, flag, i;
2469 struct rx_connection *conn;
2470 hashindex = CONN_HASH(host, port, cid, epoch, type);
2471 MUTEX_ENTER(&rx_connHashTable_lock);
2472 rxLastConn ? (conn = rxLastConn, flag = 0) : (conn =
2473 rx_connHashTable[hashindex],
2476 if ((conn->type == type) && ((cid & RX_CIDMASK) == conn->cid)
2477 && (epoch == conn->epoch)) {
2478 struct rx_peer *pp = conn->peer;
2479 if (securityIndex != conn->securityIndex) {
2480 /* this isn't supposed to happen, but someone could forge a packet
2481 * like this, and there seems to be some CM bug that makes this
2482 * happen from time to time -- in which case, the fileserver
2484 MUTEX_EXIT(&rx_connHashTable_lock);
2485 return (struct rx_connection *)0;
2487 if (pp->host == host && pp->port == port)
2489 if (type == RX_CLIENT_CONNECTION && pp->port == port)
2491 /* So what happens when it's a callback connection? */
2492 if ( /*type == RX_CLIENT_CONNECTION && */
2493 (conn->epoch & 0x80000000))
2497 /* the connection rxLastConn that was used the last time is not the
2498 ** one we are looking for now. Hence, start searching in the hash */
2500 conn = rx_connHashTable[hashindex];
2505 struct rx_service *service;
2506 if (type == RX_CLIENT_CONNECTION) {
2507 MUTEX_EXIT(&rx_connHashTable_lock);
2508 return (struct rx_connection *)0;
2510 service = rxi_FindService(socket, serviceId);
2511 if (!service || (securityIndex >= service->nSecurityObjects)
2512 || (service->securityObjects[securityIndex] == 0)) {
2513 MUTEX_EXIT(&rx_connHashTable_lock);
2514 return (struct rx_connection *)0;
2516 conn = rxi_AllocConnection(); /* This bzero's the connection */
2517 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
2518 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
2519 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
2520 conn->next = rx_connHashTable[hashindex];
2521 rx_connHashTable[hashindex] = conn;
2522 conn->peer = rxi_FindPeer(host, port, 0, 1);
2523 conn->type = RX_SERVER_CONNECTION;
2524 conn->lastSendTime = clock_Sec(); /* don't GC immediately */
2525 conn->epoch = epoch;
2526 conn->cid = cid & RX_CIDMASK;
2527 /* conn->serial = conn->lastSerial = 0; */
2528 /* conn->timeout = 0; */
2529 conn->ackRate = RX_FAST_ACK_RATE;
2530 conn->service = service;
2531 conn->serviceId = serviceId;
2532 conn->securityIndex = securityIndex;
2533 conn->securityObject = service->securityObjects[securityIndex];
2534 conn->nSpecific = 0;
2535 conn->specific = NULL;
2536 rx_SetConnDeadTime(conn, service->connDeadTime);
2537 rx_SetConnIdleDeadTime(conn, service->idleDeadTime);
2538 rx_SetServerConnIdleDeadErr(conn, service->idleDeadErr);
2539 for (i = 0; i < RX_MAXCALLS; i++) {
2540 conn->twind[i] = rx_initSendWindow;
2541 conn->rwind[i] = rx_initReceiveWindow;
2543 /* Notify security object of the new connection */
2544 RXS_NewConnection(conn->securityObject, conn);
2545 /* XXXX Connection timeout? */
2546 if (service->newConnProc)
2547 (*service->newConnProc) (conn);
2548 if (rx_stats_active)
2549 rx_MutexIncrement(rx_stats.nServerConns, rx_stats_mutex);
2552 MUTEX_ENTER(&conn->conn_data_lock);
2554 MUTEX_EXIT(&conn->conn_data_lock);
2556 rxLastConn = conn; /* store this connection as the last conn used */
2557 MUTEX_EXIT(&rx_connHashTable_lock);
2561 /* There are two packet tracing routines available for testing and monitoring
2562 * Rx. One is called just after every packet is received and the other is
2563 * called just before every packet is sent. Received packets, have had their
2564 * headers decoded, and packets to be sent have not yet had their headers
2565 * encoded. Both take two parameters: a pointer to the packet and a sockaddr
2566 * containing the network address. Both can be modified. The return value, if
2567 * non-zero, indicates that the packet should be dropped. */
2569 int (*rx_justReceived) (struct rx_packet *, struct sockaddr_in *) = 0;
2570 int (*rx_almostSent) (struct rx_packet *, struct sockaddr_in *) = 0;
2572 /* A packet has been received off the interface. Np is the packet, socket is
2573 * the socket number it was received from (useful in determining which service
2574 * this packet corresponds to), and (host, port) reflect the host,port of the
2575 * sender. This call returns the packet to the caller if it is finished with
2576 * it, rather than de-allocating it, just as a small performance hack */
2579 rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
2580 afs_uint32 host, u_short port, int *tnop,
2581 struct rx_call **newcallp)
2583 struct rx_call *call;
2584 struct rx_connection *conn;
2586 afs_uint32 currentCallNumber;
2592 struct rx_packet *tnp;
2595 /* We don't print out the packet until now because (1) the time may not be
2596 * accurate enough until now in the lwp implementation (rx_Listener only gets
2597 * the time after the packet is read) and (2) from a protocol point of view,
2598 * this is the first time the packet has been seen */
2599 packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
2600 ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
2601 dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %x",
2602 np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
2603 np->header.epoch, np->header.cid, np->header.callNumber,
2604 np->header.seq, np->header.flags, np));
2607 if (np->header.type == RX_PACKET_TYPE_VERSION) {
2608 return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
2611 if (np->header.type == RX_PACKET_TYPE_DEBUG) {
2612 return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
2615 /* If an input tracer function is defined, call it with the packet and
2616 * network address. Note this function may modify its arguments. */
2617 if (rx_justReceived) {
2618 struct sockaddr_in addr;
2620 addr.sin_family = AF_INET;
2621 addr.sin_port = port;
2622 addr.sin_addr.s_addr = host;
2623 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
2624 addr.sin_len = sizeof(addr);
2625 #endif /* AFS_OSF_ENV */
2626 drop = (*rx_justReceived) (np, &addr);
2627 /* drop packet if return value is non-zero */
2630 port = addr.sin_port; /* in case fcn changed addr */
2631 host = addr.sin_addr.s_addr;
2635 /* If packet was not sent by the client, then *we* must be the client */
2636 type = ((np->header.flags & RX_CLIENT_INITIATED) != RX_CLIENT_INITIATED)
2637 ? RX_CLIENT_CONNECTION : RX_SERVER_CONNECTION;
2639 /* Find the connection (or fabricate one, if we're the server & if
2640 * necessary) associated with this packet */
2642 rxi_FindConnection(socket, host, port, np->header.serviceId,
2643 np->header.cid, np->header.epoch, type,
2644 np->header.securityIndex);
2647 /* If no connection found or fabricated, just ignore the packet.
2648 * (An argument could be made for sending an abort packet for
2653 MUTEX_ENTER(&conn->conn_data_lock);
2654 if (conn->maxSerial < np->header.serial)
2655 conn->maxSerial = np->header.serial;
2656 MUTEX_EXIT(&conn->conn_data_lock);
2658 /* If the connection is in an error state, send an abort packet and ignore
2659 * the incoming packet */
2661 /* Don't respond to an abort packet--we don't want loops! */
2662 MUTEX_ENTER(&conn->conn_data_lock);
2663 if (np->header.type != RX_PACKET_TYPE_ABORT)
2664 np = rxi_SendConnectionAbort(conn, np, 1, 0);
2666 MUTEX_EXIT(&conn->conn_data_lock);
2670 /* Check for connection-only requests (i.e. not call specific). */
2671 if (np->header.callNumber == 0) {
2672 switch (np->header.type) {
2673 case RX_PACKET_TYPE_ABORT: {
2674 /* What if the supplied error is zero? */
2675 afs_int32 errcode = ntohl(rx_GetInt32(np, 0));
2676 dpf(("rxi_ReceivePacket ABORT rx_GetInt32 = %d", errcode));
2677 rxi_ConnectionError(conn, errcode);
2678 MUTEX_ENTER(&conn->conn_data_lock);
2680 MUTEX_EXIT(&conn->conn_data_lock);
2683 case RX_PACKET_TYPE_CHALLENGE:
2684 tnp = rxi_ReceiveChallengePacket(conn, np, 1);
2685 MUTEX_ENTER(&conn->conn_data_lock);
2687 MUTEX_EXIT(&conn->conn_data_lock);
2689 case RX_PACKET_TYPE_RESPONSE:
2690 tnp = rxi_ReceiveResponsePacket(conn, np, 1);
2691 MUTEX_ENTER(&conn->conn_data_lock);
2693 MUTEX_EXIT(&conn->conn_data_lock);
2695 case RX_PACKET_TYPE_PARAMS:
2696 case RX_PACKET_TYPE_PARAMS + 1:
2697 case RX_PACKET_TYPE_PARAMS + 2:
2698 /* ignore these packet types for now */
2699 MUTEX_ENTER(&conn->conn_data_lock);
2701 MUTEX_EXIT(&conn->conn_data_lock);
2706 /* Should not reach here, unless the peer is broken: send an
2708 rxi_ConnectionError(conn, RX_PROTOCOL_ERROR);
2709 MUTEX_ENTER(&conn->conn_data_lock);
2710 tnp = rxi_SendConnectionAbort(conn, np, 1, 0);
2712 MUTEX_EXIT(&conn->conn_data_lock);
2717 channel = np->header.cid & RX_CHANNELMASK;
2718 call = conn->call[channel];
2719 #ifdef RX_ENABLE_LOCKS
2721 MUTEX_ENTER(&call->lock);
2722 /* Test to see if call struct is still attached to conn. */
2723 if (call != conn->call[channel]) {
2725 MUTEX_EXIT(&call->lock);
2726 if (type == RX_SERVER_CONNECTION) {
2727 call = conn->call[channel];
2728 /* If we started with no call attached and there is one now,
2729 * another thread is also running this routine and has gotten
2730 * the connection channel. We should drop this packet in the tests
2731 * below. If there was a call on this connection and it's now
2732 * gone, then we'll be making a new call below.
2733 * If there was previously a call and it's now different then
2734 * the old call was freed and another thread running this routine
2735 * has created a call on this channel. One of these two threads
2736 * has a packet for the old call and the code below handles those
2740 MUTEX_ENTER(&call->lock);
2742 /* This packet can't be for this call. If the new call address is
2743 * 0 then no call is running on this channel. If there is a call
2744 * then, since this is a client connection we're getting data for
2745 * it must be for the previous call.
2747 if (rx_stats_active)
2748 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2749 MUTEX_ENTER(&conn->conn_data_lock);
2751 MUTEX_EXIT(&conn->conn_data_lock);
2756 currentCallNumber = conn->callNumber[channel];
2758 if (type == RX_SERVER_CONNECTION) { /* We're the server */
2759 if (np->header.callNumber < currentCallNumber) {
2760 if (rx_stats_active)
2761 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2762 #ifdef RX_ENABLE_LOCKS
2764 MUTEX_EXIT(&call->lock);
2766 MUTEX_ENTER(&conn->conn_data_lock);
2768 MUTEX_EXIT(&conn->conn_data_lock);
2772 MUTEX_ENTER(&conn->conn_call_lock);
2773 call = rxi_NewCall(conn, channel);
2774 MUTEX_EXIT(&conn->conn_call_lock);
2775 *call->callNumber = np->header.callNumber;
2777 if (np->header.callNumber == 0)
2778 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %lx resend %d.%0.3d len %d", np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port), np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq, np->header.flags, (unsigned long)np, np->retryTime.sec, np->retryTime.usec / 1000, np->length));
2780 call->state = RX_STATE_PRECALL;
2781 clock_GetTime(&call->queueTime);
2782 hzero(call->bytesSent);
2783 hzero(call->bytesRcvd);
2785 * If the number of queued calls exceeds the overload
2786 * threshold then abort this call.
2788 if ((rx_BusyThreshold > 0) && (rx_nWaiting > rx_BusyThreshold)) {
2789 struct rx_packet *tp;
2791 rxi_CallError(call, rx_BusyError);
2792 tp = rxi_SendCallAbort(call, np, 1, 0);
2793 MUTEX_EXIT(&call->lock);
2794 MUTEX_ENTER(&conn->conn_data_lock);
2796 MUTEX_EXIT(&conn->conn_data_lock);
2797 if (rx_stats_active)
2798 rx_MutexIncrement(rx_stats.nBusies, rx_stats_mutex);
2801 rxi_KeepAliveOn(call);
2802 } else if (np->header.callNumber != currentCallNumber) {
2803 /* Wait until the transmit queue is idle before deciding
2804 * whether to reset the current call. Chances are that the
2805 * call will be in ether DALLY or HOLD state once the TQ_BUSY
2808 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2809 while ((call->state == RX_STATE_ACTIVE)
2810 && (call->flags & RX_CALL_TQ_BUSY)) {
2811 call->flags |= RX_CALL_TQ_WAIT;
2813 #ifdef RX_ENABLE_LOCKS
2814 osirx_AssertMine(&call->lock, "rxi_Start lock3");
2815 CV_WAIT(&call->cv_tq, &call->lock);
2816 #else /* RX_ENABLE_LOCKS */
2817 osi_rxSleep(&call->tq);
2818 #endif /* RX_ENABLE_LOCKS */
2820 if (call->tqWaiters == 0)
2821 call->flags &= ~RX_CALL_TQ_WAIT;
2823 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2824 /* If the new call cannot be taken right now send a busy and set
2825 * the error condition in this call, so that it terminates as
2826 * quickly as possible */
2827 if (call->state == RX_STATE_ACTIVE) {
2828 struct rx_packet *tp;
2830 rxi_CallError(call, RX_CALL_DEAD);
2831 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
2833 MUTEX_EXIT(&call->lock);
2834 MUTEX_ENTER(&conn->conn_data_lock);
2836 MUTEX_EXIT(&conn->conn_data_lock);
2839 rxi_ResetCall(call, 0);
2840 *call->callNumber = np->header.callNumber;
2842 if (np->header.callNumber == 0)
2843 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %lx resend %d.%0.3d len %d", np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port), np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq, np->header.flags, (unsigned long)np, np->retryTime.sec, np->retryTime.usec / 1000, np->length));
2845 call->state = RX_STATE_PRECALL;
2846 clock_GetTime(&call->queueTime);
2847 hzero(call->bytesSent);
2848 hzero(call->bytesRcvd);
2850 * If the number of queued calls exceeds the overload
2851 * threshold then abort this call.
2853 if ((rx_BusyThreshold > 0) && (rx_nWaiting > rx_BusyThreshold)) {
2854 struct rx_packet *tp;
2856 rxi_CallError(call, rx_BusyError);
2857 tp = rxi_SendCallAbort(call, np, 1, 0);
2858 MUTEX_EXIT(&call->lock);
2859 MUTEX_ENTER(&conn->conn_data_lock);
2861 MUTEX_EXIT(&conn->conn_data_lock);
2862 if (rx_stats_active)
2863 rx_MutexIncrement(rx_stats.nBusies, rx_stats_mutex);
2866 rxi_KeepAliveOn(call);
2868 /* Continuing call; do nothing here. */
2870 } else { /* we're the client */
2871 /* Ignore all incoming acknowledgements for calls in DALLY state */
2872 if (call && (call->state == RX_STATE_DALLY)
2873 && (np->header.type == RX_PACKET_TYPE_ACK)) {
2874 if (rx_stats_active)
2875 rx_MutexIncrement(rx_stats.ignorePacketDally, rx_stats_mutex);
2876 #ifdef RX_ENABLE_LOCKS
2878 MUTEX_EXIT(&call->lock);
2881 MUTEX_ENTER(&conn->conn_data_lock);
2883 MUTEX_EXIT(&conn->conn_data_lock);
2887 /* Ignore anything that's not relevant to the current call. If there
2888 * isn't a current call, then no packet is relevant. */
2889 if (!call || (np->header.callNumber != currentCallNumber)) {
2890 if (rx_stats_active)
2891 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2892 #ifdef RX_ENABLE_LOCKS
2894 MUTEX_EXIT(&call->lock);
2897 MUTEX_ENTER(&conn->conn_data_lock);
2899 MUTEX_EXIT(&conn->conn_data_lock);
2902 /* If the service security object index stamped in the packet does not
2903 * match the connection's security index, ignore the packet */
2904 if (np->header.securityIndex != conn->securityIndex) {
2905 #ifdef RX_ENABLE_LOCKS
2906 MUTEX_EXIT(&call->lock);
2908 MUTEX_ENTER(&conn->conn_data_lock);
2910 MUTEX_EXIT(&conn->conn_data_lock);
2914 /* If we're receiving the response, then all transmit packets are
2915 * implicitly acknowledged. Get rid of them. */
2916 if (np->header.type == RX_PACKET_TYPE_DATA) {
2917 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2918 /* XXX Hack. Because we must release the global rx lock when
2919 * sending packets (osi_NetSend) we drop all acks while we're
2920 * traversing the tq in rxi_Start sending packets out because
2921 * packets may move to the freePacketQueue as result of being here!
2922 * So we drop these packets until we're safely out of the
2923 * traversing. Really ugly!
2924 * For fine grain RX locking, we set the acked field in the
2925 * packets and let rxi_Start remove them from the transmit queue.
2927 if (call->flags & RX_CALL_TQ_BUSY) {
2928 #ifdef RX_ENABLE_LOCKS
2929 rxi_SetAcksInTransmitQueue(call);
2932 return np; /* xmitting; drop packet */
2935 rxi_ClearTransmitQueue(call, 0);
2937 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2938 rxi_ClearTransmitQueue(call, 0);
2939 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2941 if (np->header.type == RX_PACKET_TYPE_ACK) {
2942 /* now check to see if this is an ack packet acknowledging that the
2943 * server actually *lost* some hard-acked data. If this happens we
2944 * ignore this packet, as it may indicate that the server restarted in
2945 * the middle of a call. It is also possible that this is an old ack
2946 * packet. We don't abort the connection in this case, because this
2947 * *might* just be an old ack packet. The right way to detect a server
2948 * restart in the midst of a call is to notice that the server epoch
2950 /* XXX I'm not sure this is exactly right, since tfirst **IS**
2951 * XXX unacknowledged. I think that this is off-by-one, but
2952 * XXX I don't dare change it just yet, since it will
2953 * XXX interact badly with the server-restart detection
2954 * XXX code in receiveackpacket. */
2955 if (ntohl(rx_GetInt32(np, FIRSTACKOFFSET)) < call->tfirst) {
2956 if (rx_stats_active)
2957 rx_MutexIncrement(rx_stats.spuriousPacketsRead, rx_stats_mutex);
2958 MUTEX_EXIT(&call->lock);
2959 MUTEX_ENTER(&conn->conn_data_lock);
2961 MUTEX_EXIT(&conn->conn_data_lock);
2965 } /* else not a data packet */
2968 osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
2969 /* Set remote user defined status from packet */
2970 call->remoteStatus = np->header.userStatus;
2972 /* Note the gap between the expected next packet and the actual
2973 * packet that arrived, when the new packet has a smaller serial number
2974 * than expected. Rioses frequently reorder packets all by themselves,
2975 * so this will be quite important with very large window sizes.
2976 * Skew is checked against 0 here to avoid any dependence on the type of
2977 * inPacketSkew (which may be unsigned). In C, -1 > (unsigned) 0 is always
2979 * The inPacketSkew should be a smoothed running value, not just a maximum. MTUXXX
2980 * see CalculateRoundTripTime for an example of how to keep smoothed values.
2981 * I think using a beta of 1/8 is probably appropriate. 93.04.21
2983 MUTEX_ENTER(&conn->conn_data_lock);
2984 skew = conn->lastSerial - np->header.serial;
2985 conn->lastSerial = np->header.serial;
2986 MUTEX_EXIT(&conn->conn_data_lock);
2988 struct rx_peer *peer;
2990 if (skew > peer->inPacketSkew) {
2991 dpf(("*** In skew changed from %d to %d\n", peer->inPacketSkew,
2993 peer->inPacketSkew = skew;
2997 /* Now do packet type-specific processing */
2998 switch (np->header.type) {
2999 case RX_PACKET_TYPE_DATA:
3000 np = rxi_ReceiveDataPacket(call, np, 1, socket, host, port, tnop,
3003 case RX_PACKET_TYPE_ACK:
3004 /* Respond immediately to ack packets requesting acknowledgement
3006 if (np->header.flags & RX_REQUEST_ACK) {
3008 (void)rxi_SendCallAbort(call, 0, 1, 0);
3010 (void)rxi_SendAck(call, 0, np->header.serial,
3011 RX_ACK_PING_RESPONSE, 1);
3013 np = rxi_ReceiveAckPacket(call, np, 1);
3015 case RX_PACKET_TYPE_ABORT: {
3016 /* An abort packet: reset the call, passing the error up to the user. */
3017 /* What if error is zero? */
3018 /* What if the error is -1? the application will treat it as a timeout. */
3019 afs_int32 errdata = ntohl(*(afs_int32 *) rx_DataOf(np));
3020 dpf(("rxi_ReceivePacket ABORT rx_DataOf = %d", errdata));
3021 rxi_CallError(call, errdata);
3022 MUTEX_EXIT(&call->lock);
3023 MUTEX_ENTER(&conn->conn_data_lock);
3025 MUTEX_EXIT(&conn->conn_data_lock);
3026 return np; /* xmitting; drop packet */
3028 case RX_PACKET_TYPE_BUSY:
3031 case RX_PACKET_TYPE_ACKALL:
3032 /* All packets acknowledged, so we can drop all packets previously
3033 * readied for sending */
3034 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3035 /* XXX Hack. We because we can't release the global rx lock when
3036 * sending packets (osi_NetSend) we drop all ack pkts while we're
3037 * traversing the tq in rxi_Start sending packets out because
3038 * packets may move to the freePacketQueue as result of being
3039 * here! So we drop these packets until we're safely out of the
3040 * traversing. Really ugly!
3041 * For fine grain RX locking, we set the acked field in the packets
3042 * and let rxi_Start remove the packets from the transmit queue.
3044 if (call->flags & RX_CALL_TQ_BUSY) {
3045 #ifdef RX_ENABLE_LOCKS
3046 rxi_SetAcksInTransmitQueue(call);
3048 #else /* RX_ENABLE_LOCKS */
3049 MUTEX_EXIT(&call->lock);
3050 MUTEX_ENTER(&conn->conn_data_lock);
3052 MUTEX_EXIT(&conn->conn_data_lock);
3053 return np; /* xmitting; drop packet */
3054 #endif /* RX_ENABLE_LOCKS */
3056 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3057 rxi_ClearTransmitQueue(call, 0);
3058 rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
3061 /* Should not reach here, unless the peer is broken: send an abort
3063 rxi_CallError(call, RX_PROTOCOL_ERROR);
3064 np = rxi_SendCallAbort(call, np, 1, 0);
3067 /* Note when this last legitimate packet was received, for keep-alive
3068 * processing. Note, we delay getting the time until now in the hope that
3069 * the packet will be delivered to the user before any get time is required
3070 * (if not, then the time won't actually be re-evaluated here). */
3071 call->lastReceiveTime = clock_Sec();
3072 MUTEX_EXIT(&call->lock);
3073 MUTEX_ENTER(&conn->conn_data_lock);
3075 MUTEX_EXIT(&conn->conn_data_lock);
3079 /* return true if this is an "interesting" connection from the point of view
3080 of someone trying to debug the system */
3082 rxi_IsConnInteresting(struct rx_connection *aconn)
3085 struct rx_call *tcall;
3087 if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
3089 for (i = 0; i < RX_MAXCALLS; i++) {
3090 tcall = aconn->call[i];
3092 if ((tcall->state == RX_STATE_PRECALL)
3093 || (tcall->state == RX_STATE_ACTIVE))
3095 if ((tcall->mode == RX_MODE_SENDING)
3096 || (tcall->mode == RX_MODE_RECEIVING))
3104 /* if this is one of the last few packets AND it wouldn't be used by the
3105 receiving call to immediately satisfy a read request, then drop it on
3106 the floor, since accepting it might prevent a lock-holding thread from
3107 making progress in its reading. If a call has been cleared while in
3108 the precall state then ignore all subsequent packets until the call
3109 is assigned to a thread. */
3112 TooLow(struct rx_packet *ap, struct rx_call *acall)
3116 MUTEX_ENTER(&rx_quota_mutex);
3117 if (((ap->header.seq != 1) && (acall->flags & RX_CALL_CLEARED)
3118 && (acall->state == RX_STATE_PRECALL))
3119 || ((rx_nFreePackets < rxi_dataQuota + 2)
3120 && !((ap->header.seq < acall->rnext + rx_initSendWindow)
3121 && (acall->flags & RX_CALL_READER_WAIT)))) {
3124 MUTEX_EXIT(&rx_quota_mutex);
3130 rxi_CheckReachEvent(struct rxevent *event, void *arg1, void *arg2)
3132 struct rx_connection *conn = arg1;
3133 struct rx_call *acall = arg2;
3134 struct rx_call *call = acall;
3135 struct clock when, now;
3138 MUTEX_ENTER(&conn->conn_data_lock);
3139 conn->checkReachEvent = NULL;
3140 waiting = conn->flags & RX_CONN_ATTACHWAIT;
3143 MUTEX_EXIT(&conn->conn_data_lock);
3147 MUTEX_ENTER(&conn->conn_call_lock);
3148 MUTEX_ENTER(&conn->conn_data_lock);
3149 for (i = 0; i < RX_MAXCALLS; i++) {
3150 struct rx_call *tc = conn->call[i];
3151 if (tc && tc->state == RX_STATE_PRECALL) {
3157 /* Indicate that rxi_CheckReachEvent is no longer running by
3158 * clearing the flag. Must be atomic under conn_data_lock to
3159 * avoid a new call slipping by: rxi_CheckConnReach holds
3160 * conn_data_lock while checking RX_CONN_ATTACHWAIT.
3162 conn->flags &= ~RX_CONN_ATTACHWAIT;
3163 MUTEX_EXIT(&conn->conn_data_lock);
3164 MUTEX_EXIT(&conn->conn_call_lock);
3169 MUTEX_ENTER(&call->lock);
3170 rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3172 MUTEX_EXIT(&call->lock);
3174 clock_GetTime(&now);
3176 when.sec += RX_CHECKREACH_TIMEOUT;
3177 MUTEX_ENTER(&conn->conn_data_lock);
3178 if (!conn->checkReachEvent) {
3180 conn->checkReachEvent =
3181 rxevent_PostNow(&when, &now, rxi_CheckReachEvent, conn,
3184 MUTEX_EXIT(&conn->conn_data_lock);
3190 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3192 struct rx_service *service = conn->service;
3193 struct rx_peer *peer = conn->peer;
3194 afs_uint32 now, lastReach;
3196 if (service->checkReach == 0)
3200 MUTEX_ENTER(&peer->peer_lock);
3201 lastReach = peer->lastReachTime;
3202 MUTEX_EXIT(&peer->peer_lock);
3203 if (now - lastReach < RX_CHECKREACH_TTL)
3206 MUTEX_ENTER(&conn->conn_data_lock);
3207 if (conn->flags & RX_CONN_ATTACHWAIT) {
3208 MUTEX_EXIT(&conn->conn_data_lock);
3211 conn->flags |= RX_CONN_ATTACHWAIT;
3212 MUTEX_EXIT(&conn->conn_data_lock);
3213 if (!conn->checkReachEvent)
3214 rxi_CheckReachEvent(NULL, conn, call);
3219 /* try to attach call, if authentication is complete */
3221 TryAttach(struct rx_call *acall, osi_socket socket,
3222 int *tnop, struct rx_call **newcallp,
3225 struct rx_connection *conn = acall->conn;
3227 if (conn->type == RX_SERVER_CONNECTION
3228 && acall->state == RX_STATE_PRECALL) {
3229 /* Don't attach until we have any req'd. authentication. */
3230 if (RXS_CheckAuthentication(conn->securityObject, conn) == 0) {
3231 if (reachOverride || rxi_CheckConnReach(conn, acall) == 0)
3232 rxi_AttachServerProc(acall, socket, tnop, newcallp);
3233 /* Note: this does not necessarily succeed; there
3234 * may not any proc available
3237 rxi_ChallengeOn(acall->conn);
3242 /* A data packet has been received off the interface. This packet is
3243 * appropriate to the call (the call is in the right state, etc.). This
3244 * routine can return a packet to the caller, for re-use */
3247 rxi_ReceiveDataPacket(struct rx_call *call,
3248 struct rx_packet *np, int istack,
3249 osi_socket socket, afs_uint32 host, u_short port,
3250 int *tnop, struct rx_call **newcallp)
3252 int ackNeeded = 0; /* 0 means no, otherwise ack_reason */
3257 afs_uint32 serial=0, flags=0;
3259 struct rx_packet *tnp;
3260 struct clock when, now;
3261 if (rx_stats_active)
3262 rx_MutexIncrement(rx_stats.dataPacketsRead, rx_stats_mutex);
3265 /* If there are no packet buffers, drop this new packet, unless we can find
3266 * packet buffers from inactive calls */
3268 && (rxi_OverQuota(RX_PACKET_CLASS_RECEIVE) || TooLow(np, call))) {
3269 MUTEX_ENTER(&rx_freePktQ_lock);
3270 rxi_NeedMorePackets = TRUE;
3271 MUTEX_EXIT(&rx_freePktQ_lock);
3272 if (rx_stats_active)
3273 rx_MutexIncrement(rx_stats.noPacketBuffersOnRead, rx_stats_mutex);
3274 call->rprev = np->header.serial;
3275 rxi_calltrace(RX_TRACE_DROP, call);
3276 dpf(("packet %x dropped on receipt - quota problems", np));
3278 rxi_ClearReceiveQueue(call);
3279 clock_GetTime(&now);
3281 clock_Add(&when, &rx_softAckDelay);
3282 if (!call->delayedAckEvent
3283 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3284 rxevent_Cancel(call->delayedAckEvent, call,
3285 RX_CALL_REFCOUNT_DELAY);
3286 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3287 call->delayedAckEvent =
3288 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3290 /* we've damaged this call already, might as well do it in. */
3296 * New in AFS 3.5, if the RX_JUMBO_PACKET flag is set then this
3297 * packet is one of several packets transmitted as a single
3298 * datagram. Do not send any soft or hard acks until all packets
3299 * in a jumbogram have been processed. Send negative acks right away.
3301 for (isFirst = 1, tnp = NULL; isFirst || tnp; isFirst = 0) {
3302 /* tnp is non-null when there are more packets in the
3303 * current jumbo gram */
3310 seq = np->header.seq;
3311 serial = np->header.serial;
3312 flags = np->header.flags;
3314 /* If the call is in an error state, send an abort message */
3316 return rxi_SendCallAbort(call, np, istack, 0);
3318 /* The RX_JUMBO_PACKET is set in all but the last packet in each
3319 * AFS 3.5 jumbogram. */
3320 if (flags & RX_JUMBO_PACKET) {
3321 tnp = rxi_SplitJumboPacket(np, host, port, isFirst);
3326 if (np->header.spare != 0) {
3327 MUTEX_ENTER(&call->conn->conn_data_lock);
3328 call->conn->flags |= RX_CONN_USING_PACKET_CKSUM;
3329 MUTEX_EXIT(&call->conn->conn_data_lock);
3332 /* The usual case is that this is the expected next packet */
3333 if (seq == call->rnext) {
3335 /* Check to make sure it is not a duplicate of one already queued */
3336 if (queue_IsNotEmpty(&call->rq)
3337 && queue_First(&call->rq, rx_packet)->header.seq == seq) {
3338 if (rx_stats_active)
3339 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3340 dpf(("packet %x dropped on receipt - duplicate", np));
3341 rxevent_Cancel(call->delayedAckEvent, call,
3342 RX_CALL_REFCOUNT_DELAY);
3343 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3349 /* It's the next packet. Stick it on the receive queue
3350 * for this call. Set newPackets to make sure we wake
3351 * the reader once all packets have been processed */
3352 np->flags |= RX_PKTFLAG_RQ;
3353 queue_Prepend(&call->rq, np);
3354 #ifdef RXDEBUG_PACKET
3356 #endif /* RXDEBUG_PACKET */
3358 np = NULL; /* We can't use this anymore */
3361 /* If an ack is requested then set a flag to make sure we
3362 * send an acknowledgement for this packet */
3363 if (flags & RX_REQUEST_ACK) {
3364 ackNeeded = RX_ACK_REQUESTED;
3367 /* Keep track of whether we have received the last packet */
3368 if (flags & RX_LAST_PACKET) {
3369 call->flags |= RX_CALL_HAVE_LAST;
3373 /* Check whether we have all of the packets for this call */
3374 if (call->flags & RX_CALL_HAVE_LAST) {
3375 afs_uint32 tseq; /* temporary sequence number */
3376 struct rx_packet *tp; /* Temporary packet pointer */
3377 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3379 for (tseq = seq, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3380 if (tseq != tp->header.seq)
3382 if (tp->header.flags & RX_LAST_PACKET) {
3383 call->flags |= RX_CALL_RECEIVE_DONE;
3390 /* Provide asynchronous notification for those who want it
3391 * (e.g. multi rx) */
3392 if (call->arrivalProc) {
3393 (*call->arrivalProc) (call, call->arrivalProcHandle,
3394 call->arrivalProcArg);
3395 call->arrivalProc = (void (*)())0;
3398 /* Update last packet received */
3401 /* If there is no server process serving this call, grab
3402 * one, if available. We only need to do this once. If a
3403 * server thread is available, this thread becomes a server
3404 * thread and the server thread becomes a listener thread. */
3406 TryAttach(call, socket, tnop, newcallp, 0);
3409 /* This is not the expected next packet. */
3411 /* Determine whether this is a new or old packet, and if it's
3412 * a new one, whether it fits into the current receive window.
3413 * Also figure out whether the packet was delivered in sequence.
3414 * We use the prev variable to determine whether the new packet
3415 * is the successor of its immediate predecessor in the
3416 * receive queue, and the missing flag to determine whether
3417 * any of this packets predecessors are missing. */
3419 afs_uint32 prev; /* "Previous packet" sequence number */
3420 struct rx_packet *tp; /* Temporary packet pointer */
3421 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3422 int missing; /* Are any predecessors missing? */
3424 /* If the new packet's sequence number has been sent to the
3425 * application already, then this is a duplicate */
3426 if (seq < call->rnext) {
3427 if (rx_stats_active)
3428 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3429 rxevent_Cancel(call->delayedAckEvent, call,
3430 RX_CALL_REFCOUNT_DELAY);
3431 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3437 /* If the sequence number is greater than what can be
3438 * accomodated by the current window, then send a negative
3439 * acknowledge and drop the packet */
3440 if ((call->rnext + call->rwind) <= seq) {
3441 rxevent_Cancel(call->delayedAckEvent, call,
3442 RX_CALL_REFCOUNT_DELAY);
3443 np = rxi_SendAck(call, np, serial, RX_ACK_EXCEEDS_WINDOW,
3450 /* Look for the packet in the queue of old received packets */
3451 for (prev = call->rnext - 1, missing =
3452 0, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3453 /*Check for duplicate packet */
3454 if (seq == tp->header.seq) {
3455 if (rx_stats_active)
3456 rx_MutexIncrement(rx_stats.dupPacketsRead, rx_stats_mutex);
3457 rxevent_Cancel(call->delayedAckEvent, call,
3458 RX_CALL_REFCOUNT_DELAY);
3459 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE,
3465 /* If we find a higher sequence packet, break out and
3466 * insert the new packet here. */
3467 if (seq < tp->header.seq)
3469 /* Check for missing packet */
3470 if (tp->header.seq != prev + 1) {
3474 prev = tp->header.seq;
3477 /* Keep track of whether we have received the last packet. */
3478 if (flags & RX_LAST_PACKET) {
3479 call->flags |= RX_CALL_HAVE_LAST;
3482 /* It's within the window: add it to the the receive queue.
3483 * tp is left by the previous loop either pointing at the
3484 * packet before which to insert the new packet, or at the
3485 * queue head if the queue is empty or the packet should be
3487 np->flags |= RX_PKTFLAG_RQ;
3488 #ifdef RXDEBUG_PACKET
3490 #endif /* RXDEBUG_PACKET */
3491 queue_InsertBefore(tp, np);
3495 /* Check whether we have all of the packets for this call */
3496 if ((call->flags & RX_CALL_HAVE_LAST)
3497 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
3498 afs_uint32 tseq; /* temporary sequence number */
3501 call->rnext, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3502 if (tseq != tp->header.seq)
3504 if (tp->header.flags & RX_LAST_PACKET) {
3505 call->flags |= RX_CALL_RECEIVE_DONE;
3512 /* We need to send an ack of the packet is out of sequence,
3513 * or if an ack was requested by the peer. */
3514 if (seq != prev + 1 || missing) {
3515 ackNeeded = RX_ACK_OUT_OF_SEQUENCE;
3516 } else if (flags & RX_REQUEST_ACK) {
3517 ackNeeded = RX_ACK_REQUESTED;
3520 /* Acknowledge the last packet for each call */
3521 if (flags & RX_LAST_PACKET) {
3532 * If the receiver is waiting for an iovec, fill the iovec
3533 * using the data from the receive queue */
3534 if (call->flags & RX_CALL_IOVEC_WAIT) {
3535 didHardAck = rxi_FillReadVec(call, serial);
3536 /* the call may have been aborted */
3545 /* Wakeup the reader if any */
3546 if ((call->flags & RX_CALL_READER_WAIT)
3547 && (!(call->flags & RX_CALL_IOVEC_WAIT) || !(call->iovNBytes)
3548 || (call->iovNext >= call->iovMax)
3549 || (call->flags & RX_CALL_RECEIVE_DONE))) {
3550 call->flags &= ~RX_CALL_READER_WAIT;
3551 #ifdef RX_ENABLE_LOCKS
3552 CV_BROADCAST(&call->cv_rq);
3554 osi_rxWakeup(&call->rq);
3560 * Send an ack when requested by the peer, or once every
3561 * rxi_SoftAckRate packets until the last packet has been
3562 * received. Always send a soft ack for the last packet in
3563 * the server's reply. */
3565 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3566 np = rxi_SendAck(call, np, serial, ackNeeded, istack);
3567 } else if (call->nSoftAcks > (u_short) rxi_SoftAckRate) {
3568 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3569 np = rxi_SendAck(call, np, serial, RX_ACK_IDLE, istack);
3570 } else if (call->nSoftAcks) {
3571 clock_GetTime(&now);
3573 if (haveLast && !(flags & RX_CLIENT_INITIATED)) {
3574 clock_Add(&when, &rx_lastAckDelay);
3576 clock_Add(&when, &rx_softAckDelay);
3578 if (!call->delayedAckEvent
3579 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3580 rxevent_Cancel(call->delayedAckEvent, call,
3581 RX_CALL_REFCOUNT_DELAY);
3582 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3583 call->delayedAckEvent =
3584 rxevent_PostNow(&when, &now, rxi_SendDelayedAck, call, 0);
3586 } else if (call->flags & RX_CALL_RECEIVE_DONE) {
3587 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3594 static void rxi_ComputeRate();
3598 rxi_UpdatePeerReach(struct rx_connection *conn, struct rx_call *acall)
3600 struct rx_peer *peer = conn->peer;
3602 MUTEX_ENTER(&peer->peer_lock);
3603 peer->lastReachTime = clock_Sec();
3604 MUTEX_EXIT(&peer->peer_lock);
3606 MUTEX_ENTER(&conn->conn_data_lock);
3607 if (conn->flags & RX_CONN_ATTACHWAIT) {
3610 conn->flags &= ~RX_CONN_ATTACHWAIT;
3611 MUTEX_EXIT(&conn->conn_data_lock);
3613 for (i = 0; i < RX_MAXCALLS; i++) {
3614 struct rx_call *call = conn->call[i];
3617 MUTEX_ENTER(&call->lock);
3618 /* tnop can be null if newcallp is null */
3619 TryAttach(call, (osi_socket) - 1, NULL, NULL, 1);
3621 MUTEX_EXIT(&call->lock);
3625 MUTEX_EXIT(&conn->conn_data_lock);
3629 rx_ack_reason(int reason)
3632 case RX_ACK_REQUESTED:
3634 case RX_ACK_DUPLICATE:
3636 case RX_ACK_OUT_OF_SEQUENCE:
3638 case RX_ACK_EXCEEDS_WINDOW:
3640 case RX_ACK_NOSPACE:
3644 case RX_ACK_PING_RESPONSE:
3656 /* rxi_ComputePeerNetStats
3658 * Called exclusively by rxi_ReceiveAckPacket to compute network link
3659 * estimates (like RTT and throughput) based on ack packets. Caller
3660 * must ensure that the packet in question is the right one (i.e.
3661 * serial number matches).
3664 rxi_ComputePeerNetStats(struct rx_call *call, struct rx_packet *p,
3665 struct rx_ackPacket *ap, struct rx_packet *np)
3667 struct rx_peer *peer = call->conn->peer;
3669 /* Use RTT if not delayed by client. */
3670 if (ap->reason != RX_ACK_DELAY)
3671 rxi_ComputeRoundTripTime(p, &p->timeSent, peer);
3673 rxi_ComputeRate(peer, call, p, np, ap->reason);
3677 /* The real smarts of the whole thing. */
3679 rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
3682 struct rx_ackPacket *ap;
3684 struct rx_packet *tp;
3685 struct rx_packet *nxp; /* Next packet pointer for queue_Scan */
3686 struct rx_connection *conn = call->conn;
3687 struct rx_peer *peer = conn->peer;
3690 /* because there are CM's that are bogus, sending weird values for this. */
3691 afs_uint32 skew = 0;
3696 int newAckCount = 0;
3697 u_short maxMTU = 0; /* Set if peer supports AFS 3.4a jumbo datagrams */
3698 int maxDgramPackets = 0; /* Set if peer supports AFS 3.5 jumbo datagrams */
3700 if (rx_stats_active)
3701 rx_MutexIncrement(rx_stats.ackPacketsRead, rx_stats_mutex);
3702 ap = (struct rx_ackPacket *)rx_DataOf(np);
3703 nbytes = rx_Contiguous(np) - (int)((ap->acks) - (u_char *) ap);
3705 return np; /* truncated ack packet */