Whamcloud - gitweb
LU-2456 lnet: Dynamic LNet Configuration (DLC) IOCTL changes
[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
43 int
44 lnet_peer_tables_create(void)
45 {
46         struct lnet_peer_table  *ptable;
47         struct list_head        *hash;
48         int                     i;
49         int                     j;
50
51         the_lnet.ln_peer_tables = cfs_percpt_alloc(lnet_cpt_table(),
52                                                    sizeof(*ptable));
53         if (the_lnet.ln_peer_tables == NULL) {
54                 CERROR("Failed to allocate cpu-partition peer tables\n");
55                 return -ENOMEM;
56         }
57
58         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
59                 INIT_LIST_HEAD(&ptable->pt_deathrow);
60
61                 LIBCFS_CPT_ALLOC(hash, lnet_cpt_table(), i,
62                                  LNET_PEER_HASH_SIZE * sizeof(*hash));
63                 if (hash == NULL) {
64                         CERROR("Failed to create peer hash table\n");
65                         lnet_peer_tables_destroy();
66                         return -ENOMEM;
67                 }
68
69                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
70                         INIT_LIST_HEAD(&hash[j]);
71                 ptable->pt_hash = hash; /* sign of initialization */
72         }
73
74         return 0;
75 }
76
77 void
78 lnet_peer_tables_destroy(void)
79 {
80         struct lnet_peer_table  *ptable;
81         struct list_head        *hash;
82         int                     i;
83         int                     j;
84
85         if (the_lnet.ln_peer_tables == NULL)
86                 return;
87
88         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
89                 hash = ptable->pt_hash;
90                 if (hash == NULL) /* not intialized */
91                         break;
92
93                 LASSERT(list_empty(&ptable->pt_deathrow));
94
95                 ptable->pt_hash = NULL;
96                 for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
97                         LASSERT(list_empty(&hash[j]));
98
99                 LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
100         }
101
102         cfs_percpt_free(the_lnet.ln_peer_tables);
103         the_lnet.ln_peer_tables = NULL;
104 }
105
106 static void
107 lnet_peer_table_cleanup_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable)
108 {
109         int              i;
110         lnet_peer_t     *lp;
111         lnet_peer_t     *tmp;
112
113         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
114                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
115                                          lp_hashlist) {
116                         if (ni != NULL && ni != lp->lp_ni)
117                                 continue;
118                         list_del_init(&lp->lp_hashlist);
119                         /* Lose hash table's ref */
120                         ptable->pt_zombies++;
121                         lnet_peer_decref_locked(lp);
122                 }
123         }
124 }
125
126 static void
127 lnet_peer_table_deathrow_wait_locked(struct lnet_peer_table *ptable,
128                                      int cpt_locked)
129 {
130         int     i;
131
132         for (i = 3; ptable->pt_zombies != 0; i++) {
133                 lnet_net_unlock(cpt_locked);
134
135                 if (IS_PO2(i)) {
136                         CDEBUG(D_WARNING,
137                                "Waiting for %d zombies on peer table\n",
138                                ptable->pt_zombies);
139                 }
140                 cfs_pause(cfs_time_seconds(1) >> 1);
141                 lnet_net_lock(cpt_locked);
142         }
143 }
144
145 static void
146 lnet_peer_table_del_rtrs_locked(lnet_ni_t *ni, struct lnet_peer_table *ptable,
147                                 int cpt_locked)
148 {
149         lnet_peer_t     *lp;
150         lnet_peer_t     *tmp;
151         lnet_nid_t       lp_nid;
152         int              i;
153
154         for (i = 0; i < LNET_PEER_HASH_SIZE; i++) {
155                 list_for_each_entry_safe(lp, tmp, &ptable->pt_hash[i],
156                                          lp_hashlist) {
157                         if (ni != lp->lp_ni)
158                                 continue;
159
160                         if (lp->lp_rtr_refcount == 0)
161                                 continue;
162
163                         lp_nid = lp->lp_nid;
164
165                         lnet_net_unlock(cpt_locked);
166                         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), lp_nid);
167                         lnet_net_lock(cpt_locked);
168                 }
169         }
170 }
171
172 void
173 lnet_peer_tables_cleanup(lnet_ni_t *ni)
174 {
175         int                     i;
176         struct lnet_peer_table  *ptable;
177         lnet_peer_t             *lp;
178         struct list_head        deathrow;
179
180         INIT_LIST_HEAD(&deathrow);
181
182         LASSERT(the_lnet.ln_shutdown || ni != NULL);
183         /* If just deleting the peers for a NI, get rid of any routes these
184          * peers are gateways for. */
185         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
186                 lnet_net_lock(i);
187                 lnet_peer_table_del_rtrs_locked(ni, ptable, i);
188                 lnet_net_unlock(i);
189         }
190
191         /* Start the process of moving the applicable peers to
192          * deathrow. */
193         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
194                 lnet_net_lock(i);
195                 lnet_peer_table_cleanup_locked(ni, ptable);
196                 lnet_net_unlock(i);
197         }
198
199         /* Cleanup all entries on deathrow. */
200         cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
201                 lnet_net_lock(i);
202                 lnet_peer_table_deathrow_wait_locked(ptable, i);
203                 list_splice_init(&ptable->pt_deathrow, &deathrow);
204                 lnet_net_unlock(i);
205         }
206
207         while (!list_empty(&deathrow)) {
208                 lp = list_entry(deathrow.next, lnet_peer_t, lp_hashlist);
209                 list_del(&lp->lp_hashlist);
210                 LIBCFS_FREE(lp, sizeof(*lp));
211         }
212 }
213
214 void
215 lnet_destroy_peer_locked(lnet_peer_t *lp)
216 {
217         struct lnet_peer_table *ptable;
218
219         LASSERT(lp->lp_refcount == 0);
220         LASSERT(lp->lp_rtr_refcount == 0);
221         LASSERT(list_empty(&lp->lp_txq));
222         LASSERT(list_empty(&lp->lp_hashlist));
223         LASSERT(lp->lp_txqnob == 0);
224
225         ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
226         LASSERT(ptable->pt_number > 0);
227         ptable->pt_number--;
228
229         lnet_ni_decref_locked(lp->lp_ni, lp->lp_cpt);
230         lp->lp_ni = NULL;
231
232         list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
233         LASSERT(ptable->pt_zombies > 0);
234         ptable->pt_zombies--;
235 }
236
237 lnet_peer_t *
238 lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
239 {
240         struct list_head *peers;
241         lnet_peer_t      *lp;
242
243         LASSERT(!the_lnet.ln_shutdown);
244
245         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
246         list_for_each_entry(lp, peers, lp_hashlist) {
247                 if (lp->lp_nid == nid) {
248                         lnet_peer_addref_locked(lp);
249                         return lp;
250                 }
251         }
252
253         return NULL;
254 }
255
256 int
257 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
258 {
259         struct lnet_peer_table  *ptable;
260         lnet_peer_t             *lp = NULL;
261         lnet_peer_t             *lp2;
262         int                     cpt2;
263         int                     rc = 0;
264
265         *lpp = NULL;
266         if (the_lnet.ln_shutdown) /* it's shutting down */
267                 return -ESHUTDOWN;
268
269         /* cpt can be LNET_LOCK_EX if it's called from router functions */
270         cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
271
272         ptable = the_lnet.ln_peer_tables[cpt2];
273         lp = lnet_find_peer_locked(ptable, nid);
274         if (lp != NULL) {
275                 *lpp = lp;
276                 return 0;
277         }
278
279         if (!list_empty(&ptable->pt_deathrow)) {
280                 lp = list_entry(ptable->pt_deathrow.next,
281                                 lnet_peer_t, lp_hashlist);
282                 list_del(&lp->lp_hashlist);
283         }
284
285         /*
286          * take extra refcount in case another thread has shutdown LNet
287          * and destroyed locks and peer-table before I finish the allocation
288          */
289         ptable->pt_number++;
290         lnet_net_unlock(cpt);
291
292         if (lp != NULL)
293                 memset(lp, 0, sizeof(*lp));
294         else
295                 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
296
297         if (lp == NULL) {
298                 rc = -ENOMEM;
299                 lnet_net_lock(cpt);
300                 goto out;
301         }
302
303         INIT_LIST_HEAD(&lp->lp_txq);
304         INIT_LIST_HEAD(&lp->lp_rtrq);
305         INIT_LIST_HEAD(&lp->lp_routes);
306
307         lp->lp_notify = 0;
308         lp->lp_notifylnd = 0;
309         lp->lp_notifying = 0;
310         lp->lp_alive_count = 0;
311         lp->lp_timestamp = 0;
312         lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
313         lp->lp_last_alive = cfs_time_current(); /* assumes alive */
314         lp->lp_last_query = 0; /* haven't asked NI yet */
315         lp->lp_ping_timestamp = 0;
316         lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
317         lp->lp_nid = nid;
318         lp->lp_cpt = cpt2;
319         lp->lp_refcount = 2;    /* 1 for caller; 1 for hash */
320         lp->lp_rtr_refcount = 0;
321
322         lnet_net_lock(cpt);
323
324         if (the_lnet.ln_shutdown) {
325                 rc = -ESHUTDOWN;
326                 goto out;
327         }
328
329         lp2 = lnet_find_peer_locked(ptable, nid);
330         if (lp2 != NULL) {
331                 *lpp = lp2;
332                 goto out;
333         }
334
335         lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
336         if (lp->lp_ni == NULL) {
337                 rc = -EHOSTUNREACH;
338                 goto out;
339         }
340
341         lp->lp_txcredits    =
342         lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
343         lp->lp_rtrcredits    =
344         lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
345
346         list_add_tail(&lp->lp_hashlist,
347                       &ptable->pt_hash[lnet_nid2peerhash(nid)]);
348         ptable->pt_version++;
349         *lpp = lp;
350
351         return 0;
352 out:
353         if (lp != NULL)
354                 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
355         ptable->pt_number--;
356         return rc;
357 }
358
359 void
360 lnet_debug_peer(lnet_nid_t nid)
361 {
362         char            *aliveness = "NA";
363         lnet_peer_t     *lp;
364         int             rc;
365         int             cpt;
366
367         cpt = lnet_cpt_of_nid(nid);
368         lnet_net_lock(cpt);
369
370         rc = lnet_nid2peer_locked(&lp, nid, cpt);
371         if (rc != 0) {
372                 lnet_net_unlock(cpt);
373                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
374                 return;
375         }
376
377         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
378                 aliveness = lp->lp_alive ? "up" : "down";
379
380         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
381                libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
382                aliveness, lp->lp_ni->ni_peertxcredits,
383                lp->lp_rtrcredits, lp->lp_minrtrcredits,
384                lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
385
386         lnet_peer_decref_locked(lp);
387
388         lnet_net_unlock(cpt);
389 }