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, struct lnet_nid *nid,
535 struct lnet_peer_ni *lpni;
536 struct lnet_nid primary_nid = lp->lp_primary_nid;
538 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
540 if (!(flags & LNET_PEER_CONFIGURED)) {
541 if (lp->lp_state & LNET_PEER_CONFIGURED) {
547 lpni = lnet_peer_ni_find_locked(nid);
552 lnet_peer_ni_decref_locked(lpni);
553 if (lp != lpni->lpni_peer_net->lpn_peer) {
559 * This function only allows deletion of the primary NID if it
562 if (nid_same(nid, &lp->lp_primary_nid) && lp->lp_nnis != 1 && !force) {
567 lnet_net_lock(LNET_LOCK_EX);
569 if (nid_same(nid, &lp->lp_primary_nid) && lp->lp_nnis != 1 && force) {
570 struct lnet_peer_ni *lpni2;
571 /* assign the next peer_ni to be the primary */
572 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
574 lp->lp_primary_nid = lpni2->lpni_nid;
576 rc = lnet_peer_ni_del_locked(lpni, force);
578 lnet_net_unlock(LNET_LOCK_EX);
581 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
582 libcfs_nidstr(&primary_nid), libcfs_nidstr(nid),
589 lnet_peer_table_cleanup_locked(struct lnet_net *net,
590 struct lnet_peer_table *ptable)
593 struct lnet_peer_ni *next;
594 struct lnet_peer_ni *lpni;
595 struct lnet_peer *peer;
597 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
598 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
600 if (net != NULL && net != lpni->lpni_net)
603 peer = lpni->lpni_peer_net->lpn_peer;
604 if (!nid_same(&peer->lp_primary_nid,
606 lnet_peer_ni_del_locked(lpni, false);
610 * Removing the primary NID implies removing
611 * the entire peer. Advance next beyond any
612 * peer_ni that belongs to the same peer.
614 list_for_each_entry_from(next, &ptable->pt_hash[i],
616 if (next->lpni_peer_net->lpn_peer != peer)
619 lnet_peer_del_locked(peer);
625 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
627 wait_var_event_warning(&ptable->pt_zombies,
628 ptable->pt_zombies == 0,
629 "Waiting for %d zombies on peer table\n",
634 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
635 struct lnet_peer_table *ptable)
637 struct lnet_peer_ni *lp;
638 struct lnet_peer_ni *tmp;
639 struct lnet_nid gw_nid;
642 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
643 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
645 if (net != lp->lpni_net)
648 if (!lnet_isrouter(lp))
651 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
653 lnet_net_unlock(LNET_LOCK_EX);
654 lnet_del_route(LNET_NET_ANY, &gw_nid);
655 lnet_net_lock(LNET_LOCK_EX);
661 lnet_peer_tables_cleanup(struct lnet_net *net)
664 struct lnet_peer_table *ptable;
666 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
667 /* If just deleting the peers for a NI, get rid of any routes these
668 * peers are gateways for. */
669 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
670 lnet_net_lock(LNET_LOCK_EX);
671 lnet_peer_table_del_rtrs_locked(net, ptable);
672 lnet_net_unlock(LNET_LOCK_EX);
675 /* Start the cleanup process */
676 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
677 lnet_net_lock(LNET_LOCK_EX);
678 lnet_peer_table_cleanup_locked(net, ptable);
679 lnet_net_unlock(LNET_LOCK_EX);
682 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
683 lnet_peer_ni_finalize_wait(ptable);
686 static struct lnet_peer_ni *
687 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, struct lnet_nid *nid)
689 struct list_head *peers;
690 struct lnet_peer_ni *lp;
692 if (the_lnet.ln_state != LNET_STATE_RUNNING)
695 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
696 list_for_each_entry(lp, peers, lpni_hashlist) {
697 if (nid_same(&lp->lpni_nid, nid)) {
698 lnet_peer_ni_addref_locked(lp);
706 struct lnet_peer_ni *
707 lnet_peer_ni_find_locked(struct lnet_nid *nid)
709 struct lnet_peer_ni *lpni;
710 struct lnet_peer_table *ptable;
713 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
715 ptable = the_lnet.ln_peer_tables[cpt];
716 lpni = lnet_get_peer_ni_locked(ptable, nid);
721 struct lnet_peer_ni *
722 lnet_peer_ni_get_locked(struct lnet_peer *lp, struct lnet_nid *nid)
724 struct lnet_peer_net *lpn;
725 struct lnet_peer_ni *lpni;
727 lpn = lnet_peer_get_net_locked(lp, LNET_NID_NET(nid));
731 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
732 if (nid_same(&lpni->lpni_nid, nid))
740 lnet_find_peer(struct lnet_nid *nid)
742 struct lnet_peer_ni *lpni;
743 struct lnet_peer *lp = NULL;
746 cpt = lnet_net_lock_current();
747 lpni = lnet_peer_ni_find_locked(nid);
749 lp = lpni->lpni_peer_net->lpn_peer;
750 lnet_peer_addref_locked(lp);
751 lnet_peer_ni_decref_locked(lpni);
753 lnet_net_unlock(cpt);
758 struct lnet_peer_net *
759 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
761 struct lnet_peer_net *net;
764 /* no net id provided return the first net */
765 net = list_first_entry_or_null(&lp->lp_peer_nets,
766 struct lnet_peer_net,
772 /* find the net after the one provided */
773 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
774 if (net->lpn_net_id == prev_lpn_id) {
776 * if we reached the end of the list loop to the
779 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
780 return list_first_entry_or_null(&lp->lp_peer_nets,
781 struct lnet_peer_net,
784 return list_next_entry(net, lpn_peer_nets);
791 struct lnet_peer_ni *
792 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
793 struct lnet_peer_net *peer_net,
794 struct lnet_peer_ni *prev)
796 struct lnet_peer_ni *lpni;
797 struct lnet_peer_net *net = peer_net;
801 if (list_empty(&peer->lp_peer_nets))
804 net = list_first_entry(&peer->lp_peer_nets,
805 struct lnet_peer_net,
808 lpni = list_first_entry(&net->lpn_peer_nis, struct lnet_peer_ni,
814 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
816 * if you reached the end of the peer ni list and the peer
817 * net is specified then there are no more peer nis in that
824 * we reached the end of this net ni list. move to the
827 if (prev->lpni_peer_net->lpn_peer_nets.next ==
829 /* no more nets and no more NIs. */
832 /* get the next net */
833 net = list_first_entry(&prev->lpni_peer_net->lpn_peer_nets,
834 struct lnet_peer_net,
836 /* get the ni on it */
837 lpni = list_first_entry(&net->lpn_peer_nis, struct lnet_peer_ni,
843 /* there are more nis left */
844 lpni = list_first_entry(&prev->lpni_peer_nis,
845 struct lnet_peer_ni, lpni_peer_nis);
850 /* Call with the ln_api_mutex held */
851 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
853 struct lnet_process_id id;
854 struct lnet_peer_table *ptable;
855 struct lnet_peer *lp;
864 if (the_lnet.ln_state != LNET_STATE_RUNNING)
867 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
870 * Count the number of peers, and return E2BIG if the buffer
871 * is too small. We'll also return the desired size.
874 for (cpt = 0; cpt < lncpt; cpt++) {
875 ptable = the_lnet.ln_peer_tables[cpt];
876 count += ptable->pt_peers;
878 size = count * sizeof(*ids);
883 * Walk the peer lists and copy out the primary nids.
884 * This is safe because the peer lists are only modified
885 * while the ln_api_mutex is held. So we don't need to
886 * hold the lnet_net_lock as well, and can therefore
887 * directly call copy_to_user().
890 memset(&id, 0, sizeof(id));
891 id.pid = LNET_PID_LUSTRE;
893 for (cpt = 0; cpt < lncpt; cpt++) {
894 ptable = the_lnet.ln_peer_tables[cpt];
895 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
896 if (!nid_is_nid4(&lp->lp_primary_nid))
900 id.nid = lnet_nid_to_nid4(&lp->lp_primary_nid);
901 if (copy_to_user(&ids[i], &id, sizeof(id)))
914 * Start pushes to peers that need to be updated for a configuration
915 * change on this node.
918 lnet_push_update_to_peers(int force)
920 struct lnet_peer_table *ptable;
921 struct lnet_peer *lp;
925 lnet_net_lock(LNET_LOCK_EX);
926 if (lnet_peer_discovery_disabled)
928 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
929 for (cpt = 0; cpt < lncpt; cpt++) {
930 ptable = the_lnet.ln_peer_tables[cpt];
931 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
933 spin_lock(&lp->lp_lock);
934 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
935 lp->lp_state |= LNET_PEER_FORCE_PUSH;
936 spin_unlock(&lp->lp_lock);
938 if (lnet_peer_needs_push(lp))
939 lnet_peer_queue_for_discovery(lp);
942 lnet_net_unlock(LNET_LOCK_EX);
943 wake_up(&the_lnet.ln_dc_waitq);
946 /* find the NID in the preferred gateways for the remote peer
948 * false: list is not empty and NID is not preferred
949 * false: list is empty
950 * true: nid is found in the list
953 lnet_peer_is_pref_rtr_locked(struct lnet_peer_ni *lpni,
954 struct lnet_nid *gw_nid)
956 struct lnet_nid_list *ne;
958 CDEBUG(D_NET, "%s: rtr pref emtpy: %d\n",
959 libcfs_nidstr(&lpni->lpni_nid),
960 list_empty(&lpni->lpni_rtr_pref_nids));
962 if (list_empty(&lpni->lpni_rtr_pref_nids))
965 /* iterate through all the preferred NIDs and see if any of them
966 * matches the provided gw_nid
968 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
969 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
970 libcfs_nidstr(&ne->nl_nid),
971 libcfs_nidstr(gw_nid));
972 if (nid_same(&ne->nl_nid, gw_nid))
980 lnet_peer_clr_pref_rtrs(struct lnet_peer_ni *lpni)
982 struct list_head zombies;
983 struct lnet_nid_list *ne;
984 struct lnet_nid_list *tmp;
985 int cpt = lpni->lpni_cpt;
987 INIT_LIST_HEAD(&zombies);
990 list_splice_init(&lpni->lpni_rtr_pref_nids, &zombies);
991 lnet_net_unlock(cpt);
993 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
994 list_del(&ne->nl_list);
995 LIBCFS_FREE(ne, sizeof(*ne));
1000 lnet_peer_add_pref_rtr(struct lnet_peer_ni *lpni,
1001 struct lnet_nid *gw_nid)
1003 int cpt = lpni->lpni_cpt;
1004 struct lnet_nid_list *ne = NULL;
1006 /* This function is called with api_mutex held. When the api_mutex
1007 * is held the list can not be modified, as it is only modified as
1008 * a result of applying a UDSP and that happens under api_mutex
1011 __must_hold(&the_lnet.ln_api_mutex);
1013 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
1014 if (nid_same(&ne->nl_nid, gw_nid))
1018 LIBCFS_CPT_ALLOC(ne, lnet_cpt_table(), cpt, sizeof(*ne));
1022 ne->nl_nid = *gw_nid;
1024 /* Lock the cpt to protect against addition and checks in the
1025 * selection algorithm
1028 list_add(&ne->nl_list, &lpni->lpni_rtr_pref_nids);
1029 lnet_net_unlock(cpt);
1035 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
1036 * this is a preferred point-to-point path. Call with lnet_net_lock in
1040 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1042 struct lnet_nid_list *ne;
1044 if (lpni->lpni_pref_nnids == 0)
1046 if (lpni->lpni_pref_nnids == 1)
1047 return nid_same(&lpni->lpni_pref.nid, nid);
1048 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1049 if (nid_same(&ne->nl_nid, nid))
1056 * Set a single ni as preferred, provided no preferred ni is already
1057 * defined. Only to be used for non-multi-rail peer_ni.
1060 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni,
1061 struct lnet_nid *nid)
1067 spin_lock(&lpni->lpni_lock);
1068 if (LNET_NID_IS_ANY(nid)) {
1070 } else if (lpni->lpni_pref_nnids > 0) {
1072 } else if (lpni->lpni_pref_nnids == 0) {
1073 lpni->lpni_pref.nid = *nid;
1074 lpni->lpni_pref_nnids = 1;
1075 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
1077 spin_unlock(&lpni->lpni_lock);
1079 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1080 libcfs_nidstr(&lpni->lpni_nid), libcfs_nidstr(nid), rc);
1085 * Clear the preferred NID from a non-multi-rail peer_ni, provided
1086 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
1089 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
1093 spin_lock(&lpni->lpni_lock);
1094 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
1095 lpni->lpni_pref_nnids = 0;
1096 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1097 } else if (lpni->lpni_pref_nnids == 0) {
1102 spin_unlock(&lpni->lpni_lock);
1104 CDEBUG(D_NET, "peer %s: %d\n",
1105 libcfs_nidstr(&lpni->lpni_nid), rc);
1110 lnet_peer_ni_set_selection_priority(struct lnet_peer_ni *lpni, __u32 priority)
1112 lpni->lpni_sel_priority = priority;
1116 * Clear the preferred NIDs from a non-multi-rail peer.
1119 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
1121 struct lnet_peer_ni *lpni = NULL;
1123 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
1124 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
1128 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1130 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1131 struct lnet_nid_list *ne1 = NULL;
1132 struct lnet_nid_list *ne2 = NULL;
1133 struct lnet_nid *tmp_nid = NULL;
1136 if (LNET_NID_IS_ANY(nid)) {
1141 if (lpni->lpni_pref_nnids == 1 &&
1142 nid_same(&lpni->lpni_pref.nid, nid)) {
1147 /* A non-MR node may have only one preferred NI per peer_ni */
1148 if (lpni->lpni_pref_nnids > 0 &&
1149 !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1154 /* add the new preferred nid to the list of preferred nids */
1155 if (lpni->lpni_pref_nnids != 0) {
1156 size_t alloc_size = sizeof(*ne1);
1158 if (lpni->lpni_pref_nnids == 1) {
1159 tmp_nid = &lpni->lpni_pref.nid;
1160 INIT_LIST_HEAD(&lpni->lpni_pref.nids);
1163 list_for_each_entry(ne1, &lpni->lpni_pref.nids, nl_list) {
1164 if (nid_same(&ne1->nl_nid, nid)) {
1170 LIBCFS_CPT_ALLOC(ne1, lnet_cpt_table(), lpni->lpni_cpt,
1177 /* move the originally stored nid to the list */
1178 if (lpni->lpni_pref_nnids == 1) {
1179 LIBCFS_CPT_ALLOC(ne2, lnet_cpt_table(),
1180 lpni->lpni_cpt, alloc_size);
1185 INIT_LIST_HEAD(&ne2->nl_list);
1186 ne2->nl_nid = *tmp_nid;
1191 lnet_net_lock(LNET_LOCK_EX);
1192 spin_lock(&lpni->lpni_lock);
1193 if (lpni->lpni_pref_nnids == 0) {
1194 lpni->lpni_pref.nid = *nid;
1197 list_add_tail(&ne2->nl_list, &lpni->lpni_pref.nids);
1198 list_add_tail(&ne1->nl_list, &lpni->lpni_pref.nids);
1200 lpni->lpni_pref_nnids++;
1201 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1202 spin_unlock(&lpni->lpni_lock);
1203 lnet_net_unlock(LNET_LOCK_EX);
1206 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1207 spin_lock(&lpni->lpni_lock);
1208 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1209 spin_unlock(&lpni->lpni_lock);
1211 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1212 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nidstr(nid), rc);
1217 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1219 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1220 struct lnet_nid_list *ne = NULL;
1223 if (lpni->lpni_pref_nnids == 0) {
1228 if (lpni->lpni_pref_nnids == 1) {
1229 if (!nid_same(&lpni->lpni_pref.nid, nid)) {
1234 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1235 if (nid_same(&ne->nl_nid, nid))
1236 goto remove_nid_entry;
1244 lnet_net_lock(LNET_LOCK_EX);
1245 spin_lock(&lpni->lpni_lock);
1246 if (lpni->lpni_pref_nnids == 1)
1247 lpni->lpni_pref.nid = LNET_ANY_NID;
1249 list_del_init(&ne->nl_list);
1250 if (lpni->lpni_pref_nnids == 2) {
1251 struct lnet_nid_list *ne, *tmp;
1253 list_for_each_entry_safe(ne, tmp,
1254 &lpni->lpni_pref.nids,
1256 lpni->lpni_pref.nid = ne->nl_nid;
1257 list_del_init(&ne->nl_list);
1258 LIBCFS_FREE(ne, sizeof(*ne));
1262 lpni->lpni_pref_nnids--;
1263 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1264 spin_unlock(&lpni->lpni_lock);
1265 lnet_net_unlock(LNET_LOCK_EX);
1268 LIBCFS_FREE(ne, sizeof(*ne));
1270 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1271 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nidstr(nid), rc);
1276 lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni)
1278 struct list_head zombies;
1279 struct lnet_nid_list *ne;
1280 struct lnet_nid_list *tmp;
1282 INIT_LIST_HEAD(&zombies);
1284 lnet_net_lock(LNET_LOCK_EX);
1285 if (lpni->lpni_pref_nnids == 1)
1286 lpni->lpni_pref.nid = LNET_ANY_NID;
1287 else if (lpni->lpni_pref_nnids > 1)
1288 list_splice_init(&lpni->lpni_pref.nids, &zombies);
1289 lpni->lpni_pref_nnids = 0;
1290 lnet_net_unlock(LNET_LOCK_EX);
1292 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1293 list_del_init(&ne->nl_list);
1294 LIBCFS_FREE(ne, sizeof(*ne));
1299 lnet_peer_primary_nid_locked(struct lnet_nid *nid, struct lnet_nid *result)
1301 struct lnet_peer_ni *lpni;
1304 lpni = lnet_peer_ni_find_locked(nid);
1306 *result = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1307 lnet_peer_ni_decref_locked(lpni);
1312 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1313 __must_hold(&lp->lp_lock)
1315 if (lnet_peer_discovery_disabled)
1318 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1319 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1330 lnet_is_discovery_disabled(struct lnet_peer *lp)
1334 spin_lock(&lp->lp_lock);
1335 rc = lnet_is_discovery_disabled_locked(lp);
1336 spin_unlock(&lp->lp_lock);
1342 LNetAddPeer(lnet_nid_t *nids, __u32 num_nids)
1344 struct lnet_nid pnid = LNET_ANY_NID;
1348 if (!nids || num_nids < 1)
1351 rc = LNetNIInit(LNET_PID_ANY);
1355 mutex_lock(&the_lnet.ln_api_mutex);
1357 mr = lnet_peer_discovery_disabled == 0;
1360 for (i = 0; i < num_nids; i++) {
1361 struct lnet_nid nid;
1363 if (nids[i] == LNET_NID_LO_0)
1366 lnet_nid4_to_nid(nids[i], &nid);
1367 if (LNET_NID_IS_ANY(&pnid)) {
1368 lnet_nid4_to_nid(nids[i], &pnid);
1369 rc = lnet_add_peer_ni(&pnid, &LNET_ANY_NID, mr, true);
1370 } else if (lnet_peer_discovery_disabled) {
1371 lnet_nid4_to_nid(nids[i], &nid);
1372 rc = lnet_add_peer_ni(&nid, &LNET_ANY_NID, mr, true);
1374 lnet_nid4_to_nid(nids[i], &nid);
1375 rc = lnet_add_peer_ni(&pnid, &nid, mr, true);
1378 if (rc && rc != -EEXIST)
1383 mutex_unlock(&the_lnet.ln_api_mutex);
1387 return rc == -EEXIST ? 0 : rc;
1389 EXPORT_SYMBOL(LNetAddPeer);
1391 void LNetPrimaryNID(struct lnet_nid *nid)
1393 struct lnet_peer *lp;
1394 struct lnet_peer_ni *lpni;
1395 struct lnet_nid orig;
1399 if (!nid || nid_is_lo0(nid))
1403 cpt = lnet_net_lock_current();
1404 lpni = lnet_peerni_by_nid_locked(nid, NULL, cpt);
1409 lp = lpni->lpni_peer_net->lpn_peer;
1411 /* If discovery is disabled locally then we needn't bother running
1412 * discovery here because discovery will not modify whatever
1413 * primary NID is currently set for this peer. If the specified peer is
1414 * down then this discovery can introduce long delays into the mount
1415 * process, so skip it if it isn't necessary.
1417 while (!lnet_peer_discovery_disabled && !lnet_peer_is_uptodate(lp)) {
1418 spin_lock(&lp->lp_lock);
1419 /* force a full discovery cycle */
1420 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1421 spin_unlock(&lp->lp_lock);
1423 rc = lnet_discover_peer_locked(lpni, cpt, true);
1426 /* The lpni (or lp) for this NID may have changed and our ref is
1427 * the only thing keeping the old one around. Release the ref
1428 * and lookup the lpni again
1430 lnet_peer_ni_decref_locked(lpni);
1431 lpni = lnet_peer_ni_find_locked(nid);
1436 lp = lpni->lpni_peer_net->lpn_peer;
1438 /* If we find that the peer has discovery disabled then we will
1439 * not modify whatever primary NID is currently set for this
1440 * peer. Thus, we can break out of this loop even if the peer
1441 * is not fully up to date.
1443 if (lnet_is_discovery_disabled(lp))
1446 *nid = lp->lp_primary_nid;
1448 lnet_peer_ni_decref_locked(lpni);
1450 lnet_net_unlock(cpt);
1452 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nidstr(&orig),
1453 libcfs_nidstr(nid), rc);
1455 EXPORT_SYMBOL(LNetPrimaryNID);
1457 struct lnet_peer_net *
1458 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1460 struct lnet_peer_net *peer_net;
1461 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1462 if (peer_net->lpn_net_id == net_id)
1469 * Attach a peer_ni to a peer_net and peer. This function assumes
1470 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1471 * may be attached to a different peer, in which case it will be
1472 * properly detached first. The whole operation is done atomically.
1474 * This function consumes the reference on lpni and Always returns 0.
1475 * This is the last function called from functions that do return an
1476 * int, so returning 0 here allows the compiler to do a tail call.
1479 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1480 struct lnet_peer_net *lpn,
1481 struct lnet_peer_ni *lpni,
1484 struct lnet_peer_table *ptable;
1485 bool new_lpn = false;
1488 /* Install the new peer_ni */
1489 lnet_net_lock(LNET_LOCK_EX);
1490 /* Add peer_ni to global peer table hash, if necessary. */
1491 if (list_empty(&lpni->lpni_hashlist)) {
1492 int hash = lnet_nid2peerhash(&lpni->lpni_nid);
1494 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1495 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1496 ptable->pt_version++;
1497 lnet_peer_ni_addref_locked(lpni);
1500 /* Detach the peer_ni from an existing peer, if necessary. */
1501 if (lpni->lpni_peer_net) {
1502 LASSERT(lpni->lpni_peer_net != lpn);
1503 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1504 lnet_peer_detach_peer_ni_locked(lpni);
1505 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1506 lpni->lpni_peer_net = NULL;
1509 /* Add peer_ni to peer_net */
1510 lpni->lpni_peer_net = lpn;
1511 if (nid_same(&lp->lp_primary_nid, &lpni->lpni_nid))
1512 list_add(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1514 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1515 lnet_update_peer_net_healthv(lpni);
1516 lnet_peer_net_addref_locked(lpn);
1518 /* Add peer_net to peer */
1519 if (!lpn->lpn_peer) {
1522 if (nid_same(&lp->lp_primary_nid, &lpni->lpni_nid))
1523 list_add(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1525 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1526 lnet_peer_addref_locked(lp);
1529 /* Add peer to global peer list, if necessary */
1530 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1531 if (list_empty(&lp->lp_peer_list)) {
1532 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1537 /* Update peer state */
1538 spin_lock(&lp->lp_lock);
1539 if (flags & LNET_PEER_CONFIGURED) {
1540 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1541 lp->lp_state |= LNET_PEER_CONFIGURED;
1543 if (flags & LNET_PEER_MULTI_RAIL) {
1544 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1545 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1546 lnet_peer_clr_non_mr_pref_nids(lp);
1549 spin_unlock(&lp->lp_lock);
1555 rc = lnet_udsp_apply_policies_on_lpn(lpn);
1557 CERROR("Failed to apply UDSPs on lpn %s\n",
1558 libcfs_net2str(lpn->lpn_net_id));
1560 rc = lnet_udsp_apply_policies_on_lpni(lpni);
1562 CERROR("Failed to apply UDSPs on lpni %s\n",
1563 libcfs_nidstr(&lpni->lpni_nid));
1565 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1566 libcfs_nidstr(&lp->lp_primary_nid),
1567 libcfs_nidstr(&lpni->lpni_nid), flags);
1568 lnet_peer_ni_decref_locked(lpni);
1569 lnet_net_unlock(LNET_LOCK_EX);
1575 * Create a new peer, with nid as its primary nid.
1577 * Call with the lnet_api_mutex held.
1580 lnet_peer_add(struct lnet_nid *nid, unsigned int flags)
1582 struct lnet_peer *lp;
1583 struct lnet_peer_net *lpn;
1584 struct lnet_peer_ni *lpni;
1590 * No need for the lnet_net_lock here, because the
1591 * lnet_api_mutex is held.
1593 lpni = lnet_peer_ni_find_locked(nid);
1595 /* A peer with this NID already exists. */
1596 lp = lpni->lpni_peer_net->lpn_peer;
1597 lnet_peer_ni_decref_locked(lpni);
1599 * This is an error if the peer was configured and the
1600 * primary NID differs or an attempt is made to change
1601 * the Multi-Rail flag. Otherwise the assumption is
1602 * that an existing peer is being modified.
1604 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1605 if (!nid_same(&lp->lp_primary_nid, nid))
1607 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1610 } else if (!(flags & LNET_PEER_CONFIGURED)) {
1611 if (nid_same(&lp->lp_primary_nid, nid)) {
1616 /* Delete and recreate as a configured peer. */
1617 rc = lnet_peer_del(lp);
1622 /* Create peer, peer_net, and peer_ni. */
1624 lp = lnet_peer_alloc(nid);
1627 lpn = lnet_peer_net_alloc(LNET_NID_NET(nid));
1630 lpni = lnet_peer_ni_alloc(nid);
1634 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1637 LIBCFS_FREE(lpn, sizeof(*lpn));
1639 LIBCFS_FREE(lp, sizeof(*lp));
1641 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1642 libcfs_nidstr(nid), flags, rc);
1647 * Add a NID to a peer. Call with ln_api_mutex held.
1650 * -EPERM: Non-DLC addition to a DLC-configured peer.
1651 * -EEXIST: The NID was configured by DLC for a different peer.
1652 * -ENOMEM: Out of memory.
1653 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1654 * non-multi-rail peer.
1657 lnet_peer_add_nid(struct lnet_peer *lp, struct lnet_nid *nid,
1660 struct lnet_peer_net *lpn;
1661 struct lnet_peer_ni *lpni;
1667 /* A configured peer can only be updated through configuration. */
1668 if (!(flags & LNET_PEER_CONFIGURED)) {
1669 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1676 * The MULTI_RAIL flag can be set but not cleared, because
1677 * that would leave the peer struct in an invalid state.
1679 if (flags & LNET_PEER_MULTI_RAIL) {
1680 spin_lock(&lp->lp_lock);
1681 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1682 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1683 lnet_peer_clr_non_mr_pref_nids(lp);
1685 spin_unlock(&lp->lp_lock);
1686 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1691 lpni = lnet_peer_ni_find_locked(nid);
1694 * A peer_ni already exists. This is only a problem if
1695 * it is not connected to this peer and was configured
1698 if (lpni->lpni_peer_net->lpn_peer == lp)
1700 if (lnet_peer_ni_is_configured(lpni)) {
1704 /* If this is the primary NID, destroy the peer. */
1705 if (lnet_peer_ni_is_primary(lpni)) {
1706 struct lnet_peer *rtr_lp =
1707 lpni->lpni_peer_net->lpn_peer;
1708 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1710 * if we're trying to delete a router it means
1711 * we're moving this peer NI to a new peer so must
1712 * transfer router properties to the new peer
1714 if (rtr_refcount > 0) {
1715 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1716 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1718 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1719 lnet_peer_ni_decref_locked(lpni);
1720 lpni = lnet_peer_ni_alloc(nid);
1727 lpni = lnet_peer_ni_alloc(nid);
1735 * Get the peer_net. Check that we're not adding a second
1736 * peer_ni on a peer_net of a non-multi-rail peer.
1738 lpn = lnet_peer_get_net_locked(lp, LNET_NID_NET(nid));
1740 lpn = lnet_peer_net_alloc(LNET_NID_NET(nid));
1745 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1750 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1753 lnet_peer_ni_decref_locked(lpni);
1755 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1756 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nidstr(nid),
1762 * Update the primary NID of a peer, if possible.
1764 * Call with the lnet_api_mutex held.
1767 lnet_peer_set_primary_nid(struct lnet_peer *lp, struct lnet_nid *nid,
1770 struct lnet_nid old = lp->lp_primary_nid;
1773 if (nid_same(&lp->lp_primary_nid, nid))
1776 lp->lp_primary_nid = *nid;
1778 rc = lnet_peer_add_nid(lp, nid, flags);
1780 lp->lp_primary_nid = old;
1784 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1785 libcfs_nidstr(&old), libcfs_nidstr(nid), rc);
1791 * lpni creation initiated due to traffic either sending or receiving.
1792 * Callers must hold ln_api_mutex
1793 * Ref taken on lnet_peer_ni returned by this function
1795 static struct lnet_peer_ni *
1796 lnet_peer_ni_traffic_add(struct lnet_nid *nid, struct lnet_nid *pref)
1797 __must_hold(&the_lnet.ln_api_mutex)
1799 struct lnet_peer *lp = NULL;
1800 struct lnet_peer_net *lpn = NULL;
1801 struct lnet_peer_ni *lpni;
1805 if (LNET_NID_IS_ANY(nid)) {
1810 /* lnet_net_lock is not needed here because ln_api_lock is held */
1811 lpni = lnet_peer_ni_find_locked(nid);
1814 * We must have raced with another thread. Since we
1815 * know next to nothing about a peer_ni created by
1816 * traffic, we just assume everything is ok and
1822 /* Create peer, peer_net, and peer_ni. */
1824 lp = lnet_peer_alloc(nid);
1827 lpn = lnet_peer_net_alloc(LNET_NID_NET(nid));
1830 lpni = lnet_peer_ni_alloc(nid);
1833 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1835 /* lnet_peer_attach_peer_ni() always returns 0 */
1836 rc = lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1838 lnet_peer_ni_addref_locked(lpni);
1843 LIBCFS_FREE(lpn, sizeof(*lpn));
1845 LIBCFS_FREE(lp, sizeof(*lp));
1849 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nidstr(nid), rc);
1854 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1856 * This API handles the following combinations:
1857 * Create a peer with its primary NI if only the prim_nid is provided
1858 * Add a NID to a peer identified by the prim_nid. The peer identified
1859 * by the prim_nid must already exist.
1860 * The peer being created may be non-MR.
1862 * The caller must hold ln_api_mutex. This prevents the peer from
1863 * being created/modified/deleted by a different thread.
1866 lnet_add_peer_ni(struct lnet_nid *prim_nid, struct lnet_nid *nid, bool mr,
1868 __must_hold(&the_lnet.ln_api_mutex)
1870 struct lnet_peer *lp = NULL;
1871 struct lnet_peer_ni *lpni;
1872 unsigned int flags = 0;
1874 /* The prim_nid must always be specified */
1875 if (LNET_NID_IS_ANY(prim_nid))
1879 flags = LNET_PEER_CONFIGURED;
1882 flags |= LNET_PEER_MULTI_RAIL;
1885 * If nid isn't specified, we must create a new peer with
1886 * prim_nid as its primary nid.
1888 if (LNET_NID_IS_ANY(nid))
1889 return lnet_peer_add(prim_nid, flags);
1891 /* Look up the prim_nid, which must exist. */
1892 lpni = lnet_peer_ni_find_locked(prim_nid);
1895 lnet_peer_ni_decref_locked(lpni);
1896 lp = lpni->lpni_peer_net->lpn_peer;
1898 /* Peer must have been configured. */
1899 if (!temp && !(lp->lp_state & LNET_PEER_CONFIGURED)) {
1900 CDEBUG(D_NET, "peer %s was not configured\n",
1901 libcfs_nidstr(prim_nid));
1905 /* Primary NID must match */
1906 if (!nid_same(&lp->lp_primary_nid, prim_nid)) {
1907 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1908 libcfs_nidstr(prim_nid),
1909 libcfs_nidstr(&lp->lp_primary_nid));
1913 /* Multi-Rail flag must match. */
1914 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1915 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1916 libcfs_nidstr(prim_nid));
1920 if (temp && lnet_peer_is_uptodate(lp)) {
1922 "Don't add temporary peer NI for uptodate peer %s\n",
1923 libcfs_nidstr(&lp->lp_primary_nid));
1927 return lnet_peer_add_nid(lp, nid, flags);
1931 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1933 * This API handles the following combinations:
1934 * Delete a NI from a peer if both prim_nid and nid are provided.
1935 * Delete a peer if only prim_nid is provided.
1936 * Delete a peer if its primary nid is provided.
1938 * The caller must hold ln_api_mutex. This prevents the peer from
1939 * being modified/deleted by a different thread.
1942 lnet_del_peer_ni(struct lnet_nid *prim_nid, struct lnet_nid *nid)
1944 struct lnet_peer *lp;
1945 struct lnet_peer_ni *lpni;
1948 if (!prim_nid || LNET_NID_IS_ANY(prim_nid))
1951 lpni = lnet_peer_ni_find_locked(prim_nid);
1954 lnet_peer_ni_decref_locked(lpni);
1955 lp = lpni->lpni_peer_net->lpn_peer;
1957 if (!nid_same(prim_nid, &lp->lp_primary_nid)) {
1958 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1959 libcfs_nidstr(prim_nid),
1960 libcfs_nidstr(&lp->lp_primary_nid));
1964 lnet_net_lock(LNET_LOCK_EX);
1965 if (lp->lp_rtr_refcount > 0) {
1966 lnet_net_unlock(LNET_LOCK_EX);
1967 CERROR("%s is a router. Can not be deleted\n",
1968 libcfs_nidstr(prim_nid));
1971 lnet_net_unlock(LNET_LOCK_EX);
1973 if (LNET_NID_IS_ANY(nid) || nid_same(nid, &lp->lp_primary_nid))
1974 return lnet_peer_del(lp);
1976 flags = LNET_PEER_CONFIGURED;
1977 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1978 flags |= LNET_PEER_MULTI_RAIL;
1980 return lnet_peer_del_nid(lp, nid, flags);
1984 lnet_destroy_peer_ni_locked(struct kref *ref)
1986 struct lnet_peer_ni *lpni = container_of(ref, struct lnet_peer_ni,
1988 struct lnet_peer_table *ptable;
1989 struct lnet_peer_net *lpn;
1991 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nidstr(&lpni->lpni_nid));
1993 LASSERT(kref_read(&lpni->lpni_kref) == 0);
1994 LASSERT(list_empty(&lpni->lpni_txq));
1995 LASSERT(lpni->lpni_txqnob == 0);
1996 LASSERT(list_empty(&lpni->lpni_peer_nis));
1997 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1999 lpn = lpni->lpni_peer_net;
2000 lpni->lpni_peer_net = NULL;
2001 lpni->lpni_net = NULL;
2003 if (!list_empty(&lpni->lpni_hashlist)) {
2004 /* remove the peer ni from the zombie list */
2005 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
2006 spin_lock(&ptable->pt_zombie_lock);
2007 list_del_init(&lpni->lpni_hashlist);
2008 ptable->pt_zombies--;
2009 spin_unlock(&ptable->pt_zombie_lock);
2012 if (lpni->lpni_pref_nnids > 1) {
2013 struct lnet_nid_list *ne, *tmp;
2015 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
2017 list_del_init(&ne->nl_list);
2018 LIBCFS_FREE(ne, sizeof(*ne));
2021 LIBCFS_FREE(lpni, sizeof(*lpni));
2024 lnet_peer_net_decref_locked(lpn);
2027 struct lnet_peer_ni *
2028 lnet_nid2peerni_ex(struct lnet_nid *nid)
2029 __must_hold(&the_lnet.ln_api_mutex)
2031 struct lnet_peer_ni *lpni = NULL;
2033 if (the_lnet.ln_state != LNET_STATE_RUNNING)
2034 return ERR_PTR(-ESHUTDOWN);
2037 * find if a peer_ni already exists.
2038 * If so then just return that.
2040 lpni = lnet_peer_ni_find_locked(nid);
2044 lnet_net_unlock(LNET_LOCK_EX);
2046 lpni = lnet_peer_ni_traffic_add(nid, NULL);
2048 lnet_net_lock(LNET_LOCK_EX);
2054 * Get a peer_ni for the given nid, create it if necessary. Takes a
2055 * hold on the peer_ni.
2057 struct lnet_peer_ni *
2058 lnet_peerni_by_nid_locked(struct lnet_nid *nid,
2059 struct lnet_nid *pref, int cpt)
2061 struct lnet_peer_ni *lpni = NULL;
2063 if (the_lnet.ln_state != LNET_STATE_RUNNING)
2064 return ERR_PTR(-ESHUTDOWN);
2067 * find if a peer_ni already exists.
2068 * If so then just return that.
2070 lpni = lnet_peer_ni_find_locked(nid);
2076 * use the lnet_api_mutex to serialize the creation of the peer_ni
2077 * and the creation/deletion of the local ni/net. When a local ni is
2078 * created, if there exists a set of peer_nis on that network,
2079 * they need to be traversed and updated. When a local NI is
2080 * deleted, which could result in a network being deleted, then
2081 * all peer nis on that network need to be removed as well.
2083 * Creation through traffic should also be serialized with
2084 * creation through DLC.
2086 lnet_net_unlock(cpt);
2087 mutex_lock(&the_lnet.ln_api_mutex);
2089 * the_lnet.ln_state is only modified under the ln_api_lock, so a single
2090 * check here is sufficent.
2092 if (the_lnet.ln_state == LNET_STATE_RUNNING)
2093 lpni = lnet_peer_ni_traffic_add(nid, pref);
2095 mutex_unlock(&the_lnet.ln_api_mutex);
2098 /* Lock has been dropped, check again for shutdown. */
2099 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2100 if (!IS_ERR_OR_NULL(lpni))
2101 lnet_peer_ni_decref_locked(lpni);
2102 lpni = ERR_PTR(-ESHUTDOWN);
2109 lnet_peer_gw_discovery(struct lnet_peer *lp)
2113 spin_lock(&lp->lp_lock);
2114 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
2116 spin_unlock(&lp->lp_lock);
2122 lnet_peer_is_uptodate(struct lnet_peer *lp)
2126 spin_lock(&lp->lp_lock);
2127 rc = lnet_peer_is_uptodate_locked(lp);
2128 spin_unlock(&lp->lp_lock);
2133 * Is a peer uptodate from the point of view of discovery?
2135 * If it is currently being processed, obviously not.
2136 * A forced Ping or Push is also handled by the discovery thread.
2138 * Otherwise look at whether the peer needs rediscovering.
2141 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
2142 __must_hold(&lp->lp_lock)
2146 if (lp->lp_state & (LNET_PEER_DISCOVERING |
2147 LNET_PEER_FORCE_PING |
2148 LNET_PEER_FORCE_PUSH)) {
2150 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
2152 } else if (lnet_peer_needs_push(lp)) {
2154 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
2155 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
2166 /* Add the message to the peer's lp_dc_pendq and queue the peer for discovery */
2168 lnet_peer_queue_message(struct lnet_peer *lp, struct lnet_msg *msg)
2170 /* The discovery thread holds net_lock/EX and lp_lock when it splices
2171 * the lp_dc_pendq onto a local list for resending. Thus, we do the same
2172 * when adding to the list and queuing the peer to ensure that we do not
2173 * strand any messages on the lp_dc_pendq. This scheme ensures the
2174 * message will be resent even if the peer is already being discovered.
2175 * Therefore we needn't check the return value of
2176 * lnet_peer_queue_for_discovery(lp).
2178 lnet_net_lock(LNET_LOCK_EX);
2179 spin_lock(&lp->lp_lock);
2180 list_add_tail(&msg->msg_list, &lp->lp_dc_pendq);
2181 spin_unlock(&lp->lp_lock);
2182 lnet_peer_queue_for_discovery(lp);
2183 lnet_net_unlock(LNET_LOCK_EX);
2187 * Queue a peer for the attention of the discovery thread. Call with
2188 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
2189 * -EALREADY if the peer was already queued.
2191 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
2195 spin_lock(&lp->lp_lock);
2196 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2197 lp->lp_state |= LNET_PEER_DISCOVERING;
2198 spin_unlock(&lp->lp_lock);
2199 if (list_empty(&lp->lp_dc_list)) {
2200 lnet_peer_addref_locked(lp);
2201 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2202 wake_up(&the_lnet.ln_dc_waitq);
2208 CDEBUG(D_NET, "Queue peer %s: %d\n",
2209 libcfs_nidstr(&lp->lp_primary_nid), rc);
2215 * Discovery of a peer is complete. Wake all waiters on the peer.
2216 * Call with lnet_net_lock/EX held.
2218 static void lnet_peer_discovery_complete(struct lnet_peer *lp, int dc_error)
2220 struct lnet_msg *msg, *tmp;
2222 LIST_HEAD(pending_msgs);
2224 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
2225 libcfs_nidstr(&lp->lp_primary_nid));
2227 spin_lock(&lp->lp_lock);
2228 /* Our caller dropped lp_lock which may have allowed another thread to
2229 * set LNET_PEER_DISCOVERING, or it may be set if dc_error is non-zero.
2230 * Ensure it is cleared.
2232 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2234 lp->lp_dc_error = dc_error;
2235 lp->lp_state |= LNET_PEER_REDISCOVER;
2237 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
2238 spin_unlock(&lp->lp_lock);
2239 list_del_init(&lp->lp_dc_list);
2240 wake_up(&lp->lp_dc_waitq);
2242 if (lp->lp_rtr_refcount > 0)
2243 lnet_router_discovery_complete(lp);
2245 lnet_net_unlock(LNET_LOCK_EX);
2247 /* iterate through all pending messages and send them again */
2248 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
2249 list_del_init(&msg->msg_list);
2251 lnet_finalize(msg, dc_error);
2255 CDEBUG(D_NET, "sending pending message %s to target %s\n",
2256 lnet_msgtyp2str(msg->msg_type),
2257 libcfs_idstr(&msg->msg_target));
2258 rc = lnet_send(&msg->msg_src_nid_param, msg,
2259 &msg->msg_rtr_nid_param);
2261 CNETERR("Error sending %s to %s: %d\n",
2262 lnet_msgtyp2str(msg->msg_type),
2263 libcfs_idstr(&msg->msg_target), rc);
2264 lnet_finalize(msg, rc);
2267 lnet_net_lock(LNET_LOCK_EX);
2268 lnet_peer_decref_locked(lp);
2272 * Handle inbound push.
2273 * Like any event handler, called with lnet_res_lock/CPT held.
2275 void lnet_peer_push_event(struct lnet_event *ev)
2277 struct lnet_ping_buffer *pbuf;
2278 struct lnet_peer *lp;
2281 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
2283 /* lnet_find_peer() adds a refcount */
2284 lp = lnet_find_peer(&ev->source.nid);
2286 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
2287 libcfs_nidstr(&ev->initiator.nid),
2288 libcfs_nidstr(&ev->source.nid));
2289 pbuf->pb_needs_post = true;
2293 /* Ensure peer state remains consistent while we modify it. */
2294 spin_lock(&lp->lp_lock);
2297 * If some kind of error happened the contents of the message
2298 * cannot be used. Clear the NIDS_UPTODATE and set the
2299 * FORCE_PING flag to trigger a ping.
2302 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2303 lp->lp_state |= LNET_PEER_FORCE_PING;
2304 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2306 libcfs_nidstr(&lp->lp_primary_nid),
2307 libcfs_nidstr(&ev->source.nid));
2312 * A push with invalid or corrupted info. Clear the UPTODATE
2313 * flag to trigger a ping.
2315 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2316 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2317 lp->lp_state |= LNET_PEER_FORCE_PING;
2318 CDEBUG(D_NET, "Corrupted Push from %s\n",
2319 libcfs_nidstr(&lp->lp_primary_nid));
2323 /* Make sure we'll allocate the correct size ping buffer when
2326 infobytes = lnet_ping_info_size(&pbuf->pb_info);
2327 if (lp->lp_data_bytes < infobytes)
2328 lp->lp_data_bytes = infobytes;
2331 * A non-Multi-Rail peer is not supposed to be capable of
2334 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2335 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2336 libcfs_nidstr(&lp->lp_primary_nid));
2341 * The peer may have discovery disabled at its end. Set
2342 * NO_DISCOVERY as appropriate.
2344 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2345 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2346 libcfs_nidstr(&lp->lp_primary_nid));
2348 * Mark the peer for deletion if we already know about it
2349 * and it's going from discovery set to no discovery set
2351 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2352 LNET_PEER_DISCOVERING)) &&
2353 lp->lp_state & LNET_PEER_DISCOVERED) {
2354 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2355 libcfs_nidstr(&lp->lp_primary_nid),
2357 lp->lp_state |= LNET_PEER_MARK_DELETION;
2359 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2360 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2361 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2362 libcfs_nidstr(&lp->lp_primary_nid));
2363 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2367 * Update the MULTI_RAIL flag based on the push. If the peer
2368 * was configured with DLC then the setting should match what
2370 * NB: We verified above that the MR feature bit is set in pi_features
2372 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2373 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2374 libcfs_nidstr(&lp->lp_primary_nid), lp);
2375 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2376 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2377 libcfs_nidstr(&lp->lp_primary_nid));
2378 } else if (lnet_peer_discovery_disabled) {
2379 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2380 libcfs_nidstr(&lp->lp_primary_nid), lp);
2381 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2382 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2383 libcfs_nidstr(&lp->lp_primary_nid), lp);
2385 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2386 libcfs_nidstr(&lp->lp_primary_nid), lp);
2387 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2388 lnet_peer_clr_non_mr_pref_nids(lp);
2391 /* Check for truncation of the Put message. Clear the
2392 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2393 * and tell discovery to allocate a bigger buffer.
2395 if (ev->mlength < ev->rlength) {
2396 if (the_lnet.ln_push_target_nbytes < infobytes)
2397 the_lnet.ln_push_target_nbytes = infobytes;
2398 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2399 lp->lp_state |= LNET_PEER_FORCE_PING;
2400 CDEBUG(D_NET, "Truncated Push from %s (%d bytes)\n",
2401 libcfs_nidstr(&lp->lp_primary_nid), infobytes);
2405 /* always assume new data */
2406 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2407 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2409 /* If there is data present that hasn't been processed yet,
2410 * we'll replace it if the Put contained newer data and it
2411 * fits. We're racing with a Ping or earlier Push in this
2414 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2415 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2416 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2417 infobytes <= lp->lp_data->pb_nbytes) {
2418 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2420 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2421 libcfs_nidstr(&lp->lp_primary_nid),
2422 LNET_PING_BUFFER_SEQNO(pbuf),
2423 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2429 * Allocate a buffer to copy the data. On a failure we drop
2430 * the Push and set FORCE_PING to force the discovery
2431 * thread to fix the problem by pinging the peer.
2433 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_bytes, GFP_ATOMIC);
2435 lp->lp_state |= LNET_PEER_FORCE_PING;
2436 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2437 libcfs_nidstr(&lp->lp_primary_nid),
2438 LNET_PING_BUFFER_SEQNO(pbuf));
2443 unsafe_memcpy(&lp->lp_data->pb_info, &pbuf->pb_info, infobytes,
2445 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2446 CDEBUG(D_NET, "Received Push %s %u\n",
2447 libcfs_nidstr(&lp->lp_primary_nid),
2448 LNET_PING_BUFFER_SEQNO(pbuf));
2451 /* We've processed this buffer. It can be reposted */
2452 pbuf->pb_needs_post = true;
2455 * Queue the peer for discovery if not done, force it on the request
2456 * queue and wake the discovery thread if the peer was already queued,
2457 * because its status changed.
2459 spin_unlock(&lp->lp_lock);
2460 lnet_net_lock(LNET_LOCK_EX);
2461 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2462 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2463 wake_up(&the_lnet.ln_dc_waitq);
2465 /* Drop refcount from lookup */
2466 lnet_peer_decref_locked(lp);
2467 lnet_net_unlock(LNET_LOCK_EX);
2471 * Clear the discovery error state, unless we're already discovering
2472 * this peer, in which case the error is current.
2474 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2476 spin_lock(&lp->lp_lock);
2477 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2478 lp->lp_dc_error = 0;
2479 spin_unlock(&lp->lp_lock);
2483 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2484 * dropped/retaken within this function. An lnet_peer_ni is passed in
2485 * because discovery could tear down an lnet_peer.
2488 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2491 struct lnet_peer *lp;
2496 lnet_net_unlock(cpt);
2497 lnet_net_lock(LNET_LOCK_EX);
2498 lp = lpni->lpni_peer_net->lpn_peer;
2499 lnet_peer_clear_discovery_error(lp);
2502 * We're willing to be interrupted. The lpni can become a
2503 * zombie if we race with DLC, so we must check for that.
2506 /* Keep lp alive when the lnet_net_lock is unlocked */
2507 lnet_peer_addref_locked(lp);
2508 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2509 if (signal_pending(current))
2511 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2514 * Don't repeat discovery if discovery is disabled. This is
2515 * done to ensure we can use discovery as a standard ping as
2516 * well for backwards compatibility with routers which do not
2517 * have discovery or have discovery disabled
2519 if (lnet_is_discovery_disabled(lp) && count > 0)
2521 if (lp->lp_dc_error)
2523 if (lnet_peer_is_uptodate(lp))
2525 if (lp->lp_state & LNET_PEER_MARK_DELETED)
2527 lnet_peer_queue_for_discovery(lp);
2529 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2532 * If caller requested a non-blocking operation then
2533 * return immediately. Once discovery is complete any
2534 * pending messages that were stopped due to discovery
2535 * will be transmitted.
2540 lnet_net_unlock(LNET_LOCK_EX);
2542 finish_wait(&lp->lp_dc_waitq, &wait);
2543 lnet_net_lock(LNET_LOCK_EX);
2544 lnet_peer_decref_locked(lp);
2545 /* Peer may have changed */
2546 lp = lpni->lpni_peer_net->lpn_peer;
2548 finish_wait(&lp->lp_dc_waitq, &wait);
2550 lnet_net_unlock(LNET_LOCK_EX);
2552 lnet_peer_decref_locked(lp);
2554 * The peer may have changed, so re-check and rediscover if that turns
2555 * out to have been the case. The reference count on lp ensured that
2556 * even if it was unlinked from lpni the memory could not be recycled.
2557 * Thus the check below is sufficient to determine whether the peer
2558 * changed. If the peer changed, then lp must not be dereferenced.
2560 if (lp != lpni->lpni_peer_net->lpn_peer)
2563 if (signal_pending(current))
2565 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2567 else if (lp->lp_dc_error)
2568 rc = lp->lp_dc_error;
2570 CDEBUG(D_NET, "non-blocking discovery\n");
2571 else if (!lnet_peer_is_uptodate(lp) &&
2572 !(lnet_is_discovery_disabled(lp) ||
2573 (lp->lp_state & LNET_PEER_MARK_DELETED)))
2576 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2577 (lp ? libcfs_nidstr(&lp->lp_primary_nid) : "(none)"),
2578 libcfs_nidstr(&lpni->lpni_nid), rc,
2579 (!block) ? "pending discovery" : "discovery complete");
2584 /* Handle an incoming ack for a push. */
2586 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2588 struct lnet_ping_buffer *pbuf;
2590 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2591 spin_lock(&lp->lp_lock);
2592 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2593 lp->lp_push_error = ev->status;
2595 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2597 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2598 spin_unlock(&lp->lp_lock);
2600 CDEBUG(D_NET, "peer %s ev->status %d\n",
2601 libcfs_nidstr(&lp->lp_primary_nid), ev->status);
2604 static bool find_primary(struct lnet_nid *nid,
2605 struct lnet_ping_buffer *pbuf)
2607 struct lnet_ping_info *pi = &pbuf->pb_info;
2608 struct lnet_ping_iter piter;
2611 if (pi->pi_features & LNET_PING_FEAT_PRIMARY_LARGE) {
2612 /* First large nid is primary */
2613 for (stp = ping_iter_first(&piter, pbuf, nid);
2615 stp = ping_iter_next(&piter, nid)) {
2616 if (nid_is_nid4(nid))
2618 /* nid has already been copied in */
2621 /* no large nids ... weird ... ignore the flag
2622 * and use first nid.
2625 /* pi_nids[1] is primary */
2626 if (pi->pi_nnis < 2)
2628 lnet_nid4_to_nid(pbuf->pb_info.pi_ni[1].ns_nid, nid);
2632 /* Handle a Reply message. This is the reply to a Ping message. */
2634 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2636 struct lnet_ping_buffer *pbuf;
2637 struct lnet_nid primary;
2640 bool ping_feat_disc;
2642 spin_lock(&lp->lp_lock);
2644 lp->lp_disc_src_nid = ev->target.nid;
2645 lp->lp_disc_dst_nid = ev->source.nid;
2648 * If some kind of error happened the contents of message
2649 * cannot be used. Set PING_FAILED to trigger a retry.
2652 lp->lp_state |= LNET_PEER_PING_FAILED;
2653 lp->lp_ping_error = ev->status;
2654 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2656 libcfs_nidstr(&lp->lp_primary_nid),
2657 libcfs_nidstr(&ev->source.nid));
2661 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2662 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2663 lnet_swap_pinginfo(pbuf);
2666 * A reply with invalid or corrupted info. Set PING_FAILED to
2669 rc = lnet_ping_info_validate(&pbuf->pb_info);
2671 lp->lp_state |= LNET_PEER_PING_FAILED;
2672 lp->lp_ping_error = 0;
2673 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2674 libcfs_nidstr(&lp->lp_primary_nid), rc);
2679 * The peer may have discovery disabled at its end. Set
2680 * NO_DISCOVERY as appropriate.
2682 ping_feat_disc = pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY;
2683 if (!ping_feat_disc || lnet_peer_discovery_disabled) {
2684 CDEBUG(D_NET, "Peer %s has discovery %s, local discovery %s\n",
2685 libcfs_nidstr(&lp->lp_primary_nid),
2686 ping_feat_disc ? "enabled" : "disabled",
2687 lnet_peer_discovery_disabled ? "disabled" : "enabled");
2689 /* Detect whether this peer has toggled discovery from on to
2690 * off and whether we can delete and re-create the peer. Peers
2691 * that were manually configured cannot be deleted by discovery.
2692 * We need to delete this peer and re-create it if the peer was
2693 * not configured manually, is currently considered DD capable,
2695 * 1. We've already discovered the peer (the peer has toggled
2696 * the discovery feature from on to off), or
2697 * 2. The peer is considered MR, but it was not user configured
2698 * (this was a "temporary" peer created via the kernel APIs
2699 * that we're discovering for the first time)
2701 if (!(lp->lp_state & (LNET_PEER_CONFIGURED |
2702 LNET_PEER_NO_DISCOVERY)) &&
2703 (lp->lp_state & (LNET_PEER_DISCOVERED |
2704 LNET_PEER_MULTI_RAIL))) {
2705 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2706 libcfs_nidstr(&lp->lp_primary_nid),
2708 lp->lp_state |= LNET_PEER_MARK_DELETION;
2710 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2712 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2713 libcfs_nidstr(&lp->lp_primary_nid));
2714 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2718 * Update the MULTI_RAIL flag based on the reply. If the peer
2719 * was configured with DLC then the setting should match what
2722 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2723 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2724 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2725 libcfs_nidstr(&lp->lp_primary_nid), lp);
2726 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2727 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2728 libcfs_nidstr(&lp->lp_primary_nid));
2729 } else if (lnet_peer_discovery_disabled) {
2731 "peer %s(%p) not MR: DD disabled locally\n",
2732 libcfs_nidstr(&lp->lp_primary_nid), lp);
2733 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2735 "peer %s(%p) not MR: DD disabled remotely\n",
2736 libcfs_nidstr(&lp->lp_primary_nid), lp);
2738 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2739 libcfs_nidstr(&lp->lp_primary_nid), lp);
2740 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2741 lnet_peer_clr_non_mr_pref_nids(lp);
2743 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2744 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2745 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2746 libcfs_nidstr(&lp->lp_primary_nid));
2748 CERROR("Multi-Rail state vanished from %s\n",
2749 libcfs_nidstr(&lp->lp_primary_nid));
2750 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2754 infobytes = lnet_ping_info_size(&pbuf->pb_info);
2756 * Make sure we'll allocate the correct size ping buffer when
2759 if (lp->lp_data_bytes < infobytes)
2760 lp->lp_data_bytes = infobytes;
2762 /* Check for truncation of the Reply. Clear PING_SENT and set
2763 * PING_FAILED to trigger a retry.
2765 if (pbuf->pb_nbytes < infobytes) {
2766 if (the_lnet.ln_push_target_nbytes < infobytes)
2767 the_lnet.ln_push_target_nbytes = infobytes;
2768 lp->lp_state |= LNET_PEER_PING_FAILED;
2769 lp->lp_ping_error = 0;
2770 CDEBUG(D_NET, "Truncated Reply from %s (%d bytes)\n",
2771 libcfs_nidstr(&lp->lp_primary_nid), infobytes);
2776 * Check the sequence numbers in the reply. These are only
2777 * available if the reply came from a Multi-Rail peer.
2779 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2780 find_primary(&primary, pbuf) &&
2781 nid_same(&lp->lp_primary_nid, &primary)) {
2782 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2783 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2784 libcfs_nidstr(&lp->lp_primary_nid),
2785 LNET_PING_BUFFER_SEQNO(pbuf),
2788 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2791 /* We're happy with the state of the data in the buffer. */
2792 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2793 libcfs_nidstr(&lp->lp_primary_nid), lp->lp_peer_seqno,
2795 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2796 lnet_ping_buffer_decref(lp->lp_data);
2798 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2799 lnet_ping_buffer_addref(pbuf);
2802 lp->lp_state &= ~LNET_PEER_PING_SENT;
2803 spin_unlock(&lp->lp_lock);
2807 * Send event handling. Only matters for error cases, where we clean
2808 * up state on the peer and peer_ni that would otherwise be updated in
2809 * the REPLY event handler for a successful Ping, and the ACK event
2810 * handler for a successful Push.
2813 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2820 spin_lock(&lp->lp_lock);
2821 if (ev->msg_type == LNET_MSG_GET) {
2822 lp->lp_state &= ~LNET_PEER_PING_SENT;
2823 lp->lp_state |= LNET_PEER_PING_FAILED;
2824 lp->lp_ping_error = ev->status;
2825 } else { /* ev->msg_type == LNET_MSG_PUT */
2826 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2827 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2828 lp->lp_push_error = ev->status;
2830 spin_unlock(&lp->lp_lock);
2831 rc = LNET_REDISCOVER_PEER;
2833 CDEBUG(D_NET, "%s Send to %s: %d\n",
2834 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2835 libcfs_nidstr(&ev->target.nid), rc);
2840 * Unlink event handling. This event is only seen if a call to
2841 * LNetMDUnlink() caused the event to be unlinked. If this call was
2842 * made after the event was set up in LNetGet() or LNetPut() then we
2843 * assume the Ping or Push timed out.
2846 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2848 spin_lock(&lp->lp_lock);
2849 /* We've passed through LNetGet() */
2850 if (lp->lp_state & LNET_PEER_PING_SENT) {
2851 lp->lp_state &= ~LNET_PEER_PING_SENT;
2852 lp->lp_state |= LNET_PEER_PING_FAILED;
2853 lp->lp_ping_error = -ETIMEDOUT;
2854 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2855 libcfs_nidstr(&lp->lp_primary_nid));
2857 /* We've passed through LNetPut() */
2858 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2859 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2860 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2861 lp->lp_push_error = -ETIMEDOUT;
2862 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2863 libcfs_nidstr(&lp->lp_primary_nid));
2865 spin_unlock(&lp->lp_lock);
2869 * Event handler for the discovery EQ.
2871 * Called with lnet_res_lock(cpt) held. The cpt is the
2872 * lnet_cpt_of_cookie() of the md handle cookie.
2874 static void lnet_discovery_event_handler(struct lnet_event *event)
2876 struct lnet_peer *lp = event->md_user_ptr;
2877 struct lnet_ping_buffer *pbuf;
2880 /* discovery needs to take another look */
2881 rc = LNET_REDISCOVER_PEER;
2883 CDEBUG(D_NET, "Received event: %d\n", event->type);
2885 switch (event->type) {
2886 case LNET_EVENT_ACK:
2887 lnet_discovery_event_ack(lp, event);
2889 case LNET_EVENT_REPLY:
2890 lnet_discovery_event_reply(lp, event);
2892 case LNET_EVENT_SEND:
2893 /* Only send failure triggers a retry. */
2894 rc = lnet_discovery_event_send(lp, event);
2896 case LNET_EVENT_UNLINK:
2897 /* LNetMDUnlink() was called */
2898 lnet_discovery_event_unlink(lp, event);
2901 /* Invalid events. */
2904 lnet_net_lock(LNET_LOCK_EX);
2905 if (event->unlinked) {
2906 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2907 lnet_ping_buffer_decref(pbuf);
2908 lnet_peer_decref_locked(lp);
2911 /* put peer back at end of request queue, if discovery not already
2913 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp) &&
2914 lnet_peer_queue_for_discovery(lp)) {
2915 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2916 wake_up(&the_lnet.ln_dc_waitq);
2918 lnet_net_unlock(LNET_LOCK_EX);
2921 u32 *ping_iter_first(struct lnet_ping_iter *pi,
2922 struct lnet_ping_buffer *pbuf,
2923 struct lnet_nid *nid)
2925 pi->pinfo = &pbuf->pb_info;
2926 pi->pos = &pbuf->pb_info.pi_ni;
2927 pi->end = (void *)pi->pinfo +
2928 min_t(int, pbuf->pb_nbytes,
2929 lnet_ping_info_size(pi->pinfo));
2930 /* lnet_ping_info_validiate ensures there will be one
2931 * lnet_ni_status at the start
2934 lnet_nid4_to_nid(pbuf->pb_info.pi_ni[0].ns_nid, nid);
2936 pi->pos += sizeof(struct lnet_ni_status);
2937 return &pbuf->pb_info.pi_ni[0].ns_status;
2940 u32 *ping_iter_next(struct lnet_ping_iter *pi, struct lnet_nid *nid)
2942 int off = offsetof(struct lnet_ping_info, pi_ni[pi->pinfo->pi_nnis]);
2944 if (pi->pos < ((void *)pi->pinfo + off)) {
2945 struct lnet_ni_status *ns = pi->pos;
2948 if (pi->pos > pi->end)
2951 lnet_nid4_to_nid(ns->ns_nid, nid);
2952 return &ns->ns_status;
2955 while (pi->pinfo->pi_features & LNET_PING_FEAT_LARGE_ADDR) {
2956 struct lnet_ni_large_status *lns = pi->pos;
2958 if (pi->pos + 8 > pi->end)
2959 /* Not safe to examine next */
2961 pi->pos = lnet_ping_sts_next(lns);
2962 if (pi->pos > pi->end)
2964 if (NID_BYTES(&lns->ns_nid) > sizeof(struct lnet_nid))
2968 return &lns->ns_status;
2973 int ping_info_count_entries(struct lnet_ping_buffer *pbuf)
2975 struct lnet_ping_iter pi;
2979 for (st = ping_iter_first(&pi, pbuf, NULL); st;
2980 st = ping_iter_next(&pi, NULL))
2987 * Build a peer from incoming data.
2989 * The NIDs in the incoming data are supposed to be structured as follows:
2992 * - other NIDs in same net
2993 * - NIDs in second net
2994 * - NIDs in third net
2996 * This due to the way the list of NIDs in the data is created.
2998 * Note that this function will mark the peer uptodate unless an
2999 * ENOMEM is encontered. All other errors are due to a conflict
3000 * between the DLC configuration and what discovery sees. We treat DLC
3001 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
3002 * peer from becoming stuck in discovery.
3004 static int lnet_peer_merge_data(struct lnet_peer *lp,
3005 struct lnet_ping_buffer *pbuf)
3007 struct lnet_peer_net *lpn;
3008 struct lnet_peer_ni *lpni;
3009 struct lnet_nid *curnis = NULL;
3010 struct lnet_ni_large_status *addnis = NULL;
3011 struct lnet_nid *delnis = NULL;
3012 struct lnet_ping_iter pi;
3013 struct lnet_nid nid;
3015 struct lnet_nid primary = {};
3016 bool want_large_primary;
3026 flags = LNET_PEER_DISCOVERED;
3027 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
3028 flags |= LNET_PEER_MULTI_RAIL;
3031 * Cache the routing feature for the peer; whether it is enabled
3032 * for disabled as reported by the remote peer.
3034 spin_lock(&lp->lp_lock);
3035 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
3036 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
3038 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
3039 spin_unlock(&lp->lp_lock);
3041 nnis = ping_info_count_entries(pbuf);
3042 nnis = max_t(int, lp->lp_nnis, nnis);
3043 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
3044 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
3045 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
3046 if (!curnis || !addnis || !delnis) {
3054 /* Construct the list of NIDs present in peer. */
3056 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
3057 curnis[ncurnis++] = lpni->lpni_nid;
3059 /* Check for NIDs in pbuf not present in curnis[].
3060 * Skip the first, which is loop-back. Take second as
3061 * primary, unless a large primary is found.
3063 ping_iter_first(&pi, pbuf, NULL);
3064 stp = ping_iter_next(&pi, &nid);
3067 want_large_primary = (pbuf->pb_info.pi_features &
3068 LNET_PING_FEAT_PRIMARY_LARGE);
3069 for (; stp; stp = ping_iter_next(&pi, &nid)) {
3070 for (j = 0; j < ncurnis; j++)
3071 if (nid_same(&nid, &curnis[j]))
3074 addnis[naddnis].ns_nid = nid;
3075 addnis[naddnis].ns_status = *stp;
3078 if (want_large_primary && nid.nid_size) {
3080 want_large_primary = false;
3084 * Check for NIDs in curnis[] not present in pbuf.
3085 * The nested loop starts at 1 to skip the loopback NID.
3087 * But never add the loopback NID to delnis[]: if it is
3088 * present in curnis[] then this peer is for this node.
3090 for (i = 0; i < ncurnis; i++) {
3091 if (nid_is_lo0(&curnis[i]))
3093 ping_iter_first(&pi, pbuf, NULL);
3094 while ((stp = ping_iter_next(&pi, &nid)) != NULL) {
3095 if (nid_same(&curnis[i], &nid)) {
3097 * update the information we cache for the
3098 * peer with the latest information we
3101 lpni = lnet_peer_ni_find_locked(&curnis[i]);
3103 lpni->lpni_ns_status = *stp;
3104 lnet_peer_ni_decref_locked(lpni);
3110 delnis[ndelnis++] = curnis[i];
3114 * If we get here and the discovery is disabled then we don't want
3115 * to add or delete any NIs. We just updated the ones we have some
3116 * information on, and call it a day
3119 if (lnet_is_discovery_disabled(lp))
3122 for (i = 0; i < naddnis; i++) {
3123 rc = lnet_peer_add_nid(lp, &addnis[i].ns_nid, flags);
3125 CERROR("Error adding NID %s to peer %s: %d\n",
3126 libcfs_nidstr(&addnis[i].ns_nid),
3127 libcfs_nidstr(&lp->lp_primary_nid), rc);
3131 lpni = lnet_peer_ni_find_locked(&addnis[i].ns_nid);
3133 lpni->lpni_ns_status = addnis[i].ns_status;
3134 lnet_peer_ni_decref_locked(lpni);
3138 for (i = 0; i < ndelnis; i++) {
3140 * for routers it's okay to delete the primary_nid because
3141 * the upper layers don't really rely on it. So if we're
3142 * being told that the router changed its primary_nid
3143 * then it's okay to delete it.
3145 if (lp->lp_rtr_refcount > 0)
3146 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
3147 rc = lnet_peer_del_nid(lp, &delnis[i], flags);
3149 CERROR("Error deleting NID %s from peer %s: %d\n",
3150 libcfs_nidstr(&delnis[i]),
3151 libcfs_nidstr(&lp->lp_primary_nid), rc);
3157 /* The peer net for the primary NID should be the first entry in the
3158 * peer's lp_peer_nets list, and the peer NI for the primary NID should
3159 * be the first entry in its peer net's lpn_peer_nis list.
3161 find_primary(&nid, pbuf);
3162 lpni = lnet_peer_ni_find_locked(&nid);
3164 CERROR("Internal error: Failed to lookup peer NI for primary NID: %s\n",
3165 libcfs_nidstr(&nid));
3169 lnet_peer_ni_decref_locked(lpni);
3171 lpn = lpni->lpni_peer_net;
3172 if (lpn->lpn_peer_nets.prev != &lp->lp_peer_nets)
3173 list_move(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
3175 if (lpni->lpni_peer_nis.prev != &lpni->lpni_peer_net->lpn_peer_nis)
3176 list_move(&lpni->lpni_peer_nis,
3177 &lpni->lpni_peer_net->lpn_peer_nis);
3180 * Errors other than -ENOMEM are due to peers having been
3181 * configured with DLC. Ignore these because DLC overrides
3186 /* If this peer is a gateway, invoke the routing callback to update
3187 * the associated route status
3189 if (lp->lp_rtr_refcount > 0)
3190 lnet_router_discovery_ping_reply(lp, pbuf);
3192 CFS_FREE_PTR_ARRAY(curnis, nnis);
3193 CFS_FREE_PTR_ARRAY(addnis, nnis);
3194 CFS_FREE_PTR_ARRAY(delnis, nnis);
3195 lnet_ping_buffer_decref(pbuf);
3196 CDEBUG(D_NET, "peer %s (%p): %d\n",
3197 libcfs_nidstr(&lp->lp_primary_nid), lp, rc);
3200 spin_lock(&lp->lp_lock);
3201 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3202 lp->lp_state |= LNET_PEER_FORCE_PING;
3203 spin_unlock(&lp->lp_lock);
3209 * The data in pbuf says lp is its primary peer, but the data was
3210 * received by a different peer. Try to update lp with the data.
3213 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
3215 struct lnet_handle_md mdh;
3217 /* Queue lp for discovery, and force it on the request queue. */
3218 lnet_net_lock(LNET_LOCK_EX);
3219 if (lnet_peer_queue_for_discovery(lp))
3220 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
3221 lnet_net_unlock(LNET_LOCK_EX);
3223 LNetInvalidateMDHandle(&mdh);
3226 * Decide whether we can move the peer to the DATA_PRESENT state.
3228 * We replace stale data for a multi-rail peer, repair PING_FAILED
3229 * status, and preempt FORCE_PING.
3231 * If after that we have DATA_PRESENT, we merge it into this peer.
3233 spin_lock(&lp->lp_lock);
3234 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
3235 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
3236 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
3237 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3238 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3239 lnet_ping_buffer_decref(pbuf);
3244 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3245 lnet_ping_buffer_decref(lp->lp_data);
3247 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3249 if (lp->lp_state & LNET_PEER_PING_FAILED) {
3250 mdh = lp->lp_ping_mdh;
3251 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3252 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3253 lp->lp_ping_error = 0;
3255 if (lp->lp_state & LNET_PEER_FORCE_PING)
3256 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3257 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3258 spin_unlock(&lp->lp_lock);
3260 if (!LNetMDHandleIsInvalid(mdh))
3264 return lnet_peer_merge_data(lp, pbuf);
3266 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3270 static bool lnet_is_nid_in_ping_info(struct lnet_nid *nid,
3271 struct lnet_ping_buffer *pbuf)
3273 struct lnet_ping_iter pi;
3274 struct lnet_nid pnid;
3277 for (st = ping_iter_first(&pi, pbuf, &pnid);
3279 st = ping_iter_next(&pi, &pnid))
3280 if (nid_same(nid, &pnid))
3285 /* Delete a peer that has been marked for deletion. NB: when this peer was added
3286 * to the discovery queue a reference was taken that will prevent the peer from
3287 * actually being freed by this function. After this function exits the
3288 * discovery thread should call lnet_peer_discovery_complete() which will
3289 * drop that reference as well as wake any waiters that may also be holding a
3292 static int lnet_peer_deletion(struct lnet_peer *lp)
3293 __must_hold(&lp->lp_lock)
3295 struct list_head rlist;
3296 struct lnet_route *route, *tmp;
3297 int sensitivity = lp->lp_health_sensitivity;
3300 INIT_LIST_HEAD(&rlist);
3302 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3303 libcfs_nidstr(&lp->lp_primary_nid), lp, lp->lp_state);
3305 /* no-op if lnet_peer_del() has already been called on this peer */
3306 if (lp->lp_state & LNET_PEER_MARK_DELETED)
3307 goto clear_discovering;
3309 spin_unlock(&lp->lp_lock);
3311 mutex_lock(&the_lnet.ln_api_mutex);
3312 if (the_lnet.ln_state != LNET_STATE_RUNNING ||
3313 the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
3314 mutex_unlock(&the_lnet.ln_api_mutex);
3315 spin_lock(&lp->lp_lock);
3317 goto clear_discovering;
3320 lnet_peer_cancel_discovery(lp);
3321 lnet_net_lock(LNET_LOCK_EX);
3322 list_for_each_entry_safe(route, tmp,
3325 lnet_move_route(route, NULL, &rlist);
3327 /* lnet_peer_del_locked() deletes all the peer NIs owned by this peer */
3328 rc = lnet_peer_del_locked(lp);
3330 CNETERR("Internal error: Unable to delete peer %s rc %d\n",
3331 libcfs_nidstr(&lp->lp_primary_nid), rc);
3333 lnet_net_unlock(LNET_LOCK_EX);
3335 list_for_each_entry_safe(route, tmp,
3337 /* re-add these routes */
3338 lnet_add_route(route->lr_net,
3343 LIBCFS_FREE(route, sizeof(*route));
3346 mutex_unlock(&the_lnet.ln_api_mutex);
3348 spin_lock(&lp->lp_lock);
3353 lp->lp_state &= ~(LNET_PEER_DISCOVERING | LNET_PEER_FORCE_PING |
3354 LNET_PEER_FORCE_PUSH);
3360 * Update a peer using the data received.
3362 static int lnet_peer_data_present(struct lnet_peer *lp)
3363 __must_hold(&lp->lp_lock)
3365 struct lnet_ping_buffer *pbuf;
3366 struct lnet_peer_ni *lpni;
3367 struct lnet_nid nid;
3373 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3374 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3375 spin_unlock(&lp->lp_lock);
3378 * Modifications of peer structures are done while holding the
3379 * ln_api_mutex. A global lock is required because we may be
3380 * modifying multiple peer structures, and a mutex greatly
3381 * simplifies memory management.
3383 * The actual changes to the data structures must also protect
3384 * against concurrent lookups, for which the lnet_net_lock in
3385 * LNET_LOCK_EX mode is used.
3387 mutex_lock(&the_lnet.ln_api_mutex);
3388 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3394 * If this peer is not on the peer list then it is being torn
3395 * down, and our reference count may be all that is keeping it
3396 * alive. Don't do any work on it.
3398 if (list_empty(&lp->lp_peer_list)) {
3399 lnet_ping_buffer_decref(pbuf);
3403 flags = LNET_PEER_DISCOVERED;
3404 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
3405 flags |= LNET_PEER_MULTI_RAIL;
3408 * Check whether the primary NID in the message matches the
3409 * primary NID of the peer. If it does, update the peer, if
3410 * it it does not, check whether there is already a peer with
3411 * that primary NID. If no such peer exists, try to update
3412 * the primary NID of the current peer (allowed if it was
3413 * created due to message traffic) and complete the update.
3414 * If the peer did exist, hand off the data to it.
3416 * The peer for the loopback interface is a special case: this
3417 * is the peer for the local node, and we want to set its
3418 * primary NID to the correct value here. Moreover, this peer
3419 * can show up with only the loopback NID in the ping buffer.
3421 if (!find_primary(&nid, pbuf)) {
3422 lnet_ping_buffer_decref(pbuf);
3425 if (nid_is_lo0(&lp->lp_primary_nid)) {
3426 rc = lnet_peer_set_primary_nid(lp, &nid, flags);
3428 lnet_ping_buffer_decref(pbuf);
3430 rc = lnet_peer_merge_data(lp, pbuf);
3432 * if the primary nid of the peer is present in the ping info returned
3433 * from the peer, but it's not the local primary peer we have
3434 * cached and discovery is disabled, then we don't want to update
3435 * our local peer info, by adding or removing NIDs, we just want
3436 * to update the status of the nids that we currently have
3437 * recorded in that peer.
3439 } else if (nid_same(&lp->lp_primary_nid, &nid) ||
3440 (lnet_is_nid_in_ping_info(&lp->lp_primary_nid, pbuf) &&
3441 lnet_is_discovery_disabled(lp))) {
3442 rc = lnet_peer_merge_data(lp, pbuf);
3444 lpni = lnet_peer_ni_find_locked(&nid);
3445 if (!lpni || lp == lpni->lpni_peer_net->lpn_peer) {
3446 rc = lnet_peer_set_primary_nid(lp, &nid, flags);
3448 CERROR("Primary NID error %s versus %s: %d\n",
3449 libcfs_nidstr(&lp->lp_primary_nid),
3450 libcfs_nidstr(&nid), rc);
3451 lnet_ping_buffer_decref(pbuf);
3453 rc = lnet_peer_merge_data(lp, pbuf);
3456 lnet_peer_ni_decref_locked(lpni);
3458 struct lnet_peer *new_lp;
3459 new_lp = lpni->lpni_peer_net->lpn_peer;
3461 * if lp has discovery/MR enabled that means new_lp
3462 * should have discovery/MR enabled as well, since
3463 * it's the same peer, which we're about to merge
3465 spin_lock(&lp->lp_lock);
3466 spin_lock(&new_lp->lp_lock);
3467 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
3468 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
3469 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
3470 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
3471 /* If we're processing a ping reply then we may be
3472 * about to send a push to the peer that we ping'd.
3473 * Since the ping reply that we're processing was
3474 * received by lp, we need to set the discovery source
3475 * NID for new_lp to the NID stored in lp.
3477 if (!LNET_NID_IS_ANY(&lp->lp_disc_src_nid)) {
3478 new_lp->lp_disc_src_nid = lp->lp_disc_src_nid;
3479 new_lp->lp_disc_dst_nid = lp->lp_disc_dst_nid;
3481 spin_unlock(&new_lp->lp_lock);
3482 spin_unlock(&lp->lp_lock);
3484 rc = lnet_peer_set_primary_data(new_lp, pbuf);
3485 lnet_consolidate_routes_locked(lp, new_lp);
3486 lnet_peer_ni_decref_locked(lpni);
3490 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n",
3491 libcfs_nidstr(&lp->lp_primary_nid), lp, rc,
3493 mutex_unlock(&the_lnet.ln_api_mutex);
3495 spin_lock(&lp->lp_lock);
3496 /* Tell discovery to re-check the peer immediately. */
3498 rc = LNET_REDISCOVER_PEER;
3503 * A ping failed. Clear the PING_FAILED state and set the
3504 * FORCE_PING state, to ensure a retry even if discovery is
3505 * disabled. This avoids being left with incorrect state.
3507 static int lnet_peer_ping_failed(struct lnet_peer *lp)
3508 __must_hold(&lp->lp_lock)
3510 struct lnet_handle_md mdh;
3513 mdh = lp->lp_ping_mdh;
3514 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3515 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3516 lp->lp_state |= LNET_PEER_FORCE_PING;
3517 rc = lp->lp_ping_error;
3518 lp->lp_ping_error = 0;
3519 spin_unlock(&lp->lp_lock);
3521 if (!LNetMDHandleIsInvalid(mdh))
3524 CDEBUG(D_NET, "peer %s:%d\n",
3525 libcfs_nidstr(&lp->lp_primary_nid), rc);
3527 spin_lock(&lp->lp_lock);
3528 return rc ? rc : LNET_REDISCOVER_PEER;
3531 /* Active side of ping. */
3532 static int lnet_peer_send_ping(struct lnet_peer *lp)
3533 __must_hold(&lp->lp_lock)
3539 lp->lp_state |= LNET_PEER_PING_SENT;
3540 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3541 spin_unlock(&lp->lp_lock);
3543 cpt = lnet_net_lock_current();
3544 /* Refcount for MD. */
3545 lnet_peer_addref_locked(lp);
3546 lnet_net_unlock(cpt);
3548 bytes = max_t(int, lp->lp_data_bytes, LNET_PING_INFO_MIN_SIZE);
3550 rc = lnet_send_ping(&lp->lp_primary_nid, &lp->lp_ping_mdh, bytes, lp,
3551 the_lnet.ln_dc_handler, false);
3552 /* if LNetMDBind in lnet_send_ping fails we need to decrement the
3553 * refcount on the peer, otherwise LNetMDUnlink will be called
3554 * which will eventually do that.
3558 lnet_peer_decref_locked(lp);
3559 lnet_net_unlock(cpt);
3560 rc = -rc; /* change the rc to negative value */
3562 } else if (rc < 0) {
3566 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3568 spin_lock(&lp->lp_lock);
3572 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nidstr(&lp->lp_primary_nid), rc);
3574 * The errors that get us here are considered hard errors and
3575 * cause Discovery to terminate. So we clear PING_SENT, but do
3576 * not set either PING_FAILED or FORCE_PING. In fact we need
3577 * to clear PING_FAILED, because the unlink event handler will
3578 * have set it if we called LNetMDUnlink() above.
3580 spin_lock(&lp->lp_lock);
3581 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3586 * This function exists because you cannot call LNetMDUnlink() from an
3589 static int lnet_peer_push_failed(struct lnet_peer *lp)
3590 __must_hold(&lp->lp_lock)
3592 struct lnet_handle_md mdh;
3595 mdh = lp->lp_push_mdh;
3596 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3597 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3598 rc = lp->lp_push_error;
3599 lp->lp_push_error = 0;
3600 spin_unlock(&lp->lp_lock);
3602 if (!LNetMDHandleIsInvalid(mdh))
3605 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3606 spin_lock(&lp->lp_lock);
3607 return rc ? rc : LNET_REDISCOVER_PEER;
3611 * Mark the peer as discovered.
3613 static int lnet_peer_discovered(struct lnet_peer *lp)
3614 __must_hold(&lp->lp_lock)
3616 lp->lp_state |= LNET_PEER_DISCOVERED;
3617 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3618 LNET_PEER_REDISCOVER);
3620 lp->lp_dc_error = 0;
3622 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3627 /* Active side of push. */
3628 static int lnet_peer_send_push(struct lnet_peer *lp)
3629 __must_hold(&lp->lp_lock)
3631 struct lnet_ping_buffer *pbuf;
3632 struct lnet_processid id;
3637 /* Don't push to a non-multi-rail peer. */
3638 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3639 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3640 /* if peer's NIDs are uptodate then peer is discovered */
3641 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3642 rc = lnet_peer_discovered(lp);
3649 lp->lp_state |= LNET_PEER_PUSH_SENT;
3650 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3651 spin_unlock(&lp->lp_lock);
3653 cpt = lnet_net_lock_current();
3654 pbuf = the_lnet.ln_ping_target;
3655 lnet_ping_buffer_addref(pbuf);
3656 lnet_net_unlock(cpt);
3658 /* Push source MD */
3659 md.start = &pbuf->pb_info;
3660 md.length = pbuf->pb_nbytes;
3661 md.threshold = 2; /* Put/Ack */
3663 md.options = LNET_MD_TRACK_RESPONSE;
3664 md.handler = the_lnet.ln_dc_handler;
3667 rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3669 lnet_ping_buffer_decref(pbuf);
3670 CERROR("Can't bind push source MD: %d\n", rc);
3674 cpt = lnet_net_lock_current();
3675 /* Refcount for MD. */
3676 lnet_peer_addref_locked(lp);
3677 id.pid = LNET_PID_LUSTRE;
3678 if (!LNET_NID_IS_ANY(&lp->lp_disc_dst_nid))
3679 id.nid = lp->lp_disc_dst_nid;
3681 id.nid = lp->lp_primary_nid;
3682 lnet_net_unlock(cpt);
3684 rc = LNetPut(&lp->lp_disc_src_nid, lp->lp_push_mdh,
3685 LNET_ACK_REQ, &id, LNET_RESERVED_PORTAL,
3686 LNET_PROTO_PING_MATCHBITS, 0, 0);
3689 * reset the discovery nid. There is no need to restrict sending
3690 * from that source, if we call lnet_push_update_to_peers(). It'll
3691 * get set to a specific NID, if we initiate discovery from the
3694 lp->lp_disc_src_nid = LNET_ANY_NID;
3695 lp->lp_disc_dst_nid = LNET_ANY_NID;
3700 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3702 spin_lock(&lp->lp_lock);
3706 LNetMDUnlink(lp->lp_push_mdh);
3707 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3709 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nidstr(&lp->lp_primary_nid),
3712 * The errors that get us here are considered hard errors and
3713 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3714 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3715 * because the unlink event handler will have set it if we
3716 * called LNetMDUnlink() above.
3718 spin_lock(&lp->lp_lock);
3719 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3724 * Wait for work to be queued or some other change that must be
3725 * attended to. Returns non-zero if the discovery thread should shut
3728 static int lnet_peer_discovery_wait_for_work(void)
3735 cpt = lnet_net_lock_current();
3737 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3738 TASK_INTERRUPTIBLE);
3739 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3741 if (lnet_push_target_resize_needed() ||
3742 the_lnet.ln_push_target->pb_needs_post)
3744 if (!list_empty(&the_lnet.ln_dc_request))
3746 if (!list_empty(&the_lnet.ln_msg_resend))
3748 lnet_net_unlock(cpt);
3751 * wakeup max every second to check if there are peers that
3752 * have been stuck on the working queue for greater than
3755 schedule_timeout(cfs_time_seconds(1));
3756 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3757 cpt = lnet_net_lock_current();
3759 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3761 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3764 lnet_net_unlock(cpt);
3766 CDEBUG(D_NET, "woken: %d\n", rc);
3772 * Messages that were pending on a destroyed peer will be put on a global
3773 * resend list. The message resend list will be checked by
3774 * the discovery thread when it wakes up, and will resend messages. These
3775 * messages can still be sendable in the case the lpni which was the initial
3776 * cause of the message re-queue was transfered to another peer.
3778 * It is possible that LNet could be shutdown while we're iterating
3779 * through the list. lnet_shudown_lndnets() will attempt to access the
3780 * resend list, but will have to wait until the spinlock is released, by
3781 * which time there shouldn't be any more messages on the resend list.
3782 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3783 * for the messages so they can be released. The other case is that
3784 * lnet_shudown_lndnets() can finalize all the messages before this
3785 * function can visit the resend list, in which case this function will be
3788 static void lnet_resend_msgs(void)
3790 struct lnet_msg *msg, *tmp;
3794 spin_lock(&the_lnet.ln_msg_resend_lock);
3795 list_splice(&the_lnet.ln_msg_resend, &resend);
3796 spin_unlock(&the_lnet.ln_msg_resend_lock);
3798 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3799 list_del_init(&msg->msg_list);
3800 rc = lnet_send(&msg->msg_src_nid_param, msg,
3801 &msg->msg_rtr_nid_param);
3803 CNETERR("Error sending %s to %s: %d\n",
3804 lnet_msgtyp2str(msg->msg_type),
3805 libcfs_idstr(&msg->msg_target), rc);
3806 lnet_finalize(msg, rc);
3811 /* The discovery thread. */
3812 static int lnet_peer_discovery(void *arg)
3814 struct lnet_peer *lp;
3817 wait_for_completion(&the_lnet.ln_started);
3819 CDEBUG(D_NET, "started\n");
3822 if (lnet_peer_discovery_wait_for_work())
3825 if (lnet_push_target_resize_needed())
3826 lnet_push_target_resize();
3827 else if (the_lnet.ln_push_target->pb_needs_post)
3828 lnet_push_target_post(the_lnet.ln_push_target,
3829 &the_lnet.ln_push_target_md);
3833 lnet_net_lock(LNET_LOCK_EX);
3834 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3835 lnet_net_unlock(LNET_LOCK_EX);
3840 * Process all incoming discovery work requests. When
3841 * discovery must wait on a peer to change state, it
3842 * is added to the tail of the ln_dc_working queue. A
3843 * timestamp keeps track of when the peer was added,
3844 * so we can time out discovery requests that take too
3847 while (!list_empty(&the_lnet.ln_dc_request)) {
3848 lp = list_first_entry(&the_lnet.ln_dc_request,
3849 struct lnet_peer, lp_dc_list);
3850 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3852 * set the time the peer was put on the dc_working
3853 * queue. It shouldn't remain on the queue
3854 * forever, in case the GET message (for ping)
3855 * doesn't get a REPLY or the PUT message (for
3856 * push) doesn't get an ACK.
3858 lp->lp_last_queued = ktime_get_real_seconds();
3859 lnet_net_unlock(LNET_LOCK_EX);
3861 if (lnet_push_target_resize_needed())
3862 lnet_push_target_resize();
3863 else if (the_lnet.ln_push_target->pb_needs_post)
3864 lnet_push_target_post(the_lnet.ln_push_target,
3865 &the_lnet.ln_push_target_md);
3868 * Select an action depending on the state of
3869 * the peer and whether discovery is disabled.
3870 * The check whether discovery is disabled is
3871 * done after the code that handles processing
3872 * for arrived data, cleanup for failures, and
3873 * forcing a Ping or Push.
3875 spin_lock(&lp->lp_lock);
3876 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3877 libcfs_nidstr(&lp->lp_primary_nid), lp,
3879 if (lp->lp_state & (LNET_PEER_MARK_DELETION |
3880 LNET_PEER_MARK_DELETED))
3881 rc = lnet_peer_deletion(lp);
3882 else if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3883 rc = lnet_peer_data_present(lp);
3884 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3885 rc = lnet_peer_ping_failed(lp);
3886 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3887 rc = lnet_peer_push_failed(lp);
3888 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3889 rc = lnet_peer_send_ping(lp);
3890 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3891 rc = lnet_peer_send_push(lp);
3892 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3893 rc = lnet_peer_send_ping(lp);
3894 else if (lnet_peer_needs_push(lp))
3895 rc = lnet_peer_send_push(lp);
3897 rc = lnet_peer_discovered(lp);
3898 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3899 libcfs_nidstr(&lp->lp_primary_nid), lp,
3902 if (rc == LNET_REDISCOVER_PEER) {
3903 spin_unlock(&lp->lp_lock);
3904 lnet_net_lock(LNET_LOCK_EX);
3905 list_move(&lp->lp_dc_list,
3906 &the_lnet.ln_dc_request);
3908 !(lp->lp_state & LNET_PEER_DISCOVERING)) {
3909 spin_unlock(&lp->lp_lock);
3910 lnet_net_lock(LNET_LOCK_EX);
3911 lnet_peer_discovery_complete(lp, rc);
3913 spin_unlock(&lp->lp_lock);
3914 lnet_net_lock(LNET_LOCK_EX);
3917 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3922 lnet_net_unlock(LNET_LOCK_EX);
3925 CDEBUG(D_NET, "stopping\n");
3927 * Clean up before telling lnet_peer_discovery_stop() that
3928 * we're done. Use wake_up() below to somewhat reduce the
3929 * size of the thundering herd if there are multiple threads
3930 * waiting on discovery of a single peer.
3933 /* Queue cleanup 1: stop all pending pings and pushes. */
3934 lnet_net_lock(LNET_LOCK_EX);
3935 while (!list_empty(&the_lnet.ln_dc_working)) {
3936 lp = list_first_entry(&the_lnet.ln_dc_working,
3937 struct lnet_peer, lp_dc_list);
3938 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3939 lnet_net_unlock(LNET_LOCK_EX);
3940 lnet_peer_cancel_discovery(lp);
3941 lnet_net_lock(LNET_LOCK_EX);
3943 lnet_net_unlock(LNET_LOCK_EX);
3945 /* Queue cleanup 2: wait for the expired queue to clear. */
3946 while (!list_empty(&the_lnet.ln_dc_expired))
3947 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3949 /* Queue cleanup 3: clear the request queue. */
3950 lnet_net_lock(LNET_LOCK_EX);
3951 while (!list_empty(&the_lnet.ln_dc_request)) {
3952 lp = list_first_entry(&the_lnet.ln_dc_request,
3953 struct lnet_peer, lp_dc_list);
3954 lnet_peer_discovery_complete(lp, -ESHUTDOWN);
3956 lnet_net_unlock(LNET_LOCK_EX);
3958 lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3959 the_lnet.ln_dc_handler = NULL;
3961 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3962 wake_up(&the_lnet.ln_dc_waitq);
3964 CDEBUG(D_NET, "stopped\n");
3969 /* ln_api_mutex is held on entry. */
3970 int lnet_peer_discovery_start(void)
3972 struct task_struct *task;
3975 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3978 the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3979 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3980 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3983 CERROR("Can't start peer discovery thread: %d\n", rc);
3985 the_lnet.ln_dc_handler = NULL;
3987 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3990 CDEBUG(D_NET, "discovery start: %d\n", rc);
3995 /* ln_api_mutex is held on entry. */
3996 void lnet_peer_discovery_stop(void)
3998 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
4001 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
4002 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
4004 /* In the LNetNIInit() path we may be stopping discovery before it
4005 * entered its work loop
4007 if (!completion_done(&the_lnet.ln_started))
4008 complete(&the_lnet.ln_started);
4010 wake_up(&the_lnet.ln_dc_waitq);
4012 mutex_unlock(&the_lnet.ln_api_mutex);
4013 wait_event(the_lnet.ln_dc_waitq,
4014 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
4015 mutex_lock(&the_lnet.ln_api_mutex);
4017 LASSERT(list_empty(&the_lnet.ln_dc_request));
4018 LASSERT(list_empty(&the_lnet.ln_dc_working));
4019 LASSERT(list_empty(&the_lnet.ln_dc_expired));
4021 CDEBUG(D_NET, "discovery stopped\n");
4027 lnet_debug_peer(struct lnet_nid *nid)
4029 char *aliveness = "NA";
4030 struct lnet_peer_ni *lp;
4033 cpt = lnet_nid2cpt(nid, NULL);
4036 lp = lnet_peerni_by_nid_locked(nid, NULL, cpt);
4038 lnet_net_unlock(cpt);
4039 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nidstr(nid));
4043 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
4044 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
4046 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
4047 libcfs_nidstr(&lp->lpni_nid), kref_read(&lp->lpni_kref),
4048 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
4049 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
4050 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
4052 lnet_peer_ni_decref_locked(lp);
4054 lnet_net_unlock(cpt);
4057 /* Gathering information for userspace. */
4059 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
4060 char aliveness[LNET_MAX_STR_LEN],
4061 __u32 *cpt_iter, __u32 *refcount,
4062 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
4063 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
4064 __u32 *peer_tx_qnob)
4066 struct lnet_peer_table *peer_table;
4067 struct lnet_peer_ni *lp;
4072 /* get the number of CPTs */
4073 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
4075 /* if the cpt number to be examined is >= the number of cpts in
4076 * the system then indicate that there are no more cpts to examin
4078 if (*cpt_iter >= lncpt)
4081 /* get the current table */
4082 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
4083 /* if the ptable is NULL then there are no more cpts to examine */
4084 if (peer_table == NULL)
4087 lnet_net_lock(*cpt_iter);
4089 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
4090 struct list_head *peers = &peer_table->pt_hash[j];
4092 list_for_each_entry(lp, peers, lpni_hashlist) {
4093 if (!nid_is_nid4(&lp->lpni_nid))
4095 if (peer_index-- > 0)
4098 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
4099 if (lnet_isrouter(lp) ||
4100 lnet_peer_aliveness_enabled(lp))
4101 snprintf(aliveness, LNET_MAX_STR_LEN,
4102 lnet_is_peer_ni_alive(lp) ? "up" : "down");
4104 *nid = lnet_nid_to_nid4(&lp->lpni_nid);
4105 *refcount = kref_read(&lp->lpni_kref);
4106 *ni_peer_tx_credits =
4107 lp->lpni_net->net_tunables.lct_peer_tx_credits;
4108 *peer_tx_credits = lp->lpni_txcredits;
4109 *peer_rtr_credits = lp->lpni_rtrcredits;
4110 *peer_min_rtr_credits = lp->lpni_mintxcredits;
4111 *peer_tx_qnob = lp->lpni_txqnob;
4117 lnet_net_unlock(*cpt_iter);
4121 return found ? 0 : -ENOENT;
4124 /* ln_api_mutex is held, which keeps the peer list stable */
4125 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
4127 struct lnet_ioctl_element_stats *lpni_stats;
4128 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
4129 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
4130 struct lnet_peer_ni_credit_info *lpni_info;
4131 struct lnet_peer_ni *lpni;
4132 struct lnet_peer *lp;
4134 struct lnet_nid nid;
4138 lnet_nid4_to_nid(cfg->prcfg_prim_nid, &nid);
4139 lp = lnet_find_peer(&nid);
4146 size = sizeof(nid4) + sizeof(*lpni_info) + sizeof(*lpni_stats)
4147 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
4148 size *= lp->lp_nnis;
4149 if (size > cfg->prcfg_size) {
4150 cfg->prcfg_size = size;
4155 cfg->prcfg_prim_nid = lnet_nid_to_nid4(&lp->lp_primary_nid);
4156 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
4157 cfg->prcfg_cfg_nid = lnet_nid_to_nid4(&lp->lp_primary_nid);
4158 cfg->prcfg_count = lp->lp_nnis;
4159 cfg->prcfg_size = size;
4160 cfg->prcfg_state = lp->lp_state;
4162 /* Allocate helper buffers. */
4164 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
4167 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
4170 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
4171 if (!lpni_msg_stats)
4172 goto out_free_stats;
4173 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
4175 goto out_free_msg_stats;
4180 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
4181 if (!nid_is_nid4(&lpni->lpni_nid))
4183 nid4 = lnet_nid_to_nid4(&lpni->lpni_nid);
4184 if (copy_to_user(bulk, &nid4, sizeof(nid4)))
4185 goto out_free_hstats;
4186 bulk += sizeof(nid4);
4188 memset(lpni_info, 0, sizeof(*lpni_info));
4189 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
4190 if (lnet_isrouter(lpni) ||
4191 lnet_peer_aliveness_enabled(lpni))
4192 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
4193 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
4195 lpni_info->cr_refcount = kref_read(&lpni->lpni_kref);
4196 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
4197 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
4198 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
4199 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
4200 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
4201 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
4202 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
4203 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
4204 goto out_free_hstats;
4205 bulk += sizeof(*lpni_info);
4207 memset(lpni_stats, 0, sizeof(*lpni_stats));
4208 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
4209 LNET_STATS_TYPE_SEND);
4210 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
4211 LNET_STATS_TYPE_RECV);
4212 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
4213 LNET_STATS_TYPE_DROP);
4214 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
4215 goto out_free_hstats;
4216 bulk += sizeof(*lpni_stats);
4217 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
4218 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
4219 goto out_free_hstats;
4220 bulk += sizeof(*lpni_msg_stats);
4221 lpni_hstats->hlpni_network_timeout =
4222 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
4223 lpni_hstats->hlpni_remote_dropped =
4224 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
4225 lpni_hstats->hlpni_remote_timeout =
4226 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
4227 lpni_hstats->hlpni_remote_error =
4228 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
4229 lpni_hstats->hlpni_health_value =
4230 atomic_read(&lpni->lpni_healthv);
4231 lpni_hstats->hlpni_ping_count = lpni->lpni_ping_count;
4232 lpni_hstats->hlpni_next_ping = lpni->lpni_next_ping;
4233 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
4234 goto out_free_hstats;
4235 bulk += sizeof(*lpni_hstats);
4240 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
4242 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
4244 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
4246 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
4248 lnet_peer_decref_locked(lp);
4253 /* must hold net_lock/0 */
4255 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni,
4256 struct list_head *recovery_queue,
4259 /* the mt could've shutdown and cleaned up the queues */
4260 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
4263 if (!list_empty(&lpni->lpni_recovery))
4266 if (atomic_read(&lpni->lpni_healthv) == LNET_MAX_HEALTH_VALUE)
4269 if (!lpni->lpni_last_alive) {
4271 "lpni %s(%p) not eligible for recovery last alive %lld\n",
4272 libcfs_nidstr(&lpni->lpni_nid), lpni,
4273 lpni->lpni_last_alive);
4277 if (lnet_recovery_limit &&
4278 now > lpni->lpni_last_alive + lnet_recovery_limit) {
4279 CDEBUG(D_NET, "lpni %s aged out last alive %lld\n",
4280 libcfs_nidstr(&lpni->lpni_nid),
4281 lpni->lpni_last_alive);
4282 /* Reset the ping count so that if this peer NI is added back to
4283 * the recovery queue we will send the first ping right away.
4285 lpni->lpni_ping_count = 0;
4289 /* This peer NI is going on the recovery queue, so take a ref on it */
4290 lnet_peer_ni_addref_locked(lpni);
4292 lnet_peer_ni_set_next_ping(lpni, now);
4294 CDEBUG(D_NET, "%s added to recovery queue. ping count: %u next ping: %lld last alive: %lld health: %d\n",
4295 libcfs_nidstr(&lpni->lpni_nid),
4296 lpni->lpni_ping_count,
4297 lpni->lpni_next_ping,
4298 lpni->lpni_last_alive,
4299 atomic_read(&lpni->lpni_healthv));
4301 list_add_tail(&lpni->lpni_recovery, recovery_queue);
4304 /* Call with the ln_api_mutex held */
4306 lnet_peer_ni_set_healthv(lnet_nid_t nid4, int value, bool all)
4308 struct lnet_peer_table *ptable;
4309 struct lnet_peer *lp;
4310 struct lnet_peer_net *lpn;
4311 struct lnet_peer_ni *lpni;
4312 struct lnet_nid nid;
4317 if (the_lnet.ln_state != LNET_STATE_RUNNING)
4320 lnet_nid4_to_nid(nid4, &nid);
4321 now = ktime_get_seconds();
4324 lnet_net_lock(LNET_LOCK_EX);
4325 lpni = lnet_peer_ni_find_locked(&nid);
4327 lnet_net_unlock(LNET_LOCK_EX);
4330 lnet_set_lpni_healthv_locked(lpni, value);
4331 lnet_peer_ni_add_to_recoveryq_locked(lpni,
4332 &the_lnet.ln_mt_peerNIRecovq, now);
4333 lnet_peer_ni_decref_locked(lpni);
4334 lnet_net_unlock(LNET_LOCK_EX);
4338 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
4341 * Walk all the peers and reset the health value for each one to the
4344 lnet_net_lock(LNET_LOCK_EX);
4345 for (cpt = 0; cpt < lncpt; cpt++) {
4346 ptable = the_lnet.ln_peer_tables[cpt];
4347 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
4348 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
4349 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
4351 lnet_set_lpni_healthv_locked(lpni,
4353 lnet_peer_ni_add_to_recoveryq_locked(lpni,
4354 &the_lnet.ln_mt_peerNIRecovq, now);
4359 lnet_net_unlock(LNET_LOCK_EX);