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_recovery);
170 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
171 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
173 spin_lock_init(&lpni->lpni_lock);
175 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
176 lpni->lpni_last_alive = ktime_get_seconds(); /* assumes alive */
177 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
178 lpni->lpni_nid = nid;
179 lpni->lpni_cpt = cpt;
180 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
182 net = lnet_get_net_locked(LNET_NIDNET(nid));
183 lpni->lpni_net = net;
185 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
186 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
187 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
188 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
191 * This peer_ni is not on a local network, so we
192 * cannot add the credits here. In case the net is
193 * added later, add the peer_ni to the remote peer ni
194 * list so it can be easily found and revisited.
196 /* FIXME: per-net implementation instead? */
197 atomic_inc(&lpni->lpni_refcount);
198 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
199 &the_lnet.ln_remote_peer_ni_list);
202 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
207 static struct lnet_peer_net *
208 lnet_peer_net_alloc(__u32 net_id)
210 struct lnet_peer_net *lpn;
212 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
216 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
217 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
218 lpn->lpn_net_id = net_id;
220 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
226 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
228 struct lnet_peer *lp;
230 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
232 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
233 LASSERT(list_empty(&lpn->lpn_peer_nis));
234 LASSERT(list_empty(&lpn->lpn_peer_nets));
236 lpn->lpn_peer = NULL;
237 LIBCFS_FREE(lpn, sizeof(*lpn));
239 lnet_peer_decref_locked(lp);
242 static struct lnet_peer *
243 lnet_peer_alloc(lnet_nid_t nid)
245 struct lnet_peer *lp;
247 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
251 INIT_LIST_HEAD(&lp->lp_peer_list);
252 INIT_LIST_HEAD(&lp->lp_peer_nets);
253 INIT_LIST_HEAD(&lp->lp_dc_list);
254 INIT_LIST_HEAD(&lp->lp_dc_pendq);
255 init_waitqueue_head(&lp->lp_dc_waitq);
256 spin_lock_init(&lp->lp_lock);
257 lp->lp_primary_nid = nid;
259 * Turn off discovery for loopback peer. If you're creating a peer
260 * for the loopback interface then that was initiated when we
261 * attempted to send a message over the loopback. There is no need
262 * to ever use a different interface when sending messages to
265 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
266 lp->lp_state = LNET_PEER_NO_DISCOVERY;
267 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
269 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
275 lnet_destroy_peer_locked(struct lnet_peer *lp)
277 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
279 LASSERT(atomic_read(&lp->lp_refcount) == 0);
280 LASSERT(list_empty(&lp->lp_peer_nets));
281 LASSERT(list_empty(&lp->lp_peer_list));
282 LASSERT(list_empty(&lp->lp_dc_list));
285 lnet_ping_buffer_decref(lp->lp_data);
288 * if there are messages still on the pending queue, then make
289 * sure to queue them on the ln_msg_resend list so they can be
290 * resent at a later point if the discovery thread is still
292 * If the discovery thread has stopped, then the wakeup will be a
293 * no-op, and it is expected the lnet_shutdown_lndnets() will
294 * eventually be called, which will traverse this list and
295 * finalize the messages on the list.
296 * We can not resend them now because we're holding the cpt lock.
297 * Releasing the lock can cause an inconsistent state
299 spin_lock(&the_lnet.ln_msg_resend_lock);
300 spin_lock(&lp->lp_lock);
301 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
302 spin_unlock(&lp->lp_lock);
303 spin_unlock(&the_lnet.ln_msg_resend_lock);
304 wake_up(&the_lnet.ln_dc_waitq);
306 LIBCFS_FREE(lp, sizeof(*lp));
310 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
311 * that peer_net, detach the peer_net from the peer.
313 * Call with lnet_net_lock/EX held
316 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
318 struct lnet_peer_table *ptable;
319 struct lnet_peer_net *lpn;
320 struct lnet_peer *lp;
323 * Belts and suspenders: gracefully handle teardown of a
324 * partially connected peer_ni.
326 lpn = lpni->lpni_peer_net;
328 list_del_init(&lpni->lpni_peer_nis);
330 * If there are no lpni's left, we detach lpn from
331 * lp_peer_nets, so it cannot be found anymore.
333 if (list_empty(&lpn->lpn_peer_nis))
334 list_del_init(&lpn->lpn_peer_nets);
336 /* Update peer NID count. */
341 * If there are no more peer nets, make the peer unfindable
342 * via the peer_tables.
344 * Otherwise, if the peer is DISCOVERED, tell discovery to
345 * take another look at it. This is a no-op if discovery for
346 * this peer did the detaching.
348 if (list_empty(&lp->lp_peer_nets)) {
349 list_del_init(&lp->lp_peer_list);
350 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
352 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
353 /* Discovery isn't running, nothing to do here. */
354 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
355 lnet_peer_queue_for_discovery(lp);
356 wake_up(&the_lnet.ln_dc_waitq);
358 CDEBUG(D_NET, "peer %s NID %s\n",
359 libcfs_nid2str(lp->lp_primary_nid),
360 libcfs_nid2str(lpni->lpni_nid));
363 /* called with lnet_net_lock LNET_LOCK_EX held */
365 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
367 struct lnet_peer_table *ptable = NULL;
369 /* don't remove a peer_ni if it's also a gateway */
370 if (lpni->lpni_rtr_refcount > 0) {
371 CERROR("Peer NI %s is a gateway. Can not delete it\n",
372 libcfs_nid2str(lpni->lpni_nid));
376 lnet_peer_remove_from_remote_list(lpni);
378 /* remove peer ni from the hash list. */
379 list_del_init(&lpni->lpni_hashlist);
382 * indicate the peer is being deleted so the monitor thread can
383 * remove it from the recovery queue.
385 spin_lock(&lpni->lpni_lock);
386 lpni->lpni_state |= LNET_PEER_NI_DELETING;
387 spin_unlock(&lpni->lpni_lock);
389 /* decrement the ref count on the peer table */
390 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
391 LASSERT(ptable->pt_number > 0);
395 * The peer_ni can no longer be found with a lookup. But there
396 * can be current users, so keep track of it on the zombie
397 * list until the reference count has gone to zero.
399 * The last reference may be lost in a place where the
400 * lnet_net_lock locks only a single cpt, and that cpt may not
401 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
404 spin_lock(&ptable->pt_zombie_lock);
405 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
406 ptable->pt_zombies++;
407 spin_unlock(&ptable->pt_zombie_lock);
409 /* no need to keep this peer_ni on the hierarchy anymore */
410 lnet_peer_detach_peer_ni_locked(lpni);
412 /* remove hashlist reference on peer_ni */
413 lnet_peer_ni_decref_locked(lpni);
418 void lnet_peer_uninit(void)
420 struct lnet_peer_ni *lpni, *tmp;
422 lnet_net_lock(LNET_LOCK_EX);
424 /* remove all peer_nis from the remote peer and the hash list */
425 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
426 lpni_on_remote_peer_ni_list)
427 lnet_peer_ni_del_locked(lpni);
429 lnet_peer_tables_destroy();
431 lnet_net_unlock(LNET_LOCK_EX);
435 lnet_peer_del_locked(struct lnet_peer *peer)
437 struct lnet_peer_ni *lpni = NULL, *lpni2;
440 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
442 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
443 while (lpni != NULL) {
444 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
445 rc = lnet_peer_ni_del_locked(lpni);
455 lnet_peer_del(struct lnet_peer *peer)
457 lnet_net_lock(LNET_LOCK_EX);
458 lnet_peer_del_locked(peer);
459 lnet_net_unlock(LNET_LOCK_EX);
465 * Delete a NID from a peer. Call with ln_api_mutex held.
468 * -EPERM: Non-DLC deletion from DLC-configured peer.
469 * -ENOENT: No lnet_peer_ni corresponding to the nid.
470 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
471 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
474 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
476 struct lnet_peer_ni *lpni;
477 lnet_nid_t primary_nid = lp->lp_primary_nid;
480 if (!(flags & LNET_PEER_CONFIGURED)) {
481 if (lp->lp_state & LNET_PEER_CONFIGURED) {
486 lpni = lnet_find_peer_ni_locked(nid);
491 lnet_peer_ni_decref_locked(lpni);
492 if (lp != lpni->lpni_peer_net->lpn_peer) {
498 * This function only allows deletion of the primary NID if it
501 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
506 lnet_net_lock(LNET_LOCK_EX);
508 rc = lnet_peer_ni_del_locked(lpni);
510 lnet_net_unlock(LNET_LOCK_EX);
513 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
514 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
520 lnet_peer_table_cleanup_locked(struct lnet_net *net,
521 struct lnet_peer_table *ptable)
524 struct lnet_peer_ni *next;
525 struct lnet_peer_ni *lpni;
526 struct lnet_peer *peer;
528 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
529 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
531 if (net != NULL && net != lpni->lpni_net)
534 peer = lpni->lpni_peer_net->lpn_peer;
535 if (peer->lp_primary_nid != lpni->lpni_nid) {
536 lnet_peer_ni_del_locked(lpni);
540 * Removing the primary NID implies removing
541 * the entire peer. Advance next beyond any
542 * peer_ni that belongs to the same peer.
544 list_for_each_entry_from(next, &ptable->pt_hash[i],
546 if (next->lpni_peer_net->lpn_peer != peer)
549 lnet_peer_del_locked(peer);
555 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
559 spin_lock(&ptable->pt_zombie_lock);
560 while (ptable->pt_zombies) {
561 spin_unlock(&ptable->pt_zombie_lock);
563 if (is_power_of_2(i)) {
565 "Waiting for %d zombies on peer table\n",
568 set_current_state(TASK_UNINTERRUPTIBLE);
569 schedule_timeout(cfs_time_seconds(1) >> 1);
570 spin_lock(&ptable->pt_zombie_lock);
572 spin_unlock(&ptable->pt_zombie_lock);
576 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
577 struct lnet_peer_table *ptable)
579 struct lnet_peer_ni *lp;
580 struct lnet_peer_ni *tmp;
584 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
585 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
587 if (net != lp->lpni_net)
590 if (lp->lpni_rtr_refcount == 0)
593 lpni_nid = lp->lpni_nid;
595 lnet_net_unlock(LNET_LOCK_EX);
596 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
597 lnet_net_lock(LNET_LOCK_EX);
603 lnet_peer_tables_cleanup(struct lnet_net *net)
606 struct lnet_peer_table *ptable;
608 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
609 /* If just deleting the peers for a NI, get rid of any routes these
610 * peers are gateways for. */
611 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
612 lnet_net_lock(LNET_LOCK_EX);
613 lnet_peer_table_del_rtrs_locked(net, ptable);
614 lnet_net_unlock(LNET_LOCK_EX);
617 /* Start the cleanup process */
618 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
619 lnet_net_lock(LNET_LOCK_EX);
620 lnet_peer_table_cleanup_locked(net, ptable);
621 lnet_net_unlock(LNET_LOCK_EX);
624 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
625 lnet_peer_ni_finalize_wait(ptable);
628 static struct lnet_peer_ni *
629 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
631 struct list_head *peers;
632 struct lnet_peer_ni *lp;
634 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
636 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
637 list_for_each_entry(lp, peers, lpni_hashlist) {
638 if (lp->lpni_nid == nid) {
639 lnet_peer_ni_addref_locked(lp);
647 struct lnet_peer_ni *
648 lnet_find_peer_ni_locked(lnet_nid_t nid)
650 struct lnet_peer_ni *lpni;
651 struct lnet_peer_table *ptable;
654 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
656 ptable = the_lnet.ln_peer_tables[cpt];
657 lpni = lnet_get_peer_ni_locked(ptable, nid);
663 lnet_find_peer(lnet_nid_t nid)
665 struct lnet_peer_ni *lpni;
666 struct lnet_peer *lp = NULL;
669 cpt = lnet_net_lock_current();
670 lpni = lnet_find_peer_ni_locked(nid);
672 lp = lpni->lpni_peer_net->lpn_peer;
673 lnet_peer_addref_locked(lp);
674 lnet_peer_ni_decref_locked(lpni);
676 lnet_net_unlock(cpt);
681 struct lnet_peer_ni *
682 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
683 struct lnet_peer_net *peer_net,
684 struct lnet_peer_ni *prev)
686 struct lnet_peer_ni *lpni;
687 struct lnet_peer_net *net = peer_net;
691 if (list_empty(&peer->lp_peer_nets))
694 net = list_entry(peer->lp_peer_nets.next,
695 struct lnet_peer_net,
698 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
704 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
706 * if you reached the end of the peer ni list and the peer
707 * net is specified then there are no more peer nis in that
714 * we reached the end of this net ni list. move to the
717 if (prev->lpni_peer_net->lpn_peer_nets.next ==
719 /* no more nets and no more NIs. */
722 /* get the next net */
723 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
724 struct lnet_peer_net,
726 /* get the ni on it */
727 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
733 /* there are more nis left */
734 lpni = list_entry(prev->lpni_peer_nis.next,
735 struct lnet_peer_ni, lpni_peer_nis);
740 /* Call with the ln_api_mutex held */
741 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
743 struct lnet_process_id id;
744 struct lnet_peer_table *ptable;
745 struct lnet_peer *lp;
754 if (the_lnet.ln_state != LNET_STATE_RUNNING)
757 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
760 * Count the number of peers, and return E2BIG if the buffer
761 * is too small. We'll also return the desired size.
764 for (cpt = 0; cpt < lncpt; cpt++) {
765 ptable = the_lnet.ln_peer_tables[cpt];
766 count += ptable->pt_peers;
768 size = count * sizeof(*ids);
773 * Walk the peer lists and copy out the primary nids.
774 * This is safe because the peer lists are only modified
775 * while the ln_api_mutex is held. So we don't need to
776 * hold the lnet_net_lock as well, and can therefore
777 * directly call copy_to_user().
780 memset(&id, 0, sizeof(id));
781 id.pid = LNET_PID_LUSTRE;
783 for (cpt = 0; cpt < lncpt; cpt++) {
784 ptable = the_lnet.ln_peer_tables[cpt];
785 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
788 id.nid = lp->lp_primary_nid;
789 if (copy_to_user(&ids[i], &id, sizeof(id)))
802 * Start pushes to peers that need to be updated for a configuration
803 * change on this node.
806 lnet_push_update_to_peers(int force)
808 struct lnet_peer_table *ptable;
809 struct lnet_peer *lp;
813 lnet_net_lock(LNET_LOCK_EX);
814 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
815 for (cpt = 0; cpt < lncpt; cpt++) {
816 ptable = the_lnet.ln_peer_tables[cpt];
817 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
819 spin_lock(&lp->lp_lock);
820 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
821 lp->lp_state |= LNET_PEER_FORCE_PUSH;
822 spin_unlock(&lp->lp_lock);
824 if (lnet_peer_needs_push(lp))
825 lnet_peer_queue_for_discovery(lp);
828 lnet_net_unlock(LNET_LOCK_EX);
829 wake_up(&the_lnet.ln_dc_waitq);
833 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
834 * this is a preferred point-to-point path. Call with lnet_net_lock in
838 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
842 if (lpni->lpni_pref_nnids == 0)
844 if (lpni->lpni_pref_nnids == 1)
845 return lpni->lpni_pref.nid == nid;
846 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
847 if (lpni->lpni_pref.nids[i] == nid)
854 * Set a single ni as preferred, provided no preferred ni is already
855 * defined. Only to be used for non-multi-rail peer_ni.
858 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
862 spin_lock(&lpni->lpni_lock);
863 if (nid == LNET_NID_ANY) {
865 } else if (lpni->lpni_pref_nnids > 0) {
867 } else if (lpni->lpni_pref_nnids == 0) {
868 lpni->lpni_pref.nid = nid;
869 lpni->lpni_pref_nnids = 1;
870 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
872 spin_unlock(&lpni->lpni_lock);
874 CDEBUG(D_NET, "peer %s nid %s: %d\n",
875 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
880 * Clear the preferred NID from a non-multi-rail peer_ni, provided
881 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
884 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
888 spin_lock(&lpni->lpni_lock);
889 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
890 lpni->lpni_pref_nnids = 0;
891 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
892 } else if (lpni->lpni_pref_nnids == 0) {
897 spin_unlock(&lpni->lpni_lock);
899 CDEBUG(D_NET, "peer %s: %d\n",
900 libcfs_nid2str(lpni->lpni_nid), rc);
905 * Clear the preferred NIDs from a non-multi-rail peer.
908 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
910 struct lnet_peer_ni *lpni = NULL;
912 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
913 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
917 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
919 lnet_nid_t *nids = NULL;
920 lnet_nid_t *oldnids = NULL;
921 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
926 if (nid == LNET_NID_ANY) {
931 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
936 /* A non-MR node may have only one preferred NI per peer_ni */
937 if (lpni->lpni_pref_nnids > 0) {
938 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
944 if (lpni->lpni_pref_nnids != 0) {
945 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
946 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
951 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
952 if (lpni->lpni_pref.nids[i] == nid) {
953 LIBCFS_FREE(nids, size);
957 nids[i] = lpni->lpni_pref.nids[i];
962 lnet_net_lock(LNET_LOCK_EX);
963 spin_lock(&lpni->lpni_lock);
964 if (lpni->lpni_pref_nnids == 0) {
965 lpni->lpni_pref.nid = nid;
967 oldnids = lpni->lpni_pref.nids;
968 lpni->lpni_pref.nids = nids;
970 lpni->lpni_pref_nnids++;
971 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
972 spin_unlock(&lpni->lpni_lock);
973 lnet_net_unlock(LNET_LOCK_EX);
976 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
977 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
980 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
981 spin_lock(&lpni->lpni_lock);
982 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
983 spin_unlock(&lpni->lpni_lock);
985 CDEBUG(D_NET, "peer %s nid %s: %d\n",
986 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
991 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
993 lnet_nid_t *nids = NULL;
994 lnet_nid_t *oldnids = NULL;
995 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1000 if (lpni->lpni_pref_nnids == 0) {
1005 if (lpni->lpni_pref_nnids == 1) {
1006 if (lpni->lpni_pref.nid != nid) {
1010 } else if (lpni->lpni_pref_nnids == 2) {
1011 if (lpni->lpni_pref.nids[0] != nid &&
1012 lpni->lpni_pref.nids[1] != nid) {
1017 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1018 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1023 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1024 if (lpni->lpni_pref.nids[i] != nid)
1026 nids[j++] = lpni->lpni_pref.nids[i];
1028 /* Check if we actually removed a nid. */
1029 if (j == lpni->lpni_pref_nnids) {
1030 LIBCFS_FREE(nids, size);
1036 lnet_net_lock(LNET_LOCK_EX);
1037 spin_lock(&lpni->lpni_lock);
1038 if (lpni->lpni_pref_nnids == 1) {
1039 lpni->lpni_pref.nid = LNET_NID_ANY;
1040 } else if (lpni->lpni_pref_nnids == 2) {
1041 oldnids = lpni->lpni_pref.nids;
1042 if (oldnids[0] == nid)
1043 lpni->lpni_pref.nid = oldnids[1];
1045 lpni->lpni_pref.nid = oldnids[2];
1047 oldnids = lpni->lpni_pref.nids;
1048 lpni->lpni_pref.nids = nids;
1050 lpni->lpni_pref_nnids--;
1051 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1052 spin_unlock(&lpni->lpni_lock);
1053 lnet_net_unlock(LNET_LOCK_EX);
1056 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1057 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1060 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1061 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1066 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1068 struct lnet_peer_ni *lpni;
1069 lnet_nid_t primary_nid = nid;
1071 lpni = lnet_find_peer_ni_locked(nid);
1073 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1074 lnet_peer_ni_decref_locked(lpni);
1081 LNetPrimaryNID(lnet_nid_t nid)
1083 struct lnet_peer *lp;
1084 struct lnet_peer_ni *lpni;
1085 lnet_nid_t primary_nid = nid;
1089 cpt = lnet_net_lock_current();
1090 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1095 lp = lpni->lpni_peer_net->lpn_peer;
1096 while (!lnet_peer_is_uptodate(lp)) {
1097 rc = lnet_discover_peer_locked(lpni, cpt, true);
1100 lp = lpni->lpni_peer_net->lpn_peer;
1102 primary_nid = lp->lp_primary_nid;
1104 lnet_peer_ni_decref_locked(lpni);
1106 lnet_net_unlock(cpt);
1108 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1109 libcfs_nid2str(primary_nid), rc);
1112 EXPORT_SYMBOL(LNetPrimaryNID);
1114 struct lnet_peer_net *
1115 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1117 struct lnet_peer_net *peer_net;
1118 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1119 if (peer_net->lpn_net_id == net_id)
1126 * Attach a peer_ni to a peer_net and peer. This function assumes
1127 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1128 * may be attached to a different peer, in which case it will be
1129 * properly detached first. The whole operation is done atomically.
1131 * Always returns 0. This is the last function called from functions
1132 * that do return an int, so returning 0 here allows the compiler to
1136 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1137 struct lnet_peer_net *lpn,
1138 struct lnet_peer_ni *lpni,
1141 struct lnet_peer_table *ptable;
1143 /* Install the new peer_ni */
1144 lnet_net_lock(LNET_LOCK_EX);
1145 /* Add peer_ni to global peer table hash, if necessary. */
1146 if (list_empty(&lpni->lpni_hashlist)) {
1147 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1149 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1150 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1151 ptable->pt_version++;
1152 ptable->pt_number++;
1153 /* This is the 1st refcount on lpni. */
1154 atomic_inc(&lpni->lpni_refcount);
1157 /* Detach the peer_ni from an existing peer, if necessary. */
1158 if (lpni->lpni_peer_net) {
1159 LASSERT(lpni->lpni_peer_net != lpn);
1160 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1161 lnet_peer_detach_peer_ni_locked(lpni);
1162 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1163 lpni->lpni_peer_net = NULL;
1166 /* Add peer_ni to peer_net */
1167 lpni->lpni_peer_net = lpn;
1168 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1169 lnet_peer_net_addref_locked(lpn);
1171 /* Add peer_net to peer */
1172 if (!lpn->lpn_peer) {
1174 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1175 lnet_peer_addref_locked(lp);
1178 /* Add peer to global peer list, if necessary */
1179 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1180 if (list_empty(&lp->lp_peer_list)) {
1181 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1186 /* Update peer state */
1187 spin_lock(&lp->lp_lock);
1188 if (flags & LNET_PEER_CONFIGURED) {
1189 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1190 lp->lp_state |= LNET_PEER_CONFIGURED;
1192 if (flags & LNET_PEER_MULTI_RAIL) {
1193 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1194 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1195 lnet_peer_clr_non_mr_pref_nids(lp);
1198 spin_unlock(&lp->lp_lock);
1201 lnet_net_unlock(LNET_LOCK_EX);
1203 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1204 libcfs_nid2str(lp->lp_primary_nid),
1205 libcfs_nid2str(lpni->lpni_nid), flags);
1211 * Create a new peer, with nid as its primary nid.
1213 * Call with the lnet_api_mutex held.
1216 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1218 struct lnet_peer *lp;
1219 struct lnet_peer_net *lpn;
1220 struct lnet_peer_ni *lpni;
1223 LASSERT(nid != LNET_NID_ANY);
1226 * No need for the lnet_net_lock here, because the
1227 * lnet_api_mutex is held.
1229 lpni = lnet_find_peer_ni_locked(nid);
1231 /* A peer with this NID already exists. */
1232 lp = lpni->lpni_peer_net->lpn_peer;
1233 lnet_peer_ni_decref_locked(lpni);
1235 * This is an error if the peer was configured and the
1236 * primary NID differs or an attempt is made to change
1237 * the Multi-Rail flag. Otherwise the assumption is
1238 * that an existing peer is being modified.
1240 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1241 if (lp->lp_primary_nid != nid)
1243 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1247 /* Delete and recreate as a configured peer. */
1251 /* Create peer, peer_net, and peer_ni. */
1253 lp = lnet_peer_alloc(nid);
1256 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1259 lpni = lnet_peer_ni_alloc(nid);
1263 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1266 LIBCFS_FREE(lpn, sizeof(*lpn));
1268 LIBCFS_FREE(lp, sizeof(*lp));
1270 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1271 libcfs_nid2str(nid), flags, rc);
1276 * Add a NID to a peer. Call with ln_api_mutex held.
1279 * -EPERM: Non-DLC addition to a DLC-configured peer.
1280 * -EEXIST: The NID was configured by DLC for a different peer.
1281 * -ENOMEM: Out of memory.
1282 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1283 * non-multi-rail peer.
1286 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1288 struct lnet_peer_net *lpn;
1289 struct lnet_peer_ni *lpni;
1293 LASSERT(nid != LNET_NID_ANY);
1295 /* A configured peer can only be updated through configuration. */
1296 if (!(flags & LNET_PEER_CONFIGURED)) {
1297 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1304 * The MULTI_RAIL flag can be set but not cleared, because
1305 * that would leave the peer struct in an invalid state.
1307 if (flags & LNET_PEER_MULTI_RAIL) {
1308 spin_lock(&lp->lp_lock);
1309 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1310 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1311 lnet_peer_clr_non_mr_pref_nids(lp);
1313 spin_unlock(&lp->lp_lock);
1314 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1319 lpni = lnet_find_peer_ni_locked(nid);
1322 * A peer_ni already exists. This is only a problem if
1323 * it is not connected to this peer and was configured
1326 lnet_peer_ni_decref_locked(lpni);
1327 if (lpni->lpni_peer_net->lpn_peer == lp)
1329 if (lnet_peer_ni_is_configured(lpni)) {
1333 /* If this is the primary NID, destroy the peer. */
1334 if (lnet_peer_ni_is_primary(lpni)) {
1335 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1336 lpni = lnet_peer_ni_alloc(nid);
1343 lpni = lnet_peer_ni_alloc(nid);
1351 * Get the peer_net. Check that we're not adding a second
1352 * peer_ni on a peer_net of a non-multi-rail peer.
1354 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1356 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1361 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1366 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1369 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1370 if (!lpni->lpni_peer_net)
1371 LIBCFS_FREE(lpni, sizeof(*lpni));
1373 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1374 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1380 * Update the primary NID of a peer, if possible.
1382 * Call with the lnet_api_mutex held.
1385 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1387 lnet_nid_t old = lp->lp_primary_nid;
1390 if (lp->lp_primary_nid == nid)
1392 rc = lnet_peer_add_nid(lp, nid, flags);
1395 lp->lp_primary_nid = nid;
1397 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1398 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1403 * lpni creation initiated due to traffic either sending or receiving.
1406 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1408 struct lnet_peer *lp;
1409 struct lnet_peer_net *lpn;
1410 struct lnet_peer_ni *lpni;
1414 if (nid == LNET_NID_ANY) {
1419 /* lnet_net_lock is not needed here because ln_api_lock is held */
1420 lpni = lnet_find_peer_ni_locked(nid);
1423 * We must have raced with another thread. Since we
1424 * know next to nothing about a peer_ni created by
1425 * traffic, we just assume everything is ok and
1428 lnet_peer_ni_decref_locked(lpni);
1432 /* Create peer, peer_net, and peer_ni. */
1434 lp = lnet_peer_alloc(nid);
1437 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1440 lpni = lnet_peer_ni_alloc(nid);
1443 if (pref != LNET_NID_ANY)
1444 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1446 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1449 LIBCFS_FREE(lpn, sizeof(*lpn));
1451 LIBCFS_FREE(lp, sizeof(*lp));
1453 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1458 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1460 * This API handles the following combinations:
1461 * Create a peer with its primary NI if only the prim_nid is provided
1462 * Add a NID to a peer identified by the prim_nid. The peer identified
1463 * by the prim_nid must already exist.
1464 * The peer being created may be non-MR.
1466 * The caller must hold ln_api_mutex. This prevents the peer from
1467 * being created/modified/deleted by a different thread.
1470 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1472 struct lnet_peer *lp = NULL;
1473 struct lnet_peer_ni *lpni;
1476 /* The prim_nid must always be specified */
1477 if (prim_nid == LNET_NID_ANY)
1480 flags = LNET_PEER_CONFIGURED;
1482 flags |= LNET_PEER_MULTI_RAIL;
1485 * If nid isn't specified, we must create a new peer with
1486 * prim_nid as its primary nid.
1488 if (nid == LNET_NID_ANY)
1489 return lnet_peer_add(prim_nid, flags);
1491 /* Look up the prim_nid, which must exist. */
1492 lpni = lnet_find_peer_ni_locked(prim_nid);
1495 lnet_peer_ni_decref_locked(lpni);
1496 lp = lpni->lpni_peer_net->lpn_peer;
1498 /* Peer must have been configured. */
1499 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1500 CDEBUG(D_NET, "peer %s was not configured\n",
1501 libcfs_nid2str(prim_nid));
1505 /* Primary NID must match */
1506 if (lp->lp_primary_nid != prim_nid) {
1507 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1508 libcfs_nid2str(prim_nid),
1509 libcfs_nid2str(lp->lp_primary_nid));
1513 /* Multi-Rail flag must match. */
1514 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1515 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1516 libcfs_nid2str(prim_nid));
1520 return lnet_peer_add_nid(lp, nid, flags);
1524 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1526 * This API handles the following combinations:
1527 * Delete a NI from a peer if both prim_nid and nid are provided.
1528 * Delete a peer if only prim_nid is provided.
1529 * Delete a peer if its primary nid is provided.
1531 * The caller must hold ln_api_mutex. This prevents the peer from
1532 * being modified/deleted by a different thread.
1535 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1537 struct lnet_peer *lp;
1538 struct lnet_peer_ni *lpni;
1541 if (prim_nid == LNET_NID_ANY)
1544 lpni = lnet_find_peer_ni_locked(prim_nid);
1547 lnet_peer_ni_decref_locked(lpni);
1548 lp = lpni->lpni_peer_net->lpn_peer;
1550 if (prim_nid != lp->lp_primary_nid) {
1551 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1552 libcfs_nid2str(prim_nid),
1553 libcfs_nid2str(lp->lp_primary_nid));
1557 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1558 return lnet_peer_del(lp);
1560 flags = LNET_PEER_CONFIGURED;
1561 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1562 flags |= LNET_PEER_MULTI_RAIL;
1564 return lnet_peer_del_nid(lp, nid, flags);
1568 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1570 struct lnet_peer_table *ptable;
1571 struct lnet_peer_net *lpn;
1573 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1575 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1576 LASSERT(lpni->lpni_rtr_refcount == 0);
1577 LASSERT(list_empty(&lpni->lpni_txq));
1578 LASSERT(lpni->lpni_txqnob == 0);
1579 LASSERT(list_empty(&lpni->lpni_peer_nis));
1580 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1582 lpn = lpni->lpni_peer_net;
1583 lpni->lpni_peer_net = NULL;
1584 lpni->lpni_net = NULL;
1586 /* remove the peer ni from the zombie list */
1587 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1588 spin_lock(&ptable->pt_zombie_lock);
1589 list_del_init(&lpni->lpni_hashlist);
1590 ptable->pt_zombies--;
1591 spin_unlock(&ptable->pt_zombie_lock);
1593 if (lpni->lpni_pref_nnids > 1) {
1594 LIBCFS_FREE(lpni->lpni_pref.nids,
1595 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1597 LIBCFS_FREE(lpni, sizeof(*lpni));
1599 lnet_peer_net_decref_locked(lpn);
1602 struct lnet_peer_ni *
1603 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1605 struct lnet_peer_ni *lpni = NULL;
1608 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1609 return ERR_PTR(-ESHUTDOWN);
1612 * find if a peer_ni already exists.
1613 * If so then just return that.
1615 lpni = lnet_find_peer_ni_locked(nid);
1619 lnet_net_unlock(cpt);
1621 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1624 goto out_net_relock;
1627 lpni = lnet_find_peer_ni_locked(nid);
1637 * Get a peer_ni for the given nid, create it if necessary. Takes a
1638 * hold on the peer_ni.
1640 struct lnet_peer_ni *
1641 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1643 struct lnet_peer_ni *lpni = NULL;
1646 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1647 return ERR_PTR(-ESHUTDOWN);
1650 * find if a peer_ni already exists.
1651 * If so then just return that.
1653 lpni = lnet_find_peer_ni_locked(nid);
1659 * use the lnet_api_mutex to serialize the creation of the peer_ni
1660 * and the creation/deletion of the local ni/net. When a local ni is
1661 * created, if there exists a set of peer_nis on that network,
1662 * they need to be traversed and updated. When a local NI is
1663 * deleted, which could result in a network being deleted, then
1664 * all peer nis on that network need to be removed as well.
1666 * Creation through traffic should also be serialized with
1667 * creation through DLC.
1669 lnet_net_unlock(cpt);
1670 mutex_lock(&the_lnet.ln_api_mutex);
1672 * Shutdown is only set under the ln_api_lock, so a single
1673 * check here is sufficent.
1675 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1676 lpni = ERR_PTR(-ESHUTDOWN);
1677 goto out_mutex_unlock;
1680 rc = lnet_peer_ni_traffic_add(nid, pref);
1683 goto out_mutex_unlock;
1686 lpni = lnet_find_peer_ni_locked(nid);
1690 mutex_unlock(&the_lnet.ln_api_mutex);
1693 /* Lock has been dropped, check again for shutdown. */
1694 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1696 lnet_peer_ni_decref_locked(lpni);
1697 lpni = ERR_PTR(-ESHUTDOWN);
1708 * Is a peer uptodate from the point of view of discovery?
1710 * If it is currently being processed, obviously not.
1711 * A forced Ping or Push is also handled by the discovery thread.
1713 * Otherwise look at whether the peer needs rediscovering.
1716 lnet_peer_is_uptodate(struct lnet_peer *lp)
1720 spin_lock(&lp->lp_lock);
1721 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1722 LNET_PEER_FORCE_PING |
1723 LNET_PEER_FORCE_PUSH)) {
1725 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1727 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1728 if (lnet_peer_discovery_disabled)
1732 } else if (lnet_peer_needs_push(lp)) {
1734 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1735 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1742 spin_unlock(&lp->lp_lock);
1748 * Queue a peer for the attention of the discovery thread. Call with
1749 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1750 * -EALREADY if the peer was already queued.
1752 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1756 spin_lock(&lp->lp_lock);
1757 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1758 lp->lp_state |= LNET_PEER_DISCOVERING;
1759 spin_unlock(&lp->lp_lock);
1760 if (list_empty(&lp->lp_dc_list)) {
1761 lnet_peer_addref_locked(lp);
1762 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1763 wake_up(&the_lnet.ln_dc_waitq);
1769 CDEBUG(D_NET, "Queue peer %s: %d\n",
1770 libcfs_nid2str(lp->lp_primary_nid), rc);
1776 * Discovery of a peer is complete. Wake all waiters on the peer.
1777 * Call with lnet_net_lock/EX held.
1779 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1781 struct lnet_msg *msg, *tmp;
1783 struct list_head pending_msgs;
1785 INIT_LIST_HEAD(&pending_msgs);
1787 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1788 libcfs_nid2str(lp->lp_primary_nid));
1790 list_del_init(&lp->lp_dc_list);
1791 spin_lock(&lp->lp_lock);
1792 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1793 spin_unlock(&lp->lp_lock);
1794 wake_up_all(&lp->lp_dc_waitq);
1796 lnet_net_unlock(LNET_LOCK_EX);
1798 /* iterate through all pending messages and send them again */
1799 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1800 list_del_init(&msg->msg_list);
1801 if (lp->lp_dc_error) {
1802 lnet_finalize(msg, lp->lp_dc_error);
1806 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1807 lnet_msgtyp2str(msg->msg_type),
1808 libcfs_id2str(msg->msg_target));
1809 rc = lnet_send(msg->msg_src_nid_param, msg,
1810 msg->msg_rtr_nid_param);
1812 CNETERR("Error sending %s to %s: %d\n",
1813 lnet_msgtyp2str(msg->msg_type),
1814 libcfs_id2str(msg->msg_target), rc);
1815 lnet_finalize(msg, rc);
1818 lnet_net_lock(LNET_LOCK_EX);
1819 lnet_peer_decref_locked(lp);
1823 * Handle inbound push.
1824 * Like any event handler, called with lnet_res_lock/CPT held.
1826 void lnet_peer_push_event(struct lnet_event *ev)
1828 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1829 struct lnet_peer *lp;
1831 /* lnet_find_peer() adds a refcount */
1832 lp = lnet_find_peer(ev->source.nid);
1834 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1835 libcfs_nid2str(ev->initiator.nid),
1836 libcfs_nid2str(ev->source.nid));
1840 /* Ensure peer state remains consistent while we modify it. */
1841 spin_lock(&lp->lp_lock);
1844 * If some kind of error happened the contents of the message
1845 * cannot be used. Clear the NIDS_UPTODATE and set the
1846 * FORCE_PING flag to trigger a ping.
1849 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1850 lp->lp_state |= LNET_PEER_FORCE_PING;
1851 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1853 libcfs_nid2str(lp->lp_primary_nid),
1854 libcfs_nid2str(ev->source.nid));
1859 * A push with invalid or corrupted info. Clear the UPTODATE
1860 * flag to trigger a ping.
1862 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1863 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1864 lp->lp_state |= LNET_PEER_FORCE_PING;
1865 CDEBUG(D_NET, "Corrupted Push from %s\n",
1866 libcfs_nid2str(lp->lp_primary_nid));
1871 * Make sure we'll allocate the correct size ping buffer when
1874 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1875 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1878 * A non-Multi-Rail peer is not supposed to be capable of
1881 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1882 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1883 libcfs_nid2str(lp->lp_primary_nid));
1888 * Check the MULTIRAIL flag. Complain if the peer was DLC
1889 * configured without it.
1891 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1892 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1893 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1894 libcfs_nid2str(lp->lp_primary_nid));
1896 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1897 lnet_peer_clr_non_mr_pref_nids(lp);
1902 * The peer may have discovery disabled at its end. Set
1903 * NO_DISCOVERY as appropriate.
1905 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1906 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1907 libcfs_nid2str(lp->lp_primary_nid));
1908 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1909 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1910 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1911 libcfs_nid2str(lp->lp_primary_nid));
1912 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1916 * Check for truncation of the Put message. Clear the
1917 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1918 * and tell discovery to allocate a bigger buffer.
1920 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1921 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1922 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1923 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1924 lp->lp_state |= LNET_PEER_FORCE_PING;
1925 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1926 libcfs_nid2str(lp->lp_primary_nid),
1927 pbuf->pb_info.pi_nnis);
1932 * Check whether the Put data is stale. Stale data can just be
1935 if (pbuf->pb_info.pi_nnis > 1 &&
1936 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1937 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1938 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1939 libcfs_nid2str(lp->lp_primary_nid),
1940 LNET_PING_BUFFER_SEQNO(pbuf),
1946 * Check whether the Put data is new, in which case we clear
1947 * the UPTODATE flag and prepare to process it.
1949 * If the Put data is current, and the peer is UPTODATE then
1950 * we assome everything is all right and drop the data as
1953 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1954 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1955 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1956 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1957 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1958 libcfs_nid2str(lp->lp_primary_nid),
1959 LNET_PING_BUFFER_SEQNO(pbuf),
1965 * If there is data present that hasn't been processed yet,
1966 * we'll replace it if the Put contained newer data and it
1967 * fits. We're racing with a Ping or earlier Push in this
1970 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1971 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1972 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1973 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1974 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1975 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1976 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1977 libcfs_nid2str(lp->lp_primary_nid),
1978 LNET_PING_BUFFER_SEQNO(pbuf),
1979 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1985 * Allocate a buffer to copy the data. On a failure we drop
1986 * the Push and set FORCE_PING to force the discovery
1987 * thread to fix the problem by pinging the peer.
1989 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
1991 lp->lp_state |= LNET_PEER_FORCE_PING;
1992 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
1993 libcfs_nid2str(lp->lp_primary_nid),
1994 LNET_PING_BUFFER_SEQNO(pbuf));
1999 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2000 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2001 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2002 CDEBUG(D_NET, "Received Push %s %u\n",
2003 libcfs_nid2str(lp->lp_primary_nid),
2004 LNET_PING_BUFFER_SEQNO(pbuf));
2008 * Queue the peer for discovery if not done, force it on the request
2009 * queue and wake the discovery thread if the peer was already queued,
2010 * because its status changed.
2012 spin_unlock(&lp->lp_lock);
2013 lnet_net_lock(LNET_LOCK_EX);
2014 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2015 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2016 wake_up(&the_lnet.ln_dc_waitq);
2018 /* Drop refcount from lookup */
2019 lnet_peer_decref_locked(lp);
2020 lnet_net_unlock(LNET_LOCK_EX);
2024 * Clear the discovery error state, unless we're already discovering
2025 * this peer, in which case the error is current.
2027 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2029 spin_lock(&lp->lp_lock);
2030 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2031 lp->lp_dc_error = 0;
2032 spin_unlock(&lp->lp_lock);
2036 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2037 * dropped/retaken within this function. An lnet_peer_ni is passed in
2038 * because discovery could tear down an lnet_peer.
2041 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2044 struct lnet_peer *lp;
2048 lnet_net_unlock(cpt);
2049 lnet_net_lock(LNET_LOCK_EX);
2050 lp = lpni->lpni_peer_net->lpn_peer;
2051 lnet_peer_clear_discovery_error(lp);
2054 * We're willing to be interrupted. The lpni can become a
2055 * zombie if we race with DLC, so we must check for that.
2058 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2059 if (signal_pending(current))
2061 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2063 if (lp->lp_dc_error)
2065 if (lnet_peer_is_uptodate(lp))
2067 lnet_peer_queue_for_discovery(lp);
2069 * if caller requested a non-blocking operation then
2070 * return immediately. Once discovery is complete then the
2071 * peer ref will be decremented and any pending messages
2072 * that were stopped due to discovery will be transmitted.
2077 lnet_peer_addref_locked(lp);
2078 lnet_net_unlock(LNET_LOCK_EX);
2080 finish_wait(&lp->lp_dc_waitq, &wait);
2081 lnet_net_lock(LNET_LOCK_EX);
2082 lnet_peer_decref_locked(lp);
2083 /* Peer may have changed */
2084 lp = lpni->lpni_peer_net->lpn_peer;
2086 finish_wait(&lp->lp_dc_waitq, &wait);
2088 lnet_net_unlock(LNET_LOCK_EX);
2092 * If the peer has changed after we've discovered the older peer,
2093 * then we need to discovery the new peer to make sure the
2094 * interface information is up to date
2096 if (lp != lpni->lpni_peer_net->lpn_peer)
2099 if (signal_pending(current))
2101 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2103 else if (lp->lp_dc_error)
2104 rc = lp->lp_dc_error;
2106 CDEBUG(D_NET, "non-blocking discovery\n");
2107 else if (!lnet_peer_is_uptodate(lp))
2110 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2111 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2112 libcfs_nid2str(lpni->lpni_nid), rc,
2113 (!block) ? "pending discovery" : "discovery complete");
2118 /* Handle an incoming ack for a push. */
2120 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2122 struct lnet_ping_buffer *pbuf;
2124 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2125 spin_lock(&lp->lp_lock);
2126 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2127 lp->lp_push_error = ev->status;
2129 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2131 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2132 spin_unlock(&lp->lp_lock);
2134 CDEBUG(D_NET, "peer %s ev->status %d\n",
2135 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2138 /* Handle a Reply message. This is the reply to a Ping message. */
2140 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2142 struct lnet_ping_buffer *pbuf;
2145 spin_lock(&lp->lp_lock);
2148 * If some kind of error happened the contents of message
2149 * cannot be used. Set PING_FAILED to trigger a retry.
2152 lp->lp_state |= LNET_PEER_PING_FAILED;
2153 lp->lp_ping_error = ev->status;
2154 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2156 libcfs_nid2str(lp->lp_primary_nid),
2157 libcfs_nid2str(ev->source.nid));
2161 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2162 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2163 lnet_swap_pinginfo(pbuf);
2166 * A reply with invalid or corrupted info. Set PING_FAILED to
2169 rc = lnet_ping_info_validate(&pbuf->pb_info);
2171 lp->lp_state |= LNET_PEER_PING_FAILED;
2172 lp->lp_ping_error = 0;
2173 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2174 libcfs_nid2str(lp->lp_primary_nid), rc);
2179 * Update the MULTI_RAIL flag based on the reply. If the peer
2180 * was configured with DLC then the setting should match what
2183 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2184 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2185 /* Everything's fine */
2186 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2187 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2188 libcfs_nid2str(lp->lp_primary_nid));
2190 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2191 lnet_peer_clr_non_mr_pref_nids(lp);
2193 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2194 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2195 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2196 libcfs_nid2str(lp->lp_primary_nid));
2198 CERROR("Multi-Rail state vanished from %s\n",
2199 libcfs_nid2str(lp->lp_primary_nid));
2200 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2205 * Make sure we'll allocate the correct size ping buffer when
2208 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2209 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2212 * The peer may have discovery disabled at its end. Set
2213 * NO_DISCOVERY as appropriate.
2215 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2216 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2217 libcfs_nid2str(lp->lp_primary_nid));
2218 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2219 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2220 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2221 libcfs_nid2str(lp->lp_primary_nid));
2222 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2226 * Check for truncation of the Reply. Clear PING_SENT and set
2227 * PING_FAILED to trigger a retry.
2229 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2230 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2231 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2232 lp->lp_state |= LNET_PEER_PING_FAILED;
2233 lp->lp_ping_error = 0;
2234 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2235 libcfs_nid2str(lp->lp_primary_nid),
2236 pbuf->pb_info.pi_nnis);
2241 * Check the sequence numbers in the reply. These are only
2242 * available if the reply came from a Multi-Rail peer.
2244 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2245 pbuf->pb_info.pi_nnis > 1 &&
2246 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2247 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2248 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2249 libcfs_nid2str(lp->lp_primary_nid),
2250 LNET_PING_BUFFER_SEQNO(pbuf),
2255 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2256 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2259 /* We're happy with the state of the data in the buffer. */
2260 CDEBUG(D_NET, "peer %s data present %u\n",
2261 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2262 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2263 lnet_ping_buffer_decref(lp->lp_data);
2265 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2266 lnet_ping_buffer_addref(pbuf);
2269 lp->lp_state &= ~LNET_PEER_PING_SENT;
2270 spin_unlock(&lp->lp_lock);
2274 * Send event handling. Only matters for error cases, where we clean
2275 * up state on the peer and peer_ni that would otherwise be updated in
2276 * the REPLY event handler for a successful Ping, and the ACK event
2277 * handler for a successful Push.
2280 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2287 spin_lock(&lp->lp_lock);
2288 if (ev->msg_type == LNET_MSG_GET) {
2289 lp->lp_state &= ~LNET_PEER_PING_SENT;
2290 lp->lp_state |= LNET_PEER_PING_FAILED;
2291 lp->lp_ping_error = ev->status;
2292 } else { /* ev->msg_type == LNET_MSG_PUT */
2293 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2294 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2295 lp->lp_push_error = ev->status;
2297 spin_unlock(&lp->lp_lock);
2298 rc = LNET_REDISCOVER_PEER;
2300 CDEBUG(D_NET, "%s Send to %s: %d\n",
2301 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2302 libcfs_nid2str(ev->target.nid), rc);
2307 * Unlink event handling. This event is only seen if a call to
2308 * LNetMDUnlink() caused the event to be unlinked. If this call was
2309 * made after the event was set up in LNetGet() or LNetPut() then we
2310 * assume the Ping or Push timed out.
2313 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2315 spin_lock(&lp->lp_lock);
2316 /* We've passed through LNetGet() */
2317 if (lp->lp_state & LNET_PEER_PING_SENT) {
2318 lp->lp_state &= ~LNET_PEER_PING_SENT;
2319 lp->lp_state |= LNET_PEER_PING_FAILED;
2320 lp->lp_ping_error = -ETIMEDOUT;
2321 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2322 libcfs_nid2str(lp->lp_primary_nid));
2324 /* We've passed through LNetPut() */
2325 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2326 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2327 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2328 lp->lp_push_error = -ETIMEDOUT;
2329 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2330 libcfs_nid2str(lp->lp_primary_nid));
2332 spin_unlock(&lp->lp_lock);
2336 * Event handler for the discovery EQ.
2338 * Called with lnet_res_lock(cpt) held. The cpt is the
2339 * lnet_cpt_of_cookie() of the md handle cookie.
2341 static void lnet_discovery_event_handler(struct lnet_event *event)
2343 struct lnet_peer *lp = event->md.user_ptr;
2344 struct lnet_ping_buffer *pbuf;
2347 /* discovery needs to take another look */
2348 rc = LNET_REDISCOVER_PEER;
2350 CDEBUG(D_NET, "Received event: %d\n", event->type);
2352 switch (event->type) {
2353 case LNET_EVENT_ACK:
2354 lnet_discovery_event_ack(lp, event);
2356 case LNET_EVENT_REPLY:
2357 lnet_discovery_event_reply(lp, event);
2359 case LNET_EVENT_SEND:
2360 /* Only send failure triggers a retry. */
2361 rc = lnet_discovery_event_send(lp, event);
2363 case LNET_EVENT_UNLINK:
2364 /* LNetMDUnlink() was called */
2365 lnet_discovery_event_unlink(lp, event);
2368 /* Invalid events. */
2371 lnet_net_lock(LNET_LOCK_EX);
2372 if (event->unlinked) {
2373 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2374 lnet_ping_buffer_decref(pbuf);
2375 lnet_peer_decref_locked(lp);
2378 /* put peer back at end of request queue, if discovery not already
2380 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2381 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2382 wake_up(&the_lnet.ln_dc_waitq);
2384 lnet_net_unlock(LNET_LOCK_EX);
2388 * Build a peer from incoming data.
2390 * The NIDs in the incoming data are supposed to be structured as follows:
2393 * - other NIDs in same net
2394 * - NIDs in second net
2395 * - NIDs in third net
2397 * This due to the way the list of NIDs in the data is created.
2399 * Note that this function will mark the peer uptodate unless an
2400 * ENOMEM is encontered. All other errors are due to a conflict
2401 * between the DLC configuration and what discovery sees. We treat DLC
2402 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2403 * peer from becoming stuck in discovery.
2405 static int lnet_peer_merge_data(struct lnet_peer *lp,
2406 struct lnet_ping_buffer *pbuf)
2408 struct lnet_peer_ni *lpni;
2409 lnet_nid_t *curnis = NULL;
2410 lnet_nid_t *addnis = NULL;
2411 lnet_nid_t *delnis = NULL;
2421 flags = LNET_PEER_DISCOVERED;
2422 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2423 flags |= LNET_PEER_MULTI_RAIL;
2425 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2426 LIBCFS_ALLOC(curnis, nnis * sizeof(lnet_nid_t));
2427 LIBCFS_ALLOC(addnis, nnis * sizeof(lnet_nid_t));
2428 LIBCFS_ALLOC(delnis, nnis * sizeof(lnet_nid_t));
2429 if (!curnis || !addnis || !delnis) {
2437 /* Construct the list of NIDs present in peer. */
2439 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2440 curnis[ncurnis++] = lpni->lpni_nid;
2443 * Check for NIDs in pbuf not present in curnis[].
2444 * The loop starts at 1 to skip the loopback NID.
2446 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2447 for (j = 0; j < ncurnis; j++)
2448 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2451 addnis[naddnis++] = pbuf->pb_info.pi_ni[i].ns_nid;
2454 * Check for NIDs in curnis[] not present in pbuf.
2455 * The nested loop starts at 1 to skip the loopback NID.
2457 * But never add the loopback NID to delnis[]: if it is
2458 * present in curnis[] then this peer is for this node.
2460 for (i = 0; i < ncurnis; i++) {
2461 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2463 for (j = 1; j < pbuf->pb_info.pi_nnis; j++)
2464 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid)
2466 if (j == pbuf->pb_info.pi_nnis)
2467 delnis[ndelnis++] = curnis[i];
2470 for (i = 0; i < naddnis; i++) {
2471 rc = lnet_peer_add_nid(lp, addnis[i], flags);
2473 CERROR("Error adding NID %s to peer %s: %d\n",
2474 libcfs_nid2str(addnis[i]),
2475 libcfs_nid2str(lp->lp_primary_nid), rc);
2480 for (i = 0; i < ndelnis; i++) {
2481 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2483 CERROR("Error deleting NID %s from peer %s: %d\n",
2484 libcfs_nid2str(delnis[i]),
2485 libcfs_nid2str(lp->lp_primary_nid), rc);
2491 * Errors other than -ENOMEM are due to peers having been
2492 * configured with DLC. Ignore these because DLC overrides
2497 LIBCFS_FREE(curnis, nnis * sizeof(lnet_nid_t));
2498 LIBCFS_FREE(addnis, nnis * sizeof(lnet_nid_t));
2499 LIBCFS_FREE(delnis, nnis * sizeof(lnet_nid_t));
2500 lnet_ping_buffer_decref(pbuf);
2501 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2504 spin_lock(&lp->lp_lock);
2505 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2506 lp->lp_state |= LNET_PEER_FORCE_PING;
2507 spin_unlock(&lp->lp_lock);
2513 * The data in pbuf says lp is its primary peer, but the data was
2514 * received by a different peer. Try to update lp with the data.
2517 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2519 struct lnet_handle_md mdh;
2521 /* Queue lp for discovery, and force it on the request queue. */
2522 lnet_net_lock(LNET_LOCK_EX);
2523 if (lnet_peer_queue_for_discovery(lp))
2524 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2525 lnet_net_unlock(LNET_LOCK_EX);
2527 LNetInvalidateMDHandle(&mdh);
2530 * Decide whether we can move the peer to the DATA_PRESENT state.
2532 * We replace stale data for a multi-rail peer, repair PING_FAILED
2533 * status, and preempt FORCE_PING.
2535 * If after that we have DATA_PRESENT, we merge it into this peer.
2537 spin_lock(&lp->lp_lock);
2538 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2539 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2540 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2541 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2542 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2543 lnet_ping_buffer_decref(pbuf);
2548 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2549 lnet_ping_buffer_decref(lp->lp_data);
2551 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2553 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2554 mdh = lp->lp_ping_mdh;
2555 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2556 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2557 lp->lp_ping_error = 0;
2559 if (lp->lp_state & LNET_PEER_FORCE_PING)
2560 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2561 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2562 spin_unlock(&lp->lp_lock);
2564 if (!LNetMDHandleIsInvalid(mdh))
2568 return lnet_peer_merge_data(lp, pbuf);
2570 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2575 * Update a peer using the data received.
2577 static int lnet_peer_data_present(struct lnet_peer *lp)
2578 __must_hold(&lp->lp_lock)
2580 struct lnet_ping_buffer *pbuf;
2581 struct lnet_peer_ni *lpni;
2582 lnet_nid_t nid = LNET_NID_ANY;
2588 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2589 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2590 spin_unlock(&lp->lp_lock);
2593 * Modifications of peer structures are done while holding the
2594 * ln_api_mutex. A global lock is required because we may be
2595 * modifying multiple peer structures, and a mutex greatly
2596 * simplifies memory management.
2598 * The actual changes to the data structures must also protect
2599 * against concurrent lookups, for which the lnet_net_lock in
2600 * LNET_LOCK_EX mode is used.
2602 mutex_lock(&the_lnet.ln_api_mutex);
2603 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2609 * If this peer is not on the peer list then it is being torn
2610 * down, and our reference count may be all that is keeping it
2611 * alive. Don't do any work on it.
2613 if (list_empty(&lp->lp_peer_list))
2616 flags = LNET_PEER_DISCOVERED;
2617 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2618 flags |= LNET_PEER_MULTI_RAIL;
2621 * Check whether the primary NID in the message matches the
2622 * primary NID of the peer. If it does, update the peer, if
2623 * it it does not, check whether there is already a peer with
2624 * that primary NID. If no such peer exists, try to update
2625 * the primary NID of the current peer (allowed if it was
2626 * created due to message traffic) and complete the update.
2627 * If the peer did exist, hand off the data to it.
2629 * The peer for the loopback interface is a special case: this
2630 * is the peer for the local node, and we want to set its
2631 * primary NID to the correct value here. Moreover, this peer
2632 * can show up with only the loopback NID in the ping buffer.
2634 if (pbuf->pb_info.pi_nnis <= 1)
2636 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2637 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2638 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2640 rc = lnet_peer_merge_data(lp, pbuf);
2641 } else if (lp->lp_primary_nid == nid) {
2642 rc = lnet_peer_merge_data(lp, pbuf);
2644 lpni = lnet_find_peer_ni_locked(nid);
2646 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2648 CERROR("Primary NID error %s versus %s: %d\n",
2649 libcfs_nid2str(lp->lp_primary_nid),
2650 libcfs_nid2str(nid), rc);
2652 rc = lnet_peer_merge_data(lp, pbuf);
2655 rc = lnet_peer_set_primary_data(
2656 lpni->lpni_peer_net->lpn_peer, pbuf);
2657 lnet_peer_ni_decref_locked(lpni);
2661 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2662 mutex_unlock(&the_lnet.ln_api_mutex);
2664 spin_lock(&lp->lp_lock);
2665 /* Tell discovery to re-check the peer immediately. */
2667 rc = LNET_REDISCOVER_PEER;
2672 * A ping failed. Clear the PING_FAILED state and set the
2673 * FORCE_PING state, to ensure a retry even if discovery is
2674 * disabled. This avoids being left with incorrect state.
2676 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2677 __must_hold(&lp->lp_lock)
2679 struct lnet_handle_md mdh;
2682 mdh = lp->lp_ping_mdh;
2683 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2684 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2685 lp->lp_state |= LNET_PEER_FORCE_PING;
2686 rc = lp->lp_ping_error;
2687 lp->lp_ping_error = 0;
2688 spin_unlock(&lp->lp_lock);
2690 if (!LNetMDHandleIsInvalid(mdh))
2693 CDEBUG(D_NET, "peer %s:%d\n",
2694 libcfs_nid2str(lp->lp_primary_nid), rc);
2696 spin_lock(&lp->lp_lock);
2697 return rc ? rc : LNET_REDISCOVER_PEER;
2701 * Select NID to send a Ping or Push to.
2703 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2705 struct lnet_peer_ni *lpni;
2707 /* Look for a direct-connected NID for this peer. */
2709 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2710 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2715 return lpni->lpni_nid;
2717 /* Look for a routed-connected NID for this peer. */
2719 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2720 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2725 return lpni->lpni_nid;
2727 return LNET_NID_ANY;
2730 /* Active side of ping. */
2731 static int lnet_peer_send_ping(struct lnet_peer *lp)
2732 __must_hold(&lp->lp_lock)
2739 lp->lp_state |= LNET_PEER_PING_SENT;
2740 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2741 spin_unlock(&lp->lp_lock);
2743 cpt = lnet_net_lock_current();
2744 /* Refcount for MD. */
2745 lnet_peer_addref_locked(lp);
2746 pnid = lnet_peer_select_nid(lp);
2747 lnet_net_unlock(cpt);
2749 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2751 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2752 the_lnet.ln_dc_eqh, false);
2755 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2756 * refcount on the peer, otherwise LNetMDUnlink will be called
2757 * which will eventually do that.
2761 lnet_peer_decref_locked(lp);
2762 lnet_net_unlock(cpt);
2763 rc = -rc; /* change the rc to negative value */
2765 } else if (rc < 0) {
2769 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2771 spin_lock(&lp->lp_lock);
2775 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2777 * The errors that get us here are considered hard errors and
2778 * cause Discovery to terminate. So we clear PING_SENT, but do
2779 * not set either PING_FAILED or FORCE_PING. In fact we need
2780 * to clear PING_FAILED, because the unlink event handler will
2781 * have set it if we called LNetMDUnlink() above.
2783 spin_lock(&lp->lp_lock);
2784 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2789 * This function exists because you cannot call LNetMDUnlink() from an
2792 static int lnet_peer_push_failed(struct lnet_peer *lp)
2793 __must_hold(&lp->lp_lock)
2795 struct lnet_handle_md mdh;
2798 mdh = lp->lp_push_mdh;
2799 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2800 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2801 rc = lp->lp_push_error;
2802 lp->lp_push_error = 0;
2803 spin_unlock(&lp->lp_lock);
2805 if (!LNetMDHandleIsInvalid(mdh))
2808 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2809 spin_lock(&lp->lp_lock);
2810 return rc ? rc : LNET_REDISCOVER_PEER;
2813 /* Active side of push. */
2814 static int lnet_peer_send_push(struct lnet_peer *lp)
2815 __must_hold(&lp->lp_lock)
2817 struct lnet_ping_buffer *pbuf;
2818 struct lnet_process_id id;
2823 /* Don't push to a non-multi-rail peer. */
2824 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2825 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2829 lp->lp_state |= LNET_PEER_PUSH_SENT;
2830 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2831 spin_unlock(&lp->lp_lock);
2833 cpt = lnet_net_lock_current();
2834 pbuf = the_lnet.ln_ping_target;
2835 lnet_ping_buffer_addref(pbuf);
2836 lnet_net_unlock(cpt);
2838 /* Push source MD */
2839 md.start = &pbuf->pb_info;
2840 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2841 md.threshold = 2; /* Put/Ack */
2844 md.eq_handle = the_lnet.ln_dc_eqh;
2847 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2849 lnet_ping_buffer_decref(pbuf);
2850 CERROR("Can't bind push source MD: %d\n", rc);
2853 cpt = lnet_net_lock_current();
2854 /* Refcount for MD. */
2855 lnet_peer_addref_locked(lp);
2856 id.pid = LNET_PID_LUSTRE;
2857 id.nid = lnet_peer_select_nid(lp);
2858 lnet_net_unlock(cpt);
2860 if (id.nid == LNET_NID_ANY) {
2865 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2866 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2867 LNET_PROTO_PING_MATCHBITS, 0, 0);
2872 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2874 spin_lock(&lp->lp_lock);
2878 LNetMDUnlink(lp->lp_push_mdh);
2879 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2881 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2883 * The errors that get us here are considered hard errors and
2884 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2885 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2886 * because the unlink event handler will have set it if we
2887 * called LNetMDUnlink() above.
2889 spin_lock(&lp->lp_lock);
2890 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2895 * An unrecoverable error was encountered during discovery.
2896 * Set error status in peer and abort discovery.
2898 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2900 CDEBUG(D_NET, "Discovery error %s: %d\n",
2901 libcfs_nid2str(lp->lp_primary_nid), error);
2903 spin_lock(&lp->lp_lock);
2904 lp->lp_dc_error = error;
2905 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2906 lp->lp_state |= LNET_PEER_REDISCOVER;
2907 spin_unlock(&lp->lp_lock);
2911 * Mark the peer as discovered.
2913 static int lnet_peer_discovered(struct lnet_peer *lp)
2914 __must_hold(&lp->lp_lock)
2916 lp->lp_state |= LNET_PEER_DISCOVERED;
2917 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2918 LNET_PEER_REDISCOVER);
2920 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2926 * Mark the peer as to be rediscovered.
2928 static int lnet_peer_rediscover(struct lnet_peer *lp)
2929 __must_hold(&lp->lp_lock)
2931 lp->lp_state |= LNET_PEER_REDISCOVER;
2932 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2934 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2940 * Discovering this peer is taking too long. Cancel any Ping or Push
2941 * that discovery is waiting on by unlinking the relevant MDs. The
2942 * lnet_discovery_event_handler() will proceed from here and complete
2945 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
2947 struct lnet_handle_md ping_mdh;
2948 struct lnet_handle_md push_mdh;
2950 LNetInvalidateMDHandle(&ping_mdh);
2951 LNetInvalidateMDHandle(&push_mdh);
2953 spin_lock(&lp->lp_lock);
2954 if (lp->lp_state & LNET_PEER_PING_SENT) {
2955 ping_mdh = lp->lp_ping_mdh;
2956 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2958 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2959 push_mdh = lp->lp_push_mdh;
2960 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2962 spin_unlock(&lp->lp_lock);
2964 if (!LNetMDHandleIsInvalid(ping_mdh))
2965 LNetMDUnlink(ping_mdh);
2966 if (!LNetMDHandleIsInvalid(push_mdh))
2967 LNetMDUnlink(push_mdh);
2971 * Wait for work to be queued or some other change that must be
2972 * attended to. Returns non-zero if the discovery thread should shut
2975 static int lnet_peer_discovery_wait_for_work(void)
2982 cpt = lnet_net_lock_current();
2984 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
2985 TASK_INTERRUPTIBLE);
2986 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2988 if (lnet_push_target_resize_needed())
2990 if (!list_empty(&the_lnet.ln_dc_request))
2992 if (!list_empty(&the_lnet.ln_msg_resend))
2994 lnet_net_unlock(cpt);
2997 * wakeup max every second to check if there are peers that
2998 * have been stuck on the working queue for greater than
3001 schedule_timeout(cfs_time_seconds(1));
3002 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3003 cpt = lnet_net_lock_current();
3005 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3007 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3010 lnet_net_unlock(cpt);
3012 CDEBUG(D_NET, "woken: %d\n", rc);
3018 * Messages that were pending on a destroyed peer will be put on a global
3019 * resend list. The message resend list will be checked by
3020 * the discovery thread when it wakes up, and will resend messages. These
3021 * messages can still be sendable in the case the lpni which was the initial
3022 * cause of the message re-queue was transfered to another peer.
3024 * It is possible that LNet could be shutdown while we're iterating
3025 * through the list. lnet_shudown_lndnets() will attempt to access the
3026 * resend list, but will have to wait until the spinlock is released, by
3027 * which time there shouldn't be any more messages on the resend list.
3028 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3029 * for the messages so they can be released. The other case is that
3030 * lnet_shudown_lndnets() can finalize all the messages before this
3031 * function can visit the resend list, in which case this function will be
3034 static void lnet_resend_msgs(void)
3036 struct lnet_msg *msg, *tmp;
3037 struct list_head resend;
3040 INIT_LIST_HEAD(&resend);
3042 spin_lock(&the_lnet.ln_msg_resend_lock);
3043 list_splice(&the_lnet.ln_msg_resend, &resend);
3044 spin_unlock(&the_lnet.ln_msg_resend_lock);
3046 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3047 list_del_init(&msg->msg_list);
3048 rc = lnet_send(msg->msg_src_nid_param, msg,
3049 msg->msg_rtr_nid_param);
3051 CNETERR("Error sending %s to %s: %d\n",
3052 lnet_msgtyp2str(msg->msg_type),
3053 libcfs_id2str(msg->msg_target), rc);
3054 lnet_finalize(msg, rc);
3059 /* The discovery thread. */
3060 static int lnet_peer_discovery(void *arg)
3062 struct lnet_peer *lp;
3065 CDEBUG(D_NET, "started\n");
3066 cfs_block_allsigs();
3069 if (lnet_peer_discovery_wait_for_work())
3074 if (lnet_push_target_resize_needed())
3075 lnet_push_target_resize();
3077 lnet_net_lock(LNET_LOCK_EX);
3078 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3082 * Process all incoming discovery work requests. When
3083 * discovery must wait on a peer to change state, it
3084 * is added to the tail of the ln_dc_working queue. A
3085 * timestamp keeps track of when the peer was added,
3086 * so we can time out discovery requests that take too
3089 while (!list_empty(&the_lnet.ln_dc_request)) {
3090 lp = list_first_entry(&the_lnet.ln_dc_request,
3091 struct lnet_peer, lp_dc_list);
3092 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3094 * set the time the peer was put on the dc_working
3095 * queue. It shouldn't remain on the queue
3096 * forever, in case the GET message (for ping)
3097 * doesn't get a REPLY or the PUT message (for
3098 * push) doesn't get an ACK.
3100 lp->lp_last_queued = ktime_get_real_seconds();
3101 lnet_net_unlock(LNET_LOCK_EX);
3104 * Select an action depending on the state of
3105 * the peer and whether discovery is disabled.
3106 * The check whether discovery is disabled is
3107 * done after the code that handles processing
3108 * for arrived data, cleanup for failures, and
3109 * forcing a Ping or Push.
3111 spin_lock(&lp->lp_lock);
3112 CDEBUG(D_NET, "peer %s state %#x\n",
3113 libcfs_nid2str(lp->lp_primary_nid),
3115 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3116 rc = lnet_peer_data_present(lp);
3117 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3118 rc = lnet_peer_ping_failed(lp);
3119 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3120 rc = lnet_peer_push_failed(lp);
3121 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3122 rc = lnet_peer_send_ping(lp);
3123 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3124 rc = lnet_peer_send_push(lp);
3125 else if (lnet_peer_discovery_disabled)
3126 rc = lnet_peer_rediscover(lp);
3127 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3128 rc = lnet_peer_send_ping(lp);
3129 else if (lnet_peer_needs_push(lp))
3130 rc = lnet_peer_send_push(lp);
3132 rc = lnet_peer_discovered(lp);
3133 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3134 libcfs_nid2str(lp->lp_primary_nid),
3136 spin_unlock(&lp->lp_lock);
3138 lnet_net_lock(LNET_LOCK_EX);
3139 if (rc == LNET_REDISCOVER_PEER) {
3140 list_move(&lp->lp_dc_list,
3141 &the_lnet.ln_dc_request);
3143 lnet_peer_discovery_error(lp, rc);
3145 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3146 lnet_peer_discovery_complete(lp);
3147 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3151 lnet_net_unlock(LNET_LOCK_EX);
3154 CDEBUG(D_NET, "stopping\n");
3156 * Clean up before telling lnet_peer_discovery_stop() that
3157 * we're done. Use wake_up() below to somewhat reduce the
3158 * size of the thundering herd if there are multiple threads
3159 * waiting on discovery of a single peer.
3162 /* Queue cleanup 1: stop all pending pings and pushes. */
3163 lnet_net_lock(LNET_LOCK_EX);
3164 while (!list_empty(&the_lnet.ln_dc_working)) {
3165 lp = list_first_entry(&the_lnet.ln_dc_working,
3166 struct lnet_peer, lp_dc_list);
3167 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3168 lnet_net_unlock(LNET_LOCK_EX);
3169 lnet_peer_cancel_discovery(lp);
3170 lnet_net_lock(LNET_LOCK_EX);
3172 lnet_net_unlock(LNET_LOCK_EX);
3174 /* Queue cleanup 2: wait for the expired queue to clear. */
3175 while (!list_empty(&the_lnet.ln_dc_expired))
3176 schedule_timeout(cfs_time_seconds(1));
3178 /* Queue cleanup 3: clear the request queue. */
3179 lnet_net_lock(LNET_LOCK_EX);
3180 while (!list_empty(&the_lnet.ln_dc_request)) {
3181 lp = list_first_entry(&the_lnet.ln_dc_request,
3182 struct lnet_peer, lp_dc_list);
3183 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3184 lnet_peer_discovery_complete(lp);
3186 lnet_net_unlock(LNET_LOCK_EX);
3188 LNetEQFree(the_lnet.ln_dc_eqh);
3189 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3191 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3192 wake_up(&the_lnet.ln_dc_waitq);
3194 CDEBUG(D_NET, "stopped\n");
3199 /* ln_api_mutex is held on entry. */
3200 int lnet_peer_discovery_start(void)
3202 struct task_struct *task;
3205 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3208 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3210 CERROR("Can't allocate discovery EQ: %d\n", rc);
3214 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3215 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3218 CERROR("Can't start peer discovery thread: %d\n", rc);
3220 LNetEQFree(the_lnet.ln_dc_eqh);
3221 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3223 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3226 CDEBUG(D_NET, "discovery start: %d\n", rc);
3231 /* ln_api_mutex is held on entry. */
3232 void lnet_peer_discovery_stop(void)
3234 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3237 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3238 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3239 wake_up(&the_lnet.ln_dc_waitq);
3241 wait_event(the_lnet.ln_dc_waitq,
3242 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3244 LASSERT(list_empty(&the_lnet.ln_dc_request));
3245 LASSERT(list_empty(&the_lnet.ln_dc_working));
3246 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3248 CDEBUG(D_NET, "discovery stopped\n");
3254 lnet_debug_peer(lnet_nid_t nid)
3256 char *aliveness = "NA";
3257 struct lnet_peer_ni *lp;
3260 cpt = lnet_cpt_of_nid(nid, NULL);
3263 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3265 lnet_net_unlock(cpt);
3266 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3270 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3271 aliveness = lp->lpni_alive ? "up" : "down";
3273 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3274 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3275 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3276 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3277 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3279 lnet_peer_ni_decref_locked(lp);
3281 lnet_net_unlock(cpt);
3284 /* Gathering information for userspace. */
3286 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3287 char aliveness[LNET_MAX_STR_LEN],
3288 __u32 *cpt_iter, __u32 *refcount,
3289 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3290 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3291 __u32 *peer_tx_qnob)
3293 struct lnet_peer_table *peer_table;
3294 struct lnet_peer_ni *lp;
3299 /* get the number of CPTs */
3300 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3302 /* if the cpt number to be examined is >= the number of cpts in
3303 * the system then indicate that there are no more cpts to examin
3305 if (*cpt_iter >= lncpt)
3308 /* get the current table */
3309 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3310 /* if the ptable is NULL then there are no more cpts to examine */
3311 if (peer_table == NULL)
3314 lnet_net_lock(*cpt_iter);
3316 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3317 struct list_head *peers = &peer_table->pt_hash[j];
3319 list_for_each_entry(lp, peers, lpni_hashlist) {
3320 if (peer_index-- > 0)
3323 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3324 if (lnet_isrouter(lp) ||
3325 lnet_peer_aliveness_enabled(lp))
3326 snprintf(aliveness, LNET_MAX_STR_LEN,
3327 lp->lpni_alive ? "up" : "down");
3329 *nid = lp->lpni_nid;
3330 *refcount = atomic_read(&lp->lpni_refcount);
3331 *ni_peer_tx_credits =
3332 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3333 *peer_tx_credits = lp->lpni_txcredits;
3334 *peer_rtr_credits = lp->lpni_rtrcredits;
3335 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3336 *peer_tx_qnob = lp->lpni_txqnob;
3342 lnet_net_unlock(*cpt_iter);
3346 return found ? 0 : -ENOENT;
3349 /* ln_api_mutex is held, which keeps the peer list stable */
3350 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3352 struct lnet_ioctl_element_stats *lpni_stats;
3353 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3354 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3355 struct lnet_peer_ni_credit_info *lpni_info;
3356 struct lnet_peer_ni *lpni;
3357 struct lnet_peer *lp;
3362 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3369 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3370 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3371 size *= lp->lp_nnis;
3372 if (size > cfg->prcfg_size) {
3373 cfg->prcfg_size = size;
3378 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3379 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3380 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3381 cfg->prcfg_count = lp->lp_nnis;
3382 cfg->prcfg_size = size;
3383 cfg->prcfg_state = lp->lp_state;
3385 /* Allocate helper buffers. */
3387 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3390 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3393 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3394 if (!lpni_msg_stats)
3395 goto out_free_stats;
3396 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3398 goto out_free_msg_stats;
3403 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3404 nid = lpni->lpni_nid;
3405 if (copy_to_user(bulk, &nid, sizeof(nid)))
3406 goto out_free_hstats;
3407 bulk += sizeof(nid);
3409 memset(lpni_info, 0, sizeof(*lpni_info));
3410 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3411 if (lnet_isrouter(lpni) ||
3412 lnet_peer_aliveness_enabled(lpni))
3413 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3414 lpni->lpni_alive ? "up" : "down");
3416 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3417 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3418 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3419 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3420 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3421 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3422 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3423 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3424 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3425 goto out_free_hstats;
3426 bulk += sizeof(*lpni_info);
3428 memset(lpni_stats, 0, sizeof(*lpni_stats));
3429 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3430 LNET_STATS_TYPE_SEND);
3431 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3432 LNET_STATS_TYPE_RECV);
3433 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3434 LNET_STATS_TYPE_DROP);
3435 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3436 goto out_free_hstats;
3437 bulk += sizeof(*lpni_stats);
3438 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3439 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3440 goto out_free_hstats;
3441 bulk += sizeof(*lpni_msg_stats);
3442 lpni_hstats->hlpni_network_timeout =
3443 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3444 lpni_hstats->hlpni_remote_dropped =
3445 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3446 lpni_hstats->hlpni_remote_timeout =
3447 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3448 lpni_hstats->hlpni_remote_error =
3449 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3450 lpni_hstats->hlpni_health_value =
3451 atomic_read(&lpni->lpni_healthv);
3452 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3453 goto out_free_hstats;
3454 bulk += sizeof(*lpni_hstats);
3459 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3461 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3463 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3465 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3467 lnet_peer_decref_locked(lp);
3473 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3475 /* the mt could've shutdown and cleaned up the queues */
3476 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3479 if (list_empty(&lpni->lpni_recovery) &&
3480 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3481 CERROR("lpni %s added to recovery queue. Health = %d\n",
3482 libcfs_nid2str(lpni->lpni_nid),
3483 atomic_read(&lpni->lpni_healthv));
3484 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3485 lnet_peer_ni_addref_locked(lpni);
3489 /* Call with the ln_api_mutex held */
3491 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3493 struct lnet_peer_table *ptable;
3494 struct lnet_peer *lp;
3495 struct lnet_peer_net *lpn;
3496 struct lnet_peer_ni *lpni;
3500 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3504 lnet_net_lock(LNET_LOCK_EX);
3505 lpni = lnet_find_peer_ni_locked(nid);
3507 lnet_net_unlock(LNET_LOCK_EX);
3510 atomic_set(&lpni->lpni_healthv, value);
3511 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3512 lnet_peer_ni_decref_locked(lpni);
3513 lnet_net_unlock(LNET_LOCK_EX);
3517 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3520 * Walk all the peers and reset the healhv for each one to the
3523 lnet_net_lock(LNET_LOCK_EX);
3524 for (cpt = 0; cpt < lncpt; cpt++) {
3525 ptable = the_lnet.ln_peer_tables[cpt];
3526 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3527 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3528 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3530 atomic_set(&lpni->lpni_healthv, value);
3531 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3536 lnet_net_unlock(LNET_LOCK_EX);