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