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