0001
0002
0003
0004 #include <linux/if_link.h> /* before test_progs.h, avoid bpf_util.h redefines */
0005 #include <test_progs.h>
0006 #include "test_check_mtu.skel.h"
0007 #include "network_helpers.h"
0008
0009 #include <stdlib.h>
0010 #include <inttypes.h>
0011
0012 #define IFINDEX_LO 1
0013
0014 static __u32 duration;
0015
0016 static int read_mtu_device_lo(void)
0017 {
0018 const char *filename = "/sys/class/net/lo/mtu";
0019 char buf[11] = {};
0020 int value, n, fd;
0021
0022 fd = open(filename, 0, O_RDONLY);
0023 if (fd == -1)
0024 return -1;
0025
0026 n = read(fd, buf, sizeof(buf));
0027 close(fd);
0028
0029 if (n == -1)
0030 return -2;
0031
0032 value = strtoimax(buf, NULL, 10);
0033 if (errno == ERANGE)
0034 return -3;
0035
0036 return value;
0037 }
0038
0039 static void test_check_mtu_xdp_attach(void)
0040 {
0041 struct bpf_link_info link_info;
0042 __u32 link_info_len = sizeof(link_info);
0043 struct test_check_mtu *skel;
0044 struct bpf_program *prog;
0045 struct bpf_link *link;
0046 int err = 0;
0047 int fd;
0048
0049 skel = test_check_mtu__open_and_load();
0050 if (CHECK(!skel, "open and load skel", "failed"))
0051 return;
0052
0053 prog = skel->progs.xdp_use_helper_basic;
0054
0055 link = bpf_program__attach_xdp(prog, IFINDEX_LO);
0056 if (!ASSERT_OK_PTR(link, "link_attach"))
0057 goto out;
0058 skel->links.xdp_use_helper_basic = link;
0059
0060 memset(&link_info, 0, sizeof(link_info));
0061 fd = bpf_link__fd(link);
0062 err = bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
0063 if (CHECK(err, "link_info", "failed: %d\n", err))
0064 goto out;
0065
0066 CHECK(link_info.type != BPF_LINK_TYPE_XDP, "link_type",
0067 "got %u != exp %u\n", link_info.type, BPF_LINK_TYPE_XDP);
0068 CHECK(link_info.xdp.ifindex != IFINDEX_LO, "link_ifindex",
0069 "got %u != exp %u\n", link_info.xdp.ifindex, IFINDEX_LO);
0070
0071 err = bpf_link__detach(link);
0072 CHECK(err, "link_detach", "failed %d\n", err);
0073
0074 out:
0075 test_check_mtu__destroy(skel);
0076 }
0077
0078 static void test_check_mtu_run_xdp(struct test_check_mtu *skel,
0079 struct bpf_program *prog,
0080 __u32 mtu_expect)
0081 {
0082 int retval_expect = XDP_PASS;
0083 __u32 mtu_result = 0;
0084 char buf[256] = {};
0085 int err, prog_fd = bpf_program__fd(prog);
0086 LIBBPF_OPTS(bpf_test_run_opts, topts,
0087 .repeat = 1,
0088 .data_in = &pkt_v4,
0089 .data_size_in = sizeof(pkt_v4),
0090 .data_out = buf,
0091 .data_size_out = sizeof(buf),
0092 );
0093
0094 err = bpf_prog_test_run_opts(prog_fd, &topts);
0095 ASSERT_OK(err, "test_run");
0096 ASSERT_EQ(topts.retval, retval_expect, "retval");
0097
0098
0099 mtu_result = skel->bss->global_bpf_mtu_xdp;
0100 ASSERT_EQ(mtu_result, mtu_expect, "MTU-compare-user");
0101 }
0102
0103
0104 static void test_check_mtu_xdp(__u32 mtu, __u32 ifindex)
0105 {
0106 struct test_check_mtu *skel;
0107 int err;
0108
0109 skel = test_check_mtu__open();
0110 if (CHECK(!skel, "skel_open", "failed"))
0111 return;
0112
0113
0114 skel->rodata->GLOBAL_USER_MTU = mtu;
0115 skel->rodata->GLOBAL_USER_IFINDEX = ifindex;
0116
0117 err = test_check_mtu__load(skel);
0118 if (CHECK(err, "skel_load", "failed: %d\n", err))
0119 goto cleanup;
0120
0121 test_check_mtu_run_xdp(skel, skel->progs.xdp_use_helper, mtu);
0122 test_check_mtu_run_xdp(skel, skel->progs.xdp_exceed_mtu, mtu);
0123 test_check_mtu_run_xdp(skel, skel->progs.xdp_minus_delta, mtu);
0124 test_check_mtu_run_xdp(skel, skel->progs.xdp_input_len, mtu);
0125 test_check_mtu_run_xdp(skel, skel->progs.xdp_input_len_exceed, mtu);
0126
0127 cleanup:
0128 test_check_mtu__destroy(skel);
0129 }
0130
0131 static void test_check_mtu_run_tc(struct test_check_mtu *skel,
0132 struct bpf_program *prog,
0133 __u32 mtu_expect)
0134 {
0135 int retval_expect = BPF_OK;
0136 __u32 mtu_result = 0;
0137 char buf[256] = {};
0138 int err, prog_fd = bpf_program__fd(prog);
0139 LIBBPF_OPTS(bpf_test_run_opts, topts,
0140 .data_in = &pkt_v4,
0141 .data_size_in = sizeof(pkt_v4),
0142 .data_out = buf,
0143 .data_size_out = sizeof(buf),
0144 .repeat = 1,
0145 );
0146
0147 err = bpf_prog_test_run_opts(prog_fd, &topts);
0148 ASSERT_OK(err, "test_run");
0149 ASSERT_EQ(topts.retval, retval_expect, "retval");
0150
0151
0152 mtu_result = skel->bss->global_bpf_mtu_tc;
0153 ASSERT_EQ(mtu_result, mtu_expect, "MTU-compare-user");
0154 }
0155
0156
0157 static void test_check_mtu_tc(__u32 mtu, __u32 ifindex)
0158 {
0159 struct test_check_mtu *skel;
0160 int err;
0161
0162 skel = test_check_mtu__open();
0163 if (CHECK(!skel, "skel_open", "failed"))
0164 return;
0165
0166
0167 skel->rodata->GLOBAL_USER_MTU = mtu;
0168 skel->rodata->GLOBAL_USER_IFINDEX = ifindex;
0169
0170 err = test_check_mtu__load(skel);
0171 if (CHECK(err, "skel_load", "failed: %d\n", err))
0172 goto cleanup;
0173
0174 test_check_mtu_run_tc(skel, skel->progs.tc_use_helper, mtu);
0175 test_check_mtu_run_tc(skel, skel->progs.tc_exceed_mtu, mtu);
0176 test_check_mtu_run_tc(skel, skel->progs.tc_exceed_mtu_da, mtu);
0177 test_check_mtu_run_tc(skel, skel->progs.tc_minus_delta, mtu);
0178 test_check_mtu_run_tc(skel, skel->progs.tc_input_len, mtu);
0179 test_check_mtu_run_tc(skel, skel->progs.tc_input_len_exceed, mtu);
0180 cleanup:
0181 test_check_mtu__destroy(skel);
0182 }
0183
0184 void serial_test_check_mtu(void)
0185 {
0186 __u32 mtu_lo;
0187
0188 if (test__start_subtest("bpf_check_mtu XDP-attach"))
0189 test_check_mtu_xdp_attach();
0190
0191 mtu_lo = read_mtu_device_lo();
0192 if (CHECK(mtu_lo < 0, "reading MTU value", "failed (err:%d)", mtu_lo))
0193 return;
0194
0195 if (test__start_subtest("bpf_check_mtu XDP-run"))
0196 test_check_mtu_xdp(mtu_lo, 0);
0197
0198 if (test__start_subtest("bpf_check_mtu XDP-run ifindex-lookup"))
0199 test_check_mtu_xdp(mtu_lo, IFINDEX_LO);
0200
0201 if (test__start_subtest("bpf_check_mtu TC-run"))
0202 test_check_mtu_tc(mtu_lo, 0);
0203
0204 if (test__start_subtest("bpf_check_mtu TC-run ifindex-lookup"))
0205 test_check_mtu_tc(mtu_lo, IFINDEX_LO);
0206 }