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, 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 "../../include/linux/lnet/lib-lnet.h"
38 #include "../../include/linux/lnet/lib-dlc.h"
39
40 int
lnet_peer_tables_create(void)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) {
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) {
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
lnet_peer_tables_destroy(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)
83 return;
84
85 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
86 hash = ptable->pt_hash;
87 if (!hash) /* not initialized */
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
lnet_peer_table_cleanup_locked(lnet_ni_t * ni,struct lnet_peer_table * ptable)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 && 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
lnet_peer_table_deathrow_wait_locked(struct lnet_peer_table * ptable,int cpt_locked)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; i++) {
130 lnet_net_unlock(cpt_locked);
131
132 if (is_power_of_2(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
lnet_peer_table_del_rtrs_locked(lnet_ni_t * ni,struct lnet_peer_table * ptable,int cpt_locked)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)
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
lnet_peer_tables_cleanup(lnet_ni_t * ni)171 lnet_peer_tables_cleanup(lnet_ni_t *ni)
172 {
173 struct lnet_peer_table *ptable;
174 struct list_head deathrow;
175 lnet_peer_t *lp;
176 lnet_peer_t *temp;
177 int i;
178
179 INIT_LIST_HEAD(&deathrow);
180
181 LASSERT(the_lnet.ln_shutdown || ni);
182 /*
183 * If just deleting the peers for a NI, get rid of any routes these
184 * peers are gateways for.
185 */
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 /*
193 * Start the process of moving the applicable peers to
194 * deathrow.
195 */
196 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
197 lnet_net_lock(i);
198 lnet_peer_table_cleanup_locked(ni, ptable);
199 lnet_net_unlock(i);
200 }
201
202 /* Cleanup all entries on deathrow. */
203 cfs_percpt_for_each(ptable, i, the_lnet.ln_peer_tables) {
204 lnet_net_lock(i);
205 lnet_peer_table_deathrow_wait_locked(ptable, i);
206 list_splice_init(&ptable->pt_deathrow, &deathrow);
207 lnet_net_unlock(i);
208 }
209
210 list_for_each_entry_safe(lp, temp, &deathrow, lp_hashlist) {
211 list_del(&lp->lp_hashlist);
212 LIBCFS_FREE(lp, sizeof(*lp));
213 }
214 }
215
216 void
lnet_destroy_peer_locked(lnet_peer_t * lp)217 lnet_destroy_peer_locked(lnet_peer_t *lp)
218 {
219 struct lnet_peer_table *ptable;
220
221 LASSERT(!lp->lp_refcount);
222 LASSERT(!lp->lp_rtr_refcount);
223 LASSERT(list_empty(&lp->lp_txq));
224 LASSERT(list_empty(&lp->lp_hashlist));
225 LASSERT(!lp->lp_txqnob);
226
227 ptable = the_lnet.ln_peer_tables[lp->lp_cpt];
228 LASSERT(ptable->pt_number > 0);
229 ptable->pt_number--;
230
231 lnet_ni_decref_locked(lp->lp_ni, lp->lp_cpt);
232 lp->lp_ni = NULL;
233
234 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
235 LASSERT(ptable->pt_zombies > 0);
236 ptable->pt_zombies--;
237 }
238
239 lnet_peer_t *
lnet_find_peer_locked(struct lnet_peer_table * ptable,lnet_nid_t nid)240 lnet_find_peer_locked(struct lnet_peer_table *ptable, lnet_nid_t nid)
241 {
242 struct list_head *peers;
243 lnet_peer_t *lp;
244
245 LASSERT(!the_lnet.ln_shutdown);
246
247 peers = &ptable->pt_hash[lnet_nid2peerhash(nid)];
248 list_for_each_entry(lp, peers, lp_hashlist) {
249 if (lp->lp_nid == nid) {
250 lnet_peer_addref_locked(lp);
251 return lp;
252 }
253 }
254
255 return NULL;
256 }
257
258 int
lnet_nid2peer_locked(lnet_peer_t ** lpp,lnet_nid_t nid,int cpt)259 lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt)
260 {
261 struct lnet_peer_table *ptable;
262 lnet_peer_t *lp = NULL;
263 lnet_peer_t *lp2;
264 int cpt2;
265 int rc = 0;
266
267 *lpp = NULL;
268 if (the_lnet.ln_shutdown) /* it's shutting down */
269 return -ESHUTDOWN;
270
271 /* cpt can be LNET_LOCK_EX if it's called from router functions */
272 cpt2 = cpt != LNET_LOCK_EX ? cpt : lnet_cpt_of_nid_locked(nid);
273
274 ptable = the_lnet.ln_peer_tables[cpt2];
275 lp = lnet_find_peer_locked(ptable, nid);
276 if (lp) {
277 *lpp = lp;
278 return 0;
279 }
280
281 if (!list_empty(&ptable->pt_deathrow)) {
282 lp = list_entry(ptable->pt_deathrow.next,
283 lnet_peer_t, lp_hashlist);
284 list_del(&lp->lp_hashlist);
285 }
286
287 /*
288 * take extra refcount in case another thread has shutdown LNet
289 * and destroyed locks and peer-table before I finish the allocation
290 */
291 ptable->pt_number++;
292 lnet_net_unlock(cpt);
293
294 if (lp)
295 memset(lp, 0, sizeof(*lp));
296 else
297 LIBCFS_CPT_ALLOC(lp, lnet_cpt_table(), cpt2, sizeof(*lp));
298
299 if (!lp) {
300 rc = -ENOMEM;
301 lnet_net_lock(cpt);
302 goto out;
303 }
304
305 INIT_LIST_HEAD(&lp->lp_txq);
306 INIT_LIST_HEAD(&lp->lp_rtrq);
307 INIT_LIST_HEAD(&lp->lp_routes);
308
309 lp->lp_notify = 0;
310 lp->lp_notifylnd = 0;
311 lp->lp_notifying = 0;
312 lp->lp_alive_count = 0;
313 lp->lp_timestamp = 0;
314 lp->lp_alive = !lnet_peers_start_down(); /* 1 bit!! */
315 lp->lp_last_alive = cfs_time_current(); /* assumes alive */
316 lp->lp_last_query = 0; /* haven't asked NI yet */
317 lp->lp_ping_timestamp = 0;
318 lp->lp_ping_feats = LNET_PING_FEAT_INVAL;
319 lp->lp_nid = nid;
320 lp->lp_cpt = cpt2;
321 lp->lp_refcount = 2; /* 1 for caller; 1 for hash */
322 lp->lp_rtr_refcount = 0;
323
324 lnet_net_lock(cpt);
325
326 if (the_lnet.ln_shutdown) {
327 rc = -ESHUTDOWN;
328 goto out;
329 }
330
331 lp2 = lnet_find_peer_locked(ptable, nid);
332 if (lp2) {
333 *lpp = lp2;
334 goto out;
335 }
336
337 lp->lp_ni = lnet_net2ni_locked(LNET_NIDNET(nid), cpt2);
338 if (!lp->lp_ni) {
339 rc = -EHOSTUNREACH;
340 goto out;
341 }
342
343 lp->lp_txcredits = lp->lp_ni->ni_peertxcredits;
344 lp->lp_mintxcredits = lp->lp_ni->ni_peertxcredits;
345 lp->lp_rtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
346 lp->lp_minrtrcredits = lnet_peer_buffer_credits(lp->lp_ni);
347
348 list_add_tail(&lp->lp_hashlist,
349 &ptable->pt_hash[lnet_nid2peerhash(nid)]);
350 ptable->pt_version++;
351 *lpp = lp;
352
353 return 0;
354 out:
355 if (lp)
356 list_add(&lp->lp_hashlist, &ptable->pt_deathrow);
357 ptable->pt_number--;
358 return rc;
359 }
360
361 void
lnet_debug_peer(lnet_nid_t nid)362 lnet_debug_peer(lnet_nid_t nid)
363 {
364 char *aliveness = "NA";
365 lnet_peer_t *lp;
366 int rc;
367 int cpt;
368
369 cpt = lnet_cpt_of_nid(nid);
370 lnet_net_lock(cpt);
371
372 rc = lnet_nid2peer_locked(&lp, nid, cpt);
373 if (rc) {
374 lnet_net_unlock(cpt);
375 CDEBUG(D_WARNING, "No peer %s\n", libcfs_nid2str(nid));
376 return;
377 }
378
379 if (lnet_isrouter(lp) || lnet_peer_aliveness_enabled(lp))
380 aliveness = lp->lp_alive ? "up" : "down";
381
382 CDEBUG(D_WARNING, "%-24s %4d %5s %5d %5d %5d %5d %5d %ld\n",
383 libcfs_nid2str(lp->lp_nid), lp->lp_refcount,
384 aliveness, lp->lp_ni->ni_peertxcredits,
385 lp->lp_rtrcredits, lp->lp_minrtrcredits,
386 lp->lp_txcredits, lp->lp_mintxcredits, lp->lp_txqnob);
387
388 lnet_peer_decref_locked(lp);
389
390 lnet_net_unlock(cpt);
391 }
392
393 int
lnet_get_peer_info(__u32 peer_index,__u64 * nid,char aliveness[LNET_MAX_STR_LEN],__u32 * cpt_iter,__u32 * refcount,__u32 * ni_peer_tx_credits,__u32 * peer_tx_credits,__u32 * peer_rtr_credits,__u32 * peer_min_rtr_credits,__u32 * peer_tx_qnob)394 lnet_get_peer_info(__u32 peer_index, __u64 *nid,
395 char aliveness[LNET_MAX_STR_LEN],
396 __u32 *cpt_iter, __u32 *refcount,
397 __u32 *ni_peer_tx_credits, __u32 *peer_tx_credits,
398 __u32 *peer_rtr_credits, __u32 *peer_min_rtr_credits,
399 __u32 *peer_tx_qnob)
400 {
401 struct lnet_peer_table *peer_table;
402 lnet_peer_t *lp;
403 bool found = false;
404 int lncpt, j;
405
406 /* get the number of CPTs */
407 lncpt = cfs_percpt_number(the_lnet.ln_peer_tables);
408
409 /*
410 * if the cpt number to be examined is >= the number of cpts in
411 * the system then indicate that there are no more cpts to examin
412 */
413 if (*cpt_iter >= lncpt)
414 return -ENOENT;
415
416 /* get the current table */
417 peer_table = the_lnet.ln_peer_tables[*cpt_iter];
418 /* if the ptable is NULL then there are no more cpts to examine */
419 if (!peer_table)
420 return -ENOENT;
421
422 lnet_net_lock(*cpt_iter);
423
424 for (j = 0; j < LNET_PEER_HASH_SIZE && !found; j++) {
425 struct list_head *peers = &peer_table->pt_hash[j];
426
427 list_for_each_entry(lp, peers, lp_hashlist) {
428 if (peer_index-- > 0)
429 continue;
430
431 snprintf(aliveness, LNET_MAX_STR_LEN, "NA");
432 if (lnet_isrouter(lp) ||
433 lnet_peer_aliveness_enabled(lp))
434 snprintf(aliveness, LNET_MAX_STR_LEN,
435 lp->lp_alive ? "up" : "down");
436
437 *nid = lp->lp_nid;
438 *refcount = lp->lp_refcount;
439 *ni_peer_tx_credits = lp->lp_ni->ni_peertxcredits;
440 *peer_tx_credits = lp->lp_txcredits;
441 *peer_rtr_credits = lp->lp_rtrcredits;
442 *peer_min_rtr_credits = lp->lp_mintxcredits;
443 *peer_tx_qnob = lp->lp_txqnob;
444
445 found = true;
446 }
447 }
448 lnet_net_unlock(*cpt_iter);
449
450 *cpt_iter = lncpt;
451
452 return found ? 0 : -ENOENT;
453 }
454