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