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