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);
172 spin_lock_init(&lpni->lpni_lock);
174 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
175 lpni->lpni_last_alive = ktime_get_seconds(); /* assumes alive */
176 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
177 lpni->lpni_nid = nid;
178 lpni->lpni_cpt = cpt;
179 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
180 lnet_set_peer_ni_health_locked(lpni, true);
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 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
301 spin_unlock(&the_lnet.ln_msg_resend_lock);
302 wake_up(&the_lnet.ln_dc_waitq);
304 LIBCFS_FREE(lp, sizeof(*lp));
308 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
309 * that peer_net, detach the peer_net from the peer.
311 * Call with lnet_net_lock/EX held
314 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
316 struct lnet_peer_table *ptable;
317 struct lnet_peer_net *lpn;
318 struct lnet_peer *lp;
321 * Belts and suspenders: gracefully handle teardown of a
322 * partially connected peer_ni.
324 lpn = lpni->lpni_peer_net;
326 list_del_init(&lpni->lpni_peer_nis);
328 * If there are no lpni's left, we detach lpn from
329 * lp_peer_nets, so it cannot be found anymore.
331 if (list_empty(&lpn->lpn_peer_nis))
332 list_del_init(&lpn->lpn_peer_nets);
334 /* Update peer NID count. */
339 * If there are no more peer nets, make the peer unfindable
340 * via the peer_tables.
342 * Otherwise, if the peer is DISCOVERED, tell discovery to
343 * take another look at it. This is a no-op if discovery for
344 * this peer did the detaching.
346 if (list_empty(&lp->lp_peer_nets)) {
347 list_del_init(&lp->lp_peer_list);
348 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
350 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
351 /* Discovery isn't running, nothing to do here. */
352 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
353 lnet_peer_queue_for_discovery(lp);
354 wake_up(&the_lnet.ln_dc_waitq);
356 CDEBUG(D_NET, "peer %s NID %s\n",
357 libcfs_nid2str(lp->lp_primary_nid),
358 libcfs_nid2str(lpni->lpni_nid));
361 /* called with lnet_net_lock LNET_LOCK_EX held */
363 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
365 struct lnet_peer_table *ptable = NULL;
367 /* don't remove a peer_ni if it's also a gateway */
368 if (lpni->lpni_rtr_refcount > 0) {
369 CERROR("Peer NI %s is a gateway. Can not delete it\n",
370 libcfs_nid2str(lpni->lpni_nid));
374 lnet_peer_remove_from_remote_list(lpni);
376 /* remove peer ni from the hash list. */
377 list_del_init(&lpni->lpni_hashlist);
380 * indicate the peer is being deleted so the monitor thread can
381 * remove it from the recovery queue.
383 spin_lock(&lpni->lpni_lock);
384 lpni->lpni_state |= LNET_PEER_NI_DELETING;
385 spin_unlock(&lpni->lpni_lock);
387 /* decrement the ref count on the peer table */
388 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
389 LASSERT(ptable->pt_number > 0);
393 * The peer_ni can no longer be found with a lookup. But there
394 * can be current users, so keep track of it on the zombie
395 * list until the reference count has gone to zero.
397 * The last reference may be lost in a place where the
398 * lnet_net_lock locks only a single cpt, and that cpt may not
399 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
402 spin_lock(&ptable->pt_zombie_lock);
403 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
404 ptable->pt_zombies++;
405 spin_unlock(&ptable->pt_zombie_lock);
407 /* no need to keep this peer_ni on the hierarchy anymore */
408 lnet_peer_detach_peer_ni_locked(lpni);
410 /* remove hashlist reference on peer_ni */
411 lnet_peer_ni_decref_locked(lpni);
416 void lnet_peer_uninit(void)
418 struct lnet_peer_ni *lpni, *tmp;
420 lnet_net_lock(LNET_LOCK_EX);
422 /* remove all peer_nis from the remote peer and the hash list */
423 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
424 lpni_on_remote_peer_ni_list)
425 lnet_peer_ni_del_locked(lpni);
427 lnet_peer_tables_destroy();
429 lnet_net_unlock(LNET_LOCK_EX);
433 lnet_peer_del_locked(struct lnet_peer *peer)
435 struct lnet_peer_ni *lpni = NULL, *lpni2;
438 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
440 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
441 while (lpni != NULL) {
442 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
443 rc = lnet_peer_ni_del_locked(lpni);
453 lnet_peer_del(struct lnet_peer *peer)
455 lnet_net_lock(LNET_LOCK_EX);
456 lnet_peer_del_locked(peer);
457 lnet_net_unlock(LNET_LOCK_EX);
463 * Delete a NID from a peer. Call with ln_api_mutex held.
466 * -EPERM: Non-DLC deletion from DLC-configured peer.
467 * -ENOENT: No lnet_peer_ni corresponding to the nid.
468 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
469 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
472 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
474 struct lnet_peer_ni *lpni;
475 lnet_nid_t primary_nid = lp->lp_primary_nid;
478 if (!(flags & LNET_PEER_CONFIGURED)) {
479 if (lp->lp_state & LNET_PEER_CONFIGURED) {
484 lpni = lnet_find_peer_ni_locked(nid);
489 lnet_peer_ni_decref_locked(lpni);
490 if (lp != lpni->lpni_peer_net->lpn_peer) {
496 * This function only allows deletion of the primary NID if it
499 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
504 lnet_net_lock(LNET_LOCK_EX);
505 lnet_peer_ni_del_locked(lpni);
506 lnet_net_unlock(LNET_LOCK_EX);
509 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
510 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
516 lnet_peer_table_cleanup_locked(struct lnet_net *net,
517 struct lnet_peer_table *ptable)
520 struct lnet_peer_ni *next;
521 struct lnet_peer_ni *lpni;
522 struct lnet_peer *peer;
524 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
525 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
527 if (net != NULL && net != lpni->lpni_net)
530 peer = lpni->lpni_peer_net->lpn_peer;
531 if (peer->lp_primary_nid != lpni->lpni_nid) {
532 lnet_peer_ni_del_locked(lpni);
536 * Removing the primary NID implies removing
537 * the entire peer. Advance next beyond any
538 * peer_ni that belongs to the same peer.
540 list_for_each_entry_from(next, &ptable->pt_hash[i],
542 if (next->lpni_peer_net->lpn_peer != peer)
545 lnet_peer_del_locked(peer);
551 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
555 spin_lock(&ptable->pt_zombie_lock);
556 while (ptable->pt_zombies) {
557 spin_unlock(&ptable->pt_zombie_lock);
559 if (is_power_of_2(i)) {
561 "Waiting for %d zombies on peer table\n",
564 set_current_state(TASK_UNINTERRUPTIBLE);
565 schedule_timeout(cfs_time_seconds(1) >> 1);
566 spin_lock(&ptable->pt_zombie_lock);
568 spin_unlock(&ptable->pt_zombie_lock);
572 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
573 struct lnet_peer_table *ptable)
575 struct lnet_peer_ni *lp;
576 struct lnet_peer_ni *tmp;
580 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
581 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
583 if (net != lp->lpni_net)
586 if (lp->lpni_rtr_refcount == 0)
589 lpni_nid = lp->lpni_nid;
591 lnet_net_unlock(LNET_LOCK_EX);
592 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
593 lnet_net_lock(LNET_LOCK_EX);
599 lnet_peer_tables_cleanup(struct lnet_net *net)
602 struct lnet_peer_table *ptable;
604 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
605 /* If just deleting the peers for a NI, get rid of any routes these
606 * peers are gateways for. */
607 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
608 lnet_net_lock(LNET_LOCK_EX);
609 lnet_peer_table_del_rtrs_locked(net, ptable);
610 lnet_net_unlock(LNET_LOCK_EX);
613 /* Start the cleanup process */
614 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
615 lnet_net_lock(LNET_LOCK_EX);
616 lnet_peer_table_cleanup_locked(net, ptable);
617 lnet_net_unlock(LNET_LOCK_EX);
620 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
621 lnet_peer_ni_finalize_wait(ptable);
624 static struct lnet_peer_ni *
625 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
627 struct list_head *peers;
628 struct lnet_peer_ni *lp;
630 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
632 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
633 list_for_each_entry(lp, peers, lpni_hashlist) {
634 if (lp->lpni_nid == nid) {
635 lnet_peer_ni_addref_locked(lp);
643 struct lnet_peer_ni *
644 lnet_find_peer_ni_locked(lnet_nid_t nid)
646 struct lnet_peer_ni *lpni;
647 struct lnet_peer_table *ptable;
650 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
652 ptable = the_lnet.ln_peer_tables[cpt];
653 lpni = lnet_get_peer_ni_locked(ptable, nid);
659 lnet_find_peer(lnet_nid_t nid)
661 struct lnet_peer_ni *lpni;
662 struct lnet_peer *lp = NULL;
665 cpt = lnet_net_lock_current();
666 lpni = lnet_find_peer_ni_locked(nid);
668 lp = lpni->lpni_peer_net->lpn_peer;
669 lnet_peer_addref_locked(lp);
670 lnet_peer_ni_decref_locked(lpni);
672 lnet_net_unlock(cpt);
677 struct lnet_peer_ni *
678 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
679 struct lnet_peer_net *peer_net,
680 struct lnet_peer_ni *prev)
682 struct lnet_peer_ni *lpni;
683 struct lnet_peer_net *net = peer_net;
687 if (list_empty(&peer->lp_peer_nets))
690 net = list_entry(peer->lp_peer_nets.next,
691 struct lnet_peer_net,
694 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
700 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
702 * if you reached the end of the peer ni list and the peer
703 * net is specified then there are no more peer nis in that
710 * we reached the end of this net ni list. move to the
713 if (prev->lpni_peer_net->lpn_peer_nets.next ==
715 /* no more nets and no more NIs. */
718 /* get the next net */
719 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
720 struct lnet_peer_net,
722 /* get the ni on it */
723 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
729 /* there are more nis left */
730 lpni = list_entry(prev->lpni_peer_nis.next,
731 struct lnet_peer_ni, lpni_peer_nis);
736 /* Call with the ln_api_mutex held */
737 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
739 struct lnet_process_id id;
740 struct lnet_peer_table *ptable;
741 struct lnet_peer *lp;
750 if (the_lnet.ln_state != LNET_STATE_RUNNING)
753 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
756 * Count the number of peers, and return E2BIG if the buffer
757 * is too small. We'll also return the desired size.
760 for (cpt = 0; cpt < lncpt; cpt++) {
761 ptable = the_lnet.ln_peer_tables[cpt];
762 count += ptable->pt_peers;
764 size = count * sizeof(*ids);
769 * Walk the peer lists and copy out the primary nids.
770 * This is safe because the peer lists are only modified
771 * while the ln_api_mutex is held. So we don't need to
772 * hold the lnet_net_lock as well, and can therefore
773 * directly call copy_to_user().
776 memset(&id, 0, sizeof(id));
777 id.pid = LNET_PID_LUSTRE;
779 for (cpt = 0; cpt < lncpt; cpt++) {
780 ptable = the_lnet.ln_peer_tables[cpt];
781 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
784 id.nid = lp->lp_primary_nid;
785 if (copy_to_user(&ids[i], &id, sizeof(id)))
798 * Start pushes to peers that need to be updated for a configuration
799 * change on this node.
802 lnet_push_update_to_peers(int force)
804 struct lnet_peer_table *ptable;
805 struct lnet_peer *lp;
809 lnet_net_lock(LNET_LOCK_EX);
810 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
811 for (cpt = 0; cpt < lncpt; cpt++) {
812 ptable = the_lnet.ln_peer_tables[cpt];
813 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
815 spin_lock(&lp->lp_lock);
816 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
817 lp->lp_state |= LNET_PEER_FORCE_PUSH;
818 spin_unlock(&lp->lp_lock);
820 if (lnet_peer_needs_push(lp))
821 lnet_peer_queue_for_discovery(lp);
824 lnet_net_unlock(LNET_LOCK_EX);
825 wake_up(&the_lnet.ln_dc_waitq);
829 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
830 * this is a preferred point-to-point path. Call with lnet_net_lock in
834 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
838 if (lpni->lpni_pref_nnids == 0)
840 if (lpni->lpni_pref_nnids == 1)
841 return lpni->lpni_pref.nid == nid;
842 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
843 if (lpni->lpni_pref.nids[i] == nid)
850 * Set a single ni as preferred, provided no preferred ni is already
851 * defined. Only to be used for non-multi-rail peer_ni.
854 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
858 spin_lock(&lpni->lpni_lock);
859 if (nid == LNET_NID_ANY) {
861 } else if (lpni->lpni_pref_nnids > 0) {
863 } else if (lpni->lpni_pref_nnids == 0) {
864 lpni->lpni_pref.nid = nid;
865 lpni->lpni_pref_nnids = 1;
866 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
868 spin_unlock(&lpni->lpni_lock);
870 CDEBUG(D_NET, "peer %s nid %s: %d\n",
871 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
876 * Clear the preferred NID from a non-multi-rail peer_ni, provided
877 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
880 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
884 spin_lock(&lpni->lpni_lock);
885 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
886 lpni->lpni_pref_nnids = 0;
887 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
888 } else if (lpni->lpni_pref_nnids == 0) {
893 spin_unlock(&lpni->lpni_lock);
895 CDEBUG(D_NET, "peer %s: %d\n",
896 libcfs_nid2str(lpni->lpni_nid), rc);
901 * Clear the preferred NIDs from a non-multi-rail peer.
904 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
906 struct lnet_peer_ni *lpni = NULL;
908 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
909 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
913 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
915 lnet_nid_t *nids = NULL;
916 lnet_nid_t *oldnids = NULL;
917 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
922 if (nid == LNET_NID_ANY) {
927 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
932 /* A non-MR node may have only one preferred NI per peer_ni */
933 if (lpni->lpni_pref_nnids > 0) {
934 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
940 if (lpni->lpni_pref_nnids != 0) {
941 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
942 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
947 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
948 if (lpni->lpni_pref.nids[i] == nid) {
949 LIBCFS_FREE(nids, size);
953 nids[i] = lpni->lpni_pref.nids[i];
958 lnet_net_lock(LNET_LOCK_EX);
959 spin_lock(&lpni->lpni_lock);
960 if (lpni->lpni_pref_nnids == 0) {
961 lpni->lpni_pref.nid = nid;
963 oldnids = lpni->lpni_pref.nids;
964 lpni->lpni_pref.nids = nids;
966 lpni->lpni_pref_nnids++;
967 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
968 spin_unlock(&lpni->lpni_lock);
969 lnet_net_unlock(LNET_LOCK_EX);
972 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
973 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
976 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
977 spin_lock(&lpni->lpni_lock);
978 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
979 spin_unlock(&lpni->lpni_lock);
981 CDEBUG(D_NET, "peer %s nid %s: %d\n",
982 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
987 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
989 lnet_nid_t *nids = NULL;
990 lnet_nid_t *oldnids = NULL;
991 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
996 if (lpni->lpni_pref_nnids == 0) {
1001 if (lpni->lpni_pref_nnids == 1) {
1002 if (lpni->lpni_pref.nid != nid) {
1006 } else if (lpni->lpni_pref_nnids == 2) {
1007 if (lpni->lpni_pref.nids[0] != nid &&
1008 lpni->lpni_pref.nids[1] != nid) {
1013 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1014 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1019 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1020 if (lpni->lpni_pref.nids[i] != nid)
1022 nids[j++] = lpni->lpni_pref.nids[i];
1024 /* Check if we actually removed a nid. */
1025 if (j == lpni->lpni_pref_nnids) {
1026 LIBCFS_FREE(nids, size);
1032 lnet_net_lock(LNET_LOCK_EX);
1033 spin_lock(&lpni->lpni_lock);
1034 if (lpni->lpni_pref_nnids == 1) {
1035 lpni->lpni_pref.nid = LNET_NID_ANY;
1036 } else if (lpni->lpni_pref_nnids == 2) {
1037 oldnids = lpni->lpni_pref.nids;
1038 if (oldnids[0] == nid)
1039 lpni->lpni_pref.nid = oldnids[1];
1041 lpni->lpni_pref.nid = oldnids[2];
1043 oldnids = lpni->lpni_pref.nids;
1044 lpni->lpni_pref.nids = nids;
1046 lpni->lpni_pref_nnids--;
1047 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1048 spin_unlock(&lpni->lpni_lock);
1049 lnet_net_unlock(LNET_LOCK_EX);
1052 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1053 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1056 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1057 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1062 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1064 struct lnet_peer_ni *lpni;
1065 lnet_nid_t primary_nid = nid;
1067 lpni = lnet_find_peer_ni_locked(nid);
1069 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1070 lnet_peer_ni_decref_locked(lpni);
1077 LNetPrimaryNID(lnet_nid_t nid)
1079 struct lnet_peer *lp;
1080 struct lnet_peer_ni *lpni;
1081 lnet_nid_t primary_nid = nid;
1085 cpt = lnet_net_lock_current();
1086 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1091 lp = lpni->lpni_peer_net->lpn_peer;
1092 while (!lnet_peer_is_uptodate(lp)) {
1093 rc = lnet_discover_peer_locked(lpni, cpt, true);
1096 lp = lpni->lpni_peer_net->lpn_peer;
1098 primary_nid = lp->lp_primary_nid;
1100 lnet_peer_ni_decref_locked(lpni);
1102 lnet_net_unlock(cpt);
1104 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1105 libcfs_nid2str(primary_nid), rc);
1108 EXPORT_SYMBOL(LNetPrimaryNID);
1110 struct lnet_peer_net *
1111 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1113 struct lnet_peer_net *peer_net;
1114 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1115 if (peer_net->lpn_net_id == net_id)
1122 * Attach a peer_ni to a peer_net and peer. This function assumes
1123 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1124 * may be attached to a different peer, in which case it will be
1125 * properly detached first. The whole operation is done atomically.
1127 * Always returns 0. This is the last function called from functions
1128 * that do return an int, so returning 0 here allows the compiler to
1132 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1133 struct lnet_peer_net *lpn,
1134 struct lnet_peer_ni *lpni,
1137 struct lnet_peer_table *ptable;
1139 /* Install the new peer_ni */
1140 lnet_net_lock(LNET_LOCK_EX);
1141 /* Add peer_ni to global peer table hash, if necessary. */
1142 if (list_empty(&lpni->lpni_hashlist)) {
1143 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1145 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1146 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1147 ptable->pt_version++;
1148 ptable->pt_number++;
1149 /* This is the 1st refcount on lpni. */
1150 atomic_inc(&lpni->lpni_refcount);
1153 /* Detach the peer_ni from an existing peer, if necessary. */
1154 if (lpni->lpni_peer_net) {
1155 LASSERT(lpni->lpni_peer_net != lpn);
1156 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1157 lnet_peer_detach_peer_ni_locked(lpni);
1158 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1159 lpni->lpni_peer_net = NULL;
1162 /* Add peer_ni to peer_net */
1163 lpni->lpni_peer_net = lpn;
1164 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1165 lnet_peer_net_addref_locked(lpn);
1167 /* Add peer_net to peer */
1168 if (!lpn->lpn_peer) {
1170 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1171 lnet_peer_addref_locked(lp);
1174 /* Add peer to global peer list, if necessary */
1175 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1176 if (list_empty(&lp->lp_peer_list)) {
1177 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1182 /* Update peer state */
1183 spin_lock(&lp->lp_lock);
1184 if (flags & LNET_PEER_CONFIGURED) {
1185 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1186 lp->lp_state |= LNET_PEER_CONFIGURED;
1188 if (flags & LNET_PEER_MULTI_RAIL) {
1189 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1190 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1191 lnet_peer_clr_non_mr_pref_nids(lp);
1194 spin_unlock(&lp->lp_lock);
1197 lnet_net_unlock(LNET_LOCK_EX);
1199 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1200 libcfs_nid2str(lp->lp_primary_nid),
1201 libcfs_nid2str(lpni->lpni_nid), flags);
1207 * Create a new peer, with nid as its primary nid.
1209 * Call with the lnet_api_mutex held.
1212 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1214 struct lnet_peer *lp;
1215 struct lnet_peer_net *lpn;
1216 struct lnet_peer_ni *lpni;
1219 LASSERT(nid != LNET_NID_ANY);
1222 * No need for the lnet_net_lock here, because the
1223 * lnet_api_mutex is held.
1225 lpni = lnet_find_peer_ni_locked(nid);
1227 /* A peer with this NID already exists. */
1228 lp = lpni->lpni_peer_net->lpn_peer;
1229 lnet_peer_ni_decref_locked(lpni);
1231 * This is an error if the peer was configured and the
1232 * primary NID differs or an attempt is made to change
1233 * the Multi-Rail flag. Otherwise the assumption is
1234 * that an existing peer is being modified.
1236 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1237 if (lp->lp_primary_nid != nid)
1239 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1243 /* Delete and recreate as a configured peer. */
1247 /* Create peer, peer_net, and peer_ni. */
1249 lp = lnet_peer_alloc(nid);
1252 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1255 lpni = lnet_peer_ni_alloc(nid);
1259 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1262 LIBCFS_FREE(lpn, sizeof(*lpn));
1264 LIBCFS_FREE(lp, sizeof(*lp));
1266 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1267 libcfs_nid2str(nid), flags, rc);
1272 * Add a NID to a peer. Call with ln_api_mutex held.
1275 * -EPERM: Non-DLC addition to a DLC-configured peer.
1276 * -EEXIST: The NID was configured by DLC for a different peer.
1277 * -ENOMEM: Out of memory.
1278 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1279 * non-multi-rail peer.
1282 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1284 struct lnet_peer_net *lpn;
1285 struct lnet_peer_ni *lpni;
1289 LASSERT(nid != LNET_NID_ANY);
1291 /* A configured peer can only be updated through configuration. */
1292 if (!(flags & LNET_PEER_CONFIGURED)) {
1293 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1300 * The MULTI_RAIL flag can be set but not cleared, because
1301 * that would leave the peer struct in an invalid state.
1303 if (flags & LNET_PEER_MULTI_RAIL) {
1304 spin_lock(&lp->lp_lock);
1305 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1306 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1307 lnet_peer_clr_non_mr_pref_nids(lp);
1309 spin_unlock(&lp->lp_lock);
1310 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1315 lpni = lnet_find_peer_ni_locked(nid);
1318 * A peer_ni already exists. This is only a problem if
1319 * it is not connected to this peer and was configured
1322 lnet_peer_ni_decref_locked(lpni);
1323 if (lpni->lpni_peer_net->lpn_peer == lp)
1325 if (lnet_peer_ni_is_configured(lpni)) {
1329 /* If this is the primary NID, destroy the peer. */
1330 if (lnet_peer_ni_is_primary(lpni)) {
1331 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1332 lpni = lnet_peer_ni_alloc(nid);
1339 lpni = lnet_peer_ni_alloc(nid);
1347 * Get the peer_net. Check that we're not adding a second
1348 * peer_ni on a peer_net of a non-multi-rail peer.
1350 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1352 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1357 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1362 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1365 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1366 if (!lpni->lpni_peer_net)
1367 LIBCFS_FREE(lpni, sizeof(*lpni));
1369 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1370 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1376 * Update the primary NID of a peer, if possible.
1378 * Call with the lnet_api_mutex held.
1381 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1383 lnet_nid_t old = lp->lp_primary_nid;
1386 if (lp->lp_primary_nid == nid)
1388 rc = lnet_peer_add_nid(lp, nid, flags);
1391 lp->lp_primary_nid = nid;
1393 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1394 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1399 * lpni creation initiated due to traffic either sending or receiving.
1402 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1404 struct lnet_peer *lp;
1405 struct lnet_peer_net *lpn;
1406 struct lnet_peer_ni *lpni;
1410 if (nid == LNET_NID_ANY) {
1415 /* lnet_net_lock is not needed here because ln_api_lock is held */
1416 lpni = lnet_find_peer_ni_locked(nid);
1419 * We must have raced with another thread. Since we
1420 * know next to nothing about a peer_ni created by
1421 * traffic, we just assume everything is ok and
1424 lnet_peer_ni_decref_locked(lpni);
1428 /* Create peer, peer_net, and peer_ni. */
1430 lp = lnet_peer_alloc(nid);
1433 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1436 lpni = lnet_peer_ni_alloc(nid);
1439 if (pref != LNET_NID_ANY)
1440 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1442 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1445 LIBCFS_FREE(lpn, sizeof(*lpn));
1447 LIBCFS_FREE(lp, sizeof(*lp));
1449 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1454 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1456 * This API handles the following combinations:
1457 * Create a peer with its primary NI if only the prim_nid is provided
1458 * Add a NID to a peer identified by the prim_nid. The peer identified
1459 * by the prim_nid must already exist.
1460 * The peer being created may be non-MR.
1462 * The caller must hold ln_api_mutex. This prevents the peer from
1463 * being created/modified/deleted by a different thread.
1466 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1468 struct lnet_peer *lp = NULL;
1469 struct lnet_peer_ni *lpni;
1472 /* The prim_nid must always be specified */
1473 if (prim_nid == LNET_NID_ANY)
1476 flags = LNET_PEER_CONFIGURED;
1478 flags |= LNET_PEER_MULTI_RAIL;
1481 * If nid isn't specified, we must create a new peer with
1482 * prim_nid as its primary nid.
1484 if (nid == LNET_NID_ANY)
1485 return lnet_peer_add(prim_nid, flags);
1487 /* Look up the prim_nid, which must exist. */
1488 lpni = lnet_find_peer_ni_locked(prim_nid);
1491 lnet_peer_ni_decref_locked(lpni);
1492 lp = lpni->lpni_peer_net->lpn_peer;
1494 /* Peer must have been configured. */
1495 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1496 CDEBUG(D_NET, "peer %s was not configured\n",
1497 libcfs_nid2str(prim_nid));
1501 /* Primary NID must match */
1502 if (lp->lp_primary_nid != prim_nid) {
1503 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1504 libcfs_nid2str(prim_nid),
1505 libcfs_nid2str(lp->lp_primary_nid));
1509 /* Multi-Rail flag must match. */
1510 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1511 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1512 libcfs_nid2str(prim_nid));
1516 return lnet_peer_add_nid(lp, nid, flags);
1520 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1522 * This API handles the following combinations:
1523 * Delete a NI from a peer if both prim_nid and nid are provided.
1524 * Delete a peer if only prim_nid is provided.
1525 * Delete a peer if its primary nid is provided.
1527 * The caller must hold ln_api_mutex. This prevents the peer from
1528 * being modified/deleted by a different thread.
1531 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1533 struct lnet_peer *lp;
1534 struct lnet_peer_ni *lpni;
1537 if (prim_nid == LNET_NID_ANY)
1540 lpni = lnet_find_peer_ni_locked(prim_nid);
1543 lnet_peer_ni_decref_locked(lpni);
1544 lp = lpni->lpni_peer_net->lpn_peer;
1546 if (prim_nid != lp->lp_primary_nid) {
1547 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1548 libcfs_nid2str(prim_nid),
1549 libcfs_nid2str(lp->lp_primary_nid));
1553 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1554 return lnet_peer_del(lp);
1556 flags = LNET_PEER_CONFIGURED;
1557 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1558 flags |= LNET_PEER_MULTI_RAIL;
1560 return lnet_peer_del_nid(lp, nid, flags);
1564 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1566 struct lnet_peer_table *ptable;
1567 struct lnet_peer_net *lpn;
1569 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1571 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1572 LASSERT(lpni->lpni_rtr_refcount == 0);
1573 LASSERT(list_empty(&lpni->lpni_txq));
1574 LASSERT(lpni->lpni_txqnob == 0);
1575 LASSERT(list_empty(&lpni->lpni_peer_nis));
1576 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1578 lpn = lpni->lpni_peer_net;
1579 lpni->lpni_peer_net = NULL;
1580 lpni->lpni_net = NULL;
1582 /* remove the peer ni from the zombie list */
1583 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1584 spin_lock(&ptable->pt_zombie_lock);
1585 list_del_init(&lpni->lpni_hashlist);
1586 ptable->pt_zombies--;
1587 spin_unlock(&ptable->pt_zombie_lock);
1589 if (lpni->lpni_pref_nnids > 1) {
1590 LIBCFS_FREE(lpni->lpni_pref.nids,
1591 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1593 LIBCFS_FREE(lpni, sizeof(*lpni));
1595 lnet_peer_net_decref_locked(lpn);
1598 struct lnet_peer_ni *
1599 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1601 struct lnet_peer_ni *lpni = NULL;
1604 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1605 return ERR_PTR(-ESHUTDOWN);
1608 * find if a peer_ni already exists.
1609 * If so then just return that.
1611 lpni = lnet_find_peer_ni_locked(nid);
1615 lnet_net_unlock(cpt);
1617 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1620 goto out_net_relock;
1623 lpni = lnet_find_peer_ni_locked(nid);
1633 * Get a peer_ni for the given nid, create it if necessary. Takes a
1634 * hold on the peer_ni.
1636 struct lnet_peer_ni *
1637 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1639 struct lnet_peer_ni *lpni = NULL;
1642 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1643 return ERR_PTR(-ESHUTDOWN);
1646 * find if a peer_ni already exists.
1647 * If so then just return that.
1649 lpni = lnet_find_peer_ni_locked(nid);
1655 * use the lnet_api_mutex to serialize the creation of the peer_ni
1656 * and the creation/deletion of the local ni/net. When a local ni is
1657 * created, if there exists a set of peer_nis on that network,
1658 * they need to be traversed and updated. When a local NI is
1659 * deleted, which could result in a network being deleted, then
1660 * all peer nis on that network need to be removed as well.
1662 * Creation through traffic should also be serialized with
1663 * creation through DLC.
1665 lnet_net_unlock(cpt);
1666 mutex_lock(&the_lnet.ln_api_mutex);
1668 * Shutdown is only set under the ln_api_lock, so a single
1669 * check here is sufficent.
1671 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1672 lpni = ERR_PTR(-ESHUTDOWN);
1673 goto out_mutex_unlock;
1676 rc = lnet_peer_ni_traffic_add(nid, pref);
1679 goto out_mutex_unlock;
1682 lpni = lnet_find_peer_ni_locked(nid);
1686 mutex_unlock(&the_lnet.ln_api_mutex);
1689 /* Lock has been dropped, check again for shutdown. */
1690 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1692 lnet_peer_ni_decref_locked(lpni);
1693 lpni = ERR_PTR(-ESHUTDOWN);
1704 * Is a peer uptodate from the point of view of discovery?
1706 * If it is currently being processed, obviously not.
1707 * A forced Ping or Push is also handled by the discovery thread.
1709 * Otherwise look at whether the peer needs rediscovering.
1712 lnet_peer_is_uptodate(struct lnet_peer *lp)
1716 spin_lock(&lp->lp_lock);
1717 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1718 LNET_PEER_FORCE_PING |
1719 LNET_PEER_FORCE_PUSH)) {
1721 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1723 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1724 if (lnet_peer_discovery_disabled)
1728 } else if (lnet_peer_needs_push(lp)) {
1730 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1731 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1738 spin_unlock(&lp->lp_lock);
1744 * Queue a peer for the attention of the discovery thread. Call with
1745 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1746 * -EALREADY if the peer was already queued.
1748 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1752 spin_lock(&lp->lp_lock);
1753 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1754 lp->lp_state |= LNET_PEER_DISCOVERING;
1755 spin_unlock(&lp->lp_lock);
1756 if (list_empty(&lp->lp_dc_list)) {
1757 lnet_peer_addref_locked(lp);
1758 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1759 wake_up(&the_lnet.ln_dc_waitq);
1765 CDEBUG(D_NET, "Queue peer %s: %d\n",
1766 libcfs_nid2str(lp->lp_primary_nid), rc);
1772 * Discovery of a peer is complete. Wake all waiters on the peer.
1773 * Call with lnet_net_lock/EX held.
1775 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1777 struct lnet_msg *msg, *tmp;
1779 struct list_head pending_msgs;
1781 INIT_LIST_HEAD(&pending_msgs);
1783 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1784 libcfs_nid2str(lp->lp_primary_nid));
1786 list_del_init(&lp->lp_dc_list);
1787 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1788 wake_up_all(&lp->lp_dc_waitq);
1790 lnet_net_unlock(LNET_LOCK_EX);
1792 /* iterate through all pending messages and send them again */
1793 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1794 list_del_init(&msg->msg_list);
1795 if (lp->lp_dc_error) {
1796 lnet_finalize(msg, lp->lp_dc_error);
1800 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1801 lnet_msgtyp2str(msg->msg_type),
1802 libcfs_id2str(msg->msg_target));
1803 rc = lnet_send(msg->msg_src_nid_param, msg,
1804 msg->msg_rtr_nid_param);
1806 CNETERR("Error sending %s to %s: %d\n",
1807 lnet_msgtyp2str(msg->msg_type),
1808 libcfs_id2str(msg->msg_target), rc);
1809 lnet_finalize(msg, rc);
1812 lnet_net_lock(LNET_LOCK_EX);
1813 lnet_peer_decref_locked(lp);
1817 * Handle inbound push.
1818 * Like any event handler, called with lnet_res_lock/CPT held.
1820 void lnet_peer_push_event(struct lnet_event *ev)
1822 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1823 struct lnet_peer *lp;
1825 /* lnet_find_peer() adds a refcount */
1826 lp = lnet_find_peer(ev->source.nid);
1828 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1829 libcfs_nid2str(ev->initiator.nid),
1830 libcfs_nid2str(ev->source.nid));
1834 /* Ensure peer state remains consistent while we modify it. */
1835 spin_lock(&lp->lp_lock);
1838 * If some kind of error happened the contents of the message
1839 * cannot be used. Clear the NIDS_UPTODATE and set the
1840 * FORCE_PING flag to trigger a ping.
1843 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1844 lp->lp_state |= LNET_PEER_FORCE_PING;
1845 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1847 libcfs_nid2str(lp->lp_primary_nid),
1848 libcfs_nid2str(ev->source.nid));
1853 * A push with invalid or corrupted info. Clear the UPTODATE
1854 * flag to trigger a ping.
1856 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1857 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1858 lp->lp_state |= LNET_PEER_FORCE_PING;
1859 CDEBUG(D_NET, "Corrupted Push from %s\n",
1860 libcfs_nid2str(lp->lp_primary_nid));
1865 * Make sure we'll allocate the correct size ping buffer when
1868 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1869 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1872 * A non-Multi-Rail peer is not supposed to be capable of
1875 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1876 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1877 libcfs_nid2str(lp->lp_primary_nid));
1882 * Check the MULTIRAIL flag. Complain if the peer was DLC
1883 * configured without it.
1885 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1886 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1887 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1888 libcfs_nid2str(lp->lp_primary_nid));
1890 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1891 lnet_peer_clr_non_mr_pref_nids(lp);
1896 * The peer may have discovery disabled at its end. Set
1897 * NO_DISCOVERY as appropriate.
1899 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1900 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1901 libcfs_nid2str(lp->lp_primary_nid));
1902 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1903 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1904 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1905 libcfs_nid2str(lp->lp_primary_nid));
1906 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1910 * Check for truncation of the Put message. Clear the
1911 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1912 * and tell discovery to allocate a bigger buffer.
1914 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1915 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1916 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1917 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1918 lp->lp_state |= LNET_PEER_FORCE_PING;
1919 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1920 libcfs_nid2str(lp->lp_primary_nid),
1921 pbuf->pb_info.pi_nnis);
1926 * Check whether the Put data is stale. Stale data can just be
1929 if (pbuf->pb_info.pi_nnis > 1 &&
1930 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1931 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1932 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1933 libcfs_nid2str(lp->lp_primary_nid),
1934 LNET_PING_BUFFER_SEQNO(pbuf),
1940 * Check whether the Put data is new, in which case we clear
1941 * the UPTODATE flag and prepare to process it.
1943 * If the Put data is current, and the peer is UPTODATE then
1944 * we assome everything is all right and drop the data as
1947 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1948 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1949 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1950 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1951 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1952 libcfs_nid2str(lp->lp_primary_nid),
1953 LNET_PING_BUFFER_SEQNO(pbuf),
1959 * If there is data present that hasn't been processed yet,
1960 * we'll replace it if the Put contained newer data and it
1961 * fits. We're racing with a Ping or earlier Push in this
1964 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1965 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1966 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1967 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1968 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1969 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1970 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1971 libcfs_nid2str(lp->lp_primary_nid),
1972 LNET_PING_BUFFER_SEQNO(pbuf),
1973 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1979 * Allocate a buffer to copy the data. On a failure we drop
1980 * the Push and set FORCE_PING to force the discovery
1981 * thread to fix the problem by pinging the peer.
1983 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
1985 lp->lp_state |= LNET_PEER_FORCE_PING;
1986 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
1987 libcfs_nid2str(lp->lp_primary_nid),
1988 LNET_PING_BUFFER_SEQNO(pbuf));
1993 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1994 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1995 lp->lp_state |= LNET_PEER_DATA_PRESENT;
1996 CDEBUG(D_NET, "Received Push %s %u\n",
1997 libcfs_nid2str(lp->lp_primary_nid),
1998 LNET_PING_BUFFER_SEQNO(pbuf));
2002 * Queue the peer for discovery if not done, force it on the request
2003 * queue and wake the discovery thread if the peer was already queued,
2004 * because its status changed.
2006 spin_unlock(&lp->lp_lock);
2007 lnet_net_lock(LNET_LOCK_EX);
2008 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2009 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2010 wake_up(&the_lnet.ln_dc_waitq);
2012 /* Drop refcount from lookup */
2013 lnet_peer_decref_locked(lp);
2014 lnet_net_unlock(LNET_LOCK_EX);
2018 * Clear the discovery error state, unless we're already discovering
2019 * this peer, in which case the error is current.
2021 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2023 spin_lock(&lp->lp_lock);
2024 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2025 lp->lp_dc_error = 0;
2026 spin_unlock(&lp->lp_lock);
2030 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2031 * dropped/retaken within this function. An lnet_peer_ni is passed in
2032 * because discovery could tear down an lnet_peer.
2035 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2038 struct lnet_peer *lp;
2042 lnet_net_unlock(cpt);
2043 lnet_net_lock(LNET_LOCK_EX);
2044 lp = lpni->lpni_peer_net->lpn_peer;
2045 lnet_peer_clear_discovery_error(lp);
2048 * We're willing to be interrupted. The lpni can become a
2049 * zombie if we race with DLC, so we must check for that.
2052 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2053 if (signal_pending(current))
2055 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2057 if (lp->lp_dc_error)
2059 if (lnet_peer_is_uptodate(lp))
2061 lnet_peer_queue_for_discovery(lp);
2063 * if caller requested a non-blocking operation then
2064 * return immediately. Once discovery is complete then the
2065 * peer ref will be decremented and any pending messages
2066 * that were stopped due to discovery will be transmitted.
2071 lnet_peer_addref_locked(lp);
2072 lnet_net_unlock(LNET_LOCK_EX);
2074 finish_wait(&lp->lp_dc_waitq, &wait);
2075 lnet_net_lock(LNET_LOCK_EX);
2076 lnet_peer_decref_locked(lp);
2077 /* Peer may have changed */
2078 lp = lpni->lpni_peer_net->lpn_peer;
2080 finish_wait(&lp->lp_dc_waitq, &wait);
2082 lnet_net_unlock(LNET_LOCK_EX);
2086 * If the peer has changed after we've discovered the older peer,
2087 * then we need to discovery the new peer to make sure the
2088 * interface information is up to date
2090 if (lp != lpni->lpni_peer_net->lpn_peer)
2093 if (signal_pending(current))
2095 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2097 else if (lp->lp_dc_error)
2098 rc = lp->lp_dc_error;
2100 CDEBUG(D_NET, "non-blocking discovery\n");
2101 else if (!lnet_peer_is_uptodate(lp))
2104 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2105 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2106 libcfs_nid2str(lpni->lpni_nid), rc,
2107 (!block) ? "pending discovery" : "discovery complete");
2112 /* Handle an incoming ack for a push. */
2114 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2116 struct lnet_ping_buffer *pbuf;
2118 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2119 spin_lock(&lp->lp_lock);
2120 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2121 lp->lp_push_error = ev->status;
2123 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2125 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2126 spin_unlock(&lp->lp_lock);
2128 CDEBUG(D_NET, "peer %s ev->status %d\n",
2129 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2132 /* Handle a Reply message. This is the reply to a Ping message. */
2134 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2136 struct lnet_ping_buffer *pbuf;
2139 spin_lock(&lp->lp_lock);
2142 * If some kind of error happened the contents of message
2143 * cannot be used. Set PING_FAILED to trigger a retry.
2146 lp->lp_state |= LNET_PEER_PING_FAILED;
2147 lp->lp_ping_error = ev->status;
2148 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2150 libcfs_nid2str(lp->lp_primary_nid),
2151 libcfs_nid2str(ev->source.nid));
2155 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2156 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2157 lnet_swap_pinginfo(pbuf);
2160 * A reply with invalid or corrupted info. Set PING_FAILED to
2163 rc = lnet_ping_info_validate(&pbuf->pb_info);
2165 lp->lp_state |= LNET_PEER_PING_FAILED;
2166 lp->lp_ping_error = 0;
2167 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2168 libcfs_nid2str(lp->lp_primary_nid), rc);
2173 * Update the MULTI_RAIL flag based on the reply. If the peer
2174 * was configured with DLC then the setting should match what
2177 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2178 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2179 /* Everything's fine */
2180 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2181 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2182 libcfs_nid2str(lp->lp_primary_nid));
2184 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2185 lnet_peer_clr_non_mr_pref_nids(lp);
2187 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2188 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2189 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2190 libcfs_nid2str(lp->lp_primary_nid));
2192 CERROR("Multi-Rail state vanished from %s\n",
2193 libcfs_nid2str(lp->lp_primary_nid));
2194 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2199 * Make sure we'll allocate the correct size ping buffer when
2202 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2203 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2206 * The peer may have discovery disabled at its end. Set
2207 * NO_DISCOVERY as appropriate.
2209 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2210 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2211 libcfs_nid2str(lp->lp_primary_nid));
2212 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2213 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2214 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2215 libcfs_nid2str(lp->lp_primary_nid));
2216 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2220 * Check for truncation of the Reply. Clear PING_SENT and set
2221 * PING_FAILED to trigger a retry.
2223 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2224 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2225 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2226 lp->lp_state |= LNET_PEER_PING_FAILED;
2227 lp->lp_ping_error = 0;
2228 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2229 libcfs_nid2str(lp->lp_primary_nid),
2230 pbuf->pb_info.pi_nnis);
2235 * Check the sequence numbers in the reply. These are only
2236 * available if the reply came from a Multi-Rail peer.
2238 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2239 pbuf->pb_info.pi_nnis > 1 &&
2240 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2241 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2242 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2243 libcfs_nid2str(lp->lp_primary_nid),
2244 LNET_PING_BUFFER_SEQNO(pbuf),
2249 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2250 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2253 /* We're happy with the state of the data in the buffer. */
2254 CDEBUG(D_NET, "peer %s data present %u\n",
2255 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2256 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2257 lnet_ping_buffer_decref(lp->lp_data);
2259 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2260 lnet_ping_buffer_addref(pbuf);
2263 lp->lp_state &= ~LNET_PEER_PING_SENT;
2264 spin_unlock(&lp->lp_lock);
2268 * Send event handling. Only matters for error cases, where we clean
2269 * up state on the peer and peer_ni that would otherwise be updated in
2270 * the REPLY event handler for a successful Ping, and the ACK event
2271 * handler for a successful Push.
2274 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2281 spin_lock(&lp->lp_lock);
2282 if (ev->msg_type == LNET_MSG_GET) {
2283 lp->lp_state &= ~LNET_PEER_PING_SENT;
2284 lp->lp_state |= LNET_PEER_PING_FAILED;
2285 lp->lp_ping_error = ev->status;
2286 } else { /* ev->msg_type == LNET_MSG_PUT */
2287 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2288 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2289 lp->lp_push_error = ev->status;
2291 spin_unlock(&lp->lp_lock);
2292 rc = LNET_REDISCOVER_PEER;
2294 CDEBUG(D_NET, "%s Send to %s: %d\n",
2295 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2296 libcfs_nid2str(ev->target.nid), rc);
2301 * Unlink event handling. This event is only seen if a call to
2302 * LNetMDUnlink() caused the event to be unlinked. If this call was
2303 * made after the event was set up in LNetGet() or LNetPut() then we
2304 * assume the Ping or Push timed out.
2307 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2309 spin_lock(&lp->lp_lock);
2310 /* We've passed through LNetGet() */
2311 if (lp->lp_state & LNET_PEER_PING_SENT) {
2312 lp->lp_state &= ~LNET_PEER_PING_SENT;
2313 lp->lp_state |= LNET_PEER_PING_FAILED;
2314 lp->lp_ping_error = -ETIMEDOUT;
2315 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2316 libcfs_nid2str(lp->lp_primary_nid));
2318 /* We've passed through LNetPut() */
2319 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2320 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2321 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2322 lp->lp_push_error = -ETIMEDOUT;
2323 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2324 libcfs_nid2str(lp->lp_primary_nid));
2326 spin_unlock(&lp->lp_lock);
2330 * Event handler for the discovery EQ.
2332 * Called with lnet_res_lock(cpt) held. The cpt is the
2333 * lnet_cpt_of_cookie() of the md handle cookie.
2335 static void lnet_discovery_event_handler(struct lnet_event *event)
2337 struct lnet_peer *lp = event->md.user_ptr;
2338 struct lnet_ping_buffer *pbuf;
2341 /* discovery needs to take another look */
2342 rc = LNET_REDISCOVER_PEER;
2344 CDEBUG(D_NET, "Received event: %d\n", event->type);
2346 switch (event->type) {
2347 case LNET_EVENT_ACK:
2348 lnet_discovery_event_ack(lp, event);
2350 case LNET_EVENT_REPLY:
2351 lnet_discovery_event_reply(lp, event);
2353 case LNET_EVENT_SEND:
2354 /* Only send failure triggers a retry. */
2355 rc = lnet_discovery_event_send(lp, event);
2357 case LNET_EVENT_UNLINK:
2358 /* LNetMDUnlink() was called */
2359 lnet_discovery_event_unlink(lp, event);
2362 /* Invalid events. */
2365 lnet_net_lock(LNET_LOCK_EX);
2366 if (event->unlinked) {
2367 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2368 lnet_ping_buffer_decref(pbuf);
2369 lnet_peer_decref_locked(lp);
2372 /* put peer back at end of request queue, if discovery not already
2374 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2375 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2376 wake_up(&the_lnet.ln_dc_waitq);
2378 lnet_net_unlock(LNET_LOCK_EX);
2382 * Build a peer from incoming data.
2384 * The NIDs in the incoming data are supposed to be structured as follows:
2387 * - other NIDs in same net
2388 * - NIDs in second net
2389 * - NIDs in third net
2391 * This due to the way the list of NIDs in the data is created.
2393 * Note that this function will mark the peer uptodate unless an
2394 * ENOMEM is encontered. All other errors are due to a conflict
2395 * between the DLC configuration and what discovery sees. We treat DLC
2396 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2397 * peer from becoming stuck in discovery.
2399 static int lnet_peer_merge_data(struct lnet_peer *lp,
2400 struct lnet_ping_buffer *pbuf)
2402 struct lnet_peer_ni *lpni;
2403 lnet_nid_t *curnis = NULL;
2404 lnet_nid_t *addnis = NULL;
2405 lnet_nid_t *delnis = NULL;
2415 flags = LNET_PEER_DISCOVERED;
2416 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2417 flags |= LNET_PEER_MULTI_RAIL;
2419 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2420 LIBCFS_ALLOC(curnis, nnis * sizeof(lnet_nid_t));
2421 LIBCFS_ALLOC(addnis, nnis * sizeof(lnet_nid_t));
2422 LIBCFS_ALLOC(delnis, nnis * sizeof(lnet_nid_t));
2423 if (!curnis || !addnis || !delnis) {
2431 /* Construct the list of NIDs present in peer. */
2433 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2434 curnis[ncurnis++] = lpni->lpni_nid;
2437 * Check for NIDs in pbuf not present in curnis[].
2438 * The loop starts at 1 to skip the loopback NID.
2440 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2441 for (j = 0; j < ncurnis; j++)
2442 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2445 addnis[naddnis++] = pbuf->pb_info.pi_ni[i].ns_nid;
2448 * Check for NIDs in curnis[] not present in pbuf.
2449 * The nested loop starts at 1 to skip the loopback NID.
2451 * But never add the loopback NID to delnis[]: if it is
2452 * present in curnis[] then this peer is for this node.
2454 for (i = 0; i < ncurnis; i++) {
2455 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2457 for (j = 1; j < pbuf->pb_info.pi_nnis; j++)
2458 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid)
2460 if (j == pbuf->pb_info.pi_nnis)
2461 delnis[ndelnis++] = curnis[i];
2464 for (i = 0; i < naddnis; i++) {
2465 rc = lnet_peer_add_nid(lp, addnis[i], flags);
2467 CERROR("Error adding NID %s to peer %s: %d\n",
2468 libcfs_nid2str(addnis[i]),
2469 libcfs_nid2str(lp->lp_primary_nid), rc);
2474 for (i = 0; i < ndelnis; i++) {
2475 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2477 CERROR("Error deleting NID %s from peer %s: %d\n",
2478 libcfs_nid2str(delnis[i]),
2479 libcfs_nid2str(lp->lp_primary_nid), rc);
2485 * Errors other than -ENOMEM are due to peers having been
2486 * configured with DLC. Ignore these because DLC overrides
2491 LIBCFS_FREE(curnis, nnis * sizeof(lnet_nid_t));
2492 LIBCFS_FREE(addnis, nnis * sizeof(lnet_nid_t));
2493 LIBCFS_FREE(delnis, nnis * sizeof(lnet_nid_t));
2494 lnet_ping_buffer_decref(pbuf);
2495 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2498 spin_lock(&lp->lp_lock);
2499 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2500 lp->lp_state |= LNET_PEER_FORCE_PING;
2501 spin_unlock(&lp->lp_lock);
2507 * The data in pbuf says lp is its primary peer, but the data was
2508 * received by a different peer. Try to update lp with the data.
2511 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2513 struct lnet_handle_md mdh;
2515 /* Queue lp for discovery, and force it on the request queue. */
2516 lnet_net_lock(LNET_LOCK_EX);
2517 if (lnet_peer_queue_for_discovery(lp))
2518 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2519 lnet_net_unlock(LNET_LOCK_EX);
2521 LNetInvalidateMDHandle(&mdh);
2524 * Decide whether we can move the peer to the DATA_PRESENT state.
2526 * We replace stale data for a multi-rail peer, repair PING_FAILED
2527 * status, and preempt FORCE_PING.
2529 * If after that we have DATA_PRESENT, we merge it into this peer.
2531 spin_lock(&lp->lp_lock);
2532 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2533 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2534 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2535 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2536 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2537 lnet_ping_buffer_decref(pbuf);
2542 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2543 lnet_ping_buffer_decref(lp->lp_data);
2545 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2547 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2548 mdh = lp->lp_ping_mdh;
2549 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2550 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2551 lp->lp_ping_error = 0;
2553 if (lp->lp_state & LNET_PEER_FORCE_PING)
2554 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2555 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2556 spin_unlock(&lp->lp_lock);
2558 if (!LNetMDHandleIsInvalid(mdh))
2562 return lnet_peer_merge_data(lp, pbuf);
2564 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2569 * Update a peer using the data received.
2571 static int lnet_peer_data_present(struct lnet_peer *lp)
2572 __must_hold(&lp->lp_lock)
2574 struct lnet_ping_buffer *pbuf;
2575 struct lnet_peer_ni *lpni;
2576 lnet_nid_t nid = LNET_NID_ANY;
2582 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2583 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2584 spin_unlock(&lp->lp_lock);
2587 * Modifications of peer structures are done while holding the
2588 * ln_api_mutex. A global lock is required because we may be
2589 * modifying multiple peer structures, and a mutex greatly
2590 * simplifies memory management.
2592 * The actual changes to the data structures must also protect
2593 * against concurrent lookups, for which the lnet_net_lock in
2594 * LNET_LOCK_EX mode is used.
2596 mutex_lock(&the_lnet.ln_api_mutex);
2597 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2603 * If this peer is not on the peer list then it is being torn
2604 * down, and our reference count may be all that is keeping it
2605 * alive. Don't do any work on it.
2607 if (list_empty(&lp->lp_peer_list))
2610 flags = LNET_PEER_DISCOVERED;
2611 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2612 flags |= LNET_PEER_MULTI_RAIL;
2615 * Check whether the primary NID in the message matches the
2616 * primary NID of the peer. If it does, update the peer, if
2617 * it it does not, check whether there is already a peer with
2618 * that primary NID. If no such peer exists, try to update
2619 * the primary NID of the current peer (allowed if it was
2620 * created due to message traffic) and complete the update.
2621 * If the peer did exist, hand off the data to it.
2623 * The peer for the loopback interface is a special case: this
2624 * is the peer for the local node, and we want to set its
2625 * primary NID to the correct value here. Moreover, this peer
2626 * can show up with only the loopback NID in the ping buffer.
2628 if (pbuf->pb_info.pi_nnis <= 1)
2630 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2631 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2632 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2634 rc = lnet_peer_merge_data(lp, pbuf);
2635 } else if (lp->lp_primary_nid == nid) {
2636 rc = lnet_peer_merge_data(lp, pbuf);
2638 lpni = lnet_find_peer_ni_locked(nid);
2640 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2642 CERROR("Primary NID error %s versus %s: %d\n",
2643 libcfs_nid2str(lp->lp_primary_nid),
2644 libcfs_nid2str(nid), rc);
2646 rc = lnet_peer_merge_data(lp, pbuf);
2649 rc = lnet_peer_set_primary_data(
2650 lpni->lpni_peer_net->lpn_peer, pbuf);
2651 lnet_peer_ni_decref_locked(lpni);
2655 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2656 mutex_unlock(&the_lnet.ln_api_mutex);
2658 spin_lock(&lp->lp_lock);
2659 /* Tell discovery to re-check the peer immediately. */
2661 rc = LNET_REDISCOVER_PEER;
2666 * A ping failed. Clear the PING_FAILED state and set the
2667 * FORCE_PING state, to ensure a retry even if discovery is
2668 * disabled. This avoids being left with incorrect state.
2670 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2671 __must_hold(&lp->lp_lock)
2673 struct lnet_handle_md mdh;
2676 mdh = lp->lp_ping_mdh;
2677 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2678 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2679 lp->lp_state |= LNET_PEER_FORCE_PING;
2680 rc = lp->lp_ping_error;
2681 lp->lp_ping_error = 0;
2682 spin_unlock(&lp->lp_lock);
2684 if (!LNetMDHandleIsInvalid(mdh))
2687 CDEBUG(D_NET, "peer %s:%d\n",
2688 libcfs_nid2str(lp->lp_primary_nid), rc);
2690 spin_lock(&lp->lp_lock);
2691 return rc ? rc : LNET_REDISCOVER_PEER;
2695 * Select NID to send a Ping or Push to.
2697 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2699 struct lnet_peer_ni *lpni;
2701 /* Look for a direct-connected NID for this peer. */
2703 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2704 if (!lnet_is_peer_ni_healthy_locked(lpni))
2706 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2711 return lpni->lpni_nid;
2713 /* Look for a routed-connected NID for this peer. */
2715 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2716 if (!lnet_is_peer_ni_healthy_locked(lpni))
2718 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2723 return lpni->lpni_nid;
2725 return LNET_NID_ANY;
2728 /* Active side of ping. */
2729 static int lnet_peer_send_ping(struct lnet_peer *lp)
2730 __must_hold(&lp->lp_lock)
2737 lp->lp_state |= LNET_PEER_PING_SENT;
2738 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2739 spin_unlock(&lp->lp_lock);
2741 cpt = lnet_net_lock_current();
2742 /* Refcount for MD. */
2743 lnet_peer_addref_locked(lp);
2744 pnid = lnet_peer_select_nid(lp);
2745 lnet_net_unlock(cpt);
2747 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2749 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2750 the_lnet.ln_dc_eqh, false);
2753 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2754 * refcount on the peer, otherwise LNetMDUnlink will be called
2755 * which will eventually do that.
2759 lnet_peer_decref_locked(lp);
2760 lnet_net_unlock(cpt);
2761 rc = -rc; /* change the rc to negative value */
2763 } else if (rc < 0) {
2767 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2769 spin_lock(&lp->lp_lock);
2773 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2775 * The errors that get us here are considered hard errors and
2776 * cause Discovery to terminate. So we clear PING_SENT, but do
2777 * not set either PING_FAILED or FORCE_PING. In fact we need
2778 * to clear PING_FAILED, because the unlink event handler will
2779 * have set it if we called LNetMDUnlink() above.
2781 spin_lock(&lp->lp_lock);
2782 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2787 * This function exists because you cannot call LNetMDUnlink() from an
2790 static int lnet_peer_push_failed(struct lnet_peer *lp)
2791 __must_hold(&lp->lp_lock)
2793 struct lnet_handle_md mdh;
2796 mdh = lp->lp_push_mdh;
2797 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2798 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2799 rc = lp->lp_push_error;
2800 lp->lp_push_error = 0;
2801 spin_unlock(&lp->lp_lock);
2803 if (!LNetMDHandleIsInvalid(mdh))
2806 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2807 spin_lock(&lp->lp_lock);
2808 return rc ? rc : LNET_REDISCOVER_PEER;
2811 /* Active side of push. */
2812 static int lnet_peer_send_push(struct lnet_peer *lp)
2813 __must_hold(&lp->lp_lock)
2815 struct lnet_ping_buffer *pbuf;
2816 struct lnet_process_id id;
2821 /* Don't push to a non-multi-rail peer. */
2822 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2823 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2827 lp->lp_state |= LNET_PEER_PUSH_SENT;
2828 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2829 spin_unlock(&lp->lp_lock);
2831 cpt = lnet_net_lock_current();
2832 pbuf = the_lnet.ln_ping_target;
2833 lnet_ping_buffer_addref(pbuf);
2834 lnet_net_unlock(cpt);
2836 /* Push source MD */
2837 md.start = &pbuf->pb_info;
2838 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2839 md.threshold = 2; /* Put/Ack */
2842 md.eq_handle = the_lnet.ln_dc_eqh;
2845 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2847 lnet_ping_buffer_decref(pbuf);
2848 CERROR("Can't bind push source MD: %d\n", rc);
2851 cpt = lnet_net_lock_current();
2852 /* Refcount for MD. */
2853 lnet_peer_addref_locked(lp);
2854 id.pid = LNET_PID_LUSTRE;
2855 id.nid = lnet_peer_select_nid(lp);
2856 lnet_net_unlock(cpt);
2858 if (id.nid == LNET_NID_ANY) {
2863 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2864 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2865 LNET_PROTO_PING_MATCHBITS, 0, 0);
2870 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2872 spin_lock(&lp->lp_lock);
2876 LNetMDUnlink(lp->lp_push_mdh);
2877 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2879 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2881 * The errors that get us here are considered hard errors and
2882 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2883 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2884 * because the unlink event handler will have set it if we
2885 * called LNetMDUnlink() above.
2887 spin_lock(&lp->lp_lock);
2888 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2893 * An unrecoverable error was encountered during discovery.
2894 * Set error status in peer and abort discovery.
2896 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2898 CDEBUG(D_NET, "Discovery error %s: %d\n",
2899 libcfs_nid2str(lp->lp_primary_nid), error);
2901 spin_lock(&lp->lp_lock);
2902 lp->lp_dc_error = error;
2903 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2904 lp->lp_state |= LNET_PEER_REDISCOVER;
2905 spin_unlock(&lp->lp_lock);
2909 * Mark the peer as discovered.
2911 static int lnet_peer_discovered(struct lnet_peer *lp)
2912 __must_hold(&lp->lp_lock)
2914 lp->lp_state |= LNET_PEER_DISCOVERED;
2915 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2916 LNET_PEER_REDISCOVER);
2918 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2924 * Mark the peer as to be rediscovered.
2926 static int lnet_peer_rediscover(struct lnet_peer *lp)
2927 __must_hold(&lp->lp_lock)
2929 lp->lp_state |= LNET_PEER_REDISCOVER;
2930 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2932 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2938 * Returns the first peer on the ln_dc_working queue if its timeout
2939 * has expired. Takes the current time as an argument so as to not
2940 * obsessively re-check the clock. The oldest discovery request will
2941 * be at the head of the queue.
2943 static struct lnet_peer *lnet_peer_get_dc_timed_out(time64_t now)
2945 struct lnet_peer *lp;
2947 if (list_empty(&the_lnet.ln_dc_working))
2949 lp = list_first_entry(&the_lnet.ln_dc_working,
2950 struct lnet_peer, lp_dc_list);
2951 if (now < lp->lp_last_queued + lnet_transaction_timeout)
2957 * Discovering this peer is taking too long. Cancel any Ping or Push
2958 * that discovery is waiting on by unlinking the relevant MDs. The
2959 * lnet_discovery_event_handler() will proceed from here and complete
2962 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
2964 struct lnet_handle_md ping_mdh;
2965 struct lnet_handle_md push_mdh;
2967 LNetInvalidateMDHandle(&ping_mdh);
2968 LNetInvalidateMDHandle(&push_mdh);
2970 spin_lock(&lp->lp_lock);
2971 if (lp->lp_state & LNET_PEER_PING_SENT) {
2972 ping_mdh = lp->lp_ping_mdh;
2973 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2975 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2976 push_mdh = lp->lp_push_mdh;
2977 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2979 spin_unlock(&lp->lp_lock);
2981 if (!LNetMDHandleIsInvalid(ping_mdh))
2982 LNetMDUnlink(ping_mdh);
2983 if (!LNetMDHandleIsInvalid(push_mdh))
2984 LNetMDUnlink(push_mdh);
2988 * Wait for work to be queued or some other change that must be
2989 * attended to. Returns non-zero if the discovery thread should shut
2992 static int lnet_peer_discovery_wait_for_work(void)
2999 cpt = lnet_net_lock_current();
3001 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3002 TASK_INTERRUPTIBLE);
3003 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3005 if (lnet_push_target_resize_needed())
3007 if (!list_empty(&the_lnet.ln_dc_request))
3009 if (!list_empty(&the_lnet.ln_msg_resend))
3011 if (lnet_peer_get_dc_timed_out(ktime_get_real_seconds()))
3013 lnet_net_unlock(cpt);
3016 * wakeup max every second to check if there are peers that
3017 * have been stuck on the working queue for greater than
3020 schedule_timeout(cfs_time_seconds(1));
3021 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3022 cpt = lnet_net_lock_current();
3024 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3026 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3029 lnet_net_unlock(cpt);
3031 CDEBUG(D_NET, "woken: %d\n", rc);
3037 * Messages that were pending on a destroyed peer will be put on a global
3038 * resend list. The message resend list will be checked by
3039 * the discovery thread when it wakes up, and will resend messages. These
3040 * messages can still be sendable in the case the lpni which was the initial
3041 * cause of the message re-queue was transfered to another peer.
3043 * It is possible that LNet could be shutdown while we're iterating
3044 * through the list. lnet_shudown_lndnets() will attempt to access the
3045 * resend list, but will have to wait until the spinlock is released, by
3046 * which time there shouldn't be any more messages on the resend list.
3047 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3048 * for the messages so they can be released. The other case is that
3049 * lnet_shudown_lndnets() can finalize all the messages before this
3050 * function can visit the resend list, in which case this function will be
3053 static void lnet_resend_msgs(void)
3055 struct lnet_msg *msg, *tmp;
3056 struct list_head resend;
3059 INIT_LIST_HEAD(&resend);
3061 spin_lock(&the_lnet.ln_msg_resend_lock);
3062 list_splice(&the_lnet.ln_msg_resend, &resend);
3063 spin_unlock(&the_lnet.ln_msg_resend_lock);
3065 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3066 list_del_init(&msg->msg_list);
3067 rc = lnet_send(msg->msg_src_nid_param, msg,
3068 msg->msg_rtr_nid_param);
3070 CNETERR("Error sending %s to %s: %d\n",
3071 lnet_msgtyp2str(msg->msg_type),
3072 libcfs_id2str(msg->msg_target), rc);
3073 lnet_finalize(msg, rc);
3078 /* The discovery thread. */
3079 static int lnet_peer_discovery(void *arg)
3081 struct lnet_peer *lp;
3085 CDEBUG(D_NET, "started\n");
3086 cfs_block_allsigs();
3089 if (lnet_peer_discovery_wait_for_work())
3094 if (lnet_push_target_resize_needed())
3095 lnet_push_target_resize();
3097 lnet_net_lock(LNET_LOCK_EX);
3098 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3102 * Process all incoming discovery work requests. When
3103 * discovery must wait on a peer to change state, it
3104 * is added to the tail of the ln_dc_working queue. A
3105 * timestamp keeps track of when the peer was added,
3106 * so we can time out discovery requests that take too
3109 while (!list_empty(&the_lnet.ln_dc_request)) {
3110 lp = list_first_entry(&the_lnet.ln_dc_request,
3111 struct lnet_peer, lp_dc_list);
3112 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3114 * set the time the peer was put on the dc_working
3115 * queue. It shouldn't remain on the queue
3116 * forever, in case the GET message (for ping)
3117 * doesn't get a REPLY or the PUT message (for
3118 * push) doesn't get an ACK.
3120 * TODO: LNet Health will deal with this scenario
3123 lp->lp_last_queued = ktime_get_real_seconds();
3124 lnet_net_unlock(LNET_LOCK_EX);
3127 * Select an action depending on the state of
3128 * the peer and whether discovery is disabled.
3129 * The check whether discovery is disabled is
3130 * done after the code that handles processing
3131 * for arrived data, cleanup for failures, and
3132 * forcing a Ping or Push.
3134 spin_lock(&lp->lp_lock);
3135 CDEBUG(D_NET, "peer %s state %#x\n",
3136 libcfs_nid2str(lp->lp_primary_nid),
3138 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3139 rc = lnet_peer_data_present(lp);
3140 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3141 rc = lnet_peer_ping_failed(lp);
3142 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3143 rc = lnet_peer_push_failed(lp);
3144 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3145 rc = lnet_peer_send_ping(lp);
3146 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3147 rc = lnet_peer_send_push(lp);
3148 else if (lnet_peer_discovery_disabled)
3149 rc = lnet_peer_rediscover(lp);
3150 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3151 rc = lnet_peer_send_ping(lp);
3152 else if (lnet_peer_needs_push(lp))
3153 rc = lnet_peer_send_push(lp);
3155 rc = lnet_peer_discovered(lp);
3156 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3157 libcfs_nid2str(lp->lp_primary_nid),
3159 spin_unlock(&lp->lp_lock);
3161 lnet_net_lock(LNET_LOCK_EX);
3162 if (rc == LNET_REDISCOVER_PEER) {
3163 list_move(&lp->lp_dc_list,
3164 &the_lnet.ln_dc_request);
3166 lnet_peer_discovery_error(lp, rc);
3168 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3169 lnet_peer_discovery_complete(lp);
3170 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3175 * Now that the ln_dc_request queue has been emptied
3176 * check the ln_dc_working queue for peers that are
3177 * taking too long. Move all that are found to the
3178 * ln_dc_expired queue and time out any pending
3179 * Ping or Push. We have to drop the lnet_net_lock
3180 * in the loop because lnet_peer_cancel_discovery()
3181 * calls LNetMDUnlink().
3183 now = ktime_get_real_seconds();
3184 while ((lp = lnet_peer_get_dc_timed_out(now)) != NULL) {
3185 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3186 lnet_net_unlock(LNET_LOCK_EX);
3187 lnet_peer_cancel_discovery(lp);
3188 lnet_net_lock(LNET_LOCK_EX);
3191 lnet_net_unlock(LNET_LOCK_EX);
3194 CDEBUG(D_NET, "stopping\n");
3196 * Clean up before telling lnet_peer_discovery_stop() that
3197 * we're done. Use wake_up() below to somewhat reduce the
3198 * size of the thundering herd if there are multiple threads
3199 * waiting on discovery of a single peer.
3201 LNetEQFree(the_lnet.ln_dc_eqh);
3202 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3204 /* Queue cleanup 1: stop all pending pings and pushes. */
3205 lnet_net_lock(LNET_LOCK_EX);
3206 while (!list_empty(&the_lnet.ln_dc_working)) {
3207 lp = list_first_entry(&the_lnet.ln_dc_working,
3208 struct lnet_peer, lp_dc_list);
3209 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3210 lnet_net_unlock(LNET_LOCK_EX);
3211 lnet_peer_cancel_discovery(lp);
3212 lnet_net_lock(LNET_LOCK_EX);
3214 lnet_net_unlock(LNET_LOCK_EX);
3216 /* Queue cleanup 2: wait for the expired queue to clear. */
3217 while (!list_empty(&the_lnet.ln_dc_expired))
3218 schedule_timeout(cfs_time_seconds(1));
3220 /* Queue cleanup 3: clear the request queue. */
3221 lnet_net_lock(LNET_LOCK_EX);
3222 while (!list_empty(&the_lnet.ln_dc_request)) {
3223 lp = list_first_entry(&the_lnet.ln_dc_request,
3224 struct lnet_peer, lp_dc_list);
3225 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3226 lnet_peer_discovery_complete(lp);
3228 lnet_net_unlock(LNET_LOCK_EX);
3230 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3231 wake_up(&the_lnet.ln_dc_waitq);
3233 CDEBUG(D_NET, "stopped\n");
3238 /* ln_api_mutex is held on entry. */
3239 int lnet_peer_discovery_start(void)
3241 struct task_struct *task;
3244 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3247 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3249 CERROR("Can't allocate discovery EQ: %d\n", rc);
3253 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3254 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3257 CERROR("Can't start peer discovery thread: %d\n", rc);
3259 LNetEQFree(the_lnet.ln_dc_eqh);
3260 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3262 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3265 CDEBUG(D_NET, "discovery start: %d\n", rc);
3270 /* ln_api_mutex is held on entry. */
3271 void lnet_peer_discovery_stop(void)
3273 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3276 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3277 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3278 wake_up(&the_lnet.ln_dc_waitq);
3280 wait_event(the_lnet.ln_dc_waitq,
3281 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3283 LASSERT(list_empty(&the_lnet.ln_dc_request));
3284 LASSERT(list_empty(&the_lnet.ln_dc_working));
3285 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3287 CDEBUG(D_NET, "discovery stopped\n");
3293 lnet_debug_peer(lnet_nid_t nid)
3295 char *aliveness = "NA";
3296 struct lnet_peer_ni *lp;
3299 cpt = lnet_cpt_of_nid(nid, NULL);
3302 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3304 lnet_net_unlock(cpt);
3305 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3309 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3310 aliveness = lp->lpni_alive ? "up" : "down";
3312 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3313 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3314 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3315 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3316 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3318 lnet_peer_ni_decref_locked(lp);
3320 lnet_net_unlock(cpt);
3323 /* Gathering information for userspace. */
3325 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3326 char aliveness[LNET_MAX_STR_LEN],
3327 __u32 *cpt_iter, __u32 *refcount,
3328 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3329 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3330 __u32 *peer_tx_qnob)
3332 struct lnet_peer_table *peer_table;
3333 struct lnet_peer_ni *lp;
3338 /* get the number of CPTs */
3339 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3341 /* if the cpt number to be examined is >= the number of cpts in
3342 * the system then indicate that there are no more cpts to examin
3344 if (*cpt_iter >= lncpt)
3347 /* get the current table */
3348 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3349 /* if the ptable is NULL then there are no more cpts to examine */
3350 if (peer_table == NULL)
3353 lnet_net_lock(*cpt_iter);
3355 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3356 struct list_head *peers = &peer_table->pt_hash[j];
3358 list_for_each_entry(lp, peers, lpni_hashlist) {
3359 if (peer_index-- > 0)
3362 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3363 if (lnet_isrouter(lp) ||
3364 lnet_peer_aliveness_enabled(lp))
3365 snprintf(aliveness, LNET_MAX_STR_LEN,
3366 lp->lpni_alive ? "up" : "down");
3368 *nid = lp->lpni_nid;
3369 *refcount = atomic_read(&lp->lpni_refcount);
3370 *ni_peer_tx_credits =
3371 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3372 *peer_tx_credits = lp->lpni_txcredits;
3373 *peer_rtr_credits = lp->lpni_rtrcredits;
3374 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3375 *peer_tx_qnob = lp->lpni_txqnob;
3381 lnet_net_unlock(*cpt_iter);
3385 return found ? 0 : -ENOENT;
3388 /* ln_api_mutex is held, which keeps the peer list stable */
3389 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3391 struct lnet_ioctl_element_stats *lpni_stats;
3392 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3393 struct lnet_peer_ni_credit_info *lpni_info;
3394 struct lnet_peer_ni *lpni;
3395 struct lnet_peer *lp;
3400 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3407 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3408 + sizeof(*lpni_msg_stats);
3409 size *= lp->lp_nnis;
3410 if (size > cfg->prcfg_size) {
3411 cfg->prcfg_size = size;
3416 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3417 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3418 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3419 cfg->prcfg_count = lp->lp_nnis;
3420 cfg->prcfg_size = size;
3421 cfg->prcfg_state = lp->lp_state;
3423 /* Allocate helper buffers. */
3425 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3428 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3431 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3432 if (!lpni_msg_stats)
3433 goto out_free_stats;
3438 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3439 nid = lpni->lpni_nid;
3440 if (copy_to_user(bulk, &nid, sizeof(nid)))
3441 goto out_free_msg_stats;
3442 bulk += sizeof(nid);
3444 memset(lpni_info, 0, sizeof(*lpni_info));
3445 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3446 if (lnet_isrouter(lpni) ||
3447 lnet_peer_aliveness_enabled(lpni))
3448 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3449 lpni->lpni_alive ? "up" : "down");
3451 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3452 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3453 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3454 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3455 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3456 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3457 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3458 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3459 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3460 goto out_free_msg_stats;
3461 bulk += sizeof(*lpni_info);
3463 memset(lpni_stats, 0, sizeof(*lpni_stats));
3464 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3465 LNET_STATS_TYPE_SEND);
3466 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3467 LNET_STATS_TYPE_RECV);
3468 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3469 LNET_STATS_TYPE_DROP);
3470 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3471 goto out_free_msg_stats;
3472 bulk += sizeof(*lpni_stats);
3473 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3474 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3475 goto out_free_msg_stats;
3476 bulk += sizeof(*lpni_msg_stats);
3481 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3483 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3485 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3487 lnet_peer_decref_locked(lp);