Whamcloud - gitweb
33bd35144c6e6ad7d0b0eaa70216e06f8e938b6d
[fs/lustre-release.git] / lnet / lnet / peer.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lnet/lnet/peer.c
32  */
33
34 #define DEBUG_SUBSYSTEM S_LNET
35
36 #include <linux/sched.h>
37 #ifdef HAVE_SCHED_HEADERS
38 #include <linux/sched/signal.h>
39 #endif
40 #include <linux/uaccess.h>
41
42 #include <lnet/udsp.h>
43 #include <lnet/lib-lnet.h>
44 #include <uapi/linux/lnet/lnet-dlc.h>
45
46 /* Value indicating that recovery needs to re-check a peer immediately. */
47 #define LNET_REDISCOVER_PEER    (1)
48
49 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp);
50
51 static void
52 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
53 {
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);
57         }
58 }
59
60 void
61 lnet_peer_net_added(struct lnet_net *net)
62 {
63         struct lnet_peer_ni *lpni, *tmp;
64
65         list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
66                                  lpni_on_remote_peer_ni_list) {
67
68                 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
69                         lpni->lpni_net = net;
70
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);
79
80                         lnet_peer_remove_from_remote_list(lpni);
81                 }
82         }
83 }
84
85 static void
86 lnet_peer_tables_destroy(void)
87 {
88         struct lnet_peer_table  *ptable;
89         struct list_head        *hash;
90         int                     i;
91         int                     j;
92
93         if (!the_lnet.ln_peer_tables)
94                 return;
95
96         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
97                 hash = ptable->pt_hash;
98                 if (!hash) /* not intialized */
99                         break;
100
101                 LASSERT(list_empty(&ptable->pt_zombie_list));
102
103                 ptable->pt_hash = NULL;
104                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
105                         LASSERT(list_empty(&hash[j]));
106
107                 CFS_FREE_PTR_ARRAY(hash, LNET_PEER_HASH_SIZE);
108         }
109
110         cfs_percpt_free(the_lnet.ln_peer_tables);
111         the_lnet.ln_peer_tables = NULL;
112 }
113
114 int
115 lnet_peer_tables_create(void)
116 {
117         struct lnet_peer_table  *ptable;
118         struct list_head        *hash;
119         int                     i;
120         int                     j;
121
122         the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
123                                                    sizeof(*ptable));
124         if (the_lnet.ln_peer_tables == NULL) {
125                 CERROR("Failed to allocate cpu-partition peer tables\n");
126                 return -ENOMEM;
127         }
128
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));
132                 if (hash == NULL) {
133                         CERROR("Failed to create peer hash table\n");
134                         lnet_peer_tables_destroy();
135                         return -ENOMEM;
136                 }
137
138                 spin_lock_init(&ptable->pt_zombie_lock);
139                 INIT_LIST_HEAD(&ptable->pt_zombie_list);
140
141                 INIT_LIST_HEAD(&ptable->pt_peer_list);
142
143                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
144                         INIT_LIST_HEAD(&hash[j]);
145                 ptable->pt_hash = hash; /* sign of initialization */
146         }
147
148         return 0;
149 }
150
151 static struct lnet_peer_ni *
152 lnet_peer_ni_alloc(lnet_nid_t nid)
153 {
154         struct lnet_peer_ni *lpni;
155         struct lnet_net *net;
156         int cpt;
157
158         cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
159
160         LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
161         if (!lpni)
162                 return NULL;
163
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         INIT_LIST_HEAD(&lpni->lpni_rtr_pref_nids);
170         LNetInvalidateMDHandle(&lpni->lpni_recovery_ping_mdh);
171         kref_init(&lpni->lpni_kref);
172         lpni->lpni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
173
174         spin_lock_init(&lpni->lpni_lock);
175
176         if (lnet_peers_start_down())
177                 lpni->lpni_ns_status = LNET_NI_STATUS_DOWN;
178         else
179                 lpni->lpni_ns_status = LNET_NI_STATUS_UP;
180         lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
181         lpni->lpni_nid = nid;
182         lpni->lpni_cpt = cpt;
183         atomic_set(&lpni->lpni_healthv, LNET_MAX_HEALTH_VALUE);
184
185         net = lnet_get_net_locked(LNET_NIDNET(nid));
186         lpni->lpni_net = net;
187         if (net) {
188                 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
189                 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
190                 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
191                 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
192         } else {
193                 /*
194                  * This peer_ni is not on a local network, so we
195                  * cannot add the credits here. In case the net is
196                  * added later, add the peer_ni to the remote peer ni
197                  * list so it can be easily found and revisited.
198                  */
199                 /* FIXME: per-net implementation instead? */
200                 lnet_peer_ni_addref_locked(lpni);
201                 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
202                               &the_lnet.ln_remote_peer_ni_list);
203         }
204
205         CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
206
207         return lpni;
208 }
209
210 static struct lnet_peer_net *
211 lnet_peer_net_alloc(__u32 net_id)
212 {
213         struct lnet_peer_net *lpn;
214
215         LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
216         if (!lpn)
217                 return NULL;
218
219         INIT_LIST_HEAD(&lpn->lpn_peer_nets);
220         INIT_LIST_HEAD(&lpn->lpn_peer_nis);
221         lpn->lpn_net_id = net_id;
222         lpn->lpn_sel_priority = LNET_MAX_SELECTION_PRIORITY;
223
224         CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
225
226         return lpn;
227 }
228
229 void
230 lnet_destroy_peer_net_locked(struct lnet_peer_net *lpn)
231 {
232         struct lnet_peer *lp;
233
234         CDEBUG(D_NET, "%p net %s\n", lpn, libcfs_net2str(lpn->lpn_net_id));
235
236         LASSERT(atomic_read(&lpn->lpn_refcount) == 0);
237         LASSERT(list_empty(&lpn->lpn_peer_nis));
238         LASSERT(list_empty(&lpn->lpn_peer_nets));
239         lp = lpn->lpn_peer;
240         lpn->lpn_peer = NULL;
241         LIBCFS_FREE(lpn, sizeof(*lpn));
242
243         lnet_peer_decref_locked(lp);
244 }
245
246 static struct lnet_peer *
247 lnet_peer_alloc(lnet_nid_t nid)
248 {
249         struct lnet_peer *lp;
250
251         LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
252         if (!lp)
253                 return NULL;
254
255         INIT_LIST_HEAD(&lp->lp_rtrq);
256         INIT_LIST_HEAD(&lp->lp_routes);
257         INIT_LIST_HEAD(&lp->lp_peer_list);
258         INIT_LIST_HEAD(&lp->lp_peer_nets);
259         INIT_LIST_HEAD(&lp->lp_dc_list);
260         INIT_LIST_HEAD(&lp->lp_dc_pendq);
261         INIT_LIST_HEAD(&lp->lp_rtr_list);
262         init_waitqueue_head(&lp->lp_dc_waitq);
263         spin_lock_init(&lp->lp_lock);
264         lp->lp_primary_nid = nid;
265         lp->lp_disc_src_nid = LNET_NID_ANY;
266         lp->lp_disc_dst_nid = LNET_NID_ANY;
267         if (lnet_peers_start_down())
268                 lp->lp_alive = false;
269         else
270                 lp->lp_alive = true;
271
272         /*
273          * all peers created on a router should have health on
274          * if it's not already on.
275          */
276         if (the_lnet.ln_routing && !lnet_health_sensitivity)
277                 lp->lp_health_sensitivity = 1;
278
279         /*
280          * Turn off discovery for loopback peer. If you're creating a peer
281          * for the loopback interface then that was initiated when we
282          * attempted to send a message over the loopback. There is no need
283          * to ever use a different interface when sending messages to
284          * myself.
285          */
286         if (nid == LNET_NID_LO_0)
287                 lp->lp_state = LNET_PEER_NO_DISCOVERY;
288         lp->lp_cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
289
290         CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
291
292         return lp;
293 }
294
295 void
296 lnet_destroy_peer_locked(struct lnet_peer *lp)
297 {
298         CDEBUG(D_NET, "%p nid %s\n", lp, libcfs_nid2str(lp->lp_primary_nid));
299
300         LASSERT(atomic_read(&lp->lp_refcount) == 0);
301         LASSERT(lp->lp_rtr_refcount == 0);
302         LASSERT(list_empty(&lp->lp_peer_nets));
303         LASSERT(list_empty(&lp->lp_peer_list));
304         LASSERT(list_empty(&lp->lp_dc_list));
305
306         if (lp->lp_data)
307                 lnet_ping_buffer_decref(lp->lp_data);
308
309         /*
310          * if there are messages still on the pending queue, then make
311          * sure to queue them on the ln_msg_resend list so they can be
312          * resent at a later point if the discovery thread is still
313          * running.
314          * If the discovery thread has stopped, then the wakeup will be a
315          * no-op, and it is expected the lnet_shutdown_lndnets() will
316          * eventually be called, which will traverse this list and
317          * finalize the messages on the list.
318          * We can not resend them now because we're holding the cpt lock.
319          * Releasing the lock can cause an inconsistent state
320          */
321         spin_lock(&the_lnet.ln_msg_resend_lock);
322         spin_lock(&lp->lp_lock);
323         list_splice(&lp->lp_dc_pendq, &the_lnet.ln_msg_resend);
324         spin_unlock(&lp->lp_lock);
325         spin_unlock(&the_lnet.ln_msg_resend_lock);
326         wake_up(&the_lnet.ln_dc_waitq);
327
328         LIBCFS_FREE(lp, sizeof(*lp));
329 }
330
331 /*
332  * Detach a peer_ni from its peer_net. If this was the last peer_ni on
333  * that peer_net, detach the peer_net from the peer.
334  *
335  * Call with lnet_net_lock/EX held
336  */
337 static void
338 lnet_peer_detach_peer_ni_locked(struct lnet_peer_ni *lpni)
339 {
340         struct lnet_peer_table *ptable;
341         struct lnet_peer_net *lpn;
342         struct lnet_peer *lp;
343
344         /*
345          * Belts and suspenders: gracefully handle teardown of a
346          * partially connected peer_ni.
347          */
348         lpn = lpni->lpni_peer_net;
349
350         list_del_init(&lpni->lpni_peer_nis);
351         /*
352          * If there are no lpni's left, we detach lpn from
353          * lp_peer_nets, so it cannot be found anymore.
354          */
355         if (list_empty(&lpn->lpn_peer_nis))
356                 list_del_init(&lpn->lpn_peer_nets);
357
358         /* Update peer NID count. */
359         lp = lpn->lpn_peer;
360         lp->lp_nnis--;
361
362         /*
363          * If there are no more peer nets, make the peer unfindable
364          * via the peer_tables.
365          *
366          * Otherwise, if the peer is DISCOVERED, tell discovery to
367          * take another look at it. This is a no-op if discovery for
368          * this peer did the detaching.
369          */
370         if (list_empty(&lp->lp_peer_nets)) {
371                 list_del_init(&lp->lp_peer_list);
372                 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
373                 ptable->pt_peers--;
374         } else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING) {
375                 /* Discovery isn't running, nothing to do here. */
376         } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
377                 lnet_peer_queue_for_discovery(lp);
378                 wake_up(&the_lnet.ln_dc_waitq);
379         }
380         CDEBUG(D_NET, "peer %s NID %s\n",
381                 libcfs_nid2str(lp->lp_primary_nid),
382                 libcfs_nid2str(lpni->lpni_nid));
383 }
384
385 /* called with lnet_net_lock LNET_LOCK_EX held */
386 static int
387 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni, bool force)
388 {
389         struct lnet_peer_table *ptable = NULL;
390
391         /* don't remove a peer_ni if it's also a gateway */
392         if (lnet_isrouter(lpni) && !force) {
393                 CERROR("Peer NI %s is a gateway. Can not delete it\n",
394                        libcfs_nid2str(lpni->lpni_nid));
395                 return -EBUSY;
396         }
397
398         lnet_peer_remove_from_remote_list(lpni);
399
400         /* remove peer ni from the hash list. */
401         list_del_init(&lpni->lpni_hashlist);
402
403         /*
404          * indicate the peer is being deleted so the monitor thread can
405          * remove it from the recovery queue.
406          */
407         spin_lock(&lpni->lpni_lock);
408         lpni->lpni_state |= LNET_PEER_NI_DELETING;
409         spin_unlock(&lpni->lpni_lock);
410
411         /* decrement the ref count on the peer table */
412         ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
413
414         /*
415          * The peer_ni can no longer be found with a lookup. But there
416          * can be current users, so keep track of it on the zombie
417          * list until the reference count has gone to zero.
418          *
419          * The last reference may be lost in a place where the
420          * lnet_net_lock locks only a single cpt, and that cpt may not
421          * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
422          * has its own lock.
423          */
424         spin_lock(&ptable->pt_zombie_lock);
425         list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
426         ptable->pt_zombies++;
427         spin_unlock(&ptable->pt_zombie_lock);
428
429         /* no need to keep this peer_ni on the hierarchy anymore */
430         lnet_peer_detach_peer_ni_locked(lpni);
431
432         /* remove hashlist reference on peer_ni */
433         lnet_peer_ni_decref_locked(lpni);
434
435         return 0;
436 }
437
438 void lnet_peer_uninit(void)
439 {
440         struct lnet_peer_ni *lpni, *tmp;
441
442         lnet_net_lock(LNET_LOCK_EX);
443
444         /* remove all peer_nis from the remote peer and the hash list */
445         list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
446                                  lpni_on_remote_peer_ni_list)
447                 lnet_peer_ni_del_locked(lpni, false);
448
449         lnet_peer_tables_destroy();
450
451         lnet_net_unlock(LNET_LOCK_EX);
452 }
453
454 static int
455 lnet_peer_del_locked(struct lnet_peer *peer)
456 {
457         struct lnet_peer_ni *lpni = NULL, *lpni2;
458         int rc = 0, rc2 = 0;
459
460         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
461
462         spin_lock(&peer->lp_lock);
463         peer->lp_state |= LNET_PEER_MARK_DELETED;
464         spin_unlock(&peer->lp_lock);
465
466         lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
467         while (lpni != NULL) {
468                 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
469                 rc = lnet_peer_ni_del_locked(lpni, false);
470                 if (rc != 0)
471                         rc2 = rc;
472                 lpni = lpni2;
473         }
474
475         return rc2;
476 }
477
478 /*
479  * Discovering this peer is taking too long. Cancel any Ping or Push
480  * that discovery is waiting on by unlinking the relevant MDs. The
481  * lnet_discovery_event_handler() will proceed from here and complete
482  * the cleanup.
483  */
484 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
485 {
486         struct lnet_handle_md ping_mdh;
487         struct lnet_handle_md push_mdh;
488
489         LNetInvalidateMDHandle(&ping_mdh);
490         LNetInvalidateMDHandle(&push_mdh);
491
492         spin_lock(&lp->lp_lock);
493         if (lp->lp_state & LNET_PEER_PING_SENT) {
494                 ping_mdh = lp->lp_ping_mdh;
495                 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
496         }
497         if (lp->lp_state & LNET_PEER_PUSH_SENT) {
498                 push_mdh = lp->lp_push_mdh;
499                 LNetInvalidateMDHandle(&lp->lp_push_mdh);
500         }
501         spin_unlock(&lp->lp_lock);
502
503         if (!LNetMDHandleIsInvalid(ping_mdh))
504                 LNetMDUnlink(ping_mdh);
505         if (!LNetMDHandleIsInvalid(push_mdh))
506                 LNetMDUnlink(push_mdh);
507 }
508
509 static int
510 lnet_peer_del(struct lnet_peer *peer)
511 {
512         lnet_peer_cancel_discovery(peer);
513         lnet_net_lock(LNET_LOCK_EX);
514         lnet_peer_del_locked(peer);
515         lnet_net_unlock(LNET_LOCK_EX);
516
517         return 0;
518 }
519
520 /*
521  * Delete a NID from a peer. Call with ln_api_mutex held.
522  *
523  * Error codes:
524  *  -EPERM:  Non-DLC deletion from DLC-configured peer.
525  *  -ENOENT: No lnet_peer_ni corresponding to the nid.
526  *  -ECHILD: The lnet_peer_ni isn't connected to the peer.
527  *  -EBUSY:  The lnet_peer_ni is the primary, and not the only peer_ni.
528  */
529 static int
530 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
531 {
532         struct lnet_peer_ni *lpni;
533         lnet_nid_t primary_nid = lp->lp_primary_nid;
534         int rc = 0;
535         bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
536
537         if (!(flags & LNET_PEER_CONFIGURED)) {
538                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
539                         rc = -EPERM;
540                         goto out;
541                 }
542         }
543         lpni = lnet_find_peer_ni_locked(nid);
544         if (!lpni) {
545                 rc = -ENOENT;
546                 goto out;
547         }
548         lnet_peer_ni_decref_locked(lpni);
549         if (lp != lpni->lpni_peer_net->lpn_peer) {
550                 rc = -ECHILD;
551                 goto out;
552         }
553
554         /*
555          * This function only allows deletion of the primary NID if it
556          * is the only NID.
557          */
558         if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
559                 rc = -EBUSY;
560                 goto out;
561         }
562
563         lnet_net_lock(LNET_LOCK_EX);
564
565         if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
566                 struct lnet_peer_ni *lpni2;
567                 /* assign the next peer_ni to be the primary */
568                 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
569                 LASSERT(lpni2);
570                 lp->lp_primary_nid = lpni2->lpni_nid;
571         }
572         rc = lnet_peer_ni_del_locked(lpni, force);
573
574         lnet_net_unlock(LNET_LOCK_EX);
575
576 out:
577         CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
578                libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
579
580         return rc;
581 }
582
583 static void
584 lnet_peer_table_cleanup_locked(struct lnet_net *net,
585                                struct lnet_peer_table *ptable)
586 {
587         int                      i;
588         struct lnet_peer_ni     *next;
589         struct lnet_peer_ni     *lpni;
590         struct lnet_peer        *peer;
591
592         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
593                 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
594                                          lpni_hashlist) {
595                         if (net != NULL && net != lpni->lpni_net)
596                                 continue;
597
598                         peer = lpni->lpni_peer_net->lpn_peer;
599                         if (peer->lp_primary_nid != lpni->lpni_nid) {
600                                 lnet_peer_ni_del_locked(lpni, false);
601                                 continue;
602                         }
603                         /*
604                          * Removing the primary NID implies removing
605                          * the entire peer. Advance next beyond any
606                          * peer_ni that belongs to the same peer.
607                          */
608                         list_for_each_entry_from(next, &ptable->pt_hash[i],
609                                                  lpni_hashlist) {
610                                 if (next->lpni_peer_net->lpn_peer != peer)
611                                         break;
612                         }
613                         lnet_peer_del_locked(peer);
614                 }
615         }
616 }
617
618 static void
619 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
620 {
621         wait_var_event_warning(&ptable->pt_zombies,
622                                ptable->pt_zombies == 0,
623                                "Waiting for %d zombies on peer table\n",
624                                ptable->pt_zombies);
625 }
626
627 static void
628 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
629                                 struct lnet_peer_table *ptable)
630 {
631         struct lnet_peer_ni     *lp;
632         struct lnet_peer_ni     *tmp;
633         lnet_nid_t              gw_nid;
634         int                     i;
635
636         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
637                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
638                                          lpni_hashlist) {
639                         if (net != lp->lpni_net)
640                                 continue;
641
642                         if (!lnet_isrouter(lp))
643                                 continue;
644
645                         gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
646
647                         lnet_net_unlock(LNET_LOCK_EX);
648                         lnet_del_route(LNET_NET_ANY, gw_nid);
649                         lnet_net_lock(LNET_LOCK_EX);
650                 }
651         }
652 }
653
654 void
655 lnet_peer_tables_cleanup(struct lnet_net *net)
656 {
657         int i;
658         struct lnet_peer_table *ptable;
659
660         LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
661         /* If just deleting the peers for a NI, get rid of any routes these
662          * peers are gateways for. */
663         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
664                 lnet_net_lock(LNET_LOCK_EX);
665                 lnet_peer_table_del_rtrs_locked(net, ptable);
666                 lnet_net_unlock(LNET_LOCK_EX);
667         }
668
669         /* Start the cleanup process */
670         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
671                 lnet_net_lock(LNET_LOCK_EX);
672                 lnet_peer_table_cleanup_locked(net, ptable);
673                 lnet_net_unlock(LNET_LOCK_EX);
674         }
675
676         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
677                 lnet_peer_ni_finalize_wait(ptable);
678 }
679
680 static struct lnet_peer_ni *
681 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
682 {
683         struct list_head        *peers;
684         struct lnet_peer_ni     *lp;
685
686         if (the_lnet.ln_state != LNET_STATE_RUNNING)
687                 return NULL;
688
689         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
690         list_for_each_entry(lp, peers, lpni_hashlist) {
691                 if (lp->lpni_nid == nid) {
692                         lnet_peer_ni_addref_locked(lp);
693                         return lp;
694                 }
695         }
696
697         return NULL;
698 }
699
700 struct lnet_peer_ni *
701 lnet_find_peer_ni_locked(lnet_nid_t nid)
702 {
703         struct lnet_peer_ni *lpni;
704         struct lnet_peer_table *ptable;
705         int cpt;
706
707         cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
708
709         ptable = the_lnet.ln_peer_tables[cpt];
710         lpni = lnet_get_peer_ni_locked(ptable, nid);
711
712         return lpni;
713 }
714
715 struct lnet_peer_ni *
716 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
717 {
718         struct lnet_peer_net *lpn;
719         struct lnet_peer_ni *lpni;
720
721         lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
722         if (!lpn)
723                 return NULL;
724
725         list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
726                 if (lpni->lpni_nid == nid)
727                         return lpni;
728         }
729
730         return NULL;
731 }
732
733 struct lnet_peer *
734 lnet_find_peer(lnet_nid_t nid)
735 {
736         struct lnet_peer_ni *lpni;
737         struct lnet_peer *lp = NULL;
738         int cpt;
739
740         cpt = lnet_net_lock_current();
741         lpni = lnet_find_peer_ni_locked(nid);
742         if (lpni) {
743                 lp = lpni->lpni_peer_net->lpn_peer;
744                 lnet_peer_addref_locked(lp);
745                 lnet_peer_ni_decref_locked(lpni);
746         }
747         lnet_net_unlock(cpt);
748
749         return lp;
750 }
751
752 struct lnet_peer_net *
753 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
754 {
755         struct lnet_peer_net *net;
756
757         if (!prev_lpn_id) {
758                 /* no net id provided return the first net */
759                 net = list_first_entry_or_null(&lp->lp_peer_nets,
760                                                struct lnet_peer_net,
761                                                lpn_peer_nets);
762
763                 return net;
764         }
765
766         /* find the net after the one provided */
767         list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
768                 if (net->lpn_net_id == prev_lpn_id) {
769                         /*
770                          * if we reached the end of the list loop to the
771                          * beginning.
772                          */
773                         if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
774                                 return list_first_entry_or_null(&lp->lp_peer_nets,
775                                                                 struct lnet_peer_net,
776                                                                 lpn_peer_nets);
777                         else
778                                 return list_next_entry(net, lpn_peer_nets);
779                 }
780         }
781
782         return NULL;
783 }
784
785 struct lnet_peer_ni *
786 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
787                              struct lnet_peer_net *peer_net,
788                              struct lnet_peer_ni *prev)
789 {
790         struct lnet_peer_ni *lpni;
791         struct lnet_peer_net *net = peer_net;
792
793         if (!prev) {
794                 if (!net) {
795                         if (list_empty(&peer->lp_peer_nets))
796                                 return NULL;
797
798                         net = list_entry(peer->lp_peer_nets.next,
799                                          struct lnet_peer_net,
800                                          lpn_peer_nets);
801                 }
802                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
803                                   lpni_peer_nis);
804
805                 return lpni;
806         }
807
808         if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
809                 /*
810                  * if you reached the end of the peer ni list and the peer
811                  * net is specified then there are no more peer nis in that
812                  * net.
813                  */
814                 if (net)
815                         return NULL;
816
817                 /*
818                  * we reached the end of this net ni list. move to the
819                  * next net
820                  */
821                 if (prev->lpni_peer_net->lpn_peer_nets.next ==
822                     &peer->lp_peer_nets)
823                         /* no more nets and no more NIs. */
824                         return NULL;
825
826                 /* get the next net */
827                 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
828                                  struct lnet_peer_net,
829                                  lpn_peer_nets);
830                 /* get the ni on it */
831                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
832                                   lpni_peer_nis);
833
834                 return lpni;
835         }
836
837         /* there are more nis left */
838         lpni = list_entry(prev->lpni_peer_nis.next,
839                           struct lnet_peer_ni, lpni_peer_nis);
840
841         return lpni;
842 }
843
844 /* Call with the ln_api_mutex held */
845 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
846 {
847         struct lnet_process_id id;
848         struct lnet_peer_table *ptable;
849         struct lnet_peer *lp;
850         __u32 count = 0;
851         __u32 size = 0;
852         int lncpt;
853         int cpt;
854         __u32 i;
855         int rc;
856
857         rc = -ESHUTDOWN;
858         if (the_lnet.ln_state != LNET_STATE_RUNNING)
859                 goto done;
860
861         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
862
863         /*
864          * Count the number of peers, and return E2BIG if the buffer
865          * is too small. We'll also return the desired size.
866          */
867         rc = -E2BIG;
868         for (cpt = 0; cpt < lncpt; cpt++) {
869                 ptable = the_lnet.ln_peer_tables[cpt];
870                 count += ptable->pt_peers;
871         }
872         size = count * sizeof(*ids);
873         if (size > *sizep)
874                 goto done;
875
876         /*
877          * Walk the peer lists and copy out the primary nids.
878          * This is safe because the peer lists are only modified
879          * while the ln_api_mutex is held. So we don't need to
880          * hold the lnet_net_lock as well, and can therefore
881          * directly call copy_to_user().
882          */
883         rc = -EFAULT;
884         memset(&id, 0, sizeof(id));
885         id.pid = LNET_PID_LUSTRE;
886         i = 0;
887         for (cpt = 0; cpt < lncpt; cpt++) {
888                 ptable = the_lnet.ln_peer_tables[cpt];
889                 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
890                         if (i >= count)
891                                 goto done;
892                         id.nid = lp->lp_primary_nid;
893                         if (copy_to_user(&ids[i], &id, sizeof(id)))
894                                 goto done;
895                         i++;
896                 }
897         }
898         rc = 0;
899 done:
900         *countp = count;
901         *sizep = size;
902         return rc;
903 }
904
905 /*
906  * Start pushes to peers that need to be updated for a configuration
907  * change on this node.
908  */
909 void
910 lnet_push_update_to_peers(int force)
911 {
912         struct lnet_peer_table *ptable;
913         struct lnet_peer *lp;
914         int lncpt;
915         int cpt;
916
917         lnet_net_lock(LNET_LOCK_EX);
918         if (lnet_peer_discovery_disabled)
919                 force = 0;
920         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
921         for (cpt = 0; cpt < lncpt; cpt++) {
922                 ptable = the_lnet.ln_peer_tables[cpt];
923                 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
924                         if (force) {
925                                 spin_lock(&lp->lp_lock);
926                                 if (lp->lp_state & LNET_PEER_MULTI_RAIL)
927                                         lp->lp_state |= LNET_PEER_FORCE_PUSH;
928                                 spin_unlock(&lp->lp_lock);
929                         }
930                         if (lnet_peer_needs_push(lp))
931                                 lnet_peer_queue_for_discovery(lp);
932                 }
933         }
934         lnet_net_unlock(LNET_LOCK_EX);
935         wake_up(&the_lnet.ln_dc_waitq);
936 }
937
938 /* find the NID in the preferred gateways for the remote peer
939  * return:
940  *      false: list is not empty and NID is not preferred
941  *      false: list is empty
942  *      true: nid is found in the list
943  */
944 bool
945 lnet_peer_is_pref_rtr_locked(struct lnet_peer_ni *lpni,
946                              lnet_nid_t gw_nid)
947 {
948         struct lnet_nid_list *ne;
949
950         CDEBUG(D_NET, "%s: rtr pref emtpy: %d\n",
951                libcfs_nid2str(lpni->lpni_nid),
952                list_empty(&lpni->lpni_rtr_pref_nids));
953
954         if (list_empty(&lpni->lpni_rtr_pref_nids))
955                 return false;
956
957         /* iterate through all the preferred NIDs and see if any of them
958          * matches the provided gw_nid
959          */
960         list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
961                 CDEBUG(D_NET, "Comparing pref %s with gw %s\n",
962                        libcfs_nid2str(ne->nl_nid),
963                        libcfs_nid2str(gw_nid));
964                 if (ne->nl_nid == gw_nid)
965                         return true;
966         }
967
968         return false;
969 }
970
971 void
972 lnet_peer_clr_pref_rtrs(struct lnet_peer_ni *lpni)
973 {
974         struct list_head zombies;
975         struct lnet_nid_list *ne;
976         struct lnet_nid_list *tmp;
977         int cpt = lpni->lpni_cpt;
978
979         INIT_LIST_HEAD(&zombies);
980
981         lnet_net_lock(cpt);
982         list_splice_init(&lpni->lpni_rtr_pref_nids, &zombies);
983         lnet_net_unlock(cpt);
984
985         list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
986                 list_del(&ne->nl_list);
987                 LIBCFS_FREE(ne, sizeof(*ne));
988         }
989 }
990
991 int
992 lnet_peer_add_pref_rtr(struct lnet_peer_ni *lpni,
993                        lnet_nid_t gw_nid)
994 {
995         int cpt = lpni->lpni_cpt;
996         struct lnet_nid_list *ne = NULL;
997
998         /* This function is called with api_mutex held. When the api_mutex
999          * is held the list can not be modified, as it is only modified as
1000          * a result of applying a UDSP and that happens under api_mutex
1001          * lock.
1002          */
1003         __must_hold(&the_lnet.ln_api_mutex);
1004
1005         list_for_each_entry(ne, &lpni->lpni_rtr_pref_nids, nl_list) {
1006                 if (ne->nl_nid == gw_nid)
1007                         return -EEXIST;
1008         }
1009
1010         LIBCFS_CPT_ALLOC(ne, lnet_cpt_table(), cpt, sizeof(*ne));
1011         if (!ne)
1012                 return -ENOMEM;
1013
1014         ne->nl_nid = gw_nid;
1015
1016         /* Lock the cpt to protect against addition and checks in the
1017          * selection algorithm
1018          */
1019         lnet_net_lock(cpt);
1020         list_add(&ne->nl_list, &lpni->lpni_rtr_pref_nids);
1021         lnet_net_unlock(cpt);
1022
1023         return 0;
1024 }
1025
1026 /*
1027  * Test whether a ni is a preferred ni for this peer_ni, e.g, whether
1028  * this is a preferred point-to-point path. Call with lnet_net_lock in
1029  * shared mmode.
1030  */
1031 bool
1032 lnet_peer_is_pref_nid_locked(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1033 {
1034         struct lnet_nid_list *ne;
1035
1036         if (lpni->lpni_pref_nnids == 0)
1037                 return false;
1038         if (lpni->lpni_pref_nnids == 1)
1039                 return lpni->lpni_pref.nid == nid;
1040         list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1041                 if (ne->nl_nid == nid)
1042                         return true;
1043         }
1044         return false;
1045 }
1046
1047 /*
1048  * Set a single ni as preferred, provided no preferred ni is already
1049  * defined. Only to be used for non-multi-rail peer_ni.
1050  */
1051 int
1052 lnet_peer_ni_set_non_mr_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1053 {
1054         int rc = 0;
1055
1056         spin_lock(&lpni->lpni_lock);
1057         if (nid == LNET_NID_ANY) {
1058                 rc = -EINVAL;
1059         } else if (lpni->lpni_pref_nnids > 0) {
1060                 rc = -EPERM;
1061         } else if (lpni->lpni_pref_nnids == 0) {
1062                 lpni->lpni_pref.nid = nid;
1063                 lpni->lpni_pref_nnids = 1;
1064                 lpni->lpni_state |= LNET_PEER_NI_NON_MR_PREF;
1065         }
1066         spin_unlock(&lpni->lpni_lock);
1067
1068         CDEBUG(D_NET, "peer %s nid %s: %d\n",
1069                libcfs_nid2str(lpni->lpni_nid), libcfs_nid2str(nid), rc);
1070         return rc;
1071 }
1072
1073 /*
1074  * Clear the preferred NID from a non-multi-rail peer_ni, provided
1075  * this preference was set by lnet_peer_ni_set_non_mr_pref_nid().
1076  */
1077 int
1078 lnet_peer_ni_clr_non_mr_pref_nid(struct lnet_peer_ni *lpni)
1079 {
1080         int rc = 0;
1081
1082         spin_lock(&lpni->lpni_lock);
1083         if (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF) {
1084                 lpni->lpni_pref_nnids = 0;
1085                 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1086         } else if (lpni->lpni_pref_nnids == 0) {
1087                 rc = -ENOENT;
1088         } else {
1089                 rc = -EPERM;
1090         }
1091         spin_unlock(&lpni->lpni_lock);
1092
1093         CDEBUG(D_NET, "peer %s: %d\n",
1094                libcfs_nid2str(lpni->lpni_nid), rc);
1095         return rc;
1096 }
1097
1098 void
1099 lnet_peer_ni_set_selection_priority(struct lnet_peer_ni *lpni, __u32 priority)
1100 {
1101         lpni->lpni_sel_priority = priority;
1102 }
1103
1104 /*
1105  * Clear the preferred NIDs from a non-multi-rail peer.
1106  */
1107 void
1108 lnet_peer_clr_non_mr_pref_nids(struct lnet_peer *lp)
1109 {
1110         struct lnet_peer_ni *lpni = NULL;
1111
1112         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
1113                 lnet_peer_ni_clr_non_mr_pref_nid(lpni);
1114 }
1115
1116 int
1117 lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1118 {
1119         struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1120         struct lnet_nid_list *ne1 = NULL;
1121         struct lnet_nid_list *ne2 = NULL;
1122         lnet_nid_t tmp_nid = LNET_NID_ANY;
1123         int rc = 0;
1124
1125         if (nid == LNET_NID_ANY) {
1126                 rc = -EINVAL;
1127                 goto out;
1128         }
1129
1130         if (lpni->lpni_pref_nnids == 1 && lpni->lpni_pref.nid == nid) {
1131                 rc = -EEXIST;
1132                 goto out;
1133         }
1134
1135         /* A non-MR node may have only one preferred NI per peer_ni */
1136         if (lpni->lpni_pref_nnids > 0 &&
1137             !(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1138                 rc = -EPERM;
1139                 goto out;
1140         }
1141
1142         /* add the new preferred nid to the list of preferred nids */
1143         if (lpni->lpni_pref_nnids != 0) {
1144                 size_t alloc_size = sizeof(*ne1);
1145
1146                 if (lpni->lpni_pref_nnids == 1) {
1147                         tmp_nid = lpni->lpni_pref.nid;
1148                         INIT_LIST_HEAD(&lpni->lpni_pref.nids);
1149                 }
1150
1151                 list_for_each_entry(ne1, &lpni->lpni_pref.nids, nl_list) {
1152                         if (ne1->nl_nid == nid) {
1153                                 rc = -EEXIST;
1154                                 goto out;
1155                         }
1156                 }
1157
1158                 LIBCFS_CPT_ALLOC(ne1, lnet_cpt_table(), lpni->lpni_cpt,
1159                                  alloc_size);
1160                 if (!ne1) {
1161                         rc = -ENOMEM;
1162                         goto out;
1163                 }
1164
1165                 /* move the originally stored nid to the list */
1166                 if (lpni->lpni_pref_nnids == 1) {
1167                         LIBCFS_CPT_ALLOC(ne2, lnet_cpt_table(),
1168                                 lpni->lpni_cpt, alloc_size);
1169                         if (!ne2) {
1170                                 rc = -ENOMEM;
1171                                 goto out;
1172                         }
1173                         INIT_LIST_HEAD(&ne2->nl_list);
1174                         ne2->nl_nid = tmp_nid;
1175                 }
1176                 ne1->nl_nid = nid;
1177         }
1178
1179         lnet_net_lock(LNET_LOCK_EX);
1180         spin_lock(&lpni->lpni_lock);
1181         if (lpni->lpni_pref_nnids == 0) {
1182                 lpni->lpni_pref.nid = nid;
1183         } else {
1184                 if (ne2)
1185                         list_add_tail(&ne2->nl_list, &lpni->lpni_pref.nids);
1186                 list_add_tail(&ne1->nl_list, &lpni->lpni_pref.nids);
1187         }
1188         lpni->lpni_pref_nnids++;
1189         lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1190         spin_unlock(&lpni->lpni_lock);
1191         lnet_net_unlock(LNET_LOCK_EX);
1192
1193 out:
1194         if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
1195                 spin_lock(&lpni->lpni_lock);
1196                 lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1197                 spin_unlock(&lpni->lpni_lock);
1198         }
1199         CDEBUG(D_NET, "peer %s nid %s: %d\n",
1200                libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1201         return rc;
1202 }
1203
1204 int
1205 lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
1206 {
1207         struct lnet_peer *lp = lpni->lpni_peer_net->lpn_peer;
1208         struct lnet_nid_list *ne = NULL;
1209         int rc = 0;
1210
1211         if (lpni->lpni_pref_nnids == 0) {
1212                 rc = -ENOENT;
1213                 goto out;
1214         }
1215
1216         if (lpni->lpni_pref_nnids == 1) {
1217                 if (lpni->lpni_pref.nid != nid) {
1218                         rc = -ENOENT;
1219                         goto out;
1220                 }
1221         } else {
1222                 list_for_each_entry(ne, &lpni->lpni_pref.nids, nl_list) {
1223                         if (ne->nl_nid == nid)
1224                                 goto remove_nid_entry;
1225                 }
1226                 rc = -ENOENT;
1227                 ne = NULL;
1228                 goto out;
1229         }
1230
1231 remove_nid_entry:
1232         lnet_net_lock(LNET_LOCK_EX);
1233         spin_lock(&lpni->lpni_lock);
1234         if (lpni->lpni_pref_nnids == 1)
1235                 lpni->lpni_pref.nid = LNET_NID_ANY;
1236         else {
1237                 list_del_init(&ne->nl_list);
1238                 if (lpni->lpni_pref_nnids == 2) {
1239                         struct lnet_nid_list *ne, *tmp;
1240
1241                         list_for_each_entry_safe(ne, tmp,
1242                                                  &lpni->lpni_pref.nids,
1243                                                  nl_list) {
1244                                 lpni->lpni_pref.nid = ne->nl_nid;
1245                                 list_del_init(&ne->nl_list);
1246                                 LIBCFS_FREE(ne, sizeof(*ne));
1247                         }
1248                 }
1249         }
1250         lpni->lpni_pref_nnids--;
1251         lpni->lpni_state &= ~LNET_PEER_NI_NON_MR_PREF;
1252         spin_unlock(&lpni->lpni_lock);
1253         lnet_net_unlock(LNET_LOCK_EX);
1254
1255         if (ne)
1256                 LIBCFS_FREE(ne, sizeof(*ne));
1257 out:
1258         CDEBUG(D_NET, "peer %s nid %s: %d\n",
1259                libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid), rc);
1260         return rc;
1261 }
1262
1263 void
1264 lnet_peer_clr_pref_nids(struct lnet_peer_ni *lpni)
1265 {
1266         struct list_head zombies;
1267         struct lnet_nid_list *ne;
1268         struct lnet_nid_list *tmp;
1269
1270         INIT_LIST_HEAD(&zombies);
1271
1272         lnet_net_lock(LNET_LOCK_EX);
1273         if (lpni->lpni_pref_nnids == 1)
1274                 lpni->lpni_pref.nid = LNET_NID_ANY;
1275         else if (lpni->lpni_pref_nnids > 1)
1276                 list_splice_init(&lpni->lpni_pref.nids, &zombies);
1277         lpni->lpni_pref_nnids = 0;
1278         lnet_net_unlock(LNET_LOCK_EX);
1279
1280         list_for_each_entry_safe(ne, tmp, &zombies, nl_list) {
1281                 list_del_init(&ne->nl_list);
1282                 LIBCFS_FREE(ne, sizeof(*ne));
1283         }
1284 }
1285
1286 lnet_nid_t
1287 lnet_peer_primary_nid_locked(lnet_nid_t nid)
1288 {
1289         struct lnet_peer_ni *lpni;
1290         lnet_nid_t primary_nid = nid;
1291
1292         lpni = lnet_find_peer_ni_locked(nid);
1293         if (lpni) {
1294                 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
1295                 lnet_peer_ni_decref_locked(lpni);
1296         }
1297
1298         return primary_nid;
1299 }
1300
1301 bool
1302 lnet_is_discovery_disabled_locked(struct lnet_peer *lp)
1303 __must_hold(&lp->lp_lock)
1304 {
1305         if (lnet_peer_discovery_disabled)
1306                 return true;
1307
1308         if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1309             (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1310                 return true;
1311         }
1312
1313         return false;
1314 }
1315
1316 /*
1317  * Peer Discovery
1318  */
1319 bool
1320 lnet_is_discovery_disabled(struct lnet_peer *lp)
1321 {
1322         bool rc = false;
1323
1324         spin_lock(&lp->lp_lock);
1325         rc = lnet_is_discovery_disabled_locked(lp);
1326         spin_unlock(&lp->lp_lock);
1327
1328         return rc;
1329 }
1330
1331 int
1332 LNetAddPeer(lnet_nid_t *nids, __u32 num_nids)
1333 {
1334         lnet_nid_t pnid = 0;
1335         bool mr;
1336         int i, rc;
1337
1338         if (!nids || num_nids < 1)
1339                 return -EINVAL;
1340
1341         rc = LNetNIInit(LNET_PID_ANY);
1342         if (rc < 0)
1343                 return rc;
1344
1345         mutex_lock(&the_lnet.ln_api_mutex);
1346
1347         mr = lnet_peer_discovery_disabled == 0;
1348
1349         rc = 0;
1350         for (i = 0; i < num_nids; i++) {
1351                 if (nids[i] == LNET_NID_LO_0)
1352                         continue;
1353
1354                 if (!pnid) {
1355                         pnid = nids[i];
1356                         rc = lnet_add_peer_ni(pnid, LNET_NID_ANY, mr, true);
1357                 } else if (lnet_peer_discovery_disabled) {
1358                         rc = lnet_add_peer_ni(nids[i], LNET_NID_ANY, mr, true);
1359                 } else {
1360                         rc = lnet_add_peer_ni(pnid, nids[i], mr, true);
1361                 }
1362
1363                 if (rc && rc != -EEXIST)
1364                         goto unlock;
1365         }
1366
1367 unlock:
1368         mutex_unlock(&the_lnet.ln_api_mutex);
1369
1370         LNetNIFini();
1371
1372         return rc == -EEXIST ? 0 : rc;
1373 }
1374 EXPORT_SYMBOL(LNetAddPeer);
1375
1376 lnet_nid_t
1377 LNetPrimaryNID(lnet_nid_t nid)
1378 {
1379         struct lnet_peer *lp;
1380         struct lnet_peer_ni *lpni;
1381         lnet_nid_t primary_nid = nid;
1382         int rc = 0;
1383         int cpt;
1384
1385         if (nid == LNET_NID_LO_0)
1386                 return LNET_NID_LO_0;
1387
1388         cpt = lnet_net_lock_current();
1389         lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1390         if (IS_ERR(lpni)) {
1391                 rc = PTR_ERR(lpni);
1392                 goto out_unlock;
1393         }
1394         lp = lpni->lpni_peer_net->lpn_peer;
1395
1396         /* If discovery is disabled locally then we needn't bother running
1397          * discovery here because discovery will not modify whatever
1398          * primary NID is currently set for this peer. If the specified peer is
1399          * down then this discovery can introduce long delays into the mount
1400          * process, so skip it if it isn't necessary.
1401          */
1402         while (!lnet_peer_discovery_disabled && !lnet_peer_is_uptodate(lp)) {
1403                 spin_lock(&lp->lp_lock);
1404                 /* force a full discovery cycle */
1405                 lp->lp_state |= LNET_PEER_FORCE_PING | LNET_PEER_FORCE_PUSH;
1406                 spin_unlock(&lp->lp_lock);
1407
1408                 rc = lnet_discover_peer_locked(lpni, cpt, true);
1409                 if (rc)
1410                         goto out_decref;
1411                 /* The lpni (or lp) for this NID may have changed and our ref is
1412                  * the only thing keeping the old one around. Release the ref
1413                  * and lookup the lpni again
1414                  */
1415                 lnet_peer_ni_decref_locked(lpni);
1416                 lpni = lnet_find_peer_ni_locked(nid);
1417                 if (!lpni) {
1418                         rc = -ENOENT;
1419                         goto out_unlock;
1420                 }
1421                 lp = lpni->lpni_peer_net->lpn_peer;
1422
1423                 /* If we find that the peer has discovery disabled then we will
1424                  * not modify whatever primary NID is currently set for this
1425                  * peer. Thus, we can break out of this loop even if the peer
1426                  * is not fully up to date.
1427                  */
1428                 if (lnet_is_discovery_disabled(lp))
1429                         break;
1430         }
1431         primary_nid = lp->lp_primary_nid;
1432 out_decref:
1433         lnet_peer_ni_decref_locked(lpni);
1434 out_unlock:
1435         lnet_net_unlock(cpt);
1436
1437         CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1438                libcfs_nid2str(primary_nid), rc);
1439         return primary_nid;
1440 }
1441 EXPORT_SYMBOL(LNetPrimaryNID);
1442
1443 struct lnet_peer_net *
1444 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1445 {
1446         struct lnet_peer_net *peer_net;
1447         list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1448                 if (peer_net->lpn_net_id == net_id)
1449                         return peer_net;
1450         }
1451         return NULL;
1452 }
1453
1454 /*
1455  * Attach a peer_ni to a peer_net and peer. This function assumes
1456  * peer_ni is not already attached to the peer_net/peer. The peer_ni
1457  * may be attached to a different peer, in which case it will be
1458  * properly detached first. The whole operation is done atomically.
1459  *
1460  * This function consumes the reference on lpni and Always returns 0.
1461  * This is the last function called from functions that do return an
1462  * int, so returning 0 here allows the compiler to do a tail call.
1463  */
1464 static int
1465 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1466                                 struct lnet_peer_net *lpn,
1467                                 struct lnet_peer_ni *lpni,
1468                                 unsigned flags)
1469 {
1470         struct lnet_peer_table *ptable;
1471         bool new_lpn = false;
1472         int rc;
1473
1474         /* Install the new peer_ni */
1475         lnet_net_lock(LNET_LOCK_EX);
1476         /* Add peer_ni to global peer table hash, if necessary. */
1477         if (list_empty(&lpni->lpni_hashlist)) {
1478                 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1479
1480                 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1481                 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1482                 ptable->pt_version++;
1483                 lnet_peer_ni_addref_locked(lpni);
1484         }
1485
1486         /* Detach the peer_ni from an existing peer, if necessary. */
1487         if (lpni->lpni_peer_net) {
1488                 LASSERT(lpni->lpni_peer_net != lpn);
1489                 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1490                 lnet_peer_detach_peer_ni_locked(lpni);
1491                 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1492                 lpni->lpni_peer_net = NULL;
1493         }
1494
1495         /* Add peer_ni to peer_net */
1496         lpni->lpni_peer_net = lpn;
1497         if (lp->lp_primary_nid == lpni->lpni_nid)
1498                 list_add(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1499         else
1500                 list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1501         lnet_update_peer_net_healthv(lpni);
1502         lnet_peer_net_addref_locked(lpn);
1503
1504         /* Add peer_net to peer */
1505         if (!lpn->lpn_peer) {
1506                 new_lpn = true;
1507                 lpn->lpn_peer = lp;
1508                 if (lp->lp_primary_nid == lpni->lpni_nid)
1509                         list_add(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1510                 else
1511                         list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1512                 lnet_peer_addref_locked(lp);
1513         }
1514
1515         /* Add peer to global peer list, if necessary */
1516         ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1517         if (list_empty(&lp->lp_peer_list)) {
1518                 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1519                 ptable->pt_peers++;
1520         }
1521
1522
1523         /* Update peer state */
1524         spin_lock(&lp->lp_lock);
1525         if (flags & LNET_PEER_CONFIGURED) {
1526                 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1527                         lp->lp_state |= LNET_PEER_CONFIGURED;
1528         }
1529         if (flags & LNET_PEER_MULTI_RAIL) {
1530                 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1531                         lp->lp_state |= LNET_PEER_MULTI_RAIL;
1532                         lnet_peer_clr_non_mr_pref_nids(lp);
1533                 }
1534         }
1535         spin_unlock(&lp->lp_lock);
1536
1537         lp->lp_nnis++;
1538
1539         /* apply UDSPs */
1540         if (new_lpn) {
1541                 rc = lnet_udsp_apply_policies_on_lpn(lpn);
1542                 if (rc)
1543                         CERROR("Failed to apply UDSPs on lpn %s\n",
1544                                libcfs_net2str(lpn->lpn_net_id));
1545         }
1546         rc = lnet_udsp_apply_policies_on_lpni(lpni);
1547         if (rc)
1548                 CERROR("Failed to apply UDSPs on lpni %s\n",
1549                        libcfs_nid2str(lpni->lpni_nid));
1550
1551         CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1552                libcfs_nid2str(lp->lp_primary_nid),
1553                libcfs_nid2str(lpni->lpni_nid), flags);
1554         lnet_peer_ni_decref_locked(lpni);
1555         lnet_net_unlock(LNET_LOCK_EX);
1556
1557         return 0;
1558 }
1559
1560 /*
1561  * Create a new peer, with nid as its primary nid.
1562  *
1563  * Call with the lnet_api_mutex held.
1564  */
1565 static int
1566 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1567 {
1568         struct lnet_peer *lp;
1569         struct lnet_peer_net *lpn;
1570         struct lnet_peer_ni *lpni;
1571         int rc = 0;
1572
1573         LASSERT(nid != LNET_NID_ANY);
1574
1575         /*
1576          * No need for the lnet_net_lock here, because the
1577          * lnet_api_mutex is held.
1578          */
1579         lpni = lnet_find_peer_ni_locked(nid);
1580         if (lpni) {
1581                 /* A peer with this NID already exists. */
1582                 lp = lpni->lpni_peer_net->lpn_peer;
1583                 lnet_peer_ni_decref_locked(lpni);
1584                 /*
1585                  * This is an error if the peer was configured and the
1586                  * primary NID differs or an attempt is made to change
1587                  * the Multi-Rail flag. Otherwise the assumption is
1588                  * that an existing peer is being modified.
1589                  */
1590                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1591                         if (lp->lp_primary_nid != nid)
1592                                 rc = -EEXIST;
1593                         else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1594                                 rc = -EPERM;
1595                         goto out;
1596                 } else if (!(flags & LNET_PEER_CONFIGURED)) {
1597                         if (lp->lp_primary_nid == nid) {
1598                                 rc = -EEXIST;
1599                                 goto out;
1600                         }
1601                 }
1602                 /* Delete and recreate as a configured peer. */
1603                 lnet_peer_del(lp);
1604         }
1605
1606         /* Create peer, peer_net, and peer_ni. */
1607         rc = -ENOMEM;
1608         lp = lnet_peer_alloc(nid);
1609         if (!lp)
1610                 goto out;
1611         lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1612         if (!lpn)
1613                 goto out_free_lp;
1614         lpni = lnet_peer_ni_alloc(nid);
1615         if (!lpni)
1616                 goto out_free_lpn;
1617
1618         return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1619
1620 out_free_lpn:
1621         LIBCFS_FREE(lpn, sizeof(*lpn));
1622 out_free_lp:
1623         LIBCFS_FREE(lp, sizeof(*lp));
1624 out:
1625         CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1626                libcfs_nid2str(nid), flags, rc);
1627         return rc;
1628 }
1629
1630 /*
1631  * Add a NID to a peer. Call with ln_api_mutex held.
1632  *
1633  * Error codes:
1634  *  -EPERM:    Non-DLC addition to a DLC-configured peer.
1635  *  -EEXIST:   The NID was configured by DLC for a different peer.
1636  *  -ENOMEM:   Out of memory.
1637  *  -ENOTUNIQ: Adding a second peer NID on a single network on a
1638  *             non-multi-rail peer.
1639  */
1640 static int
1641 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1642 {
1643         struct lnet_peer_net *lpn;
1644         struct lnet_peer_ni *lpni;
1645         int rc = 0;
1646
1647         LASSERT(lp);
1648         LASSERT(nid != LNET_NID_ANY);
1649
1650         /* A configured peer can only be updated through configuration. */
1651         if (!(flags & LNET_PEER_CONFIGURED)) {
1652                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1653                         rc = -EPERM;
1654                         goto out;
1655                 }
1656         }
1657
1658         /*
1659          * The MULTI_RAIL flag can be set but not cleared, because
1660          * that would leave the peer struct in an invalid state.
1661          */
1662         if (flags & LNET_PEER_MULTI_RAIL) {
1663                 spin_lock(&lp->lp_lock);
1664                 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1665                         lp->lp_state |= LNET_PEER_MULTI_RAIL;
1666                         lnet_peer_clr_non_mr_pref_nids(lp);
1667                 }
1668                 spin_unlock(&lp->lp_lock);
1669         } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1670                 rc = -EPERM;
1671                 goto out;
1672         }
1673
1674         lpni = lnet_find_peer_ni_locked(nid);
1675         if (lpni) {
1676                 /*
1677                  * A peer_ni already exists. This is only a problem if
1678                  * it is not connected to this peer and was configured
1679                  * by DLC.
1680                  */
1681                 if (lpni->lpni_peer_net->lpn_peer == lp)
1682                         goto out_free_lpni;
1683                 if (lnet_peer_ni_is_configured(lpni)) {
1684                         rc = -EEXIST;
1685                         goto out_free_lpni;
1686                 }
1687                 /* If this is the primary NID, destroy the peer. */
1688                 if (lnet_peer_ni_is_primary(lpni)) {
1689                         struct lnet_peer *rtr_lp =
1690                                 lpni->lpni_peer_net->lpn_peer;
1691                         int rtr_refcount = rtr_lp->lp_rtr_refcount;
1692                         /*
1693                          * if we're trying to delete a router it means
1694                          * we're moving this peer NI to a new peer so must
1695                          * transfer router properties to the new peer
1696                          */
1697                         if (rtr_refcount > 0) {
1698                                 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1699                                 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1700                         }
1701                         lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1702                         lnet_peer_ni_decref_locked(lpni);
1703                         lpni = lnet_peer_ni_alloc(nid);
1704                         if (!lpni) {
1705                                 rc = -ENOMEM;
1706                                 goto out_free_lpni;
1707                         }
1708                 }
1709         } else {
1710                 lpni = lnet_peer_ni_alloc(nid);
1711                 if (!lpni) {
1712                         rc = -ENOMEM;
1713                         goto out_free_lpni;
1714                 }
1715         }
1716
1717         /*
1718          * Get the peer_net. Check that we're not adding a second
1719          * peer_ni on a peer_net of a non-multi-rail peer.
1720          */
1721         lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1722         if (!lpn) {
1723                 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1724                 if (!lpn) {
1725                         rc = -ENOMEM;
1726                         goto out_free_lpni;
1727                 }
1728         } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1729                 rc = -ENOTUNIQ;
1730                 goto out_free_lpni;
1731         }
1732
1733         return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1734
1735 out_free_lpni:
1736         lnet_peer_ni_decref_locked(lpni);
1737 out:
1738         CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1739                libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1740                flags, rc);
1741         return rc;
1742 }
1743
1744 /*
1745  * Update the primary NID of a peer, if possible.
1746  *
1747  * Call with the lnet_api_mutex held.
1748  */
1749 static int
1750 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1751 {
1752         lnet_nid_t old = lp->lp_primary_nid;
1753         int rc = 0;
1754
1755         if (lp->lp_primary_nid == nid)
1756                 goto out;
1757
1758         lp->lp_primary_nid = nid;
1759
1760         rc = lnet_peer_add_nid(lp, nid, flags);
1761         if (rc) {
1762                 lp->lp_primary_nid = old;
1763                 goto out;
1764         }
1765 out:
1766         CDEBUG(D_NET, "peer %s NID %s: %d\n",
1767                libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1768         return rc;
1769 }
1770
1771 /*
1772  * lpni creation initiated due to traffic either sending or receiving.
1773  */
1774 static int
1775 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1776 {
1777         struct lnet_peer *lp;
1778         struct lnet_peer_net *lpn;
1779         struct lnet_peer_ni *lpni;
1780         unsigned flags = 0;
1781         int rc = 0;
1782
1783         if (nid == LNET_NID_ANY) {
1784                 rc = -EINVAL;
1785                 goto out;
1786         }
1787
1788         /* lnet_net_lock is not needed here because ln_api_lock is held */
1789         lpni = lnet_find_peer_ni_locked(nid);
1790         if (lpni) {
1791                 /*
1792                  * We must have raced with another thread. Since we
1793                  * know next to nothing about a peer_ni created by
1794                  * traffic, we just assume everything is ok and
1795                  * return.
1796                  */
1797                 lnet_peer_ni_decref_locked(lpni);
1798                 goto out;
1799         }
1800
1801         /* Create peer, peer_net, and peer_ni. */
1802         rc = -ENOMEM;
1803         lp = lnet_peer_alloc(nid);
1804         if (!lp)
1805                 goto out;
1806         lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1807         if (!lpn)
1808                 goto out_free_lp;
1809         lpni = lnet_peer_ni_alloc(nid);
1810         if (!lpni)
1811                 goto out_free_lpn;
1812         if (pref != LNET_NID_ANY)
1813                 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1814
1815         return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1816
1817 out_free_lpn:
1818         LIBCFS_FREE(lpn, sizeof(*lpn));
1819 out_free_lp:
1820         LIBCFS_FREE(lp, sizeof(*lp));
1821 out:
1822         CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1823         return rc;
1824 }
1825
1826 /*
1827  * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1828  *
1829  * This API handles the following combinations:
1830  *   Create a peer with its primary NI if only the prim_nid is provided
1831  *   Add a NID to a peer identified by the prim_nid. The peer identified
1832  *   by the prim_nid must already exist.
1833  *   The peer being created may be non-MR.
1834  *
1835  * The caller must hold ln_api_mutex. This prevents the peer from
1836  * being created/modified/deleted by a different thread.
1837  */
1838 int
1839 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr, bool temp)
1840 {
1841         struct lnet_peer *lp = NULL;
1842         struct lnet_peer_ni *lpni;
1843         unsigned int flags = 0;
1844
1845         /* The prim_nid must always be specified */
1846         if (prim_nid == LNET_NID_ANY)
1847                 return -EINVAL;
1848
1849         if (!temp)
1850                 flags = LNET_PEER_CONFIGURED;
1851
1852         if (mr)
1853                 flags |= LNET_PEER_MULTI_RAIL;
1854
1855         /*
1856          * If nid isn't specified, we must create a new peer with
1857          * prim_nid as its primary nid.
1858          */
1859         if (nid == LNET_NID_ANY)
1860                 return lnet_peer_add(prim_nid, flags);
1861
1862         /* Look up the prim_nid, which must exist. */
1863         lpni = lnet_find_peer_ni_locked(prim_nid);
1864         if (!lpni)
1865                 return -ENOENT;
1866         lnet_peer_ni_decref_locked(lpni);
1867         lp = lpni->lpni_peer_net->lpn_peer;
1868
1869         /* Peer must have been configured. */
1870         if (!temp && !(lp->lp_state & LNET_PEER_CONFIGURED)) {
1871                 CDEBUG(D_NET, "peer %s was not configured\n",
1872                        libcfs_nid2str(prim_nid));
1873                 return -ENOENT;
1874         }
1875
1876         /* Primary NID must match */
1877         if (lp->lp_primary_nid != prim_nid) {
1878                 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1879                        libcfs_nid2str(prim_nid),
1880                        libcfs_nid2str(lp->lp_primary_nid));
1881                 return -ENODEV;
1882         }
1883
1884         /* Multi-Rail flag must match. */
1885         if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1886                 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1887                        libcfs_nid2str(prim_nid));
1888                 return -EPERM;
1889         }
1890
1891         return lnet_peer_add_nid(lp, nid, flags);
1892 }
1893
1894 /*
1895  * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1896  *
1897  * This API handles the following combinations:
1898  *   Delete a NI from a peer if both prim_nid and nid are provided.
1899  *   Delete a peer if only prim_nid is provided.
1900  *   Delete a peer if its primary nid is provided.
1901  *
1902  * The caller must hold ln_api_mutex. This prevents the peer from
1903  * being modified/deleted by a different thread.
1904  */
1905 int
1906 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1907 {
1908         struct lnet_peer *lp;
1909         struct lnet_peer_ni *lpni;
1910         unsigned flags;
1911
1912         if (prim_nid == LNET_NID_ANY)
1913                 return -EINVAL;
1914
1915         lpni = lnet_find_peer_ni_locked(prim_nid);
1916         if (!lpni)
1917                 return -ENOENT;
1918         lnet_peer_ni_decref_locked(lpni);
1919         lp = lpni->lpni_peer_net->lpn_peer;
1920
1921         if (prim_nid != lp->lp_primary_nid) {
1922                 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1923                        libcfs_nid2str(prim_nid),
1924                        libcfs_nid2str(lp->lp_primary_nid));
1925                 return -ENODEV;
1926         }
1927
1928         lnet_net_lock(LNET_LOCK_EX);
1929         if (lp->lp_rtr_refcount > 0) {
1930                 lnet_net_unlock(LNET_LOCK_EX);
1931                 CERROR("%s is a router. Can not be deleted\n",
1932                        libcfs_nid2str(prim_nid));
1933                 return -EBUSY;
1934         }
1935         lnet_net_unlock(LNET_LOCK_EX);
1936
1937         if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1938                 return lnet_peer_del(lp);
1939
1940         flags = LNET_PEER_CONFIGURED;
1941         if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1942                 flags |= LNET_PEER_MULTI_RAIL;
1943
1944         return lnet_peer_del_nid(lp, nid, flags);
1945 }
1946
1947 void
1948 lnet_destroy_peer_ni_locked(struct kref *ref)
1949 {
1950         struct lnet_peer_ni *lpni = container_of(ref, struct lnet_peer_ni,
1951                                                  lpni_kref);
1952         struct lnet_peer_table *ptable;
1953         struct lnet_peer_net *lpn;
1954
1955         CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1956
1957         LASSERT(kref_read(&lpni->lpni_kref) == 0);
1958         LASSERT(list_empty(&lpni->lpni_txq));
1959         LASSERT(lpni->lpni_txqnob == 0);
1960         LASSERT(list_empty(&lpni->lpni_peer_nis));
1961         LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1962
1963         lpn = lpni->lpni_peer_net;
1964         lpni->lpni_peer_net = NULL;
1965         lpni->lpni_net = NULL;
1966
1967         if (!list_empty(&lpni->lpni_hashlist)) {
1968                 /* remove the peer ni from the zombie list */
1969                 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1970                 spin_lock(&ptable->pt_zombie_lock);
1971                 list_del_init(&lpni->lpni_hashlist);
1972                 ptable->pt_zombies--;
1973                 spin_unlock(&ptable->pt_zombie_lock);
1974         }
1975
1976         if (lpni->lpni_pref_nnids > 1) {
1977                 struct lnet_nid_list *ne, *tmp;
1978
1979                 list_for_each_entry_safe(ne, tmp, &lpni->lpni_pref.nids,
1980                                          nl_list) {
1981                         list_del_init(&ne->nl_list);
1982                         LIBCFS_FREE(ne, sizeof(*ne));
1983                 }
1984         }
1985         LIBCFS_FREE(lpni, sizeof(*lpni));
1986
1987         if (lpn)
1988                 lnet_peer_net_decref_locked(lpn);
1989 }
1990
1991 struct lnet_peer_ni *
1992 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1993 {
1994         struct lnet_peer_ni *lpni = NULL;
1995         int rc;
1996
1997         if (the_lnet.ln_state != LNET_STATE_RUNNING)
1998                 return ERR_PTR(-ESHUTDOWN);
1999
2000         /*
2001          * find if a peer_ni already exists.
2002          * If so then just return that.
2003          */
2004         lpni = lnet_find_peer_ni_locked(nid);
2005         if (lpni)
2006                 return lpni;
2007
2008         lnet_net_unlock(cpt);
2009
2010         rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
2011         if (rc) {
2012                 lpni = ERR_PTR(rc);
2013                 goto out_net_relock;
2014         }
2015
2016         lpni = lnet_find_peer_ni_locked(nid);
2017         LASSERT(lpni);
2018
2019 out_net_relock:
2020         lnet_net_lock(cpt);
2021
2022         return lpni;
2023 }
2024
2025 /*
2026  * Get a peer_ni for the given nid, create it if necessary. Takes a
2027  * hold on the peer_ni.
2028  */
2029 struct lnet_peer_ni *
2030 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
2031 {
2032         struct lnet_peer_ni *lpni = NULL;
2033         int rc;
2034
2035         if (the_lnet.ln_state != LNET_STATE_RUNNING)
2036                 return ERR_PTR(-ESHUTDOWN);
2037
2038         /*
2039          * find if a peer_ni already exists.
2040          * If so then just return that.
2041          */
2042         lpni = lnet_find_peer_ni_locked(nid);
2043         if (lpni)
2044                 return lpni;
2045
2046         /*
2047          * Slow path:
2048          * use the lnet_api_mutex to serialize the creation of the peer_ni
2049          * and the creation/deletion of the local ni/net. When a local ni is
2050          * created, if there exists a set of peer_nis on that network,
2051          * they need to be traversed and updated. When a local NI is
2052          * deleted, which could result in a network being deleted, then
2053          * all peer nis on that network need to be removed as well.
2054          *
2055          * Creation through traffic should also be serialized with
2056          * creation through DLC.
2057          */
2058         lnet_net_unlock(cpt);
2059         mutex_lock(&the_lnet.ln_api_mutex);
2060         /*
2061          * Shutdown is only set under the ln_api_lock, so a single
2062          * check here is sufficent.
2063          */
2064         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2065                 lpni = ERR_PTR(-ESHUTDOWN);
2066                 goto out_mutex_unlock;
2067         }
2068
2069         rc = lnet_peer_ni_traffic_add(nid, pref);
2070         if (rc) {
2071                 lpni = ERR_PTR(rc);
2072                 goto out_mutex_unlock;
2073         }
2074
2075         lpni = lnet_find_peer_ni_locked(nid);
2076         LASSERT(lpni);
2077
2078 out_mutex_unlock:
2079         mutex_unlock(&the_lnet.ln_api_mutex);
2080         lnet_net_lock(cpt);
2081
2082         /* Lock has been dropped, check again for shutdown. */
2083         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2084                 if (!IS_ERR(lpni))
2085                         lnet_peer_ni_decref_locked(lpni);
2086                 lpni = ERR_PTR(-ESHUTDOWN);
2087         }
2088
2089         return lpni;
2090 }
2091
2092 bool
2093 lnet_peer_gw_discovery(struct lnet_peer *lp)
2094 {
2095         bool rc = false;
2096
2097         spin_lock(&lp->lp_lock);
2098         if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
2099                 rc = true;
2100         spin_unlock(&lp->lp_lock);
2101
2102         return rc;
2103 }
2104
2105 bool
2106 lnet_peer_is_uptodate(struct lnet_peer *lp)
2107 {
2108         bool rc;
2109
2110         spin_lock(&lp->lp_lock);
2111         rc = lnet_peer_is_uptodate_locked(lp);
2112         spin_unlock(&lp->lp_lock);
2113         return rc;
2114 }
2115
2116 /*
2117  * Is a peer uptodate from the point of view of discovery?
2118  *
2119  * If it is currently being processed, obviously not.
2120  * A forced Ping or Push is also handled by the discovery thread.
2121  *
2122  * Otherwise look at whether the peer needs rediscovering.
2123  */
2124 bool
2125 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
2126 __must_hold(&lp->lp_lock)
2127 {
2128         bool rc;
2129
2130         if (lp->lp_state & (LNET_PEER_DISCOVERING |
2131                             LNET_PEER_FORCE_PING |
2132                             LNET_PEER_FORCE_PUSH)) {
2133                 rc = false;
2134         } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
2135                 rc = false;
2136         } else if (lnet_peer_needs_push(lp)) {
2137                 rc = false;
2138         } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
2139                 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
2140                         rc = true;
2141                 else
2142                         rc = false;
2143         } else {
2144                 rc = false;
2145         }
2146
2147         return rc;
2148 }
2149
2150 /* Add the message to the peer's lp_dc_pendq and queue the peer for discovery */
2151 void
2152 lnet_peer_queue_message(struct lnet_peer *lp, struct lnet_msg *msg)
2153 {
2154         /* The discovery thread holds net_lock/EX and lp_lock when it splices
2155          * the lp_dc_pendq onto a local list for resending. Thus, we do the same
2156          * when adding to the list and queuing the peer to ensure that we do not
2157          * strand any messages on the lp_dc_pendq. This scheme ensures the
2158          * message will be resent even if the peer is already being discovered.
2159          * Therefore we needn't check the return value of
2160          * lnet_peer_queue_for_discovery(lp).
2161          */
2162         lnet_net_lock(LNET_LOCK_EX);
2163         spin_lock(&lp->lp_lock);
2164         list_add_tail(&msg->msg_list, &lp->lp_dc_pendq);
2165         spin_unlock(&lp->lp_lock);
2166         lnet_peer_queue_for_discovery(lp);
2167         lnet_net_unlock(LNET_LOCK_EX);
2168 }
2169
2170 /*
2171  * Queue a peer for the attention of the discovery thread.  Call with
2172  * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
2173  * -EALREADY if the peer was already queued.
2174  */
2175 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
2176 {
2177         int rc;
2178
2179         spin_lock(&lp->lp_lock);
2180         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2181                 lp->lp_state |= LNET_PEER_DISCOVERING;
2182         spin_unlock(&lp->lp_lock);
2183         if (list_empty(&lp->lp_dc_list)) {
2184                 lnet_peer_addref_locked(lp);
2185                 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2186                 wake_up(&the_lnet.ln_dc_waitq);
2187                 rc = 0;
2188         } else {
2189                 rc = -EALREADY;
2190         }
2191
2192         CDEBUG(D_NET, "Queue peer %s: %d\n",
2193                libcfs_nid2str(lp->lp_primary_nid), rc);
2194
2195         return rc;
2196 }
2197
2198 /*
2199  * Discovery of a peer is complete. Wake all waiters on the peer.
2200  * Call with lnet_net_lock/EX held.
2201  */
2202 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
2203 {
2204         struct lnet_msg *msg, *tmp;
2205         int rc = 0;
2206         LIST_HEAD(pending_msgs);
2207
2208         CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
2209                libcfs_nid2str(lp->lp_primary_nid));
2210
2211         list_del_init(&lp->lp_dc_list);
2212         spin_lock(&lp->lp_lock);
2213         list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
2214         spin_unlock(&lp->lp_lock);
2215         wake_up(&lp->lp_dc_waitq);
2216
2217         if (lp->lp_rtr_refcount > 0)
2218                 lnet_router_discovery_complete(lp);
2219
2220         lnet_net_unlock(LNET_LOCK_EX);
2221
2222         /* iterate through all pending messages and send them again */
2223         list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
2224                 list_del_init(&msg->msg_list);
2225                 if (lp->lp_dc_error) {
2226                         lnet_finalize(msg, lp->lp_dc_error);
2227                         continue;
2228                 }
2229
2230                 CDEBUG(D_NET, "sending pending message %s to target %s\n",
2231                        lnet_msgtyp2str(msg->msg_type),
2232                        libcfs_id2str(msg->msg_target));
2233                 rc = lnet_send(msg->msg_src_nid_param, msg,
2234                                msg->msg_rtr_nid_param);
2235                 if (rc < 0) {
2236                         CNETERR("Error sending %s to %s: %d\n",
2237                                lnet_msgtyp2str(msg->msg_type),
2238                                libcfs_id2str(msg->msg_target), rc);
2239                         lnet_finalize(msg, rc);
2240                 }
2241         }
2242         lnet_net_lock(LNET_LOCK_EX);
2243         lnet_peer_decref_locked(lp);
2244 }
2245
2246 /*
2247  * Handle inbound push.
2248  * Like any event handler, called with lnet_res_lock/CPT held.
2249  */
2250 void lnet_peer_push_event(struct lnet_event *ev)
2251 {
2252         struct lnet_ping_buffer *pbuf;
2253         struct lnet_peer *lp;
2254
2255         pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
2256
2257         /* lnet_find_peer() adds a refcount */
2258         lp = lnet_find_peer(ev->source.nid);
2259         if (!lp) {
2260                 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
2261                        libcfs_nid2str(ev->initiator.nid),
2262                        libcfs_nid2str(ev->source.nid));
2263                 pbuf->pb_needs_post = true;
2264                 return;
2265         }
2266
2267         /* Ensure peer state remains consistent while we modify it. */
2268         spin_lock(&lp->lp_lock);
2269
2270         /*
2271          * If some kind of error happened the contents of the message
2272          * cannot be used. Clear the NIDS_UPTODATE and set the
2273          * FORCE_PING flag to trigger a ping.
2274          */
2275         if (ev->status) {
2276                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2277                 lp->lp_state |= LNET_PEER_FORCE_PING;
2278                 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
2279                        ev->status,
2280                        libcfs_nid2str(lp->lp_primary_nid),
2281                        libcfs_nid2str(ev->source.nid));
2282                 goto out;
2283         }
2284
2285         /*
2286          * A push with invalid or corrupted info. Clear the UPTODATE
2287          * flag to trigger a ping.
2288          */
2289         if (lnet_ping_info_validate(&pbuf->pb_info)) {
2290                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2291                 lp->lp_state |= LNET_PEER_FORCE_PING;
2292                 CDEBUG(D_NET, "Corrupted Push from %s\n",
2293                        libcfs_nid2str(lp->lp_primary_nid));
2294                 goto out;
2295         }
2296
2297         /*
2298          * Make sure we'll allocate the correct size ping buffer when
2299          * pinging the peer.
2300          */
2301         if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2302                 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2303
2304         /*
2305          * A non-Multi-Rail peer is not supposed to be capable of
2306          * sending a push.
2307          */
2308         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2309                 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2310                        libcfs_nid2str(lp->lp_primary_nid));
2311                 goto out;
2312         }
2313
2314         /*
2315          * The peer may have discovery disabled at its end. Set
2316          * NO_DISCOVERY as appropriate.
2317          */
2318         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2319                 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2320                        libcfs_nid2str(lp->lp_primary_nid));
2321                 /*
2322                  * Mark the peer for deletion if we already know about it
2323                  * and it's going from discovery set to no discovery set
2324                  */
2325                 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2326                                       LNET_PEER_DISCOVERING)) &&
2327                      lp->lp_state & LNET_PEER_DISCOVERED) {
2328                         CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2329                                libcfs_nid2str(lp->lp_primary_nid),
2330                                lp->lp_state);
2331                         lp->lp_state |= LNET_PEER_MARK_DELETION;
2332                 }
2333                 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2334         } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2335                 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2336                        libcfs_nid2str(lp->lp_primary_nid));
2337                 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2338         }
2339
2340         /*
2341          * Update the MULTI_RAIL flag based on the push. If the peer
2342          * was configured with DLC then the setting should match what
2343          * DLC put in.
2344          * NB: We verified above that the MR feature bit is set in pi_features
2345          */
2346         if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2347                 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2348                        libcfs_nid2str(lp->lp_primary_nid), lp);
2349         } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2350                 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2351                       libcfs_nid2str(lp->lp_primary_nid));
2352         } else if (lnet_peer_discovery_disabled) {
2353                 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2354                        libcfs_nid2str(lp->lp_primary_nid), lp);
2355         } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2356                 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2357                        libcfs_nid2str(lp->lp_primary_nid), lp);
2358         } else {
2359                 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2360                        libcfs_nid2str(lp->lp_primary_nid), lp);
2361                 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2362                 lnet_peer_clr_non_mr_pref_nids(lp);
2363         }
2364
2365         /*
2366          * Check for truncation of the Put message. Clear the
2367          * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2368          * and tell discovery to allocate a bigger buffer.
2369          */
2370         if (ev->mlength < ev->rlength) {
2371                 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2372                         the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2373                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2374                 lp->lp_state |= LNET_PEER_FORCE_PING;
2375                 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2376                        libcfs_nid2str(lp->lp_primary_nid),
2377                        pbuf->pb_info.pi_nnis);
2378                 goto out;
2379         }
2380
2381         /* always assume new data */
2382         lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2383         lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2384
2385         /*
2386          * If there is data present that hasn't been processed yet,
2387          * we'll replace it if the Put contained newer data and it
2388          * fits. We're racing with a Ping or earlier Push in this
2389          * case.
2390          */
2391         if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2392                 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2393                         LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2394                     pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2395                         memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2396                                LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2397                         CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2398                               libcfs_nid2str(lp->lp_primary_nid),
2399                               LNET_PING_BUFFER_SEQNO(pbuf),
2400                               LNET_PING_BUFFER_SEQNO(lp->lp_data));
2401                 }
2402                 goto out;
2403         }
2404
2405         /*
2406          * Allocate a buffer to copy the data. On a failure we drop
2407          * the Push and set FORCE_PING to force the discovery
2408          * thread to fix the problem by pinging the peer.
2409          */
2410         lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2411         if (!lp->lp_data) {
2412                 lp->lp_state |= LNET_PEER_FORCE_PING;
2413                 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2414                        libcfs_nid2str(lp->lp_primary_nid),
2415                        LNET_PING_BUFFER_SEQNO(pbuf));
2416                 goto out;
2417         }
2418
2419         /* Success */
2420         memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2421                LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2422         lp->lp_state |= LNET_PEER_DATA_PRESENT;
2423         CDEBUG(D_NET, "Received Push %s %u\n",
2424                libcfs_nid2str(lp->lp_primary_nid),
2425                LNET_PING_BUFFER_SEQNO(pbuf));
2426
2427 out:
2428         /* We've processed this buffer. It can be reposted */
2429         pbuf->pb_needs_post = true;
2430
2431         /*
2432          * Queue the peer for discovery if not done, force it on the request
2433          * queue and wake the discovery thread if the peer was already queued,
2434          * because its status changed.
2435          */
2436         spin_unlock(&lp->lp_lock);
2437         lnet_net_lock(LNET_LOCK_EX);
2438         if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2439                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2440                 wake_up(&the_lnet.ln_dc_waitq);
2441         }
2442         /* Drop refcount from lookup */
2443         lnet_peer_decref_locked(lp);
2444         lnet_net_unlock(LNET_LOCK_EX);
2445 }
2446
2447 /*
2448  * Clear the discovery error state, unless we're already discovering
2449  * this peer, in which case the error is current.
2450  */
2451 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2452 {
2453         spin_lock(&lp->lp_lock);
2454         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2455                 lp->lp_dc_error = 0;
2456         spin_unlock(&lp->lp_lock);
2457 }
2458
2459 /*
2460  * Peer discovery slow path. The ln_api_mutex is held on entry, and
2461  * dropped/retaken within this function. An lnet_peer_ni is passed in
2462  * because discovery could tear down an lnet_peer.
2463  */
2464 int
2465 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2466 {
2467         DEFINE_WAIT(wait);
2468         struct lnet_peer *lp;
2469         int rc = 0;
2470         int count = 0;
2471
2472 again:
2473         lnet_net_unlock(cpt);
2474         lnet_net_lock(LNET_LOCK_EX);
2475         lp = lpni->lpni_peer_net->lpn_peer;
2476         lnet_peer_clear_discovery_error(lp);
2477
2478         /*
2479          * We're willing to be interrupted. The lpni can become a
2480          * zombie if we race with DLC, so we must check for that.
2481          */
2482         for (;;) {
2483                 /* Keep lp alive when the lnet_net_lock is unlocked */
2484                 lnet_peer_addref_locked(lp);
2485                 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2486                 if (signal_pending(current))
2487                         break;
2488                 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2489                         break;
2490                 /*
2491                  * Don't repeat discovery if discovery is disabled. This is
2492                  * done to ensure we can use discovery as a standard ping as
2493                  * well for backwards compatibility with routers which do not
2494                  * have discovery or have discovery disabled
2495                  */
2496                 if (lnet_is_discovery_disabled(lp) && count > 0)
2497                         break;
2498                 if (lp->lp_dc_error)
2499                         break;
2500                 if (lnet_peer_is_uptodate(lp))
2501                         break;
2502                 lnet_peer_queue_for_discovery(lp);
2503                 count++;
2504                 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2505
2506                 /*
2507                  * If caller requested a non-blocking operation then
2508                  * return immediately. Once discovery is complete any
2509                  * pending messages that were stopped due to discovery
2510                  * will be transmitted.
2511                  */
2512                 if (!block)
2513                         break;
2514
2515                 lnet_net_unlock(LNET_LOCK_EX);
2516                 schedule();
2517                 finish_wait(&lp->lp_dc_waitq, &wait);
2518                 lnet_net_lock(LNET_LOCK_EX);
2519                 lnet_peer_decref_locked(lp);
2520                 /* Peer may have changed */
2521                 lp = lpni->lpni_peer_net->lpn_peer;
2522         }
2523         finish_wait(&lp->lp_dc_waitq, &wait);
2524
2525         lnet_net_unlock(LNET_LOCK_EX);
2526         lnet_net_lock(cpt);
2527         lnet_peer_decref_locked(lp);
2528         /*
2529          * The peer may have changed, so re-check and rediscover if that turns
2530          * out to have been the case. The reference count on lp ensured that
2531          * even if it was unlinked from lpni the memory could not be recycled.
2532          * Thus the check below is sufficient to determine whether the peer
2533          * changed. If the peer changed, then lp must not be dereferenced.
2534          */
2535         if (lp != lpni->lpni_peer_net->lpn_peer)
2536                 goto again;
2537
2538         if (signal_pending(current))
2539                 rc = -EINTR;
2540         else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2541                 rc = -ESHUTDOWN;
2542         else if (lp->lp_dc_error)
2543                 rc = lp->lp_dc_error;
2544         else if (!block)
2545                 CDEBUG(D_NET, "non-blocking discovery\n");
2546         else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2547                 goto again;
2548
2549         CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2550                (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2551                libcfs_nid2str(lpni->lpni_nid), rc,
2552                (!block) ? "pending discovery" : "discovery complete");
2553
2554         return rc;
2555 }
2556
2557 /* Handle an incoming ack for a push. */
2558 static void
2559 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2560 {
2561         struct lnet_ping_buffer *pbuf;
2562
2563         pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2564         spin_lock(&lp->lp_lock);
2565         lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2566         lp->lp_push_error = ev->status;
2567         if (ev->status)
2568                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2569         else
2570                 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2571         spin_unlock(&lp->lp_lock);
2572
2573         CDEBUG(D_NET, "peer %s ev->status %d\n",
2574                libcfs_nid2str(lp->lp_primary_nid), ev->status);
2575 }
2576
2577 /* Handle a Reply message. This is the reply to a Ping message. */
2578 static void
2579 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2580 {
2581         struct lnet_ping_buffer *pbuf;
2582         int rc;
2583
2584         spin_lock(&lp->lp_lock);
2585
2586         lp->lp_disc_src_nid = ev->target.nid;
2587         lp->lp_disc_dst_nid = ev->source.nid;
2588
2589         /*
2590          * If some kind of error happened the contents of message
2591          * cannot be used. Set PING_FAILED to trigger a retry.
2592          */
2593         if (ev->status) {
2594                 lp->lp_state |= LNET_PEER_PING_FAILED;
2595                 lp->lp_ping_error = ev->status;
2596                 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2597                        ev->status,
2598                        libcfs_nid2str(lp->lp_primary_nid),
2599                        libcfs_nid2str(ev->source.nid));
2600                 goto out;
2601         }
2602
2603         pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2604         if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2605                 lnet_swap_pinginfo(pbuf);
2606
2607         /*
2608          * A reply with invalid or corrupted info. Set PING_FAILED to
2609          * trigger a retry.
2610          */
2611         rc = lnet_ping_info_validate(&pbuf->pb_info);
2612         if (rc) {
2613                 lp->lp_state |= LNET_PEER_PING_FAILED;
2614                 lp->lp_ping_error = 0;
2615                 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2616                        libcfs_nid2str(lp->lp_primary_nid), rc);
2617                 goto out;
2618         }
2619
2620         /*
2621          * The peer may have discovery disabled at its end. Set
2622          * NO_DISCOVERY as appropriate.
2623          */
2624         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) ||
2625             lnet_peer_discovery_disabled) {
2626                 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2627                        libcfs_nid2str(lp->lp_primary_nid));
2628
2629                 /* Detect whether this peer has toggled discovery from on to
2630                  * off and whether we can delete and re-create the peer. Peers
2631                  * that were manually configured cannot be deleted by discovery.
2632                  * We need to delete this peer and re-create it if the peer was
2633                  * not configured manually, is currently considered DD capable,
2634                  * and either:
2635                  * 1. We've already discovered the peer (the peer has toggled
2636                  *    the discovery feature from on to off), or
2637                  * 2. The peer is considered MR, but it was not user configured
2638                  *    (this was a "temporary" peer created via the kernel APIs
2639                  *     that we're discovering for the first time)
2640                  */
2641                 if (!(lp->lp_state & (LNET_PEER_CONFIGURED |
2642                                       LNET_PEER_NO_DISCOVERY)) &&
2643                     (lp->lp_state & (LNET_PEER_DISCOVERED |
2644                                      LNET_PEER_MULTI_RAIL))) {
2645                         CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2646                                libcfs_nid2str(lp->lp_primary_nid),
2647                                lp->lp_state);
2648                         lp->lp_state |= LNET_PEER_MARK_DELETION;
2649                 }
2650                 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2651         } else {
2652                 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2653                        libcfs_nid2str(lp->lp_primary_nid));
2654                 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2655         }
2656
2657         /*
2658          * Update the MULTI_RAIL flag based on the reply. If the peer
2659          * was configured with DLC then the setting should match what
2660          * DLC put in.
2661          */
2662         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2663                 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2664                         CDEBUG(D_NET, "peer %s(%p) is MR\n",
2665                                libcfs_nid2str(lp->lp_primary_nid), lp);
2666                 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2667                         CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2668                               libcfs_nid2str(lp->lp_primary_nid));
2669                 } else if (lnet_peer_discovery_disabled) {
2670                         CDEBUG(D_NET,
2671                                "peer %s(%p) not MR: DD disabled locally\n",
2672                                libcfs_nid2str(lp->lp_primary_nid), lp);
2673                 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2674                         CDEBUG(D_NET,
2675                                "peer %s(%p) not MR: DD disabled remotely\n",
2676                                libcfs_nid2str(lp->lp_primary_nid), lp);
2677                 } else {
2678                         CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2679                                libcfs_nid2str(lp->lp_primary_nid), lp);
2680                         lp->lp_state |= LNET_PEER_MULTI_RAIL;
2681                         lnet_peer_clr_non_mr_pref_nids(lp);
2682                 }
2683         } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2684                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2685                         CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2686                               libcfs_nid2str(lp->lp_primary_nid));
2687                 } else {
2688                         CERROR("Multi-Rail state vanished from %s\n",
2689                                libcfs_nid2str(lp->lp_primary_nid));
2690                         lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2691                 }
2692         }
2693
2694         /*
2695          * Make sure we'll allocate the correct size ping buffer when
2696          * pinging the peer.
2697          */
2698         if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2699                 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2700
2701         /*
2702          * Check for truncation of the Reply. Clear PING_SENT and set
2703          * PING_FAILED to trigger a retry.
2704          */
2705         if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2706                 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2707                         the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2708                 lp->lp_state |= LNET_PEER_PING_FAILED;
2709                 lp->lp_ping_error = 0;
2710                 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2711                        libcfs_nid2str(lp->lp_primary_nid),
2712                        pbuf->pb_info.pi_nnis);
2713                 goto out;
2714         }
2715
2716         /*
2717          * Check the sequence numbers in the reply. These are only
2718          * available if the reply came from a Multi-Rail peer.
2719          */
2720         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2721             pbuf->pb_info.pi_nnis > 1 &&
2722             lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2723                 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2724                         CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2725                                 libcfs_nid2str(lp->lp_primary_nid),
2726                                 LNET_PING_BUFFER_SEQNO(pbuf),
2727                                 lp->lp_peer_seqno);
2728
2729                 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2730         }
2731
2732         /* We're happy with the state of the data in the buffer. */
2733         CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2734                libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2735         if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2736                 lnet_ping_buffer_decref(lp->lp_data);
2737         else
2738                 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2739         lnet_ping_buffer_addref(pbuf);
2740         lp->lp_data = pbuf;
2741 out:
2742         lp->lp_state &= ~LNET_PEER_PING_SENT;
2743         spin_unlock(&lp->lp_lock);
2744
2745         lnet_net_lock(LNET_LOCK_EX);
2746         /*
2747          * If this peer is a gateway, call the routing callback to
2748          * handle the ping reply
2749          */
2750         if (lp->lp_rtr_refcount > 0)
2751                 lnet_router_discovery_ping_reply(lp);
2752         lnet_net_unlock(LNET_LOCK_EX);
2753 }
2754
2755 /*
2756  * Send event handling. Only matters for error cases, where we clean
2757  * up state on the peer and peer_ni that would otherwise be updated in
2758  * the REPLY event handler for a successful Ping, and the ACK event
2759  * handler for a successful Push.
2760  */
2761 static int
2762 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2763 {
2764         int rc = 0;
2765
2766         if (!ev->status)
2767                 goto out;
2768
2769         spin_lock(&lp->lp_lock);
2770         if (ev->msg_type == LNET_MSG_GET) {
2771                 lp->lp_state &= ~LNET_PEER_PING_SENT;
2772                 lp->lp_state |= LNET_PEER_PING_FAILED;
2773                 lp->lp_ping_error = ev->status;
2774         } else { /* ev->msg_type == LNET_MSG_PUT */
2775                 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2776                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2777                 lp->lp_push_error = ev->status;
2778         }
2779         spin_unlock(&lp->lp_lock);
2780         rc = LNET_REDISCOVER_PEER;
2781 out:
2782         CDEBUG(D_NET, "%s Send to %s: %d\n",
2783                 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2784                 libcfs_nid2str(ev->target.nid), rc);
2785         return rc;
2786 }
2787
2788 /*
2789  * Unlink event handling. This event is only seen if a call to
2790  * LNetMDUnlink() caused the event to be unlinked. If this call was
2791  * made after the event was set up in LNetGet() or LNetPut() then we
2792  * assume the Ping or Push timed out.
2793  */
2794 static void
2795 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2796 {
2797         spin_lock(&lp->lp_lock);
2798         /* We've passed through LNetGet() */
2799         if (lp->lp_state & LNET_PEER_PING_SENT) {
2800                 lp->lp_state &= ~LNET_PEER_PING_SENT;
2801                 lp->lp_state |= LNET_PEER_PING_FAILED;
2802                 lp->lp_ping_error = -ETIMEDOUT;
2803                 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2804                         libcfs_nid2str(lp->lp_primary_nid));
2805         }
2806         /* We've passed through LNetPut() */
2807         if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2808                 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2809                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2810                 lp->lp_push_error = -ETIMEDOUT;
2811                 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2812                         libcfs_nid2str(lp->lp_primary_nid));
2813         }
2814         spin_unlock(&lp->lp_lock);
2815 }
2816
2817 /*
2818  * Event handler for the discovery EQ.
2819  *
2820  * Called with lnet_res_lock(cpt) held. The cpt is the
2821  * lnet_cpt_of_cookie() of the md handle cookie.
2822  */
2823 static void lnet_discovery_event_handler(struct lnet_event *event)
2824 {
2825         struct lnet_peer *lp = event->md_user_ptr;
2826         struct lnet_ping_buffer *pbuf;
2827         int rc;
2828
2829         /* discovery needs to take another look */
2830         rc = LNET_REDISCOVER_PEER;
2831
2832         CDEBUG(D_NET, "Received event: %d\n", event->type);
2833
2834         switch (event->type) {
2835         case LNET_EVENT_ACK:
2836                 lnet_discovery_event_ack(lp, event);
2837                 break;
2838         case LNET_EVENT_REPLY:
2839                 lnet_discovery_event_reply(lp, event);
2840                 break;
2841         case LNET_EVENT_SEND:
2842                 /* Only send failure triggers a retry. */
2843                 rc = lnet_discovery_event_send(lp, event);
2844                 break;
2845         case LNET_EVENT_UNLINK:
2846                 /* LNetMDUnlink() was called */
2847                 lnet_discovery_event_unlink(lp, event);
2848                 break;
2849         default:
2850                 /* Invalid events. */
2851                 LBUG();
2852         }
2853         lnet_net_lock(LNET_LOCK_EX);
2854         if (event->unlinked) {
2855                 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2856                 lnet_ping_buffer_decref(pbuf);
2857                 lnet_peer_decref_locked(lp);
2858         }
2859
2860         /* put peer back at end of request queue, if discovery not already
2861          * done */
2862         if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp) &&
2863             lnet_peer_queue_for_discovery(lp)) {
2864                 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2865                 wake_up(&the_lnet.ln_dc_waitq);
2866         }
2867         lnet_net_unlock(LNET_LOCK_EX);
2868 }
2869
2870 /*
2871  * Build a peer from incoming data.
2872  *
2873  * The NIDs in the incoming data are supposed to be structured as follows:
2874  *  - loopback
2875  *  - primary NID
2876  *  - other NIDs in same net
2877  *  - NIDs in second net
2878  *  - NIDs in third net
2879  *  - ...
2880  * This due to the way the list of NIDs in the data is created.
2881  *
2882  * Note that this function will mark the peer uptodate unless an
2883  * ENOMEM is encontered. All other errors are due to a conflict
2884  * between the DLC configuration and what discovery sees. We treat DLC
2885  * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2886  * peer from becoming stuck in discovery.
2887  */
2888 static int lnet_peer_merge_data(struct lnet_peer *lp,
2889                                 struct lnet_ping_buffer *pbuf)
2890 {
2891         struct lnet_peer_net *lpn;
2892         struct lnet_peer_ni *lpni;
2893         lnet_nid_t *curnis = NULL;
2894         struct lnet_ni_status *addnis = NULL;
2895         lnet_nid_t *delnis = NULL;
2896         unsigned flags;
2897         int ncurnis;
2898         int naddnis;
2899         int ndelnis;
2900         int nnis = 0;
2901         int i;
2902         int j;
2903         int rc;
2904
2905         flags = LNET_PEER_DISCOVERED;
2906         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2907                 flags |= LNET_PEER_MULTI_RAIL;
2908
2909         /*
2910          * Cache the routing feature for the peer; whether it is enabled
2911          * for disabled as reported by the remote peer.
2912          */
2913         spin_lock(&lp->lp_lock);
2914         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2915                 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2916         else
2917                 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2918         spin_unlock(&lp->lp_lock);
2919
2920         nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2921         CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2922         CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2923         CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2924         if (!curnis || !addnis || !delnis) {
2925                 rc = -ENOMEM;
2926                 goto out;
2927         }
2928         ncurnis = 0;
2929         naddnis = 0;
2930         ndelnis = 0;
2931
2932         /* Construct the list of NIDs present in peer. */
2933         lpni = NULL;
2934         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2935                 curnis[ncurnis++] = lpni->lpni_nid;
2936
2937         /*
2938          * Check for NIDs in pbuf not present in curnis[].
2939          * The loop starts at 1 to skip the loopback NID.
2940          */
2941         for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2942                 for (j = 0; j < ncurnis; j++)
2943                         if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2944                                 break;
2945                 if (j == ncurnis)
2946                         addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2947         }
2948         /*
2949          * Check for NIDs in curnis[] not present in pbuf.
2950          * The nested loop starts at 1 to skip the loopback NID.
2951          *
2952          * But never add the loopback NID to delnis[]: if it is
2953          * present in curnis[] then this peer is for this node.
2954          */
2955         for (i = 0; i < ncurnis; i++) {
2956                 if (curnis[i] == LNET_NID_LO_0)
2957                         continue;
2958                 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2959                         if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2960                                 /*
2961                                  * update the information we cache for the
2962                                  * peer with the latest information we
2963                                  * received
2964                                  */
2965                                 lpni = lnet_find_peer_ni_locked(curnis[i]);
2966                                 if (lpni) {
2967                                         lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2968                                         lnet_peer_ni_decref_locked(lpni);
2969                                 }
2970                                 break;
2971                         }
2972                 }
2973                 if (j == pbuf->pb_info.pi_nnis)
2974                         delnis[ndelnis++] = curnis[i];
2975         }
2976
2977         /*
2978          * If we get here and the discovery is disabled then we don't want
2979          * to add or delete any NIs. We just updated the ones we have some
2980          * information on, and call it a day
2981          */
2982         rc = 0;
2983         if (lnet_is_discovery_disabled(lp))
2984                 goto out;
2985
2986         for (i = 0; i < naddnis; i++) {
2987                 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2988                 if (rc) {
2989                         CERROR("Error adding NID %s to peer %s: %d\n",
2990                                libcfs_nid2str(addnis[i].ns_nid),
2991                                libcfs_nid2str(lp->lp_primary_nid), rc);
2992                         if (rc == -ENOMEM)
2993                                 goto out;
2994                 }
2995                 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2996                 if (lpni) {
2997                         lpni->lpni_ns_status = addnis[i].ns_status;
2998                         lnet_peer_ni_decref_locked(lpni);
2999                 }
3000         }
3001
3002         for (i = 0; i < ndelnis; i++) {
3003                 /*
3004                  * for routers it's okay to delete the primary_nid because
3005                  * the upper layers don't really rely on it. So if we're
3006                  * being told that the router changed its primary_nid
3007                  * then it's okay to delete it.
3008                  */
3009                 if (lp->lp_rtr_refcount > 0)
3010                         flags |= LNET_PEER_RTR_NI_FORCE_DEL;
3011                 rc = lnet_peer_del_nid(lp, delnis[i], flags);
3012                 if (rc) {
3013                         CERROR("Error deleting NID %s from peer %s: %d\n",
3014                                libcfs_nid2str(delnis[i]),
3015                                libcfs_nid2str(lp->lp_primary_nid), rc);
3016                         if (rc == -ENOMEM)
3017                                 goto out;
3018                 }
3019         }
3020
3021         /* The peer net for the primary NID should be the first entry in the
3022          * peer's lp_peer_nets list, and the peer NI for the primary NID should
3023          * be the first entry in its peer net's lpn_peer_nis list.
3024          */
3025         lpni = lnet_find_peer_ni_locked(pbuf->pb_info.pi_ni[1].ns_nid);
3026         if (!lpni) {
3027                 CERROR("Internal error: Failed to lookup peer NI for primary NID: %s\n",
3028                        libcfs_nid2str(pbuf->pb_info.pi_ni[1].ns_nid));
3029                 goto out;
3030         }
3031
3032         lnet_peer_ni_decref_locked(lpni);
3033
3034         lpn = lpni->lpni_peer_net;
3035         if (lpn->lpn_peer_nets.prev != &lp->lp_peer_nets)
3036                 list_move(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
3037
3038         if (lpni->lpni_peer_nis.prev != &lpni->lpni_peer_net->lpn_peer_nis)
3039                 list_move(&lpni->lpni_peer_nis,
3040                           &lpni->lpni_peer_net->lpn_peer_nis);
3041
3042         /*
3043          * Errors other than -ENOMEM are due to peers having been
3044          * configured with DLC. Ignore these because DLC overrides
3045          * Discovery.
3046          */
3047         rc = 0;
3048 out:
3049         CFS_FREE_PTR_ARRAY(curnis, nnis);
3050         CFS_FREE_PTR_ARRAY(addnis, nnis);
3051         CFS_FREE_PTR_ARRAY(delnis, nnis);
3052         lnet_ping_buffer_decref(pbuf);
3053         CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3054
3055         if (rc) {
3056                 spin_lock(&lp->lp_lock);
3057                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
3058                 lp->lp_state |= LNET_PEER_FORCE_PING;
3059                 spin_unlock(&lp->lp_lock);
3060         }
3061         return rc;
3062 }
3063
3064 /*
3065  * The data in pbuf says lp is its primary peer, but the data was
3066  * received by a different peer. Try to update lp with the data.
3067  */
3068 static int
3069 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
3070 {
3071         struct lnet_handle_md mdh;
3072
3073         /* Queue lp for discovery, and force it on the request queue. */
3074         lnet_net_lock(LNET_LOCK_EX);
3075         if (lnet_peer_queue_for_discovery(lp))
3076                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
3077         lnet_net_unlock(LNET_LOCK_EX);
3078
3079         LNetInvalidateMDHandle(&mdh);
3080
3081         /*
3082          * Decide whether we can move the peer to the DATA_PRESENT state.
3083          *
3084          * We replace stale data for a multi-rail peer, repair PING_FAILED
3085          * status, and preempt FORCE_PING.
3086          *
3087          * If after that we have DATA_PRESENT, we merge it into this peer.
3088          */
3089         spin_lock(&lp->lp_lock);
3090         if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
3091                 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
3092                         lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
3093                 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3094                         lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3095                         lnet_ping_buffer_decref(pbuf);
3096                         pbuf = lp->lp_data;
3097                         lp->lp_data = NULL;
3098                 }
3099         }
3100         if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
3101                 lnet_ping_buffer_decref(lp->lp_data);
3102                 lp->lp_data = NULL;
3103                 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3104         }
3105         if (lp->lp_state & LNET_PEER_PING_FAILED) {
3106                 mdh = lp->lp_ping_mdh;
3107                 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3108                 lp->lp_state &= ~LNET_PEER_PING_FAILED;
3109                 lp->lp_ping_error = 0;
3110         }
3111         if (lp->lp_state & LNET_PEER_FORCE_PING)
3112                 lp->lp_state &= ~LNET_PEER_FORCE_PING;
3113         lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3114         spin_unlock(&lp->lp_lock);
3115
3116         if (!LNetMDHandleIsInvalid(mdh))
3117                 LNetMDUnlink(mdh);
3118
3119         if (pbuf)
3120                 return lnet_peer_merge_data(lp, pbuf);
3121
3122         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3123         return 0;
3124 }
3125
3126 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
3127 {
3128         int i;
3129
3130         for (i = 0; i < pinfo->pi_nnis; i++) {
3131                 if (pinfo->pi_ni[i].ns_nid == nid)
3132                         return true;
3133         }
3134
3135         return false;
3136 }
3137
3138 /* Delete a peer that has been marked for deletion. NB: when this peer was added
3139  * to the discovery queue a reference was taken that will prevent the peer from
3140  * actually being freed by this function. After this function exits the
3141  * discovery thread should call lnet_peer_discovery_complete() which will
3142  * drop that reference as well as wake any waiters that may also be holding a
3143  * ref on the peer
3144  */
3145 static int lnet_peer_deletion(struct lnet_peer *lp)
3146 __must_hold(&lp->lp_lock)
3147 {
3148         struct list_head rlist;
3149         struct lnet_route *route, *tmp;
3150         int sensitivity = lp->lp_health_sensitivity;
3151
3152         INIT_LIST_HEAD(&rlist);
3153
3154         lp->lp_state &= ~(LNET_PEER_DISCOVERING | LNET_PEER_FORCE_PING |
3155                           LNET_PEER_FORCE_PUSH);
3156         CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3157                libcfs_nid2str(lp->lp_primary_nid), lp, lp->lp_state);
3158
3159         /* no-op if lnet_peer_del() has already been called on this peer */
3160         if (lp->lp_state & LNET_PEER_MARK_DELETED)
3161                 return 0;
3162
3163         if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
3164                 return -ESHUTDOWN;
3165
3166         spin_unlock(&lp->lp_lock);
3167
3168         mutex_lock(&the_lnet.ln_api_mutex);
3169
3170         lnet_net_lock(LNET_LOCK_EX);
3171         /* remove the peer from the discovery work
3172          * queue if it's on there in preparation
3173          * of deleting it.
3174          */
3175         if (!list_empty(&lp->lp_dc_list))
3176                 list_del_init(&lp->lp_dc_list);
3177         list_for_each_entry_safe(route, tmp,
3178                                  &lp->lp_routes,
3179                                  lr_gwlist)
3180                 lnet_move_route(route, NULL, &rlist);
3181         lnet_net_unlock(LNET_LOCK_EX);
3182
3183         /* lnet_peer_del() deletes all the peer NIs owned by this peer */
3184         lnet_peer_del(lp);
3185
3186         list_for_each_entry_safe(route, tmp,
3187                                  &rlist, lr_list) {
3188                 /* re-add these routes */
3189                 lnet_add_route(route->lr_net,
3190                                route->lr_hops,
3191                                route->lr_nid,
3192                                route->lr_priority,
3193                                sensitivity);
3194                 LIBCFS_FREE(route, sizeof(*route));
3195         }
3196
3197         mutex_unlock(&the_lnet.ln_api_mutex);
3198
3199         spin_lock(&lp->lp_lock);
3200
3201         return 0;
3202 }
3203
3204 /*
3205  * Update a peer using the data received.
3206  */
3207 static int lnet_peer_data_present(struct lnet_peer *lp)
3208 __must_hold(&lp->lp_lock)
3209 {
3210         struct lnet_ping_buffer *pbuf;
3211         struct lnet_peer_ni *lpni;
3212         lnet_nid_t nid = LNET_NID_ANY;
3213         unsigned flags;
3214         int rc = 0;
3215
3216         pbuf = lp->lp_data;
3217         lp->lp_data = NULL;
3218         lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
3219         lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
3220         spin_unlock(&lp->lp_lock);
3221
3222         /*
3223          * Modifications of peer structures are done while holding the
3224          * ln_api_mutex. A global lock is required because we may be
3225          * modifying multiple peer structures, and a mutex greatly
3226          * simplifies memory management.
3227          *
3228          * The actual changes to the data structures must also protect
3229          * against concurrent lookups, for which the lnet_net_lock in
3230          * LNET_LOCK_EX mode is used.
3231          */
3232         mutex_lock(&the_lnet.ln_api_mutex);
3233         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
3234                 rc = -ESHUTDOWN;
3235                 goto out;
3236         }
3237
3238         /*
3239          * If this peer is not on the peer list then it is being torn
3240          * down, and our reference count may be all that is keeping it
3241          * alive. Don't do any work on it.
3242          */
3243         if (list_empty(&lp->lp_peer_list))
3244                 goto out;
3245
3246         flags = LNET_PEER_DISCOVERED;
3247         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
3248                 flags |= LNET_PEER_MULTI_RAIL;
3249
3250         /*
3251          * Check whether the primary NID in the message matches the
3252          * primary NID of the peer. If it does, update the peer, if
3253          * it it does not, check whether there is already a peer with
3254          * that primary NID. If no such peer exists, try to update
3255          * the primary NID of the current peer (allowed if it was
3256          * created due to message traffic) and complete the update.
3257          * If the peer did exist, hand off the data to it.
3258          *
3259          * The peer for the loopback interface is a special case: this
3260          * is the peer for the local node, and we want to set its
3261          * primary NID to the correct value here. Moreover, this peer
3262          * can show up with only the loopback NID in the ping buffer.
3263          */
3264         if (pbuf->pb_info.pi_nnis <= 1)
3265                 goto out;
3266         nid = pbuf->pb_info.pi_ni[1].ns_nid;
3267         if (lp->lp_primary_nid == LNET_NID_LO_0) {
3268                 rc = lnet_peer_set_primary_nid(lp, nid, flags);
3269                 if (!rc)
3270                         rc = lnet_peer_merge_data(lp, pbuf);
3271         /*
3272          * if the primary nid of the peer is present in the ping info returned
3273          * from the peer, but it's not the local primary peer we have
3274          * cached and discovery is disabled, then we don't want to update
3275          * our local peer info, by adding or removing NIDs, we just want
3276          * to update the status of the nids that we currently have
3277          * recorded in that peer.
3278          */
3279         } else if (lp->lp_primary_nid == nid ||
3280                    (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
3281                     lnet_is_discovery_disabled(lp))) {
3282                 rc = lnet_peer_merge_data(lp, pbuf);
3283         } else {
3284                 lpni = lnet_find_peer_ni_locked(nid);
3285                 if (!lpni || lp == lpni->lpni_peer_net->lpn_peer) {
3286                         rc = lnet_peer_set_primary_nid(lp, nid, flags);
3287                         if (rc) {
3288                                 CERROR("Primary NID error %s versus %s: %d\n",
3289                                        libcfs_nid2str(lp->lp_primary_nid),
3290                                        libcfs_nid2str(nid), rc);
3291                         } else {
3292                                 rc = lnet_peer_merge_data(lp, pbuf);
3293                         }
3294                         if (lpni)
3295                                 lnet_peer_ni_decref_locked(lpni);
3296                 } else {
3297                         struct lnet_peer *new_lp;
3298                         new_lp = lpni->lpni_peer_net->lpn_peer;
3299                         /*
3300                          * if lp has discovery/MR enabled that means new_lp
3301                          * should have discovery/MR enabled as well, since
3302                          * it's the same peer, which we're about to merge
3303                          */
3304                         spin_lock(&lp->lp_lock);
3305                         spin_lock(&new_lp->lp_lock);
3306                         if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
3307                                 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
3308                         if (lp->lp_state & LNET_PEER_MULTI_RAIL)
3309                                 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
3310                         /* If we're processing a ping reply then we may be
3311                          * about to send a push to the peer that we ping'd.
3312                          * Since the ping reply that we're processing was
3313                          * received by lp, we need to set the discovery source
3314                          * NID for new_lp to the NID stored in lp.
3315                          */
3316                         if (lp->lp_disc_src_nid != LNET_NID_ANY) {
3317                                 new_lp->lp_disc_src_nid = lp->lp_disc_src_nid;
3318                                 new_lp->lp_disc_dst_nid = lp->lp_disc_dst_nid;
3319                         }
3320                         spin_unlock(&new_lp->lp_lock);
3321                         spin_unlock(&lp->lp_lock);
3322
3323                         rc = lnet_peer_set_primary_data(new_lp, pbuf);
3324                         lnet_consolidate_routes_locked(lp, new_lp);
3325                         lnet_peer_ni_decref_locked(lpni);
3326                 }
3327         }
3328 out:
3329         CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
3330                lp->lp_state);
3331         mutex_unlock(&the_lnet.ln_api_mutex);
3332
3333         spin_lock(&lp->lp_lock);
3334         /* Tell discovery to re-check the peer immediately. */
3335         if (!rc)
3336                 rc = LNET_REDISCOVER_PEER;
3337         return rc;
3338 }
3339
3340 /*
3341  * A ping failed. Clear the PING_FAILED state and set the
3342  * FORCE_PING state, to ensure a retry even if discovery is
3343  * disabled. This avoids being left with incorrect state.
3344  */
3345 static int lnet_peer_ping_failed(struct lnet_peer *lp)
3346 __must_hold(&lp->lp_lock)
3347 {
3348         struct lnet_handle_md mdh;
3349         int rc;
3350
3351         mdh = lp->lp_ping_mdh;
3352         LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3353         lp->lp_state &= ~LNET_PEER_PING_FAILED;
3354         lp->lp_state |= LNET_PEER_FORCE_PING;
3355         rc = lp->lp_ping_error;
3356         lp->lp_ping_error = 0;
3357         spin_unlock(&lp->lp_lock);
3358
3359         if (!LNetMDHandleIsInvalid(mdh))
3360                 LNetMDUnlink(mdh);
3361
3362         CDEBUG(D_NET, "peer %s:%d\n",
3363                libcfs_nid2str(lp->lp_primary_nid), rc);
3364
3365         spin_lock(&lp->lp_lock);
3366         return rc ? rc : LNET_REDISCOVER_PEER;
3367 }
3368
3369 /* Active side of ping. */
3370 static int lnet_peer_send_ping(struct lnet_peer *lp)
3371 __must_hold(&lp->lp_lock)
3372 {
3373         int nnis;
3374         int rc;
3375         int cpt;
3376
3377         lp->lp_state |= LNET_PEER_PING_SENT;
3378         lp->lp_state &= ~LNET_PEER_FORCE_PING;
3379         spin_unlock(&lp->lp_lock);
3380
3381         cpt = lnet_net_lock_current();
3382         /* Refcount for MD. */
3383         lnet_peer_addref_locked(lp);
3384         lnet_net_unlock(cpt);
3385
3386         nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3387
3388         rc = lnet_send_ping(lp->lp_primary_nid, &lp->lp_ping_mdh, nnis, lp,
3389                             the_lnet.ln_dc_handler, false);
3390
3391         /*
3392          * if LNetMDBind in lnet_send_ping fails we need to decrement the
3393          * refcount on the peer, otherwise LNetMDUnlink will be called
3394          * which will eventually do that.
3395          */
3396         if (rc > 0) {
3397                 lnet_net_lock(cpt);
3398                 lnet_peer_decref_locked(lp);
3399                 lnet_net_unlock(cpt);
3400                 rc = -rc; /* change the rc to negative value */
3401                 goto fail_error;
3402         } else if (rc < 0) {
3403                 goto fail_error;
3404         }
3405
3406         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3407
3408         spin_lock(&lp->lp_lock);
3409         return 0;
3410
3411 fail_error:
3412         CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3413         /*
3414          * The errors that get us here are considered hard errors and
3415          * cause Discovery to terminate. So we clear PING_SENT, but do
3416          * not set either PING_FAILED or FORCE_PING. In fact we need
3417          * to clear PING_FAILED, because the unlink event handler will
3418          * have set it if we called LNetMDUnlink() above.
3419          */
3420         spin_lock(&lp->lp_lock);
3421         lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3422         return rc;
3423 }
3424
3425 /*
3426  * This function exists because you cannot call LNetMDUnlink() from an
3427  * event handler.
3428  */
3429 static int lnet_peer_push_failed(struct lnet_peer *lp)
3430 __must_hold(&lp->lp_lock)
3431 {
3432         struct lnet_handle_md mdh;
3433         int rc;
3434
3435         mdh = lp->lp_push_mdh;
3436         LNetInvalidateMDHandle(&lp->lp_push_mdh);
3437         lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3438         rc = lp->lp_push_error;
3439         lp->lp_push_error = 0;
3440         spin_unlock(&lp->lp_lock);
3441
3442         if (!LNetMDHandleIsInvalid(mdh))
3443                 LNetMDUnlink(mdh);
3444
3445         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3446         spin_lock(&lp->lp_lock);
3447         return rc ? rc : LNET_REDISCOVER_PEER;
3448 }
3449
3450 /*
3451  * Mark the peer as discovered.
3452  */
3453 static int lnet_peer_discovered(struct lnet_peer *lp)
3454 __must_hold(&lp->lp_lock)
3455 {
3456         lp->lp_state |= LNET_PEER_DISCOVERED;
3457         lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3458                           LNET_PEER_REDISCOVER);
3459
3460         lp->lp_dc_error = 0;
3461
3462         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3463
3464         return 0;
3465 }
3466
3467 /* Active side of push. */
3468 static int lnet_peer_send_push(struct lnet_peer *lp)
3469 __must_hold(&lp->lp_lock)
3470 {
3471         struct lnet_ping_buffer *pbuf;
3472         struct lnet_process_id id;
3473         struct lnet_md md;
3474         int cpt;
3475         int rc;
3476
3477         /* Don't push to a non-multi-rail peer. */
3478         if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3479                 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3480                 /* if peer's NIDs are uptodate then peer is discovered */
3481                 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE) {
3482                         rc = lnet_peer_discovered(lp);
3483                         return rc;
3484                 }
3485
3486                 return 0;
3487         }
3488
3489         lp->lp_state |= LNET_PEER_PUSH_SENT;
3490         lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3491         spin_unlock(&lp->lp_lock);
3492
3493         cpt = lnet_net_lock_current();
3494         pbuf = the_lnet.ln_ping_target;
3495         lnet_ping_buffer_addref(pbuf);
3496         lnet_net_unlock(cpt);
3497
3498         /* Push source MD */
3499         md.start     = &pbuf->pb_info;
3500         md.length    = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3501         md.threshold = 2; /* Put/Ack */
3502         md.max_size  = 0;
3503         md.options   = LNET_MD_TRACK_RESPONSE;
3504         md.handler   = the_lnet.ln_dc_handler;
3505         md.user_ptr  = lp;
3506
3507         rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3508         if (rc) {
3509                 lnet_ping_buffer_decref(pbuf);
3510                 CERROR("Can't bind push source MD: %d\n", rc);
3511                 goto fail_error;
3512         }
3513
3514         cpt = lnet_net_lock_current();
3515         /* Refcount for MD. */
3516         lnet_peer_addref_locked(lp);
3517         id.pid = LNET_PID_LUSTRE;
3518         if (lp->lp_disc_dst_nid != LNET_NID_ANY)
3519                 id.nid = lp->lp_disc_dst_nid;
3520         else
3521                 id.nid = lp->lp_primary_nid;
3522         lnet_net_unlock(cpt);
3523
3524         rc = LNetPut(lp->lp_disc_src_nid, lp->lp_push_mdh,
3525                      LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3526                      LNET_PROTO_PING_MATCHBITS, 0, 0);
3527
3528         /*
3529          * reset the discovery nid. There is no need to restrict sending
3530          * from that source, if we call lnet_push_update_to_peers(). It'll
3531          * get set to a specific NID, if we initiate discovery from the
3532          * scratch
3533          */
3534         lp->lp_disc_src_nid = LNET_NID_ANY;
3535         lp->lp_disc_dst_nid = LNET_NID_ANY;
3536
3537         if (rc)
3538                 goto fail_unlink;
3539
3540         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3541
3542         spin_lock(&lp->lp_lock);
3543         return 0;
3544
3545 fail_unlink:
3546         LNetMDUnlink(lp->lp_push_mdh);
3547         LNetInvalidateMDHandle(&lp->lp_push_mdh);
3548 fail_error:
3549         CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3550         /*
3551          * The errors that get us here are considered hard errors and
3552          * cause Discovery to terminate. So we clear PUSH_SENT, but do
3553          * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3554          * because the unlink event handler will have set it if we
3555          * called LNetMDUnlink() above.
3556          */
3557         spin_lock(&lp->lp_lock);
3558         lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3559         return rc;
3560 }
3561
3562 /*
3563  * An unrecoverable error was encountered during discovery.
3564  * Set error status in peer and abort discovery.
3565  */
3566 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3567 {
3568         CDEBUG(D_NET, "Discovery error %s: %d\n",
3569                libcfs_nid2str(lp->lp_primary_nid), error);
3570
3571         spin_lock(&lp->lp_lock);
3572         lp->lp_dc_error = error;
3573         lp->lp_state &= ~LNET_PEER_DISCOVERING;
3574         lp->lp_state |= LNET_PEER_REDISCOVER;
3575         spin_unlock(&lp->lp_lock);
3576 }
3577
3578 /*
3579  * Wait for work to be queued or some other change that must be
3580  * attended to. Returns non-zero if the discovery thread should shut
3581  * down.
3582  */
3583 static int lnet_peer_discovery_wait_for_work(void)
3584 {
3585         int cpt;
3586         int rc = 0;
3587
3588         DEFINE_WAIT(wait);
3589
3590         cpt = lnet_net_lock_current();
3591         for (;;) {
3592                 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3593                                 TASK_INTERRUPTIBLE);
3594                 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3595                         break;
3596                 if (lnet_push_target_resize_needed() ||
3597                     the_lnet.ln_push_target->pb_needs_post)
3598                         break;
3599                 if (!list_empty(&the_lnet.ln_dc_request))
3600                         break;
3601                 if (!list_empty(&the_lnet.ln_msg_resend))
3602                         break;
3603                 lnet_net_unlock(cpt);
3604
3605                 /*
3606                  * wakeup max every second to check if there are peers that
3607                  * have been stuck on the working queue for greater than
3608                  * the peer timeout.
3609                  */
3610                 schedule_timeout(cfs_time_seconds(1));
3611                 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3612                 cpt = lnet_net_lock_current();
3613         }
3614         finish_wait(&the_lnet.ln_dc_waitq, &wait);
3615
3616         if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3617                 rc = -ESHUTDOWN;
3618
3619         lnet_net_unlock(cpt);
3620
3621         CDEBUG(D_NET, "woken: %d\n", rc);
3622
3623         return rc;
3624 }
3625
3626 /*
3627  * Messages that were pending on a destroyed peer will be put on a global
3628  * resend list. The message resend list will be checked by
3629  * the discovery thread when it wakes up, and will resend messages. These
3630  * messages can still be sendable in the case the lpni which was the initial
3631  * cause of the message re-queue was transfered to another peer.
3632  *
3633  * It is possible that LNet could be shutdown while we're iterating
3634  * through the list. lnet_shudown_lndnets() will attempt to access the
3635  * resend list, but will have to wait until the spinlock is released, by
3636  * which time there shouldn't be any more messages on the resend list.
3637  * During shutdown lnet_send() will fail and lnet_finalize() will be called
3638  * for the messages so they can be released. The other case is that
3639  * lnet_shudown_lndnets() can finalize all the messages before this
3640  * function can visit the resend list, in which case this function will be
3641  * a no-op.
3642  */
3643 static void lnet_resend_msgs(void)
3644 {
3645         struct lnet_msg *msg, *tmp;
3646         LIST_HEAD(resend);
3647         int rc;
3648
3649         spin_lock(&the_lnet.ln_msg_resend_lock);
3650         list_splice(&the_lnet.ln_msg_resend, &resend);
3651         spin_unlock(&the_lnet.ln_msg_resend_lock);
3652
3653         list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3654                 list_del_init(&msg->msg_list);
3655                 rc = lnet_send(msg->msg_src_nid_param, msg,
3656                                msg->msg_rtr_nid_param);
3657                 if (rc < 0) {
3658                         CNETERR("Error sending %s to %s: %d\n",
3659                                lnet_msgtyp2str(msg->msg_type),
3660                                libcfs_id2str(msg->msg_target), rc);
3661                         lnet_finalize(msg, rc);
3662                 }
3663         }
3664 }
3665
3666 /* The discovery thread. */
3667 static int lnet_peer_discovery(void *arg)
3668 {
3669         struct lnet_peer *lp;
3670         int rc;
3671
3672         wait_for_completion(&the_lnet.ln_started);
3673
3674         CDEBUG(D_NET, "started\n");
3675
3676         for (;;) {
3677                 if (lnet_peer_discovery_wait_for_work())
3678                         break;
3679
3680                 if (lnet_push_target_resize_needed())
3681                         lnet_push_target_resize();
3682                 else if (the_lnet.ln_push_target->pb_needs_post)
3683                         lnet_push_target_post(the_lnet.ln_push_target,
3684                                               &the_lnet.ln_push_target_md);
3685
3686                 lnet_resend_msgs();
3687
3688                 lnet_net_lock(LNET_LOCK_EX);
3689                 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3690                         lnet_net_unlock(LNET_LOCK_EX);
3691                         break;
3692                 }
3693
3694                 /*
3695                  * Process all incoming discovery work requests.  When
3696                  * discovery must wait on a peer to change state, it
3697                  * is added to the tail of the ln_dc_working queue. A
3698                  * timestamp keeps track of when the peer was added,
3699                  * so we can time out discovery requests that take too
3700                  * long.
3701                  */
3702                 while (!list_empty(&the_lnet.ln_dc_request)) {
3703                         lp = list_first_entry(&the_lnet.ln_dc_request,
3704                                               struct lnet_peer, lp_dc_list);
3705                         list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3706                         /*
3707                          * set the time the peer was put on the dc_working
3708                          * queue. It shouldn't remain on the queue
3709                          * forever, in case the GET message (for ping)
3710                          * doesn't get a REPLY or the PUT message (for
3711                          * push) doesn't get an ACK.
3712                          */
3713                         lp->lp_last_queued = ktime_get_real_seconds();
3714                         lnet_net_unlock(LNET_LOCK_EX);
3715
3716                         if (lnet_push_target_resize_needed())
3717                                 lnet_push_target_resize();
3718                         else if (the_lnet.ln_push_target->pb_needs_post)
3719                                 lnet_push_target_post(the_lnet.ln_push_target,
3720                                                       &the_lnet.ln_push_target_md);
3721
3722                         /*
3723                          * Select an action depending on the state of
3724                          * the peer and whether discovery is disabled.
3725                          * The check whether discovery is disabled is
3726                          * done after the code that handles processing
3727                          * for arrived data, cleanup for failures, and
3728                          * forcing a Ping or Push.
3729                          */
3730                         spin_lock(&lp->lp_lock);
3731                         CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3732                                 libcfs_nid2str(lp->lp_primary_nid), lp,
3733                                 lp->lp_state);
3734                         if (lp->lp_state & (LNET_PEER_MARK_DELETION |
3735                                             LNET_PEER_MARK_DELETED))
3736                                 rc = lnet_peer_deletion(lp);
3737                         else if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3738                                 rc = lnet_peer_data_present(lp);
3739                         else if (lp->lp_state & LNET_PEER_PING_FAILED)
3740                                 rc = lnet_peer_ping_failed(lp);
3741                         else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3742                                 rc = lnet_peer_push_failed(lp);
3743                         else if (lp->lp_state & LNET_PEER_FORCE_PING)
3744                                 rc = lnet_peer_send_ping(lp);
3745                         else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3746                                 rc = lnet_peer_send_push(lp);
3747                         else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3748                                 rc = lnet_peer_send_ping(lp);
3749                         else if (lnet_peer_needs_push(lp))
3750                                 rc = lnet_peer_send_push(lp);
3751                         else
3752                                 rc = lnet_peer_discovered(lp);
3753                         CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3754                                 libcfs_nid2str(lp->lp_primary_nid), lp,
3755                                 lp->lp_state, rc);
3756                         spin_unlock(&lp->lp_lock);
3757
3758                         lnet_net_lock(LNET_LOCK_EX);
3759                         if (rc == LNET_REDISCOVER_PEER) {
3760                                 list_move(&lp->lp_dc_list,
3761                                           &the_lnet.ln_dc_request);
3762                         } else if (rc) {
3763                                 lnet_peer_discovery_error(lp, rc);
3764                         }
3765                         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3766                                 lnet_peer_discovery_complete(lp);
3767                         if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3768                                 break;
3769
3770                 }
3771
3772                 lnet_net_unlock(LNET_LOCK_EX);
3773         }
3774
3775         CDEBUG(D_NET, "stopping\n");
3776         /*
3777          * Clean up before telling lnet_peer_discovery_stop() that
3778          * we're done. Use wake_up() below to somewhat reduce the
3779          * size of the thundering herd if there are multiple threads
3780          * waiting on discovery of a single peer.
3781          */
3782
3783         /* Queue cleanup 1: stop all pending pings and pushes. */
3784         lnet_net_lock(LNET_LOCK_EX);
3785         while (!list_empty(&the_lnet.ln_dc_working)) {
3786                 lp = list_first_entry(&the_lnet.ln_dc_working,
3787                                       struct lnet_peer, lp_dc_list);
3788                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3789                 lnet_net_unlock(LNET_LOCK_EX);
3790                 lnet_peer_cancel_discovery(lp);
3791                 lnet_net_lock(LNET_LOCK_EX);
3792         }
3793         lnet_net_unlock(LNET_LOCK_EX);
3794
3795         /* Queue cleanup 2: wait for the expired queue to clear. */
3796         while (!list_empty(&the_lnet.ln_dc_expired))
3797                 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3798
3799         /* Queue cleanup 3: clear the request queue. */
3800         lnet_net_lock(LNET_LOCK_EX);
3801         while (!list_empty(&the_lnet.ln_dc_request)) {
3802                 lp = list_first_entry(&the_lnet.ln_dc_request,
3803                                       struct lnet_peer, lp_dc_list);
3804                 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3805                 lnet_peer_discovery_complete(lp);
3806         }
3807         lnet_net_unlock(LNET_LOCK_EX);
3808
3809         lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3810         the_lnet.ln_dc_handler = NULL;
3811
3812         the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3813         wake_up(&the_lnet.ln_dc_waitq);
3814
3815         CDEBUG(D_NET, "stopped\n");
3816
3817         return 0;
3818 }
3819
3820 /* ln_api_mutex is held on entry. */
3821 int lnet_peer_discovery_start(void)
3822 {
3823         struct task_struct *task;
3824         int rc = 0;
3825
3826         if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3827                 return -EALREADY;
3828
3829         the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3830         the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3831         task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3832         if (IS_ERR(task)) {
3833                 rc = PTR_ERR(task);
3834                 CERROR("Can't start peer discovery thread: %d\n", rc);
3835
3836                 the_lnet.ln_dc_handler = NULL;
3837
3838                 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3839         }
3840
3841         CDEBUG(D_NET, "discovery start: %d\n", rc);
3842
3843         return rc;
3844 }
3845
3846 /* ln_api_mutex is held on entry. */
3847 void lnet_peer_discovery_stop(void)
3848 {
3849         if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3850                 return;
3851
3852         LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3853         the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3854
3855         /* In the LNetNIInit() path we may be stopping discovery before it
3856          * entered its work loop
3857          */
3858         if (!completion_done(&the_lnet.ln_started))
3859                 complete(&the_lnet.ln_started);
3860         else
3861                 wake_up(&the_lnet.ln_dc_waitq);
3862
3863         wait_event(the_lnet.ln_dc_waitq,
3864                    the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3865
3866         LASSERT(list_empty(&the_lnet.ln_dc_request));
3867         LASSERT(list_empty(&the_lnet.ln_dc_working));
3868         LASSERT(list_empty(&the_lnet.ln_dc_expired));
3869
3870         CDEBUG(D_NET, "discovery stopped\n");
3871 }
3872
3873 /* Debugging */
3874
3875 void
3876 lnet_debug_peer(lnet_nid_t nid)
3877 {
3878         char                    *aliveness = "NA";
3879         struct lnet_peer_ni     *lp;
3880         int                     cpt;
3881
3882         cpt = lnet_cpt_of_nid(nid, NULL);
3883         lnet_net_lock(cpt);
3884
3885         lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3886         if (IS_ERR(lp)) {
3887                 lnet_net_unlock(cpt);
3888                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3889                 return;
3890         }
3891
3892         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3893                 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3894
3895         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3896                libcfs_nid2str(lp->lpni_nid), kref_read(&lp->lpni_kref),
3897                aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3898                lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3899                lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3900
3901         lnet_peer_ni_decref_locked(lp);
3902
3903         lnet_net_unlock(cpt);
3904 }
3905
3906 /* Gathering information for userspace. */
3907
3908 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3909                           char aliveness[LNET_MAX_STR_LEN],
3910                           __u32 *cpt_iter, __u32 *refcount,
3911                           __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3912                           __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3913                           __u32 *peer_tx_qnob)
3914 {
3915         struct lnet_peer_table          *peer_table;
3916         struct lnet_peer_ni             *lp;
3917         int                             j;
3918         int                             lncpt;
3919         bool                            found = false;
3920
3921         /* get the number of CPTs */
3922         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3923
3924         /* if the cpt number to be examined is >= the number of cpts in
3925          * the system then indicate that there are no more cpts to examin
3926          */
3927         if (*cpt_iter >= lncpt)
3928                 return -ENOENT;
3929
3930         /* get the current table */
3931         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3932         /* if the ptable is NULL then there are no more cpts to examine */
3933         if (peer_table == NULL)
3934                 return -ENOENT;
3935
3936         lnet_net_lock(*cpt_iter);
3937
3938         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3939                 struct list_head *peers = &peer_table->pt_hash[j];
3940
3941                 list_for_each_entry(lp, peers, lpni_hashlist) {
3942                         if (peer_index-- > 0)
3943                                 continue;
3944
3945                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3946                         if (lnet_isrouter(lp) ||
3947                                 lnet_peer_aliveness_enabled(lp))
3948                                 snprintf(aliveness, LNET_MAX_STR_LEN,
3949                                          lnet_is_peer_ni_alive(lp) ? "up" : "down");
3950
3951                         *nid = lp->lpni_nid;
3952                         *refcount = kref_read(&lp->lpni_kref);
3953                         *ni_peer_tx_credits =
3954                                 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3955                         *peer_tx_credits = lp->lpni_txcredits;
3956                         *peer_rtr_credits = lp->lpni_rtrcredits;
3957                         *peer_min_rtr_credits = lp->lpni_mintxcredits;
3958                         *peer_tx_qnob = lp->lpni_txqnob;
3959
3960                         found = true;
3961                 }
3962
3963         }
3964         lnet_net_unlock(*cpt_iter);
3965
3966         *cpt_iter = lncpt;
3967
3968         return found ? 0 : -ENOENT;
3969 }
3970
3971 /* ln_api_mutex is held, which keeps the peer list stable */
3972 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3973 {
3974         struct lnet_ioctl_element_stats *lpni_stats;
3975         struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3976         struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3977         struct lnet_peer_ni_credit_info *lpni_info;
3978         struct lnet_peer_ni *lpni;
3979         struct lnet_peer *lp;
3980         lnet_nid_t nid;
3981         __u32 size;
3982         int rc;
3983
3984         lp = lnet_find_peer(cfg->prcfg_prim_nid);
3985
3986         if (!lp) {
3987                 rc = -ENOENT;
3988                 goto out;
3989         }
3990
3991         size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3992                 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3993         size *= lp->lp_nnis;
3994         if (size > cfg->prcfg_size) {
3995                 cfg->prcfg_size = size;
3996                 rc = -E2BIG;
3997                 goto out_lp_decref;
3998         }
3999
4000         cfg->prcfg_prim_nid = lp->lp_primary_nid;
4001         cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
4002         cfg->prcfg_cfg_nid = lp->lp_primary_nid;
4003         cfg->prcfg_count = lp->lp_nnis;
4004         cfg->prcfg_size = size;
4005         cfg->prcfg_state = lp->lp_state;
4006
4007         /* Allocate helper buffers. */
4008         rc = -ENOMEM;
4009         LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
4010         if (!lpni_info)
4011                 goto out_lp_decref;
4012         LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
4013         if (!lpni_stats)
4014                 goto out_free_info;
4015         LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
4016         if (!lpni_msg_stats)
4017                 goto out_free_stats;
4018         LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
4019         if (!lpni_hstats)
4020                 goto out_free_msg_stats;
4021
4022
4023         lpni = NULL;
4024         rc = -EFAULT;
4025         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
4026                 nid = lpni->lpni_nid;
4027                 if (copy_to_user(bulk, &nid, sizeof(nid)))
4028                         goto out_free_hstats;
4029                 bulk += sizeof(nid);
4030
4031                 memset(lpni_info, 0, sizeof(*lpni_info));
4032                 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
4033                 if (lnet_isrouter(lpni) ||
4034                         lnet_peer_aliveness_enabled(lpni))
4035                         snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
4036                                 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
4037
4038                 lpni_info->cr_refcount = kref_read(&lpni->lpni_kref);
4039                 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
4040                         lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
4041                 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
4042                 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
4043                 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
4044                 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
4045                 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
4046                 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
4047                         goto out_free_hstats;
4048                 bulk += sizeof(*lpni_info);
4049
4050                 memset(lpni_stats, 0, sizeof(*lpni_stats));
4051                 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
4052                                                             LNET_STATS_TYPE_SEND);
4053                 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
4054                                                             LNET_STATS_TYPE_RECV);
4055                 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
4056                                                             LNET_STATS_TYPE_DROP);
4057                 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
4058                         goto out_free_hstats;
4059                 bulk += sizeof(*lpni_stats);
4060                 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
4061                 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
4062                         goto out_free_hstats;
4063                 bulk += sizeof(*lpni_msg_stats);
4064                 lpni_hstats->hlpni_network_timeout =
4065                   atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
4066                 lpni_hstats->hlpni_remote_dropped =
4067                   atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
4068                 lpni_hstats->hlpni_remote_timeout =
4069                   atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
4070                 lpni_hstats->hlpni_remote_error =
4071                   atomic_read(&lpni->lpni_hstats.hlt_remote_error);
4072                 lpni_hstats->hlpni_health_value =
4073                   atomic_read(&lpni->lpni_healthv);
4074                 lpni_hstats->hlpni_ping_count = lpni->lpni_ping_count;
4075                 lpni_hstats->hlpni_next_ping = lpni->lpni_next_ping;
4076                 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
4077                         goto out_free_hstats;
4078                 bulk += sizeof(*lpni_hstats);
4079         }
4080         rc = 0;
4081
4082 out_free_hstats:
4083         LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
4084 out_free_msg_stats:
4085         LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
4086 out_free_stats:
4087         LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
4088 out_free_info:
4089         LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
4090 out_lp_decref:
4091         lnet_peer_decref_locked(lp);
4092 out:
4093         return rc;
4094 }
4095
4096 /* must hold net_lock/0 */
4097 void
4098 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni,
4099                                      struct list_head *recovery_queue,
4100                                      time64_t now)
4101 {
4102         /* the mt could've shutdown and cleaned up the queues */
4103         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
4104                 return;
4105
4106         if (!list_empty(&lpni->lpni_recovery))
4107                 return;
4108
4109         if (atomic_read(&lpni->lpni_healthv) == LNET_MAX_HEALTH_VALUE)
4110                 return;
4111
4112         if (!lpni->lpni_last_alive) {
4113                 CDEBUG(D_NET,
4114                        "lpni %s(%p) not eligible for recovery last alive %lld\n",
4115                        libcfs_nid2str(lpni->lpni_nid), lpni,
4116                        lpni->lpni_last_alive);
4117                 return;
4118         }
4119
4120         if (lnet_recovery_limit &&
4121             now > lpni->lpni_last_alive + lnet_recovery_limit) {
4122                 CDEBUG(D_NET, "lpni %s aged out last alive %lld\n",
4123                        libcfs_nid2str(lpni->lpni_nid),
4124                        lpni->lpni_last_alive);
4125                 /* Reset the ping count so that if this peer NI is added back to
4126                  * the recovery queue we will send the first ping right away.
4127                  */
4128                 lpni->lpni_ping_count = 0;
4129                 return;
4130         }
4131
4132         /* This peer NI is going on the recovery queue, so take a ref on it */
4133         lnet_peer_ni_addref_locked(lpni);
4134
4135         lnet_peer_ni_set_next_ping(lpni, now);
4136
4137         CDEBUG(D_NET, "%s added to recovery queue. ping count: %u next ping: %lld last alive: %lld health: %d\n",
4138                libcfs_nid2str(lpni->lpni_nid),
4139                lpni->lpni_ping_count,
4140                lpni->lpni_next_ping,
4141                lpni->lpni_last_alive,
4142                atomic_read(&lpni->lpni_healthv));
4143
4144         list_add_tail(&lpni->lpni_recovery, recovery_queue);
4145 }
4146
4147 /* Call with the ln_api_mutex held */
4148 void
4149 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
4150 {
4151         struct lnet_peer_table *ptable;
4152         struct lnet_peer *lp;
4153         struct lnet_peer_net *lpn;
4154         struct lnet_peer_ni *lpni;
4155         int lncpt;
4156         int cpt;
4157         time64_t now;
4158
4159         if (the_lnet.ln_state != LNET_STATE_RUNNING)
4160                 return;
4161
4162         now = ktime_get_seconds();
4163
4164         if (!all) {
4165                 lnet_net_lock(LNET_LOCK_EX);
4166                 lpni = lnet_find_peer_ni_locked(nid);
4167                 if (!lpni) {
4168                         lnet_net_unlock(LNET_LOCK_EX);
4169                         return;
4170                 }
4171                 lnet_set_lpni_healthv_locked(lpni, value);
4172                 lnet_peer_ni_add_to_recoveryq_locked(lpni,
4173                                              &the_lnet.ln_mt_peerNIRecovq, now);
4174                 lnet_peer_ni_decref_locked(lpni);
4175                 lnet_net_unlock(LNET_LOCK_EX);
4176                 return;
4177         }
4178
4179         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
4180
4181         /*
4182          * Walk all the peers and reset the health value for each one to the
4183          * specified value.
4184          */
4185         lnet_net_lock(LNET_LOCK_EX);
4186         for (cpt = 0; cpt < lncpt; cpt++) {
4187                 ptable = the_lnet.ln_peer_tables[cpt];
4188                 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
4189                         list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
4190                                 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
4191                                                     lpni_peer_nis) {
4192                                         lnet_set_lpni_healthv_locked(lpni,
4193                                                                      value);
4194                                         lnet_peer_ni_add_to_recoveryq_locked(lpni,
4195                                              &the_lnet.ln_mt_peerNIRecovq, now);
4196                                 }
4197                         }
4198                 }
4199         }
4200         lnet_net_unlock(LNET_LOCK_EX);
4201 }
4202