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