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