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