Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[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 != lp->lp_ni)
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 != lp->lp_ni)
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         lnet_ni_decref_locked(lp->lp_ni, lp->lp_cpt);
228         lp->lp_ni = NULL;
229
230         list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
231         LASSERT(ptable->pt_zombies > 0);
232         ptable->pt_zombies--;
233 }
234
235 lnet_peer_t *
236 lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
237 {
238         struct list_head *peers;
239         lnet_peer_t      *lp;
240
241         LASSERT(!the_lnet.ln_shutdown);
242
243         peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
244         list_for_each_entry(lp, peers, lp_hashlist) {
245                 if (lp->lp_nid == nid) {
246                         lnet_peer_addref_locked(lp);
247                         return lp;
248                 }
249         }
250
251         return NULL;
252 }
253
254 int
255 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
256 {
257         struct lnet_peer_table  *ptable;
258         lnet_peer_t             *lp = NULL;
259         lnet_peer_t             *lp2;
260         int                     cpt2;
261         int                     rc = 0;
262
263         *lpp = NULL;
264         if (the_lnet.ln_shutdown) /* it's shutting down */
265                 return -ESHUTDOWN;
266
267         /* cpt can be LNET_LOCK_EX if it's called from router functions */
268         cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
269
270         ptable = the_lnet.ln_peer_tables[cpt2];
271         lp = lnet_find_peer_locked(ptable, nid);
272         if (lp != NULL) {
273                 *lpp = lp;
274                 return 0;
275         }
276
277         if (!list_empty(&ptable->pt_deathrow)) {
278                 lp = list_entry(ptable->pt_deathrow.next,
279                                 lnet_peer_t, lp_hashlist);
280                 list_del(&lp->lp_hashlist);
281         }
282
283         /*
284          * take extra refcount in case another thread has shutdown LNet
285          * and destroyed locks and peer-table before I finish the allocation
286          */
287         ptable->pt_number++;
288         lnet_net_unlock(cpt);
289
290         if (lp != NULL)
291                 memset(lp, 0, sizeof(*lp));
292         else
293                 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
294
295         if (lp == NULL) {
296                 rc = -ENOMEM;
297                 lnet_net_lock(cpt);
298                 goto out;
299         }
300
301         INIT_LIST_HEAD(&lp->lp_txq);
302         INIT_LIST_HEAD(&lp->lp_rtrq);
303         INIT_LIST_HEAD(&lp->lp_routes);
304
305         lp->lp_notify = 0;
306         lp->lp_notifylnd = 0;
307         lp->lp_notifying = 0;
308         lp->lp_alive_count = 0;
309         lp->lp_timestamp = 0;
310         lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
311         lp->lp_last_alive = cfs_time_current(); /* assumes alive */
312         lp->lp_last_query = 0; /* haven't asked NI yet */
313         lp->lp_ping_timestamp = 0;
314         lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
315         lp->lp_nid = nid;
316         lp->lp_cpt = cpt2;
317         lp->lp_refcount = 2;    /* 1 for caller; 1 for hash */
318         lp->lp_rtr_refcount = 0;
319
320         lnet_net_lock(cpt);
321
322         if (the_lnet.ln_shutdown) {
323                 rc = -ESHUTDOWN;
324                 goto out;
325         }
326
327         lp2 = lnet_find_peer_locked(ptable, nid);
328         if (lp2 != NULL) {
329                 *lpp = lp2;
330                 goto out;
331         }
332
333         lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
334         if (lp->lp_ni == NULL) {
335                 rc = -EHOSTUNREACH;
336                 goto out;
337         }
338
339         lp->lp_txcredits    =
340         lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
341         lp->lp_rtrcredits    =
342         lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
343
344         list_add_tail(&lp->lp_hashlist,
345                       &ptable->pt_hash[lnet_nid2peerhash(nid)]);
346         ptable->pt_version++;
347         *lpp = lp;
348
349         return 0;
350 out:
351         if (lp != NULL)
352                 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
353         ptable->pt_number--;
354         return rc;
355 }
356
357 void
358 lnet_debug_peer(lnet_nid_t nid)
359 {
360         char            *aliveness = "NA";
361         lnet_peer_t     *lp;
362         int             rc;
363         int             cpt;
364
365         cpt = lnet_cpt_of_nid(nid);
366         lnet_net_lock(cpt);
367
368         rc = lnet_nid2peer_locked(&lp, nid, cpt);
369         if (rc != 0) {
370                 lnet_net_unlock(cpt);
371                 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
372                 return;
373         }
374
375         if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
376                 aliveness = lp->lp_alive ? "up" : "down";
377
378         CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
379                libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
380                aliveness, lp->lp_ni->ni_peertxcredits,
381                lp->lp_rtrcredits, lp->lp_minrtrcredits,
382                lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
383
384         lnet_peer_decref_locked(lp);
385
386         lnet_net_unlock(cpt);
387 }
388
389 int lnet_get_peer_info(__u32 peer_index, __u64 *nid,
390                        char aliveness[LNET_MAX_STR_LEN],
391                        __u32 *cpt_iter, __u32 *refcount,
392                        __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
393                        __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
394                        __u32 *peer_tx_qnob)
395 {
396         struct lnet_peer_table  *peer_table;
397         lnet_peer_t             *lp;
398         int                     j;
399         int                     lncpt;
400         bool                    found = false;
401
402         /* get the number of CPTs */
403         lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
404
405         /* if the cpt number to be examined is >= the number of cpts in
406          * the system then indicate that there are no more cpts to examin
407          */
408         if (*cpt_iter > lncpt)
409                 return -ENOENT;
410
411         /* get the current table */
412         peer_table = the_lnet.ln_peer_tables[*cpt_iter];
413         /* if the ptable is NULL then there are no more cpts to examine */
414         if (peer_table == NULL)
415                 return -ENOENT;
416
417         lnet_net_lock(*cpt_iter);
418
419         for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
420                 struct list_head *peers = &peer_table->pt_hash[j];
421
422                 list_for_each_entry(lp, peers, lp_hashlist) {
423                         if (peer_index-- > 0)
424                                 continue;
425
426                         snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
427                         if (lnet_isrouter(lp) ||
428                                 lnet_peer_aliveness_enabled(lp))
429                                 snprintf(aliveness, LNET_MAX_STR_LEN,
430                                          lp->lp_alive ? "up" : "down");
431
432                         *nid = lp->lp_nid;
433                         *refcount = lp->lp_refcount;
434                         *ni_peer_tx_credits = lp->lp_ni->ni_peertxcredits;
435                         *peer_tx_credits = lp->lp_txcredits;
436                         *peer_rtr_credits = lp->lp_rtrcredits;
437                         *peer_min_rtr_credits = lp->lp_mintxcredits;
438                         *peer_tx_qnob = lp->lp_txqnob;
439
440                         found = true;
441                 }
442
443         }
444         lnet_net_unlock(*cpt_iter);
445
446         *cpt_iter = lncpt;
447
448         return found ? 0 : -ENOENT;
449 }