xref: /openbmc/linux/lib/checksum_kunit.c (revision b38460bc)
1688eb819SNoah Goldstein // SPDX-License-Identifier: GPL-2.0+
2688eb819SNoah Goldstein /*
3688eb819SNoah Goldstein  * Test cases csum_partial and csum_fold
4688eb819SNoah Goldstein  */
5688eb819SNoah Goldstein 
6688eb819SNoah Goldstein #include <kunit/test.h>
7688eb819SNoah Goldstein #include <asm/checksum.h>
8688eb819SNoah Goldstein 
9688eb819SNoah Goldstein #define MAX_LEN 512
10688eb819SNoah Goldstein #define MAX_ALIGN 64
11688eb819SNoah Goldstein #define TEST_BUFLEN (MAX_LEN + MAX_ALIGN)
12688eb819SNoah Goldstein 
13*b38460bcSChristophe Leroy /* Values for a little endian CPU. Byte swap each half on big endian CPU. */
14*b38460bcSChristophe Leroy static const u32 random_init_sum = 0x2847aab;
15688eb819SNoah Goldstein static const u8 random_buf[] = {
16688eb819SNoah Goldstein 	0xac, 0xd7, 0x76, 0x69, 0x6e, 0xf2, 0x93, 0x2c, 0x1f, 0xe0, 0xde, 0x86,
17688eb819SNoah Goldstein 	0x8f, 0x54, 0x33, 0x90, 0x95, 0xbf, 0xff, 0xb9, 0xea, 0x62, 0x6e, 0xb5,
18688eb819SNoah Goldstein 	0xd3, 0x4f, 0xf5, 0x60, 0x50, 0x5c, 0xc7, 0xfa, 0x6d, 0x1a, 0xc7, 0xf0,
19688eb819SNoah Goldstein 	0xd2, 0x2c, 0x12, 0x3d, 0x88, 0xe3, 0x14, 0x21, 0xb1, 0x5e, 0x45, 0x31,
20688eb819SNoah Goldstein 	0xa2, 0x85, 0x36, 0x76, 0xba, 0xd8, 0xad, 0xbb, 0x9e, 0x49, 0x8f, 0xf7,
21688eb819SNoah Goldstein 	0xce, 0xea, 0xef, 0xca, 0x2c, 0x29, 0xf7, 0x15, 0x5c, 0x1d, 0x4d, 0x09,
22688eb819SNoah Goldstein 	0x1f, 0xe2, 0x14, 0x31, 0x8c, 0x07, 0x57, 0x23, 0x1f, 0x6f, 0x03, 0xe1,
23688eb819SNoah Goldstein 	0x93, 0x19, 0x53, 0x03, 0x45, 0x49, 0x9a, 0x3b, 0x8e, 0x0c, 0x12, 0x5d,
24688eb819SNoah Goldstein 	0x8a, 0xb8, 0x9b, 0x8c, 0x9a, 0x03, 0xe5, 0xa2, 0x43, 0xd2, 0x3b, 0x4e,
25688eb819SNoah Goldstein 	0x7e, 0x30, 0x3c, 0x22, 0x2d, 0xc5, 0xfc, 0x9e, 0xdb, 0xc6, 0xf9, 0x69,
26688eb819SNoah Goldstein 	0x12, 0x39, 0x1f, 0xa0, 0x11, 0x0c, 0x3f, 0xf5, 0x53, 0xc9, 0x30, 0xfb,
27688eb819SNoah Goldstein 	0xb0, 0xdd, 0x21, 0x1d, 0x34, 0xe2, 0x65, 0x30, 0xf1, 0xe8, 0x1b, 0xe7,
28688eb819SNoah Goldstein 	0x55, 0x0d, 0xeb, 0xbd, 0xcc, 0x9d, 0x24, 0xa4, 0xad, 0xa7, 0x93, 0x47,
29688eb819SNoah Goldstein 	0x19, 0x2e, 0xc4, 0x5c, 0x3b, 0xc7, 0x6d, 0x95, 0x0c, 0x47, 0x60, 0xaf,
30688eb819SNoah Goldstein 	0x5b, 0x47, 0xee, 0xdc, 0x31, 0x31, 0x14, 0x12, 0x7e, 0x9e, 0x45, 0xb1,
31688eb819SNoah Goldstein 	0xc1, 0x69, 0x4b, 0x84, 0xfc, 0x88, 0xc1, 0x9e, 0x46, 0xb4, 0xc2, 0x25,
32688eb819SNoah Goldstein 	0xc5, 0x6c, 0x4c, 0x22, 0x58, 0x5c, 0xbe, 0xff, 0xea, 0x88, 0x88, 0x7a,
33688eb819SNoah Goldstein 	0xcb, 0x1c, 0x5d, 0x63, 0xa1, 0xf2, 0x33, 0x0c, 0xa2, 0x16, 0x0b, 0x6e,
34688eb819SNoah Goldstein 	0x2b, 0x79, 0x58, 0xf7, 0xac, 0xd3, 0x6a, 0x3f, 0x81, 0x57, 0x48, 0x45,
35688eb819SNoah Goldstein 	0xe3, 0x7c, 0xdc, 0xd6, 0x34, 0x7e, 0xe6, 0x73, 0xfa, 0xcb, 0x31, 0x18,
36688eb819SNoah Goldstein 	0xa9, 0x0b, 0xee, 0x6b, 0x99, 0xb9, 0x2d, 0xde, 0x22, 0x0e, 0x71, 0x57,
37688eb819SNoah Goldstein 	0x0e, 0x9b, 0x11, 0xd1, 0x15, 0x41, 0xd0, 0x6b, 0x50, 0x8a, 0x23, 0x64,
38688eb819SNoah Goldstein 	0xe3, 0x9c, 0xb3, 0x55, 0x09, 0xe9, 0x32, 0x67, 0xf9, 0xe0, 0x73, 0xf1,
39688eb819SNoah Goldstein 	0x60, 0x66, 0x0b, 0x88, 0x79, 0x8d, 0x4b, 0x52, 0x83, 0x20, 0x26, 0x78,
40688eb819SNoah Goldstein 	0x49, 0x27, 0xe7, 0x3e, 0x29, 0xa8, 0x18, 0x82, 0x41, 0xdd, 0x1e, 0xcc,
41688eb819SNoah Goldstein 	0x3b, 0xc4, 0x65, 0xd1, 0x21, 0x40, 0x72, 0xb2, 0x87, 0x5e, 0x16, 0x10,
42688eb819SNoah Goldstein 	0x80, 0x3f, 0x4b, 0x58, 0x1c, 0xc2, 0x79, 0x20, 0xf0, 0xe0, 0x80, 0xd3,
43688eb819SNoah Goldstein 	0x52, 0xa5, 0x19, 0x6e, 0x47, 0x90, 0x08, 0xf5, 0x50, 0xe2, 0xd6, 0xae,
44688eb819SNoah Goldstein 	0xe9, 0x2e, 0xdc, 0xd5, 0xb4, 0x90, 0x1f, 0x79, 0x49, 0x82, 0x21, 0x84,
45688eb819SNoah Goldstein 	0xa0, 0xb5, 0x2f, 0xff, 0x30, 0x71, 0xed, 0x80, 0x68, 0xb1, 0x6d, 0xef,
46688eb819SNoah Goldstein 	0xf6, 0xcf, 0xb8, 0x41, 0x79, 0xf5, 0x01, 0xbc, 0x0c, 0x9b, 0x0e, 0x06,
47688eb819SNoah Goldstein 	0xf3, 0xb0, 0xbb, 0x97, 0xb8, 0xb1, 0xfd, 0x51, 0x4e, 0xef, 0x0a, 0x3d,
48688eb819SNoah Goldstein 	0x7a, 0x3d, 0xbd, 0x61, 0x00, 0xa2, 0xb3, 0xf0, 0x1d, 0x77, 0x7b, 0x6c,
49688eb819SNoah Goldstein 	0x01, 0x61, 0xa5, 0xa3, 0xdb, 0xd5, 0xd5, 0xf4, 0xb5, 0x28, 0x9f, 0x0a,
50688eb819SNoah Goldstein 	0xa3, 0x82, 0x5f, 0x4b, 0x40, 0x0f, 0x05, 0x0e, 0x78, 0xed, 0xbf, 0x17,
51688eb819SNoah Goldstein 	0xf6, 0x5a, 0x8a, 0x7d, 0xf9, 0x45, 0xc1, 0xd7, 0x1b, 0x9d, 0x6c, 0x07,
52688eb819SNoah Goldstein 	0x88, 0xf3, 0xbc, 0xf1, 0xea, 0x28, 0x1f, 0xb8, 0x7a, 0x60, 0x3c, 0xce,
53688eb819SNoah Goldstein 	0x3e, 0x50, 0xb2, 0x0b, 0xcf, 0xe5, 0x08, 0x1f, 0x48, 0x04, 0xf9, 0x35,
54688eb819SNoah Goldstein 	0x29, 0x15, 0xbe, 0x82, 0x96, 0xc2, 0x55, 0x04, 0x6c, 0x19, 0x45, 0x29,
55688eb819SNoah Goldstein 	0x0b, 0xb6, 0x49, 0x12, 0xfb, 0x8d, 0x1b, 0x75, 0x8b, 0xd9, 0x6a, 0x5c,
56688eb819SNoah Goldstein 	0xbe, 0x46, 0x2b, 0x41, 0xfe, 0x21, 0xad, 0x1f, 0x75, 0xe7, 0x90, 0x3d,
57688eb819SNoah Goldstein 	0xe1, 0xdf, 0x4b, 0xe1, 0x81, 0xe2, 0x17, 0x02, 0x7b, 0x58, 0x8b, 0x92,
58688eb819SNoah Goldstein 	0x1a, 0xac, 0x46, 0xdd, 0x2e, 0xce, 0x40, 0x09
59688eb819SNoah Goldstein };
60*b38460bcSChristophe Leroy 
61*b38460bcSChristophe Leroy /* Values for a little endian CPU. Byte swap on big endian CPU. */
62*b38460bcSChristophe Leroy static const u16 expected_results[] = {
63688eb819SNoah Goldstein 	0x82d0, 0x8224, 0xab23, 0xaaad, 0x41ad, 0x413f, 0x4f3e, 0x4eab, 0x22ab,
64688eb819SNoah Goldstein 	0x228c, 0x428b, 0x41ad, 0xbbac, 0xbb1d, 0x671d, 0x66ea, 0xd6e9, 0xd654,
65688eb819SNoah Goldstein 	0x1754, 0x1655, 0x5d54, 0x5c6a, 0xfa69, 0xf9fb, 0x44fb, 0x4428, 0xf527,
66688eb819SNoah Goldstein 	0xf432, 0x9432, 0x93e2, 0x37e2, 0x371b, 0x3d1a, 0x3cad, 0x22ad, 0x21e6,
67688eb819SNoah Goldstein 	0x31e5, 0x3113, 0x0513, 0x0501, 0xc800, 0xc778, 0xe477, 0xe463, 0xc363,
68688eb819SNoah Goldstein 	0xc2b2, 0x64b2, 0x646d, 0x336d, 0x32cb, 0xadca, 0xad94, 0x3794, 0x36da,
69688eb819SNoah Goldstein 	0x5ed9, 0x5e2c, 0xa32b, 0xa28d, 0x598d, 0x58fe, 0x61fd, 0x612f, 0x772e,
70688eb819SNoah Goldstein 	0x763f, 0xac3e, 0xac12, 0x8312, 0x821b, 0x6d1b, 0x6cbf, 0x4fbf, 0x4f72,
71688eb819SNoah Goldstein 	0x4672, 0x4653, 0x6452, 0x643e, 0x333e, 0x32b2, 0x2bb2, 0x2b5b, 0x085b,
72688eb819SNoah Goldstein 	0x083c, 0x993b, 0x9938, 0xb837, 0xb7a4, 0x9ea4, 0x9e51, 0x9b51, 0x9b0c,
73688eb819SNoah Goldstein 	0x520c, 0x5172, 0x1672, 0x15e4, 0x09e4, 0x09d2, 0xacd1, 0xac47, 0xf446,
74688eb819SNoah Goldstein 	0xf3ab, 0x67ab, 0x6711, 0x6411, 0x632c, 0xc12b, 0xc0e8, 0xeee7, 0xeeac,
75688eb819SNoah Goldstein 	0xa0ac, 0xa02e, 0x702e, 0x6ff2, 0x4df2, 0x4dc5, 0x88c4, 0x87c8, 0xe9c7,
76688eb819SNoah Goldstein 	0xe8ec, 0x22ec, 0x21f3, 0xb8f2, 0xb8e0, 0x7fe0, 0x7fc1, 0xdfc0, 0xdfaf,
77688eb819SNoah Goldstein 	0xd3af, 0xd370, 0xde6f, 0xde1c, 0x151c, 0x14ec, 0x19eb, 0x193b, 0x3c3a,
78688eb819SNoah Goldstein 	0x3c19, 0x1f19, 0x1ee5, 0x3ce4, 0x3c7f, 0x0c7f, 0x0b8e, 0x238d, 0x2372,
79688eb819SNoah Goldstein 	0x3c71, 0x3c1c, 0x2f1c, 0x2e31, 0x7130, 0x7064, 0xd363, 0xd33f, 0x2f3f,
80688eb819SNoah Goldstein 	0x2e92, 0x8791, 0x86fe, 0x3ffe, 0x3fe5, 0x11e5, 0x1121, 0xb520, 0xb4e5,
81688eb819SNoah Goldstein 	0xede4, 0xed77, 0x5877, 0x586b, 0x116b, 0x110b, 0x620a, 0x61af, 0x1aaf,
82688eb819SNoah Goldstein 	0x19c1, 0x3dc0, 0x3d8f, 0x0c8f, 0x0c7b, 0xfa7a, 0xf9fc, 0x5bfc, 0x5bb7,
83688eb819SNoah Goldstein 	0xaab6, 0xa9f5, 0x40f5, 0x40aa, 0xbca9, 0xbbad, 0x33ad, 0x32ec, 0x94eb,
84688eb819SNoah Goldstein 	0x94a5, 0xe0a4, 0xdfe2, 0xbae2, 0xba1d, 0x4e1d, 0x4dd1, 0x2bd1, 0x2b79,
85688eb819SNoah Goldstein 	0xcf78, 0xceba, 0xcfb9, 0xcecf, 0x46cf, 0x4647, 0xcc46, 0xcb7b, 0xaf7b,
86688eb819SNoah Goldstein 	0xaf1e, 0x4c1e, 0x4b7d, 0x597c, 0x5949, 0x4d49, 0x4ca7, 0x36a7, 0x369c,
87688eb819SNoah Goldstein 	0xc89b, 0xc870, 0x4f70, 0x4f18, 0x5817, 0x576b, 0x846a, 0x8400, 0x4500,
88688eb819SNoah Goldstein 	0x447f, 0xed7e, 0xed36, 0xa836, 0xa753, 0x2b53, 0x2a77, 0x5476, 0x5442,
89688eb819SNoah Goldstein 	0xd641, 0xd55b, 0x625b, 0x6161, 0x9660, 0x962f, 0x7e2f, 0x7d86, 0x7286,
90688eb819SNoah Goldstein 	0x7198, 0x0698, 0x05ff, 0x4cfe, 0x4cd1, 0x6ed0, 0x6eae, 0x60ae, 0x603d,
91688eb819SNoah Goldstein 	0x093d, 0x092f, 0x6e2e, 0x6e1d, 0x9d1c, 0x9d07, 0x5c07, 0x5b37, 0xf036,
92688eb819SNoah Goldstein 	0xefe6, 0x65e6, 0x65c3, 0x01c3, 0x00e0, 0x64df, 0x642c, 0x0f2c, 0x0f23,
93688eb819SNoah Goldstein 	0x2622, 0x25f0, 0xbeef, 0xbdf6, 0xddf5, 0xdd82, 0xec81, 0xec21, 0x8621,
94688eb819SNoah Goldstein 	0x8616, 0xfe15, 0xfd9c, 0x709c, 0x7051, 0x1e51, 0x1dce, 0xfdcd, 0xfda7,
95688eb819SNoah Goldstein 	0x85a7, 0x855e, 0x5e5e, 0x5d77, 0x1f77, 0x1f4e, 0x774d, 0x7735, 0xf534,
96688eb819SNoah Goldstein 	0xf4f3, 0x17f3, 0x17d5, 0x4bd4, 0x4b99, 0x8798, 0x8733, 0xb632, 0xb611,
97688eb819SNoah Goldstein 	0x7611, 0x759f, 0xc39e, 0xc317, 0x6517, 0x6501, 0x5501, 0x5481, 0x1581,
98688eb819SNoah Goldstein 	0x1536, 0xbd35, 0xbd19, 0xfb18, 0xfa9f, 0xda9f, 0xd9af, 0xf9ae, 0xf92e,
99688eb819SNoah Goldstein 	0x262e, 0x25dc, 0x80db, 0x80c2, 0x12c2, 0x127b, 0x827a, 0x8272, 0x8d71,
100688eb819SNoah Goldstein 	0x8d21, 0xab20, 0xaa4a, 0xfc49, 0xfb60, 0xcd60, 0xcc84, 0xf783, 0xf6cf,
101688eb819SNoah Goldstein 	0x66cf, 0x66b0, 0xedaf, 0xed66, 0x6b66, 0x6b45, 0xe744, 0xe6a4, 0x31a4,
102688eb819SNoah Goldstein 	0x3175, 0x3274, 0x3244, 0xc143, 0xc056, 0x4056, 0x3fee, 0x8eed, 0x8e80,
103688eb819SNoah Goldstein 	0x9f7f, 0x9e89, 0xcf88, 0xced0, 0x8dd0, 0x8d57, 0x9856, 0x9855, 0xdc54,
104688eb819SNoah Goldstein 	0xdc48, 0x4148, 0x413a, 0x3b3a, 0x3a47, 0x8a46, 0x898b, 0xf28a, 0xf1d2,
105688eb819SNoah Goldstein 	0x40d2, 0x3fd5, 0xeed4, 0xee86, 0xff85, 0xff7b, 0xc27b, 0xc201, 0x8501,
106688eb819SNoah Goldstein 	0x8444, 0x2344, 0x2344, 0x8143, 0x8090, 0x908f, 0x9072, 0x1972, 0x18f7,
107688eb819SNoah Goldstein 	0xacf6, 0xacf5, 0x4bf5, 0x4b50, 0xa84f, 0xa774, 0xd273, 0xd19e, 0xdd9d,
108688eb819SNoah Goldstein 	0xdce8, 0xb4e8, 0xb449, 0xaa49, 0xa9a6, 0x27a6, 0x2747, 0xdc46, 0xdc06,
109688eb819SNoah Goldstein 	0xcd06, 0xcd01, 0xbf01, 0xbe89, 0xd188, 0xd0c9, 0xb9c9, 0xb8d3, 0x5ed3,
110688eb819SNoah Goldstein 	0x5e49, 0xe148, 0xe04f, 0x9b4f, 0x9a8e, 0xc38d, 0xc372, 0x2672, 0x2606,
111688eb819SNoah Goldstein 	0x1f06, 0x1e7e, 0x2b7d, 0x2ac1, 0x39c0, 0x38d6, 0x10d6, 0x10b7, 0x58b6,
112688eb819SNoah Goldstein 	0x583c, 0xf83b, 0xf7ff, 0x29ff, 0x29c1, 0xd9c0, 0xd90e, 0xce0e, 0xcd3f,
113688eb819SNoah Goldstein 	0xe83e, 0xe836, 0xc936, 0xc8ee, 0xc4ee, 0xc3f5, 0x8ef5, 0x8ecc, 0x79cc,
114688eb819SNoah Goldstein 	0x790e, 0xf70d, 0xf677, 0x3477, 0x3422, 0x3022, 0x2fb6, 0x16b6, 0x1671,
115688eb819SNoah Goldstein 	0xed70, 0xed65, 0x3765, 0x371c, 0x251c, 0x2421, 0x9720, 0x9705, 0x2205,
116688eb819SNoah Goldstein 	0x217a, 0x4879, 0x480f, 0xec0e, 0xeb50, 0xa550, 0xa525, 0x6425, 0x6327,
117688eb819SNoah Goldstein 	0x4227, 0x417a, 0x227a, 0x2205, 0x3b04, 0x3a74, 0xfd73, 0xfc92, 0x1d92,
118688eb819SNoah Goldstein 	0x1d47, 0x3c46, 0x3bc5, 0x59c4, 0x59ad, 0x57ad, 0x5732, 0xff31, 0xfea6,
119688eb819SNoah Goldstein 	0x6ca6, 0x6c8c, 0xc08b, 0xc045, 0xe344, 0xe316, 0x1516, 0x14d6,
120688eb819SNoah Goldstein };
121*b38460bcSChristophe Leroy 
122*b38460bcSChristophe Leroy /* Values for a little endian CPU. Byte swap each half on big endian CPU. */
123*b38460bcSChristophe Leroy static const u32 init_sums_no_overflow[] = {
124688eb819SNoah Goldstein 	0xffffffff, 0xfffffffb, 0xfffffbfb, 0xfffffbf7, 0xfffff7f7, 0xfffff7f3,
125688eb819SNoah Goldstein 	0xfffff3f3, 0xfffff3ef, 0xffffefef, 0xffffefeb, 0xffffebeb, 0xffffebe7,
126688eb819SNoah Goldstein 	0xffffe7e7, 0xffffe7e3, 0xffffe3e3, 0xffffe3df, 0xffffdfdf, 0xffffdfdb,
127688eb819SNoah Goldstein 	0xffffdbdb, 0xffffdbd7, 0xffffd7d7, 0xffffd7d3, 0xffffd3d3, 0xffffd3cf,
128688eb819SNoah Goldstein 	0xffffcfcf, 0xffffcfcb, 0xffffcbcb, 0xffffcbc7, 0xffffc7c7, 0xffffc7c3,
129688eb819SNoah Goldstein 	0xffffc3c3, 0xffffc3bf, 0xffffbfbf, 0xffffbfbb, 0xffffbbbb, 0xffffbbb7,
130688eb819SNoah Goldstein 	0xffffb7b7, 0xffffb7b3, 0xffffb3b3, 0xffffb3af, 0xffffafaf, 0xffffafab,
131688eb819SNoah Goldstein 	0xffffabab, 0xffffaba7, 0xffffa7a7, 0xffffa7a3, 0xffffa3a3, 0xffffa39f,
132688eb819SNoah Goldstein 	0xffff9f9f, 0xffff9f9b, 0xffff9b9b, 0xffff9b97, 0xffff9797, 0xffff9793,
133688eb819SNoah Goldstein 	0xffff9393, 0xffff938f, 0xffff8f8f, 0xffff8f8b, 0xffff8b8b, 0xffff8b87,
134688eb819SNoah Goldstein 	0xffff8787, 0xffff8783, 0xffff8383, 0xffff837f, 0xffff7f7f, 0xffff7f7b,
135688eb819SNoah Goldstein 	0xffff7b7b, 0xffff7b77, 0xffff7777, 0xffff7773, 0xffff7373, 0xffff736f,
136688eb819SNoah Goldstein 	0xffff6f6f, 0xffff6f6b, 0xffff6b6b, 0xffff6b67, 0xffff6767, 0xffff6763,
137688eb819SNoah Goldstein 	0xffff6363, 0xffff635f, 0xffff5f5f, 0xffff5f5b, 0xffff5b5b, 0xffff5b57,
138688eb819SNoah Goldstein 	0xffff5757, 0xffff5753, 0xffff5353, 0xffff534f, 0xffff4f4f, 0xffff4f4b,
139688eb819SNoah Goldstein 	0xffff4b4b, 0xffff4b47, 0xffff4747, 0xffff4743, 0xffff4343, 0xffff433f,
140688eb819SNoah Goldstein 	0xffff3f3f, 0xffff3f3b, 0xffff3b3b, 0xffff3b37, 0xffff3737, 0xffff3733,
141688eb819SNoah Goldstein 	0xffff3333, 0xffff332f, 0xffff2f2f, 0xffff2f2b, 0xffff2b2b, 0xffff2b27,
142688eb819SNoah Goldstein 	0xffff2727, 0xffff2723, 0xffff2323, 0xffff231f, 0xffff1f1f, 0xffff1f1b,
143688eb819SNoah Goldstein 	0xffff1b1b, 0xffff1b17, 0xffff1717, 0xffff1713, 0xffff1313, 0xffff130f,
144688eb819SNoah Goldstein 	0xffff0f0f, 0xffff0f0b, 0xffff0b0b, 0xffff0b07, 0xffff0707, 0xffff0703,
145688eb819SNoah Goldstein 	0xffff0303, 0xffff02ff, 0xfffffefe, 0xfffffefa, 0xfffffafa, 0xfffffaf6,
146688eb819SNoah Goldstein 	0xfffff6f6, 0xfffff6f2, 0xfffff2f2, 0xfffff2ee, 0xffffeeee, 0xffffeeea,
147688eb819SNoah Goldstein 	0xffffeaea, 0xffffeae6, 0xffffe6e6, 0xffffe6e2, 0xffffe2e2, 0xffffe2de,
148688eb819SNoah Goldstein 	0xffffdede, 0xffffdeda, 0xffffdada, 0xffffdad6, 0xffffd6d6, 0xffffd6d2,
149688eb819SNoah Goldstein 	0xffffd2d2, 0xffffd2ce, 0xffffcece, 0xffffceca, 0xffffcaca, 0xffffcac6,
150688eb819SNoah Goldstein 	0xffffc6c6, 0xffffc6c2, 0xffffc2c2, 0xffffc2be, 0xffffbebe, 0xffffbeba,
151688eb819SNoah Goldstein 	0xffffbaba, 0xffffbab6, 0xffffb6b6, 0xffffb6b2, 0xffffb2b2, 0xffffb2ae,
152688eb819SNoah Goldstein 	0xffffaeae, 0xffffaeaa, 0xffffaaaa, 0xffffaaa6, 0xffffa6a6, 0xffffa6a2,
153688eb819SNoah Goldstein 	0xffffa2a2, 0xffffa29e, 0xffff9e9e, 0xffff9e9a, 0xffff9a9a, 0xffff9a96,
154688eb819SNoah Goldstein 	0xffff9696, 0xffff9692, 0xffff9292, 0xffff928e, 0xffff8e8e, 0xffff8e8a,
155688eb819SNoah Goldstein 	0xffff8a8a, 0xffff8a86, 0xffff8686, 0xffff8682, 0xffff8282, 0xffff827e,
156688eb819SNoah Goldstein 	0xffff7e7e, 0xffff7e7a, 0xffff7a7a, 0xffff7a76, 0xffff7676, 0xffff7672,
157688eb819SNoah Goldstein 	0xffff7272, 0xffff726e, 0xffff6e6e, 0xffff6e6a, 0xffff6a6a, 0xffff6a66,
158688eb819SNoah Goldstein 	0xffff6666, 0xffff6662, 0xffff6262, 0xffff625e, 0xffff5e5e, 0xffff5e5a,
159688eb819SNoah Goldstein 	0xffff5a5a, 0xffff5a56, 0xffff5656, 0xffff5652, 0xffff5252, 0xffff524e,
160688eb819SNoah Goldstein 	0xffff4e4e, 0xffff4e4a, 0xffff4a4a, 0xffff4a46, 0xffff4646, 0xffff4642,
161688eb819SNoah Goldstein 	0xffff4242, 0xffff423e, 0xffff3e3e, 0xffff3e3a, 0xffff3a3a, 0xffff3a36,
162688eb819SNoah Goldstein 	0xffff3636, 0xffff3632, 0xffff3232, 0xffff322e, 0xffff2e2e, 0xffff2e2a,
163688eb819SNoah Goldstein 	0xffff2a2a, 0xffff2a26, 0xffff2626, 0xffff2622, 0xffff2222, 0xffff221e,
164688eb819SNoah Goldstein 	0xffff1e1e, 0xffff1e1a, 0xffff1a1a, 0xffff1a16, 0xffff1616, 0xffff1612,
165688eb819SNoah Goldstein 	0xffff1212, 0xffff120e, 0xffff0e0e, 0xffff0e0a, 0xffff0a0a, 0xffff0a06,
166688eb819SNoah Goldstein 	0xffff0606, 0xffff0602, 0xffff0202, 0xffff01fe, 0xfffffdfd, 0xfffffdf9,
167688eb819SNoah Goldstein 	0xfffff9f9, 0xfffff9f5, 0xfffff5f5, 0xfffff5f1, 0xfffff1f1, 0xfffff1ed,
168688eb819SNoah Goldstein 	0xffffeded, 0xffffede9, 0xffffe9e9, 0xffffe9e5, 0xffffe5e5, 0xffffe5e1,
169688eb819SNoah Goldstein 	0xffffe1e1, 0xffffe1dd, 0xffffdddd, 0xffffddd9, 0xffffd9d9, 0xffffd9d5,
170688eb819SNoah Goldstein 	0xffffd5d5, 0xffffd5d1, 0xffffd1d1, 0xffffd1cd, 0xffffcdcd, 0xffffcdc9,
171688eb819SNoah Goldstein 	0xffffc9c9, 0xffffc9c5, 0xffffc5c5, 0xffffc5c1, 0xffffc1c1, 0xffffc1bd,
172688eb819SNoah Goldstein 	0xffffbdbd, 0xffffbdb9, 0xffffb9b9, 0xffffb9b5, 0xffffb5b5, 0xffffb5b1,
173688eb819SNoah Goldstein 	0xffffb1b1, 0xffffb1ad, 0xffffadad, 0xffffada9, 0xffffa9a9, 0xffffa9a5,
174688eb819SNoah Goldstein 	0xffffa5a5, 0xffffa5a1, 0xffffa1a1, 0xffffa19d, 0xffff9d9d, 0xffff9d99,
175688eb819SNoah Goldstein 	0xffff9999, 0xffff9995, 0xffff9595, 0xffff9591, 0xffff9191, 0xffff918d,
176688eb819SNoah Goldstein 	0xffff8d8d, 0xffff8d89, 0xffff8989, 0xffff8985, 0xffff8585, 0xffff8581,
177688eb819SNoah Goldstein 	0xffff8181, 0xffff817d, 0xffff7d7d, 0xffff7d79, 0xffff7979, 0xffff7975,
178688eb819SNoah Goldstein 	0xffff7575, 0xffff7571, 0xffff7171, 0xffff716d, 0xffff6d6d, 0xffff6d69,
179688eb819SNoah Goldstein 	0xffff6969, 0xffff6965, 0xffff6565, 0xffff6561, 0xffff6161, 0xffff615d,
180688eb819SNoah Goldstein 	0xffff5d5d, 0xffff5d59, 0xffff5959, 0xffff5955, 0xffff5555, 0xffff5551,
181688eb819SNoah Goldstein 	0xffff5151, 0xffff514d, 0xffff4d4d, 0xffff4d49, 0xffff4949, 0xffff4945,
182688eb819SNoah Goldstein 	0xffff4545, 0xffff4541, 0xffff4141, 0xffff413d, 0xffff3d3d, 0xffff3d39,
183688eb819SNoah Goldstein 	0xffff3939, 0xffff3935, 0xffff3535, 0xffff3531, 0xffff3131, 0xffff312d,
184688eb819SNoah Goldstein 	0xffff2d2d, 0xffff2d29, 0xffff2929, 0xffff2925, 0xffff2525, 0xffff2521,
185688eb819SNoah Goldstein 	0xffff2121, 0xffff211d, 0xffff1d1d, 0xffff1d19, 0xffff1919, 0xffff1915,
186688eb819SNoah Goldstein 	0xffff1515, 0xffff1511, 0xffff1111, 0xffff110d, 0xffff0d0d, 0xffff0d09,
187688eb819SNoah Goldstein 	0xffff0909, 0xffff0905, 0xffff0505, 0xffff0501, 0xffff0101, 0xffff00fd,
188688eb819SNoah Goldstein 	0xfffffcfc, 0xfffffcf8, 0xfffff8f8, 0xfffff8f4, 0xfffff4f4, 0xfffff4f0,
189688eb819SNoah Goldstein 	0xfffff0f0, 0xfffff0ec, 0xffffecec, 0xffffece8, 0xffffe8e8, 0xffffe8e4,
190688eb819SNoah Goldstein 	0xffffe4e4, 0xffffe4e0, 0xffffe0e0, 0xffffe0dc, 0xffffdcdc, 0xffffdcd8,
191688eb819SNoah Goldstein 	0xffffd8d8, 0xffffd8d4, 0xffffd4d4, 0xffffd4d0, 0xffffd0d0, 0xffffd0cc,
192688eb819SNoah Goldstein 	0xffffcccc, 0xffffccc8, 0xffffc8c8, 0xffffc8c4, 0xffffc4c4, 0xffffc4c0,
193688eb819SNoah Goldstein 	0xffffc0c0, 0xffffc0bc, 0xffffbcbc, 0xffffbcb8, 0xffffb8b8, 0xffffb8b4,
194688eb819SNoah Goldstein 	0xffffb4b4, 0xffffb4b0, 0xffffb0b0, 0xffffb0ac, 0xffffacac, 0xffffaca8,
195688eb819SNoah Goldstein 	0xffffa8a8, 0xffffa8a4, 0xffffa4a4, 0xffffa4a0, 0xffffa0a0, 0xffffa09c,
196688eb819SNoah Goldstein 	0xffff9c9c, 0xffff9c98, 0xffff9898, 0xffff9894, 0xffff9494, 0xffff9490,
197688eb819SNoah Goldstein 	0xffff9090, 0xffff908c, 0xffff8c8c, 0xffff8c88, 0xffff8888, 0xffff8884,
198688eb819SNoah Goldstein 	0xffff8484, 0xffff8480, 0xffff8080, 0xffff807c, 0xffff7c7c, 0xffff7c78,
199688eb819SNoah Goldstein 	0xffff7878, 0xffff7874, 0xffff7474, 0xffff7470, 0xffff7070, 0xffff706c,
200688eb819SNoah Goldstein 	0xffff6c6c, 0xffff6c68, 0xffff6868, 0xffff6864, 0xffff6464, 0xffff6460,
201688eb819SNoah Goldstein 	0xffff6060, 0xffff605c, 0xffff5c5c, 0xffff5c58, 0xffff5858, 0xffff5854,
202688eb819SNoah Goldstein 	0xffff5454, 0xffff5450, 0xffff5050, 0xffff504c, 0xffff4c4c, 0xffff4c48,
203688eb819SNoah Goldstein 	0xffff4848, 0xffff4844, 0xffff4444, 0xffff4440, 0xffff4040, 0xffff403c,
204688eb819SNoah Goldstein 	0xffff3c3c, 0xffff3c38, 0xffff3838, 0xffff3834, 0xffff3434, 0xffff3430,
205688eb819SNoah Goldstein 	0xffff3030, 0xffff302c, 0xffff2c2c, 0xffff2c28, 0xffff2828, 0xffff2824,
206688eb819SNoah Goldstein 	0xffff2424, 0xffff2420, 0xffff2020, 0xffff201c, 0xffff1c1c, 0xffff1c18,
207688eb819SNoah Goldstein 	0xffff1818, 0xffff1814, 0xffff1414, 0xffff1410, 0xffff1010, 0xffff100c,
208688eb819SNoah Goldstein 	0xffff0c0c, 0xffff0c08, 0xffff0808, 0xffff0804, 0xffff0404, 0xffff0400,
209688eb819SNoah Goldstein 	0xffff0000, 0xfffffffb,
210688eb819SNoah Goldstein };
211688eb819SNoah Goldstein 
212688eb819SNoah Goldstein static u8 tmp_buf[TEST_BUFLEN];
213688eb819SNoah Goldstein 
214688eb819SNoah Goldstein #define full_csum(buff, len, sum) csum_fold(csum_partial(buff, len, sum))
215688eb819SNoah Goldstein 
216*b38460bcSChristophe Leroy #define CHECK_EQ(lhs, rhs) KUNIT_ASSERT_EQ(test, (__force u64)lhs, (__force u64)rhs)
217*b38460bcSChristophe Leroy 
to_sum16(u16 x)218*b38460bcSChristophe Leroy static __sum16 to_sum16(u16 x)
219*b38460bcSChristophe Leroy {
220*b38460bcSChristophe Leroy 	return (__force __sum16)le16_to_cpu((__force __le16)x);
221*b38460bcSChristophe Leroy }
222*b38460bcSChristophe Leroy 
223*b38460bcSChristophe Leroy /* This function swaps the bytes inside each half of a __wsum */
to_wsum(u32 x)224*b38460bcSChristophe Leroy static __wsum to_wsum(u32 x)
225*b38460bcSChristophe Leroy {
226*b38460bcSChristophe Leroy 	u16 hi = le16_to_cpu((__force __le16)(x >> 16));
227*b38460bcSChristophe Leroy 	u16 lo = le16_to_cpu((__force __le16)x);
228*b38460bcSChristophe Leroy 
229*b38460bcSChristophe Leroy 	return (__force __wsum)((hi << 16) | lo);
230*b38460bcSChristophe Leroy }
231688eb819SNoah Goldstein 
assert_setup_correct(struct kunit * test)232688eb819SNoah Goldstein static void assert_setup_correct(struct kunit *test)
233688eb819SNoah Goldstein {
234688eb819SNoah Goldstein 	CHECK_EQ(sizeof(random_buf) / sizeof(random_buf[0]), MAX_LEN);
235688eb819SNoah Goldstein 	CHECK_EQ(sizeof(expected_results) / sizeof(expected_results[0]),
236688eb819SNoah Goldstein 		 MAX_LEN);
237688eb819SNoah Goldstein 	CHECK_EQ(sizeof(init_sums_no_overflow) /
238688eb819SNoah Goldstein 			 sizeof(init_sums_no_overflow[0]),
239688eb819SNoah Goldstein 		 MAX_LEN);
240688eb819SNoah Goldstein }
241688eb819SNoah Goldstein 
242688eb819SNoah Goldstein /*
243688eb819SNoah Goldstein  * Test with randomized input (pre determined random with known results).
244688eb819SNoah Goldstein  */
test_csum_fixed_random_inputs(struct kunit * test)245688eb819SNoah Goldstein static void test_csum_fixed_random_inputs(struct kunit *test)
246688eb819SNoah Goldstein {
247688eb819SNoah Goldstein 	int len, align;
248*b38460bcSChristophe Leroy 	__wsum sum;
249*b38460bcSChristophe Leroy 	__sum16 result, expec;
250688eb819SNoah Goldstein 
251688eb819SNoah Goldstein 	assert_setup_correct(test);
252688eb819SNoah Goldstein 	for (align = 0; align < TEST_BUFLEN; ++align) {
253688eb819SNoah Goldstein 		memcpy(&tmp_buf[align], random_buf,
254688eb819SNoah Goldstein 		       min(MAX_LEN, TEST_BUFLEN - align));
255688eb819SNoah Goldstein 		for (len = 0; len < MAX_LEN && (align + len) < TEST_BUFLEN;
256688eb819SNoah Goldstein 		     ++len) {
257688eb819SNoah Goldstein 			/*
258688eb819SNoah Goldstein 			 * Test the precomputed random input.
259688eb819SNoah Goldstein 			 */
260*b38460bcSChristophe Leroy 			sum = to_wsum(random_init_sum);
261688eb819SNoah Goldstein 			result = full_csum(&tmp_buf[align], len, sum);
262*b38460bcSChristophe Leroy 			expec = to_sum16(expected_results[len]);
263688eb819SNoah Goldstein 			CHECK_EQ(result, expec);
264688eb819SNoah Goldstein 		}
265688eb819SNoah Goldstein 	}
266688eb819SNoah Goldstein }
267688eb819SNoah Goldstein 
268688eb819SNoah Goldstein /*
269688eb819SNoah Goldstein  * All ones input test. If there are any missing carry operations, it fails.
270688eb819SNoah Goldstein  */
test_csum_all_carry_inputs(struct kunit * test)271688eb819SNoah Goldstein static void test_csum_all_carry_inputs(struct kunit *test)
272688eb819SNoah Goldstein {
273688eb819SNoah Goldstein 	int len, align;
274*b38460bcSChristophe Leroy 	__wsum sum;
275*b38460bcSChristophe Leroy 	__sum16 result, expec;
276688eb819SNoah Goldstein 
277688eb819SNoah Goldstein 	assert_setup_correct(test);
278688eb819SNoah Goldstein 	memset(tmp_buf, 0xff, TEST_BUFLEN);
279688eb819SNoah Goldstein 	for (align = 0; align < TEST_BUFLEN; ++align) {
280688eb819SNoah Goldstein 		for (len = 0; len < MAX_LEN && (align + len) < TEST_BUFLEN;
281688eb819SNoah Goldstein 		     ++len) {
282688eb819SNoah Goldstein 			/*
283688eb819SNoah Goldstein 			 * All carries from input and initial sum.
284688eb819SNoah Goldstein 			 */
285*b38460bcSChristophe Leroy 			sum = to_wsum(0xffffffff);
286688eb819SNoah Goldstein 			result = full_csum(&tmp_buf[align], len, sum);
287*b38460bcSChristophe Leroy 			expec = to_sum16((len & 1) ? 0xff00 : 0);
288688eb819SNoah Goldstein 			CHECK_EQ(result, expec);
289688eb819SNoah Goldstein 
290688eb819SNoah Goldstein 			/*
291688eb819SNoah Goldstein 			 * All carries from input.
292688eb819SNoah Goldstein 			 */
293688eb819SNoah Goldstein 			sum = 0;
294688eb819SNoah Goldstein 			result = full_csum(&tmp_buf[align], len, sum);
295688eb819SNoah Goldstein 			if (len & 1)
296*b38460bcSChristophe Leroy 				expec = to_sum16(0xff00);
297688eb819SNoah Goldstein 			else if (len)
298688eb819SNoah Goldstein 				expec = 0;
299688eb819SNoah Goldstein 			else
300*b38460bcSChristophe Leroy 				expec = to_sum16(0xffff);
301688eb819SNoah Goldstein 			CHECK_EQ(result, expec);
302688eb819SNoah Goldstein 		}
303688eb819SNoah Goldstein 	}
304688eb819SNoah Goldstein }
305688eb819SNoah Goldstein 
306688eb819SNoah Goldstein /*
307688eb819SNoah Goldstein  * Test with input that alone doesn't cause any carries. By selecting the
308688eb819SNoah Goldstein  * maximum initial sum, this allows us to test that there are no carries
309688eb819SNoah Goldstein  * where there shouldn't be.
310688eb819SNoah Goldstein  */
test_csum_no_carry_inputs(struct kunit * test)311688eb819SNoah Goldstein static void test_csum_no_carry_inputs(struct kunit *test)
312688eb819SNoah Goldstein {
313688eb819SNoah Goldstein 	int len, align;
314*b38460bcSChristophe Leroy 	__wsum sum;
315*b38460bcSChristophe Leroy 	__sum16 result, expec;
316688eb819SNoah Goldstein 
317688eb819SNoah Goldstein 	assert_setup_correct(test);
318688eb819SNoah Goldstein 	memset(tmp_buf, 0x4, TEST_BUFLEN);
319688eb819SNoah Goldstein 	for (align = 0; align < TEST_BUFLEN; ++align) {
320688eb819SNoah Goldstein 		for (len = 0; len < MAX_LEN && (align + len) < TEST_BUFLEN;
321688eb819SNoah Goldstein 		     ++len) {
322688eb819SNoah Goldstein 			/*
323688eb819SNoah Goldstein 			 * Expect no carries.
324688eb819SNoah Goldstein 			 */
325*b38460bcSChristophe Leroy 			sum = to_wsum(init_sums_no_overflow[len]);
326688eb819SNoah Goldstein 			result = full_csum(&tmp_buf[align], len, sum);
327688eb819SNoah Goldstein 			expec = 0;
328688eb819SNoah Goldstein 			CHECK_EQ(result, expec);
329688eb819SNoah Goldstein 
330688eb819SNoah Goldstein 			/*
331688eb819SNoah Goldstein 			 * Expect one carry.
332688eb819SNoah Goldstein 			 */
333*b38460bcSChristophe Leroy 			sum = to_wsum(init_sums_no_overflow[len] + 1);
334688eb819SNoah Goldstein 			result = full_csum(&tmp_buf[align], len, sum);
335*b38460bcSChristophe Leroy 			expec = to_sum16(len ? 0xfffe : 0xffff);
336688eb819SNoah Goldstein 			CHECK_EQ(result, expec);
337688eb819SNoah Goldstein 		}
338688eb819SNoah Goldstein 	}
339688eb819SNoah Goldstein }
340688eb819SNoah Goldstein 
341688eb819SNoah Goldstein static struct kunit_case __refdata checksum_test_cases[] = {
342688eb819SNoah Goldstein 	KUNIT_CASE(test_csum_fixed_random_inputs),
343688eb819SNoah Goldstein 	KUNIT_CASE(test_csum_all_carry_inputs),
344688eb819SNoah Goldstein 	KUNIT_CASE(test_csum_no_carry_inputs),
345688eb819SNoah Goldstein 	{}
346688eb819SNoah Goldstein };
347688eb819SNoah Goldstein 
348688eb819SNoah Goldstein static struct kunit_suite checksum_test_suite = {
349688eb819SNoah Goldstein 	.name = "checksum",
350688eb819SNoah Goldstein 	.test_cases = checksum_test_cases,
351688eb819SNoah Goldstein };
352688eb819SNoah Goldstein 
353688eb819SNoah Goldstein kunit_test_suites(&checksum_test_suite);
354688eb819SNoah Goldstein 
355688eb819SNoah Goldstein MODULE_AUTHOR("Noah Goldstein <goldstein.w.n@gmail.com>");
356688eb819SNoah Goldstein MODULE_LICENSE("GPL");
357