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 LASSERT(!the_lnet.ln_shutdown);
191 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
192 cfs_list_for_each_entry(lp, peers, lp_hashlist) {
193 if (lp->lp_nid == nid) {
194 lnet_peer_addref_locked(lp);
203 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
205 struct lnet_peer_table *ptable;
206 lnet_peer_t *lp = NULL;
212 if (the_lnet.ln_shutdown) /* it's shutting down */
215 /* cpt can be LNET_LOCK_EX if it's called from router functions */
216 cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
218 ptable = the_lnet.ln_peer_tables[cpt2];
219 lp = lnet_find_peer_locked(ptable, nid);
225 if (!cfs_list_empty(&ptable->pt_deathrow)) {
226 lp = cfs_list_entry(ptable->pt_deathrow.next,
227 lnet_peer_t, lp_hashlist);
228 cfs_list_del(&lp->lp_hashlist);
232 * take extra refcount in case another thread has shutdown LNet
233 * and destroyed locks and peer-table before I finish the allocation
236 lnet_net_unlock(cpt);
239 memset(lp, 0, sizeof(*lp));
241 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
249 CFS_INIT_LIST_HEAD(&lp->lp_txq);
250 CFS_INIT_LIST_HEAD(&lp->lp_rtrq);
251 CFS_INIT_LIST_HEAD(&lp->lp_routes);
254 lp->lp_notifylnd = 0;
255 lp->lp_notifying = 0;
256 lp->lp_alive_count = 0;
257 lp->lp_timestamp = 0;
258 lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
259 lp->lp_last_alive = cfs_time_current(); /* assumes alive */
260 lp->lp_last_query = 0; /* haven't asked NI yet */
261 lp->lp_ping_timestamp = 0;
262 lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
265 lp->lp_refcount = 2; /* 1 for caller; 1 for hash */
266 lp->lp_rtr_refcount = 0;
270 if (the_lnet.ln_shutdown) {
275 lp2 = lnet_find_peer_locked(ptable, nid);
281 lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
282 if (lp->lp_ni == NULL) {
288 lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
290 lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
292 cfs_list_add_tail(&lp->lp_hashlist,
293 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
294 ptable->pt_version++;
300 cfs_list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
306 lnet_debug_peer(lnet_nid_t nid)
308 char *aliveness = "NA";
313 cpt = lnet_cpt_of_nid(nid);
316 rc = lnet_nid2peer_locked(&lp, nid, cpt);
318 lnet_net_unlock(cpt);
319 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
323 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
324 aliveness = lp->lp_alive ? "up" : "down";
326 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
327 libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
328 aliveness, lp->lp_ni->ni_peertxcredits,
329 lp->lp_rtrcredits, lp->lp_minrtrcredits,
330 lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
332 lnet_peer_decref_locked(lp);
334 lnet_net_unlock(cpt);