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