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);
271 * if there are messages still on the pending queue, then make
272 * sure to queue them on the ln_msg_resend list so they can be
273 * resent at a later point if the discovery thread is still
275 * If the discovery thread has stopped, then the wakeup will be a
276 * no-op, and it is expected the lnet_shutdown_lndnets() will
277 * eventually be called, which will traverse this list and
278 * finalize the messages on the list.
279 * We can not resend them now because we're holding the cpt lock.
280 * Releasing the lock can cause an inconsistent state
282 spin_lock(&the_lnet.ln_msg_resend_lock);
283 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
284 spin_unlock(&the_lnet.ln_msg_resend_lock);
285 wake_up(&the_lnet.ln_dc_waitq);
287 LIBCFS_FREE(lp, sizeof(*lp));
291 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
292 * that peer_net, detach the peer_net from the peer.
294 * Call with lnet_net_lock/EX held
297 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
299 struct lnet_peer_table *ptable;
300 struct lnet_peer_net *lpn;
301 struct lnet_peer *lp;
304 * Belts and suspenders: gracefully handle teardown of a
305 * partially connected peer_ni.
307 lpn = lpni->lpni_peer_net;
309 list_del_init(&lpni->lpni_peer_nis);
311 * If there are no lpni's left, we detach lpn from
312 * lp_peer_nets, so it cannot be found anymore.
314 if (list_empty(&lpn->lpn_peer_nis))
315 list_del_init(&lpn->lpn_peer_nets);
317 /* Update peer NID count. */
322 * If there are no more peer nets, make the peer unfindable
323 * via the peer_tables.
325 * Otherwise, if the peer is DISCOVERED, tell discovery to
326 * take another look at it. This is a no-op if discovery for
327 * this peer did the detaching.
329 if (list_empty(&lp->lp_peer_nets)) {
330 list_del_init(&lp->lp_peer_list);
331 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
333 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
334 /* Discovery isn't running, nothing to do here. */
335 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
336 lnet_peer_queue_for_discovery(lp);
337 wake_up(&the_lnet.ln_dc_waitq);
339 CDEBUG(D_NET, "peer %s NID %s\n",
340 libcfs_nid2str(lp->lp_primary_nid),
341 libcfs_nid2str(lpni->lpni_nid));
344 /* called with lnet_net_lock LNET_LOCK_EX held */
346 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
348 struct lnet_peer_table *ptable = NULL;
350 /* don't remove a peer_ni if it's also a gateway */
351 if (lpni->lpni_rtr_refcount > 0) {
352 CERROR("Peer NI %s is a gateway. Can not delete it\n",
353 libcfs_nid2str(lpni->lpni_nid));
357 lnet_peer_remove_from_remote_list(lpni);
359 /* remove peer ni from the hash list. */
360 list_del_init(&lpni->lpni_hashlist);
362 /* decrement the ref count on the peer table */
363 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
364 LASSERT(ptable->pt_number > 0);
368 * The peer_ni can no longer be found with a lookup. But there
369 * can be current users, so keep track of it on the zombie
370 * list until the reference count has gone to zero.
372 * The last reference may be lost in a place where the
373 * lnet_net_lock locks only a single cpt, and that cpt may not
374 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
377 spin_lock(&ptable->pt_zombie_lock);
378 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
379 ptable->pt_zombies++;
380 spin_unlock(&ptable->pt_zombie_lock);
382 /* no need to keep this peer_ni on the hierarchy anymore */
383 lnet_peer_detach_peer_ni_locked(lpni);
385 /* remove hashlist reference on peer_ni */
386 lnet_peer_ni_decref_locked(lpni);
391 void lnet_peer_uninit(void)
393 struct lnet_peer_ni *lpni, *tmp;
395 lnet_net_lock(LNET_LOCK_EX);
397 /* remove all peer_nis from the remote peer and the hash list */
398 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
399 lpni_on_remote_peer_ni_list)
400 lnet_peer_ni_del_locked(lpni);
402 lnet_peer_tables_destroy();
404 lnet_net_unlock(LNET_LOCK_EX);
408 lnet_peer_del_locked(struct lnet_peer *peer)
410 struct lnet_peer_ni *lpni = NULL, *lpni2;
413 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
415 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
416 while (lpni != NULL) {
417 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
418 rc = lnet_peer_ni_del_locked(lpni);
428 lnet_peer_del(struct lnet_peer *peer)
430 lnet_net_lock(LNET_LOCK_EX);
431 lnet_peer_del_locked(peer);
432 lnet_net_unlock(LNET_LOCK_EX);
438 * Delete a NID from a peer. Call with ln_api_mutex held.
441 * -EPERM: Non-DLC deletion from DLC-configured peer.
442 * -ENOENT: No lnet_peer_ni corresponding to the nid.
443 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
444 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
447 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
449 struct lnet_peer_ni *lpni;
450 lnet_nid_t primary_nid = lp->lp_primary_nid;
453 if (!(flags & LNET_PEER_CONFIGURED)) {
454 if (lp->lp_state & LNET_PEER_CONFIGURED) {
459 lpni = lnet_find_peer_ni_locked(nid);
464 lnet_peer_ni_decref_locked(lpni);
465 if (lp != lpni->lpni_peer_net->lpn_peer) {
471 * This function only allows deletion of the primary NID if it
474 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
479 lnet_net_lock(LNET_LOCK_EX);
480 lnet_peer_ni_del_locked(lpni);
481 lnet_net_unlock(LNET_LOCK_EX);
484 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
485 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
491 lnet_peer_table_cleanup_locked(struct lnet_net *net,
492 struct lnet_peer_table *ptable)
495 struct lnet_peer_ni *next;
496 struct lnet_peer_ni *lpni;
497 struct lnet_peer *peer;
499 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
500 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
502 if (net != NULL && net != lpni->lpni_net)
505 peer = lpni->lpni_peer_net->lpn_peer;
506 if (peer->lp_primary_nid != lpni->lpni_nid) {
507 lnet_peer_ni_del_locked(lpni);
511 * Removing the primary NID implies removing
512 * the entire peer. Advance next beyond any
513 * peer_ni that belongs to the same peer.
515 list_for_each_entry_from(next, &ptable->pt_hash[i],
517 if (next->lpni_peer_net->lpn_peer != peer)
520 lnet_peer_del_locked(peer);
526 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
530 spin_lock(&ptable->pt_zombie_lock);
531 while (ptable->pt_zombies) {
532 spin_unlock(&ptable->pt_zombie_lock);
534 if (is_power_of_2(i)) {
536 "Waiting for %d zombies on peer table\n",
539 set_current_state(TASK_UNINTERRUPTIBLE);
540 schedule_timeout(cfs_time_seconds(1) >> 1);
541 spin_lock(&ptable->pt_zombie_lock);
543 spin_unlock(&ptable->pt_zombie_lock);
547 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
548 struct lnet_peer_table *ptable)
550 struct lnet_peer_ni *lp;
551 struct lnet_peer_ni *tmp;
555 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
556 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
558 if (net != lp->lpni_net)
561 if (lp->lpni_rtr_refcount == 0)
564 lpni_nid = lp->lpni_nid;
566 lnet_net_unlock(LNET_LOCK_EX);
567 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
568 lnet_net_lock(LNET_LOCK_EX);
574 lnet_peer_tables_cleanup(struct lnet_net *net)
577 struct lnet_peer_table *ptable;
579 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
580 /* If just deleting the peers for a NI, get rid of any routes these
581 * peers are gateways for. */
582 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
583 lnet_net_lock(LNET_LOCK_EX);
584 lnet_peer_table_del_rtrs_locked(net, ptable);
585 lnet_net_unlock(LNET_LOCK_EX);
588 /* Start the cleanup process */
589 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
590 lnet_net_lock(LNET_LOCK_EX);
591 lnet_peer_table_cleanup_locked(net, ptable);
592 lnet_net_unlock(LNET_LOCK_EX);
595 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
596 lnet_peer_ni_finalize_wait(ptable);
599 static struct lnet_peer_ni *
600 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
602 struct list_head *peers;
603 struct lnet_peer_ni *lp;
605 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
607 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
608 list_for_each_entry(lp, peers, lpni_hashlist) {
609 if (lp->lpni_nid == nid) {
610 lnet_peer_ni_addref_locked(lp);
618 struct lnet_peer_ni *
619 lnet_find_peer_ni_locked(lnet_nid_t nid)
621 struct lnet_peer_ni *lpni;
622 struct lnet_peer_table *ptable;
625 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
627 ptable = the_lnet.ln_peer_tables[cpt];
628 lpni = lnet_get_peer_ni_locked(ptable, nid);
634 lnet_find_peer(lnet_nid_t nid)
636 struct lnet_peer_ni *lpni;
637 struct lnet_peer *lp = NULL;
640 cpt = lnet_net_lock_current();
641 lpni = lnet_find_peer_ni_locked(nid);
643 lp = lpni->lpni_peer_net->lpn_peer;
644 lnet_peer_addref_locked(lp);
645 lnet_peer_ni_decref_locked(lpni);
647 lnet_net_unlock(cpt);
652 struct lnet_peer_ni *
653 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
654 struct lnet_peer_net *peer_net,
655 struct lnet_peer_ni *prev)
657 struct lnet_peer_ni *lpni;
658 struct lnet_peer_net *net = peer_net;
662 if (list_empty(&peer->lp_peer_nets))
665 net = list_entry(peer->lp_peer_nets.next,
666 struct lnet_peer_net,
669 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
675 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
677 * if you reached the end of the peer ni list and the peer
678 * net is specified then there are no more peer nis in that
685 * we reached the end of this net ni list. move to the
688 if (prev->lpni_peer_net->lpn_peer_nets.next ==
690 /* no more nets and no more NIs. */
693 /* get the next net */
694 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
695 struct lnet_peer_net,
697 /* get the ni on it */
698 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
704 /* there are more nis left */
705 lpni = list_entry(prev->lpni_peer_nis.next,
706 struct lnet_peer_ni, lpni_peer_nis);
711 /* Call with the ln_api_mutex held */
713 lnet_get_peer_list(__u32 *countp, __u32 *sizep, lnet_process_id_t __user *ids)
715 lnet_process_id_t id;
716 struct lnet_peer_table *ptable;
717 struct lnet_peer *lp;
726 if (the_lnet.ln_state != LNET_STATE_RUNNING)
729 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
732 * Count the number of peers, and return E2BIG if the buffer
733 * is too small. We'll also return the desired size.
736 for (cpt = 0; cpt < lncpt; cpt++) {
737 ptable = the_lnet.ln_peer_tables[cpt];
738 count += ptable->pt_peers;
740 size = count * sizeof(*ids);
745 * Walk the peer lists and copy out the primary nids.
746 * This is safe because the peer lists are only modified
747 * while the ln_api_mutex is held. So we don't need to
748 * hold the lnet_net_lock as well, and can therefore
749 * directly call copy_to_user().
752 memset(&id, 0, sizeof(id));
753 id.pid = LNET_PID_LUSTRE;
755 for (cpt = 0; cpt < lncpt; cpt++) {
756 ptable = the_lnet.ln_peer_tables[cpt];
757 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
760 id.nid = lp->lp_primary_nid;
761 if (copy_to_user(&ids[i], &id, sizeof(id)))
774 * Start pushes to peers that need to be updated for a configuration
775 * change on this node.
778 lnet_push_update_to_peers(int force)
780 struct lnet_peer_table *ptable;
781 struct lnet_peer *lp;
785 lnet_net_lock(LNET_LOCK_EX);
786 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
787 for (cpt = 0; cpt < lncpt; cpt++) {
788 ptable = the_lnet.ln_peer_tables[cpt];
789 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
791 spin_lock(&lp->lp_lock);
792 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
793 lp->lp_state |= LNET_PEER_FORCE_PUSH;
794 spin_unlock(&lp->lp_lock);
796 if (lnet_peer_needs_push(lp))
797 lnet_peer_queue_for_discovery(lp);
800 lnet_net_unlock(LNET_LOCK_EX);
801 wake_up(&the_lnet.ln_dc_waitq);
805 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
806 * this is a preferred point-to-point path. Call with lnet_net_lock in
810 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
814 if (lpni->lpni_pref_nnids == 0)
816 if (lpni->lpni_pref_nnids == 1)
817 return lpni->lpni_pref.nid == nid;
818 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
819 if (lpni->lpni_pref.nids[i] == nid)
826 * Set a single ni as preferred, provided no preferred ni is already
827 * defined. Only to be used for non-multi-rail peer_ni.
830 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
834 spin_lock(&lpni->lpni_lock);
835 if (nid == LNET_NID_ANY) {
837 } else if (lpni->lpni_pref_nnids > 0) {
839 } else if (lpni->lpni_pref_nnids == 0) {
840 lpni->lpni_pref.nid = nid;
841 lpni->lpni_pref_nnids = 1;
842 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
844 spin_unlock(&lpni->lpni_lock);
846 CDEBUG(D_NET, "peer %s nid %s: %d\n",
847 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
852 * Clear the preferred NID from a non-multi-rail peer_ni, provided
853 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
856 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
860 spin_lock(&lpni->lpni_lock);
861 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
862 lpni->lpni_pref_nnids = 0;
863 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
864 } else if (lpni->lpni_pref_nnids == 0) {
869 spin_unlock(&lpni->lpni_lock);
871 CDEBUG(D_NET, "peer %s: %d\n",
872 libcfs_nid2str(lpni->lpni_nid), rc);
877 * Clear the preferred NIDs from a non-multi-rail peer.
880 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
882 struct lnet_peer_ni *lpni = NULL;
884 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
885 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
889 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
891 lnet_nid_t *nids = NULL;
892 lnet_nid_t *oldnids = NULL;
893 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
898 if (nid == LNET_NID_ANY) {
903 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
908 /* A non-MR node may have only one preferred NI per peer_ni */
909 if (lpni->lpni_pref_nnids > 0) {
910 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
916 if (lpni->lpni_pref_nnids != 0) {
917 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
918 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
923 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
924 if (lpni->lpni_pref.nids[i] == nid) {
925 LIBCFS_FREE(nids, size);
929 nids[i] = lpni->lpni_pref.nids[i];
934 lnet_net_lock(LNET_LOCK_EX);
935 spin_lock(&lpni->lpni_lock);
936 if (lpni->lpni_pref_nnids == 0) {
937 lpni->lpni_pref.nid = nid;
939 oldnids = lpni->lpni_pref.nids;
940 lpni->lpni_pref.nids = nids;
942 lpni->lpni_pref_nnids++;
943 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
944 spin_unlock(&lpni->lpni_lock);
945 lnet_net_unlock(LNET_LOCK_EX);
948 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
949 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
952 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
953 spin_lock(&lpni->lpni_lock);
954 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
955 spin_unlock(&lpni->lpni_lock);
957 CDEBUG(D_NET, "peer %s nid %s: %d\n",
958 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
963 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
965 lnet_nid_t *nids = NULL;
966 lnet_nid_t *oldnids = NULL;
967 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
972 if (lpni->lpni_pref_nnids == 0) {
977 if (lpni->lpni_pref_nnids == 1) {
978 if (lpni->lpni_pref.nid != nid) {
982 } else if (lpni->lpni_pref_nnids == 2) {
983 if (lpni->lpni_pref.nids[0] != nid &&
984 lpni->lpni_pref.nids[1] != nid) {
989 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
990 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
995 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
996 if (lpni->lpni_pref.nids[i] != nid)
998 nids[j++] = lpni->lpni_pref.nids[i];
1000 /* Check if we actually removed a nid. */
1001 if (j == lpni->lpni_pref_nnids) {
1002 LIBCFS_FREE(nids, size);
1008 lnet_net_lock(LNET_LOCK_EX);
1009 spin_lock(&lpni->lpni_lock);
1010 if (lpni->lpni_pref_nnids == 1) {
1011 lpni->lpni_pref.nid = LNET_NID_ANY;
1012 } else if (lpni->lpni_pref_nnids == 2) {
1013 oldnids = lpni->lpni_pref.nids;
1014 if (oldnids[0] == nid)
1015 lpni->lpni_pref.nid = oldnids[1];
1017 lpni->lpni_pref.nid = oldnids[2];
1019 oldnids = lpni->lpni_pref.nids;
1020 lpni->lpni_pref.nids = nids;
1022 lpni->lpni_pref_nnids--;
1023 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1024 spin_unlock(&lpni->lpni_lock);
1025 lnet_net_unlock(LNET_LOCK_EX);
1028 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1029 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1032 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1033 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1038 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1040 struct lnet_peer_ni *lpni;
1041 lnet_nid_t primary_nid = nid;
1043 lpni = lnet_find_peer_ni_locked(nid);
1045 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1046 lnet_peer_ni_decref_locked(lpni);
1053 LNetPrimaryNID(lnet_nid_t nid)
1055 struct lnet_peer *lp;
1056 struct lnet_peer_ni *lpni;
1057 lnet_nid_t primary_nid = nid;
1061 cpt = lnet_net_lock_current();
1062 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1067 lp = lpni->lpni_peer_net->lpn_peer;
1068 while (!lnet_peer_is_uptodate(lp)) {
1069 rc = lnet_discover_peer_locked(lpni, cpt, true);
1072 lp = lpni->lpni_peer_net->lpn_peer;
1074 primary_nid = lp->lp_primary_nid;
1076 lnet_peer_ni_decref_locked(lpni);
1078 lnet_net_unlock(cpt);
1080 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1081 libcfs_nid2str(primary_nid), rc);
1084 EXPORT_SYMBOL(LNetPrimaryNID);
1086 struct lnet_peer_net *
1087 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1089 struct lnet_peer_net *peer_net;
1090 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1091 if (peer_net->lpn_net_id == net_id)
1098 * Attach a peer_ni to a peer_net and peer. This function assumes
1099 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1100 * may be attached to a different peer, in which case it will be
1101 * properly detached first. The whole operation is done atomically.
1103 * Always returns 0. This is the last function called from functions
1104 * that do return an int, so returning 0 here allows the compiler to
1108 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1109 struct lnet_peer_net *lpn,
1110 struct lnet_peer_ni *lpni,
1113 struct lnet_peer_table *ptable;
1115 /* Install the new peer_ni */
1116 lnet_net_lock(LNET_LOCK_EX);
1117 /* Add peer_ni to global peer table hash, if necessary. */
1118 if (list_empty(&lpni->lpni_hashlist)) {
1119 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1121 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1122 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1123 ptable->pt_version++;
1124 ptable->pt_number++;
1125 /* This is the 1st refcount on lpni. */
1126 atomic_inc(&lpni->lpni_refcount);
1129 /* Detach the peer_ni from an existing peer, if necessary. */
1130 if (lpni->lpni_peer_net) {
1131 LASSERT(lpni->lpni_peer_net != lpn);
1132 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1133 lnet_peer_detach_peer_ni_locked(lpni);
1134 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1135 lpni->lpni_peer_net = NULL;
1138 /* Add peer_ni to peer_net */
1139 lpni->lpni_peer_net = lpn;
1140 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1141 lnet_peer_net_addref_locked(lpn);
1143 /* Add peer_net to peer */
1144 if (!lpn->lpn_peer) {
1146 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1147 lnet_peer_addref_locked(lp);
1150 /* Add peer to global peer list, if necessary */
1151 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1152 if (list_empty(&lp->lp_peer_list)) {
1153 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1158 /* Update peer state */
1159 spin_lock(&lp->lp_lock);
1160 if (flags & LNET_PEER_CONFIGURED) {
1161 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1162 lp->lp_state |= LNET_PEER_CONFIGURED;
1164 if (flags & LNET_PEER_MULTI_RAIL) {
1165 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1166 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1167 lnet_peer_clr_non_mr_pref_nids(lp);
1170 spin_unlock(&lp->lp_lock);
1173 lnet_net_unlock(LNET_LOCK_EX);
1175 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1176 libcfs_nid2str(lp->lp_primary_nid),
1177 libcfs_nid2str(lpni->lpni_nid), flags);
1183 * Create a new peer, with nid as its primary nid.
1185 * Call with the lnet_api_mutex held.
1188 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1190 struct lnet_peer *lp;
1191 struct lnet_peer_net *lpn;
1192 struct lnet_peer_ni *lpni;
1195 LASSERT(nid != LNET_NID_ANY);
1198 * No need for the lnet_net_lock here, because the
1199 * lnet_api_mutex is held.
1201 lpni = lnet_find_peer_ni_locked(nid);
1203 /* A peer with this NID already exists. */
1204 lp = lpni->lpni_peer_net->lpn_peer;
1205 lnet_peer_ni_decref_locked(lpni);
1207 * This is an error if the peer was configured and the
1208 * primary NID differs or an attempt is made to change
1209 * the Multi-Rail flag. Otherwise the assumption is
1210 * that an existing peer is being modified.
1212 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1213 if (lp->lp_primary_nid != nid)
1215 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1219 /* Delete and recreate as a configured peer. */
1223 /* Create peer, peer_net, and peer_ni. */
1225 lp = lnet_peer_alloc(nid);
1228 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1231 lpni = lnet_peer_ni_alloc(nid);
1235 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1238 LIBCFS_FREE(lpn, sizeof(*lpn));
1240 LIBCFS_FREE(lp, sizeof(*lp));
1242 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1243 libcfs_nid2str(nid), flags, rc);
1248 * Add a NID to a peer. Call with ln_api_mutex held.
1251 * -EPERM: Non-DLC addition to a DLC-configured peer.
1252 * -EEXIST: The NID was configured by DLC for a different peer.
1253 * -ENOMEM: Out of memory.
1254 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1255 * non-multi-rail peer.
1258 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1260 struct lnet_peer_net *lpn;
1261 struct lnet_peer_ni *lpni;
1265 LASSERT(nid != LNET_NID_ANY);
1267 /* A configured peer can only be updated through configuration. */
1268 if (!(flags & LNET_PEER_CONFIGURED)) {
1269 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1276 * The MULTI_RAIL flag can be set but not cleared, because
1277 * that would leave the peer struct in an invalid state.
1279 if (flags & LNET_PEER_MULTI_RAIL) {
1280 spin_lock(&lp->lp_lock);
1281 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1282 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1283 lnet_peer_clr_non_mr_pref_nids(lp);
1285 spin_unlock(&lp->lp_lock);
1286 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1291 lpni = lnet_find_peer_ni_locked(nid);
1294 * A peer_ni already exists. This is only a problem if
1295 * it is not connected to this peer and was configured
1298 lnet_peer_ni_decref_locked(lpni);
1299 if (lpni->lpni_peer_net->lpn_peer == lp)
1301 if (lnet_peer_ni_is_configured(lpni)) {
1305 /* If this is the primary NID, destroy the peer. */
1306 if (lnet_peer_ni_is_primary(lpni)) {
1307 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1308 lpni = lnet_peer_ni_alloc(nid);
1315 lpni = lnet_peer_ni_alloc(nid);
1323 * Get the peer_net. Check that we're not adding a second
1324 * peer_ni on a peer_net of a non-multi-rail peer.
1326 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1328 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1333 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1338 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1341 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1342 if (!lpni->lpni_peer_net)
1343 LIBCFS_FREE(lpni, sizeof(*lpni));
1345 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1346 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1352 * Update the primary NID of a peer, if possible.
1354 * Call with the lnet_api_mutex held.
1357 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1359 lnet_nid_t old = lp->lp_primary_nid;
1362 if (lp->lp_primary_nid == nid)
1364 rc = lnet_peer_add_nid(lp, nid, flags);
1367 lp->lp_primary_nid = nid;
1369 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1370 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1375 * lpni creation initiated due to traffic either sending or receiving.
1378 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1380 struct lnet_peer *lp;
1381 struct lnet_peer_net *lpn;
1382 struct lnet_peer_ni *lpni;
1386 if (nid == LNET_NID_ANY) {
1391 /* lnet_net_lock is not needed here because ln_api_lock is held */
1392 lpni = lnet_find_peer_ni_locked(nid);
1395 * We must have raced with another thread. Since we
1396 * know next to nothing about a peer_ni created by
1397 * traffic, we just assume everything is ok and
1400 lnet_peer_ni_decref_locked(lpni);
1404 /* Create peer, peer_net, and peer_ni. */
1406 lp = lnet_peer_alloc(nid);
1409 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1412 lpni = lnet_peer_ni_alloc(nid);
1415 if (pref != LNET_NID_ANY)
1416 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1418 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1421 LIBCFS_FREE(lpn, sizeof(*lpn));
1423 LIBCFS_FREE(lp, sizeof(*lp));
1425 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1430 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1432 * This API handles the following combinations:
1433 * Create a peer with its primary NI if only the prim_nid is provided
1434 * Add a NID to a peer identified by the prim_nid. The peer identified
1435 * by the prim_nid must already exist.
1436 * The peer being created may be non-MR.
1438 * The caller must hold ln_api_mutex. This prevents the peer from
1439 * being created/modified/deleted by a different thread.
1442 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1444 struct lnet_peer *lp = NULL;
1445 struct lnet_peer_ni *lpni;
1448 /* The prim_nid must always be specified */
1449 if (prim_nid == LNET_NID_ANY)
1452 flags = LNET_PEER_CONFIGURED;
1454 flags |= LNET_PEER_MULTI_RAIL;
1457 * If nid isn't specified, we must create a new peer with
1458 * prim_nid as its primary nid.
1460 if (nid == LNET_NID_ANY)
1461 return lnet_peer_add(prim_nid, flags);
1463 /* Look up the prim_nid, which must exist. */
1464 lpni = lnet_find_peer_ni_locked(prim_nid);
1467 lnet_peer_ni_decref_locked(lpni);
1468 lp = lpni->lpni_peer_net->lpn_peer;
1470 /* Peer must have been configured. */
1471 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1472 CDEBUG(D_NET, "peer %s was not configured\n",
1473 libcfs_nid2str(prim_nid));
1477 /* Primary NID must match */
1478 if (lp->lp_primary_nid != prim_nid) {
1479 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1480 libcfs_nid2str(prim_nid),
1481 libcfs_nid2str(lp->lp_primary_nid));
1485 /* Multi-Rail flag must match. */
1486 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1487 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1488 libcfs_nid2str(prim_nid));
1492 return lnet_peer_add_nid(lp, nid, flags);
1496 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1498 * This API handles the following combinations:
1499 * Delete a NI from a peer if both prim_nid and nid are provided.
1500 * Delete a peer if only prim_nid is provided.
1501 * Delete a peer if its primary nid is provided.
1503 * The caller must hold ln_api_mutex. This prevents the peer from
1504 * being modified/deleted by a different thread.
1507 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1509 struct lnet_peer *lp;
1510 struct lnet_peer_ni *lpni;
1513 if (prim_nid == LNET_NID_ANY)
1516 lpni = lnet_find_peer_ni_locked(prim_nid);
1519 lnet_peer_ni_decref_locked(lpni);
1520 lp = lpni->lpni_peer_net->lpn_peer;
1522 if (prim_nid != lp->lp_primary_nid) {
1523 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1524 libcfs_nid2str(prim_nid),
1525 libcfs_nid2str(lp->lp_primary_nid));
1529 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1530 return lnet_peer_del(lp);
1532 flags = LNET_PEER_CONFIGURED;
1533 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1534 flags |= LNET_PEER_MULTI_RAIL;
1536 return lnet_peer_del_nid(lp, nid, flags);
1540 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1542 struct lnet_peer_table *ptable;
1543 struct lnet_peer_net *lpn;
1545 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1547 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1548 LASSERT(lpni->lpni_rtr_refcount == 0);
1549 LASSERT(list_empty(&lpni->lpni_txq));
1550 LASSERT(lpni->lpni_txqnob == 0);
1551 LASSERT(list_empty(&lpni->lpni_peer_nis));
1552 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1554 lpn = lpni->lpni_peer_net;
1555 lpni->lpni_peer_net = NULL;
1556 lpni->lpni_net = NULL;
1558 /* remove the peer ni from the zombie list */
1559 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1560 spin_lock(&ptable->pt_zombie_lock);
1561 list_del_init(&lpni->lpni_hashlist);
1562 ptable->pt_zombies--;
1563 spin_unlock(&ptable->pt_zombie_lock);
1565 if (lpni->lpni_pref_nnids > 1) {
1566 LIBCFS_FREE(lpni->lpni_pref.nids,
1567 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1569 LIBCFS_FREE(lpni, sizeof(*lpni));
1571 lnet_peer_net_decref_locked(lpn);
1574 struct lnet_peer_ni *
1575 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1577 struct lnet_peer_ni *lpni = NULL;
1580 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1581 return ERR_PTR(-ESHUTDOWN);
1584 * find if a peer_ni already exists.
1585 * If so then just return that.
1587 lpni = lnet_find_peer_ni_locked(nid);
1591 lnet_net_unlock(cpt);
1593 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1596 goto out_net_relock;
1599 lpni = lnet_find_peer_ni_locked(nid);
1609 * Get a peer_ni for the given nid, create it if necessary. Takes a
1610 * hold on the peer_ni.
1612 struct lnet_peer_ni *
1613 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1615 struct lnet_peer_ni *lpni = NULL;
1618 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1619 return ERR_PTR(-ESHUTDOWN);
1622 * find if a peer_ni already exists.
1623 * If so then just return that.
1625 lpni = lnet_find_peer_ni_locked(nid);
1631 * use the lnet_api_mutex to serialize the creation of the peer_ni
1632 * and the creation/deletion of the local ni/net. When a local ni is
1633 * created, if there exists a set of peer_nis on that network,
1634 * they need to be traversed and updated. When a local NI is
1635 * deleted, which could result in a network being deleted, then
1636 * all peer nis on that network need to be removed as well.
1638 * Creation through traffic should also be serialized with
1639 * creation through DLC.
1641 lnet_net_unlock(cpt);
1642 mutex_lock(&the_lnet.ln_api_mutex);
1644 * Shutdown is only set under the ln_api_lock, so a single
1645 * check here is sufficent.
1647 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1648 lpni = ERR_PTR(-ESHUTDOWN);
1649 goto out_mutex_unlock;
1652 rc = lnet_peer_ni_traffic_add(nid, pref);
1655 goto out_mutex_unlock;
1658 lpni = lnet_find_peer_ni_locked(nid);
1662 mutex_unlock(&the_lnet.ln_api_mutex);
1665 /* Lock has been dropped, check again for shutdown. */
1666 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1668 lnet_peer_ni_decref_locked(lpni);
1669 lpni = ERR_PTR(-ESHUTDOWN);
1680 * Is a peer uptodate from the point of view of discovery?
1682 * If it is currently being processed, obviously not.
1683 * A forced Ping or Push is also handled by the discovery thread.
1685 * Otherwise look at whether the peer needs rediscovering.
1688 lnet_peer_is_uptodate(struct lnet_peer *lp)
1692 spin_lock(&lp->lp_lock);
1693 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1694 LNET_PEER_FORCE_PING |
1695 LNET_PEER_FORCE_PUSH)) {
1697 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1699 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1700 if (lnet_peer_discovery_disabled)
1704 } else if (lnet_peer_needs_push(lp)) {
1706 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1707 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1714 spin_unlock(&lp->lp_lock);
1720 * Queue a peer for the attention of the discovery thread. Call with
1721 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1722 * -EALREADY if the peer was already queued.
1724 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1728 spin_lock(&lp->lp_lock);
1729 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1730 lp->lp_state |= LNET_PEER_DISCOVERING;
1731 spin_unlock(&lp->lp_lock);
1732 if (list_empty(&lp->lp_dc_list)) {
1733 lnet_peer_addref_locked(lp);
1734 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1735 wake_up(&the_lnet.ln_dc_waitq);
1741 CDEBUG(D_NET, "Queue peer %s: %d\n",
1742 libcfs_nid2str(lp->lp_primary_nid), rc);
1748 * Discovery of a peer is complete. Wake all waiters on the peer.
1749 * Call with lnet_net_lock/EX held.
1751 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1753 struct lnet_msg *msg, *tmp;
1755 struct list_head pending_msgs;
1757 INIT_LIST_HEAD(&pending_msgs);
1759 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1760 libcfs_nid2str(lp->lp_primary_nid));
1762 list_del_init(&lp->lp_dc_list);
1763 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1764 wake_up_all(&lp->lp_dc_waitq);
1766 lnet_net_unlock(LNET_LOCK_EX);
1768 /* iterate through all pending messages and send them again */
1769 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1770 list_del_init(&msg->msg_list);
1771 if (lp->lp_dc_error) {
1772 lnet_finalize(msg, lp->lp_dc_error);
1776 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1777 lnet_msgtyp2str(msg->msg_type),
1778 libcfs_id2str(msg->msg_target));
1779 rc = lnet_send(msg->msg_src_nid_param, msg,
1780 msg->msg_rtr_nid_param);
1782 CNETERR("Error sending %s to %s: %d\n",
1783 lnet_msgtyp2str(msg->msg_type),
1784 libcfs_id2str(msg->msg_target), rc);
1785 lnet_finalize(msg, rc);
1788 lnet_net_lock(LNET_LOCK_EX);
1789 lnet_peer_decref_locked(lp);
1793 * Handle inbound push.
1794 * Like any event handler, called with lnet_res_lock/CPT held.
1796 void lnet_peer_push_event(struct lnet_event *ev)
1798 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1799 struct lnet_peer *lp;
1801 /* lnet_find_peer() adds a refcount */
1802 lp = lnet_find_peer(ev->source.nid);
1804 CERROR("Push Put from unknown %s (source %s)\n",
1805 libcfs_nid2str(ev->initiator.nid),
1806 libcfs_nid2str(ev->source.nid));
1810 /* Ensure peer state remains consistent while we modify it. */
1811 spin_lock(&lp->lp_lock);
1814 * If some kind of error happened the contents of the message
1815 * cannot be used. Clear the NIDS_UPTODATE and set the
1816 * FORCE_PING flag to trigger a ping.
1819 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1820 lp->lp_state |= LNET_PEER_FORCE_PING;
1821 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1823 libcfs_nid2str(lp->lp_primary_nid),
1824 libcfs_nid2str(ev->source.nid));
1829 * A push with invalid or corrupted info. Clear the UPTODATE
1830 * flag to trigger a ping.
1832 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1833 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1834 lp->lp_state |= LNET_PEER_FORCE_PING;
1835 CDEBUG(D_NET, "Corrupted Push from %s\n",
1836 libcfs_nid2str(lp->lp_primary_nid));
1841 * Make sure we'll allocate the correct size ping buffer when
1844 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1845 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1848 * A non-Multi-Rail peer is not supposed to be capable of
1851 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1852 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1853 libcfs_nid2str(lp->lp_primary_nid));
1858 * Check the MULTIRAIL flag. Complain if the peer was DLC
1859 * configured without it.
1861 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1862 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1863 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1864 libcfs_nid2str(lp->lp_primary_nid));
1866 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1867 lnet_peer_clr_non_mr_pref_nids(lp);
1872 * The peer may have discovery disabled at its end. Set
1873 * NO_DISCOVERY as appropriate.
1875 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1876 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1877 libcfs_nid2str(lp->lp_primary_nid));
1878 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1879 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1880 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1881 libcfs_nid2str(lp->lp_primary_nid));
1882 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1886 * Check for truncation of the Put message. Clear the
1887 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1888 * and tell discovery to allocate a bigger buffer.
1890 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1891 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1892 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1893 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1894 lp->lp_state |= LNET_PEER_FORCE_PING;
1895 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1896 libcfs_nid2str(lp->lp_primary_nid),
1897 pbuf->pb_info.pi_nnis);
1902 * Check whether the Put data is stale. Stale data can just be
1905 if (pbuf->pb_info.pi_nnis > 1 &&
1906 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1907 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1908 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1909 libcfs_nid2str(lp->lp_primary_nid),
1910 LNET_PING_BUFFER_SEQNO(pbuf),
1916 * Check whether the Put data is new, in which case we clear
1917 * the UPTODATE flag and prepare to process it.
1919 * If the Put data is current, and the peer is UPTODATE then
1920 * we assome everything is all right and drop the data as
1923 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1924 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1925 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1926 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1927 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1928 libcfs_nid2str(lp->lp_primary_nid),
1929 LNET_PING_BUFFER_SEQNO(pbuf),
1935 * If there is data present that hasn't been processed yet,
1936 * we'll replace it if the Put contained newer data and it
1937 * fits. We're racing with a Ping or earlier Push in this
1940 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1941 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1942 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1943 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1944 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1945 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1946 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1947 libcfs_nid2str(lp->lp_primary_nid),
1948 LNET_PING_BUFFER_SEQNO(pbuf),
1949 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1955 * Allocate a buffer to copy the data. On a failure we drop
1956 * the Push and set FORCE_PING to force the discovery
1957 * thread to fix the problem by pinging the peer.
1959 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
1961 lp->lp_state |= LNET_PEER_FORCE_PING;
1962 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
1963 libcfs_nid2str(lp->lp_primary_nid),
1964 LNET_PING_BUFFER_SEQNO(pbuf));
1969 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1970 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1971 lp->lp_state |= LNET_PEER_DATA_PRESENT;
1972 CDEBUG(D_NET, "Received Push %s %u\n",
1973 libcfs_nid2str(lp->lp_primary_nid),
1974 LNET_PING_BUFFER_SEQNO(pbuf));
1978 * Queue the peer for discovery, and wake the discovery thread
1979 * if the peer was already queued, because its status changed.
1981 spin_unlock(&lp->lp_lock);
1982 lnet_net_lock(LNET_LOCK_EX);
1983 if (lnet_peer_queue_for_discovery(lp))
1984 wake_up(&the_lnet.ln_dc_waitq);
1985 /* Drop refcount from lookup */
1986 lnet_peer_decref_locked(lp);
1987 lnet_net_unlock(LNET_LOCK_EX);
1991 * Clear the discovery error state, unless we're already discovering
1992 * this peer, in which case the error is current.
1994 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
1996 spin_lock(&lp->lp_lock);
1997 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1998 lp->lp_dc_error = 0;
1999 spin_unlock(&lp->lp_lock);
2003 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2004 * dropped/retaken within this function. An lnet_peer_ni is passed in
2005 * because discovery could tear down an lnet_peer.
2008 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2011 struct lnet_peer *lp;
2015 lnet_net_unlock(cpt);
2016 lnet_net_lock(LNET_LOCK_EX);
2017 lp = lpni->lpni_peer_net->lpn_peer;
2018 lnet_peer_clear_discovery_error(lp);
2021 * We're willing to be interrupted. The lpni can become a
2022 * zombie if we race with DLC, so we must check for that.
2025 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2026 if (signal_pending(current))
2028 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2030 if (lp->lp_dc_error)
2032 if (lnet_peer_is_uptodate(lp))
2034 lnet_peer_queue_for_discovery(lp);
2036 * if caller requested a non-blocking operation then
2037 * return immediately. Once discovery is complete then the
2038 * peer ref will be decremented and any pending messages
2039 * that were stopped due to discovery will be transmitted.
2044 lnet_peer_addref_locked(lp);
2045 lnet_net_unlock(LNET_LOCK_EX);
2047 finish_wait(&lp->lp_dc_waitq, &wait);
2048 lnet_net_lock(LNET_LOCK_EX);
2049 lnet_peer_decref_locked(lp);
2050 /* Peer may have changed */
2051 lp = lpni->lpni_peer_net->lpn_peer;
2053 finish_wait(&lp->lp_dc_waitq, &wait);
2055 lnet_net_unlock(LNET_LOCK_EX);
2059 * If the peer has changed after we've discovered the older peer,
2060 * then we need to discovery the new peer to make sure the
2061 * interface information is up to date
2063 if (lp != lpni->lpni_peer_net->lpn_peer)
2066 if (signal_pending(current))
2068 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2070 else if (lp->lp_dc_error)
2071 rc = lp->lp_dc_error;
2073 CDEBUG(D_NET, "non-blocking discovery\n");
2074 else if (!lnet_peer_is_uptodate(lp))
2077 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2078 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2079 libcfs_nid2str(lpni->lpni_nid), rc,
2080 (!block) ? "pending discovery" : "discovery complete");
2085 /* Handle an incoming ack for a push. */
2087 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2089 struct lnet_ping_buffer *pbuf;
2091 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2092 spin_lock(&lp->lp_lock);
2093 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2094 lp->lp_push_error = ev->status;
2096 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2098 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2099 spin_unlock(&lp->lp_lock);
2101 CDEBUG(D_NET, "peer %s ev->status %d\n",
2102 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2105 /* Handle a Reply message. This is the reply to a Ping message. */
2107 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2109 struct lnet_ping_buffer *pbuf;
2112 spin_lock(&lp->lp_lock);
2115 * If some kind of error happened the contents of message
2116 * cannot be used. Set PING_FAILED to trigger a retry.
2119 lp->lp_state |= LNET_PEER_PING_FAILED;
2120 lp->lp_ping_error = ev->status;
2121 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2123 libcfs_nid2str(lp->lp_primary_nid),
2124 libcfs_nid2str(ev->source.nid));
2128 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2129 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2130 lnet_swap_pinginfo(pbuf);
2133 * A reply with invalid or corrupted info. Set PING_FAILED to
2136 rc = lnet_ping_info_validate(&pbuf->pb_info);
2138 lp->lp_state |= LNET_PEER_PING_FAILED;
2139 lp->lp_ping_error = 0;
2140 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2141 libcfs_nid2str(lp->lp_primary_nid), rc);
2146 * Update the MULTI_RAIL flag based on the reply. If the peer
2147 * was configured with DLC then the setting should match what
2150 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2151 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2152 /* Everything's fine */
2153 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2154 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2155 libcfs_nid2str(lp->lp_primary_nid));
2157 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2158 lnet_peer_clr_non_mr_pref_nids(lp);
2160 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2161 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2162 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2163 libcfs_nid2str(lp->lp_primary_nid));
2165 CERROR("Multi-Rail state vanished from %s\n",
2166 libcfs_nid2str(lp->lp_primary_nid));
2167 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2172 * Make sure we'll allocate the correct size ping buffer when
2175 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2176 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2179 * The peer may have discovery disabled at its end. Set
2180 * NO_DISCOVERY as appropriate.
2182 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2183 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2184 libcfs_nid2str(lp->lp_primary_nid));
2185 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2186 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2187 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2188 libcfs_nid2str(lp->lp_primary_nid));
2189 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2193 * Check for truncation of the Reply. Clear PING_SENT and set
2194 * PING_FAILED to trigger a retry.
2196 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2197 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2198 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2199 lp->lp_state |= LNET_PEER_PING_FAILED;
2200 lp->lp_ping_error = 0;
2201 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2202 libcfs_nid2str(lp->lp_primary_nid),
2203 pbuf->pb_info.pi_nnis);
2208 * Check the sequence numbers in the reply. These are only
2209 * available if the reply came from a Multi-Rail peer.
2211 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2212 pbuf->pb_info.pi_nnis > 1 &&
2213 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2214 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2215 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2216 libcfs_nid2str(lp->lp_primary_nid),
2217 LNET_PING_BUFFER_SEQNO(pbuf),
2222 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2223 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2226 /* We're happy with the state of the data in the buffer. */
2227 CDEBUG(D_NET, "peer %s data present %u\n",
2228 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2229 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2230 lnet_ping_buffer_decref(lp->lp_data);
2232 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2233 lnet_ping_buffer_addref(pbuf);
2236 lp->lp_state &= ~LNET_PEER_PING_SENT;
2237 spin_unlock(&lp->lp_lock);
2241 * Send event handling. Only matters for error cases, where we clean
2242 * up state on the peer and peer_ni that would otherwise be updated in
2243 * the REPLY event handler for a successful Ping, and the ACK event
2244 * handler for a successful Push.
2247 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2254 spin_lock(&lp->lp_lock);
2255 if (ev->msg_type == LNET_MSG_GET) {
2256 lp->lp_state &= ~LNET_PEER_PING_SENT;
2257 lp->lp_state |= LNET_PEER_PING_FAILED;
2258 lp->lp_ping_error = ev->status;
2259 } else { /* ev->msg_type == LNET_MSG_PUT */
2260 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2261 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2262 lp->lp_push_error = ev->status;
2264 spin_unlock(&lp->lp_lock);
2265 rc = LNET_REDISCOVER_PEER;
2267 CDEBUG(D_NET, "%s Send to %s: %d\n",
2268 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2269 libcfs_nid2str(ev->target.nid), rc);
2274 * Unlink event handling. This event is only seen if a call to
2275 * LNetMDUnlink() caused the event to be unlinked. If this call was
2276 * made after the event was set up in LNetGet() or LNetPut() then we
2277 * assume the Ping or Push timed out.
2280 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2282 spin_lock(&lp->lp_lock);
2283 /* We've passed through LNetGet() */
2284 if (lp->lp_state & LNET_PEER_PING_SENT) {
2285 lp->lp_state &= ~LNET_PEER_PING_SENT;
2286 lp->lp_state |= LNET_PEER_PING_FAILED;
2287 lp->lp_ping_error = -ETIMEDOUT;
2288 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2289 libcfs_nid2str(lp->lp_primary_nid));
2291 /* We've passed through LNetPut() */
2292 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2293 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2294 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2295 lp->lp_push_error = -ETIMEDOUT;
2296 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2297 libcfs_nid2str(lp->lp_primary_nid));
2299 spin_unlock(&lp->lp_lock);
2303 * Event handler for the discovery EQ.
2305 * Called with lnet_res_lock(cpt) held. The cpt is the
2306 * lnet_cpt_of_cookie() of the md handle cookie.
2308 static void lnet_discovery_event_handler(lnet_event_t *event)
2310 struct lnet_peer *lp = event->md.user_ptr;
2311 struct lnet_ping_buffer *pbuf;
2314 /* discovery needs to take another look */
2315 rc = LNET_REDISCOVER_PEER;
2317 CDEBUG(D_NET, "Received event: %d\n", event->type);
2319 switch (event->type) {
2320 case LNET_EVENT_ACK:
2321 lnet_discovery_event_ack(lp, event);
2323 case LNET_EVENT_REPLY:
2324 lnet_discovery_event_reply(lp, event);
2326 case LNET_EVENT_SEND:
2327 /* Only send failure triggers a retry. */
2328 rc = lnet_discovery_event_send(lp, event);
2330 case LNET_EVENT_UNLINK:
2331 /* LNetMDUnlink() was called */
2332 lnet_discovery_event_unlink(lp, event);
2335 /* Invalid events. */
2338 lnet_net_lock(LNET_LOCK_EX);
2339 if (event->unlinked) {
2340 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2341 lnet_ping_buffer_decref(pbuf);
2342 lnet_peer_decref_locked(lp);
2344 if (rc == LNET_REDISCOVER_PEER) {
2345 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2346 wake_up(&the_lnet.ln_dc_waitq);
2348 lnet_net_unlock(LNET_LOCK_EX);
2352 * Build a peer from incoming data.
2354 * The NIDs in the incoming data are supposed to be structured as follows:
2357 * - other NIDs in same net
2358 * - NIDs in second net
2359 * - NIDs in third net
2361 * This due to the way the list of NIDs in the data is created.
2363 * Note that this function will mark the peer uptodate unless an
2364 * ENOMEM is encontered. All other errors are due to a conflict
2365 * between the DLC configuration and what discovery sees. We treat DLC
2366 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2367 * peer from becoming stuck in discovery.
2369 static int lnet_peer_merge_data(struct lnet_peer *lp,
2370 struct lnet_ping_buffer *pbuf)
2372 struct lnet_peer_ni *lpni;
2373 lnet_nid_t *curnis = NULL;
2374 lnet_nid_t *addnis = NULL;
2375 lnet_nid_t *delnis = NULL;
2385 flags = LNET_PEER_DISCOVERED;
2386 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2387 flags |= LNET_PEER_MULTI_RAIL;
2389 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2390 LIBCFS_ALLOC(curnis, nnis * sizeof(lnet_nid_t));
2391 LIBCFS_ALLOC(addnis, nnis * sizeof(lnet_nid_t));
2392 LIBCFS_ALLOC(delnis, nnis * sizeof(lnet_nid_t));
2393 if (!curnis || !addnis || !delnis) {
2401 /* Construct the list of NIDs present in peer. */
2403 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2404 curnis[ncurnis++] = lpni->lpni_nid;
2407 * Check for NIDs in pbuf not present in curnis[].
2408 * The loop starts at 1 to skip the loopback NID.
2410 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2411 for (j = 0; j < ncurnis; j++)
2412 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2415 addnis[naddnis++] = pbuf->pb_info.pi_ni[i].ns_nid;
2418 * Check for NIDs in curnis[] not present in pbuf.
2419 * The nested loop starts at 1 to skip the loopback NID.
2421 * But never add the loopback NID to delnis[]: if it is
2422 * present in curnis[] then this peer is for this node.
2424 for (i = 0; i < ncurnis; i++) {
2425 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2427 for (j = 1; j < pbuf->pb_info.pi_nnis; j++)
2428 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid)
2430 if (j == pbuf->pb_info.pi_nnis)
2431 delnis[ndelnis++] = curnis[i];
2434 for (i = 0; i < naddnis; i++) {
2435 rc = lnet_peer_add_nid(lp, addnis[i], flags);
2437 CERROR("Error adding NID %s to peer %s: %d\n",
2438 libcfs_nid2str(addnis[i]),
2439 libcfs_nid2str(lp->lp_primary_nid), rc);
2444 for (i = 0; i < ndelnis; i++) {
2445 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2447 CERROR("Error deleting NID %s from peer %s: %d\n",
2448 libcfs_nid2str(delnis[i]),
2449 libcfs_nid2str(lp->lp_primary_nid), rc);
2455 * Errors other than -ENOMEM are due to peers having been
2456 * configured with DLC. Ignore these because DLC overrides
2461 LIBCFS_FREE(curnis, nnis * sizeof(lnet_nid_t));
2462 LIBCFS_FREE(addnis, nnis * sizeof(lnet_nid_t));
2463 LIBCFS_FREE(delnis, nnis * sizeof(lnet_nid_t));
2464 lnet_ping_buffer_decref(pbuf);
2465 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2468 spin_lock(&lp->lp_lock);
2469 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2470 lp->lp_state |= LNET_PEER_FORCE_PING;
2471 spin_unlock(&lp->lp_lock);
2477 * The data in pbuf says lp is its primary peer, but the data was
2478 * received by a different peer. Try to update lp with the data.
2481 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2483 lnet_handle_md_t mdh;
2485 /* Queue lp for discovery, and force it on the request queue. */
2486 lnet_net_lock(LNET_LOCK_EX);
2487 if (lnet_peer_queue_for_discovery(lp))
2488 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2489 lnet_net_unlock(LNET_LOCK_EX);
2491 LNetInvalidateMDHandle(&mdh);
2494 * Decide whether we can move the peer to the DATA_PRESENT state.
2496 * We replace stale data for a multi-rail peer, repair PING_FAILED
2497 * status, and preempt FORCE_PING.
2499 * If after that we have DATA_PRESENT, we merge it into this peer.
2501 spin_lock(&lp->lp_lock);
2502 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2503 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2504 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2505 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2506 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2507 lnet_ping_buffer_decref(pbuf);
2512 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2513 lnet_ping_buffer_decref(lp->lp_data);
2515 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2517 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2518 mdh = lp->lp_ping_mdh;
2519 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2520 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2521 lp->lp_ping_error = 0;
2523 if (lp->lp_state & LNET_PEER_FORCE_PING)
2524 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2525 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2526 spin_unlock(&lp->lp_lock);
2528 if (!LNetMDHandleIsInvalid(mdh))
2532 return lnet_peer_merge_data(lp, pbuf);
2534 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2539 * Update a peer using the data received.
2541 static int lnet_peer_data_present(struct lnet_peer *lp)
2542 __must_hold(&lp->lp_lock)
2544 struct lnet_ping_buffer *pbuf;
2545 struct lnet_peer_ni *lpni;
2546 lnet_nid_t nid = LNET_NID_ANY;
2552 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2553 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2554 spin_unlock(&lp->lp_lock);
2557 * Modifications of peer structures are done while holding the
2558 * ln_api_mutex. A global lock is required because we may be
2559 * modifying multiple peer structures, and a mutex greatly
2560 * simplifies memory management.
2562 * The actual changes to the data structures must also protect
2563 * against concurrent lookups, for which the lnet_net_lock in
2564 * LNET_LOCK_EX mode is used.
2566 mutex_lock(&the_lnet.ln_api_mutex);
2567 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2573 * If this peer is not on the peer list then it is being torn
2574 * down, and our reference count may be all that is keeping it
2575 * alive. Don't do any work on it.
2577 if (list_empty(&lp->lp_peer_list))
2580 flags = LNET_PEER_DISCOVERED;
2581 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2582 flags |= LNET_PEER_MULTI_RAIL;
2585 * Check whether the primary NID in the message matches the
2586 * primary NID of the peer. If it does, update the peer, if
2587 * it it does not, check whether there is already a peer with
2588 * that primary NID. If no such peer exists, try to update
2589 * the primary NID of the current peer (allowed if it was
2590 * created due to message traffic) and complete the update.
2591 * If the peer did exist, hand off the data to it.
2593 * The peer for the loopback interface is a special case: this
2594 * is the peer for the local node, and we want to set its
2595 * primary NID to the correct value here. Moreover, this peer
2596 * can show up with only the loopback NID in the ping buffer.
2598 if (pbuf->pb_info.pi_nnis <= 1)
2600 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2601 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2602 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2604 rc = lnet_peer_merge_data(lp, pbuf);
2605 } else if (lp->lp_primary_nid == nid) {
2606 rc = lnet_peer_merge_data(lp, pbuf);
2608 lpni = lnet_find_peer_ni_locked(nid);
2610 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2612 CERROR("Primary NID error %s versus %s: %d\n",
2613 libcfs_nid2str(lp->lp_primary_nid),
2614 libcfs_nid2str(nid), rc);
2616 rc = lnet_peer_merge_data(lp, pbuf);
2619 rc = lnet_peer_set_primary_data(
2620 lpni->lpni_peer_net->lpn_peer, pbuf);
2621 lnet_peer_ni_decref_locked(lpni);
2625 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2626 mutex_unlock(&the_lnet.ln_api_mutex);
2628 spin_lock(&lp->lp_lock);
2629 /* Tell discovery to re-check the peer immediately. */
2631 rc = LNET_REDISCOVER_PEER;
2636 * A ping failed. Clear the PING_FAILED state and set the
2637 * FORCE_PING state, to ensure a retry even if discovery is
2638 * disabled. This avoids being left with incorrect state.
2640 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2641 __must_hold(&lp->lp_lock)
2643 lnet_handle_md_t mdh;
2646 mdh = lp->lp_ping_mdh;
2647 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2648 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2649 lp->lp_state |= LNET_PEER_FORCE_PING;
2650 rc = lp->lp_ping_error;
2651 lp->lp_ping_error = 0;
2652 spin_unlock(&lp->lp_lock);
2654 if (!LNetMDHandleIsInvalid(mdh))
2657 CDEBUG(D_NET, "peer %s:%d\n",
2658 libcfs_nid2str(lp->lp_primary_nid), rc);
2660 spin_lock(&lp->lp_lock);
2661 return rc ? rc : LNET_REDISCOVER_PEER;
2665 * Select NID to send a Ping or Push to.
2667 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2669 struct lnet_peer_ni *lpni;
2671 /* Look for a direct-connected NID for this peer. */
2673 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2674 if (!lnet_is_peer_ni_healthy_locked(lpni))
2676 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2681 return lpni->lpni_nid;
2683 /* Look for a routed-connected NID for this peer. */
2685 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2686 if (!lnet_is_peer_ni_healthy_locked(lpni))
2688 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2693 return lpni->lpni_nid;
2695 return LNET_NID_ANY;
2698 /* Active side of ping. */
2699 static int lnet_peer_send_ping(struct lnet_peer *lp)
2700 __must_hold(&lp->lp_lock)
2702 lnet_md_t md = { NULL };
2703 lnet_process_id_t id;
2704 struct lnet_ping_buffer *pbuf;
2709 lp->lp_state |= LNET_PEER_PING_SENT;
2710 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2711 spin_unlock(&lp->lp_lock);
2713 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2714 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
2720 /* initialize md content */
2721 md.start = &pbuf->pb_info;
2722 md.length = LNET_PING_INFO_SIZE(nnis);
2723 md.threshold = 2; /* GET/REPLY */
2725 md.options = LNET_MD_TRUNCATE;
2727 md.eq_handle = the_lnet.ln_dc_eqh;
2729 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_ping_mdh);
2731 lnet_ping_buffer_decref(pbuf);
2732 CERROR("Can't bind MD: %d\n", rc);
2735 cpt = lnet_net_lock_current();
2736 /* Refcount for MD. */
2737 lnet_peer_addref_locked(lp);
2738 id.pid = LNET_PID_LUSTRE;
2739 id.nid = lnet_peer_select_nid(lp);
2740 lnet_net_unlock(cpt);
2742 if (id.nid == LNET_NID_ANY) {
2744 goto fail_unlink_md;
2747 rc = LNetGet(LNET_NID_ANY, lp->lp_ping_mdh, id,
2748 LNET_RESERVED_PORTAL,
2749 LNET_PROTO_PING_MATCHBITS, 0);
2752 goto fail_unlink_md;
2754 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2756 spin_lock(&lp->lp_lock);
2760 LNetMDUnlink(lp->lp_ping_mdh);
2761 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2763 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2765 * The errors that get us here are considered hard errors and
2766 * cause Discovery to terminate. So we clear PING_SENT, but do
2767 * not set either PING_FAILED or FORCE_PING. In fact we need
2768 * to clear PING_FAILED, because the unlink event handler will
2769 * have set it if we called LNetMDUnlink() above.
2771 spin_lock(&lp->lp_lock);
2772 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2777 * This function exists because you cannot call LNetMDUnlink() from an
2780 static int lnet_peer_push_failed(struct lnet_peer *lp)
2781 __must_hold(&lp->lp_lock)
2783 lnet_handle_md_t mdh;
2786 mdh = lp->lp_push_mdh;
2787 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2788 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2789 rc = lp->lp_push_error;
2790 lp->lp_push_error = 0;
2791 spin_unlock(&lp->lp_lock);
2793 if (!LNetMDHandleIsInvalid(mdh))
2796 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2797 spin_lock(&lp->lp_lock);
2798 return rc ? rc : LNET_REDISCOVER_PEER;
2801 /* Active side of push. */
2802 static int lnet_peer_send_push(struct lnet_peer *lp)
2803 __must_hold(&lp->lp_lock)
2805 struct lnet_ping_buffer *pbuf;
2806 lnet_process_id_t id;
2811 /* Don't push to a non-multi-rail peer. */
2812 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2813 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2817 lp->lp_state |= LNET_PEER_PUSH_SENT;
2818 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2819 spin_unlock(&lp->lp_lock);
2821 cpt = lnet_net_lock_current();
2822 pbuf = the_lnet.ln_ping_target;
2823 lnet_ping_buffer_addref(pbuf);
2824 lnet_net_unlock(cpt);
2826 /* Push source MD */
2827 md.start = &pbuf->pb_info;
2828 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2829 md.threshold = 2; /* Put/Ack */
2832 md.eq_handle = the_lnet.ln_dc_eqh;
2835 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2837 lnet_ping_buffer_decref(pbuf);
2838 CERROR("Can't bind push source MD: %d\n", rc);
2841 cpt = lnet_net_lock_current();
2842 /* Refcount for MD. */
2843 lnet_peer_addref_locked(lp);
2844 id.pid = LNET_PID_LUSTRE;
2845 id.nid = lnet_peer_select_nid(lp);
2846 lnet_net_unlock(cpt);
2848 if (id.nid == LNET_NID_ANY) {
2853 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2854 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2855 LNET_PROTO_PING_MATCHBITS, 0, 0);
2860 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2862 spin_lock(&lp->lp_lock);
2866 LNetMDUnlink(lp->lp_push_mdh);
2867 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2869 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2871 * The errors that get us here are considered hard errors and
2872 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2873 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2874 * because the unlink event handler will have set it if we
2875 * called LNetMDUnlink() above.
2877 spin_lock(&lp->lp_lock);
2878 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2883 * An unrecoverable error was encountered during discovery.
2884 * Set error status in peer and abort discovery.
2886 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2888 CDEBUG(D_NET, "Discovery error %s: %d\n",
2889 libcfs_nid2str(lp->lp_primary_nid), error);
2891 spin_lock(&lp->lp_lock);
2892 lp->lp_dc_error = error;
2893 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2894 lp->lp_state |= LNET_PEER_REDISCOVER;
2895 spin_unlock(&lp->lp_lock);
2899 * Mark the peer as discovered.
2901 static int lnet_peer_discovered(struct lnet_peer *lp)
2902 __must_hold(&lp->lp_lock)
2904 lp->lp_state |= LNET_PEER_DISCOVERED;
2905 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2906 LNET_PEER_REDISCOVER);
2908 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2914 * Mark the peer as to be rediscovered.
2916 static int lnet_peer_rediscover(struct lnet_peer *lp)
2917 __must_hold(&lp->lp_lock)
2919 lp->lp_state |= LNET_PEER_REDISCOVER;
2920 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2922 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2928 * Returns the first peer on the ln_dc_working queue if its timeout
2929 * has expired. Takes the current time as an argument so as to not
2930 * obsessively re-check the clock. The oldest discovery request will
2931 * be at the head of the queue.
2933 static struct lnet_peer *lnet_peer_dc_timed_out(time64_t now)
2935 struct lnet_peer *lp;
2937 if (list_empty(&the_lnet.ln_dc_working))
2939 lp = list_first_entry(&the_lnet.ln_dc_working,
2940 struct lnet_peer, lp_dc_list);
2941 if (now < lp->lp_last_queued + DEFAULT_PEER_TIMEOUT)
2947 * Discovering this peer is taking too long. Cancel any Ping or Push
2948 * that discovery is waiting on by unlinking the relevant MDs. The
2949 * lnet_discovery_event_handler() will proceed from here and complete
2952 static void lnet_peer_discovery_timeout(struct lnet_peer *lp)
2954 struct lnet_handle_md ping_mdh;
2955 struct lnet_handle_md push_mdh;
2957 LNetInvalidateMDHandle(&ping_mdh);
2958 LNetInvalidateMDHandle(&push_mdh);
2960 spin_lock(&lp->lp_lock);
2961 if (lp->lp_state & LNET_PEER_PING_SENT) {
2962 ping_mdh = lp->lp_ping_mdh;
2963 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2965 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2966 push_mdh = lp->lp_push_mdh;
2967 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2969 spin_unlock(&lp->lp_lock);
2971 if (!LNetMDHandleIsInvalid(ping_mdh))
2972 LNetMDUnlink(ping_mdh);
2973 if (!LNetMDHandleIsInvalid(push_mdh))
2974 LNetMDUnlink(push_mdh);
2978 * Wait for work to be queued or some other change that must be
2979 * attended to. Returns non-zero if the discovery thread should shut
2982 static int lnet_peer_discovery_wait_for_work(void)
2989 cpt = lnet_net_lock_current();
2991 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
2992 TASK_INTERRUPTIBLE);
2993 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2995 if (lnet_push_target_resize_needed())
2997 if (!list_empty(&the_lnet.ln_dc_request))
2999 if (!list_empty(&the_lnet.ln_msg_resend))
3001 if (lnet_peer_dc_timed_out(ktime_get_real_seconds()))
3003 lnet_net_unlock(cpt);
3006 * wakeup max every second to check if there are peers that
3007 * have been stuck on the working queue for greater than
3010 schedule_timeout(cfs_time_seconds(1));
3011 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3012 cpt = lnet_net_lock_current();
3014 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3016 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3019 lnet_net_unlock(cpt);
3021 CDEBUG(D_NET, "woken: %d\n", rc);
3027 * Messages that were pending on a destroyed peer will be put on a global
3028 * resend list. The message resend list will be checked by
3029 * the discovery thread when it wakes up, and will resend messages. These
3030 * messages can still be sendable in the case the lpni which was the initial
3031 * cause of the message re-queue was transfered to another peer.
3033 * It is possible that LNet could be shutdown while we're iterating
3034 * through the list. lnet_shudown_lndnets() will attempt to access the
3035 * resend list, but will have to wait until the spinlock is released, by
3036 * which time there shouldn't be any more messages on the resend list.
3037 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3038 * for the messages so they can be released. The other case is that
3039 * lnet_shudown_lndnets() can finalize all the messages before this
3040 * function can visit the resend list, in which case this function will be
3043 static void lnet_resend_msgs(void)
3045 struct lnet_msg *msg, *tmp;
3046 struct list_head resend;
3049 INIT_LIST_HEAD(&resend);
3051 spin_lock(&the_lnet.ln_msg_resend_lock);
3052 list_splice(&the_lnet.ln_msg_resend, &resend);
3053 spin_unlock(&the_lnet.ln_msg_resend_lock);
3055 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3056 list_del_init(&msg->msg_list);
3057 rc = lnet_send(msg->msg_src_nid_param, msg,
3058 msg->msg_rtr_nid_param);
3060 CNETERR("Error sending %s to %s: %d\n",
3061 lnet_msgtyp2str(msg->msg_type),
3062 libcfs_id2str(msg->msg_target), rc);
3063 lnet_finalize(msg, rc);
3068 /* The discovery thread. */
3069 static int lnet_peer_discovery(void *arg)
3071 struct lnet_peer *lp;
3075 CDEBUG(D_NET, "started\n");
3076 cfs_block_allsigs();
3079 if (lnet_peer_discovery_wait_for_work())
3084 if (lnet_push_target_resize_needed())
3085 lnet_push_target_resize();
3087 lnet_net_lock(LNET_LOCK_EX);
3088 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3092 * Process all incoming discovery work requests. When
3093 * discovery must wait on a peer to change state, it
3094 * is added to the tail of the ln_dc_working queue. A
3095 * timestamp keeps track of when the peer was added,
3096 * so we can time out discovery requests that take too
3099 while (!list_empty(&the_lnet.ln_dc_request)) {
3100 lp = list_first_entry(&the_lnet.ln_dc_request,
3101 struct lnet_peer, lp_dc_list);
3102 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3104 * set the time the peer was put on the dc_working
3105 * queue. It shouldn't remain on the queue
3106 * forever, in case the GET message (for ping)
3107 * doesn't get a REPLY or the PUT message (for
3108 * push) doesn't get an ACK.
3110 * TODO: LNet Health will deal with this scenario
3113 lp->lp_last_queued = ktime_get_real_seconds();
3114 lnet_net_unlock(LNET_LOCK_EX);
3117 * Select an action depending on the state of
3118 * the peer and whether discovery is disabled.
3119 * The check whether discovery is disabled is
3120 * done after the code that handles processing
3121 * for arrived data, cleanup for failures, and
3122 * forcing a Ping or Push.
3124 spin_lock(&lp->lp_lock);
3125 CDEBUG(D_NET, "peer %s state %#x\n",
3126 libcfs_nid2str(lp->lp_primary_nid),
3128 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3129 rc = lnet_peer_data_present(lp);
3130 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3131 rc = lnet_peer_ping_failed(lp);
3132 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3133 rc = lnet_peer_push_failed(lp);
3134 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3135 rc = lnet_peer_send_ping(lp);
3136 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3137 rc = lnet_peer_send_push(lp);
3138 else if (lnet_peer_discovery_disabled)
3139 rc = lnet_peer_rediscover(lp);
3140 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3141 rc = lnet_peer_send_ping(lp);
3142 else if (lnet_peer_needs_push(lp))
3143 rc = lnet_peer_send_push(lp);
3145 rc = lnet_peer_discovered(lp);
3146 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3147 libcfs_nid2str(lp->lp_primary_nid),
3149 spin_unlock(&lp->lp_lock);
3151 lnet_net_lock(LNET_LOCK_EX);
3152 if (rc == LNET_REDISCOVER_PEER) {
3153 list_move(&lp->lp_dc_list,
3154 &the_lnet.ln_dc_request);
3156 lnet_peer_discovery_error(lp, rc);
3158 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3159 lnet_peer_discovery_complete(lp);
3160 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3165 * Now that the ln_dc_request queue has been emptied
3166 * check the ln_dc_working queue for peers that are
3167 * taking too long. Move all that are found to the
3168 * ln_dc_expired queue and time out any pending
3169 * Ping or Push. We have to drop the lnet_net_lock
3170 * in the loop because lnet_peer_discovery_timeout()
3171 * calls LNetMDUnlink().
3173 now = ktime_get_real_seconds();
3174 while ((lp = lnet_peer_dc_timed_out(now)) != NULL) {
3175 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3176 lnet_net_unlock(LNET_LOCK_EX);
3177 lnet_peer_discovery_timeout(lp);
3178 lnet_net_lock(LNET_LOCK_EX);
3181 lnet_net_unlock(LNET_LOCK_EX);
3184 CDEBUG(D_NET, "stopping\n");
3186 * Clean up before telling lnet_peer_discovery_stop() that
3187 * we're done. Use wake_up() below to somewhat reduce the
3188 * size of the thundering herd if there are multiple threads
3189 * waiting on discovery of a single peer.
3191 LNetEQFree(the_lnet.ln_dc_eqh);
3192 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3194 /* Queue cleanup 1: stop all pending pings and pushes. */
3195 lnet_net_lock(LNET_LOCK_EX);
3196 while (!list_empty(&the_lnet.ln_dc_working)) {
3197 lp = list_first_entry(&the_lnet.ln_dc_working,
3198 struct lnet_peer, lp_dc_list);
3199 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3200 lnet_net_unlock(LNET_LOCK_EX);
3201 lnet_peer_discovery_timeout(lp);
3202 lnet_net_lock(LNET_LOCK_EX);
3204 lnet_net_unlock(LNET_LOCK_EX);
3206 /* Queue cleanup 2: wait for the expired queue to clear. */
3207 while (!list_empty(&the_lnet.ln_dc_expired))
3208 schedule_timeout(cfs_time_seconds(1));
3210 /* Queue cleanup 3: clear the request queue. */
3211 lnet_net_lock(LNET_LOCK_EX);
3212 while (!list_empty(&the_lnet.ln_dc_request)) {
3213 lp = list_first_entry(&the_lnet.ln_dc_request,
3214 struct lnet_peer, lp_dc_list);
3215 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3216 lnet_peer_discovery_complete(lp);
3218 lnet_net_unlock(LNET_LOCK_EX);
3220 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3221 wake_up(&the_lnet.ln_dc_waitq);
3223 CDEBUG(D_NET, "stopped\n");
3228 /* ln_api_mutex is held on entry. */
3229 int lnet_peer_discovery_start(void)
3231 struct task_struct *task;
3234 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3237 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3239 CERROR("Can't allocate discovery EQ: %d\n", rc);
3243 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3244 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3247 CERROR("Can't start peer discovery thread: %d\n", rc);
3249 LNetEQFree(the_lnet.ln_dc_eqh);
3250 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3252 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3255 CDEBUG(D_NET, "discovery start: %d\n", rc);
3260 /* ln_api_mutex is held on entry. */
3261 void lnet_peer_discovery_stop(void)
3263 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3266 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3267 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3268 wake_up(&the_lnet.ln_dc_waitq);
3270 wait_event(the_lnet.ln_dc_waitq,
3271 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3273 LASSERT(list_empty(&the_lnet.ln_dc_request));
3274 LASSERT(list_empty(&the_lnet.ln_dc_working));
3275 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3277 CDEBUG(D_NET, "discovery stopped\n");
3283 lnet_debug_peer(lnet_nid_t nid)
3285 char *aliveness = "NA";
3286 struct lnet_peer_ni *lp;
3289 cpt = lnet_cpt_of_nid(nid, NULL);
3292 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3294 lnet_net_unlock(cpt);
3295 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3299 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3300 aliveness = lp->lpni_alive ? "up" : "down";
3302 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3303 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3304 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3305 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3306 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3308 lnet_peer_ni_decref_locked(lp);
3310 lnet_net_unlock(cpt);
3313 /* Gathering information for userspace. */
3315 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3316 char aliveness[LNET_MAX_STR_LEN],
3317 __u32 *cpt_iter, __u32 *refcount,
3318 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3319 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3320 __u32 *peer_tx_qnob)
3322 struct lnet_peer_table *peer_table;
3323 struct lnet_peer_ni *lp;
3328 /* get the number of CPTs */
3329 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3331 /* if the cpt number to be examined is >= the number of cpts in
3332 * the system then indicate that there are no more cpts to examin
3334 if (*cpt_iter >= lncpt)
3337 /* get the current table */
3338 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3339 /* if the ptable is NULL then there are no more cpts to examine */
3340 if (peer_table == NULL)
3343 lnet_net_lock(*cpt_iter);
3345 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3346 struct list_head *peers = &peer_table->pt_hash[j];
3348 list_for_each_entry(lp, peers, lpni_hashlist) {
3349 if (peer_index-- > 0)
3352 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3353 if (lnet_isrouter(lp) ||
3354 lnet_peer_aliveness_enabled(lp))
3355 snprintf(aliveness, LNET_MAX_STR_LEN,
3356 lp->lpni_alive ? "up" : "down");
3358 *nid = lp->lpni_nid;
3359 *refcount = atomic_read(&lp->lpni_refcount);
3360 *ni_peer_tx_credits =
3361 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3362 *peer_tx_credits = lp->lpni_txcredits;
3363 *peer_rtr_credits = lp->lpni_rtrcredits;
3364 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3365 *peer_tx_qnob = lp->lpni_txqnob;
3371 lnet_net_unlock(*cpt_iter);
3375 return found ? 0 : -ENOENT;
3378 /* ln_api_mutex is held, which keeps the peer list stable */
3379 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3381 struct lnet_ioctl_element_stats *lpni_stats;
3382 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3383 struct lnet_peer_ni_credit_info *lpni_info;
3384 struct lnet_peer_ni *lpni;
3385 struct lnet_peer *lp;
3390 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3397 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3398 + sizeof(*lpni_msg_stats);
3399 size *= lp->lp_nnis;
3400 if (size > cfg->prcfg_size) {
3401 cfg->prcfg_size = size;
3406 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3407 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3408 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3409 cfg->prcfg_count = lp->lp_nnis;
3410 cfg->prcfg_size = size;
3411 cfg->prcfg_state = lp->lp_state;
3413 /* Allocate helper buffers. */
3415 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3418 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3421 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3422 if (!lpni_msg_stats)
3423 goto out_free_stats;
3428 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3429 nid = lpni->lpni_nid;
3430 if (copy_to_user(bulk, &nid, sizeof(nid)))
3431 goto out_free_msg_stats;
3432 bulk += sizeof(nid);
3434 memset(lpni_info, 0, sizeof(*lpni_info));
3435 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3436 if (lnet_isrouter(lpni) ||
3437 lnet_peer_aliveness_enabled(lpni))
3438 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3439 lpni->lpni_alive ? "up" : "down");
3441 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3442 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3443 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3444 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3445 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3446 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3447 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3448 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3449 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3450 goto out_free_msg_stats;
3451 bulk += sizeof(*lpni_info);
3453 memset(lpni_stats, 0, sizeof(*lpni_stats));
3454 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3455 LNET_STATS_TYPE_SEND);
3456 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3457 LNET_STATS_TYPE_RECV);
3458 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3459 LNET_STATS_TYPE_DROP);
3460 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3461 goto out_free_msg_stats;
3462 bulk += sizeof(*lpni_stats);
3463 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3464 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3465 goto out_free_msg_stats;
3466 bulk += sizeof(*lpni_msg_stats);
3471 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3473 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3475 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3477 lnet_peer_decref_locked(lp);