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>
40 /* Value indicating that recovery needs to re-check a peer immediately. */
41 #define LNET_REDISCOVER_PEER (1)
43 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
46 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
48 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
49 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
50 lnet_peer_ni_decref_locked(lpni);
55 lnet_peer_net_added(struct lnet_net *net)
57 struct lnet_peer_ni *lpni, *tmp;
59 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
60 lpni_on_remote_peer_ni_list) {
62 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
65 spin_lock(&lpni->lpni_lock);
66 lpni->lpni_txcredits =
67 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
68 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
69 lpni->lpni_rtrcredits =
70 lnet_peer_buffer_credits(lpni->lpni_net);
71 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
72 spin_unlock(&lpni->lpni_lock);
74 lnet_peer_remove_from_remote_list(lpni);
80 lnet_peer_tables_destroy(void)
82 struct lnet_peer_table *ptable;
83 struct list_head *hash;
87 if (!the_lnet.ln_peer_tables)
90 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
91 hash = ptable->pt_hash;
92 if (!hash) /* not intialized */
95 LASSERT(list_empty(&ptable->pt_zombie_list));
97 ptable->pt_hash = NULL;
98 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
99 LASSERT(list_empty(&hash[j]));
101 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
104 cfs_percpt_free(the_lnet.ln_peer_tables);
105 the_lnet.ln_peer_tables = NULL;
109 lnet_peer_tables_create(void)
111 struct lnet_peer_table *ptable;
112 struct list_head *hash;
116 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
118 if (the_lnet.ln_peer_tables == NULL) {
119 CERROR("Failed to allocate cpu-partition peer tables\n");
123 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
124 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
125 LNET_PEER_HASH_SIZE * sizeof(*hash));
127 CERROR("Failed to create peer hash table\n");
128 lnet_peer_tables_destroy();
132 spin_lock_init(&ptable->pt_zombie_lock);
133 INIT_LIST_HEAD(&ptable->pt_zombie_list);
135 INIT_LIST_HEAD(&ptable->pt_peer_list);
137 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
138 INIT_LIST_HEAD(&hash[j]);
139 ptable->pt_hash = hash; /* sign of initialization */
145 static struct lnet_peer_ni *
146 lnet_peer_ni_alloc(lnet_nid_t nid)
148 struct lnet_peer_ni *lpni;
149 struct lnet_net *net;
152 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
154 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
158 INIT_LIST_HEAD(&lpni->lpni_txq);
159 INIT_LIST_HEAD(&lpni->lpni_rtrq);
160 INIT_LIST_HEAD(&lpni->lpni_routes);
161 INIT_LIST_HEAD(&lpni->lpni_hashlist);
162 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
163 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
165 spin_lock_init(&lpni->lpni_lock);
167 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
168 lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
169 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
170 lpni->lpni_nid = nid;
171 lpni->lpni_cpt = cpt;
172 lnet_set_peer_ni_health_locked(lpni, true);
174 net = lnet_get_net_locked(LNET_NIDNET(nid));
175 lpni->lpni_net = net;
177 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
178 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
179 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
180 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
183 * This peer_ni is not on a local network, so we
184 * cannot add the credits here. In case the net is
185 * added later, add the peer_ni to the remote peer ni
186 * list so it can be easily found and revisited.
188 /* FIXME: per-net implementation instead? */
189 atomic_inc(&lpni->lpni_refcount);
190 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
191 &the_lnet.ln_remote_peer_ni_list);
194 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
199 static struct lnet_peer_net *
200 lnet_peer_net_alloc(__u32 net_id)
202 struct lnet_peer_net *lpn;
204 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
208 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
209 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
210 lpn->lpn_net_id = net_id;
212 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
218 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
220 struct lnet_peer *lp;
222 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
224 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
225 LASSERT(list_empty(&lpn->lpn_peer_nis));
226 LASSERT(list_empty(&lpn->lpn_peer_nets));
228 lpn->lpn_peer = NULL;
229 LIBCFS_FREE(lpn, sizeof(*lpn));
231 lnet_peer_decref_locked(lp);
234 static struct lnet_peer *
235 lnet_peer_alloc(lnet_nid_t nid)
237 struct lnet_peer *lp;
239 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
243 INIT_LIST_HEAD(&lp->lp_peer_list);
244 INIT_LIST_HEAD(&lp->lp_peer_nets);
245 INIT_LIST_HEAD(&lp->lp_dc_list);
246 INIT_LIST_HEAD(&lp->lp_dc_pendq);
247 init_waitqueue_head(&lp->lp_dc_waitq);
248 spin_lock_init(&lp->lp_lock);
249 lp->lp_primary_nid = nid;
250 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
252 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
258 lnet_destroy_peer_locked(struct lnet_peer *lp)
260 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
262 LASSERT(atomic_read(&lp->lp_refcount) == 0);
263 LASSERT(list_empty(&lp->lp_peer_nets));
264 LASSERT(list_empty(&lp->lp_peer_list));
265 LASSERT(list_empty(&lp->lp_dc_list));
268 lnet_ping_buffer_decref(lp->lp_data);
270 LIBCFS_FREE(lp, sizeof(*lp));
274 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
275 * that peer_net, detach the peer_net from the peer.
277 * Call with lnet_net_lock/EX held
280 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
282 struct lnet_peer_table *ptable;
283 struct lnet_peer_net *lpn;
284 struct lnet_peer *lp;
287 * Belts and suspenders: gracefully handle teardown of a
288 * partially connected peer_ni.
290 lpn = lpni->lpni_peer_net;
292 list_del_init(&lpni->lpni_peer_nis);
294 * If there are no lpni's left, we detach lpn from
295 * lp_peer_nets, so it cannot be found anymore.
297 if (list_empty(&lpn->lpn_peer_nis))
298 list_del_init(&lpn->lpn_peer_nets);
300 /* Update peer NID count. */
302 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
304 ptable->pt_peer_nnids--;
307 * If there are no more peer nets, make the peer unfindable
308 * via the peer_tables.
310 * Otherwise, if the peer is DISCOVERED, tell discovery to
311 * take another look at it. This is a no-op if discovery for
312 * this peer did the detaching.
314 if (list_empty(&lp->lp_peer_nets)) {
315 list_del_init(&lp->lp_peer_list);
317 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
318 /* Discovery isn't running, nothing to do here. */
319 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
320 lnet_peer_queue_for_discovery(lp);
321 wake_up(&the_lnet.ln_dc_waitq);
323 CDEBUG(D_NET, "peer %s NID %s\n",
324 libcfs_nid2str(lp->lp_primary_nid),
325 libcfs_nid2str(lpni->lpni_nid));
328 /* called with lnet_net_lock LNET_LOCK_EX held */
330 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
332 struct lnet_peer_table *ptable = NULL;
334 /* don't remove a peer_ni if it's also a gateway */
335 if (lpni->lpni_rtr_refcount > 0) {
336 CERROR("Peer NI %s is a gateway. Can not delete it\n",
337 libcfs_nid2str(lpni->lpni_nid));
341 lnet_peer_remove_from_remote_list(lpni);
343 /* remove peer ni from the hash list. */
344 list_del_init(&lpni->lpni_hashlist);
346 /* decrement the ref count on the peer table */
347 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
348 LASSERT(ptable->pt_number > 0);
352 * The peer_ni can no longer be found with a lookup. But there
353 * can be current users, so keep track of it on the zombie
354 * list until the reference count has gone to zero.
356 * The last reference may be lost in a place where the
357 * lnet_net_lock locks only a single cpt, and that cpt may not
358 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
361 spin_lock(&ptable->pt_zombie_lock);
362 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
363 ptable->pt_zombies++;
364 spin_unlock(&ptable->pt_zombie_lock);
366 /* no need to keep this peer_ni on the hierarchy anymore */
367 lnet_peer_detach_peer_ni_locked(lpni);
369 /* remove hashlist reference on peer_ni */
370 lnet_peer_ni_decref_locked(lpni);
375 void lnet_peer_uninit(void)
377 struct lnet_peer_ni *lpni, *tmp;
379 lnet_net_lock(LNET_LOCK_EX);
381 /* remove all peer_nis from the remote peer and the hash list */
382 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
383 lpni_on_remote_peer_ni_list)
384 lnet_peer_ni_del_locked(lpni);
386 lnet_peer_tables_destroy();
388 lnet_net_unlock(LNET_LOCK_EX);
392 lnet_peer_del_locked(struct lnet_peer *peer)
394 struct lnet_peer_ni *lpni = NULL, *lpni2;
397 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
399 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
400 while (lpni != NULL) {
401 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
402 rc = lnet_peer_ni_del_locked(lpni);
412 lnet_peer_del(struct lnet_peer *peer)
414 lnet_net_lock(LNET_LOCK_EX);
415 lnet_peer_del_locked(peer);
416 lnet_net_unlock(LNET_LOCK_EX);
422 * Delete a NID from a peer. Call with ln_api_mutex held.
425 * -EPERM: Non-DLC deletion from DLC-configured peer.
426 * -ENOENT: No lnet_peer_ni corresponding to the nid.
427 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
428 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
431 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
433 struct lnet_peer_ni *lpni;
434 lnet_nid_t primary_nid = lp->lp_primary_nid;
437 if (!(flags & LNET_PEER_CONFIGURED)) {
438 if (lp->lp_state & LNET_PEER_CONFIGURED) {
443 lpni = lnet_find_peer_ni_locked(nid);
448 lnet_peer_ni_decref_locked(lpni);
449 if (lp != lpni->lpni_peer_net->lpn_peer) {
455 * This function only allows deletion of the primary NID if it
458 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
463 lnet_net_lock(LNET_LOCK_EX);
464 lnet_peer_ni_del_locked(lpni);
465 lnet_net_unlock(LNET_LOCK_EX);
468 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
469 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
475 lnet_peer_table_cleanup_locked(struct lnet_net *net,
476 struct lnet_peer_table *ptable)
479 struct lnet_peer_ni *next;
480 struct lnet_peer_ni *lpni;
481 struct lnet_peer *peer;
483 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
484 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
486 if (net != NULL && net != lpni->lpni_net)
489 peer = lpni->lpni_peer_net->lpn_peer;
490 if (peer->lp_primary_nid != lpni->lpni_nid) {
491 lnet_peer_ni_del_locked(lpni);
495 * Removing the primary NID implies removing
496 * the entire peer. Advance next beyond any
497 * peer_ni that belongs to the same peer.
499 list_for_each_entry_from(next, &ptable->pt_hash[i],
501 if (next->lpni_peer_net->lpn_peer != peer)
504 lnet_peer_del_locked(peer);
510 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
514 spin_lock(&ptable->pt_zombie_lock);
515 while (ptable->pt_zombies) {
516 spin_unlock(&ptable->pt_zombie_lock);
518 if (is_power_of_2(i)) {
520 "Waiting for %d zombies on peer table\n",
523 set_current_state(TASK_UNINTERRUPTIBLE);
524 schedule_timeout(cfs_time_seconds(1) >> 1);
525 spin_lock(&ptable->pt_zombie_lock);
527 spin_unlock(&ptable->pt_zombie_lock);
531 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
532 struct lnet_peer_table *ptable)
534 struct lnet_peer_ni *lp;
535 struct lnet_peer_ni *tmp;
539 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
540 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
542 if (net != lp->lpni_net)
545 if (lp->lpni_rtr_refcount == 0)
548 lpni_nid = lp->lpni_nid;
550 lnet_net_unlock(LNET_LOCK_EX);
551 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
552 lnet_net_lock(LNET_LOCK_EX);
558 lnet_peer_tables_cleanup(struct lnet_net *net)
561 struct lnet_peer_table *ptable;
563 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
564 /* If just deleting the peers for a NI, get rid of any routes these
565 * peers are gateways for. */
566 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
567 lnet_net_lock(LNET_LOCK_EX);
568 lnet_peer_table_del_rtrs_locked(net, ptable);
569 lnet_net_unlock(LNET_LOCK_EX);
572 /* Start the cleanup process */
573 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
574 lnet_net_lock(LNET_LOCK_EX);
575 lnet_peer_table_cleanup_locked(net, ptable);
576 lnet_net_unlock(LNET_LOCK_EX);
579 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
580 lnet_peer_ni_finalize_wait(ptable);
583 static struct lnet_peer_ni *
584 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
586 struct list_head *peers;
587 struct lnet_peer_ni *lp;
589 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
591 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
592 list_for_each_entry(lp, peers, lpni_hashlist) {
593 if (lp->lpni_nid == nid) {
594 lnet_peer_ni_addref_locked(lp);
602 struct lnet_peer_ni *
603 lnet_find_peer_ni_locked(lnet_nid_t nid)
605 struct lnet_peer_ni *lpni;
606 struct lnet_peer_table *ptable;
609 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
611 ptable = the_lnet.ln_peer_tables[cpt];
612 lpni = lnet_get_peer_ni_locked(ptable, nid);
618 lnet_find_peer(lnet_nid_t nid)
620 struct lnet_peer_ni *lpni;
621 struct lnet_peer *lp = NULL;
624 cpt = lnet_net_lock_current();
625 lpni = lnet_find_peer_ni_locked(nid);
627 lp = lpni->lpni_peer_net->lpn_peer;
628 lnet_peer_addref_locked(lp);
629 lnet_peer_ni_decref_locked(lpni);
631 lnet_net_unlock(cpt);
636 struct lnet_peer_ni *
637 lnet_get_peer_ni_idx_locked(int idx, struct lnet_peer_net **lpn,
638 struct lnet_peer **lp)
640 struct lnet_peer_table *ptable;
641 struct lnet_peer_ni *lpni;
645 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
647 for (cpt = 0; cpt < lncpt; cpt++) {
648 ptable = the_lnet.ln_peer_tables[cpt];
649 if (ptable->pt_peer_nnids > idx)
651 idx -= ptable->pt_peer_nnids;
656 list_for_each_entry((*lp), &ptable->pt_peer_list, lp_peer_list) {
657 if ((*lp)->lp_nnis <= idx) {
658 idx -= (*lp)->lp_nnis;
661 list_for_each_entry((*lpn), &((*lp)->lp_peer_nets),
663 list_for_each_entry(lpni, &((*lpn)->lpn_peer_nis),
674 struct lnet_peer_ni *
675 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
676 struct lnet_peer_net *peer_net,
677 struct lnet_peer_ni *prev)
679 struct lnet_peer_ni *lpni;
680 struct lnet_peer_net *net = peer_net;
684 if (list_empty(&peer->lp_peer_nets))
687 net = list_entry(peer->lp_peer_nets.next,
688 struct lnet_peer_net,
691 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
697 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
699 * if you reached the end of the peer ni list and the peer
700 * net is specified then there are no more peer nis in that
707 * we reached the end of this net ni list. move to the
710 if (prev->lpni_peer_net->lpn_peer_nets.next ==
712 /* no more nets and no more NIs. */
715 /* get the next net */
716 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
717 struct lnet_peer_net,
719 /* get the ni on it */
720 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
726 /* there are more nis left */
727 lpni = list_entry(prev->lpni_peer_nis.next,
728 struct lnet_peer_ni, lpni_peer_nis);
734 * Start pushes to peers that need to be updated for a configuration
735 * change on this node.
738 lnet_push_update_to_peers(int force)
740 struct lnet_peer_table *ptable;
741 struct lnet_peer *lp;
745 lnet_net_lock(LNET_LOCK_EX);
746 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
747 for (cpt = 0; cpt < lncpt; cpt++) {
748 ptable = the_lnet.ln_peer_tables[cpt];
749 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
751 spin_lock(&lp->lp_lock);
752 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
753 lp->lp_state |= LNET_PEER_FORCE_PUSH;
754 spin_unlock(&lp->lp_lock);
756 if (lnet_peer_needs_push(lp))
757 lnet_peer_queue_for_discovery(lp);
760 lnet_net_unlock(LNET_LOCK_EX);
761 wake_up(&the_lnet.ln_dc_waitq);
765 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
766 * this is a preferred point-to-point path. Call with lnet_net_lock in
770 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
774 if (lpni->lpni_pref_nnids == 0)
776 if (lpni->lpni_pref_nnids == 1)
777 return lpni->lpni_pref.nid == nid;
778 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
779 if (lpni->lpni_pref.nids[i] == nid)
786 * Set a single ni as preferred, provided no preferred ni is already
787 * defined. Only to be used for non-multi-rail peer_ni.
790 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
794 spin_lock(&lpni->lpni_lock);
795 if (nid == LNET_NID_ANY) {
797 } else if (lpni->lpni_pref_nnids > 0) {
799 } else if (lpni->lpni_pref_nnids == 0) {
800 lpni->lpni_pref.nid = nid;
801 lpni->lpni_pref_nnids = 1;
802 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
804 spin_unlock(&lpni->lpni_lock);
806 CDEBUG(D_NET, "peer %s nid %s: %d\n",
807 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
812 * Clear the preferred NID from a non-multi-rail peer_ni, provided
813 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
816 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
820 spin_lock(&lpni->lpni_lock);
821 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
822 lpni->lpni_pref_nnids = 0;
823 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
824 } else if (lpni->lpni_pref_nnids == 0) {
829 spin_unlock(&lpni->lpni_lock);
831 CDEBUG(D_NET, "peer %s: %d\n",
832 libcfs_nid2str(lpni->lpni_nid), rc);
837 * Clear the preferred NIDs from a non-multi-rail peer.
840 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
842 struct lnet_peer_ni *lpni = NULL;
844 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
845 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
849 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
851 lnet_nid_t *nids = NULL;
852 lnet_nid_t *oldnids = NULL;
853 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
858 if (nid == LNET_NID_ANY) {
863 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
868 /* A non-MR node may have only one preferred NI per peer_ni */
869 if (lpni->lpni_pref_nnids > 0) {
870 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
876 if (lpni->lpni_pref_nnids != 0) {
877 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
878 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
883 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
884 if (lpni->lpni_pref.nids[i] == nid) {
885 LIBCFS_FREE(nids, size);
889 nids[i] = lpni->lpni_pref.nids[i];
894 lnet_net_lock(LNET_LOCK_EX);
895 spin_lock(&lpni->lpni_lock);
896 if (lpni->lpni_pref_nnids == 0) {
897 lpni->lpni_pref.nid = nid;
899 oldnids = lpni->lpni_pref.nids;
900 lpni->lpni_pref.nids = nids;
902 lpni->lpni_pref_nnids++;
903 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
904 spin_unlock(&lpni->lpni_lock);
905 lnet_net_unlock(LNET_LOCK_EX);
908 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
909 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
912 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
913 spin_lock(&lpni->lpni_lock);
914 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
915 spin_unlock(&lpni->lpni_lock);
917 CDEBUG(D_NET, "peer %s nid %s: %d\n",
918 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
923 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
925 lnet_nid_t *nids = NULL;
926 lnet_nid_t *oldnids = NULL;
927 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
932 if (lpni->lpni_pref_nnids == 0) {
937 if (lpni->lpni_pref_nnids == 1) {
938 if (lpni->lpni_pref.nid != nid) {
942 } else if (lpni->lpni_pref_nnids == 2) {
943 if (lpni->lpni_pref.nids[0] != nid &&
944 lpni->lpni_pref.nids[1] != nid) {
949 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
950 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
955 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
956 if (lpni->lpni_pref.nids[i] != nid)
958 nids[j++] = lpni->lpni_pref.nids[i];
960 /* Check if we actually removed a nid. */
961 if (j == lpni->lpni_pref_nnids) {
962 LIBCFS_FREE(nids, size);
968 lnet_net_lock(LNET_LOCK_EX);
969 spin_lock(&lpni->lpni_lock);
970 if (lpni->lpni_pref_nnids == 1) {
971 lpni->lpni_pref.nid = LNET_NID_ANY;
972 } else if (lpni->lpni_pref_nnids == 2) {
973 oldnids = lpni->lpni_pref.nids;
974 if (oldnids[0] == nid)
975 lpni->lpni_pref.nid = oldnids[1];
977 lpni->lpni_pref.nid = oldnids[2];
979 oldnids = lpni->lpni_pref.nids;
980 lpni->lpni_pref.nids = nids;
982 lpni->lpni_pref_nnids--;
983 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
984 spin_unlock(&lpni->lpni_lock);
985 lnet_net_unlock(LNET_LOCK_EX);
988 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
989 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
992 CDEBUG(D_NET, "peer %s nid %s: %d\n",
993 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
998 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1000 struct lnet_peer_ni *lpni;
1001 lnet_nid_t primary_nid = nid;
1003 lpni = lnet_find_peer_ni_locked(nid);
1005 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1006 lnet_peer_ni_decref_locked(lpni);
1013 LNetPrimaryNID(lnet_nid_t nid)
1015 struct lnet_peer *lp;
1016 struct lnet_peer_ni *lpni;
1017 lnet_nid_t primary_nid = nid;
1021 cpt = lnet_net_lock_current();
1022 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1027 lp = lpni->lpni_peer_net->lpn_peer;
1028 while (!lnet_peer_is_uptodate(lp)) {
1029 rc = lnet_discover_peer_locked(lpni, cpt, true);
1032 lp = lpni->lpni_peer_net->lpn_peer;
1034 primary_nid = lp->lp_primary_nid;
1036 lnet_peer_ni_decref_locked(lpni);
1038 lnet_net_unlock(cpt);
1040 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1041 libcfs_nid2str(primary_nid), rc);
1044 EXPORT_SYMBOL(LNetPrimaryNID);
1046 struct lnet_peer_net *
1047 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1049 struct lnet_peer_net *peer_net;
1050 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1051 if (peer_net->lpn_net_id == net_id)
1058 * Attach a peer_ni to a peer_net and peer. This function assumes
1059 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1060 * may be attached to a different peer, in which case it will be
1061 * properly detached first. The whole operation is done atomically.
1063 * Always returns 0. This is the last function called from functions
1064 * that do return an int, so returning 0 here allows the compiler to
1068 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1069 struct lnet_peer_net *lpn,
1070 struct lnet_peer_ni *lpni,
1073 struct lnet_peer_table *ptable;
1075 /* Install the new peer_ni */
1076 lnet_net_lock(LNET_LOCK_EX);
1077 /* Add peer_ni to global peer table hash, if necessary. */
1078 if (list_empty(&lpni->lpni_hashlist)) {
1079 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1081 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1082 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1083 ptable->pt_version++;
1084 ptable->pt_number++;
1085 /* This is the 1st refcount on lpni. */
1086 atomic_inc(&lpni->lpni_refcount);
1089 /* Detach the peer_ni from an existing peer, if necessary. */
1090 if (lpni->lpni_peer_net) {
1091 LASSERT(lpni->lpni_peer_net != lpn);
1092 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1093 lnet_peer_detach_peer_ni_locked(lpni);
1094 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1095 lpni->lpni_peer_net = NULL;
1098 /* Add peer_ni to peer_net */
1099 lpni->lpni_peer_net = lpn;
1100 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1101 lnet_peer_net_addref_locked(lpn);
1103 /* Add peer_net to peer */
1104 if (!lpn->lpn_peer) {
1106 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1107 lnet_peer_addref_locked(lp);
1110 /* Add peer to global peer list, if necessary */
1111 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1112 if (list_empty(&lp->lp_peer_list)) {
1113 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1118 /* Update peer state */
1119 spin_lock(&lp->lp_lock);
1120 if (flags & LNET_PEER_CONFIGURED) {
1121 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1122 lp->lp_state |= LNET_PEER_CONFIGURED;
1124 if (flags & LNET_PEER_MULTI_RAIL) {
1125 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1126 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1127 lnet_peer_clr_non_mr_pref_nids(lp);
1130 spin_unlock(&lp->lp_lock);
1133 the_lnet.ln_peer_tables[lp->lp_cpt]->pt_peer_nnids++;
1134 lnet_net_unlock(LNET_LOCK_EX);
1136 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1137 libcfs_nid2str(lp->lp_primary_nid),
1138 libcfs_nid2str(lpni->lpni_nid), flags);
1144 * Create a new peer, with nid as its primary nid.
1146 * Call with the lnet_api_mutex held.
1149 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1151 struct lnet_peer *lp;
1152 struct lnet_peer_net *lpn;
1153 struct lnet_peer_ni *lpni;
1156 LASSERT(nid != LNET_NID_ANY);
1159 * No need for the lnet_net_lock here, because the
1160 * lnet_api_mutex is held.
1162 lpni = lnet_find_peer_ni_locked(nid);
1164 /* A peer with this NID already exists. */
1165 lp = lpni->lpni_peer_net->lpn_peer;
1166 lnet_peer_ni_decref_locked(lpni);
1168 * This is an error if the peer was configured and the
1169 * primary NID differs or an attempt is made to change
1170 * the Multi-Rail flag. Otherwise the assumption is
1171 * that an existing peer is being modified.
1173 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1174 if (lp->lp_primary_nid != nid)
1176 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1180 /* Delete and recreate as a configured peer. */
1184 /* Create peer, peer_net, and peer_ni. */
1186 lp = lnet_peer_alloc(nid);
1189 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1192 lpni = lnet_peer_ni_alloc(nid);
1196 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1199 LIBCFS_FREE(lpn, sizeof(*lpn));
1201 LIBCFS_FREE(lp, sizeof(*lp));
1203 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1204 libcfs_nid2str(nid), flags, rc);
1209 * Add a NID to a peer. Call with ln_api_mutex held.
1212 * -EPERM: Non-DLC addition to a DLC-configured peer.
1213 * -EEXIST: The NID was configured by DLC for a different peer.
1214 * -ENOMEM: Out of memory.
1215 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1216 * non-multi-rail peer.
1219 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1221 struct lnet_peer_net *lpn;
1222 struct lnet_peer_ni *lpni;
1226 LASSERT(nid != LNET_NID_ANY);
1228 /* A configured peer can only be updated through configuration. */
1229 if (!(flags & LNET_PEER_CONFIGURED)) {
1230 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1237 * The MULTI_RAIL flag can be set but not cleared, because
1238 * that would leave the peer struct in an invalid state.
1240 if (flags & LNET_PEER_MULTI_RAIL) {
1241 spin_lock(&lp->lp_lock);
1242 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1243 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1244 lnet_peer_clr_non_mr_pref_nids(lp);
1246 spin_unlock(&lp->lp_lock);
1247 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1252 lpni = lnet_find_peer_ni_locked(nid);
1255 * A peer_ni already exists. This is only a problem if
1256 * it is not connected to this peer and was configured
1259 lnet_peer_ni_decref_locked(lpni);
1260 if (lpni->lpni_peer_net->lpn_peer == lp)
1262 if (lnet_peer_ni_is_configured(lpni)) {
1266 /* If this is the primary NID, destroy the peer. */
1267 if (lnet_peer_ni_is_primary(lpni)) {
1268 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1269 lpni = lnet_peer_ni_alloc(nid);
1276 lpni = lnet_peer_ni_alloc(nid);
1284 * Get the peer_net. Check that we're not adding a second
1285 * peer_ni on a peer_net of a non-multi-rail peer.
1287 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1289 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1294 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1299 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1302 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1303 if (!lpni->lpni_peer_net)
1304 LIBCFS_FREE(lpni, sizeof(*lpni));
1306 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1307 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1313 * Update the primary NID of a peer, if possible.
1315 * Call with the lnet_api_mutex held.
1318 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1320 lnet_nid_t old = lp->lp_primary_nid;
1323 if (lp->lp_primary_nid == nid)
1325 rc = lnet_peer_add_nid(lp, nid, flags);
1328 lp->lp_primary_nid = nid;
1330 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1331 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1336 * lpni creation initiated due to traffic either sending or receiving.
1339 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1341 struct lnet_peer *lp;
1342 struct lnet_peer_net *lpn;
1343 struct lnet_peer_ni *lpni;
1347 if (nid == LNET_NID_ANY) {
1352 /* lnet_net_lock is not needed here because ln_api_lock is held */
1353 lpni = lnet_find_peer_ni_locked(nid);
1356 * We must have raced with another thread. Since we
1357 * know next to nothing about a peer_ni created by
1358 * traffic, we just assume everything is ok and
1361 lnet_peer_ni_decref_locked(lpni);
1365 /* Create peer, peer_net, and peer_ni. */
1367 lp = lnet_peer_alloc(nid);
1370 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1373 lpni = lnet_peer_ni_alloc(nid);
1376 if (pref != LNET_NID_ANY)
1377 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1379 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1382 LIBCFS_FREE(lpn, sizeof(*lpn));
1384 LIBCFS_FREE(lp, sizeof(*lp));
1386 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1391 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1393 * This API handles the following combinations:
1394 * Create a peer with its primary NI if only the prim_nid is provided
1395 * Add a NID to a peer identified by the prim_nid. The peer identified
1396 * by the prim_nid must already exist.
1397 * The peer being created may be non-MR.
1399 * The caller must hold ln_api_mutex. This prevents the peer from
1400 * being created/modified/deleted by a different thread.
1403 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1405 struct lnet_peer *lp = NULL;
1406 struct lnet_peer_ni *lpni;
1409 /* The prim_nid must always be specified */
1410 if (prim_nid == LNET_NID_ANY)
1413 flags = LNET_PEER_CONFIGURED;
1415 flags |= LNET_PEER_MULTI_RAIL;
1418 * If nid isn't specified, we must create a new peer with
1419 * prim_nid as its primary nid.
1421 if (nid == LNET_NID_ANY)
1422 return lnet_peer_add(prim_nid, flags);
1424 /* Look up the prim_nid, which must exist. */
1425 lpni = lnet_find_peer_ni_locked(prim_nid);
1428 lnet_peer_ni_decref_locked(lpni);
1429 lp = lpni->lpni_peer_net->lpn_peer;
1431 /* Peer must have been configured. */
1432 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1433 CDEBUG(D_NET, "peer %s was not configured\n",
1434 libcfs_nid2str(prim_nid));
1438 /* Primary NID must match */
1439 if (lp->lp_primary_nid != prim_nid) {
1440 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1441 libcfs_nid2str(prim_nid),
1442 libcfs_nid2str(lp->lp_primary_nid));
1446 /* Multi-Rail flag must match. */
1447 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1448 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1449 libcfs_nid2str(prim_nid));
1453 return lnet_peer_add_nid(lp, nid, flags);
1457 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1459 * This API handles the following combinations:
1460 * Delete a NI from a peer if both prim_nid and nid are provided.
1461 * Delete a peer if only prim_nid is provided.
1462 * Delete a peer if its primary nid is provided.
1464 * The caller must hold ln_api_mutex. This prevents the peer from
1465 * being modified/deleted by a different thread.
1468 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1470 struct lnet_peer *lp;
1471 struct lnet_peer_ni *lpni;
1474 if (prim_nid == LNET_NID_ANY)
1477 lpni = lnet_find_peer_ni_locked(prim_nid);
1480 lnet_peer_ni_decref_locked(lpni);
1481 lp = lpni->lpni_peer_net->lpn_peer;
1483 if (prim_nid != lp->lp_primary_nid) {
1484 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1485 libcfs_nid2str(prim_nid),
1486 libcfs_nid2str(lp->lp_primary_nid));
1490 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1491 return lnet_peer_del(lp);
1493 flags = LNET_PEER_CONFIGURED;
1494 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1495 flags |= LNET_PEER_MULTI_RAIL;
1497 return lnet_peer_del_nid(lp, nid, flags);
1501 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1503 struct lnet_peer_table *ptable;
1504 struct lnet_peer_net *lpn;
1506 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1508 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1509 LASSERT(lpni->lpni_rtr_refcount == 0);
1510 LASSERT(list_empty(&lpni->lpni_txq));
1511 LASSERT(lpni->lpni_txqnob == 0);
1513 lpn = lpni->lpni_peer_net;
1514 lpni->lpni_peer_net = NULL;
1515 lpni->lpni_net = NULL;
1517 /* remove the peer ni from the zombie list */
1518 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1519 spin_lock(&ptable->pt_zombie_lock);
1520 list_del_init(&lpni->lpni_hashlist);
1521 ptable->pt_zombies--;
1522 spin_unlock(&ptable->pt_zombie_lock);
1524 if (lpni->lpni_pref_nnids > 1) {
1525 LIBCFS_FREE(lpni->lpni_pref.nids,
1526 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1528 LIBCFS_FREE(lpni, sizeof(*lpni));
1530 lnet_peer_net_decref_locked(lpn);
1533 struct lnet_peer_ni *
1534 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1536 struct lnet_peer_ni *lpni = NULL;
1539 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1540 return ERR_PTR(-ESHUTDOWN);
1543 * find if a peer_ni already exists.
1544 * If so then just return that.
1546 lpni = lnet_find_peer_ni_locked(nid);
1550 lnet_net_unlock(cpt);
1552 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1555 goto out_net_relock;
1558 lpni = lnet_find_peer_ni_locked(nid);
1568 * Get a peer_ni for the given nid, create it if necessary. Takes a
1569 * hold on the peer_ni.
1571 struct lnet_peer_ni *
1572 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1574 struct lnet_peer_ni *lpni = NULL;
1577 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1578 return ERR_PTR(-ESHUTDOWN);
1581 * find if a peer_ni already exists.
1582 * If so then just return that.
1584 lpni = lnet_find_peer_ni_locked(nid);
1590 * use the lnet_api_mutex to serialize the creation of the peer_ni
1591 * and the creation/deletion of the local ni/net. When a local ni is
1592 * created, if there exists a set of peer_nis on that network,
1593 * they need to be traversed and updated. When a local NI is
1594 * deleted, which could result in a network being deleted, then
1595 * all peer nis on that network need to be removed as well.
1597 * Creation through traffic should also be serialized with
1598 * creation through DLC.
1600 lnet_net_unlock(cpt);
1601 mutex_lock(&the_lnet.ln_api_mutex);
1603 * Shutdown is only set under the ln_api_lock, so a single
1604 * check here is sufficent.
1606 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1607 lpni = ERR_PTR(-ESHUTDOWN);
1608 goto out_mutex_unlock;
1611 rc = lnet_peer_ni_traffic_add(nid, pref);
1614 goto out_mutex_unlock;
1617 lpni = lnet_find_peer_ni_locked(nid);
1621 mutex_unlock(&the_lnet.ln_api_mutex);
1624 /* Lock has been dropped, check again for shutdown. */
1625 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1627 lnet_peer_ni_decref_locked(lpni);
1628 lpni = ERR_PTR(-ESHUTDOWN);
1639 * Is a peer uptodate from the point of view of discovery?
1641 * If it is currently being processed, obviously not.
1642 * A forced Ping or Push is also handled by the discovery thread.
1644 * Otherwise look at whether the peer needs rediscovering.
1647 lnet_peer_is_uptodate(struct lnet_peer *lp)
1651 spin_lock(&lp->lp_lock);
1652 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1653 LNET_PEER_FORCE_PING |
1654 LNET_PEER_FORCE_PUSH)) {
1656 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1658 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1659 if (lnet_peer_discovery_disabled)
1663 } else if (lnet_peer_needs_push(lp)) {
1665 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1666 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1673 spin_unlock(&lp->lp_lock);
1679 * Queue a peer for the attention of the discovery thread. Call with
1680 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1681 * -EALREADY if the peer was already queued.
1683 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1687 spin_lock(&lp->lp_lock);
1688 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1689 lp->lp_state |= LNET_PEER_DISCOVERING;
1690 spin_unlock(&lp->lp_lock);
1691 if (list_empty(&lp->lp_dc_list)) {
1692 lnet_peer_addref_locked(lp);
1693 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1694 wake_up(&the_lnet.ln_dc_waitq);
1700 CDEBUG(D_NET, "Queue peer %s: %d\n",
1701 libcfs_nid2str(lp->lp_primary_nid), rc);
1707 * Discovery of a peer is complete. Wake all waiters on the peer.
1708 * Call with lnet_net_lock/EX held.
1710 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1712 struct lnet_msg *msg = NULL;
1714 struct list_head pending_msgs;
1716 INIT_LIST_HEAD(&pending_msgs);
1718 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1719 libcfs_nid2str(lp->lp_primary_nid));
1721 list_del_init(&lp->lp_dc_list);
1722 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1723 wake_up_all(&lp->lp_dc_waitq);
1725 lnet_net_unlock(LNET_LOCK_EX);
1727 /* iterate through all pending messages and send them again */
1728 list_for_each_entry(msg, &pending_msgs, msg_list) {
1729 if (lp->lp_dc_error) {
1730 lnet_finalize(msg, lp->lp_dc_error);
1734 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1735 lnet_msgtyp2str(msg->msg_type),
1736 libcfs_id2str(msg->msg_target));
1737 rc = lnet_send(msg->msg_src_nid_param, msg,
1738 msg->msg_rtr_nid_param);
1740 CNETERR("Error sending %s to %s: %d\n",
1741 lnet_msgtyp2str(msg->msg_type),
1742 libcfs_id2str(msg->msg_target), rc);
1743 lnet_finalize(msg, rc);
1746 lnet_net_lock(LNET_LOCK_EX);
1747 lnet_peer_decref_locked(lp);
1751 * Handle inbound push.
1752 * Like any event handler, called with lnet_res_lock/CPT held.
1754 void lnet_peer_push_event(struct lnet_event *ev)
1756 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1757 struct lnet_peer *lp;
1759 /* lnet_find_peer() adds a refcount */
1760 lp = lnet_find_peer(ev->source.nid);
1762 CERROR("Push Put from unknown %s (source %s)\n",
1763 libcfs_nid2str(ev->initiator.nid),
1764 libcfs_nid2str(ev->source.nid));
1768 /* Ensure peer state remains consistent while we modify it. */
1769 spin_lock(&lp->lp_lock);
1772 * If some kind of error happened the contents of the message
1773 * cannot be used. Clear the NIDS_UPTODATE and set the
1774 * FORCE_PING flag to trigger a ping.
1777 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1778 lp->lp_state |= LNET_PEER_FORCE_PING;
1779 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1781 libcfs_nid2str(lp->lp_primary_nid),
1782 libcfs_nid2str(ev->source.nid));
1787 * A push with invalid or corrupted info. Clear the UPTODATE
1788 * flag to trigger a ping.
1790 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1791 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1792 lp->lp_state |= LNET_PEER_FORCE_PING;
1793 CDEBUG(D_NET, "Corrupted Push from %s\n",
1794 libcfs_nid2str(lp->lp_primary_nid));
1799 * Make sure we'll allocate the correct size ping buffer when
1802 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1803 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1806 * A non-Multi-Rail peer is not supposed to be capable of
1809 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1810 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1811 libcfs_nid2str(lp->lp_primary_nid));
1816 * Check the MULTIRAIL flag. Complain if the peer was DLC
1817 * configured without it.
1819 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1820 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1821 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1822 libcfs_nid2str(lp->lp_primary_nid));
1824 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1825 lnet_peer_clr_non_mr_pref_nids(lp);
1830 * The peer may have discovery disabled at its end. Set
1831 * NO_DISCOVERY as appropriate.
1833 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1834 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1835 libcfs_nid2str(lp->lp_primary_nid));
1836 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1837 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1838 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1839 libcfs_nid2str(lp->lp_primary_nid));
1840 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1844 * Check for truncation of the Put message. Clear the
1845 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1846 * and tell discovery to allocate a bigger buffer.
1848 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1849 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1850 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1851 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1852 lp->lp_state |= LNET_PEER_FORCE_PING;
1853 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1854 libcfs_nid2str(lp->lp_primary_nid),
1855 pbuf->pb_info.pi_nnis);
1860 * Check whether the Put data is stale. Stale data can just be
1863 if (pbuf->pb_info.pi_nnis > 1 &&
1864 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1865 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1866 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1867 libcfs_nid2str(lp->lp_primary_nid),
1868 LNET_PING_BUFFER_SEQNO(pbuf),
1874 * Check whether the Put data is new, in which case we clear
1875 * the UPTODATE flag and prepare to process it.
1877 * If the Put data is current, and the peer is UPTODATE then
1878 * we assome everything is all right and drop the data as
1881 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1882 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1883 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1884 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1885 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1886 libcfs_nid2str(lp->lp_primary_nid),
1887 LNET_PING_BUFFER_SEQNO(pbuf),
1893 * If there is data present that hasn't been processed yet,
1894 * we'll replace it if the Put contained newer data and it
1895 * fits. We're racing with a Ping or earlier Push in this
1898 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1899 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1900 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1901 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1902 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1903 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1904 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1905 libcfs_nid2str(lp->lp_primary_nid),
1906 LNET_PING_BUFFER_SEQNO(pbuf),
1907 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1913 * Allocate a buffer to copy the data. On a failure we drop
1914 * the Push and set FORCE_PING to force the discovery
1915 * thread to fix the problem by pinging the peer.
1917 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
1919 lp->lp_state |= LNET_PEER_FORCE_PING;
1920 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
1921 libcfs_nid2str(lp->lp_primary_nid),
1922 LNET_PING_BUFFER_SEQNO(pbuf));
1927 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1928 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1929 lp->lp_state |= LNET_PEER_DATA_PRESENT;
1930 CDEBUG(D_NET, "Received Push %s %u\n",
1931 libcfs_nid2str(lp->lp_primary_nid),
1932 LNET_PING_BUFFER_SEQNO(pbuf));
1936 * Queue the peer for discovery, and wake the discovery thread
1937 * if the peer was already queued, because its status changed.
1939 spin_unlock(&lp->lp_lock);
1940 lnet_net_lock(LNET_LOCK_EX);
1941 if (lnet_peer_queue_for_discovery(lp))
1942 wake_up(&the_lnet.ln_dc_waitq);
1943 /* Drop refcount from lookup */
1944 lnet_peer_decref_locked(lp);
1945 lnet_net_unlock(LNET_LOCK_EX);
1949 * Clear the discovery error state, unless we're already discovering
1950 * this peer, in which case the error is current.
1952 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
1954 spin_lock(&lp->lp_lock);
1955 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1956 lp->lp_dc_error = 0;
1957 spin_unlock(&lp->lp_lock);
1961 * Peer discovery slow path. The ln_api_mutex is held on entry, and
1962 * dropped/retaken within this function. An lnet_peer_ni is passed in
1963 * because discovery could tear down an lnet_peer.
1966 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
1969 struct lnet_peer *lp;
1973 lnet_net_unlock(cpt);
1974 lnet_net_lock(LNET_LOCK_EX);
1975 lp = lpni->lpni_peer_net->lpn_peer;
1976 lnet_peer_clear_discovery_error(lp);
1979 * We're willing to be interrupted. The lpni can become a
1980 * zombie if we race with DLC, so we must check for that.
1983 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
1984 if (signal_pending(current))
1986 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
1988 if (lp->lp_dc_error)
1990 if (lnet_peer_is_uptodate(lp))
1992 lnet_peer_queue_for_discovery(lp);
1993 lnet_peer_addref_locked(lp);
1995 * if caller requested a non-blocking operation then
1996 * return immediately. Once discovery is complete then the
1997 * peer ref will be decremented and any pending messages
1998 * that were stopped due to discovery will be transmitted.
2002 lnet_net_unlock(LNET_LOCK_EX);
2004 finish_wait(&lp->lp_dc_waitq, &wait);
2005 lnet_net_lock(LNET_LOCK_EX);
2006 lnet_peer_decref_locked(lp);
2007 /* Peer may have changed */
2008 lp = lpni->lpni_peer_net->lpn_peer;
2010 finish_wait(&lp->lp_dc_waitq, &wait);
2012 lnet_net_unlock(LNET_LOCK_EX);
2015 if (signal_pending(current))
2017 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2019 else if (lp->lp_dc_error)
2020 rc = lp->lp_dc_error;
2022 CDEBUG(D_NET, "non-blocking discovery\n");
2023 else if (!lnet_peer_is_uptodate(lp))
2026 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2027 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2028 libcfs_nid2str(lpni->lpni_nid), rc,
2029 (!block) ? "pending discovery" : "discovery complete");
2034 /* Handle an incoming ack for a push. */
2036 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2038 struct lnet_ping_buffer *pbuf;
2040 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2041 spin_lock(&lp->lp_lock);
2042 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2043 lp->lp_push_error = ev->status;
2045 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2047 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2048 spin_unlock(&lp->lp_lock);
2050 CDEBUG(D_NET, "peer %s ev->status %d\n",
2051 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2054 /* Handle a Reply message. This is the reply to a Ping message. */
2056 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2058 struct lnet_ping_buffer *pbuf;
2061 spin_lock(&lp->lp_lock);
2064 * If some kind of error happened the contents of message
2065 * cannot be used. Set PING_FAILED to trigger a retry.
2068 lp->lp_state |= LNET_PEER_PING_FAILED;
2069 lp->lp_ping_error = ev->status;
2070 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2072 libcfs_nid2str(lp->lp_primary_nid),
2073 libcfs_nid2str(ev->source.nid));
2077 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2078 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2079 lnet_swap_pinginfo(pbuf);
2082 * A reply with invalid or corrupted info. Set PING_FAILED to
2085 rc = lnet_ping_info_validate(&pbuf->pb_info);
2087 lp->lp_state |= LNET_PEER_PING_FAILED;
2088 lp->lp_ping_error = 0;
2089 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2090 libcfs_nid2str(lp->lp_primary_nid), rc);
2095 * Update the MULTI_RAIL flag based on the reply. If the peer
2096 * was configured with DLC then the setting should match what
2099 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2100 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2101 /* Everything's fine */
2102 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2103 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2104 libcfs_nid2str(lp->lp_primary_nid));
2106 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2107 lnet_peer_clr_non_mr_pref_nids(lp);
2109 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2110 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2111 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2112 libcfs_nid2str(lp->lp_primary_nid));
2114 CERROR("Multi-Rail state vanished from %s\n",
2115 libcfs_nid2str(lp->lp_primary_nid));
2116 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2121 * Make sure we'll allocate the correct size ping buffer when
2124 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2125 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2128 * The peer may have discovery disabled at its end. Set
2129 * NO_DISCOVERY as appropriate.
2131 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2132 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2133 libcfs_nid2str(lp->lp_primary_nid));
2134 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2135 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2136 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2137 libcfs_nid2str(lp->lp_primary_nid));
2138 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2142 * Check for truncation of the Reply. Clear PING_SENT and set
2143 * PING_FAILED to trigger a retry.
2145 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2146 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2147 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2148 lp->lp_state |= LNET_PEER_PING_FAILED;
2149 lp->lp_ping_error = 0;
2150 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2151 libcfs_nid2str(lp->lp_primary_nid),
2152 pbuf->pb_info.pi_nnis);
2157 * Check the sequence numbers in the reply. These are only
2158 * available if the reply came from a Multi-Rail peer.
2160 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2161 pbuf->pb_info.pi_nnis > 1 &&
2162 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2163 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2164 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2165 libcfs_nid2str(lp->lp_primary_nid),
2166 LNET_PING_BUFFER_SEQNO(pbuf),
2171 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2172 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2175 /* We're happy with the state of the data in the buffer. */
2176 CDEBUG(D_NET, "peer %s data present %u\n",
2177 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2178 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2179 lnet_ping_buffer_decref(lp->lp_data);
2181 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2182 lnet_ping_buffer_addref(pbuf);
2185 lp->lp_state &= ~LNET_PEER_PING_SENT;
2186 spin_unlock(&lp->lp_lock);
2190 * Send event handling. Only matters for error cases, where we clean
2191 * up state on the peer and peer_ni that would otherwise be updated in
2192 * the REPLY event handler for a successful Ping, and the ACK event
2193 * handler for a successful Push.
2196 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2203 spin_lock(&lp->lp_lock);
2204 if (ev->msg_type == LNET_MSG_GET) {
2205 lp->lp_state &= ~LNET_PEER_PING_SENT;
2206 lp->lp_state |= LNET_PEER_PING_FAILED;
2207 lp->lp_ping_error = ev->status;
2208 } else { /* ev->msg_type == LNET_MSG_PUT */
2209 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2210 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2211 lp->lp_push_error = ev->status;
2213 spin_unlock(&lp->lp_lock);
2214 rc = LNET_REDISCOVER_PEER;
2216 CDEBUG(D_NET, "%s Send to %s: %d\n",
2217 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2218 libcfs_nid2str(ev->target.nid), rc);
2223 * Unlink event handling. This event is only seen if a call to
2224 * LNetMDUnlink() caused the event to be unlinked. If this call was
2225 * made after the event was set up in LNetGet() or LNetPut() then we
2226 * assume the Ping or Push timed out.
2229 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2231 spin_lock(&lp->lp_lock);
2232 /* We've passed through LNetGet() */
2233 if (lp->lp_state & LNET_PEER_PING_SENT) {
2234 lp->lp_state &= ~LNET_PEER_PING_SENT;
2235 lp->lp_state |= LNET_PEER_PING_FAILED;
2236 lp->lp_ping_error = -ETIMEDOUT;
2237 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2238 libcfs_nid2str(lp->lp_primary_nid));
2240 /* We've passed through LNetPut() */
2241 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2242 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2243 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2244 lp->lp_push_error = -ETIMEDOUT;
2245 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2246 libcfs_nid2str(lp->lp_primary_nid));
2248 spin_unlock(&lp->lp_lock);
2252 * Event handler for the discovery EQ.
2254 * Called with lnet_res_lock(cpt) held. The cpt is the
2255 * lnet_cpt_of_cookie() of the md handle cookie.
2257 static void lnet_discovery_event_handler(lnet_event_t *event)
2259 struct lnet_peer *lp = event->md.user_ptr;
2260 struct lnet_ping_buffer *pbuf;
2263 /* discovery needs to take another look */
2264 rc = LNET_REDISCOVER_PEER;
2266 CDEBUG(D_NET, "Received event: %d\n", event->type);
2268 switch (event->type) {
2269 case LNET_EVENT_ACK:
2270 lnet_discovery_event_ack(lp, event);
2272 case LNET_EVENT_REPLY:
2273 lnet_discovery_event_reply(lp, event);
2275 case LNET_EVENT_SEND:
2276 /* Only send failure triggers a retry. */
2277 rc = lnet_discovery_event_send(lp, event);
2279 case LNET_EVENT_UNLINK:
2280 /* LNetMDUnlink() was called */
2281 lnet_discovery_event_unlink(lp, event);
2284 /* Invalid events. */
2287 lnet_net_lock(LNET_LOCK_EX);
2288 if (event->unlinked) {
2289 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2290 lnet_ping_buffer_decref(pbuf);
2291 lnet_peer_decref_locked(lp);
2293 if (rc == LNET_REDISCOVER_PEER) {
2294 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2295 wake_up(&the_lnet.ln_dc_waitq);
2297 lnet_net_unlock(LNET_LOCK_EX);
2301 * Build a peer from incoming data.
2303 * The NIDs in the incoming data are supposed to be structured as follows:
2306 * - other NIDs in same net
2307 * - NIDs in second net
2308 * - NIDs in third net
2310 * This due to the way the list of NIDs in the data is created.
2312 * Note that this function will mark the peer uptodate unless an
2313 * ENOMEM is encontered. All other errors are due to a conflict
2314 * between the DLC configuration and what discovery sees. We treat DLC
2315 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2316 * peer from becoming stuck in discovery.
2318 static int lnet_peer_merge_data(struct lnet_peer *lp,
2319 struct lnet_ping_buffer *pbuf)
2321 struct lnet_peer_ni *lpni;
2322 lnet_nid_t *curnis = NULL;
2323 lnet_nid_t *addnis = NULL;
2324 lnet_nid_t *delnis = NULL;
2334 flags = LNET_PEER_DISCOVERED;
2335 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2336 flags |= LNET_PEER_MULTI_RAIL;
2338 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2339 LIBCFS_ALLOC(curnis, nnis * sizeof(lnet_nid_t));
2340 LIBCFS_ALLOC(addnis, nnis * sizeof(lnet_nid_t));
2341 LIBCFS_ALLOC(delnis, nnis * sizeof(lnet_nid_t));
2342 if (!curnis || !addnis || !delnis) {
2350 /* Construct the list of NIDs present in peer. */
2352 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2353 curnis[ncurnis++] = lpni->lpni_nid;
2356 * Check for NIDs in pbuf not present in curnis[].
2357 * The loop starts at 1 to skip the loopback NID.
2359 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2360 for (j = 0; j < ncurnis; j++)
2361 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2364 addnis[naddnis++] = pbuf->pb_info.pi_ni[i].ns_nid;
2367 * Check for NIDs in curnis[] not present in pbuf.
2368 * The nested loop starts at 1 to skip the loopback NID.
2370 * But never add the loopback NID to delnis[]: if it is
2371 * present in curnis[] then this peer is for this node.
2373 for (i = 0; i < ncurnis; i++) {
2374 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2376 for (j = 1; j < pbuf->pb_info.pi_nnis; j++)
2377 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid)
2379 if (j == pbuf->pb_info.pi_nnis)
2380 delnis[ndelnis++] = curnis[i];
2383 for (i = 0; i < naddnis; i++) {
2384 rc = lnet_peer_add_nid(lp, addnis[i], flags);
2386 CERROR("Error adding NID %s to peer %s: %d\n",
2387 libcfs_nid2str(addnis[i]),
2388 libcfs_nid2str(lp->lp_primary_nid), rc);
2393 for (i = 0; i < ndelnis; i++) {
2394 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2396 CERROR("Error deleting NID %s from peer %s: %d\n",
2397 libcfs_nid2str(delnis[i]),
2398 libcfs_nid2str(lp->lp_primary_nid), rc);
2404 * Errors other than -ENOMEM are due to peers having been
2405 * configured with DLC. Ignore these because DLC overrides
2410 LIBCFS_FREE(curnis, nnis * sizeof(lnet_nid_t));
2411 LIBCFS_FREE(addnis, nnis * sizeof(lnet_nid_t));
2412 LIBCFS_FREE(delnis, nnis * sizeof(lnet_nid_t));
2413 lnet_ping_buffer_decref(pbuf);
2414 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2417 spin_lock(&lp->lp_lock);
2418 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2419 lp->lp_state |= LNET_PEER_FORCE_PING;
2420 spin_unlock(&lp->lp_lock);
2426 * The data in pbuf says lp is its primary peer, but the data was
2427 * received by a different peer. Try to update lp with the data.
2430 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2432 lnet_handle_md_t mdh;
2434 /* Queue lp for discovery, and force it on the request queue. */
2435 lnet_net_lock(LNET_LOCK_EX);
2436 if (lnet_peer_queue_for_discovery(lp))
2437 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2438 lnet_net_unlock(LNET_LOCK_EX);
2440 LNetInvalidateMDHandle(&mdh);
2443 * Decide whether we can move the peer to the DATA_PRESENT state.
2445 * We replace stale data for a multi-rail peer, repair PING_FAILED
2446 * status, and preempt FORCE_PING.
2448 * If after that we have DATA_PRESENT, we merge it into this peer.
2450 spin_lock(&lp->lp_lock);
2451 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2452 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2453 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2454 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2455 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2456 lnet_ping_buffer_decref(pbuf);
2461 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2462 lnet_ping_buffer_decref(lp->lp_data);
2464 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2466 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2467 mdh = lp->lp_ping_mdh;
2468 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2469 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2470 lp->lp_ping_error = 0;
2472 if (lp->lp_state & LNET_PEER_FORCE_PING)
2473 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2474 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2475 spin_unlock(&lp->lp_lock);
2477 if (!LNetMDHandleIsInvalid(mdh))
2481 return lnet_peer_merge_data(lp, pbuf);
2483 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2488 * Update a peer using the data received.
2490 static int lnet_peer_data_present(struct lnet_peer *lp)
2491 __must_hold(&lp->lp_lock)
2493 struct lnet_ping_buffer *pbuf;
2494 struct lnet_peer_ni *lpni;
2495 lnet_nid_t nid = LNET_NID_ANY;
2501 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2502 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2503 spin_unlock(&lp->lp_lock);
2506 * Modifications of peer structures are done while holding the
2507 * ln_api_mutex. A global lock is required because we may be
2508 * modifying multiple peer structures, and a mutex greatly
2509 * simplifies memory management.
2511 * The actual changes to the data structures must also protect
2512 * against concurrent lookups, for which the lnet_net_lock in
2513 * LNET_LOCK_EX mode is used.
2515 mutex_lock(&the_lnet.ln_api_mutex);
2516 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2522 * If this peer is not on the peer list then it is being torn
2523 * down, and our reference count may be all that is keeping it
2524 * alive. Don't do any work on it.
2526 if (list_empty(&lp->lp_peer_list))
2529 flags = LNET_PEER_DISCOVERED;
2530 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2531 flags |= LNET_PEER_MULTI_RAIL;
2534 * Check whether the primary NID in the message matches the
2535 * primary NID of the peer. If it does, update the peer, if
2536 * it it does not, check whether there is already a peer with
2537 * that primary NID. If no such peer exists, try to update
2538 * the primary NID of the current peer (allowed if it was
2539 * created due to message traffic) and complete the update.
2540 * If the peer did exist, hand off the data to it.
2542 * The peer for the loopback interface is a special case: this
2543 * is the peer for the local node, and we want to set its
2544 * primary NID to the correct value here.
2546 if (pbuf->pb_info.pi_nnis > 1)
2547 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2548 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2549 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2551 rc = lnet_peer_merge_data(lp, pbuf);
2552 } else if (lp->lp_primary_nid == nid) {
2553 rc = lnet_peer_merge_data(lp, pbuf);
2555 lpni = lnet_find_peer_ni_locked(nid);
2557 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2559 CERROR("Primary NID error %s versus %s: %d\n",
2560 libcfs_nid2str(lp->lp_primary_nid),
2561 libcfs_nid2str(nid), rc);
2563 rc = lnet_peer_merge_data(lp, pbuf);
2566 rc = lnet_peer_set_primary_data(
2567 lpni->lpni_peer_net->lpn_peer, pbuf);
2568 lnet_peer_ni_decref_locked(lpni);
2572 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2573 mutex_unlock(&the_lnet.ln_api_mutex);
2575 spin_lock(&lp->lp_lock);
2576 /* Tell discovery to re-check the peer immediately. */
2578 rc = LNET_REDISCOVER_PEER;
2583 * A ping failed. Clear the PING_FAILED state and set the
2584 * FORCE_PING state, to ensure a retry even if discovery is
2585 * disabled. This avoids being left with incorrect state.
2587 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2588 __must_hold(&lp->lp_lock)
2590 lnet_handle_md_t mdh;
2593 mdh = lp->lp_ping_mdh;
2594 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2595 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2596 lp->lp_state |= LNET_PEER_FORCE_PING;
2597 rc = lp->lp_ping_error;
2598 lp->lp_ping_error = 0;
2599 spin_unlock(&lp->lp_lock);
2601 if (!LNetMDHandleIsInvalid(mdh))
2604 CDEBUG(D_NET, "peer %s:%d\n",
2605 libcfs_nid2str(lp->lp_primary_nid), rc);
2607 spin_lock(&lp->lp_lock);
2608 return rc ? rc : LNET_REDISCOVER_PEER;
2612 * Select NID to send a Ping or Push to.
2614 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2616 struct lnet_peer_ni *lpni;
2618 /* Look for a direct-connected NID for this peer. */
2620 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2621 if (!lnet_is_peer_ni_healthy_locked(lpni))
2623 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2628 return lpni->lpni_nid;
2630 /* Look for a routed-connected NID for this peer. */
2632 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2633 if (!lnet_is_peer_ni_healthy_locked(lpni))
2635 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2640 return lpni->lpni_nid;
2642 return LNET_NID_ANY;
2645 /* Active side of ping. */
2646 static int lnet_peer_send_ping(struct lnet_peer *lp)
2647 __must_hold(&lp->lp_lock)
2649 lnet_md_t md = { NULL };
2650 lnet_process_id_t id;
2651 struct lnet_ping_buffer *pbuf;
2656 lp->lp_state |= LNET_PEER_PING_SENT;
2657 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2658 spin_unlock(&lp->lp_lock);
2660 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2661 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
2667 /* initialize md content */
2668 md.start = &pbuf->pb_info;
2669 md.length = LNET_PING_INFO_SIZE(nnis);
2670 md.threshold = 2; /* GET/REPLY */
2672 md.options = LNET_MD_TRUNCATE;
2674 md.eq_handle = the_lnet.ln_dc_eqh;
2676 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_ping_mdh);
2678 lnet_ping_buffer_decref(pbuf);
2679 CERROR("Can't bind MD: %d\n", rc);
2682 cpt = lnet_net_lock_current();
2683 /* Refcount for MD. */
2684 lnet_peer_addref_locked(lp);
2685 id.pid = LNET_PID_LUSTRE;
2686 id.nid = lnet_peer_select_nid(lp);
2687 lnet_net_unlock(cpt);
2689 if (id.nid == LNET_NID_ANY) {
2691 goto fail_unlink_md;
2694 rc = LNetGet(LNET_NID_ANY, lp->lp_ping_mdh, id,
2695 LNET_RESERVED_PORTAL,
2696 LNET_PROTO_PING_MATCHBITS, 0);
2699 goto fail_unlink_md;
2701 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2703 spin_lock(&lp->lp_lock);
2707 LNetMDUnlink(lp->lp_ping_mdh);
2708 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2710 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2712 * The errors that get us here are considered hard errors and
2713 * cause Discovery to terminate. So we clear PING_SENT, but do
2714 * not set either PING_FAILED or FORCE_PING. In fact we need
2715 * to clear PING_FAILED, because the unlink event handler will
2716 * have set it if we called LNetMDUnlink() above.
2718 spin_lock(&lp->lp_lock);
2719 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2724 * This function exists because you cannot call LNetMDUnlink() from an
2727 static int lnet_peer_push_failed(struct lnet_peer *lp)
2728 __must_hold(&lp->lp_lock)
2730 lnet_handle_md_t mdh;
2733 mdh = lp->lp_push_mdh;
2734 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2735 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2736 rc = lp->lp_push_error;
2737 lp->lp_push_error = 0;
2738 spin_unlock(&lp->lp_lock);
2740 if (!LNetMDHandleIsInvalid(mdh))
2743 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2744 spin_lock(&lp->lp_lock);
2745 return rc ? rc : LNET_REDISCOVER_PEER;
2748 /* Active side of push. */
2749 static int lnet_peer_send_push(struct lnet_peer *lp)
2750 __must_hold(&lp->lp_lock)
2752 struct lnet_ping_buffer *pbuf;
2753 lnet_process_id_t id;
2758 /* Don't push to a non-multi-rail peer. */
2759 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2760 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2764 lp->lp_state |= LNET_PEER_PUSH_SENT;
2765 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2766 spin_unlock(&lp->lp_lock);
2768 cpt = lnet_net_lock_current();
2769 pbuf = the_lnet.ln_ping_target;
2770 lnet_ping_buffer_addref(pbuf);
2771 lnet_net_unlock(cpt);
2773 /* Push source MD */
2774 md.start = &pbuf->pb_info;
2775 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2776 md.threshold = 2; /* Put/Ack */
2779 md.eq_handle = the_lnet.ln_dc_eqh;
2782 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2784 lnet_ping_buffer_decref(pbuf);
2785 CERROR("Can't bind push source MD: %d\n", rc);
2788 cpt = lnet_net_lock_current();
2789 /* Refcount for MD. */
2790 lnet_peer_addref_locked(lp);
2791 id.pid = LNET_PID_LUSTRE;
2792 id.nid = lnet_peer_select_nid(lp);
2793 lnet_net_unlock(cpt);
2795 if (id.nid == LNET_NID_ANY) {
2800 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2801 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2802 LNET_PROTO_PING_MATCHBITS, 0, 0);
2807 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2809 spin_lock(&lp->lp_lock);
2813 LNetMDUnlink(lp->lp_push_mdh);
2814 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2816 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2818 * The errors that get us here are considered hard errors and
2819 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2820 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2821 * because the unlink event handler will have set it if we
2822 * called LNetMDUnlink() above.
2824 spin_lock(&lp->lp_lock);
2825 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2830 * An unrecoverable error was encountered during discovery.
2831 * Set error status in peer and abort discovery.
2833 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2835 CDEBUG(D_NET, "Discovery error %s: %d\n",
2836 libcfs_nid2str(lp->lp_primary_nid), error);
2838 spin_lock(&lp->lp_lock);
2839 lp->lp_dc_error = error;
2840 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2841 lp->lp_state |= LNET_PEER_REDISCOVER;
2842 spin_unlock(&lp->lp_lock);
2846 * Mark the peer as discovered.
2848 static int lnet_peer_discovered(struct lnet_peer *lp)
2849 __must_hold(&lp->lp_lock)
2851 lp->lp_state |= LNET_PEER_DISCOVERED;
2852 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2853 LNET_PEER_REDISCOVER);
2855 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2861 * Mark the peer as to be rediscovered.
2863 static int lnet_peer_rediscover(struct lnet_peer *lp)
2864 __must_hold(&lp->lp_lock)
2866 lp->lp_state |= LNET_PEER_REDISCOVER;
2867 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2869 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2875 * Returns the first peer on the ln_dc_working queue if its timeout
2876 * has expired. Takes the current time as an argument so as to not
2877 * obsessively re-check the clock. The oldest discovery request will
2878 * be at the head of the queue.
2880 static struct lnet_peer *lnet_peer_dc_timed_out(time64_t now)
2882 struct lnet_peer *lp;
2884 if (list_empty(&the_lnet.ln_dc_working))
2886 lp = list_first_entry(&the_lnet.ln_dc_working,
2887 struct lnet_peer, lp_dc_list);
2888 if (now < lp->lp_last_queued + DISCOVERY_TIMEOUT)
2894 * Discovering this peer is taking too long. Cancel any Ping or Push
2895 * that discovery is waiting on by unlinking the relevant MDs. The
2896 * lnet_discovery_event_handler() will proceed from here and complete
2899 static void lnet_peer_discovery_timeout(struct lnet_peer *lp)
2901 struct lnet_handle_md ping_mdh;
2902 struct lnet_handle_md push_mdh;
2904 LNetInvalidateMDHandle(&ping_mdh);
2905 LNetInvalidateMDHandle(&push_mdh);
2907 spin_lock(&lp->lp_lock);
2908 if (lp->lp_state & LNET_PEER_PING_SENT) {
2909 ping_mdh = lp->lp_ping_mdh;
2910 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2912 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2913 push_mdh = lp->lp_push_mdh;
2914 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2916 spin_unlock(&lp->lp_lock);
2918 if (!LNetMDHandleIsInvalid(ping_mdh))
2919 LNetMDUnlink(ping_mdh);
2920 if (!LNetMDHandleIsInvalid(push_mdh))
2921 LNetMDUnlink(push_mdh);
2925 * Wait for work to be queued or some other change that must be
2926 * attended to. Returns non-zero if the discovery thread should shut
2929 static int lnet_peer_discovery_wait_for_work(void)
2936 cpt = lnet_net_lock_current();
2938 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
2939 TASK_INTERRUPTIBLE);
2940 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2942 if (lnet_push_target_resize_needed())
2944 if (!list_empty(&the_lnet.ln_dc_request))
2946 if (lnet_peer_dc_timed_out(ktime_get_real_seconds()))
2948 lnet_net_unlock(cpt);
2951 * wakeup max every second to check if there are peers that
2952 * have been stuck on the working queue for greater than
2955 schedule_timeout(cfs_time_seconds(1));
2956 finish_wait(&the_lnet.ln_dc_waitq, &wait);
2957 cpt = lnet_net_lock_current();
2959 finish_wait(&the_lnet.ln_dc_waitq, &wait);
2961 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2964 lnet_net_unlock(cpt);
2966 CDEBUG(D_NET, "woken: %d\n", rc);
2971 /* The discovery thread. */
2972 static int lnet_peer_discovery(void *arg)
2974 struct lnet_peer *lp;
2978 CDEBUG(D_NET, "started\n");
2979 cfs_block_allsigs();
2982 if (lnet_peer_discovery_wait_for_work())
2985 if (lnet_push_target_resize_needed())
2986 lnet_push_target_resize();
2988 lnet_net_lock(LNET_LOCK_EX);
2989 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2993 * Process all incoming discovery work requests. When
2994 * discovery must wait on a peer to change state, it
2995 * is added to the tail of the ln_dc_working queue. A
2996 * timestamp keeps track of when the peer was added,
2997 * so we can time out discovery requests that take too
3000 while (!list_empty(&the_lnet.ln_dc_request)) {
3001 lp = list_first_entry(&the_lnet.ln_dc_request,
3002 struct lnet_peer, lp_dc_list);
3003 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3005 * set the time the peer was put on the dc_working
3006 * queue. It shouldn't remain on the queue
3007 * forever, in case the GET message (for ping)
3008 * doesn't get a REPLY or the PUT message (for
3009 * push) doesn't get an ACK.
3011 * TODO: LNet Health will deal with this scenario
3014 lp->lp_last_queued = ktime_get_real_seconds();
3015 lnet_net_unlock(LNET_LOCK_EX);
3018 * Select an action depending on the state of
3019 * the peer and whether discovery is disabled.
3020 * The check whether discovery is disabled is
3021 * done after the code that handles processing
3022 * for arrived data, cleanup for failures, and
3023 * forcing a Ping or Push.
3025 spin_lock(&lp->lp_lock);
3026 CDEBUG(D_NET, "peer %s state %#x\n",
3027 libcfs_nid2str(lp->lp_primary_nid),
3029 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3030 rc = lnet_peer_data_present(lp);
3031 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3032 rc = lnet_peer_ping_failed(lp);
3033 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3034 rc = lnet_peer_push_failed(lp);
3035 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3036 rc = lnet_peer_send_ping(lp);
3037 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3038 rc = lnet_peer_send_push(lp);
3039 else if (lnet_peer_discovery_disabled)
3040 rc = lnet_peer_rediscover(lp);
3041 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3042 rc = lnet_peer_send_ping(lp);
3043 else if (lnet_peer_needs_push(lp))
3044 rc = lnet_peer_send_push(lp);
3046 rc = lnet_peer_discovered(lp);
3047 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3048 libcfs_nid2str(lp->lp_primary_nid),
3050 spin_unlock(&lp->lp_lock);
3052 lnet_net_lock(LNET_LOCK_EX);
3053 if (rc == LNET_REDISCOVER_PEER) {
3054 list_move(&lp->lp_dc_list,
3055 &the_lnet.ln_dc_request);
3057 lnet_peer_discovery_error(lp, rc);
3059 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3060 lnet_peer_discovery_complete(lp);
3061 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3066 * Now that the ln_dc_request queue has been emptied
3067 * check the ln_dc_working queue for peers that are
3068 * taking too long. Move all that are found to the
3069 * ln_dc_expired queue and time out any pending
3070 * Ping or Push. We have to drop the lnet_net_lock
3071 * in the loop because lnet_peer_discovery_timeout()
3072 * calls LNetMDUnlink().
3074 now = ktime_get_real_seconds();
3075 while ((lp = lnet_peer_dc_timed_out(now)) != NULL) {
3076 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3077 lnet_net_unlock(LNET_LOCK_EX);
3078 lnet_peer_discovery_timeout(lp);
3079 lnet_net_lock(LNET_LOCK_EX);
3082 lnet_net_unlock(LNET_LOCK_EX);
3085 CDEBUG(D_NET, "stopping\n");
3087 * Clean up before telling lnet_peer_discovery_stop() that
3088 * we're done. Use wake_up() below to somewhat reduce the
3089 * size of the thundering herd if there are multiple threads
3090 * waiting on discovery of a single peer.
3092 LNetEQFree(the_lnet.ln_dc_eqh);
3093 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3095 /* Queue cleanup 1: stop all pending pings and pushes. */
3096 lnet_net_lock(LNET_LOCK_EX);
3097 while (!list_empty(&the_lnet.ln_dc_working)) {
3098 lp = list_first_entry(&the_lnet.ln_dc_working,
3099 struct lnet_peer, lp_dc_list);
3100 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3101 lnet_net_unlock(LNET_LOCK_EX);
3102 lnet_peer_discovery_timeout(lp);
3103 lnet_net_lock(LNET_LOCK_EX);
3105 lnet_net_unlock(LNET_LOCK_EX);
3107 /* Queue cleanup 2: wait for the expired queue to clear. */
3108 while (!list_empty(&the_lnet.ln_dc_expired))
3109 schedule_timeout(cfs_time_seconds(1));
3111 /* Queue cleanup 3: clear the request queue. */
3112 lnet_net_lock(LNET_LOCK_EX);
3113 while (!list_empty(&the_lnet.ln_dc_request)) {
3114 lp = list_first_entry(&the_lnet.ln_dc_request,
3115 struct lnet_peer, lp_dc_list);
3116 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3117 lnet_peer_discovery_complete(lp);
3119 lnet_net_unlock(LNET_LOCK_EX);
3121 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3122 wake_up(&the_lnet.ln_dc_waitq);
3124 CDEBUG(D_NET, "stopped\n");
3129 /* ln_api_mutex is held on entry. */
3130 int lnet_peer_discovery_start(void)
3132 struct task_struct *task;
3135 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3138 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3140 CERROR("Can't allocate discovery EQ: %d\n", rc);
3144 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3145 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3148 CERROR("Can't start peer discovery thread: %d\n", rc);
3150 LNetEQFree(the_lnet.ln_dc_eqh);
3151 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3153 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3156 CDEBUG(D_NET, "discovery start: %d\n", rc);
3161 /* ln_api_mutex is held on entry. */
3162 void lnet_peer_discovery_stop(void)
3164 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3167 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3168 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3169 wake_up(&the_lnet.ln_dc_waitq);
3171 wait_event(the_lnet.ln_dc_waitq,
3172 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3174 LASSERT(list_empty(&the_lnet.ln_dc_request));
3175 LASSERT(list_empty(&the_lnet.ln_dc_working));
3176 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3178 CDEBUG(D_NET, "discovery stopped\n");
3184 lnet_debug_peer(lnet_nid_t nid)
3186 char *aliveness = "NA";
3187 struct lnet_peer_ni *lp;
3190 cpt = lnet_cpt_of_nid(nid, NULL);
3193 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3195 lnet_net_unlock(cpt);
3196 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3200 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3201 aliveness = lp->lpni_alive ? "up" : "down";
3203 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3204 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3205 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3206 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3207 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3209 lnet_peer_ni_decref_locked(lp);
3211 lnet_net_unlock(cpt);
3214 /* Gathering information for userspace. */
3216 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3217 char aliveness[LNET_MAX_STR_LEN],
3218 __u32 *cpt_iter, __u32 *refcount,
3219 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3220 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3221 __u32 *peer_tx_qnob)
3223 struct lnet_peer_table *peer_table;
3224 struct lnet_peer_ni *lp;
3229 /* get the number of CPTs */
3230 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3232 /* if the cpt number to be examined is >= the number of cpts in
3233 * the system then indicate that there are no more cpts to examin
3235 if (*cpt_iter >= lncpt)
3238 /* get the current table */
3239 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3240 /* if the ptable is NULL then there are no more cpts to examine */
3241 if (peer_table == NULL)
3244 lnet_net_lock(*cpt_iter);
3246 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3247 struct list_head *peers = &peer_table->pt_hash[j];
3249 list_for_each_entry(lp, peers, lpni_hashlist) {
3250 if (peer_index-- > 0)
3253 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3254 if (lnet_isrouter(lp) ||
3255 lnet_peer_aliveness_enabled(lp))
3256 snprintf(aliveness, LNET_MAX_STR_LEN,
3257 lp->lpni_alive ? "up" : "down");
3259 *nid = lp->lpni_nid;
3260 *refcount = atomic_read(&lp->lpni_refcount);
3261 *ni_peer_tx_credits =
3262 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3263 *peer_tx_credits = lp->lpni_txcredits;
3264 *peer_rtr_credits = lp->lpni_rtrcredits;
3265 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3266 *peer_tx_qnob = lp->lpni_txqnob;
3272 lnet_net_unlock(*cpt_iter);
3276 return found ? 0 : -ENOENT;
3279 /* ln_api_mutex is held, which keeps the peer list stable */
3280 int lnet_get_peer_info(__u32 idx, lnet_nid_t *primary_nid, lnet_nid_t *nid,
3282 struct lnet_peer_ni_credit_info __user *peer_ni_info,
3283 struct lnet_ioctl_element_stats __user *peer_ni_stats)
3285 struct lnet_peer_ni *lpni = NULL;
3286 struct lnet_peer_net *lpn = NULL;
3287 struct lnet_peer *lp = NULL;
3288 struct lnet_peer_ni_credit_info ni_info;
3289 struct lnet_ioctl_element_stats ni_stats;
3292 lpni = lnet_get_peer_ni_idx_locked(idx, &lpn, &lp);
3297 *primary_nid = lp->lp_primary_nid;
3298 *mr = lnet_peer_is_multi_rail(lp);
3299 *nid = lpni->lpni_nid;
3300 snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN, "NA");
3301 if (lnet_isrouter(lpni) ||
3302 lnet_peer_aliveness_enabled(lpni))
3303 snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN,
3304 lpni->lpni_alive ? "up" : "down");
3306 ni_info.cr_refcount = atomic_read(&lpni->lpni_refcount);
3307 ni_info.cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3308 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3309 ni_info.cr_peer_tx_credits = lpni->lpni_txcredits;
3310 ni_info.cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3311 ni_info.cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3312 ni_info.cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3313 ni_info.cr_peer_tx_qnob = lpni->lpni_txqnob;
3314 ni_info.cr_ncpt = lpni->lpni_cpt;
3316 ni_stats.iel_send_count = atomic_read(&lpni->lpni_stats.send_count);
3317 ni_stats.iel_recv_count = atomic_read(&lpni->lpni_stats.recv_count);
3318 ni_stats.iel_drop_count = atomic_read(&lpni->lpni_stats.drop_count);
3320 /* If copy_to_user fails */
3322 if (copy_to_user(peer_ni_info, &ni_info, sizeof(ni_info)))
3325 if (copy_to_user(peer_ni_stats, &ni_stats, sizeof(ni_stats)))