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;
262 * Turn off discovery for loopback peer. If you're creating a peer
263 * for the loopback interface then that was initiated when we
264 * attempted to send a message over the loopback. There is no need
265 * to ever use a different interface when sending messages to
268 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
269 lp->lp_state = LNET_PEER_NO_DISCOVERY;
270 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
272 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
278 lnet_destroy_peer_locked(struct lnet_peer *lp)
280 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
282 LASSERT(atomic_read(&lp->lp_refcount) == 0);
283 LASSERT(lp->lp_rtr_refcount == 0);
284 LASSERT(list_empty(&lp->lp_peer_nets));
285 LASSERT(list_empty(&lp->lp_peer_list));
286 LASSERT(list_empty(&lp->lp_dc_list));
289 lnet_ping_buffer_decref(lp->lp_data);
292 * if there are messages still on the pending queue, then make
293 * sure to queue them on the ln_msg_resend list so they can be
294 * resent at a later point if the discovery thread is still
296 * If the discovery thread has stopped, then the wakeup will be a
297 * no-op, and it is expected the lnet_shutdown_lndnets() will
298 * eventually be called, which will traverse this list and
299 * finalize the messages on the list.
300 * We can not resend them now because we're holding the cpt lock.
301 * Releasing the lock can cause an inconsistent state
303 spin_lock(&the_lnet.ln_msg_resend_lock);
304 spin_lock(&lp->lp_lock);
305 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
306 spin_unlock(&lp->lp_lock);
307 spin_unlock(&the_lnet.ln_msg_resend_lock);
308 wake_up(&the_lnet.ln_dc_waitq);
310 LIBCFS_FREE(lp, sizeof(*lp));
314 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
315 * that peer_net, detach the peer_net from the peer.
317 * Call with lnet_net_lock/EX held
320 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
322 struct lnet_peer_table *ptable;
323 struct lnet_peer_net *lpn;
324 struct lnet_peer *lp;
327 * Belts and suspenders: gracefully handle teardown of a
328 * partially connected peer_ni.
330 lpn = lpni->lpni_peer_net;
332 list_del_init(&lpni->lpni_peer_nis);
334 * If there are no lpni's left, we detach lpn from
335 * lp_peer_nets, so it cannot be found anymore.
337 if (list_empty(&lpn->lpn_peer_nis))
338 list_del_init(&lpn->lpn_peer_nets);
340 /* Update peer NID count. */
345 * If there are no more peer nets, make the peer unfindable
346 * via the peer_tables.
348 * Otherwise, if the peer is DISCOVERED, tell discovery to
349 * take another look at it. This is a no-op if discovery for
350 * this peer did the detaching.
352 if (list_empty(&lp->lp_peer_nets)) {
353 list_del_init(&lp->lp_peer_list);
354 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
356 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
357 /* Discovery isn't running, nothing to do here. */
358 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
359 lnet_peer_queue_for_discovery(lp);
360 wake_up(&the_lnet.ln_dc_waitq);
362 CDEBUG(D_NET, "peer %s NID %s\n",
363 libcfs_nid2str(lp->lp_primary_nid),
364 libcfs_nid2str(lpni->lpni_nid));
367 /* called with lnet_net_lock LNET_LOCK_EX held */
369 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
371 struct lnet_peer_table *ptable = NULL;
373 /* don't remove a peer_ni if it's also a gateway */
374 if (lnet_isrouter(lpni) && !force) {
375 CERROR("Peer NI %s is a gateway. Can not delete it\n",
376 libcfs_nid2str(lpni->lpni_nid));
380 lnet_peer_remove_from_remote_list(lpni);
382 /* remove peer ni from the hash list. */
383 list_del_init(&lpni->lpni_hashlist);
386 * indicate the peer is being deleted so the monitor thread can
387 * remove it from the recovery queue.
389 spin_lock(&lpni->lpni_lock);
390 lpni->lpni_state |= LNET_PEER_NI_DELETING;
391 spin_unlock(&lpni->lpni_lock);
393 /* decrement the ref count on the peer table */
394 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
395 LASSERT(ptable->pt_number > 0);
399 * The peer_ni can no longer be found with a lookup. But there
400 * can be current users, so keep track of it on the zombie
401 * list until the reference count has gone to zero.
403 * The last reference may be lost in a place where the
404 * lnet_net_lock locks only a single cpt, and that cpt may not
405 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
408 spin_lock(&ptable->pt_zombie_lock);
409 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
410 ptable->pt_zombies++;
411 spin_unlock(&ptable->pt_zombie_lock);
413 /* no need to keep this peer_ni on the hierarchy anymore */
414 lnet_peer_detach_peer_ni_locked(lpni);
416 /* remove hashlist reference on peer_ni */
417 lnet_peer_ni_decref_locked(lpni);
422 void lnet_peer_uninit(void)
424 struct lnet_peer_ni *lpni, *tmp;
426 lnet_net_lock(LNET_LOCK_EX);
428 /* remove all peer_nis from the remote peer and the hash list */
429 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
430 lpni_on_remote_peer_ni_list)
431 lnet_peer_ni_del_locked(lpni, false);
433 lnet_peer_tables_destroy();
435 lnet_net_unlock(LNET_LOCK_EX);
439 lnet_peer_del_locked(struct lnet_peer *peer)
441 struct lnet_peer_ni *lpni = NULL, *lpni2;
444 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
446 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
447 while (lpni != NULL) {
448 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
449 rc = lnet_peer_ni_del_locked(lpni, false);
459 lnet_peer_del(struct lnet_peer *peer)
461 lnet_net_lock(LNET_LOCK_EX);
462 lnet_peer_del_locked(peer);
463 lnet_net_unlock(LNET_LOCK_EX);
469 * Delete a NID from a peer. Call with ln_api_mutex held.
472 * -EPERM: Non-DLC deletion from DLC-configured peer.
473 * -ENOENT: No lnet_peer_ni corresponding to the nid.
474 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
475 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
478 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
480 struct lnet_peer_ni *lpni;
481 lnet_nid_t primary_nid = lp->lp_primary_nid;
483 bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
485 if (!(flags & LNET_PEER_CONFIGURED)) {
486 if (lp->lp_state & LNET_PEER_CONFIGURED) {
491 lpni = lnet_find_peer_ni_locked(nid);
496 lnet_peer_ni_decref_locked(lpni);
497 if (lp != lpni->lpni_peer_net->lpn_peer) {
503 * This function only allows deletion of the primary NID if it
506 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
511 lnet_net_lock(LNET_LOCK_EX);
513 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
514 struct lnet_peer_ni *lpni2;
515 /* assign the next peer_ni to be the primary */
516 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
518 lp->lp_primary_nid = lpni->lpni_nid;
520 rc = lnet_peer_ni_del_locked(lpni, force);
522 lnet_net_unlock(LNET_LOCK_EX);
525 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
526 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
532 lnet_peer_table_cleanup_locked(struct lnet_net *net,
533 struct lnet_peer_table *ptable)
536 struct lnet_peer_ni *next;
537 struct lnet_peer_ni *lpni;
538 struct lnet_peer *peer;
540 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
541 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
543 if (net != NULL && net != lpni->lpni_net)
546 peer = lpni->lpni_peer_net->lpn_peer;
547 if (peer->lp_primary_nid != lpni->lpni_nid) {
548 lnet_peer_ni_del_locked(lpni, false);
552 * Removing the primary NID implies removing
553 * the entire peer. Advance next beyond any
554 * peer_ni that belongs to the same peer.
556 list_for_each_entry_from(next, &ptable->pt_hash[i],
558 if (next->lpni_peer_net->lpn_peer != peer)
561 lnet_peer_del_locked(peer);
567 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
571 spin_lock(&ptable->pt_zombie_lock);
572 while (ptable->pt_zombies) {
573 spin_unlock(&ptable->pt_zombie_lock);
575 if (is_power_of_2(i)) {
577 "Waiting for %d zombies on peer table\n",
580 set_current_state(TASK_UNINTERRUPTIBLE);
581 schedule_timeout(cfs_time_seconds(1) >> 1);
582 spin_lock(&ptable->pt_zombie_lock);
584 spin_unlock(&ptable->pt_zombie_lock);
588 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
589 struct lnet_peer_table *ptable)
591 struct lnet_peer_ni *lp;
592 struct lnet_peer_ni *tmp;
596 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
597 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
599 if (net != lp->lpni_net)
602 if (!lnet_isrouter(lp))
605 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
607 lnet_net_unlock(LNET_LOCK_EX);
608 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
609 lnet_net_lock(LNET_LOCK_EX);
615 lnet_peer_tables_cleanup(struct lnet_net *net)
618 struct lnet_peer_table *ptable;
620 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
621 /* If just deleting the peers for a NI, get rid of any routes these
622 * peers are gateways for. */
623 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
624 lnet_net_lock(LNET_LOCK_EX);
625 lnet_peer_table_del_rtrs_locked(net, ptable);
626 lnet_net_unlock(LNET_LOCK_EX);
629 /* Start the cleanup process */
630 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
631 lnet_net_lock(LNET_LOCK_EX);
632 lnet_peer_table_cleanup_locked(net, ptable);
633 lnet_net_unlock(LNET_LOCK_EX);
636 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
637 lnet_peer_ni_finalize_wait(ptable);
640 static struct lnet_peer_ni *
641 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
643 struct list_head *peers;
644 struct lnet_peer_ni *lp;
646 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
648 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
649 list_for_each_entry(lp, peers, lpni_hashlist) {
650 if (lp->lpni_nid == nid) {
651 lnet_peer_ni_addref_locked(lp);
659 struct lnet_peer_ni *
660 lnet_find_peer_ni_locked(lnet_nid_t nid)
662 struct lnet_peer_ni *lpni;
663 struct lnet_peer_table *ptable;
666 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
668 ptable = the_lnet.ln_peer_tables[cpt];
669 lpni = lnet_get_peer_ni_locked(ptable, nid);
674 struct lnet_peer_ni *
675 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
677 struct lnet_peer_net *lpn;
678 struct lnet_peer_ni *lpni;
680 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
684 list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
685 if (lpni->lpni_nid == nid)
693 lnet_find_peer(lnet_nid_t nid)
695 struct lnet_peer_ni *lpni;
696 struct lnet_peer *lp = NULL;
699 cpt = lnet_net_lock_current();
700 lpni = lnet_find_peer_ni_locked(nid);
702 lp = lpni->lpni_peer_net->lpn_peer;
703 lnet_peer_addref_locked(lp);
704 lnet_peer_ni_decref_locked(lpni);
706 lnet_net_unlock(cpt);
711 struct lnet_peer_ni *
712 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
713 struct lnet_peer_net *peer_net,
714 struct lnet_peer_ni *prev)
716 struct lnet_peer_ni *lpni;
717 struct lnet_peer_net *net = peer_net;
721 if (list_empty(&peer->lp_peer_nets))
724 net = list_entry(peer->lp_peer_nets.next,
725 struct lnet_peer_net,
728 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
734 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
736 * if you reached the end of the peer ni list and the peer
737 * net is specified then there are no more peer nis in that
744 * we reached the end of this net ni list. move to the
747 if (prev->lpni_peer_net->lpn_peer_nets.next ==
749 /* no more nets and no more NIs. */
752 /* get the next net */
753 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
754 struct lnet_peer_net,
756 /* get the ni on it */
757 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
763 /* there are more nis left */
764 lpni = list_entry(prev->lpni_peer_nis.next,
765 struct lnet_peer_ni, lpni_peer_nis);
770 /* Call with the ln_api_mutex held */
771 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
773 struct lnet_process_id id;
774 struct lnet_peer_table *ptable;
775 struct lnet_peer *lp;
784 if (the_lnet.ln_state != LNET_STATE_RUNNING)
787 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
790 * Count the number of peers, and return E2BIG if the buffer
791 * is too small. We'll also return the desired size.
794 for (cpt = 0; cpt < lncpt; cpt++) {
795 ptable = the_lnet.ln_peer_tables[cpt];
796 count += ptable->pt_peers;
798 size = count * sizeof(*ids);
803 * Walk the peer lists and copy out the primary nids.
804 * This is safe because the peer lists are only modified
805 * while the ln_api_mutex is held. So we don't need to
806 * hold the lnet_net_lock as well, and can therefore
807 * directly call copy_to_user().
810 memset(&id, 0, sizeof(id));
811 id.pid = LNET_PID_LUSTRE;
813 for (cpt = 0; cpt < lncpt; cpt++) {
814 ptable = the_lnet.ln_peer_tables[cpt];
815 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
818 id.nid = lp->lp_primary_nid;
819 if (copy_to_user(&ids[i], &id, sizeof(id)))
832 * Start pushes to peers that need to be updated for a configuration
833 * change on this node.
836 lnet_push_update_to_peers(int force)
838 struct lnet_peer_table *ptable;
839 struct lnet_peer *lp;
843 lnet_net_lock(LNET_LOCK_EX);
844 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
845 for (cpt = 0; cpt < lncpt; cpt++) {
846 ptable = the_lnet.ln_peer_tables[cpt];
847 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
849 spin_lock(&lp->lp_lock);
850 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
851 lp->lp_state |= LNET_PEER_FORCE_PUSH;
852 spin_unlock(&lp->lp_lock);
854 if (lnet_peer_needs_push(lp))
855 lnet_peer_queue_for_discovery(lp);
858 lnet_net_unlock(LNET_LOCK_EX);
859 wake_up(&the_lnet.ln_dc_waitq);
863 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
864 * this is a preferred point-to-point path. Call with lnet_net_lock in
868 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
872 if (lpni->lpni_pref_nnids == 0)
874 if (lpni->lpni_pref_nnids == 1)
875 return lpni->lpni_pref.nid == nid;
876 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
877 if (lpni->lpni_pref.nids[i] == nid)
884 * Set a single ni as preferred, provided no preferred ni is already
885 * defined. Only to be used for non-multi-rail peer_ni.
888 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
892 spin_lock(&lpni->lpni_lock);
893 if (nid == LNET_NID_ANY) {
895 } else if (lpni->lpni_pref_nnids > 0) {
897 } else if (lpni->lpni_pref_nnids == 0) {
898 lpni->lpni_pref.nid = nid;
899 lpni->lpni_pref_nnids = 1;
900 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
902 spin_unlock(&lpni->lpni_lock);
904 CDEBUG(D_NET, "peer %s nid %s: %d\n",
905 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
910 * Clear the preferred NID from a non-multi-rail peer_ni, provided
911 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
914 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
918 spin_lock(&lpni->lpni_lock);
919 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
920 lpni->lpni_pref_nnids = 0;
921 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
922 } else if (lpni->lpni_pref_nnids == 0) {
927 spin_unlock(&lpni->lpni_lock);
929 CDEBUG(D_NET, "peer %s: %d\n",
930 libcfs_nid2str(lpni->lpni_nid), rc);
935 * Clear the preferred NIDs from a non-multi-rail peer.
938 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
940 struct lnet_peer_ni *lpni = NULL;
942 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
943 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
947 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
949 lnet_nid_t *nids = NULL;
950 lnet_nid_t *oldnids = NULL;
951 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
956 if (nid == LNET_NID_ANY) {
961 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
966 /* A non-MR node may have only one preferred NI per peer_ni */
967 if (lpni->lpni_pref_nnids > 0) {
968 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
974 if (lpni->lpni_pref_nnids != 0) {
975 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
976 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
981 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
982 if (lpni->lpni_pref.nids[i] == nid) {
983 LIBCFS_FREE(nids, size);
987 nids[i] = lpni->lpni_pref.nids[i];
992 lnet_net_lock(LNET_LOCK_EX);
993 spin_lock(&lpni->lpni_lock);
994 if (lpni->lpni_pref_nnids == 0) {
995 lpni->lpni_pref.nid = nid;
997 oldnids = lpni->lpni_pref.nids;
998 lpni->lpni_pref.nids = nids;
1000 lpni->lpni_pref_nnids++;
1001 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1002 spin_unlock(&lpni->lpni_lock);
1003 lnet_net_unlock(LNET_LOCK_EX);
1006 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1007 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1010 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1011 spin_lock(&lpni->lpni_lock);
1012 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1013 spin_unlock(&lpni->lpni_lock);
1015 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1016 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1021 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1023 lnet_nid_t *nids = NULL;
1024 lnet_nid_t *oldnids = NULL;
1025 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1030 if (lpni->lpni_pref_nnids == 0) {
1035 if (lpni->lpni_pref_nnids == 1) {
1036 if (lpni->lpni_pref.nid != nid) {
1040 } else if (lpni->lpni_pref_nnids == 2) {
1041 if (lpni->lpni_pref.nids[0] != nid &&
1042 lpni->lpni_pref.nids[1] != nid) {
1047 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1048 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1053 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1054 if (lpni->lpni_pref.nids[i] != nid)
1056 nids[j++] = lpni->lpni_pref.nids[i];
1058 /* Check if we actually removed a nid. */
1059 if (j == lpni->lpni_pref_nnids) {
1060 LIBCFS_FREE(nids, size);
1066 lnet_net_lock(LNET_LOCK_EX);
1067 spin_lock(&lpni->lpni_lock);
1068 if (lpni->lpni_pref_nnids == 1) {
1069 lpni->lpni_pref.nid = LNET_NID_ANY;
1070 } else if (lpni->lpni_pref_nnids == 2) {
1071 oldnids = lpni->lpni_pref.nids;
1072 if (oldnids[0] == nid)
1073 lpni->lpni_pref.nid = oldnids[1];
1075 lpni->lpni_pref.nid = oldnids[2];
1077 oldnids = lpni->lpni_pref.nids;
1078 lpni->lpni_pref.nids = nids;
1080 lpni->lpni_pref_nnids--;
1081 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1082 spin_unlock(&lpni->lpni_lock);
1083 lnet_net_unlock(LNET_LOCK_EX);
1086 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1087 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1090 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1091 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1096 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1098 struct lnet_peer_ni *lpni;
1099 lnet_nid_t primary_nid = nid;
1101 lpni = lnet_find_peer_ni_locked(nid);
1103 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1104 lnet_peer_ni_decref_locked(lpni);
1111 LNetPrimaryNID(lnet_nid_t nid)
1113 struct lnet_peer *lp;
1114 struct lnet_peer_ni *lpni;
1115 lnet_nid_t primary_nid = nid;
1119 cpt = lnet_net_lock_current();
1120 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1125 lp = lpni->lpni_peer_net->lpn_peer;
1126 while (!lnet_peer_is_uptodate(lp)) {
1127 rc = lnet_discover_peer_locked(lpni, cpt, true);
1130 lp = lpni->lpni_peer_net->lpn_peer;
1132 primary_nid = lp->lp_primary_nid;
1134 lnet_peer_ni_decref_locked(lpni);
1136 lnet_net_unlock(cpt);
1138 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1139 libcfs_nid2str(primary_nid), rc);
1142 EXPORT_SYMBOL(LNetPrimaryNID);
1144 struct lnet_peer_net *
1145 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1147 struct lnet_peer_net *peer_net;
1148 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1149 if (peer_net->lpn_net_id == net_id)
1156 * Attach a peer_ni to a peer_net and peer. This function assumes
1157 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1158 * may be attached to a different peer, in which case it will be
1159 * properly detached first. The whole operation is done atomically.
1161 * Always returns 0. This is the last function called from functions
1162 * that do return an int, so returning 0 here allows the compiler to
1166 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1167 struct lnet_peer_net *lpn,
1168 struct lnet_peer_ni *lpni,
1171 struct lnet_peer_table *ptable;
1173 /* Install the new peer_ni */
1174 lnet_net_lock(LNET_LOCK_EX);
1175 /* Add peer_ni to global peer table hash, if necessary. */
1176 if (list_empty(&lpni->lpni_hashlist)) {
1177 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1179 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1180 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1181 ptable->pt_version++;
1182 ptable->pt_number++;
1183 /* This is the 1st refcount on lpni. */
1184 atomic_inc(&lpni->lpni_refcount);
1187 /* Detach the peer_ni from an existing peer, if necessary. */
1188 if (lpni->lpni_peer_net) {
1189 LASSERT(lpni->lpni_peer_net != lpn);
1190 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1191 lnet_peer_detach_peer_ni_locked(lpni);
1192 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1193 lpni->lpni_peer_net = NULL;
1196 /* Add peer_ni to peer_net */
1197 lpni->lpni_peer_net = lpn;
1198 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1199 lnet_peer_net_addref_locked(lpn);
1201 /* Add peer_net to peer */
1202 if (!lpn->lpn_peer) {
1204 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1205 lnet_peer_addref_locked(lp);
1208 /* Add peer to global peer list, if necessary */
1209 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1210 if (list_empty(&lp->lp_peer_list)) {
1211 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1216 /* Update peer state */
1217 spin_lock(&lp->lp_lock);
1218 if (flags & LNET_PEER_CONFIGURED) {
1219 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1220 lp->lp_state |= LNET_PEER_CONFIGURED;
1222 if (flags & LNET_PEER_MULTI_RAIL) {
1223 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1224 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1225 lnet_peer_clr_non_mr_pref_nids(lp);
1228 spin_unlock(&lp->lp_lock);
1231 lnet_net_unlock(LNET_LOCK_EX);
1233 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1234 libcfs_nid2str(lp->lp_primary_nid),
1235 libcfs_nid2str(lpni->lpni_nid), flags);
1241 * Create a new peer, with nid as its primary nid.
1243 * Call with the lnet_api_mutex held.
1246 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1248 struct lnet_peer *lp;
1249 struct lnet_peer_net *lpn;
1250 struct lnet_peer_ni *lpni;
1253 LASSERT(nid != LNET_NID_ANY);
1256 * No need for the lnet_net_lock here, because the
1257 * lnet_api_mutex is held.
1259 lpni = lnet_find_peer_ni_locked(nid);
1261 /* A peer with this NID already exists. */
1262 lp = lpni->lpni_peer_net->lpn_peer;
1263 lnet_peer_ni_decref_locked(lpni);
1265 * This is an error if the peer was configured and the
1266 * primary NID differs or an attempt is made to change
1267 * the Multi-Rail flag. Otherwise the assumption is
1268 * that an existing peer is being modified.
1270 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1271 if (lp->lp_primary_nid != nid)
1273 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1277 /* Delete and recreate as a configured peer. */
1281 /* Create peer, peer_net, and peer_ni. */
1283 lp = lnet_peer_alloc(nid);
1286 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1289 lpni = lnet_peer_ni_alloc(nid);
1293 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1296 LIBCFS_FREE(lpn, sizeof(*lpn));
1298 LIBCFS_FREE(lp, sizeof(*lp));
1300 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1301 libcfs_nid2str(nid), flags, rc);
1306 * Add a NID to a peer. Call with ln_api_mutex held.
1309 * -EPERM: Non-DLC addition to a DLC-configured peer.
1310 * -EEXIST: The NID was configured by DLC for a different peer.
1311 * -ENOMEM: Out of memory.
1312 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1313 * non-multi-rail peer.
1316 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1318 struct lnet_peer_net *lpn;
1319 struct lnet_peer_ni *lpni;
1323 LASSERT(nid != LNET_NID_ANY);
1325 /* A configured peer can only be updated through configuration. */
1326 if (!(flags & LNET_PEER_CONFIGURED)) {
1327 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1334 * The MULTI_RAIL flag can be set but not cleared, because
1335 * that would leave the peer struct in an invalid state.
1337 if (flags & LNET_PEER_MULTI_RAIL) {
1338 spin_lock(&lp->lp_lock);
1339 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1340 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1341 lnet_peer_clr_non_mr_pref_nids(lp);
1343 spin_unlock(&lp->lp_lock);
1344 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1349 lpni = lnet_find_peer_ni_locked(nid);
1352 * A peer_ni already exists. This is only a problem if
1353 * it is not connected to this peer and was configured
1356 lnet_peer_ni_decref_locked(lpni);
1357 if (lpni->lpni_peer_net->lpn_peer == lp)
1359 if (lnet_peer_ni_is_configured(lpni)) {
1363 /* If this is the primary NID, destroy the peer. */
1364 if (lnet_peer_ni_is_primary(lpni)) {
1365 struct lnet_peer *rtr_lp =
1366 lpni->lpni_peer_net->lpn_peer;
1367 int rtr_refcount = rtr_lp->lp_rtr_refcount;
1369 * if we're trying to delete a router it means
1370 * we're moving this peer NI to a new peer so must
1371 * transfer router properties to the new peer
1373 if (rtr_refcount > 0) {
1374 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1375 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1377 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1378 lpni = lnet_peer_ni_alloc(nid);
1385 lpni = lnet_peer_ni_alloc(nid);
1393 * Get the peer_net. Check that we're not adding a second
1394 * peer_ni on a peer_net of a non-multi-rail peer.
1396 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1398 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1403 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1408 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1411 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1412 if (!lpni->lpni_peer_net)
1413 LIBCFS_FREE(lpni, sizeof(*lpni));
1415 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1416 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1422 * Update the primary NID of a peer, if possible.
1424 * Call with the lnet_api_mutex held.
1427 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1429 lnet_nid_t old = lp->lp_primary_nid;
1432 if (lp->lp_primary_nid == nid)
1434 rc = lnet_peer_add_nid(lp, nid, flags);
1437 lp->lp_primary_nid = nid;
1439 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1440 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1445 * lpni creation initiated due to traffic either sending or receiving.
1448 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1450 struct lnet_peer *lp;
1451 struct lnet_peer_net *lpn;
1452 struct lnet_peer_ni *lpni;
1454 * Assume peer is Multi-Rail capable and let discovery find out
1457 unsigned flags = LNET_PEER_MULTI_RAIL;
1460 if (nid == LNET_NID_ANY) {
1465 /* lnet_net_lock is not needed here because ln_api_lock is held */
1466 lpni = lnet_find_peer_ni_locked(nid);
1469 * We must have raced with another thread. Since we
1470 * know next to nothing about a peer_ni created by
1471 * traffic, we just assume everything is ok and
1474 lnet_peer_ni_decref_locked(lpni);
1478 /* Create peer, peer_net, and peer_ni. */
1480 lp = lnet_peer_alloc(nid);
1483 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1486 lpni = lnet_peer_ni_alloc(nid);
1489 if (pref != LNET_NID_ANY)
1490 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1492 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1495 LIBCFS_FREE(lpn, sizeof(*lpn));
1497 LIBCFS_FREE(lp, sizeof(*lp));
1499 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1504 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1506 * This API handles the following combinations:
1507 * Create a peer with its primary NI if only the prim_nid is provided
1508 * Add a NID to a peer identified by the prim_nid. The peer identified
1509 * by the prim_nid must already exist.
1510 * The peer being created may be non-MR.
1512 * The caller must hold ln_api_mutex. This prevents the peer from
1513 * being created/modified/deleted by a different thread.
1516 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1518 struct lnet_peer *lp = NULL;
1519 struct lnet_peer_ni *lpni;
1522 /* The prim_nid must always be specified */
1523 if (prim_nid == LNET_NID_ANY)
1526 flags = LNET_PEER_CONFIGURED;
1528 flags |= LNET_PEER_MULTI_RAIL;
1531 * If nid isn't specified, we must create a new peer with
1532 * prim_nid as its primary nid.
1534 if (nid == LNET_NID_ANY)
1535 return lnet_peer_add(prim_nid, flags);
1537 /* Look up the prim_nid, which must exist. */
1538 lpni = lnet_find_peer_ni_locked(prim_nid);
1541 lnet_peer_ni_decref_locked(lpni);
1542 lp = lpni->lpni_peer_net->lpn_peer;
1544 /* Peer must have been configured. */
1545 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1546 CDEBUG(D_NET, "peer %s was not configured\n",
1547 libcfs_nid2str(prim_nid));
1551 /* Primary NID must match */
1552 if (lp->lp_primary_nid != prim_nid) {
1553 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1554 libcfs_nid2str(prim_nid),
1555 libcfs_nid2str(lp->lp_primary_nid));
1559 /* Multi-Rail flag must match. */
1560 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1561 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1562 libcfs_nid2str(prim_nid));
1566 return lnet_peer_add_nid(lp, nid, flags);
1570 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1572 * This API handles the following combinations:
1573 * Delete a NI from a peer if both prim_nid and nid are provided.
1574 * Delete a peer if only prim_nid is provided.
1575 * Delete a peer if its primary nid is provided.
1577 * The caller must hold ln_api_mutex. This prevents the peer from
1578 * being modified/deleted by a different thread.
1581 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1583 struct lnet_peer *lp;
1584 struct lnet_peer_ni *lpni;
1587 if (prim_nid == LNET_NID_ANY)
1590 lpni = lnet_find_peer_ni_locked(prim_nid);
1593 lnet_peer_ni_decref_locked(lpni);
1594 lp = lpni->lpni_peer_net->lpn_peer;
1596 if (prim_nid != lp->lp_primary_nid) {
1597 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1598 libcfs_nid2str(prim_nid),
1599 libcfs_nid2str(lp->lp_primary_nid));
1603 lnet_net_lock(LNET_LOCK_EX);
1604 if (lp->lp_rtr_refcount > 0) {
1605 lnet_net_unlock(LNET_LOCK_EX);
1606 CERROR("%s is a router. Can not be deleted\n",
1607 libcfs_nid2str(prim_nid));
1610 lnet_net_unlock(LNET_LOCK_EX);
1612 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1613 return lnet_peer_del(lp);
1615 flags = LNET_PEER_CONFIGURED;
1616 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1617 flags |= LNET_PEER_MULTI_RAIL;
1619 return lnet_peer_del_nid(lp, nid, flags);
1623 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1625 struct lnet_peer_table *ptable;
1626 struct lnet_peer_net *lpn;
1628 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1630 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1631 LASSERT(list_empty(&lpni->lpni_txq));
1632 LASSERT(lpni->lpni_txqnob == 0);
1633 LASSERT(list_empty(&lpni->lpni_peer_nis));
1634 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1636 lpn = lpni->lpni_peer_net;
1637 lpni->lpni_peer_net = NULL;
1638 lpni->lpni_net = NULL;
1640 /* remove the peer ni from the zombie list */
1641 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1642 spin_lock(&ptable->pt_zombie_lock);
1643 list_del_init(&lpni->lpni_hashlist);
1644 ptable->pt_zombies--;
1645 spin_unlock(&ptable->pt_zombie_lock);
1647 if (lpni->lpni_pref_nnids > 1) {
1648 LIBCFS_FREE(lpni->lpni_pref.nids,
1649 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1651 LIBCFS_FREE(lpni, sizeof(*lpni));
1653 lnet_peer_net_decref_locked(lpn);
1656 struct lnet_peer_ni *
1657 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1659 struct lnet_peer_ni *lpni = NULL;
1662 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1663 return ERR_PTR(-ESHUTDOWN);
1666 * find if a peer_ni already exists.
1667 * If so then just return that.
1669 lpni = lnet_find_peer_ni_locked(nid);
1673 lnet_net_unlock(cpt);
1675 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1678 goto out_net_relock;
1681 lpni = lnet_find_peer_ni_locked(nid);
1691 * Get a peer_ni for the given nid, create it if necessary. Takes a
1692 * hold on the peer_ni.
1694 struct lnet_peer_ni *
1695 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1697 struct lnet_peer_ni *lpni = NULL;
1700 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1701 return ERR_PTR(-ESHUTDOWN);
1704 * find if a peer_ni already exists.
1705 * If so then just return that.
1707 lpni = lnet_find_peer_ni_locked(nid);
1713 * use the lnet_api_mutex to serialize the creation of the peer_ni
1714 * and the creation/deletion of the local ni/net. When a local ni is
1715 * created, if there exists a set of peer_nis on that network,
1716 * they need to be traversed and updated. When a local NI is
1717 * deleted, which could result in a network being deleted, then
1718 * all peer nis on that network need to be removed as well.
1720 * Creation through traffic should also be serialized with
1721 * creation through DLC.
1723 lnet_net_unlock(cpt);
1724 mutex_lock(&the_lnet.ln_api_mutex);
1726 * Shutdown is only set under the ln_api_lock, so a single
1727 * check here is sufficent.
1729 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1730 lpni = ERR_PTR(-ESHUTDOWN);
1731 goto out_mutex_unlock;
1734 rc = lnet_peer_ni_traffic_add(nid, pref);
1737 goto out_mutex_unlock;
1740 lpni = lnet_find_peer_ni_locked(nid);
1744 mutex_unlock(&the_lnet.ln_api_mutex);
1747 /* Lock has been dropped, check again for shutdown. */
1748 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1750 lnet_peer_ni_decref_locked(lpni);
1751 lpni = ERR_PTR(-ESHUTDOWN);
1758 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1760 if (lnet_peer_discovery_disabled)
1763 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1764 (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1775 lnet_is_discovery_disabled(struct lnet_peer *lp)
1779 spin_lock(&lp->lp_lock);
1780 rc = lnet_is_discovery_disabled_locked(lp);
1781 spin_unlock(&lp->lp_lock);
1787 lnet_peer_gw_discovery(struct lnet_peer *lp)
1791 spin_lock(&lp->lp_lock);
1792 if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1794 spin_unlock(&lp->lp_lock);
1800 * Is a peer uptodate from the point of view of discovery?
1802 * If it is currently being processed, obviously not.
1803 * A forced Ping or Push is also handled by the discovery thread.
1805 * Otherwise look at whether the peer needs rediscovering.
1808 lnet_peer_is_uptodate(struct lnet_peer *lp)
1812 spin_lock(&lp->lp_lock);
1813 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1814 LNET_PEER_FORCE_PING |
1815 LNET_PEER_FORCE_PUSH)) {
1817 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1819 } else if (lnet_peer_needs_push(lp)) {
1821 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1822 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1829 spin_unlock(&lp->lp_lock);
1835 * Queue a peer for the attention of the discovery thread. Call with
1836 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1837 * -EALREADY if the peer was already queued.
1839 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1843 spin_lock(&lp->lp_lock);
1844 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1845 lp->lp_state |= LNET_PEER_DISCOVERING;
1846 spin_unlock(&lp->lp_lock);
1847 if (list_empty(&lp->lp_dc_list)) {
1848 lnet_peer_addref_locked(lp);
1849 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1850 wake_up(&the_lnet.ln_dc_waitq);
1856 CDEBUG(D_NET, "Queue peer %s: %d\n",
1857 libcfs_nid2str(lp->lp_primary_nid), rc);
1863 * Discovery of a peer is complete. Wake all waiters on the peer.
1864 * Call with lnet_net_lock/EX held.
1866 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1868 struct lnet_msg *msg, *tmp;
1870 struct list_head pending_msgs;
1872 INIT_LIST_HEAD(&pending_msgs);
1874 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1875 libcfs_nid2str(lp->lp_primary_nid));
1877 list_del_init(&lp->lp_dc_list);
1878 spin_lock(&lp->lp_lock);
1879 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1880 spin_unlock(&lp->lp_lock);
1881 wake_up_all(&lp->lp_dc_waitq);
1883 if (lp->lp_rtr_refcount > 0)
1884 lnet_router_discovery_complete(lp);
1886 lnet_net_unlock(LNET_LOCK_EX);
1888 /* iterate through all pending messages and send them again */
1889 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1890 list_del_init(&msg->msg_list);
1891 if (lp->lp_dc_error) {
1892 lnet_finalize(msg, lp->lp_dc_error);
1896 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1897 lnet_msgtyp2str(msg->msg_type),
1898 libcfs_id2str(msg->msg_target));
1899 rc = lnet_send(msg->msg_src_nid_param, msg,
1900 msg->msg_rtr_nid_param);
1902 CNETERR("Error sending %s to %s: %d\n",
1903 lnet_msgtyp2str(msg->msg_type),
1904 libcfs_id2str(msg->msg_target), rc);
1905 lnet_finalize(msg, rc);
1908 lnet_net_lock(LNET_LOCK_EX);
1909 lnet_peer_decref_locked(lp);
1913 * Handle inbound push.
1914 * Like any event handler, called with lnet_res_lock/CPT held.
1916 void lnet_peer_push_event(struct lnet_event *ev)
1918 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1919 struct lnet_peer *lp;
1921 /* lnet_find_peer() adds a refcount */
1922 lp = lnet_find_peer(ev->source.nid);
1924 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1925 libcfs_nid2str(ev->initiator.nid),
1926 libcfs_nid2str(ev->source.nid));
1930 /* Ensure peer state remains consistent while we modify it. */
1931 spin_lock(&lp->lp_lock);
1934 * If some kind of error happened the contents of the message
1935 * cannot be used. Clear the NIDS_UPTODATE and set the
1936 * FORCE_PING flag to trigger a ping.
1939 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1940 lp->lp_state |= LNET_PEER_FORCE_PING;
1941 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1943 libcfs_nid2str(lp->lp_primary_nid),
1944 libcfs_nid2str(ev->source.nid));
1949 * A push with invalid or corrupted info. Clear the UPTODATE
1950 * flag to trigger a ping.
1952 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1953 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1954 lp->lp_state |= LNET_PEER_FORCE_PING;
1955 CDEBUG(D_NET, "Corrupted Push from %s\n",
1956 libcfs_nid2str(lp->lp_primary_nid));
1961 * Make sure we'll allocate the correct size ping buffer when
1964 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1965 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1968 * A non-Multi-Rail peer is not supposed to be capable of
1971 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1972 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1973 libcfs_nid2str(lp->lp_primary_nid));
1978 * Check the MULTIRAIL flag. Complain if the peer was DLC
1979 * configured without it.
1981 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1982 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1983 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1984 libcfs_nid2str(lp->lp_primary_nid));
1986 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1987 lnet_peer_clr_non_mr_pref_nids(lp);
1992 * The peer may have discovery disabled at its end. Set
1993 * NO_DISCOVERY as appropriate.
1995 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1996 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1997 libcfs_nid2str(lp->lp_primary_nid));
1998 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1999 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2000 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2001 libcfs_nid2str(lp->lp_primary_nid));
2002 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2006 * Check for truncation of the Put message. Clear the
2007 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2008 * and tell discovery to allocate a bigger buffer.
2010 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2011 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2012 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2013 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2014 lp->lp_state |= LNET_PEER_FORCE_PING;
2015 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2016 libcfs_nid2str(lp->lp_primary_nid),
2017 pbuf->pb_info.pi_nnis);
2021 /* always assume new data */
2022 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2023 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2026 * If there is data present that hasn't been processed yet,
2027 * we'll replace it if the Put contained newer data and it
2028 * fits. We're racing with a Ping or earlier Push in this
2031 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2032 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2033 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2034 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2035 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2036 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2037 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2038 libcfs_nid2str(lp->lp_primary_nid),
2039 LNET_PING_BUFFER_SEQNO(pbuf),
2040 LNET_PING_BUFFER_SEQNO(lp->lp_data));
2046 * Allocate a buffer to copy the data. On a failure we drop
2047 * the Push and set FORCE_PING to force the discovery
2048 * thread to fix the problem by pinging the peer.
2050 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2052 lp->lp_state |= LNET_PEER_FORCE_PING;
2053 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2054 libcfs_nid2str(lp->lp_primary_nid),
2055 LNET_PING_BUFFER_SEQNO(pbuf));
2060 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2061 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2062 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2063 CDEBUG(D_NET, "Received Push %s %u\n",
2064 libcfs_nid2str(lp->lp_primary_nid),
2065 LNET_PING_BUFFER_SEQNO(pbuf));
2069 * Queue the peer for discovery if not done, force it on the request
2070 * queue and wake the discovery thread if the peer was already queued,
2071 * because its status changed.
2073 spin_unlock(&lp->lp_lock);
2074 lnet_net_lock(LNET_LOCK_EX);
2075 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2076 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2077 wake_up(&the_lnet.ln_dc_waitq);
2079 /* Drop refcount from lookup */
2080 lnet_peer_decref_locked(lp);
2081 lnet_net_unlock(LNET_LOCK_EX);
2085 * Clear the discovery error state, unless we're already discovering
2086 * this peer, in which case the error is current.
2088 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2090 spin_lock(&lp->lp_lock);
2091 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2092 lp->lp_dc_error = 0;
2093 spin_unlock(&lp->lp_lock);
2097 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2098 * dropped/retaken within this function. An lnet_peer_ni is passed in
2099 * because discovery could tear down an lnet_peer.
2102 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2105 struct lnet_peer *lp;
2109 lnet_net_unlock(cpt);
2110 lnet_net_lock(LNET_LOCK_EX);
2111 lp = lpni->lpni_peer_net->lpn_peer;
2112 lnet_peer_clear_discovery_error(lp);
2115 * We're willing to be interrupted. The lpni can become a
2116 * zombie if we race with DLC, so we must check for that.
2119 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2120 if (signal_pending(current))
2122 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2124 if (lp->lp_dc_error)
2126 if (lnet_peer_is_uptodate(lp))
2128 lnet_peer_queue_for_discovery(lp);
2130 if (lnet_is_discovery_disabled(lp))
2133 * if caller requested a non-blocking operation then
2134 * return immediately. Once discovery is complete then the
2135 * peer ref will be decremented and any pending messages
2136 * that were stopped due to discovery will be transmitted.
2141 lnet_peer_addref_locked(lp);
2142 lnet_net_unlock(LNET_LOCK_EX);
2144 finish_wait(&lp->lp_dc_waitq, &wait);
2145 lnet_net_lock(LNET_LOCK_EX);
2146 lnet_peer_decref_locked(lp);
2147 /* Peer may have changed */
2148 lp = lpni->lpni_peer_net->lpn_peer;
2150 finish_wait(&lp->lp_dc_waitq, &wait);
2152 lnet_net_unlock(LNET_LOCK_EX);
2156 * If the peer has changed after we've discovered the older peer,
2157 * then we need to discovery the new peer to make sure the
2158 * interface information is up to date
2160 if (lp != lpni->lpni_peer_net->lpn_peer)
2163 if (signal_pending(current))
2165 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2167 else if (lp->lp_dc_error)
2168 rc = lp->lp_dc_error;
2170 CDEBUG(D_NET, "non-blocking discovery\n");
2171 else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2174 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2175 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2176 libcfs_nid2str(lpni->lpni_nid), rc,
2177 (!block) ? "pending discovery" : "discovery complete");
2182 /* Handle an incoming ack for a push. */
2184 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2186 struct lnet_ping_buffer *pbuf;
2188 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2189 spin_lock(&lp->lp_lock);
2190 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2191 lp->lp_push_error = ev->status;
2193 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2195 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2196 spin_unlock(&lp->lp_lock);
2198 CDEBUG(D_NET, "peer %s ev->status %d\n",
2199 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2202 /* Handle a Reply message. This is the reply to a Ping message. */
2204 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2206 struct lnet_ping_buffer *pbuf;
2209 spin_lock(&lp->lp_lock);
2212 * If some kind of error happened the contents of message
2213 * cannot be used. Set PING_FAILED to trigger a retry.
2216 lp->lp_state |= LNET_PEER_PING_FAILED;
2217 lp->lp_ping_error = ev->status;
2218 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2220 libcfs_nid2str(lp->lp_primary_nid),
2221 libcfs_nid2str(ev->source.nid));
2225 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2226 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2227 lnet_swap_pinginfo(pbuf);
2230 * A reply with invalid or corrupted info. Set PING_FAILED to
2233 rc = lnet_ping_info_validate(&pbuf->pb_info);
2235 lp->lp_state |= LNET_PEER_PING_FAILED;
2236 lp->lp_ping_error = 0;
2237 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2238 libcfs_nid2str(lp->lp_primary_nid), rc);
2244 * Only enable the multi-rail feature on the peer if both sides of
2245 * the connection have discovery on
2247 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2248 CDEBUG(D_NET, "Peer %s has Multi-Rail feature enabled\n",
2249 libcfs_nid2str(lp->lp_primary_nid));
2250 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2252 CDEBUG(D_NET, "Peer %s has Multi-Rail feature disabled\n",
2253 libcfs_nid2str(lp->lp_primary_nid));
2254 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2258 * The peer may have discovery disabled at its end. Set
2259 * NO_DISCOVERY as appropriate.
2261 if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2262 !lnet_peer_discovery_disabled) {
2263 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2264 libcfs_nid2str(lp->lp_primary_nid));
2265 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2267 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2268 libcfs_nid2str(lp->lp_primary_nid));
2269 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2273 * Update the MULTI_RAIL flag based on the reply. If the peer
2274 * was configured with DLC then the setting should match what
2277 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2278 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2279 /* Everything's fine */
2280 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2281 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2282 libcfs_nid2str(lp->lp_primary_nid));
2285 * if discovery is disabled then we don't want to
2286 * update the state of the peer. All we'll do is
2287 * update the peer_nis which were reported back in
2291 if (!lnet_is_discovery_disabled_locked(lp)) {
2292 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2293 lnet_peer_clr_non_mr_pref_nids(lp);
2296 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2297 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2298 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2299 libcfs_nid2str(lp->lp_primary_nid));
2301 CERROR("Multi-Rail state vanished from %s\n",
2302 libcfs_nid2str(lp->lp_primary_nid));
2303 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2308 * Make sure we'll allocate the correct size ping buffer when
2311 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2312 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2315 * Check for truncation of the Reply. Clear PING_SENT and set
2316 * PING_FAILED to trigger a retry.
2318 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2319 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2320 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2321 lp->lp_state |= LNET_PEER_PING_FAILED;
2322 lp->lp_ping_error = 0;
2323 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2324 libcfs_nid2str(lp->lp_primary_nid),
2325 pbuf->pb_info.pi_nnis);
2330 * Check the sequence numbers in the reply. These are only
2331 * available if the reply came from a Multi-Rail peer.
2333 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2334 pbuf->pb_info.pi_nnis > 1 &&
2335 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2336 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2337 CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2338 libcfs_nid2str(lp->lp_primary_nid),
2339 LNET_PING_BUFFER_SEQNO(pbuf),
2342 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2345 /* We're happy with the state of the data in the buffer. */
2346 CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2347 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2348 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2349 lnet_ping_buffer_decref(lp->lp_data);
2351 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2352 lnet_ping_buffer_addref(pbuf);
2355 lp->lp_state &= ~LNET_PEER_PING_SENT;
2356 spin_unlock(&lp->lp_lock);
2360 * Send event handling. Only matters for error cases, where we clean
2361 * up state on the peer and peer_ni that would otherwise be updated in
2362 * the REPLY event handler for a successful Ping, and the ACK event
2363 * handler for a successful Push.
2366 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2373 spin_lock(&lp->lp_lock);
2374 if (ev->msg_type == LNET_MSG_GET) {
2375 lp->lp_state &= ~LNET_PEER_PING_SENT;
2376 lp->lp_state |= LNET_PEER_PING_FAILED;
2377 lp->lp_ping_error = ev->status;
2378 } else { /* ev->msg_type == LNET_MSG_PUT */
2379 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2380 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2381 lp->lp_push_error = ev->status;
2383 spin_unlock(&lp->lp_lock);
2384 rc = LNET_REDISCOVER_PEER;
2386 CDEBUG(D_NET, "%s Send to %s: %d\n",
2387 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2388 libcfs_nid2str(ev->target.nid), rc);
2393 * Unlink event handling. This event is only seen if a call to
2394 * LNetMDUnlink() caused the event to be unlinked. If this call was
2395 * made after the event was set up in LNetGet() or LNetPut() then we
2396 * assume the Ping or Push timed out.
2399 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2401 spin_lock(&lp->lp_lock);
2402 /* We've passed through LNetGet() */
2403 if (lp->lp_state & LNET_PEER_PING_SENT) {
2404 lp->lp_state &= ~LNET_PEER_PING_SENT;
2405 lp->lp_state |= LNET_PEER_PING_FAILED;
2406 lp->lp_ping_error = -ETIMEDOUT;
2407 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2408 libcfs_nid2str(lp->lp_primary_nid));
2410 /* We've passed through LNetPut() */
2411 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2412 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2413 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2414 lp->lp_push_error = -ETIMEDOUT;
2415 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2416 libcfs_nid2str(lp->lp_primary_nid));
2418 spin_unlock(&lp->lp_lock);
2422 * Event handler for the discovery EQ.
2424 * Called with lnet_res_lock(cpt) held. The cpt is the
2425 * lnet_cpt_of_cookie() of the md handle cookie.
2427 static void lnet_discovery_event_handler(struct lnet_event *event)
2429 struct lnet_peer *lp = event->md.user_ptr;
2430 struct lnet_ping_buffer *pbuf;
2433 /* discovery needs to take another look */
2434 rc = LNET_REDISCOVER_PEER;
2436 CDEBUG(D_NET, "Received event: %d\n", event->type);
2438 switch (event->type) {
2439 case LNET_EVENT_ACK:
2440 lnet_discovery_event_ack(lp, event);
2442 case LNET_EVENT_REPLY:
2443 lnet_discovery_event_reply(lp, event);
2445 case LNET_EVENT_SEND:
2446 /* Only send failure triggers a retry. */
2447 rc = lnet_discovery_event_send(lp, event);
2449 case LNET_EVENT_UNLINK:
2450 /* LNetMDUnlink() was called */
2451 lnet_discovery_event_unlink(lp, event);
2454 /* Invalid events. */
2457 lnet_net_lock(LNET_LOCK_EX);
2458 if (event->unlinked) {
2459 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2460 lnet_ping_buffer_decref(pbuf);
2461 lnet_peer_decref_locked(lp);
2464 /* put peer back at end of request queue, if discovery not already
2466 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2467 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2468 wake_up(&the_lnet.ln_dc_waitq);
2470 lnet_net_unlock(LNET_LOCK_EX);
2474 * Build a peer from incoming data.
2476 * The NIDs in the incoming data are supposed to be structured as follows:
2479 * - other NIDs in same net
2480 * - NIDs in second net
2481 * - NIDs in third net
2483 * This due to the way the list of NIDs in the data is created.
2485 * Note that this function will mark the peer uptodate unless an
2486 * ENOMEM is encontered. All other errors are due to a conflict
2487 * between the DLC configuration and what discovery sees. We treat DLC
2488 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2489 * peer from becoming stuck in discovery.
2491 static int lnet_peer_merge_data(struct lnet_peer *lp,
2492 struct lnet_ping_buffer *pbuf)
2494 struct lnet_peer_ni *lpni;
2495 lnet_nid_t *curnis = NULL;
2496 struct lnet_ni_status *addnis = NULL;
2497 lnet_nid_t *delnis = NULL;
2507 flags = LNET_PEER_DISCOVERED;
2508 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2509 flags |= LNET_PEER_MULTI_RAIL;
2512 * Cache the routing feature for the peer; whether it is enabled
2513 * for disabled as reported by the remote peer.
2515 spin_lock(&lp->lp_lock);
2516 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2517 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2519 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2520 spin_unlock(&lp->lp_lock);
2522 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2523 LIBCFS_ALLOC(curnis, nnis * sizeof(*curnis));
2524 LIBCFS_ALLOC(addnis, nnis * sizeof(*addnis));
2525 LIBCFS_ALLOC(delnis, nnis * sizeof(*delnis));
2526 if (!curnis || !addnis || !delnis) {
2534 /* Construct the list of NIDs present in peer. */
2536 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2537 curnis[ncurnis++] = lpni->lpni_nid;
2540 * Check for NIDs in pbuf not present in curnis[].
2541 * The loop starts at 1 to skip the loopback NID.
2543 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2544 for (j = 0; j < ncurnis; j++)
2545 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2548 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2551 * Check for NIDs in curnis[] not present in pbuf.
2552 * The nested loop starts at 1 to skip the loopback NID.
2554 * But never add the loopback NID to delnis[]: if it is
2555 * present in curnis[] then this peer is for this node.
2557 for (i = 0; i < ncurnis; i++) {
2558 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2560 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2561 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2563 * update the information we cache for the
2564 * peer with the latest information we
2567 lpni = lnet_find_peer_ni_locked(curnis[i]);
2569 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2570 lnet_peer_ni_decref_locked(lpni);
2575 if (j == pbuf->pb_info.pi_nnis)
2576 delnis[ndelnis++] = curnis[i];
2580 * If we get here and the discovery is disabled then we don't want
2581 * to add or delete any NIs. We just updated the ones we have some
2582 * information on, and call it a day
2585 if (lnet_is_discovery_disabled(lp))
2588 for (i = 0; i < naddnis; i++) {
2589 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2591 CERROR("Error adding NID %s to peer %s: %d\n",
2592 libcfs_nid2str(addnis[i].ns_nid),
2593 libcfs_nid2str(lp->lp_primary_nid), rc);
2597 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2599 lpni->lpni_ns_status = addnis[i].ns_status;
2600 lnet_peer_ni_decref_locked(lpni);
2604 for (i = 0; i < ndelnis; i++) {
2606 * for routers it's okay to delete the primary_nid because
2607 * the upper layers don't really rely on it. So if we're
2608 * being told that the router changed its primary_nid
2609 * then it's okay to delete it.
2611 if (lp->lp_rtr_refcount > 0)
2612 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2613 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2615 CERROR("Error deleting NID %s from peer %s: %d\n",
2616 libcfs_nid2str(delnis[i]),
2617 libcfs_nid2str(lp->lp_primary_nid), rc);
2623 * Errors other than -ENOMEM are due to peers having been
2624 * configured with DLC. Ignore these because DLC overrides
2629 LIBCFS_FREE(curnis, nnis * sizeof(*curnis));
2630 LIBCFS_FREE(addnis, nnis * sizeof(*addnis));
2631 LIBCFS_FREE(delnis, nnis * sizeof(*delnis));
2632 lnet_ping_buffer_decref(pbuf);
2633 CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2636 spin_lock(&lp->lp_lock);
2637 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2638 lp->lp_state |= LNET_PEER_FORCE_PING;
2639 spin_unlock(&lp->lp_lock);
2645 * The data in pbuf says lp is its primary peer, but the data was
2646 * received by a different peer. Try to update lp with the data.
2649 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2651 struct lnet_handle_md mdh;
2653 /* Queue lp for discovery, and force it on the request queue. */
2654 lnet_net_lock(LNET_LOCK_EX);
2655 if (lnet_peer_queue_for_discovery(lp))
2656 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2657 lnet_net_unlock(LNET_LOCK_EX);
2659 LNetInvalidateMDHandle(&mdh);
2662 * Decide whether we can move the peer to the DATA_PRESENT state.
2664 * We replace stale data for a multi-rail peer, repair PING_FAILED
2665 * status, and preempt FORCE_PING.
2667 * If after that we have DATA_PRESENT, we merge it into this peer.
2669 spin_lock(&lp->lp_lock);
2670 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2671 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2672 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2673 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2674 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2675 lnet_ping_buffer_decref(pbuf);
2680 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2681 lnet_ping_buffer_decref(lp->lp_data);
2683 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2685 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2686 mdh = lp->lp_ping_mdh;
2687 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2688 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2689 lp->lp_ping_error = 0;
2691 if (lp->lp_state & LNET_PEER_FORCE_PING)
2692 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2693 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2694 spin_unlock(&lp->lp_lock);
2696 if (!LNetMDHandleIsInvalid(mdh))
2700 return lnet_peer_merge_data(lp, pbuf);
2702 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2706 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2710 for (i = 0; i < pinfo->pi_nnis; i++) {
2711 if (pinfo->pi_ni[i].ns_nid == nid)
2719 * Update a peer using the data received.
2721 static int lnet_peer_data_present(struct lnet_peer *lp)
2722 __must_hold(&lp->lp_lock)
2724 struct lnet_ping_buffer *pbuf;
2725 struct lnet_peer_ni *lpni;
2726 lnet_nid_t nid = LNET_NID_ANY;
2732 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2733 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2734 spin_unlock(&lp->lp_lock);
2737 * Modifications of peer structures are done while holding the
2738 * ln_api_mutex. A global lock is required because we may be
2739 * modifying multiple peer structures, and a mutex greatly
2740 * simplifies memory management.
2742 * The actual changes to the data structures must also protect
2743 * against concurrent lookups, for which the lnet_net_lock in
2744 * LNET_LOCK_EX mode is used.
2746 mutex_lock(&the_lnet.ln_api_mutex);
2747 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2753 * If this peer is not on the peer list then it is being torn
2754 * down, and our reference count may be all that is keeping it
2755 * alive. Don't do any work on it.
2757 if (list_empty(&lp->lp_peer_list))
2760 flags = LNET_PEER_DISCOVERED;
2761 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2762 flags |= LNET_PEER_MULTI_RAIL;
2765 * Check whether the primary NID in the message matches the
2766 * primary NID of the peer. If it does, update the peer, if
2767 * it it does not, check whether there is already a peer with
2768 * that primary NID. If no such peer exists, try to update
2769 * the primary NID of the current peer (allowed if it was
2770 * created due to message traffic) and complete the update.
2771 * If the peer did exist, hand off the data to it.
2773 * The peer for the loopback interface is a special case: this
2774 * is the peer for the local node, and we want to set its
2775 * primary NID to the correct value here. Moreover, this peer
2776 * can show up with only the loopback NID in the ping buffer.
2778 if (pbuf->pb_info.pi_nnis <= 1)
2780 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2781 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2782 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2784 rc = lnet_peer_merge_data(lp, pbuf);
2786 * if the primary nid of the peer is present in the ping info returned
2787 * from the peer, but it's not the local primary peer we have
2788 * cached and discovery is disabled, then we don't want to update
2789 * our local peer info, by adding or removing NIDs, we just want
2790 * to update the status of the nids that we currently have
2791 * recorded in that peer.
2793 } else if (lp->lp_primary_nid == nid ||
2794 (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2795 lnet_is_discovery_disabled(lp))) {
2796 rc = lnet_peer_merge_data(lp, pbuf);
2798 lpni = lnet_find_peer_ni_locked(nid);
2800 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2802 CERROR("Primary NID error %s versus %s: %d\n",
2803 libcfs_nid2str(lp->lp_primary_nid),
2804 libcfs_nid2str(nid), rc);
2806 rc = lnet_peer_merge_data(lp, pbuf);
2809 struct lnet_peer *new_lp;
2810 new_lp = lpni->lpni_peer_net->lpn_peer;
2812 * if lp has discovery/MR enabled that means new_lp
2813 * should have discovery/MR enabled as well, since
2814 * it's the same peer, which we're about to merge
2816 if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2817 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2818 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2819 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2821 rc = lnet_peer_set_primary_data(new_lp, pbuf);
2822 lnet_consolidate_routes_locked(lp, new_lp);
2823 lnet_peer_ni_decref_locked(lpni);
2827 CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2829 mutex_unlock(&the_lnet.ln_api_mutex);
2831 spin_lock(&lp->lp_lock);
2832 /* Tell discovery to re-check the peer immediately. */
2834 rc = LNET_REDISCOVER_PEER;
2839 * A ping failed. Clear the PING_FAILED state and set the
2840 * FORCE_PING state, to ensure a retry even if discovery is
2841 * disabled. This avoids being left with incorrect state.
2843 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2844 __must_hold(&lp->lp_lock)
2846 struct lnet_handle_md mdh;
2849 mdh = lp->lp_ping_mdh;
2850 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2851 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2852 lp->lp_state |= LNET_PEER_FORCE_PING;
2853 rc = lp->lp_ping_error;
2854 lp->lp_ping_error = 0;
2855 spin_unlock(&lp->lp_lock);
2857 if (!LNetMDHandleIsInvalid(mdh))
2860 CDEBUG(D_NET, "peer %s:%d\n",
2861 libcfs_nid2str(lp->lp_primary_nid), rc);
2863 spin_lock(&lp->lp_lock);
2864 return rc ? rc : LNET_REDISCOVER_PEER;
2868 * Select NID to send a Ping or Push to.
2870 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2872 struct lnet_peer_ni *lpni;
2874 /* Look for a direct-connected NID for this peer. */
2876 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2877 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2882 return lpni->lpni_nid;
2884 /* Look for a routed-connected NID for this peer. */
2886 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2887 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2892 return lpni->lpni_nid;
2894 return LNET_NID_ANY;
2897 /* Active side of ping. */
2898 static int lnet_peer_send_ping(struct lnet_peer *lp)
2899 __must_hold(&lp->lp_lock)
2906 lp->lp_state |= LNET_PEER_PING_SENT;
2907 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2908 spin_unlock(&lp->lp_lock);
2910 cpt = lnet_net_lock_current();
2911 /* Refcount for MD. */
2912 lnet_peer_addref_locked(lp);
2913 pnid = lnet_peer_select_nid(lp);
2914 lnet_net_unlock(cpt);
2916 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2918 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2919 the_lnet.ln_dc_eqh, false);
2922 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2923 * refcount on the peer, otherwise LNetMDUnlink will be called
2924 * which will eventually do that.
2928 lnet_peer_decref_locked(lp);
2929 lnet_net_unlock(cpt);
2930 rc = -rc; /* change the rc to negative value */
2932 } else if (rc < 0) {
2936 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2938 spin_lock(&lp->lp_lock);
2942 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2944 * The errors that get us here are considered hard errors and
2945 * cause Discovery to terminate. So we clear PING_SENT, but do
2946 * not set either PING_FAILED or FORCE_PING. In fact we need
2947 * to clear PING_FAILED, because the unlink event handler will
2948 * have set it if we called LNetMDUnlink() above.
2950 spin_lock(&lp->lp_lock);
2951 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2956 * This function exists because you cannot call LNetMDUnlink() from an
2959 static int lnet_peer_push_failed(struct lnet_peer *lp)
2960 __must_hold(&lp->lp_lock)
2962 struct lnet_handle_md mdh;
2965 mdh = lp->lp_push_mdh;
2966 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2967 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2968 rc = lp->lp_push_error;
2969 lp->lp_push_error = 0;
2970 spin_unlock(&lp->lp_lock);
2972 if (!LNetMDHandleIsInvalid(mdh))
2975 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2976 spin_lock(&lp->lp_lock);
2977 return rc ? rc : LNET_REDISCOVER_PEER;
2980 /* Active side of push. */
2981 static int lnet_peer_send_push(struct lnet_peer *lp)
2982 __must_hold(&lp->lp_lock)
2984 struct lnet_ping_buffer *pbuf;
2985 struct lnet_process_id id;
2990 /* Don't push to a non-multi-rail peer. */
2991 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2992 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2996 lp->lp_state |= LNET_PEER_PUSH_SENT;
2997 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2998 spin_unlock(&lp->lp_lock);
3000 cpt = lnet_net_lock_current();
3001 pbuf = the_lnet.ln_ping_target;
3002 lnet_ping_buffer_addref(pbuf);
3003 lnet_net_unlock(cpt);
3005 /* Push source MD */
3006 md.start = &pbuf->pb_info;
3007 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3008 md.threshold = 2; /* Put/Ack */
3011 md.eq_handle = the_lnet.ln_dc_eqh;
3014 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
3016 lnet_ping_buffer_decref(pbuf);
3017 CERROR("Can't bind push source MD: %d\n", rc);
3020 cpt = lnet_net_lock_current();
3021 /* Refcount for MD. */
3022 lnet_peer_addref_locked(lp);
3023 id.pid = LNET_PID_LUSTRE;
3024 id.nid = lnet_peer_select_nid(lp);
3025 lnet_net_unlock(cpt);
3027 if (id.nid == LNET_NID_ANY) {
3032 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
3033 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3034 LNET_PROTO_PING_MATCHBITS, 0, 0);
3039 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3041 spin_lock(&lp->lp_lock);
3045 LNetMDUnlink(lp->lp_push_mdh);
3046 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3048 CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3050 * The errors that get us here are considered hard errors and
3051 * cause Discovery to terminate. So we clear PUSH_SENT, but do
3052 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3053 * because the unlink event handler will have set it if we
3054 * called LNetMDUnlink() above.
3056 spin_lock(&lp->lp_lock);
3057 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3062 * An unrecoverable error was encountered during discovery.
3063 * Set error status in peer and abort discovery.
3065 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3067 CDEBUG(D_NET, "Discovery error %s: %d\n",
3068 libcfs_nid2str(lp->lp_primary_nid), error);
3070 spin_lock(&lp->lp_lock);
3071 lp->lp_dc_error = error;
3072 lp->lp_state &= ~LNET_PEER_DISCOVERING;
3073 lp->lp_state |= LNET_PEER_REDISCOVER;
3074 spin_unlock(&lp->lp_lock);
3078 * Mark the peer as discovered.
3080 static int lnet_peer_discovered(struct lnet_peer *lp)
3081 __must_hold(&lp->lp_lock)
3083 lp->lp_state |= LNET_PEER_DISCOVERED;
3084 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3085 LNET_PEER_REDISCOVER);
3087 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3094 * Discovering this peer is taking too long. Cancel any Ping or Push
3095 * that discovery is waiting on by unlinking the relevant MDs. The
3096 * lnet_discovery_event_handler() will proceed from here and complete
3099 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3101 struct lnet_handle_md ping_mdh;
3102 struct lnet_handle_md push_mdh;
3104 LNetInvalidateMDHandle(&ping_mdh);
3105 LNetInvalidateMDHandle(&push_mdh);
3107 spin_lock(&lp->lp_lock);
3108 if (lp->lp_state & LNET_PEER_PING_SENT) {
3109 ping_mdh = lp->lp_ping_mdh;
3110 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3112 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3113 push_mdh = lp->lp_push_mdh;
3114 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3116 spin_unlock(&lp->lp_lock);
3118 if (!LNetMDHandleIsInvalid(ping_mdh))
3119 LNetMDUnlink(ping_mdh);
3120 if (!LNetMDHandleIsInvalid(push_mdh))
3121 LNetMDUnlink(push_mdh);
3125 * Wait for work to be queued or some other change that must be
3126 * attended to. Returns non-zero if the discovery thread should shut
3129 static int lnet_peer_discovery_wait_for_work(void)
3136 cpt = lnet_net_lock_current();
3138 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3139 TASK_INTERRUPTIBLE);
3140 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3142 if (lnet_push_target_resize_needed())
3144 if (!list_empty(&the_lnet.ln_dc_request))
3146 if (!list_empty(&the_lnet.ln_msg_resend))
3148 lnet_net_unlock(cpt);
3151 * wakeup max every second to check if there are peers that
3152 * have been stuck on the working queue for greater than
3155 schedule_timeout(cfs_time_seconds(1));
3156 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3157 cpt = lnet_net_lock_current();
3159 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3161 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3164 lnet_net_unlock(cpt);
3166 CDEBUG(D_NET, "woken: %d\n", rc);
3172 * Messages that were pending on a destroyed peer will be put on a global
3173 * resend list. The message resend list will be checked by
3174 * the discovery thread when it wakes up, and will resend messages. These
3175 * messages can still be sendable in the case the lpni which was the initial
3176 * cause of the message re-queue was transfered to another peer.
3178 * It is possible that LNet could be shutdown while we're iterating
3179 * through the list. lnet_shudown_lndnets() will attempt to access the
3180 * resend list, but will have to wait until the spinlock is released, by
3181 * which time there shouldn't be any more messages on the resend list.
3182 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3183 * for the messages so they can be released. The other case is that
3184 * lnet_shudown_lndnets() can finalize all the messages before this
3185 * function can visit the resend list, in which case this function will be
3188 static void lnet_resend_msgs(void)
3190 struct lnet_msg *msg, *tmp;
3191 struct list_head resend;
3194 INIT_LIST_HEAD(&resend);
3196 spin_lock(&the_lnet.ln_msg_resend_lock);
3197 list_splice(&the_lnet.ln_msg_resend, &resend);
3198 spin_unlock(&the_lnet.ln_msg_resend_lock);
3200 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3201 list_del_init(&msg->msg_list);
3202 rc = lnet_send(msg->msg_src_nid_param, msg,
3203 msg->msg_rtr_nid_param);
3205 CNETERR("Error sending %s to %s: %d\n",
3206 lnet_msgtyp2str(msg->msg_type),
3207 libcfs_id2str(msg->msg_target), rc);
3208 lnet_finalize(msg, rc);
3213 /* The discovery thread. */
3214 static int lnet_peer_discovery(void *arg)
3216 struct lnet_peer *lp;
3219 CDEBUG(D_NET, "started\n");
3220 cfs_block_allsigs();
3223 if (lnet_peer_discovery_wait_for_work())
3228 if (lnet_push_target_resize_needed())
3229 lnet_push_target_resize();
3231 lnet_net_lock(LNET_LOCK_EX);
3232 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3236 * Process all incoming discovery work requests. When
3237 * discovery must wait on a peer to change state, it
3238 * is added to the tail of the ln_dc_working queue. A
3239 * timestamp keeps track of when the peer was added,
3240 * so we can time out discovery requests that take too
3243 while (!list_empty(&the_lnet.ln_dc_request)) {
3244 lp = list_first_entry(&the_lnet.ln_dc_request,
3245 struct lnet_peer, lp_dc_list);
3246 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3248 * set the time the peer was put on the dc_working
3249 * queue. It shouldn't remain on the queue
3250 * forever, in case the GET message (for ping)
3251 * doesn't get a REPLY or the PUT message (for
3252 * push) doesn't get an ACK.
3254 lp->lp_last_queued = ktime_get_real_seconds();
3255 lnet_net_unlock(LNET_LOCK_EX);
3258 * Select an action depending on the state of
3259 * the peer and whether discovery is disabled.
3260 * The check whether discovery is disabled is
3261 * done after the code that handles processing
3262 * for arrived data, cleanup for failures, and
3263 * forcing a Ping or Push.
3265 spin_lock(&lp->lp_lock);
3266 CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3267 libcfs_nid2str(lp->lp_primary_nid), lp,
3269 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3270 rc = lnet_peer_data_present(lp);
3271 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3272 rc = lnet_peer_ping_failed(lp);
3273 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3274 rc = lnet_peer_push_failed(lp);
3275 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3276 rc = lnet_peer_send_ping(lp);
3277 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3278 rc = lnet_peer_send_push(lp);
3279 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3280 rc = lnet_peer_send_ping(lp);
3281 else if (lnet_peer_needs_push(lp))
3282 rc = lnet_peer_send_push(lp);
3284 rc = lnet_peer_discovered(lp);
3285 CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3286 libcfs_nid2str(lp->lp_primary_nid), lp,
3288 spin_unlock(&lp->lp_lock);
3290 lnet_net_lock(LNET_LOCK_EX);
3291 if (rc == LNET_REDISCOVER_PEER) {
3292 list_move(&lp->lp_dc_list,
3293 &the_lnet.ln_dc_request);
3295 lnet_peer_discovery_error(lp, rc);
3297 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3298 lnet_peer_discovery_complete(lp);
3299 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3303 lnet_net_unlock(LNET_LOCK_EX);
3306 CDEBUG(D_NET, "stopping\n");
3308 * Clean up before telling lnet_peer_discovery_stop() that
3309 * we're done. Use wake_up() below to somewhat reduce the
3310 * size of the thundering herd if there are multiple threads
3311 * waiting on discovery of a single peer.
3314 /* Queue cleanup 1: stop all pending pings and pushes. */
3315 lnet_net_lock(LNET_LOCK_EX);
3316 while (!list_empty(&the_lnet.ln_dc_working)) {
3317 lp = list_first_entry(&the_lnet.ln_dc_working,
3318 struct lnet_peer, lp_dc_list);
3319 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3320 lnet_net_unlock(LNET_LOCK_EX);
3321 lnet_peer_cancel_discovery(lp);
3322 lnet_net_lock(LNET_LOCK_EX);
3324 lnet_net_unlock(LNET_LOCK_EX);
3326 /* Queue cleanup 2: wait for the expired queue to clear. */
3327 while (!list_empty(&the_lnet.ln_dc_expired))
3328 schedule_timeout(cfs_time_seconds(1));
3330 /* Queue cleanup 3: clear the request queue. */
3331 lnet_net_lock(LNET_LOCK_EX);
3332 while (!list_empty(&the_lnet.ln_dc_request)) {
3333 lp = list_first_entry(&the_lnet.ln_dc_request,
3334 struct lnet_peer, lp_dc_list);
3335 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3336 lnet_peer_discovery_complete(lp);
3338 lnet_net_unlock(LNET_LOCK_EX);
3340 LNetEQFree(the_lnet.ln_dc_eqh);
3341 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3343 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3344 wake_up(&the_lnet.ln_dc_waitq);
3346 CDEBUG(D_NET, "stopped\n");
3351 /* ln_api_mutex is held on entry. */
3352 int lnet_peer_discovery_start(void)
3354 struct task_struct *task;
3357 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3360 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3362 CERROR("Can't allocate discovery EQ: %d\n", rc);
3366 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3367 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3370 CERROR("Can't start peer discovery thread: %d\n", rc);
3372 LNetEQFree(the_lnet.ln_dc_eqh);
3373 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3375 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3378 CDEBUG(D_NET, "discovery start: %d\n", rc);
3383 /* ln_api_mutex is held on entry. */
3384 void lnet_peer_discovery_stop(void)
3386 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3389 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3390 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3391 wake_up(&the_lnet.ln_dc_waitq);
3393 wait_event(the_lnet.ln_dc_waitq,
3394 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3396 LASSERT(list_empty(&the_lnet.ln_dc_request));
3397 LASSERT(list_empty(&the_lnet.ln_dc_working));
3398 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3400 CDEBUG(D_NET, "discovery stopped\n");
3406 lnet_debug_peer(lnet_nid_t nid)
3408 char *aliveness = "NA";
3409 struct lnet_peer_ni *lp;
3412 cpt = lnet_cpt_of_nid(nid, NULL);
3415 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3417 lnet_net_unlock(cpt);
3418 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3422 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3423 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3425 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3426 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3427 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3428 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3429 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3431 lnet_peer_ni_decref_locked(lp);
3433 lnet_net_unlock(cpt);
3436 /* Gathering information for userspace. */
3438 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3439 char aliveness[LNET_MAX_STR_LEN],
3440 __u32 *cpt_iter, __u32 *refcount,
3441 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3442 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3443 __u32 *peer_tx_qnob)
3445 struct lnet_peer_table *peer_table;
3446 struct lnet_peer_ni *lp;
3451 /* get the number of CPTs */
3452 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3454 /* if the cpt number to be examined is >= the number of cpts in
3455 * the system then indicate that there are no more cpts to examin
3457 if (*cpt_iter >= lncpt)
3460 /* get the current table */
3461 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3462 /* if the ptable is NULL then there are no more cpts to examine */
3463 if (peer_table == NULL)
3466 lnet_net_lock(*cpt_iter);
3468 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3469 struct list_head *peers = &peer_table->pt_hash[j];
3471 list_for_each_entry(lp, peers, lpni_hashlist) {
3472 if (peer_index-- > 0)
3475 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3476 if (lnet_isrouter(lp) ||
3477 lnet_peer_aliveness_enabled(lp))
3478 snprintf(aliveness, LNET_MAX_STR_LEN,
3479 lnet_is_peer_ni_alive(lp) ? "up" : "down");
3481 *nid = lp->lpni_nid;
3482 *refcount = atomic_read(&lp->lpni_refcount);
3483 *ni_peer_tx_credits =
3484 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3485 *peer_tx_credits = lp->lpni_txcredits;
3486 *peer_rtr_credits = lp->lpni_rtrcredits;
3487 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3488 *peer_tx_qnob = lp->lpni_txqnob;
3494 lnet_net_unlock(*cpt_iter);
3498 return found ? 0 : -ENOENT;
3501 /* ln_api_mutex is held, which keeps the peer list stable */
3502 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3504 struct lnet_ioctl_element_stats *lpni_stats;
3505 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3506 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3507 struct lnet_peer_ni_credit_info *lpni_info;
3508 struct lnet_peer_ni *lpni;
3509 struct lnet_peer *lp;
3514 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3521 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3522 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3523 size *= lp->lp_nnis;
3524 if (size > cfg->prcfg_size) {
3525 cfg->prcfg_size = size;
3530 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3531 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3532 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3533 cfg->prcfg_count = lp->lp_nnis;
3534 cfg->prcfg_size = size;
3535 cfg->prcfg_state = lp->lp_state;
3537 /* Allocate helper buffers. */
3539 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3542 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3545 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3546 if (!lpni_msg_stats)
3547 goto out_free_stats;
3548 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3550 goto out_free_msg_stats;
3555 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3556 nid = lpni->lpni_nid;
3557 if (copy_to_user(bulk, &nid, sizeof(nid)))
3558 goto out_free_hstats;
3559 bulk += sizeof(nid);
3561 memset(lpni_info, 0, sizeof(*lpni_info));
3562 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3563 if (lnet_isrouter(lpni) ||
3564 lnet_peer_aliveness_enabled(lpni))
3565 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3566 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3568 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3569 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3570 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3571 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3572 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3573 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3574 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3575 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3576 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3577 goto out_free_hstats;
3578 bulk += sizeof(*lpni_info);
3580 memset(lpni_stats, 0, sizeof(*lpni_stats));
3581 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3582 LNET_STATS_TYPE_SEND);
3583 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3584 LNET_STATS_TYPE_RECV);
3585 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3586 LNET_STATS_TYPE_DROP);
3587 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3588 goto out_free_hstats;
3589 bulk += sizeof(*lpni_stats);
3590 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3591 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3592 goto out_free_hstats;
3593 bulk += sizeof(*lpni_msg_stats);
3594 lpni_hstats->hlpni_network_timeout =
3595 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3596 lpni_hstats->hlpni_remote_dropped =
3597 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3598 lpni_hstats->hlpni_remote_timeout =
3599 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3600 lpni_hstats->hlpni_remote_error =
3601 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3602 lpni_hstats->hlpni_health_value =
3603 atomic_read(&lpni->lpni_healthv);
3604 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3605 goto out_free_hstats;
3606 bulk += sizeof(*lpni_hstats);
3611 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3613 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3615 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3617 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3619 lnet_peer_decref_locked(lp);
3625 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3627 /* the mt could've shutdown and cleaned up the queues */
3628 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3631 if (list_empty(&lpni->lpni_recovery) &&
3632 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3633 CERROR("lpni %s added to recovery queue. Health = %d\n",
3634 libcfs_nid2str(lpni->lpni_nid),
3635 atomic_read(&lpni->lpni_healthv));
3636 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3637 lnet_peer_ni_addref_locked(lpni);
3641 /* Call with the ln_api_mutex held */
3643 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3645 struct lnet_peer_table *ptable;
3646 struct lnet_peer *lp;
3647 struct lnet_peer_net *lpn;
3648 struct lnet_peer_ni *lpni;
3652 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3656 lnet_net_lock(LNET_LOCK_EX);
3657 lpni = lnet_find_peer_ni_locked(nid);
3659 lnet_net_unlock(LNET_LOCK_EX);
3662 atomic_set(&lpni->lpni_healthv, value);
3663 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3664 lnet_peer_ni_decref_locked(lpni);
3665 lnet_net_unlock(LNET_LOCK_EX);
3669 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3672 * Walk all the peers and reset the healhv for each one to the
3675 lnet_net_lock(LNET_LOCK_EX);
3676 for (cpt = 0; cpt < lncpt; cpt++) {
3677 ptable = the_lnet.ln_peer_tables[cpt];
3678 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3679 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3680 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3682 atomic_set(&lpni->lpni_healthv, value);
3683 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3688 lnet_net_unlock(LNET_LOCK_EX);