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