Skip to content

Commit 5b2be2a

Browse files
alobakindavem330
authored andcommitted
net: net_test: add tests for IP tunnel flags conversion helpers
Now that there are helpers for converting IP tunnel flags between the old __be16 format and the bitmap format, make sure they work as expected by adding a couple of tests to the networking testing suite. The helpers are all inline, so no dependencies on the related CONFIG_* (or a standalone module) are needed. Cover three possible cases: 1. No bits past BIT(15) are set, VTI/SIT bits are not set. This conversion is almost a direct assignment. 2. No bits past BIT(15) are set, but VTI/SIT bit is set. During the conversion, it must be transformed into BIT(16) in the bitmap, but still compatible with the __be16 format. 3. The bitmap has bits past BIT(15) set (not the VTI/SIT one). The result will be truncated. Note that currently __IP_TUNNEL_FLAG_NUM is 17 (incl. special), which means that the result of this case is currently semi-false-positive. When BIT(17) is finally here, it will be adjusted accordingly. Signed-off-by: Alexander Lobakin <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 5832c4a commit 5b2be2a

File tree

2 files changed

+125
-9
lines changed

2 files changed

+125
-9
lines changed

net/core/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ obj-$(CONFIG_NET_SOCK_MSG) += skmsg.o
4141
obj-$(CONFIG_BPF_SYSCALL) += sock_map.o
4242
obj-$(CONFIG_BPF_SYSCALL) += bpf_sk_storage.o
4343
obj-$(CONFIG_OF) += of_net.o
44-
obj-$(CONFIG_NET_TEST) += gso_test.o
44+
obj-$(CONFIG_NET_TEST) += net_test.o

net/core/gso_test.c renamed to net/core/net_test.c

Lines changed: 124 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// SPDX-License-Identifier: GPL-2.0-or-later
22

33
#include <kunit/test.h>
4+
5+
/* GSO */
6+
47
#include <linux/skbuff.h>
58

69
static const char hdr[] = "abcdefgh";
@@ -258,17 +261,130 @@ static void gso_test_func(struct kunit *test)
258261
consume_skb(skb);
259262
}
260263

261-
static struct kunit_case gso_test_cases[] = {
262-
KUNIT_CASE_PARAM(gso_test_func, gso_test_gen_params),
263-
{}
264+
/* IP tunnel flags */
265+
266+
#include <net/ip_tunnels.h>
267+
268+
struct ip_tunnel_flags_test {
269+
const char *name;
270+
271+
const u16 *src_bits;
272+
const u16 *exp_bits;
273+
u8 src_num;
274+
u8 exp_num;
275+
276+
__be16 exp_val;
277+
bool exp_comp;
278+
};
279+
280+
#define IP_TUNNEL_FLAGS_TEST(n, src, comp, eval, exp) { \
281+
.name = (n), \
282+
.src_bits = (src), \
283+
.src_num = ARRAY_SIZE(src), \
284+
.exp_comp = (comp), \
285+
.exp_val = (eval), \
286+
.exp_bits = (exp), \
287+
.exp_num = ARRAY_SIZE(exp), \
288+
}
289+
290+
/* These are __be16-compatible and can be compared as is */
291+
static const u16 ip_tunnel_flags_1[] = {
292+
IP_TUNNEL_KEY_BIT,
293+
IP_TUNNEL_STRICT_BIT,
294+
IP_TUNNEL_ERSPAN_OPT_BIT,
295+
};
296+
297+
/* Due to the previous flags design limitation, setting either
298+
* ``IP_TUNNEL_CSUM_BIT`` (on Big Endian) or ``IP_TUNNEL_DONT_FRAGMENT_BIT``
299+
* (on Little) also sets VTI/ISATAP bit. In the bitmap implementation, they
300+
* correspond to ``BIT(16)``, which is bigger than ``U16_MAX``, but still is
301+
* backward-compatible.
302+
*/
303+
#ifdef __LITTLE_ENDIAN
304+
#define IP_TUNNEL_CONFLICT_BIT IP_TUNNEL_DONT_FRAGMENT_BIT
305+
#else
306+
#define IP_TUNNEL_CONFLICT_BIT IP_TUNNEL_CSUM_BIT
307+
#endif
308+
309+
static const u16 ip_tunnel_flags_2_src[] = {
310+
IP_TUNNEL_CONFLICT_BIT,
311+
};
312+
313+
static const u16 ip_tunnel_flags_2_exp[] = {
314+
IP_TUNNEL_CONFLICT_BIT,
315+
IP_TUNNEL_SIT_ISATAP_BIT,
264316
};
265317

