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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <lnet/lib-lnet.h>
42 lnet_peer_tables_create(void)
44 struct lnet_peer_table *ptable;
49 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
51 if (the_lnet.ln_peer_tables == NULL) {
52 CERROR("Failed to allocate cpu-partition peer tables\n");
56 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
57 CFS_INIT_LIST_HEAD(&ptable->pt_deathrow);
59 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
60 LNET_PEER_HASH_SIZE * sizeof(*hash));
62 CERROR("Failed to create peer hash table\n");
63 lnet_peer_tables_destroy();
67 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
68 CFS_INIT_LIST_HEAD(&hash[j]);
69 ptable->pt_hash = hash; /* sign of initialization */
76 lnet_peer_tables_destroy(void)
78 struct lnet_peer_table *ptable;
83 if (the_lnet.ln_peer_tables == NULL)
86 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
87 hash = ptable->pt_hash;
88 if (hash == NULL) /* not intialized */
91 LASSERT(cfs_list_empty(&ptable->pt_deathrow));
93 ptable->pt_hash = NULL;
94 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
95 LASSERT(cfs_list_empty(&hash[j]));
97 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
100 cfs_percpt_free(the_lnet.ln_peer_tables);
101 the_lnet.ln_peer_tables = NULL;
105 lnet_peer_tables_cleanup(void)
107 struct lnet_peer_table *ptable;
111 LASSERT(the_lnet.ln_shutdown); /* i.e. no new peers */
113 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
116 for (j = 0; j < LNET_PEER_HASH_SIZE; j++) {
117 cfs_list_t *peers = &ptable->pt_hash[j];
119 while (!cfs_list_empty(peers)) {
120 lnet_peer_t *lp = cfs_list_entry(peers->next,
123 cfs_list_del_init(&lp->lp_hashlist);
124 /* lose hash table's ref */
125 lnet_peer_decref_locked(lp);
132 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
133 CFS_LIST_HEAD (deathrow);
138 for (j = 3; ptable->pt_number != 0; j++) {
141 if ((j & (j - 1)) == 0) {
143 "Waiting for %d peers on peer table\n",
146 cfs_pause(cfs_time_seconds(1) / 2);
149 cfs_list_splice_init(&ptable->pt_deathrow, &deathrow);
153 while (!cfs_list_empty(&deathrow)) {
154 lp = cfs_list_entry(deathrow.next,
155 lnet_peer_t, lp_hashlist);
156 cfs_list_del(&lp->lp_hashlist);
157 LIBCFS_FREE(lp, sizeof(*lp));
163 lnet_destroy_peer_locked(lnet_peer_t *lp)
165 struct lnet_peer_table *ptable;
167 LASSERT(lp->lp_refcount == 0);
168 LASSERT(lp->lp_rtr_refcount == 0);
169 LASSERT(cfs_list_empty(&lp->lp_txq));
170 LASSERT(cfs_list_empty(&lp->lp_hashlist));
171 LASSERT(lp->lp_txqnob == 0);
173 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
174 LASSERT(ptable->pt_number > 0);
177 lnet_ni_decref_locked(lp->lp_ni, lp->lp_cpt);
180 cfs_list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
184 lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
189 if (the_lnet.ln_shutdown)
192 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
193 cfs_list_for_each_entry(lp, peers, lp_hashlist) {
194 if (lp->lp_nid == nid) {
195 lnet_peer_addref_locked(lp);
204 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
206 struct lnet_peer_table *ptable;
207 lnet_peer_t *lp = NULL;
211 /* cpt can be LNET_LOCK_EX if it's called from router functions */
212 cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
214 ptable = the_lnet.ln_peer_tables[cpt2];
215 lp = lnet_find_peer_locked(ptable, nid);
221 if (!cfs_list_empty(&ptable->pt_deathrow)) {
222 lp = cfs_list_entry(ptable->pt_deathrow.next,
223 lnet_peer_t, lp_hashlist);
224 cfs_list_del(&lp->lp_hashlist);
227 lnet_net_unlock(cpt);
230 memset(lp, 0, sizeof(*lp));
232 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
240 CFS_INIT_LIST_HEAD(&lp->lp_txq);
241 CFS_INIT_LIST_HEAD(&lp->lp_rtrq);
242 CFS_INIT_LIST_HEAD(&lp->lp_routes);
245 lp->lp_notifylnd = 0;
246 lp->lp_notifying = 0;
247 lp->lp_alive_count = 0;
248 lp->lp_timestamp = 0;
249 lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
250 lp->lp_last_alive = cfs_time_current(); /* assumes alive */
251 lp->lp_last_query = 0; /* haven't asked NI yet */
252 lp->lp_ping_timestamp = 0;
253 lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
256 lp->lp_refcount = 2; /* 1 for caller; 1 for hash */
257 lp->lp_rtr_refcount = 0;
261 lp2 = lnet_find_peer_locked(ptable, nid);
263 cfs_list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
265 if (the_lnet.ln_shutdown) {
266 lnet_peer_decref_locked(lp2);
275 lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
276 if (lp->lp_ni == NULL) {
277 cfs_list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
280 return the_lnet.ln_shutdown ? -ESHUTDOWN : -EHOSTUNREACH;
284 lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
286 lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
288 /* can't add peers after shutdown starts */
289 LASSERT (!the_lnet.ln_shutdown);
291 cfs_list_add_tail(&lp->lp_hashlist,
292 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
293 ptable->pt_version++;
301 lnet_debug_peer(lnet_nid_t nid)
303 char *aliveness = "NA";
308 cpt = lnet_cpt_of_nid(nid);
311 rc = lnet_nid2peer_locked(&lp, nid, cpt);
313 lnet_net_unlock(cpt);
314 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
318 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
319 aliveness = lp->lp_alive ? "up" : "down";
321 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
322 libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
323 aliveness, lp->lp_ni->ni_peertxcredits,
324 lp->lp_rtrcredits, lp->lp_minrtrcredits,
325 lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
327 lnet_peer_decref_locked(lp);
329 lnet_net_unlock(cpt);