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