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 <uapi/linux/lnet/lnet-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 /* don't remove a peer_ni if it's also a gateway */
267 if (lpni->lpni_rtr_refcount > 0) {
268 CERROR("Peer NI %s is a gateway. Can not delete it\n",
269 libcfs_nid2str(lpni->lpni_nid));
273 lnet_peer_remove_from_remote_list(lpni);
275 /* remove peer ni from the hash list. */
276 list_del_init(&lpni->lpni_hashlist);
278 /* decrement the ref count on the peer table */
279 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
280 LASSERT(atomic_read(&ptable->pt_number) > 0);
281 atomic_dec(&ptable->pt_number);
284 * The peer_ni can no longer be found with a lookup. But there
285 * can be current users, so keep track of it on the zombie
286 * list until the reference count has gone to zero.
288 * The last reference may be lost in a place where the
289 * lnet_net_lock locks only a single cpt, and that cpt may not
290 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
293 spin_lock(&ptable->pt_zombie_lock);
294 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
295 ptable->pt_zombies++;
296 spin_unlock(&ptable->pt_zombie_lock);
298 /* no need to keep this peer on the hierarchy anymore */
299 lnet_try_destroy_peer_hierarchy_locked(lpni);
301 /* decrement reference on peer */
302 lnet_peer_ni_decref_locked(lpni);
307 void lnet_peer_uninit(void)
309 struct lnet_peer_ni *lpni, *tmp;
311 lnet_net_lock(LNET_LOCK_EX);
313 /* remove all peer_nis from the remote peer and the hash list */
314 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
315 lpni_on_remote_peer_ni_list)
316 lnet_peer_ni_del_locked(lpni);
318 lnet_peer_tables_destroy();
320 lnet_net_unlock(LNET_LOCK_EX);
324 lnet_peer_del_locked(struct lnet_peer *peer)
326 struct lnet_peer_ni *lpni = NULL, *lpni2;
329 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
330 while (lpni != NULL) {
331 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
332 rc = lnet_peer_ni_del_locked(lpni);
342 lnet_peer_del(struct lnet_peer *peer)
344 lnet_net_lock(LNET_LOCK_EX);
345 lnet_peer_del_locked(peer);
346 lnet_net_unlock(LNET_LOCK_EX);
352 * Delete a NID from a peer.
353 * Implements a few sanity checks.
354 * Call with ln_api_mutex held.
357 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid)
359 struct lnet_peer *lp2;
360 struct lnet_peer_ni *lpni;
362 lpni = lnet_find_peer_ni_locked(nid);
364 CERROR("Cannot remove unknown nid %s from peer %s\n",
366 libcfs_nid2str(lp->lp_primary_nid));
369 lnet_peer_ni_decref_locked(lpni);
370 lp2 = lpni->lpni_peer_net->lpn_peer;
372 CERROR("Nid %s is attached to peer %s, not peer %s\n",
374 libcfs_nid2str(lp2->lp_primary_nid),
375 libcfs_nid2str(lp->lp_primary_nid));
380 * This function only allows deletion of the primary NID if it
383 if (nid == lp->lp_primary_nid && lnet_get_num_peer_nis(lp) != 1) {
384 CERROR("Cannot delete primary NID %s from multi-NID peer\n",
385 libcfs_nid2str(nid));
389 lnet_net_lock(LNET_LOCK_EX);
390 lnet_peer_ni_del_locked(lpni);
391 lnet_net_unlock(LNET_LOCK_EX);
397 lnet_peer_table_cleanup_locked(struct lnet_net *net,
398 struct lnet_peer_table *ptable)
401 struct lnet_peer_ni *next;
402 struct lnet_peer_ni *lpni;
403 struct lnet_peer *peer;
405 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
406 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
408 if (net != NULL && net != lpni->lpni_net)
411 peer = lpni->lpni_peer_net->lpn_peer;
412 if (peer->lp_primary_nid != lpni->lpni_nid) {
413 lnet_peer_ni_del_locked(lpni);
417 * Removing the primary NID implies removing
418 * the entire peer. Advance next beyond any
419 * peer_ni that belongs to the same peer.
421 list_for_each_entry_from(next, &ptable->pt_hash[i],
423 if (next->lpni_peer_net->lpn_peer != peer)
426 lnet_peer_del_locked(peer);
432 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
436 spin_lock(&ptable->pt_zombie_lock);
437 while (ptable->pt_zombies) {
438 spin_unlock(&ptable->pt_zombie_lock);
440 if (is_power_of_2(i)) {
442 "Waiting for %d zombies on peer table\n",
445 set_current_state(TASK_UNINTERRUPTIBLE);
446 schedule_timeout(cfs_time_seconds(1) >> 1);
447 spin_lock(&ptable->pt_zombie_lock);
449 spin_unlock(&ptable->pt_zombie_lock);
453 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
454 struct lnet_peer_table *ptable)
456 struct lnet_peer_ni *lp;
457 struct lnet_peer_ni *tmp;
461 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
462 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
464 if (net != lp->lpni_net)
467 if (lp->lpni_rtr_refcount == 0)
470 lpni_nid = lp->lpni_nid;
472 lnet_net_unlock(LNET_LOCK_EX);
473 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
474 lnet_net_lock(LNET_LOCK_EX);
480 lnet_peer_tables_cleanup(struct lnet_net *net)
483 struct lnet_peer_table *ptable;
485 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
486 /* If just deleting the peers for a NI, get rid of any routes these
487 * peers are gateways for. */
488 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
489 lnet_net_lock(LNET_LOCK_EX);
490 lnet_peer_table_del_rtrs_locked(net, ptable);
491 lnet_net_unlock(LNET_LOCK_EX);
494 /* Start the cleanup process */
495 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
496 lnet_net_lock(LNET_LOCK_EX);
497 lnet_peer_table_cleanup_locked(net, ptable);
498 lnet_net_unlock(LNET_LOCK_EX);
501 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
502 lnet_peer_ni_finalize_wait(ptable);
505 static struct lnet_peer_ni *
506 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
508 struct list_head *peers;
509 struct lnet_peer_ni *lp;
511 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
513 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
514 list_for_each_entry(lp, peers, lpni_hashlist) {
515 if (lp->lpni_nid == nid) {
516 lnet_peer_ni_addref_locked(lp);
524 struct lnet_peer_ni *
525 lnet_find_peer_ni_locked(lnet_nid_t nid)
527 struct lnet_peer_ni *lpni;
528 struct lnet_peer_table *ptable;
531 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
533 ptable = the_lnet.ln_peer_tables[cpt];
534 lpni = lnet_get_peer_ni_locked(ptable, nid);
539 struct lnet_peer_ni *
540 lnet_get_peer_ni_idx_locked(int idx, struct lnet_peer_net **lpn,
541 struct lnet_peer **lp)
543 struct lnet_peer_ni *lpni;
545 list_for_each_entry((*lp), &the_lnet.ln_peers, lp_on_lnet_peer_list) {
546 list_for_each_entry((*lpn), &((*lp)->lp_peer_nets), lpn_on_peer_list) {
547 list_for_each_entry(lpni, &((*lpn)->lpn_peer_nis),
548 lpni_on_peer_net_list)
557 struct lnet_peer_ni *
558 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
559 struct lnet_peer_net *peer_net,
560 struct lnet_peer_ni *prev)
562 struct lnet_peer_ni *lpni;
563 struct lnet_peer_net *net = peer_net;
567 net = list_entry(peer->lp_peer_nets.next,
568 struct lnet_peer_net,
570 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
571 lpni_on_peer_net_list);
576 if (prev->lpni_on_peer_net_list.next ==
577 &prev->lpni_peer_net->lpn_peer_nis) {
579 * if you reached the end of the peer ni list and the peer
580 * net is specified then there are no more peer nis in that
587 * we reached the end of this net ni list. move to the
590 if (prev->lpni_peer_net->lpn_on_peer_list.next ==
592 /* no more nets and no more NIs. */
595 /* get the next net */
596 net = list_entry(prev->lpni_peer_net->lpn_on_peer_list.next,
597 struct lnet_peer_net,
599 /* get the ni on it */
600 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
601 lpni_on_peer_net_list);
606 /* there are more nis left */
607 lpni = list_entry(prev->lpni_on_peer_net_list.next,
608 struct lnet_peer_ni, lpni_on_peer_net_list);
614 lnet_peer_is_ni_pref_locked(struct lnet_peer_ni *lpni, struct lnet_ni *ni)
618 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
619 if (lpni->lpni_pref_nids[i] == ni->ni_nid)
626 lnet_peer_primary_nid_locked(lnet_nid_t nid)
628 struct lnet_peer_ni *lpni;
629 lnet_nid_t primary_nid = nid;
631 lpni = lnet_find_peer_ni_locked(nid);
633 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
634 lnet_peer_ni_decref_locked(lpni);
641 LNetPrimaryNID(lnet_nid_t nid)
643 struct lnet_peer_ni *lpni;
644 lnet_nid_t primary_nid = nid;
648 cpt = lnet_net_lock_current();
649 lpni = lnet_nid2peerni_locked(nid, cpt);
654 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
655 lnet_peer_ni_decref_locked(lpni);
657 lnet_net_unlock(cpt);
659 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
660 libcfs_nid2str(primary_nid), rc);
663 EXPORT_SYMBOL(LNetPrimaryNID);
665 struct lnet_peer_net *
666 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
668 struct lnet_peer_net *peer_net;
669 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
670 if (peer_net->lpn_net_id == net_id)
677 lnet_peer_setup_hierarchy(struct lnet_peer *lp, struct lnet_peer_ni *lpni,
680 struct lnet_peer_net *lpn = NULL;
681 struct lnet_peer_table *ptable;
682 __u32 net_id = LNET_NIDNET(nid);
685 * Create the peer_ni, peer_net, and peer if they don't exist
689 lpn = lnet_peer_get_net_locked(lp, net_id);
691 lp = lnet_peer_alloc(nid);
697 lpn = lnet_peer_net_alloc(net_id);
699 goto out_maybe_free_lp;
703 lpni = lnet_peer_ni_alloc(nid);
705 goto out_maybe_free_lpn;
708 /* Install the new peer_ni */
709 lnet_net_lock(LNET_LOCK_EX);
710 /* Add peer_ni to global peer table hash, if necessary. */
711 if (list_empty(&lpni->lpni_hashlist)) {
712 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
713 list_add_tail(&lpni->lpni_hashlist,
714 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
715 ptable->pt_version++;
716 atomic_inc(&ptable->pt_number);
717 atomic_inc(&lpni->lpni_refcount);
720 /* Detach the peer_ni from an existing peer, if necessary. */
721 if (lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer != lp)
722 lnet_try_destroy_peer_hierarchy_locked(lpni);
724 /* Add peer_ni to peer_net */
725 lpni->lpni_peer_net = lpn;
726 list_add_tail(&lpni->lpni_on_peer_net_list, &lpn->lpn_peer_nis);
728 /* Add peer_net to peer */
729 if (!lpn->lpn_peer) {
731 list_add_tail(&lpn->lpn_on_peer_list, &lp->lp_peer_nets);
734 /* Add peer to global peer list */
735 if (list_empty(&lp->lp_on_lnet_peer_list))
736 list_add_tail(&lp->lp_on_lnet_peer_list, &the_lnet.ln_peers);
737 lnet_net_unlock(LNET_LOCK_EX);
742 if (list_empty(&lpn->lpn_on_peer_list))
743 LIBCFS_FREE(lpn, sizeof(*lpn));
745 if (list_empty(&lp->lp_on_lnet_peer_list))
746 LIBCFS_FREE(lp, sizeof(*lp));
752 * Create a new peer, with nid as its primary nid.
754 * It is not an error if the peer already exists, provided that the
755 * given nid is the primary NID.
757 * Call with the lnet_api_mutex held.
760 lnet_peer_add(lnet_nid_t nid, bool mr)
762 struct lnet_peer *lp;
763 struct lnet_peer_ni *lpni;
766 LASSERT(nid != LNET_NID_ANY);
769 * No need for the lnet_net_lock here, because the
770 * lnet_api_mutex is held.
772 lpni = lnet_find_peer_ni_locked(nid);
774 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
777 lpni = lnet_find_peer_ni_locked(nid);
780 lp = lpni->lpni_peer_net->lpn_peer;
781 lnet_peer_ni_decref_locked(lpni);
783 /* A found peer must have this primary NID */
784 if (lp->lp_primary_nid != nid)
788 * If we found an lpni that is not a multi-rail, which could occur
789 * if lpni is already created as a non-mr lpni or we just created
790 * it, then make sure you indicate that this lpni is a primary mr
793 * TODO: update flags if necessary
795 if (mr && !lp->lp_multi_rail) {
796 lp->lp_multi_rail = true;
797 } else if (!mr && lp->lp_multi_rail) {
798 /* The mr state is sticky. */
799 CDEBUG(D_NET, "Cannot clear multi-flag from peer %s\n",
800 libcfs_nid2str(nid));
807 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, bool mr)
809 struct lnet_peer_ni *lpni;
812 LASSERT(nid != LNET_NID_ANY);
814 if (!mr && !lp->lp_multi_rail) {
815 CERROR("Cannot add nid %s to non-multi-rail peer %s\n",
817 libcfs_nid2str(lp->lp_primary_nid));
821 if (!lp->lp_multi_rail)
822 lp->lp_multi_rail = true;
824 lpni = lnet_find_peer_ni_locked(nid);
826 return lnet_peer_setup_hierarchy(lp, NULL, nid);
828 if (lpni->lpni_peer_net->lpn_peer != lp) {
829 struct lnet_peer *lp2 = lpni->lpni_peer_net->lpn_peer;
830 CERROR("Cannot add NID %s owned by peer %s to peer %s\n",
831 libcfs_nid2str(lpni->lpni_nid),
832 libcfs_nid2str(lp2->lp_primary_nid),
833 libcfs_nid2str(lp->lp_primary_nid));
837 CDEBUG(D_NET, "NID %s is already owned by peer %s\n",
838 libcfs_nid2str(lpni->lpni_nid),
839 libcfs_nid2str(lp->lp_primary_nid));
844 * lpni creation initiated due to traffic either sending or receiving.
847 lnet_peer_ni_traffic_add(lnet_nid_t nid)
849 struct lnet_peer_ni *lpni;
852 if (nid == LNET_NID_ANY)
855 /* lnet_net_lock is not needed here because ln_api_lock is held */
856 lpni = lnet_find_peer_ni_locked(nid);
859 * TODO: lnet_update_primary_nid() but not all of it
860 * only indicate if we're converting this to MR capable
861 * Can happen due to DD
863 lnet_peer_ni_decref_locked(lpni);
865 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
873 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
875 * This API handles the following combinations:
876 * Create a peer with its primary NI if only the prim_nid is provided
877 * Add a NID to a peer identified by the prim_nid. The peer identified
878 * by the prim_nid must already exist.
879 * The peer being created may be non-MR.
881 * The caller must hold ln_api_mutex. This prevents the peer from
882 * being created/modified/deleted by a different thread.
885 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
887 struct lnet_peer *lp = NULL;
888 struct lnet_peer_ni *lpni;
890 /* The prim_nid must always be specified */
891 if (prim_nid == LNET_NID_ANY)
895 * If nid isn't specified, we must create a new peer with
896 * prim_nid as its primary nid.
898 if (nid == LNET_NID_ANY)
899 return lnet_peer_add(prim_nid, mr);
901 /* Look up the prim_nid, which must exist. */
902 lpni = lnet_find_peer_ni_locked(prim_nid);
905 lnet_peer_ni_decref_locked(lpni);
906 lp = lpni->lpni_peer_net->lpn_peer;
908 if (lp->lp_primary_nid != prim_nid) {
909 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
910 libcfs_nid2str(prim_nid),
911 libcfs_nid2str(lp->lp_primary_nid));
915 return lnet_peer_add_nid(lp, nid, mr);
919 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
921 * This API handles the following combinations:
922 * Delete a NI from a peer if both prim_nid and nid are provided.
923 * Delete a peer if only prim_nid is provided.
924 * Delete a peer if its primary nid is provided.
926 * The caller must hold ln_api_mutex. This prevents the peer from
927 * being modified/deleted by a different thread.
930 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
932 struct lnet_peer *lp;
933 struct lnet_peer_ni *lpni;
935 if (prim_nid == LNET_NID_ANY)
938 lpni = lnet_find_peer_ni_locked(prim_nid);
941 lnet_peer_ni_decref_locked(lpni);
942 lp = lpni->lpni_peer_net->lpn_peer;
944 if (prim_nid != lp->lp_primary_nid) {
945 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
946 libcfs_nid2str(prim_nid),
947 libcfs_nid2str(lp->lp_primary_nid));
951 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
952 return lnet_peer_del(lp);
954 return lnet_peer_del_nid(lp, nid);
958 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
960 struct lnet_peer_table *ptable;
962 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
963 LASSERT(lpni->lpni_rtr_refcount == 0);
964 LASSERT(list_empty(&lpni->lpni_txq));
965 LASSERT(lpni->lpni_txqnob == 0);
967 lpni->lpni_net = NULL;
969 /* remove the peer ni from the zombie list */
970 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
971 spin_lock(&ptable->pt_zombie_lock);
972 list_del_init(&lpni->lpni_hashlist);
973 ptable->pt_zombies--;
974 spin_unlock(&ptable->pt_zombie_lock);
976 LIBCFS_FREE(lpni, sizeof(*lpni));
979 struct lnet_peer_ni *
980 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
982 struct lnet_peer_ni *lpni = NULL;
985 if (the_lnet.ln_state != LNET_STATE_RUNNING)
986 return ERR_PTR(-ESHUTDOWN);
989 * find if a peer_ni already exists.
990 * If so then just return that.
992 lpni = lnet_find_peer_ni_locked(nid);
996 lnet_net_unlock(cpt);
998 rc = lnet_peer_ni_traffic_add(nid);
1001 goto out_net_relock;
1004 lpni = lnet_find_peer_ni_locked(nid);
1013 struct lnet_peer_ni *
1014 lnet_nid2peerni_locked(lnet_nid_t nid, int cpt)
1016 struct lnet_peer_ni *lpni = NULL;
1019 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1020 return ERR_PTR(-ESHUTDOWN);
1023 * find if a peer_ni already exists.
1024 * If so then just return that.
1026 lpni = lnet_find_peer_ni_locked(nid);
1032 * use the lnet_api_mutex to serialize the creation of the peer_ni
1033 * and the creation/deletion of the local ni/net. When a local ni is
1034 * created, if there exists a set of peer_nis on that network,
1035 * they need to be traversed and updated. When a local NI is
1036 * deleted, which could result in a network being deleted, then
1037 * all peer nis on that network need to be removed as well.
1039 * Creation through traffic should also be serialized with
1040 * creation through DLC.
1042 lnet_net_unlock(cpt);
1043 mutex_lock(&the_lnet.ln_api_mutex);
1045 * Shutdown is only set under the ln_api_lock, so a single
1046 * check here is sufficent.
1048 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1049 lpni = ERR_PTR(-ESHUTDOWN);
1050 goto out_mutex_unlock;
1053 rc = lnet_peer_ni_traffic_add(nid);
1056 goto out_mutex_unlock;
1059 lpni = lnet_find_peer_ni_locked(nid);
1063 mutex_unlock(&the_lnet.ln_api_mutex);
1070 lnet_debug_peer(lnet_nid_t nid)
1072 char *aliveness = "NA";
1073 struct lnet_peer_ni *lp;
1076 cpt = lnet_cpt_of_nid(nid, NULL);
1079 lp = lnet_nid2peerni_locked(nid, cpt);
1081 lnet_net_unlock(cpt);
1082 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
1086 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
1087 aliveness = lp->lpni_alive ? "up" : "down";
1089 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
1090 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
1091 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
1092 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
1093 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
1095 lnet_peer_ni_decref_locked(lp);
1097 lnet_net_unlock(cpt);
1100 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
1101 char aliveness[LNET_MAX_STR_LEN],
1102 __u32 *cpt_iter, __u32 *refcount,
1103 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
1104 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
1105 __u32 *peer_tx_qnob)
1107 struct lnet_peer_table *peer_table;
1108 struct lnet_peer_ni *lp;
1113 /* get the number of CPTs */
1114 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
1116 /* if the cpt number to be examined is >= the number of cpts in
1117 * the system then indicate that there are no more cpts to examin
1119 if (*cpt_iter >= lncpt)
1122 /* get the current table */
1123 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
1124 /* if the ptable is NULL then there are no more cpts to examine */
1125 if (peer_table == NULL)
1128 lnet_net_lock(*cpt_iter);
1130 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
1131 struct list_head *peers = &peer_table->pt_hash[j];
1133 list_for_each_entry(lp, peers, lpni_hashlist) {
1134 if (peer_index-- > 0)
1137 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
1138 if (lnet_isrouter(lp) ||
1139 lnet_peer_aliveness_enabled(lp))
1140 snprintf(aliveness, LNET_MAX_STR_LEN,
1141 lp->lpni_alive ? "up" : "down");
1143 *nid = lp->lpni_nid;
1144 *refcount = atomic_read(&lp->lpni_refcount);
1145 *ni_peer_tx_credits =
1146 lp->lpni_net->net_tunables.lct_peer_tx_credits;
1147 *peer_tx_credits = lp->lpni_txcredits;
1148 *peer_rtr_credits = lp->lpni_rtrcredits;
1149 *peer_min_rtr_credits = lp->lpni_mintxcredits;
1150 *peer_tx_qnob = lp->lpni_txqnob;
1156 lnet_net_unlock(*cpt_iter);
1160 return found ? 0 : -ENOENT;
1163 int lnet_get_peer_info(__u32 idx, lnet_nid_t *primary_nid, lnet_nid_t *nid,
1165 struct lnet_peer_ni_credit_info __user *peer_ni_info,
1166 struct lnet_ioctl_element_stats __user *peer_ni_stats)
1168 struct lnet_peer_ni *lpni = NULL;
1169 struct lnet_peer_net *lpn = NULL;
1170 struct lnet_peer *lp = NULL;
1171 struct lnet_peer_ni_credit_info ni_info;
1172 struct lnet_ioctl_element_stats ni_stats;
1175 lpni = lnet_get_peer_ni_idx_locked(idx, &lpn, &lp);
1180 *primary_nid = lp->lp_primary_nid;
1181 *mr = lp->lp_multi_rail;
1182 *nid = lpni->lpni_nid;
1183 snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN, "NA");
1184 if (lnet_isrouter(lpni) ||
1185 lnet_peer_aliveness_enabled(lpni))
1186 snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN,
1187 lpni->lpni_alive ? "up" : "down");
1189 ni_info.cr_refcount = atomic_read(&lpni->lpni_refcount);
1190 ni_info.cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
1191 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
1192 ni_info.cr_peer_tx_credits = lpni->lpni_txcredits;
1193 ni_info.cr_peer_rtr_credits = lpni->lpni_rtrcredits;
1194 ni_info.cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
1195 ni_info.cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
1196 ni_info.cr_peer_tx_qnob = lpni->lpni_txqnob;
1197 ni_info.cr_ncpt = lpni->lpni_cpt;
1199 ni_stats.iel_send_count = atomic_read(&lpni->lpni_stats.send_count);
1200 ni_stats.iel_recv_count = atomic_read(&lpni->lpni_stats.recv_count);
1201 ni_stats.iel_drop_count = atomic_read(&lpni->lpni_stats.drop_count);
1203 /* If copy_to_user fails */
1205 if (copy_to_user(peer_ni_info, &ni_info, sizeof(ni_info)))
1208 if (copy_to_user(peer_ni_stats, &ni_stats, sizeof(ni_stats)))