4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <linux/sched.h>
38 #ifdef HAVE_SCHED_HEADERS
39 #include <linux/sched/signal.h>
41 #include <linux/uaccess.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER (1)
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
54 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
55 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
56 lnet_peer_ni_decref_locked(lpni);
61 lnet_peer_net_added(struct lnet_net *net)
63 struct lnet_peer_ni *lpni, *tmp;
65 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66 lpni_on_remote_peer_ni_list) {
68 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
71 spin_lock(&lpni->lpni_lock);
72 lpni->lpni_txcredits =
73 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
74 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
75 lpni->lpni_rtrcredits =
76 lnet_peer_buffer_credits(lpni->lpni_net);
77 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
78 spin_unlock(&lpni->lpni_lock);
80 lnet_peer_remove_from_remote_list(lpni);
86 lnet_peer_tables_destroy(void)
88 struct lnet_peer_table *ptable;
89 struct list_head *hash;
93 if (!the_lnet.ln_peer_tables)
96 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
97 hash = ptable->pt_hash;
98 if (!hash) /* not intialized */
101 LASSERT(list_empty(&ptable->pt_zombie_list));
103 ptable->pt_hash = NULL;
104 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
105 LASSERT(list_empty(&hash[j]));
107 CFS_FREE_PTR_ARRAY(hash, LNET_PEER_HASH_SIZE);
110 cfs_percpt_free(the_lnet.ln_peer_tables);
111 the_lnet.ln_peer_tables = NULL;
115 lnet_peer_tables_create(void)
117 struct lnet_peer_table *ptable;
118 struct list_head *hash;
122 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
124 if (the_lnet.ln_peer_tables == NULL) {
125 CERROR("Failed to allocate cpu-partition peer tables\n");
129 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
130 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
131 LNET_PEER_HASH_SIZE * sizeof(*hash));
133 CERROR("Failed to create peer hash table\n");
134 lnet_peer_tables_destroy();
138 spin_lock_init(&ptable->pt_zombie_lock);
139 INIT_LIST_HEAD(&ptable->pt_zombie_list);
141 INIT_LIST_HEAD(&ptable->pt_peer_list);
143 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
144 INIT_LIST_HEAD(&hash[j]);
145 ptable->pt_hash = hash; /* sign of initialization */
151 static struct lnet_peer_ni *
152 lnet_peer_ni_alloc(lnet_nid_t nid)
154 struct lnet_peer_ni *lpni;
155 struct lnet_net *net;
158 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
160 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
164 INIT_LIST_HEAD(&lpni->lpni_txq);
165 INIT_LIST_HEAD(&lpni->lpni_hashlist);
166 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
167 INIT_LIST_HEAD(&lpni->lpni_recovery);
168 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
169 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
170 atomic_set(&lpni->lpni_refcount, 1);
172 spin_lock_init(&lpni->lpni_lock);
174 if (lnet_peers_start_down())
175 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
177 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
178 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
179 lpni->lpni_nid = nid;
180 lpni->lpni_cpt = cpt;
181 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
183 net = lnet_get_net_locked(LNET_NIDNET(nid));
184 lpni->lpni_net = net;
186 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
187 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
188 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
189 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
192 * This peer_ni is not on a local network, so we
193 * cannot add the credits here. In case the net is
194 * added later, add the peer_ni to the remote peer ni
195 * list so it can be easily found and revisited.
197 /* FIXME: per-net implementation instead? */
198 lnet_peer_ni_addref_locked(lpni);
199 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
200 &the_lnet.ln_remote_peer_ni_list);
203 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
208 static struct lnet_peer_net *
209 lnet_peer_net_alloc(__u32 net_id)
211 struct lnet_peer_net *lpn;
213 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
217 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
218 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
219 lpn->lpn_net_id = net_id;
221 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
227 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
229 struct lnet_peer *lp;
231 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
233 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
234 LASSERT(list_empty(&lpn->lpn_peer_nis));
235 LASSERT(list_empty(&lpn->lpn_peer_nets));
237 lpn->lpn_peer = NULL;
238 LIBCFS_FREE(lpn, sizeof(*lpn));
240 lnet_peer_decref_locked(lp);
243 static struct lnet_peer *
244 lnet_peer_alloc(lnet_nid_t nid)
246 struct lnet_peer *lp;
248 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
252 INIT_LIST_HEAD(&lp->lp_rtrq);
253 INIT_LIST_HEAD(&lp->lp_routes);
254 INIT_LIST_HEAD(&lp->lp_peer_list);
255 INIT_LIST_HEAD(&lp->lp_peer_nets);
256 INIT_LIST_HEAD(&lp->lp_dc_list);
257 INIT_LIST_HEAD(&lp->lp_dc_pendq);
258 INIT_LIST_HEAD(&lp->lp_rtr_list);
259 init_waitqueue_head(&lp->lp_dc_waitq);
260 spin_lock_init(&lp->lp_lock);
261 lp->lp_primary_nid = nid;
262 lp->lp_disc_src_nid = LNET_NID_ANY;
263 if (lnet_peers_start_down())
264 lp->lp_alive = false;
269 * all peers created on a router should have health on
270 * if it's not already on.
272 if (the_lnet.ln_routing && !lnet_health_sensitivity)
273 lp->lp_health_sensitivity = 1;
276 * Turn off discovery for loopback peer. If you're creating a peer
277 * for the loopback interface then that was initiated when we
278 * attempted to send a message over the loopback. There is no need
279 * to ever use a different interface when sending messages to
282 if (nid == LNET_NID_LO_0)
283 lp->lp_state = LNET_PEER_NO_DISCOVERY;
284 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
286 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
292 lnet_destroy_peer_locked(struct lnet_peer *lp)
294 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
296 LASSERT(atomic_read(&lp->lp_refcount) == 0);
297 LASSERT(lp->lp_rtr_refcount == 0);
298 LASSERT(list_empty(&lp->lp_peer_nets));
299 LASSERT(list_empty(&lp->lp_peer_list));
300 LASSERT(list_empty(&lp->lp_dc_list));
303 lnet_ping_buffer_decref(lp->lp_data);
306 * if there are messages still on the pending queue, then make
307 * sure to queue them on the ln_msg_resend list so they can be
308 * resent at a later point if the discovery thread is still
310 * If the discovery thread has stopped, then the wakeup will be a
311 * no-op, and it is expected the lnet_shutdown_lndnets() will
312 * eventually be called, which will traverse this list and
313 * finalize the messages on the list.
314 * We can not resend them now because we're holding the cpt lock.
315 * Releasing the lock can cause an inconsistent state
317 spin_lock(&the_lnet.ln_msg_resend_lock);
318 spin_lock(&lp->lp_lock);
319 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
320 spin_unlock(&lp->lp_lock);
321 spin_unlock(&the_lnet.ln_msg_resend_lock);
322 wake_up(&the_lnet.ln_dc_waitq);
324 LIBCFS_FREE(lp, sizeof(*lp));
328 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
329 * that peer_net, detach the peer_net from the peer.
331 * Call with lnet_net_lock/EX held
334 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
336 struct lnet_peer_table *ptable;
337 struct lnet_peer_net *lpn;
338 struct lnet_peer *lp;
341 * Belts and suspenders: gracefully handle teardown of a
342 * partially connected peer_ni.
344 lpn = lpni->lpni_peer_net;
346 list_del_init(&lpni->lpni_peer_nis);
348 * If there are no lpni's left, we detach lpn from
349 * lp_peer_nets, so it cannot be found anymore.
351 if (list_empty(&lpn->lpn_peer_nis))
352 list_del_init(&lpn->lpn_peer_nets);
354 /* Update peer NID count. */
359 * If there are no more peer nets, make the peer unfindable
360 * via the peer_tables.
362 * Otherwise, if the peer is DISCOVERED, tell discovery to
363 * take another look at it. This is a no-op if discovery for
364 * this peer did the detaching.
366 if (list_empty(&lp->lp_peer_nets)) {
367 list_del_init(&lp->lp_peer_list);
368 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
370 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
371 /* Discovery isn't running, nothing to do here. */
372 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
373 lnet_peer_queue_for_discovery(lp);
374 wake_up(&the_lnet.ln_dc_waitq);
376 CDEBUG(D_NET, "peer %s NID %s\n",
377 libcfs_nid2str(lp->lp_primary_nid),
378 libcfs_nid2str(lpni->lpni_nid));
381 /* called with lnet_net_lock LNET_LOCK_EX held */
383 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
385 struct lnet_peer_table *ptable = NULL;
387 /* don't remove a peer_ni if it's also a gateway */
388 if (lnet_isrouter(lpni) && !force) {
389 CERROR("Peer NI %s is a gateway. Can not delete it\n",
390 libcfs_nid2str(lpni->lpni_nid));
394 lnet_peer_remove_from_remote_list(lpni);
396 /* remove peer ni from the hash list. */
397 list_del_init(&lpni->lpni_hashlist);
400 * indicate the peer is being deleted so the monitor thread can
401 * remove it from the recovery queue.
403 spin_lock(&lpni->lpni_lock);
404 lpni->lpni_state |= LNET_PEER_NI_DELETING;
405 spin_unlock(&lpni->lpni_lock);
407 /* decrement the ref count on the peer table */
408 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
411 * The peer_ni can no longer be found with a lookup. But there
412 * can be current users, so keep track of it on the zombie
413 * list until the reference count has gone to zero.
415 * The last reference may be lost in a place where the
416 * lnet_net_lock locks only a single cpt, and that cpt may not
417 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
420 spin_lock(&ptable->pt_zombie_lock);
421 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
422 ptable->pt_zombies++;
423 spin_unlock(&ptable->pt_zombie_lock);
425 /* no need to keep this peer_ni on the hierarchy anymore */
426 lnet_peer_detach_peer_ni_locked(lpni);
428 /* remove hashlist reference on peer_ni */
429 lnet_peer_ni_decref_locked(lpni);
434 void lnet_peer_uninit(void)
436 struct lnet_peer_ni *lpni, *tmp;
438 lnet_net_lock(LNET_LOCK_EX);
440 /* remove all peer_nis from the remote peer and the hash list */
441 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
442 lpni_on_remote_peer_ni_list)
443 lnet_peer_ni_del_locked(lpni, false);
445 lnet_peer_tables_destroy();
447 lnet_net_unlock(LNET_LOCK_EX);
451 lnet_peer_del_locked(struct lnet_peer *peer)
453 struct lnet_peer_ni *lpni = NULL, *lpni2;
456 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
458 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
459 while (lpni != NULL) {
460 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
461 rc = lnet_peer_ni_del_locked(lpni, false);
471 lnet_peer_del(struct lnet_peer *peer)
473 lnet_net_lock(LNET_LOCK_EX);
474 lnet_peer_del_locked(peer);
475 lnet_net_unlock(LNET_LOCK_EX);
481 * Delete a NID from a peer. Call with ln_api_mutex held.
484 * -EPERM: Non-DLC deletion from DLC-configured peer.
485 * -ENOENT: No lnet_peer_ni corresponding to the nid.
486 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
487 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
490 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
492 struct lnet_peer_ni *lpni;
493 lnet_nid_t primary_nid = lp->lp_primary_nid;
495 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
497 if (!(flags & LNET_PEER_CONFIGURED)) {
498 if (lp->lp_state & LNET_PEER_CONFIGURED) {
503 lpni = lnet_find_peer_ni_locked(nid);
508 lnet_peer_ni_decref_locked(lpni);
509 if (lp != lpni->lpni_peer_net->lpn_peer) {
515 * This function only allows deletion of the primary NID if it
518 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
523 lnet_net_lock(LNET_LOCK_EX);
525 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
526 struct lnet_peer_ni *lpni2;
527 /* assign the next peer_ni to be the primary */
528 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
530 lp->lp_primary_nid = lpni2->lpni_nid;
532 rc = lnet_peer_ni_del_locked(lpni, force);
534 lnet_net_unlock(LNET_LOCK_EX);
537 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
538 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
544 lnet_peer_table_cleanup_locked(struct lnet_net *net,
545 struct lnet_peer_table *ptable)
548 struct lnet_peer_ni *next;
549 struct lnet_peer_ni *lpni;
550 struct lnet_peer *peer;
552 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
553 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
555 if (net != NULL && net != lpni->lpni_net)
558 peer = lpni->lpni_peer_net->lpn_peer;
559 if (peer->lp_primary_nid != lpni->lpni_nid) {
560 lnet_peer_ni_del_locked(lpni, false);
564 * Removing the primary NID implies removing
565 * the entire peer. Advance next beyond any
566 * peer_ni that belongs to the same peer.
568 list_for_each_entry_from(next, &ptable->pt_hash[i],
570 if (next->lpni_peer_net->lpn_peer != peer)
573 lnet_peer_del_locked(peer);
579 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
581 wait_var_event_warning(&ptable->pt_zombies,
582 ptable->pt_zombies == 0,
583 "Waiting for %d zombies on peer table\n",
588 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
589 struct lnet_peer_table *ptable)
591 struct lnet_peer_ni *lp;
592 struct lnet_peer_ni *tmp;
596 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
597 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
599 if (net != lp->lpni_net)
602 if (!lnet_isrouter(lp))
605 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
607 lnet_net_unlock(LNET_LOCK_EX);
608 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
609 lnet_net_lock(LNET_LOCK_EX);
615 lnet_peer_tables_cleanup(struct lnet_net *net)
618 struct lnet_peer_table *ptable;
620 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
621 /* If just deleting the peers for a NI, get rid of any routes these
622 * peers are gateways for. */
623 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
624 lnet_net_lock(LNET_LOCK_EX);
625 lnet_peer_table_del_rtrs_locked(net, ptable);
626 lnet_net_unlock(LNET_LOCK_EX);
629 /* Start the cleanup process */
630 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
631 lnet_net_lock(LNET_LOCK_EX);
632 lnet_peer_table_cleanup_locked(net, ptable);
633 lnet_net_unlock(LNET_LOCK_EX);
636 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
637 lnet_peer_ni_finalize_wait(ptable);
640 static struct lnet_peer_ni *
641 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
643 struct list_head *peers;
644 struct lnet_peer_ni *lp;
646 if (the_lnet.ln_state != LNET_STATE_RUNNING)
649 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
650 list_for_each_entry(lp, peers, lpni_hashlist) {
651 if (lp->lpni_nid == nid) {
652 lnet_peer_ni_addref_locked(lp);
660 struct lnet_peer_ni *
661 lnet_find_peer_ni_locked(lnet_nid_t nid)
663 struct lnet_peer_ni *lpni;
664 struct lnet_peer_table *ptable;
667 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
669 ptable = the_lnet.ln_peer_tables[cpt];
670 lpni = lnet_get_peer_ni_locked(ptable, nid);
675 struct lnet_peer_ni *
676 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
678 struct lnet_peer_net *lpn;
679 struct lnet_peer_ni *lpni;
681 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
685 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
686 if (lpni->lpni_nid == nid)
694 lnet_find_peer(lnet_nid_t nid)
696 struct lnet_peer_ni *lpni;
697 struct lnet_peer *lp = NULL;
700 cpt = lnet_net_lock_current();
701 lpni = lnet_find_peer_ni_locked(nid);
703 lp = lpni->lpni_peer_net->lpn_peer;
704 lnet_peer_addref_locked(lp);
705 lnet_peer_ni_decref_locked(lpni);
707 lnet_net_unlock(cpt);
712 struct lnet_peer_net *
713 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
715 struct lnet_peer_net *net;
718 /* no net id provided return the first net */
719 net = list_first_entry_or_null(&lp->lp_peer_nets,
720 struct lnet_peer_net,
726 /* find the net after the one provided */
727 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
728 if (net->lpn_net_id == prev_lpn_id) {
730 * if we reached the end of the list loop to the
733 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
734 return list_first_entry_or_null(&lp->lp_peer_nets,
735 struct lnet_peer_net,
738 return list_next_entry(net, lpn_peer_nets);
745 struct lnet_peer_ni *
746 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
747 struct lnet_peer_net *peer_net,
748 struct lnet_peer_ni *prev)
750 struct lnet_peer_ni *lpni;
751 struct lnet_peer_net *net = peer_net;
755 if (list_empty(&peer->lp_peer_nets))
758 net = list_entry(peer->lp_peer_nets.next,
759 struct lnet_peer_net,
762 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
768 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
770 * if you reached the end of the peer ni list and the peer
771 * net is specified then there are no more peer nis in that
778 * we reached the end of this net ni list. move to the
781 if (prev->lpni_peer_net->lpn_peer_nets.next ==
783 /* no more nets and no more NIs. */
786 /* get the next net */
787 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
788 struct lnet_peer_net,
790 /* get the ni on it */
791 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
797 /* there are more nis left */
798 lpni = list_entry(prev->lpni_peer_nis.next,
799 struct lnet_peer_ni, lpni_peer_nis);
804 /* Call with the ln_api_mutex held */
805 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
807 struct lnet_process_id id;
808 struct lnet_peer_table *ptable;
809 struct lnet_peer *lp;
818 if (the_lnet.ln_state != LNET_STATE_RUNNING)
821 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
824 * Count the number of peers, and return E2BIG if the buffer
825 * is too small. We'll also return the desired size.
828 for (cpt = 0; cpt < lncpt; cpt++) {
829 ptable = the_lnet.ln_peer_tables[cpt];
830 count += ptable->pt_peers;
832 size = count * sizeof(*ids);
837 * Walk the peer lists and copy out the primary nids.
838 * This is safe because the peer lists are only modified
839 * while the ln_api_mutex is held. So we don't need to
840 * hold the lnet_net_lock as well, and can therefore
841 * directly call copy_to_user().
844 memset(&id, 0, sizeof(id));
845 id.pid = LNET_PID_LUSTRE;
847 for (cpt = 0; cpt < lncpt; cpt++) {
848 ptable = the_lnet.ln_peer_tables[cpt];
849 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
852 id.nid = lp->lp_primary_nid;
853 if (copy_to_user(&ids[i], &id, sizeof(id)))
866 * Start pushes to peers that need to be updated for a configuration
867 * change on this node.
870 lnet_push_update_to_peers(int force)
872 struct lnet_peer_table *ptable;
873 struct lnet_peer *lp;
877 lnet_net_lock(LNET_LOCK_EX);
878 if (lnet_peer_discovery_disabled)
880 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
881 for (cpt = 0; cpt < lncpt; cpt++) {
882 ptable = the_lnet.ln_peer_tables[cpt];
883 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
885 spin_lock(&lp->lp_lock);
886 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
887 lp->lp_state |= LNET_PEER_FORCE_PUSH;
888 spin_unlock(&lp->lp_lock);
890 if (lnet_peer_needs_push(lp))
891 lnet_peer_queue_for_discovery(lp);
894 lnet_net_unlock(LNET_LOCK_EX);
895 wake_up(&the_lnet.ln_dc_waitq);
899 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
900 * this is a preferred point-to-point path. Call with lnet_net_lock in
904 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
908 if (lpni->lpni_pref_nnids == 0)
910 if (lpni->lpni_pref_nnids == 1)
911 return lpni->lpni_pref.nid == nid;
912 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
913 if (lpni->lpni_pref.nids[i] == nid)
920 * Set a single ni as preferred, provided no preferred ni is already
921 * defined. Only to be used for non-multi-rail peer_ni.
924 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
928 spin_lock(&lpni->lpni_lock);
929 if (nid == LNET_NID_ANY) {
931 } else if (lpni->lpni_pref_nnids > 0) {
933 } else if (lpni->lpni_pref_nnids == 0) {
934 lpni->lpni_pref.nid = nid;
935 lpni->lpni_pref_nnids = 1;
936 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
938 spin_unlock(&lpni->lpni_lock);
940 CDEBUG(D_NET, "peer %s nid %s: %d\n",
941 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
946 * Clear the preferred NID from a non-multi-rail peer_ni, provided
947 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
950 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
954 spin_lock(&lpni->lpni_lock);
955 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
956 lpni->lpni_pref_nnids = 0;
957 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
958 } else if (lpni->lpni_pref_nnids == 0) {
963 spin_unlock(&lpni->lpni_lock);
965 CDEBUG(D_NET, "peer %s: %d\n",
966 libcfs_nid2str(lpni->lpni_nid), rc);
971 * Clear the preferred NIDs from a non-multi-rail peer.
974 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
976 struct lnet_peer_ni *lpni = NULL;
978 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
979 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
983 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
985 lnet_nid_t *nids = NULL;
986 lnet_nid_t *oldnids = NULL;
987 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
992 if (nid == LNET_NID_ANY) {
997 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1002 /* A non-MR node may have only one preferred NI per peer_ni */
1003 if (lpni->lpni_pref_nnids > 0) {
1004 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1010 if (lpni->lpni_pref_nnids != 0) {
1011 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1012 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1017 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
1018 if (lpni->lpni_pref.nids[i] == nid) {
1019 LIBCFS_FREE(nids, size);
1023 nids[i] = lpni->lpni_pref.nids[i];
1028 lnet_net_lock(LNET_LOCK_EX);
1029 spin_lock(&lpni->lpni_lock);
1030 if (lpni->lpni_pref_nnids == 0) {
1031 lpni->lpni_pref.nid = nid;
1033 oldnids = lpni->lpni_pref.nids;
1034 lpni->lpni_pref.nids = nids;
1036 lpni->lpni_pref_nnids++;
1037 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1038 spin_unlock(&lpni->lpni_lock);
1039 lnet_net_unlock(LNET_LOCK_EX);
1042 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1043 CFS_FREE_PTR_ARRAY(oldnids, size);
1046 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1047 spin_lock(&lpni->lpni_lock);
1048 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1049 spin_unlock(&lpni->lpni_lock);
1051 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1052 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1057 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1059 lnet_nid_t *nids = NULL;
1060 lnet_nid_t *oldnids = NULL;
1061 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1066 if (lpni->lpni_pref_nnids == 0) {
1071 if (lpni->lpni_pref_nnids == 1) {
1072 if (lpni->lpni_pref.nid != nid) {
1076 } else if (lpni->lpni_pref_nnids == 2) {
1077 if (lpni->lpni_pref.nids[0] != nid &&
1078 lpni->lpni_pref.nids[1] != nid) {
1083 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1084 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1089 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1090 if (lpni->lpni_pref.nids[i] != nid)
1092 nids[j++] = lpni->lpni_pref.nids[i];
1094 /* Check if we actually removed a nid. */
1095 if (j == lpni->lpni_pref_nnids) {
1096 LIBCFS_FREE(nids, size);
1102 lnet_net_lock(LNET_LOCK_EX);
1103 spin_lock(&lpni->lpni_lock);
1104 if (lpni->lpni_pref_nnids == 1) {
1105 lpni->lpni_pref.nid = LNET_NID_ANY;
1106 } else if (lpni->lpni_pref_nnids == 2) {
1107 oldnids = lpni->lpni_pref.nids;
1108 if (oldnids[0] == nid)
1109 lpni->lpni_pref.nid = oldnids[1];
1111 lpni->lpni_pref.nid = oldnids[2];
1113 oldnids = lpni->lpni_pref.nids;
1114 lpni->lpni_pref.nids = nids;
1116 lpni->lpni_pref_nnids--;
1117 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1118 spin_unlock(&lpni->lpni_lock);
1119 lnet_net_unlock(LNET_LOCK_EX);
1122 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1123 CFS_FREE_PTR_ARRAY(oldnids, size);
1126 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1127 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1132 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1134 struct lnet_peer_ni *lpni;
1135 lnet_nid_t primary_nid = nid;
1137 lpni = lnet_find_peer_ni_locked(nid);
1139 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1140 lnet_peer_ni_decref_locked(lpni);
1147 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1148 __must_hold(&lp->lp_lock)
1150 if (lnet_peer_discovery_disabled)
1153 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1154 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1165 lnet_is_discovery_disabled(struct lnet_peer *lp)
1169 spin_lock(&lp->lp_lock);
1170 rc = lnet_is_discovery_disabled_locked(lp);
1171 spin_unlock(&lp->lp_lock);
1177 LNetPrimaryNID(lnet_nid_t nid)
1179 struct lnet_peer *lp;
1180 struct lnet_peer_ni *lpni;
1181 lnet_nid_t primary_nid = nid;
1185 if (nid == LNET_NID_LO_0)
1186 return LNET_NID_LO_0;
1188 cpt = lnet_net_lock_current();
1189 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1194 lp = lpni->lpni_peer_net->lpn_peer;
1196 while (!lnet_peer_is_uptodate(lp)) {
1197 spin_lock(&lp->lp_lock);
1198 /* force a full discovery cycle */
1199 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1200 spin_unlock(&lp->lp_lock);
1202 rc = lnet_discover_peer_locked(lpni, cpt, true);
1205 lp = lpni->lpni_peer_net->lpn_peer;
1207 /* Only try once if discovery is disabled */
1208 if (lnet_is_discovery_disabled(lp))
1211 primary_nid = lp->lp_primary_nid;
1213 lnet_peer_ni_decref_locked(lpni);
1215 lnet_net_unlock(cpt);
1217 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1218 libcfs_nid2str(primary_nid), rc);
1221 EXPORT_SYMBOL(LNetPrimaryNID);
1223 struct lnet_peer_net *
1224 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1226 struct lnet_peer_net *peer_net;
1227 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1228 if (peer_net->lpn_net_id == net_id)
1235 * Attach a peer_ni to a peer_net and peer. This function assumes
1236 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1237 * may be attached to a different peer, in which case it will be
1238 * properly detached first. The whole operation is done atomically.
1240 * This function consumes the reference on lpni and Always returns 0.
1241 * This is the last function called from functions that do return an
1242 * int, so returning 0 here allows the compiler to do a tail call.
1245 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1246 struct lnet_peer_net *lpn,
1247 struct lnet_peer_ni *lpni,
1250 struct lnet_peer_table *ptable;
1252 /* Install the new peer_ni */
1253 lnet_net_lock(LNET_LOCK_EX);
1254 /* Add peer_ni to global peer table hash, if necessary. */
1255 if (list_empty(&lpni->lpni_hashlist)) {
1256 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1258 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1259 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1260 ptable->pt_version++;
1261 lnet_peer_ni_addref_locked(lpni);
1264 /* Detach the peer_ni from an existing peer, if necessary. */
1265 if (lpni->lpni_peer_net) {
1266 LASSERT(lpni->lpni_peer_net != lpn);
1267 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1268 lnet_peer_detach_peer_ni_locked(lpni);
1269 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1270 lpni->lpni_peer_net = NULL;
1273 /* Add peer_ni to peer_net */
1274 lpni->lpni_peer_net = lpn;
1275 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1276 lnet_update_peer_net_healthv(lpni);
1277 lnet_peer_net_addref_locked(lpn);
1279 /* Add peer_net to peer */
1280 if (!lpn->lpn_peer) {
1282 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1283 lnet_peer_addref_locked(lp);
1286 /* Add peer to global peer list, if necessary */
1287 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1288 if (list_empty(&lp->lp_peer_list)) {
1289 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1294 /* Update peer state */
1295 spin_lock(&lp->lp_lock);
1296 if (flags & LNET_PEER_CONFIGURED) {
1297 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1298 lp->lp_state |= LNET_PEER_CONFIGURED;
1300 if (flags & LNET_PEER_MULTI_RAIL) {
1301 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1302 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1303 lnet_peer_clr_non_mr_pref_nids(lp);
1306 spin_unlock(&lp->lp_lock);
1310 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1311 libcfs_nid2str(lp->lp_primary_nid),
1312 libcfs_nid2str(lpni->lpni_nid), flags);
1313 lnet_peer_ni_decref_locked(lpni);
1314 lnet_net_unlock(LNET_LOCK_EX);
1320 * Create a new peer, with nid as its primary nid.
1322 * Call with the lnet_api_mutex held.
1325 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1327 struct lnet_peer *lp;
1328 struct lnet_peer_net *lpn;
1329 struct lnet_peer_ni *lpni;
1332 LASSERT(nid != LNET_NID_ANY);
1335 * No need for the lnet_net_lock here, because the
1336 * lnet_api_mutex is held.
1338 lpni = lnet_find_peer_ni_locked(nid);
1340 /* A peer with this NID already exists. */
1341 lp = lpni->lpni_peer_net->lpn_peer;
1342 lnet_peer_ni_decref_locked(lpni);
1344 * This is an error if the peer was configured and the
1345 * primary NID differs or an attempt is made to change
1346 * the Multi-Rail flag. Otherwise the assumption is
1347 * that an existing peer is being modified.
1349 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1350 if (lp->lp_primary_nid != nid)
1352 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1356 /* Delete and recreate as a configured peer. */
1360 /* Create peer, peer_net, and peer_ni. */
1362 lp = lnet_peer_alloc(nid);
1365 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1368 lpni = lnet_peer_ni_alloc(nid);
1372 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1375 LIBCFS_FREE(lpn, sizeof(*lpn));
1377 LIBCFS_FREE(lp, sizeof(*lp));
1379 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1380 libcfs_nid2str(nid), flags, rc);
1385 * Add a NID to a peer. Call with ln_api_mutex held.
1388 * -EPERM: Non-DLC addition to a DLC-configured peer.
1389 * -EEXIST: The NID was configured by DLC for a different peer.
1390 * -ENOMEM: Out of memory.
1391 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1392 * non-multi-rail peer.
1395 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1397 struct lnet_peer_net *lpn;
1398 struct lnet_peer_ni *lpni;
1402 LASSERT(nid != LNET_NID_ANY);
1404 /* A configured peer can only be updated through configuration. */
1405 if (!(flags & LNET_PEER_CONFIGURED)) {
1406 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1413 * The MULTI_RAIL flag can be set but not cleared, because
1414 * that would leave the peer struct in an invalid state.
1416 if (flags & LNET_PEER_MULTI_RAIL) {
1417 spin_lock(&lp->lp_lock);
1418 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1419 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1420 lnet_peer_clr_non_mr_pref_nids(lp);
1422 spin_unlock(&lp->lp_lock);
1423 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1428 lpni = lnet_find_peer_ni_locked(nid);
1431 * A peer_ni already exists. This is only a problem if
1432 * it is not connected to this peer and was configured
1435 if (lpni->lpni_peer_net->lpn_peer == lp)
1437 if (lnet_peer_ni_is_configured(lpni)) {
1441 /* If this is the primary NID, destroy the peer. */
1442 if (lnet_peer_ni_is_primary(lpni)) {
1443 struct lnet_peer *rtr_lp =
1444 lpni->lpni_peer_net->lpn_peer;
1445 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1447 * if we're trying to delete a router it means
1448 * we're moving this peer NI to a new peer so must
1449 * transfer router properties to the new peer
1451 if (rtr_refcount > 0) {
1452 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1453 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1455 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1456 lnet_peer_ni_decref_locked(lpni);
1457 lpni = lnet_peer_ni_alloc(nid);
1464 lpni = lnet_peer_ni_alloc(nid);
1472 * Get the peer_net. Check that we're not adding a second
1473 * peer_ni on a peer_net of a non-multi-rail peer.
1475 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1477 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1482 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1487 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1490 lnet_peer_ni_decref_locked(lpni);
1492 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1493 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1499 * Update the primary NID of a peer, if possible.
1501 * Call with the lnet_api_mutex held.
1504 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1506 lnet_nid_t old = lp->lp_primary_nid;
1509 if (lp->lp_primary_nid == nid)
1511 rc = lnet_peer_add_nid(lp, nid, flags);
1514 lp->lp_primary_nid = nid;
1516 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1517 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1522 * lpni creation initiated due to traffic either sending or receiving.
1525 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1527 struct lnet_peer *lp;
1528 struct lnet_peer_net *lpn;
1529 struct lnet_peer_ni *lpni;
1533 if (nid == LNET_NID_ANY) {
1538 /* lnet_net_lock is not needed here because ln_api_lock is held */
1539 lpni = lnet_find_peer_ni_locked(nid);
1542 * We must have raced with another thread. Since we
1543 * know next to nothing about a peer_ni created by
1544 * traffic, we just assume everything is ok and
1547 lnet_peer_ni_decref_locked(lpni);
1551 /* Create peer, peer_net, and peer_ni. */
1553 lp = lnet_peer_alloc(nid);
1556 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1559 lpni = lnet_peer_ni_alloc(nid);
1562 if (pref != LNET_NID_ANY)
1563 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1565 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1568 LIBCFS_FREE(lpn, sizeof(*lpn));
1570 LIBCFS_FREE(lp, sizeof(*lp));
1572 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1577 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1579 * This API handles the following combinations:
1580 * Create a peer with its primary NI if only the prim_nid is provided
1581 * Add a NID to a peer identified by the prim_nid. The peer identified
1582 * by the prim_nid must already exist.
1583 * The peer being created may be non-MR.
1585 * The caller must hold ln_api_mutex. This prevents the peer from
1586 * being created/modified/deleted by a different thread.
1589 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1591 struct lnet_peer *lp = NULL;
1592 struct lnet_peer_ni *lpni;
1595 /* The prim_nid must always be specified */
1596 if (prim_nid == LNET_NID_ANY)
1599 flags = LNET_PEER_CONFIGURED;
1601 flags |= LNET_PEER_MULTI_RAIL;
1604 * If nid isn't specified, we must create a new peer with
1605 * prim_nid as its primary nid.
1607 if (nid == LNET_NID_ANY)
1608 return lnet_peer_add(prim_nid, flags);
1610 /* Look up the prim_nid, which must exist. */
1611 lpni = lnet_find_peer_ni_locked(prim_nid);
1614 lnet_peer_ni_decref_locked(lpni);
1615 lp = lpni->lpni_peer_net->lpn_peer;
1617 /* Peer must have been configured. */
1618 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1619 CDEBUG(D_NET, "peer %s was not configured\n",
1620 libcfs_nid2str(prim_nid));
1624 /* Primary NID must match */
1625 if (lp->lp_primary_nid != prim_nid) {
1626 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1627 libcfs_nid2str(prim_nid),
1628 libcfs_nid2str(lp->lp_primary_nid));
1632 /* Multi-Rail flag must match. */
1633 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1634 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1635 libcfs_nid2str(prim_nid));
1639 return lnet_peer_add_nid(lp, nid, flags);
1643 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1645 * This API handles the following combinations:
1646 * Delete a NI from a peer if both prim_nid and nid are provided.
1647 * Delete a peer if only prim_nid is provided.
1648 * Delete a peer if its primary nid is provided.
1650 * The caller must hold ln_api_mutex. This prevents the peer from
1651 * being modified/deleted by a different thread.
1654 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1656 struct lnet_peer *lp;
1657 struct lnet_peer_ni *lpni;
1660 if (prim_nid == LNET_NID_ANY)
1663 lpni = lnet_find_peer_ni_locked(prim_nid);
1666 lnet_peer_ni_decref_locked(lpni);
1667 lp = lpni->lpni_peer_net->lpn_peer;
1669 if (prim_nid != lp->lp_primary_nid) {
1670 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1671 libcfs_nid2str(prim_nid),
1672 libcfs_nid2str(lp->lp_primary_nid));
1676 lnet_net_lock(LNET_LOCK_EX);
1677 if (lp->lp_rtr_refcount > 0) {
1678 lnet_net_unlock(LNET_LOCK_EX);
1679 CERROR("%s is a router. Can not be deleted\n",
1680 libcfs_nid2str(prim_nid));
1683 lnet_net_unlock(LNET_LOCK_EX);
1685 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1686 return lnet_peer_del(lp);
1688 flags = LNET_PEER_CONFIGURED;
1689 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1690 flags |= LNET_PEER_MULTI_RAIL;
1692 return lnet_peer_del_nid(lp, nid, flags);
1696 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1698 struct lnet_peer_table *ptable;
1699 struct lnet_peer_net *lpn;
1701 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1703 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1704 LASSERT(list_empty(&lpni->lpni_txq));
1705 LASSERT(lpni->lpni_txqnob == 0);
1706 LASSERT(list_empty(&lpni->lpni_peer_nis));
1707 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1709 lpn = lpni->lpni_peer_net;
1710 lpni->lpni_peer_net = NULL;
1711 lpni->lpni_net = NULL;
1713 if (!list_empty(&lpni->lpni_hashlist)) {
1714 /* remove the peer ni from the zombie list */
1715 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1716 spin_lock(&ptable->pt_zombie_lock);
1717 list_del_init(&lpni->lpni_hashlist);
1718 ptable->pt_zombies--;
1719 spin_unlock(&ptable->pt_zombie_lock);
1722 if (lpni->lpni_pref_nnids > 1)
1723 CFS_FREE_PTR_ARRAY(lpni->lpni_pref.nids, lpni->lpni_pref_nnids);
1725 LIBCFS_FREE(lpni, sizeof(*lpni));
1728 lnet_peer_net_decref_locked(lpn);
1731 struct lnet_peer_ni *
1732 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1734 struct lnet_peer_ni *lpni = NULL;
1737 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1738 return ERR_PTR(-ESHUTDOWN);
1741 * find if a peer_ni already exists.
1742 * If so then just return that.
1744 lpni = lnet_find_peer_ni_locked(nid);
1748 lnet_net_unlock(cpt);
1750 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1753 goto out_net_relock;
1756 lpni = lnet_find_peer_ni_locked(nid);
1766 * Get a peer_ni for the given nid, create it if necessary. Takes a
1767 * hold on the peer_ni.
1769 struct lnet_peer_ni *
1770 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1772 struct lnet_peer_ni *lpni = NULL;
1775 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1776 return ERR_PTR(-ESHUTDOWN);
1779 * find if a peer_ni already exists.
1780 * If so then just return that.
1782 lpni = lnet_find_peer_ni_locked(nid);
1788 * use the lnet_api_mutex to serialize the creation of the peer_ni
1789 * and the creation/deletion of the local ni/net. When a local ni is
1790 * created, if there exists a set of peer_nis on that network,
1791 * they need to be traversed and updated. When a local NI is
1792 * deleted, which could result in a network being deleted, then
1793 * all peer nis on that network need to be removed as well.
1795 * Creation through traffic should also be serialized with
1796 * creation through DLC.
1798 lnet_net_unlock(cpt);
1799 mutex_lock(&the_lnet.ln_api_mutex);
1801 * Shutdown is only set under the ln_api_lock, so a single
1802 * check here is sufficent.
1804 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1805 lpni = ERR_PTR(-ESHUTDOWN);
1806 goto out_mutex_unlock;
1809 rc = lnet_peer_ni_traffic_add(nid, pref);
1812 goto out_mutex_unlock;
1815 lpni = lnet_find_peer_ni_locked(nid);
1819 mutex_unlock(&the_lnet.ln_api_mutex);
1822 /* Lock has been dropped, check again for shutdown. */
1823 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1825 lnet_peer_ni_decref_locked(lpni);
1826 lpni = ERR_PTR(-ESHUTDOWN);
1833 lnet_peer_gw_discovery(struct lnet_peer *lp)
1837 spin_lock(&lp->lp_lock);
1838 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1840 spin_unlock(&lp->lp_lock);
1846 lnet_peer_is_uptodate(struct lnet_peer *lp)
1850 spin_lock(&lp->lp_lock);
1851 rc = lnet_peer_is_uptodate_locked(lp);
1852 spin_unlock(&lp->lp_lock);
1857 * Is a peer uptodate from the point of view of discovery?
1859 * If it is currently being processed, obviously not.
1860 * A forced Ping or Push is also handled by the discovery thread.
1862 * Otherwise look at whether the peer needs rediscovering.
1865 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
1866 __must_hold(&lp->lp_lock)
1870 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1871 LNET_PEER_FORCE_PING |
1872 LNET_PEER_FORCE_PUSH)) {
1874 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1876 } else if (lnet_peer_needs_push(lp)) {
1878 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1879 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1891 * Queue a peer for the attention of the discovery thread. Call with
1892 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1893 * -EALREADY if the peer was already queued.
1895 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1899 spin_lock(&lp->lp_lock);
1900 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1901 lp->lp_state |= LNET_PEER_DISCOVERING;
1902 spin_unlock(&lp->lp_lock);
1903 if (list_empty(&lp->lp_dc_list)) {
1904 lnet_peer_addref_locked(lp);
1905 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1906 wake_up(&the_lnet.ln_dc_waitq);
1912 CDEBUG(D_NET, "Queue peer %s: %d\n",
1913 libcfs_nid2str(lp->lp_primary_nid), rc);
1919 * Discovery of a peer is complete. Wake all waiters on the peer.
1920 * Call with lnet_net_lock/EX held.
1922 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1924 struct lnet_msg *msg, *tmp;
1926 LIST_HEAD(pending_msgs);
1928 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1929 libcfs_nid2str(lp->lp_primary_nid));
1931 list_del_init(&lp->lp_dc_list);
1932 spin_lock(&lp->lp_lock);
1933 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1934 spin_unlock(&lp->lp_lock);
1935 wake_up_all(&lp->lp_dc_waitq);
1937 if (lp->lp_rtr_refcount > 0)
1938 lnet_router_discovery_complete(lp);
1940 lnet_net_unlock(LNET_LOCK_EX);
1942 /* iterate through all pending messages and send them again */
1943 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1944 list_del_init(&msg->msg_list);
1945 if (lp->lp_dc_error) {
1946 lnet_finalize(msg, lp->lp_dc_error);
1950 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1951 lnet_msgtyp2str(msg->msg_type),
1952 libcfs_id2str(msg->msg_target));
1953 rc = lnet_send(msg->msg_src_nid_param, msg,
1954 msg->msg_rtr_nid_param);
1956 CNETERR("Error sending %s to %s: %d\n",
1957 lnet_msgtyp2str(msg->msg_type),
1958 libcfs_id2str(msg->msg_target), rc);
1959 lnet_finalize(msg, rc);
1962 lnet_net_lock(LNET_LOCK_EX);
1963 lnet_peer_decref_locked(lp);
1967 * Handle inbound push.
1968 * Like any event handler, called with lnet_res_lock/CPT held.
1970 void lnet_peer_push_event(struct lnet_event *ev)
1972 struct lnet_ping_buffer *pbuf;
1973 struct lnet_peer *lp;
1975 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
1977 /* lnet_find_peer() adds a refcount */
1978 lp = lnet_find_peer(ev->source.nid);
1980 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1981 libcfs_nid2str(ev->initiator.nid),
1982 libcfs_nid2str(ev->source.nid));
1983 pbuf->pb_needs_post = true;
1987 /* Ensure peer state remains consistent while we modify it. */
1988 spin_lock(&lp->lp_lock);
1991 * If some kind of error happened the contents of the message
1992 * cannot be used. Clear the NIDS_UPTODATE and set the
1993 * FORCE_PING flag to trigger a ping.
1996 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1997 lp->lp_state |= LNET_PEER_FORCE_PING;
1998 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2000 libcfs_nid2str(lp->lp_primary_nid),
2001 libcfs_nid2str(ev->source.nid));
2006 * A push with invalid or corrupted info. Clear the UPTODATE
2007 * flag to trigger a ping.
2009 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2010 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2011 lp->lp_state |= LNET_PEER_FORCE_PING;
2012 CDEBUG(D_NET, "Corrupted Push from %s\n",
2013 libcfs_nid2str(lp->lp_primary_nid));
2018 * Make sure we'll allocate the correct size ping buffer when
2021 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2022 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2025 * A non-Multi-Rail peer is not supposed to be capable of
2028 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2029 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2030 libcfs_nid2str(lp->lp_primary_nid));
2035 * The peer may have discovery disabled at its end. Set
2036 * NO_DISCOVERY as appropriate.
2038 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2039 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2040 libcfs_nid2str(lp->lp_primary_nid));
2042 * Mark the peer for deletion if we already know about it
2043 * and it's going from discovery set to no discovery set
2045 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2046 LNET_PEER_DISCOVERING)) &&
2047 lp->lp_state & LNET_PEER_DISCOVERED) {
2048 CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2049 libcfs_nid2str(lp->lp_primary_nid),
2051 lp->lp_state |= LNET_PEER_MARK_DELETION;
2053 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2054 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2055 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2056 libcfs_nid2str(lp->lp_primary_nid));
2057 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2061 * Update the MULTI_RAIL flag based on the push. If the peer
2062 * was configured with DLC then the setting should match what
2064 * NB: We verified above that the MR feature bit is set in pi_features
2066 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2067 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2068 libcfs_nid2str(lp->lp_primary_nid), lp);
2069 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2070 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2071 libcfs_nid2str(lp->lp_primary_nid));
2072 } else if (lnet_peer_discovery_disabled) {
2073 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2074 libcfs_nid2str(lp->lp_primary_nid), lp);
2075 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2076 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2077 libcfs_nid2str(lp->lp_primary_nid), lp);
2079 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2080 libcfs_nid2str(lp->lp_primary_nid), lp);
2081 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2082 lnet_peer_clr_non_mr_pref_nids(lp);
2086 * Check for truncation of the Put message. Clear the
2087 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2088 * and tell discovery to allocate a bigger buffer.
2090 if (ev->mlength < ev->rlength) {
2091 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2092 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2093 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2094 lp->lp_state |= LNET_PEER_FORCE_PING;
2095 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2096 libcfs_nid2str(lp->lp_primary_nid),
2097 pbuf->pb_info.pi_nnis);
2101 /* always assume new data */
2102 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2103 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2106 * If there is data present that hasn't been processed yet,
2107 * we'll replace it if the Put contained newer data and it
2108 * fits. We're racing with a Ping or earlier Push in this
2111 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2112 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2113 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2114 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2115 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2116 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2117 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2118 libcfs_nid2str(lp->lp_primary_nid),
2119 LNET_PING_BUFFER_SEQNO(pbuf),
2120 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2126 * Allocate a buffer to copy the data. On a failure we drop
2127 * the Push and set FORCE_PING to force the discovery
2128 * thread to fix the problem by pinging the peer.
2130 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2132 lp->lp_state |= LNET_PEER_FORCE_PING;
2133 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2134 libcfs_nid2str(lp->lp_primary_nid),
2135 LNET_PING_BUFFER_SEQNO(pbuf));
2140 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2141 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2142 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2143 CDEBUG(D_NET, "Received Push %s %u\n",
2144 libcfs_nid2str(lp->lp_primary_nid),
2145 LNET_PING_BUFFER_SEQNO(pbuf));
2148 /* We've processed this buffer. It can be reposted */
2149 pbuf->pb_needs_post = true;
2152 * Queue the peer for discovery if not done, force it on the request
2153 * queue and wake the discovery thread if the peer was already queued,
2154 * because its status changed.
2156 spin_unlock(&lp->lp_lock);
2157 lnet_net_lock(LNET_LOCK_EX);
2158 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2159 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2160 wake_up(&the_lnet.ln_dc_waitq);
2162 /* Drop refcount from lookup */
2163 lnet_peer_decref_locked(lp);
2164 lnet_net_unlock(LNET_LOCK_EX);
2168 * Clear the discovery error state, unless we're already discovering
2169 * this peer, in which case the error is current.
2171 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2173 spin_lock(&lp->lp_lock);
2174 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2175 lp->lp_dc_error = 0;
2176 spin_unlock(&lp->lp_lock);
2180 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2181 * dropped/retaken within this function. An lnet_peer_ni is passed in
2182 * because discovery could tear down an lnet_peer.
2185 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2188 struct lnet_peer *lp;
2193 lnet_net_unlock(cpt);
2194 lnet_net_lock(LNET_LOCK_EX);
2195 lp = lpni->lpni_peer_net->lpn_peer;
2196 lnet_peer_clear_discovery_error(lp);
2199 * We're willing to be interrupted. The lpni can become a
2200 * zombie if we race with DLC, so we must check for that.
2203 /* Keep lp alive when the lnet_net_lock is unlocked */
2204 lnet_peer_addref_locked(lp);
2205 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2206 if (signal_pending(current))
2208 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2211 * Don't repeat discovery if discovery is disabled. This is
2212 * done to ensure we can use discovery as a standard ping as
2213 * well for backwards compatibility with routers which do not
2214 * have discovery or have discovery disabled
2216 if (lnet_is_discovery_disabled(lp) && count > 0)
2218 if (lp->lp_dc_error)
2220 if (lnet_peer_is_uptodate(lp))
2222 lnet_peer_queue_for_discovery(lp);
2224 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2227 * If caller requested a non-blocking operation then
2228 * return immediately. Once discovery is complete any
2229 * pending messages that were stopped due to discovery
2230 * will be transmitted.
2235 lnet_net_unlock(LNET_LOCK_EX);
2237 finish_wait(&lp->lp_dc_waitq, &wait);
2238 lnet_net_lock(LNET_LOCK_EX);
2239 lnet_peer_decref_locked(lp);
2240 /* Peer may have changed */
2241 lp = lpni->lpni_peer_net->lpn_peer;
2243 finish_wait(&lp->lp_dc_waitq, &wait);
2245 lnet_net_unlock(LNET_LOCK_EX);
2247 lnet_peer_decref_locked(lp);
2249 * The peer may have changed, so re-check and rediscover if that turns
2250 * out to have been the case. The reference count on lp ensured that
2251 * even if it was unlinked from lpni the memory could not be recycled.
2252 * Thus the check below is sufficient to determine whether the peer
2253 * changed. If the peer changed, then lp must not be dereferenced.
2255 if (lp != lpni->lpni_peer_net->lpn_peer)
2258 if (signal_pending(current))
2260 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2262 else if (lp->lp_dc_error)
2263 rc = lp->lp_dc_error;
2265 CDEBUG(D_NET, "non-blocking discovery\n");
2266 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2269 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2270 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2271 libcfs_nid2str(lpni->lpni_nid), rc,
2272 (!block) ? "pending discovery" : "discovery complete");
2277 /* Handle an incoming ack for a push. */
2279 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2281 struct lnet_ping_buffer *pbuf;
2283 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2284 spin_lock(&lp->lp_lock);
2285 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2286 lp->lp_push_error = ev->status;
2288 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2290 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2291 spin_unlock(&lp->lp_lock);
2293 CDEBUG(D_NET, "peer %s ev->status %d\n",
2294 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2297 /* Handle a Reply message. This is the reply to a Ping message. */
2299 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2301 struct lnet_ping_buffer *pbuf;
2304 spin_lock(&lp->lp_lock);
2306 lp->lp_disc_src_nid = ev->target.nid;
2309 * If some kind of error happened the contents of message
2310 * cannot be used. Set PING_FAILED to trigger a retry.
2313 lp->lp_state |= LNET_PEER_PING_FAILED;
2314 lp->lp_ping_error = ev->status;
2315 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2317 libcfs_nid2str(lp->lp_primary_nid),
2318 libcfs_nid2str(ev->source.nid));
2322 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2323 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2324 lnet_swap_pinginfo(pbuf);
2327 * A reply with invalid or corrupted info. Set PING_FAILED to
2330 rc = lnet_ping_info_validate(&pbuf->pb_info);
2332 lp->lp_state |= LNET_PEER_PING_FAILED;
2333 lp->lp_ping_error = 0;
2334 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2335 libcfs_nid2str(lp->lp_primary_nid), rc);
2341 * The peer may have discovery disabled at its end. Set
2342 * NO_DISCOVERY as appropriate.
2344 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2345 !lnet_peer_discovery_disabled) {
2346 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2347 libcfs_nid2str(lp->lp_primary_nid));
2348 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2350 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2351 libcfs_nid2str(lp->lp_primary_nid));
2352 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2356 * Update the MULTI_RAIL flag based on the reply. If the peer
2357 * was configured with DLC then the setting should match what
2360 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2361 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2362 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2363 libcfs_nid2str(lp->lp_primary_nid), lp);
2364 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2365 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2366 libcfs_nid2str(lp->lp_primary_nid));
2367 } else if (lnet_peer_discovery_disabled) {
2369 "peer %s(%p) not MR: DD disabled locally\n",
2370 libcfs_nid2str(lp->lp_primary_nid), lp);
2371 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2373 "peer %s(%p) not MR: DD disabled remotely\n",
2374 libcfs_nid2str(lp->lp_primary_nid), lp);
2376 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2377 libcfs_nid2str(lp->lp_primary_nid), lp);
2378 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2379 lnet_peer_clr_non_mr_pref_nids(lp);
2381 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2382 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2383 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2384 libcfs_nid2str(lp->lp_primary_nid));
2386 CERROR("Multi-Rail state vanished from %s\n",
2387 libcfs_nid2str(lp->lp_primary_nid));
2388 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2393 * Make sure we'll allocate the correct size ping buffer when
2396 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2397 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2400 * Check for truncation of the Reply. Clear PING_SENT and set
2401 * PING_FAILED to trigger a retry.
2403 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2404 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2405 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2406 lp->lp_state |= LNET_PEER_PING_FAILED;
2407 lp->lp_ping_error = 0;
2408 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2409 libcfs_nid2str(lp->lp_primary_nid),
2410 pbuf->pb_info.pi_nnis);
2415 * Check the sequence numbers in the reply. These are only
2416 * available if the reply came from a Multi-Rail peer.
2418 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2419 pbuf->pb_info.pi_nnis > 1 &&
2420 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2421 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2422 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2423 libcfs_nid2str(lp->lp_primary_nid),
2424 LNET_PING_BUFFER_SEQNO(pbuf),
2427 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2430 /* We're happy with the state of the data in the buffer. */
2431 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2432 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2433 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2434 lnet_ping_buffer_decref(lp->lp_data);
2436 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2437 lnet_ping_buffer_addref(pbuf);
2440 lp->lp_state &= ~LNET_PEER_PING_SENT;
2441 spin_unlock(&lp->lp_lock);
2443 lnet_net_lock(LNET_LOCK_EX);
2445 * If this peer is a gateway, call the routing callback to
2446 * handle the ping reply
2448 if (lp->lp_rtr_refcount > 0)
2449 lnet_router_discovery_ping_reply(lp);
2450 lnet_net_unlock(LNET_LOCK_EX);
2454 * Send event handling. Only matters for error cases, where we clean
2455 * up state on the peer and peer_ni that would otherwise be updated in
2456 * the REPLY event handler for a successful Ping, and the ACK event
2457 * handler for a successful Push.
2460 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2467 spin_lock(&lp->lp_lock);
2468 if (ev->msg_type == LNET_MSG_GET) {
2469 lp->lp_state &= ~LNET_PEER_PING_SENT;
2470 lp->lp_state |= LNET_PEER_PING_FAILED;
2471 lp->lp_ping_error = ev->status;
2472 } else { /* ev->msg_type == LNET_MSG_PUT */
2473 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2474 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2475 lp->lp_push_error = ev->status;
2477 spin_unlock(&lp->lp_lock);
2478 rc = LNET_REDISCOVER_PEER;
2480 CDEBUG(D_NET, "%s Send to %s: %d\n",
2481 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2482 libcfs_nid2str(ev->target.nid), rc);
2487 * Unlink event handling. This event is only seen if a call to
2488 * LNetMDUnlink() caused the event to be unlinked. If this call was
2489 * made after the event was set up in LNetGet() or LNetPut() then we
2490 * assume the Ping or Push timed out.
2493 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2495 spin_lock(&lp->lp_lock);
2496 /* We've passed through LNetGet() */
2497 if (lp->lp_state & LNET_PEER_PING_SENT) {
2498 lp->lp_state &= ~LNET_PEER_PING_SENT;
2499 lp->lp_state |= LNET_PEER_PING_FAILED;
2500 lp->lp_ping_error = -ETIMEDOUT;
2501 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2502 libcfs_nid2str(lp->lp_primary_nid));
2504 /* We've passed through LNetPut() */
2505 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2506 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2507 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2508 lp->lp_push_error = -ETIMEDOUT;
2509 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2510 libcfs_nid2str(lp->lp_primary_nid));
2512 spin_unlock(&lp->lp_lock);
2516 * Event handler for the discovery EQ.
2518 * Called with lnet_res_lock(cpt) held. The cpt is the
2519 * lnet_cpt_of_cookie() of the md handle cookie.
2521 static void lnet_discovery_event_handler(struct lnet_event *event)
2523 struct lnet_peer *lp = event->md_user_ptr;
2524 struct lnet_ping_buffer *pbuf;
2527 /* discovery needs to take another look */
2528 rc = LNET_REDISCOVER_PEER;
2530 CDEBUG(D_NET, "Received event: %d\n", event->type);
2532 switch (event->type) {
2533 case LNET_EVENT_ACK:
2534 lnet_discovery_event_ack(lp, event);
2536 case LNET_EVENT_REPLY:
2537 lnet_discovery_event_reply(lp, event);
2539 case LNET_EVENT_SEND:
2540 /* Only send failure triggers a retry. */
2541 rc = lnet_discovery_event_send(lp, event);
2543 case LNET_EVENT_UNLINK:
2544 /* LNetMDUnlink() was called */
2545 lnet_discovery_event_unlink(lp, event);
2548 /* Invalid events. */
2551 lnet_net_lock(LNET_LOCK_EX);
2552 if (event->unlinked) {
2553 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2554 lnet_ping_buffer_decref(pbuf);
2555 lnet_peer_decref_locked(lp);
2558 /* put peer back at end of request queue, if discovery not already
2560 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2561 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2562 wake_up(&the_lnet.ln_dc_waitq);
2564 lnet_net_unlock(LNET_LOCK_EX);
2568 * Build a peer from incoming data.
2570 * The NIDs in the incoming data are supposed to be structured as follows:
2573 * - other NIDs in same net
2574 * - NIDs in second net
2575 * - NIDs in third net
2577 * This due to the way the list of NIDs in the data is created.
2579 * Note that this function will mark the peer uptodate unless an
2580 * ENOMEM is encontered. All other errors are due to a conflict
2581 * between the DLC configuration and what discovery sees. We treat DLC
2582 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2583 * peer from becoming stuck in discovery.
2585 static int lnet_peer_merge_data(struct lnet_peer *lp,
2586 struct lnet_ping_buffer *pbuf)
2588 struct lnet_peer_ni *lpni;
2589 lnet_nid_t *curnis = NULL;
2590 struct lnet_ni_status *addnis = NULL;
2591 lnet_nid_t *delnis = NULL;
2601 flags = LNET_PEER_DISCOVERED;
2602 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2603 flags |= LNET_PEER_MULTI_RAIL;
2606 * Cache the routing feature for the peer; whether it is enabled
2607 * for disabled as reported by the remote peer.
2609 spin_lock(&lp->lp_lock);
2610 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2611 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2613 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2614 spin_unlock(&lp->lp_lock);
2616 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2617 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2618 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2619 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2620 if (!curnis || !addnis || !delnis) {
2628 /* Construct the list of NIDs present in peer. */
2630 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2631 curnis[ncurnis++] = lpni->lpni_nid;
2634 * Check for NIDs in pbuf not present in curnis[].
2635 * The loop starts at 1 to skip the loopback NID.
2637 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2638 for (j = 0; j < ncurnis; j++)
2639 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2642 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2645 * Check for NIDs in curnis[] not present in pbuf.
2646 * The nested loop starts at 1 to skip the loopback NID.
2648 * But never add the loopback NID to delnis[]: if it is
2649 * present in curnis[] then this peer is for this node.
2651 for (i = 0; i < ncurnis; i++) {
2652 if (curnis[i] == LNET_NID_LO_0)
2654 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2655 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2657 * update the information we cache for the
2658 * peer with the latest information we
2661 lpni = lnet_find_peer_ni_locked(curnis[i]);
2663 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2664 lnet_peer_ni_decref_locked(lpni);
2669 if (j == pbuf->pb_info.pi_nnis)
2670 delnis[ndelnis++] = curnis[i];
2674 * If we get here and the discovery is disabled then we don't want
2675 * to add or delete any NIs. We just updated the ones we have some
2676 * information on, and call it a day
2679 if (lnet_is_discovery_disabled(lp))
2682 for (i = 0; i < naddnis; i++) {
2683 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2685 CERROR("Error adding NID %s to peer %s: %d\n",
2686 libcfs_nid2str(addnis[i].ns_nid),
2687 libcfs_nid2str(lp->lp_primary_nid), rc);
2691 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2693 lpni->lpni_ns_status = addnis[i].ns_status;
2694 lnet_peer_ni_decref_locked(lpni);
2698 for (i = 0; i < ndelnis; i++) {
2700 * for routers it's okay to delete the primary_nid because
2701 * the upper layers don't really rely on it. So if we're
2702 * being told that the router changed its primary_nid
2703 * then it's okay to delete it.
2705 if (lp->lp_rtr_refcount > 0)
2706 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2707 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2709 CERROR("Error deleting NID %s from peer %s: %d\n",
2710 libcfs_nid2str(delnis[i]),
2711 libcfs_nid2str(lp->lp_primary_nid), rc);
2717 * Errors other than -ENOMEM are due to peers having been
2718 * configured with DLC. Ignore these because DLC overrides
2723 CFS_FREE_PTR_ARRAY(curnis, nnis);
2724 CFS_FREE_PTR_ARRAY(addnis, nnis);
2725 CFS_FREE_PTR_ARRAY(delnis, nnis);
2726 lnet_ping_buffer_decref(pbuf);
2727 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2730 spin_lock(&lp->lp_lock);
2731 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2732 lp->lp_state |= LNET_PEER_FORCE_PING;
2733 spin_unlock(&lp->lp_lock);
2739 * The data in pbuf says lp is its primary peer, but the data was
2740 * received by a different peer. Try to update lp with the data.
2743 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2745 struct lnet_handle_md mdh;
2747 /* Queue lp for discovery, and force it on the request queue. */
2748 lnet_net_lock(LNET_LOCK_EX);
2749 if (lnet_peer_queue_for_discovery(lp))
2750 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2751 lnet_net_unlock(LNET_LOCK_EX);
2753 LNetInvalidateMDHandle(&mdh);
2756 * Decide whether we can move the peer to the DATA_PRESENT state.
2758 * We replace stale data for a multi-rail peer, repair PING_FAILED
2759 * status, and preempt FORCE_PING.
2761 * If after that we have DATA_PRESENT, we merge it into this peer.
2763 spin_lock(&lp->lp_lock);
2764 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2765 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2766 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2767 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2768 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2769 lnet_ping_buffer_decref(pbuf);
2774 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2775 lnet_ping_buffer_decref(lp->lp_data);
2777 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2779 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2780 mdh = lp->lp_ping_mdh;
2781 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2782 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2783 lp->lp_ping_error = 0;
2785 if (lp->lp_state & LNET_PEER_FORCE_PING)
2786 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2787 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2788 spin_unlock(&lp->lp_lock);
2790 if (!LNetMDHandleIsInvalid(mdh))
2794 return lnet_peer_merge_data(lp, pbuf);
2796 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2800 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2804 for (i = 0; i < pinfo->pi_nnis; i++) {
2805 if (pinfo->pi_ni[i].ns_nid == nid)
2813 * Update a peer using the data received.
2815 static int lnet_peer_data_present(struct lnet_peer *lp)
2816 __must_hold(&lp->lp_lock)
2818 struct lnet_ping_buffer *pbuf;
2819 struct lnet_peer_ni *lpni;
2820 lnet_nid_t nid = LNET_NID_ANY;
2826 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2827 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2828 spin_unlock(&lp->lp_lock);
2831 * Modifications of peer structures are done while holding the
2832 * ln_api_mutex. A global lock is required because we may be
2833 * modifying multiple peer structures, and a mutex greatly
2834 * simplifies memory management.
2836 * The actual changes to the data structures must also protect
2837 * against concurrent lookups, for which the lnet_net_lock in
2838 * LNET_LOCK_EX mode is used.
2840 mutex_lock(&the_lnet.ln_api_mutex);
2841 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2847 * If this peer is not on the peer list then it is being torn
2848 * down, and our reference count may be all that is keeping it
2849 * alive. Don't do any work on it.
2851 if (list_empty(&lp->lp_peer_list))
2854 flags = LNET_PEER_DISCOVERED;
2855 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2856 flags |= LNET_PEER_MULTI_RAIL;
2859 * Check whether the primary NID in the message matches the
2860 * primary NID of the peer. If it does, update the peer, if
2861 * it it does not, check whether there is already a peer with
2862 * that primary NID. If no such peer exists, try to update
2863 * the primary NID of the current peer (allowed if it was
2864 * created due to message traffic) and complete the update.
2865 * If the peer did exist, hand off the data to it.
2867 * The peer for the loopback interface is a special case: this
2868 * is the peer for the local node, and we want to set its
2869 * primary NID to the correct value here. Moreover, this peer
2870 * can show up with only the loopback NID in the ping buffer.
2872 if (pbuf->pb_info.pi_nnis <= 1)
2874 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2875 if (lp->lp_primary_nid == LNET_NID_LO_0) {
2876 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2878 rc = lnet_peer_merge_data(lp, pbuf);
2880 * if the primary nid of the peer is present in the ping info returned
2881 * from the peer, but it's not the local primary peer we have
2882 * cached and discovery is disabled, then we don't want to update
2883 * our local peer info, by adding or removing NIDs, we just want
2884 * to update the status of the nids that we currently have
2885 * recorded in that peer.
2887 } else if (lp->lp_primary_nid == nid ||
2888 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2889 lnet_is_discovery_disabled(lp))) {
2890 rc = lnet_peer_merge_data(lp, pbuf);
2892 lpni = lnet_find_peer_ni_locked(nid);
2894 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2896 CERROR("Primary NID error %s versus %s: %d\n",
2897 libcfs_nid2str(lp->lp_primary_nid),
2898 libcfs_nid2str(nid), rc);
2900 rc = lnet_peer_merge_data(lp, pbuf);
2903 struct lnet_peer *new_lp;
2904 new_lp = lpni->lpni_peer_net->lpn_peer;
2906 * if lp has discovery/MR enabled that means new_lp
2907 * should have discovery/MR enabled as well, since
2908 * it's the same peer, which we're about to merge
2910 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2911 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2912 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2913 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2915 rc = lnet_peer_set_primary_data(new_lp, pbuf);
2916 lnet_consolidate_routes_locked(lp, new_lp);
2917 lnet_peer_ni_decref_locked(lpni);
2921 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2923 mutex_unlock(&the_lnet.ln_api_mutex);
2925 spin_lock(&lp->lp_lock);
2926 /* Tell discovery to re-check the peer immediately. */
2928 rc = LNET_REDISCOVER_PEER;
2933 * A ping failed. Clear the PING_FAILED state and set the
2934 * FORCE_PING state, to ensure a retry even if discovery is
2935 * disabled. This avoids being left with incorrect state.
2937 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2938 __must_hold(&lp->lp_lock)
2940 struct lnet_handle_md mdh;
2943 mdh = lp->lp_ping_mdh;
2944 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2945 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2946 lp->lp_state |= LNET_PEER_FORCE_PING;
2947 rc = lp->lp_ping_error;
2948 lp->lp_ping_error = 0;
2949 spin_unlock(&lp->lp_lock);
2951 if (!LNetMDHandleIsInvalid(mdh))
2954 CDEBUG(D_NET, "peer %s:%d\n",
2955 libcfs_nid2str(lp->lp_primary_nid), rc);
2957 spin_lock(&lp->lp_lock);
2958 return rc ? rc : LNET_REDISCOVER_PEER;
2962 * Select NID to send a Ping or Push to.
2964 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2966 struct lnet_peer_ni *lpni;
2968 /* Look for a direct-connected NID for this peer. */
2970 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2971 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2976 return lpni->lpni_nid;
2978 /* Look for a routed-connected NID for this peer. */
2980 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2981 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2986 return lpni->lpni_nid;
2988 return LNET_NID_ANY;
2991 /* Active side of ping. */
2992 static int lnet_peer_send_ping(struct lnet_peer *lp)
2993 __must_hold(&lp->lp_lock)
3000 lp->lp_state |= LNET_PEER_PING_SENT;
3001 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3002 spin_unlock(&lp->lp_lock);
3004 cpt = lnet_net_lock_current();
3005 /* Refcount for MD. */
3006 lnet_peer_addref_locked(lp);
3007 pnid = lnet_peer_select_nid(lp);
3008 lnet_net_unlock(cpt);
3010 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3012 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
3013 the_lnet.ln_dc_handler, false);
3016 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3017 * refcount on the peer, otherwise LNetMDUnlink will be called
3018 * which will eventually do that.
3022 lnet_peer_decref_locked(lp);
3023 lnet_net_unlock(cpt);
3024 rc = -rc; /* change the rc to negative value */
3026 } else if (rc < 0) {
3030 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3032 spin_lock(&lp->lp_lock);
3036 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3038 * The errors that get us here are considered hard errors and
3039 * cause Discovery to terminate. So we clear PING_SENT, but do
3040 * not set either PING_FAILED or FORCE_PING. In fact we need
3041 * to clear PING_FAILED, because the unlink event handler will
3042 * have set it if we called LNetMDUnlink() above.
3044 spin_lock(&lp->lp_lock);
3045 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3050 * This function exists because you cannot call LNetMDUnlink() from an
3053 static int lnet_peer_push_failed(struct lnet_peer *lp)
3054 __must_hold(&lp->lp_lock)
3056 struct lnet_handle_md mdh;
3059 mdh = lp->lp_push_mdh;
3060 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3061 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3062 rc = lp->lp_push_error;
3063 lp->lp_push_error = 0;
3064 spin_unlock(&lp->lp_lock);
3066 if (!LNetMDHandleIsInvalid(mdh))
3069 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3070 spin_lock(&lp->lp_lock);
3071 return rc ? rc : LNET_REDISCOVER_PEER;
3075 * Mark the peer as discovered.
3077 static int lnet_peer_discovered(struct lnet_peer *lp)
3078 __must_hold(&lp->lp_lock)
3080 lp->lp_state |= LNET_PEER_DISCOVERED;
3081 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3082 LNET_PEER_REDISCOVER);
3084 lp->lp_dc_error = 0;
3086 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3091 /* Active side of push. */
3092 static int lnet_peer_send_push(struct lnet_peer *lp)
3093 __must_hold(&lp->lp_lock)
3095 struct lnet_ping_buffer *pbuf;
3096 struct lnet_process_id id;
3101 /* Don't push to a non-multi-rail peer. */
3102 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3103 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3104 /* if peer's NIDs are uptodate then peer is discovered */
3105 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3106 rc = lnet_peer_discovered(lp);
3113 lp->lp_state |= LNET_PEER_PUSH_SENT;
3114 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3115 spin_unlock(&lp->lp_lock);
3117 cpt = lnet_net_lock_current();
3118 pbuf = the_lnet.ln_ping_target;
3119 lnet_ping_buffer_addref(pbuf);
3120 lnet_net_unlock(cpt);
3122 /* Push source MD */
3123 md.start = &pbuf->pb_info;
3124 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3125 md.threshold = 2; /* Put/Ack */
3127 md.options = LNET_MD_TRACK_RESPONSE;
3128 md.handler = the_lnet.ln_dc_handler;
3131 rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3133 lnet_ping_buffer_decref(pbuf);
3134 CERROR("Can't bind push source MD: %d\n", rc);
3137 cpt = lnet_net_lock_current();
3138 /* Refcount for MD. */
3139 lnet_peer_addref_locked(lp);
3140 id.pid = LNET_PID_LUSTRE;
3141 id.nid = lnet_peer_select_nid(lp);
3142 lnet_net_unlock(cpt);
3144 if (id.nid == LNET_NID_ANY) {
3149 rc = LNetPut(lp->lp_disc_src_nid, lp->lp_push_mdh,
3150 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3151 LNET_PROTO_PING_MATCHBITS, 0, 0);
3154 * reset the discovery nid. There is no need to restrict sending
3155 * from that source, if we call lnet_push_update_to_peers(). It'll
3156 * get set to a specific NID, if we initiate discovery from the
3159 lp->lp_disc_src_nid = LNET_NID_ANY;
3164 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3166 spin_lock(&lp->lp_lock);
3170 LNetMDUnlink(lp->lp_push_mdh);
3171 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3173 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3175 * The errors that get us here are considered hard errors and
3176 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3177 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3178 * because the unlink event handler will have set it if we
3179 * called LNetMDUnlink() above.
3181 spin_lock(&lp->lp_lock);
3182 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3187 * An unrecoverable error was encountered during discovery.
3188 * Set error status in peer and abort discovery.
3190 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3192 CDEBUG(D_NET, "Discovery error %s: %d\n",
3193 libcfs_nid2str(lp->lp_primary_nid), error);
3195 spin_lock(&lp->lp_lock);
3196 lp->lp_dc_error = error;
3197 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3198 lp->lp_state |= LNET_PEER_REDISCOVER;
3199 spin_unlock(&lp->lp_lock);
3203 * Discovering this peer is taking too long. Cancel any Ping or Push
3204 * that discovery is waiting on by unlinking the relevant MDs. The
3205 * lnet_discovery_event_handler() will proceed from here and complete
3208 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3210 struct lnet_handle_md ping_mdh;
3211 struct lnet_handle_md push_mdh;
3213 LNetInvalidateMDHandle(&ping_mdh);
3214 LNetInvalidateMDHandle(&push_mdh);
3216 spin_lock(&lp->lp_lock);
3217 if (lp->lp_state & LNET_PEER_PING_SENT) {
3218 ping_mdh = lp->lp_ping_mdh;
3219 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3221 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3222 push_mdh = lp->lp_push_mdh;
3223 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3225 spin_unlock(&lp->lp_lock);
3227 if (!LNetMDHandleIsInvalid(ping_mdh))
3228 LNetMDUnlink(ping_mdh);
3229 if (!LNetMDHandleIsInvalid(push_mdh))
3230 LNetMDUnlink(push_mdh);
3234 * Wait for work to be queued or some other change that must be
3235 * attended to. Returns non-zero if the discovery thread should shut
3238 static int lnet_peer_discovery_wait_for_work(void)
3245 cpt = lnet_net_lock_current();
3247 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3248 TASK_INTERRUPTIBLE);
3249 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3251 if (lnet_push_target_resize_needed() ||
3252 the_lnet.ln_push_target->pb_needs_post)
3254 if (!list_empty(&the_lnet.ln_dc_request))
3256 if (!list_empty(&the_lnet.ln_msg_resend))
3258 lnet_net_unlock(cpt);
3261 * wakeup max every second to check if there are peers that
3262 * have been stuck on the working queue for greater than
3265 schedule_timeout(cfs_time_seconds(1));
3266 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3267 cpt = lnet_net_lock_current();
3269 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3271 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3274 lnet_net_unlock(cpt);
3276 CDEBUG(D_NET, "woken: %d\n", rc);
3282 * Messages that were pending on a destroyed peer will be put on a global
3283 * resend list. The message resend list will be checked by
3284 * the discovery thread when it wakes up, and will resend messages. These
3285 * messages can still be sendable in the case the lpni which was the initial
3286 * cause of the message re-queue was transfered to another peer.
3288 * It is possible that LNet could be shutdown while we're iterating
3289 * through the list. lnet_shudown_lndnets() will attempt to access the
3290 * resend list, but will have to wait until the spinlock is released, by
3291 * which time there shouldn't be any more messages on the resend list.
3292 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3293 * for the messages so they can be released. The other case is that
3294 * lnet_shudown_lndnets() can finalize all the messages before this
3295 * function can visit the resend list, in which case this function will be
3298 static void lnet_resend_msgs(void)
3300 struct lnet_msg *msg, *tmp;
3304 spin_lock(&the_lnet.ln_msg_resend_lock);
3305 list_splice(&the_lnet.ln_msg_resend, &resend);
3306 spin_unlock(&the_lnet.ln_msg_resend_lock);
3308 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3309 list_del_init(&msg->msg_list);
3310 rc = lnet_send(msg->msg_src_nid_param, msg,
3311 msg->msg_rtr_nid_param);
3313 CNETERR("Error sending %s to %s: %d\n",
3314 lnet_msgtyp2str(msg->msg_type),
3315 libcfs_id2str(msg->msg_target), rc);
3316 lnet_finalize(msg, rc);
3321 /* The discovery thread. */
3322 static int lnet_peer_discovery(void *arg)
3324 struct lnet_peer *lp;
3327 wait_for_completion(&the_lnet.ln_started);
3329 CDEBUG(D_NET, "started\n");
3332 if (lnet_peer_discovery_wait_for_work())
3335 if (lnet_push_target_resize_needed())
3336 lnet_push_target_resize();
3337 else if (the_lnet.ln_push_target->pb_needs_post)
3338 lnet_push_target_post(the_lnet.ln_push_target,
3339 &the_lnet.ln_push_target_md);
3343 lnet_net_lock(LNET_LOCK_EX);
3344 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3345 lnet_net_unlock(LNET_LOCK_EX);
3350 * Process all incoming discovery work requests. When
3351 * discovery must wait on a peer to change state, it
3352 * is added to the tail of the ln_dc_working queue. A
3353 * timestamp keeps track of when the peer was added,
3354 * so we can time out discovery requests that take too
3357 while (!list_empty(&the_lnet.ln_dc_request)) {
3358 lp = list_first_entry(&the_lnet.ln_dc_request,
3359 struct lnet_peer, lp_dc_list);
3360 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3362 * set the time the peer was put on the dc_working
3363 * queue. It shouldn't remain on the queue
3364 * forever, in case the GET message (for ping)
3365 * doesn't get a REPLY or the PUT message (for
3366 * push) doesn't get an ACK.
3368 lp->lp_last_queued = ktime_get_real_seconds();
3369 lnet_net_unlock(LNET_LOCK_EX);
3371 if (lnet_push_target_resize_needed())
3372 lnet_push_target_resize();
3373 else if (the_lnet.ln_push_target->pb_needs_post)
3374 lnet_push_target_post(the_lnet.ln_push_target,
3375 &the_lnet.ln_push_target_md);
3378 * Select an action depending on the state of
3379 * the peer and whether discovery is disabled.
3380 * The check whether discovery is disabled is
3381 * done after the code that handles processing
3382 * for arrived data, cleanup for failures, and
3383 * forcing a Ping or Push.
3385 spin_lock(&lp->lp_lock);
3386 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3387 libcfs_nid2str(lp->lp_primary_nid), lp,
3389 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3390 rc = lnet_peer_data_present(lp);
3391 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3392 rc = lnet_peer_ping_failed(lp);
3393 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3394 rc = lnet_peer_push_failed(lp);
3395 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3396 rc = lnet_peer_send_ping(lp);
3397 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3398 rc = lnet_peer_send_push(lp);
3399 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3400 rc = lnet_peer_send_ping(lp);
3401 else if (lnet_peer_needs_push(lp))
3402 rc = lnet_peer_send_push(lp);
3404 rc = lnet_peer_discovered(lp);
3405 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3406 libcfs_nid2str(lp->lp_primary_nid), lp,
3408 spin_unlock(&lp->lp_lock);
3410 lnet_net_lock(LNET_LOCK_EX);
3411 if (rc == LNET_REDISCOVER_PEER) {
3412 list_move(&lp->lp_dc_list,
3413 &the_lnet.ln_dc_request);
3415 lnet_peer_discovery_error(lp, rc);
3417 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3418 lnet_peer_discovery_complete(lp);
3419 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3422 if (lp->lp_state & LNET_PEER_MARK_DELETION) {
3423 struct list_head rlist;
3424 struct lnet_route *route, *tmp;
3425 int sensitivity = lp->lp_health_sensitivity;
3427 INIT_LIST_HEAD(&rlist);
3430 * remove the peer from the discovery work
3431 * queue if it's on there in preparation
3434 if (!list_empty(&lp->lp_dc_list))
3435 list_del(&lp->lp_dc_list);
3437 lnet_net_unlock(LNET_LOCK_EX);
3439 mutex_lock(&the_lnet.ln_api_mutex);
3441 lnet_net_lock(LNET_LOCK_EX);
3442 list_for_each_entry_safe(route, tmp,
3445 lnet_move_route(route, NULL, &rlist);
3446 lnet_net_unlock(LNET_LOCK_EX);
3448 /* delete the peer */
3451 list_for_each_entry_safe(route, tmp,
3453 /* re-add these routes */
3454 lnet_add_route(route->lr_net,
3459 LIBCFS_FREE(route, sizeof(*route));
3461 mutex_unlock(&the_lnet.ln_api_mutex);
3463 lnet_net_lock(LNET_LOCK_EX);
3467 lnet_net_unlock(LNET_LOCK_EX);
3470 CDEBUG(D_NET, "stopping\n");
3472 * Clean up before telling lnet_peer_discovery_stop() that
3473 * we're done. Use wake_up() below to somewhat reduce the
3474 * size of the thundering herd if there are multiple threads
3475 * waiting on discovery of a single peer.
3478 /* Queue cleanup 1: stop all pending pings and pushes. */
3479 lnet_net_lock(LNET_LOCK_EX);
3480 while (!list_empty(&the_lnet.ln_dc_working)) {
3481 lp = list_first_entry(&the_lnet.ln_dc_working,
3482 struct lnet_peer, lp_dc_list);
3483 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3484 lnet_net_unlock(LNET_LOCK_EX);
3485 lnet_peer_cancel_discovery(lp);
3486 lnet_net_lock(LNET_LOCK_EX);
3488 lnet_net_unlock(LNET_LOCK_EX);
3490 /* Queue cleanup 2: wait for the expired queue to clear. */
3491 while (!list_empty(&the_lnet.ln_dc_expired))
3492 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3494 /* Queue cleanup 3: clear the request queue. */
3495 lnet_net_lock(LNET_LOCK_EX);
3496 while (!list_empty(&the_lnet.ln_dc_request)) {
3497 lp = list_first_entry(&the_lnet.ln_dc_request,
3498 struct lnet_peer, lp_dc_list);
3499 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3500 lnet_peer_discovery_complete(lp);
3502 lnet_net_unlock(LNET_LOCK_EX);
3504 lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3505 the_lnet.ln_dc_handler = NULL;
3507 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3508 wake_up(&the_lnet.ln_dc_waitq);
3510 CDEBUG(D_NET, "stopped\n");
3515 /* ln_api_mutex is held on entry. */
3516 int lnet_peer_discovery_start(void)
3518 struct task_struct *task;
3521 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3524 the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3525 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3526 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3529 CERROR("Can't start peer discovery thread: %d\n", rc);
3531 the_lnet.ln_dc_handler = NULL;
3533 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3536 CDEBUG(D_NET, "discovery start: %d\n", rc);
3541 /* ln_api_mutex is held on entry. */
3542 void lnet_peer_discovery_stop(void)
3544 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3547 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3548 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3550 /* In the LNetNIInit() path we may be stopping discovery before it
3551 * entered its work loop
3553 if (!completion_done(&the_lnet.ln_started))
3554 complete(&the_lnet.ln_started);
3556 wake_up(&the_lnet.ln_dc_waitq);
3558 wait_event(the_lnet.ln_dc_waitq,
3559 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3561 LASSERT(list_empty(&the_lnet.ln_dc_request));
3562 LASSERT(list_empty(&the_lnet.ln_dc_working));
3563 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3565 CDEBUG(D_NET, "discovery stopped\n");
3571 lnet_debug_peer(lnet_nid_t nid)
3573 char *aliveness = "NA";
3574 struct lnet_peer_ni *lp;
3577 cpt = lnet_cpt_of_nid(nid, NULL);
3580 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3582 lnet_net_unlock(cpt);
3583 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3587 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3588 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3590 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3591 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3592 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3593 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3594 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3596 lnet_peer_ni_decref_locked(lp);
3598 lnet_net_unlock(cpt);
3601 /* Gathering information for userspace. */
3603 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3604 char aliveness[LNET_MAX_STR_LEN],
3605 __u32 *cpt_iter, __u32 *refcount,
3606 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3607 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3608 __u32 *peer_tx_qnob)
3610 struct lnet_peer_table *peer_table;
3611 struct lnet_peer_ni *lp;
3616 /* get the number of CPTs */
3617 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3619 /* if the cpt number to be examined is >= the number of cpts in
3620 * the system then indicate that there are no more cpts to examin
3622 if (*cpt_iter >= lncpt)
3625 /* get the current table */
3626 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3627 /* if the ptable is NULL then there are no more cpts to examine */
3628 if (peer_table == NULL)
3631 lnet_net_lock(*cpt_iter);
3633 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3634 struct list_head *peers = &peer_table->pt_hash[j];
3636 list_for_each_entry(lp, peers, lpni_hashlist) {
3637 if (peer_index-- > 0)
3640 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3641 if (lnet_isrouter(lp) ||
3642 lnet_peer_aliveness_enabled(lp))
3643 snprintf(aliveness, LNET_MAX_STR_LEN,
3644 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3646 *nid = lp->lpni_nid;
3647 *refcount = atomic_read(&lp->lpni_refcount);
3648 *ni_peer_tx_credits =
3649 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3650 *peer_tx_credits = lp->lpni_txcredits;
3651 *peer_rtr_credits = lp->lpni_rtrcredits;
3652 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3653 *peer_tx_qnob = lp->lpni_txqnob;
3659 lnet_net_unlock(*cpt_iter);
3663 return found ? 0 : -ENOENT;
3666 /* ln_api_mutex is held, which keeps the peer list stable */
3667 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3669 struct lnet_ioctl_element_stats *lpni_stats;
3670 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3671 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3672 struct lnet_peer_ni_credit_info *lpni_info;
3673 struct lnet_peer_ni *lpni;
3674 struct lnet_peer *lp;
3679 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3686 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3687 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3688 size *= lp->lp_nnis;
3689 if (size > cfg->prcfg_size) {
3690 cfg->prcfg_size = size;
3695 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3696 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3697 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3698 cfg->prcfg_count = lp->lp_nnis;
3699 cfg->prcfg_size = size;
3700 cfg->prcfg_state = lp->lp_state;
3702 /* Allocate helper buffers. */
3704 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3707 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3710 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3711 if (!lpni_msg_stats)
3712 goto out_free_stats;
3713 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3715 goto out_free_msg_stats;
3720 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3721 nid = lpni->lpni_nid;
3722 if (copy_to_user(bulk, &nid, sizeof(nid)))
3723 goto out_free_hstats;
3724 bulk += sizeof(nid);
3726 memset(lpni_info, 0, sizeof(*lpni_info));
3727 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3728 if (lnet_isrouter(lpni) ||
3729 lnet_peer_aliveness_enabled(lpni))
3730 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3731 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3733 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3734 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3735 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3736 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3737 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3738 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3739 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3740 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3741 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3742 goto out_free_hstats;
3743 bulk += sizeof(*lpni_info);
3745 memset(lpni_stats, 0, sizeof(*lpni_stats));
3746 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3747 LNET_STATS_TYPE_SEND);
3748 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3749 LNET_STATS_TYPE_RECV);
3750 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3751 LNET_STATS_TYPE_DROP);
3752 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3753 goto out_free_hstats;
3754 bulk += sizeof(*lpni_stats);
3755 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3756 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3757 goto out_free_hstats;
3758 bulk += sizeof(*lpni_msg_stats);
3759 lpni_hstats->hlpni_network_timeout =
3760 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3761 lpni_hstats->hlpni_remote_dropped =
3762 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3763 lpni_hstats->hlpni_remote_timeout =
3764 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3765 lpni_hstats->hlpni_remote_error =
3766 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3767 lpni_hstats->hlpni_health_value =
3768 atomic_read(&lpni->lpni_healthv);
3769 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3770 goto out_free_hstats;
3771 bulk += sizeof(*lpni_hstats);
3776 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3778 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3780 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3782 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3784 lnet_peer_decref_locked(lp);
3790 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3792 /* the mt could've shutdown and cleaned up the queues */
3793 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3796 if (list_empty(&lpni->lpni_recovery) &&
3797 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3798 CDEBUG(D_NET, "lpni %s added to recovery queue. Health = %d\n",
3799 libcfs_nid2str(lpni->lpni_nid),
3800 atomic_read(&lpni->lpni_healthv));
3801 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3802 lnet_peer_ni_addref_locked(lpni);
3806 /* Call with the ln_api_mutex held */
3808 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3810 struct lnet_peer_table *ptable;
3811 struct lnet_peer *lp;
3812 struct lnet_peer_net *lpn;
3813 struct lnet_peer_ni *lpni;
3817 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3821 lnet_net_lock(LNET_LOCK_EX);
3822 lpni = lnet_find_peer_ni_locked(nid);
3824 lnet_net_unlock(LNET_LOCK_EX);
3827 atomic_set(&lpni->lpni_healthv, value);
3828 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3829 lnet_peer_ni_decref_locked(lpni);
3830 lnet_net_unlock(LNET_LOCK_EX);
3834 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3837 * Walk all the peers and reset the healhv for each one to the
3840 lnet_net_lock(LNET_LOCK_EX);
3841 for (cpt = 0; cpt < lncpt; cpt++) {
3842 ptable = the_lnet.ln_peer_tables[cpt];
3843 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3844 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3845 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3847 atomic_set(&lpni->lpni_healthv, value);
3848 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3853 lnet_net_unlock(LNET_LOCK_EX);