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 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
886 for (cpt = 0; cpt < lncpt; cpt++) {
887 ptable = the_lnet.ln_peer_tables[cpt];
888 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
890 spin_lock(&lp->lp_lock);
891 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
892 lp->lp_state |= LNET_PEER_FORCE_PUSH;
893 spin_unlock(&lp->lp_lock);
895 if (lnet_peer_needs_push(lp))
896 lnet_peer_queue_for_discovery(lp);
899 lnet_net_unlock(LNET_LOCK_EX);
900 wake_up(&the_lnet.ln_dc_waitq);
904 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
905 * this is a preferred point-to-point path. Call with lnet_net_lock in
909 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
913 if (lpni->lpni_pref_nnids == 0)
915 if (lpni->lpni_pref_nnids == 1)
916 return lpni->lpni_pref.nid == nid;
917 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
918 if (lpni->lpni_pref.nids[i] == nid)
925 * Set a single ni as preferred, provided no preferred ni is already
926 * defined. Only to be used for non-multi-rail peer_ni.
929 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
933 spin_lock(&lpni->lpni_lock);
934 if (nid == LNET_NID_ANY) {
936 } else if (lpni->lpni_pref_nnids > 0) {
938 } else if (lpni->lpni_pref_nnids == 0) {
939 lpni->lpni_pref.nid = nid;
940 lpni->lpni_pref_nnids = 1;
941 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
943 spin_unlock(&lpni->lpni_lock);
945 CDEBUG(D_NET, "peer %s nid %s: %d\n",
946 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
951 * Clear the preferred NID from a non-multi-rail peer_ni, provided
952 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
955 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
959 spin_lock(&lpni->lpni_lock);
960 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
961 lpni->lpni_pref_nnids = 0;
962 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
963 } else if (lpni->lpni_pref_nnids == 0) {
968 spin_unlock(&lpni->lpni_lock);
970 CDEBUG(D_NET, "peer %s: %d\n",
971 libcfs_nid2str(lpni->lpni_nid), rc);
976 * Clear the preferred NIDs from a non-multi-rail peer.
979 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
981 struct lnet_peer_ni *lpni = NULL;
983 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
984 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
988 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
990 lnet_nid_t *nids = NULL;
991 lnet_nid_t *oldnids = NULL;
992 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
997 if (nid == LNET_NID_ANY) {
1002 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1007 /* A non-MR node may have only one preferred NI per peer_ni */
1008 if (lpni->lpni_pref_nnids > 0) {
1009 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1015 if (lpni->lpni_pref_nnids != 0) {
1016 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1017 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1022 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
1023 if (lpni->lpni_pref.nids[i] == nid) {
1024 LIBCFS_FREE(nids, size);
1028 nids[i] = lpni->lpni_pref.nids[i];
1033 lnet_net_lock(LNET_LOCK_EX);
1034 spin_lock(&lpni->lpni_lock);
1035 if (lpni->lpni_pref_nnids == 0) {
1036 lpni->lpni_pref.nid = nid;
1038 oldnids = lpni->lpni_pref.nids;
1039 lpni->lpni_pref.nids = nids;
1041 lpni->lpni_pref_nnids++;
1042 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1043 spin_unlock(&lpni->lpni_lock);
1044 lnet_net_unlock(LNET_LOCK_EX);
1047 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1048 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1051 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1052 spin_lock(&lpni->lpni_lock);
1053 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1054 spin_unlock(&lpni->lpni_lock);
1056 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1057 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1062 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1064 lnet_nid_t *nids = NULL;
1065 lnet_nid_t *oldnids = NULL;
1066 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1071 if (lpni->lpni_pref_nnids == 0) {
1076 if (lpni->lpni_pref_nnids == 1) {
1077 if (lpni->lpni_pref.nid != nid) {
1081 } else if (lpni->lpni_pref_nnids == 2) {
1082 if (lpni->lpni_pref.nids[0] != nid &&
1083 lpni->lpni_pref.nids[1] != nid) {
1088 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1089 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1094 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1095 if (lpni->lpni_pref.nids[i] != nid)
1097 nids[j++] = lpni->lpni_pref.nids[i];
1099 /* Check if we actually removed a nid. */
1100 if (j == lpni->lpni_pref_nnids) {
1101 LIBCFS_FREE(nids, size);
1107 lnet_net_lock(LNET_LOCK_EX);
1108 spin_lock(&lpni->lpni_lock);
1109 if (lpni->lpni_pref_nnids == 1) {
1110 lpni->lpni_pref.nid = LNET_NID_ANY;
1111 } else if (lpni->lpni_pref_nnids == 2) {
1112 oldnids = lpni->lpni_pref.nids;
1113 if (oldnids[0] == nid)
1114 lpni->lpni_pref.nid = oldnids[1];
1116 lpni->lpni_pref.nid = oldnids[2];
1118 oldnids = lpni->lpni_pref.nids;
1119 lpni->lpni_pref.nids = nids;
1121 lpni->lpni_pref_nnids--;
1122 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1123 spin_unlock(&lpni->lpni_lock);
1124 lnet_net_unlock(LNET_LOCK_EX);
1127 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1128 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1131 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1132 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1137 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1139 struct lnet_peer_ni *lpni;
1140 lnet_nid_t primary_nid = nid;
1142 lpni = lnet_find_peer_ni_locked(nid);
1144 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1145 lnet_peer_ni_decref_locked(lpni);
1152 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1154 if (lnet_peer_discovery_disabled)
1157 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1158 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1169 lnet_is_discovery_disabled(struct lnet_peer *lp)
1173 spin_lock(&lp->lp_lock);
1174 rc = lnet_is_discovery_disabled_locked(lp);
1175 spin_unlock(&lp->lp_lock);
1181 LNetPrimaryNID(lnet_nid_t nid)
1183 struct lnet_peer *lp;
1184 struct lnet_peer_ni *lpni;
1185 lnet_nid_t primary_nid = nid;
1189 cpt = lnet_net_lock_current();
1190 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1195 lp = lpni->lpni_peer_net->lpn_peer;
1197 while (!lnet_peer_is_uptodate(lp)) {
1198 rc = lnet_discover_peer_locked(lpni, cpt, true);
1201 lp = lpni->lpni_peer_net->lpn_peer;
1203 /* Only try once if discovery is disabled */
1204 if (lnet_is_discovery_disabled(lp))
1207 primary_nid = lp->lp_primary_nid;
1209 lnet_peer_ni_decref_locked(lpni);
1211 lnet_net_unlock(cpt);
1213 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1214 libcfs_nid2str(primary_nid), rc);
1217 EXPORT_SYMBOL(LNetPrimaryNID);
1219 struct lnet_peer_net *
1220 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1222 struct lnet_peer_net *peer_net;
1223 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1224 if (peer_net->lpn_net_id == net_id)
1231 * Attach a peer_ni to a peer_net and peer. This function assumes
1232 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1233 * may be attached to a different peer, in which case it will be
1234 * properly detached first. The whole operation is done atomically.
1236 * Always returns 0. This is the last function called from functions
1237 * that do return an int, so returning 0 here allows the compiler to
1241 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1242 struct lnet_peer_net *lpn,
1243 struct lnet_peer_ni *lpni,
1246 struct lnet_peer_table *ptable;
1248 /* Install the new peer_ni */
1249 lnet_net_lock(LNET_LOCK_EX);
1250 /* Add peer_ni to global peer table hash, if necessary. */
1251 if (list_empty(&lpni->lpni_hashlist)) {
1252 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1254 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1255 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1256 ptable->pt_version++;
1257 ptable->pt_number++;
1258 /* This is the 1st refcount on lpni. */
1259 atomic_inc(&lpni->lpni_refcount);
1262 /* Detach the peer_ni from an existing peer, if necessary. */
1263 if (lpni->lpni_peer_net) {
1264 LASSERT(lpni->lpni_peer_net != lpn);
1265 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1266 lnet_peer_detach_peer_ni_locked(lpni);
1267 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1268 lpni->lpni_peer_net = NULL;
1271 /* Add peer_ni to peer_net */
1272 lpni->lpni_peer_net = lpn;
1273 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1274 lnet_peer_net_addref_locked(lpn);
1276 /* Add peer_net to peer */
1277 if (!lpn->lpn_peer) {
1279 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1280 lnet_peer_addref_locked(lp);
1283 /* Add peer to global peer list, if necessary */
1284 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1285 if (list_empty(&lp->lp_peer_list)) {
1286 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1291 /* Update peer state */
1292 spin_lock(&lp->lp_lock);
1293 if (flags & LNET_PEER_CONFIGURED) {
1294 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1295 lp->lp_state |= LNET_PEER_CONFIGURED;
1297 if (flags & LNET_PEER_MULTI_RAIL) {
1298 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1299 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1300 lnet_peer_clr_non_mr_pref_nids(lp);
1303 spin_unlock(&lp->lp_lock);
1306 lnet_net_unlock(LNET_LOCK_EX);
1308 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1309 libcfs_nid2str(lp->lp_primary_nid),
1310 libcfs_nid2str(lpni->lpni_nid), flags);
1316 * Create a new peer, with nid as its primary nid.
1318 * Call with the lnet_api_mutex held.
1321 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1323 struct lnet_peer *lp;
1324 struct lnet_peer_net *lpn;
1325 struct lnet_peer_ni *lpni;
1328 LASSERT(nid != LNET_NID_ANY);
1331 * No need for the lnet_net_lock here, because the
1332 * lnet_api_mutex is held.
1334 lpni = lnet_find_peer_ni_locked(nid);
1336 /* A peer with this NID already exists. */
1337 lp = lpni->lpni_peer_net->lpn_peer;
1338 lnet_peer_ni_decref_locked(lpni);
1340 * This is an error if the peer was configured and the
1341 * primary NID differs or an attempt is made to change
1342 * the Multi-Rail flag. Otherwise the assumption is
1343 * that an existing peer is being modified.
1345 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1346 if (lp->lp_primary_nid != nid)
1348 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1352 /* Delete and recreate as a configured peer. */
1356 /* Create peer, peer_net, and peer_ni. */
1358 lp = lnet_peer_alloc(nid);
1361 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1364 lpni = lnet_peer_ni_alloc(nid);
1368 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1371 LIBCFS_FREE(lpn, sizeof(*lpn));
1373 LIBCFS_FREE(lp, sizeof(*lp));
1375 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1376 libcfs_nid2str(nid), flags, rc);
1381 * Add a NID to a peer. Call with ln_api_mutex held.
1384 * -EPERM: Non-DLC addition to a DLC-configured peer.
1385 * -EEXIST: The NID was configured by DLC for a different peer.
1386 * -ENOMEM: Out of memory.
1387 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1388 * non-multi-rail peer.
1391 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1393 struct lnet_peer_net *lpn;
1394 struct lnet_peer_ni *lpni;
1398 LASSERT(nid != LNET_NID_ANY);
1400 /* A configured peer can only be updated through configuration. */
1401 if (!(flags & LNET_PEER_CONFIGURED)) {
1402 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1409 * The MULTI_RAIL flag can be set but not cleared, because
1410 * that would leave the peer struct in an invalid state.
1412 if (flags & LNET_PEER_MULTI_RAIL) {
1413 spin_lock(&lp->lp_lock);
1414 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1415 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1416 lnet_peer_clr_non_mr_pref_nids(lp);
1418 spin_unlock(&lp->lp_lock);
1419 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1424 lpni = lnet_find_peer_ni_locked(nid);
1427 * A peer_ni already exists. This is only a problem if
1428 * it is not connected to this peer and was configured
1431 lnet_peer_ni_decref_locked(lpni);
1432 if (lpni->lpni_peer_net->lpn_peer == lp)
1434 if (lnet_peer_ni_is_configured(lpni)) {
1438 /* If this is the primary NID, destroy the peer. */
1439 if (lnet_peer_ni_is_primary(lpni)) {
1440 struct lnet_peer *rtr_lp =
1441 lpni->lpni_peer_net->lpn_peer;
1442 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1444 * if we're trying to delete a router it means
1445 * we're moving this peer NI to a new peer so must
1446 * transfer router properties to the new peer
1448 if (rtr_refcount > 0) {
1449 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1450 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1452 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1453 lpni = lnet_peer_ni_alloc(nid);
1460 lpni = lnet_peer_ni_alloc(nid);
1468 * Get the peer_net. Check that we're not adding a second
1469 * peer_ni on a peer_net of a non-multi-rail peer.
1471 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1473 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1478 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1483 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1486 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1487 if (!lpni->lpni_peer_net)
1488 LIBCFS_FREE(lpni, sizeof(*lpni));
1490 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1491 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1497 * Update the primary NID of a peer, if possible.
1499 * Call with the lnet_api_mutex held.
1502 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1504 lnet_nid_t old = lp->lp_primary_nid;
1507 if (lp->lp_primary_nid == nid)
1509 rc = lnet_peer_add_nid(lp, nid, flags);
1512 lp->lp_primary_nid = nid;
1514 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1515 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1520 * lpni creation initiated due to traffic either sending or receiving.
1523 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1525 struct lnet_peer *lp;
1526 struct lnet_peer_net *lpn;
1527 struct lnet_peer_ni *lpni;
1529 * Assume peer is Multi-Rail capable and let discovery find out
1532 unsigned flags = LNET_PEER_MULTI_RAIL;
1535 if (nid == LNET_NID_ANY) {
1540 /* lnet_net_lock is not needed here because ln_api_lock is held */
1541 lpni = lnet_find_peer_ni_locked(nid);
1544 * We must have raced with another thread. Since we
1545 * know next to nothing about a peer_ni created by
1546 * traffic, we just assume everything is ok and
1549 lnet_peer_ni_decref_locked(lpni);
1553 /* Create peer, peer_net, and peer_ni. */
1555 lp = lnet_peer_alloc(nid);
1558 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1561 lpni = lnet_peer_ni_alloc(nid);
1564 if (pref != LNET_NID_ANY)
1565 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1567 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1570 LIBCFS_FREE(lpn, sizeof(*lpn));
1572 LIBCFS_FREE(lp, sizeof(*lp));
1574 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1579 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1581 * This API handles the following combinations:
1582 * Create a peer with its primary NI if only the prim_nid is provided
1583 * Add a NID to a peer identified by the prim_nid. The peer identified
1584 * by the prim_nid must already exist.
1585 * The peer being created may be non-MR.
1587 * The caller must hold ln_api_mutex. This prevents the peer from
1588 * being created/modified/deleted by a different thread.
1591 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1593 struct lnet_peer *lp = NULL;
1594 struct lnet_peer_ni *lpni;
1597 /* The prim_nid must always be specified */
1598 if (prim_nid == LNET_NID_ANY)
1601 flags = LNET_PEER_CONFIGURED;
1603 flags |= LNET_PEER_MULTI_RAIL;
1606 * If nid isn't specified, we must create a new peer with
1607 * prim_nid as its primary nid.
1609 if (nid == LNET_NID_ANY)
1610 return lnet_peer_add(prim_nid, flags);
1612 /* Look up the prim_nid, which must exist. */
1613 lpni = lnet_find_peer_ni_locked(prim_nid);
1616 lnet_peer_ni_decref_locked(lpni);
1617 lp = lpni->lpni_peer_net->lpn_peer;
1619 /* Peer must have been configured. */
1620 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1621 CDEBUG(D_NET, "peer %s was not configured\n",
1622 libcfs_nid2str(prim_nid));
1626 /* Primary NID must match */
1627 if (lp->lp_primary_nid != prim_nid) {
1628 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1629 libcfs_nid2str(prim_nid),
1630 libcfs_nid2str(lp->lp_primary_nid));
1634 /* Multi-Rail flag must match. */
1635 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1636 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1637 libcfs_nid2str(prim_nid));
1641 return lnet_peer_add_nid(lp, nid, flags);
1645 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1647 * This API handles the following combinations:
1648 * Delete a NI from a peer if both prim_nid and nid are provided.
1649 * Delete a peer if only prim_nid is provided.
1650 * Delete a peer if its primary nid is provided.
1652 * The caller must hold ln_api_mutex. This prevents the peer from
1653 * being modified/deleted by a different thread.
1656 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1658 struct lnet_peer *lp;
1659 struct lnet_peer_ni *lpni;
1662 if (prim_nid == LNET_NID_ANY)
1665 lpni = lnet_find_peer_ni_locked(prim_nid);
1668 lnet_peer_ni_decref_locked(lpni);
1669 lp = lpni->lpni_peer_net->lpn_peer;
1671 if (prim_nid != lp->lp_primary_nid) {
1672 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1673 libcfs_nid2str(prim_nid),
1674 libcfs_nid2str(lp->lp_primary_nid));
1678 lnet_net_lock(LNET_LOCK_EX);
1679 if (lp->lp_rtr_refcount > 0) {
1680 lnet_net_unlock(LNET_LOCK_EX);
1681 CERROR("%s is a router. Can not be deleted\n",
1682 libcfs_nid2str(prim_nid));
1685 lnet_net_unlock(LNET_LOCK_EX);
1687 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1688 return lnet_peer_del(lp);
1690 flags = LNET_PEER_CONFIGURED;
1691 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1692 flags |= LNET_PEER_MULTI_RAIL;
1694 return lnet_peer_del_nid(lp, nid, flags);
1698 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1700 struct lnet_peer_table *ptable;
1701 struct lnet_peer_net *lpn;
1703 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1705 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1706 LASSERT(list_empty(&lpni->lpni_txq));
1707 LASSERT(lpni->lpni_txqnob == 0);
1708 LASSERT(list_empty(&lpni->lpni_peer_nis));
1709 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1711 lpn = lpni->lpni_peer_net;
1712 lpni->lpni_peer_net = NULL;
1713 lpni->lpni_net = NULL;
1715 /* remove the peer ni from the zombie list */
1716 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1717 spin_lock(&ptable->pt_zombie_lock);
1718 list_del_init(&lpni->lpni_hashlist);
1719 ptable->pt_zombies--;
1720 spin_unlock(&ptable->pt_zombie_lock);
1722 if (lpni->lpni_pref_nnids > 1) {
1723 LIBCFS_FREE(lpni->lpni_pref.nids,
1724 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1726 LIBCFS_FREE(lpni, sizeof(*lpni));
1728 lnet_peer_net_decref_locked(lpn);
1731 struct lnet_peer_ni *
1732 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1734 struct lnet_peer_ni *lpni = NULL;
1737 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1738 return ERR_PTR(-ESHUTDOWN);
1741 * find if a peer_ni already exists.
1742 * If so then just return that.
1744 lpni = lnet_find_peer_ni_locked(nid);
1748 lnet_net_unlock(cpt);
1750 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1753 goto out_net_relock;
1756 lpni = lnet_find_peer_ni_locked(nid);
1766 * Get a peer_ni for the given nid, create it if necessary. Takes a
1767 * hold on the peer_ni.
1769 struct lnet_peer_ni *
1770 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1772 struct lnet_peer_ni *lpni = NULL;
1775 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1776 return ERR_PTR(-ESHUTDOWN);
1779 * find if a peer_ni already exists.
1780 * If so then just return that.
1782 lpni = lnet_find_peer_ni_locked(nid);
1788 * use the lnet_api_mutex to serialize the creation of the peer_ni
1789 * and the creation/deletion of the local ni/net. When a local ni is
1790 * created, if there exists a set of peer_nis on that network,
1791 * they need to be traversed and updated. When a local NI is
1792 * deleted, which could result in a network being deleted, then
1793 * all peer nis on that network need to be removed as well.
1795 * Creation through traffic should also be serialized with
1796 * creation through DLC.
1798 lnet_net_unlock(cpt);
1799 mutex_lock(&the_lnet.ln_api_mutex);
1801 * Shutdown is only set under the ln_api_lock, so a single
1802 * check here is sufficent.
1804 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1805 lpni = ERR_PTR(-ESHUTDOWN);
1806 goto out_mutex_unlock;
1809 rc = lnet_peer_ni_traffic_add(nid, pref);
1812 goto out_mutex_unlock;
1815 lpni = lnet_find_peer_ni_locked(nid);
1819 mutex_unlock(&the_lnet.ln_api_mutex);
1822 /* Lock has been dropped, check again for shutdown. */
1823 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1825 lnet_peer_ni_decref_locked(lpni);
1826 lpni = ERR_PTR(-ESHUTDOWN);
1833 lnet_peer_gw_discovery(struct lnet_peer *lp)
1837 spin_lock(&lp->lp_lock);
1838 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1840 spin_unlock(&lp->lp_lock);
1846 * Is a peer uptodate from the point of view of discovery?
1848 * If it is currently being processed, obviously not.
1849 * A forced Ping or Push is also handled by the discovery thread.
1851 * Otherwise look at whether the peer needs rediscovering.
1854 lnet_peer_is_uptodate(struct lnet_peer *lp)
1858 spin_lock(&lp->lp_lock);
1859 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1860 LNET_PEER_FORCE_PING |
1861 LNET_PEER_FORCE_PUSH)) {
1863 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1865 } else if (lnet_peer_needs_push(lp)) {
1867 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1868 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1875 spin_unlock(&lp->lp_lock);
1881 * Queue a peer for the attention of the discovery thread. Call with
1882 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1883 * -EALREADY if the peer was already queued.
1885 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1889 spin_lock(&lp->lp_lock);
1890 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1891 lp->lp_state |= LNET_PEER_DISCOVERING;
1892 spin_unlock(&lp->lp_lock);
1893 if (list_empty(&lp->lp_dc_list)) {
1894 lnet_peer_addref_locked(lp);
1895 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1896 wake_up(&the_lnet.ln_dc_waitq);
1902 CDEBUG(D_NET, "Queue peer %s: %d\n",
1903 libcfs_nid2str(lp->lp_primary_nid), rc);
1909 * Discovery of a peer is complete. Wake all waiters on the peer.
1910 * Call with lnet_net_lock/EX held.
1912 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1914 struct lnet_msg *msg, *tmp;
1916 struct list_head pending_msgs;
1918 INIT_LIST_HEAD(&pending_msgs);
1920 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1921 libcfs_nid2str(lp->lp_primary_nid));
1923 list_del_init(&lp->lp_dc_list);
1924 spin_lock(&lp->lp_lock);
1925 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1926 spin_unlock(&lp->lp_lock);
1927 wake_up_all(&lp->lp_dc_waitq);
1929 if (lp->lp_rtr_refcount > 0)
1930 lnet_router_discovery_complete(lp);
1932 lnet_net_unlock(LNET_LOCK_EX);
1934 /* iterate through all pending messages and send them again */
1935 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1936 list_del_init(&msg->msg_list);
1937 if (lp->lp_dc_error) {
1938 lnet_finalize(msg, lp->lp_dc_error);
1942 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1943 lnet_msgtyp2str(msg->msg_type),
1944 libcfs_id2str(msg->msg_target));
1945 rc = lnet_send(msg->msg_src_nid_param, msg,
1946 msg->msg_rtr_nid_param);
1948 CNETERR("Error sending %s to %s: %d\n",
1949 lnet_msgtyp2str(msg->msg_type),
1950 libcfs_id2str(msg->msg_target), rc);
1951 lnet_finalize(msg, rc);
1954 lnet_net_lock(LNET_LOCK_EX);
1955 lnet_peer_decref_locked(lp);
1959 * Handle inbound push.
1960 * Like any event handler, called with lnet_res_lock/CPT held.
1962 void lnet_peer_push_event(struct lnet_event *ev)
1964 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1965 struct lnet_peer *lp;
1967 /* lnet_find_peer() adds a refcount */
1968 lp = lnet_find_peer(ev->source.nid);
1970 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1971 libcfs_nid2str(ev->initiator.nid),
1972 libcfs_nid2str(ev->source.nid));
1976 /* Ensure peer state remains consistent while we modify it. */
1977 spin_lock(&lp->lp_lock);
1980 * If some kind of error happened the contents of the message
1981 * cannot be used. Clear the NIDS_UPTODATE and set the
1982 * FORCE_PING flag to trigger a ping.
1985 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1986 lp->lp_state |= LNET_PEER_FORCE_PING;
1987 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1989 libcfs_nid2str(lp->lp_primary_nid),
1990 libcfs_nid2str(ev->source.nid));
1995 * A push with invalid or corrupted info. Clear the UPTODATE
1996 * flag to trigger a ping.
1998 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1999 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2000 lp->lp_state |= LNET_PEER_FORCE_PING;
2001 CDEBUG(D_NET, "Corrupted Push from %s\n",
2002 libcfs_nid2str(lp->lp_primary_nid));
2007 * Make sure we'll allocate the correct size ping buffer when
2010 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2011 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2014 * A non-Multi-Rail peer is not supposed to be capable of
2017 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2018 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2019 libcfs_nid2str(lp->lp_primary_nid));
2024 * Check the MULTIRAIL flag. Complain if the peer was DLC
2025 * configured without it.
2027 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2028 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2029 CERROR("Push says %s is Multi-Rail, DLC says not\n",
2030 libcfs_nid2str(lp->lp_primary_nid));
2032 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2033 lnet_peer_clr_non_mr_pref_nids(lp);
2038 * The peer may have discovery disabled at its end. Set
2039 * NO_DISCOVERY as appropriate.
2041 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2042 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2043 libcfs_nid2str(lp->lp_primary_nid));
2044 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2045 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2046 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2047 libcfs_nid2str(lp->lp_primary_nid));
2048 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2052 * Check for truncation of the Put message. Clear the
2053 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2054 * and tell discovery to allocate a bigger buffer.
2056 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2057 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2058 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2059 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2060 lp->lp_state |= LNET_PEER_FORCE_PING;
2061 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2062 libcfs_nid2str(lp->lp_primary_nid),
2063 pbuf->pb_info.pi_nnis);
2067 /* always assume new data */
2068 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2069 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2072 * If there is data present that hasn't been processed yet,
2073 * we'll replace it if the Put contained newer data and it
2074 * fits. We're racing with a Ping or earlier Push in this
2077 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2078 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2079 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2080 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2081 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2082 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2083 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2084 libcfs_nid2str(lp->lp_primary_nid),
2085 LNET_PING_BUFFER_SEQNO(pbuf),
2086 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2092 * Allocate a buffer to copy the data. On a failure we drop
2093 * the Push and set FORCE_PING to force the discovery
2094 * thread to fix the problem by pinging the peer.
2096 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2098 lp->lp_state |= LNET_PEER_FORCE_PING;
2099 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2100 libcfs_nid2str(lp->lp_primary_nid),
2101 LNET_PING_BUFFER_SEQNO(pbuf));
2106 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2107 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2108 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2109 CDEBUG(D_NET, "Received Push %s %u\n",
2110 libcfs_nid2str(lp->lp_primary_nid),
2111 LNET_PING_BUFFER_SEQNO(pbuf));
2115 * Queue the peer for discovery if not done, force it on the request
2116 * queue and wake the discovery thread if the peer was already queued,
2117 * because its status changed.
2119 spin_unlock(&lp->lp_lock);
2120 lnet_net_lock(LNET_LOCK_EX);
2121 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2122 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2123 wake_up(&the_lnet.ln_dc_waitq);
2125 /* Drop refcount from lookup */
2126 lnet_peer_decref_locked(lp);
2127 lnet_net_unlock(LNET_LOCK_EX);
2131 * Clear the discovery error state, unless we're already discovering
2132 * this peer, in which case the error is current.
2134 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2136 spin_lock(&lp->lp_lock);
2137 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2138 lp->lp_dc_error = 0;
2139 spin_unlock(&lp->lp_lock);
2143 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2144 * dropped/retaken within this function. An lnet_peer_ni is passed in
2145 * because discovery could tear down an lnet_peer.
2148 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2151 struct lnet_peer *lp;
2156 lnet_net_unlock(cpt);
2157 lnet_net_lock(LNET_LOCK_EX);
2158 lp = lpni->lpni_peer_net->lpn_peer;
2159 lnet_peer_clear_discovery_error(lp);
2162 * We're willing to be interrupted. The lpni can become a
2163 * zombie if we race with DLC, so we must check for that.
2166 /* Keep lp alive when the lnet_net_lock is unlocked */
2167 lnet_peer_addref_locked(lp);
2168 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2169 if (signal_pending(current))
2171 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2174 * Don't repeat discovery if discovery is disabled. This is
2175 * done to ensure we can use discovery as a standard ping as
2176 * well for backwards compatibility with routers which do not
2177 * have discovery or have discovery disabled
2179 if (lnet_is_discovery_disabled(lp) && count > 0)
2181 if (lp->lp_dc_error)
2183 if (lnet_peer_is_uptodate(lp))
2185 lnet_peer_queue_for_discovery(lp);
2187 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2190 * If caller requested a non-blocking operation then
2191 * return immediately. Once discovery is complete any
2192 * pending messages that were stopped due to discovery
2193 * will be transmitted.
2198 lnet_net_unlock(LNET_LOCK_EX);
2200 finish_wait(&lp->lp_dc_waitq, &wait);
2201 lnet_net_lock(LNET_LOCK_EX);
2202 lnet_peer_decref_locked(lp);
2203 /* Peer may have changed */
2204 lp = lpni->lpni_peer_net->lpn_peer;
2206 finish_wait(&lp->lp_dc_waitq, &wait);
2208 lnet_net_unlock(LNET_LOCK_EX);
2210 lnet_peer_decref_locked(lp);
2212 * The peer may have changed, so re-check and rediscover if that turns
2213 * out to have been the case. The reference count on lp ensured that
2214 * even if it was unlinked from lpni the memory could not be recycled.
2215 * Thus the check below is sufficient to determine whether the peer
2216 * changed. If the peer changed, then lp must not be dereferenced.
2218 if (lp != lpni->lpni_peer_net->lpn_peer)
2221 if (signal_pending(current))
2223 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2225 else if (lp->lp_dc_error)
2226 rc = lp->lp_dc_error;
2228 CDEBUG(D_NET, "non-blocking discovery\n");
2229 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2232 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2233 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2234 libcfs_nid2str(lpni->lpni_nid), rc,
2235 (!block) ? "pending discovery" : "discovery complete");
2240 /* Handle an incoming ack for a push. */
2242 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2244 struct lnet_ping_buffer *pbuf;
2246 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2247 spin_lock(&lp->lp_lock);
2248 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2249 lp->lp_push_error = ev->status;
2251 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2253 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2254 spin_unlock(&lp->lp_lock);
2256 CDEBUG(D_NET, "peer %s ev->status %d\n",
2257 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2260 /* Handle a Reply message. This is the reply to a Ping message. */
2262 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2264 struct lnet_ping_buffer *pbuf;
2267 spin_lock(&lp->lp_lock);
2270 * If some kind of error happened the contents of message
2271 * cannot be used. Set PING_FAILED to trigger a retry.
2274 lp->lp_state |= LNET_PEER_PING_FAILED;
2275 lp->lp_ping_error = ev->status;
2276 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2278 libcfs_nid2str(lp->lp_primary_nid),
2279 libcfs_nid2str(ev->source.nid));
2283 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2284 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2285 lnet_swap_pinginfo(pbuf);
2288 * A reply with invalid or corrupted info. Set PING_FAILED to
2291 rc = lnet_ping_info_validate(&pbuf->pb_info);
2293 lp->lp_state |= LNET_PEER_PING_FAILED;
2294 lp->lp_ping_error = 0;
2295 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2296 libcfs_nid2str(lp->lp_primary_nid), rc);
2302 * Only enable the multi-rail feature on the peer if both sides of
2303 * the connection have discovery on
2305 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2306 CDEBUG(D_NET, "Peer %s has Multi-Rail feature enabled\n",
2307 libcfs_nid2str(lp->lp_primary_nid));
2308 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2310 CDEBUG(D_NET, "Peer %s has Multi-Rail feature disabled\n",
2311 libcfs_nid2str(lp->lp_primary_nid));
2312 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2316 * The peer may have discovery disabled at its end. Set
2317 * NO_DISCOVERY as appropriate.
2319 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2320 !lnet_peer_discovery_disabled) {
2321 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2322 libcfs_nid2str(lp->lp_primary_nid));
2323 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2325 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2326 libcfs_nid2str(lp->lp_primary_nid));
2327 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2331 * Update the MULTI_RAIL flag based on the reply. If the peer
2332 * was configured with DLC then the setting should match what
2335 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2336 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2337 /* Everything's fine */
2338 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2339 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2340 libcfs_nid2str(lp->lp_primary_nid));
2343 * if discovery is disabled then we don't want to
2344 * update the state of the peer. All we'll do is
2345 * update the peer_nis which were reported back in
2349 if (!lnet_is_discovery_disabled_locked(lp)) {
2350 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2351 lnet_peer_clr_non_mr_pref_nids(lp);
2354 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2355 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2356 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2357 libcfs_nid2str(lp->lp_primary_nid));
2359 CERROR("Multi-Rail state vanished from %s\n",
2360 libcfs_nid2str(lp->lp_primary_nid));
2361 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2366 * Make sure we'll allocate the correct size ping buffer when
2369 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2370 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2373 * Check for truncation of the Reply. Clear PING_SENT and set
2374 * PING_FAILED to trigger a retry.
2376 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2377 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2378 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2379 lp->lp_state |= LNET_PEER_PING_FAILED;
2380 lp->lp_ping_error = 0;
2381 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2382 libcfs_nid2str(lp->lp_primary_nid),
2383 pbuf->pb_info.pi_nnis);
2388 * Check the sequence numbers in the reply. These are only
2389 * available if the reply came from a Multi-Rail peer.
2391 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2392 pbuf->pb_info.pi_nnis > 1 &&
2393 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2394 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2395 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2396 libcfs_nid2str(lp->lp_primary_nid),
2397 LNET_PING_BUFFER_SEQNO(pbuf),
2400 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2403 /* We're happy with the state of the data in the buffer. */
2404 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2405 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2406 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2407 lnet_ping_buffer_decref(lp->lp_data);
2409 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2410 lnet_ping_buffer_addref(pbuf);
2413 lp->lp_state &= ~LNET_PEER_PING_SENT;
2414 spin_unlock(&lp->lp_lock);
2418 * Send event handling. Only matters for error cases, where we clean
2419 * up state on the peer and peer_ni that would otherwise be updated in
2420 * the REPLY event handler for a successful Ping, and the ACK event
2421 * handler for a successful Push.
2424 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2431 spin_lock(&lp->lp_lock);
2432 if (ev->msg_type == LNET_MSG_GET) {
2433 lp->lp_state &= ~LNET_PEER_PING_SENT;
2434 lp->lp_state |= LNET_PEER_PING_FAILED;
2435 lp->lp_ping_error = ev->status;
2436 } else { /* ev->msg_type == LNET_MSG_PUT */
2437 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2438 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2439 lp->lp_push_error = ev->status;
2441 spin_unlock(&lp->lp_lock);
2442 rc = LNET_REDISCOVER_PEER;
2444 CDEBUG(D_NET, "%s Send to %s: %d\n",
2445 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2446 libcfs_nid2str(ev->target.nid), rc);
2451 * Unlink event handling. This event is only seen if a call to
2452 * LNetMDUnlink() caused the event to be unlinked. If this call was
2453 * made after the event was set up in LNetGet() or LNetPut() then we
2454 * assume the Ping or Push timed out.
2457 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2459 spin_lock(&lp->lp_lock);
2460 /* We've passed through LNetGet() */
2461 if (lp->lp_state & LNET_PEER_PING_SENT) {
2462 lp->lp_state &= ~LNET_PEER_PING_SENT;
2463 lp->lp_state |= LNET_PEER_PING_FAILED;
2464 lp->lp_ping_error = -ETIMEDOUT;
2465 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2466 libcfs_nid2str(lp->lp_primary_nid));
2468 /* We've passed through LNetPut() */
2469 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2470 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2471 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2472 lp->lp_push_error = -ETIMEDOUT;
2473 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2474 libcfs_nid2str(lp->lp_primary_nid));
2476 spin_unlock(&lp->lp_lock);
2480 * Event handler for the discovery EQ.
2482 * Called with lnet_res_lock(cpt) held. The cpt is the
2483 * lnet_cpt_of_cookie() of the md handle cookie.
2485 static void lnet_discovery_event_handler(struct lnet_event *event)
2487 struct lnet_peer *lp = event->md.user_ptr;
2488 struct lnet_ping_buffer *pbuf;
2491 /* discovery needs to take another look */
2492 rc = LNET_REDISCOVER_PEER;
2494 CDEBUG(D_NET, "Received event: %d\n", event->type);
2496 switch (event->type) {
2497 case LNET_EVENT_ACK:
2498 lnet_discovery_event_ack(lp, event);
2500 case LNET_EVENT_REPLY:
2501 lnet_discovery_event_reply(lp, event);
2503 case LNET_EVENT_SEND:
2504 /* Only send failure triggers a retry. */
2505 rc = lnet_discovery_event_send(lp, event);
2507 case LNET_EVENT_UNLINK:
2508 /* LNetMDUnlink() was called */
2509 lnet_discovery_event_unlink(lp, event);
2512 /* Invalid events. */
2515 lnet_net_lock(LNET_LOCK_EX);
2516 if (event->unlinked) {
2517 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2518 lnet_ping_buffer_decref(pbuf);
2519 lnet_peer_decref_locked(lp);
2522 /* put peer back at end of request queue, if discovery not already
2524 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2525 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2526 wake_up(&the_lnet.ln_dc_waitq);
2528 lnet_net_unlock(LNET_LOCK_EX);
2532 * Build a peer from incoming data.
2534 * The NIDs in the incoming data are supposed to be structured as follows:
2537 * - other NIDs in same net
2538 * - NIDs in second net
2539 * - NIDs in third net
2541 * This due to the way the list of NIDs in the data is created.
2543 * Note that this function will mark the peer uptodate unless an
2544 * ENOMEM is encontered. All other errors are due to a conflict
2545 * between the DLC configuration and what discovery sees. We treat DLC
2546 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2547 * peer from becoming stuck in discovery.
2549 static int lnet_peer_merge_data(struct lnet_peer *lp,
2550 struct lnet_ping_buffer *pbuf)
2552 struct lnet_peer_ni *lpni;
2553 lnet_nid_t *curnis = NULL;
2554 struct lnet_ni_status *addnis = NULL;
2555 lnet_nid_t *delnis = NULL;
2565 flags = LNET_PEER_DISCOVERED;
2566 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2567 flags |= LNET_PEER_MULTI_RAIL;
2570 * Cache the routing feature for the peer; whether it is enabled
2571 * for disabled as reported by the remote peer.
2573 spin_lock(&lp->lp_lock);
2574 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2575 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2577 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2578 spin_unlock(&lp->lp_lock);
2580 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2581 LIBCFS_ALLOC(curnis, nnis * sizeof(*curnis));
2582 LIBCFS_ALLOC(addnis, nnis * sizeof(*addnis));
2583 LIBCFS_ALLOC(delnis, nnis * sizeof(*delnis));
2584 if (!curnis || !addnis || !delnis) {
2592 /* Construct the list of NIDs present in peer. */
2594 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2595 curnis[ncurnis++] = lpni->lpni_nid;
2598 * Check for NIDs in pbuf not present in curnis[].
2599 * The loop starts at 1 to skip the loopback NID.
2601 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2602 for (j = 0; j < ncurnis; j++)
2603 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2606 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2609 * Check for NIDs in curnis[] not present in pbuf.
2610 * The nested loop starts at 1 to skip the loopback NID.
2612 * But never add the loopback NID to delnis[]: if it is
2613 * present in curnis[] then this peer is for this node.
2615 for (i = 0; i < ncurnis; i++) {
2616 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2618 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2619 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2621 * update the information we cache for the
2622 * peer with the latest information we
2625 lpni = lnet_find_peer_ni_locked(curnis[i]);
2627 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2628 lnet_peer_ni_decref_locked(lpni);
2633 if (j == pbuf->pb_info.pi_nnis)
2634 delnis[ndelnis++] = curnis[i];
2638 * If we get here and the discovery is disabled then we don't want
2639 * to add or delete any NIs. We just updated the ones we have some
2640 * information on, and call it a day
2643 if (lnet_is_discovery_disabled(lp))
2646 for (i = 0; i < naddnis; i++) {
2647 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2649 CERROR("Error adding NID %s to peer %s: %d\n",
2650 libcfs_nid2str(addnis[i].ns_nid),
2651 libcfs_nid2str(lp->lp_primary_nid), rc);
2655 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2657 lpni->lpni_ns_status = addnis[i].ns_status;
2658 lnet_peer_ni_decref_locked(lpni);
2662 for (i = 0; i < ndelnis; i++) {
2664 * for routers it's okay to delete the primary_nid because
2665 * the upper layers don't really rely on it. So if we're
2666 * being told that the router changed its primary_nid
2667 * then it's okay to delete it.
2669 if (lp->lp_rtr_refcount > 0)
2670 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2671 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2673 CERROR("Error deleting NID %s from peer %s: %d\n",
2674 libcfs_nid2str(delnis[i]),
2675 libcfs_nid2str(lp->lp_primary_nid), rc);
2681 * Errors other than -ENOMEM are due to peers having been
2682 * configured with DLC. Ignore these because DLC overrides
2687 LIBCFS_FREE(curnis, nnis * sizeof(*curnis));
2688 LIBCFS_FREE(addnis, nnis * sizeof(*addnis));
2689 LIBCFS_FREE(delnis, nnis * sizeof(*delnis));
2690 lnet_ping_buffer_decref(pbuf);
2691 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2694 spin_lock(&lp->lp_lock);
2695 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2696 lp->lp_state |= LNET_PEER_FORCE_PING;
2697 spin_unlock(&lp->lp_lock);
2703 * The data in pbuf says lp is its primary peer, but the data was
2704 * received by a different peer. Try to update lp with the data.
2707 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2709 struct lnet_handle_md mdh;
2711 /* Queue lp for discovery, and force it on the request queue. */
2712 lnet_net_lock(LNET_LOCK_EX);
2713 if (lnet_peer_queue_for_discovery(lp))
2714 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2715 lnet_net_unlock(LNET_LOCK_EX);
2717 LNetInvalidateMDHandle(&mdh);
2720 * Decide whether we can move the peer to the DATA_PRESENT state.
2722 * We replace stale data for a multi-rail peer, repair PING_FAILED
2723 * status, and preempt FORCE_PING.
2725 * If after that we have DATA_PRESENT, we merge it into this peer.
2727 spin_lock(&lp->lp_lock);
2728 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2729 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2730 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2731 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2732 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2733 lnet_ping_buffer_decref(pbuf);
2738 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2739 lnet_ping_buffer_decref(lp->lp_data);
2741 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2743 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2744 mdh = lp->lp_ping_mdh;
2745 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2746 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2747 lp->lp_ping_error = 0;
2749 if (lp->lp_state & LNET_PEER_FORCE_PING)
2750 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2751 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2752 spin_unlock(&lp->lp_lock);
2754 if (!LNetMDHandleIsInvalid(mdh))
2758 return lnet_peer_merge_data(lp, pbuf);
2760 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2764 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2768 for (i = 0; i < pinfo->pi_nnis; i++) {
2769 if (pinfo->pi_ni[i].ns_nid == nid)
2777 * Update a peer using the data received.
2779 static int lnet_peer_data_present(struct lnet_peer *lp)
2780 __must_hold(&lp->lp_lock)
2782 struct lnet_ping_buffer *pbuf;
2783 struct lnet_peer_ni *lpni;
2784 lnet_nid_t nid = LNET_NID_ANY;
2790 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2791 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2792 spin_unlock(&lp->lp_lock);
2795 * Modifications of peer structures are done while holding the
2796 * ln_api_mutex. A global lock is required because we may be
2797 * modifying multiple peer structures, and a mutex greatly
2798 * simplifies memory management.
2800 * The actual changes to the data structures must also protect
2801 * against concurrent lookups, for which the lnet_net_lock in
2802 * LNET_LOCK_EX mode is used.
2804 mutex_lock(&the_lnet.ln_api_mutex);
2805 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2811 * If this peer is not on the peer list then it is being torn
2812 * down, and our reference count may be all that is keeping it
2813 * alive. Don't do any work on it.
2815 if (list_empty(&lp->lp_peer_list))
2818 flags = LNET_PEER_DISCOVERED;
2819 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2820 flags |= LNET_PEER_MULTI_RAIL;
2823 * Check whether the primary NID in the message matches the
2824 * primary NID of the peer. If it does, update the peer, if
2825 * it it does not, check whether there is already a peer with
2826 * that primary NID. If no such peer exists, try to update
2827 * the primary NID of the current peer (allowed if it was
2828 * created due to message traffic) and complete the update.
2829 * If the peer did exist, hand off the data to it.
2831 * The peer for the loopback interface is a special case: this
2832 * is the peer for the local node, and we want to set its
2833 * primary NID to the correct value here. Moreover, this peer
2834 * can show up with only the loopback NID in the ping buffer.
2836 if (pbuf->pb_info.pi_nnis <= 1)
2838 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2839 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2840 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2842 rc = lnet_peer_merge_data(lp, pbuf);
2844 * if the primary nid of the peer is present in the ping info returned
2845 * from the peer, but it's not the local primary peer we have
2846 * cached and discovery is disabled, then we don't want to update
2847 * our local peer info, by adding or removing NIDs, we just want
2848 * to update the status of the nids that we currently have
2849 * recorded in that peer.
2851 } else if (lp->lp_primary_nid == nid ||
2852 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2853 lnet_is_discovery_disabled(lp))) {
2854 rc = lnet_peer_merge_data(lp, pbuf);
2856 lpni = lnet_find_peer_ni_locked(nid);
2858 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2860 CERROR("Primary NID error %s versus %s: %d\n",
2861 libcfs_nid2str(lp->lp_primary_nid),
2862 libcfs_nid2str(nid), rc);
2864 rc = lnet_peer_merge_data(lp, pbuf);
2867 struct lnet_peer *new_lp;
2868 new_lp = lpni->lpni_peer_net->lpn_peer;
2870 * if lp has discovery/MR enabled that means new_lp
2871 * should have discovery/MR enabled as well, since
2872 * it's the same peer, which we're about to merge
2874 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2875 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2876 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2877 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2879 rc = lnet_peer_set_primary_data(new_lp, pbuf);
2880 lnet_consolidate_routes_locked(lp, new_lp);
2881 lnet_peer_ni_decref_locked(lpni);
2885 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2887 mutex_unlock(&the_lnet.ln_api_mutex);
2889 spin_lock(&lp->lp_lock);
2890 /* Tell discovery to re-check the peer immediately. */
2892 rc = LNET_REDISCOVER_PEER;
2897 * A ping failed. Clear the PING_FAILED state and set the
2898 * FORCE_PING state, to ensure a retry even if discovery is
2899 * disabled. This avoids being left with incorrect state.
2901 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2902 __must_hold(&lp->lp_lock)
2904 struct lnet_handle_md mdh;
2907 mdh = lp->lp_ping_mdh;
2908 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2909 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2910 lp->lp_state |= LNET_PEER_FORCE_PING;
2911 rc = lp->lp_ping_error;
2912 lp->lp_ping_error = 0;
2913 spin_unlock(&lp->lp_lock);
2915 if (!LNetMDHandleIsInvalid(mdh))
2918 CDEBUG(D_NET, "peer %s:%d\n",
2919 libcfs_nid2str(lp->lp_primary_nid), rc);
2921 spin_lock(&lp->lp_lock);
2922 return rc ? rc : LNET_REDISCOVER_PEER;
2926 * Select NID to send a Ping or Push to.
2928 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2930 struct lnet_peer_ni *lpni;
2932 /* Look for a direct-connected NID for this peer. */
2934 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2935 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2940 return lpni->lpni_nid;
2942 /* Look for a routed-connected NID for this peer. */
2944 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2945 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2950 return lpni->lpni_nid;
2952 return LNET_NID_ANY;
2955 /* Active side of ping. */
2956 static int lnet_peer_send_ping(struct lnet_peer *lp)
2957 __must_hold(&lp->lp_lock)
2964 lp->lp_state |= LNET_PEER_PING_SENT;
2965 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2966 spin_unlock(&lp->lp_lock);
2968 cpt = lnet_net_lock_current();
2969 /* Refcount for MD. */
2970 lnet_peer_addref_locked(lp);
2971 pnid = lnet_peer_select_nid(lp);
2972 lnet_net_unlock(cpt);
2974 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2976 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2977 the_lnet.ln_dc_eqh, false);
2980 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2981 * refcount on the peer, otherwise LNetMDUnlink will be called
2982 * which will eventually do that.
2986 lnet_peer_decref_locked(lp);
2987 lnet_net_unlock(cpt);
2988 rc = -rc; /* change the rc to negative value */
2990 } else if (rc < 0) {
2994 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2996 spin_lock(&lp->lp_lock);
3000 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3002 * The errors that get us here are considered hard errors and
3003 * cause Discovery to terminate. So we clear PING_SENT, but do
3004 * not set either PING_FAILED or FORCE_PING. In fact we need
3005 * to clear PING_FAILED, because the unlink event handler will
3006 * have set it if we called LNetMDUnlink() above.
3008 spin_lock(&lp->lp_lock);
3009 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3014 * This function exists because you cannot call LNetMDUnlink() from an
3017 static int lnet_peer_push_failed(struct lnet_peer *lp)
3018 __must_hold(&lp->lp_lock)
3020 struct lnet_handle_md mdh;
3023 mdh = lp->lp_push_mdh;
3024 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3025 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3026 rc = lp->lp_push_error;
3027 lp->lp_push_error = 0;
3028 spin_unlock(&lp->lp_lock);
3030 if (!LNetMDHandleIsInvalid(mdh))
3033 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3034 spin_lock(&lp->lp_lock);
3035 return rc ? rc : LNET_REDISCOVER_PEER;
3038 /* Active side of push. */
3039 static int lnet_peer_send_push(struct lnet_peer *lp)
3040 __must_hold(&lp->lp_lock)
3042 struct lnet_ping_buffer *pbuf;
3043 struct lnet_process_id id;
3048 /* Don't push to a non-multi-rail peer. */
3049 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3050 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3054 lp->lp_state |= LNET_PEER_PUSH_SENT;
3055 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3056 spin_unlock(&lp->lp_lock);
3058 cpt = lnet_net_lock_current();
3059 pbuf = the_lnet.ln_ping_target;
3060 lnet_ping_buffer_addref(pbuf);
3061 lnet_net_unlock(cpt);
3063 /* Push source MD */
3064 md.start = &pbuf->pb_info;
3065 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3066 md.threshold = 2; /* Put/Ack */
3069 md.eq_handle = the_lnet.ln_dc_eqh;
3072 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
3074 lnet_ping_buffer_decref(pbuf);
3075 CERROR("Can't bind push source MD: %d\n", rc);
3078 cpt = lnet_net_lock_current();
3079 /* Refcount for MD. */
3080 lnet_peer_addref_locked(lp);
3081 id.pid = LNET_PID_LUSTRE;
3082 id.nid = lnet_peer_select_nid(lp);
3083 lnet_net_unlock(cpt);
3085 if (id.nid == LNET_NID_ANY) {
3090 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
3091 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3092 LNET_PROTO_PING_MATCHBITS, 0, 0);
3097 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3099 spin_lock(&lp->lp_lock);
3103 LNetMDUnlink(lp->lp_push_mdh);
3104 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3106 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3108 * The errors that get us here are considered hard errors and
3109 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3110 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3111 * because the unlink event handler will have set it if we
3112 * called LNetMDUnlink() above.
3114 spin_lock(&lp->lp_lock);
3115 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3120 * An unrecoverable error was encountered during discovery.
3121 * Set error status in peer and abort discovery.
3123 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3125 CDEBUG(D_NET, "Discovery error %s: %d\n",
3126 libcfs_nid2str(lp->lp_primary_nid), error);
3128 spin_lock(&lp->lp_lock);
3129 lp->lp_dc_error = error;
3130 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3131 lp->lp_state |= LNET_PEER_REDISCOVER;
3132 spin_unlock(&lp->lp_lock);
3136 * Mark the peer as discovered.
3138 static int lnet_peer_discovered(struct lnet_peer *lp)
3139 __must_hold(&lp->lp_lock)
3141 lp->lp_state |= LNET_PEER_DISCOVERED;
3142 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3143 LNET_PEER_REDISCOVER);
3145 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3152 * Discovering this peer is taking too long. Cancel any Ping or Push
3153 * that discovery is waiting on by unlinking the relevant MDs. The
3154 * lnet_discovery_event_handler() will proceed from here and complete
3157 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3159 struct lnet_handle_md ping_mdh;
3160 struct lnet_handle_md push_mdh;
3162 LNetInvalidateMDHandle(&ping_mdh);
3163 LNetInvalidateMDHandle(&push_mdh);
3165 spin_lock(&lp->lp_lock);
3166 if (lp->lp_state & LNET_PEER_PING_SENT) {
3167 ping_mdh = lp->lp_ping_mdh;
3168 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3170 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3171 push_mdh = lp->lp_push_mdh;
3172 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3174 spin_unlock(&lp->lp_lock);
3176 if (!LNetMDHandleIsInvalid(ping_mdh))
3177 LNetMDUnlink(ping_mdh);
3178 if (!LNetMDHandleIsInvalid(push_mdh))
3179 LNetMDUnlink(push_mdh);
3183 * Wait for work to be queued or some other change that must be
3184 * attended to. Returns non-zero if the discovery thread should shut
3187 static int lnet_peer_discovery_wait_for_work(void)
3194 cpt = lnet_net_lock_current();
3196 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3197 TASK_INTERRUPTIBLE);
3198 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3200 if (lnet_push_target_resize_needed())
3202 if (!list_empty(&the_lnet.ln_dc_request))
3204 if (!list_empty(&the_lnet.ln_msg_resend))
3206 lnet_net_unlock(cpt);
3209 * wakeup max every second to check if there are peers that
3210 * have been stuck on the working queue for greater than
3213 schedule_timeout(cfs_time_seconds(1));
3214 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3215 cpt = lnet_net_lock_current();
3217 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3219 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3222 lnet_net_unlock(cpt);
3224 CDEBUG(D_NET, "woken: %d\n", rc);
3230 * Messages that were pending on a destroyed peer will be put on a global
3231 * resend list. The message resend list will be checked by
3232 * the discovery thread when it wakes up, and will resend messages. These
3233 * messages can still be sendable in the case the lpni which was the initial
3234 * cause of the message re-queue was transfered to another peer.
3236 * It is possible that LNet could be shutdown while we're iterating
3237 * through the list. lnet_shudown_lndnets() will attempt to access the
3238 * resend list, but will have to wait until the spinlock is released, by
3239 * which time there shouldn't be any more messages on the resend list.
3240 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3241 * for the messages so they can be released. The other case is that
3242 * lnet_shudown_lndnets() can finalize all the messages before this
3243 * function can visit the resend list, in which case this function will be
3246 static void lnet_resend_msgs(void)
3248 struct lnet_msg *msg, *tmp;
3249 struct list_head resend;
3252 INIT_LIST_HEAD(&resend);
3254 spin_lock(&the_lnet.ln_msg_resend_lock);
3255 list_splice(&the_lnet.ln_msg_resend, &resend);
3256 spin_unlock(&the_lnet.ln_msg_resend_lock);
3258 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3259 list_del_init(&msg->msg_list);
3260 rc = lnet_send(msg->msg_src_nid_param, msg,
3261 msg->msg_rtr_nid_param);
3263 CNETERR("Error sending %s to %s: %d\n",
3264 lnet_msgtyp2str(msg->msg_type),
3265 libcfs_id2str(msg->msg_target), rc);
3266 lnet_finalize(msg, rc);
3271 /* The discovery thread. */
3272 static int lnet_peer_discovery(void *arg)
3274 struct lnet_peer *lp;
3277 CDEBUG(D_NET, "started\n");
3278 cfs_block_allsigs();
3281 if (lnet_peer_discovery_wait_for_work())
3286 if (lnet_push_target_resize_needed())
3287 lnet_push_target_resize();
3289 lnet_net_lock(LNET_LOCK_EX);
3290 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3294 * Process all incoming discovery work requests. When
3295 * discovery must wait on a peer to change state, it
3296 * is added to the tail of the ln_dc_working queue. A
3297 * timestamp keeps track of when the peer was added,
3298 * so we can time out discovery requests that take too
3301 while (!list_empty(&the_lnet.ln_dc_request)) {
3302 lp = list_first_entry(&the_lnet.ln_dc_request,
3303 struct lnet_peer, lp_dc_list);
3304 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3306 * set the time the peer was put on the dc_working
3307 * queue. It shouldn't remain on the queue
3308 * forever, in case the GET message (for ping)
3309 * doesn't get a REPLY or the PUT message (for
3310 * push) doesn't get an ACK.
3312 lp->lp_last_queued = ktime_get_real_seconds();
3313 lnet_net_unlock(LNET_LOCK_EX);
3316 * Select an action depending on the state of
3317 * the peer and whether discovery is disabled.
3318 * The check whether discovery is disabled is
3319 * done after the code that handles processing
3320 * for arrived data, cleanup for failures, and
3321 * forcing a Ping or Push.
3323 spin_lock(&lp->lp_lock);
3324 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3325 libcfs_nid2str(lp->lp_primary_nid), lp,
3327 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3328 rc = lnet_peer_data_present(lp);
3329 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3330 rc = lnet_peer_ping_failed(lp);
3331 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3332 rc = lnet_peer_push_failed(lp);
3333 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3334 rc = lnet_peer_send_ping(lp);
3335 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3336 rc = lnet_peer_send_push(lp);
3337 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3338 rc = lnet_peer_send_ping(lp);
3339 else if (lnet_peer_needs_push(lp))
3340 rc = lnet_peer_send_push(lp);
3342 rc = lnet_peer_discovered(lp);
3343 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3344 libcfs_nid2str(lp->lp_primary_nid), lp,
3346 spin_unlock(&lp->lp_lock);
3348 lnet_net_lock(LNET_LOCK_EX);
3349 if (rc == LNET_REDISCOVER_PEER) {
3350 list_move(&lp->lp_dc_list,
3351 &the_lnet.ln_dc_request);
3353 lnet_peer_discovery_error(lp, rc);
3355 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3356 lnet_peer_discovery_complete(lp);
3357 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3361 lnet_net_unlock(LNET_LOCK_EX);
3364 CDEBUG(D_NET, "stopping\n");
3366 * Clean up before telling lnet_peer_discovery_stop() that
3367 * we're done. Use wake_up() below to somewhat reduce the
3368 * size of the thundering herd if there are multiple threads
3369 * waiting on discovery of a single peer.
3372 /* Queue cleanup 1: stop all pending pings and pushes. */
3373 lnet_net_lock(LNET_LOCK_EX);
3374 while (!list_empty(&the_lnet.ln_dc_working)) {
3375 lp = list_first_entry(&the_lnet.ln_dc_working,
3376 struct lnet_peer, lp_dc_list);
3377 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3378 lnet_net_unlock(LNET_LOCK_EX);
3379 lnet_peer_cancel_discovery(lp);
3380 lnet_net_lock(LNET_LOCK_EX);
3382 lnet_net_unlock(LNET_LOCK_EX);
3384 /* Queue cleanup 2: wait for the expired queue to clear. */
3385 while (!list_empty(&the_lnet.ln_dc_expired))
3386 schedule_timeout(cfs_time_seconds(1));
3388 /* Queue cleanup 3: clear the request queue. */
3389 lnet_net_lock(LNET_LOCK_EX);
3390 while (!list_empty(&the_lnet.ln_dc_request)) {
3391 lp = list_first_entry(&the_lnet.ln_dc_request,
3392 struct lnet_peer, lp_dc_list);
3393 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3394 lnet_peer_discovery_complete(lp);
3396 lnet_net_unlock(LNET_LOCK_EX);
3398 LNetEQFree(the_lnet.ln_dc_eqh);
3399 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3401 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3402 wake_up(&the_lnet.ln_dc_waitq);
3404 CDEBUG(D_NET, "stopped\n");
3409 /* ln_api_mutex is held on entry. */
3410 int lnet_peer_discovery_start(void)
3412 struct task_struct *task;
3415 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3418 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3420 CERROR("Can't allocate discovery EQ: %d\n", rc);
3424 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3425 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3428 CERROR("Can't start peer discovery thread: %d\n", rc);
3430 LNetEQFree(the_lnet.ln_dc_eqh);
3431 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3433 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3436 CDEBUG(D_NET, "discovery start: %d\n", rc);
3441 /* ln_api_mutex is held on entry. */
3442 void lnet_peer_discovery_stop(void)
3444 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3447 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3448 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3449 wake_up(&the_lnet.ln_dc_waitq);
3451 wait_event(the_lnet.ln_dc_waitq,
3452 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3454 LASSERT(list_empty(&the_lnet.ln_dc_request));
3455 LASSERT(list_empty(&the_lnet.ln_dc_working));
3456 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3458 CDEBUG(D_NET, "discovery stopped\n");
3464 lnet_debug_peer(lnet_nid_t nid)
3466 char *aliveness = "NA";
3467 struct lnet_peer_ni *lp;
3470 cpt = lnet_cpt_of_nid(nid, NULL);
3473 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3475 lnet_net_unlock(cpt);
3476 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3480 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3481 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3483 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3484 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3485 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3486 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3487 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3489 lnet_peer_ni_decref_locked(lp);
3491 lnet_net_unlock(cpt);
3494 /* Gathering information for userspace. */
3496 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3497 char aliveness[LNET_MAX_STR_LEN],
3498 __u32 *cpt_iter, __u32 *refcount,
3499 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3500 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3501 __u32 *peer_tx_qnob)
3503 struct lnet_peer_table *peer_table;
3504 struct lnet_peer_ni *lp;
3509 /* get the number of CPTs */
3510 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3512 /* if the cpt number to be examined is >= the number of cpts in
3513 * the system then indicate that there are no more cpts to examin
3515 if (*cpt_iter >= lncpt)
3518 /* get the current table */
3519 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3520 /* if the ptable is NULL then there are no more cpts to examine */
3521 if (peer_table == NULL)
3524 lnet_net_lock(*cpt_iter);
3526 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3527 struct list_head *peers = &peer_table->pt_hash[j];
3529 list_for_each_entry(lp, peers, lpni_hashlist) {
3530 if (peer_index-- > 0)
3533 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3534 if (lnet_isrouter(lp) ||
3535 lnet_peer_aliveness_enabled(lp))
3536 snprintf(aliveness, LNET_MAX_STR_LEN,
3537 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3539 *nid = lp->lpni_nid;
3540 *refcount = atomic_read(&lp->lpni_refcount);
3541 *ni_peer_tx_credits =
3542 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3543 *peer_tx_credits = lp->lpni_txcredits;
3544 *peer_rtr_credits = lp->lpni_rtrcredits;
3545 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3546 *peer_tx_qnob = lp->lpni_txqnob;
3552 lnet_net_unlock(*cpt_iter);
3556 return found ? 0 : -ENOENT;
3559 /* ln_api_mutex is held, which keeps the peer list stable */
3560 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3562 struct lnet_ioctl_element_stats *lpni_stats;
3563 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3564 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3565 struct lnet_peer_ni_credit_info *lpni_info;
3566 struct lnet_peer_ni *lpni;
3567 struct lnet_peer *lp;
3572 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3579 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3580 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3581 size *= lp->lp_nnis;
3582 if (size > cfg->prcfg_size) {
3583 cfg->prcfg_size = size;
3588 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3589 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3590 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3591 cfg->prcfg_count = lp->lp_nnis;
3592 cfg->prcfg_size = size;
3593 cfg->prcfg_state = lp->lp_state;
3595 /* Allocate helper buffers. */
3597 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3600 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3603 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3604 if (!lpni_msg_stats)
3605 goto out_free_stats;
3606 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3608 goto out_free_msg_stats;
3613 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3614 nid = lpni->lpni_nid;
3615 if (copy_to_user(bulk, &nid, sizeof(nid)))
3616 goto out_free_hstats;
3617 bulk += sizeof(nid);
3619 memset(lpni_info, 0, sizeof(*lpni_info));
3620 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3621 if (lnet_isrouter(lpni) ||
3622 lnet_peer_aliveness_enabled(lpni))
3623 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3624 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3626 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3627 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3628 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3629 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3630 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3631 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3632 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3633 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3634 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3635 goto out_free_hstats;
3636 bulk += sizeof(*lpni_info);
3638 memset(lpni_stats, 0, sizeof(*lpni_stats));
3639 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3640 LNET_STATS_TYPE_SEND);
3641 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3642 LNET_STATS_TYPE_RECV);
3643 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3644 LNET_STATS_TYPE_DROP);
3645 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3646 goto out_free_hstats;
3647 bulk += sizeof(*lpni_stats);
3648 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3649 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3650 goto out_free_hstats;
3651 bulk += sizeof(*lpni_msg_stats);
3652 lpni_hstats->hlpni_network_timeout =
3653 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3654 lpni_hstats->hlpni_remote_dropped =
3655 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3656 lpni_hstats->hlpni_remote_timeout =
3657 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3658 lpni_hstats->hlpni_remote_error =
3659 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3660 lpni_hstats->hlpni_health_value =
3661 atomic_read(&lpni->lpni_healthv);
3662 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3663 goto out_free_hstats;
3664 bulk += sizeof(*lpni_hstats);
3669 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3671 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3673 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3675 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3677 lnet_peer_decref_locked(lp);
3683 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3685 /* the mt could've shutdown and cleaned up the queues */
3686 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3689 if (list_empty(&lpni->lpni_recovery) &&
3690 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3691 CERROR("lpni %s added to recovery queue. Health = %d\n",
3692 libcfs_nid2str(lpni->lpni_nid),
3693 atomic_read(&lpni->lpni_healthv));
3694 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3695 lnet_peer_ni_addref_locked(lpni);
3699 /* Call with the ln_api_mutex held */
3701 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3703 struct lnet_peer_table *ptable;
3704 struct lnet_peer *lp;
3705 struct lnet_peer_net *lpn;
3706 struct lnet_peer_ni *lpni;
3710 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3714 lnet_net_lock(LNET_LOCK_EX);
3715 lpni = lnet_find_peer_ni_locked(nid);
3717 lnet_net_unlock(LNET_LOCK_EX);
3720 atomic_set(&lpni->lpni_healthv, value);
3721 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3722 lnet_peer_ni_decref_locked(lpni);
3723 lnet_net_unlock(LNET_LOCK_EX);
3727 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3730 * Walk all the peers and reset the healhv for each one to the
3733 lnet_net_lock(LNET_LOCK_EX);
3734 for (cpt = 0; cpt < lncpt; cpt++) {
3735 ptable = the_lnet.ln_peer_tables[cpt];
3736 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3737 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3738 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3740 atomic_set(&lpni->lpni_healthv, value);
3741 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3746 lnet_net_unlock(LNET_LOCK_EX);