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 INIT_LIST_HEAD(&lpni->lpni_rtr_pref_nids);
170 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
171 atomic_set(&lpni->lpni_refcount, 1);
172 lpni->lpni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
174 spin_lock_init(&lpni->lpni_lock);
176 if (lnet_peers_start_down())
177 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
179 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
180 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
181 lpni->lpni_nid = nid;
182 lpni->lpni_cpt = cpt;
183 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
185 net = lnet_get_net_locked(LNET_NIDNET(nid));
186 lpni->lpni_net = net;
188 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
189 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
190 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
191 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
194 * This peer_ni is not on a local network, so we
195 * cannot add the credits here. In case the net is
196 * added later, add the peer_ni to the remote peer ni
197 * list so it can be easily found and revisited.
199 /* FIXME: per-net implementation instead? */
200 lnet_peer_ni_addref_locked(lpni);
201 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
202 &the_lnet.ln_remote_peer_ni_list);
205 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
210 static struct lnet_peer_net *
211 lnet_peer_net_alloc(__u32 net_id)
213 struct lnet_peer_net *lpn;
215 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
219 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
220 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
221 lpn->lpn_net_id = net_id;
222 lpn->lpn_sel_priority = LNET_MAX_SELECTION_PRIORITY;
224 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
230 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
232 struct lnet_peer *lp;
234 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
236 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
237 LASSERT(list_empty(&lpn->lpn_peer_nis));
238 LASSERT(list_empty(&lpn->lpn_peer_nets));
240 lpn->lpn_peer = NULL;
241 LIBCFS_FREE(lpn, sizeof(*lpn));
243 lnet_peer_decref_locked(lp);
246 static struct lnet_peer *
247 lnet_peer_alloc(lnet_nid_t nid)
249 struct lnet_peer *lp;
251 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
255 INIT_LIST_HEAD(&lp->lp_rtrq);
256 INIT_LIST_HEAD(&lp->lp_routes);
257 INIT_LIST_HEAD(&lp->lp_peer_list);
258 INIT_LIST_HEAD(&lp->lp_peer_nets);
259 INIT_LIST_HEAD(&lp->lp_dc_list);
260 INIT_LIST_HEAD(&lp->lp_dc_pendq);
261 INIT_LIST_HEAD(&lp->lp_rtr_list);
262 init_waitqueue_head(&lp->lp_dc_waitq);
263 spin_lock_init(&lp->lp_lock);
264 lp->lp_primary_nid = nid;
265 lp->lp_disc_src_nid = LNET_NID_ANY;
266 if (lnet_peers_start_down())
267 lp->lp_alive = false;
272 * all peers created on a router should have health on
273 * if it's not already on.
275 if (the_lnet.ln_routing && !lnet_health_sensitivity)
276 lp->lp_health_sensitivity = 1;
279 * Turn off discovery for loopback peer. If you're creating a peer
280 * for the loopback interface then that was initiated when we
281 * attempted to send a message over the loopback. There is no need
282 * to ever use a different interface when sending messages to
285 if (nid == LNET_NID_LO_0)
286 lp->lp_state = LNET_PEER_NO_DISCOVERY;
287 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
289 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
295 lnet_destroy_peer_locked(struct lnet_peer *lp)
297 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
299 LASSERT(atomic_read(&lp->lp_refcount) == 0);
300 LASSERT(lp->lp_rtr_refcount == 0);
301 LASSERT(list_empty(&lp->lp_peer_nets));
302 LASSERT(list_empty(&lp->lp_peer_list));
303 LASSERT(list_empty(&lp->lp_dc_list));
306 lnet_ping_buffer_decref(lp->lp_data);
309 * if there are messages still on the pending queue, then make
310 * sure to queue them on the ln_msg_resend list so they can be
311 * resent at a later point if the discovery thread is still
313 * If the discovery thread has stopped, then the wakeup will be a
314 * no-op, and it is expected the lnet_shutdown_lndnets() will
315 * eventually be called, which will traverse this list and
316 * finalize the messages on the list.
317 * We can not resend them now because we're holding the cpt lock.
318 * Releasing the lock can cause an inconsistent state
320 spin_lock(&the_lnet.ln_msg_resend_lock);
321 spin_lock(&lp->lp_lock);
322 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
323 spin_unlock(&lp->lp_lock);
324 spin_unlock(&the_lnet.ln_msg_resend_lock);
325 wake_up(&the_lnet.ln_dc_waitq);
327 LIBCFS_FREE(lp, sizeof(*lp));
331 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
332 * that peer_net, detach the peer_net from the peer.
334 * Call with lnet_net_lock/EX held
337 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
339 struct lnet_peer_table *ptable;
340 struct lnet_peer_net *lpn;
341 struct lnet_peer *lp;
344 * Belts and suspenders: gracefully handle teardown of a
345 * partially connected peer_ni.
347 lpn = lpni->lpni_peer_net;
349 list_del_init(&lpni->lpni_peer_nis);
351 * If there are no lpni's left, we detach lpn from
352 * lp_peer_nets, so it cannot be found anymore.
354 if (list_empty(&lpn->lpn_peer_nis))
355 list_del_init(&lpn->lpn_peer_nets);
357 /* Update peer NID count. */
362 * If there are no more peer nets, make the peer unfindable
363 * via the peer_tables.
365 * Otherwise, if the peer is DISCOVERED, tell discovery to
366 * take another look at it. This is a no-op if discovery for
367 * this peer did the detaching.
369 if (list_empty(&lp->lp_peer_nets)) {
370 list_del_init(&lp->lp_peer_list);
371 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
373 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
374 /* Discovery isn't running, nothing to do here. */
375 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
376 lnet_peer_queue_for_discovery(lp);
377 wake_up(&the_lnet.ln_dc_waitq);
379 CDEBUG(D_NET, "peer %s NID %s\n",
380 libcfs_nid2str(lp->lp_primary_nid),
381 libcfs_nid2str(lpni->lpni_nid));
384 /* called with lnet_net_lock LNET_LOCK_EX held */
386 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
388 struct lnet_peer_table *ptable = NULL;
390 /* don't remove a peer_ni if it's also a gateway */
391 if (lnet_isrouter(lpni) && !force) {
392 CERROR("Peer NI %s is a gateway. Can not delete it\n",
393 libcfs_nid2str(lpni->lpni_nid));
397 lnet_peer_remove_from_remote_list(lpni);
399 /* remove peer ni from the hash list. */
400 list_del_init(&lpni->lpni_hashlist);
403 * indicate the peer is being deleted so the monitor thread can
404 * remove it from the recovery queue.
406 spin_lock(&lpni->lpni_lock);
407 lpni->lpni_state |= LNET_PEER_NI_DELETING;
408 spin_unlock(&lpni->lpni_lock);
410 /* decrement the ref count on the peer table */
411 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
414 * The peer_ni can no longer be found with a lookup. But there
415 * can be current users, so keep track of it on the zombie
416 * list until the reference count has gone to zero.
418 * The last reference may be lost in a place where the
419 * lnet_net_lock locks only a single cpt, and that cpt may not
420 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
423 spin_lock(&ptable->pt_zombie_lock);
424 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
425 ptable->pt_zombies++;
426 spin_unlock(&ptable->pt_zombie_lock);
428 /* no need to keep this peer_ni on the hierarchy anymore */
429 lnet_peer_detach_peer_ni_locked(lpni);
431 /* remove hashlist reference on peer_ni */
432 lnet_peer_ni_decref_locked(lpni);
437 void lnet_peer_uninit(void)
439 struct lnet_peer_ni *lpni, *tmp;
441 lnet_net_lock(LNET_LOCK_EX);
443 /* remove all peer_nis from the remote peer and the hash list */
444 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
445 lpni_on_remote_peer_ni_list)
446 lnet_peer_ni_del_locked(lpni, false);
448 lnet_peer_tables_destroy();
450 lnet_net_unlock(LNET_LOCK_EX);
454 lnet_peer_del_locked(struct lnet_peer *peer)
456 struct lnet_peer_ni *lpni = NULL, *lpni2;
459 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
461 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
462 while (lpni != NULL) {
463 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
464 rc = lnet_peer_ni_del_locked(lpni, false);
474 lnet_peer_del(struct lnet_peer *peer)
476 lnet_net_lock(LNET_LOCK_EX);
477 lnet_peer_del_locked(peer);
478 lnet_net_unlock(LNET_LOCK_EX);
484 * Delete a NID from a peer. Call with ln_api_mutex held.
487 * -EPERM: Non-DLC deletion from DLC-configured peer.
488 * -ENOENT: No lnet_peer_ni corresponding to the nid.
489 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
490 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
493 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
495 struct lnet_peer_ni *lpni;
496 lnet_nid_t primary_nid = lp->lp_primary_nid;
498 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
500 if (!(flags & LNET_PEER_CONFIGURED)) {
501 if (lp->lp_state & LNET_PEER_CONFIGURED) {
506 lpni = lnet_find_peer_ni_locked(nid);
511 lnet_peer_ni_decref_locked(lpni);
512 if (lp != lpni->lpni_peer_net->lpn_peer) {
518 * This function only allows deletion of the primary NID if it
521 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
526 lnet_net_lock(LNET_LOCK_EX);
528 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
529 struct lnet_peer_ni *lpni2;
530 /* assign the next peer_ni to be the primary */
531 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
533 lp->lp_primary_nid = lpni2->lpni_nid;
535 rc = lnet_peer_ni_del_locked(lpni, force);
537 lnet_net_unlock(LNET_LOCK_EX);
540 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
541 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
547 lnet_peer_table_cleanup_locked(struct lnet_net *net,
548 struct lnet_peer_table *ptable)
551 struct lnet_peer_ni *next;
552 struct lnet_peer_ni *lpni;
553 struct lnet_peer *peer;
555 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
556 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
558 if (net != NULL && net != lpni->lpni_net)
561 peer = lpni->lpni_peer_net->lpn_peer;
562 if (peer->lp_primary_nid != lpni->lpni_nid) {
563 lnet_peer_ni_del_locked(lpni, false);
567 * Removing the primary NID implies removing
568 * the entire peer. Advance next beyond any
569 * peer_ni that belongs to the same peer.
571 list_for_each_entry_from(next, &ptable->pt_hash[i],
573 if (next->lpni_peer_net->lpn_peer != peer)
576 lnet_peer_del_locked(peer);
582 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
584 wait_var_event_warning(&ptable->pt_zombies,
585 ptable->pt_zombies == 0,
586 "Waiting for %d zombies on peer table\n",
591 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
592 struct lnet_peer_table *ptable)
594 struct lnet_peer_ni *lp;
595 struct lnet_peer_ni *tmp;
599 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
600 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
602 if (net != lp->lpni_net)
605 if (!lnet_isrouter(lp))
608 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
610 lnet_net_unlock(LNET_LOCK_EX);
611 lnet_del_route(LNET_NET_ANY, gw_nid);
612 lnet_net_lock(LNET_LOCK_EX);
618 lnet_peer_tables_cleanup(struct lnet_net *net)
621 struct lnet_peer_table *ptable;
623 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
624 /* If just deleting the peers for a NI, get rid of any routes these
625 * peers are gateways for. */
626 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
627 lnet_net_lock(LNET_LOCK_EX);
628 lnet_peer_table_del_rtrs_locked(net, ptable);
629 lnet_net_unlock(LNET_LOCK_EX);
632 /* Start the cleanup process */
633 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
634 lnet_net_lock(LNET_LOCK_EX);
635 lnet_peer_table_cleanup_locked(net, ptable);
636 lnet_net_unlock(LNET_LOCK_EX);
639 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
640 lnet_peer_ni_finalize_wait(ptable);
643 static struct lnet_peer_ni *
644 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
646 struct list_head *peers;
647 struct lnet_peer_ni *lp;
649 if (the_lnet.ln_state != LNET_STATE_RUNNING)
652 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
653 list_for_each_entry(lp, peers, lpni_hashlist) {
654 if (lp->lpni_nid == nid) {
655 lnet_peer_ni_addref_locked(lp);
663 struct lnet_peer_ni *
664 lnet_find_peer_ni_locked(lnet_nid_t nid)
666 struct lnet_peer_ni *lpni;
667 struct lnet_peer_table *ptable;
670 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
672 ptable = the_lnet.ln_peer_tables[cpt];
673 lpni = lnet_get_peer_ni_locked(ptable, nid);
678 struct lnet_peer_ni *
679 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
681 struct lnet_peer_net *lpn;
682 struct lnet_peer_ni *lpni;
684 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
688 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
689 if (lpni->lpni_nid == nid)
697 lnet_find_peer(lnet_nid_t nid)
699 struct lnet_peer_ni *lpni;
700 struct lnet_peer *lp = NULL;
703 cpt = lnet_net_lock_current();
704 lpni = lnet_find_peer_ni_locked(nid);
706 lp = lpni->lpni_peer_net->lpn_peer;
707 lnet_peer_addref_locked(lp);
708 lnet_peer_ni_decref_locked(lpni);
710 lnet_net_unlock(cpt);
715 struct lnet_peer_net *
716 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
718 struct lnet_peer_net *net;
721 /* no net id provided return the first net */
722 net = list_first_entry_or_null(&lp->lp_peer_nets,
723 struct lnet_peer_net,
729 /* find the net after the one provided */
730 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
731 if (net->lpn_net_id == prev_lpn_id) {
733 * if we reached the end of the list loop to the
736 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
737 return list_first_entry_or_null(&lp->lp_peer_nets,
738 struct lnet_peer_net,
741 return list_next_entry(net, lpn_peer_nets);
748 struct lnet_peer_ni *
749 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
750 struct lnet_peer_net *peer_net,
751 struct lnet_peer_ni *prev)
753 struct lnet_peer_ni *lpni;
754 struct lnet_peer_net *net = peer_net;
758 if (list_empty(&peer->lp_peer_nets))
761 net = list_entry(peer->lp_peer_nets.next,
762 struct lnet_peer_net,
765 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
771 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
773 * if you reached the end of the peer ni list and the peer
774 * net is specified then there are no more peer nis in that
781 * we reached the end of this net ni list. move to the
784 if (prev->lpni_peer_net->lpn_peer_nets.next ==
786 /* no more nets and no more NIs. */
789 /* get the next net */
790 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
791 struct lnet_peer_net,
793 /* get the ni on it */
794 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
800 /* there are more nis left */
801 lpni = list_entry(prev->lpni_peer_nis.next,
802 struct lnet_peer_ni, lpni_peer_nis);
807 /* Call with the ln_api_mutex held */
808 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
810 struct lnet_process_id id;
811 struct lnet_peer_table *ptable;
812 struct lnet_peer *lp;
821 if (the_lnet.ln_state != LNET_STATE_RUNNING)
824 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
827 * Count the number of peers, and return E2BIG if the buffer
828 * is too small. We'll also return the desired size.
831 for (cpt = 0; cpt < lncpt; cpt++) {
832 ptable = the_lnet.ln_peer_tables[cpt];
833 count += ptable->pt_peers;
835 size = count * sizeof(*ids);
840 * Walk the peer lists and copy out the primary nids.
841 * This is safe because the peer lists are only modified
842 * while the ln_api_mutex is held. So we don't need to
843 * hold the lnet_net_lock as well, and can therefore
844 * directly call copy_to_user().
847 memset(&id, 0, sizeof(id));
848 id.pid = LNET_PID_LUSTRE;
850 for (cpt = 0; cpt < lncpt; cpt++) {
851 ptable = the_lnet.ln_peer_tables[cpt];
852 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
855 id.nid = lp->lp_primary_nid;
856 if (copy_to_user(&ids[i], &id, sizeof(id)))
869 * Start pushes to peers that need to be updated for a configuration
870 * change on this node.
873 lnet_push_update_to_peers(int force)
875 struct lnet_peer_table *ptable;
876 struct lnet_peer *lp;
880 lnet_net_lock(LNET_LOCK_EX);
881 if (lnet_peer_discovery_disabled)
883 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
884 for (cpt = 0; cpt < lncpt; cpt++) {
885 ptable = the_lnet.ln_peer_tables[cpt];
886 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
888 spin_lock(&lp->lp_lock);
889 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
890 lp->lp_state |= LNET_PEER_FORCE_PUSH;
891 spin_unlock(&lp->lp_lock);
893 if (lnet_peer_needs_push(lp))
894 lnet_peer_queue_for_discovery(lp);
897 lnet_net_unlock(LNET_LOCK_EX);
898 wake_up(&the_lnet.ln_dc_waitq);
901 /* find the NID in the preferred gateways for the remote peer
903 * false: list is not empty and NID is not preferred
904 * false: list is empty
905 * true: nid is found in the list
908 lnet_peer_is_pref_rtr_locked(struct lnet_peer_ni *lpni,
911 struct lnet_nid_list *ne;
913 CDEBUG(D_NET, "%s: rtr pref emtpy: %d\n",
914 libcfs_nid2str(lpni->lpni_nid),
915 list_empty(&lpni->lpni_rtr_pref_nids));
917 if (list_empty(&lpni->lpni_rtr_pref_nids))
920 /* iterate through all the preferred NIDs and see if any of them
921 * matches the provided gw_nid
923 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
924 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
925 libcfs_nid2str(ne->nl_nid),
926 libcfs_nid2str(gw_nid));
927 if (ne->nl_nid == gw_nid)
935 lnet_peer_clr_pref_rtrs(struct lnet_peer_ni *lpni)
937 struct list_head zombies;
938 struct lnet_nid_list *ne;
939 struct lnet_nid_list *tmp;
940 int cpt = lpni->lpni_cpt;
942 INIT_LIST_HEAD(&zombies);
945 list_splice_init(&lpni->lpni_rtr_pref_nids, &zombies);
946 lnet_net_unlock(cpt);
948 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
949 list_del(&ne->nl_list);
950 LIBCFS_FREE(ne, sizeof(*ne));
955 lnet_peer_add_pref_rtr(struct lnet_peer_ni *lpni,
958 int cpt = lpni->lpni_cpt;
959 struct lnet_nid_list *ne = NULL;
961 /* This function is called with api_mutex held. When the api_mutex
962 * is held the list can not be modified, as it is only modified as
963 * a result of applying a UDSP and that happens under api_mutex
966 __must_hold(&the_lnet.ln_api_mutex);
968 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
969 if (ne->nl_nid == gw_nid)
973 LIBCFS_CPT_ALLOC(ne, lnet_cpt_table(), cpt, sizeof(*ne));
979 /* Lock the cpt to protect against addition and checks in the
980 * selection algorithm
983 list_add(&ne->nl_list, &lpni->lpni_rtr_pref_nids);
984 lnet_net_unlock(cpt);
990 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
991 * this is a preferred point-to-point path. Call with lnet_net_lock in
995 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
997 struct lnet_nid_list *ne;
999 if (lpni->lpni_pref_nnids == 0)
1001 if (lpni->lpni_pref_nnids == 1)
1002 return lpni->lpni_pref.nid == nid;
1003 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1004 if (ne->nl_nid == nid)
1011 * Set a single ni as preferred, provided no preferred ni is already
1012 * defined. Only to be used for non-multi-rail peer_ni.
1015 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1019 spin_lock(&lpni->lpni_lock);
1020 if (nid == LNET_NID_ANY) {
1022 } else if (lpni->lpni_pref_nnids > 0) {
1024 } else if (lpni->lpni_pref_nnids == 0) {
1025 lpni->lpni_pref.nid = nid;
1026 lpni->lpni_pref_nnids = 1;
1027 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
1029 spin_unlock(&lpni->lpni_lock);
1031 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1032 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
1037 * Clear the preferred NID from a non-multi-rail peer_ni, provided
1038 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
1041 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
1045 spin_lock(&lpni->lpni_lock);
1046 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
1047 lpni->lpni_pref_nnids = 0;
1048 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1049 } else if (lpni->lpni_pref_nnids == 0) {
1054 spin_unlock(&lpni->lpni_lock);
1056 CDEBUG(D_NET, "peer %s: %d\n",
1057 libcfs_nid2str(lpni->lpni_nid), rc);
1062 * Clear the preferred NIDs from a non-multi-rail peer.
1065 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
1067 struct lnet_peer_ni *lpni = NULL;
1069 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
1070 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
1074 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1076 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1077 struct lnet_nid_list *ne1 = NULL;
1078 struct lnet_nid_list *ne2 = NULL;
1079 lnet_nid_t tmp_nid = LNET_NID_ANY;
1082 if (nid == LNET_NID_ANY) {
1087 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1092 /* A non-MR node may have only one preferred NI per peer_ni */
1093 if (lpni->lpni_pref_nnids > 0 &&
1094 !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1099 /* add the new preferred nid to the list of preferred nids */
1100 if (lpni->lpni_pref_nnids != 0) {
1101 size_t alloc_size = sizeof(*ne1);
1103 if (lpni->lpni_pref_nnids == 1) {
1104 tmp_nid = lpni->lpni_pref.nid;
1105 INIT_LIST_HEAD(&lpni->lpni_pref.nids);
1108 list_for_each_entry(ne1, &lpni->lpni_pref.nids, nl_list) {
1109 if (ne1->nl_nid == nid) {
1115 LIBCFS_CPT_ALLOC(ne1, lnet_cpt_table(), lpni->lpni_cpt,
1122 /* move the originally stored nid to the list */
1123 if (lpni->lpni_pref_nnids == 1) {
1124 LIBCFS_CPT_ALLOC(ne2, lnet_cpt_table(),
1125 lpni->lpni_cpt, alloc_size);
1130 INIT_LIST_HEAD(&ne2->nl_list);
1131 ne2->nl_nid = tmp_nid;
1136 lnet_net_lock(LNET_LOCK_EX);
1137 spin_lock(&lpni->lpni_lock);
1138 if (lpni->lpni_pref_nnids == 0) {
1139 lpni->lpni_pref.nid = nid;
1142 list_add_tail(&ne2->nl_list, &lpni->lpni_pref.nids);
1143 list_add_tail(&ne1->nl_list, &lpni->lpni_pref.nids);
1145 lpni->lpni_pref_nnids++;
1146 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1147 spin_unlock(&lpni->lpni_lock);
1148 lnet_net_unlock(LNET_LOCK_EX);
1151 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1152 spin_lock(&lpni->lpni_lock);
1153 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1154 spin_unlock(&lpni->lpni_lock);
1156 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1157 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1162 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1164 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1165 struct lnet_nid_list *ne = NULL;
1168 if (lpni->lpni_pref_nnids == 0) {
1173 if (lpni->lpni_pref_nnids == 1) {
1174 if (lpni->lpni_pref.nid != nid) {
1179 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1180 if (ne->nl_nid == nid)
1181 goto remove_nid_entry;
1189 lnet_net_lock(LNET_LOCK_EX);
1190 spin_lock(&lpni->lpni_lock);
1191 if (lpni->lpni_pref_nnids == 1)
1192 lpni->lpni_pref.nid = LNET_NID_ANY;
1194 list_del_init(&ne->nl_list);
1195 if (lpni->lpni_pref_nnids == 2) {
1196 struct lnet_nid_list *ne, *tmp;
1198 list_for_each_entry_safe(ne, tmp,
1199 &lpni->lpni_pref.nids,
1201 lpni->lpni_pref.nid = ne->nl_nid;
1202 list_del_init(&ne->nl_list);
1203 LIBCFS_FREE(ne, sizeof(*ne));
1207 lpni->lpni_pref_nnids--;
1208 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1209 spin_unlock(&lpni->lpni_lock);
1210 lnet_net_unlock(LNET_LOCK_EX);
1213 LIBCFS_FREE(ne, sizeof(*ne));
1215 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1216 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1221 lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni)
1223 struct list_head zombies;
1224 struct lnet_nid_list *ne;
1225 struct lnet_nid_list *tmp;
1227 INIT_LIST_HEAD(&zombies);
1229 lnet_net_lock(LNET_LOCK_EX);
1230 if (lpni->lpni_pref_nnids == 1)
1231 lpni->lpni_pref.nid = LNET_NID_ANY;
1232 else if (lpni->lpni_pref_nnids > 1)
1233 list_splice_init(&lpni->lpni_pref.nids, &zombies);
1234 lpni->lpni_pref_nnids = 0;
1235 lnet_net_unlock(LNET_LOCK_EX);
1237 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1238 list_del_init(&ne->nl_list);
1239 LIBCFS_FREE(ne, sizeof(*ne));
1244 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1246 struct lnet_peer_ni *lpni;
1247 lnet_nid_t primary_nid = nid;
1249 lpni = lnet_find_peer_ni_locked(nid);
1251 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1252 lnet_peer_ni_decref_locked(lpni);
1259 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1260 __must_hold(&lp->lp_lock)
1262 if (lnet_peer_discovery_disabled)
1265 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1266 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1277 lnet_is_discovery_disabled(struct lnet_peer *lp)
1281 spin_lock(&lp->lp_lock);
1282 rc = lnet_is_discovery_disabled_locked(lp);
1283 spin_unlock(&lp->lp_lock);
1289 LNetPrimaryNID(lnet_nid_t nid)
1291 struct lnet_peer *lp;
1292 struct lnet_peer_ni *lpni;
1293 lnet_nid_t primary_nid = nid;
1297 if (nid == LNET_NID_LO_0)
1298 return LNET_NID_LO_0;
1300 cpt = lnet_net_lock_current();
1301 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1306 lp = lpni->lpni_peer_net->lpn_peer;
1308 while (!lnet_peer_is_uptodate(lp)) {
1309 spin_lock(&lp->lp_lock);
1310 /* force a full discovery cycle */
1311 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1312 spin_unlock(&lp->lp_lock);
1314 rc = lnet_discover_peer_locked(lpni, cpt, true);
1317 lp = lpni->lpni_peer_net->lpn_peer;
1319 /* Only try once if discovery is disabled */
1320 if (lnet_is_discovery_disabled(lp))
1323 primary_nid = lp->lp_primary_nid;
1325 lnet_peer_ni_decref_locked(lpni);
1327 lnet_net_unlock(cpt);
1329 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1330 libcfs_nid2str(primary_nid), rc);
1333 EXPORT_SYMBOL(LNetPrimaryNID);
1335 struct lnet_peer_net *
1336 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1338 struct lnet_peer_net *peer_net;
1339 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1340 if (peer_net->lpn_net_id == net_id)
1347 * Attach a peer_ni to a peer_net and peer. This function assumes
1348 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1349 * may be attached to a different peer, in which case it will be
1350 * properly detached first. The whole operation is done atomically.
1352 * This function consumes the reference on lpni and Always returns 0.
1353 * This is the last function called from functions that do return an
1354 * int, so returning 0 here allows the compiler to do a tail call.
1357 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1358 struct lnet_peer_net *lpn,
1359 struct lnet_peer_ni *lpni,
1362 struct lnet_peer_table *ptable;
1364 /* Install the new peer_ni */
1365 lnet_net_lock(LNET_LOCK_EX);
1366 /* Add peer_ni to global peer table hash, if necessary. */
1367 if (list_empty(&lpni->lpni_hashlist)) {
1368 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1370 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1371 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1372 ptable->pt_version++;
1373 lnet_peer_ni_addref_locked(lpni);
1376 /* Detach the peer_ni from an existing peer, if necessary. */
1377 if (lpni->lpni_peer_net) {
1378 LASSERT(lpni->lpni_peer_net != lpn);
1379 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1380 lnet_peer_detach_peer_ni_locked(lpni);
1381 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1382 lpni->lpni_peer_net = NULL;
1385 /* Add peer_ni to peer_net */
1386 lpni->lpni_peer_net = lpn;
1387 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1388 lnet_update_peer_net_healthv(lpni);
1389 lnet_peer_net_addref_locked(lpn);
1391 /* Add peer_net to peer */
1392 if (!lpn->lpn_peer) {
1394 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1395 lnet_peer_addref_locked(lp);
1398 /* Add peer to global peer list, if necessary */
1399 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1400 if (list_empty(&lp->lp_peer_list)) {
1401 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1406 /* Update peer state */
1407 spin_lock(&lp->lp_lock);
1408 if (flags & LNET_PEER_CONFIGURED) {
1409 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1410 lp->lp_state |= LNET_PEER_CONFIGURED;
1412 if (flags & LNET_PEER_MULTI_RAIL) {
1413 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1414 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1415 lnet_peer_clr_non_mr_pref_nids(lp);
1418 spin_unlock(&lp->lp_lock);
1422 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1423 libcfs_nid2str(lp->lp_primary_nid),
1424 libcfs_nid2str(lpni->lpni_nid), flags);
1425 lnet_peer_ni_decref_locked(lpni);
1426 lnet_net_unlock(LNET_LOCK_EX);
1432 * Create a new peer, with nid as its primary nid.
1434 * Call with the lnet_api_mutex held.
1437 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1439 struct lnet_peer *lp;
1440 struct lnet_peer_net *lpn;
1441 struct lnet_peer_ni *lpni;
1444 LASSERT(nid != LNET_NID_ANY);
1447 * No need for the lnet_net_lock here, because the
1448 * lnet_api_mutex is held.
1450 lpni = lnet_find_peer_ni_locked(nid);
1452 /* A peer with this NID already exists. */
1453 lp = lpni->lpni_peer_net->lpn_peer;
1454 lnet_peer_ni_decref_locked(lpni);
1456 * This is an error if the peer was configured and the
1457 * primary NID differs or an attempt is made to change
1458 * the Multi-Rail flag. Otherwise the assumption is
1459 * that an existing peer is being modified.
1461 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1462 if (lp->lp_primary_nid != nid)
1464 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1468 /* Delete and recreate as a configured peer. */
1472 /* Create peer, peer_net, and peer_ni. */
1474 lp = lnet_peer_alloc(nid);
1477 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1480 lpni = lnet_peer_ni_alloc(nid);
1484 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1487 LIBCFS_FREE(lpn, sizeof(*lpn));
1489 LIBCFS_FREE(lp, sizeof(*lp));
1491 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1492 libcfs_nid2str(nid), flags, rc);
1497 * Add a NID to a peer. Call with ln_api_mutex held.
1500 * -EPERM: Non-DLC addition to a DLC-configured peer.
1501 * -EEXIST: The NID was configured by DLC for a different peer.
1502 * -ENOMEM: Out of memory.
1503 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1504 * non-multi-rail peer.
1507 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1509 struct lnet_peer_net *lpn;
1510 struct lnet_peer_ni *lpni;
1514 LASSERT(nid != LNET_NID_ANY);
1516 /* A configured peer can only be updated through configuration. */
1517 if (!(flags & LNET_PEER_CONFIGURED)) {
1518 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1525 * The MULTI_RAIL flag can be set but not cleared, because
1526 * that would leave the peer struct in an invalid state.
1528 if (flags & LNET_PEER_MULTI_RAIL) {
1529 spin_lock(&lp->lp_lock);
1530 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1531 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1532 lnet_peer_clr_non_mr_pref_nids(lp);
1534 spin_unlock(&lp->lp_lock);
1535 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1540 lpni = lnet_find_peer_ni_locked(nid);
1543 * A peer_ni already exists. This is only a problem if
1544 * it is not connected to this peer and was configured
1547 if (lpni->lpni_peer_net->lpn_peer == lp)
1549 if (lnet_peer_ni_is_configured(lpni)) {
1553 /* If this is the primary NID, destroy the peer. */
1554 if (lnet_peer_ni_is_primary(lpni)) {
1555 struct lnet_peer *rtr_lp =
1556 lpni->lpni_peer_net->lpn_peer;
1557 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1559 * if we're trying to delete a router it means
1560 * we're moving this peer NI to a new peer so must
1561 * transfer router properties to the new peer
1563 if (rtr_refcount > 0) {
1564 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1565 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1567 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1568 lnet_peer_ni_decref_locked(lpni);
1569 lpni = lnet_peer_ni_alloc(nid);
1576 lpni = lnet_peer_ni_alloc(nid);
1584 * Get the peer_net. Check that we're not adding a second
1585 * peer_ni on a peer_net of a non-multi-rail peer.
1587 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1589 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1594 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1599 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1602 lnet_peer_ni_decref_locked(lpni);
1604 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1605 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1611 * Update the primary NID of a peer, if possible.
1613 * Call with the lnet_api_mutex held.
1616 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1618 lnet_nid_t old = lp->lp_primary_nid;
1621 if (lp->lp_primary_nid == nid)
1623 rc = lnet_peer_add_nid(lp, nid, flags);
1626 lp->lp_primary_nid = nid;
1628 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1629 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1634 * lpni creation initiated due to traffic either sending or receiving.
1637 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1639 struct lnet_peer *lp;
1640 struct lnet_peer_net *lpn;
1641 struct lnet_peer_ni *lpni;
1645 if (nid == LNET_NID_ANY) {
1650 /* lnet_net_lock is not needed here because ln_api_lock is held */
1651 lpni = lnet_find_peer_ni_locked(nid);
1654 * We must have raced with another thread. Since we
1655 * know next to nothing about a peer_ni created by
1656 * traffic, we just assume everything is ok and
1659 lnet_peer_ni_decref_locked(lpni);
1663 /* Create peer, peer_net, and peer_ni. */
1665 lp = lnet_peer_alloc(nid);
1668 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1671 lpni = lnet_peer_ni_alloc(nid);
1674 if (pref != LNET_NID_ANY)
1675 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1677 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1680 LIBCFS_FREE(lpn, sizeof(*lpn));
1682 LIBCFS_FREE(lp, sizeof(*lp));
1684 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1689 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1691 * This API handles the following combinations:
1692 * Create a peer with its primary NI if only the prim_nid is provided
1693 * Add a NID to a peer identified by the prim_nid. The peer identified
1694 * by the prim_nid must already exist.
1695 * The peer being created may be non-MR.
1697 * The caller must hold ln_api_mutex. This prevents the peer from
1698 * being created/modified/deleted by a different thread.
1701 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1703 struct lnet_peer *lp = NULL;
1704 struct lnet_peer_ni *lpni;
1707 /* The prim_nid must always be specified */
1708 if (prim_nid == LNET_NID_ANY)
1711 flags = LNET_PEER_CONFIGURED;
1713 flags |= LNET_PEER_MULTI_RAIL;
1716 * If nid isn't specified, we must create a new peer with
1717 * prim_nid as its primary nid.
1719 if (nid == LNET_NID_ANY)
1720 return lnet_peer_add(prim_nid, flags);
1722 /* Look up the prim_nid, which must exist. */
1723 lpni = lnet_find_peer_ni_locked(prim_nid);
1726 lnet_peer_ni_decref_locked(lpni);
1727 lp = lpni->lpni_peer_net->lpn_peer;
1729 /* Peer must have been configured. */
1730 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1731 CDEBUG(D_NET, "peer %s was not configured\n",
1732 libcfs_nid2str(prim_nid));
1736 /* Primary NID must match */
1737 if (lp->lp_primary_nid != prim_nid) {
1738 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1739 libcfs_nid2str(prim_nid),
1740 libcfs_nid2str(lp->lp_primary_nid));
1744 /* Multi-Rail flag must match. */
1745 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1746 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1747 libcfs_nid2str(prim_nid));
1751 return lnet_peer_add_nid(lp, nid, flags);
1755 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1757 * This API handles the following combinations:
1758 * Delete a NI from a peer if both prim_nid and nid are provided.
1759 * Delete a peer if only prim_nid is provided.
1760 * Delete a peer if its primary nid is provided.
1762 * The caller must hold ln_api_mutex. This prevents the peer from
1763 * being modified/deleted by a different thread.
1766 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1768 struct lnet_peer *lp;
1769 struct lnet_peer_ni *lpni;
1772 if (prim_nid == LNET_NID_ANY)
1775 lpni = lnet_find_peer_ni_locked(prim_nid);
1778 lnet_peer_ni_decref_locked(lpni);
1779 lp = lpni->lpni_peer_net->lpn_peer;
1781 if (prim_nid != lp->lp_primary_nid) {
1782 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1783 libcfs_nid2str(prim_nid),
1784 libcfs_nid2str(lp->lp_primary_nid));
1788 lnet_net_lock(LNET_LOCK_EX);
1789 if (lp->lp_rtr_refcount > 0) {
1790 lnet_net_unlock(LNET_LOCK_EX);
1791 CERROR("%s is a router. Can not be deleted\n",
1792 libcfs_nid2str(prim_nid));
1795 lnet_net_unlock(LNET_LOCK_EX);
1797 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1798 return lnet_peer_del(lp);
1800 flags = LNET_PEER_CONFIGURED;
1801 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1802 flags |= LNET_PEER_MULTI_RAIL;
1804 return lnet_peer_del_nid(lp, nid, flags);
1808 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1810 struct lnet_peer_table *ptable;
1811 struct lnet_peer_net *lpn;
1813 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1815 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1816 LASSERT(list_empty(&lpni->lpni_txq));
1817 LASSERT(lpni->lpni_txqnob == 0);
1818 LASSERT(list_empty(&lpni->lpni_peer_nis));
1819 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1821 lpn = lpni->lpni_peer_net;
1822 lpni->lpni_peer_net = NULL;
1823 lpni->lpni_net = NULL;
1825 if (!list_empty(&lpni->lpni_hashlist)) {
1826 /* remove the peer ni from the zombie list */
1827 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1828 spin_lock(&ptable->pt_zombie_lock);
1829 list_del_init(&lpni->lpni_hashlist);
1830 ptable->pt_zombies--;
1831 spin_unlock(&ptable->pt_zombie_lock);
1834 if (lpni->lpni_pref_nnids > 1) {
1835 struct lnet_nid_list *ne, *tmp;
1837 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
1839 list_del_init(&ne->nl_list);
1840 LIBCFS_FREE(ne, sizeof(*ne));
1843 LIBCFS_FREE(lpni, sizeof(*lpni));
1846 lnet_peer_net_decref_locked(lpn);
1849 struct lnet_peer_ni *
1850 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1852 struct lnet_peer_ni *lpni = NULL;
1855 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1856 return ERR_PTR(-ESHUTDOWN);
1859 * find if a peer_ni already exists.
1860 * If so then just return that.
1862 lpni = lnet_find_peer_ni_locked(nid);
1866 lnet_net_unlock(cpt);
1868 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1871 goto out_net_relock;
1874 lpni = lnet_find_peer_ni_locked(nid);
1884 * Get a peer_ni for the given nid, create it if necessary. Takes a
1885 * hold on the peer_ni.
1887 struct lnet_peer_ni *
1888 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1890 struct lnet_peer_ni *lpni = NULL;
1893 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1894 return ERR_PTR(-ESHUTDOWN);
1897 * find if a peer_ni already exists.
1898 * If so then just return that.
1900 lpni = lnet_find_peer_ni_locked(nid);
1906 * use the lnet_api_mutex to serialize the creation of the peer_ni
1907 * and the creation/deletion of the local ni/net. When a local ni is
1908 * created, if there exists a set of peer_nis on that network,
1909 * they need to be traversed and updated. When a local NI is
1910 * deleted, which could result in a network being deleted, then
1911 * all peer nis on that network need to be removed as well.
1913 * Creation through traffic should also be serialized with
1914 * creation through DLC.
1916 lnet_net_unlock(cpt);
1917 mutex_lock(&the_lnet.ln_api_mutex);
1919 * Shutdown is only set under the ln_api_lock, so a single
1920 * check here is sufficent.
1922 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1923 lpni = ERR_PTR(-ESHUTDOWN);
1924 goto out_mutex_unlock;
1927 rc = lnet_peer_ni_traffic_add(nid, pref);
1930 goto out_mutex_unlock;
1933 lpni = lnet_find_peer_ni_locked(nid);
1937 mutex_unlock(&the_lnet.ln_api_mutex);
1940 /* Lock has been dropped, check again for shutdown. */
1941 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1943 lnet_peer_ni_decref_locked(lpni);
1944 lpni = ERR_PTR(-ESHUTDOWN);
1951 lnet_peer_gw_discovery(struct lnet_peer *lp)
1955 spin_lock(&lp->lp_lock);
1956 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1958 spin_unlock(&lp->lp_lock);
1964 lnet_peer_is_uptodate(struct lnet_peer *lp)
1968 spin_lock(&lp->lp_lock);
1969 rc = lnet_peer_is_uptodate_locked(lp);
1970 spin_unlock(&lp->lp_lock);
1975 * Is a peer uptodate from the point of view of discovery?
1977 * If it is currently being processed, obviously not.
1978 * A forced Ping or Push is also handled by the discovery thread.
1980 * Otherwise look at whether the peer needs rediscovering.
1983 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
1984 __must_hold(&lp->lp_lock)
1988 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1989 LNET_PEER_FORCE_PING |
1990 LNET_PEER_FORCE_PUSH)) {
1992 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1994 } else if (lnet_peer_needs_push(lp)) {
1996 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1997 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
2009 * Queue a peer for the attention of the discovery thread. Call with
2010 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
2011 * -EALREADY if the peer was already queued.
2013 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
2017 spin_lock(&lp->lp_lock);
2018 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2019 lp->lp_state |= LNET_PEER_DISCOVERING;
2020 spin_unlock(&lp->lp_lock);
2021 if (list_empty(&lp->lp_dc_list)) {
2022 lnet_peer_addref_locked(lp);
2023 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2024 wake_up(&the_lnet.ln_dc_waitq);
2030 CDEBUG(D_NET, "Queue peer %s: %d\n",
2031 libcfs_nid2str(lp->lp_primary_nid), rc);
2037 * Discovery of a peer is complete. Wake all waiters on the peer.
2038 * Call with lnet_net_lock/EX held.
2040 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
2042 struct lnet_msg *msg, *tmp;
2044 LIST_HEAD(pending_msgs);
2046 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
2047 libcfs_nid2str(lp->lp_primary_nid));
2049 list_del_init(&lp->lp_dc_list);
2050 spin_lock(&lp->lp_lock);
2051 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
2052 spin_unlock(&lp->lp_lock);
2053 wake_up_all(&lp->lp_dc_waitq);
2055 if (lp->lp_rtr_refcount > 0)
2056 lnet_router_discovery_complete(lp);
2058 lnet_net_unlock(LNET_LOCK_EX);
2060 /* iterate through all pending messages and send them again */
2061 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
2062 list_del_init(&msg->msg_list);
2063 if (lp->lp_dc_error) {
2064 lnet_finalize(msg, lp->lp_dc_error);
2068 CDEBUG(D_NET, "sending pending message %s to target %s\n",
2069 lnet_msgtyp2str(msg->msg_type),
2070 libcfs_id2str(msg->msg_target));
2071 rc = lnet_send(msg->msg_src_nid_param, msg,
2072 msg->msg_rtr_nid_param);
2074 CNETERR("Error sending %s to %s: %d\n",
2075 lnet_msgtyp2str(msg->msg_type),
2076 libcfs_id2str(msg->msg_target), rc);
2077 lnet_finalize(msg, rc);
2080 lnet_net_lock(LNET_LOCK_EX);
2081 lnet_peer_decref_locked(lp);
2085 * Handle inbound push.
2086 * Like any event handler, called with lnet_res_lock/CPT held.
2088 void lnet_peer_push_event(struct lnet_event *ev)
2090 struct lnet_ping_buffer *pbuf;
2091 struct lnet_peer *lp;
2093 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
2095 /* lnet_find_peer() adds a refcount */
2096 lp = lnet_find_peer(ev->source.nid);
2098 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
2099 libcfs_nid2str(ev->initiator.nid),
2100 libcfs_nid2str(ev->source.nid));
2101 pbuf->pb_needs_post = true;
2105 /* Ensure peer state remains consistent while we modify it. */
2106 spin_lock(&lp->lp_lock);
2109 * If some kind of error happened the contents of the message
2110 * cannot be used. Clear the NIDS_UPTODATE and set the
2111 * FORCE_PING flag to trigger a ping.
2114 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2115 lp->lp_state |= LNET_PEER_FORCE_PING;
2116 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2118 libcfs_nid2str(lp->lp_primary_nid),
2119 libcfs_nid2str(ev->source.nid));
2124 * A push with invalid or corrupted info. Clear the UPTODATE
2125 * flag to trigger a ping.
2127 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2128 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2129 lp->lp_state |= LNET_PEER_FORCE_PING;
2130 CDEBUG(D_NET, "Corrupted Push from %s\n",
2131 libcfs_nid2str(lp->lp_primary_nid));
2136 * Make sure we'll allocate the correct size ping buffer when
2139 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2140 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2143 * A non-Multi-Rail peer is not supposed to be capable of
2146 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2147 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2148 libcfs_nid2str(lp->lp_primary_nid));
2153 * The peer may have discovery disabled at its end. Set
2154 * NO_DISCOVERY as appropriate.
2156 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2157 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2158 libcfs_nid2str(lp->lp_primary_nid));
2160 * Mark the peer for deletion if we already know about it
2161 * and it's going from discovery set to no discovery set
2163 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2164 LNET_PEER_DISCOVERING)) &&
2165 lp->lp_state & LNET_PEER_DISCOVERED) {
2166 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2167 libcfs_nid2str(lp->lp_primary_nid),
2169 lp->lp_state |= LNET_PEER_MARK_DELETION;
2171 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2172 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2173 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2174 libcfs_nid2str(lp->lp_primary_nid));
2175 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2179 * Update the MULTI_RAIL flag based on the push. If the peer
2180 * was configured with DLC then the setting should match what
2182 * NB: We verified above that the MR feature bit is set in pi_features
2184 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2185 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2186 libcfs_nid2str(lp->lp_primary_nid), lp);
2187 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2188 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2189 libcfs_nid2str(lp->lp_primary_nid));
2190 } else if (lnet_peer_discovery_disabled) {
2191 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2192 libcfs_nid2str(lp->lp_primary_nid), lp);
2193 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2194 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2195 libcfs_nid2str(lp->lp_primary_nid), lp);
2197 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2198 libcfs_nid2str(lp->lp_primary_nid), lp);
2199 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2200 lnet_peer_clr_non_mr_pref_nids(lp);
2204 * Check for truncation of the Put message. Clear the
2205 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2206 * and tell discovery to allocate a bigger buffer.
2208 if (ev->mlength < ev->rlength) {
2209 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2210 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2211 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2212 lp->lp_state |= LNET_PEER_FORCE_PING;
2213 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2214 libcfs_nid2str(lp->lp_primary_nid),
2215 pbuf->pb_info.pi_nnis);
2219 /* always assume new data */
2220 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2221 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2224 * If there is data present that hasn't been processed yet,
2225 * we'll replace it if the Put contained newer data and it
2226 * fits. We're racing with a Ping or earlier Push in this
2229 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2230 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2231 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2232 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2233 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2234 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2235 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2236 libcfs_nid2str(lp->lp_primary_nid),
2237 LNET_PING_BUFFER_SEQNO(pbuf),
2238 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2244 * Allocate a buffer to copy the data. On a failure we drop
2245 * the Push and set FORCE_PING to force the discovery
2246 * thread to fix the problem by pinging the peer.
2248 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2250 lp->lp_state |= LNET_PEER_FORCE_PING;
2251 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2252 libcfs_nid2str(lp->lp_primary_nid),
2253 LNET_PING_BUFFER_SEQNO(pbuf));
2258 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2259 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2260 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2261 CDEBUG(D_NET, "Received Push %s %u\n",
2262 libcfs_nid2str(lp->lp_primary_nid),
2263 LNET_PING_BUFFER_SEQNO(pbuf));
2266 /* We've processed this buffer. It can be reposted */
2267 pbuf->pb_needs_post = true;
2270 * Queue the peer for discovery if not done, force it on the request
2271 * queue and wake the discovery thread if the peer was already queued,
2272 * because its status changed.
2274 spin_unlock(&lp->lp_lock);
2275 lnet_net_lock(LNET_LOCK_EX);
2276 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2277 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2278 wake_up(&the_lnet.ln_dc_waitq);
2280 /* Drop refcount from lookup */
2281 lnet_peer_decref_locked(lp);
2282 lnet_net_unlock(LNET_LOCK_EX);
2286 * Clear the discovery error state, unless we're already discovering
2287 * this peer, in which case the error is current.
2289 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2291 spin_lock(&lp->lp_lock);
2292 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2293 lp->lp_dc_error = 0;
2294 spin_unlock(&lp->lp_lock);
2298 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2299 * dropped/retaken within this function. An lnet_peer_ni is passed in
2300 * because discovery could tear down an lnet_peer.
2303 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2306 struct lnet_peer *lp;
2311 lnet_net_unlock(cpt);
2312 lnet_net_lock(LNET_LOCK_EX);
2313 lp = lpni->lpni_peer_net->lpn_peer;
2314 lnet_peer_clear_discovery_error(lp);
2317 * We're willing to be interrupted. The lpni can become a
2318 * zombie if we race with DLC, so we must check for that.
2321 /* Keep lp alive when the lnet_net_lock is unlocked */
2322 lnet_peer_addref_locked(lp);
2323 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2324 if (signal_pending(current))
2326 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2329 * Don't repeat discovery if discovery is disabled. This is
2330 * done to ensure we can use discovery as a standard ping as
2331 * well for backwards compatibility with routers which do not
2332 * have discovery or have discovery disabled
2334 if (lnet_is_discovery_disabled(lp) && count > 0)
2336 if (lp->lp_dc_error)
2338 if (lnet_peer_is_uptodate(lp))
2340 lnet_peer_queue_for_discovery(lp);
2342 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2345 * If caller requested a non-blocking operation then
2346 * return immediately. Once discovery is complete any
2347 * pending messages that were stopped due to discovery
2348 * will be transmitted.
2353 lnet_net_unlock(LNET_LOCK_EX);
2355 finish_wait(&lp->lp_dc_waitq, &wait);
2356 lnet_net_lock(LNET_LOCK_EX);
2357 lnet_peer_decref_locked(lp);
2358 /* Peer may have changed */
2359 lp = lpni->lpni_peer_net->lpn_peer;
2361 finish_wait(&lp->lp_dc_waitq, &wait);
2363 lnet_net_unlock(LNET_LOCK_EX);
2365 lnet_peer_decref_locked(lp);
2367 * The peer may have changed, so re-check and rediscover if that turns
2368 * out to have been the case. The reference count on lp ensured that
2369 * even if it was unlinked from lpni the memory could not be recycled.
2370 * Thus the check below is sufficient to determine whether the peer
2371 * changed. If the peer changed, then lp must not be dereferenced.
2373 if (lp != lpni->lpni_peer_net->lpn_peer)
2376 if (signal_pending(current))
2378 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2380 else if (lp->lp_dc_error)
2381 rc = lp->lp_dc_error;
2383 CDEBUG(D_NET, "non-blocking discovery\n");
2384 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2387 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2388 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2389 libcfs_nid2str(lpni->lpni_nid), rc,
2390 (!block) ? "pending discovery" : "discovery complete");
2395 /* Handle an incoming ack for a push. */
2397 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2399 struct lnet_ping_buffer *pbuf;
2401 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2402 spin_lock(&lp->lp_lock);
2403 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2404 lp->lp_push_error = ev->status;
2406 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2408 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2409 spin_unlock(&lp->lp_lock);
2411 CDEBUG(D_NET, "peer %s ev->status %d\n",
2412 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2415 /* Handle a Reply message. This is the reply to a Ping message. */
2417 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2419 struct lnet_ping_buffer *pbuf;
2422 spin_lock(&lp->lp_lock);
2424 lp->lp_disc_src_nid = ev->target.nid;
2427 * If some kind of error happened the contents of message
2428 * cannot be used. Set PING_FAILED to trigger a retry.
2431 lp->lp_state |= LNET_PEER_PING_FAILED;
2432 lp->lp_ping_error = ev->status;
2433 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2435 libcfs_nid2str(lp->lp_primary_nid),
2436 libcfs_nid2str(ev->source.nid));
2440 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2441 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2442 lnet_swap_pinginfo(pbuf);
2445 * A reply with invalid or corrupted info. Set PING_FAILED to
2448 rc = lnet_ping_info_validate(&pbuf->pb_info);
2450 lp->lp_state |= LNET_PEER_PING_FAILED;
2451 lp->lp_ping_error = 0;
2452 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2453 libcfs_nid2str(lp->lp_primary_nid), rc);
2459 * The peer may have discovery disabled at its end. Set
2460 * NO_DISCOVERY as appropriate.
2462 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2463 !lnet_peer_discovery_disabled) {
2464 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2465 libcfs_nid2str(lp->lp_primary_nid));
2466 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2468 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2469 libcfs_nid2str(lp->lp_primary_nid));
2470 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2474 * Update the MULTI_RAIL flag based on the reply. If the peer
2475 * was configured with DLC then the setting should match what
2478 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2479 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2480 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2481 libcfs_nid2str(lp->lp_primary_nid), lp);
2482 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2483 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2484 libcfs_nid2str(lp->lp_primary_nid));
2485 } else if (lnet_peer_discovery_disabled) {
2487 "peer %s(%p) not MR: DD disabled locally\n",
2488 libcfs_nid2str(lp->lp_primary_nid), lp);
2489 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2491 "peer %s(%p) not MR: DD disabled remotely\n",
2492 libcfs_nid2str(lp->lp_primary_nid), lp);
2494 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2495 libcfs_nid2str(lp->lp_primary_nid), lp);
2496 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2497 lnet_peer_clr_non_mr_pref_nids(lp);
2499 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2500 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2501 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2502 libcfs_nid2str(lp->lp_primary_nid));
2504 CERROR("Multi-Rail state vanished from %s\n",
2505 libcfs_nid2str(lp->lp_primary_nid));
2506 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2511 * Make sure we'll allocate the correct size ping buffer when
2514 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2515 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2518 * Check for truncation of the Reply. Clear PING_SENT and set
2519 * PING_FAILED to trigger a retry.
2521 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2522 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2523 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2524 lp->lp_state |= LNET_PEER_PING_FAILED;
2525 lp->lp_ping_error = 0;
2526 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2527 libcfs_nid2str(lp->lp_primary_nid),
2528 pbuf->pb_info.pi_nnis);
2533 * Check the sequence numbers in the reply. These are only
2534 * available if the reply came from a Multi-Rail peer.
2536 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2537 pbuf->pb_info.pi_nnis > 1 &&
2538 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2539 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2540 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2541 libcfs_nid2str(lp->lp_primary_nid),
2542 LNET_PING_BUFFER_SEQNO(pbuf),
2545 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2548 /* We're happy with the state of the data in the buffer. */
2549 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2550 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2551 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2552 lnet_ping_buffer_decref(lp->lp_data);
2554 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2555 lnet_ping_buffer_addref(pbuf);
2558 lp->lp_state &= ~LNET_PEER_PING_SENT;
2559 spin_unlock(&lp->lp_lock);
2561 lnet_net_lock(LNET_LOCK_EX);
2563 * If this peer is a gateway, call the routing callback to
2564 * handle the ping reply
2566 if (lp->lp_rtr_refcount > 0)
2567 lnet_router_discovery_ping_reply(lp);
2568 lnet_net_unlock(LNET_LOCK_EX);
2572 * Send event handling. Only matters for error cases, where we clean
2573 * up state on the peer and peer_ni that would otherwise be updated in
2574 * the REPLY event handler for a successful Ping, and the ACK event
2575 * handler for a successful Push.
2578 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2585 spin_lock(&lp->lp_lock);
2586 if (ev->msg_type == LNET_MSG_GET) {
2587 lp->lp_state &= ~LNET_PEER_PING_SENT;
2588 lp->lp_state |= LNET_PEER_PING_FAILED;
2589 lp->lp_ping_error = ev->status;
2590 } else { /* ev->msg_type == LNET_MSG_PUT */
2591 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2592 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2593 lp->lp_push_error = ev->status;
2595 spin_unlock(&lp->lp_lock);
2596 rc = LNET_REDISCOVER_PEER;
2598 CDEBUG(D_NET, "%s Send to %s: %d\n",
2599 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2600 libcfs_nid2str(ev->target.nid), rc);
2605 * Unlink event handling. This event is only seen if a call to
2606 * LNetMDUnlink() caused the event to be unlinked. If this call was
2607 * made after the event was set up in LNetGet() or LNetPut() then we
2608 * assume the Ping or Push timed out.
2611 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2613 spin_lock(&lp->lp_lock);
2614 /* We've passed through LNetGet() */
2615 if (lp->lp_state & LNET_PEER_PING_SENT) {
2616 lp->lp_state &= ~LNET_PEER_PING_SENT;
2617 lp->lp_state |= LNET_PEER_PING_FAILED;
2618 lp->lp_ping_error = -ETIMEDOUT;
2619 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2620 libcfs_nid2str(lp->lp_primary_nid));
2622 /* We've passed through LNetPut() */
2623 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2624 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2625 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2626 lp->lp_push_error = -ETIMEDOUT;
2627 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2628 libcfs_nid2str(lp->lp_primary_nid));
2630 spin_unlock(&lp->lp_lock);
2634 * Event handler for the discovery EQ.
2636 * Called with lnet_res_lock(cpt) held. The cpt is the
2637 * lnet_cpt_of_cookie() of the md handle cookie.
2639 static void lnet_discovery_event_handler(struct lnet_event *event)
2641 struct lnet_peer *lp = event->md_user_ptr;
2642 struct lnet_ping_buffer *pbuf;
2645 /* discovery needs to take another look */
2646 rc = LNET_REDISCOVER_PEER;
2648 CDEBUG(D_NET, "Received event: %d\n", event->type);
2650 switch (event->type) {
2651 case LNET_EVENT_ACK:
2652 lnet_discovery_event_ack(lp, event);
2654 case LNET_EVENT_REPLY:
2655 lnet_discovery_event_reply(lp, event);
2657 case LNET_EVENT_SEND:
2658 /* Only send failure triggers a retry. */
2659 rc = lnet_discovery_event_send(lp, event);
2661 case LNET_EVENT_UNLINK:
2662 /* LNetMDUnlink() was called */
2663 lnet_discovery_event_unlink(lp, event);
2666 /* Invalid events. */
2669 lnet_net_lock(LNET_LOCK_EX);
2670 if (event->unlinked) {
2671 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2672 lnet_ping_buffer_decref(pbuf);
2673 lnet_peer_decref_locked(lp);
2676 /* put peer back at end of request queue, if discovery not already
2678 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2679 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2680 wake_up(&the_lnet.ln_dc_waitq);
2682 lnet_net_unlock(LNET_LOCK_EX);
2686 * Build a peer from incoming data.
2688 * The NIDs in the incoming data are supposed to be structured as follows:
2691 * - other NIDs in same net
2692 * - NIDs in second net
2693 * - NIDs in third net
2695 * This due to the way the list of NIDs in the data is created.
2697 * Note that this function will mark the peer uptodate unless an
2698 * ENOMEM is encontered. All other errors are due to a conflict
2699 * between the DLC configuration and what discovery sees. We treat DLC
2700 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2701 * peer from becoming stuck in discovery.
2703 static int lnet_peer_merge_data(struct lnet_peer *lp,
2704 struct lnet_ping_buffer *pbuf)
2706 struct lnet_peer_ni *lpni;
2707 lnet_nid_t *curnis = NULL;
2708 struct lnet_ni_status *addnis = NULL;
2709 lnet_nid_t *delnis = NULL;
2719 flags = LNET_PEER_DISCOVERED;
2720 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2721 flags |= LNET_PEER_MULTI_RAIL;
2724 * Cache the routing feature for the peer; whether it is enabled
2725 * for disabled as reported by the remote peer.
2727 spin_lock(&lp->lp_lock);
2728 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2729 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2731 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2732 spin_unlock(&lp->lp_lock);
2734 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2735 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2736 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2737 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2738 if (!curnis || !addnis || !delnis) {
2746 /* Construct the list of NIDs present in peer. */
2748 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2749 curnis[ncurnis++] = lpni->lpni_nid;
2752 * Check for NIDs in pbuf not present in curnis[].
2753 * The loop starts at 1 to skip the loopback NID.
2755 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2756 for (j = 0; j < ncurnis; j++)
2757 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2760 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2763 * Check for NIDs in curnis[] not present in pbuf.
2764 * The nested loop starts at 1 to skip the loopback NID.
2766 * But never add the loopback NID to delnis[]: if it is
2767 * present in curnis[] then this peer is for this node.
2769 for (i = 0; i < ncurnis; i++) {
2770 if (curnis[i] == LNET_NID_LO_0)
2772 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2773 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2775 * update the information we cache for the
2776 * peer with the latest information we
2779 lpni = lnet_find_peer_ni_locked(curnis[i]);
2781 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2782 lnet_peer_ni_decref_locked(lpni);
2787 if (j == pbuf->pb_info.pi_nnis)
2788 delnis[ndelnis++] = curnis[i];
2792 * If we get here and the discovery is disabled then we don't want
2793 * to add or delete any NIs. We just updated the ones we have some
2794 * information on, and call it a day
2797 if (lnet_is_discovery_disabled(lp))
2800 for (i = 0; i < naddnis; i++) {
2801 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2803 CERROR("Error adding NID %s to peer %s: %d\n",
2804 libcfs_nid2str(addnis[i].ns_nid),
2805 libcfs_nid2str(lp->lp_primary_nid), rc);
2809 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2811 lpni->lpni_ns_status = addnis[i].ns_status;
2812 lnet_peer_ni_decref_locked(lpni);
2816 for (i = 0; i < ndelnis; i++) {
2818 * for routers it's okay to delete the primary_nid because
2819 * the upper layers don't really rely on it. So if we're
2820 * being told that the router changed its primary_nid
2821 * then it's okay to delete it.
2823 if (lp->lp_rtr_refcount > 0)
2824 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2825 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2827 CERROR("Error deleting NID %s from peer %s: %d\n",
2828 libcfs_nid2str(delnis[i]),
2829 libcfs_nid2str(lp->lp_primary_nid), rc);
2835 * Errors other than -ENOMEM are due to peers having been
2836 * configured with DLC. Ignore these because DLC overrides
2841 CFS_FREE_PTR_ARRAY(curnis, nnis);
2842 CFS_FREE_PTR_ARRAY(addnis, nnis);
2843 CFS_FREE_PTR_ARRAY(delnis, nnis);
2844 lnet_ping_buffer_decref(pbuf);
2845 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2848 spin_lock(&lp->lp_lock);
2849 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2850 lp->lp_state |= LNET_PEER_FORCE_PING;
2851 spin_unlock(&lp->lp_lock);
2857 * The data in pbuf says lp is its primary peer, but the data was
2858 * received by a different peer. Try to update lp with the data.
2861 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2863 struct lnet_handle_md mdh;
2865 /* Queue lp for discovery, and force it on the request queue. */
2866 lnet_net_lock(LNET_LOCK_EX);
2867 if (lnet_peer_queue_for_discovery(lp))
2868 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2869 lnet_net_unlock(LNET_LOCK_EX);
2871 LNetInvalidateMDHandle(&mdh);
2874 * Decide whether we can move the peer to the DATA_PRESENT state.
2876 * We replace stale data for a multi-rail peer, repair PING_FAILED
2877 * status, and preempt FORCE_PING.
2879 * If after that we have DATA_PRESENT, we merge it into this peer.
2881 spin_lock(&lp->lp_lock);
2882 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2883 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2884 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2885 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2886 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2887 lnet_ping_buffer_decref(pbuf);
2892 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2893 lnet_ping_buffer_decref(lp->lp_data);
2895 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2897 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2898 mdh = lp->lp_ping_mdh;
2899 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2900 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2901 lp->lp_ping_error = 0;
2903 if (lp->lp_state & LNET_PEER_FORCE_PING)
2904 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2905 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2906 spin_unlock(&lp->lp_lock);
2908 if (!LNetMDHandleIsInvalid(mdh))
2912 return lnet_peer_merge_data(lp, pbuf);
2914 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2918 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2922 for (i = 0; i < pinfo->pi_nnis; i++) {
2923 if (pinfo->pi_ni[i].ns_nid == nid)
2931 * Update a peer using the data received.
2933 static int lnet_peer_data_present(struct lnet_peer *lp)
2934 __must_hold(&lp->lp_lock)
2936 struct lnet_ping_buffer *pbuf;
2937 struct lnet_peer_ni *lpni;
2938 lnet_nid_t nid = LNET_NID_ANY;
2944 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2945 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2946 spin_unlock(&lp->lp_lock);
2949 * Modifications of peer structures are done while holding the
2950 * ln_api_mutex. A global lock is required because we may be
2951 * modifying multiple peer structures, and a mutex greatly
2952 * simplifies memory management.
2954 * The actual changes to the data structures must also protect
2955 * against concurrent lookups, for which the lnet_net_lock in
2956 * LNET_LOCK_EX mode is used.
2958 mutex_lock(&the_lnet.ln_api_mutex);
2959 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2965 * If this peer is not on the peer list then it is being torn
2966 * down, and our reference count may be all that is keeping it
2967 * alive. Don't do any work on it.
2969 if (list_empty(&lp->lp_peer_list))
2972 flags = LNET_PEER_DISCOVERED;
2973 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2974 flags |= LNET_PEER_MULTI_RAIL;
2977 * Check whether the primary NID in the message matches the
2978 * primary NID of the peer. If it does, update the peer, if
2979 * it it does not, check whether there is already a peer with
2980 * that primary NID. If no such peer exists, try to update
2981 * the primary NID of the current peer (allowed if it was
2982 * created due to message traffic) and complete the update.
2983 * If the peer did exist, hand off the data to it.
2985 * The peer for the loopback interface is a special case: this
2986 * is the peer for the local node, and we want to set its
2987 * primary NID to the correct value here. Moreover, this peer
2988 * can show up with only the loopback NID in the ping buffer.
2990 if (pbuf->pb_info.pi_nnis <= 1)
2992 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2993 if (lp->lp_primary_nid == LNET_NID_LO_0) {
2994 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2996 rc = lnet_peer_merge_data(lp, pbuf);
2998 * if the primary nid of the peer is present in the ping info returned
2999 * from the peer, but it's not the local primary peer we have
3000 * cached and discovery is disabled, then we don't want to update
3001 * our local peer info, by adding or removing NIDs, we just want
3002 * to update the status of the nids that we currently have
3003 * recorded in that peer.
3005 } else if (lp->lp_primary_nid == nid ||
3006 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
3007 lnet_is_discovery_disabled(lp))) {
3008 rc = lnet_peer_merge_data(lp, pbuf);
3010 lpni = lnet_find_peer_ni_locked(nid);
3012 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3014 CERROR("Primary NID error %s versus %s: %d\n",
3015 libcfs_nid2str(lp->lp_primary_nid),
3016 libcfs_nid2str(nid), rc);
3018 rc = lnet_peer_merge_data(lp, pbuf);
3021 struct lnet_peer *new_lp;
3022 new_lp = lpni->lpni_peer_net->lpn_peer;
3024 * if lp has discovery/MR enabled that means new_lp
3025 * should have discovery/MR enabled as well, since
3026 * it's the same peer, which we're about to merge
3028 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
3029 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
3030 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
3031 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
3033 rc = lnet_peer_set_primary_data(new_lp, pbuf);
3034 lnet_consolidate_routes_locked(lp, new_lp);
3035 lnet_peer_ni_decref_locked(lpni);
3039 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
3041 mutex_unlock(&the_lnet.ln_api_mutex);
3043 spin_lock(&lp->lp_lock);
3044 /* Tell discovery to re-check the peer immediately. */
3046 rc = LNET_REDISCOVER_PEER;
3051 * A ping failed. Clear the PING_FAILED state and set the
3052 * FORCE_PING state, to ensure a retry even if discovery is
3053 * disabled. This avoids being left with incorrect state.
3055 static int lnet_peer_ping_failed(struct lnet_peer *lp)
3056 __must_hold(&lp->lp_lock)
3058 struct lnet_handle_md mdh;
3061 mdh = lp->lp_ping_mdh;
3062 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3063 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3064 lp->lp_state |= LNET_PEER_FORCE_PING;
3065 rc = lp->lp_ping_error;
3066 lp->lp_ping_error = 0;
3067 spin_unlock(&lp->lp_lock);
3069 if (!LNetMDHandleIsInvalid(mdh))
3072 CDEBUG(D_NET, "peer %s:%d\n",
3073 libcfs_nid2str(lp->lp_primary_nid), rc);
3075 spin_lock(&lp->lp_lock);
3076 return rc ? rc : LNET_REDISCOVER_PEER;
3080 * Select NID to send a Ping or Push to.
3082 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
3084 struct lnet_peer_ni *lpni;
3086 /* Look for a direct-connected NID for this peer. */
3088 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3089 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
3094 return lpni->lpni_nid;
3096 /* Look for a routed-connected NID for this peer. */
3098 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3099 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
3104 return lpni->lpni_nid;
3106 return LNET_NID_ANY;
3109 /* Active side of ping. */
3110 static int lnet_peer_send_ping(struct lnet_peer *lp)
3111 __must_hold(&lp->lp_lock)
3118 lp->lp_state |= LNET_PEER_PING_SENT;
3119 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3120 spin_unlock(&lp->lp_lock);
3122 cpt = lnet_net_lock_current();
3123 /* Refcount for MD. */
3124 lnet_peer_addref_locked(lp);
3125 pnid = lnet_peer_select_nid(lp);
3126 lnet_net_unlock(cpt);
3128 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3130 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
3131 the_lnet.ln_dc_handler, false);
3134 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3135 * refcount on the peer, otherwise LNetMDUnlink will be called
3136 * which will eventually do that.
3140 lnet_peer_decref_locked(lp);
3141 lnet_net_unlock(cpt);
3142 rc = -rc; /* change the rc to negative value */
3144 } else if (rc < 0) {
3148 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3150 spin_lock(&lp->lp_lock);
3154 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3156 * The errors that get us here are considered hard errors and
3157 * cause Discovery to terminate. So we clear PING_SENT, but do
3158 * not set either PING_FAILED or FORCE_PING. In fact we need
3159 * to clear PING_FAILED, because the unlink event handler will
3160 * have set it if we called LNetMDUnlink() above.
3162 spin_lock(&lp->lp_lock);
3163 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3168 * This function exists because you cannot call LNetMDUnlink() from an
3171 static int lnet_peer_push_failed(struct lnet_peer *lp)
3172 __must_hold(&lp->lp_lock)
3174 struct lnet_handle_md mdh;
3177 mdh = lp->lp_push_mdh;
3178 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3179 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3180 rc = lp->lp_push_error;
3181 lp->lp_push_error = 0;
3182 spin_unlock(&lp->lp_lock);
3184 if (!LNetMDHandleIsInvalid(mdh))
3187 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3188 spin_lock(&lp->lp_lock);
3189 return rc ? rc : LNET_REDISCOVER_PEER;
3193 * Mark the peer as discovered.
3195 static int lnet_peer_discovered(struct lnet_peer *lp)
3196 __must_hold(&lp->lp_lock)
3198 lp->lp_state |= LNET_PEER_DISCOVERED;
3199 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3200 LNET_PEER_REDISCOVER);
3202 lp->lp_dc_error = 0;
3204 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3209 /* Active side of push. */
3210 static int lnet_peer_send_push(struct lnet_peer *lp)
3211 __must_hold(&lp->lp_lock)
3213 struct lnet_ping_buffer *pbuf;
3214 struct lnet_process_id id;
3219 /* Don't push to a non-multi-rail peer. */
3220 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3221 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3222 /* if peer's NIDs are uptodate then peer is discovered */
3223 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3224 rc = lnet_peer_discovered(lp);
3231 lp->lp_state |= LNET_PEER_PUSH_SENT;
3232 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3233 spin_unlock(&lp->lp_lock);
3235 cpt = lnet_net_lock_current();
3236 pbuf = the_lnet.ln_ping_target;
3237 lnet_ping_buffer_addref(pbuf);
3238 lnet_net_unlock(cpt);
3240 /* Push source MD */
3241 md.start = &pbuf->pb_info;
3242 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3243 md.threshold = 2; /* Put/Ack */
3245 md.options = LNET_MD_TRACK_RESPONSE;
3246 md.handler = the_lnet.ln_dc_handler;
3249 rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3251 lnet_ping_buffer_decref(pbuf);
3252 CERROR("Can't bind push source MD: %d\n", rc);
3255 cpt = lnet_net_lock_current();
3256 /* Refcount for MD. */
3257 lnet_peer_addref_locked(lp);
3258 id.pid = LNET_PID_LUSTRE;
3259 id.nid = lnet_peer_select_nid(lp);
3260 lnet_net_unlock(cpt);
3262 if (id.nid == LNET_NID_ANY) {
3267 rc = LNetPut(lp->lp_disc_src_nid, lp->lp_push_mdh,
3268 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3269 LNET_PROTO_PING_MATCHBITS, 0, 0);
3272 * reset the discovery nid. There is no need to restrict sending
3273 * from that source, if we call lnet_push_update_to_peers(). It'll
3274 * get set to a specific NID, if we initiate discovery from the
3277 lp->lp_disc_src_nid = LNET_NID_ANY;
3282 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3284 spin_lock(&lp->lp_lock);
3288 LNetMDUnlink(lp->lp_push_mdh);
3289 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3291 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3293 * The errors that get us here are considered hard errors and
3294 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3295 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3296 * because the unlink event handler will have set it if we
3297 * called LNetMDUnlink() above.
3299 spin_lock(&lp->lp_lock);
3300 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3305 * An unrecoverable error was encountered during discovery.
3306 * Set error status in peer and abort discovery.
3308 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3310 CDEBUG(D_NET, "Discovery error %s: %d\n",
3311 libcfs_nid2str(lp->lp_primary_nid), error);
3313 spin_lock(&lp->lp_lock);
3314 lp->lp_dc_error = error;
3315 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3316 lp->lp_state |= LNET_PEER_REDISCOVER;
3317 spin_unlock(&lp->lp_lock);
3321 * Discovering this peer is taking too long. Cancel any Ping or Push
3322 * that discovery is waiting on by unlinking the relevant MDs. The
3323 * lnet_discovery_event_handler() will proceed from here and complete
3326 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3328 struct lnet_handle_md ping_mdh;
3329 struct lnet_handle_md push_mdh;
3331 LNetInvalidateMDHandle(&ping_mdh);
3332 LNetInvalidateMDHandle(&push_mdh);
3334 spin_lock(&lp->lp_lock);
3335 if (lp->lp_state & LNET_PEER_PING_SENT) {
3336 ping_mdh = lp->lp_ping_mdh;
3337 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3339 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3340 push_mdh = lp->lp_push_mdh;
3341 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3343 spin_unlock(&lp->lp_lock);
3345 if (!LNetMDHandleIsInvalid(ping_mdh))
3346 LNetMDUnlink(ping_mdh);
3347 if (!LNetMDHandleIsInvalid(push_mdh))
3348 LNetMDUnlink(push_mdh);
3352 * Wait for work to be queued or some other change that must be
3353 * attended to. Returns non-zero if the discovery thread should shut
3356 static int lnet_peer_discovery_wait_for_work(void)
3363 cpt = lnet_net_lock_current();
3365 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3366 TASK_INTERRUPTIBLE);
3367 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3369 if (lnet_push_target_resize_needed() ||
3370 the_lnet.ln_push_target->pb_needs_post)
3372 if (!list_empty(&the_lnet.ln_dc_request))
3374 if (!list_empty(&the_lnet.ln_msg_resend))
3376 lnet_net_unlock(cpt);
3379 * wakeup max every second to check if there are peers that
3380 * have been stuck on the working queue for greater than
3383 schedule_timeout(cfs_time_seconds(1));
3384 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3385 cpt = lnet_net_lock_current();
3387 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3389 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3392 lnet_net_unlock(cpt);
3394 CDEBUG(D_NET, "woken: %d\n", rc);
3400 * Messages that were pending on a destroyed peer will be put on a global
3401 * resend list. The message resend list will be checked by
3402 * the discovery thread when it wakes up, and will resend messages. These
3403 * messages can still be sendable in the case the lpni which was the initial
3404 * cause of the message re-queue was transfered to another peer.
3406 * It is possible that LNet could be shutdown while we're iterating
3407 * through the list. lnet_shudown_lndnets() will attempt to access the
3408 * resend list, but will have to wait until the spinlock is released, by
3409 * which time there shouldn't be any more messages on the resend list.
3410 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3411 * for the messages so they can be released. The other case is that
3412 * lnet_shudown_lndnets() can finalize all the messages before this
3413 * function can visit the resend list, in which case this function will be
3416 static void lnet_resend_msgs(void)
3418 struct lnet_msg *msg, *tmp;
3422 spin_lock(&the_lnet.ln_msg_resend_lock);
3423 list_splice(&the_lnet.ln_msg_resend, &resend);
3424 spin_unlock(&the_lnet.ln_msg_resend_lock);
3426 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3427 list_del_init(&msg->msg_list);
3428 rc = lnet_send(msg->msg_src_nid_param, msg,
3429 msg->msg_rtr_nid_param);
3431 CNETERR("Error sending %s to %s: %d\n",
3432 lnet_msgtyp2str(msg->msg_type),
3433 libcfs_id2str(msg->msg_target), rc);
3434 lnet_finalize(msg, rc);
3439 /* The discovery thread. */
3440 static int lnet_peer_discovery(void *arg)
3442 struct lnet_peer *lp;
3445 wait_for_completion(&the_lnet.ln_started);
3447 CDEBUG(D_NET, "started\n");
3450 if (lnet_peer_discovery_wait_for_work())
3453 if (lnet_push_target_resize_needed())
3454 lnet_push_target_resize();
3455 else if (the_lnet.ln_push_target->pb_needs_post)
3456 lnet_push_target_post(the_lnet.ln_push_target,
3457 &the_lnet.ln_push_target_md);
3461 lnet_net_lock(LNET_LOCK_EX);
3462 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3463 lnet_net_unlock(LNET_LOCK_EX);
3468 * Process all incoming discovery work requests. When
3469 * discovery must wait on a peer to change state, it
3470 * is added to the tail of the ln_dc_working queue. A
3471 * timestamp keeps track of when the peer was added,
3472 * so we can time out discovery requests that take too
3475 while (!list_empty(&the_lnet.ln_dc_request)) {
3476 lp = list_first_entry(&the_lnet.ln_dc_request,
3477 struct lnet_peer, lp_dc_list);
3478 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3480 * set the time the peer was put on the dc_working
3481 * queue. It shouldn't remain on the queue
3482 * forever, in case the GET message (for ping)
3483 * doesn't get a REPLY or the PUT message (for
3484 * push) doesn't get an ACK.
3486 lp->lp_last_queued = ktime_get_real_seconds();
3487 lnet_net_unlock(LNET_LOCK_EX);
3489 if (lnet_push_target_resize_needed())
3490 lnet_push_target_resize();
3491 else if (the_lnet.ln_push_target->pb_needs_post)
3492 lnet_push_target_post(the_lnet.ln_push_target,
3493 &the_lnet.ln_push_target_md);
3496 * Select an action depending on the state of
3497 * the peer and whether discovery is disabled.
3498 * The check whether discovery is disabled is
3499 * done after the code that handles processing
3500 * for arrived data, cleanup for failures, and
3501 * forcing a Ping or Push.
3503 spin_lock(&lp->lp_lock);
3504 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3505 libcfs_nid2str(lp->lp_primary_nid), lp,
3507 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3508 rc = lnet_peer_data_present(lp);
3509 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3510 rc = lnet_peer_ping_failed(lp);
3511 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3512 rc = lnet_peer_push_failed(lp);
3513 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3514 rc = lnet_peer_send_ping(lp);
3515 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3516 rc = lnet_peer_send_push(lp);
3517 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3518 rc = lnet_peer_send_ping(lp);
3519 else if (lnet_peer_needs_push(lp))
3520 rc = lnet_peer_send_push(lp);
3522 rc = lnet_peer_discovered(lp);
3523 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3524 libcfs_nid2str(lp->lp_primary_nid), lp,
3526 spin_unlock(&lp->lp_lock);
3528 lnet_net_lock(LNET_LOCK_EX);
3529 if (rc == LNET_REDISCOVER_PEER) {
3530 list_move(&lp->lp_dc_list,
3531 &the_lnet.ln_dc_request);
3533 lnet_peer_discovery_error(lp, rc);
3535 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3536 lnet_peer_discovery_complete(lp);
3537 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3540 if (lp->lp_state & LNET_PEER_MARK_DELETION) {
3541 struct list_head rlist;
3542 struct lnet_route *route, *tmp;
3543 int sensitivity = lp->lp_health_sensitivity;
3545 INIT_LIST_HEAD(&rlist);
3548 * remove the peer from the discovery work
3549 * queue if it's on there in preparation
3552 if (!list_empty(&lp->lp_dc_list))
3553 list_del(&lp->lp_dc_list);
3555 lnet_net_unlock(LNET_LOCK_EX);
3557 mutex_lock(&the_lnet.ln_api_mutex);
3559 lnet_net_lock(LNET_LOCK_EX);
3560 list_for_each_entry_safe(route, tmp,
3563 lnet_move_route(route, NULL, &rlist);
3564 lnet_net_unlock(LNET_LOCK_EX);
3566 /* delete the peer */
3569 list_for_each_entry_safe(route, tmp,
3571 /* re-add these routes */
3572 lnet_add_route(route->lr_net,
3577 LIBCFS_FREE(route, sizeof(*route));
3579 mutex_unlock(&the_lnet.ln_api_mutex);
3581 lnet_net_lock(LNET_LOCK_EX);
3585 lnet_net_unlock(LNET_LOCK_EX);
3588 CDEBUG(D_NET, "stopping\n");
3590 * Clean up before telling lnet_peer_discovery_stop() that
3591 * we're done. Use wake_up() below to somewhat reduce the
3592 * size of the thundering herd if there are multiple threads
3593 * waiting on discovery of a single peer.
3596 /* Queue cleanup 1: stop all pending pings and pushes. */
3597 lnet_net_lock(LNET_LOCK_EX);
3598 while (!list_empty(&the_lnet.ln_dc_working)) {
3599 lp = list_first_entry(&the_lnet.ln_dc_working,
3600 struct lnet_peer, lp_dc_list);
3601 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3602 lnet_net_unlock(LNET_LOCK_EX);
3603 lnet_peer_cancel_discovery(lp);
3604 lnet_net_lock(LNET_LOCK_EX);
3606 lnet_net_unlock(LNET_LOCK_EX);
3608 /* Queue cleanup 2: wait for the expired queue to clear. */
3609 while (!list_empty(&the_lnet.ln_dc_expired))
3610 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3612 /* Queue cleanup 3: clear the request queue. */
3613 lnet_net_lock(LNET_LOCK_EX);
3614 while (!list_empty(&the_lnet.ln_dc_request)) {
3615 lp = list_first_entry(&the_lnet.ln_dc_request,
3616 struct lnet_peer, lp_dc_list);
3617 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3618 lnet_peer_discovery_complete(lp);
3620 lnet_net_unlock(LNET_LOCK_EX);
3622 lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3623 the_lnet.ln_dc_handler = NULL;
3625 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3626 wake_up(&the_lnet.ln_dc_waitq);
3628 CDEBUG(D_NET, "stopped\n");
3633 /* ln_api_mutex is held on entry. */
3634 int lnet_peer_discovery_start(void)
3636 struct task_struct *task;
3639 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3642 the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3643 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3644 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3647 CERROR("Can't start peer discovery thread: %d\n", rc);
3649 the_lnet.ln_dc_handler = NULL;
3651 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3654 CDEBUG(D_NET, "discovery start: %d\n", rc);
3659 /* ln_api_mutex is held on entry. */
3660 void lnet_peer_discovery_stop(void)
3662 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3665 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3666 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3668 /* In the LNetNIInit() path we may be stopping discovery before it
3669 * entered its work loop
3671 if (!completion_done(&the_lnet.ln_started))
3672 complete(&the_lnet.ln_started);
3674 wake_up(&the_lnet.ln_dc_waitq);
3676 wait_event(the_lnet.ln_dc_waitq,
3677 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3679 LASSERT(list_empty(&the_lnet.ln_dc_request));
3680 LASSERT(list_empty(&the_lnet.ln_dc_working));
3681 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3683 CDEBUG(D_NET, "discovery stopped\n");
3689 lnet_debug_peer(lnet_nid_t nid)
3691 char *aliveness = "NA";
3692 struct lnet_peer_ni *lp;
3695 cpt = lnet_cpt_of_nid(nid, NULL);
3698 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3700 lnet_net_unlock(cpt);
3701 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3705 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3706 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3708 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3709 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3710 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3711 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3712 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3714 lnet_peer_ni_decref_locked(lp);
3716 lnet_net_unlock(cpt);
3719 /* Gathering information for userspace. */
3721 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3722 char aliveness[LNET_MAX_STR_LEN],
3723 __u32 *cpt_iter, __u32 *refcount,
3724 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3725 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3726 __u32 *peer_tx_qnob)
3728 struct lnet_peer_table *peer_table;
3729 struct lnet_peer_ni *lp;
3734 /* get the number of CPTs */
3735 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3737 /* if the cpt number to be examined is >= the number of cpts in
3738 * the system then indicate that there are no more cpts to examin
3740 if (*cpt_iter >= lncpt)
3743 /* get the current table */
3744 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3745 /* if the ptable is NULL then there are no more cpts to examine */
3746 if (peer_table == NULL)
3749 lnet_net_lock(*cpt_iter);
3751 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3752 struct list_head *peers = &peer_table->pt_hash[j];
3754 list_for_each_entry(lp, peers, lpni_hashlist) {
3755 if (peer_index-- > 0)
3758 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3759 if (lnet_isrouter(lp) ||
3760 lnet_peer_aliveness_enabled(lp))
3761 snprintf(aliveness, LNET_MAX_STR_LEN,
3762 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3764 *nid = lp->lpni_nid;
3765 *refcount = atomic_read(&lp->lpni_refcount);
3766 *ni_peer_tx_credits =
3767 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3768 *peer_tx_credits = lp->lpni_txcredits;
3769 *peer_rtr_credits = lp->lpni_rtrcredits;
3770 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3771 *peer_tx_qnob = lp->lpni_txqnob;
3777 lnet_net_unlock(*cpt_iter);
3781 return found ? 0 : -ENOENT;
3784 /* ln_api_mutex is held, which keeps the peer list stable */
3785 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3787 struct lnet_ioctl_element_stats *lpni_stats;
3788 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3789 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3790 struct lnet_peer_ni_credit_info *lpni_info;
3791 struct lnet_peer_ni *lpni;
3792 struct lnet_peer *lp;
3797 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3804 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3805 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3806 size *= lp->lp_nnis;
3807 if (size > cfg->prcfg_size) {
3808 cfg->prcfg_size = size;
3813 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3814 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3815 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3816 cfg->prcfg_count = lp->lp_nnis;
3817 cfg->prcfg_size = size;
3818 cfg->prcfg_state = lp->lp_state;
3820 /* Allocate helper buffers. */
3822 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3825 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3828 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3829 if (!lpni_msg_stats)
3830 goto out_free_stats;
3831 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3833 goto out_free_msg_stats;
3838 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3839 nid = lpni->lpni_nid;
3840 if (copy_to_user(bulk, &nid, sizeof(nid)))
3841 goto out_free_hstats;
3842 bulk += sizeof(nid);
3844 memset(lpni_info, 0, sizeof(*lpni_info));
3845 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3846 if (lnet_isrouter(lpni) ||
3847 lnet_peer_aliveness_enabled(lpni))
3848 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3849 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3851 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3852 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3853 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3854 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3855 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3856 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3857 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3858 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3859 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3860 goto out_free_hstats;
3861 bulk += sizeof(*lpni_info);
3863 memset(lpni_stats, 0, sizeof(*lpni_stats));
3864 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3865 LNET_STATS_TYPE_SEND);
3866 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3867 LNET_STATS_TYPE_RECV);
3868 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3869 LNET_STATS_TYPE_DROP);
3870 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3871 goto out_free_hstats;
3872 bulk += sizeof(*lpni_stats);
3873 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3874 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3875 goto out_free_hstats;
3876 bulk += sizeof(*lpni_msg_stats);
3877 lpni_hstats->hlpni_network_timeout =
3878 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3879 lpni_hstats->hlpni_remote_dropped =
3880 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3881 lpni_hstats->hlpni_remote_timeout =
3882 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3883 lpni_hstats->hlpni_remote_error =
3884 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3885 lpni_hstats->hlpni_health_value =
3886 atomic_read(&lpni->lpni_healthv);
3887 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3888 goto out_free_hstats;
3889 bulk += sizeof(*lpni_hstats);
3894 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3896 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3898 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3900 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3902 lnet_peer_decref_locked(lp);
3908 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3910 /* the mt could've shutdown and cleaned up the queues */
3911 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3914 if (list_empty(&lpni->lpni_recovery) &&
3915 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3916 CDEBUG(D_NET, "lpni %s added to recovery queue. Health = %d\n",
3917 libcfs_nid2str(lpni->lpni_nid),
3918 atomic_read(&lpni->lpni_healthv));
3919 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3920 lnet_peer_ni_addref_locked(lpni);
3924 /* Call with the ln_api_mutex held */
3926 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3928 struct lnet_peer_table *ptable;
3929 struct lnet_peer *lp;
3930 struct lnet_peer_net *lpn;
3931 struct lnet_peer_ni *lpni;
3935 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3939 lnet_net_lock(LNET_LOCK_EX);
3940 lpni = lnet_find_peer_ni_locked(nid);
3942 lnet_net_unlock(LNET_LOCK_EX);
3945 atomic_set(&lpni->lpni_healthv, value);
3946 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3947 lnet_peer_ni_decref_locked(lpni);
3948 lnet_net_unlock(LNET_LOCK_EX);
3952 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3955 * Walk all the peers and reset the healhv for each one to the
3958 lnet_net_lock(LNET_LOCK_EX);
3959 for (cpt = 0; cpt < lncpt; cpt++) {
3960 ptable = the_lnet.ln_peer_tables[cpt];
3961 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3962 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3963 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3965 atomic_set(&lpni->lpni_healthv, value);
3966 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3971 lnet_net_unlock(LNET_LOCK_EX);