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, 2014, 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 <lnet/lib-lnet.h>
38 #include <uapi/linux/lnet/lnet-dlc.h>
40 /* Value indicating that recovery needs to re-check a peer immediately. */
41 #define LNET_REDISCOVER_PEER (1)
43 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
46 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
48 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
49 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
50 lnet_peer_ni_decref_locked(lpni);
55 lnet_peer_net_added(struct lnet_net *net)
57 struct lnet_peer_ni *lpni, *tmp;
59 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
60 lpni_on_remote_peer_ni_list) {
62 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
65 spin_lock(&lpni->lpni_lock);
66 lpni->lpni_txcredits =
67 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
68 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
69 lpni->lpni_rtrcredits =
70 lnet_peer_buffer_credits(lpni->lpni_net);
71 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
72 spin_unlock(&lpni->lpni_lock);
74 lnet_peer_remove_from_remote_list(lpni);
80 lnet_peer_tables_destroy(void)
82 struct lnet_peer_table *ptable;
83 struct list_head *hash;
87 if (!the_lnet.ln_peer_tables)
90 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
91 hash = ptable->pt_hash;
92 if (!hash) /* not intialized */
95 LASSERT(list_empty(&ptable->pt_zombie_list));
97 ptable->pt_hash = NULL;
98 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
99 LASSERT(list_empty(&hash[j]));
101 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
104 cfs_percpt_free(the_lnet.ln_peer_tables);
105 the_lnet.ln_peer_tables = NULL;
109 lnet_peer_tables_create(void)
111 struct lnet_peer_table *ptable;
112 struct list_head *hash;
116 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
118 if (the_lnet.ln_peer_tables == NULL) {
119 CERROR("Failed to allocate cpu-partition peer tables\n");
123 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
124 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
125 LNET_PEER_HASH_SIZE * sizeof(*hash));
127 CERROR("Failed to create peer hash table\n");
128 lnet_peer_tables_destroy();
132 spin_lock_init(&ptable->pt_zombie_lock);
133 INIT_LIST_HEAD(&ptable->pt_zombie_list);
135 INIT_LIST_HEAD(&ptable->pt_peer_list);
137 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
138 INIT_LIST_HEAD(&hash[j]);
139 ptable->pt_hash = hash; /* sign of initialization */
145 static struct lnet_peer_ni *
146 lnet_peer_ni_alloc(lnet_nid_t nid)
148 struct lnet_peer_ni *lpni;
149 struct lnet_net *net;
152 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
154 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
158 INIT_LIST_HEAD(&lpni->lpni_txq);
159 INIT_LIST_HEAD(&lpni->lpni_rtrq);
160 INIT_LIST_HEAD(&lpni->lpni_routes);
161 INIT_LIST_HEAD(&lpni->lpni_hashlist);
162 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
163 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
165 spin_lock_init(&lpni->lpni_lock);
167 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
168 lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
169 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
170 lpni->lpni_nid = nid;
171 lpni->lpni_cpt = cpt;
172 lnet_set_peer_ni_health_locked(lpni, true);
174 net = lnet_get_net_locked(LNET_NIDNET(nid));
175 lpni->lpni_net = net;
177 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
178 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
179 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
180 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
183 * This peer_ni is not on a local network, so we
184 * cannot add the credits here. In case the net is
185 * added later, add the peer_ni to the remote peer ni
186 * list so it can be easily found and revisited.
188 /* FIXME: per-net implementation instead? */
189 atomic_inc(&lpni->lpni_refcount);
190 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
191 &the_lnet.ln_remote_peer_ni_list);
194 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
199 static struct lnet_peer_net *
200 lnet_peer_net_alloc(__u32 net_id)
202 struct lnet_peer_net *lpn;
204 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
208 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
209 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
210 lpn->lpn_net_id = net_id;
212 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
218 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
220 struct lnet_peer *lp;
222 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
224 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
225 LASSERT(list_empty(&lpn->lpn_peer_nis));
226 LASSERT(list_empty(&lpn->lpn_peer_nets));
228 lpn->lpn_peer = NULL;
229 LIBCFS_FREE(lpn, sizeof(*lpn));
231 lnet_peer_decref_locked(lp);
234 static struct lnet_peer *
235 lnet_peer_alloc(lnet_nid_t nid)
237 struct lnet_peer *lp;
239 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
243 INIT_LIST_HEAD(&lp->lp_peer_list);
244 INIT_LIST_HEAD(&lp->lp_peer_nets);
245 INIT_LIST_HEAD(&lp->lp_dc_list);
246 INIT_LIST_HEAD(&lp->lp_dc_pendq);
247 init_waitqueue_head(&lp->lp_dc_waitq);
248 spin_lock_init(&lp->lp_lock);
249 lp->lp_primary_nid = nid;
250 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
252 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
258 lnet_destroy_peer_locked(struct lnet_peer *lp)
260 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
262 LASSERT(atomic_read(&lp->lp_refcount) == 0);
263 LASSERT(list_empty(&lp->lp_peer_nets));
264 LASSERT(list_empty(&lp->lp_peer_list));
265 LASSERT(list_empty(&lp->lp_dc_list));
268 lnet_ping_buffer_decref(lp->lp_data);
270 LIBCFS_FREE(lp, sizeof(*lp));
274 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
275 * that peer_net, detach the peer_net from the peer.
277 * Call with lnet_net_lock/EX held
280 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
282 struct lnet_peer_table *ptable;
283 struct lnet_peer_net *lpn;
284 struct lnet_peer *lp;
287 * Belts and suspenders: gracefully handle teardown of a
288 * partially connected peer_ni.
290 lpn = lpni->lpni_peer_net;
292 list_del_init(&lpni->lpni_peer_nis);
294 * If there are no lpni's left, we detach lpn from
295 * lp_peer_nets, so it cannot be found anymore.
297 if (list_empty(&lpn->lpn_peer_nis))
298 list_del_init(&lpn->lpn_peer_nets);
300 /* Update peer NID count. */
305 * If there are no more peer nets, make the peer unfindable
306 * via the peer_tables.
308 * Otherwise, if the peer is DISCOVERED, tell discovery to
309 * take another look at it. This is a no-op if discovery for
310 * this peer did the detaching.
312 if (list_empty(&lp->lp_peer_nets)) {
313 list_del_init(&lp->lp_peer_list);
314 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
316 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
317 /* Discovery isn't running, nothing to do here. */
318 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
319 lnet_peer_queue_for_discovery(lp);
320 wake_up(&the_lnet.ln_dc_waitq);
322 CDEBUG(D_NET, "peer %s NID %s\n",
323 libcfs_nid2str(lp->lp_primary_nid),
324 libcfs_nid2str(lpni->lpni_nid));
327 /* called with lnet_net_lock LNET_LOCK_EX held */
329 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
331 struct lnet_peer_table *ptable = NULL;
333 /* don't remove a peer_ni if it's also a gateway */
334 if (lpni->lpni_rtr_refcount > 0) {
335 CERROR("Peer NI %s is a gateway. Can not delete it\n",
336 libcfs_nid2str(lpni->lpni_nid));
340 lnet_peer_remove_from_remote_list(lpni);
342 /* remove peer ni from the hash list. */
343 list_del_init(&lpni->lpni_hashlist);
345 /* decrement the ref count on the peer table */
346 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
347 LASSERT(ptable->pt_number > 0);
351 * The peer_ni can no longer be found with a lookup. But there
352 * can be current users, so keep track of it on the zombie
353 * list until the reference count has gone to zero.
355 * The last reference may be lost in a place where the
356 * lnet_net_lock locks only a single cpt, and that cpt may not
357 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
360 spin_lock(&ptable->pt_zombie_lock);
361 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
362 ptable->pt_zombies++;
363 spin_unlock(&ptable->pt_zombie_lock);
365 /* no need to keep this peer_ni on the hierarchy anymore */
366 lnet_peer_detach_peer_ni_locked(lpni);
368 /* remove hashlist reference on peer_ni */
369 lnet_peer_ni_decref_locked(lpni);
374 void lnet_peer_uninit(void)
376 struct lnet_peer_ni *lpni, *tmp;
378 lnet_net_lock(LNET_LOCK_EX);
380 /* remove all peer_nis from the remote peer and the hash list */
381 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
382 lpni_on_remote_peer_ni_list)
383 lnet_peer_ni_del_locked(lpni);
385 lnet_peer_tables_destroy();
387 lnet_net_unlock(LNET_LOCK_EX);
391 lnet_peer_del_locked(struct lnet_peer *peer)
393 struct lnet_peer_ni *lpni = NULL, *lpni2;
396 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
398 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
399 while (lpni != NULL) {
400 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
401 rc = lnet_peer_ni_del_locked(lpni);
411 lnet_peer_del(struct lnet_peer *peer)
413 lnet_net_lock(LNET_LOCK_EX);
414 lnet_peer_del_locked(peer);
415 lnet_net_unlock(LNET_LOCK_EX);
421 * Delete a NID from a peer. Call with ln_api_mutex held.
424 * -EPERM: Non-DLC deletion from DLC-configured peer.
425 * -ENOENT: No lnet_peer_ni corresponding to the nid.
426 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
427 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
430 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
432 struct lnet_peer_ni *lpni;
433 lnet_nid_t primary_nid = lp->lp_primary_nid;
436 if (!(flags & LNET_PEER_CONFIGURED)) {
437 if (lp->lp_state & LNET_PEER_CONFIGURED) {
442 lpni = lnet_find_peer_ni_locked(nid);
447 lnet_peer_ni_decref_locked(lpni);
448 if (lp != lpni->lpni_peer_net->lpn_peer) {
454 * This function only allows deletion of the primary NID if it
457 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
462 lnet_net_lock(LNET_LOCK_EX);
463 lnet_peer_ni_del_locked(lpni);
464 lnet_net_unlock(LNET_LOCK_EX);
467 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
468 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
474 lnet_peer_table_cleanup_locked(struct lnet_net *net,
475 struct lnet_peer_table *ptable)
478 struct lnet_peer_ni *next;
479 struct lnet_peer_ni *lpni;
480 struct lnet_peer *peer;
482 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
483 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
485 if (net != NULL && net != lpni->lpni_net)
488 peer = lpni->lpni_peer_net->lpn_peer;
489 if (peer->lp_primary_nid != lpni->lpni_nid) {
490 lnet_peer_ni_del_locked(lpni);
494 * Removing the primary NID implies removing
495 * the entire peer. Advance next beyond any
496 * peer_ni that belongs to the same peer.
498 list_for_each_entry_from(next, &ptable->pt_hash[i],
500 if (next->lpni_peer_net->lpn_peer != peer)
503 lnet_peer_del_locked(peer);
509 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
513 spin_lock(&ptable->pt_zombie_lock);
514 while (ptable->pt_zombies) {
515 spin_unlock(&ptable->pt_zombie_lock);
517 if (is_power_of_2(i)) {
519 "Waiting for %d zombies on peer table\n",
522 set_current_state(TASK_UNINTERRUPTIBLE);
523 schedule_timeout(cfs_time_seconds(1) >> 1);
524 spin_lock(&ptable->pt_zombie_lock);
526 spin_unlock(&ptable->pt_zombie_lock);
530 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
531 struct lnet_peer_table *ptable)
533 struct lnet_peer_ni *lp;
534 struct lnet_peer_ni *tmp;
538 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
539 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
541 if (net != lp->lpni_net)
544 if (lp->lpni_rtr_refcount == 0)
547 lpni_nid = lp->lpni_nid;
549 lnet_net_unlock(LNET_LOCK_EX);
550 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
551 lnet_net_lock(LNET_LOCK_EX);
557 lnet_peer_tables_cleanup(struct lnet_net *net)
560 struct lnet_peer_table *ptable;
562 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
563 /* If just deleting the peers for a NI, get rid of any routes these
564 * peers are gateways for. */
565 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
566 lnet_net_lock(LNET_LOCK_EX);
567 lnet_peer_table_del_rtrs_locked(net, ptable);
568 lnet_net_unlock(LNET_LOCK_EX);
571 /* Start the cleanup process */
572 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
573 lnet_net_lock(LNET_LOCK_EX);
574 lnet_peer_table_cleanup_locked(net, ptable);
575 lnet_net_unlock(LNET_LOCK_EX);
578 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
579 lnet_peer_ni_finalize_wait(ptable);
582 static struct lnet_peer_ni *
583 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
585 struct list_head *peers;
586 struct lnet_peer_ni *lp;
588 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
590 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
591 list_for_each_entry(lp, peers, lpni_hashlist) {
592 if (lp->lpni_nid == nid) {
593 lnet_peer_ni_addref_locked(lp);
601 struct lnet_peer_ni *
602 lnet_find_peer_ni_locked(lnet_nid_t nid)
604 struct lnet_peer_ni *lpni;
605 struct lnet_peer_table *ptable;
608 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
610 ptable = the_lnet.ln_peer_tables[cpt];
611 lpni = lnet_get_peer_ni_locked(ptable, nid);
617 lnet_find_peer(lnet_nid_t nid)
619 struct lnet_peer_ni *lpni;
620 struct lnet_peer *lp = NULL;
623 cpt = lnet_net_lock_current();
624 lpni = lnet_find_peer_ni_locked(nid);
626 lp = lpni->lpni_peer_net->lpn_peer;
627 lnet_peer_addref_locked(lp);
628 lnet_peer_ni_decref_locked(lpni);
630 lnet_net_unlock(cpt);
635 struct lnet_peer_ni *
636 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
637 struct lnet_peer_net *peer_net,
638 struct lnet_peer_ni *prev)
640 struct lnet_peer_ni *lpni;
641 struct lnet_peer_net *net = peer_net;
645 if (list_empty(&peer->lp_peer_nets))
648 net = list_entry(peer->lp_peer_nets.next,
649 struct lnet_peer_net,
652 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
658 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
660 * if you reached the end of the peer ni list and the peer
661 * net is specified then there are no more peer nis in that
668 * we reached the end of this net ni list. move to the
671 if (prev->lpni_peer_net->lpn_peer_nets.next ==
673 /* no more nets and no more NIs. */
676 /* get the next net */
677 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
678 struct lnet_peer_net,
680 /* get the ni on it */
681 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
687 /* there are more nis left */
688 lpni = list_entry(prev->lpni_peer_nis.next,
689 struct lnet_peer_ni, lpni_peer_nis);
694 /* Call with the ln_api_mutex held */
696 lnet_get_peer_list(__u32 *countp, __u32 *sizep, lnet_process_id_t __user *ids)
698 lnet_process_id_t id;
699 struct lnet_peer_table *ptable;
700 struct lnet_peer *lp;
709 if (the_lnet.ln_state != LNET_STATE_RUNNING)
712 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
715 * Count the number of peers, and return E2BIG if the buffer
716 * is too small. We'll also return the desired size.
719 for (cpt = 0; cpt < lncpt; cpt++) {
720 ptable = the_lnet.ln_peer_tables[cpt];
721 count += ptable->pt_peers;
723 size = count * sizeof(*ids);
728 * Walk the peer lists and copy out the primary nids.
729 * This is safe because the peer lists are only modified
730 * while the ln_api_mutex is held. So we don't need to
731 * hold the lnet_net_lock as well, and can therefore
732 * directly call copy_to_user().
735 memset(&id, 0, sizeof(id));
736 id.pid = LNET_PID_LUSTRE;
738 for (cpt = 0; cpt < lncpt; cpt++) {
739 ptable = the_lnet.ln_peer_tables[cpt];
740 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
743 id.nid = lp->lp_primary_nid;
744 if (copy_to_user(&ids[i], &id, sizeof(id)))
757 * Start pushes to peers that need to be updated for a configuration
758 * change on this node.
761 lnet_push_update_to_peers(int force)
763 struct lnet_peer_table *ptable;
764 struct lnet_peer *lp;
768 lnet_net_lock(LNET_LOCK_EX);
769 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
770 for (cpt = 0; cpt < lncpt; cpt++) {
771 ptable = the_lnet.ln_peer_tables[cpt];
772 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
774 spin_lock(&lp->lp_lock);
775 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
776 lp->lp_state |= LNET_PEER_FORCE_PUSH;
777 spin_unlock(&lp->lp_lock);
779 if (lnet_peer_needs_push(lp))
780 lnet_peer_queue_for_discovery(lp);
783 lnet_net_unlock(LNET_LOCK_EX);
784 wake_up(&the_lnet.ln_dc_waitq);
788 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
789 * this is a preferred point-to-point path. Call with lnet_net_lock in
793 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
797 if (lpni->lpni_pref_nnids == 0)
799 if (lpni->lpni_pref_nnids == 1)
800 return lpni->lpni_pref.nid == nid;
801 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
802 if (lpni->lpni_pref.nids[i] == nid)
809 * Set a single ni as preferred, provided no preferred ni is already
810 * defined. Only to be used for non-multi-rail peer_ni.
813 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
817 spin_lock(&lpni->lpni_lock);
818 if (nid == LNET_NID_ANY) {
820 } else if (lpni->lpni_pref_nnids > 0) {
822 } else if (lpni->lpni_pref_nnids == 0) {
823 lpni->lpni_pref.nid = nid;
824 lpni->lpni_pref_nnids = 1;
825 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
827 spin_unlock(&lpni->lpni_lock);
829 CDEBUG(D_NET, "peer %s nid %s: %d\n",
830 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
835 * Clear the preferred NID from a non-multi-rail peer_ni, provided
836 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
839 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
843 spin_lock(&lpni->lpni_lock);
844 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
845 lpni->lpni_pref_nnids = 0;
846 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
847 } else if (lpni->lpni_pref_nnids == 0) {
852 spin_unlock(&lpni->lpni_lock);
854 CDEBUG(D_NET, "peer %s: %d\n",
855 libcfs_nid2str(lpni->lpni_nid), rc);
860 * Clear the preferred NIDs from a non-multi-rail peer.
863 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
865 struct lnet_peer_ni *lpni = NULL;
867 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
868 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
872 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
874 lnet_nid_t *nids = NULL;
875 lnet_nid_t *oldnids = NULL;
876 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
881 if (nid == LNET_NID_ANY) {
886 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
891 /* A non-MR node may have only one preferred NI per peer_ni */
892 if (lpni->lpni_pref_nnids > 0) {
893 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
899 if (lpni->lpni_pref_nnids != 0) {
900 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
901 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
906 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
907 if (lpni->lpni_pref.nids[i] == nid) {
908 LIBCFS_FREE(nids, size);
912 nids[i] = lpni->lpni_pref.nids[i];
917 lnet_net_lock(LNET_LOCK_EX);
918 spin_lock(&lpni->lpni_lock);
919 if (lpni->lpni_pref_nnids == 0) {
920 lpni->lpni_pref.nid = nid;
922 oldnids = lpni->lpni_pref.nids;
923 lpni->lpni_pref.nids = nids;
925 lpni->lpni_pref_nnids++;
926 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
927 spin_unlock(&lpni->lpni_lock);
928 lnet_net_unlock(LNET_LOCK_EX);
931 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
932 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
935 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
936 spin_lock(&lpni->lpni_lock);
937 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
938 spin_unlock(&lpni->lpni_lock);
940 CDEBUG(D_NET, "peer %s nid %s: %d\n",
941 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
946 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
948 lnet_nid_t *nids = NULL;
949 lnet_nid_t *oldnids = NULL;
950 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
955 if (lpni->lpni_pref_nnids == 0) {
960 if (lpni->lpni_pref_nnids == 1) {
961 if (lpni->lpni_pref.nid != nid) {
965 } else if (lpni->lpni_pref_nnids == 2) {
966 if (lpni->lpni_pref.nids[0] != nid &&
967 lpni->lpni_pref.nids[1] != nid) {
972 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
973 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
978 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
979 if (lpni->lpni_pref.nids[i] != nid)
981 nids[j++] = lpni->lpni_pref.nids[i];
983 /* Check if we actually removed a nid. */
984 if (j == lpni->lpni_pref_nnids) {
985 LIBCFS_FREE(nids, size);
991 lnet_net_lock(LNET_LOCK_EX);
992 spin_lock(&lpni->lpni_lock);
993 if (lpni->lpni_pref_nnids == 1) {
994 lpni->lpni_pref.nid = LNET_NID_ANY;
995 } else if (lpni->lpni_pref_nnids == 2) {
996 oldnids = lpni->lpni_pref.nids;
997 if (oldnids[0] == nid)
998 lpni->lpni_pref.nid = oldnids[1];
1000 lpni->lpni_pref.nid = oldnids[2];
1002 oldnids = lpni->lpni_pref.nids;
1003 lpni->lpni_pref.nids = nids;
1005 lpni->lpni_pref_nnids--;
1006 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1007 spin_unlock(&lpni->lpni_lock);
1008 lnet_net_unlock(LNET_LOCK_EX);
1011 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1012 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1015 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1016 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1021 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1023 struct lnet_peer_ni *lpni;
1024 lnet_nid_t primary_nid = nid;
1026 lpni = lnet_find_peer_ni_locked(nid);
1028 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1029 lnet_peer_ni_decref_locked(lpni);
1036 LNetPrimaryNID(lnet_nid_t nid)
1038 struct lnet_peer *lp;
1039 struct lnet_peer_ni *lpni;
1040 lnet_nid_t primary_nid = nid;
1044 cpt = lnet_net_lock_current();
1045 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1050 lp = lpni->lpni_peer_net->lpn_peer;
1051 while (!lnet_peer_is_uptodate(lp)) {
1052 rc = lnet_discover_peer_locked(lpni, cpt, true);
1055 lp = lpni->lpni_peer_net->lpn_peer;
1057 primary_nid = lp->lp_primary_nid;
1059 lnet_peer_ni_decref_locked(lpni);
1061 lnet_net_unlock(cpt);
1063 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1064 libcfs_nid2str(primary_nid), rc);
1067 EXPORT_SYMBOL(LNetPrimaryNID);
1069 struct lnet_peer_net *
1070 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1072 struct lnet_peer_net *peer_net;
1073 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1074 if (peer_net->lpn_net_id == net_id)
1081 * Attach a peer_ni to a peer_net and peer. This function assumes
1082 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1083 * may be attached to a different peer, in which case it will be
1084 * properly detached first. The whole operation is done atomically.
1086 * Always returns 0. This is the last function called from functions
1087 * that do return an int, so returning 0 here allows the compiler to
1091 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1092 struct lnet_peer_net *lpn,
1093 struct lnet_peer_ni *lpni,
1096 struct lnet_peer_table *ptable;
1098 /* Install the new peer_ni */
1099 lnet_net_lock(LNET_LOCK_EX);
1100 /* Add peer_ni to global peer table hash, if necessary. */
1101 if (list_empty(&lpni->lpni_hashlist)) {
1102 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1104 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1105 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1106 ptable->pt_version++;
1107 ptable->pt_number++;
1108 /* This is the 1st refcount on lpni. */
1109 atomic_inc(&lpni->lpni_refcount);
1112 /* Detach the peer_ni from an existing peer, if necessary. */
1113 if (lpni->lpni_peer_net) {
1114 LASSERT(lpni->lpni_peer_net != lpn);
1115 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1116 lnet_peer_detach_peer_ni_locked(lpni);
1117 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1118 lpni->lpni_peer_net = NULL;
1121 /* Add peer_ni to peer_net */
1122 lpni->lpni_peer_net = lpn;
1123 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1124 lnet_peer_net_addref_locked(lpn);
1126 /* Add peer_net to peer */
1127 if (!lpn->lpn_peer) {
1129 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1130 lnet_peer_addref_locked(lp);
1133 /* Add peer to global peer list, if necessary */
1134 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1135 if (list_empty(&lp->lp_peer_list)) {
1136 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1141 /* Update peer state */
1142 spin_lock(&lp->lp_lock);
1143 if (flags & LNET_PEER_CONFIGURED) {
1144 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1145 lp->lp_state |= LNET_PEER_CONFIGURED;
1147 if (flags & LNET_PEER_MULTI_RAIL) {
1148 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1149 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1150 lnet_peer_clr_non_mr_pref_nids(lp);
1153 spin_unlock(&lp->lp_lock);
1156 lnet_net_unlock(LNET_LOCK_EX);
1158 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1159 libcfs_nid2str(lp->lp_primary_nid),
1160 libcfs_nid2str(lpni->lpni_nid), flags);
1166 * Create a new peer, with nid as its primary nid.
1168 * Call with the lnet_api_mutex held.
1171 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1173 struct lnet_peer *lp;
1174 struct lnet_peer_net *lpn;
1175 struct lnet_peer_ni *lpni;
1178 LASSERT(nid != LNET_NID_ANY);
1181 * No need for the lnet_net_lock here, because the
1182 * lnet_api_mutex is held.
1184 lpni = lnet_find_peer_ni_locked(nid);
1186 /* A peer with this NID already exists. */
1187 lp = lpni->lpni_peer_net->lpn_peer;
1188 lnet_peer_ni_decref_locked(lpni);
1190 * This is an error if the peer was configured and the
1191 * primary NID differs or an attempt is made to change
1192 * the Multi-Rail flag. Otherwise the assumption is
1193 * that an existing peer is being modified.
1195 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1196 if (lp->lp_primary_nid != nid)
1198 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1202 /* Delete and recreate as a configured peer. */
1206 /* Create peer, peer_net, and peer_ni. */
1208 lp = lnet_peer_alloc(nid);
1211 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1214 lpni = lnet_peer_ni_alloc(nid);
1218 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1221 LIBCFS_FREE(lpn, sizeof(*lpn));
1223 LIBCFS_FREE(lp, sizeof(*lp));
1225 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1226 libcfs_nid2str(nid), flags, rc);
1231 * Add a NID to a peer. Call with ln_api_mutex held.
1234 * -EPERM: Non-DLC addition to a DLC-configured peer.
1235 * -EEXIST: The NID was configured by DLC for a different peer.
1236 * -ENOMEM: Out of memory.
1237 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1238 * non-multi-rail peer.
1241 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1243 struct lnet_peer_net *lpn;
1244 struct lnet_peer_ni *lpni;
1248 LASSERT(nid != LNET_NID_ANY);
1250 /* A configured peer can only be updated through configuration. */
1251 if (!(flags & LNET_PEER_CONFIGURED)) {
1252 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1259 * The MULTI_RAIL flag can be set but not cleared, because
1260 * that would leave the peer struct in an invalid state.
1262 if (flags & LNET_PEER_MULTI_RAIL) {
1263 spin_lock(&lp->lp_lock);
1264 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1265 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1266 lnet_peer_clr_non_mr_pref_nids(lp);
1268 spin_unlock(&lp->lp_lock);
1269 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1274 lpni = lnet_find_peer_ni_locked(nid);
1277 * A peer_ni already exists. This is only a problem if
1278 * it is not connected to this peer and was configured
1281 lnet_peer_ni_decref_locked(lpni);
1282 if (lpni->lpni_peer_net->lpn_peer == lp)
1284 if (lnet_peer_ni_is_configured(lpni)) {
1288 /* If this is the primary NID, destroy the peer. */
1289 if (lnet_peer_ni_is_primary(lpni)) {
1290 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1291 lpni = lnet_peer_ni_alloc(nid);
1298 lpni = lnet_peer_ni_alloc(nid);
1306 * Get the peer_net. Check that we're not adding a second
1307 * peer_ni on a peer_net of a non-multi-rail peer.
1309 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1311 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1316 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1321 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1324 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1325 if (!lpni->lpni_peer_net)
1326 LIBCFS_FREE(lpni, sizeof(*lpni));
1328 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1329 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1335 * Update the primary NID of a peer, if possible.
1337 * Call with the lnet_api_mutex held.
1340 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1342 lnet_nid_t old = lp->lp_primary_nid;
1345 if (lp->lp_primary_nid == nid)
1347 rc = lnet_peer_add_nid(lp, nid, flags);
1350 lp->lp_primary_nid = nid;
1352 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1353 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1358 * lpni creation initiated due to traffic either sending or receiving.
1361 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1363 struct lnet_peer *lp;
1364 struct lnet_peer_net *lpn;
1365 struct lnet_peer_ni *lpni;
1369 if (nid == LNET_NID_ANY) {
1374 /* lnet_net_lock is not needed here because ln_api_lock is held */
1375 lpni = lnet_find_peer_ni_locked(nid);
1378 * We must have raced with another thread. Since we
1379 * know next to nothing about a peer_ni created by
1380 * traffic, we just assume everything is ok and
1383 lnet_peer_ni_decref_locked(lpni);
1387 /* Create peer, peer_net, and peer_ni. */
1389 lp = lnet_peer_alloc(nid);
1392 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1395 lpni = lnet_peer_ni_alloc(nid);
1398 if (pref != LNET_NID_ANY)
1399 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1401 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1404 LIBCFS_FREE(lpn, sizeof(*lpn));
1406 LIBCFS_FREE(lp, sizeof(*lp));
1408 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1413 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1415 * This API handles the following combinations:
1416 * Create a peer with its primary NI if only the prim_nid is provided
1417 * Add a NID to a peer identified by the prim_nid. The peer identified
1418 * by the prim_nid must already exist.
1419 * The peer being created may be non-MR.
1421 * The caller must hold ln_api_mutex. This prevents the peer from
1422 * being created/modified/deleted by a different thread.
1425 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1427 struct lnet_peer *lp = NULL;
1428 struct lnet_peer_ni *lpni;
1431 /* The prim_nid must always be specified */
1432 if (prim_nid == LNET_NID_ANY)
1435 flags = LNET_PEER_CONFIGURED;
1437 flags |= LNET_PEER_MULTI_RAIL;
1440 * If nid isn't specified, we must create a new peer with
1441 * prim_nid as its primary nid.
1443 if (nid == LNET_NID_ANY)
1444 return lnet_peer_add(prim_nid, flags);
1446 /* Look up the prim_nid, which must exist. */
1447 lpni = lnet_find_peer_ni_locked(prim_nid);
1450 lnet_peer_ni_decref_locked(lpni);
1451 lp = lpni->lpni_peer_net->lpn_peer;
1453 /* Peer must have been configured. */
1454 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1455 CDEBUG(D_NET, "peer %s was not configured\n",
1456 libcfs_nid2str(prim_nid));
1460 /* Primary NID must match */
1461 if (lp->lp_primary_nid != prim_nid) {
1462 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1463 libcfs_nid2str(prim_nid),
1464 libcfs_nid2str(lp->lp_primary_nid));
1468 /* Multi-Rail flag must match. */
1469 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1470 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1471 libcfs_nid2str(prim_nid));
1475 return lnet_peer_add_nid(lp, nid, flags);
1479 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1481 * This API handles the following combinations:
1482 * Delete a NI from a peer if both prim_nid and nid are provided.
1483 * Delete a peer if only prim_nid is provided.
1484 * Delete a peer if its primary nid is provided.
1486 * The caller must hold ln_api_mutex. This prevents the peer from
1487 * being modified/deleted by a different thread.
1490 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1492 struct lnet_peer *lp;
1493 struct lnet_peer_ni *lpni;
1496 if (prim_nid == LNET_NID_ANY)
1499 lpni = lnet_find_peer_ni_locked(prim_nid);
1502 lnet_peer_ni_decref_locked(lpni);
1503 lp = lpni->lpni_peer_net->lpn_peer;
1505 if (prim_nid != lp->lp_primary_nid) {
1506 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1507 libcfs_nid2str(prim_nid),
1508 libcfs_nid2str(lp->lp_primary_nid));
1512 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1513 return lnet_peer_del(lp);
1515 flags = LNET_PEER_CONFIGURED;
1516 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1517 flags |= LNET_PEER_MULTI_RAIL;
1519 return lnet_peer_del_nid(lp, nid, flags);
1523 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1525 struct lnet_peer_table *ptable;
1526 struct lnet_peer_net *lpn;
1528 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1530 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1531 LASSERT(lpni->lpni_rtr_refcount == 0);
1532 LASSERT(list_empty(&lpni->lpni_txq));
1533 LASSERT(lpni->lpni_txqnob == 0);
1535 lpn = lpni->lpni_peer_net;
1536 lpni->lpni_peer_net = NULL;
1537 lpni->lpni_net = NULL;
1539 /* remove the peer ni from the zombie list */
1540 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1541 spin_lock(&ptable->pt_zombie_lock);
1542 list_del_init(&lpni->lpni_hashlist);
1543 ptable->pt_zombies--;
1544 spin_unlock(&ptable->pt_zombie_lock);
1546 if (lpni->lpni_pref_nnids > 1) {
1547 LIBCFS_FREE(lpni->lpni_pref.nids,
1548 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1550 LIBCFS_FREE(lpni, sizeof(*lpni));
1552 lnet_peer_net_decref_locked(lpn);
1555 struct lnet_peer_ni *
1556 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1558 struct lnet_peer_ni *lpni = NULL;
1561 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1562 return ERR_PTR(-ESHUTDOWN);
1565 * find if a peer_ni already exists.
1566 * If so then just return that.
1568 lpni = lnet_find_peer_ni_locked(nid);
1572 lnet_net_unlock(cpt);
1574 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1577 goto out_net_relock;
1580 lpni = lnet_find_peer_ni_locked(nid);
1590 * Get a peer_ni for the given nid, create it if necessary. Takes a
1591 * hold on the peer_ni.
1593 struct lnet_peer_ni *
1594 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1596 struct lnet_peer_ni *lpni = NULL;
1599 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1600 return ERR_PTR(-ESHUTDOWN);
1603 * find if a peer_ni already exists.
1604 * If so then just return that.
1606 lpni = lnet_find_peer_ni_locked(nid);
1612 * use the lnet_api_mutex to serialize the creation of the peer_ni
1613 * and the creation/deletion of the local ni/net. When a local ni is
1614 * created, if there exists a set of peer_nis on that network,
1615 * they need to be traversed and updated. When a local NI is
1616 * deleted, which could result in a network being deleted, then
1617 * all peer nis on that network need to be removed as well.
1619 * Creation through traffic should also be serialized with
1620 * creation through DLC.
1622 lnet_net_unlock(cpt);
1623 mutex_lock(&the_lnet.ln_api_mutex);
1625 * Shutdown is only set under the ln_api_lock, so a single
1626 * check here is sufficent.
1628 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1629 lpni = ERR_PTR(-ESHUTDOWN);
1630 goto out_mutex_unlock;
1633 rc = lnet_peer_ni_traffic_add(nid, pref);
1636 goto out_mutex_unlock;
1639 lpni = lnet_find_peer_ni_locked(nid);
1643 mutex_unlock(&the_lnet.ln_api_mutex);
1646 /* Lock has been dropped, check again for shutdown. */
1647 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1649 lnet_peer_ni_decref_locked(lpni);
1650 lpni = ERR_PTR(-ESHUTDOWN);
1661 * Is a peer uptodate from the point of view of discovery?
1663 * If it is currently being processed, obviously not.
1664 * A forced Ping or Push is also handled by the discovery thread.
1666 * Otherwise look at whether the peer needs rediscovering.
1669 lnet_peer_is_uptodate(struct lnet_peer *lp)
1673 spin_lock(&lp->lp_lock);
1674 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1675 LNET_PEER_FORCE_PING |
1676 LNET_PEER_FORCE_PUSH)) {
1678 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1680 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1681 if (lnet_peer_discovery_disabled)
1685 } else if (lnet_peer_needs_push(lp)) {
1687 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1688 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1695 spin_unlock(&lp->lp_lock);
1701 * Queue a peer for the attention of the discovery thread. Call with
1702 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1703 * -EALREADY if the peer was already queued.
1705 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1709 spin_lock(&lp->lp_lock);
1710 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1711 lp->lp_state |= LNET_PEER_DISCOVERING;
1712 spin_unlock(&lp->lp_lock);
1713 if (list_empty(&lp->lp_dc_list)) {
1714 lnet_peer_addref_locked(lp);
1715 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1716 wake_up(&the_lnet.ln_dc_waitq);
1722 CDEBUG(D_NET, "Queue peer %s: %d\n",
1723 libcfs_nid2str(lp->lp_primary_nid), rc);
1729 * Discovery of a peer is complete. Wake all waiters on the peer.
1730 * Call with lnet_net_lock/EX held.
1732 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1734 struct lnet_msg *msg = NULL;
1736 struct list_head pending_msgs;
1738 INIT_LIST_HEAD(&pending_msgs);
1740 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1741 libcfs_nid2str(lp->lp_primary_nid));
1743 list_del_init(&lp->lp_dc_list);
1744 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1745 wake_up_all(&lp->lp_dc_waitq);
1747 lnet_net_unlock(LNET_LOCK_EX);
1749 /* iterate through all pending messages and send them again */
1750 list_for_each_entry(msg, &pending_msgs, msg_list) {
1751 if (lp->lp_dc_error) {
1752 lnet_finalize(msg, lp->lp_dc_error);
1756 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1757 lnet_msgtyp2str(msg->msg_type),
1758 libcfs_id2str(msg->msg_target));
1759 rc = lnet_send(msg->msg_src_nid_param, msg,
1760 msg->msg_rtr_nid_param);
1762 CNETERR("Error sending %s to %s: %d\n",
1763 lnet_msgtyp2str(msg->msg_type),
1764 libcfs_id2str(msg->msg_target), rc);
1765 lnet_finalize(msg, rc);
1768 lnet_net_lock(LNET_LOCK_EX);
1769 lnet_peer_decref_locked(lp);
1773 * Handle inbound push.
1774 * Like any event handler, called with lnet_res_lock/CPT held.
1776 void lnet_peer_push_event(struct lnet_event *ev)
1778 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1779 struct lnet_peer *lp;
1781 /* lnet_find_peer() adds a refcount */
1782 lp = lnet_find_peer(ev->source.nid);
1784 CERROR("Push Put from unknown %s (source %s)\n",
1785 libcfs_nid2str(ev->initiator.nid),
1786 libcfs_nid2str(ev->source.nid));
1790 /* Ensure peer state remains consistent while we modify it. */
1791 spin_lock(&lp->lp_lock);
1794 * If some kind of error happened the contents of the message
1795 * cannot be used. Clear the NIDS_UPTODATE and set the
1796 * FORCE_PING flag to trigger a ping.
1799 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1800 lp->lp_state |= LNET_PEER_FORCE_PING;
1801 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1803 libcfs_nid2str(lp->lp_primary_nid),
1804 libcfs_nid2str(ev->source.nid));
1809 * A push with invalid or corrupted info. Clear the UPTODATE
1810 * flag to trigger a ping.
1812 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1813 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1814 lp->lp_state |= LNET_PEER_FORCE_PING;
1815 CDEBUG(D_NET, "Corrupted Push from %s\n",
1816 libcfs_nid2str(lp->lp_primary_nid));
1821 * Make sure we'll allocate the correct size ping buffer when
1824 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1825 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1828 * A non-Multi-Rail peer is not supposed to be capable of
1831 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1832 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1833 libcfs_nid2str(lp->lp_primary_nid));
1838 * Check the MULTIRAIL flag. Complain if the peer was DLC
1839 * configured without it.
1841 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1842 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1843 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1844 libcfs_nid2str(lp->lp_primary_nid));
1846 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1847 lnet_peer_clr_non_mr_pref_nids(lp);
1852 * The peer may have discovery disabled at its end. Set
1853 * NO_DISCOVERY as appropriate.
1855 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1856 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1857 libcfs_nid2str(lp->lp_primary_nid));
1858 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1859 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1860 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1861 libcfs_nid2str(lp->lp_primary_nid));
1862 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1866 * Check for truncation of the Put message. Clear the
1867 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1868 * and tell discovery to allocate a bigger buffer.
1870 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1871 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1872 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1873 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1874 lp->lp_state |= LNET_PEER_FORCE_PING;
1875 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1876 libcfs_nid2str(lp->lp_primary_nid),
1877 pbuf->pb_info.pi_nnis);
1882 * Check whether the Put data is stale. Stale data can just be
1885 if (pbuf->pb_info.pi_nnis > 1 &&
1886 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1887 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1888 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1889 libcfs_nid2str(lp->lp_primary_nid),
1890 LNET_PING_BUFFER_SEQNO(pbuf),
1896 * Check whether the Put data is new, in which case we clear
1897 * the UPTODATE flag and prepare to process it.
1899 * If the Put data is current, and the peer is UPTODATE then
1900 * we assome everything is all right and drop the data as
1903 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1904 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1905 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1906 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1907 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1908 libcfs_nid2str(lp->lp_primary_nid),
1909 LNET_PING_BUFFER_SEQNO(pbuf),
1915 * If there is data present that hasn't been processed yet,
1916 * we'll replace it if the Put contained newer data and it
1917 * fits. We're racing with a Ping or earlier Push in this
1920 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1921 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1922 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1923 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1924 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1925 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1926 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1927 libcfs_nid2str(lp->lp_primary_nid),
1928 LNET_PING_BUFFER_SEQNO(pbuf),
1929 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1935 * Allocate a buffer to copy the data. On a failure we drop
1936 * the Push and set FORCE_PING to force the discovery
1937 * thread to fix the problem by pinging the peer.
1939 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
1941 lp->lp_state |= LNET_PEER_FORCE_PING;
1942 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
1943 libcfs_nid2str(lp->lp_primary_nid),
1944 LNET_PING_BUFFER_SEQNO(pbuf));
1949 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1950 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1951 lp->lp_state |= LNET_PEER_DATA_PRESENT;
1952 CDEBUG(D_NET, "Received Push %s %u\n",
1953 libcfs_nid2str(lp->lp_primary_nid),
1954 LNET_PING_BUFFER_SEQNO(pbuf));
1958 * Queue the peer for discovery, and wake the discovery thread
1959 * if the peer was already queued, because its status changed.
1961 spin_unlock(&lp->lp_lock);
1962 lnet_net_lock(LNET_LOCK_EX);
1963 if (lnet_peer_queue_for_discovery(lp))
1964 wake_up(&the_lnet.ln_dc_waitq);
1965 /* Drop refcount from lookup */
1966 lnet_peer_decref_locked(lp);
1967 lnet_net_unlock(LNET_LOCK_EX);
1971 * Clear the discovery error state, unless we're already discovering
1972 * this peer, in which case the error is current.
1974 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
1976 spin_lock(&lp->lp_lock);
1977 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1978 lp->lp_dc_error = 0;
1979 spin_unlock(&lp->lp_lock);
1983 * Peer discovery slow path. The ln_api_mutex is held on entry, and
1984 * dropped/retaken within this function. An lnet_peer_ni is passed in
1985 * because discovery could tear down an lnet_peer.
1988 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
1991 struct lnet_peer *lp;
1995 lnet_net_unlock(cpt);
1996 lnet_net_lock(LNET_LOCK_EX);
1997 lp = lpni->lpni_peer_net->lpn_peer;
1998 lnet_peer_clear_discovery_error(lp);
2001 * We're willing to be interrupted. The lpni can become a
2002 * zombie if we race with DLC, so we must check for that.
2005 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2006 if (signal_pending(current))
2008 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2010 if (lp->lp_dc_error)
2012 if (lnet_peer_is_uptodate(lp))
2014 lnet_peer_queue_for_discovery(lp);
2016 * if caller requested a non-blocking operation then
2017 * return immediately. Once discovery is complete then the
2018 * peer ref will be decremented and any pending messages
2019 * that were stopped due to discovery will be transmitted.
2024 lnet_peer_addref_locked(lp);
2025 lnet_net_unlock(LNET_LOCK_EX);
2027 finish_wait(&lp->lp_dc_waitq, &wait);
2028 lnet_net_lock(LNET_LOCK_EX);
2029 lnet_peer_decref_locked(lp);
2030 /* Peer may have changed */
2031 lp = lpni->lpni_peer_net->lpn_peer;
2033 finish_wait(&lp->lp_dc_waitq, &wait);
2035 lnet_net_unlock(LNET_LOCK_EX);
2039 * If the peer has changed after we've discovered the older peer,
2040 * then we need to discovery the new peer to make sure the
2041 * interface information is up to date
2043 if (lp != lpni->lpni_peer_net->lpn_peer)
2046 if (signal_pending(current))
2048 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2050 else if (lp->lp_dc_error)
2051 rc = lp->lp_dc_error;
2053 CDEBUG(D_NET, "non-blocking discovery\n");
2054 else if (!lnet_peer_is_uptodate(lp))
2057 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2058 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2059 libcfs_nid2str(lpni->lpni_nid), rc,
2060 (!block) ? "pending discovery" : "discovery complete");
2065 /* Handle an incoming ack for a push. */
2067 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2069 struct lnet_ping_buffer *pbuf;
2071 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2072 spin_lock(&lp->lp_lock);
2073 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2074 lp->lp_push_error = ev->status;
2076 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2078 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2079 spin_unlock(&lp->lp_lock);
2081 CDEBUG(D_NET, "peer %s ev->status %d\n",
2082 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2085 /* Handle a Reply message. This is the reply to a Ping message. */
2087 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2089 struct lnet_ping_buffer *pbuf;
2092 spin_lock(&lp->lp_lock);
2095 * If some kind of error happened the contents of message
2096 * cannot be used. Set PING_FAILED to trigger a retry.
2099 lp->lp_state |= LNET_PEER_PING_FAILED;
2100 lp->lp_ping_error = ev->status;
2101 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2103 libcfs_nid2str(lp->lp_primary_nid),
2104 libcfs_nid2str(ev->source.nid));
2108 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2109 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2110 lnet_swap_pinginfo(pbuf);
2113 * A reply with invalid or corrupted info. Set PING_FAILED to
2116 rc = lnet_ping_info_validate(&pbuf->pb_info);
2118 lp->lp_state |= LNET_PEER_PING_FAILED;
2119 lp->lp_ping_error = 0;
2120 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2121 libcfs_nid2str(lp->lp_primary_nid), rc);
2126 * Update the MULTI_RAIL flag based on the reply. If the peer
2127 * was configured with DLC then the setting should match what
2130 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2131 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2132 /* Everything's fine */
2133 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2134 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2135 libcfs_nid2str(lp->lp_primary_nid));
2137 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2138 lnet_peer_clr_non_mr_pref_nids(lp);
2140 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2141 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2142 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2143 libcfs_nid2str(lp->lp_primary_nid));
2145 CERROR("Multi-Rail state vanished from %s\n",
2146 libcfs_nid2str(lp->lp_primary_nid));
2147 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2152 * Make sure we'll allocate the correct size ping buffer when
2155 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2156 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2159 * The peer may have discovery disabled at its end. Set
2160 * NO_DISCOVERY as appropriate.
2162 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2163 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2164 libcfs_nid2str(lp->lp_primary_nid));
2165 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2166 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2167 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2168 libcfs_nid2str(lp->lp_primary_nid));
2169 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2173 * Check for truncation of the Reply. Clear PING_SENT and set
2174 * PING_FAILED to trigger a retry.
2176 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2177 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2178 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2179 lp->lp_state |= LNET_PEER_PING_FAILED;
2180 lp->lp_ping_error = 0;
2181 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2182 libcfs_nid2str(lp->lp_primary_nid),
2183 pbuf->pb_info.pi_nnis);
2188 * Check the sequence numbers in the reply. These are only
2189 * available if the reply came from a Multi-Rail peer.
2191 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2192 pbuf->pb_info.pi_nnis > 1 &&
2193 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2194 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2195 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2196 libcfs_nid2str(lp->lp_primary_nid),
2197 LNET_PING_BUFFER_SEQNO(pbuf),
2202 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2203 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2206 /* We're happy with the state of the data in the buffer. */
2207 CDEBUG(D_NET, "peer %s data present %u\n",
2208 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2209 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2210 lnet_ping_buffer_decref(lp->lp_data);
2212 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2213 lnet_ping_buffer_addref(pbuf);
2216 lp->lp_state &= ~LNET_PEER_PING_SENT;
2217 spin_unlock(&lp->lp_lock);
2221 * Send event handling. Only matters for error cases, where we clean
2222 * up state on the peer and peer_ni that would otherwise be updated in
2223 * the REPLY event handler for a successful Ping, and the ACK event
2224 * handler for a successful Push.
2227 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2234 spin_lock(&lp->lp_lock);
2235 if (ev->msg_type == LNET_MSG_GET) {
2236 lp->lp_state &= ~LNET_PEER_PING_SENT;
2237 lp->lp_state |= LNET_PEER_PING_FAILED;
2238 lp->lp_ping_error = ev->status;
2239 } else { /* ev->msg_type == LNET_MSG_PUT */
2240 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2241 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2242 lp->lp_push_error = ev->status;
2244 spin_unlock(&lp->lp_lock);
2245 rc = LNET_REDISCOVER_PEER;
2247 CDEBUG(D_NET, "%s Send to %s: %d\n",
2248 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2249 libcfs_nid2str(ev->target.nid), rc);
2254 * Unlink event handling. This event is only seen if a call to
2255 * LNetMDUnlink() caused the event to be unlinked. If this call was
2256 * made after the event was set up in LNetGet() or LNetPut() then we
2257 * assume the Ping or Push timed out.
2260 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2262 spin_lock(&lp->lp_lock);
2263 /* We've passed through LNetGet() */
2264 if (lp->lp_state & LNET_PEER_PING_SENT) {
2265 lp->lp_state &= ~LNET_PEER_PING_SENT;
2266 lp->lp_state |= LNET_PEER_PING_FAILED;
2267 lp->lp_ping_error = -ETIMEDOUT;
2268 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2269 libcfs_nid2str(lp->lp_primary_nid));
2271 /* We've passed through LNetPut() */
2272 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2273 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2274 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2275 lp->lp_push_error = -ETIMEDOUT;
2276 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2277 libcfs_nid2str(lp->lp_primary_nid));
2279 spin_unlock(&lp->lp_lock);
2283 * Event handler for the discovery EQ.
2285 * Called with lnet_res_lock(cpt) held. The cpt is the
2286 * lnet_cpt_of_cookie() of the md handle cookie.
2288 static void lnet_discovery_event_handler(lnet_event_t *event)
2290 struct lnet_peer *lp = event->md.user_ptr;
2291 struct lnet_ping_buffer *pbuf;
2294 /* discovery needs to take another look */
2295 rc = LNET_REDISCOVER_PEER;
2297 CDEBUG(D_NET, "Received event: %d\n", event->type);
2299 switch (event->type) {
2300 case LNET_EVENT_ACK:
2301 lnet_discovery_event_ack(lp, event);
2303 case LNET_EVENT_REPLY:
2304 lnet_discovery_event_reply(lp, event);
2306 case LNET_EVENT_SEND:
2307 /* Only send failure triggers a retry. */
2308 rc = lnet_discovery_event_send(lp, event);
2310 case LNET_EVENT_UNLINK:
2311 /* LNetMDUnlink() was called */
2312 lnet_discovery_event_unlink(lp, event);
2315 /* Invalid events. */
2318 lnet_net_lock(LNET_LOCK_EX);
2319 if (event->unlinked) {
2320 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2321 lnet_ping_buffer_decref(pbuf);
2322 lnet_peer_decref_locked(lp);
2324 if (rc == LNET_REDISCOVER_PEER) {
2325 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2326 wake_up(&the_lnet.ln_dc_waitq);
2328 lnet_net_unlock(LNET_LOCK_EX);
2332 * Build a peer from incoming data.
2334 * The NIDs in the incoming data are supposed to be structured as follows:
2337 * - other NIDs in same net
2338 * - NIDs in second net
2339 * - NIDs in third net
2341 * This due to the way the list of NIDs in the data is created.
2343 * Note that this function will mark the peer uptodate unless an
2344 * ENOMEM is encontered. All other errors are due to a conflict
2345 * between the DLC configuration and what discovery sees. We treat DLC
2346 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2347 * peer from becoming stuck in discovery.
2349 static int lnet_peer_merge_data(struct lnet_peer *lp,
2350 struct lnet_ping_buffer *pbuf)
2352 struct lnet_peer_ni *lpni;
2353 lnet_nid_t *curnis = NULL;
2354 lnet_nid_t *addnis = NULL;
2355 lnet_nid_t *delnis = NULL;
2365 flags = LNET_PEER_DISCOVERED;
2366 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2367 flags |= LNET_PEER_MULTI_RAIL;
2369 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2370 LIBCFS_ALLOC(curnis, nnis * sizeof(lnet_nid_t));
2371 LIBCFS_ALLOC(addnis, nnis * sizeof(lnet_nid_t));
2372 LIBCFS_ALLOC(delnis, nnis * sizeof(lnet_nid_t));
2373 if (!curnis || !addnis || !delnis) {
2381 /* Construct the list of NIDs present in peer. */
2383 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2384 curnis[ncurnis++] = lpni->lpni_nid;
2387 * Check for NIDs in pbuf not present in curnis[].
2388 * The loop starts at 1 to skip the loopback NID.
2390 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2391 for (j = 0; j < ncurnis; j++)
2392 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2395 addnis[naddnis++] = pbuf->pb_info.pi_ni[i].ns_nid;
2398 * Check for NIDs in curnis[] not present in pbuf.
2399 * The nested loop starts at 1 to skip the loopback NID.
2401 * But never add the loopback NID to delnis[]: if it is
2402 * present in curnis[] then this peer is for this node.
2404 for (i = 0; i < ncurnis; i++) {
2405 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2407 for (j = 1; j < pbuf->pb_info.pi_nnis; j++)
2408 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid)
2410 if (j == pbuf->pb_info.pi_nnis)
2411 delnis[ndelnis++] = curnis[i];
2414 for (i = 0; i < naddnis; i++) {
2415 rc = lnet_peer_add_nid(lp, addnis[i], flags);
2417 CERROR("Error adding NID %s to peer %s: %d\n",
2418 libcfs_nid2str(addnis[i]),
2419 libcfs_nid2str(lp->lp_primary_nid), rc);
2424 for (i = 0; i < ndelnis; i++) {
2425 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2427 CERROR("Error deleting NID %s from peer %s: %d\n",
2428 libcfs_nid2str(delnis[i]),
2429 libcfs_nid2str(lp->lp_primary_nid), rc);
2435 * Errors other than -ENOMEM are due to peers having been
2436 * configured with DLC. Ignore these because DLC overrides
2441 LIBCFS_FREE(curnis, nnis * sizeof(lnet_nid_t));
2442 LIBCFS_FREE(addnis, nnis * sizeof(lnet_nid_t));
2443 LIBCFS_FREE(delnis, nnis * sizeof(lnet_nid_t));
2444 lnet_ping_buffer_decref(pbuf);
2445 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2448 spin_lock(&lp->lp_lock);
2449 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2450 lp->lp_state |= LNET_PEER_FORCE_PING;
2451 spin_unlock(&lp->lp_lock);
2457 * The data in pbuf says lp is its primary peer, but the data was
2458 * received by a different peer. Try to update lp with the data.
2461 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2463 lnet_handle_md_t mdh;
2465 /* Queue lp for discovery, and force it on the request queue. */
2466 lnet_net_lock(LNET_LOCK_EX);
2467 if (lnet_peer_queue_for_discovery(lp))
2468 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2469 lnet_net_unlock(LNET_LOCK_EX);
2471 LNetInvalidateMDHandle(&mdh);
2474 * Decide whether we can move the peer to the DATA_PRESENT state.
2476 * We replace stale data for a multi-rail peer, repair PING_FAILED
2477 * status, and preempt FORCE_PING.
2479 * If after that we have DATA_PRESENT, we merge it into this peer.
2481 spin_lock(&lp->lp_lock);
2482 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2483 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2484 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2485 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2486 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2487 lnet_ping_buffer_decref(pbuf);
2492 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2493 lnet_ping_buffer_decref(lp->lp_data);
2495 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2497 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2498 mdh = lp->lp_ping_mdh;
2499 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2500 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2501 lp->lp_ping_error = 0;
2503 if (lp->lp_state & LNET_PEER_FORCE_PING)
2504 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2505 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2506 spin_unlock(&lp->lp_lock);
2508 if (!LNetMDHandleIsInvalid(mdh))
2512 return lnet_peer_merge_data(lp, pbuf);
2514 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2519 * Update a peer using the data received.
2521 static int lnet_peer_data_present(struct lnet_peer *lp)
2522 __must_hold(&lp->lp_lock)
2524 struct lnet_ping_buffer *pbuf;
2525 struct lnet_peer_ni *lpni;
2526 lnet_nid_t nid = LNET_NID_ANY;
2532 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2533 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2534 spin_unlock(&lp->lp_lock);
2537 * Modifications of peer structures are done while holding the
2538 * ln_api_mutex. A global lock is required because we may be
2539 * modifying multiple peer structures, and a mutex greatly
2540 * simplifies memory management.
2542 * The actual changes to the data structures must also protect
2543 * against concurrent lookups, for which the lnet_net_lock in
2544 * LNET_LOCK_EX mode is used.
2546 mutex_lock(&the_lnet.ln_api_mutex);
2547 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2553 * If this peer is not on the peer list then it is being torn
2554 * down, and our reference count may be all that is keeping it
2555 * alive. Don't do any work on it.
2557 if (list_empty(&lp->lp_peer_list))
2560 flags = LNET_PEER_DISCOVERED;
2561 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2562 flags |= LNET_PEER_MULTI_RAIL;
2565 * Check whether the primary NID in the message matches the
2566 * primary NID of the peer. If it does, update the peer, if
2567 * it it does not, check whether there is already a peer with
2568 * that primary NID. If no such peer exists, try to update
2569 * the primary NID of the current peer (allowed if it was
2570 * created due to message traffic) and complete the update.
2571 * If the peer did exist, hand off the data to it.
2573 * The peer for the loopback interface is a special case: this
2574 * is the peer for the local node, and we want to set its
2575 * primary NID to the correct value here.
2577 if (pbuf->pb_info.pi_nnis > 1)
2578 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2579 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2580 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2582 rc = lnet_peer_merge_data(lp, pbuf);
2583 } else if (lp->lp_primary_nid == nid) {
2584 rc = lnet_peer_merge_data(lp, pbuf);
2586 lpni = lnet_find_peer_ni_locked(nid);
2588 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2590 CERROR("Primary NID error %s versus %s: %d\n",
2591 libcfs_nid2str(lp->lp_primary_nid),
2592 libcfs_nid2str(nid), rc);
2594 rc = lnet_peer_merge_data(lp, pbuf);
2597 rc = lnet_peer_set_primary_data(
2598 lpni->lpni_peer_net->lpn_peer, pbuf);
2599 lnet_peer_ni_decref_locked(lpni);
2603 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2604 mutex_unlock(&the_lnet.ln_api_mutex);
2606 spin_lock(&lp->lp_lock);
2607 /* Tell discovery to re-check the peer immediately. */
2609 rc = LNET_REDISCOVER_PEER;
2614 * A ping failed. Clear the PING_FAILED state and set the
2615 * FORCE_PING state, to ensure a retry even if discovery is
2616 * disabled. This avoids being left with incorrect state.
2618 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2619 __must_hold(&lp->lp_lock)
2621 lnet_handle_md_t mdh;
2624 mdh = lp->lp_ping_mdh;
2625 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2626 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2627 lp->lp_state |= LNET_PEER_FORCE_PING;
2628 rc = lp->lp_ping_error;
2629 lp->lp_ping_error = 0;
2630 spin_unlock(&lp->lp_lock);
2632 if (!LNetMDHandleIsInvalid(mdh))
2635 CDEBUG(D_NET, "peer %s:%d\n",
2636 libcfs_nid2str(lp->lp_primary_nid), rc);
2638 spin_lock(&lp->lp_lock);
2639 return rc ? rc : LNET_REDISCOVER_PEER;
2643 * Select NID to send a Ping or Push to.
2645 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2647 struct lnet_peer_ni *lpni;
2649 /* Look for a direct-connected NID for this peer. */
2651 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2652 if (!lnet_is_peer_ni_healthy_locked(lpni))
2654 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2659 return lpni->lpni_nid;
2661 /* Look for a routed-connected NID for this peer. */
2663 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2664 if (!lnet_is_peer_ni_healthy_locked(lpni))
2666 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2671 return lpni->lpni_nid;
2673 return LNET_NID_ANY;
2676 /* Active side of ping. */
2677 static int lnet_peer_send_ping(struct lnet_peer *lp)
2678 __must_hold(&lp->lp_lock)
2680 lnet_md_t md = { NULL };
2681 lnet_process_id_t id;
2682 struct lnet_ping_buffer *pbuf;
2687 lp->lp_state |= LNET_PEER_PING_SENT;
2688 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2689 spin_unlock(&lp->lp_lock);
2691 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2692 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
2698 /* initialize md content */
2699 md.start = &pbuf->pb_info;
2700 md.length = LNET_PING_INFO_SIZE(nnis);
2701 md.threshold = 2; /* GET/REPLY */
2703 md.options = LNET_MD_TRUNCATE;
2705 md.eq_handle = the_lnet.ln_dc_eqh;
2707 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_ping_mdh);
2709 lnet_ping_buffer_decref(pbuf);
2710 CERROR("Can't bind MD: %d\n", rc);
2713 cpt = lnet_net_lock_current();
2714 /* Refcount for MD. */
2715 lnet_peer_addref_locked(lp);
2716 id.pid = LNET_PID_LUSTRE;
2717 id.nid = lnet_peer_select_nid(lp);
2718 lnet_net_unlock(cpt);
2720 if (id.nid == LNET_NID_ANY) {
2722 goto fail_unlink_md;
2725 rc = LNetGet(LNET_NID_ANY, lp->lp_ping_mdh, id,
2726 LNET_RESERVED_PORTAL,
2727 LNET_PROTO_PING_MATCHBITS, 0);
2730 goto fail_unlink_md;
2732 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2734 spin_lock(&lp->lp_lock);
2738 LNetMDUnlink(lp->lp_ping_mdh);
2739 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2741 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2743 * The errors that get us here are considered hard errors and
2744 * cause Discovery to terminate. So we clear PING_SENT, but do
2745 * not set either PING_FAILED or FORCE_PING. In fact we need
2746 * to clear PING_FAILED, because the unlink event handler will
2747 * have set it if we called LNetMDUnlink() above.
2749 spin_lock(&lp->lp_lock);
2750 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2755 * This function exists because you cannot call LNetMDUnlink() from an
2758 static int lnet_peer_push_failed(struct lnet_peer *lp)
2759 __must_hold(&lp->lp_lock)
2761 lnet_handle_md_t mdh;
2764 mdh = lp->lp_push_mdh;
2765 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2766 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2767 rc = lp->lp_push_error;
2768 lp->lp_push_error = 0;
2769 spin_unlock(&lp->lp_lock);
2771 if (!LNetMDHandleIsInvalid(mdh))
2774 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2775 spin_lock(&lp->lp_lock);
2776 return rc ? rc : LNET_REDISCOVER_PEER;
2779 /* Active side of push. */
2780 static int lnet_peer_send_push(struct lnet_peer *lp)
2781 __must_hold(&lp->lp_lock)
2783 struct lnet_ping_buffer *pbuf;
2784 lnet_process_id_t id;
2789 /* Don't push to a non-multi-rail peer. */
2790 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2791 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2795 lp->lp_state |= LNET_PEER_PUSH_SENT;
2796 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2797 spin_unlock(&lp->lp_lock);
2799 cpt = lnet_net_lock_current();
2800 pbuf = the_lnet.ln_ping_target;
2801 lnet_ping_buffer_addref(pbuf);
2802 lnet_net_unlock(cpt);
2804 /* Push source MD */
2805 md.start = &pbuf->pb_info;
2806 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2807 md.threshold = 2; /* Put/Ack */
2810 md.eq_handle = the_lnet.ln_dc_eqh;
2813 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2815 lnet_ping_buffer_decref(pbuf);
2816 CERROR("Can't bind push source MD: %d\n", rc);
2819 cpt = lnet_net_lock_current();
2820 /* Refcount for MD. */
2821 lnet_peer_addref_locked(lp);
2822 id.pid = LNET_PID_LUSTRE;
2823 id.nid = lnet_peer_select_nid(lp);
2824 lnet_net_unlock(cpt);
2826 if (id.nid == LNET_NID_ANY) {
2831 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2832 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2833 LNET_PROTO_PING_MATCHBITS, 0, 0);
2838 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2840 spin_lock(&lp->lp_lock);
2844 LNetMDUnlink(lp->lp_push_mdh);
2845 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2847 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2849 * The errors that get us here are considered hard errors and
2850 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2851 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2852 * because the unlink event handler will have set it if we
2853 * called LNetMDUnlink() above.
2855 spin_lock(&lp->lp_lock);
2856 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2861 * An unrecoverable error was encountered during discovery.
2862 * Set error status in peer and abort discovery.
2864 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2866 CDEBUG(D_NET, "Discovery error %s: %d\n",
2867 libcfs_nid2str(lp->lp_primary_nid), error);
2869 spin_lock(&lp->lp_lock);
2870 lp->lp_dc_error = error;
2871 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2872 lp->lp_state |= LNET_PEER_REDISCOVER;
2873 spin_unlock(&lp->lp_lock);
2877 * Mark the peer as discovered.
2879 static int lnet_peer_discovered(struct lnet_peer *lp)
2880 __must_hold(&lp->lp_lock)
2882 lp->lp_state |= LNET_PEER_DISCOVERED;
2883 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2884 LNET_PEER_REDISCOVER);
2886 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2892 * Mark the peer as to be rediscovered.
2894 static int lnet_peer_rediscover(struct lnet_peer *lp)
2895 __must_hold(&lp->lp_lock)
2897 lp->lp_state |= LNET_PEER_REDISCOVER;
2898 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2900 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2906 * Returns the first peer on the ln_dc_working queue if its timeout
2907 * has expired. Takes the current time as an argument so as to not
2908 * obsessively re-check the clock. The oldest discovery request will
2909 * be at the head of the queue.
2911 static struct lnet_peer *lnet_peer_dc_timed_out(time64_t now)
2913 struct lnet_peer *lp;
2915 if (list_empty(&the_lnet.ln_dc_working))
2917 lp = list_first_entry(&the_lnet.ln_dc_working,
2918 struct lnet_peer, lp_dc_list);
2919 if (now < lp->lp_last_queued + DEFAULT_PEER_TIMEOUT)
2925 * Discovering this peer is taking too long. Cancel any Ping or Push
2926 * that discovery is waiting on by unlinking the relevant MDs. The
2927 * lnet_discovery_event_handler() will proceed from here and complete
2930 static void lnet_peer_discovery_timeout(struct lnet_peer *lp)
2932 struct lnet_handle_md ping_mdh;
2933 struct lnet_handle_md push_mdh;
2935 LNetInvalidateMDHandle(&ping_mdh);
2936 LNetInvalidateMDHandle(&push_mdh);
2938 spin_lock(&lp->lp_lock);
2939 if (lp->lp_state & LNET_PEER_PING_SENT) {
2940 ping_mdh = lp->lp_ping_mdh;
2941 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2943 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2944 push_mdh = lp->lp_push_mdh;
2945 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2947 spin_unlock(&lp->lp_lock);
2949 if (!LNetMDHandleIsInvalid(ping_mdh))
2950 LNetMDUnlink(ping_mdh);
2951 if (!LNetMDHandleIsInvalid(push_mdh))
2952 LNetMDUnlink(push_mdh);
2956 * Wait for work to be queued or some other change that must be
2957 * attended to. Returns non-zero if the discovery thread should shut
2960 static int lnet_peer_discovery_wait_for_work(void)
2967 cpt = lnet_net_lock_current();
2969 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
2970 TASK_INTERRUPTIBLE);
2971 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2973 if (lnet_push_target_resize_needed())
2975 if (!list_empty(&the_lnet.ln_dc_request))
2977 if (lnet_peer_dc_timed_out(ktime_get_real_seconds()))
2979 lnet_net_unlock(cpt);
2982 * wakeup max every second to check if there are peers that
2983 * have been stuck on the working queue for greater than
2986 schedule_timeout(cfs_time_seconds(1));
2987 finish_wait(&the_lnet.ln_dc_waitq, &wait);
2988 cpt = lnet_net_lock_current();
2990 finish_wait(&the_lnet.ln_dc_waitq, &wait);
2992 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2995 lnet_net_unlock(cpt);
2997 CDEBUG(D_NET, "woken: %d\n", rc);
3002 /* The discovery thread. */
3003 static int lnet_peer_discovery(void *arg)
3005 struct lnet_peer *lp;
3009 CDEBUG(D_NET, "started\n");
3010 cfs_block_allsigs();
3013 if (lnet_peer_discovery_wait_for_work())
3016 if (lnet_push_target_resize_needed())
3017 lnet_push_target_resize();
3019 lnet_net_lock(LNET_LOCK_EX);
3020 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3024 * Process all incoming discovery work requests. When
3025 * discovery must wait on a peer to change state, it
3026 * is added to the tail of the ln_dc_working queue. A
3027 * timestamp keeps track of when the peer was added,
3028 * so we can time out discovery requests that take too
3031 while (!list_empty(&the_lnet.ln_dc_request)) {
3032 lp = list_first_entry(&the_lnet.ln_dc_request,
3033 struct lnet_peer, lp_dc_list);
3034 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3036 * set the time the peer was put on the dc_working
3037 * queue. It shouldn't remain on the queue
3038 * forever, in case the GET message (for ping)
3039 * doesn't get a REPLY or the PUT message (for
3040 * push) doesn't get an ACK.
3042 * TODO: LNet Health will deal with this scenario
3045 lp->lp_last_queued = ktime_get_real_seconds();
3046 lnet_net_unlock(LNET_LOCK_EX);
3049 * Select an action depending on the state of
3050 * the peer and whether discovery is disabled.
3051 * The check whether discovery is disabled is
3052 * done after the code that handles processing
3053 * for arrived data, cleanup for failures, and
3054 * forcing a Ping or Push.
3056 spin_lock(&lp->lp_lock);
3057 CDEBUG(D_NET, "peer %s state %#x\n",
3058 libcfs_nid2str(lp->lp_primary_nid),
3060 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3061 rc = lnet_peer_data_present(lp);
3062 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3063 rc = lnet_peer_ping_failed(lp);
3064 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3065 rc = lnet_peer_push_failed(lp);
3066 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3067 rc = lnet_peer_send_ping(lp);
3068 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3069 rc = lnet_peer_send_push(lp);
3070 else if (lnet_peer_discovery_disabled)
3071 rc = lnet_peer_rediscover(lp);
3072 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3073 rc = lnet_peer_send_ping(lp);
3074 else if (lnet_peer_needs_push(lp))
3075 rc = lnet_peer_send_push(lp);
3077 rc = lnet_peer_discovered(lp);
3078 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3079 libcfs_nid2str(lp->lp_primary_nid),
3081 spin_unlock(&lp->lp_lock);
3083 lnet_net_lock(LNET_LOCK_EX);
3084 if (rc == LNET_REDISCOVER_PEER) {
3085 list_move(&lp->lp_dc_list,
3086 &the_lnet.ln_dc_request);
3088 lnet_peer_discovery_error(lp, rc);
3090 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3091 lnet_peer_discovery_complete(lp);
3092 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3097 * Now that the ln_dc_request queue has been emptied
3098 * check the ln_dc_working queue for peers that are
3099 * taking too long. Move all that are found to the
3100 * ln_dc_expired queue and time out any pending
3101 * Ping or Push. We have to drop the lnet_net_lock
3102 * in the loop because lnet_peer_discovery_timeout()
3103 * calls LNetMDUnlink().
3105 now = ktime_get_real_seconds();
3106 while ((lp = lnet_peer_dc_timed_out(now)) != NULL) {
3107 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3108 lnet_net_unlock(LNET_LOCK_EX);
3109 lnet_peer_discovery_timeout(lp);
3110 lnet_net_lock(LNET_LOCK_EX);
3113 lnet_net_unlock(LNET_LOCK_EX);
3116 CDEBUG(D_NET, "stopping\n");
3118 * Clean up before telling lnet_peer_discovery_stop() that
3119 * we're done. Use wake_up() below to somewhat reduce the
3120 * size of the thundering herd if there are multiple threads
3121 * waiting on discovery of a single peer.
3123 LNetEQFree(the_lnet.ln_dc_eqh);
3124 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3126 /* Queue cleanup 1: stop all pending pings and pushes. */
3127 lnet_net_lock(LNET_LOCK_EX);
3128 while (!list_empty(&the_lnet.ln_dc_working)) {
3129 lp = list_first_entry(&the_lnet.ln_dc_working,
3130 struct lnet_peer, lp_dc_list);
3131 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3132 lnet_net_unlock(LNET_LOCK_EX);
3133 lnet_peer_discovery_timeout(lp);
3134 lnet_net_lock(LNET_LOCK_EX);
3136 lnet_net_unlock(LNET_LOCK_EX);
3138 /* Queue cleanup 2: wait for the expired queue to clear. */
3139 while (!list_empty(&the_lnet.ln_dc_expired))
3140 schedule_timeout(cfs_time_seconds(1));
3142 /* Queue cleanup 3: clear the request queue. */
3143 lnet_net_lock(LNET_LOCK_EX);
3144 while (!list_empty(&the_lnet.ln_dc_request)) {
3145 lp = list_first_entry(&the_lnet.ln_dc_request,
3146 struct lnet_peer, lp_dc_list);
3147 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3148 lnet_peer_discovery_complete(lp);
3150 lnet_net_unlock(LNET_LOCK_EX);
3152 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3153 wake_up(&the_lnet.ln_dc_waitq);
3155 CDEBUG(D_NET, "stopped\n");
3160 /* ln_api_mutex is held on entry. */
3161 int lnet_peer_discovery_start(void)
3163 struct task_struct *task;
3166 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3169 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3171 CERROR("Can't allocate discovery EQ: %d\n", rc);
3175 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3176 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3179 CERROR("Can't start peer discovery thread: %d\n", rc);
3181 LNetEQFree(the_lnet.ln_dc_eqh);
3182 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3184 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3187 CDEBUG(D_NET, "discovery start: %d\n", rc);
3192 /* ln_api_mutex is held on entry. */
3193 void lnet_peer_discovery_stop(void)
3195 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3198 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3199 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3200 wake_up(&the_lnet.ln_dc_waitq);
3202 wait_event(the_lnet.ln_dc_waitq,
3203 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3205 LASSERT(list_empty(&the_lnet.ln_dc_request));
3206 LASSERT(list_empty(&the_lnet.ln_dc_working));
3207 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3209 CDEBUG(D_NET, "discovery stopped\n");
3215 lnet_debug_peer(lnet_nid_t nid)
3217 char *aliveness = "NA";
3218 struct lnet_peer_ni *lp;
3221 cpt = lnet_cpt_of_nid(nid, NULL);
3224 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3226 lnet_net_unlock(cpt);
3227 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3231 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3232 aliveness = lp->lpni_alive ? "up" : "down";
3234 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3235 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3236 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3237 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3238 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3240 lnet_peer_ni_decref_locked(lp);
3242 lnet_net_unlock(cpt);
3245 /* Gathering information for userspace. */
3247 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3248 char aliveness[LNET_MAX_STR_LEN],
3249 __u32 *cpt_iter, __u32 *refcount,
3250 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3251 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3252 __u32 *peer_tx_qnob)
3254 struct lnet_peer_table *peer_table;
3255 struct lnet_peer_ni *lp;
3260 /* get the number of CPTs */
3261 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3263 /* if the cpt number to be examined is >= the number of cpts in
3264 * the system then indicate that there are no more cpts to examin
3266 if (*cpt_iter >= lncpt)
3269 /* get the current table */
3270 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3271 /* if the ptable is NULL then there are no more cpts to examine */
3272 if (peer_table == NULL)
3275 lnet_net_lock(*cpt_iter);
3277 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3278 struct list_head *peers = &peer_table->pt_hash[j];
3280 list_for_each_entry(lp, peers, lpni_hashlist) {
3281 if (peer_index-- > 0)
3284 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3285 if (lnet_isrouter(lp) ||
3286 lnet_peer_aliveness_enabled(lp))
3287 snprintf(aliveness, LNET_MAX_STR_LEN,
3288 lp->lpni_alive ? "up" : "down");
3290 *nid = lp->lpni_nid;
3291 *refcount = atomic_read(&lp->lpni_refcount);
3292 *ni_peer_tx_credits =
3293 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3294 *peer_tx_credits = lp->lpni_txcredits;
3295 *peer_rtr_credits = lp->lpni_rtrcredits;
3296 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3297 *peer_tx_qnob = lp->lpni_txqnob;
3303 lnet_net_unlock(*cpt_iter);
3307 return found ? 0 : -ENOENT;
3310 /* ln_api_mutex is held, which keeps the peer list stable */
3311 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3313 struct lnet_ioctl_element_stats *lpni_stats;
3314 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3315 struct lnet_peer_ni_credit_info *lpni_info;
3316 struct lnet_peer_ni *lpni;
3317 struct lnet_peer *lp;
3322 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3329 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3330 + sizeof(*lpni_msg_stats);
3331 size *= lp->lp_nnis;
3332 if (size > cfg->prcfg_size) {
3333 cfg->prcfg_size = size;
3338 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3339 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3340 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3341 cfg->prcfg_count = lp->lp_nnis;
3342 cfg->prcfg_size = size;
3343 cfg->prcfg_state = lp->lp_state;
3345 /* Allocate helper buffers. */
3347 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3350 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3353 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3354 if (!lpni_msg_stats)
3355 goto out_free_stats;
3360 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3361 nid = lpni->lpni_nid;
3362 if (copy_to_user(bulk, &nid, sizeof(nid)))
3363 goto out_free_msg_stats;
3364 bulk += sizeof(nid);
3366 memset(lpni_info, 0, sizeof(*lpni_info));
3367 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3368 if (lnet_isrouter(lpni) ||
3369 lnet_peer_aliveness_enabled(lpni))
3370 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3371 lpni->lpni_alive ? "up" : "down");
3373 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3374 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3375 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3376 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3377 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3378 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3379 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3380 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3381 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3382 goto out_free_msg_stats;
3383 bulk += sizeof(*lpni_info);
3385 memset(lpni_stats, 0, sizeof(*lpni_stats));
3386 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3387 LNET_STATS_TYPE_SEND);
3388 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3389 LNET_STATS_TYPE_RECV);
3390 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3391 LNET_STATS_TYPE_DROP);
3392 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3393 goto out_free_msg_stats;
3394 bulk += sizeof(*lpni_stats);
3395 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3396 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3397 goto out_free_msg_stats;
3398 bulk += sizeof(*lpni_msg_stats);
3403 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3405 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3407 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3409 lnet_peer_decref_locked(lp);