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