selftests/bpf: Fix check_mtu using wrong variable type
[platform/kernel/linux-starfive.git] / tools / testing / selftests / bpf / prog_tests / check_mtu.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Jesper Dangaard Brouer */
3
4 #include <linux/if_link.h> /* before test_progs.h, avoid bpf_util.h redefines */
5 #include <test_progs.h>
6 #include "test_check_mtu.skel.h"
7 #include "network_helpers.h"
8
9 #include <stdlib.h>
10 #include <inttypes.h>
11
12 #define IFINDEX_LO 1
13
14 static __u32 duration; /* Hint: needed for CHECK macro */
15
16 static int read_mtu_device_lo(void)
17 {
18         const char *filename = "/sys/class/net/lo/mtu";
19         char buf[11] = {};
20         int value, n, fd;
21
22         fd = open(filename, 0, O_RDONLY);
23         if (fd == -1)
24                 return -1;
25
26         n = read(fd, buf, sizeof(buf));
27         close(fd);
28
29         if (n == -1)
30                 return -2;
31
32         value = strtoimax(buf, NULL, 10);
33         if (errno == ERANGE)
34                 return -3;
35
36         return value;
37 }
38
39 static void test_check_mtu_xdp_attach(void)
40 {
41         struct bpf_link_info link_info;
42         __u32 link_info_len = sizeof(link_info);
43         struct test_check_mtu *skel;
44         struct bpf_program *prog;
45         struct bpf_link *link;
46         int err = 0;
47         int fd;
48
49         skel = test_check_mtu__open_and_load();
50         if (CHECK(!skel, "open and load skel", "failed"))
51                 return; /* Exit if e.g. helper unknown to kernel */
52
53         prog = skel->progs.xdp_use_helper_basic;
54
55         link = bpf_program__attach_xdp(prog, IFINDEX_LO);
56         if (!ASSERT_OK_PTR(link, "link_attach"))
57                 goto out;
58         skel->links.xdp_use_helper_basic = link;
59
60         memset(&link_info, 0, sizeof(link_info));
61         fd = bpf_link__fd(link);
62         err = bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
63         if (CHECK(err, "link_info", "failed: %d\n", err))
64                 goto out;
65
66         CHECK(link_info.type != BPF_LINK_TYPE_XDP, "link_type",
67               "got %u != exp %u\n", link_info.type, BPF_LINK_TYPE_XDP);
68         CHECK(link_info.xdp.ifindex != IFINDEX_LO, "link_ifindex",
69               "got %u != exp %u\n", link_info.xdp.ifindex, IFINDEX_LO);
70
71         err = bpf_link__detach(link);
72         CHECK(err, "link_detach", "failed %d\n", err);
73
74 out:
75         test_check_mtu__destroy(skel);
76 }
77
78 static void test_check_mtu_run_xdp(struct test_check_mtu *skel,
79                                    struct bpf_program *prog,
80                                    __u32 mtu_expect)
81 {
82         int retval_expect = XDP_PASS;
83         __u32 mtu_result = 0;
84         char buf[256] = {};
85         int err, prog_fd = bpf_program__fd(prog);
86         LIBBPF_OPTS(bpf_test_run_opts, topts,
87                 .repeat = 1,
88                 .data_in = &pkt_v4,
89                 .data_size_in = sizeof(pkt_v4),
90                 .data_out = buf,
91                 .data_size_out = sizeof(buf),
92         );
93
94         err = bpf_prog_test_run_opts(prog_fd, &topts);
95         ASSERT_OK(err, "test_run");
96         ASSERT_EQ(topts.retval, retval_expect, "retval");
97
98         /* Extract MTU that BPF-prog got */
99         mtu_result = skel->bss->global_bpf_mtu_xdp;
100         ASSERT_EQ(mtu_result, mtu_expect, "MTU-compare-user");
101 }
102
103
104 static void test_check_mtu_xdp(__u32 mtu, __u32 ifindex)
105 {
106         struct test_check_mtu *skel;
107         int err;
108
109         skel = test_check_mtu__open();
110         if (CHECK(!skel, "skel_open", "failed"))
111                 return;
112
113         /* Update "constants" in BPF-prog *BEFORE* libbpf load */
114         skel->rodata->GLOBAL_USER_MTU = mtu;
115         skel->rodata->GLOBAL_USER_IFINDEX = ifindex;
116
117         err = test_check_mtu__load(skel);
118         if (CHECK(err, "skel_load", "failed: %d\n", err))
119                 goto cleanup;
120
121         test_check_mtu_run_xdp(skel, skel->progs.xdp_use_helper, mtu);
122         test_check_mtu_run_xdp(skel, skel->progs.xdp_exceed_mtu, mtu);
123         test_check_mtu_run_xdp(skel, skel->progs.xdp_minus_delta, mtu);
124         test_check_mtu_run_xdp(skel, skel->progs.xdp_input_len, mtu);
125         test_check_mtu_run_xdp(skel, skel->progs.xdp_input_len_exceed, mtu);
126
127 cleanup:
128         test_check_mtu__destroy(skel);
129 }
130
131 static void test_check_mtu_run_tc(struct test_check_mtu *skel,
132                                   struct bpf_program *prog,
133                                   __u32 mtu_expect)
134 {
135         int retval_expect = BPF_OK;
136         __u32 mtu_result = 0;
137         char buf[256] = {};
138         int err, prog_fd = bpf_program__fd(prog);
139         LIBBPF_OPTS(bpf_test_run_opts, topts,
140                 .data_in = &pkt_v4,
141                 .data_size_in = sizeof(pkt_v4),
142                 .data_out = buf,
143                 .data_size_out = sizeof(buf),
144                 .repeat = 1,
145         );
146
147         err = bpf_prog_test_run_opts(prog_fd, &topts);
148         ASSERT_OK(err, "test_run");
149         ASSERT_EQ(topts.retval, retval_expect, "retval");
150
151         /* Extract MTU that BPF-prog got */
152         mtu_result = skel->bss->global_bpf_mtu_tc;
153         ASSERT_EQ(mtu_result, mtu_expect, "MTU-compare-user");
154 }
155
156
157 static void test_check_mtu_tc(__u32 mtu, __u32 ifindex)
158 {
159         struct test_check_mtu *skel;
160         int err;
161
162         skel = test_check_mtu__open();
163         if (CHECK(!skel, "skel_open", "failed"))
164                 return;
165
166         /* Update "constants" in BPF-prog *BEFORE* libbpf load */
167         skel->rodata->GLOBAL_USER_MTU = mtu;
168         skel->rodata->GLOBAL_USER_IFINDEX = ifindex;
169
170         err = test_check_mtu__load(skel);
171         if (CHECK(err, "skel_load", "failed: %d\n", err))
172                 goto cleanup;
173
174         test_check_mtu_run_tc(skel, skel->progs.tc_use_helper, mtu);
175         test_check_mtu_run_tc(skel, skel->progs.tc_exceed_mtu, mtu);
176         test_check_mtu_run_tc(skel, skel->progs.tc_exceed_mtu_da, mtu);
177         test_check_mtu_run_tc(skel, skel->progs.tc_minus_delta, mtu);
178         test_check_mtu_run_tc(skel, skel->progs.tc_input_len, mtu);
179         test_check_mtu_run_tc(skel, skel->progs.tc_input_len_exceed, mtu);
180 cleanup:
181         test_check_mtu__destroy(skel);
182 }
183
184 void serial_test_check_mtu(void)
185 {
186         int mtu_lo;
187
188         if (test__start_subtest("bpf_check_mtu XDP-attach"))
189                 test_check_mtu_xdp_attach();
190
191         mtu_lo = read_mtu_device_lo();
192         if (CHECK(mtu_lo < 0, "reading MTU value", "failed (err:%d)", mtu_lo))
193                 return;
194
195         if (test__start_subtest("bpf_check_mtu XDP-run"))
196                 test_check_mtu_xdp(mtu_lo, 0);
197
198         if (test__start_subtest("bpf_check_mtu XDP-run ifindex-lookup"))
199                 test_check_mtu_xdp(mtu_lo, IFINDEX_LO);
200
201         if (test__start_subtest("bpf_check_mtu TC-run"))
202                 test_check_mtu_tc(mtu_lo, 0);
203
204         if (test__start_subtest("bpf_check_mtu TC-run ifindex-lookup"))
205                 test_check_mtu_tc(mtu_lo, IFINDEX_LO);
206 }