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