linux/tools/testing/selftests/bpf/prog_tests/xdp.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2#include <test_progs.h>
   3
   4void test_xdp(void)
   5{
   6        struct vip key4 = {.protocol = 6, .family = AF_INET};
   7        struct vip key6 = {.protocol = 6, .family = AF_INET6};
   8        struct iptnl_info value4 = {.family = AF_INET};
   9        struct iptnl_info value6 = {.family = AF_INET6};
  10        const char *file = "./test_xdp.o";
  11        struct bpf_object *obj;
  12        char buf[128];
  13        struct ipv6hdr *iph6 = (void *)buf + sizeof(struct ethhdr);
  14        struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
  15        __u32 duration, retval, size;
  16        int err, prog_fd, map_fd;
  17
  18        err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
  19        if (err) {
  20                error_cnt++;
  21                return;
  22        }
  23
  24        map_fd = bpf_find_map(__func__, obj, "vip2tnl");
  25        if (map_fd < 0)
  26                goto out;
  27        bpf_map_update_elem(map_fd, &key4, &value4, 0);
  28        bpf_map_update_elem(map_fd, &key6, &value6, 0);
  29
  30        err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
  31                                buf, &size, &retval, &duration);
  32
  33        CHECK(err || retval != XDP_TX || size != 74 ||
  34              iph->protocol != IPPROTO_IPIP, "ipv4",
  35              "err %d errno %d retval %d size %d\n",
  36              err, errno, retval, size);
  37
  38        err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
  39                                buf, &size, &retval, &duration);
  40        CHECK(err || retval != XDP_TX || size != 114 ||
  41              iph6->nexthdr != IPPROTO_IPV6, "ipv6",
  42              "err %d errno %d retval %d size %d\n",
  43              err, errno, retval, size);
  44out:
  45        bpf_object__close(obj);
  46}
  47