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