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