Whamcloud - gitweb
f444911a0ef1f399711a9862b637b5b88f0532c3
[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                 rc = lnet_discover_peer_locked(lpni, cpt, true);
1197                 if (rc)
1198                         goto out_decref;
1199                 lp = lpni->lpni_peer_net->lpn_peer;
1200
1201                 /* Only try once if discovery is disabled */
1202                 if (lnet_is_discovery_disabled(lp))
1203                         break;
1204         }
1205         primary_nid = lp->lp_primary_nid;
1206 out_decref:
1207         lnet_peer_ni_decref_locked(lpni);
1208 out_unlock:
1209         lnet_net_unlock(cpt);
1210
1211         CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1212                libcfs_nid2str(primary_nid), rc);
1213         return primary_nid;
1214 }
1215 EXPORT_SYMBOL(LNetPrimaryNID);
1216
1217 struct lnet_peer_net *
1218 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1219 {
1220         struct lnet_peer_net *peer_net;
1221         list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1222                 if (peer_net->lpn_net_id == net_id)
1223                         return peer_net;
1224         }
1225         return NULL;
1226 }
1227
1228 /*
1229  * Attach a peer_ni to a peer_net and peer. This function assumes
1230  * peer_ni is not already attached to the peer_net/peer. The peer_ni
1231  * may be attached to a different peer, in which case it will be
1232  * properly detached first. The whole operation is done atomically.
1233  *
1234  * Always returns 0.  This is the last function called from functions
1235  * that do return an int, so returning 0 here allows the compiler to
1236  * do a tail call.
1237  */
1238 static int
1239 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1240                                 struct lnet_peer_net *lpn,
1241                                 struct lnet_peer_ni *lpni,
1242                                 unsigned flags)
1243 {
1244         struct lnet_peer_table *ptable;
1245
1246         /* Install the new peer_ni */
1247         lnet_net_lock(LNET_LOCK_EX);
1248         /* Add peer_ni to global peer table hash, if necessary. */
1249         if (list_empty(&lpni->lpni_hashlist)) {
1250                 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1251
1252                 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1253                 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1254                 ptable->pt_version++;
1255                 /* This is the 1st refcount on lpni. */
1256                 atomic_inc(&lpni->lpni_refcount);
1257         }
1258
1259         /* Detach the peer_ni from an existing peer, if necessary. */
1260         if (lpni->lpni_peer_net) {
1261                 LASSERT(lpni->lpni_peer_net != lpn);
1262                 LASSERT(lpni->lpni_peer_net->lpn_peer != lp);
1263                 lnet_peer_detach_peer_ni_locked(lpni);
1264                 lnet_peer_net_decref_locked(lpni->lpni_peer_net);
1265                 lpni->lpni_peer_net = NULL;
1266         }
1267
1268         /* Add peer_ni to peer_net */
1269         lpni->lpni_peer_net = lpn;
1270         list_add_tail(&lpni->lpni_peer_nis, &lpn->lpn_peer_nis);
1271         lnet_update_peer_net_healthv(lpni);
1272         lnet_peer_net_addref_locked(lpn);
1273
1274         /* Add peer_net to peer */
1275         if (!lpn->lpn_peer) {
1276                 lpn->lpn_peer = lp;
1277                 list_add_tail(&lpn->lpn_peer_nets, &lp->lp_peer_nets);
1278                 lnet_peer_addref_locked(lp);
1279         }
1280
1281         /* Add peer to global peer list, if necessary */
1282         ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
1283         if (list_empty(&lp->lp_peer_list)) {
1284                 list_add_tail(&lp->lp_peer_list, &ptable->pt_peer_list);
1285                 ptable->pt_peers++;
1286         }
1287
1288
1289         /* Update peer state */
1290         spin_lock(&lp->lp_lock);
1291         if (flags & LNET_PEER_CONFIGURED) {
1292                 if (!(lp->lp_state & LNET_PEER_CONFIGURED))
1293                         lp->lp_state |= LNET_PEER_CONFIGURED;
1294         }
1295         if (flags & LNET_PEER_MULTI_RAIL) {
1296                 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1297                         lp->lp_state |= LNET_PEER_MULTI_RAIL;
1298                         lnet_peer_clr_non_mr_pref_nids(lp);
1299                 }
1300         }
1301         spin_unlock(&lp->lp_lock);
1302
1303         lp->lp_nnis++;
1304         lnet_net_unlock(LNET_LOCK_EX);
1305
1306         CDEBUG(D_NET, "peer %s NID %s flags %#x\n",
1307                libcfs_nid2str(lp->lp_primary_nid),
1308                libcfs_nid2str(lpni->lpni_nid), flags);
1309
1310         return 0;
1311 }
1312
1313 /*
1314  * Create a new peer, with nid as its primary nid.
1315  *
1316  * Call with the lnet_api_mutex held.
1317  */
1318 static int
1319 lnet_peer_add(lnet_nid_t nid, unsigned flags)
1320 {
1321         struct lnet_peer *lp;
1322         struct lnet_peer_net *lpn;
1323         struct lnet_peer_ni *lpni;
1324         int rc = 0;
1325
1326         LASSERT(nid != LNET_NID_ANY);
1327
1328         /*
1329          * No need for the lnet_net_lock here, because the
1330          * lnet_api_mutex is held.
1331          */
1332         lpni = lnet_find_peer_ni_locked(nid);
1333         if (lpni) {
1334                 /* A peer with this NID already exists. */
1335                 lp = lpni->lpni_peer_net->lpn_peer;
1336                 lnet_peer_ni_decref_locked(lpni);
1337                 /*
1338                  * This is an error if the peer was configured and the
1339                  * primary NID differs or an attempt is made to change
1340                  * the Multi-Rail flag. Otherwise the assumption is
1341                  * that an existing peer is being modified.
1342                  */
1343                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1344                         if (lp->lp_primary_nid != nid)
1345                                 rc = -EEXIST;
1346                         else if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL)
1347                                 rc = -EPERM;
1348                         goto out;
1349                 }
1350                 /* Delete and recreate as a configured peer. */
1351                 lnet_peer_del(lp);
1352         }
1353
1354         /* Create peer, peer_net, and peer_ni. */
1355         rc = -ENOMEM;
1356         lp = lnet_peer_alloc(nid);
1357         if (!lp)
1358                 goto out;
1359         lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1360         if (!lpn)
1361                 goto out_free_lp;
1362         lpni = lnet_peer_ni_alloc(nid);
1363         if (!lpni)
1364                 goto out_free_lpn;
1365
1366         return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1367
1368 out_free_lpn:
1369         LIBCFS_FREE(lpn, sizeof(*lpn));
1370 out_free_lp:
1371         LIBCFS_FREE(lp, sizeof(*lp));
1372 out:
1373         CDEBUG(D_NET, "peer %s NID flags %#x: %d\n",
1374                libcfs_nid2str(nid), flags, rc);
1375         return rc;
1376 }
1377
1378 /*
1379  * Add a NID to a peer. Call with ln_api_mutex held.
1380  *
1381  * Error codes:
1382  *  -EPERM:    Non-DLC addition to a DLC-configured peer.
1383  *  -EEXIST:   The NID was configured by DLC for a different peer.
1384  *  -ENOMEM:   Out of memory.
1385  *  -ENOTUNIQ: Adding a second peer NID on a single network on a
1386  *             non-multi-rail peer.
1387  */
1388 static int
1389 lnet_peer_add_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1390 {
1391         struct lnet_peer_net *lpn;
1392         struct lnet_peer_ni *lpni;
1393         int rc = 0;
1394
1395         LASSERT(lp);
1396         LASSERT(nid != LNET_NID_ANY);
1397
1398         /* A configured peer can only be updated through configuration. */
1399         if (!(flags & LNET_PEER_CONFIGURED)) {
1400                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
1401                         rc = -EPERM;
1402                         goto out;
1403                 }
1404         }
1405
1406         /*
1407          * The MULTI_RAIL flag can be set but not cleared, because
1408          * that would leave the peer struct in an invalid state.
1409          */
1410         if (flags & LNET_PEER_MULTI_RAIL) {
1411                 spin_lock(&lp->lp_lock);
1412                 if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1413                         lp->lp_state |= LNET_PEER_MULTI_RAIL;
1414                         lnet_peer_clr_non_mr_pref_nids(lp);
1415                 }
1416                 spin_unlock(&lp->lp_lock);
1417         } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
1418                 rc = -EPERM;
1419                 goto out;
1420         }
1421
1422         lpni = lnet_find_peer_ni_locked(nid);
1423         if (lpni) {
1424                 /*
1425                  * A peer_ni already exists. This is only a problem if
1426                  * it is not connected to this peer and was configured
1427                  * by DLC.
1428                  */
1429                 lnet_peer_ni_decref_locked(lpni);
1430                 if (lpni->lpni_peer_net->lpn_peer == lp)
1431                         goto out;
1432                 if (lnet_peer_ni_is_configured(lpni)) {
1433                         rc = -EEXIST;
1434                         goto out;
1435                 }
1436                 /* If this is the primary NID, destroy the peer. */
1437                 if (lnet_peer_ni_is_primary(lpni)) {
1438                         struct lnet_peer *rtr_lp =
1439                           lpni->lpni_peer_net->lpn_peer;
1440                         int rtr_refcount = rtr_lp->lp_rtr_refcount;
1441                         /*
1442                          * if we're trying to delete a router it means
1443                          * we're moving this peer NI to a new peer so must
1444                          * transfer router properties to the new peer
1445                          */
1446                         if (rtr_refcount > 0) {
1447                                 flags |= LNET_PEER_RTR_NI_FORCE_DEL;
1448                                 lnet_rtr_transfer_to_peer(rtr_lp, lp);
1449                         }
1450                         lnet_peer_del(lpni->lpni_peer_net->lpn_peer);
1451                         lpni = lnet_peer_ni_alloc(nid);
1452                         if (!lpni) {
1453                                 rc = -ENOMEM;
1454                                 goto out;
1455                         }
1456                 }
1457         } else {
1458                 lpni = lnet_peer_ni_alloc(nid);
1459                 if (!lpni) {
1460                         rc = -ENOMEM;
1461                         goto out;
1462                 }
1463         }
1464
1465         /*
1466          * Get the peer_net. Check that we're not adding a second
1467          * peer_ni on a peer_net of a non-multi-rail peer.
1468          */
1469         lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
1470         if (!lpn) {
1471                 lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1472                 if (!lpn) {
1473                         rc = -ENOMEM;
1474                         goto out_free_lpni;
1475                 }
1476         } else if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
1477                 rc = -ENOTUNIQ;
1478                 goto out_free_lpni;
1479         }
1480
1481         return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1482
1483 out_free_lpni:
1484         /* If the peer_ni was allocated above its peer_net pointer is NULL */
1485         if (!lpni->lpni_peer_net)
1486                 LIBCFS_FREE(lpni, sizeof(*lpni));
1487 out:
1488         CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
1489                libcfs_nid2str(lp->lp_primary_nid), libcfs_nid2str(nid),
1490                flags, rc);
1491         return rc;
1492 }
1493
1494 /*
1495  * Update the primary NID of a peer, if possible.
1496  *
1497  * Call with the lnet_api_mutex held.
1498  */
1499 static int
1500 lnet_peer_set_primary_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
1501 {
1502         lnet_nid_t old = lp->lp_primary_nid;
1503         int rc = 0;
1504
1505         if (lp->lp_primary_nid == nid)
1506                 goto out;
1507         rc = lnet_peer_add_nid(lp, nid, flags);
1508         if (rc)
1509                 goto out;
1510         lp->lp_primary_nid = nid;
1511 out:
1512         CDEBUG(D_NET, "peer %s NID %s: %d\n",
1513                libcfs_nid2str(old), libcfs_nid2str(nid), rc);
1514         return rc;
1515 }
1516
1517 /*
1518  * lpni creation initiated due to traffic either sending or receiving.
1519  */
1520 static int
1521 lnet_peer_ni_traffic_add(lnet_nid_t nid, lnet_nid_t pref)
1522 {
1523         struct lnet_peer *lp;
1524         struct lnet_peer_net *lpn;
1525         struct lnet_peer_ni *lpni;
1526         unsigned flags = 0;
1527         int rc = 0;
1528
1529         if (nid == LNET_NID_ANY) {
1530                 rc = -EINVAL;
1531                 goto out;
1532         }
1533
1534         /* lnet_net_lock is not needed here because ln_api_lock is held */
1535         lpni = lnet_find_peer_ni_locked(nid);
1536         if (lpni) {
1537                 /*
1538                  * We must have raced with another thread. Since we
1539                  * know next to nothing about a peer_ni created by
1540                  * traffic, we just assume everything is ok and
1541                  * return.
1542                  */
1543                 lnet_peer_ni_decref_locked(lpni);
1544                 goto out;
1545         }
1546
1547         /* Create peer, peer_net, and peer_ni. */
1548         rc = -ENOMEM;
1549         lp = lnet_peer_alloc(nid);
1550         if (!lp)
1551                 goto out;
1552         lpn = lnet_peer_net_alloc(LNET_NIDNET(nid));
1553         if (!lpn)
1554                 goto out_free_lp;
1555         lpni = lnet_peer_ni_alloc(nid);
1556         if (!lpni)
1557                 goto out_free_lpn;
1558         if (pref != LNET_NID_ANY)
1559                 lnet_peer_ni_set_non_mr_pref_nid(lpni, pref);
1560
1561         return lnet_peer_attach_peer_ni(lp, lpn, lpni, flags);
1562
1563 out_free_lpn:
1564         LIBCFS_FREE(lpn, sizeof(*lpn));
1565 out_free_lp:
1566         LIBCFS_FREE(lp, sizeof(*lp));
1567 out:
1568         CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(nid), rc);
1569         return rc;
1570 }
1571
1572 /*
1573  * Implementation of IOC_LIBCFS_ADD_PEER_NI.
1574  *
1575  * This API handles the following combinations:
1576  *   Create a peer with its primary NI if only the prim_nid is provided
1577  *   Add a NID to a peer identified by the prim_nid. The peer identified
1578  *   by the prim_nid must already exist.
1579  *   The peer being created may be non-MR.
1580  *
1581  * The caller must hold ln_api_mutex. This prevents the peer from
1582  * being created/modified/deleted by a different thread.
1583  */
1584 int
1585 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
1586 {
1587         struct lnet_peer *lp = NULL;
1588         struct lnet_peer_ni *lpni;
1589         unsigned flags;
1590
1591         /* The prim_nid must always be specified */
1592         if (prim_nid == LNET_NID_ANY)
1593                 return -EINVAL;
1594
1595         flags = LNET_PEER_CONFIGURED;
1596         if (mr)
1597                 flags |= LNET_PEER_MULTI_RAIL;
1598
1599         /*
1600          * If nid isn't specified, we must create a new peer with
1601          * prim_nid as its primary nid.
1602          */
1603         if (nid == LNET_NID_ANY)
1604                 return lnet_peer_add(prim_nid, flags);
1605
1606         /* Look up the prim_nid, which must exist. */
1607         lpni = lnet_find_peer_ni_locked(prim_nid);
1608         if (!lpni)
1609                 return -ENOENT;
1610         lnet_peer_ni_decref_locked(lpni);
1611         lp = lpni->lpni_peer_net->lpn_peer;
1612
1613         /* Peer must have been configured. */
1614         if (!(lp->lp_state & LNET_PEER_CONFIGURED)) {
1615                 CDEBUG(D_NET, "peer %s was not configured\n",
1616                        libcfs_nid2str(prim_nid));
1617                 return -ENOENT;
1618         }
1619
1620         /* Primary NID must match */
1621         if (lp->lp_primary_nid != prim_nid) {
1622                 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1623                        libcfs_nid2str(prim_nid),
1624                        libcfs_nid2str(lp->lp_primary_nid));
1625                 return -ENODEV;
1626         }
1627
1628         /* Multi-Rail flag must match. */
1629         if ((lp->lp_state ^ flags) & LNET_PEER_MULTI_RAIL) {
1630                 CDEBUG(D_NET, "multi-rail state mismatch for peer %s\n",
1631                        libcfs_nid2str(prim_nid));
1632                 return -EPERM;
1633         }
1634
1635         return lnet_peer_add_nid(lp, nid, flags);
1636 }
1637
1638 /*
1639  * Implementation of IOC_LIBCFS_DEL_PEER_NI.
1640  *
1641  * This API handles the following combinations:
1642  *   Delete a NI from a peer if both prim_nid and nid are provided.
1643  *   Delete a peer if only prim_nid is provided.
1644  *   Delete a peer if its primary nid is provided.
1645  *
1646  * The caller must hold ln_api_mutex. This prevents the peer from
1647  * being modified/deleted by a different thread.
1648  */
1649 int
1650 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
1651 {
1652         struct lnet_peer *lp;
1653         struct lnet_peer_ni *lpni;
1654         unsigned flags;
1655
1656         if (prim_nid == LNET_NID_ANY)
1657                 return -EINVAL;
1658
1659         lpni = lnet_find_peer_ni_locked(prim_nid);
1660         if (!lpni)
1661                 return -ENOENT;
1662         lnet_peer_ni_decref_locked(lpni);
1663         lp = lpni->lpni_peer_net->lpn_peer;
1664
1665         if (prim_nid != lp->lp_primary_nid) {
1666                 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1667                        libcfs_nid2str(prim_nid),
1668                        libcfs_nid2str(lp->lp_primary_nid));
1669                 return -ENODEV;
1670         }
1671
1672         lnet_net_lock(LNET_LOCK_EX);
1673         if (lp->lp_rtr_refcount > 0) {
1674                 lnet_net_unlock(LNET_LOCK_EX);
1675                 CERROR("%s is a router. Can not be deleted\n",
1676                        libcfs_nid2str(prim_nid));
1677                 return -EBUSY;
1678         }
1679         lnet_net_unlock(LNET_LOCK_EX);
1680
1681         if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1682                 return lnet_peer_del(lp);
1683
1684         flags = LNET_PEER_CONFIGURED;
1685         if (lp->lp_state & LNET_PEER_MULTI_RAIL)
1686                 flags |= LNET_PEER_MULTI_RAIL;
1687
1688         return lnet_peer_del_nid(lp, nid, flags);
1689 }
1690
1691 void
1692 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1693 {
1694         struct lnet_peer_table *ptable;
1695         struct lnet_peer_net *lpn;
1696
1697         CDEBUG(D_NET, "%p nid %s\n", lpni, libcfs_nid2str(lpni->lpni_nid));
1698
1699         LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1700         LASSERT(list_empty(&lpni->lpni_txq));
1701         LASSERT(lpni->lpni_txqnob == 0);
1702         LASSERT(list_empty(&lpni->lpni_peer_nis));
1703         LASSERT(list_empty(&lpni->lpni_on_remote_peer_ni_list));
1704
1705         lpn = lpni->lpni_peer_net;
1706         lpni->lpni_peer_net = NULL;
1707         lpni->lpni_net = NULL;
1708
1709         /* remove the peer ni from the zombie list */
1710         ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1711         spin_lock(&ptable->pt_zombie_lock);
1712         list_del_init(&lpni->lpni_hashlist);
1713         ptable->pt_zombies--;
1714         spin_unlock(&ptable->pt_zombie_lock);
1715
1716         if (lpni->lpni_pref_nnids > 1)
1717                 CFS_FREE_PTR_ARRAY(lpni->lpni_pref.nids, lpni->lpni_pref_nnids);
1718
1719         LIBCFS_FREE(lpni, sizeof(*lpni));
1720
1721         lnet_peer_net_decref_locked(lpn);
1722 }
1723
1724 struct lnet_peer_ni *
1725 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1726 {
1727         struct lnet_peer_ni *lpni = NULL;
1728         int rc;
1729
1730         if (the_lnet.ln_state != LNET_STATE_RUNNING)
1731                 return ERR_PTR(-ESHUTDOWN);
1732
1733         /*
1734          * find if a peer_ni already exists.
1735          * If so then just return that.
1736          */
1737         lpni = lnet_find_peer_ni_locked(nid);
1738         if (lpni)
1739                 return lpni;
1740
1741         lnet_net_unlock(cpt);
1742
1743         rc = lnet_peer_ni_traffic_add(nid, LNET_NID_ANY);
1744         if (rc) {
1745                 lpni = ERR_PTR(rc);
1746                 goto out_net_relock;
1747         }
1748
1749         lpni = lnet_find_peer_ni_locked(nid);
1750         LASSERT(lpni);
1751
1752 out_net_relock:
1753         lnet_net_lock(cpt);
1754
1755         return lpni;
1756 }
1757
1758 /*
1759  * Get a peer_ni for the given nid, create it if necessary. Takes a
1760  * hold on the peer_ni.
1761  */
1762 struct lnet_peer_ni *
1763 lnet_nid2peerni_locked(lnet_nid_t nid, lnet_nid_t pref, int cpt)
1764 {
1765         struct lnet_peer_ni *lpni = NULL;
1766         int rc;
1767
1768         if (the_lnet.ln_state != LNET_STATE_RUNNING)
1769                 return ERR_PTR(-ESHUTDOWN);
1770
1771         /*
1772          * find if a peer_ni already exists.
1773          * If so then just return that.
1774          */
1775         lpni = lnet_find_peer_ni_locked(nid);
1776         if (lpni)
1777                 return lpni;
1778
1779         /*
1780          * Slow path:
1781          * use the lnet_api_mutex to serialize the creation of the peer_ni
1782          * and the creation/deletion of the local ni/net. When a local ni is
1783          * created, if there exists a set of peer_nis on that network,
1784          * they need to be traversed and updated. When a local NI is
1785          * deleted, which could result in a network being deleted, then
1786          * all peer nis on that network need to be removed as well.
1787          *
1788          * Creation through traffic should also be serialized with
1789          * creation through DLC.
1790          */
1791         lnet_net_unlock(cpt);
1792         mutex_lock(&the_lnet.ln_api_mutex);
1793         /*
1794          * Shutdown is only set under the ln_api_lock, so a single
1795          * check here is sufficent.
1796          */
1797         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1798                 lpni = ERR_PTR(-ESHUTDOWN);
1799                 goto out_mutex_unlock;
1800         }
1801
1802         rc = lnet_peer_ni_traffic_add(nid, pref);
1803         if (rc) {
1804                 lpni = ERR_PTR(rc);
1805                 goto out_mutex_unlock;
1806         }
1807
1808         lpni = lnet_find_peer_ni_locked(nid);
1809         LASSERT(lpni);
1810
1811 out_mutex_unlock:
1812         mutex_unlock(&the_lnet.ln_api_mutex);
1813         lnet_net_lock(cpt);
1814
1815         /* Lock has been dropped, check again for shutdown. */
1816         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1817                 if (!IS_ERR(lpni))
1818                         lnet_peer_ni_decref_locked(lpni);
1819                 lpni = ERR_PTR(-ESHUTDOWN);
1820         }
1821
1822         return lpni;
1823 }
1824
1825 bool
1826 lnet_peer_gw_discovery(struct lnet_peer *lp)
1827 {
1828         bool rc = false;
1829
1830         spin_lock(&lp->lp_lock);
1831         if (lp->lp_state & LNET_PEER_RTR_DISCOVERY)
1832                 rc = true;
1833         spin_unlock(&lp->lp_lock);
1834
1835         return rc;
1836 }
1837
1838 bool
1839 lnet_peer_is_uptodate(struct lnet_peer *lp)
1840 {
1841         bool rc;
1842
1843         spin_lock(&lp->lp_lock);
1844         rc = lnet_peer_is_uptodate_locked(lp);
1845         spin_unlock(&lp->lp_lock);
1846         return rc;
1847 }
1848
1849 /*
1850  * Is a peer uptodate from the point of view of discovery?
1851  *
1852  * If it is currently being processed, obviously not.
1853  * A forced Ping or Push is also handled by the discovery thread.
1854  *
1855  * Otherwise look at whether the peer needs rediscovering.
1856  */
1857 bool
1858 lnet_peer_is_uptodate_locked(struct lnet_peer *lp)
1859 __must_hold(&lp->lp_lock)
1860 {
1861         bool rc;
1862
1863         if (lp->lp_state & (LNET_PEER_DISCOVERING |
1864                             LNET_PEER_FORCE_PING |
1865                             LNET_PEER_FORCE_PUSH)) {
1866                 rc = false;
1867         } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1868                 rc = false;
1869         } else if (lnet_peer_needs_push(lp)) {
1870                 rc = false;
1871         } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1872                 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1873                         rc = true;
1874                 else
1875                         rc = false;
1876         } else {
1877                 rc = false;
1878         }
1879
1880         return rc;
1881 }
1882
1883 /*
1884  * Queue a peer for the attention of the discovery thread.  Call with
1885  * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1886  * -EALREADY if the peer was already queued.
1887  */
1888 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1889 {
1890         int rc;
1891
1892         spin_lock(&lp->lp_lock);
1893         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1894                 lp->lp_state |= LNET_PEER_DISCOVERING;
1895         spin_unlock(&lp->lp_lock);
1896         if (list_empty(&lp->lp_dc_list)) {
1897                 lnet_peer_addref_locked(lp);
1898                 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1899                 wake_up(&the_lnet.ln_dc_waitq);
1900                 rc = 0;
1901         } else {
1902                 rc = -EALREADY;
1903         }
1904
1905         CDEBUG(D_NET, "Queue peer %s: %d\n",
1906                libcfs_nid2str(lp->lp_primary_nid), rc);
1907
1908         return rc;
1909 }
1910
1911 /*
1912  * Discovery of a peer is complete. Wake all waiters on the peer.
1913  * Call with lnet_net_lock/EX held.
1914  */
1915 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1916 {
1917         struct lnet_msg *msg, *tmp;
1918         int rc = 0;
1919         LIST_HEAD(pending_msgs);
1920
1921         CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1922                libcfs_nid2str(lp->lp_primary_nid));
1923
1924         list_del_init(&lp->lp_dc_list);
1925         spin_lock(&lp->lp_lock);
1926         list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1927         spin_unlock(&lp->lp_lock);
1928         wake_up_all(&lp->lp_dc_waitq);
1929
1930         if (lp->lp_rtr_refcount > 0)
1931                 lnet_router_discovery_complete(lp);
1932
1933         lnet_net_unlock(LNET_LOCK_EX);
1934
1935         /* iterate through all pending messages and send them again */
1936         list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1937                 list_del_init(&msg->msg_list);
1938                 if (lp->lp_dc_error) {
1939                         lnet_finalize(msg, lp->lp_dc_error);
1940                         continue;
1941                 }
1942
1943                 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1944                        lnet_msgtyp2str(msg->msg_type),
1945                        libcfs_id2str(msg->msg_target));
1946                 rc = lnet_send(msg->msg_src_nid_param, msg,
1947                                msg->msg_rtr_nid_param);
1948                 if (rc < 0) {
1949                         CNETERR("Error sending %s to %s: %d\n",
1950                                lnet_msgtyp2str(msg->msg_type),
1951                                libcfs_id2str(msg->msg_target), rc);
1952                         lnet_finalize(msg, rc);
1953                 }
1954         }
1955         lnet_net_lock(LNET_LOCK_EX);
1956         lnet_peer_decref_locked(lp);
1957 }
1958
1959 /*
1960  * Handle inbound push.
1961  * Like any event handler, called with lnet_res_lock/CPT held.
1962  */
1963 void lnet_peer_push_event(struct lnet_event *ev)
1964 {
1965         struct lnet_ping_buffer *pbuf;
1966         struct lnet_peer *lp;
1967
1968         pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start + ev->offset);
1969
1970         /* lnet_find_peer() adds a refcount */
1971         lp = lnet_find_peer(ev->source.nid);
1972         if (!lp) {
1973                 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1974                        libcfs_nid2str(ev->initiator.nid),
1975                        libcfs_nid2str(ev->source.nid));
1976                 pbuf->pb_needs_post = true;
1977                 return;
1978         }
1979
1980         /* Ensure peer state remains consistent while we modify it. */
1981         spin_lock(&lp->lp_lock);
1982
1983         /*
1984          * If some kind of error happened the contents of the message
1985          * cannot be used. Clear the NIDS_UPTODATE and set the
1986          * FORCE_PING flag to trigger a ping.
1987          */
1988         if (ev->status) {
1989                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1990                 lp->lp_state |= LNET_PEER_FORCE_PING;
1991                 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1992                        ev->status,
1993                        libcfs_nid2str(lp->lp_primary_nid),
1994                        libcfs_nid2str(ev->source.nid));
1995                 goto out;
1996         }
1997
1998         /*
1999          * A push with invalid or corrupted info. Clear the UPTODATE
2000          * flag to trigger a ping.
2001          */
2002         if (lnet_ping_info_validate(&pbuf->pb_info)) {
2003                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2004                 lp->lp_state |= LNET_PEER_FORCE_PING;
2005                 CDEBUG(D_NET, "Corrupted Push from %s\n",
2006                        libcfs_nid2str(lp->lp_primary_nid));
2007                 goto out;
2008         }
2009
2010         /*
2011          * Make sure we'll allocate the correct size ping buffer when
2012          * pinging the peer.
2013          */
2014         if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2015                 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2016
2017         /*
2018          * A non-Multi-Rail peer is not supposed to be capable of
2019          * sending a push.
2020          */
2021         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2022                 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2023                        libcfs_nid2str(lp->lp_primary_nid));
2024                 goto out;
2025         }
2026
2027         /*
2028          * The peer may have discovery disabled at its end. Set
2029          * NO_DISCOVERY as appropriate.
2030          */
2031         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2032                 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2033                        libcfs_nid2str(lp->lp_primary_nid));
2034                 /*
2035                  * Mark the peer for deletion if we already know about it
2036                  * and it's going from discovery set to no discovery set
2037                  */
2038                 if (!(lp->lp_state & (LNET_PEER_NO_DISCOVERY |
2039                                       LNET_PEER_DISCOVERING)) &&
2040                      lp->lp_state & LNET_PEER_DISCOVERED) {
2041                         CDEBUG(D_NET, "Marking %s:0x%x for deletion\n",
2042                                libcfs_nid2str(lp->lp_primary_nid),
2043                                lp->lp_state);
2044                         lp->lp_state |= LNET_PEER_MARK_DELETION;
2045                 }
2046                 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2047         } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2048                 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2049                        libcfs_nid2str(lp->lp_primary_nid));
2050                 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2051         }
2052
2053         /*
2054          * Update the MULTI_RAIL flag based on the push. If the peer
2055          * was configured with DLC then the setting should match what
2056          * DLC put in.
2057          * NB: We verified above that the MR feature bit is set in pi_features
2058          */
2059         if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2060                 CDEBUG(D_NET, "peer %s(%p) is MR\n",
2061                        libcfs_nid2str(lp->lp_primary_nid), lp);
2062         } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2063                 CWARN("Push says %s is Multi-Rail, DLC says not\n",
2064                       libcfs_nid2str(lp->lp_primary_nid));
2065         } else if (lnet_peer_discovery_disabled) {
2066                 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled locally\n",
2067                        libcfs_nid2str(lp->lp_primary_nid), lp);
2068         } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2069                 CDEBUG(D_NET, "peer %s(%p) not MR: DD disabled remotely\n",
2070                        libcfs_nid2str(lp->lp_primary_nid), lp);
2071         } else {
2072                 CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2073                        libcfs_nid2str(lp->lp_primary_nid), lp);
2074                 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2075                 lnet_peer_clr_non_mr_pref_nids(lp);
2076         }
2077
2078         /*
2079          * Check for truncation of the Put message. Clear the
2080          * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2081          * and tell discovery to allocate a bigger buffer.
2082          */
2083         if (ev->mlength < ev->rlength) {
2084                 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2085                         the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2086                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2087                 lp->lp_state |= LNET_PEER_FORCE_PING;
2088                 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2089                        libcfs_nid2str(lp->lp_primary_nid),
2090                        pbuf->pb_info.pi_nnis);
2091                 goto out;
2092         }
2093
2094         /* always assume new data */
2095         lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2096         lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2097
2098         /*
2099          * If there is data present that hasn't been processed yet,
2100          * we'll replace it if the Put contained newer data and it
2101          * fits. We're racing with a Ping or earlier Push in this
2102          * case.
2103          */
2104         if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2105                 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2106                         LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2107                     pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2108                         memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2109                                LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2110                         CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2111                               libcfs_nid2str(lp->lp_primary_nid),
2112                               LNET_PING_BUFFER_SEQNO(pbuf),
2113                               LNET_PING_BUFFER_SEQNO(lp->lp_data));
2114                 }
2115                 goto out;
2116         }
2117
2118         /*
2119          * Allocate a buffer to copy the data. On a failure we drop
2120          * the Push and set FORCE_PING to force the discovery
2121          * thread to fix the problem by pinging the peer.
2122          */
2123         lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2124         if (!lp->lp_data) {
2125                 lp->lp_state |= LNET_PEER_FORCE_PING;
2126                 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2127                        libcfs_nid2str(lp->lp_primary_nid),
2128                        LNET_PING_BUFFER_SEQNO(pbuf));
2129                 goto out;
2130         }
2131
2132         /* Success */
2133         memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2134                LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2135         lp->lp_state |= LNET_PEER_DATA_PRESENT;
2136         CDEBUG(D_NET, "Received Push %s %u\n",
2137                libcfs_nid2str(lp->lp_primary_nid),
2138                LNET_PING_BUFFER_SEQNO(pbuf));
2139
2140 out:
2141         /* We've processed this buffer. It can be reposted */
2142         pbuf->pb_needs_post = true;
2143
2144         /*
2145          * Queue the peer for discovery if not done, force it on the request
2146          * queue and wake the discovery thread if the peer was already queued,
2147          * because its status changed.
2148          */
2149         spin_unlock(&lp->lp_lock);
2150         lnet_net_lock(LNET_LOCK_EX);
2151         if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2152                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2153                 wake_up(&the_lnet.ln_dc_waitq);
2154         }
2155         /* Drop refcount from lookup */
2156         lnet_peer_decref_locked(lp);
2157         lnet_net_unlock(LNET_LOCK_EX);
2158 }
2159
2160 /*
2161  * Clear the discovery error state, unless we're already discovering
2162  * this peer, in which case the error is current.
2163  */
2164 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2165 {
2166         spin_lock(&lp->lp_lock);
2167         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2168                 lp->lp_dc_error = 0;
2169         spin_unlock(&lp->lp_lock);
2170 }
2171
2172 /*
2173  * Peer discovery slow path. The ln_api_mutex is held on entry, and
2174  * dropped/retaken within this function. An lnet_peer_ni is passed in
2175  * because discovery could tear down an lnet_peer.
2176  */
2177 int
2178 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2179 {
2180         DEFINE_WAIT(wait);
2181         struct lnet_peer *lp;
2182         int rc = 0;
2183         int count = 0;
2184
2185 again:
2186         lnet_net_unlock(cpt);
2187         lnet_net_lock(LNET_LOCK_EX);
2188         lp = lpni->lpni_peer_net->lpn_peer;
2189         lnet_peer_clear_discovery_error(lp);
2190
2191         /*
2192          * We're willing to be interrupted. The lpni can become a
2193          * zombie if we race with DLC, so we must check for that.
2194          */
2195         for (;;) {
2196                 /* Keep lp alive when the lnet_net_lock is unlocked */
2197                 lnet_peer_addref_locked(lp);
2198                 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2199                 if (signal_pending(current))
2200                         break;
2201                 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2202                         break;
2203                 /*
2204                  * Don't repeat discovery if discovery is disabled. This is
2205                  * done to ensure we can use discovery as a standard ping as
2206                  * well for backwards compatibility with routers which do not
2207                  * have discovery or have discovery disabled
2208                  */
2209                 if (lnet_is_discovery_disabled(lp) && count > 0)
2210                         break;
2211                 if (lp->lp_dc_error)
2212                         break;
2213                 if (lnet_peer_is_uptodate(lp))
2214                         break;
2215                 lnet_peer_queue_for_discovery(lp);
2216                 count++;
2217                 CDEBUG(D_NET, "Discovery attempt # %d\n", count);
2218
2219                 /*
2220                  * If caller requested a non-blocking operation then
2221                  * return immediately. Once discovery is complete any
2222                  * pending messages that were stopped due to discovery
2223                  * will be transmitted.
2224                  */
2225                 if (!block)
2226                         break;
2227
2228                 lnet_net_unlock(LNET_LOCK_EX);
2229                 schedule();
2230                 finish_wait(&lp->lp_dc_waitq, &wait);
2231                 lnet_net_lock(LNET_LOCK_EX);
2232                 lnet_peer_decref_locked(lp);
2233                 /* Peer may have changed */
2234                 lp = lpni->lpni_peer_net->lpn_peer;
2235         }
2236         finish_wait(&lp->lp_dc_waitq, &wait);
2237
2238         lnet_net_unlock(LNET_LOCK_EX);
2239         lnet_net_lock(cpt);
2240         lnet_peer_decref_locked(lp);
2241         /*
2242          * The peer may have changed, so re-check and rediscover if that turns
2243          * out to have been the case. The reference count on lp ensured that
2244          * even if it was unlinked from lpni the memory could not be recycled.
2245          * Thus the check below is sufficient to determine whether the peer
2246          * changed. If the peer changed, then lp must not be dereferenced.
2247          */
2248         if (lp != lpni->lpni_peer_net->lpn_peer)
2249                 goto again;
2250
2251         if (signal_pending(current))
2252                 rc = -EINTR;
2253         else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2254                 rc = -ESHUTDOWN;
2255         else if (lp->lp_dc_error)
2256                 rc = lp->lp_dc_error;
2257         else if (!block)
2258                 CDEBUG(D_NET, "non-blocking discovery\n");
2259         else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2260                 goto again;
2261
2262         CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2263                (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2264                libcfs_nid2str(lpni->lpni_nid), rc,
2265                (!block) ? "pending discovery" : "discovery complete");
2266
2267         return rc;
2268 }
2269
2270 /* Handle an incoming ack for a push. */
2271 static void
2272 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2273 {
2274         struct lnet_ping_buffer *pbuf;
2275
2276         pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2277         spin_lock(&lp->lp_lock);
2278         lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2279         lp->lp_push_error = ev->status;
2280         if (ev->status)
2281                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2282         else
2283                 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2284         spin_unlock(&lp->lp_lock);
2285
2286         CDEBUG(D_NET, "peer %s ev->status %d\n",
2287                libcfs_nid2str(lp->lp_primary_nid), ev->status);
2288 }
2289
2290 /* Handle a Reply message. This is the reply to a Ping message. */
2291 static void
2292 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2293 {
2294         struct lnet_ping_buffer *pbuf;
2295         int rc;
2296
2297         spin_lock(&lp->lp_lock);
2298
2299         lp->lp_disc_src_nid = ev->target.nid;
2300
2301         /*
2302          * If some kind of error happened the contents of message
2303          * cannot be used. Set PING_FAILED to trigger a retry.
2304          */
2305         if (ev->status) {
2306                 lp->lp_state |= LNET_PEER_PING_FAILED;
2307                 lp->lp_ping_error = ev->status;
2308                 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2309                        ev->status,
2310                        libcfs_nid2str(lp->lp_primary_nid),
2311                        libcfs_nid2str(ev->source.nid));
2312                 goto out;
2313         }
2314
2315         pbuf = LNET_PING_INFO_TO_BUFFER(ev->md_start);
2316         if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2317                 lnet_swap_pinginfo(pbuf);
2318
2319         /*
2320          * A reply with invalid or corrupted info. Set PING_FAILED to
2321          * trigger a retry.
2322          */
2323         rc = lnet_ping_info_validate(&pbuf->pb_info);
2324         if (rc) {
2325                 lp->lp_state |= LNET_PEER_PING_FAILED;
2326                 lp->lp_ping_error = 0;
2327                 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2328                        libcfs_nid2str(lp->lp_primary_nid), rc);
2329                 goto out;
2330         }
2331
2332
2333         /*
2334          * The peer may have discovery disabled at its end. Set
2335          * NO_DISCOVERY as appropriate.
2336          */
2337         if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2338             !lnet_peer_discovery_disabled) {
2339                 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2340                        libcfs_nid2str(lp->lp_primary_nid));
2341                 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2342         } else {
2343                 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2344                        libcfs_nid2str(lp->lp_primary_nid));
2345                 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2346         }
2347
2348         /*
2349          * Update the MULTI_RAIL flag based on the reply. If the peer
2350          * was configured with DLC then the setting should match what
2351          * DLC put in.
2352          */
2353         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2354                 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2355                         CDEBUG(D_NET, "peer %s(%p) is MR\n",
2356                                libcfs_nid2str(lp->lp_primary_nid), lp);
2357                 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2358                         CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2359                               libcfs_nid2str(lp->lp_primary_nid));
2360                 } else if (lnet_peer_discovery_disabled) {
2361                         CDEBUG(D_NET,
2362                                "peer %s(%p) not MR: DD disabled locally\n",
2363                                libcfs_nid2str(lp->lp_primary_nid), lp);
2364                 } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2365                         CDEBUG(D_NET,
2366                                "peer %s(%p) not MR: DD disabled remotely\n",
2367                                libcfs_nid2str(lp->lp_primary_nid), lp);
2368                 } else {
2369                         CDEBUG(D_NET, "peer %s(%p) is MR capable\n",
2370                                libcfs_nid2str(lp->lp_primary_nid), lp);
2371                         lp->lp_state |= LNET_PEER_MULTI_RAIL;
2372                         lnet_peer_clr_non_mr_pref_nids(lp);
2373                 }
2374         } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2375                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2376                         CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2377                               libcfs_nid2str(lp->lp_primary_nid));
2378                 } else {
2379                         CERROR("Multi-Rail state vanished from %s\n",
2380                                libcfs_nid2str(lp->lp_primary_nid));
2381                         lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2382                 }
2383         }
2384
2385         /*
2386          * Make sure we'll allocate the correct size ping buffer when
2387          * pinging the peer.
2388          */
2389         if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2390                 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2391
2392         /*
2393          * Check for truncation of the Reply. Clear PING_SENT and set
2394          * PING_FAILED to trigger a retry.
2395          */
2396         if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2397                 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2398                         the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2399                 lp->lp_state |= LNET_PEER_PING_FAILED;
2400                 lp->lp_ping_error = 0;
2401                 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2402                        libcfs_nid2str(lp->lp_primary_nid),
2403                        pbuf->pb_info.pi_nnis);
2404                 goto out;
2405         }
2406
2407         /*
2408          * Check the sequence numbers in the reply. These are only
2409          * available if the reply came from a Multi-Rail peer.
2410          */
2411         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2412             pbuf->pb_info.pi_nnis > 1 &&
2413             lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2414                 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2415                         CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2416                                 libcfs_nid2str(lp->lp_primary_nid),
2417                                 LNET_PING_BUFFER_SEQNO(pbuf),
2418                                 lp->lp_peer_seqno);
2419
2420                 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2421         }
2422
2423         /* We're happy with the state of the data in the buffer. */
2424         CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2425                libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2426         if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2427                 lnet_ping_buffer_decref(lp->lp_data);
2428         else
2429                 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2430         lnet_ping_buffer_addref(pbuf);
2431         lp->lp_data = pbuf;
2432 out:
2433         lp->lp_state &= ~LNET_PEER_PING_SENT;
2434         spin_unlock(&lp->lp_lock);
2435
2436         lnet_net_lock(LNET_LOCK_EX);
2437         /*
2438          * If this peer is a gateway, call the routing callback to
2439          * handle the ping reply
2440          */
2441         if (lp->lp_rtr_refcount > 0)
2442                 lnet_router_discovery_ping_reply(lp);
2443         lnet_net_unlock(LNET_LOCK_EX);
2444 }
2445
2446 /*
2447  * Send event handling. Only matters for error cases, where we clean
2448  * up state on the peer and peer_ni that would otherwise be updated in
2449  * the REPLY event handler for a successful Ping, and the ACK event
2450  * handler for a successful Push.
2451  */
2452 static int
2453 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2454 {
2455         int rc = 0;
2456
2457         if (!ev->status)
2458                 goto out;
2459
2460         spin_lock(&lp->lp_lock);
2461         if (ev->msg_type == LNET_MSG_GET) {
2462                 lp->lp_state &= ~LNET_PEER_PING_SENT;
2463                 lp->lp_state |= LNET_PEER_PING_FAILED;
2464                 lp->lp_ping_error = ev->status;
2465         } else { /* ev->msg_type == LNET_MSG_PUT */
2466                 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2467                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2468                 lp->lp_push_error = ev->status;
2469         }
2470         spin_unlock(&lp->lp_lock);
2471         rc = LNET_REDISCOVER_PEER;
2472 out:
2473         CDEBUG(D_NET, "%s Send to %s: %d\n",
2474                 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2475                 libcfs_nid2str(ev->target.nid), rc);
2476         return rc;
2477 }
2478
2479 /*
2480  * Unlink event handling. This event is only seen if a call to
2481  * LNetMDUnlink() caused the event to be unlinked. If this call was
2482  * made after the event was set up in LNetGet() or LNetPut() then we
2483  * assume the Ping or Push timed out.
2484  */
2485 static void
2486 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2487 {
2488         spin_lock(&lp->lp_lock);
2489         /* We've passed through LNetGet() */
2490         if (lp->lp_state & LNET_PEER_PING_SENT) {
2491                 lp->lp_state &= ~LNET_PEER_PING_SENT;
2492                 lp->lp_state |= LNET_PEER_PING_FAILED;
2493                 lp->lp_ping_error = -ETIMEDOUT;
2494                 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2495                         libcfs_nid2str(lp->lp_primary_nid));
2496         }
2497         /* We've passed through LNetPut() */
2498         if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2499                 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2500                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2501                 lp->lp_push_error = -ETIMEDOUT;
2502                 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2503                         libcfs_nid2str(lp->lp_primary_nid));
2504         }
2505         spin_unlock(&lp->lp_lock);
2506 }
2507
2508 /*
2509  * Event handler for the discovery EQ.
2510  *
2511  * Called with lnet_res_lock(cpt) held. The cpt is the
2512  * lnet_cpt_of_cookie() of the md handle cookie.
2513  */
2514 static void lnet_discovery_event_handler(struct lnet_event *event)
2515 {
2516         struct lnet_peer *lp = event->md_user_ptr;
2517         struct lnet_ping_buffer *pbuf;
2518         int rc;
2519
2520         /* discovery needs to take another look */
2521         rc = LNET_REDISCOVER_PEER;
2522
2523         CDEBUG(D_NET, "Received event: %d\n", event->type);
2524
2525         switch (event->type) {
2526         case LNET_EVENT_ACK:
2527                 lnet_discovery_event_ack(lp, event);
2528                 break;
2529         case LNET_EVENT_REPLY:
2530                 lnet_discovery_event_reply(lp, event);
2531                 break;
2532         case LNET_EVENT_SEND:
2533                 /* Only send failure triggers a retry. */
2534                 rc = lnet_discovery_event_send(lp, event);
2535                 break;
2536         case LNET_EVENT_UNLINK:
2537                 /* LNetMDUnlink() was called */
2538                 lnet_discovery_event_unlink(lp, event);
2539                 break;
2540         default:
2541                 /* Invalid events. */
2542                 LBUG();
2543         }
2544         lnet_net_lock(LNET_LOCK_EX);
2545         if (event->unlinked) {
2546                 pbuf = LNET_PING_INFO_TO_BUFFER(event->md_start);
2547                 lnet_ping_buffer_decref(pbuf);
2548                 lnet_peer_decref_locked(lp);
2549         }
2550
2551         /* put peer back at end of request queue, if discovery not already
2552          * done */
2553         if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2554                 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2555                 wake_up(&the_lnet.ln_dc_waitq);
2556         }
2557         lnet_net_unlock(LNET_LOCK_EX);
2558 }
2559
2560 /*
2561  * Build a peer from incoming data.
2562  *
2563  * The NIDs in the incoming data are supposed to be structured as follows:
2564  *  - loopback
2565  *  - primary NID
2566  *  - other NIDs in same net
2567  *  - NIDs in second net
2568  *  - NIDs in third net
2569  *  - ...
2570  * This due to the way the list of NIDs in the data is created.
2571  *
2572  * Note that this function will mark the peer uptodate unless an
2573  * ENOMEM is encontered. All other errors are due to a conflict
2574  * between the DLC configuration and what discovery sees. We treat DLC
2575  * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2576  * peer from becoming stuck in discovery.
2577  */
2578 static int lnet_peer_merge_data(struct lnet_peer *lp,
2579                                 struct lnet_ping_buffer *pbuf)
2580 {
2581         struct lnet_peer_ni *lpni;
2582         lnet_nid_t *curnis = NULL;
2583         struct lnet_ni_status *addnis = NULL;
2584         lnet_nid_t *delnis = NULL;
2585         unsigned flags;
2586         int ncurnis;
2587         int naddnis;
2588         int ndelnis;
2589         int nnis = 0;
2590         int i;
2591         int j;
2592         int rc;
2593
2594         flags = LNET_PEER_DISCOVERED;
2595         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2596                 flags |= LNET_PEER_MULTI_RAIL;
2597
2598         /*
2599          * Cache the routing feature for the peer; whether it is enabled
2600          * for disabled as reported by the remote peer.
2601          */
2602         spin_lock(&lp->lp_lock);
2603         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2604                 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2605         else
2606                 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2607         spin_unlock(&lp->lp_lock);
2608
2609         nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
2610         CFS_ALLOC_PTR_ARRAY(curnis, nnis);
2611         CFS_ALLOC_PTR_ARRAY(addnis, nnis);
2612         CFS_ALLOC_PTR_ARRAY(delnis, nnis);
2613         if (!curnis || !addnis || !delnis) {
2614                 rc = -ENOMEM;
2615                 goto out;
2616         }
2617         ncurnis = 0;
2618         naddnis = 0;
2619         ndelnis = 0;
2620
2621         /* Construct the list of NIDs present in peer. */
2622         lpni = NULL;
2623         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2624                 curnis[ncurnis++] = lpni->lpni_nid;
2625
2626         /*
2627          * Check for NIDs in pbuf not present in curnis[].
2628          * The loop starts at 1 to skip the loopback NID.
2629          */
2630         for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2631                 for (j = 0; j < ncurnis; j++)
2632                         if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2633                                 break;
2634                 if (j == ncurnis)
2635                         addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2636         }
2637         /*
2638          * Check for NIDs in curnis[] not present in pbuf.
2639          * The nested loop starts at 1 to skip the loopback NID.
2640          *
2641          * But never add the loopback NID to delnis[]: if it is
2642          * present in curnis[] then this peer is for this node.
2643          */
2644         for (i = 0; i < ncurnis; i++) {
2645                 if (curnis[i] == LNET_NID_LO_0)
2646                         continue;
2647                 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2648                         if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2649                                 /*
2650                                  * update the information we cache for the
2651                                  * peer with the latest information we
2652                                  * received
2653                                  */
2654                                 lpni = lnet_find_peer_ni_locked(curnis[i]);
2655                                 if (lpni) {
2656                                         lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2657                                         lnet_peer_ni_decref_locked(lpni);
2658                                 }
2659                                 break;
2660                         }
2661                 }
2662                 if (j == pbuf->pb_info.pi_nnis)
2663                         delnis[ndelnis++] = curnis[i];
2664         }
2665
2666         /*
2667          * If we get here and the discovery is disabled then we don't want
2668          * to add or delete any NIs. We just updated the ones we have some
2669          * information on, and call it a day
2670          */
2671         rc = 0;
2672         if (lnet_is_discovery_disabled(lp))
2673                 goto out;
2674
2675         for (i = 0; i < naddnis; i++) {
2676                 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2677                 if (rc) {
2678                         CERROR("Error adding NID %s to peer %s: %d\n",
2679                                libcfs_nid2str(addnis[i].ns_nid),
2680                                libcfs_nid2str(lp->lp_primary_nid), rc);
2681                         if (rc == -ENOMEM)
2682                                 goto out;
2683                 }
2684                 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2685                 if (lpni) {
2686                         lpni->lpni_ns_status = addnis[i].ns_status;
2687                         lnet_peer_ni_decref_locked(lpni);
2688                 }
2689         }
2690
2691         for (i = 0; i < ndelnis; i++) {
2692                 /*
2693                  * for routers it's okay to delete the primary_nid because
2694                  * the upper layers don't really rely on it. So if we're
2695                  * being told that the router changed its primary_nid
2696                  * then it's okay to delete it.
2697                  */
2698                 if (lp->lp_rtr_refcount > 0)
2699                         flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2700                 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2701                 if (rc) {
2702                         CERROR("Error deleting NID %s from peer %s: %d\n",
2703                                libcfs_nid2str(delnis[i]),
2704                                libcfs_nid2str(lp->lp_primary_nid), rc);
2705                         if (rc == -ENOMEM)
2706                                 goto out;
2707                 }
2708         }
2709         /*
2710          * Errors other than -ENOMEM are due to peers having been
2711          * configured with DLC. Ignore these because DLC overrides
2712          * Discovery.
2713          */
2714         rc = 0;
2715 out:
2716         CFS_FREE_PTR_ARRAY(curnis, nnis);
2717         CFS_FREE_PTR_ARRAY(addnis, nnis);
2718         CFS_FREE_PTR_ARRAY(delnis, nnis);
2719         lnet_ping_buffer_decref(pbuf);
2720         CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2721
2722         if (rc) {
2723                 spin_lock(&lp->lp_lock);
2724                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2725                 lp->lp_state |= LNET_PEER_FORCE_PING;
2726                 spin_unlock(&lp->lp_lock);
2727         }
2728         return rc;
2729 }
2730
2731 /*
2732  * The data in pbuf says lp is its primary peer, but the data was
2733  * received by a different peer. Try to update lp with the data.
2734  */
2735 static int
2736 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2737 {
2738         struct lnet_handle_md mdh;
2739
2740         /* Queue lp for discovery, and force it on the request queue. */
2741         lnet_net_lock(LNET_LOCK_EX);
2742         if (lnet_peer_queue_for_discovery(lp))
2743                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2744         lnet_net_unlock(LNET_LOCK_EX);
2745
2746         LNetInvalidateMDHandle(&mdh);
2747
2748         /*
2749          * Decide whether we can move the peer to the DATA_PRESENT state.
2750          *
2751          * We replace stale data for a multi-rail peer, repair PING_FAILED
2752          * status, and preempt FORCE_PING.
2753          *
2754          * If after that we have DATA_PRESENT, we merge it into this peer.
2755          */
2756         spin_lock(&lp->lp_lock);
2757         if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2758                 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2759                         lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2760                 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2761                         lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2762                         lnet_ping_buffer_decref(pbuf);
2763                         pbuf = lp->lp_data;
2764                         lp->lp_data = NULL;
2765                 }
2766         }
2767         if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2768                 lnet_ping_buffer_decref(lp->lp_data);
2769                 lp->lp_data = NULL;
2770                 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2771         }
2772         if (lp->lp_state & LNET_PEER_PING_FAILED) {
2773                 mdh = lp->lp_ping_mdh;
2774                 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2775                 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2776                 lp->lp_ping_error = 0;
2777         }
2778         if (lp->lp_state & LNET_PEER_FORCE_PING)
2779                 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2780         lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2781         spin_unlock(&lp->lp_lock);
2782
2783         if (!LNetMDHandleIsInvalid(mdh))
2784                 LNetMDUnlink(mdh);
2785
2786         if (pbuf)
2787                 return lnet_peer_merge_data(lp, pbuf);
2788
2789         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2790         return 0;
2791 }
2792
2793 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2794 {
2795         int i;
2796
2797         for (i = 0; i < pinfo->pi_nnis; i++) {
2798                 if (pinfo->pi_ni[i].ns_nid == nid)
2799                         return true;
2800         }
2801
2802         return false;
2803 }
2804
2805 /*
2806  * Update a peer using the data received.
2807  */
2808 static int lnet_peer_data_present(struct lnet_peer *lp)
2809 __must_hold(&lp->lp_lock)
2810 {
2811         struct lnet_ping_buffer *pbuf;
2812         struct lnet_peer_ni *lpni;
2813         lnet_nid_t nid = LNET_NID_ANY;
2814         unsigned flags;
2815         int rc = 0;
2816
2817         pbuf = lp->lp_data;
2818         lp->lp_data = NULL;
2819         lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2820         lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2821         spin_unlock(&lp->lp_lock);
2822
2823         /*
2824          * Modifications of peer structures are done while holding the
2825          * ln_api_mutex. A global lock is required because we may be
2826          * modifying multiple peer structures, and a mutex greatly
2827          * simplifies memory management.
2828          *
2829          * The actual changes to the data structures must also protect
2830          * against concurrent lookups, for which the lnet_net_lock in
2831          * LNET_LOCK_EX mode is used.
2832          */
2833         mutex_lock(&the_lnet.ln_api_mutex);
2834         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2835                 rc = -ESHUTDOWN;
2836                 goto out;
2837         }
2838
2839         /*
2840          * If this peer is not on the peer list then it is being torn
2841          * down, and our reference count may be all that is keeping it
2842          * alive. Don't do any work on it.
2843          */
2844         if (list_empty(&lp->lp_peer_list))
2845                 goto out;
2846
2847         flags = LNET_PEER_DISCOVERED;
2848         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2849                 flags |= LNET_PEER_MULTI_RAIL;
2850
2851         /*
2852          * Check whether the primary NID in the message matches the
2853          * primary NID of the peer. If it does, update the peer, if
2854          * it it does not, check whether there is already a peer with
2855          * that primary NID. If no such peer exists, try to update
2856          * the primary NID of the current peer (allowed if it was
2857          * created due to message traffic) and complete the update.
2858          * If the peer did exist, hand off the data to it.
2859          *
2860          * The peer for the loopback interface is a special case: this
2861          * is the peer for the local node, and we want to set its
2862          * primary NID to the correct value here. Moreover, this peer
2863          * can show up with only the loopback NID in the ping buffer.
2864          */
2865         if (pbuf->pb_info.pi_nnis <= 1)
2866                 goto out;
2867         nid = pbuf->pb_info.pi_ni[1].ns_nid;
2868         if (lp->lp_primary_nid == LNET_NID_LO_0) {
2869                 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2870                 if (!rc)
2871                         rc = lnet_peer_merge_data(lp, pbuf);
2872         /*
2873          * if the primary nid of the peer is present in the ping info returned
2874          * from the peer, but it's not the local primary peer we have
2875          * cached and discovery is disabled, then we don't want to update
2876          * our local peer info, by adding or removing NIDs, we just want
2877          * to update the status of the nids that we currently have
2878          * recorded in that peer.
2879          */
2880         } else if (lp->lp_primary_nid == nid ||
2881                    (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2882                     lnet_is_discovery_disabled(lp))) {
2883                 rc = lnet_peer_merge_data(lp, pbuf);
2884         } else {
2885                 lpni = lnet_find_peer_ni_locked(nid);
2886                 if (!lpni) {
2887                         rc = lnet_peer_set_primary_nid(lp, nid, flags);
2888                         if (rc) {
2889                                 CERROR("Primary NID error %s versus %s: %d\n",
2890                                        libcfs_nid2str(lp->lp_primary_nid),
2891                                        libcfs_nid2str(nid), rc);
2892                         } else {
2893                                 rc = lnet_peer_merge_data(lp, pbuf);
2894                         }
2895                 } else {
2896                         struct lnet_peer *new_lp;
2897                         new_lp = lpni->lpni_peer_net->lpn_peer;
2898                         /*
2899                          * if lp has discovery/MR enabled that means new_lp
2900                          * should have discovery/MR enabled as well, since
2901                          * it's the same peer, which we're about to merge
2902                          */
2903                         if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2904                                 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2905                         if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2906                                 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2907
2908                         rc = lnet_peer_set_primary_data(new_lp, pbuf);
2909                         lnet_consolidate_routes_locked(lp, new_lp);
2910                         lnet_peer_ni_decref_locked(lpni);
2911                 }
2912         }
2913 out:
2914         CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2915                lp->lp_state);
2916         mutex_unlock(&the_lnet.ln_api_mutex);
2917
2918         spin_lock(&lp->lp_lock);
2919         /* Tell discovery to re-check the peer immediately. */
2920         if (!rc)
2921                 rc = LNET_REDISCOVER_PEER;
2922         return rc;
2923 }
2924
2925 /*
2926  * A ping failed. Clear the PING_FAILED state and set the
2927  * FORCE_PING state, to ensure a retry even if discovery is
2928  * disabled. This avoids being left with incorrect state.
2929  */
2930 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2931 __must_hold(&lp->lp_lock)
2932 {
2933         struct lnet_handle_md mdh;
2934         int rc;
2935
2936         mdh = lp->lp_ping_mdh;
2937         LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2938         lp->lp_state &= ~LNET_PEER_PING_FAILED;
2939         lp->lp_state |= LNET_PEER_FORCE_PING;
2940         rc = lp->lp_ping_error;
2941         lp->lp_ping_error = 0;
2942         spin_unlock(&lp->lp_lock);
2943
2944         if (!LNetMDHandleIsInvalid(mdh))
2945                 LNetMDUnlink(mdh);
2946
2947         CDEBUG(D_NET, "peer %s:%d\n",
2948                libcfs_nid2str(lp->lp_primary_nid), rc);
2949
2950         spin_lock(&lp->lp_lock);
2951         return rc ? rc : LNET_REDISCOVER_PEER;
2952 }
2953
2954 /*
2955  * Select NID to send a Ping or Push to.
2956  */
2957 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2958 {
2959         struct lnet_peer_ni *lpni;
2960
2961         /* Look for a direct-connected NID for this peer. */
2962         lpni = NULL;
2963         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2964                 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2965                         continue;
2966                 break;
2967         }
2968         if (lpni)
2969                 return lpni->lpni_nid;
2970
2971         /* Look for a routed-connected NID for this peer. */
2972         lpni = NULL;
2973         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2974                 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2975                         continue;
2976                 break;
2977         }
2978         if (lpni)
2979                 return lpni->lpni_nid;
2980
2981         return LNET_NID_ANY;
2982 }
2983
2984 /* Active side of ping. */
2985 static int lnet_peer_send_ping(struct lnet_peer *lp)
2986 __must_hold(&lp->lp_lock)
2987 {
2988         lnet_nid_t pnid;
2989         int nnis;
2990         int rc;
2991         int cpt;
2992
2993         lp->lp_state |= LNET_PEER_PING_SENT;
2994         lp->lp_state &= ~LNET_PEER_FORCE_PING;
2995         spin_unlock(&lp->lp_lock);
2996
2997         cpt = lnet_net_lock_current();
2998         /* Refcount for MD. */
2999         lnet_peer_addref_locked(lp);
3000         pnid = lnet_peer_select_nid(lp);
3001         lnet_net_unlock(cpt);
3002
3003         nnis = max(lp->lp_data_nnis, LNET_INTERFACES_MIN);
3004
3005         rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
3006                             the_lnet.ln_dc_handler, false);
3007
3008         /*
3009          * if LNetMDBind in lnet_send_ping fails we need to decrement the
3010          * refcount on the peer, otherwise LNetMDUnlink will be called
3011          * which will eventually do that.
3012          */
3013         if (rc > 0) {
3014                 lnet_net_lock(cpt);
3015                 lnet_peer_decref_locked(lp);
3016                 lnet_net_unlock(cpt);
3017                 rc = -rc; /* change the rc to negative value */
3018                 goto fail_error;
3019         } else if (rc < 0) {
3020                 goto fail_error;
3021         }
3022
3023         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3024
3025         spin_lock(&lp->lp_lock);
3026         return 0;
3027
3028 fail_error:
3029         CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3030         /*
3031          * The errors that get us here are considered hard errors and
3032          * cause Discovery to terminate. So we clear PING_SENT, but do
3033          * not set either PING_FAILED or FORCE_PING. In fact we need
3034          * to clear PING_FAILED, because the unlink event handler will
3035          * have set it if we called LNetMDUnlink() above.
3036          */
3037         spin_lock(&lp->lp_lock);
3038         lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3039         return rc;
3040 }
3041
3042 /*
3043  * This function exists because you cannot call LNetMDUnlink() from an
3044  * event handler.
3045  */
3046 static int lnet_peer_push_failed(struct lnet_peer *lp)
3047 __must_hold(&lp->lp_lock)
3048 {
3049         struct lnet_handle_md mdh;
3050         int rc;
3051
3052         mdh = lp->lp_push_mdh;
3053         LNetInvalidateMDHandle(&lp->lp_push_mdh);
3054         lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3055         rc = lp->lp_push_error;
3056         lp->lp_push_error = 0;
3057         spin_unlock(&lp->lp_lock);
3058
3059         if (!LNetMDHandleIsInvalid(mdh))
3060                 LNetMDUnlink(mdh);
3061
3062         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3063         spin_lock(&lp->lp_lock);
3064         return rc ? rc : LNET_REDISCOVER_PEER;
3065 }
3066
3067 /* Active side of push. */
3068 static int lnet_peer_send_push(struct lnet_peer *lp)
3069 __must_hold(&lp->lp_lock)
3070 {
3071         struct lnet_ping_buffer *pbuf;
3072         struct lnet_process_id id;
3073         struct lnet_md md;
3074         int cpt;
3075         int rc;
3076
3077         /* Don't push to a non-multi-rail peer. */
3078         if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3079                 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3080                 return 0;
3081         }
3082
3083         lp->lp_state |= LNET_PEER_PUSH_SENT;
3084         lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3085         spin_unlock(&lp->lp_lock);
3086
3087         cpt = lnet_net_lock_current();
3088         pbuf = the_lnet.ln_ping_target;
3089         lnet_ping_buffer_addref(pbuf);
3090         lnet_net_unlock(cpt);
3091
3092         /* Push source MD */
3093         md.start     = &pbuf->pb_info;
3094         md.length    = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3095         md.threshold = 2; /* Put/Ack */
3096         md.max_size  = 0;
3097         md.options   = 0;
3098         md.handler   = the_lnet.ln_dc_handler;
3099         md.user_ptr  = lp;
3100
3101         rc = LNetMDBind(&md, LNET_UNLINK, &lp->lp_push_mdh);
3102         if (rc) {
3103                 lnet_ping_buffer_decref(pbuf);
3104                 CERROR("Can't bind push source MD: %d\n", rc);
3105                 goto fail_error;
3106         }
3107         cpt = lnet_net_lock_current();
3108         /* Refcount for MD. */
3109         lnet_peer_addref_locked(lp);
3110         id.pid = LNET_PID_LUSTRE;
3111         id.nid = lnet_peer_select_nid(lp);
3112         lnet_net_unlock(cpt);
3113
3114         if (id.nid == LNET_NID_ANY) {
3115                 rc = -EHOSTUNREACH;
3116                 goto fail_unlink;
3117         }
3118
3119         rc = LNetPut(lp->lp_disc_src_nid, lp->lp_push_mdh,
3120                      LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3121                      LNET_PROTO_PING_MATCHBITS, 0, 0);
3122
3123         /*
3124          * reset the discovery nid. There is no need to restrict sending
3125          * from that source, if we call lnet_push_update_to_peers(). It'll
3126          * get set to a specific NID, if we initiate discovery from the
3127          * scratch
3128          */
3129         lp->lp_disc_src_nid = LNET_NID_ANY;
3130
3131         if (rc)
3132                 goto fail_unlink;
3133
3134         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3135
3136         spin_lock(&lp->lp_lock);
3137         return 0;
3138
3139 fail_unlink:
3140         LNetMDUnlink(lp->lp_push_mdh);
3141         LNetInvalidateMDHandle(&lp->lp_push_mdh);
3142 fail_error:
3143         CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3144         /*
3145          * The errors that get us here are considered hard errors and
3146          * cause Discovery to terminate. So we clear PUSH_SENT, but do
3147          * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3148          * because the unlink event handler will have set it if we
3149          * called LNetMDUnlink() above.
3150          */
3151         spin_lock(&lp->lp_lock);
3152         lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3153         return rc;
3154 }
3155
3156 /*
3157  * An unrecoverable error was encountered during discovery.
3158  * Set error status in peer and abort discovery.
3159  */
3160 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3161 {
3162         CDEBUG(D_NET, "Discovery error %s: %d\n",
3163                libcfs_nid2str(lp->lp_primary_nid), error);
3164
3165         spin_lock(&lp->lp_lock);
3166         lp->lp_dc_error = error;
3167         lp->lp_state &= ~LNET_PEER_DISCOVERING;
3168         lp->lp_state |= LNET_PEER_REDISCOVER;
3169         spin_unlock(&lp->lp_lock);
3170 }
3171
3172 /*
3173  * Mark the peer as discovered.
3174  */
3175 static int lnet_peer_discovered(struct lnet_peer *lp)
3176 __must_hold(&lp->lp_lock)
3177 {
3178         lp->lp_state |= LNET_PEER_DISCOVERED;
3179         lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3180                           LNET_PEER_REDISCOVER);
3181
3182         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3183
3184         return 0;
3185 }
3186
3187
3188 /*
3189  * Discovering this peer is taking too long. Cancel any Ping or Push
3190  * that discovery is waiting on by unlinking the relevant MDs. The
3191  * lnet_discovery_event_handler() will proceed from here and complete
3192  * the cleanup.
3193  */
3194 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3195 {
3196         struct lnet_handle_md ping_mdh;
3197         struct lnet_handle_md push_mdh;
3198
3199         LNetInvalidateMDHandle(&ping_mdh);
3200         LNetInvalidateMDHandle(&push_mdh);
3201
3202         spin_lock(&lp->lp_lock);
3203         if (lp->lp_state & LNET_PEER_PING_SENT) {
3204                 ping_mdh = lp->lp_ping_mdh;
3205                 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3206         }
3207         if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3208                 push_mdh = lp->lp_push_mdh;
3209                 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3210         }
3211         spin_unlock(&lp->lp_lock);
3212
3213         if (!LNetMDHandleIsInvalid(ping_mdh))
3214                 LNetMDUnlink(ping_mdh);
3215         if (!LNetMDHandleIsInvalid(push_mdh))
3216                 LNetMDUnlink(push_mdh);
3217 }
3218
3219 /*
3220  * Wait for work to be queued or some other change that must be
3221  * attended to. Returns non-zero if the discovery thread should shut
3222  * down.
3223  */
3224 static int lnet_peer_discovery_wait_for_work(void)
3225 {
3226         int cpt;
3227         int rc = 0;
3228
3229         DEFINE_WAIT(wait);
3230
3231         cpt = lnet_net_lock_current();
3232         for (;;) {
3233                 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3234                                 TASK_INTERRUPTIBLE);
3235                 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3236                         break;
3237                 if (lnet_push_target_resize_needed() ||
3238                     the_lnet.ln_push_target->pb_needs_post)
3239                         break;
3240                 if (!list_empty(&the_lnet.ln_dc_request))
3241                         break;
3242                 if (!list_empty(&the_lnet.ln_msg_resend))
3243                         break;
3244                 lnet_net_unlock(cpt);
3245
3246                 /*
3247                  * wakeup max every second to check if there are peers that
3248                  * have been stuck on the working queue for greater than
3249                  * the peer timeout.
3250                  */
3251                 schedule_timeout(cfs_time_seconds(1));
3252                 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3253                 cpt = lnet_net_lock_current();
3254         }
3255         finish_wait(&the_lnet.ln_dc_waitq, &wait);
3256
3257         if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3258                 rc = -ESHUTDOWN;
3259
3260         lnet_net_unlock(cpt);
3261
3262         CDEBUG(D_NET, "woken: %d\n", rc);
3263
3264         return rc;
3265 }
3266
3267 /*
3268  * Messages that were pending on a destroyed peer will be put on a global
3269  * resend list. The message resend list will be checked by
3270  * the discovery thread when it wakes up, and will resend messages. These
3271  * messages can still be sendable in the case the lpni which was the initial
3272  * cause of the message re-queue was transfered to another peer.
3273  *
3274  * It is possible that LNet could be shutdown while we're iterating
3275  * through the list. lnet_shudown_lndnets() will attempt to access the
3276  * resend list, but will have to wait until the spinlock is released, by
3277  * which time there shouldn't be any more messages on the resend list.
3278  * During shutdown lnet_send() will fail and lnet_finalize() will be called
3279  * for the messages so they can be released. The other case is that
3280  * lnet_shudown_lndnets() can finalize all the messages before this
3281  * function can visit the resend list, in which case this function will be
3282  * a no-op.
3283  */
3284 static void lnet_resend_msgs(void)
3285 {
3286         struct lnet_msg *msg, *tmp;
3287         LIST_HEAD(resend);
3288         int rc;
3289
3290         spin_lock(&the_lnet.ln_msg_resend_lock);
3291         list_splice(&the_lnet.ln_msg_resend, &resend);
3292         spin_unlock(&the_lnet.ln_msg_resend_lock);
3293
3294         list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3295                 list_del_init(&msg->msg_list);
3296                 rc = lnet_send(msg->msg_src_nid_param, msg,
3297                                msg->msg_rtr_nid_param);
3298                 if (rc < 0) {
3299                         CNETERR("Error sending %s to %s: %d\n",
3300                                lnet_msgtyp2str(msg->msg_type),
3301                                libcfs_id2str(msg->msg_target), rc);
3302                         lnet_finalize(msg, rc);
3303                 }
3304         }
3305 }
3306
3307 /* The discovery thread. */
3308 static int lnet_peer_discovery(void *arg)
3309 {
3310         struct lnet_peer *lp;
3311         int rc;
3312
3313         wait_for_completion(&the_lnet.ln_started);
3314
3315         CDEBUG(D_NET, "started\n");
3316
3317         for (;;) {
3318                 if (lnet_peer_discovery_wait_for_work())
3319                         break;
3320
3321                 if (lnet_push_target_resize_needed())
3322                         lnet_push_target_resize();
3323                 else if (the_lnet.ln_push_target->pb_needs_post)
3324                         lnet_push_target_post(the_lnet.ln_push_target,
3325                                               &the_lnet.ln_push_target_md);
3326
3327                 lnet_resend_msgs();
3328
3329                 lnet_net_lock(LNET_LOCK_EX);
3330                 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING) {
3331                         lnet_net_unlock(LNET_LOCK_EX);
3332                         break;
3333                 }
3334
3335                 /*
3336                  * Process all incoming discovery work requests.  When
3337                  * discovery must wait on a peer to change state, it
3338                  * is added to the tail of the ln_dc_working queue. A
3339                  * timestamp keeps track of when the peer was added,
3340                  * so we can time out discovery requests that take too
3341                  * long.
3342                  */
3343                 while (!list_empty(&the_lnet.ln_dc_request)) {
3344                         lp = list_first_entry(&the_lnet.ln_dc_request,
3345                                               struct lnet_peer, lp_dc_list);
3346                         list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3347                         /*
3348                          * set the time the peer was put on the dc_working
3349                          * queue. It shouldn't remain on the queue
3350                          * forever, in case the GET message (for ping)
3351                          * doesn't get a REPLY or the PUT message (for
3352                          * push) doesn't get an ACK.
3353                          */
3354                         lp->lp_last_queued = ktime_get_real_seconds();
3355                         lnet_net_unlock(LNET_LOCK_EX);
3356
3357                         if (lnet_push_target_resize_needed())
3358                                 lnet_push_target_resize();
3359                         else if (the_lnet.ln_push_target->pb_needs_post)
3360                                 lnet_push_target_post(the_lnet.ln_push_target,
3361                                                       &the_lnet.ln_push_target_md);
3362
3363                         /*
3364                          * Select an action depending on the state of
3365                          * the peer and whether discovery is disabled.
3366                          * The check whether discovery is disabled is
3367                          * done after the code that handles processing
3368                          * for arrived data, cleanup for failures, and
3369                          * forcing a Ping or Push.
3370                          */
3371                         spin_lock(&lp->lp_lock);
3372                         CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3373                                 libcfs_nid2str(lp->lp_primary_nid), lp,
3374                                 lp->lp_state);
3375                         if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3376                                 rc = lnet_peer_data_present(lp);
3377                         else if (lp->lp_state & LNET_PEER_PING_FAILED)
3378                                 rc = lnet_peer_ping_failed(lp);
3379                         else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3380                                 rc = lnet_peer_push_failed(lp);
3381                         else if (lp->lp_state & LNET_PEER_FORCE_PING)
3382                                 rc = lnet_peer_send_ping(lp);
3383                         else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3384                                 rc = lnet_peer_send_push(lp);
3385                         else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3386                                 rc = lnet_peer_send_ping(lp);
3387                         else if (lnet_peer_needs_push(lp))
3388                                 rc = lnet_peer_send_push(lp);
3389                         else
3390                                 rc = lnet_peer_discovered(lp);
3391                         CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3392                                 libcfs_nid2str(lp->lp_primary_nid), lp,
3393                                 lp->lp_state, rc);
3394                         spin_unlock(&lp->lp_lock);
3395
3396                         lnet_net_lock(LNET_LOCK_EX);
3397                         if (rc == LNET_REDISCOVER_PEER) {
3398                                 list_move(&lp->lp_dc_list,
3399                                           &the_lnet.ln_dc_request);
3400                         } else if (rc) {
3401                                 lnet_peer_discovery_error(lp, rc);
3402                         }
3403                         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3404                                 lnet_peer_discovery_complete(lp);
3405                         if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3406                                 break;
3407
3408                         if (lp->lp_state & LNET_PEER_MARK_DELETION) {
3409                                 struct list_head rlist;
3410                                 struct lnet_route *route, *tmp;
3411                                 int sensitivity = lp->lp_health_sensitivity;
3412
3413                                 INIT_LIST_HEAD(&rlist);
3414
3415                                 /*
3416                                  * remove the peer from the discovery work
3417                                  * queue if it's on there in preparation
3418                                  * of deleting it.
3419                                  */
3420                                 if (!list_empty(&lp->lp_dc_list))
3421                                         list_del(&lp->lp_dc_list);
3422
3423                                 lnet_net_unlock(LNET_LOCK_EX);
3424
3425                                 mutex_lock(&the_lnet.ln_api_mutex);
3426
3427                                 lnet_net_lock(LNET_LOCK_EX);
3428                                 list_for_each_entry_safe(route, tmp,
3429                                                          &lp->lp_routes,
3430                                                          lr_gwlist)
3431                                         lnet_move_route(route, NULL, &rlist);
3432                                 lnet_net_unlock(LNET_LOCK_EX);
3433
3434                                 /* delete the peer */
3435                                 lnet_peer_del(lp);
3436
3437                                 list_for_each_entry_safe(route, tmp,
3438                                                          &rlist, lr_list) {
3439                                         /* re-add these routes */
3440                                         lnet_add_route(route->lr_net,
3441                                                        route->lr_hops,
3442                                                        route->lr_nid,
3443                                                        route->lr_priority,
3444                                                        sensitivity);
3445                                         LIBCFS_FREE(route, sizeof(*route));
3446                                 }
3447                                 mutex_unlock(&the_lnet.ln_api_mutex);
3448
3449                                 lnet_net_lock(LNET_LOCK_EX);
3450                         }
3451                 }
3452
3453                 lnet_net_unlock(LNET_LOCK_EX);
3454         }
3455
3456         CDEBUG(D_NET, "stopping\n");
3457         /*
3458          * Clean up before telling lnet_peer_discovery_stop() that
3459          * we're done. Use wake_up() below to somewhat reduce the
3460          * size of the thundering herd if there are multiple threads
3461          * waiting on discovery of a single peer.
3462          */
3463
3464         /* Queue cleanup 1: stop all pending pings and pushes. */
3465         lnet_net_lock(LNET_LOCK_EX);
3466         while (!list_empty(&the_lnet.ln_dc_working)) {
3467                 lp = list_first_entry(&the_lnet.ln_dc_working,
3468                                       struct lnet_peer, lp_dc_list);
3469                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3470                 lnet_net_unlock(LNET_LOCK_EX);
3471                 lnet_peer_cancel_discovery(lp);
3472                 lnet_net_lock(LNET_LOCK_EX);
3473         }
3474         lnet_net_unlock(LNET_LOCK_EX);
3475
3476         /* Queue cleanup 2: wait for the expired queue to clear. */
3477         while (!list_empty(&the_lnet.ln_dc_expired))
3478                 schedule_timeout_uninterruptible(cfs_time_seconds(1));
3479
3480         /* Queue cleanup 3: clear the request queue. */
3481         lnet_net_lock(LNET_LOCK_EX);
3482         while (!list_empty(&the_lnet.ln_dc_request)) {
3483                 lp = list_first_entry(&the_lnet.ln_dc_request,
3484                                       struct lnet_peer, lp_dc_list);
3485                 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3486                 lnet_peer_discovery_complete(lp);
3487         }
3488         lnet_net_unlock(LNET_LOCK_EX);
3489
3490         lnet_assert_handler_unused(the_lnet.ln_dc_handler);
3491         the_lnet.ln_dc_handler = NULL;
3492
3493         the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3494         wake_up(&the_lnet.ln_dc_waitq);
3495
3496         CDEBUG(D_NET, "stopped\n");
3497
3498         return 0;
3499 }
3500
3501 /* ln_api_mutex is held on entry. */
3502 int lnet_peer_discovery_start(void)
3503 {
3504         struct task_struct *task;
3505         int rc = 0;
3506
3507         if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3508                 return -EALREADY;
3509
3510         the_lnet.ln_dc_handler = lnet_discovery_event_handler;
3511         the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3512         task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3513         if (IS_ERR(task)) {
3514                 rc = PTR_ERR(task);
3515                 CERROR("Can't start peer discovery thread: %d\n", rc);
3516
3517                 the_lnet.ln_dc_handler = NULL;
3518
3519                 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3520         }
3521
3522         CDEBUG(D_NET, "discovery start: %d\n", rc);
3523
3524         return rc;
3525 }
3526
3527 /* ln_api_mutex is held on entry. */
3528 void lnet_peer_discovery_stop(void)
3529 {
3530         if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3531                 return;
3532
3533         LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3534         the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3535
3536         /* In the LNetNIInit() path we may be stopping discovery before it
3537          * entered its work loop
3538          */
3539         if (!completion_done(&the_lnet.ln_started))
3540                 complete(&the_lnet.ln_started);
3541         else
3542                 wake_up(&the_lnet.ln_dc_waitq);
3543
3544         wait_event(the_lnet.ln_dc_waitq,
3545                    the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3546
3547         LASSERT(list_empty(&the_lnet.ln_dc_request));
3548         LASSERT(list_empty(&the_lnet.ln_dc_working));
3549         LASSERT(list_empty(&the_lnet.ln_dc_expired));
3550
3551         CDEBUG(D_NET, "discovery stopped\n");
3552 }
3553
3554 /* Debugging */
3555
3556 void
3557 lnet_debug_peer(lnet_nid_t nid)
3558 {
3559         char                    *aliveness = "NA";
3560         struct lnet_peer_ni     *lp;
3561         int                     cpt;
3562
3563         cpt = lnet_cpt_of_nid(nid, NULL);
3564         lnet_net_lock(cpt);
3565
3566         lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3567         if (IS_ERR(lp)) {
3568                 lnet_net_unlock(cpt);
3569                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3570                 return;
3571         }
3572
3573         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3574                 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3575
3576         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3577                libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3578                aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3579                lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3580                lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3581
3582         lnet_peer_ni_decref_locked(lp);
3583
3584         lnet_net_unlock(cpt);
3585 }
3586
3587 /* Gathering information for userspace. */
3588
3589 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3590                           char aliveness[LNET_MAX_STR_LEN],
3591                           __u32 *cpt_iter, __u32 *refcount,
3592                           __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3593                           __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3594                           __u32 *peer_tx_qnob)
3595 {
3596         struct lnet_peer_table          *peer_table;
3597         struct lnet_peer_ni             *lp;
3598         int                             j;
3599         int                             lncpt;
3600         bool                            found = false;
3601
3602         /* get the number of CPTs */
3603         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3604
3605         /* if the cpt number to be examined is >= the number of cpts in
3606          * the system then indicate that there are no more cpts to examin
3607          */
3608         if (*cpt_iter >= lncpt)
3609                 return -ENOENT;
3610
3611         /* get the current table */
3612         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3613         /* if the ptable is NULL then there are no more cpts to examine */
3614         if (peer_table == NULL)
3615                 return -ENOENT;
3616
3617         lnet_net_lock(*cpt_iter);
3618
3619         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3620                 struct list_head *peers = &peer_table->pt_hash[j];
3621
3622                 list_for_each_entry(lp, peers, lpni_hashlist) {
3623                         if (peer_index-- > 0)
3624                                 continue;
3625
3626                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3627                         if (lnet_isrouter(lp) ||
3628                                 lnet_peer_aliveness_enabled(lp))
3629                                 snprintf(aliveness, LNET_MAX_STR_LEN,
3630                                          lnet_is_peer_ni_alive(lp) ? "up" : "down");
3631
3632                         *nid = lp->lpni_nid;
3633                         *refcount = atomic_read(&lp->lpni_refcount);
3634                         *ni_peer_tx_credits =
3635                                 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3636                         *peer_tx_credits = lp->lpni_txcredits;
3637                         *peer_rtr_credits = lp->lpni_rtrcredits;
3638                         *peer_min_rtr_credits = lp->lpni_mintxcredits;
3639                         *peer_tx_qnob = lp->lpni_txqnob;
3640
3641                         found = true;
3642                 }
3643
3644         }
3645         lnet_net_unlock(*cpt_iter);
3646
3647         *cpt_iter = lncpt;
3648
3649         return found ? 0 : -ENOENT;
3650 }
3651
3652 /* ln_api_mutex is held, which keeps the peer list stable */
3653 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3654 {
3655         struct lnet_ioctl_element_stats *lpni_stats;
3656         struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3657         struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3658         struct lnet_peer_ni_credit_info *lpni_info;
3659         struct lnet_peer_ni *lpni;
3660         struct lnet_peer *lp;
3661         lnet_nid_t nid;
3662         __u32 size;
3663         int rc;
3664
3665         lp = lnet_find_peer(cfg->prcfg_prim_nid);
3666
3667         if (!lp) {
3668                 rc = -ENOENT;
3669                 goto out;
3670         }
3671
3672         size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3673                 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3674         size *= lp->lp_nnis;
3675         if (size > cfg->prcfg_size) {
3676                 cfg->prcfg_size = size;
3677                 rc = -E2BIG;
3678                 goto out_lp_decref;
3679         }
3680
3681         cfg->prcfg_prim_nid = lp->lp_primary_nid;
3682         cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3683         cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3684         cfg->prcfg_count = lp->lp_nnis;
3685         cfg->prcfg_size = size;
3686         cfg->prcfg_state = lp->lp_state;
3687
3688         /* Allocate helper buffers. */
3689         rc = -ENOMEM;
3690         LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3691         if (!lpni_info)
3692                 goto out_lp_decref;
3693         LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3694         if (!lpni_stats)
3695                 goto out_free_info;
3696         LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3697         if (!lpni_msg_stats)
3698                 goto out_free_stats;
3699         LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3700         if (!lpni_hstats)
3701                 goto out_free_msg_stats;
3702
3703
3704         lpni = NULL;
3705         rc = -EFAULT;
3706         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3707                 nid = lpni->lpni_nid;
3708                 if (copy_to_user(bulk, &nid, sizeof(nid)))
3709                         goto out_free_hstats;
3710                 bulk += sizeof(nid);
3711
3712                 memset(lpni_info, 0, sizeof(*lpni_info));
3713                 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3714                 if (lnet_isrouter(lpni) ||
3715                         lnet_peer_aliveness_enabled(lpni))
3716                         snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3717                                 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3718
3719                 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3720                 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3721                         lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3722                 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3723                 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3724                 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3725                 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3726                 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3727                 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3728                         goto out_free_hstats;
3729                 bulk += sizeof(*lpni_info);
3730
3731                 memset(lpni_stats, 0, sizeof(*lpni_stats));
3732                 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3733                                                             LNET_STATS_TYPE_SEND);
3734                 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3735                                                             LNET_STATS_TYPE_RECV);
3736                 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3737                                                             LNET_STATS_TYPE_DROP);
3738                 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3739                         goto out_free_hstats;
3740                 bulk += sizeof(*lpni_stats);
3741                 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3742                 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3743                         goto out_free_hstats;
3744                 bulk += sizeof(*lpni_msg_stats);
3745                 lpni_hstats->hlpni_network_timeout =
3746                   atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3747                 lpni_hstats->hlpni_remote_dropped =
3748                   atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3749                 lpni_hstats->hlpni_remote_timeout =
3750                   atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3751                 lpni_hstats->hlpni_remote_error =
3752                   atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3753                 lpni_hstats->hlpni_health_value =
3754                   atomic_read(&lpni->lpni_healthv);
3755                 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3756                         goto out_free_hstats;
3757                 bulk += sizeof(*lpni_hstats);
3758         }
3759         rc = 0;
3760
3761 out_free_hstats:
3762         LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3763 out_free_msg_stats:
3764         LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3765 out_free_stats:
3766         LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3767 out_free_info:
3768         LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3769 out_lp_decref:
3770         lnet_peer_decref_locked(lp);
3771 out:
3772         return rc;
3773 }
3774
3775 void
3776 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3777 {
3778         /* the mt could've shutdown and cleaned up the queues */
3779         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3780                 return;
3781
3782         if (list_empty(&lpni->lpni_recovery) &&
3783             atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3784                 CDEBUG(D_NET, "lpni %s added to recovery queue. Health = %d\n",
3785                         libcfs_nid2str(lpni->lpni_nid),
3786                         atomic_read(&lpni->lpni_healthv));
3787                 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3788                 lnet_peer_ni_addref_locked(lpni);
3789         }
3790 }
3791
3792 /* Call with the ln_api_mutex held */
3793 void
3794 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3795 {
3796         struct lnet_peer_table *ptable;
3797         struct lnet_peer *lp;
3798         struct lnet_peer_net *lpn;
3799         struct lnet_peer_ni *lpni;
3800         int lncpt;
3801         int cpt;
3802
3803         if (the_lnet.ln_state != LNET_STATE_RUNNING)
3804                 return;
3805
3806         if (!all) {
3807                 lnet_net_lock(LNET_LOCK_EX);
3808                 lpni = lnet_find_peer_ni_locked(nid);
3809                 if (!lpni) {
3810                         lnet_net_unlock(LNET_LOCK_EX);
3811                         return;
3812                 }
3813                 atomic_set(&lpni->lpni_healthv, value);
3814                 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3815                 lnet_peer_ni_decref_locked(lpni);
3816                 lnet_net_unlock(LNET_LOCK_EX);
3817                 return;
3818         }
3819
3820         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3821
3822         /*
3823          * Walk all the peers and reset the healhv for each one to the
3824          * maximum value.
3825          */
3826         lnet_net_lock(LNET_LOCK_EX);
3827         for (cpt = 0; cpt < lncpt; cpt++) {
3828                 ptable = the_lnet.ln_peer_tables[cpt];
3829                 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3830                         list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3831                                 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3832                                                     lpni_peer_nis) {
3833                                         atomic_set(&lpni->lpni_healthv, value);
3834                                         lnet_peer_ni_add_to_recoveryq_locked(lpni);
3835                                 }
3836                         }
3837                 }
3838         }
3839         lnet_net_unlock(LNET_LOCK_EX);
3840 }
3841