linux/samples/bpf/xdp_redirect_map_user.c
<<
>>
Prefs
   1/* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
   2 *
   3 * This program is free software; you can redistribute it and/or
   4 * modify it under the terms of version 2 of the GNU General Public
   5 * License as published by the Free Software Foundation.
   6 *
   7 * This program is distributed in the hope that it will be useful, but
   8 * WITHOUT ANY WARRANTY; without even the implied warranty of
   9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10 * General Public License for more details.
  11 */
  12#include <linux/bpf.h>
  13#include <linux/if_link.h>
  14#include <assert.h>
  15#include <errno.h>
  16#include <signal.h>
  17#include <stdio.h>
  18#include <stdlib.h>
  19#include <stdbool.h>
  20#include <string.h>
  21#include <unistd.h>
  22#include <libgen.h>
  23#include <sys/resource.h>
  24
  25#include "bpf_load.h"
  26#include "bpf_util.h"
  27#include "libbpf.h"
  28
  29static int ifindex_in;
  30static int ifindex_out;
  31static bool ifindex_out_xdp_dummy_attached = true;
  32
  33static __u32 xdp_flags;
  34
  35static void int_exit(int sig)
  36{
  37        bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
  38        if (ifindex_out_xdp_dummy_attached)
  39                bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
  40        exit(0);
  41}
  42
  43static void poll_stats(int interval, int ifindex)
  44{
  45        unsigned int nr_cpus = bpf_num_possible_cpus();
  46        __u64 values[nr_cpus], prev[nr_cpus];
  47
  48        memset(prev, 0, sizeof(prev));
  49
  50        while (1) {
  51                __u64 sum = 0;
  52                __u32 key = 0;
  53                int i;
  54
  55                sleep(interval);
  56                assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0);
  57                for (i = 0; i < nr_cpus; i++)
  58                        sum += (values[i] - prev[i]);
  59                if (sum)
  60                        printf("ifindex %i: %10llu pkt/s\n",
  61                               ifindex, sum / interval);
  62                memcpy(prev, values, sizeof(values));
  63        }
  64}
  65
  66static void usage(const char *prog)
  67{
  68        fprintf(stderr,
  69                "usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
  70                "OPTS:\n"
  71                "    -S    use skb-mode\n"
  72                "    -N    enforce native mode\n",
  73                prog);
  74}
  75
  76int main(int argc, char **argv)
  77{
  78        struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  79        const char *optstr = "SN";
  80        char filename[256];
  81        int ret, opt, key = 0;
  82
  83        while ((opt = getopt(argc, argv, optstr)) != -1) {
  84                switch (opt) {
  85                case 'S':
  86                        xdp_flags |= XDP_FLAGS_SKB_MODE;
  87                        break;
  88                case 'N':
  89                        xdp_flags |= XDP_FLAGS_DRV_MODE;
  90                        break;
  91                default:
  92                        usage(basename(argv[0]));
  93                        return 1;
  94                }
  95        }
  96
  97        if (optind == argc) {
  98                printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
  99                return 1;
 100        }
 101
 102        if (setrlimit(RLIMIT_MEMLOCK, &r)) {
 103                perror("setrlimit(RLIMIT_MEMLOCK)");
 104                return 1;
 105        }
 106
 107        ifindex_in = strtoul(argv[optind], NULL, 0);
 108        ifindex_out = strtoul(argv[optind + 1], NULL, 0);
 109        printf("input: %d output: %d\n", ifindex_in, ifindex_out);
 110
 111        snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 112
 113        if (load_bpf_file(filename)) {
 114                printf("%s", bpf_log_buf);
 115                return 1;
 116        }
 117
 118        if (!prog_fd[0]) {
 119                printf("load_bpf_file: %s\n", strerror(errno));
 120                return 1;
 121        }
 122
 123        if (bpf_set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
 124                printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
 125                return 1;
 126        }
 127
 128        /* Loading dummy XDP prog on out-device */
 129        if (bpf_set_link_xdp_fd(ifindex_out, prog_fd[1],
 130                            (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
 131                printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
 132                ifindex_out_xdp_dummy_attached = false;
 133        }
 134
 135        signal(SIGINT, int_exit);
 136        signal(SIGTERM, int_exit);
 137
 138        printf("map[0] (vports) = %i, map[1] (map) = %i, map[2] (count) = %i\n",
 139                map_fd[0], map_fd[1], map_fd[2]);
 140
 141        /* populate virtual to physical port map */
 142        ret = bpf_map_update_elem(map_fd[0], &key, &ifindex_out, 0);
 143        if (ret) {
 144                perror("bpf_update_elem");
 145                goto out;
 146        }
 147
 148        poll_stats(2, ifindex_out);
 149
 150out:
 151        return 0;
 152}
 153