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 lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
174 lpni->lpni_last_alive = ktime_get_seconds(); /* assumes alive */
175 lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
176 lpni->lpni_nid = nid;
177 lpni->lpni_cpt = cpt;
178 atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
180 net = lnet_get_net_locked(LNET_NIDNET(nid));
181 lpni->lpni_net = net;
183 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
184 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
185 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
186 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
189 * This peer_ni is not on a local network, so we
190 * cannot add the credits here. In case the net is
191 * added later, add the peer_ni to the remote peer ni
192 * list so it can be easily found and revisited.
194 /* FIXME: per-net implementation instead? */
195 atomic_inc(&lpni->lpni_refcount);
196 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
197 &the_lnet.ln_remote_peer_ni_list);
200 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
205 static struct lnet_peer_net *
206 lnet_peer_net_alloc(__u32 net_id)
208 struct lnet_peer_net *lpn;
210 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
214 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
215 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
216 lpn->lpn_net_id = net_id;
218 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
224 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
226 struct lnet_peer *lp;
228 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
230 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
231 LASSERT(list_empty(&lpn->lpn_peer_nis));
232 LASSERT(list_empty(&lpn->lpn_peer_nets));
234 lpn->lpn_peer = NULL;
235 LIBCFS_FREE(lpn, sizeof(*lpn));
237 lnet_peer_decref_locked(lp);
240 static struct lnet_peer *
241 lnet_peer_alloc(lnet_nid_t nid)
243 struct lnet_peer *lp;
245 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
249 INIT_LIST_HEAD(&lp->lp_rtrq);
250 INIT_LIST_HEAD(&lp->lp_routes);
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_LIST_HEAD(&lp->lp_rtr_list);
256 init_waitqueue_head(&lp->lp_dc_waitq);
257 spin_lock_init(&lp->lp_lock);
258 lp->lp_primary_nid = nid;
260 * Turn off discovery for loopback peer. If you're creating a peer
261 * for the loopback interface then that was initiated when we
262 * attempted to send a message over the loopback. There is no need
263 * to ever use a different interface when sending messages to
266 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
267 lp->lp_state = LNET_PEER_NO_DISCOVERY;
268 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
270 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
276 lnet_destroy_peer_locked(struct lnet_peer *lp)
278 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
280 LASSERT(atomic_read(&lp->lp_refcount) == 0);
281 LASSERT(lp->lp_rtr_refcount == 0);
282 LASSERT(list_empty(&lp->lp_peer_nets));
283 LASSERT(list_empty(&lp->lp_peer_list));
284 LASSERT(list_empty(&lp->lp_dc_list));
287 lnet_ping_buffer_decref(lp->lp_data);
290 * if there are messages still on the pending queue, then make
291 * sure to queue them on the ln_msg_resend list so they can be
292 * resent at a later point if the discovery thread is still
294 * If the discovery thread has stopped, then the wakeup will be a
295 * no-op, and it is expected the lnet_shutdown_lndnets() will
296 * eventually be called, which will traverse this list and
297 * finalize the messages on the list.
298 * We can not resend them now because we're holding the cpt lock.
299 * Releasing the lock can cause an inconsistent state
301 spin_lock(&the_lnet.ln_msg_resend_lock);
302 spin_lock(&lp->lp_lock);
303 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
304 spin_unlock(&lp->lp_lock);
305 spin_unlock(&the_lnet.ln_msg_resend_lock);
306 wake_up(&the_lnet.ln_dc_waitq);
308 LIBCFS_FREE(lp, sizeof(*lp));
312 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
313 * that peer_net, detach the peer_net from the peer.
315 * Call with lnet_net_lock/EX held
318 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
320 struct lnet_peer_table *ptable;
321 struct lnet_peer_net *lpn;
322 struct lnet_peer *lp;
325 * Belts and suspenders: gracefully handle teardown of a
326 * partially connected peer_ni.
328 lpn = lpni->lpni_peer_net;
330 list_del_init(&lpni->lpni_peer_nis);
332 * If there are no lpni's left, we detach lpn from
333 * lp_peer_nets, so it cannot be found anymore.
335 if (list_empty(&lpn->lpn_peer_nis))
336 list_del_init(&lpn->lpn_peer_nets);
338 /* Update peer NID count. */
343 * If there are no more peer nets, make the peer unfindable
344 * via the peer_tables.
346 * Otherwise, if the peer is DISCOVERED, tell discovery to
347 * take another look at it. This is a no-op if discovery for
348 * this peer did the detaching.
350 if (list_empty(&lp->lp_peer_nets)) {
351 list_del_init(&lp->lp_peer_list);
352 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
354 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
355 /* Discovery isn't running, nothing to do here. */
356 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
357 lnet_peer_queue_for_discovery(lp);
358 wake_up(&the_lnet.ln_dc_waitq);
360 CDEBUG(D_NET, "peer %s NID %s\n",
361 libcfs_nid2str(lp->lp_primary_nid),
362 libcfs_nid2str(lpni->lpni_nid));
365 /* called with lnet_net_lock LNET_LOCK_EX held */
367 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
369 struct lnet_peer_table *ptable = NULL;
371 /* don't remove a peer_ni if it's also a gateway */
372 if (lnet_isrouter(lpni)) {
373 CERROR("Peer NI %s is a gateway. Can not delete it\n",
374 libcfs_nid2str(lpni->lpni_nid));
378 lnet_peer_remove_from_remote_list(lpni);
380 /* remove peer ni from the hash list. */
381 list_del_init(&lpni->lpni_hashlist);
384 * indicate the peer is being deleted so the monitor thread can
385 * remove it from the recovery queue.
387 spin_lock(&lpni->lpni_lock);
388 lpni->lpni_state |= LNET_PEER_NI_DELETING;
389 spin_unlock(&lpni->lpni_lock);
391 /* decrement the ref count on the peer table */
392 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
393 LASSERT(ptable->pt_number > 0);
397 * The peer_ni can no longer be found with a lookup. But there
398 * can be current users, so keep track of it on the zombie
399 * list until the reference count has gone to zero.
401 * The last reference may be lost in a place where the
402 * lnet_net_lock locks only a single cpt, and that cpt may not
403 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
406 spin_lock(&ptable->pt_zombie_lock);
407 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
408 ptable->pt_zombies++;
409 spin_unlock(&ptable->pt_zombie_lock);
411 /* no need to keep this peer_ni on the hierarchy anymore */
412 lnet_peer_detach_peer_ni_locked(lpni);
414 /* remove hashlist reference on peer_ni */
415 lnet_peer_ni_decref_locked(lpni);
420 void lnet_peer_uninit(void)
422 struct lnet_peer_ni *lpni, *tmp;
424 lnet_net_lock(LNET_LOCK_EX);
426 /* remove all peer_nis from the remote peer and the hash list */
427 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
428 lpni_on_remote_peer_ni_list)
429 lnet_peer_ni_del_locked(lpni);
431 lnet_peer_tables_destroy();
433 lnet_net_unlock(LNET_LOCK_EX);
437 lnet_peer_del_locked(struct lnet_peer *peer)
439 struct lnet_peer_ni *lpni = NULL, *lpni2;
442 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
444 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
445 while (lpni != NULL) {
446 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
447 rc = lnet_peer_ni_del_locked(lpni);
457 lnet_peer_del(struct lnet_peer *peer)
459 lnet_net_lock(LNET_LOCK_EX);
460 lnet_peer_del_locked(peer);
461 lnet_net_unlock(LNET_LOCK_EX);
467 * Delete a NID from a peer. Call with ln_api_mutex held.
470 * -EPERM: Non-DLC deletion from DLC-configured peer.
471 * -ENOENT: No lnet_peer_ni corresponding to the nid.
472 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
473 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
476 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
478 struct lnet_peer_ni *lpni;
479 lnet_nid_t primary_nid = lp->lp_primary_nid;
482 if (!(flags & LNET_PEER_CONFIGURED)) {
483 if (lp->lp_state & LNET_PEER_CONFIGURED) {
488 lpni = lnet_find_peer_ni_locked(nid);
493 lnet_peer_ni_decref_locked(lpni);
494 if (lp != lpni->lpni_peer_net->lpn_peer) {
500 * This function only allows deletion of the primary NID if it
503 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
508 lnet_net_lock(LNET_LOCK_EX);
510 rc = lnet_peer_ni_del_locked(lpni);
512 lnet_net_unlock(LNET_LOCK_EX);
515 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
516 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
522 lnet_peer_table_cleanup_locked(struct lnet_net *net,
523 struct lnet_peer_table *ptable)
526 struct lnet_peer_ni *next;
527 struct lnet_peer_ni *lpni;
528 struct lnet_peer *peer;
530 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
531 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
533 if (net != NULL && net != lpni->lpni_net)
536 peer = lpni->lpni_peer_net->lpn_peer;
537 if (peer->lp_primary_nid != lpni->lpni_nid) {
538 lnet_peer_ni_del_locked(lpni);
542 * Removing the primary NID implies removing
543 * the entire peer. Advance next beyond any
544 * peer_ni that belongs to the same peer.
546 list_for_each_entry_from(next, &ptable->pt_hash[i],
548 if (next->lpni_peer_net->lpn_peer != peer)
551 lnet_peer_del_locked(peer);
557 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
561 spin_lock(&ptable->pt_zombie_lock);
562 while (ptable->pt_zombies) {
563 spin_unlock(&ptable->pt_zombie_lock);
565 if (is_power_of_2(i)) {
567 "Waiting for %d zombies on peer table\n",
570 set_current_state(TASK_UNINTERRUPTIBLE);
571 schedule_timeout(cfs_time_seconds(1) >> 1);
572 spin_lock(&ptable->pt_zombie_lock);
574 spin_unlock(&ptable->pt_zombie_lock);
578 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
579 struct lnet_peer_table *ptable)
581 struct lnet_peer_ni *lp;
582 struct lnet_peer_ni *tmp;
586 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
587 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
589 if (net != lp->lpni_net)
592 if (!lnet_isrouter(lp))
595 gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
597 lnet_net_unlock(LNET_LOCK_EX);
598 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
599 lnet_net_lock(LNET_LOCK_EX);
605 lnet_peer_tables_cleanup(struct lnet_net *net)
608 struct lnet_peer_table *ptable;
610 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
611 /* If just deleting the peers for a NI, get rid of any routes these
612 * peers are gateways for. */
613 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
614 lnet_net_lock(LNET_LOCK_EX);
615 lnet_peer_table_del_rtrs_locked(net, ptable);
616 lnet_net_unlock(LNET_LOCK_EX);
619 /* Start the cleanup process */
620 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
621 lnet_net_lock(LNET_LOCK_EX);
622 lnet_peer_table_cleanup_locked(net, ptable);
623 lnet_net_unlock(LNET_LOCK_EX);
626 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
627 lnet_peer_ni_finalize_wait(ptable);
630 static struct lnet_peer_ni *
631 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
633 struct list_head *peers;
634 struct lnet_peer_ni *lp;
636 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
638 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
639 list_for_each_entry(lp, peers, lpni_hashlist) {
640 if (lp->lpni_nid == nid) {
641 lnet_peer_ni_addref_locked(lp);
649 struct lnet_peer_ni *
650 lnet_find_peer_ni_locked(lnet_nid_t nid)
652 struct lnet_peer_ni *lpni;
653 struct lnet_peer_table *ptable;
656 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
658 ptable = the_lnet.ln_peer_tables[cpt];
659 lpni = lnet_get_peer_ni_locked(ptable, nid);
665 lnet_find_peer(lnet_nid_t nid)
667 struct lnet_peer_ni *lpni;
668 struct lnet_peer *lp = NULL;
671 cpt = lnet_net_lock_current();
672 lpni = lnet_find_peer_ni_locked(nid);
674 lp = lpni->lpni_peer_net->lpn_peer;
675 lnet_peer_addref_locked(lp);
676 lnet_peer_ni_decref_locked(lpni);
678 lnet_net_unlock(cpt);
683 struct lnet_peer_ni *
684 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
685 struct lnet_peer_net *peer_net,
686 struct lnet_peer_ni *prev)
688 struct lnet_peer_ni *lpni;
689 struct lnet_peer_net *net = peer_net;
693 if (list_empty(&peer->lp_peer_nets))
696 net = list_entry(peer->lp_peer_nets.next,
697 struct lnet_peer_net,
700 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
706 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
708 * if you reached the end of the peer ni list and the peer
709 * net is specified then there are no more peer nis in that
716 * we reached the end of this net ni list. move to the
719 if (prev->lpni_peer_net->lpn_peer_nets.next ==
721 /* no more nets and no more NIs. */
724 /* get the next net */
725 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
726 struct lnet_peer_net,
728 /* get the ni on it */
729 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
735 /* there are more nis left */
736 lpni = list_entry(prev->lpni_peer_nis.next,
737 struct lnet_peer_ni, lpni_peer_nis);
742 /* Call with the ln_api_mutex held */
743 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
745 struct lnet_process_id id;
746 struct lnet_peer_table *ptable;
747 struct lnet_peer *lp;
756 if (the_lnet.ln_state != LNET_STATE_RUNNING)
759 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
762 * Count the number of peers, and return E2BIG if the buffer
763 * is too small. We'll also return the desired size.
766 for (cpt = 0; cpt < lncpt; cpt++) {
767 ptable = the_lnet.ln_peer_tables[cpt];
768 count += ptable->pt_peers;
770 size = count * sizeof(*ids);
775 * Walk the peer lists and copy out the primary nids.
776 * This is safe because the peer lists are only modified
777 * while the ln_api_mutex is held. So we don't need to
778 * hold the lnet_net_lock as well, and can therefore
779 * directly call copy_to_user().
782 memset(&id, 0, sizeof(id));
783 id.pid = LNET_PID_LUSTRE;
785 for (cpt = 0; cpt < lncpt; cpt++) {
786 ptable = the_lnet.ln_peer_tables[cpt];
787 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
790 id.nid = lp->lp_primary_nid;
791 if (copy_to_user(&ids[i], &id, sizeof(id)))
804 * Start pushes to peers that need to be updated for a configuration
805 * change on this node.
808 lnet_push_update_to_peers(int force)
810 struct lnet_peer_table *ptable;
811 struct lnet_peer *lp;
815 lnet_net_lock(LNET_LOCK_EX);
816 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
817 for (cpt = 0; cpt < lncpt; cpt++) {
818 ptable = the_lnet.ln_peer_tables[cpt];
819 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
821 spin_lock(&lp->lp_lock);
822 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
823 lp->lp_state |= LNET_PEER_FORCE_PUSH;
824 spin_unlock(&lp->lp_lock);
826 if (lnet_peer_needs_push(lp))
827 lnet_peer_queue_for_discovery(lp);
830 lnet_net_unlock(LNET_LOCK_EX);
831 wake_up(&the_lnet.ln_dc_waitq);
835 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
836 * this is a preferred point-to-point path. Call with lnet_net_lock in
840 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
844 if (lpni->lpni_pref_nnids == 0)
846 if (lpni->lpni_pref_nnids == 1)
847 return lpni->lpni_pref.nid == nid;
848 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
849 if (lpni->lpni_pref.nids[i] == nid)
856 * Set a single ni as preferred, provided no preferred ni is already
857 * defined. Only to be used for non-multi-rail peer_ni.
860 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
864 spin_lock(&lpni->lpni_lock);
865 if (nid == LNET_NID_ANY) {
867 } else if (lpni->lpni_pref_nnids > 0) {
869 } else if (lpni->lpni_pref_nnids == 0) {
870 lpni->lpni_pref.nid = nid;
871 lpni->lpni_pref_nnids = 1;
872 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
874 spin_unlock(&lpni->lpni_lock);
876 CDEBUG(D_NET, "peer %s nid %s: %d\n",
877 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
882 * Clear the preferred NID from a non-multi-rail peer_ni, provided
883 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
886 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
890 spin_lock(&lpni->lpni_lock);
891 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
892 lpni->lpni_pref_nnids = 0;
893 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
894 } else if (lpni->lpni_pref_nnids == 0) {
899 spin_unlock(&lpni->lpni_lock);
901 CDEBUG(D_NET, "peer %s: %d\n",
902 libcfs_nid2str(lpni->lpni_nid), rc);
907 * Clear the preferred NIDs from a non-multi-rail peer.
910 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
912 struct lnet_peer_ni *lpni = NULL;
914 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
915 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
919 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
921 lnet_nid_t *nids = NULL;
922 lnet_nid_t *oldnids = NULL;
923 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
928 if (nid == LNET_NID_ANY) {
933 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
938 /* A non-MR node may have only one preferred NI per peer_ni */
939 if (lpni->lpni_pref_nnids > 0) {
940 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
946 if (lpni->lpni_pref_nnids != 0) {
947 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
948 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
953 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
954 if (lpni->lpni_pref.nids[i] == nid) {
955 LIBCFS_FREE(nids, size);
959 nids[i] = lpni->lpni_pref.nids[i];
964 lnet_net_lock(LNET_LOCK_EX);
965 spin_lock(&lpni->lpni_lock);
966 if (lpni->lpni_pref_nnids == 0) {
967 lpni->lpni_pref.nid = nid;
969 oldnids = lpni->lpni_pref.nids;
970 lpni->lpni_pref.nids = nids;
972 lpni->lpni_pref_nnids++;
973 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
974 spin_unlock(&lpni->lpni_lock);
975 lnet_net_unlock(LNET_LOCK_EX);
978 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
979 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
982 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
983 spin_lock(&lpni->lpni_lock);
984 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
985 spin_unlock(&lpni->lpni_lock);
987 CDEBUG(D_NET, "peer %s nid %s: %d\n",
988 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
993 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
995 lnet_nid_t *nids = NULL;
996 lnet_nid_t *oldnids = NULL;
997 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1002 if (lpni->lpni_pref_nnids == 0) {
1007 if (lpni->lpni_pref_nnids == 1) {
1008 if (lpni->lpni_pref.nid != nid) {
1012 } else if (lpni->lpni_pref_nnids == 2) {
1013 if (lpni->lpni_pref.nids[0] != nid &&
1014 lpni->lpni_pref.nids[1] != nid) {
1019 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1020 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1025 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1026 if (lpni->lpni_pref.nids[i] != nid)
1028 nids[j++] = lpni->lpni_pref.nids[i];
1030 /* Check if we actually removed a nid. */
1031 if (j == lpni->lpni_pref_nnids) {
1032 LIBCFS_FREE(nids, size);
1038 lnet_net_lock(LNET_LOCK_EX);
1039 spin_lock(&lpni->lpni_lock);
1040 if (lpni->lpni_pref_nnids == 1) {
1041 lpni->lpni_pref.nid = LNET_NID_ANY;
1042 } else if (lpni->lpni_pref_nnids == 2) {
1043 oldnids = lpni->lpni_pref.nids;
1044 if (oldnids[0] == nid)
1045 lpni->lpni_pref.nid = oldnids[1];
1047 lpni->lpni_pref.nid = oldnids[2];
1049 oldnids = lpni->lpni_pref.nids;
1050 lpni->lpni_pref.nids = nids;
1052 lpni->lpni_pref_nnids--;
1053 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1054 spin_unlock(&lpni->lpni_lock);
1055 lnet_net_unlock(LNET_LOCK_EX);
1058 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1059 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1062 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1063 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1068 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1070 struct lnet_peer_ni *lpni;
1071 lnet_nid_t primary_nid = nid;
1073 lpni = lnet_find_peer_ni_locked(nid);
1075 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1076 lnet_peer_ni_decref_locked(lpni);
1083 LNetPrimaryNID(lnet_nid_t nid)
1085 struct lnet_peer *lp;
1086 struct lnet_peer_ni *lpni;
1087 lnet_nid_t primary_nid = nid;
1091 cpt = lnet_net_lock_current();
1092 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1097 lp = lpni->lpni_peer_net->lpn_peer;
1098 while (!lnet_peer_is_uptodate(lp)) {
1099 rc = lnet_discover_peer_locked(lpni, cpt, true);
1102 lp = lpni->lpni_peer_net->lpn_peer;
1104 primary_nid = lp->lp_primary_nid;
1106 lnet_peer_ni_decref_locked(lpni);
1108 lnet_net_unlock(cpt);
1110 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1111 libcfs_nid2str(primary_nid), rc);
1114 EXPORT_SYMBOL(LNetPrimaryNID);
1116 struct lnet_peer_net *
1117 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1119 struct lnet_peer_net *peer_net;
1120 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1121 if (peer_net->lpn_net_id == net_id)
1128 * Attach a peer_ni to a peer_net and peer. This function assumes
1129 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1130 * may be attached to a different peer, in which case it will be
1131 * properly detached first. The whole operation is done atomically.
1133 * Always returns 0. This is the last function called from functions
1134 * that do return an int, so returning 0 here allows the compiler to
1138 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1139 struct lnet_peer_net *lpn,
1140 struct lnet_peer_ni *lpni,
1143 struct lnet_peer_table *ptable;
1145 /* Install the new peer_ni */
1146 lnet_net_lock(LNET_LOCK_EX);
1147 /* Add peer_ni to global peer table hash, if necessary. */
1148 if (list_empty(&lpni->lpni_hashlist)) {
1149 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1151 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1152 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1153 ptable->pt_version++;
1154 ptable->pt_number++;
1155 /* This is the 1st refcount on lpni. */
1156 atomic_inc(&lpni->lpni_refcount);
1159 /* Detach the peer_ni from an existing peer, if necessary. */
1160 if (lpni->lpni_peer_net) {
1161 LASSERT(lpni->lpni_peer_net != lpn);
1162 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1163 lnet_peer_detach_peer_ni_locked(lpni);
1164 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1165 lpni->lpni_peer_net = NULL;
1168 /* Add peer_ni to peer_net */
1169 lpni->lpni_peer_net = lpn;
1170 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1171 lnet_peer_net_addref_locked(lpn);
1173 /* Add peer_net to peer */
1174 if (!lpn->lpn_peer) {
1176 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1177 lnet_peer_addref_locked(lp);
1180 /* Add peer to global peer list, if necessary */
1181 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1182 if (list_empty(&lp->lp_peer_list)) {
1183 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1188 /* Update peer state */
1189 spin_lock(&lp->lp_lock);
1190 if (flags & LNET_PEER_CONFIGURED) {
1191 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1192 lp->lp_state |= LNET_PEER_CONFIGURED;
1194 if (flags & LNET_PEER_MULTI_RAIL) {
1195 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1196 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1197 lnet_peer_clr_non_mr_pref_nids(lp);
1200 spin_unlock(&lp->lp_lock);
1203 lnet_net_unlock(LNET_LOCK_EX);
1205 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1206 libcfs_nid2str(lp->lp_primary_nid),
1207 libcfs_nid2str(lpni->lpni_nid), flags);
1213 * Create a new peer, with nid as its primary nid.
1215 * Call with the lnet_api_mutex held.
1218 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1220 struct lnet_peer *lp;
1221 struct lnet_peer_net *lpn;
1222 struct lnet_peer_ni *lpni;
1225 LASSERT(nid != LNET_NID_ANY);
1228 * No need for the lnet_net_lock here, because the
1229 * lnet_api_mutex is held.
1231 lpni = lnet_find_peer_ni_locked(nid);
1233 /* A peer with this NID already exists. */
1234 lp = lpni->lpni_peer_net->lpn_peer;
1235 lnet_peer_ni_decref_locked(lpni);
1237 * This is an error if the peer was configured and the
1238 * primary NID differs or an attempt is made to change
1239 * the Multi-Rail flag. Otherwise the assumption is
1240 * that an existing peer is being modified.
1242 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1243 if (lp->lp_primary_nid != nid)
1245 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1249 /* Delete and recreate as a configured peer. */
1253 /* Create peer, peer_net, and peer_ni. */
1255 lp = lnet_peer_alloc(nid);
1258 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1261 lpni = lnet_peer_ni_alloc(nid);
1265 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1268 LIBCFS_FREE(lpn, sizeof(*lpn));
1270 LIBCFS_FREE(lp, sizeof(*lp));
1272 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1273 libcfs_nid2str(nid), flags, rc);
1278 * Add a NID to a peer. Call with ln_api_mutex held.
1281 * -EPERM: Non-DLC addition to a DLC-configured peer.
1282 * -EEXIST: The NID was configured by DLC for a different peer.
1283 * -ENOMEM: Out of memory.
1284 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1285 * non-multi-rail peer.
1288 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1290 struct lnet_peer_net *lpn;
1291 struct lnet_peer_ni *lpni;
1295 LASSERT(nid != LNET_NID_ANY);
1297 /* A configured peer can only be updated through configuration. */
1298 if (!(flags & LNET_PEER_CONFIGURED)) {
1299 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1306 * The MULTI_RAIL flag can be set but not cleared, because
1307 * that would leave the peer struct in an invalid state.
1309 if (flags & LNET_PEER_MULTI_RAIL) {
1310 spin_lock(&lp->lp_lock);
1311 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1312 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1313 lnet_peer_clr_non_mr_pref_nids(lp);
1315 spin_unlock(&lp->lp_lock);
1316 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1321 lpni = lnet_find_peer_ni_locked(nid);
1324 * A peer_ni already exists. This is only a problem if
1325 * it is not connected to this peer and was configured
1328 lnet_peer_ni_decref_locked(lpni);
1329 if (lpni->lpni_peer_net->lpn_peer == lp)
1331 if (lnet_peer_ni_is_configured(lpni)) {
1335 /* If this is the primary NID, destroy the peer. */
1336 if (lnet_peer_ni_is_primary(lpni)) {
1337 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1338 lpni = lnet_peer_ni_alloc(nid);
1345 lpni = lnet_peer_ni_alloc(nid);
1353 * Get the peer_net. Check that we're not adding a second
1354 * peer_ni on a peer_net of a non-multi-rail peer.
1356 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1358 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1363 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1368 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1371 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1372 if (!lpni->lpni_peer_net)
1373 LIBCFS_FREE(lpni, sizeof(*lpni));
1375 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1376 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1382 * Update the primary NID of a peer, if possible.
1384 * Call with the lnet_api_mutex held.
1387 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1389 lnet_nid_t old = lp->lp_primary_nid;
1392 if (lp->lp_primary_nid == nid)
1394 rc = lnet_peer_add_nid(lp, nid, flags);
1397 lp->lp_primary_nid = nid;
1399 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1400 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1405 * lpni creation initiated due to traffic either sending or receiving.
1408 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1410 struct lnet_peer *lp;
1411 struct lnet_peer_net *lpn;
1412 struct lnet_peer_ni *lpni;
1416 if (nid == LNET_NID_ANY) {
1421 /* lnet_net_lock is not needed here because ln_api_lock is held */
1422 lpni = lnet_find_peer_ni_locked(nid);
1425 * We must have raced with another thread. Since we
1426 * know next to nothing about a peer_ni created by
1427 * traffic, we just assume everything is ok and
1430 lnet_peer_ni_decref_locked(lpni);
1434 /* Create peer, peer_net, and peer_ni. */
1436 lp = lnet_peer_alloc(nid);
1439 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1442 lpni = lnet_peer_ni_alloc(nid);
1445 if (pref != LNET_NID_ANY)
1446 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1448 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1451 LIBCFS_FREE(lpn, sizeof(*lpn));
1453 LIBCFS_FREE(lp, sizeof(*lp));
1455 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1460 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1462 * This API handles the following combinations:
1463 * Create a peer with its primary NI if only the prim_nid is provided
1464 * Add a NID to a peer identified by the prim_nid. The peer identified
1465 * by the prim_nid must already exist.
1466 * The peer being created may be non-MR.
1468 * The caller must hold ln_api_mutex. This prevents the peer from
1469 * being created/modified/deleted by a different thread.
1472 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1474 struct lnet_peer *lp = NULL;
1475 struct lnet_peer_ni *lpni;
1478 /* The prim_nid must always be specified */
1479 if (prim_nid == LNET_NID_ANY)
1482 flags = LNET_PEER_CONFIGURED;
1484 flags |= LNET_PEER_MULTI_RAIL;
1487 * If nid isn't specified, we must create a new peer with
1488 * prim_nid as its primary nid.
1490 if (nid == LNET_NID_ANY)
1491 return lnet_peer_add(prim_nid, flags);
1493 /* Look up the prim_nid, which must exist. */
1494 lpni = lnet_find_peer_ni_locked(prim_nid);
1497 lnet_peer_ni_decref_locked(lpni);
1498 lp = lpni->lpni_peer_net->lpn_peer;
1500 /* Peer must have been configured. */
1501 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1502 CDEBUG(D_NET, "peer %s was not configured\n",
1503 libcfs_nid2str(prim_nid));
1507 /* Primary NID must match */
1508 if (lp->lp_primary_nid != prim_nid) {
1509 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1510 libcfs_nid2str(prim_nid),
1511 libcfs_nid2str(lp->lp_primary_nid));
1515 /* Multi-Rail flag must match. */
1516 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1517 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1518 libcfs_nid2str(prim_nid));
1522 return lnet_peer_add_nid(lp, nid, flags);
1526 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1528 * This API handles the following combinations:
1529 * Delete a NI from a peer if both prim_nid and nid are provided.
1530 * Delete a peer if only prim_nid is provided.
1531 * Delete a peer if its primary nid is provided.
1533 * The caller must hold ln_api_mutex. This prevents the peer from
1534 * being modified/deleted by a different thread.
1537 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1539 struct lnet_peer *lp;
1540 struct lnet_peer_ni *lpni;
1543 if (prim_nid == LNET_NID_ANY)
1546 lpni = lnet_find_peer_ni_locked(prim_nid);
1549 lnet_peer_ni_decref_locked(lpni);
1550 lp = lpni->lpni_peer_net->lpn_peer;
1552 if (prim_nid != lp->lp_primary_nid) {
1553 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1554 libcfs_nid2str(prim_nid),
1555 libcfs_nid2str(lp->lp_primary_nid));
1559 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1560 return lnet_peer_del(lp);
1562 flags = LNET_PEER_CONFIGURED;
1563 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1564 flags |= LNET_PEER_MULTI_RAIL;
1566 return lnet_peer_del_nid(lp, nid, flags);
1570 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1572 struct lnet_peer_table *ptable;
1573 struct lnet_peer_net *lpn;
1575 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1577 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1578 LASSERT(list_empty(&lpni->lpni_txq));
1579 LASSERT(lpni->lpni_txqnob == 0);
1580 LASSERT(list_empty(&lpni->lpni_peer_nis));
1581 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1583 lpn = lpni->lpni_peer_net;
1584 lpni->lpni_peer_net = NULL;
1585 lpni->lpni_net = NULL;
1587 /* remove the peer ni from the zombie list */
1588 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1589 spin_lock(&ptable->pt_zombie_lock);
1590 list_del_init(&lpni->lpni_hashlist);
1591 ptable->pt_zombies--;
1592 spin_unlock(&ptable->pt_zombie_lock);
1594 if (lpni->lpni_pref_nnids > 1) {
1595 LIBCFS_FREE(lpni->lpni_pref.nids,
1596 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1598 LIBCFS_FREE(lpni, sizeof(*lpni));
1600 lnet_peer_net_decref_locked(lpn);
1603 struct lnet_peer_ni *
1604 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1606 struct lnet_peer_ni *lpni = NULL;
1609 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1610 return ERR_PTR(-ESHUTDOWN);
1613 * find if a peer_ni already exists.
1614 * If so then just return that.
1616 lpni = lnet_find_peer_ni_locked(nid);
1620 lnet_net_unlock(cpt);
1622 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1625 goto out_net_relock;
1628 lpni = lnet_find_peer_ni_locked(nid);
1638 * Get a peer_ni for the given nid, create it if necessary. Takes a
1639 * hold on the peer_ni.
1641 struct lnet_peer_ni *
1642 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1644 struct lnet_peer_ni *lpni = NULL;
1647 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1648 return ERR_PTR(-ESHUTDOWN);
1651 * find if a peer_ni already exists.
1652 * If so then just return that.
1654 lpni = lnet_find_peer_ni_locked(nid);
1660 * use the lnet_api_mutex to serialize the creation of the peer_ni
1661 * and the creation/deletion of the local ni/net. When a local ni is
1662 * created, if there exists a set of peer_nis on that network,
1663 * they need to be traversed and updated. When a local NI is
1664 * deleted, which could result in a network being deleted, then
1665 * all peer nis on that network need to be removed as well.
1667 * Creation through traffic should also be serialized with
1668 * creation through DLC.
1670 lnet_net_unlock(cpt);
1671 mutex_lock(&the_lnet.ln_api_mutex);
1673 * Shutdown is only set under the ln_api_lock, so a single
1674 * check here is sufficent.
1676 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1677 lpni = ERR_PTR(-ESHUTDOWN);
1678 goto out_mutex_unlock;
1681 rc = lnet_peer_ni_traffic_add(nid, pref);
1684 goto out_mutex_unlock;
1687 lpni = lnet_find_peer_ni_locked(nid);
1691 mutex_unlock(&the_lnet.ln_api_mutex);
1694 /* Lock has been dropped, check again for shutdown. */
1695 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1697 lnet_peer_ni_decref_locked(lpni);
1698 lpni = ERR_PTR(-ESHUTDOWN);
1709 * Is a peer uptodate from the point of view of discovery?
1711 * If it is currently being processed, obviously not.
1712 * A forced Ping or Push is also handled by the discovery thread.
1714 * Otherwise look at whether the peer needs rediscovering.
1717 lnet_peer_is_uptodate(struct lnet_peer *lp)
1721 spin_lock(&lp->lp_lock);
1722 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1723 LNET_PEER_FORCE_PING |
1724 LNET_PEER_FORCE_PUSH)) {
1726 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1728 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1729 if (lnet_peer_discovery_disabled)
1733 } else if (lnet_peer_needs_push(lp)) {
1735 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1736 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1743 spin_unlock(&lp->lp_lock);
1749 * Queue a peer for the attention of the discovery thread. Call with
1750 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1751 * -EALREADY if the peer was already queued.
1753 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1757 spin_lock(&lp->lp_lock);
1758 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1759 lp->lp_state |= LNET_PEER_DISCOVERING;
1760 spin_unlock(&lp->lp_lock);
1761 if (list_empty(&lp->lp_dc_list)) {
1762 lnet_peer_addref_locked(lp);
1763 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1764 wake_up(&the_lnet.ln_dc_waitq);
1770 CDEBUG(D_NET, "Queue peer %s: %d\n",
1771 libcfs_nid2str(lp->lp_primary_nid), rc);
1777 * Discovery of a peer is complete. Wake all waiters on the peer.
1778 * Call with lnet_net_lock/EX held.
1780 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1782 struct lnet_msg *msg, *tmp;
1784 struct list_head pending_msgs;
1786 INIT_LIST_HEAD(&pending_msgs);
1788 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1789 libcfs_nid2str(lp->lp_primary_nid));
1791 list_del_init(&lp->lp_dc_list);
1792 spin_lock(&lp->lp_lock);
1793 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1794 spin_unlock(&lp->lp_lock);
1795 wake_up_all(&lp->lp_dc_waitq);
1797 lnet_net_unlock(LNET_LOCK_EX);
1799 /* iterate through all pending messages and send them again */
1800 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1801 list_del_init(&msg->msg_list);
1802 if (lp->lp_dc_error) {
1803 lnet_finalize(msg, lp->lp_dc_error);
1807 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1808 lnet_msgtyp2str(msg->msg_type),
1809 libcfs_id2str(msg->msg_target));
1810 rc = lnet_send(msg->msg_src_nid_param, msg,
1811 msg->msg_rtr_nid_param);
1813 CNETERR("Error sending %s to %s: %d\n",
1814 lnet_msgtyp2str(msg->msg_type),
1815 libcfs_id2str(msg->msg_target), rc);
1816 lnet_finalize(msg, rc);
1819 lnet_net_lock(LNET_LOCK_EX);
1820 lnet_peer_decref_locked(lp);
1824 * Handle inbound push.
1825 * Like any event handler, called with lnet_res_lock/CPT held.
1827 void lnet_peer_push_event(struct lnet_event *ev)
1829 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1830 struct lnet_peer *lp;
1832 /* lnet_find_peer() adds a refcount */
1833 lp = lnet_find_peer(ev->source.nid);
1835 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1836 libcfs_nid2str(ev->initiator.nid),
1837 libcfs_nid2str(ev->source.nid));
1841 /* Ensure peer state remains consistent while we modify it. */
1842 spin_lock(&lp->lp_lock);
1845 * If some kind of error happened the contents of the message
1846 * cannot be used. Clear the NIDS_UPTODATE and set the
1847 * FORCE_PING flag to trigger a ping.
1850 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1851 lp->lp_state |= LNET_PEER_FORCE_PING;
1852 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1854 libcfs_nid2str(lp->lp_primary_nid),
1855 libcfs_nid2str(ev->source.nid));
1860 * A push with invalid or corrupted info. Clear the UPTODATE
1861 * flag to trigger a ping.
1863 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1864 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1865 lp->lp_state |= LNET_PEER_FORCE_PING;
1866 CDEBUG(D_NET, "Corrupted Push from %s\n",
1867 libcfs_nid2str(lp->lp_primary_nid));
1872 * Make sure we'll allocate the correct size ping buffer when
1875 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1876 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1879 * A non-Multi-Rail peer is not supposed to be capable of
1882 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1883 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1884 libcfs_nid2str(lp->lp_primary_nid));
1889 * Check the MULTIRAIL flag. Complain if the peer was DLC
1890 * configured without it.
1892 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1893 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1894 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1895 libcfs_nid2str(lp->lp_primary_nid));
1897 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1898 lnet_peer_clr_non_mr_pref_nids(lp);
1903 * The peer may have discovery disabled at its end. Set
1904 * NO_DISCOVERY as appropriate.
1906 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1907 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1908 libcfs_nid2str(lp->lp_primary_nid));
1909 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1910 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1911 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1912 libcfs_nid2str(lp->lp_primary_nid));
1913 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1917 * Check for truncation of the Put message. Clear the
1918 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1919 * and tell discovery to allocate a bigger buffer.
1921 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1922 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1923 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1924 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1925 lp->lp_state |= LNET_PEER_FORCE_PING;
1926 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1927 libcfs_nid2str(lp->lp_primary_nid),
1928 pbuf->pb_info.pi_nnis);
1933 * Check whether the Put data is stale. Stale data can just be
1936 if (pbuf->pb_info.pi_nnis > 1 &&
1937 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1938 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1939 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1940 libcfs_nid2str(lp->lp_primary_nid),
1941 LNET_PING_BUFFER_SEQNO(pbuf),
1947 * Check whether the Put data is new, in which case we clear
1948 * the UPTODATE flag and prepare to process it.
1950 * If the Put data is current, and the peer is UPTODATE then
1951 * we assome everything is all right and drop the data as
1954 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1955 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1956 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1957 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1958 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1959 libcfs_nid2str(lp->lp_primary_nid),
1960 LNET_PING_BUFFER_SEQNO(pbuf),
1966 * If there is data present that hasn't been processed yet,
1967 * we'll replace it if the Put contained newer data and it
1968 * fits. We're racing with a Ping or earlier Push in this
1971 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1972 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1973 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1974 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1975 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1976 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1977 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1978 libcfs_nid2str(lp->lp_primary_nid),
1979 LNET_PING_BUFFER_SEQNO(pbuf),
1980 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1986 * Allocate a buffer to copy the data. On a failure we drop
1987 * the Push and set FORCE_PING to force the discovery
1988 * thread to fix the problem by pinging the peer.
1990 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
1992 lp->lp_state |= LNET_PEER_FORCE_PING;
1993 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
1994 libcfs_nid2str(lp->lp_primary_nid),
1995 LNET_PING_BUFFER_SEQNO(pbuf));
2000 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2001 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2002 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2003 CDEBUG(D_NET, "Received Push %s %u\n",
2004 libcfs_nid2str(lp->lp_primary_nid),
2005 LNET_PING_BUFFER_SEQNO(pbuf));
2009 * Queue the peer for discovery if not done, force it on the request
2010 * queue and wake the discovery thread if the peer was already queued,
2011 * because its status changed.
2013 spin_unlock(&lp->lp_lock);
2014 lnet_net_lock(LNET_LOCK_EX);
2015 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2016 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2017 wake_up(&the_lnet.ln_dc_waitq);
2019 /* Drop refcount from lookup */
2020 lnet_peer_decref_locked(lp);
2021 lnet_net_unlock(LNET_LOCK_EX);
2025 * Clear the discovery error state, unless we're already discovering
2026 * this peer, in which case the error is current.
2028 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2030 spin_lock(&lp->lp_lock);
2031 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2032 lp->lp_dc_error = 0;
2033 spin_unlock(&lp->lp_lock);
2037 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2038 * dropped/retaken within this function. An lnet_peer_ni is passed in
2039 * because discovery could tear down an lnet_peer.
2042 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2045 struct lnet_peer *lp;
2049 lnet_net_unlock(cpt);
2050 lnet_net_lock(LNET_LOCK_EX);
2051 lp = lpni->lpni_peer_net->lpn_peer;
2052 lnet_peer_clear_discovery_error(lp);
2055 * We're willing to be interrupted. The lpni can become a
2056 * zombie if we race with DLC, so we must check for that.
2059 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2060 if (signal_pending(current))
2062 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2064 if (lp->lp_dc_error)
2066 if (lnet_peer_is_uptodate(lp))
2068 lnet_peer_queue_for_discovery(lp);
2070 * if caller requested a non-blocking operation then
2071 * return immediately. Once discovery is complete then the
2072 * peer ref will be decremented and any pending messages
2073 * that were stopped due to discovery will be transmitted.
2078 lnet_peer_addref_locked(lp);
2079 lnet_net_unlock(LNET_LOCK_EX);
2081 finish_wait(&lp->lp_dc_waitq, &wait);
2082 lnet_net_lock(LNET_LOCK_EX);
2083 lnet_peer_decref_locked(lp);
2084 /* Peer may have changed */
2085 lp = lpni->lpni_peer_net->lpn_peer;
2087 finish_wait(&lp->lp_dc_waitq, &wait);
2089 lnet_net_unlock(LNET_LOCK_EX);
2093 * If the peer has changed after we've discovered the older peer,
2094 * then we need to discovery the new peer to make sure the
2095 * interface information is up to date
2097 if (lp != lpni->lpni_peer_net->lpn_peer)
2100 if (signal_pending(current))
2102 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2104 else if (lp->lp_dc_error)
2105 rc = lp->lp_dc_error;
2107 CDEBUG(D_NET, "non-blocking discovery\n");
2108 else if (!lnet_peer_is_uptodate(lp))
2111 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2112 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2113 libcfs_nid2str(lpni->lpni_nid), rc,
2114 (!block) ? "pending discovery" : "discovery complete");
2119 /* Handle an incoming ack for a push. */
2121 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2123 struct lnet_ping_buffer *pbuf;
2125 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2126 spin_lock(&lp->lp_lock);
2127 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2128 lp->lp_push_error = ev->status;
2130 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2132 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2133 spin_unlock(&lp->lp_lock);
2135 CDEBUG(D_NET, "peer %s ev->status %d\n",
2136 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2139 /* Handle a Reply message. This is the reply to a Ping message. */
2141 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2143 struct lnet_ping_buffer *pbuf;
2146 spin_lock(&lp->lp_lock);
2149 * If some kind of error happened the contents of message
2150 * cannot be used. Set PING_FAILED to trigger a retry.
2153 lp->lp_state |= LNET_PEER_PING_FAILED;
2154 lp->lp_ping_error = ev->status;
2155 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2157 libcfs_nid2str(lp->lp_primary_nid),
2158 libcfs_nid2str(ev->source.nid));
2162 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2163 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2164 lnet_swap_pinginfo(pbuf);
2167 * A reply with invalid or corrupted info. Set PING_FAILED to
2170 rc = lnet_ping_info_validate(&pbuf->pb_info);
2172 lp->lp_state |= LNET_PEER_PING_FAILED;
2173 lp->lp_ping_error = 0;
2174 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2175 libcfs_nid2str(lp->lp_primary_nid), rc);
2180 * Update the MULTI_RAIL flag based on the reply. If the peer
2181 * was configured with DLC then the setting should match what
2184 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2185 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2186 /* Everything's fine */
2187 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2188 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2189 libcfs_nid2str(lp->lp_primary_nid));
2191 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2192 lnet_peer_clr_non_mr_pref_nids(lp);
2194 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2195 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2196 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2197 libcfs_nid2str(lp->lp_primary_nid));
2199 CERROR("Multi-Rail state vanished from %s\n",
2200 libcfs_nid2str(lp->lp_primary_nid));
2201 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2206 * Make sure we'll allocate the correct size ping buffer when
2209 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2210 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2213 * The peer may have discovery disabled at its end. Set
2214 * NO_DISCOVERY as appropriate.
2216 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2217 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2218 libcfs_nid2str(lp->lp_primary_nid));
2219 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2220 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2221 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2222 libcfs_nid2str(lp->lp_primary_nid));
2223 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2227 * Check for truncation of the Reply. Clear PING_SENT and set
2228 * PING_FAILED to trigger a retry.
2230 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2231 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2232 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2233 lp->lp_state |= LNET_PEER_PING_FAILED;
2234 lp->lp_ping_error = 0;
2235 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2236 libcfs_nid2str(lp->lp_primary_nid),
2237 pbuf->pb_info.pi_nnis);
2242 * Check the sequence numbers in the reply. These are only
2243 * available if the reply came from a Multi-Rail peer.
2245 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2246 pbuf->pb_info.pi_nnis > 1 &&
2247 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2248 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2249 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2250 libcfs_nid2str(lp->lp_primary_nid),
2251 LNET_PING_BUFFER_SEQNO(pbuf),
2256 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2257 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2260 /* We're happy with the state of the data in the buffer. */
2261 CDEBUG(D_NET, "peer %s data present %u\n",
2262 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2263 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2264 lnet_ping_buffer_decref(lp->lp_data);
2266 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2267 lnet_ping_buffer_addref(pbuf);
2270 lp->lp_state &= ~LNET_PEER_PING_SENT;
2271 spin_unlock(&lp->lp_lock);
2275 * Send event handling. Only matters for error cases, where we clean
2276 * up state on the peer and peer_ni that would otherwise be updated in
2277 * the REPLY event handler for a successful Ping, and the ACK event
2278 * handler for a successful Push.
2281 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2288 spin_lock(&lp->lp_lock);
2289 if (ev->msg_type == LNET_MSG_GET) {
2290 lp->lp_state &= ~LNET_PEER_PING_SENT;
2291 lp->lp_state |= LNET_PEER_PING_FAILED;
2292 lp->lp_ping_error = ev->status;
2293 } else { /* ev->msg_type == LNET_MSG_PUT */
2294 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2295 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2296 lp->lp_push_error = ev->status;
2298 spin_unlock(&lp->lp_lock);
2299 rc = LNET_REDISCOVER_PEER;
2301 CDEBUG(D_NET, "%s Send to %s: %d\n",
2302 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2303 libcfs_nid2str(ev->target.nid), rc);
2308 * Unlink event handling. This event is only seen if a call to
2309 * LNetMDUnlink() caused the event to be unlinked. If this call was
2310 * made after the event was set up in LNetGet() or LNetPut() then we
2311 * assume the Ping or Push timed out.
2314 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2316 spin_lock(&lp->lp_lock);
2317 /* We've passed through LNetGet() */
2318 if (lp->lp_state & LNET_PEER_PING_SENT) {
2319 lp->lp_state &= ~LNET_PEER_PING_SENT;
2320 lp->lp_state |= LNET_PEER_PING_FAILED;
2321 lp->lp_ping_error = -ETIMEDOUT;
2322 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2323 libcfs_nid2str(lp->lp_primary_nid));
2325 /* We've passed through LNetPut() */
2326 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2327 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2328 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2329 lp->lp_push_error = -ETIMEDOUT;
2330 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2331 libcfs_nid2str(lp->lp_primary_nid));
2333 spin_unlock(&lp->lp_lock);
2337 * Event handler for the discovery EQ.
2339 * Called with lnet_res_lock(cpt) held. The cpt is the
2340 * lnet_cpt_of_cookie() of the md handle cookie.
2342 static void lnet_discovery_event_handler(struct lnet_event *event)
2344 struct lnet_peer *lp = event->md.user_ptr;
2345 struct lnet_ping_buffer *pbuf;
2348 /* discovery needs to take another look */
2349 rc = LNET_REDISCOVER_PEER;
2351 CDEBUG(D_NET, "Received event: %d\n", event->type);
2353 switch (event->type) {
2354 case LNET_EVENT_ACK:
2355 lnet_discovery_event_ack(lp, event);
2357 case LNET_EVENT_REPLY:
2358 lnet_discovery_event_reply(lp, event);
2360 case LNET_EVENT_SEND:
2361 /* Only send failure triggers a retry. */
2362 rc = lnet_discovery_event_send(lp, event);
2364 case LNET_EVENT_UNLINK:
2365 /* LNetMDUnlink() was called */
2366 lnet_discovery_event_unlink(lp, event);
2369 /* Invalid events. */
2372 lnet_net_lock(LNET_LOCK_EX);
2373 if (event->unlinked) {
2374 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2375 lnet_ping_buffer_decref(pbuf);
2376 lnet_peer_decref_locked(lp);
2379 /* put peer back at end of request queue, if discovery not already
2381 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2382 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2383 wake_up(&the_lnet.ln_dc_waitq);
2385 lnet_net_unlock(LNET_LOCK_EX);
2389 * Build a peer from incoming data.
2391 * The NIDs in the incoming data are supposed to be structured as follows:
2394 * - other NIDs in same net
2395 * - NIDs in second net
2396 * - NIDs in third net
2398 * This due to the way the list of NIDs in the data is created.
2400 * Note that this function will mark the peer uptodate unless an
2401 * ENOMEM is encontered. All other errors are due to a conflict
2402 * between the DLC configuration and what discovery sees. We treat DLC
2403 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2404 * peer from becoming stuck in discovery.
2406 static int lnet_peer_merge_data(struct lnet_peer *lp,
2407 struct lnet_ping_buffer *pbuf)
2409 struct lnet_peer_ni *lpni;
2410 lnet_nid_t *curnis = NULL;
2411 lnet_nid_t *addnis = NULL;
2412 lnet_nid_t *delnis = NULL;
2422 flags = LNET_PEER_DISCOVERED;
2423 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2424 flags |= LNET_PEER_MULTI_RAIL;
2426 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2427 LIBCFS_ALLOC(curnis, nnis * sizeof(lnet_nid_t));
2428 LIBCFS_ALLOC(addnis, nnis * sizeof(lnet_nid_t));
2429 LIBCFS_ALLOC(delnis, nnis * sizeof(lnet_nid_t));
2430 if (!curnis || !addnis || !delnis) {
2438 /* Construct the list of NIDs present in peer. */
2440 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2441 curnis[ncurnis++] = lpni->lpni_nid;
2444 * Check for NIDs in pbuf not present in curnis[].
2445 * The loop starts at 1 to skip the loopback NID.
2447 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2448 for (j = 0; j < ncurnis; j++)
2449 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2452 addnis[naddnis++] = pbuf->pb_info.pi_ni[i].ns_nid;
2455 * Check for NIDs in curnis[] not present in pbuf.
2456 * The nested loop starts at 1 to skip the loopback NID.
2458 * But never add the loopback NID to delnis[]: if it is
2459 * present in curnis[] then this peer is for this node.
2461 for (i = 0; i < ncurnis; i++) {
2462 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2464 for (j = 1; j < pbuf->pb_info.pi_nnis; j++)
2465 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid)
2467 if (j == pbuf->pb_info.pi_nnis)
2468 delnis[ndelnis++] = curnis[i];
2471 for (i = 0; i < naddnis; i++) {
2472 rc = lnet_peer_add_nid(lp, addnis[i], flags);
2474 CERROR("Error adding NID %s to peer %s: %d\n",
2475 libcfs_nid2str(addnis[i]),
2476 libcfs_nid2str(lp->lp_primary_nid), rc);
2481 for (i = 0; i < ndelnis; i++) {
2482 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2484 CERROR("Error deleting NID %s from peer %s: %d\n",
2485 libcfs_nid2str(delnis[i]),
2486 libcfs_nid2str(lp->lp_primary_nid), rc);
2492 * Errors other than -ENOMEM are due to peers having been
2493 * configured with DLC. Ignore these because DLC overrides
2498 LIBCFS_FREE(curnis, nnis * sizeof(lnet_nid_t));
2499 LIBCFS_FREE(addnis, nnis * sizeof(lnet_nid_t));
2500 LIBCFS_FREE(delnis, nnis * sizeof(lnet_nid_t));
2501 lnet_ping_buffer_decref(pbuf);
2502 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2505 spin_lock(&lp->lp_lock);
2506 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2507 lp->lp_state |= LNET_PEER_FORCE_PING;
2508 spin_unlock(&lp->lp_lock);
2514 * The data in pbuf says lp is its primary peer, but the data was
2515 * received by a different peer. Try to update lp with the data.
2518 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2520 struct lnet_handle_md mdh;
2522 /* Queue lp for discovery, and force it on the request queue. */
2523 lnet_net_lock(LNET_LOCK_EX);
2524 if (lnet_peer_queue_for_discovery(lp))
2525 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2526 lnet_net_unlock(LNET_LOCK_EX);
2528 LNetInvalidateMDHandle(&mdh);
2531 * Decide whether we can move the peer to the DATA_PRESENT state.
2533 * We replace stale data for a multi-rail peer, repair PING_FAILED
2534 * status, and preempt FORCE_PING.
2536 * If after that we have DATA_PRESENT, we merge it into this peer.
2538 spin_lock(&lp->lp_lock);
2539 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2540 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2541 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2542 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2543 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2544 lnet_ping_buffer_decref(pbuf);
2549 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2550 lnet_ping_buffer_decref(lp->lp_data);
2552 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2554 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2555 mdh = lp->lp_ping_mdh;
2556 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2557 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2558 lp->lp_ping_error = 0;
2560 if (lp->lp_state & LNET_PEER_FORCE_PING)
2561 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2562 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2563 spin_unlock(&lp->lp_lock);
2565 if (!LNetMDHandleIsInvalid(mdh))
2569 return lnet_peer_merge_data(lp, pbuf);
2571 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2576 * Update a peer using the data received.
2578 static int lnet_peer_data_present(struct lnet_peer *lp)
2579 __must_hold(&lp->lp_lock)
2581 struct lnet_ping_buffer *pbuf;
2582 struct lnet_peer_ni *lpni;
2583 lnet_nid_t nid = LNET_NID_ANY;
2589 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2590 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2591 spin_unlock(&lp->lp_lock);
2594 * Modifications of peer structures are done while holding the
2595 * ln_api_mutex. A global lock is required because we may be
2596 * modifying multiple peer structures, and a mutex greatly
2597 * simplifies memory management.
2599 * The actual changes to the data structures must also protect
2600 * against concurrent lookups, for which the lnet_net_lock in
2601 * LNET_LOCK_EX mode is used.
2603 mutex_lock(&the_lnet.ln_api_mutex);
2604 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2610 * If this peer is not on the peer list then it is being torn
2611 * down, and our reference count may be all that is keeping it
2612 * alive. Don't do any work on it.
2614 if (list_empty(&lp->lp_peer_list))
2617 flags = LNET_PEER_DISCOVERED;
2618 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2619 flags |= LNET_PEER_MULTI_RAIL;
2622 * Check whether the primary NID in the message matches the
2623 * primary NID of the peer. If it does, update the peer, if
2624 * it it does not, check whether there is already a peer with
2625 * that primary NID. If no such peer exists, try to update
2626 * the primary NID of the current peer (allowed if it was
2627 * created due to message traffic) and complete the update.
2628 * If the peer did exist, hand off the data to it.
2630 * The peer for the loopback interface is a special case: this
2631 * is the peer for the local node, and we want to set its
2632 * primary NID to the correct value here. Moreover, this peer
2633 * can show up with only the loopback NID in the ping buffer.
2635 if (pbuf->pb_info.pi_nnis <= 1)
2637 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2638 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2639 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2641 rc = lnet_peer_merge_data(lp, pbuf);
2642 } else if (lp->lp_primary_nid == nid) {
2643 rc = lnet_peer_merge_data(lp, pbuf);
2645 lpni = lnet_find_peer_ni_locked(nid);
2647 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2649 CERROR("Primary NID error %s versus %s: %d\n",
2650 libcfs_nid2str(lp->lp_primary_nid),
2651 libcfs_nid2str(nid), rc);
2653 rc = lnet_peer_merge_data(lp, pbuf);
2656 rc = lnet_peer_set_primary_data(
2657 lpni->lpni_peer_net->lpn_peer, pbuf);
2658 lnet_peer_ni_decref_locked(lpni);
2662 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2663 mutex_unlock(&the_lnet.ln_api_mutex);
2665 spin_lock(&lp->lp_lock);
2666 /* Tell discovery to re-check the peer immediately. */
2668 rc = LNET_REDISCOVER_PEER;
2673 * A ping failed. Clear the PING_FAILED state and set the
2674 * FORCE_PING state, to ensure a retry even if discovery is
2675 * disabled. This avoids being left with incorrect state.
2677 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2678 __must_hold(&lp->lp_lock)
2680 struct lnet_handle_md mdh;
2683 mdh = lp->lp_ping_mdh;
2684 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2685 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2686 lp->lp_state |= LNET_PEER_FORCE_PING;
2687 rc = lp->lp_ping_error;
2688 lp->lp_ping_error = 0;
2689 spin_unlock(&lp->lp_lock);
2691 if (!LNetMDHandleIsInvalid(mdh))
2694 CDEBUG(D_NET, "peer %s:%d\n",
2695 libcfs_nid2str(lp->lp_primary_nid), rc);
2697 spin_lock(&lp->lp_lock);
2698 return rc ? rc : LNET_REDISCOVER_PEER;
2702 * Select NID to send a Ping or Push to.
2704 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2706 struct lnet_peer_ni *lpni;
2708 /* Look for a direct-connected NID for this peer. */
2710 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2711 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2716 return lpni->lpni_nid;
2718 /* Look for a routed-connected NID for this peer. */
2720 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2721 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2726 return lpni->lpni_nid;
2728 return LNET_NID_ANY;
2731 /* Active side of ping. */
2732 static int lnet_peer_send_ping(struct lnet_peer *lp)
2733 __must_hold(&lp->lp_lock)
2740 lp->lp_state |= LNET_PEER_PING_SENT;
2741 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2742 spin_unlock(&lp->lp_lock);
2744 cpt = lnet_net_lock_current();
2745 /* Refcount for MD. */
2746 lnet_peer_addref_locked(lp);
2747 pnid = lnet_peer_select_nid(lp);
2748 lnet_net_unlock(cpt);
2750 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2752 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2753 the_lnet.ln_dc_eqh, false);
2756 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2757 * refcount on the peer, otherwise LNetMDUnlink will be called
2758 * which will eventually do that.
2762 lnet_peer_decref_locked(lp);
2763 lnet_net_unlock(cpt);
2764 rc = -rc; /* change the rc to negative value */
2766 } else if (rc < 0) {
2770 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2772 spin_lock(&lp->lp_lock);
2776 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2778 * The errors that get us here are considered hard errors and
2779 * cause Discovery to terminate. So we clear PING_SENT, but do
2780 * not set either PING_FAILED or FORCE_PING. In fact we need
2781 * to clear PING_FAILED, because the unlink event handler will
2782 * have set it if we called LNetMDUnlink() above.
2784 spin_lock(&lp->lp_lock);
2785 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2790 * This function exists because you cannot call LNetMDUnlink() from an
2793 static int lnet_peer_push_failed(struct lnet_peer *lp)
2794 __must_hold(&lp->lp_lock)
2796 struct lnet_handle_md mdh;
2799 mdh = lp->lp_push_mdh;
2800 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2801 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2802 rc = lp->lp_push_error;
2803 lp->lp_push_error = 0;
2804 spin_unlock(&lp->lp_lock);
2806 if (!LNetMDHandleIsInvalid(mdh))
2809 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2810 spin_lock(&lp->lp_lock);
2811 return rc ? rc : LNET_REDISCOVER_PEER;
2814 /* Active side of push. */
2815 static int lnet_peer_send_push(struct lnet_peer *lp)
2816 __must_hold(&lp->lp_lock)
2818 struct lnet_ping_buffer *pbuf;
2819 struct lnet_process_id id;
2824 /* Don't push to a non-multi-rail peer. */
2825 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2826 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2830 lp->lp_state |= LNET_PEER_PUSH_SENT;
2831 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2832 spin_unlock(&lp->lp_lock);
2834 cpt = lnet_net_lock_current();
2835 pbuf = the_lnet.ln_ping_target;
2836 lnet_ping_buffer_addref(pbuf);
2837 lnet_net_unlock(cpt);
2839 /* Push source MD */
2840 md.start = &pbuf->pb_info;
2841 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2842 md.threshold = 2; /* Put/Ack */
2845 md.eq_handle = the_lnet.ln_dc_eqh;
2848 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2850 lnet_ping_buffer_decref(pbuf);
2851 CERROR("Can't bind push source MD: %d\n", rc);
2854 cpt = lnet_net_lock_current();
2855 /* Refcount for MD. */
2856 lnet_peer_addref_locked(lp);
2857 id.pid = LNET_PID_LUSTRE;
2858 id.nid = lnet_peer_select_nid(lp);
2859 lnet_net_unlock(cpt);
2861 if (id.nid == LNET_NID_ANY) {
2866 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2867 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2868 LNET_PROTO_PING_MATCHBITS, 0, 0);
2873 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2875 spin_lock(&lp->lp_lock);
2879 LNetMDUnlink(lp->lp_push_mdh);
2880 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2882 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2884 * The errors that get us here are considered hard errors and
2885 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2886 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2887 * because the unlink event handler will have set it if we
2888 * called LNetMDUnlink() above.
2890 spin_lock(&lp->lp_lock);
2891 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2896 * An unrecoverable error was encountered during discovery.
2897 * Set error status in peer and abort discovery.
2899 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2901 CDEBUG(D_NET, "Discovery error %s: %d\n",
2902 libcfs_nid2str(lp->lp_primary_nid), error);
2904 spin_lock(&lp->lp_lock);
2905 lp->lp_dc_error = error;
2906 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2907 lp->lp_state |= LNET_PEER_REDISCOVER;
2908 spin_unlock(&lp->lp_lock);
2912 * Mark the peer as discovered.
2914 static int lnet_peer_discovered(struct lnet_peer *lp)
2915 __must_hold(&lp->lp_lock)
2917 lp->lp_state |= LNET_PEER_DISCOVERED;
2918 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2919 LNET_PEER_REDISCOVER);
2921 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2927 * Mark the peer as to be rediscovered.
2929 static int lnet_peer_rediscover(struct lnet_peer *lp)
2930 __must_hold(&lp->lp_lock)
2932 lp->lp_state |= LNET_PEER_REDISCOVER;
2933 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2935 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2941 * Discovering this peer is taking too long. Cancel any Ping or Push
2942 * that discovery is waiting on by unlinking the relevant MDs. The
2943 * lnet_discovery_event_handler() will proceed from here and complete
2946 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
2948 struct lnet_handle_md ping_mdh;
2949 struct lnet_handle_md push_mdh;
2951 LNetInvalidateMDHandle(&ping_mdh);
2952 LNetInvalidateMDHandle(&push_mdh);
2954 spin_lock(&lp->lp_lock);
2955 if (lp->lp_state & LNET_PEER_PING_SENT) {
2956 ping_mdh = lp->lp_ping_mdh;
2957 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2959 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2960 push_mdh = lp->lp_push_mdh;
2961 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2963 spin_unlock(&lp->lp_lock);
2965 if (!LNetMDHandleIsInvalid(ping_mdh))
2966 LNetMDUnlink(ping_mdh);
2967 if (!LNetMDHandleIsInvalid(push_mdh))
2968 LNetMDUnlink(push_mdh);
2972 * Wait for work to be queued or some other change that must be
2973 * attended to. Returns non-zero if the discovery thread should shut
2976 static int lnet_peer_discovery_wait_for_work(void)
2983 cpt = lnet_net_lock_current();
2985 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
2986 TASK_INTERRUPTIBLE);
2987 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2989 if (lnet_push_target_resize_needed())
2991 if (!list_empty(&the_lnet.ln_dc_request))
2993 if (!list_empty(&the_lnet.ln_msg_resend))
2995 lnet_net_unlock(cpt);
2998 * wakeup max every second to check if there are peers that
2999 * have been stuck on the working queue for greater than
3002 schedule_timeout(cfs_time_seconds(1));
3003 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3004 cpt = lnet_net_lock_current();
3006 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3008 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3011 lnet_net_unlock(cpt);
3013 CDEBUG(D_NET, "woken: %d\n", rc);
3019 * Messages that were pending on a destroyed peer will be put on a global
3020 * resend list. The message resend list will be checked by
3021 * the discovery thread when it wakes up, and will resend messages. These
3022 * messages can still be sendable in the case the lpni which was the initial
3023 * cause of the message re-queue was transfered to another peer.
3025 * It is possible that LNet could be shutdown while we're iterating
3026 * through the list. lnet_shudown_lndnets() will attempt to access the
3027 * resend list, but will have to wait until the spinlock is released, by
3028 * which time there shouldn't be any more messages on the resend list.
3029 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3030 * for the messages so they can be released. The other case is that
3031 * lnet_shudown_lndnets() can finalize all the messages before this
3032 * function can visit the resend list, in which case this function will be
3035 static void lnet_resend_msgs(void)
3037 struct lnet_msg *msg, *tmp;
3038 struct list_head resend;
3041 INIT_LIST_HEAD(&resend);
3043 spin_lock(&the_lnet.ln_msg_resend_lock);
3044 list_splice(&the_lnet.ln_msg_resend, &resend);
3045 spin_unlock(&the_lnet.ln_msg_resend_lock);
3047 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3048 list_del_init(&msg->msg_list);
3049 rc = lnet_send(msg->msg_src_nid_param, msg,
3050 msg->msg_rtr_nid_param);
3052 CNETERR("Error sending %s to %s: %d\n",
3053 lnet_msgtyp2str(msg->msg_type),
3054 libcfs_id2str(msg->msg_target), rc);
3055 lnet_finalize(msg, rc);
3060 /* The discovery thread. */
3061 static int lnet_peer_discovery(void *arg)
3063 struct lnet_peer *lp;
3066 CDEBUG(D_NET, "started\n");
3067 cfs_block_allsigs();
3070 if (lnet_peer_discovery_wait_for_work())
3075 if (lnet_push_target_resize_needed())
3076 lnet_push_target_resize();
3078 lnet_net_lock(LNET_LOCK_EX);
3079 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3083 * Process all incoming discovery work requests. When
3084 * discovery must wait on a peer to change state, it
3085 * is added to the tail of the ln_dc_working queue. A
3086 * timestamp keeps track of when the peer was added,
3087 * so we can time out discovery requests that take too
3090 while (!list_empty(&the_lnet.ln_dc_request)) {
3091 lp = list_first_entry(&the_lnet.ln_dc_request,
3092 struct lnet_peer, lp_dc_list);
3093 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3095 * set the time the peer was put on the dc_working
3096 * queue. It shouldn't remain on the queue
3097 * forever, in case the GET message (for ping)
3098 * doesn't get a REPLY or the PUT message (for
3099 * push) doesn't get an ACK.
3101 lp->lp_last_queued = ktime_get_real_seconds();
3102 lnet_net_unlock(LNET_LOCK_EX);
3105 * Select an action depending on the state of
3106 * the peer and whether discovery is disabled.
3107 * The check whether discovery is disabled is
3108 * done after the code that handles processing
3109 * for arrived data, cleanup for failures, and
3110 * forcing a Ping or Push.
3112 spin_lock(&lp->lp_lock);
3113 CDEBUG(D_NET, "peer %s state %#x\n",
3114 libcfs_nid2str(lp->lp_primary_nid),
3116 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3117 rc = lnet_peer_data_present(lp);
3118 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3119 rc = lnet_peer_ping_failed(lp);
3120 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3121 rc = lnet_peer_push_failed(lp);
3122 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3123 rc = lnet_peer_send_ping(lp);
3124 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3125 rc = lnet_peer_send_push(lp);
3126 else if (lnet_peer_discovery_disabled)
3127 rc = lnet_peer_rediscover(lp);
3128 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3129 rc = lnet_peer_send_ping(lp);
3130 else if (lnet_peer_needs_push(lp))
3131 rc = lnet_peer_send_push(lp);
3133 rc = lnet_peer_discovered(lp);
3134 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3135 libcfs_nid2str(lp->lp_primary_nid),
3137 spin_unlock(&lp->lp_lock);
3139 lnet_net_lock(LNET_LOCK_EX);
3140 if (rc == LNET_REDISCOVER_PEER) {
3141 list_move(&lp->lp_dc_list,
3142 &the_lnet.ln_dc_request);
3144 lnet_peer_discovery_error(lp, rc);
3146 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3147 lnet_peer_discovery_complete(lp);
3148 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3152 lnet_net_unlock(LNET_LOCK_EX);
3155 CDEBUG(D_NET, "stopping\n");
3157 * Clean up before telling lnet_peer_discovery_stop() that
3158 * we're done. Use wake_up() below to somewhat reduce the
3159 * size of the thundering herd if there are multiple threads
3160 * waiting on discovery of a single peer.
3163 /* Queue cleanup 1: stop all pending pings and pushes. */
3164 lnet_net_lock(LNET_LOCK_EX);
3165 while (!list_empty(&the_lnet.ln_dc_working)) {
3166 lp = list_first_entry(&the_lnet.ln_dc_working,
3167 struct lnet_peer, lp_dc_list);
3168 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3169 lnet_net_unlock(LNET_LOCK_EX);
3170 lnet_peer_cancel_discovery(lp);
3171 lnet_net_lock(LNET_LOCK_EX);
3173 lnet_net_unlock(LNET_LOCK_EX);
3175 /* Queue cleanup 2: wait for the expired queue to clear. */
3176 while (!list_empty(&the_lnet.ln_dc_expired))
3177 schedule_timeout(cfs_time_seconds(1));
3179 /* Queue cleanup 3: clear the request queue. */
3180 lnet_net_lock(LNET_LOCK_EX);
3181 while (!list_empty(&the_lnet.ln_dc_request)) {
3182 lp = list_first_entry(&the_lnet.ln_dc_request,
3183 struct lnet_peer, lp_dc_list);
3184 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3185 lnet_peer_discovery_complete(lp);
3187 lnet_net_unlock(LNET_LOCK_EX);
3189 LNetEQFree(the_lnet.ln_dc_eqh);
3190 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3192 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3193 wake_up(&the_lnet.ln_dc_waitq);
3195 CDEBUG(D_NET, "stopped\n");
3200 /* ln_api_mutex is held on entry. */
3201 int lnet_peer_discovery_start(void)
3203 struct task_struct *task;
3206 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3209 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3211 CERROR("Can't allocate discovery EQ: %d\n", rc);
3215 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3216 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3219 CERROR("Can't start peer discovery thread: %d\n", rc);
3221 LNetEQFree(the_lnet.ln_dc_eqh);
3222 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3224 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3227 CDEBUG(D_NET, "discovery start: %d\n", rc);
3232 /* ln_api_mutex is held on entry. */
3233 void lnet_peer_discovery_stop(void)
3235 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3238 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3239 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3240 wake_up(&the_lnet.ln_dc_waitq);
3242 wait_event(the_lnet.ln_dc_waitq,
3243 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3245 LASSERT(list_empty(&the_lnet.ln_dc_request));
3246 LASSERT(list_empty(&the_lnet.ln_dc_working));
3247 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3249 CDEBUG(D_NET, "discovery stopped\n");
3255 lnet_debug_peer(lnet_nid_t nid)
3257 char *aliveness = "NA";
3258 struct lnet_peer_ni *lp;
3261 cpt = lnet_cpt_of_nid(nid, NULL);
3264 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3266 lnet_net_unlock(cpt);
3267 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3271 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3272 aliveness = lp->lpni_alive ? "up" : "down";
3274 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3275 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3276 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3277 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3278 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3280 lnet_peer_ni_decref_locked(lp);
3282 lnet_net_unlock(cpt);
3285 /* Gathering information for userspace. */
3287 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3288 char aliveness[LNET_MAX_STR_LEN],
3289 __u32 *cpt_iter, __u32 *refcount,
3290 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3291 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3292 __u32 *peer_tx_qnob)
3294 struct lnet_peer_table *peer_table;
3295 struct lnet_peer_ni *lp;
3300 /* get the number of CPTs */
3301 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3303 /* if the cpt number to be examined is >= the number of cpts in
3304 * the system then indicate that there are no more cpts to examin
3306 if (*cpt_iter >= lncpt)
3309 /* get the current table */
3310 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3311 /* if the ptable is NULL then there are no more cpts to examine */
3312 if (peer_table == NULL)
3315 lnet_net_lock(*cpt_iter);
3317 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3318 struct list_head *peers = &peer_table->pt_hash[j];
3320 list_for_each_entry(lp, peers, lpni_hashlist) {
3321 if (peer_index-- > 0)
3324 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3325 if (lnet_isrouter(lp) ||
3326 lnet_peer_aliveness_enabled(lp))
3327 snprintf(aliveness, LNET_MAX_STR_LEN,
3328 lp->lpni_alive ? "up" : "down");
3330 *nid = lp->lpni_nid;
3331 *refcount = atomic_read(&lp->lpni_refcount);
3332 *ni_peer_tx_credits =
3333 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3334 *peer_tx_credits = lp->lpni_txcredits;
3335 *peer_rtr_credits = lp->lpni_rtrcredits;
3336 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3337 *peer_tx_qnob = lp->lpni_txqnob;
3343 lnet_net_unlock(*cpt_iter);
3347 return found ? 0 : -ENOENT;
3350 /* ln_api_mutex is held, which keeps the peer list stable */
3351 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3353 struct lnet_ioctl_element_stats *lpni_stats;
3354 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3355 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3356 struct lnet_peer_ni_credit_info *lpni_info;
3357 struct lnet_peer_ni *lpni;
3358 struct lnet_peer *lp;
3363 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3370 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3371 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3372 size *= lp->lp_nnis;
3373 if (size > cfg->prcfg_size) {
3374 cfg->prcfg_size = size;
3379 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3380 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3381 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3382 cfg->prcfg_count = lp->lp_nnis;
3383 cfg->prcfg_size = size;
3384 cfg->prcfg_state = lp->lp_state;
3386 /* Allocate helper buffers. */
3388 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3391 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3394 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3395 if (!lpni_msg_stats)
3396 goto out_free_stats;
3397 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3399 goto out_free_msg_stats;
3404 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3405 nid = lpni->lpni_nid;
3406 if (copy_to_user(bulk, &nid, sizeof(nid)))
3407 goto out_free_hstats;
3408 bulk += sizeof(nid);
3410 memset(lpni_info, 0, sizeof(*lpni_info));
3411 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3412 if (lnet_isrouter(lpni) ||
3413 lnet_peer_aliveness_enabled(lpni))
3414 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3415 lpni->lpni_alive ? "up" : "down");
3417 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3418 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3419 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3420 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3421 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3422 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3423 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3424 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3425 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3426 goto out_free_hstats;
3427 bulk += sizeof(*lpni_info);
3429 memset(lpni_stats, 0, sizeof(*lpni_stats));
3430 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3431 LNET_STATS_TYPE_SEND);
3432 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3433 LNET_STATS_TYPE_RECV);
3434 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3435 LNET_STATS_TYPE_DROP);
3436 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3437 goto out_free_hstats;
3438 bulk += sizeof(*lpni_stats);
3439 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3440 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3441 goto out_free_hstats;
3442 bulk += sizeof(*lpni_msg_stats);
3443 lpni_hstats->hlpni_network_timeout =
3444 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3445 lpni_hstats->hlpni_remote_dropped =
3446 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3447 lpni_hstats->hlpni_remote_timeout =
3448 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3449 lpni_hstats->hlpni_remote_error =
3450 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3451 lpni_hstats->hlpni_health_value =
3452 atomic_read(&lpni->lpni_healthv);
3453 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3454 goto out_free_hstats;
3455 bulk += sizeof(*lpni_hstats);
3460 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3462 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3464 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3466 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3468 lnet_peer_decref_locked(lp);
3474 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3476 /* the mt could've shutdown and cleaned up the queues */
3477 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3480 if (list_empty(&lpni->lpni_recovery) &&
3481 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3482 CERROR("lpni %s added to recovery queue. Health = %d\n",
3483 libcfs_nid2str(lpni->lpni_nid),
3484 atomic_read(&lpni->lpni_healthv));
3485 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3486 lnet_peer_ni_addref_locked(lpni);
3490 /* Call with the ln_api_mutex held */
3492 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3494 struct lnet_peer_table *ptable;
3495 struct lnet_peer *lp;
3496 struct lnet_peer_net *lpn;
3497 struct lnet_peer_ni *lpni;
3501 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3505 lnet_net_lock(LNET_LOCK_EX);
3506 lpni = lnet_find_peer_ni_locked(nid);
3508 lnet_net_unlock(LNET_LOCK_EX);
3511 atomic_set(&lpni->lpni_healthv, value);
3512 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3513 lnet_peer_ni_decref_locked(lpni);
3514 lnet_net_unlock(LNET_LOCK_EX);
3518 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3521 * Walk all the peers and reset the healhv for each one to the
3524 lnet_net_lock(LNET_LOCK_EX);
3525 for (cpt = 0; cpt < lncpt; cpt++) {
3526 ptable = the_lnet.ln_peer_tables[cpt];
3527 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3528 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3529 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3531 atomic_set(&lpni->lpni_healthv, value);
3532 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3537 lnet_net_unlock(LNET_LOCK_EX);