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