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>
24 * We initialize rxi_connectionCache at compile time, so there is no
25 * need to call queue_Init(&rxi_connectionCache).
27 static struct rx_queue rxi_connectionCache = { &rxi_connectionCache,
31 #ifdef AFS_PTHREAD_ENV
34 * This mutex protects the following global variables:
38 afs_kmutex_t rxi_connCacheMutex;
39 #define LOCK_CONN_CACHE MUTEX_ENTER(&rxi_connCacheMutex)
40 #define UNLOCK_CONN_CACHE MUTEX_EXIT(&rxi_connCacheMutex)
42 #define LOCK_CONN_CACHE
43 #define UNLOCK_CONN_CACHE
44 #endif /* AFS_PTHREAD_ENV */
47 * convenience typedef - all the stuff that makes up an rx
51 typedef struct rx_connParts {
52 unsigned int hostAddr;
54 unsigned short service;
55 struct rx_securityClass *securityObject;
57 } rx_connParts_t, *rx_connParts_p;
60 * Each element in the cache is represented by the following
61 * structure. I use an rx_queue to manipulate the cache entries.
62 * inUse tracks the number of calls within this connection that
66 typedef struct cache_entry {
67 struct rx_queue queue_header;
68 struct rx_connection *conn;
72 } cache_entry_t, *cache_entry_p;
77 * rxi_connCacheMutex is the only mutex used by these functions. It should
78 * be locked when manipulating the connection cache.
86 * Compare two connections for equality
90 rxi_CachedConnectionsEqual(rx_connParts_p a, rx_connParts_p b)
92 return ((a->hostAddr == b->hostAddr) && (a->port == b->port)
93 && (a->service == b->service)
94 && (a->securityObject == b->securityObject)
95 && (a->securityIndex == b->securityIndex));
99 * Check the cache for a connection
103 rxi_FindCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
106 cache_entry_p cacheConn, nCacheConn;
108 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
109 if ((rxi_CachedConnectionsEqual(parts, &cacheConn->parts))
110 && (cacheConn->inUse < RX_MAXCALLS)
111 && (cacheConn->hasError == 0)) {
113 *conn = cacheConn->conn;
122 * Create an rx connection and return it to the caller
126 * Add a connection to the cache keeping track of the input
127 * arguments that were used to create it
131 rxi_AddCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
133 cache_entry_p new_entry;
135 if ((new_entry = malloc(sizeof(cache_entry_t)))) {
136 new_entry->conn = *conn;
137 new_entry->parts = *parts;
138 new_entry->inUse = 1;
139 new_entry->hasError = 0;
140 queue_Prepend(&rxi_connectionCache, new_entry);
144 * if malloc fails, we fail silently
151 * Get a connection by first checking to see if any matching
152 * available connections are stored in the cache.
153 * Create a new connection if none are currently available.
157 rxi_GetCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
162 * Look to see if we have a cached connection
164 * Note - we hold the connection cache mutex for the entire
165 * search/create/enter operation. We want this entire block to
166 * be atomic so that in the event four threads all pass through
167 * this code at the same time only one actually allocates the
168 * new connection and the other three experience cache hits.
170 * We intentionally slow down throughput in order to
171 * increase the frequency of cache hits.
175 if (!rxi_FindCachedConnection(parts, conn)) {
177 * Create a new connection and enter it in the cache
180 rx_NewConnection(parts->hostAddr, parts->port, parts->service,
181 parts->securityObject, parts->securityIndex))) {
182 rxi_AddCachedConnection(parts, conn);
192 * Delete remaining entries in the cache.
193 * Note - only call this routine from rx_Finalize.
197 rxi_DeleteCachedConnections(void)
199 cache_entry_p cacheConn, nCacheConn;
202 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
205 queue_Remove(cacheConn);
206 rxi_DestroyConnection(cacheConn->conn);
217 * Hand back the caller a connection
218 * The function has the same foot print and return values
219 * as rx_NewConnection.
222 struct rx_connection *
223 rx_GetCachedConnection(unsigned int remoteAddr, unsigned short port,
224 unsigned short service,
225 struct rx_securityClass *securityObject,
228 struct rx_connection *conn = NULL;
229 rx_connParts_t parts;
231 parts.hostAddr = remoteAddr;
233 parts.service = service;
234 parts.securityObject = securityObject;
235 parts.securityIndex = securityIndex;
237 * Get a connection matching the user's request
238 * note we don't propagate the error returned by rxi_GetCachedConnection
239 * since rx_NewConnection doesn't return errors either.
241 rxi_GetCachedConnection(&parts, &conn);
247 * Release a connection we previously handed out
251 rx_ReleaseCachedConnection(struct rx_connection *conn)
253 cache_entry_p cacheConn, nCacheConn;
256 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
257 if (conn == cacheConn->conn) {
260 * check to see if the connection is in error.
261 * If it is, mark its cache entry so it won't be
262 * given out subsequently. If nobody is using it, delete
265 if (rx_ConnError(conn)) {
266 cacheConn->hasError = 1;
267 if (cacheConn->inUse == 0) {
268 queue_Remove(cacheConn);
269 rxi_DestroyConnection(cacheConn->conn);