Whamcloud - gitweb
86b56ae8b13a51b837b6d9684adb3c119c2db32d
[fs/lustre-release.git] / lnet / lnet / peer.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/lnet/peer.c
33  */
34
35 #define DEBUG_SUBSYSTEM S_LNET
36
37 #include <lnet/lib-lnet.h>
38 #include <lnet/lib-dlc.h>
39
40 static void
41 lnet_peer_remove_from_remote_list(struct lnet_peer_ni *lpni)
42 {
43         if (!list_empty(&lpni->lpni_on_remote_peer_ni_list)) {
44                 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
45                 lnet_peer_ni_decref_locked(lpni);
46         }
47 }
48
49 void
50 lnet_peer_net_added(struct lnet_net *net)
51 {
52         struct lnet_peer_ni *lpni, *tmp;
53
54         list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
55                                  lpni_on_remote_peer_ni_list) {
56
57                 if (LNET_NIDNET(lpni->lpni_nid) == net->net_id) {
58                         lpni->lpni_net = net;
59                         lpni->lpni_txcredits =
60                         lpni->lpni_mintxcredits =
61                                 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
62                         lpni->lpni_rtrcredits =
63                         lpni->lpni_minrtrcredits =
64                                 lnet_peer_buffer_credits(lpni->lpni_net);
65
66                         lnet_peer_remove_from_remote_list(lpni);
67                 }
68         }
69 }
70
71 static void
72 lnet_peer_tables_destroy(void)
73 {
74         struct lnet_peer_table  *ptable;
75         struct list_head        *hash;
76         int                     i;
77         int                     j;
78
79         if (!the_lnet.ln_peer_tables)
80                 return;
81
82         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
83                 hash = ptable->pt_hash;
84                 if (!hash) /* not intialized */
85                         break;
86
87                 ptable->pt_hash = NULL;
88                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
89                         LASSERT(list_empty(&hash[j]));
90
91                 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
92         }
93
94         cfs_percpt_free(the_lnet.ln_peer_tables);
95         the_lnet.ln_peer_tables = NULL;
96 }
97
98 int
99 lnet_peer_tables_create(void)
100 {
101         struct lnet_peer_table  *ptable;
102         struct list_head        *hash;
103         int                     i;
104         int                     j;
105
106         the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
107                                                    sizeof(*ptable));
108         if (the_lnet.ln_peer_tables == NULL) {
109                 CERROR("Failed to allocate cpu-partition peer tables\n");
110                 return -ENOMEM;
111         }
112
113         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
114                 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
115                                  LNET_PEER_HASH_SIZE * sizeof(*hash));
116                 if (hash == NULL) {
117                         CERROR("Failed to create peer hash table\n");
118                         lnet_peer_tables_destroy();
119                         return -ENOMEM;
120                 }
121
122                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
123                         INIT_LIST_HEAD(&hash[j]);
124                 ptable->pt_hash = hash; /* sign of initialization */
125         }
126
127         return 0;
128 }
129
130 void lnet_peer_uninit()
131 {
132         int cpt;
133         struct lnet_peer_ni *lpni, *tmp;
134         struct lnet_peer_table *ptable = NULL;
135
136         /* remove all peer_nis from the remote peer and he hash list */
137         list_for_each_entry_safe(lpni, tmp, &the_lnet.ln_remote_peer_ni_list,
138                                  lpni_on_remote_peer_ni_list) {
139                 list_del_init(&lpni->lpni_on_remote_peer_ni_list);
140                 lnet_peer_ni_decref_locked(lpni);
141
142                 cpt = lnet_cpt_of_nid_locked(lpni->lpni_nid, NULL);
143                 ptable = the_lnet.ln_peer_tables[cpt];
144                 ptable->pt_zombies++;
145
146                 list_del_init(&lpni->lpni_hashlist);
147                 lnet_peer_ni_decref_locked(lpni);
148         }
149
150         lnet_peer_tables_destroy();
151 }
152
153 static void
154 lnet_peer_table_cleanup_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable)
155 {
156         int                      i;
157         struct lnet_peer_ni     *lp;
158         struct lnet_peer_ni     *tmp;
159
160         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
161                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
162                                          lpni_hashlist) {
163                         if (ni != NULL && ni->ni_net != lp->lpni_net)
164                                 continue;
165                         list_del_init(&lp->lpni_hashlist);
166                         /* Lose hash table's ref */
167                         ptable->pt_zombies++;
168                         lnet_peer_ni_decref_locked(lp);
169                 }
170         }
171 }
172
173 static void
174 lnet_peer_table_finalize_wait_locked(struct lnet_peer_table *ptable,
175                                      int cpt_locked)
176 {
177         int     i;
178
179         for (i = 3; ptable->pt_zombies != 0; i++) {
180                 lnet_net_unlock(cpt_locked);
181
182                 if (IS_PO2(i)) {
183                         CDEBUG(D_WARNING,
184                                "Waiting for %d zombies on peer table\n",
185                                ptable->pt_zombies);
186                 }
187                 set_current_state(TASK_UNINTERRUPTIBLE);
188                 schedule_timeout(cfs_time_seconds(1) >> 1);
189                 lnet_net_lock(cpt_locked);
190         }
191 }
192
193 static void
194 lnet_peer_table_del_rtrs_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable,
195                                 int cpt_locked)
196 {
197         struct lnet_peer_ni     *lp;
198         struct lnet_peer_ni     *tmp;
199         lnet_nid_t              lpni_nid;
200         int                     i;
201
202         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
203                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
204                                          lpni_hashlist) {
205                         if (ni->ni_net != lp->lpni_net)
206                                 continue;
207
208                         if (lp->lpni_rtr_refcount == 0)
209                                 continue;
210
211                         lpni_nid = lp->lpni_nid;
212
213                         lnet_net_unlock(cpt_locked);
214                         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
215                         lnet_net_lock(cpt_locked);
216                 }
217         }
218 }
219
220 void
221 lnet_peer_tables_cleanup(lnet_ni_t *ni)
222 {
223         int                             i;
224         struct lnet_peer_table          *ptable;
225
226         LASSERT(the_lnet.ln_shutdown || ni != NULL);
227         /* If just deleting the peers for a NI, get rid of any routes these
228          * peers are gateways for. */
229         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
230                 lnet_net_lock(LNET_LOCK_EX);
231                 lnet_peer_table_del_rtrs_locked(ni, ptable, i);
232                 lnet_net_unlock(LNET_LOCK_EX);
233         }
234
235         /* Start the cleanup process */
236         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
237                 lnet_net_lock(LNET_LOCK_EX);
238                 lnet_peer_table_cleanup_locked(ni, ptable);
239                 lnet_net_unlock(LNET_LOCK_EX);
240         }
241
242         /* Wait until all peers have been destroyed. */
243         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
244                 lnet_net_lock(LNET_LOCK_EX);
245                 lnet_peer_table_finalize_wait_locked(ptable, i);
246                 lnet_net_unlock(LNET_LOCK_EX);
247         }
248 }
249
250 static struct lnet_peer_ni *
251 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
252 {
253         struct list_head        *peers;
254         struct lnet_peer_ni     *lp;
255
256         LASSERT(!the_lnet.ln_shutdown);
257
258         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
259         list_for_each_entry(lp, peers, lpni_hashlist) {
260                 if (lp->lpni_nid == nid) {
261                         lnet_peer_ni_addref_locked(lp);
262                         return lp;
263                 }
264         }
265
266         return NULL;
267 }
268
269 struct lnet_peer_ni *
270 lnet_find_peer_ni_locked(lnet_nid_t nid)
271 {
272         struct lnet_peer_ni *lpni;
273         struct lnet_peer_table *ptable;
274         int cpt;
275
276         cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
277
278         ptable = the_lnet.ln_peer_tables[cpt];
279         lpni = lnet_get_peer_ni_locked(ptable, nid);
280
281         return lpni;
282 }
283
284 int
285 lnet_find_or_create_peer_locked(lnet_nid_t dst_nid, int cpt, struct lnet_peer **peer)
286 {
287         struct lnet_peer_ni *lpni;
288
289         lpni = lnet_find_peer_ni_locked(dst_nid);
290         if (!lpni) {
291                 int rc;
292                 rc = lnet_nid2peerni_locked(&lpni, dst_nid, cpt);
293                 if (rc != 0)
294                         return rc;
295         }
296
297         *peer = lpni->lpni_peer_net->lpn_peer;
298         lnet_peer_ni_decref_locked(lpni);
299
300         return 0;
301 }
302
303 struct lnet_peer_ni *
304 lnet_get_peer_ni_idx_locked(int idx, struct lnet_peer_net **lpn,
305                             struct lnet_peer **lp)
306 {
307         struct lnet_peer_ni     *lpni;
308
309         list_for_each_entry((*lp), &the_lnet.ln_peers, lp_on_lnet_peer_list) {
310                 list_for_each_entry((*lpn), &((*lp)->lp_peer_nets), lpn_on_peer_list) {
311                         list_for_each_entry(lpni, &((*lpn)->lpn_peer_nis),
312                                             lpni_on_peer_net_list)
313                                 if (idx-- == 0)
314                                         return lpni;
315                 }
316         }
317
318         return NULL;
319 }
320
321 struct lnet_peer_ni *
322 lnet_get_next_peer_ni_locked(struct lnet_peer *peer,
323                              struct lnet_peer_net *peer_net,
324                              struct lnet_peer_ni *prev)
325 {
326         struct lnet_peer_ni *lpni;
327         struct lnet_peer_net *net = peer_net;
328
329         if (!prev) {
330                 if (!net)
331                         net = list_entry(peer->lp_peer_nets.next,
332                                          struct lnet_peer_net,
333                                          lpn_on_peer_list);
334                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
335                                   lpni_on_peer_net_list);
336
337                 return lpni;
338         }
339
340         if (prev->lpni_on_peer_net_list.next ==
341             &prev->lpni_peer_net->lpn_peer_nis) {
342                 /*
343                  * if you reached the end of the peer ni list and the peer
344                  * net is specified then there are no more peer nis in that
345                  * net.
346                  */
347                 if (net)
348                         return NULL;
349
350                 /*
351                  * we reached the end of this net ni list. move to the
352                  * next net
353                  */
354                 if (prev->lpni_peer_net->lpn_on_peer_list.next ==
355                     &peer->lp_peer_nets)
356                         /* no more nets and no more NIs. */
357                         return NULL;
358
359                 /* get the next net */
360                 net = list_entry(prev->lpni_peer_net->lpn_on_peer_list.next,
361                                  struct lnet_peer_net,
362                                  lpn_on_peer_list);
363                 /* get the ni on it */
364                 lpni = list_entry(net->lpn_peer_nis.next, struct lnet_peer_ni,
365                                   lpni_on_peer_net_list);
366
367                 return lpni;
368         }
369
370         /* there are more nis left */
371         lpni = list_entry(prev->lpni_on_peer_net_list.next,
372                           struct lnet_peer_ni, lpni_on_peer_net_list);
373
374         return lpni;
375 }
376
377 bool
378 lnet_peer_is_ni_pref_locked(struct lnet_peer_ni *lpni, struct lnet_ni *ni)
379 {
380         int i;
381
382         for (i = 0; i < lpni->lpni_pref_nnids; i++) {
383                 if (lpni->lpni_pref_nids[i] == ni->ni_nid)
384                         return true;
385         }
386         return false;
387 }
388
389 lnet_nid_t
390 lnet_peer_primary_nid(lnet_nid_t nid)
391 {
392         struct lnet_peer_ni *lpni;
393         lnet_nid_t primary_nid = nid;
394         int cpt;
395
396         cpt = lnet_net_lock_current();
397         lpni = lnet_find_peer_ni_locked(nid);
398         if (lpni) {
399                 primary_nid = lpni->lpni_peer_net->lpn_peer->lp_primary_nid;
400                 lnet_peer_ni_decref_locked(lpni);
401         }
402         lnet_net_unlock(cpt);
403
404         return primary_nid;
405 }
406
407 static void
408 lnet_try_destroy_peer_hierarchy_locked(struct lnet_peer_ni *lpni)
409 {
410         struct lnet_peer_net *peer_net;
411         struct lnet_peer *peer;
412
413         /* TODO: could the below situation happen? accessing an already
414          * destroyed peer? */
415         if (lpni->lpni_peer_net == NULL ||
416             lpni->lpni_peer_net->lpn_peer == NULL)
417                 return;
418
419         peer_net = lpni->lpni_peer_net;
420         peer = lpni->lpni_peer_net->lpn_peer;
421
422         list_del_init(&lpni->lpni_on_peer_net_list);
423         lpni->lpni_peer_net = NULL;
424
425         /* if peer_net is empty, then remove it from the peer */
426         if (list_empty(&peer_net->lpn_peer_nis)) {
427                 list_del_init(&peer_net->lpn_on_peer_list);
428                 peer_net->lpn_peer = NULL;
429                 LIBCFS_FREE(peer_net, sizeof(*peer_net));
430
431                 /* if the peer is empty then remove it from the
432                  * the_lnet.ln_peers */
433                 if (list_empty(&peer->lp_peer_nets)) {
434                         list_del_init(&peer->lp_on_lnet_peer_list);
435                         LIBCFS_FREE(peer, sizeof(*peer));
436                 }
437         }
438 }
439
440 static int
441 lnet_build_peer_hierarchy(struct lnet_peer_ni *lpni)
442 {
443         struct lnet_peer *peer;
444         struct lnet_peer_net *peer_net;
445         __u32 lpni_net = LNET_NIDNET(lpni->lpni_nid);
446
447         peer = NULL;
448         peer_net = NULL;
449
450         LIBCFS_ALLOC(peer, sizeof(*peer));
451         if (peer == NULL)
452                 return -ENOMEM;
453
454         LIBCFS_ALLOC(peer_net, sizeof(*peer_net));
455         if (peer_net == NULL) {
456                 LIBCFS_FREE(peer, sizeof(*peer));
457                 return -ENOMEM;
458         }
459
460         INIT_LIST_HEAD(&peer->lp_on_lnet_peer_list);
461         INIT_LIST_HEAD(&peer->lp_peer_nets);
462         INIT_LIST_HEAD(&peer_net->lpn_on_peer_list);
463         INIT_LIST_HEAD(&peer_net->lpn_peer_nis);
464
465         /* build the hierarchy */
466         peer_net->lpn_net_id = lpni_net;
467         peer_net->lpn_peer = peer;
468         lpni->lpni_peer_net = peer_net;
469         peer->lp_primary_nid = lpni->lpni_nid;
470         peer->lp_multi_rail = false;
471         list_add_tail(&peer_net->lpn_on_peer_list, &peer->lp_peer_nets);
472         list_add_tail(&lpni->lpni_on_peer_net_list, &peer_net->lpn_peer_nis);
473         list_add_tail(&peer->lp_on_lnet_peer_list, &the_lnet.ln_peers);
474
475         return 0;
476 }
477
478 struct lnet_peer_net *
479 lnet_peer_get_net_locked(struct lnet_peer *peer, __u32 net_id)
480 {
481         struct lnet_peer_net *peer_net;
482         list_for_each_entry(peer_net, &peer->lp_peer_nets, lpn_on_peer_list) {
483                 if (peer_net->lpn_net_id == net_id)
484                         return peer_net;
485         }
486         return NULL;
487 }
488
489 /*
490  * given the key nid find the peer to add the new peer NID to. If the key
491  * nid is NULL, then create a new peer, but first make sure that the NID
492  * is unique
493  */
494 int
495 lnet_add_peer_ni_to_peer(lnet_nid_t key_nid, lnet_nid_t nid, bool mr)
496 {
497         struct lnet_peer_ni *lpni, *lpni2;
498         struct lnet_peer *peer;
499         struct lnet_peer_net *peer_net, *pn;
500         int cpt, cpt2, rc;
501         struct lnet_peer_table *ptable = NULL;
502         __u32 net_id = LNET_NIDNET(nid);
503
504         if (nid == LNET_NID_ANY)
505                 return -EINVAL;
506
507         /* check that nid is unique */
508         cpt = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
509         lnet_net_lock(cpt);
510         lpni = lnet_find_peer_ni_locked(nid);
511         if (lpni != NULL) {
512                 lnet_peer_ni_decref_locked(lpni);
513                 lnet_net_unlock(cpt);
514                 return -EEXIST;
515         }
516         lnet_net_unlock(cpt);
517
518         if (key_nid != LNET_NID_ANY) {
519                 cpt2 = lnet_nid_cpt_hash(key_nid, LNET_CPT_NUMBER);
520                 lnet_net_lock(cpt2);
521                 lpni = lnet_find_peer_ni_locked(key_nid);
522                 if (lpni == NULL) {
523                         lnet_net_unlock(cpt2);
524                         /* key_nid refers to a non-existant peer_ni.*/
525                         return -EINVAL;
526                 }
527                 peer = lpni->lpni_peer_net->lpn_peer;
528                 peer->lp_multi_rail = mr;
529                 lnet_peer_ni_decref_locked(lpni);
530                 lnet_net_unlock(cpt2);
531         } else {
532                 lnet_net_lock(LNET_LOCK_EX);
533                 rc = lnet_nid2peerni_locked(&lpni, nid, LNET_LOCK_EX);
534                 if (rc == 0) {
535                         lpni->lpni_peer_net->lpn_peer->lp_multi_rail = mr;
536                         lnet_peer_ni_decref_locked(lpni);
537                 }
538                 lnet_net_unlock(LNET_LOCK_EX);
539                 return rc;
540         }
541
542         lpni = NULL;
543
544         LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt, sizeof(*lpni));
545         if (lpni == NULL)
546                 return -ENOMEM;
547
548         INIT_LIST_HEAD(&lpni->lpni_txq);
549         INIT_LIST_HEAD(&lpni->lpni_rtrq);
550         INIT_LIST_HEAD(&lpni->lpni_routes);
551         INIT_LIST_HEAD(&lpni->lpni_hashlist);
552         INIT_LIST_HEAD(&lpni->lpni_on_peer_net_list);
553         INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
554
555         lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
556         lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
557         lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
558         lpni->lpni_nid = nid;
559         lpni->lpni_cpt = cpt;
560         lnet_set_peer_ni_health_locked(lpni, true);
561
562         /* allocate here in case we need to add a new peer_net */
563         peer_net = NULL;
564         LIBCFS_ALLOC(peer_net, sizeof(*peer_net));
565         if (peer_net == NULL) {
566                 rc = -ENOMEM;
567                 if (lpni != NULL)
568                         LIBCFS_FREE(lpni, sizeof(*lpni));
569                 return rc;
570         }
571
572         lnet_net_lock(LNET_LOCK_EX);
573
574         ptable = the_lnet.ln_peer_tables[cpt];
575         ptable->pt_number++;
576
577         lpni2 = lnet_find_peer_ni_locked(nid);
578         if (lpni2 != NULL) {
579                 lnet_peer_ni_decref_locked(lpni2);
580                 /* sanity check that lpni2's peer is what we expect */
581                 if (lpni2->lpni_peer_net->lpn_peer != peer)
582                         rc = -EEXIST;
583                 else
584                         rc = -EINVAL;
585
586                 ptable->pt_number--;
587                 /* another thread has already added it */
588                 lnet_net_unlock(LNET_LOCK_EX);
589                 LIBCFS_FREE(peer_net, sizeof(*peer_net));
590                 return rc;
591         }
592
593         lpni->lpni_net = lnet_get_net_locked(LNET_NIDNET(lpni->lpni_nid));
594         if (lpni->lpni_net != NULL) {
595                 lpni->lpni_txcredits    =
596                 lpni->lpni_mintxcredits =
597                         lpni->lpni_net->net_tunables.lct_peer_tx_credits;
598                 lpni->lpni_rtrcredits =
599                 lpni->lpni_minrtrcredits = lnet_peer_buffer_credits(lpni->lpni_net);
600         } else {
601                 /*
602                  * if you're adding a peer which is not on a local network
603                  * then we can't assign any of the credits. It won't be
604                  * picked for sending anyway. Eventually a network can be
605                  * added, in this case we need to revisit this peer and
606                  * update its credits.
607                  */
608
609                 /* increment refcount for remote peer list */
610                 atomic_inc(&lpni->lpni_refcount);
611                 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
612                               &the_lnet.ln_remote_peer_ni_list);
613         }
614
615         /* increment refcount for peer on hash list */
616         atomic_inc(&lpni->lpni_refcount);
617
618         list_add_tail(&lpni->lpni_hashlist,
619                       &ptable->pt_hash[lnet_nid2peerhash(nid)]);
620         ptable->pt_version++;
621
622         /* add the lpni to a net */
623         list_for_each_entry(pn, &peer->lp_peer_nets, lpn_on_peer_list) {
624                 if (pn->lpn_net_id == net_id) {
625                         list_add_tail(&lpni->lpni_on_peer_net_list,
626                                       &pn->lpn_peer_nis);
627                         lpni->lpni_peer_net = pn;
628                         lnet_net_unlock(LNET_LOCK_EX);
629                         LIBCFS_FREE(peer_net, sizeof(*peer_net));
630                         return 0;
631                 }
632         }
633
634         INIT_LIST_HEAD(&peer_net->lpn_on_peer_list);
635         INIT_LIST_HEAD(&peer_net->lpn_peer_nis);
636
637         /* build the hierarchy */
638         peer_net->lpn_net_id = net_id;
639         peer_net->lpn_peer = peer;
640         lpni->lpni_peer_net = peer_net;
641         list_add_tail(&lpni->lpni_on_peer_net_list, &peer_net->lpn_peer_nis);
642         list_add_tail(&peer_net->lpn_on_peer_list, &peer->lp_peer_nets);
643
644         lnet_net_unlock(LNET_LOCK_EX);
645         return 0;
646 }
647
648 int
649 lnet_del_peer_ni_from_peer(lnet_nid_t key_nid, lnet_nid_t nid)
650 {
651         int cpt;
652         lnet_nid_t local_nid;
653         struct lnet_peer *peer;
654         struct lnet_peer_ni *lpni, *lpni2;
655         struct lnet_peer_table *ptable = NULL;
656
657         if (key_nid == LNET_NID_ANY)
658                 return -EINVAL;
659
660         local_nid = (nid != LNET_NID_ANY) ? nid : key_nid;
661         cpt = lnet_nid_cpt_hash(local_nid, LNET_CPT_NUMBER);
662         lnet_net_lock(LNET_LOCK_EX);
663
664         lpni = lnet_find_peer_ni_locked(local_nid);
665         if (lpni == NULL) {
666                 lnet_net_unlock(cpt);
667                 return -EINVAL;
668         }
669         lnet_peer_ni_decref_locked(lpni);
670
671         peer = lpni->lpni_peer_net->lpn_peer;
672         LASSERT(peer != NULL);
673
674         if (peer->lp_primary_nid == lpni->lpni_nid) {
675                 /*
676                  * deleting the primary ni is equivalent to deleting the
677                  * entire peer
678                  */
679                 lpni = NULL;
680                 lpni = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
681                 while (lpni != NULL) {
682                         lpni2 = lnet_get_next_peer_ni_locked(peer, NULL, lpni);
683                         cpt = lnet_nid_cpt_hash(lpni->lpni_nid,
684                                                 LNET_CPT_NUMBER);
685                         lnet_peer_remove_from_remote_list(lpni);
686                         ptable = the_lnet.ln_peer_tables[cpt];
687                         ptable->pt_zombies++;
688                         list_del_init(&lpni->lpni_hashlist);
689                         lnet_peer_ni_decref_locked(lpni);
690                         lpni = lpni2;
691                 }
692                 lnet_net_unlock(LNET_LOCK_EX);
693
694                 return 0;
695         }
696
697         lnet_peer_remove_from_remote_list(lpni);
698         cpt = lnet_nid_cpt_hash(lpni->lpni_nid, LNET_CPT_NUMBER);
699         ptable = the_lnet.ln_peer_tables[cpt];
700         ptable->pt_zombies++;
701         list_del_init(&lpni->lpni_hashlist);
702         lnet_peer_ni_decref_locked(lpni);
703         lnet_net_unlock(LNET_LOCK_EX);
704
705         return 0;
706 }
707
708 void
709 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
710 {
711         struct lnet_peer_table *ptable;
712
713         LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
714         LASSERT(lpni->lpni_rtr_refcount == 0);
715         LASSERT(list_empty(&lpni->lpni_txq));
716         LASSERT(list_empty(&lpni->lpni_hashlist));
717         LASSERT(lpni->lpni_txqnob == 0);
718         LASSERT(lpni->lpni_peer_net != NULL);
719         LASSERT(lpni->lpni_peer_net->lpn_peer != NULL);
720
721         ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
722         LASSERT(ptable->pt_number > 0);
723         ptable->pt_number--;
724
725         lpni->lpni_net = NULL;
726
727         lnet_try_destroy_peer_hierarchy_locked(lpni);
728
729         LIBCFS_FREE(lpni, sizeof(*lpni));
730
731         LASSERT(ptable->pt_zombies > 0);
732         ptable->pt_zombies--;
733 }
734
735 int
736 lnet_nid2peerni_locked(struct lnet_peer_ni **lpnip, lnet_nid_t nid, int cpt)
737 {
738         struct lnet_peer_table  *ptable;
739         struct lnet_peer_ni     *lpni = NULL;
740         struct lnet_peer_ni     *lpni2;
741         int                     cpt2;
742         int                     rc = 0;
743
744         *lpnip = NULL;
745         if (the_lnet.ln_shutdown) /* it's shutting down */
746                 return -ESHUTDOWN;
747
748         /*
749          * calculate cpt2 with the standard hash function
750          * This cpt2 becomes the slot where we'll find or create the peer.
751          */
752         cpt2 = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
753
754         /*
755          * Any changes to the peer tables happen under exclusive write
756          * lock. Any reads to the peer tables can be done via a standard
757          * CPT read lock.
758          */
759         if (cpt != LNET_LOCK_EX) {
760                 lnet_net_unlock(cpt);
761                 lnet_net_lock(LNET_LOCK_EX);
762         }
763
764         ptable = the_lnet.ln_peer_tables[cpt2];
765         lpni = lnet_get_peer_ni_locked(ptable, nid);
766         if (lpni != NULL) {
767                 *lpnip = lpni;
768                 if (cpt != LNET_LOCK_EX) {
769                         lnet_net_unlock(LNET_LOCK_EX);
770                         lnet_net_lock(cpt);
771                 }
772                 return 0;
773         }
774
775         /*
776          * take extra refcount in case another thread has shutdown LNet
777          * and destroyed locks and peer-table before I finish the allocation
778          */
779         ptable->pt_number++;
780         lnet_net_unlock(LNET_LOCK_EX);
781
782         LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt2, sizeof(*lpni));
783
784         if (lpni == NULL) {
785                 rc = -ENOMEM;
786                 lnet_net_lock(cpt);
787                 goto out;
788         }
789
790         INIT_LIST_HEAD(&lpni->lpni_txq);
791         INIT_LIST_HEAD(&lpni->lpni_rtrq);
792         INIT_LIST_HEAD(&lpni->lpni_routes);
793         INIT_LIST_HEAD(&lpni->lpni_hashlist);
794         INIT_LIST_HEAD(&lpni->lpni_on_peer_net_list);
795         INIT_LIST_HEAD(&lpni->lpni_on_remote_peer_ni_list);
796
797         lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
798         lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
799         lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
800         lpni->lpni_nid = nid;
801         lpni->lpni_cpt = cpt2;
802         atomic_set(&lpni->lpni_refcount, 2);    /* 1 for caller; 1 for hash */
803
804         rc = lnet_build_peer_hierarchy(lpni);
805         if (rc != 0)
806                 goto out;
807
808         lnet_net_lock(LNET_LOCK_EX);
809
810         if (the_lnet.ln_shutdown) {
811                 rc = -ESHUTDOWN;
812                 goto out;
813         }
814
815         lpni2 = lnet_get_peer_ni_locked(ptable, nid);
816         if (lpni2 != NULL) {
817                 *lpnip = lpni2;
818                 goto out;
819         }
820
821         lpni->lpni_net = lnet_get_net_locked(LNET_NIDNET(lpni->lpni_nid));
822         if (lpni->lpni_net) {
823                 lpni->lpni_txcredits    =
824                 lpni->lpni_mintxcredits =
825                         lpni->lpni_net->net_tunables.lct_peer_tx_credits;
826                 lpni->lpni_rtrcredits    =
827                 lpni->lpni_minrtrcredits =
828                         lnet_peer_buffer_credits(lpni->lpni_net);
829         } else {
830                 /*
831                  * if you're adding a peer which is not on a local network
832                  * then we can't assign any of the credits. It won't be
833                  * picked for sending anyway. Eventually a network can be
834                  * added, in this case we need to revisit this peer and
835                  * update its credits.
836                  */
837
838                 CDEBUG(D_NET, "peer_ni %s is not directly connected\n",
839                        libcfs_nid2str(nid));
840                 /* increment refcount for remote peer list */
841                 atomic_inc(&lpni->lpni_refcount);
842                 list_add_tail(&lpni->lpni_on_remote_peer_ni_list,
843                               &the_lnet.ln_remote_peer_ni_list);
844         }
845
846         lnet_set_peer_ni_health_locked(lpni, true);
847
848         list_add_tail(&lpni->lpni_hashlist,
849                         &ptable->pt_hash[lnet_nid2peerhash(nid)]);
850         ptable->pt_version++;
851         *lpnip = lpni;
852
853         if (cpt != LNET_LOCK_EX) {
854                 lnet_net_unlock(LNET_LOCK_EX);
855                 lnet_net_lock(cpt);
856         }
857
858         return 0;
859 out:
860         if (lpni != NULL) {
861                 lnet_try_destroy_peer_hierarchy_locked(lpni);
862                 LIBCFS_FREE(lpni, sizeof(*lpni));
863         }
864         ptable->pt_number--;
865         if (cpt != LNET_LOCK_EX) {
866                 lnet_net_unlock(LNET_LOCK_EX);
867                 lnet_net_lock(cpt);
868         }
869         return rc;
870 }
871
872 void
873 lnet_debug_peer(lnet_nid_t nid)
874 {
875         char                    *aliveness = "NA";
876         struct lnet_peer_ni     *lp;
877         int                     rc;
878         int                     cpt;
879
880         cpt = lnet_cpt_of_nid(nid, NULL);
881         lnet_net_lock(cpt);
882
883         rc = lnet_nid2peerni_locked(&lp, nid, cpt);
884         if (rc != 0) {
885                 lnet_net_unlock(cpt);
886                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
887                 return;
888         }
889
890         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
891                 aliveness = lp->lpni_alive ? "up" : "down";
892
893         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
894                libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
895                aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
896                lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
897                lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
898
899         lnet_peer_ni_decref_locked(lp);
900
901         lnet_net_unlock(cpt);
902 }
903
904 int lnet_get_peer_ni_info(__u32 peer_index, __u64 *nid,
905                           char aliveness[LNET_MAX_STR_LEN],
906                           __u32 *cpt_iter, __u32 *refcount,
907                           __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
908                           __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
909                           __u32 *peer_tx_qnob)
910 {
911         struct lnet_peer_table          *peer_table;
912         struct lnet_peer_ni             *lp;
913         int                             j;
914         int                             lncpt;
915         bool                            found = false;
916
917         /* get the number of CPTs */
918         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
919
920         /* if the cpt number to be examined is >= the number of cpts in
921          * the system then indicate that there are no more cpts to examin
922          */
923         if (*cpt_iter >= lncpt)
924                 return -ENOENT;
925
926         /* get the current table */
927         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
928         /* if the ptable is NULL then there are no more cpts to examine */
929         if (peer_table == NULL)
930                 return -ENOENT;
931
932         lnet_net_lock(*cpt_iter);
933
934         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
935                 struct list_head *peers = &peer_table->pt_hash[j];
936
937                 list_for_each_entry(lp, peers, lpni_hashlist) {
938                         if (peer_index-- > 0)
939                                 continue;
940
941                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
942                         if (lnet_isrouter(lp) ||
943                                 lnet_peer_aliveness_enabled(lp))
944                                 snprintf(aliveness, LNET_MAX_STR_LEN,
945                                          lp->lpni_alive ? "up" : "down");
946
947                         *nid = lp->lpni_nid;
948                         *refcount = atomic_read(&lp->lpni_refcount);
949                         *ni_peer_tx_credits =
950                                 lp->lpni_net->net_tunables.lct_peer_tx_credits;
951                         *peer_tx_credits = lp->lpni_txcredits;
952                         *peer_rtr_credits = lp->lpni_rtrcredits;
953                         *peer_min_rtr_credits = lp->lpni_mintxcredits;
954                         *peer_tx_qnob = lp->lpni_txqnob;
955
956                         found = true;
957                 }
958
959         }
960         lnet_net_unlock(*cpt_iter);
961
962         *cpt_iter = lncpt;
963
964         return found ? 0 : -ENOENT;
965 }
966
967 int lnet_get_peer_info(__u32 idx, lnet_nid_t *primary_nid, lnet_nid_t *nid,
968                        struct lnet_peer_ni_credit_info *peer_ni_info,
969                        struct lnet_ioctl_element_stats *peer_ni_stats)
970 {
971         struct lnet_peer_ni *lpni = NULL;
972         struct lnet_peer_net *lpn = NULL;
973         struct lnet_peer *lp = NULL;
974
975         lpni = lnet_get_peer_ni_idx_locked(idx, &lpn, &lp);
976
977         if (!lpni)
978                 return -ENOENT;
979
980         *primary_nid = lp->lp_primary_nid;
981         *nid = lpni->lpni_nid;
982         snprintf(peer_ni_info->cr_aliveness, LNET_MAX_STR_LEN, "NA");
983         if (lnet_isrouter(lpni) ||
984                 lnet_peer_aliveness_enabled(lpni))
985                 snprintf(peer_ni_info->cr_aliveness, LNET_MAX_STR_LEN,
986                          lpni->lpni_alive ? "up" : "down");
987
988         peer_ni_info->cr_refcount = atomic_read(&lpni->lpni_refcount);
989         peer_ni_info->cr_ni_peer_tx_credits = (lpni->lpni_net != NULL) ?
990                 lpni->lpni_net->net_tunables.lct_peer_tx_credits : 0;
991         peer_ni_info->cr_peer_tx_credits = lpni->lpni_txcredits;
992         peer_ni_info->cr_peer_rtr_credits = lpni->lpni_rtrcredits;
993         peer_ni_info->cr_peer_min_rtr_credits = lpni->lpni_mintxcredits;
994         peer_ni_info->cr_peer_tx_qnob = lpni->lpni_txqnob;
995
996         peer_ni_stats->send_count = atomic_read(&lpni->lpni_stats.send_count);
997         peer_ni_stats->recv_count = atomic_read(&lpni->lpni_stats.recv_count);
998         peer_ni_stats->drop_count = atomic_read(&lpni->lpni_stats.drop_count);
999
1000         return 0;
1001 }