xref: /linux/lib/reed_solomon/test_rslib.c (revision 0af298e62e5cff4ebf9bec47b8711177a7e2f115)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Tests for Generic Reed Solomon encoder / decoder library
4  *
5  * Written by Ferdinand Blomqvist
6  * Based on previous work by Phil Karn, KA9Q
7  */
8 #include <linux/rslib.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/random.h>
13 #include <linux/slab.h>
14 
15 enum verbosity {
16 	V_SILENT,
17 	V_PROGRESS,
18 	V_CSUMMARY
19 };
20 
21 enum method {
22 	CORR_BUFFER,
23 	CALLER_SYNDROME,
24 	IN_PLACE
25 };
26 
27 #define __param(type, name, init, msg)		\
28 	static type name = init;		\
29 	module_param(name, type, 0444);		\
30 	MODULE_PARM_DESC(name, msg)
31 
32 __param(int, v, V_PROGRESS, "Verbosity level");
33 __param(int, ewsc, 1, "Erasures without symbol corruption");
34 __param(int, bc, 1, "Test for correct behaviour beyond error correction capacity");
35 
36 struct etab {
37 	int	symsize;
38 	int	genpoly;
39 	int	fcs;
40 	int	prim;
41 	int	nroots;
42 	int	ntrials;
43 };
44 
45 /* List of codes to test */
46 static struct etab Tab[] = {
47 	{2,	0x7,	1,	1,	1,	100000	},
48 	{3,	0xb,	1,	1,	2,	100000	},
49 	{3,	0xb,	1,	1,	3,	100000	},
50 	{3,	0xb,	2,	1,	4,	100000	},
51 	{4,	0x13,	1,	1,	4,	10000	},
52 	{5,	0x25,	1,	1,	6,	1000	},
53 	{6,	0x43,	3,	1,	8,	1000	},
54 	{7,	0x89,	1,	1,	14,	500	},
55 	{8,	0x11d,	1,	1,	30,	100	},
56 	{8,	0x187,	112,	11,	32,	100	},
57 	{9,	0x211,	1,	1,	33,	80	},
58 	{0, 0, 0, 0, 0, 0},
59 };
60 
61 
62 struct estat {
63 	int	dwrong;
64 	int	irv;
65 	int	wepos;
66 	int	nwords;
67 };
68 
69 struct bcstat {
70 	int	rfail;
71 	int	rsuccess;
72 	int	noncw;
73 	int	nwords;
74 };
75 
76 struct wspace {
77 	uint16_t	*c;		/* sent codeword */
78 	uint16_t	*r;		/* received word */
79 	uint16_t	*s;		/* syndrome */
80 	uint16_t	*corr;		/* correction buffer */
81 	int		*errlocs;
82 	int		*derrlocs;
83 };
84 
85 struct pad {
86 	int	mult;
87 	int	shift;
88 };
89 
90 static struct pad pad_coef[] = {
91 	{ 0, 0 },
92 	{ 1, 2 },
93 	{ 1, 1 },
94 	{ 3, 2 },
95 	{ 1, 0 },
96 };
97 
98 static void free_ws(struct wspace *ws)
99 {
100 	if (!ws)
101 		return;
102 
103 	kfree(ws->errlocs);
104 	kfree(ws->c);
105 	kfree(ws);
106 }
107 
108 static struct wspace *alloc_ws(struct rs_codec *rs)
109 {
110 	int nroots = rs->nroots;
111 	struct wspace *ws;
112 	int nn = rs->nn;
113 
114 	ws = kzalloc_obj(*ws);
115 	if (!ws)
116 		return NULL;
117 
118 	ws->c = kmalloc_objs(uint16_t, 2 * (nn + nroots));
119 	if (!ws->c)
120 		goto err;
121 
122 	ws->r = ws->c + nn;
123 	ws->s = ws->r + nn;
124 	ws->corr = ws->s + nroots;
125 
126 	ws->errlocs = kmalloc_objs(int, nn + nroots);
127 	if (!ws->errlocs)
128 		goto err;
129 
130 	ws->derrlocs = ws->errlocs + nn;
131 	return ws;
132 
133 err:
134 	free_ws(ws);
135 	return NULL;
136 }
137 
138 
139 /*
140  * Generates a random codeword and stores it in c. Generates random errors and
141  * erasures, and stores the random word with errors in r. Erasure positions are
142  * stored in derrlocs, while errlocs has one of three values in every position:
143  *
144  * 0 if there is no error in this position;
145  * 1 if there is a symbol error in this position;
146  * 2 if there is an erasure without symbol corruption.
147  *
148  * Returns the number of corrupted symbols.
149  */
150 static int get_rcw_we(struct rs_control *rs, struct wspace *ws,
151 			int len, int errs, int eras)
152 {
153 	int nroots = rs->codec->nroots;
154 	int *derrlocs = ws->derrlocs;
155 	int *errlocs = ws->errlocs;
156 	int dlen = len - nroots;
157 	int nn = rs->codec->nn;
158 	uint16_t *c = ws->c;
159 	uint16_t *r = ws->r;
160 	int errval;
161 	int errloc;
162 	int i;
163 
164 	/* Load c with random data and encode */
165 	for (i = 0; i < dlen; i++)
166 		c[i] = get_random_u32() & nn;
167 
168 	memset(c + dlen, 0, nroots * sizeof(*c));
169 	encode_rs16(rs, c, dlen, c + dlen, 0);
170 
171 	/* Make copyand add errors and erasures */
172 	memcpy(r, c, len * sizeof(*r));
173 	memset(errlocs, 0, len * sizeof(*errlocs));
174 	memset(derrlocs, 0, nroots * sizeof(*derrlocs));
175 
176 	/* Generating random errors */
177 	for (i = 0; i < errs; i++) {
178 		do {
179 			/* Error value must be nonzero */
180 			errval = get_random_u32() & nn;
181 		} while (errval == 0);
182 
183 		do {
184 			/* Must not choose the same location twice */
185 			errloc = get_random_u32_below(len);
186 		} while (errlocs[errloc] != 0);
187 
188 		errlocs[errloc] = 1;
189 		r[errloc] ^= errval;
190 	}
191 
192 	/* Generating random erasures */
193 	for (i = 0; i < eras; i++) {
194 		do {
195 			/* Must not choose the same location twice */
196 			errloc = get_random_u32_below(len);
197 		} while (errlocs[errloc] != 0);
198 
199 		derrlocs[i] = errloc;
200 
201 		if (ewsc && get_random_u32_below(2)) {
202 			/* Erasure with the symbol intact */
203 			errlocs[errloc] = 2;
204 		} else {
205 			/* Erasure with corrupted symbol */
206 			do {
207 				/* Error value must be nonzero */
208 				errval = get_random_u32() & nn;
209 			} while (errval == 0);
210 
211 			errlocs[errloc] = 1;
212 			r[errloc] ^= errval;
213 			errs++;
214 		}
215 	}
216 
217 	return errs;
218 }
219 
220 static void fix_err(uint16_t *data, int nerrs, uint16_t *corr, int *errlocs)
221 {
222 	int i;
223 
224 	for (i = 0; i < nerrs; i++)
225 		data[errlocs[i]] ^= corr[i];
226 }
227 
228 static void compute_syndrome(struct rs_control *rsc, uint16_t *data,
229 				int len, uint16_t *syn)
230 {
231 	struct rs_codec *rs = rsc->codec;
232 	uint16_t *alpha_to = rs->alpha_to;
233 	uint16_t *index_of = rs->index_of;
234 	int nroots = rs->nroots;
235 	int prim = rs->prim;
236 	int fcr = rs->fcr;
237 	int i, j;
238 
239 	/* Calculating syndrome */
240 	for (i = 0; i < nroots; i++) {
241 		syn[i] = data[0];
242 		for (j = 1; j < len; j++) {
243 			if (syn[i] == 0) {
244 				syn[i] = data[j];
245 			} else {
246 				syn[i] = data[j] ^
247 					alpha_to[rs_modnn(rs, index_of[syn[i]]
248 						+ (fcr + i) * prim)];
249 			}
250 		}
251 	}
252 
253 	/* Convert to index form */
254 	for (i = 0; i < nroots; i++)
255 		syn[i] = rs->index_of[syn[i]];
256 }
257 
258 /* Test up to error correction capacity */
259 static void test_uc(struct rs_control *rs, int len, int errs,
260 		int eras, int trials, struct estat *stat,
261 		struct wspace *ws, int method)
262 {
263 	int dlen = len - rs->codec->nroots;
264 	int *derrlocs = ws->derrlocs;
265 	int *errlocs = ws->errlocs;
266 	uint16_t *corr = ws->corr;
267 	uint16_t *c = ws->c;
268 	uint16_t *r = ws->r;
269 	uint16_t *s = ws->s;
270 	int derrs, nerrs;
271 	int i, j;
272 
273 	for (j = 0; j < trials; j++) {
274 		nerrs = get_rcw_we(rs, ws, len, errs, eras);
275 
276 		switch (method) {
277 		case CORR_BUFFER:
278 			derrs = decode_rs16(rs, r, r + dlen, dlen,
279 					NULL, eras, derrlocs, 0, corr);
280 			fix_err(r, derrs, corr, derrlocs);
281 			break;
282 		case CALLER_SYNDROME:
283 			compute_syndrome(rs, r, len, s);
284 			derrs = decode_rs16(rs, NULL, NULL, dlen,
285 					s, eras, derrlocs, 0, corr);
286 			fix_err(r, derrs, corr, derrlocs);
287 			break;
288 		case IN_PLACE:
289 			derrs = decode_rs16(rs, r, r + dlen, dlen,
290 					NULL, eras, derrlocs, 0, NULL);
291 			break;
292 		default:
293 			continue;
294 		}
295 
296 		if (derrs != nerrs)
297 			stat->irv++;
298 
299 		if (method != IN_PLACE) {
300 			for (i = 0; i < derrs; i++) {
301 				if (errlocs[derrlocs[i]] != 1)
302 					stat->wepos++;
303 			}
304 		}
305 
306 		if (memcmp(r, c, len * sizeof(*r)))
307 			stat->dwrong++;
308 	}
309 	stat->nwords += trials;
310 }
311 
312 static int ex_rs_helper(struct rs_control *rs, struct wspace *ws,
313 			int len, int trials, int method)
314 {
315 	static const char * const desc[] = {
316 		"Testing correction buffer interface...",
317 		"Testing with caller provided syndrome...",
318 		"Testing in-place interface..."
319 	};
320 
321 	struct estat stat = {0, 0, 0, 0};
322 	int nroots = rs->codec->nroots;
323 	int errs, eras, retval;
324 
325 	if (v >= V_PROGRESS)
326 		pr_info("  %s\n", desc[method]);
327 
328 	for (errs = 0; errs <= nroots / 2; errs++)
329 		for (eras = 0; eras <= nroots - 2 * errs; eras++)
330 			test_uc(rs, len, errs, eras, trials, &stat, ws, method);
331 
332 	if (v >= V_CSUMMARY) {
333 		pr_info("    Decodes wrong:        %d / %d\n",
334 				stat.dwrong, stat.nwords);
335 		pr_info("    Wrong return value:   %d / %d\n",
336 				stat.irv, stat.nwords);
337 		if (method != IN_PLACE)
338 			pr_info("    Wrong error position: %d\n", stat.wepos);
339 	}
340 
341 	retval = stat.dwrong + stat.wepos + stat.irv;
342 	if (retval && v >= V_PROGRESS)
343 		pr_warn("    FAIL: %d decoding failures!\n", retval);
344 
345 	return retval;
346 }
347 
348 static int exercise_rs(struct rs_control *rs, struct wspace *ws,
349 		       int len, int trials)
350 {
351 
352 	int retval = 0;
353 	int i;
354 
355 	if (v >= V_PROGRESS)
356 		pr_info("Testing up to error correction capacity...\n");
357 
358 	for (i = 0; i <= IN_PLACE; i++)
359 		retval |= ex_rs_helper(rs, ws, len, trials, i);
360 
361 	return retval;
362 }
363 
364 /* Tests for correct behaviour beyond error correction capacity */
365 static void test_bc(struct rs_control *rs, int len, int errs,
366 		int eras, int trials, struct bcstat *stat,
367 		struct wspace *ws)
368 {
369 	int nroots = rs->codec->nroots;
370 	int dlen = len - nroots;
371 	int *derrlocs = ws->derrlocs;
372 	uint16_t *corr = ws->corr;
373 	uint16_t *r = ws->r;
374 	int derrs, j;
375 
376 	for (j = 0; j < trials; j++) {
377 		get_rcw_we(rs, ws, len, errs, eras);
378 		derrs = decode_rs16(rs, r, r + dlen, dlen,
379 				NULL, eras, derrlocs, 0, corr);
380 		fix_err(r, derrs, corr, derrlocs);
381 
382 		if (derrs >= 0) {
383 			stat->rsuccess++;
384 
385 			/*
386 			 * We check that the returned word is actually a
387 			 * codeword. The obvious way to do this would be to
388 			 * compute the syndrome, but we don't want to replicate
389 			 * that code here. However, all the codes are in
390 			 * systematic form, and therefore we can encode the
391 			 * returned word, and see whether the parity changes or
392 			 * not.
393 			 */
394 			memset(corr, 0, nroots * sizeof(*corr));
395 			encode_rs16(rs, r, dlen, corr, 0);
396 
397 			if (memcmp(r + dlen, corr, nroots * sizeof(*corr)))
398 				stat->noncw++;
399 		} else {
400 			stat->rfail++;
401 		}
402 	}
403 	stat->nwords += trials;
404 }
405 
406 static int exercise_rs_bc(struct rs_control *rs, struct wspace *ws,
407 			  int len, int trials)
408 {
409 	struct bcstat stat = {0, 0, 0, 0};
410 	int nroots = rs->codec->nroots;
411 	int errs, eras, cutoff;
412 
413 	if (v >= V_PROGRESS)
414 		pr_info("Testing beyond error correction capacity...\n");
415 
416 	for (errs = 1; errs <= nroots; errs++) {
417 		eras = nroots - 2 * errs + 1;
418 		if (eras < 0)
419 			eras = 0;
420 
421 		cutoff = nroots <= len - errs ? nroots : len - errs;
422 		for (; eras <= cutoff; eras++)
423 			test_bc(rs, len, errs, eras, trials, &stat, ws);
424 	}
425 
426 	if (v >= V_CSUMMARY) {
427 		pr_info("  decoder gives up:        %d / %d\n",
428 				stat.rfail, stat.nwords);
429 		pr_info("  decoder returns success: %d / %d\n",
430 				stat.rsuccess, stat.nwords);
431 		pr_info("    not a codeword:        %d / %d\n",
432 				stat.noncw, stat.rsuccess);
433 	}
434 
435 	if (stat.noncw && v >= V_PROGRESS)
436 		pr_warn("    FAIL: %d silent failures!\n", stat.noncw);
437 
438 	return stat.noncw;
439 }
440 
441 static int run_exercise(struct etab *e)
442 {
443 	int nn = (1 << e->symsize) - 1;
444 	int kk = nn - e->nroots;
445 	struct rs_control *rsc;
446 	int retval = -ENOMEM;
447 	int max_pad = kk - 1;
448 	int prev_pad = -1;
449 	struct wspace *ws;
450 	int i;
451 
452 	rsc = init_rs(e->symsize, e->genpoly, e->fcs, e->prim, e->nroots);
453 	if (!rsc)
454 		return retval;
455 
456 	ws = alloc_ws(rsc->codec);
457 	if (!ws)
458 		goto err;
459 
460 	retval = 0;
461 	for (i = 0; i < ARRAY_SIZE(pad_coef); i++) {
462 		int pad = (pad_coef[i].mult * max_pad) >> pad_coef[i].shift;
463 		int len = nn - pad;
464 
465 		if (pad == prev_pad)
466 			continue;
467 
468 		prev_pad = pad;
469 		if (v >= V_PROGRESS) {
470 			pr_info("Testing (%d,%d)_%d code...\n",
471 					len, kk - pad, nn + 1);
472 		}
473 
474 		retval |= exercise_rs(rsc, ws, len, e->ntrials);
475 		if (bc)
476 			retval |= exercise_rs_bc(rsc, ws, len, e->ntrials);
477 	}
478 
479 	free_ws(ws);
480 
481 err:
482 	free_rs(rsc);
483 	return retval;
484 }
485 
486 static int __init test_rslib_init(void)
487 {
488 	int i, fail = 0;
489 
490 	for (i = 0; Tab[i].symsize != 0 ; i++) {
491 		int retval;
492 
493 		retval = run_exercise(Tab + i);
494 		if (retval < 0)
495 			return -ENOMEM;
496 
497 		fail |= retval;
498 	}
499 
500 	if (fail)
501 		pr_warn("rslib: test failed\n");
502 	else
503 		pr_info("rslib: test ok\n");
504 
505 	return -EAGAIN; /* Fail will directly unload the module */
506 }
507 
508 static void __exit test_rslib_exit(void)
509 {
510 }
511 
512 module_init(test_rslib_init)
513 module_exit(test_rslib_exit)
514 
515 MODULE_LICENSE("GPL");
516 MODULE_AUTHOR("Ferdinand Blomqvist");
517 MODULE_DESCRIPTION("Reed-Solomon library test");
518