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 spin_lock_init(&lp->lp_lock);
220 lp->lp_primary_nid = nid;
222 /* TODO: update flags */
229 lnet_try_destroy_peer_hierarchy_locked(struct lnet_peer_ni *lpni)
231 struct lnet_peer_net *peer_net;
232 struct lnet_peer *peer;
234 /* TODO: could the below situation happen? accessing an already
236 if (lpni->lpni_peer_net == NULL ||
237 lpni->lpni_peer_net->lpn_peer == NULL)
240 peer_net = lpni->lpni_peer_net;
241 peer = lpni->lpni_peer_net->lpn_peer;
243 list_del_init(&lpni->lpni_on_peer_net_list);
244 lpni->lpni_peer_net = NULL;
246 /* if peer_net is empty, then remove it from the peer */
247 if (list_empty(&peer_net->lpn_peer_nis)) {
248 list_del_init(&peer_net->lpn_on_peer_list);
249 peer_net->lpn_peer = NULL;
250 LIBCFS_FREE(peer_net, sizeof(*peer_net));
252 /* if the peer is empty then remove it from the
253 * the_lnet.ln_peers */
254 if (list_empty(&peer->lp_peer_nets)) {
255 list_del_init(&peer->lp_on_lnet_peer_list);
256 LIBCFS_FREE(peer, sizeof(*peer));
261 /* called with lnet_net_lock LNET_LOCK_EX held */
263 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
265 struct lnet_peer_table *ptable = NULL;
267 /* don't remove a peer_ni if it's also a gateway */
268 if (lpni->lpni_rtr_refcount > 0) {
269 CERROR("Peer NI %s is a gateway. Can not delete it\n",
270 libcfs_nid2str(lpni->lpni_nid));
274 lnet_peer_remove_from_remote_list(lpni);
276 /* remove peer ni from the hash list. */
277 list_del_init(&lpni->lpni_hashlist);
279 /* decrement the ref count on the peer table */
280 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
281 LASSERT(atomic_read(&ptable->pt_number) > 0);
282 atomic_dec(&ptable->pt_number);
285 * The peer_ni can no longer be found with a lookup. But there
286 * can be current users, so keep track of it on the zombie
287 * list until the reference count has gone to zero.
289 * The last reference may be lost in a place where the
290 * lnet_net_lock locks only a single cpt, and that cpt may not
291 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
294 spin_lock(&ptable->pt_zombie_lock);
295 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
296 ptable->pt_zombies++;
297 spin_unlock(&ptable->pt_zombie_lock);
299 /* no need to keep this peer on the hierarchy anymore */
300 lnet_try_destroy_peer_hierarchy_locked(lpni);
302 /* decrement reference on peer */
303 lnet_peer_ni_decref_locked(lpni);
308 void lnet_peer_uninit(void)
310 struct lnet_peer_ni *lpni, *tmp;
312 lnet_net_lock(LNET_LOCK_EX);
314 /* remove all peer_nis from the remote peer and the hash list */
315 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
316 lpni_on_remote_peer_ni_list)
317 lnet_peer_ni_del_locked(lpni);
319 lnet_peer_tables_destroy();
321 lnet_net_unlock(LNET_LOCK_EX);
325 lnet_peer_del_locked(struct lnet_peer *peer)
327 struct lnet_peer_ni *lpni = NULL, *lpni2;
330 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
331 while (lpni != NULL) {
332 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
333 rc = lnet_peer_ni_del_locked(lpni);
343 lnet_peer_del(struct lnet_peer *peer)
345 lnet_net_lock(LNET_LOCK_EX);
346 lnet_peer_del_locked(peer);
347 lnet_net_unlock(LNET_LOCK_EX);
353 * Delete a NID from a peer.
354 * Implements a few sanity checks.
355 * Call with ln_api_mutex held.
358 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid)
360 struct lnet_peer *lp2;
361 struct lnet_peer_ni *lpni;
363 lpni = lnet_find_peer_ni_locked(nid);
365 CERROR("Cannot remove unknown nid %s from peer %s\n",
367 libcfs_nid2str(lp->lp_primary_nid));
370 lnet_peer_ni_decref_locked(lpni);
371 lp2 = lpni->lpni_peer_net->lpn_peer;
373 CERROR("Nid %s is attached to peer %s, not peer %s\n",
375 libcfs_nid2str(lp2->lp_primary_nid),
376 libcfs_nid2str(lp->lp_primary_nid));
381 * This function only allows deletion of the primary NID if it
384 if (nid == lp->lp_primary_nid && lnet_get_num_peer_nis(lp) != 1) {
385 CERROR("Cannot delete primary NID %s from multi-NID peer\n",
386 libcfs_nid2str(nid));
390 lnet_net_lock(LNET_LOCK_EX);
391 lnet_peer_ni_del_locked(lpni);
392 lnet_net_unlock(LNET_LOCK_EX);
398 lnet_peer_table_cleanup_locked(struct lnet_net *net,
399 struct lnet_peer_table *ptable)
402 struct lnet_peer_ni *next;
403 struct lnet_peer_ni *lpni;
404 struct lnet_peer *peer;
406 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
407 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
409 if (net != NULL && net != lpni->lpni_net)
412 peer = lpni->lpni_peer_net->lpn_peer;
413 if (peer->lp_primary_nid != lpni->lpni_nid) {
414 lnet_peer_ni_del_locked(lpni);
418 * Removing the primary NID implies removing
419 * the entire peer. Advance next beyond any
420 * peer_ni that belongs to the same peer.
422 list_for_each_entry_from(next, &ptable->pt_hash[i],
424 if (next->lpni_peer_net->lpn_peer != peer)
427 lnet_peer_del_locked(peer);
433 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
437 spin_lock(&ptable->pt_zombie_lock);
438 while (ptable->pt_zombies) {
439 spin_unlock(&ptable->pt_zombie_lock);
441 if (is_power_of_2(i)) {
443 "Waiting for %d zombies on peer table\n",
446 set_current_state(TASK_UNINTERRUPTIBLE);
447 schedule_timeout(cfs_time_seconds(1) >> 1);
448 spin_lock(&ptable->pt_zombie_lock);
450 spin_unlock(&ptable->pt_zombie_lock);
454 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
455 struct lnet_peer_table *ptable)
457 struct lnet_peer_ni *lp;
458 struct lnet_peer_ni *tmp;
462 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
463 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
465 if (net != lp->lpni_net)
468 if (lp->lpni_rtr_refcount == 0)
471 lpni_nid = lp->lpni_nid;
473 lnet_net_unlock(LNET_LOCK_EX);
474 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
475 lnet_net_lock(LNET_LOCK_EX);
481 lnet_peer_tables_cleanup(struct lnet_net *net)
484 struct lnet_peer_table *ptable;
486 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
487 /* If just deleting the peers for a NI, get rid of any routes these
488 * peers are gateways for. */
489 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
490 lnet_net_lock(LNET_LOCK_EX);
491 lnet_peer_table_del_rtrs_locked(net, ptable);
492 lnet_net_unlock(LNET_LOCK_EX);
495 /* Start the cleanup process */
496 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
497 lnet_net_lock(LNET_LOCK_EX);
498 lnet_peer_table_cleanup_locked(net, ptable);
499 lnet_net_unlock(LNET_LOCK_EX);
502 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
503 lnet_peer_ni_finalize_wait(ptable);
506 static struct lnet_peer_ni *
507 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
509 struct list_head *peers;
510 struct lnet_peer_ni *lp;
512 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
514 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
515 list_for_each_entry(lp, peers, lpni_hashlist) {
516 if (lp->lpni_nid == nid) {
517 lnet_peer_ni_addref_locked(lp);
525 struct lnet_peer_ni *
526 lnet_find_peer_ni_locked(lnet_nid_t nid)
528 struct lnet_peer_ni *lpni;
529 struct lnet_peer_table *ptable;
532 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
534 ptable = the_lnet.ln_peer_tables[cpt];
535 lpni = lnet_get_peer_ni_locked(ptable, nid);
540 struct lnet_peer_ni *
541 lnet_get_peer_ni_idx_locked(int idx, struct lnet_peer_net **lpn,
542 struct lnet_peer **lp)
544 struct lnet_peer_ni *lpni;
546 list_for_each_entry((*lp), &the_lnet.ln_peers, lp_on_lnet_peer_list) {
547 list_for_each_entry((*lpn), &((*lp)->lp_peer_nets), lpn_on_peer_list) {
548 list_for_each_entry(lpni, &((*lpn)->lpn_peer_nis),
549 lpni_on_peer_net_list)
558 struct lnet_peer_ni *
559 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
560 struct lnet_peer_net *peer_net,
561 struct lnet_peer_ni *prev)
563 struct lnet_peer_ni *lpni;
564 struct lnet_peer_net *net = peer_net;
568 net = list_entry(peer->lp_peer_nets.next,
569 struct lnet_peer_net,
571 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
572 lpni_on_peer_net_list);
577 if (prev->lpni_on_peer_net_list.next ==
578 &prev->lpni_peer_net->lpn_peer_nis) {
580 * if you reached the end of the peer ni list and the peer
581 * net is specified then there are no more peer nis in that
588 * we reached the end of this net ni list. move to the
591 if (prev->lpni_peer_net->lpn_on_peer_list.next ==
593 /* no more nets and no more NIs. */
596 /* get the next net */
597 net = list_entry(prev->lpni_peer_net->lpn_on_peer_list.next,
598 struct lnet_peer_net,
600 /* get the ni on it */
601 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
602 lpni_on_peer_net_list);
607 /* there are more nis left */
608 lpni = list_entry(prev->lpni_on_peer_net_list.next,
609 struct lnet_peer_ni, lpni_on_peer_net_list);
615 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
616 * this is a preferred point-to-point path. Call with lnet_net_lock in
620 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
624 if (lpni->lpni_pref_nnids == 0)
626 if (lpni->lpni_pref_nnids == 1)
627 return lpni->lpni_pref.nid == nid;
628 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
629 if (lpni->lpni_pref.nids[i] == nid)
636 * Set a single ni as preferred, provided no preferred ni is already
637 * defined. Only to be used for non-multi-rail peer_ni.
640 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
644 spin_lock(&lpni->lpni_lock);
645 if (nid == LNET_NID_ANY) {
647 } else if (lpni->lpni_pref_nnids > 0) {
649 } else if (lpni->lpni_pref_nnids == 0) {
650 lpni->lpni_pref.nid = nid;
651 lpni->lpni_pref_nnids = 1;
652 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
654 spin_unlock(&lpni->lpni_lock);
656 CDEBUG(D_NET, "peer %s nid %s: %d\n",
657 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
662 * Clear the preferred NID from a non-multi-rail peer_ni, provided
663 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
666 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
670 spin_lock(&lpni->lpni_lock);
671 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
672 lpni->lpni_pref_nnids = 0;
673 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
674 } else if (lpni->lpni_pref_nnids == 0) {
679 spin_unlock(&lpni->lpni_lock);
681 CDEBUG(D_NET, "peer %s: %d\n",
682 libcfs_nid2str(lpni->lpni_nid), rc);
687 * Clear the preferred NIDs from a non-multi-rail peer.
690 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
692 struct lnet_peer_ni *lpni = NULL;
694 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
695 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
699 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
701 lnet_nid_t *nids = NULL;
702 lnet_nid_t *oldnids = NULL;
703 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
708 if (nid == LNET_NID_ANY) {
713 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
718 /* A non-MR node may have only one preferred NI per peer_ni */
719 if (lpni->lpni_pref_nnids > 0) {
720 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
726 if (lpni->lpni_pref_nnids != 0) {
727 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
728 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
733 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
734 if (lpni->lpni_pref.nids[i] == nid) {
735 LIBCFS_FREE(nids, size);
739 nids[i] = lpni->lpni_pref.nids[i];
744 lnet_net_lock(LNET_LOCK_EX);
745 spin_lock(&lpni->lpni_lock);
746 if (lpni->lpni_pref_nnids == 0) {
747 lpni->lpni_pref.nid = nid;
749 oldnids = lpni->lpni_pref.nids;
750 lpni->lpni_pref.nids = nids;
752 lpni->lpni_pref_nnids++;
753 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
754 spin_unlock(&lpni->lpni_lock);
755 lnet_net_unlock(LNET_LOCK_EX);
758 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
759 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
762 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
763 spin_lock(&lpni->lpni_lock);
764 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
765 spin_unlock(&lpni->lpni_lock);
767 CDEBUG(D_NET, "peer %s nid %s: %d\n",
768 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
773 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
775 lnet_nid_t *nids = NULL;
776 lnet_nid_t *oldnids = NULL;
777 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
782 if (lpni->lpni_pref_nnids == 0) {
787 if (lpni->lpni_pref_nnids == 1) {
788 if (lpni->lpni_pref.nid != nid) {
792 } else if (lpni->lpni_pref_nnids == 2) {
793 if (lpni->lpni_pref.nids[0] != nid &&
794 lpni->lpni_pref.nids[1] != nid) {
799 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
800 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
805 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
806 if (lpni->lpni_pref.nids[i] != nid)
808 nids[j++] = lpni->lpni_pref.nids[i];
810 /* Check if we actually removed a nid. */
811 if (j == lpni->lpni_pref_nnids) {
812 LIBCFS_FREE(nids, size);
818 lnet_net_lock(LNET_LOCK_EX);
819 spin_lock(&lpni->lpni_lock);
820 if (lpni->lpni_pref_nnids == 1) {
821 lpni->lpni_pref.nid = LNET_NID_ANY;
822 } else if (lpni->lpni_pref_nnids == 2) {
823 oldnids = lpni->lpni_pref.nids;
824 if (oldnids[0] == nid)
825 lpni->lpni_pref.nid = oldnids[1];
827 lpni->lpni_pref.nid = oldnids[2];
829 oldnids = lpni->lpni_pref.nids;
830 lpni->lpni_pref.nids = nids;
832 lpni->lpni_pref_nnids--;
833 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
834 spin_unlock(&lpni->lpni_lock);
835 lnet_net_unlock(LNET_LOCK_EX);
838 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
839 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
842 CDEBUG(D_NET, "peer %s nid %s: %d\n",
843 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
848 lnet_peer_primary_nid_locked(lnet_nid_t nid)
850 struct lnet_peer_ni *lpni;
851 lnet_nid_t primary_nid = nid;
853 lpni = lnet_find_peer_ni_locked(nid);
855 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
856 lnet_peer_ni_decref_locked(lpni);
863 LNetPrimaryNID(lnet_nid_t nid)
865 struct lnet_peer_ni *lpni;
866 lnet_nid_t primary_nid = nid;
870 cpt = lnet_net_lock_current();
871 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
876 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
877 lnet_peer_ni_decref_locked(lpni);
879 lnet_net_unlock(cpt);
881 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
882 libcfs_nid2str(primary_nid), rc);
885 EXPORT_SYMBOL(LNetPrimaryNID);
887 struct lnet_peer_net *
888 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
890 struct lnet_peer_net *peer_net;
891 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
892 if (peer_net->lpn_net_id == net_id)
899 lnet_peer_setup_hierarchy(struct lnet_peer *lp, struct lnet_peer_ni *lpni,
902 struct lnet_peer_net *lpn = NULL;
903 struct lnet_peer_table *ptable;
904 __u32 net_id = LNET_NIDNET(nid);
907 * Create the peer_ni, peer_net, and peer if they don't exist
911 lpn = lnet_peer_get_net_locked(lp, net_id);
913 lp = lnet_peer_alloc(nid);
919 lpn = lnet_peer_net_alloc(net_id);
921 goto out_maybe_free_lp;
925 lpni = lnet_peer_ni_alloc(nid);
927 goto out_maybe_free_lpn;
930 /* Install the new peer_ni */
931 lnet_net_lock(LNET_LOCK_EX);
932 /* Add peer_ni to global peer table hash, if necessary. */
933 if (list_empty(&lpni->lpni_hashlist)) {
934 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
935 list_add_tail(&lpni->lpni_hashlist,
936 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
937 ptable->pt_version++;
938 atomic_inc(&ptable->pt_number);
939 atomic_inc(&lpni->lpni_refcount);
942 /* Detach the peer_ni from an existing peer, if necessary. */
943 if (lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer != lp)
944 lnet_try_destroy_peer_hierarchy_locked(lpni);
946 /* Add peer_ni to peer_net */
947 lpni->lpni_peer_net = lpn;
948 list_add_tail(&lpni->lpni_on_peer_net_list, &lpn->lpn_peer_nis);
950 /* Add peer_net to peer */
951 if (!lpn->lpn_peer) {
953 list_add_tail(&lpn->lpn_on_peer_list, &lp->lp_peer_nets);
956 /* Add peer to global peer list */
957 if (list_empty(&lp->lp_on_lnet_peer_list))
958 list_add_tail(&lp->lp_on_lnet_peer_list, &the_lnet.ln_peers);
959 lnet_net_unlock(LNET_LOCK_EX);
964 if (list_empty(&lpn->lpn_on_peer_list))
965 LIBCFS_FREE(lpn, sizeof(*lpn));
967 if (list_empty(&lp->lp_on_lnet_peer_list))
968 LIBCFS_FREE(lp, sizeof(*lp));
974 * Create a new peer, with nid as its primary nid.
976 * It is not an error if the peer already exists, provided that the
977 * given nid is the primary NID.
979 * Call with the lnet_api_mutex held.
982 lnet_peer_add(lnet_nid_t nid, bool mr)
984 struct lnet_peer *lp;
985 struct lnet_peer_ni *lpni;
988 LASSERT(nid != LNET_NID_ANY);
991 * No need for the lnet_net_lock here, because the
992 * lnet_api_mutex is held.
994 lpni = lnet_find_peer_ni_locked(nid);
996 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
999 lpni = lnet_find_peer_ni_locked(nid);
1002 lp = lpni->lpni_peer_net->lpn_peer;
1003 lnet_peer_ni_decref_locked(lpni);
1005 /* A found peer must have this primary NID */
1006 if (lp->lp_primary_nid != nid)
1010 * If we found an lpni that is not a multi-rail, which could occur
1011 * if lpni is already created as a non-mr lpni or we just created
1012 * it, then make sure you indicate that this lpni is a primary mr
1015 * TODO: update flags if necessary
1017 spin_lock(&lp->lp_lock);
1018 if (mr && !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1019 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1020 lnet_peer_clr_non_mr_pref_nids(lp);
1021 } else if (!mr && (lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1022 /* The mr state is sticky. */
1023 CDEBUG(D_NET, "Cannot clear multi-rail flag from peer %s\n",
1024 libcfs_nid2str(nid));
1026 spin_unlock(&lp->lp_lock);
1032 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, bool mr)
1034 struct lnet_peer_ni *lpni;
1037 LASSERT(nid != LNET_NID_ANY);
1039 spin_lock(&lp->lp_lock);
1040 if (!mr && !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1041 spin_unlock(&lp->lp_lock);
1042 CERROR("Cannot add nid %s to non-multi-rail peer %s\n",
1043 libcfs_nid2str(nid),
1044 libcfs_nid2str(lp->lp_primary_nid));
1048 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1049 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1050 lnet_peer_clr_non_mr_pref_nids(lp);
1052 spin_unlock(&lp->lp_lock);
1054 lpni = lnet_find_peer_ni_locked(nid);
1056 return lnet_peer_setup_hierarchy(lp, NULL, nid);
1058 if (lpni->lpni_peer_net->lpn_peer != lp) {
1059 struct lnet_peer *lp2 = lpni->lpni_peer_net->lpn_peer;
1060 CERROR("Cannot add NID %s owned by peer %s to peer %s\n",
1061 libcfs_nid2str(lpni->lpni_nid),
1062 libcfs_nid2str(lp2->lp_primary_nid),
1063 libcfs_nid2str(lp->lp_primary_nid));
1067 CDEBUG(D_NET, "NID %s is already owned by peer %s\n",
1068 libcfs_nid2str(lpni->lpni_nid),
1069 libcfs_nid2str(lp->lp_primary_nid));
1074 * lpni creation initiated due to traffic either sending or receiving.
1077 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1079 struct lnet_peer_ni *lpni;
1082 if (nid == LNET_NID_ANY)
1085 /* lnet_net_lock is not needed here because ln_api_lock is held */
1086 lpni = lnet_find_peer_ni_locked(nid);
1088 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
1091 lpni = lnet_find_peer_ni_locked(nid);
1093 if (pref != LNET_NID_ANY)
1094 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1095 lnet_peer_ni_decref_locked(lpni);
1101 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1103 * This API handles the following combinations:
1104 * Create a peer with its primary NI if only the prim_nid is provided
1105 * Add a NID to a peer identified by the prim_nid. The peer identified
1106 * by the prim_nid must already exist.
1107 * The peer being created may be non-MR.
1109 * The caller must hold ln_api_mutex. This prevents the peer from
1110 * being created/modified/deleted by a different thread.
1113 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1115 struct lnet_peer *lp = NULL;
1116 struct lnet_peer_ni *lpni;
1118 /* The prim_nid must always be specified */
1119 if (prim_nid == LNET_NID_ANY)
1123 * If nid isn't specified, we must create a new peer with
1124 * prim_nid as its primary nid.
1126 if (nid == LNET_NID_ANY)
1127 return lnet_peer_add(prim_nid, mr);
1129 /* Look up the prim_nid, which must exist. */
1130 lpni = lnet_find_peer_ni_locked(prim_nid);
1133 lnet_peer_ni_decref_locked(lpni);
1134 lp = lpni->lpni_peer_net->lpn_peer;
1136 if (lp->lp_primary_nid != prim_nid) {
1137 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1138 libcfs_nid2str(prim_nid),
1139 libcfs_nid2str(lp->lp_primary_nid));
1143 return lnet_peer_add_nid(lp, nid, mr);
1147 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1149 * This API handles the following combinations:
1150 * Delete a NI from a peer if both prim_nid and nid are provided.
1151 * Delete a peer if only prim_nid is provided.
1152 * Delete a peer if its primary nid is provided.
1154 * The caller must hold ln_api_mutex. This prevents the peer from
1155 * being modified/deleted by a different thread.
1158 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1160 struct lnet_peer *lp;
1161 struct lnet_peer_ni *lpni;
1163 if (prim_nid == LNET_NID_ANY)
1166 lpni = lnet_find_peer_ni_locked(prim_nid);
1169 lnet_peer_ni_decref_locked(lpni);
1170 lp = lpni->lpni_peer_net->lpn_peer;
1172 if (prim_nid != lp->lp_primary_nid) {
1173 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1174 libcfs_nid2str(prim_nid),
1175 libcfs_nid2str(lp->lp_primary_nid));
1179 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1180 return lnet_peer_del(lp);
1182 return lnet_peer_del_nid(lp, nid);
1186 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1188 struct lnet_peer_table *ptable;
1190 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1191 LASSERT(lpni->lpni_rtr_refcount == 0);
1192 LASSERT(list_empty(&lpni->lpni_txq));
1193 LASSERT(lpni->lpni_txqnob == 0);
1195 lpni->lpni_net = NULL;
1197 /* remove the peer ni from the zombie list */
1198 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1199 spin_lock(&ptable->pt_zombie_lock);
1200 list_del_init(&lpni->lpni_hashlist);
1201 ptable->pt_zombies--;
1202 spin_unlock(&ptable->pt_zombie_lock);
1204 if (lpni->lpni_pref_nnids > 1) {
1205 LIBCFS_FREE(lpni->lpni_pref.nids,
1206 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1208 LIBCFS_FREE(lpni, sizeof(*lpni));
1211 struct lnet_peer_ni *
1212 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1214 struct lnet_peer_ni *lpni = NULL;
1217 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1218 return ERR_PTR(-ESHUTDOWN);
1221 * find if a peer_ni already exists.
1222 * If so then just return that.
1224 lpni = lnet_find_peer_ni_locked(nid);
1228 lnet_net_unlock(cpt);
1230 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1233 goto out_net_relock;
1236 lpni = lnet_find_peer_ni_locked(nid);
1245 struct lnet_peer_ni *
1246 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1248 struct lnet_peer_ni *lpni = NULL;
1251 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1252 return ERR_PTR(-ESHUTDOWN);
1255 * find if a peer_ni already exists.
1256 * If so then just return that.
1258 lpni = lnet_find_peer_ni_locked(nid);
1264 * use the lnet_api_mutex to serialize the creation of the peer_ni
1265 * and the creation/deletion of the local ni/net. When a local ni is
1266 * created, if there exists a set of peer_nis on that network,
1267 * they need to be traversed and updated. When a local NI is
1268 * deleted, which could result in a network being deleted, then
1269 * all peer nis on that network need to be removed as well.
1271 * Creation through traffic should also be serialized with
1272 * creation through DLC.
1274 lnet_net_unlock(cpt);
1275 mutex_lock(&the_lnet.ln_api_mutex);
1277 * Shutdown is only set under the ln_api_lock, so a single
1278 * check here is sufficent.
1280 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1281 lpni = ERR_PTR(-ESHUTDOWN);
1282 goto out_mutex_unlock;
1285 rc = lnet_peer_ni_traffic_add(nid, pref);
1288 goto out_mutex_unlock;
1291 lpni = lnet_find_peer_ni_locked(nid);
1295 mutex_unlock(&the_lnet.ln_api_mutex);
1302 lnet_debug_peer(lnet_nid_t nid)
1304 char *aliveness = "NA";
1305 struct lnet_peer_ni *lp;
1308 cpt = lnet_cpt_of_nid(nid, NULL);
1311 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1313 lnet_net_unlock(cpt);
1314 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
1318 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
1319 aliveness = lp->lpni_alive ? "up" : "down";
1321 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
1322 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
1323 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
1324 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
1325 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
1327 lnet_peer_ni_decref_locked(lp);
1329 lnet_net_unlock(cpt);
1332 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
1333 char aliveness[LNET_MAX_STR_LEN],
1334 __u32 *cpt_iter, __u32 *refcount,
1335 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
1336 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
1337 __u32 *peer_tx_qnob)
1339 struct lnet_peer_table *peer_table;
1340 struct lnet_peer_ni *lp;
1345 /* get the number of CPTs */
1346 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
1348 /* if the cpt number to be examined is >= the number of cpts in
1349 * the system then indicate that there are no more cpts to examin
1351 if (*cpt_iter >= lncpt)
1354 /* get the current table */
1355 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
1356 /* if the ptable is NULL then there are no more cpts to examine */
1357 if (peer_table == NULL)
1360 lnet_net_lock(*cpt_iter);
1362 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
1363 struct list_head *peers = &peer_table->pt_hash[j];
1365 list_for_each_entry(lp, peers, lpni_hashlist) {
1366 if (peer_index-- > 0)
1369 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
1370 if (lnet_isrouter(lp) ||
1371 lnet_peer_aliveness_enabled(lp))
1372 snprintf(aliveness, LNET_MAX_STR_LEN,
1373 lp->lpni_alive ? "up" : "down");
1375 *nid = lp->lpni_nid;
1376 *refcount = atomic_read(&lp->lpni_refcount);
1377 *ni_peer_tx_credits =
1378 lp->lpni_net->net_tunables.lct_peer_tx_credits;
1379 *peer_tx_credits = lp->lpni_txcredits;
1380 *peer_rtr_credits = lp->lpni_rtrcredits;
1381 *peer_min_rtr_credits = lp->lpni_mintxcredits;
1382 *peer_tx_qnob = lp->lpni_txqnob;
1388 lnet_net_unlock(*cpt_iter);
1392 return found ? 0 : -ENOENT;
1395 int lnet_get_peer_info(__u32 idx, lnet_nid_t *primary_nid, lnet_nid_t *nid,
1397 struct lnet_peer_ni_credit_info __user *peer_ni_info,
1398 struct lnet_ioctl_element_stats __user *peer_ni_stats)
1400 struct lnet_peer_ni *lpni = NULL;
1401 struct lnet_peer_net *lpn = NULL;
1402 struct lnet_peer *lp = NULL;
1403 struct lnet_peer_ni_credit_info ni_info;
1404 struct lnet_ioctl_element_stats ni_stats;
1407 lpni = lnet_get_peer_ni_idx_locked(idx, &lpn, &lp);
1412 *primary_nid = lp->lp_primary_nid;
1413 *mr = lnet_peer_is_multi_rail(lp);
1414 *nid = lpni->lpni_nid;
1415 snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN, "NA");
1416 if (lnet_isrouter(lpni) ||
1417 lnet_peer_aliveness_enabled(lpni))
1418 snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN,
1419 lpni->lpni_alive ? "up" : "down");
1421 ni_info.cr_refcount = atomic_read(&lpni->lpni_refcount);
1422 ni_info.cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
1423 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
1424 ni_info.cr_peer_tx_credits = lpni->lpni_txcredits;
1425 ni_info.cr_peer_rtr_credits = lpni->lpni_rtrcredits;
1426 ni_info.cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
1427 ni_info.cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
1428 ni_info.cr_peer_tx_qnob = lpni->lpni_txqnob;
1429 ni_info.cr_ncpt = lpni->lpni_cpt;
1431 ni_stats.iel_send_count = atomic_read(&lpni->lpni_stats.send_count);
1432 ni_stats.iel_recv_count = atomic_read(&lpni->lpni_stats.recv_count);
1433 ni_stats.iel_drop_count = atomic_read(&lpni->lpni_stats.drop_count);
1435 /* If copy_to_user fails */
1437 if (copy_to_user(peer_ni_info, &ni_info, sizeof(ni_info)))
1440 if (copy_to_user(peer_ni_stats, &ni_stats, sizeof(ni_stats)))