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>
16 #include "afs/param.h"
18 #include <afs/param.h>
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
27 #include <sys/types.h>
34 * We initialize rxi_connectionCache at compile time, so there is no
35 * need to call queue_Init(&rxi_connectionCache).
37 static struct rx_queue rxi_connectionCache = { &rxi_connectionCache,
41 #ifdef AFS_PTHREAD_ENV
44 * This mutex protects the following global variables:
48 afs_kmutex_t rxi_connCacheMutex;
49 #define LOCK_CONN_CACHE MUTEX_ENTER(&rxi_connCacheMutex)
50 #define UNLOCK_CONN_CACHE MUTEX_EXIT(&rxi_connCacheMutex)
52 #define LOCK_CONN_CACHE
53 #define UNLOCK_CONN_CACHE
54 #endif /* AFS_PTHREAD_ENV */
57 * convenience typedef - all the stuff that makes up an rx
61 typedef struct rx_connParts {
62 unsigned int hostAddr;
64 unsigned short service;
65 struct rx_securityClass *securityObject;
67 } rx_connParts_t, *rx_connParts_p;
70 * Each element in the cache is represented by the following
71 * structure. I use an rx_queue to manipulate the cache entries.
72 * inUse tracks the number of calls within this connection that
76 typedef struct cache_entry {
77 struct rx_queue queue_header;
78 struct rx_connection *conn;
82 } cache_entry_t, *cache_entry_p;
87 * rxi_connCacheMutex is the only mutex used by these functions. It should
88 * be locked when manipulating the connection cache.
96 * Compare two connections for equality
100 rxi_CachedConnectionsEqual(rx_connParts_p a, rx_connParts_p b)
102 return ((a->hostAddr == b->hostAddr) && (a->port == b->port)
103 && (a->service == b->service)
104 && (a->securityObject == b->securityObject)
105 && (a->securityIndex == b->securityIndex));
109 * Check the cache for a connection
113 rxi_FindCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
116 cache_entry_p cacheConn, nCacheConn;
118 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
119 if ((rxi_CachedConnectionsEqual(parts, &cacheConn->parts))
120 && (cacheConn->inUse < RX_MAXCALLS)
121 && (cacheConn->hasError == 0)) {
123 *conn = cacheConn->conn;
132 * Create an rx connection and return it to the caller
136 * Add a connection to the cache keeping track of the input
137 * arguments that were used to create it
141 rxi_AddCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
143 cache_entry_p new_entry;
145 if ((new_entry = (cache_entry_p) malloc(sizeof(cache_entry_t)))) {
146 new_entry->conn = *conn;
147 new_entry->parts = *parts;
148 new_entry->inUse = 1;
149 new_entry->hasError = 0;
150 queue_Prepend(&rxi_connectionCache, new_entry);
154 * if malloc fails, we fail silently
161 * Get a connection by first checking to see if any matching
162 * available connections are stored in the cache.
163 * Create a new connection if none are currently available.
167 rxi_GetCachedConnection(rx_connParts_p parts, struct rx_connection **conn)
172 * Look to see if we have a cached connection
174 * Note - we hold the connection cache mutex for the entire
175 * search/create/enter operation. We want this entire block to
176 * be atomic so that in the event four threads all pass through
177 * this code at the same time only one actually allocates the
178 * new connection and the other three experience cache hits.
180 * We intentionally slow down throughput in order to
181 * increase the frequency of cache hits.
185 if (!rxi_FindCachedConnection(parts, conn)) {
187 * Create a new connection and enter it in the cache
190 rx_NewConnection(parts->hostAddr, parts->port, parts->service,
191 parts->securityObject, parts->securityIndex))) {
192 rxi_AddCachedConnection(parts, conn);
202 * Delete remaining entries in the cache.
203 * Note - only call this routine from rx_Finalize.
207 rxi_DeleteCachedConnections(void)
209 cache_entry_p cacheConn, nCacheConn;
212 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
215 queue_Remove(cacheConn);
216 rxi_DestroyConnection(cacheConn->conn);
227 * Hand back the caller a connection
228 * The function has the same foot print and return values
229 * as rx_NewConnection.
232 struct rx_connection *
233 rx_GetCachedConnection(unsigned int remoteAddr, unsigned short port,
234 unsigned short service,
235 struct rx_securityClass *securityObject,
238 struct rx_connection *conn = NULL;
239 rx_connParts_t parts;
241 parts.hostAddr = remoteAddr;
243 parts.service = service;
244 parts.securityObject = securityObject;
245 parts.securityIndex = securityIndex;
247 * Get a connection matching the user's request
248 * note we don't propagate the error returned by rxi_GetCachedConnection
249 * since rx_NewConnection doesn't return errors either.
251 rxi_GetCachedConnection(&parts, &conn);
257 * Release a connection we previously handed out
261 rx_ReleaseCachedConnection(struct rx_connection *conn)
263 cache_entry_p cacheConn, nCacheConn;
266 for (queue_Scan(&rxi_connectionCache, cacheConn, nCacheConn, cache_entry)) {
267 if (conn == cacheConn->conn) {
270 * check to see if the connection is in error.
271 * If it is, mark its cache entry so it won't be
272 * given out subsequently. If nobody is using it, delete
275 if (rx_ConnError(conn)) {
276 cacheConn->hasError = 1;
277 if (cacheConn->inUse == 0) {
278 queue_Remove(cacheConn);
279 rxi_DestroyConnection(cacheConn->conn);