4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <linux/sched.h>
38 #ifdef HAVE_SCHED_HEADERS
39 #include <linux/sched/signal.h>
41 #include <linux/uaccess.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER (1)
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
54 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
55 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
56 lnet_peer_ni_decref_locked(lpni);
61 lnet_peer_net_added(struct lnet_net *net)
63 struct lnet_peer_ni *lpni, *tmp;
65 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66 lpni_on_remote_peer_ni_list) {
68 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
71 spin_lock(&lpni->lpni_lock);
72 lpni->lpni_txcredits =
73 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
74 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
75 lpni->lpni_rtrcredits =
76 lnet_peer_buffer_credits(lpni->lpni_net);
77 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
78 spin_unlock(&lpni->lpni_lock);
80 lnet_peer_remove_from_remote_list(lpni);
86 lnet_peer_tables_destroy(void)
88 struct lnet_peer_table *ptable;
89 struct list_head *hash;
93 if (!the_lnet.ln_peer_tables)
96 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
97 hash = ptable->pt_hash;
98 if (!hash) /* not intialized */
101 LASSERT(list_empty(&ptable->pt_zombie_list));
103 ptable->pt_hash = NULL;
104 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
105 LASSERT(list_empty(&hash[j]));
107 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
110 cfs_percpt_free(the_lnet.ln_peer_tables);
111 the_lnet.ln_peer_tables = NULL;
115 lnet_peer_tables_create(void)
117 struct lnet_peer_table *ptable;
118 struct list_head *hash;
122 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
124 if (the_lnet.ln_peer_tables == NULL) {
125 CERROR("Failed to allocate cpu-partition peer tables\n");
129 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
130 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
131 LNET_PEER_HASH_SIZE * sizeof(*hash));
133 CERROR("Failed to create peer hash table\n");
134 lnet_peer_tables_destroy();
138 spin_lock_init(&ptable->pt_zombie_lock);
139 INIT_LIST_HEAD(&ptable->pt_zombie_list);
141 INIT_LIST_HEAD(&ptable->pt_peer_list);
143 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
144 INIT_LIST_HEAD(&hash[j]);
145 ptable->pt_hash = hash; /* sign of initialization */
151 static struct lnet_peer_ni *
152 lnet_peer_ni_alloc(lnet_nid_t nid)
154 struct lnet_peer_ni *lpni;
155 struct lnet_net *net;
158 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
160 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
164 INIT_LIST_HEAD(&lpni->lpni_txq);
165 INIT_LIST_HEAD(&lpni->lpni_rtrq);
166 INIT_LIST_HEAD(&lpni->lpni_routes);
167 INIT_LIST_HEAD(&lpni->lpni_hashlist);
168 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
169 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
171 spin_lock_init(&lpni->lpni_lock);
173 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
174 lpni->lpni_last_alive = ktime_get_seconds(); /* assumes alive */
175 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
176 lpni->lpni_nid = nid;
177 lpni->lpni_cpt = cpt;
178 lnet_set_peer_ni_health_locked(lpni, true);
180 net = lnet_get_net_locked(LNET_NIDNET(nid));
181 lpni->lpni_net = net;
183 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
184 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
185 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
186 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
189 * This peer_ni is not on a local network, so we
190 * cannot add the credits here. In case the net is
191 * added later, add the peer_ni to the remote peer ni
192 * list so it can be easily found and revisited.
194 /* FIXME: per-net implementation instead? */
195 atomic_inc(&lpni->lpni_refcount);
196 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
197 &the_lnet.ln_remote_peer_ni_list);
200 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
205 static struct lnet_peer_net *
206 lnet_peer_net_alloc(__u32 net_id)
208 struct lnet_peer_net *lpn;
210 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
214 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
215 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
216 lpn->lpn_net_id = net_id;
218 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
224 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
226 struct lnet_peer *lp;
228 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
230 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
231 LASSERT(list_empty(&lpn->lpn_peer_nis));
232 LASSERT(list_empty(&lpn->lpn_peer_nets));
234 lpn->lpn_peer = NULL;
235 LIBCFS_FREE(lpn, sizeof(*lpn));
237 lnet_peer_decref_locked(lp);
240 static struct lnet_peer *
241 lnet_peer_alloc(lnet_nid_t nid)
243 struct lnet_peer *lp;
245 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
249 INIT_LIST_HEAD(&lp->lp_peer_list);
250 INIT_LIST_HEAD(&lp->lp_peer_nets);
251 INIT_LIST_HEAD(&lp->lp_dc_list);
252 INIT_LIST_HEAD(&lp->lp_dc_pendq);
253 init_waitqueue_head(&lp->lp_dc_waitq);
254 spin_lock_init(&lp->lp_lock);
255 lp->lp_primary_nid = nid;
257 * Turn off discovery for loopback peer. If you're creating a peer
258 * for the loopback interface then that was initiated when we
259 * attempted to send a message over the loopback. There is no need
260 * to ever use a different interface when sending messages to
263 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
264 lp->lp_state = LNET_PEER_NO_DISCOVERY;
265 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
267 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
273 lnet_destroy_peer_locked(struct lnet_peer *lp)
275 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
277 LASSERT(atomic_read(&lp->lp_refcount) == 0);
278 LASSERT(list_empty(&lp->lp_peer_nets));
279 LASSERT(list_empty(&lp->lp_peer_list));
280 LASSERT(list_empty(&lp->lp_dc_list));
283 lnet_ping_buffer_decref(lp->lp_data);
286 * if there are messages still on the pending queue, then make
287 * sure to queue them on the ln_msg_resend list so they can be
288 * resent at a later point if the discovery thread is still
290 * If the discovery thread has stopped, then the wakeup will be a
291 * no-op, and it is expected the lnet_shutdown_lndnets() will
292 * eventually be called, which will traverse this list and
293 * finalize the messages on the list.
294 * We can not resend them now because we're holding the cpt lock.
295 * Releasing the lock can cause an inconsistent state
297 spin_lock(&the_lnet.ln_msg_resend_lock);
298 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
299 spin_unlock(&the_lnet.ln_msg_resend_lock);
300 wake_up(&the_lnet.ln_dc_waitq);
302 LIBCFS_FREE(lp, sizeof(*lp));
306 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
307 * that peer_net, detach the peer_net from the peer.
309 * Call with lnet_net_lock/EX held
312 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
314 struct lnet_peer_table *ptable;
315 struct lnet_peer_net *lpn;
316 struct lnet_peer *lp;
319 * Belts and suspenders: gracefully handle teardown of a
320 * partially connected peer_ni.
322 lpn = lpni->lpni_peer_net;
324 list_del_init(&lpni->lpni_peer_nis);
326 * If there are no lpni's left, we detach lpn from
327 * lp_peer_nets, so it cannot be found anymore.
329 if (list_empty(&lpn->lpn_peer_nis))
330 list_del_init(&lpn->lpn_peer_nets);
332 /* Update peer NID count. */
337 * If there are no more peer nets, make the peer unfindable
338 * via the peer_tables.
340 * Otherwise, if the peer is DISCOVERED, tell discovery to
341 * take another look at it. This is a no-op if discovery for
342 * this peer did the detaching.
344 if (list_empty(&lp->lp_peer_nets)) {
345 list_del_init(&lp->lp_peer_list);
346 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
348 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
349 /* Discovery isn't running, nothing to do here. */
350 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
351 lnet_peer_queue_for_discovery(lp);
352 wake_up(&the_lnet.ln_dc_waitq);
354 CDEBUG(D_NET, "peer %s NID %s\n",
355 libcfs_nid2str(lp->lp_primary_nid),
356 libcfs_nid2str(lpni->lpni_nid));
359 /* called with lnet_net_lock LNET_LOCK_EX held */
361 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
363 struct lnet_peer_table *ptable = NULL;
365 /* don't remove a peer_ni if it's also a gateway */
366 if (lpni->lpni_rtr_refcount > 0) {
367 CERROR("Peer NI %s is a gateway. Can not delete it\n",
368 libcfs_nid2str(lpni->lpni_nid));
372 lnet_peer_remove_from_remote_list(lpni);
374 /* remove peer ni from the hash list. */
375 list_del_init(&lpni->lpni_hashlist);
377 /* decrement the ref count on the peer table */
378 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
379 LASSERT(ptable->pt_number > 0);
383 * The peer_ni can no longer be found with a lookup. But there
384 * can be current users, so keep track of it on the zombie
385 * list until the reference count has gone to zero.
387 * The last reference may be lost in a place where the
388 * lnet_net_lock locks only a single cpt, and that cpt may not
389 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
392 spin_lock(&ptable->pt_zombie_lock);
393 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
394 ptable->pt_zombies++;
395 spin_unlock(&ptable->pt_zombie_lock);
397 /* no need to keep this peer_ni on the hierarchy anymore */
398 lnet_peer_detach_peer_ni_locked(lpni);
400 /* remove hashlist reference on peer_ni */
401 lnet_peer_ni_decref_locked(lpni);
406 void lnet_peer_uninit(void)
408 struct lnet_peer_ni *lpni, *tmp;
410 lnet_net_lock(LNET_LOCK_EX);
412 /* remove all peer_nis from the remote peer and the hash list */
413 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
414 lpni_on_remote_peer_ni_list)
415 lnet_peer_ni_del_locked(lpni);
417 lnet_peer_tables_destroy();
419 lnet_net_unlock(LNET_LOCK_EX);
423 lnet_peer_del_locked(struct lnet_peer *peer)
425 struct lnet_peer_ni *lpni = NULL, *lpni2;
428 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
430 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
431 while (lpni != NULL) {
432 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
433 rc = lnet_peer_ni_del_locked(lpni);
443 lnet_peer_del(struct lnet_peer *peer)
445 lnet_net_lock(LNET_LOCK_EX);
446 lnet_peer_del_locked(peer);
447 lnet_net_unlock(LNET_LOCK_EX);
453 * Delete a NID from a peer. Call with ln_api_mutex held.
456 * -EPERM: Non-DLC deletion from DLC-configured peer.
457 * -ENOENT: No lnet_peer_ni corresponding to the nid.
458 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
459 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
462 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
464 struct lnet_peer_ni *lpni;
465 lnet_nid_t primary_nid = lp->lp_primary_nid;
468 if (!(flags & LNET_PEER_CONFIGURED)) {
469 if (lp->lp_state & LNET_PEER_CONFIGURED) {
474 lpni = lnet_find_peer_ni_locked(nid);
479 lnet_peer_ni_decref_locked(lpni);
480 if (lp != lpni->lpni_peer_net->lpn_peer) {
486 * This function only allows deletion of the primary NID if it
489 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
494 lnet_net_lock(LNET_LOCK_EX);
495 lnet_peer_ni_del_locked(lpni);
496 lnet_net_unlock(LNET_LOCK_EX);
499 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
500 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
506 lnet_peer_table_cleanup_locked(struct lnet_net *net,
507 struct lnet_peer_table *ptable)
510 struct lnet_peer_ni *next;
511 struct lnet_peer_ni *lpni;
512 struct lnet_peer *peer;
514 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
515 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
517 if (net != NULL && net != lpni->lpni_net)
520 peer = lpni->lpni_peer_net->lpn_peer;
521 if (peer->lp_primary_nid != lpni->lpni_nid) {
522 lnet_peer_ni_del_locked(lpni);
526 * Removing the primary NID implies removing
527 * the entire peer. Advance next beyond any
528 * peer_ni that belongs to the same peer.
530 list_for_each_entry_from(next, &ptable->pt_hash[i],
532 if (next->lpni_peer_net->lpn_peer != peer)
535 lnet_peer_del_locked(peer);
541 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
545 spin_lock(&ptable->pt_zombie_lock);
546 while (ptable->pt_zombies) {
547 spin_unlock(&ptable->pt_zombie_lock);
549 if (is_power_of_2(i)) {
551 "Waiting for %d zombies on peer table\n",
554 set_current_state(TASK_UNINTERRUPTIBLE);
555 schedule_timeout(cfs_time_seconds(1) >> 1);
556 spin_lock(&ptable->pt_zombie_lock);
558 spin_unlock(&ptable->pt_zombie_lock);
562 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
563 struct lnet_peer_table *ptable)
565 struct lnet_peer_ni *lp;
566 struct lnet_peer_ni *tmp;
570 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
571 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
573 if (net != lp->lpni_net)
576 if (lp->lpni_rtr_refcount == 0)
579 lpni_nid = lp->lpni_nid;
581 lnet_net_unlock(LNET_LOCK_EX);
582 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
583 lnet_net_lock(LNET_LOCK_EX);
589 lnet_peer_tables_cleanup(struct lnet_net *net)
592 struct lnet_peer_table *ptable;
594 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
595 /* If just deleting the peers for a NI, get rid of any routes these
596 * peers are gateways for. */
597 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
598 lnet_net_lock(LNET_LOCK_EX);
599 lnet_peer_table_del_rtrs_locked(net, ptable);
600 lnet_net_unlock(LNET_LOCK_EX);
603 /* Start the cleanup process */
604 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
605 lnet_net_lock(LNET_LOCK_EX);
606 lnet_peer_table_cleanup_locked(net, ptable);
607 lnet_net_unlock(LNET_LOCK_EX);
610 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
611 lnet_peer_ni_finalize_wait(ptable);
614 static struct lnet_peer_ni *
615 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
617 struct list_head *peers;
618 struct lnet_peer_ni *lp;
620 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
622 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
623 list_for_each_entry(lp, peers, lpni_hashlist) {
624 if (lp->lpni_nid == nid) {
625 lnet_peer_ni_addref_locked(lp);
633 struct lnet_peer_ni *
634 lnet_find_peer_ni_locked(lnet_nid_t nid)
636 struct lnet_peer_ni *lpni;
637 struct lnet_peer_table *ptable;
640 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
642 ptable = the_lnet.ln_peer_tables[cpt];
643 lpni = lnet_get_peer_ni_locked(ptable, nid);
649 lnet_find_peer(lnet_nid_t nid)
651 struct lnet_peer_ni *lpni;
652 struct lnet_peer *lp = NULL;
655 cpt = lnet_net_lock_current();
656 lpni = lnet_find_peer_ni_locked(nid);
658 lp = lpni->lpni_peer_net->lpn_peer;
659 lnet_peer_addref_locked(lp);
660 lnet_peer_ni_decref_locked(lpni);
662 lnet_net_unlock(cpt);
667 struct lnet_peer_ni *
668 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
669 struct lnet_peer_net *peer_net,
670 struct lnet_peer_ni *prev)
672 struct lnet_peer_ni *lpni;
673 struct lnet_peer_net *net = peer_net;
677 if (list_empty(&peer->lp_peer_nets))
680 net = list_entry(peer->lp_peer_nets.next,
681 struct lnet_peer_net,
684 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
690 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
692 * if you reached the end of the peer ni list and the peer
693 * net is specified then there are no more peer nis in that
700 * we reached the end of this net ni list. move to the
703 if (prev->lpni_peer_net->lpn_peer_nets.next ==
705 /* no more nets and no more NIs. */
708 /* get the next net */
709 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
710 struct lnet_peer_net,
712 /* get the ni on it */
713 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
719 /* there are more nis left */
720 lpni = list_entry(prev->lpni_peer_nis.next,
721 struct lnet_peer_ni, lpni_peer_nis);
726 /* Call with the ln_api_mutex held */
727 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
729 struct lnet_process_id id;
730 struct lnet_peer_table *ptable;
731 struct lnet_peer *lp;
740 if (the_lnet.ln_state != LNET_STATE_RUNNING)
743 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
746 * Count the number of peers, and return E2BIG if the buffer
747 * is too small. We'll also return the desired size.
750 for (cpt = 0; cpt < lncpt; cpt++) {
751 ptable = the_lnet.ln_peer_tables[cpt];
752 count += ptable->pt_peers;
754 size = count * sizeof(*ids);
759 * Walk the peer lists and copy out the primary nids.
760 * This is safe because the peer lists are only modified
761 * while the ln_api_mutex is held. So we don't need to
762 * hold the lnet_net_lock as well, and can therefore
763 * directly call copy_to_user().
766 memset(&id, 0, sizeof(id));
767 id.pid = LNET_PID_LUSTRE;
769 for (cpt = 0; cpt < lncpt; cpt++) {
770 ptable = the_lnet.ln_peer_tables[cpt];
771 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
774 id.nid = lp->lp_primary_nid;
775 if (copy_to_user(&ids[i], &id, sizeof(id)))
788 * Start pushes to peers that need to be updated for a configuration
789 * change on this node.
792 lnet_push_update_to_peers(int force)
794 struct lnet_peer_table *ptable;
795 struct lnet_peer *lp;
799 lnet_net_lock(LNET_LOCK_EX);
800 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
801 for (cpt = 0; cpt < lncpt; cpt++) {
802 ptable = the_lnet.ln_peer_tables[cpt];
803 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
805 spin_lock(&lp->lp_lock);
806 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
807 lp->lp_state |= LNET_PEER_FORCE_PUSH;
808 spin_unlock(&lp->lp_lock);
810 if (lnet_peer_needs_push(lp))
811 lnet_peer_queue_for_discovery(lp);
814 lnet_net_unlock(LNET_LOCK_EX);
815 wake_up(&the_lnet.ln_dc_waitq);
819 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
820 * this is a preferred point-to-point path. Call with lnet_net_lock in
824 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
828 if (lpni->lpni_pref_nnids == 0)
830 if (lpni->lpni_pref_nnids == 1)
831 return lpni->lpni_pref.nid == nid;
832 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
833 if (lpni->lpni_pref.nids[i] == nid)
840 * Set a single ni as preferred, provided no preferred ni is already
841 * defined. Only to be used for non-multi-rail peer_ni.
844 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
848 spin_lock(&lpni->lpni_lock);
849 if (nid == LNET_NID_ANY) {
851 } else if (lpni->lpni_pref_nnids > 0) {
853 } else if (lpni->lpni_pref_nnids == 0) {
854 lpni->lpni_pref.nid = nid;
855 lpni->lpni_pref_nnids = 1;
856 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
858 spin_unlock(&lpni->lpni_lock);
860 CDEBUG(D_NET, "peer %s nid %s: %d\n",
861 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
866 * Clear the preferred NID from a non-multi-rail peer_ni, provided
867 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
870 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
874 spin_lock(&lpni->lpni_lock);
875 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
876 lpni->lpni_pref_nnids = 0;
877 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
878 } else if (lpni->lpni_pref_nnids == 0) {
883 spin_unlock(&lpni->lpni_lock);
885 CDEBUG(D_NET, "peer %s: %d\n",
886 libcfs_nid2str(lpni->lpni_nid), rc);
891 * Clear the preferred NIDs from a non-multi-rail peer.
894 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
896 struct lnet_peer_ni *lpni = NULL;
898 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
899 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
903 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
905 lnet_nid_t *nids = NULL;
906 lnet_nid_t *oldnids = NULL;
907 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
912 if (nid == LNET_NID_ANY) {
917 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
922 /* A non-MR node may have only one preferred NI per peer_ni */
923 if (lpni->lpni_pref_nnids > 0) {
924 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
930 if (lpni->lpni_pref_nnids != 0) {
931 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
932 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
937 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
938 if (lpni->lpni_pref.nids[i] == nid) {
939 LIBCFS_FREE(nids, size);
943 nids[i] = lpni->lpni_pref.nids[i];
948 lnet_net_lock(LNET_LOCK_EX);
949 spin_lock(&lpni->lpni_lock);
950 if (lpni->lpni_pref_nnids == 0) {
951 lpni->lpni_pref.nid = nid;
953 oldnids = lpni->lpni_pref.nids;
954 lpni->lpni_pref.nids = nids;
956 lpni->lpni_pref_nnids++;
957 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
958 spin_unlock(&lpni->lpni_lock);
959 lnet_net_unlock(LNET_LOCK_EX);
962 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
963 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
966 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
967 spin_lock(&lpni->lpni_lock);
968 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
969 spin_unlock(&lpni->lpni_lock);
971 CDEBUG(D_NET, "peer %s nid %s: %d\n",
972 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
977 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
979 lnet_nid_t *nids = NULL;
980 lnet_nid_t *oldnids = NULL;
981 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
986 if (lpni->lpni_pref_nnids == 0) {
991 if (lpni->lpni_pref_nnids == 1) {
992 if (lpni->lpni_pref.nid != nid) {
996 } else if (lpni->lpni_pref_nnids == 2) {
997 if (lpni->lpni_pref.nids[0] != nid &&
998 lpni->lpni_pref.nids[1] != nid) {
1003 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1004 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1009 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1010 if (lpni->lpni_pref.nids[i] != nid)
1012 nids[j++] = lpni->lpni_pref.nids[i];
1014 /* Check if we actually removed a nid. */
1015 if (j == lpni->lpni_pref_nnids) {
1016 LIBCFS_FREE(nids, size);
1022 lnet_net_lock(LNET_LOCK_EX);
1023 spin_lock(&lpni->lpni_lock);
1024 if (lpni->lpni_pref_nnids == 1) {
1025 lpni->lpni_pref.nid = LNET_NID_ANY;
1026 } else if (lpni->lpni_pref_nnids == 2) {
1027 oldnids = lpni->lpni_pref.nids;
1028 if (oldnids[0] == nid)
1029 lpni->lpni_pref.nid = oldnids[1];
1031 lpni->lpni_pref.nid = oldnids[2];
1033 oldnids = lpni->lpni_pref.nids;
1034 lpni->lpni_pref.nids = nids;
1036 lpni->lpni_pref_nnids--;
1037 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1038 spin_unlock(&lpni->lpni_lock);
1039 lnet_net_unlock(LNET_LOCK_EX);
1042 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1043 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1046 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1047 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1052 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1054 struct lnet_peer_ni *lpni;
1055 lnet_nid_t primary_nid = nid;
1057 lpni = lnet_find_peer_ni_locked(nid);
1059 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1060 lnet_peer_ni_decref_locked(lpni);
1067 LNetPrimaryNID(lnet_nid_t nid)
1069 struct lnet_peer *lp;
1070 struct lnet_peer_ni *lpni;
1071 lnet_nid_t primary_nid = nid;
1075 cpt = lnet_net_lock_current();
1076 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1081 lp = lpni->lpni_peer_net->lpn_peer;
1082 while (!lnet_peer_is_uptodate(lp)) {
1083 rc = lnet_discover_peer_locked(lpni, cpt, true);
1086 lp = lpni->lpni_peer_net->lpn_peer;
1088 primary_nid = lp->lp_primary_nid;
1090 lnet_peer_ni_decref_locked(lpni);
1092 lnet_net_unlock(cpt);
1094 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1095 libcfs_nid2str(primary_nid), rc);
1098 EXPORT_SYMBOL(LNetPrimaryNID);
1100 struct lnet_peer_net *
1101 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1103 struct lnet_peer_net *peer_net;
1104 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1105 if (peer_net->lpn_net_id == net_id)
1112 * Attach a peer_ni to a peer_net and peer. This function assumes
1113 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1114 * may be attached to a different peer, in which case it will be
1115 * properly detached first. The whole operation is done atomically.
1117 * Always returns 0. This is the last function called from functions
1118 * that do return an int, so returning 0 here allows the compiler to
1122 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1123 struct lnet_peer_net *lpn,
1124 struct lnet_peer_ni *lpni,
1127 struct lnet_peer_table *ptable;
1129 /* Install the new peer_ni */
1130 lnet_net_lock(LNET_LOCK_EX);
1131 /* Add peer_ni to global peer table hash, if necessary. */
1132 if (list_empty(&lpni->lpni_hashlist)) {
1133 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1135 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1136 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1137 ptable->pt_version++;
1138 ptable->pt_number++;
1139 /* This is the 1st refcount on lpni. */
1140 atomic_inc(&lpni->lpni_refcount);
1143 /* Detach the peer_ni from an existing peer, if necessary. */
1144 if (lpni->lpni_peer_net) {
1145 LASSERT(lpni->lpni_peer_net != lpn);
1146 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1147 lnet_peer_detach_peer_ni_locked(lpni);
1148 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1149 lpni->lpni_peer_net = NULL;
1152 /* Add peer_ni to peer_net */
1153 lpni->lpni_peer_net = lpn;
1154 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1155 lnet_peer_net_addref_locked(lpn);
1157 /* Add peer_net to peer */
1158 if (!lpn->lpn_peer) {
1160 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1161 lnet_peer_addref_locked(lp);
1164 /* Add peer to global peer list, if necessary */
1165 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1166 if (list_empty(&lp->lp_peer_list)) {
1167 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1172 /* Update peer state */
1173 spin_lock(&lp->lp_lock);
1174 if (flags & LNET_PEER_CONFIGURED) {
1175 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1176 lp->lp_state |= LNET_PEER_CONFIGURED;
1178 if (flags & LNET_PEER_MULTI_RAIL) {
1179 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1180 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1181 lnet_peer_clr_non_mr_pref_nids(lp);
1184 spin_unlock(&lp->lp_lock);
1187 lnet_net_unlock(LNET_LOCK_EX);
1189 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1190 libcfs_nid2str(lp->lp_primary_nid),
1191 libcfs_nid2str(lpni->lpni_nid), flags);
1197 * Create a new peer, with nid as its primary nid.
1199 * Call with the lnet_api_mutex held.
1202 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1204 struct lnet_peer *lp;
1205 struct lnet_peer_net *lpn;
1206 struct lnet_peer_ni *lpni;
1209 LASSERT(nid != LNET_NID_ANY);
1212 * No need for the lnet_net_lock here, because the
1213 * lnet_api_mutex is held.
1215 lpni = lnet_find_peer_ni_locked(nid);
1217 /* A peer with this NID already exists. */
1218 lp = lpni->lpni_peer_net->lpn_peer;
1219 lnet_peer_ni_decref_locked(lpni);
1221 * This is an error if the peer was configured and the
1222 * primary NID differs or an attempt is made to change
1223 * the Multi-Rail flag. Otherwise the assumption is
1224 * that an existing peer is being modified.
1226 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1227 if (lp->lp_primary_nid != nid)
1229 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1233 /* Delete and recreate as a configured peer. */
1237 /* Create peer, peer_net, and peer_ni. */
1239 lp = lnet_peer_alloc(nid);
1242 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1245 lpni = lnet_peer_ni_alloc(nid);
1249 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1252 LIBCFS_FREE(lpn, sizeof(*lpn));
1254 LIBCFS_FREE(lp, sizeof(*lp));
1256 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1257 libcfs_nid2str(nid), flags, rc);
1262 * Add a NID to a peer. Call with ln_api_mutex held.
1265 * -EPERM: Non-DLC addition to a DLC-configured peer.
1266 * -EEXIST: The NID was configured by DLC for a different peer.
1267 * -ENOMEM: Out of memory.
1268 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1269 * non-multi-rail peer.
1272 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1274 struct lnet_peer_net *lpn;
1275 struct lnet_peer_ni *lpni;
1279 LASSERT(nid != LNET_NID_ANY);
1281 /* A configured peer can only be updated through configuration. */
1282 if (!(flags & LNET_PEER_CONFIGURED)) {
1283 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1290 * The MULTI_RAIL flag can be set but not cleared, because
1291 * that would leave the peer struct in an invalid state.
1293 if (flags & LNET_PEER_MULTI_RAIL) {
1294 spin_lock(&lp->lp_lock);
1295 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1296 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1297 lnet_peer_clr_non_mr_pref_nids(lp);
1299 spin_unlock(&lp->lp_lock);
1300 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1305 lpni = lnet_find_peer_ni_locked(nid);
1308 * A peer_ni already exists. This is only a problem if
1309 * it is not connected to this peer and was configured
1312 lnet_peer_ni_decref_locked(lpni);
1313 if (lpni->lpni_peer_net->lpn_peer == lp)
1315 if (lnet_peer_ni_is_configured(lpni)) {
1319 /* If this is the primary NID, destroy the peer. */
1320 if (lnet_peer_ni_is_primary(lpni)) {
1321 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1322 lpni = lnet_peer_ni_alloc(nid);
1329 lpni = lnet_peer_ni_alloc(nid);
1337 * Get the peer_net. Check that we're not adding a second
1338 * peer_ni on a peer_net of a non-multi-rail peer.
1340 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1342 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1347 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1352 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1355 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1356 if (!lpni->lpni_peer_net)
1357 LIBCFS_FREE(lpni, sizeof(*lpni));
1359 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1360 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1366 * Update the primary NID of a peer, if possible.
1368 * Call with the lnet_api_mutex held.
1371 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1373 lnet_nid_t old = lp->lp_primary_nid;
1376 if (lp->lp_primary_nid == nid)
1378 rc = lnet_peer_add_nid(lp, nid, flags);
1381 lp->lp_primary_nid = nid;
1383 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1384 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1389 * lpni creation initiated due to traffic either sending or receiving.
1392 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1394 struct lnet_peer *lp;
1395 struct lnet_peer_net *lpn;
1396 struct lnet_peer_ni *lpni;
1400 if (nid == LNET_NID_ANY) {
1405 /* lnet_net_lock is not needed here because ln_api_lock is held */
1406 lpni = lnet_find_peer_ni_locked(nid);
1409 * We must have raced with another thread. Since we
1410 * know next to nothing about a peer_ni created by
1411 * traffic, we just assume everything is ok and
1414 lnet_peer_ni_decref_locked(lpni);
1418 /* Create peer, peer_net, and peer_ni. */
1420 lp = lnet_peer_alloc(nid);
1423 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1426 lpni = lnet_peer_ni_alloc(nid);
1429 if (pref != LNET_NID_ANY)
1430 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1432 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1435 LIBCFS_FREE(lpn, sizeof(*lpn));
1437 LIBCFS_FREE(lp, sizeof(*lp));
1439 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1444 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1446 * This API handles the following combinations:
1447 * Create a peer with its primary NI if only the prim_nid is provided
1448 * Add a NID to a peer identified by the prim_nid. The peer identified
1449 * by the prim_nid must already exist.
1450 * The peer being created may be non-MR.
1452 * The caller must hold ln_api_mutex. This prevents the peer from
1453 * being created/modified/deleted by a different thread.
1456 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1458 struct lnet_peer *lp = NULL;
1459 struct lnet_peer_ni *lpni;
1462 /* The prim_nid must always be specified */
1463 if (prim_nid == LNET_NID_ANY)
1466 flags = LNET_PEER_CONFIGURED;
1468 flags |= LNET_PEER_MULTI_RAIL;
1471 * If nid isn't specified, we must create a new peer with
1472 * prim_nid as its primary nid.
1474 if (nid == LNET_NID_ANY)
1475 return lnet_peer_add(prim_nid, flags);
1477 /* Look up the prim_nid, which must exist. */
1478 lpni = lnet_find_peer_ni_locked(prim_nid);
1481 lnet_peer_ni_decref_locked(lpni);
1482 lp = lpni->lpni_peer_net->lpn_peer;
1484 /* Peer must have been configured. */
1485 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1486 CDEBUG(D_NET, "peer %s was not configured\n",
1487 libcfs_nid2str(prim_nid));
1491 /* Primary NID must match */
1492 if (lp->lp_primary_nid != prim_nid) {
1493 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1494 libcfs_nid2str(prim_nid),
1495 libcfs_nid2str(lp->lp_primary_nid));
1499 /* Multi-Rail flag must match. */
1500 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1501 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1502 libcfs_nid2str(prim_nid));
1506 return lnet_peer_add_nid(lp, nid, flags);
1510 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1512 * This API handles the following combinations:
1513 * Delete a NI from a peer if both prim_nid and nid are provided.
1514 * Delete a peer if only prim_nid is provided.
1515 * Delete a peer if its primary nid is provided.
1517 * The caller must hold ln_api_mutex. This prevents the peer from
1518 * being modified/deleted by a different thread.
1521 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1523 struct lnet_peer *lp;
1524 struct lnet_peer_ni *lpni;
1527 if (prim_nid == LNET_NID_ANY)
1530 lpni = lnet_find_peer_ni_locked(prim_nid);
1533 lnet_peer_ni_decref_locked(lpni);
1534 lp = lpni->lpni_peer_net->lpn_peer;
1536 if (prim_nid != lp->lp_primary_nid) {
1537 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1538 libcfs_nid2str(prim_nid),
1539 libcfs_nid2str(lp->lp_primary_nid));
1543 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1544 return lnet_peer_del(lp);
1546 flags = LNET_PEER_CONFIGURED;
1547 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1548 flags |= LNET_PEER_MULTI_RAIL;
1550 return lnet_peer_del_nid(lp, nid, flags);
1554 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1556 struct lnet_peer_table *ptable;
1557 struct lnet_peer_net *lpn;
1559 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1561 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1562 LASSERT(lpni->lpni_rtr_refcount == 0);
1563 LASSERT(list_empty(&lpni->lpni_txq));
1564 LASSERT(lpni->lpni_txqnob == 0);
1565 LASSERT(list_empty(&lpni->lpni_peer_nis));
1566 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1568 lpn = lpni->lpni_peer_net;
1569 lpni->lpni_peer_net = NULL;
1570 lpni->lpni_net = NULL;
1572 /* remove the peer ni from the zombie list */
1573 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1574 spin_lock(&ptable->pt_zombie_lock);
1575 list_del_init(&lpni->lpni_hashlist);
1576 ptable->pt_zombies--;
1577 spin_unlock(&ptable->pt_zombie_lock);
1579 if (lpni->lpni_pref_nnids > 1) {
1580 LIBCFS_FREE(lpni->lpni_pref.nids,
1581 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1583 LIBCFS_FREE(lpni, sizeof(*lpni));
1585 lnet_peer_net_decref_locked(lpn);
1588 struct lnet_peer_ni *
1589 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1591 struct lnet_peer_ni *lpni = NULL;
1594 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1595 return ERR_PTR(-ESHUTDOWN);
1598 * find if a peer_ni already exists.
1599 * If so then just return that.
1601 lpni = lnet_find_peer_ni_locked(nid);
1605 lnet_net_unlock(cpt);
1607 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1610 goto out_net_relock;
1613 lpni = lnet_find_peer_ni_locked(nid);
1623 * Get a peer_ni for the given nid, create it if necessary. Takes a
1624 * hold on the peer_ni.
1626 struct lnet_peer_ni *
1627 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1629 struct lnet_peer_ni *lpni = NULL;
1632 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1633 return ERR_PTR(-ESHUTDOWN);
1636 * find if a peer_ni already exists.
1637 * If so then just return that.
1639 lpni = lnet_find_peer_ni_locked(nid);
1645 * use the lnet_api_mutex to serialize the creation of the peer_ni
1646 * and the creation/deletion of the local ni/net. When a local ni is
1647 * created, if there exists a set of peer_nis on that network,
1648 * they need to be traversed and updated. When a local NI is
1649 * deleted, which could result in a network being deleted, then
1650 * all peer nis on that network need to be removed as well.
1652 * Creation through traffic should also be serialized with
1653 * creation through DLC.
1655 lnet_net_unlock(cpt);
1656 mutex_lock(&the_lnet.ln_api_mutex);
1658 * Shutdown is only set under the ln_api_lock, so a single
1659 * check here is sufficent.
1661 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1662 lpni = ERR_PTR(-ESHUTDOWN);
1663 goto out_mutex_unlock;
1666 rc = lnet_peer_ni_traffic_add(nid, pref);
1669 goto out_mutex_unlock;
1672 lpni = lnet_find_peer_ni_locked(nid);
1676 mutex_unlock(&the_lnet.ln_api_mutex);
1679 /* Lock has been dropped, check again for shutdown. */
1680 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1682 lnet_peer_ni_decref_locked(lpni);
1683 lpni = ERR_PTR(-ESHUTDOWN);
1694 * Is a peer uptodate from the point of view of discovery?
1696 * If it is currently being processed, obviously not.
1697 * A forced Ping or Push is also handled by the discovery thread.
1699 * Otherwise look at whether the peer needs rediscovering.
1702 lnet_peer_is_uptodate(struct lnet_peer *lp)
1706 spin_lock(&lp->lp_lock);
1707 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1708 LNET_PEER_FORCE_PING |
1709 LNET_PEER_FORCE_PUSH)) {
1711 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1713 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1714 if (lnet_peer_discovery_disabled)
1718 } else if (lnet_peer_needs_push(lp)) {
1720 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1721 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1728 spin_unlock(&lp->lp_lock);
1734 * Queue a peer for the attention of the discovery thread. Call with
1735 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1736 * -EALREADY if the peer was already queued.
1738 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1742 spin_lock(&lp->lp_lock);
1743 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1744 lp->lp_state |= LNET_PEER_DISCOVERING;
1745 spin_unlock(&lp->lp_lock);
1746 if (list_empty(&lp->lp_dc_list)) {
1747 lnet_peer_addref_locked(lp);
1748 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1749 wake_up(&the_lnet.ln_dc_waitq);
1755 CDEBUG(D_NET, "Queue peer %s: %d\n",
1756 libcfs_nid2str(lp->lp_primary_nid), rc);
1762 * Discovery of a peer is complete. Wake all waiters on the peer.
1763 * Call with lnet_net_lock/EX held.
1765 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1767 struct lnet_msg *msg, *tmp;
1769 struct list_head pending_msgs;
1771 INIT_LIST_HEAD(&pending_msgs);
1773 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1774 libcfs_nid2str(lp->lp_primary_nid));
1776 list_del_init(&lp->lp_dc_list);
1777 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1778 wake_up_all(&lp->lp_dc_waitq);
1780 lnet_net_unlock(LNET_LOCK_EX);
1782 /* iterate through all pending messages and send them again */
1783 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1784 list_del_init(&msg->msg_list);
1785 if (lp->lp_dc_error) {
1786 lnet_finalize(msg, lp->lp_dc_error);
1790 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1791 lnet_msgtyp2str(msg->msg_type),
1792 libcfs_id2str(msg->msg_target));
1793 rc = lnet_send(msg->msg_src_nid_param, msg,
1794 msg->msg_rtr_nid_param);
1796 CNETERR("Error sending %s to %s: %d\n",
1797 lnet_msgtyp2str(msg->msg_type),
1798 libcfs_id2str(msg->msg_target), rc);
1799 lnet_finalize(msg, rc);
1802 lnet_net_lock(LNET_LOCK_EX);
1803 lnet_peer_decref_locked(lp);
1807 * Handle inbound push.
1808 * Like any event handler, called with lnet_res_lock/CPT held.
1810 void lnet_peer_push_event(struct lnet_event *ev)
1812 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1813 struct lnet_peer *lp;
1815 /* lnet_find_peer() adds a refcount */
1816 lp = lnet_find_peer(ev->source.nid);
1818 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1819 libcfs_nid2str(ev->initiator.nid),
1820 libcfs_nid2str(ev->source.nid));
1824 /* Ensure peer state remains consistent while we modify it. */
1825 spin_lock(&lp->lp_lock);
1828 * If some kind of error happened the contents of the message
1829 * cannot be used. Clear the NIDS_UPTODATE and set the
1830 * FORCE_PING flag to trigger a ping.
1833 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1834 lp->lp_state |= LNET_PEER_FORCE_PING;
1835 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1837 libcfs_nid2str(lp->lp_primary_nid),
1838 libcfs_nid2str(ev->source.nid));
1843 * A push with invalid or corrupted info. Clear the UPTODATE
1844 * flag to trigger a ping.
1846 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1847 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1848 lp->lp_state |= LNET_PEER_FORCE_PING;
1849 CDEBUG(D_NET, "Corrupted Push from %s\n",
1850 libcfs_nid2str(lp->lp_primary_nid));
1855 * Make sure we'll allocate the correct size ping buffer when
1858 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1859 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1862 * A non-Multi-Rail peer is not supposed to be capable of
1865 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1866 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1867 libcfs_nid2str(lp->lp_primary_nid));
1872 * Check the MULTIRAIL flag. Complain if the peer was DLC
1873 * configured without it.
1875 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1876 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1877 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1878 libcfs_nid2str(lp->lp_primary_nid));
1880 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1881 lnet_peer_clr_non_mr_pref_nids(lp);
1886 * The peer may have discovery disabled at its end. Set
1887 * NO_DISCOVERY as appropriate.
1889 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1890 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1891 libcfs_nid2str(lp->lp_primary_nid));
1892 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1893 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1894 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1895 libcfs_nid2str(lp->lp_primary_nid));
1896 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1900 * Check for truncation of the Put message. Clear the
1901 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1902 * and tell discovery to allocate a bigger buffer.
1904 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1905 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1906 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1907 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1908 lp->lp_state |= LNET_PEER_FORCE_PING;
1909 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1910 libcfs_nid2str(lp->lp_primary_nid),
1911 pbuf->pb_info.pi_nnis);
1916 * Check whether the Put data is stale. Stale data can just be
1919 if (pbuf->pb_info.pi_nnis > 1 &&
1920 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1921 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1922 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1923 libcfs_nid2str(lp->lp_primary_nid),
1924 LNET_PING_BUFFER_SEQNO(pbuf),
1930 * Check whether the Put data is new, in which case we clear
1931 * the UPTODATE flag and prepare to process it.
1933 * If the Put data is current, and the peer is UPTODATE then
1934 * we assome everything is all right and drop the data as
1937 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1938 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1939 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1940 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1941 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1942 libcfs_nid2str(lp->lp_primary_nid),
1943 LNET_PING_BUFFER_SEQNO(pbuf),
1949 * If there is data present that hasn't been processed yet,
1950 * we'll replace it if the Put contained newer data and it
1951 * fits. We're racing with a Ping or earlier Push in this
1954 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1955 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1956 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1957 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1958 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1959 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1960 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1961 libcfs_nid2str(lp->lp_primary_nid),
1962 LNET_PING_BUFFER_SEQNO(pbuf),
1963 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1969 * Allocate a buffer to copy the data. On a failure we drop
1970 * the Push and set FORCE_PING to force the discovery
1971 * thread to fix the problem by pinging the peer.
1973 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
1975 lp->lp_state |= LNET_PEER_FORCE_PING;
1976 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
1977 libcfs_nid2str(lp->lp_primary_nid),
1978 LNET_PING_BUFFER_SEQNO(pbuf));
1983 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1984 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1985 lp->lp_state |= LNET_PEER_DATA_PRESENT;
1986 CDEBUG(D_NET, "Received Push %s %u\n",
1987 libcfs_nid2str(lp->lp_primary_nid),
1988 LNET_PING_BUFFER_SEQNO(pbuf));
1992 * Queue the peer for discovery if not done, force it on the request
1993 * queue and wake the discovery thread if the peer was already queued,
1994 * because its status changed.
1996 spin_unlock(&lp->lp_lock);
1997 lnet_net_lock(LNET_LOCK_EX);
1998 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
1999 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2000 wake_up(&the_lnet.ln_dc_waitq);
2002 /* Drop refcount from lookup */
2003 lnet_peer_decref_locked(lp);
2004 lnet_net_unlock(LNET_LOCK_EX);
2008 * Clear the discovery error state, unless we're already discovering
2009 * this peer, in which case the error is current.
2011 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2013 spin_lock(&lp->lp_lock);
2014 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2015 lp->lp_dc_error = 0;
2016 spin_unlock(&lp->lp_lock);
2020 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2021 * dropped/retaken within this function. An lnet_peer_ni is passed in
2022 * because discovery could tear down an lnet_peer.
2025 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2028 struct lnet_peer *lp;
2032 lnet_net_unlock(cpt);
2033 lnet_net_lock(LNET_LOCK_EX);
2034 lp = lpni->lpni_peer_net->lpn_peer;
2035 lnet_peer_clear_discovery_error(lp);
2038 * We're willing to be interrupted. The lpni can become a
2039 * zombie if we race with DLC, so we must check for that.
2042 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2043 if (signal_pending(current))
2045 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2047 if (lp->lp_dc_error)
2049 if (lnet_peer_is_uptodate(lp))
2051 lnet_peer_queue_for_discovery(lp);
2053 * if caller requested a non-blocking operation then
2054 * return immediately. Once discovery is complete then the
2055 * peer ref will be decremented and any pending messages
2056 * that were stopped due to discovery will be transmitted.
2061 lnet_peer_addref_locked(lp);
2062 lnet_net_unlock(LNET_LOCK_EX);
2064 finish_wait(&lp->lp_dc_waitq, &wait);
2065 lnet_net_lock(LNET_LOCK_EX);
2066 lnet_peer_decref_locked(lp);
2067 /* Peer may have changed */
2068 lp = lpni->lpni_peer_net->lpn_peer;
2070 finish_wait(&lp->lp_dc_waitq, &wait);
2072 lnet_net_unlock(LNET_LOCK_EX);
2076 * If the peer has changed after we've discovered the older peer,
2077 * then we need to discovery the new peer to make sure the
2078 * interface information is up to date
2080 if (lp != lpni->lpni_peer_net->lpn_peer)
2083 if (signal_pending(current))
2085 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2087 else if (lp->lp_dc_error)
2088 rc = lp->lp_dc_error;
2090 CDEBUG(D_NET, "non-blocking discovery\n");
2091 else if (!lnet_peer_is_uptodate(lp))
2094 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2095 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2096 libcfs_nid2str(lpni->lpni_nid), rc,
2097 (!block) ? "pending discovery" : "discovery complete");
2102 /* Handle an incoming ack for a push. */
2104 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2106 struct lnet_ping_buffer *pbuf;
2108 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2109 spin_lock(&lp->lp_lock);
2110 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2111 lp->lp_push_error = ev->status;
2113 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2115 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2116 spin_unlock(&lp->lp_lock);
2118 CDEBUG(D_NET, "peer %s ev->status %d\n",
2119 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2122 /* Handle a Reply message. This is the reply to a Ping message. */
2124 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2126 struct lnet_ping_buffer *pbuf;
2129 spin_lock(&lp->lp_lock);
2132 * If some kind of error happened the contents of message
2133 * cannot be used. Set PING_FAILED to trigger a retry.
2136 lp->lp_state |= LNET_PEER_PING_FAILED;
2137 lp->lp_ping_error = ev->status;
2138 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2140 libcfs_nid2str(lp->lp_primary_nid),
2141 libcfs_nid2str(ev->source.nid));
2145 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2146 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2147 lnet_swap_pinginfo(pbuf);
2150 * A reply with invalid or corrupted info. Set PING_FAILED to
2153 rc = lnet_ping_info_validate(&pbuf->pb_info);
2155 lp->lp_state |= LNET_PEER_PING_FAILED;
2156 lp->lp_ping_error = 0;
2157 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2158 libcfs_nid2str(lp->lp_primary_nid), rc);
2163 * Update the MULTI_RAIL flag based on the reply. If the peer
2164 * was configured with DLC then the setting should match what
2167 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2168 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2169 /* Everything's fine */
2170 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2171 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2172 libcfs_nid2str(lp->lp_primary_nid));
2174 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2175 lnet_peer_clr_non_mr_pref_nids(lp);
2177 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2178 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2179 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2180 libcfs_nid2str(lp->lp_primary_nid));
2182 CERROR("Multi-Rail state vanished from %s\n",
2183 libcfs_nid2str(lp->lp_primary_nid));
2184 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2189 * Make sure we'll allocate the correct size ping buffer when
2192 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2193 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2196 * The peer may have discovery disabled at its end. Set
2197 * NO_DISCOVERY as appropriate.
2199 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2200 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2201 libcfs_nid2str(lp->lp_primary_nid));
2202 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2203 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2204 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2205 libcfs_nid2str(lp->lp_primary_nid));
2206 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2210 * Check for truncation of the Reply. Clear PING_SENT and set
2211 * PING_FAILED to trigger a retry.
2213 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2214 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2215 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2216 lp->lp_state |= LNET_PEER_PING_FAILED;
2217 lp->lp_ping_error = 0;
2218 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2219 libcfs_nid2str(lp->lp_primary_nid),
2220 pbuf->pb_info.pi_nnis);
2225 * Check the sequence numbers in the reply. These are only
2226 * available if the reply came from a Multi-Rail peer.
2228 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2229 pbuf->pb_info.pi_nnis > 1 &&
2230 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2231 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2232 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2233 libcfs_nid2str(lp->lp_primary_nid),
2234 LNET_PING_BUFFER_SEQNO(pbuf),
2239 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2240 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2243 /* We're happy with the state of the data in the buffer. */
2244 CDEBUG(D_NET, "peer %s data present %u\n",
2245 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2246 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2247 lnet_ping_buffer_decref(lp->lp_data);
2249 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2250 lnet_ping_buffer_addref(pbuf);
2253 lp->lp_state &= ~LNET_PEER_PING_SENT;
2254 spin_unlock(&lp->lp_lock);
2258 * Send event handling. Only matters for error cases, where we clean
2259 * up state on the peer and peer_ni that would otherwise be updated in
2260 * the REPLY event handler for a successful Ping, and the ACK event
2261 * handler for a successful Push.
2264 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2271 spin_lock(&lp->lp_lock);
2272 if (ev->msg_type == LNET_MSG_GET) {
2273 lp->lp_state &= ~LNET_PEER_PING_SENT;
2274 lp->lp_state |= LNET_PEER_PING_FAILED;
2275 lp->lp_ping_error = ev->status;
2276 } else { /* ev->msg_type == LNET_MSG_PUT */
2277 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2278 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2279 lp->lp_push_error = ev->status;
2281 spin_unlock(&lp->lp_lock);
2282 rc = LNET_REDISCOVER_PEER;
2284 CDEBUG(D_NET, "%s Send to %s: %d\n",
2285 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2286 libcfs_nid2str(ev->target.nid), rc);
2291 * Unlink event handling. This event is only seen if a call to
2292 * LNetMDUnlink() caused the event to be unlinked. If this call was
2293 * made after the event was set up in LNetGet() or LNetPut() then we
2294 * assume the Ping or Push timed out.
2297 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2299 spin_lock(&lp->lp_lock);
2300 /* We've passed through LNetGet() */
2301 if (lp->lp_state & LNET_PEER_PING_SENT) {
2302 lp->lp_state &= ~LNET_PEER_PING_SENT;
2303 lp->lp_state |= LNET_PEER_PING_FAILED;
2304 lp->lp_ping_error = -ETIMEDOUT;
2305 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2306 libcfs_nid2str(lp->lp_primary_nid));
2308 /* We've passed through LNetPut() */
2309 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2310 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2311 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2312 lp->lp_push_error = -ETIMEDOUT;
2313 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2314 libcfs_nid2str(lp->lp_primary_nid));
2316 spin_unlock(&lp->lp_lock);
2320 * Event handler for the discovery EQ.
2322 * Called with lnet_res_lock(cpt) held. The cpt is the
2323 * lnet_cpt_of_cookie() of the md handle cookie.
2325 static void lnet_discovery_event_handler(struct lnet_event *event)
2327 struct lnet_peer *lp = event->md.user_ptr;
2328 struct lnet_ping_buffer *pbuf;
2331 /* discovery needs to take another look */
2332 rc = LNET_REDISCOVER_PEER;
2334 CDEBUG(D_NET, "Received event: %d\n", event->type);
2336 switch (event->type) {
2337 case LNET_EVENT_ACK:
2338 lnet_discovery_event_ack(lp, event);
2340 case LNET_EVENT_REPLY:
2341 lnet_discovery_event_reply(lp, event);
2343 case LNET_EVENT_SEND:
2344 /* Only send failure triggers a retry. */
2345 rc = lnet_discovery_event_send(lp, event);
2347 case LNET_EVENT_UNLINK:
2348 /* LNetMDUnlink() was called */
2349 lnet_discovery_event_unlink(lp, event);
2352 /* Invalid events. */
2355 lnet_net_lock(LNET_LOCK_EX);
2356 if (event->unlinked) {
2357 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2358 lnet_ping_buffer_decref(pbuf);
2359 lnet_peer_decref_locked(lp);
2362 /* put peer back at end of request queue, if discovery not already
2364 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2365 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2366 wake_up(&the_lnet.ln_dc_waitq);
2368 lnet_net_unlock(LNET_LOCK_EX);
2372 * Build a peer from incoming data.
2374 * The NIDs in the incoming data are supposed to be structured as follows:
2377 * - other NIDs in same net
2378 * - NIDs in second net
2379 * - NIDs in third net
2381 * This due to the way the list of NIDs in the data is created.
2383 * Note that this function will mark the peer uptodate unless an
2384 * ENOMEM is encontered. All other errors are due to a conflict
2385 * between the DLC configuration and what discovery sees. We treat DLC
2386 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2387 * peer from becoming stuck in discovery.
2389 static int lnet_peer_merge_data(struct lnet_peer *lp,
2390 struct lnet_ping_buffer *pbuf)
2392 struct lnet_peer_ni *lpni;
2393 lnet_nid_t *curnis = NULL;
2394 lnet_nid_t *addnis = NULL;
2395 lnet_nid_t *delnis = NULL;
2405 flags = LNET_PEER_DISCOVERED;
2406 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2407 flags |= LNET_PEER_MULTI_RAIL;
2409 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2410 LIBCFS_ALLOC(curnis, nnis * sizeof(lnet_nid_t));
2411 LIBCFS_ALLOC(addnis, nnis * sizeof(lnet_nid_t));
2412 LIBCFS_ALLOC(delnis, nnis * sizeof(lnet_nid_t));
2413 if (!curnis || !addnis || !delnis) {
2421 /* Construct the list of NIDs present in peer. */
2423 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2424 curnis[ncurnis++] = lpni->lpni_nid;
2427 * Check for NIDs in pbuf not present in curnis[].
2428 * The loop starts at 1 to skip the loopback NID.
2430 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2431 for (j = 0; j < ncurnis; j++)
2432 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2435 addnis[naddnis++] = pbuf->pb_info.pi_ni[i].ns_nid;
2438 * Check for NIDs in curnis[] not present in pbuf.
2439 * The nested loop starts at 1 to skip the loopback NID.
2441 * But never add the loopback NID to delnis[]: if it is
2442 * present in curnis[] then this peer is for this node.
2444 for (i = 0; i < ncurnis; i++) {
2445 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2447 for (j = 1; j < pbuf->pb_info.pi_nnis; j++)
2448 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid)
2450 if (j == pbuf->pb_info.pi_nnis)
2451 delnis[ndelnis++] = curnis[i];
2454 for (i = 0; i < naddnis; i++) {
2455 rc = lnet_peer_add_nid(lp, addnis[i], flags);
2457 CERROR("Error adding NID %s to peer %s: %d\n",
2458 libcfs_nid2str(addnis[i]),
2459 libcfs_nid2str(lp->lp_primary_nid), rc);
2464 for (i = 0; i < ndelnis; i++) {
2465 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2467 CERROR("Error deleting NID %s from peer %s: %d\n",
2468 libcfs_nid2str(delnis[i]),
2469 libcfs_nid2str(lp->lp_primary_nid), rc);
2475 * Errors other than -ENOMEM are due to peers having been
2476 * configured with DLC. Ignore these because DLC overrides
2481 LIBCFS_FREE(curnis, nnis * sizeof(lnet_nid_t));
2482 LIBCFS_FREE(addnis, nnis * sizeof(lnet_nid_t));
2483 LIBCFS_FREE(delnis, nnis * sizeof(lnet_nid_t));
2484 lnet_ping_buffer_decref(pbuf);
2485 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2488 spin_lock(&lp->lp_lock);
2489 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2490 lp->lp_state |= LNET_PEER_FORCE_PING;
2491 spin_unlock(&lp->lp_lock);
2497 * The data in pbuf says lp is its primary peer, but the data was
2498 * received by a different peer. Try to update lp with the data.
2501 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2503 struct lnet_handle_md mdh;
2505 /* Queue lp for discovery, and force it on the request queue. */
2506 lnet_net_lock(LNET_LOCK_EX);
2507 if (lnet_peer_queue_for_discovery(lp))
2508 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2509 lnet_net_unlock(LNET_LOCK_EX);
2511 LNetInvalidateMDHandle(&mdh);
2514 * Decide whether we can move the peer to the DATA_PRESENT state.
2516 * We replace stale data for a multi-rail peer, repair PING_FAILED
2517 * status, and preempt FORCE_PING.
2519 * If after that we have DATA_PRESENT, we merge it into this peer.
2521 spin_lock(&lp->lp_lock);
2522 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2523 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2524 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2525 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2526 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2527 lnet_ping_buffer_decref(pbuf);
2532 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2533 lnet_ping_buffer_decref(lp->lp_data);
2535 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2537 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2538 mdh = lp->lp_ping_mdh;
2539 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2540 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2541 lp->lp_ping_error = 0;
2543 if (lp->lp_state & LNET_PEER_FORCE_PING)
2544 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2545 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2546 spin_unlock(&lp->lp_lock);
2548 if (!LNetMDHandleIsInvalid(mdh))
2552 return lnet_peer_merge_data(lp, pbuf);
2554 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2559 * Update a peer using the data received.
2561 static int lnet_peer_data_present(struct lnet_peer *lp)
2562 __must_hold(&lp->lp_lock)
2564 struct lnet_ping_buffer *pbuf;
2565 struct lnet_peer_ni *lpni;
2566 lnet_nid_t nid = LNET_NID_ANY;
2572 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2573 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2574 spin_unlock(&lp->lp_lock);
2577 * Modifications of peer structures are done while holding the
2578 * ln_api_mutex. A global lock is required because we may be
2579 * modifying multiple peer structures, and a mutex greatly
2580 * simplifies memory management.
2582 * The actual changes to the data structures must also protect
2583 * against concurrent lookups, for which the lnet_net_lock in
2584 * LNET_LOCK_EX mode is used.
2586 mutex_lock(&the_lnet.ln_api_mutex);
2587 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2593 * If this peer is not on the peer list then it is being torn
2594 * down, and our reference count may be all that is keeping it
2595 * alive. Don't do any work on it.
2597 if (list_empty(&lp->lp_peer_list))
2600 flags = LNET_PEER_DISCOVERED;
2601 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2602 flags |= LNET_PEER_MULTI_RAIL;
2605 * Check whether the primary NID in the message matches the
2606 * primary NID of the peer. If it does, update the peer, if
2607 * it it does not, check whether there is already a peer with
2608 * that primary NID. If no such peer exists, try to update
2609 * the primary NID of the current peer (allowed if it was
2610 * created due to message traffic) and complete the update.
2611 * If the peer did exist, hand off the data to it.
2613 * The peer for the loopback interface is a special case: this
2614 * is the peer for the local node, and we want to set its
2615 * primary NID to the correct value here. Moreover, this peer
2616 * can show up with only the loopback NID in the ping buffer.
2618 if (pbuf->pb_info.pi_nnis <= 1)
2620 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2621 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2622 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2624 rc = lnet_peer_merge_data(lp, pbuf);
2625 } else if (lp->lp_primary_nid == nid) {
2626 rc = lnet_peer_merge_data(lp, pbuf);
2628 lpni = lnet_find_peer_ni_locked(nid);
2630 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2632 CERROR("Primary NID error %s versus %s: %d\n",
2633 libcfs_nid2str(lp->lp_primary_nid),
2634 libcfs_nid2str(nid), rc);
2636 rc = lnet_peer_merge_data(lp, pbuf);
2639 rc = lnet_peer_set_primary_data(
2640 lpni->lpni_peer_net->lpn_peer, pbuf);
2641 lnet_peer_ni_decref_locked(lpni);
2645 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2646 mutex_unlock(&the_lnet.ln_api_mutex);
2648 spin_lock(&lp->lp_lock);
2649 /* Tell discovery to re-check the peer immediately. */
2651 rc = LNET_REDISCOVER_PEER;
2656 * A ping failed. Clear the PING_FAILED state and set the
2657 * FORCE_PING state, to ensure a retry even if discovery is
2658 * disabled. This avoids being left with incorrect state.
2660 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2661 __must_hold(&lp->lp_lock)
2663 struct lnet_handle_md mdh;
2666 mdh = lp->lp_ping_mdh;
2667 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2668 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2669 lp->lp_state |= LNET_PEER_FORCE_PING;
2670 rc = lp->lp_ping_error;
2671 lp->lp_ping_error = 0;
2672 spin_unlock(&lp->lp_lock);
2674 if (!LNetMDHandleIsInvalid(mdh))
2677 CDEBUG(D_NET, "peer %s:%d\n",
2678 libcfs_nid2str(lp->lp_primary_nid), rc);
2680 spin_lock(&lp->lp_lock);
2681 return rc ? rc : LNET_REDISCOVER_PEER;
2685 * Select NID to send a Ping or Push to.
2687 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2689 struct lnet_peer_ni *lpni;
2691 /* Look for a direct-connected NID for this peer. */
2693 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2694 if (!lnet_is_peer_ni_healthy_locked(lpni))
2696 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2701 return lpni->lpni_nid;
2703 /* Look for a routed-connected NID for this peer. */
2705 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2706 if (!lnet_is_peer_ni_healthy_locked(lpni))
2708 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2713 return lpni->lpni_nid;
2715 return LNET_NID_ANY;
2718 /* Active side of ping. */
2719 static int lnet_peer_send_ping(struct lnet_peer *lp)
2720 __must_hold(&lp->lp_lock)
2722 struct lnet_md md = { NULL };
2723 struct lnet_process_id id;
2724 struct lnet_ping_buffer *pbuf;
2729 lp->lp_state |= LNET_PEER_PING_SENT;
2730 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2731 spin_unlock(&lp->lp_lock);
2733 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2734 pbuf = lnet_ping_buffer_alloc(nnis, GFP_NOFS);
2740 /* initialize md content */
2741 md.start = &pbuf->pb_info;
2742 md.length = LNET_PING_INFO_SIZE(nnis);
2743 md.threshold = 2; /* GET/REPLY */
2745 md.options = LNET_MD_TRUNCATE;
2747 md.eq_handle = the_lnet.ln_dc_eqh;
2749 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_ping_mdh);
2751 lnet_ping_buffer_decref(pbuf);
2752 CERROR("Can't bind MD: %d\n", rc);
2755 cpt = lnet_net_lock_current();
2756 /* Refcount for MD. */
2757 lnet_peer_addref_locked(lp);
2758 id.pid = LNET_PID_LUSTRE;
2759 id.nid = lnet_peer_select_nid(lp);
2760 lnet_net_unlock(cpt);
2762 if (id.nid == LNET_NID_ANY) {
2764 goto fail_unlink_md;
2767 rc = LNetGet(LNET_NID_ANY, lp->lp_ping_mdh, id,
2768 LNET_RESERVED_PORTAL,
2769 LNET_PROTO_PING_MATCHBITS, 0);
2772 goto fail_unlink_md;
2774 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2776 spin_lock(&lp->lp_lock);
2780 LNetMDUnlink(lp->lp_ping_mdh);
2781 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2783 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2785 * The errors that get us here are considered hard errors and
2786 * cause Discovery to terminate. So we clear PING_SENT, but do
2787 * not set either PING_FAILED or FORCE_PING. In fact we need
2788 * to clear PING_FAILED, because the unlink event handler will
2789 * have set it if we called LNetMDUnlink() above.
2791 spin_lock(&lp->lp_lock);
2792 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2797 * This function exists because you cannot call LNetMDUnlink() from an
2800 static int lnet_peer_push_failed(struct lnet_peer *lp)
2801 __must_hold(&lp->lp_lock)
2803 struct lnet_handle_md mdh;
2806 mdh = lp->lp_push_mdh;
2807 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2808 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2809 rc = lp->lp_push_error;
2810 lp->lp_push_error = 0;
2811 spin_unlock(&lp->lp_lock);
2813 if (!LNetMDHandleIsInvalid(mdh))
2816 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2817 spin_lock(&lp->lp_lock);
2818 return rc ? rc : LNET_REDISCOVER_PEER;
2821 /* Active side of push. */
2822 static int lnet_peer_send_push(struct lnet_peer *lp)
2823 __must_hold(&lp->lp_lock)
2825 struct lnet_ping_buffer *pbuf;
2826 struct lnet_process_id id;
2831 /* Don't push to a non-multi-rail peer. */
2832 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2833 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2837 lp->lp_state |= LNET_PEER_PUSH_SENT;
2838 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2839 spin_unlock(&lp->lp_lock);
2841 cpt = lnet_net_lock_current();
2842 pbuf = the_lnet.ln_ping_target;
2843 lnet_ping_buffer_addref(pbuf);
2844 lnet_net_unlock(cpt);
2846 /* Push source MD */
2847 md.start = &pbuf->pb_info;
2848 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2849 md.threshold = 2; /* Put/Ack */
2852 md.eq_handle = the_lnet.ln_dc_eqh;
2855 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2857 lnet_ping_buffer_decref(pbuf);
2858 CERROR("Can't bind push source MD: %d\n", rc);
2861 cpt = lnet_net_lock_current();
2862 /* Refcount for MD. */
2863 lnet_peer_addref_locked(lp);
2864 id.pid = LNET_PID_LUSTRE;
2865 id.nid = lnet_peer_select_nid(lp);
2866 lnet_net_unlock(cpt);
2868 if (id.nid == LNET_NID_ANY) {
2873 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2874 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2875 LNET_PROTO_PING_MATCHBITS, 0, 0);
2880 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2882 spin_lock(&lp->lp_lock);
2886 LNetMDUnlink(lp->lp_push_mdh);
2887 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2889 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2891 * The errors that get us here are considered hard errors and
2892 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2893 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2894 * because the unlink event handler will have set it if we
2895 * called LNetMDUnlink() above.
2897 spin_lock(&lp->lp_lock);
2898 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2903 * An unrecoverable error was encountered during discovery.
2904 * Set error status in peer and abort discovery.
2906 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2908 CDEBUG(D_NET, "Discovery error %s: %d\n",
2909 libcfs_nid2str(lp->lp_primary_nid), error);
2911 spin_lock(&lp->lp_lock);
2912 lp->lp_dc_error = error;
2913 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2914 lp->lp_state |= LNET_PEER_REDISCOVER;
2915 spin_unlock(&lp->lp_lock);
2919 * Mark the peer as discovered.
2921 static int lnet_peer_discovered(struct lnet_peer *lp)
2922 __must_hold(&lp->lp_lock)
2924 lp->lp_state |= LNET_PEER_DISCOVERED;
2925 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2926 LNET_PEER_REDISCOVER);
2928 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2934 * Mark the peer as to be rediscovered.
2936 static int lnet_peer_rediscover(struct lnet_peer *lp)
2937 __must_hold(&lp->lp_lock)
2939 lp->lp_state |= LNET_PEER_REDISCOVER;
2940 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2942 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2948 * Returns the first peer on the ln_dc_working queue if its timeout
2949 * has expired. Takes the current time as an argument so as to not
2950 * obsessively re-check the clock. The oldest discovery request will
2951 * be at the head of the queue.
2953 static struct lnet_peer *lnet_peer_get_dc_timed_out(time64_t now)
2955 struct lnet_peer *lp;
2957 if (list_empty(&the_lnet.ln_dc_working))
2959 lp = list_first_entry(&the_lnet.ln_dc_working,
2960 struct lnet_peer, lp_dc_list);
2961 if (now < lp->lp_last_queued + lnet_transaction_timeout)
2967 * Discovering this peer is taking too long. Cancel any Ping or Push
2968 * that discovery is waiting on by unlinking the relevant MDs. The
2969 * lnet_discovery_event_handler() will proceed from here and complete
2972 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
2974 struct lnet_handle_md ping_mdh;
2975 struct lnet_handle_md push_mdh;
2977 LNetInvalidateMDHandle(&ping_mdh);
2978 LNetInvalidateMDHandle(&push_mdh);
2980 spin_lock(&lp->lp_lock);
2981 if (lp->lp_state & LNET_PEER_PING_SENT) {
2982 ping_mdh = lp->lp_ping_mdh;
2983 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2985 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2986 push_mdh = lp->lp_push_mdh;
2987 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2989 spin_unlock(&lp->lp_lock);
2991 if (!LNetMDHandleIsInvalid(ping_mdh))
2992 LNetMDUnlink(ping_mdh);
2993 if (!LNetMDHandleIsInvalid(push_mdh))
2994 LNetMDUnlink(push_mdh);
2998 * Wait for work to be queued or some other change that must be
2999 * attended to. Returns non-zero if the discovery thread should shut
3002 static int lnet_peer_discovery_wait_for_work(void)
3009 cpt = lnet_net_lock_current();
3011 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3012 TASK_INTERRUPTIBLE);
3013 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3015 if (lnet_push_target_resize_needed())
3017 if (!list_empty(&the_lnet.ln_dc_request))
3019 if (!list_empty(&the_lnet.ln_msg_resend))
3021 if (lnet_peer_get_dc_timed_out(ktime_get_real_seconds()))
3023 lnet_net_unlock(cpt);
3026 * wakeup max every second to check if there are peers that
3027 * have been stuck on the working queue for greater than
3030 schedule_timeout(cfs_time_seconds(1));
3031 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3032 cpt = lnet_net_lock_current();
3034 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3036 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3039 lnet_net_unlock(cpt);
3041 CDEBUG(D_NET, "woken: %d\n", rc);
3047 * Messages that were pending on a destroyed peer will be put on a global
3048 * resend list. The message resend list will be checked by
3049 * the discovery thread when it wakes up, and will resend messages. These
3050 * messages can still be sendable in the case the lpni which was the initial
3051 * cause of the message re-queue was transfered to another peer.
3053 * It is possible that LNet could be shutdown while we're iterating
3054 * through the list. lnet_shudown_lndnets() will attempt to access the
3055 * resend list, but will have to wait until the spinlock is released, by
3056 * which time there shouldn't be any more messages on the resend list.
3057 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3058 * for the messages so they can be released. The other case is that
3059 * lnet_shudown_lndnets() can finalize all the messages before this
3060 * function can visit the resend list, in which case this function will be
3063 static void lnet_resend_msgs(void)
3065 struct lnet_msg *msg, *tmp;
3066 struct list_head resend;
3069 INIT_LIST_HEAD(&resend);
3071 spin_lock(&the_lnet.ln_msg_resend_lock);
3072 list_splice(&the_lnet.ln_msg_resend, &resend);
3073 spin_unlock(&the_lnet.ln_msg_resend_lock);
3075 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3076 list_del_init(&msg->msg_list);
3077 rc = lnet_send(msg->msg_src_nid_param, msg,
3078 msg->msg_rtr_nid_param);
3080 CNETERR("Error sending %s to %s: %d\n",
3081 lnet_msgtyp2str(msg->msg_type),
3082 libcfs_id2str(msg->msg_target), rc);
3083 lnet_finalize(msg, rc);
3088 /* The discovery thread. */
3089 static int lnet_peer_discovery(void *arg)
3091 struct lnet_peer *lp;
3095 CDEBUG(D_NET, "started\n");
3096 cfs_block_allsigs();
3099 if (lnet_peer_discovery_wait_for_work())
3104 if (lnet_push_target_resize_needed())
3105 lnet_push_target_resize();
3107 lnet_net_lock(LNET_LOCK_EX);
3108 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3112 * Process all incoming discovery work requests. When
3113 * discovery must wait on a peer to change state, it
3114 * is added to the tail of the ln_dc_working queue. A
3115 * timestamp keeps track of when the peer was added,
3116 * so we can time out discovery requests that take too
3119 while (!list_empty(&the_lnet.ln_dc_request)) {
3120 lp = list_first_entry(&the_lnet.ln_dc_request,
3121 struct lnet_peer, lp_dc_list);
3122 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3124 * set the time the peer was put on the dc_working
3125 * queue. It shouldn't remain on the queue
3126 * forever, in case the GET message (for ping)
3127 * doesn't get a REPLY or the PUT message (for
3128 * push) doesn't get an ACK.
3130 * TODO: LNet Health will deal with this scenario
3133 lp->lp_last_queued = ktime_get_real_seconds();
3134 lnet_net_unlock(LNET_LOCK_EX);
3137 * Select an action depending on the state of
3138 * the peer and whether discovery is disabled.
3139 * The check whether discovery is disabled is
3140 * done after the code that handles processing
3141 * for arrived data, cleanup for failures, and
3142 * forcing a Ping or Push.
3144 spin_lock(&lp->lp_lock);
3145 CDEBUG(D_NET, "peer %s state %#x\n",
3146 libcfs_nid2str(lp->lp_primary_nid),
3148 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3149 rc = lnet_peer_data_present(lp);
3150 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3151 rc = lnet_peer_ping_failed(lp);
3152 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3153 rc = lnet_peer_push_failed(lp);
3154 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3155 rc = lnet_peer_send_ping(lp);
3156 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3157 rc = lnet_peer_send_push(lp);
3158 else if (lnet_peer_discovery_disabled)
3159 rc = lnet_peer_rediscover(lp);
3160 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3161 rc = lnet_peer_send_ping(lp);
3162 else if (lnet_peer_needs_push(lp))
3163 rc = lnet_peer_send_push(lp);
3165 rc = lnet_peer_discovered(lp);
3166 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3167 libcfs_nid2str(lp->lp_primary_nid),
3169 spin_unlock(&lp->lp_lock);
3171 lnet_net_lock(LNET_LOCK_EX);
3172 if (rc == LNET_REDISCOVER_PEER) {
3173 list_move(&lp->lp_dc_list,
3174 &the_lnet.ln_dc_request);
3176 lnet_peer_discovery_error(lp, rc);
3178 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3179 lnet_peer_discovery_complete(lp);
3180 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3185 * Now that the ln_dc_request queue has been emptied
3186 * check the ln_dc_working queue for peers that are
3187 * taking too long. Move all that are found to the
3188 * ln_dc_expired queue and time out any pending
3189 * Ping or Push. We have to drop the lnet_net_lock
3190 * in the loop because lnet_peer_cancel_discovery()
3191 * calls LNetMDUnlink().
3193 now = ktime_get_real_seconds();
3194 while ((lp = lnet_peer_get_dc_timed_out(now)) != NULL) {
3195 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3196 lnet_net_unlock(LNET_LOCK_EX);
3197 lnet_peer_cancel_discovery(lp);
3198 lnet_net_lock(LNET_LOCK_EX);
3201 lnet_net_unlock(LNET_LOCK_EX);
3204 CDEBUG(D_NET, "stopping\n");
3206 * Clean up before telling lnet_peer_discovery_stop() that
3207 * we're done. Use wake_up() below to somewhat reduce the
3208 * size of the thundering herd if there are multiple threads
3209 * waiting on discovery of a single peer.
3211 LNetEQFree(the_lnet.ln_dc_eqh);
3212 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3214 /* Queue cleanup 1: stop all pending pings and pushes. */
3215 lnet_net_lock(LNET_LOCK_EX);
3216 while (!list_empty(&the_lnet.ln_dc_working)) {
3217 lp = list_first_entry(&the_lnet.ln_dc_working,
3218 struct lnet_peer, lp_dc_list);
3219 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3220 lnet_net_unlock(LNET_LOCK_EX);
3221 lnet_peer_cancel_discovery(lp);
3222 lnet_net_lock(LNET_LOCK_EX);
3224 lnet_net_unlock(LNET_LOCK_EX);
3226 /* Queue cleanup 2: wait for the expired queue to clear. */
3227 while (!list_empty(&the_lnet.ln_dc_expired))
3228 schedule_timeout(cfs_time_seconds(1));
3230 /* Queue cleanup 3: clear the request queue. */
3231 lnet_net_lock(LNET_LOCK_EX);
3232 while (!list_empty(&the_lnet.ln_dc_request)) {
3233 lp = list_first_entry(&the_lnet.ln_dc_request,
3234 struct lnet_peer, lp_dc_list);
3235 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3236 lnet_peer_discovery_complete(lp);
3238 lnet_net_unlock(LNET_LOCK_EX);
3240 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3241 wake_up(&the_lnet.ln_dc_waitq);
3243 CDEBUG(D_NET, "stopped\n");
3248 /* ln_api_mutex is held on entry. */
3249 int lnet_peer_discovery_start(void)
3251 struct task_struct *task;
3254 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3257 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3259 CERROR("Can't allocate discovery EQ: %d\n", rc);
3263 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3264 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3267 CERROR("Can't start peer discovery thread: %d\n", rc);
3269 LNetEQFree(the_lnet.ln_dc_eqh);
3270 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3272 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3275 CDEBUG(D_NET, "discovery start: %d\n", rc);
3280 /* ln_api_mutex is held on entry. */
3281 void lnet_peer_discovery_stop(void)
3283 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3286 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3287 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3288 wake_up(&the_lnet.ln_dc_waitq);
3290 wait_event(the_lnet.ln_dc_waitq,
3291 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3293 LASSERT(list_empty(&the_lnet.ln_dc_request));
3294 LASSERT(list_empty(&the_lnet.ln_dc_working));
3295 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3297 CDEBUG(D_NET, "discovery stopped\n");
3303 lnet_debug_peer(lnet_nid_t nid)
3305 char *aliveness = "NA";
3306 struct lnet_peer_ni *lp;
3309 cpt = lnet_cpt_of_nid(nid, NULL);
3312 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3314 lnet_net_unlock(cpt);
3315 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3319 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3320 aliveness = lp->lpni_alive ? "up" : "down";
3322 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3323 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3324 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3325 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3326 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3328 lnet_peer_ni_decref_locked(lp);
3330 lnet_net_unlock(cpt);
3333 /* Gathering information for userspace. */
3335 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3336 char aliveness[LNET_MAX_STR_LEN],
3337 __u32 *cpt_iter, __u32 *refcount,
3338 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3339 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3340 __u32 *peer_tx_qnob)
3342 struct lnet_peer_table *peer_table;
3343 struct lnet_peer_ni *lp;
3348 /* get the number of CPTs */
3349 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3351 /* if the cpt number to be examined is >= the number of cpts in
3352 * the system then indicate that there are no more cpts to examin
3354 if (*cpt_iter >= lncpt)
3357 /* get the current table */
3358 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3359 /* if the ptable is NULL then there are no more cpts to examine */
3360 if (peer_table == NULL)
3363 lnet_net_lock(*cpt_iter);
3365 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3366 struct list_head *peers = &peer_table->pt_hash[j];
3368 list_for_each_entry(lp, peers, lpni_hashlist) {
3369 if (peer_index-- > 0)
3372 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3373 if (lnet_isrouter(lp) ||
3374 lnet_peer_aliveness_enabled(lp))
3375 snprintf(aliveness, LNET_MAX_STR_LEN,
3376 lp->lpni_alive ? "up" : "down");
3378 *nid = lp->lpni_nid;
3379 *refcount = atomic_read(&lp->lpni_refcount);
3380 *ni_peer_tx_credits =
3381 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3382 *peer_tx_credits = lp->lpni_txcredits;
3383 *peer_rtr_credits = lp->lpni_rtrcredits;
3384 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3385 *peer_tx_qnob = lp->lpni_txqnob;
3391 lnet_net_unlock(*cpt_iter);
3395 return found ? 0 : -ENOENT;
3398 /* ln_api_mutex is held, which keeps the peer list stable */
3399 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3401 struct lnet_ioctl_element_stats *lpni_stats;
3402 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3403 struct lnet_peer_ni_credit_info *lpni_info;
3404 struct lnet_peer_ni *lpni;
3405 struct lnet_peer *lp;
3410 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3417 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3418 + sizeof(*lpni_msg_stats);
3419 size *= lp->lp_nnis;
3420 if (size > cfg->prcfg_size) {
3421 cfg->prcfg_size = size;
3426 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3427 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3428 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3429 cfg->prcfg_count = lp->lp_nnis;
3430 cfg->prcfg_size = size;
3431 cfg->prcfg_state = lp->lp_state;
3433 /* Allocate helper buffers. */
3435 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3438 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3441 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3442 if (!lpni_msg_stats)
3443 goto out_free_stats;
3448 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3449 nid = lpni->lpni_nid;
3450 if (copy_to_user(bulk, &nid, sizeof(nid)))
3451 goto out_free_msg_stats;
3452 bulk += sizeof(nid);
3454 memset(lpni_info, 0, sizeof(*lpni_info));
3455 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3456 if (lnet_isrouter(lpni) ||
3457 lnet_peer_aliveness_enabled(lpni))
3458 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3459 lpni->lpni_alive ? "up" : "down");
3461 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3462 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3463 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3464 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3465 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3466 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3467 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3468 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3469 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3470 goto out_free_msg_stats;
3471 bulk += sizeof(*lpni_info);
3473 memset(lpni_stats, 0, sizeof(*lpni_stats));
3474 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3475 LNET_STATS_TYPE_SEND);
3476 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3477 LNET_STATS_TYPE_RECV);
3478 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3479 LNET_STATS_TYPE_DROP);
3480 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3481 goto out_free_msg_stats;
3482 bulk += sizeof(*lpni_stats);
3483 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3484 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3485 goto out_free_msg_stats;
3486 bulk += sizeof(*lpni_msg_stats);
3491 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3493 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3495 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3497 lnet_peer_decref_locked(lp);