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