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);
171 spin_lock_init(&lpni->lpni_lock);
173 if (lnet_peers_start_down())
174 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
176 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
177 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
178 lpni->lpni_nid = nid;
179 lpni->lpni_cpt = cpt;
180 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
182 net = lnet_get_net_locked(LNET_NIDNET(nid));
183 lpni->lpni_net = net;
185 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
186 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
187 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
188 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
191 * This peer_ni is not on a local network, so we
192 * cannot add the credits here. In case the net is
193 * added later, add the peer_ni to the remote peer ni
194 * list so it can be easily found and revisited.
196 /* FIXME: per-net implementation instead? */
197 atomic_inc(&lpni->lpni_refcount);
198 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
199 &the_lnet.ln_remote_peer_ni_list);
202 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
207 static struct lnet_peer_net *
208 lnet_peer_net_alloc(__u32 net_id)
210 struct lnet_peer_net *lpn;
212 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
216 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
217 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
218 lpn->lpn_net_id = net_id;
220 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
226 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
228 struct lnet_peer *lp;
230 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
232 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
233 LASSERT(list_empty(&lpn->lpn_peer_nis));
234 LASSERT(list_empty(&lpn->lpn_peer_nets));
236 lpn->lpn_peer = NULL;
237 LIBCFS_FREE(lpn, sizeof(*lpn));
239 lnet_peer_decref_locked(lp);
242 static struct lnet_peer *
243 lnet_peer_alloc(lnet_nid_t nid)
245 struct lnet_peer *lp;
247 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
251 INIT_LIST_HEAD(&lp->lp_rtrq);
252 INIT_LIST_HEAD(&lp->lp_routes);
253 INIT_LIST_HEAD(&lp->lp_peer_list);
254 INIT_LIST_HEAD(&lp->lp_peer_nets);
255 INIT_LIST_HEAD(&lp->lp_dc_list);
256 INIT_LIST_HEAD(&lp->lp_dc_pendq);
257 INIT_LIST_HEAD(&lp->lp_rtr_list);
258 init_waitqueue_head(&lp->lp_dc_waitq);
259 spin_lock_init(&lp->lp_lock);
260 lp->lp_primary_nid = nid;
261 if (lnet_peers_start_down())
262 lp->lp_alive = false;
267 * all peers created on a router should have health on
268 * if it's not already on.
270 if (the_lnet.ln_routing && !lnet_health_sensitivity)
271 lp->lp_health_sensitivity = 1;
274 * Turn off discovery for loopback peer. If you're creating a peer
275 * for the loopback interface then that was initiated when we
276 * attempted to send a message over the loopback. There is no need
277 * to ever use a different interface when sending messages to
280 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
281 lp->lp_state = LNET_PEER_NO_DISCOVERY;
282 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
284 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
290 lnet_destroy_peer_locked(struct lnet_peer *lp)
292 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
294 LASSERT(atomic_read(&lp->lp_refcount) == 0);
295 LASSERT(lp->lp_rtr_refcount == 0);
296 LASSERT(list_empty(&lp->lp_peer_nets));
297 LASSERT(list_empty(&lp->lp_peer_list));
298 LASSERT(list_empty(&lp->lp_dc_list));
301 lnet_ping_buffer_decref(lp->lp_data);
304 * if there are messages still on the pending queue, then make
305 * sure to queue them on the ln_msg_resend list so they can be
306 * resent at a later point if the discovery thread is still
308 * If the discovery thread has stopped, then the wakeup will be a
309 * no-op, and it is expected the lnet_shutdown_lndnets() will
310 * eventually be called, which will traverse this list and
311 * finalize the messages on the list.
312 * We can not resend them now because we're holding the cpt lock.
313 * Releasing the lock can cause an inconsistent state
315 spin_lock(&the_lnet.ln_msg_resend_lock);
316 spin_lock(&lp->lp_lock);
317 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
318 spin_unlock(&lp->lp_lock);
319 spin_unlock(&the_lnet.ln_msg_resend_lock);
320 wake_up(&the_lnet.ln_dc_waitq);
322 LIBCFS_FREE(lp, sizeof(*lp));
326 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
327 * that peer_net, detach the peer_net from the peer.
329 * Call with lnet_net_lock/EX held
332 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
334 struct lnet_peer_table *ptable;
335 struct lnet_peer_net *lpn;
336 struct lnet_peer *lp;
339 * Belts and suspenders: gracefully handle teardown of a
340 * partially connected peer_ni.
342 lpn = lpni->lpni_peer_net;
344 list_del_init(&lpni->lpni_peer_nis);
346 * If there are no lpni's left, we detach lpn from
347 * lp_peer_nets, so it cannot be found anymore.
349 if (list_empty(&lpn->lpn_peer_nis))
350 list_del_init(&lpn->lpn_peer_nets);
352 /* Update peer NID count. */
357 * If there are no more peer nets, make the peer unfindable
358 * via the peer_tables.
360 * Otherwise, if the peer is DISCOVERED, tell discovery to
361 * take another look at it. This is a no-op if discovery for
362 * this peer did the detaching.
364 if (list_empty(&lp->lp_peer_nets)) {
365 list_del_init(&lp->lp_peer_list);
366 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
368 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
369 /* Discovery isn't running, nothing to do here. */
370 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
371 lnet_peer_queue_for_discovery(lp);
372 wake_up(&the_lnet.ln_dc_waitq);
374 CDEBUG(D_NET, "peer %s NID %s\n",
375 libcfs_nid2str(lp->lp_primary_nid),
376 libcfs_nid2str(lpni->lpni_nid));
379 /* called with lnet_net_lock LNET_LOCK_EX held */
381 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
383 struct lnet_peer_table *ptable = NULL;
385 /* don't remove a peer_ni if it's also a gateway */
386 if (lnet_isrouter(lpni) && !force) {
387 CERROR("Peer NI %s is a gateway. Can not delete it\n",
388 libcfs_nid2str(lpni->lpni_nid));
392 lnet_peer_remove_from_remote_list(lpni);
394 /* remove peer ni from the hash list. */
395 list_del_init(&lpni->lpni_hashlist);
398 * indicate the peer is being deleted so the monitor thread can
399 * remove it from the recovery queue.
401 spin_lock(&lpni->lpni_lock);
402 lpni->lpni_state |= LNET_PEER_NI_DELETING;
403 spin_unlock(&lpni->lpni_lock);
405 /* decrement the ref count on the peer table */
406 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
409 * The peer_ni can no longer be found with a lookup. But there
410 * can be current users, so keep track of it on the zombie
411 * list until the reference count has gone to zero.
413 * The last reference may be lost in a place where the
414 * lnet_net_lock locks only a single cpt, and that cpt may not
415 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
418 spin_lock(&ptable->pt_zombie_lock);
419 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
420 ptable->pt_zombies++;
421 spin_unlock(&ptable->pt_zombie_lock);
423 /* no need to keep this peer_ni on the hierarchy anymore */
424 lnet_peer_detach_peer_ni_locked(lpni);
426 /* remove hashlist reference on peer_ni */
427 lnet_peer_ni_decref_locked(lpni);
432 void lnet_peer_uninit(void)
434 struct lnet_peer_ni *lpni, *tmp;
436 lnet_net_lock(LNET_LOCK_EX);
438 /* remove all peer_nis from the remote peer and the hash list */
439 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
440 lpni_on_remote_peer_ni_list)
441 lnet_peer_ni_del_locked(lpni, false);
443 lnet_peer_tables_destroy();
445 lnet_net_unlock(LNET_LOCK_EX);
449 lnet_peer_del_locked(struct lnet_peer *peer)
451 struct lnet_peer_ni *lpni = NULL, *lpni2;
454 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
456 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
457 while (lpni != NULL) {
458 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
459 rc = lnet_peer_ni_del_locked(lpni, false);
469 lnet_peer_del(struct lnet_peer *peer)
471 lnet_net_lock(LNET_LOCK_EX);
472 lnet_peer_del_locked(peer);
473 lnet_net_unlock(LNET_LOCK_EX);
479 * Delete a NID from a peer. Call with ln_api_mutex held.
482 * -EPERM: Non-DLC deletion from DLC-configured peer.
483 * -ENOENT: No lnet_peer_ni corresponding to the nid.
484 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
485 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
488 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
490 struct lnet_peer_ni *lpni;
491 lnet_nid_t primary_nid = lp->lp_primary_nid;
493 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
495 if (!(flags & LNET_PEER_CONFIGURED)) {
496 if (lp->lp_state & LNET_PEER_CONFIGURED) {
501 lpni = lnet_find_peer_ni_locked(nid);
506 lnet_peer_ni_decref_locked(lpni);
507 if (lp != lpni->lpni_peer_net->lpn_peer) {
513 * This function only allows deletion of the primary NID if it
516 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
521 lnet_net_lock(LNET_LOCK_EX);
523 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
524 struct lnet_peer_ni *lpni2;
525 /* assign the next peer_ni to be the primary */
526 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
528 lp->lp_primary_nid = lpni->lpni_nid;
530 rc = lnet_peer_ni_del_locked(lpni, force);
532 lnet_net_unlock(LNET_LOCK_EX);
535 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
536 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
542 lnet_peer_table_cleanup_locked(struct lnet_net *net,
543 struct lnet_peer_table *ptable)
546 struct lnet_peer_ni *next;
547 struct lnet_peer_ni *lpni;
548 struct lnet_peer *peer;
550 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
551 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
553 if (net != NULL && net != lpni->lpni_net)
556 peer = lpni->lpni_peer_net->lpn_peer;
557 if (peer->lp_primary_nid != lpni->lpni_nid) {
558 lnet_peer_ni_del_locked(lpni, false);
562 * Removing the primary NID implies removing
563 * the entire peer. Advance next beyond any
564 * peer_ni that belongs to the same peer.
566 list_for_each_entry_from(next, &ptable->pt_hash[i],
568 if (next->lpni_peer_net->lpn_peer != peer)
571 lnet_peer_del_locked(peer);
577 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
579 wait_var_event_warning(&ptable->pt_zombies,
580 ptable->pt_zombies == 0,
581 "Waiting for %d zombies on peer table\n",
586 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
587 struct lnet_peer_table *ptable)
589 struct lnet_peer_ni *lp;
590 struct lnet_peer_ni *tmp;
594 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
595 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
597 if (net != lp->lpni_net)
600 if (!lnet_isrouter(lp))
603 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
605 lnet_net_unlock(LNET_LOCK_EX);
606 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
607 lnet_net_lock(LNET_LOCK_EX);
613 lnet_peer_tables_cleanup(struct lnet_net *net)
616 struct lnet_peer_table *ptable;
618 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
619 /* If just deleting the peers for a NI, get rid of any routes these
620 * peers are gateways for. */
621 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
622 lnet_net_lock(LNET_LOCK_EX);
623 lnet_peer_table_del_rtrs_locked(net, ptable);
624 lnet_net_unlock(LNET_LOCK_EX);
627 /* Start the cleanup process */
628 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
629 lnet_net_lock(LNET_LOCK_EX);
630 lnet_peer_table_cleanup_locked(net, ptable);
631 lnet_net_unlock(LNET_LOCK_EX);
634 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
635 lnet_peer_ni_finalize_wait(ptable);
638 static struct lnet_peer_ni *
639 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
641 struct list_head *peers;
642 struct lnet_peer_ni *lp;
644 if (the_lnet.ln_state != LNET_STATE_RUNNING)
647 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
648 list_for_each_entry(lp, peers, lpni_hashlist) {
649 if (lp->lpni_nid == nid) {
650 lnet_peer_ni_addref_locked(lp);
658 struct lnet_peer_ni *
659 lnet_find_peer_ni_locked(lnet_nid_t nid)
661 struct lnet_peer_ni *lpni;
662 struct lnet_peer_table *ptable;
665 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
667 ptable = the_lnet.ln_peer_tables[cpt];
668 lpni = lnet_get_peer_ni_locked(ptable, nid);
673 struct lnet_peer_ni *
674 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
676 struct lnet_peer_net *lpn;
677 struct lnet_peer_ni *lpni;
679 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
683 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
684 if (lpni->lpni_nid == nid)
692 lnet_find_peer(lnet_nid_t nid)
694 struct lnet_peer_ni *lpni;
695 struct lnet_peer *lp = NULL;
698 cpt = lnet_net_lock_current();
699 lpni = lnet_find_peer_ni_locked(nid);
701 lp = lpni->lpni_peer_net->lpn_peer;
702 lnet_peer_addref_locked(lp);
703 lnet_peer_ni_decref_locked(lpni);
705 lnet_net_unlock(cpt);
710 struct lnet_peer_net *
711 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
713 struct lnet_peer_net *net;
716 /* no net id provided return the first net */
717 net = list_first_entry_or_null(&lp->lp_peer_nets,
718 struct lnet_peer_net,
724 /* find the net after the one provided */
725 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
726 if (net->lpn_net_id == prev_lpn_id) {
728 * if we reached the end of the list loop to the
731 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
732 return list_first_entry_or_null(&lp->lp_peer_nets,
733 struct lnet_peer_net,
736 return list_next_entry(net, lpn_peer_nets);
743 struct lnet_peer_ni *
744 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
745 struct lnet_peer_net *peer_net,
746 struct lnet_peer_ni *prev)
748 struct lnet_peer_ni *lpni;
749 struct lnet_peer_net *net = peer_net;
753 if (list_empty(&peer->lp_peer_nets))
756 net = list_entry(peer->lp_peer_nets.next,
757 struct lnet_peer_net,
760 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
766 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
768 * if you reached the end of the peer ni list and the peer
769 * net is specified then there are no more peer nis in that
776 * we reached the end of this net ni list. move to the
779 if (prev->lpni_peer_net->lpn_peer_nets.next ==
781 /* no more nets and no more NIs. */
784 /* get the next net */
785 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
786 struct lnet_peer_net,
788 /* get the ni on it */
789 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
795 /* there are more nis left */
796 lpni = list_entry(prev->lpni_peer_nis.next,
797 struct lnet_peer_ni, lpni_peer_nis);
802 /* Call with the ln_api_mutex held */
803 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
805 struct lnet_process_id id;
806 struct lnet_peer_table *ptable;
807 struct lnet_peer *lp;
816 if (the_lnet.ln_state != LNET_STATE_RUNNING)
819 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
822 * Count the number of peers, and return E2BIG if the buffer
823 * is too small. We'll also return the desired size.
826 for (cpt = 0; cpt < lncpt; cpt++) {
827 ptable = the_lnet.ln_peer_tables[cpt];
828 count += ptable->pt_peers;
830 size = count * sizeof(*ids);
835 * Walk the peer lists and copy out the primary nids.
836 * This is safe because the peer lists are only modified
837 * while the ln_api_mutex is held. So we don't need to
838 * hold the lnet_net_lock as well, and can therefore
839 * directly call copy_to_user().
842 memset(&id, 0, sizeof(id));
843 id.pid = LNET_PID_LUSTRE;
845 for (cpt = 0; cpt < lncpt; cpt++) {
846 ptable = the_lnet.ln_peer_tables[cpt];
847 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
850 id.nid = lp->lp_primary_nid;
851 if (copy_to_user(&ids[i], &id, sizeof(id)))
864 * Start pushes to peers that need to be updated for a configuration
865 * change on this node.
868 lnet_push_update_to_peers(int force)
870 struct lnet_peer_table *ptable;
871 struct lnet_peer *lp;
875 lnet_net_lock(LNET_LOCK_EX);
876 if (lnet_peer_discovery_disabled)
878 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
879 for (cpt = 0; cpt < lncpt; cpt++) {
880 ptable = the_lnet.ln_peer_tables[cpt];
881 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
883 spin_lock(&lp->lp_lock);
884 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
885 lp->lp_state |= LNET_PEER_FORCE_PUSH;
886 spin_unlock(&lp->lp_lock);
888 if (lnet_peer_needs_push(lp))
889 lnet_peer_queue_for_discovery(lp);
892 lnet_net_unlock(LNET_LOCK_EX);
893 wake_up(&the_lnet.ln_dc_waitq);
897 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
898 * this is a preferred point-to-point path. Call with lnet_net_lock in
902 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
906 if (lpni->lpni_pref_nnids == 0)
908 if (lpni->lpni_pref_nnids == 1)
909 return lpni->lpni_pref.nid == nid;
910 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
911 if (lpni->lpni_pref.nids[i] == nid)
918 * Set a single ni as preferred, provided no preferred ni is already
919 * defined. Only to be used for non-multi-rail peer_ni.
922 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
926 spin_lock(&lpni->lpni_lock);
927 if (nid == LNET_NID_ANY) {
929 } else if (lpni->lpni_pref_nnids > 0) {
931 } else if (lpni->lpni_pref_nnids == 0) {
932 lpni->lpni_pref.nid = nid;
933 lpni->lpni_pref_nnids = 1;
934 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
936 spin_unlock(&lpni->lpni_lock);
938 CDEBUG(D_NET, "peer %s nid %s: %d\n",
939 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
944 * Clear the preferred NID from a non-multi-rail peer_ni, provided
945 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
948 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
952 spin_lock(&lpni->lpni_lock);
953 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
954 lpni->lpni_pref_nnids = 0;
955 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
956 } else if (lpni->lpni_pref_nnids == 0) {
961 spin_unlock(&lpni->lpni_lock);
963 CDEBUG(D_NET, "peer %s: %d\n",
964 libcfs_nid2str(lpni->lpni_nid), rc);
969 * Clear the preferred NIDs from a non-multi-rail peer.
972 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
974 struct lnet_peer_ni *lpni = NULL;
976 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
977 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
981 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
983 lnet_nid_t *nids = NULL;
984 lnet_nid_t *oldnids = NULL;
985 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
990 if (nid == LNET_NID_ANY) {
995 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1000 /* A non-MR node may have only one preferred NI per peer_ni */
1001 if (lpni->lpni_pref_nnids > 0) {
1002 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1008 if (lpni->lpni_pref_nnids != 0) {
1009 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1010 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1015 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
1016 if (lpni->lpni_pref.nids[i] == nid) {
1017 LIBCFS_FREE(nids, size);
1021 nids[i] = lpni->lpni_pref.nids[i];
1026 lnet_net_lock(LNET_LOCK_EX);
1027 spin_lock(&lpni->lpni_lock);
1028 if (lpni->lpni_pref_nnids == 0) {
1029 lpni->lpni_pref.nid = nid;
1031 oldnids = lpni->lpni_pref.nids;
1032 lpni->lpni_pref.nids = nids;
1034 lpni->lpni_pref_nnids++;
1035 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1036 spin_unlock(&lpni->lpni_lock);
1037 lnet_net_unlock(LNET_LOCK_EX);
1040 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1041 CFS_FREE_PTR_ARRAY(oldnids, size);
1044 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1045 spin_lock(&lpni->lpni_lock);
1046 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1047 spin_unlock(&lpni->lpni_lock);
1049 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1050 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1055 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1057 lnet_nid_t *nids = NULL;
1058 lnet_nid_t *oldnids = NULL;
1059 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1064 if (lpni->lpni_pref_nnids == 0) {
1069 if (lpni->lpni_pref_nnids == 1) {
1070 if (lpni->lpni_pref.nid != nid) {
1074 } else if (lpni->lpni_pref_nnids == 2) {
1075 if (lpni->lpni_pref.nids[0] != nid &&
1076 lpni->lpni_pref.nids[1] != nid) {
1081 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1082 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1087 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1088 if (lpni->lpni_pref.nids[i] != nid)
1090 nids[j++] = lpni->lpni_pref.nids[i];
1092 /* Check if we actually removed a nid. */
1093 if (j == lpni->lpni_pref_nnids) {
1094 LIBCFS_FREE(nids, size);
1100 lnet_net_lock(LNET_LOCK_EX);
1101 spin_lock(&lpni->lpni_lock);
1102 if (lpni->lpni_pref_nnids == 1) {
1103 lpni->lpni_pref.nid = LNET_NID_ANY;
1104 } else if (lpni->lpni_pref_nnids == 2) {
1105 oldnids = lpni->lpni_pref.nids;
1106 if (oldnids[0] == nid)
1107 lpni->lpni_pref.nid = oldnids[1];
1109 lpni->lpni_pref.nid = oldnids[2];
1111 oldnids = lpni->lpni_pref.nids;
1112 lpni->lpni_pref.nids = nids;
1114 lpni->lpni_pref_nnids--;
1115 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1116 spin_unlock(&lpni->lpni_lock);
1117 lnet_net_unlock(LNET_LOCK_EX);
1120 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1121 CFS_FREE_PTR_ARRAY(oldnids, size);
1124 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1125 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1130 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1132 struct lnet_peer_ni *lpni;
1133 lnet_nid_t primary_nid = nid;
1135 lpni = lnet_find_peer_ni_locked(nid);
1137 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1138 lnet_peer_ni_decref_locked(lpni);
1145 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1146 __must_hold(&lp->lp_lock)
1148 if (lnet_peer_discovery_disabled)
1151 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1152 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1163 lnet_is_discovery_disabled(struct lnet_peer *lp)
1167 spin_lock(&lp->lp_lock);
1168 rc = lnet_is_discovery_disabled_locked(lp);
1169 spin_unlock(&lp->lp_lock);
1175 LNetPrimaryNID(lnet_nid_t nid)
1177 struct lnet_peer *lp;
1178 struct lnet_peer_ni *lpni;
1179 lnet_nid_t primary_nid = nid;
1183 cpt = lnet_net_lock_current();
1184 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1189 lp = lpni->lpni_peer_net->lpn_peer;
1191 while (!lnet_peer_is_uptodate(lp)) {
1192 rc = lnet_discover_peer_locked(lpni, cpt, true);
1195 lp = lpni->lpni_peer_net->lpn_peer;
1197 /* Only try once if discovery is disabled */
1198 if (lnet_is_discovery_disabled(lp))
1201 primary_nid = lp->lp_primary_nid;
1203 lnet_peer_ni_decref_locked(lpni);
1205 lnet_net_unlock(cpt);
1207 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1208 libcfs_nid2str(primary_nid), rc);
1211 EXPORT_SYMBOL(LNetPrimaryNID);
1213 struct lnet_peer_net *
1214 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1216 struct lnet_peer_net *peer_net;
1217 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1218 if (peer_net->lpn_net_id == net_id)
1225 * Attach a peer_ni to a peer_net and peer. This function assumes
1226 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1227 * may be attached to a different peer, in which case it will be
1228 * properly detached first. The whole operation is done atomically.
1230 * Always returns 0. This is the last function called from functions
1231 * that do return an int, so returning 0 here allows the compiler to
1235 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1236 struct lnet_peer_net *lpn,
1237 struct lnet_peer_ni *lpni,
1240 struct lnet_peer_table *ptable;
1242 /* Install the new peer_ni */
1243 lnet_net_lock(LNET_LOCK_EX);
1244 /* Add peer_ni to global peer table hash, if necessary. */
1245 if (list_empty(&lpni->lpni_hashlist)) {
1246 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1248 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1249 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1250 ptable->pt_version++;
1251 /* This is the 1st refcount on lpni. */
1252 atomic_inc(&lpni->lpni_refcount);
1255 /* Detach the peer_ni from an existing peer, if necessary. */
1256 if (lpni->lpni_peer_net) {
1257 LASSERT(lpni->lpni_peer_net != lpn);
1258 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1259 lnet_peer_detach_peer_ni_locked(lpni);
1260 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1261 lpni->lpni_peer_net = NULL;
1264 /* Add peer_ni to peer_net */
1265 lpni->lpni_peer_net = lpn;
1266 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1267 lnet_update_peer_net_healthv(lpni);
1268 lnet_peer_net_addref_locked(lpn);
1270 /* Add peer_net to peer */
1271 if (!lpn->lpn_peer) {
1273 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1274 lnet_peer_addref_locked(lp);
1277 /* Add peer to global peer list, if necessary */
1278 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1279 if (list_empty(&lp->lp_peer_list)) {
1280 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1285 /* Update peer state */
1286 spin_lock(&lp->lp_lock);
1287 if (flags & LNET_PEER_CONFIGURED) {
1288 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1289 lp->lp_state |= LNET_PEER_CONFIGURED;
1291 if (flags & LNET_PEER_MULTI_RAIL) {
1292 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1293 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1294 lnet_peer_clr_non_mr_pref_nids(lp);
1297 spin_unlock(&lp->lp_lock);
1300 lnet_net_unlock(LNET_LOCK_EX);
1302 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1303 libcfs_nid2str(lp->lp_primary_nid),
1304 libcfs_nid2str(lpni->lpni_nid), flags);
1310 * Create a new peer, with nid as its primary nid.
1312 * Call with the lnet_api_mutex held.
1315 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1317 struct lnet_peer *lp;
1318 struct lnet_peer_net *lpn;
1319 struct lnet_peer_ni *lpni;
1322 LASSERT(nid != LNET_NID_ANY);
1325 * No need for the lnet_net_lock here, because the
1326 * lnet_api_mutex is held.
1328 lpni = lnet_find_peer_ni_locked(nid);
1330 /* A peer with this NID already exists. */
1331 lp = lpni->lpni_peer_net->lpn_peer;
1332 lnet_peer_ni_decref_locked(lpni);
1334 * This is an error if the peer was configured and the
1335 * primary NID differs or an attempt is made to change
1336 * the Multi-Rail flag. Otherwise the assumption is
1337 * that an existing peer is being modified.
1339 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1340 if (lp->lp_primary_nid != nid)
1342 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1346 /* Delete and recreate as a configured peer. */
1350 /* Create peer, peer_net, and peer_ni. */
1352 lp = lnet_peer_alloc(nid);
1355 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1358 lpni = lnet_peer_ni_alloc(nid);
1362 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1365 LIBCFS_FREE(lpn, sizeof(*lpn));
1367 LIBCFS_FREE(lp, sizeof(*lp));
1369 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1370 libcfs_nid2str(nid), flags, rc);
1375 * Add a NID to a peer. Call with ln_api_mutex held.
1378 * -EPERM: Non-DLC addition to a DLC-configured peer.
1379 * -EEXIST: The NID was configured by DLC for a different peer.
1380 * -ENOMEM: Out of memory.
1381 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1382 * non-multi-rail peer.
1385 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1387 struct lnet_peer_net *lpn;
1388 struct lnet_peer_ni *lpni;
1392 LASSERT(nid != LNET_NID_ANY);
1394 /* A configured peer can only be updated through configuration. */
1395 if (!(flags & LNET_PEER_CONFIGURED)) {
1396 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1403 * The MULTI_RAIL flag can be set but not cleared, because
1404 * that would leave the peer struct in an invalid state.
1406 if (flags & LNET_PEER_MULTI_RAIL) {
1407 spin_lock(&lp->lp_lock);
1408 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1409 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1410 lnet_peer_clr_non_mr_pref_nids(lp);
1412 spin_unlock(&lp->lp_lock);
1413 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1418 lpni = lnet_find_peer_ni_locked(nid);
1421 * A peer_ni already exists. This is only a problem if
1422 * it is not connected to this peer and was configured
1425 lnet_peer_ni_decref_locked(lpni);
1426 if (lpni->lpni_peer_net->lpn_peer == lp)
1428 if (lnet_peer_ni_is_configured(lpni)) {
1432 /* If this is the primary NID, destroy the peer. */
1433 if (lnet_peer_ni_is_primary(lpni)) {
1434 struct lnet_peer *rtr_lp =
1435 lpni->lpni_peer_net->lpn_peer;
1436 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1438 * if we're trying to delete a router it means
1439 * we're moving this peer NI to a new peer so must
1440 * transfer router properties to the new peer
1442 if (rtr_refcount > 0) {
1443 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1444 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1446 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1447 lpni = lnet_peer_ni_alloc(nid);
1454 lpni = lnet_peer_ni_alloc(nid);
1462 * Get the peer_net. Check that we're not adding a second
1463 * peer_ni on a peer_net of a non-multi-rail peer.
1465 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1467 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1472 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1477 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1480 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1481 if (!lpni->lpni_peer_net)
1482 LIBCFS_FREE(lpni, sizeof(*lpni));
1484 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1485 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1491 * Update the primary NID of a peer, if possible.
1493 * Call with the lnet_api_mutex held.
1496 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1498 lnet_nid_t old = lp->lp_primary_nid;
1501 if (lp->lp_primary_nid == nid)
1503 rc = lnet_peer_add_nid(lp, nid, flags);
1506 lp->lp_primary_nid = nid;
1508 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1509 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1514 * lpni creation initiated due to traffic either sending or receiving.
1517 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1519 struct lnet_peer *lp;
1520 struct lnet_peer_net *lpn;
1521 struct lnet_peer_ni *lpni;
1525 if (nid == LNET_NID_ANY) {
1530 /* lnet_net_lock is not needed here because ln_api_lock is held */
1531 lpni = lnet_find_peer_ni_locked(nid);
1534 * We must have raced with another thread. Since we
1535 * know next to nothing about a peer_ni created by
1536 * traffic, we just assume everything is ok and
1539 lnet_peer_ni_decref_locked(lpni);
1543 /* Create peer, peer_net, and peer_ni. */
1545 lp = lnet_peer_alloc(nid);
1548 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1551 lpni = lnet_peer_ni_alloc(nid);
1554 if (pref != LNET_NID_ANY)
1555 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1557 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1560 LIBCFS_FREE(lpn, sizeof(*lpn));
1562 LIBCFS_FREE(lp, sizeof(*lp));
1564 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1569 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1571 * This API handles the following combinations:
1572 * Create a peer with its primary NI if only the prim_nid is provided
1573 * Add a NID to a peer identified by the prim_nid. The peer identified
1574 * by the prim_nid must already exist.
1575 * The peer being created may be non-MR.
1577 * The caller must hold ln_api_mutex. This prevents the peer from
1578 * being created/modified/deleted by a different thread.
1581 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1583 struct lnet_peer *lp = NULL;
1584 struct lnet_peer_ni *lpni;
1587 /* The prim_nid must always be specified */
1588 if (prim_nid == LNET_NID_ANY)
1591 flags = LNET_PEER_CONFIGURED;
1593 flags |= LNET_PEER_MULTI_RAIL;
1596 * If nid isn't specified, we must create a new peer with
1597 * prim_nid as its primary nid.
1599 if (nid == LNET_NID_ANY)
1600 return lnet_peer_add(prim_nid, flags);
1602 /* Look up the prim_nid, which must exist. */
1603 lpni = lnet_find_peer_ni_locked(prim_nid);
1606 lnet_peer_ni_decref_locked(lpni);
1607 lp = lpni->lpni_peer_net->lpn_peer;
1609 /* Peer must have been configured. */
1610 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1611 CDEBUG(D_NET, "peer %s was not configured\n",
1612 libcfs_nid2str(prim_nid));
1616 /* Primary NID must match */
1617 if (lp->lp_primary_nid != prim_nid) {
1618 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1619 libcfs_nid2str(prim_nid),
1620 libcfs_nid2str(lp->lp_primary_nid));
1624 /* Multi-Rail flag must match. */
1625 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1626 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1627 libcfs_nid2str(prim_nid));
1631 return lnet_peer_add_nid(lp, nid, flags);
1635 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1637 * This API handles the following combinations:
1638 * Delete a NI from a peer if both prim_nid and nid are provided.
1639 * Delete a peer if only prim_nid is provided.
1640 * Delete a peer if its primary nid is provided.
1642 * The caller must hold ln_api_mutex. This prevents the peer from
1643 * being modified/deleted by a different thread.
1646 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1648 struct lnet_peer *lp;
1649 struct lnet_peer_ni *lpni;
1652 if (prim_nid == LNET_NID_ANY)
1655 lpni = lnet_find_peer_ni_locked(prim_nid);
1658 lnet_peer_ni_decref_locked(lpni);
1659 lp = lpni->lpni_peer_net->lpn_peer;
1661 if (prim_nid != lp->lp_primary_nid) {
1662 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1663 libcfs_nid2str(prim_nid),
1664 libcfs_nid2str(lp->lp_primary_nid));
1668 lnet_net_lock(LNET_LOCK_EX);
1669 if (lp->lp_rtr_refcount > 0) {
1670 lnet_net_unlock(LNET_LOCK_EX);
1671 CERROR("%s is a router. Can not be deleted\n",
1672 libcfs_nid2str(prim_nid));
1675 lnet_net_unlock(LNET_LOCK_EX);
1677 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1678 return lnet_peer_del(lp);
1680 flags = LNET_PEER_CONFIGURED;
1681 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1682 flags |= LNET_PEER_MULTI_RAIL;
1684 return lnet_peer_del_nid(lp, nid, flags);
1688 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1690 struct lnet_peer_table *ptable;
1691 struct lnet_peer_net *lpn;
1693 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1695 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1696 LASSERT(list_empty(&lpni->lpni_txq));
1697 LASSERT(lpni->lpni_txqnob == 0);
1698 LASSERT(list_empty(&lpni->lpni_peer_nis));
1699 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1701 lpn = lpni->lpni_peer_net;
1702 lpni->lpni_peer_net = NULL;
1703 lpni->lpni_net = NULL;
1705 /* remove the peer ni from the zombie list */
1706 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1707 spin_lock(&ptable->pt_zombie_lock);
1708 list_del_init(&lpni->lpni_hashlist);
1709 ptable->pt_zombies--;
1710 spin_unlock(&ptable->pt_zombie_lock);
1712 if (lpni->lpni_pref_nnids > 1)
1713 CFS_FREE_PTR_ARRAY(lpni->lpni_pref.nids, lpni->lpni_pref_nnids);
1715 LIBCFS_FREE(lpni, sizeof(*lpni));
1717 lnet_peer_net_decref_locked(lpn);
1720 struct lnet_peer_ni *
1721 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1723 struct lnet_peer_ni *lpni = NULL;
1726 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1727 return ERR_PTR(-ESHUTDOWN);
1730 * find if a peer_ni already exists.
1731 * If so then just return that.
1733 lpni = lnet_find_peer_ni_locked(nid);
1737 lnet_net_unlock(cpt);
1739 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1742 goto out_net_relock;
1745 lpni = lnet_find_peer_ni_locked(nid);
1755 * Get a peer_ni for the given nid, create it if necessary. Takes a
1756 * hold on the peer_ni.
1758 struct lnet_peer_ni *
1759 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1761 struct lnet_peer_ni *lpni = NULL;
1764 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1765 return ERR_PTR(-ESHUTDOWN);
1768 * find if a peer_ni already exists.
1769 * If so then just return that.
1771 lpni = lnet_find_peer_ni_locked(nid);
1777 * use the lnet_api_mutex to serialize the creation of the peer_ni
1778 * and the creation/deletion of the local ni/net. When a local ni is
1779 * created, if there exists a set of peer_nis on that network,
1780 * they need to be traversed and updated. When a local NI is
1781 * deleted, which could result in a network being deleted, then
1782 * all peer nis on that network need to be removed as well.
1784 * Creation through traffic should also be serialized with
1785 * creation through DLC.
1787 lnet_net_unlock(cpt);
1788 mutex_lock(&the_lnet.ln_api_mutex);
1790 * Shutdown is only set under the ln_api_lock, so a single
1791 * check here is sufficent.
1793 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1794 lpni = ERR_PTR(-ESHUTDOWN);
1795 goto out_mutex_unlock;
1798 rc = lnet_peer_ni_traffic_add(nid, pref);
1801 goto out_mutex_unlock;
1804 lpni = lnet_find_peer_ni_locked(nid);
1808 mutex_unlock(&the_lnet.ln_api_mutex);
1811 /* Lock has been dropped, check again for shutdown. */
1812 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1814 lnet_peer_ni_decref_locked(lpni);
1815 lpni = ERR_PTR(-ESHUTDOWN);
1822 lnet_peer_gw_discovery(struct lnet_peer *lp)
1826 spin_lock(&lp->lp_lock);
1827 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1829 spin_unlock(&lp->lp_lock);
1835 lnet_peer_is_uptodate(struct lnet_peer *lp)
1839 spin_lock(&lp->lp_lock);
1840 rc = lnet_peer_is_uptodate_locked(lp);
1841 spin_unlock(&lp->lp_lock);
1846 * Is a peer uptodate from the point of view of discovery?
1848 * If it is currently being processed, obviously not.
1849 * A forced Ping or Push is also handled by the discovery thread.
1851 * Otherwise look at whether the peer needs rediscovering.
1854 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
1855 __must_hold(&lp->lp_lock)
1859 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1860 LNET_PEER_FORCE_PING |
1861 LNET_PEER_FORCE_PUSH)) {
1863 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1865 } else if (lnet_peer_needs_push(lp)) {
1867 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1868 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1880 * Queue a peer for the attention of the discovery thread. Call with
1881 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1882 * -EALREADY if the peer was already queued.
1884 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1888 spin_lock(&lp->lp_lock);
1889 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1890 lp->lp_state |= LNET_PEER_DISCOVERING;
1891 spin_unlock(&lp->lp_lock);
1892 if (list_empty(&lp->lp_dc_list)) {
1893 lnet_peer_addref_locked(lp);
1894 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1895 wake_up(&the_lnet.ln_dc_waitq);
1901 CDEBUG(D_NET, "Queue peer %s: %d\n",
1902 libcfs_nid2str(lp->lp_primary_nid), rc);
1908 * Discovery of a peer is complete. Wake all waiters on the peer.
1909 * Call with lnet_net_lock/EX held.
1911 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1913 struct lnet_msg *msg, *tmp;
1915 LIST_HEAD(pending_msgs);
1917 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1918 libcfs_nid2str(lp->lp_primary_nid));
1920 list_del_init(&lp->lp_dc_list);
1921 spin_lock(&lp->lp_lock);
1922 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1923 spin_unlock(&lp->lp_lock);
1924 wake_up_all(&lp->lp_dc_waitq);
1926 if (lp->lp_rtr_refcount > 0)
1927 lnet_router_discovery_complete(lp);
1929 lnet_net_unlock(LNET_LOCK_EX);
1931 /* iterate through all pending messages and send them again */
1932 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1933 list_del_init(&msg->msg_list);
1934 if (lp->lp_dc_error) {
1935 lnet_finalize(msg, lp->lp_dc_error);
1939 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1940 lnet_msgtyp2str(msg->msg_type),
1941 libcfs_id2str(msg->msg_target));
1942 rc = lnet_send(msg->msg_src_nid_param, msg,
1943 msg->msg_rtr_nid_param);
1945 CNETERR("Error sending %s to %s: %d\n",
1946 lnet_msgtyp2str(msg->msg_type),
1947 libcfs_id2str(msg->msg_target), rc);
1948 lnet_finalize(msg, rc);
1951 lnet_net_lock(LNET_LOCK_EX);
1952 lnet_peer_decref_locked(lp);
1956 * Handle inbound push.
1957 * Like any event handler, called with lnet_res_lock/CPT held.
1959 void lnet_peer_push_event(struct lnet_event *ev)
1961 struct lnet_ping_buffer *pbuf;
1962 struct lnet_peer *lp;
1964 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start + ev->offset);
1966 /* lnet_find_peer() adds a refcount */
1967 lp = lnet_find_peer(ev->source.nid);
1969 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1970 libcfs_nid2str(ev->initiator.nid),
1971 libcfs_nid2str(ev->source.nid));
1972 pbuf->pb_needs_post = true;
1976 /* Ensure peer state remains consistent while we modify it. */
1977 spin_lock(&lp->lp_lock);
1980 * If some kind of error happened the contents of the message
1981 * cannot be used. Clear the NIDS_UPTODATE and set the
1982 * FORCE_PING flag to trigger a ping.
1985 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1986 lp->lp_state |= LNET_PEER_FORCE_PING;
1987 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1989 libcfs_nid2str(lp->lp_primary_nid),
1990 libcfs_nid2str(ev->source.nid));
1995 * A push with invalid or corrupted info. Clear the UPTODATE
1996 * flag to trigger a ping.
1998 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1999 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2000 lp->lp_state |= LNET_PEER_FORCE_PING;
2001 CDEBUG(D_NET, "Corrupted Push from %s\n",
2002 libcfs_nid2str(lp->lp_primary_nid));
2007 * Make sure we'll allocate the correct size ping buffer when
2010 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2011 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2014 * A non-Multi-Rail peer is not supposed to be capable of
2017 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2018 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2019 libcfs_nid2str(lp->lp_primary_nid));
2024 * Check the MULTIRAIL flag. Complain if the peer was DLC
2025 * configured without it.
2027 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2028 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2029 CERROR("Push says %s is Multi-Rail, DLC says not\n",
2030 libcfs_nid2str(lp->lp_primary_nid));
2032 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2033 lnet_peer_clr_non_mr_pref_nids(lp);
2038 * The peer may have discovery disabled at its end. Set
2039 * NO_DISCOVERY as appropriate.
2041 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2042 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2043 libcfs_nid2str(lp->lp_primary_nid));
2044 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2045 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2046 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2047 libcfs_nid2str(lp->lp_primary_nid));
2048 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2052 * Check for truncation of the Put message. Clear the
2053 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2054 * and tell discovery to allocate a bigger buffer.
2056 if (ev->mlength < ev->rlength) {
2057 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2058 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2059 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2060 lp->lp_state |= LNET_PEER_FORCE_PING;
2061 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2062 libcfs_nid2str(lp->lp_primary_nid),
2063 pbuf->pb_info.pi_nnis);
2067 /* always assume new data */
2068 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2069 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2072 * If there is data present that hasn't been processed yet,
2073 * we'll replace it if the Put contained newer data and it
2074 * fits. We're racing with a Ping or earlier Push in this
2077 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2078 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2079 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2080 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2081 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2082 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2083 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2084 libcfs_nid2str(lp->lp_primary_nid),
2085 LNET_PING_BUFFER_SEQNO(pbuf),
2086 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2092 * Allocate a buffer to copy the data. On a failure we drop
2093 * the Push and set FORCE_PING to force the discovery
2094 * thread to fix the problem by pinging the peer.
2096 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2098 lp->lp_state |= LNET_PEER_FORCE_PING;
2099 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2100 libcfs_nid2str(lp->lp_primary_nid),
2101 LNET_PING_BUFFER_SEQNO(pbuf));
2106 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2107 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2108 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2109 CDEBUG(D_NET, "Received Push %s %u\n",
2110 libcfs_nid2str(lp->lp_primary_nid),
2111 LNET_PING_BUFFER_SEQNO(pbuf));
2114 /* We've processed this buffer. It can be reposted */
2115 pbuf->pb_needs_post = true;
2118 * Queue the peer for discovery if not done, force it on the request
2119 * queue and wake the discovery thread if the peer was already queued,
2120 * because its status changed.
2122 spin_unlock(&lp->lp_lock);
2123 lnet_net_lock(LNET_LOCK_EX);
2124 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2125 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2126 wake_up(&the_lnet.ln_dc_waitq);
2128 /* Drop refcount from lookup */
2129 lnet_peer_decref_locked(lp);
2130 lnet_net_unlock(LNET_LOCK_EX);
2134 * Clear the discovery error state, unless we're already discovering
2135 * this peer, in which case the error is current.
2137 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2139 spin_lock(&lp->lp_lock);
2140 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2141 lp->lp_dc_error = 0;
2142 spin_unlock(&lp->lp_lock);
2146 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2147 * dropped/retaken within this function. An lnet_peer_ni is passed in
2148 * because discovery could tear down an lnet_peer.
2151 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2154 struct lnet_peer *lp;
2159 lnet_net_unlock(cpt);
2160 lnet_net_lock(LNET_LOCK_EX);
2161 lp = lpni->lpni_peer_net->lpn_peer;
2162 lnet_peer_clear_discovery_error(lp);
2165 * We're willing to be interrupted. The lpni can become a
2166 * zombie if we race with DLC, so we must check for that.
2169 /* Keep lp alive when the lnet_net_lock is unlocked */
2170 lnet_peer_addref_locked(lp);
2171 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2172 if (signal_pending(current))
2174 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2177 * Don't repeat discovery if discovery is disabled. This is
2178 * done to ensure we can use discovery as a standard ping as
2179 * well for backwards compatibility with routers which do not
2180 * have discovery or have discovery disabled
2182 if (lnet_is_discovery_disabled(lp) && count > 0)
2184 if (lp->lp_dc_error)
2186 if (lnet_peer_is_uptodate(lp))
2188 lnet_peer_queue_for_discovery(lp);
2190 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2193 * If caller requested a non-blocking operation then
2194 * return immediately. Once discovery is complete any
2195 * pending messages that were stopped due to discovery
2196 * will be transmitted.
2201 lnet_net_unlock(LNET_LOCK_EX);
2203 finish_wait(&lp->lp_dc_waitq, &wait);
2204 lnet_net_lock(LNET_LOCK_EX);
2205 lnet_peer_decref_locked(lp);
2206 /* Peer may have changed */
2207 lp = lpni->lpni_peer_net->lpn_peer;
2209 finish_wait(&lp->lp_dc_waitq, &wait);
2211 lnet_net_unlock(LNET_LOCK_EX);
2213 lnet_peer_decref_locked(lp);
2215 * The peer may have changed, so re-check and rediscover if that turns
2216 * out to have been the case. The reference count on lp ensured that
2217 * even if it was unlinked from lpni the memory could not be recycled.
2218 * Thus the check below is sufficient to determine whether the peer
2219 * changed. If the peer changed, then lp must not be dereferenced.
2221 if (lp != lpni->lpni_peer_net->lpn_peer)
2224 if (signal_pending(current))
2226 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2228 else if (lp->lp_dc_error)
2229 rc = lp->lp_dc_error;
2231 CDEBUG(D_NET, "non-blocking discovery\n");
2232 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2235 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2236 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2237 libcfs_nid2str(lpni->lpni_nid), rc,
2238 (!block) ? "pending discovery" : "discovery complete");
2243 /* Handle an incoming ack for a push. */
2245 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2247 struct lnet_ping_buffer *pbuf;
2249 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2250 spin_lock(&lp->lp_lock);
2251 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2252 lp->lp_push_error = ev->status;
2254 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2256 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2257 spin_unlock(&lp->lp_lock);
2259 CDEBUG(D_NET, "peer %s ev->status %d\n",
2260 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2263 /* Handle a Reply message. This is the reply to a Ping message. */
2265 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2267 struct lnet_ping_buffer *pbuf;
2270 spin_lock(&lp->lp_lock);
2273 * If some kind of error happened the contents of message
2274 * cannot be used. Set PING_FAILED to trigger a retry.
2277 lp->lp_state |= LNET_PEER_PING_FAILED;
2278 lp->lp_ping_error = ev->status;
2279 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2281 libcfs_nid2str(lp->lp_primary_nid),
2282 libcfs_nid2str(ev->source.nid));
2286 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2287 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2288 lnet_swap_pinginfo(pbuf);
2291 * A reply with invalid or corrupted info. Set PING_FAILED to
2294 rc = lnet_ping_info_validate(&pbuf->pb_info);
2296 lp->lp_state |= LNET_PEER_PING_FAILED;
2297 lp->lp_ping_error = 0;
2298 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2299 libcfs_nid2str(lp->lp_primary_nid), rc);
2305 * The peer may have discovery disabled at its end. Set
2306 * NO_DISCOVERY as appropriate.
2308 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2309 !lnet_peer_discovery_disabled) {
2310 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2311 libcfs_nid2str(lp->lp_primary_nid));
2312 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2314 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2315 libcfs_nid2str(lp->lp_primary_nid));
2316 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2320 * Update the MULTI_RAIL flag based on the reply. If the peer
2321 * was configured with DLC then the setting should match what
2324 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2325 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2326 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2327 libcfs_nid2str(lp->lp_primary_nid), lp);
2328 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2329 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2330 libcfs_nid2str(lp->lp_primary_nid));
2331 } else if (lnet_peer_discovery_disabled) {
2333 "peer %s(%p) not MR: DD disabled locally\n",
2334 libcfs_nid2str(lp->lp_primary_nid), lp);
2335 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2337 "peer %s(%p) not MR: DD disabled remotely\n",
2338 libcfs_nid2str(lp->lp_primary_nid), lp);
2340 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2341 libcfs_nid2str(lp->lp_primary_nid), lp);
2342 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2343 lnet_peer_clr_non_mr_pref_nids(lp);
2345 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2346 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2347 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2348 libcfs_nid2str(lp->lp_primary_nid));
2350 CERROR("Multi-Rail state vanished from %s\n",
2351 libcfs_nid2str(lp->lp_primary_nid));
2352 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2357 * Make sure we'll allocate the correct size ping buffer when
2360 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2361 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2364 * Check for truncation of the Reply. Clear PING_SENT and set
2365 * PING_FAILED to trigger a retry.
2367 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2368 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2369 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2370 lp->lp_state |= LNET_PEER_PING_FAILED;
2371 lp->lp_ping_error = 0;
2372 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2373 libcfs_nid2str(lp->lp_primary_nid),
2374 pbuf->pb_info.pi_nnis);
2379 * Check the sequence numbers in the reply. These are only
2380 * available if the reply came from a Multi-Rail peer.
2382 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2383 pbuf->pb_info.pi_nnis > 1 &&
2384 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2385 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2386 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2387 libcfs_nid2str(lp->lp_primary_nid),
2388 LNET_PING_BUFFER_SEQNO(pbuf),
2391 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2394 /* We're happy with the state of the data in the buffer. */
2395 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2396 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2397 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2398 lnet_ping_buffer_decref(lp->lp_data);
2400 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2401 lnet_ping_buffer_addref(pbuf);
2404 lp->lp_state &= ~LNET_PEER_PING_SENT;
2405 spin_unlock(&lp->lp_lock);
2407 lnet_net_lock(LNET_LOCK_EX);
2409 * If this peer is a gateway, call the routing callback to
2410 * handle the ping reply
2412 if (lp->lp_rtr_refcount > 0)
2413 lnet_router_discovery_ping_reply(lp);
2414 lnet_net_unlock(LNET_LOCK_EX);
2418 * Send event handling. Only matters for error cases, where we clean
2419 * up state on the peer and peer_ni that would otherwise be updated in
2420 * the REPLY event handler for a successful Ping, and the ACK event
2421 * handler for a successful Push.
2424 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2431 spin_lock(&lp->lp_lock);
2432 if (ev->msg_type == LNET_MSG_GET) {
2433 lp->lp_state &= ~LNET_PEER_PING_SENT;
2434 lp->lp_state |= LNET_PEER_PING_FAILED;
2435 lp->lp_ping_error = ev->status;
2436 } else { /* ev->msg_type == LNET_MSG_PUT */
2437 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2438 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2439 lp->lp_push_error = ev->status;
2441 spin_unlock(&lp->lp_lock);
2442 rc = LNET_REDISCOVER_PEER;
2444 CDEBUG(D_NET, "%s Send to %s: %d\n",
2445 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2446 libcfs_nid2str(ev->target.nid), rc);
2451 * Unlink event handling. This event is only seen if a call to
2452 * LNetMDUnlink() caused the event to be unlinked. If this call was
2453 * made after the event was set up in LNetGet() or LNetPut() then we
2454 * assume the Ping or Push timed out.
2457 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2459 spin_lock(&lp->lp_lock);
2460 /* We've passed through LNetGet() */
2461 if (lp->lp_state & LNET_PEER_PING_SENT) {
2462 lp->lp_state &= ~LNET_PEER_PING_SENT;
2463 lp->lp_state |= LNET_PEER_PING_FAILED;
2464 lp->lp_ping_error = -ETIMEDOUT;
2465 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2466 libcfs_nid2str(lp->lp_primary_nid));
2468 /* We've passed through LNetPut() */
2469 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2470 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2471 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2472 lp->lp_push_error = -ETIMEDOUT;
2473 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2474 libcfs_nid2str(lp->lp_primary_nid));
2476 spin_unlock(&lp->lp_lock);
2480 * Event handler for the discovery EQ.
2482 * Called with lnet_res_lock(cpt) held. The cpt is the
2483 * lnet_cpt_of_cookie() of the md handle cookie.
2485 static void lnet_discovery_event_handler(struct lnet_event *event)
2487 struct lnet_peer *lp = event->md.user_ptr;
2488 struct lnet_ping_buffer *pbuf;
2491 /* discovery needs to take another look */
2492 rc = LNET_REDISCOVER_PEER;
2494 CDEBUG(D_NET, "Received event: %d\n", event->type);
2496 switch (event->type) {
2497 case LNET_EVENT_ACK:
2498 lnet_discovery_event_ack(lp, event);
2500 case LNET_EVENT_REPLY:
2501 lnet_discovery_event_reply(lp, event);
2503 case LNET_EVENT_SEND:
2504 /* Only send failure triggers a retry. */
2505 rc = lnet_discovery_event_send(lp, event);
2507 case LNET_EVENT_UNLINK:
2508 /* LNetMDUnlink() was called */
2509 lnet_discovery_event_unlink(lp, event);
2512 /* Invalid events. */
2515 lnet_net_lock(LNET_LOCK_EX);
2516 if (event->unlinked) {
2517 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2518 lnet_ping_buffer_decref(pbuf);
2519 lnet_peer_decref_locked(lp);
2522 /* put peer back at end of request queue, if discovery not already
2524 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2525 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2526 wake_up(&the_lnet.ln_dc_waitq);
2528 lnet_net_unlock(LNET_LOCK_EX);
2532 * Build a peer from incoming data.
2534 * The NIDs in the incoming data are supposed to be structured as follows:
2537 * - other NIDs in same net
2538 * - NIDs in second net
2539 * - NIDs in third net
2541 * This due to the way the list of NIDs in the data is created.
2543 * Note that this function will mark the peer uptodate unless an
2544 * ENOMEM is encontered. All other errors are due to a conflict
2545 * between the DLC configuration and what discovery sees. We treat DLC
2546 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2547 * peer from becoming stuck in discovery.
2549 static int lnet_peer_merge_data(struct lnet_peer *lp,
2550 struct lnet_ping_buffer *pbuf)
2552 struct lnet_peer_ni *lpni;
2553 lnet_nid_t *curnis = NULL;
2554 struct lnet_ni_status *addnis = NULL;
2555 lnet_nid_t *delnis = NULL;
2565 flags = LNET_PEER_DISCOVERED;
2566 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2567 flags |= LNET_PEER_MULTI_RAIL;
2570 * Cache the routing feature for the peer; whether it is enabled
2571 * for disabled as reported by the remote peer.
2573 spin_lock(&lp->lp_lock);
2574 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2575 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2577 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2578 spin_unlock(&lp->lp_lock);
2580 nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2581 CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2582 CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2583 CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2584 if (!curnis || !addnis || !delnis) {
2592 /* Construct the list of NIDs present in peer. */
2594 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2595 curnis[ncurnis++] = lpni->lpni_nid;
2598 * Check for NIDs in pbuf not present in curnis[].
2599 * The loop starts at 1 to skip the loopback NID.
2601 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2602 for (j = 0; j < ncurnis; j++)
2603 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2606 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2609 * Check for NIDs in curnis[] not present in pbuf.
2610 * The nested loop starts at 1 to skip the loopback NID.
2612 * But never add the loopback NID to delnis[]: if it is
2613 * present in curnis[] then this peer is for this node.
2615 for (i = 0; i < ncurnis; i++) {
2616 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2618 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2619 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2621 * update the information we cache for the
2622 * peer with the latest information we
2625 lpni = lnet_find_peer_ni_locked(curnis[i]);
2627 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2628 lnet_peer_ni_decref_locked(lpni);
2633 if (j == pbuf->pb_info.pi_nnis)
2634 delnis[ndelnis++] = curnis[i];
2638 * If we get here and the discovery is disabled then we don't want
2639 * to add or delete any NIs. We just updated the ones we have some
2640 * information on, and call it a day
2643 if (lnet_is_discovery_disabled(lp))
2646 for (i = 0; i < naddnis; i++) {
2647 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2649 CERROR("Error adding NID %s to peer %s: %d\n",
2650 libcfs_nid2str(addnis[i].ns_nid),
2651 libcfs_nid2str(lp->lp_primary_nid), rc);
2655 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2657 lpni->lpni_ns_status = addnis[i].ns_status;
2658 lnet_peer_ni_decref_locked(lpni);
2662 for (i = 0; i < ndelnis; i++) {
2664 * for routers it's okay to delete the primary_nid because
2665 * the upper layers don't really rely on it. So if we're
2666 * being told that the router changed its primary_nid
2667 * then it's okay to delete it.
2669 if (lp->lp_rtr_refcount > 0)
2670 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2671 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2673 CERROR("Error deleting NID %s from peer %s: %d\n",
2674 libcfs_nid2str(delnis[i]),
2675 libcfs_nid2str(lp->lp_primary_nid), rc);
2681 * Errors other than -ENOMEM are due to peers having been
2682 * configured with DLC. Ignore these because DLC overrides
2687 CFS_FREE_PTR_ARRAY(curnis, nnis);
2688 CFS_FREE_PTR_ARRAY(addnis, nnis);
2689 CFS_FREE_PTR_ARRAY(delnis, nnis);
2690 lnet_ping_buffer_decref(pbuf);
2691 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2694 spin_lock(&lp->lp_lock);
2695 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2696 lp->lp_state |= LNET_PEER_FORCE_PING;
2697 spin_unlock(&lp->lp_lock);
2703 * The data in pbuf says lp is its primary peer, but the data was
2704 * received by a different peer. Try to update lp with the data.
2707 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2709 struct lnet_handle_md mdh;
2711 /* Queue lp for discovery, and force it on the request queue. */
2712 lnet_net_lock(LNET_LOCK_EX);
2713 if (lnet_peer_queue_for_discovery(lp))
2714 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2715 lnet_net_unlock(LNET_LOCK_EX);
2717 LNetInvalidateMDHandle(&mdh);
2720 * Decide whether we can move the peer to the DATA_PRESENT state.
2722 * We replace stale data for a multi-rail peer, repair PING_FAILED
2723 * status, and preempt FORCE_PING.
2725 * If after that we have DATA_PRESENT, we merge it into this peer.
2727 spin_lock(&lp->lp_lock);
2728 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2729 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2730 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2731 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2732 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2733 lnet_ping_buffer_decref(pbuf);
2738 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2739 lnet_ping_buffer_decref(lp->lp_data);
2741 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2743 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2744 mdh = lp->lp_ping_mdh;
2745 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2746 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2747 lp->lp_ping_error = 0;
2749 if (lp->lp_state & LNET_PEER_FORCE_PING)
2750 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2751 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2752 spin_unlock(&lp->lp_lock);
2754 if (!LNetMDHandleIsInvalid(mdh))
2758 return lnet_peer_merge_data(lp, pbuf);
2760 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2764 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2768 for (i = 0; i < pinfo->pi_nnis; i++) {
2769 if (pinfo->pi_ni[i].ns_nid == nid)
2777 * Update a peer using the data received.
2779 static int lnet_peer_data_present(struct lnet_peer *lp)
2780 __must_hold(&lp->lp_lock)
2782 struct lnet_ping_buffer *pbuf;
2783 struct lnet_peer_ni *lpni;
2784 lnet_nid_t nid = LNET_NID_ANY;
2790 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2791 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2792 spin_unlock(&lp->lp_lock);
2795 * Modifications of peer structures are done while holding the
2796 * ln_api_mutex. A global lock is required because we may be
2797 * modifying multiple peer structures, and a mutex greatly
2798 * simplifies memory management.
2800 * The actual changes to the data structures must also protect
2801 * against concurrent lookups, for which the lnet_net_lock in
2802 * LNET_LOCK_EX mode is used.
2804 mutex_lock(&the_lnet.ln_api_mutex);
2805 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2811 * If this peer is not on the peer list then it is being torn
2812 * down, and our reference count may be all that is keeping it
2813 * alive. Don't do any work on it.
2815 if (list_empty(&lp->lp_peer_list))
2818 flags = LNET_PEER_DISCOVERED;
2819 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2820 flags |= LNET_PEER_MULTI_RAIL;
2823 * Check whether the primary NID in the message matches the
2824 * primary NID of the peer. If it does, update the peer, if
2825 * it it does not, check whether there is already a peer with
2826 * that primary NID. If no such peer exists, try to update
2827 * the primary NID of the current peer (allowed if it was
2828 * created due to message traffic) and complete the update.
2829 * If the peer did exist, hand off the data to it.
2831 * The peer for the loopback interface is a special case: this
2832 * is the peer for the local node, and we want to set its
2833 * primary NID to the correct value here. Moreover, this peer
2834 * can show up with only the loopback NID in the ping buffer.
2836 if (pbuf->pb_info.pi_nnis <= 1)
2838 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2839 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2840 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2842 rc = lnet_peer_merge_data(lp, pbuf);
2844 * if the primary nid of the peer is present in the ping info returned
2845 * from the peer, but it's not the local primary peer we have
2846 * cached and discovery is disabled, then we don't want to update
2847 * our local peer info, by adding or removing NIDs, we just want
2848 * to update the status of the nids that we currently have
2849 * recorded in that peer.
2851 } else if (lp->lp_primary_nid == nid ||
2852 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2853 lnet_is_discovery_disabled(lp))) {
2854 rc = lnet_peer_merge_data(lp, pbuf);
2856 lpni = lnet_find_peer_ni_locked(nid);
2858 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2860 CERROR("Primary NID error %s versus %s: %d\n",
2861 libcfs_nid2str(lp->lp_primary_nid),
2862 libcfs_nid2str(nid), rc);
2864 rc = lnet_peer_merge_data(lp, pbuf);
2867 struct lnet_peer *new_lp;
2868 new_lp = lpni->lpni_peer_net->lpn_peer;
2870 * if lp has discovery/MR enabled that means new_lp
2871 * should have discovery/MR enabled as well, since
2872 * it's the same peer, which we're about to merge
2874 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2875 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2876 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2877 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2879 rc = lnet_peer_set_primary_data(new_lp, pbuf);
2880 lnet_consolidate_routes_locked(lp, new_lp);
2881 lnet_peer_ni_decref_locked(lpni);
2885 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2887 mutex_unlock(&the_lnet.ln_api_mutex);
2889 spin_lock(&lp->lp_lock);
2890 /* Tell discovery to re-check the peer immediately. */
2892 rc = LNET_REDISCOVER_PEER;
2897 * A ping failed. Clear the PING_FAILED state and set the
2898 * FORCE_PING state, to ensure a retry even if discovery is
2899 * disabled. This avoids being left with incorrect state.
2901 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2902 __must_hold(&lp->lp_lock)
2904 struct lnet_handle_md mdh;
2907 mdh = lp->lp_ping_mdh;
2908 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2909 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2910 lp->lp_state |= LNET_PEER_FORCE_PING;
2911 rc = lp->lp_ping_error;
2912 lp->lp_ping_error = 0;
2913 spin_unlock(&lp->lp_lock);
2915 if (!LNetMDHandleIsInvalid(mdh))
2918 CDEBUG(D_NET, "peer %s:%d\n",
2919 libcfs_nid2str(lp->lp_primary_nid), rc);
2921 spin_lock(&lp->lp_lock);
2922 return rc ? rc : LNET_REDISCOVER_PEER;
2926 * Select NID to send a Ping or Push to.
2928 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2930 struct lnet_peer_ni *lpni;
2932 /* Look for a direct-connected NID for this peer. */
2934 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2935 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2940 return lpni->lpni_nid;
2942 /* Look for a routed-connected NID for this peer. */
2944 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2945 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2950 return lpni->lpni_nid;
2952 return LNET_NID_ANY;
2955 /* Active side of ping. */
2956 static int lnet_peer_send_ping(struct lnet_peer *lp)
2957 __must_hold(&lp->lp_lock)
2964 lp->lp_state |= LNET_PEER_PING_SENT;
2965 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2966 spin_unlock(&lp->lp_lock);
2968 cpt = lnet_net_lock_current();
2969 /* Refcount for MD. */
2970 lnet_peer_addref_locked(lp);
2971 pnid = lnet_peer_select_nid(lp);
2972 lnet_net_unlock(cpt);
2974 nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2976 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2977 the_lnet.ln_dc_eq, false);
2980 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2981 * refcount on the peer, otherwise LNetMDUnlink will be called
2982 * which will eventually do that.
2986 lnet_peer_decref_locked(lp);
2987 lnet_net_unlock(cpt);
2988 rc = -rc; /* change the rc to negative value */
2990 } else if (rc < 0) {
2994 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2996 spin_lock(&lp->lp_lock);
3000 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3002 * The errors that get us here are considered hard errors and
3003 * cause Discovery to terminate. So we clear PING_SENT, but do
3004 * not set either PING_FAILED or FORCE_PING. In fact we need
3005 * to clear PING_FAILED, because the unlink event handler will
3006 * have set it if we called LNetMDUnlink() above.
3008 spin_lock(&lp->lp_lock);
3009 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3014 * This function exists because you cannot call LNetMDUnlink() from an
3017 static int lnet_peer_push_failed(struct lnet_peer *lp)
3018 __must_hold(&lp->lp_lock)
3020 struct lnet_handle_md mdh;
3023 mdh = lp->lp_push_mdh;
3024 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3025 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3026 rc = lp->lp_push_error;
3027 lp->lp_push_error = 0;
3028 spin_unlock(&lp->lp_lock);
3030 if (!LNetMDHandleIsInvalid(mdh))
3033 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3034 spin_lock(&lp->lp_lock);
3035 return rc ? rc : LNET_REDISCOVER_PEER;
3038 /* Active side of push. */
3039 static int lnet_peer_send_push(struct lnet_peer *lp)
3040 __must_hold(&lp->lp_lock)
3042 struct lnet_ping_buffer *pbuf;
3043 struct lnet_process_id id;
3048 /* Don't push to a non-multi-rail peer. */
3049 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3050 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3054 lp->lp_state |= LNET_PEER_PUSH_SENT;
3055 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3056 spin_unlock(&lp->lp_lock);
3058 cpt = lnet_net_lock_current();
3059 pbuf = the_lnet.ln_ping_target;
3060 lnet_ping_buffer_addref(pbuf);
3061 lnet_net_unlock(cpt);
3063 /* Push source MD */
3064 md.start = &pbuf->pb_info;
3065 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3066 md.threshold = 2; /* Put/Ack */
3069 md.eq_handle = the_lnet.ln_dc_eq;
3072 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
3074 lnet_ping_buffer_decref(pbuf);
3075 CERROR("Can't bind push source MD: %d\n", rc);
3078 cpt = lnet_net_lock_current();
3079 /* Refcount for MD. */
3080 lnet_peer_addref_locked(lp);
3081 id.pid = LNET_PID_LUSTRE;
3082 id.nid = lnet_peer_select_nid(lp);
3083 lnet_net_unlock(cpt);
3085 if (id.nid == LNET_NID_ANY) {
3090 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
3091 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3092 LNET_PROTO_PING_MATCHBITS, 0, 0);
3097 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3099 spin_lock(&lp->lp_lock);
3103 LNetMDUnlink(lp->lp_push_mdh);
3104 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3106 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3108 * The errors that get us here are considered hard errors and
3109 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3110 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3111 * because the unlink event handler will have set it if we
3112 * called LNetMDUnlink() above.
3114 spin_lock(&lp->lp_lock);
3115 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3120 * An unrecoverable error was encountered during discovery.
3121 * Set error status in peer and abort discovery.
3123 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3125 CDEBUG(D_NET, "Discovery error %s: %d\n",
3126 libcfs_nid2str(lp->lp_primary_nid), error);
3128 spin_lock(&lp->lp_lock);
3129 lp->lp_dc_error = error;
3130 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3131 lp->lp_state |= LNET_PEER_REDISCOVER;
3132 spin_unlock(&lp->lp_lock);
3136 * Mark the peer as discovered.
3138 static int lnet_peer_discovered(struct lnet_peer *lp)
3139 __must_hold(&lp->lp_lock)
3141 lp->lp_state |= LNET_PEER_DISCOVERED;
3142 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3143 LNET_PEER_REDISCOVER);
3145 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3152 * Discovering this peer is taking too long. Cancel any Ping or Push
3153 * that discovery is waiting on by unlinking the relevant MDs. The
3154 * lnet_discovery_event_handler() will proceed from here and complete
3157 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3159 struct lnet_handle_md ping_mdh;
3160 struct lnet_handle_md push_mdh;
3162 LNetInvalidateMDHandle(&ping_mdh);
3163 LNetInvalidateMDHandle(&push_mdh);
3165 spin_lock(&lp->lp_lock);
3166 if (lp->lp_state & LNET_PEER_PING_SENT) {
3167 ping_mdh = lp->lp_ping_mdh;
3168 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3170 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3171 push_mdh = lp->lp_push_mdh;
3172 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3174 spin_unlock(&lp->lp_lock);
3176 if (!LNetMDHandleIsInvalid(ping_mdh))
3177 LNetMDUnlink(ping_mdh);
3178 if (!LNetMDHandleIsInvalid(push_mdh))
3179 LNetMDUnlink(push_mdh);
3183 * Wait for work to be queued or some other change that must be
3184 * attended to. Returns non-zero if the discovery thread should shut
3187 static int lnet_peer_discovery_wait_for_work(void)
3194 cpt = lnet_net_lock_current();
3196 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3197 TASK_INTERRUPTIBLE);
3198 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3200 if (lnet_push_target_resize_needed() ||
3201 the_lnet.ln_push_target->pb_needs_post)
3203 if (!list_empty(&the_lnet.ln_dc_request))
3205 if (!list_empty(&the_lnet.ln_msg_resend))
3207 lnet_net_unlock(cpt);
3210 * wakeup max every second to check if there are peers that
3211 * have been stuck on the working queue for greater than
3214 schedule_timeout(cfs_time_seconds(1));
3215 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3216 cpt = lnet_net_lock_current();
3218 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3220 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3223 lnet_net_unlock(cpt);
3225 CDEBUG(D_NET, "woken: %d\n", rc);
3231 * Messages that were pending on a destroyed peer will be put on a global
3232 * resend list. The message resend list will be checked by
3233 * the discovery thread when it wakes up, and will resend messages. These
3234 * messages can still be sendable in the case the lpni which was the initial
3235 * cause of the message re-queue was transfered to another peer.
3237 * It is possible that LNet could be shutdown while we're iterating
3238 * through the list. lnet_shudown_lndnets() will attempt to access the
3239 * resend list, but will have to wait until the spinlock is released, by
3240 * which time there shouldn't be any more messages on the resend list.
3241 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3242 * for the messages so they can be released. The other case is that
3243 * lnet_shudown_lndnets() can finalize all the messages before this
3244 * function can visit the resend list, in which case this function will be
3247 static void lnet_resend_msgs(void)
3249 struct lnet_msg *msg, *tmp;
3253 spin_lock(&the_lnet.ln_msg_resend_lock);
3254 list_splice(&the_lnet.ln_msg_resend, &resend);
3255 spin_unlock(&the_lnet.ln_msg_resend_lock);
3257 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3258 list_del_init(&msg->msg_list);
3259 rc = lnet_send(msg->msg_src_nid_param, msg,
3260 msg->msg_rtr_nid_param);
3262 CNETERR("Error sending %s to %s: %d\n",
3263 lnet_msgtyp2str(msg->msg_type),
3264 libcfs_id2str(msg->msg_target), rc);
3265 lnet_finalize(msg, rc);
3270 /* The discovery thread. */
3271 static int lnet_peer_discovery(void *arg)
3273 struct lnet_peer *lp;
3276 wait_for_completion(&the_lnet.ln_started);
3278 CDEBUG(D_NET, "started\n");
3281 if (lnet_peer_discovery_wait_for_work())
3284 if (lnet_push_target_resize_needed())
3285 lnet_push_target_resize();
3286 else if (the_lnet.ln_push_target->pb_needs_post)
3287 lnet_push_target_post(the_lnet.ln_push_target,
3288 &the_lnet.ln_push_target_md);
3292 lnet_net_lock(LNET_LOCK_EX);
3293 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3294 lnet_net_unlock(LNET_LOCK_EX);
3299 * Process all incoming discovery work requests. When
3300 * discovery must wait on a peer to change state, it
3301 * is added to the tail of the ln_dc_working queue. A
3302 * timestamp keeps track of when the peer was added,
3303 * so we can time out discovery requests that take too
3306 while (!list_empty(&the_lnet.ln_dc_request)) {
3307 lp = list_first_entry(&the_lnet.ln_dc_request,
3308 struct lnet_peer, lp_dc_list);
3309 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3311 * set the time the peer was put on the dc_working
3312 * queue. It shouldn't remain on the queue
3313 * forever, in case the GET message (for ping)
3314 * doesn't get a REPLY or the PUT message (for
3315 * push) doesn't get an ACK.
3317 lp->lp_last_queued = ktime_get_real_seconds();
3318 lnet_net_unlock(LNET_LOCK_EX);
3320 if (lnet_push_target_resize_needed())
3321 lnet_push_target_resize();
3322 else if (the_lnet.ln_push_target->pb_needs_post)
3323 lnet_push_target_post(the_lnet.ln_push_target,
3324 &the_lnet.ln_push_target_md);
3327 * Select an action depending on the state of
3328 * the peer and whether discovery is disabled.
3329 * The check whether discovery is disabled is
3330 * done after the code that handles processing
3331 * for arrived data, cleanup for failures, and
3332 * forcing a Ping or Push.
3334 spin_lock(&lp->lp_lock);
3335 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3336 libcfs_nid2str(lp->lp_primary_nid), lp,
3338 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3339 rc = lnet_peer_data_present(lp);
3340 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3341 rc = lnet_peer_ping_failed(lp);
3342 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3343 rc = lnet_peer_push_failed(lp);
3344 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3345 rc = lnet_peer_send_ping(lp);
3346 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3347 rc = lnet_peer_send_push(lp);
3348 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3349 rc = lnet_peer_send_ping(lp);
3350 else if (lnet_peer_needs_push(lp))
3351 rc = lnet_peer_send_push(lp);
3353 rc = lnet_peer_discovered(lp);
3354 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3355 libcfs_nid2str(lp->lp_primary_nid), lp,
3357 spin_unlock(&lp->lp_lock);
3359 lnet_net_lock(LNET_LOCK_EX);
3360 if (rc == LNET_REDISCOVER_PEER) {
3361 list_move(&lp->lp_dc_list,
3362 &the_lnet.ln_dc_request);
3364 lnet_peer_discovery_error(lp, rc);
3366 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3367 lnet_peer_discovery_complete(lp);
3368 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3372 lnet_net_unlock(LNET_LOCK_EX);
3375 CDEBUG(D_NET, "stopping\n");
3377 * Clean up before telling lnet_peer_discovery_stop() that
3378 * we're done. Use wake_up() below to somewhat reduce the
3379 * size of the thundering herd if there are multiple threads
3380 * waiting on discovery of a single peer.
3383 /* Queue cleanup 1: stop all pending pings and pushes. */
3384 lnet_net_lock(LNET_LOCK_EX);
3385 while (!list_empty(&the_lnet.ln_dc_working)) {
3386 lp = list_first_entry(&the_lnet.ln_dc_working,
3387 struct lnet_peer, lp_dc_list);
3388 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3389 lnet_net_unlock(LNET_LOCK_EX);
3390 lnet_peer_cancel_discovery(lp);
3391 lnet_net_lock(LNET_LOCK_EX);
3393 lnet_net_unlock(LNET_LOCK_EX);
3395 /* Queue cleanup 2: wait for the expired queue to clear. */
3396 while (!list_empty(&the_lnet.ln_dc_expired))
3397 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3399 /* Queue cleanup 3: clear the request queue. */
3400 lnet_net_lock(LNET_LOCK_EX);
3401 while (!list_empty(&the_lnet.ln_dc_request)) {
3402 lp = list_first_entry(&the_lnet.ln_dc_request,
3403 struct lnet_peer, lp_dc_list);
3404 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3405 lnet_peer_discovery_complete(lp);
3407 lnet_net_unlock(LNET_LOCK_EX);
3409 LNetEQFree(the_lnet.ln_dc_eq);
3410 the_lnet.ln_dc_eq = NULL;
3412 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3413 wake_up(&the_lnet.ln_dc_waitq);
3415 CDEBUG(D_NET, "stopped\n");
3420 /* ln_api_mutex is held on entry. */
3421 int lnet_peer_discovery_start(void)
3423 struct task_struct *task;
3426 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3429 the_lnet.ln_dc_eq = LNetEQAlloc(lnet_discovery_event_handler);
3430 if (IS_ERR(the_lnet.ln_dc_eq)) {
3431 rc = PTR_ERR(the_lnet.ln_dc_eq);
3432 CERROR("Can't allocate discovery EQ: %d\n", rc);
3436 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3437 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3440 CERROR("Can't start peer discovery thread: %d\n", rc);
3442 LNetEQFree(the_lnet.ln_dc_eq);
3443 the_lnet.ln_dc_eq = NULL;
3445 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3448 CDEBUG(D_NET, "discovery start: %d\n", rc);
3453 /* ln_api_mutex is held on entry. */
3454 void lnet_peer_discovery_stop(void)
3456 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3459 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3460 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3462 /* In the LNetNIInit() path we may be stopping discovery before it
3463 * entered its work loop
3465 if (!completion_done(&the_lnet.ln_started))
3466 complete(&the_lnet.ln_started);
3468 wake_up(&the_lnet.ln_dc_waitq);
3470 wait_event(the_lnet.ln_dc_waitq,
3471 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3473 LASSERT(list_empty(&the_lnet.ln_dc_request));
3474 LASSERT(list_empty(&the_lnet.ln_dc_working));
3475 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3477 CDEBUG(D_NET, "discovery stopped\n");
3483 lnet_debug_peer(lnet_nid_t nid)
3485 char *aliveness = "NA";
3486 struct lnet_peer_ni *lp;
3489 cpt = lnet_cpt_of_nid(nid, NULL);
3492 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3494 lnet_net_unlock(cpt);
3495 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3499 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3500 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3502 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3503 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3504 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3505 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3506 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3508 lnet_peer_ni_decref_locked(lp);
3510 lnet_net_unlock(cpt);
3513 /* Gathering information for userspace. */
3515 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3516 char aliveness[LNET_MAX_STR_LEN],
3517 __u32 *cpt_iter, __u32 *refcount,
3518 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3519 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3520 __u32 *peer_tx_qnob)
3522 struct lnet_peer_table *peer_table;
3523 struct lnet_peer_ni *lp;
3528 /* get the number of CPTs */
3529 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3531 /* if the cpt number to be examined is >= the number of cpts in
3532 * the system then indicate that there are no more cpts to examin
3534 if (*cpt_iter >= lncpt)
3537 /* get the current table */
3538 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3539 /* if the ptable is NULL then there are no more cpts to examine */
3540 if (peer_table == NULL)
3543 lnet_net_lock(*cpt_iter);
3545 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3546 struct list_head *peers = &peer_table->pt_hash[j];
3548 list_for_each_entry(lp, peers, lpni_hashlist) {
3549 if (peer_index-- > 0)
3552 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3553 if (lnet_isrouter(lp) ||
3554 lnet_peer_aliveness_enabled(lp))
3555 snprintf(aliveness, LNET_MAX_STR_LEN,
3556 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3558 *nid = lp->lpni_nid;
3559 *refcount = atomic_read(&lp->lpni_refcount);
3560 *ni_peer_tx_credits =
3561 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3562 *peer_tx_credits = lp->lpni_txcredits;
3563 *peer_rtr_credits = lp->lpni_rtrcredits;
3564 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3565 *peer_tx_qnob = lp->lpni_txqnob;
3571 lnet_net_unlock(*cpt_iter);
3575 return found ? 0 : -ENOENT;
3578 /* ln_api_mutex is held, which keeps the peer list stable */
3579 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3581 struct lnet_ioctl_element_stats *lpni_stats;
3582 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3583 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3584 struct lnet_peer_ni_credit_info *lpni_info;
3585 struct lnet_peer_ni *lpni;
3586 struct lnet_peer *lp;
3591 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3598 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3599 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3600 size *= lp->lp_nnis;
3601 if (size > cfg->prcfg_size) {
3602 cfg->prcfg_size = size;
3607 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3608 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3609 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3610 cfg->prcfg_count = lp->lp_nnis;
3611 cfg->prcfg_size = size;
3612 cfg->prcfg_state = lp->lp_state;
3614 /* Allocate helper buffers. */
3616 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3619 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3622 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3623 if (!lpni_msg_stats)
3624 goto out_free_stats;
3625 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3627 goto out_free_msg_stats;
3632 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3633 nid = lpni->lpni_nid;
3634 if (copy_to_user(bulk, &nid, sizeof(nid)))
3635 goto out_free_hstats;
3636 bulk += sizeof(nid);
3638 memset(lpni_info, 0, sizeof(*lpni_info));
3639 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3640 if (lnet_isrouter(lpni) ||
3641 lnet_peer_aliveness_enabled(lpni))
3642 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3643 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3645 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3646 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3647 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3648 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3649 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3650 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3651 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3652 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3653 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3654 goto out_free_hstats;
3655 bulk += sizeof(*lpni_info);
3657 memset(lpni_stats, 0, sizeof(*lpni_stats));
3658 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3659 LNET_STATS_TYPE_SEND);
3660 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3661 LNET_STATS_TYPE_RECV);
3662 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3663 LNET_STATS_TYPE_DROP);
3664 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3665 goto out_free_hstats;
3666 bulk += sizeof(*lpni_stats);
3667 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3668 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3669 goto out_free_hstats;
3670 bulk += sizeof(*lpni_msg_stats);
3671 lpni_hstats->hlpni_network_timeout =
3672 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3673 lpni_hstats->hlpni_remote_dropped =
3674 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3675 lpni_hstats->hlpni_remote_timeout =
3676 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3677 lpni_hstats->hlpni_remote_error =
3678 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3679 lpni_hstats->hlpni_health_value =
3680 atomic_read(&lpni->lpni_healthv);
3681 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3682 goto out_free_hstats;
3683 bulk += sizeof(*lpni_hstats);
3688 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3690 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3692 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3694 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3696 lnet_peer_decref_locked(lp);
3702 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3704 /* the mt could've shutdown and cleaned up the queues */
3705 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3708 if (list_empty(&lpni->lpni_recovery) &&
3709 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3710 CDEBUG(D_NET, "lpni %s added to recovery queue. Health = %d\n",
3711 libcfs_nid2str(lpni->lpni_nid),
3712 atomic_read(&lpni->lpni_healthv));
3713 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3714 lnet_peer_ni_addref_locked(lpni);
3718 /* Call with the ln_api_mutex held */
3720 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3722 struct lnet_peer_table *ptable;
3723 struct lnet_peer *lp;
3724 struct lnet_peer_net *lpn;
3725 struct lnet_peer_ni *lpni;
3729 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3733 lnet_net_lock(LNET_LOCK_EX);
3734 lpni = lnet_find_peer_ni_locked(nid);
3736 lnet_net_unlock(LNET_LOCK_EX);
3739 atomic_set(&lpni->lpni_healthv, value);
3740 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3741 lnet_peer_ni_decref_locked(lpni);
3742 lnet_net_unlock(LNET_LOCK_EX);
3746 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3749 * Walk all the peers and reset the healhv for each one to the
3752 lnet_net_lock(LNET_LOCK_EX);
3753 for (cpt = 0; cpt < lncpt; cpt++) {
3754 ptable = the_lnet.ln_peer_tables[cpt];
3755 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3756 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3757 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3759 atomic_set(&lpni->lpni_healthv, value);
3760 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3765 lnet_net_unlock(LNET_LOCK_EX);