Whamcloud - gitweb
6a6f56b1bd3f9c0b4b257398e8c85b17ff2e5e7c
[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 int
41 lnet_peer_tables_create(void)
42 {
43         struct lnet_peer_table  *ptable;
44         struct list_head        *hash;
45         int                     i;
46         int                     j;
47
48         the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
49                                                    sizeof(*ptable));
50         if (the_lnet.ln_peer_tables == NULL) {
51                 CERROR("Failed to allocate cpu-partition peer tables\n");
52                 return -ENOMEM;
53         }
54
55         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
56                 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
57                                  LNET_PEER_HASH_SIZE * sizeof(*hash));
58                 if (hash == NULL) {
59                         CERROR("Failed to create peer hash table\n");
60                         lnet_peer_tables_destroy();
61                         return -ENOMEM;
62                 }
63
64                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
65                         INIT_LIST_HEAD(&hash[j]);
66                 ptable->pt_hash = hash; /* sign of initialization */
67         }
68
69         return 0;
70 }
71
72 void
73 lnet_peer_tables_destroy(void)
74 {
75         struct lnet_peer_table  *ptable;
76         struct list_head        *hash;
77         int                     i;
78         int                     j;
79
80         if (the_lnet.ln_peer_tables == NULL)
81                 return;
82
83         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
84                 hash = ptable->pt_hash;
85                 if (hash == NULL) /* not intialized */
86                         break;
87
88                 ptable->pt_hash = NULL;
89                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
90                         LASSERT(list_empty(&hash[j]));
91
92                 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
93         }
94
95         cfs_percpt_free(the_lnet.ln_peer_tables);
96         the_lnet.ln_peer_tables = NULL;
97 }
98
99 static void
100 lnet_peer_table_cleanup_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable)
101 {
102         int                      i;
103         struct lnet_peer_ni     *lp;
104         struct lnet_peer_ni     *tmp;
105
106         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
107                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
108                                          lpni_hashlist) {
109                         if (ni != NULL && ni->ni_net != lp->lpni_net)
110                                 continue;
111                         list_del_init(&lp->lpni_hashlist);
112                         /* Lose hash table's ref */
113                         ptable->pt_zombies++;
114                         lnet_peer_ni_decref_locked(lp);
115                 }
116         }
117 }
118
119 static void
120 lnet_peer_table_finalize_wait_locked(struct lnet_peer_table *ptable,
121                                      int cpt_locked)
122 {
123         int     i;
124
125         for (i = 3; ptable->pt_zombies != 0; i++) {
126                 lnet_net_unlock(cpt_locked);
127
128                 if (IS_PO2(i)) {
129                         CDEBUG(D_WARNING,
130                                "Waiting for %d zombies on peer table\n",
131                                ptable->pt_zombies);
132                 }
133                 set_current_state(TASK_UNINTERRUPTIBLE);
134                 schedule_timeout(cfs_time_seconds(1) >> 1);
135                 lnet_net_lock(cpt_locked);
136         }
137 }
138
139 static void
140 lnet_peer_table_del_rtrs_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable,
141                                 int cpt_locked)
142 {
143         struct lnet_peer_ni     *lp;
144         struct lnet_peer_ni     *tmp;
145         lnet_nid_t              lpni_nid;
146         int                     i;
147
148         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
149                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
150                                          lpni_hashlist) {
151                         if (ni->ni_net != lp->lpni_net)
152                                 continue;
153
154                         if (lp->lpni_rtr_refcount == 0)
155                                 continue;
156
157                         lpni_nid = lp->lpni_nid;
158
159                         lnet_net_unlock(cpt_locked);
160                         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lpni_nid);
161                         lnet_net_lock(cpt_locked);
162                 }
163         }
164 }
165
166 void
167 lnet_peer_tables_cleanup(lnet_ni_t *ni)
168 {
169         int                             i;
170         struct lnet_peer_table          *ptable;
171
172         LASSERT(the_lnet.ln_shutdown || ni != NULL);
173         /* If just deleting the peers for a NI, get rid of any routes these
174          * peers are gateways for. */
175         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
176                 lnet_net_lock(LNET_LOCK_EX);
177                 lnet_peer_table_del_rtrs_locked(ni, ptable, i);
178                 lnet_net_unlock(LNET_LOCK_EX);
179         }
180
181         /* Start the cleanup process */
182         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
183                 lnet_net_lock(LNET_LOCK_EX);
184                 lnet_peer_table_cleanup_locked(ni, ptable);
185                 lnet_net_unlock(LNET_LOCK_EX);
186         }
187
188         /* Wait until all peers have been destroyed. */
189         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
190                 lnet_net_lock(LNET_LOCK_EX);
191                 lnet_peer_table_finalize_wait_locked(ptable, i);
192                 lnet_net_unlock(LNET_LOCK_EX);
193         }
194 }
195
196 static struct lnet_peer_ni *
197 lnet_get_peer_ni_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
198 {
199         struct list_head        *peers;
200         struct lnet_peer_ni     *lp;
201
202         LASSERT(!the_lnet.ln_shutdown);
203
204         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
205         list_for_each_entry(lp, peers, lpni_hashlist) {
206                 if (lp->lpni_nid == nid) {
207                         lnet_peer_ni_addref_locked(lp);
208                         return lp;
209                 }
210         }
211
212         return NULL;
213 }
214
215 struct lnet_peer_ni *
216 lnet_find_peer_ni_locked(lnet_nid_t nid, int cpt)
217 {
218         struct lnet_peer_ni *lpni;
219         struct lnet_peer_table *ptable;
220
221         ptable = the_lnet.ln_peer_tables[cpt];
222         lpni = lnet_get_peer_ni_locked(ptable, nid);
223
224         return lpni;
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 static int
261 lnet_build_peer_hierarchy(struct lnet_peer_ni *lpni)
262 {
263         struct lnet_peer *peer;
264         struct lnet_peer_net *peer_net;
265         __u32 lpni_net = LNET_NIDNET(lpni->lpni_nid);
266
267         peer = NULL;
268         peer_net = NULL;
269
270         LIBCFS_ALLOC(peer, sizeof(*peer));
271         if (peer == NULL)
272                 return -ENOMEM;
273
274         LIBCFS_ALLOC(peer_net, sizeof(*peer_net));
275         if (peer_net == NULL) {
276                 LIBCFS_FREE(peer, sizeof(*peer));
277                 return -ENOMEM;
278         }
279
280         INIT_LIST_HEAD(&peer->lp_on_lnet_peer_list);
281         INIT_LIST_HEAD(&peer->lp_peer_nets);
282         INIT_LIST_HEAD(&peer_net->lpn_on_peer_list);
283         INIT_LIST_HEAD(&peer_net->lpn_peer_nis);
284
285         /* build the hierarchy */
286         peer_net->lpn_net_id = lpni_net;
287         peer_net->lpn_peer = peer;
288         lpni->lpni_peer_net = peer_net;
289         peer->lp_primary_nid = lpni->lpni_nid;
290         list_add_tail(&peer_net->lpn_on_peer_list, &peer->lp_peer_nets);
291         list_add_tail(&lpni->lpni_on_peer_net_list, &peer_net->lpn_peer_nis);
292         list_add_tail(&peer->lp_on_lnet_peer_list, &the_lnet.ln_peers);
293
294         return 0;
295 }
296
297 void
298 lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
299 {
300         struct lnet_peer_table *ptable;
301
302         LASSERT(atomic_read(&lpni->lpni_refcount) == 0);
303         LASSERT(lpni->lpni_rtr_refcount == 0);
304         LASSERT(list_empty(&lpni->lpni_txq));
305         LASSERT(list_empty(&lpni->lpni_hashlist));
306         LASSERT(lpni->lpni_txqnob == 0);
307         LASSERT(lpni->lpni_peer_net != NULL);
308         LASSERT(lpni->lpni_peer_net->lpn_peer != NULL);
309
310         ptable = the_lnet.ln_peer_tables[lpni->lpni_cpt];
311         LASSERT(ptable->pt_number > 0);
312         ptable->pt_number--;
313
314         lpni->lpni_net = NULL;
315
316         lnet_try_destroy_peer_hierarchy_locked(lpni);
317
318         LIBCFS_FREE(lpni, sizeof(*lpni));
319
320         LASSERT(ptable->pt_zombies > 0);
321         ptable->pt_zombies--;
322 }
323
324 int
325 lnet_nid2peerni_locked(struct lnet_peer_ni **lpnip, lnet_nid_t nid, int cpt)
326 {
327         struct lnet_peer_table  *ptable;
328         struct lnet_peer_ni     *lpni = NULL;
329         struct lnet_peer_ni     *lpni2;
330         int                     cpt2;
331         int                     rc = 0;
332
333         *lpnip = NULL;
334         if (the_lnet.ln_shutdown) /* it's shutting down */
335                 return -ESHUTDOWN;
336
337         /*
338          * calculate cpt2 with the standard hash function
339          * This cpt2 becomes the slot where we'll find or create the peer.
340          */
341         cpt2 = lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
342
343         /*
344          * Any changes to the peer tables happen under exclusive write
345          * lock. Any reads to the peer tables can be done via a standard
346          * CPT read lock.
347          */
348         if (cpt != LNET_LOCK_EX) {
349                 lnet_net_unlock(cpt);
350                 lnet_net_lock(LNET_LOCK_EX);
351         }
352
353         ptable = the_lnet.ln_peer_tables[cpt2];
354         lpni = lnet_get_peer_ni_locked(ptable, nid);
355         if (lpni != NULL) {
356                 *lpnip = lpni;
357                 if (cpt != LNET_LOCK_EX) {
358                         lnet_net_unlock(LNET_LOCK_EX);
359                         lnet_net_lock(cpt);
360                 }
361                 return 0;
362         }
363
364         /*
365          * take extra refcount in case another thread has shutdown LNet
366          * and destroyed locks and peer-table before I finish the allocation
367          */
368         ptable->pt_number++;
369         lnet_net_unlock(LNET_LOCK_EX);
370
371         LIBCFS_CPT_ALLOC(lpni, lnet_cpt_table(), cpt2, sizeof(*lpni));
372
373         if (lpni == NULL) {
374                 rc = -ENOMEM;
375                 lnet_net_lock(cpt);
376                 goto out;
377         }
378
379         INIT_LIST_HEAD(&lpni->lpni_txq);
380         INIT_LIST_HEAD(&lpni->lpni_rtrq);
381         INIT_LIST_HEAD(&lpni->lpni_routes);
382
383         lpni->lpni_alive = !lnet_peers_start_down(); /* 1 bit!! */
384         lpni->lpni_last_alive = cfs_time_current(); /* assumes alive */
385         lpni->lpni_ping_feats = LNET_PING_FEAT_INVAL;
386         lpni->lpni_nid = nid;
387         lpni->lpni_cpt = cpt2;
388         atomic_set(&lpni->lpni_refcount, 2);    /* 1 for caller; 1 for hash */
389
390         rc = lnet_build_peer_hierarchy(lpni);
391         if (rc != 0)
392                 goto out;
393
394         lnet_net_lock(LNET_LOCK_EX);
395
396         if (the_lnet.ln_shutdown) {
397                 rc = -ESHUTDOWN;
398                 goto out;
399         }
400
401         lpni2 = lnet_get_peer_ni_locked(ptable, nid);
402         if (lpni2 != NULL) {
403                 *lpnip = lpni2;
404                 goto out;
405         }
406
407         lpni->lpni_net = lnet_get_net_locked(LNET_NIDNET(lpni->lpni_nid));
408         lpni->lpni_txcredits    =
409         lpni->lpni_mintxcredits =
410                 lpni->lpni_net->net_tunables.lct_peer_tx_credits;
411         lpni->lpni_rtrcredits    =
412         lpni->lpni_minrtrcredits =
413                 lnet_peer_buffer_credits(lpni->lpni_net);
414
415         list_add_tail(&lpni->lpni_hashlist,
416                         &ptable->pt_hash[lnet_nid2peerhash(nid)]);
417         ptable->pt_version++;
418         *lpnip = lpni;
419
420         if (cpt != LNET_LOCK_EX) {
421                 lnet_net_unlock(LNET_LOCK_EX);
422                 lnet_net_lock(cpt);
423         }
424
425         return 0;
426 out:
427         if (lpni != NULL) {
428                 lnet_try_destroy_peer_hierarchy_locked(lpni);
429                 LIBCFS_FREE(lpni, sizeof(*lpni));
430         }
431         ptable->pt_number--;
432         if (cpt != LNET_LOCK_EX) {
433                 lnet_net_unlock(LNET_LOCK_EX);
434                 lnet_net_lock(cpt);
435         }
436         return rc;
437 }
438
439 void
440 lnet_debug_peer(lnet_nid_t nid)
441 {
442         char                    *aliveness = "NA";
443         struct lnet_peer_ni     *lp;
444         int                     rc;
445         int                     cpt;
446
447         cpt = lnet_cpt_of_nid(nid, NULL);
448         lnet_net_lock(cpt);
449
450         rc = lnet_nid2peerni_locked(&lp, nid, cpt);
451         if (rc != 0) {
452                 lnet_net_unlock(cpt);
453                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
454                 return;
455         }
456
457         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
458                 aliveness = lp->lpni_alive ? "up" : "down";
459
460         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
461                libcfs_nid2str(lp->lpni_nid), atomic_read(&lp->lpni_refcount),
462                aliveness, lp->lpni_net->net_tunables.lct_peer_tx_credits,
463                lp->lpni_rtrcredits, lp->lpni_minrtrcredits,
464                lp->lpni_txcredits, lp->lpni_mintxcredits, lp->lpni_txqnob);
465
466         lnet_peer_ni_decref_locked(lp);
467
468         lnet_net_unlock(cpt);
469 }
470
471 int lnet_get_peer_info(__u32 peer_index, __u64 *nid,
472                        char aliveness[LNET_MAX_STR_LEN],
473                        __u32 *cpt_iter, __u32 *refcount,
474                        __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
475                        __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
476                        __u32 *peer_tx_qnob)
477 {
478         struct lnet_peer_table          *peer_table;
479         struct lnet_peer_ni             *lp;
480         int                             j;
481         int                             lncpt;
482         bool                            found = false;
483
484         /* get the number of CPTs */
485         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
486
487         /* if the cpt number to be examined is >= the number of cpts in
488          * the system then indicate that there are no more cpts to examin
489          */
490         if (*cpt_iter >= lncpt)
491                 return -ENOENT;
492
493         /* get the current table */
494         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
495         /* if the ptable is NULL then there are no more cpts to examine */
496         if (peer_table == NULL)
497                 return -ENOENT;
498
499         lnet_net_lock(*cpt_iter);
500
501         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
502                 struct list_head *peers = &peer_table->pt_hash[j];
503
504                 list_for_each_entry(lp, peers, lpni_hashlist) {
505                         if (peer_index-- > 0)
506                                 continue;
507
508                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
509                         if (lnet_isrouter(lp) ||
510                                 lnet_peer_aliveness_enabled(lp))
511                                 snprintf(aliveness, LNET_MAX_STR_LEN,
512                                          lp->lpni_alive ? "up" : "down");
513
514                         *nid = lp->lpni_nid;
515                         *refcount = atomic_read(&lp->lpni_refcount);
516                         *ni_peer_tx_credits =
517                                 lp->lpni_net->net_tunables.lct_peer_tx_credits;
518                         *peer_tx_credits = lp->lpni_txcredits;
519                         *peer_rtr_credits = lp->lpni_rtrcredits;
520                         *peer_min_rtr_credits = lp->lpni_mintxcredits;
521                         *peer_tx_qnob = lp->lpni_txqnob;
522
523                         found = true;
524                 }
525
526         }
527         lnet_net_unlock(*cpt_iter);
528
529         *cpt_iter = lncpt;
530
531         return found ? 0 : -ENOENT;
532 }