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