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