4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
34 #define DEBUG_SUBSYSTEM S_LNET
36 #include <linux/sched.h>
37 #ifdef HAVE_SCHED_HEADERS
38 #include <linux/sched/signal.h>
40 #include <linux/uaccess.h>
42 #include <lnet/udsp.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER (1)
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
54 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
55 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
56 lnet_peer_ni_decref_locked(lpni);
61 lnet_peer_net_added(struct lnet_net *net)
63 struct lnet_peer_ni *lpni, *tmp;
65 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66 lpni_on_remote_peer_ni_list) {
68 if (LNET_NID_NET(&lpni->lpni_nid) == net->net_id) {
71 spin_lock(&lpni->lpni_lock);
72 lpni->lpni_txcredits =
73 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
74 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
75 lpni->lpni_rtrcredits =
76 lnet_peer_buffer_credits(lpni->lpni_net);
77 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
78 spin_unlock(&lpni->lpni_lock);
80 lnet_peer_remove_from_remote_list(lpni);
86 lnet_peer_tables_destroy(void)
88 struct lnet_peer_table *ptable;
89 struct list_head *hash;
93 if (!the_lnet.ln_peer_tables)
96 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
97 hash = ptable->pt_hash;
98 if (!hash) /* not intialized */
101 LASSERT(list_empty(&ptable->pt_zombie_list));
103 ptable->pt_hash = NULL;
104 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
105 LASSERT(list_empty(&hash[j]));
107 CFS_FREE_PTR_ARRAY(hash, LNET_PEER_HASH_SIZE);
110 cfs_percpt_free(the_lnet.ln_peer_tables);
111 the_lnet.ln_peer_tables = NULL;
115 lnet_peer_tables_create(void)
117 struct lnet_peer_table *ptable;
118 struct list_head *hash;
122 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
124 if (the_lnet.ln_peer_tables == NULL) {
125 CERROR("Failed to allocate cpu-partition peer tables\n");
129 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
130 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
131 LNET_PEER_HASH_SIZE * sizeof(*hash));
133 CERROR("Failed to create peer hash table\n");
134 lnet_peer_tables_destroy();
138 spin_lock_init(&ptable->pt_zombie_lock);
139 INIT_LIST_HEAD(&ptable->pt_zombie_list);
141 INIT_LIST_HEAD(&ptable->pt_peer_list);
143 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
144 INIT_LIST_HEAD(&hash[j]);
145 ptable->pt_hash = hash; /* sign of initialization */
151 static struct lnet_peer_ni *
152 lnet_peer_ni_alloc(struct lnet_nid *nid)
154 struct lnet_peer_ni *lpni;
155 struct lnet_net *net;
158 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
160 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
164 INIT_LIST_HEAD(&lpni->lpni_txq);
165 INIT_LIST_HEAD(&lpni->lpni_hashlist);
166 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
167 INIT_LIST_HEAD(&lpni->lpni_recovery);
168 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
169 INIT_LIST_HEAD(&lpni->lpni_rtr_pref_nids);
170 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
171 kref_init(&lpni->lpni_kref);
172 lpni->lpni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
174 spin_lock_init(&lpni->lpni_lock);
176 if (lnet_peers_start_down())
177 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
179 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
180 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
181 lpni->lpni_nid = *nid;
182 lpni->lpni_cpt = cpt;
183 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
185 net = lnet_get_net_locked(LNET_NID_NET(nid));
186 lpni->lpni_net = net;
188 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
189 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
190 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
191 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
194 * This peer_ni is not on a local network, so we
195 * cannot add the credits here. In case the net is
196 * added later, add the peer_ni to the remote peer ni
197 * list so it can be easily found and revisited.
199 /* FIXME: per-net implementation instead? */
200 lnet_peer_ni_addref_locked(lpni);
201 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
202 &the_lnet.ln_remote_peer_ni_list);
205 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nidstr(&lpni->lpni_nid));
210 static struct lnet_peer_net *
211 lnet_peer_net_alloc(__u32 net_id)
213 struct lnet_peer_net *lpn;
215 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
219 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
220 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
221 lpn->lpn_net_id = net_id;
222 lpn->lpn_sel_priority = LNET_MAX_SELECTION_PRIORITY;
224 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
230 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
232 struct lnet_peer *lp;
234 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
236 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
237 LASSERT(list_empty(&lpn->lpn_peer_nis));
238 LASSERT(list_empty(&lpn->lpn_peer_nets));
240 lpn->lpn_peer = NULL;
241 LIBCFS_FREE(lpn, sizeof(*lpn));
243 lnet_peer_decref_locked(lp);
246 static struct lnet_peer *
247 lnet_peer_alloc(struct lnet_nid *nid)
249 struct lnet_peer *lp;
251 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
255 INIT_LIST_HEAD(&lp->lp_rtrq);
256 INIT_LIST_HEAD(&lp->lp_routes);
257 INIT_LIST_HEAD(&lp->lp_peer_list);
258 INIT_LIST_HEAD(&lp->lp_peer_nets);
259 INIT_LIST_HEAD(&lp->lp_dc_list);
260 INIT_LIST_HEAD(&lp->lp_dc_pendq);
261 INIT_LIST_HEAD(&lp->lp_rtr_list);
262 init_waitqueue_head(&lp->lp_dc_waitq);
263 spin_lock_init(&lp->lp_lock);
264 lp->lp_primary_nid = *nid;
265 lp->lp_disc_src_nid = LNET_ANY_NID;
266 lp->lp_disc_dst_nid = LNET_ANY_NID;
267 if (lnet_peers_start_down())
268 lp->lp_alive = false;
273 * all peers created on a router should have health on
274 * if it's not already on.
276 if (the_lnet.ln_routing && !lnet_health_sensitivity)
277 lp->lp_health_sensitivity = 1;
280 * Turn off discovery for loopback peer. If you're creating a peer
281 * for the loopback interface then that was initiated when we
282 * attempted to send a message over the loopback. There is no need
283 * to ever use a different interface when sending messages to
287 lp->lp_state = LNET_PEER_NO_DISCOVERY;
288 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
290 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nidstr(&lp->lp_primary_nid));
296 lnet_destroy_peer_locked(struct lnet_peer *lp)
298 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nidstr(&lp->lp_primary_nid));
300 LASSERT(atomic_read(&lp->lp_refcount) == 0);
301 LASSERT(lp->lp_rtr_refcount == 0);
302 LASSERT(list_empty(&lp->lp_peer_nets));
303 LASSERT(list_empty(&lp->lp_peer_list));
304 LASSERT(list_empty(&lp->lp_dc_list));
307 lnet_ping_buffer_decref(lp->lp_data);
310 * if there are messages still on the pending queue, then make
311 * sure to queue them on the ln_msg_resend list so they can be
312 * resent at a later point if the discovery thread is still
314 * If the discovery thread has stopped, then the wakeup will be a
315 * no-op, and it is expected the lnet_shutdown_lndnets() will
316 * eventually be called, which will traverse this list and
317 * finalize the messages on the list.
318 * We can not resend them now because we're holding the cpt lock.
319 * Releasing the lock can cause an inconsistent state
321 spin_lock(&the_lnet.ln_msg_resend_lock);
322 spin_lock(&lp->lp_lock);
323 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
324 spin_unlock(&lp->lp_lock);
325 spin_unlock(&the_lnet.ln_msg_resend_lock);
326 wake_up(&the_lnet.ln_dc_waitq);
328 LIBCFS_FREE(lp, sizeof(*lp));
332 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
333 * that peer_net, detach the peer_net from the peer.
335 * Call with lnet_net_lock/EX held
338 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
340 struct lnet_peer_table *ptable;
341 struct lnet_peer_net *lpn;
342 struct lnet_peer *lp;
345 * Belts and suspenders: gracefully handle teardown of a
346 * partially connected peer_ni.
348 lpn = lpni->lpni_peer_net;
350 list_del_init(&lpni->lpni_peer_nis);
352 * If there are no lpni's left, we detach lpn from
353 * lp_peer_nets, so it cannot be found anymore.
355 if (list_empty(&lpn->lpn_peer_nis))
356 list_del_init(&lpn->lpn_peer_nets);
358 /* Update peer NID count. */
363 * If there are no more peer nets, make the peer unfindable
364 * via the peer_tables.
366 * Otherwise, if the peer is DISCOVERED, tell discovery to
367 * take another look at it. This is a no-op if discovery for
368 * this peer did the detaching.
370 if (list_empty(&lp->lp_peer_nets)) {
371 list_del_init(&lp->lp_peer_list);
372 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
374 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
375 /* Discovery isn't running, nothing to do here. */
376 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
377 lnet_peer_queue_for_discovery(lp);
378 wake_up(&the_lnet.ln_dc_waitq);
380 CDEBUG(D_NET, "peer %s NID %s\n",
381 libcfs_nidstr(&lp->lp_primary_nid),
382 libcfs_nidstr(&lpni->lpni_nid));
385 /* called with lnet_net_lock LNET_LOCK_EX held */
387 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
389 struct lnet_peer_table *ptable = NULL;
391 /* don't remove a peer_ni if it's also a gateway */
392 if (lnet_isrouter(lpni) && !force) {
393 CERROR("Peer NI %s is a gateway. Can not delete it\n",
394 libcfs_nidstr(&lpni->lpni_nid));
398 lnet_peer_remove_from_remote_list(lpni);
400 /* remove peer ni from the hash list. */
401 list_del_init(&lpni->lpni_hashlist);
404 * indicate the peer is being deleted so the monitor thread can
405 * remove it from the recovery queue.
407 spin_lock(&lpni->lpni_lock);
408 lpni->lpni_state |= LNET_PEER_NI_DELETING;
409 spin_unlock(&lpni->lpni_lock);
411 /* decrement the ref count on the peer table */
412 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
415 * The peer_ni can no longer be found with a lookup. But there
416 * can be current users, so keep track of it on the zombie
417 * list until the reference count has gone to zero.
419 * The last reference may be lost in a place where the
420 * lnet_net_lock locks only a single cpt, and that cpt may not
421 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
424 spin_lock(&ptable->pt_zombie_lock);
425 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
426 ptable->pt_zombies++;
427 spin_unlock(&ptable->pt_zombie_lock);
429 /* no need to keep this peer_ni on the hierarchy anymore */
430 lnet_peer_detach_peer_ni_locked(lpni);
432 /* remove hashlist reference on peer_ni */
433 lnet_peer_ni_decref_locked(lpni);
438 void lnet_peer_uninit(void)
440 struct lnet_peer_ni *lpni, *tmp;
442 lnet_net_lock(LNET_LOCK_EX);
444 /* remove all peer_nis from the remote peer and the hash list */
445 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
446 lpni_on_remote_peer_ni_list)
447 lnet_peer_ni_del_locked(lpni, false);
449 lnet_peer_tables_destroy();
451 lnet_net_unlock(LNET_LOCK_EX);
455 lnet_peer_del_locked(struct lnet_peer *peer)
457 struct lnet_peer_ni *lpni = NULL, *lpni2;
460 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&peer->lp_primary_nid));
462 spin_lock(&peer->lp_lock);
463 peer->lp_state |= LNET_PEER_MARK_DELETED;
464 spin_unlock(&peer->lp_lock);
466 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
467 while (lpni != NULL) {
468 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
469 rc = lnet_peer_ni_del_locked(lpni, false);
479 * Discovering this peer is taking too long. Cancel any Ping or Push
480 * that discovery is waiting on by unlinking the relevant MDs. The
481 * lnet_discovery_event_handler() will proceed from here and complete
484 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
486 struct lnet_handle_md ping_mdh;
487 struct lnet_handle_md push_mdh;
489 LNetInvalidateMDHandle(&ping_mdh);
490 LNetInvalidateMDHandle(&push_mdh);
492 spin_lock(&lp->lp_lock);
493 if (lp->lp_state & LNET_PEER_PING_SENT) {
494 ping_mdh = lp->lp_ping_mdh;
495 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
497 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
498 push_mdh = lp->lp_push_mdh;
499 LNetInvalidateMDHandle(&lp->lp_push_mdh);
501 spin_unlock(&lp->lp_lock);
503 if (!LNetMDHandleIsInvalid(ping_mdh))
504 LNetMDUnlink(ping_mdh);
505 if (!LNetMDHandleIsInvalid(push_mdh))
506 LNetMDUnlink(push_mdh);
510 lnet_peer_del(struct lnet_peer *peer)
514 lnet_peer_cancel_discovery(peer);
515 lnet_net_lock(LNET_LOCK_EX);
516 rc = lnet_peer_del_locked(peer);
517 lnet_net_unlock(LNET_LOCK_EX);
523 * Delete a NID from a peer. Call with ln_api_mutex held.
526 * -EPERM: Non-DLC deletion from DLC-configured peer.
527 * -ENOENT: No lnet_peer_ni corresponding to the nid.
528 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
529 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
532 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid4, unsigned int flags)
534 struct lnet_peer_ni *lpni;
535 struct lnet_nid primary_nid = lp->lp_primary_nid;
538 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
540 lnet_nid4_to_nid(nid4, &nid);
541 if (!(flags & LNET_PEER_CONFIGURED)) {
542 if (lp->lp_state & LNET_PEER_CONFIGURED) {
548 lpni = lnet_peer_ni_find_locked(&nid);
553 lnet_peer_ni_decref_locked(lpni);
554 if (lp != lpni->lpni_peer_net->lpn_peer) {
560 * This function only allows deletion of the primary NID if it
563 if (nid_same(&nid, &lp->lp_primary_nid) && lp->lp_nnis != 1 && !force) {
568 lnet_net_lock(LNET_LOCK_EX);
570 if (nid_same(&nid, &lp->lp_primary_nid) && lp->lp_nnis != 1 && force) {
571 struct lnet_peer_ni *lpni2;
572 /* assign the next peer_ni to be the primary */
573 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
575 lp->lp_primary_nid = lpni2->lpni_nid;
577 rc = lnet_peer_ni_del_locked(lpni, force);
579 lnet_net_unlock(LNET_LOCK_EX);
582 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
583 libcfs_nidstr(&primary_nid), libcfs_nidstr(&nid),
590 lnet_peer_table_cleanup_locked(struct lnet_net *net,
591 struct lnet_peer_table *ptable)
594 struct lnet_peer_ni *next;
595 struct lnet_peer_ni *lpni;
596 struct lnet_peer *peer;
598 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
599 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
601 if (net != NULL && net != lpni->lpni_net)
604 peer = lpni->lpni_peer_net->lpn_peer;
605 if (!nid_same(&peer->lp_primary_nid,
607 lnet_peer_ni_del_locked(lpni, false);
611 * Removing the primary NID implies removing
612 * the entire peer. Advance next beyond any
613 * peer_ni that belongs to the same peer.
615 list_for_each_entry_from(next, &ptable->pt_hash[i],
617 if (next->lpni_peer_net->lpn_peer != peer)
620 lnet_peer_del_locked(peer);
626 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
628 wait_var_event_warning(&ptable->pt_zombies,
629 ptable->pt_zombies == 0,
630 "Waiting for %d zombies on peer table\n",
635 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
636 struct lnet_peer_table *ptable)
638 struct lnet_peer_ni *lp;
639 struct lnet_peer_ni *tmp;
640 struct lnet_nid gw_nid;
643 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
644 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
646 if (net != lp->lpni_net)
649 if (!lnet_isrouter(lp))
652 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
654 lnet_net_unlock(LNET_LOCK_EX);
655 lnet_del_route(LNET_NET_ANY, &gw_nid);
656 lnet_net_lock(LNET_LOCK_EX);
662 lnet_peer_tables_cleanup(struct lnet_net *net)
665 struct lnet_peer_table *ptable;
667 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
668 /* If just deleting the peers for a NI, get rid of any routes these
669 * peers are gateways for. */
670 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
671 lnet_net_lock(LNET_LOCK_EX);
672 lnet_peer_table_del_rtrs_locked(net, ptable);
673 lnet_net_unlock(LNET_LOCK_EX);
676 /* Start the cleanup process */
677 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
678 lnet_net_lock(LNET_LOCK_EX);
679 lnet_peer_table_cleanup_locked(net, ptable);
680 lnet_net_unlock(LNET_LOCK_EX);
683 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
684 lnet_peer_ni_finalize_wait(ptable);
687 static struct lnet_peer_ni *
688 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, struct lnet_nid *nid)
690 struct list_head *peers;
691 struct lnet_peer_ni *lp;
693 if (the_lnet.ln_state != LNET_STATE_RUNNING)
696 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
697 list_for_each_entry(lp, peers, lpni_hashlist) {
698 if (nid_same(&lp->lpni_nid, nid)) {
699 lnet_peer_ni_addref_locked(lp);
707 struct lnet_peer_ni *
708 lnet_find_peer_ni_locked(lnet_nid_t nid4)
710 struct lnet_peer_ni *lpni;
711 struct lnet_peer_table *ptable;
715 lnet_nid4_to_nid(nid4, &nid);
717 cpt = lnet_nid_cpt_hash(&nid, LNET_CPT_NUMBER);
719 ptable = the_lnet.ln_peer_tables[cpt];
720 lpni = lnet_get_peer_ni_locked(ptable, &nid);
725 struct lnet_peer_ni *
726 lnet_peer_ni_find_locked(struct lnet_nid *nid)
728 struct lnet_peer_ni *lpni;
729 struct lnet_peer_table *ptable;
732 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
734 ptable = the_lnet.ln_peer_tables[cpt];
735 lpni = lnet_get_peer_ni_locked(ptable, nid);
740 struct lnet_peer_ni *
741 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
743 struct lnet_peer_net *lpn;
744 struct lnet_peer_ni *lpni;
746 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
750 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
751 if (lnet_nid_to_nid4(&lpni->lpni_nid) == nid)
758 struct lnet_peer_ni *
759 lnet_peer_ni_get_locked(struct lnet_peer *lp, struct lnet_nid *nid)
761 struct lnet_peer_net *lpn;
762 struct lnet_peer_ni *lpni;
764 lpn = lnet_peer_get_net_locked(lp, LNET_NID_NET(nid));
768 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
769 if (nid_same(&lpni->lpni_nid, nid))
777 lnet_find_peer4(lnet_nid_t nid)
779 struct lnet_peer_ni *lpni;
780 struct lnet_peer *lp = NULL;
783 cpt = lnet_net_lock_current();
784 lpni = lnet_find_peer_ni_locked(nid);
786 lp = lpni->lpni_peer_net->lpn_peer;
787 lnet_peer_addref_locked(lp);
788 lnet_peer_ni_decref_locked(lpni);
790 lnet_net_unlock(cpt);
796 lnet_find_peer(struct lnet_nid *nid)
798 struct lnet_peer_ni *lpni;
799 struct lnet_peer *lp = NULL;
802 cpt = lnet_net_lock_current();
803 lpni = lnet_peer_ni_find_locked(nid);
805 lp = lpni->lpni_peer_net->lpn_peer;
806 lnet_peer_addref_locked(lp);
807 lnet_peer_ni_decref_locked(lpni);
809 lnet_net_unlock(cpt);
814 struct lnet_peer_net *
815 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
817 struct lnet_peer_net *net;
820 /* no net id provided return the first net */
821 net = list_first_entry_or_null(&lp->lp_peer_nets,
822 struct lnet_peer_net,
828 /* find the net after the one provided */
829 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
830 if (net->lpn_net_id == prev_lpn_id) {
832 * if we reached the end of the list loop to the
835 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
836 return list_first_entry_or_null(&lp->lp_peer_nets,
837 struct lnet_peer_net,
840 return list_next_entry(net, lpn_peer_nets);
847 struct lnet_peer_ni *
848 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
849 struct lnet_peer_net *peer_net,
850 struct lnet_peer_ni *prev)
852 struct lnet_peer_ni *lpni;
853 struct lnet_peer_net *net = peer_net;
857 if (list_empty(&peer->lp_peer_nets))
860 net = list_entry(peer->lp_peer_nets.next,
861 struct lnet_peer_net,
864 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
870 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
872 * if you reached the end of the peer ni list and the peer
873 * net is specified then there are no more peer nis in that
880 * we reached the end of this net ni list. move to the
883 if (prev->lpni_peer_net->lpn_peer_nets.next ==
885 /* no more nets and no more NIs. */
888 /* get the next net */
889 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
890 struct lnet_peer_net,
892 /* get the ni on it */
893 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
899 /* there are more nis left */
900 lpni = list_entry(prev->lpni_peer_nis.next,
901 struct lnet_peer_ni, lpni_peer_nis);
906 /* Call with the ln_api_mutex held */
907 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
909 struct lnet_process_id id;
910 struct lnet_peer_table *ptable;
911 struct lnet_peer *lp;
920 if (the_lnet.ln_state != LNET_STATE_RUNNING)
923 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
926 * Count the number of peers, and return E2BIG if the buffer
927 * is too small. We'll also return the desired size.
930 for (cpt = 0; cpt < lncpt; cpt++) {
931 ptable = the_lnet.ln_peer_tables[cpt];
932 count += ptable->pt_peers;
934 size = count * sizeof(*ids);
939 * Walk the peer lists and copy out the primary nids.
940 * This is safe because the peer lists are only modified
941 * while the ln_api_mutex is held. So we don't need to
942 * hold the lnet_net_lock as well, and can therefore
943 * directly call copy_to_user().
946 memset(&id, 0, sizeof(id));
947 id.pid = LNET_PID_LUSTRE;
949 for (cpt = 0; cpt < lncpt; cpt++) {
950 ptable = the_lnet.ln_peer_tables[cpt];
951 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
952 if (!nid_is_nid4(&lp->lp_primary_nid))
956 id.nid = lnet_nid_to_nid4(&lp->lp_primary_nid);
957 if (copy_to_user(&ids[i], &id, sizeof(id)))
970 * Start pushes to peers that need to be updated for a configuration
971 * change on this node.
974 lnet_push_update_to_peers(int force)
976 struct lnet_peer_table *ptable;
977 struct lnet_peer *lp;
981 lnet_net_lock(LNET_LOCK_EX);
982 if (lnet_peer_discovery_disabled)
984 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
985 for (cpt = 0; cpt < lncpt; cpt++) {
986 ptable = the_lnet.ln_peer_tables[cpt];
987 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
989 spin_lock(&lp->lp_lock);
990 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
991 lp->lp_state |= LNET_PEER_FORCE_PUSH;
992 spin_unlock(&lp->lp_lock);
994 if (lnet_peer_needs_push(lp))
995 lnet_peer_queue_for_discovery(lp);
998 lnet_net_unlock(LNET_LOCK_EX);
999 wake_up(&the_lnet.ln_dc_waitq);
1002 /* find the NID in the preferred gateways for the remote peer
1004 * false: list is not empty and NID is not preferred
1005 * false: list is empty
1006 * true: nid is found in the list
1009 lnet_peer_is_pref_rtr_locked(struct lnet_peer_ni *lpni,
1010 struct lnet_nid *gw_nid)
1012 struct lnet_nid_list *ne;
1014 CDEBUG(D_NET, "%s: rtr pref emtpy: %d\n",
1015 libcfs_nidstr(&lpni->lpni_nid),
1016 list_empty(&lpni->lpni_rtr_pref_nids));
1018 if (list_empty(&lpni->lpni_rtr_pref_nids))
1021 /* iterate through all the preferred NIDs and see if any of them
1022 * matches the provided gw_nid
1024 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
1025 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
1026 libcfs_nidstr(&ne->nl_nid),
1027 libcfs_nidstr(gw_nid));
1028 if (nid_same(&ne->nl_nid, gw_nid))
1036 lnet_peer_clr_pref_rtrs(struct lnet_peer_ni *lpni)
1038 struct list_head zombies;
1039 struct lnet_nid_list *ne;
1040 struct lnet_nid_list *tmp;
1041 int cpt = lpni->lpni_cpt;
1043 INIT_LIST_HEAD(&zombies);
1046 list_splice_init(&lpni->lpni_rtr_pref_nids, &zombies);
1047 lnet_net_unlock(cpt);
1049 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1050 list_del(&ne->nl_list);
1051 LIBCFS_FREE(ne, sizeof(*ne));
1056 lnet_peer_add_pref_rtr(struct lnet_peer_ni *lpni,
1057 struct lnet_nid *gw_nid)
1059 int cpt = lpni->lpni_cpt;
1060 struct lnet_nid_list *ne = NULL;
1062 /* This function is called with api_mutex held. When the api_mutex
1063 * is held the list can not be modified, as it is only modified as
1064 * a result of applying a UDSP and that happens under api_mutex
1067 __must_hold(&the_lnet.ln_api_mutex);
1069 list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
1070 if (nid_same(&ne->nl_nid, gw_nid))
1074 LIBCFS_CPT_ALLOC(ne, lnet_cpt_table(), cpt, sizeof(*ne));
1078 ne->nl_nid = *gw_nid;
1080 /* Lock the cpt to protect against addition and checks in the
1081 * selection algorithm
1084 list_add(&ne->nl_list, &lpni->lpni_rtr_pref_nids);
1085 lnet_net_unlock(cpt);
1091 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
1092 * this is a preferred point-to-point path. Call with lnet_net_lock in
1096 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1098 struct lnet_nid_list *ne;
1100 if (lpni->lpni_pref_nnids == 0)
1102 if (lpni->lpni_pref_nnids == 1)
1103 return nid_same(&lpni->lpni_pref.nid, nid);
1104 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1105 if (nid_same(&ne->nl_nid, nid))
1112 * Set a single ni as preferred, provided no preferred ni is already
1113 * defined. Only to be used for non-multi-rail peer_ni.
1116 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni,
1117 struct lnet_nid *nid)
1123 spin_lock(&lpni->lpni_lock);
1124 if (LNET_NID_IS_ANY(nid)) {
1126 } else if (lpni->lpni_pref_nnids > 0) {
1128 } else if (lpni->lpni_pref_nnids == 0) {
1129 lpni->lpni_pref.nid = *nid;
1130 lpni->lpni_pref_nnids = 1;
1131 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
1133 spin_unlock(&lpni->lpni_lock);
1135 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1136 libcfs_nidstr(&lpni->lpni_nid), libcfs_nidstr(nid), rc);
1141 * Clear the preferred NID from a non-multi-rail peer_ni, provided
1142 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
1145 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
1149 spin_lock(&lpni->lpni_lock);
1150 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
1151 lpni->lpni_pref_nnids = 0;
1152 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1153 } else if (lpni->lpni_pref_nnids == 0) {
1158 spin_unlock(&lpni->lpni_lock);
1160 CDEBUG(D_NET, "peer %s: %d\n",
1161 libcfs_nidstr(&lpni->lpni_nid), rc);
1166 lnet_peer_ni_set_selection_priority(struct lnet_peer_ni *lpni, __u32 priority)
1168 lpni->lpni_sel_priority = priority;
1172 * Clear the preferred NIDs from a non-multi-rail peer.
1175 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
1177 struct lnet_peer_ni *lpni = NULL;
1179 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
1180 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
1184 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1186 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1187 struct lnet_nid_list *ne1 = NULL;
1188 struct lnet_nid_list *ne2 = NULL;
1189 struct lnet_nid *tmp_nid = NULL;
1192 if (LNET_NID_IS_ANY(nid)) {
1197 if (lpni->lpni_pref_nnids == 1 &&
1198 nid_same(&lpni->lpni_pref.nid, nid)) {
1203 /* A non-MR node may have only one preferred NI per peer_ni */
1204 if (lpni->lpni_pref_nnids > 0 &&
1205 !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1210 /* add the new preferred nid to the list of preferred nids */
1211 if (lpni->lpni_pref_nnids != 0) {
1212 size_t alloc_size = sizeof(*ne1);
1214 if (lpni->lpni_pref_nnids == 1) {
1215 tmp_nid = &lpni->lpni_pref.nid;
1216 INIT_LIST_HEAD(&lpni->lpni_pref.nids);
1219 list_for_each_entry(ne1, &lpni->lpni_pref.nids, nl_list) {
1220 if (nid_same(&ne1->nl_nid, nid)) {
1226 LIBCFS_CPT_ALLOC(ne1, lnet_cpt_table(), lpni->lpni_cpt,
1233 /* move the originally stored nid to the list */
1234 if (lpni->lpni_pref_nnids == 1) {
1235 LIBCFS_CPT_ALLOC(ne2, lnet_cpt_table(),
1236 lpni->lpni_cpt, alloc_size);
1241 INIT_LIST_HEAD(&ne2->nl_list);
1242 ne2->nl_nid = *tmp_nid;
1247 lnet_net_lock(LNET_LOCK_EX);
1248 spin_lock(&lpni->lpni_lock);
1249 if (lpni->lpni_pref_nnids == 0) {
1250 lpni->lpni_pref.nid = *nid;
1253 list_add_tail(&ne2->nl_list, &lpni->lpni_pref.nids);
1254 list_add_tail(&ne1->nl_list, &lpni->lpni_pref.nids);
1256 lpni->lpni_pref_nnids++;
1257 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1258 spin_unlock(&lpni->lpni_lock);
1259 lnet_net_unlock(LNET_LOCK_EX);
1262 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1263 spin_lock(&lpni->lpni_lock);
1264 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1265 spin_unlock(&lpni->lpni_lock);
1267 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1268 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nidstr(nid), rc);
1273 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, struct lnet_nid *nid)
1275 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1276 struct lnet_nid_list *ne = NULL;
1279 if (lpni->lpni_pref_nnids == 0) {
1284 if (lpni->lpni_pref_nnids == 1) {
1285 if (!nid_same(&lpni->lpni_pref.nid, nid)) {
1290 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1291 if (nid_same(&ne->nl_nid, nid))
1292 goto remove_nid_entry;
1300 lnet_net_lock(LNET_LOCK_EX);
1301 spin_lock(&lpni->lpni_lock);
1302 if (lpni->lpni_pref_nnids == 1)
1303 lpni->lpni_pref.nid = LNET_ANY_NID;
1305 list_del_init(&ne->nl_list);
1306 if (lpni->lpni_pref_nnids == 2) {
1307 struct lnet_nid_list *ne, *tmp;
1309 list_for_each_entry_safe(ne, tmp,
1310 &lpni->lpni_pref.nids,
1312 lpni->lpni_pref.nid = ne->nl_nid;
1313 list_del_init(&ne->nl_list);
1314 LIBCFS_FREE(ne, sizeof(*ne));
1318 lpni->lpni_pref_nnids--;
1319 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1320 spin_unlock(&lpni->lpni_lock);
1321 lnet_net_unlock(LNET_LOCK_EX);
1324 LIBCFS_FREE(ne, sizeof(*ne));
1326 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1327 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nidstr(nid), rc);
1332 lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni)
1334 struct list_head zombies;
1335 struct lnet_nid_list *ne;
1336 struct lnet_nid_list *tmp;
1338 INIT_LIST_HEAD(&zombies);
1340 lnet_net_lock(LNET_LOCK_EX);
1341 if (lpni->lpni_pref_nnids == 1)
1342 lpni->lpni_pref.nid = LNET_ANY_NID;
1343 else if (lpni->lpni_pref_nnids > 1)
1344 list_splice_init(&lpni->lpni_pref.nids, &zombies);
1345 lpni->lpni_pref_nnids = 0;
1346 lnet_net_unlock(LNET_LOCK_EX);
1348 list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1349 list_del_init(&ne->nl_list);
1350 LIBCFS_FREE(ne, sizeof(*ne));
1355 lnet_peer_primary_nid_locked(struct lnet_nid *nid, struct lnet_nid *result)
1357 struct lnet_peer_ni *lpni;
1360 lpni = lnet_peer_ni_find_locked(nid);
1362 *result = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1363 lnet_peer_ni_decref_locked(lpni);
1368 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1369 __must_hold(&lp->lp_lock)
1371 if (lnet_peer_discovery_disabled)
1374 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1375 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1386 lnet_is_discovery_disabled(struct lnet_peer *lp)
1390 spin_lock(&lp->lp_lock);
1391 rc = lnet_is_discovery_disabled_locked(lp);
1392 spin_unlock(&lp->lp_lock);
1398 LNetAddPeer(lnet_nid_t *nids, __u32 num_nids)
1400 lnet_nid_t pnid = 0;
1404 if (!nids || num_nids < 1)
1407 rc = LNetNIInit(LNET_PID_ANY);
1411 mutex_lock(&the_lnet.ln_api_mutex);
1413 mr = lnet_peer_discovery_disabled == 0;
1416 for (i = 0; i < num_nids; i++) {
1417 if (nids[i] == LNET_NID_LO_0)
1422 rc = lnet_add_peer_ni(pnid, LNET_NID_ANY, mr, true);
1423 } else if (lnet_peer_discovery_disabled) {
1424 rc = lnet_add_peer_ni(nids[i], LNET_NID_ANY, mr, true);
1426 rc = lnet_add_peer_ni(pnid, nids[i], mr, true);
1429 if (rc && rc != -EEXIST)
1434 mutex_unlock(&the_lnet.ln_api_mutex);
1438 return rc == -EEXIST ? 0 : rc;
1440 EXPORT_SYMBOL(LNetAddPeer);
1442 void LNetPrimaryNID(struct lnet_nid *nid)
1444 struct lnet_peer *lp;
1445 struct lnet_peer_ni *lpni;
1446 struct lnet_nid orig;
1450 if (!nid || nid_is_lo0(nid))
1454 cpt = lnet_net_lock_current();
1455 lpni = lnet_peerni_by_nid_locked(nid, NULL, cpt);
1460 lp = lpni->lpni_peer_net->lpn_peer;
1462 /* If discovery is disabled locally then we needn't bother running
1463 * discovery here because discovery will not modify whatever
1464 * primary NID is currently set for this peer. If the specified peer is
1465 * down then this discovery can introduce long delays into the mount
1466 * process, so skip it if it isn't necessary.
1468 while (!lnet_peer_discovery_disabled && !lnet_peer_is_uptodate(lp)) {
1469 spin_lock(&lp->lp_lock);
1470 /* force a full discovery cycle */
1471 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1472 spin_unlock(&lp->lp_lock);
1474 rc = lnet_discover_peer_locked(lpni, cpt, true);
1477 /* The lpni (or lp) for this NID may have changed and our ref is
1478 * the only thing keeping the old one around. Release the ref
1479 * and lookup the lpni again
1481 lnet_peer_ni_decref_locked(lpni);
1482 lpni = lnet_peer_ni_find_locked(nid);
1487 lp = lpni->lpni_peer_net->lpn_peer;
1489 /* If we find that the peer has discovery disabled then we will
1490 * not modify whatever primary NID is currently set for this
1491 * peer. Thus, we can break out of this loop even if the peer
1492 * is not fully up to date.
1494 if (lnet_is_discovery_disabled(lp))
1497 *nid = lp->lp_primary_nid;
1499 lnet_peer_ni_decref_locked(lpni);
1501 lnet_net_unlock(cpt);
1503 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nidstr(&orig),
1504 libcfs_nidstr(nid), rc);
1506 EXPORT_SYMBOL(LNetPrimaryNID);
1508 struct lnet_peer_net *
1509 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1511 struct lnet_peer_net *peer_net;
1512 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1513 if (peer_net->lpn_net_id == net_id)
1520 * Attach a peer_ni to a peer_net and peer. This function assumes
1521 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1522 * may be attached to a different peer, in which case it will be
1523 * properly detached first. The whole operation is done atomically.
1525 * This function consumes the reference on lpni and Always returns 0.
1526 * This is the last function called from functions that do return an
1527 * int, so returning 0 here allows the compiler to do a tail call.
1530 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1531 struct lnet_peer_net *lpn,
1532 struct lnet_peer_ni *lpni,
1535 struct lnet_peer_table *ptable;
1536 bool new_lpn = false;
1539 /* Install the new peer_ni */
1540 lnet_net_lock(LNET_LOCK_EX);
1541 /* Add peer_ni to global peer table hash, if necessary. */
1542 if (list_empty(&lpni->lpni_hashlist)) {
1543 int hash = lnet_nid2peerhash(&lpni->lpni_nid);
1545 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1546 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1547 ptable->pt_version++;
1548 lnet_peer_ni_addref_locked(lpni);
1551 /* Detach the peer_ni from an existing peer, if necessary. */
1552 if (lpni->lpni_peer_net) {
1553 LASSERT(lpni->lpni_peer_net != lpn);
1554 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1555 lnet_peer_detach_peer_ni_locked(lpni);
1556 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1557 lpni->lpni_peer_net = NULL;
1560 /* Add peer_ni to peer_net */
1561 lpni->lpni_peer_net = lpn;
1562 if (nid_same(&lp->lp_primary_nid, &lpni->lpni_nid))
1563 list_add(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1565 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1566 lnet_update_peer_net_healthv(lpni);
1567 lnet_peer_net_addref_locked(lpn);
1569 /* Add peer_net to peer */
1570 if (!lpn->lpn_peer) {
1573 if (nid_same(&lp->lp_primary_nid, &lpni->lpni_nid))
1574 list_add(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1576 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1577 lnet_peer_addref_locked(lp);
1580 /* Add peer to global peer list, if necessary */
1581 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1582 if (list_empty(&lp->lp_peer_list)) {
1583 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1588 /* Update peer state */
1589 spin_lock(&lp->lp_lock);
1590 if (flags & LNET_PEER_CONFIGURED) {
1591 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1592 lp->lp_state |= LNET_PEER_CONFIGURED;
1594 if (flags & LNET_PEER_MULTI_RAIL) {
1595 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1596 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1597 lnet_peer_clr_non_mr_pref_nids(lp);
1600 spin_unlock(&lp->lp_lock);
1606 rc = lnet_udsp_apply_policies_on_lpn(lpn);
1608 CERROR("Failed to apply UDSPs on lpn %s\n",
1609 libcfs_net2str(lpn->lpn_net_id));
1611 rc = lnet_udsp_apply_policies_on_lpni(lpni);
1613 CERROR("Failed to apply UDSPs on lpni %s\n",
1614 libcfs_nidstr(&lpni->lpni_nid));
1616 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1617 libcfs_nidstr(&lp->lp_primary_nid),
1618 libcfs_nidstr(&lpni->lpni_nid), flags);
1619 lnet_peer_ni_decref_locked(lpni);
1620 lnet_net_unlock(LNET_LOCK_EX);
1626 * Create a new peer, with nid as its primary nid.
1628 * Call with the lnet_api_mutex held.
1631 lnet_peer_add(lnet_nid_t nid4, unsigned int flags)
1633 struct lnet_nid nid;
1634 struct lnet_peer *lp;
1635 struct lnet_peer_net *lpn;
1636 struct lnet_peer_ni *lpni;
1639 LASSERT(nid4 != LNET_NID_ANY);
1642 * No need for the lnet_net_lock here, because the
1643 * lnet_api_mutex is held.
1645 lpni = lnet_find_peer_ni_locked(nid4);
1647 /* A peer with this NID already exists. */
1648 lp = lpni->lpni_peer_net->lpn_peer;
1649 lnet_peer_ni_decref_locked(lpni);
1651 * This is an error if the peer was configured and the
1652 * primary NID differs or an attempt is made to change
1653 * the Multi-Rail flag. Otherwise the assumption is
1654 * that an existing peer is being modified.
1656 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1657 if (lnet_nid_to_nid4(&lp->lp_primary_nid) != nid4)
1659 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1662 } else if (!(flags & LNET_PEER_CONFIGURED)) {
1663 if (lnet_nid_to_nid4(&lp->lp_primary_nid) == nid4) {
1668 /* Delete and recreate as a configured peer. */
1669 rc = lnet_peer_del(lp);
1674 /* Create peer, peer_net, and peer_ni. */
1676 lnet_nid4_to_nid(nid4, &nid);
1677 lp = lnet_peer_alloc(&nid);
1680 lpn = lnet_peer_net_alloc(LNET_NID_NET(&nid));
1683 lpni = lnet_peer_ni_alloc(&nid);
1687 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1690 LIBCFS_FREE(lpn, sizeof(*lpn));
1692 LIBCFS_FREE(lp, sizeof(*lp));
1694 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1695 libcfs_nid2str(nid4), flags, rc);
1700 * Add a NID to a peer. Call with ln_api_mutex held.
1703 * -EPERM: Non-DLC addition to a DLC-configured peer.
1704 * -EEXIST: The NID was configured by DLC for a different peer.
1705 * -ENOMEM: Out of memory.
1706 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1707 * non-multi-rail peer.
1710 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid4, unsigned int flags)
1712 struct lnet_peer_net *lpn;
1713 struct lnet_peer_ni *lpni;
1714 struct lnet_nid nid;
1718 LASSERT(nid4 != LNET_NID_ANY);
1720 lnet_nid4_to_nid(nid4, &nid);
1722 /* A configured peer can only be updated through configuration. */
1723 if (!(flags & LNET_PEER_CONFIGURED)) {
1724 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1731 * The MULTI_RAIL flag can be set but not cleared, because
1732 * that would leave the peer struct in an invalid state.
1734 if (flags & LNET_PEER_MULTI_RAIL) {
1735 spin_lock(&lp->lp_lock);
1736 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1737 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1738 lnet_peer_clr_non_mr_pref_nids(lp);
1740 spin_unlock(&lp->lp_lock);
1741 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1746 lpni = lnet_find_peer_ni_locked(nid4);
1749 * A peer_ni already exists. This is only a problem if
1750 * it is not connected to this peer and was configured
1753 if (lpni->lpni_peer_net->lpn_peer == lp)
1755 if (lnet_peer_ni_is_configured(lpni)) {
1759 /* If this is the primary NID, destroy the peer. */
1760 if (lnet_peer_ni_is_primary(lpni)) {
1761 struct lnet_peer *rtr_lp =
1762 lpni->lpni_peer_net->lpn_peer;
1763 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1765 * if we're trying to delete a router it means
1766 * we're moving this peer NI to a new peer so must
1767 * transfer router properties to the new peer
1769 if (rtr_refcount > 0) {
1770 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1771 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1773 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1774 lnet_peer_ni_decref_locked(lpni);
1775 lpni = lnet_peer_ni_alloc(&nid);
1782 lpni = lnet_peer_ni_alloc(&nid);
1790 * Get the peer_net. Check that we're not adding a second
1791 * peer_ni on a peer_net of a non-multi-rail peer.
1793 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid4));
1795 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid4));
1800 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1805 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1808 lnet_peer_ni_decref_locked(lpni);
1810 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1811 libcfs_nidstr(&lp->lp_primary_nid), libcfs_nid2str(nid4),
1817 * Update the primary NID of a peer, if possible.
1819 * Call with the lnet_api_mutex held.
1822 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid,
1825 struct lnet_nid old = lp->lp_primary_nid;
1828 if (lnet_nid_to_nid4(&lp->lp_primary_nid) == nid)
1831 lnet_nid4_to_nid(nid, &lp->lp_primary_nid);
1833 rc = lnet_peer_add_nid(lp, nid, flags);
1835 lp->lp_primary_nid = old;
1839 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1840 libcfs_nidstr(&old), libcfs_nid2str(nid), rc);
1846 * lpni creation initiated due to traffic either sending or receiving.
1847 * Callers must hold ln_api_mutex
1848 * Ref taken on lnet_peer_ni returned by this function
1850 static struct lnet_peer_ni *
1851 lnet_peer_ni_traffic_add(struct lnet_nid *nid, struct lnet_nid *pref)
1852 __must_hold(&the_lnet.ln_api_mutex)
1854 struct lnet_peer *lp = NULL;
1855 struct lnet_peer_net *lpn = NULL;
1856 struct lnet_peer_ni *lpni;
1860 if (LNET_NID_IS_ANY(nid)) {
1865 /* lnet_net_lock is not needed here because ln_api_lock is held */
1866 lpni = lnet_peer_ni_find_locked(nid);
1869 * We must have raced with another thread. Since we
1870 * know next to nothing about a peer_ni created by
1871 * traffic, we just assume everything is ok and
1877 /* Create peer, peer_net, and peer_ni. */
1879 lp = lnet_peer_alloc(nid);
1882 lpn = lnet_peer_net_alloc(LNET_NID_NET(nid));
1885 lpni = lnet_peer_ni_alloc(nid);
1888 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1890 /* lnet_peer_attach_peer_ni() always returns 0 */
1891 rc = lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1893 lnet_peer_ni_addref_locked(lpni);
1898 LIBCFS_FREE(lpn, sizeof(*lpn));
1900 LIBCFS_FREE(lp, sizeof(*lp));
1904 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nidstr(nid), rc);
1909 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1911 * This API handles the following combinations:
1912 * Create a peer with its primary NI if only the prim_nid is provided
1913 * Add a NID to a peer identified by the prim_nid. The peer identified
1914 * by the prim_nid must already exist.
1915 * The peer being created may be non-MR.
1917 * The caller must hold ln_api_mutex. This prevents the peer from
1918 * being created/modified/deleted by a different thread.
1921 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr, bool temp)
1923 struct lnet_peer *lp = NULL;
1924 struct lnet_peer_ni *lpni;
1925 unsigned int flags = 0;
1927 /* The prim_nid must always be specified */
1928 if (prim_nid == LNET_NID_ANY)
1932 flags = LNET_PEER_CONFIGURED;
1935 flags |= LNET_PEER_MULTI_RAIL;
1938 * If nid isn't specified, we must create a new peer with
1939 * prim_nid as its primary nid.
1941 if (nid == LNET_NID_ANY)
1942 return lnet_peer_add(prim_nid, flags);
1944 /* Look up the prim_nid, which must exist. */
1945 lpni = lnet_find_peer_ni_locked(prim_nid);
1948 lnet_peer_ni_decref_locked(lpni);
1949 lp = lpni->lpni_peer_net->lpn_peer;
1951 /* Peer must have been configured. */
1952 if (!temp && !(lp->lp_state & LNET_PEER_CONFIGURED)) {
1953 CDEBUG(D_NET, "peer %s was not configured\n",
1954 libcfs_nid2str(prim_nid));
1958 /* Primary NID must match */
1959 if (lnet_nid_to_nid4(&lp->lp_primary_nid) != prim_nid) {
1960 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1961 libcfs_nid2str(prim_nid),
1962 libcfs_nidstr(&lp->lp_primary_nid));
1966 /* Multi-Rail flag must match. */
1967 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1968 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1969 libcfs_nid2str(prim_nid));
1973 return lnet_peer_add_nid(lp, nid, flags);
1977 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1979 * This API handles the following combinations:
1980 * Delete a NI from a peer if both prim_nid and nid are provided.
1981 * Delete a peer if only prim_nid is provided.
1982 * Delete a peer if its primary nid is provided.
1984 * The caller must hold ln_api_mutex. This prevents the peer from
1985 * being modified/deleted by a different thread.
1988 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1990 struct lnet_peer *lp;
1991 struct lnet_peer_ni *lpni;
1994 if (prim_nid == LNET_NID_ANY)
1997 lpni = lnet_find_peer_ni_locked(prim_nid);
2000 lnet_peer_ni_decref_locked(lpni);
2001 lp = lpni->lpni_peer_net->lpn_peer;
2003 if (prim_nid != lnet_nid_to_nid4(&lp->lp_primary_nid)) {
2004 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
2005 libcfs_nid2str(prim_nid),
2006 libcfs_nidstr(&lp->lp_primary_nid));
2010 lnet_net_lock(LNET_LOCK_EX);
2011 if (lp->lp_rtr_refcount > 0) {
2012 lnet_net_unlock(LNET_LOCK_EX);
2013 CERROR("%s is a router. Can not be deleted\n",
2014 libcfs_nid2str(prim_nid));
2017 lnet_net_unlock(LNET_LOCK_EX);
2019 if (nid == LNET_NID_ANY || nid == lnet_nid_to_nid4(&lp->lp_primary_nid))
2020 return lnet_peer_del(lp);
2022 flags = LNET_PEER_CONFIGURED;
2023 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2024 flags |= LNET_PEER_MULTI_RAIL;
2026 return lnet_peer_del_nid(lp, nid, flags);
2030 lnet_destroy_peer_ni_locked(struct kref *ref)
2032 struct lnet_peer_ni *lpni = container_of(ref, struct lnet_peer_ni,
2034 struct lnet_peer_table *ptable;
2035 struct lnet_peer_net *lpn;
2037 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nidstr(&lpni->lpni_nid));
2039 LASSERT(kref_read(&lpni->lpni_kref) == 0);
2040 LASSERT(list_empty(&lpni->lpni_txq));
2041 LASSERT(lpni->lpni_txqnob == 0);
2042 LASSERT(list_empty(&lpni->lpni_peer_nis));
2043 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
2045 lpn = lpni->lpni_peer_net;
2046 lpni->lpni_peer_net = NULL;
2047 lpni->lpni_net = NULL;
2049 if (!list_empty(&lpni->lpni_hashlist)) {
2050 /* remove the peer ni from the zombie list */
2051 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
2052 spin_lock(&ptable->pt_zombie_lock);
2053 list_del_init(&lpni->lpni_hashlist);
2054 ptable->pt_zombies--;
2055 spin_unlock(&ptable->pt_zombie_lock);
2058 if (lpni->lpni_pref_nnids > 1) {
2059 struct lnet_nid_list *ne, *tmp;
2061 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
2063 list_del_init(&ne->nl_list);
2064 LIBCFS_FREE(ne, sizeof(*ne));
2067 LIBCFS_FREE(lpni, sizeof(*lpni));
2070 lnet_peer_net_decref_locked(lpn);
2073 struct lnet_peer_ni *
2074 lnet_nid2peerni_ex(struct lnet_nid *nid)
2075 __must_hold(&the_lnet.ln_api_mutex)
2077 struct lnet_peer_ni *lpni = NULL;
2079 if (the_lnet.ln_state != LNET_STATE_RUNNING)
2080 return ERR_PTR(-ESHUTDOWN);
2083 * find if a peer_ni already exists.
2084 * If so then just return that.
2086 lpni = lnet_peer_ni_find_locked(nid);
2090 lnet_net_unlock(LNET_LOCK_EX);
2092 lpni = lnet_peer_ni_traffic_add(nid, NULL);
2094 lnet_net_lock(LNET_LOCK_EX);
2100 * Get a peer_ni for the given nid, create it if necessary. Takes a
2101 * hold on the peer_ni.
2103 struct lnet_peer_ni *
2104 lnet_peerni_by_nid_locked(struct lnet_nid *nid,
2105 struct lnet_nid *pref, int cpt)
2107 struct lnet_peer_ni *lpni = NULL;
2109 if (the_lnet.ln_state != LNET_STATE_RUNNING)
2110 return ERR_PTR(-ESHUTDOWN);
2113 * find if a peer_ni already exists.
2114 * If so then just return that.
2116 lpni = lnet_peer_ni_find_locked(nid);
2122 * use the lnet_api_mutex to serialize the creation of the peer_ni
2123 * and the creation/deletion of the local ni/net. When a local ni is
2124 * created, if there exists a set of peer_nis on that network,
2125 * they need to be traversed and updated. When a local NI is
2126 * deleted, which could result in a network being deleted, then
2127 * all peer nis on that network need to be removed as well.
2129 * Creation through traffic should also be serialized with
2130 * creation through DLC.
2132 lnet_net_unlock(cpt);
2133 mutex_lock(&the_lnet.ln_api_mutex);
2135 * the_lnet.ln_state is only modified under the ln_api_lock, so a single
2136 * check here is sufficent.
2138 if (the_lnet.ln_state == LNET_STATE_RUNNING)
2139 lpni = lnet_peer_ni_traffic_add(nid, pref);
2141 mutex_unlock(&the_lnet.ln_api_mutex);
2144 /* Lock has been dropped, check again for shutdown. */
2145 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2146 if (!IS_ERR_OR_NULL(lpni))
2147 lnet_peer_ni_decref_locked(lpni);
2148 lpni = ERR_PTR(-ESHUTDOWN);
2154 struct lnet_peer_ni *
2155 lnet_nid2peerni_locked(lnet_nid_t nid4, lnet_nid_t pref4, int cpt)
2157 struct lnet_nid nid, pref;
2159 lnet_nid4_to_nid(nid4, &nid);
2160 lnet_nid4_to_nid(pref4, &pref);
2161 if (pref4 == LNET_NID_ANY)
2162 return lnet_peerni_by_nid_locked(&nid, NULL, cpt);
2164 return lnet_peerni_by_nid_locked(&nid, &pref, cpt);
2168 lnet_peer_gw_discovery(struct lnet_peer *lp)
2172 spin_lock(&lp->lp_lock);
2173 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
2175 spin_unlock(&lp->lp_lock);
2181 lnet_peer_is_uptodate(struct lnet_peer *lp)
2185 spin_lock(&lp->lp_lock);
2186 rc = lnet_peer_is_uptodate_locked(lp);
2187 spin_unlock(&lp->lp_lock);
2192 * Is a peer uptodate from the point of view of discovery?
2194 * If it is currently being processed, obviously not.
2195 * A forced Ping or Push is also handled by the discovery thread.
2197 * Otherwise look at whether the peer needs rediscovering.
2200 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
2201 __must_hold(&lp->lp_lock)
2205 if (lp->lp_state & (LNET_PEER_DISCOVERING |
2206 LNET_PEER_FORCE_PING |
2207 LNET_PEER_FORCE_PUSH)) {
2209 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
2211 } else if (lnet_peer_needs_push(lp)) {
2213 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
2214 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
2225 /* Add the message to the peer's lp_dc_pendq and queue the peer for discovery */
2227 lnet_peer_queue_message(struct lnet_peer *lp, struct lnet_msg *msg)
2229 /* The discovery thread holds net_lock/EX and lp_lock when it splices
2230 * the lp_dc_pendq onto a local list for resending. Thus, we do the same
2231 * when adding to the list and queuing the peer to ensure that we do not
2232 * strand any messages on the lp_dc_pendq. This scheme ensures the
2233 * message will be resent even if the peer is already being discovered.
2234 * Therefore we needn't check the return value of
2235 * lnet_peer_queue_for_discovery(lp).
2237 lnet_net_lock(LNET_LOCK_EX);
2238 spin_lock(&lp->lp_lock);
2239 list_add_tail(&msg->msg_list, &lp->lp_dc_pendq);
2240 spin_unlock(&lp->lp_lock);
2241 lnet_peer_queue_for_discovery(lp);
2242 lnet_net_unlock(LNET_LOCK_EX);
2246 * Queue a peer for the attention of the discovery thread. Call with
2247 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
2248 * -EALREADY if the peer was already queued.
2250 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
2254 spin_lock(&lp->lp_lock);
2255 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2256 lp->lp_state |= LNET_PEER_DISCOVERING;
2257 spin_unlock(&lp->lp_lock);
2258 if (list_empty(&lp->lp_dc_list)) {
2259 lnet_peer_addref_locked(lp);
2260 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2261 wake_up(&the_lnet.ln_dc_waitq);
2267 CDEBUG(D_NET, "Queue peer %s: %d\n",
2268 libcfs_nidstr(&lp->lp_primary_nid), rc);
2274 * Discovery of a peer is complete. Wake all waiters on the peer.
2275 * Call with lnet_net_lock/EX held.
2277 static void lnet_peer_discovery_complete(struct lnet_peer *lp, int dc_error)
2279 struct lnet_msg *msg, *tmp;
2281 LIST_HEAD(pending_msgs);
2283 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
2284 libcfs_nidstr(&lp->lp_primary_nid));
2286 list_del_init(&lp->lp_dc_list);
2287 spin_lock(&lp->lp_lock);
2289 lp->lp_dc_error = dc_error;
2290 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2291 lp->lp_state |= LNET_PEER_REDISCOVER;
2293 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
2294 spin_unlock(&lp->lp_lock);
2295 wake_up(&lp->lp_dc_waitq);
2297 if (lp->lp_rtr_refcount > 0)
2298 lnet_router_discovery_complete(lp);
2300 lnet_net_unlock(LNET_LOCK_EX);
2302 /* iterate through all pending messages and send them again */
2303 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
2304 list_del_init(&msg->msg_list);
2306 lnet_finalize(msg, dc_error);
2310 CDEBUG(D_NET, "sending pending message %s to target %s\n",
2311 lnet_msgtyp2str(msg->msg_type),
2312 libcfs_idstr(&msg->msg_target));
2313 rc = lnet_send(&msg->msg_src_nid_param, msg,
2314 &msg->msg_rtr_nid_param);
2316 CNETERR("Error sending %s to %s: %d\n",
2317 lnet_msgtyp2str(msg->msg_type),
2318 libcfs_idstr(&msg->msg_target), rc);
2319 lnet_finalize(msg, rc);
2322 lnet_net_lock(LNET_LOCK_EX);
2323 lnet_peer_decref_locked(lp);
2327 * Handle inbound push.
2328 * Like any event handler, called with lnet_res_lock/CPT held.
2330 void lnet_peer_push_event(struct lnet_event *ev)
2332 struct lnet_ping_buffer *pbuf;
2333 struct lnet_peer *lp;
2335 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
2337 /* lnet_find_peer() adds a refcount */
2338 lp = lnet_find_peer(&ev->source.nid);
2340 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
2341 libcfs_nidstr(&ev->initiator.nid),
2342 libcfs_nidstr(&ev->source.nid));
2343 pbuf->pb_needs_post = true;
2347 /* Ensure peer state remains consistent while we modify it. */
2348 spin_lock(&lp->lp_lock);
2351 * If some kind of error happened the contents of the message
2352 * cannot be used. Clear the NIDS_UPTODATE and set the
2353 * FORCE_PING flag to trigger a ping.
2356 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2357 lp->lp_state |= LNET_PEER_FORCE_PING;
2358 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2360 libcfs_nidstr(&lp->lp_primary_nid),
2361 libcfs_nidstr(&ev->source.nid));
2366 * A push with invalid or corrupted info. Clear the UPTODATE
2367 * flag to trigger a ping.
2369 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2370 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2371 lp->lp_state |= LNET_PEER_FORCE_PING;
2372 CDEBUG(D_NET, "Corrupted Push from %s\n",
2373 libcfs_nidstr(&lp->lp_primary_nid));
2378 * Make sure we'll allocate the correct size ping buffer when
2381 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2382 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2385 * A non-Multi-Rail peer is not supposed to be capable of
2388 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2389 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2390 libcfs_nidstr(&lp->lp_primary_nid));
2395 * The peer may have discovery disabled at its end. Set
2396 * NO_DISCOVERY as appropriate.
2398 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2399 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2400 libcfs_nidstr(&lp->lp_primary_nid));
2402 * Mark the peer for deletion if we already know about it
2403 * and it's going from discovery set to no discovery set
2405 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2406 LNET_PEER_DISCOVERING)) &&
2407 lp->lp_state & LNET_PEER_DISCOVERED) {
2408 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2409 libcfs_nidstr(&lp->lp_primary_nid),
2411 lp->lp_state |= LNET_PEER_MARK_DELETION;
2413 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2414 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2415 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2416 libcfs_nidstr(&lp->lp_primary_nid));
2417 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2421 * Update the MULTI_RAIL flag based on the push. If the peer
2422 * was configured with DLC then the setting should match what
2424 * NB: We verified above that the MR feature bit is set in pi_features
2426 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2427 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2428 libcfs_nidstr(&lp->lp_primary_nid), lp);
2429 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2430 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2431 libcfs_nidstr(&lp->lp_primary_nid));
2432 } else if (lnet_peer_discovery_disabled) {
2433 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2434 libcfs_nidstr(&lp->lp_primary_nid), lp);
2435 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2436 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2437 libcfs_nidstr(&lp->lp_primary_nid), lp);
2439 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2440 libcfs_nidstr(&lp->lp_primary_nid), lp);
2441 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2442 lnet_peer_clr_non_mr_pref_nids(lp);
2446 * Check for truncation of the Put message. Clear the
2447 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2448 * and tell discovery to allocate a bigger buffer.
2450 if (ev->mlength < ev->rlength) {
2451 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2452 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2453 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2454 lp->lp_state |= LNET_PEER_FORCE_PING;
2455 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2456 libcfs_nidstr(&lp->lp_primary_nid),
2457 pbuf->pb_info.pi_nnis);
2461 /* always assume new data */
2462 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2463 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2466 * If there is data present that hasn't been processed yet,
2467 * we'll replace it if the Put contained newer data and it
2468 * fits. We're racing with a Ping or earlier Push in this
2471 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2472 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2473 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2474 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2475 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2476 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2477 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2478 libcfs_nidstr(&lp->lp_primary_nid),
2479 LNET_PING_BUFFER_SEQNO(pbuf),
2480 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2486 * Allocate a buffer to copy the data. On a failure we drop
2487 * the Push and set FORCE_PING to force the discovery
2488 * thread to fix the problem by pinging the peer.
2490 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2492 lp->lp_state |= LNET_PEER_FORCE_PING;
2493 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2494 libcfs_nidstr(&lp->lp_primary_nid),
2495 LNET_PING_BUFFER_SEQNO(pbuf));
2500 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2501 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2502 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2503 CDEBUG(D_NET, "Received Push %s %u\n",
2504 libcfs_nidstr(&lp->lp_primary_nid),
2505 LNET_PING_BUFFER_SEQNO(pbuf));
2508 /* We've processed this buffer. It can be reposted */
2509 pbuf->pb_needs_post = true;
2512 * Queue the peer for discovery if not done, force it on the request
2513 * queue and wake the discovery thread if the peer was already queued,
2514 * because its status changed.
2516 spin_unlock(&lp->lp_lock);
2517 lnet_net_lock(LNET_LOCK_EX);
2518 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2519 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2520 wake_up(&the_lnet.ln_dc_waitq);
2522 /* Drop refcount from lookup */
2523 lnet_peer_decref_locked(lp);
2524 lnet_net_unlock(LNET_LOCK_EX);
2528 * Clear the discovery error state, unless we're already discovering
2529 * this peer, in which case the error is current.
2531 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2533 spin_lock(&lp->lp_lock);
2534 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2535 lp->lp_dc_error = 0;
2536 spin_unlock(&lp->lp_lock);
2540 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2541 * dropped/retaken within this function. An lnet_peer_ni is passed in
2542 * because discovery could tear down an lnet_peer.
2545 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2548 struct lnet_peer *lp;
2553 lnet_net_unlock(cpt);
2554 lnet_net_lock(LNET_LOCK_EX);
2555 lp = lpni->lpni_peer_net->lpn_peer;
2556 lnet_peer_clear_discovery_error(lp);
2559 * We're willing to be interrupted. The lpni can become a
2560 * zombie if we race with DLC, so we must check for that.
2563 /* Keep lp alive when the lnet_net_lock is unlocked */
2564 lnet_peer_addref_locked(lp);
2565 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2566 if (signal_pending(current))
2568 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2571 * Don't repeat discovery if discovery is disabled. This is
2572 * done to ensure we can use discovery as a standard ping as
2573 * well for backwards compatibility with routers which do not
2574 * have discovery or have discovery disabled
2576 if (lnet_is_discovery_disabled(lp) && count > 0)
2578 if (lp->lp_dc_error)
2580 if (lnet_peer_is_uptodate(lp))
2582 if (lp->lp_state & LNET_PEER_MARK_DELETED)
2584 lnet_peer_queue_for_discovery(lp);
2586 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2589 * If caller requested a non-blocking operation then
2590 * return immediately. Once discovery is complete any
2591 * pending messages that were stopped due to discovery
2592 * will be transmitted.
2597 lnet_net_unlock(LNET_LOCK_EX);
2599 finish_wait(&lp->lp_dc_waitq, &wait);
2600 lnet_net_lock(LNET_LOCK_EX);
2601 lnet_peer_decref_locked(lp);
2602 /* Peer may have changed */
2603 lp = lpni->lpni_peer_net->lpn_peer;
2605 finish_wait(&lp->lp_dc_waitq, &wait);
2607 lnet_net_unlock(LNET_LOCK_EX);
2609 lnet_peer_decref_locked(lp);
2611 * The peer may have changed, so re-check and rediscover if that turns
2612 * out to have been the case. The reference count on lp ensured that
2613 * even if it was unlinked from lpni the memory could not be recycled.
2614 * Thus the check below is sufficient to determine whether the peer
2615 * changed. If the peer changed, then lp must not be dereferenced.
2617 if (lp != lpni->lpni_peer_net->lpn_peer)
2620 if (signal_pending(current))
2622 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2624 else if (lp->lp_dc_error)
2625 rc = lp->lp_dc_error;
2627 CDEBUG(D_NET, "non-blocking discovery\n");
2628 else if (!lnet_peer_is_uptodate(lp) &&
2629 !(lnet_is_discovery_disabled(lp) ||
2630 (lp->lp_state & LNET_PEER_MARK_DELETED)))
2633 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2634 (lp ? libcfs_nidstr(&lp->lp_primary_nid) : "(none)"),
2635 libcfs_nidstr(&lpni->lpni_nid), rc,
2636 (!block) ? "pending discovery" : "discovery complete");
2641 /* Handle an incoming ack for a push. */
2643 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2645 struct lnet_ping_buffer *pbuf;
2647 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2648 spin_lock(&lp->lp_lock);
2649 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2650 lp->lp_push_error = ev->status;
2652 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2654 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2655 spin_unlock(&lp->lp_lock);
2657 CDEBUG(D_NET, "peer %s ev->status %d\n",
2658 libcfs_nidstr(&lp->lp_primary_nid), ev->status);
2661 /* Handle a Reply message. This is the reply to a Ping message. */
2663 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2665 struct lnet_ping_buffer *pbuf;
2668 spin_lock(&lp->lp_lock);
2670 lp->lp_disc_src_nid = ev->target.nid;
2671 lp->lp_disc_dst_nid = ev->source.nid;
2674 * If some kind of error happened the contents of message
2675 * cannot be used. Set PING_FAILED to trigger a retry.
2678 lp->lp_state |= LNET_PEER_PING_FAILED;
2679 lp->lp_ping_error = ev->status;
2680 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2682 libcfs_nidstr(&lp->lp_primary_nid),
2683 libcfs_nidstr(&ev->source.nid));
2687 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2688 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2689 lnet_swap_pinginfo(pbuf);
2692 * A reply with invalid or corrupted info. Set PING_FAILED to
2695 rc = lnet_ping_info_validate(&pbuf->pb_info);
2697 lp->lp_state |= LNET_PEER_PING_FAILED;
2698 lp->lp_ping_error = 0;
2699 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2700 libcfs_nidstr(&lp->lp_primary_nid), rc);
2705 * The peer may have discovery disabled at its end. Set
2706 * NO_DISCOVERY as appropriate.
2708 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) ||
2709 lnet_peer_discovery_disabled) {
2710 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2711 libcfs_nidstr(&lp->lp_primary_nid));
2713 /* Detect whether this peer has toggled discovery from on to
2714 * off and whether we can delete and re-create the peer. Peers
2715 * that were manually configured cannot be deleted by discovery.
2716 * We need to delete this peer and re-create it if the peer was
2717 * not configured manually, is currently considered DD capable,
2719 * 1. We've already discovered the peer (the peer has toggled
2720 * the discovery feature from on to off), or
2721 * 2. The peer is considered MR, but it was not user configured
2722 * (this was a "temporary" peer created via the kernel APIs
2723 * that we're discovering for the first time)
2725 if (!(lp->lp_state & (LNET_PEER_CONFIGURED |
2726 LNET_PEER_NO_DISCOVERY)) &&
2727 (lp->lp_state & (LNET_PEER_DISCOVERED |
2728 LNET_PEER_MULTI_RAIL))) {
2729 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2730 libcfs_nidstr(&lp->lp_primary_nid),
2732 lp->lp_state |= LNET_PEER_MARK_DELETION;
2734 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2736 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2737 libcfs_nidstr(&lp->lp_primary_nid));
2738 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2742 * Update the MULTI_RAIL flag based on the reply. If the peer
2743 * was configured with DLC then the setting should match what
2746 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2747 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2748 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2749 libcfs_nidstr(&lp->lp_primary_nid), lp);
2750 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2751 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2752 libcfs_nidstr(&lp->lp_primary_nid));
2753 } else if (lnet_peer_discovery_disabled) {
2755 "peer %s(%p) not MR: DD disabled locally\n",
2756 libcfs_nidstr(&lp->lp_primary_nid), lp);
2757 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2759 "peer %s(%p) not MR: DD disabled remotely\n",
2760 libcfs_nidstr(&lp->lp_primary_nid), lp);
2762 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2763 libcfs_nidstr(&lp->lp_primary_nid), lp);
2764 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2765 lnet_peer_clr_non_mr_pref_nids(lp);
2767 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2768 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2769 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2770 libcfs_nidstr(&lp->lp_primary_nid));
2772 CERROR("Multi-Rail state vanished from %s\n",
2773 libcfs_nidstr(&lp->lp_primary_nid));
2774 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2779 * Make sure we'll allocate the correct size ping buffer when
2782 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2783 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2786 * Check for truncation of the Reply. Clear PING_SENT and set
2787 * PING_FAILED to trigger a retry.
2789 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2790 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2791 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2792 lp->lp_state |= LNET_PEER_PING_FAILED;
2793 lp->lp_ping_error = 0;
2794 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2795 libcfs_nidstr(&lp->lp_primary_nid),
2796 pbuf->pb_info.pi_nnis);
2801 * Check the sequence numbers in the reply. These are only
2802 * available if the reply came from a Multi-Rail peer.
2804 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2805 pbuf->pb_info.pi_nnis > 1 &&
2806 lnet_nid_to_nid4(&lp->lp_primary_nid) ==
2807 pbuf->pb_info.pi_ni[1].ns_nid) {
2808 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2809 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2810 libcfs_nidstr(&lp->lp_primary_nid),
2811 LNET_PING_BUFFER_SEQNO(pbuf),
2814 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2817 /* We're happy with the state of the data in the buffer. */
2818 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2819 libcfs_nidstr(&lp->lp_primary_nid), lp->lp_peer_seqno,
2821 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2822 lnet_ping_buffer_decref(lp->lp_data);
2824 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2825 lnet_ping_buffer_addref(pbuf);
2828 lp->lp_state &= ~LNET_PEER_PING_SENT;
2829 spin_unlock(&lp->lp_lock);
2831 lnet_net_lock(LNET_LOCK_EX);
2833 * If this peer is a gateway, call the routing callback to
2834 * handle the ping reply
2836 if (lp->lp_rtr_refcount > 0)
2837 lnet_router_discovery_ping_reply(lp);
2838 lnet_net_unlock(LNET_LOCK_EX);
2842 * Send event handling. Only matters for error cases, where we clean
2843 * up state on the peer and peer_ni that would otherwise be updated in
2844 * the REPLY event handler for a successful Ping, and the ACK event
2845 * handler for a successful Push.
2848 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2855 spin_lock(&lp->lp_lock);
2856 if (ev->msg_type == LNET_MSG_GET) {
2857 lp->lp_state &= ~LNET_PEER_PING_SENT;
2858 lp->lp_state |= LNET_PEER_PING_FAILED;
2859 lp->lp_ping_error = ev->status;
2860 } else { /* ev->msg_type == LNET_MSG_PUT */
2861 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2862 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2863 lp->lp_push_error = ev->status;
2865 spin_unlock(&lp->lp_lock);
2866 rc = LNET_REDISCOVER_PEER;
2868 CDEBUG(D_NET, "%s Send to %s: %d\n",
2869 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2870 libcfs_nidstr(&ev->target.nid), rc);
2875 * Unlink event handling. This event is only seen if a call to
2876 * LNetMDUnlink() caused the event to be unlinked. If this call was
2877 * made after the event was set up in LNetGet() or LNetPut() then we
2878 * assume the Ping or Push timed out.
2881 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2883 spin_lock(&lp->lp_lock);
2884 /* We've passed through LNetGet() */
2885 if (lp->lp_state & LNET_PEER_PING_SENT) {
2886 lp->lp_state &= ~LNET_PEER_PING_SENT;
2887 lp->lp_state |= LNET_PEER_PING_FAILED;
2888 lp->lp_ping_error = -ETIMEDOUT;
2889 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2890 libcfs_nidstr(&lp->lp_primary_nid));
2892 /* We've passed through LNetPut() */
2893 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2894 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2895 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2896 lp->lp_push_error = -ETIMEDOUT;
2897 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2898 libcfs_nidstr(&lp->lp_primary_nid));
2900 spin_unlock(&lp->lp_lock);
2904 * Event handler for the discovery EQ.
2906 * Called with lnet_res_lock(cpt) held. The cpt is the
2907 * lnet_cpt_of_cookie() of the md handle cookie.
2909 static void lnet_discovery_event_handler(struct lnet_event *event)
2911 struct lnet_peer *lp = event->md_user_ptr;
2912 struct lnet_ping_buffer *pbuf;
2915 /* discovery needs to take another look */
2916 rc = LNET_REDISCOVER_PEER;
2918 CDEBUG(D_NET, "Received event: %d\n", event->type);
2920 switch (event->type) {
2921 case LNET_EVENT_ACK:
2922 lnet_discovery_event_ack(lp, event);
2924 case LNET_EVENT_REPLY:
2925 lnet_discovery_event_reply(lp, event);
2927 case LNET_EVENT_SEND:
2928 /* Only send failure triggers a retry. */
2929 rc = lnet_discovery_event_send(lp, event);
2931 case LNET_EVENT_UNLINK:
2932 /* LNetMDUnlink() was called */
2933 lnet_discovery_event_unlink(lp, event);
2936 /* Invalid events. */
2939 lnet_net_lock(LNET_LOCK_EX);
2940 if (event->unlinked) {
2941 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2942 lnet_ping_buffer_decref(pbuf);
2943 lnet_peer_decref_locked(lp);
2946 /* put peer back at end of request queue, if discovery not already
2948 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp) &&
2949 lnet_peer_queue_for_discovery(lp)) {
2950 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2951 wake_up(&the_lnet.ln_dc_waitq);
2953 lnet_net_unlock(LNET_LOCK_EX);
2957 * Build a peer from incoming data.
2959 * The NIDs in the incoming data are supposed to be structured as follows:
2962 * - other NIDs in same net
2963 * - NIDs in second net
2964 * - NIDs in third net
2966 * This due to the way the list of NIDs in the data is created.
2968 * Note that this function will mark the peer uptodate unless an
2969 * ENOMEM is encontered. All other errors are due to a conflict
2970 * between the DLC configuration and what discovery sees. We treat DLC
2971 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2972 * peer from becoming stuck in discovery.
2974 static int lnet_peer_merge_data(struct lnet_peer *lp,
2975 struct lnet_ping_buffer *pbuf)
2977 struct lnet_peer_net *lpn;
2978 struct lnet_peer_ni *lpni;
2979 lnet_nid_t *curnis = NULL;
2980 struct lnet_ni_status *addnis = NULL;
2981 lnet_nid_t *delnis = NULL;
2991 flags = LNET_PEER_DISCOVERED;
2992 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2993 flags |= LNET_PEER_MULTI_RAIL;
2996 * Cache the routing feature for the peer; whether it is enabled
2997 * for disabled as reported by the remote peer.
2999 spin_lock(&lp->lp_lock);
3000 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
3001 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
3003 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
3004 spin_unlock(&lp->lp_lock);
3006 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
3007 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
3008 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
3009 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
3010 if (!curnis || !addnis || !delnis) {
3018 /* Construct the list of NIDs present in peer. */
3020 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
3021 curnis[ncurnis++] = lnet_nid_to_nid4(&lpni->lpni_nid);
3024 * Check for NIDs in pbuf not present in curnis[].
3025 * The loop starts at 1 to skip the loopback NID.
3027 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
3028 for (j = 0; j < ncurnis; j++)
3029 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
3032 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
3035 * Check for NIDs in curnis[] not present in pbuf.
3036 * The nested loop starts at 1 to skip the loopback NID.
3038 * But never add the loopback NID to delnis[]: if it is
3039 * present in curnis[] then this peer is for this node.
3041 for (i = 0; i < ncurnis; i++) {
3042 if (curnis[i] == LNET_NID_LO_0)
3044 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
3045 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
3047 * update the information we cache for the
3048 * peer with the latest information we
3051 lpni = lnet_find_peer_ni_locked(curnis[i]);
3053 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
3054 lnet_peer_ni_decref_locked(lpni);
3059 if (j == pbuf->pb_info.pi_nnis)
3060 delnis[ndelnis++] = curnis[i];
3064 * If we get here and the discovery is disabled then we don't want
3065 * to add or delete any NIs. We just updated the ones we have some
3066 * information on, and call it a day
3069 if (lnet_is_discovery_disabled(lp))
3072 for (i = 0; i < naddnis; i++) {
3073 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
3075 CERROR("Error adding NID %s to peer %s: %d\n",
3076 libcfs_nid2str(addnis[i].ns_nid),
3077 libcfs_nidstr(&lp->lp_primary_nid), rc);
3081 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
3083 lpni->lpni_ns_status = addnis[i].ns_status;
3084 lnet_peer_ni_decref_locked(lpni);
3088 for (i = 0; i < ndelnis; i++) {
3090 * for routers it's okay to delete the primary_nid because
3091 * the upper layers don't really rely on it. So if we're
3092 * being told that the router changed its primary_nid
3093 * then it's okay to delete it.
3095 if (lp->lp_rtr_refcount > 0)
3096 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
3097 rc = lnet_peer_del_nid(lp, delnis[i], flags);
3099 CERROR("Error deleting NID %s from peer %s: %d\n",
3100 libcfs_nid2str(delnis[i]),
3101 libcfs_nidstr(&lp->lp_primary_nid), rc);
3107 /* The peer net for the primary NID should be the first entry in the
3108 * peer's lp_peer_nets list, and the peer NI for the primary NID should
3109 * be the first entry in its peer net's lpn_peer_nis list.
3111 lpni = lnet_find_peer_ni_locked(pbuf->pb_info.pi_ni[1].ns_nid);
3113 CERROR("Internal error: Failed to lookup peer NI for primary NID: %s\n",
3114 libcfs_nid2str(pbuf->pb_info.pi_ni[1].ns_nid));
3118 lnet_peer_ni_decref_locked(lpni);
3120 lpn = lpni->lpni_peer_net;
3121 if (lpn->lpn_peer_nets.prev != &lp->lp_peer_nets)
3122 list_move(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
3124 if (lpni->lpni_peer_nis.prev != &lpni->lpni_peer_net->lpn_peer_nis)
3125 list_move(&lpni->lpni_peer_nis,
3126 &lpni->lpni_peer_net->lpn_peer_nis);
3129 * Errors other than -ENOMEM are due to peers having been
3130 * configured with DLC. Ignore these because DLC overrides
3135 CFS_FREE_PTR_ARRAY(curnis, nnis);
3136 CFS_FREE_PTR_ARRAY(addnis, nnis);
3137 CFS_FREE_PTR_ARRAY(delnis, nnis);
3138 lnet_ping_buffer_decref(pbuf);
3139 CDEBUG(D_NET, "peer %s (%p): %d\n",
3140 libcfs_nidstr(&lp->lp_primary_nid), lp, rc);
3143 spin_lock(&lp->lp_lock);
3144 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3145 lp->lp_state |= LNET_PEER_FORCE_PING;
3146 spin_unlock(&lp->lp_lock);
3152 * The data in pbuf says lp is its primary peer, but the data was
3153 * received by a different peer. Try to update lp with the data.
3156 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
3158 struct lnet_handle_md mdh;
3160 /* Queue lp for discovery, and force it on the request queue. */
3161 lnet_net_lock(LNET_LOCK_EX);
3162 if (lnet_peer_queue_for_discovery(lp))
3163 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
3164 lnet_net_unlock(LNET_LOCK_EX);
3166 LNetInvalidateMDHandle(&mdh);
3169 * Decide whether we can move the peer to the DATA_PRESENT state.
3171 * We replace stale data for a multi-rail peer, repair PING_FAILED
3172 * status, and preempt FORCE_PING.
3174 * If after that we have DATA_PRESENT, we merge it into this peer.
3176 spin_lock(&lp->lp_lock);
3177 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
3178 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
3179 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
3180 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3181 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3182 lnet_ping_buffer_decref(pbuf);
3187 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3188 lnet_ping_buffer_decref(lp->lp_data);
3190 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3192 if (lp->lp_state & LNET_PEER_PING_FAILED) {
3193 mdh = lp->lp_ping_mdh;
3194 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3195 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3196 lp->lp_ping_error = 0;
3198 if (lp->lp_state & LNET_PEER_FORCE_PING)
3199 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3200 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3201 spin_unlock(&lp->lp_lock);
3203 if (!LNetMDHandleIsInvalid(mdh))
3207 return lnet_peer_merge_data(lp, pbuf);
3209 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3213 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
3217 for (i = 0; i < pinfo->pi_nnis; i++) {
3218 if (pinfo->pi_ni[i].ns_nid == nid)
3225 /* Delete a peer that has been marked for deletion. NB: when this peer was added
3226 * to the discovery queue a reference was taken that will prevent the peer from
3227 * actually being freed by this function. After this function exits the
3228 * discovery thread should call lnet_peer_discovery_complete() which will
3229 * drop that reference as well as wake any waiters that may also be holding a
3232 static int lnet_peer_deletion(struct lnet_peer *lp)
3233 __must_hold(&lp->lp_lock)
3235 struct list_head rlist;
3236 struct lnet_route *route, *tmp;
3237 int sensitivity = lp->lp_health_sensitivity;
3240 INIT_LIST_HEAD(&rlist);
3242 lp->lp_state &= ~(LNET_PEER_DISCOVERING | LNET_PEER_FORCE_PING |
3243 LNET_PEER_FORCE_PUSH);
3244 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3245 libcfs_nidstr(&lp->lp_primary_nid), lp, lp->lp_state);
3247 /* no-op if lnet_peer_del() has already been called on this peer */
3248 if (lp->lp_state & LNET_PEER_MARK_DELETED)
3251 spin_unlock(&lp->lp_lock);
3253 mutex_lock(&the_lnet.ln_api_mutex);
3254 if (the_lnet.ln_state != LNET_STATE_RUNNING ||
3255 the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
3256 mutex_unlock(&the_lnet.ln_api_mutex);
3257 spin_lock(&lp->lp_lock);
3261 lnet_net_lock(LNET_LOCK_EX);
3262 /* remove the peer from the discovery work
3263 * queue if it's on there in preparation
3266 if (!list_empty(&lp->lp_dc_list))
3267 list_del_init(&lp->lp_dc_list);
3268 list_for_each_entry_safe(route, tmp,
3271 lnet_move_route(route, NULL, &rlist);
3272 lnet_net_unlock(LNET_LOCK_EX);
3274 /* lnet_peer_del() deletes all the peer NIs owned by this peer */
3275 rc = lnet_peer_del(lp);
3277 CNETERR("Internal error: Unable to delete peer %s rc %d\n",
3278 libcfs_nidstr(&lp->lp_primary_nid), rc);
3280 list_for_each_entry_safe(route, tmp,
3282 /* re-add these routes */
3283 lnet_add_route(route->lr_net,
3288 LIBCFS_FREE(route, sizeof(*route));
3291 mutex_unlock(&the_lnet.ln_api_mutex);
3293 spin_lock(&lp->lp_lock);
3299 * Update a peer using the data received.
3301 static int lnet_peer_data_present(struct lnet_peer *lp)
3302 __must_hold(&lp->lp_lock)
3304 struct lnet_ping_buffer *pbuf;
3305 struct lnet_peer_ni *lpni;
3306 lnet_nid_t nid = LNET_NID_ANY;
3312 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3313 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3314 spin_unlock(&lp->lp_lock);
3317 * Modifications of peer structures are done while holding the
3318 * ln_api_mutex. A global lock is required because we may be
3319 * modifying multiple peer structures, and a mutex greatly
3320 * simplifies memory management.
3322 * The actual changes to the data structures must also protect
3323 * against concurrent lookups, for which the lnet_net_lock in
3324 * LNET_LOCK_EX mode is used.
3326 mutex_lock(&the_lnet.ln_api_mutex);
3327 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3333 * If this peer is not on the peer list then it is being torn
3334 * down, and our reference count may be all that is keeping it
3335 * alive. Don't do any work on it.
3337 if (list_empty(&lp->lp_peer_list)) {
3338 lnet_ping_buffer_decref(pbuf);
3342 flags = LNET_PEER_DISCOVERED;
3343 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
3344 flags |= LNET_PEER_MULTI_RAIL;
3347 * Check whether the primary NID in the message matches the
3348 * primary NID of the peer. If it does, update the peer, if
3349 * it it does not, check whether there is already a peer with
3350 * that primary NID. If no such peer exists, try to update
3351 * the primary NID of the current peer (allowed if it was
3352 * created due to message traffic) and complete the update.
3353 * If the peer did exist, hand off the data to it.
3355 * The peer for the loopback interface is a special case: this
3356 * is the peer for the local node, and we want to set its
3357 * primary NID to the correct value here. Moreover, this peer
3358 * can show up with only the loopback NID in the ping buffer.
3360 if (pbuf->pb_info.pi_nnis <= 1) {
3361 lnet_ping_buffer_decref(pbuf);
3364 nid = pbuf->pb_info.pi_ni[1].ns_nid;
3365 if (nid_is_lo0(&lp->lp_primary_nid)) {
3366 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3368 lnet_ping_buffer_decref(pbuf);
3370 rc = lnet_peer_merge_data(lp, pbuf);
3372 * if the primary nid of the peer is present in the ping info returned
3373 * from the peer, but it's not the local primary peer we have
3374 * cached and discovery is disabled, then we don't want to update
3375 * our local peer info, by adding or removing NIDs, we just want
3376 * to update the status of the nids that we currently have
3377 * recorded in that peer.
3379 } else if (lnet_nid_to_nid4(&lp->lp_primary_nid) == nid ||
3380 (lnet_is_nid_in_ping_info(lnet_nid_to_nid4(&lp->lp_primary_nid),
3382 lnet_is_discovery_disabled(lp))) {
3383 rc = lnet_peer_merge_data(lp, pbuf);
3385 lpni = lnet_find_peer_ni_locked(nid);
3386 if (!lpni || lp == lpni->lpni_peer_net->lpn_peer) {
3387 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3389 CERROR("Primary NID error %s versus %s: %d\n",
3390 libcfs_nidstr(&lp->lp_primary_nid),
3391 libcfs_nid2str(nid), rc);
3392 lnet_ping_buffer_decref(pbuf);
3394 rc = lnet_peer_merge_data(lp, pbuf);
3397 lnet_peer_ni_decref_locked(lpni);
3399 struct lnet_peer *new_lp;
3400 new_lp = lpni->lpni_peer_net->lpn_peer;
3402 * if lp has discovery/MR enabled that means new_lp
3403 * should have discovery/MR enabled as well, since
3404 * it's the same peer, which we're about to merge
3406 spin_lock(&lp->lp_lock);
3407 spin_lock(&new_lp->lp_lock);
3408 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
3409 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
3410 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
3411 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
3412 /* If we're processing a ping reply then we may be
3413 * about to send a push to the peer that we ping'd.
3414 * Since the ping reply that we're processing was
3415 * received by lp, we need to set the discovery source
3416 * NID for new_lp to the NID stored in lp.
3418 if (!LNET_NID_IS_ANY(&lp->lp_disc_src_nid)) {
3419 new_lp->lp_disc_src_nid = lp->lp_disc_src_nid;
3420 new_lp->lp_disc_dst_nid = lp->lp_disc_dst_nid;
3422 spin_unlock(&new_lp->lp_lock);
3423 spin_unlock(&lp->lp_lock);
3425 rc = lnet_peer_set_primary_data(new_lp, pbuf);
3426 lnet_consolidate_routes_locked(lp, new_lp);
3427 lnet_peer_ni_decref_locked(lpni);
3431 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n",
3432 libcfs_nidstr(&lp->lp_primary_nid), lp, rc,
3434 mutex_unlock(&the_lnet.ln_api_mutex);
3436 spin_lock(&lp->lp_lock);
3437 /* Tell discovery to re-check the peer immediately. */
3439 rc = LNET_REDISCOVER_PEER;
3444 * A ping failed. Clear the PING_FAILED state and set the
3445 * FORCE_PING state, to ensure a retry even if discovery is
3446 * disabled. This avoids being left with incorrect state.
3448 static int lnet_peer_ping_failed(struct lnet_peer *lp)
3449 __must_hold(&lp->lp_lock)
3451 struct lnet_handle_md mdh;
3454 mdh = lp->lp_ping_mdh;
3455 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3456 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3457 lp->lp_state |= LNET_PEER_FORCE_PING;
3458 rc = lp->lp_ping_error;
3459 lp->lp_ping_error = 0;
3460 spin_unlock(&lp->lp_lock);
3462 if (!LNetMDHandleIsInvalid(mdh))
3465 CDEBUG(D_NET, "peer %s:%d\n",
3466 libcfs_nidstr(&lp->lp_primary_nid), rc);
3468 spin_lock(&lp->lp_lock);
3469 return rc ? rc : LNET_REDISCOVER_PEER;
3472 /* Active side of ping. */
3473 static int lnet_peer_send_ping(struct lnet_peer *lp)
3474 __must_hold(&lp->lp_lock)
3480 lp->lp_state |= LNET_PEER_PING_SENT;
3481 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3482 spin_unlock(&lp->lp_lock);
3484 cpt = lnet_net_lock_current();
3485 /* Refcount for MD. */
3486 lnet_peer_addref_locked(lp);
3487 lnet_net_unlock(cpt);
3489 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3491 rc = lnet_send_ping(&lp->lp_primary_nid, &lp->lp_ping_mdh, nnis, lp,
3492 the_lnet.ln_dc_handler, false);
3495 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3496 * refcount on the peer, otherwise LNetMDUnlink will be called
3497 * which will eventually do that.
3501 lnet_peer_decref_locked(lp);
3502 lnet_net_unlock(cpt);
3503 rc = -rc; /* change the rc to negative value */
3505 } else if (rc < 0) {
3509 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3511 spin_lock(&lp->lp_lock);
3515 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nidstr(&lp->lp_primary_nid), rc);
3517 * The errors that get us here are considered hard errors and
3518 * cause Discovery to terminate. So we clear PING_SENT, but do
3519 * not set either PING_FAILED or FORCE_PING. In fact we need
3520 * to clear PING_FAILED, because the unlink event handler will
3521 * have set it if we called LNetMDUnlink() above.
3523 spin_lock(&lp->lp_lock);
3524 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3529 * This function exists because you cannot call LNetMDUnlink() from an
3532 static int lnet_peer_push_failed(struct lnet_peer *lp)
3533 __must_hold(&lp->lp_lock)
3535 struct lnet_handle_md mdh;
3538 mdh = lp->lp_push_mdh;
3539 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3540 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3541 rc = lp->lp_push_error;
3542 lp->lp_push_error = 0;
3543 spin_unlock(&lp->lp_lock);
3545 if (!LNetMDHandleIsInvalid(mdh))
3548 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3549 spin_lock(&lp->lp_lock);
3550 return rc ? rc : LNET_REDISCOVER_PEER;
3554 * Mark the peer as discovered.
3556 static int lnet_peer_discovered(struct lnet_peer *lp)
3557 __must_hold(&lp->lp_lock)
3559 lp->lp_state |= LNET_PEER_DISCOVERED;
3560 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3561 LNET_PEER_REDISCOVER);
3563 lp->lp_dc_error = 0;
3565 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3570 /* Active side of push. */
3571 static int lnet_peer_send_push(struct lnet_peer *lp)
3572 __must_hold(&lp->lp_lock)
3574 struct lnet_ping_buffer *pbuf;
3575 struct lnet_processid id;
3580 /* Don't push to a non-multi-rail peer. */
3581 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3582 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3583 /* if peer's NIDs are uptodate then peer is discovered */
3584 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3585 rc = lnet_peer_discovered(lp);
3592 lp->lp_state |= LNET_PEER_PUSH_SENT;
3593 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3594 spin_unlock(&lp->lp_lock);
3596 cpt = lnet_net_lock_current();
3597 pbuf = the_lnet.ln_ping_target;
3598 lnet_ping_buffer_addref(pbuf);
3599 lnet_net_unlock(cpt);
3601 /* Push source MD */
3602 md.start = &pbuf->pb_info;
3603 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3604 md.threshold = 2; /* Put/Ack */
3606 md.options = LNET_MD_TRACK_RESPONSE;
3607 md.handler = the_lnet.ln_dc_handler;
3610 rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3612 lnet_ping_buffer_decref(pbuf);
3613 CERROR("Can't bind push source MD: %d\n", rc);
3617 cpt = lnet_net_lock_current();
3618 /* Refcount for MD. */
3619 lnet_peer_addref_locked(lp);
3620 id.pid = LNET_PID_LUSTRE;
3621 if (!LNET_NID_IS_ANY(&lp->lp_disc_dst_nid))
3622 id.nid = lp->lp_disc_dst_nid;
3624 id.nid = lp->lp_primary_nid;
3625 lnet_net_unlock(cpt);
3627 rc = LNetPut(&lp->lp_disc_src_nid, lp->lp_push_mdh,
3628 LNET_ACK_REQ, &id, LNET_RESERVED_PORTAL,
3629 LNET_PROTO_PING_MATCHBITS, 0, 0);
3632 * reset the discovery nid. There is no need to restrict sending
3633 * from that source, if we call lnet_push_update_to_peers(). It'll
3634 * get set to a specific NID, if we initiate discovery from the
3637 lp->lp_disc_src_nid = LNET_ANY_NID;
3638 lp->lp_disc_dst_nid = LNET_ANY_NID;
3643 CDEBUG(D_NET, "peer %s\n", libcfs_nidstr(&lp->lp_primary_nid));
3645 spin_lock(&lp->lp_lock);
3649 LNetMDUnlink(lp->lp_push_mdh);
3650 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3652 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nidstr(&lp->lp_primary_nid),
3655 * The errors that get us here are considered hard errors and
3656 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3657 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3658 * because the unlink event handler will have set it if we
3659 * called LNetMDUnlink() above.
3661 spin_lock(&lp->lp_lock);
3662 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3667 * Wait for work to be queued or some other change that must be
3668 * attended to. Returns non-zero if the discovery thread should shut
3671 static int lnet_peer_discovery_wait_for_work(void)
3678 cpt = lnet_net_lock_current();
3680 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3681 TASK_INTERRUPTIBLE);
3682 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3684 if (lnet_push_target_resize_needed() ||
3685 the_lnet.ln_push_target->pb_needs_post)
3687 if (!list_empty(&the_lnet.ln_dc_request))
3689 if (!list_empty(&the_lnet.ln_msg_resend))
3691 lnet_net_unlock(cpt);
3694 * wakeup max every second to check if there are peers that
3695 * have been stuck on the working queue for greater than
3698 schedule_timeout(cfs_time_seconds(1));
3699 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3700 cpt = lnet_net_lock_current();
3702 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3704 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3707 lnet_net_unlock(cpt);
3709 CDEBUG(D_NET, "woken: %d\n", rc);
3715 * Messages that were pending on a destroyed peer will be put on a global
3716 * resend list. The message resend list will be checked by
3717 * the discovery thread when it wakes up, and will resend messages. These
3718 * messages can still be sendable in the case the lpni which was the initial
3719 * cause of the message re-queue was transfered to another peer.
3721 * It is possible that LNet could be shutdown while we're iterating
3722 * through the list. lnet_shudown_lndnets() will attempt to access the
3723 * resend list, but will have to wait until the spinlock is released, by
3724 * which time there shouldn't be any more messages on the resend list.
3725 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3726 * for the messages so they can be released. The other case is that
3727 * lnet_shudown_lndnets() can finalize all the messages before this
3728 * function can visit the resend list, in which case this function will be
3731 static void lnet_resend_msgs(void)
3733 struct lnet_msg *msg, *tmp;
3737 spin_lock(&the_lnet.ln_msg_resend_lock);
3738 list_splice(&the_lnet.ln_msg_resend, &resend);
3739 spin_unlock(&the_lnet.ln_msg_resend_lock);
3741 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3742 list_del_init(&msg->msg_list);
3743 rc = lnet_send(&msg->msg_src_nid_param, msg,
3744 &msg->msg_rtr_nid_param);
3746 CNETERR("Error sending %s to %s: %d\n",
3747 lnet_msgtyp2str(msg->msg_type),
3748 libcfs_idstr(&msg->msg_target), rc);
3749 lnet_finalize(msg, rc);
3754 /* The discovery thread. */
3755 static int lnet_peer_discovery(void *arg)
3757 struct lnet_peer *lp;
3760 wait_for_completion(&the_lnet.ln_started);
3762 CDEBUG(D_NET, "started\n");
3765 if (lnet_peer_discovery_wait_for_work())
3768 if (lnet_push_target_resize_needed())
3769 lnet_push_target_resize();
3770 else if (the_lnet.ln_push_target->pb_needs_post)
3771 lnet_push_target_post(the_lnet.ln_push_target,
3772 &the_lnet.ln_push_target_md);
3776 lnet_net_lock(LNET_LOCK_EX);
3777 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3778 lnet_net_unlock(LNET_LOCK_EX);
3783 * Process all incoming discovery work requests. When
3784 * discovery must wait on a peer to change state, it
3785 * is added to the tail of the ln_dc_working queue. A
3786 * timestamp keeps track of when the peer was added,
3787 * so we can time out discovery requests that take too
3790 while (!list_empty(&the_lnet.ln_dc_request)) {
3791 lp = list_first_entry(&the_lnet.ln_dc_request,
3792 struct lnet_peer, lp_dc_list);
3793 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3795 * set the time the peer was put on the dc_working
3796 * queue. It shouldn't remain on the queue
3797 * forever, in case the GET message (for ping)
3798 * doesn't get a REPLY or the PUT message (for
3799 * push) doesn't get an ACK.
3801 lp->lp_last_queued = ktime_get_real_seconds();
3802 lnet_net_unlock(LNET_LOCK_EX);
3804 if (lnet_push_target_resize_needed())
3805 lnet_push_target_resize();
3806 else if (the_lnet.ln_push_target->pb_needs_post)
3807 lnet_push_target_post(the_lnet.ln_push_target,
3808 &the_lnet.ln_push_target_md);
3811 * Select an action depending on the state of
3812 * the peer and whether discovery is disabled.
3813 * The check whether discovery is disabled is
3814 * done after the code that handles processing
3815 * for arrived data, cleanup for failures, and
3816 * forcing a Ping or Push.
3818 spin_lock(&lp->lp_lock);
3819 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3820 libcfs_nidstr(&lp->lp_primary_nid), lp,
3822 if (lp->lp_state & (LNET_PEER_MARK_DELETION |
3823 LNET_PEER_MARK_DELETED))
3824 rc = lnet_peer_deletion(lp);
3825 else if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3826 rc = lnet_peer_data_present(lp);
3827 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3828 rc = lnet_peer_ping_failed(lp);
3829 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3830 rc = lnet_peer_push_failed(lp);
3831 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3832 rc = lnet_peer_send_ping(lp);
3833 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3834 rc = lnet_peer_send_push(lp);
3835 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3836 rc = lnet_peer_send_ping(lp);
3837 else if (lnet_peer_needs_push(lp))
3838 rc = lnet_peer_send_push(lp);
3840 rc = lnet_peer_discovered(lp);
3841 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3842 libcfs_nidstr(&lp->lp_primary_nid), lp,
3845 if (rc == LNET_REDISCOVER_PEER) {
3846 spin_unlock(&lp->lp_lock);
3847 lnet_net_lock(LNET_LOCK_EX);
3848 list_move(&lp->lp_dc_list,
3849 &the_lnet.ln_dc_request);
3851 !(lp->lp_state & LNET_PEER_DISCOVERING)) {
3852 spin_unlock(&lp->lp_lock);
3853 lnet_net_lock(LNET_LOCK_EX);
3854 lnet_peer_discovery_complete(lp, rc);
3856 spin_unlock(&lp->lp_lock);
3857 lnet_net_lock(LNET_LOCK_EX);
3860 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3865 lnet_net_unlock(LNET_LOCK_EX);
3868 CDEBUG(D_NET, "stopping\n");
3870 * Clean up before telling lnet_peer_discovery_stop() that
3871 * we're done. Use wake_up() below to somewhat reduce the
3872 * size of the thundering herd if there are multiple threads
3873 * waiting on discovery of a single peer.
3876 /* Queue cleanup 1: stop all pending pings and pushes. */
3877 lnet_net_lock(LNET_LOCK_EX);
3878 while (!list_empty(&the_lnet.ln_dc_working)) {
3879 lp = list_first_entry(&the_lnet.ln_dc_working,
3880 struct lnet_peer, lp_dc_list);
3881 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3882 lnet_net_unlock(LNET_LOCK_EX);
3883 lnet_peer_cancel_discovery(lp);
3884 lnet_net_lock(LNET_LOCK_EX);
3886 lnet_net_unlock(LNET_LOCK_EX);
3888 /* Queue cleanup 2: wait for the expired queue to clear. */
3889 while (!list_empty(&the_lnet.ln_dc_expired))
3890 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3892 /* Queue cleanup 3: clear the request queue. */
3893 lnet_net_lock(LNET_LOCK_EX);
3894 while (!list_empty(&the_lnet.ln_dc_request)) {
3895 lp = list_first_entry(&the_lnet.ln_dc_request,
3896 struct lnet_peer, lp_dc_list);
3897 lnet_peer_discovery_complete(lp, -ESHUTDOWN);
3899 lnet_net_unlock(LNET_LOCK_EX);
3901 lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3902 the_lnet.ln_dc_handler = NULL;
3904 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3905 wake_up(&the_lnet.ln_dc_waitq);
3907 CDEBUG(D_NET, "stopped\n");
3912 /* ln_api_mutex is held on entry. */
3913 int lnet_peer_discovery_start(void)
3915 struct task_struct *task;
3918 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3921 the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3922 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3923 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3926 CERROR("Can't start peer discovery thread: %d\n", rc);
3928 the_lnet.ln_dc_handler = NULL;
3930 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3933 CDEBUG(D_NET, "discovery start: %d\n", rc);