Whamcloud - gitweb
LU-2456 lnet: DLC user/kernel space glue code
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/lnet/peer.c
37  */
38
39 #define DEBUG_SUBSYSTEM S_LNET
40
41 #include <lnet/lib-lnet.h>
42 #include <lnet/lib-dlc.h>
43
44 int
45 lnet_peer_tables_create(void)
46 {
47         struct lnet_peer_table  *ptable;
48         struct list_head        *hash;
49         int                     i;
50         int                     j;
51
52         the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
53                                                    sizeof(*ptable));
54         if (the_lnet.ln_peer_tables == NULL) {
55                 CERROR("Failed to allocate cpu-partition peer tables\n");
56                 return -ENOMEM;
57         }
58
59         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
60                 INIT_LIST_HEAD(&ptable->pt_deathrow);
61
62                 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
63                                  LNET_PEER_HASH_SIZE * sizeof(*hash));
64                 if (hash == NULL) {
65                         CERROR("Failed to create peer hash table\n");
66                         lnet_peer_tables_destroy();
67                         return -ENOMEM;
68                 }
69
70                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
71                         INIT_LIST_HEAD(&hash[j]);
72                 ptable->pt_hash = hash; /* sign of initialization */
73         }
74
75         return 0;
76 }
77
78 void
79 lnet_peer_tables_destroy(void)
80 {
81         struct lnet_peer_table  *ptable;
82         struct list_head        *hash;
83         int                     i;
84         int                     j;
85
86         if (the_lnet.ln_peer_tables == NULL)
87                 return;
88
89         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
90                 hash = ptable->pt_hash;
91                 if (hash == NULL) /* not intialized */
92                         break;
93
94                 LASSERT(list_empty(&ptable->pt_deathrow));
95
96                 ptable->pt_hash = NULL;
97                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
98                         LASSERT(list_empty(&hash[j]));
99
100                 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
101         }
102
103         cfs_percpt_free(the_lnet.ln_peer_tables);
104         the_lnet.ln_peer_tables = NULL;
105 }
106
107 static void
108 lnet_peer_table_cleanup_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable)
109 {
110         int              i;
111         lnet_peer_t     *lp;
112         lnet_peer_t     *tmp;
113
114         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
115                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
116                                          lp_hashlist) {
117                         if (ni != NULL && ni != lp->lp_ni)
118                                 continue;
119                         list_del_init(&lp->lp_hashlist);
120                         /* Lose hash table's ref */
121                         ptable->pt_zombies++;
122                         lnet_peer_decref_locked(lp);
123                 }
124         }
125 }
126
127 static void
128 lnet_peer_table_deathrow_wait_locked(struct lnet_peer_table *ptable,
129                                      int cpt_locked)
130 {
131         int     i;
132
133         for (i = 3; ptable->pt_zombies != 0; i++) {
134                 lnet_net_unlock(cpt_locked);
135
136                 if (IS_PO2(i)) {
137                         CDEBUG(D_WARNING,
138                                "Waiting for %d zombies on peer table\n",
139                                ptable->pt_zombies);
140                 }
141                 cfs_pause(cfs_time_seconds(1) >> 1);
142                 lnet_net_lock(cpt_locked);
143         }
144 }
145
146 static void
147 lnet_peer_table_del_rtrs_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable,
148                                 int cpt_locked)
149 {
150         lnet_peer_t     *lp;
151         lnet_peer_t     *tmp;
152         lnet_nid_t       lp_nid;
153         int              i;
154
155         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
156                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
157                                          lp_hashlist) {
158                         if (ni != lp->lp_ni)
159                                 continue;
160
161                         if (lp->lp_rtr_refcount == 0)
162                                 continue;
163
164                         lp_nid = lp->lp_nid;
165
166                         lnet_net_unlock(cpt_locked);
167                         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lp_nid);
168                         lnet_net_lock(cpt_locked);
169                 }
170         }
171 }
172
173 void
174 lnet_peer_tables_cleanup(lnet_ni_t *ni)
175 {
176         int                     i;
177         struct lnet_peer_table  *ptable;
178         lnet_peer_t             *lp;
179         struct list_head        deathrow;
180
181         INIT_LIST_HEAD(&deathrow);
182
183         LASSERT(the_lnet.ln_shutdown || ni != NULL);
184         /* If just deleting the peers for a NI, get rid of any routes these
185          * peers are gateways for. */
186         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
187                 lnet_net_lock(i);
188                 lnet_peer_table_del_rtrs_locked(ni, ptable, i);
189                 lnet_net_unlock(i);
190         }
191
192         /* Start the process of moving the applicable peers to
193          * deathrow. */
194         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
195                 lnet_net_lock(i);
196                 lnet_peer_table_cleanup_locked(ni, ptable);
197                 lnet_net_unlock(i);
198         }
199
200         /* Cleanup all entries on deathrow. */
201         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
202                 lnet_net_lock(i);
203                 lnet_peer_table_deathrow_wait_locked(ptable, i);
204                 list_splice_init(&ptable->pt_deathrow, &deathrow);
205                 lnet_net_unlock(i);
206         }
207
208         while (!list_empty(&deathrow)) {
209                 lp = list_entry(deathrow.next, lnet_peer_t, lp_hashlist);
210                 list_del(&lp->lp_hashlist);
211                 LIBCFS_FREE(lp, sizeof(*lp));
212         }
213 }
214
215 void
216 lnet_destroy_peer_locked(lnet_peer_t *lp)
217 {
218         struct lnet_peer_table *ptable;
219
220         LASSERT(lp->lp_refcount == 0);
221         LASSERT(lp->lp_rtr_refcount == 0);
222         LASSERT(list_empty(&lp->lp_txq));
223         LASSERT(list_empty(&lp->lp_hashlist));
224         LASSERT(lp->lp_txqnob == 0);
225
226         ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
227         LASSERT(ptable->pt_number > 0);
228         ptable->pt_number--;
229
230         lnet_ni_decref_locked(lp->lp_ni, lp->lp_cpt);
231         lp->lp_ni = NULL;
232
233         list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
234         LASSERT(ptable->pt_zombies > 0);
235         ptable->pt_zombies--;
236 }
237
238 lnet_peer_t *
239 lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
240 {
241         struct list_head *peers;
242         lnet_peer_t      *lp;
243
244         LASSERT(!the_lnet.ln_shutdown);
245
246         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
247         list_for_each_entry(lp, peers, lp_hashlist) {
248                 if (lp->lp_nid == nid) {
249                         lnet_peer_addref_locked(lp);
250                         return lp;
251                 }
252         }
253
254         return NULL;
255 }
256
257 int
258 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
259 {
260         struct lnet_peer_table  *ptable;
261         lnet_peer_t             *lp = NULL;
262         lnet_peer_t             *lp2;
263         int                     cpt2;
264         int                     rc = 0;
265
266         *lpp = NULL;
267         if (the_lnet.ln_shutdown) /* it's shutting down */
268                 return -ESHUTDOWN;
269
270         /* cpt can be LNET_LOCK_EX if it's called from router functions */
271         cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
272
273         ptable = the_lnet.ln_peer_tables[cpt2];
274         lp = lnet_find_peer_locked(ptable, nid);
275         if (lp != NULL) {
276                 *lpp = lp;
277                 return 0;
278         }
279
280         if (!list_empty(&ptable->pt_deathrow)) {
281                 lp = list_entry(ptable->pt_deathrow.next,
282                                 lnet_peer_t, lp_hashlist);
283                 list_del(&lp->lp_hashlist);
284         }
285
286         /*
287          * take extra refcount in case another thread has shutdown LNet
288          * and destroyed locks and peer-table before I finish the allocation
289          */
290         ptable->pt_number++;
291         lnet_net_unlock(cpt);
292
293         if (lp != NULL)
294                 memset(lp, 0, sizeof(*lp));
295         else
296                 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
297
298         if (lp == NULL) {
299                 rc = -ENOMEM;
300                 lnet_net_lock(cpt);
301                 goto out;
302         }
303
304         INIT_LIST_HEAD(&lp->lp_txq);
305         INIT_LIST_HEAD(&lp->lp_rtrq);
306         INIT_LIST_HEAD(&lp->lp_routes);
307
308         lp->lp_notify = 0;
309         lp->lp_notifylnd = 0;
310         lp->lp_notifying = 0;
311         lp->lp_alive_count = 0;
312         lp->lp_timestamp = 0;
313         lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
314         lp->lp_last_alive = cfs_time_current(); /* assumes alive */
315         lp->lp_last_query = 0; /* haven't asked NI yet */
316         lp->lp_ping_timestamp = 0;
317         lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
318         lp->lp_nid = nid;
319         lp->lp_cpt = cpt2;
320         lp->lp_refcount = 2;    /* 1 for caller; 1 for hash */
321         lp->lp_rtr_refcount = 0;
322
323         lnet_net_lock(cpt);
324
325         if (the_lnet.ln_shutdown) {
326                 rc = -ESHUTDOWN;
327                 goto out;
328         }
329
330         lp2 = lnet_find_peer_locked(ptable, nid);
331         if (lp2 != NULL) {
332                 *lpp = lp2;
333                 goto out;
334         }
335
336         lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
337         if (lp->lp_ni == NULL) {
338                 rc = -EHOSTUNREACH;
339                 goto out;
340         }
341
342         lp->lp_txcredits    =
343         lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
344         lp->lp_rtrcredits    =
345         lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
346
347         list_add_tail(&lp->lp_hashlist,
348                       &ptable->pt_hash[lnet_nid2peerhash(nid)]);
349         ptable->pt_version++;
350         *lpp = lp;
351
352         return 0;
353 out:
354         if (lp != NULL)
355                 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
356         ptable->pt_number--;
357         return rc;
358 }
359
360 void
361 lnet_debug_peer(lnet_nid_t nid)
362 {
363         char            *aliveness = "NA";
364         lnet_peer_t     *lp;
365         int             rc;
366         int             cpt;
367
368         cpt = lnet_cpt_of_nid(nid);
369         lnet_net_lock(cpt);
370
371         rc = lnet_nid2peer_locked(&lp, nid, cpt);
372         if (rc != 0) {
373                 lnet_net_unlock(cpt);
374                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
375                 return;
376         }
377
378         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
379                 aliveness = lp->lp_alive ? "up" : "down";
380
381         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
382                libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
383                aliveness, lp->lp_ni->ni_peertxcredits,
384                lp->lp_rtrcredits, lp->lp_minrtrcredits,
385                lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
386
387         lnet_peer_decref_locked(lp);
388
389         lnet_net_unlock(cpt);
390 }
391
392 int lnet_get_peer_info(__u32 peer_index, __u64 *nid,
393                        char aliveness[LNET_MAX_STR_LEN],
394                        __u32 *cpt_iter, __u32 *refcount,
395                        __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
396                        __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
397                        __u32 *peer_tx_qnob)
398 {
399         struct lnet_peer_table  *peer_table;
400         lnet_peer_t             *lp;
401         int                     j;
402         int                     lncpt;
403         bool                    found = false;
404
405         /* get the number of CPTs */
406         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
407
408         /* if the cpt number to be examined is >= the number of cpts in
409          * the system then indicate that there are no more cpts to examin
410          */
411         if (*cpt_iter > lncpt)
412                 return -ENOENT;
413
414         /* get the current table */
415         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
416         /* if the ptable is NULL then there are no more cpts to examine */
417         if (peer_table == NULL)
418                 return -ENOENT;
419
420         lnet_net_lock(*cpt_iter);
421
422         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
423                 struct list_head *peers = &peer_table->pt_hash[j];
424
425                 list_for_each_entry(lp, peers, lp_hashlist) {
426                         if (peer_index-- > 0)
427                                 continue;
428
429                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
430                         if (lnet_isrouter(lp) ||
431                                 lnet_peer_aliveness_enabled(lp))
432                                 snprintf(aliveness, LNET_MAX_STR_LEN,
433                                          lp->lp_alive ? "up" : "down");
434
435                         *nid = lp->lp_nid;
436                         *refcount = lp->lp_refcount;
437                         *ni_peer_tx_credits = lp->lp_ni->ni_peertxcredits;
438                         *peer_tx_credits = lp->lp_txcredits;
439                         *peer_rtr_credits = lp->lp_rtrcredits;
440                         *peer_min_rtr_credits = lp->lp_mintxcredits;
441                         *peer_tx_qnob = lp->lp_txqnob;
442
443                         found = true;
444                 }
445
446         }
447         lnet_net_unlock(*cpt_iter);
448
449         *cpt_iter = lncpt;
450
451         return found ? 0 : -ENOENT;
452 }