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