4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_RPC
38 #include <obd_support.h>
39 #include <obd_class.h>
40 #include <lustre_net.h>
42 #include "ptlrpc_internal.h"
44 static cfs_hash_t *conn_hash = NULL;
45 static cfs_hash_ops_t conn_hash_ops;
47 struct ptlrpc_connection *
48 ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self,
49 struct obd_uuid *uuid)
51 struct ptlrpc_connection *conn, *conn2;
54 conn = cfs_hash_lookup(conn_hash, &peer);
64 INIT_HLIST_NODE(&conn->c_hash);
65 atomic_set(&conn->c_refcount, 1);
67 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
70 * Add the newly created conn to the hash, on key collision we
71 * lost a racing addition and must destroy our newly allocated
72 * connection. The object which exists in the has will be
73 * returned and may be compared against out object.
75 /* In the function below, .hs_keycmp resolves to
77 /* coverity[overrun-buffer-val] */
78 conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
85 CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
86 conn, atomic_read(&conn->c_refcount),
87 libcfs_nid2str(conn->c_peer.nid));
91 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
99 LASSERT(atomic_read(&conn->c_refcount) > 1);
102 * We do not remove connection from hashtable and
103 * do not free it even if last caller released ref,
104 * as we want to have it cached for the case it is
107 * Deallocating it and later creating new connection
108 * again would be wastful. This way we also avoid
109 * expensive locking to protect things from get/put
110 * race when found cached connection is freed by
111 * ptlrpc_connection_put().
113 * It will be freed later in module unload time,
114 * when ptlrpc_connection_fini()->lh_exit->conn_exit()
117 if (atomic_dec_return(&conn->c_refcount) == 1)
120 CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
121 conn, atomic_read(&conn->c_refcount),
122 libcfs_nid2str(conn->c_peer.nid));
127 struct ptlrpc_connection *
128 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
132 atomic_inc(&conn->c_refcount);
133 CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
134 conn, atomic_read(&conn->c_refcount),
135 libcfs_nid2str(conn->c_peer.nid));
140 int ptlrpc_connection_init(void)
144 conn_hash = cfs_hash_create("CONN_HASH",
147 HASH_CONN_BKT_BITS, 0,
150 &conn_hash_ops, CFS_HASH_DEFAULT);
157 void ptlrpc_connection_fini(void) {
159 cfs_hash_putref(conn_hash);
164 * Hash operations for net_peer<->connection
167 conn_hashfn(cfs_hash_t *hs, const void *key, unsigned mask)
169 return cfs_hash_djb2_hash(key, sizeof(lnet_process_id_t), mask);
173 conn_keycmp(const void *key, struct hlist_node *hnode)
175 struct ptlrpc_connection *conn;
176 const lnet_process_id_t *conn_key;
178 LASSERT(key != NULL);
179 conn_key = (lnet_process_id_t *)key;
180 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
182 return conn_key->nid == conn->c_peer.nid &&
183 conn_key->pid == conn->c_peer.pid;
187 conn_key(struct hlist_node *hnode)
189 struct ptlrpc_connection *conn;
190 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
191 return &conn->c_peer;
195 conn_object(struct hlist_node *hnode)
197 return hlist_entry(hnode, struct ptlrpc_connection, c_hash);
201 conn_get(cfs_hash_t *hs, struct hlist_node *hnode)
203 struct ptlrpc_connection *conn;
205 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
206 atomic_inc(&conn->c_refcount);
210 conn_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
212 struct ptlrpc_connection *conn;
214 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
215 atomic_dec(&conn->c_refcount);
219 conn_exit(cfs_hash_t *hs, struct hlist_node *hnode)
221 struct ptlrpc_connection *conn;
223 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
225 * Nothing should be left. Connection user put it and
226 * connection also was deleted from table by this time
227 * so we should have 0 refs.
229 LASSERTF(atomic_read(&conn->c_refcount) == 0,
230 "Busy connection with %d refs\n",
231 atomic_read(&conn->c_refcount));
235 static cfs_hash_ops_t conn_hash_ops = {
236 .hs_hash = conn_hashfn,
237 .hs_keycmp = conn_keycmp,
239 .hs_object = conn_object,
241 .hs_put_locked = conn_put_locked,
242 .hs_exit = conn_exit,