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);
181 net = lnet_get_net_locked(LNET_NIDNET(nid));
182 lpni->lpni_net = net;
184 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
185 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
186 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
187 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
190 * This peer_ni is not on a local network, so we
191 * cannot add the credits here. In case the net is
192 * added later, add the peer_ni to the remote peer ni
193 * list so it can be easily found and revisited.
195 /* FIXME: per-net implementation instead? */
196 atomic_inc(&lpni->lpni_refcount);
197 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
198 &the_lnet.ln_remote_peer_ni_list);
201 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
206 static struct lnet_peer_net *
207 lnet_peer_net_alloc(__u32 net_id)
209 struct lnet_peer_net *lpn;
211 LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
215 INIT_LIST_HEAD(&lpn->lpn_peer_nets);
216 INIT_LIST_HEAD(&lpn->lpn_peer_nis);
217 lpn->lpn_net_id = net_id;
219 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
225 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
227 struct lnet_peer *lp;
229 CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
231 LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
232 LASSERT(list_empty(&lpn->lpn_peer_nis));
233 LASSERT(list_empty(&lpn->lpn_peer_nets));
235 lpn->lpn_peer = NULL;
236 LIBCFS_FREE(lpn, sizeof(*lpn));
238 lnet_peer_decref_locked(lp);
241 static struct lnet_peer *
242 lnet_peer_alloc(lnet_nid_t nid)
244 struct lnet_peer *lp;
246 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
250 INIT_LIST_HEAD(&lp->lp_peer_list);
251 INIT_LIST_HEAD(&lp->lp_peer_nets);
252 INIT_LIST_HEAD(&lp->lp_dc_list);
253 INIT_LIST_HEAD(&lp->lp_dc_pendq);
254 init_waitqueue_head(&lp->lp_dc_waitq);
255 spin_lock_init(&lp->lp_lock);
256 lp->lp_primary_nid = nid;
258 * Turn off discovery for loopback peer. If you're creating a peer
259 * for the loopback interface then that was initiated when we
260 * attempted to send a message over the loopback. There is no need
261 * to ever use a different interface when sending messages to
264 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND)
265 lp->lp_state = LNET_PEER_NO_DISCOVERY;
266 lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
268 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
274 lnet_destroy_peer_locked(struct lnet_peer *lp)
276 CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
278 LASSERT(atomic_read(&lp->lp_refcount) == 0);
279 LASSERT(list_empty(&lp->lp_peer_nets));
280 LASSERT(list_empty(&lp->lp_peer_list));
281 LASSERT(list_empty(&lp->lp_dc_list));
284 lnet_ping_buffer_decref(lp->lp_data);
287 * if there are messages still on the pending queue, then make
288 * sure to queue them on the ln_msg_resend list so they can be
289 * resent at a later point if the discovery thread is still
291 * If the discovery thread has stopped, then the wakeup will be a
292 * no-op, and it is expected the lnet_shutdown_lndnets() will
293 * eventually be called, which will traverse this list and
294 * finalize the messages on the list.
295 * We can not resend them now because we're holding the cpt lock.
296 * Releasing the lock can cause an inconsistent state
298 spin_lock(&the_lnet.ln_msg_resend_lock);
299 list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
300 spin_unlock(&the_lnet.ln_msg_resend_lock);
301 wake_up(&the_lnet.ln_dc_waitq);
303 LIBCFS_FREE(lp, sizeof(*lp));
307 * Detach a peer_ni from its peer_net. If this was the last peer_ni on
308 * that peer_net, detach the peer_net from the peer.
310 * Call with lnet_net_lock/EX held
313 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
315 struct lnet_peer_table *ptable;
316 struct lnet_peer_net *lpn;
317 struct lnet_peer *lp;
320 * Belts and suspenders: gracefully handle teardown of a
321 * partially connected peer_ni.
323 lpn = lpni->lpni_peer_net;
325 list_del_init(&lpni->lpni_peer_nis);
327 * If there are no lpni's left, we detach lpn from
328 * lp_peer_nets, so it cannot be found anymore.
330 if (list_empty(&lpn->lpn_peer_nis))
331 list_del_init(&lpn->lpn_peer_nets);
333 /* Update peer NID count. */
338 * If there are no more peer nets, make the peer unfindable
339 * via the peer_tables.
341 * Otherwise, if the peer is DISCOVERED, tell discovery to
342 * take another look at it. This is a no-op if discovery for
343 * this peer did the detaching.
345 if (list_empty(&lp->lp_peer_nets)) {
346 list_del_init(&lp->lp_peer_list);
347 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
349 } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
350 /* Discovery isn't running, nothing to do here. */
351 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
352 lnet_peer_queue_for_discovery(lp);
353 wake_up(&the_lnet.ln_dc_waitq);
355 CDEBUG(D_NET, "peer %s NID %s\n",
356 libcfs_nid2str(lp->lp_primary_nid),
357 libcfs_nid2str(lpni->lpni_nid));
360 /* called with lnet_net_lock LNET_LOCK_EX held */
362 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
364 struct lnet_peer_table *ptable = NULL;
366 /* don't remove a peer_ni if it's also a gateway */
367 if (lpni->lpni_rtr_refcount > 0) {
368 CERROR("Peer NI %s is a gateway. Can not delete it\n",
369 libcfs_nid2str(lpni->lpni_nid));
373 lnet_peer_remove_from_remote_list(lpni);
375 /* remove peer ni from the hash list. */
376 list_del_init(&lpni->lpni_hashlist);
379 * indicate the peer is being deleted so the monitor thread can
380 * remove it from the recovery queue.
382 spin_lock(&lpni->lpni_lock);
383 lpni->lpni_state |= LNET_PEER_NI_DELETING;
384 spin_unlock(&lpni->lpni_lock);
386 /* decrement the ref count on the peer table */
387 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
388 LASSERT(ptable->pt_number > 0);
392 * The peer_ni can no longer be found with a lookup. But there
393 * can be current users, so keep track of it on the zombie
394 * list until the reference count has gone to zero.
396 * The last reference may be lost in a place where the
397 * lnet_net_lock locks only a single cpt, and that cpt may not
398 * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
401 spin_lock(&ptable->pt_zombie_lock);
402 list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
403 ptable->pt_zombies++;
404 spin_unlock(&ptable->pt_zombie_lock);
406 /* no need to keep this peer_ni on the hierarchy anymore */
407 lnet_peer_detach_peer_ni_locked(lpni);
409 /* remove hashlist reference on peer_ni */
410 lnet_peer_ni_decref_locked(lpni);
415 void lnet_peer_uninit(void)
417 struct lnet_peer_ni *lpni, *tmp;
419 lnet_net_lock(LNET_LOCK_EX);
421 /* remove all peer_nis from the remote peer and the hash list */
422 list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
423 lpni_on_remote_peer_ni_list)
424 lnet_peer_ni_del_locked(lpni);
426 lnet_peer_tables_destroy();
428 lnet_net_unlock(LNET_LOCK_EX);
432 lnet_peer_del_locked(struct lnet_peer *peer)
434 struct lnet_peer_ni *lpni = NULL, *lpni2;
437 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
439 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
440 while (lpni != NULL) {
441 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
442 rc = lnet_peer_ni_del_locked(lpni);
452 lnet_peer_del(struct lnet_peer *peer)
454 lnet_net_lock(LNET_LOCK_EX);
455 lnet_peer_del_locked(peer);
456 lnet_net_unlock(LNET_LOCK_EX);
462 * Delete a NID from a peer. Call with ln_api_mutex held.
465 * -EPERM: Non-DLC deletion from DLC-configured peer.
466 * -ENOENT: No lnet_peer_ni corresponding to the nid.
467 * -ECHILD: The lnet_peer_ni isn't connected to the peer.
468 * -EBUSY: The lnet_peer_ni is the primary, and not the only peer_ni.
471 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
473 struct lnet_peer_ni *lpni;
474 lnet_nid_t primary_nid = lp->lp_primary_nid;
477 if (!(flags & LNET_PEER_CONFIGURED)) {
478 if (lp->lp_state & LNET_PEER_CONFIGURED) {
483 lpni = lnet_find_peer_ni_locked(nid);
488 lnet_peer_ni_decref_locked(lpni);
489 if (lp != lpni->lpni_peer_net->lpn_peer) {
495 * This function only allows deletion of the primary NID if it
498 if (nid == lp->lp_primary_nid && lp->lp_nnis != 1) {
503 lnet_net_lock(LNET_LOCK_EX);
504 lnet_peer_ni_del_locked(lpni);
505 lnet_net_unlock(LNET_LOCK_EX);
508 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
509 libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
515 lnet_peer_table_cleanup_locked(struct lnet_net *net,
516 struct lnet_peer_table *ptable)
519 struct lnet_peer_ni *next;
520 struct lnet_peer_ni *lpni;
521 struct lnet_peer *peer;
523 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
524 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
526 if (net != NULL && net != lpni->lpni_net)
529 peer = lpni->lpni_peer_net->lpn_peer;
530 if (peer->lp_primary_nid != lpni->lpni_nid) {
531 lnet_peer_ni_del_locked(lpni);
535 * Removing the primary NID implies removing
536 * the entire peer. Advance next beyond any
537 * peer_ni that belongs to the same peer.
539 list_for_each_entry_from(next, &ptable->pt_hash[i],
541 if (next->lpni_peer_net->lpn_peer != peer)
544 lnet_peer_del_locked(peer);
550 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
554 spin_lock(&ptable->pt_zombie_lock);
555 while (ptable->pt_zombies) {
556 spin_unlock(&ptable->pt_zombie_lock);
558 if (is_power_of_2(i)) {
560 "Waiting for %d zombies on peer table\n",
563 set_current_state(TASK_UNINTERRUPTIBLE);
564 schedule_timeout(cfs_time_seconds(1) >> 1);
565 spin_lock(&ptable->pt_zombie_lock);
567 spin_unlock(&ptable->pt_zombie_lock);
571 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
572 struct lnet_peer_table *ptable)
574 struct lnet_peer_ni *lp;
575 struct lnet_peer_ni *tmp;
579 for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
580 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
582 if (net != lp->lpni_net)
585 if (lp->lpni_rtr_refcount == 0)
588 lpni_nid = lp->lpni_nid;
590 lnet_net_unlock(LNET_LOCK_EX);
591 lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
592 lnet_net_lock(LNET_LOCK_EX);
598 lnet_peer_tables_cleanup(struct lnet_net *net)
601 struct lnet_peer_table *ptable;
603 LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
604 /* If just deleting the peers for a NI, get rid of any routes these
605 * peers are gateways for. */
606 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
607 lnet_net_lock(LNET_LOCK_EX);
608 lnet_peer_table_del_rtrs_locked(net, ptable);
609 lnet_net_unlock(LNET_LOCK_EX);
612 /* Start the cleanup process */
613 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
614 lnet_net_lock(LNET_LOCK_EX);
615 lnet_peer_table_cleanup_locked(net, ptable);
616 lnet_net_unlock(LNET_LOCK_EX);
619 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
620 lnet_peer_ni_finalize_wait(ptable);
623 static struct lnet_peer_ni *
624 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
626 struct list_head *peers;
627 struct lnet_peer_ni *lp;
629 LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
631 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
632 list_for_each_entry(lp, peers, lpni_hashlist) {
633 if (lp->lpni_nid == nid) {
634 lnet_peer_ni_addref_locked(lp);
642 struct lnet_peer_ni *
643 lnet_find_peer_ni_locked(lnet_nid_t nid)
645 struct lnet_peer_ni *lpni;
646 struct lnet_peer_table *ptable;
649 cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
651 ptable = the_lnet.ln_peer_tables[cpt];
652 lpni = lnet_get_peer_ni_locked(ptable, nid);
658 lnet_find_peer(lnet_nid_t nid)
660 struct lnet_peer_ni *lpni;
661 struct lnet_peer *lp = NULL;
664 cpt = lnet_net_lock_current();
665 lpni = lnet_find_peer_ni_locked(nid);
667 lp = lpni->lpni_peer_net->lpn_peer;
668 lnet_peer_addref_locked(lp);
669 lnet_peer_ni_decref_locked(lpni);
671 lnet_net_unlock(cpt);
676 struct lnet_peer_ni *
677 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
678 struct lnet_peer_net *peer_net,
679 struct lnet_peer_ni *prev)
681 struct lnet_peer_ni *lpni;
682 struct lnet_peer_net *net = peer_net;
686 if (list_empty(&peer->lp_peer_nets))
689 net = list_entry(peer->lp_peer_nets.next,
690 struct lnet_peer_net,
693 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
699 if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
701 * if you reached the end of the peer ni list and the peer
702 * net is specified then there are no more peer nis in that
709 * we reached the end of this net ni list. move to the
712 if (prev->lpni_peer_net->lpn_peer_nets.next ==
714 /* no more nets and no more NIs. */
717 /* get the next net */
718 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
719 struct lnet_peer_net,
721 /* get the ni on it */
722 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
728 /* there are more nis left */
729 lpni = list_entry(prev->lpni_peer_nis.next,
730 struct lnet_peer_ni, lpni_peer_nis);
735 /* Call with the ln_api_mutex held */
736 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
738 struct lnet_process_id id;
739 struct lnet_peer_table *ptable;
740 struct lnet_peer *lp;
749 if (the_lnet.ln_state != LNET_STATE_RUNNING)
752 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
755 * Count the number of peers, and return E2BIG if the buffer
756 * is too small. We'll also return the desired size.
759 for (cpt = 0; cpt < lncpt; cpt++) {
760 ptable = the_lnet.ln_peer_tables[cpt];
761 count += ptable->pt_peers;
763 size = count * sizeof(*ids);
768 * Walk the peer lists and copy out the primary nids.
769 * This is safe because the peer lists are only modified
770 * while the ln_api_mutex is held. So we don't need to
771 * hold the lnet_net_lock as well, and can therefore
772 * directly call copy_to_user().
775 memset(&id, 0, sizeof(id));
776 id.pid = LNET_PID_LUSTRE;
778 for (cpt = 0; cpt < lncpt; cpt++) {
779 ptable = the_lnet.ln_peer_tables[cpt];
780 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
783 id.nid = lp->lp_primary_nid;
784 if (copy_to_user(&ids[i], &id, sizeof(id)))
797 * Start pushes to peers that need to be updated for a configuration
798 * change on this node.
801 lnet_push_update_to_peers(int force)
803 struct lnet_peer_table *ptable;
804 struct lnet_peer *lp;
808 lnet_net_lock(LNET_LOCK_EX);
809 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
810 for (cpt = 0; cpt < lncpt; cpt++) {
811 ptable = the_lnet.ln_peer_tables[cpt];
812 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
814 spin_lock(&lp->lp_lock);
815 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
816 lp->lp_state |= LNET_PEER_FORCE_PUSH;
817 spin_unlock(&lp->lp_lock);
819 if (lnet_peer_needs_push(lp))
820 lnet_peer_queue_for_discovery(lp);
823 lnet_net_unlock(LNET_LOCK_EX);
824 wake_up(&the_lnet.ln_dc_waitq);
828 * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
829 * this is a preferred point-to-point path. Call with lnet_net_lock in
833 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
837 if (lpni->lpni_pref_nnids == 0)
839 if (lpni->lpni_pref_nnids == 1)
840 return lpni->lpni_pref.nid == nid;
841 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
842 if (lpni->lpni_pref.nids[i] == nid)
849 * Set a single ni as preferred, provided no preferred ni is already
850 * defined. Only to be used for non-multi-rail peer_ni.
853 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
857 spin_lock(&lpni->lpni_lock);
858 if (nid == LNET_NID_ANY) {
860 } else if (lpni->lpni_pref_nnids > 0) {
862 } else if (lpni->lpni_pref_nnids == 0) {
863 lpni->lpni_pref.nid = nid;
864 lpni->lpni_pref_nnids = 1;
865 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
867 spin_unlock(&lpni->lpni_lock);
869 CDEBUG(D_NET, "peer %s nid %s: %d\n",
870 libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
875 * Clear the preferred NID from a non-multi-rail peer_ni, provided
876 * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
879 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
883 spin_lock(&lpni->lpni_lock);
884 if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
885 lpni->lpni_pref_nnids = 0;
886 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
887 } else if (lpni->lpni_pref_nnids == 0) {
892 spin_unlock(&lpni->lpni_lock);
894 CDEBUG(D_NET, "peer %s: %d\n",
895 libcfs_nid2str(lpni->lpni_nid), rc);
900 * Clear the preferred NIDs from a non-multi-rail peer.
903 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
905 struct lnet_peer_ni *lpni = NULL;
907 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
908 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
912 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
914 lnet_nid_t *nids = NULL;
915 lnet_nid_t *oldnids = NULL;
916 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
921 if (nid == LNET_NID_ANY) {
926 if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
931 /* A non-MR node may have only one preferred NI per peer_ni */
932 if (lpni->lpni_pref_nnids > 0) {
933 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
939 if (lpni->lpni_pref_nnids != 0) {
940 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
941 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
946 for (i = 0; i < lpni->lpni_pref_nnids; i++) {
947 if (lpni->lpni_pref.nids[i] == nid) {
948 LIBCFS_FREE(nids, size);
952 nids[i] = lpni->lpni_pref.nids[i];
957 lnet_net_lock(LNET_LOCK_EX);
958 spin_lock(&lpni->lpni_lock);
959 if (lpni->lpni_pref_nnids == 0) {
960 lpni->lpni_pref.nid = nid;
962 oldnids = lpni->lpni_pref.nids;
963 lpni->lpni_pref.nids = nids;
965 lpni->lpni_pref_nnids++;
966 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
967 spin_unlock(&lpni->lpni_lock);
968 lnet_net_unlock(LNET_LOCK_EX);
971 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
972 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
975 if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
976 spin_lock(&lpni->lpni_lock);
977 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
978 spin_unlock(&lpni->lpni_lock);
980 CDEBUG(D_NET, "peer %s nid %s: %d\n",
981 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
986 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
988 lnet_nid_t *nids = NULL;
989 lnet_nid_t *oldnids = NULL;
990 struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
995 if (lpni->lpni_pref_nnids == 0) {
1000 if (lpni->lpni_pref_nnids == 1) {
1001 if (lpni->lpni_pref.nid != nid) {
1005 } else if (lpni->lpni_pref_nnids == 2) {
1006 if (lpni->lpni_pref.nids[0] != nid &&
1007 lpni->lpni_pref.nids[1] != nid) {
1012 size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
1013 LIBCFS_CPT_ALLOC(nids, lnet_cpt_table(), lpni->lpni_cpt, size);
1018 for (i = 0, j = 0; i < lpni->lpni_pref_nnids; i++) {
1019 if (lpni->lpni_pref.nids[i] != nid)
1021 nids[j++] = lpni->lpni_pref.nids[i];
1023 /* Check if we actually removed a nid. */
1024 if (j == lpni->lpni_pref_nnids) {
1025 LIBCFS_FREE(nids, size);
1031 lnet_net_lock(LNET_LOCK_EX);
1032 spin_lock(&lpni->lpni_lock);
1033 if (lpni->lpni_pref_nnids == 1) {
1034 lpni->lpni_pref.nid = LNET_NID_ANY;
1035 } else if (lpni->lpni_pref_nnids == 2) {
1036 oldnids = lpni->lpni_pref.nids;
1037 if (oldnids[0] == nid)
1038 lpni->lpni_pref.nid = oldnids[1];
1040 lpni->lpni_pref.nid = oldnids[2];
1042 oldnids = lpni->lpni_pref.nids;
1043 lpni->lpni_pref.nids = nids;
1045 lpni->lpni_pref_nnids--;
1046 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1047 spin_unlock(&lpni->lpni_lock);
1048 lnet_net_unlock(LNET_LOCK_EX);
1051 size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
1052 LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
1055 CDEBUG(D_NET, "peer %s nid %s: %d\n",
1056 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1061 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1063 struct lnet_peer_ni *lpni;
1064 lnet_nid_t primary_nid = nid;
1066 lpni = lnet_find_peer_ni_locked(nid);
1068 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1069 lnet_peer_ni_decref_locked(lpni);
1076 LNetPrimaryNID(lnet_nid_t nid)
1078 struct lnet_peer *lp;
1079 struct lnet_peer_ni *lpni;
1080 lnet_nid_t primary_nid = nid;
1084 cpt = lnet_net_lock_current();
1085 lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1090 lp = lpni->lpni_peer_net->lpn_peer;
1091 while (!lnet_peer_is_uptodate(lp)) {
1092 rc = lnet_discover_peer_locked(lpni, cpt, true);
1095 lp = lpni->lpni_peer_net->lpn_peer;
1097 primary_nid = lp->lp_primary_nid;
1099 lnet_peer_ni_decref_locked(lpni);
1101 lnet_net_unlock(cpt);
1103 CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1104 libcfs_nid2str(primary_nid), rc);
1107 EXPORT_SYMBOL(LNetPrimaryNID);
1109 struct lnet_peer_net *
1110 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1112 struct lnet_peer_net *peer_net;
1113 list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1114 if (peer_net->lpn_net_id == net_id)
1121 * Attach a peer_ni to a peer_net and peer. This function assumes
1122 * peer_ni is not already attached to the peer_net/peer. The peer_ni
1123 * may be attached to a different peer, in which case it will be
1124 * properly detached first. The whole operation is done atomically.
1126 * Always returns 0. This is the last function called from functions
1127 * that do return an int, so returning 0 here allows the compiler to
1131 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1132 struct lnet_peer_net *lpn,
1133 struct lnet_peer_ni *lpni,
1136 struct lnet_peer_table *ptable;
1138 /* Install the new peer_ni */
1139 lnet_net_lock(LNET_LOCK_EX);
1140 /* Add peer_ni to global peer table hash, if necessary. */
1141 if (list_empty(&lpni->lpni_hashlist)) {
1142 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1144 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1145 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1146 ptable->pt_version++;
1147 ptable->pt_number++;
1148 /* This is the 1st refcount on lpni. */
1149 atomic_inc(&lpni->lpni_refcount);
1152 /* Detach the peer_ni from an existing peer, if necessary. */
1153 if (lpni->lpni_peer_net) {
1154 LASSERT(lpni->lpni_peer_net != lpn);
1155 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1156 lnet_peer_detach_peer_ni_locked(lpni);
1157 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1158 lpni->lpni_peer_net = NULL;
1161 /* Add peer_ni to peer_net */
1162 lpni->lpni_peer_net = lpn;
1163 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1164 lnet_peer_net_addref_locked(lpn);
1166 /* Add peer_net to peer */
1167 if (!lpn->lpn_peer) {
1169 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1170 lnet_peer_addref_locked(lp);
1173 /* Add peer to global peer list, if necessary */
1174 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1175 if (list_empty(&lp->lp_peer_list)) {
1176 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1181 /* Update peer state */
1182 spin_lock(&lp->lp_lock);
1183 if (flags & LNET_PEER_CONFIGURED) {
1184 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1185 lp->lp_state |= LNET_PEER_CONFIGURED;
1187 if (flags & LNET_PEER_MULTI_RAIL) {
1188 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1189 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1190 lnet_peer_clr_non_mr_pref_nids(lp);
1193 spin_unlock(&lp->lp_lock);
1196 lnet_net_unlock(LNET_LOCK_EX);
1198 CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1199 libcfs_nid2str(lp->lp_primary_nid),
1200 libcfs_nid2str(lpni->lpni_nid), flags);
1206 * Create a new peer, with nid as its primary nid.
1208 * Call with the lnet_api_mutex held.
1211 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1213 struct lnet_peer *lp;
1214 struct lnet_peer_net *lpn;
1215 struct lnet_peer_ni *lpni;
1218 LASSERT(nid != LNET_NID_ANY);
1221 * No need for the lnet_net_lock here, because the
1222 * lnet_api_mutex is held.
1224 lpni = lnet_find_peer_ni_locked(nid);
1226 /* A peer with this NID already exists. */
1227 lp = lpni->lpni_peer_net->lpn_peer;
1228 lnet_peer_ni_decref_locked(lpni);
1230 * This is an error if the peer was configured and the
1231 * primary NID differs or an attempt is made to change
1232 * the Multi-Rail flag. Otherwise the assumption is
1233 * that an existing peer is being modified.
1235 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1236 if (lp->lp_primary_nid != nid)
1238 else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1242 /* Delete and recreate as a configured peer. */
1246 /* Create peer, peer_net, and peer_ni. */
1248 lp = lnet_peer_alloc(nid);
1251 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1254 lpni = lnet_peer_ni_alloc(nid);
1258 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1261 LIBCFS_FREE(lpn, sizeof(*lpn));
1263 LIBCFS_FREE(lp, sizeof(*lp));
1265 CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1266 libcfs_nid2str(nid), flags, rc);
1271 * Add a NID to a peer. Call with ln_api_mutex held.
1274 * -EPERM: Non-DLC addition to a DLC-configured peer.
1275 * -EEXIST: The NID was configured by DLC for a different peer.
1276 * -ENOMEM: Out of memory.
1277 * -ENOTUNIQ: Adding a second peer NID on a single network on a
1278 * non-multi-rail peer.
1281 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1283 struct lnet_peer_net *lpn;
1284 struct lnet_peer_ni *lpni;
1288 LASSERT(nid != LNET_NID_ANY);
1290 /* A configured peer can only be updated through configuration. */
1291 if (!(flags & LNET_PEER_CONFIGURED)) {
1292 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1299 * The MULTI_RAIL flag can be set but not cleared, because
1300 * that would leave the peer struct in an invalid state.
1302 if (flags & LNET_PEER_MULTI_RAIL) {
1303 spin_lock(&lp->lp_lock);
1304 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1305 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1306 lnet_peer_clr_non_mr_pref_nids(lp);
1308 spin_unlock(&lp->lp_lock);
1309 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1314 lpni = lnet_find_peer_ni_locked(nid);
1317 * A peer_ni already exists. This is only a problem if
1318 * it is not connected to this peer and was configured
1321 lnet_peer_ni_decref_locked(lpni);
1322 if (lpni->lpni_peer_net->lpn_peer == lp)
1324 if (lnet_peer_ni_is_configured(lpni)) {
1328 /* If this is the primary NID, destroy the peer. */
1329 if (lnet_peer_ni_is_primary(lpni)) {
1330 lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1331 lpni = lnet_peer_ni_alloc(nid);
1338 lpni = lnet_peer_ni_alloc(nid);
1346 * Get the peer_net. Check that we're not adding a second
1347 * peer_ni on a peer_net of a non-multi-rail peer.
1349 lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1351 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1356 } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1361 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1364 /* If the peer_ni was allocated above its peer_net pointer is NULL */
1365 if (!lpni->lpni_peer_net)
1366 LIBCFS_FREE(lpni, sizeof(*lpni));
1368 CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1369 libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1375 * Update the primary NID of a peer, if possible.
1377 * Call with the lnet_api_mutex held.
1380 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1382 lnet_nid_t old = lp->lp_primary_nid;
1385 if (lp->lp_primary_nid == nid)
1387 rc = lnet_peer_add_nid(lp, nid, flags);
1390 lp->lp_primary_nid = nid;
1392 CDEBUG(D_NET, "peer %s NID %s: %d\n",
1393 libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1398 * lpni creation initiated due to traffic either sending or receiving.
1401 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1403 struct lnet_peer *lp;
1404 struct lnet_peer_net *lpn;
1405 struct lnet_peer_ni *lpni;
1409 if (nid == LNET_NID_ANY) {
1414 /* lnet_net_lock is not needed here because ln_api_lock is held */
1415 lpni = lnet_find_peer_ni_locked(nid);
1418 * We must have raced with another thread. Since we
1419 * know next to nothing about a peer_ni created by
1420 * traffic, we just assume everything is ok and
1423 lnet_peer_ni_decref_locked(lpni);
1427 /* Create peer, peer_net, and peer_ni. */
1429 lp = lnet_peer_alloc(nid);
1432 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1435 lpni = lnet_peer_ni_alloc(nid);
1438 if (pref != LNET_NID_ANY)
1439 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1441 return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1444 LIBCFS_FREE(lpn, sizeof(*lpn));
1446 LIBCFS_FREE(lp, sizeof(*lp));
1448 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1453 * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1455 * This API handles the following combinations:
1456 * Create a peer with its primary NI if only the prim_nid is provided
1457 * Add a NID to a peer identified by the prim_nid. The peer identified
1458 * by the prim_nid must already exist.
1459 * The peer being created may be non-MR.
1461 * The caller must hold ln_api_mutex. This prevents the peer from
1462 * being created/modified/deleted by a different thread.
1465 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1467 struct lnet_peer *lp = NULL;
1468 struct lnet_peer_ni *lpni;
1471 /* The prim_nid must always be specified */
1472 if (prim_nid == LNET_NID_ANY)
1475 flags = LNET_PEER_CONFIGURED;
1477 flags |= LNET_PEER_MULTI_RAIL;
1480 * If nid isn't specified, we must create a new peer with
1481 * prim_nid as its primary nid.
1483 if (nid == LNET_NID_ANY)
1484 return lnet_peer_add(prim_nid, flags);
1486 /* Look up the prim_nid, which must exist. */
1487 lpni = lnet_find_peer_ni_locked(prim_nid);
1490 lnet_peer_ni_decref_locked(lpni);
1491 lp = lpni->lpni_peer_net->lpn_peer;
1493 /* Peer must have been configured. */
1494 if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1495 CDEBUG(D_NET, "peer %s was not configured\n",
1496 libcfs_nid2str(prim_nid));
1500 /* Primary NID must match */
1501 if (lp->lp_primary_nid != prim_nid) {
1502 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1503 libcfs_nid2str(prim_nid),
1504 libcfs_nid2str(lp->lp_primary_nid));
1508 /* Multi-Rail flag must match. */
1509 if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1510 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1511 libcfs_nid2str(prim_nid));
1515 return lnet_peer_add_nid(lp, nid, flags);
1519 * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1521 * This API handles the following combinations:
1522 * Delete a NI from a peer if both prim_nid and nid are provided.
1523 * Delete a peer if only prim_nid is provided.
1524 * Delete a peer if its primary nid is provided.
1526 * The caller must hold ln_api_mutex. This prevents the peer from
1527 * being modified/deleted by a different thread.
1530 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1532 struct lnet_peer *lp;
1533 struct lnet_peer_ni *lpni;
1536 if (prim_nid == LNET_NID_ANY)
1539 lpni = lnet_find_peer_ni_locked(prim_nid);
1542 lnet_peer_ni_decref_locked(lpni);
1543 lp = lpni->lpni_peer_net->lpn_peer;
1545 if (prim_nid != lp->lp_primary_nid) {
1546 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1547 libcfs_nid2str(prim_nid),
1548 libcfs_nid2str(lp->lp_primary_nid));
1552 if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1553 return lnet_peer_del(lp);
1555 flags = LNET_PEER_CONFIGURED;
1556 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1557 flags |= LNET_PEER_MULTI_RAIL;
1559 return lnet_peer_del_nid(lp, nid, flags);
1563 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1565 struct lnet_peer_table *ptable;
1566 struct lnet_peer_net *lpn;
1568 CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1570 LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1571 LASSERT(lpni->lpni_rtr_refcount == 0);
1572 LASSERT(list_empty(&lpni->lpni_txq));
1573 LASSERT(lpni->lpni_txqnob == 0);
1574 LASSERT(list_empty(&lpni->lpni_peer_nis));
1575 LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1577 lpn = lpni->lpni_peer_net;
1578 lpni->lpni_peer_net = NULL;
1579 lpni->lpni_net = NULL;
1581 /* remove the peer ni from the zombie list */
1582 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1583 spin_lock(&ptable->pt_zombie_lock);
1584 list_del_init(&lpni->lpni_hashlist);
1585 ptable->pt_zombies--;
1586 spin_unlock(&ptable->pt_zombie_lock);
1588 if (lpni->lpni_pref_nnids > 1) {
1589 LIBCFS_FREE(lpni->lpni_pref.nids,
1590 sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
1592 LIBCFS_FREE(lpni, sizeof(*lpni));
1594 lnet_peer_net_decref_locked(lpn);
1597 struct lnet_peer_ni *
1598 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1600 struct lnet_peer_ni *lpni = NULL;
1603 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1604 return ERR_PTR(-ESHUTDOWN);
1607 * find if a peer_ni already exists.
1608 * If so then just return that.
1610 lpni = lnet_find_peer_ni_locked(nid);
1614 lnet_net_unlock(cpt);
1616 rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1619 goto out_net_relock;
1622 lpni = lnet_find_peer_ni_locked(nid);
1632 * Get a peer_ni for the given nid, create it if necessary. Takes a
1633 * hold on the peer_ni.
1635 struct lnet_peer_ni *
1636 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1638 struct lnet_peer_ni *lpni = NULL;
1641 if (the_lnet.ln_state != LNET_STATE_RUNNING)
1642 return ERR_PTR(-ESHUTDOWN);
1645 * find if a peer_ni already exists.
1646 * If so then just return that.
1648 lpni = lnet_find_peer_ni_locked(nid);
1654 * use the lnet_api_mutex to serialize the creation of the peer_ni
1655 * and the creation/deletion of the local ni/net. When a local ni is
1656 * created, if there exists a set of peer_nis on that network,
1657 * they need to be traversed and updated. When a local NI is
1658 * deleted, which could result in a network being deleted, then
1659 * all peer nis on that network need to be removed as well.
1661 * Creation through traffic should also be serialized with
1662 * creation through DLC.
1664 lnet_net_unlock(cpt);
1665 mutex_lock(&the_lnet.ln_api_mutex);
1667 * Shutdown is only set under the ln_api_lock, so a single
1668 * check here is sufficent.
1670 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1671 lpni = ERR_PTR(-ESHUTDOWN);
1672 goto out_mutex_unlock;
1675 rc = lnet_peer_ni_traffic_add(nid, pref);
1678 goto out_mutex_unlock;
1681 lpni = lnet_find_peer_ni_locked(nid);
1685 mutex_unlock(&the_lnet.ln_api_mutex);
1688 /* Lock has been dropped, check again for shutdown. */
1689 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1691 lnet_peer_ni_decref_locked(lpni);
1692 lpni = ERR_PTR(-ESHUTDOWN);
1703 * Is a peer uptodate from the point of view of discovery?
1705 * If it is currently being processed, obviously not.
1706 * A forced Ping or Push is also handled by the discovery thread.
1708 * Otherwise look at whether the peer needs rediscovering.
1711 lnet_peer_is_uptodate(struct lnet_peer *lp)
1715 spin_lock(&lp->lp_lock);
1716 if (lp->lp_state & (LNET_PEER_DISCOVERING |
1717 LNET_PEER_FORCE_PING |
1718 LNET_PEER_FORCE_PUSH)) {
1720 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1722 } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1723 if (lnet_peer_discovery_disabled)
1727 } else if (lnet_peer_needs_push(lp)) {
1729 } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1730 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1737 spin_unlock(&lp->lp_lock);
1743 * Queue a peer for the attention of the discovery thread. Call with
1744 * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1745 * -EALREADY if the peer was already queued.
1747 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1751 spin_lock(&lp->lp_lock);
1752 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1753 lp->lp_state |= LNET_PEER_DISCOVERING;
1754 spin_unlock(&lp->lp_lock);
1755 if (list_empty(&lp->lp_dc_list)) {
1756 lnet_peer_addref_locked(lp);
1757 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1758 wake_up(&the_lnet.ln_dc_waitq);
1764 CDEBUG(D_NET, "Queue peer %s: %d\n",
1765 libcfs_nid2str(lp->lp_primary_nid), rc);
1771 * Discovery of a peer is complete. Wake all waiters on the peer.
1772 * Call with lnet_net_lock/EX held.
1774 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1776 struct lnet_msg *msg, *tmp;
1778 struct list_head pending_msgs;
1780 INIT_LIST_HEAD(&pending_msgs);
1782 CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1783 libcfs_nid2str(lp->lp_primary_nid));
1785 list_del_init(&lp->lp_dc_list);
1786 list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1787 wake_up_all(&lp->lp_dc_waitq);
1789 lnet_net_unlock(LNET_LOCK_EX);
1791 /* iterate through all pending messages and send them again */
1792 list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1793 list_del_init(&msg->msg_list);
1794 if (lp->lp_dc_error) {
1795 lnet_finalize(msg, lp->lp_dc_error);
1799 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1800 lnet_msgtyp2str(msg->msg_type),
1801 libcfs_id2str(msg->msg_target));
1802 rc = lnet_send(msg->msg_src_nid_param, msg,
1803 msg->msg_rtr_nid_param);
1805 CNETERR("Error sending %s to %s: %d\n",
1806 lnet_msgtyp2str(msg->msg_type),
1807 libcfs_id2str(msg->msg_target), rc);
1808 lnet_finalize(msg, rc);
1811 lnet_net_lock(LNET_LOCK_EX);
1812 lnet_peer_decref_locked(lp);
1816 * Handle inbound push.
1817 * Like any event handler, called with lnet_res_lock/CPT held.
1819 void lnet_peer_push_event(struct lnet_event *ev)
1821 struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1822 struct lnet_peer *lp;
1824 /* lnet_find_peer() adds a refcount */
1825 lp = lnet_find_peer(ev->source.nid);
1827 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1828 libcfs_nid2str(ev->initiator.nid),
1829 libcfs_nid2str(ev->source.nid));
1833 /* Ensure peer state remains consistent while we modify it. */
1834 spin_lock(&lp->lp_lock);
1837 * If some kind of error happened the contents of the message
1838 * cannot be used. Clear the NIDS_UPTODATE and set the
1839 * FORCE_PING flag to trigger a ping.
1842 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1843 lp->lp_state |= LNET_PEER_FORCE_PING;
1844 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1846 libcfs_nid2str(lp->lp_primary_nid),
1847 libcfs_nid2str(ev->source.nid));
1852 * A push with invalid or corrupted info. Clear the UPTODATE
1853 * flag to trigger a ping.
1855 if (lnet_ping_info_validate(&pbuf->pb_info)) {
1856 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1857 lp->lp_state |= LNET_PEER_FORCE_PING;
1858 CDEBUG(D_NET, "Corrupted Push from %s\n",
1859 libcfs_nid2str(lp->lp_primary_nid));
1864 * Make sure we'll allocate the correct size ping buffer when
1867 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
1868 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
1871 * A non-Multi-Rail peer is not supposed to be capable of
1874 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
1875 CERROR("Push from non-Multi-Rail peer %s dropped\n",
1876 libcfs_nid2str(lp->lp_primary_nid));
1881 * Check the MULTIRAIL flag. Complain if the peer was DLC
1882 * configured without it.
1884 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1885 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1886 CERROR("Push says %s is Multi-Rail, DLC says not\n",
1887 libcfs_nid2str(lp->lp_primary_nid));
1889 lp->lp_state |= LNET_PEER_MULTI_RAIL;
1890 lnet_peer_clr_non_mr_pref_nids(lp);
1895 * The peer may have discovery disabled at its end. Set
1896 * NO_DISCOVERY as appropriate.
1898 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
1899 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
1900 libcfs_nid2str(lp->lp_primary_nid));
1901 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
1902 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
1903 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
1904 libcfs_nid2str(lp->lp_primary_nid));
1905 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
1909 * Check for truncation of the Put message. Clear the
1910 * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
1911 * and tell discovery to allocate a bigger buffer.
1913 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
1914 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
1915 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
1916 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1917 lp->lp_state |= LNET_PEER_FORCE_PING;
1918 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
1919 libcfs_nid2str(lp->lp_primary_nid),
1920 pbuf->pb_info.pi_nnis);
1925 * Check whether the Put data is stale. Stale data can just be
1928 if (pbuf->pb_info.pi_nnis > 1 &&
1929 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid &&
1930 LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
1931 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1932 libcfs_nid2str(lp->lp_primary_nid),
1933 LNET_PING_BUFFER_SEQNO(pbuf),
1939 * Check whether the Put data is new, in which case we clear
1940 * the UPTODATE flag and prepare to process it.
1942 * If the Put data is current, and the peer is UPTODATE then
1943 * we assome everything is all right and drop the data as
1946 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno) {
1947 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
1948 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1949 } else if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
1950 CDEBUG(D_NET, "Stale Push from %s: got %u have %u\n",
1951 libcfs_nid2str(lp->lp_primary_nid),
1952 LNET_PING_BUFFER_SEQNO(pbuf),
1958 * If there is data present that hasn't been processed yet,
1959 * we'll replace it if the Put contained newer data and it
1960 * fits. We're racing with a Ping or earlier Push in this
1963 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
1964 if (LNET_PING_BUFFER_SEQNO(pbuf) >
1965 LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
1966 pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
1967 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1968 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1969 CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
1970 libcfs_nid2str(lp->lp_primary_nid),
1971 LNET_PING_BUFFER_SEQNO(pbuf),
1972 LNET_PING_BUFFER_SEQNO(lp->lp_data));
1978 * Allocate a buffer to copy the data. On a failure we drop
1979 * the Push and set FORCE_PING to force the discovery
1980 * thread to fix the problem by pinging the peer.
1982 lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
1984 lp->lp_state |= LNET_PEER_FORCE_PING;
1985 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
1986 libcfs_nid2str(lp->lp_primary_nid),
1987 LNET_PING_BUFFER_SEQNO(pbuf));
1992 memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
1993 LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
1994 lp->lp_state |= LNET_PEER_DATA_PRESENT;
1995 CDEBUG(D_NET, "Received Push %s %u\n",
1996 libcfs_nid2str(lp->lp_primary_nid),
1997 LNET_PING_BUFFER_SEQNO(pbuf));
2001 * Queue the peer for discovery if not done, force it on the request
2002 * queue and wake the discovery thread if the peer was already queued,
2003 * because its status changed.
2005 spin_unlock(&lp->lp_lock);
2006 lnet_net_lock(LNET_LOCK_EX);
2007 if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2008 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2009 wake_up(&the_lnet.ln_dc_waitq);
2011 /* Drop refcount from lookup */
2012 lnet_peer_decref_locked(lp);
2013 lnet_net_unlock(LNET_LOCK_EX);
2017 * Clear the discovery error state, unless we're already discovering
2018 * this peer, in which case the error is current.
2020 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2022 spin_lock(&lp->lp_lock);
2023 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2024 lp->lp_dc_error = 0;
2025 spin_unlock(&lp->lp_lock);
2029 * Peer discovery slow path. The ln_api_mutex is held on entry, and
2030 * dropped/retaken within this function. An lnet_peer_ni is passed in
2031 * because discovery could tear down an lnet_peer.
2034 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2037 struct lnet_peer *lp;
2041 lnet_net_unlock(cpt);
2042 lnet_net_lock(LNET_LOCK_EX);
2043 lp = lpni->lpni_peer_net->lpn_peer;
2044 lnet_peer_clear_discovery_error(lp);
2047 * We're willing to be interrupted. The lpni can become a
2048 * zombie if we race with DLC, so we must check for that.
2051 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2052 if (signal_pending(current))
2054 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2056 if (lp->lp_dc_error)
2058 if (lnet_peer_is_uptodate(lp))
2060 lnet_peer_queue_for_discovery(lp);
2062 * if caller requested a non-blocking operation then
2063 * return immediately. Once discovery is complete then the
2064 * peer ref will be decremented and any pending messages
2065 * that were stopped due to discovery will be transmitted.
2070 lnet_peer_addref_locked(lp);
2071 lnet_net_unlock(LNET_LOCK_EX);
2073 finish_wait(&lp->lp_dc_waitq, &wait);
2074 lnet_net_lock(LNET_LOCK_EX);
2075 lnet_peer_decref_locked(lp);
2076 /* Peer may have changed */
2077 lp = lpni->lpni_peer_net->lpn_peer;
2079 finish_wait(&lp->lp_dc_waitq, &wait);
2081 lnet_net_unlock(LNET_LOCK_EX);
2085 * If the peer has changed after we've discovered the older peer,
2086 * then we need to discovery the new peer to make sure the
2087 * interface information is up to date
2089 if (lp != lpni->lpni_peer_net->lpn_peer)
2092 if (signal_pending(current))
2094 else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2096 else if (lp->lp_dc_error)
2097 rc = lp->lp_dc_error;
2099 CDEBUG(D_NET, "non-blocking discovery\n");
2100 else if (!lnet_peer_is_uptodate(lp))
2103 CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2104 (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2105 libcfs_nid2str(lpni->lpni_nid), rc,
2106 (!block) ? "pending discovery" : "discovery complete");
2111 /* Handle an incoming ack for a push. */
2113 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2115 struct lnet_ping_buffer *pbuf;
2117 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2118 spin_lock(&lp->lp_lock);
2119 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2120 lp->lp_push_error = ev->status;
2122 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2124 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2125 spin_unlock(&lp->lp_lock);
2127 CDEBUG(D_NET, "peer %s ev->status %d\n",
2128 libcfs_nid2str(lp->lp_primary_nid), ev->status);
2131 /* Handle a Reply message. This is the reply to a Ping message. */
2133 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2135 struct lnet_ping_buffer *pbuf;
2138 spin_lock(&lp->lp_lock);
2141 * If some kind of error happened the contents of message
2142 * cannot be used. Set PING_FAILED to trigger a retry.
2145 lp->lp_state |= LNET_PEER_PING_FAILED;
2146 lp->lp_ping_error = ev->status;
2147 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2149 libcfs_nid2str(lp->lp_primary_nid),
2150 libcfs_nid2str(ev->source.nid));
2154 pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2155 if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2156 lnet_swap_pinginfo(pbuf);
2159 * A reply with invalid or corrupted info. Set PING_FAILED to
2162 rc = lnet_ping_info_validate(&pbuf->pb_info);
2164 lp->lp_state |= LNET_PEER_PING_FAILED;
2165 lp->lp_ping_error = 0;
2166 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2167 libcfs_nid2str(lp->lp_primary_nid), rc);
2172 * Update the MULTI_RAIL flag based on the reply. If the peer
2173 * was configured with DLC then the setting should match what
2176 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2177 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2178 /* Everything's fine */
2179 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2180 CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2181 libcfs_nid2str(lp->lp_primary_nid));
2183 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2184 lnet_peer_clr_non_mr_pref_nids(lp);
2186 } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2187 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2188 CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2189 libcfs_nid2str(lp->lp_primary_nid));
2191 CERROR("Multi-Rail state vanished from %s\n",
2192 libcfs_nid2str(lp->lp_primary_nid));
2193 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2198 * Make sure we'll allocate the correct size ping buffer when
2201 if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2202 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2205 * The peer may have discovery disabled at its end. Set
2206 * NO_DISCOVERY as appropriate.
2208 if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2209 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2210 libcfs_nid2str(lp->lp_primary_nid));
2211 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2212 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2213 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2214 libcfs_nid2str(lp->lp_primary_nid));
2215 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2219 * Check for truncation of the Reply. Clear PING_SENT and set
2220 * PING_FAILED to trigger a retry.
2222 if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2223 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2224 the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2225 lp->lp_state |= LNET_PEER_PING_FAILED;
2226 lp->lp_ping_error = 0;
2227 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2228 libcfs_nid2str(lp->lp_primary_nid),
2229 pbuf->pb_info.pi_nnis);
2234 * Check the sequence numbers in the reply. These are only
2235 * available if the reply came from a Multi-Rail peer.
2237 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2238 pbuf->pb_info.pi_nnis > 1 &&
2239 lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2240 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno) {
2241 CDEBUG(D_NET, "Stale Reply from %s: got %u have %u\n",
2242 libcfs_nid2str(lp->lp_primary_nid),
2243 LNET_PING_BUFFER_SEQNO(pbuf),
2248 if (LNET_PING_BUFFER_SEQNO(pbuf) > lp->lp_peer_seqno)
2249 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2252 /* We're happy with the state of the data in the buffer. */
2253 CDEBUG(D_NET, "peer %s data present %u\n",
2254 libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno);
2255 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2256 lnet_ping_buffer_decref(lp->lp_data);
2258 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2259 lnet_ping_buffer_addref(pbuf);
2262 lp->lp_state &= ~LNET_PEER_PING_SENT;
2263 spin_unlock(&lp->lp_lock);
2267 * Send event handling. Only matters for error cases, where we clean
2268 * up state on the peer and peer_ni that would otherwise be updated in
2269 * the REPLY event handler for a successful Ping, and the ACK event
2270 * handler for a successful Push.
2273 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2280 spin_lock(&lp->lp_lock);
2281 if (ev->msg_type == LNET_MSG_GET) {
2282 lp->lp_state &= ~LNET_PEER_PING_SENT;
2283 lp->lp_state |= LNET_PEER_PING_FAILED;
2284 lp->lp_ping_error = ev->status;
2285 } else { /* ev->msg_type == LNET_MSG_PUT */
2286 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2287 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2288 lp->lp_push_error = ev->status;
2290 spin_unlock(&lp->lp_lock);
2291 rc = LNET_REDISCOVER_PEER;
2293 CDEBUG(D_NET, "%s Send to %s: %d\n",
2294 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2295 libcfs_nid2str(ev->target.nid), rc);
2300 * Unlink event handling. This event is only seen if a call to
2301 * LNetMDUnlink() caused the event to be unlinked. If this call was
2302 * made after the event was set up in LNetGet() or LNetPut() then we
2303 * assume the Ping or Push timed out.
2306 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2308 spin_lock(&lp->lp_lock);
2309 /* We've passed through LNetGet() */
2310 if (lp->lp_state & LNET_PEER_PING_SENT) {
2311 lp->lp_state &= ~LNET_PEER_PING_SENT;
2312 lp->lp_state |= LNET_PEER_PING_FAILED;
2313 lp->lp_ping_error = -ETIMEDOUT;
2314 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2315 libcfs_nid2str(lp->lp_primary_nid));
2317 /* We've passed through LNetPut() */
2318 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2319 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2320 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2321 lp->lp_push_error = -ETIMEDOUT;
2322 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2323 libcfs_nid2str(lp->lp_primary_nid));
2325 spin_unlock(&lp->lp_lock);
2329 * Event handler for the discovery EQ.
2331 * Called with lnet_res_lock(cpt) held. The cpt is the
2332 * lnet_cpt_of_cookie() of the md handle cookie.
2334 static void lnet_discovery_event_handler(struct lnet_event *event)
2336 struct lnet_peer *lp = event->md.user_ptr;
2337 struct lnet_ping_buffer *pbuf;
2340 /* discovery needs to take another look */
2341 rc = LNET_REDISCOVER_PEER;
2343 CDEBUG(D_NET, "Received event: %d\n", event->type);
2345 switch (event->type) {
2346 case LNET_EVENT_ACK:
2347 lnet_discovery_event_ack(lp, event);
2349 case LNET_EVENT_REPLY:
2350 lnet_discovery_event_reply(lp, event);
2352 case LNET_EVENT_SEND:
2353 /* Only send failure triggers a retry. */
2354 rc = lnet_discovery_event_send(lp, event);
2356 case LNET_EVENT_UNLINK:
2357 /* LNetMDUnlink() was called */
2358 lnet_discovery_event_unlink(lp, event);
2361 /* Invalid events. */
2364 lnet_net_lock(LNET_LOCK_EX);
2365 if (event->unlinked) {
2366 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2367 lnet_ping_buffer_decref(pbuf);
2368 lnet_peer_decref_locked(lp);
2371 /* put peer back at end of request queue, if discovery not already
2373 if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2374 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2375 wake_up(&the_lnet.ln_dc_waitq);
2377 lnet_net_unlock(LNET_LOCK_EX);
2381 * Build a peer from incoming data.
2383 * The NIDs in the incoming data are supposed to be structured as follows:
2386 * - other NIDs in same net
2387 * - NIDs in second net
2388 * - NIDs in third net
2390 * This due to the way the list of NIDs in the data is created.
2392 * Note that this function will mark the peer uptodate unless an
2393 * ENOMEM is encontered. All other errors are due to a conflict
2394 * between the DLC configuration and what discovery sees. We treat DLC
2395 * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2396 * peer from becoming stuck in discovery.
2398 static int lnet_peer_merge_data(struct lnet_peer *lp,
2399 struct lnet_ping_buffer *pbuf)
2401 struct lnet_peer_ni *lpni;
2402 lnet_nid_t *curnis = NULL;
2403 lnet_nid_t *addnis = NULL;
2404 lnet_nid_t *delnis = NULL;
2414 flags = LNET_PEER_DISCOVERED;
2415 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2416 flags |= LNET_PEER_MULTI_RAIL;
2418 nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2419 LIBCFS_ALLOC(curnis, nnis * sizeof(lnet_nid_t));
2420 LIBCFS_ALLOC(addnis, nnis * sizeof(lnet_nid_t));
2421 LIBCFS_ALLOC(delnis, nnis * sizeof(lnet_nid_t));
2422 if (!curnis || !addnis || !delnis) {
2430 /* Construct the list of NIDs present in peer. */
2432 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2433 curnis[ncurnis++] = lpni->lpni_nid;
2436 * Check for NIDs in pbuf not present in curnis[].
2437 * The loop starts at 1 to skip the loopback NID.
2439 for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2440 for (j = 0; j < ncurnis; j++)
2441 if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2444 addnis[naddnis++] = pbuf->pb_info.pi_ni[i].ns_nid;
2447 * Check for NIDs in curnis[] not present in pbuf.
2448 * The nested loop starts at 1 to skip the loopback NID.
2450 * But never add the loopback NID to delnis[]: if it is
2451 * present in curnis[] then this peer is for this node.
2453 for (i = 0; i < ncurnis; i++) {
2454 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2456 for (j = 1; j < pbuf->pb_info.pi_nnis; j++)
2457 if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid)
2459 if (j == pbuf->pb_info.pi_nnis)
2460 delnis[ndelnis++] = curnis[i];
2463 for (i = 0; i < naddnis; i++) {
2464 rc = lnet_peer_add_nid(lp, addnis[i], flags);
2466 CERROR("Error adding NID %s to peer %s: %d\n",
2467 libcfs_nid2str(addnis[i]),
2468 libcfs_nid2str(lp->lp_primary_nid), rc);
2473 for (i = 0; i < ndelnis; i++) {
2474 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2476 CERROR("Error deleting NID %s from peer %s: %d\n",
2477 libcfs_nid2str(delnis[i]),
2478 libcfs_nid2str(lp->lp_primary_nid), rc);
2484 * Errors other than -ENOMEM are due to peers having been
2485 * configured with DLC. Ignore these because DLC overrides
2490 LIBCFS_FREE(curnis, nnis * sizeof(lnet_nid_t));
2491 LIBCFS_FREE(addnis, nnis * sizeof(lnet_nid_t));
2492 LIBCFS_FREE(delnis, nnis * sizeof(lnet_nid_t));
2493 lnet_ping_buffer_decref(pbuf);
2494 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2497 spin_lock(&lp->lp_lock);
2498 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2499 lp->lp_state |= LNET_PEER_FORCE_PING;
2500 spin_unlock(&lp->lp_lock);
2506 * The data in pbuf says lp is its primary peer, but the data was
2507 * received by a different peer. Try to update lp with the data.
2510 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2512 struct lnet_handle_md mdh;
2514 /* Queue lp for discovery, and force it on the request queue. */
2515 lnet_net_lock(LNET_LOCK_EX);
2516 if (lnet_peer_queue_for_discovery(lp))
2517 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2518 lnet_net_unlock(LNET_LOCK_EX);
2520 LNetInvalidateMDHandle(&mdh);
2523 * Decide whether we can move the peer to the DATA_PRESENT state.
2525 * We replace stale data for a multi-rail peer, repair PING_FAILED
2526 * status, and preempt FORCE_PING.
2528 * If after that we have DATA_PRESENT, we merge it into this peer.
2530 spin_lock(&lp->lp_lock);
2531 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2532 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2533 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2534 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2535 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2536 lnet_ping_buffer_decref(pbuf);
2541 if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2542 lnet_ping_buffer_decref(lp->lp_data);
2544 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2546 if (lp->lp_state & LNET_PEER_PING_FAILED) {
2547 mdh = lp->lp_ping_mdh;
2548 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2549 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2550 lp->lp_ping_error = 0;
2552 if (lp->lp_state & LNET_PEER_FORCE_PING)
2553 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2554 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2555 spin_unlock(&lp->lp_lock);
2557 if (!LNetMDHandleIsInvalid(mdh))
2561 return lnet_peer_merge_data(lp, pbuf);
2563 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2568 * Update a peer using the data received.
2570 static int lnet_peer_data_present(struct lnet_peer *lp)
2571 __must_hold(&lp->lp_lock)
2573 struct lnet_ping_buffer *pbuf;
2574 struct lnet_peer_ni *lpni;
2575 lnet_nid_t nid = LNET_NID_ANY;
2581 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2582 lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2583 spin_unlock(&lp->lp_lock);
2586 * Modifications of peer structures are done while holding the
2587 * ln_api_mutex. A global lock is required because we may be
2588 * modifying multiple peer structures, and a mutex greatly
2589 * simplifies memory management.
2591 * The actual changes to the data structures must also protect
2592 * against concurrent lookups, for which the lnet_net_lock in
2593 * LNET_LOCK_EX mode is used.
2595 mutex_lock(&the_lnet.ln_api_mutex);
2596 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2602 * If this peer is not on the peer list then it is being torn
2603 * down, and our reference count may be all that is keeping it
2604 * alive. Don't do any work on it.
2606 if (list_empty(&lp->lp_peer_list))
2609 flags = LNET_PEER_DISCOVERED;
2610 if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2611 flags |= LNET_PEER_MULTI_RAIL;
2614 * Check whether the primary NID in the message matches the
2615 * primary NID of the peer. If it does, update the peer, if
2616 * it it does not, check whether there is already a peer with
2617 * that primary NID. If no such peer exists, try to update
2618 * the primary NID of the current peer (allowed if it was
2619 * created due to message traffic) and complete the update.
2620 * If the peer did exist, hand off the data to it.
2622 * The peer for the loopback interface is a special case: this
2623 * is the peer for the local node, and we want to set its
2624 * primary NID to the correct value here. Moreover, this peer
2625 * can show up with only the loopback NID in the ping buffer.
2627 if (pbuf->pb_info.pi_nnis <= 1)
2629 nid = pbuf->pb_info.pi_ni[1].ns_nid;
2630 if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2631 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2633 rc = lnet_peer_merge_data(lp, pbuf);
2634 } else if (lp->lp_primary_nid == nid) {
2635 rc = lnet_peer_merge_data(lp, pbuf);
2637 lpni = lnet_find_peer_ni_locked(nid);
2639 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2641 CERROR("Primary NID error %s versus %s: %d\n",
2642 libcfs_nid2str(lp->lp_primary_nid),
2643 libcfs_nid2str(nid), rc);
2645 rc = lnet_peer_merge_data(lp, pbuf);
2648 rc = lnet_peer_set_primary_data(
2649 lpni->lpni_peer_net->lpn_peer, pbuf);
2650 lnet_peer_ni_decref_locked(lpni);
2654 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2655 mutex_unlock(&the_lnet.ln_api_mutex);
2657 spin_lock(&lp->lp_lock);
2658 /* Tell discovery to re-check the peer immediately. */
2660 rc = LNET_REDISCOVER_PEER;
2665 * A ping failed. Clear the PING_FAILED state and set the
2666 * FORCE_PING state, to ensure a retry even if discovery is
2667 * disabled. This avoids being left with incorrect state.
2669 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2670 __must_hold(&lp->lp_lock)
2672 struct lnet_handle_md mdh;
2675 mdh = lp->lp_ping_mdh;
2676 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2677 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2678 lp->lp_state |= LNET_PEER_FORCE_PING;
2679 rc = lp->lp_ping_error;
2680 lp->lp_ping_error = 0;
2681 spin_unlock(&lp->lp_lock);
2683 if (!LNetMDHandleIsInvalid(mdh))
2686 CDEBUG(D_NET, "peer %s:%d\n",
2687 libcfs_nid2str(lp->lp_primary_nid), rc);
2689 spin_lock(&lp->lp_lock);
2690 return rc ? rc : LNET_REDISCOVER_PEER;
2694 * Select NID to send a Ping or Push to.
2696 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2698 struct lnet_peer_ni *lpni;
2700 /* Look for a direct-connected NID for this peer. */
2702 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2703 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2708 return lpni->lpni_nid;
2710 /* Look for a routed-connected NID for this peer. */
2712 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2713 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2718 return lpni->lpni_nid;
2720 return LNET_NID_ANY;
2723 /* Active side of ping. */
2724 static int lnet_peer_send_ping(struct lnet_peer *lp)
2725 __must_hold(&lp->lp_lock)
2732 lp->lp_state |= LNET_PEER_PING_SENT;
2733 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2734 spin_unlock(&lp->lp_lock);
2736 cpt = lnet_net_lock_current();
2737 /* Refcount for MD. */
2738 lnet_peer_addref_locked(lp);
2739 pnid = lnet_peer_select_nid(lp);
2740 lnet_net_unlock(cpt);
2742 nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2744 rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2745 the_lnet.ln_dc_eqh, false);
2748 * if LNetMDBind in lnet_send_ping fails we need to decrement the
2749 * refcount on the peer, otherwise LNetMDUnlink will be called
2750 * which will eventually do that.
2754 lnet_peer_decref_locked(lp);
2755 lnet_net_unlock(cpt);
2756 rc = -rc; /* change the rc to negative value */
2758 } else if (rc < 0) {
2762 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2764 spin_lock(&lp->lp_lock);
2768 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2770 * The errors that get us here are considered hard errors and
2771 * cause Discovery to terminate. So we clear PING_SENT, but do
2772 * not set either PING_FAILED or FORCE_PING. In fact we need
2773 * to clear PING_FAILED, because the unlink event handler will
2774 * have set it if we called LNetMDUnlink() above.
2776 spin_lock(&lp->lp_lock);
2777 lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
2782 * This function exists because you cannot call LNetMDUnlink() from an
2785 static int lnet_peer_push_failed(struct lnet_peer *lp)
2786 __must_hold(&lp->lp_lock)
2788 struct lnet_handle_md mdh;
2791 mdh = lp->lp_push_mdh;
2792 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2793 lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
2794 rc = lp->lp_push_error;
2795 lp->lp_push_error = 0;
2796 spin_unlock(&lp->lp_lock);
2798 if (!LNetMDHandleIsInvalid(mdh))
2801 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2802 spin_lock(&lp->lp_lock);
2803 return rc ? rc : LNET_REDISCOVER_PEER;
2806 /* Active side of push. */
2807 static int lnet_peer_send_push(struct lnet_peer *lp)
2808 __must_hold(&lp->lp_lock)
2810 struct lnet_ping_buffer *pbuf;
2811 struct lnet_process_id id;
2816 /* Don't push to a non-multi-rail peer. */
2817 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2818 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2822 lp->lp_state |= LNET_PEER_PUSH_SENT;
2823 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
2824 spin_unlock(&lp->lp_lock);
2826 cpt = lnet_net_lock_current();
2827 pbuf = the_lnet.ln_ping_target;
2828 lnet_ping_buffer_addref(pbuf);
2829 lnet_net_unlock(cpt);
2831 /* Push source MD */
2832 md.start = &pbuf->pb_info;
2833 md.length = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
2834 md.threshold = 2; /* Put/Ack */
2837 md.eq_handle = the_lnet.ln_dc_eqh;
2840 rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
2842 lnet_ping_buffer_decref(pbuf);
2843 CERROR("Can't bind push source MD: %d\n", rc);
2846 cpt = lnet_net_lock_current();
2847 /* Refcount for MD. */
2848 lnet_peer_addref_locked(lp);
2849 id.pid = LNET_PID_LUSTRE;
2850 id.nid = lnet_peer_select_nid(lp);
2851 lnet_net_unlock(cpt);
2853 if (id.nid == LNET_NID_ANY) {
2858 rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
2859 LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
2860 LNET_PROTO_PING_MATCHBITS, 0, 0);
2865 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2867 spin_lock(&lp->lp_lock);
2871 LNetMDUnlink(lp->lp_push_mdh);
2872 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2874 CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
2876 * The errors that get us here are considered hard errors and
2877 * cause Discovery to terminate. So we clear PUSH_SENT, but do
2878 * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
2879 * because the unlink event handler will have set it if we
2880 * called LNetMDUnlink() above.
2882 spin_lock(&lp->lp_lock);
2883 lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
2888 * An unrecoverable error was encountered during discovery.
2889 * Set error status in peer and abort discovery.
2891 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
2893 CDEBUG(D_NET, "Discovery error %s: %d\n",
2894 libcfs_nid2str(lp->lp_primary_nid), error);
2896 spin_lock(&lp->lp_lock);
2897 lp->lp_dc_error = error;
2898 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2899 lp->lp_state |= LNET_PEER_REDISCOVER;
2900 spin_unlock(&lp->lp_lock);
2904 * Mark the peer as discovered.
2906 static int lnet_peer_discovered(struct lnet_peer *lp)
2907 __must_hold(&lp->lp_lock)
2909 lp->lp_state |= LNET_PEER_DISCOVERED;
2910 lp->lp_state &= ~(LNET_PEER_DISCOVERING |
2911 LNET_PEER_REDISCOVER);
2913 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2919 * Mark the peer as to be rediscovered.
2921 static int lnet_peer_rediscover(struct lnet_peer *lp)
2922 __must_hold(&lp->lp_lock)
2924 lp->lp_state |= LNET_PEER_REDISCOVER;
2925 lp->lp_state &= ~LNET_PEER_DISCOVERING;
2927 CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2933 * Discovering this peer is taking too long. Cancel any Ping or Push
2934 * that discovery is waiting on by unlinking the relevant MDs. The
2935 * lnet_discovery_event_handler() will proceed from here and complete
2938 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
2940 struct lnet_handle_md ping_mdh;
2941 struct lnet_handle_md push_mdh;
2943 LNetInvalidateMDHandle(&ping_mdh);
2944 LNetInvalidateMDHandle(&push_mdh);
2946 spin_lock(&lp->lp_lock);
2947 if (lp->lp_state & LNET_PEER_PING_SENT) {
2948 ping_mdh = lp->lp_ping_mdh;
2949 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2951 if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2952 push_mdh = lp->lp_push_mdh;
2953 LNetInvalidateMDHandle(&lp->lp_push_mdh);
2955 spin_unlock(&lp->lp_lock);
2957 if (!LNetMDHandleIsInvalid(ping_mdh))
2958 LNetMDUnlink(ping_mdh);
2959 if (!LNetMDHandleIsInvalid(push_mdh))
2960 LNetMDUnlink(push_mdh);
2964 * Wait for work to be queued or some other change that must be
2965 * attended to. Returns non-zero if the discovery thread should shut
2968 static int lnet_peer_discovery_wait_for_work(void)
2975 cpt = lnet_net_lock_current();
2977 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
2978 TASK_INTERRUPTIBLE);
2979 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
2981 if (lnet_push_target_resize_needed())
2983 if (!list_empty(&the_lnet.ln_dc_request))
2985 if (!list_empty(&the_lnet.ln_msg_resend))
2987 lnet_net_unlock(cpt);
2990 * wakeup max every second to check if there are peers that
2991 * have been stuck on the working queue for greater than
2994 schedule_timeout(cfs_time_seconds(1));
2995 finish_wait(&the_lnet.ln_dc_waitq, &wait);
2996 cpt = lnet_net_lock_current();
2998 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3000 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3003 lnet_net_unlock(cpt);
3005 CDEBUG(D_NET, "woken: %d\n", rc);
3011 * Messages that were pending on a destroyed peer will be put on a global
3012 * resend list. The message resend list will be checked by
3013 * the discovery thread when it wakes up, and will resend messages. These
3014 * messages can still be sendable in the case the lpni which was the initial
3015 * cause of the message re-queue was transfered to another peer.
3017 * It is possible that LNet could be shutdown while we're iterating
3018 * through the list. lnet_shudown_lndnets() will attempt to access the
3019 * resend list, but will have to wait until the spinlock is released, by
3020 * which time there shouldn't be any more messages on the resend list.
3021 * During shutdown lnet_send() will fail and lnet_finalize() will be called
3022 * for the messages so they can be released. The other case is that
3023 * lnet_shudown_lndnets() can finalize all the messages before this
3024 * function can visit the resend list, in which case this function will be
3027 static void lnet_resend_msgs(void)
3029 struct lnet_msg *msg, *tmp;
3030 struct list_head resend;
3033 INIT_LIST_HEAD(&resend);
3035 spin_lock(&the_lnet.ln_msg_resend_lock);
3036 list_splice(&the_lnet.ln_msg_resend, &resend);
3037 spin_unlock(&the_lnet.ln_msg_resend_lock);
3039 list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3040 list_del_init(&msg->msg_list);
3041 rc = lnet_send(msg->msg_src_nid_param, msg,
3042 msg->msg_rtr_nid_param);
3044 CNETERR("Error sending %s to %s: %d\n",
3045 lnet_msgtyp2str(msg->msg_type),
3046 libcfs_id2str(msg->msg_target), rc);
3047 lnet_finalize(msg, rc);
3052 /* The discovery thread. */
3053 static int lnet_peer_discovery(void *arg)
3055 struct lnet_peer *lp;
3058 CDEBUG(D_NET, "started\n");
3059 cfs_block_allsigs();
3062 if (lnet_peer_discovery_wait_for_work())
3067 if (lnet_push_target_resize_needed())
3068 lnet_push_target_resize();
3070 lnet_net_lock(LNET_LOCK_EX);
3071 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3075 * Process all incoming discovery work requests. When
3076 * discovery must wait on a peer to change state, it
3077 * is added to the tail of the ln_dc_working queue. A
3078 * timestamp keeps track of when the peer was added,
3079 * so we can time out discovery requests that take too
3082 while (!list_empty(&the_lnet.ln_dc_request)) {
3083 lp = list_first_entry(&the_lnet.ln_dc_request,
3084 struct lnet_peer, lp_dc_list);
3085 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3087 * set the time the peer was put on the dc_working
3088 * queue. It shouldn't remain on the queue
3089 * forever, in case the GET message (for ping)
3090 * doesn't get a REPLY or the PUT message (for
3091 * push) doesn't get an ACK.
3093 lp->lp_last_queued = ktime_get_real_seconds();
3094 lnet_net_unlock(LNET_LOCK_EX);
3097 * Select an action depending on the state of
3098 * the peer and whether discovery is disabled.
3099 * The check whether discovery is disabled is
3100 * done after the code that handles processing
3101 * for arrived data, cleanup for failures, and
3102 * forcing a Ping or Push.
3104 spin_lock(&lp->lp_lock);
3105 CDEBUG(D_NET, "peer %s state %#x\n",
3106 libcfs_nid2str(lp->lp_primary_nid),
3108 if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3109 rc = lnet_peer_data_present(lp);
3110 else if (lp->lp_state & LNET_PEER_PING_FAILED)
3111 rc = lnet_peer_ping_failed(lp);
3112 else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3113 rc = lnet_peer_push_failed(lp);
3114 else if (lp->lp_state & LNET_PEER_FORCE_PING)
3115 rc = lnet_peer_send_ping(lp);
3116 else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3117 rc = lnet_peer_send_push(lp);
3118 else if (lnet_peer_discovery_disabled)
3119 rc = lnet_peer_rediscover(lp);
3120 else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3121 rc = lnet_peer_send_ping(lp);
3122 else if (lnet_peer_needs_push(lp))
3123 rc = lnet_peer_send_push(lp);
3125 rc = lnet_peer_discovered(lp);
3126 CDEBUG(D_NET, "peer %s state %#x rc %d\n",
3127 libcfs_nid2str(lp->lp_primary_nid),
3129 spin_unlock(&lp->lp_lock);
3131 lnet_net_lock(LNET_LOCK_EX);
3132 if (rc == LNET_REDISCOVER_PEER) {
3133 list_move(&lp->lp_dc_list,
3134 &the_lnet.ln_dc_request);
3136 lnet_peer_discovery_error(lp, rc);
3138 if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3139 lnet_peer_discovery_complete(lp);
3140 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3144 lnet_net_unlock(LNET_LOCK_EX);
3147 CDEBUG(D_NET, "stopping\n");
3149 * Clean up before telling lnet_peer_discovery_stop() that
3150 * we're done. Use wake_up() below to somewhat reduce the
3151 * size of the thundering herd if there are multiple threads
3152 * waiting on discovery of a single peer.
3154 LNetEQFree(the_lnet.ln_dc_eqh);
3155 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3157 /* Queue cleanup 1: stop all pending pings and pushes. */
3158 lnet_net_lock(LNET_LOCK_EX);
3159 while (!list_empty(&the_lnet.ln_dc_working)) {
3160 lp = list_first_entry(&the_lnet.ln_dc_working,
3161 struct lnet_peer, lp_dc_list);
3162 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3163 lnet_net_unlock(LNET_LOCK_EX);
3164 lnet_peer_cancel_discovery(lp);
3165 lnet_net_lock(LNET_LOCK_EX);
3167 lnet_net_unlock(LNET_LOCK_EX);
3169 /* Queue cleanup 2: wait for the expired queue to clear. */
3170 while (!list_empty(&the_lnet.ln_dc_expired))
3171 schedule_timeout(cfs_time_seconds(1));
3173 /* Queue cleanup 3: clear the request queue. */
3174 lnet_net_lock(LNET_LOCK_EX);
3175 while (!list_empty(&the_lnet.ln_dc_request)) {
3176 lp = list_first_entry(&the_lnet.ln_dc_request,
3177 struct lnet_peer, lp_dc_list);
3178 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3179 lnet_peer_discovery_complete(lp);
3181 lnet_net_unlock(LNET_LOCK_EX);
3183 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3184 wake_up(&the_lnet.ln_dc_waitq);
3186 CDEBUG(D_NET, "stopped\n");
3191 /* ln_api_mutex is held on entry. */
3192 int lnet_peer_discovery_start(void)
3194 struct task_struct *task;
3197 if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3200 rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3202 CERROR("Can't allocate discovery EQ: %d\n", rc);
3206 the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3207 task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3210 CERROR("Can't start peer discovery thread: %d\n", rc);
3212 LNetEQFree(the_lnet.ln_dc_eqh);
3213 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3215 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3218 CDEBUG(D_NET, "discovery start: %d\n", rc);
3223 /* ln_api_mutex is held on entry. */
3224 void lnet_peer_discovery_stop(void)
3226 if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3229 LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3230 the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3231 wake_up(&the_lnet.ln_dc_waitq);
3233 wait_event(the_lnet.ln_dc_waitq,
3234 the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3236 LASSERT(list_empty(&the_lnet.ln_dc_request));
3237 LASSERT(list_empty(&the_lnet.ln_dc_working));
3238 LASSERT(list_empty(&the_lnet.ln_dc_expired));
3240 CDEBUG(D_NET, "discovery stopped\n");
3246 lnet_debug_peer(lnet_nid_t nid)
3248 char *aliveness = "NA";
3249 struct lnet_peer_ni *lp;
3252 cpt = lnet_cpt_of_nid(nid, NULL);
3255 lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3257 lnet_net_unlock(cpt);
3258 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3262 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3263 aliveness = lp->lpni_alive ? "up" : "down";
3265 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3266 libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3267 aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3268 lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3269 lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3271 lnet_peer_ni_decref_locked(lp);
3273 lnet_net_unlock(cpt);
3276 /* Gathering information for userspace. */
3278 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3279 char aliveness[LNET_MAX_STR_LEN],
3280 __u32 *cpt_iter, __u32 *refcount,
3281 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3282 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3283 __u32 *peer_tx_qnob)
3285 struct lnet_peer_table *peer_table;
3286 struct lnet_peer_ni *lp;
3291 /* get the number of CPTs */
3292 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3294 /* if the cpt number to be examined is >= the number of cpts in
3295 * the system then indicate that there are no more cpts to examin
3297 if (*cpt_iter >= lncpt)
3300 /* get the current table */
3301 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3302 /* if the ptable is NULL then there are no more cpts to examine */
3303 if (peer_table == NULL)
3306 lnet_net_lock(*cpt_iter);
3308 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3309 struct list_head *peers = &peer_table->pt_hash[j];
3311 list_for_each_entry(lp, peers, lpni_hashlist) {
3312 if (peer_index-- > 0)
3315 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3316 if (lnet_isrouter(lp) ||
3317 lnet_peer_aliveness_enabled(lp))
3318 snprintf(aliveness, LNET_MAX_STR_LEN,
3319 lp->lpni_alive ? "up" : "down");
3321 *nid = lp->lpni_nid;
3322 *refcount = atomic_read(&lp->lpni_refcount);
3323 *ni_peer_tx_credits =
3324 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3325 *peer_tx_credits = lp->lpni_txcredits;
3326 *peer_rtr_credits = lp->lpni_rtrcredits;
3327 *peer_min_rtr_credits = lp->lpni_mintxcredits;
3328 *peer_tx_qnob = lp->lpni_txqnob;
3334 lnet_net_unlock(*cpt_iter);
3338 return found ? 0 : -ENOENT;
3341 /* ln_api_mutex is held, which keeps the peer list stable */
3342 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3344 struct lnet_ioctl_element_stats *lpni_stats;
3345 struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3346 struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3347 struct lnet_peer_ni_credit_info *lpni_info;
3348 struct lnet_peer_ni *lpni;
3349 struct lnet_peer *lp;
3354 lp = lnet_find_peer(cfg->prcfg_prim_nid);
3361 size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3362 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3363 size *= lp->lp_nnis;
3364 if (size > cfg->prcfg_size) {
3365 cfg->prcfg_size = size;
3370 cfg->prcfg_prim_nid = lp->lp_primary_nid;
3371 cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3372 cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3373 cfg->prcfg_count = lp->lp_nnis;
3374 cfg->prcfg_size = size;
3375 cfg->prcfg_state = lp->lp_state;
3377 /* Allocate helper buffers. */
3379 LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3382 LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3385 LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3386 if (!lpni_msg_stats)
3387 goto out_free_stats;
3388 LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3390 goto out_free_msg_stats;
3395 while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3396 nid = lpni->lpni_nid;
3397 if (copy_to_user(bulk, &nid, sizeof(nid)))
3398 goto out_free_hstats;
3399 bulk += sizeof(nid);
3401 memset(lpni_info, 0, sizeof(*lpni_info));
3402 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3403 if (lnet_isrouter(lpni) ||
3404 lnet_peer_aliveness_enabled(lpni))
3405 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3406 lpni->lpni_alive ? "up" : "down");
3408 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3409 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3410 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3411 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3412 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3413 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3414 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3415 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3416 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3417 goto out_free_hstats;
3418 bulk += sizeof(*lpni_info);
3420 memset(lpni_stats, 0, sizeof(*lpni_stats));
3421 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3422 LNET_STATS_TYPE_SEND);
3423 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3424 LNET_STATS_TYPE_RECV);
3425 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3426 LNET_STATS_TYPE_DROP);
3427 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3428 goto out_free_hstats;
3429 bulk += sizeof(*lpni_stats);
3430 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3431 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3432 goto out_free_hstats;
3433 bulk += sizeof(*lpni_msg_stats);
3434 lpni_hstats->hlpni_network_timeout =
3435 atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3436 lpni_hstats->hlpni_remote_dropped =
3437 atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3438 lpni_hstats->hlpni_remote_timeout =
3439 atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3440 lpni_hstats->hlpni_remote_error =
3441 atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3442 lpni_hstats->hlpni_health_value =
3443 atomic_read(&lpni->lpni_healthv);
3444 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3445 goto out_free_hstats;
3446 bulk += sizeof(*lpni_hstats);
3451 LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3453 LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3455 LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3457 LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3459 lnet_peer_decref_locked(lp);
3465 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3467 /* the mt could've shutdown and cleaned up the queues */
3468 if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3471 if (list_empty(&lpni->lpni_recovery) &&
3472 atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3473 CERROR("lpni %s added to recovery queue. Health = %d\n",
3474 libcfs_nid2str(lpni->lpni_nid),
3475 atomic_read(&lpni->lpni_healthv));
3476 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3477 lnet_peer_ni_addref_locked(lpni);
3481 /* Call with the ln_api_mutex held */
3483 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3485 struct lnet_peer_table *ptable;
3486 struct lnet_peer *lp;
3487 struct lnet_peer_net *lpn;
3488 struct lnet_peer_ni *lpni;
3492 if (the_lnet.ln_state != LNET_STATE_RUNNING)
3496 lnet_net_lock(LNET_LOCK_EX);
3497 lpni = lnet_find_peer_ni_locked(nid);
3499 lnet_net_unlock(LNET_LOCK_EX);
3502 atomic_set(&lpni->lpni_healthv, value);
3503 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3504 lnet_peer_ni_decref_locked(lpni);
3505 lnet_net_unlock(LNET_LOCK_EX);
3509 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3512 * Walk all the peers and reset the healhv for each one to the
3515 lnet_net_lock(LNET_LOCK_EX);
3516 for (cpt = 0; cpt < lncpt; cpt++) {
3517 ptable = the_lnet.ln_peer_tables[cpt];
3518 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3519 list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3520 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3522 atomic_set(&lpni->lpni_healthv, value);
3523 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3528 lnet_net_unlock(LNET_LOCK_EX);