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