4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <linux/sched.h>
38 #ifdef HAVE_SCHED_HEADERS
39 #include <linux/sched/signal.h>
41 #include <linux/uaccess.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER (1)
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
54 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
55 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
56 lnet_peer_ni_decref_locked(lpni);
61 lnet_peer_net_added(struct lnet_net *net)
63 struct lnet_peer_ni *lpni, *tmp;
65 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66 lpni_on_remote_peer_ni_list) {
68 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
71 spin_lock(&lpni->lpni_lock);
72 lpni->lpni_txcredits =
73 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
74 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
75 lpni->lpni_rtrcredits =
76 lnet_peer_buffer_credits(lpni->lpni_net);
77 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
78 spin_unlock(&lpni->lpni_lock);
80 lnet_peer_remove_from_remote_list(lpni);
86 lnet_peer_tables_destroy(void)
88 struct lnet_peer_table *ptable;
89 struct list_head *hash;
93 if (!the_lnet.ln_peer_tables)
96 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
97 hash = ptable->pt_hash;
98 if (!hash) /* not intialized */
101 LASSERT(list_empty(&ptable->pt_zombie_list));
103 ptable->pt_hash = NULL;
104 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
105 LASSERT(list_empty(&hash[j]));
107 CFS_FREE_PTR_ARRAY(hash, LNET_PEER_HASH_SIZE);
110 cfs_percpt_free(the_lnet.ln_peer_tables);
111 the_lnet.ln_peer_tables = NULL;
115 lnet_peer_tables_create(void)
117 struct lnet_peer_table *ptable;
118 struct list_head *hash;
122 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
124 if (the_lnet.ln_peer_tables == NULL) {
125 CERROR("Failed to allocate cpu-partition peer tables\n");
129 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
130 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
131 LNET_PEER_HASH_SIZE * sizeof(*hash));
133 CERROR("Failed to create peer hash table\n");
134 lnet_peer_tables_destroy();
138 spin_lock_init(&ptable->pt_zombie_lock);
139 INIT_LIST_HEAD(&ptable->pt_zombie_list);
141 INIT_LIST_HEAD(&ptable->pt_peer_list);
143 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
144 INIT_LIST_HEAD(&hash[j]);
145 ptable->pt_hash = hash; /* sign of initialization */
151 static struct lnet_peer_ni *
152 lnet_peer_ni_alloc(lnet_nid_t nid)
154 struct lnet_peer_ni *lpni;
155 struct lnet_net *net;
158 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
160 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
164 INIT_LIST_HEAD(&lpni->lpni_txq);
165 INIT_LIST_HEAD(&lpni->lpni_hashlist);
166 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
167 INIT_LIST_HEAD(&lpni->lpni_recovery);
168 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
169 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
171 spin_lock_init(&lpni->lpni_lock);
173 if (lnet_peers_start_down())
174 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
176 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
177 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
178 lpni->lpni_nid = nid;
179 lpni->lpni_cpt = cpt;
180 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
182 net = lnet_get_net_locked(LNET_NIDNET(nid));
183 lpni->lpni_net = net;
185 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
186 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
187 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
188 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
191 * This peer_ni is not on a local network, so we
192 * cannot add the credits here. In case the net is
193 * added later, add the peer_ni to the remote peer ni
194 * list so it can be easily found and revisited.
196 /* FIXME: per-net implementation instead? */
197 atomic_inc(&lpni->lpni_refcount);
198 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
199 &the_lnet.ln_remote_peer_ni_list);
202 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
207 static struct lnet_peer_net *
208 lnet_peer_net_alloc(__u32 net_id)
210 struct lnet_peer_net *lpn;
212 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
216 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
217 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
218 lpn->lpn_net_id = net_id;
220 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
226 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
228 struct lnet_peer *lp;
230 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
232 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
233 LASSERT(list_empty(&lpn->lpn_peer_nis));
234 LASSERT(list_empty(&lpn->lpn_peer_nets));
236 lpn->lpn_peer = NULL;
237 LIBCFS_FREE(lpn, sizeof(*lpn));
239 lnet_peer_decref_locked(lp);
242 static struct lnet_peer *
243 lnet_peer_alloc(lnet_nid_t nid)
245 struct lnet_peer *lp;
247 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
251 INIT_LIST_HEAD(&lp->lp_rtrq);
252 INIT_LIST_HEAD(&lp->lp_routes);
253 INIT_LIST_HEAD(&lp->lp_peer_list);
254 INIT_LIST_HEAD(&lp->lp_peer_nets);
255 INIT_LIST_HEAD(&lp->lp_dc_list);
256 INIT_LIST_HEAD(&lp->lp_dc_pendq);
257 INIT_LIST_HEAD(&lp->lp_rtr_list);
258 init_waitqueue_head(&lp->lp_dc_waitq);
259 spin_lock_init(&lp->lp_lock);
260 lp->lp_primary_nid = nid;
261 if (lnet_peers_start_down())
262 lp->lp_alive = false;
267 * all peers created on a router should have health on
268 * if it's not already on.
270 if (the_lnet.ln_routing && !lnet_health_sensitivity)
271 lp->lp_health_sensitivity = 1;
274 * Turn off discovery for loopback peer. If you're creating a peer
275 * for the loopback interface then that was initiated when we
276 * attempted to send a message over the loopback. There is no need
277 * to ever use a different interface when sending messages to
280 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
281 lp->lp_state = LNET_PEER_NO_DISCOVERY;
282 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
284 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
290 lnet_destroy_peer_locked(struct lnet_peer *lp)
292 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
294 LASSERT(atomic_read(&lp->lp_refcount) == 0);
295 LASSERT(lp->lp_rtr_refcount == 0);
296 LASSERT(list_empty(&lp->lp_peer_nets));
297 LASSERT(list_empty(&lp->lp_peer_list));
298 LASSERT(list_empty(&lp->lp_dc_list));
301 lnet_ping_buffer_decref(lp->lp_data);
304 * if there are messages still on the pending queue, then make
305 * sure to queue them on the ln_msg_resend list so they can be
306 * resent at a later point if the discovery thread is still
308 * If the discovery thread has stopped, then the wakeup will be a
309 * no-op, and it is expected the lnet_shutdown_lndnets() will
310 * eventually be called, which will traverse this list and
311 * finalize the messages on the list.
312 * We can not resend them now because we're holding the cpt lock.
313 * Releasing the lock can cause an inconsistent state
315 spin_lock(&the_lnet.ln_msg_resend_lock);
316 spin_lock(&lp->lp_lock);
317 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
318 spin_unlock(&lp->lp_lock);
319 spin_unlock(&the_lnet.ln_msg_resend_lock);
320 wake_up(&the_lnet.ln_dc_waitq);
322 LIBCFS_FREE(lp, sizeof(*lp));
326 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
327 * that peer_net, detach the peer_net from the peer.
329 * Call with lnet_net_lock/EX held
332 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
334 struct lnet_peer_table *ptable;
335 struct lnet_peer_net *lpn;
336 struct lnet_peer *lp;
339 * Belts and suspenders: gracefully handle teardown of a
340 * partially connected peer_ni.
342 lpn = lpni->lpni_peer_net;
344 list_del_init(&lpni->lpni_peer_nis);
346 * If there are no lpni's left, we detach lpn from
347 * lp_peer_nets, so it cannot be found anymore.
349 if (list_empty(&lpn->lpn_peer_nis))
350 list_del_init(&lpn->lpn_peer_nets);
352 /* Update peer NID count. */
357 * If there are no more peer nets, make the peer unfindable
358 * via the peer_tables.
360 * Otherwise, if the peer is DISCOVERED, tell discovery to
361 * take another look at it. This is a no-op if discovery for
362 * this peer did the detaching.
364 if (list_empty(&lp->lp_peer_nets)) {
365 list_del_init(&lp->lp_peer_list);
366 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
368 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
369 /* Discovery isn't running, nothing to do here. */
370 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
371 lnet_peer_queue_for_discovery(lp);
372 wake_up(&the_lnet.ln_dc_waitq);
374 CDEBUG(D_NET, "peer %s NID %s\n",
375 libcfs_nid2str(lp->lp_primary_nid),
376 libcfs_nid2str(lpni->lpni_nid));
379 /* called with lnet_net_lock LNET_LOCK_EX held */
381 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
383 struct lnet_peer_table *ptable = NULL;
385 /* don't remove a peer_ni if it's also a gateway */
386 if (lnet_isrouter(lpni) && !force) {
387 CERROR("Peer NI %s is a gateway. Can not delete it\n",
388 libcfs_nid2str(lpni->lpni_nid));
392 lnet_peer_remove_from_remote_list(lpni);
394 /* remove peer ni from the hash list. */
395 list_del_init(&lpni->lpni_hashlist);
398 * indicate the peer is being deleted so the monitor thread can
399 * remove it from the recovery queue.
401 spin_lock(&lpni->lpni_lock);
402 lpni->lpni_state |= LNET_PEER_NI_DELETING;
403 spin_unlock(&lpni->lpni_lock);
405 /* decrement the ref count on the peer table */
406 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
409 * The peer_ni can no longer be found with a lookup. But there
410 * can be current users, so keep track of it on the zombie
411 * list until the reference count has gone to zero.
413 * The last reference may be lost in a place where the
414 * lnet_net_lock locks only a single cpt, and that cpt may not
415 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
418 spin_lock(&ptable->pt_zombie_lock);
419 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
420 ptable->pt_zombies++;
421 spin_unlock(&ptable->pt_zombie_lock);
423 /* no need to keep this peer_ni on the hierarchy anymore */
424 lnet_peer_detach_peer_ni_locked(lpni);
426 /* remove hashlist reference on peer_ni */
427 lnet_peer_ni_decref_locked(lpni);
432 void lnet_peer_uninit(void)
434 struct lnet_peer_ni *lpni, *tmp;
436 lnet_net_lock(LNET_LOCK_EX);
438 /* remove all peer_nis from the remote peer and the hash list */
439 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
440 lpni_on_remote_peer_ni_list)
441 lnet_peer_ni_del_locked(lpni, false);
443 lnet_peer_tables_destroy();
445 lnet_net_unlock(LNET_LOCK_EX);
449 lnet_peer_del_locked(struct lnet_peer *peer)
451 struct lnet_peer_ni *lpni = NULL, *lpni2;
454 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
456 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
457 while (lpni != NULL) {
458 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
459 rc = lnet_peer_ni_del_locked(lpni, false);
469 lnet_peer_del(struct lnet_peer *peer)
471 lnet_net_lock(LNET_LOCK_EX);
472 lnet_peer_del_locked(peer);
473 lnet_net_unlock(LNET_LOCK_EX);
479 * Delete a NID from a peer. Call with ln_api_mutex held.
482 * -EPERM: Non-DLC deletion from DLC-configured peer.
483 * -ENOENT: No lnet_peer_ni corresponding to the nid.
484 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
485 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
488 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
490 struct lnet_peer_ni *lpni;
491 lnet_nid_t primary_nid = lp->lp_primary_nid;
493 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
495 if (!(flags & LNET_PEER_CONFIGURED)) {
496 if (lp->lp_state & LNET_PEER_CONFIGURED) {
501 lpni = lnet_find_peer_ni_locked(nid);
506 lnet_peer_ni_decref_locked(lpni);
507 if (lp != lpni->lpni_peer_net->lpn_peer) {
513 * This function only allows deletion of the primary NID if it
516 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
521 lnet_net_lock(LNET_LOCK_EX);
523 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
524 struct lnet_peer_ni *lpni2;
525 /* assign the next peer_ni to be the primary */
526 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
528 lp->lp_primary_nid = lpni->lpni_nid;
530 rc = lnet_peer_ni_del_locked(lpni, force);
532 lnet_net_unlock(LNET_LOCK_EX);
535 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
536 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
542 lnet_peer_table_cleanup_locked(struct lnet_net *net,
543 struct lnet_peer_table *ptable)
546 struct lnet_peer_ni *next;
547 struct lnet_peer_ni *lpni;
548 struct lnet_peer *peer;
550 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
551 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
553 if (net != NULL && net != lpni->lpni_net)
556 peer = lpni->lpni_peer_net->lpn_peer;
557 if (peer->lp_primary_nid != lpni->lpni_nid) {
558 lnet_peer_ni_del_locked(lpni, false);
562 * Removing the primary NID implies removing
563 * the entire peer. Advance next beyond any
564 * peer_ni that belongs to the same peer.
566 list_for_each_entry_from(next, &ptable->pt_hash[i],
568 if (next->lpni_peer_net->lpn_peer != peer)
571 lnet_peer_del_locked(peer);
577 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
581 spin_lock(&ptable->pt_zombie_lock);
582 while (ptable->pt_zombies) {
583 spin_unlock(&ptable->pt_zombie_lock);
585 if (is_power_of_2(i)) {
587 "Waiting for %d zombies on peer table\n",
590 schedule_timeout_uninterruptible(cfs_time_seconds(1) >> 1);
591 spin_lock(&ptable->pt_zombie_lock);
593 spin_unlock(&ptable->pt_zombie_lock);
597 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
598 struct lnet_peer_table *ptable)
600 struct lnet_peer_ni *lp;
601 struct lnet_peer_ni *tmp;
605 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
606 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
608 if (net != lp->lpni_net)
611 if (!lnet_isrouter(lp))
614 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
616 lnet_net_unlock(LNET_LOCK_EX);
617 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
618 lnet_net_lock(LNET_LOCK_EX);
624 lnet_peer_tables_cleanup(struct lnet_net *net)
627 struct lnet_peer_table *ptable;
629 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
630 /* If just deleting the peers for a NI, get rid of any routes these
631 * peers are gateways for. */
632 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
633 lnet_net_lock(LNET_LOCK_EX);
634 lnet_peer_table_del_rtrs_locked(net, ptable);
635 lnet_net_unlock(LNET_LOCK_EX);
638 /* Start the cleanup process */
639 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
640 lnet_net_lock(LNET_LOCK_EX);
641 lnet_peer_table_cleanup_locked(net, ptable);
642 lnet_net_unlock(LNET_LOCK_EX);
645 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
646 lnet_peer_ni_finalize_wait(ptable);
649 static struct lnet_peer_ni *
650 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
652 struct list_head *peers;
653 struct lnet_peer_ni *lp;
655 if (the_lnet.ln_state != LNET_STATE_RUNNING)
658 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
659 list_for_each_entry(lp, peers, lpni_hashlist) {
660 if (lp->lpni_nid == nid) {
661 lnet_peer_ni_addref_locked(lp);
669 struct lnet_peer_ni *
670 lnet_find_peer_ni_locked(lnet_nid_t nid)
672 struct lnet_peer_ni *lpni;
673 struct lnet_peer_table *ptable;
676 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
678 ptable = the_lnet.ln_peer_tables[cpt];
679 lpni = lnet_get_peer_ni_locked(ptable, nid);
684 struct lnet_peer_ni *
685 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
687 struct lnet_peer_net *lpn;
688 struct lnet_peer_ni *lpni;
690 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
694 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
695 if (lpni->lpni_nid == nid)
703 lnet_find_peer(lnet_nid_t nid)
705 struct lnet_peer_ni *lpni;
706 struct lnet_peer *lp = NULL;
709 cpt = lnet_net_lock_current();
710 lpni = lnet_find_peer_ni_locked(nid);
712 lp = lpni->lpni_peer_net->lpn_peer;
713 lnet_peer_addref_locked(lp);
714 lnet_peer_ni_decref_locked(lpni);
716 lnet_net_unlock(cpt);
721 struct lnet_peer_net *
722 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
724 struct lnet_peer_net *net;
727 /* no net id provided return the first net */
728 net = list_first_entry_or_null(&lp->lp_peer_nets,
729 struct lnet_peer_net,
735 /* find the net after the one provided */
736 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
737 if (net->lpn_net_id == prev_lpn_id) {
739 * if we reached the end of the list loop to the
742 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
743 return list_first_entry_or_null(&lp->lp_peer_nets,
744 struct lnet_peer_net,
747 return list_next_entry(net, lpn_peer_nets);
754 struct lnet_peer_ni *
755 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
756 struct lnet_peer_net *peer_net,
757 struct lnet_peer_ni *prev)
759 struct lnet_peer_ni *lpni;
760 struct lnet_peer_net *net = peer_net;
764 if (list_empty(&peer->lp_peer_nets))
767 net = list_entry(peer->lp_peer_nets.next,
768 struct lnet_peer_net,
771 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
777 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
779 * if you reached the end of the peer ni list and the peer
780 * net is specified then there are no more peer nis in that
787 * we reached the end of this net ni list. move to the
790 if (prev->lpni_peer_net->lpn_peer_nets.next ==
792 /* no more nets and no more NIs. */
795 /* get the next net */
796 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
797 struct lnet_peer_net,
799 /* get the ni on it */
800 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
806 /* there are more nis left */
807 lpni = list_entry(prev->lpni_peer_nis.next,
808 struct lnet_peer_ni, lpni_peer_nis);
813 /* Call with the ln_api_mutex held */
814 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
816 struct lnet_process_id id;
817 struct lnet_peer_table *ptable;
818 struct lnet_peer *lp;
827 if (the_lnet.ln_state != LNET_STATE_RUNNING)
830 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
833 * Count the number of peers, and return E2BIG if the buffer
834 * is too small. We'll also return the desired size.
837 for (cpt = 0; cpt < lncpt; cpt++) {
838 ptable = the_lnet.ln_peer_tables[cpt];
839 count += ptable->pt_peers;
841 size = count * sizeof(*ids);
846 * Walk the peer lists and copy out the primary nids.
847 * This is safe because the peer lists are only modified
848 * while the ln_api_mutex is held. So we don't need to
849 * hold the lnet_net_lock as well, and can therefore
850 * directly call copy_to_user().
853 memset(&id, 0, sizeof(id));
854 id.pid = LNET_PID_LUSTRE;
856 for (cpt = 0; cpt < lncpt; cpt++) {
857 ptable = the_lnet.ln_peer_tables[cpt];
858 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
861 id.nid = lp->lp_primary_nid;
862 if (copy_to_user(&ids[i], &id, sizeof(id)))
875 * Start pushes to peers that need to be updated for a configuration
876 * change on this node.
879 lnet_push_update_to_peers(int force)
881 struct lnet_peer_table *ptable;
882 struct lnet_peer *lp;
886 lnet_net_lock(LNET_LOCK_EX);
887 if (lnet_peer_discovery_disabled)
889 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
890 for (cpt = 0; cpt < lncpt; cpt++) {
891 ptable = the_lnet.ln_peer_tables[cpt];
892 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
894 spin_lock(&lp->lp_lock);
895 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
896 lp->lp_state |= LNET_PEER_FORCE_PUSH;
897 spin_unlock(&lp->lp_lock);
899 if (lnet_peer_needs_push(lp))
900 lnet_peer_queue_for_discovery(lp);
903 lnet_net_unlock(LNET_LOCK_EX);
904 wake_up(&the_lnet.ln_dc_waitq);
908 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
909 * this is a preferred point-to-point path. Call with lnet_net_lock in
913 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
917 if (lpni->lpni_pref_nnids == 0)
919 if (lpni->lpni_pref_nnids == 1)
920 return lpni->lpni_pref.nid == nid;
921 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
922 if (lpni->lpni_pref.nids[i] == nid)
929 * Set a single ni as preferred, provided no preferred ni is already
930 * defined. Only to be used for non-multi-rail peer_ni.
933 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
937 spin_lock(&lpni->lpni_lock);
938 if (nid == LNET_NID_ANY) {
940 } else if (lpni->lpni_pref_nnids > 0) {
942 } else if (lpni->lpni_pref_nnids == 0) {
943 lpni->lpni_pref.nid = nid;
944 lpni->lpni_pref_nnids = 1;
945 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
947 spin_unlock(&lpni->lpni_lock);
949 CDEBUG(D_NET, "peer %s nid %s: %d\n",
950 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
955 * Clear the preferred NID from a non-multi-rail peer_ni, provided
956 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
959 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
963 spin_lock(&lpni->lpni_lock);
964 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
965 lpni->lpni_pref_nnids = 0;
966 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
967 } else if (lpni->lpni_pref_nnids == 0) {
972 spin_unlock(&lpni->lpni_lock);
974 CDEBUG(D_NET, "peer %s: %d\n",
975 libcfs_nid2str(lpni->lpni_nid), rc);
980 * Clear the preferred NIDs from a non-multi-rail peer.
983 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
985 struct lnet_peer_ni *lpni = NULL;
987 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
988 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
992 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
994 lnet_nid_t *nids = NULL;
995 lnet_nid_t *oldnids = NULL;
996 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1001 if (nid == LNET_NID_ANY) {
1006 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1011 /* A non-MR node may have only one preferred NI per peer_ni */
1012 if (lpni->lpni_pref_nnids > 0) {
1013 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1019 if (lpni->lpni_pref_nnids != 0) {
1020 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1021 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1026 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
1027 if (lpni->lpni_pref.nids[i] == nid) {
1028 LIBCFS_FREE(nids, size);
1032 nids[i] = lpni->lpni_pref.nids[i];
1037 lnet_net_lock(LNET_LOCK_EX);
1038 spin_lock(&lpni->lpni_lock);
1039 if (lpni->lpni_pref_nnids == 0) {
1040 lpni->lpni_pref.nid = nid;
1042 oldnids = lpni->lpni_pref.nids;
1043 lpni->lpni_pref.nids = nids;
1045 lpni->lpni_pref_nnids++;
1046 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1047 spin_unlock(&lpni->lpni_lock);
1048 lnet_net_unlock(LNET_LOCK_EX);
1051 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1052 CFS_FREE_PTR_ARRAY(oldnids, size);
1055 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1056 spin_lock(&lpni->lpni_lock);
1057 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1058 spin_unlock(&lpni->lpni_lock);
1060 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1061 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1066 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1068 lnet_nid_t *nids = NULL;
1069 lnet_nid_t *oldnids = NULL;
1070 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1075 if (lpni->lpni_pref_nnids == 0) {
1080 if (lpni->lpni_pref_nnids == 1) {
1081 if (lpni->lpni_pref.nid != nid) {
1085 } else if (lpni->lpni_pref_nnids == 2) {
1086 if (lpni->lpni_pref.nids[0] != nid &&
1087 lpni->lpni_pref.nids[1] != nid) {
1092 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1093 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1098 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1099 if (lpni->lpni_pref.nids[i] != nid)
1101 nids[j++] = lpni->lpni_pref.nids[i];
1103 /* Check if we actually removed a nid. */
1104 if (j == lpni->lpni_pref_nnids) {
1105 LIBCFS_FREE(nids, size);
1111 lnet_net_lock(LNET_LOCK_EX);
1112 spin_lock(&lpni->lpni_lock);
1113 if (lpni->lpni_pref_nnids == 1) {
1114 lpni->lpni_pref.nid = LNET_NID_ANY;
1115 } else if (lpni->lpni_pref_nnids == 2) {
1116 oldnids = lpni->lpni_pref.nids;
1117 if (oldnids[0] == nid)
1118 lpni->lpni_pref.nid = oldnids[1];
1120 lpni->lpni_pref.nid = oldnids[2];
1122 oldnids = lpni->lpni_pref.nids;
1123 lpni->lpni_pref.nids = nids;
1125 lpni->lpni_pref_nnids--;
1126 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1127 spin_unlock(&lpni->lpni_lock);
1128 lnet_net_unlock(LNET_LOCK_EX);
1131 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1132 CFS_FREE_PTR_ARRAY(oldnids, size);
1135 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1136 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1141 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1143 struct lnet_peer_ni *lpni;
1144 lnet_nid_t primary_nid = nid;
1146 lpni = lnet_find_peer_ni_locked(nid);
1148 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1149 lnet_peer_ni_decref_locked(lpni);
1156 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1157 __must_hold(&lp->lp_lock)
1159 if (lnet_peer_discovery_disabled)
1162 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1163 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1174 lnet_is_discovery_disabled(struct lnet_peer *lp)
1178 spin_lock(&lp->lp_lock);
1179 rc = lnet_is_discovery_disabled_locked(lp);
1180 spin_unlock(&lp->lp_lock);
1186 LNetPrimaryNID(lnet_nid_t nid)
1188 struct lnet_peer *lp;
1189 struct lnet_peer_ni *lpni;
1190 lnet_nid_t primary_nid = nid;
1194 cpt = lnet_net_lock_current();
1195 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1200 lp = lpni->lpni_peer_net->lpn_peer;
1202 while (!lnet_peer_is_uptodate(lp)) {
1203 rc = lnet_discover_peer_locked(lpni, cpt, true);
1206 lp = lpni->lpni_peer_net->lpn_peer;
1208 /* Only try once if discovery is disabled */
1209 if (lnet_is_discovery_disabled(lp))
1212 primary_nid = lp->lp_primary_nid;
1214 lnet_peer_ni_decref_locked(lpni);
1216 lnet_net_unlock(cpt);
1218 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1219 libcfs_nid2str(primary_nid), rc);
1222 EXPORT_SYMBOL(LNetPrimaryNID);
1224 struct lnet_peer_net *
1225 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1227 struct lnet_peer_net *peer_net;
1228 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1229 if (peer_net->lpn_net_id == net_id)
1236 * Attach a peer_ni to a peer_net and peer. This function assumes
1237 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1238 * may be attached to a different peer, in which case it will be
1239 * properly detached first. The whole operation is done atomically.
1241 * Always returns 0. This is the last function called from functions
1242 * that do return an int, so returning 0 here allows the compiler to
1246 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1247 struct lnet_peer_net *lpn,
1248 struct lnet_peer_ni *lpni,
1251 struct lnet_peer_table *ptable;
1253 /* Install the new peer_ni */
1254 lnet_net_lock(LNET_LOCK_EX);
1255 /* Add peer_ni to global peer table hash, if necessary. */
1256 if (list_empty(&lpni->lpni_hashlist)) {
1257 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1259 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1260 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1261 ptable->pt_version++;
1262 /* This is the 1st refcount on lpni. */
1263 atomic_inc(&lpni->lpni_refcount);
1266 /* Detach the peer_ni from an existing peer, if necessary. */
1267 if (lpni->lpni_peer_net) {
1268 LASSERT(lpni->lpni_peer_net != lpn);
1269 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1270 lnet_peer_detach_peer_ni_locked(lpni);
1271 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1272 lpni->lpni_peer_net = NULL;
1275 /* Add peer_ni to peer_net */
1276 lpni->lpni_peer_net = lpn;
1277 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1278 lnet_update_peer_net_healthv(lpni);
1279 lnet_peer_net_addref_locked(lpn);
1281 /* Add peer_net to peer */
1282 if (!lpn->lpn_peer) {
1284 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1285 lnet_peer_addref_locked(lp);
1288 /* Add peer to global peer list, if necessary */
1289 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1290 if (list_empty(&lp->lp_peer_list)) {
1291 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1296 /* Update peer state */
1297 spin_lock(&lp->lp_lock);
1298 if (flags & LNET_PEER_CONFIGURED) {
1299 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1300 lp->lp_state |= LNET_PEER_CONFIGURED;
1302 if (flags & LNET_PEER_MULTI_RAIL) {
1303 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1304 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1305 lnet_peer_clr_non_mr_pref_nids(lp);
1308 spin_unlock(&lp->lp_lock);
1311 lnet_net_unlock(LNET_LOCK_EX);
1313 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1314 libcfs_nid2str(lp->lp_primary_nid),
1315 libcfs_nid2str(lpni->lpni_nid), flags);
1321 * Create a new peer, with nid as its primary nid.
1323 * Call with the lnet_api_mutex held.
1326 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1328 struct lnet_peer *lp;
1329 struct lnet_peer_net *lpn;
1330 struct lnet_peer_ni *lpni;
1333 LASSERT(nid != LNET_NID_ANY);
1336 * No need for the lnet_net_lock here, because the
1337 * lnet_api_mutex is held.
1339 lpni = lnet_find_peer_ni_locked(nid);
1341 /* A peer with this NID already exists. */
1342 lp = lpni->lpni_peer_net->lpn_peer;
1343 lnet_peer_ni_decref_locked(lpni);
1345 * This is an error if the peer was configured and the
1346 * primary NID differs or an attempt is made to change
1347 * the Multi-Rail flag. Otherwise the assumption is
1348 * that an existing peer is being modified.
1350 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1351 if (lp->lp_primary_nid != nid)
1353 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1357 /* Delete and recreate as a configured peer. */
1361 /* Create peer, peer_net, and peer_ni. */
1363 lp = lnet_peer_alloc(nid);
1366 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1369 lpni = lnet_peer_ni_alloc(nid);
1373 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1376 LIBCFS_FREE(lpn, sizeof(*lpn));
1378 LIBCFS_FREE(lp, sizeof(*lp));
1380 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1381 libcfs_nid2str(nid), flags, rc);
1386 * Add a NID to a peer. Call with ln_api_mutex held.
1389 * -EPERM: Non-DLC addition to a DLC-configured peer.
1390 * -EEXIST: The NID was configured by DLC for a different peer.
1391 * -ENOMEM: Out of memory.
1392 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1393 * non-multi-rail peer.
1396 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1398 struct lnet_peer_net *lpn;
1399 struct lnet_peer_ni *lpni;
1403 LASSERT(nid != LNET_NID_ANY);
1405 /* A configured peer can only be updated through configuration. */
1406 if (!(flags & LNET_PEER_CONFIGURED)) {
1407 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1414 * The MULTI_RAIL flag can be set but not cleared, because
1415 * that would leave the peer struct in an invalid state.
1417 if (flags & LNET_PEER_MULTI_RAIL) {
1418 spin_lock(&lp->lp_lock);
1419 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1420 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1421 lnet_peer_clr_non_mr_pref_nids(lp);
1423 spin_unlock(&lp->lp_lock);
1424 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1429 lpni = lnet_find_peer_ni_locked(nid);
1432 * A peer_ni already exists. This is only a problem if
1433 * it is not connected to this peer and was configured
1436 lnet_peer_ni_decref_locked(lpni);
1437 if (lpni->lpni_peer_net->lpn_peer == lp)
1439 if (lnet_peer_ni_is_configured(lpni)) {
1443 /* If this is the primary NID, destroy the peer. */
1444 if (lnet_peer_ni_is_primary(lpni)) {
1445 struct lnet_peer *rtr_lp =
1446 lpni->lpni_peer_net->lpn_peer;
1447 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1449 * if we're trying to delete a router it means
1450 * we're moving this peer NI to a new peer so must
1451 * transfer router properties to the new peer
1453 if (rtr_refcount > 0) {
1454 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1455 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1457 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1458 lpni = lnet_peer_ni_alloc(nid);
1465 lpni = lnet_peer_ni_alloc(nid);
1473 * Get the peer_net. Check that we're not adding a second
1474 * peer_ni on a peer_net of a non-multi-rail peer.
1476 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1478 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1483 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1488 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1491 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1492 if (!lpni->lpni_peer_net)
1493 LIBCFS_FREE(lpni, sizeof(*lpni));
1495 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1496 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1502 * Update the primary NID of a peer, if possible.
1504 * Call with the lnet_api_mutex held.
1507 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1509 lnet_nid_t old = lp->lp_primary_nid;
1512 if (lp->lp_primary_nid == nid)
1514 rc = lnet_peer_add_nid(lp, nid, flags);
1517 lp->lp_primary_nid = nid;
1519 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1520 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1525 * lpni creation initiated due to traffic either sending or receiving.
1528 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1530 struct lnet_peer *lp;
1531 struct lnet_peer_net *lpn;
1532 struct lnet_peer_ni *lpni;
1536 if (nid == LNET_NID_ANY) {
1541 /* lnet_net_lock is not needed here because ln_api_lock is held */
1542 lpni = lnet_find_peer_ni_locked(nid);
1545 * We must have raced with another thread. Since we
1546 * know next to nothing about a peer_ni created by
1547 * traffic, we just assume everything is ok and
1550 lnet_peer_ni_decref_locked(lpni);
1554 /* Create peer, peer_net, and peer_ni. */
1556 lp = lnet_peer_alloc(nid);
1559 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1562 lpni = lnet_peer_ni_alloc(nid);
1565 if (pref != LNET_NID_ANY)
1566 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1568 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1571 LIBCFS_FREE(lpn, sizeof(*lpn));
1573 LIBCFS_FREE(lp, sizeof(*lp));
1575 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1580 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1582 * This API handles the following combinations:
1583 * Create a peer with its primary NI if only the prim_nid is provided
1584 * Add a NID to a peer identified by the prim_nid. The peer identified
1585 * by the prim_nid must already exist.
1586 * The peer being created may be non-MR.
1588 * The caller must hold ln_api_mutex. This prevents the peer from
1589 * being created/modified/deleted by a different thread.
1592 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1594 struct lnet_peer *lp = NULL;
1595 struct lnet_peer_ni *lpni;
1598 /* The prim_nid must always be specified */
1599 if (prim_nid == LNET_NID_ANY)
1602 flags = LNET_PEER_CONFIGURED;
1604 flags |= LNET_PEER_MULTI_RAIL;
1607 * If nid isn't specified, we must create a new peer with
1608 * prim_nid as its primary nid.
1610 if (nid == LNET_NID_ANY)
1611 return lnet_peer_add(prim_nid, flags);
1613 /* Look up the prim_nid, which must exist. */
1614 lpni = lnet_find_peer_ni_locked(prim_nid);
1617 lnet_peer_ni_decref_locked(lpni);
1618 lp = lpni->lpni_peer_net->lpn_peer;
1620 /* Peer must have been configured. */
1621 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1622 CDEBUG(D_NET, "peer %s was not configured\n",
1623 libcfs_nid2str(prim_nid));
1627 /* Primary NID must match */
1628 if (lp->lp_primary_nid != prim_nid) {
1629 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1630 libcfs_nid2str(prim_nid),
1631 libcfs_nid2str(lp->lp_primary_nid));
1635 /* Multi-Rail flag must match. */
1636 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1637 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1638 libcfs_nid2str(prim_nid));
1642 return lnet_peer_add_nid(lp, nid, flags);
1646 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1648 * This API handles the following combinations:
1649 * Delete a NI from a peer if both prim_nid and nid are provided.
1650 * Delete a peer if only prim_nid is provided.
1651 * Delete a peer if its primary nid is provided.
1653 * The caller must hold ln_api_mutex. This prevents the peer from
1654 * being modified/deleted by a different thread.
1657 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1659 struct lnet_peer *lp;
1660 struct lnet_peer_ni *lpni;
1663 if (prim_nid == LNET_NID_ANY)
1666 lpni = lnet_find_peer_ni_locked(prim_nid);
1669 lnet_peer_ni_decref_locked(lpni);
1670 lp = lpni->lpni_peer_net->lpn_peer;
1672 if (prim_nid != lp->lp_primary_nid) {
1673 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1674 libcfs_nid2str(prim_nid),
1675 libcfs_nid2str(lp->lp_primary_nid));
1679 lnet_net_lock(LNET_LOCK_EX);
1680 if (lp->lp_rtr_refcount > 0) {
1681 lnet_net_unlock(LNET_LOCK_EX);
1682 CERROR("%s is a router. Can not be deleted\n",
1683 libcfs_nid2str(prim_nid));
1686 lnet_net_unlock(LNET_LOCK_EX);
1688 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1689 return lnet_peer_del(lp);
1691 flags = LNET_PEER_CONFIGURED;
1692 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1693 flags |= LNET_PEER_MULTI_RAIL;
1695 return lnet_peer_del_nid(lp, nid, flags);
1699 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1701 struct lnet_peer_table *ptable;
1702 struct lnet_peer_net *lpn;
1704 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1706 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1707 LASSERT(list_empty(&lpni->lpni_txq));
1708 LASSERT(lpni->lpni_txqnob == 0);
1709 LASSERT(list_empty(&lpni->lpni_peer_nis));
1710 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1712 lpn = lpni->lpni_peer_net;
1713 lpni->lpni_peer_net = NULL;
1714 lpni->lpni_net = NULL;
1716 /* remove the peer ni from the zombie list */
1717 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1718 spin_lock(&ptable->pt_zombie_lock);
1719 list_del_init(&lpni->lpni_hashlist);
1720 ptable->pt_zombies--;
1721 spin_unlock(&ptable->pt_zombie_lock);
1723 if (lpni->lpni_pref_nnids > 1)
1724 CFS_FREE_PTR_ARRAY(lpni->lpni_pref.nids, lpni->lpni_pref_nnids);
1726 LIBCFS_FREE(lpni, sizeof(*lpni));
1728 lnet_peer_net_decref_locked(lpn);
1731 struct lnet_peer_ni *
1732 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1734 struct lnet_peer_ni *lpni = NULL;
1737 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1738 return ERR_PTR(-ESHUTDOWN);
1741 * find if a peer_ni already exists.
1742 * If so then just return that.
1744 lpni = lnet_find_peer_ni_locked(nid);
1748 lnet_net_unlock(cpt);
1750 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1753 goto out_net_relock;
1756 lpni = lnet_find_peer_ni_locked(nid);
1766 * Get a peer_ni for the given nid, create it if necessary. Takes a
1767 * hold on the peer_ni.
1769 struct lnet_peer_ni *
1770 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1772 struct lnet_peer_ni *lpni = NULL;
1775 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1776 return ERR_PTR(-ESHUTDOWN);
1779 * find if a peer_ni already exists.
1780 * If so then just return that.
1782 lpni = lnet_find_peer_ni_locked(nid);
1788 * use the lnet_api_mutex to serialize the creation of the peer_ni
1789 * and the creation/deletion of the local ni/net. When a local ni is
1790 * created, if there exists a set of peer_nis on that network,
1791 * they need to be traversed and updated. When a local NI is
1792 * deleted, which could result in a network being deleted, then
1793 * all peer nis on that network need to be removed as well.
1795 * Creation through traffic should also be serialized with
1796 * creation through DLC.
1798 lnet_net_unlock(cpt);
1799 mutex_lock(&the_lnet.ln_api_mutex);
1801 * Shutdown is only set under the ln_api_lock, so a single
1802 * check here is sufficent.
1804 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1805 lpni = ERR_PTR(-ESHUTDOWN);
1806 goto out_mutex_unlock;
1809 rc = lnet_peer_ni_traffic_add(nid, pref);
1812 goto out_mutex_unlock;
1815 lpni = lnet_find_peer_ni_locked(nid);
1819 mutex_unlock(&the_lnet.ln_api_mutex);
1822 /* Lock has been dropped, check again for shutdown. */
1823 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1825 lnet_peer_ni_decref_locked(lpni);
1826 lpni = ERR_PTR(-ESHUTDOWN);
1833 lnet_peer_gw_discovery(struct lnet_peer *lp)
1837 spin_lock(&lp->lp_lock);
1838 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1840 spin_unlock(&lp->lp_lock);
1846 lnet_peer_is_uptodate(struct lnet_peer *lp)
1850 spin_lock(&lp->lp_lock);
1851 rc = lnet_peer_is_uptodate_locked(lp);
1852 spin_unlock(&lp->lp_lock);
1857 * Is a peer uptodate from the point of view of discovery?
1859 * If it is currently being processed, obviously not.
1860 * A forced Ping or Push is also handled by the discovery thread.
1862 * Otherwise look at whether the peer needs rediscovering.
1865 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
1866 __must_hold(&lp->lp_lock)
1870 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1871 LNET_PEER_FORCE_PING |
1872 LNET_PEER_FORCE_PUSH)) {
1874 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1876 } else if (lnet_peer_needs_push(lp)) {
1878 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1879 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1891 * Queue a peer for the attention of the discovery thread. Call with
1892 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1893 * -EALREADY if the peer was already queued.
1895 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1899 spin_lock(&lp->lp_lock);
1900 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1901 lp->lp_state |= LNET_PEER_DISCOVERING;
1902 spin_unlock(&lp->lp_lock);
1903 if (list_empty(&lp->lp_dc_list)) {
1904 lnet_peer_addref_locked(lp);
1905 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1906 wake_up(&the_lnet.ln_dc_waitq);
1912 CDEBUG(D_NET, "Queue peer %s: %d\n",
1913 libcfs_nid2str(lp->lp_primary_nid), rc);
1919 * Discovery of a peer is complete. Wake all waiters on the peer.
1920 * Call with lnet_net_lock/EX held.
1922 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1924 struct lnet_msg *msg, *tmp;
1926 LIST_HEAD(pending_msgs);
1928 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1929 libcfs_nid2str(lp->lp_primary_nid));
1931 list_del_init(&lp->lp_dc_list);
1932 spin_lock(&lp->lp_lock);
1933 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1934 spin_unlock(&lp->lp_lock);
1935 wake_up_all(&lp->lp_dc_waitq);
1937 if (lp->lp_rtr_refcount > 0)
1938 lnet_router_discovery_complete(lp);
1940 lnet_net_unlock(LNET_LOCK_EX);
1942 /* iterate through all pending messages and send them again */
1943 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1944 list_del_init(&msg->msg_list);
1945 if (lp->lp_dc_error) {
1946 lnet_finalize(msg, lp->lp_dc_error);
1950 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1951 lnet_msgtyp2str(msg->msg_type),
1952 libcfs_id2str(msg->msg_target));
1953 rc = lnet_send(msg->msg_src_nid_param, msg,
1954 msg->msg_rtr_nid_param);
1956 CNETERR("Error sending %s to %s: %d\n",
1957 lnet_msgtyp2str(msg->msg_type),
1958 libcfs_id2str(msg->msg_target), rc);
1959 lnet_finalize(msg, rc);
1962 lnet_net_lock(LNET_LOCK_EX);
1963 lnet_peer_decref_locked(lp);
1967 * Handle inbound push.
1968 * Like any event handler, called with lnet_res_lock/CPT held.
1970 void lnet_peer_push_event(struct lnet_event *ev)
1972 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1973 struct lnet_peer *lp;
1975 /* lnet_find_peer() adds a refcount */
1976 lp = lnet_find_peer(ev->source.nid);
1978 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1979 libcfs_nid2str(ev->initiator.nid),
1980 libcfs_nid2str(ev->source.nid));
1984 /* Ensure peer state remains consistent while we modify it. */
1985 spin_lock(&lp->lp_lock);
1988 * If some kind of error happened the contents of the message
1989 * cannot be used. Clear the NIDS_UPTODATE and set the
1990 * FORCE_PING flag to trigger a ping.
1993 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1994 lp->lp_state |= LNET_PEER_FORCE_PING;
1995 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1997 libcfs_nid2str(lp->lp_primary_nid),
1998 libcfs_nid2str(ev->source.nid));
2003 * A push with invalid or corrupted info. Clear the UPTODATE
2004 * flag to trigger a ping.
2006 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2007 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2008 lp->lp_state |= LNET_PEER_FORCE_PING;
2009 CDEBUG(D_NET, "Corrupted Push from %s\n",
2010 libcfs_nid2str(lp->lp_primary_nid));
2015 * Make sure we'll allocate the correct size ping buffer when
2018 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2019 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2022 * A non-Multi-Rail peer is not supposed to be capable of
2025 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2026 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2027 libcfs_nid2str(lp->lp_primary_nid));
2032 * Check the MULTIRAIL flag. Complain if the peer was DLC
2033 * configured without it.
2035 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2036 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2037 CERROR("Push says %s is Multi-Rail, DLC says not\n",
2038 libcfs_nid2str(lp->lp_primary_nid));
2040 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2041 lnet_peer_clr_non_mr_pref_nids(lp);
2046 * The peer may have discovery disabled at its end. Set
2047 * NO_DISCOVERY as appropriate.
2049 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2050 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2051 libcfs_nid2str(lp->lp_primary_nid));
2052 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2053 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2054 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2055 libcfs_nid2str(lp->lp_primary_nid));
2056 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2060 * Check for truncation of the Put message. Clear the
2061 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2062 * and tell discovery to allocate a bigger buffer.
2064 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2065 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2066 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2067 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2068 lp->lp_state |= LNET_PEER_FORCE_PING;
2069 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2070 libcfs_nid2str(lp->lp_primary_nid),
2071 pbuf->pb_info.pi_nnis);
2075 /* always assume new data */
2076 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2077 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2080 * If there is data present that hasn't been processed yet,
2081 * we'll replace it if the Put contained newer data and it
2082 * fits. We're racing with a Ping or earlier Push in this
2085 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2086 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2087 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2088 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2089 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2090 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2091 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2092 libcfs_nid2str(lp->lp_primary_nid),
2093 LNET_PING_BUFFER_SEQNO(pbuf),
2094 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2100 * Allocate a buffer to copy the data. On a failure we drop
2101 * the Push and set FORCE_PING to force the discovery
2102 * thread to fix the problem by pinging the peer.
2104 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2106 lp->lp_state |= LNET_PEER_FORCE_PING;
2107 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2108 libcfs_nid2str(lp->lp_primary_nid),
2109 LNET_PING_BUFFER_SEQNO(pbuf));
2114 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2115 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2116 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2117 CDEBUG(D_NET, "Received Push %s %u\n",
2118 libcfs_nid2str(lp->lp_primary_nid),
2119 LNET_PING_BUFFER_SEQNO(pbuf));
2123 * Queue the peer for discovery if not done, force it on the request
2124 * queue and wake the discovery thread if the peer was already queued,
2125 * because its status changed.
2127 spin_unlock(&lp->lp_lock);
2128 lnet_net_lock(LNET_LOCK_EX);
2129 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2130 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2131 wake_up(&the_lnet.ln_dc_waitq);
2133 /* Drop refcount from lookup */
2134 lnet_peer_decref_locked(lp);
2135 lnet_net_unlock(LNET_LOCK_EX);
2139 * Clear the discovery error state, unless we're already discovering
2140 * this peer, in which case the error is current.
2142 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2144 spin_lock(&lp->lp_lock);
2145 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2146 lp->lp_dc_error = 0;
2147 spin_unlock(&lp->lp_lock);
2151 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2152 * dropped/retaken within this function. An lnet_peer_ni is passed in
2153 * because discovery could tear down an lnet_peer.
2156 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2159 struct lnet_peer *lp;
2164 lnet_net_unlock(cpt);
2165 lnet_net_lock(LNET_LOCK_EX);
2166 lp = lpni->lpni_peer_net->lpn_peer;
2167 lnet_peer_clear_discovery_error(lp);
2170 * We're willing to be interrupted. The lpni can become a
2171 * zombie if we race with DLC, so we must check for that.
2174 /* Keep lp alive when the lnet_net_lock is unlocked */
2175 lnet_peer_addref_locked(lp);
2176 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2177 if (signal_pending(current))
2179 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2182 * Don't repeat discovery if discovery is disabled. This is
2183 * done to ensure we can use discovery as a standard ping as
2184 * well for backwards compatibility with routers which do not
2185 * have discovery or have discovery disabled
2187 if (lnet_is_discovery_disabled(lp) && count > 0)
2189 if (lp->lp_dc_error)
2191 if (lnet_peer_is_uptodate(lp))
2193 lnet_peer_queue_for_discovery(lp);
2195 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2198 * If caller requested a non-blocking operation then
2199 * return immediately. Once discovery is complete any
2200 * pending messages that were stopped due to discovery
2201 * will be transmitted.
2206 lnet_net_unlock(LNET_LOCK_EX);
2208 finish_wait(&lp->lp_dc_waitq, &wait);
2209 lnet_net_lock(LNET_LOCK_EX);
2210 lnet_peer_decref_locked(lp);
2211 /* Peer may have changed */
2212 lp = lpni->lpni_peer_net->lpn_peer;
2214 finish_wait(&lp->lp_dc_waitq, &wait);
2216 lnet_net_unlock(LNET_LOCK_EX);
2218 lnet_peer_decref_locked(lp);
2220 * The peer may have changed, so re-check and rediscover if that turns
2221 * out to have been the case. The reference count on lp ensured that
2222 * even if it was unlinked from lpni the memory could not be recycled.
2223 * Thus the check below is sufficient to determine whether the peer
2224 * changed. If the peer changed, then lp must not be dereferenced.
2226 if (lp != lpni->lpni_peer_net->lpn_peer)
2229 if (signal_pending(current))
2231 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2233 else if (lp->lp_dc_error)
2234 rc = lp->lp_dc_error;
2236 CDEBUG(D_NET, "non-blocking discovery\n");
2237 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2240 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2241 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2242 libcfs_nid2str(lpni->lpni_nid), rc,
2243 (!block) ? "pending discovery" : "discovery complete");
2248 /* Handle an incoming ack for a push. */
2250 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2252 struct lnet_ping_buffer *pbuf;
2254 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2255 spin_lock(&lp->lp_lock);
2256 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2257 lp->lp_push_error = ev->status;
2259 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2261 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2262 spin_unlock(&lp->lp_lock);
2264 CDEBUG(D_NET, "peer %s ev->status %d\n",
2265 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2268 /* Handle a Reply message. This is the reply to a Ping message. */
2270 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2272 struct lnet_ping_buffer *pbuf;
2275 spin_lock(&lp->lp_lock);
2278 * If some kind of error happened the contents of message
2279 * cannot be used. Set PING_FAILED to trigger a retry.
2282 lp->lp_state |= LNET_PEER_PING_FAILED;
2283 lp->lp_ping_error = ev->status;
2284 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2286 libcfs_nid2str(lp->lp_primary_nid),
2287 libcfs_nid2str(ev->source.nid));
2291 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2292 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2293 lnet_swap_pinginfo(pbuf);
2296 * A reply with invalid or corrupted info. Set PING_FAILED to
2299 rc = lnet_ping_info_validate(&pbuf->pb_info);
2301 lp->lp_state |= LNET_PEER_PING_FAILED;
2302 lp->lp_ping_error = 0;
2303 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2304 libcfs_nid2str(lp->lp_primary_nid), rc);
2310 * The peer may have discovery disabled at its end. Set
2311 * NO_DISCOVERY as appropriate.
2313 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2314 !lnet_peer_discovery_disabled) {
2315 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2316 libcfs_nid2str(lp->lp_primary_nid));
2317 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2319 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2320 libcfs_nid2str(lp->lp_primary_nid));
2321 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2325 * Update the MULTI_RAIL flag based on the reply. If the peer
2326 * was configured with DLC then the setting should match what
2329 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2330 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2331 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2332 libcfs_nid2str(lp->lp_primary_nid), lp);
2333 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2334 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2335 libcfs_nid2str(lp->lp_primary_nid));
2336 } else if (lnet_peer_discovery_disabled) {
2338 "peer %s(%p) not MR: DD disabled locally\n",
2339 libcfs_nid2str(lp->lp_primary_nid), lp);
2340 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2342 "peer %s(%p) not MR: DD disabled remotely\n",
2343 libcfs_nid2str(lp->lp_primary_nid), lp);
2345 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2346 libcfs_nid2str(lp->lp_primary_nid), lp);
2347 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2348 lnet_peer_clr_non_mr_pref_nids(lp);
2350 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2351 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2352 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2353 libcfs_nid2str(lp->lp_primary_nid));
2355 CERROR("Multi-Rail state vanished from %s\n",
2356 libcfs_nid2str(lp->lp_primary_nid));
2357 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2362 * Make sure we'll allocate the correct size ping buffer when
2365 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2366 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2369 * Check for truncation of the Reply. Clear PING_SENT and set
2370 * PING_FAILED to trigger a retry.
2372 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2373 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2374 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2375 lp->lp_state |= LNET_PEER_PING_FAILED;
2376 lp->lp_ping_error = 0;
2377 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2378 libcfs_nid2str(lp->lp_primary_nid),
2379 pbuf->pb_info.pi_nnis);
2384 * Check the sequence numbers in the reply. These are only
2385 * available if the reply came from a Multi-Rail peer.
2387 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2388 pbuf->pb_info.pi_nnis > 1 &&
2389 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2390 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2391 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2392 libcfs_nid2str(lp->lp_primary_nid),
2393 LNET_PING_BUFFER_SEQNO(pbuf),
2396 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2399 /* We're happy with the state of the data in the buffer. */
2400 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2401 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2402 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2403 lnet_ping_buffer_decref(lp->lp_data);
2405 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2406 lnet_ping_buffer_addref(pbuf);
2409 lp->lp_state &= ~LNET_PEER_PING_SENT;
2410 spin_unlock(&lp->lp_lock);
2412 lnet_net_lock(LNET_LOCK_EX);
2414 * If this peer is a gateway, call the routing callback to
2415 * handle the ping reply
2417 if (lp->lp_rtr_refcount > 0)
2418 lnet_router_discovery_ping_reply(lp);
2419 lnet_net_unlock(LNET_LOCK_EX);
2423 * Send event handling. Only matters for error cases, where we clean
2424 * up state on the peer and peer_ni that would otherwise be updated in
2425 * the REPLY event handler for a successful Ping, and the ACK event
2426 * handler for a successful Push.
2429 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2436 spin_lock(&lp->lp_lock);
2437 if (ev->msg_type == LNET_MSG_GET) {
2438 lp->lp_state &= ~LNET_PEER_PING_SENT;
2439 lp->lp_state |= LNET_PEER_PING_FAILED;
2440 lp->lp_ping_error = ev->status;
2441 } else { /* ev->msg_type == LNET_MSG_PUT */
2442 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2443 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2444 lp->lp_push_error = ev->status;
2446 spin_unlock(&lp->lp_lock);
2447 rc = LNET_REDISCOVER_PEER;
2449 CDEBUG(D_NET, "%s Send to %s: %d\n",
2450 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2451 libcfs_nid2str(ev->target.nid), rc);
2456 * Unlink event handling. This event is only seen if a call to
2457 * LNetMDUnlink() caused the event to be unlinked. If this call was
2458 * made after the event was set up in LNetGet() or LNetPut() then we
2459 * assume the Ping or Push timed out.
2462 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2464 spin_lock(&lp->lp_lock);
2465 /* We've passed through LNetGet() */
2466 if (lp->lp_state & LNET_PEER_PING_SENT) {
2467 lp->lp_state &= ~LNET_PEER_PING_SENT;
2468 lp->lp_state |= LNET_PEER_PING_FAILED;
2469 lp->lp_ping_error = -ETIMEDOUT;
2470 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2471 libcfs_nid2str(lp->lp_primary_nid));
2473 /* We've passed through LNetPut() */
2474 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2475 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2476 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2477 lp->lp_push_error = -ETIMEDOUT;
2478 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2479 libcfs_nid2str(lp->lp_primary_nid));
2481 spin_unlock(&lp->lp_lock);
2485 * Event handler for the discovery EQ.
2487 * Called with lnet_res_lock(cpt) held. The cpt is the
2488 * lnet_cpt_of_cookie() of the md handle cookie.
2490 static void lnet_discovery_event_handler(struct lnet_event *event)
2492 struct lnet_peer *lp = event->md.user_ptr;
2493 struct lnet_ping_buffer *pbuf;
2496 /* discovery needs to take another look */
2497 rc = LNET_REDISCOVER_PEER;
2499 CDEBUG(D_NET, "Received event: %d\n", event->type);
2501 switch (event->type) {
2502 case LNET_EVENT_ACK:
2503 lnet_discovery_event_ack(lp, event);
2505 case LNET_EVENT_REPLY:
2506 lnet_discovery_event_reply(lp, event);
2508 case LNET_EVENT_SEND:
2509 /* Only send failure triggers a retry. */
2510 rc = lnet_discovery_event_send(lp, event);
2512 case LNET_EVENT_UNLINK:
2513 /* LNetMDUnlink() was called */
2514 lnet_discovery_event_unlink(lp, event);
2517 /* Invalid events. */
2520 lnet_net_lock(LNET_LOCK_EX);
2521 if (event->unlinked) {
2522 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2523 lnet_ping_buffer_decref(pbuf);
2524 lnet_peer_decref_locked(lp);
2527 /* put peer back at end of request queue, if discovery not already
2529 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2530 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2531 wake_up(&the_lnet.ln_dc_waitq);
2533 lnet_net_unlock(LNET_LOCK_EX);
2537 * Build a peer from incoming data.
2539 * The NIDs in the incoming data are supposed to be structured as follows:
2542 * - other NIDs in same net
2543 * - NIDs in second net
2544 * - NIDs in third net
2546 * This due to the way the list of NIDs in the data is created.
2548 * Note that this function will mark the peer uptodate unless an
2549 * ENOMEM is encontered. All other errors are due to a conflict
2550 * between the DLC configuration and what discovery sees. We treat DLC
2551 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2552 * peer from becoming stuck in discovery.
2554 static int lnet_peer_merge_data(struct lnet_peer *lp,
2555 struct lnet_ping_buffer *pbuf)
2557 struct lnet_peer_ni *lpni;
2558 lnet_nid_t *curnis = NULL;
2559 struct lnet_ni_status *addnis = NULL;
2560 lnet_nid_t *delnis = NULL;
2570 flags = LNET_PEER_DISCOVERED;
2571 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2572 flags |= LNET_PEER_MULTI_RAIL;
2575 * Cache the routing feature for the peer; whether it is enabled
2576 * for disabled as reported by the remote peer.
2578 spin_lock(&lp->lp_lock);
2579 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2580 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2582 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2583 spin_unlock(&lp->lp_lock);
2585 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2586 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2587 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2588 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2589 if (!curnis || !addnis || !delnis) {
2597 /* Construct the list of NIDs present in peer. */
2599 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2600 curnis[ncurnis++] = lpni->lpni_nid;
2603 * Check for NIDs in pbuf not present in curnis[].
2604 * The loop starts at 1 to skip the loopback NID.
2606 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2607 for (j = 0; j < ncurnis; j++)
2608 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2611 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2614 * Check for NIDs in curnis[] not present in pbuf.
2615 * The nested loop starts at 1 to skip the loopback NID.
2617 * But never add the loopback NID to delnis[]: if it is
2618 * present in curnis[] then this peer is for this node.
2620 for (i = 0; i < ncurnis; i++) {
2621 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2623 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2624 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2626 * update the information we cache for the
2627 * peer with the latest information we
2630 lpni = lnet_find_peer_ni_locked(curnis[i]);
2632 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2633 lnet_peer_ni_decref_locked(lpni);
2638 if (j == pbuf->pb_info.pi_nnis)
2639 delnis[ndelnis++] = curnis[i];
2643 * If we get here and the discovery is disabled then we don't want
2644 * to add or delete any NIs. We just updated the ones we have some
2645 * information on, and call it a day
2648 if (lnet_is_discovery_disabled(lp))
2651 for (i = 0; i < naddnis; i++) {
2652 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2654 CERROR("Error adding NID %s to peer %s: %d\n",
2655 libcfs_nid2str(addnis[i].ns_nid),
2656 libcfs_nid2str(lp->lp_primary_nid), rc);
2660 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2662 lpni->lpni_ns_status = addnis[i].ns_status;
2663 lnet_peer_ni_decref_locked(lpni);
2667 for (i = 0; i < ndelnis; i++) {
2669 * for routers it's okay to delete the primary_nid because
2670 * the upper layers don't really rely on it. So if we're
2671 * being told that the router changed its primary_nid
2672 * then it's okay to delete it.
2674 if (lp->lp_rtr_refcount > 0)
2675 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2676 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2678 CERROR("Error deleting NID %s from peer %s: %d\n",
2679 libcfs_nid2str(delnis[i]),
2680 libcfs_nid2str(lp->lp_primary_nid), rc);
2686 * Errors other than -ENOMEM are due to peers having been
2687 * configured with DLC. Ignore these because DLC overrides
2692 CFS_FREE_PTR_ARRAY(curnis, nnis);
2693 CFS_FREE_PTR_ARRAY(addnis, nnis);
2694 CFS_FREE_PTR_ARRAY(delnis, nnis);
2695 lnet_ping_buffer_decref(pbuf);
2696 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2699 spin_lock(&lp->lp_lock);
2700 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2701 lp->lp_state |= LNET_PEER_FORCE_PING;
2702 spin_unlock(&lp->lp_lock);
2708 * The data in pbuf says lp is its primary peer, but the data was
2709 * received by a different peer. Try to update lp with the data.
2712 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2714 struct lnet_handle_md mdh;
2716 /* Queue lp for discovery, and force it on the request queue. */
2717 lnet_net_lock(LNET_LOCK_EX);
2718 if (lnet_peer_queue_for_discovery(lp))
2719 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2720 lnet_net_unlock(LNET_LOCK_EX);
2722 LNetInvalidateMDHandle(&mdh);
2725 * Decide whether we can move the peer to the DATA_PRESENT state.
2727 * We replace stale data for a multi-rail peer, repair PING_FAILED
2728 * status, and preempt FORCE_PING.
2730 * If after that we have DATA_PRESENT, we merge it into this peer.
2732 spin_lock(&lp->lp_lock);
2733 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2734 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2735 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2736 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2737 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2738 lnet_ping_buffer_decref(pbuf);
2743 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2744 lnet_ping_buffer_decref(lp->lp_data);
2746 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2748 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2749 mdh = lp->lp_ping_mdh;
2750 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2751 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2752 lp->lp_ping_error = 0;
2754 if (lp->lp_state & LNET_PEER_FORCE_PING)
2755 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2756 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2757 spin_unlock(&lp->lp_lock);
2759 if (!LNetMDHandleIsInvalid(mdh))
2763 return lnet_peer_merge_data(lp, pbuf);
2765 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2769 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2773 for (i = 0; i < pinfo->pi_nnis; i++) {
2774 if (pinfo->pi_ni[i].ns_nid == nid)
2782 * Update a peer using the data received.
2784 static int lnet_peer_data_present(struct lnet_peer *lp)
2785 __must_hold(&lp->lp_lock)
2787 struct lnet_ping_buffer *pbuf;
2788 struct lnet_peer_ni *lpni;
2789 lnet_nid_t nid = LNET_NID_ANY;
2795 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2796 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2797 spin_unlock(&lp->lp_lock);
2800 * Modifications of peer structures are done while holding the
2801 * ln_api_mutex. A global lock is required because we may be
2802 * modifying multiple peer structures, and a mutex greatly
2803 * simplifies memory management.
2805 * The actual changes to the data structures must also protect
2806 * against concurrent lookups, for which the lnet_net_lock in
2807 * LNET_LOCK_EX mode is used.
2809 mutex_lock(&the_lnet.ln_api_mutex);
2810 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2816 * If this peer is not on the peer list then it is being torn
2817 * down, and our reference count may be all that is keeping it
2818 * alive. Don't do any work on it.
2820 if (list_empty(&lp->lp_peer_list))
2823 flags = LNET_PEER_DISCOVERED;
2824 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2825 flags |= LNET_PEER_MULTI_RAIL;
2828 * Check whether the primary NID in the message matches the
2829 * primary NID of the peer. If it does, update the peer, if
2830 * it it does not, check whether there is already a peer with
2831 * that primary NID. If no such peer exists, try to update
2832 * the primary NID of the current peer (allowed if it was
2833 * created due to message traffic) and complete the update.
2834 * If the peer did exist, hand off the data to it.
2836 * The peer for the loopback interface is a special case: this
2837 * is the peer for the local node, and we want to set its
2838 * primary NID to the correct value here. Moreover, this peer
2839 * can show up with only the loopback NID in the ping buffer.
2841 if (pbuf->pb_info.pi_nnis <= 1)
2843 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2844 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2845 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2847 rc = lnet_peer_merge_data(lp, pbuf);
2849 * if the primary nid of the peer is present in the ping info returned
2850 * from the peer, but it's not the local primary peer we have
2851 * cached and discovery is disabled, then we don't want to update
2852 * our local peer info, by adding or removing NIDs, we just want
2853 * to update the status of the nids that we currently have
2854 * recorded in that peer.
2856 } else if (lp->lp_primary_nid == nid ||
2857 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2858 lnet_is_discovery_disabled(lp))) {
2859 rc = lnet_peer_merge_data(lp, pbuf);
2861 lpni = lnet_find_peer_ni_locked(nid);
2863 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2865 CERROR("Primary NID error %s versus %s: %d\n",
2866 libcfs_nid2str(lp->lp_primary_nid),
2867 libcfs_nid2str(nid), rc);
2869 rc = lnet_peer_merge_data(lp, pbuf);
2872 struct lnet_peer *new_lp;
2873 new_lp = lpni->lpni_peer_net->lpn_peer;
2875 * if lp has discovery/MR enabled that means new_lp
2876 * should have discovery/MR enabled as well, since
2877 * it's the same peer, which we're about to merge
2879 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2880 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2881 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2882 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2884 rc = lnet_peer_set_primary_data(new_lp, pbuf);
2885 lnet_consolidate_routes_locked(lp, new_lp);
2886 lnet_peer_ni_decref_locked(lpni);
2890 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2892 mutex_unlock(&the_lnet.ln_api_mutex);
2894 spin_lock(&lp->lp_lock);
2895 /* Tell discovery to re-check the peer immediately. */
2897 rc = LNET_REDISCOVER_PEER;
2902 * A ping failed. Clear the PING_FAILED state and set the
2903 * FORCE_PING state, to ensure a retry even if discovery is
2904 * disabled. This avoids being left with incorrect state.
2906 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2907 __must_hold(&lp->lp_lock)
2909 struct lnet_handle_md mdh;
2912 mdh = lp->lp_ping_mdh;
2913 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2914 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2915 lp->lp_state |= LNET_PEER_FORCE_PING;
2916 rc = lp->lp_ping_error;
2917 lp->lp_ping_error = 0;
2918 spin_unlock(&lp->lp_lock);
2920 if (!LNetMDHandleIsInvalid(mdh))
2923 CDEBUG(D_NET, "peer %s:%d\n",
2924 libcfs_nid2str(lp->lp_primary_nid), rc);
2926 spin_lock(&lp->lp_lock);
2927 return rc ? rc : LNET_REDISCOVER_PEER;
2931 * Select NID to send a Ping or Push to.
2933 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2935 struct lnet_peer_ni *lpni;
2937 /* Look for a direct-connected NID for this peer. */
2939 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2940 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2945 return lpni->lpni_nid;
2947 /* Look for a routed-connected NID for this peer. */
2949 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2950 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2955 return lpni->lpni_nid;
2957 return LNET_NID_ANY;
2960 /* Active side of ping. */
2961 static int lnet_peer_send_ping(struct lnet_peer *lp)
2962 __must_hold(&lp->lp_lock)
2969 lp->lp_state |= LNET_PEER_PING_SENT;
2970 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2971 spin_unlock(&lp->lp_lock);
2973 cpt = lnet_net_lock_current();
2974 /* Refcount for MD. */
2975 lnet_peer_addref_locked(lp);
2976 pnid = lnet_peer_select_nid(lp);
2977 lnet_net_unlock(cpt);
2979 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2981 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2982 the_lnet.ln_dc_eq, false);
2985 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2986 * refcount on the peer, otherwise LNetMDUnlink will be called
2987 * which will eventually do that.
2991 lnet_peer_decref_locked(lp);
2992 lnet_net_unlock(cpt);
2993 rc = -rc; /* change the rc to negative value */
2995 } else if (rc < 0) {
2999 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3001 spin_lock(&lp->lp_lock);
3005 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3007 * The errors that get us here are considered hard errors and
3008 * cause Discovery to terminate. So we clear PING_SENT, but do
3009 * not set either PING_FAILED or FORCE_PING. In fact we need
3010 * to clear PING_FAILED, because the unlink event handler will
3011 * have set it if we called LNetMDUnlink() above.
3013 spin_lock(&lp->lp_lock);
3014 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3019 * This function exists because you cannot call LNetMDUnlink() from an
3022 static int lnet_peer_push_failed(struct lnet_peer *lp)
3023 __must_hold(&lp->lp_lock)
3025 struct lnet_handle_md mdh;
3028 mdh = lp->lp_push_mdh;
3029 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3030 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3031 rc = lp->lp_push_error;
3032 lp->lp_push_error = 0;
3033 spin_unlock(&lp->lp_lock);
3035 if (!LNetMDHandleIsInvalid(mdh))
3038 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3039 spin_lock(&lp->lp_lock);
3040 return rc ? rc : LNET_REDISCOVER_PEER;
3043 /* Active side of push. */
3044 static int lnet_peer_send_push(struct lnet_peer *lp)
3045 __must_hold(&lp->lp_lock)
3047 struct lnet_ping_buffer *pbuf;
3048 struct lnet_process_id id;
3053 /* Don't push to a non-multi-rail peer. */
3054 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3055 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3059 lp->lp_state |= LNET_PEER_PUSH_SENT;
3060 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3061 spin_unlock(&lp->lp_lock);
3063 cpt = lnet_net_lock_current();
3064 pbuf = the_lnet.ln_ping_target;
3065 lnet_ping_buffer_addref(pbuf);
3066 lnet_net_unlock(cpt);
3068 /* Push source MD */
3069 md.start = &pbuf->pb_info;
3070 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3071 md.threshold = 2; /* Put/Ack */
3074 md.eq_handle = the_lnet.ln_dc_eq;
3077 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
3079 lnet_ping_buffer_decref(pbuf);
3080 CERROR("Can't bind push source MD: %d\n", rc);
3083 cpt = lnet_net_lock_current();
3084 /* Refcount for MD. */
3085 lnet_peer_addref_locked(lp);
3086 id.pid = LNET_PID_LUSTRE;
3087 id.nid = lnet_peer_select_nid(lp);
3088 lnet_net_unlock(cpt);
3090 if (id.nid == LNET_NID_ANY) {
3095 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
3096 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3097 LNET_PROTO_PING_MATCHBITS, 0, 0);
3102 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3104 spin_lock(&lp->lp_lock);
3108 LNetMDUnlink(lp->lp_push_mdh);
3109 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3111 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3113 * The errors that get us here are considered hard errors and
3114 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3115 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3116 * because the unlink event handler will have set it if we
3117 * called LNetMDUnlink() above.
3119 spin_lock(&lp->lp_lock);
3120 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3125 * An unrecoverable error was encountered during discovery.
3126 * Set error status in peer and abort discovery.
3128 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3130 CDEBUG(D_NET, "Discovery error %s: %d\n",
3131 libcfs_nid2str(lp->lp_primary_nid), error);
3133 spin_lock(&lp->lp_lock);
3134 lp->lp_dc_error = error;
3135 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3136 lp->lp_state |= LNET_PEER_REDISCOVER;
3137 spin_unlock(&lp->lp_lock);
3141 * Mark the peer as discovered.
3143 static int lnet_peer_discovered(struct lnet_peer *lp)
3144 __must_hold(&lp->lp_lock)
3146 lp->lp_state |= LNET_PEER_DISCOVERED;
3147 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3148 LNET_PEER_REDISCOVER);
3150 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3157 * Discovering this peer is taking too long. Cancel any Ping or Push
3158 * that discovery is waiting on by unlinking the relevant MDs. The
3159 * lnet_discovery_event_handler() will proceed from here and complete
3162 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3164 struct lnet_handle_md ping_mdh;
3165 struct lnet_handle_md push_mdh;
3167 LNetInvalidateMDHandle(&ping_mdh);
3168 LNetInvalidateMDHandle(&push_mdh);
3170 spin_lock(&lp->lp_lock);
3171 if (lp->lp_state & LNET_PEER_PING_SENT) {
3172 ping_mdh = lp->lp_ping_mdh;
3173 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3175 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3176 push_mdh = lp->lp_push_mdh;
3177 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3179 spin_unlock(&lp->lp_lock);
3181 if (!LNetMDHandleIsInvalid(ping_mdh))
3182 LNetMDUnlink(ping_mdh);
3183 if (!LNetMDHandleIsInvalid(push_mdh))
3184 LNetMDUnlink(push_mdh);
3188 * Wait for work to be queued or some other change that must be
3189 * attended to. Returns non-zero if the discovery thread should shut
3192 static int lnet_peer_discovery_wait_for_work(void)
3199 cpt = lnet_net_lock_current();
3201 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3202 TASK_INTERRUPTIBLE);
3203 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3205 if (lnet_push_target_resize_needed())
3207 if (!list_empty(&the_lnet.ln_dc_request))
3209 if (!list_empty(&the_lnet.ln_msg_resend))
3211 lnet_net_unlock(cpt);
3214 * wakeup max every second to check if there are peers that
3215 * have been stuck on the working queue for greater than
3218 schedule_timeout(cfs_time_seconds(1));
3219 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3220 cpt = lnet_net_lock_current();
3222 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3224 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3227 lnet_net_unlock(cpt);
3229 CDEBUG(D_NET, "woken: %d\n", rc);
3235 * Messages that were pending on a destroyed peer will be put on a global
3236 * resend list. The message resend list will be checked by
3237 * the discovery thread when it wakes up, and will resend messages. These
3238 * messages can still be sendable in the case the lpni which was the initial
3239 * cause of the message re-queue was transfered to another peer.
3241 * It is possible that LNet could be shutdown while we're iterating
3242 * through the list. lnet_shudown_lndnets() will attempt to access the
3243 * resend list, but will have to wait until the spinlock is released, by
3244 * which time there shouldn't be any more messages on the resend list.
3245 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3246 * for the messages so they can be released. The other case is that
3247 * lnet_shudown_lndnets() can finalize all the messages before this
3248 * function can visit the resend list, in which case this function will be
3251 static void lnet_resend_msgs(void)
3253 struct lnet_msg *msg, *tmp;
3257 spin_lock(&the_lnet.ln_msg_resend_lock);
3258 list_splice(&the_lnet.ln_msg_resend, &resend);
3259 spin_unlock(&the_lnet.ln_msg_resend_lock);
3261 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3262 list_del_init(&msg->msg_list);
3263 rc = lnet_send(msg->msg_src_nid_param, msg,
3264 msg->msg_rtr_nid_param);
3266 CNETERR("Error sending %s to %s: %d\n",
3267 lnet_msgtyp2str(msg->msg_type),
3268 libcfs_id2str(msg->msg_target), rc);
3269 lnet_finalize(msg, rc);
3274 /* The discovery thread. */
3275 static int lnet_peer_discovery(void *arg)
3277 struct lnet_peer *lp;
3280 wait_for_completion(&the_lnet.ln_started);
3282 CDEBUG(D_NET, "started\n");
3285 if (lnet_peer_discovery_wait_for_work())
3290 if (lnet_push_target_resize_needed())
3291 lnet_push_target_resize();
3293 lnet_net_lock(LNET_LOCK_EX);
3294 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3298 * Process all incoming discovery work requests. When
3299 * discovery must wait on a peer to change state, it
3300 * is added to the tail of the ln_dc_working queue. A
3301 * timestamp keeps track of when the peer was added,
3302 * so we can time out discovery requests that take too
3305 while (!list_empty(&the_lnet.ln_dc_request)) {
3306 lp = list_first_entry(&the_lnet.ln_dc_request,
3307 struct lnet_peer, lp_dc_list);
3308 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3310 * set the time the peer was put on the dc_working
3311 * queue. It shouldn't remain on the queue
3312 * forever, in case the GET message (for ping)
3313 * doesn't get a REPLY or the PUT message (for
3314 * push) doesn't get an ACK.
3316 lp->lp_last_queued = ktime_get_real_seconds();
3317 lnet_net_unlock(LNET_LOCK_EX);
3320 * Select an action depending on the state of
3321 * the peer and whether discovery is disabled.
3322 * The check whether discovery is disabled is
3323 * done after the code that handles processing
3324 * for arrived data, cleanup for failures, and
3325 * forcing a Ping or Push.
3327 spin_lock(&lp->lp_lock);
3328 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3329 libcfs_nid2str(lp->lp_primary_nid), lp,
3331 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3332 rc = lnet_peer_data_present(lp);
3333 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3334 rc = lnet_peer_ping_failed(lp);
3335 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3336 rc = lnet_peer_push_failed(lp);
3337 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3338 rc = lnet_peer_send_ping(lp);
3339 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3340 rc = lnet_peer_send_push(lp);
3341 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3342 rc = lnet_peer_send_ping(lp);
3343 else if (lnet_peer_needs_push(lp))
3344 rc = lnet_peer_send_push(lp);
3346 rc = lnet_peer_discovered(lp);
3347 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3348 libcfs_nid2str(lp->lp_primary_nid), lp,
3350 spin_unlock(&lp->lp_lock);
3352 lnet_net_lock(LNET_LOCK_EX);
3353 if (rc == LNET_REDISCOVER_PEER) {
3354 list_move(&lp->lp_dc_list,
3355 &the_lnet.ln_dc_request);
3357 lnet_peer_discovery_error(lp, rc);
3359 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3360 lnet_peer_discovery_complete(lp);
3361 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3365 lnet_net_unlock(LNET_LOCK_EX);
3368 CDEBUG(D_NET, "stopping\n");
3370 * Clean up before telling lnet_peer_discovery_stop() that
3371 * we're done. Use wake_up() below to somewhat reduce the
3372 * size of the thundering herd if there are multiple threads
3373 * waiting on discovery of a single peer.
3376 /* Queue cleanup 1: stop all pending pings and pushes. */
3377 lnet_net_lock(LNET_LOCK_EX);
3378 while (!list_empty(&the_lnet.ln_dc_working)) {
3379 lp = list_first_entry(&the_lnet.ln_dc_working,
3380 struct lnet_peer, lp_dc_list);
3381 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3382 lnet_net_unlock(LNET_LOCK_EX);
3383 lnet_peer_cancel_discovery(lp);
3384 lnet_net_lock(LNET_LOCK_EX);
3386 lnet_net_unlock(LNET_LOCK_EX);
3388 /* Queue cleanup 2: wait for the expired queue to clear. */
3389 while (!list_empty(&the_lnet.ln_dc_expired))
3390 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3392 /* Queue cleanup 3: clear the request queue. */
3393 lnet_net_lock(LNET_LOCK_EX);
3394 while (!list_empty(&the_lnet.ln_dc_request)) {
3395 lp = list_first_entry(&the_lnet.ln_dc_request,
3396 struct lnet_peer, lp_dc_list);
3397 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3398 lnet_peer_discovery_complete(lp);
3400 lnet_net_unlock(LNET_LOCK_EX);
3402 LNetEQFree(the_lnet.ln_dc_eq);
3403 the_lnet.ln_dc_eq = NULL;
3405 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3406 wake_up(&the_lnet.ln_dc_waitq);
3408 CDEBUG(D_NET, "stopped\n");
3413 /* ln_api_mutex is held on entry. */
3414 int lnet_peer_discovery_start(void)
3416 struct task_struct *task;
3419 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3422 the_lnet.ln_dc_eq = LNetEQAlloc(lnet_discovery_event_handler);
3423 if (IS_ERR(the_lnet.ln_dc_eq)) {
3424 rc = PTR_ERR(the_lnet.ln_dc_eq);
3425 CERROR("Can't allocate discovery EQ: %d\n", rc);
3429 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3430 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3433 CERROR("Can't start peer discovery thread: %d\n", rc);
3435 LNetEQFree(the_lnet.ln_dc_eq);
3436 the_lnet.ln_dc_eq = NULL;
3438 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3441 CDEBUG(D_NET, "discovery start: %d\n", rc);
3446 /* ln_api_mutex is held on entry. */
3447 void lnet_peer_discovery_stop(void)
3449 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3452 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3453 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3455 /* In the LNetNIInit() path we may be stopping discovery before it
3456 * entered its work loop
3458 if (!completion_done(&the_lnet.ln_started))
3459 complete(&the_lnet.ln_started);
3461 wake_up(&the_lnet.ln_dc_waitq);
3463 wait_event(the_lnet.ln_dc_waitq,
3464 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3466 LASSERT(list_empty(&the_lnet.ln_dc_request));
3467 LASSERT(list_empty(&the_lnet.ln_dc_working));
3468 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3470 CDEBUG(D_NET, "discovery stopped\n");
3476 lnet_debug_peer(lnet_nid_t nid)
3478 char *aliveness = "NA";
3479 struct lnet_peer_ni *lp;
3482 cpt = lnet_cpt_of_nid(nid, NULL);
3485 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3487 lnet_net_unlock(cpt);
3488 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3492 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3493 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3495 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3496 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3497 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3498 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3499 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3501 lnet_peer_ni_decref_locked(lp);
3503 lnet_net_unlock(cpt);
3506 /* Gathering information for userspace. */
3508 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3509 char aliveness[LNET_MAX_STR_LEN],
3510 __u32 *cpt_iter, __u32 *refcount,
3511 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3512 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3513 __u32 *peer_tx_qnob)
3515 struct lnet_peer_table *peer_table;
3516 struct lnet_peer_ni *lp;
3521 /* get the number of CPTs */
3522 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3524 /* if the cpt number to be examined is >= the number of cpts in
3525 * the system then indicate that there are no more cpts to examin
3527 if (*cpt_iter >= lncpt)
3530 /* get the current table */
3531 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3532 /* if the ptable is NULL then there are no more cpts to examine */
3533 if (peer_table == NULL)
3536 lnet_net_lock(*cpt_iter);
3538 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3539 struct list_head *peers = &peer_table->pt_hash[j];
3541 list_for_each_entry(lp, peers, lpni_hashlist) {
3542 if (peer_index-- > 0)
3545 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3546 if (lnet_isrouter(lp) ||
3547 lnet_peer_aliveness_enabled(lp))
3548 snprintf(aliveness, LNET_MAX_STR_LEN,
3549 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3551 *nid = lp->lpni_nid;
3552 *refcount = atomic_read(&lp->lpni_refcount);
3553 *ni_peer_tx_credits =
3554 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3555 *peer_tx_credits = lp->lpni_txcredits;
3556 *peer_rtr_credits = lp->lpni_rtrcredits;
3557 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3558 *peer_tx_qnob = lp->lpni_txqnob;
3564 lnet_net_unlock(*cpt_iter);
3568 return found ? 0 : -ENOENT;
3571 /* ln_api_mutex is held, which keeps the peer list stable */
3572 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3574 struct lnet_ioctl_element_stats *lpni_stats;
3575 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3576 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3577 struct lnet_peer_ni_credit_info *lpni_info;
3578 struct lnet_peer_ni *lpni;
3579 struct lnet_peer *lp;
3584 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3591 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3592 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3593 size *= lp->lp_nnis;
3594 if (size > cfg->prcfg_size) {
3595 cfg->prcfg_size = size;
3600 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3601 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3602 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3603 cfg->prcfg_count = lp->lp_nnis;
3604 cfg->prcfg_size = size;
3605 cfg->prcfg_state = lp->lp_state;
3607 /* Allocate helper buffers. */
3609 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3612 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3615 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3616 if (!lpni_msg_stats)
3617 goto out_free_stats;
3618 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3620 goto out_free_msg_stats;
3625 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3626 nid = lpni->lpni_nid;
3627 if (copy_to_user(bulk, &nid, sizeof(nid)))
3628 goto out_free_hstats;
3629 bulk += sizeof(nid);
3631 memset(lpni_info, 0, sizeof(*lpni_info));
3632 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3633 if (lnet_isrouter(lpni) ||
3634 lnet_peer_aliveness_enabled(lpni))
3635 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3636 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3638 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3639 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3640 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3641 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3642 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3643 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3644 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3645 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3646 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3647 goto out_free_hstats;
3648 bulk += sizeof(*lpni_info);
3650 memset(lpni_stats, 0, sizeof(*lpni_stats));
3651 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3652 LNET_STATS_TYPE_SEND);
3653 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3654 LNET_STATS_TYPE_RECV);
3655 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3656 LNET_STATS_TYPE_DROP);
3657 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3658 goto out_free_hstats;
3659 bulk += sizeof(*lpni_stats);
3660 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3661 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3662 goto out_free_hstats;
3663 bulk += sizeof(*lpni_msg_stats);
3664 lpni_hstats->hlpni_network_timeout =
3665 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3666 lpni_hstats->hlpni_remote_dropped =
3667 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3668 lpni_hstats->hlpni_remote_timeout =
3669 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3670 lpni_hstats->hlpni_remote_error =
3671 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3672 lpni_hstats->hlpni_health_value =
3673 atomic_read(&lpni->lpni_healthv);
3674 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3675 goto out_free_hstats;
3676 bulk += sizeof(*lpni_hstats);
3681 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3683 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3685 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3687 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3689 lnet_peer_decref_locked(lp);
3695 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3697 /* the mt could've shutdown and cleaned up the queues */
3698 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3701 if (list_empty(&lpni->lpni_recovery) &&
3702 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3703 CDEBUG(D_NET, "lpni %s added to recovery queue. Health = %d\n",
3704 libcfs_nid2str(lpni->lpni_nid),
3705 atomic_read(&lpni->lpni_healthv));
3706 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3707 lnet_peer_ni_addref_locked(lpni);
3711 /* Call with the ln_api_mutex held */
3713 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3715 struct lnet_peer_table *ptable;
3716 struct lnet_peer *lp;
3717 struct lnet_peer_net *lpn;
3718 struct lnet_peer_ni *lpni;
3722 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3726 lnet_net_lock(LNET_LOCK_EX);
3727 lpni = lnet_find_peer_ni_locked(nid);
3729 lnet_net_unlock(LNET_LOCK_EX);
3732 atomic_set(&lpni->lpni_healthv, value);
3733 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3734 lnet_peer_ni_decref_locked(lpni);
3735 lnet_net_unlock(LNET_LOCK_EX);
3739 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3742 * Walk all the peers and reset the healhv for each one to the
3745 lnet_net_lock(LNET_LOCK_EX);
3746 for (cpt = 0; cpt < lncpt; cpt++) {
3747 ptable = the_lnet.ln_peer_tables[cpt];
3748 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3749 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3750 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3752 atomic_set(&lpni->lpni_healthv, value);
3753 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3758 lnet_net_unlock(LNET_LOCK_EX);