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.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
39 #define DEBUG_SUBSYSTEM S_LNET
41 #include "../../include/linux/lnet/lib-lnet.h"
42 #include "../../include/linux/lnet/lib-dlc.h"
45 lnet_peer_tables_create(void)
47 struct lnet_peer_table *ptable;
48 struct list_head *hash;
52 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
54 if (!the_lnet.ln_peer_tables) {
55 CERROR("Failed to allocate cpu-partition peer tables\n");
59 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
60 INIT_LIST_HEAD(&ptable->pt_deathrow);
62 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
63 LNET_PEER_HASH_SIZE * sizeof(*hash));
65 CERROR("Failed to create peer hash table\n");
66 lnet_peer_tables_destroy();
70 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
71 INIT_LIST_HEAD(&hash[j]);
72 ptable->pt_hash = hash; /* sign of initialization */
79 lnet_peer_tables_destroy(void)
81 struct lnet_peer_table *ptable;
82 struct list_head *hash;
86 if (!the_lnet.ln_peer_tables)
89 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
90 hash = ptable->pt_hash;
91 if (!hash) /* not initialized */
94 LASSERT(list_empty(&ptable->pt_deathrow));
96 ptable->pt_hash = NULL;
97 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
98 LASSERT(list_empty(&hash[j]));
100 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
103 cfs_percpt_free(the_lnet.ln_peer_tables);
104 the_lnet.ln_peer_tables = NULL;
108 lnet_peer_table_cleanup_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable)
114 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
115 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
117 if (ni && ni != lp->lp_ni)
119 list_del_init(&lp->lp_hashlist);
120 /* Lose hash table's ref */
121 ptable->pt_zombies++;
122 lnet_peer_decref_locked(lp);
128 lnet_peer_table_deathrow_wait_locked(struct lnet_peer_table *ptable,
133 for (i = 3; ptable->pt_zombies; i++) {
134 lnet_net_unlock(cpt_locked);
136 if (is_power_of_2(i)) {
138 "Waiting for %d zombies on peer table\n",
141 set_current_state(TASK_UNINTERRUPTIBLE);
142 schedule_timeout(cfs_time_seconds(1) >> 1);
143 lnet_net_lock(cpt_locked);
148 lnet_peer_table_del_rtrs_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable,
156 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
157 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
162 if (!lp->lp_rtr_refcount)
167 lnet_net_unlock(cpt_locked);
168 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lp_nid);
169 lnet_net_lock(cpt_locked);
175 lnet_peer_tables_cleanup(lnet_ni_t *ni)
177 struct lnet_peer_table *ptable;
178 struct list_head deathrow;
183 INIT_LIST_HEAD(&deathrow);
185 LASSERT(the_lnet.ln_shutdown || ni);
187 * If just deleting the peers for a NI, get rid of any routes these
188 * peers are gateways for.
190 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
192 lnet_peer_table_del_rtrs_locked(ni, ptable, i);
197 * Start the process of moving the applicable peers to
200 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
202 lnet_peer_table_cleanup_locked(ni, ptable);
206 /* Cleanup all entries on deathrow. */
207 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
209 lnet_peer_table_deathrow_wait_locked(ptable, i);
210 list_splice_init(&ptable->pt_deathrow, &deathrow);
214 list_for_each_entry_safe(lp, temp, &deathrow, lp_hashlist) {
215 list_del(&lp->lp_hashlist);
216 LIBCFS_FREE(lp, sizeof(*lp));
221 lnet_destroy_peer_locked(lnet_peer_t *lp)
223 struct lnet_peer_table *ptable;
225 LASSERT(!lp->lp_refcount);
226 LASSERT(!lp->lp_rtr_refcount);
227 LASSERT(list_empty(&lp->lp_txq));
228 LASSERT(list_empty(&lp->lp_hashlist));
229 LASSERT(!lp->lp_txqnob);
231 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
232 LASSERT(ptable->pt_number > 0);
235 lnet_ni_decref_locked(lp->lp_ni, lp->lp_cpt);
238 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
239 LASSERT(ptable->pt_zombies > 0);
240 ptable->pt_zombies--;
244 lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
246 struct list_head *peers;
249 LASSERT(!the_lnet.ln_shutdown);
251 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
252 list_for_each_entry(lp, peers, lp_hashlist) {
253 if (lp->lp_nid == nid) {
254 lnet_peer_addref_locked(lp);
263 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
265 struct lnet_peer_table *ptable;
266 lnet_peer_t *lp = NULL;
272 if (the_lnet.ln_shutdown) /* it's shutting down */
275 /* cpt can be LNET_LOCK_EX if it's called from router functions */
276 cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
278 ptable = the_lnet.ln_peer_tables[cpt2];
279 lp = lnet_find_peer_locked(ptable, nid);
285 if (!list_empty(&ptable->pt_deathrow)) {
286 lp = list_entry(ptable->pt_deathrow.next,
287 lnet_peer_t, lp_hashlist);
288 list_del(&lp->lp_hashlist);
292 * take extra refcount in case another thread has shutdown LNet
293 * and destroyed locks and peer-table before I finish the allocation
296 lnet_net_unlock(cpt);
299 memset(lp, 0, sizeof(*lp));
301 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
309 INIT_LIST_HEAD(&lp->lp_txq);
310 INIT_LIST_HEAD(&lp->lp_rtrq);
311 INIT_LIST_HEAD(&lp->lp_routes);
314 lp->lp_notifylnd = 0;
315 lp->lp_notifying = 0;
316 lp->lp_alive_count = 0;
317 lp->lp_timestamp = 0;
318 lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
319 lp->lp_last_alive = cfs_time_current(); /* assumes alive */
320 lp->lp_last_query = 0; /* haven't asked NI yet */
321 lp->lp_ping_timestamp = 0;
322 lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
325 lp->lp_refcount = 2; /* 1 for caller; 1 for hash */
326 lp->lp_rtr_refcount = 0;
330 if (the_lnet.ln_shutdown) {
335 lp2 = lnet_find_peer_locked(ptable, nid);
341 lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
347 lp->lp_txcredits = lp->lp_ni->ni_peertxcredits;
348 lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
349 lp->lp_rtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
350 lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
352 list_add_tail(&lp->lp_hashlist,
353 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
354 ptable->pt_version++;
360 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
366 lnet_debug_peer(lnet_nid_t nid)
368 char *aliveness = "NA";
373 cpt = lnet_cpt_of_nid(nid);
376 rc = lnet_nid2peer_locked(&lp, nid, cpt);
378 lnet_net_unlock(cpt);
379 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
383 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
384 aliveness = lp->lp_alive ? "up" : "down";
386 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
387 libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
388 aliveness, lp->lp_ni->ni_peertxcredits,
389 lp->lp_rtrcredits, lp->lp_minrtrcredits,
390 lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
392 lnet_peer_decref_locked(lp);
394 lnet_net_unlock(cpt);
398 lnet_get_peer_info(__u32 peer_index, __u64 *nid,
399 char aliveness[LNET_MAX_STR_LEN],
400 __u32 *cpt_iter, __u32 *refcount,
401 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
402 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
405 struct lnet_peer_table *peer_table;
410 /* get the number of CPTs */
411 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
414 * if the cpt number to be examined is >= the number of cpts in
415 * the system then indicate that there are no more cpts to examin
417 if (*cpt_iter >= lncpt)
420 /* get the current table */
421 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
422 /* if the ptable is NULL then there are no more cpts to examine */
426 lnet_net_lock(*cpt_iter);
428 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
429 struct list_head *peers = &peer_table->pt_hash[j];
431 list_for_each_entry(lp, peers, lp_hashlist) {
432 if (peer_index-- > 0)
435 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
436 if (lnet_isrouter(lp) ||
437 lnet_peer_aliveness_enabled(lp))
438 snprintf(aliveness, LNET_MAX_STR_LEN,
439 lp->lp_alive ? "up" : "down");
442 *refcount = lp->lp_refcount;
443 *ni_peer_tx_credits = lp->lp_ni->ni_peertxcredits;
444 *peer_tx_credits = lp->lp_txcredits;
445 *peer_rtr_credits = lp->lp_rtrcredits;
446 *peer_min_rtr_credits = lp->lp_mintxcredits;
447 *peer_tx_qnob = lp->lp_txqnob;
452 lnet_net_unlock(*cpt_iter);
456 return found ? 0 : -ENOENT;