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 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
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;
263 * all peers created on a router should have health on
264 * if it's not already on.
266 if (the_lnet.ln_routing && !lnet_health_sensitivity)
267 lp->lp_health_sensitivity = 1;
270 * Turn off discovery for loopback peer. If you're creating a peer
271 * for the loopback interface then that was initiated when we
272 * attempted to send a message over the loopback. There is no need
273 * to ever use a different interface when sending messages to
276 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
277 lp->lp_state = LNET_PEER_NO_DISCOVERY;
278 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
280 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
286 lnet_destroy_peer_locked(struct lnet_peer *lp)
288 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
290 LASSERT(atomic_read(&lp->lp_refcount) == 0);
291 LASSERT(lp->lp_rtr_refcount == 0);
292 LASSERT(list_empty(&lp->lp_peer_nets));
293 LASSERT(list_empty(&lp->lp_peer_list));
294 LASSERT(list_empty(&lp->lp_dc_list));
297 lnet_ping_buffer_decref(lp->lp_data);
300 * if there are messages still on the pending queue, then make
301 * sure to queue them on the ln_msg_resend list so they can be
302 * resent at a later point if the discovery thread is still
304 * If the discovery thread has stopped, then the wakeup will be a
305 * no-op, and it is expected the lnet_shutdown_lndnets() will
306 * eventually be called, which will traverse this list and
307 * finalize the messages on the list.
308 * We can not resend them now because we're holding the cpt lock.
309 * Releasing the lock can cause an inconsistent state
311 spin_lock(&the_lnet.ln_msg_resend_lock);
312 spin_lock(&lp->lp_lock);
313 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
314 spin_unlock(&lp->lp_lock);
315 spin_unlock(&the_lnet.ln_msg_resend_lock);
316 wake_up(&the_lnet.ln_dc_waitq);
318 LIBCFS_FREE(lp, sizeof(*lp));
322 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
323 * that peer_net, detach the peer_net from the peer.
325 * Call with lnet_net_lock/EX held
328 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
330 struct lnet_peer_table *ptable;
331 struct lnet_peer_net *lpn;
332 struct lnet_peer *lp;
335 * Belts and suspenders: gracefully handle teardown of a
336 * partially connected peer_ni.
338 lpn = lpni->lpni_peer_net;
340 list_del_init(&lpni->lpni_peer_nis);
342 * If there are no lpni's left, we detach lpn from
343 * lp_peer_nets, so it cannot be found anymore.
345 if (list_empty(&lpn->lpn_peer_nis))
346 list_del_init(&lpn->lpn_peer_nets);
348 /* Update peer NID count. */
353 * If there are no more peer nets, make the peer unfindable
354 * via the peer_tables.
356 * Otherwise, if the peer is DISCOVERED, tell discovery to
357 * take another look at it. This is a no-op if discovery for
358 * this peer did the detaching.
360 if (list_empty(&lp->lp_peer_nets)) {
361 list_del_init(&lp->lp_peer_list);
362 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
364 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
365 /* Discovery isn't running, nothing to do here. */
366 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
367 lnet_peer_queue_for_discovery(lp);
368 wake_up(&the_lnet.ln_dc_waitq);
370 CDEBUG(D_NET, "peer %s NID %s\n",
371 libcfs_nid2str(lp->lp_primary_nid),
372 libcfs_nid2str(lpni->lpni_nid));
375 /* called with lnet_net_lock LNET_LOCK_EX held */
377 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
379 struct lnet_peer_table *ptable = NULL;
381 /* don't remove a peer_ni if it's also a gateway */
382 if (lnet_isrouter(lpni) && !force) {
383 CERROR("Peer NI %s is a gateway. Can not delete it\n",
384 libcfs_nid2str(lpni->lpni_nid));
388 lnet_peer_remove_from_remote_list(lpni);
390 /* remove peer ni from the hash list. */
391 list_del_init(&lpni->lpni_hashlist);
394 * indicate the peer is being deleted so the monitor thread can
395 * remove it from the recovery queue.
397 spin_lock(&lpni->lpni_lock);
398 lpni->lpni_state |= LNET_PEER_NI_DELETING;
399 spin_unlock(&lpni->lpni_lock);
401 /* decrement the ref count on the peer table */
402 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
403 LASSERT(ptable->pt_number > 0);
407 * The peer_ni can no longer be found with a lookup. But there
408 * can be current users, so keep track of it on the zombie
409 * list until the reference count has gone to zero.
411 * The last reference may be lost in a place where the
412 * lnet_net_lock locks only a single cpt, and that cpt may not
413 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
416 spin_lock(&ptable->pt_zombie_lock);
417 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
418 ptable->pt_zombies++;
419 spin_unlock(&ptable->pt_zombie_lock);
421 /* no need to keep this peer_ni on the hierarchy anymore */
422 lnet_peer_detach_peer_ni_locked(lpni);
424 /* remove hashlist reference on peer_ni */
425 lnet_peer_ni_decref_locked(lpni);
430 void lnet_peer_uninit(void)
432 struct lnet_peer_ni *lpni, *tmp;
434 lnet_net_lock(LNET_LOCK_EX);
436 /* remove all peer_nis from the remote peer and the hash list */
437 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
438 lpni_on_remote_peer_ni_list)
439 lnet_peer_ni_del_locked(lpni, false);
441 lnet_peer_tables_destroy();
443 lnet_net_unlock(LNET_LOCK_EX);
447 lnet_peer_del_locked(struct lnet_peer *peer)
449 struct lnet_peer_ni *lpni = NULL, *lpni2;
452 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
454 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
455 while (lpni != NULL) {
456 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
457 rc = lnet_peer_ni_del_locked(lpni, false);
467 lnet_peer_del(struct lnet_peer *peer)
469 lnet_net_lock(LNET_LOCK_EX);
470 lnet_peer_del_locked(peer);
471 lnet_net_unlock(LNET_LOCK_EX);
477 * Delete a NID from a peer. Call with ln_api_mutex held.
480 * -EPERM: Non-DLC deletion from DLC-configured peer.
481 * -ENOENT: No lnet_peer_ni corresponding to the nid.
482 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
483 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
486 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
488 struct lnet_peer_ni *lpni;
489 lnet_nid_t primary_nid = lp->lp_primary_nid;
491 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
493 if (!(flags & LNET_PEER_CONFIGURED)) {
494 if (lp->lp_state & LNET_PEER_CONFIGURED) {
499 lpni = lnet_find_peer_ni_locked(nid);
504 lnet_peer_ni_decref_locked(lpni);
505 if (lp != lpni->lpni_peer_net->lpn_peer) {
511 * This function only allows deletion of the primary NID if it
514 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
519 lnet_net_lock(LNET_LOCK_EX);
521 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
522 struct lnet_peer_ni *lpni2;
523 /* assign the next peer_ni to be the primary */
524 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
526 lp->lp_primary_nid = lpni->lpni_nid;
528 rc = lnet_peer_ni_del_locked(lpni, force);
530 lnet_net_unlock(LNET_LOCK_EX);
533 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
534 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
540 lnet_peer_table_cleanup_locked(struct lnet_net *net,
541 struct lnet_peer_table *ptable)
544 struct lnet_peer_ni *next;
545 struct lnet_peer_ni *lpni;
546 struct lnet_peer *peer;
548 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
549 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
551 if (net != NULL && net != lpni->lpni_net)
554 peer = lpni->lpni_peer_net->lpn_peer;
555 if (peer->lp_primary_nid != lpni->lpni_nid) {
556 lnet_peer_ni_del_locked(lpni, false);
560 * Removing the primary NID implies removing
561 * the entire peer. Advance next beyond any
562 * peer_ni that belongs to the same peer.
564 list_for_each_entry_from(next, &ptable->pt_hash[i],
566 if (next->lpni_peer_net->lpn_peer != peer)
569 lnet_peer_del_locked(peer);
575 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
579 spin_lock(&ptable->pt_zombie_lock);
580 while (ptable->pt_zombies) {
581 spin_unlock(&ptable->pt_zombie_lock);
583 if (is_power_of_2(i)) {
585 "Waiting for %d zombies on peer table\n",
588 set_current_state(TASK_UNINTERRUPTIBLE);
589 schedule_timeout(cfs_time_seconds(1) >> 1);
590 spin_lock(&ptable->pt_zombie_lock);
592 spin_unlock(&ptable->pt_zombie_lock);
596 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
597 struct lnet_peer_table *ptable)
599 struct lnet_peer_ni *lp;
600 struct lnet_peer_ni *tmp;
604 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
605 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
607 if (net != lp->lpni_net)
610 if (!lnet_isrouter(lp))
613 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
615 lnet_net_unlock(LNET_LOCK_EX);
616 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
617 lnet_net_lock(LNET_LOCK_EX);
623 lnet_peer_tables_cleanup(struct lnet_net *net)
626 struct lnet_peer_table *ptable;
628 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
629 /* If just deleting the peers for a NI, get rid of any routes these
630 * peers are gateways for. */
631 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
632 lnet_net_lock(LNET_LOCK_EX);
633 lnet_peer_table_del_rtrs_locked(net, ptable);
634 lnet_net_unlock(LNET_LOCK_EX);
637 /* Start the cleanup process */
638 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
639 lnet_net_lock(LNET_LOCK_EX);
640 lnet_peer_table_cleanup_locked(net, ptable);
641 lnet_net_unlock(LNET_LOCK_EX);
644 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
645 lnet_peer_ni_finalize_wait(ptable);
648 static struct lnet_peer_ni *
649 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
651 struct list_head *peers;
652 struct lnet_peer_ni *lp;
654 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
656 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
657 list_for_each_entry(lp, peers, lpni_hashlist) {
658 if (lp->lpni_nid == nid) {
659 lnet_peer_ni_addref_locked(lp);
667 struct lnet_peer_ni *
668 lnet_find_peer_ni_locked(lnet_nid_t nid)
670 struct lnet_peer_ni *lpni;
671 struct lnet_peer_table *ptable;
674 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
676 ptable = the_lnet.ln_peer_tables[cpt];
677 lpni = lnet_get_peer_ni_locked(ptable, nid);
682 struct lnet_peer_ni *
683 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
685 struct lnet_peer_net *lpn;
686 struct lnet_peer_ni *lpni;
688 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
692 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
693 if (lpni->lpni_nid == nid)
701 lnet_find_peer(lnet_nid_t nid)
703 struct lnet_peer_ni *lpni;
704 struct lnet_peer *lp = NULL;
707 cpt = lnet_net_lock_current();
708 lpni = lnet_find_peer_ni_locked(nid);
710 lp = lpni->lpni_peer_net->lpn_peer;
711 lnet_peer_addref_locked(lp);
712 lnet_peer_ni_decref_locked(lpni);
714 lnet_net_unlock(cpt);
719 struct lnet_peer_net *
720 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
722 struct lnet_peer_net *net;
725 /* no net id provided return the first net */
726 net = list_first_entry_or_null(&lp->lp_peer_nets,
727 struct lnet_peer_net,
733 /* find the net after the one provided */
734 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
735 if (net->lpn_net_id == prev_lpn_id) {
737 * if we reached the end of the list loop to the
740 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
741 return list_first_entry_or_null(&lp->lp_peer_nets,
742 struct lnet_peer_net,
745 return list_next_entry(net, lpn_peer_nets);
752 struct lnet_peer_ni *
753 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
754 struct lnet_peer_net *peer_net,
755 struct lnet_peer_ni *prev)
757 struct lnet_peer_ni *lpni;
758 struct lnet_peer_net *net = peer_net;
762 if (list_empty(&peer->lp_peer_nets))
765 net = list_entry(peer->lp_peer_nets.next,
766 struct lnet_peer_net,
769 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
775 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
777 * if you reached the end of the peer ni list and the peer
778 * net is specified then there are no more peer nis in that
785 * we reached the end of this net ni list. move to the
788 if (prev->lpni_peer_net->lpn_peer_nets.next ==
790 /* no more nets and no more NIs. */
793 /* get the next net */
794 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
795 struct lnet_peer_net,
797 /* get the ni on it */
798 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
804 /* there are more nis left */
805 lpni = list_entry(prev->lpni_peer_nis.next,
806 struct lnet_peer_ni, lpni_peer_nis);
811 /* Call with the ln_api_mutex held */
812 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
814 struct lnet_process_id id;
815 struct lnet_peer_table *ptable;
816 struct lnet_peer *lp;
825 if (the_lnet.ln_state != LNET_STATE_RUNNING)
828 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
831 * Count the number of peers, and return E2BIG if the buffer
832 * is too small. We'll also return the desired size.
835 for (cpt = 0; cpt < lncpt; cpt++) {
836 ptable = the_lnet.ln_peer_tables[cpt];
837 count += ptable->pt_peers;
839 size = count * sizeof(*ids);
844 * Walk the peer lists and copy out the primary nids.
845 * This is safe because the peer lists are only modified
846 * while the ln_api_mutex is held. So we don't need to
847 * hold the lnet_net_lock as well, and can therefore
848 * directly call copy_to_user().
851 memset(&id, 0, sizeof(id));
852 id.pid = LNET_PID_LUSTRE;
854 for (cpt = 0; cpt < lncpt; cpt++) {
855 ptable = the_lnet.ln_peer_tables[cpt];
856 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
859 id.nid = lp->lp_primary_nid;
860 if (copy_to_user(&ids[i], &id, sizeof(id)))
873 * Start pushes to peers that need to be updated for a configuration
874 * change on this node.
877 lnet_push_update_to_peers(int force)
879 struct lnet_peer_table *ptable;
880 struct lnet_peer *lp;
884 lnet_net_lock(LNET_LOCK_EX);
885 if (lnet_peer_discovery_disabled)
887 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
888 for (cpt = 0; cpt < lncpt; cpt++) {
889 ptable = the_lnet.ln_peer_tables[cpt];
890 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
892 spin_lock(&lp->lp_lock);
893 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
894 lp->lp_state |= LNET_PEER_FORCE_PUSH;
895 spin_unlock(&lp->lp_lock);
897 if (lnet_peer_needs_push(lp))
898 lnet_peer_queue_for_discovery(lp);
901 lnet_net_unlock(LNET_LOCK_EX);
902 wake_up(&the_lnet.ln_dc_waitq);
906 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
907 * this is a preferred point-to-point path. Call with lnet_net_lock in
911 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
915 if (lpni->lpni_pref_nnids == 0)
917 if (lpni->lpni_pref_nnids == 1)
918 return lpni->lpni_pref.nid == nid;
919 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
920 if (lpni->lpni_pref.nids[i] == nid)
927 * Set a single ni as preferred, provided no preferred ni is already
928 * defined. Only to be used for non-multi-rail peer_ni.
931 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
935 spin_lock(&lpni->lpni_lock);
936 if (nid == LNET_NID_ANY) {
938 } else if (lpni->lpni_pref_nnids > 0) {
940 } else if (lpni->lpni_pref_nnids == 0) {
941 lpni->lpni_pref.nid = nid;
942 lpni->lpni_pref_nnids = 1;
943 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
945 spin_unlock(&lpni->lpni_lock);
947 CDEBUG(D_NET, "peer %s nid %s: %d\n",
948 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
953 * Clear the preferred NID from a non-multi-rail peer_ni, provided
954 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
957 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
961 spin_lock(&lpni->lpni_lock);
962 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
963 lpni->lpni_pref_nnids = 0;
964 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
965 } else if (lpni->lpni_pref_nnids == 0) {
970 spin_unlock(&lpni->lpni_lock);
972 CDEBUG(D_NET, "peer %s: %d\n",
973 libcfs_nid2str(lpni->lpni_nid), rc);
978 * Clear the preferred NIDs from a non-multi-rail peer.
981 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
983 struct lnet_peer_ni *lpni = NULL;
985 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
986 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
990 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
992 lnet_nid_t *nids = NULL;
993 lnet_nid_t *oldnids = NULL;
994 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
999 if (nid == LNET_NID_ANY) {
1004 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1009 /* A non-MR node may have only one preferred NI per peer_ni */
1010 if (lpni->lpni_pref_nnids > 0) {
1011 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1017 if (lpni->lpni_pref_nnids != 0) {
1018 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1019 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1024 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
1025 if (lpni->lpni_pref.nids[i] == nid) {
1026 LIBCFS_FREE(nids, size);
1030 nids[i] = lpni->lpni_pref.nids[i];
1035 lnet_net_lock(LNET_LOCK_EX);
1036 spin_lock(&lpni->lpni_lock);
1037 if (lpni->lpni_pref_nnids == 0) {
1038 lpni->lpni_pref.nid = nid;
1040 oldnids = lpni->lpni_pref.nids;
1041 lpni->lpni_pref.nids = nids;
1043 lpni->lpni_pref_nnids++;
1044 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1045 spin_unlock(&lpni->lpni_lock);
1046 lnet_net_unlock(LNET_LOCK_EX);
1049 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1050 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1053 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1054 spin_lock(&lpni->lpni_lock);
1055 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1056 spin_unlock(&lpni->lpni_lock);
1058 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1059 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1064 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1066 lnet_nid_t *nids = NULL;
1067 lnet_nid_t *oldnids = NULL;
1068 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1073 if (lpni->lpni_pref_nnids == 0) {
1078 if (lpni->lpni_pref_nnids == 1) {
1079 if (lpni->lpni_pref.nid != nid) {
1083 } else if (lpni->lpni_pref_nnids == 2) {
1084 if (lpni->lpni_pref.nids[0] != nid &&
1085 lpni->lpni_pref.nids[1] != nid) {
1090 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1091 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1096 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1097 if (lpni->lpni_pref.nids[i] != nid)
1099 nids[j++] = lpni->lpni_pref.nids[i];
1101 /* Check if we actually removed a nid. */
1102 if (j == lpni->lpni_pref_nnids) {
1103 LIBCFS_FREE(nids, size);
1109 lnet_net_lock(LNET_LOCK_EX);
1110 spin_lock(&lpni->lpni_lock);
1111 if (lpni->lpni_pref_nnids == 1) {
1112 lpni->lpni_pref.nid = LNET_NID_ANY;
1113 } else if (lpni->lpni_pref_nnids == 2) {
1114 oldnids = lpni->lpni_pref.nids;
1115 if (oldnids[0] == nid)
1116 lpni->lpni_pref.nid = oldnids[1];
1118 lpni->lpni_pref.nid = oldnids[2];
1120 oldnids = lpni->lpni_pref.nids;
1121 lpni->lpni_pref.nids = nids;
1123 lpni->lpni_pref_nnids--;
1124 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1125 spin_unlock(&lpni->lpni_lock);
1126 lnet_net_unlock(LNET_LOCK_EX);
1129 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1130 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1133 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1134 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1139 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1141 struct lnet_peer_ni *lpni;
1142 lnet_nid_t primary_nid = nid;
1144 lpni = lnet_find_peer_ni_locked(nid);
1146 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1147 lnet_peer_ni_decref_locked(lpni);
1154 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1155 __must_hold(&lp->lp_lock)
1157 if (lnet_peer_discovery_disabled)
1160 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1161 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1172 lnet_is_discovery_disabled(struct lnet_peer *lp)
1176 spin_lock(&lp->lp_lock);
1177 rc = lnet_is_discovery_disabled_locked(lp);
1178 spin_unlock(&lp->lp_lock);
1184 LNetPrimaryNID(lnet_nid_t nid)
1186 struct lnet_peer *lp;
1187 struct lnet_peer_ni *lpni;
1188 lnet_nid_t primary_nid = nid;
1192 cpt = lnet_net_lock_current();
1193 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1198 lp = lpni->lpni_peer_net->lpn_peer;
1200 while (!lnet_peer_is_uptodate(lp)) {
1201 rc = lnet_discover_peer_locked(lpni, cpt, true);
1204 lp = lpni->lpni_peer_net->lpn_peer;
1206 /* Only try once if discovery is disabled */
1207 if (lnet_is_discovery_disabled(lp))
1210 primary_nid = lp->lp_primary_nid;
1212 lnet_peer_ni_decref_locked(lpni);
1214 lnet_net_unlock(cpt);
1216 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1217 libcfs_nid2str(primary_nid), rc);
1220 EXPORT_SYMBOL(LNetPrimaryNID);
1222 struct lnet_peer_net *
1223 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1225 struct lnet_peer_net *peer_net;
1226 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1227 if (peer_net->lpn_net_id == net_id)
1234 * Attach a peer_ni to a peer_net and peer. This function assumes
1235 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1236 * may be attached to a different peer, in which case it will be
1237 * properly detached first. The whole operation is done atomically.
1239 * Always returns 0. This is the last function called from functions
1240 * that do return an int, so returning 0 here allows the compiler to
1244 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1245 struct lnet_peer_net *lpn,
1246 struct lnet_peer_ni *lpni,
1249 struct lnet_peer_table *ptable;
1251 /* Install the new peer_ni */
1252 lnet_net_lock(LNET_LOCK_EX);
1253 /* Add peer_ni to global peer table hash, if necessary. */
1254 if (list_empty(&lpni->lpni_hashlist)) {
1255 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1257 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1258 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1259 ptable->pt_version++;
1260 ptable->pt_number++;
1261 /* This is the 1st refcount on lpni. */
1262 atomic_inc(&lpni->lpni_refcount);
1265 /* Detach the peer_ni from an existing peer, if necessary. */
1266 if (lpni->lpni_peer_net) {
1267 LASSERT(lpni->lpni_peer_net != lpn);
1268 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1269 lnet_peer_detach_peer_ni_locked(lpni);
1270 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1271 lpni->lpni_peer_net = NULL;
1274 /* Add peer_ni to peer_net */
1275 lpni->lpni_peer_net = lpn;
1276 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1277 lnet_peer_net_addref_locked(lpn);
1279 /* Add peer_net to peer */
1280 if (!lpn->lpn_peer) {
1282 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1283 lnet_peer_addref_locked(lp);
1286 /* Add peer to global peer list, if necessary */
1287 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1288 if (list_empty(&lp->lp_peer_list)) {
1289 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1294 /* Update peer state */
1295 spin_lock(&lp->lp_lock);
1296 if (flags & LNET_PEER_CONFIGURED) {
1297 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1298 lp->lp_state |= LNET_PEER_CONFIGURED;
1300 if (flags & LNET_PEER_MULTI_RAIL) {
1301 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1302 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1303 lnet_peer_clr_non_mr_pref_nids(lp);
1306 spin_unlock(&lp->lp_lock);
1309 lnet_net_unlock(LNET_LOCK_EX);
1311 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1312 libcfs_nid2str(lp->lp_primary_nid),
1313 libcfs_nid2str(lpni->lpni_nid), flags);
1319 * Create a new peer, with nid as its primary nid.
1321 * Call with the lnet_api_mutex held.
1324 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1326 struct lnet_peer *lp;
1327 struct lnet_peer_net *lpn;
1328 struct lnet_peer_ni *lpni;
1331 LASSERT(nid != LNET_NID_ANY);
1334 * No need for the lnet_net_lock here, because the
1335 * lnet_api_mutex is held.
1337 lpni = lnet_find_peer_ni_locked(nid);
1339 /* A peer with this NID already exists. */
1340 lp = lpni->lpni_peer_net->lpn_peer;
1341 lnet_peer_ni_decref_locked(lpni);
1343 * This is an error if the peer was configured and the
1344 * primary NID differs or an attempt is made to change
1345 * the Multi-Rail flag. Otherwise the assumption is
1346 * that an existing peer is being modified.
1348 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1349 if (lp->lp_primary_nid != nid)
1351 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1355 /* Delete and recreate as a configured peer. */
1359 /* Create peer, peer_net, and peer_ni. */
1361 lp = lnet_peer_alloc(nid);
1364 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1367 lpni = lnet_peer_ni_alloc(nid);
1371 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1374 LIBCFS_FREE(lpn, sizeof(*lpn));
1376 LIBCFS_FREE(lp, sizeof(*lp));
1378 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1379 libcfs_nid2str(nid), flags, rc);
1384 * Add a NID to a peer. Call with ln_api_mutex held.
1387 * -EPERM: Non-DLC addition to a DLC-configured peer.
1388 * -EEXIST: The NID was configured by DLC for a different peer.
1389 * -ENOMEM: Out of memory.
1390 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1391 * non-multi-rail peer.
1394 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1396 struct lnet_peer_net *lpn;
1397 struct lnet_peer_ni *lpni;
1401 LASSERT(nid != LNET_NID_ANY);
1403 /* A configured peer can only be updated through configuration. */
1404 if (!(flags & LNET_PEER_CONFIGURED)) {
1405 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1412 * The MULTI_RAIL flag can be set but not cleared, because
1413 * that would leave the peer struct in an invalid state.
1415 if (flags & LNET_PEER_MULTI_RAIL) {
1416 spin_lock(&lp->lp_lock);
1417 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1418 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1419 lnet_peer_clr_non_mr_pref_nids(lp);
1421 spin_unlock(&lp->lp_lock);
1422 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1427 lpni = lnet_find_peer_ni_locked(nid);
1430 * A peer_ni already exists. This is only a problem if
1431 * it is not connected to this peer and was configured
1434 lnet_peer_ni_decref_locked(lpni);
1435 if (lpni->lpni_peer_net->lpn_peer == lp)
1437 if (lnet_peer_ni_is_configured(lpni)) {
1441 /* If this is the primary NID, destroy the peer. */
1442 if (lnet_peer_ni_is_primary(lpni)) {
1443 struct lnet_peer *rtr_lp =
1444 lpni->lpni_peer_net->lpn_peer;
1445 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1447 * if we're trying to delete a router it means
1448 * we're moving this peer NI to a new peer so must
1449 * transfer router properties to the new peer
1451 if (rtr_refcount > 0) {
1452 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1453 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1455 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1456 lpni = lnet_peer_ni_alloc(nid);
1463 lpni = lnet_peer_ni_alloc(nid);
1471 * Get the peer_net. Check that we're not adding a second
1472 * peer_ni on a peer_net of a non-multi-rail peer.
1474 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1476 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1481 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1486 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1489 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1490 if (!lpni->lpni_peer_net)
1491 LIBCFS_FREE(lpni, sizeof(*lpni));
1493 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1494 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1500 * Update the primary NID of a peer, if possible.
1502 * Call with the lnet_api_mutex held.
1505 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1507 lnet_nid_t old = lp->lp_primary_nid;
1510 if (lp->lp_primary_nid == nid)
1512 rc = lnet_peer_add_nid(lp, nid, flags);
1515 lp->lp_primary_nid = nid;
1517 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1518 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1523 * lpni creation initiated due to traffic either sending or receiving.
1526 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1528 struct lnet_peer *lp;
1529 struct lnet_peer_net *lpn;
1530 struct lnet_peer_ni *lpni;
1532 * Assume peer is Multi-Rail capable and let discovery find out
1535 unsigned flags = LNET_PEER_MULTI_RAIL;
1538 if (nid == LNET_NID_ANY) {
1543 /* lnet_net_lock is not needed here because ln_api_lock is held */
1544 lpni = lnet_find_peer_ni_locked(nid);
1547 * We must have raced with another thread. Since we
1548 * know next to nothing about a peer_ni created by
1549 * traffic, we just assume everything is ok and
1552 lnet_peer_ni_decref_locked(lpni);
1556 /* Create peer, peer_net, and peer_ni. */
1558 lp = lnet_peer_alloc(nid);
1561 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1564 lpni = lnet_peer_ni_alloc(nid);
1567 if (pref != LNET_NID_ANY)
1568 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1570 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1573 LIBCFS_FREE(lpn, sizeof(*lpn));
1575 LIBCFS_FREE(lp, sizeof(*lp));
1577 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1582 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1584 * This API handles the following combinations:
1585 * Create a peer with its primary NI if only the prim_nid is provided
1586 * Add a NID to a peer identified by the prim_nid. The peer identified
1587 * by the prim_nid must already exist.
1588 * The peer being created may be non-MR.
1590 * The caller must hold ln_api_mutex. This prevents the peer from
1591 * being created/modified/deleted by a different thread.
1594 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1596 struct lnet_peer *lp = NULL;
1597 struct lnet_peer_ni *lpni;
1600 /* The prim_nid must always be specified */
1601 if (prim_nid == LNET_NID_ANY)
1604 flags = LNET_PEER_CONFIGURED;
1606 flags |= LNET_PEER_MULTI_RAIL;
1609 * If nid isn't specified, we must create a new peer with
1610 * prim_nid as its primary nid.
1612 if (nid == LNET_NID_ANY)
1613 return lnet_peer_add(prim_nid, flags);
1615 /* Look up the prim_nid, which must exist. */
1616 lpni = lnet_find_peer_ni_locked(prim_nid);
1619 lnet_peer_ni_decref_locked(lpni);
1620 lp = lpni->lpni_peer_net->lpn_peer;
1622 /* Peer must have been configured. */
1623 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1624 CDEBUG(D_NET, "peer %s was not configured\n",
1625 libcfs_nid2str(prim_nid));
1629 /* Primary NID must match */
1630 if (lp->lp_primary_nid != prim_nid) {
1631 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1632 libcfs_nid2str(prim_nid),
1633 libcfs_nid2str(lp->lp_primary_nid));
1637 /* Multi-Rail flag must match. */
1638 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1639 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1640 libcfs_nid2str(prim_nid));
1644 return lnet_peer_add_nid(lp, nid, flags);
1648 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1650 * This API handles the following combinations:
1651 * Delete a NI from a peer if both prim_nid and nid are provided.
1652 * Delete a peer if only prim_nid is provided.
1653 * Delete a peer if its primary nid is provided.
1655 * The caller must hold ln_api_mutex. This prevents the peer from
1656 * being modified/deleted by a different thread.
1659 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1661 struct lnet_peer *lp;
1662 struct lnet_peer_ni *lpni;
1665 if (prim_nid == LNET_NID_ANY)
1668 lpni = lnet_find_peer_ni_locked(prim_nid);
1671 lnet_peer_ni_decref_locked(lpni);
1672 lp = lpni->lpni_peer_net->lpn_peer;
1674 if (prim_nid != lp->lp_primary_nid) {
1675 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1676 libcfs_nid2str(prim_nid),
1677 libcfs_nid2str(lp->lp_primary_nid));
1681 lnet_net_lock(LNET_LOCK_EX);
1682 if (lp->lp_rtr_refcount > 0) {
1683 lnet_net_unlock(LNET_LOCK_EX);
1684 CERROR("%s is a router. Can not be deleted\n",
1685 libcfs_nid2str(prim_nid));
1688 lnet_net_unlock(LNET_LOCK_EX);
1690 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1691 return lnet_peer_del(lp);
1693 flags = LNET_PEER_CONFIGURED;
1694 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1695 flags |= LNET_PEER_MULTI_RAIL;
1697 return lnet_peer_del_nid(lp, nid, flags);
1701 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1703 struct lnet_peer_table *ptable;
1704 struct lnet_peer_net *lpn;
1706 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1708 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1709 LASSERT(list_empty(&lpni->lpni_txq));
1710 LASSERT(lpni->lpni_txqnob == 0);
1711 LASSERT(list_empty(&lpni->lpni_peer_nis));
1712 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1714 lpn = lpni->lpni_peer_net;
1715 lpni->lpni_peer_net = NULL;
1716 lpni->lpni_net = NULL;
1718 /* remove the peer ni from the zombie list */
1719 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1720 spin_lock(&ptable->pt_zombie_lock);
1721 list_del_init(&lpni->lpni_hashlist);
1722 ptable->pt_zombies--;
1723 spin_unlock(&ptable->pt_zombie_lock);
1725 if (lpni->lpni_pref_nnids > 1) {
1726 LIBCFS_FREE(lpni->lpni_pref.nids,
1727 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1729 LIBCFS_FREE(lpni, sizeof(*lpni));
1731 lnet_peer_net_decref_locked(lpn);
1734 struct lnet_peer_ni *
1735 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1737 struct lnet_peer_ni *lpni = NULL;
1740 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1741 return ERR_PTR(-ESHUTDOWN);
1744 * find if a peer_ni already exists.
1745 * If so then just return that.
1747 lpni = lnet_find_peer_ni_locked(nid);
1751 lnet_net_unlock(cpt);
1753 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1756 goto out_net_relock;
1759 lpni = lnet_find_peer_ni_locked(nid);
1769 * Get a peer_ni for the given nid, create it if necessary. Takes a
1770 * hold on the peer_ni.
1772 struct lnet_peer_ni *
1773 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1775 struct lnet_peer_ni *lpni = NULL;
1778 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1779 return ERR_PTR(-ESHUTDOWN);
1782 * find if a peer_ni already exists.
1783 * If so then just return that.
1785 lpni = lnet_find_peer_ni_locked(nid);
1791 * use the lnet_api_mutex to serialize the creation of the peer_ni
1792 * and the creation/deletion of the local ni/net. When a local ni is
1793 * created, if there exists a set of peer_nis on that network,
1794 * they need to be traversed and updated. When a local NI is
1795 * deleted, which could result in a network being deleted, then
1796 * all peer nis on that network need to be removed as well.
1798 * Creation through traffic should also be serialized with
1799 * creation through DLC.
1801 lnet_net_unlock(cpt);
1802 mutex_lock(&the_lnet.ln_api_mutex);
1804 * Shutdown is only set under the ln_api_lock, so a single
1805 * check here is sufficent.
1807 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1808 lpni = ERR_PTR(-ESHUTDOWN);
1809 goto out_mutex_unlock;
1812 rc = lnet_peer_ni_traffic_add(nid, pref);
1815 goto out_mutex_unlock;
1818 lpni = lnet_find_peer_ni_locked(nid);
1822 mutex_unlock(&the_lnet.ln_api_mutex);
1825 /* Lock has been dropped, check again for shutdown. */
1826 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1828 lnet_peer_ni_decref_locked(lpni);
1829 lpni = ERR_PTR(-ESHUTDOWN);
1836 lnet_peer_gw_discovery(struct lnet_peer *lp)
1840 spin_lock(&lp->lp_lock);
1841 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1843 spin_unlock(&lp->lp_lock);
1849 lnet_peer_is_uptodate(struct lnet_peer *lp)
1853 spin_lock(&lp->lp_lock);
1854 rc = lnet_peer_is_uptodate_locked(lp);
1855 spin_unlock(&lp->lp_lock);
1860 * Is a peer uptodate from the point of view of discovery?
1862 * If it is currently being processed, obviously not.
1863 * A forced Ping or Push is also handled by the discovery thread.
1865 * Otherwise look at whether the peer needs rediscovering.
1868 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
1869 __must_hold(&lp->lp_lock)
1873 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1874 LNET_PEER_FORCE_PING |
1875 LNET_PEER_FORCE_PUSH)) {
1877 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1879 } else if (lnet_peer_needs_push(lp)) {
1881 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1882 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1894 * Queue a peer for the attention of the discovery thread. Call with
1895 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1896 * -EALREADY if the peer was already queued.
1898 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1902 spin_lock(&lp->lp_lock);
1903 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1904 lp->lp_state |= LNET_PEER_DISCOVERING;
1905 spin_unlock(&lp->lp_lock);
1906 if (list_empty(&lp->lp_dc_list)) {
1907 lnet_peer_addref_locked(lp);
1908 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1909 wake_up(&the_lnet.ln_dc_waitq);
1915 CDEBUG(D_NET, "Queue peer %s: %d\n",
1916 libcfs_nid2str(lp->lp_primary_nid), rc);
1922 * Discovery of a peer is complete. Wake all waiters on the peer.
1923 * Call with lnet_net_lock/EX held.
1925 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1927 struct lnet_msg *msg, *tmp;
1929 struct list_head pending_msgs;
1931 INIT_LIST_HEAD(&pending_msgs);
1933 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1934 libcfs_nid2str(lp->lp_primary_nid));
1936 list_del_init(&lp->lp_dc_list);
1937 spin_lock(&lp->lp_lock);
1938 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1939 spin_unlock(&lp->lp_lock);
1940 wake_up_all(&lp->lp_dc_waitq);
1942 if (lp->lp_rtr_refcount > 0)
1943 lnet_router_discovery_complete(lp);
1945 lnet_net_unlock(LNET_LOCK_EX);
1947 /* iterate through all pending messages and send them again */
1948 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1949 list_del_init(&msg->msg_list);
1950 if (lp->lp_dc_error) {
1951 lnet_finalize(msg, lp->lp_dc_error);
1955 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1956 lnet_msgtyp2str(msg->msg_type),
1957 libcfs_id2str(msg->msg_target));
1958 rc = lnet_send(msg->msg_src_nid_param, msg,
1959 msg->msg_rtr_nid_param);
1961 CNETERR("Error sending %s to %s: %d\n",
1962 lnet_msgtyp2str(msg->msg_type),
1963 libcfs_id2str(msg->msg_target), rc);
1964 lnet_finalize(msg, rc);
1967 lnet_net_lock(LNET_LOCK_EX);
1968 lnet_peer_decref_locked(lp);
1972 * Handle inbound push.
1973 * Like any event handler, called with lnet_res_lock/CPT held.
1975 void lnet_peer_push_event(struct lnet_event *ev)
1977 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1978 struct lnet_peer *lp;
1980 /* lnet_find_peer() adds a refcount */
1981 lp = lnet_find_peer(ev->source.nid);
1983 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1984 libcfs_nid2str(ev->initiator.nid),
1985 libcfs_nid2str(ev->source.nid));
1989 /* Ensure peer state remains consistent while we modify it. */
1990 spin_lock(&lp->lp_lock);
1993 * If some kind of error happened the contents of the message
1994 * cannot be used. Clear the NIDS_UPTODATE and set the
1995 * FORCE_PING flag to trigger a ping.
1998 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1999 lp->lp_state |= LNET_PEER_FORCE_PING;
2000 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2002 libcfs_nid2str(lp->lp_primary_nid),
2003 libcfs_nid2str(ev->source.nid));
2008 * A push with invalid or corrupted info. Clear the UPTODATE
2009 * flag to trigger a ping.
2011 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2012 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2013 lp->lp_state |= LNET_PEER_FORCE_PING;
2014 CDEBUG(D_NET, "Corrupted Push from %s\n",
2015 libcfs_nid2str(lp->lp_primary_nid));
2020 * Make sure we'll allocate the correct size ping buffer when
2023 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2024 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2027 * A non-Multi-Rail peer is not supposed to be capable of
2030 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2031 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2032 libcfs_nid2str(lp->lp_primary_nid));
2037 * Check the MULTIRAIL flag. Complain if the peer was DLC
2038 * configured without it.
2040 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2041 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2042 CERROR("Push says %s is Multi-Rail, DLC says not\n",
2043 libcfs_nid2str(lp->lp_primary_nid));
2045 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2046 lnet_peer_clr_non_mr_pref_nids(lp);
2051 * The peer may have discovery disabled at its end. Set
2052 * NO_DISCOVERY as appropriate.
2054 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2055 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2056 libcfs_nid2str(lp->lp_primary_nid));
2057 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2058 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2059 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2060 libcfs_nid2str(lp->lp_primary_nid));
2061 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2065 * Check for truncation of the Put message. Clear the
2066 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2067 * and tell discovery to allocate a bigger buffer.
2069 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2070 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2071 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2072 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2073 lp->lp_state |= LNET_PEER_FORCE_PING;
2074 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2075 libcfs_nid2str(lp->lp_primary_nid),
2076 pbuf->pb_info.pi_nnis);
2080 /* always assume new data */
2081 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2082 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2085 * If there is data present that hasn't been processed yet,
2086 * we'll replace it if the Put contained newer data and it
2087 * fits. We're racing with a Ping or earlier Push in this
2090 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2091 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2092 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2093 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2094 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2095 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2096 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2097 libcfs_nid2str(lp->lp_primary_nid),
2098 LNET_PING_BUFFER_SEQNO(pbuf),
2099 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2105 * Allocate a buffer to copy the data. On a failure we drop
2106 * the Push and set FORCE_PING to force the discovery
2107 * thread to fix the problem by pinging the peer.
2109 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2111 lp->lp_state |= LNET_PEER_FORCE_PING;
2112 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2113 libcfs_nid2str(lp->lp_primary_nid),
2114 LNET_PING_BUFFER_SEQNO(pbuf));
2119 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2120 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2121 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2122 CDEBUG(D_NET, "Received Push %s %u\n",
2123 libcfs_nid2str(lp->lp_primary_nid),
2124 LNET_PING_BUFFER_SEQNO(pbuf));
2128 * Queue the peer for discovery if not done, force it on the request
2129 * queue and wake the discovery thread if the peer was already queued,
2130 * because its status changed.
2132 spin_unlock(&lp->lp_lock);
2133 lnet_net_lock(LNET_LOCK_EX);
2134 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2135 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2136 wake_up(&the_lnet.ln_dc_waitq);
2138 /* Drop refcount from lookup */
2139 lnet_peer_decref_locked(lp);
2140 lnet_net_unlock(LNET_LOCK_EX);
2144 * Clear the discovery error state, unless we're already discovering
2145 * this peer, in which case the error is current.
2147 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2149 spin_lock(&lp->lp_lock);
2150 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2151 lp->lp_dc_error = 0;
2152 spin_unlock(&lp->lp_lock);
2156 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2157 * dropped/retaken within this function. An lnet_peer_ni is passed in
2158 * because discovery could tear down an lnet_peer.
2161 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2164 struct lnet_peer *lp;
2169 lnet_net_unlock(cpt);
2170 lnet_net_lock(LNET_LOCK_EX);
2171 lp = lpni->lpni_peer_net->lpn_peer;
2172 lnet_peer_clear_discovery_error(lp);
2175 * We're willing to be interrupted. The lpni can become a
2176 * zombie if we race with DLC, so we must check for that.
2179 /* Keep lp alive when the lnet_net_lock is unlocked */
2180 lnet_peer_addref_locked(lp);
2181 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2182 if (signal_pending(current))
2184 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2187 * Don't repeat discovery if discovery is disabled. This is
2188 * done to ensure we can use discovery as a standard ping as
2189 * well for backwards compatibility with routers which do not
2190 * have discovery or have discovery disabled
2192 if (lnet_is_discovery_disabled(lp) && count > 0)
2194 if (lp->lp_dc_error)
2196 if (lnet_peer_is_uptodate(lp))
2198 lnet_peer_queue_for_discovery(lp);
2200 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2203 * If caller requested a non-blocking operation then
2204 * return immediately. Once discovery is complete any
2205 * pending messages that were stopped due to discovery
2206 * will be transmitted.
2211 lnet_net_unlock(LNET_LOCK_EX);
2213 finish_wait(&lp->lp_dc_waitq, &wait);
2214 lnet_net_lock(LNET_LOCK_EX);
2215 lnet_peer_decref_locked(lp);
2216 /* Peer may have changed */
2217 lp = lpni->lpni_peer_net->lpn_peer;
2219 finish_wait(&lp->lp_dc_waitq, &wait);
2221 lnet_net_unlock(LNET_LOCK_EX);
2223 lnet_peer_decref_locked(lp);
2225 * The peer may have changed, so re-check and rediscover if that turns
2226 * out to have been the case. The reference count on lp ensured that
2227 * even if it was unlinked from lpni the memory could not be recycled.
2228 * Thus the check below is sufficient to determine whether the peer
2229 * changed. If the peer changed, then lp must not be dereferenced.
2231 if (lp != lpni->lpni_peer_net->lpn_peer)
2234 if (signal_pending(current))
2236 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2238 else if (lp->lp_dc_error)
2239 rc = lp->lp_dc_error;
2241 CDEBUG(D_NET, "non-blocking discovery\n");
2242 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2245 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2246 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2247 libcfs_nid2str(lpni->lpni_nid), rc,
2248 (!block) ? "pending discovery" : "discovery complete");
2253 /* Handle an incoming ack for a push. */
2255 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2257 struct lnet_ping_buffer *pbuf;
2259 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2260 spin_lock(&lp->lp_lock);
2261 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2262 lp->lp_push_error = ev->status;
2264 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2266 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2267 spin_unlock(&lp->lp_lock);
2269 CDEBUG(D_NET, "peer %s ev->status %d\n",
2270 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2273 /* Handle a Reply message. This is the reply to a Ping message. */
2275 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2277 struct lnet_ping_buffer *pbuf;
2280 spin_lock(&lp->lp_lock);
2283 * If some kind of error happened the contents of message
2284 * cannot be used. Set PING_FAILED to trigger a retry.
2287 lp->lp_state |= LNET_PEER_PING_FAILED;
2288 lp->lp_ping_error = ev->status;
2289 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2291 libcfs_nid2str(lp->lp_primary_nid),
2292 libcfs_nid2str(ev->source.nid));
2296 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2297 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2298 lnet_swap_pinginfo(pbuf);
2301 * A reply with invalid or corrupted info. Set PING_FAILED to
2304 rc = lnet_ping_info_validate(&pbuf->pb_info);
2306 lp->lp_state |= LNET_PEER_PING_FAILED;
2307 lp->lp_ping_error = 0;
2308 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2309 libcfs_nid2str(lp->lp_primary_nid), rc);
2315 * Only enable the multi-rail feature on the peer if both sides of
2316 * the connection have discovery on
2318 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2319 CDEBUG(D_NET, "Peer %s has Multi-Rail feature enabled\n",
2320 libcfs_nid2str(lp->lp_primary_nid));
2321 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2323 CDEBUG(D_NET, "Peer %s has Multi-Rail feature disabled\n",
2324 libcfs_nid2str(lp->lp_primary_nid));
2325 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2329 * The peer may have discovery disabled at its end. Set
2330 * NO_DISCOVERY as appropriate.
2332 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2333 !lnet_peer_discovery_disabled) {
2334 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2335 libcfs_nid2str(lp->lp_primary_nid));
2336 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2338 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2339 libcfs_nid2str(lp->lp_primary_nid));
2340 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2344 * Update the MULTI_RAIL flag based on the reply. If the peer
2345 * was configured with DLC then the setting should match what
2348 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2349 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2350 /* Everything's fine */
2351 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2352 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2353 libcfs_nid2str(lp->lp_primary_nid));
2356 * if discovery is disabled then we don't want to
2357 * update the state of the peer. All we'll do is
2358 * update the peer_nis which were reported back in
2362 if (!lnet_is_discovery_disabled_locked(lp)) {
2363 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2364 lnet_peer_clr_non_mr_pref_nids(lp);
2367 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2368 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2369 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2370 libcfs_nid2str(lp->lp_primary_nid));
2372 CERROR("Multi-Rail state vanished from %s\n",
2373 libcfs_nid2str(lp->lp_primary_nid));
2374 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2379 * Make sure we'll allocate the correct size ping buffer when
2382 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2383 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2386 * Check for truncation of the Reply. Clear PING_SENT and set
2387 * PING_FAILED to trigger a retry.
2389 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2390 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2391 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2392 lp->lp_state |= LNET_PEER_PING_FAILED;
2393 lp->lp_ping_error = 0;
2394 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2395 libcfs_nid2str(lp->lp_primary_nid),
2396 pbuf->pb_info.pi_nnis);
2401 * Check the sequence numbers in the reply. These are only
2402 * available if the reply came from a Multi-Rail peer.
2404 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2405 pbuf->pb_info.pi_nnis > 1 &&
2406 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2407 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2408 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2409 libcfs_nid2str(lp->lp_primary_nid),
2410 LNET_PING_BUFFER_SEQNO(pbuf),
2413 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2416 /* We're happy with the state of the data in the buffer. */
2417 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2418 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2419 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2420 lnet_ping_buffer_decref(lp->lp_data);
2422 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2423 lnet_ping_buffer_addref(pbuf);
2426 lp->lp_state &= ~LNET_PEER_PING_SENT;
2427 spin_unlock(&lp->lp_lock);
2429 lnet_net_lock(LNET_LOCK_EX);
2431 * If this peer is a gateway, call the routing callback to
2432 * handle the ping reply
2434 if (lp->lp_rtr_refcount > 0)
2435 lnet_router_discovery_ping_reply(lp);
2436 lnet_net_unlock(LNET_LOCK_EX);
2440 * Send event handling. Only matters for error cases, where we clean
2441 * up state on the peer and peer_ni that would otherwise be updated in
2442 * the REPLY event handler for a successful Ping, and the ACK event
2443 * handler for a successful Push.
2446 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2453 spin_lock(&lp->lp_lock);
2454 if (ev->msg_type == LNET_MSG_GET) {
2455 lp->lp_state &= ~LNET_PEER_PING_SENT;
2456 lp->lp_state |= LNET_PEER_PING_FAILED;
2457 lp->lp_ping_error = ev->status;
2458 } else { /* ev->msg_type == LNET_MSG_PUT */
2459 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2460 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2461 lp->lp_push_error = ev->status;
2463 spin_unlock(&lp->lp_lock);
2464 rc = LNET_REDISCOVER_PEER;
2466 CDEBUG(D_NET, "%s Send to %s: %d\n",
2467 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2468 libcfs_nid2str(ev->target.nid), rc);
2473 * Unlink event handling. This event is only seen if a call to
2474 * LNetMDUnlink() caused the event to be unlinked. If this call was
2475 * made after the event was set up in LNetGet() or LNetPut() then we
2476 * assume the Ping or Push timed out.
2479 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2481 spin_lock(&lp->lp_lock);
2482 /* We've passed through LNetGet() */
2483 if (lp->lp_state & LNET_PEER_PING_SENT) {
2484 lp->lp_state &= ~LNET_PEER_PING_SENT;
2485 lp->lp_state |= LNET_PEER_PING_FAILED;
2486 lp->lp_ping_error = -ETIMEDOUT;
2487 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2488 libcfs_nid2str(lp->lp_primary_nid));
2490 /* We've passed through LNetPut() */
2491 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2492 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2493 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2494 lp->lp_push_error = -ETIMEDOUT;
2495 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2496 libcfs_nid2str(lp->lp_primary_nid));
2498 spin_unlock(&lp->lp_lock);
2502 * Event handler for the discovery EQ.
2504 * Called with lnet_res_lock(cpt) held. The cpt is the
2505 * lnet_cpt_of_cookie() of the md handle cookie.
2507 static void lnet_discovery_event_handler(struct lnet_event *event)
2509 struct lnet_peer *lp = event->md.user_ptr;
2510 struct lnet_ping_buffer *pbuf;
2513 /* discovery needs to take another look */
2514 rc = LNET_REDISCOVER_PEER;
2516 CDEBUG(D_NET, "Received event: %d\n", event->type);
2518 switch (event->type) {
2519 case LNET_EVENT_ACK:
2520 lnet_discovery_event_ack(lp, event);
2522 case LNET_EVENT_REPLY:
2523 lnet_discovery_event_reply(lp, event);
2525 case LNET_EVENT_SEND:
2526 /* Only send failure triggers a retry. */
2527 rc = lnet_discovery_event_send(lp, event);
2529 case LNET_EVENT_UNLINK:
2530 /* LNetMDUnlink() was called */
2531 lnet_discovery_event_unlink(lp, event);
2534 /* Invalid events. */
2537 lnet_net_lock(LNET_LOCK_EX);
2538 if (event->unlinked) {
2539 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2540 lnet_ping_buffer_decref(pbuf);
2541 lnet_peer_decref_locked(lp);
2544 /* put peer back at end of request queue, if discovery not already
2546 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2547 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2548 wake_up(&the_lnet.ln_dc_waitq);
2550 lnet_net_unlock(LNET_LOCK_EX);
2554 * Build a peer from incoming data.
2556 * The NIDs in the incoming data are supposed to be structured as follows:
2559 * - other NIDs in same net
2560 * - NIDs in second net
2561 * - NIDs in third net
2563 * This due to the way the list of NIDs in the data is created.
2565 * Note that this function will mark the peer uptodate unless an
2566 * ENOMEM is encontered. All other errors are due to a conflict
2567 * between the DLC configuration and what discovery sees. We treat DLC
2568 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2569 * peer from becoming stuck in discovery.
2571 static int lnet_peer_merge_data(struct lnet_peer *lp,
2572 struct lnet_ping_buffer *pbuf)
2574 struct lnet_peer_ni *lpni;
2575 lnet_nid_t *curnis = NULL;
2576 struct lnet_ni_status *addnis = NULL;
2577 lnet_nid_t *delnis = NULL;
2587 flags = LNET_PEER_DISCOVERED;
2588 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2589 flags |= LNET_PEER_MULTI_RAIL;
2592 * Cache the routing feature for the peer; whether it is enabled
2593 * for disabled as reported by the remote peer.
2595 spin_lock(&lp->lp_lock);
2596 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2597 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2599 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2600 spin_unlock(&lp->lp_lock);
2602 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2603 LIBCFS_ALLOC(curnis, nnis * sizeof(*curnis));
2604 LIBCFS_ALLOC(addnis, nnis * sizeof(*addnis));
2605 LIBCFS_ALLOC(delnis, nnis * sizeof(*delnis));
2606 if (!curnis || !addnis || !delnis) {
2614 /* Construct the list of NIDs present in peer. */
2616 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2617 curnis[ncurnis++] = lpni->lpni_nid;
2620 * Check for NIDs in pbuf not present in curnis[].
2621 * The loop starts at 1 to skip the loopback NID.
2623 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2624 for (j = 0; j < ncurnis; j++)
2625 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2628 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2631 * Check for NIDs in curnis[] not present in pbuf.
2632 * The nested loop starts at 1 to skip the loopback NID.
2634 * But never add the loopback NID to delnis[]: if it is
2635 * present in curnis[] then this peer is for this node.
2637 for (i = 0; i < ncurnis; i++) {
2638 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2640 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2641 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2643 * update the information we cache for the
2644 * peer with the latest information we
2647 lpni = lnet_find_peer_ni_locked(curnis[i]);
2649 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2650 lnet_peer_ni_decref_locked(lpni);
2655 if (j == pbuf->pb_info.pi_nnis)
2656 delnis[ndelnis++] = curnis[i];
2660 * If we get here and the discovery is disabled then we don't want
2661 * to add or delete any NIs. We just updated the ones we have some
2662 * information on, and call it a day
2665 if (lnet_is_discovery_disabled(lp))
2668 for (i = 0; i < naddnis; i++) {
2669 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2671 CERROR("Error adding NID %s to peer %s: %d\n",
2672 libcfs_nid2str(addnis[i].ns_nid),
2673 libcfs_nid2str(lp->lp_primary_nid), rc);
2677 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2679 lpni->lpni_ns_status = addnis[i].ns_status;
2680 lnet_peer_ni_decref_locked(lpni);
2684 for (i = 0; i < ndelnis; i++) {
2686 * for routers it's okay to delete the primary_nid because
2687 * the upper layers don't really rely on it. So if we're
2688 * being told that the router changed its primary_nid
2689 * then it's okay to delete it.
2691 if (lp->lp_rtr_refcount > 0)
2692 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2693 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2695 CERROR("Error deleting NID %s from peer %s: %d\n",
2696 libcfs_nid2str(delnis[i]),
2697 libcfs_nid2str(lp->lp_primary_nid), rc);
2703 * Errors other than -ENOMEM are due to peers having been
2704 * configured with DLC. Ignore these because DLC overrides
2709 LIBCFS_FREE(curnis, nnis * sizeof(*curnis));
2710 LIBCFS_FREE(addnis, nnis * sizeof(*addnis));
2711 LIBCFS_FREE(delnis, nnis * sizeof(*delnis));
2712 lnet_ping_buffer_decref(pbuf);
2713 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2716 spin_lock(&lp->lp_lock);
2717 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2718 lp->lp_state |= LNET_PEER_FORCE_PING;
2719 spin_unlock(&lp->lp_lock);
2725 * The data in pbuf says lp is its primary peer, but the data was
2726 * received by a different peer. Try to update lp with the data.
2729 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2731 struct lnet_handle_md mdh;
2733 /* Queue lp for discovery, and force it on the request queue. */
2734 lnet_net_lock(LNET_LOCK_EX);
2735 if (lnet_peer_queue_for_discovery(lp))
2736 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2737 lnet_net_unlock(LNET_LOCK_EX);
2739 LNetInvalidateMDHandle(&mdh);
2742 * Decide whether we can move the peer to the DATA_PRESENT state.
2744 * We replace stale data for a multi-rail peer, repair PING_FAILED
2745 * status, and preempt FORCE_PING.
2747 * If after that we have DATA_PRESENT, we merge it into this peer.
2749 spin_lock(&lp->lp_lock);
2750 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2751 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2752 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2753 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2754 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2755 lnet_ping_buffer_decref(pbuf);
2760 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2761 lnet_ping_buffer_decref(lp->lp_data);
2763 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2765 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2766 mdh = lp->lp_ping_mdh;
2767 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2768 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2769 lp->lp_ping_error = 0;
2771 if (lp->lp_state & LNET_PEER_FORCE_PING)
2772 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2773 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2774 spin_unlock(&lp->lp_lock);
2776 if (!LNetMDHandleIsInvalid(mdh))
2780 return lnet_peer_merge_data(lp, pbuf);
2782 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2786 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2790 for (i = 0; i < pinfo->pi_nnis; i++) {
2791 if (pinfo->pi_ni[i].ns_nid == nid)
2799 * Update a peer using the data received.
2801 static int lnet_peer_data_present(struct lnet_peer *lp)
2802 __must_hold(&lp->lp_lock)
2804 struct lnet_ping_buffer *pbuf;
2805 struct lnet_peer_ni *lpni;
2806 lnet_nid_t nid = LNET_NID_ANY;
2812 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2813 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2814 spin_unlock(&lp->lp_lock);
2817 * Modifications of peer structures are done while holding the
2818 * ln_api_mutex. A global lock is required because we may be
2819 * modifying multiple peer structures, and a mutex greatly
2820 * simplifies memory management.
2822 * The actual changes to the data structures must also protect
2823 * against concurrent lookups, for which the lnet_net_lock in
2824 * LNET_LOCK_EX mode is used.
2826 mutex_lock(&the_lnet.ln_api_mutex);
2827 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2833 * If this peer is not on the peer list then it is being torn
2834 * down, and our reference count may be all that is keeping it
2835 * alive. Don't do any work on it.
2837 if (list_empty(&lp->lp_peer_list))
2840 flags = LNET_PEER_DISCOVERED;
2841 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2842 flags |= LNET_PEER_MULTI_RAIL;
2845 * Check whether the primary NID in the message matches the
2846 * primary NID of the peer. If it does, update the peer, if
2847 * it it does not, check whether there is already a peer with
2848 * that primary NID. If no such peer exists, try to update
2849 * the primary NID of the current peer (allowed if it was
2850 * created due to message traffic) and complete the update.
2851 * If the peer did exist, hand off the data to it.
2853 * The peer for the loopback interface is a special case: this
2854 * is the peer for the local node, and we want to set its
2855 * primary NID to the correct value here. Moreover, this peer
2856 * can show up with only the loopback NID in the ping buffer.
2858 if (pbuf->pb_info.pi_nnis <= 1)
2860 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2861 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2862 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2864 rc = lnet_peer_merge_data(lp, pbuf);
2866 * if the primary nid of the peer is present in the ping info returned
2867 * from the peer, but it's not the local primary peer we have
2868 * cached and discovery is disabled, then we don't want to update
2869 * our local peer info, by adding or removing NIDs, we just want
2870 * to update the status of the nids that we currently have
2871 * recorded in that peer.
2873 } else if (lp->lp_primary_nid == nid ||
2874 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2875 lnet_is_discovery_disabled(lp))) {
2876 rc = lnet_peer_merge_data(lp, pbuf);
2878 lpni = lnet_find_peer_ni_locked(nid);
2880 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2882 CERROR("Primary NID error %s versus %s: %d\n",
2883 libcfs_nid2str(lp->lp_primary_nid),
2884 libcfs_nid2str(nid), rc);
2886 rc = lnet_peer_merge_data(lp, pbuf);
2889 struct lnet_peer *new_lp;
2890 new_lp = lpni->lpni_peer_net->lpn_peer;
2892 * if lp has discovery/MR enabled that means new_lp
2893 * should have discovery/MR enabled as well, since
2894 * it's the same peer, which we're about to merge
2896 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2897 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2898 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2899 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2901 rc = lnet_peer_set_primary_data(new_lp, pbuf);
2902 lnet_consolidate_routes_locked(lp, new_lp);
2903 lnet_peer_ni_decref_locked(lpni);
2907 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2909 mutex_unlock(&the_lnet.ln_api_mutex);
2911 spin_lock(&lp->lp_lock);
2912 /* Tell discovery to re-check the peer immediately. */
2914 rc = LNET_REDISCOVER_PEER;
2919 * A ping failed. Clear the PING_FAILED state and set the
2920 * FORCE_PING state, to ensure a retry even if discovery is
2921 * disabled. This avoids being left with incorrect state.
2923 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2924 __must_hold(&lp->lp_lock)
2926 struct lnet_handle_md mdh;
2929 mdh = lp->lp_ping_mdh;
2930 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2931 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2932 lp->lp_state |= LNET_PEER_FORCE_PING;
2933 rc = lp->lp_ping_error;
2934 lp->lp_ping_error = 0;
2935 spin_unlock(&lp->lp_lock);
2937 if (!LNetMDHandleIsInvalid(mdh))
2940 CDEBUG(D_NET, "peer %s:%d\n",
2941 libcfs_nid2str(lp->lp_primary_nid), rc);
2943 spin_lock(&lp->lp_lock);
2944 return rc ? rc : LNET_REDISCOVER_PEER;
2948 * Select NID to send a Ping or Push to.
2950 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2952 struct lnet_peer_ni *lpni;
2954 /* Look for a direct-connected NID for this peer. */
2956 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2957 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2962 return lpni->lpni_nid;
2964 /* Look for a routed-connected NID for this peer. */
2966 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2967 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2972 return lpni->lpni_nid;
2974 return LNET_NID_ANY;
2977 /* Active side of ping. */
2978 static int lnet_peer_send_ping(struct lnet_peer *lp)
2979 __must_hold(&lp->lp_lock)
2986 lp->lp_state |= LNET_PEER_PING_SENT;
2987 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2988 spin_unlock(&lp->lp_lock);
2990 cpt = lnet_net_lock_current();
2991 /* Refcount for MD. */
2992 lnet_peer_addref_locked(lp);
2993 pnid = lnet_peer_select_nid(lp);
2994 lnet_net_unlock(cpt);
2996 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2998 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2999 the_lnet.ln_dc_eqh, false);
3002 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3003 * refcount on the peer, otherwise LNetMDUnlink will be called
3004 * which will eventually do that.
3008 lnet_peer_decref_locked(lp);
3009 lnet_net_unlock(cpt);
3010 rc = -rc; /* change the rc to negative value */
3012 } else if (rc < 0) {
3016 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3018 spin_lock(&lp->lp_lock);
3022 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3024 * The errors that get us here are considered hard errors and
3025 * cause Discovery to terminate. So we clear PING_SENT, but do
3026 * not set either PING_FAILED or FORCE_PING. In fact we need
3027 * to clear PING_FAILED, because the unlink event handler will
3028 * have set it if we called LNetMDUnlink() above.
3030 spin_lock(&lp->lp_lock);
3031 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3036 * This function exists because you cannot call LNetMDUnlink() from an
3039 static int lnet_peer_push_failed(struct lnet_peer *lp)
3040 __must_hold(&lp->lp_lock)
3042 struct lnet_handle_md mdh;
3045 mdh = lp->lp_push_mdh;
3046 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3047 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3048 rc = lp->lp_push_error;
3049 lp->lp_push_error = 0;
3050 spin_unlock(&lp->lp_lock);
3052 if (!LNetMDHandleIsInvalid(mdh))
3055 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3056 spin_lock(&lp->lp_lock);
3057 return rc ? rc : LNET_REDISCOVER_PEER;
3060 /* Active side of push. */
3061 static int lnet_peer_send_push(struct lnet_peer *lp)
3062 __must_hold(&lp->lp_lock)
3064 struct lnet_ping_buffer *pbuf;
3065 struct lnet_process_id id;
3070 /* Don't push to a non-multi-rail peer. */
3071 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3072 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3076 lp->lp_state |= LNET_PEER_PUSH_SENT;
3077 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3078 spin_unlock(&lp->lp_lock);
3080 cpt = lnet_net_lock_current();
3081 pbuf = the_lnet.ln_ping_target;
3082 lnet_ping_buffer_addref(pbuf);
3083 lnet_net_unlock(cpt);
3085 /* Push source MD */
3086 md.start = &pbuf->pb_info;
3087 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3088 md.threshold = 2; /* Put/Ack */
3091 md.eq_handle = the_lnet.ln_dc_eqh;
3094 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
3096 lnet_ping_buffer_decref(pbuf);
3097 CERROR("Can't bind push source MD: %d\n", rc);
3100 cpt = lnet_net_lock_current();
3101 /* Refcount for MD. */
3102 lnet_peer_addref_locked(lp);
3103 id.pid = LNET_PID_LUSTRE;
3104 id.nid = lnet_peer_select_nid(lp);
3105 lnet_net_unlock(cpt);
3107 if (id.nid == LNET_NID_ANY) {
3112 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
3113 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3114 LNET_PROTO_PING_MATCHBITS, 0, 0);
3119 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3121 spin_lock(&lp->lp_lock);
3125 LNetMDUnlink(lp->lp_push_mdh);
3126 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3128 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3130 * The errors that get us here are considered hard errors and
3131 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3132 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3133 * because the unlink event handler will have set it if we
3134 * called LNetMDUnlink() above.
3136 spin_lock(&lp->lp_lock);
3137 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3142 * An unrecoverable error was encountered during discovery.
3143 * Set error status in peer and abort discovery.
3145 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3147 CDEBUG(D_NET, "Discovery error %s: %d\n",
3148 libcfs_nid2str(lp->lp_primary_nid), error);
3150 spin_lock(&lp->lp_lock);
3151 lp->lp_dc_error = error;
3152 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3153 lp->lp_state |= LNET_PEER_REDISCOVER;
3154 spin_unlock(&lp->lp_lock);
3158 * Mark the peer as discovered.
3160 static int lnet_peer_discovered(struct lnet_peer *lp)
3161 __must_hold(&lp->lp_lock)
3163 lp->lp_state |= LNET_PEER_DISCOVERED;
3164 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3165 LNET_PEER_REDISCOVER);
3167 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3174 * Discovering this peer is taking too long. Cancel any Ping or Push
3175 * that discovery is waiting on by unlinking the relevant MDs. The
3176 * lnet_discovery_event_handler() will proceed from here and complete
3179 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3181 struct lnet_handle_md ping_mdh;
3182 struct lnet_handle_md push_mdh;
3184 LNetInvalidateMDHandle(&ping_mdh);
3185 LNetInvalidateMDHandle(&push_mdh);
3187 spin_lock(&lp->lp_lock);
3188 if (lp->lp_state & LNET_PEER_PING_SENT) {
3189 ping_mdh = lp->lp_ping_mdh;
3190 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3192 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3193 push_mdh = lp->lp_push_mdh;
3194 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3196 spin_unlock(&lp->lp_lock);
3198 if (!LNetMDHandleIsInvalid(ping_mdh))
3199 LNetMDUnlink(ping_mdh);
3200 if (!LNetMDHandleIsInvalid(push_mdh))
3201 LNetMDUnlink(push_mdh);
3205 * Wait for work to be queued or some other change that must be
3206 * attended to. Returns non-zero if the discovery thread should shut
3209 static int lnet_peer_discovery_wait_for_work(void)
3216 cpt = lnet_net_lock_current();
3218 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3219 TASK_INTERRUPTIBLE);
3220 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3222 if (lnet_push_target_resize_needed())
3224 if (!list_empty(&the_lnet.ln_dc_request))
3226 if (!list_empty(&the_lnet.ln_msg_resend))
3228 lnet_net_unlock(cpt);
3231 * wakeup max every second to check if there are peers that
3232 * have been stuck on the working queue for greater than
3235 schedule_timeout(cfs_time_seconds(1));
3236 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3237 cpt = lnet_net_lock_current();
3239 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3241 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3244 lnet_net_unlock(cpt);
3246 CDEBUG(D_NET, "woken: %d\n", rc);
3252 * Messages that were pending on a destroyed peer will be put on a global
3253 * resend list. The message resend list will be checked by
3254 * the discovery thread when it wakes up, and will resend messages. These
3255 * messages can still be sendable in the case the lpni which was the initial
3256 * cause of the message re-queue was transfered to another peer.
3258 * It is possible that LNet could be shutdown while we're iterating
3259 * through the list. lnet_shudown_lndnets() will attempt to access the
3260 * resend list, but will have to wait until the spinlock is released, by
3261 * which time there shouldn't be any more messages on the resend list.
3262 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3263 * for the messages so they can be released. The other case is that
3264 * lnet_shudown_lndnets() can finalize all the messages before this
3265 * function can visit the resend list, in which case this function will be
3268 static void lnet_resend_msgs(void)
3270 struct lnet_msg *msg, *tmp;
3271 struct list_head resend;
3274 INIT_LIST_HEAD(&resend);
3276 spin_lock(&the_lnet.ln_msg_resend_lock);
3277 list_splice(&the_lnet.ln_msg_resend, &resend);
3278 spin_unlock(&the_lnet.ln_msg_resend_lock);
3280 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3281 list_del_init(&msg->msg_list);
3282 rc = lnet_send(msg->msg_src_nid_param, msg,
3283 msg->msg_rtr_nid_param);
3285 CNETERR("Error sending %s to %s: %d\n",
3286 lnet_msgtyp2str(msg->msg_type),
3287 libcfs_id2str(msg->msg_target), rc);
3288 lnet_finalize(msg, rc);
3293 /* The discovery thread. */
3294 static int lnet_peer_discovery(void *arg)
3296 struct lnet_peer *lp;
3299 wait_for_completion(&the_lnet.ln_started);
3301 CDEBUG(D_NET, "started\n");
3302 cfs_block_allsigs();
3305 if (lnet_peer_discovery_wait_for_work())
3310 if (lnet_push_target_resize_needed())
3311 lnet_push_target_resize();
3313 lnet_net_lock(LNET_LOCK_EX);
3314 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3318 * Process all incoming discovery work requests. When
3319 * discovery must wait on a peer to change state, it
3320 * is added to the tail of the ln_dc_working queue. A
3321 * timestamp keeps track of when the peer was added,
3322 * so we can time out discovery requests that take too
3325 while (!list_empty(&the_lnet.ln_dc_request)) {
3326 lp = list_first_entry(&the_lnet.ln_dc_request,
3327 struct lnet_peer, lp_dc_list);
3328 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3330 * set the time the peer was put on the dc_working
3331 * queue. It shouldn't remain on the queue
3332 * forever, in case the GET message (for ping)
3333 * doesn't get a REPLY or the PUT message (for
3334 * push) doesn't get an ACK.
3336 lp->lp_last_queued = ktime_get_real_seconds();
3337 lnet_net_unlock(LNET_LOCK_EX);
3340 * Select an action depending on the state of
3341 * the peer and whether discovery is disabled.
3342 * The check whether discovery is disabled is
3343 * done after the code that handles processing
3344 * for arrived data, cleanup for failures, and
3345 * forcing a Ping or Push.
3347 spin_lock(&lp->lp_lock);
3348 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3349 libcfs_nid2str(lp->lp_primary_nid), lp,
3351 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3352 rc = lnet_peer_data_present(lp);
3353 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3354 rc = lnet_peer_ping_failed(lp);
3355 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3356 rc = lnet_peer_push_failed(lp);
3357 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3358 rc = lnet_peer_send_ping(lp);
3359 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3360 rc = lnet_peer_send_push(lp);
3361 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3362 rc = lnet_peer_send_ping(lp);
3363 else if (lnet_peer_needs_push(lp))
3364 rc = lnet_peer_send_push(lp);
3366 rc = lnet_peer_discovered(lp);
3367 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3368 libcfs_nid2str(lp->lp_primary_nid), lp,
3370 spin_unlock(&lp->lp_lock);
3372 lnet_net_lock(LNET_LOCK_EX);
3373 if (rc == LNET_REDISCOVER_PEER) {
3374 list_move(&lp->lp_dc_list,
3375 &the_lnet.ln_dc_request);
3377 lnet_peer_discovery_error(lp, rc);
3379 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3380 lnet_peer_discovery_complete(lp);
3381 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3385 lnet_net_unlock(LNET_LOCK_EX);
3388 CDEBUG(D_NET, "stopping\n");
3390 * Clean up before telling lnet_peer_discovery_stop() that
3391 * we're done. Use wake_up() below to somewhat reduce the
3392 * size of the thundering herd if there are multiple threads
3393 * waiting on discovery of a single peer.
3396 /* Queue cleanup 1: stop all pending pings and pushes. */
3397 lnet_net_lock(LNET_LOCK_EX);
3398 while (!list_empty(&the_lnet.ln_dc_working)) {
3399 lp = list_first_entry(&the_lnet.ln_dc_working,
3400 struct lnet_peer, lp_dc_list);
3401 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3402 lnet_net_unlock(LNET_LOCK_EX);
3403 lnet_peer_cancel_discovery(lp);
3404 lnet_net_lock(LNET_LOCK_EX);
3406 lnet_net_unlock(LNET_LOCK_EX);
3408 /* Queue cleanup 2: wait for the expired queue to clear. */
3409 while (!list_empty(&the_lnet.ln_dc_expired))
3410 schedule_timeout(cfs_time_seconds(1));
3412 /* Queue cleanup 3: clear the request queue. */
3413 lnet_net_lock(LNET_LOCK_EX);
3414 while (!list_empty(&the_lnet.ln_dc_request)) {
3415 lp = list_first_entry(&the_lnet.ln_dc_request,
3416 struct lnet_peer, lp_dc_list);
3417 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3418 lnet_peer_discovery_complete(lp);
3420 lnet_net_unlock(LNET_LOCK_EX);
3422 LNetEQFree(the_lnet.ln_dc_eqh);
3423 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3425 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3426 wake_up(&the_lnet.ln_dc_waitq);
3428 CDEBUG(D_NET, "stopped\n");
3433 /* ln_api_mutex is held on entry. */
3434 int lnet_peer_discovery_start(void)
3436 struct task_struct *task;
3439 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3442 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3444 CERROR("Can't allocate discovery EQ: %d\n", rc);
3448 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3449 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3452 CERROR("Can't start peer discovery thread: %d\n", rc);
3454 LNetEQFree(the_lnet.ln_dc_eqh);
3455 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3457 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3460 CDEBUG(D_NET, "discovery start: %d\n", rc);
3465 /* ln_api_mutex is held on entry. */
3466 void lnet_peer_discovery_stop(void)
3468 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3471 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3472 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3474 /* In the LNetNIInit() path we may be stopping discovery before it
3475 * entered its work loop
3477 if (!completion_done(&the_lnet.ln_started))
3478 complete(&the_lnet.ln_started);
3480 wake_up(&the_lnet.ln_dc_waitq);
3482 wait_event(the_lnet.ln_dc_waitq,
3483 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3485 LASSERT(list_empty(&the_lnet.ln_dc_request));
3486 LASSERT(list_empty(&the_lnet.ln_dc_working));
3487 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3489 CDEBUG(D_NET, "discovery stopped\n");
3495 lnet_debug_peer(lnet_nid_t nid)
3497 char *aliveness = "NA";
3498 struct lnet_peer_ni *lp;
3501 cpt = lnet_cpt_of_nid(nid, NULL);
3504 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3506 lnet_net_unlock(cpt);
3507 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3511 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3512 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3514 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3515 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3516 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3517 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3518 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3520 lnet_peer_ni_decref_locked(lp);
3522 lnet_net_unlock(cpt);
3525 /* Gathering information for userspace. */
3527 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3528 char aliveness[LNET_MAX_STR_LEN],
3529 __u32 *cpt_iter, __u32 *refcount,
3530 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3531 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3532 __u32 *peer_tx_qnob)
3534 struct lnet_peer_table *peer_table;
3535 struct lnet_peer_ni *lp;
3540 /* get the number of CPTs */
3541 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3543 /* if the cpt number to be examined is >= the number of cpts in
3544 * the system then indicate that there are no more cpts to examin
3546 if (*cpt_iter >= lncpt)
3549 /* get the current table */
3550 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3551 /* if the ptable is NULL then there are no more cpts to examine */
3552 if (peer_table == NULL)
3555 lnet_net_lock(*cpt_iter);
3557 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3558 struct list_head *peers = &peer_table->pt_hash[j];
3560 list_for_each_entry(lp, peers, lpni_hashlist) {
3561 if (peer_index-- > 0)
3564 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3565 if (lnet_isrouter(lp) ||
3566 lnet_peer_aliveness_enabled(lp))
3567 snprintf(aliveness, LNET_MAX_STR_LEN,
3568 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3570 *nid = lp->lpni_nid;
3571 *refcount = atomic_read(&lp->lpni_refcount);
3572 *ni_peer_tx_credits =
3573 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3574 *peer_tx_credits = lp->lpni_txcredits;
3575 *peer_rtr_credits = lp->lpni_rtrcredits;
3576 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3577 *peer_tx_qnob = lp->lpni_txqnob;
3583 lnet_net_unlock(*cpt_iter);
3587 return found ? 0 : -ENOENT;
3590 /* ln_api_mutex is held, which keeps the peer list stable */
3591 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3593 struct lnet_ioctl_element_stats *lpni_stats;
3594 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3595 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3596 struct lnet_peer_ni_credit_info *lpni_info;
3597 struct lnet_peer_ni *lpni;
3598 struct lnet_peer *lp;
3603 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3610 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3611 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3612 size *= lp->lp_nnis;
3613 if (size > cfg->prcfg_size) {
3614 cfg->prcfg_size = size;
3619 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3620 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3621 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3622 cfg->prcfg_count = lp->lp_nnis;
3623 cfg->prcfg_size = size;
3624 cfg->prcfg_state = lp->lp_state;
3626 /* Allocate helper buffers. */
3628 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3631 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3634 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3635 if (!lpni_msg_stats)
3636 goto out_free_stats;
3637 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3639 goto out_free_msg_stats;
3644 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3645 nid = lpni->lpni_nid;
3646 if (copy_to_user(bulk, &nid, sizeof(nid)))
3647 goto out_free_hstats;
3648 bulk += sizeof(nid);
3650 memset(lpni_info, 0, sizeof(*lpni_info));
3651 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3652 if (lnet_isrouter(lpni) ||
3653 lnet_peer_aliveness_enabled(lpni))
3654 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3655 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3657 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3658 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3659 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3660 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3661 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3662 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3663 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3664 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3665 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3666 goto out_free_hstats;
3667 bulk += sizeof(*lpni_info);
3669 memset(lpni_stats, 0, sizeof(*lpni_stats));
3670 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3671 LNET_STATS_TYPE_SEND);
3672 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3673 LNET_STATS_TYPE_RECV);
3674 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3675 LNET_STATS_TYPE_DROP);
3676 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3677 goto out_free_hstats;
3678 bulk += sizeof(*lpni_stats);
3679 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3680 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3681 goto out_free_hstats;
3682 bulk += sizeof(*lpni_msg_stats);
3683 lpni_hstats->hlpni_network_timeout =
3684 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3685 lpni_hstats->hlpni_remote_dropped =
3686 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3687 lpni_hstats->hlpni_remote_timeout =
3688 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3689 lpni_hstats->hlpni_remote_error =
3690 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3691 lpni_hstats->hlpni_health_value =
3692 atomic_read(&lpni->lpni_healthv);
3693 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3694 goto out_free_hstats;
3695 bulk += sizeof(*lpni_hstats);
3700 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3702 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3704 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3706 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3708 lnet_peer_decref_locked(lp);
3714 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3716 /* the mt could've shutdown and cleaned up the queues */
3717 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3720 if (list_empty(&lpni->lpni_recovery) &&
3721 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3722 CERROR("lpni %s added to recovery queue. Health = %d\n",
3723 libcfs_nid2str(lpni->lpni_nid),
3724 atomic_read(&lpni->lpni_healthv));
3725 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3726 lnet_peer_ni_addref_locked(lpni);
3730 /* Call with the ln_api_mutex held */
3732 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3734 struct lnet_peer_table *ptable;
3735 struct lnet_peer *lp;
3736 struct lnet_peer_net *lpn;
3737 struct lnet_peer_ni *lpni;
3741 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3745 lnet_net_lock(LNET_LOCK_EX);
3746 lpni = lnet_find_peer_ni_locked(nid);
3748 lnet_net_unlock(LNET_LOCK_EX);
3751 atomic_set(&lpni->lpni_healthv, value);
3752 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3753 lnet_peer_ni_decref_locked(lpni);
3754 lnet_net_unlock(LNET_LOCK_EX);
3758 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3761 * Walk all the peers and reset the healhv for each one to the
3764 lnet_net_lock(LNET_LOCK_EX);
3765 for (cpt = 0; cpt < lncpt; cpt++) {
3766 ptable = the_lnet.ln_peer_tables[cpt];
3767 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3768 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3769 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3771 atomic_set(&lpni->lpni_healthv, value);
3772 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3777 lnet_net_unlock(LNET_LOCK_EX);