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/
34 #define DEBUG_SUBSYSTEM S_LNET
36 #include <linux/sched.h>
37 #ifdef HAVE_SCHED_HEADERS
38 #include <linux/sched/signal.h>
40 #include <linux/uaccess.h>
42 #include <lnet/udsp.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER (1)
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
54 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
55 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
56 lnet_peer_ni_decref_locked(lpni);
61 lnet_peer_net_added(struct lnet_net *net)
63 struct lnet_peer_ni *lpni, *tmp;
65 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66 lpni_on_remote_peer_ni_list) {
68 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
71 spin_lock(&lpni->lpni_lock);
72 lpni->lpni_txcredits =
73 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
74 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
75 lpni->lpni_rtrcredits =
76 lnet_peer_buffer_credits(lpni->lpni_net);
77 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
78 spin_unlock(&lpni->lpni_lock);
80 lnet_peer_remove_from_remote_list(lpni);
86 lnet_peer_tables_destroy(void)
88 struct lnet_peer_table *ptable;
89 struct list_head *hash;
93 if (!the_lnet.ln_peer_tables)
96 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
97 hash = ptable->pt_hash;
98 if (!hash) /* not intialized */
101 LASSERT(list_empty(&ptable->pt_zombie_list));
103 ptable->pt_hash = NULL;
104 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
105 LASSERT(list_empty(&hash[j]));
107 CFS_FREE_PTR_ARRAY(hash, LNET_PEER_HASH_SIZE);
110 cfs_percpt_free(the_lnet.ln_peer_tables);
111 the_lnet.ln_peer_tables = NULL;
115 lnet_peer_tables_create(void)
117 struct lnet_peer_table *ptable;
118 struct list_head *hash;
122 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
124 if (the_lnet.ln_peer_tables == NULL) {
125 CERROR("Failed to allocate cpu-partition peer tables\n");
129 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
130 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
131 LNET_PEER_HASH_SIZE * sizeof(*hash));
133 CERROR("Failed to create peer hash table\n");
134 lnet_peer_tables_destroy();
138 spin_lock_init(&ptable->pt_zombie_lock);
139 INIT_LIST_HEAD(&ptable->pt_zombie_list);
141 INIT_LIST_HEAD(&ptable->pt_peer_list);
143 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
144 INIT_LIST_HEAD(&hash[j]);
145 ptable->pt_hash = hash; /* sign of initialization */
151 static struct lnet_peer_ni *
152 lnet_peer_ni_alloc(lnet_nid_t nid)
154 struct lnet_peer_ni *lpni;
155 struct lnet_net *net;
158 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
160 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
164 INIT_LIST_HEAD(&lpni->lpni_txq);
165 INIT_LIST_HEAD(&lpni->lpni_hashlist);
166 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
167 INIT_LIST_HEAD(&lpni->lpni_recovery);
168 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
169 INIT_LIST_HEAD(&lpni->lpni_rtr_pref_nids);
170 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
171 kref_init(&lpni->lpni_kref);
172 lpni->lpni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
174 spin_lock_init(&lpni->lpni_lock);
176 if (lnet_peers_start_down())
177 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
179 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
180 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
181 lpni->lpni_nid = nid;
182 lpni->lpni_cpt = cpt;
183 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
185 net = lnet_get_net_locked(LNET_NIDNET(nid));
186 lpni->lpni_net = net;
188 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
189 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
190 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
191 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
194 * This peer_ni is not on a local network, so we
195 * cannot add the credits here. In case the net is
196 * added later, add the peer_ni to the remote peer ni
197 * list so it can be easily found and revisited.
199 /* FIXME: per-net implementation instead? */
200 lnet_peer_ni_addref_locked(lpni);
201 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
202 &the_lnet.ln_remote_peer_ni_list);
205 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
210 static struct lnet_peer_net *
211 lnet_peer_net_alloc(__u32 net_id)
213 struct lnet_peer_net *lpn;
215 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
219 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
220 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
221 lpn->lpn_net_id = net_id;
222 lpn->lpn_sel_priority = LNET_MAX_SELECTION_PRIORITY;
224 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
230 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
232 struct lnet_peer *lp;
234 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
236 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
237 LASSERT(list_empty(&lpn->lpn_peer_nis));
238 LASSERT(list_empty(&lpn->lpn_peer_nets));
240 lpn->lpn_peer = NULL;
241 LIBCFS_FREE(lpn, sizeof(*lpn));
243 lnet_peer_decref_locked(lp);
246 static struct lnet_peer *
247 lnet_peer_alloc(lnet_nid_t nid)
249 struct lnet_peer *lp;
251 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
255 INIT_LIST_HEAD(&lp->lp_rtrq);
256 INIT_LIST_HEAD(&lp->lp_routes);
257 INIT_LIST_HEAD(&lp->lp_peer_list);
258 INIT_LIST_HEAD(&lp->lp_peer_nets);
259 INIT_LIST_HEAD(&lp->lp_dc_list);
260 INIT_LIST_HEAD(&lp->lp_dc_pendq);
261 INIT_LIST_HEAD(&lp->lp_rtr_list);
262 init_waitqueue_head(&lp->lp_dc_waitq);
263 spin_lock_init(&lp->lp_lock);
264 lp->lp_primary_nid = nid;
265 lp->lp_disc_src_nid = LNET_NID_ANY;
266 lp->lp_disc_dst_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 LNetAddPeer(lnet_nid_t *nids, __u32 num_nids)
1334 lnet_nid_t pnid = 0;
1338 if (!nids || num_nids < 1)
1341 rc = LNetNIInit(LNET_PID_ANY);
1345 mutex_lock(&the_lnet.ln_api_mutex);
1347 mr = lnet_peer_discovery_disabled == 0;
1350 for (i = 0; i < num_nids; i++) {
1351 if (nids[i] == LNET_NID_LO_0)
1356 rc = lnet_add_peer_ni(pnid, LNET_NID_ANY, mr, true);
1357 } else if (lnet_peer_discovery_disabled) {
1358 rc = lnet_add_peer_ni(nids[i], LNET_NID_ANY, mr, true);
1360 rc = lnet_add_peer_ni(pnid, nids[i], mr, true);
1363 if (rc && rc != -EEXIST)
1368 mutex_unlock(&the_lnet.ln_api_mutex);
1372 return rc == -EEXIST ? 0 : rc;
1374 EXPORT_SYMBOL(LNetAddPeer);
1377 LNetPrimaryNID(lnet_nid_t nid)
1379 struct lnet_peer *lp;
1380 struct lnet_peer_ni *lpni;
1381 lnet_nid_t primary_nid = nid;
1385 if (nid == LNET_NID_LO_0)
1386 return LNET_NID_LO_0;
1388 cpt = lnet_net_lock_current();
1389 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1394 lp = lpni->lpni_peer_net->lpn_peer;
1396 /* If discovery is disabled locally then we needn't bother running
1397 * discovery here because discovery will not modify whatever
1398 * primary NID is currently set for this peer. If the specified peer is
1399 * down then this discovery can introduce long delays into the mount
1400 * process, so skip it if it isn't necessary.
1402 while (!lnet_peer_discovery_disabled && !lnet_peer_is_uptodate(lp)) {
1403 spin_lock(&lp->lp_lock);
1404 /* force a full discovery cycle */
1405 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1406 spin_unlock(&lp->lp_lock);
1408 rc = lnet_discover_peer_locked(lpni, cpt, true);
1411 /* The lpni (or lp) for this NID may have changed and our ref is
1412 * the only thing keeping the old one around. Release the ref
1413 * and lookup the lpni again
1415 lnet_peer_ni_decref_locked(lpni);
1416 lpni = lnet_find_peer_ni_locked(nid);
1421 lp = lpni->lpni_peer_net->lpn_peer;
1423 /* If we find that the peer has discovery disabled then we will
1424 * not modify whatever primary NID is currently set for this
1425 * peer. Thus, we can break out of this loop even if the peer
1426 * is not fully up to date.
1428 if (lnet_is_discovery_disabled(lp))
1431 primary_nid = lp->lp_primary_nid;
1433 lnet_peer_ni_decref_locked(lpni);
1435 lnet_net_unlock(cpt);
1437 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1438 libcfs_nid2str(primary_nid), rc);
1441 EXPORT_SYMBOL(LNetPrimaryNID);
1443 struct lnet_peer_net *
1444 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1446 struct lnet_peer_net *peer_net;
1447 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1448 if (peer_net->lpn_net_id == net_id)
1455 * Attach a peer_ni to a peer_net and peer. This function assumes
1456 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1457 * may be attached to a different peer, in which case it will be
1458 * properly detached first. The whole operation is done atomically.
1460 * This function consumes the reference on lpni and Always returns 0.
1461 * This is the last function called from functions that do return an
1462 * int, so returning 0 here allows the compiler to do a tail call.
1465 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1466 struct lnet_peer_net *lpn,
1467 struct lnet_peer_ni *lpni,
1470 struct lnet_peer_table *ptable;
1471 bool new_lpn = false;
1474 /* Install the new peer_ni */
1475 lnet_net_lock(LNET_LOCK_EX);
1476 /* Add peer_ni to global peer table hash, if necessary. */
1477 if (list_empty(&lpni->lpni_hashlist)) {
1478 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1480 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1481 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1482 ptable->pt_version++;
1483 lnet_peer_ni_addref_locked(lpni);
1486 /* Detach the peer_ni from an existing peer, if necessary. */
1487 if (lpni->lpni_peer_net) {
1488 LASSERT(lpni->lpni_peer_net != lpn);
1489 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1490 lnet_peer_detach_peer_ni_locked(lpni);
1491 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1492 lpni->lpni_peer_net = NULL;
1495 /* Add peer_ni to peer_net */
1496 lpni->lpni_peer_net = lpn;
1497 if (lp->lp_primary_nid == lpni->lpni_nid)
1498 list_add(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1500 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1501 lnet_update_peer_net_healthv(lpni);
1502 lnet_peer_net_addref_locked(lpn);
1504 /* Add peer_net to peer */
1505 if (!lpn->lpn_peer) {
1508 if (lp->lp_primary_nid == lpni->lpni_nid)
1509 list_add(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1511 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1512 lnet_peer_addref_locked(lp);
1515 /* Add peer to global peer list, if necessary */
1516 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1517 if (list_empty(&lp->lp_peer_list)) {
1518 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1523 /* Update peer state */
1524 spin_lock(&lp->lp_lock);
1525 if (flags & LNET_PEER_CONFIGURED) {
1526 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1527 lp->lp_state |= LNET_PEER_CONFIGURED;
1529 if (flags & LNET_PEER_MULTI_RAIL) {
1530 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1531 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1532 lnet_peer_clr_non_mr_pref_nids(lp);
1535 spin_unlock(&lp->lp_lock);
1541 rc = lnet_udsp_apply_policies_on_lpn(lpn);
1543 CERROR("Failed to apply UDSPs on lpn %s\n",
1544 libcfs_net2str(lpn->lpn_net_id));
1546 rc = lnet_udsp_apply_policies_on_lpni(lpni);
1548 CERROR("Failed to apply UDSPs on lpni %s\n",
1549 libcfs_nid2str(lpni->lpni_nid));
1551 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1552 libcfs_nid2str(lp->lp_primary_nid),
1553 libcfs_nid2str(lpni->lpni_nid), flags);
1554 lnet_peer_ni_decref_locked(lpni);
1555 lnet_net_unlock(LNET_LOCK_EX);
1561 * Create a new peer, with nid as its primary nid.
1563 * Call with the lnet_api_mutex held.
1566 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1568 struct lnet_peer *lp;
1569 struct lnet_peer_net *lpn;
1570 struct lnet_peer_ni *lpni;
1573 LASSERT(nid != LNET_NID_ANY);
1576 * No need for the lnet_net_lock here, because the
1577 * lnet_api_mutex is held.
1579 lpni = lnet_find_peer_ni_locked(nid);
1581 /* A peer with this NID already exists. */
1582 lp = lpni->lpni_peer_net->lpn_peer;
1583 lnet_peer_ni_decref_locked(lpni);
1585 * This is an error if the peer was configured and the
1586 * primary NID differs or an attempt is made to change
1587 * the Multi-Rail flag. Otherwise the assumption is
1588 * that an existing peer is being modified.
1590 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1591 if (lp->lp_primary_nid != nid)
1593 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1596 } else if (!(flags & LNET_PEER_CONFIGURED)) {
1597 if (lp->lp_primary_nid == nid) {
1602 /* Delete and recreate as a configured peer. */
1606 /* Create peer, peer_net, and peer_ni. */
1608 lp = lnet_peer_alloc(nid);
1611 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1614 lpni = lnet_peer_ni_alloc(nid);
1618 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1621 LIBCFS_FREE(lpn, sizeof(*lpn));
1623 LIBCFS_FREE(lp, sizeof(*lp));
1625 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1626 libcfs_nid2str(nid), flags, rc);
1631 * Add a NID to a peer. Call with ln_api_mutex held.
1634 * -EPERM: Non-DLC addition to a DLC-configured peer.
1635 * -EEXIST: The NID was configured by DLC for a different peer.
1636 * -ENOMEM: Out of memory.
1637 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1638 * non-multi-rail peer.
1641 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1643 struct lnet_peer_net *lpn;
1644 struct lnet_peer_ni *lpni;
1648 LASSERT(nid != LNET_NID_ANY);
1650 /* A configured peer can only be updated through configuration. */
1651 if (!(flags & LNET_PEER_CONFIGURED)) {
1652 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1659 * The MULTI_RAIL flag can be set but not cleared, because
1660 * that would leave the peer struct in an invalid state.
1662 if (flags & LNET_PEER_MULTI_RAIL) {
1663 spin_lock(&lp->lp_lock);
1664 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1665 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1666 lnet_peer_clr_non_mr_pref_nids(lp);
1668 spin_unlock(&lp->lp_lock);
1669 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1674 lpni = lnet_find_peer_ni_locked(nid);
1677 * A peer_ni already exists. This is only a problem if
1678 * it is not connected to this peer and was configured
1681 if (lpni->lpni_peer_net->lpn_peer == lp)
1683 if (lnet_peer_ni_is_configured(lpni)) {
1687 /* If this is the primary NID, destroy the peer. */
1688 if (lnet_peer_ni_is_primary(lpni)) {
1689 struct lnet_peer *rtr_lp =
1690 lpni->lpni_peer_net->lpn_peer;
1691 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1693 * if we're trying to delete a router it means
1694 * we're moving this peer NI to a new peer so must
1695 * transfer router properties to the new peer
1697 if (rtr_refcount > 0) {
1698 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1699 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1701 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1702 lnet_peer_ni_decref_locked(lpni);
1703 lpni = lnet_peer_ni_alloc(nid);
1710 lpni = lnet_peer_ni_alloc(nid);
1718 * Get the peer_net. Check that we're not adding a second
1719 * peer_ni on a peer_net of a non-multi-rail peer.
1721 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1723 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1728 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1733 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1736 lnet_peer_ni_decref_locked(lpni);
1738 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1739 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1745 * Update the primary NID of a peer, if possible.
1747 * Call with the lnet_api_mutex held.
1750 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1752 lnet_nid_t old = lp->lp_primary_nid;
1755 if (lp->lp_primary_nid == nid)
1758 lp->lp_primary_nid = nid;
1760 rc = lnet_peer_add_nid(lp, nid, flags);
1762 lp->lp_primary_nid = old;
1766 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1767 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1772 * lpni creation initiated due to traffic either sending or receiving.
1775 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1777 struct lnet_peer *lp;
1778 struct lnet_peer_net *lpn;
1779 struct lnet_peer_ni *lpni;
1783 if (nid == LNET_NID_ANY) {
1788 /* lnet_net_lock is not needed here because ln_api_lock is held */
1789 lpni = lnet_find_peer_ni_locked(nid);
1792 * We must have raced with another thread. Since we
1793 * know next to nothing about a peer_ni created by
1794 * traffic, we just assume everything is ok and
1797 lnet_peer_ni_decref_locked(lpni);
1801 /* Create peer, peer_net, and peer_ni. */
1803 lp = lnet_peer_alloc(nid);
1806 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1809 lpni = lnet_peer_ni_alloc(nid);
1812 if (pref != LNET_NID_ANY)
1813 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1815 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1818 LIBCFS_FREE(lpn, sizeof(*lpn));
1820 LIBCFS_FREE(lp, sizeof(*lp));
1822 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1827 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1829 * This API handles the following combinations:
1830 * Create a peer with its primary NI if only the prim_nid is provided
1831 * Add a NID to a peer identified by the prim_nid. The peer identified
1832 * by the prim_nid must already exist.
1833 * The peer being created may be non-MR.
1835 * The caller must hold ln_api_mutex. This prevents the peer from
1836 * being created/modified/deleted by a different thread.
1839 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr, bool temp)
1841 struct lnet_peer *lp = NULL;
1842 struct lnet_peer_ni *lpni;
1843 unsigned int flags = 0;
1845 /* The prim_nid must always be specified */
1846 if (prim_nid == LNET_NID_ANY)
1850 flags = LNET_PEER_CONFIGURED;
1853 flags |= LNET_PEER_MULTI_RAIL;
1856 * If nid isn't specified, we must create a new peer with
1857 * prim_nid as its primary nid.
1859 if (nid == LNET_NID_ANY)
1860 return lnet_peer_add(prim_nid, flags);
1862 /* Look up the prim_nid, which must exist. */
1863 lpni = lnet_find_peer_ni_locked(prim_nid);
1866 lnet_peer_ni_decref_locked(lpni);
1867 lp = lpni->lpni_peer_net->lpn_peer;
1869 /* Peer must have been configured. */
1870 if (!temp && !(lp->lp_state & LNET_PEER_CONFIGURED)) {
1871 CDEBUG(D_NET, "peer %s was not configured\n",
1872 libcfs_nid2str(prim_nid));
1876 /* Primary NID must match */
1877 if (lp->lp_primary_nid != prim_nid) {
1878 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1879 libcfs_nid2str(prim_nid),
1880 libcfs_nid2str(lp->lp_primary_nid));
1884 /* Multi-Rail flag must match. */
1885 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1886 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1887 libcfs_nid2str(prim_nid));
1891 return lnet_peer_add_nid(lp, nid, flags);
1895 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1897 * This API handles the following combinations:
1898 * Delete a NI from a peer if both prim_nid and nid are provided.
1899 * Delete a peer if only prim_nid is provided.
1900 * Delete a peer if its primary nid is provided.
1902 * The caller must hold ln_api_mutex. This prevents the peer from
1903 * being modified/deleted by a different thread.
1906 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1908 struct lnet_peer *lp;
1909 struct lnet_peer_ni *lpni;
1912 if (prim_nid == LNET_NID_ANY)
1915 lpni = lnet_find_peer_ni_locked(prim_nid);
1918 lnet_peer_ni_decref_locked(lpni);
1919 lp = lpni->lpni_peer_net->lpn_peer;
1921 if (prim_nid != lp->lp_primary_nid) {
1922 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1923 libcfs_nid2str(prim_nid),
1924 libcfs_nid2str(lp->lp_primary_nid));
1928 lnet_net_lock(LNET_LOCK_EX);
1929 if (lp->lp_rtr_refcount > 0) {
1930 lnet_net_unlock(LNET_LOCK_EX);
1931 CERROR("%s is a router. Can not be deleted\n",
1932 libcfs_nid2str(prim_nid));
1935 lnet_net_unlock(LNET_LOCK_EX);
1937 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1938 return lnet_peer_del(lp);
1940 flags = LNET_PEER_CONFIGURED;
1941 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1942 flags |= LNET_PEER_MULTI_RAIL;
1944 return lnet_peer_del_nid(lp, nid, flags);
1948 lnet_destroy_peer_ni_locked(struct kref *ref)
1950 struct lnet_peer_ni *lpni = container_of(ref, struct lnet_peer_ni,
1952 struct lnet_peer_table *ptable;
1953 struct lnet_peer_net *lpn;
1955 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1957 LASSERT(kref_read(&lpni->lpni_kref) == 0);
1958 LASSERT(list_empty(&lpni->lpni_txq));
1959 LASSERT(lpni->lpni_txqnob == 0);
1960 LASSERT(list_empty(&lpni->lpni_peer_nis));
1961 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1963 lpn = lpni->lpni_peer_net;
1964 lpni->lpni_peer_net = NULL;
1965 lpni->lpni_net = NULL;
1967 if (!list_empty(&lpni->lpni_hashlist)) {
1968 /* remove the peer ni from the zombie list */
1969 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1970 spin_lock(&ptable->pt_zombie_lock);
1971 list_del_init(&lpni->lpni_hashlist);
1972 ptable->pt_zombies--;
1973 spin_unlock(&ptable->pt_zombie_lock);
1976 if (lpni->lpni_pref_nnids > 1) {
1977 struct lnet_nid_list *ne, *tmp;
1979 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
1981 list_del_init(&ne->nl_list);
1982 LIBCFS_FREE(ne, sizeof(*ne));
1985 LIBCFS_FREE(lpni, sizeof(*lpni));
1988 lnet_peer_net_decref_locked(lpn);
1991 struct lnet_peer_ni *
1992 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1994 struct lnet_peer_ni *lpni = NULL;
1997 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1998 return ERR_PTR(-ESHUTDOWN);
2001 * find if a peer_ni already exists.
2002 * If so then just return that.
2004 lpni = lnet_find_peer_ni_locked(nid);
2008 lnet_net_unlock(cpt);
2010 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
2013 goto out_net_relock;
2016 lpni = lnet_find_peer_ni_locked(nid);
2026 * Get a peer_ni for the given nid, create it if necessary. Takes a
2027 * hold on the peer_ni.
2029 struct lnet_peer_ni *
2030 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
2032 struct lnet_peer_ni *lpni = NULL;
2035 if (the_lnet.ln_state != LNET_STATE_RUNNING)
2036 return ERR_PTR(-ESHUTDOWN);
2039 * find if a peer_ni already exists.
2040 * If so then just return that.
2042 lpni = lnet_find_peer_ni_locked(nid);
2048 * use the lnet_api_mutex to serialize the creation of the peer_ni
2049 * and the creation/deletion of the local ni/net. When a local ni is
2050 * created, if there exists a set of peer_nis on that network,
2051 * they need to be traversed and updated. When a local NI is
2052 * deleted, which could result in a network being deleted, then
2053 * all peer nis on that network need to be removed as well.
2055 * Creation through traffic should also be serialized with
2056 * creation through DLC.
2058 lnet_net_unlock(cpt);
2059 mutex_lock(&the_lnet.ln_api_mutex);
2061 * Shutdown is only set under the ln_api_lock, so a single
2062 * check here is sufficent.
2064 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2065 lpni = ERR_PTR(-ESHUTDOWN);
2066 goto out_mutex_unlock;
2069 rc = lnet_peer_ni_traffic_add(nid, pref);
2072 goto out_mutex_unlock;
2075 lpni = lnet_find_peer_ni_locked(nid);
2079 mutex_unlock(&the_lnet.ln_api_mutex);
2082 /* Lock has been dropped, check again for shutdown. */
2083 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2085 lnet_peer_ni_decref_locked(lpni);
2086 lpni = ERR_PTR(-ESHUTDOWN);
2093 lnet_peer_gw_discovery(struct lnet_peer *lp)
2097 spin_lock(&lp->lp_lock);
2098 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
2100 spin_unlock(&lp->lp_lock);
2106 lnet_peer_is_uptodate(struct lnet_peer *lp)
2110 spin_lock(&lp->lp_lock);
2111 rc = lnet_peer_is_uptodate_locked(lp);
2112 spin_unlock(&lp->lp_lock);
2117 * Is a peer uptodate from the point of view of discovery?
2119 * If it is currently being processed, obviously not.
2120 * A forced Ping or Push is also handled by the discovery thread.
2122 * Otherwise look at whether the peer needs rediscovering.
2125 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
2126 __must_hold(&lp->lp_lock)
2130 if (lp->lp_state & (LNET_PEER_DISCOVERING |
2131 LNET_PEER_FORCE_PING |
2132 LNET_PEER_FORCE_PUSH)) {
2134 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
2136 } else if (lnet_peer_needs_push(lp)) {
2138 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
2139 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
2150 /* Add the message to the peer's lp_dc_pendq and queue the peer for discovery */
2152 lnet_peer_queue_message(struct lnet_peer *lp, struct lnet_msg *msg)
2154 /* The discovery thread holds net_lock/EX and lp_lock when it splices
2155 * the lp_dc_pendq onto a local list for resending. Thus, we do the same
2156 * when adding to the list and queuing the peer to ensure that we do not
2157 * strand any messages on the lp_dc_pendq. This scheme ensures the
2158 * message will be resent even if the peer is already being discovered.
2159 * Therefore we needn't check the return value of
2160 * lnet_peer_queue_for_discovery(lp).
2162 lnet_net_lock(LNET_LOCK_EX);
2163 spin_lock(&lp->lp_lock);
2164 list_add_tail(&msg->msg_list, &lp->lp_dc_pendq);
2165 spin_unlock(&lp->lp_lock);
2166 lnet_peer_queue_for_discovery(lp);
2167 lnet_net_unlock(LNET_LOCK_EX);
2171 * Queue a peer for the attention of the discovery thread. Call with
2172 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
2173 * -EALREADY if the peer was already queued.
2175 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
2179 spin_lock(&lp->lp_lock);
2180 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2181 lp->lp_state |= LNET_PEER_DISCOVERING;
2182 spin_unlock(&lp->lp_lock);
2183 if (list_empty(&lp->lp_dc_list)) {
2184 lnet_peer_addref_locked(lp);
2185 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2186 wake_up(&the_lnet.ln_dc_waitq);
2192 CDEBUG(D_NET, "Queue peer %s: %d\n",
2193 libcfs_nid2str(lp->lp_primary_nid), rc);
2199 * Discovery of a peer is complete. Wake all waiters on the peer.
2200 * Call with lnet_net_lock/EX held.
2202 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
2204 struct lnet_msg *msg, *tmp;
2206 LIST_HEAD(pending_msgs);
2208 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
2209 libcfs_nid2str(lp->lp_primary_nid));
2211 list_del_init(&lp->lp_dc_list);
2212 spin_lock(&lp->lp_lock);
2213 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
2214 spin_unlock(&lp->lp_lock);
2215 wake_up(&lp->lp_dc_waitq);
2217 if (lp->lp_rtr_refcount > 0)
2218 lnet_router_discovery_complete(lp);
2220 lnet_net_unlock(LNET_LOCK_EX);
2222 /* iterate through all pending messages and send them again */
2223 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
2224 list_del_init(&msg->msg_list);
2225 if (lp->lp_dc_error) {
2226 lnet_finalize(msg, lp->lp_dc_error);
2230 CDEBUG(D_NET, "sending pending message %s to target %s\n",
2231 lnet_msgtyp2str(msg->msg_type),
2232 libcfs_id2str(msg->msg_target));
2233 rc = lnet_send(msg->msg_src_nid_param, msg,
2234 msg->msg_rtr_nid_param);
2236 CNETERR("Error sending %s to %s: %d\n",
2237 lnet_msgtyp2str(msg->msg_type),
2238 libcfs_id2str(msg->msg_target), rc);
2239 lnet_finalize(msg, rc);
2242 lnet_net_lock(LNET_LOCK_EX);
2243 lnet_peer_decref_locked(lp);
2247 * Handle inbound push.
2248 * Like any event handler, called with lnet_res_lock/CPT held.
2250 void lnet_peer_push_event(struct lnet_event *ev)
2252 struct lnet_ping_buffer *pbuf;
2253 struct lnet_peer *lp;
2255 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
2257 /* lnet_find_peer() adds a refcount */
2258 lp = lnet_find_peer(ev->source.nid);
2260 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
2261 libcfs_nid2str(ev->initiator.nid),
2262 libcfs_nid2str(ev->source.nid));
2263 pbuf->pb_needs_post = true;
2267 /* Ensure peer state remains consistent while we modify it. */
2268 spin_lock(&lp->lp_lock);
2271 * If some kind of error happened the contents of the message
2272 * cannot be used. Clear the NIDS_UPTODATE and set the
2273 * FORCE_PING flag to trigger a ping.
2276 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2277 lp->lp_state |= LNET_PEER_FORCE_PING;
2278 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2280 libcfs_nid2str(lp->lp_primary_nid),
2281 libcfs_nid2str(ev->source.nid));
2286 * A push with invalid or corrupted info. Clear the UPTODATE
2287 * flag to trigger a ping.
2289 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2290 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2291 lp->lp_state |= LNET_PEER_FORCE_PING;
2292 CDEBUG(D_NET, "Corrupted Push from %s\n",
2293 libcfs_nid2str(lp->lp_primary_nid));
2298 * Make sure we'll allocate the correct size ping buffer when
2301 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2302 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2305 * A non-Multi-Rail peer is not supposed to be capable of
2308 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2309 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2310 libcfs_nid2str(lp->lp_primary_nid));
2315 * The peer may have discovery disabled at its end. Set
2316 * NO_DISCOVERY as appropriate.
2318 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2319 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2320 libcfs_nid2str(lp->lp_primary_nid));
2322 * Mark the peer for deletion if we already know about it
2323 * and it's going from discovery set to no discovery set
2325 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2326 LNET_PEER_DISCOVERING)) &&
2327 lp->lp_state & LNET_PEER_DISCOVERED) {
2328 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2329 libcfs_nid2str(lp->lp_primary_nid),
2331 lp->lp_state |= LNET_PEER_MARK_DELETION;
2333 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2334 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2335 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2336 libcfs_nid2str(lp->lp_primary_nid));
2337 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2341 * Update the MULTI_RAIL flag based on the push. If the peer
2342 * was configured with DLC then the setting should match what
2344 * NB: We verified above that the MR feature bit is set in pi_features
2346 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2347 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2348 libcfs_nid2str(lp->lp_primary_nid), lp);
2349 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2350 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2351 libcfs_nid2str(lp->lp_primary_nid));
2352 } else if (lnet_peer_discovery_disabled) {
2353 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2354 libcfs_nid2str(lp->lp_primary_nid), lp);
2355 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2356 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2357 libcfs_nid2str(lp->lp_primary_nid), lp);
2359 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2360 libcfs_nid2str(lp->lp_primary_nid), lp);
2361 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2362 lnet_peer_clr_non_mr_pref_nids(lp);
2366 * Check for truncation of the Put message. Clear the
2367 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2368 * and tell discovery to allocate a bigger buffer.
2370 if (ev->mlength < ev->rlength) {
2371 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2372 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2373 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2374 lp->lp_state |= LNET_PEER_FORCE_PING;
2375 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2376 libcfs_nid2str(lp->lp_primary_nid),
2377 pbuf->pb_info.pi_nnis);
2381 /* always assume new data */
2382 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2383 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2386 * If there is data present that hasn't been processed yet,
2387 * we'll replace it if the Put contained newer data and it
2388 * fits. We're racing with a Ping or earlier Push in this
2391 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2392 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2393 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2394 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2395 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2396 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2397 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2398 libcfs_nid2str(lp->lp_primary_nid),
2399 LNET_PING_BUFFER_SEQNO(pbuf),
2400 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2406 * Allocate a buffer to copy the data. On a failure we drop
2407 * the Push and set FORCE_PING to force the discovery
2408 * thread to fix the problem by pinging the peer.
2410 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2412 lp->lp_state |= LNET_PEER_FORCE_PING;
2413 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2414 libcfs_nid2str(lp->lp_primary_nid),
2415 LNET_PING_BUFFER_SEQNO(pbuf));
2420 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2421 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2422 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2423 CDEBUG(D_NET, "Received Push %s %u\n",
2424 libcfs_nid2str(lp->lp_primary_nid),
2425 LNET_PING_BUFFER_SEQNO(pbuf));
2428 /* We've processed this buffer. It can be reposted */
2429 pbuf->pb_needs_post = true;
2432 * Queue the peer for discovery if not done, force it on the request
2433 * queue and wake the discovery thread if the peer was already queued,
2434 * because its status changed.
2436 spin_unlock(&lp->lp_lock);
2437 lnet_net_lock(LNET_LOCK_EX);
2438 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2439 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2440 wake_up(&the_lnet.ln_dc_waitq);
2442 /* Drop refcount from lookup */
2443 lnet_peer_decref_locked(lp);
2444 lnet_net_unlock(LNET_LOCK_EX);
2448 * Clear the discovery error state, unless we're already discovering
2449 * this peer, in which case the error is current.
2451 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2453 spin_lock(&lp->lp_lock);
2454 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2455 lp->lp_dc_error = 0;
2456 spin_unlock(&lp->lp_lock);
2460 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2461 * dropped/retaken within this function. An lnet_peer_ni is passed in
2462 * because discovery could tear down an lnet_peer.
2465 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2468 struct lnet_peer *lp;
2473 lnet_net_unlock(cpt);
2474 lnet_net_lock(LNET_LOCK_EX);
2475 lp = lpni->lpni_peer_net->lpn_peer;
2476 lnet_peer_clear_discovery_error(lp);
2479 * We're willing to be interrupted. The lpni can become a
2480 * zombie if we race with DLC, so we must check for that.
2483 /* Keep lp alive when the lnet_net_lock is unlocked */
2484 lnet_peer_addref_locked(lp);
2485 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2486 if (signal_pending(current))
2488 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2491 * Don't repeat discovery if discovery is disabled. This is
2492 * done to ensure we can use discovery as a standard ping as
2493 * well for backwards compatibility with routers which do not
2494 * have discovery or have discovery disabled
2496 if (lnet_is_discovery_disabled(lp) && count > 0)
2498 if (lp->lp_dc_error)
2500 if (lnet_peer_is_uptodate(lp))
2502 lnet_peer_queue_for_discovery(lp);
2504 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2507 * If caller requested a non-blocking operation then
2508 * return immediately. Once discovery is complete any
2509 * pending messages that were stopped due to discovery
2510 * will be transmitted.
2515 lnet_net_unlock(LNET_LOCK_EX);
2517 finish_wait(&lp->lp_dc_waitq, &wait);
2518 lnet_net_lock(LNET_LOCK_EX);
2519 lnet_peer_decref_locked(lp);
2520 /* Peer may have changed */
2521 lp = lpni->lpni_peer_net->lpn_peer;
2523 finish_wait(&lp->lp_dc_waitq, &wait);
2525 lnet_net_unlock(LNET_LOCK_EX);
2527 lnet_peer_decref_locked(lp);
2529 * The peer may have changed, so re-check and rediscover if that turns
2530 * out to have been the case. The reference count on lp ensured that
2531 * even if it was unlinked from lpni the memory could not be recycled.
2532 * Thus the check below is sufficient to determine whether the peer
2533 * changed. If the peer changed, then lp must not be dereferenced.
2535 if (lp != lpni->lpni_peer_net->lpn_peer)
2538 if (signal_pending(current))
2540 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2542 else if (lp->lp_dc_error)
2543 rc = lp->lp_dc_error;
2545 CDEBUG(D_NET, "non-blocking discovery\n");
2546 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2549 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2550 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2551 libcfs_nid2str(lpni->lpni_nid), rc,
2552 (!block) ? "pending discovery" : "discovery complete");
2557 /* Handle an incoming ack for a push. */
2559 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2561 struct lnet_ping_buffer *pbuf;
2563 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2564 spin_lock(&lp->lp_lock);
2565 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2566 lp->lp_push_error = ev->status;
2568 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2570 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2571 spin_unlock(&lp->lp_lock);
2573 CDEBUG(D_NET, "peer %s ev->status %d\n",
2574 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2577 /* Handle a Reply message. This is the reply to a Ping message. */
2579 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2581 struct lnet_ping_buffer *pbuf;
2584 spin_lock(&lp->lp_lock);
2586 lp->lp_disc_src_nid = ev->target.nid;
2587 lp->lp_disc_dst_nid = ev->source.nid;
2590 * If some kind of error happened the contents of message
2591 * cannot be used. Set PING_FAILED to trigger a retry.
2594 lp->lp_state |= LNET_PEER_PING_FAILED;
2595 lp->lp_ping_error = ev->status;
2596 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2598 libcfs_nid2str(lp->lp_primary_nid),
2599 libcfs_nid2str(ev->source.nid));
2603 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2604 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2605 lnet_swap_pinginfo(pbuf);
2608 * A reply with invalid or corrupted info. Set PING_FAILED to
2611 rc = lnet_ping_info_validate(&pbuf->pb_info);
2613 lp->lp_state |= LNET_PEER_PING_FAILED;
2614 lp->lp_ping_error = 0;
2615 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2616 libcfs_nid2str(lp->lp_primary_nid), rc);
2621 * The peer may have discovery disabled at its end. Set
2622 * NO_DISCOVERY as appropriate.
2624 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) ||
2625 lnet_peer_discovery_disabled) {
2626 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2627 libcfs_nid2str(lp->lp_primary_nid));
2629 /* Detect whether this peer has toggled discovery from on to
2630 * off and whether we can delete and re-create the peer. Peers
2631 * that were manually configured cannot be deleted by discovery.
2632 * We need to delete this peer and re-create it if the peer was
2633 * not configured manually, is currently considered DD capable,
2635 * 1. We've already discovered the peer (the peer has toggled
2636 * the discovery feature from on to off), or
2637 * 2. The peer is considered MR, but it was not user configured
2638 * (this was a "temporary" peer created via the kernel APIs
2639 * that we're discovering for the first time)
2641 if (!(lp->lp_state & (LNET_PEER_CONFIGURED |
2642 LNET_PEER_NO_DISCOVERY)) &&
2643 (lp->lp_state & (LNET_PEER_DISCOVERED |
2644 LNET_PEER_MULTI_RAIL))) {
2645 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2646 libcfs_nid2str(lp->lp_primary_nid),
2648 lp->lp_state |= LNET_PEER_MARK_DELETION;
2650 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2652 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2653 libcfs_nid2str(lp->lp_primary_nid));
2654 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2658 * Update the MULTI_RAIL flag based on the reply. If the peer
2659 * was configured with DLC then the setting should match what
2662 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2663 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2664 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2665 libcfs_nid2str(lp->lp_primary_nid), lp);
2666 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2667 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2668 libcfs_nid2str(lp->lp_primary_nid));
2669 } else if (lnet_peer_discovery_disabled) {
2671 "peer %s(%p) not MR: DD disabled locally\n",
2672 libcfs_nid2str(lp->lp_primary_nid), lp);
2673 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2675 "peer %s(%p) not MR: DD disabled remotely\n",
2676 libcfs_nid2str(lp->lp_primary_nid), lp);
2678 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2679 libcfs_nid2str(lp->lp_primary_nid), lp);
2680 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2681 lnet_peer_clr_non_mr_pref_nids(lp);
2683 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2684 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2685 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2686 libcfs_nid2str(lp->lp_primary_nid));
2688 CERROR("Multi-Rail state vanished from %s\n",
2689 libcfs_nid2str(lp->lp_primary_nid));
2690 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2695 * Make sure we'll allocate the correct size ping buffer when
2698 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2699 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2702 * Check for truncation of the Reply. Clear PING_SENT and set
2703 * PING_FAILED to trigger a retry.
2705 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2706 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2707 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2708 lp->lp_state |= LNET_PEER_PING_FAILED;
2709 lp->lp_ping_error = 0;
2710 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2711 libcfs_nid2str(lp->lp_primary_nid),
2712 pbuf->pb_info.pi_nnis);
2717 * Check the sequence numbers in the reply. These are only
2718 * available if the reply came from a Multi-Rail peer.
2720 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2721 pbuf->pb_info.pi_nnis > 1 &&
2722 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2723 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2724 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2725 libcfs_nid2str(lp->lp_primary_nid),
2726 LNET_PING_BUFFER_SEQNO(pbuf),
2729 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2732 /* We're happy with the state of the data in the buffer. */
2733 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2734 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2735 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2736 lnet_ping_buffer_decref(lp->lp_data);
2738 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2739 lnet_ping_buffer_addref(pbuf);
2742 lp->lp_state &= ~LNET_PEER_PING_SENT;
2743 spin_unlock(&lp->lp_lock);
2745 lnet_net_lock(LNET_LOCK_EX);
2747 * If this peer is a gateway, call the routing callback to
2748 * handle the ping reply
2750 if (lp->lp_rtr_refcount > 0)
2751 lnet_router_discovery_ping_reply(lp);
2752 lnet_net_unlock(LNET_LOCK_EX);
2756 * Send event handling. Only matters for error cases, where we clean
2757 * up state on the peer and peer_ni that would otherwise be updated in
2758 * the REPLY event handler for a successful Ping, and the ACK event
2759 * handler for a successful Push.
2762 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2769 spin_lock(&lp->lp_lock);
2770 if (ev->msg_type == LNET_MSG_GET) {
2771 lp->lp_state &= ~LNET_PEER_PING_SENT;
2772 lp->lp_state |= LNET_PEER_PING_FAILED;
2773 lp->lp_ping_error = ev->status;
2774 } else { /* ev->msg_type == LNET_MSG_PUT */
2775 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2776 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2777 lp->lp_push_error = ev->status;
2779 spin_unlock(&lp->lp_lock);
2780 rc = LNET_REDISCOVER_PEER;
2782 CDEBUG(D_NET, "%s Send to %s: %d\n",
2783 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2784 libcfs_nid2str(ev->target.nid), rc);
2789 * Unlink event handling. This event is only seen if a call to
2790 * LNetMDUnlink() caused the event to be unlinked. If this call was
2791 * made after the event was set up in LNetGet() or LNetPut() then we
2792 * assume the Ping or Push timed out.
2795 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2797 spin_lock(&lp->lp_lock);
2798 /* We've passed through LNetGet() */
2799 if (lp->lp_state & LNET_PEER_PING_SENT) {
2800 lp->lp_state &= ~LNET_PEER_PING_SENT;
2801 lp->lp_state |= LNET_PEER_PING_FAILED;
2802 lp->lp_ping_error = -ETIMEDOUT;
2803 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2804 libcfs_nid2str(lp->lp_primary_nid));
2806 /* We've passed through LNetPut() */
2807 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2808 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2809 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2810 lp->lp_push_error = -ETIMEDOUT;
2811 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2812 libcfs_nid2str(lp->lp_primary_nid));
2814 spin_unlock(&lp->lp_lock);
2818 * Event handler for the discovery EQ.
2820 * Called with lnet_res_lock(cpt) held. The cpt is the
2821 * lnet_cpt_of_cookie() of the md handle cookie.
2823 static void lnet_discovery_event_handler(struct lnet_event *event)
2825 struct lnet_peer *lp = event->md_user_ptr;
2826 struct lnet_ping_buffer *pbuf;
2829 /* discovery needs to take another look */
2830 rc = LNET_REDISCOVER_PEER;
2832 CDEBUG(D_NET, "Received event: %d\n", event->type);
2834 switch (event->type) {
2835 case LNET_EVENT_ACK:
2836 lnet_discovery_event_ack(lp, event);
2838 case LNET_EVENT_REPLY:
2839 lnet_discovery_event_reply(lp, event);
2841 case LNET_EVENT_SEND:
2842 /* Only send failure triggers a retry. */
2843 rc = lnet_discovery_event_send(lp, event);
2845 case LNET_EVENT_UNLINK:
2846 /* LNetMDUnlink() was called */
2847 lnet_discovery_event_unlink(lp, event);
2850 /* Invalid events. */
2853 lnet_net_lock(LNET_LOCK_EX);
2854 if (event->unlinked) {
2855 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2856 lnet_ping_buffer_decref(pbuf);
2857 lnet_peer_decref_locked(lp);
2860 /* put peer back at end of request queue, if discovery not already
2862 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp) &&
2863 lnet_peer_queue_for_discovery(lp)) {
2864 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2865 wake_up(&the_lnet.ln_dc_waitq);
2867 lnet_net_unlock(LNET_LOCK_EX);
2871 * Build a peer from incoming data.
2873 * The NIDs in the incoming data are supposed to be structured as follows:
2876 * - other NIDs in same net
2877 * - NIDs in second net
2878 * - NIDs in third net
2880 * This due to the way the list of NIDs in the data is created.
2882 * Note that this function will mark the peer uptodate unless an
2883 * ENOMEM is encontered. All other errors are due to a conflict
2884 * between the DLC configuration and what discovery sees. We treat DLC
2885 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2886 * peer from becoming stuck in discovery.
2888 static int lnet_peer_merge_data(struct lnet_peer *lp,
2889 struct lnet_ping_buffer *pbuf)
2891 struct lnet_peer_net *lpn;
2892 struct lnet_peer_ni *lpni;
2893 lnet_nid_t *curnis = NULL;
2894 struct lnet_ni_status *addnis = NULL;
2895 lnet_nid_t *delnis = NULL;
2905 flags = LNET_PEER_DISCOVERED;
2906 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2907 flags |= LNET_PEER_MULTI_RAIL;
2910 * Cache the routing feature for the peer; whether it is enabled
2911 * for disabled as reported by the remote peer.
2913 spin_lock(&lp->lp_lock);
2914 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2915 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2917 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2918 spin_unlock(&lp->lp_lock);
2920 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2921 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2922 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2923 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2924 if (!curnis || !addnis || !delnis) {
2932 /* Construct the list of NIDs present in peer. */
2934 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2935 curnis[ncurnis++] = lpni->lpni_nid;
2938 * Check for NIDs in pbuf not present in curnis[].
2939 * The loop starts at 1 to skip the loopback NID.
2941 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2942 for (j = 0; j < ncurnis; j++)
2943 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2946 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2949 * Check for NIDs in curnis[] not present in pbuf.
2950 * The nested loop starts at 1 to skip the loopback NID.
2952 * But never add the loopback NID to delnis[]: if it is
2953 * present in curnis[] then this peer is for this node.
2955 for (i = 0; i < ncurnis; i++) {
2956 if (curnis[i] == LNET_NID_LO_0)
2958 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2959 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2961 * update the information we cache for the
2962 * peer with the latest information we
2965 lpni = lnet_find_peer_ni_locked(curnis[i]);
2967 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2968 lnet_peer_ni_decref_locked(lpni);
2973 if (j == pbuf->pb_info.pi_nnis)
2974 delnis[ndelnis++] = curnis[i];
2978 * If we get here and the discovery is disabled then we don't want
2979 * to add or delete any NIs. We just updated the ones we have some
2980 * information on, and call it a day
2983 if (lnet_is_discovery_disabled(lp))
2986 for (i = 0; i < naddnis; i++) {
2987 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2989 CERROR("Error adding NID %s to peer %s: %d\n",
2990 libcfs_nid2str(addnis[i].ns_nid),
2991 libcfs_nid2str(lp->lp_primary_nid), rc);
2995 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2997 lpni->lpni_ns_status = addnis[i].ns_status;
2998 lnet_peer_ni_decref_locked(lpni);
3002 for (i = 0; i < ndelnis; i++) {
3004 * for routers it's okay to delete the primary_nid because
3005 * the upper layers don't really rely on it. So if we're
3006 * being told that the router changed its primary_nid
3007 * then it's okay to delete it.
3009 if (lp->lp_rtr_refcount > 0)
3010 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
3011 rc = lnet_peer_del_nid(lp, delnis[i], flags);
3013 CERROR("Error deleting NID %s from peer %s: %d\n",
3014 libcfs_nid2str(delnis[i]),
3015 libcfs_nid2str(lp->lp_primary_nid), rc);
3021 /* The peer net for the primary NID should be the first entry in the
3022 * peer's lp_peer_nets list, and the peer NI for the primary NID should
3023 * be the first entry in its peer net's lpn_peer_nis list.
3025 lpni = lnet_find_peer_ni_locked(pbuf->pb_info.pi_ni[1].ns_nid);
3027 CERROR("Internal error: Failed to lookup peer NI for primary NID: %s\n",
3028 libcfs_nid2str(pbuf->pb_info.pi_ni[1].ns_nid));
3032 lnet_peer_ni_decref_locked(lpni);
3034 lpn = lpni->lpni_peer_net;
3035 if (lpn->lpn_peer_nets.prev != &lp->lp_peer_nets)
3036 list_move(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
3038 if (lpni->lpni_peer_nis.prev != &lpni->lpni_peer_net->lpn_peer_nis)
3039 list_move(&lpni->lpni_peer_nis,
3040 &lpni->lpni_peer_net->lpn_peer_nis);
3043 * Errors other than -ENOMEM are due to peers having been
3044 * configured with DLC. Ignore these because DLC overrides
3049 CFS_FREE_PTR_ARRAY(curnis, nnis);
3050 CFS_FREE_PTR_ARRAY(addnis, nnis);
3051 CFS_FREE_PTR_ARRAY(delnis, nnis);
3052 lnet_ping_buffer_decref(pbuf);
3053 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3056 spin_lock(&lp->lp_lock);
3057 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3058 lp->lp_state |= LNET_PEER_FORCE_PING;
3059 spin_unlock(&lp->lp_lock);
3065 * The data in pbuf says lp is its primary peer, but the data was
3066 * received by a different peer. Try to update lp with the data.
3069 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
3071 struct lnet_handle_md mdh;
3073 /* Queue lp for discovery, and force it on the request queue. */
3074 lnet_net_lock(LNET_LOCK_EX);
3075 if (lnet_peer_queue_for_discovery(lp))
3076 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
3077 lnet_net_unlock(LNET_LOCK_EX);
3079 LNetInvalidateMDHandle(&mdh);
3082 * Decide whether we can move the peer to the DATA_PRESENT state.
3084 * We replace stale data for a multi-rail peer, repair PING_FAILED
3085 * status, and preempt FORCE_PING.
3087 * If after that we have DATA_PRESENT, we merge it into this peer.
3089 spin_lock(&lp->lp_lock);
3090 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
3091 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
3092 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
3093 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3094 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3095 lnet_ping_buffer_decref(pbuf);
3100 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3101 lnet_ping_buffer_decref(lp->lp_data);
3103 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3105 if (lp->lp_state & LNET_PEER_PING_FAILED) {
3106 mdh = lp->lp_ping_mdh;
3107 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3108 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3109 lp->lp_ping_error = 0;
3111 if (lp->lp_state & LNET_PEER_FORCE_PING)
3112 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3113 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3114 spin_unlock(&lp->lp_lock);
3116 if (!LNetMDHandleIsInvalid(mdh))
3120 return lnet_peer_merge_data(lp, pbuf);
3122 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3126 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
3130 for (i = 0; i < pinfo->pi_nnis; i++) {
3131 if (pinfo->pi_ni[i].ns_nid == nid)
3138 /* Delete a peer that has been marked for deletion. NB: when this peer was added
3139 * to the discovery queue a reference was taken that will prevent the peer from
3140 * actually being freed by this function. After this function exits the
3141 * discovery thread should call lnet_peer_discovery_complete() which will
3142 * drop that reference as well as wake any waiters that may also be holding a
3145 static int lnet_peer_deletion(struct lnet_peer *lp)
3146 __must_hold(&lp->lp_lock)
3148 struct list_head rlist;
3149 struct lnet_route *route, *tmp;
3150 int sensitivity = lp->lp_health_sensitivity;
3152 INIT_LIST_HEAD(&rlist);
3154 lp->lp_state &= ~(LNET_PEER_DISCOVERING | LNET_PEER_FORCE_PING |
3155 LNET_PEER_FORCE_PUSH);
3156 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3157 libcfs_nid2str(lp->lp_primary_nid), lp, lp->lp_state);
3159 /* no-op if lnet_peer_del() has already been called on this peer */
3160 if (lp->lp_state & LNET_PEER_MARK_DELETED)
3163 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
3166 spin_unlock(&lp->lp_lock);
3168 mutex_lock(&the_lnet.ln_api_mutex);
3170 lnet_net_lock(LNET_LOCK_EX);
3171 /* remove the peer from the discovery work
3172 * queue if it's on there in preparation
3175 if (!list_empty(&lp->lp_dc_list))
3176 list_del_init(&lp->lp_dc_list);
3177 list_for_each_entry_safe(route, tmp,
3180 lnet_move_route(route, NULL, &rlist);
3181 lnet_net_unlock(LNET_LOCK_EX);
3183 /* lnet_peer_del() deletes all the peer NIs owned by this peer */
3186 list_for_each_entry_safe(route, tmp,
3188 /* re-add these routes */
3189 lnet_add_route(route->lr_net,
3194 LIBCFS_FREE(route, sizeof(*route));
3197 mutex_unlock(&the_lnet.ln_api_mutex);
3199 spin_lock(&lp->lp_lock);
3205 * Update a peer using the data received.
3207 static int lnet_peer_data_present(struct lnet_peer *lp)
3208 __must_hold(&lp->lp_lock)
3210 struct lnet_ping_buffer *pbuf;
3211 struct lnet_peer_ni *lpni;
3212 lnet_nid_t nid = LNET_NID_ANY;
3218 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3219 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3220 spin_unlock(&lp->lp_lock);
3223 * Modifications of peer structures are done while holding the
3224 * ln_api_mutex. A global lock is required because we may be
3225 * modifying multiple peer structures, and a mutex greatly
3226 * simplifies memory management.
3228 * The actual changes to the data structures must also protect
3229 * against concurrent lookups, for which the lnet_net_lock in
3230 * LNET_LOCK_EX mode is used.
3232 mutex_lock(&the_lnet.ln_api_mutex);
3233 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3239 * If this peer is not on the peer list then it is being torn
3240 * down, and our reference count may be all that is keeping it
3241 * alive. Don't do any work on it.
3243 if (list_empty(&lp->lp_peer_list))
3246 flags = LNET_PEER_DISCOVERED;
3247 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
3248 flags |= LNET_PEER_MULTI_RAIL;
3251 * Check whether the primary NID in the message matches the
3252 * primary NID of the peer. If it does, update the peer, if
3253 * it it does not, check whether there is already a peer with
3254 * that primary NID. If no such peer exists, try to update
3255 * the primary NID of the current peer (allowed if it was
3256 * created due to message traffic) and complete the update.
3257 * If the peer did exist, hand off the data to it.
3259 * The peer for the loopback interface is a special case: this
3260 * is the peer for the local node, and we want to set its
3261 * primary NID to the correct value here. Moreover, this peer
3262 * can show up with only the loopback NID in the ping buffer.
3264 if (pbuf->pb_info.pi_nnis <= 1)
3266 nid = pbuf->pb_info.pi_ni[1].ns_nid;
3267 if (lp->lp_primary_nid == LNET_NID_LO_0) {
3268 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3270 rc = lnet_peer_merge_data(lp, pbuf);
3272 * if the primary nid of the peer is present in the ping info returned
3273 * from the peer, but it's not the local primary peer we have
3274 * cached and discovery is disabled, then we don't want to update
3275 * our local peer info, by adding or removing NIDs, we just want
3276 * to update the status of the nids that we currently have
3277 * recorded in that peer.
3279 } else if (lp->lp_primary_nid == nid ||
3280 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
3281 lnet_is_discovery_disabled(lp))) {
3282 rc = lnet_peer_merge_data(lp, pbuf);
3284 lpni = lnet_find_peer_ni_locked(nid);
3285 if (!lpni || lp == lpni->lpni_peer_net->lpn_peer) {
3286 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3288 CERROR("Primary NID error %s versus %s: %d\n",
3289 libcfs_nid2str(lp->lp_primary_nid),
3290 libcfs_nid2str(nid), rc);
3292 rc = lnet_peer_merge_data(lp, pbuf);
3295 lnet_peer_ni_decref_locked(lpni);
3297 struct lnet_peer *new_lp;
3298 new_lp = lpni->lpni_peer_net->lpn_peer;
3300 * if lp has discovery/MR enabled that means new_lp
3301 * should have discovery/MR enabled as well, since
3302 * it's the same peer, which we're about to merge
3304 spin_lock(&lp->lp_lock);
3305 spin_lock(&new_lp->lp_lock);
3306 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
3307 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
3308 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
3309 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
3310 /* If we're processing a ping reply then we may be
3311 * about to send a push to the peer that we ping'd.
3312 * Since the ping reply that we're processing was
3313 * received by lp, we need to set the discovery source
3314 * NID for new_lp to the NID stored in lp.
3316 if (lp->lp_disc_src_nid != LNET_NID_ANY) {
3317 new_lp->lp_disc_src_nid = lp->lp_disc_src_nid;
3318 new_lp->lp_disc_dst_nid = lp->lp_disc_dst_nid;
3320 spin_unlock(&new_lp->lp_lock);
3321 spin_unlock(&lp->lp_lock);
3323 rc = lnet_peer_set_primary_data(new_lp, pbuf);
3324 lnet_consolidate_routes_locked(lp, new_lp);
3325 lnet_peer_ni_decref_locked(lpni);
3329 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
3331 mutex_unlock(&the_lnet.ln_api_mutex);
3333 spin_lock(&lp->lp_lock);
3334 /* Tell discovery to re-check the peer immediately. */
3336 rc = LNET_REDISCOVER_PEER;
3341 * A ping failed. Clear the PING_FAILED state and set the
3342 * FORCE_PING state, to ensure a retry even if discovery is
3343 * disabled. This avoids being left with incorrect state.
3345 static int lnet_peer_ping_failed(struct lnet_peer *lp)
3346 __must_hold(&lp->lp_lock)
3348 struct lnet_handle_md mdh;
3351 mdh = lp->lp_ping_mdh;
3352 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3353 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3354 lp->lp_state |= LNET_PEER_FORCE_PING;
3355 rc = lp->lp_ping_error;
3356 lp->lp_ping_error = 0;
3357 spin_unlock(&lp->lp_lock);
3359 if (!LNetMDHandleIsInvalid(mdh))
3362 CDEBUG(D_NET, "peer %s:%d\n",
3363 libcfs_nid2str(lp->lp_primary_nid), rc);
3365 spin_lock(&lp->lp_lock);
3366 return rc ? rc : LNET_REDISCOVER_PEER;
3369 /* Active side of ping. */
3370 static int lnet_peer_send_ping(struct lnet_peer *lp)
3371 __must_hold(&lp->lp_lock)
3377 lp->lp_state |= LNET_PEER_PING_SENT;
3378 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3379 spin_unlock(&lp->lp_lock);
3381 cpt = lnet_net_lock_current();
3382 /* Refcount for MD. */
3383 lnet_peer_addref_locked(lp);
3384 lnet_net_unlock(cpt);
3386 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3388 rc = lnet_send_ping(lp->lp_primary_nid, &lp->lp_ping_mdh, nnis, lp,