1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/* SPDX-License-Identifier: Apache-2.0
* Copyright(c) 2021 Cisco Systems, Inc.
*/
#include <vppinfra/format.h>
#include <vppinfra/test/test.h>
#include <vppinfra/vector/ip_csum.h>
typedef struct
{
struct
{
u8 *src;
u32 count;
} chunk[5];
u16 result;
} ip_csum_test_t;
static u8 test1[] = { 0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40,
0x00, 0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8,
0x00, 0x01, 0xc0, 0xa8, 0x00, 0xc7, 0x00 };
#define TEST_LEN(x) (ARRAY_LEN (x) - 1)
static ip_csum_test_t tests[] = { {
.chunk[0].src = test1,
.chunk[0].count = TEST_LEN (test1),
.result = 0x61b8,
},
{
.chunk[0].src = test1,
.chunk[0].count = 1,
.chunk[1].src = test1 + 1,
.chunk[1].count = 2,
.chunk[2].src = test1 + 3,
.chunk[2].count = 3,
.chunk[3].src = test1 + 6,
.chunk[3].count = 4,
.chunk[4].src = test1 + 10,
.chunk[4].count = TEST_LEN (test1) - 10,
.result = 0x61b8,
},
{
.chunk[0].count = 1,
.result = 0xff0f,
},
{
.chunk[0].count = 2,
.result = 0x080f,
},
{
.chunk[0].count = 3,
.result = 0x0711,
},
{
.chunk[0].count = 4,
.result = 0x1210,
},
{
.chunk[0].count = 63,
.result = 0xda01,
},
{
.chunk[0].count = 64,
.result = 0xe100,
},
{
.chunk[0].count = 65,
.result = 0xe010,
},
{
.chunk[0].count = 65535,
.result = 0xfc84,
},
{
.chunk[0].count = 65536,
.result = 0xffff,
} };
static clib_error_t *
test_clib_ip_csum (clib_error_t *err)
{
u8 *buf;
buf = test_mem_alloc (65536);
for (int i = 0; i < 65536; i++)
buf[i] = 0xf0 + ((i * 7) & 0xf);
for (int i = 0; i < ARRAY_LEN (tests); i++)
{
clib_ip_csum_t c = {};
ip_csum_test_t *t = tests + i;
u16 rv;
for (int j = 0; j < ARRAY_LEN (((ip_csum_test_t *) 0)->chunk); j++)
if (t->chunk[j].count > 0)
{
if (t->chunk[j].src == 0)
clib_ip_csum_chunk (&c, buf, t->chunk[j].count);
else
clib_ip_csum_chunk (&c, t->chunk[j].src, t->chunk[j].count);
}
rv = clib_ip_csum_fold (&c);
if (rv != tests[i].result)
{
err = clib_error_return (err,
"bad checksum in test case %u (expected "
"0x%04x, calculated 0x%04x)",
i, tests[i].result, rv);
goto done;
}
}
done:
return err;
}
void __test_perf_fn
perftest_ip4_hdr (test_perf_t *tp)
{
u32 n = tp->n_ops;
u8 *data = test_mem_alloc_and_splat (20, n, (void *) &test1);
u16 *res = test_mem_alloc (n * sizeof (u16));
test_perf_event_enable (tp);
for (int i = 0; i < n; i++)
res[i] = clib_ip_csum (data + i * 20, 20);
test_perf_event_disable (tp);
}
void __test_perf_fn
perftest_tcp_payload (test_perf_t *tp)
{
u32 n = tp->n_ops;
volatile uword *lenp = &tp->arg0;
u8 *data = test_mem_alloc_and_splat (20, n, (void *) &test1);
u16 *res = test_mem_alloc (n * sizeof (u16));
test_perf_event_enable (tp);
for (int i = 0; i < n; i++)
res[i] = clib_ip_csum (data + i * lenp[0], lenp[0]);
test_perf_event_disable (tp);
}
void __test_perf_fn
perftest_byte (test_perf_t *tp)
{
volatile uword *np = &tp->n_ops;
u8 *data = test_mem_alloc_and_fill_inc_u8 (*np, 0, 0);
u16 *res = test_mem_alloc (sizeof (u16));
test_perf_event_enable (tp);
res[0] = clib_ip_csum (data, np[0]);
test_perf_event_disable (tp);
}
REGISTER_TEST (clib_ip_csum) = {
.name = "clib_ip_csum",
.fn = test_clib_ip_csum,
.perf_tests = PERF_TESTS (
{ .name = "fixed size (per IPv4 Header)",
.n_ops = 1024,
.fn = perftest_ip4_hdr },
{ .name = "fixed size (per 1460 byte block)",
.n_ops = 16,
.arg0 = 1460,
.fn = perftest_tcp_payload },
{ .name = "variable size (per byte)", .n_ops = 16384, .fn = perftest_byte }
),
};
|