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) {
59 lpni->lpni_txcredits =
60 lpni->lpni_mintxcredits =
61 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
62 lpni->lpni_rtrcredits =
63 lpni->lpni_minrtrcredits =
64 lnet_peer_buffer_credits(lpni->lpni_net);
66 lnet_peer_remove_from_remote_list(lpni);
72 lnet_peer_tables_destroy(void)
74 struct lnet_peer_table *ptable;
75 struct list_head *hash;
79 if (!the_lnet.ln_peer_tables)
82 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
83 hash = ptable->pt_hash;
84 if (!hash) /* not intialized */
87 LASSERT(list_empty(&ptable->pt_zombie_list));
89 ptable->pt_hash = NULL;
90 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
91 LASSERT(list_empty(&hash[j]));
93 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
96 cfs_percpt_free(the_lnet.ln_peer_tables);
97 the_lnet.ln_peer_tables = NULL;
101 lnet_peer_tables_create(void)
103 struct lnet_peer_table *ptable;
104 struct list_head *hash;
108 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
110 if (the_lnet.ln_peer_tables == NULL) {
111 CERROR("Failed to allocate cpu-partition peer tables\n");
115 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
116 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
117 LNET_PEER_HASH_SIZE * sizeof(*hash));
119 CERROR("Failed to create peer hash table\n");
120 lnet_peer_tables_destroy();
124 spin_lock_init(&ptable->pt_zombie_lock);
125 INIT_LIST_HEAD(&ptable->pt_zombie_list);
127 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
128 INIT_LIST_HEAD(&hash[j]);
129 ptable->pt_hash = hash; /* sign of initialization */
135 static struct lnet_peer_ni *
136 lnet_peer_ni_alloc(lnet_nid_t nid)
138 struct lnet_peer_ni *lpni;
139 struct lnet_net *net;
142 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
144 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
148 INIT_LIST_HEAD(&lpni->lpni_txq);
149 INIT_LIST_HEAD(&lpni->lpni_rtrq);
150 INIT_LIST_HEAD(&lpni->lpni_routes);
151 INIT_LIST_HEAD(&lpni->lpni_hashlist);
152 INIT_LIST_HEAD(&lpni->lpni_on_peer_net_list);
153 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
155 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
156 lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
157 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
158 lpni->lpni_nid = nid;
159 lpni->lpni_cpt = cpt;
160 lnet_set_peer_ni_health_locked(lpni, true);
162 net = lnet_get_net_locked(LNET_NIDNET(nid));
163 lpni->lpni_net = net;
165 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
166 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
167 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
168 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
171 * This peer_ni is not on a local network, so we
172 * cannot add the credits here. In case the net is
173 * added later, add the peer_ni to the remote peer ni
174 * list so it can be easily found and revisited.
176 /* FIXME: per-net implementation instead? */
177 atomic_inc(&lpni->lpni_refcount);
178 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
179 &the_lnet.ln_remote_peer_ni_list);
182 /* TODO: update flags */
187 static struct lnet_peer_net *
188 lnet_peer_net_alloc(__u32 net_id)
190 struct lnet_peer_net *lpn;
192 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
196 INIT_LIST_HEAD(&lpn->lpn_on_peer_list);
197 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
198 lpn->lpn_net_id = net_id;
203 static struct lnet_peer *
204 lnet_peer_alloc(lnet_nid_t nid)
206 struct lnet_peer *lp;
208 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
212 INIT_LIST_HEAD(&lp->lp_on_lnet_peer_list);
213 INIT_LIST_HEAD(&lp->lp_peer_nets);
214 lp->lp_primary_nid = nid;
216 /* TODO: update flags */
223 lnet_try_destroy_peer_hierarchy_locked(struct lnet_peer_ni *lpni)
225 struct lnet_peer_net *peer_net;
226 struct lnet_peer *peer;
228 /* TODO: could the below situation happen? accessing an already
230 if (lpni->lpni_peer_net == NULL ||
231 lpni->lpni_peer_net->lpn_peer == NULL)
234 peer_net = lpni->lpni_peer_net;
235 peer = lpni->lpni_peer_net->lpn_peer;
237 list_del_init(&lpni->lpni_on_peer_net_list);
238 lpni->lpni_peer_net = NULL;
240 /* if peer_net is empty, then remove it from the peer */
241 if (list_empty(&peer_net->lpn_peer_nis)) {
242 list_del_init(&peer_net->lpn_on_peer_list);
243 peer_net->lpn_peer = NULL;
244 LIBCFS_FREE(peer_net, sizeof(*peer_net));
246 /* if the peer is empty then remove it from the
247 * the_lnet.ln_peers */
248 if (list_empty(&peer->lp_peer_nets)) {
249 list_del_init(&peer->lp_on_lnet_peer_list);
250 LIBCFS_FREE(peer, sizeof(*peer));
255 /* called with lnet_net_lock LNET_LOCK_EX held */
257 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
259 struct lnet_peer_table *ptable = NULL;
261 lnet_peer_remove_from_remote_list(lpni);
263 /* remove peer ni from the hash list. */
264 list_del_init(&lpni->lpni_hashlist);
266 /* decrement the ref count on the peer table */
267 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
268 LASSERT(atomic_read(&ptable->pt_number) > 0);
269 atomic_dec(&ptable->pt_number);
272 * The peer_ni can no longer be found with a lookup. But there
273 * can be current users, so keep track of it on the zombie
274 * list until the reference count has gone to zero.
276 * The last reference may be lost in a place where the
277 * lnet_net_lock locks only a single cpt, and that cpt may not
278 * be lpni->lpni_cpt. So the zombie list of this peer_table
281 spin_lock(&ptable->pt_zombie_lock);
282 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
283 ptable->pt_zombies++;
284 spin_unlock(&ptable->pt_zombie_lock);
286 /* no need to keep this peer on the hierarchy anymore */
287 lnet_try_destroy_peer_hierarchy_locked(lpni);
289 /* decrement reference on peer */
290 lnet_peer_ni_decref_locked(lpni);
293 void lnet_peer_uninit()
295 struct lnet_peer_ni *lpni, *tmp;
297 lnet_net_lock(LNET_LOCK_EX);
299 /* remove all peer_nis from the remote peer and the hash list */
300 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
301 lpni_on_remote_peer_ni_list)
302 lnet_peer_ni_del_locked(lpni);
304 lnet_peer_tables_destroy();
306 lnet_net_unlock(LNET_LOCK_EX);
310 lnet_peer_del_locked(struct lnet_peer *peer)
312 struct lnet_peer_ni *lpni = NULL, *lpni2;
314 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
315 while (lpni != NULL) {
316 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
317 lnet_peer_ni_del_locked(lpni);
323 lnet_peer_table_cleanup_locked(struct lnet_net *net,
324 struct lnet_peer_table *ptable)
327 struct lnet_peer_ni *lpni;
328 struct lnet_peer_ni *tmp;
329 struct lnet_peer *peer;
331 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
332 list_for_each_entry_safe(lpni, tmp, &ptable->pt_hash[i],
334 if (net != NULL && net != lpni->lpni_net)
338 * check if by removing this peer ni we should be
339 * removing the entire peer.
341 peer = lpni->lpni_peer_net->lpn_peer;
343 if (peer->lp_primary_nid == lpni->lpni_nid)
344 lnet_peer_del_locked(peer);
346 lnet_peer_ni_del_locked(lpni);
352 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
356 spin_lock(&ptable->pt_zombie_lock);
357 while (ptable->pt_zombies) {
358 spin_unlock(&ptable->pt_zombie_lock);
362 "Waiting for %d zombies on peer table\n",
365 set_current_state(TASK_UNINTERRUPTIBLE);
366 schedule_timeout(cfs_time_seconds(1) >> 1);
367 spin_lock(&ptable->pt_zombie_lock);
369 spin_unlock(&ptable->pt_zombie_lock);
373 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
374 struct lnet_peer_table *ptable)
376 struct lnet_peer_ni *lp;
377 struct lnet_peer_ni *tmp;
381 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
382 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
384 if (net != lp->lpni_net)
387 if (lp->lpni_rtr_refcount == 0)
390 lpni_nid = lp->lpni_nid;
392 lnet_net_unlock(LNET_LOCK_EX);
393 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
394 lnet_net_lock(LNET_LOCK_EX);
400 lnet_peer_tables_cleanup(struct lnet_net *net)
403 struct lnet_peer_table *ptable;
405 LASSERT(the_lnet.ln_shutdown || net != NULL);
406 /* If just deleting the peers for a NI, get rid of any routes these
407 * peers are gateways for. */
408 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
409 lnet_net_lock(LNET_LOCK_EX);
410 lnet_peer_table_del_rtrs_locked(net, ptable);
411 lnet_net_unlock(LNET_LOCK_EX);
414 /* Start the cleanup process */
415 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
416 lnet_net_lock(LNET_LOCK_EX);
417 lnet_peer_table_cleanup_locked(net, ptable);
418 lnet_net_unlock(LNET_LOCK_EX);
421 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
422 lnet_peer_ni_finalize_wait(ptable);
425 static struct lnet_peer_ni *
426 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
428 struct list_head *peers;
429 struct lnet_peer_ni *lp;
431 LASSERT(!the_lnet.ln_shutdown);
433 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
434 list_for_each_entry(lp, peers, lpni_hashlist) {
435 if (lp->lpni_nid == nid) {
436 lnet_peer_ni_addref_locked(lp);
444 struct lnet_peer_ni *
445 lnet_find_peer_ni_locked(lnet_nid_t nid)
447 struct lnet_peer_ni *lpni;
448 struct lnet_peer_table *ptable;
451 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
453 ptable = the_lnet.ln_peer_tables[cpt];
454 lpni = lnet_get_peer_ni_locked(ptable, nid);
460 lnet_find_or_create_peer_locked(lnet_nid_t dst_nid, int cpt)
462 struct lnet_peer_ni *lpni;
463 struct lnet_peer *lp;
465 lpni = lnet_find_peer_ni_locked(dst_nid);
467 lpni = lnet_nid2peerni_locked(dst_nid, cpt);
469 return ERR_CAST(lpni);
472 lp = lpni->lpni_peer_net->lpn_peer;
473 lnet_peer_ni_decref_locked(lpni);
478 struct lnet_peer_ni *
479 lnet_get_peer_ni_idx_locked(int idx, struct lnet_peer_net **lpn,
480 struct lnet_peer **lp)
482 struct lnet_peer_ni *lpni;
484 list_for_each_entry((*lp), &the_lnet.ln_peers, lp_on_lnet_peer_list) {
485 list_for_each_entry((*lpn), &((*lp)->lp_peer_nets), lpn_on_peer_list) {
486 list_for_each_entry(lpni, &((*lpn)->lpn_peer_nis),
487 lpni_on_peer_net_list)
496 struct lnet_peer_ni *
497 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
498 struct lnet_peer_net *peer_net,
499 struct lnet_peer_ni *prev)
501 struct lnet_peer_ni *lpni;
502 struct lnet_peer_net *net = peer_net;
506 net = list_entry(peer->lp_peer_nets.next,
507 struct lnet_peer_net,
509 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
510 lpni_on_peer_net_list);
515 if (prev->lpni_on_peer_net_list.next ==
516 &prev->lpni_peer_net->lpn_peer_nis) {
518 * if you reached the end of the peer ni list and the peer
519 * net is specified then there are no more peer nis in that
526 * we reached the end of this net ni list. move to the
529 if (prev->lpni_peer_net->lpn_on_peer_list.next ==
531 /* no more nets and no more NIs. */
534 /* get the next net */
535 net = list_entry(prev->lpni_peer_net->lpn_on_peer_list.next,
536 struct lnet_peer_net,
538 /* get the ni on it */
539 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
540 lpni_on_peer_net_list);
545 /* there are more nis left */
546 lpni = list_entry(prev->lpni_on_peer_net_list.next,
547 struct lnet_peer_ni, lpni_on_peer_net_list);
553 lnet_peer_is_ni_pref_locked(struct lnet_peer_ni *lpni, struct lnet_ni *ni)
557 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
558 if (lpni->lpni_pref_nids[i] == ni->ni_nid)
565 lnet_peer_primary_nid(lnet_nid_t nid)
567 struct lnet_peer_ni *lpni;
568 lnet_nid_t primary_nid = nid;
571 cpt = lnet_net_lock_current();
572 lpni = lnet_find_peer_ni_locked(nid);
574 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
575 lnet_peer_ni_decref_locked(lpni);
577 lnet_net_unlock(cpt);
582 struct lnet_peer_net *
583 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
585 struct lnet_peer_net *peer_net;
586 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
587 if (peer_net->lpn_net_id == net_id)
594 lnet_peer_setup_hierarchy(struct lnet_peer *lp, struct lnet_peer_ni *lpni,
597 struct lnet_peer_net *lpn = NULL;
598 struct lnet_peer_table *ptable;
599 __u32 net_id = LNET_NIDNET(nid);
602 * Create the peer_ni, peer_net, and peer if they don't exist
606 lpn = lnet_peer_get_net_locked(lp, net_id);
608 lp = lnet_peer_alloc(nid);
614 lpn = lnet_peer_net_alloc(net_id);
616 goto out_maybe_free_lp;
620 lpni = lnet_peer_ni_alloc(nid);
622 goto out_maybe_free_lpn;
625 /* Install the new peer_ni */
626 lnet_net_lock(LNET_LOCK_EX);
627 /* Add peer_ni to global peer table hash, if necessary. */
628 if (list_empty(&lpni->lpni_hashlist)) {
629 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
630 list_add_tail(&lpni->lpni_hashlist,
631 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
632 ptable->pt_version++;
633 atomic_inc(&ptable->pt_number);
634 atomic_inc(&lpni->lpni_refcount);
637 /* Detach the peer_ni from an existing peer, if necessary. */
638 if (lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer != lp)
639 lnet_try_destroy_peer_hierarchy_locked(lpni);
641 /* Add peer_ni to peer_net */
642 lpni->lpni_peer_net = lpn;
643 list_add_tail(&lpni->lpni_on_peer_net_list, &lpn->lpn_peer_nis);
645 /* Add peer_net to peer */
646 if (!lpn->lpn_peer) {
648 list_add_tail(&lpn->lpn_on_peer_list, &lp->lp_peer_nets);
651 /* Add peer to global peer list */
652 if (list_empty(&lp->lp_on_lnet_peer_list))
653 list_add_tail(&lp->lp_on_lnet_peer_list, &the_lnet.ln_peers);
654 lnet_net_unlock(LNET_LOCK_EX);
659 if (list_empty(&lpn->lpn_on_peer_list))
660 LIBCFS_FREE(lpn, sizeof(*lpn));
662 if (list_empty(&lp->lp_on_lnet_peer_list))
663 LIBCFS_FREE(lp, sizeof(*lp));
669 lnet_add_prim_lpni(lnet_nid_t nid)
672 struct lnet_peer *peer;
673 struct lnet_peer_ni *lpni;
675 LASSERT(nid != LNET_NID_ANY);
678 * lookup the NID and its peer
679 * if the peer doesn't exist, create it.
680 * if this is a non-MR peer then change its state to MR and exit.
681 * if this is an MR peer and it's a primary NI: NO-OP.
682 * if this is an MR peer and it's not a primary NI. Operation not
685 * The adding and deleting of peer nis is being serialized through
686 * the api_mutex. So we can look up peers with the mutex locked
687 * safely. Only when we need to change the ptable, do we need to
688 * exclusively lock the lnet_net_lock()
690 lpni = lnet_find_peer_ni_locked(nid);
692 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
695 lpni = lnet_find_peer_ni_locked(nid);
700 lnet_peer_ni_decref_locked(lpni);
702 peer = lpni->lpni_peer_net->lpn_peer;
705 * If we found a lpni with the same nid as the NID we're trying to
706 * create, then we're trying to create an already existing lpni
707 * that belongs to a different peer
709 if (peer->lp_primary_nid != nid)
713 * if we found an lpni that is not a multi-rail, which could occur
714 * if lpni is already created as a non-mr lpni or we just created
715 * it, then make sure you indicate that this lpni is a primary mr
718 * TODO: update flags if necessary
720 if (!peer->lp_multi_rail && peer->lp_primary_nid == nid)
721 peer->lp_multi_rail = true;
727 lnet_add_peer_ni_to_prim_lpni(lnet_nid_t key_nid, lnet_nid_t nid)
729 struct lnet_peer *peer, *primary_peer;
730 struct lnet_peer_ni *lpni = NULL, *klpni = NULL;
732 LASSERT(key_nid != LNET_NID_ANY && nid != LNET_NID_ANY);
735 * key nid must be created by this point. If not then this
736 * operation is not permitted
738 klpni = lnet_find_peer_ni_locked(key_nid);
742 lnet_peer_ni_decref_locked(klpni);
744 primary_peer = klpni->lpni_peer_net->lpn_peer;
746 lpni = lnet_find_peer_ni_locked(nid);
748 lnet_peer_ni_decref_locked(lpni);
750 peer = lpni->lpni_peer_net->lpn_peer;
752 * lpni already exists in the system but it belongs to
753 * a different peer. We can't re-added it
755 if (peer->lp_primary_nid != key_nid && peer->lp_multi_rail) {
756 CERROR("Cannot add NID %s owned by peer %s to peer %s\n",
757 libcfs_nid2str(lpni->lpni_nid),
758 libcfs_nid2str(peer->lp_primary_nid),
759 libcfs_nid2str(key_nid));
761 } else if (peer->lp_primary_nid == key_nid) {
763 * found a peer_ni that is already part of the
764 * peer. This is a no-op operation.
770 * TODO: else if (peer->lp_primary_nid != key_nid &&
771 * !peer->lp_multi_rail)
772 * peer is not an MR peer and it will be moved in the next
773 * step to klpni, so update its flags accordingly.
774 * lnet_move_peer_ni()
778 * TODO: call lnet_update_peer() from here to update the
779 * flags. This is the case when the lpni you're trying to
780 * add is already part of the peer. This could've been
781 * added by the DD previously, so go ahead and do any
782 * updates to the state if necessary
788 * When we get here we either have found an existing lpni, which
789 * we can switch to the new peer. Or we need to create one and
790 * add it to the new peer
792 return lnet_peer_setup_hierarchy(primary_peer, lpni, nid);
796 * lpni creation initiated due to traffic either sending or receiving.
799 lnet_peer_ni_traffic_add(lnet_nid_t nid)
801 struct lnet_peer_ni *lpni;
804 if (nid == LNET_NID_ANY)
807 /* lnet_net_lock is not needed here because ln_api_lock is held */
808 lpni = lnet_find_peer_ni_locked(nid);
811 * TODO: lnet_update_primary_nid() but not all of it
812 * only indicate if we're converting this to MR capable
813 * Can happen due to DD
815 lnet_peer_ni_decref_locked(lpni);
817 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
825 lnet_peer_ni_add_non_mr(lnet_nid_t nid)
827 struct lnet_peer_ni *lpni;
829 lpni = lnet_find_peer_ni_locked(nid);
831 CERROR("Cannot add %s as non-mr when it already exists\n",
832 libcfs_nid2str(nid));
833 lnet_peer_ni_decref_locked(lpni);
837 return lnet_peer_setup_hierarchy(NULL, NULL, nid);
841 * This API handles the following combinations:
842 * Create a primary NI if only the key_nid is provided
843 * Create or add an lpni to a primary NI. Primary NI must've already
845 * Create a non-MR peer.
848 lnet_add_peer_ni_to_peer(lnet_nid_t key_nid, lnet_nid_t nid, bool mr)
851 * Caller trying to setup an MR like peer hierarchy but
852 * specifying it to be non-MR. This is not allowed.
854 if (key_nid != LNET_NID_ANY &&
855 nid != LNET_NID_ANY && !mr)
858 /* Add the primary NID of a peer */
859 if (key_nid != LNET_NID_ANY &&
860 nid == LNET_NID_ANY && mr)
861 return lnet_add_prim_lpni(key_nid);
863 /* Add a NID to an existing peer */
864 if (key_nid != LNET_NID_ANY &&
865 nid != LNET_NID_ANY && mr)
866 return lnet_add_peer_ni_to_prim_lpni(key_nid, nid);
868 /* Add a non-MR peer NI */
869 if (((key_nid != LNET_NID_ANY &&
870 nid == LNET_NID_ANY) ||
871 (key_nid == LNET_NID_ANY &&
872 nid != LNET_NID_ANY)) && !mr)
873 return lnet_peer_ni_add_non_mr(key_nid != LNET_NID_ANY ?
880 lnet_del_peer_ni_from_peer(lnet_nid_t key_nid, lnet_nid_t nid)
882 lnet_nid_t local_nid;
883 struct lnet_peer *peer;
884 struct lnet_peer_ni *lpni;
886 if (key_nid == LNET_NID_ANY)
889 local_nid = (nid != LNET_NID_ANY) ? nid : key_nid;
891 lpni = lnet_find_peer_ni_locked(local_nid);
894 lnet_peer_ni_decref_locked(lpni);
896 peer = lpni->lpni_peer_net->lpn_peer;
897 LASSERT(peer != NULL);
899 if (peer->lp_primary_nid == lpni->lpni_nid) {
901 * deleting the primary ni is equivalent to deleting the
904 lnet_net_lock(LNET_LOCK_EX);
905 lnet_peer_del_locked(peer);
906 lnet_net_unlock(LNET_LOCK_EX);
911 lnet_net_lock(LNET_LOCK_EX);
912 lnet_peer_ni_del_locked(lpni);
913 lnet_net_unlock(LNET_LOCK_EX);
919 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
921 struct lnet_peer_table *ptable;
923 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
924 LASSERT(lpni->lpni_rtr_refcount == 0);
925 LASSERT(list_empty(&lpni->lpni_txq));
926 LASSERT(lpni->lpni_txqnob == 0);
928 lpni->lpni_net = NULL;
930 /* remove the peer ni from the zombie list */
931 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
932 spin_lock(&ptable->pt_zombie_lock);
933 list_del_init(&lpni->lpni_hashlist);
934 ptable->pt_zombies--;
935 spin_unlock(&ptable->pt_zombie_lock);
937 LIBCFS_FREE(lpni, sizeof(*lpni));
940 struct lnet_peer_ni *
941 lnet_nid2peerni_locked(lnet_nid_t nid, int cpt)
943 struct lnet_peer_table *ptable;
944 struct lnet_peer_ni *lpni = NULL;
948 if (the_lnet.ln_shutdown) /* it's shutting down */
949 return ERR_PTR(-ESHUTDOWN);
952 * calculate cpt2 with the standard hash function
953 * This cpt2 is the slot where we'll find or create the peer.
955 cpt2 = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
956 ptable = the_lnet.ln_peer_tables[cpt2];
957 lpni = lnet_get_peer_ni_locked(ptable, nid);
961 /* Slow path: serialized using the ln_api_mutex. */
962 lnet_net_unlock(cpt);
963 mutex_lock(&the_lnet.ln_api_mutex);
965 * Shutdown is only set under the ln_api_lock, so a single
966 * check here is sufficent.
968 * lnet_add_nid_to_peer() also handles the case where we've
969 * raced and a different thread added the NID.
971 if (the_lnet.ln_shutdown) {
972 lpni = ERR_PTR(-ESHUTDOWN);
973 goto out_mutex_unlock;
976 rc = lnet_peer_ni_traffic_add(nid);
979 goto out_mutex_unlock;
982 lpni = lnet_get_peer_ni_locked(ptable, nid);
986 mutex_unlock(&the_lnet.ln_api_mutex);
993 lnet_debug_peer(lnet_nid_t nid)
995 char *aliveness = "NA";
996 struct lnet_peer_ni *lp;
999 cpt = lnet_cpt_of_nid(nid, NULL);
1002 lp = lnet_nid2peerni_locked(nid, cpt);
1004 lnet_net_unlock(cpt);
1005 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
1009 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
1010 aliveness = lp->lpni_alive ? "up" : "down";
1012 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
1013 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
1014 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
1015 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
1016 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
1018 lnet_peer_ni_decref_locked(lp);
1020 lnet_net_unlock(cpt);
1023 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
1024 char aliveness[LNET_MAX_STR_LEN],
1025 __u32 *cpt_iter, __u32 *refcount,
1026 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
1027 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
1028 __u32 *peer_tx_qnob)
1030 struct lnet_peer_table *peer_table;
1031 struct lnet_peer_ni *lp;
1036 /* get the number of CPTs */
1037 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
1039 /* if the cpt number to be examined is >= the number of cpts in
1040 * the system then indicate that there are no more cpts to examin
1042 if (*cpt_iter >= lncpt)
1045 /* get the current table */
1046 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
1047 /* if the ptable is NULL then there are no more cpts to examine */
1048 if (peer_table == NULL)
1051 lnet_net_lock(*cpt_iter);
1053 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
1054 struct list_head *peers = &peer_table->pt_hash[j];
1056 list_for_each_entry(lp, peers, lpni_hashlist) {
1057 if (peer_index-- > 0)
1060 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
1061 if (lnet_isrouter(lp) ||
1062 lnet_peer_aliveness_enabled(lp))
1063 snprintf(aliveness, LNET_MAX_STR_LEN,
1064 lp->lpni_alive ? "up" : "down");
1066 *nid = lp->lpni_nid;
1067 *refcount = atomic_read(&lp->lpni_refcount);
1068 *ni_peer_tx_credits =
1069 lp->lpni_net->net_tunables.lct_peer_tx_credits;
1070 *peer_tx_credits = lp->lpni_txcredits;
1071 *peer_rtr_credits = lp->lpni_rtrcredits;
1072 *peer_min_rtr_credits = lp->lpni_mintxcredits;
1073 *peer_tx_qnob = lp->lpni_txqnob;
1079 lnet_net_unlock(*cpt_iter);
1083 return found ? 0 : -ENOENT;
1086 int lnet_get_peer_info(__u32 idx, lnet_nid_t *primary_nid, lnet_nid_t *nid,
1087 bool *mr, struct lnet_peer_ni_credit_info *peer_ni_info,
1088 struct lnet_ioctl_element_stats *peer_ni_stats)
1090 struct lnet_peer_ni *lpni = NULL;
1091 struct lnet_peer_net *lpn = NULL;
1092 struct lnet_peer *lp = NULL;
1094 lpni = lnet_get_peer_ni_idx_locked(idx, &lpn, &lp);
1099 *primary_nid = lp->lp_primary_nid;
1100 *mr = lp->lp_multi_rail;
1101 *nid = lpni->lpni_nid;
1102 snprintf(peer_ni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
1103 if (lnet_isrouter(lpni) ||
1104 lnet_peer_aliveness_enabled(lpni))
1105 snprintf(peer_ni_info->cr_aliveness, LNET_MAX_STR_LEN,
1106 lpni->lpni_alive ? "up" : "down");
1108 peer_ni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
1109 peer_ni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
1110 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
1111 peer_ni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
1112 peer_ni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
1113 peer_ni_info->cr_peer_min_rtr_credits = lpni->lpni_mintxcredits;
1114 peer_ni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
1116 peer_ni_stats->send_count = atomic_read(&lpni->lpni_stats.send_count);
1117 peer_ni_stats->recv_count = atomic_read(&lpni->lpni_stats.recv_count);
1118 peer_ni_stats->drop_count = atomic_read(&lpni->lpni_stats.drop_count);