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