4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <linux/sched.h>
38 #ifdef HAVE_SCHED_HEADERS
39 #include <linux/sched/signal.h>
41 #include <linux/uaccess.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER (1)
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
54 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
55 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
56 lnet_peer_ni_decref_locked(lpni);
61 lnet_peer_net_added(struct lnet_net *net)
63 struct lnet_peer_ni *lpni, *tmp;
65 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66 lpni_on_remote_peer_ni_list) {
68 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
71 spin_lock(&lpni->lpni_lock);
72 lpni->lpni_txcredits =
73 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
74 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
75 lpni->lpni_rtrcredits =
76 lnet_peer_buffer_credits(lpni->lpni_net);
77 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
78 spin_unlock(&lpni->lpni_lock);
80 lnet_peer_remove_from_remote_list(lpni);
86 lnet_peer_tables_destroy(void)
88 struct lnet_peer_table *ptable;
89 struct list_head *hash;
93 if (!the_lnet.ln_peer_tables)
96 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
97 hash = ptable->pt_hash;
98 if (!hash) /* not intialized */
101 LASSERT(list_empty(&ptable->pt_zombie_list));
103 ptable->pt_hash = NULL;
104 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
105 LASSERT(list_empty(&hash[j]));
107 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
110 cfs_percpt_free(the_lnet.ln_peer_tables);
111 the_lnet.ln_peer_tables = NULL;
115 lnet_peer_tables_create(void)
117 struct lnet_peer_table *ptable;
118 struct list_head *hash;
122 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
124 if (the_lnet.ln_peer_tables == NULL) {
125 CERROR("Failed to allocate cpu-partition peer tables\n");
129 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
130 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
131 LNET_PEER_HASH_SIZE * sizeof(*hash));
133 CERROR("Failed to create peer hash table\n");
134 lnet_peer_tables_destroy();
138 spin_lock_init(&ptable->pt_zombie_lock);
139 INIT_LIST_HEAD(&ptable->pt_zombie_list);
141 INIT_LIST_HEAD(&ptable->pt_peer_list);
143 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
144 INIT_LIST_HEAD(&hash[j]);
145 ptable->pt_hash = hash; /* sign of initialization */
151 static struct lnet_peer_ni *
152 lnet_peer_ni_alloc(lnet_nid_t nid)
154 struct lnet_peer_ni *lpni;
155 struct lnet_net *net;
158 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
160 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
164 INIT_LIST_HEAD(&lpni->lpni_txq);
165 INIT_LIST_HEAD(&lpni->lpni_hashlist);
166 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
167 INIT_LIST_HEAD(&lpni->lpni_recovery);
168 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
169 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
171 spin_lock_init(&lpni->lpni_lock);
173 if (lnet_peers_start_down())
174 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
176 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
177 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
178 lpni->lpni_nid = nid;
179 lpni->lpni_cpt = cpt;
180 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
182 net = lnet_get_net_locked(LNET_NIDNET(nid));
183 lpni->lpni_net = net;
185 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
186 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
187 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
188 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
191 * This peer_ni is not on a local network, so we
192 * cannot add the credits here. In case the net is
193 * added later, add the peer_ni to the remote peer ni
194 * list so it can be easily found and revisited.
196 /* FIXME: per-net implementation instead? */
197 atomic_inc(&lpni->lpni_refcount);
198 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
199 &the_lnet.ln_remote_peer_ni_list);
202 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
207 static struct lnet_peer_net *
208 lnet_peer_net_alloc(__u32 net_id)
210 struct lnet_peer_net *lpn;
212 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
216 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
217 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
218 lpn->lpn_net_id = net_id;
220 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
226 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
228 struct lnet_peer *lp;
230 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
232 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
233 LASSERT(list_empty(&lpn->lpn_peer_nis));
234 LASSERT(list_empty(&lpn->lpn_peer_nets));
236 lpn->lpn_peer = NULL;
237 LIBCFS_FREE(lpn, sizeof(*lpn));
239 lnet_peer_decref_locked(lp);
242 static struct lnet_peer *
243 lnet_peer_alloc(lnet_nid_t nid)
245 struct lnet_peer *lp;
247 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
251 INIT_LIST_HEAD(&lp->lp_rtrq);
252 INIT_LIST_HEAD(&lp->lp_routes);
253 INIT_LIST_HEAD(&lp->lp_peer_list);
254 INIT_LIST_HEAD(&lp->lp_peer_nets);
255 INIT_LIST_HEAD(&lp->lp_dc_list);
256 INIT_LIST_HEAD(&lp->lp_dc_pendq);
257 INIT_LIST_HEAD(&lp->lp_rtr_list);
258 init_waitqueue_head(&lp->lp_dc_waitq);
259 spin_lock_init(&lp->lp_lock);
260 lp->lp_primary_nid = nid;
263 * all peers created on a router should have health on
264 * if it's not already on.
266 if (the_lnet.ln_routing && !lnet_health_sensitivity)
267 lp->lp_health_sensitivity = 1;
270 * Turn off discovery for loopback peer. If you're creating a peer
271 * for the loopback interface then that was initiated when we
272 * attempted to send a message over the loopback. There is no need
273 * to ever use a different interface when sending messages to
276 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
277 lp->lp_state = LNET_PEER_NO_DISCOVERY;
278 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
280 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
286 lnet_destroy_peer_locked(struct lnet_peer *lp)
288 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
290 LASSERT(atomic_read(&lp->lp_refcount) == 0);
291 LASSERT(lp->lp_rtr_refcount == 0);
292 LASSERT(list_empty(&lp->lp_peer_nets));
293 LASSERT(list_empty(&lp->lp_peer_list));
294 LASSERT(list_empty(&lp->lp_dc_list));
297 lnet_ping_buffer_decref(lp->lp_data);
300 * if there are messages still on the pending queue, then make
301 * sure to queue them on the ln_msg_resend list so they can be
302 * resent at a later point if the discovery thread is still
304 * If the discovery thread has stopped, then the wakeup will be a
305 * no-op, and it is expected the lnet_shutdown_lndnets() will
306 * eventually be called, which will traverse this list and
307 * finalize the messages on the list.
308 * We can not resend them now because we're holding the cpt lock.
309 * Releasing the lock can cause an inconsistent state
311 spin_lock(&the_lnet.ln_msg_resend_lock);
312 spin_lock(&lp->lp_lock);
313 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
314 spin_unlock(&lp->lp_lock);
315 spin_unlock(&the_lnet.ln_msg_resend_lock);
316 wake_up(&the_lnet.ln_dc_waitq);
318 LIBCFS_FREE(lp, sizeof(*lp));
322 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
323 * that peer_net, detach the peer_net from the peer.
325 * Call with lnet_net_lock/EX held
328 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
330 struct lnet_peer_table *ptable;
331 struct lnet_peer_net *lpn;
332 struct lnet_peer *lp;
335 * Belts and suspenders: gracefully handle teardown of a
336 * partially connected peer_ni.
338 lpn = lpni->lpni_peer_net;
340 list_del_init(&lpni->lpni_peer_nis);
342 * If there are no lpni's left, we detach lpn from
343 * lp_peer_nets, so it cannot be found anymore.
345 if (list_empty(&lpn->lpn_peer_nis))
346 list_del_init(&lpn->lpn_peer_nets);
348 /* Update peer NID count. */
353 * If there are no more peer nets, make the peer unfindable
354 * via the peer_tables.
356 * Otherwise, if the peer is DISCOVERED, tell discovery to
357 * take another look at it. This is a no-op if discovery for
358 * this peer did the detaching.
360 if (list_empty(&lp->lp_peer_nets)) {
361 list_del_init(&lp->lp_peer_list);
362 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
364 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
365 /* Discovery isn't running, nothing to do here. */
366 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
367 lnet_peer_queue_for_discovery(lp);
368 wake_up(&the_lnet.ln_dc_waitq);
370 CDEBUG(D_NET, "peer %s NID %s\n",
371 libcfs_nid2str(lp->lp_primary_nid),
372 libcfs_nid2str(lpni->lpni_nid));
375 /* called with lnet_net_lock LNET_LOCK_EX held */
377 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
379 struct lnet_peer_table *ptable = NULL;
381 /* don't remove a peer_ni if it's also a gateway */
382 if (lnet_isrouter(lpni) && !force) {
383 CERROR("Peer NI %s is a gateway. Can not delete it\n",
384 libcfs_nid2str(lpni->lpni_nid));
388 lnet_peer_remove_from_remote_list(lpni);
390 /* remove peer ni from the hash list. */
391 list_del_init(&lpni->lpni_hashlist);
394 * indicate the peer is being deleted so the monitor thread can
395 * remove it from the recovery queue.
397 spin_lock(&lpni->lpni_lock);
398 lpni->lpni_state |= LNET_PEER_NI_DELETING;
399 spin_unlock(&lpni->lpni_lock);
401 /* decrement the ref count on the peer table */
402 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
403 LASSERT(ptable->pt_number > 0);
407 * The peer_ni can no longer be found with a lookup. But there
408 * can be current users, so keep track of it on the zombie
409 * list until the reference count has gone to zero.
411 * The last reference may be lost in a place where the
412 * lnet_net_lock locks only a single cpt, and that cpt may not
413 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
416 spin_lock(&ptable->pt_zombie_lock);
417 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
418 ptable->pt_zombies++;
419 spin_unlock(&ptable->pt_zombie_lock);
421 /* no need to keep this peer_ni on the hierarchy anymore */
422 lnet_peer_detach_peer_ni_locked(lpni);
424 /* remove hashlist reference on peer_ni */
425 lnet_peer_ni_decref_locked(lpni);
430 void lnet_peer_uninit(void)
432 struct lnet_peer_ni *lpni, *tmp;
434 lnet_net_lock(LNET_LOCK_EX);
436 /* remove all peer_nis from the remote peer and the hash list */
437 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
438 lpni_on_remote_peer_ni_list)
439 lnet_peer_ni_del_locked(lpni, false);
441 lnet_peer_tables_destroy();
443 lnet_net_unlock(LNET_LOCK_EX);
447 lnet_peer_del_locked(struct lnet_peer *peer)
449 struct lnet_peer_ni *lpni = NULL, *lpni2;
452 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
454 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
455 while (lpni != NULL) {
456 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
457 rc = lnet_peer_ni_del_locked(lpni, false);
467 lnet_peer_del(struct lnet_peer *peer)
469 lnet_net_lock(LNET_LOCK_EX);
470 lnet_peer_del_locked(peer);
471 lnet_net_unlock(LNET_LOCK_EX);
477 * Delete a NID from a peer. Call with ln_api_mutex held.
480 * -EPERM: Non-DLC deletion from DLC-configured peer.
481 * -ENOENT: No lnet_peer_ni corresponding to the nid.
482 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
483 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
486 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
488 struct lnet_peer_ni *lpni;
489 lnet_nid_t primary_nid = lp->lp_primary_nid;
491 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
493 if (!(flags & LNET_PEER_CONFIGURED)) {
494 if (lp->lp_state & LNET_PEER_CONFIGURED) {
499 lpni = lnet_find_peer_ni_locked(nid);
504 lnet_peer_ni_decref_locked(lpni);
505 if (lp != lpni->lpni_peer_net->lpn_peer) {
511 * This function only allows deletion of the primary NID if it
514 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
519 lnet_net_lock(LNET_LOCK_EX);
521 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
522 struct lnet_peer_ni *lpni2;
523 /* assign the next peer_ni to be the primary */
524 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
526 lp->lp_primary_nid = lpni->lpni_nid;
528 rc = lnet_peer_ni_del_locked(lpni, force);
530 lnet_net_unlock(LNET_LOCK_EX);
533 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
534 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
540 lnet_peer_table_cleanup_locked(struct lnet_net *net,
541 struct lnet_peer_table *ptable)
544 struct lnet_peer_ni *next;
545 struct lnet_peer_ni *lpni;
546 struct lnet_peer *peer;
548 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
549 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
551 if (net != NULL && net != lpni->lpni_net)
554 peer = lpni->lpni_peer_net->lpn_peer;
555 if (peer->lp_primary_nid != lpni->lpni_nid) {
556 lnet_peer_ni_del_locked(lpni, false);
560 * Removing the primary NID implies removing
561 * the entire peer. Advance next beyond any
562 * peer_ni that belongs to the same peer.
564 list_for_each_entry_from(next, &ptable->pt_hash[i],
566 if (next->lpni_peer_net->lpn_peer != peer)
569 lnet_peer_del_locked(peer);
575 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
579 spin_lock(&ptable->pt_zombie_lock);
580 while (ptable->pt_zombies) {
581 spin_unlock(&ptable->pt_zombie_lock);
583 if (is_power_of_2(i)) {
585 "Waiting for %d zombies on peer table\n",
588 set_current_state(TASK_UNINTERRUPTIBLE);
589 schedule_timeout(cfs_time_seconds(1) >> 1);
590 spin_lock(&ptable->pt_zombie_lock);
592 spin_unlock(&ptable->pt_zombie_lock);
596 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
597 struct lnet_peer_table *ptable)
599 struct lnet_peer_ni *lp;
600 struct lnet_peer_ni *tmp;
604 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
605 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
607 if (net != lp->lpni_net)
610 if (!lnet_isrouter(lp))
613 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
615 lnet_net_unlock(LNET_LOCK_EX);
616 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
617 lnet_net_lock(LNET_LOCK_EX);
623 lnet_peer_tables_cleanup(struct lnet_net *net)
626 struct lnet_peer_table *ptable;
628 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
629 /* If just deleting the peers for a NI, get rid of any routes these
630 * peers are gateways for. */
631 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
632 lnet_net_lock(LNET_LOCK_EX);
633 lnet_peer_table_del_rtrs_locked(net, ptable);
634 lnet_net_unlock(LNET_LOCK_EX);
637 /* Start the cleanup process */
638 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
639 lnet_net_lock(LNET_LOCK_EX);
640 lnet_peer_table_cleanup_locked(net, ptable);
641 lnet_net_unlock(LNET_LOCK_EX);
644 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
645 lnet_peer_ni_finalize_wait(ptable);
648 static struct lnet_peer_ni *
649 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
651 struct list_head *peers;
652 struct lnet_peer_ni *lp;
654 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
656 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
657 list_for_each_entry(lp, peers, lpni_hashlist) {
658 if (lp->lpni_nid == nid) {
659 lnet_peer_ni_addref_locked(lp);
667 struct lnet_peer_ni *
668 lnet_find_peer_ni_locked(lnet_nid_t nid)
670 struct lnet_peer_ni *lpni;
671 struct lnet_peer_table *ptable;
674 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
676 ptable = the_lnet.ln_peer_tables[cpt];
677 lpni = lnet_get_peer_ni_locked(ptable, nid);
682 struct lnet_peer_ni *
683 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
685 struct lnet_peer_net *lpn;
686 struct lnet_peer_ni *lpni;
688 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
692 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
693 if (lpni->lpni_nid == nid)
701 lnet_find_peer(lnet_nid_t nid)
703 struct lnet_peer_ni *lpni;
704 struct lnet_peer *lp = NULL;
707 cpt = lnet_net_lock_current();
708 lpni = lnet_find_peer_ni_locked(nid);
710 lp = lpni->lpni_peer_net->lpn_peer;
711 lnet_peer_addref_locked(lp);
712 lnet_peer_ni_decref_locked(lpni);
714 lnet_net_unlock(cpt);
719 struct lnet_peer_net *
720 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
722 struct lnet_peer_net *net;
725 /* no net id provided return the first net */
726 net = list_first_entry_or_null(&lp->lp_peer_nets,
727 struct lnet_peer_net,
733 /* find the net after the one provided */
734 list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
735 if (net->lpn_net_id == prev_lpn_id) {
737 * if we reached the end of the list loop to the
740 if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
741 return list_first_entry_or_null(&lp->lp_peer_nets,
742 struct lnet_peer_net,
745 return list_next_entry(net, lpn_peer_nets);
752 struct lnet_peer_ni *
753 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
754 struct lnet_peer_net *peer_net,
755 struct lnet_peer_ni *prev)
757 struct lnet_peer_ni *lpni;
758 struct lnet_peer_net *net = peer_net;
762 if (list_empty(&peer->lp_peer_nets))
765 net = list_entry(peer->lp_peer_nets.next,
766 struct lnet_peer_net,
769 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
775 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
777 * if you reached the end of the peer ni list and the peer
778 * net is specified then there are no more peer nis in that
785 * we reached the end of this net ni list. move to the
788 if (prev->lpni_peer_net->lpn_peer_nets.next ==
790 /* no more nets and no more NIs. */
793 /* get the next net */
794 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
795 struct lnet_peer_net,
797 /* get the ni on it */
798 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
804 /* there are more nis left */
805 lpni = list_entry(prev->lpni_peer_nis.next,
806 struct lnet_peer_ni, lpni_peer_nis);
811 /* Call with the ln_api_mutex held */
812 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
814 struct lnet_process_id id;
815 struct lnet_peer_table *ptable;
816 struct lnet_peer *lp;
825 if (the_lnet.ln_state != LNET_STATE_RUNNING)
828 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
831 * Count the number of peers, and return E2BIG if the buffer
832 * is too small. We'll also return the desired size.
835 for (cpt = 0; cpt < lncpt; cpt++) {
836 ptable = the_lnet.ln_peer_tables[cpt];
837 count += ptable->pt_peers;
839 size = count * sizeof(*ids);
844 * Walk the peer lists and copy out the primary nids.
845 * This is safe because the peer lists are only modified
846 * while the ln_api_mutex is held. So we don't need to
847 * hold the lnet_net_lock as well, and can therefore
848 * directly call copy_to_user().
851 memset(&id, 0, sizeof(id));
852 id.pid = LNET_PID_LUSTRE;
854 for (cpt = 0; cpt < lncpt; cpt++) {
855 ptable = the_lnet.ln_peer_tables[cpt];
856 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
859 id.nid = lp->lp_primary_nid;
860 if (copy_to_user(&ids[i], &id, sizeof(id)))
873 * Start pushes to peers that need to be updated for a configuration
874 * change on this node.
877 lnet_push_update_to_peers(int force)
879 struct lnet_peer_table *ptable;
880 struct lnet_peer *lp;
884 lnet_net_lock(LNET_LOCK_EX);
885 if (lnet_peer_discovery_disabled)
887 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
888 for (cpt = 0; cpt < lncpt; cpt++) {
889 ptable = the_lnet.ln_peer_tables[cpt];
890 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
892 spin_lock(&lp->lp_lock);
893 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
894 lp->lp_state |= LNET_PEER_FORCE_PUSH;
895 spin_unlock(&lp->lp_lock);
897 if (lnet_peer_needs_push(lp))
898 lnet_peer_queue_for_discovery(lp);
901 lnet_net_unlock(LNET_LOCK_EX);
902 wake_up(&the_lnet.ln_dc_waitq);
906 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
907 * this is a preferred point-to-point path. Call with lnet_net_lock in
911 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
915 if (lpni->lpni_pref_nnids == 0)
917 if (lpni->lpni_pref_nnids == 1)
918 return lpni->lpni_pref.nid == nid;
919 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
920 if (lpni->lpni_pref.nids[i] == nid)
927 * Set a single ni as preferred, provided no preferred ni is already
928 * defined. Only to be used for non-multi-rail peer_ni.
931 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
935 spin_lock(&lpni->lpni_lock);
936 if (nid == LNET_NID_ANY) {
938 } else if (lpni->lpni_pref_nnids > 0) {
940 } else if (lpni->lpni_pref_nnids == 0) {
941 lpni->lpni_pref.nid = nid;
942 lpni->lpni_pref_nnids = 1;
943 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
945 spin_unlock(&lpni->lpni_lock);
947 CDEBUG(D_NET, "peer %s nid %s: %d\n",
948 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
953 * Clear the preferred NID from a non-multi-rail peer_ni, provided
954 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
957 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
961 spin_lock(&lpni->lpni_lock);
962 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
963 lpni->lpni_pref_nnids = 0;
964 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
965 } else if (lpni->lpni_pref_nnids == 0) {
970 spin_unlock(&lpni->lpni_lock);
972 CDEBUG(D_NET, "peer %s: %d\n",
973 libcfs_nid2str(lpni->lpni_nid), rc);
978 * Clear the preferred NIDs from a non-multi-rail peer.
981 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
983 struct lnet_peer_ni *lpni = NULL;
985 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
986 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
990 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
992 lnet_nid_t *nids = NULL;
993 lnet_nid_t *oldnids = NULL;
994 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
999 if (nid == LNET_NID_ANY) {
1004 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1009 /* A non-MR node may have only one preferred NI per peer_ni */
1010 if (lpni->lpni_pref_nnids > 0) {
1011 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1017 if (lpni->lpni_pref_nnids != 0) {
1018 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1019 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1024 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
1025 if (lpni->lpni_pref.nids[i] == nid) {
1026 LIBCFS_FREE(nids, size);
1030 nids[i] = lpni->lpni_pref.nids[i];
1035 lnet_net_lock(LNET_LOCK_EX);
1036 spin_lock(&lpni->lpni_lock);
1037 if (lpni->lpni_pref_nnids == 0) {
1038 lpni->lpni_pref.nid = nid;
1040 oldnids = lpni->lpni_pref.nids;
1041 lpni->lpni_pref.nids = nids;
1043 lpni->lpni_pref_nnids++;
1044 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1045 spin_unlock(&lpni->lpni_lock);
1046 lnet_net_unlock(LNET_LOCK_EX);
1049 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1050 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1053 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1054 spin_lock(&lpni->lpni_lock);
1055 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1056 spin_unlock(&lpni->lpni_lock);
1058 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1059 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1064 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1066 lnet_nid_t *nids = NULL;
1067 lnet_nid_t *oldnids = NULL;
1068 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1073 if (lpni->lpni_pref_nnids == 0) {
1078 if (lpni->lpni_pref_nnids == 1) {
1079 if (lpni->lpni_pref.nid != nid) {
1083 } else if (lpni->lpni_pref_nnids == 2) {
1084 if (lpni->lpni_pref.nids[0] != nid &&
1085 lpni->lpni_pref.nids[1] != nid) {
1090 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1091 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1096 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1097 if (lpni->lpni_pref.nids[i] != nid)
1099 nids[j++] = lpni->lpni_pref.nids[i];
1101 /* Check if we actually removed a nid. */
1102 if (j == lpni->lpni_pref_nnids) {
1103 LIBCFS_FREE(nids, size);
1109 lnet_net_lock(LNET_LOCK_EX);
1110 spin_lock(&lpni->lpni_lock);
1111 if (lpni->lpni_pref_nnids == 1) {
1112 lpni->lpni_pref.nid = LNET_NID_ANY;
1113 } else if (lpni->lpni_pref_nnids == 2) {
1114 oldnids = lpni->lpni_pref.nids;
1115 if (oldnids[0] == nid)
1116 lpni->lpni_pref.nid = oldnids[1];
1118 lpni->lpni_pref.nid = oldnids[2];
1120 oldnids = lpni->lpni_pref.nids;
1121 lpni->lpni_pref.nids = nids;
1123 lpni->lpni_pref_nnids--;
1124 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1125 spin_unlock(&lpni->lpni_lock);
1126 lnet_net_unlock(LNET_LOCK_EX);
1129 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1130 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1133 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1134 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1139 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1141 struct lnet_peer_ni *lpni;
1142 lnet_nid_t primary_nid = nid;
1144 lpni = lnet_find_peer_ni_locked(nid);
1146 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1147 lnet_peer_ni_decref_locked(lpni);
1154 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1156 if (lnet_peer_discovery_disabled)
1159 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1160 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1171 lnet_is_discovery_disabled(struct lnet_peer *lp)
1175 spin_lock(&lp->lp_lock);
1176 rc = lnet_is_discovery_disabled_locked(lp);
1177 spin_unlock(&lp->lp_lock);
1183 LNetPrimaryNID(lnet_nid_t nid)
1185 struct lnet_peer *lp;
1186 struct lnet_peer_ni *lpni;
1187 lnet_nid_t primary_nid = nid;
1191 cpt = lnet_net_lock_current();
1192 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1197 lp = lpni->lpni_peer_net->lpn_peer;
1199 while (!lnet_peer_is_uptodate(lp)) {
1200 rc = lnet_discover_peer_locked(lpni, cpt, true);
1203 lp = lpni->lpni_peer_net->lpn_peer;
1205 /* Only try once if discovery is disabled */
1206 if (lnet_is_discovery_disabled(lp))
1209 primary_nid = lp->lp_primary_nid;
1211 lnet_peer_ni_decref_locked(lpni);
1213 lnet_net_unlock(cpt);
1215 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1216 libcfs_nid2str(primary_nid), rc);
1219 EXPORT_SYMBOL(LNetPrimaryNID);
1221 struct lnet_peer_net *
1222 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1224 struct lnet_peer_net *peer_net;
1225 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1226 if (peer_net->lpn_net_id == net_id)
1233 * Attach a peer_ni to a peer_net and peer. This function assumes
1234 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1235 * may be attached to a different peer, in which case it will be
1236 * properly detached first. The whole operation is done atomically.
1238 * Always returns 0. This is the last function called from functions
1239 * that do return an int, so returning 0 here allows the compiler to
1243 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1244 struct lnet_peer_net *lpn,
1245 struct lnet_peer_ni *lpni,
1248 struct lnet_peer_table *ptable;
1250 /* Install the new peer_ni */
1251 lnet_net_lock(LNET_LOCK_EX);
1252 /* Add peer_ni to global peer table hash, if necessary. */
1253 if (list_empty(&lpni->lpni_hashlist)) {
1254 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1256 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1257 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1258 ptable->pt_version++;
1259 ptable->pt_number++;
1260 /* This is the 1st refcount on lpni. */
1261 atomic_inc(&lpni->lpni_refcount);
1264 /* Detach the peer_ni from an existing peer, if necessary. */
1265 if (lpni->lpni_peer_net) {
1266 LASSERT(lpni->lpni_peer_net != lpn);
1267 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1268 lnet_peer_detach_peer_ni_locked(lpni);
1269 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1270 lpni->lpni_peer_net = NULL;
1273 /* Add peer_ni to peer_net */
1274 lpni->lpni_peer_net = lpn;
1275 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1276 lnet_peer_net_addref_locked(lpn);
1278 /* Add peer_net to peer */
1279 if (!lpn->lpn_peer) {
1281 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1282 lnet_peer_addref_locked(lp);
1285 /* Add peer to global peer list, if necessary */
1286 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1287 if (list_empty(&lp->lp_peer_list)) {
1288 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1293 /* Update peer state */
1294 spin_lock(&lp->lp_lock);
1295 if (flags & LNET_PEER_CONFIGURED) {
1296 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1297 lp->lp_state |= LNET_PEER_CONFIGURED;
1299 if (flags & LNET_PEER_MULTI_RAIL) {
1300 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1301 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1302 lnet_peer_clr_non_mr_pref_nids(lp);
1305 spin_unlock(&lp->lp_lock);
1308 lnet_net_unlock(LNET_LOCK_EX);
1310 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1311 libcfs_nid2str(lp->lp_primary_nid),
1312 libcfs_nid2str(lpni->lpni_nid), flags);
1318 * Create a new peer, with nid as its primary nid.
1320 * Call with the lnet_api_mutex held.
1323 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1325 struct lnet_peer *lp;
1326 struct lnet_peer_net *lpn;
1327 struct lnet_peer_ni *lpni;
1330 LASSERT(nid != LNET_NID_ANY);
1333 * No need for the lnet_net_lock here, because the
1334 * lnet_api_mutex is held.
1336 lpni = lnet_find_peer_ni_locked(nid);
1338 /* A peer with this NID already exists. */
1339 lp = lpni->lpni_peer_net->lpn_peer;
1340 lnet_peer_ni_decref_locked(lpni);
1342 * This is an error if the peer was configured and the
1343 * primary NID differs or an attempt is made to change
1344 * the Multi-Rail flag. Otherwise the assumption is
1345 * that an existing peer is being modified.
1347 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1348 if (lp->lp_primary_nid != nid)
1350 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1354 /* Delete and recreate as a configured peer. */
1358 /* Create peer, peer_net, and peer_ni. */
1360 lp = lnet_peer_alloc(nid);
1363 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1366 lpni = lnet_peer_ni_alloc(nid);
1370 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1373 LIBCFS_FREE(lpn, sizeof(*lpn));
1375 LIBCFS_FREE(lp, sizeof(*lp));
1377 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1378 libcfs_nid2str(nid), flags, rc);
1383 * Add a NID to a peer. Call with ln_api_mutex held.
1386 * -EPERM: Non-DLC addition to a DLC-configured peer.
1387 * -EEXIST: The NID was configured by DLC for a different peer.
1388 * -ENOMEM: Out of memory.
1389 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1390 * non-multi-rail peer.
1393 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1395 struct lnet_peer_net *lpn;
1396 struct lnet_peer_ni *lpni;
1400 LASSERT(nid != LNET_NID_ANY);
1402 /* A configured peer can only be updated through configuration. */
1403 if (!(flags & LNET_PEER_CONFIGURED)) {
1404 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1411 * The MULTI_RAIL flag can be set but not cleared, because
1412 * that would leave the peer struct in an invalid state.
1414 if (flags & LNET_PEER_MULTI_RAIL) {
1415 spin_lock(&lp->lp_lock);
1416 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1417 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1418 lnet_peer_clr_non_mr_pref_nids(lp);
1420 spin_unlock(&lp->lp_lock);
1421 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1426 lpni = lnet_find_peer_ni_locked(nid);
1429 * A peer_ni already exists. This is only a problem if
1430 * it is not connected to this peer and was configured
1433 lnet_peer_ni_decref_locked(lpni);
1434 if (lpni->lpni_peer_net->lpn_peer == lp)
1436 if (lnet_peer_ni_is_configured(lpni)) {
1440 /* If this is the primary NID, destroy the peer. */
1441 if (lnet_peer_ni_is_primary(lpni)) {
1442 struct lnet_peer *rtr_lp =
1443 lpni->lpni_peer_net->lpn_peer;
1444 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1446 * if we're trying to delete a router it means
1447 * we're moving this peer NI to a new peer so must
1448 * transfer router properties to the new peer
1450 if (rtr_refcount > 0) {
1451 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1452 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1454 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1455 lpni = lnet_peer_ni_alloc(nid);
1462 lpni = lnet_peer_ni_alloc(nid);
1470 * Get the peer_net. Check that we're not adding a second
1471 * peer_ni on a peer_net of a non-multi-rail peer.
1473 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1475 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1480 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1485 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1488 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1489 if (!lpni->lpni_peer_net)
1490 LIBCFS_FREE(lpni, sizeof(*lpni));
1492 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1493 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1499 * Update the primary NID of a peer, if possible.
1501 * Call with the lnet_api_mutex held.
1504 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1506 lnet_nid_t old = lp->lp_primary_nid;
1509 if (lp->lp_primary_nid == nid)
1511 rc = lnet_peer_add_nid(lp, nid, flags);
1514 lp->lp_primary_nid = nid;
1516 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1517 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1522 * lpni creation initiated due to traffic either sending or receiving.
1525 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1527 struct lnet_peer *lp;
1528 struct lnet_peer_net *lpn;
1529 struct lnet_peer_ni *lpni;
1531 * Assume peer is Multi-Rail capable and let discovery find out
1534 unsigned flags = LNET_PEER_MULTI_RAIL;
1537 if (nid == LNET_NID_ANY) {
1542 /* lnet_net_lock is not needed here because ln_api_lock is held */
1543 lpni = lnet_find_peer_ni_locked(nid);
1546 * We must have raced with another thread. Since we
1547 * know next to nothing about a peer_ni created by
1548 * traffic, we just assume everything is ok and
1551 lnet_peer_ni_decref_locked(lpni);
1555 /* Create peer, peer_net, and peer_ni. */
1557 lp = lnet_peer_alloc(nid);
1560 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1563 lpni = lnet_peer_ni_alloc(nid);
1566 if (pref != LNET_NID_ANY)
1567 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1569 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1572 LIBCFS_FREE(lpn, sizeof(*lpn));
1574 LIBCFS_FREE(lp, sizeof(*lp));
1576 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1581 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1583 * This API handles the following combinations:
1584 * Create a peer with its primary NI if only the prim_nid is provided
1585 * Add a NID to a peer identified by the prim_nid. The peer identified
1586 * by the prim_nid must already exist.
1587 * The peer being created may be non-MR.
1589 * The caller must hold ln_api_mutex. This prevents the peer from
1590 * being created/modified/deleted by a different thread.
1593 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1595 struct lnet_peer *lp = NULL;
1596 struct lnet_peer_ni *lpni;
1599 /* The prim_nid must always be specified */
1600 if (prim_nid == LNET_NID_ANY)
1603 flags = LNET_PEER_CONFIGURED;
1605 flags |= LNET_PEER_MULTI_RAIL;
1608 * If nid isn't specified, we must create a new peer with
1609 * prim_nid as its primary nid.
1611 if (nid == LNET_NID_ANY)
1612 return lnet_peer_add(prim_nid, flags);
1614 /* Look up the prim_nid, which must exist. */
1615 lpni = lnet_find_peer_ni_locked(prim_nid);
1618 lnet_peer_ni_decref_locked(lpni);
1619 lp = lpni->lpni_peer_net->lpn_peer;
1621 /* Peer must have been configured. */
1622 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1623 CDEBUG(D_NET, "peer %s was not configured\n",
1624 libcfs_nid2str(prim_nid));
1628 /* Primary NID must match */
1629 if (lp->lp_primary_nid != prim_nid) {
1630 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1631 libcfs_nid2str(prim_nid),
1632 libcfs_nid2str(lp->lp_primary_nid));
1636 /* Multi-Rail flag must match. */
1637 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1638 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1639 libcfs_nid2str(prim_nid));
1643 return lnet_peer_add_nid(lp, nid, flags);
1647 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1649 * This API handles the following combinations:
1650 * Delete a NI from a peer if both prim_nid and nid are provided.
1651 * Delete a peer if only prim_nid is provided.
1652 * Delete a peer if its primary nid is provided.
1654 * The caller must hold ln_api_mutex. This prevents the peer from
1655 * being modified/deleted by a different thread.
1658 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1660 struct lnet_peer *lp;
1661 struct lnet_peer_ni *lpni;
1664 if (prim_nid == LNET_NID_ANY)
1667 lpni = lnet_find_peer_ni_locked(prim_nid);
1670 lnet_peer_ni_decref_locked(lpni);
1671 lp = lpni->lpni_peer_net->lpn_peer;
1673 if (prim_nid != lp->lp_primary_nid) {
1674 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1675 libcfs_nid2str(prim_nid),
1676 libcfs_nid2str(lp->lp_primary_nid));
1680 lnet_net_lock(LNET_LOCK_EX);
1681 if (lp->lp_rtr_refcount > 0) {
1682 lnet_net_unlock(LNET_LOCK_EX);
1683 CERROR("%s is a router. Can not be deleted\n",
1684 libcfs_nid2str(prim_nid));
1687 lnet_net_unlock(LNET_LOCK_EX);
1689 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1690 return lnet_peer_del(lp);
1692 flags = LNET_PEER_CONFIGURED;
1693 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1694 flags |= LNET_PEER_MULTI_RAIL;
1696 return lnet_peer_del_nid(lp, nid, flags);
1700 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1702 struct lnet_peer_table *ptable;
1703 struct lnet_peer_net *lpn;
1705 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1707 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1708 LASSERT(list_empty(&lpni->lpni_txq));
1709 LASSERT(lpni->lpni_txqnob == 0);
1710 LASSERT(list_empty(&lpni->lpni_peer_nis));
1711 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1713 lpn = lpni->lpni_peer_net;
1714 lpni->lpni_peer_net = NULL;
1715 lpni->lpni_net = NULL;
1717 /* remove the peer ni from the zombie list */
1718 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1719 spin_lock(&ptable->pt_zombie_lock);
1720 list_del_init(&lpni->lpni_hashlist);
1721 ptable->pt_zombies--;
1722 spin_unlock(&ptable->pt_zombie_lock);
1724 if (lpni->lpni_pref_nnids > 1) {
1725 LIBCFS_FREE(lpni->lpni_pref.nids,
1726 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1728 LIBCFS_FREE(lpni, sizeof(*lpni));
1730 lnet_peer_net_decref_locked(lpn);
1733 struct lnet_peer_ni *
1734 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1736 struct lnet_peer_ni *lpni = NULL;
1739 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1740 return ERR_PTR(-ESHUTDOWN);
1743 * find if a peer_ni already exists.
1744 * If so then just return that.
1746 lpni = lnet_find_peer_ni_locked(nid);
1750 lnet_net_unlock(cpt);
1752 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1755 goto out_net_relock;
1758 lpni = lnet_find_peer_ni_locked(nid);
1768 * Get a peer_ni for the given nid, create it if necessary. Takes a
1769 * hold on the peer_ni.
1771 struct lnet_peer_ni *
1772 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1774 struct lnet_peer_ni *lpni = NULL;
1777 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1778 return ERR_PTR(-ESHUTDOWN);
1781 * find if a peer_ni already exists.
1782 * If so then just return that.
1784 lpni = lnet_find_peer_ni_locked(nid);
1790 * use the lnet_api_mutex to serialize the creation of the peer_ni
1791 * and the creation/deletion of the local ni/net. When a local ni is
1792 * created, if there exists a set of peer_nis on that network,
1793 * they need to be traversed and updated. When a local NI is
1794 * deleted, which could result in a network being deleted, then
1795 * all peer nis on that network need to be removed as well.
1797 * Creation through traffic should also be serialized with
1798 * creation through DLC.
1800 lnet_net_unlock(cpt);
1801 mutex_lock(&the_lnet.ln_api_mutex);
1803 * Shutdown is only set under the ln_api_lock, so a single
1804 * check here is sufficent.
1806 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1807 lpni = ERR_PTR(-ESHUTDOWN);
1808 goto out_mutex_unlock;
1811 rc = lnet_peer_ni_traffic_add(nid, pref);
1814 goto out_mutex_unlock;
1817 lpni = lnet_find_peer_ni_locked(nid);
1821 mutex_unlock(&the_lnet.ln_api_mutex);
1824 /* Lock has been dropped, check again for shutdown. */
1825 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1827 lnet_peer_ni_decref_locked(lpni);
1828 lpni = ERR_PTR(-ESHUTDOWN);
1835 lnet_peer_gw_discovery(struct lnet_peer *lp)
1839 spin_lock(&lp->lp_lock);
1840 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1842 spin_unlock(&lp->lp_lock);
1848 lnet_peer_is_uptodate(struct lnet_peer *lp)
1852 spin_lock(&lp->lp_lock);
1853 rc = lnet_peer_is_uptodate_locked(lp);
1854 spin_unlock(&lp->lp_lock);
1859 * Is a peer uptodate from the point of view of discovery?
1861 * If it is currently being processed, obviously not.
1862 * A forced Ping or Push is also handled by the discovery thread.
1864 * Otherwise look at whether the peer needs rediscovering.
1867 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
1868 __must_hold(&lp->lp_lock)
1872 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1873 LNET_PEER_FORCE_PING |
1874 LNET_PEER_FORCE_PUSH)) {
1876 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1878 } else if (lnet_peer_needs_push(lp)) {
1880 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1881 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1893 * Queue a peer for the attention of the discovery thread. Call with
1894 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1895 * -EALREADY if the peer was already queued.
1897 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1901 spin_lock(&lp->lp_lock);
1902 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1903 lp->lp_state |= LNET_PEER_DISCOVERING;
1904 spin_unlock(&lp->lp_lock);
1905 if (list_empty(&lp->lp_dc_list)) {
1906 lnet_peer_addref_locked(lp);
1907 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1908 wake_up(&the_lnet.ln_dc_waitq);
1914 CDEBUG(D_NET, "Queue peer %s: %d\n",
1915 libcfs_nid2str(lp->lp_primary_nid), rc);
1921 * Discovery of a peer is complete. Wake all waiters on the peer.
1922 * Call with lnet_net_lock/EX held.
1924 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1926 struct lnet_msg *msg, *tmp;
1928 struct list_head pending_msgs;
1930 INIT_LIST_HEAD(&pending_msgs);
1932 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1933 libcfs_nid2str(lp->lp_primary_nid));
1935 list_del_init(&lp->lp_dc_list);
1936 spin_lock(&lp->lp_lock);
1937 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1938 spin_unlock(&lp->lp_lock);
1939 wake_up_all(&lp->lp_dc_waitq);
1941 if (lp->lp_rtr_refcount > 0)
1942 lnet_router_discovery_complete(lp);
1944 lnet_net_unlock(LNET_LOCK_EX);
1946 /* iterate through all pending messages and send them again */
1947 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1948 list_del_init(&msg->msg_list);
1949 if (lp->lp_dc_error) {
1950 lnet_finalize(msg, lp->lp_dc_error);
1954 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1955 lnet_msgtyp2str(msg->msg_type),
1956 libcfs_id2str(msg->msg_target));
1957 rc = lnet_send(msg->msg_src_nid_param, msg,
1958 msg->msg_rtr_nid_param);
1960 CNETERR("Error sending %s to %s: %d\n",
1961 lnet_msgtyp2str(msg->msg_type),
1962 libcfs_id2str(msg->msg_target), rc);
1963 lnet_finalize(msg, rc);
1966 lnet_net_lock(LNET_LOCK_EX);
1967 lnet_peer_decref_locked(lp);
1971 * Handle inbound push.
1972 * Like any event handler, called with lnet_res_lock/CPT held.
1974 void lnet_peer_push_event(struct lnet_event *ev)
1976 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1977 struct lnet_peer *lp;
1979 /* lnet_find_peer() adds a refcount */
1980 lp = lnet_find_peer(ev->source.nid);
1982 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1983 libcfs_nid2str(ev->initiator.nid),
1984 libcfs_nid2str(ev->source.nid));
1988 /* Ensure peer state remains consistent while we modify it. */
1989 spin_lock(&lp->lp_lock);
1992 * If some kind of error happened the contents of the message
1993 * cannot be used. Clear the NIDS_UPTODATE and set the
1994 * FORCE_PING flag to trigger a ping.
1997 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1998 lp->lp_state |= LNET_PEER_FORCE_PING;
1999 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2001 libcfs_nid2str(lp->lp_primary_nid),
2002 libcfs_nid2str(ev->source.nid));
2007 * A push with invalid or corrupted info. Clear the UPTODATE
2008 * flag to trigger a ping.
2010 if (lnet_ping_info_validate(&pbuf->pb_info)) {
2011 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2012 lp->lp_state |= LNET_PEER_FORCE_PING;
2013 CDEBUG(D_NET, "Corrupted Push from %s\n",
2014 libcfs_nid2str(lp->lp_primary_nid));
2019 * Make sure we'll allocate the correct size ping buffer when
2022 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2023 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2026 * A non-Multi-Rail peer is not supposed to be capable of
2029 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2030 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2031 libcfs_nid2str(lp->lp_primary_nid));
2036 * Check the MULTIRAIL flag. Complain if the peer was DLC
2037 * configured without it.
2039 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2040 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2041 CERROR("Push says %s is Multi-Rail, DLC says not\n",
2042 libcfs_nid2str(lp->lp_primary_nid));
2044 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2045 lnet_peer_clr_non_mr_pref_nids(lp);
2050 * The peer may have discovery disabled at its end. Set
2051 * NO_DISCOVERY as appropriate.
2053 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2054 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2055 libcfs_nid2str(lp->lp_primary_nid));
2056 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2057 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2058 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2059 libcfs_nid2str(lp->lp_primary_nid));
2060 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2064 * Check for truncation of the Put message. Clear the
2065 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2066 * and tell discovery to allocate a bigger buffer.
2068 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2069 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2070 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2071 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2072 lp->lp_state |= LNET_PEER_FORCE_PING;
2073 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2074 libcfs_nid2str(lp->lp_primary_nid),
2075 pbuf->pb_info.pi_nnis);
2079 /* always assume new data */
2080 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2081 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2084 * If there is data present that hasn't been processed yet,
2085 * we'll replace it if the Put contained newer data and it
2086 * fits. We're racing with a Ping or earlier Push in this
2089 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2090 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2091 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2092 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2093 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2094 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2095 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2096 libcfs_nid2str(lp->lp_primary_nid),
2097 LNET_PING_BUFFER_SEQNO(pbuf),
2098 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2104 * Allocate a buffer to copy the data. On a failure we drop
2105 * the Push and set FORCE_PING to force the discovery
2106 * thread to fix the problem by pinging the peer.
2108 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2110 lp->lp_state |= LNET_PEER_FORCE_PING;
2111 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2112 libcfs_nid2str(lp->lp_primary_nid),
2113 LNET_PING_BUFFER_SEQNO(pbuf));
2118 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2119 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2120 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2121 CDEBUG(D_NET, "Received Push %s %u\n",
2122 libcfs_nid2str(lp->lp_primary_nid),
2123 LNET_PING_BUFFER_SEQNO(pbuf));
2127 * Queue the peer for discovery if not done, force it on the request
2128 * queue and wake the discovery thread if the peer was already queued,
2129 * because its status changed.
2131 spin_unlock(&lp->lp_lock);
2132 lnet_net_lock(LNET_LOCK_EX);
2133 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2134 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2135 wake_up(&the_lnet.ln_dc_waitq);
2137 /* Drop refcount from lookup */
2138 lnet_peer_decref_locked(lp);
2139 lnet_net_unlock(LNET_LOCK_EX);
2143 * Clear the discovery error state, unless we're already discovering
2144 * this peer, in which case the error is current.
2146 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2148 spin_lock(&lp->lp_lock);
2149 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2150 lp->lp_dc_error = 0;
2151 spin_unlock(&lp->lp_lock);
2155 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2156 * dropped/retaken within this function. An lnet_peer_ni is passed in
2157 * because discovery could tear down an lnet_peer.
2160 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2163 struct lnet_peer *lp;
2168 lnet_net_unlock(cpt);
2169 lnet_net_lock(LNET_LOCK_EX);
2170 lp = lpni->lpni_peer_net->lpn_peer;
2171 lnet_peer_clear_discovery_error(lp);
2174 * We're willing to be interrupted. The lpni can become a
2175 * zombie if we race with DLC, so we must check for that.
2178 /* Keep lp alive when the lnet_net_lock is unlocked */
2179 lnet_peer_addref_locked(lp);
2180 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2181 if (signal_pending(current))
2183 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2186 * Don't repeat discovery if discovery is disabled. This is
2187 * done to ensure we can use discovery as a standard ping as
2188 * well for backwards compatibility with routers which do not
2189 * have discovery or have discovery disabled
2191 if (lnet_is_discovery_disabled(lp) && count > 0)
2193 if (lp->lp_dc_error)
2195 if (lnet_peer_is_uptodate(lp))
2197 lnet_peer_queue_for_discovery(lp);
2199 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2202 * If caller requested a non-blocking operation then
2203 * return immediately. Once discovery is complete any
2204 * pending messages that were stopped due to discovery
2205 * will be transmitted.
2210 lnet_net_unlock(LNET_LOCK_EX);
2212 finish_wait(&lp->lp_dc_waitq, &wait);
2213 lnet_net_lock(LNET_LOCK_EX);
2214 lnet_peer_decref_locked(lp);
2215 /* Peer may have changed */
2216 lp = lpni->lpni_peer_net->lpn_peer;
2218 finish_wait(&lp->lp_dc_waitq, &wait);
2220 lnet_net_unlock(LNET_LOCK_EX);
2222 lnet_peer_decref_locked(lp);
2224 * The peer may have changed, so re-check and rediscover if that turns
2225 * out to have been the case. The reference count on lp ensured that
2226 * even if it was unlinked from lpni the memory could not be recycled.
2227 * Thus the check below is sufficient to determine whether the peer
2228 * changed. If the peer changed, then lp must not be dereferenced.
2230 if (lp != lpni->lpni_peer_net->lpn_peer)
2233 if (signal_pending(current))
2235 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2237 else if (lp->lp_dc_error)
2238 rc = lp->lp_dc_error;
2240 CDEBUG(D_NET, "non-blocking discovery\n");
2241 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2244 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2245 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2246 libcfs_nid2str(lpni->lpni_nid), rc,
2247 (!block) ? "pending discovery" : "discovery complete");
2252 /* Handle an incoming ack for a push. */
2254 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2256 struct lnet_ping_buffer *pbuf;
2258 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2259 spin_lock(&lp->lp_lock);
2260 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2261 lp->lp_push_error = ev->status;
2263 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2265 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2266 spin_unlock(&lp->lp_lock);
2268 CDEBUG(D_NET, "peer %s ev->status %d\n",
2269 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2272 /* Handle a Reply message. This is the reply to a Ping message. */
2274 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2276 struct lnet_ping_buffer *pbuf;
2279 spin_lock(&lp->lp_lock);
2282 * If some kind of error happened the contents of message
2283 * cannot be used. Set PING_FAILED to trigger a retry.
2286 lp->lp_state |= LNET_PEER_PING_FAILED;
2287 lp->lp_ping_error = ev->status;
2288 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2290 libcfs_nid2str(lp->lp_primary_nid),
2291 libcfs_nid2str(ev->source.nid));
2295 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2296 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2297 lnet_swap_pinginfo(pbuf);
2300 * A reply with invalid or corrupted info. Set PING_FAILED to
2303 rc = lnet_ping_info_validate(&pbuf->pb_info);
2305 lp->lp_state |= LNET_PEER_PING_FAILED;
2306 lp->lp_ping_error = 0;
2307 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2308 libcfs_nid2str(lp->lp_primary_nid), rc);
2314 * Only enable the multi-rail feature on the peer if both sides of
2315 * the connection have discovery on
2317 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2318 CDEBUG(D_NET, "Peer %s has Multi-Rail feature enabled\n",
2319 libcfs_nid2str(lp->lp_primary_nid));
2320 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2322 CDEBUG(D_NET, "Peer %s has Multi-Rail feature disabled\n",
2323 libcfs_nid2str(lp->lp_primary_nid));
2324 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2328 * The peer may have discovery disabled at its end. Set
2329 * NO_DISCOVERY as appropriate.
2331 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2332 !lnet_peer_discovery_disabled) {
2333 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2334 libcfs_nid2str(lp->lp_primary_nid));
2335 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2337 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2338 libcfs_nid2str(lp->lp_primary_nid));
2339 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2343 * Update the MULTI_RAIL flag based on the reply. If the peer
2344 * was configured with DLC then the setting should match what
2347 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2348 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2349 /* Everything's fine */
2350 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2351 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2352 libcfs_nid2str(lp->lp_primary_nid));
2355 * if discovery is disabled then we don't want to
2356 * update the state of the peer. All we'll do is
2357 * update the peer_nis which were reported back in
2361 if (!lnet_is_discovery_disabled_locked(lp)) {
2362 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2363 lnet_peer_clr_non_mr_pref_nids(lp);
2366 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2367 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2368 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2369 libcfs_nid2str(lp->lp_primary_nid));
2371 CERROR("Multi-Rail state vanished from %s\n",
2372 libcfs_nid2str(lp->lp_primary_nid));
2373 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2378 * Make sure we'll allocate the correct size ping buffer when
2381 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2382 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2385 * Check for truncation of the Reply. Clear PING_SENT and set
2386 * PING_FAILED to trigger a retry.
2388 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2389 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2390 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2391 lp->lp_state |= LNET_PEER_PING_FAILED;
2392 lp->lp_ping_error = 0;
2393 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2394 libcfs_nid2str(lp->lp_primary_nid),
2395 pbuf->pb_info.pi_nnis);
2400 * Check the sequence numbers in the reply. These are only
2401 * available if the reply came from a Multi-Rail peer.
2403 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2404 pbuf->pb_info.pi_nnis > 1 &&
2405 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2406 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2407 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2408 libcfs_nid2str(lp->lp_primary_nid),
2409 LNET_PING_BUFFER_SEQNO(pbuf),
2412 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2415 /* We're happy with the state of the data in the buffer. */
2416 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2417 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2418 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2419 lnet_ping_buffer_decref(lp->lp_data);
2421 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2422 lnet_ping_buffer_addref(pbuf);
2425 lp->lp_state &= ~LNET_PEER_PING_SENT;
2426 spin_unlock(&lp->lp_lock);
2428 lnet_net_lock(LNET_LOCK_EX);
2430 * If this peer is a gateway, call the routing callback to
2431 * handle the ping reply
2433 if (lp->lp_rtr_refcount > 0)
2434 lnet_router_discovery_ping_reply(lp);
2435 lnet_net_unlock(LNET_LOCK_EX);
2439 * Send event handling. Only matters for error cases, where we clean
2440 * up state on the peer and peer_ni that would otherwise be updated in
2441 * the REPLY event handler for a successful Ping, and the ACK event
2442 * handler for a successful Push.
2445 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2452 spin_lock(&lp->lp_lock);
2453 if (ev->msg_type == LNET_MSG_GET) {
2454 lp->lp_state &= ~LNET_PEER_PING_SENT;
2455 lp->lp_state |= LNET_PEER_PING_FAILED;
2456 lp->lp_ping_error = ev->status;
2457 } else { /* ev->msg_type == LNET_MSG_PUT */
2458 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2459 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2460 lp->lp_push_error = ev->status;
2462 spin_unlock(&lp->lp_lock);
2463 rc = LNET_REDISCOVER_PEER;
2465 CDEBUG(D_NET, "%s Send to %s: %d\n",
2466 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2467 libcfs_nid2str(ev->target.nid), rc);
2472 * Unlink event handling. This event is only seen if a call to
2473 * LNetMDUnlink() caused the event to be unlinked. If this call was
2474 * made after the event was set up in LNetGet() or LNetPut() then we
2475 * assume the Ping or Push timed out.
2478 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2480 spin_lock(&lp->lp_lock);
2481 /* We've passed through LNetGet() */
2482 if (lp->lp_state & LNET_PEER_PING_SENT) {
2483 lp->lp_state &= ~LNET_PEER_PING_SENT;
2484 lp->lp_state |= LNET_PEER_PING_FAILED;
2485 lp->lp_ping_error = -ETIMEDOUT;
2486 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2487 libcfs_nid2str(lp->lp_primary_nid));
2489 /* We've passed through LNetPut() */
2490 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2491 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2492 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2493 lp->lp_push_error = -ETIMEDOUT;
2494 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2495 libcfs_nid2str(lp->lp_primary_nid));
2497 spin_unlock(&lp->lp_lock);
2501 * Event handler for the discovery EQ.
2503 * Called with lnet_res_lock(cpt) held. The cpt is the
2504 * lnet_cpt_of_cookie() of the md handle cookie.
2506 static void lnet_discovery_event_handler(struct lnet_event *event)
2508 struct lnet_peer *lp = event->md.user_ptr;
2509 struct lnet_ping_buffer *pbuf;
2512 /* discovery needs to take another look */
2513 rc = LNET_REDISCOVER_PEER;
2515 CDEBUG(D_NET, "Received event: %d\n", event->type);
2517 switch (event->type) {
2518 case LNET_EVENT_ACK:
2519 lnet_discovery_event_ack(lp, event);
2521 case LNET_EVENT_REPLY:
2522 lnet_discovery_event_reply(lp, event);
2524 case LNET_EVENT_SEND:
2525 /* Only send failure triggers a retry. */
2526 rc = lnet_discovery_event_send(lp, event);
2528 case LNET_EVENT_UNLINK:
2529 /* LNetMDUnlink() was called */
2530 lnet_discovery_event_unlink(lp, event);
2533 /* Invalid events. */
2536 lnet_net_lock(LNET_LOCK_EX);
2537 if (event->unlinked) {
2538 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2539 lnet_ping_buffer_decref(pbuf);
2540 lnet_peer_decref_locked(lp);
2543 /* put peer back at end of request queue, if discovery not already
2545 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2546 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2547 wake_up(&the_lnet.ln_dc_waitq);
2549 lnet_net_unlock(LNET_LOCK_EX);
2553 * Build a peer from incoming data.
2555 * The NIDs in the incoming data are supposed to be structured as follows:
2558 * - other NIDs in same net
2559 * - NIDs in second net
2560 * - NIDs in third net
2562 * This due to the way the list of NIDs in the data is created.
2564 * Note that this function will mark the peer uptodate unless an
2565 * ENOMEM is encontered. All other errors are due to a conflict
2566 * between the DLC configuration and what discovery sees. We treat DLC
2567 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2568 * peer from becoming stuck in discovery.
2570 static int lnet_peer_merge_data(struct lnet_peer *lp,
2571 struct lnet_ping_buffer *pbuf)
2573 struct lnet_peer_ni *lpni;
2574 lnet_nid_t *curnis = NULL;
2575 struct lnet_ni_status *addnis = NULL;
2576 lnet_nid_t *delnis = NULL;
2586 flags = LNET_PEER_DISCOVERED;
2587 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2588 flags |= LNET_PEER_MULTI_RAIL;
2591 * Cache the routing feature for the peer; whether it is enabled
2592 * for disabled as reported by the remote peer.
2594 spin_lock(&lp->lp_lock);
2595 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2596 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2598 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2599 spin_unlock(&lp->lp_lock);
2601 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2602 LIBCFS_ALLOC(curnis, nnis * sizeof(*curnis));
2603 LIBCFS_ALLOC(addnis, nnis * sizeof(*addnis));
2604 LIBCFS_ALLOC(delnis, nnis * sizeof(*delnis));
2605 if (!curnis || !addnis || !delnis) {
2613 /* Construct the list of NIDs present in peer. */
2615 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2616 curnis[ncurnis++] = lpni->lpni_nid;
2619 * Check for NIDs in pbuf not present in curnis[].
2620 * The loop starts at 1 to skip the loopback NID.
2622 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2623 for (j = 0; j < ncurnis; j++)
2624 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2627 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2630 * Check for NIDs in curnis[] not present in pbuf.
2631 * The nested loop starts at 1 to skip the loopback NID.
2633 * But never add the loopback NID to delnis[]: if it is
2634 * present in curnis[] then this peer is for this node.
2636 for (i = 0; i < ncurnis; i++) {
2637 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2639 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2640 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2642 * update the information we cache for the
2643 * peer with the latest information we
2646 lpni = lnet_find_peer_ni_locked(curnis[i]);
2648 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2649 lnet_peer_ni_decref_locked(lpni);
2654 if (j == pbuf->pb_info.pi_nnis)
2655 delnis[ndelnis++] = curnis[i];
2659 * If we get here and the discovery is disabled then we don't want
2660 * to add or delete any NIs. We just updated the ones we have some
2661 * information on, and call it a day
2664 if (lnet_is_discovery_disabled(lp))
2667 for (i = 0; i < naddnis; i++) {
2668 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2670 CERROR("Error adding NID %s to peer %s: %d\n",
2671 libcfs_nid2str(addnis[i].ns_nid),
2672 libcfs_nid2str(lp->lp_primary_nid), rc);
2676 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2678 lpni->lpni_ns_status = addnis[i].ns_status;
2679 lnet_peer_ni_decref_locked(lpni);
2683 for (i = 0; i < ndelnis; i++) {
2685 * for routers it's okay to delete the primary_nid because
2686 * the upper layers don't really rely on it. So if we're
2687 * being told that the router changed its primary_nid
2688 * then it's okay to delete it.
2690 if (lp->lp_rtr_refcount > 0)
2691 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2692 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2694 CERROR("Error deleting NID %s from peer %s: %d\n",
2695 libcfs_nid2str(delnis[i]),
2696 libcfs_nid2str(lp->lp_primary_nid), rc);
2702 * Errors other than -ENOMEM are due to peers having been
2703 * configured with DLC. Ignore these because DLC overrides
2708 LIBCFS_FREE(curnis, nnis * sizeof(*curnis));
2709 LIBCFS_FREE(addnis, nnis * sizeof(*addnis));
2710 LIBCFS_FREE(delnis, nnis * sizeof(*delnis));
2711 lnet_ping_buffer_decref(pbuf);
2712 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2715 spin_lock(&lp->lp_lock);
2716 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2717 lp->lp_state |= LNET_PEER_FORCE_PING;
2718 spin_unlock(&lp->lp_lock);
2724 * The data in pbuf says lp is its primary peer, but the data was
2725 * received by a different peer. Try to update lp with the data.
2728 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2730 struct lnet_handle_md mdh;
2732 /* Queue lp for discovery, and force it on the request queue. */
2733 lnet_net_lock(LNET_LOCK_EX);
2734 if (lnet_peer_queue_for_discovery(lp))
2735 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2736 lnet_net_unlock(LNET_LOCK_EX);
2738 LNetInvalidateMDHandle(&mdh);
2741 * Decide whether we can move the peer to the DATA_PRESENT state.
2743 * We replace stale data for a multi-rail peer, repair PING_FAILED
2744 * status, and preempt FORCE_PING.
2746 * If after that we have DATA_PRESENT, we merge it into this peer.
2748 spin_lock(&lp->lp_lock);
2749 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2750 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2751 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2752 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2753 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2754 lnet_ping_buffer_decref(pbuf);
2759 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2760 lnet_ping_buffer_decref(lp->lp_data);
2762 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2764 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2765 mdh = lp->lp_ping_mdh;
2766 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2767 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2768 lp->lp_ping_error = 0;
2770 if (lp->lp_state & LNET_PEER_FORCE_PING)
2771 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2772 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2773 spin_unlock(&lp->lp_lock);
2775 if (!LNetMDHandleIsInvalid(mdh))
2779 return lnet_peer_merge_data(lp, pbuf);
2781 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2785 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2789 for (i = 0; i < pinfo->pi_nnis; i++) {
2790 if (pinfo->pi_ni[i].ns_nid == nid)
2798 * Update a peer using the data received.
2800 static int lnet_peer_data_present(struct lnet_peer *lp)
2801 __must_hold(&lp->lp_lock)
2803 struct lnet_ping_buffer *pbuf;
2804 struct lnet_peer_ni *lpni;
2805 lnet_nid_t nid = LNET_NID_ANY;
2811 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2812 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2813 spin_unlock(&lp->lp_lock);
2816 * Modifications of peer structures are done while holding the
2817 * ln_api_mutex. A global lock is required because we may be
2818 * modifying multiple peer structures, and a mutex greatly
2819 * simplifies memory management.
2821 * The actual changes to the data structures must also protect
2822 * against concurrent lookups, for which the lnet_net_lock in
2823 * LNET_LOCK_EX mode is used.
2825 mutex_lock(&the_lnet.ln_api_mutex);
2826 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2832 * If this peer is not on the peer list then it is being torn
2833 * down, and our reference count may be all that is keeping it
2834 * alive. Don't do any work on it.
2836 if (list_empty(&lp->lp_peer_list))
2839 flags = LNET_PEER_DISCOVERED;
2840 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2841 flags |= LNET_PEER_MULTI_RAIL;
2844 * Check whether the primary NID in the message matches the
2845 * primary NID of the peer. If it does, update the peer, if
2846 * it it does not, check whether there is already a peer with
2847 * that primary NID. If no such peer exists, try to update
2848 * the primary NID of the current peer (allowed if it was
2849 * created due to message traffic) and complete the update.
2850 * If the peer did exist, hand off the data to it.
2852 * The peer for the loopback interface is a special case: this
2853 * is the peer for the local node, and we want to set its
2854 * primary NID to the correct value here. Moreover, this peer
2855 * can show up with only the loopback NID in the ping buffer.
2857 if (pbuf->pb_info.pi_nnis <= 1)
2859 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2860 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2861 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2863 rc = lnet_peer_merge_data(lp, pbuf);
2865 * if the primary nid of the peer is present in the ping info returned
2866 * from the peer, but it's not the local primary peer we have
2867 * cached and discovery is disabled, then we don't want to update
2868 * our local peer info, by adding or removing NIDs, we just want
2869 * to update the status of the nids that we currently have
2870 * recorded in that peer.
2872 } else if (lp->lp_primary_nid == nid ||
2873 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2874 lnet_is_discovery_disabled(lp))) {
2875 rc = lnet_peer_merge_data(lp, pbuf);
2877 lpni = lnet_find_peer_ni_locked(nid);
2879 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2881 CERROR("Primary NID error %s versus %s: %d\n",
2882 libcfs_nid2str(lp->lp_primary_nid),
2883 libcfs_nid2str(nid), rc);
2885 rc = lnet_peer_merge_data(lp, pbuf);
2888 struct lnet_peer *new_lp;
2889 new_lp = lpni->lpni_peer_net->lpn_peer;
2891 * if lp has discovery/MR enabled that means new_lp
2892 * should have discovery/MR enabled as well, since
2893 * it's the same peer, which we're about to merge
2895 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2896 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2897 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2898 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2900 rc = lnet_peer_set_primary_data(new_lp, pbuf);
2901 lnet_consolidate_routes_locked(lp, new_lp);
2902 lnet_peer_ni_decref_locked(lpni);
2906 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2908 mutex_unlock(&the_lnet.ln_api_mutex);
2910 spin_lock(&lp->lp_lock);
2911 /* Tell discovery to re-check the peer immediately. */
2913 rc = LNET_REDISCOVER_PEER;
2918 * A ping failed. Clear the PING_FAILED state and set the
2919 * FORCE_PING state, to ensure a retry even if discovery is
2920 * disabled. This avoids being left with incorrect state.
2922 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2923 __must_hold(&lp->lp_lock)
2925 struct lnet_handle_md mdh;
2928 mdh = lp->lp_ping_mdh;
2929 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2930 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2931 lp->lp_state |= LNET_PEER_FORCE_PING;
2932 rc = lp->lp_ping_error;
2933 lp->lp_ping_error = 0;
2934 spin_unlock(&lp->lp_lock);
2936 if (!LNetMDHandleIsInvalid(mdh))
2939 CDEBUG(D_NET, "peer %s:%d\n",
2940 libcfs_nid2str(lp->lp_primary_nid), rc);
2942 spin_lock(&lp->lp_lock);
2943 return rc ? rc : LNET_REDISCOVER_PEER;
2947 * Select NID to send a Ping or Push to.
2949 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2951 struct lnet_peer_ni *lpni;
2953 /* Look for a direct-connected NID for this peer. */
2955 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2956 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2961 return lpni->lpni_nid;
2963 /* Look for a routed-connected NID for this peer. */
2965 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2966 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2971 return lpni->lpni_nid;
2973 return LNET_NID_ANY;
2976 /* Active side of ping. */
2977 static int lnet_peer_send_ping(struct lnet_peer *lp)
2978 __must_hold(&lp->lp_lock)
2985 lp->lp_state |= LNET_PEER_PING_SENT;
2986 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2987 spin_unlock(&lp->lp_lock);
2989 cpt = lnet_net_lock_current();
2990 /* Refcount for MD. */
2991 lnet_peer_addref_locked(lp);
2992 pnid = lnet_peer_select_nid(lp);
2993 lnet_net_unlock(cpt);
2995 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2997 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2998 the_lnet.ln_dc_eqh, false);
3001 * if LNetMDBind in lnet_send_ping fails we need to decrement the
3002 * refcount on the peer, otherwise LNetMDUnlink will be called
3003 * which will eventually do that.
3007 lnet_peer_decref_locked(lp);
3008 lnet_net_unlock(cpt);
3009 rc = -rc; /* change the rc to negative value */
3011 } else if (rc < 0) {
3015 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3017 spin_lock(&lp->lp_lock);
3021 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3023 * The errors that get us here are considered hard errors and
3024 * cause Discovery to terminate. So we clear PING_SENT, but do
3025 * not set either PING_FAILED or FORCE_PING. In fact we need
3026 * to clear PING_FAILED, because the unlink event handler will
3027 * have set it if we called LNetMDUnlink() above.
3029 spin_lock(&lp->lp_lock);
3030 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3035 * This function exists because you cannot call LNetMDUnlink() from an
3038 static int lnet_peer_push_failed(struct lnet_peer *lp)
3039 __must_hold(&lp->lp_lock)
3041 struct lnet_handle_md mdh;
3044 mdh = lp->lp_push_mdh;
3045 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3046 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3047 rc = lp->lp_push_error;
3048 lp->lp_push_error = 0;
3049 spin_unlock(&lp->lp_lock);
3051 if (!LNetMDHandleIsInvalid(mdh))
3054 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3055 spin_lock(&lp->lp_lock);
3056 return rc ? rc : LNET_REDISCOVER_PEER;
3059 /* Active side of push. */
3060 static int lnet_peer_send_push(struct lnet_peer *lp)
3061 __must_hold(&lp->lp_lock)
3063 struct lnet_ping_buffer *pbuf;
3064 struct lnet_process_id id;
3069 /* Don't push to a non-multi-rail peer. */
3070 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3071 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3075 lp->lp_state |= LNET_PEER_PUSH_SENT;
3076 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3077 spin_unlock(&lp->lp_lock);
3079 cpt = lnet_net_lock_current();
3080 pbuf = the_lnet.ln_ping_target;
3081 lnet_ping_buffer_addref(pbuf);
3082 lnet_net_unlock(cpt);
3084 /* Push source MD */
3085 md.start = &pbuf->pb_info;
3086 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3087 md.threshold = 2; /* Put/Ack */
3090 md.eq_handle = the_lnet.ln_dc_eqh;
3093 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
3095 lnet_ping_buffer_decref(pbuf);
3096 CERROR("Can't bind push source MD: %d\n", rc);
3099 cpt = lnet_net_lock_current();
3100 /* Refcount for MD. */
3101 lnet_peer_addref_locked(lp);
3102 id.pid = LNET_PID_LUSTRE;
3103 id.nid = lnet_peer_select_nid(lp);
3104 lnet_net_unlock(cpt);
3106 if (id.nid == LNET_NID_ANY) {
3111 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
3112 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3113 LNET_PROTO_PING_MATCHBITS, 0, 0);
3118 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3120 spin_lock(&lp->lp_lock);
3124 LNetMDUnlink(lp->lp_push_mdh);
3125 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3127 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3129 * The errors that get us here are considered hard errors and
3130 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3131 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3132 * because the unlink event handler will have set it if we
3133 * called LNetMDUnlink() above.
3135 spin_lock(&lp->lp_lock);
3136 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3141 * An unrecoverable error was encountered during discovery.
3142 * Set error status in peer and abort discovery.
3144 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3146 CDEBUG(D_NET, "Discovery error %s: %d\n",
3147 libcfs_nid2str(lp->lp_primary_nid), error);
3149 spin_lock(&lp->lp_lock);
3150 lp->lp_dc_error = error;
3151 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3152 lp->lp_state |= LNET_PEER_REDISCOVER;
3153 spin_unlock(&lp->lp_lock);
3157 * Mark the peer as discovered.
3159 static int lnet_peer_discovered(struct lnet_peer *lp)
3160 __must_hold(&lp->lp_lock)
3162 lp->lp_state |= LNET_PEER_DISCOVERED;
3163 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3164 LNET_PEER_REDISCOVER);
3166 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3173 * Discovering this peer is taking too long. Cancel any Ping or Push
3174 * that discovery is waiting on by unlinking the relevant MDs. The
3175 * lnet_discovery_event_handler() will proceed from here and complete
3178 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3180 struct lnet_handle_md ping_mdh;
3181 struct lnet_handle_md push_mdh;
3183 LNetInvalidateMDHandle(&ping_mdh);
3184 LNetInvalidateMDHandle(&push_mdh);
3186 spin_lock(&lp->lp_lock);
3187 if (lp->lp_state & LNET_PEER_PING_SENT) {
3188 ping_mdh = lp->lp_ping_mdh;
3189 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3191 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3192 push_mdh = lp->lp_push_mdh;
3193 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3195 spin_unlock(&lp->lp_lock);
3197 if (!LNetMDHandleIsInvalid(ping_mdh))
3198 LNetMDUnlink(ping_mdh);
3199 if (!LNetMDHandleIsInvalid(push_mdh))
3200 LNetMDUnlink(push_mdh);
3204 * Wait for work to be queued or some other change that must be
3205 * attended to. Returns non-zero if the discovery thread should shut
3208 static int lnet_peer_discovery_wait_for_work(void)
3215 cpt = lnet_net_lock_current();
3217 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3218 TASK_INTERRUPTIBLE);
3219 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3221 if (lnet_push_target_resize_needed())
3223 if (!list_empty(&the_lnet.ln_dc_request))
3225 if (!list_empty(&the_lnet.ln_msg_resend))
3227 lnet_net_unlock(cpt);
3230 * wakeup max every second to check if there are peers that
3231 * have been stuck on the working queue for greater than
3234 schedule_timeout(cfs_time_seconds(1));
3235 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3236 cpt = lnet_net_lock_current();
3238 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3240 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3243 lnet_net_unlock(cpt);
3245 CDEBUG(D_NET, "woken: %d\n", rc);
3251 * Messages that were pending on a destroyed peer will be put on a global
3252 * resend list. The message resend list will be checked by
3253 * the discovery thread when it wakes up, and will resend messages. These
3254 * messages can still be sendable in the case the lpni which was the initial
3255 * cause of the message re-queue was transfered to another peer.
3257 * It is possible that LNet could be shutdown while we're iterating
3258 * through the list. lnet_shudown_lndnets() will attempt to access the
3259 * resend list, but will have to wait until the spinlock is released, by
3260 * which time there shouldn't be any more messages on the resend list.
3261 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3262 * for the messages so they can be released. The other case is that
3263 * lnet_shudown_lndnets() can finalize all the messages before this
3264 * function can visit the resend list, in which case this function will be
3267 static void lnet_resend_msgs(void)
3269 struct lnet_msg *msg, *tmp;
3270 struct list_head resend;
3273 INIT_LIST_HEAD(&resend);
3275 spin_lock(&the_lnet.ln_msg_resend_lock);
3276 list_splice(&the_lnet.ln_msg_resend, &resend);
3277 spin_unlock(&the_lnet.ln_msg_resend_lock);
3279 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3280 list_del_init(&msg->msg_list);
3281 rc = lnet_send(msg->msg_src_nid_param, msg,
3282 msg->msg_rtr_nid_param);
3284 CNETERR("Error sending %s to %s: %d\n",
3285 lnet_msgtyp2str(msg->msg_type),
3286 libcfs_id2str(msg->msg_target), rc);
3287 lnet_finalize(msg, rc);
3292 /* The discovery thread. */
3293 static int lnet_peer_discovery(void *arg)
3295 struct lnet_peer *lp;
3298 wait_for_completion(&the_lnet.ln_started);
3300 CDEBUG(D_NET, "started\n");
3301 cfs_block_allsigs();
3304 if (lnet_peer_discovery_wait_for_work())
3309 if (lnet_push_target_resize_needed())
3310 lnet_push_target_resize();
3312 lnet_net_lock(LNET_LOCK_EX);
3313 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3317 * Process all incoming discovery work requests. When
3318 * discovery must wait on a peer to change state, it
3319 * is added to the tail of the ln_dc_working queue. A
3320 * timestamp keeps track of when the peer was added,
3321 * so we can time out discovery requests that take too
3324 while (!list_empty(&the_lnet.ln_dc_request)) {
3325 lp = list_first_entry(&the_lnet.ln_dc_request,
3326 struct lnet_peer, lp_dc_list);
3327 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3329 * set the time the peer was put on the dc_working
3330 * queue. It shouldn't remain on the queue
3331 * forever, in case the GET message (for ping)
3332 * doesn't get a REPLY or the PUT message (for
3333 * push) doesn't get an ACK.
3335 lp->lp_last_queued = ktime_get_real_seconds();
3336 lnet_net_unlock(LNET_LOCK_EX);
3339 * Select an action depending on the state of
3340 * the peer and whether discovery is disabled.
3341 * The check whether discovery is disabled is
3342 * done after the code that handles processing
3343 * for arrived data, cleanup for failures, and
3344 * forcing a Ping or Push.
3346 spin_lock(&lp->lp_lock);
3347 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3348 libcfs_nid2str(lp->lp_primary_nid), lp,
3350 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3351 rc = lnet_peer_data_present(lp);
3352 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3353 rc = lnet_peer_ping_failed(lp);
3354 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3355 rc = lnet_peer_push_failed(lp);
3356 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3357 rc = lnet_peer_send_ping(lp);
3358 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3359 rc = lnet_peer_send_push(lp);
3360 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3361 rc = lnet_peer_send_ping(lp);
3362 else if (lnet_peer_needs_push(lp))
3363 rc = lnet_peer_send_push(lp);
3365 rc = lnet_peer_discovered(lp);
3366 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3367 libcfs_nid2str(lp->lp_primary_nid), lp,
3369 spin_unlock(&lp->lp_lock);
3371 lnet_net_lock(LNET_LOCK_EX);
3372 if (rc == LNET_REDISCOVER_PEER) {
3373 list_move(&lp->lp_dc_list,
3374 &the_lnet.ln_dc_request);
3376 lnet_peer_discovery_error(lp, rc);
3378 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3379 lnet_peer_discovery_complete(lp);
3380 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3384 lnet_net_unlock(LNET_LOCK_EX);
3387 CDEBUG(D_NET, "stopping\n");
3389 * Clean up before telling lnet_peer_discovery_stop() that
3390 * we're done. Use wake_up() below to somewhat reduce the
3391 * size of the thundering herd if there are multiple threads
3392 * waiting on discovery of a single peer.
3395 /* Queue cleanup 1: stop all pending pings and pushes. */
3396 lnet_net_lock(LNET_LOCK_EX);
3397 while (!list_empty(&the_lnet.ln_dc_working)) {
3398 lp = list_first_entry(&the_lnet.ln_dc_working,
3399 struct lnet_peer, lp_dc_list);
3400 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3401 lnet_net_unlock(LNET_LOCK_EX);
3402 lnet_peer_cancel_discovery(lp);
3403 lnet_net_lock(LNET_LOCK_EX);
3405 lnet_net_unlock(LNET_LOCK_EX);
3407 /* Queue cleanup 2: wait for the expired queue to clear. */
3408 while (!list_empty(&the_lnet.ln_dc_expired))
3409 schedule_timeout(cfs_time_seconds(1));
3411 /* Queue cleanup 3: clear the request queue. */
3412 lnet_net_lock(LNET_LOCK_EX);
3413 while (!list_empty(&the_lnet.ln_dc_request)) {
3414 lp = list_first_entry(&the_lnet.ln_dc_request,
3415 struct lnet_peer, lp_dc_list);
3416 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3417 lnet_peer_discovery_complete(lp);
3419 lnet_net_unlock(LNET_LOCK_EX);
3421 LNetEQFree(the_lnet.ln_dc_eqh);
3422 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3424 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3425 wake_up(&the_lnet.ln_dc_waitq);
3427 CDEBUG(D_NET, "stopped\n");
3432 /* ln_api_mutex is held on entry. */
3433 int lnet_peer_discovery_start(void)
3435 struct task_struct *task;
3438 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3441 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3443 CERROR("Can't allocate discovery EQ: %d\n", rc);
3447 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3448 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3451 CERROR("Can't start peer discovery thread: %d\n", rc);
3453 LNetEQFree(the_lnet.ln_dc_eqh);
3454 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3456 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3459 CDEBUG(D_NET, "discovery start: %d\n", rc);
3464 /* ln_api_mutex is held on entry. */
3465 void lnet_peer_discovery_stop(void)
3467 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3470 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3471 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3473 /* In the LNetNIInit() path we may be stopping discovery before it
3474 * entered its work loop
3476 if (!completion_done(&the_lnet.ln_started))
3477 complete(&the_lnet.ln_started);
3479 wake_up(&the_lnet.ln_dc_waitq);
3481 wait_event(the_lnet.ln_dc_waitq,
3482 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3484 LASSERT(list_empty(&the_lnet.ln_dc_request));
3485 LASSERT(list_empty(&the_lnet.ln_dc_working));
3486 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3488 CDEBUG(D_NET, "discovery stopped\n");
3494 lnet_debug_peer(lnet_nid_t nid)
3496 char *aliveness = "NA";
3497 struct lnet_peer_ni *lp;
3500 cpt = lnet_cpt_of_nid(nid, NULL);
3503 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3505 lnet_net_unlock(cpt);
3506 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3510 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3511 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3513 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3514 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3515 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3516 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3517 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3519 lnet_peer_ni_decref_locked(lp);
3521 lnet_net_unlock(cpt);
3524 /* Gathering information for userspace. */
3526 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3527 char aliveness[LNET_MAX_STR_LEN],
3528 __u32 *cpt_iter, __u32 *refcount,
3529 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3530 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3531 __u32 *peer_tx_qnob)
3533 struct lnet_peer_table *peer_table;
3534 struct lnet_peer_ni *lp;
3539 /* get the number of CPTs */
3540 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3542 /* if the cpt number to be examined is >= the number of cpts in
3543 * the system then indicate that there are no more cpts to examin
3545 if (*cpt_iter >= lncpt)
3548 /* get the current table */
3549 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3550 /* if the ptable is NULL then there are no more cpts to examine */
3551 if (peer_table == NULL)
3554 lnet_net_lock(*cpt_iter);
3556 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3557 struct list_head *peers = &peer_table->pt_hash[j];
3559 list_for_each_entry(lp, peers, lpni_hashlist) {
3560 if (peer_index-- > 0)
3563 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3564 if (lnet_isrouter(lp) ||
3565 lnet_peer_aliveness_enabled(lp))
3566 snprintf(aliveness, LNET_MAX_STR_LEN,
3567 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3569 *nid = lp->lpni_nid;
3570 *refcount = atomic_read(&lp->lpni_refcount);
3571 *ni_peer_tx_credits =
3572 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3573 *peer_tx_credits = lp->lpni_txcredits;
3574 *peer_rtr_credits = lp->lpni_rtrcredits;
3575 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3576 *peer_tx_qnob = lp->lpni_txqnob;
3582 lnet_net_unlock(*cpt_iter);
3586 return found ? 0 : -ENOENT;
3589 /* ln_api_mutex is held, which keeps the peer list stable */
3590 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3592 struct lnet_ioctl_element_stats *lpni_stats;
3593 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3594 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3595 struct lnet_peer_ni_credit_info *lpni_info;
3596 struct lnet_peer_ni *lpni;
3597 struct lnet_peer *lp;
3602 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3609 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3610 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3611 size *= lp->lp_nnis;
3612 if (size > cfg->prcfg_size) {
3613 cfg->prcfg_size = size;
3618 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3619 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3620 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3621 cfg->prcfg_count = lp->lp_nnis;
3622 cfg->prcfg_size = size;
3623 cfg->prcfg_state = lp->lp_state;
3625 /* Allocate helper buffers. */
3627 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3630 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3633 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3634 if (!lpni_msg_stats)
3635 goto out_free_stats;
3636 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3638 goto out_free_msg_stats;
3643 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3644 nid = lpni->lpni_nid;
3645 if (copy_to_user(bulk, &nid, sizeof(nid)))
3646 goto out_free_hstats;
3647 bulk += sizeof(nid);
3649 memset(lpni_info, 0, sizeof(*lpni_info));
3650 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3651 if (lnet_isrouter(lpni) ||
3652 lnet_peer_aliveness_enabled(lpni))
3653 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3654 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3656 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3657 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3658 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3659 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3660 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3661 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3662 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3663 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3664 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3665 goto out_free_hstats;
3666 bulk += sizeof(*lpni_info);
3668 memset(lpni_stats, 0, sizeof(*lpni_stats));
3669 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3670 LNET_STATS_TYPE_SEND);
3671 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3672 LNET_STATS_TYPE_RECV);
3673 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3674 LNET_STATS_TYPE_DROP);
3675 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3676 goto out_free_hstats;
3677 bulk += sizeof(*lpni_stats);
3678 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3679 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3680 goto out_free_hstats;
3681 bulk += sizeof(*lpni_msg_stats);
3682 lpni_hstats->hlpni_network_timeout =
3683 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3684 lpni_hstats->hlpni_remote_dropped =
3685 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3686 lpni_hstats->hlpni_remote_timeout =
3687 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3688 lpni_hstats->hlpni_remote_error =
3689 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3690 lpni_hstats->hlpni_health_value =
3691 atomic_read(&lpni->lpni_healthv);
3692 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3693 goto out_free_hstats;
3694 bulk += sizeof(*lpni_hstats);
3699 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3701 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3703 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3705 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3707 lnet_peer_decref_locked(lp);
3713 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3715 /* the mt could've shutdown and cleaned up the queues */
3716 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3719 if (list_empty(&lpni->lpni_recovery) &&
3720 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3721 CERROR("lpni %s added to recovery queue. Health = %d\n",
3722 libcfs_nid2str(lpni->lpni_nid),
3723 atomic_read(&lpni->lpni_healthv));
3724 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3725 lnet_peer_ni_addref_locked(lpni);
3729 /* Call with the ln_api_mutex held */
3731 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3733 struct lnet_peer_table *ptable;
3734 struct lnet_peer *lp;
3735 struct lnet_peer_net *lpn;
3736 struct lnet_peer_ni *lpni;
3740 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3744 lnet_net_lock(LNET_LOCK_EX);
3745 lpni = lnet_find_peer_ni_locked(nid);
3747 lnet_net_unlock(LNET_LOCK_EX);
3750 atomic_set(&lpni->lpni_healthv, value);
3751 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3752 lnet_peer_ni_decref_locked(lpni);
3753 lnet_net_unlock(LNET_LOCK_EX);
3757 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3760 * Walk all the peers and reset the healhv for each one to the
3763 lnet_net_lock(LNET_LOCK_EX);
3764 for (cpt = 0; cpt < lncpt; cpt++) {
3765 ptable = the_lnet.ln_peer_tables[cpt];
3766 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3767 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3768 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3770 atomic_set(&lpni->lpni_healthv, value);
3771 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3776 lnet_net_unlock(LNET_LOCK_EX);