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