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