Whamcloud - gitweb
LU-7734 lnet: Multi-Rail local NI split
[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                 INIT_LIST_HEAD(&ptable->pt_deathrow);
57
58                 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
59                                  LNET_PEER_HASH_SIZE * sizeof(*hash));
60                 if (hash == NULL) {
61                         CERROR("Failed to create peer hash table\n");
62                         lnet_peer_tables_destroy();
63                         return -ENOMEM;
64                 }
65
66                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
67                         INIT_LIST_HEAD(&hash[j]);
68                 ptable->pt_hash = hash; /* sign of initialization */
69         }
70
71         return 0;
72 }
73
74 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 == NULL)
83                 return;
84
85         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
86                 hash = ptable->pt_hash;
87                 if (hash == NULL) /* not intialized */
88                         break;
89
90                 LASSERT(list_empty(&ptable->pt_deathrow));
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 static void
104 lnet_peer_table_cleanup_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable)
105 {
106         int              i;
107         lnet_peer_t     *lp;
108         lnet_peer_t     *tmp;
109
110         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
111                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
112                                          lp_hashlist) {
113                         if (ni != NULL && ni->ni_net != lp->lp_net)
114                                 continue;
115                         list_del_init(&lp->lp_hashlist);
116                         /* Lose hash table's ref */
117                         ptable->pt_zombies++;
118                         lnet_peer_decref_locked(lp);
119                 }
120         }
121 }
122
123 static void
124 lnet_peer_table_deathrow_wait_locked(struct lnet_peer_table *ptable,
125                                      int cpt_locked)
126 {
127         int     i;
128
129         for (i = 3; ptable->pt_zombies != 0; i++) {
130                 lnet_net_unlock(cpt_locked);
131
132                 if (IS_PO2(i)) {
133                         CDEBUG(D_WARNING,
134                                "Waiting for %d zombies on peer table\n",
135                                ptable->pt_zombies);
136                 }
137                 set_current_state(TASK_UNINTERRUPTIBLE);
138                 schedule_timeout(cfs_time_seconds(1) >> 1);
139                 lnet_net_lock(cpt_locked);
140         }
141 }
142
143 static void
144 lnet_peer_table_del_rtrs_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable,
145                                 int cpt_locked)
146 {
147         lnet_peer_t     *lp;
148         lnet_peer_t     *tmp;
149         lnet_nid_t       lp_nid;
150         int              i;
151
152         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
153                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
154                                          lp_hashlist) {
155                         if (ni->ni_net != lp->lp_net)
156                                 continue;
157
158                         if (lp->lp_rtr_refcount == 0)
159                                 continue;
160
161                         lp_nid = lp->lp_nid;
162
163                         lnet_net_unlock(cpt_locked);
164                         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lp_nid);
165                         lnet_net_lock(cpt_locked);
166                 }
167         }
168 }
169
170 void
171 lnet_peer_tables_cleanup(lnet_ni_t *ni)
172 {
173         int                     i;
174         struct lnet_peer_table  *ptable;
175         lnet_peer_t             *lp;
176         struct list_head        deathrow;
177
178         INIT_LIST_HEAD(&deathrow);
179
180         LASSERT(the_lnet.ln_shutdown || ni != NULL);
181         /* If just deleting the peers for a NI, get rid of any routes these
182          * peers are gateways for. */
183         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
184                 lnet_net_lock(i);
185                 lnet_peer_table_del_rtrs_locked(ni, ptable, i);
186                 lnet_net_unlock(i);
187         }
188
189         /* Start the process of moving the applicable peers to
190          * deathrow. */
191         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
192                 lnet_net_lock(i);
193                 lnet_peer_table_cleanup_locked(ni, ptable);
194                 lnet_net_unlock(i);
195         }
196
197         /* Cleanup all entries on deathrow. */
198         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
199                 lnet_net_lock(i);
200                 lnet_peer_table_deathrow_wait_locked(ptable, i);
201                 list_splice_init(&ptable->pt_deathrow, &deathrow);
202                 lnet_net_unlock(i);
203         }
204
205         while (!list_empty(&deathrow)) {
206                 lp = list_entry(deathrow.next, lnet_peer_t, lp_hashlist);
207                 list_del(&lp->lp_hashlist);
208                 LIBCFS_FREE(lp, sizeof(*lp));
209         }
210 }
211
212 void
213 lnet_destroy_peer_locked(lnet_peer_t *lp)
214 {
215         struct lnet_peer_table *ptable;
216
217         LASSERT(lp->lp_refcount == 0);
218         LASSERT(lp->lp_rtr_refcount == 0);
219         LASSERT(list_empty(&lp->lp_txq));
220         LASSERT(list_empty(&lp->lp_hashlist));
221         LASSERT(lp->lp_txqnob == 0);
222
223         ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
224         LASSERT(ptable->pt_number > 0);
225         ptable->pt_number--;
226
227         lp->lp_net = NULL;
228
229         list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
230         LASSERT(ptable->pt_zombies > 0);
231         ptable->pt_zombies--;
232 }
233
234 lnet_peer_t *
235 lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
236 {
237         struct list_head *peers;
238         lnet_peer_t      *lp;
239
240         LASSERT(!the_lnet.ln_shutdown);
241
242         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
243         list_for_each_entry(lp, peers, lp_hashlist) {
244                 if (lp->lp_nid == nid) {
245                         lnet_peer_addref_locked(lp);
246                         return lp;
247                 }
248         }
249
250         return NULL;
251 }
252
253 int
254 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
255 {
256         struct lnet_peer_table  *ptable;
257         lnet_peer_t             *lp = NULL;
258         lnet_peer_t             *lp2;
259         int                     cpt2;
260         int                     rc = 0;
261
262         *lpp = NULL;
263         if (the_lnet.ln_shutdown) /* it's shutting down */
264                 return -ESHUTDOWN;
265
266         /* cpt can be LNET_LOCK_EX if it's called from router functions */
267         cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid, NULL);
268
269         ptable = the_lnet.ln_peer_tables[cpt2];
270         lp = lnet_find_peer_locked(ptable, nid);
271         if (lp != NULL) {
272                 *lpp = lp;
273                 return 0;
274         }
275
276         if (!list_empty(&ptable->pt_deathrow)) {
277                 lp = list_entry(ptable->pt_deathrow.next,
278                                 lnet_peer_t, lp_hashlist);
279                 list_del(&lp->lp_hashlist);
280         }
281
282         /*
283          * take extra refcount in case another thread has shutdown LNet
284          * and destroyed locks and peer-table before I finish the allocation
285          */
286         ptable->pt_number++;
287         lnet_net_unlock(cpt);
288
289         if (lp != NULL)
290                 memset(lp, 0, sizeof(*lp));
291         else
292                 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
293
294         if (lp == NULL) {
295                 rc = -ENOMEM;
296                 lnet_net_lock(cpt);
297                 goto out;
298         }
299
300         INIT_LIST_HEAD(&lp->lp_txq);
301         INIT_LIST_HEAD(&lp->lp_rtrq);
302         INIT_LIST_HEAD(&lp->lp_routes);
303
304         lp->lp_notify = 0;
305         lp->lp_notifylnd = 0;
306         lp->lp_notifying = 0;
307         lp->lp_alive_count = 0;
308         lp->lp_timestamp = 0;
309         lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
310         lp->lp_last_alive = cfs_time_current(); /* assumes alive */
311         lp->lp_last_query = 0; /* haven't asked NI yet */
312         lp->lp_ping_timestamp = 0;
313         lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
314         lp->lp_nid = nid;
315         lp->lp_cpt = cpt2;
316         lp->lp_refcount = 2;    /* 1 for caller; 1 for hash */
317         lp->lp_rtr_refcount = 0;
318
319         lnet_net_lock(cpt);
320
321         if (the_lnet.ln_shutdown) {
322                 rc = -ESHUTDOWN;
323                 goto out;
324         }
325
326         lp2 = lnet_find_peer_locked(ptable, nid);
327         if (lp2 != NULL) {
328                 *lpp = lp2;
329                 goto out;
330         }
331
332         lp->lp_net = lnet_get_net_locked(LNET_NIDNET(lp->lp_nid));
333         lp->lp_txcredits    =
334         lp->lp_mintxcredits = lp->lp_net->net_tunables.lct_peer_tx_credits;
335         lp->lp_rtrcredits    =
336         lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_net);
337
338         list_add_tail(&lp->lp_hashlist,
339                       &ptable->pt_hash[lnet_nid2peerhash(nid)]);
340         ptable->pt_version++;
341         *lpp = lp;
342
343         return 0;
344 out:
345         if (lp != NULL)
346                 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
347         ptable->pt_number--;
348         return rc;
349 }
350
351 void
352 lnet_debug_peer(lnet_nid_t nid)
353 {
354         char            *aliveness = "NA";
355         lnet_peer_t     *lp;
356         int             rc;
357         int             cpt;
358
359         cpt = lnet_cpt_of_nid(nid, NULL);
360         lnet_net_lock(cpt);
361
362         rc = lnet_nid2peer_locked(&lp, nid, cpt);
363         if (rc != 0) {
364                 lnet_net_unlock(cpt);
365                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
366                 return;
367         }
368
369         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
370                 aliveness = lp->lp_alive ? "up" : "down";
371
372         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
373                libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
374                aliveness, lp->lp_net->net_tunables.lct_peer_tx_credits,
375                lp->lp_rtrcredits, lp->lp_minrtrcredits,
376                lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
377
378         lnet_peer_decref_locked(lp);
379
380         lnet_net_unlock(cpt);
381 }
382
383 int lnet_get_peer_info(__u32 peer_index, __u64 *nid,
384                        char aliveness[LNET_MAX_STR_LEN],
385                        __u32 *cpt_iter, __u32 *refcount,
386                        __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
387                        __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
388                        __u32 *peer_tx_qnob)
389 {
390         struct lnet_peer_table  *peer_table;
391         lnet_peer_t             *lp;
392         int                     j;
393         int                     lncpt;
394         bool                    found = false;
395
396         /* get the number of CPTs */
397         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
398
399         /* if the cpt number to be examined is >= the number of cpts in
400          * the system then indicate that there are no more cpts to examin
401          */
402         if (*cpt_iter >= lncpt)
403                 return -ENOENT;
404
405         /* get the current table */
406         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
407         /* if the ptable is NULL then there are no more cpts to examine */
408         if (peer_table == NULL)
409                 return -ENOENT;
410
411         lnet_net_lock(*cpt_iter);
412
413         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
414                 struct list_head *peers = &peer_table->pt_hash[j];
415
416                 list_for_each_entry(lp, peers, lp_hashlist) {
417                         if (peer_index-- > 0)
418                                 continue;
419
420                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
421                         if (lnet_isrouter(lp) ||
422                                 lnet_peer_aliveness_enabled(lp))
423                                 snprintf(aliveness, LNET_MAX_STR_LEN,
424                                          lp->lp_alive ? "up" : "down");
425
426                         *nid = lp->lp_nid;
427                         *refcount = lp->lp_refcount;
428                         *ni_peer_tx_credits =
429                                 lp->lp_net->net_tunables.lct_peer_tx_credits;
430                         *peer_tx_credits = lp->lp_txcredits;
431                         *peer_rtr_credits = lp->lp_rtrcredits;
432                         *peer_min_rtr_credits = lp->lp_mintxcredits;
433                         *peer_tx_qnob = lp->lp_txqnob;
434
435                         found = true;
436                 }
437
438         }
439         lnet_net_unlock(*cpt_iter);
440
441         *cpt_iter = lncpt;
442
443         return found ? 0 : -ENOENT;
444 }