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>
13 #include <afs/param.h>
16 # include "afs/sysincludes.h"
17 # include "afsincludes.h"
22 # ifdef AFS_LINUX20_ENV
23 # include "h/socket.h"
25 # include "netinet/in.h"
27 # include "netinet/ip6.h"
28 # include "inet/common.h"
30 # include "inet/ip_ire.h"
32 # include "afs/afs_args.h"
33 # include "afs/afs_osi.h"
34 # ifdef RX_KERNEL_TRACE
35 # include "rx_kcommon.h"
37 # if defined(AFS_AIX_ENV)
41 # undef RXDEBUG /* turn off debugging */
43 # if defined(AFS_SGI_ENV)
44 # include "sys/debug.h"
47 # include "afs/sysincludes.h"
48 # include "afsincludes.h"
49 # endif /* !UKERNEL */
50 # include "afs/lock.h"
51 # include "rx_kmutex.h"
52 # include "rx_kernel.h"
53 # define AFSOP_STOP_RXCALLBACK 210 /* Stop CALLBACK process */
54 # define AFSOP_STOP_AFS 211 /* Stop AFS process */
55 # define AFSOP_STOP_BKG 212 /* Stop BKG process */
56 extern afs_int32 afs_termState;
58 # include "sys/lockl.h"
59 # include "sys/lock_def.h"
60 # endif /* AFS_AIX41_ENV */
61 # include "afs/rxgen_consts.h"
66 # include <afs/afsutil.h>
67 # include <WINNT\afsreg.h>
76 #include "rx_atomic.h"
77 #include "rx_globals.h"
79 #include "rx_internal.h"
85 #include "rx_packet.h"
87 #include <afs/rxgen_consts.h>
90 #ifdef AFS_PTHREAD_ENV
92 int (*registerProgram) (pid_t, char *) = 0;
93 int (*swapNameProgram) (pid_t, const char *, char *) = 0;
96 int (*registerProgram) (PROCESS, char *) = 0;
97 int (*swapNameProgram) (PROCESS, const char *, char *) = 0;
101 /* Local static routines */
102 static void rxi_DestroyConnectionNoLock(struct rx_connection *conn);
103 static void rxi_ComputeRoundTripTime(struct rx_packet *, struct rx_ackPacket *,
104 struct rx_call *, struct rx_peer *,
106 static void rxi_Resend(struct rxevent *event, void *arg0, void *arg1,
108 static void rxi_SendDelayedAck(struct rxevent *event, void *call,
109 void *dummy, int dummy2);
110 static void rxi_SendDelayedCallAbort(struct rxevent *event, void *arg1,
111 void *dummy, int dummy2);
112 static void rxi_SendDelayedConnAbort(struct rxevent *event, void *arg1,
113 void *unused, int unused2);
114 static void rxi_ReapConnections(struct rxevent *unused, void *unused1,
115 void *unused2, int unused3);
117 #ifdef RX_ENABLE_LOCKS
118 static void rxi_SetAcksInTransmitQueue(struct rx_call *call);
121 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
123 rx_atomic_t rxi_start_aborted; /* rxi_start awoke after rxi_Send in error.*/
124 rx_atomic_t rxi_start_in_error;
126 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
128 /* Constant delay time before sending an acknowledge of the last packet
129 * received. This is to avoid sending an extra acknowledge when the
130 * client is about to make another call, anyway, or the server is
133 * The lastAckDelay may not exceeed 400ms without causing peers to
134 * unecessarily timeout.
136 struct clock rx_lastAckDelay = {0, 400000};
138 /* Constant delay time before sending a soft ack when none was requested.
139 * This is to make sure we send soft acks before the sender times out,
140 * Normally we wait and send a hard ack when the receiver consumes the packet
142 * This value has been 100ms in all shipping versions of OpenAFS. Changing it
143 * will require changes to the peer's RTT calculations.
145 struct clock rx_softAckDelay = {0, 100000};
148 * rxi_rpc_peer_stat_cnt counts the total number of peer stat structures
149 * currently allocated within rx. This number is used to allocate the
150 * memory required to return the statistics when queried.
151 * Protected by the rx_rpc_stats mutex.
154 static unsigned int rxi_rpc_peer_stat_cnt;
157 * rxi_rpc_process_stat_cnt counts the total number of local process stat
158 * structures currently allocated within rx. The number is used to allocate
159 * the memory required to return the statistics when queried.
160 * Protected by the rx_rpc_stats mutex.
163 static unsigned int rxi_rpc_process_stat_cnt;
166 * rxi_busyChannelError is a boolean. It indicates whether or not RX_CALL_BUSY
167 * errors should be reported to the application when a call channel appears busy
168 * (inferred from the receipt of RX_PACKET_TYPE_BUSY packets on the channel),
169 * and there are other call channels in the connection that are not busy.
170 * If 0, we do not return errors upon receiving busy packets; we just keep
171 * trying on the same call channel until we hit a timeout.
173 static afs_int32 rxi_busyChannelError = 0;
175 rx_atomic_t rx_nWaiting = RX_ATOMIC_INIT(0);
176 rx_atomic_t rx_nWaited = RX_ATOMIC_INIT(0);
178 #if !defined(offsetof)
179 #include <stddef.h> /* for definition of offsetof() */
182 #ifdef RX_ENABLE_LOCKS
183 afs_kmutex_t rx_atomic_mutex;
186 /* Forward prototypes */
187 static struct rx_call * rxi_NewCall(struct rx_connection *, int);
190 putConnection (struct rx_connection *conn) {
191 MUTEX_ENTER(&rx_refcnt_mutex);
193 MUTEX_EXIT(&rx_refcnt_mutex);
196 #ifdef AFS_PTHREAD_ENV
199 * Use procedural initialization of mutexes/condition variables
203 extern afs_kmutex_t rx_quota_mutex;
204 extern afs_kmutex_t rx_pthread_mutex;
205 extern afs_kmutex_t rx_packets_mutex;
206 extern afs_kmutex_t rx_refcnt_mutex;
207 extern afs_kmutex_t des_init_mutex;
208 extern afs_kmutex_t des_random_mutex;
209 extern afs_kmutex_t rx_clock_mutex;
210 extern afs_kmutex_t rxi_connCacheMutex;
211 extern afs_kmutex_t event_handler_mutex;
212 extern afs_kmutex_t listener_mutex;
213 extern afs_kmutex_t rx_if_init_mutex;
214 extern afs_kmutex_t rx_if_mutex;
216 extern afs_kcondvar_t rx_event_handler_cond;
217 extern afs_kcondvar_t rx_listener_cond;
219 static afs_kmutex_t epoch_mutex;
220 static afs_kmutex_t rx_init_mutex;
221 static afs_kmutex_t rx_debug_mutex;
222 static afs_kmutex_t rx_rpc_stats;
225 rxi_InitPthread(void)
227 MUTEX_INIT(&rx_clock_mutex, "clock", MUTEX_DEFAULT, 0);
228 MUTEX_INIT(&rx_stats_mutex, "stats", MUTEX_DEFAULT, 0);
229 MUTEX_INIT(&rx_atomic_mutex, "atomic", MUTEX_DEFAULT, 0);
230 MUTEX_INIT(&rx_quota_mutex, "quota", MUTEX_DEFAULT, 0);
231 MUTEX_INIT(&rx_pthread_mutex, "pthread", MUTEX_DEFAULT, 0);
232 MUTEX_INIT(&rx_packets_mutex, "packets", MUTEX_DEFAULT, 0);
233 MUTEX_INIT(&rx_refcnt_mutex, "refcnts", MUTEX_DEFAULT, 0);
234 MUTEX_INIT(&epoch_mutex, "epoch", MUTEX_DEFAULT, 0);
235 MUTEX_INIT(&rx_init_mutex, "init", MUTEX_DEFAULT, 0);
236 MUTEX_INIT(&event_handler_mutex, "event handler", MUTEX_DEFAULT, 0);
237 MUTEX_INIT(&rxi_connCacheMutex, "conn cache", MUTEX_DEFAULT, 0);
238 MUTEX_INIT(&listener_mutex, "listener", MUTEX_DEFAULT, 0);
239 MUTEX_INIT(&rx_if_init_mutex, "if init", MUTEX_DEFAULT, 0);
240 MUTEX_INIT(&rx_if_mutex, "if", MUTEX_DEFAULT, 0);
241 MUTEX_INIT(&rx_debug_mutex, "debug", MUTEX_DEFAULT, 0);
243 CV_INIT(&rx_event_handler_cond, "evhand", CV_DEFAULT, 0);
244 CV_INIT(&rx_listener_cond, "rxlisten", CV_DEFAULT, 0);
246 osi_Assert(pthread_key_create(&rx_thread_id_key, NULL) == 0);
247 osi_Assert(pthread_key_create(&rx_ts_info_key, NULL) == 0);
249 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
250 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
251 #ifdef RX_ENABLE_LOCKS
254 #endif /* RX_LOCKS_DB */
255 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
256 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
258 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
260 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
262 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
264 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
265 MUTEX_INIT(&rxi_keyCreate_lock, "rxi_keyCreate_lock", MUTEX_DEFAULT, 0);
266 #endif /* RX_ENABLE_LOCKS */
269 pthread_once_t rx_once_init = PTHREAD_ONCE_INIT;
270 #define INIT_PTHREAD_LOCKS osi_Assert(pthread_once(&rx_once_init, rxi_InitPthread)==0)
272 * The rx_stats_mutex mutex protects the following global variables:
273 * rxi_lowConnRefCount
274 * rxi_lowPeerRefCount
283 * The rx_quota_mutex mutex protects the following global variables:
291 * The rx_freePktQ_lock protects the following global variables:
296 * The rx_packets_mutex mutex protects the following global variables:
304 * The rx_pthread_mutex mutex protects the following global variables:
305 * rxi_fcfs_thread_num
308 #define INIT_PTHREAD_LOCKS
312 /* Variables for handling the minProcs implementation. availProcs gives the
313 * number of threads available in the pool at this moment (not counting dudes
314 * executing right now). totalMin gives the total number of procs required
315 * for handling all minProcs requests. minDeficit is a dynamic variable
316 * tracking the # of procs required to satisfy all of the remaining minProcs
318 * For fine grain locking to work, the quota check and the reservation of
319 * a server thread has to come while rxi_availProcs and rxi_minDeficit
320 * are locked. To this end, the code has been modified under #ifdef
321 * RX_ENABLE_LOCKS so that quota checks and reservation occur at the
322 * same time. A new function, ReturnToServerPool() returns the allocation.
324 * A call can be on several queue's (but only one at a time). When
325 * rxi_ResetCall wants to remove the call from a queue, it has to ensure
326 * that no one else is touching the queue. To this end, we store the address
327 * of the queue lock in the call structure (under the call lock) when we
328 * put the call on a queue, and we clear the call_queue_lock when the
329 * call is removed from a queue (once the call lock has been obtained).
330 * This allows rxi_ResetCall to safely synchronize with others wishing
331 * to manipulate the queue.
334 #if defined(RX_ENABLE_LOCKS)
335 static afs_kmutex_t rx_rpc_stats;
338 /* We keep a "last conn pointer" in rxi_FindConnection. The odds are
339 ** pretty good that the next packet coming in is from the same connection
340 ** as the last packet, since we're send multiple packets in a transmit window.
342 struct rx_connection *rxLastConn = 0;
344 #ifdef RX_ENABLE_LOCKS
345 /* The locking hierarchy for rx fine grain locking is composed of these
348 * rx_connHashTable_lock - synchronizes conn creation, rx_connHashTable access
349 * conn_call_lock - used to synchonize rx_EndCall and rx_NewCall
350 * call->lock - locks call data fields.
351 * These are independent of each other:
352 * rx_freeCallQueue_lock
357 * serverQueueEntry->lock
358 * rx_peerHashTable_lock - locked under rx_connHashTable_lock
360 * peer->lock - locks peer data fields.
361 * conn_data_lock - that more than one thread is not updating a conn data
362 * field at the same time.
373 * Do we need a lock to protect the peer field in the conn structure?
374 * conn->peer was previously a constant for all intents and so has no
375 * lock protecting this field. The multihomed client delta introduced
376 * a RX code change : change the peer field in the connection structure
377 * to that remote interface from which the last packet for this
378 * connection was sent out. This may become an issue if further changes
381 #define SET_CALL_QUEUE_LOCK(C, L) (C)->call_queue_lock = (L)
382 #define CLEAR_CALL_QUEUE_LOCK(C) (C)->call_queue_lock = NULL
384 /* rxdb_fileID is used to identify the lock location, along with line#. */
385 static int rxdb_fileID = RXDB_FILE_RX;
386 #endif /* RX_LOCKS_DB */
387 #else /* RX_ENABLE_LOCKS */
388 #define SET_CALL_QUEUE_LOCK(C, L)
389 #define CLEAR_CALL_QUEUE_LOCK(C)
390 #endif /* RX_ENABLE_LOCKS */
391 struct rx_serverQueueEntry *rx_waitForPacket = 0;
392 struct rx_serverQueueEntry *rx_waitingForPacket = 0;
394 /* ------------Exported Interfaces------------- */
396 /* This function allows rxkad to set the epoch to a suitably random number
397 * which rx_NewConnection will use in the future. The principle purpose is to
398 * get rxnull connections to use the same epoch as the rxkad connections do, at
399 * least once the first rxkad connection is established. This is important now
400 * that the host/port addresses aren't used in FindConnection: the uniqueness
401 * of epoch/cid matters and the start time won't do. */
403 #ifdef AFS_PTHREAD_ENV
405 * This mutex protects the following global variables:
409 #define LOCK_EPOCH MUTEX_ENTER(&epoch_mutex)
410 #define UNLOCK_EPOCH MUTEX_EXIT(&epoch_mutex)
414 #endif /* AFS_PTHREAD_ENV */
417 rx_SetEpoch(afs_uint32 epoch)
424 /* Initialize rx. A port number may be mentioned, in which case this
425 * becomes the default port number for any service installed later.
426 * If 0 is provided for the port number, a random port will be chosen
427 * by the kernel. Whether this will ever overlap anything in
428 * /etc/services is anybody's guess... Returns 0 on success, -1 on
433 int rxinit_status = 1;
434 #ifdef AFS_PTHREAD_ENV
436 * This mutex protects the following global variables:
440 #define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
441 #define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
444 #define UNLOCK_RX_INIT
448 rx_InitHost(u_int host, u_int port)
455 char *htable, *ptable;
462 if (rxinit_status == 0) {
463 tmp_status = rxinit_status;
465 return tmp_status; /* Already started; return previous error code. */
471 if (afs_winsockInit() < 0)
477 * Initialize anything necessary to provide a non-premptive threading
480 rxi_InitializeThreadSupport();
483 /* Allocate and initialize a socket for client and perhaps server
486 rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
487 if (rx_socket == OSI_NULLSOCKET) {
491 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
494 #endif /* RX_LOCKS_DB */
495 MUTEX_INIT(&rx_stats_mutex, "rx_stats_mutex", MUTEX_DEFAULT, 0);
496 MUTEX_INIT(&rx_quota_mutex, "rx_quota_mutex", MUTEX_DEFAULT, 0);
497 MUTEX_INIT(&rx_pthread_mutex, "rx_pthread_mutex", MUTEX_DEFAULT, 0);
498 MUTEX_INIT(&rx_packets_mutex, "rx_packets_mutex", MUTEX_DEFAULT, 0);
499 MUTEX_INIT(&rx_refcnt_mutex, "rx_refcnt_mutex", MUTEX_DEFAULT, 0);
500 MUTEX_INIT(&rx_rpc_stats, "rx_rpc_stats", MUTEX_DEFAULT, 0);
501 MUTEX_INIT(&rx_freePktQ_lock, "rx_freePktQ_lock", MUTEX_DEFAULT, 0);
502 MUTEX_INIT(&freeSQEList_lock, "freeSQEList lock", MUTEX_DEFAULT, 0);
503 MUTEX_INIT(&rx_freeCallQueue_lock, "rx_freeCallQueue_lock", MUTEX_DEFAULT,
505 CV_INIT(&rx_waitingForPackets_cv, "rx_waitingForPackets_cv", CV_DEFAULT,
507 MUTEX_INIT(&rx_peerHashTable_lock, "rx_peerHashTable_lock", MUTEX_DEFAULT,
509 MUTEX_INIT(&rx_connHashTable_lock, "rx_connHashTable_lock", MUTEX_DEFAULT,
511 MUTEX_INIT(&rx_serverPool_lock, "rx_serverPool_lock", MUTEX_DEFAULT, 0);
512 #if defined(AFS_HPUX110_ENV)
514 rx_sleepLock = alloc_spinlock(LAST_HELD_ORDER - 10, "rx_sleepLock");
515 #endif /* AFS_HPUX110_ENV */
516 #endif /* RX_ENABLE_LOCKS && KERNEL */
519 rx_connDeadTime = 12;
520 rx_tranquil = 0; /* reset flag */
521 rxi_ResetStatistics();
523 osi_Alloc(rx_hashTableSize * sizeof(struct rx_connection *));
524 PIN(htable, rx_hashTableSize * sizeof(struct rx_connection *)); /* XXXXX */
525 memset(htable, 0, rx_hashTableSize * sizeof(struct rx_connection *));
526 ptable = (char *)osi_Alloc(rx_hashTableSize * sizeof(struct rx_peer *));
527 PIN(ptable, rx_hashTableSize * sizeof(struct rx_peer *)); /* XXXXX */
528 memset(ptable, 0, rx_hashTableSize * sizeof(struct rx_peer *));
530 /* Malloc up a bunch of packets & buffers */
532 queue_Init(&rx_freePacketQueue);
533 rxi_NeedMorePackets = FALSE;
534 rx_nPackets = 0; /* rx_nPackets is managed by rxi_MorePackets* */
536 /* enforce a minimum number of allocated packets */
537 if (rx_extraPackets < rxi_nSendFrags * rx_maxSendWindow)
538 rx_extraPackets = rxi_nSendFrags * rx_maxSendWindow;
540 /* allocate the initial free packet pool */
541 #ifdef RX_ENABLE_TSFPQ
542 rxi_MorePacketsTSFPQ(rx_extraPackets + RX_MAX_QUOTA + 2, RX_TS_FPQ_FLUSH_GLOBAL, 0);
543 #else /* RX_ENABLE_TSFPQ */
544 rxi_MorePackets(rx_extraPackets + RX_MAX_QUOTA + 2); /* fudge */
545 #endif /* RX_ENABLE_TSFPQ */
552 #if defined(AFS_NT40_ENV) && !defined(AFS_PTHREAD_ENV)
553 tv.tv_sec = clock_now.sec;
554 tv.tv_usec = clock_now.usec;
555 srand((unsigned int)tv.tv_usec);
562 #if defined(KERNEL) && !defined(UKERNEL)
563 /* Really, this should never happen in a real kernel */
566 struct sockaddr_in addr;
568 int addrlen = sizeof(addr);
570 socklen_t addrlen = sizeof(addr);
572 if (getsockname((intptr_t)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
576 rx_port = addr.sin_port;
579 rx_stats.minRtt.sec = 9999999;
581 rx_SetEpoch(tv.tv_sec | 0x80000000);
583 rx_SetEpoch(tv.tv_sec); /* Start time of this package, rxkad
584 * will provide a randomer value. */
586 MUTEX_ENTER(&rx_quota_mutex);
587 rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
588 MUTEX_EXIT(&rx_quota_mutex);
589 /* *Slightly* random start time for the cid. This is just to help
590 * out with the hashing function at the peer */
591 rx_nextCid = ((tv.tv_sec ^ tv.tv_usec) << RX_CIDSHIFT);
592 rx_connHashTable = (struct rx_connection **)htable;
593 rx_peerHashTable = (struct rx_peer **)ptable;
595 rx_hardAckDelay.sec = 0;
596 rx_hardAckDelay.usec = 100000; /* 100 milliseconds */
598 rxevent_Init(20, rxi_ReScheduleEvents);
600 /* Initialize various global queues */
601 queue_Init(&rx_idleServerQueue);
602 queue_Init(&rx_incomingCallQueue);
603 queue_Init(&rx_freeCallQueue);
605 #if defined(AFS_NT40_ENV) && !defined(KERNEL)
606 /* Initialize our list of usable IP addresses. */
610 #if defined(RXK_LISTENER_ENV) || !defined(KERNEL)
611 /* Start listener process (exact function is dependent on the
612 * implementation environment--kernel or user space) */
617 tmp_status = rxinit_status = 0;
625 return rx_InitHost(htonl(INADDR_ANY), port);
631 * The rxi_rto functions implement a TCP (RFC2988) style algorithm for
632 * maintaing the round trip timer.
637 * Start a new RTT timer for a given call and packet.
639 * There must be no resendEvent already listed for this call, otherwise this
640 * will leak events - intended for internal use within the RTO code only
643 * the RX call to start the timer for
644 * @param[in] lastPacket
645 * a flag indicating whether the last packet has been sent or not
647 * @pre call must be locked before calling this function
651 rxi_rto_startTimer(struct rx_call *call, int lastPacket, int istack)
653 struct clock now, retryTime;
658 clock_Add(&retryTime, &call->rto);
660 /* If we're sending the last packet, and we're the client, then the server
661 * may wait for an additional 400ms before returning the ACK, wait for it
662 * rather than hitting a timeout */
663 if (lastPacket && call->conn->type == RX_CLIENT_CONNECTION)
664 clock_Addmsec(&retryTime, 400);
666 CALL_HOLD(call, RX_CALL_REFCOUNT_RESEND);
667 call->resendEvent = rxevent_Post(&retryTime, &now, rxi_Resend,
672 * Cancel an RTT timer for a given call.
676 * the RX call to cancel the timer for
678 * @pre call must be locked before calling this function
683 rxi_rto_cancel(struct rx_call *call)
685 rxevent_Cancel(&call->resendEvent, call, RX_CALL_REFCOUNT_RESEND);
689 * Tell the RTO timer that we have sent a packet.
691 * If the timer isn't already running, then start it. If the timer is running,
695 * the RX call that the packet has been sent on
696 * @param[in] lastPacket
697 * A flag which is true if this is the last packet for the call
699 * @pre The call must be locked before calling this function
704 rxi_rto_packet_sent(struct rx_call *call, int lastPacket, int istack)
706 if (call->resendEvent)
709 rxi_rto_startTimer(call, lastPacket, istack);
713 * Tell the RTO timer that we have received an new ACK message
715 * This function should be called whenever a call receives an ACK that
716 * acknowledges new packets. Whatever happens, we stop the current timer.
717 * If there are unacked packets in the queue which have been sent, then
718 * we restart the timer from now. Otherwise, we leave it stopped.
721 * the RX call that the ACK has been received on
725 rxi_rto_packet_acked(struct rx_call *call, int istack)
727 struct rx_packet *p, *nxp;
729 rxi_rto_cancel(call);
731 if (queue_IsEmpty(&call->tq))
734 for (queue_Scan(&call->tq, p, nxp, rx_packet)) {
735 if (p->header.seq > call->tfirst + call->twind)
738 if (!(p->flags & RX_PKTFLAG_ACKED) && p->flags & RX_PKTFLAG_SENT) {
739 rxi_rto_startTimer(call, p->header.flags & RX_LAST_PACKET, istack);
747 * Set an initial round trip timeout for a peer connection
749 * @param[in] secs The timeout to set in seconds
753 rx_rto_setPeerTimeoutSecs(struct rx_peer *peer, int secs) {
754 peer->rtt = secs * 8000;
758 * Enables or disables the busy call channel error (RX_CALL_BUSY).
760 * @param[in] onoff Non-zero to enable busy call channel errors.
762 * @pre Neither rx_Init nor rx_InitHost have been called yet
765 rx_SetBusyChannelError(afs_int32 onoff)
767 osi_Assert(rxinit_status != 0);
768 rxi_busyChannelError = onoff ? 1 : 0;
772 * Set a delayed ack event on the specified call for the given time
774 * @param[in] call - the call on which to set the event
775 * @param[in] offset - the delay from now after which the event fires
778 rxi_PostDelayedAckEvent(struct rx_call *call, struct clock *offset)
780 struct clock now, when;
784 clock_Add(&when, offset);
786 if (!call->delayedAckEvent
787 || clock_Gt(&call->delayedAckTime, &when)) {
789 rxevent_Cancel(&call->delayedAckEvent, call,
790 RX_CALL_REFCOUNT_DELAY);
791 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
793 call->delayedAckEvent = rxevent_Post(&when, &now,
796 call->delayedAckTime = when;
800 /* called with unincremented nRequestsRunning to see if it is OK to start
801 * a new thread in this service. Could be "no" for two reasons: over the
802 * max quota, or would prevent others from reaching their min quota.
804 #ifdef RX_ENABLE_LOCKS
805 /* This verion of QuotaOK reserves quota if it's ok while the
806 * rx_serverPool_lock is held. Return quota using ReturnToServerPool().
809 QuotaOK(struct rx_service *aservice)
811 /* check if over max quota */
812 if (aservice->nRequestsRunning >= aservice->maxProcs) {
816 /* under min quota, we're OK */
817 /* otherwise, can use only if there are enough to allow everyone
818 * to go to their min quota after this guy starts.
821 MUTEX_ENTER(&rx_quota_mutex);
822 if ((aservice->nRequestsRunning < aservice->minProcs)
823 || (rxi_availProcs > rxi_minDeficit)) {
824 aservice->nRequestsRunning++;
825 /* just started call in minProcs pool, need fewer to maintain
827 if (aservice->nRequestsRunning <= aservice->minProcs)
830 MUTEX_EXIT(&rx_quota_mutex);
833 MUTEX_EXIT(&rx_quota_mutex);
839 ReturnToServerPool(struct rx_service *aservice)
841 aservice->nRequestsRunning--;
842 MUTEX_ENTER(&rx_quota_mutex);
843 if (aservice->nRequestsRunning < aservice->minProcs)
846 MUTEX_EXIT(&rx_quota_mutex);
849 #else /* RX_ENABLE_LOCKS */
851 QuotaOK(struct rx_service *aservice)
854 /* under min quota, we're OK */
855 if (aservice->nRequestsRunning < aservice->minProcs)
858 /* check if over max quota */
859 if (aservice->nRequestsRunning >= aservice->maxProcs)
862 /* otherwise, can use only if there are enough to allow everyone
863 * to go to their min quota after this guy starts.
865 MUTEX_ENTER(&rx_quota_mutex);
866 if (rxi_availProcs > rxi_minDeficit)
868 MUTEX_EXIT(&rx_quota_mutex);
871 #endif /* RX_ENABLE_LOCKS */
874 /* Called by rx_StartServer to start up lwp's to service calls.
875 NExistingProcs gives the number of procs already existing, and which
876 therefore needn't be created. */
878 rxi_StartServerProcs(int nExistingProcs)
880 struct rx_service *service;
885 /* For each service, reserve N processes, where N is the "minimum"
886 * number of processes that MUST be able to execute a request in parallel,
887 * at any time, for that process. Also compute the maximum difference
888 * between any service's maximum number of processes that can run
889 * (i.e. the maximum number that ever will be run, and a guarantee
890 * that this number will run if other services aren't running), and its
891 * minimum number. The result is the extra number of processes that
892 * we need in order to provide the latter guarantee */
893 for (i = 0; i < RX_MAX_SERVICES; i++) {
895 service = rx_services[i];
896 if (service == (struct rx_service *)0)
898 nProcs += service->minProcs;
899 diff = service->maxProcs - service->minProcs;
903 nProcs += maxdiff; /* Extra processes needed to allow max number requested to run in any given service, under good conditions */
904 nProcs -= nExistingProcs; /* Subtract the number of procs that were previously created for use as server procs */
905 for (i = 0; i < nProcs; i++) {
906 rxi_StartServerProc(rx_ServerProc, rx_stackSize);
912 /* This routine is only required on Windows */
914 rx_StartClientThread(void)
916 #ifdef AFS_PTHREAD_ENV
918 pid = pthread_self();
919 #endif /* AFS_PTHREAD_ENV */
921 #endif /* AFS_NT40_ENV */
923 /* This routine must be called if any services are exported. If the
924 * donateMe flag is set, the calling process is donated to the server
927 rx_StartServer(int donateMe)
929 struct rx_service *service;
935 /* Start server processes, if necessary (exact function is dependent
936 * on the implementation environment--kernel or user space). DonateMe
937 * will be 1 if there is 1 pre-existing proc, i.e. this one. In this
938 * case, one less new proc will be created rx_StartServerProcs.
940 rxi_StartServerProcs(donateMe);
942 /* count up the # of threads in minProcs, and add set the min deficit to
943 * be that value, too.
945 for (i = 0; i < RX_MAX_SERVICES; i++) {
946 service = rx_services[i];
947 if (service == (struct rx_service *)0)
949 MUTEX_ENTER(&rx_quota_mutex);
950 rxi_totalMin += service->minProcs;
951 /* below works even if a thread is running, since minDeficit would
952 * still have been decremented and later re-incremented.
954 rxi_minDeficit += service->minProcs;
955 MUTEX_EXIT(&rx_quota_mutex);
958 /* Turn on reaping of idle server connections */
959 rxi_ReapConnections(NULL, NULL, NULL, 0);
968 #ifdef AFS_PTHREAD_ENV
970 pid = afs_pointer_to_int(pthread_self());
971 #else /* AFS_PTHREAD_ENV */
973 LWP_CurrentProcess(&pid);
974 #endif /* AFS_PTHREAD_ENV */
976 sprintf(name, "srv_%d", ++nProcs);
978 (*registerProgram) (pid, name);
980 #endif /* AFS_NT40_ENV */
981 rx_ServerProc(NULL); /* Never returns */
983 #ifdef RX_ENABLE_TSFPQ
984 /* no use leaving packets around in this thread's local queue if
985 * it isn't getting donated to the server thread pool.
987 rxi_FlushLocalPacketsTSFPQ();
988 #endif /* RX_ENABLE_TSFPQ */
992 /* Create a new client connection to the specified service, using the
993 * specified security object to implement the security model for this
995 struct rx_connection *
996 rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
997 struct rx_securityClass *securityObject,
998 int serviceSecurityIndex)
1002 struct rx_connection *conn;
1007 dpf(("rx_NewConnection(host %x, port %u, service %u, securityObject %p, "
1008 "serviceSecurityIndex %d)\n",
1009 ntohl(shost), ntohs(sport), sservice, securityObject,
1010 serviceSecurityIndex));
1012 /* Vasilsi said: "NETPRI protects Cid and Alloc", but can this be true in
1013 * the case of kmem_alloc? */
1014 conn = rxi_AllocConnection();
1015 #ifdef RX_ENABLE_LOCKS
1016 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
1017 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
1018 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
1021 MUTEX_ENTER(&rx_connHashTable_lock);
1022 cid = (rx_nextCid += RX_MAXCALLS);
1023 conn->type = RX_CLIENT_CONNECTION;
1025 conn->epoch = rx_epoch;
1026 conn->peer = rxi_FindPeer(shost, sport, 0, 1);
1027 conn->serviceId = sservice;
1028 conn->securityObject = securityObject;
1029 conn->securityData = (void *) 0;
1030 conn->securityIndex = serviceSecurityIndex;
1031 rx_SetConnDeadTime(conn, rx_connDeadTime);
1032 rx_SetConnSecondsUntilNatPing(conn, 0);
1033 conn->ackRate = RX_FAST_ACK_RATE;
1034 conn->nSpecific = 0;
1035 conn->specific = NULL;
1036 conn->challengeEvent = NULL;
1037 conn->delayedAbortEvent = NULL;
1038 conn->abortCount = 0;
1040 for (i = 0; i < RX_MAXCALLS; i++) {
1041 conn->twind[i] = rx_initSendWindow;
1042 conn->rwind[i] = rx_initReceiveWindow;
1043 conn->lastBusy[i] = 0;
1046 RXS_NewConnection(securityObject, conn);
1048 CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
1050 conn->refCount++; /* no lock required since only this thread knows... */
1051 conn->next = rx_connHashTable[hashindex];
1052 rx_connHashTable[hashindex] = conn;
1053 if (rx_stats_active)
1054 rx_atomic_inc(&rx_stats.nClientConns);
1055 MUTEX_EXIT(&rx_connHashTable_lock);
1061 * Ensure a connection's timeout values are valid.
1063 * @param[in] conn The connection to check
1065 * @post conn->secondUntilDead <= conn->idleDeadTime <= conn->hardDeadTime,
1066 * unless idleDeadTime and/or hardDeadTime are not set
1070 rxi_CheckConnTimeouts(struct rx_connection *conn)
1072 /* a connection's timeouts must have the relationship
1073 * deadTime <= idleDeadTime <= hardDeadTime. Otherwise, for example, a
1074 * total loss of network to a peer may cause an idle timeout instead of a
1075 * dead timeout, simply because the idle timeout gets hit first. Also set
1076 * a minimum deadTime of 6, just to ensure it doesn't get set too low. */
1077 /* this logic is slightly complicated by the fact that
1078 * idleDeadTime/hardDeadTime may not be set at all, but it's not too bad.
1080 conn->secondsUntilDead = MAX(conn->secondsUntilDead, 6);
1081 if (conn->idleDeadTime) {
1082 conn->idleDeadTime = MAX(conn->idleDeadTime, conn->secondsUntilDead);
1084 if (conn->hardDeadTime) {
1085 if (conn->idleDeadTime) {
1086 conn->hardDeadTime = MAX(conn->idleDeadTime, conn->hardDeadTime);
1088 conn->hardDeadTime = MAX(conn->secondsUntilDead, conn->hardDeadTime);
1094 rx_SetConnDeadTime(struct rx_connection *conn, int seconds)
1096 /* The idea is to set the dead time to a value that allows several
1097 * keepalives to be dropped without timing out the connection. */
1098 conn->secondsUntilDead = seconds;
1099 rxi_CheckConnTimeouts(conn);
1100 conn->secondsUntilPing = conn->secondsUntilDead / 6;
1104 rx_SetConnHardDeadTime(struct rx_connection *conn, int seconds)
1106 conn->hardDeadTime = seconds;
1107 rxi_CheckConnTimeouts(conn);
1111 rx_SetConnIdleDeadTime(struct rx_connection *conn, int seconds)
1113 conn->idleDeadTime = seconds;
1114 conn->idleDeadDetection = (seconds ? 1 : 0);
1115 rxi_CheckConnTimeouts(conn);
1118 int rxi_lowPeerRefCount = 0;
1119 int rxi_lowConnRefCount = 0;
1122 * Cleanup a connection that was destroyed in rxi_DestroyConnectioNoLock.
1123 * NOTE: must not be called with rx_connHashTable_lock held.
1126 rxi_CleanupConnection(struct rx_connection *conn)
1128 /* Notify the service exporter, if requested, that this connection
1129 * is being destroyed */
1130 if (conn->type == RX_SERVER_CONNECTION && conn->service->destroyConnProc)
1131 (*conn->service->destroyConnProc) (conn);
1133 /* Notify the security module that this connection is being destroyed */
1134 RXS_DestroyConnection(conn->securityObject, conn);
1136 /* If this is the last connection using the rx_peer struct, set its
1137 * idle time to now. rxi_ReapConnections will reap it if it's still
1138 * idle (refCount == 0) after rx_idlePeerTime (60 seconds) have passed.
1140 MUTEX_ENTER(&rx_peerHashTable_lock);
1141 if (conn->peer->refCount < 2) {
1142 conn->peer->idleWhen = clock_Sec();
1143 if (conn->peer->refCount < 1) {
1144 conn->peer->refCount = 1;
1145 if (rx_stats_active) {
1146 MUTEX_ENTER(&rx_stats_mutex);
1147 rxi_lowPeerRefCount++;
1148 MUTEX_EXIT(&rx_stats_mutex);
1152 conn->peer->refCount--;
1153 MUTEX_EXIT(&rx_peerHashTable_lock);
1155 if (rx_stats_active)
1157 if (conn->type == RX_SERVER_CONNECTION)
1158 rx_atomic_dec(&rx_stats.nServerConns);
1160 rx_atomic_dec(&rx_stats.nClientConns);
1163 if (conn->specific) {
1165 for (i = 0; i < conn->nSpecific; i++) {
1166 if (conn->specific[i] && rxi_keyCreate_destructor[i])
1167 (*rxi_keyCreate_destructor[i]) (conn->specific[i]);
1168 conn->specific[i] = NULL;
1170 free(conn->specific);
1172 conn->specific = NULL;
1173 conn->nSpecific = 0;
1174 #endif /* !KERNEL */
1176 MUTEX_DESTROY(&conn->conn_call_lock);
1177 MUTEX_DESTROY(&conn->conn_data_lock);
1178 CV_DESTROY(&conn->conn_call_cv);
1180 rxi_FreeConnection(conn);
1183 /* Destroy the specified connection */
1185 rxi_DestroyConnection(struct rx_connection *conn)
1187 MUTEX_ENTER(&rx_connHashTable_lock);
1188 rxi_DestroyConnectionNoLock(conn);
1189 /* conn should be at the head of the cleanup list */
1190 if (conn == rx_connCleanup_list) {
1191 rx_connCleanup_list = rx_connCleanup_list->next;
1192 MUTEX_EXIT(&rx_connHashTable_lock);
1193 rxi_CleanupConnection(conn);
1195 #ifdef RX_ENABLE_LOCKS
1197 MUTEX_EXIT(&rx_connHashTable_lock);
1199 #endif /* RX_ENABLE_LOCKS */
1203 rxi_DestroyConnectionNoLock(struct rx_connection *conn)
1205 struct rx_connection **conn_ptr;
1207 struct rx_packet *packet;
1214 MUTEX_ENTER(&conn->conn_data_lock);
1215 MUTEX_ENTER(&rx_refcnt_mutex);
1216 if (conn->refCount > 0)
1219 if (rx_stats_active) {
1220 MUTEX_ENTER(&rx_stats_mutex);
1221 rxi_lowConnRefCount++;
1222 MUTEX_EXIT(&rx_stats_mutex);
1226 if ((conn->refCount > 0) || (conn->flags & RX_CONN_BUSY)) {
1227 /* Busy; wait till the last guy before proceeding */
1228 MUTEX_EXIT(&rx_refcnt_mutex);
1229 MUTEX_EXIT(&conn->conn_data_lock);
1234 /* If the client previously called rx_NewCall, but it is still
1235 * waiting, treat this as a running call, and wait to destroy the
1236 * connection later when the call completes. */
1237 if ((conn->type == RX_CLIENT_CONNECTION)
1238 && (conn->flags & (RX_CONN_MAKECALL_WAITING|RX_CONN_MAKECALL_ACTIVE))) {
1239 conn->flags |= RX_CONN_DESTROY_ME;
1240 MUTEX_EXIT(&conn->conn_data_lock);
1244 MUTEX_EXIT(&rx_refcnt_mutex);
1245 MUTEX_EXIT(&conn->conn_data_lock);
1247 /* Check for extant references to this connection */
1248 MUTEX_ENTER(&conn->conn_call_lock);
1249 for (i = 0; i < RX_MAXCALLS; i++) {
1250 struct rx_call *call = conn->call[i];
1253 if (conn->type == RX_CLIENT_CONNECTION) {
1254 MUTEX_ENTER(&call->lock);
1255 if (call->delayedAckEvent) {
1256 /* Push the final acknowledgment out now--there
1257 * won't be a subsequent call to acknowledge the
1258 * last reply packets */
1259 rxevent_Cancel(&call->delayedAckEvent, call,
1260 RX_CALL_REFCOUNT_DELAY);
1261 if (call->state == RX_STATE_PRECALL
1262 || call->state == RX_STATE_ACTIVE) {
1263 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
1265 rxi_AckAll(NULL, call, 0);
1268 MUTEX_EXIT(&call->lock);
1272 MUTEX_EXIT(&conn->conn_call_lock);
1274 #ifdef RX_ENABLE_LOCKS
1276 if (MUTEX_TRYENTER(&conn->conn_data_lock)) {
1277 MUTEX_EXIT(&conn->conn_data_lock);
1279 /* Someone is accessing a packet right now. */
1283 #endif /* RX_ENABLE_LOCKS */
1286 /* Don't destroy the connection if there are any call
1287 * structures still in use */
1288 MUTEX_ENTER(&conn->conn_data_lock);
1289 conn->flags |= RX_CONN_DESTROY_ME;
1290 MUTEX_EXIT(&conn->conn_data_lock);
1295 if (conn->natKeepAliveEvent) {
1296 rxi_NatKeepAliveOff(conn);
1299 if (conn->delayedAbortEvent) {
1300 rxevent_Cancel(&conn->delayedAbortEvent, NULL, 0);
1301 packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
1303 MUTEX_ENTER(&conn->conn_data_lock);
1304 rxi_SendConnectionAbort(conn, packet, 0, 1);
1305 MUTEX_EXIT(&conn->conn_data_lock);
1306 rxi_FreePacket(packet);
1310 /* Remove from connection hash table before proceeding */
1312 &rx_connHashTable[CONN_HASH
1313 (peer->host, peer->port, conn->cid, conn->epoch,
1315 for (; *conn_ptr; conn_ptr = &(*conn_ptr)->next) {
1316 if (*conn_ptr == conn) {
1317 *conn_ptr = conn->next;
1321 /* if the conn that we are destroying was the last connection, then we
1322 * clear rxLastConn as well */
1323 if (rxLastConn == conn)
1326 /* Make sure the connection is completely reset before deleting it. */
1327 /* get rid of pending events that could zap us later */
1328 rxevent_Cancel(&conn->challengeEvent, NULL, 0);
1329 rxevent_Cancel(&conn->checkReachEvent, NULL, 0);
1330 rxevent_Cancel(&conn->natKeepAliveEvent, NULL, 0);
1332 /* Add the connection to the list of destroyed connections that
1333 * need to be cleaned up. This is necessary to avoid deadlocks
1334 * in the routines we call to inform others that this connection is
1335 * being destroyed. */
1336 conn->next = rx_connCleanup_list;
1337 rx_connCleanup_list = conn;
1340 /* Externally available version */
1342 rx_DestroyConnection(struct rx_connection *conn)
1347 rxi_DestroyConnection(conn);
1352 rx_GetConnection(struct rx_connection *conn)
1357 MUTEX_ENTER(&rx_refcnt_mutex);
1359 MUTEX_EXIT(&rx_refcnt_mutex);
1363 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1364 /* Wait for the transmit queue to no longer be busy.
1365 * requires the call->lock to be held */
1367 rxi_WaitforTQBusy(struct rx_call *call) {
1368 while (!call->error && (call->flags & RX_CALL_TQ_BUSY)) {
1369 call->flags |= RX_CALL_TQ_WAIT;
1371 #ifdef RX_ENABLE_LOCKS
1372 osirx_AssertMine(&call->lock, "rxi_WaitforTQ lock");
1373 CV_WAIT(&call->cv_tq, &call->lock);
1374 #else /* RX_ENABLE_LOCKS */
1375 osi_rxSleep(&call->tq);
1376 #endif /* RX_ENABLE_LOCKS */
1378 if (call->tqWaiters == 0) {
1379 call->flags &= ~RX_CALL_TQ_WAIT;
1386 rxi_WakeUpTransmitQueue(struct rx_call *call)
1388 if (call->tqWaiters || (call->flags & RX_CALL_TQ_WAIT)) {
1389 dpf(("call %"AFS_PTR_FMT" has %d waiters and flags %d\n",
1390 call, call->tqWaiters, call->flags));
1391 #ifdef RX_ENABLE_LOCKS
1392 osirx_AssertMine(&call->lock, "rxi_Start start");
1393 CV_BROADCAST(&call->cv_tq);
1394 #else /* RX_ENABLE_LOCKS */
1395 osi_rxWakeup(&call->tq);
1396 #endif /* RX_ENABLE_LOCKS */
1400 /* Start a new rx remote procedure call, on the specified connection.
1401 * If wait is set to 1, wait for a free call channel; otherwise return
1402 * 0. Maxtime gives the maximum number of seconds this call may take,
1403 * after rx_NewCall returns. After this time interval, a call to any
1404 * of rx_SendData, rx_ReadData, etc. will fail with RX_CALL_TIMEOUT.
1405 * For fine grain locking, we hold the conn_call_lock in order to
1406 * to ensure that we don't get signalle after we found a call in an active
1407 * state and before we go to sleep.
1410 rx_NewCall(struct rx_connection *conn)
1412 int i, wait, ignoreBusy = 1;
1413 struct rx_call *call;
1414 struct clock queueTime;
1415 afs_uint32 leastBusy = 0;
1419 dpf(("rx_NewCall(conn %"AFS_PTR_FMT")\n", conn));
1422 clock_GetTime(&queueTime);
1424 * Check if there are others waiting for a new call.
1425 * If so, let them go first to avoid starving them.
1426 * This is a fairly simple scheme, and might not be
1427 * a complete solution for large numbers of waiters.
1429 * makeCallWaiters keeps track of the number of
1430 * threads waiting to make calls and the
1431 * RX_CONN_MAKECALL_WAITING flag bit is used to
1432 * indicate that there are indeed calls waiting.
1433 * The flag is set when the waiter is incremented.
1434 * It is only cleared when makeCallWaiters is 0.
1435 * This prevents us from accidently destroying the
1436 * connection while it is potentially about to be used.
1438 MUTEX_ENTER(&conn->conn_call_lock);
1439 MUTEX_ENTER(&conn->conn_data_lock);
1440 while (conn->flags & RX_CONN_MAKECALL_ACTIVE) {
1441 conn->flags |= RX_CONN_MAKECALL_WAITING;
1442 conn->makeCallWaiters++;
1443 MUTEX_EXIT(&conn->conn_data_lock);
1445 #ifdef RX_ENABLE_LOCKS
1446 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1450 MUTEX_ENTER(&conn->conn_data_lock);
1451 conn->makeCallWaiters--;
1452 if (conn->makeCallWaiters == 0)
1453 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1456 /* We are now the active thread in rx_NewCall */
1457 conn->flags |= RX_CONN_MAKECALL_ACTIVE;
1458 MUTEX_EXIT(&conn->conn_data_lock);
1463 for (i = 0; i < RX_MAXCALLS; i++) {
1464 call = conn->call[i];
1466 if (!ignoreBusy && conn->lastBusy[i] != leastBusy) {
1467 /* we're not ignoring busy call slots; only look at the
1468 * call slot that is the "least" busy */
1472 if (call->state == RX_STATE_DALLY) {
1473 MUTEX_ENTER(&call->lock);
1474 if (call->state == RX_STATE_DALLY) {
1475 if (ignoreBusy && conn->lastBusy[i]) {
1476 /* if we're ignoring busy call slots, skip any ones that
1477 * have lastBusy set */
1478 if (leastBusy == 0 || conn->lastBusy[i] < leastBusy) {
1479 leastBusy = conn->lastBusy[i];
1481 MUTEX_EXIT(&call->lock);
1486 * We are setting the state to RX_STATE_RESET to
1487 * ensure that no one else will attempt to use this
1488 * call once we drop the conn->conn_call_lock and
1489 * call->lock. We must drop the conn->conn_call_lock
1490 * before calling rxi_ResetCall because the process
1491 * of clearing the transmit queue can block for an
1492 * extended period of time. If we block while holding
1493 * the conn->conn_call_lock, then all rx_EndCall
1494 * processing will block as well. This has a detrimental
1495 * effect on overall system performance.
1497 call->state = RX_STATE_RESET;
1498 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1499 (*call->callNumber)++;
1500 MUTEX_EXIT(&conn->conn_call_lock);
1501 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1502 rxi_ResetCall(call, 0);
1503 if (MUTEX_TRYENTER(&conn->conn_call_lock))
1507 * If we failed to be able to safely obtain the
1508 * conn->conn_call_lock we will have to drop the
1509 * call->lock to avoid a deadlock. When the call->lock
1510 * is released the state of the call can change. If it
1511 * is no longer RX_STATE_RESET then some other thread is
1514 MUTEX_EXIT(&call->lock);
1515 MUTEX_ENTER(&conn->conn_call_lock);
1516 MUTEX_ENTER(&call->lock);
1518 if (call->state == RX_STATE_RESET)
1522 * If we get here it means that after dropping
1523 * the conn->conn_call_lock and call->lock that
1524 * the call is no longer ours. If we can't find
1525 * a free call in the remaining slots we should
1526 * not go immediately to RX_CONN_MAKECALL_WAITING
1527 * because by dropping the conn->conn_call_lock
1528 * we have given up synchronization with rx_EndCall.
1529 * Instead, cycle through one more time to see if
1530 * we can find a call that can call our own.
1532 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
1535 MUTEX_EXIT(&call->lock);
1538 if (ignoreBusy && conn->lastBusy[i]) {
1539 /* if we're ignoring busy call slots, skip any ones that
1540 * have lastBusy set */
1541 if (leastBusy == 0 || conn->lastBusy[i] < leastBusy) {
1542 leastBusy = conn->lastBusy[i];
1547 /* rxi_NewCall returns with mutex locked */
1548 call = rxi_NewCall(conn, i);
1549 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
1553 if (i < RX_MAXCALLS) {
1554 conn->lastBusy[i] = 0;
1555 call->flags &= ~RX_CALL_PEER_BUSY;
1560 if (leastBusy && ignoreBusy) {
1561 /* we didn't find a useable call slot, but we did see at least one
1562 * 'busy' slot; look again and only use a slot with the 'least
1568 MUTEX_ENTER(&conn->conn_data_lock);
1569 conn->flags |= RX_CONN_MAKECALL_WAITING;
1570 conn->makeCallWaiters++;
1571 MUTEX_EXIT(&conn->conn_data_lock);
1573 #ifdef RX_ENABLE_LOCKS
1574 CV_WAIT(&conn->conn_call_cv, &conn->conn_call_lock);
1578 MUTEX_ENTER(&conn->conn_data_lock);
1579 conn->makeCallWaiters--;
1580 if (conn->makeCallWaiters == 0)
1581 conn->flags &= ~RX_CONN_MAKECALL_WAITING;
1582 MUTEX_EXIT(&conn->conn_data_lock);
1584 /* Client is initially in send mode */
1585 call->state = RX_STATE_ACTIVE;
1586 call->error = conn->error;
1588 call->mode = RX_MODE_ERROR;
1590 call->mode = RX_MODE_SENDING;
1592 /* remember start time for call in case we have hard dead time limit */
1593 call->queueTime = queueTime;
1594 clock_GetTime(&call->startTime);
1595 hzero(call->bytesSent);
1596 hzero(call->bytesRcvd);
1598 /* Turn on busy protocol. */
1599 rxi_KeepAliveOn(call);
1601 /* Attempt MTU discovery */
1602 rxi_GrowMTUOn(call);
1605 * We are no longer the active thread in rx_NewCall
1607 MUTEX_ENTER(&conn->conn_data_lock);
1608 conn->flags &= ~RX_CONN_MAKECALL_ACTIVE;
1609 MUTEX_EXIT(&conn->conn_data_lock);
1612 * Wake up anyone else who might be giving us a chance to
1613 * run (see code above that avoids resource starvation).
1615 #ifdef RX_ENABLE_LOCKS
1616 CV_BROADCAST(&conn->conn_call_cv);
1620 MUTEX_EXIT(&conn->conn_call_lock);
1622 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1623 if (call->flags & (RX_CALL_TQ_BUSY | RX_CALL_TQ_CLEARME)) {
1624 osi_Panic("rx_NewCall call about to be used without an empty tq");
1626 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1628 MUTEX_EXIT(&call->lock);
1631 dpf(("rx_NewCall(call %"AFS_PTR_FMT")\n", call));
1636 rxi_HasActiveCalls(struct rx_connection *aconn)
1639 struct rx_call *tcall;
1643 for (i = 0; i < RX_MAXCALLS; i++) {
1644 if ((tcall = aconn->call[i])) {
1645 if ((tcall->state == RX_STATE_ACTIVE)
1646 || (tcall->state == RX_STATE_PRECALL)) {
1657 rxi_GetCallNumberVector(struct rx_connection *aconn,
1658 afs_int32 * aint32s)
1661 struct rx_call *tcall;
1665 MUTEX_ENTER(&aconn->conn_call_lock);
1666 for (i = 0; i < RX_MAXCALLS; i++) {
1667 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1668 aint32s[i] = aconn->callNumber[i] + 1;
1670 aint32s[i] = aconn->callNumber[i];
1672 MUTEX_EXIT(&aconn->conn_call_lock);
1678 rxi_SetCallNumberVector(struct rx_connection *aconn,
1679 afs_int32 * aint32s)
1682 struct rx_call *tcall;
1686 MUTEX_ENTER(&aconn->conn_call_lock);
1687 for (i = 0; i < RX_MAXCALLS; i++) {
1688 if ((tcall = aconn->call[i]) && (tcall->state == RX_STATE_DALLY))
1689 aconn->callNumber[i] = aint32s[i] - 1;
1691 aconn->callNumber[i] = aint32s[i];
1693 MUTEX_EXIT(&aconn->conn_call_lock);
1698 /* Advertise a new service. A service is named locally by a UDP port
1699 * number plus a 16-bit service id. Returns (struct rx_service *) 0
1702 char *serviceName; Name for identification purposes (e.g. the
1703 service name might be used for probing for
1706 rx_NewServiceHost(afs_uint32 host, u_short port, u_short serviceId,
1707 char *serviceName, struct rx_securityClass **securityObjects,
1708 int nSecurityObjects,
1709 afs_int32(*serviceProc) (struct rx_call * acall))
1711 osi_socket socket = OSI_NULLSOCKET;
1712 struct rx_service *tservice;
1718 if (serviceId == 0) {
1720 "rx_NewService: service id for service %s is not non-zero.\n",
1727 "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",
1735 tservice = rxi_AllocService();
1738 #ifdef RX_ENABLE_LOCKS
1739 MUTEX_INIT(&tservice->svc_data_lock, "svc data lock", MUTEX_DEFAULT, 0);
1742 for (i = 0; i < RX_MAX_SERVICES; i++) {
1743 struct rx_service *service = rx_services[i];
1745 if (port == service->servicePort && host == service->serviceHost) {
1746 if (service->serviceId == serviceId) {
1747 /* The identical service has already been
1748 * installed; if the caller was intending to
1749 * change the security classes used by this
1750 * service, he/she loses. */
1752 "rx_NewService: tried to install service %s with service id %d, which is already in use for service %s\n",
1753 serviceName, serviceId, service->serviceName);
1755 rxi_FreeService(tservice);
1758 /* Different service, same port: re-use the socket
1759 * which is bound to the same port */
1760 socket = service->socket;
1763 if (socket == OSI_NULLSOCKET) {
1764 /* If we don't already have a socket (from another
1765 * service on same port) get a new one */
1766 socket = rxi_GetHostUDPSocket(host, port);
1767 if (socket == OSI_NULLSOCKET) {
1769 rxi_FreeService(tservice);
1774 service->socket = socket;
1775 service->serviceHost = host;
1776 service->servicePort = port;
1777 service->serviceId = serviceId;
1778 service->serviceName = serviceName;
1779 service->nSecurityObjects = nSecurityObjects;
1780 service->securityObjects = securityObjects;
1781 service->minProcs = 0;
1782 service->maxProcs = 1;
1783 service->idleDeadTime = 60;
1784 service->idleDeadErr = 0;
1785 service->connDeadTime = rx_connDeadTime;
1786 service->executeRequestProc = serviceProc;
1787 service->checkReach = 0;
1788 service->nSpecific = 0;
1789 service->specific = NULL;
1790 rx_services[i] = service; /* not visible until now */
1796 rxi_FreeService(tservice);
1797 (osi_Msg "rx_NewService: cannot support > %d services\n",
1802 /* Set configuration options for all of a service's security objects */
1805 rx_SetSecurityConfiguration(struct rx_service *service,
1806 rx_securityConfigVariables type,
1810 for (i = 0; i<service->nSecurityObjects; i++) {
1811 if (service->securityObjects[i]) {
1812 RXS_SetConfiguration(service->securityObjects[i], NULL, type,
1820 rx_NewService(u_short port, u_short serviceId, char *serviceName,
1821 struct rx_securityClass **securityObjects, int nSecurityObjects,
1822 afs_int32(*serviceProc) (struct rx_call * acall))
1824 return rx_NewServiceHost(htonl(INADDR_ANY), port, serviceId, serviceName, securityObjects, nSecurityObjects, serviceProc);
1827 /* Generic request processing loop. This routine should be called
1828 * by the implementation dependent rx_ServerProc. If socketp is
1829 * non-null, it will be set to the file descriptor that this thread
1830 * is now listening on. If socketp is null, this routine will never
1833 rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
1835 struct rx_call *call;
1837 struct rx_service *tservice = NULL;
1844 call = rx_GetCall(threadID, tservice, socketp);
1845 if (socketp && *socketp != OSI_NULLSOCKET) {
1846 /* We are now a listener thread */
1852 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
1853 #ifdef RX_ENABLE_LOCKS
1855 #endif /* RX_ENABLE_LOCKS */
1856 afs_termState = AFSOP_STOP_AFS;
1857 afs_osi_Wakeup(&afs_termState);
1858 #ifdef RX_ENABLE_LOCKS
1860 #endif /* RX_ENABLE_LOCKS */
1865 /* if server is restarting( typically smooth shutdown) then do not
1866 * allow any new calls.
1869 if (rx_tranquil && (call != NULL)) {
1873 MUTEX_ENTER(&call->lock);
1875 rxi_CallError(call, RX_RESTARTING);
1876 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
1878 MUTEX_EXIT(&call->lock);
1883 tservice = call->conn->service;
1885 if (tservice->beforeProc)
1886 (*tservice->beforeProc) (call);
1888 code = tservice->executeRequestProc(call);
1890 if (tservice->afterProc)
1891 (*tservice->afterProc) (call, code);
1893 rx_EndCall(call, code);
1895 if (tservice->postProc)
1896 (*tservice->postProc) (code);
1898 if (rx_stats_active) {
1899 MUTEX_ENTER(&rx_stats_mutex);
1901 MUTEX_EXIT(&rx_stats_mutex);
1908 rx_WakeupServerProcs(void)
1910 struct rx_serverQueueEntry *np, *tqp;
1914 MUTEX_ENTER(&rx_serverPool_lock);
1916 #ifdef RX_ENABLE_LOCKS
1917 if (rx_waitForPacket)
1918 CV_BROADCAST(&rx_waitForPacket->cv);
1919 #else /* RX_ENABLE_LOCKS */
1920 if (rx_waitForPacket)
1921 osi_rxWakeup(rx_waitForPacket);
1922 #endif /* RX_ENABLE_LOCKS */
1923 MUTEX_ENTER(&freeSQEList_lock);
1924 for (np = rx_FreeSQEList; np; np = tqp) {
1925 tqp = *(struct rx_serverQueueEntry **)np;
1926 #ifdef RX_ENABLE_LOCKS
1927 CV_BROADCAST(&np->cv);
1928 #else /* RX_ENABLE_LOCKS */
1930 #endif /* RX_ENABLE_LOCKS */
1932 MUTEX_EXIT(&freeSQEList_lock);
1933 for (queue_Scan(&rx_idleServerQueue, np, tqp, rx_serverQueueEntry)) {
1934 #ifdef RX_ENABLE_LOCKS
1935 CV_BROADCAST(&np->cv);
1936 #else /* RX_ENABLE_LOCKS */
1938 #endif /* RX_ENABLE_LOCKS */
1940 MUTEX_EXIT(&rx_serverPool_lock);
1945 * One thing that seems to happen is that all the server threads get
1946 * tied up on some empty or slow call, and then a whole bunch of calls
1947 * arrive at once, using up the packet pool, so now there are more
1948 * empty calls. The most critical resources here are server threads
1949 * and the free packet pool. The "doreclaim" code seems to help in
1950 * general. I think that eventually we arrive in this state: there
1951 * are lots of pending calls which do have all their packets present,
1952 * so they won't be reclaimed, are multi-packet calls, so they won't
1953 * be scheduled until later, and thus are tying up most of the free
1954 * packet pool for a very long time.
1956 * 1. schedule multi-packet calls if all the packets are present.
1957 * Probably CPU-bound operation, useful to return packets to pool.
1958 * Do what if there is a full window, but the last packet isn't here?
1959 * 3. preserve one thread which *only* runs "best" calls, otherwise
1960 * it sleeps and waits for that type of call.
1961 * 4. Don't necessarily reserve a whole window for each thread. In fact,
1962 * the current dataquota business is badly broken. The quota isn't adjusted
1963 * to reflect how many packets are presently queued for a running call.
1964 * So, when we schedule a queued call with a full window of packets queued
1965 * up for it, that *should* free up a window full of packets for other 2d-class
1966 * calls to be able to use from the packet pool. But it doesn't.
1968 * NB. Most of the time, this code doesn't run -- since idle server threads
1969 * sit on the idle server queue and are assigned by "...ReceivePacket" as soon
1970 * as a new call arrives.
1972 /* Sleep until a call arrives. Returns a pointer to the call, ready
1973 * for an rx_Read. */
1974 #ifdef RX_ENABLE_LOCKS
1976 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
1978 struct rx_serverQueueEntry *sq;
1979 struct rx_call *call = (struct rx_call *)0;
1980 struct rx_service *service = NULL;
1982 MUTEX_ENTER(&freeSQEList_lock);
1984 if ((sq = rx_FreeSQEList)) {
1985 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
1986 MUTEX_EXIT(&freeSQEList_lock);
1987 } else { /* otherwise allocate a new one and return that */
1988 MUTEX_EXIT(&freeSQEList_lock);
1989 sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
1990 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
1991 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
1994 MUTEX_ENTER(&rx_serverPool_lock);
1995 if (cur_service != NULL) {
1996 ReturnToServerPool(cur_service);
1999 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
2000 struct rx_call *tcall, *ncall, *choice2 = NULL;
2002 /* Scan for eligible incoming calls. A call is not eligible
2003 * if the maximum number of calls for its service type are
2004 * already executing */
2005 /* One thread will process calls FCFS (to prevent starvation),
2006 * while the other threads may run ahead looking for calls which
2007 * have all their input data available immediately. This helps
2008 * keep threads from blocking, waiting for data from the client. */
2009 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
2010 service = tcall->conn->service;
2011 if (!QuotaOK(service)) {
2014 MUTEX_ENTER(&rx_pthread_mutex);
2015 if (tno == rxi_fcfs_thread_num
2016 || queue_IsLast(&rx_incomingCallQueue, tcall)) {
2017 MUTEX_EXIT(&rx_pthread_mutex);
2018 /* If we're the fcfs thread , then we'll just use
2019 * this call. If we haven't been able to find an optimal
2020 * choice, and we're at the end of the list, then use a
2021 * 2d choice if one has been identified. Otherwise... */
2022 call = (choice2 ? choice2 : tcall);
2023 service = call->conn->service;
2025 MUTEX_EXIT(&rx_pthread_mutex);
2026 if (!queue_IsEmpty(&tcall->rq)) {
2027 struct rx_packet *rp;
2028 rp = queue_First(&tcall->rq, rx_packet);
2029 if (rp->header.seq == 1) {
2031 || (rp->header.flags & RX_LAST_PACKET)) {
2033 } else if (rxi_2dchoice && !choice2
2034 && !(tcall->flags & RX_CALL_CLEARED)
2035 && (tcall->rprev > rxi_HardAckRate)) {
2045 ReturnToServerPool(service);
2052 MUTEX_EXIT(&rx_serverPool_lock);
2053 MUTEX_ENTER(&call->lock);
2055 if (call->flags & RX_CALL_WAIT_PROC) {
2056 call->flags &= ~RX_CALL_WAIT_PROC;
2057 rx_atomic_dec(&rx_nWaiting);
2060 if (call->state != RX_STATE_PRECALL || call->error) {
2061 MUTEX_EXIT(&call->lock);
2062 MUTEX_ENTER(&rx_serverPool_lock);
2063 ReturnToServerPool(service);
2068 if (queue_IsEmpty(&call->rq)
2069 || queue_First(&call->rq, rx_packet)->header.seq != 1)
2070 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
2072 CLEAR_CALL_QUEUE_LOCK(call);
2075 /* If there are no eligible incoming calls, add this process
2076 * to the idle server queue, to wait for one */
2080 *socketp = OSI_NULLSOCKET;
2082 sq->socketp = socketp;
2083 queue_Append(&rx_idleServerQueue, sq);
2084 #ifndef AFS_AIX41_ENV
2085 rx_waitForPacket = sq;
2087 rx_waitingForPacket = sq;
2088 #endif /* AFS_AIX41_ENV */
2090 CV_WAIT(&sq->cv, &rx_serverPool_lock);
2092 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
2093 MUTEX_EXIT(&rx_serverPool_lock);
2094 return (struct rx_call *)0;
2097 } while (!(call = sq->newcall)
2098 && !(socketp && *socketp != OSI_NULLSOCKET));
2099 MUTEX_EXIT(&rx_serverPool_lock);
2101 MUTEX_ENTER(&call->lock);
2107 MUTEX_ENTER(&freeSQEList_lock);
2108 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
2109 rx_FreeSQEList = sq;
2110 MUTEX_EXIT(&freeSQEList_lock);
2113 clock_GetTime(&call->startTime);
2114 call->state = RX_STATE_ACTIVE;
2115 call->mode = RX_MODE_RECEIVING;
2116 #ifdef RX_KERNEL_TRACE
2117 if (ICL_SETACTIVE(afs_iclSetp)) {
2118 int glockOwner = ISAFS_GLOCK();
2121 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
2122 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2129 rxi_calltrace(RX_CALL_START, call);
2130 dpf(("rx_GetCall(port=%d, service=%d) ==> call %"AFS_PTR_FMT"\n",
2131 call->conn->service->servicePort, call->conn->service->serviceId,
2134 MUTEX_EXIT(&call->lock);
2135 CALL_HOLD(call, RX_CALL_REFCOUNT_BEGIN);
2137 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
2142 #else /* RX_ENABLE_LOCKS */
2144 rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
2146 struct rx_serverQueueEntry *sq;
2147 struct rx_call *call = (struct rx_call *)0, *choice2;
2148 struct rx_service *service = NULL;
2152 MUTEX_ENTER(&freeSQEList_lock);
2154 if ((sq = rx_FreeSQEList)) {
2155 rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
2156 MUTEX_EXIT(&freeSQEList_lock);
2157 } else { /* otherwise allocate a new one and return that */
2158 MUTEX_EXIT(&freeSQEList_lock);
2159 sq = rxi_Alloc(sizeof(struct rx_serverQueueEntry));
2160 MUTEX_INIT(&sq->lock, "server Queue lock", MUTEX_DEFAULT, 0);
2161 CV_INIT(&sq->cv, "server Queue lock", CV_DEFAULT, 0);
2163 MUTEX_ENTER(&sq->lock);
2165 if (cur_service != NULL) {
2166 cur_service->nRequestsRunning--;
2167 MUTEX_ENTER(&rx_quota_mutex);
2168 if (cur_service->nRequestsRunning < cur_service->minProcs)
2171 MUTEX_EXIT(&rx_quota_mutex);
2173 if (queue_IsNotEmpty(&rx_incomingCallQueue)) {
2174 struct rx_call *tcall, *ncall;
2175 /* Scan for eligible incoming calls. A call is not eligible
2176 * if the maximum number of calls for its service type are
2177 * already executing */
2178 /* One thread will process calls FCFS (to prevent starvation),
2179 * while the other threads may run ahead looking for calls which
2180 * have all their input data available immediately. This helps
2181 * keep threads from blocking, waiting for data from the client. */
2182 choice2 = (struct rx_call *)0;
2183 for (queue_Scan(&rx_incomingCallQueue, tcall, ncall, rx_call)) {
2184 service = tcall->conn->service;
2185 if (QuotaOK(service)) {
2186 MUTEX_ENTER(&rx_pthread_mutex);
2187 if (tno == rxi_fcfs_thread_num
2188 || !tcall->queue_item_header.next) {
2189 MUTEX_EXIT(&rx_pthread_mutex);
2190 /* If we're the fcfs thread, then we'll just use
2191 * this call. If we haven't been able to find an optimal
2192 * choice, and we're at the end of the list, then use a
2193 * 2d choice if one has been identified. Otherwise... */
2194 call = (choice2 ? choice2 : tcall);
2195 service = call->conn->service;
2197 MUTEX_EXIT(&rx_pthread_mutex);
2198 if (!queue_IsEmpty(&tcall->rq)) {
2199 struct rx_packet *rp;
2200 rp = queue_First(&tcall->rq, rx_packet);
2201 if (rp->header.seq == 1
2203 || (rp->header.flags & RX_LAST_PACKET))) {
2205 } else if (rxi_2dchoice && !choice2
2206 && !(tcall->flags & RX_CALL_CLEARED)
2207 && (tcall->rprev > rxi_HardAckRate)) {
2221 /* we can't schedule a call if there's no data!!! */
2222 /* send an ack if there's no data, if we're missing the
2223 * first packet, or we're missing something between first
2224 * and last -- there's a "hole" in the incoming data. */
2225 if (queue_IsEmpty(&call->rq)
2226 || queue_First(&call->rq, rx_packet)->header.seq != 1
2227 || call->rprev != queue_Last(&call->rq, rx_packet)->header.seq)
2228 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
2230 call->flags &= (~RX_CALL_WAIT_PROC);
2231 service->nRequestsRunning++;
2232 /* just started call in minProcs pool, need fewer to maintain
2234 MUTEX_ENTER(&rx_quota_mutex);
2235 if (service->nRequestsRunning <= service->minProcs)
2238 MUTEX_EXIT(&rx_quota_mutex);
2239 rx_atomic_dec(&rx_nWaiting);
2240 /* MUTEX_EXIT(&call->lock); */
2242 /* If there are no eligible incoming calls, add this process
2243 * to the idle server queue, to wait for one */
2246 *socketp = OSI_NULLSOCKET;
2248 sq->socketp = socketp;
2249 queue_Append(&rx_idleServerQueue, sq);
2253 if (afs_termState == AFSOP_STOP_RXCALLBACK) {
2255 rxi_Free(sq, sizeof(struct rx_serverQueueEntry));
2256 return (struct rx_call *)0;
2259 } while (!(call = sq->newcall)
2260 && !(socketp && *socketp != OSI_NULLSOCKET));
2262 MUTEX_EXIT(&sq->lock);
2264 MUTEX_ENTER(&freeSQEList_lock);
2265 *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
2266 rx_FreeSQEList = sq;
2267 MUTEX_EXIT(&freeSQEList_lock);
2270 clock_GetTime(&call->startTime);
2271 call->state = RX_STATE_ACTIVE;
2272 call->mode = RX_MODE_RECEIVING;
2273 #ifdef RX_KERNEL_TRACE
2274 if (ICL_SETACTIVE(afs_iclSetp)) {
2275 int glockOwner = ISAFS_GLOCK();
2278 afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
2279 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2286 rxi_calltrace(RX_CALL_START, call);
2287 dpf(("rx_GetCall(port=%d, service=%d) ==> call %p\n",
2288 call->conn->service->servicePort, call->conn->service->serviceId,
2291 dpf(("rx_GetCall(socketp=%p, *socketp=0x%x)\n", socketp, *socketp));
2298 #endif /* RX_ENABLE_LOCKS */
2302 /* Establish a procedure to be called when a packet arrives for a
2303 * call. This routine will be called at most once after each call,
2304 * and will also be called if there is an error condition on the or
2305 * the call is complete. Used by multi rx to build a selection
2306 * function which determines which of several calls is likely to be a
2307 * good one to read from.
2308 * NOTE: the way this is currently implemented it is probably only a
2309 * good idea to (1) use it immediately after a newcall (clients only)
2310 * and (2) only use it once. Other uses currently void your warranty
2313 rx_SetArrivalProc(struct rx_call *call,
2314 void (*proc) (struct rx_call * call,
2317 void * handle, int arg)
2319 call->arrivalProc = proc;
2320 call->arrivalProcHandle = handle;
2321 call->arrivalProcArg = arg;
2324 /* Call is finished (possibly prematurely). Return rc to the peer, if
2325 * appropriate, and return the final error code from the conversation
2329 rx_EndCall(struct rx_call *call, afs_int32 rc)
2331 struct rx_connection *conn = call->conn;
2335 dpf(("rx_EndCall(call %"AFS_PTR_FMT" rc %d error %d abortCode %d)\n",
2336 call, rc, call->error, call->abortCode));
2339 MUTEX_ENTER(&call->lock);
2341 if (rc == 0 && call->error == 0) {
2342 call->abortCode = 0;
2343 call->abortCount = 0;
2346 call->arrivalProc = (void (*)())0;
2347 if (rc && call->error == 0) {
2348 rxi_CallError(call, rc);
2349 call->mode = RX_MODE_ERROR;
2350 /* Send an abort message to the peer if this error code has
2351 * only just been set. If it was set previously, assume the
2352 * peer has already been sent the error code or will request it
2354 rxi_SendCallAbort(call, (struct rx_packet *)0, 0, 0);
2356 if (conn->type == RX_SERVER_CONNECTION) {
2357 /* Make sure reply or at least dummy reply is sent */
2358 if (call->mode == RX_MODE_RECEIVING) {
2359 MUTEX_EXIT(&call->lock);
2360 rxi_WriteProc(call, 0, 0);
2361 MUTEX_ENTER(&call->lock);
2363 if (call->mode == RX_MODE_SENDING) {
2364 MUTEX_EXIT(&call->lock);
2365 rxi_FlushWrite(call);
2366 MUTEX_ENTER(&call->lock);
2368 rxi_calltrace(RX_CALL_END, call);
2369 /* Call goes to hold state until reply packets are acknowledged */
2370 if (call->tfirst + call->nSoftAcked < call->tnext) {
2371 call->state = RX_STATE_HOLD;
2373 call->state = RX_STATE_DALLY;
2374 rxi_ClearTransmitQueue(call, 0);
2375 rxi_rto_cancel(call);
2376 rxevent_Cancel(&call->keepAliveEvent, call,
2377 RX_CALL_REFCOUNT_ALIVE);
2379 } else { /* Client connection */
2381 /* Make sure server receives input packets, in the case where
2382 * no reply arguments are expected */
2383 if ((call->mode == RX_MODE_SENDING)
2384 || (call->mode == RX_MODE_RECEIVING && call->rnext == 1)) {
2385 MUTEX_EXIT(&call->lock);
2386 (void)rxi_ReadProc(call, &dummy, 1);
2387 MUTEX_ENTER(&call->lock);
2390 /* If we had an outstanding delayed ack, be nice to the server
2391 * and force-send it now.
2393 if (call->delayedAckEvent) {
2394 rxevent_Cancel(&call->delayedAckEvent, call,
2395 RX_CALL_REFCOUNT_DELAY);
2396 rxi_SendDelayedAck(NULL, call, NULL, 0);
2399 /* We need to release the call lock since it's lower than the
2400 * conn_call_lock and we don't want to hold the conn_call_lock
2401 * over the rx_ReadProc call. The conn_call_lock needs to be held
2402 * here for the case where rx_NewCall is perusing the calls on
2403 * the connection structure. We don't want to signal until
2404 * rx_NewCall is in a stable state. Otherwise, rx_NewCall may
2405 * have checked this call, found it active and by the time it
2406 * goes to sleep, will have missed the signal.
2408 MUTEX_EXIT(&call->lock);
2409 MUTEX_ENTER(&conn->conn_call_lock);
2410 MUTEX_ENTER(&call->lock);
2412 if (!(call->flags & RX_CALL_PEER_BUSY)) {
2413 conn->lastBusy[call->channel] = 0;
2416 MUTEX_ENTER(&conn->conn_data_lock);
2417 conn->flags |= RX_CONN_BUSY;
2418 if (conn->flags & RX_CONN_MAKECALL_WAITING) {
2419 MUTEX_EXIT(&conn->conn_data_lock);
2420 #ifdef RX_ENABLE_LOCKS
2421 CV_BROADCAST(&conn->conn_call_cv);
2426 #ifdef RX_ENABLE_LOCKS
2428 MUTEX_EXIT(&conn->conn_data_lock);
2430 #endif /* RX_ENABLE_LOCKS */
2431 call->state = RX_STATE_DALLY;
2433 error = call->error;
2435 /* currentPacket, nLeft, and NFree must be zeroed here, because
2436 * ResetCall cannot: ResetCall may be called at splnet(), in the
2437 * kernel version, and may interrupt the macros rx_Read or
2438 * rx_Write, which run at normal priority for efficiency. */
2439 if (call->currentPacket) {
2440 #ifdef RX_TRACK_PACKETS
2441 call->currentPacket->flags &= ~RX_PKTFLAG_CP;
2443 rxi_FreePacket(call->currentPacket);
2444 call->currentPacket = (struct rx_packet *)0;
2447 call->nLeft = call->nFree = call->curlen = 0;
2449 /* Free any packets from the last call to ReadvProc/WritevProc */
2450 #ifdef RXDEBUG_PACKET
2452 #endif /* RXDEBUG_PACKET */
2453 rxi_FreePackets(0, &call->iovq);
2454 MUTEX_EXIT(&call->lock);
2456 CALL_RELE(call, RX_CALL_REFCOUNT_BEGIN);
2457 if (conn->type == RX_CLIENT_CONNECTION) {
2458 MUTEX_ENTER(&conn->conn_data_lock);
2459 conn->flags &= ~RX_CONN_BUSY;
2460 MUTEX_EXIT(&conn->conn_data_lock);
2461 MUTEX_EXIT(&conn->conn_call_lock);
2465 * Map errors to the local host's errno.h format.
2467 error = ntoh_syserr_conv(error);
2471 #if !defined(KERNEL)
2473 /* Call this routine when shutting down a server or client (especially
2474 * clients). This will allow Rx to gracefully garbage collect server
2475 * connections, and reduce the number of retries that a server might
2476 * make to a dead client.
2477 * This is not quite right, since some calls may still be ongoing and
2478 * we can't lock them to destroy them. */
2482 struct rx_connection **conn_ptr, **conn_end;
2486 if (rxinit_status == 1) {
2488 return; /* Already shutdown. */
2490 rxi_DeleteCachedConnections();
2491 if (rx_connHashTable) {
2492 MUTEX_ENTER(&rx_connHashTable_lock);
2493 for (conn_ptr = &rx_connHashTable[0], conn_end =
2494 &rx_connHashTable[rx_hashTableSize]; conn_ptr < conn_end;
2496 struct rx_connection *conn, *next;
2497 for (conn = *conn_ptr; conn; conn = next) {
2499 if (conn->type == RX_CLIENT_CONNECTION) {
2500 MUTEX_ENTER(&rx_refcnt_mutex);
2502 MUTEX_EXIT(&rx_refcnt_mutex);
2503 #ifdef RX_ENABLE_LOCKS
2504 rxi_DestroyConnectionNoLock(conn);
2505 #else /* RX_ENABLE_LOCKS */
2506 rxi_DestroyConnection(conn);
2507 #endif /* RX_ENABLE_LOCKS */
2511 #ifdef RX_ENABLE_LOCKS
2512 while (rx_connCleanup_list) {
2513 struct rx_connection *conn;
2514 conn = rx_connCleanup_list;
2515 rx_connCleanup_list = rx_connCleanup_list->next;
2516 MUTEX_EXIT(&rx_connHashTable_lock);
2517 rxi_CleanupConnection(conn);
2518 MUTEX_ENTER(&rx_connHashTable_lock);
2520 MUTEX_EXIT(&rx_connHashTable_lock);
2521 #endif /* RX_ENABLE_LOCKS */
2526 afs_winsockCleanup();
2534 /* if we wakeup packet waiter too often, can get in loop with two
2535 AllocSendPackets each waking each other up (from ReclaimPacket calls) */
2537 rxi_PacketsUnWait(void)
2539 if (!rx_waitingForPackets) {
2543 if (rxi_OverQuota(RX_PACKET_CLASS_SEND)) {
2544 return; /* still over quota */
2547 rx_waitingForPackets = 0;
2548 #ifdef RX_ENABLE_LOCKS
2549 CV_BROADCAST(&rx_waitingForPackets_cv);
2551 osi_rxWakeup(&rx_waitingForPackets);
2557 /* ------------------Internal interfaces------------------------- */
2559 /* Return this process's service structure for the
2560 * specified socket and service */
2561 static struct rx_service *
2562 rxi_FindService(osi_socket socket, u_short serviceId)
2564 struct rx_service **sp;
2565 for (sp = &rx_services[0]; *sp; sp++) {
2566 if ((*sp)->serviceId == serviceId && (*sp)->socket == socket)
2572 #ifdef RXDEBUG_PACKET
2573 #ifdef KDUMP_RX_LOCK
2574 static struct rx_call_rx_lock *rx_allCallsp = 0;
2576 static struct rx_call *rx_allCallsp = 0;
2578 #endif /* RXDEBUG_PACKET */
2580 /* Allocate a call structure, for the indicated channel of the
2581 * supplied connection. The mode and state of the call must be set by
2582 * the caller. Returns the call with mutex locked. */
2583 static struct rx_call *
2584 rxi_NewCall(struct rx_connection *conn, int channel)
2586 struct rx_call *call;
2587 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2588 struct rx_call *cp; /* Call pointer temp */
2589 struct rx_call *nxp; /* Next call pointer, for queue_Scan */
2590 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2592 dpf(("rxi_NewCall(conn %"AFS_PTR_FMT", channel %d)\n", conn, channel));
2594 /* Grab an existing call structure, or allocate a new one.
2595 * Existing call structures are assumed to have been left reset by
2597 MUTEX_ENTER(&rx_freeCallQueue_lock);
2599 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2601 * EXCEPT that the TQ might not yet be cleared out.
2602 * Skip over those with in-use TQs.
2605 for (queue_Scan(&rx_freeCallQueue, cp, nxp, rx_call)) {
2606 if (!(cp->flags & RX_CALL_TQ_BUSY)) {
2612 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2613 if (queue_IsNotEmpty(&rx_freeCallQueue)) {
2614 call = queue_First(&rx_freeCallQueue, rx_call);
2615 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2617 if (rx_stats_active)
2618 rx_atomic_dec(&rx_stats.nFreeCallStructs);
2619 MUTEX_EXIT(&rx_freeCallQueue_lock);
2620 MUTEX_ENTER(&call->lock);
2621 CLEAR_CALL_QUEUE_LOCK(call);
2622 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2623 /* Now, if TQ wasn't cleared earlier, do it now. */
2624 rxi_WaitforTQBusy(call);
2625 if (call->flags & RX_CALL_TQ_CLEARME) {
2626 rxi_ClearTransmitQueue(call, 1);
2627 /*queue_Init(&call->tq);*/
2629 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2630 /* Bind the call to its connection structure */
2632 rxi_ResetCall(call, 1);
2635 call = rxi_Alloc(sizeof(struct rx_call));
2636 #ifdef RXDEBUG_PACKET
2637 call->allNextp = rx_allCallsp;
2638 rx_allCallsp = call;
2640 rx_atomic_inc_and_read(&rx_stats.nCallStructs);
2641 #else /* RXDEBUG_PACKET */
2642 rx_atomic_inc(&rx_stats.nCallStructs);
2643 #endif /* RXDEBUG_PACKET */
2645 MUTEX_EXIT(&rx_freeCallQueue_lock);
2646 MUTEX_INIT(&call->lock, "call lock", MUTEX_DEFAULT, NULL);
2647 MUTEX_ENTER(&call->lock);
2648 CV_INIT(&call->cv_twind, "call twind", CV_DEFAULT, 0);
2649 CV_INIT(&call->cv_rq, "call rq", CV_DEFAULT, 0);
2650 CV_INIT(&call->cv_tq, "call tq", CV_DEFAULT, 0);
2652 /* Initialize once-only items */
2653 queue_Init(&call->tq);
2654 queue_Init(&call->rq);
2655 queue_Init(&call->iovq);
2656 #ifdef RXDEBUG_PACKET
2657 call->rqc = call->tqc = call->iovqc = 0;
2658 #endif /* RXDEBUG_PACKET */
2659 /* Bind the call to its connection structure (prereq for reset) */
2661 rxi_ResetCall(call, 1);
2663 call->channel = channel;
2664 call->callNumber = &conn->callNumber[channel];
2665 call->rwind = conn->rwind[channel];
2666 call->twind = conn->twind[channel];
2667 /* Note that the next expected call number is retained (in
2668 * conn->callNumber[i]), even if we reallocate the call structure
2670 conn->call[channel] = call;
2671 /* if the channel's never been used (== 0), we should start at 1, otherwise
2672 * the call number is valid from the last time this channel was used */
2673 if (*call->callNumber == 0)
2674 *call->callNumber = 1;
2679 /* A call has been inactive long enough that so we can throw away
2680 * state, including the call structure, which is placed on the call
2683 * call->lock amd rx_refcnt_mutex are held upon entry.
2684 * haveCTLock is set when called from rxi_ReapConnections.
2686 * return 1 if the call is freed, 0 if not.
2689 rxi_FreeCall(struct rx_call *call, int haveCTLock)
2691 int channel = call->channel;
2692 struct rx_connection *conn = call->conn;
2693 u_char state = call->state;
2696 * We are setting the state to RX_STATE_RESET to
2697 * ensure that no one else will attempt to use this
2698 * call once we drop the refcnt lock. We must drop
2699 * the refcnt lock before calling rxi_ResetCall
2700 * because it cannot be held across acquiring the
2701 * freepktQ lock. NewCall does the same.
2703 call->state = RX_STATE_RESET;
2704 MUTEX_EXIT(&rx_refcnt_mutex);
2705 rxi_ResetCall(call, 0);
2707 if (MUTEX_TRYENTER(&conn->conn_call_lock))
2709 if (state == RX_STATE_DALLY || state == RX_STATE_HOLD)
2710 (*call->callNumber)++;
2712 if (call->conn->call[channel] == call)
2713 call->conn->call[channel] = 0;
2714 MUTEX_EXIT(&conn->conn_call_lock);
2717 * We couldn't obtain the conn_call_lock so we can't
2718 * disconnect the call from the connection. Set the
2719 * call state to dally so that the call can be reused.
2721 MUTEX_ENTER(&rx_refcnt_mutex);
2722 call->state = RX_STATE_DALLY;
2726 MUTEX_ENTER(&rx_freeCallQueue_lock);
2727 SET_CALL_QUEUE_LOCK(call, &rx_freeCallQueue_lock);
2728 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
2729 /* A call may be free even though its transmit queue is still in use.
2730 * Since we search the call list from head to tail, put busy calls at
2731 * the head of the list, and idle calls at the tail.
2733 if (call->flags & RX_CALL_TQ_BUSY)
2734 queue_Prepend(&rx_freeCallQueue, call);
2736 queue_Append(&rx_freeCallQueue, call);
2737 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
2738 queue_Append(&rx_freeCallQueue, call);
2739 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
2740 if (rx_stats_active)
2741 rx_atomic_inc(&rx_stats.nFreeCallStructs);
2742 MUTEX_EXIT(&rx_freeCallQueue_lock);
2744 /* Destroy the connection if it was previously slated for
2745 * destruction, i.e. the Rx client code previously called
2746 * rx_DestroyConnection (client connections), or
2747 * rxi_ReapConnections called the same routine (server
2748 * connections). Only do this, however, if there are no
2749 * outstanding calls. Note that for fine grain locking, there appears
2750 * to be a deadlock in that rxi_FreeCall has a call locked and
2751 * DestroyConnectionNoLock locks each call in the conn. But note a
2752 * few lines up where we have removed this call from the conn.
2753 * If someone else destroys a connection, they either have no
2754 * call lock held or are going through this section of code.
2756 MUTEX_ENTER(&conn->conn_data_lock);
2757 if (conn->flags & RX_CONN_DESTROY_ME && !(conn->flags & RX_CONN_MAKECALL_WAITING)) {
2758 MUTEX_ENTER(&rx_refcnt_mutex);
2760 MUTEX_EXIT(&rx_refcnt_mutex);
2761 MUTEX_EXIT(&conn->conn_data_lock);
2762 #ifdef RX_ENABLE_LOCKS
2764 rxi_DestroyConnectionNoLock(conn);
2766 rxi_DestroyConnection(conn);
2767 #else /* RX_ENABLE_LOCKS */
2768 rxi_DestroyConnection(conn);
2769 #endif /* RX_ENABLE_LOCKS */
2771 MUTEX_EXIT(&conn->conn_data_lock);
2773 MUTEX_ENTER(&rx_refcnt_mutex);
2777 rx_atomic_t rxi_Allocsize = RX_ATOMIC_INIT(0);
2778 rx_atomic_t rxi_Alloccnt = RX_ATOMIC_INIT(0);
2781 rxi_Alloc(size_t size)
2785 if (rx_stats_active) {
2786 rx_atomic_add(&rxi_Allocsize, (int) size);
2787 rx_atomic_inc(&rxi_Alloccnt);
2791 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
2792 afs_osi_Alloc_NoSleep(size);
2797 osi_Panic("rxi_Alloc error");
2803 rxi_Free(void *addr, size_t size)
2805 if (rx_stats_active) {
2806 rx_atomic_sub(&rxi_Allocsize, (int) size);
2807 rx_atomic_dec(&rxi_Alloccnt);
2809 osi_Free(addr, size);
2813 rxi_SetPeerMtu(struct rx_peer *peer, afs_uint32 host, afs_uint32 port, int mtu)
2815 struct rx_peer **peer_ptr = NULL, **peer_end = NULL;
2816 struct rx_peer *next = NULL;
2820 MUTEX_ENTER(&rx_peerHashTable_lock);
2822 peer_ptr = &rx_peerHashTable[0];
2823 peer_end = &rx_peerHashTable[rx_hashTableSize];
2826 for ( ; peer_ptr < peer_end; peer_ptr++) {
2829 for ( ; peer; peer = next) {
2831 if (host == peer->host)
2836 hashIndex = PEER_HASH(host, port);
2837 for (peer = rx_peerHashTable[hashIndex]; peer; peer = peer->next) {
2838 if ((peer->host == host) && (peer->port == port))
2843 MUTEX_ENTER(&rx_peerHashTable_lock);
2848 MUTEX_EXIT(&rx_peerHashTable_lock);
2850 MUTEX_ENTER(&peer->peer_lock);
2851 /* We don't handle dropping below min, so don't */
2852 mtu = MAX(mtu, RX_MIN_PACKET_SIZE);
2853 peer->ifMTU=MIN(mtu, peer->ifMTU);
2854 peer->natMTU = rxi_AdjustIfMTU(peer->ifMTU);
2855 /* if we tweaked this down, need to tune our peer MTU too */
2856 peer->MTU = MIN(peer->MTU, peer->natMTU);
2857 /* if we discovered a sub-1500 mtu, degrade */
2858 if (peer->ifMTU < OLD_MAX_PACKET_SIZE)
2859 peer->maxDgramPackets = 1;
2860 /* We no longer have valid peer packet information */
2861 if (peer->maxPacketSize-RX_IPUDP_SIZE > peer->ifMTU)
2862 peer->maxPacketSize = 0;
2863 MUTEX_EXIT(&peer->peer_lock);
2865 MUTEX_ENTER(&rx_peerHashTable_lock);
2867 if (host && !port) {
2869 /* pick up where we left off */
2873 MUTEX_EXIT(&rx_peerHashTable_lock);
2876 /* Find the peer process represented by the supplied (host,port)
2877 * combination. If there is no appropriate active peer structure, a
2878 * new one will be allocated and initialized
2879 * The origPeer, if set, is a pointer to a peer structure on which the
2880 * refcount will be be decremented. This is used to replace the peer
2881 * structure hanging off a connection structure */
2883 rxi_FindPeer(afs_uint32 host, u_short port,
2884 struct rx_peer *origPeer, int create)
2888 hashIndex = PEER_HASH(host, port);
2889 MUTEX_ENTER(&rx_peerHashTable_lock);
2890 for (pp = rx_peerHashTable[hashIndex]; pp; pp = pp->next) {
2891 if ((pp->host == host) && (pp->port == port))
2896 pp = rxi_AllocPeer(); /* This bzero's *pp */
2897 pp->host = host; /* set here or in InitPeerParams is zero */
2899 MUTEX_INIT(&pp->peer_lock, "peer_lock", MUTEX_DEFAULT, 0);
2900 queue_Init(&pp->congestionQueue);
2901 queue_Init(&pp->rpcStats);
2902 pp->next = rx_peerHashTable[hashIndex];
2903 rx_peerHashTable[hashIndex] = pp;
2904 rxi_InitPeerParams(pp);
2905 if (rx_stats_active)
2906 rx_atomic_inc(&rx_stats.nPeerStructs);
2913 origPeer->refCount--;
2914 MUTEX_EXIT(&rx_peerHashTable_lock);
2919 /* Find the connection at (host, port) started at epoch, and with the
2920 * given connection id. Creates the server connection if necessary.
2921 * The type specifies whether a client connection or a server
2922 * connection is desired. In both cases, (host, port) specify the
2923 * peer's (host, pair) pair. Client connections are not made
2924 * automatically by this routine. The parameter socket gives the
2925 * socket descriptor on which the packet was received. This is used,
2926 * in the case of server connections, to check that *new* connections
2927 * come via a valid (port, serviceId). Finally, the securityIndex
2928 * parameter must match the existing index for the connection. If a
2929 * server connection is created, it will be created using the supplied
2930 * index, if the index is valid for this service */
2931 struct rx_connection *
2932 rxi_FindConnection(osi_socket socket, afs_uint32 host,
2933 u_short port, u_short serviceId, afs_uint32 cid,
2934 afs_uint32 epoch, int type, u_int securityIndex)
2936 int hashindex, flag, i;
2937 struct rx_connection *conn;
2938 hashindex = CONN_HASH(host, port, cid, epoch, type);
2939 MUTEX_ENTER(&rx_connHashTable_lock);
2940 rxLastConn ? (conn = rxLastConn, flag = 0) : (conn =
2941 rx_connHashTable[hashindex],
2944 if ((conn->type == type) && ((cid & RX_CIDMASK) == conn->cid)
2945 && (epoch == conn->epoch)) {
2946 struct rx_peer *pp = conn->peer;
2947 if (securityIndex != conn->securityIndex) {
2948 /* this isn't supposed to happen, but someone could forge a packet
2949 * like this, and there seems to be some CM bug that makes this
2950 * happen from time to time -- in which case, the fileserver
2952 MUTEX_EXIT(&rx_connHashTable_lock);
2953 return (struct rx_connection *)0;
2955 if (pp->host == host && pp->port == port)
2957 if (type == RX_CLIENT_CONNECTION && pp->port == port)
2959 /* So what happens when it's a callback connection? */
2960 if ( /*type == RX_CLIENT_CONNECTION && */
2961 (conn->epoch & 0x80000000))
2965 /* the connection rxLastConn that was used the last time is not the
2966 ** one we are looking for now. Hence, start searching in the hash */
2968 conn = rx_connHashTable[hashindex];
2973 struct rx_service *service;
2974 if (type == RX_CLIENT_CONNECTION) {
2975 MUTEX_EXIT(&rx_connHashTable_lock);
2976 return (struct rx_connection *)0;
2978 service = rxi_FindService(socket, serviceId);
2979 if (!service || (securityIndex >= service->nSecurityObjects)
2980 || (service->securityObjects[securityIndex] == 0)) {
2981 MUTEX_EXIT(&rx_connHashTable_lock);
2982 return (struct rx_connection *)0;
2984 conn = rxi_AllocConnection(); /* This bzero's the connection */
2985 MUTEX_INIT(&conn->conn_call_lock, "conn call lock", MUTEX_DEFAULT, 0);
2986 MUTEX_INIT(&conn->conn_data_lock, "conn data lock", MUTEX_DEFAULT, 0);
2987 CV_INIT(&conn->conn_call_cv, "conn call cv", CV_DEFAULT, 0);
2988 conn->next = rx_connHashTable[hashindex];
2989 rx_connHashTable[hashindex] = conn;
2990 conn->peer = rxi_FindPeer(host, port, 0, 1);
2991 conn->type = RX_SERVER_CONNECTION;
2992 conn->lastSendTime = clock_Sec(); /* don't GC immediately */
2993 conn->epoch = epoch;
2994 conn->cid = cid & RX_CIDMASK;
2995 /* conn->serial = conn->lastSerial = 0; */
2996 /* conn->timeout = 0; */
2997 conn->ackRate = RX_FAST_ACK_RATE;
2998 conn->service = service;
2999 conn->serviceId = serviceId;
3000 conn->securityIndex = securityIndex;
3001 conn->securityObject = service->securityObjects[securityIndex];
3002 conn->nSpecific = 0;
3003 conn->specific = NULL;
3004 rx_SetConnDeadTime(conn, service->connDeadTime);
3005 conn->idleDeadTime = service->idleDeadTime;
3006 conn->idleDeadDetection = service->idleDeadErr ? 1 : 0;
3007 for (i = 0; i < RX_MAXCALLS; i++) {
3008 conn->twind[i] = rx_initSendWindow;
3009 conn->rwind[i] = rx_initReceiveWindow;
3011 /* Notify security object of the new connection */
3012 RXS_NewConnection(conn->securityObject, conn);
3013 /* XXXX Connection timeout? */
3014 if (service->newConnProc)
3015 (*service->newConnProc) (conn);
3016 if (rx_stats_active)
3017 rx_atomic_inc(&rx_stats.nServerConns);
3020 MUTEX_ENTER(&rx_refcnt_mutex);
3022 MUTEX_EXIT(&rx_refcnt_mutex);
3024 rxLastConn = conn; /* store this connection as the last conn used */
3025 MUTEX_EXIT(&rx_connHashTable_lock);
3030 * Timeout a call on a busy call channel if appropriate.
3032 * @param[in] call The busy call.
3034 * @pre 'call' is marked as busy (namely,
3035 * call->conn->lastBusy[call->channel] != 0)
3037 * @pre call->lock is held
3038 * @pre rxi_busyChannelError is nonzero
3040 * @note call->lock is dropped and reacquired
3043 rxi_CheckBusy(struct rx_call *call)
3045 struct rx_connection *conn = call->conn;
3046 int channel = call->channel;
3047 int freechannel = 0;
3049 afs_uint32 callNumber;
3051 MUTEX_EXIT(&call->lock);
3053 MUTEX_ENTER(&conn->conn_call_lock);
3054 callNumber = *call->callNumber;
3056 /* Are there any other call slots on this conn that we should try? Look for
3057 * slots that are empty and are either non-busy, or were marked as busy
3058 * longer than conn->secondsUntilDead seconds before this call started. */
3060 for (i = 0; i < RX_MAXCALLS && !freechannel; i++) {
3062 /* only look at channels that aren't us */
3066 if (conn->lastBusy[i]) {
3067 /* if this channel looked busy too recently, don't look at it */
3068 if (conn->lastBusy[i] >= call->startTime.sec) {
3071 if (call->startTime.sec - conn->lastBusy[i] < conn->secondsUntilDead) {
3076 if (conn->call[i]) {
3077 struct rx_call *tcall = conn->call[i];
3078 MUTEX_ENTER(&tcall->lock);
3079 if (tcall->state == RX_STATE_DALLY) {
3082 MUTEX_EXIT(&tcall->lock);
3088 MUTEX_ENTER(&call->lock);
3090 /* Since the call->lock and conn->conn_call_lock have been released it is
3091 * possible that (1) the call may no longer be busy and/or (2) the call may
3092 * have been reused by another waiting thread. Therefore, we must confirm
3093 * that the call state has not changed when deciding whether or not to
3094 * force this application thread to retry by forcing a Timeout error. */
3096 if (freechannel && *call->callNumber == callNumber &&
3097 (call->flags & RX_CALL_PEER_BUSY)) {
3098 /* Since 'freechannel' is set, there exists another channel in this
3099 * rx_conn that the application thread might be able to use. We know
3100 * that we have the correct call since callNumber is unchanged, and we
3101 * know that the call is still busy. So, set the call error state to
3102 * rxi_busyChannelError so the application can retry the request,
3103 * presumably on a less-busy call channel. */
3105 rxi_CallError(call, RX_CALL_BUSY);
3107 MUTEX_EXIT(&conn->conn_call_lock);
3110 /* There are two packet tracing routines available for testing and monitoring
3111 * Rx. One is called just after every packet is received and the other is
3112 * called just before every packet is sent. Received packets, have had their
3113 * headers decoded, and packets to be sent have not yet had their headers
3114 * encoded. Both take two parameters: a pointer to the packet and a sockaddr
3115 * containing the network address. Both can be modified. The return value, if
3116 * non-zero, indicates that the packet should be dropped. */
3118 int (*rx_justReceived) (struct rx_packet *, struct sockaddr_in *) = 0;
3119 int (*rx_almostSent) (struct rx_packet *, struct sockaddr_in *) = 0;
3121 /* A packet has been received off the interface. Np is the packet, socket is
3122 * the socket number it was received from (useful in determining which service
3123 * this packet corresponds to), and (host, port) reflect the host,port of the
3124 * sender. This call returns the packet to the caller if it is finished with
3125 * it, rather than de-allocating it, just as a small performance hack */
3128 rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
3129 afs_uint32 host, u_short port, int *tnop,
3130 struct rx_call **newcallp)
3132 struct rx_call *call;
3133 struct rx_connection *conn;
3135 afs_uint32 currentCallNumber;
3141 struct rx_packet *tnp;
3144 /* We don't print out the packet until now because (1) the time may not be
3145 * accurate enough until now in the lwp implementation (rx_Listener only gets
3146 * the time after the packet is read) and (2) from a protocol point of view,
3147 * this is the first time the packet has been seen */
3148 packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
3149 ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
3150 dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %"AFS_PTR_FMT"\n",
3151 np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
3152 np->header.epoch, np->header.cid, np->header.callNumber,
3153 np->header.seq, np->header.flags, np));
3156 if (np->header.type == RX_PACKET_TYPE_VERSION) {
3157 return rxi_ReceiveVersionPacket(np, socket, host, port, 1);
3160 if (np->header.type == RX_PACKET_TYPE_DEBUG) {
3161 return rxi_ReceiveDebugPacket(np, socket, host, port, 1);
3164 /* If an input tracer function is defined, call it with the packet and
3165 * network address. Note this function may modify its arguments. */
3166 if (rx_justReceived) {
3167 struct sockaddr_in addr;
3169 addr.sin_family = AF_INET;
3170 addr.sin_port = port;
3171 addr.sin_addr.s_addr = host;
3172 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
3173 addr.sin_len = sizeof(addr);
3174 #endif /* AFS_OSF_ENV */
3175 drop = (*rx_justReceived) (np, &addr);
3176 /* drop packet if return value is non-zero */
3179 port = addr.sin_port; /* in case fcn changed addr */
3180 host = addr.sin_addr.s_addr;
3184 /* If packet was not sent by the client, then *we* must be the client */
3185 type = ((np->header.flags & RX_CLIENT_INITIATED) != RX_CLIENT_INITIATED)
3186 ? RX_CLIENT_CONNECTION : RX_SERVER_CONNECTION;
3188 /* Find the connection (or fabricate one, if we're the server & if
3189 * necessary) associated with this packet */
3191 rxi_FindConnection(socket, host, port, np->header.serviceId,
3192 np->header.cid, np->header.epoch, type,
3193 np->header.securityIndex);
3196 /* If no connection found or fabricated, just ignore the packet.
3197 * (An argument could be made for sending an abort packet for
3202 /* If the connection is in an error state, send an abort packet and ignore
3203 * the incoming packet */
3205 /* Don't respond to an abort packet--we don't want loops! */
3206 MUTEX_ENTER(&conn->conn_data_lock);
3207 if (np->header.type != RX_PACKET_TYPE_ABORT)
3208 np = rxi_SendConnectionAbort(conn, np, 1, 0);
3209 putConnection(conn);
3210 MUTEX_EXIT(&conn->conn_data_lock);
3214 /* Check for connection-only requests (i.e. not call specific). */
3215 if (np->header.callNumber == 0) {
3216 switch (np->header.type) {
3217 case RX_PACKET_TYPE_ABORT: {
3218 /* What if the supplied error is zero? */
3219 afs_int32 errcode = ntohl(rx_GetInt32(np, 0));
3220 dpf(("rxi_ReceivePacket ABORT rx_GetInt32 = %d\n", errcode));
3221 rxi_ConnectionError(conn, errcode);
3222 putConnection(conn);
3225 case RX_PACKET_TYPE_CHALLENGE:
3226 tnp = rxi_ReceiveChallengePacket(conn, np, 1);
3227 putConnection(conn);
3229 case RX_PACKET_TYPE_RESPONSE:
3230 tnp = rxi_ReceiveResponsePacket(conn, np, 1);
3231 putConnection(conn);
3233 case RX_PACKET_TYPE_PARAMS:
3234 case RX_PACKET_TYPE_PARAMS + 1:
3235 case RX_PACKET_TYPE_PARAMS + 2:
3236 /* ignore these packet types for now */
3237 putConnection(conn);
3241 /* Should not reach here, unless the peer is broken: send an
3243 rxi_ConnectionError(conn, RX_PROTOCOL_ERROR);
3244 MUTEX_ENTER(&conn->conn_data_lock);
3245 tnp = rxi_SendConnectionAbort(conn, np, 1, 0);
3246 putConnection(conn);
3247 MUTEX_EXIT(&conn->conn_data_lock);
3252 channel = np->header.cid & RX_CHANNELMASK;
3253 MUTEX_ENTER(&conn->conn_call_lock);
3254 call = conn->call[channel];
3257 MUTEX_ENTER(&call->lock);
3258 currentCallNumber = conn->callNumber[channel];
3259 MUTEX_EXIT(&conn->conn_call_lock);
3260 } else if (type == RX_SERVER_CONNECTION) { /* No call allocated */
3261 call = conn->call[channel];
3263 MUTEX_ENTER(&call->lock);
3264 currentCallNumber = conn->callNumber[channel];
3265 MUTEX_EXIT(&conn->conn_call_lock);
3267 call = rxi_NewCall(conn, channel); /* returns locked call */
3268 *call->callNumber = currentCallNumber = np->header.callNumber;
3269 MUTEX_EXIT(&conn->conn_call_lock);
3271 if (np->header.callNumber == 0)
3272 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" len %d\n",
3273 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
3274 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
3275 np->header.flags, np, np->length));
3277 call->state = RX_STATE_PRECALL;
3278 clock_GetTime(&call->queueTime);
3279 hzero(call->bytesSent);
3280 hzero(call->bytesRcvd);
3282 * If the number of queued calls exceeds the overload
3283 * threshold then abort this call.
3285 if ((rx_BusyThreshold > 0) &&
3286 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
3287 struct rx_packet *tp;
3289 rxi_CallError(call, rx_BusyError);
3290 tp = rxi_SendCallAbort(call, np, 1, 0);
3291 MUTEX_EXIT(&call->lock);
3292 putConnection(conn);
3293 if (rx_stats_active)
3294 rx_atomic_inc(&rx_stats.nBusies);
3297 rxi_KeepAliveOn(call);
3299 } else { /* RX_CLIENT_CONNECTION and No call allocated */
3300 /* This packet can't be for this call. If the new call address is
3301 * 0 then no call is running on this channel. If there is a call
3302 * then, since this is a client connection we're getting data for
3303 * it must be for the previous call.
3305 if (rx_stats_active)
3306 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3307 putConnection(conn);
3311 /* There is a non-NULL locked call at this point */
3312 if (type == RX_SERVER_CONNECTION) { /* We're the server */
3313 if (np->header.callNumber < currentCallNumber) {
3314 MUTEX_EXIT(&call->lock);
3315 if (rx_stats_active)
3316 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3317 putConnection(conn);
3319 } else if (np->header.callNumber != currentCallNumber) {
3320 /* Wait until the transmit queue is idle before deciding
3321 * whether to reset the current call. Chances are that the
3322 * call will be in ether DALLY or HOLD state once the TQ_BUSY
3325 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3326 if (call->state == RX_STATE_ACTIVE) {
3327 rxi_WaitforTQBusy(call);
3329 * If we entered error state while waiting,
3330 * must call rxi_CallError to permit rxi_ResetCall
3331 * to processed when the tqWaiter count hits zero.
3334 rxi_CallError(call, call->error);
3335 MUTEX_EXIT(&call->lock);
3336 putConnection(conn);
3340 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3341 /* If the new call cannot be taken right now send a busy and set
3342 * the error condition in this call, so that it terminates as
3343 * quickly as possible */
3344 if (call->state == RX_STATE_ACTIVE) {
3345 struct rx_packet *tp;
3347 rxi_CallError(call, RX_CALL_DEAD);
3348 tp = rxi_SendSpecial(call, conn, np, RX_PACKET_TYPE_BUSY,
3350 MUTEX_EXIT(&call->lock);
3351 putConnection(conn);
3354 rxi_ResetCall(call, 0);
3356 * The conn_call_lock is not held but no one else should be
3357 * using this call channel while we are processing this incoming
3358 * packet. This assignment should be safe.
3360 *call->callNumber = np->header.callNumber;
3362 if (np->header.callNumber == 0)
3363 dpf(("RecPacket call 0 %d %s: %x.%u.%u.%u.%u.%u.%u flags %d, packet %"AFS_PTR_FMT" len %d\n",
3364 np->header.serial, rx_packetTypes[np->header.type - 1], ntohl(conn->peer->host), ntohs(conn->peer->port),
3365 np->header.serial, np->header.epoch, np->header.cid, np->header.callNumber, np->header.seq,
3366 np->header.flags, np, np->length));
3368 call->state = RX_STATE_PRECALL;
3369 clock_GetTime(&call->queueTime);
3370 hzero(call->bytesSent);
3371 hzero(call->bytesRcvd);
3373 * If the number of queued calls exceeds the overload
3374 * threshold then abort this call.
3376 if ((rx_BusyThreshold > 0) &&
3377 (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
3378 struct rx_packet *tp;
3380 rxi_CallError(call, rx_BusyError);
3381 tp = rxi_SendCallAbort(call, np, 1, 0);
3382 MUTEX_EXIT(&call->lock);
3383 putConnection(conn);
3384 if (rx_stats_active)
3385 rx_atomic_inc(&rx_stats.nBusies);
3388 rxi_KeepAliveOn(call);
3390 /* Continuing call; do nothing here. */
3392 } else { /* we're the client */
3393 /* Ignore all incoming acknowledgements for calls in DALLY state */
3394 if ((call->state == RX_STATE_DALLY)
3395 && (np->header.type == RX_PACKET_TYPE_ACK)) {
3396 if (rx_stats_active)
3397 rx_atomic_inc(&rx_stats.ignorePacketDally);
3398 MUTEX_EXIT(&call->lock);
3399 putConnection(conn);
3403 /* Ignore anything that's not relevant to the current call. If there
3404 * isn't a current call, then no packet is relevant. */
3405 if (np->header.callNumber != currentCallNumber) {
3406 if (rx_stats_active)
3407 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3408 MUTEX_EXIT(&call->lock);
3409 putConnection(conn);
3412 /* If the service security object index stamped in the packet does not
3413 * match the connection's security index, ignore the packet */
3414 if (np->header.securityIndex != conn->securityIndex) {
3415 MUTEX_EXIT(&call->lock);
3416 putConnection(conn);
3420 /* If we're receiving the response, then all transmit packets are
3421 * implicitly acknowledged. Get rid of them. */
3422 if (np->header.type == RX_PACKET_TYPE_DATA) {
3423 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3424 /* XXX Hack. Because we must release the global rx lock when
3425 * sending packets (osi_NetSend) we drop all acks while we're
3426 * traversing the tq in rxi_Start sending packets out because
3427 * packets may move to the freePacketQueue as result of being here!
3428 * So we drop these packets until we're safely out of the
3429 * traversing. Really ugly!
3430 * For fine grain RX locking, we set the acked field in the
3431 * packets and let rxi_Start remove them from the transmit queue.
3433 if (call->flags & RX_CALL_TQ_BUSY) {
3434 #ifdef RX_ENABLE_LOCKS
3435 rxi_SetAcksInTransmitQueue(call);
3437 putConnection(conn);
3438 return np; /* xmitting; drop packet */
3441 rxi_ClearTransmitQueue(call, 0);
3443 #else /* AFS_GLOBAL_RXLOCK_KERNEL */
3444 rxi_ClearTransmitQueue(call, 0);
3445 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3447 if (np->header.type == RX_PACKET_TYPE_ACK) {
3448 /* now check to see if this is an ack packet acknowledging that the
3449 * server actually *lost* some hard-acked data. If this happens we
3450 * ignore this packet, as it may indicate that the server restarted in
3451 * the middle of a call. It is also possible that this is an old ack
3452 * packet. We don't abort the connection in this case, because this
3453 * *might* just be an old ack packet. The right way to detect a server
3454 * restart in the midst of a call is to notice that the server epoch
3456 /* XXX I'm not sure this is exactly right, since tfirst **IS**
3457 * XXX unacknowledged. I think that this is off-by-one, but
3458 * XXX I don't dare change it just yet, since it will
3459 * XXX interact badly with the server-restart detection
3460 * XXX code in receiveackpacket. */
3461 if (ntohl(rx_GetInt32(np, FIRSTACKOFFSET)) < call->tfirst) {
3462 if (rx_stats_active)
3463 rx_atomic_inc(&rx_stats.spuriousPacketsRead);
3464 MUTEX_EXIT(&call->lock);
3465 putConnection(conn);
3469 } /* else not a data packet */
3472 osirx_AssertMine(&call->lock, "rxi_ReceivePacket middle");
3473 /* Set remote user defined status from packet */
3474 call->remoteStatus = np->header.userStatus;
3476 /* Note the gap between the expected next packet and the actual
3477 * packet that arrived, when the new packet has a smaller serial number
3478 * than expected. Rioses frequently reorder packets all by themselves,
3479 * so this will be quite important with very large window sizes.
3480 * Skew is checked against 0 here to avoid any dependence on the type of
3481 * inPacketSkew (which may be unsigned). In C, -1 > (unsigned) 0 is always
3483 * The inPacketSkew should be a smoothed running value, not just a maximum. MTUXXX
3484 * see CalculateRoundTripTime for an example of how to keep smoothed values.
3485 * I think using a beta of 1/8 is probably appropriate. 93.04.21
3487 MUTEX_ENTER(&conn->conn_data_lock);
3488 skew = conn->lastSerial - np->header.serial;
3489 conn->lastSerial = np->header.serial;
3490 MUTEX_EXIT(&conn->conn_data_lock);
3492 struct rx_peer *peer;
3494 if (skew > peer->inPacketSkew) {
3495 dpf(("*** In skew changed from %d to %d\n",
3496 peer->inPacketSkew, skew));
3497 peer->inPacketSkew = skew;
3501 /* Now do packet type-specific processing */
3502 switch (np->header.type) {
3503 case RX_PACKET_TYPE_DATA:
3504 np = rxi_ReceiveDataPacket(call, np, 1, socket, host, port, tnop,
3507 case RX_PACKET_TYPE_ACK:
3508 /* Respond immediately to ack packets requesting acknowledgement
3510 if (np->header.flags & RX_REQUEST_ACK) {
3512 (void)rxi_SendCallAbort(call, 0, 1, 0);
3514 (void)rxi_SendAck(call, 0, np->header.serial,
3515 RX_ACK_PING_RESPONSE, 1);
3517 np = rxi_ReceiveAckPacket(call, np, 1);
3519 case RX_PACKET_TYPE_ABORT: {
3520 /* An abort packet: reset the call, passing the error up to the user. */
3521 /* What if error is zero? */
3522 /* What if the error is -1? the application will treat it as a timeout. */
3523 afs_int32 errdata = ntohl(*(afs_int32 *) rx_DataOf(np));
3524 dpf(("rxi_ReceivePacket ABORT rx_DataOf = %d\n", errdata));
3525 rxi_CallError(call, errdata);
3526 MUTEX_EXIT(&call->lock);
3527 putConnection(conn);
3528 return np; /* xmitting; drop packet */
3530 case RX_PACKET_TYPE_BUSY: {
3531 struct clock busyTime;
3533 clock_GetTime(&busyTime);
3535 MUTEX_EXIT(&call->lock);
3537 MUTEX_ENTER(&conn->conn_call_lock);
3538 MUTEX_ENTER(&call->lock);
3539 conn->lastBusy[call->channel] = busyTime.sec;
3540 call->flags |= RX_CALL_PEER_BUSY;
3541 MUTEX_EXIT(&call->lock);
3542 MUTEX_EXIT(&conn->conn_call_lock);
3544 putConnection(conn);
3548 case RX_PACKET_TYPE_ACKALL:
3549 /* All packets acknowledged, so we can drop all packets previously
3550 * readied for sending */
3551 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
3552 /* XXX Hack. We because we can't release the global rx lock when
3553 * sending packets (osi_NetSend) we drop all ack pkts while we're
3554 * traversing the tq in rxi_Start sending packets out because
3555 * packets may move to the freePacketQueue as result of being
3556 * here! So we drop these packets until we're safely out of the
3557 * traversing. Really ugly!
3558 * For fine grain RX locking, we set the acked field in the packets
3559 * and let rxi_Start remove the packets from the transmit queue.
3561 if (call->flags & RX_CALL_TQ_BUSY) {
3562 #ifdef RX_ENABLE_LOCKS
3563 rxi_SetAcksInTransmitQueue(call);
3565 #else /* RX_ENABLE_LOCKS */
3566 MUTEX_EXIT(&call->lock);
3567 putConnection(conn);
3568 return np; /* xmitting; drop packet */
3569 #endif /* RX_ENABLE_LOCKS */
3571 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
3572 rxi_ClearTransmitQueue(call, 0);
3575 /* Should not reach here, unless the peer is broken: send an abort
3577 rxi_CallError(call, RX_PROTOCOL_ERROR);
3578 np = rxi_SendCallAbort(call, np, 1, 0);
3581 /* Note when this last legitimate packet was received, for keep-alive
3582 * processing. Note, we delay getting the time until now in the hope that
3583 * the packet will be delivered to the user before any get time is required
3584 * (if not, then the time won't actually be re-evaluated here). */
3585 call->lastReceiveTime = clock_Sec();
3586 /* we've received a legit packet, so the channel is not busy */
3587 call->flags &= ~RX_CALL_PEER_BUSY;
3588 MUTEX_EXIT(&call->lock);
3589 putConnection(conn);
3593 /* return true if this is an "interesting" connection from the point of view
3594 of someone trying to debug the system */
3596 rxi_IsConnInteresting(struct rx_connection *aconn)
3599 struct rx_call *tcall;
3601 if (aconn->flags & (RX_CONN_MAKECALL_WAITING | RX_CONN_DESTROY_ME))
3604 for (i = 0; i < RX_MAXCALLS; i++) {
3605 tcall = aconn->call[i];
3607 if ((tcall->state == RX_STATE_PRECALL)
3608 || (tcall->state == RX_STATE_ACTIVE))
3610 if ((tcall->mode == RX_MODE_SENDING)
3611 || (tcall->mode == RX_MODE_RECEIVING))
3619 /* if this is one of the last few packets AND it wouldn't be used by the
3620 receiving call to immediately satisfy a read request, then drop it on
3621 the floor, since accepting it might prevent a lock-holding thread from
3622 making progress in its reading. If a call has been cleared while in
3623 the precall state then ignore all subsequent packets until the call
3624 is assigned to a thread. */
3627 TooLow(struct rx_packet *ap, struct rx_call *acall)
3631 MUTEX_ENTER(&rx_quota_mutex);
3632 if (((ap->header.seq != 1) && (acall->flags & RX_CALL_CLEARED)
3633 && (acall->state == RX_STATE_PRECALL))
3634 || ((rx_nFreePackets < rxi_dataQuota + 2)
3635 && !((ap->header.seq < acall->rnext + rx_initSendWindow)
3636 && (acall->flags & RX_CALL_READER_WAIT)))) {
3639 MUTEX_EXIT(&rx_quota_mutex);
3645 * Clear the attach wait flag on a connection and proceed.
3647 * Any processing waiting for a connection to be attached should be
3648 * unblocked. We clear the flag and do any other needed tasks.
3651 * the conn to unmark waiting for attach
3653 * @pre conn's conn_data_lock must be locked before calling this function
3657 rxi_ConnClearAttachWait(struct rx_connection *conn)
3659 /* Indicate that rxi_CheckReachEvent is no longer running by
3660 * clearing the flag. Must be atomic under conn_data_lock to
3661 * avoid a new call slipping by: rxi_CheckConnReach holds
3662 * conn_data_lock while checking RX_CONN_ATTACHWAIT.
3664 conn->flags &= ~RX_CONN_ATTACHWAIT;
3665 if (conn->flags & RX_CONN_NAT_PING) {
3666 conn->flags &= ~RX_CONN_NAT_PING;
3667 rxi_ScheduleNatKeepAliveEvent(conn);
3672 rxi_CheckReachEvent(struct rxevent *event, void *arg1, void *arg2, int dummy)
3674 struct rx_connection *conn = arg1;
3675 struct rx_call *acall = arg2;
3676 struct rx_call *call = acall;
3677 struct clock when, now;
3680 MUTEX_ENTER(&conn->conn_data_lock);
3683 rxevent_Put(conn->checkReachEvent);
3684 conn->checkReachEvent = NULL;
3687 waiting = conn->flags & RX_CONN_ATTACHWAIT;
3689 putConnection(conn);
3691 MUTEX_EXIT(&conn->conn_data_lock);
3695 MUTEX_ENTER(&conn->conn_call_lock);
3696 MUTEX_ENTER(&conn->conn_data_lock);
3697 for (i = 0; i < RX_MAXCALLS; i++) {
3698 struct rx_call *tc = conn->call[i];
3699 if (tc && tc->state == RX_STATE_PRECALL) {
3705 rxi_ConnClearAttachWait(conn);
3706 MUTEX_EXIT(&conn->conn_data_lock);
3707 MUTEX_EXIT(&conn->conn_call_lock);
3712 MUTEX_ENTER(&call->lock);
3713 rxi_SendAck(call, NULL, 0, RX_ACK_PING, 0);
3715 MUTEX_EXIT(&call->lock);
3717 clock_GetTime(&now);
3719 when.sec += RX_CHECKREACH_TIMEOUT;
3720 MUTEX_ENTER(&conn->conn_data_lock);
3721 if (!conn->checkReachEvent) {
3722 MUTEX_ENTER(&rx_refcnt_mutex);
3724 MUTEX_EXIT(&rx_refcnt_mutex);
3725 conn->checkReachEvent = rxevent_Post(&when, &now,
3726 rxi_CheckReachEvent, conn,
3729 MUTEX_EXIT(&conn->conn_data_lock);
3735 rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
3737 struct rx_service *service = conn->service;
3738 struct rx_peer *peer = conn->peer;