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>
22 * We initialize rxi_connectionCache at compile time, so there is no
23 * need to call queue_Init(&rxi_connectionCache).
25 static struct rx_queue rxi_connectionCache = { &rxi_connectionCache,
29 #ifdef AFS_PTHREAD_ENV
32 * This mutex protects the following global variables:
36 afs_kmutex_t rxi_connCacheMutex;
37 #define LOCK_CONN_CACHE MUTEX_ENTER(&rxi_connCacheMutex)
38 #define UNLOCK_CONN_CACHE MUTEX_EXIT(&rxi_connCacheMutex)
40 #define LOCK_CONN_CACHE
41 #define UNLOCK_CONN_CACHE
42 #endif /* AFS_PTHREAD_ENV */
45 * convenience typedef - all the stuff that makes up an rx
49 typedef struct rx_connParts {
50 unsigned int hostAddr;
52 unsigned short service;
53 struct rx_securityClass *securityObject;
55 } rx_connParts_t, *rx_connParts_p;
58 * Each element in the cache is represented by the following
59 * structure. I use an rx_queue to manipulate the cache entries.
60 * inUse tracks the number of calls within this connection that
64 typedef struct cache_entry {
65 struct rx_queue queue_header;
66 struct rx_connection *conn;
70 } cache_entry_t, *cache_entry_p;
75 * rxi_connCacheMutex is the only mutex used by these functions. It should
76 * be locked when manipulating the connection cache.
84 * Compare two connections for equality
88 rxi_CachedConnectionsEqual(rx_connParts_p a, rx_connParts_p b)
90 return ((a->hostAddr == b->hostAddr) && (a->port == b->port)
91 && (a->service == b->service)
92 && (a->securityObject == b->securityObject)
93 && (a->securityIndex == b->securityIndex));
97 * Check the cache for a connection
101 rxi_FindCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
104 cache_entry_p cacheConn, nCacheConn;
106 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
107 if ((rxi_CachedConnectionsEqual(parts, &cacheConn->parts))
108 && (cacheConn->inUse < RX_MAXCALLS)
109 && (cacheConn->hasError == 0)) {
111 *conn = cacheConn->conn;
120 * Create an rx connection and return it to the caller
124 * Add a connection to the cache keeping track of the input
125 * arguments that were used to create it
129 rxi_AddCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
131 cache_entry_p new_entry;
133 if ((new_entry = (cache_entry_p) malloc(sizeof(cache_entry_t)))) {
134 new_entry->conn = *conn;
135 new_entry->parts = *parts;
136 new_entry->inUse = 1;
137 new_entry->hasError = 0;
138 queue_Prepend(&rxi_connectionCache, new_entry);
142 * if malloc fails, we fail silently
149 * Get a connection by first checking to see if any matching
150 * available connections are stored in the cache.
151 * Create a new connection if none are currently available.
155 rxi_GetCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
160 * Look to see if we have a cached connection
162 * Note - we hold the connection cache mutex for the entire
163 * search/create/enter operation. We want this entire block to
164 * be atomic so that in the event four threads all pass through
165 * this code at the same time only one actually allocates the
166 * new connection and the other three experience cache hits.
168 * We intentionally slow down throughput in order to
169 * increase the frequency of cache hits.
173 if (!rxi_FindCachedConnection(parts, conn)) {
175 * Create a new connection and enter it in the cache
178 rx_NewConnection(parts->hostAddr, parts->port, parts->service,
179 parts->securityObject, parts->securityIndex))) {
180 rxi_AddCachedConnection(parts, conn);
190 * Delete remaining entries in the cache.
191 * Note - only call this routine from rx_Finalize.
195 rxi_DeleteCachedConnections(void)
197 cache_entry_p cacheConn, nCacheConn;
200 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
203 queue_Remove(cacheConn);
204 rxi_DestroyConnection(cacheConn->conn);
215 * Hand back the caller a connection
216 * The function has the same foot print and return values
217 * as rx_NewConnection.
220 struct rx_connection *
221 rx_GetCachedConnection(unsigned int remoteAddr, unsigned short port,
222 unsigned short service,
223 struct rx_securityClass *securityObject,
226 struct rx_connection *conn = NULL;
227 rx_connParts_t parts;
229 parts.hostAddr = remoteAddr;
231 parts.service = service;
232 parts.securityObject = securityObject;
233 parts.securityIndex = securityIndex;
235 * Get a connection matching the user's request
236 * note we don't propagate the error returned by rxi_GetCachedConnection
237 * since rx_NewConnection doesn't return errors either.
239 rxi_GetCachedConnection(&parts, &conn);
245 * Release a connection we previously handed out
249 rx_ReleaseCachedConnection(struct rx_connection *conn)
251 cache_entry_p cacheConn, nCacheConn;
254 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
255 if (conn == cacheConn->conn) {
258 * check to see if the connection is in error.
259 * If it is, mark its cache entry so it won't be
260 * given out subsequently. If nobody is using it, delete
263 if (rx_ConnError(conn)) {
264 cacheConn->hasError = 1;
265 if (cacheConn->inUse == 0) {
266 queue_Remove(cacheConn);
267 rxi_DestroyConnection(cacheConn->conn);