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);
902 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
903 * this is a preferred point-to-point path. Call with lnet_net_lock in
907 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
909 struct lnet_nid_list *ne;
911 if (lpni->lpni_pref_nnids == 0)
913 if (lpni->lpni_pref_nnids == 1)
914 return lpni->lpni_pref.nid == nid;
915 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
916 if (ne->nl_nid == nid)
923 * Set a single ni as preferred, provided no preferred ni is already
924 * defined. Only to be used for non-multi-rail peer_ni.
927 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
931 spin_lock(&lpni->lpni_lock);
932 if (nid == LNET_NID_ANY) {
934 } else if (lpni->lpni_pref_nnids > 0) {
936 } else if (lpni->lpni_pref_nnids == 0) {
937 lpni->lpni_pref.nid = nid;
938 lpni->lpni_pref_nnids = 1;
939 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
941 spin_unlock(&lpni->lpni_lock);
943 CDEBUG(D_NET, "peer %s nid %s: %d\n",
944 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
949 * Clear the preferred NID from a non-multi-rail peer_ni, provided
950 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
953 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
957 spin_lock(&lpni->lpni_lock);
958 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
959 lpni->lpni_pref_nnids = 0;
960 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
961 } else if (lpni->lpni_pref_nnids == 0) {
966 spin_unlock(&lpni->lpni_lock);
968 CDEBUG(D_NET, "peer %s: %d\n",
969 libcfs_nid2str(lpni->lpni_nid), rc);
974 * Clear the preferred NIDs from a non-multi-rail peer.
977 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
979 struct lnet_peer_ni *lpni = NULL;
981 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
982 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
986 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
988 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
989 struct lnet_nid_list *ne1 = NULL;
990 struct lnet_nid_list *ne2 = NULL;
991 lnet_nid_t tmp_nid = LNET_NID_ANY;
994 if (nid == LNET_NID_ANY) {
999 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1004 /* A non-MR node may have only one preferred NI per peer_ni */
1005 if (lpni->lpni_pref_nnids > 0 &&
1006 !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1011 /* add the new preferred nid to the list of preferred nids */
1012 if (lpni->lpni_pref_nnids != 0) {
1013 size_t alloc_size = sizeof(*ne1);
1015 if (lpni->lpni_pref_nnids == 1) {
1016 tmp_nid = lpni->lpni_pref.nid;
1017 INIT_LIST_HEAD(&lpni->lpni_pref.nids);
1020 list_for_each_entry(ne1, &lpni->lpni_pref.nids, nl_list) {
1021 if (ne1->nl_nid == nid) {
1027 LIBCFS_CPT_ALLOC(ne1, lnet_cpt_table(), lpni->lpni_cpt,
1034 /* move the originally stored nid to the list */
1035 if (lpni->lpni_pref_nnids == 1) {
1036 LIBCFS_CPT_ALLOC(ne2, lnet_cpt_table(),
1037 lpni->lpni_cpt, alloc_size);
1042 INIT_LIST_HEAD(&ne2->nl_list);
1043 ne2->nl_nid = tmp_nid;
1048 lnet_net_lock(LNET_LOCK_EX);
1049 spin_lock(&lpni->lpni_lock);
1050 if (lpni->lpni_pref_nnids == 0) {
1051 lpni->lpni_pref.nid = nid;
1054 list_add_tail(&ne2->nl_list, &lpni->lpni_pref.nids);
1055 list_add_tail(&ne1->nl_list, &lpni->lpni_pref.nids);
1057 lpni->lpni_pref_nnids++;
1058 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1059 spin_unlock(&lpni->lpni_lock);
1060 lnet_net_unlock(LNET_LOCK_EX);
1063 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1064 spin_lock(&lpni->lpni_lock);
1065 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1066 spin_unlock(&lpni->lpni_lock);
1068 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1069 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1074 lnet_peer_del_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 *ne = NULL;
1080 if (lpni->lpni_pref_nnids == 0) {
1085 if (lpni->lpni_pref_nnids == 1) {
1086 if (lpni->lpni_pref.nid != nid) {
1091 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1092 if (ne->nl_nid == nid)
1093 goto remove_nid_entry;
1101 lnet_net_lock(LNET_LOCK_EX);
1102 spin_lock(&lpni->lpni_lock);
1103 if (lpni->lpni_pref_nnids == 1)
1104 lpni->lpni_pref.nid = LNET_NID_ANY;
1106 list_del_init(&ne->nl_list);
1107 if (lpni->lpni_pref_nnids == 2) {
1108 struct lnet_nid_list *ne, *tmp;
1110 list_for_each_entry_safe(ne, tmp,
1111 &lpni->lpni_pref.nids,
1113 lpni->lpni_pref.nid = ne->nl_nid;
1114 list_del_init(&ne->nl_list);
1115 LIBCFS_FREE(ne, sizeof(*ne));
1119 lpni->lpni_pref_nnids--;
1120 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1121 spin_unlock(&lpni->lpni_lock);
1122 lnet_net_unlock(LNET_LOCK_EX);
1125 LIBCFS_FREE(ne, sizeof(*ne));
1127 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1128 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1133 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1135 struct lnet_peer_ni *lpni;
1136 lnet_nid_t primary_nid = nid;
1138 lpni = lnet_find_peer_ni_locked(nid);
1140 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1141 lnet_peer_ni_decref_locked(lpni);
1148 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1149 __must_hold(&lp->lp_lock)
1151 if (lnet_peer_discovery_disabled)
1154 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1155 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1166 lnet_is_discovery_disabled(struct lnet_peer *lp)
1170 spin_lock(&lp->lp_lock);
1171 rc = lnet_is_discovery_disabled_locked(lp);
1172 spin_unlock(&lp->lp_lock);
1178 LNetPrimaryNID(lnet_nid_t nid)
1180 struct lnet_peer *lp;
1181 struct lnet_peer_ni *lpni;
1182 lnet_nid_t primary_nid = nid;
1186 if (nid == LNET_NID_LO_0)
1187 return LNET_NID_LO_0;
1189 cpt = lnet_net_lock_current();
1190 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1195 lp = lpni->lpni_peer_net->lpn_peer;
1197 while (!lnet_peer_is_uptodate(lp)) {
1198 spin_lock(&lp->lp_lock);
1199 /* force a full discovery cycle */
1200 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1201 spin_unlock(&lp->lp_lock);
1203 rc = lnet_discover_peer_locked(lpni, cpt, true);
1206 lp = lpni->lpni_peer_net->lpn_peer;
1208 /* Only try once if discovery is disabled */
1209 if (lnet_is_discovery_disabled(lp))
1212 primary_nid = lp->lp_primary_nid;
1214 lnet_peer_ni_decref_locked(lpni);
1216 lnet_net_unlock(cpt);
1218 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1219 libcfs_nid2str(primary_nid), rc);
1222 EXPORT_SYMBOL(LNetPrimaryNID);
1224 struct lnet_peer_net *
1225 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1227 struct lnet_peer_net *peer_net;
1228 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1229 if (peer_net->lpn_net_id == net_id)
1236 * Attach a peer_ni to a peer_net and peer. This function assumes
1237 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1238 * may be attached to a different peer, in which case it will be
1239 * properly detached first. The whole operation is done atomically.
1241 * This function consumes the reference on lpni and Always returns 0.
1242 * This is the last function called from functions that do return an
1243 * int, so returning 0 here allows the compiler to do a tail call.
1246 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1247 struct lnet_peer_net *lpn,
1248 struct lnet_peer_ni *lpni,
1251 struct lnet_peer_table *ptable;
1253 /* Install the new peer_ni */
1254 lnet_net_lock(LNET_LOCK_EX);
1255 /* Add peer_ni to global peer table hash, if necessary. */
1256 if (list_empty(&lpni->lpni_hashlist)) {
1257 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1259 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1260 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1261 ptable->pt_version++;
1262 lnet_peer_ni_addref_locked(lpni);
1265 /* Detach the peer_ni from an existing peer, if necessary. */
1266 if (lpni->lpni_peer_net) {
1267 LASSERT(lpni->lpni_peer_net != lpn);
1268 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1269 lnet_peer_detach_peer_ni_locked(lpni);
1270 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1271 lpni->lpni_peer_net = NULL;
1274 /* Add peer_ni to peer_net */
1275 lpni->lpni_peer_net = lpn;
1276 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1277 lnet_update_peer_net_healthv(lpni);
1278 lnet_peer_net_addref_locked(lpn);
1280 /* Add peer_net to peer */
1281 if (!lpn->lpn_peer) {
1283 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1284 lnet_peer_addref_locked(lp);
1287 /* Add peer to global peer list, if necessary */
1288 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1289 if (list_empty(&lp->lp_peer_list)) {
1290 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1295 /* Update peer state */
1296 spin_lock(&lp->lp_lock);
1297 if (flags & LNET_PEER_CONFIGURED) {
1298 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1299 lp->lp_state |= LNET_PEER_CONFIGURED;
1301 if (flags & LNET_PEER_MULTI_RAIL) {
1302 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1303 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1304 lnet_peer_clr_non_mr_pref_nids(lp);
1307 spin_unlock(&lp->lp_lock);
1311 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1312 libcfs_nid2str(lp->lp_primary_nid),
1313 libcfs_nid2str(lpni->lpni_nid), flags);
1314 lnet_peer_ni_decref_locked(lpni);
1315 lnet_net_unlock(LNET_LOCK_EX);
1321 * Create a new peer, with nid as its primary nid.
1323 * Call with the lnet_api_mutex held.
1326 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1328 struct lnet_peer *lp;
1329 struct lnet_peer_net *lpn;
1330 struct lnet_peer_ni *lpni;
1333 LASSERT(nid != LNET_NID_ANY);
1336 * No need for the lnet_net_lock here, because the
1337 * lnet_api_mutex is held.
1339 lpni = lnet_find_peer_ni_locked(nid);
1341 /* A peer with this NID already exists. */
1342 lp = lpni->lpni_peer_net->lpn_peer;
1343 lnet_peer_ni_decref_locked(lpni);
1345 * This is an error if the peer was configured and the
1346 * primary NID differs or an attempt is made to change
1347 * the Multi-Rail flag. Otherwise the assumption is
1348 * that an existing peer is being modified.
1350 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1351 if (lp->lp_primary_nid != nid)
1353 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1357 /* Delete and recreate as a configured peer. */
1361 /* Create peer, peer_net, and peer_ni. */
1363 lp = lnet_peer_alloc(nid);
1366 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1369 lpni = lnet_peer_ni_alloc(nid);
1373 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1376 LIBCFS_FREE(lpn, sizeof(*lpn));
1378 LIBCFS_FREE(lp, sizeof(*lp));
1380 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1381 libcfs_nid2str(nid), flags, rc);
1386 * Add a NID to a peer. Call with ln_api_mutex held.
1389 * -EPERM: Non-DLC addition to a DLC-configured peer.
1390 * -EEXIST: The NID was configured by DLC for a different peer.
1391 * -ENOMEM: Out of memory.
1392 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1393 * non-multi-rail peer.
1396 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1398 struct lnet_peer_net *lpn;
1399 struct lnet_peer_ni *lpni;
1403 LASSERT(nid != LNET_NID_ANY);
1405 /* A configured peer can only be updated through configuration. */
1406 if (!(flags & LNET_PEER_CONFIGURED)) {
1407 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1414 * The MULTI_RAIL flag can be set but not cleared, because
1415 * that would leave the peer struct in an invalid state.
1417 if (flags & LNET_PEER_MULTI_RAIL) {
1418 spin_lock(&lp->lp_lock);
1419 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1420 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1421 lnet_peer_clr_non_mr_pref_nids(lp);
1423 spin_unlock(&lp->lp_lock);
1424 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1429 lpni = lnet_find_peer_ni_locked(nid);
1432 * A peer_ni already exists. This is only a problem if
1433 * it is not connected to this peer and was configured
1436 if (lpni->lpni_peer_net->lpn_peer == lp)
1438 if (lnet_peer_ni_is_configured(lpni)) {
1442 /* If this is the primary NID, destroy the peer. */
1443 if (lnet_peer_ni_is_primary(lpni)) {
1444 struct lnet_peer *rtr_lp =
1445 lpni->lpni_peer_net->lpn_peer;
1446 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1448 * if we're trying to delete a router it means
1449 * we're moving this peer NI to a new peer so must
1450 * transfer router properties to the new peer
1452 if (rtr_refcount > 0) {
1453 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1454 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1456 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1457 lnet_peer_ni_decref_locked(lpni);
1458 lpni = lnet_peer_ni_alloc(nid);
1465 lpni = lnet_peer_ni_alloc(nid);
1473 * Get the peer_net. Check that we're not adding a second
1474 * peer_ni on a peer_net of a non-multi-rail peer.
1476 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1478 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1483 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1488 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1491 lnet_peer_ni_decref_locked(lpni);
1493 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1494 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1500 * Update the primary NID of a peer, if possible.
1502 * Call with the lnet_api_mutex held.
1505 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1507 lnet_nid_t old = lp->lp_primary_nid;
1510 if (lp->lp_primary_nid == nid)
1512 rc = lnet_peer_add_nid(lp, nid, flags);
1515 lp->lp_primary_nid = nid;
1517 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1518 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1523 * lpni creation initiated due to traffic either sending or receiving.
1526 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1528 struct lnet_peer *lp;
1529 struct lnet_peer_net *lpn;
1530 struct lnet_peer_ni *lpni;
1534 if (nid == LNET_NID_ANY) {
1539 /* lnet_net_lock is not needed here because ln_api_lock is held */
1540 lpni = lnet_find_peer_ni_locked(nid);
1543 * We must have raced with another thread. Since we
1544 * know next to nothing about a peer_ni created by
1545 * traffic, we just assume everything is ok and
1548 lnet_peer_ni_decref_locked(lpni);
1552 /* Create peer, peer_net, and peer_ni. */
1554 lp = lnet_peer_alloc(nid);
1557 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1560 lpni = lnet_peer_ni_alloc(nid);
1563 if (pref != LNET_NID_ANY)
1564 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1566 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1569 LIBCFS_FREE(lpn, sizeof(*lpn));
1571 LIBCFS_FREE(lp, sizeof(*lp));
1573 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1578 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1580 * This API handles the following combinations:
1581 * Create a peer with its primary NI if only the prim_nid is provided
1582 * Add a NID to a peer identified by the prim_nid. The peer identified
1583 * by the prim_nid must already exist.
1584 * The peer being created may be non-MR.
1586 * The caller must hold ln_api_mutex. This prevents the peer from
1587 * being created/modified/deleted by a different thread.
1590 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1592 struct lnet_peer *lp = NULL;
1593 struct lnet_peer_ni *lpni;
1596 /* The prim_nid must always be specified */
1597 if (prim_nid == LNET_NID_ANY)
1600 flags = LNET_PEER_CONFIGURED;
1602 flags |= LNET_PEER_MULTI_RAIL;
1605 * If nid isn't specified, we must create a new peer with
1606 * prim_nid as its primary nid.
1608 if (nid == LNET_NID_ANY)
1609 return lnet_peer_add(prim_nid, flags);
1611 /* Look up the prim_nid, which must exist. */
1612 lpni = lnet_find_peer_ni_locked(prim_nid);
1615 lnet_peer_ni_decref_locked(lpni);
1616 lp = lpni->lpni_peer_net->lpn_peer;
1618 /* Peer must have been configured. */
1619 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1620 CDEBUG(D_NET, "peer %s was not configured\n",
1621 libcfs_nid2str(prim_nid));
1625 /* Primary NID must match */
1626 if (lp->lp_primary_nid != prim_nid) {
1627 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1628 libcfs_nid2str(prim_nid),
1629 libcfs_nid2str(lp->lp_primary_nid));
1633 /* Multi-Rail flag must match. */
1634 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1635 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1636 libcfs_nid2str(prim_nid));
1640 return lnet_peer_add_nid(lp, nid, flags);
1644 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1646 * This API handles the following combinations:
1647 * Delete a NI from a peer if both prim_nid and nid are provided.
1648 * Delete a peer if only prim_nid is provided.
1649 * Delete a peer if its primary nid is provided.
1651 * The caller must hold ln_api_mutex. This prevents the peer from
1652 * being modified/deleted by a different thread.
1655 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1657 struct lnet_peer *lp;
1658 struct lnet_peer_ni *lpni;
1661 if (prim_nid == LNET_NID_ANY)
1664 lpni = lnet_find_peer_ni_locked(prim_nid);
1667 lnet_peer_ni_decref_locked(lpni);
1668 lp = lpni->lpni_peer_net->lpn_peer;
1670 if (prim_nid != lp->lp_primary_nid) {
1671 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1672 libcfs_nid2str(prim_nid),
1673 libcfs_nid2str(lp->lp_primary_nid));
1677 lnet_net_lock(LNET_LOCK_EX);
1678 if (lp->lp_rtr_refcount > 0) {
1679 lnet_net_unlock(LNET_LOCK_EX);
1680 CERROR("%s is a router. Can not be deleted\n",
1681 libcfs_nid2str(prim_nid));
1684 lnet_net_unlock(LNET_LOCK_EX);
1686 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1687 return lnet_peer_del(lp);
1689 flags = LNET_PEER_CONFIGURED;
1690 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1691 flags |= LNET_PEER_MULTI_RAIL;
1693 return lnet_peer_del_nid(lp, nid, flags);
1697 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1699 struct lnet_peer_table *ptable;
1700 struct lnet_peer_net *lpn;
1702 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1704 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1705 LASSERT(list_empty(&lpni->lpni_txq));
1706 LASSERT(lpni->lpni_txqnob == 0);
1707 LASSERT(list_empty(&lpni->lpni_peer_nis));
1708 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1710 lpn = lpni->lpni_peer_net;
1711 lpni->lpni_peer_net = NULL;
1712 lpni->lpni_net = NULL;
1714 if (!list_empty(&lpni->lpni_hashlist)) {
1715 /* remove the peer ni from the zombie list */
1716 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1717 spin_lock(&ptable->pt_zombie_lock);
1718 list_del_init(&lpni->lpni_hashlist);
1719 ptable->pt_zombies--;
1720 spin_unlock(&ptable->pt_zombie_lock);
1723 if (lpni->lpni_pref_nnids > 1) {
1724 struct lnet_nid_list *ne, *tmp;
1726 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
1728 list_del_init(&ne->nl_list);
1729 LIBCFS_FREE(ne, sizeof(*ne));
1732 LIBCFS_FREE(lpni, sizeof(*lpni));
1735 lnet_peer_net_decref_locked(lpn);
1738 struct lnet_peer_ni *
1739 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1741 struct lnet_peer_ni *lpni = NULL;
1744 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1745 return ERR_PTR(-ESHUTDOWN);
1748 * find if a peer_ni already exists.
1749 * If so then just return that.
1751 lpni = lnet_find_peer_ni_locked(nid);
1755 lnet_net_unlock(cpt);
1757 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1760 goto out_net_relock;
1763 lpni = lnet_find_peer_ni_locked(nid);
1773 * Get a peer_ni for the given nid, create it if necessary. Takes a
1774 * hold on the peer_ni.
1776 struct lnet_peer_ni *
1777 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1779 struct lnet_peer_ni *lpni = NULL;
1782 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1783 return ERR_PTR(-ESHUTDOWN);
1786 * find if a peer_ni already exists.
1787 * If so then just return that.
1789 lpni = lnet_find_peer_ni_locked(nid);
1795 * use the lnet_api_mutex to serialize the creation of the peer_ni
1796 * and the creation/deletion of the local ni/net. When a local ni is
1797 * created, if there exists a set of peer_nis on that network,
1798 * they need to be traversed and updated. When a local NI is
1799 * deleted, which could result in a network being deleted, then
1800 * all peer nis on that network need to be removed as well.
1802 * Creation through traffic should also be serialized with
1803 * creation through DLC.
1805 lnet_net_unlock(cpt);
1806 mutex_lock(&the_lnet.ln_api_mutex);
1808 * Shutdown is only set under the ln_api_lock, so a single
1809 * check here is sufficent.
1811 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1812 lpni = ERR_PTR(-ESHUTDOWN);
1813 goto out_mutex_unlock;
1816 rc = lnet_peer_ni_traffic_add(nid, pref);
1819 goto out_mutex_unlock;
1822 lpni = lnet_find_peer_ni_locked(nid);
1826 mutex_unlock(&the_lnet.ln_api_mutex);
1829 /* Lock has been dropped, check again for shutdown. */
1830 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1832 lnet_peer_ni_decref_locked(lpni);
1833 lpni = ERR_PTR(-ESHUTDOWN);
1840 lnet_peer_gw_discovery(struct lnet_peer *lp)
1844 spin_lock(&lp->lp_lock);
1845 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1847 spin_unlock(&lp->lp_lock);
1853 lnet_peer_is_uptodate(struct lnet_peer *lp)
1857 spin_lock(&lp->lp_lock);
1858 rc = lnet_peer_is_uptodate_locked(lp);
1859 spin_unlock(&lp->lp_lock);
1864 * Is a peer uptodate from the point of view of discovery?
1866 * If it is currently being processed, obviously not.
1867 * A forced Ping or Push is also handled by the discovery thread.
1869 * Otherwise look at whether the peer needs rediscovering.
1872 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
1873 __must_hold(&lp->lp_lock)
1877 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1878 LNET_PEER_FORCE_PING |
1879 LNET_PEER_FORCE_PUSH)) {
1881 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1883 } else if (lnet_peer_needs_push(lp)) {
1885 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1886 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1898 * Queue a peer for the attention of the discovery thread. Call with
1899 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1900 * -EALREADY if the peer was already queued.
1902 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1906 spin_lock(&lp->lp_lock);
1907 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1908 lp->lp_state |= LNET_PEER_DISCOVERING;
1909 spin_unlock(&lp->lp_lock);
1910 if (list_empty(&lp->lp_dc_list)) {
1911 lnet_peer_addref_locked(lp);
1912 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1913 wake_up(&the_lnet.ln_dc_waitq);
1919 CDEBUG(D_NET, "Queue peer %s: %d\n",
1920 libcfs_nid2str(lp->lp_primary_nid), rc);
1926 * Discovery of a peer is complete. Wake all waiters on the peer.
1927 * Call with lnet_net_lock/EX held.
1929 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1931 struct lnet_msg *msg, *tmp;
1933 LIST_HEAD(pending_msgs);
1935 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1936 libcfs_nid2str(lp->lp_primary_nid));
1938 list_del_init(&lp->lp_dc_list);
1939 spin_lock(&lp->lp_lock);
1940 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1941 spin_unlock(&lp->lp_lock);
1942 wake_up_all(&lp->lp_dc_waitq);
1944 if (lp->lp_rtr_refcount > 0)
1945 lnet_router_discovery_complete(lp);
1947 lnet_net_unlock(LNET_LOCK_EX);
1949 /* iterate through all pending messages and send them again */
1950 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1951 list_del_init(&msg->msg_list);
1952 if (lp->lp_dc_error) {
1953 lnet_finalize(msg, lp->lp_dc_error);
1957 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1958 lnet_msgtyp2str(msg->msg_type),
1959 libcfs_id2str(msg->msg_target));
1960 rc = lnet_send(msg->msg_src_nid_param, msg,
1961 msg->msg_rtr_nid_param);
1963 CNETERR("Error sending %s to %s: %d\n",
1964 lnet_msgtyp2str(msg->msg_type),
1965 libcfs_id2str(msg->msg_target), rc);
1966 lnet_finalize(msg, rc);
1969 lnet_net_lock(LNET_LOCK_EX);
1970 lnet_peer_decref_locked(lp);
1974 * Handle inbound push.
1975 * Like any event handler, called with lnet_res_lock/CPT held.
1977 void lnet_peer_push_event(struct lnet_event *ev)
1979 struct lnet_ping_buffer *pbuf;
1980 struct lnet_peer *lp;
1982 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
1984 /* lnet_find_peer() adds a refcount */
1985 lp = lnet_find_peer(ev->source.nid);
1987 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1988 libcfs_nid2str(ev->initiator.nid),
1989 libcfs_nid2str(ev->source.nid));
1990 pbuf->pb_needs_post = true;
1994 /* Ensure peer state remains consistent while we modify it. */
1995 spin_lock(&lp->lp_lock);
1998 * If some kind of error happened the contents of the message
1999 * cannot be used. Clear the NIDS_UPTODATE and set the
2000 * FORCE_PING flag to trigger a ping.
2003 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2004 lp->lp_state |= LNET_PEER_FORCE_PING;
2005 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2007 libcfs_nid2str(lp->lp_primary_nid),
2008 libcfs_nid2str(ev->source.nid));
2013 * A push with invalid or corrupted info. Clear the UPTODATE
2014 * flag to trigger a ping.
2016 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2017 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2018 lp->lp_state |= LNET_PEER_FORCE_PING;
2019 CDEBUG(D_NET, "Corrupted Push from %s\n",
2020 libcfs_nid2str(lp->lp_primary_nid));
2025 * Make sure we'll allocate the correct size ping buffer when
2028 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2029 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2032 * A non-Multi-Rail peer is not supposed to be capable of
2035 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2036 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2037 libcfs_nid2str(lp->lp_primary_nid));
2042 * The peer may have discovery disabled at its end. Set
2043 * NO_DISCOVERY as appropriate.
2045 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2046 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2047 libcfs_nid2str(lp->lp_primary_nid));
2049 * Mark the peer for deletion if we already know about it
2050 * and it's going from discovery set to no discovery set
2052 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2053 LNET_PEER_DISCOVERING)) &&
2054 lp->lp_state & LNET_PEER_DISCOVERED) {
2055 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2056 libcfs_nid2str(lp->lp_primary_nid),
2058 lp->lp_state |= LNET_PEER_MARK_DELETION;
2060 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2061 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2062 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2063 libcfs_nid2str(lp->lp_primary_nid));
2064 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2068 * Update the MULTI_RAIL flag based on the push. If the peer
2069 * was configured with DLC then the setting should match what
2071 * NB: We verified above that the MR feature bit is set in pi_features
2073 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2074 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2075 libcfs_nid2str(lp->lp_primary_nid), lp);
2076 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2077 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2078 libcfs_nid2str(lp->lp_primary_nid));
2079 } else if (lnet_peer_discovery_disabled) {
2080 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2081 libcfs_nid2str(lp->lp_primary_nid), lp);
2082 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2083 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2084 libcfs_nid2str(lp->lp_primary_nid), lp);
2086 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2087 libcfs_nid2str(lp->lp_primary_nid), lp);
2088 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2089 lnet_peer_clr_non_mr_pref_nids(lp);
2093 * Check for truncation of the Put message. Clear the
2094 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2095 * and tell discovery to allocate a bigger buffer.
2097 if (ev->mlength < ev->rlength) {
2098 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2099 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2100 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2101 lp->lp_state |= LNET_PEER_FORCE_PING;
2102 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2103 libcfs_nid2str(lp->lp_primary_nid),
2104 pbuf->pb_info.pi_nnis);
2108 /* always assume new data */
2109 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2110 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2113 * If there is data present that hasn't been processed yet,
2114 * we'll replace it if the Put contained newer data and it
2115 * fits. We're racing with a Ping or earlier Push in this
2118 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2119 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2120 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2121 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2122 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2123 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2124 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2125 libcfs_nid2str(lp->lp_primary_nid),
2126 LNET_PING_BUFFER_SEQNO(pbuf),
2127 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2133 * Allocate a buffer to copy the data. On a failure we drop
2134 * the Push and set FORCE_PING to force the discovery
2135 * thread to fix the problem by pinging the peer.
2137 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2139 lp->lp_state |= LNET_PEER_FORCE_PING;
2140 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2141 libcfs_nid2str(lp->lp_primary_nid),
2142 LNET_PING_BUFFER_SEQNO(pbuf));
2147 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2148 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2149 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2150 CDEBUG(D_NET, "Received Push %s %u\n",
2151 libcfs_nid2str(lp->lp_primary_nid),
2152 LNET_PING_BUFFER_SEQNO(pbuf));
2155 /* We've processed this buffer. It can be reposted */
2156 pbuf->pb_needs_post = true;
2159 * Queue the peer for discovery if not done, force it on the request
2160 * queue and wake the discovery thread if the peer was already queued,
2161 * because its status changed.
2163 spin_unlock(&lp->lp_lock);
2164 lnet_net_lock(LNET_LOCK_EX);
2165 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2166 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2167 wake_up(&the_lnet.ln_dc_waitq);
2169 /* Drop refcount from lookup */
2170 lnet_peer_decref_locked(lp);
2171 lnet_net_unlock(LNET_LOCK_EX);
2175 * Clear the discovery error state, unless we're already discovering
2176 * this peer, in which case the error is current.
2178 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2180 spin_lock(&lp->lp_lock);
2181 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2182 lp->lp_dc_error = 0;
2183 spin_unlock(&lp->lp_lock);
2187 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2188 * dropped/retaken within this function. An lnet_peer_ni is passed in
2189 * because discovery could tear down an lnet_peer.
2192 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2195 struct lnet_peer *lp;
2200 lnet_net_unlock(cpt);
2201 lnet_net_lock(LNET_LOCK_EX);
2202 lp = lpni->lpni_peer_net->lpn_peer;
2203 lnet_peer_clear_discovery_error(lp);
2206 * We're willing to be interrupted. The lpni can become a
2207 * zombie if we race with DLC, so we must check for that.
2210 /* Keep lp alive when the lnet_net_lock is unlocked */
2211 lnet_peer_addref_locked(lp);
2212 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2213 if (signal_pending(current))
2215 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2218 * Don't repeat discovery if discovery is disabled. This is
2219 * done to ensure we can use discovery as a standard ping as
2220 * well for backwards compatibility with routers which do not
2221 * have discovery or have discovery disabled
2223 if (lnet_is_discovery_disabled(lp) && count > 0)
2225 if (lp->lp_dc_error)
2227 if (lnet_peer_is_uptodate(lp))
2229 lnet_peer_queue_for_discovery(lp);
2231 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2234 * If caller requested a non-blocking operation then
2235 * return immediately. Once discovery is complete any
2236 * pending messages that were stopped due to discovery
2237 * will be transmitted.
2242 lnet_net_unlock(LNET_LOCK_EX);
2244 finish_wait(&lp->lp_dc_waitq, &wait);
2245 lnet_net_lock(LNET_LOCK_EX);
2246 lnet_peer_decref_locked(lp);
2247 /* Peer may have changed */
2248 lp = lpni->lpni_peer_net->lpn_peer;
2250 finish_wait(&lp->lp_dc_waitq, &wait);
2252 lnet_net_unlock(LNET_LOCK_EX);
2254 lnet_peer_decref_locked(lp);
2256 * The peer may have changed, so re-check and rediscover if that turns
2257 * out to have been the case. The reference count on lp ensured that
2258 * even if it was unlinked from lpni the memory could not be recycled.
2259 * Thus the check below is sufficient to determine whether the peer
2260 * changed. If the peer changed, then lp must not be dereferenced.
2262 if (lp != lpni->lpni_peer_net->lpn_peer)
2265 if (signal_pending(current))
2267 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2269 else if (lp->lp_dc_error)
2270 rc = lp->lp_dc_error;
2272 CDEBUG(D_NET, "non-blocking discovery\n");
2273 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2276 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2277 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2278 libcfs_nid2str(lpni->lpni_nid), rc,
2279 (!block) ? "pending discovery" : "discovery complete");
2284 /* Handle an incoming ack for a push. */
2286 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2288 struct lnet_ping_buffer *pbuf;
2290 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2291 spin_lock(&lp->lp_lock);
2292 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2293 lp->lp_push_error = ev->status;
2295 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2297 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2298 spin_unlock(&lp->lp_lock);
2300 CDEBUG(D_NET, "peer %s ev->status %d\n",
2301 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2304 /* Handle a Reply message. This is the reply to a Ping message. */
2306 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2308 struct lnet_ping_buffer *pbuf;
2311 spin_lock(&lp->lp_lock);
2313 lp->lp_disc_src_nid = ev->target.nid;
2316 * If some kind of error happened the contents of message
2317 * cannot be used. Set PING_FAILED to trigger a retry.
2320 lp->lp_state |= LNET_PEER_PING_FAILED;
2321 lp->lp_ping_error = ev->status;
2322 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2324 libcfs_nid2str(lp->lp_primary_nid),
2325 libcfs_nid2str(ev->source.nid));
2329 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2330 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2331 lnet_swap_pinginfo(pbuf);
2334 * A reply with invalid or corrupted info. Set PING_FAILED to
2337 rc = lnet_ping_info_validate(&pbuf->pb_info);
2339 lp->lp_state |= LNET_PEER_PING_FAILED;
2340 lp->lp_ping_error = 0;
2341 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2342 libcfs_nid2str(lp->lp_primary_nid), rc);
2348 * The peer may have discovery disabled at its end. Set
2349 * NO_DISCOVERY as appropriate.
2351 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2352 !lnet_peer_discovery_disabled) {
2353 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2354 libcfs_nid2str(lp->lp_primary_nid));
2355 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2357 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2358 libcfs_nid2str(lp->lp_primary_nid));
2359 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2363 * Update the MULTI_RAIL flag based on the reply. If the peer
2364 * was configured with DLC then the setting should match what
2367 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2368 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2369 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2370 libcfs_nid2str(lp->lp_primary_nid), lp);
2371 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2372 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2373 libcfs_nid2str(lp->lp_primary_nid));
2374 } else if (lnet_peer_discovery_disabled) {
2376 "peer %s(%p) not MR: DD disabled locally\n",
2377 libcfs_nid2str(lp->lp_primary_nid), lp);
2378 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2380 "peer %s(%p) not MR: DD disabled remotely\n",
2381 libcfs_nid2str(lp->lp_primary_nid), lp);
2383 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2384 libcfs_nid2str(lp->lp_primary_nid), lp);
2385 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2386 lnet_peer_clr_non_mr_pref_nids(lp);
2388 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2389 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2390 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2391 libcfs_nid2str(lp->lp_primary_nid));
2393 CERROR("Multi-Rail state vanished from %s\n",
2394 libcfs_nid2str(lp->lp_primary_nid));
2395 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2400 * Make sure we'll allocate the correct size ping buffer when
2403 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2404 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2407 * Check for truncation of the Reply. Clear PING_SENT and set
2408 * PING_FAILED to trigger a retry.
2410 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2411 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2412 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2413 lp->lp_state |= LNET_PEER_PING_FAILED;
2414 lp->lp_ping_error = 0;
2415 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2416 libcfs_nid2str(lp->lp_primary_nid),
2417 pbuf->pb_info.pi_nnis);
2422 * Check the sequence numbers in the reply. These are only
2423 * available if the reply came from a Multi-Rail peer.
2425 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2426 pbuf->pb_info.pi_nnis > 1 &&
2427 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2428 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2429 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2430 libcfs_nid2str(lp->lp_primary_nid),
2431 LNET_PING_BUFFER_SEQNO(pbuf),
2434 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2437 /* We're happy with the state of the data in the buffer. */
2438 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2439 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2440 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2441 lnet_ping_buffer_decref(lp->lp_data);
2443 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2444 lnet_ping_buffer_addref(pbuf);
2447 lp->lp_state &= ~LNET_PEER_PING_SENT;
2448 spin_unlock(&lp->lp_lock);
2450 lnet_net_lock(LNET_LOCK_EX);
2452 * If this peer is a gateway, call the routing callback to
2453 * handle the ping reply
2455 if (lp->lp_rtr_refcount > 0)
2456 lnet_router_discovery_ping_reply(lp);
2457 lnet_net_unlock(LNET_LOCK_EX);
2461 * Send event handling. Only matters for error cases, where we clean
2462 * up state on the peer and peer_ni that would otherwise be updated in
2463 * the REPLY event handler for a successful Ping, and the ACK event
2464 * handler for a successful Push.
2467 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2474 spin_lock(&lp->lp_lock);
2475 if (ev->msg_type == LNET_MSG_GET) {
2476 lp->lp_state &= ~LNET_PEER_PING_SENT;
2477 lp->lp_state |= LNET_PEER_PING_FAILED;
2478 lp->lp_ping_error = ev->status;
2479 } else { /* ev->msg_type == LNET_MSG_PUT */
2480 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2481 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2482 lp->lp_push_error = ev->status;
2484 spin_unlock(&lp->lp_lock);
2485 rc = LNET_REDISCOVER_PEER;
2487 CDEBUG(D_NET, "%s Send to %s: %d\n",
2488 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2489 libcfs_nid2str(ev->target.nid), rc);
2494 * Unlink event handling. This event is only seen if a call to
2495 * LNetMDUnlink() caused the event to be unlinked. If this call was
2496 * made after the event was set up in LNetGet() or LNetPut() then we
2497 * assume the Ping or Push timed out.
2500 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2502 spin_lock(&lp->lp_lock);
2503 /* We've passed through LNetGet() */
2504 if (lp->lp_state & LNET_PEER_PING_SENT) {
2505 lp->lp_state &= ~LNET_PEER_PING_SENT;
2506 lp->lp_state |= LNET_PEER_PING_FAILED;
2507 lp->lp_ping_error = -ETIMEDOUT;
2508 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2509 libcfs_nid2str(lp->lp_primary_nid));
2511 /* We've passed through LNetPut() */
2512 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2513 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2514 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2515 lp->lp_push_error = -ETIMEDOUT;
2516 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2517 libcfs_nid2str(lp->lp_primary_nid));
2519 spin_unlock(&lp->lp_lock);
2523 * Event handler for the discovery EQ.
2525 * Called with lnet_res_lock(cpt) held. The cpt is the
2526 * lnet_cpt_of_cookie() of the md handle cookie.
2528 static void lnet_discovery_event_handler(struct lnet_event *event)
2530 struct lnet_peer *lp = event->md_user_ptr;
2531 struct lnet_ping_buffer *pbuf;
2534 /* discovery needs to take another look */
2535 rc = LNET_REDISCOVER_PEER;
2537 CDEBUG(D_NET, "Received event: %d\n", event->type);
2539 switch (event->type) {
2540 case LNET_EVENT_ACK:
2541 lnet_discovery_event_ack(lp, event);
2543 case LNET_EVENT_REPLY:
2544 lnet_discovery_event_reply(lp, event);
2546 case LNET_EVENT_SEND:
2547 /* Only send failure triggers a retry. */
2548 rc = lnet_discovery_event_send(lp, event);
2550 case LNET_EVENT_UNLINK:
2551 /* LNetMDUnlink() was called */
2552 lnet_discovery_event_unlink(lp, event);
2555 /* Invalid events. */
2558 lnet_net_lock(LNET_LOCK_EX);
2559 if (event->unlinked) {
2560 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2561 lnet_ping_buffer_decref(pbuf);
2562 lnet_peer_decref_locked(lp);
2565 /* put peer back at end of request queue, if discovery not already
2567 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2568 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2569 wake_up(&the_lnet.ln_dc_waitq);
2571 lnet_net_unlock(LNET_LOCK_EX);
2575 * Build a peer from incoming data.
2577 * The NIDs in the incoming data are supposed to be structured as follows:
2580 * - other NIDs in same net
2581 * - NIDs in second net
2582 * - NIDs in third net
2584 * This due to the way the list of NIDs in the data is created.
2586 * Note that this function will mark the peer uptodate unless an
2587 * ENOMEM is encontered. All other errors are due to a conflict
2588 * between the DLC configuration and what discovery sees. We treat DLC
2589 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2590 * peer from becoming stuck in discovery.
2592 static int lnet_peer_merge_data(struct lnet_peer *lp,
2593 struct lnet_ping_buffer *pbuf)
2595 struct lnet_peer_ni *lpni;
2596 lnet_nid_t *curnis = NULL;
2597 struct lnet_ni_status *addnis = NULL;
2598 lnet_nid_t *delnis = NULL;
2608 flags = LNET_PEER_DISCOVERED;
2609 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2610 flags |= LNET_PEER_MULTI_RAIL;
2613 * Cache the routing feature for the peer; whether it is enabled
2614 * for disabled as reported by the remote peer.
2616 spin_lock(&lp->lp_lock);
2617 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2618 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2620 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2621 spin_unlock(&lp->lp_lock);
2623 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2624 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2625 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2626 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2627 if (!curnis || !addnis || !delnis) {
2635 /* Construct the list of NIDs present in peer. */
2637 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2638 curnis[ncurnis++] = lpni->lpni_nid;
2641 * Check for NIDs in pbuf not present in curnis[].
2642 * The loop starts at 1 to skip the loopback NID.
2644 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2645 for (j = 0; j < ncurnis; j++)
2646 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2649 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2652 * Check for NIDs in curnis[] not present in pbuf.
2653 * The nested loop starts at 1 to skip the loopback NID.
2655 * But never add the loopback NID to delnis[]: if it is
2656 * present in curnis[] then this peer is for this node.
2658 for (i = 0; i < ncurnis; i++) {
2659 if (curnis[i] == LNET_NID_LO_0)
2661 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2662 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2664 * update the information we cache for the
2665 * peer with the latest information we
2668 lpni = lnet_find_peer_ni_locked(curnis[i]);
2670 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2671 lnet_peer_ni_decref_locked(lpni);
2676 if (j == pbuf->pb_info.pi_nnis)
2677 delnis[ndelnis++] = curnis[i];
2681 * If we get here and the discovery is disabled then we don't want
2682 * to add or delete any NIs. We just updated the ones we have some
2683 * information on, and call it a day
2686 if (lnet_is_discovery_disabled(lp))
2689 for (i = 0; i < naddnis; i++) {
2690 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2692 CERROR("Error adding NID %s to peer %s: %d\n",
2693 libcfs_nid2str(addnis[i].ns_nid),
2694 libcfs_nid2str(lp->lp_primary_nid), rc);
2698 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2700 lpni->lpni_ns_status = addnis[i].ns_status;
2701 lnet_peer_ni_decref_locked(lpni);
2705 for (i = 0; i < ndelnis; i++) {
2707 * for routers it's okay to delete the primary_nid because
2708 * the upper layers don't really rely on it. So if we're
2709 * being told that the router changed its primary_nid
2710 * then it's okay to delete it.
2712 if (lp->lp_rtr_refcount > 0)
2713 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2714 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2716 CERROR("Error deleting NID %s from peer %s: %d\n",
2717 libcfs_nid2str(delnis[i]),
2718 libcfs_nid2str(lp->lp_primary_nid), rc);
2724 * Errors other than -ENOMEM are due to peers having been
2725 * configured with DLC. Ignore these because DLC overrides
2730 CFS_FREE_PTR_ARRAY(curnis, nnis);
2731 CFS_FREE_PTR_ARRAY(addnis, nnis);
2732 CFS_FREE_PTR_ARRAY(delnis, nnis);
2733 lnet_ping_buffer_decref(pbuf);
2734 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2737 spin_lock(&lp->lp_lock);
2738 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2739 lp->lp_state |= LNET_PEER_FORCE_PING;
2740 spin_unlock(&lp->lp_lock);
2746 * The data in pbuf says lp is its primary peer, but the data was
2747 * received by a different peer. Try to update lp with the data.
2750 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2752 struct lnet_handle_md mdh;
2754 /* Queue lp for discovery, and force it on the request queue. */
2755 lnet_net_lock(LNET_LOCK_EX);
2756 if (lnet_peer_queue_for_discovery(lp))
2757 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2758 lnet_net_unlock(LNET_LOCK_EX);
2760 LNetInvalidateMDHandle(&mdh);
2763 * Decide whether we can move the peer to the DATA_PRESENT state.
2765 * We replace stale data for a multi-rail peer, repair PING_FAILED
2766 * status, and preempt FORCE_PING.
2768 * If after that we have DATA_PRESENT, we merge it into this peer.
2770 spin_lock(&lp->lp_lock);
2771 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2772 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2773 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2774 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2775 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2776 lnet_ping_buffer_decref(pbuf);
2781 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2782 lnet_ping_buffer_decref(lp->lp_data);
2784 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2786 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2787 mdh = lp->lp_ping_mdh;
2788 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2789 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2790 lp->lp_ping_error = 0;
2792 if (lp->lp_state & LNET_PEER_FORCE_PING)
2793 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2794 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2795 spin_unlock(&lp->lp_lock);
2797 if (!LNetMDHandleIsInvalid(mdh))
2801 return lnet_peer_merge_data(lp, pbuf);
2803 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2807 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2811 for (i = 0; i < pinfo->pi_nnis; i++) {
2812 if (pinfo->pi_ni[i].ns_nid == nid)
2820 * Update a peer using the data received.
2822 static int lnet_peer_data_present(struct lnet_peer *lp)
2823 __must_hold(&lp->lp_lock)
2825 struct lnet_ping_buffer *pbuf;
2826 struct lnet_peer_ni *lpni;
2827 lnet_nid_t nid = LNET_NID_ANY;
2833 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2834 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2835 spin_unlock(&lp->lp_lock);
2838 * Modifications of peer structures are done while holding the
2839 * ln_api_mutex. A global lock is required because we may be
2840 * modifying multiple peer structures, and a mutex greatly
2841 * simplifies memory management.
2843 * The actual changes to the data structures must also protect
2844 * against concurrent lookups, for which the lnet_net_lock in
2845 * LNET_LOCK_EX mode is used.
2847 mutex_lock(&the_lnet.ln_api_mutex);
2848 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2854 * If this peer is not on the peer list then it is being torn
2855 * down, and our reference count may be all that is keeping it
2856 * alive. Don't do any work on it.
2858 if (list_empty(&lp->lp_peer_list))
2861 flags = LNET_PEER_DISCOVERED;
2862 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2863 flags |= LNET_PEER_MULTI_RAIL;
2866 * Check whether the primary NID in the message matches the
2867 * primary NID of the peer. If it does, update the peer, if
2868 * it it does not, check whether there is already a peer with
2869 * that primary NID. If no such peer exists, try to update
2870 * the primary NID of the current peer (allowed if it was
2871 * created due to message traffic) and complete the update.
2872 * If the peer did exist, hand off the data to it.
2874 * The peer for the loopback interface is a special case: this
2875 * is the peer for the local node, and we want to set its
2876 * primary NID to the correct value here. Moreover, this peer
2877 * can show up with only the loopback NID in the ping buffer.
2879 if (pbuf->pb_info.pi_nnis <= 1)
2881 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2882 if (lp->lp_primary_nid == LNET_NID_LO_0) {
2883 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2885 rc = lnet_peer_merge_data(lp, pbuf);
2887 * if the primary nid of the peer is present in the ping info returned
2888 * from the peer, but it's not the local primary peer we have
2889 * cached and discovery is disabled, then we don't want to update
2890 * our local peer info, by adding or removing NIDs, we just want
2891 * to update the status of the nids that we currently have
2892 * recorded in that peer.
2894 } else if (lp->lp_primary_nid == nid ||
2895 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2896 lnet_is_discovery_disabled(lp))) {
2897 rc = lnet_peer_merge_data(lp, pbuf);
2899 lpni = lnet_find_peer_ni_locked(nid);
2901 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2903 CERROR("Primary NID error %s versus %s: %d\n",
2904 libcfs_nid2str(lp->lp_primary_nid),
2905 libcfs_nid2str(nid), rc);
2907 rc = lnet_peer_merge_data(lp, pbuf);
2910 struct lnet_peer *new_lp;
2911 new_lp = lpni->lpni_peer_net->lpn_peer;
2913 * if lp has discovery/MR enabled that means new_lp
2914 * should have discovery/MR enabled as well, since
2915 * it's the same peer, which we're about to merge
2917 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2918 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2919 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2920 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2922 rc = lnet_peer_set_primary_data(new_lp, pbuf);
2923 lnet_consolidate_routes_locked(lp, new_lp);
2924 lnet_peer_ni_decref_locked(lpni);
2928 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2930 mutex_unlock(&the_lnet.ln_api_mutex);
2932 spin_lock(&lp->lp_lock);
2933 /* Tell discovery to re-check the peer immediately. */
2935 rc = LNET_REDISCOVER_PEER;
2940 * A ping failed. Clear the PING_FAILED state and set the
2941 * FORCE_PING state, to ensure a retry even if discovery is
2942 * disabled. This avoids being left with incorrect state.
2944 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2945 __must_hold(&lp->lp_lock)
2947 struct lnet_handle_md mdh;
2950 mdh = lp->lp_ping_mdh;
2951 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2952 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2953 lp->lp_state |= LNET_PEER_FORCE_PING;
2954 rc = lp->lp_ping_error;
2955 lp->lp_ping_error = 0;
2956 spin_unlock(&lp->lp_lock);
2958 if (!LNetMDHandleIsInvalid(mdh))
2961 CDEBUG(D_NET, "peer %s:%d\n",
2962 libcfs_nid2str(lp->lp_primary_nid), rc);
2964 spin_lock(&lp->lp_lock);
2965 return rc ? rc : LNET_REDISCOVER_PEER;
2969 * Select NID to send a Ping or Push to.
2971 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2973 struct lnet_peer_ni *lpni;
2975 /* Look for a direct-connected NID for this peer. */
2977 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2978 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2983 return lpni->lpni_nid;
2985 /* Look for a routed-connected NID for this peer. */
2987 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2988 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2993 return lpni->lpni_nid;
2995 return LNET_NID_ANY;
2998 /* Active side of ping. */
2999 static int lnet_peer_send_ping(struct lnet_peer *lp)
3000 __must_hold(&lp->lp_lock)
3007 lp->lp_state |= LNET_PEER_PING_SENT;
3008 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3009 spin_unlock(&lp->lp_lock);
3011 cpt = lnet_net_lock_current();
3012 /* Refcount for MD. */
3013 lnet_peer_addref_locked(lp);
3014 pnid = lnet_peer_select_nid(lp);
3015 lnet_net_unlock(cpt);
3017 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3019 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
3020 the_lnet.ln_dc_handler, false);
3023 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3024 * refcount on the peer, otherwise LNetMDUnlink will be called
3025 * which will eventually do that.
3029 lnet_peer_decref_locked(lp);
3030 lnet_net_unlock(cpt);
3031 rc = -rc; /* change the rc to negative value */
3033 } else if (rc < 0) {
3037 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3039 spin_lock(&lp->lp_lock);
3043 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3045 * The errors that get us here are considered hard errors and
3046 * cause Discovery to terminate. So we clear PING_SENT, but do
3047 * not set either PING_FAILED or FORCE_PING. In fact we need
3048 * to clear PING_FAILED, because the unlink event handler will
3049 * have set it if we called LNetMDUnlink() above.
3051 spin_lock(&lp->lp_lock);
3052 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3057 * This function exists because you cannot call LNetMDUnlink() from an
3060 static int lnet_peer_push_failed(struct lnet_peer *lp)
3061 __must_hold(&lp->lp_lock)
3063 struct lnet_handle_md mdh;
3066 mdh = lp->lp_push_mdh;
3067 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3068 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3069 rc = lp->lp_push_error;
3070 lp->lp_push_error = 0;
3071 spin_unlock(&lp->lp_lock);
3073 if (!LNetMDHandleIsInvalid(mdh))
3076 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3077 spin_lock(&lp->lp_lock);
3078 return rc ? rc : LNET_REDISCOVER_PEER;
3082 * Mark the peer as discovered.
3084 static int lnet_peer_discovered(struct lnet_peer *lp)
3085 __must_hold(&lp->lp_lock)
3087 lp->lp_state |= LNET_PEER_DISCOVERED;
3088 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3089 LNET_PEER_REDISCOVER);
3091 lp->lp_dc_error = 0;
3093 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3098 /* Active side of push. */
3099 static int lnet_peer_send_push(struct lnet_peer *lp)
3100 __must_hold(&lp->lp_lock)
3102 struct lnet_ping_buffer *pbuf;
3103 struct lnet_process_id id;
3108 /* Don't push to a non-multi-rail peer. */
3109 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3110 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3111 /* if peer's NIDs are uptodate then peer is discovered */
3112 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3113 rc = lnet_peer_discovered(lp);
3120 lp->lp_state |= LNET_PEER_PUSH_SENT;
3121 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3122 spin_unlock(&lp->lp_lock);
3124 cpt = lnet_net_lock_current();
3125 pbuf = the_lnet.ln_ping_target;
3126 lnet_ping_buffer_addref(pbuf);
3127 lnet_net_unlock(cpt);
3129 /* Push source MD */
3130 md.start = &pbuf->pb_info;
3131 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3132 md.threshold = 2; /* Put/Ack */
3134 md.options = LNET_MD_TRACK_RESPONSE;
3135 md.handler = the_lnet.ln_dc_handler;
3138 rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3140 lnet_ping_buffer_decref(pbuf);
3141 CERROR("Can't bind push source MD: %d\n", rc);
3144 cpt = lnet_net_lock_current();
3145 /* Refcount for MD. */
3146 lnet_peer_addref_locked(lp);
3147 id.pid = LNET_PID_LUSTRE;
3148 id.nid = lnet_peer_select_nid(lp);
3149 lnet_net_unlock(cpt);
3151 if (id.nid == LNET_NID_ANY) {
3156 rc = LNetPut(lp->lp_disc_src_nid, lp->lp_push_mdh,
3157 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3158 LNET_PROTO_PING_MATCHBITS, 0, 0);
3161 * reset the discovery nid. There is no need to restrict sending
3162 * from that source, if we call lnet_push_update_to_peers(). It'll
3163 * get set to a specific NID, if we initiate discovery from the
3166 lp->lp_disc_src_nid = LNET_NID_ANY;
3171 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3173 spin_lock(&lp->lp_lock);
3177 LNetMDUnlink(lp->lp_push_mdh);
3178 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3180 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3182 * The errors that get us here are considered hard errors and
3183 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3184 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3185 * because the unlink event handler will have set it if we
3186 * called LNetMDUnlink() above.
3188 spin_lock(&lp->lp_lock);
3189 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3194 * An unrecoverable error was encountered during discovery.
3195 * Set error status in peer and abort discovery.
3197 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3199 CDEBUG(D_NET, "Discovery error %s: %d\n",
3200 libcfs_nid2str(lp->lp_primary_nid), error);
3202 spin_lock(&lp->lp_lock);
3203 lp->lp_dc_error = error;
3204 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3205 lp->lp_state |= LNET_PEER_REDISCOVER;
3206 spin_unlock(&lp->lp_lock);
3210 * Discovering this peer is taking too long. Cancel any Ping or Push
3211 * that discovery is waiting on by unlinking the relevant MDs. The
3212 * lnet_discovery_event_handler() will proceed from here and complete
3215 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3217 struct lnet_handle_md ping_mdh;
3218 struct lnet_handle_md push_mdh;
3220 LNetInvalidateMDHandle(&ping_mdh);
3221 LNetInvalidateMDHandle(&push_mdh);
3223 spin_lock(&lp->lp_lock);
3224 if (lp->lp_state & LNET_PEER_PING_SENT) {
3225 ping_mdh = lp->lp_ping_mdh;
3226 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3228 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3229 push_mdh = lp->lp_push_mdh;
3230 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3232 spin_unlock(&lp->lp_lock);
3234 if (!LNetMDHandleIsInvalid(ping_mdh))
3235 LNetMDUnlink(ping_mdh);
3236 if (!LNetMDHandleIsInvalid(push_mdh))
3237 LNetMDUnlink(push_mdh);
3241 * Wait for work to be queued or some other change that must be
3242 * attended to. Returns non-zero if the discovery thread should shut
3245 static int lnet_peer_discovery_wait_for_work(void)
3252 cpt = lnet_net_lock_current();
3254 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3255 TASK_INTERRUPTIBLE);
3256 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3258 if (lnet_push_target_resize_needed() ||
3259 the_lnet.ln_push_target->pb_needs_post)
3261 if (!list_empty(&the_lnet.ln_dc_request))
3263 if (!list_empty(&the_lnet.ln_msg_resend))
3265 lnet_net_unlock(cpt);
3268 * wakeup max every second to check if there are peers that
3269 * have been stuck on the working queue for greater than
3272 schedule_timeout(cfs_time_seconds(1));
3273 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3274 cpt = lnet_net_lock_current();
3276 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3278 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3281 lnet_net_unlock(cpt);
3283 CDEBUG(D_NET, "woken: %d\n", rc);
3289 * Messages that were pending on a destroyed peer will be put on a global
3290 * resend list. The message resend list will be checked by
3291 * the discovery thread when it wakes up, and will resend messages. These
3292 * messages can still be sendable in the case the lpni which was the initial
3293 * cause of the message re-queue was transfered to another peer.
3295 * It is possible that LNet could be shutdown while we're iterating
3296 * through the list. lnet_shudown_lndnets() will attempt to access the
3297 * resend list, but will have to wait until the spinlock is released, by
3298 * which time there shouldn't be any more messages on the resend list.
3299 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3300 * for the messages so they can be released. The other case is that
3301 * lnet_shudown_lndnets() can finalize all the messages before this
3302 * function can visit the resend list, in which case this function will be
3305 static void lnet_resend_msgs(void)
3307 struct lnet_msg *msg, *tmp;
3311 spin_lock(&the_lnet.ln_msg_resend_lock);
3312 list_splice(&the_lnet.ln_msg_resend, &resend);
3313 spin_unlock(&the_lnet.ln_msg_resend_lock);
3315 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3316 list_del_init(&msg->msg_list);
3317 rc = lnet_send(msg->msg_src_nid_param, msg,
3318 msg->msg_rtr_nid_param);
3320 CNETERR("Error sending %s to %s: %d\n",
3321 lnet_msgtyp2str(msg->msg_type),
3322 libcfs_id2str(msg->msg_target), rc);
3323 lnet_finalize(msg, rc);
3328 /* The discovery thread. */
3329 static int lnet_peer_discovery(void *arg)
3331 struct lnet_peer *lp;
3334 wait_for_completion(&the_lnet.ln_started);
3336 CDEBUG(D_NET, "started\n");
3339 if (lnet_peer_discovery_wait_for_work())
3342 if (lnet_push_target_resize_needed())
3343 lnet_push_target_resize();
3344 else if (the_lnet.ln_push_target->pb_needs_post)
3345 lnet_push_target_post(the_lnet.ln_push_target,
3346 &the_lnet.ln_push_target_md);
3350 lnet_net_lock(LNET_LOCK_EX);
3351 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3352 lnet_net_unlock(LNET_LOCK_EX);
3357 * Process all incoming discovery work requests. When
3358 * discovery must wait on a peer to change state, it
3359 * is added to the tail of the ln_dc_working queue. A
3360 * timestamp keeps track of when the peer was added,
3361 * so we can time out discovery requests that take too
3364 while (!list_empty(&the_lnet.ln_dc_request)) {
3365 lp = list_first_entry(&the_lnet.ln_dc_request,
3366 struct lnet_peer, lp_dc_list);
3367 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3369 * set the time the peer was put on the dc_working
3370 * queue. It shouldn't remain on the queue
3371 * forever, in case the GET message (for ping)
3372 * doesn't get a REPLY or the PUT message (for
3373 * push) doesn't get an ACK.
3375 lp->lp_last_queued = ktime_get_real_seconds();
3376 lnet_net_unlock(LNET_LOCK_EX);
3378 if (lnet_push_target_resize_needed())
3379 lnet_push_target_resize();
3380 else if (the_lnet.ln_push_target->pb_needs_post)
3381 lnet_push_target_post(the_lnet.ln_push_target,
3382 &the_lnet.ln_push_target_md);
3385 * Select an action depending on the state of
3386 * the peer and whether discovery is disabled.
3387 * The check whether discovery is disabled is
3388 * done after the code that handles processing
3389 * for arrived data, cleanup for failures, and
3390 * forcing a Ping or Push.
3392 spin_lock(&lp->lp_lock);
3393 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3394 libcfs_nid2str(lp->lp_primary_nid), lp,
3396 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3397 rc = lnet_peer_data_present(lp);
3398 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3399 rc = lnet_peer_ping_failed(lp);
3400 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3401 rc = lnet_peer_push_failed(lp);
3402 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3403 rc = lnet_peer_send_ping(lp);
3404 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3405 rc = lnet_peer_send_push(lp);
3406 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3407 rc = lnet_peer_send_ping(lp);
3408 else if (lnet_peer_needs_push(lp))
3409 rc = lnet_peer_send_push(lp);
3411 rc = lnet_peer_discovered(lp);
3412 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3413 libcfs_nid2str(lp->lp_primary_nid), lp,
3415 spin_unlock(&lp->lp_lock);
3417 lnet_net_lock(LNET_LOCK_EX);
3418 if (rc == LNET_REDISCOVER_PEER) {
3419 list_move(&lp->lp_dc_list,
3420 &the_lnet.ln_dc_request);
3422 lnet_peer_discovery_error(lp, rc);
3424 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3425 lnet_peer_discovery_complete(lp);
3426 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3429 if (lp->lp_state & LNET_PEER_MARK_DELETION) {
3430 struct list_head rlist;
3431 struct lnet_route *route, *tmp;
3432 int sensitivity = lp->lp_health_sensitivity;
3434 INIT_LIST_HEAD(&rlist);
3437 * remove the peer from the discovery work
3438 * queue if it's on there in preparation
3441 if (!list_empty(&lp->lp_dc_list))
3442 list_del(&lp->lp_dc_list);
3444 lnet_net_unlock(LNET_LOCK_EX);
3446 mutex_lock(&the_lnet.ln_api_mutex);
3448 lnet_net_lock(LNET_LOCK_EX);
3449 list_for_each_entry_safe(route, tmp,
3452 lnet_move_route(route, NULL, &rlist);
3453 lnet_net_unlock(LNET_LOCK_EX);
3455 /* delete the peer */
3458 list_for_each_entry_safe(route, tmp,
3460 /* re-add these routes */
3461 lnet_add_route(route->lr_net,
3466 LIBCFS_FREE(route, sizeof(*route));
3468 mutex_unlock(&the_lnet.ln_api_mutex);
3470 lnet_net_lock(LNET_LOCK_EX);
3474 lnet_net_unlock(LNET_LOCK_EX);
3477 CDEBUG(D_NET, "stopping\n");
3479 * Clean up before telling lnet_peer_discovery_stop() that
3480 * we're done. Use wake_up() below to somewhat reduce the
3481 * size of the thundering herd if there are multiple threads
3482 * waiting on discovery of a single peer.
3485 /* Queue cleanup 1: stop all pending pings and pushes. */
3486 lnet_net_lock(LNET_LOCK_EX);
3487 while (!list_empty(&the_lnet.ln_dc_working)) {
3488 lp = list_first_entry(&the_lnet.ln_dc_working,
3489 struct lnet_peer, lp_dc_list);
3490 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3491 lnet_net_unlock(LNET_LOCK_EX);
3492 lnet_peer_cancel_discovery(lp);
3493 lnet_net_lock(LNET_LOCK_EX);
3495 lnet_net_unlock(LNET_LOCK_EX);
3497 /* Queue cleanup 2: wait for the expired queue to clear. */
3498 while (!list_empty(&the_lnet.ln_dc_expired))
3499 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3501 /* Queue cleanup 3: clear the request queue. */
3502 lnet_net_lock(LNET_LOCK_EX);
3503 while (!list_empty(&the_lnet.ln_dc_request)) {
3504 lp = list_first_entry(&the_lnet.ln_dc_request,
3505 struct lnet_peer, lp_dc_list);
3506 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3507 lnet_peer_discovery_complete(lp);
3509 lnet_net_unlock(LNET_LOCK_EX);
3511 lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3512 the_lnet.ln_dc_handler = NULL;
3514 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3515 wake_up(&the_lnet.ln_dc_waitq);
3517 CDEBUG(D_NET, "stopped\n");
3522 /* ln_api_mutex is held on entry. */
3523 int lnet_peer_discovery_start(void)
3525 struct task_struct *task;
3528 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3531 the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3532 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3533 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3536 CERROR("Can't start peer discovery thread: %d\n", rc);
3538 the_lnet.ln_dc_handler = NULL;
3540 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3543 CDEBUG(D_NET, "discovery start: %d\n", rc);
3548 /* ln_api_mutex is held on entry. */
3549 void lnet_peer_discovery_stop(void)
3551 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3554 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3555 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3557 /* In the LNetNIInit() path we may be stopping discovery before it
3558 * entered its work loop
3560 if (!completion_done(&the_lnet.ln_started))
3561 complete(&the_lnet.ln_started);
3563 wake_up(&the_lnet.ln_dc_waitq);
3565 wait_event(the_lnet.ln_dc_waitq,
3566 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3568 LASSERT(list_empty(&the_lnet.ln_dc_request));
3569 LASSERT(list_empty(&the_lnet.ln_dc_working));
3570 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3572 CDEBUG(D_NET, "discovery stopped\n");
3578 lnet_debug_peer(lnet_nid_t nid)
3580 char *aliveness = "NA";
3581 struct lnet_peer_ni *lp;
3584 cpt = lnet_cpt_of_nid(nid, NULL);
3587 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3589 lnet_net_unlock(cpt);
3590 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3594 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3595 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3597 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3598 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3599 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3600 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3601 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3603 lnet_peer_ni_decref_locked(lp);
3605 lnet_net_unlock(cpt);
3608 /* Gathering information for userspace. */
3610 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3611 char aliveness[LNET_MAX_STR_LEN],
3612 __u32 *cpt_iter, __u32 *refcount,
3613 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3614 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3615 __u32 *peer_tx_qnob)
3617 struct lnet_peer_table *peer_table;
3618 struct lnet_peer_ni *lp;
3623 /* get the number of CPTs */
3624 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3626 /* if the cpt number to be examined is >= the number of cpts in
3627 * the system then indicate that there are no more cpts to examin
3629 if (*cpt_iter >= lncpt)
3632 /* get the current table */
3633 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3634 /* if the ptable is NULL then there are no more cpts to examine */
3635 if (peer_table == NULL)
3638 lnet_net_lock(*cpt_iter);
3640 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3641 struct list_head *peers = &peer_table->pt_hash[j];
3643 list_for_each_entry(lp, peers, lpni_hashlist) {
3644 if (peer_index-- > 0)
3647 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3648 if (lnet_isrouter(lp) ||
3649 lnet_peer_aliveness_enabled(lp))
3650 snprintf(aliveness, LNET_MAX_STR_LEN,
3651 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3653 *nid = lp->lpni_nid;
3654 *refcount = atomic_read(&lp->lpni_refcount);
3655 *ni_peer_tx_credits =
3656 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3657 *peer_tx_credits = lp->lpni_txcredits;
3658 *peer_rtr_credits = lp->lpni_rtrcredits;
3659 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3660 *peer_tx_qnob = lp->lpni_txqnob;
3666 lnet_net_unlock(*cpt_iter);
3670 return found ? 0 : -ENOENT;
3673 /* ln_api_mutex is held, which keeps the peer list stable */
3674 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3676 struct lnet_ioctl_element_stats *lpni_stats;
3677 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3678 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3679 struct lnet_peer_ni_credit_info *lpni_info;
3680 struct lnet_peer_ni *lpni;
3681 struct lnet_peer *lp;
3686 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3693 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3694 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3695 size *= lp->lp_nnis;
3696 if (size > cfg->prcfg_size) {
3697 cfg->prcfg_size = size;
3702 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3703 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3704 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3705 cfg->prcfg_count = lp->lp_nnis;
3706 cfg->prcfg_size = size;
3707 cfg->prcfg_state = lp->lp_state;
3709 /* Allocate helper buffers. */
3711 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3714 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3717 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3718 if (!lpni_msg_stats)
3719 goto out_free_stats;
3720 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3722 goto out_free_msg_stats;
3727 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3728 nid = lpni->lpni_nid;
3729 if (copy_to_user(bulk, &nid, sizeof(nid)))
3730 goto out_free_hstats;
3731 bulk += sizeof(nid);
3733 memset(lpni_info, 0, sizeof(*lpni_info));
3734 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3735 if (lnet_isrouter(lpni) ||
3736 lnet_peer_aliveness_enabled(lpni))
3737 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3738 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3740 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3741 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3742 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3743 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3744 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3745 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3746 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3747 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3748 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3749 goto out_free_hstats;
3750 bulk += sizeof(*lpni_info);
3752 memset(lpni_stats, 0, sizeof(*lpni_stats));
3753 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3754 LNET_STATS_TYPE_SEND);
3755 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3756 LNET_STATS_TYPE_RECV);
3757 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3758 LNET_STATS_TYPE_DROP);
3759 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3760 goto out_free_hstats;
3761 bulk += sizeof(*lpni_stats);
3762 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3763 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3764 goto out_free_hstats;
3765 bulk += sizeof(*lpni_msg_stats);
3766 lpni_hstats->hlpni_network_timeout =
3767 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3768 lpni_hstats->hlpni_remote_dropped =
3769 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3770 lpni_hstats->hlpni_remote_timeout =
3771 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3772 lpni_hstats->hlpni_remote_error =
3773 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3774 lpni_hstats->hlpni_health_value =
3775 atomic_read(&lpni->lpni_healthv);
3776 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3777 goto out_free_hstats;
3778 bulk += sizeof(*lpni_hstats);
3783 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3785 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3787 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3789 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3791 lnet_peer_decref_locked(lp);
3797 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3799 /* the mt could've shutdown and cleaned up the queues */
3800 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3803 if (list_empty(&lpni->lpni_recovery) &&
3804 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3805 CDEBUG(D_NET, "lpni %s added to recovery queue. Health = %d\n",
3806 libcfs_nid2str(lpni->lpni_nid),
3807 atomic_read(&lpni->lpni_healthv));
3808 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3809 lnet_peer_ni_addref_locked(lpni);
3813 /* Call with the ln_api_mutex held */
3815 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3817 struct lnet_peer_table *ptable;
3818 struct lnet_peer *lp;
3819 struct lnet_peer_net *lpn;
3820 struct lnet_peer_ni *lpni;
3824 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3828 lnet_net_lock(LNET_LOCK_EX);
3829 lpni = lnet_find_peer_ni_locked(nid);
3831 lnet_net_unlock(LNET_LOCK_EX);
3834 atomic_set(&lpni->lpni_healthv, value);
3835 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3836 lnet_peer_ni_decref_locked(lpni);
3837 lnet_net_unlock(LNET_LOCK_EX);
3841 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3844 * Walk all the peers and reset the healhv for each one to the
3847 lnet_net_lock(LNET_LOCK_EX);
3848 for (cpt = 0; cpt < lncpt; cpt++) {
3849 ptable = the_lnet.ln_peer_tables[cpt];
3850 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3851 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3852 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3854 atomic_set(&lpni->lpni_healthv, value);
3855 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3860 lnet_net_unlock(LNET_LOCK_EX);