|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | + |
| 3 | +static const char *__doc__ = "XDP redirect helper\n" |
| 4 | + " - Allows to populate/query tx_port and redirect_params maps\n"; |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <errno.h> |
| 10 | +#include <getopt.h> |
| 11 | +#include <stdbool.h> |
| 12 | + |
| 13 | +#include <locale.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <time.h> |
| 16 | + |
| 17 | +#include <bpf/bpf.h> |
| 18 | +#include <bpf/libbpf.h> |
| 19 | + |
| 20 | +#include <net/if.h> |
| 21 | +#include <linux/if_ether.h> |
| 22 | +#include <linux/if_link.h> /* depend on kernel-headers installed */ |
| 23 | + |
| 24 | +#include "../common/common_params.h" |
| 25 | +#include "../common/common_user_bpf_xdp.h" |
| 26 | +#include "../common/common_libbpf.h" |
| 27 | + |
| 28 | +#include "../common/xdp_stats_kern_user.h" |
| 29 | + |
| 30 | +static const struct option_wrapper long_options[] = { |
| 31 | + |
| 32 | + {{"help", no_argument, NULL, 'h' }, |
| 33 | + "Show help", false}, |
| 34 | + |
| 35 | + {{"dev", required_argument, NULL, 'd' }, |
| 36 | + "Operate on device <ifname>", "<ifname>", true}, |
| 37 | + |
| 38 | + {{"redirect-dev", required_argument, NULL, 'r' }, |
| 39 | + "Redirect to device <ifname>", "<ifname>", true}, |
| 40 | + |
| 41 | + {{"src-mac", required_argument, NULL, 'L' }, |
| 42 | + "Source MAC address of <dev>", "<mac>", true }, |
| 43 | + |
| 44 | + {{"dest-mac", required_argument, NULL, 'R' }, |
| 45 | + "Destination MAC address of <redirect-dev>", "<mac>", true }, |
| 46 | + |
| 47 | + {{"quiet", no_argument, NULL, 'q' }, |
| 48 | + "Quiet mode (no output)"}, |
| 49 | + |
| 50 | + {{0, 0, NULL, 0 }, NULL, false} |
| 51 | +}; |
| 52 | + |
| 53 | +static int parse_u8(char *str, unsigned char *x) |
| 54 | +{ |
| 55 | + unsigned long z; |
| 56 | + |
| 57 | + z = strtoul(str, 0, 16); |
| 58 | + if (z > 0xff) |
| 59 | + return -1; |
| 60 | + |
| 61 | + if (x) |
| 62 | + *x = z; |
| 63 | + |
| 64 | + return 0; |
| 65 | +} |
| 66 | + |
| 67 | +static int parse_mac(char *str, unsigned char mac[ETH_ALEN]) |
| 68 | +{ |
| 69 | + if (parse_u8(str, &mac[0]) < 0) |
| 70 | + return -1; |
| 71 | + if (parse_u8(str + 3, &mac[1]) < 0) |
| 72 | + return -1; |
| 73 | + if (parse_u8(str + 6, &mac[2]) < 0) |
| 74 | + return -1; |
| 75 | + if (parse_u8(str + 9, &mac[3]) < 0) |
| 76 | + return -1; |
| 77 | + if (parse_u8(str + 12, &mac[4]) < 0) |
| 78 | + return -1; |
| 79 | + if (parse_u8(str + 15, &mac[5]) < 0) |
| 80 | + return -1; |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static int write_iface_params(int map_fd, unsigned char *src, unsigned char *dest) |
| 86 | +{ |
| 87 | + if (bpf_map_update_elem(map_fd, src, dest, 0) < 0) { |
| 88 | + fprintf(stderr, |
| 89 | + "WARN: Failed to update bpf map file: err(%d):%s\n", |
| 90 | + errno, strerror(errno)); |
| 91 | + return -1; |
| 92 | + } |
| 93 | + |
| 94 | + printf("forward: %02x:%02x:%02x:%02x:%02x:%02x -> %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 95 | + src[0], src[1], src[2], src[3], src[4], src[5], |
| 96 | + dest[0], dest[1], dest[2], dest[3], dest[4], dest[5] |
| 97 | + ); |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +#ifndef PATH_MAX |
| 103 | +#define PATH_MAX 4096 |
| 104 | +#endif |
| 105 | + |
| 106 | +const char *pin_basedir = "/sys/fs/bpf"; |
| 107 | + |
| 108 | +int main(int argc, char **argv) |
| 109 | +{ |
| 110 | + int i; |
| 111 | + int len; |
| 112 | + int map_fd; |
| 113 | + bool redirect_map; |
| 114 | + char pin_dir[PATH_MAX]; |
| 115 | + unsigned char src[ETH_ALEN]; |
| 116 | + unsigned char dest[ETH_ALEN]; |
| 117 | + |
| 118 | + struct config cfg = { |
| 119 | + .ifindex = -1, |
| 120 | + .redirect_ifindex = -1, |
| 121 | + }; |
| 122 | + |
| 123 | + /* Cmdline options can change progsec */ |
| 124 | + parse_cmdline_args(argc, argv, long_options, &cfg, __doc__); |
| 125 | + |
| 126 | + redirect_map = (cfg.ifindex > 0) && (cfg.redirect_ifindex > 0); |
| 127 | + |
| 128 | + if (cfg.redirect_ifindex > 0 && cfg.ifindex == -1) { |
| 129 | + fprintf(stderr, "ERR: required option --dev missing\n\n"); |
| 130 | + usage(argv[0], __doc__, long_options, (argc == 1)); |
| 131 | + return EXIT_FAIL_OPTION; |
| 132 | + } |
| 133 | + |
| 134 | + len = snprintf(pin_dir, PATH_MAX, "%s/%s", pin_basedir, cfg.ifname); |
| 135 | + if (len < 0) { |
| 136 | + fprintf(stderr, "ERR: creating pin dirname\n"); |
| 137 | + return EXIT_FAIL_OPTION; |
| 138 | + } |
| 139 | + |
| 140 | + if (parse_mac(cfg.src_mac, src) < 0) { |
| 141 | + fprintf(stderr, "ERR: can't parse mac address %s\n", cfg.src_mac); |
| 142 | + return EXIT_FAIL_OPTION; |
| 143 | + } |
| 144 | + |
| 145 | + if (parse_mac(cfg.dest_mac, dest) < 0) { |
| 146 | + fprintf(stderr, "ERR: can't parse mac address %s\n", cfg.dest_mac); |
| 147 | + return EXIT_FAIL_OPTION; |
| 148 | + } |
| 149 | + |
| 150 | + /* Open the tx_port map corresponding to the cfg.ifname interface */ |
| 151 | + map_fd = open_bpf_map_file(pin_dir, "tx_port", NULL); |
| 152 | + if (map_fd < 0) { |
| 153 | + return EXIT_FAIL_BPF; |
| 154 | + } |
| 155 | + |
| 156 | + printf("map dir: %s\n", pin_dir); |
| 157 | + |
| 158 | + if (redirect_map) { |
| 159 | + /* setup a virtual port for the static redirect */ |
| 160 | + i = 0; |
| 161 | + bpf_map_update_elem(map_fd, &i, &cfg.redirect_ifindex, 0); |
| 162 | + printf("redirect from ifnum=%d to ifnum=%d\n", cfg.ifindex, cfg.redirect_ifindex); |
| 163 | + |
| 164 | + /* Open the redirect_params map */ |
| 165 | + map_fd = open_bpf_map_file(pin_dir, "redirect_params", NULL); |
| 166 | + if (map_fd < 0) { |
| 167 | + return EXIT_FAIL_BPF; |
| 168 | + } |
| 169 | + |
| 170 | + /* Setup the mapping containing MAC addresses */ |
| 171 | + if (write_iface_params(map_fd, src, dest) < 0) { |
| 172 | + fprintf(stderr, "can't write iface params\n"); |
| 173 | + return 1; |
| 174 | + } |
| 175 | + } else { |
| 176 | + /* setup 1-1 mapping for the dynamic router */ |
| 177 | + for (i = 1; i < 256; ++i) |
| 178 | + bpf_map_update_elem(map_fd, &i, &i, 0); |
| 179 | + } |
| 180 | + |
| 181 | + return EXIT_OK; |
| 182 | +} |
0 commit comments