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_remove_from_remote_list(struct lnet_peer_ni *lpni)
43 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
44 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
45 lnet_peer_ni_decref_locked(lpni);
50 lnet_peer_net_added(struct lnet_net *net)
52 struct lnet_peer_ni *lpni, *tmp;
54 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
55 lpni_on_remote_peer_ni_list) {
57 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
60 spin_lock(&lpni->lpni_lock);
61 lpni->lpni_txcredits =
62 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
63 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
64 lpni->lpni_rtrcredits =
65 lnet_peer_buffer_credits(lpni->lpni_net);
66 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
67 spin_unlock(&lpni->lpni_lock);
69 lnet_peer_remove_from_remote_list(lpni);
75 lnet_peer_tables_destroy(void)
77 struct lnet_peer_table *ptable;
78 struct list_head *hash;
82 if (!the_lnet.ln_peer_tables)
85 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
86 hash = ptable->pt_hash;
87 if (!hash) /* not intialized */
90 LASSERT(list_empty(&ptable->pt_zombie_list));
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_tables_create(void)
106 struct lnet_peer_table *ptable;
107 struct list_head *hash;
111 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
113 if (the_lnet.ln_peer_tables == NULL) {
114 CERROR("Failed to allocate cpu-partition peer tables\n");
118 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
119 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
120 LNET_PEER_HASH_SIZE * sizeof(*hash));
122 CERROR("Failed to create peer hash table\n");
123 lnet_peer_tables_destroy();
127 spin_lock_init(&ptable->pt_zombie_lock);
128 INIT_LIST_HEAD(&ptable->pt_zombie_list);
130 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
131 INIT_LIST_HEAD(&hash[j]);
132 ptable->pt_hash = hash; /* sign of initialization */
138 static struct lnet_peer_ni *
139 lnet_peer_ni_alloc(lnet_nid_t nid)
141 struct lnet_peer_ni *lpni;
142 struct lnet_net *net;
145 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
147 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
151 INIT_LIST_HEAD(&lpni->lpni_txq);
152 INIT_LIST_HEAD(&lpni->lpni_rtrq);
153 INIT_LIST_HEAD(&lpni->lpni_routes);
154 INIT_LIST_HEAD(&lpni->lpni_hashlist);
155 INIT_LIST_HEAD(&lpni->lpni_on_peer_net_list);
156 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
158 spin_lock_init(&lpni->lpni_lock);
160 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
161 lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
162 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
163 lpni->lpni_nid = nid;
164 lpni->lpni_cpt = cpt;
165 lnet_set_peer_ni_health_locked(lpni, true);
167 net = lnet_get_net_locked(LNET_NIDNET(nid));
168 lpni->lpni_net = net;
170 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
171 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
172 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
173 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
176 * This peer_ni is not on a local network, so we
177 * cannot add the credits here. In case the net is
178 * added later, add the peer_ni to the remote peer ni
179 * list so it can be easily found and revisited.
181 /* FIXME: per-net implementation instead? */
182 atomic_inc(&lpni->lpni_refcount);
183 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
184 &the_lnet.ln_remote_peer_ni_list);
187 /* TODO: update flags */
192 static struct lnet_peer_net *
193 lnet_peer_net_alloc(__u32 net_id)
195 struct lnet_peer_net *lpn;
197 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
201 INIT_LIST_HEAD(&lpn->lpn_on_peer_list);
202 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
203 lpn->lpn_net_id = net_id;
208 static struct lnet_peer *
209 lnet_peer_alloc(lnet_nid_t nid)
211 struct lnet_peer *lp;
213 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
217 INIT_LIST_HEAD(&lp->lp_on_lnet_peer_list);
218 INIT_LIST_HEAD(&lp->lp_peer_nets);
219 lp->lp_primary_nid = nid;
221 /* TODO: update flags */
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));
260 /* called with lnet_net_lock LNET_LOCK_EX held */
262 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
264 struct lnet_peer_table *ptable = NULL;
266 lnet_peer_remove_from_remote_list(lpni);
268 /* remove peer ni from the hash list. */
269 list_del_init(&lpni->lpni_hashlist);
271 /* decrement the ref count on the peer table */
272 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
273 LASSERT(atomic_read(&ptable->pt_number) > 0);
274 atomic_dec(&ptable->pt_number);
277 * The peer_ni can no longer be found with a lookup. But there
278 * can be current users, so keep track of it on the zombie
279 * list until the reference count has gone to zero.
281 * The last reference may be lost in a place where the
282 * lnet_net_lock locks only a single cpt, and that cpt may not
283 * be lpni->lpni_cpt. So the zombie list of this peer_table
286 spin_lock(&ptable->pt_zombie_lock);
287 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
288 ptable->pt_zombies++;
289 spin_unlock(&ptable->pt_zombie_lock);
291 /* no need to keep this peer on the hierarchy anymore */
292 lnet_try_destroy_peer_hierarchy_locked(lpni);
294 /* decrement reference on peer */
295 lnet_peer_ni_decref_locked(lpni);
298 void lnet_peer_uninit()
300 struct lnet_peer_ni *lpni, *tmp;
302 lnet_net_lock(LNET_LOCK_EX);
304 /* remove all peer_nis from the remote peer and the hash list */
305 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
306 lpni_on_remote_peer_ni_list)
307 lnet_peer_ni_del_locked(lpni);
309 lnet_peer_tables_destroy();
311 lnet_net_unlock(LNET_LOCK_EX);
315 lnet_peer_del_locked(struct lnet_peer *peer)
317 struct lnet_peer_ni *lpni = NULL, *lpni2;
319 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
320 while (lpni != NULL) {
321 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
322 lnet_peer_ni_del_locked(lpni);
328 lnet_peer_table_cleanup_locked(struct lnet_net *net,
329 struct lnet_peer_table *ptable)
332 struct lnet_peer_ni *lpni;
333 struct lnet_peer_ni *tmp;
334 struct lnet_peer *peer;
336 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
337 list_for_each_entry_safe(lpni, tmp, &ptable->pt_hash[i],
339 if (net != NULL && net != lpni->lpni_net)
343 * check if by removing this peer ni we should be
344 * removing the entire peer.
346 peer = lpni->lpni_peer_net->lpn_peer;
348 if (peer->lp_primary_nid == lpni->lpni_nid)
349 lnet_peer_del_locked(peer);
351 lnet_peer_ni_del_locked(lpni);
357 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
361 spin_lock(&ptable->pt_zombie_lock);
362 while (ptable->pt_zombies) {
363 spin_unlock(&ptable->pt_zombie_lock);
367 "Waiting for %d zombies on peer table\n",
370 set_current_state(TASK_UNINTERRUPTIBLE);
371 schedule_timeout(cfs_time_seconds(1) >> 1);
372 spin_lock(&ptable->pt_zombie_lock);
374 spin_unlock(&ptable->pt_zombie_lock);
378 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
379 struct lnet_peer_table *ptable)
381 struct lnet_peer_ni *lp;
382 struct lnet_peer_ni *tmp;
386 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
387 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
389 if (net != lp->lpni_net)
392 if (lp->lpni_rtr_refcount == 0)
395 lpni_nid = lp->lpni_nid;
397 lnet_net_unlock(LNET_LOCK_EX);
398 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
399 lnet_net_lock(LNET_LOCK_EX);
405 lnet_peer_tables_cleanup(struct lnet_net *net)
408 struct lnet_peer_table *ptable;
410 LASSERT(the_lnet.ln_shutdown || net != NULL);
411 /* If just deleting the peers for a NI, get rid of any routes these
412 * peers are gateways for. */
413 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
414 lnet_net_lock(LNET_LOCK_EX);
415 lnet_peer_table_del_rtrs_locked(net, ptable);
416 lnet_net_unlock(LNET_LOCK_EX);
419 /* Start the cleanup process */
420 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
421 lnet_net_lock(LNET_LOCK_EX);
422 lnet_peer_table_cleanup_locked(net, ptable);
423 lnet_net_unlock(LNET_LOCK_EX);
426 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
427 lnet_peer_ni_finalize_wait(ptable);
430 static struct lnet_peer_ni *
431 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
433 struct list_head *peers;
434 struct lnet_peer_ni *lp;
436 LASSERT(!the_lnet.ln_shutdown);
438 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
439 list_for_each_entry(lp, peers, lpni_hashlist) {
440 if (lp->lpni_nid == nid) {
441 lnet_peer_ni_addref_locked(lp);
449 struct lnet_peer_ni *
450 lnet_find_peer_ni_locked(lnet_nid_t nid)
452 struct lnet_peer_ni *lpni;
453 struct lnet_peer_table *ptable;
456 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
458 ptable = the_lnet.ln_peer_tables[cpt];
459 lpni = lnet_get_peer_ni_locked(ptable, nid);
465 lnet_find_or_create_peer_locked(lnet_nid_t dst_nid, int cpt)
467 struct lnet_peer_ni *lpni;
468 struct lnet_peer *lp;
470 lpni = lnet_find_peer_ni_locked(dst_nid);
472 lpni = lnet_nid2peerni_locked(dst_nid, cpt);
474 return ERR_CAST(lpni);
477 lp = lpni->lpni_peer_net->lpn_peer;
478 lnet_peer_ni_decref_locked(lpni);
483 struct lnet_peer_ni *
484 lnet_get_peer_ni_idx_locked(int idx, struct lnet_peer_net **lpn,
485 struct lnet_peer **lp)
487 struct lnet_peer_ni *lpni;
489 list_for_each_entry((*lp), &the_lnet.ln_peers, lp_on_lnet_peer_list) {
490 list_for_each_entry((*lpn), &((*lp)->lp_peer_nets), lpn_on_peer_list) {
491 list_for_each_entry(lpni, &((*lpn)->lpn_peer_nis),
492 lpni_on_peer_net_list)
501 struct lnet_peer_ni *
502 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
503 struct lnet_peer_net *peer_net,
504 struct lnet_peer_ni *prev)
506 struct lnet_peer_ni *lpni;
507 struct lnet_peer_net *net = peer_net;
511 net = list_entry(peer->lp_peer_nets.next,
512 struct lnet_peer_net,
514 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
515 lpni_on_peer_net_list);
520 if (prev->lpni_on_peer_net_list.next ==
521 &prev->lpni_peer_net->lpn_peer_nis) {
523 * if you reached the end of the peer ni list and the peer
524 * net is specified then there are no more peer nis in that
531 * we reached the end of this net ni list. move to the
534 if (prev->lpni_peer_net->lpn_on_peer_list.next ==
536 /* no more nets and no more NIs. */
539 /* get the next net */
540 net = list_entry(prev->lpni_peer_net->lpn_on_peer_list.next,
541 struct lnet_peer_net,
543 /* get the ni on it */
544 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
545 lpni_on_peer_net_list);
550 /* there are more nis left */
551 lpni = list_entry(prev->lpni_on_peer_net_list.next,
552 struct lnet_peer_ni, lpni_on_peer_net_list);
558 lnet_peer_is_ni_pref_locked(struct lnet_peer_ni *lpni, struct lnet_ni *ni)
562 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
563 if (lpni->lpni_pref_nids[i] == ni->ni_nid)
570 lnet_peer_primary_nid(lnet_nid_t nid)
572 struct lnet_peer_ni *lpni;
573 lnet_nid_t primary_nid = nid;
576 cpt = lnet_net_lock_current();
577 lpni = lnet_find_peer_ni_locked(nid);
579 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
580 lnet_peer_ni_decref_locked(lpni);
582 lnet_net_unlock(cpt);
587 struct lnet_peer_net *
588 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
590 struct lnet_peer_net *peer_net;
591 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
592 if (peer_net->lpn_net_id == net_id)
599 lnet_peer_setup_hierarchy(struct lnet_peer *lp, struct lnet_peer_ni *lpni,
602 struct lnet_peer_net *lpn = NULL;
603 struct lnet_peer_table *ptable;
604 __u32 net_id = LNET_NIDNET(nid);
607 * Create the peer_ni, peer_net, and peer if they don't exist
611 lpn = lnet_peer_get_net_locked(lp, net_id);
613 lp = lnet_peer_alloc(nid);
619 lpn = lnet_peer_net_alloc(net_id);
621 goto out_maybe_free_lp;
625 lpni = lnet_peer_ni_alloc(nid);
627 goto out_maybe_free_lpn;
630 /* Install the new peer_ni */
631 lnet_net_lock(LNET_LOCK_EX);
632 /* Add peer_ni to global peer table hash, if necessary. */
633 if (list_empty(&lpni->lpni_hashlist)) {
634 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
635 list_add_tail(&lpni->lpni_hashlist,
636 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
637 ptable->pt_version++;
638 atomic_inc(&ptable->pt_number);
639 atomic_inc(&lpni->lpni_refcount);
642 /* Detach the peer_ni from an existing peer, if necessary. */
643 if (lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer != lp)
644 lnet_try_destroy_peer_hierarchy_locked(lpni);
646 /* Add peer_ni to peer_net */
647 lpni->lpni_peer_net = lpn;
648 list_add_tail(&lpni->lpni_on_peer_net_list, &lpn->lpn_peer_nis);
650 /* Add peer_net to peer */
651 if (!lpn->lpn_peer) {
653 list_add_tail(&lpn->lpn_on_peer_list, &lp->lp_peer_nets);
656 /* Add peer to global peer list */
657 if (list_empty(&lp->lp_on_lnet_peer_list))
658 list_add_tail(&lp->lp_on_lnet_peer_list, &the_lnet.ln_peers);
659 lnet_net_unlock(LNET_LOCK_EX);
664 if (list_empty(&lpn->lpn_on_peer_list))
665 LIBCFS_FREE(lpn, sizeof(*lpn));
667 if (list_empty(&lp->lp_on_lnet_peer_list))
668 LIBCFS_FREE(lp, sizeof(*lp));
674 lnet_add_prim_lpni(lnet_nid_t nid)
677 struct lnet_peer *peer;
678 struct lnet_peer_ni *lpni;
680 LASSERT(nid != LNET_NID_ANY);
683 * lookup the NID and its peer
684 * if the peer doesn't exist, create it.
685 * if this is a non-MR peer then change its state to MR and exit.
686 * if this is an MR peer and it's a primary NI: NO-OP.
687 * if this is an MR peer and it's not a primary NI. Operation not
690 * The adding and deleting of peer nis is being serialized through
691 * the api_mutex. So we can look up peers with the mutex locked
692 * safely. Only when we need to change the ptable, do we need to
693 * exclusively lock the lnet_net_lock()
695 lpni = lnet_find_peer_ni_locked(nid);
697 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
700 lpni = lnet_find_peer_ni_locked(nid);
705 lnet_peer_ni_decref_locked(lpni);
707 peer = lpni->lpni_peer_net->lpn_peer;
710 * If we found a lpni with the same nid as the NID we're trying to
711 * create, then we're trying to create an already existing lpni
712 * that belongs to a different peer
714 if (peer->lp_primary_nid != nid)
718 * if we found an lpni that is not a multi-rail, which could occur
719 * if lpni is already created as a non-mr lpni or we just created
720 * it, then make sure you indicate that this lpni is a primary mr
723 * TODO: update flags if necessary
725 if (!peer->lp_multi_rail && peer->lp_primary_nid == nid)
726 peer->lp_multi_rail = true;
732 lnet_add_peer_ni_to_prim_lpni(lnet_nid_t key_nid, lnet_nid_t nid)
734 struct lnet_peer *peer, *primary_peer;
735 struct lnet_peer_ni *lpni = NULL, *klpni = NULL;
737 LASSERT(key_nid != LNET_NID_ANY && nid != LNET_NID_ANY);
740 * key nid must be created by this point. If not then this
741 * operation is not permitted
743 klpni = lnet_find_peer_ni_locked(key_nid);
747 lnet_peer_ni_decref_locked(klpni);
749 primary_peer = klpni->lpni_peer_net->lpn_peer;
751 lpni = lnet_find_peer_ni_locked(nid);
753 lnet_peer_ni_decref_locked(lpni);
755 peer = lpni->lpni_peer_net->lpn_peer;
757 * lpni already exists in the system but it belongs to
758 * a different peer. We can't re-added it
760 if (peer->lp_primary_nid != key_nid && peer->lp_multi_rail) {
761 CERROR("Cannot add NID %s owned by peer %s to peer %s\n",
762 libcfs_nid2str(lpni->lpni_nid),
763 libcfs_nid2str(peer->lp_primary_nid),
764 libcfs_nid2str(key_nid));
766 } else if (peer->lp_primary_nid == key_nid) {
768 * found a peer_ni that is already part of the
769 * peer. This is a no-op operation.
775 * TODO: else if (peer->lp_primary_nid != key_nid &&
776 * !peer->lp_multi_rail)
777 * peer is not an MR peer and it will be moved in the next
778 * step to klpni, so update its flags accordingly.
779 * lnet_move_peer_ni()
783 * TODO: call lnet_update_peer() from here to update the
784 * flags. This is the case when the lpni you're trying to
785 * add is already part of the peer. This could've been
786 * added by the DD previously, so go ahead and do any
787 * updates to the state if necessary
793 * When we get here we either have found an existing lpni, which
794 * we can switch to the new peer. Or we need to create one and
795 * add it to the new peer
797 return lnet_peer_setup_hierarchy(primary_peer, lpni, nid);
801 * lpni creation initiated due to traffic either sending or receiving.
804 lnet_peer_ni_traffic_add(lnet_nid_t nid)
806 struct lnet_peer_ni *lpni;
809 if (nid == LNET_NID_ANY)
812 /* lnet_net_lock is not needed here because ln_api_lock is held */
813 lpni = lnet_find_peer_ni_locked(nid);
816 * TODO: lnet_update_primary_nid() but not all of it
817 * only indicate if we're converting this to MR capable
818 * Can happen due to DD
820 lnet_peer_ni_decref_locked(lpni);
822 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
830 lnet_peer_ni_add_non_mr(lnet_nid_t nid)
832 struct lnet_peer_ni *lpni;
834 lpni = lnet_find_peer_ni_locked(nid);
836 CERROR("Cannot add %s as non-mr when it already exists\n",
837 libcfs_nid2str(nid));
838 lnet_peer_ni_decref_locked(lpni);
842 return lnet_peer_setup_hierarchy(NULL, NULL, nid);
846 * This API handles the following combinations:
847 * Create a primary NI if only the key_nid is provided
848 * Create or add an lpni to a primary NI. Primary NI must've already
850 * Create a non-MR peer.
853 lnet_add_peer_ni_to_peer(lnet_nid_t key_nid, lnet_nid_t nid, bool mr)
856 * Caller trying to setup an MR like peer hierarchy but
857 * specifying it to be non-MR. This is not allowed.
859 if (key_nid != LNET_NID_ANY &&
860 nid != LNET_NID_ANY && !mr)
863 /* Add the primary NID of a peer */
864 if (key_nid != LNET_NID_ANY &&
865 nid == LNET_NID_ANY && mr)
866 return lnet_add_prim_lpni(key_nid);
868 /* Add a NID to an existing peer */
869 if (key_nid != LNET_NID_ANY &&
870 nid != LNET_NID_ANY && mr)
871 return lnet_add_peer_ni_to_prim_lpni(key_nid, nid);
873 /* Add a non-MR peer NI */
874 if (((key_nid != LNET_NID_ANY &&
875 nid == LNET_NID_ANY) ||
876 (key_nid == LNET_NID_ANY &&
877 nid != LNET_NID_ANY)) && !mr)
878 return lnet_peer_ni_add_non_mr(key_nid != LNET_NID_ANY ?
885 lnet_del_peer_ni_from_peer(lnet_nid_t key_nid, lnet_nid_t nid)
887 lnet_nid_t local_nid;
888 struct lnet_peer *peer;
889 struct lnet_peer_ni *lpni;
891 if (key_nid == LNET_NID_ANY)
894 local_nid = (nid != LNET_NID_ANY) ? nid : key_nid;
896 lpni = lnet_find_peer_ni_locked(local_nid);
899 lnet_peer_ni_decref_locked(lpni);
901 peer = lpni->lpni_peer_net->lpn_peer;
902 LASSERT(peer != NULL);
904 if (peer->lp_primary_nid == lpni->lpni_nid) {
906 * deleting the primary ni is equivalent to deleting the
909 lnet_net_lock(LNET_LOCK_EX);
910 lnet_peer_del_locked(peer);
911 lnet_net_unlock(LNET_LOCK_EX);
916 lnet_net_lock(LNET_LOCK_EX);
917 lnet_peer_ni_del_locked(lpni);
918 lnet_net_unlock(LNET_LOCK_EX);
924 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
926 struct lnet_peer_table *ptable;
928 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
929 LASSERT(lpni->lpni_rtr_refcount == 0);
930 LASSERT(list_empty(&lpni->lpni_txq));
931 LASSERT(lpni->lpni_txqnob == 0);
933 lpni->lpni_net = NULL;
935 /* remove the peer ni from the zombie list */
936 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
937 spin_lock(&ptable->pt_zombie_lock);
938 list_del_init(&lpni->lpni_hashlist);
939 ptable->pt_zombies--;
940 spin_unlock(&ptable->pt_zombie_lock);
942 LIBCFS_FREE(lpni, sizeof(*lpni));
945 struct lnet_peer_ni *
946 lnet_nid2peerni_locked(lnet_nid_t nid, int cpt)
948 struct lnet_peer_table *ptable;
949 struct lnet_peer_ni *lpni = NULL;
953 if (the_lnet.ln_shutdown) /* it's shutting down */
954 return ERR_PTR(-ESHUTDOWN);
957 * calculate cpt2 with the standard hash function
958 * This cpt2 is the slot where we'll find or create the peer.
960 cpt2 = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
961 ptable = the_lnet.ln_peer_tables[cpt2];
962 lpni = lnet_get_peer_ni_locked(ptable, nid);
966 /* Slow path: serialized using the ln_api_mutex. */
967 lnet_net_unlock(cpt);
968 mutex_lock(&the_lnet.ln_api_mutex);
970 * Shutdown is only set under the ln_api_lock, so a single
971 * check here is sufficent.
973 * lnet_add_nid_to_peer() also handles the case where we've
974 * raced and a different thread added the NID.
976 if (the_lnet.ln_shutdown) {
977 lpni = ERR_PTR(-ESHUTDOWN);
978 goto out_mutex_unlock;
981 rc = lnet_peer_ni_traffic_add(nid);
984 goto out_mutex_unlock;
987 lpni = lnet_get_peer_ni_locked(ptable, nid);
991 mutex_unlock(&the_lnet.ln_api_mutex);
998 lnet_debug_peer(lnet_nid_t nid)
1000 char *aliveness = "NA";
1001 struct lnet_peer_ni *lp;
1004 cpt = lnet_cpt_of_nid(nid, NULL);
1007 lp = lnet_nid2peerni_locked(nid, cpt);
1009 lnet_net_unlock(cpt);
1010 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
1014 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
1015 aliveness = lp->lpni_alive ? "up" : "down";
1017 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
1018 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
1019 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
1020 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
1021 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
1023 lnet_peer_ni_decref_locked(lp);
1025 lnet_net_unlock(cpt);
1028 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
1029 char aliveness[LNET_MAX_STR_LEN],
1030 __u32 *cpt_iter, __u32 *refcount,
1031 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
1032 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
1033 __u32 *peer_tx_qnob)
1035 struct lnet_peer_table *peer_table;
1036 struct lnet_peer_ni *lp;
1041 /* get the number of CPTs */
1042 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
1044 /* if the cpt number to be examined is >= the number of cpts in
1045 * the system then indicate that there are no more cpts to examin
1047 if (*cpt_iter >= lncpt)
1050 /* get the current table */
1051 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
1052 /* if the ptable is NULL then there are no more cpts to examine */
1053 if (peer_table == NULL)
1056 lnet_net_lock(*cpt_iter);
1058 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
1059 struct list_head *peers = &peer_table->pt_hash[j];
1061 list_for_each_entry(lp, peers, lpni_hashlist) {
1062 if (peer_index-- > 0)
1065 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
1066 if (lnet_isrouter(lp) ||
1067 lnet_peer_aliveness_enabled(lp))
1068 snprintf(aliveness, LNET_MAX_STR_LEN,
1069 lp->lpni_alive ? "up" : "down");
1071 *nid = lp->lpni_nid;
1072 *refcount = atomic_read(&lp->lpni_refcount);
1073 *ni_peer_tx_credits =
1074 lp->lpni_net->net_tunables.lct_peer_tx_credits;
1075 *peer_tx_credits = lp->lpni_txcredits;
1076 *peer_rtr_credits = lp->lpni_rtrcredits;
1077 *peer_min_rtr_credits = lp->lpni_mintxcredits;
1078 *peer_tx_qnob = lp->lpni_txqnob;
1084 lnet_net_unlock(*cpt_iter);
1088 return found ? 0 : -ENOENT;
1091 int lnet_get_peer_info(__u32 idx, lnet_nid_t *primary_nid, lnet_nid_t *nid,
1092 bool *mr, struct lnet_peer_ni_credit_info *peer_ni_info,
1093 struct lnet_ioctl_element_stats *peer_ni_stats)
1095 struct lnet_peer_ni *lpni = NULL;
1096 struct lnet_peer_net *lpn = NULL;
1097 struct lnet_peer *lp = NULL;
1099 lpni = lnet_get_peer_ni_idx_locked(idx, &lpn, &lp);
1104 *primary_nid = lp->lp_primary_nid;
1105 *mr = lp->lp_multi_rail;
1106 *nid = lpni->lpni_nid;
1107 snprintf(peer_ni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
1108 if (lnet_isrouter(lpni) ||
1109 lnet_peer_aliveness_enabled(lpni))
1110 snprintf(peer_ni_info->cr_aliveness, LNET_MAX_STR_LEN,
1111 lpni->lpni_alive ? "up" : "down");
1113 peer_ni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
1114 peer_ni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
1115 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
1116 peer_ni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
1117 peer_ni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
1118 peer_ni_info->cr_peer_min_rtr_credits = lpni->lpni_mintxcredits;
1119 peer_ni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
1121 peer_ni_stats->send_count = atomic_read(&lpni->lpni_stats.send_count);
1122 peer_ni_stats->recv_count = atomic_read(&lpni->lpni_stats.recv_count);
1123 peer_ni_stats->drop_count = atomic_read(&lpni->lpni_stats.drop_count);