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 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
57 LNET_PEER_HASH_SIZE * sizeof(*hash));
59 CERROR("Failed to create peer hash table\n");
60 lnet_peer_tables_destroy();
64 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
65 INIT_LIST_HEAD(&hash[j]);
66 ptable->pt_hash = hash; /* sign of initialization */
73 lnet_peer_tables_destroy(void)
75 struct lnet_peer_table *ptable;
76 struct list_head *hash;
80 if (the_lnet.ln_peer_tables == NULL)
83 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
84 hash = ptable->pt_hash;
85 if (hash == NULL) /* not intialized */
88 ptable->pt_hash = NULL;
89 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
90 LASSERT(list_empty(&hash[j]));
92 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
95 cfs_percpt_free(the_lnet.ln_peer_tables);
96 the_lnet.ln_peer_tables = NULL;
100 lnet_peer_table_cleanup_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable)
103 struct lnet_peer_ni *lp;
104 struct lnet_peer_ni *tmp;
106 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
107 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
109 if (ni != NULL && ni->ni_net != lp->lpni_net)
111 list_del_init(&lp->lpni_hashlist);
112 /* Lose hash table's ref */
113 ptable->pt_zombies++;
114 lnet_peer_ni_decref_locked(lp);
120 lnet_peer_table_finalize_wait_locked(struct lnet_peer_table *ptable,
125 for (i = 3; ptable->pt_zombies != 0; i++) {
126 lnet_net_unlock(cpt_locked);
130 "Waiting for %d zombies on peer table\n",
133 set_current_state(TASK_UNINTERRUPTIBLE);
134 schedule_timeout(cfs_time_seconds(1) >> 1);
135 lnet_net_lock(cpt_locked);
140 lnet_peer_table_del_rtrs_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable,
143 struct lnet_peer_ni *lp;
144 struct lnet_peer_ni *tmp;
148 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
149 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
151 if (ni->ni_net != lp->lpni_net)
154 if (lp->lpni_rtr_refcount == 0)
157 lpni_nid = lp->lpni_nid;
159 lnet_net_unlock(cpt_locked);
160 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
161 lnet_net_lock(cpt_locked);
167 lnet_peer_tables_cleanup(lnet_ni_t *ni)
170 struct lnet_peer_table *ptable;
172 LASSERT(the_lnet.ln_shutdown || ni != NULL);
173 /* If just deleting the peers for a NI, get rid of any routes these
174 * peers are gateways for. */
175 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
176 lnet_net_lock(LNET_LOCK_EX);
177 lnet_peer_table_del_rtrs_locked(ni, ptable, i);
178 lnet_net_unlock(LNET_LOCK_EX);
181 /* Start the cleanup process */
182 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
183 lnet_net_lock(LNET_LOCK_EX);
184 lnet_peer_table_cleanup_locked(ni, ptable);
185 lnet_net_unlock(LNET_LOCK_EX);
188 /* Wait until all peers have been destroyed. */
189 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
190 lnet_net_lock(LNET_LOCK_EX);
191 lnet_peer_table_finalize_wait_locked(ptable, i);
192 lnet_net_unlock(LNET_LOCK_EX);
196 static struct lnet_peer_ni *
197 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
199 struct list_head *peers;
200 struct lnet_peer_ni *lp;
202 LASSERT(!the_lnet.ln_shutdown);
204 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
205 list_for_each_entry(lp, peers, lpni_hashlist) {
206 if (lp->lpni_nid == nid) {
207 lnet_peer_ni_addref_locked(lp);
215 struct lnet_peer_ni *
216 lnet_find_peer_ni_locked(lnet_nid_t nid, int cpt)
218 struct lnet_peer_ni *lpni;
219 struct lnet_peer_table *ptable;
221 ptable = the_lnet.ln_peer_tables[cpt];
222 lpni = lnet_get_peer_ni_locked(ptable, nid);
228 lnet_try_destroy_peer_hierarchy_locked(struct lnet_peer_ni *lpni)
230 struct lnet_peer_net *peer_net;
231 struct lnet_peer *peer;
233 /* TODO: could the below situation happen? accessing an already
235 if (lpni->lpni_peer_net == NULL ||
236 lpni->lpni_peer_net->lpn_peer == NULL)
239 peer_net = lpni->lpni_peer_net;
240 peer = lpni->lpni_peer_net->lpn_peer;
242 list_del_init(&lpni->lpni_on_peer_net_list);
243 lpni->lpni_peer_net = NULL;
245 /* if peer_net is empty, then remove it from the peer */
246 if (list_empty(&peer_net->lpn_peer_nis)) {
247 list_del_init(&peer_net->lpn_on_peer_list);
248 peer_net->lpn_peer = NULL;
249 LIBCFS_FREE(peer_net, sizeof(*peer_net));
251 /* if the peer is empty then remove it from the
252 * the_lnet.ln_peers */
253 if (list_empty(&peer->lp_peer_nets)) {
254 list_del_init(&peer->lp_on_lnet_peer_list);
255 LIBCFS_FREE(peer, sizeof(*peer));
261 lnet_build_peer_hierarchy(struct lnet_peer_ni *lpni)
263 struct lnet_peer *peer;
264 struct lnet_peer_net *peer_net;
265 __u32 lpni_net = LNET_NIDNET(lpni->lpni_nid);
270 LIBCFS_ALLOC(peer, sizeof(*peer));
274 LIBCFS_ALLOC(peer_net, sizeof(*peer_net));
275 if (peer_net == NULL) {
276 LIBCFS_FREE(peer, sizeof(*peer));
280 INIT_LIST_HEAD(&peer->lp_on_lnet_peer_list);
281 INIT_LIST_HEAD(&peer->lp_peer_nets);
282 INIT_LIST_HEAD(&peer_net->lpn_on_peer_list);
283 INIT_LIST_HEAD(&peer_net->lpn_peer_nis);
285 /* build the hierarchy */
286 peer_net->lpn_net_id = lpni_net;
287 peer_net->lpn_peer = peer;
288 lpni->lpni_peer_net = peer_net;
289 peer->lp_primary_nid = lpni->lpni_nid;
290 list_add_tail(&peer_net->lpn_on_peer_list, &peer->lp_peer_nets);
291 list_add_tail(&lpni->lpni_on_peer_net_list, &peer_net->lpn_peer_nis);
292 list_add_tail(&peer->lp_on_lnet_peer_list, &the_lnet.ln_peers);
298 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
300 struct lnet_peer_table *ptable;
302 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
303 LASSERT(lpni->lpni_rtr_refcount == 0);
304 LASSERT(list_empty(&lpni->lpni_txq));
305 LASSERT(list_empty(&lpni->lpni_hashlist));
306 LASSERT(lpni->lpni_txqnob == 0);
307 LASSERT(lpni->lpni_peer_net != NULL);
308 LASSERT(lpni->lpni_peer_net->lpn_peer != NULL);
310 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
311 LASSERT(ptable->pt_number > 0);
314 lpni->lpni_net = NULL;
316 lnet_try_destroy_peer_hierarchy_locked(lpni);
318 LIBCFS_FREE(lpni, sizeof(*lpni));
320 LASSERT(ptable->pt_zombies > 0);
321 ptable->pt_zombies--;
325 lnet_nid2peerni_locked(struct lnet_peer_ni **lpnip, lnet_nid_t nid, int cpt)
327 struct lnet_peer_table *ptable;
328 struct lnet_peer_ni *lpni = NULL;
329 struct lnet_peer_ni *lpni2;
334 if (the_lnet.ln_shutdown) /* it's shutting down */
338 * calculate cpt2 with the standard hash function
339 * This cpt2 becomes the slot where we'll find or create the peer.
341 cpt2 = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
344 * Any changes to the peer tables happen under exclusive write
345 * lock. Any reads to the peer tables can be done via a standard
348 if (cpt != LNET_LOCK_EX) {
349 lnet_net_unlock(cpt);
350 lnet_net_lock(LNET_LOCK_EX);
353 ptable = the_lnet.ln_peer_tables[cpt2];
354 lpni = lnet_get_peer_ni_locked(ptable, nid);
357 if (cpt != LNET_LOCK_EX) {
358 lnet_net_unlock(LNET_LOCK_EX);
365 * take extra refcount in case another thread has shutdown LNet
366 * and destroyed locks and peer-table before I finish the allocation
369 lnet_net_unlock(LNET_LOCK_EX);
371 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt2, sizeof(*lpni));
379 INIT_LIST_HEAD(&lpni->lpni_txq);
380 INIT_LIST_HEAD(&lpni->lpni_rtrq);
381 INIT_LIST_HEAD(&lpni->lpni_routes);
383 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
384 lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
385 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
386 lpni->lpni_nid = nid;
387 lpni->lpni_cpt = cpt2;
388 atomic_set(&lpni->lpni_refcount, 2); /* 1 for caller; 1 for hash */
390 rc = lnet_build_peer_hierarchy(lpni);
394 lnet_net_lock(LNET_LOCK_EX);
396 if (the_lnet.ln_shutdown) {
401 lpni2 = lnet_get_peer_ni_locked(ptable, nid);
407 lpni->lpni_net = lnet_get_net_locked(LNET_NIDNET(lpni->lpni_nid));
408 lpni->lpni_txcredits =
409 lpni->lpni_mintxcredits =
410 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
411 lpni->lpni_rtrcredits =
412 lpni->lpni_minrtrcredits =
413 lnet_peer_buffer_credits(lpni->lpni_net);
415 list_add_tail(&lpni->lpni_hashlist,
416 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
417 ptable->pt_version++;
420 if (cpt != LNET_LOCK_EX) {
421 lnet_net_unlock(LNET_LOCK_EX);
428 lnet_try_destroy_peer_hierarchy_locked(lpni);
429 LIBCFS_FREE(lpni, sizeof(*lpni));
432 if (cpt != LNET_LOCK_EX) {
433 lnet_net_unlock(LNET_LOCK_EX);
440 lnet_debug_peer(lnet_nid_t nid)
442 char *aliveness = "NA";
443 struct lnet_peer_ni *lp;
447 cpt = lnet_cpt_of_nid(nid, NULL);
450 rc = lnet_nid2peerni_locked(&lp, nid, cpt);
452 lnet_net_unlock(cpt);
453 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
457 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
458 aliveness = lp->lpni_alive ? "up" : "down";
460 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
461 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
462 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
463 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
464 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
466 lnet_peer_ni_decref_locked(lp);
468 lnet_net_unlock(cpt);
471 int lnet_get_peer_info(__u32 peer_index, __u64 *nid,
472 char aliveness[LNET_MAX_STR_LEN],
473 __u32 *cpt_iter, __u32 *refcount,
474 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
475 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
478 struct lnet_peer_table *peer_table;
479 struct lnet_peer_ni *lp;
484 /* get the number of CPTs */
485 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
487 /* if the cpt number to be examined is >= the number of cpts in
488 * the system then indicate that there are no more cpts to examin
490 if (*cpt_iter >= lncpt)
493 /* get the current table */
494 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
495 /* if the ptable is NULL then there are no more cpts to examine */
496 if (peer_table == NULL)
499 lnet_net_lock(*cpt_iter);
501 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
502 struct list_head *peers = &peer_table->pt_hash[j];
504 list_for_each_entry(lp, peers, lpni_hashlist) {
505 if (peer_index-- > 0)
508 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
509 if (lnet_isrouter(lp) ||
510 lnet_peer_aliveness_enabled(lp))
511 snprintf(aliveness, LNET_MAX_STR_LEN,
512 lp->lpni_alive ? "up" : "down");
515 *refcount = atomic_read(&lp->lpni_refcount);
516 *ni_peer_tx_credits =
517 lp->lpni_net->net_tunables.lct_peer_tx_credits;
518 *peer_tx_credits = lp->lpni_txcredits;
519 *peer_rtr_credits = lp->lpni_rtrcredits;
520 *peer_min_rtr_credits = lp->lpni_mintxcredits;
521 *peer_tx_qnob = lp->lpni_txqnob;
527 lnet_net_unlock(*cpt_iter);
531 return found ? 0 : -ENOENT;