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