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