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
11 * Implement caching of rx connections.
14 #include <afsconfig.h>
15 #include <afs/param.h>
17 #include <sys/types.h>
23 * We initialize rxi_connectionCache at compile time, so there is no
24 * need to call queue_Init(&rxi_connectionCache).
26 static struct rx_queue rxi_connectionCache = { &rxi_connectionCache,
30 #ifdef AFS_PTHREAD_ENV
33 * This mutex protects the following global variables:
37 afs_kmutex_t rxi_connCacheMutex;
38 #define LOCK_CONN_CACHE MUTEX_ENTER(&rxi_connCacheMutex)
39 #define UNLOCK_CONN_CACHE MUTEX_EXIT(&rxi_connCacheMutex)
41 #define LOCK_CONN_CACHE
42 #define UNLOCK_CONN_CACHE
43 #endif /* AFS_PTHREAD_ENV */
46 * convenience typedef - all the stuff that makes up an rx
50 typedef struct rx_connParts {
51 unsigned int hostAddr;
53 unsigned short service;
54 struct rx_securityClass *securityObject;
56 } rx_connParts_t, *rx_connParts_p;
59 * Each element in the cache is represented by the following
60 * structure. I use an rx_queue to manipulate the cache entries.
61 * inUse tracks the number of calls within this connection that
65 typedef struct cache_entry {
66 struct rx_queue queue_header;
67 struct rx_connection *conn;
71 } cache_entry_t, *cache_entry_p;
76 * rxi_connCacheMutex is the only mutex used by these functions. It should
77 * be locked when manipulating the connection cache.
85 * Compare two connections for equality
89 rxi_CachedConnectionsEqual(rx_connParts_p a, rx_connParts_p b)
91 return ((a->hostAddr == b->hostAddr) && (a->port == b->port)
92 && (a->service == b->service)
93 && (a->securityObject == b->securityObject)
94 && (a->securityIndex == b->securityIndex));
98 * Check the cache for a connection
102 rxi_FindCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
105 cache_entry_p cacheConn, nCacheConn;
107 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
108 if ((rxi_CachedConnectionsEqual(parts, &cacheConn->parts))
109 && (cacheConn->inUse < RX_MAXCALLS)
110 && (cacheConn->hasError == 0)) {
112 *conn = cacheConn->conn;
121 * Create an rx connection and return it to the caller
125 * Add a connection to the cache keeping track of the input
126 * arguments that were used to create it
130 rxi_AddCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
132 cache_entry_p new_entry;
134 if ((new_entry = (cache_entry_p) malloc(sizeof(cache_entry_t)))) {
135 new_entry->conn = *conn;
136 new_entry->parts = *parts;
137 new_entry->inUse = 1;
138 new_entry->hasError = 0;
139 queue_Prepend(&rxi_connectionCache, new_entry);
143 * if malloc fails, we fail silently
150 * Get a connection by first checking to see if any matching
151 * available connections are stored in the cache.
152 * Create a new connection if none are currently available.
156 rxi_GetCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
161 * Look to see if we have a cached connection
163 * Note - we hold the connection cache mutex for the entire
164 * search/create/enter operation. We want this entire block to
165 * be atomic so that in the event four threads all pass through
166 * this code at the same time only one actually allocates the
167 * new connection and the other three experience cache hits.
169 * We intentionally slow down throughput in order to
170 * increase the frequency of cache hits.
174 if (!rxi_FindCachedConnection(parts, conn)) {
176 * Create a new connection and enter it in the cache
179 rx_NewConnection(parts->hostAddr, parts->port, parts->service,
180 parts->securityObject, parts->securityIndex))) {
181 rxi_AddCachedConnection(parts, conn);
191 * Delete remaining entries in the cache.
192 * Note - only call this routine from rx_Finalize.
196 rxi_DeleteCachedConnections(void)
198 cache_entry_p cacheConn, nCacheConn;
201 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
204 queue_Remove(cacheConn);
205 rxi_DestroyConnection(cacheConn->conn);
216 * Hand back the caller a connection
217 * The function has the same foot print and return values
218 * as rx_NewConnection.
221 struct rx_connection *
222 rx_GetCachedConnection(unsigned int remoteAddr, unsigned short port,
223 unsigned short service,
224 struct rx_securityClass *securityObject,
227 struct rx_connection *conn = NULL;
228 rx_connParts_t parts;
230 parts.hostAddr = remoteAddr;
232 parts.service = service;
233 parts.securityObject = securityObject;
234 parts.securityIndex = securityIndex;
236 * Get a connection matching the user's request
237 * note we don't propagate the error returned by rxi_GetCachedConnection
238 * since rx_NewConnection doesn't return errors either.
240 rxi_GetCachedConnection(&parts, &conn);
246 * Release a connection we previously handed out
250 rx_ReleaseCachedConnection(struct rx_connection *conn)
252 cache_entry_p cacheConn, nCacheConn;
255 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
256 if (conn == cacheConn->conn) {
259 * check to see if the connection is in error.
260 * If it is, mark its cache entry so it won't be
261 * given out subsequently. If nobody is using it, delete
264 if (rx_ConnError(conn)) {
265 cacheConn->hasError = 1;
266 if (cacheConn->inUse == 0) {
267 queue_Remove(cacheConn);
268 rxi_DestroyConnection(cacheConn->conn);