4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <linux/sched.h>
38 #ifdef HAVE_SCHED_HEADERS
39 #include <linux/sched/signal.h>
41 #include <linux/uaccess.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER (1)
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
54 if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
55 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
56 lnet_peer_ni_decref_locked(lpni);
61 lnet_peer_net_added(struct lnet_net *net)
63 struct lnet_peer_ni *lpni, *tmp;
65 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66 lpni_on_remote_peer_ni_list) {
68 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
71 spin_lock(&lpni->lpni_lock);
72 lpni->lpni_txcredits =
73 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
74 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
75 lpni->lpni_rtrcredits =
76 lnet_peer_buffer_credits(lpni->lpni_net);
77 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
78 spin_unlock(&lpni->lpni_lock);
80 lnet_peer_remove_from_remote_list(lpni);
86 lnet_peer_tables_destroy(void)
88 struct lnet_peer_table *ptable;
89 struct list_head *hash;
93 if (!the_lnet.ln_peer_tables)
96 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
97 hash = ptable->pt_hash;
98 if (!hash) /* not intialized */
101 LASSERT(list_empty(&ptable->pt_zombie_list));
103 ptable->pt_hash = NULL;
104 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
105 LASSERT(list_empty(&hash[j]));
107 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
110 cfs_percpt_free(the_lnet.ln_peer_tables);
111 the_lnet.ln_peer_tables = NULL;
115 lnet_peer_tables_create(void)
117 struct lnet_peer_table *ptable;
118 struct list_head *hash;
122 the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
124 if (the_lnet.ln_peer_tables == NULL) {
125 CERROR("Failed to allocate cpu-partition peer tables\n");
129 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
130 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
131 LNET_PEER_HASH_SIZE * sizeof(*hash));
133 CERROR("Failed to create peer hash table\n");
134 lnet_peer_tables_destroy();
138 spin_lock_init(&ptable->pt_zombie_lock);
139 INIT_LIST_HEAD(&ptable->pt_zombie_list);
141 INIT_LIST_HEAD(&ptable->pt_peer_list);
143 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
144 INIT_LIST_HEAD(&hash[j]);
145 ptable->pt_hash = hash; /* sign of initialization */
151 static struct lnet_peer_ni *
152 lnet_peer_ni_alloc(lnet_nid_t nid)
154 struct lnet_peer_ni *lpni;
155 struct lnet_net *net;
158 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
160 LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
164 INIT_LIST_HEAD(&lpni->lpni_txq);
165 INIT_LIST_HEAD(&lpni->lpni_hashlist);
166 INIT_LIST_HEAD(&lpni->lpni_peer_nis);
167 INIT_LIST_HEAD(&lpni->lpni_recovery);
168 INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
169 LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
171 spin_lock_init(&lpni->lpni_lock);
173 if (lnet_peers_start_down())
174 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
176 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
177 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
178 lpni->lpni_nid = nid;
179 lpni->lpni_cpt = cpt;
180 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
182 net = lnet_get_net_locked(LNET_NIDNET(nid));
183 lpni->lpni_net = net;
185 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
186 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
187 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
188 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
191 * This peer_ni is not on a local network, so we
192 * cannot add the credits here. In case the net is
193 * added later, add the peer_ni to the remote peer ni
194 * list so it can be easily found and revisited.
196 /* FIXME: per-net implementation instead? */
197 atomic_inc(&lpni->lpni_refcount);
198 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
199 &the_lnet.ln_remote_peer_ni_list);
202 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
207 static struct lnet_peer_net *
208 lnet_peer_net_alloc(__u32 net_id)
210 struct lnet_peer_net *lpn;
212 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
216 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
217 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
218 lpn->lpn_net_id = net_id;
220 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
226 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
228 struct lnet_peer *lp;
230 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
232 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
233 LASSERT(list_empty(&lpn->lpn_peer_nis));
234 LASSERT(list_empty(&lpn->lpn_peer_nets));
236 lpn->lpn_peer = NULL;
237 LIBCFS_FREE(lpn, sizeof(*lpn));
239 lnet_peer_decref_locked(lp);
242 static struct lnet_peer *
243 lnet_peer_alloc(lnet_nid_t nid)
245 struct lnet_peer *lp;
247 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
251 INIT_LIST_HEAD(&lp->lp_rtrq);
252 INIT_LIST_HEAD(&lp->lp_routes);
253 INIT_LIST_HEAD(&lp->lp_peer_list);
254 INIT_LIST_HEAD(&lp->lp_peer_nets);
255 INIT_LIST_HEAD(&lp->lp_dc_list);
256 INIT_LIST_HEAD(&lp->lp_dc_pendq);
257 INIT_LIST_HEAD(&lp->lp_rtr_list);
258 init_waitqueue_head(&lp->lp_dc_waitq);
259 spin_lock_init(&lp->lp_lock);
260 lp->lp_primary_nid = nid;
262 * Turn off discovery for loopback peer. If you're creating a peer
263 * for the loopback interface then that was initiated when we
264 * attempted to send a message over the loopback. There is no need
265 * to ever use a different interface when sending messages to
268 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
269 lp->lp_state = LNET_PEER_NO_DISCOVERY;
270 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
272 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
278 lnet_destroy_peer_locked(struct lnet_peer *lp)
280 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
282 LASSERT(atomic_read(&lp->lp_refcount) == 0);
283 LASSERT(lp->lp_rtr_refcount == 0);
284 LASSERT(list_empty(&lp->lp_peer_nets));
285 LASSERT(list_empty(&lp->lp_peer_list));
286 LASSERT(list_empty(&lp->lp_dc_list));
289 lnet_ping_buffer_decref(lp->lp_data);
292 * if there are messages still on the pending queue, then make
293 * sure to queue them on the ln_msg_resend list so they can be
294 * resent at a later point if the discovery thread is still
296 * If the discovery thread has stopped, then the wakeup will be a
297 * no-op, and it is expected the lnet_shutdown_lndnets() will
298 * eventually be called, which will traverse this list and
299 * finalize the messages on the list.
300 * We can not resend them now because we're holding the cpt lock.
301 * Releasing the lock can cause an inconsistent state
303 spin_lock(&the_lnet.ln_msg_resend_lock);
304 spin_lock(&lp->lp_lock);
305 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
306 spin_unlock(&lp->lp_lock);
307 spin_unlock(&the_lnet.ln_msg_resend_lock);
308 wake_up(&the_lnet.ln_dc_waitq);
310 LIBCFS_FREE(lp, sizeof(*lp));
314 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
315 * that peer_net, detach the peer_net from the peer.
317 * Call with lnet_net_lock/EX held
320 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
322 struct lnet_peer_table *ptable;
323 struct lnet_peer_net *lpn;
324 struct lnet_peer *lp;
327 * Belts and suspenders: gracefully handle teardown of a
328 * partially connected peer_ni.
330 lpn = lpni->lpni_peer_net;
332 list_del_init(&lpni->lpni_peer_nis);
334 * If there are no lpni's left, we detach lpn from
335 * lp_peer_nets, so it cannot be found anymore.
337 if (list_empty(&lpn->lpn_peer_nis))
338 list_del_init(&lpn->lpn_peer_nets);
340 /* Update peer NID count. */
345 * If there are no more peer nets, make the peer unfindable
346 * via the peer_tables.
348 * Otherwise, if the peer is DISCOVERED, tell discovery to
349 * take another look at it. This is a no-op if discovery for
350 * this peer did the detaching.
352 if (list_empty(&lp->lp_peer_nets)) {
353 list_del_init(&lp->lp_peer_list);
354 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
356 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
357 /* Discovery isn't running, nothing to do here. */
358 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
359 lnet_peer_queue_for_discovery(lp);
360 wake_up(&the_lnet.ln_dc_waitq);
362 CDEBUG(D_NET, "peer %s NID %s\n",
363 libcfs_nid2str(lp->lp_primary_nid),
364 libcfs_nid2str(lpni->lpni_nid));
367 /* called with lnet_net_lock LNET_LOCK_EX held */
369 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
371 struct lnet_peer_table *ptable = NULL;
373 /* don't remove a peer_ni if it's also a gateway */
374 if (lnet_isrouter(lpni)) {
375 CERROR("Peer NI %s is a gateway. Can not delete it\n",
376 libcfs_nid2str(lpni->lpni_nid));
380 lnet_peer_remove_from_remote_list(lpni);
382 /* remove peer ni from the hash list. */
383 list_del_init(&lpni->lpni_hashlist);
386 * indicate the peer is being deleted so the monitor thread can
387 * remove it from the recovery queue.
389 spin_lock(&lpni->lpni_lock);
390 lpni->lpni_state |= LNET_PEER_NI_DELETING;
391 spin_unlock(&lpni->lpni_lock);
393 /* decrement the ref count on the peer table */
394 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
395 LASSERT(ptable->pt_number > 0);
399 * The peer_ni can no longer be found with a lookup. But there
400 * can be current users, so keep track of it on the zombie
401 * list until the reference count has gone to zero.
403 * The last reference may be lost in a place where the
404 * lnet_net_lock locks only a single cpt, and that cpt may not
405 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
408 spin_lock(&ptable->pt_zombie_lock);
409 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
410 ptable->pt_zombies++;
411 spin_unlock(&ptable->pt_zombie_lock);
413 /* no need to keep this peer_ni on the hierarchy anymore */
414 lnet_peer_detach_peer_ni_locked(lpni);
416 /* remove hashlist reference on peer_ni */
417 lnet_peer_ni_decref_locked(lpni);
422 void lnet_peer_uninit(void)
424 struct lnet_peer_ni *lpni, *tmp;
426 lnet_net_lock(LNET_LOCK_EX);
428 /* remove all peer_nis from the remote peer and the hash list */
429 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
430 lpni_on_remote_peer_ni_list)
431 lnet_peer_ni_del_locked(lpni);
433 lnet_peer_tables_destroy();
435 lnet_net_unlock(LNET_LOCK_EX);
439 lnet_peer_del_locked(struct lnet_peer *peer)
441 struct lnet_peer_ni *lpni = NULL, *lpni2;
444 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
446 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
447 while (lpni != NULL) {
448 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
449 rc = lnet_peer_ni_del_locked(lpni);
459 lnet_peer_del(struct lnet_peer *peer)
461 lnet_net_lock(LNET_LOCK_EX);
462 lnet_peer_del_locked(peer);
463 lnet_net_unlock(LNET_LOCK_EX);
469 * Delete a NID from a peer. Call with ln_api_mutex held.
472 * -EPERM: Non-DLC deletion from DLC-configured peer.
473 * -ENOENT: No lnet_peer_ni corresponding to the nid.
474 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
475 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
478 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
480 struct lnet_peer_ni *lpni;
481 lnet_nid_t primary_nid = lp->lp_primary_nid;
484 if (!(flags & LNET_PEER_CONFIGURED)) {
485 if (lp->lp_state & LNET_PEER_CONFIGURED) {
490 lpni = lnet_find_peer_ni_locked(nid);
495 lnet_peer_ni_decref_locked(lpni);
496 if (lp != lpni->lpni_peer_net->lpn_peer) {
502 * This function only allows deletion of the primary NID if it
505 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
510 lnet_net_lock(LNET_LOCK_EX);
512 rc = lnet_peer_ni_del_locked(lpni);
514 lnet_net_unlock(LNET_LOCK_EX);
517 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
518 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
524 lnet_peer_table_cleanup_locked(struct lnet_net *net,
525 struct lnet_peer_table *ptable)
528 struct lnet_peer_ni *next;
529 struct lnet_peer_ni *lpni;
530 struct lnet_peer *peer;
532 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
533 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
535 if (net != NULL && net != lpni->lpni_net)
538 peer = lpni->lpni_peer_net->lpn_peer;
539 if (peer->lp_primary_nid != lpni->lpni_nid) {
540 lnet_peer_ni_del_locked(lpni);
544 * Removing the primary NID implies removing
545 * the entire peer. Advance next beyond any
546 * peer_ni that belongs to the same peer.
548 list_for_each_entry_from(next, &ptable->pt_hash[i],
550 if (next->lpni_peer_net->lpn_peer != peer)
553 lnet_peer_del_locked(peer);
559 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
563 spin_lock(&ptable->pt_zombie_lock);
564 while (ptable->pt_zombies) {
565 spin_unlock(&ptable->pt_zombie_lock);
567 if (is_power_of_2(i)) {
569 "Waiting for %d zombies on peer table\n",
572 set_current_state(TASK_UNINTERRUPTIBLE);
573 schedule_timeout(cfs_time_seconds(1) >> 1);
574 spin_lock(&ptable->pt_zombie_lock);
576 spin_unlock(&ptable->pt_zombie_lock);
580 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
581 struct lnet_peer_table *ptable)
583 struct lnet_peer_ni *lp;
584 struct lnet_peer_ni *tmp;
588 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
589 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
591 if (net != lp->lpni_net)
594 if (!lnet_isrouter(lp))
597 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
599 lnet_net_unlock(LNET_LOCK_EX);
600 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
601 lnet_net_lock(LNET_LOCK_EX);
607 lnet_peer_tables_cleanup(struct lnet_net *net)
610 struct lnet_peer_table *ptable;
612 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
613 /* If just deleting the peers for a NI, get rid of any routes these
614 * peers are gateways for. */
615 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
616 lnet_net_lock(LNET_LOCK_EX);
617 lnet_peer_table_del_rtrs_locked(net, ptable);
618 lnet_net_unlock(LNET_LOCK_EX);
621 /* Start the cleanup process */
622 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
623 lnet_net_lock(LNET_LOCK_EX);
624 lnet_peer_table_cleanup_locked(net, ptable);
625 lnet_net_unlock(LNET_LOCK_EX);
628 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
629 lnet_peer_ni_finalize_wait(ptable);
632 static struct lnet_peer_ni *
633 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
635 struct list_head *peers;
636 struct lnet_peer_ni *lp;
638 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
640 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
641 list_for_each_entry(lp, peers, lpni_hashlist) {
642 if (lp->lpni_nid == nid) {
643 lnet_peer_ni_addref_locked(lp);
651 struct lnet_peer_ni *
652 lnet_find_peer_ni_locked(lnet_nid_t nid)
654 struct lnet_peer_ni *lpni;
655 struct lnet_peer_table *ptable;
658 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
660 ptable = the_lnet.ln_peer_tables[cpt];
661 lpni = lnet_get_peer_ni_locked(ptable, nid);
667 lnet_find_peer(lnet_nid_t nid)
669 struct lnet_peer_ni *lpni;
670 struct lnet_peer *lp = NULL;
673 cpt = lnet_net_lock_current();
674 lpni = lnet_find_peer_ni_locked(nid);
676 lp = lpni->lpni_peer_net->lpn_peer;
677 lnet_peer_addref_locked(lp);
678 lnet_peer_ni_decref_locked(lpni);
680 lnet_net_unlock(cpt);
685 struct lnet_peer_ni *
686 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
687 struct lnet_peer_net *peer_net,
688 struct lnet_peer_ni *prev)
690 struct lnet_peer_ni *lpni;
691 struct lnet_peer_net *net = peer_net;
695 if (list_empty(&peer->lp_peer_nets))
698 net = list_entry(peer->lp_peer_nets.next,
699 struct lnet_peer_net,
702 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
708 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
710 * if you reached the end of the peer ni list and the peer
711 * net is specified then there are no more peer nis in that
718 * we reached the end of this net ni list. move to the
721 if (prev->lpni_peer_net->lpn_peer_nets.next ==
723 /* no more nets and no more NIs. */
726 /* get the next net */
727 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
728 struct lnet_peer_net,
730 /* get the ni on it */
731 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
737 /* there are more nis left */
738 lpni = list_entry(prev->lpni_peer_nis.next,
739 struct lnet_peer_ni, lpni_peer_nis);
744 /* Call with the ln_api_mutex held */
745 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
747 struct lnet_process_id id;
748 struct lnet_peer_table *ptable;
749 struct lnet_peer *lp;
758 if (the_lnet.ln_state != LNET_STATE_RUNNING)
761 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
764 * Count the number of peers, and return E2BIG if the buffer
765 * is too small. We'll also return the desired size.
768 for (cpt = 0; cpt < lncpt; cpt++) {
769 ptable = the_lnet.ln_peer_tables[cpt];
770 count += ptable->pt_peers;
772 size = count * sizeof(*ids);
777 * Walk the peer lists and copy out the primary nids.
778 * This is safe because the peer lists are only modified
779 * while the ln_api_mutex is held. So we don't need to
780 * hold the lnet_net_lock as well, and can therefore
781 * directly call copy_to_user().
784 memset(&id, 0, sizeof(id));
785 id.pid = LNET_PID_LUSTRE;
787 for (cpt = 0; cpt < lncpt; cpt++) {
788 ptable = the_lnet.ln_peer_tables[cpt];
789 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
792 id.nid = lp->lp_primary_nid;
793 if (copy_to_user(&ids[i], &id, sizeof(id)))
806 * Start pushes to peers that need to be updated for a configuration
807 * change on this node.
810 lnet_push_update_to_peers(int force)
812 struct lnet_peer_table *ptable;
813 struct lnet_peer *lp;
817 lnet_net_lock(LNET_LOCK_EX);
818 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
819 for (cpt = 0; cpt < lncpt; cpt++) {
820 ptable = the_lnet.ln_peer_tables[cpt];
821 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
823 spin_lock(&lp->lp_lock);
824 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
825 lp->lp_state |= LNET_PEER_FORCE_PUSH;
826 spin_unlock(&lp->lp_lock);
828 if (lnet_peer_needs_push(lp))
829 lnet_peer_queue_for_discovery(lp);
832 lnet_net_unlock(LNET_LOCK_EX);
833 wake_up(&the_lnet.ln_dc_waitq);
837 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
838 * this is a preferred point-to-point path. Call with lnet_net_lock in
842 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
846 if (lpni->lpni_pref_nnids == 0)
848 if (lpni->lpni_pref_nnids == 1)
849 return lpni->lpni_pref.nid == nid;
850 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
851 if (lpni->lpni_pref.nids[i] == nid)
858 * Set a single ni as preferred, provided no preferred ni is already
859 * defined. Only to be used for non-multi-rail peer_ni.
862 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
866 spin_lock(&lpni->lpni_lock);
867 if (nid == LNET_NID_ANY) {
869 } else if (lpni->lpni_pref_nnids > 0) {
871 } else if (lpni->lpni_pref_nnids == 0) {
872 lpni->lpni_pref.nid = nid;
873 lpni->lpni_pref_nnids = 1;
874 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
876 spin_unlock(&lpni->lpni_lock);
878 CDEBUG(D_NET, "peer %s nid %s: %d\n",
879 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
884 * Clear the preferred NID from a non-multi-rail peer_ni, provided
885 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
888 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
892 spin_lock(&lpni->lpni_lock);
893 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
894 lpni->lpni_pref_nnids = 0;
895 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
896 } else if (lpni->lpni_pref_nnids == 0) {
901 spin_unlock(&lpni->lpni_lock);
903 CDEBUG(D_NET, "peer %s: %d\n",
904 libcfs_nid2str(lpni->lpni_nid), rc);
909 * Clear the preferred NIDs from a non-multi-rail peer.
912 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
914 struct lnet_peer_ni *lpni = NULL;
916 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
917 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
921 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
923 lnet_nid_t *nids = NULL;
924 lnet_nid_t *oldnids = NULL;
925 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
930 if (nid == LNET_NID_ANY) {
935 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
940 /* A non-MR node may have only one preferred NI per peer_ni */
941 if (lpni->lpni_pref_nnids > 0) {
942 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
948 if (lpni->lpni_pref_nnids != 0) {
949 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
950 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
955 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
956 if (lpni->lpni_pref.nids[i] == nid) {
957 LIBCFS_FREE(nids, size);
961 nids[i] = lpni->lpni_pref.nids[i];
966 lnet_net_lock(LNET_LOCK_EX);
967 spin_lock(&lpni->lpni_lock);
968 if (lpni->lpni_pref_nnids == 0) {
969 lpni->lpni_pref.nid = nid;
971 oldnids = lpni->lpni_pref.nids;
972 lpni->lpni_pref.nids = nids;
974 lpni->lpni_pref_nnids++;
975 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
976 spin_unlock(&lpni->lpni_lock);
977 lnet_net_unlock(LNET_LOCK_EX);
980 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
981 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
984 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
985 spin_lock(&lpni->lpni_lock);
986 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
987 spin_unlock(&lpni->lpni_lock);
989 CDEBUG(D_NET, "peer %s nid %s: %d\n",
990 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
995 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
997 lnet_nid_t *nids = NULL;
998 lnet_nid_t *oldnids = NULL;
999 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1004 if (lpni->lpni_pref_nnids == 0) {
1009 if (lpni->lpni_pref_nnids == 1) {
1010 if (lpni->lpni_pref.nid != nid) {
1014 } else if (lpni->lpni_pref_nnids == 2) {
1015 if (lpni->lpni_pref.nids[0] != nid &&
1016 lpni->lpni_pref.nids[1] != nid) {
1021 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1022 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1027 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1028 if (lpni->lpni_pref.nids[i] != nid)
1030 nids[j++] = lpni->lpni_pref.nids[i];
1032 /* Check if we actually removed a nid. */
1033 if (j == lpni->lpni_pref_nnids) {
1034 LIBCFS_FREE(nids, size);
1040 lnet_net_lock(LNET_LOCK_EX);
1041 spin_lock(&lpni->lpni_lock);
1042 if (lpni->lpni_pref_nnids == 1) {
1043 lpni->lpni_pref.nid = LNET_NID_ANY;
1044 } else if (lpni->lpni_pref_nnids == 2) {
1045 oldnids = lpni->lpni_pref.nids;
1046 if (oldnids[0] == nid)
1047 lpni->lpni_pref.nid = oldnids[1];
1049 lpni->lpni_pref.nid = oldnids[2];
1051 oldnids = lpni->lpni_pref.nids;
1052 lpni->lpni_pref.nids = nids;
1054 lpni->lpni_pref_nnids--;
1055 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1056 spin_unlock(&lpni->lpni_lock);
1057 lnet_net_unlock(LNET_LOCK_EX);
1060 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1061 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1064 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1065 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1070 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1072 struct lnet_peer_ni *lpni;
1073 lnet_nid_t primary_nid = nid;
1075 lpni = lnet_find_peer_ni_locked(nid);
1077 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1078 lnet_peer_ni_decref_locked(lpni);
1085 LNetPrimaryNID(lnet_nid_t nid)
1087 struct lnet_peer *lp;
1088 struct lnet_peer_ni *lpni;
1089 lnet_nid_t primary_nid = nid;
1093 cpt = lnet_net_lock_current();
1094 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1099 lp = lpni->lpni_peer_net->lpn_peer;
1100 while (!lnet_peer_is_uptodate(lp)) {
1101 rc = lnet_discover_peer_locked(lpni, cpt, true);
1104 lp = lpni->lpni_peer_net->lpn_peer;
1106 primary_nid = lp->lp_primary_nid;
1108 lnet_peer_ni_decref_locked(lpni);
1110 lnet_net_unlock(cpt);
1112 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1113 libcfs_nid2str(primary_nid), rc);
1116 EXPORT_SYMBOL(LNetPrimaryNID);
1118 struct lnet_peer_net *
1119 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1121 struct lnet_peer_net *peer_net;
1122 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1123 if (peer_net->lpn_net_id == net_id)
1130 * Attach a peer_ni to a peer_net and peer. This function assumes
1131 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1132 * may be attached to a different peer, in which case it will be
1133 * properly detached first. The whole operation is done atomically.
1135 * Always returns 0. This is the last function called from functions
1136 * that do return an int, so returning 0 here allows the compiler to
1140 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1141 struct lnet_peer_net *lpn,
1142 struct lnet_peer_ni *lpni,
1145 struct lnet_peer_table *ptable;
1147 /* Install the new peer_ni */
1148 lnet_net_lock(LNET_LOCK_EX);
1149 /* Add peer_ni to global peer table hash, if necessary. */
1150 if (list_empty(&lpni->lpni_hashlist)) {
1151 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1153 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1154 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1155 ptable->pt_version++;
1156 ptable->pt_number++;
1157 /* This is the 1st refcount on lpni. */
1158 atomic_inc(&lpni->lpni_refcount);
1161 /* Detach the peer_ni from an existing peer, if necessary. */
1162 if (lpni->lpni_peer_net) {
1163 LASSERT(lpni->lpni_peer_net != lpn);
1164 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1165 lnet_peer_detach_peer_ni_locked(lpni);
1166 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1167 lpni->lpni_peer_net = NULL;
1170 /* Add peer_ni to peer_net */
1171 lpni->lpni_peer_net = lpn;
1172 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1173 lnet_peer_net_addref_locked(lpn);
1175 /* Add peer_net to peer */
1176 if (!lpn->lpn_peer) {
1178 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1179 lnet_peer_addref_locked(lp);
1182 /* Add peer to global peer list, if necessary */
1183 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1184 if (list_empty(&lp->lp_peer_list)) {
1185 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1190 /* Update peer state */
1191 spin_lock(&lp->lp_lock);
1192 if (flags & LNET_PEER_CONFIGURED) {
1193 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1194 lp->lp_state |= LNET_PEER_CONFIGURED;
1196 if (flags & LNET_PEER_MULTI_RAIL) {
1197 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1198 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1199 lnet_peer_clr_non_mr_pref_nids(lp);
1202 spin_unlock(&lp->lp_lock);
1205 lnet_net_unlock(LNET_LOCK_EX);
1207 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1208 libcfs_nid2str(lp->lp_primary_nid),
1209 libcfs_nid2str(lpni->lpni_nid), flags);
1215 * Create a new peer, with nid as its primary nid.
1217 * Call with the lnet_api_mutex held.
1220 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1222 struct lnet_peer *lp;
1223 struct lnet_peer_net *lpn;
1224 struct lnet_peer_ni *lpni;
1227 LASSERT(nid != LNET_NID_ANY);
1230 * No need for the lnet_net_lock here, because the
1231 * lnet_api_mutex is held.
1233 lpni = lnet_find_peer_ni_locked(nid);
1235 /* A peer with this NID already exists. */
1236 lp = lpni->lpni_peer_net->lpn_peer;
1237 lnet_peer_ni_decref_locked(lpni);
1239 * This is an error if the peer was configured and the
1240 * primary NID differs or an attempt is made to change
1241 * the Multi-Rail flag. Otherwise the assumption is
1242 * that an existing peer is being modified.
1244 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1245 if (lp->lp_primary_nid != nid)
1247 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1251 /* Delete and recreate as a configured peer. */
1255 /* Create peer, peer_net, and peer_ni. */
1257 lp = lnet_peer_alloc(nid);
1260 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1263 lpni = lnet_peer_ni_alloc(nid);
1267 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1270 LIBCFS_FREE(lpn, sizeof(*lpn));
1272 LIBCFS_FREE(lp, sizeof(*lp));
1274 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1275 libcfs_nid2str(nid), flags, rc);
1280 * Add a NID to a peer. Call with ln_api_mutex held.
1283 * -EPERM: Non-DLC addition to a DLC-configured peer.
1284 * -EEXIST: The NID was configured by DLC for a different peer.
1285 * -ENOMEM: Out of memory.
1286 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1287 * non-multi-rail peer.
1290 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1292 struct lnet_peer_net *lpn;
1293 struct lnet_peer_ni *lpni;
1297 LASSERT(nid != LNET_NID_ANY);
1299 /* A configured peer can only be updated through configuration. */
1300 if (!(flags & LNET_PEER_CONFIGURED)) {
1301 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1308 * The MULTI_RAIL flag can be set but not cleared, because
1309 * that would leave the peer struct in an invalid state.
1311 if (flags & LNET_PEER_MULTI_RAIL) {
1312 spin_lock(&lp->lp_lock);
1313 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1314 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1315 lnet_peer_clr_non_mr_pref_nids(lp);
1317 spin_unlock(&lp->lp_lock);
1318 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1323 lpni = lnet_find_peer_ni_locked(nid);
1326 * A peer_ni already exists. This is only a problem if
1327 * it is not connected to this peer and was configured
1330 lnet_peer_ni_decref_locked(lpni);
1331 if (lpni->lpni_peer_net->lpn_peer == lp)
1333 if (lnet_peer_ni_is_configured(lpni)) {
1337 /* If this is the primary NID, destroy the peer. */
1338 if (lnet_peer_ni_is_primary(lpni)) {
1339 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1340 lpni = lnet_peer_ni_alloc(nid);
1347 lpni = lnet_peer_ni_alloc(nid);
1355 * Get the peer_net. Check that we're not adding a second
1356 * peer_ni on a peer_net of a non-multi-rail peer.
1358 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1360 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1365 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1370 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1373 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1374 if (!lpni->lpni_peer_net)
1375 LIBCFS_FREE(lpni, sizeof(*lpni));
1377 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1378 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1384 * Update the primary NID of a peer, if possible.
1386 * Call with the lnet_api_mutex held.
1389 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1391 lnet_nid_t old = lp->lp_primary_nid;
1394 if (lp->lp_primary_nid == nid)
1396 rc = lnet_peer_add_nid(lp, nid, flags);
1399 lp->lp_primary_nid = nid;
1401 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1402 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1407 * lpni creation initiated due to traffic either sending or receiving.
1410 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1412 struct lnet_peer *lp;
1413 struct lnet_peer_net *lpn;
1414 struct lnet_peer_ni *lpni;
1418 if (nid == LNET_NID_ANY) {
1423 /* lnet_net_lock is not needed here because ln_api_lock is held */
1424 lpni = lnet_find_peer_ni_locked(nid);
1427 * We must have raced with another thread. Since we
1428 * know next to nothing about a peer_ni created by
1429 * traffic, we just assume everything is ok and
1432 lnet_peer_ni_decref_locked(lpni);
1436 /* Create peer, peer_net, and peer_ni. */
1438 lp = lnet_peer_alloc(nid);
1441 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1444 lpni = lnet_peer_ni_alloc(nid);
1447 if (pref != LNET_NID_ANY)
1448 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1450 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1453 LIBCFS_FREE(lpn, sizeof(*lpn));
1455 LIBCFS_FREE(lp, sizeof(*lp));
1457 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1462 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1464 * This API handles the following combinations:
1465 * Create a peer with its primary NI if only the prim_nid is provided
1466 * Add a NID to a peer identified by the prim_nid. The peer identified
1467 * by the prim_nid must already exist.
1468 * The peer being created may be non-MR.
1470 * The caller must hold ln_api_mutex. This prevents the peer from
1471 * being created/modified/deleted by a different thread.
1474 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1476 struct lnet_peer *lp = NULL;
1477 struct lnet_peer_ni *lpni;
1480 /* The prim_nid must always be specified */
1481 if (prim_nid == LNET_NID_ANY)
1484 flags = LNET_PEER_CONFIGURED;
1486 flags |= LNET_PEER_MULTI_RAIL;
1489 * If nid isn't specified, we must create a new peer with
1490 * prim_nid as its primary nid.
1492 if (nid == LNET_NID_ANY)
1493 return lnet_peer_add(prim_nid, flags);
1495 /* Look up the prim_nid, which must exist. */
1496 lpni = lnet_find_peer_ni_locked(prim_nid);
1499 lnet_peer_ni_decref_locked(lpni);
1500 lp = lpni->lpni_peer_net->lpn_peer;
1502 /* Peer must have been configured. */
1503 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1504 CDEBUG(D_NET, "peer %s was not configured\n",
1505 libcfs_nid2str(prim_nid));
1509 /* Primary NID must match */
1510 if (lp->lp_primary_nid != prim_nid) {
1511 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1512 libcfs_nid2str(prim_nid),
1513 libcfs_nid2str(lp->lp_primary_nid));
1517 /* Multi-Rail flag must match. */
1518 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1519 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1520 libcfs_nid2str(prim_nid));
1524 return lnet_peer_add_nid(lp, nid, flags);
1528 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1530 * This API handles the following combinations:
1531 * Delete a NI from a peer if both prim_nid and nid are provided.
1532 * Delete a peer if only prim_nid is provided.
1533 * Delete a peer if its primary nid is provided.
1535 * The caller must hold ln_api_mutex. This prevents the peer from
1536 * being modified/deleted by a different thread.
1539 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1541 struct lnet_peer *lp;
1542 struct lnet_peer_ni *lpni;
1545 if (prim_nid == LNET_NID_ANY)
1548 lpni = lnet_find_peer_ni_locked(prim_nid);
1551 lnet_peer_ni_decref_locked(lpni);
1552 lp = lpni->lpni_peer_net->lpn_peer;
1554 if (prim_nid != lp->lp_primary_nid) {
1555 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1556 libcfs_nid2str(prim_nid),
1557 libcfs_nid2str(lp->lp_primary_nid));
1561 lnet_net_lock(LNET_LOCK_EX);
1562 if (lp->lp_rtr_refcount > 0) {
1563 lnet_net_unlock(LNET_LOCK_EX);
1564 CERROR("%s is a router. Can not be deleted\n",
1565 libcfs_nid2str(prim_nid));
1568 lnet_net_unlock(LNET_LOCK_EX);
1570 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1571 return lnet_peer_del(lp);
1573 flags = LNET_PEER_CONFIGURED;
1574 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1575 flags |= LNET_PEER_MULTI_RAIL;
1577 return lnet_peer_del_nid(lp, nid, flags);
1581 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1583 struct lnet_peer_table *ptable;
1584 struct lnet_peer_net *lpn;
1586 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1588 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1589 LASSERT(list_empty(&lpni->lpni_txq));
1590 LASSERT(lpni->lpni_txqnob == 0);
1591 LASSERT(list_empty(&lpni->lpni_peer_nis));
1592 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1594 lpn = lpni->lpni_peer_net;
1595 lpni->lpni_peer_net = NULL;
1596 lpni->lpni_net = NULL;
1598 /* remove the peer ni from the zombie list */
1599 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1600 spin_lock(&ptable->pt_zombie_lock);
1601 list_del_init(&lpni->lpni_hashlist);
1602 ptable->pt_zombies--;
1603 spin_unlock(&ptable->pt_zombie_lock);
1605 if (lpni->lpni_pref_nnids > 1) {
1606 LIBCFS_FREE(lpni->lpni_pref.nids,
1607 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1609 LIBCFS_FREE(lpni, sizeof(*lpni));
1611 lnet_peer_net_decref_locked(lpn);
1614 struct lnet_peer_ni *
1615 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1617 struct lnet_peer_ni *lpni = NULL;
1620 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1621 return ERR_PTR(-ESHUTDOWN);
1624 * find if a peer_ni already exists.
1625 * If so then just return that.
1627 lpni = lnet_find_peer_ni_locked(nid);
1631 lnet_net_unlock(cpt);
1633 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1636 goto out_net_relock;
1639 lpni = lnet_find_peer_ni_locked(nid);
1649 * Get a peer_ni for the given nid, create it if necessary. Takes a
1650 * hold on the peer_ni.
1652 struct lnet_peer_ni *
1653 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1655 struct lnet_peer_ni *lpni = NULL;
1658 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1659 return ERR_PTR(-ESHUTDOWN);
1662 * find if a peer_ni already exists.
1663 * If so then just return that.
1665 lpni = lnet_find_peer_ni_locked(nid);
1671 * use the lnet_api_mutex to serialize the creation of the peer_ni
1672 * and the creation/deletion of the local ni/net. When a local ni is
1673 * created, if there exists a set of peer_nis on that network,
1674 * they need to be traversed and updated. When a local NI is
1675 * deleted, which could result in a network being deleted, then
1676 * all peer nis on that network need to be removed as well.
1678 * Creation through traffic should also be serialized with
1679 * creation through DLC.
1681 lnet_net_unlock(cpt);
1682 mutex_lock(&the_lnet.ln_api_mutex);
1684 * Shutdown is only set under the ln_api_lock, so a single
1685 * check here is sufficent.
1687 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1688 lpni = ERR_PTR(-ESHUTDOWN);
1689 goto out_mutex_unlock;
1692 rc = lnet_peer_ni_traffic_add(nid, pref);
1695 goto out_mutex_unlock;
1698 lpni = lnet_find_peer_ni_locked(nid);
1702 mutex_unlock(&the_lnet.ln_api_mutex);
1705 /* Lock has been dropped, check again for shutdown. */
1706 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1708 lnet_peer_ni_decref_locked(lpni);
1709 lpni = ERR_PTR(-ESHUTDOWN);
1720 * Is a peer uptodate from the point of view of discovery?
1722 * If it is currently being processed, obviously not.
1723 * A forced Ping or Push is also handled by the discovery thread.
1725 * Otherwise look at whether the peer needs rediscovering.
1728 lnet_peer_is_uptodate(struct lnet_peer *lp)
1732 spin_lock(&lp->lp_lock);
1733 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1734 LNET_PEER_FORCE_PING |
1735 LNET_PEER_FORCE_PUSH)) {
1737 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1739 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1740 if (lnet_peer_discovery_disabled)
1744 } else if (lnet_peer_needs_push(lp)) {
1746 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1747 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1754 spin_unlock(&lp->lp_lock);
1760 * Queue a peer for the attention of the discovery thread. Call with
1761 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1762 * -EALREADY if the peer was already queued.
1764 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1768 spin_lock(&lp->lp_lock);
1769 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1770 lp->lp_state |= LNET_PEER_DISCOVERING;
1771 spin_unlock(&lp->lp_lock);
1772 if (list_empty(&lp->lp_dc_list)) {
1773 lnet_peer_addref_locked(lp);
1774 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1775 wake_up(&the_lnet.ln_dc_waitq);
1781 CDEBUG(D_NET, "Queue peer %s: %d\n",
1782 libcfs_nid2str(lp->lp_primary_nid), rc);
1788 * Discovery of a peer is complete. Wake all waiters on the peer.
1789 * Call with lnet_net_lock/EX held.
1791 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1793 struct lnet_msg *msg, *tmp;
1795 struct list_head pending_msgs;
1797 INIT_LIST_HEAD(&pending_msgs);
1799 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1800 libcfs_nid2str(lp->lp_primary_nid));
1802 list_del_init(&lp->lp_dc_list);
1803 spin_lock(&lp->lp_lock);
1804 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1805 spin_unlock(&lp->lp_lock);
1806 wake_up_all(&lp->lp_dc_waitq);
1808 lnet_net_unlock(LNET_LOCK_EX);
1810 /* iterate through all pending messages and send them again */
1811 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1812 list_del_init(&msg->msg_list);
1813 if (lp->lp_dc_error) {
1814 lnet_finalize(msg, lp->lp_dc_error);
1818 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1819 lnet_msgtyp2str(msg->msg_type),
1820 libcfs_id2str(msg->msg_target));
1821 rc = lnet_send(msg->msg_src_nid_param, msg,
1822 msg->msg_rtr_nid_param);
1824 CNETERR("Error sending %s to %s: %d\n",
1825 lnet_msgtyp2str(msg->msg_type),
1826 libcfs_id2str(msg->msg_target), rc);
1827 lnet_finalize(msg, rc);
1830 lnet_net_lock(LNET_LOCK_EX);
1831 lnet_peer_decref_locked(lp);
1835 * Handle inbound push.
1836 * Like any event handler, called with lnet_res_lock/CPT held.
1838 void lnet_peer_push_event(struct lnet_event *ev)
1840 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1841 struct lnet_peer *lp;
1843 /* lnet_find_peer() adds a refcount */
1844 lp = lnet_find_peer(ev->source.nid);
1846 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1847 libcfs_nid2str(ev->initiator.nid),
1848 libcfs_nid2str(ev->source.nid));
1852 /* Ensure peer state remains consistent while we modify it. */
1853 spin_lock(&lp->lp_lock);
1856 * If some kind of error happened the contents of the message
1857 * cannot be used. Clear the NIDS_UPTODATE and set the
1858 * FORCE_PING flag to trigger a ping.
1861 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1862 lp->lp_state |= LNET_PEER_FORCE_PING;
1863 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1865 libcfs_nid2str(lp->lp_primary_nid),
1866 libcfs_nid2str(ev->source.nid));
1871 * A push with invalid or corrupted info. Clear the UPTODATE
1872 * flag to trigger a ping.
1874 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1875 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1876 lp->lp_state |= LNET_PEER_FORCE_PING;
1877 CDEBUG(D_NET, "Corrupted Push from %s\n",
1878 libcfs_nid2str(lp->lp_primary_nid));
1883 * Make sure we'll allocate the correct size ping buffer when
1886 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1887 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1890 * A non-Multi-Rail peer is not supposed to be capable of
1893 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1894 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1895 libcfs_nid2str(lp->lp_primary_nid));
1900 * Check the MULTIRAIL flag. Complain if the peer was DLC
1901 * configured without it.
1903 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1904 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1905 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1906 libcfs_nid2str(lp->lp_primary_nid));
1908 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1909 lnet_peer_clr_non_mr_pref_nids(lp);
1914 * The peer may have discovery disabled at its end. Set
1915 * NO_DISCOVERY as appropriate.
1917 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1918 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1919 libcfs_nid2str(lp->lp_primary_nid));
1920 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1921 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1922 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1923 libcfs_nid2str(lp->lp_primary_nid));
1924 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1928 * Check for truncation of the Put message. Clear the
1929 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1930 * and tell discovery to allocate a bigger buffer.
1932 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1933 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1934 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1935 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1936 lp->lp_state |= LNET_PEER_FORCE_PING;
1937 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1938 libcfs_nid2str(lp->lp_primary_nid),
1939 pbuf->pb_info.pi_nnis);
1944 * Check whether the Put data is stale. Stale data can just be
1947 if (pbuf->pb_info.pi_nnis > 1 &&
1948 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1949 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1950 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1951 libcfs_nid2str(lp->lp_primary_nid),
1952 LNET_PING_BUFFER_SEQNO(pbuf),
1958 * Check whether the Put data is new, in which case we clear
1959 * the UPTODATE flag and prepare to process it.
1961 * If the Put data is current, and the peer is UPTODATE then
1962 * we assome everything is all right and drop the data as
1965 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1966 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1967 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1968 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1969 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1970 libcfs_nid2str(lp->lp_primary_nid),
1971 LNET_PING_BUFFER_SEQNO(pbuf),
1977 * If there is data present that hasn't been processed yet,
1978 * we'll replace it if the Put contained newer data and it
1979 * fits. We're racing with a Ping or earlier Push in this
1982 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1983 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1984 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1985 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1986 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1987 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1988 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1989 libcfs_nid2str(lp->lp_primary_nid),
1990 LNET_PING_BUFFER_SEQNO(pbuf),
1991 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1997 * Allocate a buffer to copy the data. On a failure we drop
1998 * the Push and set FORCE_PING to force the discovery
1999 * thread to fix the problem by pinging the peer.
2001 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2003 lp->lp_state |= LNET_PEER_FORCE_PING;
2004 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2005 libcfs_nid2str(lp->lp_primary_nid),
2006 LNET_PING_BUFFER_SEQNO(pbuf));
2011 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2012 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2013 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2014 CDEBUG(D_NET, "Received Push %s %u\n",
2015 libcfs_nid2str(lp->lp_primary_nid),
2016 LNET_PING_BUFFER_SEQNO(pbuf));
2020 * Queue the peer for discovery if not done, force it on the request
2021 * queue and wake the discovery thread if the peer was already queued,
2022 * because its status changed.
2024 spin_unlock(&lp->lp_lock);
2025 lnet_net_lock(LNET_LOCK_EX);
2026 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2027 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2028 wake_up(&the_lnet.ln_dc_waitq);
2030 /* Drop refcount from lookup */
2031 lnet_peer_decref_locked(lp);
2032 lnet_net_unlock(LNET_LOCK_EX);
2036 * Clear the discovery error state, unless we're already discovering
2037 * this peer, in which case the error is current.
2039 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2041 spin_lock(&lp->lp_lock);
2042 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2043 lp->lp_dc_error = 0;
2044 spin_unlock(&lp->lp_lock);
2048 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2049 * dropped/retaken within this function. An lnet_peer_ni is passed in
2050 * because discovery could tear down an lnet_peer.
2053 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2056 struct lnet_peer *lp;
2060 lnet_net_unlock(cpt);
2061 lnet_net_lock(LNET_LOCK_EX);
2062 lp = lpni->lpni_peer_net->lpn_peer;
2063 lnet_peer_clear_discovery_error(lp);
2066 * We're willing to be interrupted. The lpni can become a
2067 * zombie if we race with DLC, so we must check for that.
2070 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2071 if (signal_pending(current))
2073 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2075 if (lp->lp_dc_error)
2077 if (lnet_peer_is_uptodate(lp))
2079 lnet_peer_queue_for_discovery(lp);
2081 * if caller requested a non-blocking operation then
2082 * return immediately. Once discovery is complete then the
2083 * peer ref will be decremented and any pending messages
2084 * that were stopped due to discovery will be transmitted.
2089 lnet_peer_addref_locked(lp);
2090 lnet_net_unlock(LNET_LOCK_EX);
2092 finish_wait(&lp->lp_dc_waitq, &wait);
2093 lnet_net_lock(LNET_LOCK_EX);
2094 lnet_peer_decref_locked(lp);
2095 /* Peer may have changed */
2096 lp = lpni->lpni_peer_net->lpn_peer;
2098 finish_wait(&lp->lp_dc_waitq, &wait);
2100 lnet_net_unlock(LNET_LOCK_EX);
2104 * If the peer has changed after we've discovered the older peer,
2105 * then we need to discovery the new peer to make sure the
2106 * interface information is up to date
2108 if (lp != lpni->lpni_peer_net->lpn_peer)
2111 if (signal_pending(current))
2113 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2115 else if (lp->lp_dc_error)
2116 rc = lp->lp_dc_error;
2118 CDEBUG(D_NET, "non-blocking discovery\n");
2119 else if (!lnet_peer_is_uptodate(lp))
2122 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2123 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2124 libcfs_nid2str(lpni->lpni_nid), rc,
2125 (!block) ? "pending discovery" : "discovery complete");
2130 /* Handle an incoming ack for a push. */
2132 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2134 struct lnet_ping_buffer *pbuf;
2136 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2137 spin_lock(&lp->lp_lock);
2138 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2139 lp->lp_push_error = ev->status;
2141 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2143 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2144 spin_unlock(&lp->lp_lock);
2146 CDEBUG(D_NET, "peer %s ev->status %d\n",
2147 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2150 /* Handle a Reply message. This is the reply to a Ping message. */
2152 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2154 struct lnet_ping_buffer *pbuf;
2157 spin_lock(&lp->lp_lock);
2160 * If some kind of error happened the contents of message
2161 * cannot be used. Set PING_FAILED to trigger a retry.
2164 lp->lp_state |= LNET_PEER_PING_FAILED;
2165 lp->lp_ping_error = ev->status;
2166 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2168 libcfs_nid2str(lp->lp_primary_nid),
2169 libcfs_nid2str(ev->source.nid));
2173 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2174 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2175 lnet_swap_pinginfo(pbuf);
2178 * A reply with invalid or corrupted info. Set PING_FAILED to
2181 rc = lnet_ping_info_validate(&pbuf->pb_info);
2183 lp->lp_state |= LNET_PEER_PING_FAILED;
2184 lp->lp_ping_error = 0;
2185 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2186 libcfs_nid2str(lp->lp_primary_nid), rc);
2191 * Update the MULTI_RAIL flag based on the reply. If the peer
2192 * was configured with DLC then the setting should match what
2195 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2196 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2197 /* Everything's fine */
2198 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2199 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2200 libcfs_nid2str(lp->lp_primary_nid));
2202 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2203 lnet_peer_clr_non_mr_pref_nids(lp);
2205 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2206 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2207 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2208 libcfs_nid2str(lp->lp_primary_nid));
2210 CERROR("Multi-Rail state vanished from %s\n",
2211 libcfs_nid2str(lp->lp_primary_nid));
2212 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2217 * Make sure we'll allocate the correct size ping buffer when
2220 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2221 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2224 * The peer may have discovery disabled at its end. Set
2225 * NO_DISCOVERY as appropriate.
2227 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2228 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2229 libcfs_nid2str(lp->lp_primary_nid));
2230 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2231 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2232 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2233 libcfs_nid2str(lp->lp_primary_nid));
2234 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2238 * Check for truncation of the Reply. Clear PING_SENT and set
2239 * PING_FAILED to trigger a retry.
2241 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2242 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2243 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2244 lp->lp_state |= LNET_PEER_PING_FAILED;
2245 lp->lp_ping_error = 0;
2246 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2247 libcfs_nid2str(lp->lp_primary_nid),
2248 pbuf->pb_info.pi_nnis);
2253 * Check the sequence numbers in the reply. These are only
2254 * available if the reply came from a Multi-Rail peer.
2256 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2257 pbuf->pb_info.pi_nnis > 1 &&
2258 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2259 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2260 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2261 libcfs_nid2str(lp->lp_primary_nid),
2262 LNET_PING_BUFFER_SEQNO(pbuf),
2267 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2268 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2271 /* We're happy with the state of the data in the buffer. */
2272 CDEBUG(D_NET, "peer %s data present %u\n",
2273 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2274 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2275 lnet_ping_buffer_decref(lp->lp_data);
2277 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2278 lnet_ping_buffer_addref(pbuf);
2281 lp->lp_state &= ~LNET_PEER_PING_SENT;
2282 spin_unlock(&lp->lp_lock);
2286 * Send event handling. Only matters for error cases, where we clean
2287 * up state on the peer and peer_ni that would otherwise be updated in
2288 * the REPLY event handler for a successful Ping, and the ACK event
2289 * handler for a successful Push.
2292 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2299 spin_lock(&lp->lp_lock);
2300 if (ev->msg_type == LNET_MSG_GET) {
2301 lp->lp_state &= ~LNET_PEER_PING_SENT;
2302 lp->lp_state |= LNET_PEER_PING_FAILED;
2303 lp->lp_ping_error = ev->status;
2304 } else { /* ev->msg_type == LNET_MSG_PUT */
2305 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2306 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2307 lp->lp_push_error = ev->status;
2309 spin_unlock(&lp->lp_lock);
2310 rc = LNET_REDISCOVER_PEER;
2312 CDEBUG(D_NET, "%s Send to %s: %d\n",
2313 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2314 libcfs_nid2str(ev->target.nid), rc);
2319 * Unlink event handling. This event is only seen if a call to
2320 * LNetMDUnlink() caused the event to be unlinked. If this call was
2321 * made after the event was set up in LNetGet() or LNetPut() then we
2322 * assume the Ping or Push timed out.
2325 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2327 spin_lock(&lp->lp_lock);
2328 /* We've passed through LNetGet() */
2329 if (lp->lp_state & LNET_PEER_PING_SENT) {
2330 lp->lp_state &= ~LNET_PEER_PING_SENT;
2331 lp->lp_state |= LNET_PEER_PING_FAILED;
2332 lp->lp_ping_error = -ETIMEDOUT;
2333 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2334 libcfs_nid2str(lp->lp_primary_nid));
2336 /* We've passed through LNetPut() */
2337 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2338 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2339 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2340 lp->lp_push_error = -ETIMEDOUT;
2341 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2342 libcfs_nid2str(lp->lp_primary_nid));
2344 spin_unlock(&lp->lp_lock);
2348 * Event handler for the discovery EQ.
2350 * Called with lnet_res_lock(cpt) held. The cpt is the
2351 * lnet_cpt_of_cookie() of the md handle cookie.
2353 static void lnet_discovery_event_handler(struct lnet_event *event)
2355 struct lnet_peer *lp = event->md.user_ptr;
2356 struct lnet_ping_buffer *pbuf;
2359 /* discovery needs to take another look */
2360 rc = LNET_REDISCOVER_PEER;
2362 CDEBUG(D_NET, "Received event: %d\n", event->type);
2364 switch (event->type) {
2365 case LNET_EVENT_ACK:
2366 lnet_discovery_event_ack(lp, event);
2368 case LNET_EVENT_REPLY:
2369 lnet_discovery_event_reply(lp, event);
2371 case LNET_EVENT_SEND:
2372 /* Only send failure triggers a retry. */
2373 rc = lnet_discovery_event_send(lp, event);
2375 case LNET_EVENT_UNLINK:
2376 /* LNetMDUnlink() was called */
2377 lnet_discovery_event_unlink(lp, event);
2380 /* Invalid events. */
2383 lnet_net_lock(LNET_LOCK_EX);
2384 if (event->unlinked) {
2385 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2386 lnet_ping_buffer_decref(pbuf);
2387 lnet_peer_decref_locked(lp);
2390 /* put peer back at end of request queue, if discovery not already
2392 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2393 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2394 wake_up(&the_lnet.ln_dc_waitq);
2396 lnet_net_unlock(LNET_LOCK_EX);
2400 * Build a peer from incoming data.
2402 * The NIDs in the incoming data are supposed to be structured as follows:
2405 * - other NIDs in same net
2406 * - NIDs in second net
2407 * - NIDs in third net
2409 * This due to the way the list of NIDs in the data is created.
2411 * Note that this function will mark the peer uptodate unless an
2412 * ENOMEM is encontered. All other errors are due to a conflict
2413 * between the DLC configuration and what discovery sees. We treat DLC
2414 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2415 * peer from becoming stuck in discovery.
2417 static int lnet_peer_merge_data(struct lnet_peer *lp,
2418 struct lnet_ping_buffer *pbuf)
2420 struct lnet_peer_ni *lpni;
2421 lnet_nid_t *curnis = NULL;
2422 struct lnet_ni_status *addnis = NULL;
2423 lnet_nid_t *delnis = NULL;
2433 flags = LNET_PEER_DISCOVERED;
2434 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2435 flags |= LNET_PEER_MULTI_RAIL;
2438 * Cache the routing feature for the peer; whether it is enabled
2439 * for disabled as reported by the remote peer.
2441 spin_lock(&lp->lp_lock);
2442 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2443 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2445 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2446 spin_unlock(&lp->lp_lock);
2448 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2449 LIBCFS_ALLOC(curnis, nnis * sizeof(*curnis));
2450 LIBCFS_ALLOC(addnis, nnis * sizeof(*addnis));
2451 LIBCFS_ALLOC(delnis, nnis * sizeof(*delnis));
2452 if (!curnis || !addnis || !delnis) {
2460 /* Construct the list of NIDs present in peer. */
2462 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2463 curnis[ncurnis++] = lpni->lpni_nid;
2466 * Check for NIDs in pbuf not present in curnis[].
2467 * The loop starts at 1 to skip the loopback NID.
2469 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2470 for (j = 0; j < ncurnis; j++)
2471 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2474 addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2477 * Check for NIDs in curnis[] not present in pbuf.
2478 * The nested loop starts at 1 to skip the loopback NID.
2480 * But never add the loopback NID to delnis[]: if it is
2481 * present in curnis[] then this peer is for this node.
2483 for (i = 0; i < ncurnis; i++) {
2484 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2486 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2487 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2489 * update the information we cache for the
2490 * peer with the latest information we
2493 lpni = lnet_find_peer_ni_locked(curnis[i]);
2495 lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2496 lnet_peer_ni_decref_locked(lpni);
2501 if (j == pbuf->pb_info.pi_nnis)
2502 delnis[ndelnis++] = curnis[i];
2505 for (i = 0; i < naddnis; i++) {
2506 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2508 CERROR("Error adding NID %s to peer %s: %d\n",
2509 libcfs_nid2str(addnis[i].ns_nid),
2510 libcfs_nid2str(lp->lp_primary_nid), rc);
2514 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2516 lpni->lpni_ns_status = addnis[i].ns_status;
2517 lnet_peer_ni_decref_locked(lpni);
2521 for (i = 0; i < ndelnis; i++) {
2522 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2524 CERROR("Error deleting NID %s from peer %s: %d\n",
2525 libcfs_nid2str(delnis[i]),
2526 libcfs_nid2str(lp->lp_primary_nid), rc);
2532 * Errors other than -ENOMEM are due to peers having been
2533 * configured with DLC. Ignore these because DLC overrides
2538 LIBCFS_FREE(curnis, nnis * sizeof(*curnis));
2539 LIBCFS_FREE(addnis, nnis * sizeof(*addnis));
2540 LIBCFS_FREE(delnis, nnis * sizeof(*delnis));
2541 lnet_ping_buffer_decref(pbuf);
2542 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2545 spin_lock(&lp->lp_lock);
2546 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2547 lp->lp_state |= LNET_PEER_FORCE_PING;
2548 spin_unlock(&lp->lp_lock);
2554 * The data in pbuf says lp is its primary peer, but the data was
2555 * received by a different peer. Try to update lp with the data.
2558 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2560 struct lnet_handle_md mdh;
2562 /* Queue lp for discovery, and force it on the request queue. */
2563 lnet_net_lock(LNET_LOCK_EX);
2564 if (lnet_peer_queue_for_discovery(lp))
2565 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2566 lnet_net_unlock(LNET_LOCK_EX);
2568 LNetInvalidateMDHandle(&mdh);
2571 * Decide whether we can move the peer to the DATA_PRESENT state.
2573 * We replace stale data for a multi-rail peer, repair PING_FAILED
2574 * status, and preempt FORCE_PING.
2576 * If after that we have DATA_PRESENT, we merge it into this peer.
2578 spin_lock(&lp->lp_lock);
2579 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2580 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2581 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2582 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2583 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2584 lnet_ping_buffer_decref(pbuf);
2589 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2590 lnet_ping_buffer_decref(lp->lp_data);
2592 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2594 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2595 mdh = lp->lp_ping_mdh;
2596 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2597 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2598 lp->lp_ping_error = 0;
2600 if (lp->lp_state & LNET_PEER_FORCE_PING)
2601 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2602 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2603 spin_unlock(&lp->lp_lock);
2605 if (!LNetMDHandleIsInvalid(mdh))
2609 return lnet_peer_merge_data(lp, pbuf);
2611 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2616 * Update a peer using the data received.
2618 static int lnet_peer_data_present(struct lnet_peer *lp)
2619 __must_hold(&lp->lp_lock)
2621 struct lnet_ping_buffer *pbuf;
2622 struct lnet_peer_ni *lpni;
2623 lnet_nid_t nid = LNET_NID_ANY;
2629 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2630 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2631 spin_unlock(&lp->lp_lock);
2634 * Modifications of peer structures are done while holding the
2635 * ln_api_mutex. A global lock is required because we may be
2636 * modifying multiple peer structures, and a mutex greatly
2637 * simplifies memory management.
2639 * The actual changes to the data structures must also protect
2640 * against concurrent lookups, for which the lnet_net_lock in
2641 * LNET_LOCK_EX mode is used.
2643 mutex_lock(&the_lnet.ln_api_mutex);
2644 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2650 * If this peer is not on the peer list then it is being torn
2651 * down, and our reference count may be all that is keeping it
2652 * alive. Don't do any work on it.
2654 if (list_empty(&lp->lp_peer_list))
2657 flags = LNET_PEER_DISCOVERED;
2658 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2659 flags |= LNET_PEER_MULTI_RAIL;
2662 * Check whether the primary NID in the message matches the
2663 * primary NID of the peer. If it does, update the peer, if
2664 * it it does not, check whether there is already a peer with
2665 * that primary NID. If no such peer exists, try to update
2666 * the primary NID of the current peer (allowed if it was
2667 * created due to message traffic) and complete the update.
2668 * If the peer did exist, hand off the data to it.
2670 * The peer for the loopback interface is a special case: this
2671 * is the peer for the local node, and we want to set its
2672 * primary NID to the correct value here. Moreover, this peer
2673 * can show up with only the loopback NID in the ping buffer.
2675 if (pbuf->pb_info.pi_nnis <= 1)
2677 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2678 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2679 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2681 rc = lnet_peer_merge_data(lp, pbuf);
2682 } else if (lp->lp_primary_nid == nid) {
2683 rc = lnet_peer_merge_data(lp, pbuf);
2685 lpni = lnet_find_peer_ni_locked(nid);
2687 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2689 CERROR("Primary NID error %s versus %s: %d\n",
2690 libcfs_nid2str(lp->lp_primary_nid),
2691 libcfs_nid2str(nid), rc);
2693 rc = lnet_peer_merge_data(lp, pbuf);
2696 rc = lnet_peer_set_primary_data(
2697 lpni->lpni_peer_net->lpn_peer, pbuf);
2698 lnet_peer_ni_decref_locked(lpni);
2702 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2703 mutex_unlock(&the_lnet.ln_api_mutex);
2705 spin_lock(&lp->lp_lock);
2706 /* Tell discovery to re-check the peer immediately. */
2708 rc = LNET_REDISCOVER_PEER;
2713 * A ping failed. Clear the PING_FAILED state and set the
2714 * FORCE_PING state, to ensure a retry even if discovery is
2715 * disabled. This avoids being left with incorrect state.
2717 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2718 __must_hold(&lp->lp_lock)
2720 struct lnet_handle_md mdh;
2723 mdh = lp->lp_ping_mdh;
2724 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2725 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2726 lp->lp_state |= LNET_PEER_FORCE_PING;
2727 rc = lp->lp_ping_error;
2728 lp->lp_ping_error = 0;
2729 spin_unlock(&lp->lp_lock);
2731 if (!LNetMDHandleIsInvalid(mdh))
2734 CDEBUG(D_NET, "peer %s:%d\n",
2735 libcfs_nid2str(lp->lp_primary_nid), rc);
2737 spin_lock(&lp->lp_lock);
2738 return rc ? rc : LNET_REDISCOVER_PEER;
2742 * Select NID to send a Ping or Push to.
2744 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2746 struct lnet_peer_ni *lpni;
2748 /* Look for a direct-connected NID for this peer. */
2750 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2751 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2756 return lpni->lpni_nid;
2758 /* Look for a routed-connected NID for this peer. */
2760 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2761 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2766 return lpni->lpni_nid;
2768 return LNET_NID_ANY;
2771 /* Active side of ping. */
2772 static int lnet_peer_send_ping(struct lnet_peer *lp)
2773 __must_hold(&lp->lp_lock)
2780 lp->lp_state |= LNET_PEER_PING_SENT;
2781 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2782 spin_unlock(&lp->lp_lock);
2784 cpt = lnet_net_lock_current();
2785 /* Refcount for MD. */
2786 lnet_peer_addref_locked(lp);
2787 pnid = lnet_peer_select_nid(lp);
2788 lnet_net_unlock(cpt);
2790 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2792 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2793 the_lnet.ln_dc_eqh, false);
2796 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2797 * refcount on the peer, otherwise LNetMDUnlink will be called
2798 * which will eventually do that.
2802 lnet_peer_decref_locked(lp);
2803 lnet_net_unlock(cpt);
2804 rc = -rc; /* change the rc to negative value */
2806 } else if (rc < 0) {
2810 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2812 spin_lock(&lp->lp_lock);
2816 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2818 * The errors that get us here are considered hard errors and
2819 * cause Discovery to terminate. So we clear PING_SENT, but do
2820 * not set either PING_FAILED or FORCE_PING. In fact we need
2821 * to clear PING_FAILED, because the unlink event handler will
2822 * have set it if we called LNetMDUnlink() above.
2824 spin_lock(&lp->lp_lock);
2825 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2830 * This function exists because you cannot call LNetMDUnlink() from an
2833 static int lnet_peer_push_failed(struct lnet_peer *lp)
2834 __must_hold(&lp->lp_lock)
2836 struct lnet_handle_md mdh;
2839 mdh = lp->lp_push_mdh;
2840 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2841 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2842 rc = lp->lp_push_error;
2843 lp->lp_push_error = 0;
2844 spin_unlock(&lp->lp_lock);
2846 if (!LNetMDHandleIsInvalid(mdh))
2849 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2850 spin_lock(&lp->lp_lock);
2851 return rc ? rc : LNET_REDISCOVER_PEER;
2854 /* Active side of push. */
2855 static int lnet_peer_send_push(struct lnet_peer *lp)
2856 __must_hold(&lp->lp_lock)
2858 struct lnet_ping_buffer *pbuf;
2859 struct lnet_process_id id;
2864 /* Don't push to a non-multi-rail peer. */
2865 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2866 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2870 lp->lp_state |= LNET_PEER_PUSH_SENT;
2871 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2872 spin_unlock(&lp->lp_lock);
2874 cpt = lnet_net_lock_current();
2875 pbuf = the_lnet.ln_ping_target;
2876 lnet_ping_buffer_addref(pbuf);
2877 lnet_net_unlock(cpt);
2879 /* Push source MD */
2880 md.start = &pbuf->pb_info;
2881 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2882 md.threshold = 2; /* Put/Ack */
2885 md.eq_handle = the_lnet.ln_dc_eqh;
2888 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2890 lnet_ping_buffer_decref(pbuf);
2891 CERROR("Can't bind push source MD: %d\n", rc);
2894 cpt = lnet_net_lock_current();
2895 /* Refcount for MD. */
2896 lnet_peer_addref_locked(lp);
2897 id.pid = LNET_PID_LUSTRE;
2898 id.nid = lnet_peer_select_nid(lp);
2899 lnet_net_unlock(cpt);
2901 if (id.nid == LNET_NID_ANY) {
2906 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2907 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2908 LNET_PROTO_PING_MATCHBITS, 0, 0);
2913 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2915 spin_lock(&lp->lp_lock);
2919 LNetMDUnlink(lp->lp_push_mdh);
2920 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2922 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2924 * The errors that get us here are considered hard errors and
2925 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2926 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2927 * because the unlink event handler will have set it if we
2928 * called LNetMDUnlink() above.
2930 spin_lock(&lp->lp_lock);
2931 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2936 * An unrecoverable error was encountered during discovery.
2937 * Set error status in peer and abort discovery.
2939 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2941 CDEBUG(D_NET, "Discovery error %s: %d\n",
2942 libcfs_nid2str(lp->lp_primary_nid), error);
2944 spin_lock(&lp->lp_lock);
2945 lp->lp_dc_error = error;
2946 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2947 lp->lp_state |= LNET_PEER_REDISCOVER;
2948 spin_unlock(&lp->lp_lock);
2952 * Mark the peer as discovered.
2954 static int lnet_peer_discovered(struct lnet_peer *lp)
2955 __must_hold(&lp->lp_lock)
2957 lp->lp_state |= LNET_PEER_DISCOVERED;
2958 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2959 LNET_PEER_REDISCOVER);
2961 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2967 * Mark the peer as to be rediscovered.
2969 static int lnet_peer_rediscover(struct lnet_peer *lp)
2970 __must_hold(&lp->lp_lock)
2972 lp->lp_state |= LNET_PEER_REDISCOVER;
2973 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2975 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2981 * Discovering this peer is taking too long. Cancel any Ping or Push
2982 * that discovery is waiting on by unlinking the relevant MDs. The
2983 * lnet_discovery_event_handler() will proceed from here and complete
2986 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
2988 struct lnet_handle_md ping_mdh;
2989 struct lnet_handle_md push_mdh;
2991 LNetInvalidateMDHandle(&ping_mdh);
2992 LNetInvalidateMDHandle(&push_mdh);
2994 spin_lock(&lp->lp_lock);
2995 if (lp->lp_state & LNET_PEER_PING_SENT) {
2996 ping_mdh = lp->lp_ping_mdh;
2997 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2999 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3000 push_mdh = lp->lp_push_mdh;
3001 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3003 spin_unlock(&lp->lp_lock);
3005 if (!LNetMDHandleIsInvalid(ping_mdh))
3006 LNetMDUnlink(ping_mdh);
3007 if (!LNetMDHandleIsInvalid(push_mdh))
3008 LNetMDUnlink(push_mdh);
3012 * Wait for work to be queued or some other change that must be
3013 * attended to. Returns non-zero if the discovery thread should shut
3016 static int lnet_peer_discovery_wait_for_work(void)
3023 cpt = lnet_net_lock_current();
3025 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3026 TASK_INTERRUPTIBLE);
3027 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3029 if (lnet_push_target_resize_needed())
3031 if (!list_empty(&the_lnet.ln_dc_request))
3033 if (!list_empty(&the_lnet.ln_msg_resend))
3035 lnet_net_unlock(cpt);
3038 * wakeup max every second to check if there are peers that
3039 * have been stuck on the working queue for greater than
3042 schedule_timeout(cfs_time_seconds(1));
3043 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3044 cpt = lnet_net_lock_current();
3046 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3048 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3051 lnet_net_unlock(cpt);
3053 CDEBUG(D_NET, "woken: %d\n", rc);
3059 * Messages that were pending on a destroyed peer will be put on a global
3060 * resend list. The message resend list will be checked by
3061 * the discovery thread when it wakes up, and will resend messages. These
3062 * messages can still be sendable in the case the lpni which was the initial
3063 * cause of the message re-queue was transfered to another peer.
3065 * It is possible that LNet could be shutdown while we're iterating
3066 * through the list. lnet_shudown_lndnets() will attempt to access the
3067 * resend list, but will have to wait until the spinlock is released, by
3068 * which time there shouldn't be any more messages on the resend list.
3069 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3070 * for the messages so they can be released. The other case is that
3071 * lnet_shudown_lndnets() can finalize all the messages before this
3072 * function can visit the resend list, in which case this function will be
3075 static void lnet_resend_msgs(void)
3077 struct lnet_msg *msg, *tmp;
3078 struct list_head resend;
3081 INIT_LIST_HEAD(&resend);
3083 spin_lock(&the_lnet.ln_msg_resend_lock);
3084 list_splice(&the_lnet.ln_msg_resend, &resend);
3085 spin_unlock(&the_lnet.ln_msg_resend_lock);
3087 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3088 list_del_init(&msg->msg_list);
3089 rc = lnet_send(msg->msg_src_nid_param, msg,
3090 msg->msg_rtr_nid_param);
3092 CNETERR("Error sending %s to %s: %d\n",
3093 lnet_msgtyp2str(msg->msg_type),
3094 libcfs_id2str(msg->msg_target), rc);
3095 lnet_finalize(msg, rc);
3100 /* The discovery thread. */
3101 static int lnet_peer_discovery(void *arg)
3103 struct lnet_peer *lp;
3106 CDEBUG(D_NET, "started\n");
3107 cfs_block_allsigs();
3110 if (lnet_peer_discovery_wait_for_work())
3115 if (lnet_push_target_resize_needed())
3116 lnet_push_target_resize();
3118 lnet_net_lock(LNET_LOCK_EX);
3119 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3123 * Process all incoming discovery work requests. When
3124 * discovery must wait on a peer to change state, it
3125 * is added to the tail of the ln_dc_working queue. A
3126 * timestamp keeps track of when the peer was added,
3127 * so we can time out discovery requests that take too
3130 while (!list_empty(&the_lnet.ln_dc_request)) {
3131 lp = list_first_entry(&the_lnet.ln_dc_request,
3132 struct lnet_peer, lp_dc_list);
3133 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3135 * set the time the peer was put on the dc_working
3136 * queue. It shouldn't remain on the queue
3137 * forever, in case the GET message (for ping)
3138 * doesn't get a REPLY or the PUT message (for
3139 * push) doesn't get an ACK.
3141 lp->lp_last_queued = ktime_get_real_seconds();
3142 lnet_net_unlock(LNET_LOCK_EX);
3145 * Select an action depending on the state of
3146 * the peer and whether discovery is disabled.
3147 * The check whether discovery is disabled is
3148 * done after the code that handles processing
3149 * for arrived data, cleanup for failures, and
3150 * forcing a Ping or Push.
3152 spin_lock(&lp->lp_lock);
3153 CDEBUG(D_NET, "peer %s state %#x\n",
3154 libcfs_nid2str(lp->lp_primary_nid),
3156 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3157 rc = lnet_peer_data_present(lp);
3158 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3159 rc = lnet_peer_ping_failed(lp);
3160 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3161 rc = lnet_peer_push_failed(lp);
3162 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3163 rc = lnet_peer_send_ping(lp);
3164 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3165 rc = lnet_peer_send_push(lp);
3166 else if (lnet_peer_discovery_disabled)
3167 rc = lnet_peer_rediscover(lp);
3168 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3169 rc = lnet_peer_send_ping(lp);
3170 else if (lnet_peer_needs_push(lp))
3171 rc = lnet_peer_send_push(lp);
3173 rc = lnet_peer_discovered(lp);
3174 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3175 libcfs_nid2str(lp->lp_primary_nid),
3177 spin_unlock(&lp->lp_lock);
3179 lnet_net_lock(LNET_LOCK_EX);
3180 if (rc == LNET_REDISCOVER_PEER) {
3181 list_move(&lp->lp_dc_list,
3182 &the_lnet.ln_dc_request);
3184 lnet_peer_discovery_error(lp, rc);
3186 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3187 lnet_peer_discovery_complete(lp);
3188 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3192 lnet_net_unlock(LNET_LOCK_EX);
3195 CDEBUG(D_NET, "stopping\n");
3197 * Clean up before telling lnet_peer_discovery_stop() that
3198 * we're done. Use wake_up() below to somewhat reduce the
3199 * size of the thundering herd if there are multiple threads
3200 * waiting on discovery of a single peer.
3203 /* Queue cleanup 1: stop all pending pings and pushes. */
3204 lnet_net_lock(LNET_LOCK_EX);
3205 while (!list_empty(&the_lnet.ln_dc_working)) {
3206 lp = list_first_entry(&the_lnet.ln_dc_working,
3207 struct lnet_peer, lp_dc_list);
3208 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3209 lnet_net_unlock(LNET_LOCK_EX);
3210 lnet_peer_cancel_discovery(lp);
3211 lnet_net_lock(LNET_LOCK_EX);
3213 lnet_net_unlock(LNET_LOCK_EX);
3215 /* Queue cleanup 2: wait for the expired queue to clear. */
3216 while (!list_empty(&the_lnet.ln_dc_expired))
3217 schedule_timeout(cfs_time_seconds(1));
3219 /* Queue cleanup 3: clear the request queue. */
3220 lnet_net_lock(LNET_LOCK_EX);
3221 while (!list_empty(&the_lnet.ln_dc_request)) {
3222 lp = list_first_entry(&the_lnet.ln_dc_request,
3223 struct lnet_peer, lp_dc_list);
3224 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3225 lnet_peer_discovery_complete(lp);
3227 lnet_net_unlock(LNET_LOCK_EX);
3229 LNetEQFree(the_lnet.ln_dc_eqh);
3230 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3232 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3233 wake_up(&the_lnet.ln_dc_waitq);
3235 CDEBUG(D_NET, "stopped\n");
3240 /* ln_api_mutex is held on entry. */
3241 int lnet_peer_discovery_start(void)
3243 struct task_struct *task;
3246 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3249 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3251 CERROR("Can't allocate discovery EQ: %d\n", rc);
3255 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3256 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3259 CERROR("Can't start peer discovery thread: %d\n", rc);
3261 LNetEQFree(the_lnet.ln_dc_eqh);
3262 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3264 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3267 CDEBUG(D_NET, "discovery start: %d\n", rc);
3272 /* ln_api_mutex is held on entry. */
3273 void lnet_peer_discovery_stop(void)
3275 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3278 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3279 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3280 wake_up(&the_lnet.ln_dc_waitq);
3282 wait_event(the_lnet.ln_dc_waitq,
3283 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3285 LASSERT(list_empty(&the_lnet.ln_dc_request));
3286 LASSERT(list_empty(&the_lnet.ln_dc_working));
3287 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3289 CDEBUG(D_NET, "discovery stopped\n");
3295 lnet_debug_peer(lnet_nid_t nid)
3297 char *aliveness = "NA";
3298 struct lnet_peer_ni *lp;
3301 cpt = lnet_cpt_of_nid(nid, NULL);
3304 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3306 lnet_net_unlock(cpt);
3307 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3311 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3312 aliveness = lp->lpni_alive ? "up" : "down";
3314 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3315 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3316 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3317 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3318 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3320 lnet_peer_ni_decref_locked(lp);
3322 lnet_net_unlock(cpt);
3325 /* Gathering information for userspace. */
3327 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3328 char aliveness[LNET_MAX_STR_LEN],
3329 __u32 *cpt_iter, __u32 *refcount,
3330 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3331 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3332 __u32 *peer_tx_qnob)
3334 struct lnet_peer_table *peer_table;
3335 struct lnet_peer_ni *lp;
3340 /* get the number of CPTs */
3341 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3343 /* if the cpt number to be examined is >= the number of cpts in
3344 * the system then indicate that there are no more cpts to examin
3346 if (*cpt_iter >= lncpt)
3349 /* get the current table */
3350 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3351 /* if the ptable is NULL then there are no more cpts to examine */
3352 if (peer_table == NULL)
3355 lnet_net_lock(*cpt_iter);
3357 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3358 struct list_head *peers = &peer_table->pt_hash[j];
3360 list_for_each_entry(lp, peers, lpni_hashlist) {
3361 if (peer_index-- > 0)
3364 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3365 if (lnet_isrouter(lp) ||
3366 lnet_peer_aliveness_enabled(lp))
3367 snprintf(aliveness, LNET_MAX_STR_LEN,
3368 lp->lpni_alive ? "up" : "down");
3370 *nid = lp->lpni_nid;
3371 *refcount = atomic_read(&lp->lpni_refcount);
3372 *ni_peer_tx_credits =
3373 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3374 *peer_tx_credits = lp->lpni_txcredits;
3375 *peer_rtr_credits = lp->lpni_rtrcredits;
3376 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3377 *peer_tx_qnob = lp->lpni_txqnob;
3383 lnet_net_unlock(*cpt_iter);
3387 return found ? 0 : -ENOENT;
3390 /* ln_api_mutex is held, which keeps the peer list stable */
3391 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3393 struct lnet_ioctl_element_stats *lpni_stats;
3394 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3395 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3396 struct lnet_peer_ni_credit_info *lpni_info;
3397 struct lnet_peer_ni *lpni;
3398 struct lnet_peer *lp;
3403 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3410 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3411 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3412 size *= lp->lp_nnis;
3413 if (size > cfg->prcfg_size) {
3414 cfg->prcfg_size = size;
3419 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3420 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3421 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3422 cfg->prcfg_count = lp->lp_nnis;
3423 cfg->prcfg_size = size;
3424 cfg->prcfg_state = lp->lp_state;
3426 /* Allocate helper buffers. */
3428 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3431 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3434 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3435 if (!lpni_msg_stats)
3436 goto out_free_stats;
3437 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3439 goto out_free_msg_stats;
3444 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3445 nid = lpni->lpni_nid;
3446 if (copy_to_user(bulk, &nid, sizeof(nid)))
3447 goto out_free_hstats;
3448 bulk += sizeof(nid);
3450 memset(lpni_info, 0, sizeof(*lpni_info));
3451 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3452 if (lnet_isrouter(lpni) ||
3453 lnet_peer_aliveness_enabled(lpni))
3454 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3455 lpni->lpni_alive ? "up" : "down");
3457 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3458 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3459 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3460 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3461 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3462 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3463 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3464 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3465 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3466 goto out_free_hstats;
3467 bulk += sizeof(*lpni_info);
3469 memset(lpni_stats, 0, sizeof(*lpni_stats));
3470 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3471 LNET_STATS_TYPE_SEND);
3472 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3473 LNET_STATS_TYPE_RECV);
3474 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3475 LNET_STATS_TYPE_DROP);
3476 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3477 goto out_free_hstats;
3478 bulk += sizeof(*lpni_stats);
3479 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3480 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3481 goto out_free_hstats;
3482 bulk += sizeof(*lpni_msg_stats);
3483 lpni_hstats->hlpni_network_timeout =
3484 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3485 lpni_hstats->hlpni_remote_dropped =
3486 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3487 lpni_hstats->hlpni_remote_timeout =
3488 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3489 lpni_hstats->hlpni_remote_error =
3490 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3491 lpni_hstats->hlpni_health_value =
3492 atomic_read(&lpni->lpni_healthv);
3493 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3494 goto out_free_hstats;
3495 bulk += sizeof(*lpni_hstats);
3500 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3502 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3504 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3506 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3508 lnet_peer_decref_locked(lp);
3514 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3516 /* the mt could've shutdown and cleaned up the queues */
3517 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3520 if (list_empty(&lpni->lpni_recovery) &&
3521 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3522 CERROR("lpni %s added to recovery queue. Health = %d\n",
3523 libcfs_nid2str(lpni->lpni_nid),
3524 atomic_read(&lpni->lpni_healthv));
3525 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3526 lnet_peer_ni_addref_locked(lpni);
3530 /* Call with the ln_api_mutex held */
3532 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3534 struct lnet_peer_table *ptable;
3535 struct lnet_peer *lp;
3536 struct lnet_peer_net *lpn;
3537 struct lnet_peer_ni *lpni;
3541 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3545 lnet_net_lock(LNET_LOCK_EX);
3546 lpni = lnet_find_peer_ni_locked(nid);
3548 lnet_net_unlock(LNET_LOCK_EX);
3551 atomic_set(&lpni->lpni_healthv, value);
3552 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3553 lnet_peer_ni_decref_locked(lpni);
3554 lnet_net_unlock(LNET_LOCK_EX);
3558 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3561 * Walk all the peers and reset the healhv for each one to the
3564 lnet_net_lock(LNET_LOCK_EX);
3565 for (cpt = 0; cpt < lncpt; cpt++) {
3566 ptable = the_lnet.ln_peer_tables[cpt];
3567 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3568 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3569 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3571 atomic_set(&lpni->lpni_healthv, value);
3572 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3577 lnet_net_unlock(LNET_LOCK_EX);