Whamcloud - gitweb
LU-9480 lnet: refactor lnet_del_peer_ni()
[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 <uapi/linux/lnet/lnet-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 lnet_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 int
342 lnet_peer_del(struct lnet_peer *peer)
343 {
344         lnet_net_lock(LNET_LOCK_EX);
345         lnet_peer_del_locked(peer);
346         lnet_net_unlock(LNET_LOCK_EX);
347
348         return 0;
349 }
350
351 /*
352  * Delete a NID from a peer.
353  * Implements a few sanity checks.
354  * Call with ln_api_mutex held.
355  */
356 static int
357 lnet_peer_del_nid(struct lnet_peer *lp, lnet_nid_t nid)
358 {
359         struct lnet_peer *lp2;
360         struct lnet_peer_ni *lpni;
361
362         lpni = lnet_find_peer_ni_locked(nid);
363         if (!lpni) {
364                 CERROR("Cannot remove unknown nid %s from peer %s\n",
365                        libcfs_nid2str(nid),
366                        libcfs_nid2str(lp->lp_primary_nid));
367                 return -ENOENT;
368         }
369         lnet_peer_ni_decref_locked(lpni);
370         lp2 = lpni->lpni_peer_net->lpn_peer;
371         if (lp2 != lp) {
372                 CERROR("Nid %s is attached to peer %s, not peer %s\n",
373                        libcfs_nid2str(nid),
374                        libcfs_nid2str(lp2->lp_primary_nid),
375                        libcfs_nid2str(lp->lp_primary_nid));
376                 return -EINVAL;
377         }
378
379         /*
380          * This function only allows deletion of the primary NID if it
381          * is the only NID.
382          */
383         if (nid == lp->lp_primary_nid && lnet_get_num_peer_nis(lp) != 1) {
384                 CERROR("Cannot delete primary NID %s from multi-NID peer\n",
385                        libcfs_nid2str(nid));
386                 return -EINVAL;
387         }
388
389         lnet_net_lock(LNET_LOCK_EX);
390         lnet_peer_ni_del_locked(lpni);
391         lnet_net_unlock(LNET_LOCK_EX);
392
393         return 0;
394 }
395
396 static void
397 lnet_peer_table_cleanup_locked(struct lnet_net *net,
398                                struct lnet_peer_table *ptable)
399 {
400         int                      i;
401         struct lnet_peer_ni     *next;
402         struct lnet_peer_ni     *lpni;
403         struct lnet_peer        *peer;
404
405         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
406                 list_for_each_entry_safe(lpni, next, &ptable->pt_hash[i],
407                                          lpni_hashlist) {
408                         if (net != NULL && net != lpni->lpni_net)
409                                 continue;
410
411                         peer = lpni->lpni_peer_net->lpn_peer;
412                         if (peer->lp_primary_nid != lpni->lpni_nid) {
413                                 lnet_peer_ni_del_locked(lpni);
414                                 continue;
415                         }
416                         /*
417                          * Removing the primary NID implies removing
418                          * the entire peer. Advance next beyond any
419                          * peer_ni that belongs to the same peer.
420                          */
421                         list_for_each_entry_from(next, &ptable->pt_hash[i],
422                                                  lpni_hashlist) {
423                                 if (next->lpni_peer_net->lpn_peer != peer)
424                                         break;
425                         }
426                         lnet_peer_del_locked(peer);
427                 }
428         }
429 }
430
431 static void
432 lnet_peer_ni_finalize_wait(struct lnet_peer_table *ptable)
433 {
434         int     i = 3;
435
436         spin_lock(&ptable->pt_zombie_lock);
437         while (ptable->pt_zombies) {
438                 spin_unlock(&ptable->pt_zombie_lock);
439
440                 if (is_power_of_2(i)) {
441                         CDEBUG(D_WARNING,
442                                "Waiting for %d zombies on peer table\n",
443                                ptable->pt_zombies);
444                 }
445                 set_current_state(TASK_UNINTERRUPTIBLE);
446                 schedule_timeout(cfs_time_seconds(1) >> 1);
447                 spin_lock(&ptable->pt_zombie_lock);
448         }
449         spin_unlock(&ptable->pt_zombie_lock);
450 }
451
452 static void
453 lnet_peer_table_del_rtrs_locked(struct lnet_net *net,
454                                 struct lnet_peer_table *ptable)
455 {
456         struct lnet_peer_ni     *lp;
457         struct lnet_peer_ni     *tmp;
458         lnet_nid_t              lpni_nid;
459         int                     i;
460
461         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
462                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
463                                          lpni_hashlist) {
464                         if (net != lp->lpni_net)
465                                 continue;
466
467                         if (lp->lpni_rtr_refcount == 0)
468                                 continue;
469
470                         lpni_nid = lp->lpni_nid;
471
472                         lnet_net_unlock(LNET_LOCK_EX);
473                         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
474                         lnet_net_lock(LNET_LOCK_EX);
475                 }
476         }
477 }
478
479 void
480 lnet_peer_tables_cleanup(struct lnet_net *net)
481 {
482         int                             i;
483         struct lnet_peer_table          *ptable;
484
485         LASSERT(the_lnet.ln_state != LNET_STATE_SHUTDOWN || net != NULL);
486         /* If just deleting the peers for a NI, get rid of any routes these
487          * peers are gateways for. */
488         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
489                 lnet_net_lock(LNET_LOCK_EX);
490                 lnet_peer_table_del_rtrs_locked(net, ptable);
491                 lnet_net_unlock(LNET_LOCK_EX);
492         }
493
494         /* Start the cleanup process */
495         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
496                 lnet_net_lock(LNET_LOCK_EX);
497                 lnet_peer_table_cleanup_locked(net, ptable);
498                 lnet_net_unlock(LNET_LOCK_EX);
499         }
500
501         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables)
502                 lnet_peer_ni_finalize_wait(ptable);
503 }
504
505 static struct lnet_peer_ni *
506 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
507 {
508         struct list_head        *peers;
509         struct lnet_peer_ni     *lp;
510
511         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
512
513         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
514         list_for_each_entry(lp, peers, lpni_hashlist) {
515                 if (lp->lpni_nid == nid) {
516                         lnet_peer_ni_addref_locked(lp);
517                         return lp;
518                 }
519         }
520
521         return NULL;
522 }
523
524 struct lnet_peer_ni *
525 lnet_find_peer_ni_locked(lnet_nid_t nid)
526 {
527         struct lnet_peer_ni *lpni;
528         struct lnet_peer_table *ptable;
529         int cpt;
530
531         cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
532
533         ptable = the_lnet.ln_peer_tables[cpt];
534         lpni = lnet_get_peer_ni_locked(ptable, nid);
535
536         return lpni;
537 }
538
539 struct lnet_peer *
540 lnet_find_or_create_peer_locked(lnet_nid_t dst_nid, int cpt)
541 {
542         struct lnet_peer_ni *lpni;
543         struct lnet_peer *lp;
544
545         lpni = lnet_find_peer_ni_locked(dst_nid);
546         if (!lpni) {
547                 lpni = lnet_nid2peerni_locked(dst_nid, cpt);
548                 if (IS_ERR(lpni))
549                         return ERR_CAST(lpni);
550         }
551
552         lp = lpni->lpni_peer_net->lpn_peer;
553         lnet_peer_ni_decref_locked(lpni);
554
555         return lp;
556 }
557
558 struct lnet_peer_ni *
559 lnet_get_peer_ni_idx_locked(int idx, struct lnet_peer_net **lpn,
560                             struct lnet_peer **lp)
561 {
562         struct lnet_peer_ni     *lpni;
563
564         list_for_each_entry((*lp), &the_lnet.ln_peers, lp_on_lnet_peer_list) {
565                 list_for_each_entry((*lpn), &((*lp)->lp_peer_nets), lpn_on_peer_list) {
566                         list_for_each_entry(lpni, &((*lpn)->lpn_peer_nis),
567                                             lpni_on_peer_net_list)
568                                 if (idx-- == 0)
569                                         return lpni;
570                 }
571         }
572
573         return NULL;
574 }
575
576 struct lnet_peer_ni *
577 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
578                              struct lnet_peer_net *peer_net,
579                              struct lnet_peer_ni *prev)
580 {
581         struct lnet_peer_ni *lpni;
582         struct lnet_peer_net *net = peer_net;
583
584         if (!prev) {
585                 if (!net)
586                         net = list_entry(peer->lp_peer_nets.next,
587                                          struct lnet_peer_net,
588                                          lpn_on_peer_list);
589                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
590                                   lpni_on_peer_net_list);
591
592                 return lpni;
593         }
594
595         if (prev->lpni_on_peer_net_list.next ==
596             &prev->lpni_peer_net->lpn_peer_nis) {
597                 /*
598                  * if you reached the end of the peer ni list and the peer
599                  * net is specified then there are no more peer nis in that
600                  * net.
601                  */
602                 if (net)
603                         return NULL;
604
605                 /*
606                  * we reached the end of this net ni list. move to the
607                  * next net
608                  */
609                 if (prev->lpni_peer_net->lpn_on_peer_list.next ==
610                     &peer->lp_peer_nets)
611                         /* no more nets and no more NIs. */
612                         return NULL;
613
614                 /* get the next net */
615                 net = list_entry(prev->lpni_peer_net->lpn_on_peer_list.next,
616                                  struct lnet_peer_net,
617                                  lpn_on_peer_list);
618                 /* get the ni on it */
619                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
620                                   lpni_on_peer_net_list);
621
622                 return lpni;
623         }
624
625         /* there are more nis left */
626         lpni = list_entry(prev->lpni_on_peer_net_list.next,
627                           struct lnet_peer_ni, lpni_on_peer_net_list);
628
629         return lpni;
630 }
631
632 bool
633 lnet_peer_is_ni_pref_locked(struct lnet_peer_ni *lpni, struct lnet_ni *ni)
634 {
635         int i;
636
637         for (i = 0; i < lpni->lpni_pref_nnids; i++) {
638                 if (lpni->lpni_pref_nids[i] == ni->ni_nid)
639                         return true;
640         }
641         return false;
642 }
643
644 lnet_nid_t
645 lnet_peer_primary_nid_locked(lnet_nid_t nid)
646 {
647         struct lnet_peer_ni *lpni;
648         lnet_nid_t primary_nid = nid;
649
650         lpni = lnet_find_peer_ni_locked(nid);
651         if (lpni) {
652                 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
653                 lnet_peer_ni_decref_locked(lpni);
654         }
655
656         return primary_nid;
657 }
658
659 lnet_nid_t
660 LNetPrimaryNID(lnet_nid_t nid)
661 {
662         struct lnet_peer_ni *lpni;
663         lnet_nid_t primary_nid = nid;
664         int rc = 0;
665         int cpt;
666
667         cpt = lnet_net_lock_current();
668         lpni = lnet_nid2peerni_locked(nid, cpt);
669         if (IS_ERR(lpni)) {
670                 rc = PTR_ERR(lpni);
671                 goto out_unlock;
672         }
673         primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
674         lnet_peer_ni_decref_locked(lpni);
675 out_unlock:
676         lnet_net_unlock(cpt);
677
678         CDEBUG(D_NET, "NID %s primary NID %s rc %d\n", libcfs_nid2str(nid),
679                libcfs_nid2str(primary_nid), rc);
680         return primary_nid;
681 }
682 EXPORT_SYMBOL(LNetPrimaryNID);
683
684 struct lnet_peer_net *
685 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
686 {
687         struct lnet_peer_net *peer_net;
688         list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
689                 if (peer_net->lpn_net_id == net_id)
690                         return peer_net;
691         }
692         return NULL;
693 }
694
695 static int
696 lnet_peer_setup_hierarchy(struct lnet_peer *lp, struct lnet_peer_ni *lpni,
697                           lnet_nid_t nid)
698 {
699         struct lnet_peer_net *lpn = NULL;
700         struct lnet_peer_table *ptable;
701         __u32 net_id = LNET_NIDNET(nid);
702
703         /*
704          * Create the peer_ni, peer_net, and peer if they don't exist
705          * yet.
706          */
707         if (lp) {
708                 lpn = lnet_peer_get_net_locked(lp, net_id);
709         } else {
710                 lp = lnet_peer_alloc(nid);
711                 if (!lp)
712                         goto out_enomem;
713         }
714
715         if (!lpn) {
716                 lpn = lnet_peer_net_alloc(net_id);
717                 if (!lpn)
718                         goto out_maybe_free_lp;
719         }
720
721         if (!lpni) {
722                 lpni = lnet_peer_ni_alloc(nid);
723                 if (!lpni)
724                         goto out_maybe_free_lpn;
725         }
726
727         /* Install the new peer_ni */
728         lnet_net_lock(LNET_LOCK_EX);
729         /* Add peer_ni to global peer table hash, if necessary. */
730         if (list_empty(&lpni->lpni_hashlist)) {
731                 ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
732                 list_add_tail(&lpni->lpni_hashlist,
733                               &ptable->pt_hash[lnet_nid2peerhash(nid)]);
734                 ptable->pt_version++;
735                 atomic_inc(&ptable->pt_number);
736                 atomic_inc(&lpni->lpni_refcount);
737         }
738
739         /* Detach the peer_ni from an existing peer, if necessary. */
740         if (lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer != lp)
741                 lnet_try_destroy_peer_hierarchy_locked(lpni);
742
743         /* Add peer_ni to peer_net */
744         lpni->lpni_peer_net = lpn;
745         list_add_tail(&lpni->lpni_on_peer_net_list, &lpn->lpn_peer_nis);
746
747         /* Add peer_net to peer */
748         if (!lpn->lpn_peer) {
749                 lpn->lpn_peer = lp;
750                 list_add_tail(&lpn->lpn_on_peer_list, &lp->lp_peer_nets);
751         }
752
753         /* Add peer to global peer list */
754         if (list_empty(&lp->lp_on_lnet_peer_list))
755                 list_add_tail(&lp->lp_on_lnet_peer_list, &the_lnet.ln_peers);
756         lnet_net_unlock(LNET_LOCK_EX);
757
758         return 0;
759
760 out_maybe_free_lpn:
761         if (list_empty(&lpn->lpn_on_peer_list))
762                 LIBCFS_FREE(lpn, sizeof(*lpn));
763 out_maybe_free_lp:
764         if (list_empty(&lp->lp_on_lnet_peer_list))
765                 LIBCFS_FREE(lp, sizeof(*lp));
766 out_enomem:
767         return -ENOMEM;
768 }
769
770 static int
771 lnet_add_prim_lpni(lnet_nid_t nid)
772 {
773         int rc = 0;
774         struct lnet_peer *peer;
775         struct lnet_peer_ni *lpni;
776
777         LASSERT(nid != LNET_NID_ANY);
778
779         /*
780          * lookup the NID and its peer
781          *  if the peer doesn't exist, create it.
782          *  if this is a non-MR peer then change its state to MR and exit.
783          *  if this is an MR peer and it's a primary NI: NO-OP.
784          *  if this is an MR peer and it's not a primary NI. Operation not
785          *     allowed.
786          *
787          * The adding and deleting of peer nis is being serialized through
788          * the api_mutex. So we can look up peers with the mutex locked
789          * safely. Only when we need to change the ptable, do we need to
790          * exclusively lock the lnet_net_lock()
791          */
792         lpni = lnet_find_peer_ni_locked(nid);
793         if (!lpni) {
794                 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
795                 if (rc != 0)
796                         return rc;
797                 lpni = lnet_find_peer_ni_locked(nid);
798         }
799
800         LASSERT(lpni);
801
802         lnet_peer_ni_decref_locked(lpni);
803
804         peer = lpni->lpni_peer_net->lpn_peer;
805
806         /*
807          * If we found a lpni with the same nid as the NID we're trying to
808          * create, then we're trying to create an already existing lpni 
809          * that belongs to a different peer
810          */
811         if (peer->lp_primary_nid != nid)
812                 return -EEXIST;
813
814         /*
815          * if we found an lpni that is not a multi-rail, which could occur
816          * if lpni is already created as a non-mr lpni or we just created
817          * it, then make sure you indicate that this lpni is a primary mr
818          * capable peer.
819          *
820          * TODO: update flags if necessary
821          */
822         if (!peer->lp_multi_rail && peer->lp_primary_nid == nid)
823                 peer->lp_multi_rail = true;
824
825         return rc;
826 }
827
828 static int
829 lnet_add_peer_ni_to_prim_lpni(lnet_nid_t prim_nid, lnet_nid_t nid)
830 {
831         struct lnet_peer *peer, *primary_peer;
832         struct lnet_peer_ni *lpni = NULL, *klpni = NULL;
833
834         LASSERT(prim_nid != LNET_NID_ANY && nid != LNET_NID_ANY);
835
836         /*
837          * key nid must be created by this point. If not then this
838          * operation is not permitted
839          */
840         klpni = lnet_find_peer_ni_locked(prim_nid);
841         if (!klpni)
842                 return -ENOENT;
843
844         lnet_peer_ni_decref_locked(klpni);
845
846         primary_peer = klpni->lpni_peer_net->lpn_peer;
847
848         lpni = lnet_find_peer_ni_locked(nid);
849         if (lpni) {
850                 lnet_peer_ni_decref_locked(lpni);
851
852                 peer = lpni->lpni_peer_net->lpn_peer;
853                 /*
854                  * lpni already exists in the system but it belongs to
855                  * a different peer. We can't re-added it
856                  */
857                 if (peer->lp_primary_nid != prim_nid && peer->lp_multi_rail) {
858                         CERROR("Cannot add NID %s owned by peer %s to peer %s\n",
859                                libcfs_nid2str(lpni->lpni_nid),
860                                libcfs_nid2str(peer->lp_primary_nid),
861                                libcfs_nid2str(prim_nid));
862                         return -EEXIST;
863                 } else if (peer->lp_primary_nid == prim_nid) {
864                         /*
865                          * found a peer_ni that is already part of the
866                          * peer. This is a no-op operation.
867                          */
868                         return 0;
869                 }
870
871                 /*
872                  * TODO: else if (peer->lp_primary_nid != prim_nid &&
873                  *                !peer->lp_multi_rail)
874                  * peer is not an MR peer and it will be moved in the next
875                  * step to klpni, so update its flags accordingly.
876                  * lnet_move_peer_ni()
877                  */
878
879                 /*
880                  * TODO: call lnet_update_peer() from here to update the
881                  * flags. This is the case when the lpni you're trying to
882                  * add is already part of the peer. This could've been
883                  * added by the DD previously, so go ahead and do any
884                  * updates to the state if necessary
885                  */
886
887         }
888
889         /*
890          * When we get here we either have found an existing lpni, which
891          * we can switch to the new peer. Or we need to create one and
892          * add it to the new peer
893          */
894         return lnet_peer_setup_hierarchy(primary_peer, lpni, nid);
895 }
896
897 /*
898  * lpni creation initiated due to traffic either sending or receiving.
899  */
900 static int
901 lnet_peer_ni_traffic_add(lnet_nid_t nid)
902 {
903         struct lnet_peer_ni *lpni;
904         int rc = 0;
905
906         if (nid == LNET_NID_ANY)
907                 return -EINVAL;
908
909         /* lnet_net_lock is not needed here because ln_api_lock is held */
910         lpni = lnet_find_peer_ni_locked(nid);
911         if (lpni) {
912                 /*
913                  * TODO: lnet_update_primary_nid() but not all of it
914                  * only indicate if we're converting this to MR capable
915                  * Can happen due to DD
916                  */
917                 lnet_peer_ni_decref_locked(lpni);
918         } else {
919                 rc = lnet_peer_setup_hierarchy(NULL, NULL, nid);
920         }
921
922         return rc;
923
924 }
925
926 static int
927 lnet_peer_ni_add_non_mr(lnet_nid_t nid)
928 {
929         struct lnet_peer_ni *lpni;
930
931         lpni = lnet_find_peer_ni_locked(nid);
932         if (lpni) {
933                 CERROR("Cannot add %s as non-mr when it already exists\n",
934                        libcfs_nid2str(nid));
935                 lnet_peer_ni_decref_locked(lpni);
936                 return -EEXIST;
937         }
938
939         return lnet_peer_setup_hierarchy(NULL, NULL, nid);
940 }
941
942 /*
943  * Implementation of IOC_LIBCFS_ADD_PEER_NI.
944  *
945  * This API handles the following combinations:
946  *   Create a primary NI if only the prim_nid is provided
947  *   Create or add an lpni to a primary NI. Primary NI must've already
948  *   been created
949  *   Create a non-MR peer.
950  */
951 int
952 lnet_add_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid, bool mr)
953 {
954         /*
955          * Caller trying to setup an MR like peer hierarchy but
956          * specifying it to be non-MR. This is not allowed.
957          */
958         if (prim_nid != LNET_NID_ANY &&
959             nid != LNET_NID_ANY && !mr)
960                 return -EPERM;
961
962         /* Add the primary NID of a peer */
963         if (prim_nid != LNET_NID_ANY &&
964             nid == LNET_NID_ANY && mr)
965                 return lnet_add_prim_lpni(prim_nid);
966
967         /* Add a NID to an existing peer */
968         if (prim_nid != LNET_NID_ANY &&
969             nid != LNET_NID_ANY && mr)
970                 return lnet_add_peer_ni_to_prim_lpni(prim_nid, nid);
971
972         /* Add a non-MR peer NI */
973         if (((prim_nid != LNET_NID_ANY &&
974               nid == LNET_NID_ANY) ||
975              (prim_nid == LNET_NID_ANY &&
976               nid != LNET_NID_ANY)) && !mr)
977                 return lnet_peer_ni_add_non_mr(prim_nid != LNET_NID_ANY ?
978                                                          prim_nid : nid);
979
980         return 0;
981 }
982
983 /*
984  * Implementation of IOC_LIBCFS_DEL_PEER_NI.
985  *
986  * This API handles the following combinations:
987  *   Delete a NI from a peer if both prim_nid and nid are provided.
988  *   Delete a peer if only prim_nid is provided.
989  *   Delete a peer if its primary nid is provided.
990  *
991  * The caller must hold ln_api_mutex. This prevents the peer from
992  * being modified/deleted by a different thread.
993  */
994 int
995 lnet_del_peer_ni(lnet_nid_t prim_nid, lnet_nid_t nid)
996 {
997         struct lnet_peer *lp;
998         struct lnet_peer_ni *lpni;
999
1000         if (prim_nid == LNET_NID_ANY)
1001                 return -EINVAL;
1002
1003         lpni = lnet_find_peer_ni_locked(prim_nid);
1004         if (!lpni)
1005                 return -ENOENT;
1006         lnet_peer_ni_decref_locked(lpni);
1007         lp = lpni->lpni_peer_net->lpn_peer;
1008
1009         if (prim_nid != lp->lp_primary_nid) {
1010                 CDEBUG(D_NET, "prim_nid %s is not primary for peer %s\n",
1011                        libcfs_nid2str(prim_nid),
1012                        libcfs_nid2str(lp->lp_primary_nid));
1013                 return -ENODEV;
1014         }
1015
1016         if (nid == LNET_NID_ANY || nid == lp->lp_primary_nid)
1017                 return lnet_peer_del(lp);
1018
1019         return lnet_peer_del_nid(lp, nid);
1020 }
1021
1022 void
1023 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
1024 {
1025         struct lnet_peer_table *ptable;
1026
1027         LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
1028         LASSERT(lpni->lpni_rtr_refcount == 0);
1029         LASSERT(list_empty(&lpni->lpni_txq));
1030         LASSERT(lpni->lpni_txqnob == 0);
1031
1032         lpni->lpni_net = NULL;
1033
1034         /* remove the peer ni from the zombie list */
1035         ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
1036         spin_lock(&ptable->pt_zombie_lock);
1037         list_del_init(&lpni->lpni_hashlist);
1038         ptable->pt_zombies--;
1039         spin_unlock(&ptable->pt_zombie_lock);
1040
1041         LIBCFS_FREE(lpni, sizeof(*lpni));
1042 }
1043
1044 struct lnet_peer_ni *
1045 lnet_nid2peerni_ex(lnet_nid_t nid, int cpt)
1046 {
1047         struct lnet_peer_ni *lpni = NULL;
1048         int rc;
1049
1050         if (the_lnet.ln_state != LNET_STATE_RUNNING)
1051                 return ERR_PTR(-ESHUTDOWN);
1052
1053         /*
1054          * find if a peer_ni already exists.
1055          * If so then just return that.
1056          */
1057         lpni = lnet_find_peer_ni_locked(nid);
1058         if (lpni)
1059                 return lpni;
1060
1061         lnet_net_unlock(cpt);
1062
1063         rc = lnet_peer_ni_traffic_add(nid);
1064         if (rc) {
1065                 lpni = ERR_PTR(rc);
1066                 goto out_net_relock;
1067         }
1068
1069         lpni = lnet_find_peer_ni_locked(nid);
1070         LASSERT(lpni);
1071
1072 out_net_relock:
1073         lnet_net_lock(cpt);
1074
1075         return lpni;
1076 }
1077
1078 struct lnet_peer_ni *
1079 lnet_nid2peerni_locked(lnet_nid_t nid, int cpt)
1080 {
1081         struct lnet_peer_ni *lpni = NULL;
1082         int rc;
1083
1084         if (the_lnet.ln_state != LNET_STATE_RUNNING)
1085                 return ERR_PTR(-ESHUTDOWN);
1086
1087         /*
1088          * find if a peer_ni already exists.
1089          * If so then just return that.
1090          */
1091         lpni = lnet_find_peer_ni_locked(nid);
1092         if (lpni)
1093                 return lpni;
1094
1095         /*
1096          * Slow path:
1097          * use the lnet_api_mutex to serialize the creation of the peer_ni
1098          * and the creation/deletion of the local ni/net. When a local ni is
1099          * created, if there exists a set of peer_nis on that network,
1100          * they need to be traversed and updated. When a local NI is
1101          * deleted, which could result in a network being deleted, then
1102          * all peer nis on that network need to be removed as well.
1103          *
1104          * Creation through traffic should also be serialized with
1105          * creation through DLC.
1106          */
1107         lnet_net_unlock(cpt);
1108         mutex_lock(&the_lnet.ln_api_mutex);
1109         /*
1110          * Shutdown is only set under the ln_api_lock, so a single
1111          * check here is sufficent.
1112          */
1113         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1114                 lpni = ERR_PTR(-ESHUTDOWN);
1115                 goto out_mutex_unlock;
1116         }
1117
1118         rc = lnet_peer_ni_traffic_add(nid);
1119         if (rc) {
1120                 lpni = ERR_PTR(rc);
1121                 goto out_mutex_unlock;
1122         }
1123
1124         lpni = lnet_find_peer_ni_locked(nid);
1125         LASSERT(lpni);
1126
1127 out_mutex_unlock:
1128         mutex_unlock(&the_lnet.ln_api_mutex);
1129         lnet_net_lock(cpt);
1130
1131         return lpni;
1132 }
1133
1134 void
1135 lnet_debug_peer(lnet_nid_t nid)
1136 {
1137         char                    *aliveness = "NA";
1138         struct lnet_peer_ni     *lp;
1139         int                     cpt;
1140
1141         cpt = lnet_cpt_of_nid(nid, NULL);
1142         lnet_net_lock(cpt);
1143
1144         lp = lnet_nid2peerni_locked(nid, cpt);
1145         if (IS_ERR(lp)) {
1146                 lnet_net_unlock(cpt);
1147                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
1148                 return;
1149         }
1150
1151         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
1152                 aliveness = lp->lpni_alive ? "up" : "down";
1153
1154         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
1155                libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
1156                aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
1157                lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
1158                lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
1159
1160         lnet_peer_ni_decref_locked(lp);
1161
1162         lnet_net_unlock(cpt);
1163 }
1164
1165 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
1166                           char aliveness[LNET_MAX_STR_LEN],
1167                           __u32 *cpt_iter, __u32 *refcount,
1168                           __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
1169                           __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
1170                           __u32 *peer_tx_qnob)
1171 {
1172         struct lnet_peer_table          *peer_table;
1173         struct lnet_peer_ni             *lp;
1174         int                             j;
1175         int                             lncpt;
1176         bool                            found = false;
1177
1178         /* get the number of CPTs */
1179         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
1180
1181         /* if the cpt number to be examined is >= the number of cpts in
1182          * the system then indicate that there are no more cpts to examin
1183          */
1184         if (*cpt_iter >= lncpt)
1185                 return -ENOENT;
1186
1187         /* get the current table */
1188         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
1189         /* if the ptable is NULL then there are no more cpts to examine */
1190         if (peer_table == NULL)
1191                 return -ENOENT;
1192
1193         lnet_net_lock(*cpt_iter);
1194
1195         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
1196                 struct list_head *peers = &peer_table->pt_hash[j];
1197
1198                 list_for_each_entry(lp, peers, lpni_hashlist) {
1199                         if (peer_index-- > 0)
1200                                 continue;
1201
1202                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
1203                         if (lnet_isrouter(lp) ||
1204                                 lnet_peer_aliveness_enabled(lp))
1205                                 snprintf(aliveness, LNET_MAX_STR_LEN,
1206                                          lp->lpni_alive ? "up" : "down");
1207
1208                         *nid = lp->lpni_nid;
1209                         *refcount = atomic_read(&lp->lpni_refcount);
1210                         *ni_peer_tx_credits =
1211                                 lp->lpni_net->net_tunables.lct_peer_tx_credits;
1212                         *peer_tx_credits = lp->lpni_txcredits;
1213                         *peer_rtr_credits = lp->lpni_rtrcredits;
1214                         *peer_min_rtr_credits = lp->lpni_mintxcredits;
1215                         *peer_tx_qnob = lp->lpni_txqnob;
1216
1217                         found = true;
1218                 }
1219
1220         }
1221         lnet_net_unlock(*cpt_iter);
1222
1223         *cpt_iter = lncpt;
1224
1225         return found ? 0 : -ENOENT;
1226 }
1227
1228 int lnet_get_peer_info(__u32 idx, lnet_nid_t *primary_nid, lnet_nid_t *nid,
1229                        bool *mr,
1230                        struct lnet_peer_ni_credit_info __user *peer_ni_info,
1231                        struct lnet_ioctl_element_stats __user *peer_ni_stats)
1232 {
1233         struct lnet_peer_ni *lpni = NULL;
1234         struct lnet_peer_net *lpn = NULL;
1235         struct lnet_peer *lp = NULL;
1236         struct lnet_peer_ni_credit_info ni_info;
1237         struct lnet_ioctl_element_stats ni_stats;
1238         int rc;
1239
1240         lpni = lnet_get_peer_ni_idx_locked(idx, &lpn, &lp);
1241
1242         if (!lpni)
1243                 return -ENOENT;
1244
1245         *primary_nid = lp->lp_primary_nid;
1246         *mr = lp->lp_multi_rail;
1247         *nid = lpni->lpni_nid;
1248         snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN, "NA");
1249         if (lnet_isrouter(lpni) ||
1250                 lnet_peer_aliveness_enabled(lpni))
1251                 snprintf(ni_info.cr_aliveness, LNET_MAX_STR_LEN,
1252                          lpni->lpni_alive ? "up" : "down");
1253
1254         ni_info.cr_refcount = atomic_read(&lpni->lpni_refcount);
1255         ni_info.cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
1256                 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
1257         ni_info.cr_peer_tx_credits = lpni->lpni_txcredits;
1258         ni_info.cr_peer_rtr_credits = lpni->lpni_rtrcredits;
1259         ni_info.cr_peer_min_rtr_credits = lpni->lpni_minrtrcredits;
1260         ni_info.cr_peer_min_tx_credits = lpni->lpni_mintxcredits;
1261         ni_info.cr_peer_tx_qnob = lpni->lpni_txqnob;
1262         ni_info.cr_ncpt = lpni->lpni_cpt;
1263
1264         ni_stats.iel_send_count = atomic_read(&lpni->lpni_stats.send_count);
1265         ni_stats.iel_recv_count = atomic_read(&lpni->lpni_stats.recv_count);
1266         ni_stats.iel_drop_count = atomic_read(&lpni->lpni_stats.drop_count);
1267
1268         /* If copy_to_user fails */
1269         rc = -EFAULT;
1270         if (copy_to_user(peer_ni_info, &ni_info, sizeof(ni_info)))
1271                 goto copy_failed;
1272
1273         if (copy_to_user(peer_ni_stats, &ni_stats, sizeof(ni_stats)))
1274                 goto copy_failed;
1275
1276         rc = 0;
1277
1278 copy_failed:
1279         return rc;
1280 }