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"
36 #include "afs/afs_args.h"
37 #include "afs/afs_osi.h"
38 #ifdef RX_KERNEL_TRACE
39 #include "rx_kcommon.h"
41 #if (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
45 #undef RXDEBUG /* turn off debugging */
47 #if defined(AFS_SGI_ENV)
48 #include "sys/debug.h"
57 #endif /* AFS_OSF_ENV */
59 #include "afs/sysincludes.h"
60 #include "afsincludes.h"
63 #include "rx_kmutex.h"
64 #include "rx_kernel.h"
68 #include "rx_globals.h"
70 #define AFSOP_STOP_RXCALLBACK 210 /* Stop CALLBACK process */
71 #define AFSOP_STOP_AFS 211 /* Stop AFS process */
72 #define AFSOP_STOP_BKG 212 /* Stop BKG process */
74 extern afs_int32 afs_termState;
76 #include "sys/lockl.h"
77 #include "sys/lock_def.h"
78 #endif /* AFS_AIX41_ENV */
79 # include "rxgen_consts.h"
81 # include <sys/types.h>
86 # include <afs/afsutil.h>
87 # include <WINNT\afsreg.h>
89 # include <sys/socket.h>
90 # include <sys/file.h>
92 # include <sys/stat.h>
93 # include <netinet/in.h>
94 # 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>
112 int (*registerProgram) () = 0;
113 int (*swapNameProgram) () = 0;
115 /* Local static routines */
116 static void rxi_DestroyConnectionNoLock(register struct rx_connection *conn);
117 #ifdef RX_ENABLE_LOCKS
118 static void rxi_SetAcksInTransmitQueue(register struct rx_call *call);
121 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
123 afs_int32 rxi_start_aborted; /* rxi_start awoke after rxi_Send in error. */
124 afs_int32 rxi_start_in_error;
126 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
129 * rxi_rpc_peer_stat_cnt counts the total number of peer stat structures
130 * currently allocated within rx. This number is used to allocate the
131 * memory required to return the statistics when queried.
134 static unsigned int rxi_rpc_peer_stat_cnt;
137 * rxi_rpc_process_stat_cnt counts the total number of local process stat
138 * structures currently allocated within rx. The number is used to allocate
139 * the memory required to return the statistics when queried.
142 static unsigned int rxi_rpc_process_stat_cnt;
144 #if !defined(offsetof)
145 #include <stddef.h> /* for definition of offsetof() */
148 #ifdef AFS_PTHREAD_ENV
152 * Use procedural initialization of mutexes/condition variables
156 extern pthread_mutex_t rx_stats_mutex;
157 extern pthread_mutex_t des_init_mutex;
158 extern pthread_mutex_t des_random_mutex;
159 extern pthread_mutex_t rx_clock_mutex;
160 extern pthread_mutex_t rxi_connCacheMutex;
161 extern pthread_mutex_t rx_event_mutex;
162 extern pthread_mutex_t osi_malloc_mutex;
163 extern pthread_mutex_t event_handler_mutex;
164 extern pthread_mutex_t listener_mutex;
165 extern pthread_mutex_t rx_if_init_mutex;
166 extern pthread_mutex_t rx_if_mutex;
167 extern pthread_mutex_t rxkad_client_uid_mutex;
168 extern pthread_mutex_t rxkad_random_mutex;
170 extern pthread_cond_t rx_event_handler_cond;
171 extern pthread_cond_t rx_listener_cond;
173 static pthread_mutex_t epoch_mutex;
174 static pthread_mutex_t rx_init_mutex;
175 static pthread_mutex_t rx_debug_mutex;
178 rxi_InitPthread(void)
180 assert(pthread_mutex_init(&rx_clock_mutex, (const pthread_mutexattr_t *)0)
182 assert(pthread_mutex_init(&rx_stats_mutex, (const pthread_mutexattr_t *)0)
184 assert(pthread_mutex_init
185 (&rxi_connCacheMutex, (const pthread_mutexattr_t *)0) == 0);
186 assert(pthread_mutex_init(&rx_init_mutex, (const pthread_mutexattr_t *)0)
188 assert(pthread_mutex_init(&epoch_mutex, (const pthread_mutexattr_t *)0) ==
190 assert(pthread_mutex_init(&rx_event_mutex, (const pthread_mutexattr_t *)0)
192 assert(pthread_mutex_init(&des_init_mutex, (const pthread_mutexattr_t *)0)
194 assert(pthread_mutex_init
195 (&des_random_mutex, (const pthread_mutexattr_t *)0) == 0);
196 assert(pthread_mutex_init
197 (&osi_malloc_mutex, (const pthread_mutexattr_t *)0) == 0);
198 assert(pthread_mutex_init
199 (&event_handler_mutex, (const pthread_mutexattr_t *)0) == 0);
200 assert(pthread_mutex_init(&listener_mutex, (const pthread_mutexattr_t *)0)
202 assert(pthread_mutex_init
203 (&rx_if_init_mutex, (const pthread_mutexattr_t *)0) == 0);
204 assert(pthread_mutex_init(&rx_if_mutex, (const pthread_mutexattr_t *)0) ==
206 assert(pthread_mutex_init
207 (&rxkad_client_uid_mutex, (const pthread_mutexattr_t *)0) == 0);
208 assert(pthread_mutex_init
209 (&rxkad_random_mutex, (const pthread_mutexattr_t *)0) == 0);
210 assert(pthread_mutex_init(&rx_debug_mutex, (const pthread_mutexattr_t *)0)
213 assert(pthread_cond_init
214 (&rx_event_handler_cond, (const pthread_condattr_t *)0) == 0);
215 assert(pthread_cond_init(&rx_listener_cond, (const pthread_condattr_t *)0)
217 assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
218 assert(pthread_key_create(&rx_ts_info_key, NULL) == 0);
220 rxkad_global_stats_init();
223 pthread_once_t rx_once_init = PTHREAD_ONCE_INIT;
224 #define INIT_PTHREAD_LOCKS \
225 assert(pthread_once(&rx_once_init, rxi_InitPthread)==0)
227 * The rx_stats_mutex mutex protects the following global variables:
232 * rxi_lowConnRefCount
233 * rxi_lowPeerRefCount
242 #define INIT_PTHREAD_LOCKS
246 /* Variables for handling the minProcs implementation. availProcs gives the
247 * number of threads available in the pool at this moment (not counting dudes
248 * executing right now). totalMin gives the total number of procs required
249 * for handling all minProcs requests. minDeficit is a dynamic variable
250 * tracking the # of procs required to satisfy all of the remaining minProcs
252 * For fine grain locking to work, the quota check and the reservation of
253 * a server thread has to come while rxi_availProcs and rxi_minDeficit
254 * are locked. To this end, the code has been modified under #ifdef
255 * RX_ENABLE_LOCKS so that quota checks and reservation occur at the
256 * same time. A new function, ReturnToServerPool() returns the allocation.
258 * A call can be on several queue's (but only one at a time). When
259 * rxi_ResetCall wants to remove the call from a queue, it has to ensure
260 * that no one else is touching the queue. To this end, we store the address
261 * of the queue lock in the call structure (under the call lock) when we
262 * put the call on a queue, and we clear the call_queue_lock when the
263 * call is removed from a queue (once the call lock has been obtained).
264 * This allows rxi_ResetCall to safely synchronize with others wishing
265 * to manipulate the queue.
268 #ifdef RX_ENABLE_LOCKS
269 static afs_kmutex_t rx_rpc_stats;
270 void rxi_StartUnlocked();
273 /* We keep a "last conn pointer" in rxi_FindConnection. The odds are
274 ** pretty good that the next packet coming in is from the same connection
275 ** as the last packet, since we're send multiple packets in a transmit window.
277 struct rx_connection *rxLastConn = 0;
279 #ifdef RX_ENABLE_LOCKS
280 /* The locking hierarchy for rx fine grain locking is composed of these
283 * rx_connHashTable_lock - synchronizes conn creation, rx_connHashTable access
284 * conn_call_lock - used to synchonize rx_EndCall and rx_NewCall
285 * call->lock - locks call data fields.
286 * These are independent of each other:
287 * rx_freeCallQueue_lock
292 * serverQueueEntry->lock
294 * rx_peerHashTable_lock - locked under rx_connHashTable_lock
295 * peer->lock - locks peer data fields.
296 * conn_data_lock - that more than one thread is not updating a conn data
297 * field at the same time.
305 * Do we need a lock to protect the peer field in the conn structure?
306 * conn->peer was previously a constant for all intents and so has no
307 * lock protecting this field. The multihomed client delta introduced
308 * a RX code change : change the peer field in the connection structure
309 * to that remote inetrface from which the last packet for this
310 * connection was sent out. This may become an issue if further changes
313 #define SET_CALL_QUEUE_LOCK(C, L) (C)->call_queue_lock = (L)
314 #define CLEAR_CALL_QUEUE_LOCK(C) (C)->call_queue_lock = NULL
316 /* rxdb_fileID is used to identify the lock location, along with line#. */
317 static int rxdb_fileID = RXDB_FILE_RX;
318 #endif /* RX_LOCKS_DB */
319 #else /* RX_ENABLE_LOCKS */
320 #define SET_CALL_QUEUE_LOCK(C, L)
321 #define CLEAR_CALL_QUEUE_LOCK(C)
322 #endif /* RX_ENABLE_LOCKS */
323 struct rx_serverQueueEntry *rx_waitForPacket = 0;
324 struct rx_serverQueueEntry *rx_waitingForPacket = 0;
326 /* ------------Exported Interfaces------------- */
328 /* This function allows rxkad to set the epoch to a suitably random number
329 * which rx_NewConnection will use in the future. The principle purpose is to
330 * get rxnull connections to use the same epoch as the rxkad connections do, at
331 * least once the first rxkad connection is established. This is important now
332 * that the host/port addresses aren't used in FindConnection: the uniqueness
333 * of epoch/cid matters and the start time won't do. */
335 #ifdef AFS_PTHREAD_ENV
337 * This mutex protects the following global variables:
341 #define LOCK_EPOCH assert(pthread_mutex_lock(&epoch_mutex)==0)
342 #define UNLOCK_EPOCH assert(pthread_mutex_unlock(&epoch_mutex)==0)
346 #endif /* AFS_PTHREAD_ENV */
349 rx_SetEpoch(afs_uint32 epoch)
356 /* Initialize rx. A port number may be mentioned, in which case this
357 * becomes the default port number for any service installed later.
358 * If 0 is provided for the port number, a random port will be chosen
359 * by the kernel. Whether this will ever overlap anything in
360 * /etc/services is anybody's guess... Returns 0 on success, -1 on
362 static int rxinit_status = 1;
363 #ifdef AFS_PTHREAD_ENV
365 * This mutex protects the following global variables:
369 #define LOCK_RX_INIT assert(pthread_mutex_lock(&rx_init_mutex)==0)
370 #define UNLOCK_RX_INIT assert(pthread_mutex_unlock(&rx_init_mutex)==0)
373 #define UNLOCK_RX_INIT
377 rx_InitHost(u_int host, u_int port)
384 char *htable, *ptable;
391 if (rxinit_status == 0) {
392 tmp_status = rxinit_status;
394 return tmp_status; /* Already started; return previous error code. */
400 if (afs_winsockInit() < 0)
406 * Initialize anything necessary to provide a non-premptive threading
409 rxi_InitializeThreadSupport();
412 /* Allocate and initialize a socket for client and perhaps server
415 rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
416 if (rx_socket == OSI_NULLSOCKET) {
420 #ifdef RX_ENABLE_LOCKS
423 #endif /* RX_LOCKS_DB */
424 MUTEX_INIT(&rx_stats_mutex, "rx_stats_mutex", MUTEX_DEFAULT, 0);
425 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
426 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
427 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
428 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
430 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
432 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
434 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
436 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
438 MUTEX_INIT(&rxi_keyCreate_lock, "rxi_keyCreate_lock", MUTEX_DEFAULT, 0);
440 #if defined(KERNEL) && defined(AFS_HPUX110_ENV)
442 rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
443 #endif /* KERNEL && AFS_HPUX110_ENV */
444 #endif /* RX_ENABLE_LOCKS */
447 rx_connDeadTime = 12;
448 rx_tranquil = 0; /* reset flag */
449 memset((char *)&rx_stats, 0, sizeof(struct rx_stats));
451 osi_Alloc(rx_hashTableSize * sizeof(struct rx_connection *));
452 PIN(htable, rx_hashTableSize * sizeof(struct rx_connection *)); /* XXXXX */
453 memset(htable, 0, rx_hashTableSize * sizeof(struct rx_connection *));
454 ptable = (char *)osi_Alloc(rx_hashTableSize * sizeof(struct rx_peer *));
455 PIN(ptable, rx_hashTableSize * sizeof(struct rx_peer *)); /* XXXXX */
456 memset(ptable, 0, rx_hashTableSize * sizeof(struct rx_peer *));
458 /* Malloc up a bunch of packets & buffers */
460 queue_Init(&rx_freePacketQueue);
461 rxi_NeedMorePackets = FALSE;
462 #ifdef RX_ENABLE_TSFPQ
463 rx_nPackets = 0; /* in TSFPQ version, rx_nPackets is managed by rxi_MorePackets* */
464 rxi_MorePacketsTSFPQ(rx_extraPackets + RX_MAX_QUOTA + 2, RX_TS_FPQ_FLUSH_GLOBAL, 0);
465 #else /* RX_ENABLE_TSFPQ */
466 rx_nPackets = rx_extraPackets + RX_MAX_QUOTA + 2; /* fudge */
467 rxi_MorePackets(rx_nPackets);
468 #endif /* RX_ENABLE_TSFPQ */
475 #if defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
476 tv.tv_sec = clock_now.sec;
477 tv.tv_usec = clock_now.usec;
478 srand((unsigned int)tv.tv_usec);
485 #if defined(KERNEL) && !defined(UKERNEL)
486 /* Really, this should never happen in a real kernel */
489 struct sockaddr_in addr;
490 int addrlen = sizeof(addr);
491 if (getsockname((int)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
495 rx_port = addr.sin_port;
498 rx_stats.minRtt.sec = 9999999;
500 rx_SetEpoch(tv.tv_sec | 0x80000000);
502 rx_SetEpoch(tv.tv_sec); /* Start time of this package, rxkad
503 * will provide a randomer value. */
505 MUTEX_ENTER(&rx_stats_mutex);
506 rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
507 MUTEX_EXIT(&rx_stats_mutex);
508 /* *Slightly* random start time for the cid. This is just to help
509 * out with the hashing function at the peer */
510 rx_nextCid = ((tv.tv_sec ^ tv.tv_usec) << RX_CIDSHIFT);
511 rx_connHashTable = (struct rx_connection **)htable;
512 rx_peerHashTable = (struct rx_peer **)ptable;
514 rx_lastAckDelay.sec = 0;
515 rx_lastAckDelay.usec = 400000; /* 400 milliseconds */
516 rx_hardAckDelay.sec = 0;
517 rx_hardAckDelay.usec = 100000; /* 100 milliseconds */
518 rx_softAckDelay.sec = 0;
519 rx_softAckDelay.usec = 100000; /* 100 milliseconds */
521 rxevent_Init(20, rxi_ReScheduleEvents);
523 /* Initialize various global queues */
524 queue_Init(&rx_idleServerQueue);
525 queue_Init(&rx_incomingCallQueue);
526 queue_Init(&rx_freeCallQueue);
528 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
529 /* Initialize our list of usable IP addresses. */
533 /* Start listener process (exact function is dependent on the
534 * implementation environment--kernel or user space) */
538 tmp_status = rxinit_status = 0;
546 return rx_InitHost(htonl(INADDR_ANY), port);
549 /* called with unincremented nRequestsRunning to see if it is OK to start
550 * a new thread in this service. Could be "no" for two reasons: over the
551 * max quota, or would prevent others from reaching their min quota.
553 #ifdef RX_ENABLE_LOCKS
554 /* This verion of QuotaOK reserves quota if it's ok while the
555 * rx_serverPool_lock is held. Return quota using ReturnToServerPool().
558 QuotaOK(register struct rx_service *aservice)
560 /* check if over max quota */
561 if (aservice->nRequestsRunning >= aservice->maxProcs) {
565 /* under min quota, we're OK */
566 /* otherwise, can use only if there are enough to allow everyone
567 * to go to their min quota after this guy starts.
569 MUTEX_ENTER(&rx_stats_mutex);
570 if ((aservice->nRequestsRunning < aservice->minProcs)
571 || (rxi_availProcs > rxi_minDeficit)) {
572 aservice->nRequestsRunning++;
573 /* just started call in minProcs pool, need fewer to maintain
575 if (aservice->nRequestsRunning <= aservice->minProcs)
578 MUTEX_EXIT(&rx_stats_mutex);
581 MUTEX_EXIT(&rx_stats_mutex);
587 ReturnToServerPool(register struct rx_service *aservice)
589 aservice->nRequestsRunning--;
590 MUTEX_ENTER(&rx_stats_mutex);
591 if (aservice->nRequestsRunning < aservice->minProcs)
594 MUTEX_EXIT(&rx_stats_mutex);
597 #else /* RX_ENABLE_LOCKS */
599 QuotaOK(register struct rx_service *aservice)
602 /* under min quota, we're OK */
603 if (aservice->nRequestsRunning < aservice->minProcs)
606 /* check if over max quota */
607 if (aservice->nRequestsRunning >= aservice->maxProcs)
610 /* otherwise, can use only if there are enough to allow everyone
611 * to go to their min quota after this guy starts.
613 if (rxi_availProcs > rxi_minDeficit)
617 #endif /* RX_ENABLE_LOCKS */
620 /* Called by rx_StartServer to start up lwp's to service calls.
621 NExistingProcs gives the number of procs already existing, and which
622 therefore needn't be created. */
624 rxi_StartServerProcs(int nExistingProcs)
626 register struct rx_service *service;
631 /* For each service, reserve N processes, where N is the "minimum"
632 * number of processes that MUST be able to execute a request in parallel,
633 * at any time, for that process. Also compute the maximum difference
634 * between any service's maximum number of processes that can run
635 * (i.e. the maximum number that ever will be run, and a guarantee
636 * that this number will run if other services aren't running), and its
637 * minimum number. The result is the extra number of processes that
638 * we need in order to provide the latter guarantee */
639 for (i = 0; i < RX_MAX_SERVICES; i++) {
641 service = rx_services[i];
642 if (service == (struct rx_service *)0)
644 nProcs += service->minProcs;
645 diff = service->maxProcs - service->minProcs;
649 nProcs += maxdiff; /* Extra processes needed to allow max number requested to run in any given service, under good conditions */
650 nProcs -= nExistingProcs; /* Subtract the number of procs that were previously created for use as server procs */
651 for (i = 0; i < nProcs; i++) {
652 rxi_StartServerProc(rx_ServerProc, rx_stackSize);
658 /* This routine is only required on Windows */
660 rx_StartClientThread(void)
662 #ifdef AFS_PTHREAD_ENV
664 pid = (int) pthread_self();
665 #endif /* AFS_PTHREAD_ENV */
667 #endif /* AFS_NT40_ENV */
669 /* This routine must be called if any services are exported. If the
670 * donateMe flag is set, the calling process is donated to the server
673 rx_StartServer(int donateMe)
675 register struct rx_service *service;
681 /* Start server processes, if necessary (exact function is dependent
682 * on the implementation environment--kernel or user space). DonateMe
683 * will be 1 if there is 1 pre-existing proc, i.e. this one. In this
684 * case, one less new proc will be created rx_StartServerProcs.
686 rxi_StartServerProcs(donateMe);
688 /* count up the # of threads in minProcs, and add set the min deficit to
689 * be that value, too.
691 for (i = 0; i < RX_MAX_SERVICES; i++) {
692 service = rx_services[i];
693 if (service == (struct rx_service *)0)
695 MUTEX_ENTER(&rx_stats_mutex);
696 rxi_totalMin += service->minProcs;
697 /* below works even if a thread is running, since minDeficit would
698 * still have been decremented and later re-incremented.
700 rxi_minDeficit += service->minProcs;
701 MUTEX_EXIT(&rx_stats_mutex);
704 /* Turn on reaping of idle server connections */
705 rxi_ReapConnections();
714 #ifdef AFS_PTHREAD_ENV
716 pid = (pid_t) pthread_self();
717 #else /* AFS_PTHREAD_ENV */
719 LWP_CurrentProcess(&pid);
720 #endif /* AFS_PTHREAD_ENV */
722 sprintf(name, "srv_%d", ++nProcs);
724 (*registerProgram) (pid, name);
726 #endif /* AFS_NT40_ENV */
727 rx_ServerProc(); /* Never returns */
729 #ifdef RX_ENABLE_TSFPQ
730 /* no use leaving packets around in this thread's local queue if
731 * it isn't getting donated to the server thread pool.
733 rxi_FlushLocalPacketsTSFPQ();
734 #endif /* RX_ENABLE_TSFPQ */
738 /* Create a new client connection to the specified service, using the
739 * specified security object to implement the security model for this
741 struct rx_connection *
742 rx_NewConnection(register afs_uint32 shost, u_short sport, u_short sservice,
743 register struct rx_securityClass *securityObject,
744 int serviceSecurityIndex)
748 register struct rx_connection *conn;
753 dpf(("rx_NewConnection(host %x, port %u, service %u, securityObject %x, serviceSecurityIndex %d)\n", ntohl(shost), ntohs(sport), sservice, securityObject, serviceSecurityIndex));
755 /* Vasilsi said: "NETPRI protects Cid and Alloc", but can this be true in
756 * the case of kmem_alloc? */
757 conn = rxi_AllocConnection();
758 #ifdef RX_ENABLE_LOCKS
759 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
760 MUTEX_INIT(&conn->conn_data_lock, "conn call lock", MUTEX_DEFAULT, 0);
761 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
764 MUTEX_ENTER(&rx_connHashTable_lock);
765 cid = (rx_nextCid += RX_MAXCALLS);
766 conn->type = RX_CLIENT_CONNECTION;
768 conn->epoch = rx_epoch;
769 conn->peer = rxi_FindPeer(shost, sport, 0, 1);
770 conn->serviceId = sservice;
771 conn->securityObject = securityObject;
772 /* This doesn't work in all compilers with void (they're buggy), so fake it
774 conn->securityData = (VOID *) 0;
775 conn->securityIndex = serviceSecurityIndex;
776 rx_SetConnDeadTime(conn, rx_connDeadTime);
777 conn->ackRate = RX_FAST_ACK_RATE;
779 conn->specific = NULL;
780 conn->challengeEvent = NULL;
781 conn->delayedAbortEvent = NULL;
782 conn->abortCount = 0;
785 RXS_NewConnection(securityObject, conn);
787 CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
789 conn->refCount++; /* no lock required since only this thread knows... */
790 conn->next = rx_connHashTable[hashindex];
791 rx_connHashTable[hashindex] = conn;
792 MUTEX_ENTER(&rx_stats_mutex);
793 rx_stats.nClientConns++;
794 MUTEX_EXIT(&rx_stats_mutex);
796 MUTEX_EXIT(&rx_connHashTable_lock);
802 rx_SetConnDeadTime(register struct rx_connection *conn, register int seconds)
804 /* The idea is to set the dead time to a value that allows several
805 * keepalives to be dropped without timing out the connection. */
806 conn->secondsUntilDead = MAX(seconds, 6);
807 conn->secondsUntilPing = conn->secondsUntilDead / 6;
810 int rxi_lowPeerRefCount = 0;
811 int rxi_lowConnRefCount = 0;
814 * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
815 * NOTE: must not be called with rx_connHashTable_lock held.
818 rxi_CleanupConnection(struct rx_connection *conn)
820 /* Notify the service exporter, if requested, that this connection
821 * is being destroyed */
822 if (conn->type == RX_SERVER_CONNECTION && conn->service->destroyConnProc)
823 (*conn->service->destroyConnProc) (conn);
825 /* Notify the security module that this connection is being destroyed */
826 RXS_DestroyConnection(conn->securityObject, conn);
828 /* If this is the last connection using the rx_peer struct, set its
829 * idle time to now. rxi_ReapConnections will reap it if it's still
830 * idle (refCount == 0) after rx_idlePeerTime (60 seconds) have passed.
832 MUTEX_ENTER(&rx_peerHashTable_lock);
833 if (conn->peer->refCount < 2) {
834 conn->peer->idleWhen = clock_Sec();
835 if (conn->peer->refCount < 1) {
836 conn->peer->refCount = 1;
837 MUTEX_ENTER(&rx_stats_mutex);
838 rxi_lowPeerRefCount++;
839 MUTEX_EXIT(&rx_stats_mutex);
842 conn->peer->refCount--;
843 MUTEX_EXIT(&rx_peerHashTable_lock);
845 MUTEX_ENTER(&rx_stats_mutex);
846 if (conn->type == RX_SERVER_CONNECTION)
847 rx_stats.nServerConns--;
849 rx_stats.nClientConns--;
850 MUTEX_EXIT(&rx_stats_mutex);
853 if (conn->specific) {
855 for (i = 0; i < conn->nSpecific; i++) {
856 if (conn->specific[i] && rxi_keyCreate_destructor[i])
857 (*rxi_keyCreate_destructor[i]) (conn->specific[i]);
858 conn->specific[i] = NULL;
860 free(conn->specific);
862 conn->specific = NULL;
866 MUTEX_DESTROY(&conn->conn_call_lock);
867 MUTEX_DESTROY(&conn->conn_data_lock);
868 CV_DESTROY(&conn->conn_call_cv);
870 rxi_FreeConnection(conn);
873 /* Destroy the specified connection */
875 rxi_DestroyConnection(register struct rx_connection *conn)
877 MUTEX_ENTER(&rx_connHashTable_lock);
878 rxi_DestroyConnectionNoLock(conn);
879 /* conn should be at the head of the cleanup list */
880 if (conn == rx_connCleanup_list) {
881 rx_connCleanup_list = rx_connCleanup_list->next;
882 MUTEX_EXIT(&rx_connHashTable_lock);
883 rxi_CleanupConnection(conn);
885 #ifdef RX_ENABLE_LOCKS
887 MUTEX_EXIT(&rx_connHashTable_lock);
889 #endif /* RX_ENABLE_LOCKS */
893 rxi_DestroyConnectionNoLock(register struct rx_connection *conn)
895 register struct rx_connection **conn_ptr;
896 register int havecalls = 0;
897 struct rx_packet *packet;
904 MUTEX_ENTER(&conn->conn_data_lock);
905 if (conn->refCount > 0)
908 MUTEX_ENTER(&rx_stats_mutex);
909 rxi_lowConnRefCount++;
910 MUTEX_EXIT(&rx_stats_mutex);
913 if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
914 /* Busy; wait till the last guy before proceeding */
915 MUTEX_EXIT(&conn->conn_data_lock);
920 /* If the client previously called rx_NewCall, but it is still
921 * waiting, treat this as a running call, and wait to destroy the
922 * connection later when the call completes. */
923 if ((conn->type == RX_CLIENT_CONNECTION)
924 && (conn->flags & RX_CONN_MAKECALL_WAITING)) {
925 conn->flags |= RX_CONN_DESTROY_ME;
926 MUTEX_EXIT(&conn->conn_data_lock);
930 MUTEX_EXIT(&conn->conn_data_lock);
932 /* Check for extant references to this connection */
933 for (i = 0; i < RX_MAXCALLS; i++) {
934 register struct rx_call *call = conn->call[i];
937 if (conn->type == RX_CLIENT_CONNECTION) {
938 MUTEX_ENTER(&call->lock);
939 if (call->delayedAckEvent) {
940 /* Push the final acknowledgment out now--there
941 * won't be a subsequent call to acknowledge the
942 * last reply packets */
943 rxevent_Cancel(call->delayedAckEvent, call,
944 RX_CALL_REFCOUNT_DELAY);
945 if (call->state == RX_STATE_PRECALL
946 || call->state == RX_STATE_ACTIVE) {
947 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
949 rxi_AckAll(NULL, call, 0);
952 MUTEX_EXIT(&call->lock);
956 #ifdef RX_ENABLE_LOCKS
958 if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
959 MUTEX_EXIT(&conn->conn_data_lock);
961 /* Someone is accessing a packet right now. */
965 #endif /* RX_ENABLE_LOCKS */
968 /* Don't destroy the connection if there are any call
969 * structures still in use */
970 MUTEX_ENTER(&conn->conn_data_lock);
971 conn->flags |= RX_CONN_DESTROY_ME;
972 MUTEX_EXIT(&conn->conn_data_lock);
977 if (conn->delayedAbortEvent) {
978 rxevent_Cancel(conn->delayedAbortEvent, (struct rx_call *)0, 0);
979 packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
981 MUTEX_ENTER(&conn->conn_data_lock);
982 rxi_SendConnectionAbort(conn, packet, 0, 1);
983 MUTEX_EXIT(&conn->conn_data_lock);
984 rxi_FreePacket(packet);
988 /* Remove from connection hash table before proceeding */
990 &rx_connHashTable[CONN_HASH
991 (peer->host, peer->port, conn->cid, conn->epoch,
993 for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
994 if (*conn_ptr == conn) {
995 *conn_ptr = conn->next;
999 /* if the conn that we are destroying was the last connection, then we
1000 * clear rxLastConn as well */
1001 if (rxLastConn == conn)
1004 /* Make sure the connection is completely reset before deleting it. */
1005 /* get rid of pending events that could zap us later */
1006 if (conn->challengeEvent)
1007 rxevent_Cancel(conn->challengeEvent, (struct rx_call *)0, 0);
1008 if (conn->checkReachEvent)
1009 rxevent_Cancel(conn->checkReachEvent, (struct rx_call *)0, 0);
1011 /* Add the connection to the list of destroyed connections that
1012 * need to be cleaned up. This is necessary to avoid deadlocks
1013 * in the routines we call to inform others that this connection is
1014 * being destroyed. */
1015 conn->next = rx_connCleanup_list;
1016 rx_connCleanup_list = conn;
1019 /* Externally available version */
1021 rx_DestroyConnection(register struct rx_connection *conn)
1026 rxi_DestroyConnection(conn);
1031 rx_GetConnection(register struct rx_connection *conn)
1036 MUTEX_ENTER(&conn->conn_data_lock);
1038 MUTEX_EXIT(&conn->conn_data_lock);
1042 /* Start a new rx remote procedure call, on the specified connection.
1043 * If wait is set to 1, wait for a free call channel; otherwise return
1044 * 0. Maxtime gives the maximum number of seconds this call may take,
1045 * after rx_NewCall returns. After this time interval, a call to any
1046 * of rx_SendData, rx_ReadData, etc. will fail with RX_CALL_TIMEOUT.
1047 * For fine grain locking, we hold the conn_call_lock in order to
1048 * to ensure that we don't get signalle after we found a call in an active
1049 * state and before we go to sleep.
1052 rx_NewCall(register struct rx_connection *conn)
1055 register struct rx_call *call;
1056 struct clock queueTime;
1060 dpf(("rx_NewCall(conn %x)\n", conn));
1063 clock_GetTime(&queueTime);
1064 MUTEX_ENTER(&conn->conn_call_lock);
1067 * Check if there are others waiting for a new call.
1068 * If so, let them go first to avoid starving them.
1069 * This is a fairly simple scheme, and might not be
1070 * a complete solution for large numbers of waiters.
1072 * makeCallWaiters keeps track of the number of
1073 * threads waiting to make calls and the
1074 * RX_CONN_MAKECALL_WAITING flag bit is used to
1075 * indicate that there are indeed calls waiting.
1076 * The flag is set when the waiter is incremented.
1077 * It is only cleared in rx_EndCall when
1078 * makeCallWaiters is 0. This prevents us from
1079 * accidently destroying the connection while it
1080 * is potentially about to be used.
1082 MUTEX_ENTER(&conn->conn_data_lock);
1083 if (conn->makeCallWaiters) {
1084 conn->flags |= RX_CONN_MAKECALL_WAITING;
1085 conn->makeCallWaiters++;
1086 MUTEX_EXIT(&conn->conn_data_lock);
1088 #ifdef RX_ENABLE_LOCKS
1089 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1093 MUTEX_ENTER(&conn->conn_data_lock);
1094 conn->makeCallWaiters--;
1096 MUTEX_EXIT(&conn->conn_data_lock);
1099 for (i = 0; i < RX_MAXCALLS; i++) {
1100 call = conn->call[i];
1102 MUTEX_ENTER(&call->lock);
1103 if (call->state == RX_STATE_DALLY) {
1104 rxi_ResetCall(call, 0);
1105 (*call->callNumber)++;
1108 MUTEX_EXIT(&call->lock);
1110 call = rxi_NewCall(conn, i);
1114 if (i < RX_MAXCALLS) {
1117 MUTEX_ENTER(&conn->conn_data_lock);
1118 conn->flags |= RX_CONN_MAKECALL_WAITING;
1119 conn->makeCallWaiters++;
1120 MUTEX_EXIT(&conn->conn_data_lock);
1122 #ifdef RX_ENABLE_LOCKS
1123 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1127 MUTEX_ENTER(&conn->conn_data_lock);
1128 conn->makeCallWaiters--;
1129 MUTEX_EXIT(&conn->conn_data_lock);
1132 * Wake up anyone else who might be giving us a chance to
1133 * run (see code above that avoids resource starvation).
1135 #ifdef RX_ENABLE_LOCKS
1136 CV_BROADCAST(&conn->conn_call_cv);
1141 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1143 /* Client is initially in send mode */
1144 call->state = RX_STATE_ACTIVE;
1145 call->error = conn->error;
1147 call->mode = RX_MODE_ERROR;
1149 call->mode = RX_MODE_SENDING;
1151 /* remember start time for call in case we have hard dead time limit */
1152 call->queueTime = queueTime;
1153 clock_GetTime(&call->startTime);
1154 hzero(call->bytesSent);
1155 hzero(call->bytesRcvd);
1157 /* Turn on busy protocol. */
1158 rxi_KeepAliveOn(call);
1160 MUTEX_EXIT(&call->lock);
1161 MUTEX_EXIT(&conn->conn_call_lock);
1164 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1165 /* Now, if TQ wasn't cleared earlier, do it now. */
1166 MUTEX_ENTER(&call->lock);
1167 while (call->flags & RX_CALL_TQ_BUSY) {
1168 call->flags |= RX_CALL_TQ_WAIT;
1170 #ifdef RX_ENABLE_LOCKS
1171 osirx_AssertMine(&call->lock, "rxi_Start lock4");
1172 CV_WAIT(&call->cv_tq, &call->lock);
1173 #else /* RX_ENABLE_LOCKS */
1174 osi_rxSleep(&call->tq);
1175 #endif /* RX_ENABLE_LOCKS */
1177 if (call->tqWaiters == 0) {
1178 call->flags &= ~RX_CALL_TQ_WAIT;
1181 if (call->flags & RX_CALL_TQ_CLEARME) {
1182 rxi_ClearTransmitQueue(call, 0);
1183 queue_Init(&call->tq);
1185 MUTEX_EXIT(&call->lock);
1186 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1188 dpf(("rx_NewCall(call %x)\n", call));
1193 rxi_HasActiveCalls(register struct rx_connection *aconn)
1196 register struct rx_call *tcall;
1200 for (i = 0; i < RX_MAXCALLS; i++) {
1201 if ((tcall = aconn->call[i])) {
1202 if ((tcall->state == RX_STATE_ACTIVE)
1203 || (tcall->state == RX_STATE_PRECALL)) {
1214 rxi_GetCallNumberVector(register struct rx_connection *aconn,
1215 register afs_int32 * aint32s)
1218 register struct rx_call *tcall;
1222 for (i = 0; i < RX_MAXCALLS; i++) {
1223 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1224 aint32s[i] = aconn->callNumber[i] + 1;
1226 aint32s[i] = aconn->callNumber[i];
1233 rxi_SetCallNumberVector(register struct rx_connection *aconn,
1234 register afs_int32 * aint32s)
1237 register struct rx_call *tcall;
1241 for (i = 0; i < RX_MAXCALLS; i++) {
1242 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1243 aconn->callNumber[i] = aint32s[i] - 1;
1245 aconn->callNumber[i] = aint32s[i];
1251 /* Advertise a new service. A service is named locally by a UDP port
1252 * number plus a 16-bit service id. Returns (struct rx_service *) 0
1255 char *serviceName; Name for identification purposes (e.g. the
1256 service name might be used for probing for
1259 rx_NewServiceHost(afs_uint32 host, u_short port, u_short serviceId,
1260 char *serviceName, struct rx_securityClass **securityObjects,
1261 int nSecurityObjects,
1262 afs_int32(*serviceProc) (struct rx_call * acall))
1264 osi_socket socket = OSI_NULLSOCKET;
1265 register struct rx_service *tservice;
1271 if (serviceId == 0) {
1273 "rx_NewService: service id for service %s is not non-zero.\n",
1280 "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",
1288 tservice = rxi_AllocService();
1290 for (i = 0; i < RX_MAX_SERVICES; i++) {
1291 register struct rx_service *service = rx_services[i];
1293 if (port == service->servicePort && host == service->serviceHost) {
1294 if (service->serviceId == serviceId) {
1295 /* The identical service has already been
1296 * installed; if the caller was intending to
1297 * change the security classes used by this
1298 * service, he/she loses. */
1300 "rx_NewService: tried to install service %s with service id %d, which is already in use for service %s\n",
1301 serviceName, serviceId, service->serviceName);
1303 rxi_FreeService(tservice);
1306 /* Different service, same port: re-use the socket
1307 * which is bound to the same port */
1308 socket = service->socket;
1311 if (socket == OSI_NULLSOCKET) {
1312 /* If we don't already have a socket (from another
1313 * service on same port) get a new one */
1314 socket = rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
1315 if (socket == OSI_NULLSOCKET) {
1317 rxi_FreeService(tservice);
1322 service->socket = socket;
1323 service->serviceHost = host;
1324 service->servicePort = port;
1325 service->serviceId = serviceId;
1326 service->serviceName = serviceName;
1327 service->nSecurityObjects = nSecurityObjects;
1328 service->securityObjects = securityObjects;
1329 service->minProcs = 0;
1330 service->maxProcs = 1;
1331 service->idleDeadTime = 60;
1332 service->connDeadTime = rx_connDeadTime;
1333 service->executeRequestProc = serviceProc;
1334 service->checkReach = 0;
1335 rx_services[i] = service; /* not visible until now */
1341 rxi_FreeService(tservice);
1342 (osi_Msg "rx_NewService: cannot support > %d services\n",
1348 rx_NewService(u_short port, u_short serviceId, char *serviceName,
1349 struct rx_securityClass **securityObjects, int nSecurityObjects,
1350 afs_int32(*serviceProc) (struct rx_call * acall))
1352 return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
1355 /* Generic request processing loop. This routine should be called
1356 * by the implementation dependent rx_ServerProc. If socketp is
1357 * non-null, it will be set to the file descriptor that this thread
1358 * is now listening on. If socketp is null, this routine will never
1361 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1363 register struct rx_call *call;
1364 register afs_int32 code;
1365 register struct rx_service *tservice = NULL;
1372 call = rx_GetCall(threadID, tservice, socketp);
1373 if (socketp && *socketp != OSI_NULLSOCKET) {
1374 /* We are now a listener thread */
1379 /* if server is restarting( typically smooth shutdown) then do not
1380 * allow any new calls.
1383 if (rx_tranquil && (call != NULL)) {
1387 MUTEX_ENTER(&call->lock);
1389 rxi_CallError(call, RX_RESTARTING);
1390 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1392 MUTEX_EXIT(&call->lock);
1396 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1397 #ifdef RX_ENABLE_LOCKS
1399 #endif /* RX_ENABLE_LOCKS */
1400 afs_termState = AFSOP_STOP_AFS;
1401 afs_osi_Wakeup(&afs_termState);
1402 #ifdef RX_ENABLE_LOCKS
1404 #endif /* RX_ENABLE_LOCKS */
1409 tservice = call->conn->service;
1411 if (tservice->beforeProc)
1412 (*tservice->beforeProc) (call);
1414 code = call->conn->service->executeRequestProc(call);
1416 if (tservice->afterProc)
1417 (*tservice->afterProc) (call, code);
1419 rx_EndCall(call, code);
1420 MUTEX_ENTER(&rx_stats_mutex);
1422 MUTEX_EXIT(&rx_stats_mutex);
1428 rx_WakeupServerProcs(void)
1430 struct rx_serverQueueEntry *np, *tqp;
1434 MUTEX_ENTER(&rx_serverPool_lock);
1436 #ifdef RX_ENABLE_LOCKS
1437 if (rx_waitForPacket)
1438 CV_BROADCAST(&rx_waitForPacket->cv);
1439 #else /* RX_ENABLE_LOCKS */
1440 if (rx_waitForPacket)
1441 osi_rxWakeup(rx_waitForPacket);
1442 #endif /* RX_ENABLE_LOCKS */
1443 MUTEX_ENTER(&freeSQEList_lock);
1444 for (np = rx_FreeSQEList; np; np = tqp) {
1445 tqp = *(struct rx_serverQueueEntry **)np;
1446 #ifdef RX_ENABLE_LOCKS
1447 CV_BROADCAST(&np->cv);
1448 #else /* RX_ENABLE_LOCKS */
1450 #endif /* RX_ENABLE_LOCKS */
1452 MUTEX_EXIT(&freeSQEList_lock);
1453 for (queue_Scan(&rx_idleServerQueue, np, tqp, rx_serverQueueEntry)) {
1454 #ifdef RX_ENABLE_LOCKS
1455 CV_BROADCAST(&np->cv);
1456 #else /* RX_ENABLE_LOCKS */
1458 #endif /* RX_ENABLE_LOCKS */
1460 MUTEX_EXIT(&rx_serverPool_lock);
1465 * One thing that seems to happen is that all the server threads get
1466 * tied up on some empty or slow call, and then a whole bunch of calls
1467 * arrive at once, using up the packet pool, so now there are more
1468 * empty calls. The most critical resources here are server threads
1469 * and the free packet pool. The "doreclaim" code seems to help in
1470 * general. I think that eventually we arrive in this state: there
1471 * are lots of pending calls which do have all their packets present,
1472 * so they won't be reclaimed, are multi-packet calls, so they won't
1473 * be scheduled until later, and thus are tying up most of the free
1474 * packet pool for a very long time.
1476 * 1. schedule multi-packet calls if all the packets are present.
1477 * Probably CPU-bound operation, useful to return packets to pool.
1478 * Do what if there is a full window, but the last packet isn't here?
1479 * 3. preserve one thread which *only* runs "best" calls, otherwise
1480 * it sleeps and waits for that type of call.
1481 * 4. Don't necessarily reserve a whole window for each thread. In fact,
1482 * the current dataquota business is badly broken. The quota isn't adjusted
1483 * to reflect how many packets are presently queued for a running call.
1484 * So, when we schedule a queued call with a full window of packets queued
1485 * up for it, that *should* free up a window full of packets for other 2d-class
1486 * calls to be able to use from the packet pool. But it doesn't.
1488 * NB. Most of the time, this code doesn't run -- since idle server threads
1489 * sit on the idle server queue and are assigned by "...ReceivePacket" as soon
1490 * as a new call arrives.
1492 /* Sleep until a call arrives. Returns a pointer to the call, ready
1493 * for an rx_Read. */
1494 #ifdef RX_ENABLE_LOCKS
1496 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1498 struct rx_serverQueueEntry *sq;
1499 register struct rx_call *call = (struct rx_call *)0;
1500 struct rx_service *service = NULL;
1503 MUTEX_ENTER(&freeSQEList_lock);
1505 if ((sq = rx_FreeSQEList)) {
1506 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1507 MUTEX_EXIT(&freeSQEList_lock);
1508 } else { /* otherwise allocate a new one and return that */
1509 MUTEX_EXIT(&freeSQEList_lock);
1510 sq = (struct rx_serverQueueEntry *)
1511 rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1512 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1513 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1516 MUTEX_ENTER(&rx_serverPool_lock);
1517 if (cur_service != NULL) {
1518 ReturnToServerPool(cur_service);
1521 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1522 register struct rx_call *tcall, *ncall, *choice2 = NULL;
1524 /* Scan for eligible incoming calls. A call is not eligible
1525 * if the maximum number of calls for its service type are
1526 * already executing */
1527 /* One thread will process calls FCFS (to prevent starvation),
1528 * while the other threads may run ahead looking for calls which
1529 * have all their input data available immediately. This helps
1530 * keep threads from blocking, waiting for data from the client. */
1531 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1532 service = tcall->conn->service;
1533 if (!QuotaOK(service)) {
1536 if (tno == rxi_fcfs_thread_num
1537 || !tcall->queue_item_header.next) {
1538 /* If we're the fcfs thread , then we'll just use
1539 * this call. If we haven't been able to find an optimal
1540 * choice, and we're at the end of the list, then use a
1541 * 2d choice if one has been identified. Otherwise... */
1542 call = (choice2 ? choice2 : tcall);
1543 service = call->conn->service;
1544 } else if (!queue_IsEmpty(&tcall->rq)) {
1545 struct rx_packet *rp;
1546 rp = queue_First(&tcall->rq, rx_packet);
1547 if (rp->header.seq == 1) {
1549 || (rp->header.flags & RX_LAST_PACKET)) {
1551 } else if (rxi_2dchoice && !choice2
1552 && !(tcall->flags & RX_CALL_CLEARED)
1553 && (tcall->rprev > rxi_HardAckRate)) {
1562 ReturnToServerPool(service);
1569 MUTEX_EXIT(&rx_serverPool_lock);
1570 MUTEX_ENTER(&call->lock);
1572 if (call->flags & RX_CALL_WAIT_PROC) {
1573 call->flags &= ~RX_CALL_WAIT_PROC;
1574 MUTEX_ENTER(&rx_stats_mutex);
1576 MUTEX_EXIT(&rx_stats_mutex);
1579 if (call->state != RX_STATE_PRECALL || call->error) {
1580 MUTEX_EXIT(&call->lock);
1581 MUTEX_ENTER(&rx_serverPool_lock);
1582 ReturnToServerPool(service);
1587 if (queue_IsEmpty(&call->rq)
1588 || queue_First(&call->rq, rx_packet)->header.seq != 1)
1589 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1591 CLEAR_CALL_QUEUE_LOCK(call);
1594 /* If there are no eligible incoming calls, add this process
1595 * to the idle server queue, to wait for one */
1599 *socketp = OSI_NULLSOCKET;
1601 sq->socketp = socketp;
1602 queue_Append(&rx_idleServerQueue, sq);
1603 #ifndef AFS_AIX41_ENV
1604 rx_waitForPacket = sq;
1606 rx_waitingForPacket = sq;
1607 #endif /* AFS_AIX41_ENV */
1609 CV_WAIT(&sq->cv, &rx_serverPool_lock);
1611 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1612 MUTEX_EXIT(&rx_serverPool_lock);
1613 return (struct rx_call *)0;
1616 } while (!(call = sq->newcall)
1617 && !(socketp && *socketp != OSI_NULLSOCKET));
1618 MUTEX_EXIT(&rx_serverPool_lock);
1620 MUTEX_ENTER(&call->lock);
1626 MUTEX_ENTER(&freeSQEList_lock);
1627 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1628 rx_FreeSQEList = sq;
1629 MUTEX_EXIT(&freeSQEList_lock);
1632 clock_GetTime(&call->startTime);
1633 call->state = RX_STATE_ACTIVE;
1634 call->mode = RX_MODE_RECEIVING;
1635 #ifdef RX_KERNEL_TRACE
1636 if (ICL_SETACTIVE(afs_iclSetp)) {
1637 int glockOwner = ISAFS_GLOCK();
1640 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1641 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1648 rxi_calltrace(RX_CALL_START, call);
1649 dpf(("rx_GetCall(port=%d, service=%d) ==> call %x\n",
1650 call->conn->service->servicePort, call->conn->service->serviceId,
1653 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1654 MUTEX_EXIT(&call->lock);
1656 dpf(("rx_GetCall(socketp=0x%x, *socketp=0x%x)\n", socketp, *socketp));
1661 #else /* RX_ENABLE_LOCKS */
1663 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1665 struct rx_serverQueueEntry *sq;
1666 register struct rx_call *call = (struct rx_call *)0, *choice2;
1667 struct rx_service *service = NULL;
1671 MUTEX_ENTER(&freeSQEList_lock);
1673 if ((sq = rx_FreeSQEList)) {
1674 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1675 MUTEX_EXIT(&freeSQEList_lock);
1676 } else { /* otherwise allocate a new one and return that */
1677 MUTEX_EXIT(&freeSQEList_lock);
1678 sq = (struct rx_serverQueueEntry *)
1679 rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1680 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1681 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1683 MUTEX_ENTER(&sq->lock);
1685 if (cur_service != NULL) {
1686 cur_service->nRequestsRunning--;
1687 if (cur_service->nRequestsRunning < cur_service->minProcs)
1691 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
1692 register struct rx_call *tcall, *ncall;
1693 /* Scan for eligible incoming calls. A call is not eligible
1694 * if the maximum number of calls for its service type are
1695 * already executing */
1696 /* One thread will process calls FCFS (to prevent starvation),
1697 * while the other threads may run ahead looking for calls which
1698 * have all their input data available immediately. This helps
1699 * keep threads from blocking, waiting for data from the client. */
1700 choice2 = (struct rx_call *)0;
1701 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
1702 service = tcall->conn->service;
1703 if (QuotaOK(service)) {
1704 if (tno == rxi_fcfs_thread_num
1705 || !tcall->queue_item_header.next) {
1706 /* If we're the fcfs thread, then we'll just use
1707 * this call. If we haven't been able to find an optimal
1708 * choice, and we're at the end of the list, then use a
1709 * 2d choice if one has been identified. Otherwise... */
1710 call = (choice2 ? choice2 : tcall);
1711 service = call->conn->service;
1712 } else if (!queue_IsEmpty(&tcall->rq)) {
1713 struct rx_packet *rp;
1714 rp = queue_First(&tcall->rq, rx_packet);
1715 if (rp->header.seq == 1
1717 || (rp->header.flags & RX_LAST_PACKET))) {
1719 } else if (rxi_2dchoice && !choice2
1720 && !(tcall->flags & RX_CALL_CLEARED)
1721 && (tcall->rprev > rxi_HardAckRate)) {
1734 /* we can't schedule a call if there's no data!!! */
1735 /* send an ack if there's no data, if we're missing the
1736 * first packet, or we're missing something between first
1737 * and last -- there's a "hole" in the incoming data. */
1738 if (queue_IsEmpty(&call->rq)
1739 || queue_First(&call->rq, rx_packet)->header.seq != 1
1740 || call->rprev != queue_Last(&call->rq, rx_packet)->header.seq)
1741 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1743 call->flags &= (~RX_CALL_WAIT_PROC);
1744 service->nRequestsRunning++;
1745 /* just started call in minProcs pool, need fewer to maintain
1747 if (service->nRequestsRunning <= service->minProcs)
1751 /* MUTEX_EXIT(&call->lock); */
1753 /* If there are no eligible incoming calls, add this process
1754 * to the idle server queue, to wait for one */
1757 *socketp = OSI_NULLSOCKET;
1759 sq->socketp = socketp;
1760 queue_Append(&rx_idleServerQueue, sq);
1764 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1766 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
1767 return (struct rx_call *)0;
1770 } while (!(call = sq->newcall)
1771 && !(socketp && *socketp != OSI_NULLSOCKET));
1773 MUTEX_EXIT(&sq->lock);
1775 MUTEX_ENTER(&freeSQEList_lock);
1776 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
1777 rx_FreeSQEList = sq;
1778 MUTEX_EXIT(&freeSQEList_lock);
1781 clock_GetTime(&call->startTime);
1782 call->state = RX_STATE_ACTIVE;
1783 call->mode = RX_MODE_RECEIVING;
1784 #ifdef RX_KERNEL_TRACE
1785 if (ICL_SETACTIVE(afs_iclSetp)) {
1786 int glockOwner = ISAFS_GLOCK();
1789 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1790 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1797 rxi_calltrace(RX_CALL_START, call);
1798 dpf(("rx_GetCall(port=%d, service=%d) ==> call %x\n",
1799 call->conn->service->servicePort, call->conn->service->serviceId,
1802 dpf(("rx_GetCall(socketp=0x%x, *socketp=0x%x)\n", socketp, *socketp));
1809 #endif /* RX_ENABLE_LOCKS */
1813 /* Establish a procedure to be called when a packet arrives for a
1814 * call. This routine will be called at most once after each call,
1815 * and will also be called if there is an error condition on the or
1816 * the call is complete. Used by multi rx to build a selection
1817 * function which determines which of several calls is likely to be a
1818 * good one to read from.
1819 * NOTE: the way this is currently implemented it is probably only a
1820 * good idea to (1) use it immediately after a newcall (clients only)
1821 * and (2) only use it once. Other uses currently void your warranty
1824 rx_SetArrivalProc(register struct rx_call *call,
1825 register void (*proc) (register struct rx_call * call,
1827 register int index),
1828 register VOID * handle, register int arg)
1830 call->arrivalProc = proc;
1831 call->arrivalProcHandle = handle;
1832 call->arrivalProcArg = arg;
1835 /* Call is finished (possibly prematurely). Return rc to the peer, if
1836 * appropriate, and return the final error code from the conversation
1840 rx_EndCall(register struct rx_call *call, afs_int32 rc)
1842 register struct rx_connection *conn = call->conn;
1843 register struct rx_service *service;
1849 dpf(("rx_EndCall(call %x rc %d error %d abortCode %d)\n", call, rc, call->error, call->abortCode));
1852 MUTEX_ENTER(&call->lock);
1854 if (rc == 0 && call->error == 0) {
1855 call->abortCode = 0;
1856 call->abortCount = 0;
1859 call->arrivalProc = (void (*)())0;
1860 if (rc && call->error == 0) {
1861 rxi_CallError(call, rc);
1862 /* Send an abort message to the peer if this error code has
1863 * only just been set. If it was set previously, assume the
1864 * peer has already been sent the error code or will request it
1866 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1868 if (conn->type == RX_SERVER_CONNECTION) {
1869 /* Make sure reply or at least dummy reply is sent */
1870 if (call->mode == RX_MODE_RECEIVING) {
1871 rxi_WriteProc(call, 0, 0);
1873 if (call->mode == RX_MODE_SENDING) {
1874 rxi_FlushWrite(call);
1876 service = conn->service;
1877 rxi_calltrace(RX_CALL_END, call);
1878 /* Call goes to hold state until reply packets are acknowledged */
1879 if (call->tfirst + call->nSoftAcked < call->tnext) {
1880 call->state = RX_STATE_HOLD;
1882 call->state = RX_STATE_DALLY;
1883 rxi_ClearTransmitQueue(call, 0);
1884 rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
1885 rxevent_Cancel(call->keepAliveEvent, call,
1886 RX_CALL_REFCOUNT_ALIVE);
1888 } else { /* Client connection */
1890 /* Make sure server receives input packets, in the case where
1891 * no reply arguments are expected */
1892 if ((call->mode == RX_MODE_SENDING)
1893 || (call->mode == RX_MODE_RECEIVING && call->rnext == 1)) {
1894 (void)rxi_ReadProc(call, &dummy, 1);
1897 /* If we had an outstanding delayed ack, be nice to the server
1898 * and force-send it now.
1900 if (call->delayedAckEvent) {
1901 rxevent_Cancel(call->delayedAckEvent, call,
1902 RX_CALL_REFCOUNT_DELAY);
1903 call->delayedAckEvent = NULL;
1904 rxi_SendDelayedAck(NULL, call, NULL);
1907 /* We need to release the call lock since it's lower than the
1908 * conn_call_lock and we don't want to hold the conn_call_lock
1909 * over the rx_ReadProc call. The conn_call_lock needs to be held
1910 * here for the case where rx_NewCall is perusing the calls on
1911 * the connection structure. We don't want to signal until
1912 * rx_NewCall is in a stable state. Otherwise, rx_NewCall may
1913 * have checked this call, found it active and by the time it
1914 * goes to sleep, will have missed the signal.
1916 * Do not clear the RX_CONN_MAKECALL_WAITING flag as long as
1917 * there are threads waiting to use the conn object.
1919 MUTEX_EXIT(&call->lock);
1920 MUTEX_ENTER(&conn->conn_call_lock);
1921 MUTEX_ENTER(&call->lock);
1922 MUTEX_ENTER(&conn->conn_data_lock);
1923 conn->flags |= RX_CONN_BUSY;
1924 if (conn->flags & RX_CONN_MAKECALL_WAITING) {
1925 if (conn->makeCallWaiters == 0)
1926 conn->flags &= (~RX_CONN_MAKECALL_WAITING);
1927 MUTEX_EXIT(&conn->conn_data_lock);
1928 #ifdef RX_ENABLE_LOCKS
1929 CV_BROADCAST(&conn->conn_call_cv);
1934 #ifdef RX_ENABLE_LOCKS
1936 MUTEX_EXIT(&conn->conn_data_lock);
1938 #endif /* RX_ENABLE_LOCKS */
1939 call->state = RX_STATE_DALLY;
1941 error = call->error;
1943 /* currentPacket, nLeft, and NFree must be zeroed here, because
1944 * ResetCall cannot: ResetCall may be called at splnet(), in the
1945 * kernel version, and may interrupt the macros rx_Read or
1946 * rx_Write, which run at normal priority for efficiency. */
1947 if (call->currentPacket) {
1948 queue_Prepend(&call->iovq, call->currentPacket);
1949 call->currentPacket = (struct rx_packet *)0;
1952 call->nLeft = call->nFree = call->curlen = 0;
1954 /* Free any packets from the last call to ReadvProc/WritevProc */
1955 rxi_FreePackets(0, &call->iovq);
1957 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
1958 MUTEX_EXIT(&call->lock);
1959 if (conn->type == RX_CLIENT_CONNECTION) {
1960 MUTEX_EXIT(&conn->conn_call_lock);
1961 conn->flags &= ~RX_CONN_BUSY;
1965 * Map errors to the local host's errno.h format.
1967 error = ntoh_syserr_conv(error);
1971 #if !defined(KERNEL)
1973 /* Call this routine when shutting down a server or client (especially
1974 * clients). This will allow Rx to gracefully garbage collect server
1975 * connections, and reduce the number of retries that a server might
1976 * make to a dead client.
1977 * This is not quite right, since some calls may still be ongoing and
1978 * we can't lock them to destroy them. */
1982 register struct rx_connection **conn_ptr, **conn_end;
1986 if (rxinit_status == 1) {
1988 return; /* Already shutdown. */
1990 rxi_DeleteCachedConnections();
1991 if (rx_connHashTable) {
1992 MUTEX_ENTER(&rx_connHashTable_lock);
1993 for (conn_ptr = &rx_connHashTable[0], conn_end =
1994 &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
1996 struct rx_connection *conn, *next;
1997 for (conn = *conn_ptr; conn; conn = next) {
1999 if (conn->type == RX_CLIENT_CONNECTION) {
2000 /* MUTEX_ENTER(&conn->conn_data_lock); when used in kernel */
2002 /* MUTEX_EXIT(&conn->conn_data_lock); when used in kernel */
2003 #ifdef RX_ENABLE_LOCKS
2004 rxi_DestroyConnectionNoLock(conn);
2005 #else /* RX_ENABLE_LOCKS */
2006 rxi_DestroyConnection(conn);
2007 #endif /* RX_ENABLE_LOCKS */
2011 #ifdef RX_ENABLE_LOCKS
2012 while (rx_connCleanup_list) {
2013 struct rx_connection *conn;
2014 conn = rx_connCleanup_list;
2015 rx_connCleanup_list = rx_connCleanup_list->next;
2016 MUTEX_EXIT(&rx_connHashTable_lock);
2017 rxi_CleanupConnection(conn);
2018 MUTEX_ENTER(&rx_connHashTable_lock);
2020 MUTEX_EXIT(&rx_connHashTable_lock);
2021 #endif /* RX_ENABLE_LOCKS */
2026 afs_winsockCleanup();
2034 /* if we wakeup packet waiter too often, can get in loop with two
2035 AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2037 rxi_PacketsUnWait(void)
2039 if (!rx_waitingForPackets) {
2043 if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2044 return; /* still over quota */
2047 rx_waitingForPackets = 0;
2048 #ifdef RX_ENABLE_LOCKS
2049 CV_BROADCAST(&rx_waitingForPackets_cv);
2051 osi_rxWakeup(&rx_waitingForPackets);
2057 /* ------------------Internal interfaces------------------------- */
2059 /* Return this process's service structure for the
2060 * specified socket and service */
2062 rxi_FindService(register osi_socket socket, register u_short serviceId)
2064 register struct rx_service **sp;
2065 for (sp = &rx_services[0]; *sp; sp++) {
2066 if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2072 /* Allocate a call structure, for the indicated channel of the
2073 * supplied connection. The mode and state of the call must be set by
2074 * the caller. Returns the call with mutex locked. */
2076 rxi_NewCall(register struct rx_connection *conn, register int channel)
2078 register struct rx_call *call;
2079 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2080 register struct rx_call *cp; /* Call pointer temp */
2081 register struct rx_call *nxp; /* Next call pointer, for queue_Scan */
2082 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2084 dpf(("rxi_NewCall(conn %x, channel %d)\n", conn, channel));
2086 /* Grab an existing call structure, or allocate a new one.
2087 * Existing call structures are assumed to have been left reset by
2089 MUTEX_ENTER(&rx_freeCallQueue_lock);
2091 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2093 * EXCEPT that the TQ might not yet be cleared out.
2094 * Skip over those with in-use TQs.
2097 for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2098 if (!(cp->flags & RX_CALL_TQ_BUSY)) {
2104 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2105 if (queue_IsNotEmpty(&rx_freeCallQueue)) {
2106 call = queue_First(&rx_freeCallQueue, rx_call);
2107 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2109 MUTEX_ENTER(&rx_stats_mutex);
2110 rx_stats.nFreeCallStructs--;
2111 MUTEX_EXIT(&rx_stats_mutex);
2112 MUTEX_EXIT(&rx_freeCallQueue_lock);
2113 MUTEX_ENTER(&call->lock);
2114 CLEAR_CALL_QUEUE_LOCK(call);
2115 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2116 /* Now, if TQ wasn't cleared earlier, do it now. */
2117 if (call->flags & RX_CALL_TQ_CLEARME) {
2118 rxi_ClearTransmitQueue(call, 0);
2119 queue_Init(&call->tq);
2121 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2122 /* Bind the call to its connection structure */
2124 rxi_ResetCall(call, 1);
2126 call = (struct rx_call *)rxi_Alloc(sizeof(struct rx_call));
2128 MUTEX_EXIT(&rx_freeCallQueue_lock);
2129 MUTEX_INIT(&call->lock, "call lock", MUTEX_DEFAULT, NULL);
2130 MUTEX_ENTER(&call->lock);
2131 CV_INIT(&call->cv_twind, "call twind", CV_DEFAULT, 0);
2132 CV_INIT(&call->cv_rq, "call rq", CV_DEFAULT, 0);
2133 CV_INIT(&call->cv_tq, "call tq", CV_DEFAULT, 0);
2135 MUTEX_ENTER(&rx_stats_mutex);
2136 rx_stats.nCallStructs++;
2137 MUTEX_EXIT(&rx_stats_mutex);
2138 /* Initialize once-only items */
2139 queue_Init(&call->tq);
2140 queue_Init(&call->rq);
2141 queue_Init(&call->iovq);
2142 /* Bind the call to its connection structure (prereq for reset) */
2144 rxi_ResetCall(call, 1);
2146 call->channel = channel;
2147 call->callNumber = &conn->callNumber[channel];
2148 /* Note that the next expected call number is retained (in
2149 * conn->callNumber[i]), even if we reallocate the call structure
2151 conn->call[channel] = call;
2152 /* if the channel's never been used (== 0), we should start at 1, otherwise
2153 * the call number is valid from the last time this channel was used */
2154 if (*call->callNumber == 0)
2155 *call->callNumber = 1;
2160 /* A call has been inactive long enough that so we can throw away
2161 * state, including the call structure, which is placed on the call
2163 * Call is locked upon entry.
2164 * haveCTLock set if called from rxi_ReapConnections
2166 #ifdef RX_ENABLE_LOCKS
2168 rxi_FreeCall(register struct rx_call *call, int haveCTLock)
2169 #else /* RX_ENABLE_LOCKS */
2171 rxi_FreeCall(register struct rx_call *call)
2172 #endif /* RX_ENABLE_LOCKS */
2174 register int channel = call->channel;
2175 register struct rx_connection *conn = call->conn;
2178 if (call->state == RX_STATE_DALLY || call->state == RX_STATE_HOLD)
2179 (*call->callNumber)++;
2180 rxi_ResetCall(call, 0);
2181 call->conn->call[channel] = (struct rx_call *)0;
2183 MUTEX_ENTER(&rx_freeCallQueue_lock);
2184 SET_CALL_QUEUE_LOCK(call, &rx_freeCallQueue_lock);
2185 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2186 /* A call may be free even though its transmit queue is still in use.
2187 * Since we search the call list from head to tail, put busy calls at
2188 * the head of the list, and idle calls at the tail.
2190 if (call->flags & RX_CALL_TQ_BUSY)
2191 queue_Prepend(&rx_freeCallQueue, call);
2193 queue_Append(&rx_freeCallQueue, call);
2194 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2195 queue_Append(&rx_freeCallQueue, call);
2196 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2197 MUTEX_ENTER(&rx_stats_mutex);
2198 rx_stats.nFreeCallStructs++;
2199 MUTEX_EXIT(&rx_stats_mutex);
2201 MUTEX_EXIT(&rx_freeCallQueue_lock);
2203 /* Destroy the connection if it was previously slated for
2204 * destruction, i.e. the Rx client code previously called
2205 * rx_DestroyConnection (client connections), or
2206 * rxi_ReapConnections called the same routine (server
2207 * connections). Only do this, however, if there are no
2208 * outstanding calls. Note that for fine grain locking, there appears
2209 * to be a deadlock in that rxi_FreeCall has a call locked and
2210 * DestroyConnectionNoLock locks each call in the conn. But note a
2211 * few lines up where we have removed this call from the conn.
2212 * If someone else destroys a connection, they either have no
2213 * call lock held or are going through this section of code.
2215 if (conn->flags & RX_CONN_DESTROY_ME && !(conn->flags & RX_CONN_MAKECALL_WAITING)) {
2216 MUTEX_ENTER(&conn->conn_data_lock);
2218 MUTEX_EXIT(&conn->conn_data_lock);
2219 #ifdef RX_ENABLE_LOCKS
2221 rxi_DestroyConnectionNoLock(conn);
2223 rxi_DestroyConnection(conn);
2224 #else /* RX_ENABLE_LOCKS */
2225 rxi_DestroyConnection(conn);
2226 #endif /* RX_ENABLE_LOCKS */
2230 afs_int32 rxi_Alloccnt = 0, rxi_Allocsize = 0;
2232 rxi_Alloc(register size_t size)
2236 MUTEX_ENTER(&rx_stats_mutex);
2238 rxi_Allocsize += (afs_int32)size;
2239 MUTEX_EXIT(&rx_stats_mutex);
2241 p = (char *)osi_Alloc(size);
2244 osi_Panic("rxi_Alloc error");
2250 rxi_Free(void *addr, register size_t size)
2252 MUTEX_ENTER(&rx_stats_mutex);
2254 rxi_Allocsize -= (afs_int32)size;
2255 MUTEX_EXIT(&rx_stats_mutex);
2257 osi_Free(addr, size);
2260 /* Find the peer process represented by the supplied (host,port)
2261 * combination. If there is no appropriate active peer structure, a
2262 * new one will be allocated and initialized
2263 * The origPeer, if set, is a pointer to a peer structure on which the
2264 * refcount will be be decremented. This is used to replace the peer
2265 * structure hanging off a connection structure */
2267 rxi_FindPeer(register afs_uint32 host, register u_short port,
2268 struct rx_peer *origPeer, int create)
2270 register struct rx_peer *pp;
2272 hashIndex = PEER_HASH(host, port);
2273 MUTEX_ENTER(&rx_peerHashTable_lock);
2274 for (pp = rx_peerHashTable[hashIndex]; pp; pp = pp->next) {
2275 if ((pp->host == host) && (pp->port == port))
2280 pp = rxi_AllocPeer(); /* This bzero's *pp */
2281 pp->host = host; /* set here or in InitPeerParams is zero */
2283 MUTEX_INIT(&pp->peer_lock, "peer_lock", MUTEX_DEFAULT, 0);
2284 queue_Init(&pp->congestionQueue);
2285 queue_Init(&pp->rpcStats);
2286 pp->next = rx_peerHashTable[hashIndex];
2287 rx_peerHashTable[hashIndex] = pp;
2288 rxi_InitPeerParams(pp);
2289 MUTEX_ENTER(&rx_stats_mutex);
2290 rx_stats.nPeerStructs++;
2291 MUTEX_EXIT(&rx_stats_mutex);
2298 origPeer->refCount--;
2299 MUTEX_EXIT(&rx_peerHashTable_lock);
2304 /* Find the connection at (host, port) started at epoch, and with the
2305 * given connection id. Creates the server connection if necessary.
2306 * The type specifies whether a client connection or a server
2307 * connection is desired. In both cases, (host, port) specify the
2308 * peer's (host, pair) pair. Client connections are not made
2309 * automatically by this routine. The parameter socket gives the
2310 * socket descriptor on which the packet was received. This is used,
2311 * in the case of server connections, to check that *new* connections
2312 * come via a valid (port, serviceId). Finally, the securityIndex
2313 * parameter must match the existing index for the connection. If a
2314 * server connection is created, it will be created using the supplied
2315 * index, if the index is valid for this service */
2316 struct rx_connection *
2317 rxi_FindConnection(osi_socket socket, register afs_int32 host,
2318 register u_short port, u_short serviceId, afs_uint32 cid,
2319 afs_uint32 epoch, int type, u_int securityIndex)
2321 int hashindex, flag;
2322 register struct rx_connection *conn;
2323 hashindex = CONN_HASH(host, port, cid, epoch, type);
2324 MUTEX_ENTER(&rx_connHashTable_lock);
2325 rxLastConn ? (conn = rxLastConn, flag = 0) : (conn =
2326 rx_connHashTable[hashindex],
2329 if ((conn->type == type) && ((cid & RX_CIDMASK) == conn->cid)
2330 && (epoch == conn->epoch)) {
2331 register struct rx_peer *pp = conn->peer;
2332 if (securityIndex != conn->securityIndex) {
2333 /* this isn't supposed to happen, but someone could forge a packet
2334 * like this, and there seems to be some CM bug that makes this
2335 * happen from time to time -- in which case, the fileserver
2337 MUTEX_EXIT(&rx_connHashTable_lock);
2338 return (struct rx_connection *)0;
2340 if (pp->host == host && pp->port == port)
2342 if (type == RX_CLIENT_CONNECTION && pp->port == port)
2344 /* So what happens when it's a callback connection? */
2345 if ( /*type == RX_CLIENT_CONNECTION && */
2346 (conn->epoch & 0x80000000))
2350 /* the connection rxLastConn that was used the last time is not the
2351 ** one we are looking for now. Hence, start searching in the hash */
2353 conn = rx_connHashTable[hashindex];
2358 struct rx_service *service;
2359 if (type == RX_CLIENT_CONNECTION) {
2360 MUTEX_EXIT(&rx_connHashTable_lock);
2361 return (struct rx_connection *)0;
2363 service = rxi_FindService(socket, serviceId);
2364 if (!service || (securityIndex >= service->nSecurityObjects)
2365 || (service->securityObjects[securityIndex] == 0)) {
2366 MUTEX_EXIT(&rx_connHashTable_lock);
2367 return (struct rx_connection *)0;
2369 conn = rxi_AllocConnection(); /* This bzero's the connection */
2370 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
2371 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
2372 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
2373 conn->next = rx_connHashTable[hashindex];
2374 rx_connHashTable[hashindex] = conn;
2375 conn->peer = rxi_FindPeer(host, port, 0, 1);
2376 conn->type = RX_SERVER_CONNECTION;
2377 conn->lastSendTime = clock_Sec(); /* don't GC immediately */
2378 conn->epoch = epoch;
2379 conn->cid = cid & RX_CIDMASK;
2380 /* conn->serial = conn->lastSerial = 0; */
2381 /* conn->timeout = 0; */
2382 conn->ackRate = RX_FAST_ACK_RATE;
2383 conn->service = service;
2384 conn->serviceId = serviceId;
2385 conn->securityIndex = securityIndex;
2386 conn->securityObject = service->securityObjects[securityIndex];
2387 conn->nSpecific = 0;
2388 conn->specific = NULL;
2389 rx_SetConnDeadTime(conn, service->connDeadTime);
2390 rx_SetConnIdleDeadTime(conn, service->idleDeadTime);
2391 /* Notify security object of the new connection */
2392 RXS_NewConnection(conn->securityObject, conn);
2393 /* XXXX Connection timeout? */
2394 if (service->newConnProc)
2395 (*service->newConnProc) (conn);
2396 MUTEX_ENTER(&rx_stats_mutex);
2397 rx_stats.nServerConns++;
2398 MUTEX_EXIT(&rx_stats_mutex);
2401 MUTEX_ENTER(&conn->conn_data_lock);
2403 MUTEX_EXIT(&conn->conn_data_lock);
2405 rxLastConn = conn; /* store this connection as the last conn used */
2406 MUTEX_EXIT(&rx_connHashTable_lock);
2410 /* There are two packet tracing routines available for testing and monitoring
2411 * Rx. One is called just after every packet is received and the other is
2412 * called just before every packet is sent. Received packets, have had their
2413 * headers decoded, and packets to be sent have not yet had their headers
2414 * encoded. Both take two parameters: a pointer to the packet and a sockaddr
2415 * containing the network address. Both can be modified. The return value, if
2416 * non-zero, indicates that the packet should be dropped. */
2418 int (*rx_justReceived) () = 0;
2419 int (*rx_almostSent) () = 0;
2421 /* A packet has been received off the interface. Np is the packet, socket is
2422 * the socket number it was received from (useful in determining which service
2423 * this packet corresponds to), and (host, port) reflect the host,port of the
2424 * sender. This call returns the packet to the caller if it is finished with
2425 * it, rather than de-allocating it, just as a small performance hack */
2428 rxi_ReceivePacket(register struct rx_packet *np, osi_socket socket,
2429 afs_uint32 host, u_short port, int *tnop,
2430 struct rx_call **newcallp)
2432 register struct rx_call *call;
2433 register struct rx_connection *conn;
2435 afs_uint32 currentCallNumber;
2441 struct rx_packet *tnp;
2444 /* We don't print out the packet until now because (1) the time may not be
2445 * accurate enough until now in the lwp implementation (rx_Listener only gets
2446 * the time after the packet is read) and (2) from a protocol point of view,
2447 * this is the first time the packet has been seen */
2448 packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
2449 ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
2450 dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %x",
2451 np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
2452 np->header.epoch, np->header.cid, np->header.callNumber,
2453 np->header.seq, np->header.flags, np));
2456 if (np->header.type == RX_PACKET_TYPE_VERSION) {
2457 return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
2460 if (np->header.type == RX_PACKET_TYPE_DEBUG) {
2461 return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
2464 /* If an input tracer function is defined, call it with the packet and
2465 * network address. Note this function may modify its arguments. */
2466 if (rx_justReceived) {
2467 struct sockaddr_in addr;
2469 addr.sin_family = AF_INET;
2470 addr.sin_port = port;
2471 addr.sin_addr.s_addr = host;
2472 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
2473 addr.sin_len = sizeof(addr);
2474 #endif /* AFS_OSF_ENV */
2475 drop = (*rx_justReceived) (np, &addr);
2476 /* drop packet if return value is non-zero */
2479 port = addr.sin_port; /* in case fcn changed addr */
2480 host = addr.sin_addr.s_addr;
2484 /* If packet was not sent by the client, then *we* must be the client */
2485 type = ((np->header.flags & RX_CLIENT_INITIATED) != RX_CLIENT_INITIATED)
2486 ? RX_CLIENT_CONNECTION : RX_SERVER_CONNECTION;
2488 /* Find the connection (or fabricate one, if we're the server & if
2489 * necessary) associated with this packet */
2491 rxi_FindConnection(socket, host, port, np->header.serviceId,
2492 np->header.cid, np->header.epoch, type,
2493 np->header.securityIndex);
2496 /* If no connection found or fabricated, just ignore the packet.
2497 * (An argument could be made for sending an abort packet for
2502 MUTEX_ENTER(&conn->conn_data_lock);
2503 if (conn->maxSerial < np->header.serial)
2504 conn->maxSerial = np->header.serial;
2505 MUTEX_EXIT(&conn->conn_data_lock);
2507 /* If the connection is in an error state, send an abort packet and ignore
2508 * the incoming packet */
2510 /* Don't respond to an abort packet--we don't want loops! */
2511 MUTEX_ENTER(&conn->conn_data_lock);
2512 if (np->header.type != RX_PACKET_TYPE_ABORT)
2513 np = rxi_SendConnectionAbort(conn, np, 1, 0);
2515 MUTEX_EXIT(&conn->conn_data_lock);
2519 /* Check for connection-only requests (i.e. not call specific). */
2520 if (np->header.callNumber == 0) {
2521 switch (np->header.type) {
2522 case RX_PACKET_TYPE_ABORT: {
2523 /* What if the supplied error is zero? */
2524 afs_int32 errcode = ntohl(rx_GetInt32(np, 0));
2525 dpf(("rxi_ReceivePacket ABORT rx_GetInt32 = %d", errcode));
2526 rxi_ConnectionError(conn, errcode);
2527 MUTEX_ENTER(&conn->conn_data_lock);
2529 MUTEX_EXIT(&conn->conn_data_lock);
2532 case RX_PACKET_TYPE_CHALLENGE:
2533 tnp = rxi_ReceiveChallengePacket(conn, np, 1);
2534 MUTEX_ENTER(&conn->conn_data_lock);
2536 MUTEX_EXIT(&conn->conn_data_lock);
2538 case RX_PACKET_TYPE_RESPONSE:
2539 tnp = rxi_ReceiveResponsePacket(conn, np, 1);
2540 MUTEX_ENTER(&conn->conn_data_lock);
2542 MUTEX_EXIT(&conn->conn_data_lock);
2544 case RX_PACKET_TYPE_PARAMS:
2545 case RX_PACKET_TYPE_PARAMS + 1:
2546 case RX_PACKET_TYPE_PARAMS + 2:
2547 /* ignore these packet types for now */
2548 MUTEX_ENTER(&conn->conn_data_lock);
2550 MUTEX_EXIT(&conn->conn_data_lock);
2555 /* Should not reach here, unless the peer is broken: send an
2557 rxi_ConnectionError(conn, RX_PROTOCOL_ERROR);
2558 MUTEX_ENTER(&conn->conn_data_lock);
2559 tnp = rxi_SendConnectionAbort(conn, np, 1, 0);
2561 MUTEX_EXIT(&conn->conn_data_lock);
2566 channel = np->header.cid & RX_CHANNELMASK;
2567 call = conn->call[channel];
2568 #ifdef RX_ENABLE_LOCKS
2570 MUTEX_ENTER(&call->lock);
2571 /* Test to see if call struct is still attached to conn. */
2572 if (call != conn->call[channel]) {
2574 MUTEX_EXIT(&call->lock);
2575 if (type == RX_SERVER_CONNECTION) {
2576 call = conn->call[channel];
2577 /* If we started with no call attached and there is one now,
2578 * another thread is also running this routine and has gotten
2579 * the connection channel. We should drop this packet in the tests
2580 * below. If there was a call on this connection and it's now
2581 * gone, then we'll be making a new call below.
2582 * If there was previously a call and it's now different then
2583 * the old call was freed and another thread running this routine
2584 * has created a call on this channel. One of these two threads
2585 * has a packet for the old call and the code below handles those
2589 MUTEX_ENTER(&call->lock);
2591 /* This packet can't be for this call. If the new call address is
2592 * 0 then no call is running on this channel. If there is a call
2593 * then, since this is a client connection we're getting data for
2594 * it must be for the previous call.
2596 MUTEX_ENTER(&rx_stats_mutex);
2597 rx_stats.spuriousPacketsRead++;
2598 MUTEX_EXIT(&rx_stats_mutex);
2599 MUTEX_ENTER(&conn->conn_data_lock);
2601 MUTEX_EXIT(&conn->conn_data_lock);
2606 currentCallNumber = conn->callNumber[channel];
2608 if (type == RX_SERVER_CONNECTION) { /* We're the server */
2609 if (np->header.callNumber < currentCallNumber) {
2610 MUTEX_ENTER(&rx_stats_mutex);
2611 rx_stats.spuriousPacketsRead++;
2612 MUTEX_EXIT(&rx_stats_mutex);
2613 #ifdef RX_ENABLE_LOCKS
2615 MUTEX_EXIT(&call->lock);
2617 MUTEX_ENTER(&conn->conn_data_lock);
2619 MUTEX_EXIT(&conn->conn_data_lock);
2623 MUTEX_ENTER(&conn->conn_call_lock);
2624 call = rxi_NewCall(conn, channel);
2625 MUTEX_EXIT(&conn->conn_call_lock);
2626 *call->callNumber = np->header.callNumber;
2627 if (np->header.callNumber == 0)
2628 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));
2630 call->state = RX_STATE_PRECALL;
2631 clock_GetTime(&call->queueTime);
2632 hzero(call->bytesSent);
2633 hzero(call->bytesRcvd);
2635 * If the number of queued calls exceeds the overload
2636 * threshold then abort this call.
2638 if ((rx_BusyThreshold > 0) && (rx_nWaiting > rx_BusyThreshold)) {
2639 struct rx_packet *tp;
2641 rxi_CallError(call, rx_BusyError);
2642 tp = rxi_SendCallAbort(call, np, 1, 0);
2643 MUTEX_EXIT(&call->lock);
2644 MUTEX_ENTER(&conn->conn_data_lock);
2646 MUTEX_EXIT(&conn->conn_data_lock);
2647 MUTEX_ENTER(&rx_stats_mutex);
2649 MUTEX_EXIT(&rx_stats_mutex);
2652 rxi_KeepAliveOn(call);
2653 } else if (np->header.callNumber != currentCallNumber) {
2654 /* Wait until the transmit queue is idle before deciding
2655 * whether to reset the current call. Chances are that the
2656 * call will be in ether DALLY or HOLD state once the TQ_BUSY
2659 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2660 while ((call->state == RX_STATE_ACTIVE)
2661 && (call->flags & RX_CALL_TQ_BUSY)) {
2662 call->flags |= RX_CALL_TQ_WAIT;
2664 #ifdef RX_ENABLE_LOCKS
2665 osirx_AssertMine(&call->lock, "rxi_Start lock3");
2666 CV_WAIT(&call->cv_tq, &call->lock);
2667 #else /* RX_ENABLE_LOCKS */
2668 osi_rxSleep(&call->tq);
2669 #endif /* RX_ENABLE_LOCKS */
2671 if (call->tqWaiters == 0)
2672 call->flags &= ~RX_CALL_TQ_WAIT;
2674 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2675 /* If the new call cannot be taken right now send a busy and set
2676 * the error condition in this call, so that it terminates as
2677 * quickly as possible */
2678 if (call->state == RX_STATE_ACTIVE) {
2679 struct rx_packet *tp;
2681 rxi_CallError(call, RX_CALL_DEAD);
2682 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
2684 MUTEX_EXIT(&call->lock);
2685 MUTEX_ENTER(&conn->conn_data_lock);
2687 MUTEX_EXIT(&conn->conn_data_lock);
2690 rxi_ResetCall(call, 0);
2691 *call->callNumber = np->header.callNumber;
2692 if (np->header.callNumber == 0)
2693 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));
2695 call->state = RX_STATE_PRECALL;
2696 clock_GetTime(&call->queueTime);
2697 hzero(call->bytesSent);
2698 hzero(call->bytesRcvd);
2700 * If the number of queued calls exceeds the overload
2701 * threshold then abort this call.
2703 if ((rx_BusyThreshold > 0) && (rx_nWaiting > rx_BusyThreshold)) {
2704 struct rx_packet *tp;
2706 rxi_CallError(call, rx_BusyError);
2707 tp = rxi_SendCallAbort(call, np, 1, 0);
2708 MUTEX_EXIT(&call->lock);
2709 MUTEX_ENTER(&conn->conn_data_lock);
2711 MUTEX_EXIT(&conn->conn_data_lock);
2712 MUTEX_ENTER(&rx_stats_mutex);
2714 MUTEX_EXIT(&rx_stats_mutex);
2717 rxi_KeepAliveOn(call);
2719 /* Continuing call; do nothing here. */
2721 } else { /* we're the client */
2722 /* Ignore all incoming acknowledgements for calls in DALLY state */
2723 if (call && (call->state == RX_STATE_DALLY)
2724 && (np->header.type == RX_PACKET_TYPE_ACK)) {
2725 MUTEX_ENTER(&rx_stats_mutex);
2726 rx_stats.ignorePacketDally++;
2727 MUTEX_EXIT(&rx_stats_mutex);
2728 #ifdef RX_ENABLE_LOCKS
2730 MUTEX_EXIT(&call->lock);
2733 MUTEX_ENTER(&conn->conn_data_lock);
2735 MUTEX_EXIT(&conn->conn_data_lock);
2739 /* Ignore anything that's not relevant to the current call. If there
2740 * isn't a current call, then no packet is relevant. */
2741 if (!call || (np->header.callNumber != currentCallNumber)) {
2742 MUTEX_ENTER(&rx_stats_mutex);
2743 rx_stats.spuriousPacketsRead++;
2744 MUTEX_EXIT(&rx_stats_mutex);
2745 #ifdef RX_ENABLE_LOCKS
2747 MUTEX_EXIT(&call->lock);
2750 MUTEX_ENTER(&conn->conn_data_lock);
2752 MUTEX_EXIT(&conn->conn_data_lock);
2755 /* If the service security object index stamped in the packet does not
2756 * match the connection's security index, ignore the packet */
2757 if (np->header.securityIndex != conn->securityIndex) {
2758 #ifdef RX_ENABLE_LOCKS
2759 MUTEX_EXIT(&call->lock);
2761 MUTEX_ENTER(&conn->conn_data_lock);
2763 MUTEX_EXIT(&conn->conn_data_lock);
2767 /* If we're receiving the response, then all transmit packets are
2768 * implicitly acknowledged. Get rid of them. */
2769 if (np->header.type == RX_PACKET_TYPE_DATA) {
2770 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2771 /* XXX Hack. Because we must release the global rx lock when
2772 * sending packets (osi_NetSend) we drop all acks while we're
2773 * traversing the tq in rxi_Start sending packets out because
2774 * packets may move to the freePacketQueue as result of being here!
2775 * So we drop these packets until we're safely out of the
2776 * traversing. Really ugly!
2777 * For fine grain RX locking, we set the acked field in the
2778 * packets and let rxi_Start remove them from the transmit queue.
2780 if (call->flags & RX_CALL_TQ_BUSY) {
2781 #ifdef RX_ENABLE_LOCKS
2782 rxi_SetAcksInTransmitQueue(call);
2785 return np; /* xmitting; drop packet */
2788 rxi_ClearTransmitQueue(call, 0);
2790 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2791 rxi_ClearTransmitQueue(call, 0);
2792 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2794 if (np->header.type == RX_PACKET_TYPE_ACK) {
2795 /* now check to see if this is an ack packet acknowledging that the
2796 * server actually *lost* some hard-acked data. If this happens we
2797 * ignore this packet, as it may indicate that the server restarted in
2798 * the middle of a call. It is also possible that this is an old ack
2799 * packet. We don't abort the connection in this case, because this
2800 * *might* just be an old ack packet. The right way to detect a server
2801 * restart in the midst of a call is to notice that the server epoch
2803 /* XXX I'm not sure this is exactly right, since tfirst **IS**
2804 * XXX unacknowledged. I think that this is off-by-one, but
2805 * XXX I don't dare change it just yet, since it will
2806 * XXX interact badly with the server-restart detection
2807 * XXX code in receiveackpacket. */
2808 if (ntohl(rx_GetInt32(np, FIRSTACKOFFSET)) < call->tfirst) {
2809 MUTEX_ENTER(&rx_stats_mutex);
2810 rx_stats.spuriousPacketsRead++;
2811 MUTEX_EXIT(&rx_stats_mutex);
2812 MUTEX_EXIT(&call->lock);
2813 MUTEX_ENTER(&conn->conn_data_lock);
2815 MUTEX_EXIT(&conn->conn_data_lock);
2819 } /* else not a data packet */
2822 osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
2823 /* Set remote user defined status from packet */
2824 call->remoteStatus = np->header.userStatus;
2826 /* Note the gap between the expected next packet and the actual
2827 * packet that arrived, when the new packet has a smaller serial number
2828 * than expected. Rioses frequently reorder packets all by themselves,
2829 * so this will be quite important with very large window sizes.
2830 * Skew is checked against 0 here to avoid any dependence on the type of
2831 * inPacketSkew (which may be unsigned). In C, -1 > (unsigned) 0 is always
2833 * The inPacketSkew should be a smoothed running value, not just a maximum. MTUXXX
2834 * see CalculateRoundTripTime for an example of how to keep smoothed values.
2835 * I think using a beta of 1/8 is probably appropriate. 93.04.21
2837 MUTEX_ENTER(&conn->conn_data_lock);
2838 skew = conn->lastSerial - np->header.serial;
2839 conn->lastSerial = np->header.serial;
2840 MUTEX_EXIT(&conn->conn_data_lock);
2842 register struct rx_peer *peer;
2844 if (skew > peer->inPacketSkew) {
2845 dpf(("*** In skew changed from %d to %d\n", peer->inPacketSkew,
2847 peer->inPacketSkew = skew;
2851 /* Now do packet type-specific processing */
2852 switch (np->header.type) {
2853 case RX_PACKET_TYPE_DATA:
2854 np = rxi_ReceiveDataPacket(call, np, 1, socket, host, port, tnop,
2857 case RX_PACKET_TYPE_ACK:
2858 /* Respond immediately to ack packets requesting acknowledgement
2860 if (np->header.flags & RX_REQUEST_ACK) {
2862 (void)rxi_SendCallAbort(call, 0, 1, 0);
2864 (void)rxi_SendAck(call, 0, np->header.serial,
2865 RX_ACK_PING_RESPONSE, 1);
2867 np = rxi_ReceiveAckPacket(call, np, 1);
2869 case RX_PACKET_TYPE_ABORT: {
2870 /* An abort packet: reset the call, passing the error up to the user. */
2871 /* What if error is zero? */
2872 /* What if the error is -1? the application will treat it as a timeout. */
2873 afs_int32 errdata = ntohl(*(afs_int32 *) rx_DataOf(np));
2874 dpf(("rxi_ReceivePacket ABORT rx_DataOf = %d", errdata));
2875 rxi_CallError(call, errdata);
2876 MUTEX_EXIT(&call->lock);
2877 MUTEX_ENTER(&conn->conn_data_lock);
2879 MUTEX_EXIT(&conn->conn_data_lock);
2880 return np; /* xmitting; drop packet */
2882 case RX_PACKET_TYPE_BUSY:
2885 case RX_PACKET_TYPE_ACKALL:
2886 /* All packets acknowledged, so we can drop all packets previously
2887 * readied for sending */
2888 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2889 /* XXX Hack. We because we can't release the global rx lock when
2890 * sending packets (osi_NetSend) we drop all ack pkts while we're
2891 * traversing the tq in rxi_Start sending packets out because
2892 * packets may move to the freePacketQueue as result of being
2893 * here! So we drop these packets until we're safely out of the
2894 * traversing. Really ugly!
2895 * For fine grain RX locking, we set the acked field in the packets
2896 * and let rxi_Start remove the packets from the transmit queue.
2898 if (call->flags & RX_CALL_TQ_BUSY) {
2899 #ifdef RX_ENABLE_LOCKS
2900 rxi_SetAcksInTransmitQueue(call);
2902 #else /* RX_ENABLE_LOCKS */
2903 MUTEX_EXIT(&call->lock);
2904 MUTEX_ENTER(&conn->conn_data_lock);
2906 MUTEX_EXIT(&conn->conn_data_lock);
2907 return np; /* xmitting; drop packet */
2908 #endif /* RX_ENABLE_LOCKS */
2910 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2911 rxi_ClearTransmitQueue(call, 0);
2914 /* Should not reach here, unless the peer is broken: send an abort
2916 rxi_CallError(call, RX_PROTOCOL_ERROR);
2917 np = rxi_SendCallAbort(call, np, 1, 0);
2920 /* Note when this last legitimate packet was received, for keep-alive
2921 * processing. Note, we delay getting the time until now in the hope that
2922 * the packet will be delivered to the user before any get time is required
2923 * (if not, then the time won't actually be re-evaluated here). */
2924 call->lastReceiveTime = clock_Sec();
2925 MUTEX_EXIT(&call->lock);
2926 MUTEX_ENTER(&conn->conn_data_lock);
2928 MUTEX_EXIT(&conn->conn_data_lock);
2932 /* return true if this is an "interesting" connection from the point of view
2933 of someone trying to debug the system */
2935 rxi_IsConnInteresting(struct rx_connection *aconn)
2938 register struct rx_call *tcall;
2940 if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
2942 for (i = 0; i < RX_MAXCALLS; i++) {
2943 tcall = aconn->call[i];
2945 if ((tcall->state == RX_STATE_PRECALL)
2946 || (tcall->state == RX_STATE_ACTIVE))
2948 if ((tcall->mode == RX_MODE_SENDING)
2949 || (tcall->mode == RX_MODE_RECEIVING))
2957 /* if this is one of the last few packets AND it wouldn't be used by the
2958 receiving call to immediately satisfy a read request, then drop it on
2959 the floor, since accepting it might prevent a lock-holding thread from
2960 making progress in its reading. If a call has been cleared while in
2961 the precall state then ignore all subsequent packets until the call
2962 is assigned to a thread. */
2965 TooLow(struct rx_packet *ap, struct rx_call *acall)
2968 MUTEX_ENTER(&rx_stats_mutex);
2969 if (((ap->header.seq != 1) && (acall->flags & RX_CALL_CLEARED)
2970 && (acall->state == RX_STATE_PRECALL))
2971 || ((rx_nFreePackets < rxi_dataQuota + 2)
2972 && !((ap->header.seq < acall->rnext + rx_initSendWindow)
2973 && (acall->flags & RX_CALL_READER_WAIT)))) {
2976 MUTEX_EXIT(&rx_stats_mutex);
2982 rxi_CheckReachEvent(struct rxevent *event, struct rx_connection *conn,
2983 struct rx_call *acall)
2985 struct rx_call *call = acall;
2989 MUTEX_ENTER(&conn->conn_data_lock);
2990 conn->checkReachEvent = NULL;
2991 waiting = conn->flags & RX_CONN_ATTACHWAIT;
2994 MUTEX_EXIT(&conn->conn_data_lock);
2998 MUTEX_ENTER(&conn->conn_call_lock);
2999 MUTEX_ENTER(&conn->conn_data_lock);
3000 for (i = 0; i < RX_MAXCALLS; i++) {
3001 struct rx_call *tc = conn->call[i];
3002 if (tc && tc->state == RX_STATE_PRECALL) {
3008 /* Indicate that rxi_CheckReachEvent is no longer running by
3009 * clearing the flag. Must be atomic under conn_data_lock to
3010 * avoid a new call slipping by: rxi_CheckConnReach holds
3011 * conn_data_lock while checking RX_CONN_ATTACHWAIT.
3013 conn->flags &= ~RX_CONN_ATTACHWAIT;
3014 MUTEX_EXIT(&conn->conn_data_lock);
3015 MUTEX_EXIT(&conn->conn_call_lock);
3020 MUTEX_ENTER(&call->lock);
3021 rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3023 MUTEX_EXIT(&call->lock);
3025 clock_GetTime(&when);
3026 when.sec += RX_CHECKREACH_TIMEOUT;
3027 MUTEX_ENTER(&conn->conn_data_lock);
3028 if (!conn->checkReachEvent) {
3030 conn->checkReachEvent =
3031 rxevent_Post(&when, rxi_CheckReachEvent, conn, NULL);
3033 MUTEX_EXIT(&conn->conn_data_lock);
3039 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3041 struct rx_service *service = conn->service;
3042 struct rx_peer *peer = conn->peer;
3043 afs_uint32 now, lastReach;
3045 if (service->checkReach == 0)
3049 MUTEX_ENTER(&peer->peer_lock);
3050 lastReach = peer->lastReachTime;
3051 MUTEX_EXIT(&peer->peer_lock);
3052 if (now - lastReach < RX_CHECKREACH_TTL)
3055 MUTEX_ENTER(&conn->conn_data_lock);
3056 if (conn->flags & RX_CONN_ATTACHWAIT) {
3057 MUTEX_EXIT(&conn->conn_data_lock);
3060 conn->flags |= RX_CONN_ATTACHWAIT;
3061 MUTEX_EXIT(&conn->conn_data_lock);
3062 if (!conn->checkReachEvent)
3063 rxi_CheckReachEvent(NULL, conn, call);
3068 /* try to attach call, if authentication is complete */
3070 TryAttach(register struct rx_call *acall, register osi_socket socket,
3071 register int *tnop, register struct rx_call **newcallp,
3074 struct rx_connection *conn = acall->conn;
3076 if (conn->type == RX_SERVER_CONNECTION
3077 && acall->state == RX_STATE_PRECALL) {
3078 /* Don't attach until we have any req'd. authentication. */
3079 if (RXS_CheckAuthentication(conn->securityObject, conn) == 0) {
3080 if (reachOverride || rxi_CheckConnReach(conn, acall) == 0)
3081 rxi_AttachServerProc(acall, socket, tnop, newcallp);
3082 /* Note: this does not necessarily succeed; there
3083 * may not any proc available
3086 rxi_ChallengeOn(acall->conn);
3091 /* A data packet has been received off the interface. This packet is
3092 * appropriate to the call (the call is in the right state, etc.). This
3093 * routine can return a packet to the caller, for re-use */
3096 rxi_ReceiveDataPacket(register struct rx_call *call,
3097 register struct rx_packet *np, int istack,
3098 osi_socket socket, afs_uint32 host, u_short port,
3099 int *tnop, struct rx_call **newcallp)
3101 int ackNeeded = 0; /* 0 means no, otherwise ack_reason */
3105 afs_uint32 seq, serial, flags;
3107 struct rx_packet *tnp;
3109 MUTEX_ENTER(&rx_stats_mutex);
3110 rx_stats.dataPacketsRead++;
3111 MUTEX_EXIT(&rx_stats_mutex);
3114 /* If there are no packet buffers, drop this new packet, unless we can find
3115 * packet buffers from inactive calls */
3117 && (rxi_OverQuota(RX_PACKET_CLASS_RECEIVE) || TooLow(np, call))) {
3118 MUTEX_ENTER(&rx_freePktQ_lock);
3119 rxi_NeedMorePackets = TRUE;
3120 MUTEX_EXIT(&rx_freePktQ_lock);
3121 MUTEX_ENTER(&rx_stats_mutex);
3122 rx_stats.noPacketBuffersOnRead++;
3123 MUTEX_EXIT(&rx_stats_mutex);
3124 call->rprev = np->header.serial;
3125 rxi_calltrace(RX_TRACE_DROP, call);
3126 dpf(("packet %x dropped on receipt - quota problems", np));
3128 rxi_ClearReceiveQueue(call);
3129 clock_GetTime(&when);
3130 clock_Add(&when, &rx_softAckDelay);
3131 if (!call->delayedAckEvent
3132 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3133 rxevent_Cancel(call->delayedAckEvent, call,
3134 RX_CALL_REFCOUNT_DELAY);
3135 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3136 call->delayedAckEvent =
3137 rxevent_Post(&when, rxi_SendDelayedAck, call, 0);
3139 /* we've damaged this call already, might as well do it in. */
3145 * New in AFS 3.5, if the RX_JUMBO_PACKET flag is set then this
3146 * packet is one of several packets transmitted as a single
3147 * datagram. Do not send any soft or hard acks until all packets
3148 * in a jumbogram have been processed. Send negative acks right away.
3150 for (isFirst = 1, tnp = NULL; isFirst || tnp; isFirst = 0) {
3151 /* tnp is non-null when there are more packets in the
3152 * current jumbo gram */
3159 seq = np->header.seq;
3160 serial = np->header.serial;
3161 flags = np->header.flags;
3163 /* If the call is in an error state, send an abort message */
3165 return rxi_SendCallAbort(call, np, istack, 0);
3167 /* The RX_JUMBO_PACKET is set in all but the last packet in each
3168 * AFS 3.5 jumbogram. */
3169 if (flags & RX_JUMBO_PACKET) {
3170 tnp = rxi_SplitJumboPacket(np, host, port, isFirst);
3175 if (np->header.spare != 0) {
3176 MUTEX_ENTER(&call->conn->conn_data_lock);
3177 call->conn->flags |= RX_CONN_USING_PACKET_CKSUM;
3178 MUTEX_EXIT(&call->conn->conn_data_lock);
3181 /* The usual case is that this is the expected next packet */
3182 if (seq == call->rnext) {
3184 /* Check to make sure it is not a duplicate of one already queued */
3185 if (queue_IsNotEmpty(&call->rq)
3186 && queue_First(&call->rq, rx_packet)->header.seq == seq) {
3187 MUTEX_ENTER(&rx_stats_mutex);
3188 rx_stats.dupPacketsRead++;
3189 MUTEX_EXIT(&rx_stats_mutex);
3190 dpf(("packet %x dropped on receipt - duplicate", np));
3191 rxevent_Cancel(call->delayedAckEvent, call,
3192 RX_CALL_REFCOUNT_DELAY);
3193 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3199 /* It's the next packet. Stick it on the receive queue
3200 * for this call. Set newPackets to make sure we wake
3201 * the reader once all packets have been processed */
3202 queue_Prepend(&call->rq, np);
3204 np = NULL; /* We can't use this anymore */
3207 /* If an ack is requested then set a flag to make sure we
3208 * send an acknowledgement for this packet */
3209 if (flags & RX_REQUEST_ACK) {
3210 ackNeeded = RX_ACK_REQUESTED;
3213 /* Keep track of whether we have received the last packet */
3214 if (flags & RX_LAST_PACKET) {
3215 call->flags |= RX_CALL_HAVE_LAST;
3219 /* Check whether we have all of the packets for this call */
3220 if (call->flags & RX_CALL_HAVE_LAST) {
3221 afs_uint32 tseq; /* temporary sequence number */
3222 struct rx_packet *tp; /* Temporary packet pointer */
3223 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3225 for (tseq = seq, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3226 if (tseq != tp->header.seq)
3228 if (tp->header.flags & RX_LAST_PACKET) {
3229 call->flags |= RX_CALL_RECEIVE_DONE;
3236 /* Provide asynchronous notification for those who want it
3237 * (e.g. multi rx) */
3238 if (call->arrivalProc) {
3239 (*call->arrivalProc) (call, call->arrivalProcHandle,
3240 call->arrivalProcArg);
3241 call->arrivalProc = (void (*)())0;
3244 /* Update last packet received */
3247 /* If there is no server process serving this call, grab
3248 * one, if available. We only need to do this once. If a
3249 * server thread is available, this thread becomes a server
3250 * thread and the server thread becomes a listener thread. */
3252 TryAttach(call, socket, tnop, newcallp, 0);
3255 /* This is not the expected next packet. */
3257 /* Determine whether this is a new or old packet, and if it's
3258 * a new one, whether it fits into the current receive window.
3259 * Also figure out whether the packet was delivered in sequence.
3260 * We use the prev variable to determine whether the new packet
3261 * is the successor of its immediate predecessor in the
3262 * receive queue, and the missing flag to determine whether
3263 * any of this packets predecessors are missing. */
3265 afs_uint32 prev; /* "Previous packet" sequence number */
3266 struct rx_packet *tp; /* Temporary packet pointer */
3267 struct rx_packet *nxp; /* Next pointer, for queue_Scan */
3268 int missing; /* Are any predecessors missing? */
3270 /* If the new packet's sequence number has been sent to the
3271 * application already, then this is a duplicate */
3272 if (seq < call->rnext) {
3273 MUTEX_ENTER(&rx_stats_mutex);
3274 rx_stats.dupPacketsRead++;
3275 MUTEX_EXIT(&rx_stats_mutex);
3276 rxevent_Cancel(call->delayedAckEvent, call,
3277 RX_CALL_REFCOUNT_DELAY);
3278 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
3284 /* If the sequence number is greater than what can be
3285 * accomodated by the current window, then send a negative
3286 * acknowledge and drop the packet */
3287 if ((call->rnext + call->rwind) <= seq) {
3288 rxevent_Cancel(call->delayedAckEvent, call,
3289 RX_CALL_REFCOUNT_DELAY);
3290 np = rxi_SendAck(call, np, serial, RX_ACK_EXCEEDS_WINDOW,
3297 /* Look for the packet in the queue of old received packets */
3298 for (prev = call->rnext - 1, missing =
3299 0, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3300 /*Check for duplicate packet */
3301 if (seq == tp->header.seq) {
3302 MUTEX_ENTER(&rx_stats_mutex);
3303 rx_stats.dupPacketsRead++;
3304 MUTEX_EXIT(&rx_stats_mutex);
3305 rxevent_Cancel(call->delayedAckEvent, call,
3306 RX_CALL_REFCOUNT_DELAY);
3307 np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE,
3313 /* If we find a higher sequence packet, break out and
3314 * insert the new packet here. */
3315 if (seq < tp->header.seq)
3317 /* Check for missing packet */
3318 if (tp->header.seq != prev + 1) {
3322 prev = tp->header.seq;
3325 /* Keep track of whether we have received the last packet. */
3326 if (flags & RX_LAST_PACKET) {
3327 call->flags |= RX_CALL_HAVE_LAST;
3330 /* It's within the window: add it to the the receive queue.
3331 * tp is left by the previous loop either pointing at the
3332 * packet before which to insert the new packet, or at the
3333 * queue head if the queue is empty or the packet should be
3335 queue_InsertBefore(tp, np);
3339 /* Check whether we have all of the packets for this call */
3340 if ((call->flags & RX_CALL_HAVE_LAST)
3341 && !(call->flags & RX_CALL_RECEIVE_DONE)) {
3342 afs_uint32 tseq; /* temporary sequence number */
3345 call->rnext, queue_Scan(&call->rq, tp, nxp, rx_packet)) {
3346 if (tseq != tp->header.seq)
3348 if (tp->header.flags & RX_LAST_PACKET) {
3349 call->flags |= RX_CALL_RECEIVE_DONE;
3356 /* We need to send an ack of the packet is out of sequence,
3357 * or if an ack was requested by the peer. */
3358 if (seq != prev + 1 || missing) {
3359 ackNeeded = RX_ACK_OUT_OF_SEQUENCE;
3360 } else if (flags & RX_REQUEST_ACK) {
3361 ackNeeded = RX_ACK_REQUESTED;
3364 /* Acknowledge the last packet for each call */
3365 if (flags & RX_LAST_PACKET) {
3376 * If the receiver is waiting for an iovec, fill the iovec
3377 * using the data from the receive queue */
3378 if (call->flags & RX_CALL_IOVEC_WAIT) {
3379 didHardAck = rxi_FillReadVec(call, serial);
3380 /* the call may have been aborted */
3389 /* Wakeup the reader if any */
3390 if ((call->flags & RX_CALL_READER_WAIT)
3391 && (!(call->flags & RX_CALL_IOVEC_WAIT) || !(call->iovNBytes)
3392 || (call->iovNext >= call->iovMax)
3393 || (call->flags & RX_CALL_RECEIVE_DONE))) {
3394 call->flags &= ~RX_CALL_READER_WAIT;
3395 #ifdef RX_ENABLE_LOCKS
3396 CV_BROADCAST(&call->cv_rq);
3398 osi_rxWakeup(&call->rq);
3404 * Send an ack when requested by the peer, or once every
3405 * rxi_SoftAckRate packets until the last packet has been
3406 * received. Always send a soft ack for the last packet in
3407 * the server's reply. */
3409 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3410 np = rxi_SendAck(call, np, serial, ackNeeded, istack);
3411 } else if (call->nSoftAcks > (u_short) rxi_SoftAckRate) {
3412 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3413 np = rxi_SendAck(call, np, serial, RX_ACK_IDLE, istack);
3414 } else if (call->nSoftAcks) {
3415 clock_GetTime(&when);
3416 if (haveLast && !(flags & RX_CLIENT_INITIATED)) {
3417 clock_Add(&when, &rx_lastAckDelay);
3419 clock_Add(&when, &rx_softAckDelay);
3421 if (!call->delayedAckEvent
3422 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
3423 rxevent_Cancel(call->delayedAckEvent, call,
3424 RX_CALL_REFCOUNT_DELAY);
3425 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
3426 call->delayedAckEvent =
3427 rxevent_Post(&when, rxi_SendDelayedAck, call, 0);
3429 } else if (call->flags & RX_CALL_RECEIVE_DONE) {
3430 rxevent_Cancel(call->delayedAckEvent, call, RX_CALL_REFCOUNT_DELAY);
3437 static void rxi_ComputeRate();
3441 rxi_UpdatePeerReach(struct rx_connection *conn, struct rx_call *acall)
3443 struct rx_peer *peer = conn->peer;
3445 MUTEX_ENTER(&peer->peer_lock);
3446 peer->lastReachTime = clock_Sec();
3447 MUTEX_EXIT(&peer->peer_lock);
3449 MUTEX_ENTER(&conn->conn_data_lock);
3450 if (conn->flags & RX_CONN_ATTACHWAIT) {
3453 conn->flags &= ~RX_CONN_ATTACHWAIT;
3454 MUTEX_EXIT(&conn->conn_data_lock);
3456 for (i = 0; i < RX_MAXCALLS; i++) {
3457 struct rx_call *call = conn->call[i];
3460 MUTEX_ENTER(&call->lock);
3461 /* tnop can be null if newcallp is null */
3462 TryAttach(call, (osi_socket) - 1, NULL, NULL, 1);
3464 MUTEX_EXIT(&call->lock);
3468 MUTEX_EXIT(&conn->conn_data_lock);
3472 rx_ack_reason(int reason)
3475 case RX_ACK_REQUESTED:
3477 case RX_ACK_DUPLICATE:
3479 case RX_ACK_OUT_OF_SEQUENCE:
3481 case RX_ACK_EXCEEDS_WINDOW:
3483 case RX_ACK_NOSPACE:
3487 case RX_ACK_PING_RESPONSE:
3499 /* rxi_ComputePeerNetStats
3501 * Called exclusively by rxi_ReceiveAckPacket to compute network link
3502 * estimates (like RTT and throughput) based on ack packets. Caller
3503 * must ensure that the packet in question is the right one (i.e.
3504 * serial number matches).
3507 rxi_ComputePeerNetStats(struct rx_call *call, struct rx_packet *p,
3508 struct rx_ackPacket *ap, struct rx_packet *np)
3510 struct rx_peer *peer = call->conn->peer;
3512 /* Use RTT if not delayed by client. */
3513 if (ap->reason != RX_ACK_DELAY)
3514 rxi_ComputeRoundTripTime(p, &p->timeSent, peer);
3516 rxi_ComputeRate(peer, call, p, np, ap->reason);
3520 /* The real smarts of the whole thing. */
3522 rxi_ReceiveAckPacket(register struct rx_call *call, struct rx_packet *np,
3525 struct rx_ackPacket *ap;
3527 register struct rx_packet *tp;
3528 register struct rx_packet *nxp; /* Next packet pointer for queue_Scan */
3529 register struct rx_connection *conn = call->conn;
3530 struct rx_peer *peer = conn->peer;
3533 /* because there are CM's that are bogus, sending weird values for this. */
3534 afs_uint32 skew = 0;
3539 int newAckCount = 0;
3540 u_short maxMTU = 0; /* Set if peer supports AFS 3.4a jumbo datagrams */
3541 int maxDgramPackets = 0; /* Set if peer supports AFS 3.5 jumbo datagrams */
3543 MUTEX_ENTER(&rx_stats_mutex);
3544 rx_stats.ackPacketsRead++;
3545 MUTEX_EXIT(&rx_stats_mutex);
3546 ap = (struct rx_ackPacket *)rx_DataOf(np);
3547 nbytes = rx_Contiguous(np) - (int)((ap->acks) - (u_char *) ap);
3549 return np; /* truncated ack packet */
3551 /* depends on ack packet struct */
3552 nAcks = MIN((unsigned)nbytes, (unsigned)ap->nAcks);
3553 first = ntohl(ap->firstPacket);
3554 serial = ntohl(ap->serial);
3555 /* temporarily disabled -- needs to degrade over time
3556 * skew = ntohs(ap->maxSkew); */
3558 /* Ignore ack packets received out of order */
3559 if (first < call->tfirst) {
3563 if (np->header.flags & RX_SLOW_START_OK) {
3564 call->flags |= RX_CALL_SLOW_START_OK;
3567 if (ap->reason == RX_ACK_PING_RESPONSE)
3568 rxi_UpdatePeerReach(conn, call);
3572 if (rxdebug_active) {
3576 len = _snprintf(msg, sizeof(msg),
3577 "tid[%d] RACK: reason %s serial %u previous %u seq %u skew %d first %u acks %u space %u ",
3578 GetCurrentThreadId(), rx_ack_reason(ap->reason),
3579 ntohl(ap->serial), ntohl(ap->previousPacket),
3580 (unsigned int)np->header.seq, (unsigned int)skew,
3581 ntohl(ap->firstPacket), ap->nAcks, ntohs(ap->bufferSpace) );
3585 for (offset = 0; offset < nAcks && len < sizeof(msg); offset++)
3586 msg[len++] = (ap->acks[offset] == RX_ACK_TYPE_NACK ? '-' : '*');
3590 OutputDebugString(msg);
3592 #else /* AFS_NT40_ENV */
3595 "RACK: reason %x previous %u seq %u serial %u skew %d first %u",
3596 ap->reason, ntohl(ap->previousPacket),
3597 (unsigned int)np->header.seq, (unsigned int)serial,
3598 (unsigned int)skew, ntohl(ap->firstPacket));
3601 for (offset = 0; offset < nAcks; offset++)
3602 putc(ap->acks[offset] == RX_ACK_TYPE_NACK ? '-' : '*',
3607 #endif /* AFS_NT40_ENV */
3610 /* Update the outgoing packet skew value to the latest value of
3611 * the peer's incoming packet skew value. The ack packet, of
3612 * course, could arrive out of order, but that won't affect things
3614 MUTEX_ENTER(&peer->peer_lock);
3615 peer->outPacketSkew = skew;
3617 /* Check for packets that no longer need to be transmitted, and
3618 * discard them. This only applies to packets positively
3619 * acknowledged as having been sent to the peer's upper level.
3620 * All other packets must be retained. So only packets with
3621 * sequence numbers < ap->firstPacket are candidates. */
3622 for (queue_Scan(&call->tq, tp, nxp, rx_packet)) {
3623 if (tp->header.seq >= first)
3625 call->tfirst = tp->header.seq + 1;
3627 && (tp->header.serial == serial || tp->firstSerial == serial))
3628 rxi_ComputePeerNetStats(call, tp, ap, np);
3629 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
3632 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3633 /* XXX Hack. Because we have to release the global rx lock when sending
3634 * packets (osi_NetSend) we drop all acks while we're traversing the tq
3635 * in rxi_Start sending packets out because packets may move to the
3636 * freePacketQueue as result of being here! So we drop these packets until
3637 * we're safely out of the traversing. Really ugly!
3638 * To make it even uglier, if we're using fine grain locking, we can
3639 * set the ack bits in the packets and have rxi_Start remove the packets
3640 * when it's done transmitting.
3642 if (call->flags & RX_CALL_TQ_BUSY) {
3643 #ifdef RX_ENABLE_LOCKS
3644 tp->flags |= RX_PKTFLAG_ACKED;
3645 call->flags |= RX_CALL_TQ_SOME_ACKED;
3646 #else /* RX_ENABLE_LOCKS */
3648 #endif /* RX_ENABLE_LOCKS */
3650 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3653 rxi_FreePacket(tp); /* rxi_FreePacket mustn't wake up anyone, preemptively. */
3658 /* Give rate detector a chance to respond to ping requests */
3659 if (ap->reason == RX_ACK_PING_RESPONSE) {
3660 rxi_ComputeRate(peer, call, 0, np, ap->reason);
3664 /* N.B. we don't turn off any timers here. They'll go away by themselves, anyway */
3666 /* Now go through explicit acks/nacks and record the results in
3667 * the waiting packets. These are packets that can't be released
3668 * yet, even with a positive acknowledge. This positive
3669 * acknowledge only means the packet has been received by the
3670 * peer, not that it will be retained long enough to be sent to
3671 * the peer's upper level. In addition, reset the transmit timers
3672 * of any missing packets (those packets that must be missing
3673 * because this packet was out of sequence) */
3675 call->nSoftAcked = 0;
3676 for (missing = 0, queue_Scan(&call->tq, tp, nxp, rx_packet)) {
3677 /* Update round trip time if the ack was stimulated on receipt
3679 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3680 #ifdef RX_ENABLE_LOCKS
3681 if (tp->header.seq >= first)
3682 #endif /* RX_ENABLE_LOCKS */
3683 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3685 && (tp->header.serial == serial || tp->firstSerial == serial))
3686 rxi_ComputePeerNetStats(call, tp, ap, np);
3688 /* Set the acknowledge flag per packet based on the
3689 * information in the ack packet. An acknowlegded packet can
3690 * be downgraded when the server has discarded a packet it
3691 * soacked previously, or when an ack packet is received
3692 * out of sequence. */
3693 if (tp->header.seq < first) {
3694 /* Implicit ack information */
3695 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
3698 tp->flags |= RX_PKTFLAG_ACKED;
3699 } else if (tp->header.seq < first + nAcks) {
3700 /* Explicit ack information: set it in the packet appropriately */
3701 if (ap->acks[tp->header.seq - first] == RX_ACK_TYPE_ACK) {
3702 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
3704 tp->flags |= RX_PKTFLAG_ACKED;
3711 } else /* RX_ACK_TYPE_NACK */ {
3712 tp->flags &= ~RX_PKTFLAG_ACKED;
3716 tp->flags &= ~RX_PKTFLAG_ACKED;
3720 /* If packet isn't yet acked, and it has been transmitted at least
3721 * once, reset retransmit time using latest timeout
3722 * ie, this should readjust the retransmit timer for all outstanding
3723 * packets... So we don't just retransmit when we should know better*/
3725 if (!(tp->flags & RX_PKTFLAG_ACKED) && !clock_IsZero(&tp->retryTime)) {
3726 tp->retryTime = tp->timeSent;
3727 clock_Add(&tp->retryTime, &peer->timeout);
3728 /* shift by eight because one quarter-sec ~ 256 milliseconds */
3729 clock_Addmsec(&(tp->retryTime), ((afs_uint32) tp->backoff) << 8);
3733 /* If the window has been extended by this acknowledge packet,
3734 * then wakeup a sender waiting in alloc for window space, or try
3735 * sending packets now, if he's been sitting on packets due to
3736 * lack of window space */
3737 if (call->tnext < (call->tfirst + call->twind)) {
3738 #ifdef RX_ENABLE_LOCKS
3739 CV_SIGNAL(&call->cv_twind);
3741 if (call->flags & RX_CALL_WAIT_WINDOW_ALLOC) {
3742 call->flags &= ~RX_CALL_WAIT_WINDOW_ALLOC;
3743 osi_rxWakeup(&call->twind);
3746 if (call->flags & RX_CALL_WAIT_WINDOW_SEND) {
3747 call->flags &= ~RX_CALL_WAIT_WINDOW_SEND;
3751 /* if the ack packet has a receivelen field hanging off it,
3752 * update our state */
3753 if (np->length >= rx_AckDataSize(ap->nAcks) + 2 * sizeof(afs_int32)) {
3756 /* If the ack packet has a "recommended" size that is less than
3757 * what I am using now, reduce my size to match */
3758 rx_packetread(np, rx_AckDataSize(ap->nAcks) + sizeof(afs_int32),
3759 (int)sizeof(afs_int32), &tSize);
3760 tSize = (afs_uint32) ntohl(tSize);
3761 peer->natMTU = rxi_AdjustIfMTU(MIN(tSize, peer->ifMTU));
3763 /* Get the maximum packet size to send to this peer */
3764 rx_packetread(np, rx_AckDataSize(ap->nAcks), (int)sizeof(afs_int32),
3766 tSize = (afs_uint32) ntohl(tSize);
3767 tSize = (afs_uint32) MIN(tSize, rx_MyMaxSendSize);
3768 tSize = rxi_AdjustMaxMTU(peer->natMTU, tSize);
3770 /* sanity check - peer might have restarted with different params.
3771 * If peer says "send less", dammit, send less... Peer should never
3772 * be unable to accept packets of the size that prior AFS versions would
3773 * send without asking. */
3774 if (peer->maxMTU != tSize) {
3775 if (peer->maxMTU > tSize) /* possible cong., maxMTU decreased */
3777 peer->maxMTU = tSize;
3778 peer->MTU = MIN(tSize, peer->MTU);
3779 call->MTU = MIN(call->MTU, tSize);
3782 if (np->length == rx_AckDataSize(ap->nAcks) + 3 * sizeof(afs_int32)) {
3785 rx_AckDataSize(ap->nAcks) + 2 * sizeof(afs_int32),
3786 (int)sizeof(afs_int32), &tSize);
3787 tSize = (afs_uint32) ntohl(tSize); /* peer's receive window, if it's */
3788 if (tSize < call->twind) { /* smaller than our send */
3789 call->twind = tSize; /* window, we must send less... */
3790 call->ssthresh = MIN(call->twind, call->ssthresh);
3793 /* Only send jumbograms to 3.4a fileservers. 3.3a RX gets the
3794 * network MTU confused with the loopback MTU. Calculate the
3795 * maximum MTU here for use in the slow start code below.
3797 maxMTU = peer->maxMTU;
3798 /* Did peer restart with older RX version? */
3799 if (peer->maxDgramPackets > 1) {
3800 peer->maxDgramPackets = 1;
3802 } else if (np->length >=
3803 rx_AckDataSize(ap->nAcks) + 4 * sizeof(afs_int32)) {
3806 rx_AckDataSize(ap->nAcks) + 2 * sizeof(afs_int32),
3807 sizeof(afs_int32), &tSize);
3808 tSize = (afs_uint32) ntohl(tSize);
3810 * As of AFS 3.5 we set the send window to match the receive window.
3812 if (tSize < call->twind) {
3813 call->twind = tSize;
3814 call->ssthresh = MIN(call->twind, call->ssthresh);
3815 } else if (tSize > call->twind) {
3816 call->twind = tSize;
3820 * As of AFS 3.5, a jumbogram is more than one fixed size
3821 * packet transmitted in a single UDP datagram. If the remote
3822 * MTU is smaller than our local MTU then never send a datagram
3823 * larger than the natural MTU.
3826 rx_AckDataSize(ap->nAcks) + 3 * sizeof(afs_int32),
3827 sizeof(afs_int32), &tSize);
3828 maxDgramPackets = (afs_uint32) ntohl(tSize);
3829 maxDgramPackets = MIN(maxDgramPackets, rxi_nDgramPackets);
3830 maxDgramPackets = MIN(maxDgramPackets, peer->ifDgramPackets);
3831 if (peer->natMTU < peer->ifMTU)
3832 maxDgramPackets = MIN(maxDgramPackets, rxi_AdjustDgramPackets(1, peer->natMTU));
3833 if (maxDgramPackets > 1) {
3834 peer->maxDgramPackets = maxDgramPackets;
3835 call->MTU = RX_JUMBOBUFFERSIZE + RX_HEADER_SIZE;
3837 peer->maxDgramPackets = 1;
3838 call->MTU = peer->natMTU;
3840 } else if (peer->maxDgramPackets > 1) {
3841 /* Restarted with lower version of RX */
3842 peer->maxDgramPackets = 1;
3844 } else if (peer->maxDgramPackets > 1
3845 || peer->maxMTU != OLD_MAX_PACKET_SIZE) {
3846 /* Restarted with lower version of RX */
3847 peer->maxMTU = OLD_MAX_PACKET_SIZE;
3848 peer->natMTU = OLD_MAX_PACKET_SIZE;
3849 peer->MTU = OLD_MAX_PACKET_SIZE;
3850 peer->maxDgramPackets = 1;
3851 peer->nDgramPackets = 1;
3853 call->MTU = OLD_MAX_PACKET_SIZE;
3858 * Calculate how many datagrams were successfully received after
3859 * the first missing packet and adjust the negative ack counter
3864 nNacked = (nNacked + call->nDgramPackets - 1) / call->nDgramPackets;
3865 if (call->nNacks < nNacked) {
3866 call->nNacks = nNacked;
3875 if (call->flags & RX_CALL_FAST_RECOVER) {
3877 call->cwind = MIN((int)(call->cwind + 1), rx_maxSendWindow);
3879 call->flags &= ~RX_CALL_FAST_RECOVER;
3880 call->cwind = call->nextCwind;
3881 call->nextCwind = 0;
3884 call->nCwindAcks = 0;
3885 } else if (nNacked && call->nNacks >= (u_short) rx_nackThreshold) {
3886 /* Three negative acks in a row trigger congestion recovery */
3887 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3888 MUTEX_EXIT(&peer->peer_lock);
3889 if (call->flags & RX_CALL_FAST_RECOVER_WAIT) {
3890 /* someone else is waiting to start recovery */
3893 call->flags |= RX_CALL_FAST_RECOVER_WAIT;
3894 while (call->flags & RX_CALL_TQ_BUSY) {
3895 call->flags |= RX_CALL_TQ_WAIT;
3897 #ifdef RX_ENABLE_LOCKS
3898 osirx_AssertMine(&call->lock, "rxi_Start lock2");
3899 CV_WAIT(&call->cv_tq, &call->lock);
3900 #else /* RX_ENABLE_LOCKS */
3901 osi_rxSleep(&call->tq);
3902 #endif /* RX_ENABLE_LOCKS */
3904 if (call->tqWaiters == 0)
3905 call->flags &= ~RX_CALL_TQ_WAIT;
3907 MUTEX_ENTER(&peer->peer_lock);
3908 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3909 call->flags &= ~RX_CALL_FAST_RECOVER_WAIT;
3910 call->flags |= RX_CALL_FAST_RECOVER;
3911 call->ssthresh = MAX(4, MIN((int)call->cwind, (int)call->twind)) >> 1;
3913 MIN((int)(call->ssthresh + rx_nackThreshold), rx_maxSendWindow);
3914 call->nDgramPackets = MAX(2, (int)call->nDgramPackets) >> 1;
3915 call->nextCwind = call->ssthresh;
3918 peer->MTU = call->MTU;
3919 peer->cwind = call->nextCwind;
3920 peer->nDgramPackets = call->nDgramPackets;
3922 call->congestSeq = peer->congestSeq;
3923 /* Reset the resend times on the packets that were nacked
3924 * so we will retransmit as soon as the window permits*/
3925 for (acked = 0, queue_ScanBackwards(&call->tq, tp, nxp, rx_packet)) {
3927 if (!(tp->flags & RX_PKTFLAG_ACKED)) {
3928 clock_Zero(&tp->retryTime);
3930 } else if (tp->flags & RX_PKTFLAG_ACKED) {
3935 /* If cwind is smaller than ssthresh, then increase
3936 * the window one packet for each ack we receive (exponential
3938 * If cwind is greater than or equal to ssthresh then increase
3939 * the congestion window by one packet for each cwind acks we
3940 * receive (linear growth). */
3941 if (call->cwind < call->ssthresh) {
3943 MIN((int)call->ssthresh, (int)(call->cwind + newAckCount));
3944 call->nCwindAcks = 0;
3946 call->nCwindAcks += newAckCount;
3947 if (call->nCwindAcks >= call->cwind) {
3948 call->nCwindAcks = 0;
3949 call->cwind = MIN((int)(call->cwind + 1), rx_maxSendWindow);
3953 * If we have received several acknowledgements in a row then
3954 * it is time to increase the size of our datagrams
3956 if ((int)call->nAcks > rx_nDgramThreshold) {
3957 if (peer->maxDgramPackets > 1) {
3958 if (call->nDgramPackets < peer->maxDgramPackets) {
3959 call->nDgramPackets++;
3961 call->MTU = RX_HEADER_SIZE + RX_JUMBOBUFFERSIZE;
3962 } else if (call->MTU < peer->maxMTU) {
3963 call->MTU += peer->natMTU;
3964 call->MTU = MIN(call->MTU, peer->maxMTU);
3970 MUTEX_EXIT(&peer->peer_lock); /* rxi_Start will lock peer. */
3972 /* Servers need to hold the call until all response packets have
3973 * been acknowledged. Soft acks are good enough since clients
3974 * are not allowed to clear their receive queues. */
3975 if (call->state == RX_STATE_HOLD
3976 && call->tfirst + call->nSoftAcked >= call->tnext) {
3977 call->state = RX_STATE_DALLY;
3978 rxi_ClearTransmitQueue(call, 0);
3979 } else if (!queue_IsEmpty(&call->tq)) {
3980 rxi_Start(0, call, 0, istack);
3985 /* Received a response to a challenge packet */
3987 rxi_ReceiveResponsePacket(register struct rx_connection *conn,
3988 register struct rx_packet *np, int istack)
3992 /* Ignore the packet if we're the client */
3993 if (conn->type == RX_CLIENT_CONNECTION)
3996 /* If already authenticated, ignore the packet (it's probably a retry) */
3997 if (RXS_CheckAuthentication(conn->securityObject, conn) == 0)
4000 /* Otherwise, have the security object evaluate the response packet */
4001 error = RXS_CheckResponse(conn->securityObject, conn, np);
4003 /* If the response is invalid, reset the connection, sending
4004 * an abort to the peer */
4008 rxi_ConnectionError(conn, error);
4009 MUTEX_ENTER(&conn->conn_data_lock);
4010 np = rxi_SendConnectionAbort(conn, np, istack, 0);
4011 MUTEX_EXIT(&conn->conn_data_lock);
4014 /* If the response is valid, any calls waiting to attach
4015 * servers can now do so */
4018 for (i = 0; i < RX_MAXCALLS; i++) {
4019 struct rx_call *call = conn->call[i];
4021 MUTEX_ENTER(&call->lock);
4022 if (call->state == RX_STATE_PRECALL)
4023 rxi_AttachServerProc(call, (osi_socket) - 1, NULL, NULL);
4024 /* tnop can be null if newcallp is null */
4025 MUTEX_EXIT(&call->lock);
4029 /* Update the peer reachability information, just in case
4030 * some calls went into attach-wait while we were waiting
4031 * for authentication..
4033 rxi_UpdatePeerReach(conn, NULL);
4038 /* A client has received an authentication challenge: the security
4039 * object is asked to cough up a respectable response packet to send
4040 * back to the server. The server is responsible for retrying the
4041 * challenge if it fails to get a response. */
4044 rxi_ReceiveChallengePacket(register struct rx_connection *conn,
4045 register struct rx_packet *np, int istack)
4049 /* Ignore the challenge if we're the server */
4050 if (conn->type == RX_SERVER_CONNECTION)
4053 /* Ignore the challenge if the connection is otherwise idle; someone's
4054 * trying to use us as an oracle. */
4055 if (!rxi_HasActiveCalls(conn))
4058 /* Send the security object the challenge packet. It is expected to fill
4059 * in the response. */
4060 error = RXS_GetResponse(conn->securityObject, conn, np);
4062 /* If the security object is unable to return a valid response, reset the
4063 * connection and send an abort to the peer. Otherwise send the response
4064 * packet to the peer connection. */
4066 rxi_ConnectionError(conn, error);
4067 MUTEX_ENTER(&conn->conn_data_lock);
4068 np = rxi_SendConnectionAbort(conn, np, istack, 0);
4069 MUTEX_EXIT(&conn->conn_data_lock);
4071 np = rxi_SendSpecial((struct rx_call *)0, conn, np,
4072 RX_PACKET_TYPE_RESPONSE, NULL, -1, istack);
4078 /* Find an available server process to service the current request in
4079 * the given call structure. If one isn't available, queue up this
4080 * call so it eventually gets one */
4082 rxi_AttachServerProc(register struct rx_call *call,
4083 register osi_socket socket, register int *tnop,
4084 register struct rx_call **newcallp)
4086 register struct rx_serverQueueEntry *sq;
4087 register struct rx_service *service = call->conn->service;
4088 register int haveQuota = 0;
4090 /* May already be attached */
4091 if (call->state == RX_STATE_ACTIVE)
4094 MUTEX_ENTER(&rx_serverPool_lock);
4096 haveQuota = QuotaOK(service);
4097 if ((!haveQuota) || queue_IsEmpty(&rx_idleServerQueue)) {
4098 /* If there are no processes available to service this call,
4099 * put the call on the incoming call queue (unless it's
4100 * already on the queue).
4102 #ifdef RX_ENABLE_LOCKS
4104 ReturnToServerPool(service);
4105 #endif /* RX_ENABLE_LOCKS */
4107 if (!(call->flags & RX_CALL_WAIT_PROC)) {
4108 call->flags |= RX_CALL_WAIT_PROC;
4109 MUTEX_ENTER(&rx_stats_mutex);
4112 MUTEX_EXIT(&rx_stats_mutex);
4113 rxi_calltrace(RX_CALL_ARRIVAL, call);
4114 SET_CALL_QUEUE_LOCK(call, &rx_serverPool_lock);
4115 queue_Append(&rx_incomingCallQueue, call);
4118 sq = queue_First(&rx_idleServerQueue, rx_serverQueueEntry);
4120 /* If hot threads are enabled, and both newcallp and sq->socketp
4121 * are non-null, then this thread will process the call, and the
4122 * idle server thread will start listening on this threads socket.
4125 if (rx_enable_hot_thread && newcallp && sq->socketp) {
4128 *sq->socketp = socket;
4129 clock_GetTime(&call->startTime);
4130 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
4134 if (call->flags & RX_CALL_WAIT_PROC) {
4135 /* Conservative: I don't think this should happen */
4136 call->flags &= ~RX_CALL_WAIT_PROC;
4137 if (queue_IsOnQueue(call)) {
4139 MUTEX_ENTER(&rx_stats_mutex);
4141 MUTEX_EXIT(&rx_stats_mutex);
4144 call->state = RX_STATE_ACTIVE;
4145 call->mode = RX_MODE_RECEIVING;
4146 #ifdef RX_KERNEL_TRACE
4148 int glockOwner = ISAFS_GLOCK();
4151 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
4152 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
4158 if (call->flags & RX_CALL_CLEARED) {
4159 /* send an ack now to start the packet flow up again */
4160 call->flags &= ~RX_CALL_CLEARED;
4161 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
4163 #ifdef RX_ENABLE_LOCKS
4166 service->nRequestsRunning++;
4167 if (service->nRequestsRunning <= service->minProcs)
4173 MUTEX_EXIT(&rx_serverPool_lock);
4176 /* Delay the sending of an acknowledge event for a short while, while
4177 * a new call is being prepared (in the case of a client) or a reply
4178 * is being prepared (in the case of a server). Rather than sending
4179 * an ack packet, an ACKALL packet is sent. */
4181 rxi_AckAll(struct rxevent *event, register struct rx_call *call, char *dummy)
4183 #ifdef RX_ENABLE_LOCKS
4185 MUTEX_ENTER(&call->lock);
4186 call->delayedAckEvent = NULL;
4187 CALL_RELE(call, RX_CALL_REFCOUNT_ACKALL);
4189 rxi_SendSpecial(call, call->conn, (struct rx_packet *)0,
4190 RX_PACKET_TYPE_ACKALL, NULL, 0, 0);
4192 MUTEX_EXIT(&call->lock);
4193 #else /* RX_ENABLE_LOCKS */
4195 call->delayedAckEvent = NULL;
4196 rxi_SendSpecial(call, call->conn, (struct rx_packet *)0,
4197 RX_PACKET_TYPE_ACKALL, NULL, 0, 0);
4198 #endif /* RX_ENABLE_LOCKS */
4202 rxi_SendDelayedAck(struct rxevent *event, register struct rx_call *call,
4205 #ifdef RX_ENABLE_LOCKS
4207 MUTEX_ENTER(&call->lock);
4208 if (event == call->delayedAckEvent)
4209 call->delayedAckEvent = NULL;
4210 CALL_RELE(call, RX_CALL_REFCOUNT_DELAY);
4212 (void)rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
4214 MUTEX_EXIT(&call->lock);
4215 #else /* RX_ENABLE_LOCKS */
4217 call->delayedAckEvent = NULL;
4218 (void)rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
4219 #endif /* RX_ENABLE_LOCKS */
4223 #ifdef RX_ENABLE_LOCKS
4224 /* Set ack in all packets in transmit queue. rxi_Start will deal with
4225 * clearing them out.
4228 rxi_SetAcksInTransmitQueue(register struct rx_call *call)
4230 register struct rx_packet *p, *tp;
4233 for (queue_Scan(&call->tq, p, tp, rx_packet)) {
4234 p->flags |= RX_PKTFLAG_ACKED;
4238 call->flags |= RX_CALL_TQ_CLEARME;
4239 call->flags |= RX_CALL_TQ_SOME_ACKED;
4242 rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
4243 rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
4244 call->tfirst = call->tnext;
4245 call->nSoftAcked = 0;
4247 if (call->flags & RX_CALL_FAST_RECOVER) {
4248 call->flags &= ~RX_CALL_FAST_RECOVER;
4249 call->cwind = call->nextCwind;
4250 call->nextCwind = 0;
4253 CV_SIGNAL(&call->cv_twind);
4255 #endif /* RX_ENABLE_LOCKS */
4257 /* Clear out the transmit queue for the current call (all packets have
4258 * been received by peer) */
4260 rxi_ClearTransmitQueue(register struct rx_call *call, register int force)
4262 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
4263 register struct rx_packet *p, *tp;
4265 if (!force && (call->flags & RX_CALL_TQ_BUSY)) {
4267 for (queue_Scan(&call->tq, p, tp, rx_packet)) {
4268 p->flags |= RX_PKTFLAG_ACKED;
4272 call->flags |= RX_CALL_TQ_CLEARME;
4273 call->flags |= RX_CALL_TQ_SOME_ACKED;
4276 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
4277 rxi_FreePackets(0, &call->tq);
4278 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
4279 call->flags &= ~RX_CALL_TQ_CLEARME;
4281 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
4283 rxevent_Cancel(call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
4284 rxevent_Cancel(call->keepAliveEvent, call, RX_CALL_REFCOUNT_ALIVE);
4285 call->tfirst = call->tnext; /* implicitly acknowledge all data already sent */
4286 call->nSoftAcked = 0;
4288 if (call->flags & RX_CALL_FAST_RECOVER) {
4289 call->flags &= ~RX_CALL_FAST_RECOVER;
4290 call->cwind = call->nextCwind;
4292 #ifdef RX_ENABLE_LOCKS
4293 CV_SIGNAL(&call->cv_twind);
4295 osi_rxWakeup(&call->twind);
4300 rxi_ClearReceiveQueue(register struct rx_call *call)
4302 if (queue_IsNotEmpty(&call->rq)) {
4303 rx_packetReclaims += rxi_FreePackets(0, &call->rq);
4304 call->flags &= ~(RX_CALL_RECEIVE_DONE | RX_CALL_HAVE_LAST);
4306 if (call->state == RX_STATE_PRECALL) {
4307 call->flags |= RX_CALL_CLEARED;
4311 /* Send an abort packet for the specified call */
4313 rxi_SendCallAbort(register struct rx_call *call, struct rx_packet *packet,
4314 int istack, int force)
4322 /* Clients should never delay abort messages */
4323 if (rx_IsClientConn(call->conn))
4326 if (call->abortCode != call->error) {
4327 call->abortCode = call->error;
4328 call->abortCount = 0;
4331 if (force || rxi_callAbortThreshhold == 0
4332 || call->abortCount < rxi_callAbortThreshhold) {
4333 if (call->delayedAbortEvent) {
4334 rxevent_Cancel(call->delayedAbortEvent, call,
4335 RX_CALL_REFCOUNT_ABORT);
4337 error = htonl(call->error);
4340 rxi_SendSpecial(call, call->conn, packet, RX_PACKET_TYPE_ABORT,
4341 (char *)&error, sizeof(error), istack);
4342 } else if (!call->delayedAbortEvent) {
4343 clock_GetTime(&when);
4344 clock_Addmsec(&when, rxi_callAbortDelay);
4345 CALL_HOLD(call, RX_CALL_REFCOUNT_ABORT);
4346 call->delayedAbortEvent =
4347 rxevent_Post(&when, rxi_SendDelayedCallAbort, call, 0);
4352 /* Send an abort packet for the specified connection. Packet is an
4353 * optional pointer to a packet that can be used to send the abort.
4354 * Once the number of abort messages reaches the threshhold, an
4355 * event is scheduled to send the abort. Setting the force flag
4356 * overrides sending delayed abort messages.
4358 * NOTE: Called with conn_data_lock held. conn_data_lock is dropped
4359 * to send the abort packet.
4362 rxi_SendConnectionAbort(register struct rx_connection *conn,