On Tue, Dec 22, 2020 at 8:23 AM Jesper Dangaard Brouer <brouer@xxxxxxxxxx> wrote: > > Adding selftest for BPF-helper bpf_check_mtu(). Making sure > it can be used from both XDP and TC. > > V10: > - Remove errno non-zero test in CHECK_ATTR() > - Addresse comments from Andrii Nakryiko > > Signed-off-by: Jesper Dangaard Brouer <brouer@xxxxxxxxxx> > --- Looks good, few minor nits below, feel free to address if you end up with another revision. Acked-by: Andrii Nakryiko <andrii@xxxxxxxxxx> > tools/testing/selftests/bpf/prog_tests/check_mtu.c | 218 ++++++++++++++++++++ > tools/testing/selftests/bpf/progs/test_check_mtu.c | 199 ++++++++++++++++++ > 2 files changed, 417 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/check_mtu.c > create mode 100644 tools/testing/selftests/bpf/progs/test_check_mtu.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/check_mtu.c b/tools/testing/selftests/bpf/prog_tests/check_mtu.c > new file mode 100644 > index 000000000000..63f01c9e08d8 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/check_mtu.c > @@ -0,0 +1,218 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2020 Jesper Dangaard Brouer */ > + > +#include <linux/if_link.h> /* before test_progs.h, avoid bpf_util.h redefines */ > + > +#include <test_progs.h> > +#include "test_check_mtu.skel.h" > +#include <network_helpers.h> bit: test_progs and network_helpers should be included with "", not <> > + > +#include <stdlib.h> > +#include <inttypes.h> > + > +#define IFINDEX_LO 1 > + > +static __u32 duration; /* Hint: needed for CHECK macro */ > + > +static int read_mtu_device_lo(void) > +{ > + const char *filename = "/sys/class/net/lo/mtu"; > + char buf[11] = {}; > + int value; > + int fd; > + > + fd = open(filename, 0, O_RDONLY); > + if (fd == -1) > + return -1; > + > + if (read(fd, buf, sizeof(buf)) == -1) { > + close(fd); > + return -2; > + } > + close(fd); nit: imo, simpler to write: err = read(...); close(fd); if (err == -1) return -2; This way you don't need to close twice. But it's very minor. > + > + value = strtoimax(buf, NULL, 10); > + if (errno == ERANGE) > + return -3; > + > + return value; > +} > + [...] > + CHECK(link_info.type != BPF_LINK_TYPE_XDP, "link_type", > + "got %u != exp %u\n", link_info.type, BPF_LINK_TYPE_XDP); > + CHECK(link_info.xdp.ifindex != IFINDEX_LO, "link_ifindex", > + "got %u != exp %u\n", link_info.xdp.ifindex, IFINDEX_LO); > + > + err = bpf_link__detach(link); > + CHECK(err, "link_detach", "failed %d\n", err); > + unless you explicitly want to test this force-detach, destroying the link (through destroying skeleton) would detach the program just fine. > +out: > + test_check_mtu__destroy(skel); > +} > + [...] > + > +SEC("xdp") > +int xdp_exceed_mtu(struct xdp_md *ctx) > +{ > + void *data_end = (void *)(long)ctx->data_end; > + void *data = (void *)(long)ctx->data; > + __u32 ifindex = GLOBAL_USER_IFINDEX; > + __u32 data_len = data_end - data; > + int retval = XDP_ABORTED; /* Fail */ > + __u32 mtu_len = 0; > + nit: unnecessary empty line inside variable declaration block > + int delta; > + int err; > + > + /* Exceed MTU with 1 via delta adjust */ > + delta = GLOBAL_USER_MTU - (data_len - ETH_HLEN) + 1; > + > + err = bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0); > + if (err) { > + retval = XDP_PASS; /* Success in exceeding MTU check */ > + if (err != BPF_MTU_CHK_RET_FRAG_NEEDED) > + retval = XDP_DROP; > + } > + > + global_bpf_mtu_xdp = mtu_len; > + return retval; > +} > + [...]