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, 2017, 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 <linux/sched.h>
38 #ifdef HAVE_SCHED_HEADERS
39 #include <linux/sched/signal.h>
41 #include <linux/uaccess.h>
43 #include <lnet/udsp.h>
44 #include <lnet/lib-lnet.h>
45 #include <uapi/linux/lnet/lnet-dlc.h>
47 /* Value indicating that recovery needs to re-check a peer immediately. */
48 #define LNET_REDISCOVER_PEER (1)
50 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
53 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
55 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
56 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
57 lnet_peer_ni_decref_locked(lpni);
62 lnet_peer_net_added(struct lnet_net *net)
64 struct lnet_peer_ni *lpni, *tmp;
66 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
67 lpni_on_remote_peer_ni_list) {
69 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
72 spin_lock(&lpni->lpni_lock);
73 lpni->lpni_txcredits =
74 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
75 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
76 lpni->lpni_rtrcredits =
77 lnet_peer_buffer_credits(lpni->lpni_net);
78 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
79 spin_unlock(&lpni->lpni_lock);
81 lnet_peer_remove_from_remote_list(lpni);
87 lnet_peer_tables_destroy(void)
89 struct lnet_peer_table *ptable;
90 struct list_head *hash;
94 if (!the_lnet.ln_peer_tables)
97 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
98 hash = ptable->pt_hash;
99 if (!hash) /* not intialized */
102 LASSERT(list_empty(&ptable->pt_zombie_list));
104 ptable->pt_hash = NULL;
105 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
106 LASSERT(list_empty(&hash[j]));
108 CFS_FREE_PTR_ARRAY(hash, LNET_PEER_HASH_SIZE);
111 cfs_percpt_free(the_lnet.ln_peer_tables);
112 the_lnet.ln_peer_tables = NULL;
116 lnet_peer_tables_create(void)
118 struct lnet_peer_table *ptable;
119 struct list_head *hash;
123 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
125 if (the_lnet.ln_peer_tables == NULL) {
126 CERROR("Failed to allocate cpu-partition peer tables\n");
130 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
131 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
132 LNET_PEER_HASH_SIZE * sizeof(*hash));
134 CERROR("Failed to create peer hash table\n");
135 lnet_peer_tables_destroy();
139 spin_lock_init(&ptable->pt_zombie_lock);
140 INIT_LIST_HEAD(&ptable->pt_zombie_list);
142 INIT_LIST_HEAD(&ptable->pt_peer_list);
144 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
145 INIT_LIST_HEAD(&hash[j]);
146 ptable->pt_hash = hash; /* sign of initialization */
152 static struct lnet_peer_ni *
153 lnet_peer_ni_alloc(lnet_nid_t nid)
155 struct lnet_peer_ni *lpni;
156 struct lnet_net *net;
159 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
161 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
165 INIT_LIST_HEAD(&lpni->lpni_txq);
166 INIT_LIST_HEAD(&lpni->lpni_hashlist);
167 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
168 INIT_LIST_HEAD(&lpni->lpni_recovery);
169 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
170 INIT_LIST_HEAD(&lpni->lpni_rtr_pref_nids);
171 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
172 atomic_set(&lpni->lpni_refcount, 1);
173 lpni->lpni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
175 spin_lock_init(&lpni->lpni_lock);
177 if (lnet_peers_start_down())
178 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
180 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
181 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
182 lpni->lpni_nid = nid;
183 lpni->lpni_cpt = cpt;
184 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
186 net = lnet_get_net_locked(LNET_NIDNET(nid));
187 lpni->lpni_net = net;
189 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
190 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
191 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
192 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
195 * This peer_ni is not on a local network, so we
196 * cannot add the credits here. In case the net is
197 * added later, add the peer_ni to the remote peer ni
198 * list so it can be easily found and revisited.
200 /* FIXME: per-net implementation instead? */
201 lnet_peer_ni_addref_locked(lpni);
202 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
203 &the_lnet.ln_remote_peer_ni_list);
206 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
211 static struct lnet_peer_net *
212 lnet_peer_net_alloc(__u32 net_id)
214 struct lnet_peer_net *lpn;
216 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
220 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
221 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
222 lpn->lpn_net_id = net_id;
223 lpn->lpn_sel_priority = LNET_MAX_SELECTION_PRIORITY;
225 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
231 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
233 struct lnet_peer *lp;
235 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
237 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
238 LASSERT(list_empty(&lpn->lpn_peer_nis));
239 LASSERT(list_empty(&lpn->lpn_peer_nets));
241 lpn->lpn_peer = NULL;
242 LIBCFS_FREE(lpn, sizeof(*lpn));
244 lnet_peer_decref_locked(lp);
247 static struct lnet_peer *
248 lnet_peer_alloc(lnet_nid_t nid)
250 struct lnet_peer *lp;
252 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
256 INIT_LIST_HEAD(&lp->lp_rtrq);
257 INIT_LIST_HEAD(&lp->lp_routes);
258 INIT_LIST_HEAD(&lp->lp_peer_list);
259 INIT_LIST_HEAD(&lp->lp_peer_nets);
260 INIT_LIST_HEAD(&lp->lp_dc_list);
261 INIT_LIST_HEAD(&lp->lp_dc_pendq);
262 INIT_LIST_HEAD(&lp->lp_rtr_list);
263 init_waitqueue_head(&lp->lp_dc_waitq);
264 spin_lock_init(&lp->lp_lock);
265 lp->lp_primary_nid = nid;
266 lp->lp_disc_src_nid = LNET_NID_ANY;
267 if (lnet_peers_start_down())
268 lp->lp_alive = false;
273 * all peers created on a router should have health on
274 * if it's not already on.
276 if (the_lnet.ln_routing && !lnet_health_sensitivity)
277 lp->lp_health_sensitivity = 1;
280 * Turn off discovery for loopback peer. If you're creating a peer
281 * for the loopback interface then that was initiated when we
282 * attempted to send a message over the loopback. There is no need
283 * to ever use a different interface when sending messages to
286 if (nid == LNET_NID_LO_0)
287 lp->lp_state = LNET_PEER_NO_DISCOVERY;
288 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
290 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
296 lnet_destroy_peer_locked(struct lnet_peer *lp)
298 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
300 LASSERT(atomic_read(&lp->lp_refcount) == 0);
301 LASSERT(lp->lp_rtr_refcount == 0);
302 LASSERT(list_empty(&lp->lp_peer_nets));
303 LASSERT(list_empty(&lp->lp_peer_list));
304 LASSERT(list_empty(&lp->lp_dc_list));
307 lnet_ping_buffer_decref(lp->lp_data);
310 * if there are messages still on the pending queue, then make
311 * sure to queue them on the ln_msg_resend list so they can be
312 * resent at a later point if the discovery thread is still
314 * If the discovery thread has stopped, then the wakeup will be a
315 * no-op, and it is expected the lnet_shutdown_lndnets() will
316 * eventually be called, which will traverse this list and
317 * finalize the messages on the list.
318 * We can not resend them now because we're holding the cpt lock.
319 * Releasing the lock can cause an inconsistent state
321 spin_lock(&the_lnet.ln_msg_resend_lock);
322 spin_lock(&lp->lp_lock);
323 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
324 spin_unlock(&lp->lp_lock);
325 spin_unlock(&the_lnet.ln_msg_resend_lock);
326 wake_up(&the_lnet.ln_dc_waitq);
328 LIBCFS_FREE(lp, sizeof(*lp));
332 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
333 * that peer_net, detach the peer_net from the peer.
335 * Call with lnet_net_lock/EX held
338 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
340 struct lnet_peer_table *ptable;
341 struct lnet_peer_net *lpn;
342 struct lnet_peer *lp;
345 * Belts and suspenders: gracefully handle teardown of a
346 * partially connected peer_ni.
348 lpn = lpni->lpni_peer_net;
350 list_del_init(&lpni->lpni_peer_nis);
352 * If there are no lpni's left, we detach lpn from
353 * lp_peer_nets, so it cannot be found anymore.
355 if (list_empty(&lpn->lpn_peer_nis))
356 list_del_init(&lpn->lpn_peer_nets);
358 /* Update peer NID count. */
363 * If there are no more peer nets, make the peer unfindable
364 * via the peer_tables.
366 * Otherwise, if the peer is DISCOVERED, tell discovery to
367 * take another look at it. This is a no-op if discovery for
368 * this peer did the detaching.
370 if (list_empty(&lp->lp_peer_nets)) {
371 list_del_init(&lp->lp_peer_list);
372 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
374 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
375 /* Discovery isn't running, nothing to do here. */
376 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
377 lnet_peer_queue_for_discovery(lp);
378 wake_up(&the_lnet.ln_dc_waitq);
380 CDEBUG(D_NET, "peer %s NID %s\n",
381 libcfs_nid2str(lp->lp_primary_nid),
382 libcfs_nid2str(lpni->lpni_nid));
385 /* called with lnet_net_lock LNET_LOCK_EX held */
387 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
389 struct lnet_peer_table *ptable = NULL;
391 /* don't remove a peer_ni if it's also a gateway */
392 if (lnet_isrouter(lpni) && !force) {
393 CERROR("Peer NI %s is a gateway. Can not delete it\n",
394 libcfs_nid2str(lpni->lpni_nid));
398 lnet_peer_remove_from_remote_list(lpni);
400 /* remove peer ni from the hash list. */
401 list_del_init(&lpni->lpni_hashlist);
404 * indicate the peer is being deleted so the monitor thread can
405 * remove it from the recovery queue.
407 spin_lock(&lpni->lpni_lock);
408 lpni->lpni_state |= LNET_PEER_NI_DELETING;
409 spin_unlock(&lpni->lpni_lock);
411 /* decrement the ref count on the peer table */
412 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
415 * The peer_ni can no longer be found with a lookup. But there
416 * can be current users, so keep track of it on the zombie
417 * list until the reference count has gone to zero.
419 * The last reference may be lost in a place where the
420 * lnet_net_lock locks only a single cpt, and that cpt may not
421 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
424 spin_lock(&ptable->pt_zombie_lock);
425 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
426 ptable->pt_zombies++;
427 spin_unlock(&ptable->pt_zombie_lock);
429 /* no need to keep this peer_ni on the hierarchy anymore */
430 lnet_peer_detach_peer_ni_locked(lpni);
432 /* remove hashlist reference on peer_ni */
433 lnet_peer_ni_decref_locked(lpni);
438 void lnet_peer_uninit(void)
440 struct lnet_peer_ni *lpni, *tmp;
442 lnet_net_lock(LNET_LOCK_EX);
444 /* remove all peer_nis from the remote peer and the hash list */
445 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
446 lpni_on_remote_peer_ni_list)
447 lnet_peer_ni_del_locked(lpni, false);
449 lnet_peer_tables_destroy();
451 lnet_net_unlock(LNET_LOCK_EX);
455 lnet_peer_del_locked(struct lnet_peer *peer)
457 struct lnet_peer_ni *lpni = NULL, *lpni2;
460 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
462 spin_lock(&peer->lp_lock);
463 peer->lp_state |= LNET_PEER_MARK_DELETED;
464 spin_unlock(&peer->lp_lock);
466 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
467 while (lpni != NULL) {
468 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
469 rc = lnet_peer_ni_del_locked(lpni, false);
479 * Discovering this peer is taking too long. Cancel any Ping or Push
480 * that discovery is waiting on by unlinking the relevant MDs. The
481 * lnet_discovery_event_handler() will proceed from here and complete
484 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
486 struct lnet_handle_md ping_mdh;
487 struct lnet_handle_md push_mdh;
489 LNetInvalidateMDHandle(&ping_mdh);
490 LNetInvalidateMDHandle(&push_mdh);
492 spin_lock(&lp->lp_lock);
493 if (lp->lp_state & LNET_PEER_PING_SENT) {
494 ping_mdh = lp->lp_ping_mdh;
495 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
497 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
498 push_mdh = lp->lp_push_mdh;
499 LNetInvalidateMDHandle(&lp->lp_push_mdh);
501 spin_unlock(&lp->lp_lock);
503 if (!LNetMDHandleIsInvalid(ping_mdh))
504 LNetMDUnlink(ping_mdh);
505 if (!LNetMDHandleIsInvalid(push_mdh))
506 LNetMDUnlink(push_mdh);
510 lnet_peer_del(struct lnet_peer *peer)
512 lnet_peer_cancel_discovery(peer);
513 lnet_net_lock(LNET_LOCK_EX);
514 lnet_peer_del_locked(peer);
515 lnet_net_unlock(LNET_LOCK_EX);
521 * Delete a NID from a peer. Call with ln_api_mutex held.
524 * -EPERM: Non-DLC deletion from DLC-configured peer.
525 * -ENOENT: No lnet_peer_ni corresponding to the nid.
526 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
527 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
530 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
532 struct lnet_peer_ni *lpni;
533 lnet_nid_t primary_nid = lp->lp_primary_nid;
535 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
537 if (!(flags & LNET_PEER_CONFIGURED)) {
538 if (lp->lp_state & LNET_PEER_CONFIGURED) {
543 lpni = lnet_find_peer_ni_locked(nid);
548 lnet_peer_ni_decref_locked(lpni);
549 if (lp != lpni->lpni_peer_net->lpn_peer) {
555 * This function only allows deletion of the primary NID if it
558 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
563 lnet_net_lock(LNET_LOCK_EX);
565 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
566 struct lnet_peer_ni *lpni2;
567 /* assign the next peer_ni to be the primary */
568 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
570 lp->lp_primary_nid = lpni2->lpni_nid;
572 rc = lnet_peer_ni_del_locked(lpni, force);
574 lnet_net_unlock(LNET_LOCK_EX);
577 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
578 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
584 lnet_peer_table_cleanup_locked(struct lnet_net *net,
585 struct lnet_peer_table *ptable)
588 struct lnet_peer_ni *next;
589 struct lnet_peer_ni *lpni;
590 struct lnet_peer *peer;
592 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
593 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
595 if (net != NULL && net != lpni->lpni_net)
598 peer = lpni->lpni_peer_net->lpn_peer;
599 if (peer->lp_primary_nid != lpni->lpni_nid) {
600 lnet_peer_ni_del_locked(lpni, false);
604 * Removing the primary NID implies removing
605 * the entire peer. Advance next beyond any
606 * peer_ni that belongs to the same peer.
608 list_for_each_entry_from(next, &ptable->pt_hash[i],
610 if (next->lpni_peer_net->lpn_peer != peer)
613 lnet_peer_del_locked(peer);
619 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
621 wait_var_event_warning(&ptable->pt_zombies,
622 ptable->pt_zombies == 0,
623 "Waiting for %d zombies on peer table\n",
628 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
629 struct lnet_peer_table *ptable)
631 struct lnet_peer_ni *lp;
632 struct lnet_peer_ni *tmp;
636 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
637 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
639 if (net != lp->lpni_net)
642 if (!lnet_isrouter(lp))
645 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
647 lnet_net_unlock(LNET_LOCK_EX);
648 lnet_del_route(LNET_NET_ANY, gw_nid);
649 lnet_net_lock(LNET_LOCK_EX);
655 lnet_peer_tables_cleanup(struct lnet_net *net)
658 struct lnet_peer_table *ptable;
660 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
661 /* If just deleting the peers for a NI, get rid of any routes these
662 * peers are gateways for. */
663 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
664 lnet_net_lock(LNET_LOCK_EX);
665 lnet_peer_table_del_rtrs_locked(net, ptable);
666 lnet_net_unlock(LNET_LOCK_EX);
669 /* Start the cleanup process */
670 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
671 lnet_net_lock(LNET_LOCK_EX);
672 lnet_peer_table_cleanup_locked(net, ptable);
673 lnet_net_unlock(LNET_LOCK_EX);
676 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
677 lnet_peer_ni_finalize_wait(ptable);
680 static struct lnet_peer_ni *
681 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
683 struct list_head *peers;
684 struct lnet_peer_ni *lp;
686 if (the_lnet.ln_state != LNET_STATE_RUNNING)
689 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
690 list_for_each_entry(lp, peers, lpni_hashlist) {
691 if (lp->lpni_nid == nid) {
692 lnet_peer_ni_addref_locked(lp);
700 struct lnet_peer_ni *
701 lnet_find_peer_ni_locked(lnet_nid_t nid)
703 struct lnet_peer_ni *lpni;
704 struct lnet_peer_table *ptable;
707 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
709 ptable = the_lnet.ln_peer_tables[cpt];
710 lpni = lnet_get_peer_ni_locked(ptable, nid);
715 struct lnet_peer_ni *
716 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
718 struct lnet_peer_net *lpn;
719 struct lnet_peer_ni *lpni;
721 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
725 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
726 if (lpni->lpni_nid == nid)
734 lnet_find_peer(lnet_nid_t nid)
736 struct lnet_peer_ni *lpni;
737 struct lnet_peer *lp = NULL;
740 cpt = lnet_net_lock_current();
741 lpni = lnet_find_peer_ni_locked(nid);
743 lp = lpni->lpni_peer_net->lpn_peer;
744 lnet_peer_addref_locked(lp);
745 lnet_peer_ni_decref_locked(lpni);
747 lnet_net_unlock(cpt);
752 struct lnet_peer_net *
753 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
755 struct lnet_peer_net *net;
758 /* no net id provided return the first net */
759 net = list_first_entry_or_null(&lp->lp_peer_nets,
760 struct lnet_peer_net,
766 /* find the net after the one provided */
767 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
768 if (net->lpn_net_id == prev_lpn_id) {
770 * if we reached the end of the list loop to the
773 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
774 return list_first_entry_or_null(&lp->lp_peer_nets,
775 struct lnet_peer_net,
778 return list_next_entry(net, lpn_peer_nets);
785 struct lnet_peer_ni *
786 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
787 struct lnet_peer_net *peer_net,
788 struct lnet_peer_ni *prev)
790 struct lnet_peer_ni *lpni;
791 struct lnet_peer_net *net = peer_net;
795 if (list_empty(&peer->lp_peer_nets))
798 net = list_entry(peer->lp_peer_nets.next,
799 struct lnet_peer_net,
802 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
808 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
810 * if you reached the end of the peer ni list and the peer
811 * net is specified then there are no more peer nis in that
818 * we reached the end of this net ni list. move to the
821 if (prev->lpni_peer_net->lpn_peer_nets.next ==
823 /* no more nets and no more NIs. */
826 /* get the next net */
827 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
828 struct lnet_peer_net,
830 /* get the ni on it */
831 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
837 /* there are more nis left */
838 lpni = list_entry(prev->lpni_peer_nis.next,
839 struct lnet_peer_ni, lpni_peer_nis);
844 /* Call with the ln_api_mutex held */
845 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
847 struct lnet_process_id id;
848 struct lnet_peer_table *ptable;
849 struct lnet_peer *lp;
858 if (the_lnet.ln_state != LNET_STATE_RUNNING)
861 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
864 * Count the number of peers, and return E2BIG if the buffer
865 * is too small. We'll also return the desired size.
868 for (cpt = 0; cpt < lncpt; cpt++) {
869 ptable = the_lnet.ln_peer_tables[cpt];
870 count += ptable->pt_peers;
872 size = count * sizeof(*ids);
877 * Walk the peer lists and copy out the primary nids.
878 * This is safe because the peer lists are only modified
879 * while the ln_api_mutex is held. So we don't need to
880 * hold the lnet_net_lock as well, and can therefore
881 * directly call copy_to_user().
884 memset(&id, 0, sizeof(id));
885 id.pid = LNET_PID_LUSTRE;
887 for (cpt = 0; cpt < lncpt; cpt++) {
888 ptable = the_lnet.ln_peer_tables[cpt];
889 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
892 id.nid = lp->lp_primary_nid;
893 if (copy_to_user(&ids[i], &id, sizeof(id)))
906 * Start pushes to peers that need to be updated for a configuration
907 * change on this node.
910 lnet_push_update_to_peers(int force)
912 struct lnet_peer_table *ptable;
913 struct lnet_peer *lp;
917 lnet_net_lock(LNET_LOCK_EX);
918 if (lnet_peer_discovery_disabled)
920 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
921 for (cpt = 0; cpt < lncpt; cpt++) {
922 ptable = the_lnet.ln_peer_tables[cpt];
923 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
925 spin_lock(&lp->lp_lock);
926 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
927 lp->lp_state |= LNET_PEER_FORCE_PUSH;
928 spin_unlock(&lp->lp_lock);
930 if (lnet_peer_needs_push(lp))
931 lnet_peer_queue_for_discovery(lp);
934 lnet_net_unlock(LNET_LOCK_EX);
935 wake_up(&the_lnet.ln_dc_waitq);
938 /* find the NID in the preferred gateways for the remote peer
940 * false: list is not empty and NID is not preferred
941 * false: list is empty
942 * true: nid is found in the list
945 lnet_peer_is_pref_rtr_locked(struct lnet_peer_ni *lpni,
948 struct lnet_nid_list *ne;
950 CDEBUG(D_NET, "%s: rtr pref emtpy: %d\n",
951 libcfs_nid2str(lpni->lpni_nid),
952 list_empty(&lpni->lpni_rtr_pref_nids));
954 if (list_empty(&lpni->lpni_rtr_pref_nids))
957 /* iterate through all the preferred NIDs and see if any of them
958 * matches the provided gw_nid
960 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
961 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
962 libcfs_nid2str(ne->nl_nid),
963 libcfs_nid2str(gw_nid));
964 if (ne->nl_nid == gw_nid)
972 lnet_peer_clr_pref_rtrs(struct lnet_peer_ni *lpni)
974 struct list_head zombies;
975 struct lnet_nid_list *ne;
976 struct lnet_nid_list *tmp;
977 int cpt = lpni->lpni_cpt;
979 INIT_LIST_HEAD(&zombies);
982 list_splice_init(&lpni->lpni_rtr_pref_nids, &zombies);
983 lnet_net_unlock(cpt);
985 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
986 list_del(&ne->nl_list);
987 LIBCFS_FREE(ne, sizeof(*ne));
992 lnet_peer_add_pref_rtr(struct lnet_peer_ni *lpni,
995 int cpt = lpni->lpni_cpt;
996 struct lnet_nid_list *ne = NULL;
998 /* This function is called with api_mutex held. When the api_mutex
999 * is held the list can not be modified, as it is only modified as
1000 * a result of applying a UDSP and that happens under api_mutex
1003 __must_hold(&the_lnet.ln_api_mutex);
1005 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
1006 if (ne->nl_nid == gw_nid)
1010 LIBCFS_CPT_ALLOC(ne, lnet_cpt_table(), cpt, sizeof(*ne));
1014 ne->nl_nid = gw_nid;
1016 /* Lock the cpt to protect against addition and checks in the
1017 * selection algorithm
1020 list_add(&ne->nl_list, &lpni->lpni_rtr_pref_nids);
1021 lnet_net_unlock(cpt);
1027 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
1028 * this is a preferred point-to-point path. Call with lnet_net_lock in
1032 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1034 struct lnet_nid_list *ne;
1036 if (lpni->lpni_pref_nnids == 0)
1038 if (lpni->lpni_pref_nnids == 1)
1039 return lpni->lpni_pref.nid == nid;
1040 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1041 if (ne->nl_nid == nid)
1048 * Set a single ni as preferred, provided no preferred ni is already
1049 * defined. Only to be used for non-multi-rail peer_ni.
1052 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1056 spin_lock(&lpni->lpni_lock);
1057 if (nid == LNET_NID_ANY) {
1059 } else if (lpni->lpni_pref_nnids > 0) {
1061 } else if (lpni->lpni_pref_nnids == 0) {
1062 lpni->lpni_pref.nid = nid;
1063 lpni->lpni_pref_nnids = 1;
1064 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
1066 spin_unlock(&lpni->lpni_lock);
1068 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1069 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
1074 * Clear the preferred NID from a non-multi-rail peer_ni, provided
1075 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
1078 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
1082 spin_lock(&lpni->lpni_lock);
1083 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
1084 lpni->lpni_pref_nnids = 0;
1085 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1086 } else if (lpni->lpni_pref_nnids == 0) {
1091 spin_unlock(&lpni->lpni_lock);
1093 CDEBUG(D_NET, "peer %s: %d\n",
1094 libcfs_nid2str(lpni->lpni_nid), rc);
1099 lnet_peer_ni_set_selection_priority(struct lnet_peer_ni *lpni, __u32 priority)
1101 lpni->lpni_sel_priority = priority;
1105 * Clear the preferred NIDs from a non-multi-rail peer.
1108 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
1110 struct lnet_peer_ni *lpni = NULL;
1112 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
1113 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
1117 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1119 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1120 struct lnet_nid_list *ne1 = NULL;
1121 struct lnet_nid_list *ne2 = NULL;
1122 lnet_nid_t tmp_nid = LNET_NID_ANY;
1125 if (nid == LNET_NID_ANY) {
1130 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1135 /* A non-MR node may have only one preferred NI per peer_ni */
1136 if (lpni->lpni_pref_nnids > 0 &&
1137 !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1142 /* add the new preferred nid to the list of preferred nids */
1143 if (lpni->lpni_pref_nnids != 0) {
1144 size_t alloc_size = sizeof(*ne1);
1146 if (lpni->lpni_pref_nnids == 1) {
1147 tmp_nid = lpni->lpni_pref.nid;
1148 INIT_LIST_HEAD(&lpni->lpni_pref.nids);
1151 list_for_each_entry(ne1, &lpni->lpni_pref.nids, nl_list) {
1152 if (ne1->nl_nid == nid) {
1158 LIBCFS_CPT_ALLOC(ne1, lnet_cpt_table(), lpni->lpni_cpt,
1165 /* move the originally stored nid to the list */
1166 if (lpni->lpni_pref_nnids == 1) {
1167 LIBCFS_CPT_ALLOC(ne2, lnet_cpt_table(),
1168 lpni->lpni_cpt, alloc_size);
1173 INIT_LIST_HEAD(&ne2->nl_list);
1174 ne2->nl_nid = tmp_nid;
1179 lnet_net_lock(LNET_LOCK_EX);
1180 spin_lock(&lpni->lpni_lock);
1181 if (lpni->lpni_pref_nnids == 0) {
1182 lpni->lpni_pref.nid = nid;
1185 list_add_tail(&ne2->nl_list, &lpni->lpni_pref.nids);
1186 list_add_tail(&ne1->nl_list, &lpni->lpni_pref.nids);
1188 lpni->lpni_pref_nnids++;
1189 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1190 spin_unlock(&lpni->lpni_lock);
1191 lnet_net_unlock(LNET_LOCK_EX);
1194 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1195 spin_lock(&lpni->lpni_lock);
1196 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1197 spin_unlock(&lpni->lpni_lock);
1199 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1200 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1205 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1207 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1208 struct lnet_nid_list *ne = NULL;
1211 if (lpni->lpni_pref_nnids == 0) {
1216 if (lpni->lpni_pref_nnids == 1) {
1217 if (lpni->lpni_pref.nid != nid) {
1222 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1223 if (ne->nl_nid == nid)
1224 goto remove_nid_entry;
1232 lnet_net_lock(LNET_LOCK_EX);
1233 spin_lock(&lpni->lpni_lock);
1234 if (lpni->lpni_pref_nnids == 1)
1235 lpni->lpni_pref.nid = LNET_NID_ANY;
1237 list_del_init(&ne->nl_list);
1238 if (lpni->lpni_pref_nnids == 2) {
1239 struct lnet_nid_list *ne, *tmp;
1241 list_for_each_entry_safe(ne, tmp,
1242 &lpni->lpni_pref.nids,
1244 lpni->lpni_pref.nid = ne->nl_nid;
1245 list_del_init(&ne->nl_list);
1246 LIBCFS_FREE(ne, sizeof(*ne));
1250 lpni->lpni_pref_nnids--;
1251 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1252 spin_unlock(&lpni->lpni_lock);
1253 lnet_net_unlock(LNET_LOCK_EX);
1256 LIBCFS_FREE(ne, sizeof(*ne));
1258 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1259 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1264 lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni)
1266 struct list_head zombies;
1267 struct lnet_nid_list *ne;
1268 struct lnet_nid_list *tmp;
1270 INIT_LIST_HEAD(&zombies);
1272 lnet_net_lock(LNET_LOCK_EX);
1273 if (lpni->lpni_pref_nnids == 1)
1274 lpni->lpni_pref.nid = LNET_NID_ANY;
1275 else if (lpni->lpni_pref_nnids > 1)
1276 list_splice_init(&lpni->lpni_pref.nids, &zombies);
1277 lpni->lpni_pref_nnids = 0;
1278 lnet_net_unlock(LNET_LOCK_EX);
1280 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1281 list_del_init(&ne->nl_list);
1282 LIBCFS_FREE(ne, sizeof(*ne));
1287 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1289 struct lnet_peer_ni *lpni;
1290 lnet_nid_t primary_nid = nid;
1292 lpni = lnet_find_peer_ni_locked(nid);
1294 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1295 lnet_peer_ni_decref_locked(lpni);
1302 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1303 __must_hold(&lp->lp_lock)
1305 if (lnet_peer_discovery_disabled)
1308 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1309 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1320 lnet_is_discovery_disabled(struct lnet_peer *lp)
1324 spin_lock(&lp->lp_lock);
1325 rc = lnet_is_discovery_disabled_locked(lp);
1326 spin_unlock(&lp->lp_lock);
1332 LNetPrimaryNID(lnet_nid_t nid)
1334 struct lnet_peer *lp;
1335 struct lnet_peer_ni *lpni;
1336 lnet_nid_t primary_nid = nid;
1340 if (nid == LNET_NID_LO_0)
1341 return LNET_NID_LO_0;
1343 cpt = lnet_net_lock_current();
1344 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1349 lp = lpni->lpni_peer_net->lpn_peer;
1351 while (!lnet_peer_is_uptodate(lp)) {
1352 spin_lock(&lp->lp_lock);
1353 /* force a full discovery cycle */
1354 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1355 spin_unlock(&lp->lp_lock);
1357 rc = lnet_discover_peer_locked(lpni, cpt, true);
1360 /* The lpni (or lp) for this NID may have changed and our ref is
1361 * the only thing keeping the old one around. Release the ref
1362 * and lookup the lpni again
1364 lnet_peer_ni_decref_locked(lpni);
1365 lpni = lnet_find_peer_ni_locked(nid);
1370 lp = lpni->lpni_peer_net->lpn_peer;
1372 /* Only try once if discovery is disabled */
1373 if (lnet_is_discovery_disabled(lp))
1376 primary_nid = lp->lp_primary_nid;
1378 lnet_peer_ni_decref_locked(lpni);
1380 lnet_net_unlock(cpt);
1382 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1383 libcfs_nid2str(primary_nid), rc);
1386 EXPORT_SYMBOL(LNetPrimaryNID);
1388 struct lnet_peer_net *
1389 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1391 struct lnet_peer_net *peer_net;
1392 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1393 if (peer_net->lpn_net_id == net_id)
1400 * Attach a peer_ni to a peer_net and peer. This function assumes
1401 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1402 * may be attached to a different peer, in which case it will be
1403 * properly detached first. The whole operation is done atomically.
1405 * This function consumes the reference on lpni and Always returns 0.
1406 * This is the last function called from functions that do return an
1407 * int, so returning 0 here allows the compiler to do a tail call.
1410 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1411 struct lnet_peer_net *lpn,
1412 struct lnet_peer_ni *lpni,
1415 struct lnet_peer_table *ptable;
1416 bool new_lpn = false;
1419 /* Install the new peer_ni */
1420 lnet_net_lock(LNET_LOCK_EX);
1421 /* Add peer_ni to global peer table hash, if necessary. */
1422 if (list_empty(&lpni->lpni_hashlist)) {
1423 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1425 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1426 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1427 ptable->pt_version++;
1428 lnet_peer_ni_addref_locked(lpni);
1431 /* Detach the peer_ni from an existing peer, if necessary. */
1432 if (lpni->lpni_peer_net) {
1433 LASSERT(lpni->lpni_peer_net != lpn);
1434 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1435 lnet_peer_detach_peer_ni_locked(lpni);
1436 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1437 lpni->lpni_peer_net = NULL;
1440 /* Add peer_ni to peer_net */
1441 lpni->lpni_peer_net = lpn;
1442 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1443 lnet_update_peer_net_healthv(lpni);
1444 lnet_peer_net_addref_locked(lpn);
1446 /* Add peer_net to peer */
1447 if (!lpn->lpn_peer) {
1450 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1451 lnet_peer_addref_locked(lp);
1454 /* Add peer to global peer list, if necessary */
1455 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1456 if (list_empty(&lp->lp_peer_list)) {
1457 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1462 /* Update peer state */
1463 spin_lock(&lp->lp_lock);
1464 if (flags & LNET_PEER_CONFIGURED) {
1465 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1466 lp->lp_state |= LNET_PEER_CONFIGURED;
1468 if (flags & LNET_PEER_MULTI_RAIL) {
1469 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1470 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1471 lnet_peer_clr_non_mr_pref_nids(lp);
1474 spin_unlock(&lp->lp_lock);
1480 rc = lnet_udsp_apply_policies_on_lpn(lpn);
1482 CERROR("Failed to apply UDSPs on lpn %s\n",
1483 libcfs_net2str(lpn->lpn_net_id));
1485 rc = lnet_udsp_apply_policies_on_lpni(lpni);
1487 CERROR("Failed to apply UDSPs on lpni %s\n",
1488 libcfs_nid2str(lpni->lpni_nid));
1490 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1491 libcfs_nid2str(lp->lp_primary_nid),
1492 libcfs_nid2str(lpni->lpni_nid), flags);
1493 lnet_peer_ni_decref_locked(lpni);
1494 lnet_net_unlock(LNET_LOCK_EX);
1500 * Create a new peer, with nid as its primary nid.
1502 * Call with the lnet_api_mutex held.
1505 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1507 struct lnet_peer *lp;
1508 struct lnet_peer_net *lpn;
1509 struct lnet_peer_ni *lpni;
1512 LASSERT(nid != LNET_NID_ANY);
1515 * No need for the lnet_net_lock here, because the
1516 * lnet_api_mutex is held.
1518 lpni = lnet_find_peer_ni_locked(nid);
1520 /* A peer with this NID already exists. */
1521 lp = lpni->lpni_peer_net->lpn_peer;
1522 lnet_peer_ni_decref_locked(lpni);
1524 * This is an error if the peer was configured and the
1525 * primary NID differs or an attempt is made to change
1526 * the Multi-Rail flag. Otherwise the assumption is
1527 * that an existing peer is being modified.
1529 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1530 if (lp->lp_primary_nid != nid)
1532 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1536 /* Delete and recreate as a configured peer. */
1540 /* Create peer, peer_net, and peer_ni. */
1542 lp = lnet_peer_alloc(nid);
1545 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1548 lpni = lnet_peer_ni_alloc(nid);
1552 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1555 LIBCFS_FREE(lpn, sizeof(*lpn));
1557 LIBCFS_FREE(lp, sizeof(*lp));
1559 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1560 libcfs_nid2str(nid), flags, rc);
1565 * Add a NID to a peer. Call with ln_api_mutex held.
1568 * -EPERM: Non-DLC addition to a DLC-configured peer.
1569 * -EEXIST: The NID was configured by DLC for a different peer.
1570 * -ENOMEM: Out of memory.
1571 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1572 * non-multi-rail peer.
1575 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1577 struct lnet_peer_net *lpn;
1578 struct lnet_peer_ni *lpni;
1582 LASSERT(nid != LNET_NID_ANY);
1584 /* A configured peer can only be updated through configuration. */
1585 if (!(flags & LNET_PEER_CONFIGURED)) {
1586 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1593 * The MULTI_RAIL flag can be set but not cleared, because
1594 * that would leave the peer struct in an invalid state.
1596 if (flags & LNET_PEER_MULTI_RAIL) {
1597 spin_lock(&lp->lp_lock);
1598 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1599 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1600 lnet_peer_clr_non_mr_pref_nids(lp);
1602 spin_unlock(&lp->lp_lock);
1603 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1608 lpni = lnet_find_peer_ni_locked(nid);
1611 * A peer_ni already exists. This is only a problem if
1612 * it is not connected to this peer and was configured
1615 if (lpni->lpni_peer_net->lpn_peer == lp)
1617 if (lnet_peer_ni_is_configured(lpni)) {
1621 /* If this is the primary NID, destroy the peer. */
1622 if (lnet_peer_ni_is_primary(lpni)) {
1623 struct lnet_peer *rtr_lp =
1624 lpni->lpni_peer_net->lpn_peer;
1625 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1627 * if we're trying to delete a router it means
1628 * we're moving this peer NI to a new peer so must
1629 * transfer router properties to the new peer
1631 if (rtr_refcount > 0) {
1632 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1633 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1635 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1636 lnet_peer_ni_decref_locked(lpni);
1637 lpni = lnet_peer_ni_alloc(nid);
1644 lpni = lnet_peer_ni_alloc(nid);
1652 * Get the peer_net. Check that we're not adding a second
1653 * peer_ni on a peer_net of a non-multi-rail peer.
1655 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1657 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1662 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1667 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1670 lnet_peer_ni_decref_locked(lpni);
1672 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1673 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1679 * Update the primary NID of a peer, if possible.
1681 * Call with the lnet_api_mutex held.
1684 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1686 lnet_nid_t old = lp->lp_primary_nid;
1689 if (lp->lp_primary_nid == nid)
1691 rc = lnet_peer_add_nid(lp, nid, flags);
1694 lp->lp_primary_nid = nid;
1696 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1697 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1702 * lpni creation initiated due to traffic either sending or receiving.
1705 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1707 struct lnet_peer *lp;
1708 struct lnet_peer_net *lpn;
1709 struct lnet_peer_ni *lpni;
1713 if (nid == LNET_NID_ANY) {
1718 /* lnet_net_lock is not needed here because ln_api_lock is held */
1719 lpni = lnet_find_peer_ni_locked(nid);
1722 * We must have raced with another thread. Since we
1723 * know next to nothing about a peer_ni created by
1724 * traffic, we just assume everything is ok and
1727 lnet_peer_ni_decref_locked(lpni);
1731 /* Create peer, peer_net, and peer_ni. */
1733 lp = lnet_peer_alloc(nid);
1736 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1739 lpni = lnet_peer_ni_alloc(nid);
1742 if (pref != LNET_NID_ANY)
1743 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1745 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1748 LIBCFS_FREE(lpn, sizeof(*lpn));
1750 LIBCFS_FREE(lp, sizeof(*lp));
1752 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1757 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1759 * This API handles the following combinations:
1760 * Create a peer with its primary NI if only the prim_nid is provided
1761 * Add a NID to a peer identified by the prim_nid. The peer identified
1762 * by the prim_nid must already exist.
1763 * The peer being created may be non-MR.
1765 * The caller must hold ln_api_mutex. This prevents the peer from
1766 * being created/modified/deleted by a different thread.
1769 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1771 struct lnet_peer *lp = NULL;
1772 struct lnet_peer_ni *lpni;
1775 /* The prim_nid must always be specified */
1776 if (prim_nid == LNET_NID_ANY)
1779 flags = LNET_PEER_CONFIGURED;
1781 flags |= LNET_PEER_MULTI_RAIL;
1784 * If nid isn't specified, we must create a new peer with
1785 * prim_nid as its primary nid.
1787 if (nid == LNET_NID_ANY)
1788 return lnet_peer_add(prim_nid, flags);
1790 /* Look up the prim_nid, which must exist. */
1791 lpni = lnet_find_peer_ni_locked(prim_nid);
1794 lnet_peer_ni_decref_locked(lpni);
1795 lp = lpni->lpni_peer_net->lpn_peer;
1797 /* Peer must have been configured. */
1798 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1799 CDEBUG(D_NET, "peer %s was not configured\n",
1800 libcfs_nid2str(prim_nid));
1804 /* Primary NID must match */
1805 if (lp->lp_primary_nid != prim_nid) {
1806 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1807 libcfs_nid2str(prim_nid),
1808 libcfs_nid2str(lp->lp_primary_nid));
1812 /* Multi-Rail flag must match. */
1813 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1814 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1815 libcfs_nid2str(prim_nid));
1819 return lnet_peer_add_nid(lp, nid, flags);
1823 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1825 * This API handles the following combinations:
1826 * Delete a NI from a peer if both prim_nid and nid are provided.
1827 * Delete a peer if only prim_nid is provided.
1828 * Delete a peer if its primary nid is provided.
1830 * The caller must hold ln_api_mutex. This prevents the peer from
1831 * being modified/deleted by a different thread.
1834 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1836 struct lnet_peer *lp;
1837 struct lnet_peer_ni *lpni;
1840 if (prim_nid == LNET_NID_ANY)
1843 lpni = lnet_find_peer_ni_locked(prim_nid);
1846 lnet_peer_ni_decref_locked(lpni);
1847 lp = lpni->lpni_peer_net->lpn_peer;
1849 if (prim_nid != lp->lp_primary_nid) {
1850 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1851 libcfs_nid2str(prim_nid),
1852 libcfs_nid2str(lp->lp_primary_nid));
1856 lnet_net_lock(LNET_LOCK_EX);
1857 if (lp->lp_rtr_refcount > 0) {
1858 lnet_net_unlock(LNET_LOCK_EX);
1859 CERROR("%s is a router. Can not be deleted\n",
1860 libcfs_nid2str(prim_nid));
1863 lnet_net_unlock(LNET_LOCK_EX);
1865 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1866 return lnet_peer_del(lp);
1868 flags = LNET_PEER_CONFIGURED;
1869 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1870 flags |= LNET_PEER_MULTI_RAIL;
1872 return lnet_peer_del_nid(lp, nid, flags);
1876 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1878 struct lnet_peer_table *ptable;
1879 struct lnet_peer_net *lpn;
1881 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1883 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1884 LASSERT(list_empty(&lpni->lpni_txq));
1885 LASSERT(lpni->lpni_txqnob == 0);
1886 LASSERT(list_empty(&lpni->lpni_peer_nis));
1887 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1889 lpn = lpni->lpni_peer_net;
1890 lpni->lpni_peer_net = NULL;
1891 lpni->lpni_net = NULL;
1893 if (!list_empty(&lpni->lpni_hashlist)) {
1894 /* remove the peer ni from the zombie list */
1895 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1896 spin_lock(&ptable->pt_zombie_lock);
1897 list_del_init(&lpni->lpni_hashlist);
1898 ptable->pt_zombies--;
1899 spin_unlock(&ptable->pt_zombie_lock);
1902 if (lpni->lpni_pref_nnids > 1) {
1903 struct lnet_nid_list *ne, *tmp;
1905 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
1907 list_del_init(&ne->nl_list);
1908 LIBCFS_FREE(ne, sizeof(*ne));
1911 LIBCFS_FREE(lpni, sizeof(*lpni));
1914 lnet_peer_net_decref_locked(lpn);
1917 struct lnet_peer_ni *
1918 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1920 struct lnet_peer_ni *lpni = NULL;
1923 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1924 return ERR_PTR(-ESHUTDOWN);
1927 * find if a peer_ni already exists.
1928 * If so then just return that.
1930 lpni = lnet_find_peer_ni_locked(nid);
1934 lnet_net_unlock(cpt);
1936 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1939 goto out_net_relock;
1942 lpni = lnet_find_peer_ni_locked(nid);
1952 * Get a peer_ni for the given nid, create it if necessary. Takes a
1953 * hold on the peer_ni.
1955 struct lnet_peer_ni *
1956 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1958 struct lnet_peer_ni *lpni = NULL;
1961 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1962 return ERR_PTR(-ESHUTDOWN);
1965 * find if a peer_ni already exists.
1966 * If so then just return that.
1968 lpni = lnet_find_peer_ni_locked(nid);
1974 * use the lnet_api_mutex to serialize the creation of the peer_ni
1975 * and the creation/deletion of the local ni/net. When a local ni is
1976 * created, if there exists a set of peer_nis on that network,
1977 * they need to be traversed and updated. When a local NI is
1978 * deleted, which could result in a network being deleted, then
1979 * all peer nis on that network need to be removed as well.
1981 * Creation through traffic should also be serialized with
1982 * creation through DLC.
1984 lnet_net_unlock(cpt);
1985 mutex_lock(&the_lnet.ln_api_mutex);
1987 * Shutdown is only set under the ln_api_lock, so a single
1988 * check here is sufficent.
1990 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1991 lpni = ERR_PTR(-ESHUTDOWN);
1992 goto out_mutex_unlock;
1995 rc = lnet_peer_ni_traffic_add(nid, pref);
1998 goto out_mutex_unlock;
2001 lpni = lnet_find_peer_ni_locked(nid);
2005 mutex_unlock(&the_lnet.ln_api_mutex);
2008 /* Lock has been dropped, check again for shutdown. */
2009 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2011 lnet_peer_ni_decref_locked(lpni);
2012 lpni = ERR_PTR(-ESHUTDOWN);
2019 lnet_peer_gw_discovery(struct lnet_peer *lp)
2023 spin_lock(&lp->lp_lock);
2024 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
2026 spin_unlock(&lp->lp_lock);
2032 lnet_peer_is_uptodate(struct lnet_peer *lp)
2036 spin_lock(&lp->lp_lock);
2037 rc = lnet_peer_is_uptodate_locked(lp);
2038 spin_unlock(&lp->lp_lock);
2043 * Is a peer uptodate from the point of view of discovery?
2045 * If it is currently being processed, obviously not.
2046 * A forced Ping or Push is also handled by the discovery thread.
2048 * Otherwise look at whether the peer needs rediscovering.
2051 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
2052 __must_hold(&lp->lp_lock)
2056 if (lp->lp_state & (LNET_PEER_DISCOVERING |
2057 LNET_PEER_FORCE_PING |
2058 LNET_PEER_FORCE_PUSH)) {
2060 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
2062 } else if (lnet_peer_needs_push(lp)) {
2064 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
2065 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
2076 /* Add the message to the peer's lp_dc_pendq and queue the peer for discovery */
2078 lnet_peer_queue_message(struct lnet_peer *lp, struct lnet_msg *msg)
2080 /* The discovery thread holds net_lock/EX and lp_lock when it splices
2081 * the lp_dc_pendq onto a local list for resending. Thus, we do the same
2082 * when adding to the list and queuing the peer to ensure that we do not
2083 * strand any messages on the lp_dc_pendq. This scheme ensures the
2084 * message will be resent even if the peer is already being discovered.
2085 * Therefore we needn't check the return value of
2086 * lnet_peer_queue_for_discovery(lp).
2088 lnet_net_lock(LNET_LOCK_EX);
2089 spin_lock(&lp->lp_lock);
2090 list_add_tail(&msg->msg_list, &lp->lp_dc_pendq);
2091 spin_unlock(&lp->lp_lock);
2092 lnet_peer_queue_for_discovery(lp);
2093 lnet_net_unlock(LNET_LOCK_EX);
2097 * Queue a peer for the attention of the discovery thread. Call with
2098 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
2099 * -EALREADY if the peer was already queued.
2101 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
2105 spin_lock(&lp->lp_lock);
2106 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2107 lp->lp_state |= LNET_PEER_DISCOVERING;
2108 spin_unlock(&lp->lp_lock);
2109 if (list_empty(&lp->lp_dc_list)) {
2110 lnet_peer_addref_locked(lp);
2111 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2112 wake_up(&the_lnet.ln_dc_waitq);
2118 CDEBUG(D_NET, "Queue peer %s: %d\n",
2119 libcfs_nid2str(lp->lp_primary_nid), rc);
2125 * Discovery of a peer is complete. Wake all waiters on the peer.
2126 * Call with lnet_net_lock/EX held.
2128 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
2130 struct lnet_msg *msg, *tmp;
2132 LIST_HEAD(pending_msgs);
2134 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
2135 libcfs_nid2str(lp->lp_primary_nid));
2137 list_del_init(&lp->lp_dc_list);
2138 spin_lock(&lp->lp_lock);
2139 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
2140 spin_unlock(&lp->lp_lock);
2141 wake_up_all(&lp->lp_dc_waitq);
2143 if (lp->lp_rtr_refcount > 0)
2144 lnet_router_discovery_complete(lp);
2146 lnet_net_unlock(LNET_LOCK_EX);
2148 /* iterate through all pending messages and send them again */
2149 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
2150 list_del_init(&msg->msg_list);
2151 if (lp->lp_dc_error) {
2152 lnet_finalize(msg, lp->lp_dc_error);
2156 CDEBUG(D_NET, "sending pending message %s to target %s\n",
2157 lnet_msgtyp2str(msg->msg_type),
2158 libcfs_id2str(msg->msg_target));
2159 rc = lnet_send(msg->msg_src_nid_param, msg,
2160 msg->msg_rtr_nid_param);
2162 CNETERR("Error sending %s to %s: %d\n",
2163 lnet_msgtyp2str(msg->msg_type),
2164 libcfs_id2str(msg->msg_target), rc);
2165 lnet_finalize(msg, rc);
2168 lnet_net_lock(LNET_LOCK_EX);
2169 lnet_peer_decref_locked(lp);
2173 * Handle inbound push.
2174 * Like any event handler, called with lnet_res_lock/CPT held.
2176 void lnet_peer_push_event(struct lnet_event *ev)
2178 struct lnet_ping_buffer *pbuf;
2179 struct lnet_peer *lp;
2181 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
2183 /* lnet_find_peer() adds a refcount */
2184 lp = lnet_find_peer(ev->source.nid);
2186 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
2187 libcfs_nid2str(ev->initiator.nid),
2188 libcfs_nid2str(ev->source.nid));
2189 pbuf->pb_needs_post = true;
2193 /* Ensure peer state remains consistent while we modify it. */
2194 spin_lock(&lp->lp_lock);
2197 * If some kind of error happened the contents of the message
2198 * cannot be used. Clear the NIDS_UPTODATE and set the
2199 * FORCE_PING flag to trigger a ping.
2202 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2203 lp->lp_state |= LNET_PEER_FORCE_PING;
2204 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2206 libcfs_nid2str(lp->lp_primary_nid),
2207 libcfs_nid2str(ev->source.nid));
2212 * A push with invalid or corrupted info. Clear the UPTODATE
2213 * flag to trigger a ping.
2215 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2216 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2217 lp->lp_state |= LNET_PEER_FORCE_PING;
2218 CDEBUG(D_NET, "Corrupted Push from %s\n",
2219 libcfs_nid2str(lp->lp_primary_nid));
2224 * Make sure we'll allocate the correct size ping buffer when
2227 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2228 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2231 * A non-Multi-Rail peer is not supposed to be capable of
2234 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2235 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2236 libcfs_nid2str(lp->lp_primary_nid));
2241 * The peer may have discovery disabled at its end. Set
2242 * NO_DISCOVERY as appropriate.
2244 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2245 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2246 libcfs_nid2str(lp->lp_primary_nid));
2248 * Mark the peer for deletion if we already know about it
2249 * and it's going from discovery set to no discovery set
2251 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2252 LNET_PEER_DISCOVERING)) &&
2253 lp->lp_state & LNET_PEER_DISCOVERED) {
2254 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2255 libcfs_nid2str(lp->lp_primary_nid),
2257 lp->lp_state |= LNET_PEER_MARK_DELETION;
2259 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2260 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2261 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2262 libcfs_nid2str(lp->lp_primary_nid));
2263 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2267 * Update the MULTI_RAIL flag based on the push. If the peer
2268 * was configured with DLC then the setting should match what
2270 * NB: We verified above that the MR feature bit is set in pi_features
2272 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2273 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2274 libcfs_nid2str(lp->lp_primary_nid), lp);
2275 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2276 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2277 libcfs_nid2str(lp->lp_primary_nid));
2278 } else if (lnet_peer_discovery_disabled) {
2279 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2280 libcfs_nid2str(lp->lp_primary_nid), lp);
2281 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2282 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2283 libcfs_nid2str(lp->lp_primary_nid), lp);
2285 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2286 libcfs_nid2str(lp->lp_primary_nid), lp);
2287 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2288 lnet_peer_clr_non_mr_pref_nids(lp);
2292 * Check for truncation of the Put message. Clear the
2293 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2294 * and tell discovery to allocate a bigger buffer.
2296 if (ev->mlength < ev->rlength) {
2297 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2298 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2299 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2300 lp->lp_state |= LNET_PEER_FORCE_PING;
2301 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2302 libcfs_nid2str(lp->lp_primary_nid),
2303 pbuf->pb_info.pi_nnis);
2307 /* always assume new data */
2308 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2309 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2312 * If there is data present that hasn't been processed yet,
2313 * we'll replace it if the Put contained newer data and it
2314 * fits. We're racing with a Ping or earlier Push in this
2317 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2318 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2319 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2320 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2321 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2322 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2323 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2324 libcfs_nid2str(lp->lp_primary_nid),
2325 LNET_PING_BUFFER_SEQNO(pbuf),
2326 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2332 * Allocate a buffer to copy the data. On a failure we drop
2333 * the Push and set FORCE_PING to force the discovery
2334 * thread to fix the problem by pinging the peer.
2336 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2338 lp->lp_state |= LNET_PEER_FORCE_PING;
2339 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2340 libcfs_nid2str(lp->lp_primary_nid),
2341 LNET_PING_BUFFER_SEQNO(pbuf));
2346 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2347 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2348 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2349 CDEBUG(D_NET, "Received Push %s %u\n",
2350 libcfs_nid2str(lp->lp_primary_nid),
2351 LNET_PING_BUFFER_SEQNO(pbuf));
2354 /* We've processed this buffer. It can be reposted */
2355 pbuf->pb_needs_post = true;
2358 * Queue the peer for discovery if not done, force it on the request
2359 * queue and wake the discovery thread if the peer was already queued,
2360 * because its status changed.
2362 spin_unlock(&lp->lp_lock);
2363 lnet_net_lock(LNET_LOCK_EX);
2364 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2365 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2366 wake_up(&the_lnet.ln_dc_waitq);
2368 /* Drop refcount from lookup */
2369 lnet_peer_decref_locked(lp);
2370 lnet_net_unlock(LNET_LOCK_EX);
2374 * Clear the discovery error state, unless we're already discovering
2375 * this peer, in which case the error is current.
2377 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2379 spin_lock(&lp->lp_lock);
2380 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2381 lp->lp_dc_error = 0;
2382 spin_unlock(&lp->lp_lock);
2386 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2387 * dropped/retaken within this function. An lnet_peer_ni is passed in
2388 * because discovery could tear down an lnet_peer.
2391 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2394 struct lnet_peer *lp;
2399 lnet_net_unlock(cpt);
2400 lnet_net_lock(LNET_LOCK_EX);
2401 lp = lpni->lpni_peer_net->lpn_peer;
2402 lnet_peer_clear_discovery_error(lp);
2405 * We're willing to be interrupted. The lpni can become a
2406 * zombie if we race with DLC, so we must check for that.
2409 /* Keep lp alive when the lnet_net_lock is unlocked */
2410 lnet_peer_addref_locked(lp);
2411 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2412 if (signal_pending(current))
2414 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2417 * Don't repeat discovery if discovery is disabled. This is
2418 * done to ensure we can use discovery as a standard ping as
2419 * well for backwards compatibility with routers which do not
2420 * have discovery or have discovery disabled
2422 if (lnet_is_discovery_disabled(lp) && count > 0)
2424 if (lp->lp_dc_error)
2426 if (lnet_peer_is_uptodate(lp))
2428 lnet_peer_queue_for_discovery(lp);
2430 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2433 * If caller requested a non-blocking operation then
2434 * return immediately. Once discovery is complete any
2435 * pending messages that were stopped due to discovery
2436 * will be transmitted.
2441 lnet_net_unlock(LNET_LOCK_EX);
2443 finish_wait(&lp->lp_dc_waitq, &wait);
2444 lnet_net_lock(LNET_LOCK_EX);
2445 lnet_peer_decref_locked(lp);
2446 /* Peer may have changed */
2447 lp = lpni->lpni_peer_net->lpn_peer;
2449 finish_wait(&lp->lp_dc_waitq, &wait);
2451 lnet_net_unlock(LNET_LOCK_EX);
2453 lnet_peer_decref_locked(lp);
2455 * The peer may have changed, so re-check and rediscover if that turns
2456 * out to have been the case. The reference count on lp ensured that
2457 * even if it was unlinked from lpni the memory could not be recycled.
2458 * Thus the check below is sufficient to determine whether the peer
2459 * changed. If the peer changed, then lp must not be dereferenced.
2461 if (lp != lpni->lpni_peer_net->lpn_peer)
2464 if (signal_pending(current))
2466 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2468 else if (lp->lp_dc_error)
2469 rc = lp->lp_dc_error;
2471 CDEBUG(D_NET, "non-blocking discovery\n");
2472 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2475 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2476 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2477 libcfs_nid2str(lpni->lpni_nid), rc,
2478 (!block) ? "pending discovery" : "discovery complete");
2483 /* Handle an incoming ack for a push. */
2485 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2487 struct lnet_ping_buffer *pbuf;
2489 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2490 spin_lock(&lp->lp_lock);
2491 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2492 lp->lp_push_error = ev->status;
2494 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2496 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2497 spin_unlock(&lp->lp_lock);
2499 CDEBUG(D_NET, "peer %s ev->status %d\n",
2500 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2503 /* Handle a Reply message. This is the reply to a Ping message. */
2505 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2507 struct lnet_ping_buffer *pbuf;
2510 spin_lock(&lp->lp_lock);
2512 lp->lp_disc_src_nid = ev->target.nid;
2515 * If some kind of error happened the contents of message
2516 * cannot be used. Set PING_FAILED to trigger a retry.
2519 lp->lp_state |= LNET_PEER_PING_FAILED;
2520 lp->lp_ping_error = ev->status;
2521 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2523 libcfs_nid2str(lp->lp_primary_nid),
2524 libcfs_nid2str(ev->source.nid));
2528 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2529 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2530 lnet_swap_pinginfo(pbuf);
2533 * A reply with invalid or corrupted info. Set PING_FAILED to
2536 rc = lnet_ping_info_validate(&pbuf->pb_info);
2538 lp->lp_state |= LNET_PEER_PING_FAILED;
2539 lp->lp_ping_error = 0;
2540 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2541 libcfs_nid2str(lp->lp_primary_nid), rc);
2547 * The peer may have discovery disabled at its end. Set
2548 * NO_DISCOVERY as appropriate.
2550 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2551 !lnet_peer_discovery_disabled) {
2552 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2553 libcfs_nid2str(lp->lp_primary_nid));
2554 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2556 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2557 libcfs_nid2str(lp->lp_primary_nid));
2558 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2562 * Update the MULTI_RAIL flag based on the reply. If the peer
2563 * was configured with DLC then the setting should match what
2566 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2567 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2568 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2569 libcfs_nid2str(lp->lp_primary_nid), lp);
2570 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2571 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2572 libcfs_nid2str(lp->lp_primary_nid));
2573 } else if (lnet_peer_discovery_disabled) {
2575 "peer %s(%p) not MR: DD disabled locally\n",
2576 libcfs_nid2str(lp->lp_primary_nid), lp);
2577 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2579 "peer %s(%p) not MR: DD disabled remotely\n",
2580 libcfs_nid2str(lp->lp_primary_nid), lp);
2582 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2583 libcfs_nid2str(lp->lp_primary_nid), lp);
2584 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2585 lnet_peer_clr_non_mr_pref_nids(lp);
2587 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2588 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2589 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2590 libcfs_nid2str(lp->lp_primary_nid));
2592 CERROR("Multi-Rail state vanished from %s\n",
2593 libcfs_nid2str(lp->lp_primary_nid));
2594 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2599 * Make sure we'll allocate the correct size ping buffer when
2602 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2603 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2606 * Check for truncation of the Reply. Clear PING_SENT and set
2607 * PING_FAILED to trigger a retry.
2609 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2610 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2611 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2612 lp->lp_state |= LNET_PEER_PING_FAILED;
2613 lp->lp_ping_error = 0;
2614 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2615 libcfs_nid2str(lp->lp_primary_nid),
2616 pbuf->pb_info.pi_nnis);
2621 * Check the sequence numbers in the reply. These are only
2622 * available if the reply came from a Multi-Rail peer.
2624 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2625 pbuf->pb_info.pi_nnis > 1 &&
2626 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2627 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2628 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2629 libcfs_nid2str(lp->lp_primary_nid),
2630 LNET_PING_BUFFER_SEQNO(pbuf),
2633 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2636 /* We're happy with the state of the data in the buffer. */
2637 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2638 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2639 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2640 lnet_ping_buffer_decref(lp->lp_data);
2642 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2643 lnet_ping_buffer_addref(pbuf);
2646 lp->lp_state &= ~LNET_PEER_PING_SENT;
2647 spin_unlock(&lp->lp_lock);
2649 lnet_net_lock(LNET_LOCK_EX);
2651 * If this peer is a gateway, call the routing callback to
2652 * handle the ping reply
2654 if (lp->lp_rtr_refcount > 0)
2655 lnet_router_discovery_ping_reply(lp);
2656 lnet_net_unlock(LNET_LOCK_EX);
2660 * Send event handling. Only matters for error cases, where we clean
2661 * up state on the peer and peer_ni that would otherwise be updated in
2662 * the REPLY event handler for a successful Ping, and the ACK event
2663 * handler for a successful Push.
2666 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2673 spin_lock(&lp->lp_lock);
2674 if (ev->msg_type == LNET_MSG_GET) {
2675 lp->lp_state &= ~LNET_PEER_PING_SENT;
2676 lp->lp_state |= LNET_PEER_PING_FAILED;
2677 lp->lp_ping_error = ev->status;
2678 } else { /* ev->msg_type == LNET_MSG_PUT */
2679 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2680 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2681 lp->lp_push_error = ev->status;
2683 spin_unlock(&lp->lp_lock);
2684 rc = LNET_REDISCOVER_PEER;
2686 CDEBUG(D_NET, "%s Send to %s: %d\n",
2687 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2688 libcfs_nid2str(ev->target.nid), rc);
2693 * Unlink event handling. This event is only seen if a call to
2694 * LNetMDUnlink() caused the event to be unlinked. If this call was
2695 * made after the event was set up in LNetGet() or LNetPut() then we
2696 * assume the Ping or Push timed out.
2699 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2701 spin_lock(&lp->lp_lock);
2702 /* We've passed through LNetGet() */
2703 if (lp->lp_state & LNET_PEER_PING_SENT) {
2704 lp->lp_state &= ~LNET_PEER_PING_SENT;
2705 lp->lp_state |= LNET_PEER_PING_FAILED;
2706 lp->lp_ping_error = -ETIMEDOUT;
2707 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2708 libcfs_nid2str(lp->lp_primary_nid));
2710 /* We've passed through LNetPut() */
2711 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2712 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2713 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2714 lp->lp_push_error = -ETIMEDOUT;
2715 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2716 libcfs_nid2str(lp->lp_primary_nid));
2718 spin_unlock(&lp->lp_lock);
2722 * Event handler for the discovery EQ.
2724 * Called with lnet_res_lock(cpt) held. The cpt is the
2725 * lnet_cpt_of_cookie() of the md handle cookie.
2727 static void lnet_discovery_event_handler(struct lnet_event *event)
2729 struct lnet_peer *lp = event->md_user_ptr;
2730 struct lnet_ping_buffer *pbuf;
2733 /* discovery needs to take another look */
2734 rc = LNET_REDISCOVER_PEER;
2736 CDEBUG(D_NET, "Received event: %d\n", event->type);
2738 switch (event->type) {
2739 case LNET_EVENT_ACK:
2740 lnet_discovery_event_ack(lp, event);
2742 case LNET_EVENT_REPLY:
2743 lnet_discovery_event_reply(lp, event);
2745 case LNET_EVENT_SEND:
2746 /* Only send failure triggers a retry. */
2747 rc = lnet_discovery_event_send(lp, event);
2749 case LNET_EVENT_UNLINK:
2750 /* LNetMDUnlink() was called */
2751 lnet_discovery_event_unlink(lp, event);
2754 /* Invalid events. */
2757 lnet_net_lock(LNET_LOCK_EX);
2758 if (event->unlinked) {
2759 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2760 lnet_ping_buffer_decref(pbuf);
2761 lnet_peer_decref_locked(lp);
2764 /* put peer back at end of request queue, if discovery not already
2766 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2767 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2768 wake_up(&the_lnet.ln_dc_waitq);
2770 lnet_net_unlock(LNET_LOCK_EX);
2774 * Build a peer from incoming data.
2776 * The NIDs in the incoming data are supposed to be structured as follows:
2779 * - other NIDs in same net
2780 * - NIDs in second net
2781 * - NIDs in third net
2783 * This due to the way the list of NIDs in the data is created.
2785 * Note that this function will mark the peer uptodate unless an
2786 * ENOMEM is encontered. All other errors are due to a conflict
2787 * between the DLC configuration and what discovery sees. We treat DLC
2788 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2789 * peer from becoming stuck in discovery.
2791 static int lnet_peer_merge_data(struct lnet_peer *lp,
2792 struct lnet_ping_buffer *pbuf)
2794 struct lnet_peer_ni *lpni;
2795 lnet_nid_t *curnis = NULL;
2796 struct lnet_ni_status *addnis = NULL;
2797 lnet_nid_t *delnis = NULL;
2807 flags = LNET_PEER_DISCOVERED;
2808 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2809 flags |= LNET_PEER_MULTI_RAIL;
2812 * Cache the routing feature for the peer; whether it is enabled
2813 * for disabled as reported by the remote peer.
2815 spin_lock(&lp->lp_lock);
2816 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2817 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2819 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2820 spin_unlock(&lp->lp_lock);
2822 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2823 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2824 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2825 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2826 if (!curnis || !addnis || !delnis) {
2834 /* Construct the list of NIDs present in peer. */
2836 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2837 curnis[ncurnis++] = lpni->lpni_nid;
2840 * Check for NIDs in pbuf not present in curnis[].
2841 * The loop starts at 1 to skip the loopback NID.
2843 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2844 for (j = 0; j < ncurnis; j++)
2845 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2848 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2851 * Check for NIDs in curnis[] not present in pbuf.
2852 * The nested loop starts at 1 to skip the loopback NID.
2854 * But never add the loopback NID to delnis[]: if it is
2855 * present in curnis[] then this peer is for this node.
2857 for (i = 0; i < ncurnis; i++) {
2858 if (curnis[i] == LNET_NID_LO_0)
2860 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2861 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2863 * update the information we cache for the
2864 * peer with the latest information we
2867 lpni = lnet_find_peer_ni_locked(curnis[i]);
2869 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2870 lnet_peer_ni_decref_locked(lpni);
2875 if (j == pbuf->pb_info.pi_nnis)
2876 delnis[ndelnis++] = curnis[i];
2880 * If we get here and the discovery is disabled then we don't want
2881 * to add or delete any NIs. We just updated the ones we have some
2882 * information on, and call it a day
2885 if (lnet_is_discovery_disabled(lp))
2888 for (i = 0; i < naddnis; i++) {
2889 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2891 CERROR("Error adding NID %s to peer %s: %d\n",
2892 libcfs_nid2str(addnis[i].ns_nid),
2893 libcfs_nid2str(lp->lp_primary_nid), rc);
2897 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2899 lpni->lpni_ns_status = addnis[i].ns_status;
2900 lnet_peer_ni_decref_locked(lpni);
2904 for (i = 0; i < ndelnis; i++) {
2906 * for routers it's okay to delete the primary_nid because
2907 * the upper layers don't really rely on it. So if we're
2908 * being told that the router changed its primary_nid
2909 * then it's okay to delete it.
2911 if (lp->lp_rtr_refcount > 0)
2912 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2913 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2915 CERROR("Error deleting NID %s from peer %s: %d\n",
2916 libcfs_nid2str(delnis[i]),
2917 libcfs_nid2str(lp->lp_primary_nid), rc);
2923 * Errors other than -ENOMEM are due to peers having been
2924 * configured with DLC. Ignore these because DLC overrides
2929 CFS_FREE_PTR_ARRAY(curnis, nnis);
2930 CFS_FREE_PTR_ARRAY(addnis, nnis);
2931 CFS_FREE_PTR_ARRAY(delnis, nnis);
2932 lnet_ping_buffer_decref(pbuf);
2933 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2936 spin_lock(&lp->lp_lock);
2937 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2938 lp->lp_state |= LNET_PEER_FORCE_PING;
2939 spin_unlock(&lp->lp_lock);
2945 * The data in pbuf says lp is its primary peer, but the data was
2946 * received by a different peer. Try to update lp with the data.
2949 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2951 struct lnet_handle_md mdh;
2953 /* Queue lp for discovery, and force it on the request queue. */
2954 lnet_net_lock(LNET_LOCK_EX);
2955 if (lnet_peer_queue_for_discovery(lp))
2956 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2957 lnet_net_unlock(LNET_LOCK_EX);
2959 LNetInvalidateMDHandle(&mdh);
2962 * Decide whether we can move the peer to the DATA_PRESENT state.
2964 * We replace stale data for a multi-rail peer, repair PING_FAILED
2965 * status, and preempt FORCE_PING.
2967 * If after that we have DATA_PRESENT, we merge it into this peer.
2969 spin_lock(&lp->lp_lock);
2970 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2971 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2972 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2973 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2974 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2975 lnet_ping_buffer_decref(pbuf);
2980 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2981 lnet_ping_buffer_decref(lp->lp_data);
2983 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2985 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2986 mdh = lp->lp_ping_mdh;
2987 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2988 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2989 lp->lp_ping_error = 0;
2991 if (lp->lp_state & LNET_PEER_FORCE_PING)
2992 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2993 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2994 spin_unlock(&lp->lp_lock);
2996 if (!LNetMDHandleIsInvalid(mdh))
3000 return lnet_peer_merge_data(lp, pbuf);
3002 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3006 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
3010 for (i = 0; i < pinfo->pi_nnis; i++) {
3011 if (pinfo->pi_ni[i].ns_nid == nid)
3018 /* Delete a peer that has been marked for deletion. NB: when this peer was added
3019 * to the discovery queue a reference was taken that will prevent the peer from
3020 * actually being freed by this function. After this function exits the
3021 * discovery thread should call lnet_peer_discovery_complete() which will
3022 * drop that reference as well as wake any waiters that may also be holding a
3025 static int lnet_peer_deletion(struct lnet_peer *lp)
3026 __must_hold(&lp->lp_lock)
3028 struct list_head rlist;
3029 struct lnet_route *route, *tmp;
3030 int sensitivity = lp->lp_health_sensitivity;
3032 INIT_LIST_HEAD(&rlist);
3034 lp->lp_state &= ~(LNET_PEER_DISCOVERING | LNET_PEER_FORCE_PING |
3035 LNET_PEER_FORCE_PUSH);
3036 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3037 libcfs_nid2str(lp->lp_primary_nid), lp, lp->lp_state);
3039 /* no-op if lnet_peer_del() has already been called on this peer */
3040 if (lp->lp_state & LNET_PEER_MARK_DELETED)
3043 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
3046 spin_unlock(&lp->lp_lock);
3048 mutex_lock(&the_lnet.ln_api_mutex);
3050 lnet_net_lock(LNET_LOCK_EX);
3051 /* remove the peer from the discovery work
3052 * queue if it's on there in preparation
3055 if (!list_empty(&lp->lp_dc_list))
3056 list_del(&lp->lp_dc_list);
3057 list_for_each_entry_safe(route, tmp,
3060 lnet_move_route(route, NULL, &rlist);
3061 lnet_net_unlock(LNET_LOCK_EX);
3063 /* lnet_peer_del() deletes all the peer NIs owned by this peer */
3066 list_for_each_entry_safe(route, tmp,
3068 /* re-add these routes */
3069 lnet_add_route(route->lr_net,
3074 LIBCFS_FREE(route, sizeof(*route));
3077 mutex_unlock(&the_lnet.ln_api_mutex);
3079 spin_lock(&lp->lp_lock);
3085 * Update a peer using the data received.
3087 static int lnet_peer_data_present(struct lnet_peer *lp)
3088 __must_hold(&lp->lp_lock)
3090 struct lnet_ping_buffer *pbuf;
3091 struct lnet_peer_ni *lpni;
3092 lnet_nid_t nid = LNET_NID_ANY;
3098 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3099 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3100 spin_unlock(&lp->lp_lock);
3103 * Modifications of peer structures are done while holding the
3104 * ln_api_mutex. A global lock is required because we may be
3105 * modifying multiple peer structures, and a mutex greatly
3106 * simplifies memory management.
3108 * The actual changes to the data structures must also protect
3109 * against concurrent lookups, for which the lnet_net_lock in
3110 * LNET_LOCK_EX mode is used.
3112 mutex_lock(&the_lnet.ln_api_mutex);
3113 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3119 * If this peer is not on the peer list then it is being torn
3120 * down, and our reference count may be all that is keeping it
3121 * alive. Don't do any work on it.
3123 if (list_empty(&lp->lp_peer_list))
3126 flags = LNET_PEER_DISCOVERED;
3127 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
3128 flags |= LNET_PEER_MULTI_RAIL;
3131 * Check whether the primary NID in the message matches the
3132 * primary NID of the peer. If it does, update the peer, if
3133 * it it does not, check whether there is already a peer with
3134 * that primary NID. If no such peer exists, try to update
3135 * the primary NID of the current peer (allowed if it was
3136 * created due to message traffic) and complete the update.
3137 * If the peer did exist, hand off the data to it.
3139 * The peer for the loopback interface is a special case: this
3140 * is the peer for the local node, and we want to set its
3141 * primary NID to the correct value here. Moreover, this peer
3142 * can show up with only the loopback NID in the ping buffer.
3144 if (pbuf->pb_info.pi_nnis <= 1)
3146 nid = pbuf->pb_info.pi_ni[1].ns_nid;
3147 if (lp->lp_primary_nid == LNET_NID_LO_0) {
3148 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3150 rc = lnet_peer_merge_data(lp, pbuf);
3152 * if the primary nid of the peer is present in the ping info returned
3153 * from the peer, but it's not the local primary peer we have
3154 * cached and discovery is disabled, then we don't want to update
3155 * our local peer info, by adding or removing NIDs, we just want
3156 * to update the status of the nids that we currently have
3157 * recorded in that peer.
3159 } else if (lp->lp_primary_nid == nid ||
3160 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
3161 lnet_is_discovery_disabled(lp))) {
3162 rc = lnet_peer_merge_data(lp, pbuf);
3164 lpni = lnet_find_peer_ni_locked(nid);
3165 if (!lpni || lp == lpni->lpni_peer_net->lpn_peer) {
3166 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3168 CERROR("Primary NID error %s versus %s: %d\n",
3169 libcfs_nid2str(lp->lp_primary_nid),
3170 libcfs_nid2str(nid), rc);
3172 rc = lnet_peer_merge_data(lp, pbuf);
3175 lnet_peer_ni_decref_locked(lpni);
3177 struct lnet_peer *new_lp;
3178 new_lp = lpni->lpni_peer_net->lpn_peer;
3180 * if lp has discovery/MR enabled that means new_lp
3181 * should have discovery/MR enabled as well, since
3182 * it's the same peer, which we're about to merge
3184 spin_lock(&lp->lp_lock);
3185 spin_lock(&new_lp->lp_lock);
3186 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
3187 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
3188 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
3189 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
3190 /* If we're processing a ping reply then we may be
3191 * about to send a push to the peer that we ping'd.
3192 * Since the ping reply that we're processing was
3193 * received by lp, we need to set the discovery source
3194 * NID for new_lp to the NID stored in lp.
3196 if (lp->lp_disc_src_nid != LNET_NID_ANY)
3197 new_lp->lp_disc_src_nid = lp->lp_disc_src_nid;
3198 spin_unlock(&new_lp->lp_lock);
3199 spin_unlock(&lp->lp_lock);
3201 rc = lnet_peer_set_primary_data(new_lp, pbuf);
3202 lnet_consolidate_routes_locked(lp, new_lp);
3203 lnet_peer_ni_decref_locked(lpni);
3207 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
3209 mutex_unlock(&the_lnet.ln_api_mutex);
3211 spin_lock(&lp->lp_lock);
3212 /* Tell discovery to re-check the peer immediately. */
3214 rc = LNET_REDISCOVER_PEER;
3219 * A ping failed. Clear the PING_FAILED state and set the
3220 * FORCE_PING state, to ensure a retry even if discovery is
3221 * disabled. This avoids being left with incorrect state.
3223 static int lnet_peer_ping_failed(struct lnet_peer *lp)
3224 __must_hold(&lp->lp_lock)
3226 struct lnet_handle_md mdh;
3229 mdh = lp->lp_ping_mdh;
3230 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3231 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3232 lp->lp_state |= LNET_PEER_FORCE_PING;
3233 rc = lp->lp_ping_error;
3234 lp->lp_ping_error = 0;
3235 spin_unlock(&lp->lp_lock);
3237 if (!LNetMDHandleIsInvalid(mdh))
3240 CDEBUG(D_NET, "peer %s:%d\n",
3241 libcfs_nid2str(lp->lp_primary_nid), rc);
3243 spin_lock(&lp->lp_lock);
3244 return rc ? rc : LNET_REDISCOVER_PEER;
3248 * Select NID to send a Ping or Push to.
3250 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
3252 struct lnet_peer_ni *lpni;
3254 /* Look for a direct-connected NID for this peer. */
3256 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3257 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
3262 return lpni->lpni_nid;
3264 /* Look for a routed-connected NID for this peer. */
3266 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3267 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
3272 return lpni->lpni_nid;
3274 return LNET_NID_ANY;
3277 /* Active side of ping. */
3278 static int lnet_peer_send_ping(struct lnet_peer *lp)
3279 __must_hold(&lp->lp_lock)
3286 lp->lp_state |= LNET_PEER_PING_SENT;
3287 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3288 spin_unlock(&lp->lp_lock);
3290 cpt = lnet_net_lock_current();
3291 /* Refcount for MD. */
3292 lnet_peer_addref_locked(lp);
3293 pnid = lnet_peer_select_nid(lp);
3294 lnet_net_unlock(cpt);
3296 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3298 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
3299 the_lnet.ln_dc_handler, false);
3302 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3303 * refcount on the peer, otherwise LNetMDUnlink will be called
3304 * which will eventually do that.
3308 lnet_peer_decref_locked(lp);
3309 lnet_net_unlock(cpt);
3310 rc = -rc; /* change the rc to negative value */
3312 } else if (rc < 0) {
3316 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3318 spin_lock(&lp->lp_lock);
3322 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3324 * The errors that get us here are considered hard errors and
3325 * cause Discovery to terminate. So we clear PING_SENT, but do
3326 * not set either PING_FAILED or FORCE_PING. In fact we need
3327 * to clear PING_FAILED, because the unlink event handler will
3328 * have set it if we called LNetMDUnlink() above.
3330 spin_lock(&lp->lp_lock);
3331 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3336 * This function exists because you cannot call LNetMDUnlink() from an
3339 static int lnet_peer_push_failed(struct lnet_peer *lp)
3340 __must_hold(&lp->lp_lock)
3342 struct lnet_handle_md mdh;
3345 mdh = lp->lp_push_mdh;
3346 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3347 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3348 rc = lp->lp_push_error;
3349 lp->lp_push_error = 0;
3350 spin_unlock(&lp->lp_lock);
3352 if (!LNetMDHandleIsInvalid(mdh))
3355 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3356 spin_lock(&lp->lp_lock);
3357 return rc ? rc : LNET_REDISCOVER_PEER;
3361 * Mark the peer as discovered.
3363 static int lnet_peer_discovered(struct lnet_peer *lp)
3364 __must_hold(&lp->lp_lock)
3366 lp->lp_state |= LNET_PEER_DISCOVERED;
3367 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3368 LNET_PEER_REDISCOVER);
3370 lp->lp_dc_error = 0;
3372 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3377 /* Active side of push. */
3378 static int lnet_peer_send_push(struct lnet_peer *lp)
3379 __must_hold(&lp->lp_lock)
3381 struct lnet_ping_buffer *pbuf;
3382 struct lnet_process_id id;
3387 /* Don't push to a non-multi-rail peer. */
3388 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3389 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3390 /* if peer's NIDs are uptodate then peer is discovered */
3391 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3392 rc = lnet_peer_discovered(lp);
3399 lp->lp_state |= LNET_PEER_PUSH_SENT;
3400 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3401 spin_unlock(&lp->lp_lock);
3403 cpt = lnet_net_lock_current();
3404 pbuf = the_lnet.ln_ping_target;
3405 lnet_ping_buffer_addref(pbuf);
3406 lnet_net_unlock(cpt);
3408 /* Push source MD */
3409 md.start = &pbuf->pb_info;
3410 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3411 md.threshold = 2; /* Put/Ack */
3413 md.options = LNET_MD_TRACK_RESPONSE;
3414 md.handler = the_lnet.ln_dc_handler;
3417 rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3419 lnet_ping_buffer_decref(pbuf);
3420 CERROR("Can't bind push source MD: %d\n", rc);
3423 cpt = lnet_net_lock_current();
3424 /* Refcount for MD. */
3425 lnet_peer_addref_locked(lp);
3426 id.pid = LNET_PID_LUSTRE;
3427 id.nid = lnet_peer_select_nid(lp);
3428 lnet_net_unlock(cpt);
3430 if (id.nid == LNET_NID_ANY) {
3435 rc = LNetPut(lp->lp_disc_src_nid, lp->lp_push_mdh,
3436 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3437 LNET_PROTO_PING_MATCHBITS, 0, 0);
3440 * reset the discovery nid. There is no need to restrict sending
3441 * from that source, if we call lnet_push_update_to_peers(). It'll
3442 * get set to a specific NID, if we initiate discovery from the
3445 lp->lp_disc_src_nid = LNET_NID_ANY;
3450 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3452 spin_lock(&lp->lp_lock);
3456 LNetMDUnlink(lp->lp_push_mdh);
3457 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3459 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3461 * The errors that get us here are considered hard errors and
3462 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3463 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3464 * because the unlink event handler will have set it if we
3465 * called LNetMDUnlink() above.
3467 spin_lock(&lp->lp_lock);
3468 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3473 * An unrecoverable error was encountered during discovery.
3474 * Set error status in peer and abort discovery.
3476 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3478 CDEBUG(D_NET, "Discovery error %s: %d\n",
3479 libcfs_nid2str(lp->lp_primary_nid), error);
3481 spin_lock(&lp->lp_lock);
3482 lp->lp_dc_error = error;
3483 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3484 lp->lp_state |= LNET_PEER_REDISCOVER;
3485 spin_unlock(&lp->lp_lock);
3489 * Wait for work to be queued or some other change that must be
3490 * attended to. Returns non-zero if the discovery thread should shut
3493 static int lnet_peer_discovery_wait_for_work(void)
3500 cpt = lnet_net_lock_current();
3502 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3503 TASK_INTERRUPTIBLE);
3504 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3506 if (lnet_push_target_resize_needed() ||
3507 the_lnet.ln_push_target->pb_needs_post)
3509 if (!list_empty(&the_lnet.ln_dc_request))
3511 if (!list_empty(&the_lnet.ln_msg_resend))
3513 lnet_net_unlock(cpt);
3516 * wakeup max every second to check if there are peers that
3517 * have been stuck on the working queue for greater than
3520 schedule_timeout(cfs_time_seconds(1));
3521 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3522 cpt = lnet_net_lock_current();
3524 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3526 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3529 lnet_net_unlock(cpt);
3531 CDEBUG(D_NET, "woken: %d\n", rc);
3537 * Messages that were pending on a destroyed peer will be put on a global
3538 * resend list. The message resend list will be checked by
3539 * the discovery thread when it wakes up, and will resend messages. These
3540 * messages can still be sendable in the case the lpni which was the initial
3541 * cause of the message re-queue was transfered to another peer.
3543 * It is possible that LNet could be shutdown while we're iterating
3544 * through the list. lnet_shudown_lndnets() will attempt to access the
3545 * resend list, but will have to wait until the spinlock is released, by
3546 * which time there shouldn't be any more messages on the resend list.
3547 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3548 * for the messages so they can be released. The other case is that
3549 * lnet_shudown_lndnets() can finalize all the messages before this
3550 * function can visit the resend list, in which case this function will be
3553 static void lnet_resend_msgs(void)
3555 struct lnet_msg *msg, *tmp;
3559 spin_lock(&the_lnet.ln_msg_resend_lock);
3560 list_splice(&the_lnet.ln_msg_resend, &resend);
3561 spin_unlock(&the_lnet.ln_msg_resend_lock);
3563 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3564 list_del_init(&msg->msg_list);
3565 rc = lnet_send(msg->msg_src_nid_param, msg,
3566 msg->msg_rtr_nid_param);
3568 CNETERR("Error sending %s to %s: %d\n",
3569 lnet_msgtyp2str(msg->msg_type),
3570 libcfs_id2str(msg->msg_target), rc);
3571 lnet_finalize(msg, rc);
3576 /* The discovery thread. */
3577 static int lnet_peer_discovery(void *arg)
3579 struct lnet_peer *lp;
3582 wait_for_completion(&the_lnet.ln_started);
3584 CDEBUG(D_NET, "started\n");
3587 if (lnet_peer_discovery_wait_for_work())
3590 if (lnet_push_target_resize_needed())
3591 lnet_push_target_resize();
3592 else if (the_lnet.ln_push_target->pb_needs_post)
3593 lnet_push_target_post(the_lnet.ln_push_target,
3594 &the_lnet.ln_push_target_md);
3598 lnet_net_lock(LNET_LOCK_EX);
3599 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3600 lnet_net_unlock(LNET_LOCK_EX);
3605 * Process all incoming discovery work requests. When
3606 * discovery must wait on a peer to change state, it
3607 * is added to the tail of the ln_dc_working queue. A
3608 * timestamp keeps track of when the peer was added,
3609 * so we can time out discovery requests that take too
3612 while (!list_empty(&the_lnet.ln_dc_request)) {
3613 lp = list_first_entry(&the_lnet.ln_dc_request,
3614 struct lnet_peer, lp_dc_list);
3615 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3617 * set the time the peer was put on the dc_working
3618 * queue. It shouldn't remain on the queue
3619 * forever, in case the GET message (for ping)
3620 * doesn't get a REPLY or the PUT message (for
3621 * push) doesn't get an ACK.
3623 lp->lp_last_queued = ktime_get_real_seconds();
3624 lnet_net_unlock(LNET_LOCK_EX);
3626 if (lnet_push_target_resize_needed())
3627 lnet_push_target_resize();
3628 else if (the_lnet.ln_push_target->pb_needs_post)
3629 lnet_push_target_post(the_lnet.ln_push_target,
3630 &the_lnet.ln_push_target_md);
3633 * Select an action depending on the state of
3634 * the peer and whether discovery is disabled.
3635 * The check whether discovery is disabled is
3636 * done after the code that handles processing
3637 * for arrived data, cleanup for failures, and
3638 * forcing a Ping or Push.
3640 spin_lock(&lp->lp_lock);
3641 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3642 libcfs_nid2str(lp->lp_primary_nid), lp,
3644 if (lp->lp_state & (LNET_PEER_MARK_DELETION |
3645 LNET_PEER_MARK_DELETED))
3646 rc = lnet_peer_deletion(lp);
3647 else if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3648 rc = lnet_peer_data_present(lp);
3649 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3650 rc = lnet_peer_ping_failed(lp);
3651 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3652 rc = lnet_peer_push_failed(lp);
3653 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3654 rc = lnet_peer_send_ping(lp);
3655 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3656 rc = lnet_peer_send_push(lp);
3657 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3658 rc = lnet_peer_send_ping(lp);
3659 else if (lnet_peer_needs_push(lp))
3660 rc = lnet_peer_send_push(lp);
3662 rc = lnet_peer_discovered(lp);
3663 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3664 libcfs_nid2str(lp->lp_primary_nid), lp,
3666 spin_unlock(&lp->lp_lock);
3668 lnet_net_lock(LNET_LOCK_EX);
3669 if (rc == LNET_REDISCOVER_PEER) {
3670 list_move(&lp->lp_dc_list,
3671 &the_lnet.ln_dc_request);
3673 lnet_peer_discovery_error(lp, rc);
3675 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3676 lnet_peer_discovery_complete(lp);
3677 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3682 lnet_net_unlock(LNET_LOCK_EX);
3685 CDEBUG(D_NET, "stopping\n");
3687 * Clean up before telling lnet_peer_discovery_stop() that
3688 * we're done. Use wake_up() below to somewhat reduce the
3689 * size of the thundering herd if there are multiple threads
3690 * waiting on discovery of a single peer.
3693 /* Queue cleanup 1: stop all pending pings and pushes. */
3694 lnet_net_lock(LNET_LOCK_EX);
3695 while (!list_empty(&the_lnet.ln_dc_working)) {
3696 lp = list_first_entry(&the_lnet.ln_dc_working,
3697 struct lnet_peer, lp_dc_list);
3698 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3699 lnet_net_unlock(LNET_LOCK_EX);
3700 lnet_peer_cancel_discovery(lp);
3701 lnet_net_lock(LNET_LOCK_EX);
3703 lnet_net_unlock(LNET_LOCK_EX);
3705 /* Queue cleanup 2: wait for the expired queue to clear. */
3706 while (!list_empty(&the_lnet.ln_dc_expired))
3707 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3709 /* Queue cleanup 3: clear the request queue. */
3710 lnet_net_lock(LNET_LOCK_EX);
3711 while (!list_empty(&the_lnet.ln_dc_request)) {
3712 lp = list_first_entry(&the_lnet.ln_dc_request,
3713 struct lnet_peer, lp_dc_list);
3714 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3715 lnet_peer_discovery_complete(lp);
3717 lnet_net_unlock(LNET_LOCK_EX);
3719 lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3720 the_lnet.ln_dc_handler = NULL;
3722 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3723 wake_up(&the_lnet.ln_dc_waitq);
3725 CDEBUG(D_NET, "stopped\n");
3730 /* ln_api_mutex is held on entry. */
3731 int lnet_peer_discovery_start(void)
3733 struct task_struct *task;
3736 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3739 the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3740 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3741 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3744 CERROR("Can't start peer discovery thread: %d\n", rc);
3746 the_lnet.ln_dc_handler = NULL;
3748 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3751 CDEBUG(D_NET, "discovery start: %d\n", rc);
3756 /* ln_api_mutex is held on entry. */
3757 void lnet_peer_discovery_stop(void)
3759 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3762 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3763 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3765 /* In the LNetNIInit() path we may be stopping discovery before it
3766 * entered its work loop
3768 if (!completion_done(&the_lnet.ln_started))
3769 complete(&the_lnet.ln_started);
3771 wake_up(&the_lnet.ln_dc_waitq);
3773 wait_event(the_lnet.ln_dc_waitq,
3774 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3776 LASSERT(list_empty(&the_lnet.ln_dc_request));
3777 LASSERT(list_empty(&the_lnet.ln_dc_working));
3778 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3780 CDEBUG(D_NET, "discovery stopped\n");
3786 lnet_debug_peer(lnet_nid_t nid)
3788 char *aliveness = "NA";
3789 struct lnet_peer_ni *lp;
3792 cpt = lnet_cpt_of_nid(nid, NULL);
3795 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3797 lnet_net_unlock(cpt);
3798 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3802 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3803 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3805 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3806 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3807 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3808 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3809 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3811 lnet_peer_ni_decref_locked(lp);
3813 lnet_net_unlock(cpt);
3816 /* Gathering information for userspace. */
3818 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3819 char aliveness[LNET_MAX_STR_LEN],
3820 __u32 *cpt_iter, __u32 *refcount,
3821 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3822 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3823 __u32 *peer_tx_qnob)
3825 struct lnet_peer_table *peer_table;
3826 struct lnet_peer_ni *lp;
3831 /* get the number of CPTs */
3832 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3834 /* if the cpt number to be examined is >= the number of cpts in
3835 * the system then indicate that there are no more cpts to examin
3837 if (*cpt_iter >= lncpt)
3840 /* get the current table */
3841 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3842 /* if the ptable is NULL then there are no more cpts to examine */
3843 if (peer_table == NULL)
3846 lnet_net_lock(*cpt_iter);
3848 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3849 struct list_head *peers = &peer_table->pt_hash[j];
3851 list_for_each_entry(lp, peers, lpni_hashlist) {
3852 if (peer_index-- > 0)
3855 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3856 if (lnet_isrouter(lp) ||
3857 lnet_peer_aliveness_enabled(lp))
3858 snprintf(aliveness, LNET_MAX_STR_LEN,
3859 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3861 *nid = lp->lpni_nid;
3862 *refcount = atomic_read(&lp->lpni_refcount);
3863 *ni_peer_tx_credits =
3864 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3865 *peer_tx_credits = lp->lpni_txcredits;
3866 *peer_rtr_credits = lp->lpni_rtrcredits;
3867 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3868 *peer_tx_qnob = lp->lpni_txqnob;
3874 lnet_net_unlock(*cpt_iter);
3878 return found ? 0 : -ENOENT;
3881 /* ln_api_mutex is held, which keeps the peer list stable */
3882 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3884 struct lnet_ioctl_element_stats *lpni_stats;
3885 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3886 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3887 struct lnet_peer_ni_credit_info *lpni_info;
3888 struct lnet_peer_ni *lpni;
3889 struct lnet_peer *lp;
3894 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3901 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3902 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3903 size *= lp->lp_nnis;
3904 if (size > cfg->prcfg_size) {
3905 cfg->prcfg_size = size;
3910 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3911 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3912 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3913 cfg->prcfg_count = lp->lp_nnis;
3914 cfg->prcfg_size = size;
3915 cfg->prcfg_state = lp->lp_state;
3917 /* Allocate helper buffers. */
3919 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3922 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3925 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3926 if (!lpni_msg_stats)
3927 goto out_free_stats;
3928 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3930 goto out_free_msg_stats;
3935 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3936 nid = lpni->lpni_nid;
3937 if (copy_to_user(bulk, &nid, sizeof(nid)))
3938 goto out_free_hstats;
3939 bulk += sizeof(nid);
3941 memset(lpni_info, 0, sizeof(*lpni_info));
3942 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3943 if (lnet_isrouter(lpni) ||
3944 lnet_peer_aliveness_enabled(lpni))
3945 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3946 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3948 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3949 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3950 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3951 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3952 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3953 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3954 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3955 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3956 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3957 goto out_free_hstats;
3958 bulk += sizeof(*lpni_info);
3960 memset(lpni_stats, 0, sizeof(*lpni_stats));
3961 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3962 LNET_STATS_TYPE_SEND);
3963 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3964 LNET_STATS_TYPE_RECV);
3965 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3966 LNET_STATS_TYPE_DROP);
3967 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3968 goto out_free_hstats;
3969 bulk += sizeof(*lpni_stats);
3970 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3971 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3972 goto out_free_hstats;
3973 bulk += sizeof(*lpni_msg_stats);
3974 lpni_hstats->hlpni_network_timeout =
3975 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3976 lpni_hstats->hlpni_remote_dropped =
3977 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3978 lpni_hstats->hlpni_remote_timeout =
3979 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3980 lpni_hstats->hlpni_remote_error =
3981 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3982 lpni_hstats->hlpni_health_value =
3983 atomic_read(&lpni->lpni_healthv);
3984 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3985 goto out_free_hstats;
3986 bulk += sizeof(*lpni_hstats);
3991 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3993 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3995 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3997 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3999 lnet_peer_decref_locked(lp);
4005 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
4007 /* the mt could've shutdown and cleaned up the queues */
4008 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
4011 if (list_empty(&lpni->lpni_recovery) &&
4012 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
4013 CDEBUG(D_NET, "lpni %s added to recovery queue. Health = %d\n",
4014 libcfs_nid2str(lpni->lpni_nid),
4015 atomic_read(&lpni->lpni_healthv));
4016 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
4017 lnet_peer_ni_addref_locked(lpni);
4021 /* Call with the ln_api_mutex held */
4023 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
4025 struct lnet_peer_table *ptable;
4026 struct lnet_peer *lp;
4027 struct lnet_peer_net *lpn;
4028 struct lnet_peer_ni *lpni;
4032 if (the_lnet.ln_state != LNET_STATE_RUNNING)
4036 lnet_net_lock(LNET_LOCK_EX);
4037 lpni = lnet_find_peer_ni_locked(nid);
4039 lnet_net_unlock(LNET_LOCK_EX);
4042 atomic_set(&lpni->lpni_healthv, value);
4043 lnet_peer_ni_add_to_recoveryq_locked(lpni);
4044 lnet_peer_ni_decref_locked(lpni);
4045 lnet_net_unlock(LNET_LOCK_EX);
4049 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
4052 * Walk all the peers and reset the healhv for each one to the
4055 lnet_net_lock(LNET_LOCK_EX);
4056 for (cpt = 0; cpt < lncpt; cpt++) {
4057 ptable = the_lnet.ln_peer_tables[cpt];
4058 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
4059 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
4060 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
4062 atomic_set(&lpni->lpni_healthv, value);
4063 lnet_peer_ni_add_to_recoveryq_locked(lpni);
4068 lnet_net_unlock(LNET_LOCK_EX);