1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
5 #include <vppinfra/format.h>
6 #include <vppinfra/vector/test/test.h>
7 #include <vppinfra/vector/ip_csum.h>
19 static u8 test1[] = { 0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40,
20 0x00, 0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8,
21 0x00, 0x01, 0xc0, 0xa8, 0x00, 0xc7, 0x00 };
22 #define TEST_LEN(x) (ARRAY_LEN (x) - 1)
24 static ip_csum_test_t tests[] = { {
25 .chunk[0].src = test1,
26 .chunk[0].count = TEST_LEN (test1),
30 .chunk[0].src = test1,
32 .chunk[1].src = test1 + 1,
34 .chunk[2].src = test1 + 3,
36 .chunk[3].src = test1 + 6,
38 .chunk[4].src = test1 + 10,
39 .chunk[4].count = TEST_LEN (test1) - 10,
71 .chunk[0].count = 65535,
75 .chunk[0].count = 65536,
80 test_clib_ip_csum (clib_error_t *err)
83 buf = test_mem_alloc (65536);
84 for (int i = 0; i < 65536; i++)
85 buf[i] = 0xf0 + ((i * 7) & 0xf);
87 for (int i = 0; i < ARRAY_LEN (tests); i++)
89 clib_ip_csum_t c = {};
90 ip_csum_test_t *t = tests + i;
93 for (int j = 0; j < ARRAY_LEN (((ip_csum_test_t *) 0)->chunk); j++)
94 if (t->chunk[j].count > 0)
96 if (t->chunk[j].src == 0)
97 clib_ip_csum_chunk (&c, buf, t->chunk[j].count);
99 clib_ip_csum_chunk (&c, t->chunk[j].src, t->chunk[j].count);
101 rv = clib_ip_csum_fold (&c);
103 if (rv != tests[i].result)
105 err = clib_error_return (err,
106 "bad checksum in test case %u (expected "
107 "0x%04x, calculated 0x%04x)",
108 i, tests[i].result, rv);
118 perftest_ip4_hdr (test_perf_t *tp)
121 u8 *data = test_mem_alloc_and_splat (20, n, (void *) &test1);
122 u16 *res = test_mem_alloc (n * sizeof (u16));
124 test_perf_event_enable (tp);
125 for (int i = 0; i < n; i++)
126 res[i] = clib_ip_csum (data + i * 20, 20);
127 test_perf_event_disable (tp);
129 test_mem_free (data);
134 perftest_tcp_payload (test_perf_t *tp)
137 volatile uword *lenp = &tp->arg0;
138 u8 *data = test_mem_alloc_and_splat (20, n, (void *) &test1);
139 u16 *res = test_mem_alloc (n * sizeof (u16));
141 test_perf_event_enable (tp);
142 for (int i = 0; i < n; i++)
143 res[i] = clib_ip_csum (data + i * lenp[0], lenp[0]);
144 test_perf_event_disable (tp);
146 test_mem_free (data);
151 perftest_byte (test_perf_t *tp)
153 volatile uword *np = &tp->n_ops;
154 u8 *data = test_mem_alloc_and_fill_inc_u8 (*np, 0, 0);
155 u16 *res = test_mem_alloc (sizeof (u16));
157 test_perf_event_enable (tp);
158 res[0] = clib_ip_csum (data, np[0]);
159 test_perf_event_disable (tp);
161 test_mem_free (data);
165 REGISTER_TEST (clib_ip_csum) = {
166 .name = "clib_ip_csum",
167 .fn = test_clib_ip_csum,
168 .perf_tests = PERF_TESTS (
169 { .name = "fixed size (per IPv4 Header)",
171 .fn = perftest_ip4_hdr },
172 { .name = "fixed size (per 1460 byte block)",
175 .fn = perftest_tcp_payload },
176 { .name = "variable size (per byte)", .n_ops = 16384, .fn = perftest_byte }