New upstream version 18.02
[deb_dpdk.git] / test / test / test_crc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include "test.h"
6
7 #include <rte_hexdump.h>
8 #include <rte_malloc.h>
9 #include <rte_memcpy.h>
10 #include <rte_net_crc.h>
11
12 #define CRC_VEC_LEN        32
13 #define CRC32_VEC_LEN1     1512
14 #define CRC32_VEC_LEN2     348
15 #define CRC16_VEC_LEN1     12
16 #define CRC16_VEC_LEN2     2
17 #define LINE_LEN           75
18
19 /* CRC test vector */
20 static const uint8_t crc_vec[CRC_VEC_LEN] = {
21         '0', '1', '2', '3', '4', '5', '6', '7',
22         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
23         'g', 'h', 'i', 'j', 'A', 'B', 'C', 'D',
24         'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
25 };
26
27 /* 32-bit CRC test vector */
28 static const uint8_t crc32_vec1[12] = {
29         0xBE, 0xD7, 0x23, 0x47, 0x6B, 0x8F,
30         0xB3, 0x14, 0x5E, 0xFB, 0x35, 0x59,
31 };
32
33 /* 16-bit CRC test vector 1 */
34 static const uint8_t crc16_vec1[CRC16_VEC_LEN1] = {
35         0x0D, 0x01, 0x01, 0x23, 0x45, 0x67,
36         0x89, 0x01, 0x23, 0x45, 0x00, 0x01,
37 };
38
39 /* 16-bit CRC test vector 2 */
40 static const uint8_t crc16_vec2[CRC16_VEC_LEN2] = {
41         0x03, 0x3f,
42 };
43 /** CRC results */
44 static const uint32_t crc32_vec_res = 0xb491aab4;
45 static const uint32_t crc32_vec1_res = 0xac54d294;
46 static const uint32_t crc32_vec2_res = 0xefaae02f;
47 static const uint32_t crc16_vec_res = 0x6bec;
48 static const uint16_t crc16_vec1_res = 0x8cdd;
49 static const uint16_t crc16_vec2_res = 0xec5b;
50
51 static int
52 crc_calc(const uint8_t *vec,
53         uint32_t vec_len,
54         enum rte_net_crc_type type)
55 {
56         /* compute CRC */
57         uint32_t ret = rte_net_crc_calc(vec, vec_len, type);
58
59         /* dump data on console */
60         debug_hexdump(stdout, NULL, vec, vec_len);
61
62         return  ret;
63 }
64
65 static int
66 test_crc_calc(void)
67 {
68         uint32_t i;
69         enum rte_net_crc_type type;
70         uint8_t *test_data;
71         uint32_t result;
72         int error;
73
74         /* 32-bit ethernet CRC: Test 1 */
75         type = RTE_NET_CRC32_ETH;
76
77         result = crc_calc(crc_vec, CRC_VEC_LEN, type);
78         if (result != crc32_vec_res)
79                 return -1;
80
81         /* 32-bit ethernet CRC: Test 2 */
82         test_data = rte_zmalloc(NULL, CRC32_VEC_LEN1, 0);
83
84         for (i = 0; i < CRC32_VEC_LEN1; i += 12)
85                 rte_memcpy(&test_data[i], crc32_vec1, 12);
86
87         result = crc_calc(test_data, CRC32_VEC_LEN1, type);
88         if (result != crc32_vec1_res) {
89                 error = -2;
90                 goto fail;
91         }
92
93         /* 32-bit ethernet CRC: Test 3 */
94         for (i = 0; i < CRC32_VEC_LEN2; i += 12)
95                 rte_memcpy(&test_data[i], crc32_vec1, 12);
96
97         result = crc_calc(test_data, CRC32_VEC_LEN2, type);
98         if (result != crc32_vec2_res) {
99                 error = -3;
100                 goto fail;
101         }
102
103         /* 16-bit CCITT CRC:  Test 4 */
104         type = RTE_NET_CRC16_CCITT;
105         result = crc_calc(crc_vec, CRC_VEC_LEN, type);
106         if (result != crc16_vec_res) {
107                 error = -4;
108                 goto fail;
109         }
110         /* 16-bit CCITT CRC:  Test 5 */
111         result = crc_calc(crc16_vec1, CRC16_VEC_LEN1, type);
112         if (result != crc16_vec1_res) {
113                 error = -5;
114                 goto fail;
115         }
116         /* 16-bit CCITT CRC:  Test 6 */
117         result = crc_calc(crc16_vec2, CRC16_VEC_LEN2, type);
118         if (result != crc16_vec2_res) {
119                 error = -6;
120                 goto fail;
121         }
122
123         rte_free(test_data);
124         return 0;
125
126 fail:
127         rte_free(test_data);
128         return error;
129 }
130
131 static int
132 test_crc(void)
133 {
134         int ret;
135         /* set CRC scalar mode */
136         rte_net_crc_set_alg(RTE_NET_CRC_SCALAR);
137
138         ret = test_crc_calc();
139         if (ret < 0) {
140                 printf("test_crc (scalar): failed (%d)\n", ret);
141                 return ret;
142         }
143         /* set CRC sse4.2 mode */
144         rte_net_crc_set_alg(RTE_NET_CRC_SSE42);
145
146         ret = test_crc_calc();
147         if (ret < 0) {
148                 printf("test_crc (x86_64_SSE4.2): failed (%d)\n", ret);
149                 return ret;
150         }
151
152         /* set CRC neon mode */
153         rte_net_crc_set_alg(RTE_NET_CRC_NEON);
154
155         ret = test_crc_calc();
156         if (ret < 0) {
157                 printf("test crc (arm64 neon pmull): failed (%d)\n", ret);
158                 return ret;
159         }
160
161         return 0;
162 }
163
164 REGISTER_TEST_COMMAND(crc_autotest, test_crc);