Whamcloud - gitweb
01fcad3edc04d05a132c265e7fa4b295a166fcfc
[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         LASSERT(ptable->pt_number > 0);
404         ptable->pt_number--;
405
406         /*
407          * The peer_ni can no longer be found with a lookup. But there
408          * can be current users, so keep track of it on the zombie
409          * list until the reference count has gone to zero.
410          *
411          * The last reference may be lost in a place where the
412          * lnet_net_lock locks only a single cpt, and that cpt may not
413          * be lpni->lpni_cpt. So the zombie list of lnet_peer_table
414          * has its own lock.
415          */
416         spin_lock(&ptable->pt_zombie_lock);
417         list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
418         ptable->pt_zombies++;
419         spin_unlock(&ptable->pt_zombie_lock);
420
421         /* no need to keep this peer_ni on the hierarchy anymore */
422         lnet_peer_detach_peer_ni_locked(lpni);
423
424         /* remove hashlist reference on peer_ni */
425         lnet_peer_ni_decref_locked(lpni);
426
427         return 0;
428 }
429
430 void lnet_peer_uninit(void)
431 {
432         struct lnet_peer_ni *lpni, *tmp;
433
434         lnet_net_lock(LNET_LOCK_EX);
435
436         /* remove all peer_nis from the remote peer and the hash list */
437         list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
438                                  lpni_on_remote_peer_ni_list)
439                 lnet_peer_ni_del_locked(lpni, false);
440
441         lnet_peer_tables_destroy();
442
443         lnet_net_unlock(LNET_LOCK_EX);
444 }
445
446 static int
447 lnet_peer_del_locked(struct lnet_peer *peer)
448 {
449         struct lnet_peer_ni *lpni = NULL, *lpni2;
450         int rc = 0, rc2 = 0;
451
452         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(peer->lp_primary_nid));
453
454         lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
455         while (lpni != NULL) {
456                 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
457                 rc = lnet_peer_ni_del_locked(lpni, false);
458                 if (rc != 0)
459                         rc2 = rc;
460                 lpni = lpni2;
461         }
462
463         return rc2;
464 }
465
466 static int
467 lnet_peer_del(struct lnet_peer *peer)
468 {
469         lnet_net_lock(LNET_LOCK_EX);
470         lnet_peer_del_locked(peer);
471         lnet_net_unlock(LNET_LOCK_EX);
472
473         return 0;
474 }
475
476 /*
477  * Delete a NID from a peer. Call with ln_api_mutex held.
478  *
479  * Error codes:
480  *  -EPERM:  Non-DLC deletion from DLC-configured peer.
481  *  -ENOENT: No lnet_peer_ni corresponding to the nid.
482  *  -ECHILD: The lnet_peer_ni isn't connected to the peer.
483  *  -EBUSY:  The lnet_peer_ni is the primary, and not the only peer_ni.
484  */
485 static int
486 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid, unsigned flags)
487 {
488         struct lnet_peer_ni *lpni;
489         lnet_nid_t primary_nid = lp->lp_primary_nid;
490         int rc = 0;
491         bool force = (flags & LNET_PEER_RTR_NI_FORCE_DEL) ? true : false;
492
493         if (!(flags & LNET_PEER_CONFIGURED)) {
494                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
495                         rc = -EPERM;
496                         goto out;
497                 }
498         }
499         lpni = lnet_find_peer_ni_locked(nid);
500         if (!lpni) {
501                 rc = -ENOENT;
502                 goto out;
503         }
504         lnet_peer_ni_decref_locked(lpni);
505         if (lp != lpni->lpni_peer_net->lpn_peer) {
506                 rc = -ECHILD;
507                 goto out;
508         }
509
510         /*
511          * This function only allows deletion of the primary NID if it
512          * is the only NID.
513          */
514         if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && !force) {
515                 rc = -EBUSY;
516                 goto out;
517         }
518
519         lnet_net_lock(LNET_LOCK_EX);
520
521         if (nid == lp->lp_primary_nid && lp->lp_nnis != 1 && force) {
522                 struct lnet_peer_ni *lpni2;
523                 /* assign the next peer_ni to be the primary */
524                 lpni2 = lnet_get_next_peer_ni_locked(lp, NULL, lpni);
525                 LASSERT(lpni2);
526                 lp->lp_primary_nid = lpni->lpni_nid;
527         }
528         rc = lnet_peer_ni_del_locked(lpni, force);
529
530         lnet_net_unlock(LNET_LOCK_EX);
531
532 out:
533         CDEBUG(D_NET, "peer %s NID %s flags %#x: %d\n",
534                libcfs_nid2str(primary_nid), libcfs_nid2str(nid), flags, rc);
535
536         return rc;
537 }
538
539 static void
540 lnet_peer_table_cleanup_locked(struct lnet_net *net,
541                                struct lnet_peer_table *ptable)
542 {
543         int                      i;
544         struct lnet_peer_ni     *next;
545         struct lnet_peer_ni     *lpni;
546         struct lnet_peer        *peer;
547
548         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
549                 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
550                                          lpni_hashlist) {
551                         if (net != NULL && net != lpni->lpni_net)
552                                 continue;
553
554                         peer = lpni->lpni_peer_net->lpn_peer;
555                         if (peer->lp_primary_nid != lpni->lpni_nid) {
556                                 lnet_peer_ni_del_locked(lpni, false);
557                                 continue;
558                         }
559                         /*
560                          * Removing the primary NID implies removing
561                          * the entire peer. Advance next beyond any
562                          * peer_ni that belongs to the same peer.
563                          */
564                         list_for_each_entry_from(next, &ptable->pt_hash[i],
565                                                  lpni_hashlist) {
566                                 if (next->lpni_peer_net->lpn_peer != peer)
567                                         break;
568                         }
569                         lnet_peer_del_locked(peer);
570                 }
571         }
572 }
573
574 static void
575 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
576 {
577         int     i = 3;
578
579         spin_lock(&ptable->pt_zombie_lock);
580         while (ptable->pt_zombies) {
581                 spin_unlock(&ptable->pt_zombie_lock);
582
583                 if (is_power_of_2(i)) {
584                         CDEBUG(D_WARNING,
585                                "Waiting for %d zombies on peer table\n",
586                                ptable->pt_zombies);
587                 }
588                 set_current_state(TASK_UNINTERRUPTIBLE);
589                 schedule_timeout(cfs_time_seconds(1) >> 1);
590                 spin_lock(&ptable->pt_zombie_lock);
591         }
592         spin_unlock(&ptable->pt_zombie_lock);
593 }
594
595 static void
596 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
597                                 struct lnet_peer_table *ptable)
598 {
599         struct lnet_peer_ni     *lp;
600         struct lnet_peer_ni     *tmp;
601         lnet_nid_t              gw_nid;
602         int                     i;
603
604         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
605                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
606                                          lpni_hashlist) {
607                         if (net != lp->lpni_net)
608                                 continue;
609
610                         if (!lnet_isrouter(lp))
611                                 continue;
612
613                         gw_nid = lp->lpni_peer_net->lpn_peer->lp_primary_nid;
614
615                         lnet_net_unlock(LNET_LOCK_EX);
616                         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), gw_nid);
617                         lnet_net_lock(LNET_LOCK_EX);
618                 }
619         }
620 }
621
622 void
623 lnet_peer_tables_cleanup(struct lnet_net *net)
624 {
625         int i;
626         struct lnet_peer_table *ptable;
627
628         LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
629         /* If just deleting the peers for a NI, get rid of any routes these
630          * peers are gateways for. */
631         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
632                 lnet_net_lock(LNET_LOCK_EX);
633                 lnet_peer_table_del_rtrs_locked(net, ptable);
634                 lnet_net_unlock(LNET_LOCK_EX);
635         }
636
637         /* Start the cleanup process */
638         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
639                 lnet_net_lock(LNET_LOCK_EX);
640                 lnet_peer_table_cleanup_locked(net, ptable);
641                 lnet_net_unlock(LNET_LOCK_EX);
642         }
643
644         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
645                 lnet_peer_ni_finalize_wait(ptable);
646 }
647
648 static struct lnet_peer_ni *
649 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
650 {
651         struct list_head        *peers;
652         struct lnet_peer_ni     *lp;
653
654         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
655
656         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
657         list_for_each_entry(lp, peers, lpni_hashlist) {
658                 if (lp->lpni_nid == nid) {
659                         lnet_peer_ni_addref_locked(lp);
660                         return lp;
661                 }
662         }
663
664         return NULL;
665 }
666
667 struct lnet_peer_ni *
668 lnet_find_peer_ni_locked(lnet_nid_t nid)
669 {
670         struct lnet_peer_ni *lpni;
671         struct lnet_peer_table *ptable;
672         int cpt;
673
674         cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
675
676         ptable = the_lnet.ln_peer_tables[cpt];
677         lpni = lnet_get_peer_ni_locked(ptable, nid);
678
679         return lpni;
680 }
681
682 struct lnet_peer_ni *
683 lnet_peer_get_ni_locked(struct lnet_peer *lp, lnet_nid_t nid)
684 {
685         struct lnet_peer_net *lpn;
686         struct lnet_peer_ni *lpni;
687
688         lpn = lnet_peer_get_net_locked(lp, LNET_NIDNET(nid));
689         if (!lpn)
690                 return NULL;
691
692         list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
693                 if (lpni->lpni_nid == nid)
694                         return lpni;
695         }
696
697         return NULL;
698 }
699
700 struct lnet_peer *
701 lnet_find_peer(lnet_nid_t nid)
702 {
703         struct lnet_peer_ni *lpni;
704         struct lnet_peer *lp = NULL;
705         int cpt;
706
707         cpt = lnet_net_lock_current();
708         lpni = lnet_find_peer_ni_locked(nid);
709         if (lpni) {
710                 lp = lpni->lpni_peer_net->lpn_peer;
711                 lnet_peer_addref_locked(lp);
712                 lnet_peer_ni_decref_locked(lpni);
713         }
714         lnet_net_unlock(cpt);
715
716         return lp;
717 }
718
719 struct lnet_peer_net *
720 lnet_get_next_peer_net_locked(struct lnet_peer *lp, __u32 prev_lpn_id)
721 {
722         struct lnet_peer_net *net;
723
724         if (!prev_lpn_id) {
725                 /* no net id provided return the first net */
726                 net = list_first_entry_or_null(&lp->lp_peer_nets,
727                                                struct lnet_peer_net,
728                                                lpn_peer_nets);
729
730                 return net;
731         }
732
733         /* find the net after the one provided */
734         list_for_each_entry(net, &lp->lp_peer_nets, lpn_peer_nets) {
735                 if (net->lpn_net_id == prev_lpn_id) {
736                         /*
737                          * if we reached the end of the list loop to the
738                          * beginning.
739                          */
740                         if (net->lpn_peer_nets.next == &lp->lp_peer_nets)
741                                 return list_first_entry_or_null(&lp->lp_peer_nets,
742                                                                 struct lnet_peer_net,
743                                                                 lpn_peer_nets);
744                         else
745                                 return list_next_entry(net, lpn_peer_nets);
746                 }
747         }
748
749         return NULL;
750 }
751
752 struct lnet_peer_ni *
753 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
754                              struct lnet_peer_net *peer_net,
755                              struct lnet_peer_ni *prev)
756 {
757         struct lnet_peer_ni *lpni;
758         struct lnet_peer_net *net = peer_net;
759
760         if (!prev) {
761                 if (!net) {
762                         if (list_empty(&peer->lp_peer_nets))
763                                 return NULL;
764
765                         net = list_entry(peer->lp_peer_nets.next,
766                                          struct lnet_peer_net,
767                                          lpn_peer_nets);
768                 }
769                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
770                                   lpni_peer_nis);
771
772                 return lpni;
773         }
774
775         if (prev->lpni_peer_nis.next == &prev->lpni_peer_net->lpn_peer_nis) {
776                 /*
777                  * if you reached the end of the peer ni list and the peer
778                  * net is specified then there are no more peer nis in that
779                  * net.
780                  */
781                 if (net)
782                         return NULL;
783
784                 /*
785                  * we reached the end of this net ni list. move to the
786                  * next net
787                  */
788                 if (prev->lpni_peer_net->lpn_peer_nets.next ==
789                     &peer->lp_peer_nets)
790                         /* no more nets and no more NIs. */
791                         return NULL;
792
793                 /* get the next net */
794                 net = list_entry(prev->lpni_peer_net->lpn_peer_nets.next,
795                                  struct lnet_peer_net,
796                                  lpn_peer_nets);
797                 /* get the ni on it */
798                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
799                                   lpni_peer_nis);
800
801                 return lpni;
802         }
803
804         /* there are more nis left */
805         lpni = list_entry(prev->lpni_peer_nis.next,
806                           struct lnet_peer_ni, lpni_peer_nis);
807
808         return lpni;
809 }
810
811 /* Call with the ln_api_mutex held */
812 int lnet_get_peer_list(u32 *countp, u32 *sizep, struct lnet_process_id __user *ids)
813 {
814         struct lnet_process_id id;
815         struct lnet_peer_table *ptable;
816         struct lnet_peer *lp;
817         __u32 count = 0;
818         __u32 size = 0;
819         int lncpt;
820         int cpt;
821         __u32 i;
822         int rc;
823
824         rc = -ESHUTDOWN;
825         if (the_lnet.ln_state != LNET_STATE_RUNNING)
826                 goto done;
827
828         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
829
830         /*
831          * Count the number of peers, and return E2BIG if the buffer
832          * is too small. We'll also return the desired size.
833          */
834         rc = -E2BIG;
835         for (cpt = 0; cpt < lncpt; cpt++) {
836                 ptable = the_lnet.ln_peer_tables[cpt];
837                 count += ptable->pt_peers;
838         }
839         size = count * sizeof(*ids);
840         if (size > *sizep)
841                 goto done;
842
843         /*
844          * Walk the peer lists and copy out the primary nids.
845          * This is safe because the peer lists are only modified
846          * while the ln_api_mutex is held. So we don't need to
847          * hold the lnet_net_lock as well, and can therefore
848          * directly call copy_to_user().
849          */
850         rc = -EFAULT;
851         memset(&id, 0, sizeof(id));
852         id.pid = LNET_PID_LUSTRE;
853         i = 0;
854         for (cpt = 0; cpt < lncpt; cpt++) {
855                 ptable = the_lnet.ln_peer_tables[cpt];
856                 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
857                         if (i >= count)
858                                 goto done;
859                         id.nid = lp->lp_primary_nid;
860                         if (copy_to_user(&ids[i], &id, sizeof(id)))
861                                 goto done;
862                         i++;
863                 }
864         }
865         rc = 0;
866 done:
867         *countp = count;
868         *sizep = size;
869         return rc;
870 }
871
872 /*
873  * Start pushes to peers that need to be updated for a configuration
874  * change on this node.
875  */
876 void
877 lnet_push_update_to_peers(int force)
878 {
879         struct lnet_peer_table *ptable;
880         struct lnet_peer *lp;
881         int lncpt;
882         int cpt;
883
884         lnet_net_lock(LNET_LOCK_EX);
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 {
1154         if (lnet_peer_discovery_disabled)
1155                 return true;
1156
1157         if (!(lp->lp_state & LNET_PEER_MULTI_RAIL) ||
1158             (lp->lp_state & LNET_PEER_NO_DISCOVERY)) {
1159                 return true;
1160         }
1161
1162         return false;
1163 }
1164
1165 /*
1166  * Peer Discovery
1167  */
1168 bool
1169 lnet_is_discovery_disabled(struct lnet_peer *lp)
1170 {
1171         bool rc = false;
1172
1173         spin_lock(&lp->lp_lock);
1174         rc = lnet_is_discovery_disabled_locked(lp);
1175         spin_unlock(&lp->lp_lock);
1176
1177         return rc;
1178 }
1179
1180 lnet_nid_t
1181 LNetPrimaryNID(lnet_nid_t nid)
1182 {
1183         struct lnet_peer *lp;
1184         struct lnet_peer_ni *lpni;
1185         lnet_nid_t primary_nid = nid;
1186         int rc = 0;
1187         int cpt;
1188
1189         cpt = lnet_net_lock_current();
1190         lpni = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
1191         if (IS_ERR(lpni)) {
1192                 rc = PTR_ERR(lpni);
1193                 goto out_unlock;
1194         }
1195         lp = lpni->lpni_peer_net->lpn_peer;
1196
1197         while (!lnet_peer_is_uptodate(lp)) {
1198                 rc = lnet_discover_peer_locked(lpni, cpt, true);
1199                 if (rc)
1200                         goto out_decref;
1201                 lp = lpni->lpni_peer_net->lpn_peer;
1202
1203                 /* Only try once if discovery is disabled */
1204                 if (lnet_is_discovery_disabled(lp))
1205                         break;
1206         }
1207         primary_nid = lp->lp_primary_nid;
1208 out_decref:
1209         lnet_peer_ni_decref_locked(lpni);
1210 out_unlock:
1211         lnet_net_unlock(cpt);
1212
1213         CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
1214                libcfs_nid2str(primary_nid), rc);
1215         return primary_nid;
1216 }
1217 EXPORT_SYMBOL(LNetPrimaryNID);
1218
1219 struct lnet_peer_net *
1220 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
1221 {
1222         struct lnet_peer_net *peer_net;
1223         list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_peer_nets) {
1224                 if (peer_net->lpn_net_id == net_id)
1225                         return peer_net;
1226         }
1227         return NULL;
1228 }
1229
1230 /*
1231  * Attach a peer_ni to a peer_net and peer. This function assumes
1232  * peer_ni is not already attached to the peer_net/peer. The peer_ni
1233  * may be attached to a different peer, in which case it will be
1234  * properly detached first. The whole operation is done atomically.
1235  *
1236  * Always returns 0.  This is the last function called from functions
1237  * that do return an int, so returning 0 here allows the compiler to
1238  * do a tail call.
1239  */
1240 static int
1241 lnet_peer_attach_peer_ni(struct lnet_peer *lp,
1242                                 struct lnet_peer_net *lpn,
1243                                 struct lnet_peer_ni *lpni,
1244                                 unsigned flags)
1245 {
1246         struct lnet_peer_table *ptable;
1247
1248         /* Install the new peer_ni */
1249         lnet_net_lock(LNET_LOCK_EX);
1250         /* Add peer_ni to global peer table hash, if necessary. */
1251         if (list_empty(&lpni->lpni_hashlist)) {
1252                 int hash = lnet_nid2peerhash(lpni->lpni_nid);
1253
1254                 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1255                 list_add_tail(&lpni->lpni_hashlist, &ptable->pt_hash[hash]);
1256                 ptable->pt_version++;
1257                 ptable->pt_number++;
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 /*
1846  * Is a peer uptodate from the point of view of discovery?
1847  *
1848  * If it is currently being processed, obviously not.
1849  * A forced Ping or Push is also handled by the discovery thread.
1850  *
1851  * Otherwise look at whether the peer needs rediscovering.
1852  */
1853 bool
1854 lnet_peer_is_uptodate(struct lnet_peer *lp)
1855 {
1856         bool rc;
1857
1858         spin_lock(&lp->lp_lock);
1859         if (lp->lp_state & (LNET_PEER_DISCOVERING |
1860                             LNET_PEER_FORCE_PING |
1861                             LNET_PEER_FORCE_PUSH)) {
1862                 rc = false;
1863         } else if (lp->lp_state & LNET_PEER_REDISCOVER) {
1864                 rc = false;
1865         } else if (lnet_peer_needs_push(lp)) {
1866                 rc = false;
1867         } else if (lp->lp_state & LNET_PEER_DISCOVERED) {
1868                 if (lp->lp_state & LNET_PEER_NIDS_UPTODATE)
1869                         rc = true;
1870                 else
1871                         rc = false;
1872         } else {
1873                 rc = false;
1874         }
1875         spin_unlock(&lp->lp_lock);
1876
1877         return rc;
1878 }
1879
1880 /*
1881  * Queue a peer for the attention of the discovery thread.  Call with
1882  * lnet_net_lock/EX held. Returns 0 if the peer was queued, and
1883  * -EALREADY if the peer was already queued.
1884  */
1885 static int lnet_peer_queue_for_discovery(struct lnet_peer *lp)
1886 {
1887         int rc;
1888
1889         spin_lock(&lp->lp_lock);
1890         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
1891                 lp->lp_state |= LNET_PEER_DISCOVERING;
1892         spin_unlock(&lp->lp_lock);
1893         if (list_empty(&lp->lp_dc_list)) {
1894                 lnet_peer_addref_locked(lp);
1895                 list_add_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
1896                 wake_up(&the_lnet.ln_dc_waitq);
1897                 rc = 0;
1898         } else {
1899                 rc = -EALREADY;
1900         }
1901
1902         CDEBUG(D_NET, "Queue peer %s: %d\n",
1903                libcfs_nid2str(lp->lp_primary_nid), rc);
1904
1905         return rc;
1906 }
1907
1908 /*
1909  * Discovery of a peer is complete. Wake all waiters on the peer.
1910  * Call with lnet_net_lock/EX held.
1911  */
1912 static void lnet_peer_discovery_complete(struct lnet_peer *lp)
1913 {
1914         struct lnet_msg *msg, *tmp;
1915         int rc = 0;
1916         struct list_head pending_msgs;
1917
1918         INIT_LIST_HEAD(&pending_msgs);
1919
1920         CDEBUG(D_NET, "Discovery complete. Dequeue peer %s\n",
1921                libcfs_nid2str(lp->lp_primary_nid));
1922
1923         list_del_init(&lp->lp_dc_list);
1924         spin_lock(&lp->lp_lock);
1925         list_splice_init(&lp->lp_dc_pendq, &pending_msgs);
1926         spin_unlock(&lp->lp_lock);
1927         wake_up_all(&lp->lp_dc_waitq);
1928
1929         if (lp->lp_rtr_refcount > 0)
1930                 lnet_router_discovery_complete(lp);
1931
1932         lnet_net_unlock(LNET_LOCK_EX);
1933
1934         /* iterate through all pending messages and send them again */
1935         list_for_each_entry_safe(msg, tmp, &pending_msgs, msg_list) {
1936                 list_del_init(&msg->msg_list);
1937                 if (lp->lp_dc_error) {
1938                         lnet_finalize(msg, lp->lp_dc_error);
1939                         continue;
1940                 }
1941
1942                 CDEBUG(D_NET, "sending pending message %s to target %s\n",
1943                        lnet_msgtyp2str(msg->msg_type),
1944                        libcfs_id2str(msg->msg_target));
1945                 rc = lnet_send(msg->msg_src_nid_param, msg,
1946                                msg->msg_rtr_nid_param);
1947                 if (rc < 0) {
1948                         CNETERR("Error sending %s to %s: %d\n",
1949                                lnet_msgtyp2str(msg->msg_type),
1950                                libcfs_id2str(msg->msg_target), rc);
1951                         lnet_finalize(msg, rc);
1952                 }
1953         }
1954         lnet_net_lock(LNET_LOCK_EX);
1955         lnet_peer_decref_locked(lp);
1956 }
1957
1958 /*
1959  * Handle inbound push.
1960  * Like any event handler, called with lnet_res_lock/CPT held.
1961  */
1962 void lnet_peer_push_event(struct lnet_event *ev)
1963 {
1964         struct lnet_ping_buffer *pbuf = ev->md.user_ptr;
1965         struct lnet_peer *lp;
1966
1967         /* lnet_find_peer() adds a refcount */
1968         lp = lnet_find_peer(ev->source.nid);
1969         if (!lp) {
1970                 CDEBUG(D_NET, "Push Put from unknown %s (source %s). Ignoring...\n",
1971                        libcfs_nid2str(ev->initiator.nid),
1972                        libcfs_nid2str(ev->source.nid));
1973                 return;
1974         }
1975
1976         /* Ensure peer state remains consistent while we modify it. */
1977         spin_lock(&lp->lp_lock);
1978
1979         /*
1980          * If some kind of error happened the contents of the message
1981          * cannot be used. Clear the NIDS_UPTODATE and set the
1982          * FORCE_PING flag to trigger a ping.
1983          */
1984         if (ev->status) {
1985                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
1986                 lp->lp_state |= LNET_PEER_FORCE_PING;
1987                 CDEBUG(D_NET, "Push Put error %d from %s (source %s)\n",
1988                        ev->status,
1989                        libcfs_nid2str(lp->lp_primary_nid),
1990                        libcfs_nid2str(ev->source.nid));
1991                 goto out;
1992         }
1993
1994         /*
1995          * A push with invalid or corrupted info. Clear the UPTODATE
1996          * flag to trigger a ping.
1997          */
1998         if (lnet_ping_info_validate(&pbuf->pb_info)) {
1999                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2000                 lp->lp_state |= LNET_PEER_FORCE_PING;
2001                 CDEBUG(D_NET, "Corrupted Push from %s\n",
2002                        libcfs_nid2str(lp->lp_primary_nid));
2003                 goto out;
2004         }
2005
2006         /*
2007          * Make sure we'll allocate the correct size ping buffer when
2008          * pinging the peer.
2009          */
2010         if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2011                 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2012
2013         /*
2014          * A non-Multi-Rail peer is not supposed to be capable of
2015          * sending a push.
2016          */
2017         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)) {
2018                 CERROR("Push from non-Multi-Rail peer %s dropped\n",
2019                        libcfs_nid2str(lp->lp_primary_nid));
2020                 goto out;
2021         }
2022
2023         /*
2024          * Check the MULTIRAIL flag. Complain if the peer was DLC
2025          * configured without it.
2026          */
2027         if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
2028                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2029                         CERROR("Push says %s is Multi-Rail, DLC says not\n",
2030                                libcfs_nid2str(lp->lp_primary_nid));
2031                 } else {
2032                         lp->lp_state |= LNET_PEER_MULTI_RAIL;
2033                         lnet_peer_clr_non_mr_pref_nids(lp);
2034                 }
2035         }
2036
2037         /*
2038          * The peer may have discovery disabled at its end. Set
2039          * NO_DISCOVERY as appropriate.
2040          */
2041         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY)) {
2042                 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2043                        libcfs_nid2str(lp->lp_primary_nid));
2044                 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2045         } else if (lp->lp_state & LNET_PEER_NO_DISCOVERY) {
2046                 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2047                        libcfs_nid2str(lp->lp_primary_nid));
2048                 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2049         }
2050
2051         /*
2052          * Check for truncation of the Put message. Clear the
2053          * NIDS_UPTODATE flag and set FORCE_PING to trigger a ping,
2054          * and tell discovery to allocate a bigger buffer.
2055          */
2056         if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2057                 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2058                         the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2059                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2060                 lp->lp_state |= LNET_PEER_FORCE_PING;
2061                 CDEBUG(D_NET, "Truncated Push from %s (%d nids)\n",
2062                        libcfs_nid2str(lp->lp_primary_nid),
2063                        pbuf->pb_info.pi_nnis);
2064                 goto out;
2065         }
2066
2067         /* always assume new data */
2068         lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2069         lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2070
2071         /*
2072          * If there is data present that hasn't been processed yet,
2073          * we'll replace it if the Put contained newer data and it
2074          * fits. We're racing with a Ping or earlier Push in this
2075          * case.
2076          */
2077         if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2078                 if (LNET_PING_BUFFER_SEQNO(pbuf) >
2079                         LNET_PING_BUFFER_SEQNO(lp->lp_data) &&
2080                     pbuf->pb_info.pi_nnis <= lp->lp_data->pb_nnis) {
2081                         memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2082                                LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2083                         CDEBUG(D_NET, "Ping/Push race from %s: %u vs %u\n",
2084                               libcfs_nid2str(lp->lp_primary_nid),
2085                               LNET_PING_BUFFER_SEQNO(pbuf),
2086                               LNET_PING_BUFFER_SEQNO(lp->lp_data));
2087                 }
2088                 goto out;
2089         }
2090
2091         /*
2092          * Allocate a buffer to copy the data. On a failure we drop
2093          * the Push and set FORCE_PING to force the discovery
2094          * thread to fix the problem by pinging the peer.
2095          */
2096         lp->lp_data = lnet_ping_buffer_alloc(lp->lp_data_nnis, GFP_ATOMIC);
2097         if (!lp->lp_data) {
2098                 lp->lp_state |= LNET_PEER_FORCE_PING;
2099                 CDEBUG(D_NET, "Cannot allocate Push buffer for %s %u\n",
2100                        libcfs_nid2str(lp->lp_primary_nid),
2101                        LNET_PING_BUFFER_SEQNO(pbuf));
2102                 goto out;
2103         }
2104
2105         /* Success */
2106         memcpy(&lp->lp_data->pb_info, &pbuf->pb_info,
2107                LNET_PING_INFO_SIZE(pbuf->pb_info.pi_nnis));
2108         lp->lp_state |= LNET_PEER_DATA_PRESENT;
2109         CDEBUG(D_NET, "Received Push %s %u\n",
2110                libcfs_nid2str(lp->lp_primary_nid),
2111                LNET_PING_BUFFER_SEQNO(pbuf));
2112
2113 out:
2114         /*
2115          * Queue the peer for discovery if not done, force it on the request
2116          * queue and wake the discovery thread if the peer was already queued,
2117          * because its status changed.
2118          */
2119         spin_unlock(&lp->lp_lock);
2120         lnet_net_lock(LNET_LOCK_EX);
2121         if (!lnet_peer_is_uptodate(lp) && lnet_peer_queue_for_discovery(lp)) {
2122                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2123                 wake_up(&the_lnet.ln_dc_waitq);
2124         }
2125         /* Drop refcount from lookup */
2126         lnet_peer_decref_locked(lp);
2127         lnet_net_unlock(LNET_LOCK_EX);
2128 }
2129
2130 /*
2131  * Clear the discovery error state, unless we're already discovering
2132  * this peer, in which case the error is current.
2133  */
2134 static void lnet_peer_clear_discovery_error(struct lnet_peer *lp)
2135 {
2136         spin_lock(&lp->lp_lock);
2137         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
2138                 lp->lp_dc_error = 0;
2139         spin_unlock(&lp->lp_lock);
2140 }
2141
2142 /*
2143  * Peer discovery slow path. The ln_api_mutex is held on entry, and
2144  * dropped/retaken within this function. An lnet_peer_ni is passed in
2145  * because discovery could tear down an lnet_peer.
2146  */
2147 int
2148 lnet_discover_peer_locked(struct lnet_peer_ni *lpni, int cpt, bool block)
2149 {
2150         DEFINE_WAIT(wait);
2151         struct lnet_peer *lp;
2152         int rc = 0;
2153
2154 again:
2155         lnet_net_unlock(cpt);
2156         lnet_net_lock(LNET_LOCK_EX);
2157         lp = lpni->lpni_peer_net->lpn_peer;
2158         lnet_peer_clear_discovery_error(lp);
2159
2160         /*
2161          * We're willing to be interrupted. The lpni can become a
2162          * zombie if we race with DLC, so we must check for that.
2163          */
2164         for (;;) {
2165                 /* Keep lp alive when the lnet_net_lock is unlocked */
2166                 lnet_peer_addref_locked(lp);
2167                 prepare_to_wait(&lp->lp_dc_waitq, &wait, TASK_INTERRUPTIBLE);
2168                 if (signal_pending(current))
2169                         break;
2170                 if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2171                         break;
2172                 if (lp->lp_dc_error)
2173                         break;
2174                 if (lnet_peer_is_uptodate(lp))
2175                         break;
2176                 lnet_peer_queue_for_discovery(lp);
2177
2178                 /*
2179                  * If caller requested a non-blocking operation then
2180                  * return immediately. Once discovery is complete any
2181                  * pending messages that were stopped due to discovery
2182                  * will be transmitted.
2183                  */
2184                 if (!block)
2185                         break;
2186
2187                 lnet_net_unlock(LNET_LOCK_EX);
2188                 schedule();
2189                 finish_wait(&lp->lp_dc_waitq, &wait);
2190                 lnet_net_lock(LNET_LOCK_EX);
2191                 lnet_peer_decref_locked(lp);
2192                 /* Peer may have changed */
2193                 lp = lpni->lpni_peer_net->lpn_peer;
2194
2195                 /*
2196                  * Wait for discovery to complete, but don't repeat if
2197                  * discovery is disabled. This is done to ensure we can
2198                  * use discovery as a standard ping as well for backwards
2199                  * compatibility with routers which do not have discovery
2200                  * or have discovery disabled
2201                  */
2202                 if (lnet_is_discovery_disabled(lp))
2203                         break;
2204         }
2205         finish_wait(&lp->lp_dc_waitq, &wait);
2206
2207         lnet_net_unlock(LNET_LOCK_EX);
2208         lnet_net_lock(cpt);
2209         lnet_peer_decref_locked(lp);
2210         /*
2211          * The peer may have changed, so re-check and rediscover if that turns
2212          * out to have been the case. The reference count on lp ensured that
2213          * even if it was unlinked from lpni the memory could not be recycled.
2214          * Thus the check below is sufficient to determine whether the peer
2215          * changed. If the peer changed, then lp must not be dereferenced.
2216          */
2217         if (lp != lpni->lpni_peer_net->lpn_peer)
2218                 goto again;
2219
2220         if (signal_pending(current))
2221                 rc = -EINTR;
2222         else if (the_lnet.ln_dc_state != LNET_DC_STATE_RUNNING)
2223                 rc = -ESHUTDOWN;
2224         else if (lp->lp_dc_error)
2225                 rc = lp->lp_dc_error;
2226         else if (!block)
2227                 CDEBUG(D_NET, "non-blocking discovery\n");
2228         else if (!lnet_peer_is_uptodate(lp) && !lnet_is_discovery_disabled(lp))
2229                 goto again;
2230
2231         CDEBUG(D_NET, "peer %s NID %s: %d. %s\n",
2232                (lp ? libcfs_nid2str(lp->lp_primary_nid) : "(none)"),
2233                libcfs_nid2str(lpni->lpni_nid), rc,
2234                (!block) ? "pending discovery" : "discovery complete");
2235
2236         return rc;
2237 }
2238
2239 /* Handle an incoming ack for a push. */
2240 static void
2241 lnet_discovery_event_ack(struct lnet_peer *lp, struct lnet_event *ev)
2242 {
2243         struct lnet_ping_buffer *pbuf;
2244
2245         pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2246         spin_lock(&lp->lp_lock);
2247         lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2248         lp->lp_push_error = ev->status;
2249         if (ev->status)
2250                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2251         else
2252                 lp->lp_node_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2253         spin_unlock(&lp->lp_lock);
2254
2255         CDEBUG(D_NET, "peer %s ev->status %d\n",
2256                libcfs_nid2str(lp->lp_primary_nid), ev->status);
2257 }
2258
2259 /* Handle a Reply message. This is the reply to a Ping message. */
2260 static void
2261 lnet_discovery_event_reply(struct lnet_peer *lp, struct lnet_event *ev)
2262 {
2263         struct lnet_ping_buffer *pbuf;
2264         int rc;
2265
2266         spin_lock(&lp->lp_lock);
2267
2268         /*
2269          * If some kind of error happened the contents of message
2270          * cannot be used. Set PING_FAILED to trigger a retry.
2271          */
2272         if (ev->status) {
2273                 lp->lp_state |= LNET_PEER_PING_FAILED;
2274                 lp->lp_ping_error = ev->status;
2275                 CDEBUG(D_NET, "Ping Reply error %d from %s (source %s)\n",
2276                        ev->status,
2277                        libcfs_nid2str(lp->lp_primary_nid),
2278                        libcfs_nid2str(ev->source.nid));
2279                 goto out;
2280         }
2281
2282         pbuf = LNET_PING_INFO_TO_BUFFER(ev->md.start);
2283         if (pbuf->pb_info.pi_magic == __swab32(LNET_PROTO_PING_MAGIC))
2284                 lnet_swap_pinginfo(pbuf);
2285
2286         /*
2287          * A reply with invalid or corrupted info. Set PING_FAILED to
2288          * trigger a retry.
2289          */
2290         rc = lnet_ping_info_validate(&pbuf->pb_info);
2291         if (rc) {
2292                 lp->lp_state |= LNET_PEER_PING_FAILED;
2293                 lp->lp_ping_error = 0;
2294                 CDEBUG(D_NET, "Corrupted Ping Reply from %s: %d\n",
2295                        libcfs_nid2str(lp->lp_primary_nid), rc);
2296                 goto out;
2297         }
2298
2299
2300         /*
2301          * Only enable the multi-rail feature on the peer if both sides of
2302          * the connection have discovery on
2303          */
2304         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2305                 CDEBUG(D_NET, "Peer %s has Multi-Rail feature enabled\n",
2306                        libcfs_nid2str(lp->lp_primary_nid));
2307                 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2308         } else {
2309                 CDEBUG(D_NET, "Peer %s has Multi-Rail feature disabled\n",
2310                        libcfs_nid2str(lp->lp_primary_nid));
2311                 lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2312         }
2313
2314         /*
2315          * The peer may have discovery disabled at its end. Set
2316          * NO_DISCOVERY as appropriate.
2317          */
2318         if ((pbuf->pb_info.pi_features & LNET_PING_FEAT_DISCOVERY) &&
2319             !lnet_peer_discovery_disabled) {
2320                 CDEBUG(D_NET, "Peer %s has discovery enabled\n",
2321                        libcfs_nid2str(lp->lp_primary_nid));
2322                 lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2323         } else {
2324                 CDEBUG(D_NET, "Peer %s has discovery disabled\n",
2325                        libcfs_nid2str(lp->lp_primary_nid));
2326                 lp->lp_state |= LNET_PEER_NO_DISCOVERY;
2327         }
2328
2329         /*
2330          * Update the MULTI_RAIL flag based on the reply. If the peer
2331          * was configured with DLC then the setting should match what
2332          * DLC put in.
2333          */
2334         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL) {
2335                 if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2336                         /* Everything's fine */
2337                 } else if (lp->lp_state & LNET_PEER_CONFIGURED) {
2338                         CWARN("Reply says %s is Multi-Rail, DLC says not\n",
2339                               libcfs_nid2str(lp->lp_primary_nid));
2340                 } else {
2341                         /*
2342                          * if discovery is disabled then we don't want to
2343                          * update the state of the peer. All we'll do is
2344                          * update the peer_nis which were reported back in
2345                          * the initial ping
2346                          */
2347
2348                         if (!lnet_is_discovery_disabled_locked(lp)) {
2349                                 lp->lp_state |= LNET_PEER_MULTI_RAIL;
2350                                 lnet_peer_clr_non_mr_pref_nids(lp);
2351                         }
2352                 }
2353         } else if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2354                 if (lp->lp_state & LNET_PEER_CONFIGURED) {
2355                         CWARN("DLC says %s is Multi-Rail, Reply says not\n",
2356                               libcfs_nid2str(lp->lp_primary_nid));
2357                 } else {
2358                         CERROR("Multi-Rail state vanished from %s\n",
2359                                libcfs_nid2str(lp->lp_primary_nid));
2360                         lp->lp_state &= ~LNET_PEER_MULTI_RAIL;
2361                 }
2362         }
2363
2364         /*
2365          * Make sure we'll allocate the correct size ping buffer when
2366          * pinging the peer.
2367          */
2368         if (lp->lp_data_nnis < pbuf->pb_info.pi_nnis)
2369                 lp->lp_data_nnis = pbuf->pb_info.pi_nnis;
2370
2371         /*
2372          * Check for truncation of the Reply. Clear PING_SENT and set
2373          * PING_FAILED to trigger a retry.
2374          */
2375         if (pbuf->pb_nnis < pbuf->pb_info.pi_nnis) {
2376                 if (the_lnet.ln_push_target_nnis < pbuf->pb_info.pi_nnis)
2377                         the_lnet.ln_push_target_nnis = pbuf->pb_info.pi_nnis;
2378                 lp->lp_state |= LNET_PEER_PING_FAILED;
2379                 lp->lp_ping_error = 0;
2380                 CDEBUG(D_NET, "Truncated Reply from %s (%d nids)\n",
2381                        libcfs_nid2str(lp->lp_primary_nid),
2382                        pbuf->pb_info.pi_nnis);
2383                 goto out;
2384         }
2385
2386         /*
2387          * Check the sequence numbers in the reply. These are only
2388          * available if the reply came from a Multi-Rail peer.
2389          */
2390         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL &&
2391             pbuf->pb_info.pi_nnis > 1 &&
2392             lp->lp_primary_nid == pbuf->pb_info.pi_ni[1].ns_nid) {
2393                 if (LNET_PING_BUFFER_SEQNO(pbuf) < lp->lp_peer_seqno)
2394                         CDEBUG(D_NET, "peer %s: seq# got %u have %u. peer rebooted?\n",
2395                                 libcfs_nid2str(lp->lp_primary_nid),
2396                                 LNET_PING_BUFFER_SEQNO(pbuf),
2397                                 lp->lp_peer_seqno);
2398
2399                 lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2400         }
2401
2402         /* We're happy with the state of the data in the buffer. */
2403         CDEBUG(D_NET, "peer %s data present %u. state = 0x%x\n",
2404                libcfs_nid2str(lp->lp_primary_nid), lp->lp_peer_seqno, lp->lp_state);
2405         if (lp->lp_state & LNET_PEER_DATA_PRESENT)
2406                 lnet_ping_buffer_decref(lp->lp_data);
2407         else
2408                 lp->lp_state |= LNET_PEER_DATA_PRESENT;
2409         lnet_ping_buffer_addref(pbuf);
2410         lp->lp_data = pbuf;
2411 out:
2412         lp->lp_state &= ~LNET_PEER_PING_SENT;
2413         spin_unlock(&lp->lp_lock);
2414 }
2415
2416 /*
2417  * Send event handling. Only matters for error cases, where we clean
2418  * up state on the peer and peer_ni that would otherwise be updated in
2419  * the REPLY event handler for a successful Ping, and the ACK event
2420  * handler for a successful Push.
2421  */
2422 static int
2423 lnet_discovery_event_send(struct lnet_peer *lp, struct lnet_event *ev)
2424 {
2425         int rc = 0;
2426
2427         if (!ev->status)
2428                 goto out;
2429
2430         spin_lock(&lp->lp_lock);
2431         if (ev->msg_type == LNET_MSG_GET) {
2432                 lp->lp_state &= ~LNET_PEER_PING_SENT;
2433                 lp->lp_state |= LNET_PEER_PING_FAILED;
2434                 lp->lp_ping_error = ev->status;
2435         } else { /* ev->msg_type == LNET_MSG_PUT */
2436                 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2437                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2438                 lp->lp_push_error = ev->status;
2439         }
2440         spin_unlock(&lp->lp_lock);
2441         rc = LNET_REDISCOVER_PEER;
2442 out:
2443         CDEBUG(D_NET, "%s Send to %s: %d\n",
2444                 (ev->msg_type == LNET_MSG_GET ? "Ping" : "Push"),
2445                 libcfs_nid2str(ev->target.nid), rc);
2446         return rc;
2447 }
2448
2449 /*
2450  * Unlink event handling. This event is only seen if a call to
2451  * LNetMDUnlink() caused the event to be unlinked. If this call was
2452  * made after the event was set up in LNetGet() or LNetPut() then we
2453  * assume the Ping or Push timed out.
2454  */
2455 static void
2456 lnet_discovery_event_unlink(struct lnet_peer *lp, struct lnet_event *ev)
2457 {
2458         spin_lock(&lp->lp_lock);
2459         /* We've passed through LNetGet() */
2460         if (lp->lp_state & LNET_PEER_PING_SENT) {
2461                 lp->lp_state &= ~LNET_PEER_PING_SENT;
2462                 lp->lp_state |= LNET_PEER_PING_FAILED;
2463                 lp->lp_ping_error = -ETIMEDOUT;
2464                 CDEBUG(D_NET, "Ping Unlink for message to peer %s\n",
2465                         libcfs_nid2str(lp->lp_primary_nid));
2466         }
2467         /* We've passed through LNetPut() */
2468         if (lp->lp_state & LNET_PEER_PUSH_SENT) {
2469                 lp->lp_state &= ~LNET_PEER_PUSH_SENT;
2470                 lp->lp_state |= LNET_PEER_PUSH_FAILED;
2471                 lp->lp_push_error = -ETIMEDOUT;
2472                 CDEBUG(D_NET, "Push Unlink for message to peer %s\n",
2473                         libcfs_nid2str(lp->lp_primary_nid));
2474         }
2475         spin_unlock(&lp->lp_lock);
2476 }
2477
2478 /*
2479  * Event handler for the discovery EQ.
2480  *
2481  * Called with lnet_res_lock(cpt) held. The cpt is the
2482  * lnet_cpt_of_cookie() of the md handle cookie.
2483  */
2484 static void lnet_discovery_event_handler(struct lnet_event *event)
2485 {
2486         struct lnet_peer *lp = event->md.user_ptr;
2487         struct lnet_ping_buffer *pbuf;
2488         int rc;
2489
2490         /* discovery needs to take another look */
2491         rc = LNET_REDISCOVER_PEER;
2492
2493         CDEBUG(D_NET, "Received event: %d\n", event->type);
2494
2495         switch (event->type) {
2496         case LNET_EVENT_ACK:
2497                 lnet_discovery_event_ack(lp, event);
2498                 break;
2499         case LNET_EVENT_REPLY:
2500                 lnet_discovery_event_reply(lp, event);
2501                 break;
2502         case LNET_EVENT_SEND:
2503                 /* Only send failure triggers a retry. */
2504                 rc = lnet_discovery_event_send(lp, event);
2505                 break;
2506         case LNET_EVENT_UNLINK:
2507                 /* LNetMDUnlink() was called */
2508                 lnet_discovery_event_unlink(lp, event);
2509                 break;
2510         default:
2511                 /* Invalid events. */
2512                 LBUG();
2513         }
2514         lnet_net_lock(LNET_LOCK_EX);
2515         if (event->unlinked) {
2516                 pbuf = LNET_PING_INFO_TO_BUFFER(event->md.start);
2517                 lnet_ping_buffer_decref(pbuf);
2518                 lnet_peer_decref_locked(lp);
2519         }
2520
2521         /* put peer back at end of request queue, if discovery not already
2522          * done */
2523         if (rc == LNET_REDISCOVER_PEER && !lnet_peer_is_uptodate(lp)) {
2524                 list_move_tail(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2525                 wake_up(&the_lnet.ln_dc_waitq);
2526         }
2527         lnet_net_unlock(LNET_LOCK_EX);
2528 }
2529
2530 /*
2531  * Build a peer from incoming data.
2532  *
2533  * The NIDs in the incoming data are supposed to be structured as follows:
2534  *  - loopback
2535  *  - primary NID
2536  *  - other NIDs in same net
2537  *  - NIDs in second net
2538  *  - NIDs in third net
2539  *  - ...
2540  * This due to the way the list of NIDs in the data is created.
2541  *
2542  * Note that this function will mark the peer uptodate unless an
2543  * ENOMEM is encontered. All other errors are due to a conflict
2544  * between the DLC configuration and what discovery sees. We treat DLC
2545  * as binding, and therefore set the NIDS_UPTODATE flag to prevent the
2546  * peer from becoming stuck in discovery.
2547  */
2548 static int lnet_peer_merge_data(struct lnet_peer *lp,
2549                                 struct lnet_ping_buffer *pbuf)
2550 {
2551         struct lnet_peer_ni *lpni;
2552         lnet_nid_t *curnis = NULL;
2553         struct lnet_ni_status *addnis = NULL;
2554         lnet_nid_t *delnis = NULL;
2555         unsigned flags;
2556         int ncurnis;
2557         int naddnis;
2558         int ndelnis;
2559         int nnis = 0;
2560         int i;
2561         int j;
2562         int rc;
2563
2564         flags = LNET_PEER_DISCOVERED;
2565         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2566                 flags |= LNET_PEER_MULTI_RAIL;
2567
2568         /*
2569          * Cache the routing feature for the peer; whether it is enabled
2570          * for disabled as reported by the remote peer.
2571          */
2572         spin_lock(&lp->lp_lock);
2573         if (!(pbuf->pb_info.pi_features & LNET_PING_FEAT_RTE_DISABLED))
2574                 lp->lp_state |= LNET_PEER_ROUTER_ENABLED;
2575         else
2576                 lp->lp_state &= ~LNET_PEER_ROUTER_ENABLED;
2577         spin_unlock(&lp->lp_lock);
2578
2579         nnis = MAX(lp->lp_nnis, pbuf->pb_info.pi_nnis);
2580         LIBCFS_ALLOC(curnis, nnis * sizeof(*curnis));
2581         LIBCFS_ALLOC(addnis, nnis * sizeof(*addnis));
2582         LIBCFS_ALLOC(delnis, nnis * sizeof(*delnis));
2583         if (!curnis || !addnis || !delnis) {
2584                 rc = -ENOMEM;
2585                 goto out;
2586         }
2587         ncurnis = 0;
2588         naddnis = 0;
2589         ndelnis = 0;
2590
2591         /* Construct the list of NIDs present in peer. */
2592         lpni = NULL;
2593         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL)
2594                 curnis[ncurnis++] = lpni->lpni_nid;
2595
2596         /*
2597          * Check for NIDs in pbuf not present in curnis[].
2598          * The loop starts at 1 to skip the loopback NID.
2599          */
2600         for (i = 1; i < pbuf->pb_info.pi_nnis; i++) {
2601                 for (j = 0; j < ncurnis; j++)
2602                         if (pbuf->pb_info.pi_ni[i].ns_nid == curnis[j])
2603                                 break;
2604                 if (j == ncurnis)
2605                         addnis[naddnis++] = pbuf->pb_info.pi_ni[i];
2606         }
2607         /*
2608          * Check for NIDs in curnis[] not present in pbuf.
2609          * The nested loop starts at 1 to skip the loopback NID.
2610          *
2611          * But never add the loopback NID to delnis[]: if it is
2612          * present in curnis[] then this peer is for this node.
2613          */
2614         for (i = 0; i < ncurnis; i++) {
2615                 if (LNET_NETTYP(LNET_NIDNET(curnis[i])) == LOLND)
2616                         continue;
2617                 for (j = 1; j < pbuf->pb_info.pi_nnis; j++) {
2618                         if (curnis[i] == pbuf->pb_info.pi_ni[j].ns_nid) {
2619                                 /*
2620                                  * update the information we cache for the
2621                                  * peer with the latest information we
2622                                  * received
2623                                  */
2624                                 lpni = lnet_find_peer_ni_locked(curnis[i]);
2625                                 if (lpni) {
2626                                         lpni->lpni_ns_status = pbuf->pb_info.pi_ni[j].ns_status;
2627                                         lnet_peer_ni_decref_locked(lpni);
2628                                 }
2629                                 break;
2630                         }
2631                 }
2632                 if (j == pbuf->pb_info.pi_nnis)
2633                         delnis[ndelnis++] = curnis[i];
2634         }
2635
2636         /*
2637          * If we get here and the discovery is disabled then we don't want
2638          * to add or delete any NIs. We just updated the ones we have some
2639          * information on, and call it a day
2640          */
2641         rc = 0;
2642         if (lnet_is_discovery_disabled(lp))
2643                 goto out;
2644
2645         for (i = 0; i < naddnis; i++) {
2646                 rc = lnet_peer_add_nid(lp, addnis[i].ns_nid, flags);
2647                 if (rc) {
2648                         CERROR("Error adding NID %s to peer %s: %d\n",
2649                                libcfs_nid2str(addnis[i].ns_nid),
2650                                libcfs_nid2str(lp->lp_primary_nid), rc);
2651                         if (rc == -ENOMEM)
2652                                 goto out;
2653                 }
2654                 lpni = lnet_find_peer_ni_locked(addnis[i].ns_nid);
2655                 if (lpni) {
2656                         lpni->lpni_ns_status = addnis[i].ns_status;
2657                         lnet_peer_ni_decref_locked(lpni);
2658                 }
2659         }
2660
2661         for (i = 0; i < ndelnis; i++) {
2662                 /*
2663                  * for routers it's okay to delete the primary_nid because
2664                  * the upper layers don't really rely on it. So if we're
2665                  * being told that the router changed its primary_nid
2666                  * then it's okay to delete it.
2667                  */
2668                 if (lp->lp_rtr_refcount > 0)
2669                         flags |= LNET_PEER_RTR_NI_FORCE_DEL;
2670                 rc = lnet_peer_del_nid(lp, delnis[i], flags);
2671                 if (rc) {
2672                         CERROR("Error deleting NID %s from peer %s: %d\n",
2673                                libcfs_nid2str(delnis[i]),
2674                                libcfs_nid2str(lp->lp_primary_nid), rc);
2675                         if (rc == -ENOMEM)
2676                                 goto out;
2677                 }
2678         }
2679         /*
2680          * Errors other than -ENOMEM are due to peers having been
2681          * configured with DLC. Ignore these because DLC overrides
2682          * Discovery.
2683          */
2684         rc = 0;
2685 out:
2686         LIBCFS_FREE(curnis, nnis * sizeof(*curnis));
2687         LIBCFS_FREE(addnis, nnis * sizeof(*addnis));
2688         LIBCFS_FREE(delnis, nnis * sizeof(*delnis));
2689         lnet_ping_buffer_decref(pbuf);
2690         CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
2691
2692         if (rc) {
2693                 spin_lock(&lp->lp_lock);
2694                 lp->lp_state &= ~LNET_PEER_NIDS_UPTODATE;
2695                 lp->lp_state |= LNET_PEER_FORCE_PING;
2696                 spin_unlock(&lp->lp_lock);
2697         }
2698         return rc;
2699 }
2700
2701 /*
2702  * The data in pbuf says lp is its primary peer, but the data was
2703  * received by a different peer. Try to update lp with the data.
2704  */
2705 static int
2706 lnet_peer_set_primary_data(struct lnet_peer *lp, struct lnet_ping_buffer *pbuf)
2707 {
2708         struct lnet_handle_md mdh;
2709
2710         /* Queue lp for discovery, and force it on the request queue. */
2711         lnet_net_lock(LNET_LOCK_EX);
2712         if (lnet_peer_queue_for_discovery(lp))
2713                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_request);
2714         lnet_net_unlock(LNET_LOCK_EX);
2715
2716         LNetInvalidateMDHandle(&mdh);
2717
2718         /*
2719          * Decide whether we can move the peer to the DATA_PRESENT state.
2720          *
2721          * We replace stale data for a multi-rail peer, repair PING_FAILED
2722          * status, and preempt FORCE_PING.
2723          *
2724          * If after that we have DATA_PRESENT, we merge it into this peer.
2725          */
2726         spin_lock(&lp->lp_lock);
2727         if (lp->lp_state & LNET_PEER_MULTI_RAIL) {
2728                 if (lp->lp_peer_seqno < LNET_PING_BUFFER_SEQNO(pbuf)) {
2729                         lp->lp_peer_seqno = LNET_PING_BUFFER_SEQNO(pbuf);
2730                 } else if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2731                         lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2732                         lnet_ping_buffer_decref(pbuf);
2733                         pbuf = lp->lp_data;
2734                         lp->lp_data = NULL;
2735                 }
2736         }
2737         if (lp->lp_state & LNET_PEER_DATA_PRESENT) {
2738                 lnet_ping_buffer_decref(lp->lp_data);
2739                 lp->lp_data = NULL;
2740                 lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2741         }
2742         if (lp->lp_state & LNET_PEER_PING_FAILED) {
2743                 mdh = lp->lp_ping_mdh;
2744                 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2745                 lp->lp_state &= ~LNET_PEER_PING_FAILED;
2746                 lp->lp_ping_error = 0;
2747         }
2748         if (lp->lp_state & LNET_PEER_FORCE_PING)
2749                 lp->lp_state &= ~LNET_PEER_FORCE_PING;
2750         lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2751         spin_unlock(&lp->lp_lock);
2752
2753         if (!LNetMDHandleIsInvalid(mdh))
2754                 LNetMDUnlink(mdh);
2755
2756         if (pbuf)
2757                 return lnet_peer_merge_data(lp, pbuf);
2758
2759         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2760         return 0;
2761 }
2762
2763 static bool lnet_is_nid_in_ping_info(lnet_nid_t nid, struct lnet_ping_info *pinfo)
2764 {
2765         int i;
2766
2767         for (i = 0; i < pinfo->pi_nnis; i++) {
2768                 if (pinfo->pi_ni[i].ns_nid == nid)
2769                         return true;
2770         }
2771
2772         return false;
2773 }
2774
2775 /*
2776  * Update a peer using the data received.
2777  */
2778 static int lnet_peer_data_present(struct lnet_peer *lp)
2779 __must_hold(&lp->lp_lock)
2780 {
2781         struct lnet_ping_buffer *pbuf;
2782         struct lnet_peer_ni *lpni;
2783         lnet_nid_t nid = LNET_NID_ANY;
2784         unsigned flags;
2785         int rc = 0;
2786
2787         pbuf = lp->lp_data;
2788         lp->lp_data = NULL;
2789         lp->lp_state &= ~LNET_PEER_DATA_PRESENT;
2790         lp->lp_state |= LNET_PEER_NIDS_UPTODATE;
2791         spin_unlock(&lp->lp_lock);
2792
2793         /*
2794          * Modifications of peer structures are done while holding the
2795          * ln_api_mutex. A global lock is required because we may be
2796          * modifying multiple peer structures, and a mutex greatly
2797          * simplifies memory management.
2798          *
2799          * The actual changes to the data structures must also protect
2800          * against concurrent lookups, for which the lnet_net_lock in
2801          * LNET_LOCK_EX mode is used.
2802          */
2803         mutex_lock(&the_lnet.ln_api_mutex);
2804         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
2805                 rc = -ESHUTDOWN;
2806                 goto out;
2807         }
2808
2809         /*
2810          * If this peer is not on the peer list then it is being torn
2811          * down, and our reference count may be all that is keeping it
2812          * alive. Don't do any work on it.
2813          */
2814         if (list_empty(&lp->lp_peer_list))
2815                 goto out;
2816
2817         flags = LNET_PEER_DISCOVERED;
2818         if (pbuf->pb_info.pi_features & LNET_PING_FEAT_MULTI_RAIL)
2819                 flags |= LNET_PEER_MULTI_RAIL;
2820
2821         /*
2822          * Check whether the primary NID in the message matches the
2823          * primary NID of the peer. If it does, update the peer, if
2824          * it it does not, check whether there is already a peer with
2825          * that primary NID. If no such peer exists, try to update
2826          * the primary NID of the current peer (allowed if it was
2827          * created due to message traffic) and complete the update.
2828          * If the peer did exist, hand off the data to it.
2829          *
2830          * The peer for the loopback interface is a special case: this
2831          * is the peer for the local node, and we want to set its
2832          * primary NID to the correct value here. Moreover, this peer
2833          * can show up with only the loopback NID in the ping buffer.
2834          */
2835         if (pbuf->pb_info.pi_nnis <= 1)
2836                 goto out;
2837         nid = pbuf->pb_info.pi_ni[1].ns_nid;
2838         if (LNET_NETTYP(LNET_NIDNET(lp->lp_primary_nid)) == LOLND) {
2839                 rc = lnet_peer_set_primary_nid(lp, nid, flags);
2840                 if (!rc)
2841                         rc = lnet_peer_merge_data(lp, pbuf);
2842         /*
2843          * if the primary nid of the peer is present in the ping info returned
2844          * from the peer, but it's not the local primary peer we have
2845          * cached and discovery is disabled, then we don't want to update
2846          * our local peer info, by adding or removing NIDs, we just want
2847          * to update the status of the nids that we currently have
2848          * recorded in that peer.
2849          */
2850         } else if (lp->lp_primary_nid == nid ||
2851                    (lnet_is_nid_in_ping_info(lp->lp_primary_nid, &pbuf->pb_info) &&
2852                     lnet_is_discovery_disabled(lp))) {
2853                 rc = lnet_peer_merge_data(lp, pbuf);
2854         } else {
2855                 lpni = lnet_find_peer_ni_locked(nid);
2856                 if (!lpni) {
2857                         rc = lnet_peer_set_primary_nid(lp, nid, flags);
2858                         if (rc) {
2859                                 CERROR("Primary NID error %s versus %s: %d\n",
2860                                        libcfs_nid2str(lp->lp_primary_nid),
2861                                        libcfs_nid2str(nid), rc);
2862                         } else {
2863                                 rc = lnet_peer_merge_data(lp, pbuf);
2864                         }
2865                 } else {
2866                         struct lnet_peer *new_lp;
2867                         new_lp = lpni->lpni_peer_net->lpn_peer;
2868                         /*
2869                          * if lp has discovery/MR enabled that means new_lp
2870                          * should have discovery/MR enabled as well, since
2871                          * it's the same peer, which we're about to merge
2872                          */
2873                         if (!(lp->lp_state & LNET_PEER_NO_DISCOVERY))
2874                                 new_lp->lp_state &= ~LNET_PEER_NO_DISCOVERY;
2875                         if (lp->lp_state & LNET_PEER_MULTI_RAIL)
2876                                 new_lp->lp_state |= LNET_PEER_MULTI_RAIL;
2877
2878                         rc = lnet_peer_set_primary_data(new_lp, pbuf);
2879                         lnet_consolidate_routes_locked(lp, new_lp);
2880                         lnet_peer_ni_decref_locked(lpni);
2881                 }
2882         }
2883 out:
2884         CDEBUG(D_NET, "peer %s(%p): %d. state = 0x%x\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc,
2885                lp->lp_state);
2886         mutex_unlock(&the_lnet.ln_api_mutex);
2887
2888         spin_lock(&lp->lp_lock);
2889         /* Tell discovery to re-check the peer immediately. */
2890         if (!rc)
2891                 rc = LNET_REDISCOVER_PEER;
2892         return rc;
2893 }
2894
2895 /*
2896  * A ping failed. Clear the PING_FAILED state and set the
2897  * FORCE_PING state, to ensure a retry even if discovery is
2898  * disabled. This avoids being left with incorrect state.
2899  */
2900 static int lnet_peer_ping_failed(struct lnet_peer *lp)
2901 __must_hold(&lp->lp_lock)
2902 {
2903         struct lnet_handle_md mdh;
2904         int rc;
2905
2906         mdh = lp->lp_ping_mdh;
2907         LNetInvalidateMDHandle(&lp->lp_ping_mdh);
2908         lp->lp_state &= ~LNET_PEER_PING_FAILED;
2909         lp->lp_state |= LNET_PEER_FORCE_PING;
2910         rc = lp->lp_ping_error;
2911         lp->lp_ping_error = 0;
2912         spin_unlock(&lp->lp_lock);
2913
2914         if (!LNetMDHandleIsInvalid(mdh))
2915                 LNetMDUnlink(mdh);
2916
2917         CDEBUG(D_NET, "peer %s:%d\n",
2918                libcfs_nid2str(lp->lp_primary_nid), rc);
2919
2920         spin_lock(&lp->lp_lock);
2921         return rc ? rc : LNET_REDISCOVER_PEER;
2922 }
2923
2924 /*
2925  * Select NID to send a Ping or Push to.
2926  */
2927 static lnet_nid_t lnet_peer_select_nid(struct lnet_peer *lp)
2928 {
2929         struct lnet_peer_ni *lpni;
2930
2931         /* Look for a direct-connected NID for this peer. */
2932         lpni = NULL;
2933         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2934                 if (!lnet_get_net_locked(lpni->lpni_peer_net->lpn_net_id))
2935                         continue;
2936                 break;
2937         }
2938         if (lpni)
2939                 return lpni->lpni_nid;
2940
2941         /* Look for a routed-connected NID for this peer. */
2942         lpni = NULL;
2943         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
2944                 if (!lnet_find_rnet_locked(lpni->lpni_peer_net->lpn_net_id))
2945                         continue;
2946                 break;
2947         }
2948         if (lpni)
2949                 return lpni->lpni_nid;
2950
2951         return LNET_NID_ANY;
2952 }
2953
2954 /* Active side of ping. */
2955 static int lnet_peer_send_ping(struct lnet_peer *lp)
2956 __must_hold(&lp->lp_lock)
2957 {
2958         lnet_nid_t pnid;
2959         int nnis;
2960         int rc;
2961         int cpt;
2962
2963         lp->lp_state |= LNET_PEER_PING_SENT;
2964         lp->lp_state &= ~LNET_PEER_FORCE_PING;
2965         spin_unlock(&lp->lp_lock);
2966
2967         cpt = lnet_net_lock_current();
2968         /* Refcount for MD. */
2969         lnet_peer_addref_locked(lp);
2970         pnid = lnet_peer_select_nid(lp);
2971         lnet_net_unlock(cpt);
2972
2973         nnis = MAX(lp->lp_data_nnis, LNET_INTERFACES_MIN);
2974
2975         rc = lnet_send_ping(pnid, &lp->lp_ping_mdh, nnis, lp,
2976                             the_lnet.ln_dc_eqh, false);
2977
2978         /*
2979          * if LNetMDBind in lnet_send_ping fails we need to decrement the
2980          * refcount on the peer, otherwise LNetMDUnlink will be called
2981          * which will eventually do that.
2982          */
2983         if (rc > 0) {
2984                 lnet_net_lock(cpt);
2985                 lnet_peer_decref_locked(lp);
2986                 lnet_net_unlock(cpt);
2987                 rc = -rc; /* change the rc to negative value */
2988                 goto fail_error;
2989         } else if (rc < 0) {
2990                 goto fail_error;
2991         }
2992
2993         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
2994
2995         spin_lock(&lp->lp_lock);
2996         return 0;
2997
2998 fail_error:
2999         CDEBUG(D_NET, "peer %s: %d\n", libcfs_nid2str(lp->lp_primary_nid), rc);
3000         /*
3001          * The errors that get us here are considered hard errors and
3002          * cause Discovery to terminate. So we clear PING_SENT, but do
3003          * not set either PING_FAILED or FORCE_PING. In fact we need
3004          * to clear PING_FAILED, because the unlink event handler will
3005          * have set it if we called LNetMDUnlink() above.
3006          */
3007         spin_lock(&lp->lp_lock);
3008         lp->lp_state &= ~(LNET_PEER_PING_SENT | LNET_PEER_PING_FAILED);
3009         return rc;
3010 }
3011
3012 /*
3013  * This function exists because you cannot call LNetMDUnlink() from an
3014  * event handler.
3015  */
3016 static int lnet_peer_push_failed(struct lnet_peer *lp)
3017 __must_hold(&lp->lp_lock)
3018 {
3019         struct lnet_handle_md mdh;
3020         int rc;
3021
3022         mdh = lp->lp_push_mdh;
3023         LNetInvalidateMDHandle(&lp->lp_push_mdh);
3024         lp->lp_state &= ~LNET_PEER_PUSH_FAILED;
3025         rc = lp->lp_push_error;
3026         lp->lp_push_error = 0;
3027         spin_unlock(&lp->lp_lock);
3028
3029         if (!LNetMDHandleIsInvalid(mdh))
3030                 LNetMDUnlink(mdh);
3031
3032         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3033         spin_lock(&lp->lp_lock);
3034         return rc ? rc : LNET_REDISCOVER_PEER;
3035 }
3036
3037 /* Active side of push. */
3038 static int lnet_peer_send_push(struct lnet_peer *lp)
3039 __must_hold(&lp->lp_lock)
3040 {
3041         struct lnet_ping_buffer *pbuf;
3042         struct lnet_process_id id;
3043         struct lnet_md md;
3044         int cpt;
3045         int rc;
3046
3047         /* Don't push to a non-multi-rail peer. */
3048         if (!(lp->lp_state & LNET_PEER_MULTI_RAIL)) {
3049                 lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3050                 return 0;
3051         }
3052
3053         lp->lp_state |= LNET_PEER_PUSH_SENT;
3054         lp->lp_state &= ~LNET_PEER_FORCE_PUSH;
3055         spin_unlock(&lp->lp_lock);
3056
3057         cpt = lnet_net_lock_current();
3058         pbuf = the_lnet.ln_ping_target;
3059         lnet_ping_buffer_addref(pbuf);
3060         lnet_net_unlock(cpt);
3061
3062         /* Push source MD */
3063         md.start     = &pbuf->pb_info;
3064         md.length    = LNET_PING_INFO_SIZE(pbuf->pb_nnis);
3065         md.threshold = 2; /* Put/Ack */
3066         md.max_size  = 0;
3067         md.options   = 0;
3068         md.eq_handle = the_lnet.ln_dc_eqh;
3069         md.user_ptr  = lp;
3070
3071         rc = LNetMDBind(md, LNET_UNLINK, &lp->lp_push_mdh);
3072         if (rc) {
3073                 lnet_ping_buffer_decref(pbuf);
3074                 CERROR("Can't bind push source MD: %d\n", rc);
3075                 goto fail_error;
3076         }
3077         cpt = lnet_net_lock_current();
3078         /* Refcount for MD. */
3079         lnet_peer_addref_locked(lp);
3080         id.pid = LNET_PID_LUSTRE;
3081         id.nid = lnet_peer_select_nid(lp);
3082         lnet_net_unlock(cpt);
3083
3084         if (id.nid == LNET_NID_ANY) {
3085                 rc = -EHOSTUNREACH;
3086                 goto fail_unlink;
3087         }
3088
3089         rc = LNetPut(LNET_NID_ANY, lp->lp_push_mdh,
3090                      LNET_ACK_REQ, id, LNET_RESERVED_PORTAL,
3091                      LNET_PROTO_PING_MATCHBITS, 0, 0);
3092
3093         if (rc)
3094                 goto fail_unlink;
3095
3096         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3097
3098         spin_lock(&lp->lp_lock);
3099         return 0;
3100
3101 fail_unlink:
3102         LNetMDUnlink(lp->lp_push_mdh);
3103         LNetInvalidateMDHandle(&lp->lp_push_mdh);
3104 fail_error:
3105         CDEBUG(D_NET, "peer %s(%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
3106         /*
3107          * The errors that get us here are considered hard errors and
3108          * cause Discovery to terminate. So we clear PUSH_SENT, but do
3109          * not set PUSH_FAILED. In fact we need to clear PUSH_FAILED,
3110          * because the unlink event handler will have set it if we
3111          * called LNetMDUnlink() above.
3112          */
3113         spin_lock(&lp->lp_lock);
3114         lp->lp_state &= ~(LNET_PEER_PUSH_SENT | LNET_PEER_PUSH_FAILED);
3115         return rc;
3116 }
3117
3118 /*
3119  * An unrecoverable error was encountered during discovery.
3120  * Set error status in peer and abort discovery.
3121  */
3122 static void lnet_peer_discovery_error(struct lnet_peer *lp, int error)
3123 {
3124         CDEBUG(D_NET, "Discovery error %s: %d\n",
3125                libcfs_nid2str(lp->lp_primary_nid), error);
3126
3127         spin_lock(&lp->lp_lock);
3128         lp->lp_dc_error = error;
3129         lp->lp_state &= ~LNET_PEER_DISCOVERING;
3130         lp->lp_state |= LNET_PEER_REDISCOVER;
3131         spin_unlock(&lp->lp_lock);
3132 }
3133
3134 /*
3135  * Mark the peer as discovered.
3136  */
3137 static int lnet_peer_discovered(struct lnet_peer *lp)
3138 __must_hold(&lp->lp_lock)
3139 {
3140         lp->lp_state |= LNET_PEER_DISCOVERED;
3141         lp->lp_state &= ~(LNET_PEER_DISCOVERING |
3142                           LNET_PEER_REDISCOVER);
3143
3144         CDEBUG(D_NET, "peer %s\n", libcfs_nid2str(lp->lp_primary_nid));
3145
3146         return 0;
3147 }
3148
3149
3150 /*
3151  * Discovering this peer is taking too long. Cancel any Ping or Push
3152  * that discovery is waiting on by unlinking the relevant MDs. The
3153  * lnet_discovery_event_handler() will proceed from here and complete
3154  * the cleanup.
3155  */
3156 static void lnet_peer_cancel_discovery(struct lnet_peer *lp)
3157 {
3158         struct lnet_handle_md ping_mdh;
3159         struct lnet_handle_md push_mdh;
3160
3161         LNetInvalidateMDHandle(&ping_mdh);
3162         LNetInvalidateMDHandle(&push_mdh);
3163
3164         spin_lock(&lp->lp_lock);
3165         if (lp->lp_state & LNET_PEER_PING_SENT) {
3166                 ping_mdh = lp->lp_ping_mdh;
3167                 LNetInvalidateMDHandle(&lp->lp_ping_mdh);
3168         }
3169         if (lp->lp_state & LNET_PEER_PUSH_SENT) {
3170                 push_mdh = lp->lp_push_mdh;
3171                 LNetInvalidateMDHandle(&lp->lp_push_mdh);
3172         }
3173         spin_unlock(&lp->lp_lock);
3174
3175         if (!LNetMDHandleIsInvalid(ping_mdh))
3176                 LNetMDUnlink(ping_mdh);
3177         if (!LNetMDHandleIsInvalid(push_mdh))
3178                 LNetMDUnlink(push_mdh);
3179 }
3180
3181 /*
3182  * Wait for work to be queued or some other change that must be
3183  * attended to. Returns non-zero if the discovery thread should shut
3184  * down.
3185  */
3186 static int lnet_peer_discovery_wait_for_work(void)
3187 {
3188         int cpt;
3189         int rc = 0;
3190
3191         DEFINE_WAIT(wait);
3192
3193         cpt = lnet_net_lock_current();
3194         for (;;) {
3195                 prepare_to_wait(&the_lnet.ln_dc_waitq, &wait,
3196                                 TASK_INTERRUPTIBLE);
3197                 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3198                         break;
3199                 if (lnet_push_target_resize_needed())
3200                         break;
3201                 if (!list_empty(&the_lnet.ln_dc_request))
3202                         break;
3203                 if (!list_empty(&the_lnet.ln_msg_resend))
3204                         break;
3205                 lnet_net_unlock(cpt);
3206
3207                 /*
3208                  * wakeup max every second to check if there are peers that
3209                  * have been stuck on the working queue for greater than
3210                  * the peer timeout.
3211                  */
3212                 schedule_timeout(cfs_time_seconds(1));
3213                 finish_wait(&the_lnet.ln_dc_waitq, &wait);
3214                 cpt = lnet_net_lock_current();
3215         }
3216         finish_wait(&the_lnet.ln_dc_waitq, &wait);
3217
3218         if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3219                 rc = -ESHUTDOWN;
3220
3221         lnet_net_unlock(cpt);
3222
3223         CDEBUG(D_NET, "woken: %d\n", rc);
3224
3225         return rc;
3226 }
3227
3228 /*
3229  * Messages that were pending on a destroyed peer will be put on a global
3230  * resend list. The message resend list will be checked by
3231  * the discovery thread when it wakes up, and will resend messages. These
3232  * messages can still be sendable in the case the lpni which was the initial
3233  * cause of the message re-queue was transfered to another peer.
3234  *
3235  * It is possible that LNet could be shutdown while we're iterating
3236  * through the list. lnet_shudown_lndnets() will attempt to access the
3237  * resend list, but will have to wait until the spinlock is released, by
3238  * which time there shouldn't be any more messages on the resend list.
3239  * During shutdown lnet_send() will fail and lnet_finalize() will be called
3240  * for the messages so they can be released. The other case is that
3241  * lnet_shudown_lndnets() can finalize all the messages before this
3242  * function can visit the resend list, in which case this function will be
3243  * a no-op.
3244  */
3245 static void lnet_resend_msgs(void)
3246 {
3247         struct lnet_msg *msg, *tmp;
3248         struct list_head resend;
3249         int rc;
3250
3251         INIT_LIST_HEAD(&resend);
3252
3253         spin_lock(&the_lnet.ln_msg_resend_lock);
3254         list_splice(&the_lnet.ln_msg_resend, &resend);
3255         spin_unlock(&the_lnet.ln_msg_resend_lock);
3256
3257         list_for_each_entry_safe(msg, tmp, &resend, msg_list) {
3258                 list_del_init(&msg->msg_list);
3259                 rc = lnet_send(msg->msg_src_nid_param, msg,
3260                                msg->msg_rtr_nid_param);
3261                 if (rc < 0) {
3262                         CNETERR("Error sending %s to %s: %d\n",
3263                                lnet_msgtyp2str(msg->msg_type),
3264                                libcfs_id2str(msg->msg_target), rc);
3265                         lnet_finalize(msg, rc);
3266                 }
3267         }
3268 }
3269
3270 /* The discovery thread. */
3271 static int lnet_peer_discovery(void *arg)
3272 {
3273         struct lnet_peer *lp;
3274         int rc;
3275
3276         CDEBUG(D_NET, "started\n");
3277         cfs_block_allsigs();
3278
3279         for (;;) {
3280                 if (lnet_peer_discovery_wait_for_work())
3281                         break;
3282
3283                 lnet_resend_msgs();
3284
3285                 if (lnet_push_target_resize_needed())
3286                         lnet_push_target_resize();
3287
3288                 lnet_net_lock(LNET_LOCK_EX);
3289                 if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3290                         break;
3291
3292                 /*
3293                  * Process all incoming discovery work requests.  When
3294                  * discovery must wait on a peer to change state, it
3295                  * is added to the tail of the ln_dc_working queue. A
3296                  * timestamp keeps track of when the peer was added,
3297                  * so we can time out discovery requests that take too
3298                  * long.
3299                  */
3300                 while (!list_empty(&the_lnet.ln_dc_request)) {
3301                         lp = list_first_entry(&the_lnet.ln_dc_request,
3302                                               struct lnet_peer, lp_dc_list);
3303                         list_move(&lp->lp_dc_list, &the_lnet.ln_dc_working);
3304                         /*
3305                          * set the time the peer was put on the dc_working
3306                          * queue. It shouldn't remain on the queue
3307                          * forever, in case the GET message (for ping)
3308                          * doesn't get a REPLY or the PUT message (for
3309                          * push) doesn't get an ACK.
3310                          */
3311                         lp->lp_last_queued = ktime_get_real_seconds();
3312                         lnet_net_unlock(LNET_LOCK_EX);
3313
3314                         /*
3315                          * Select an action depending on the state of
3316                          * the peer and whether discovery is disabled.
3317                          * The check whether discovery is disabled is
3318                          * done after the code that handles processing
3319                          * for arrived data, cleanup for failures, and
3320                          * forcing a Ping or Push.
3321                          */
3322                         spin_lock(&lp->lp_lock);
3323                         CDEBUG(D_NET, "peer %s(%p) state %#x\n",
3324                                 libcfs_nid2str(lp->lp_primary_nid), lp,
3325                                 lp->lp_state);
3326                         if (lp->lp_state & LNET_PEER_DATA_PRESENT)
3327                                 rc = lnet_peer_data_present(lp);
3328                         else if (lp->lp_state & LNET_PEER_PING_FAILED)
3329                                 rc = lnet_peer_ping_failed(lp);
3330                         else if (lp->lp_state & LNET_PEER_PUSH_FAILED)
3331                                 rc = lnet_peer_push_failed(lp);
3332                         else if (lp->lp_state & LNET_PEER_FORCE_PING)
3333                                 rc = lnet_peer_send_ping(lp);
3334                         else if (lp->lp_state & LNET_PEER_FORCE_PUSH)
3335                                 rc = lnet_peer_send_push(lp);
3336                         else if (!(lp->lp_state & LNET_PEER_NIDS_UPTODATE))
3337                                 rc = lnet_peer_send_ping(lp);
3338                         else if (lnet_peer_needs_push(lp))
3339                                 rc = lnet_peer_send_push(lp);
3340                         else
3341                                 rc = lnet_peer_discovered(lp);
3342                         CDEBUG(D_NET, "peer %s(%p) state %#x rc %d\n",
3343                                 libcfs_nid2str(lp->lp_primary_nid), lp,
3344                                 lp->lp_state, rc);
3345                         spin_unlock(&lp->lp_lock);
3346
3347                         lnet_net_lock(LNET_LOCK_EX);
3348                         if (rc == LNET_REDISCOVER_PEER) {
3349                                 list_move(&lp->lp_dc_list,
3350                                           &the_lnet.ln_dc_request);
3351                         } else if (rc) {
3352                                 lnet_peer_discovery_error(lp, rc);
3353                         }
3354                         if (!(lp->lp_state & LNET_PEER_DISCOVERING))
3355                                 lnet_peer_discovery_complete(lp);
3356                         if (the_lnet.ln_dc_state == LNET_DC_STATE_STOPPING)
3357                                 break;
3358                 }
3359
3360                 lnet_net_unlock(LNET_LOCK_EX);
3361         }
3362
3363         CDEBUG(D_NET, "stopping\n");
3364         /*
3365          * Clean up before telling lnet_peer_discovery_stop() that
3366          * we're done. Use wake_up() below to somewhat reduce the
3367          * size of the thundering herd if there are multiple threads
3368          * waiting on discovery of a single peer.
3369          */
3370
3371         /* Queue cleanup 1: stop all pending pings and pushes. */
3372         lnet_net_lock(LNET_LOCK_EX);
3373         while (!list_empty(&the_lnet.ln_dc_working)) {
3374                 lp = list_first_entry(&the_lnet.ln_dc_working,
3375                                       struct lnet_peer, lp_dc_list);
3376                 list_move(&lp->lp_dc_list, &the_lnet.ln_dc_expired);
3377                 lnet_net_unlock(LNET_LOCK_EX);
3378                 lnet_peer_cancel_discovery(lp);
3379                 lnet_net_lock(LNET_LOCK_EX);
3380         }
3381         lnet_net_unlock(LNET_LOCK_EX);
3382
3383         /* Queue cleanup 2: wait for the expired queue to clear. */
3384         while (!list_empty(&the_lnet.ln_dc_expired))
3385                 schedule_timeout(cfs_time_seconds(1));
3386
3387         /* Queue cleanup 3: clear the request queue. */
3388         lnet_net_lock(LNET_LOCK_EX);
3389         while (!list_empty(&the_lnet.ln_dc_request)) {
3390                 lp = list_first_entry(&the_lnet.ln_dc_request,
3391                                       struct lnet_peer, lp_dc_list);
3392                 lnet_peer_discovery_error(lp, -ESHUTDOWN);
3393                 lnet_peer_discovery_complete(lp);
3394         }
3395         lnet_net_unlock(LNET_LOCK_EX);
3396
3397         LNetEQFree(the_lnet.ln_dc_eqh);
3398         LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3399
3400         the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3401         wake_up(&the_lnet.ln_dc_waitq);
3402
3403         CDEBUG(D_NET, "stopped\n");
3404
3405         return 0;
3406 }
3407
3408 /* ln_api_mutex is held on entry. */
3409 int lnet_peer_discovery_start(void)
3410 {
3411         struct task_struct *task;
3412         int rc;
3413
3414         if (the_lnet.ln_dc_state != LNET_DC_STATE_SHUTDOWN)
3415                 return -EALREADY;
3416
3417         rc = LNetEQAlloc(0, lnet_discovery_event_handler, &the_lnet.ln_dc_eqh);
3418         if (rc != 0) {
3419                 CERROR("Can't allocate discovery EQ: %d\n", rc);
3420                 return rc;
3421         }
3422
3423         the_lnet.ln_dc_state = LNET_DC_STATE_RUNNING;
3424         task = kthread_run(lnet_peer_discovery, NULL, "lnet_discovery");
3425         if (IS_ERR(task)) {
3426                 rc = PTR_ERR(task);
3427                 CERROR("Can't start peer discovery thread: %d\n", rc);
3428
3429                 LNetEQFree(the_lnet.ln_dc_eqh);
3430                 LNetInvalidateEQHandle(&the_lnet.ln_dc_eqh);
3431
3432                 the_lnet.ln_dc_state = LNET_DC_STATE_SHUTDOWN;
3433         }
3434
3435         CDEBUG(D_NET, "discovery start: %d\n", rc);
3436
3437         return rc;
3438 }
3439
3440 /* ln_api_mutex is held on entry. */
3441 void lnet_peer_discovery_stop(void)
3442 {
3443         if (the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN)
3444                 return;
3445
3446         LASSERT(the_lnet.ln_dc_state == LNET_DC_STATE_RUNNING);
3447         the_lnet.ln_dc_state = LNET_DC_STATE_STOPPING;
3448         wake_up(&the_lnet.ln_dc_waitq);
3449
3450         wait_event(the_lnet.ln_dc_waitq,
3451                    the_lnet.ln_dc_state == LNET_DC_STATE_SHUTDOWN);
3452
3453         LASSERT(list_empty(&the_lnet.ln_dc_request));
3454         LASSERT(list_empty(&the_lnet.ln_dc_working));
3455         LASSERT(list_empty(&the_lnet.ln_dc_expired));
3456
3457         CDEBUG(D_NET, "discovery stopped\n");
3458 }
3459
3460 /* Debugging */
3461
3462 void
3463 lnet_debug_peer(lnet_nid_t nid)
3464 {
3465         char                    *aliveness = "NA";
3466         struct lnet_peer_ni     *lp;
3467         int                     cpt;
3468
3469         cpt = lnet_cpt_of_nid(nid, NULL);
3470         lnet_net_lock(cpt);
3471
3472         lp = lnet_nid2peerni_locked(nid, LNET_NID_ANY, cpt);
3473         if (IS_ERR(lp)) {
3474                 lnet_net_unlock(cpt);
3475                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
3476                 return;
3477         }
3478
3479         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
3480                 aliveness = (lnet_is_peer_ni_alive(lp)) ? "up" : "down";
3481
3482         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
3483                libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
3484                aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
3485                lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
3486                lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
3487
3488         lnet_peer_ni_decref_locked(lp);
3489
3490         lnet_net_unlock(cpt);
3491 }
3492
3493 /* Gathering information for userspace. */
3494
3495 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
3496                           char aliveness[LNET_MAX_STR_LEN],
3497                           __u32 *cpt_iter, __u32 *refcount,
3498                           __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
3499                           __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
3500                           __u32 *peer_tx_qnob)
3501 {
3502         struct lnet_peer_table          *peer_table;
3503         struct lnet_peer_ni             *lp;
3504         int                             j;
3505         int                             lncpt;
3506         bool                            found = false;
3507
3508         /* get the number of CPTs */
3509         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3510
3511         /* if the cpt number to be examined is >= the number of cpts in
3512          * the system then indicate that there are no more cpts to examin
3513          */
3514         if (*cpt_iter >= lncpt)
3515                 return -ENOENT;
3516
3517         /* get the current table */
3518         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
3519         /* if the ptable is NULL then there are no more cpts to examine */
3520         if (peer_table == NULL)
3521                 return -ENOENT;
3522
3523         lnet_net_lock(*cpt_iter);
3524
3525         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
3526                 struct list_head *peers = &peer_table->pt_hash[j];
3527
3528                 list_for_each_entry(lp, peers, lpni_hashlist) {
3529                         if (peer_index-- > 0)
3530                                 continue;
3531
3532                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
3533                         if (lnet_isrouter(lp) ||
3534                                 lnet_peer_aliveness_enabled(lp))
3535                                 snprintf(aliveness, LNET_MAX_STR_LEN,
3536                                          lnet_is_peer_ni_alive(lp) ? "up" : "down");
3537
3538                         *nid = lp->lpni_nid;
3539                         *refcount = atomic_read(&lp->lpni_refcount);
3540                         *ni_peer_tx_credits =
3541                                 lp->lpni_net->net_tunables.lct_peer_tx_credits;
3542                         *peer_tx_credits = lp->lpni_txcredits;
3543                         *peer_rtr_credits = lp->lpni_rtrcredits;
3544                         *peer_min_rtr_credits = lp->lpni_mintxcredits;
3545                         *peer_tx_qnob = lp->lpni_txqnob;
3546
3547                         found = true;
3548                 }
3549
3550         }
3551         lnet_net_unlock(*cpt_iter);
3552
3553         *cpt_iter = lncpt;
3554
3555         return found ? 0 : -ENOENT;
3556 }
3557
3558 /* ln_api_mutex is held, which keeps the peer list stable */
3559 int lnet_get_peer_info(struct lnet_ioctl_peer_cfg *cfg, void __user *bulk)
3560 {
3561         struct lnet_ioctl_element_stats *lpni_stats;
3562         struct lnet_ioctl_element_msg_stats *lpni_msg_stats;
3563         struct lnet_ioctl_peer_ni_hstats *lpni_hstats;
3564         struct lnet_peer_ni_credit_info *lpni_info;
3565         struct lnet_peer_ni *lpni;
3566         struct lnet_peer *lp;
3567         lnet_nid_t nid;
3568         __u32 size;
3569         int rc;
3570
3571         lp = lnet_find_peer(cfg->prcfg_prim_nid);
3572
3573         if (!lp) {
3574                 rc = -ENOENT;
3575                 goto out;
3576         }
3577
3578         size = sizeof(nid) + sizeof(*lpni_info) + sizeof(*lpni_stats)
3579                 + sizeof(*lpni_msg_stats) + sizeof(*lpni_hstats);
3580         size *= lp->lp_nnis;
3581         if (size > cfg->prcfg_size) {
3582                 cfg->prcfg_size = size;
3583                 rc = -E2BIG;
3584                 goto out_lp_decref;
3585         }
3586
3587         cfg->prcfg_prim_nid = lp->lp_primary_nid;
3588         cfg->prcfg_mr = lnet_peer_is_multi_rail(lp);
3589         cfg->prcfg_cfg_nid = lp->lp_primary_nid;
3590         cfg->prcfg_count = lp->lp_nnis;
3591         cfg->prcfg_size = size;
3592         cfg->prcfg_state = lp->lp_state;
3593
3594         /* Allocate helper buffers. */
3595         rc = -ENOMEM;
3596         LIBCFS_ALLOC(lpni_info, sizeof(*lpni_info));
3597         if (!lpni_info)
3598                 goto out_lp_decref;
3599         LIBCFS_ALLOC(lpni_stats, sizeof(*lpni_stats));
3600         if (!lpni_stats)
3601                 goto out_free_info;
3602         LIBCFS_ALLOC(lpni_msg_stats, sizeof(*lpni_msg_stats));
3603         if (!lpni_msg_stats)
3604                 goto out_free_stats;
3605         LIBCFS_ALLOC(lpni_hstats, sizeof(*lpni_hstats));
3606         if (!lpni_hstats)
3607                 goto out_free_msg_stats;
3608
3609
3610         lpni = NULL;
3611         rc = -EFAULT;
3612         while ((lpni = lnet_get_next_peer_ni_locked(lp, NULL, lpni)) != NULL) {
3613                 nid = lpni->lpni_nid;
3614                 if (copy_to_user(bulk, &nid, sizeof(nid)))
3615                         goto out_free_hstats;
3616                 bulk += sizeof(nid);
3617
3618                 memset(lpni_info, 0, sizeof(*lpni_info));
3619                 snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
3620                 if (lnet_isrouter(lpni) ||
3621                         lnet_peer_aliveness_enabled(lpni))
3622                         snprintf(lpni_info->cr_aliveness, LNET_MAX_STR_LEN,
3623                                 lnet_is_peer_ni_alive(lpni) ? "up" : "down");
3624
3625                 lpni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
3626                 lpni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
3627                         lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
3628                 lpni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
3629                 lpni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
3630                 lpni_info->cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
3631                 lpni_info->cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
3632                 lpni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
3633                 if (copy_to_user(bulk, lpni_info, sizeof(*lpni_info)))
3634                         goto out_free_hstats;
3635                 bulk += sizeof(*lpni_info);
3636
3637                 memset(lpni_stats, 0, sizeof(*lpni_stats));
3638                 lpni_stats->iel_send_count = lnet_sum_stats(&lpni->lpni_stats,
3639                                                             LNET_STATS_TYPE_SEND);
3640                 lpni_stats->iel_recv_count = lnet_sum_stats(&lpni->lpni_stats,
3641                                                             LNET_STATS_TYPE_RECV);
3642                 lpni_stats->iel_drop_count = lnet_sum_stats(&lpni->lpni_stats,
3643                                                             LNET_STATS_TYPE_DROP);
3644                 if (copy_to_user(bulk, lpni_stats, sizeof(*lpni_stats)))
3645                         goto out_free_hstats;
3646                 bulk += sizeof(*lpni_stats);
3647                 lnet_usr_translate_stats(lpni_msg_stats, &lpni->lpni_stats);
3648                 if (copy_to_user(bulk, lpni_msg_stats, sizeof(*lpni_msg_stats)))
3649                         goto out_free_hstats;
3650                 bulk += sizeof(*lpni_msg_stats);
3651                 lpni_hstats->hlpni_network_timeout =
3652                   atomic_read(&lpni->lpni_hstats.hlt_network_timeout);
3653                 lpni_hstats->hlpni_remote_dropped =
3654                   atomic_read(&lpni->lpni_hstats.hlt_remote_dropped);
3655                 lpni_hstats->hlpni_remote_timeout =
3656                   atomic_read(&lpni->lpni_hstats.hlt_remote_timeout);
3657                 lpni_hstats->hlpni_remote_error =
3658                   atomic_read(&lpni->lpni_hstats.hlt_remote_error);
3659                 lpni_hstats->hlpni_health_value =
3660                   atomic_read(&lpni->lpni_healthv);
3661                 if (copy_to_user(bulk, lpni_hstats, sizeof(*lpni_hstats)))
3662                         goto out_free_hstats;
3663                 bulk += sizeof(*lpni_hstats);
3664         }
3665         rc = 0;
3666
3667 out_free_hstats:
3668         LIBCFS_FREE(lpni_hstats, sizeof(*lpni_hstats));
3669 out_free_msg_stats:
3670         LIBCFS_FREE(lpni_msg_stats, sizeof(*lpni_msg_stats));
3671 out_free_stats:
3672         LIBCFS_FREE(lpni_stats, sizeof(*lpni_stats));
3673 out_free_info:
3674         LIBCFS_FREE(lpni_info, sizeof(*lpni_info));
3675 out_lp_decref:
3676         lnet_peer_decref_locked(lp);
3677 out:
3678         return rc;
3679 }
3680
3681 void
3682 lnet_peer_ni_add_to_recoveryq_locked(struct lnet_peer_ni *lpni)
3683 {
3684         /* the mt could've shutdown and cleaned up the queues */
3685         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
3686                 return;
3687
3688         if (list_empty(&lpni->lpni_recovery) &&
3689             atomic_read(&lpni->lpni_healthv) < LNET_MAX_HEALTH_VALUE) {
3690                 CERROR("lpni %s added to recovery queue. Health = %d\n",
3691                         libcfs_nid2str(lpni->lpni_nid),
3692                         atomic_read(&lpni->lpni_healthv));
3693                 list_add_tail(&lpni->lpni_recovery, &the_lnet.ln_mt_peerNIRecovq);
3694                 lnet_peer_ni_addref_locked(lpni);
3695         }
3696 }
3697
3698 /* Call with the ln_api_mutex held */
3699 void
3700 lnet_peer_ni_set_healthv(lnet_nid_t nid, int value, bool all)
3701 {
3702         struct lnet_peer_table *ptable;
3703         struct lnet_peer *lp;
3704         struct lnet_peer_net *lpn;
3705         struct lnet_peer_ni *lpni;
3706         int lncpt;
3707         int cpt;
3708
3709         if (the_lnet.ln_state != LNET_STATE_RUNNING)
3710                 return;
3711
3712         if (!all) {
3713                 lnet_net_lock(LNET_LOCK_EX);
3714                 lpni = lnet_find_peer_ni_locked(nid);
3715                 if (!lpni) {
3716                         lnet_net_unlock(LNET_LOCK_EX);
3717                         return;
3718                 }
3719                 atomic_set(&lpni->lpni_healthv, value);
3720                 lnet_peer_ni_add_to_recoveryq_locked(lpni);
3721                 lnet_peer_ni_decref_locked(lpni);
3722                 lnet_net_unlock(LNET_LOCK_EX);
3723                 return;
3724         }
3725
3726         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
3727
3728         /*
3729          * Walk all the peers and reset the healhv for each one to the
3730          * maximum value.
3731          */
3732         lnet_net_lock(LNET_LOCK_EX);
3733         for (cpt = 0; cpt < lncpt; cpt++) {
3734                 ptable = the_lnet.ln_peer_tables[cpt];
3735                 list_for_each_entry(lp, &ptable->pt_peer_list, lp_peer_list) {
3736                         list_for_each_entry(lpn, &lp->lp_peer_nets, lpn_peer_nets) {
3737                                 list_for_each_entry(lpni, &lpn->lpn_peer_nis,
3738                                                     lpni_peer_nis) {
3739                                         atomic_set(&lpni->lpni_healthv, value);
3740                                         lnet_peer_ni_add_to_recoveryq_locked(lpni);
3741                                 }
3742                         }
3743                 }
3744         }
3745         lnet_net_unlock(LNET_LOCK_EX);
3746 }
3747