]>
Commit | Line | Data |
---|---|---|
d7e09d03 PT |
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) 2011, 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/selftest/brw_test.c | |
37 | * | |
38 | * Author: Isaac Huang <isaac@clusterfs.com> | |
39 | */ | |
40 | ||
41 | #include "selftest.h" | |
42 | ||
43 | static int brw_srv_workitems = SFW_TEST_WI_MAX; | |
44 | CFS_MODULE_PARM(brw_srv_workitems, "i", int, 0644, "# BRW server workitems"); | |
45 | ||
46 | static int brw_inject_errors; | |
47 | CFS_MODULE_PARM(brw_inject_errors, "i", int, 0644, | |
48 | "# data errors to inject randomly, zero by default"); | |
49 | ||
50 | static void | |
51 | brw_client_fini (sfw_test_instance_t *tsi) | |
52 | { | |
53 | srpc_bulk_t *bulk; | |
54 | sfw_test_unit_t *tsu; | |
55 | ||
56 | LASSERT (tsi->tsi_is_client); | |
57 | ||
58 | list_for_each_entry (tsu, &tsi->tsi_units, tsu_list) { | |
59 | bulk = tsu->tsu_private; | |
60 | if (bulk == NULL) continue; | |
61 | ||
62 | srpc_free_bulk(bulk); | |
63 | tsu->tsu_private = NULL; | |
64 | } | |
65 | } | |
66 | ||
67 | int | |
68 | brw_client_init (sfw_test_instance_t *tsi) | |
69 | { | |
70 | sfw_session_t *sn = tsi->tsi_batch->bat_session; | |
71 | int flags; | |
72 | int npg; | |
73 | int len; | |
74 | int opc; | |
75 | srpc_bulk_t *bulk; | |
76 | sfw_test_unit_t *tsu; | |
77 | ||
78 | LASSERT(sn != NULL); | |
79 | LASSERT(tsi->tsi_is_client); | |
80 | ||
81 | if ((sn->sn_features & LST_FEAT_BULK_LEN) == 0) { | |
82 | test_bulk_req_t *breq = &tsi->tsi_u.bulk_v0; | |
83 | ||
84 | opc = breq->blk_opc; | |
85 | flags = breq->blk_flags; | |
86 | npg = breq->blk_npg; | |
87 | /* NB: this is not going to work for variable page size, | |
88 | * but we have to keep it for compatibility */ | |
89 | len = npg * PAGE_CACHE_SIZE; | |
90 | ||
91 | } else { | |
92 | test_bulk_req_v1_t *breq = &tsi->tsi_u.bulk_v1; | |
93 | ||
94 | /* I should never get this step if it's unknown feature | |
95 | * because make_session will reject unknown feature */ | |
96 | LASSERT((sn->sn_features & ~LST_FEATS_MASK) == 0); | |
97 | ||
98 | opc = breq->blk_opc; | |
99 | flags = breq->blk_flags; | |
100 | len = breq->blk_len; | |
101 | npg = (len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
102 | } | |
103 | ||
104 | if (npg > LNET_MAX_IOV || npg <= 0) | |
105 | return -EINVAL; | |
106 | ||
107 | if (opc != LST_BRW_READ && opc != LST_BRW_WRITE) | |
108 | return -EINVAL; | |
109 | ||
110 | if (flags != LST_BRW_CHECK_NONE && | |
111 | flags != LST_BRW_CHECK_FULL && flags != LST_BRW_CHECK_SIMPLE) | |
112 | return -EINVAL; | |
113 | ||
114 | list_for_each_entry(tsu, &tsi->tsi_units, tsu_list) { | |
115 | bulk = srpc_alloc_bulk(lnet_cpt_of_nid(tsu->tsu_dest.nid), | |
116 | npg, len, opc == LST_BRW_READ); | |
117 | if (bulk == NULL) { | |
118 | brw_client_fini(tsi); | |
119 | return -ENOMEM; | |
120 | } | |
121 | ||
122 | tsu->tsu_private = bulk; | |
123 | } | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | #define BRW_POISON 0xbeefbeefbeefbeefULL | |
129 | #define BRW_MAGIC 0xeeb0eeb1eeb2eeb3ULL | |
130 | #define BRW_MSIZE sizeof(__u64) | |
131 | ||
132 | int | |
133 | brw_inject_one_error (void) | |
134 | { | |
135 | struct timeval tv; | |
136 | ||
137 | if (brw_inject_errors <= 0) return 0; | |
138 | ||
139 | do_gettimeofday(&tv); | |
140 | ||
141 | if ((tv.tv_usec & 1) == 0) return 0; | |
142 | ||
143 | return brw_inject_errors--; | |
144 | } | |
145 | ||
146 | void | |
147 | brw_fill_page (struct page *pg, int pattern, __u64 magic) | |
148 | { | |
149 | char *addr = page_address(pg); | |
150 | int i; | |
151 | ||
152 | LASSERT (addr != NULL); | |
153 | ||
154 | if (pattern == LST_BRW_CHECK_NONE) return; | |
155 | ||
156 | if (magic == BRW_MAGIC) | |
157 | magic += brw_inject_one_error(); | |
158 | ||
159 | if (pattern == LST_BRW_CHECK_SIMPLE) { | |
160 | memcpy(addr, &magic, BRW_MSIZE); | |
161 | addr += PAGE_CACHE_SIZE - BRW_MSIZE; | |
162 | memcpy(addr, &magic, BRW_MSIZE); | |
163 | return; | |
164 | } | |
165 | ||
166 | if (pattern == LST_BRW_CHECK_FULL) { | |
167 | for (i = 0; i < PAGE_CACHE_SIZE / BRW_MSIZE; i++) | |
168 | memcpy(addr + i * BRW_MSIZE, &magic, BRW_MSIZE); | |
169 | return; | |
170 | } | |
171 | ||
172 | LBUG (); | |
173 | return; | |
174 | } | |
175 | ||
176 | int | |
177 | brw_check_page (struct page *pg, int pattern, __u64 magic) | |
178 | { | |
179 | char *addr = page_address(pg); | |
180 | __u64 data = 0; /* make compiler happy */ | |
181 | int i; | |
182 | ||
183 | LASSERT (addr != NULL); | |
184 | ||
185 | if (pattern == LST_BRW_CHECK_NONE) | |
186 | return 0; | |
187 | ||
188 | if (pattern == LST_BRW_CHECK_SIMPLE) { | |
189 | data = *((__u64 *) addr); | |
190 | if (data != magic) goto bad_data; | |
191 | ||
192 | addr += PAGE_CACHE_SIZE - BRW_MSIZE; | |
193 | data = *((__u64 *) addr); | |
194 | if (data != magic) goto bad_data; | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
199 | if (pattern == LST_BRW_CHECK_FULL) { | |
200 | for (i = 0; i < PAGE_CACHE_SIZE / BRW_MSIZE; i++) { | |
201 | data = *(((__u64 *) addr) + i); | |
202 | if (data != magic) goto bad_data; | |
203 | } | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | LBUG (); | |
209 | ||
210 | bad_data: | |
211 | CERROR ("Bad data in page %p: "LPX64", "LPX64" expected\n", | |
212 | pg, data, magic); | |
213 | return 1; | |
214 | } | |
215 | ||
216 | void | |
217 | brw_fill_bulk (srpc_bulk_t *bk, int pattern, __u64 magic) | |
218 | { | |
219 | int i; | |
220 | struct page *pg; | |
221 | ||
222 | for (i = 0; i < bk->bk_niov; i++) { | |
223 | pg = bk->bk_iovs[i].kiov_page; | |
224 | brw_fill_page(pg, pattern, magic); | |
225 | } | |
226 | } | |
227 | ||
228 | int | |
229 | brw_check_bulk (srpc_bulk_t *bk, int pattern, __u64 magic) | |
230 | { | |
231 | int i; | |
232 | struct page *pg; | |
233 | ||
234 | for (i = 0; i < bk->bk_niov; i++) { | |
235 | pg = bk->bk_iovs[i].kiov_page; | |
236 | if (brw_check_page(pg, pattern, magic) != 0) { | |
237 | CERROR ("Bulk page %p (%d/%d) is corrupted!\n", | |
238 | pg, i, bk->bk_niov); | |
239 | return 1; | |
240 | } | |
241 | } | |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
246 | static int | |
247 | brw_client_prep_rpc (sfw_test_unit_t *tsu, | |
248 | lnet_process_id_t dest, srpc_client_rpc_t **rpcpp) | |
249 | { | |
250 | srpc_bulk_t *bulk = tsu->tsu_private; | |
251 | sfw_test_instance_t *tsi = tsu->tsu_instance; | |
252 | sfw_session_t *sn = tsi->tsi_batch->bat_session; | |
253 | srpc_client_rpc_t *rpc; | |
254 | srpc_brw_reqst_t *req; | |
255 | int flags; | |
256 | int npg; | |
257 | int len; | |
258 | int opc; | |
259 | int rc; | |
260 | ||
261 | LASSERT(sn != NULL); | |
262 | LASSERT(bulk != NULL); | |
263 | ||
264 | if ((sn->sn_features & LST_FEAT_BULK_LEN) == 0) { | |
265 | test_bulk_req_t *breq = &tsi->tsi_u.bulk_v0; | |
266 | ||
267 | opc = breq->blk_opc; | |
268 | flags = breq->blk_flags; | |
269 | npg = breq->blk_npg; | |
270 | len = npg * PAGE_CACHE_SIZE; | |
271 | ||
272 | } else { | |
273 | test_bulk_req_v1_t *breq = &tsi->tsi_u.bulk_v1; | |
274 | ||
275 | /* I should never get this step if it's unknown feature | |
276 | * because make_session will reject unknown feature */ | |
277 | LASSERT((sn->sn_features & ~LST_FEATS_MASK) == 0); | |
278 | ||
279 | opc = breq->blk_opc; | |
280 | flags = breq->blk_flags; | |
281 | len = breq->blk_len; | |
282 | npg = (len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
283 | } | |
284 | ||
285 | rc = sfw_create_test_rpc(tsu, dest, sn->sn_features, npg, len, &rpc); | |
286 | if (rc != 0) | |
287 | return rc; | |
288 | ||
289 | memcpy(&rpc->crpc_bulk, bulk, offsetof(srpc_bulk_t, bk_iovs[npg])); | |
290 | if (opc == LST_BRW_WRITE) | |
291 | brw_fill_bulk(&rpc->crpc_bulk, flags, BRW_MAGIC); | |
292 | else | |
293 | brw_fill_bulk(&rpc->crpc_bulk, flags, BRW_POISON); | |
294 | ||
295 | req = &rpc->crpc_reqstmsg.msg_body.brw_reqst; | |
296 | req->brw_flags = flags; | |
297 | req->brw_rw = opc; | |
298 | req->brw_len = len; | |
299 | ||
300 | *rpcpp = rpc; | |
301 | return 0; | |
302 | } | |
303 | ||
304 | static void | |
305 | brw_client_done_rpc (sfw_test_unit_t *tsu, srpc_client_rpc_t *rpc) | |
306 | { | |
307 | __u64 magic = BRW_MAGIC; | |
308 | sfw_test_instance_t *tsi = tsu->tsu_instance; | |
309 | sfw_session_t *sn = tsi->tsi_batch->bat_session; | |
310 | srpc_msg_t *msg = &rpc->crpc_replymsg; | |
311 | srpc_brw_reply_t *reply = &msg->msg_body.brw_reply; | |
312 | srpc_brw_reqst_t *reqst = &rpc->crpc_reqstmsg.msg_body.brw_reqst; | |
313 | ||
314 | LASSERT (sn != NULL); | |
315 | ||
316 | if (rpc->crpc_status != 0) { | |
317 | CERROR ("BRW RPC to %s failed with %d\n", | |
318 | libcfs_id2str(rpc->crpc_dest), rpc->crpc_status); | |
319 | if (!tsi->tsi_stopping) /* rpc could have been aborted */ | |
320 | atomic_inc(&sn->sn_brw_errors); | |
321 | goto out; | |
322 | } | |
323 | ||
324 | if (msg->msg_magic != SRPC_MSG_MAGIC) { | |
325 | __swab64s(&magic); | |
326 | __swab32s(&reply->brw_status); | |
327 | } | |
328 | ||
329 | CDEBUG (reply->brw_status ? D_WARNING : D_NET, | |
330 | "BRW RPC to %s finished with brw_status: %d\n", | |
331 | libcfs_id2str(rpc->crpc_dest), reply->brw_status); | |
332 | ||
333 | if (reply->brw_status != 0) { | |
334 | atomic_inc(&sn->sn_brw_errors); | |
335 | rpc->crpc_status = -(int)reply->brw_status; | |
336 | goto out; | |
337 | } | |
338 | ||
339 | if (reqst->brw_rw == LST_BRW_WRITE) goto out; | |
340 | ||
341 | if (brw_check_bulk(&rpc->crpc_bulk, reqst->brw_flags, magic) != 0) { | |
342 | CERROR ("Bulk data from %s is corrupted!\n", | |
343 | libcfs_id2str(rpc->crpc_dest)); | |
344 | atomic_inc(&sn->sn_brw_errors); | |
345 | rpc->crpc_status = -EBADMSG; | |
346 | } | |
347 | ||
348 | out: | |
349 | return; | |
350 | } | |
351 | ||
352 | void | |
353 | brw_server_rpc_done (srpc_server_rpc_t *rpc) | |
354 | { | |
355 | srpc_bulk_t *blk = rpc->srpc_bulk; | |
356 | ||
357 | if (blk == NULL) return; | |
358 | ||
359 | if (rpc->srpc_status != 0) | |
360 | CERROR ("Bulk transfer %s %s has failed: %d\n", | |
361 | blk->bk_sink ? "from" : "to", | |
362 | libcfs_id2str(rpc->srpc_peer), rpc->srpc_status); | |
363 | else | |
364 | CDEBUG (D_NET, "Transfered %d pages bulk data %s %s\n", | |
365 | blk->bk_niov, blk->bk_sink ? "from" : "to", | |
366 | libcfs_id2str(rpc->srpc_peer)); | |
367 | ||
368 | sfw_free_pages(rpc); | |
369 | } | |
370 | ||
371 | int | |
372 | brw_bulk_ready (srpc_server_rpc_t *rpc, int status) | |
373 | { | |
374 | __u64 magic = BRW_MAGIC; | |
375 | srpc_brw_reply_t *reply = &rpc->srpc_replymsg.msg_body.brw_reply; | |
376 | srpc_brw_reqst_t *reqst; | |
377 | srpc_msg_t *reqstmsg; | |
378 | ||
379 | LASSERT (rpc->srpc_bulk != NULL); | |
380 | LASSERT (rpc->srpc_reqstbuf != NULL); | |
381 | ||
382 | reqstmsg = &rpc->srpc_reqstbuf->buf_msg; | |
383 | reqst = &reqstmsg->msg_body.brw_reqst; | |
384 | ||
385 | if (status != 0) { | |
386 | CERROR ("BRW bulk %s failed for RPC from %s: %d\n", | |
387 | reqst->brw_rw == LST_BRW_READ ? "READ" : "WRITE", | |
388 | libcfs_id2str(rpc->srpc_peer), status); | |
389 | return -EIO; | |
390 | } | |
391 | ||
392 | if (reqst->brw_rw == LST_BRW_READ) | |
393 | return 0; | |
394 | ||
395 | if (reqstmsg->msg_magic != SRPC_MSG_MAGIC) | |
396 | __swab64s(&magic); | |
397 | ||
398 | if (brw_check_bulk(rpc->srpc_bulk, reqst->brw_flags, magic) != 0) { | |
399 | CERROR ("Bulk data from %s is corrupted!\n", | |
400 | libcfs_id2str(rpc->srpc_peer)); | |
401 | reply->brw_status = EBADMSG; | |
402 | } | |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
407 | int | |
408 | brw_server_handle(struct srpc_server_rpc *rpc) | |
409 | { | |
410 | struct srpc_service *sv = rpc->srpc_scd->scd_svc; | |
411 | srpc_msg_t *replymsg = &rpc->srpc_replymsg; | |
412 | srpc_msg_t *reqstmsg = &rpc->srpc_reqstbuf->buf_msg; | |
413 | srpc_brw_reply_t *reply = &replymsg->msg_body.brw_reply; | |
414 | srpc_brw_reqst_t *reqst = &reqstmsg->msg_body.brw_reqst; | |
415 | int npg; | |
416 | int rc; | |
417 | ||
418 | LASSERT (sv->sv_id == SRPC_SERVICE_BRW); | |
419 | ||
420 | if (reqstmsg->msg_magic != SRPC_MSG_MAGIC) { | |
421 | LASSERT (reqstmsg->msg_magic == __swab32(SRPC_MSG_MAGIC)); | |
422 | ||
423 | __swab32s(&reqst->brw_rw); | |
424 | __swab32s(&reqst->brw_len); | |
425 | __swab32s(&reqst->brw_flags); | |
426 | __swab64s(&reqst->brw_rpyid); | |
427 | __swab64s(&reqst->brw_bulkid); | |
428 | } | |
429 | LASSERT (reqstmsg->msg_type == (__u32)srpc_service2request(sv->sv_id)); | |
430 | ||
431 | reply->brw_status = 0; | |
432 | rpc->srpc_done = brw_server_rpc_done; | |
433 | ||
434 | if ((reqst->brw_rw != LST_BRW_READ && reqst->brw_rw != LST_BRW_WRITE) || | |
435 | (reqst->brw_flags != LST_BRW_CHECK_NONE && | |
436 | reqst->brw_flags != LST_BRW_CHECK_FULL && | |
437 | reqst->brw_flags != LST_BRW_CHECK_SIMPLE)) { | |
438 | reply->brw_status = EINVAL; | |
439 | return 0; | |
440 | } | |
441 | ||
442 | if ((reqstmsg->msg_ses_feats & ~LST_FEATS_MASK) != 0) { | |
443 | replymsg->msg_ses_feats = LST_FEATS_MASK; | |
444 | reply->brw_status = EPROTO; | |
445 | return 0; | |
446 | } | |
447 | ||
448 | if ((reqstmsg->msg_ses_feats & LST_FEAT_BULK_LEN) == 0) { | |
449 | /* compat with old version */ | |
450 | if ((reqst->brw_len & ~CFS_PAGE_MASK) != 0) { | |
451 | reply->brw_status = EINVAL; | |
452 | return 0; | |
453 | } | |
454 | npg = reqst->brw_len >> PAGE_CACHE_SHIFT; | |
455 | ||
456 | } else { | |
457 | npg = (reqst->brw_len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
458 | } | |
459 | ||
460 | replymsg->msg_ses_feats = reqstmsg->msg_ses_feats; | |
461 | ||
462 | if (reqst->brw_len == 0 || npg > LNET_MAX_IOV) { | |
463 | reply->brw_status = EINVAL; | |
464 | return 0; | |
465 | } | |
466 | ||
467 | rc = sfw_alloc_pages(rpc, rpc->srpc_scd->scd_cpt, npg, | |
468 | reqst->brw_len, | |
469 | reqst->brw_rw == LST_BRW_WRITE); | |
470 | if (rc != 0) | |
471 | return rc; | |
472 | ||
473 | if (reqst->brw_rw == LST_BRW_READ) | |
474 | brw_fill_bulk(rpc->srpc_bulk, reqst->brw_flags, BRW_MAGIC); | |
475 | else | |
476 | brw_fill_bulk(rpc->srpc_bulk, reqst->brw_flags, BRW_POISON); | |
477 | ||
478 | return 0; | |
479 | } | |
480 | ||
481 | sfw_test_client_ops_t brw_test_client; | |
482 | void brw_init_test_client(void) | |
483 | { | |
484 | brw_test_client.tso_init = brw_client_init; | |
485 | brw_test_client.tso_fini = brw_client_fini; | |
486 | brw_test_client.tso_prep_rpc = brw_client_prep_rpc; | |
487 | brw_test_client.tso_done_rpc = brw_client_done_rpc; | |
488 | }; | |
489 | ||
490 | srpc_service_t brw_test_service; | |
491 | void brw_init_test_service(void) | |
492 | { | |
493 | ||
494 | brw_test_service.sv_id = SRPC_SERVICE_BRW; | |
495 | brw_test_service.sv_name = "brw_test"; | |
496 | brw_test_service.sv_handler = brw_server_handle; | |
497 | brw_test_service.sv_bulk_ready = brw_bulk_ready; | |
498 | brw_test_service.sv_wi_total = brw_srv_workitems; | |
499 | } |