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>
21 #include <sys/types.h>
26 * We initialize rxi_connectionCache at compile time, so there is no
27 * need to call queue_Init(&rxi_connectionCache).
29 static struct rx_queue rxi_connectionCache = { &rxi_connectionCache,
33 #ifdef AFS_PTHREAD_ENV
36 * This mutex protects the following global variables:
40 afs_kmutex_t rxi_connCacheMutex;
41 #define LOCK_CONN_CACHE MUTEX_ENTER(&rxi_connCacheMutex)
42 #define UNLOCK_CONN_CACHE MUTEX_EXIT(&rxi_connCacheMutex)
44 #define LOCK_CONN_CACHE
45 #define UNLOCK_CONN_CACHE
46 #endif /* AFS_PTHREAD_ENV */
49 * convenience typedef - all the stuff that makes up an rx
53 typedef struct rx_connParts {
54 unsigned int hostAddr;
56 unsigned short service;
57 struct rx_securityClass *securityObject;
59 } rx_connParts_t, *rx_connParts_p;
62 * Each element in the cache is represented by the following
63 * structure. I use an rx_queue to manipulate the cache entries.
64 * inUse tracks the number of calls within this connection that
68 typedef struct cache_entry {
69 struct rx_queue queue_header;
70 struct rx_connection *conn;
74 } cache_entry_t, *cache_entry_p;
79 * rxi_connCacheMutex is the only mutex used by these functions. It should
80 * be locked when manipulating the connection cache.
88 * Compare two connections for equality
92 rxi_CachedConnectionsEqual(rx_connParts_p a, rx_connParts_p b)
94 return ((a->hostAddr == b->hostAddr) && (a->port == b->port)
95 && (a->service == b->service)
96 && (a->securityObject == b->securityObject)
97 && (a->securityIndex == b->securityIndex));
101 * Check the cache for a connection
105 rxi_FindCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
108 cache_entry_p cacheConn, nCacheConn;
110 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
111 if ((rxi_CachedConnectionsEqual(parts, &cacheConn->parts))
112 && (cacheConn->inUse < RX_MAXCALLS)
113 && (cacheConn->hasError == 0)) {
115 *conn = cacheConn->conn;
124 * Create an rx connection and return it to the caller
128 * Add a connection to the cache keeping track of the input
129 * arguments that were used to create it
133 rxi_AddCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
135 cache_entry_p new_entry;
137 if ((new_entry = (cache_entry_p) malloc(sizeof(cache_entry_t)))) {
138 new_entry->conn = *conn;
139 new_entry->parts = *parts;
140 new_entry->inUse = 1;
141 new_entry->hasError = 0;
142 queue_Prepend(&rxi_connectionCache, new_entry);
146 * if malloc fails, we fail silently
153 * Get a connection by first checking to see if any matching
154 * available connections are stored in the cache.
155 * Create a new connection if none are currently available.
159 rxi_GetCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
164 * Look to see if we have a cached connection
166 * Note - we hold the connection cache mutex for the entire
167 * search/create/enter operation. We want this entire block to
168 * be atomic so that in the event four threads all pass through
169 * this code at the same time only one actually allocates the
170 * new connection and the other three experience cache hits.
172 * We intentionally slow down throughput in order to
173 * increase the frequency of cache hits.
177 if (!rxi_FindCachedConnection(parts, conn)) {
179 * Create a new connection and enter it in the cache
182 rx_NewConnection(parts->hostAddr, parts->port, parts->service,
183 parts->securityObject, parts->securityIndex))) {
184 rxi_AddCachedConnection(parts, conn);
194 * Delete remaining entries in the cache.
195 * Note - only call this routine from rx_Finalize.
199 rxi_DeleteCachedConnections(void)
201 cache_entry_p cacheConn, nCacheConn;
204 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
207 queue_Remove(cacheConn);
208 rxi_DestroyConnection(cacheConn->conn);
219 * Hand back the caller a connection
220 * The function has the same foot print and return values
221 * as rx_NewConnection.
224 struct rx_connection *
225 rx_GetCachedConnection(unsigned int remoteAddr, unsigned short port,
226 unsigned short service,
227 struct rx_securityClass *securityObject,
230 struct rx_connection *conn = NULL;
231 rx_connParts_t parts;
233 parts.hostAddr = remoteAddr;
235 parts.service = service;
236 parts.securityObject = securityObject;
237 parts.securityIndex = securityIndex;
239 * Get a connection matching the user's request
240 * note we don't propagate the error returned by rxi_GetCachedConnection
241 * since rx_NewConnection doesn't return errors either.
243 rxi_GetCachedConnection(&parts, &conn);
249 * Release a connection we previously handed out
253 rx_ReleaseCachedConnection(struct rx_connection *conn)
255 cache_entry_p cacheConn, nCacheConn;
258 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
259 if (conn == cacheConn->conn) {
262 * check to see if the connection is in error.
263 * If it is, mark its cache entry so it won't be
264 * given out subsequently. If nobody is using it, delete
267 if (rx_ConnError(conn)) {
268 cacheConn->hasError = 1;
269 if (cacheConn->inUse == 0) {
270 queue_Remove(cacheConn);
271 rxi_DestroyConnection(cacheConn->conn);