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