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