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