xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c (revision 34d6f206a88c2651d216bd3487ac956a40b2ba8e)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define _GNU_SOURCE
4 #include <test_progs.h>
5 #include <network_helpers.h>
6 #include "test_parse_tcp_hdr_opt.skel.h"
7 #include "test_parse_tcp_hdr_opt_dynptr.skel.h"
8 #include "test_tcp_hdr_options.h"
9 
10 struct test_pkt {
11 	struct ipv6_packet pk6_v6;
12 	u8 options[16];
13 } __packed;
14 
15 struct test_pkt pkt = {
16 	.pk6_v6.eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
17 	.pk6_v6.iph.nexthdr = IPPROTO_TCP,
18 	.pk6_v6.iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
19 	.pk6_v6.tcp.urg_ptr = 123,
20 	.pk6_v6.tcp.doff = 9, /* 16 bytes of options */
21 
22 	.options = {
23 		TCPOPT_MSS, 4, 0x05, 0xB4, TCPOPT_NOP, TCPOPT_NOP,
24 		0, 6, 0xBB, 0xBB, 0xBB, 0xBB, TCPOPT_EOL
25 	},
26 };
27 
test_parse_opt(void)28 static void test_parse_opt(void)
29 {
30 	struct test_parse_tcp_hdr_opt *skel;
31 	struct bpf_program *prog;
32 	char buf[128];
33 	int err;
34 
35 	LIBBPF_OPTS(bpf_test_run_opts, topts,
36 		    .data_in = &pkt,
37 		    .data_size_in = sizeof(pkt),
38 		    .data_out = buf,
39 		    .data_size_out = sizeof(buf),
40 		    .repeat = 3,
41 	);
42 
43 	skel = test_parse_tcp_hdr_opt__open_and_load();
44 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
45 		return;
46 
47 	pkt.options[6] = skel->rodata->tcp_hdr_opt_kind_tpr;
48 	prog = skel->progs.xdp_ingress_v6;
49 
50 	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
51 	ASSERT_OK(err, "ipv6 test_run");
52 	ASSERT_EQ(topts.retval, XDP_PASS, "ipv6 test_run retval");
53 	ASSERT_EQ(skel->bss->server_id, 0xBBBBBBBB, "server id");
54 
55 	test_parse_tcp_hdr_opt__destroy(skel);
56 }
57 
test_parse_opt_dynptr(void)58 static void test_parse_opt_dynptr(void)
59 {
60 	struct test_parse_tcp_hdr_opt_dynptr *skel;
61 	struct bpf_program *prog;
62 	char buf[128];
63 	int err;
64 
65 	LIBBPF_OPTS(bpf_test_run_opts, topts,
66 		    .data_in = &pkt,
67 		    .data_size_in = sizeof(pkt),
68 		    .data_out = buf,
69 		    .data_size_out = sizeof(buf),
70 		    .repeat = 3,
71 	);
72 
73 	skel = test_parse_tcp_hdr_opt_dynptr__open_and_load();
74 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
75 		return;
76 
77 	pkt.options[6] = skel->rodata->tcp_hdr_opt_kind_tpr;
78 	prog = skel->progs.xdp_ingress_v6;
79 
80 	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
81 	ASSERT_OK(err, "ipv6 test_run");
82 	ASSERT_EQ(topts.retval, XDP_PASS, "ipv6 test_run retval");
83 	ASSERT_EQ(skel->bss->server_id, 0xBBBBBBBB, "server id");
84 
85 	test_parse_tcp_hdr_opt_dynptr__destroy(skel);
86 }
87 
test_parse_tcp_hdr_opt(void)88 void test_parse_tcp_hdr_opt(void)
89 {
90 	if (test__start_subtest("parse_tcp_hdr_opt"))
91 		test_parse_opt();
92 	if (test__start_subtest("parse_tcp_hdr_opt_dynptr"))
93 		test_parse_opt_dynptr();
94 }
95