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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <lnet/lib-lnet.h>
38 #include <lnet/lib-dlc.h>
41 lnet_peer_tables_create(void)
43 struct lnet_peer_table *ptable;
44 struct list_head *hash;
48 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
50 if (the_lnet.ln_peer_tables == NULL) {
51 CERROR("Failed to allocate cpu-partition peer tables\n");
55 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
56 INIT_LIST_HEAD(&ptable->pt_deathrow);
58 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
59 LNET_PEER_HASH_SIZE * sizeof(*hash));
61 CERROR("Failed to create peer hash table\n");
62 lnet_peer_tables_destroy();
66 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
67 INIT_LIST_HEAD(&hash[j]);
68 ptable->pt_hash = hash; /* sign of initialization */
75 lnet_peer_tables_destroy(void)
77 struct lnet_peer_table *ptable;
78 struct list_head *hash;
82 if (the_lnet.ln_peer_tables == NULL)
85 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
86 hash = ptable->pt_hash;
87 if (hash == NULL) /* not intialized */
90 LASSERT(list_empty(&ptable->pt_deathrow));
92 ptable->pt_hash = NULL;
93 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
94 LASSERT(list_empty(&hash[j]));
96 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
99 cfs_percpt_free(the_lnet.ln_peer_tables);
100 the_lnet.ln_peer_tables = NULL;
104 lnet_peer_table_cleanup_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable)
110 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
111 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
113 if (ni != NULL && ni != lp->lp_ni)
115 list_del_init(&lp->lp_hashlist);
116 /* Lose hash table's ref */
117 ptable->pt_zombies++;
118 lnet_peer_decref_locked(lp);
124 lnet_peer_table_deathrow_wait_locked(struct lnet_peer_table *ptable,
129 for (i = 3; ptable->pt_zombies != 0; i++) {
130 lnet_net_unlock(cpt_locked);
134 "Waiting for %d zombies on peer table\n",
137 set_current_state(TASK_UNINTERRUPTIBLE);
138 schedule_timeout(cfs_time_seconds(1) >> 1);
139 lnet_net_lock(cpt_locked);
144 lnet_peer_table_del_rtrs_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable,
152 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
153 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
158 if (lp->lp_rtr_refcount == 0)
163 lnet_net_unlock(cpt_locked);
164 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lp_nid);
165 lnet_net_lock(cpt_locked);
171 lnet_peer_tables_cleanup(lnet_ni_t *ni)
174 struct lnet_peer_table *ptable;
176 struct list_head deathrow;
178 INIT_LIST_HEAD(&deathrow);
180 LASSERT(the_lnet.ln_shutdown || ni != NULL);
181 /* If just deleting the peers for a NI, get rid of any routes these
182 * peers are gateways for. */
183 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
185 lnet_peer_table_del_rtrs_locked(ni, ptable, i);
189 /* Start the process of moving the applicable peers to
191 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
193 lnet_peer_table_cleanup_locked(ni, ptable);
197 /* Cleanup all entries on deathrow. */
198 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
200 lnet_peer_table_deathrow_wait_locked(ptable, i);
201 list_splice_init(&ptable->pt_deathrow, &deathrow);
205 while (!list_empty(&deathrow)) {
206 lp = list_entry(deathrow.next, lnet_peer_t, lp_hashlist);
207 list_del(&lp->lp_hashlist);
208 LIBCFS_FREE(lp, sizeof(*lp));
213 lnet_destroy_peer_locked(lnet_peer_t *lp)
215 struct lnet_peer_table *ptable;
217 LASSERT(lp->lp_refcount == 0);
218 LASSERT(lp->lp_rtr_refcount == 0);
219 LASSERT(list_empty(&lp->lp_txq));
220 LASSERT(list_empty(&lp->lp_hashlist));
221 LASSERT(lp->lp_txqnob == 0);
223 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
224 LASSERT(ptable->pt_number > 0);
227 lnet_ni_decref_locked(lp->lp_ni, lp->lp_cpt);
230 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
231 LASSERT(ptable->pt_zombies > 0);
232 ptable->pt_zombies--;
236 lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
238 struct list_head *peers;
241 LASSERT(!the_lnet.ln_shutdown);
243 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
244 list_for_each_entry(lp, peers, lp_hashlist) {
245 if (lp->lp_nid == nid) {
246 lnet_peer_addref_locked(lp);
255 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
257 struct lnet_peer_table *ptable;
258 lnet_peer_t *lp = NULL;
264 if (the_lnet.ln_shutdown) /* it's shutting down */
267 /* cpt can be LNET_LOCK_EX if it's called from router functions */
268 cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
270 ptable = the_lnet.ln_peer_tables[cpt2];
271 lp = lnet_find_peer_locked(ptable, nid);
277 if (!list_empty(&ptable->pt_deathrow)) {
278 lp = list_entry(ptable->pt_deathrow.next,
279 lnet_peer_t, lp_hashlist);
280 list_del(&lp->lp_hashlist);
284 * take extra refcount in case another thread has shutdown LNet
285 * and destroyed locks and peer-table before I finish the allocation
288 lnet_net_unlock(cpt);
291 memset(lp, 0, sizeof(*lp));
293 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
301 INIT_LIST_HEAD(&lp->lp_txq);
302 INIT_LIST_HEAD(&lp->lp_rtrq);
303 INIT_LIST_HEAD(&lp->lp_routes);
306 lp->lp_notifylnd = 0;
307 lp->lp_notifying = 0;
308 lp->lp_alive_count = 0;
309 lp->lp_timestamp = 0;
310 lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
311 lp->lp_last_alive = cfs_time_current(); /* assumes alive */
312 lp->lp_last_query = 0; /* haven't asked NI yet */
313 lp->lp_ping_timestamp = 0;
314 lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
317 lp->lp_refcount = 2; /* 1 for caller; 1 for hash */
318 lp->lp_rtr_refcount = 0;
322 if (the_lnet.ln_shutdown) {
327 lp2 = lnet_find_peer_locked(ptable, nid);
333 lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
334 if (lp->lp_ni == NULL) {
340 lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
342 lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
344 list_add_tail(&lp->lp_hashlist,
345 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
346 ptable->pt_version++;
352 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
358 lnet_debug_peer(lnet_nid_t nid)
360 char *aliveness = "NA";
365 cpt = lnet_cpt_of_nid(nid);
368 rc = lnet_nid2peer_locked(&lp, nid, cpt);
370 lnet_net_unlock(cpt);
371 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
375 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
376 aliveness = lp->lp_alive ? "up" : "down";
378 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
379 libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
380 aliveness, lp->lp_ni->ni_peertxcredits,
381 lp->lp_rtrcredits, lp->lp_minrtrcredits,
382 lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
384 lnet_peer_decref_locked(lp);
386 lnet_net_unlock(cpt);
389 int lnet_get_peer_info(__u32 peer_index, __u64 *nid,
390 char aliveness[LNET_MAX_STR_LEN],
391 __u32 *cpt_iter, __u32 *refcount,
392 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
393 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
396 struct lnet_peer_table *peer_table;
402 /* get the number of CPTs */
403 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
405 /* if the cpt number to be examined is >= the number of cpts in
406 * the system then indicate that there are no more cpts to examin
408 if (*cpt_iter > lncpt)
411 /* get the current table */
412 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
413 /* if the ptable is NULL then there are no more cpts to examine */
414 if (peer_table == NULL)
417 lnet_net_lock(*cpt_iter);
419 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
420 struct list_head *peers = &peer_table->pt_hash[j];
422 list_for_each_entry(lp, peers, lp_hashlist) {
423 if (peer_index-- > 0)
426 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
427 if (lnet_isrouter(lp) ||
428 lnet_peer_aliveness_enabled(lp))
429 snprintf(aliveness, LNET_MAX_STR_LEN,
430 lp->lp_alive ? "up" : "down");
433 *refcount = lp->lp_refcount;
434 *ni_peer_tx_credits = lp->lp_ni->ni_peertxcredits;
435 *peer_tx_credits = lp->lp_txcredits;
436 *peer_rtr_credits = lp->lp_rtrcredits;
437 *peer_min_rtr_credits = lp->lp_mintxcredits;
438 *peer_tx_qnob = lp->lp_txqnob;
444 lnet_net_unlock(*cpt_iter);
448 return found ? 0 : -ENOENT;