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