Whamcloud - gitweb
LU-9119 lnet: fix race in lnet shutdown path
[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, 2014, 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 <lnet/lib-lnet.h>
38 #include <lnet/lib-dlc.h>
39
40 static void
41 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
42 {
43         if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
44                 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
45                 lnet_peer_ni_decref_locked(lpni);
46         }
47 }
48
49 void
50 lnet_peer_net_added(struct lnet_net *net)
51 {
52         struct lnet_peer_ni *lpni, *tmp;
53
54         list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
55                                  lpni_on_remote_peer_ni_list) {
56
57                 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
58                         lpni->lpni_net = net;
59
60                         spin_lock(&lpni->lpni_lock);
61                         lpni->lpni_txcredits =
62                                 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
63                         lpni->lpni_mintxcredits = lpni->lpni_txcredits;
64                         lpni->lpni_rtrcredits =
65                                 lnet_peer_buffer_credits(lpni->lpni_net);
66                         lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
67                         spin_unlock(&lpni->lpni_lock);
68
69                         lnet_peer_remove_from_remote_list(lpni);
70                 }
71         }
72 }
73
74 static void
75 lnet_peer_tables_destroy(void)
76 {
77         struct lnet_peer_table  *ptable;
78         struct list_head        *hash;
79         int                     i;
80         int                     j;
81
82         if (!the_lnet.ln_peer_tables)
83                 return;
84
85         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
86                 hash = ptable->pt_hash;
87                 if (!hash) /* not intialized */
88                         break;
89
90                 LASSERT(list_empty(&ptable->pt_zombie_list));
91
92                 ptable->pt_hash = NULL;
93                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
94                         LASSERT(list_empty(&hash[j]));
95
96                 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
97         }
98
99         cfs_percpt_free(the_lnet.ln_peer_tables);
100         the_lnet.ln_peer_tables = NULL;
101 }
102
103 int
104 lnet_peer_tables_create(void)
105 {
106         struct lnet_peer_table  *ptable;
107         struct list_head        *hash;
108         int                     i;
109         int                     j;
110
111         the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
112                                                    sizeof(*ptable));
113         if (the_lnet.ln_peer_tables == NULL) {
114                 CERROR("Failed to allocate cpu-partition peer tables\n");
115                 return -ENOMEM;
116         }
117
118         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
119                 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
120                                  LNET_PEER_HASH_SIZE * sizeof(*hash));
121                 if (hash == NULL) {
122                         CERROR("Failed to create peer hash table\n");
123                         lnet_peer_tables_destroy();
124                         return -ENOMEM;
125                 }
126
127                 spin_lock_init(&ptable->pt_zombie_lock);
128                 INIT_LIST_HEAD(&ptable->pt_zombie_list);
129
130                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
131                         INIT_LIST_HEAD(&hash[j]);
132                 ptable->pt_hash = hash; /* sign of initialization */
133         }
134
135         return 0;
136 }
137
138 static struct lnet_peer_ni *
139 lnet_peer_ni_alloc(lnet_nid_t nid)
140 {
141         struct lnet_peer_ni *lpni;
142         struct lnet_net *net;
143         int cpt;
144
145         cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
146
147         LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
148         if (!lpni)
149                 return NULL;
150
151         INIT_LIST_HEAD(&lpni->lpni_txq);
152         INIT_LIST_HEAD(&lpni->lpni_rtrq);
153         INIT_LIST_HEAD(&lpni->lpni_routes);
154         INIT_LIST_HEAD(&lpni->lpni_hashlist);
155         INIT_LIST_HEAD(&lpni->lpni_on_peer_net_list);
156         INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
157
158         spin_lock_init(&lpni->lpni_lock);
159
160         lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
161         lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
162         lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
163         lpni->lpni_nid = nid;
164         lpni->lpni_cpt = cpt;
165         lnet_set_peer_ni_health_locked(lpni, true);
166
167         net = lnet_get_net_locked(LNET_NIDNET(nid));
168         lpni->lpni_net = net;
169         if (net) {
170                 lpni->lpni_txcredits = net->net_tunables.lct_peer_tx_credits;
171                 lpni->lpni_mintxcredits = lpni->lpni_txcredits;
172                 lpni->lpni_rtrcredits = lnet_peer_buffer_credits(net);
173                 lpni->lpni_minrtrcredits = lpni->lpni_rtrcredits;
174         } else {
175                 /*
176                  * This peer_ni is not on a local network, so we
177                  * cannot add the credits here. In case the net is
178                  * added later, add the peer_ni to the remote peer ni
179                  * list so it can be easily found and revisited.
180                  */
181                 /* FIXME: per-net implementation instead? */
182                 atomic_inc(&lpni->lpni_refcount);
183                 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
184                               &the_lnet.ln_remote_peer_ni_list);
185         }
186
187         /* TODO: update flags */
188
189         return lpni;
190 }
191
192 static struct lnet_peer_net *
193 lnet_peer_net_alloc(__u32 net_id)
194 {
195         struct lnet_peer_net *lpn;
196
197         LIBCFS_CPT_ALLOC(lpn, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lpn));
198         if (!lpn)
199                 return NULL;
200
201         INIT_LIST_HEAD(&lpn->lpn_on_peer_list);
202         INIT_LIST_HEAD(&lpn->lpn_peer_nis);
203         lpn->lpn_net_id = net_id;
204
205         return lpn;
206 }
207
208 static struct lnet_peer *
209 lnet_peer_alloc(lnet_nid_t nid)
210 {
211         struct lnet_peer *lp;
212
213         LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), CFS_CPT_ANY, sizeof(*lp));
214         if (!lp)
215                 return NULL;
216
217         INIT_LIST_HEAD(&lp->lp_on_lnet_peer_list);
218         INIT_LIST_HEAD(&lp->lp_peer_nets);
219         lp->lp_primary_nid = nid;
220
221         /* TODO: update flags */
222
223         return lp;
224 }
225
226
227 static void
228 lnet_try_destroy_peer_hierarchy_locked(struct lnet_peer_ni *lpni)
229 {
230         struct lnet_peer_net *peer_net;
231         struct lnet_peer *peer;
232
233         /* TODO: could the below situation happen? accessing an already
234          * destroyed peer? */
235         if (lpni->lpni_peer_net == NULL ||
236             lpni->lpni_peer_net->lpn_peer == NULL)
237                 return;
238
239         peer_net = lpni->lpni_peer_net;
240         peer = lpni->lpni_peer_net->lpn_peer;
241
242         list_del_init(&lpni->lpni_on_peer_net_list);
243         lpni->lpni_peer_net = NULL;
244
245         /* if peer_net is empty, then remove it from the peer */
246         if (list_empty(&peer_net->lpn_peer_nis)) {
247                 list_del_init(&peer_net->lpn_on_peer_list);
248                 peer_net->lpn_peer = NULL;
249                 LIBCFS_FREE(peer_net, sizeof(*peer_net));
250
251                 /* if the peer is empty then remove it from the
252                  * the_lnet.ln_peers */
253                 if (list_empty(&peer->lp_peer_nets)) {
254                         list_del_init(&peer->lp_on_lnet_peer_list);
255                         LIBCFS_FREE(peer, sizeof(*peer));
256                 }
257         }
258 }
259
260 /* called with lnet_net_lock LNET_LOCK_EX held */
261 static int
262 lnet_peer_ni_del_locked(struct lnet_peer_ni *lpni)
263 {
264         struct lnet_peer_table *ptable = NULL;
265
266         /* don't remove a peer_ni if it's also a gateway */
267         if (lpni->lpni_rtr_refcount > 0) {
268                 CERROR("Peer NI %s is a gateway. Can not delete it\n",
269                        libcfs_nid2str(lpni->lpni_nid));
270                 return -EBUSY;
271         }
272
273         lnet_peer_remove_from_remote_list(lpni);
274
275         /* remove peer ni from the hash list. */
276         list_del_init(&lpni->lpni_hashlist);
277
278         /* decrement the ref count on the peer table */
279         ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
280         LASSERT(atomic_read(&ptable->pt_number) > 0);
281         atomic_dec(&ptable->pt_number);
282
283         /*
284          * The peer_ni can no longer be found with a lookup. But there
285          * can be current users, so keep track of it on the zombie
286          * list until the reference count has gone to zero.
287          *
288          * The last reference may be lost in a place where the
289          * lnet_net_lock locks only a single cpt, and that cpt may not
290          * be lpni->lpni_cpt. So the zombie list of this peer_table
291          * has its own lock.
292          */
293         spin_lock(&ptable->pt_zombie_lock);
294         list_add(&lpni->lpni_hashlist, &ptable->pt_zombie_list);
295         ptable->pt_zombies++;
296         spin_unlock(&ptable->pt_zombie_lock);
297
298         /* no need to keep this peer on the hierarchy anymore */
299         lnet_try_destroy_peer_hierarchy_locked(lpni);
300
301         /* decrement reference on peer */
302         lnet_peer_ni_decref_locked(lpni);
303
304         return 0;
305 }
306
307 void lnet_peer_uninit(void)
308 {
309         struct lnet_peer_ni *lpni, *tmp;
310
311         lnet_net_lock(LNET_LOCK_EX);
312
313         /* remove all peer_nis from the remote peer and the hash list */
314         list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
315                                  lpni_on_remote_peer_ni_list)
316                 lnet_peer_ni_del_locked(lpni);
317
318         lnet_peer_tables_destroy();
319
320         lnet_net_unlock(LNET_LOCK_EX);
321 }
322
323 static int
324 lnet_peer_del_locked(struct lnet_peer *peer)
325 {
326         struct lnet_peer_ni *lpni = NULL, *lpni2;
327         int rc = 0, rc2 = 0;
328
329         lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
330         while (lpni != NULL) {
331                 lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
332                 rc = lnet_peer_ni_del_locked(lpni);
333                 if (rc != 0)
334                         rc2 = rc;
335                 lpni = lpni2;
336         }
337
338         return rc2;
339 }
340
341 static void
342 lnet_peer_table_cleanup_locked(struct lnet_net *net,
343                                struct lnet_peer_table *ptable)
344 {
345         int                      i;
346         struct lnet_peer_ni     *next;
347         struct lnet_peer_ni     *lpni;
348         struct lnet_peer        *peer;
349
350         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
351                 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
352                                          lpni_hashlist) {
353                         if (net != NULL && net != lpni->lpni_net)
354                                 continue;
355
356                         peer = lpni->lpni_peer_net->lpn_peer;
357                         if (peer->lp_primary_nid != lpni->lpni_nid) {
358                                 lnet_peer_ni_del_locked(lpni);
359                                 continue;
360                         }
361                         /*
362                          * Removing the primary NID implies removing
363                          * the entire peer. Advance next beyond any
364                          * peer_ni that belongs to the same peer.
365                          */
366                         list_for_each_entry_from(next, &ptable->pt_hash[i],
367                                                  lpni_hashlist) {
368                                 if (next->lpni_peer_net->lpn_peer != peer)
369                                         break;
370                         }
371                         lnet_peer_del_locked(peer);
372                 }
373         }
374 }
375
376 static void
377 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
378 {
379         int     i = 3;
380
381         spin_lock(&ptable->pt_zombie_lock);
382         while (ptable->pt_zombies) {
383                 spin_unlock(&ptable->pt_zombie_lock);
384
385                 if (is_power_of_2(i)) {
386                         CDEBUG(D_WARNING,
387                                "Waiting for %d zombies on peer table\n",
388                                ptable->pt_zombies);
389                 }
390                 set_current_state(TASK_UNINTERRUPTIBLE);
391                 schedule_timeout(cfs_time_seconds(1) >> 1);
392                 spin_lock(&ptable->pt_zombie_lock);
393         }
394         spin_unlock(&ptable->pt_zombie_lock);
395 }
396
397 static void
398 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
399                                 struct lnet_peer_table *ptable)
400 {
401         struct lnet_peer_ni     *lp;
402         struct lnet_peer_ni     *tmp;
403         lnet_nid_t              lpni_nid;
404         int                     i;
405
406         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
407                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
408                                          lpni_hashlist) {
409                         if (net != lp->lpni_net)
410                                 continue;
411
412                         if (lp->lpni_rtr_refcount == 0)
413                                 continue;
414
415                         lpni_nid = lp->lpni_nid;
416
417                         lnet_net_unlock(LNET_LOCK_EX);
418                         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
419                         lnet_net_lock(LNET_LOCK_EX);
420                 }
421         }
422 }
423
424 void
425 lnet_peer_tables_cleanup(struct lnet_net *net)
426 {
427         int                             i;
428         struct lnet_peer_table          *ptable;
429
430         LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
431         /* If just deleting the peers for a NI, get rid of any routes these
432          * peers are gateways for. */
433         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
434                 lnet_net_lock(LNET_LOCK_EX);
435                 lnet_peer_table_del_rtrs_locked(net, ptable);
436                 lnet_net_unlock(LNET_LOCK_EX);
437         }
438
439         /* Start the cleanup process */
440         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
441                 lnet_net_lock(LNET_LOCK_EX);
442                 lnet_peer_table_cleanup_locked(net, ptable);
443                 lnet_net_unlock(LNET_LOCK_EX);
444         }
445
446         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
447                 lnet_peer_ni_finalize_wait(ptable);
448 }
449
450 static struct lnet_peer_ni *
451 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
452 {
453         struct list_head        *peers;
454         struct lnet_peer_ni     *lp;
455
456         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
457
458         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
459         list_for_each_entry(lp, peers, lpni_hashlist) {
460                 if (lp->lpni_nid == nid) {
461                         lnet_peer_ni_addref_locked(lp);
462                         return lp;
463                 }
464         }
465
466         return NULL;
467 }
468
469 struct lnet_peer_ni *
470 lnet_find_peer_ni_locked(lnet_nid_t nid)
471 {
472         struct lnet_peer_ni *lpni;
473         struct lnet_peer_table *ptable;
474         int cpt;
475
476         cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
477
478         ptable = the_lnet.ln_peer_tables[cpt];
479         lpni = lnet_get_peer_ni_locked(ptable, nid);
480
481         return lpni;
482 }
483
484 struct lnet_peer *
485 lnet_find_or_create_peer_locked(lnet_nid_t dst_nid, int cpt)
486 {
487         struct lnet_peer_ni *lpni;
488         struct lnet_peer *lp;
489
490         lpni = lnet_find_peer_ni_locked(dst_nid);
491         if (!lpni) {
492                 lpni = lnet_nid2peerni_locked(dst_nid, cpt);
493                 if (IS_ERR(lpni))
494                         return ERR_CAST(lpni);
495         }
496
497         lp = lpni->lpni_peer_net->lpn_peer;
498         lnet_peer_ni_decref_locked(lpni);
499
500         return lp;
501 }
502
503 struct lnet_peer_ni *
504 lnet_get_peer_ni_idx_locked(int idx, struct lnet_peer_net **lpn,
505                             struct lnet_peer **lp)
506 {
507         struct lnet_peer_ni     *lpni;
508
509         list_for_each_entry((*lp), &the_lnet.ln_peers, lp_on_lnet_peer_list) {
510                 list_for_each_entry((*lpn), &((*lp)->lp_peer_nets), lpn_on_peer_list) {
511                         list_for_each_entry(lpni, &((*lpn)->lpn_peer_nis),
512                                             lpni_on_peer_net_list)
513                                 if (idx-- == 0)
514                                         return lpni;
515                 }
516         }
517
518         return NULL;
519 }
520
521 struct lnet_peer_ni *
522 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
523                              struct lnet_peer_net *peer_net,
524                              struct lnet_peer_ni *prev)
525 {
526         struct lnet_peer_ni *lpni;
527         struct lnet_peer_net *net = peer_net;
528
529         if (!prev) {
530                 if (!net)
531                         net = list_entry(peer->lp_peer_nets.next,
532                                          struct lnet_peer_net,
533                                          lpn_on_peer_list);
534                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
535                                   lpni_on_peer_net_list);
536
537                 return lpni;
538         }
539
540         if (prev->lpni_on_peer_net_list.next ==
541             &prev->lpni_peer_net->lpn_peer_nis) {
542                 /*
543                  * if you reached the end of the peer ni list and the peer
544                  * net is specified then there are no more peer nis in that
545                  * net.
546                  */
547                 if (net)
548                         return NULL;
549
550                 /*
551                  * we reached the end of this net ni list. move to the
552                  * next net
553                  */
554                 if (prev->lpni_peer_net->lpn_on_peer_list.next ==
555                     &peer->lp_peer_nets)
556                         /* no more nets and no more NIs. */
557                         return NULL;
558
559                 /* get the next net */
560                 net = list_entry(prev->lpni_peer_net->lpn_on_peer_list.next,
561                                  struct lnet_peer_net,
562                                  lpn_on_peer_list);
563                 /* get the ni on it */
564                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
565                                   lpni_on_peer_net_list);
566
567                 return lpni;
568         }
569
570         /* there are more nis left */
571         lpni = list_entry(prev->lpni_on_peer_net_list.next,
572                           struct lnet_peer_ni, lpni_on_peer_net_list);
573
574         return lpni;
575 }
576
577 bool
578 lnet_peer_is_ni_pref_locked(struct lnet_peer_ni *lpni, struct lnet_ni *ni)
579 {
580         int i;
581
582         for (i = 0; i < lpni->lpni_pref_nnids; i++) {
583                 if (lpni->lpni_pref_nids[i] == ni->ni_nid)
584                         return true;
585         }
586         return false;
587 }
588
589 lnet_nid_t
590 lnet_peer_primary_nid(lnet_nid_t nid)
591 {
592         struct lnet_peer_ni *lpni;
593         lnet_nid_t primary_nid = nid;
594         int cpt;
595
596         cpt = lnet_net_lock_current();
597         lpni = lnet_find_peer_ni_locked(nid);
598         if (lpni) {
599                 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
600                 lnet_peer_ni_decref_locked(lpni);
601         }
602         lnet_net_unlock(cpt);
603
604         return primary_nid;
605 }
606
607 lnet_nid_t
608 LNetPrimaryNID(lnet_nid_t nid)
609 {
610         struct lnet_peer_ni *lpni;
611         lnet_nid_t primary_nid = nid;
612         int rc = 0;
613         int cpt;
614
615         cpt = lnet_net_lock_current();
616         lpni = lnet_nid2peerni_locked(nid, cpt);
617         if (IS_ERR(lpni)) {
618                 rc = PTR_ERR(lpni);
619                 goto out_unlock;
620         }
621         primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
622         lnet_peer_ni_decref_locked(lpni);
623 out_unlock:
624         lnet_net_unlock(cpt);
625
626         CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
627                libcfs_nid2str(primary_nid), rc);
628         return primary_nid;
629 }
630 EXPORT_SYMBOL(LNetPrimaryNID);
631
632 struct lnet_peer_net *
633 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
634 {
635         struct lnet_peer_net *peer_net;
636         list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
637                 if (peer_net->lpn_net_id == net_id)
638                         return peer_net;
639         }
640         return NULL;
641 }
642
643 static int
644 lnet_peer_setup_hierarchy(struct lnet_peer *lp, struct lnet_peer_ni *lpni,
645                           lnet_nid_t nid)
646 {
647         struct lnet_peer_net *lpn = NULL;
648         struct lnet_peer_table *ptable;
649         __u32 net_id = LNET_NIDNET(nid);
650
651         /*
652          * Create the peer_ni, peer_net, and peer if they don't exist
653          * yet.
654          */
655         if (lp) {
656                 lpn = lnet_peer_get_net_locked(lp, net_id);
657         } else {
658                 lp = lnet_peer_alloc(nid);
659                 if (!lp)
660                         goto out_enomem;
661         }
662
663         if (!lpn) {
664                 lpn = lnet_peer_net_alloc(net_id);
665                 if (!lpn)
666                         goto out_maybe_free_lp;
667         }
668
669         if (!lpni) {
670                 lpni = lnet_peer_ni_alloc(nid);
671                 if (!lpni)
672                         goto out_maybe_free_lpn;
673         }
674
675         /* Install the new peer_ni */
676         lnet_net_lock(LNET_LOCK_EX);
677         /* Add peer_ni to global peer table hash, if necessary. */
678         if (list_empty(&lpni->lpni_hashlist)) {
679                 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
680                 list_add_tail(&lpni->lpni_hashlist,
681                               &ptable->pt_hash[lnet_nid2peerhash(nid)]);
682                 ptable->pt_version++;
683                 atomic_inc(&ptable->pt_number);
684                 atomic_inc(&lpni->lpni_refcount);
685         }
686
687         /* Detach the peer_ni from an existing peer, if necessary. */
688         if (lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer != lp)
689                 lnet_try_destroy_peer_hierarchy_locked(lpni);
690
691         /* Add peer_ni to peer_net */
692         lpni->lpni_peer_net = lpn;
693         list_add_tail(&lpni->lpni_on_peer_net_list, &lpn->lpn_peer_nis);
694
695         /* Add peer_net to peer */
696         if (!lpn->lpn_peer) {
697                 lpn->lpn_peer = lp;
698                 list_add_tail(&lpn->lpn_on_peer_list, &lp->lp_peer_nets);
699         }
700
701         /* Add peer to global peer list */
702         if (list_empty(&lp->lp_on_lnet_peer_list))
703                 list_add_tail(&lp->lp_on_lnet_peer_list, &the_lnet.ln_peers);
704         lnet_net_unlock(LNET_LOCK_EX);
705
706         return 0;
707
708 out_maybe_free_lpn:
709         if (list_empty(&lpn->lpn_on_peer_list))
710                 LIBCFS_FREE(lpn, sizeof(*lpn));
711 out_maybe_free_lp:
712         if (list_empty(&lp->lp_on_lnet_peer_list))
713                 LIBCFS_FREE(lp, sizeof(*lp));
714 out_enomem:
715         return -ENOMEM;
716 }
717
718 static int
719 lnet_add_prim_lpni(lnet_nid_t nid)
720 {
721         int rc = 0;
722         struct lnet_peer *peer;
723         struct lnet_peer_ni *lpni;
724
725         LASSERT(nid != LNET_NID_ANY);
726
727         /*
728          * lookup the NID and its peer
729          *  if the peer doesn't exist, create it.
730          *  if this is a non-MR peer then change its state to MR and exit.
731          *  if this is an MR peer and it's a primary NI: NO-OP.
732          *  if this is an MR peer and it's not a primary NI. Operation not
733          *     allowed.
734          *
735          * The adding and deleting of peer nis is being serialized through
736          * the api_mutex. So we can look up peers with the mutex locked
737          * safely. Only when we need to change the ptable, do we need to
738          * exclusively lock the lnet_net_lock()
739          */
740         lpni = lnet_find_peer_ni_locked(nid);
741         if (!lpni) {
742                 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
743                 if (rc != 0)
744                         return rc;
745                 lpni = lnet_find_peer_ni_locked(nid);
746         }
747
748         LASSERT(lpni);
749
750         lnet_peer_ni_decref_locked(lpni);
751
752         peer = lpni->lpni_peer_net->lpn_peer;
753
754         /*
755          * If we found a lpni with the same nid as the NID we're trying to
756          * create, then we're trying to create an already existing lpni 
757          * that belongs to a different peer
758          */
759         if (peer->lp_primary_nid != nid)
760                 return -EEXIST;
761
762         /*
763          * if we found an lpni that is not a multi-rail, which could occur
764          * if lpni is already created as a non-mr lpni or we just created
765          * it, then make sure you indicate that this lpni is a primary mr
766          * capable peer.
767          *
768          * TODO: update flags if necessary
769          */
770         if (!peer->lp_multi_rail && peer->lp_primary_nid == nid)
771                 peer->lp_multi_rail = true;
772
773         return rc;
774 }
775
776 static int
777 lnet_add_peer_ni_to_prim_lpni(lnet_nid_t prim_nid, lnet_nid_t nid)
778 {
779         struct lnet_peer *peer, *primary_peer;
780         struct lnet_peer_ni *lpni = NULL, *klpni = NULL;
781
782         LASSERT(prim_nid != LNET_NID_ANY && nid != LNET_NID_ANY);
783
784         /*
785          * key nid must be created by this point. If not then this
786          * operation is not permitted
787          */
788         klpni = lnet_find_peer_ni_locked(prim_nid);
789         if (!klpni)
790                 return -ENOENT;
791
792         lnet_peer_ni_decref_locked(klpni);
793
794         primary_peer = klpni->lpni_peer_net->lpn_peer;
795
796         lpni = lnet_find_peer_ni_locked(nid);
797         if (lpni) {
798                 lnet_peer_ni_decref_locked(lpni);
799
800                 peer = lpni->lpni_peer_net->lpn_peer;
801                 /*
802                  * lpni already exists in the system but it belongs to
803                  * a different peer. We can't re-added it
804                  */
805                 if (peer->lp_primary_nid != prim_nid && peer->lp_multi_rail) {
806                         CERROR("Cannot add NID %s owned by peer %s to peer %s\n",
807                                libcfs_nid2str(lpni->lpni_nid),
808                                libcfs_nid2str(peer->lp_primary_nid),
809                                libcfs_nid2str(prim_nid));
810                         return -EEXIST;
811                 } else if (peer->lp_primary_nid == prim_nid) {
812                         /*
813                          * found a peer_ni that is already part of the
814                          * peer. This is a no-op operation.
815                          */
816                         return 0;
817                 }
818
819                 /*
820                  * TODO: else if (peer->lp_primary_nid != prim_nid &&
821                  *                !peer->lp_multi_rail)
822                  * peer is not an MR peer and it will be moved in the next
823                  * step to klpni, so update its flags accordingly.
824                  * lnet_move_peer_ni()
825                  */
826
827                 /*
828                  * TODO: call lnet_update_peer() from here to update the
829                  * flags. This is the case when the lpni you're trying to
830                  * add is already part of the peer. This could've been
831                  * added by the DD previously, so go ahead and do any
832                  * updates to the state if necessary
833                  */
834
835         }
836
837         /*
838          * When we get here we either have found an existing lpni, which
839          * we can switch to the new peer. Or we need to create one and
840          * add it to the new peer
841          */
842         return lnet_peer_setup_hierarchy(primary_peer, lpni, nid);
843 }
844
845 /*
846  * lpni creation initiated due to traffic either sending or receiving.
847  */
848 static int
849 lnet_peer_ni_traffic_add(lnet_nid_t nid)
850 {
851         struct lnet_peer_ni *lpni;
852         int rc = 0;
853
854         if (nid == LNET_NID_ANY)
855                 return -EINVAL;
856
857         /* lnet_net_lock is not needed here because ln_api_lock is held */
858         lpni = lnet_find_peer_ni_locked(nid);
859         if (lpni) {
860                 /*
861                  * TODO: lnet_update_primary_nid() but not all of it
862                  * only indicate if we're converting this to MR capable
863                  * Can happen due to DD
864                  */
865                 lnet_peer_ni_decref_locked(lpni);
866         } else {
867                 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
868         }
869
870         return rc;
871
872 }
873
874 static int
875 lnet_peer_ni_add_non_mr(lnet_nid_t nid)
876 {
877         struct lnet_peer_ni *lpni;
878
879         lpni = lnet_find_peer_ni_locked(nid);
880         if (lpni) {
881                 CERROR("Cannot add %s as non-mr when it already exists\n",
882                        libcfs_nid2str(nid));
883                 lnet_peer_ni_decref_locked(lpni);
884                 return -EEXIST;
885         }
886
887         return lnet_peer_setup_hierarchy(NULL, NULL, nid);
888 }
889
890 /*
891  * This API handles the following combinations:
892  *      Create a primary NI if only the prim_nid is provided
893  *      Create or add an lpni to a primary NI. Primary NI must've already
894  *      been created
895  *      Create a non-MR peer.
896  */
897 int
898 lnet_add_peer_ni_to_peer(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
899 {
900         /*
901          * Caller trying to setup an MR like peer hierarchy but
902          * specifying it to be non-MR. This is not allowed.
903          */
904         if (prim_nid != LNET_NID_ANY &&
905             nid != LNET_NID_ANY && !mr)
906                 return -EPERM;
907
908         /* Add the primary NID of a peer */
909         if (prim_nid != LNET_NID_ANY &&
910             nid == LNET_NID_ANY && mr)
911                 return lnet_add_prim_lpni(prim_nid);
912
913         /* Add a NID to an existing peer */
914         if (prim_nid != LNET_NID_ANY &&
915             nid != LNET_NID_ANY && mr)
916                 return lnet_add_peer_ni_to_prim_lpni(prim_nid, nid);
917
918         /* Add a non-MR peer NI */
919         if (((prim_nid != LNET_NID_ANY &&
920               nid == LNET_NID_ANY) ||
921              (prim_nid == LNET_NID_ANY &&
922               nid != LNET_NID_ANY)) && !mr)
923                 return lnet_peer_ni_add_non_mr(prim_nid != LNET_NID_ANY ?
924                                                          prim_nid : nid);
925
926         return 0;
927 }
928
929 int
930 lnet_del_peer_ni_from_peer(lnet_nid_t prim_nid, lnet_nid_t nid)
931 {
932         lnet_nid_t local_nid;
933         struct lnet_peer *peer;
934         struct lnet_peer_ni *lpni;
935         int rc;
936
937         if (prim_nid == LNET_NID_ANY)
938                 return -EINVAL;
939
940         local_nid = (nid != LNET_NID_ANY) ? nid : prim_nid;
941
942         lpni = lnet_find_peer_ni_locked(local_nid);
943         if (!lpni)
944                 return -EINVAL;
945         lnet_peer_ni_decref_locked(lpni);
946
947         peer = lpni->lpni_peer_net->lpn_peer;
948         LASSERT(peer != NULL);
949
950         if (peer->lp_primary_nid == lpni->lpni_nid) {
951                 /*
952                  * deleting the primary ni is equivalent to deleting the
953                  * entire peer
954                  */
955                 lnet_net_lock(LNET_LOCK_EX);
956                 rc = lnet_peer_del_locked(peer);
957                 lnet_net_unlock(LNET_LOCK_EX);
958
959                 return rc;
960         }
961
962         lnet_net_lock(LNET_LOCK_EX);
963         rc = lnet_peer_ni_del_locked(lpni);
964         lnet_net_unlock(LNET_LOCK_EX);
965
966         return rc;
967 }
968
969 void
970 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
971 {
972         struct lnet_peer_table *ptable;
973
974         LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
975         LASSERT(lpni->lpni_rtr_refcount == 0);
976         LASSERT(list_empty(&lpni->lpni_txq));
977         LASSERT(lpni->lpni_txqnob == 0);
978
979         lpni->lpni_net = NULL;
980
981         /* remove the peer ni from the zombie list */
982         ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
983         spin_lock(&ptable->pt_zombie_lock);
984         list_del_init(&lpni->lpni_hashlist);
985         ptable->pt_zombies--;
986         spin_unlock(&ptable->pt_zombie_lock);
987
988         LIBCFS_FREE(lpni, sizeof(*lpni));
989 }
990
991 struct lnet_peer_ni *
992 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
993 {
994         struct lnet_peer_ni *lpni = NULL;
995         int rc;
996
997         if (the_lnet.ln_state != LNET_STATE_RUNNING)
998                 return ERR_PTR(-ESHUTDOWN);
999
1000         /*
1001          * find if a peer_ni already exists.
1002          * If so then just return that.
1003          */
1004         lpni = lnet_find_peer_ni_locked(nid);
1005         if (lpni)
1006                 return lpni;
1007
1008         lnet_net_unlock(cpt);
1009
1010         rc = lnet_peer_ni_traffic_add(nid);
1011         if (rc) {
1012                 lpni = ERR_PTR(rc);
1013                 goto out_net_relock;
1014         }
1015
1016         lpni = lnet_find_peer_ni_locked(nid);
1017         LASSERT(lpni);
1018
1019 out_net_relock:
1020         lnet_net_lock(cpt);
1021
1022         return lpni;
1023 }
1024
1025 struct lnet_peer_ni *
1026 lnet_nid2peerni_locked(lnet_nid_t nid, int cpt)
1027 {
1028         struct lnet_peer_ni *lpni = NULL;
1029         int rc;
1030
1031         if (the_lnet.ln_state != LNET_STATE_RUNNING)
1032                 return ERR_PTR(-ESHUTDOWN);
1033
1034         /*
1035          * find if a peer_ni already exists.
1036          * If so then just return that.
1037          */
1038         lpni = lnet_find_peer_ni_locked(nid);
1039         if (lpni)
1040                 return lpni;
1041
1042         /*
1043          * Slow path:
1044          * use the lnet_api_mutex to serialize the creation of the peer_ni
1045          * and the creation/deletion of the local ni/net. When a local ni is
1046          * created, if there exists a set of peer_nis on that network,
1047          * they need to be traversed and updated. When a local NI is
1048          * deleted, which could result in a network being deleted, then
1049          * all peer nis on that network need to be removed as well.
1050          *
1051          * Creation through traffic should also be serialized with
1052          * creation through DLC.
1053          */
1054         lnet_net_unlock(cpt);
1055         mutex_lock(&the_lnet.ln_api_mutex);
1056         /*
1057          * Shutdown is only set under the ln_api_lock, so a single
1058          * check here is sufficent.
1059          */
1060         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1061                 lpni = ERR_PTR(-ESHUTDOWN);
1062                 goto out_mutex_unlock;
1063         }
1064
1065         rc = lnet_peer_ni_traffic_add(nid);
1066         if (rc) {
1067                 lpni = ERR_PTR(rc);
1068                 goto out_mutex_unlock;
1069         }
1070
1071         lpni = lnet_find_peer_ni_locked(nid);
1072         LASSERT(lpni);
1073
1074 out_mutex_unlock:
1075         mutex_unlock(&the_lnet.ln_api_mutex);
1076         lnet_net_lock(cpt);
1077
1078         return lpni;
1079 }
1080
1081 void
1082 lnet_debug_peer(lnet_nid_t nid)
1083 {
1084         char                    *aliveness = "NA";
1085         struct lnet_peer_ni     *lp;
1086         int                     cpt;
1087
1088         cpt = lnet_cpt_of_nid(nid, NULL);
1089         lnet_net_lock(cpt);
1090
1091         lp = lnet_nid2peerni_locked(nid, cpt);
1092         if (IS_ERR(lp)) {
1093                 lnet_net_unlock(cpt);
1094                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
1095                 return;
1096         }
1097
1098         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
1099                 aliveness = lp->lpni_alive ? "up" : "down";
1100
1101         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
1102                libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
1103                aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
1104                lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
1105                lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
1106
1107         lnet_peer_ni_decref_locked(lp);
1108
1109         lnet_net_unlock(cpt);
1110 }
1111
1112 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
1113                           char aliveness[LNET_MAX_STR_LEN],
1114                           __u32 *cpt_iter, __u32 *refcount,
1115                           __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
1116                           __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
1117                           __u32 *peer_tx_qnob)
1118 {
1119         struct lnet_peer_table          *peer_table;
1120         struct lnet_peer_ni             *lp;
1121         int                             j;
1122         int                             lncpt;
1123         bool                            found = false;
1124
1125         /* get the number of CPTs */
1126         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
1127
1128         /* if the cpt number to be examined is >= the number of cpts in
1129          * the system then indicate that there are no more cpts to examin
1130          */
1131         if (*cpt_iter >= lncpt)
1132                 return -ENOENT;
1133
1134         /* get the current table */
1135         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
1136         /* if the ptable is NULL then there are no more cpts to examine */
1137         if (peer_table == NULL)
1138                 return -ENOENT;
1139
1140         lnet_net_lock(*cpt_iter);
1141
1142         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
1143                 struct list_head *peers = &peer_table->pt_hash[j];
1144
1145                 list_for_each_entry(lp, peers, lpni_hashlist) {
1146                         if (peer_index-- > 0)
1147                                 continue;
1148
1149                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
1150                         if (lnet_isrouter(lp) ||
1151                                 lnet_peer_aliveness_enabled(lp))
1152                                 snprintf(aliveness, LNET_MAX_STR_LEN,
1153                                          lp->lpni_alive ? "up" : "down");
1154
1155                         *nid = lp->lpni_nid;
1156                         *refcount = atomic_read(&lp->lpni_refcount);
1157                         *ni_peer_tx_credits =
1158                                 lp->lpni_net->net_tunables.lct_peer_tx_credits;
1159                         *peer_tx_credits = lp->lpni_txcredits;
1160                         *peer_rtr_credits = lp->lpni_rtrcredits;
1161                         *peer_min_rtr_credits = lp->lpni_mintxcredits;
1162                         *peer_tx_qnob = lp->lpni_txqnob;
1163
1164                         found = true;
1165                 }
1166
1167         }
1168         lnet_net_unlock(*cpt_iter);
1169
1170         *cpt_iter = lncpt;
1171
1172         return found ? 0 : -ENOENT;
1173 }
1174
1175 int lnet_get_peer_info(__u32 idx, lnet_nid_t *primary_nid, lnet_nid_t *nid,
1176                        bool *mr,
1177                        struct lnet_peer_ni_credit_info __user *peer_ni_info,
1178                        struct lnet_ioctl_element_stats __user *peer_ni_stats)
1179 {
1180         struct lnet_peer_ni *lpni = NULL;
1181         struct lnet_peer_net *lpn = NULL;
1182         struct lnet_peer *lp = NULL;
1183         struct lnet_peer_ni_credit_info ni_info;
1184         struct lnet_ioctl_element_stats ni_stats;
1185         int rc;
1186
1187         lpni = lnet_get_peer_ni_idx_locked(idx, &lpn, &lp);
1188
1189         if (!lpni)
1190                 return -ENOENT;
1191
1192         *primary_nid = lp->lp_primary_nid;
1193         *mr = lp->lp_multi_rail;
1194         *nid = lpni->lpni_nid;
1195         snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN, "NA");
1196         if (lnet_isrouter(lpni) ||
1197                 lnet_peer_aliveness_enabled(lpni))
1198                 snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN,
1199                          lpni->lpni_alive ? "up" : "down");
1200
1201         ni_info.cr_refcount = atomic_read(&lpni->lpni_refcount);
1202         ni_info.cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
1203                 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
1204         ni_info.cr_peer_tx_credits = lpni->lpni_txcredits;
1205         ni_info.cr_peer_rtr_credits = lpni->lpni_rtrcredits;
1206         ni_info.cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
1207         ni_info.cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
1208         ni_info.cr_peer_tx_qnob = lpni->lpni_txqnob;
1209         ni_info.cr_ncpt = lpni->lpni_cpt;
1210
1211         ni_stats.iel_send_count = atomic_read(&lpni->lpni_stats.send_count);
1212         ni_stats.iel_recv_count = atomic_read(&lpni->lpni_stats.recv_count);
1213         ni_stats.iel_drop_count = atomic_read(&lpni->lpni_stats.drop_count);
1214
1215         /* If copy_to_user fails */
1216         rc = -EFAULT;
1217         if (copy_to_user(peer_ni_info, &ni_info, sizeof(ni_info)))
1218                 goto copy_failed;
1219
1220         if (copy_to_user(peer_ni_stats, &ni_stats, sizeof(ni_stats)))
1221                 goto copy_failed;
1222
1223         rc = 0;
1224
1225 copy_failed:
1226         return rc;
1227 }