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_NID_NET(&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(struct lnet_nid *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_NID_NET(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_nidstr(&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(struct lnet_nid *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_ANY_NID;
266 lp->lp_disc_dst_nid = LNET_ANY_NID;
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
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_nidstr(&lp->lp_primary_nid));
296 lnet_destroy_peer_locked(struct lnet_peer *lp)
298 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nidstr(&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_nidstr(&lp->lp_primary_nid),
382 libcfs_nidstr(&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_nidstr(&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_nidstr(&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)
514 lnet_peer_cancel_discovery(peer);
515 lnet_net_lock(LNET_LOCK_EX);
516 rc = lnet_peer_del_locked(peer);
517 lnet_net_unlock(LNET_LOCK_EX);
523 * Delete a NID from a peer. Call with ln_api_mutex held.
526 * -EPERM: Non-DLC deletion from DLC-configured peer.
527 * -ENOENT: No lnet_peer_ni corresponding to the nid.
528 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
529 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
532 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid4, unsigned int flags)
534 struct lnet_peer_ni *lpni;
535 struct lnet_nid primary_nid = lp->lp_primary_nid;
538 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
540 lnet_nid4_to_nid(nid4, &nid);
541 if (!(flags & LNET_PEER_CONFIGURED)) {
542 if (lp->lp_state & LNET_PEER_CONFIGURED) {
548 lpni = lnet_peer_ni_find_locked(&nid);
553 lnet_peer_ni_decref_locked(lpni);
554 if (lp != lpni->lpni_peer_net->lpn_peer) {
560 * This function only allows deletion of the primary NID if it
563 if (nid_same(&nid, &lp->lp_primary_nid) && lp->lp_nnis != 1 && !force) {
568 lnet_net_lock(LNET_LOCK_EX);
570 if (nid_same(&nid, &lp->lp_primary_nid) && lp->lp_nnis != 1 && force) {
571 struct lnet_peer_ni *lpni2;
572 /* assign the next peer_ni to be the primary */
573 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
575 lp->lp_primary_nid = lpni2->lpni_nid;
577 rc = lnet_peer_ni_del_locked(lpni, force);
579 lnet_net_unlock(LNET_LOCK_EX);
582 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
583 libcfs_nidstr(&primary_nid), libcfs_nidstr(&nid),
590 lnet_peer_table_cleanup_locked(struct lnet_net *net,
591 struct lnet_peer_table *ptable)
594 struct lnet_peer_ni *next;
595 struct lnet_peer_ni *lpni;
596 struct lnet_peer *peer;
598 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
599 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
601 if (net != NULL && net != lpni->lpni_net)
604 peer = lpni->lpni_peer_net->lpn_peer;
605 if (!nid_same(&peer->lp_primary_nid,
607 lnet_peer_ni_del_locked(lpni, false);
611 * Removing the primary NID implies removing
612 * the entire peer. Advance next beyond any
613 * peer_ni that belongs to the same peer.
615 list_for_each_entry_from(next, &ptable->pt_hash[i],
617 if (next->lpni_peer_net->lpn_peer != peer)
620 lnet_peer_del_locked(peer);
626 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
628 wait_var_event_warning(&ptable->pt_zombies,
629 ptable->pt_zombies == 0,
630 "Waiting for %d zombies on peer table\n",
635 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
636 struct lnet_peer_table *ptable)
638 struct lnet_peer_ni *lp;
639 struct lnet_peer_ni *tmp;
640 struct lnet_nid gw_nid;
643 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
644 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
646 if (net != lp->lpni_net)
649 if (!lnet_isrouter(lp))
652 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
654 lnet_net_unlock(LNET_LOCK_EX);
655 lnet_del_route(LNET_NET_ANY, &gw_nid);
656 lnet_net_lock(LNET_LOCK_EX);
662 lnet_peer_tables_cleanup(struct lnet_net *net)
665 struct lnet_peer_table *ptable;
667 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
668 /* If just deleting the peers for a NI, get rid of any routes these
669 * peers are gateways for. */
670 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
671 lnet_net_lock(LNET_LOCK_EX);
672 lnet_peer_table_del_rtrs_locked(net, ptable);
673 lnet_net_unlock(LNET_LOCK_EX);
676 /* Start the cleanup process */
677 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
678 lnet_net_lock(LNET_LOCK_EX);
679 lnet_peer_table_cleanup_locked(net, ptable);
680 lnet_net_unlock(LNET_LOCK_EX);
683 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
684 lnet_peer_ni_finalize_wait(ptable);
687 static struct lnet_peer_ni *
688 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, struct lnet_nid *nid)
690 struct list_head *peers;
691 struct lnet_peer_ni *lp;
693 if (the_lnet.ln_state != LNET_STATE_RUNNING)
696 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
697 list_for_each_entry(lp, peers, lpni_hashlist) {
698 if (nid_same(&lp->lpni_nid, nid)) {
699 lnet_peer_ni_addref_locked(lp);
707 struct lnet_peer_ni *
708 lnet_peer_ni_find_locked(struct lnet_nid *nid)
710 struct lnet_peer_ni *lpni;
711 struct lnet_peer_table *ptable;
714 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
716 ptable = the_lnet.ln_peer_tables[cpt];
717 lpni = lnet_get_peer_ni_locked(ptable, nid);
722 struct lnet_peer_ni *
723 lnet_peer_ni_get_locked(struct lnet_peer *lp, struct lnet_nid *nid)
725 struct lnet_peer_net *lpn;
726 struct lnet_peer_ni *lpni;
728 lpn = lnet_peer_get_net_locked(lp, LNET_NID_NET(nid));
732 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
733 if (nid_same(&lpni->lpni_nid, nid))
741 lnet_find_peer(struct lnet_nid *nid)
743 struct lnet_peer_ni *lpni;
744 struct lnet_peer *lp = NULL;
747 cpt = lnet_net_lock_current();
748 lpni = lnet_peer_ni_find_locked(nid);
750 lp = lpni->lpni_peer_net->lpn_peer;
751 lnet_peer_addref_locked(lp);
752 lnet_peer_ni_decref_locked(lpni);
754 lnet_net_unlock(cpt);
759 struct lnet_peer_net *
760 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
762 struct lnet_peer_net *net;
765 /* no net id provided return the first net */
766 net = list_first_entry_or_null(&lp->lp_peer_nets,
767 struct lnet_peer_net,
773 /* find the net after the one provided */
774 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
775 if (net->lpn_net_id == prev_lpn_id) {
777 * if we reached the end of the list loop to the
780 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
781 return list_first_entry_or_null(&lp->lp_peer_nets,
782 struct lnet_peer_net,
785 return list_next_entry(net, lpn_peer_nets);
792 struct lnet_peer_ni *
793 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
794 struct lnet_peer_net *peer_net,
795 struct lnet_peer_ni *prev)
797 struct lnet_peer_ni *lpni;
798 struct lnet_peer_net *net = peer_net;
802 if (list_empty(&peer->lp_peer_nets))
805 net = list_entry(peer->lp_peer_nets.next,
806 struct lnet_peer_net,
809 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
815 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
817 * if you reached the end of the peer ni list and the peer
818 * net is specified then there are no more peer nis in that
825 * we reached the end of this net ni list. move to the
828 if (prev->lpni_peer_net->lpn_peer_nets.next ==
830 /* no more nets and no more NIs. */
833 /* get the next net */
834 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
835 struct lnet_peer_net,
837 /* get the ni on it */
838 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
844 /* there are more nis left */
845 lpni = list_entry(prev->lpni_peer_nis.next,
846 struct lnet_peer_ni, lpni_peer_nis);
851 /* Call with the ln_api_mutex held */
852 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
854 struct lnet_process_id id;
855 struct lnet_peer_table *ptable;
856 struct lnet_peer *lp;
865 if (the_lnet.ln_state != LNET_STATE_RUNNING)
868 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
871 * Count the number of peers, and return E2BIG if the buffer
872 * is too small. We'll also return the desired size.
875 for (cpt = 0; cpt < lncpt; cpt++) {
876 ptable = the_lnet.ln_peer_tables[cpt];
877 count += ptable->pt_peers;
879 size = count * sizeof(*ids);
884 * Walk the peer lists and copy out the primary nids.
885 * This is safe because the peer lists are only modified
886 * while the ln_api_mutex is held. So we don't need to
887 * hold the lnet_net_lock as well, and can therefore
888 * directly call copy_to_user().
891 memset(&id, 0, sizeof(id));
892 id.pid = LNET_PID_LUSTRE;
894 for (cpt = 0; cpt < lncpt; cpt++) {
895 ptable = the_lnet.ln_peer_tables[cpt];
896 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
897 if (!nid_is_nid4(&lp->lp_primary_nid))
901 id.nid = lnet_nid_to_nid4(&lp->lp_primary_nid);
902 if (copy_to_user(&ids[i], &id, sizeof(id)))
915 * Start pushes to peers that need to be updated for a configuration
916 * change on this node.
919 lnet_push_update_to_peers(int force)
921 struct lnet_peer_table *ptable;
922 struct lnet_peer *lp;
926 lnet_net_lock(LNET_LOCK_EX);
927 if (lnet_peer_discovery_disabled)
929 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
930 for (cpt = 0; cpt < lncpt; cpt++) {
931 ptable = the_lnet.ln_peer_tables[cpt];
932 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
934 spin_lock(&lp->lp_lock);
935 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
936 lp->lp_state |= LNET_PEER_FORCE_PUSH;
937 spin_unlock(&lp->lp_lock);
939 if (lnet_peer_needs_push(lp))
940 lnet_peer_queue_for_discovery(lp);
943 lnet_net_unlock(LNET_LOCK_EX);
944 wake_up(&the_lnet.ln_dc_waitq);
947 /* find the NID in the preferred gateways for the remote peer
949 * false: list is not empty and NID is not preferred
950 * false: list is empty
951 * true: nid is found in the list
954 lnet_peer_is_pref_rtr_locked(struct lnet_peer_ni *lpni,
955 struct lnet_nid *gw_nid)
957 struct lnet_nid_list *ne;
959 CDEBUG(D_NET, "%s: rtr pref emtpy: %d\n",
960 libcfs_nidstr(&lpni->lpni_nid),
961 list_empty(&lpni->lpni_rtr_pref_nids));
963 if (list_empty(&lpni->lpni_rtr_pref_nids))
966 /* iterate through all the preferred NIDs and see if any of them
967 * matches the provided gw_nid
969 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
970 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
971 libcfs_nidstr(&ne->nl_nid),
972 libcfs_nidstr(gw_nid));
973 if (nid_same(&ne->nl_nid, gw_nid))
981 lnet_peer_clr_pref_rtrs(struct lnet_peer_ni *lpni)
983 struct list_head zombies;
984 struct lnet_nid_list *ne;
985 struct lnet_nid_list *tmp;
986 int cpt = lpni->lpni_cpt;
988 INIT_LIST_HEAD(&zombies);
991 list_splice_init(&lpni->lpni_rtr_pref_nids, &zombies);
992 lnet_net_unlock(cpt);
994 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
995 list_del(&ne->nl_list);
996 LIBCFS_FREE(ne, sizeof(*ne));
1001 lnet_peer_add_pref_rtr(struct lnet_peer_ni *lpni,
1002 struct lnet_nid *gw_nid)
1004 int cpt = lpni->lpni_cpt;
1005 struct lnet_nid_list *ne = NULL;
1007 /* This function is called with api_mutex held. When the api_mutex
1008 * is held the list can not be modified, as it is only modified as
1009 * a result of applying a UDSP and that happens under api_mutex
1012 __must_hold(&the_lnet.ln_api_mutex);
1014 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
1015 if (nid_same(&ne->nl_nid, gw_nid))
1019 LIBCFS_CPT_ALLOC(ne, lnet_cpt_table(), cpt, sizeof(*ne));
1023 ne->nl_nid = *gw_nid;
1025 /* Lock the cpt to protect against addition and checks in the
1026 * selection algorithm
1029 list_add(&ne->nl_list, &lpni->lpni_rtr_pref_nids);
1030 lnet_net_unlock(cpt);
1036 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
1037 * this is a preferred point-to-point path. Call with lnet_net_lock in
1041 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1043 struct lnet_nid_list *ne;
1045 if (lpni->lpni_pref_nnids == 0)
1047 if (lpni->lpni_pref_nnids == 1)
1048 return nid_same(&lpni->lpni_pref.nid, nid);
1049 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1050 if (nid_same(&ne->nl_nid, nid))
1057 * Set a single ni as preferred, provided no preferred ni is already
1058 * defined. Only to be used for non-multi-rail peer_ni.
1061 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni,
1062 struct lnet_nid *nid)
1068 spin_lock(&lpni->lpni_lock);
1069 if (LNET_NID_IS_ANY(nid)) {
1071 } else if (lpni->lpni_pref_nnids > 0) {
1073 } else if (lpni->lpni_pref_nnids == 0) {
1074 lpni->lpni_pref.nid = *nid;
1075 lpni->lpni_pref_nnids = 1;
1076 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
1078 spin_unlock(&lpni->lpni_lock);
1080 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1081 libcfs_nidstr(&lpni->lpni_nid), libcfs_nidstr(nid), rc);
1086 * Clear the preferred NID from a non-multi-rail peer_ni, provided
1087 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
1090 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
1094 spin_lock(&lpni->lpni_lock);
1095 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
1096 lpni->lpni_pref_nnids = 0;
1097 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1098 } else if (lpni->lpni_pref_nnids == 0) {
1103 spin_unlock(&lpni->lpni_lock);
1105 CDEBUG(D_NET, "peer %s: %d\n",
1106 libcfs_nidstr(&lpni->lpni_nid), rc);
1111 lnet_peer_ni_set_selection_priority(struct lnet_peer_ni *lpni, __u32 priority)
1113 lpni->lpni_sel_priority = priority;
1117 * Clear the preferred NIDs from a non-multi-rail peer.
1120 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
1122 struct lnet_peer_ni *lpni = NULL;
1124 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
1125 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
1129 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1131 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1132 struct lnet_nid_list *ne1 = NULL;
1133 struct lnet_nid_list *ne2 = NULL;
1134 struct lnet_nid *tmp_nid = NULL;
1137 if (LNET_NID_IS_ANY(nid)) {
1142 if (lpni->lpni_pref_nnids == 1 &&
1143 nid_same(&lpni->lpni_pref.nid, nid)) {
1148 /* A non-MR node may have only one preferred NI per peer_ni */
1149 if (lpni->lpni_pref_nnids > 0 &&
1150 !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1155 /* add the new preferred nid to the list of preferred nids */
1156 if (lpni->lpni_pref_nnids != 0) {
1157 size_t alloc_size = sizeof(*ne1);
1159 if (lpni->lpni_pref_nnids == 1) {
1160 tmp_nid = &lpni->lpni_pref.nid;
1161 INIT_LIST_HEAD(&lpni->lpni_pref.nids);
1164 list_for_each_entry(ne1, &lpni->lpni_pref.nids, nl_list) {
1165 if (nid_same(&ne1->nl_nid, nid)) {
1171 LIBCFS_CPT_ALLOC(ne1, lnet_cpt_table(), lpni->lpni_cpt,
1178 /* move the originally stored nid to the list */
1179 if (lpni->lpni_pref_nnids == 1) {
1180 LIBCFS_CPT_ALLOC(ne2, lnet_cpt_table(),
1181 lpni->lpni_cpt, alloc_size);
1186 INIT_LIST_HEAD(&ne2->nl_list);
1187 ne2->nl_nid = *tmp_nid;
1192 lnet_net_lock(LNET_LOCK_EX);
1193 spin_lock(&lpni->lpni_lock);
1194 if (lpni->lpni_pref_nnids == 0) {
1195 lpni->lpni_pref.nid = *nid;
1198 list_add_tail(&ne2->nl_list, &lpni->lpni_pref.nids);
1199 list_add_tail(&ne1->nl_list, &lpni->lpni_pref.nids);
1201 lpni->lpni_pref_nnids++;
1202 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1203 spin_unlock(&lpni->lpni_lock);
1204 lnet_net_unlock(LNET_LOCK_EX);
1207 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1208 spin_lock(&lpni->lpni_lock);
1209 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1210 spin_unlock(&lpni->lpni_lock);
1212 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1213 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nidstr(nid), rc);
1218 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1220 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1221 struct lnet_nid_list *ne = NULL;
1224 if (lpni->lpni_pref_nnids == 0) {
1229 if (lpni->lpni_pref_nnids == 1) {
1230 if (!nid_same(&lpni->lpni_pref.nid, nid)) {
1235 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1236 if (nid_same(&ne->nl_nid, nid))
1237 goto remove_nid_entry;
1245 lnet_net_lock(LNET_LOCK_EX);
1246 spin_lock(&lpni->lpni_lock);
1247 if (lpni->lpni_pref_nnids == 1)
1248 lpni->lpni_pref.nid = LNET_ANY_NID;
1250 list_del_init(&ne->nl_list);
1251 if (lpni->lpni_pref_nnids == 2) {
1252 struct lnet_nid_list *ne, *tmp;
1254 list_for_each_entry_safe(ne, tmp,
1255 &lpni->lpni_pref.nids,
1257 lpni->lpni_pref.nid = ne->nl_nid;
1258 list_del_init(&ne->nl_list);
1259 LIBCFS_FREE(ne, sizeof(*ne));
1263 lpni->lpni_pref_nnids--;
1264 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1265 spin_unlock(&lpni->lpni_lock);
1266 lnet_net_unlock(LNET_LOCK_EX);
1269 LIBCFS_FREE(ne, sizeof(*ne));
1271 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1272 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nidstr(nid), rc);
1277 lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni)
1279 struct list_head zombies;
1280 struct lnet_nid_list *ne;
1281 struct lnet_nid_list *tmp;
1283 INIT_LIST_HEAD(&zombies);
1285 lnet_net_lock(LNET_LOCK_EX);
1286 if (lpni->lpni_pref_nnids == 1)
1287 lpni->lpni_pref.nid = LNET_ANY_NID;
1288 else if (lpni->lpni_pref_nnids > 1)
1289 list_splice_init(&lpni->lpni_pref.nids, &zombies);
1290 lpni->lpni_pref_nnids = 0;
1291 lnet_net_unlock(LNET_LOCK_EX);
1293 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1294 list_del_init(&ne->nl_list);
1295 LIBCFS_FREE(ne, sizeof(*ne));
1300 lnet_peer_primary_nid_locked(struct lnet_nid *nid, struct lnet_nid *result)
1302 struct lnet_peer_ni *lpni;
1305 lpni = lnet_peer_ni_find_locked(nid);
1307 *result = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1308 lnet_peer_ni_decref_locked(lpni);
1313 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1314 __must_hold(&lp->lp_lock)
1316 if (lnet_peer_discovery_disabled)
1319 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1320 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1331 lnet_is_discovery_disabled(struct lnet_peer *lp)
1335 spin_lock(&lp->lp_lock);
1336 rc = lnet_is_discovery_disabled_locked(lp);
1337 spin_unlock(&lp->lp_lock);
1343 LNetAddPeer(lnet_nid_t *nids, __u32 num_nids)
1345 lnet_nid_t pnid = 0;
1349 if (!nids || num_nids < 1)
1352 rc = LNetNIInit(LNET_PID_ANY);
1356 mutex_lock(&the_lnet.ln_api_mutex);
1358 mr = lnet_peer_discovery_disabled == 0;
1361 for (i = 0; i < num_nids; i++) {
1362 if (nids[i] == LNET_NID_LO_0)
1367 rc = lnet_add_peer_ni(pnid, LNET_NID_ANY, mr, true);
1368 } else if (lnet_peer_discovery_disabled) {
1369 rc = lnet_add_peer_ni(nids[i], LNET_NID_ANY, mr, true);
1371 rc = lnet_add_peer_ni(pnid, nids[i], mr, true);
1374 if (rc && rc != -EEXIST)
1379 mutex_unlock(&the_lnet.ln_api_mutex);
1383 return rc == -EEXIST ? 0 : rc;
1385 EXPORT_SYMBOL(LNetAddPeer);
1387 void LNetPrimaryNID(struct lnet_nid *nid)
1389 struct lnet_peer *lp;
1390 struct lnet_peer_ni *lpni;
1391 struct lnet_nid orig;
1395 if (!nid || nid_is_lo0(nid))
1399 cpt = lnet_net_lock_current();
1400 lpni = lnet_peerni_by_nid_locked(nid, NULL, cpt);
1405 lp = lpni->lpni_peer_net->lpn_peer;
1407 /* If discovery is disabled locally then we needn't bother running
1408 * discovery here because discovery will not modify whatever
1409 * primary NID is currently set for this peer. If the specified peer is
1410 * down then this discovery can introduce long delays into the mount
1411 * process, so skip it if it isn't necessary.
1413 while (!lnet_peer_discovery_disabled && !lnet_peer_is_uptodate(lp)) {
1414 spin_lock(&lp->lp_lock);
1415 /* force a full discovery cycle */
1416 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1417 spin_unlock(&lp->lp_lock);
1419 rc = lnet_discover_peer_locked(lpni, cpt, true);
1422 /* The lpni (or lp) for this NID may have changed and our ref is
1423 * the only thing keeping the old one around. Release the ref
1424 * and lookup the lpni again
1426 lnet_peer_ni_decref_locked(lpni);
1427 lpni = lnet_peer_ni_find_locked(nid);
1432 lp = lpni->lpni_peer_net->lpn_peer;
1434 /* If we find that the peer has discovery disabled then we will
1435 * not modify whatever primary NID is currently set for this
1436 * peer. Thus, we can break out of this loop even if the peer
1437 * is not fully up to date.
1439 if (lnet_is_discovery_disabled(lp))
1442 *nid = lp->lp_primary_nid;
1444 lnet_peer_ni_decref_locked(lpni);
1446 lnet_net_unlock(cpt);
1448 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nidstr(&orig),
1449 libcfs_nidstr(nid), rc);
1451 EXPORT_SYMBOL(LNetPrimaryNID);
1453 struct lnet_peer_net *
1454 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1456 struct lnet_peer_net *peer_net;
1457 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1458 if (peer_net->lpn_net_id == net_id)
1465 * Attach a peer_ni to a peer_net and peer. This function assumes
1466 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1467 * may be attached to a different peer, in which case it will be
1468 * properly detached first. The whole operation is done atomically.
1470 * This function consumes the reference on lpni and Always returns 0.
1471 * This is the last function called from functions that do return an
1472 * int, so returning 0 here allows the compiler to do a tail call.
1475 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1476 struct lnet_peer_net *lpn,
1477 struct lnet_peer_ni *lpni,
1480 struct lnet_peer_table *ptable;
1481 bool new_lpn = false;
1484 /* Install the new peer_ni */
1485 lnet_net_lock(LNET_LOCK_EX);
1486 /* Add peer_ni to global peer table hash, if necessary. */
1487 if (list_empty(&lpni->lpni_hashlist)) {
1488 int hash = lnet_nid2peerhash(&lpni->lpni_nid);
1490 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1491 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1492 ptable->pt_version++;
1493 lnet_peer_ni_addref_locked(lpni);
1496 /* Detach the peer_ni from an existing peer, if necessary. */
1497 if (lpni->lpni_peer_net) {
1498 LASSERT(lpni->lpni_peer_net != lpn);
1499 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1500 lnet_peer_detach_peer_ni_locked(lpni);
1501 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1502 lpni->lpni_peer_net = NULL;
1505 /* Add peer_ni to peer_net */
1506 lpni->lpni_peer_net = lpn;
1507 if (nid_same(&lp->lp_primary_nid, &lpni->lpni_nid))
1508 list_add(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1510 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1511 lnet_update_peer_net_healthv(lpni);
1512 lnet_peer_net_addref_locked(lpn);
1514 /* Add peer_net to peer */
1515 if (!lpn->lpn_peer) {
1518 if (nid_same(&lp->lp_primary_nid, &lpni->lpni_nid))
1519 list_add(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1521 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1522 lnet_peer_addref_locked(lp);
1525 /* Add peer to global peer list, if necessary */
1526 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1527 if (list_empty(&lp->lp_peer_list)) {
1528 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1533 /* Update peer state */
1534 spin_lock(&lp->lp_lock);
1535 if (flags & LNET_PEER_CONFIGURED) {
1536 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1537 lp->lp_state |= LNET_PEER_CONFIGURED;
1539 if (flags & LNET_PEER_MULTI_RAIL) {
1540 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1541 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1542 lnet_peer_clr_non_mr_pref_nids(lp);
1545 spin_unlock(&lp->lp_lock);
1551 rc = lnet_udsp_apply_policies_on_lpn(lpn);
1553 CERROR("Failed to apply UDSPs on lpn %s\n",
1554 libcfs_net2str(lpn->lpn_net_id));
1556 rc = lnet_udsp_apply_policies_on_lpni(lpni);
1558 CERROR("Failed to apply UDSPs on lpni %s\n",
1559 libcfs_nidstr(&lpni->lpni_nid));
1561 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1562 libcfs_nidstr(&lp->lp_primary_nid),
1563 libcfs_nidstr(&lpni->lpni_nid), flags);
1564 lnet_peer_ni_decref_locked(lpni);
1565 lnet_net_unlock(LNET_LOCK_EX);
1571 * Create a new peer, with nid as its primary nid.
1573 * Call with the lnet_api_mutex held.
1576 lnet_peer_add(struct lnet_nid *nid, unsigned int flags)
1578 struct lnet_peer *lp;
1579 struct lnet_peer_net *lpn;
1580 struct lnet_peer_ni *lpni;
1586 * No need for the lnet_net_lock here, because the
1587 * lnet_api_mutex is held.
1589 lpni = lnet_peer_ni_find_locked(nid);
1591 /* A peer with this NID already exists. */
1592 lp = lpni->lpni_peer_net->lpn_peer;
1593 lnet_peer_ni_decref_locked(lpni);
1595 * This is an error if the peer was configured and the
1596 * primary NID differs or an attempt is made to change
1597 * the Multi-Rail flag. Otherwise the assumption is
1598 * that an existing peer is being modified.
1600 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1601 if (!nid_same(&lp->lp_primary_nid, nid))
1603 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1606 } else if (!(flags & LNET_PEER_CONFIGURED)) {
1607 if (nid_same(&lp->lp_primary_nid, nid)) {
1612 /* Delete and recreate as a configured peer. */
1613 rc = lnet_peer_del(lp);
1618 /* Create peer, peer_net, and peer_ni. */
1620 lp = lnet_peer_alloc(nid);
1623 lpn = lnet_peer_net_alloc(LNET_NID_NET(nid));
1626 lpni = lnet_peer_ni_alloc(nid);
1630 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1633 LIBCFS_FREE(lpn, sizeof(*lpn));
1635 LIBCFS_FREE(lp, sizeof(*lp));
1637 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1638 libcfs_nidstr(nid), flags, rc);
1643 * Add a NID to a peer. Call with ln_api_mutex held.
1646 * -EPERM: Non-DLC addition to a DLC-configured peer.
1647 * -EEXIST: The NID was configured by DLC for a different peer.
1648 * -ENOMEM: Out of memory.
1649 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1650 * non-multi-rail peer.
1653 lnet_peer_add_nid(struct lnet_peer *lp, struct lnet_nid *nid,
1656 struct lnet_peer_net *lpn;
1657 struct lnet_peer_ni *lpni;
1663 /* A configured peer can only be updated through configuration. */
1664 if (!(flags & LNET_PEER_CONFIGURED)) {
1665 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1672 * The MULTI_RAIL flag can be set but not cleared, because
1673 * that would leave the peer struct in an invalid state.
1675 if (flags & LNET_PEER_MULTI_RAIL) {
1676 spin_lock(&lp->lp_lock);
1677 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1678 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1679 lnet_peer_clr_non_mr_pref_nids(lp);
1681 spin_unlock(&lp->lp_lock);
1682 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1687 lpni = lnet_peer_ni_find_locked(nid);
1690 * A peer_ni already exists. This is only a problem if
1691 * it is not connected to this peer and was configured
1694 if (lpni->lpni_peer_net->lpn_peer == lp)
1696 if (lnet_peer_ni_is_configured(lpni)) {
1700 /* If this is the primary NID, destroy the peer. */
1701 if (lnet_peer_ni_is_primary(lpni)) {
1702 struct lnet_peer *rtr_lp =
1703 lpni->lpni_peer_net->lpn_peer;
1704 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1706 * if we're trying to delete a router it means
1707 * we're moving this peer NI to a new peer so must
1708 * transfer router properties to the new peer
1710 if (rtr_refcount > 0) {
1711 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1712 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1714 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1715 lnet_peer_ni_decref_locked(lpni);
1716 lpni = lnet_peer_ni_alloc(nid);
1723 lpni = lnet_peer_ni_alloc(nid);
1731 * Get the peer_net. Check that we're not adding a second
1732 * peer_ni on a peer_net of a non-multi-rail peer.
1734 lpn = lnet_peer_get_net_locked(lp, LNET_NID_NET(nid));
1736 lpn = lnet_peer_net_alloc(LNET_NID_NET(nid));
1741 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1746 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1749 lnet_peer_ni_decref_locked(lpni);
1751 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1752 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nidstr(nid),
1758 * Update the primary NID of a peer, if possible.
1760 * Call with the lnet_api_mutex held.
1763 lnet_peer_set_primary_nid(struct lnet_peer *lp, struct lnet_nid *nid,
1766 struct lnet_nid old = lp->lp_primary_nid;
1769 if (nid_same(&lp->lp_primary_nid, nid))
1772 lp->lp_primary_nid = *nid;
1774 rc = lnet_peer_add_nid(lp, nid, flags);
1776 lp->lp_primary_nid = old;
1780 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1781 libcfs_nidstr(&old), libcfs_nidstr(nid), rc);
1787 * lpni creation initiated due to traffic either sending or receiving.
1788 * Callers must hold ln_api_mutex
1789 * Ref taken on lnet_peer_ni returned by this function
1791 static struct lnet_peer_ni *
1792 lnet_peer_ni_traffic_add(struct lnet_nid *nid, struct lnet_nid *pref)
1793 __must_hold(&the_lnet.ln_api_mutex)
1795 struct lnet_peer *lp = NULL;
1796 struct lnet_peer_net *lpn = NULL;
1797 struct lnet_peer_ni *lpni;
1801 if (LNET_NID_IS_ANY(nid)) {
1806 /* lnet_net_lock is not needed here because ln_api_lock is held */
1807 lpni = lnet_peer_ni_find_locked(nid);
1810 * We must have raced with another thread. Since we
1811 * know next to nothing about a peer_ni created by
1812 * traffic, we just assume everything is ok and
1818 /* Create peer, peer_net, and peer_ni. */
1820 lp = lnet_peer_alloc(nid);
1823 lpn = lnet_peer_net_alloc(LNET_NID_NET(nid));
1826 lpni = lnet_peer_ni_alloc(nid);
1829 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1831 /* lnet_peer_attach_peer_ni() always returns 0 */
1832 rc = lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1834 lnet_peer_ni_addref_locked(lpni);
1839 LIBCFS_FREE(lpn, sizeof(*lpn));
1841 LIBCFS_FREE(lp, sizeof(*lp));
1845 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nidstr(nid), rc);
1850 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1852 * This API handles the following combinations:
1853 * Create a peer with its primary NI if only the prim_nid is provided
1854 * Add a NID to a peer identified by the prim_nid. The peer identified
1855 * by the prim_nid must already exist.
1856 * The peer being created may be non-MR.
1858 * The caller must hold ln_api_mutex. This prevents the peer from
1859 * being created/modified/deleted by a different thread.
1862 lnet_add_peer_ni(lnet_nid_t prim_nid4, lnet_nid_t nid4, bool mr, bool temp)
1864 struct lnet_nid prim_nid, nid;
1865 struct lnet_peer *lp = NULL;
1866 struct lnet_peer_ni *lpni;
1867 unsigned int flags = 0;
1869 /* The prim_nid must always be specified */
1870 if (prim_nid4 == LNET_NID_ANY)
1873 lnet_nid4_to_nid(prim_nid4, &prim_nid);
1874 lnet_nid4_to_nid(nid4, &nid);
1877 flags = LNET_PEER_CONFIGURED;
1880 flags |= LNET_PEER_MULTI_RAIL;
1883 * If nid isn't specified, we must create a new peer with
1884 * prim_nid as its primary nid.
1886 if (nid4 == LNET_NID_ANY)
1887 return lnet_peer_add(&prim_nid, flags);
1889 /* Look up the prim_nid, which must exist. */
1890 lpni = lnet_peer_ni_find_locked(&prim_nid);
1893 lnet_peer_ni_decref_locked(lpni);
1894 lp = lpni->lpni_peer_net->lpn_peer;
1896 /* Peer must have been configured. */
1897 if (!temp && !(lp->lp_state & LNET_PEER_CONFIGURED)) {
1898 CDEBUG(D_NET, "peer %s was not configured\n",
1899 libcfs_nidstr(&prim_nid));
1903 /* Primary NID must match */
1904 if (!nid_same(&lp->lp_primary_nid, &prim_nid)) {
1905 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1906 libcfs_nidstr(&prim_nid),
1907 libcfs_nidstr(&lp->lp_primary_nid));
1911 /* Multi-Rail flag must match. */
1912 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1913 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1914 libcfs_nidstr(&prim_nid));
1918 return lnet_peer_add_nid(lp, &nid, flags);
1922 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1924 * This API handles the following combinations:
1925 * Delete a NI from a peer if both prim_nid and nid are provided.
1926 * Delete a peer if only prim_nid is provided.
1927 * Delete a peer if its primary nid is provided.
1929 * The caller must hold ln_api_mutex. This prevents the peer from
1930 * being modified/deleted by a different thread.
1933 lnet_del_peer_ni(lnet_nid_t prim_nid4, lnet_nid_t nid)
1935 struct lnet_peer *lp;
1936 struct lnet_peer_ni *lpni;
1938 struct lnet_nid prim_nid;
1940 if (prim_nid4 == LNET_NID_ANY)
1942 lnet_nid4_to_nid(prim_nid4, &prim_nid);
1944 lpni = lnet_peer_ni_find_locked(&prim_nid);
1947 lnet_peer_ni_decref_locked(lpni);
1948 lp = lpni->lpni_peer_net->lpn_peer;
1950 if (!nid_same(&prim_nid, &lp->lp_primary_nid)) {
1951 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1952 libcfs_nidstr(&prim_nid),
1953 libcfs_nidstr(&lp->lp_primary_nid));
1957 lnet_net_lock(LNET_LOCK_EX);
1958 if (lp->lp_rtr_refcount > 0) {
1959 lnet_net_unlock(LNET_LOCK_EX);
1960 CERROR("%s is a router. Can not be deleted\n",
1961 libcfs_nidstr(&prim_nid));
1964 lnet_net_unlock(LNET_LOCK_EX);
1966 if (nid == LNET_NID_ANY || nid == lnet_nid_to_nid4(&lp->lp_primary_nid))
1967 return lnet_peer_del(lp);
1969 flags = LNET_PEER_CONFIGURED;
1970 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1971 flags |= LNET_PEER_MULTI_RAIL;
1973 return lnet_peer_del_nid(lp, nid, flags);
1977 lnet_destroy_peer_ni_locked(struct kref *ref)
1979 struct lnet_peer_ni *lpni = container_of(ref, struct lnet_peer_ni,
1981 struct lnet_peer_table *ptable;
1982 struct lnet_peer_net *lpn;
1984 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nidstr(&lpni->lpni_nid));
1986 LASSERT(kref_read(&lpni->lpni_kref) == 0);
1987 LASSERT(list_empty(&lpni->lpni_txq));
1988 LASSERT(lpni->lpni_txqnob == 0);
1989 LASSERT(list_empty(&lpni->lpni_peer_nis));
1990 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1992 lpn = lpni->lpni_peer_net;
1993 lpni->lpni_peer_net = NULL;
1994 lpni->lpni_net = NULL;
1996 if (!list_empty(&lpni->lpni_hashlist)) {
1997 /* remove the peer ni from the zombie list */
1998 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1999 spin_lock(&ptable->pt_zombie_lock);
2000 list_del_init(&lpni->lpni_hashlist);
2001 ptable->pt_zombies--;
2002 spin_unlock(&ptable->pt_zombie_lock);
2005 if (lpni->lpni_pref_nnids > 1) {
2006 struct lnet_nid_list *ne, *tmp;
2008 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
2010 list_del_init(&ne->nl_list);
2011 LIBCFS_FREE(ne, sizeof(*ne));
2014 LIBCFS_FREE(lpni, sizeof(*lpni));
2017 lnet_peer_net_decref_locked(lpn);
2020 struct lnet_peer_ni *
2021 lnet_nid2peerni_ex(struct lnet_nid *nid)
2022 __must_hold(&the_lnet.ln_api_mutex)
2024 struct lnet_peer_ni *lpni = NULL;
2026 if (the_lnet.ln_state != LNET_STATE_RUNNING)
2027 return ERR_PTR(-ESHUTDOWN);
2030 * find if a peer_ni already exists.
2031 * If so then just return that.
2033 lpni = lnet_peer_ni_find_locked(nid);
2037 lnet_net_unlock(LNET_LOCK_EX);
2039 lpni = lnet_peer_ni_traffic_add(nid, NULL);
2041 lnet_net_lock(LNET_LOCK_EX);
2047 * Get a peer_ni for the given nid, create it if necessary. Takes a
2048 * hold on the peer_ni.
2050 struct lnet_peer_ni *
2051 lnet_peerni_by_nid_locked(struct lnet_nid *nid,
2052 struct lnet_nid *pref, int cpt)
2054 struct lnet_peer_ni *lpni = NULL;
2056 if (the_lnet.ln_state != LNET_STATE_RUNNING)
2057 return ERR_PTR(-ESHUTDOWN);
2060 * find if a peer_ni already exists.
2061 * If so then just return that.
2063 lpni = lnet_peer_ni_find_locked(nid);
2069 * use the lnet_api_mutex to serialize the creation of the peer_ni
2070 * and the creation/deletion of the local ni/net. When a local ni is
2071 * created, if there exists a set of peer_nis on that network,
2072 * they need to be traversed and updated. When a local NI is
2073 * deleted, which could result in a network being deleted, then
2074 * all peer nis on that network need to be removed as well.
2076 * Creation through traffic should also be serialized with
2077 * creation through DLC.
2079 lnet_net_unlock(cpt);
2080 mutex_lock(&the_lnet.ln_api_mutex);
2082 * the_lnet.ln_state is only modified under the ln_api_lock, so a single
2083 * check here is sufficent.
2085 if (the_lnet.ln_state == LNET_STATE_RUNNING)
2086 lpni = lnet_peer_ni_traffic_add(nid, pref);
2088 mutex_unlock(&the_lnet.ln_api_mutex);
2091 /* Lock has been dropped, check again for shutdown. */
2092 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2093 if (!IS_ERR_OR_NULL(lpni))
2094 lnet_peer_ni_decref_locked(lpni);
2095 lpni = ERR_PTR(-ESHUTDOWN);
2102 lnet_peer_gw_discovery(struct lnet_peer *lp)
2106 spin_lock(&lp->lp_lock);
2107 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
2109 spin_unlock(&lp->lp_lock);
2115 lnet_peer_is_uptodate(struct lnet_peer *lp)
2119 spin_lock(&lp->lp_lock);
2120 rc = lnet_peer_is_uptodate_locked(lp);
2121 spin_unlock(&lp->lp_lock);
2126 * Is a peer uptodate from the point of view of discovery?
2128 * If it is currently being processed, obviously not.
2129 * A forced Ping or Push is also handled by the discovery thread.
2131 * Otherwise look at whether the peer needs rediscovering.
2134 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
2135 __must_hold(&lp->lp_lock)
2139 if (lp->lp_state & (LNET_PEER_DISCOVERING |
2140 LNET_PEER_FORCE_PING |
2141 LNET_PEER_FORCE_PUSH)) {
2143 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
2145 } else if (lnet_peer_needs_push(lp)) {
2147 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
2148 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
2159 /* Add the message to the peer's lp_dc_pendq and queue the peer for discovery */
2161 lnet_peer_queue_message(struct lnet_peer *lp, struct lnet_msg *msg)
2163 /* The discovery thread holds net_lock/EX and lp_lock when it splices
2164 * the lp_dc_pendq onto a local list for resending. Thus, we do the same
2165 * when adding to the list and queuing the peer to ensure that we do not
2166 * strand any messages on the lp_dc_pendq. This scheme ensures the
2167 * message will be resent even if the peer is already being discovered.
2168 * Therefore we needn't check the return value of
2169 * lnet_peer_queue_for_discovery(lp).
2171 lnet_net_lock(LNET_LOCK_EX);
2172 spin_lock(&lp->lp_lock);
2173 list_add_tail(&msg->msg_list, &lp->lp_dc_pendq);
2174 spin_unlock(&lp->lp_lock);
2175 lnet_peer_queue_for_discovery(lp);
2176 lnet_net_unlock(LNET_LOCK_EX);
2180 * Queue a peer for the attention of the discovery thread. Call with
2181 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
2182 * -EALREADY if the peer was already queued.
2184 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
2188 spin_lock(&lp->lp_lock);
2189 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2190 lp->lp_state |= LNET_PEER_DISCOVERING;
2191 spin_unlock(&lp->lp_lock);
2192 if (list_empty(&lp->lp_dc_list)) {
2193 lnet_peer_addref_locked(lp);
2194 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2195 wake_up(&the_lnet.ln_dc_waitq);
2201 CDEBUG(D_NET, "Queue peer %s: %d\n",
2202 libcfs_nidstr(&lp->lp_primary_nid), rc);
2208 * Discovery of a peer is complete. Wake all waiters on the peer.
2209 * Call with lnet_net_lock/EX held.
2211 static void lnet_peer_discovery_complete(struct lnet_peer *lp, int dc_error)
2213 struct lnet_msg *msg, *tmp;
2215 LIST_HEAD(pending_msgs);
2217 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
2218 libcfs_nidstr(&lp->lp_primary_nid));
2220 list_del_init(&lp->lp_dc_list);
2221 spin_lock(&lp->lp_lock);
2223 lp->lp_dc_error = dc_error;
2224 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2225 lp->lp_state |= LNET_PEER_REDISCOVER;
2227 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
2228 spin_unlock(&lp->lp_lock);
2229 wake_up(&lp->lp_dc_waitq);
2231 if (lp->lp_rtr_refcount > 0)
2232 lnet_router_discovery_complete(lp);
2234 lnet_net_unlock(LNET_LOCK_EX);
2236 /* iterate through all pending messages and send them again */
2237 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
2238 list_del_init(&msg->msg_list);
2240 lnet_finalize(msg, dc_error);
2244 CDEBUG(D_NET, "sending pending message %s to target %s\n",
2245 lnet_msgtyp2str(msg->msg_type),
2246 libcfs_idstr(&msg->msg_target));
2247 rc = lnet_send(&msg->msg_src_nid_param, msg,
2248 &msg->msg_rtr_nid_param);
2250 CNETERR("Error sending %s to %s: %d\n",
2251 lnet_msgtyp2str(msg->msg_type),
2252 libcfs_idstr(&msg->msg_target), rc);
2253 lnet_finalize(msg, rc);
2256 lnet_net_lock(LNET_LOCK_EX);
2257 lnet_peer_decref_locked(lp);
2261 * Handle inbound push.
2262 * Like any event handler, called with lnet_res_lock/CPT held.
2264 void lnet_peer_push_event(struct lnet_event *ev)
2266 struct lnet_ping_buffer *pbuf;
2267 struct lnet_peer *lp;
2269 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
2271 /* lnet_find_peer() adds a refcount */
2272 lp = lnet_find_peer(&ev->source.nid);
2274 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
2275 libcfs_nidstr(&ev->initiator.nid),
2276 libcfs_nidstr(&ev->source.nid));
2277 pbuf->pb_needs_post = true;
2281 /* Ensure peer state remains consistent while we modify it. */
2282 spin_lock(&lp->lp_lock);
2285 * If some kind of error happened the contents of the message
2286 * cannot be used. Clear the NIDS_UPTODATE and set the
2287 * FORCE_PING flag to trigger a ping.
2290 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2291 lp->lp_state |= LNET_PEER_FORCE_PING;
2292 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2294 libcfs_nidstr(&lp->lp_primary_nid),
2295 libcfs_nidstr(&ev->source.nid));
2300 * A push with invalid or corrupted info. Clear the UPTODATE
2301 * flag to trigger a ping.
2303 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2304 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2305 lp->lp_state |= LNET_PEER_FORCE_PING;
2306 CDEBUG(D_NET, "Corrupted Push from %s\n",
2307 libcfs_nidstr(&lp->lp_primary_nid));
2312 * Make sure we'll allocate the correct size ping buffer when
2315 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2316 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2319 * A non-Multi-Rail peer is not supposed to be capable of
2322 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2323 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2324 libcfs_nidstr(&lp->lp_primary_nid));
2329 * The peer may have discovery disabled at its end. Set
2330 * NO_DISCOVERY as appropriate.
2332 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2333 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2334 libcfs_nidstr(&lp->lp_primary_nid));
2336 * Mark the peer for deletion if we already know about it
2337 * and it's going from discovery set to no discovery set
2339 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2340 LNET_PEER_DISCOVERING)) &&
2341 lp->lp_state & LNET_PEER_DISCOVERED) {
2342 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2343 libcfs_nidstr(&lp->lp_primary_nid),
2345 lp->lp_state |= LNET_PEER_MARK_DELETION;
2347 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2348 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2349 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2350 libcfs_nidstr(&lp->lp_primary_nid));
2351 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2355 * Update the MULTI_RAIL flag based on the push. If the peer
2356 * was configured with DLC then the setting should match what
2358 * NB: We verified above that the MR feature bit is set in pi_features
2360 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2361 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2362 libcfs_nidstr(&lp->lp_primary_nid), lp);
2363 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2364 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2365 libcfs_nidstr(&lp->lp_primary_nid));
2366 } else if (lnet_peer_discovery_disabled) {
2367 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2368 libcfs_nidstr(&lp->lp_primary_nid), lp);
2369 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2370 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2371 libcfs_nidstr(&lp->lp_primary_nid), lp);
2373 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2374 libcfs_nidstr(&lp->lp_primary_nid), lp);
2375 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2376 lnet_peer_clr_non_mr_pref_nids(lp);
2380 * Check for truncation of the Put message. Clear the
2381 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2382 * and tell discovery to allocate a bigger buffer.
2384 if (ev->mlength < ev->rlength) {
2385 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2386 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2387 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2388 lp->lp_state |= LNET_PEER_FORCE_PING;
2389 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2390 libcfs_nidstr(&lp->lp_primary_nid),
2391 pbuf->pb_info.pi_nnis);
2395 /* always assume new data */
2396 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2397 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2400 * If there is data present that hasn't been processed yet,
2401 * we'll replace it if the Put contained newer data and it
2402 * fits. We're racing with a Ping or earlier Push in this
2405 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2406 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2407 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2408 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2409 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2410 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2411 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2412 libcfs_nidstr(&lp->lp_primary_nid),
2413 LNET_PING_BUFFER_SEQNO(pbuf),
2414 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2420 * Allocate a buffer to copy the data. On a failure we drop
2421 * the Push and set FORCE_PING to force the discovery
2422 * thread to fix the problem by pinging the peer.
2424 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2426 lp->lp_state |= LNET_PEER_FORCE_PING;
2427 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2428 libcfs_nidstr(&lp->lp_primary_nid),
2429 LNET_PING_BUFFER_SEQNO(pbuf));
2434 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2435 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2436 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2437 CDEBUG(D_NET, "Received Push %s %u\n",
2438 libcfs_nidstr(&lp->lp_primary_nid),
2439 LNET_PING_BUFFER_SEQNO(pbuf));
2442 /* We've processed this buffer. It can be reposted */
2443 pbuf->pb_needs_post = true;
2446 * Queue the peer for discovery if not done, force it on the request
2447 * queue and wake the discovery thread if the peer was already queued,
2448 * because its status changed.
2450 spin_unlock(&lp->lp_lock);
2451 lnet_net_lock(LNET_LOCK_EX);
2452 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2453 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2454 wake_up(&the_lnet.ln_dc_waitq);
2456 /* Drop refcount from lookup */
2457 lnet_peer_decref_locked(lp);
2458 lnet_net_unlock(LNET_LOCK_EX);
2462 * Clear the discovery error state, unless we're already discovering
2463 * this peer, in which case the error is current.
2465 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2467 spin_lock(&lp->lp_lock);
2468 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2469 lp->lp_dc_error = 0;
2470 spin_unlock(&lp->lp_lock);
2474 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2475 * dropped/retaken within this function. An lnet_peer_ni is passed in
2476 * because discovery could tear down an lnet_peer.
2479 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2482 struct lnet_peer *lp;
2487 lnet_net_unlock(cpt);
2488 lnet_net_lock(LNET_LOCK_EX);
2489 lp = lpni->lpni_peer_net->lpn_peer;
2490 lnet_peer_clear_discovery_error(lp);
2493 * We're willing to be interrupted. The lpni can become a
2494 * zombie if we race with DLC, so we must check for that.
2497 /* Keep lp alive when the lnet_net_lock is unlocked */
2498 lnet_peer_addref_locked(lp);
2499 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2500 if (signal_pending(current))
2502 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2505 * Don't repeat discovery if discovery is disabled. This is
2506 * done to ensure we can use discovery as a standard ping as
2507 * well for backwards compatibility with routers which do not
2508 * have discovery or have discovery disabled
2510 if (lnet_is_discovery_disabled(lp) && count > 0)
2512 if (lp->lp_dc_error)
2514 if (lnet_peer_is_uptodate(lp))
2516 if (lp->lp_state & LNET_PEER_MARK_DELETED)
2518 lnet_peer_queue_for_discovery(lp);
2520 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2523 * If caller requested a non-blocking operation then
2524 * return immediately. Once discovery is complete any
2525 * pending messages that were stopped due to discovery
2526 * will be transmitted.
2531 lnet_net_unlock(LNET_LOCK_EX);
2533 finish_wait(&lp->lp_dc_waitq, &wait);
2534 lnet_net_lock(LNET_LOCK_EX);
2535 lnet_peer_decref_locked(lp);
2536 /* Peer may have changed */
2537 lp = lpni->lpni_peer_net->lpn_peer;
2539 finish_wait(&lp->lp_dc_waitq, &wait);
2541 lnet_net_unlock(LNET_LOCK_EX);
2543 lnet_peer_decref_locked(lp);
2545 * The peer may have changed, so re-check and rediscover if that turns
2546 * out to have been the case. The reference count on lp ensured that
2547 * even if it was unlinked from lpni the memory could not be recycled.
2548 * Thus the check below is sufficient to determine whether the peer
2549 * changed. If the peer changed, then lp must not be dereferenced.
2551 if (lp != lpni->lpni_peer_net->lpn_peer)
2554 if (signal_pending(current))
2556 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2558 else if (lp->lp_dc_error)
2559 rc = lp->lp_dc_error;
2561 CDEBUG(D_NET, "non-blocking discovery\n");
2562 else if (!lnet_peer_is_uptodate(lp) &&
2563 !(lnet_is_discovery_disabled(lp) ||
2564 (lp->lp_state & LNET_PEER_MARK_DELETED)))
2567 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2568 (lp ? libcfs_nidstr(&lp->lp_primary_nid) : "(none)"),
2569 libcfs_nidstr(&lpni->lpni_nid), rc,
2570 (!block) ? "pending discovery" : "discovery complete");
2575 /* Handle an incoming ack for a push. */
2577 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2579 struct lnet_ping_buffer *pbuf;
2581 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2582 spin_lock(&lp->lp_lock);
2583 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2584 lp->lp_push_error = ev->status;
2586 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2588 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2589 spin_unlock(&lp->lp_lock);
2591 CDEBUG(D_NET, "peer %s ev->status %d\n",
2592 libcfs_nidstr(&lp->lp_primary_nid), ev->status);
2595 /* Handle a Reply message. This is the reply to a Ping message. */
2597 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2599 struct lnet_ping_buffer *pbuf;
2602 spin_lock(&lp->lp_lock);
2604 lp->lp_disc_src_nid = ev->target.nid;
2605 lp->lp_disc_dst_nid = ev->source.nid;
2608 * If some kind of error happened the contents of message
2609 * cannot be used. Set PING_FAILED to trigger a retry.
2612 lp->lp_state |= LNET_PEER_PING_FAILED;
2613 lp->lp_ping_error = ev->status;
2614 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2616 libcfs_nidstr(&lp->lp_primary_nid),
2617 libcfs_nidstr(&ev->source.nid));
2621 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2622 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2623 lnet_swap_pinginfo(pbuf);
2626 * A reply with invalid or corrupted info. Set PING_FAILED to
2629 rc = lnet_ping_info_validate(&pbuf->pb_info);
2631 lp->lp_state |= LNET_PEER_PING_FAILED;
2632 lp->lp_ping_error = 0;
2633 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2634 libcfs_nidstr(&lp->lp_primary_nid), rc);
2639 * The peer may have discovery disabled at its end. Set
2640 * NO_DISCOVERY as appropriate.
2642 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) ||
2643 lnet_peer_discovery_disabled) {
2644 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2645 libcfs_nidstr(&lp->lp_primary_nid));
2647 /* Detect whether this peer has toggled discovery from on to
2648 * off and whether we can delete and re-create the peer. Peers
2649 * that were manually configured cannot be deleted by discovery.
2650 * We need to delete this peer and re-create it if the peer was
2651 * not configured manually, is currently considered DD capable,
2653 * 1. We've already discovered the peer (the peer has toggled
2654 * the discovery feature from on to off), or
2655 * 2. The peer is considered MR, but it was not user configured
2656 * (this was a "temporary" peer created via the kernel APIs
2657 * that we're discovering for the first time)
2659 if (!(lp->lp_state & (LNET_PEER_CONFIGURED |
2660 LNET_PEER_NO_DISCOVERY)) &&
2661 (lp->lp_state & (LNET_PEER_DISCOVERED |
2662 LNET_PEER_MULTI_RAIL))) {
2663 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2664 libcfs_nidstr(&lp->lp_primary_nid),
2666 lp->lp_state |= LNET_PEER_MARK_DELETION;
2668 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2670 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2671 libcfs_nidstr(&lp->lp_primary_nid));
2672 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2676 * Update the MULTI_RAIL flag based on the reply. If the peer
2677 * was configured with DLC then the setting should match what
2680 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2681 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2682 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2683 libcfs_nidstr(&lp->lp_primary_nid), lp);
2684 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2685 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2686 libcfs_nidstr(&lp->lp_primary_nid));
2687 } else if (lnet_peer_discovery_disabled) {
2689 "peer %s(%p) not MR: DD disabled locally\n",
2690 libcfs_nidstr(&lp->lp_primary_nid), lp);
2691 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2693 "peer %s(%p) not MR: DD disabled remotely\n",
2694 libcfs_nidstr(&lp->lp_primary_nid), lp);
2696 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2697 libcfs_nidstr(&lp->lp_primary_nid), lp);
2698 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2699 lnet_peer_clr_non_mr_pref_nids(lp);
2701 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2702 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2703 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2704 libcfs_nidstr(&lp->lp_primary_nid));
2706 CERROR("Multi-Rail state vanished from %s\n",
2707 libcfs_nidstr(&lp->lp_primary_nid));
2708 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2713 * Make sure we'll allocate the correct size ping buffer when
2716 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2717 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2720 * Check for truncation of the Reply. Clear PING_SENT and set
2721 * PING_FAILED to trigger a retry.
2723 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2724 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2725 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2726 lp->lp_state |= LNET_PEER_PING_FAILED;
2727 lp->lp_ping_error = 0;
2728 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2729 libcfs_nidstr(&lp->lp_primary_nid),
2730 pbuf->pb_info.pi_nnis);
2735 * Check the sequence numbers in the reply. These are only
2736 * available if the reply came from a Multi-Rail peer.
2738 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2739 pbuf->pb_info.pi_nnis > 1 &&
2740 lnet_nid_to_nid4(&lp->lp_primary_nid) ==
2741 pbuf->pb_info.pi_ni[1].ns_nid) {
2742 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2743 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2744 libcfs_nidstr(&lp->lp_primary_nid),
2745 LNET_PING_BUFFER_SEQNO(pbuf),
2748 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2751 /* We're happy with the state of the data in the buffer. */
2752 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2753 libcfs_nidstr(&lp->lp_primary_nid), lp->lp_peer_seqno,
2755 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2756 lnet_ping_buffer_decref(lp->lp_data);
2758 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2759 lnet_ping_buffer_addref(pbuf);
2762 lp->lp_state &= ~LNET_PEER_PING_SENT;
2763 spin_unlock(&lp->lp_lock);
2765 lnet_net_lock(LNET_LOCK_EX);
2767 * If this peer is a gateway, call the routing callback to
2768 * handle the ping reply
2770 if (lp->lp_rtr_refcount > 0)
2771 lnet_router_discovery_ping_reply(lp);
2772 lnet_net_unlock(LNET_LOCK_EX);
2776 * Send event handling. Only matters for error cases, where we clean
2777 * up state on the peer and peer_ni that would otherwise be updated in
2778 * the REPLY event handler for a successful Ping, and the ACK event
2779 * handler for a successful Push.
2782 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2789 spin_lock(&lp->lp_lock);
2790 if (ev->msg_type == LNET_MSG_GET) {
2791 lp->lp_state &= ~LNET_PEER_PING_SENT;
2792 lp->lp_state |= LNET_PEER_PING_FAILED;
2793 lp->lp_ping_error = ev->status;
2794 } else { /* ev->msg_type == LNET_MSG_PUT */
2795 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2796 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2797 lp->lp_push_error = ev->status;
2799 spin_unlock(&lp->lp_lock);
2800 rc = LNET_REDISCOVER_PEER;
2802 CDEBUG(D_NET, "%s Send to %s: %d\n",
2803 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2804 libcfs_nidstr(&ev->target.nid), rc);
2809 * Unlink event handling. This event is only seen if a call to
2810 * LNetMDUnlink() caused the event to be unlinked. If this call was
2811 * made after the event was set up in LNetGet() or LNetPut() then we
2812 * assume the Ping or Push timed out.
2815 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2817 spin_lock(&lp->lp_lock);
2818 /* We've passed through LNetGet() */
2819 if (lp->lp_state & LNET_PEER_PING_SENT) {
2820 lp->lp_state &= ~LNET_PEER_PING_SENT;
2821 lp->lp_state |= LNET_PEER_PING_FAILED;
2822 lp->lp_ping_error = -ETIMEDOUT;
2823 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2824 libcfs_nidstr(&lp->lp_primary_nid));
2826 /* We've passed through LNetPut() */
2827 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2828 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2829 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2830 lp->lp_push_error = -ETIMEDOUT;
2831 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2832 libcfs_nidstr(&lp->lp_primary_nid));
2834 spin_unlock(&lp->lp_lock);
2838 * Event handler for the discovery EQ.
2840 * Called with lnet_res_lock(cpt) held. The cpt is the
2841 * lnet_cpt_of_cookie() of the md handle cookie.
2843 static void lnet_discovery_event_handler(struct lnet_event *event)
2845 struct lnet_peer *lp = event->md_user_ptr;
2846 struct lnet_ping_buffer *pbuf;
2849 /* discovery needs to take another look */
2850 rc = LNET_REDISCOVER_PEER;
2852 CDEBUG(D_NET, "Received event: %d\n", event->type);
2854 switch (event->type) {
2855 case LNET_EVENT_ACK:
2856 lnet_discovery_event_ack(lp, event);
2858 case LNET_EVENT_REPLY:
2859 lnet_discovery_event_reply(lp, event);
2861 case LNET_EVENT_SEND:
2862 /* Only send failure triggers a retry. */
2863 rc = lnet_discovery_event_send(lp, event);
2865 case LNET_EVENT_UNLINK:
2866 /* LNetMDUnlink() was called */
2867 lnet_discovery_event_unlink(lp, event);
2870 /* Invalid events. */
2873 lnet_net_lock(LNET_LOCK_EX);
2874 if (event->unlinked) {
2875 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2876 lnet_ping_buffer_decref(pbuf);
2877 lnet_peer_decref_locked(lp);
2880 /* put peer back at end of request queue, if discovery not already
2882 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp) &&
2883 lnet_peer_queue_for_discovery(lp)) {
2884 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2885 wake_up(&the_lnet.ln_dc_waitq);
2887 lnet_net_unlock(LNET_LOCK_EX);
2891 * Build a peer from incoming data.
2893 * The NIDs in the incoming data are supposed to be structured as follows:
2896 * - other NIDs in same net
2897 * - NIDs in second net
2898 * - NIDs in third net
2900 * This due to the way the list of NIDs in the data is created.
2902 * Note that this function will mark the peer uptodate unless an
2903 * ENOMEM is encontered. All other errors are due to a conflict
2904 * between the DLC configuration and what discovery sees. We treat DLC
2905 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2906 * peer from becoming stuck in discovery.
2908 static int lnet_peer_merge_data(struct lnet_peer *lp,
2909 struct lnet_ping_buffer *pbuf)
2911 struct lnet_peer_net *lpn;
2912 struct lnet_peer_ni *lpni;
2913 lnet_nid_t *curnis = NULL;
2914 struct lnet_ni_status *addnis = NULL;
2915 lnet_nid_t *delnis = NULL;
2916 struct lnet_nid nid;
2926 flags = LNET_PEER_DISCOVERED;
2927 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2928 flags |= LNET_PEER_MULTI_RAIL;
2931 * Cache the routing feature for the peer; whether it is enabled
2932 * for disabled as reported by the remote peer.
2934 spin_lock(&lp->lp_lock);
2935 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2936 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2938 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2939 spin_unlock(&lp->lp_lock);
2941 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2942 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2943 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2944 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2945 if (!curnis || !addnis || !delnis) {
2953 /* Construct the list of NIDs present in peer. */
2955 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2956 curnis[ncurnis++] = lnet_nid_to_nid4(&lpni->lpni_nid);
2959 * Check for NIDs in pbuf not present in curnis[].
2960 * The loop starts at 1 to skip the loopback NID.
2962 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2963 for (j = 0; j < ncurnis; j++)
2964 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2967 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2970 * Check for NIDs in curnis[] not present in pbuf.
2971 * The nested loop starts at 1 to skip the loopback NID.
2973 * But never add the loopback NID to delnis[]: if it is
2974 * present in curnis[] then this peer is for this node.
2976 for (i = 0; i < ncurnis; i++) {
2977 if (curnis[i] == LNET_NID_LO_0)
2979 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2980 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2982 * update the information we cache for the
2983 * peer with the latest information we
2986 lnet_nid4_to_nid(curnis[i], &nid);
2987 lpni = lnet_peer_ni_find_locked(&nid);
2989 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2990 lnet_peer_ni_decref_locked(lpni);
2995 if (j == pbuf->pb_info.pi_nnis)
2996 delnis[ndelnis++] = curnis[i];
3000 * If we get here and the discovery is disabled then we don't want
3001 * to add or delete any NIs. We just updated the ones we have some
3002 * information on, and call it a day
3005 if (lnet_is_discovery_disabled(lp))
3008 for (i = 0; i < naddnis; i++) {
3009 lnet_nid4_to_nid(addnis[i].ns_nid, &nid);
3010 rc = lnet_peer_add_nid(lp, &nid, flags);
3012 CERROR("Error adding NID %s to peer %s: %d\n",
3013 libcfs_nid2str(addnis[i].ns_nid),
3014 libcfs_nidstr(&lp->lp_primary_nid), rc);
3018 lpni = lnet_peer_ni_find_locked(&nid);
3020 lpni->lpni_ns_status = addnis[i].ns_status;
3021 lnet_peer_ni_decref_locked(lpni);
3025 for (i = 0; i < ndelnis; i++) {
3027 * for routers it's okay to delete the primary_nid because
3028 * the upper layers don't really rely on it. So if we're
3029 * being told that the router changed its primary_nid
3030 * then it's okay to delete it.
3032 if (lp->lp_rtr_refcount > 0)
3033 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
3034 rc = lnet_peer_del_nid(lp, delnis[i], flags);
3036 CERROR("Error deleting NID %s from peer %s: %d\n",
3037 libcfs_nid2str(delnis[i]),
3038 libcfs_nidstr(&lp->lp_primary_nid), rc);
3044 /* The peer net for the primary NID should be the first entry in the
3045 * peer's lp_peer_nets list, and the peer NI for the primary NID should
3046 * be the first entry in its peer net's lpn_peer_nis list.
3048 lnet_nid4_to_nid(pbuf->pb_info.pi_ni[1].ns_nid, &nid);
3049 lpni = lnet_peer_ni_find_locked(&nid);
3051 CERROR("Internal error: Failed to lookup peer NI for primary NID: %s\n",
3052 libcfs_nid2str(pbuf->pb_info.pi_ni[1].ns_nid));
3056 lnet_peer_ni_decref_locked(lpni);
3058 lpn = lpni->lpni_peer_net;
3059 if (lpn->lpn_peer_nets.prev != &lp->lp_peer_nets)
3060 list_move(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
3062 if (lpni->lpni_peer_nis.prev != &lpni->lpni_peer_net->lpn_peer_nis)
3063 list_move(&lpni->lpni_peer_nis,
3064 &lpni->lpni_peer_net->lpn_peer_nis);
3067 * Errors other than -ENOMEM are due to peers having been
3068 * configured with DLC. Ignore these because DLC overrides
3073 CFS_FREE_PTR_ARRAY(curnis, nnis);
3074 CFS_FREE_PTR_ARRAY(addnis, nnis);
3075 CFS_FREE_PTR_ARRAY(delnis, nnis);
3076 lnet_ping_buffer_decref(pbuf);
3077 CDEBUG(D_NET, "peer %s (%p): %d\n",
3078 libcfs_nidstr(&lp->lp_primary_nid), lp, rc);
3081 spin_lock(&lp->lp_lock);
3082 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3083 lp->lp_state |= LNET_PEER_FORCE_PING;
3084 spin_unlock(&lp->lp_lock);
3090 * The data in pbuf says lp is its primary peer, but the data was
3091 * received by a different peer. Try to update lp with the data.
3094 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
3096 struct lnet_handle_md mdh;
3098 /* Queue lp for discovery, and force it on the request queue. */
3099 lnet_net_lock(LNET_LOCK_EX);
3100 if (lnet_peer_queue_for_discovery(lp))
3101 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
3102 lnet_net_unlock(LNET_LOCK_EX);
3104 LNetInvalidateMDHandle(&mdh);
3107 * Decide whether we can move the peer to the DATA_PRESENT state.
3109 * We replace stale data for a multi-rail peer, repair PING_FAILED
3110 * status, and preempt FORCE_PING.
3112 * If after that we have DATA_PRESENT, we merge it into this peer.
3114 spin_lock(&lp->lp_lock);
3115 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
3116 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
3117 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
3118 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3119 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3120 lnet_ping_buffer_decref(pbuf);
3125 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3126 lnet_ping_buffer_decref(lp->lp_data);
3128 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3130 if (lp->lp_state & LNET_PEER_PING_FAILED) {
3131 mdh = lp->lp_ping_mdh;
3132 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3133 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3134 lp->lp_ping_error = 0;
3136 if (lp->lp_state & LNET_PEER_FORCE_PING)
3137 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3138 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3139 spin_unlock(&lp->lp_lock);
3141 if (!LNetMDHandleIsInvalid(mdh))
3145 return lnet_peer_merge_data(lp, pbuf);
3147 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3151 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
3155 for (i = 0; i < pinfo->pi_nnis; i++) {
3156 if (pinfo->pi_ni[i].ns_nid == nid)
3163 /* Delete a peer that has been marked for deletion. NB: when this peer was added
3164 * to the discovery queue a reference was taken that will prevent the peer from
3165 * actually being freed by this function. After this function exits the
3166 * discovery thread should call lnet_peer_discovery_complete() which will
3167 * drop that reference as well as wake any waiters that may also be holding a
3170 static int lnet_peer_deletion(struct lnet_peer *lp)
3171 __must_hold(&lp->lp_lock)
3173 struct list_head rlist;
3174 struct lnet_route *route, *tmp;
3175 int sensitivity = lp->lp_health_sensitivity;
3178 INIT_LIST_HEAD(&rlist);
3180 lp->lp_state &= ~(LNET_PEER_DISCOVERING | LNET_PEER_FORCE_PING |
3181 LNET_PEER_FORCE_PUSH);
3182 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3183 libcfs_nidstr(&lp->lp_primary_nid), lp, lp->lp_state);
3185 /* no-op if lnet_peer_del() has already been called on this peer */
3186 if (lp->lp_state & LNET_PEER_MARK_DELETED)
3189 spin_unlock(&lp->lp_lock);
3191 mutex_lock(&the_lnet.ln_api_mutex);
3192 if (the_lnet.ln_state != LNET_STATE_RUNNING ||
3193 the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
3194 mutex_unlock(&the_lnet.ln_api_mutex);
3195 spin_lock(&lp->lp_lock);
3199 lnet_net_lock(LNET_LOCK_EX);
3200 /* remove the peer from the discovery work
3201 * queue if it's on there in preparation
3204 if (!list_empty(&lp->lp_dc_list))
3205 list_del_init(&lp->lp_dc_list);
3206 list_for_each_entry_safe(route, tmp,
3209 lnet_move_route(route, NULL, &rlist);
3210 lnet_net_unlock(LNET_LOCK_EX);
3212 /* lnet_peer_del() deletes all the peer NIs owned by this peer */
3213 rc = lnet_peer_del(lp);
3215 CNETERR("Internal error: Unable to delete peer %s rc %d\n",
3216 libcfs_nidstr(&lp->lp_primary_nid), rc);
3218 list_for_each_entry_safe(route, tmp,
3220 /* re-add these routes */
3221 lnet_add_route(route->lr_net,
3226 LIBCFS_FREE(route, sizeof(*route));
3229 mutex_unlock(&the_lnet.ln_api_mutex);
3231 spin_lock(&lp->lp_lock);
3237 * Update a peer using the data received.
3239 static int lnet_peer_data_present(struct lnet_peer *lp)
3240 __must_hold(&lp->lp_lock)
3242 struct lnet_ping_buffer *pbuf;
3243 struct lnet_peer_ni *lpni;
3244 struct lnet_nid nid;
3250 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3251 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3252 spin_unlock(&lp->lp_lock);
3255 * Modifications of peer structures are done while holding the
3256 * ln_api_mutex. A global lock is required because we may be
3257 * modifying multiple peer structures, and a mutex greatly
3258 * simplifies memory management.
3260 * The actual changes to the data structures must also protect
3261 * against concurrent lookups, for which the lnet_net_lock in
3262 * LNET_LOCK_EX mode is used.
3264 mutex_lock(&the_lnet.ln_api_mutex);
3265 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3271 * If this peer is not on the peer list then it is being torn
3272 * down, and our reference count may be all that is keeping it
3273 * alive. Don't do any work on it.
3275 if (list_empty(&lp->lp_peer_list)) {
3276 lnet_ping_buffer_decref(pbuf);
3280 flags = LNET_PEER_DISCOVERED;
3281 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
3282 flags |= LNET_PEER_MULTI_RAIL;
3285 * Check whether the primary NID in the message matches the
3286 * primary NID of the peer. If it does, update the peer, if
3287 * it it does not, check whether there is already a peer with
3288 * that primary NID. If no such peer exists, try to update
3289 * the primary NID of the current peer (allowed if it was
3290 * created due to message traffic) and complete the update.
3291 * If the peer did exist, hand off the data to it.
3293 * The peer for the loopback interface is a special case: this
3294 * is the peer for the local node, and we want to set its
3295 * primary NID to the correct value here. Moreover, this peer
3296 * can show up with only the loopback NID in the ping buffer.
3298 if (pbuf->pb_info.pi_nnis <= 1) {
3299 lnet_ping_buffer_decref(pbuf);
3302 lnet_nid4_to_nid(pbuf->pb_info.pi_ni[1].ns_nid, &nid);
3303 if (nid_is_lo0(&lp->lp_primary_nid)) {
3304 rc = lnet_peer_set_primary_nid(lp, &nid, flags);
3306 lnet_ping_buffer_decref(pbuf);
3308 rc = lnet_peer_merge_data(lp, pbuf);
3310 * if the primary nid of the peer is present in the ping info returned
3311 * from the peer, but it's not the local primary peer we have
3312 * cached and discovery is disabled, then we don't want to update
3313 * our local peer info, by adding or removing NIDs, we just want
3314 * to update the status of the nids that we currently have
3315 * recorded in that peer.
3317 } else if (nid_same(&lp->lp_primary_nid, &nid) ||
3318 (lnet_is_nid_in_ping_info(lnet_nid_to_nid4(&lp->lp_primary_nid),
3320 lnet_is_discovery_disabled(lp))) {
3321 rc = lnet_peer_merge_data(lp, pbuf);
3323 lpni = lnet_peer_ni_find_locked(&nid);
3324 if (!lpni || lp == lpni->lpni_peer_net->lpn_peer) {
3325 rc = lnet_peer_set_primary_nid(lp, &nid, flags);
3327 CERROR("Primary NID error %s versus %s: %d\n",
3328 libcfs_nidstr(&lp->lp_primary_nid),
3329 libcfs_nidstr(&nid), rc);
3330 lnet_ping_buffer_decref(pbuf);
3332 rc = lnet_peer_merge_data(lp, pbuf);
3335 lnet_peer_ni_decref_locked(lpni);
3337 struct lnet_peer *new_lp;
3338 new_lp = lpni->lpni_peer_net->lpn_peer;
3340 * if lp has discovery/MR enabled that means new_lp
3341 * should have discovery/MR enabled as well, since
3342 * it's the same peer, which we're about to merge
3344 spin_lock(&lp->lp_lock);
3345 spin_lock(&new_lp->lp_lock);
3346 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
3347 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
3348 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
3349 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
3350 /* If we're processing a ping reply then we may be
3351 * about to send a push to the peer that we ping'd.
3352 * Since the ping reply that we're processing was
3353 * received by lp, we need to set the discovery source
3354 * NID for new_lp to the NID stored in lp.
3356 if (!LNET_NID_IS_ANY(&lp->lp_disc_src_nid)) {
3357 new_lp->lp_disc_src_nid = lp->lp_disc_src_nid;
3358 new_lp->lp_disc_dst_nid = lp->lp_disc_dst_nid;
3360 spin_unlock(&new_lp->lp_lock);
3361 spin_unlock(&lp->lp_lock);
3363 rc = lnet_peer_set_primary_data(new_lp, pbuf);
3364 lnet_consolidate_routes_locked(lp, new_lp);
3365 lnet_peer_ni_decref_locked(lpni);
3369 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n",
3370 libcfs_nidstr(&lp->lp_primary_nid), lp, rc,
3372 mutex_unlock(&the_lnet.ln_api_mutex);
3374 spin_lock(&lp->lp_lock);
3375 /* Tell discovery to re-check the peer immediately. */
3377 rc = LNET_REDISCOVER_PEER;
3382 * A ping failed. Clear the PING_FAILED state and set the
3383 * FORCE_PING state, to ensure a retry even if discovery is
3384 * disabled. This avoids being left with incorrect state.
3386 static int lnet_peer_ping_failed(struct lnet_peer *lp)
3387 __must_hold(&lp->lp_lock)
3389 struct lnet_handle_md mdh;
3392 mdh = lp->lp_ping_mdh;
3393 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3394 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3395 lp->lp_state |= LNET_PEER_FORCE_PING;
3396 rc = lp->lp_ping_error;
3397 lp->lp_ping_error = 0;
3398 spin_unlock(&lp->lp_lock);
3400 if (!LNetMDHandleIsInvalid(mdh))
3403 CDEBUG(D_NET, "peer %s:%d\n",
3404 libcfs_nidstr(&lp->lp_primary_nid), rc);
3406 spin_lock(&lp->lp_lock);
3407 return rc ? rc : LNET_REDISCOVER_PEER;
3410 /* Active side of ping. */
3411 static int lnet_peer_send_ping(struct lnet_peer *lp)
3412 __must_hold(&lp->lp_lock)
3418 lp->lp_state |= LNET_PEER_PING_SENT;
3419 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3420 spin_unlock(&lp->lp_lock);
3422 cpt = lnet_net_lock_current();
3423 /* Refcount for MD. */
3424 lnet_peer_addref_locked(lp);
3425 lnet_net_unlock(cpt);
3427 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3429 rc = lnet_send_ping(&lp->lp_primary_nid, &lp->lp_ping_mdh, nnis, lp,
3430 the_lnet.ln_dc_handler, false);
3433 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3434 * refcount on the peer, otherwise LNetMDUnlink will be called
3435 * which will eventually do that.
3439 lnet_peer_decref_locked(lp);
3440 lnet_net_unlock(cpt);
3441 rc = -rc; /* change the rc to negative value */
3443 } else if (rc < 0) {
3447 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3449 spin_lock(&lp->lp_lock);
3453 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nidstr(&lp->lp_primary_nid), rc);
3455 * The errors that get us here are considered hard errors and
3456 * cause Discovery to terminate. So we clear PING_SENT, but do
3457 * not set either PING_FAILED or FORCE_PING. In fact we need
3458 * to clear PING_FAILED, because the unlink event handler will
3459 * have set it if we called LNetMDUnlink() above.
3461 spin_lock(&lp->lp_lock);
3462 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3467 * This function exists because you cannot call LNetMDUnlink() from an
3470 static int lnet_peer_push_failed(struct lnet_peer *lp)
3471 __must_hold(&lp->lp_lock)
3473 struct lnet_handle_md mdh;
3476 mdh = lp->lp_push_mdh;
3477 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3478 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3479 rc = lp->lp_push_error;
3480 lp->lp_push_error = 0;
3481 spin_unlock(&lp->lp_lock);
3483 if (!LNetMDHandleIsInvalid(mdh))
3486 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3487 spin_lock(&lp->lp_lock);
3488 return rc ? rc : LNET_REDISCOVER_PEER;
3492 * Mark the peer as discovered.
3494 static int lnet_peer_discovered(struct lnet_peer *lp)
3495 __must_hold(&lp->lp_lock)
3497 lp->lp_state |= LNET_PEER_DISCOVERED;
3498 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3499 LNET_PEER_REDISCOVER);
3501 lp->lp_dc_error = 0;
3503 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3508 /* Active side of push. */
3509 static int lnet_peer_send_push(struct lnet_peer *lp)
3510 __must_hold(&lp->lp_lock)
3512 struct lnet_ping_buffer *pbuf;
3513 struct lnet_processid id;
3518 /* Don't push to a non-multi-rail peer. */
3519 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3520 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3521 /* if peer's NIDs are uptodate then peer is discovered */
3522 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3523 rc = lnet_peer_discovered(lp);
3530 lp->lp_state |= LNET_PEER_PUSH_SENT;
3531 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3532 spin_unlock(&lp->lp_lock);
3534 cpt = lnet_net_lock_current();
3535 pbuf = the_lnet.ln_ping_target;
3536 lnet_ping_buffer_addref(pbuf);
3537 lnet_net_unlock(cpt);
3539 /* Push source MD */
3540 md.start = &pbuf->pb_info;
3541 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3542 md.threshold = 2; /* Put/Ack */
3544 md.options = LNET_MD_TRACK_RESPONSE;
3545 md.handler = the_lnet.ln_dc_handler;
3548 rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3550 lnet_ping_buffer_decref(pbuf);
3551 CERROR("Can't bind push source MD: %d\n", rc);
3555 cpt = lnet_net_lock_current();
3556 /* Refcount for MD. */
3557 lnet_peer_addref_locked(lp);
3558 id.pid = LNET_PID_LUSTRE;
3559 if (!LNET_NID_IS_ANY(&lp->lp_disc_dst_nid))
3560 id.nid = lp->lp_disc_dst_nid;
3562 id.nid = lp->lp_primary_nid;
3563 lnet_net_unlock(cpt);
3565 rc = LNetPut(&lp->lp_disc_src_nid, lp->lp_push_mdh,
3566 LNET_ACK_REQ, &id, LNET_RESERVED_PORTAL,
3567 LNET_PROTO_PING_MATCHBITS, 0, 0);
3570 * reset the discovery nid. There is no need to restrict sending
3571 * from that source, if we call lnet_push_update_to_peers(). It'll
3572 * get set to a specific NID, if we initiate discovery from the
3575 lp->lp_disc_src_nid = LNET_ANY_NID;
3576 lp->lp_disc_dst_nid = LNET_ANY_NID;
3581 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3583 spin_lock(&lp->lp_lock);
3587 LNetMDUnlink(lp->lp_push_mdh);
3588 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3590 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nidstr(&lp->lp_primary_nid),
3593 * The errors that get us here are considered hard errors and
3594 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3595 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3596 * because the unlink event handler will have set it if we
3597 * called LNetMDUnlink() above.
3599 spin_lock(&lp->lp_lock);
3600 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3605 * Wait for work to be queued or some other change that must be
3606 * attended to. Returns non-zero if the discovery thread should shut
3609 static int lnet_peer_discovery_wait_for_work(void)
3616 cpt = lnet_net_lock_current();
3618 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3619 TASK_INTERRUPTIBLE);
3620 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3622 if (lnet_push_target_resize_needed() ||
3623 the_lnet.ln_push_target->pb_needs_post)
3625 if (!list_empty(&the_lnet.ln_dc_request))
3627 if (!list_empty(&the_lnet.ln_msg_resend))
3629 lnet_net_unlock(cpt);
3632 * wakeup max every second to check if there are peers that
3633 * have been stuck on the working queue for greater than
3636 schedule_timeout(cfs_time_seconds(1));
3637 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3638 cpt = lnet_net_lock_current();
3640 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3642 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3645 lnet_net_unlock(cpt);
3647 CDEBUG(D_NET, "woken: %d\n", rc);
3653 * Messages that were pending on a destroyed peer will be put on a global
3654 * resend list. The message resend list will be checked by
3655 * the discovery thread when it wakes up, and will resend messages. These
3656 * messages can still be sendable in the case the lpni which was the initial
3657 * cause of the message re-queue was transfered to another peer.
3659 * It is possible that LNet could be shutdown while we're iterating
3660 * through the list. lnet_shudown_lndnets() will attempt to access the
3661 * resend list, but will have to wait until the spinlock is released, by
3662 * which time there shouldn't be any more messages on the resend list.
3663 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3664 * for the messages so they can be released. The other case is that
3665 * lnet_shudown_lndnets() can finalize all the messages before this
3666 * function can visit the resend list, in which case this function will be
3669 static void lnet_resend_msgs(void)
3671 struct lnet_msg *msg, *tmp;
3675 spin_lock(&the_lnet.ln_msg_resend_lock);
3676 list_splice(&the_lnet.ln_msg_resend, &resend);
3677 spin_unlock(&the_lnet.ln_msg_resend_lock);
3679 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3680 list_del_init(&msg->msg_list);
3681 rc = lnet_send(&msg->msg_src_nid_param, msg,
3682 &msg->msg_rtr_nid_param);
3684 CNETERR("Error sending %s to %s: %d\n",
3685 lnet_msgtyp2str(msg->msg_type),
3686 libcfs_idstr(&msg->msg_target), rc);
3687 lnet_finalize(msg, rc);
3692 /* The discovery thread. */
3693 static int lnet_peer_discovery(void *arg)
3695 struct lnet_peer *lp;
3698 wait_for_completion(&the_lnet.ln_started);
3700 CDEBUG(D_NET, "started\n");
3703 if (lnet_peer_discovery_wait_for_work())
3706 if (lnet_push_target_resize_needed())
3707 lnet_push_target_resize();
3708 else if (the_lnet.ln_push_target->pb_needs_post)
3709 lnet_push_target_post(the_lnet.ln_push_target,
3710 &the_lnet.ln_push_target_md);
3714 lnet_net_lock(LNET_LOCK_EX);
3715 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3716 lnet_net_unlock(LNET_LOCK_EX);
3721 * Process all incoming discovery work requests. When
3722 * discovery must wait on a peer to change state, it
3723 * is added to the tail of the ln_dc_working queue. A
3724 * timestamp keeps track of when the peer was added,
3725 * so we can time out discovery requests that take too
3728 while (!list_empty(&the_lnet.ln_dc_request)) {
3729 lp = list_first_entry(&the_lnet.ln_dc_request,
3730 struct lnet_peer, lp_dc_list);
3731 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3733 * set the time the peer was put on the dc_working
3734 * queue. It shouldn't remain on the queue
3735 * forever, in case the GET message (for ping)
3736 * doesn't get a REPLY or the PUT message (for
3737 * push) doesn't get an ACK.
3739 lp->lp_last_queued = ktime_get_real_seconds();
3740 lnet_net_unlock(LNET_LOCK_EX);
3742 if (lnet_push_target_resize_needed())
3743 lnet_push_target_resize();
3744 else if (the_lnet.ln_push_target->pb_needs_post)
3745 lnet_push_target_post(the_lnet.ln_push_target,
3746 &the_lnet.ln_push_target_md);
3749 * Select an action depending on the state of
3750 * the peer and whether discovery is disabled.
3751 * The check whether discovery is disabled is
3752 * done after the code that handles processing
3753 * for arrived data, cleanup for failures, and
3754 * forcing a Ping or Push.
3756 spin_lock(&lp->lp_lock);
3757 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3758 libcfs_nidstr(&lp->lp_primary_nid), lp,
3760 if (lp->lp_state & (LNET_PEER_MARK_DELETION |
3761 LNET_PEER_MARK_DELETED))
3762 rc = lnet_peer_deletion(lp);
3763 else if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3764 rc = lnet_peer_data_present(lp);
3765 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3766 rc = lnet_peer_ping_failed(lp);
3767 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3768 rc = lnet_peer_push_failed(lp);
3769 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3770 rc = lnet_peer_send_ping(lp);
3771 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3772 rc = lnet_peer_send_push(lp);
3773 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3774 rc = lnet_peer_send_ping(lp);
3775 else if (lnet_peer_needs_push(lp))
3776 rc = lnet_peer_send_push(lp);
3778 rc = lnet_peer_discovered(lp);
3779 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3780 libcfs_nidstr(&lp->lp_primary_nid), lp,
3783 if (rc == LNET_REDISCOVER_PEER) {
3784 spin_unlock(&lp->lp_lock);
3785 lnet_net_lock(LNET_LOCK_EX);
3786 list_move(&lp->lp_dc_list,
3787 &the_lnet.ln_dc_request);
3789 !(lp->lp_state & LNET_PEER_DISCOVERING)) {
3790 spin_unlock(&lp->lp_lock);
3791 lnet_net_lock(LNET_LOCK_EX);
3792 lnet_peer_discovery_complete(lp, rc);
3794 spin_unlock(&lp->lp_lock);
3795 lnet_net_lock(LNET_LOCK_EX);
3798 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3803 lnet_net_unlock(LNET_LOCK_EX);
3806 CDEBUG(D_NET, "stopping\n");
3808 * Clean up before telling lnet_peer_discovery_stop() that
3809 * we're done. Use wake_up() below to somewhat reduce the
3810 * size of the thundering herd if there are multiple threads
3811 * waiting on discovery of a single peer.
3814 /* Queue cleanup 1: stop all pending pings and pushes. */
3815 lnet_net_lock(LNET_LOCK_EX);
3816 while (!list_empty(&the_lnet.ln_dc_working)) {
3817 lp = list_first_entry(&the_lnet.ln_dc_working,
3818 struct lnet_peer, lp_dc_list);
3819 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3820 lnet_net_unlock(LNET_LOCK_EX);
3821 lnet_peer_cancel_discovery(lp);
3822 lnet_net_lock(LNET_LOCK_EX);
3824 lnet_net_unlock(LNET_LOCK_EX);
3826 /* Queue cleanup 2: wait for the expired queue to clear. */
3827 while (!list_empty(&the_lnet.ln_dc_expired))
3828 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3830 /* Queue cleanup 3: clear the request queue. */
3831 lnet_net_lock(LNET_LOCK_EX);
3832 while (!list_empty(&the_lnet.ln_dc_request)) {
3833 lp = list_first_entry(&the_lnet.ln_dc_request,
3834 struct lnet_peer, lp_dc_list);
3835 lnet_peer_discovery_complete(lp, -ESHUTDOWN);
3837 lnet_net_unlock(LNET_LOCK_EX);
3839 lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3840 the_lnet.ln_dc_handler = NULL;
3842 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3843 wake_up(&the_lnet.ln_dc_waitq);
3845 CDEBUG(D_NET, "stopped\n");
3850 /* ln_api_mutex is held on entry. */
3851 int lnet_peer_discovery_start(void)
3853 struct task_struct *task;
3856 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3859 the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3860 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3861 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3864 CERROR("Can't start peer discovery thread: %d\n", rc);
3866 the_lnet.ln_dc_handler = NULL;
3868 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3871 CDEBUG(D_NET, "discovery start: %d\n", rc);
3876 /* ln_api_mutex is held on entry. */
3877 void lnet_peer_discovery_stop(void)
3879 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3882 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3883 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3885 /* In the LNetNIInit() path we may be stopping discovery before it
3886 * entered its work loop
3888 if (!completion_done(&the_lnet.ln_started))
3889 complete(&the_lnet.ln_started);
3891 wake_up(&the_lnet.ln_dc_waitq);
3893 mutex_unlock(&the_lnet.ln_api_mutex);
3894 wait_event(the_lnet.ln_dc_waitq,
3895 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3896 mutex_lock(&the_lnet.ln_api_mutex);
3898 LASSERT(list_empty(&the_lnet.ln_dc_request));
3899 LASSERT(list_empty(&the_lnet.ln_dc_working));
3900 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3902 CDEBUG(D_NET, "discovery stopped\n");
3908 lnet_debug_peer(lnet_nid_t nid4)
3910 char *aliveness = "NA";
3911 struct lnet_peer_ni *lp;
3913 struct lnet_nid nid;
3915 lnet_nid4_to_nid(nid4, &nid);
3916 cpt = lnet_nid2cpt(&nid, NULL);
3919 lp = lnet_peerni_by_nid_locked(&nid, NULL, cpt);
3921 lnet_net_unlock(cpt);
3922 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nidstr(&nid));
3926 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3927 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3929 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3930 libcfs_nidstr(&lp->lpni_nid), kref_read(&lp->lpni_kref),
3931 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3932 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3933 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3935 lnet_peer_ni_decref_locked(lp);
3937 lnet_net_unlock(cpt);
3940 /* Gathering information for userspace. */
3942 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3943 char aliveness[LNET_MAX_STR_LEN],
3944 __u32 *cpt_iter, __u32 *refcount,
3945 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3946 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3947 __u32 *peer_tx_qnob)
3949 struct lnet_peer_table *peer_table;
3950 struct lnet_peer_ni *lp;
3955 /* get the number of CPTs */
3956 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3958 /* if the cpt number to be examined is >= the number of cpts in
3959 * the system then indicate that there are no more cpts to examin
3961 if (*cpt_iter >= lncpt)
3964 /* get the current table */
3965 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3966 /* if the ptable is NULL then there are no more cpts to examine */
3967 if (peer_table == NULL)
3970 lnet_net_lock(*cpt_iter);
3972 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3973 struct list_head *peers = &peer_table->pt_hash[j];
3975 list_for_each_entry(lp, peers, lpni_hashlist) {
3976 if (!nid_is_nid4(&lp->lpni_nid))
3978 if (peer_index-- > 0)
3981 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3982 if (lnet_isrouter(lp) ||
3983 lnet_peer_aliveness_enabled(lp))
3984 snprintf(aliveness, LNET_MAX_STR_LEN,
3985 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3987 *nid = lnet_nid_to_nid4(&lp->lpni_nid);
3988 *refcount = kref_read(&lp->lpni_kref);
3989 *ni_peer_tx_credits =
3990 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3991 *peer_tx_credits = lp->lpni_txcredits;
3992 *peer_rtr_credits = lp->lpni_rtrcredits;
3993 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3994 *peer_tx_qnob = lp->lpni_txqnob;
4000 lnet_net_unlock(*cpt_iter);
4004 return found ? 0 : -ENOENT;
4007 /* ln_api_mutex is held, which keeps the peer list stable */
4008 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
4010 struct lnet_ioctl_element_stats *lpni_stats;
4011 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
4012 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
4013 struct lnet_peer_ni_credit_info *lpni_info;
4014 struct lnet_peer_ni *lpni;
4015 struct lnet_peer *lp;
4017 struct lnet_nid nid;
4021 lnet_nid4_to_nid(cfg->prcfg_prim_nid, &nid);
4022 lp = lnet_find_peer(&nid);
4029 size = sizeof(nid4) + sizeof(*lpni_info) + sizeof(*lpni_stats)
4030 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
4031 size *= lp->lp_nnis;
4032 if (size > cfg->prcfg_size) {
4033 cfg->prcfg_size = size;
4038 cfg->prcfg_prim_nid = lnet_nid_to_nid4(&lp->lp_primary_nid);
4039 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
4040 cfg->prcfg_cfg_nid = lnet_nid_to_nid4(&lp->lp_primary_nid);
4041 cfg->prcfg_count = lp->lp_nnis;
4042 cfg->prcfg_size = size;
4043 cfg->prcfg_state = lp->lp_state;
4045 /* Allocate helper buffers. */
4047 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
4050 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
4053 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
4054 if (!lpni_msg_stats)
4055 goto out_free_stats;
4056 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
4058 goto out_free_msg_stats;
4063 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
4064 if (!nid_is_nid4(&lpni->lpni_nid))
4066 nid4 = lnet_nid_to_nid4(&lpni->lpni_nid);
4067 if (copy_to_user(bulk, &nid4, sizeof(nid4)))
4068 goto out_free_hstats;
4069 bulk += sizeof(nid4);
4071 memset(lpni_info, 0, sizeof(*lpni_info));
4072 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
4073 if (lnet_isrouter(lpni) ||
4074 lnet_peer_aliveness_enabled(lpni))
4075 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
4076 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
4078 lpni_info->cr_refcount = kref_read(&lpni->lpni_kref);
4079 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
4080 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
4081 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
4082 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
4083 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
4084 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
4085 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
4086 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
4087 goto out_free_hstats;
4088 bulk += sizeof(*lpni_info);
4090 memset(lpni_stats, 0, sizeof(*lpni_stats));
4091 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
4092 LNET_STATS_TYPE_SEND);
4093 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
4094 LNET_STATS_TYPE_RECV);
4095 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
4096 LNET_STATS_TYPE_DROP);
4097 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
4098 goto out_free_hstats;
4099 bulk += sizeof(*lpni_stats);
4100 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
4101 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
4102 goto out_free_hstats;
4103 bulk += sizeof(*lpni_msg_stats);
4104 lpni_hstats->hlpni_network_timeout =
4105 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
4106 lpni_hstats->hlpni_remote_dropped =
4107 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
4108 lpni_hstats->hlpni_remote_timeout =
4109 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
4110 lpni_hstats->hlpni_remote_error =
4111 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
4112 lpni_hstats->hlpni_health_value =
4113 atomic_read(&lpni->lpni_healthv);
4114 lpni_hstats->hlpni_ping_count = lpni->lpni_ping_count;
4115 lpni_hstats->hlpni_next_ping = lpni->lpni_next_ping;
4116 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
4117 goto out_free_hstats;
4118 bulk += sizeof(*lpni_hstats);
4123 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
4125 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
4127 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
4129 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
4131 lnet_peer_decref_locked(lp);
4136 /* must hold net_lock/0 */
4138 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni,
4139 struct list_head *recovery_queue,
4142 /* the mt could've shutdown and cleaned up the queues */
4143 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
4146 if (!list_empty(&lpni->lpni_recovery))
4149 if (atomic_read(&lpni->lpni_healthv) == LNET_MAX_HEALTH_VALUE)
4152 if (!lpni->lpni_last_alive) {
4154 "lpni %s(%p) not eligible for recovery last alive %lld\n",
4155 libcfs_nidstr(&lpni->lpni_nid), lpni,
4156 lpni->lpni_last_alive);
4160 if (lnet_recovery_limit &&
4161 now > lpni->lpni_last_alive + lnet_recovery_limit) {
4162 CDEBUG(D_NET, "lpni %s aged out last alive %lld\n",
4163 libcfs_nidstr(&lpni->lpni_nid),
4164 lpni->lpni_last_alive);
4165 /* Reset the ping count so that if this peer NI is added back to
4166 * the recovery queue we will send the first ping right away.
4168 lpni->lpni_ping_count = 0;
4172 /* This peer NI is going on the recovery queue, so take a ref on it */
4173 lnet_peer_ni_addref_locked(lpni);
4175 lnet_peer_ni_set_next_ping(lpni, now);
4177 CDEBUG(D_NET, "%s added to recovery queue. ping count: %u next ping: %lld last alive: %lld health: %d\n",
4178 libcfs_nidstr(&lpni->lpni_nid),
4179 lpni->lpni_ping_count,
4180 lpni->lpni_next_ping,
4181 lpni->lpni_last_alive,
4182 atomic_read(&lpni->lpni_healthv));
4184 list_add_tail(&lpni->lpni_recovery, recovery_queue);
4187 /* Call with the ln_api_mutex held */
4189 lnet_peer_ni_set_healthv(lnet_nid_t nid4, int value, bool all)
4191 struct lnet_peer_table *ptable;
4192 struct lnet_peer *lp;
4193 struct lnet_peer_net *lpn;
4194 struct lnet_peer_ni *lpni;
4195 struct lnet_nid nid;
4200 if (the_lnet.ln_state != LNET_STATE_RUNNING)
4203 lnet_nid4_to_nid(nid4, &nid);
4204 now = ktime_get_seconds();
4207 lnet_net_lock(LNET_LOCK_EX);
4208 lpni = lnet_peer_ni_find_locked(&nid);
4210 lnet_net_unlock(LNET_LOCK_EX);
4213 lnet_set_lpni_healthv_locked(lpni, value);
4214 lnet_peer_ni_add_to_recoveryq_locked(lpni,
4215 &the_lnet.ln_mt_peerNIRecovq, now);
4216 lnet_peer_ni_decref_locked(lpni);
4217 lnet_net_unlock(LNET_LOCK_EX);
4221 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
4224 * Walk all the peers and reset the health value for each one to the
4227 lnet_net_lock(LNET_LOCK_EX);
4228 for (cpt = 0; cpt < lncpt; cpt++) {
4229 ptable = the_lnet.ln_peer_tables[cpt];
4230 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
4231 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
4232 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
4234 lnet_set_lpni_healthv_locked(lpni,
4236 lnet_peer_ni_add_to_recoveryq_locked(lpni,
4237 &the_lnet.ln_mt_peerNIRecovq, now);
4242 lnet_net_unlock(LNET_LOCK_EX);