4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
34 #define DEBUG_SUBSYSTEM S_LNET
36 #include <linux/sched.h>
37 #ifdef HAVE_SCHED_HEADERS
38 #include <linux/sched/signal.h>
40 #include <linux/uaccess.h>
42 #include <lnet/udsp.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER (1)
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
54 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
55 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
56 lnet_peer_ni_decref_locked(lpni);
61 lnet_peer_net_added(struct lnet_net *net)
63 struct lnet_peer_ni *lpni, *tmp;
65 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66 lpni_on_remote_peer_ni_list) {
68 if (LNET_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 kref_init(&lpni->lpni_kref);
172 lpni->lpni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
174 spin_lock_init(&lpni->lpni_lock);
176 if (lnet_peers_start_down())
177 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
179 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
180 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
181 lpni->lpni_nid = nid;
182 lpni->lpni_cpt = cpt;
183 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
185 net = lnet_get_net_locked(LNET_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 spin_lock(&peer->lp_lock);
462 peer->lp_state |= LNET_PEER_MARK_DELETED;
463 spin_unlock(&peer->lp_lock);
465 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
466 while (lpni != NULL) {
467 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
468 rc = lnet_peer_ni_del_locked(lpni, false);
478 * Discovering this peer is taking too long. Cancel any Ping or Push
479 * that discovery is waiting on by unlinking the relevant MDs. The
480 * lnet_discovery_event_handler() will proceed from here and complete
483 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
485 struct lnet_handle_md ping_mdh;
486 struct lnet_handle_md push_mdh;
488 LNetInvalidateMDHandle(&ping_mdh);
489 LNetInvalidateMDHandle(&push_mdh);
491 spin_lock(&lp->lp_lock);
492 if (lp->lp_state & LNET_PEER_PING_SENT) {
493 ping_mdh = lp->lp_ping_mdh;
494 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
496 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
497 push_mdh = lp->lp_push_mdh;
498 LNetInvalidateMDHandle(&lp->lp_push_mdh);
500 spin_unlock(&lp->lp_lock);
502 if (!LNetMDHandleIsInvalid(ping_mdh))
503 LNetMDUnlink(ping_mdh);
504 if (!LNetMDHandleIsInvalid(push_mdh))
505 LNetMDUnlink(push_mdh);
509 lnet_peer_del(struct lnet_peer *peer)
511 lnet_peer_cancel_discovery(peer);
512 lnet_net_lock(LNET_LOCK_EX);
513 lnet_peer_del_locked(peer);
514 lnet_net_unlock(LNET_LOCK_EX);
520 * Delete a NID from a peer. Call with ln_api_mutex held.
523 * -EPERM: Non-DLC deletion from DLC-configured peer.
524 * -ENOENT: No lnet_peer_ni corresponding to the nid.
525 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
526 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
529 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
531 struct lnet_peer_ni *lpni;
532 lnet_nid_t primary_nid = lp->lp_primary_nid;
534 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
536 if (!(flags & LNET_PEER_CONFIGURED)) {
537 if (lp->lp_state & LNET_PEER_CONFIGURED) {
542 lpni = lnet_find_peer_ni_locked(nid);
547 lnet_peer_ni_decref_locked(lpni);
548 if (lp != lpni->lpni_peer_net->lpn_peer) {
554 * This function only allows deletion of the primary NID if it
557 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
562 lnet_net_lock(LNET_LOCK_EX);
564 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
565 struct lnet_peer_ni *lpni2;
566 /* assign the next peer_ni to be the primary */
567 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
569 lp->lp_primary_nid = lpni2->lpni_nid;
571 rc = lnet_peer_ni_del_locked(lpni, force);
573 lnet_net_unlock(LNET_LOCK_EX);
576 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
577 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
583 lnet_peer_table_cleanup_locked(struct lnet_net *net,
584 struct lnet_peer_table *ptable)
587 struct lnet_peer_ni *next;
588 struct lnet_peer_ni *lpni;
589 struct lnet_peer *peer;
591 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
592 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
594 if (net != NULL && net != lpni->lpni_net)
597 peer = lpni->lpni_peer_net->lpn_peer;
598 if (peer->lp_primary_nid != lpni->lpni_nid) {
599 lnet_peer_ni_del_locked(lpni, false);
603 * Removing the primary NID implies removing
604 * the entire peer. Advance next beyond any
605 * peer_ni that belongs to the same peer.
607 list_for_each_entry_from(next, &ptable->pt_hash[i],
609 if (next->lpni_peer_net->lpn_peer != peer)
612 lnet_peer_del_locked(peer);
618 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
620 wait_var_event_warning(&ptable->pt_zombies,
621 ptable->pt_zombies == 0,
622 "Waiting for %d zombies on peer table\n",
627 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
628 struct lnet_peer_table *ptable)
630 struct lnet_peer_ni *lp;
631 struct lnet_peer_ni *tmp;
635 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
636 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
638 if (net != lp->lpni_net)
641 if (!lnet_isrouter(lp))
644 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
646 lnet_net_unlock(LNET_LOCK_EX);
647 lnet_del_route(LNET_NET_ANY, gw_nid);
648 lnet_net_lock(LNET_LOCK_EX);
654 lnet_peer_tables_cleanup(struct lnet_net *net)
657 struct lnet_peer_table *ptable;
659 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
660 /* If just deleting the peers for a NI, get rid of any routes these
661 * peers are gateways for. */
662 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
663 lnet_net_lock(LNET_LOCK_EX);
664 lnet_peer_table_del_rtrs_locked(net, ptable);
665 lnet_net_unlock(LNET_LOCK_EX);
668 /* Start the cleanup process */
669 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
670 lnet_net_lock(LNET_LOCK_EX);
671 lnet_peer_table_cleanup_locked(net, ptable);
672 lnet_net_unlock(LNET_LOCK_EX);
675 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
676 lnet_peer_ni_finalize_wait(ptable);
679 static struct lnet_peer_ni *
680 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
682 struct list_head *peers;
683 struct lnet_peer_ni *lp;
685 if (the_lnet.ln_state != LNET_STATE_RUNNING)
688 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
689 list_for_each_entry(lp, peers, lpni_hashlist) {
690 if (lp->lpni_nid == nid) {
691 lnet_peer_ni_addref_locked(lp);
699 struct lnet_peer_ni *
700 lnet_find_peer_ni_locked(lnet_nid_t nid)
702 struct lnet_peer_ni *lpni;
703 struct lnet_peer_table *ptable;
706 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
708 ptable = the_lnet.ln_peer_tables[cpt];
709 lpni = lnet_get_peer_ni_locked(ptable, nid);
714 struct lnet_peer_ni *
715 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
717 struct lnet_peer_net *lpn;
718 struct lnet_peer_ni *lpni;
720 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
724 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
725 if (lpni->lpni_nid == nid)
733 lnet_find_peer(lnet_nid_t nid)
735 struct lnet_peer_ni *lpni;
736 struct lnet_peer *lp = NULL;
739 cpt = lnet_net_lock_current();
740 lpni = lnet_find_peer_ni_locked(nid);
742 lp = lpni->lpni_peer_net->lpn_peer;
743 lnet_peer_addref_locked(lp);
744 lnet_peer_ni_decref_locked(lpni);
746 lnet_net_unlock(cpt);
751 struct lnet_peer_net *
752 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
754 struct lnet_peer_net *net;
757 /* no net id provided return the first net */
758 net = list_first_entry_or_null(&lp->lp_peer_nets,
759 struct lnet_peer_net,
765 /* find the net after the one provided */
766 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
767 if (net->lpn_net_id == prev_lpn_id) {
769 * if we reached the end of the list loop to the
772 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
773 return list_first_entry_or_null(&lp->lp_peer_nets,
774 struct lnet_peer_net,
777 return list_next_entry(net, lpn_peer_nets);
784 struct lnet_peer_ni *
785 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
786 struct lnet_peer_net *peer_net,
787 struct lnet_peer_ni *prev)
789 struct lnet_peer_ni *lpni;
790 struct lnet_peer_net *net = peer_net;
794 if (list_empty(&peer->lp_peer_nets))
797 net = list_entry(peer->lp_peer_nets.next,
798 struct lnet_peer_net,
801 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
807 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
809 * if you reached the end of the peer ni list and the peer
810 * net is specified then there are no more peer nis in that
817 * we reached the end of this net ni list. move to the
820 if (prev->lpni_peer_net->lpn_peer_nets.next ==
822 /* no more nets and no more NIs. */
825 /* get the next net */
826 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
827 struct lnet_peer_net,
829 /* get the ni on it */
830 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
836 /* there are more nis left */
837 lpni = list_entry(prev->lpni_peer_nis.next,
838 struct lnet_peer_ni, lpni_peer_nis);
843 /* Call with the ln_api_mutex held */
844 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
846 struct lnet_process_id id;
847 struct lnet_peer_table *ptable;
848 struct lnet_peer *lp;
857 if (the_lnet.ln_state != LNET_STATE_RUNNING)
860 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
863 * Count the number of peers, and return E2BIG if the buffer
864 * is too small. We'll also return the desired size.
867 for (cpt = 0; cpt < lncpt; cpt++) {
868 ptable = the_lnet.ln_peer_tables[cpt];
869 count += ptable->pt_peers;
871 size = count * sizeof(*ids);
876 * Walk the peer lists and copy out the primary nids.
877 * This is safe because the peer lists are only modified
878 * while the ln_api_mutex is held. So we don't need to
879 * hold the lnet_net_lock as well, and can therefore
880 * directly call copy_to_user().
883 memset(&id, 0, sizeof(id));
884 id.pid = LNET_PID_LUSTRE;
886 for (cpt = 0; cpt < lncpt; cpt++) {
887 ptable = the_lnet.ln_peer_tables[cpt];
888 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
891 id.nid = lp->lp_primary_nid;
892 if (copy_to_user(&ids[i], &id, sizeof(id)))
905 * Start pushes to peers that need to be updated for a configuration
906 * change on this node.
909 lnet_push_update_to_peers(int force)
911 struct lnet_peer_table *ptable;
912 struct lnet_peer *lp;
916 lnet_net_lock(LNET_LOCK_EX);
917 if (lnet_peer_discovery_disabled)
919 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
920 for (cpt = 0; cpt < lncpt; cpt++) {
921 ptable = the_lnet.ln_peer_tables[cpt];
922 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
924 spin_lock(&lp->lp_lock);
925 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
926 lp->lp_state |= LNET_PEER_FORCE_PUSH;
927 spin_unlock(&lp->lp_lock);
929 if (lnet_peer_needs_push(lp))
930 lnet_peer_queue_for_discovery(lp);
933 lnet_net_unlock(LNET_LOCK_EX);
934 wake_up(&the_lnet.ln_dc_waitq);
937 /* find the NID in the preferred gateways for the remote peer
939 * false: list is not empty and NID is not preferred
940 * false: list is empty
941 * true: nid is found in the list
944 lnet_peer_is_pref_rtr_locked(struct lnet_peer_ni *lpni,
947 struct lnet_nid_list *ne;
949 CDEBUG(D_NET, "%s: rtr pref emtpy: %d\n",
950 libcfs_nid2str(lpni->lpni_nid),
951 list_empty(&lpni->lpni_rtr_pref_nids));
953 if (list_empty(&lpni->lpni_rtr_pref_nids))
956 /* iterate through all the preferred NIDs and see if any of them
957 * matches the provided gw_nid
959 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
960 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
961 libcfs_nid2str(ne->nl_nid),
962 libcfs_nid2str(gw_nid));
963 if (ne->nl_nid == gw_nid)
971 lnet_peer_clr_pref_rtrs(struct lnet_peer_ni *lpni)
973 struct list_head zombies;
974 struct lnet_nid_list *ne;
975 struct lnet_nid_list *tmp;
976 int cpt = lpni->lpni_cpt;
978 INIT_LIST_HEAD(&zombies);
981 list_splice_init(&lpni->lpni_rtr_pref_nids, &zombies);
982 lnet_net_unlock(cpt);
984 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
985 list_del(&ne->nl_list);
986 LIBCFS_FREE(ne, sizeof(*ne));
991 lnet_peer_add_pref_rtr(struct lnet_peer_ni *lpni,
994 int cpt = lpni->lpni_cpt;
995 struct lnet_nid_list *ne = NULL;
997 /* This function is called with api_mutex held. When the api_mutex
998 * is held the list can not be modified, as it is only modified as
999 * a result of applying a UDSP and that happens under api_mutex
1002 __must_hold(&the_lnet.ln_api_mutex);
1004 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
1005 if (ne->nl_nid == gw_nid)
1009 LIBCFS_CPT_ALLOC(ne, lnet_cpt_table(), cpt, sizeof(*ne));
1013 ne->nl_nid = gw_nid;
1015 /* Lock the cpt to protect against addition and checks in the
1016 * selection algorithm
1019 list_add(&ne->nl_list, &lpni->lpni_rtr_pref_nids);
1020 lnet_net_unlock(cpt);
1026 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
1027 * this is a preferred point-to-point path. Call with lnet_net_lock in
1031 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1033 struct lnet_nid_list *ne;
1035 if (lpni->lpni_pref_nnids == 0)
1037 if (lpni->lpni_pref_nnids == 1)
1038 return lpni->lpni_pref.nid == nid;
1039 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1040 if (ne->nl_nid == nid)
1047 * Set a single ni as preferred, provided no preferred ni is already
1048 * defined. Only to be used for non-multi-rail peer_ni.
1051 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1055 spin_lock(&lpni->lpni_lock);
1056 if (nid == LNET_NID_ANY) {
1058 } else if (lpni->lpni_pref_nnids > 0) {
1060 } else if (lpni->lpni_pref_nnids == 0) {
1061 lpni->lpni_pref.nid = nid;
1062 lpni->lpni_pref_nnids = 1;
1063 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
1065 spin_unlock(&lpni->lpni_lock);
1067 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1068 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
1073 * Clear the preferred NID from a non-multi-rail peer_ni, provided
1074 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
1077 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
1081 spin_lock(&lpni->lpni_lock);
1082 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
1083 lpni->lpni_pref_nnids = 0;
1084 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1085 } else if (lpni->lpni_pref_nnids == 0) {
1090 spin_unlock(&lpni->lpni_lock);
1092 CDEBUG(D_NET, "peer %s: %d\n",
1093 libcfs_nid2str(lpni->lpni_nid), rc);
1098 lnet_peer_ni_set_selection_priority(struct lnet_peer_ni *lpni, __u32 priority)
1100 lpni->lpni_sel_priority = priority;
1104 * Clear the preferred NIDs from a non-multi-rail peer.
1107 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
1109 struct lnet_peer_ni *lpni = NULL;
1111 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
1112 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
1116 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1118 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1119 struct lnet_nid_list *ne1 = NULL;
1120 struct lnet_nid_list *ne2 = NULL;
1121 lnet_nid_t tmp_nid = LNET_NID_ANY;
1124 if (nid == LNET_NID_ANY) {
1129 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1134 /* A non-MR node may have only one preferred NI per peer_ni */
1135 if (lpni->lpni_pref_nnids > 0 &&
1136 !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1141 /* add the new preferred nid to the list of preferred nids */
1142 if (lpni->lpni_pref_nnids != 0) {
1143 size_t alloc_size = sizeof(*ne1);
1145 if (lpni->lpni_pref_nnids == 1) {
1146 tmp_nid = lpni->lpni_pref.nid;
1147 INIT_LIST_HEAD(&lpni->lpni_pref.nids);
1150 list_for_each_entry(ne1, &lpni->lpni_pref.nids, nl_list) {
1151 if (ne1->nl_nid == nid) {
1157 LIBCFS_CPT_ALLOC(ne1, lnet_cpt_table(), lpni->lpni_cpt,
1164 /* move the originally stored nid to the list */
1165 if (lpni->lpni_pref_nnids == 1) {
1166 LIBCFS_CPT_ALLOC(ne2, lnet_cpt_table(),
1167 lpni->lpni_cpt, alloc_size);
1172 INIT_LIST_HEAD(&ne2->nl_list);
1173 ne2->nl_nid = tmp_nid;
1178 lnet_net_lock(LNET_LOCK_EX);
1179 spin_lock(&lpni->lpni_lock);
1180 if (lpni->lpni_pref_nnids == 0) {
1181 lpni->lpni_pref.nid = nid;
1184 list_add_tail(&ne2->nl_list, &lpni->lpni_pref.nids);
1185 list_add_tail(&ne1->nl_list, &lpni->lpni_pref.nids);
1187 lpni->lpni_pref_nnids++;
1188 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1189 spin_unlock(&lpni->lpni_lock);
1190 lnet_net_unlock(LNET_LOCK_EX);
1193 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1194 spin_lock(&lpni->lpni_lock);
1195 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1196 spin_unlock(&lpni->lpni_lock);
1198 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1199 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1204 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1206 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1207 struct lnet_nid_list *ne = NULL;
1210 if (lpni->lpni_pref_nnids == 0) {
1215 if (lpni->lpni_pref_nnids == 1) {
1216 if (lpni->lpni_pref.nid != nid) {
1221 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1222 if (ne->nl_nid == nid)
1223 goto remove_nid_entry;
1231 lnet_net_lock(LNET_LOCK_EX);
1232 spin_lock(&lpni->lpni_lock);
1233 if (lpni->lpni_pref_nnids == 1)
1234 lpni->lpni_pref.nid = LNET_NID_ANY;
1236 list_del_init(&ne->nl_list);
1237 if (lpni->lpni_pref_nnids == 2) {
1238 struct lnet_nid_list *ne, *tmp;
1240 list_for_each_entry_safe(ne, tmp,
1241 &lpni->lpni_pref.nids,
1243 lpni->lpni_pref.nid = ne->nl_nid;
1244 list_del_init(&ne->nl_list);
1245 LIBCFS_FREE(ne, sizeof(*ne));
1249 lpni->lpni_pref_nnids--;
1250 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1251 spin_unlock(&lpni->lpni_lock);
1252 lnet_net_unlock(LNET_LOCK_EX);
1255 LIBCFS_FREE(ne, sizeof(*ne));
1257 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1258 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1263 lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni)
1265 struct list_head zombies;
1266 struct lnet_nid_list *ne;
1267 struct lnet_nid_list *tmp;
1269 INIT_LIST_HEAD(&zombies);
1271 lnet_net_lock(LNET_LOCK_EX);
1272 if (lpni->lpni_pref_nnids == 1)
1273 lpni->lpni_pref.nid = LNET_NID_ANY;
1274 else if (lpni->lpni_pref_nnids > 1)
1275 list_splice_init(&lpni->lpni_pref.nids, &zombies);
1276 lpni->lpni_pref_nnids = 0;
1277 lnet_net_unlock(LNET_LOCK_EX);
1279 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1280 list_del_init(&ne->nl_list);
1281 LIBCFS_FREE(ne, sizeof(*ne));
1286 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1288 struct lnet_peer_ni *lpni;
1289 lnet_nid_t primary_nid = nid;
1291 lpni = lnet_find_peer_ni_locked(nid);
1293 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1294 lnet_peer_ni_decref_locked(lpni);
1301 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1302 __must_hold(&lp->lp_lock)
1304 if (lnet_peer_discovery_disabled)
1307 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1308 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1319 lnet_is_discovery_disabled(struct lnet_peer *lp)
1323 spin_lock(&lp->lp_lock);
1324 rc = lnet_is_discovery_disabled_locked(lp);
1325 spin_unlock(&lp->lp_lock);
1331 LNetPrimaryNID(lnet_nid_t nid)
1333 struct lnet_peer *lp;
1334 struct lnet_peer_ni *lpni;
1335 lnet_nid_t primary_nid = nid;
1339 if (nid == LNET_NID_LO_0)
1340 return LNET_NID_LO_0;
1342 cpt = lnet_net_lock_current();
1343 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1348 lp = lpni->lpni_peer_net->lpn_peer;
1350 while (!lnet_peer_is_uptodate(lp)) {
1351 spin_lock(&lp->lp_lock);
1352 /* force a full discovery cycle */
1353 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1354 spin_unlock(&lp->lp_lock);
1356 rc = lnet_discover_peer_locked(lpni, cpt, true);
1359 /* The lpni (or lp) for this NID may have changed and our ref is
1360 * the only thing keeping the old one around. Release the ref
1361 * and lookup the lpni again
1363 lnet_peer_ni_decref_locked(lpni);
1364 lpni = lnet_find_peer_ni_locked(nid);
1369 lp = lpni->lpni_peer_net->lpn_peer;
1371 /* Only try once if discovery is disabled */
1372 if (lnet_is_discovery_disabled(lp))
1375 primary_nid = lp->lp_primary_nid;
1377 lnet_peer_ni_decref_locked(lpni);
1379 lnet_net_unlock(cpt);
1381 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1382 libcfs_nid2str(primary_nid), rc);
1385 EXPORT_SYMBOL(LNetPrimaryNID);
1387 struct lnet_peer_net *
1388 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1390 struct lnet_peer_net *peer_net;
1391 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1392 if (peer_net->lpn_net_id == net_id)
1399 * Attach a peer_ni to a peer_net and peer. This function assumes
1400 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1401 * may be attached to a different peer, in which case it will be
1402 * properly detached first. The whole operation is done atomically.
1404 * This function consumes the reference on lpni and Always returns 0.
1405 * This is the last function called from functions that do return an
1406 * int, so returning 0 here allows the compiler to do a tail call.
1409 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1410 struct lnet_peer_net *lpn,
1411 struct lnet_peer_ni *lpni,
1414 struct lnet_peer_table *ptable;
1415 bool new_lpn = false;
1418 /* Install the new peer_ni */
1419 lnet_net_lock(LNET_LOCK_EX);
1420 /* Add peer_ni to global peer table hash, if necessary. */
1421 if (list_empty(&lpni->lpni_hashlist)) {
1422 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1424 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1425 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1426 ptable->pt_version++;
1427 lnet_peer_ni_addref_locked(lpni);
1430 /* Detach the peer_ni from an existing peer, if necessary. */
1431 if (lpni->lpni_peer_net) {
1432 LASSERT(lpni->lpni_peer_net != lpn);
1433 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1434 lnet_peer_detach_peer_ni_locked(lpni);
1435 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1436 lpni->lpni_peer_net = NULL;
1439 /* Add peer_ni to peer_net */
1440 lpni->lpni_peer_net = lpn;
1441 if (lp->lp_primary_nid == lpni->lpni_nid)
1442 list_add(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1444 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1445 lnet_update_peer_net_healthv(lpni);
1446 lnet_peer_net_addref_locked(lpn);
1448 /* Add peer_net to peer */
1449 if (!lpn->lpn_peer) {
1452 if (lp->lp_primary_nid == lpni->lpni_nid)
1453 list_add(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1455 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1456 lnet_peer_addref_locked(lp);
1459 /* Add peer to global peer list, if necessary */
1460 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1461 if (list_empty(&lp->lp_peer_list)) {
1462 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1467 /* Update peer state */
1468 spin_lock(&lp->lp_lock);
1469 if (flags & LNET_PEER_CONFIGURED) {
1470 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1471 lp->lp_state |= LNET_PEER_CONFIGURED;
1473 if (flags & LNET_PEER_MULTI_RAIL) {
1474 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1475 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1476 lnet_peer_clr_non_mr_pref_nids(lp);
1479 spin_unlock(&lp->lp_lock);
1485 rc = lnet_udsp_apply_policies_on_lpn(lpn);
1487 CERROR("Failed to apply UDSPs on lpn %s\n",
1488 libcfs_net2str(lpn->lpn_net_id));
1490 rc = lnet_udsp_apply_policies_on_lpni(lpni);
1492 CERROR("Failed to apply UDSPs on lpni %s\n",
1493 libcfs_nid2str(lpni->lpni_nid));
1495 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1496 libcfs_nid2str(lp->lp_primary_nid),
1497 libcfs_nid2str(lpni->lpni_nid), flags);
1498 lnet_peer_ni_decref_locked(lpni);
1499 lnet_net_unlock(LNET_LOCK_EX);
1505 * Create a new peer, with nid as its primary nid.
1507 * Call with the lnet_api_mutex held.
1510 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1512 struct lnet_peer *lp;
1513 struct lnet_peer_net *lpn;
1514 struct lnet_peer_ni *lpni;
1517 LASSERT(nid != LNET_NID_ANY);
1520 * No need for the lnet_net_lock here, because the
1521 * lnet_api_mutex is held.
1523 lpni = lnet_find_peer_ni_locked(nid);
1525 /* A peer with this NID already exists. */
1526 lp = lpni->lpni_peer_net->lpn_peer;
1527 lnet_peer_ni_decref_locked(lpni);
1529 * This is an error if the peer was configured and the
1530 * primary NID differs or an attempt is made to change
1531 * the Multi-Rail flag. Otherwise the assumption is
1532 * that an existing peer is being modified.
1534 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1535 if (lp->lp_primary_nid != nid)
1537 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1541 /* Delete and recreate as a configured peer. */
1545 /* Create peer, peer_net, and peer_ni. */
1547 lp = lnet_peer_alloc(nid);
1550 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1553 lpni = lnet_peer_ni_alloc(nid);
1557 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1560 LIBCFS_FREE(lpn, sizeof(*lpn));
1562 LIBCFS_FREE(lp, sizeof(*lp));
1564 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1565 libcfs_nid2str(nid), flags, rc);
1570 * Add a NID to a peer. Call with ln_api_mutex held.
1573 * -EPERM: Non-DLC addition to a DLC-configured peer.
1574 * -EEXIST: The NID was configured by DLC for a different peer.
1575 * -ENOMEM: Out of memory.
1576 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1577 * non-multi-rail peer.
1580 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1582 struct lnet_peer_net *lpn;
1583 struct lnet_peer_ni *lpni;
1587 LASSERT(nid != LNET_NID_ANY);
1589 /* A configured peer can only be updated through configuration. */
1590 if (!(flags & LNET_PEER_CONFIGURED)) {
1591 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1598 * The MULTI_RAIL flag can be set but not cleared, because
1599 * that would leave the peer struct in an invalid state.
1601 if (flags & LNET_PEER_MULTI_RAIL) {
1602 spin_lock(&lp->lp_lock);
1603 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1604 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1605 lnet_peer_clr_non_mr_pref_nids(lp);
1607 spin_unlock(&lp->lp_lock);
1608 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1613 lpni = lnet_find_peer_ni_locked(nid);
1616 * A peer_ni already exists. This is only a problem if
1617 * it is not connected to this peer and was configured
1620 if (lpni->lpni_peer_net->lpn_peer == lp)
1622 if (lnet_peer_ni_is_configured(lpni)) {
1626 /* If this is the primary NID, destroy the peer. */
1627 if (lnet_peer_ni_is_primary(lpni)) {
1628 struct lnet_peer *rtr_lp =
1629 lpni->lpni_peer_net->lpn_peer;
1630 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1632 * if we're trying to delete a router it means
1633 * we're moving this peer NI to a new peer so must
1634 * transfer router properties to the new peer
1636 if (rtr_refcount > 0) {
1637 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1638 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1640 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1641 lnet_peer_ni_decref_locked(lpni);
1642 lpni = lnet_peer_ni_alloc(nid);
1649 lpni = lnet_peer_ni_alloc(nid);
1657 * Get the peer_net. Check that we're not adding a second
1658 * peer_ni on a peer_net of a non-multi-rail peer.
1660 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1662 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1667 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1672 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1675 lnet_peer_ni_decref_locked(lpni);
1677 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1678 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1684 * Update the primary NID of a peer, if possible.
1686 * Call with the lnet_api_mutex held.
1689 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1691 lnet_nid_t old = lp->lp_primary_nid;
1694 if (lp->lp_primary_nid == nid)
1697 lp->lp_primary_nid = nid;
1699 rc = lnet_peer_add_nid(lp, nid, flags);
1701 lp->lp_primary_nid = old;
1705 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1706 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1711 * lpni creation initiated due to traffic either sending or receiving.
1714 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1716 struct lnet_peer *lp;
1717 struct lnet_peer_net *lpn;
1718 struct lnet_peer_ni *lpni;
1722 if (nid == LNET_NID_ANY) {
1727 /* lnet_net_lock is not needed here because ln_api_lock is held */
1728 lpni = lnet_find_peer_ni_locked(nid);
1731 * We must have raced with another thread. Since we
1732 * know next to nothing about a peer_ni created by
1733 * traffic, we just assume everything is ok and
1736 lnet_peer_ni_decref_locked(lpni);
1740 /* Create peer, peer_net, and peer_ni. */
1742 lp = lnet_peer_alloc(nid);
1745 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1748 lpni = lnet_peer_ni_alloc(nid);
1751 if (pref != LNET_NID_ANY)
1752 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1754 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1757 LIBCFS_FREE(lpn, sizeof(*lpn));
1759 LIBCFS_FREE(lp, sizeof(*lp));
1761 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1766 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1768 * This API handles the following combinations:
1769 * Create a peer with its primary NI if only the prim_nid is provided
1770 * Add a NID to a peer identified by the prim_nid. The peer identified
1771 * by the prim_nid must already exist.
1772 * The peer being created may be non-MR.
1774 * The caller must hold ln_api_mutex. This prevents the peer from
1775 * being created/modified/deleted by a different thread.
1778 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1780 struct lnet_peer *lp = NULL;
1781 struct lnet_peer_ni *lpni;
1784 /* The prim_nid must always be specified */
1785 if (prim_nid == LNET_NID_ANY)
1788 flags = LNET_PEER_CONFIGURED;
1790 flags |= LNET_PEER_MULTI_RAIL;
1793 * If nid isn't specified, we must create a new peer with
1794 * prim_nid as its primary nid.
1796 if (nid == LNET_NID_ANY)
1797 return lnet_peer_add(prim_nid, flags);
1799 /* Look up the prim_nid, which must exist. */
1800 lpni = lnet_find_peer_ni_locked(prim_nid);
1803 lnet_peer_ni_decref_locked(lpni);
1804 lp = lpni->lpni_peer_net->lpn_peer;
1806 /* Peer must have been configured. */
1807 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1808 CDEBUG(D_NET, "peer %s was not configured\n",
1809 libcfs_nid2str(prim_nid));
1813 /* Primary NID must match */
1814 if (lp->lp_primary_nid != prim_nid) {
1815 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1816 libcfs_nid2str(prim_nid),
1817 libcfs_nid2str(lp->lp_primary_nid));
1821 /* Multi-Rail flag must match. */
1822 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1823 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1824 libcfs_nid2str(prim_nid));
1828 return lnet_peer_add_nid(lp, nid, flags);
1832 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1834 * This API handles the following combinations:
1835 * Delete a NI from a peer if both prim_nid and nid are provided.
1836 * Delete a peer if only prim_nid is provided.
1837 * Delete a peer if its primary nid is provided.
1839 * The caller must hold ln_api_mutex. This prevents the peer from
1840 * being modified/deleted by a different thread.
1843 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1845 struct lnet_peer *lp;
1846 struct lnet_peer_ni *lpni;
1849 if (prim_nid == LNET_NID_ANY)
1852 lpni = lnet_find_peer_ni_locked(prim_nid);
1855 lnet_peer_ni_decref_locked(lpni);
1856 lp = lpni->lpni_peer_net->lpn_peer;
1858 if (prim_nid != lp->lp_primary_nid) {
1859 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1860 libcfs_nid2str(prim_nid),
1861 libcfs_nid2str(lp->lp_primary_nid));
1865 lnet_net_lock(LNET_LOCK_EX);
1866 if (lp->lp_rtr_refcount > 0) {
1867 lnet_net_unlock(LNET_LOCK_EX);
1868 CERROR("%s is a router. Can not be deleted\n",
1869 libcfs_nid2str(prim_nid));
1872 lnet_net_unlock(LNET_LOCK_EX);
1874 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1875 return lnet_peer_del(lp);
1877 flags = LNET_PEER_CONFIGURED;
1878 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1879 flags |= LNET_PEER_MULTI_RAIL;
1881 return lnet_peer_del_nid(lp, nid, flags);
1885 lnet_destroy_peer_ni_locked(struct kref *ref)
1887 struct lnet_peer_ni *lpni = container_of(ref, struct lnet_peer_ni,
1889 struct lnet_peer_table *ptable;
1890 struct lnet_peer_net *lpn;
1892 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1894 LASSERT(kref_read(&lpni->lpni_kref) == 0);
1895 LASSERT(list_empty(&lpni->lpni_txq));
1896 LASSERT(lpni->lpni_txqnob == 0);
1897 LASSERT(list_empty(&lpni->lpni_peer_nis));
1898 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1900 lpn = lpni->lpni_peer_net;
1901 lpni->lpni_peer_net = NULL;
1902 lpni->lpni_net = NULL;
1904 if (!list_empty(&lpni->lpni_hashlist)) {
1905 /* remove the peer ni from the zombie list */
1906 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1907 spin_lock(&ptable->pt_zombie_lock);
1908 list_del_init(&lpni->lpni_hashlist);
1909 ptable->pt_zombies--;
1910 spin_unlock(&ptable->pt_zombie_lock);
1913 if (lpni->lpni_pref_nnids > 1) {
1914 struct lnet_nid_list *ne, *tmp;
1916 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
1918 list_del_init(&ne->nl_list);
1919 LIBCFS_FREE(ne, sizeof(*ne));
1922 LIBCFS_FREE(lpni, sizeof(*lpni));
1925 lnet_peer_net_decref_locked(lpn);
1928 struct lnet_peer_ni *
1929 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1931 struct lnet_peer_ni *lpni = NULL;
1934 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1935 return ERR_PTR(-ESHUTDOWN);
1938 * find if a peer_ni already exists.
1939 * If so then just return that.
1941 lpni = lnet_find_peer_ni_locked(nid);
1945 lnet_net_unlock(cpt);
1947 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1950 goto out_net_relock;
1953 lpni = lnet_find_peer_ni_locked(nid);
1963 * Get a peer_ni for the given nid, create it if necessary. Takes a
1964 * hold on the peer_ni.
1966 struct lnet_peer_ni *
1967 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1969 struct lnet_peer_ni *lpni = NULL;
1972 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1973 return ERR_PTR(-ESHUTDOWN);
1976 * find if a peer_ni already exists.
1977 * If so then just return that.
1979 lpni = lnet_find_peer_ni_locked(nid);
1985 * use the lnet_api_mutex to serialize the creation of the peer_ni
1986 * and the creation/deletion of the local ni/net. When a local ni is
1987 * created, if there exists a set of peer_nis on that network,
1988 * they need to be traversed and updated. When a local NI is
1989 * deleted, which could result in a network being deleted, then
1990 * all peer nis on that network need to be removed as well.
1992 * Creation through traffic should also be serialized with
1993 * creation through DLC.
1995 lnet_net_unlock(cpt);
1996 mutex_lock(&the_lnet.ln_api_mutex);
1998 * Shutdown is only set under the ln_api_lock, so a single
1999 * check here is sufficent.
2001 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2002 lpni = ERR_PTR(-ESHUTDOWN);
2003 goto out_mutex_unlock;
2006 rc = lnet_peer_ni_traffic_add(nid, pref);
2009 goto out_mutex_unlock;
2012 lpni = lnet_find_peer_ni_locked(nid);
2016 mutex_unlock(&the_lnet.ln_api_mutex);
2019 /* Lock has been dropped, check again for shutdown. */
2020 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2022 lnet_peer_ni_decref_locked(lpni);
2023 lpni = ERR_PTR(-ESHUTDOWN);
2030 lnet_peer_gw_discovery(struct lnet_peer *lp)
2034 spin_lock(&lp->lp_lock);
2035 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
2037 spin_unlock(&lp->lp_lock);
2043 lnet_peer_is_uptodate(struct lnet_peer *lp)
2047 spin_lock(&lp->lp_lock);
2048 rc = lnet_peer_is_uptodate_locked(lp);
2049 spin_unlock(&lp->lp_lock);
2054 * Is a peer uptodate from the point of view of discovery?
2056 * If it is currently being processed, obviously not.
2057 * A forced Ping or Push is also handled by the discovery thread.
2059 * Otherwise look at whether the peer needs rediscovering.
2062 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
2063 __must_hold(&lp->lp_lock)
2067 if (lp->lp_state & (LNET_PEER_DISCOVERING |
2068 LNET_PEER_FORCE_PING |
2069 LNET_PEER_FORCE_PUSH)) {
2071 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
2073 } else if (lnet_peer_needs_push(lp)) {
2075 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
2076 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
2087 /* Add the message to the peer's lp_dc_pendq and queue the peer for discovery */
2089 lnet_peer_queue_message(struct lnet_peer *lp, struct lnet_msg *msg)
2091 /* The discovery thread holds net_lock/EX and lp_lock when it splices
2092 * the lp_dc_pendq onto a local list for resending. Thus, we do the same
2093 * when adding to the list and queuing the peer to ensure that we do not
2094 * strand any messages on the lp_dc_pendq. This scheme ensures the
2095 * message will be resent even if the peer is already being discovered.
2096 * Therefore we needn't check the return value of
2097 * lnet_peer_queue_for_discovery(lp).
2099 lnet_net_lock(LNET_LOCK_EX);
2100 spin_lock(&lp->lp_lock);
2101 list_add_tail(&msg->msg_list, &lp->lp_dc_pendq);
2102 spin_unlock(&lp->lp_lock);
2103 lnet_peer_queue_for_discovery(lp);
2104 lnet_net_unlock(LNET_LOCK_EX);
2108 * Queue a peer for the attention of the discovery thread. Call with
2109 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
2110 * -EALREADY if the peer was already queued.
2112 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
2116 spin_lock(&lp->lp_lock);
2117 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2118 lp->lp_state |= LNET_PEER_DISCOVERING;
2119 spin_unlock(&lp->lp_lock);
2120 if (list_empty(&lp->lp_dc_list)) {
2121 lnet_peer_addref_locked(lp);
2122 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2123 wake_up(&the_lnet.ln_dc_waitq);
2129 CDEBUG(D_NET, "Queue peer %s: %d\n",
2130 libcfs_nid2str(lp->lp_primary_nid), rc);
2136 * Discovery of a peer is complete. Wake all waiters on the peer.
2137 * Call with lnet_net_lock/EX held.
2139 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
2141 struct lnet_msg *msg, *tmp;
2143 LIST_HEAD(pending_msgs);
2145 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
2146 libcfs_nid2str(lp->lp_primary_nid));
2148 list_del_init(&lp->lp_dc_list);
2149 spin_lock(&lp->lp_lock);
2150 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
2151 spin_unlock(&lp->lp_lock);
2152 wake_up(&lp->lp_dc_waitq);
2154 if (lp->lp_rtr_refcount > 0)
2155 lnet_router_discovery_complete(lp);
2157 lnet_net_unlock(LNET_LOCK_EX);
2159 /* iterate through all pending messages and send them again */
2160 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
2161 list_del_init(&msg->msg_list);
2162 if (lp->lp_dc_error) {
2163 lnet_finalize(msg, lp->lp_dc_error);
2167 CDEBUG(D_NET, "sending pending message %s to target %s\n",
2168 lnet_msgtyp2str(msg->msg_type),
2169 libcfs_id2str(msg->msg_target));
2170 rc = lnet_send(msg->msg_src_nid_param, msg,
2171 msg->msg_rtr_nid_param);
2173 CNETERR("Error sending %s to %s: %d\n",
2174 lnet_msgtyp2str(msg->msg_type),
2175 libcfs_id2str(msg->msg_target), rc);
2176 lnet_finalize(msg, rc);
2179 lnet_net_lock(LNET_LOCK_EX);
2180 lnet_peer_decref_locked(lp);
2184 * Handle inbound push.
2185 * Like any event handler, called with lnet_res_lock/CPT held.
2187 void lnet_peer_push_event(struct lnet_event *ev)
2189 struct lnet_ping_buffer *pbuf;
2190 struct lnet_peer *lp;
2192 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
2194 /* lnet_find_peer() adds a refcount */
2195 lp = lnet_find_peer(ev->source.nid);
2197 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
2198 libcfs_nid2str(ev->initiator.nid),
2199 libcfs_nid2str(ev->source.nid));
2200 pbuf->pb_needs_post = true;
2204 /* Ensure peer state remains consistent while we modify it. */
2205 spin_lock(&lp->lp_lock);
2208 * If some kind of error happened the contents of the message
2209 * cannot be used. Clear the NIDS_UPTODATE and set the
2210 * FORCE_PING flag to trigger a ping.
2213 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2214 lp->lp_state |= LNET_PEER_FORCE_PING;
2215 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2217 libcfs_nid2str(lp->lp_primary_nid),
2218 libcfs_nid2str(ev->source.nid));
2223 * A push with invalid or corrupted info. Clear the UPTODATE
2224 * flag to trigger a ping.
2226 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2227 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2228 lp->lp_state |= LNET_PEER_FORCE_PING;
2229 CDEBUG(D_NET, "Corrupted Push from %s\n",
2230 libcfs_nid2str(lp->lp_primary_nid));
2235 * Make sure we'll allocate the correct size ping buffer when
2238 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2239 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2242 * A non-Multi-Rail peer is not supposed to be capable of
2245 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2246 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2247 libcfs_nid2str(lp->lp_primary_nid));
2252 * The peer may have discovery disabled at its end. Set
2253 * NO_DISCOVERY as appropriate.
2255 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2256 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2257 libcfs_nid2str(lp->lp_primary_nid));
2259 * Mark the peer for deletion if we already know about it
2260 * and it's going from discovery set to no discovery set
2262 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2263 LNET_PEER_DISCOVERING)) &&
2264 lp->lp_state & LNET_PEER_DISCOVERED) {
2265 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2266 libcfs_nid2str(lp->lp_primary_nid),
2268 lp->lp_state |= LNET_PEER_MARK_DELETION;
2270 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2271 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2272 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2273 libcfs_nid2str(lp->lp_primary_nid));
2274 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2278 * Update the MULTI_RAIL flag based on the push. If the peer
2279 * was configured with DLC then the setting should match what
2281 * NB: We verified above that the MR feature bit is set in pi_features
2283 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2284 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2285 libcfs_nid2str(lp->lp_primary_nid), lp);
2286 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2287 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2288 libcfs_nid2str(lp->lp_primary_nid));
2289 } else if (lnet_peer_discovery_disabled) {
2290 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2291 libcfs_nid2str(lp->lp_primary_nid), lp);
2292 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2293 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2294 libcfs_nid2str(lp->lp_primary_nid), lp);
2296 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2297 libcfs_nid2str(lp->lp_primary_nid), lp);
2298 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2299 lnet_peer_clr_non_mr_pref_nids(lp);
2303 * Check for truncation of the Put message. Clear the
2304 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2305 * and tell discovery to allocate a bigger buffer.
2307 if (ev->mlength < ev->rlength) {
2308 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2309 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2310 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2311 lp->lp_state |= LNET_PEER_FORCE_PING;
2312 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2313 libcfs_nid2str(lp->lp_primary_nid),
2314 pbuf->pb_info.pi_nnis);
2318 /* always assume new data */
2319 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2320 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2323 * If there is data present that hasn't been processed yet,
2324 * we'll replace it if the Put contained newer data and it
2325 * fits. We're racing with a Ping or earlier Push in this
2328 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2329 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2330 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2331 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2332 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2333 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2334 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2335 libcfs_nid2str(lp->lp_primary_nid),
2336 LNET_PING_BUFFER_SEQNO(pbuf),
2337 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2343 * Allocate a buffer to copy the data. On a failure we drop
2344 * the Push and set FORCE_PING to force the discovery
2345 * thread to fix the problem by pinging the peer.
2347 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2349 lp->lp_state |= LNET_PEER_FORCE_PING;
2350 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2351 libcfs_nid2str(lp->lp_primary_nid),
2352 LNET_PING_BUFFER_SEQNO(pbuf));
2357 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2358 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2359 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2360 CDEBUG(D_NET, "Received Push %s %u\n",
2361 libcfs_nid2str(lp->lp_primary_nid),
2362 LNET_PING_BUFFER_SEQNO(pbuf));
2365 /* We've processed this buffer. It can be reposted */
2366 pbuf->pb_needs_post = true;
2369 * Queue the peer for discovery if not done, force it on the request
2370 * queue and wake the discovery thread if the peer was already queued,
2371 * because its status changed.
2373 spin_unlock(&lp->lp_lock);
2374 lnet_net_lock(LNET_LOCK_EX);
2375 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2376 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2377 wake_up(&the_lnet.ln_dc_waitq);
2379 /* Drop refcount from lookup */
2380 lnet_peer_decref_locked(lp);
2381 lnet_net_unlock(LNET_LOCK_EX);
2385 * Clear the discovery error state, unless we're already discovering
2386 * this peer, in which case the error is current.
2388 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2390 spin_lock(&lp->lp_lock);
2391 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2392 lp->lp_dc_error = 0;
2393 spin_unlock(&lp->lp_lock);
2397 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2398 * dropped/retaken within this function. An lnet_peer_ni is passed in
2399 * because discovery could tear down an lnet_peer.
2402 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2405 struct lnet_peer *lp;
2410 lnet_net_unlock(cpt);
2411 lnet_net_lock(LNET_LOCK_EX);
2412 lp = lpni->lpni_peer_net->lpn_peer;
2413 lnet_peer_clear_discovery_error(lp);
2416 * We're willing to be interrupted. The lpni can become a
2417 * zombie if we race with DLC, so we must check for that.
2420 /* Keep lp alive when the lnet_net_lock is unlocked */
2421 lnet_peer_addref_locked(lp);
2422 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2423 if (signal_pending(current))
2425 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2428 * Don't repeat discovery if discovery is disabled. This is
2429 * done to ensure we can use discovery as a standard ping as
2430 * well for backwards compatibility with routers which do not
2431 * have discovery or have discovery disabled
2433 if (lnet_is_discovery_disabled(lp) && count > 0)
2435 if (lp->lp_dc_error)
2437 if (lnet_peer_is_uptodate(lp))
2439 lnet_peer_queue_for_discovery(lp);
2441 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2444 * If caller requested a non-blocking operation then
2445 * return immediately. Once discovery is complete any
2446 * pending messages that were stopped due to discovery
2447 * will be transmitted.
2452 lnet_net_unlock(LNET_LOCK_EX);
2454 finish_wait(&lp->lp_dc_waitq, &wait);
2455 lnet_net_lock(LNET_LOCK_EX);
2456 lnet_peer_decref_locked(lp);
2457 /* Peer may have changed */
2458 lp = lpni->lpni_peer_net->lpn_peer;
2460 finish_wait(&lp->lp_dc_waitq, &wait);
2462 lnet_net_unlock(LNET_LOCK_EX);
2464 lnet_peer_decref_locked(lp);
2466 * The peer may have changed, so re-check and rediscover if that turns
2467 * out to have been the case. The reference count on lp ensured that
2468 * even if it was unlinked from lpni the memory could not be recycled.
2469 * Thus the check below is sufficient to determine whether the peer
2470 * changed. If the peer changed, then lp must not be dereferenced.
2472 if (lp != lpni->lpni_peer_net->lpn_peer)
2475 if (signal_pending(current))
2477 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2479 else if (lp->lp_dc_error)
2480 rc = lp->lp_dc_error;
2482 CDEBUG(D_NET, "non-blocking discovery\n");
2483 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2486 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2487 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2488 libcfs_nid2str(lpni->lpni_nid), rc,
2489 (!block) ? "pending discovery" : "discovery complete");
2494 /* Handle an incoming ack for a push. */
2496 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2498 struct lnet_ping_buffer *pbuf;
2500 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2501 spin_lock(&lp->lp_lock);
2502 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2503 lp->lp_push_error = ev->status;
2505 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2507 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2508 spin_unlock(&lp->lp_lock);
2510 CDEBUG(D_NET, "peer %s ev->status %d\n",
2511 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2514 /* Handle a Reply message. This is the reply to a Ping message. */
2516 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2518 struct lnet_ping_buffer *pbuf;
2521 spin_lock(&lp->lp_lock);
2523 lp->lp_disc_src_nid = ev->target.nid;
2526 * If some kind of error happened the contents of message
2527 * cannot be used. Set PING_FAILED to trigger a retry.
2530 lp->lp_state |= LNET_PEER_PING_FAILED;
2531 lp->lp_ping_error = ev->status;
2532 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2534 libcfs_nid2str(lp->lp_primary_nid),
2535 libcfs_nid2str(ev->source.nid));
2539 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2540 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2541 lnet_swap_pinginfo(pbuf);
2544 * A reply with invalid or corrupted info. Set PING_FAILED to
2547 rc = lnet_ping_info_validate(&pbuf->pb_info);
2549 lp->lp_state |= LNET_PEER_PING_FAILED;
2550 lp->lp_ping_error = 0;
2551 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2552 libcfs_nid2str(lp->lp_primary_nid), rc);
2558 * The peer may have discovery disabled at its end. Set
2559 * NO_DISCOVERY as appropriate.
2561 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2562 !lnet_peer_discovery_disabled) {
2563 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2564 libcfs_nid2str(lp->lp_primary_nid));
2565 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2567 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2568 libcfs_nid2str(lp->lp_primary_nid));
2569 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2573 * Update the MULTI_RAIL flag based on the reply. If the peer
2574 * was configured with DLC then the setting should match what
2577 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2578 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2579 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2580 libcfs_nid2str(lp->lp_primary_nid), lp);
2581 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2582 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2583 libcfs_nid2str(lp->lp_primary_nid));
2584 } else if (lnet_peer_discovery_disabled) {
2586 "peer %s(%p) not MR: DD disabled locally\n",
2587 libcfs_nid2str(lp->lp_primary_nid), lp);
2588 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2590 "peer %s(%p) not MR: DD disabled remotely\n",
2591 libcfs_nid2str(lp->lp_primary_nid), lp);
2593 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2594 libcfs_nid2str(lp->lp_primary_nid), lp);
2595 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2596 lnet_peer_clr_non_mr_pref_nids(lp);
2598 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2599 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2600 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2601 libcfs_nid2str(lp->lp_primary_nid));
2603 CERROR("Multi-Rail state vanished from %s\n",
2604 libcfs_nid2str(lp->lp_primary_nid));
2605 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2610 * Make sure we'll allocate the correct size ping buffer when
2613 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2614 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2617 * Check for truncation of the Reply. Clear PING_SENT and set
2618 * PING_FAILED to trigger a retry.
2620 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2621 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2622 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2623 lp->lp_state |= LNET_PEER_PING_FAILED;
2624 lp->lp_ping_error = 0;
2625 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2626 libcfs_nid2str(lp->lp_primary_nid),
2627 pbuf->pb_info.pi_nnis);
2632 * Check the sequence numbers in the reply. These are only
2633 * available if the reply came from a Multi-Rail peer.
2635 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2636 pbuf->pb_info.pi_nnis > 1 &&
2637 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2638 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2639 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2640 libcfs_nid2str(lp->lp_primary_nid),
2641 LNET_PING_BUFFER_SEQNO(pbuf),
2644 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2647 /* We're happy with the state of the data in the buffer. */
2648 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2649 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2650 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2651 lnet_ping_buffer_decref(lp->lp_data);
2653 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2654 lnet_ping_buffer_addref(pbuf);
2657 lp->lp_state &= ~LNET_PEER_PING_SENT;
2658 spin_unlock(&lp->lp_lock);
2660 lnet_net_lock(LNET_LOCK_EX);
2662 * If this peer is a gateway, call the routing callback to
2663 * handle the ping reply
2665 if (lp->lp_rtr_refcount > 0)
2666 lnet_router_discovery_ping_reply(lp);
2667 lnet_net_unlock(LNET_LOCK_EX);
2671 * Send event handling. Only matters for error cases, where we clean
2672 * up state on the peer and peer_ni that would otherwise be updated in
2673 * the REPLY event handler for a successful Ping, and the ACK event
2674 * handler for a successful Push.
2677 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2684 spin_lock(&lp->lp_lock);
2685 if (ev->msg_type == LNET_MSG_GET) {
2686 lp->lp_state &= ~LNET_PEER_PING_SENT;
2687 lp->lp_state |= LNET_PEER_PING_FAILED;
2688 lp->lp_ping_error = ev->status;
2689 } else { /* ev->msg_type == LNET_MSG_PUT */
2690 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2691 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2692 lp->lp_push_error = ev->status;
2694 spin_unlock(&lp->lp_lock);
2695 rc = LNET_REDISCOVER_PEER;
2697 CDEBUG(D_NET, "%s Send to %s: %d\n",
2698 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2699 libcfs_nid2str(ev->target.nid), rc);
2704 * Unlink event handling. This event is only seen if a call to
2705 * LNetMDUnlink() caused the event to be unlinked. If this call was
2706 * made after the event was set up in LNetGet() or LNetPut() then we
2707 * assume the Ping or Push timed out.
2710 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2712 spin_lock(&lp->lp_lock);
2713 /* We've passed through LNetGet() */
2714 if (lp->lp_state & LNET_PEER_PING_SENT) {
2715 lp->lp_state &= ~LNET_PEER_PING_SENT;
2716 lp->lp_state |= LNET_PEER_PING_FAILED;
2717 lp->lp_ping_error = -ETIMEDOUT;
2718 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2719 libcfs_nid2str(lp->lp_primary_nid));
2721 /* We've passed through LNetPut() */
2722 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2723 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2724 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2725 lp->lp_push_error = -ETIMEDOUT;
2726 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2727 libcfs_nid2str(lp->lp_primary_nid));
2729 spin_unlock(&lp->lp_lock);
2733 * Event handler for the discovery EQ.
2735 * Called with lnet_res_lock(cpt) held. The cpt is the
2736 * lnet_cpt_of_cookie() of the md handle cookie.
2738 static void lnet_discovery_event_handler(struct lnet_event *event)
2740 struct lnet_peer *lp = event->md_user_ptr;
2741 struct lnet_ping_buffer *pbuf;
2744 /* discovery needs to take another look */
2745 rc = LNET_REDISCOVER_PEER;
2747 CDEBUG(D_NET, "Received event: %d\n", event->type);
2749 switch (event->type) {
2750 case LNET_EVENT_ACK:
2751 lnet_discovery_event_ack(lp, event);
2753 case LNET_EVENT_REPLY:
2754 lnet_discovery_event_reply(lp, event);
2756 case LNET_EVENT_SEND:
2757 /* Only send failure triggers a retry. */
2758 rc = lnet_discovery_event_send(lp, event);
2760 case LNET_EVENT_UNLINK:
2761 /* LNetMDUnlink() was called */
2762 lnet_discovery_event_unlink(lp, event);
2765 /* Invalid events. */
2768 lnet_net_lock(LNET_LOCK_EX);
2769 if (event->unlinked) {
2770 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2771 lnet_ping_buffer_decref(pbuf);
2772 lnet_peer_decref_locked(lp);
2775 /* put peer back at end of request queue, if discovery not already
2777 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2778 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2779 wake_up(&the_lnet.ln_dc_waitq);
2781 lnet_net_unlock(LNET_LOCK_EX);
2785 * Build a peer from incoming data.
2787 * The NIDs in the incoming data are supposed to be structured as follows:
2790 * - other NIDs in same net
2791 * - NIDs in second net
2792 * - NIDs in third net
2794 * This due to the way the list of NIDs in the data is created.
2796 * Note that this function will mark the peer uptodate unless an
2797 * ENOMEM is encontered. All other errors are due to a conflict
2798 * between the DLC configuration and what discovery sees. We treat DLC
2799 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2800 * peer from becoming stuck in discovery.
2802 static int lnet_peer_merge_data(struct lnet_peer *lp,
2803 struct lnet_ping_buffer *pbuf)
2805 struct lnet_peer_net *lpn;
2806 struct lnet_peer_ni *lpni;
2807 lnet_nid_t *curnis = NULL;
2808 struct lnet_ni_status *addnis = NULL;
2809 lnet_nid_t *delnis = NULL;
2819 flags = LNET_PEER_DISCOVERED;
2820 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2821 flags |= LNET_PEER_MULTI_RAIL;
2824 * Cache the routing feature for the peer; whether it is enabled
2825 * for disabled as reported by the remote peer.
2827 spin_lock(&lp->lp_lock);
2828 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2829 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2831 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2832 spin_unlock(&lp->lp_lock);
2834 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2835 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2836 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2837 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2838 if (!curnis || !addnis || !delnis) {
2846 /* Construct the list of NIDs present in peer. */
2848 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2849 curnis[ncurnis++] = lpni->lpni_nid;
2852 * Check for NIDs in pbuf not present in curnis[].
2853 * The loop starts at 1 to skip the loopback NID.
2855 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2856 for (j = 0; j < ncurnis; j++)
2857 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2860 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2863 * Check for NIDs in curnis[] not present in pbuf.
2864 * The nested loop starts at 1 to skip the loopback NID.
2866 * But never add the loopback NID to delnis[]: if it is
2867 * present in curnis[] then this peer is for this node.
2869 for (i = 0; i < ncurnis; i++) {
2870 if (curnis[i] == LNET_NID_LO_0)
2872 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2873 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2875 * update the information we cache for the
2876 * peer with the latest information we
2879 lpni = lnet_find_peer_ni_locked(curnis[i]);
2881 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2882 lnet_peer_ni_decref_locked(lpni);
2887 if (j == pbuf->pb_info.pi_nnis)
2888 delnis[ndelnis++] = curnis[i];
2892 * If we get here and the discovery is disabled then we don't want
2893 * to add or delete any NIs. We just updated the ones we have some
2894 * information on, and call it a day
2897 if (lnet_is_discovery_disabled(lp))
2900 for (i = 0; i < naddnis; i++) {
2901 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2903 CERROR("Error adding NID %s to peer %s: %d\n",
2904 libcfs_nid2str(addnis[i].ns_nid),
2905 libcfs_nid2str(lp->lp_primary_nid), rc);
2909 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2911 lpni->lpni_ns_status = addnis[i].ns_status;
2912 lnet_peer_ni_decref_locked(lpni);
2916 for (i = 0; i < ndelnis; i++) {
2918 * for routers it's okay to delete the primary_nid because
2919 * the upper layers don't really rely on it. So if we're
2920 * being told that the router changed its primary_nid
2921 * then it's okay to delete it.
2923 if (lp->lp_rtr_refcount > 0)
2924 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2925 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2927 CERROR("Error deleting NID %s from peer %s: %d\n",
2928 libcfs_nid2str(delnis[i]),
2929 libcfs_nid2str(lp->lp_primary_nid), rc);
2935 /* The peer net for the primary NID should be the first entry in the
2936 * peer's lp_peer_nets list, and the peer NI for the primary NID should
2937 * be the first entry in its peer net's lpn_peer_nis list.
2939 lpni = lnet_find_peer_ni_locked(pbuf->pb_info.pi_ni[1].ns_nid);
2941 CERROR("Internal error: Failed to lookup peer NI for primary NID: %s\n",
2942 libcfs_nid2str(pbuf->pb_info.pi_ni[1].ns_nid));
2946 lnet_peer_ni_decref_locked(lpni);
2948 lpn = lpni->lpni_peer_net;
2949 if (lpn->lpn_peer_nets.prev != &lp->lp_peer_nets)
2950 list_move(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
2952 if (lpni->lpni_peer_nis.prev != &lpni->lpni_peer_net->lpn_peer_nis)
2953 list_move(&lpni->lpni_peer_nis,
2954 &lpni->lpni_peer_net->lpn_peer_nis);
2957 * Errors other than -ENOMEM are due to peers having been
2958 * configured with DLC. Ignore these because DLC overrides
2963 CFS_FREE_PTR_ARRAY(curnis, nnis);
2964 CFS_FREE_PTR_ARRAY(addnis, nnis);
2965 CFS_FREE_PTR_ARRAY(delnis, nnis);
2966 lnet_ping_buffer_decref(pbuf);
2967 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2970 spin_lock(&lp->lp_lock);
2971 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2972 lp->lp_state |= LNET_PEER_FORCE_PING;
2973 spin_unlock(&lp->lp_lock);
2979 * The data in pbuf says lp is its primary peer, but the data was
2980 * received by a different peer. Try to update lp with the data.
2983 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2985 struct lnet_handle_md mdh;
2987 /* Queue lp for discovery, and force it on the request queue. */
2988 lnet_net_lock(LNET_LOCK_EX);
2989 if (lnet_peer_queue_for_discovery(lp))
2990 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2991 lnet_net_unlock(LNET_LOCK_EX);
2993 LNetInvalidateMDHandle(&mdh);
2996 * Decide whether we can move the peer to the DATA_PRESENT state.
2998 * We replace stale data for a multi-rail peer, repair PING_FAILED
2999 * status, and preempt FORCE_PING.
3001 * If after that we have DATA_PRESENT, we merge it into this peer.
3003 spin_lock(&lp->lp_lock);
3004 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
3005 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
3006 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
3007 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3008 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3009 lnet_ping_buffer_decref(pbuf);
3014 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3015 lnet_ping_buffer_decref(lp->lp_data);
3017 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3019 if (lp->lp_state & LNET_PEER_PING_FAILED) {
3020 mdh = lp->lp_ping_mdh;
3021 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3022 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3023 lp->lp_ping_error = 0;
3025 if (lp->lp_state & LNET_PEER_FORCE_PING)
3026 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3027 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3028 spin_unlock(&lp->lp_lock);
3030 if (!LNetMDHandleIsInvalid(mdh))
3034 return lnet_peer_merge_data(lp, pbuf);
3036 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3040 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
3044 for (i = 0; i < pinfo->pi_nnis; i++) {
3045 if (pinfo->pi_ni[i].ns_nid == nid)
3052 /* Delete a peer that has been marked for deletion. NB: when this peer was added
3053 * to the discovery queue a reference was taken that will prevent the peer from
3054 * actually being freed by this function. After this function exits the
3055 * discovery thread should call lnet_peer_discovery_complete() which will
3056 * drop that reference as well as wake any waiters that may also be holding a
3059 static int lnet_peer_deletion(struct lnet_peer *lp)
3060 __must_hold(&lp->lp_lock)
3062 struct list_head rlist;
3063 struct lnet_route *route, *tmp;
3064 int sensitivity = lp->lp_health_sensitivity;
3066 INIT_LIST_HEAD(&rlist);
3068 lp->lp_state &= ~(LNET_PEER_DISCOVERING | LNET_PEER_FORCE_PING |
3069 LNET_PEER_FORCE_PUSH);
3070 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3071 libcfs_nid2str(lp->lp_primary_nid), lp, lp->lp_state);
3073 /* no-op if lnet_peer_del() has already been called on this peer */
3074 if (lp->lp_state & LNET_PEER_MARK_DELETED)
3077 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
3080 spin_unlock(&lp->lp_lock);
3082 mutex_lock(&the_lnet.ln_api_mutex);
3084 lnet_net_lock(LNET_LOCK_EX);
3085 /* remove the peer from the discovery work
3086 * queue if it's on there in preparation
3089 if (!list_empty(&lp->lp_dc_list))
3090 list_del(&lp->lp_dc_list);
3091 list_for_each_entry_safe(route, tmp,
3094 lnet_move_route(route, NULL, &rlist);
3095 lnet_net_unlock(LNET_LOCK_EX);
3097 /* lnet_peer_del() deletes all the peer NIs owned by this peer */
3100 list_for_each_entry_safe(route, tmp,
3102 /* re-add these routes */
3103 lnet_add_route(route->lr_net,
3108 LIBCFS_FREE(route, sizeof(*route));
3111 mutex_unlock(&the_lnet.ln_api_mutex);
3113 spin_lock(&lp->lp_lock);
3119 * Update a peer using the data received.
3121 static int lnet_peer_data_present(struct lnet_peer *lp)
3122 __must_hold(&lp->lp_lock)
3124 struct lnet_ping_buffer *pbuf;
3125 struct lnet_peer_ni *lpni;
3126 lnet_nid_t nid = LNET_NID_ANY;
3132 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3133 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3134 spin_unlock(&lp->lp_lock);
3137 * Modifications of peer structures are done while holding the
3138 * ln_api_mutex. A global lock is required because we may be
3139 * modifying multiple peer structures, and a mutex greatly
3140 * simplifies memory management.
3142 * The actual changes to the data structures must also protect
3143 * against concurrent lookups, for which the lnet_net_lock in
3144 * LNET_LOCK_EX mode is used.
3146 mutex_lock(&the_lnet.ln_api_mutex);
3147 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3153 * If this peer is not on the peer list then it is being torn
3154 * down, and our reference count may be all that is keeping it
3155 * alive. Don't do any work on it.
3157 if (list_empty(&lp->lp_peer_list))
3160 flags = LNET_PEER_DISCOVERED;
3161 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
3162 flags |= LNET_PEER_MULTI_RAIL;
3165 * Check whether the primary NID in the message matches the
3166 * primary NID of the peer. If it does, update the peer, if
3167 * it it does not, check whether there is already a peer with
3168 * that primary NID. If no such peer exists, try to update
3169 * the primary NID of the current peer (allowed if it was
3170 * created due to message traffic) and complete the update.
3171 * If the peer did exist, hand off the data to it.
3173 * The peer for the loopback interface is a special case: this
3174 * is the peer for the local node, and we want to set its
3175 * primary NID to the correct value here. Moreover, this peer
3176 * can show up with only the loopback NID in the ping buffer.
3178 if (pbuf->pb_info.pi_nnis <= 1)
3180 nid = pbuf->pb_info.pi_ni[1].ns_nid;
3181 if (lp->lp_primary_nid == LNET_NID_LO_0) {
3182 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3184 rc = lnet_peer_merge_data(lp, pbuf);
3186 * if the primary nid of the peer is present in the ping info returned
3187 * from the peer, but it's not the local primary peer we have
3188 * cached and discovery is disabled, then we don't want to update
3189 * our local peer info, by adding or removing NIDs, we just want
3190 * to update the status of the nids that we currently have
3191 * recorded in that peer.
3193 } else if (lp->lp_primary_nid == nid ||
3194 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
3195 lnet_is_discovery_disabled(lp))) {
3196 rc = lnet_peer_merge_data(lp, pbuf);
3198 lpni = lnet_find_peer_ni_locked(nid);
3199 if (!lpni || lp == lpni->lpni_peer_net->lpn_peer) {
3200 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3202 CERROR("Primary NID error %s versus %s: %d\n",
3203 libcfs_nid2str(lp->lp_primary_nid),
3204 libcfs_nid2str(nid), rc);
3206 rc = lnet_peer_merge_data(lp, pbuf);
3209 lnet_peer_ni_decref_locked(lpni);
3211 struct lnet_peer *new_lp;
3212 new_lp = lpni->lpni_peer_net->lpn_peer;
3214 * if lp has discovery/MR enabled that means new_lp
3215 * should have discovery/MR enabled as well, since
3216 * it's the same peer, which we're about to merge
3218 spin_lock(&lp->lp_lock);
3219 spin_lock(&new_lp->lp_lock);
3220 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
3221 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
3222 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
3223 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
3224 /* If we're processing a ping reply then we may be
3225 * about to send a push to the peer that we ping'd.
3226 * Since the ping reply that we're processing was
3227 * received by lp, we need to set the discovery source
3228 * NID for new_lp to the NID stored in lp.
3230 if (lp->lp_disc_src_nid != LNET_NID_ANY)
3231 new_lp->lp_disc_src_nid = lp->lp_disc_src_nid;
3232 spin_unlock(&new_lp->lp_lock);
3233 spin_unlock(&lp->lp_lock);
3235 rc = lnet_peer_set_primary_data(new_lp, pbuf);
3236 lnet_consolidate_routes_locked(lp, new_lp);
3237 lnet_peer_ni_decref_locked(lpni);
3241 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
3243 mutex_unlock(&the_lnet.ln_api_mutex);
3245 spin_lock(&lp->lp_lock);
3246 /* Tell discovery to re-check the peer immediately. */
3248 rc = LNET_REDISCOVER_PEER;
3253 * A ping failed. Clear the PING_FAILED state and set the
3254 * FORCE_PING state, to ensure a retry even if discovery is
3255 * disabled. This avoids being left with incorrect state.
3257 static int lnet_peer_ping_failed(struct lnet_peer *lp)
3258 __must_hold(&lp->lp_lock)
3260 struct lnet_handle_md mdh;
3263 mdh = lp->lp_ping_mdh;
3264 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3265 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3266 lp->lp_state |= LNET_PEER_FORCE_PING;
3267 rc = lp->lp_ping_error;
3268 lp->lp_ping_error = 0;
3269 spin_unlock(&lp->lp_lock);
3271 if (!LNetMDHandleIsInvalid(mdh))
3274 CDEBUG(D_NET, "peer %s:%d\n",
3275 libcfs_nid2str(lp->lp_primary_nid), rc);
3277 spin_lock(&lp->lp_lock);
3278 return rc ? rc : LNET_REDISCOVER_PEER;
3282 * Select NID to send a Ping or Push to.
3284 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
3286 struct lnet_peer_ni *lpni;
3288 /* Look for a direct-connected NID for this peer. */
3290 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3291 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
3296 return lpni->lpni_nid;
3298 /* Look for a routed-connected NID for this peer. */
3300 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3301 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
3306 return lpni->lpni_nid;
3308 return LNET_NID_ANY;
3311 /* Active side of ping. */
3312 static int lnet_peer_send_ping(struct lnet_peer *lp)
3313 __must_hold(&lp->lp_lock)
3320 lp->lp_state |= LNET_PEER_PING_SENT;
3321 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3322 spin_unlock(&lp->lp_lock);
3324 cpt = lnet_net_lock_current();
3325 /* Refcount for MD. */
3326 lnet_peer_addref_locked(lp);
3327 pnid = lnet_peer_select_nid(lp);
3328 lnet_net_unlock(cpt);
3330 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3332 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
3333 the_lnet.ln_dc_handler, false);
3336 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3337 * refcount on the peer, otherwise LNetMDUnlink will be called
3338 * which will eventually do that.
3342 lnet_peer_decref_locked(lp);
3343 lnet_net_unlock(cpt);
3344 rc = -rc; /* change the rc to negative value */
3346 } else if (rc < 0) {
3350 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3352 spin_lock(&lp->lp_lock);
3356 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3358 * The errors that get us here are considered hard errors and
3359 * cause Discovery to terminate. So we clear PING_SENT, but do
3360 * not set either PING_FAILED or FORCE_PING. In fact we need
3361 * to clear PING_FAILED, because the unlink event handler will
3362 * have set it if we called LNetMDUnlink() above.
3364 spin_lock(&lp->lp_lock);
3365 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3370 * This function exists because you cannot call LNetMDUnlink() from an
3373 static int lnet_peer_push_failed(struct lnet_peer *lp)
3374 __must_hold(&lp->lp_lock)
3376 struct lnet_handle_md mdh;
3379 mdh = lp->lp_push_mdh;
3380 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3381 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3382 rc = lp->lp_push_error;
3383 lp->lp_push_error = 0;
3384 spin_unlock(&lp->lp_lock);
3386 if (!LNetMDHandleIsInvalid(mdh))
3389 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3390 spin_lock(&lp->lp_lock);
3391 return rc ? rc : LNET_REDISCOVER_PEER;
3395 * Mark the peer as discovered.
3397 static int lnet_peer_discovered(struct lnet_peer *lp)
3398 __must_hold(&lp->lp_lock)
3400 lp->lp_state |= LNET_PEER_DISCOVERED;
3401 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3402 LNET_PEER_REDISCOVER);
3404 lp->lp_dc_error = 0;
3406 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3411 /* Active side of push. */
3412 static int lnet_peer_send_push(struct lnet_peer *lp)
3413 __must_hold(&lp->lp_lock)
3415 struct lnet_ping_buffer *pbuf;
3416 struct lnet_process_id id;
3421 /* Don't push to a non-multi-rail peer. */
3422 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3423 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3424 /* if peer's NIDs are uptodate then peer is discovered */
3425 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3426 rc = lnet_peer_discovered(lp);
3433 lp->lp_state |= LNET_PEER_PUSH_SENT;
3434 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3435 spin_unlock(&lp->lp_lock);
3437 cpt = lnet_net_lock_current();
3438 pbuf = the_lnet.ln_ping_target;
3439 lnet_ping_buffer_addref(pbuf);
3440 lnet_net_unlock(cpt);
3442 /* Push source MD */
3443 md.start = &pbuf->pb_info;
3444 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3445 md.threshold = 2; /* Put/Ack */
3447 md.options = LNET_MD_TRACK_RESPONSE;
3448 md.handler = the_lnet.ln_dc_handler;
3451 rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3453 lnet_ping_buffer_decref(pbuf);
3454 CERROR("Can't bind push source MD: %d\n", rc);
3457 cpt = lnet_net_lock_current();
3458 /* Refcount for MD. */
3459 lnet_peer_addref_locked(lp);
3460 id.pid = LNET_PID_LUSTRE;
3461 id.nid = lnet_peer_select_nid(lp);
3462 lnet_net_unlock(cpt);
3464 if (id.nid == LNET_NID_ANY) {
3469 rc = LNetPut(lp->lp_disc_src_nid, lp->lp_push_mdh,
3470 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3471 LNET_PROTO_PING_MATCHBITS, 0, 0);
3474 * reset the discovery nid. There is no need to restrict sending
3475 * from that source, if we call lnet_push_update_to_peers(). It'll
3476 * get set to a specific NID, if we initiate discovery from the
3479 lp->lp_disc_src_nid = LNET_NID_ANY;
3484 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3486 spin_lock(&lp->lp_lock);
3490 LNetMDUnlink(lp->lp_push_mdh);
3491 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3493 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3495 * The errors that get us here are considered hard errors and
3496 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3497 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3498 * because the unlink event handler will have set it if we
3499 * called LNetMDUnlink() above.
3501 spin_lock(&lp->lp_lock);
3502 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3507 * An unrecoverable error was encountered during discovery.
3508 * Set error status in peer and abort discovery.
3510 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3512 CDEBUG(D_NET, "Discovery error %s: %d\n",
3513 libcfs_nid2str(lp->lp_primary_nid), error);
3515 spin_lock(&lp->lp_lock);
3516 lp->lp_dc_error = error;
3517 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3518 lp->lp_state |= LNET_PEER_REDISCOVER;
3519 spin_unlock(&lp->lp_lock);
3523 * Wait for work to be queued or some other change that must be
3524 * attended to. Returns non-zero if the discovery thread should shut
3527 static int lnet_peer_discovery_wait_for_work(void)
3534 cpt = lnet_net_lock_current();
3536 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3537 TASK_INTERRUPTIBLE);
3538 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3540 if (lnet_push_target_resize_needed() ||
3541 the_lnet.ln_push_target->pb_needs_post)
3543 if (!list_empty(&the_lnet.ln_dc_request))
3545 if (!list_empty(&the_lnet.ln_msg_resend))
3547 lnet_net_unlock(cpt);
3550 * wakeup max every second to check if there are peers that
3551 * have been stuck on the working queue for greater than
3554 schedule_timeout(cfs_time_seconds(1));
3555 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3556 cpt = lnet_net_lock_current();
3558 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3560 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3563 lnet_net_unlock(cpt);
3565 CDEBUG(D_NET, "woken: %d\n", rc);
3571 * Messages that were pending on a destroyed peer will be put on a global
3572 * resend list. The message resend list will be checked by
3573 * the discovery thread when it wakes up, and will resend messages. These
3574 * messages can still be sendable in the case the lpni which was the initial
3575 * cause of the message re-queue was transfered to another peer.
3577 * It is possible that LNet could be shutdown while we're iterating
3578 * through the list. lnet_shudown_lndnets() will attempt to access the
3579 * resend list, but will have to wait until the spinlock is released, by
3580 * which time there shouldn't be any more messages on the resend list.
3581 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3582 * for the messages so they can be released. The other case is that
3583 * lnet_shudown_lndnets() can finalize all the messages before this
3584 * function can visit the resend list, in which case this function will be
3587 static void lnet_resend_msgs(void)
3589 struct lnet_msg *msg, *tmp;
3593 spin_lock(&the_lnet.ln_msg_resend_lock);
3594 list_splice(&the_lnet.ln_msg_resend, &resend);
3595 spin_unlock(&the_lnet.ln_msg_resend_lock);
3597 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3598 list_del_init(&msg->msg_list);
3599 rc = lnet_send(msg->msg_src_nid_param, msg,
3600 msg->msg_rtr_nid_param);
3602 CNETERR("Error sending %s to %s: %d\n",
3603 lnet_msgtyp2str(msg->msg_type),
3604 libcfs_id2str(msg->msg_target), rc);
3605 lnet_finalize(msg, rc);
3610 /* The discovery thread. */
3611 static int lnet_peer_discovery(void *arg)
3613 struct lnet_peer *lp;
3616 wait_for_completion(&the_lnet.ln_started);
3618 CDEBUG(D_NET, "started\n");
3621 if (lnet_peer_discovery_wait_for_work())
3624 if (lnet_push_target_resize_needed())
3625 lnet_push_target_resize();
3626 else if (the_lnet.ln_push_target->pb_needs_post)
3627 lnet_push_target_post(the_lnet.ln_push_target,
3628 &the_lnet.ln_push_target_md);
3632 lnet_net_lock(LNET_LOCK_EX);
3633 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3634 lnet_net_unlock(LNET_LOCK_EX);
3639 * Process all incoming discovery work requests. When
3640 * discovery must wait on a peer to change state, it
3641 * is added to the tail of the ln_dc_working queue. A
3642 * timestamp keeps track of when the peer was added,
3643 * so we can time out discovery requests that take too
3646 while (!list_empty(&the_lnet.ln_dc_request)) {
3647 lp = list_first_entry(&the_lnet.ln_dc_request,
3648 struct lnet_peer, lp_dc_list);
3649 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3651 * set the time the peer was put on the dc_working
3652 * queue. It shouldn't remain on the queue
3653 * forever, in case the GET message (for ping)
3654 * doesn't get a REPLY or the PUT message (for
3655 * push) doesn't get an ACK.
3657 lp->lp_last_queued = ktime_get_real_seconds();
3658 lnet_net_unlock(LNET_LOCK_EX);
3660 if (lnet_push_target_resize_needed())
3661 lnet_push_target_resize();
3662 else if (the_lnet.ln_push_target->pb_needs_post)
3663 lnet_push_target_post(the_lnet.ln_push_target,
3664 &the_lnet.ln_push_target_md);
3667 * Select an action depending on the state of
3668 * the peer and whether discovery is disabled.
3669 * The check whether discovery is disabled is
3670 * done after the code that handles processing
3671 * for arrived data, cleanup for failures, and
3672 * forcing a Ping or Push.
3674 spin_lock(&lp->lp_lock);
3675 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3676 libcfs_nid2str(lp->lp_primary_nid), lp,
3678 if (lp->lp_state & (LNET_PEER_MARK_DELETION |
3679 LNET_PEER_MARK_DELETED))
3680 rc = lnet_peer_deletion(lp);
3681 else if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3682 rc = lnet_peer_data_present(lp);
3683 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3684 rc = lnet_peer_ping_failed(lp);
3685 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3686 rc = lnet_peer_push_failed(lp);
3687 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3688 rc = lnet_peer_send_ping(lp);
3689 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3690 rc = lnet_peer_send_push(lp);
3691 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3692 rc = lnet_peer_send_ping(lp);
3693 else if (lnet_peer_needs_push(lp))
3694 rc = lnet_peer_send_push(lp);
3696 rc = lnet_peer_discovered(lp);
3697 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3698 libcfs_nid2str(lp->lp_primary_nid), lp,
3700 spin_unlock(&lp->lp_lock);
3702 lnet_net_lock(LNET_LOCK_EX);
3703 if (rc == LNET_REDISCOVER_PEER) {
3704 list_move(&lp->lp_dc_list,
3705 &the_lnet.ln_dc_request);
3707 lnet_peer_discovery_error(lp, rc);
3709 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3710 lnet_peer_discovery_complete(lp);
3711 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3716 lnet_net_unlock(LNET_LOCK_EX);
3719 CDEBUG(D_NET, "stopping\n");
3721 * Clean up before telling lnet_peer_discovery_stop() that
3722 * we're done. Use wake_up() below to somewhat reduce the
3723 * size of the thundering herd if there are multiple threads
3724 * waiting on discovery of a single peer.
3727 /* Queue cleanup 1: stop all pending pings and pushes. */
3728 lnet_net_lock(LNET_LOCK_EX);
3729 while (!list_empty(&the_lnet.ln_dc_working)) {
3730 lp = list_first_entry(&the_lnet.ln_dc_working,
3731 struct lnet_peer, lp_dc_list);
3732 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3733 lnet_net_unlock(LNET_LOCK_EX);
3734 lnet_peer_cancel_discovery(lp);
3735 lnet_net_lock(LNET_LOCK_EX);
3737 lnet_net_unlock(LNET_LOCK_EX);
3739 /* Queue cleanup 2: wait for the expired queue to clear. */
3740 while (!list_empty(&the_lnet.ln_dc_expired))
3741 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3743 /* Queue cleanup 3: clear the request queue. */
3744 lnet_net_lock(LNET_LOCK_EX);
3745 while (!list_empty(&the_lnet.ln_dc_request)) {
3746 lp = list_first_entry(&the_lnet.ln_dc_request,
3747 struct lnet_peer, lp_dc_list);
3748 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3749 lnet_peer_discovery_complete(lp);
3751 lnet_net_unlock(LNET_LOCK_EX);
3753 lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3754 the_lnet.ln_dc_handler = NULL;
3756 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3757 wake_up(&the_lnet.ln_dc_waitq);
3759 CDEBUG(D_NET, "stopped\n");
3764 /* ln_api_mutex is held on entry. */
3765 int lnet_peer_discovery_start(void)
3767 struct task_struct *task;
3770 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3773 the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3774 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3775 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3778 CERROR("Can't start peer discovery thread: %d\n", rc);
3780 the_lnet.ln_dc_handler = NULL;
3782 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3785 CDEBUG(D_NET, "discovery start: %d\n", rc);
3790 /* ln_api_mutex is held on entry. */
3791 void lnet_peer_discovery_stop(void)
3793 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3796 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3797 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3799 /* In the LNetNIInit() path we may be stopping discovery before it
3800 * entered its work loop
3802 if (!completion_done(&the_lnet.ln_started))
3803 complete(&the_lnet.ln_started);
3805 wake_up(&the_lnet.ln_dc_waitq);
3807 wait_event(the_lnet.ln_dc_waitq,
3808 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3810 LASSERT(list_empty(&the_lnet.ln_dc_request));
3811 LASSERT(list_empty(&the_lnet.ln_dc_working));
3812 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3814 CDEBUG(D_NET, "discovery stopped\n");
3820 lnet_debug_peer(lnet_nid_t nid)
3822 char *aliveness = "NA";
3823 struct lnet_peer_ni *lp;
3826 cpt = lnet_cpt_of_nid(nid, NULL);
3829 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3831 lnet_net_unlock(cpt);
3832 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3836 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3837 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3839 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3840 libcfs_nid2str(lp->lpni_nid), kref_read(&lp->lpni_kref),
3841 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3842 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3843 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3845 lnet_peer_ni_decref_locked(lp);
3847 lnet_net_unlock(cpt);
3850 /* Gathering information for userspace. */
3852 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3853 char aliveness[LNET_MAX_STR_LEN],
3854 __u32 *cpt_iter, __u32 *refcount,
3855 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3856 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3857 __u32 *peer_tx_qnob)
3859 struct lnet_peer_table *peer_table;
3860 struct lnet_peer_ni *lp;
3865 /* get the number of CPTs */
3866 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3868 /* if the cpt number to be examined is >= the number of cpts in
3869 * the system then indicate that there are no more cpts to examin
3871 if (*cpt_iter >= lncpt)
3874 /* get the current table */
3875 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3876 /* if the ptable is NULL then there are no more cpts to examine */
3877 if (peer_table == NULL)
3880 lnet_net_lock(*cpt_iter);
3882 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3883 struct list_head *peers = &peer_table->pt_hash[j];
3885 list_for_each_entry(lp, peers, lpni_hashlist) {
3886 if (peer_index-- > 0)
3889 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3890 if (lnet_isrouter(lp) ||
3891 lnet_peer_aliveness_enabled(lp))
3892 snprintf(aliveness, LNET_MAX_STR_LEN,
3893 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3895 *nid = lp->lpni_nid;
3896 *refcount = kref_read(&lp->lpni_kref);
3897 *ni_peer_tx_credits =
3898 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3899 *peer_tx_credits = lp->lpni_txcredits;
3900 *peer_rtr_credits = lp->lpni_rtrcredits;
3901 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3902 *peer_tx_qnob = lp->lpni_txqnob;
3908 lnet_net_unlock(*cpt_iter);
3912 return found ? 0 : -ENOENT;
3915 /* ln_api_mutex is held, which keeps the peer list stable */
3916 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3918 struct lnet_ioctl_element_stats *lpni_stats;
3919 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3920 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3921 struct lnet_peer_ni_credit_info *lpni_info;
3922 struct lnet_peer_ni *lpni;
3923 struct lnet_peer *lp;
3928 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3935 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3936 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3937 size *= lp->lp_nnis;
3938 if (size > cfg->prcfg_size) {
3939 cfg->prcfg_size = size;
3944 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3945 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3946 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3947 cfg->prcfg_count = lp->lp_nnis;
3948 cfg->prcfg_size = size;
3949 cfg->prcfg_state = lp->lp_state;
3951 /* Allocate helper buffers. */
3953 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3956 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3959 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3960 if (!lpni_msg_stats)
3961 goto out_free_stats;
3962 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3964 goto out_free_msg_stats;
3969 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3970 nid = lpni->lpni_nid;
3971 if (copy_to_user(bulk, &nid, sizeof(nid)))
3972 goto out_free_hstats;
3973 bulk += sizeof(nid);
3975 memset(lpni_info, 0, sizeof(*lpni_info));
3976 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3977 if (lnet_isrouter(lpni) ||
3978 lnet_peer_aliveness_enabled(lpni))
3979 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3980 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3982 lpni_info->cr_refcount = kref_read(&lpni->lpni_kref);
3983 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3984 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3985 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3986 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3987 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3988 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3989 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3990 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3991 goto out_free_hstats;
3992 bulk += sizeof(*lpni_info);
3994 memset(lpni_stats, 0, sizeof(*lpni_stats));
3995 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3996 LNET_STATS_TYPE_SEND);
3997 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3998 LNET_STATS_TYPE_RECV);
3999 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
4000 LNET_STATS_TYPE_DROP);
4001 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
4002 goto out_free_hstats;
4003 bulk += sizeof(*lpni_stats);
4004 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
4005 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
4006 goto out_free_hstats;
4007 bulk += sizeof(*lpni_msg_stats);
4008 lpni_hstats->hlpni_network_timeout =
4009 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
4010 lpni_hstats->hlpni_remote_dropped =
4011 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
4012 lpni_hstats->hlpni_remote_timeout =
4013 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
4014 lpni_hstats->hlpni_remote_error =
4015 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
4016 lpni_hstats->hlpni_health_value =
4017 atomic_read(&lpni->lpni_healthv);
4018 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
4019 goto out_free_hstats;
4020 bulk += sizeof(*lpni_hstats);
4025 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
4027 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
4029 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
4031 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
4033 lnet_peer_decref_locked(lp);
4038 /* must hold net_lock/0 */
4040 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni,
4041 struct list_head *recovery_queue,
4044 /* the mt could've shutdown and cleaned up the queues */
4045 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
4048 if (!list_empty(&lpni->lpni_recovery))
4051 if (atomic_read(&lpni->lpni_healthv) == LNET_MAX_HEALTH_VALUE)
4054 if (!lpni->lpni_last_alive) {
4056 "lpni %s(%p) not eligible for recovery last alive %lld\n",
4057 libcfs_nid2str(lpni->lpni_nid), lpni,
4058 lpni->lpni_last_alive);
4062 if (now > lpni->lpni_last_alive + lnet_recovery_limit) {
4063 CDEBUG(D_NET, "lpni %s aged out last alive %lld\n",
4064 libcfs_nid2str(lpni->lpni_nid),
4065 lpni->lpni_last_alive);
4066 /* Reset the ping count so that if this peer NI is added back to
4067 * the recovery queue we will send the first ping right away.
4069 lpni->lpni_ping_count = 0;
4073 /* This peer NI is going on the recovery queue, so take a ref on it */
4074 lnet_peer_ni_addref_locked(lpni);
4076 lnet_peer_ni_set_next_ping(lpni, now);
4078 CDEBUG(D_NET, "%s added to recovery queue. ping count: %u next ping: %lld last alive: %lld health: %d\n",
4079 libcfs_nid2str(lpni->lpni_nid),
4080 lpni->lpni_ping_count,
4081 lpni->lpni_next_ping,
4082 lpni->lpni_last_alive,
4083 atomic_read(&lpni->lpni_healthv));
4085 list_add_tail(&lpni->lpni_recovery, recovery_queue);
4088 /* Call with the ln_api_mutex held */
4090 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
4092 struct lnet_peer_table *ptable;
4093 struct lnet_peer *lp;
4094 struct lnet_peer_net *lpn;
4095 struct lnet_peer_ni *lpni;
4100 if (the_lnet.ln_state != LNET_STATE_RUNNING)
4103 now = ktime_get_seconds();
4106 lnet_net_lock(LNET_LOCK_EX);
4107 lpni = lnet_find_peer_ni_locked(nid);
4109 lnet_net_unlock(LNET_LOCK_EX);
4112 atomic_set(&lpni->lpni_healthv, value);
4113 lnet_peer_ni_add_to_recoveryq_locked(lpni,
4114 &the_lnet.ln_mt_peerNIRecovq, now);
4115 lnet_peer_ni_decref_locked(lpni);
4116 lnet_net_unlock(LNET_LOCK_EX);
4120 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
4123 * Walk all the peers and reset the health value for each one to the
4126 lnet_net_lock(LNET_LOCK_EX);
4127 for (cpt = 0; cpt < lncpt; cpt++) {
4128 ptable = the_lnet.ln_peer_tables[cpt];
4129 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
4130 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
4131 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
4133 atomic_set(&lpni->lpni_healthv, value);
4134 lnet_peer_ni_add_to_recoveryq_locked(lpni,
4135 &the_lnet.ln_mt_peerNIRecovq, now);
4140 lnet_net_unlock(LNET_LOCK_EX);