266-
static struct kunit_suite gso_test_suite = {
267-
.name = "net_core_gso",
268-
.test_cases = gso_test_cases,
318+
/* Bits 17 and higher are not compatible with __be16 flags */
319+
static const u16 ip_tunnel_flags_3_src[] = {
320+
IP_TUNNEL_VXLAN_OPT_BIT,
321+
17,
322+
18,
323+
20,
269324
};
270325

271-
kunit_test_suite(gso_test_suite);
326+
static const u16 ip_tunnel_flags_3_exp[] = {
327+
IP_TUNNEL_VXLAN_OPT_BIT,
328+
};
329+
330+
static const struct ip_tunnel_flags_test ip_tunnel_flags_test[] = {
331+
IP_TUNNEL_FLAGS_TEST("compat", ip_tunnel_flags_1, true,
332+
cpu_to_be16(BIT(IP_TUNNEL_KEY_BIT) |
333+
BIT(IP_TUNNEL_STRICT_BIT) |
334+
BIT(IP_TUNNEL_ERSPAN_OPT_BIT)),
335+
ip_tunnel_flags_1),
336+
IP_TUNNEL_FLAGS_TEST("conflict", ip_tunnel_flags_2_src, true,
337+
VTI_ISVTI, ip_tunnel_flags_2_exp),
338+
IP_TUNNEL_FLAGS_TEST("new", ip_tunnel_flags_3_src,
339+
/* This must be set to ``false`` once
340+
* ``__IP_TUNNEL_FLAG_NUM`` goes above 17.
341+
*/
342+
true, cpu_to_be16(BIT(IP_TUNNEL_VXLAN_OPT_BIT)),
343+
ip_tunnel_flags_3_exp),
344+
};
345+
346+
static void
347+
ip_tunnel_flags_test_case_to_desc(const struct ip_tunnel_flags_test *t,
348+
char *desc)
349+
{
350+
strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
351+
}
352+
KUNIT_ARRAY_PARAM(ip_tunnel_flags_test, ip_tunnel_flags_test,
353+
ip_tunnel_flags_test_case_to_desc);
354+
355+
static void ip_tunnel_flags_test_run(struct kunit *test)
356+
{
357+
const struct ip_tunnel_flags_test *t = test->param_value;
358+
IP_TUNNEL_DECLARE_FLAGS(src) = { };
359+
IP_TUNNEL_DECLARE_FLAGS(exp) = { };
360+
IP_TUNNEL_DECLARE_FLAGS(out);
361+
362+
for (u32 j = 0; j < t->src_num; j++)
363+
__set_bit(t->src_bits[j], src);
364+
for (u32 j = 0; j < t->exp_num; j++)
365+
__set_bit(t->exp_bits[j], exp);
366+
367+
KUNIT_ASSERT_EQ(test, t->exp_comp,
368+
ip_tunnel_flags_is_be16_compat(src));
369+
KUNIT_ASSERT_EQ(test, (__force u16)t->exp_val,
370+
(__force u16)ip_tunnel_flags_to_be16(src));
371+
372+
ip_tunnel_flags_from_be16(out, t->exp_val);
373+
KUNIT_ASSERT_TRUE(test, __ipt_flag_op(bitmap_equal, exp, out));
374+
}
375+
376+
static struct kunit_case net_test_cases[] = {
377+
KUNIT_CASE_PARAM(gso_test_func, gso_test_gen_params),
378+
KUNIT_CASE_PARAM(ip_tunnel_flags_test_run,
379+
ip_tunnel_flags_test_gen_params),
380+
{ },
381+
};
382+
383+
static struct kunit_suite net_test_suite = {
384+
.name = "net_core",
385+
.test_cases = net_test_cases,
386+
};
387+
kunit_test_suite(net_test_suite);
272388

389+
MODULE_DESCRIPTION("KUnit tests for networking core");
273390
MODULE_LICENSE("GPL");
274-
MODULE_DESCRIPTION("KUnit tests for segmentation offload");

0 commit comments

Comments
 (0)