2021-05-19 09:07:46 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2021-08-21 00:20:10 +00:00
|
|
|
static const char *__doc__ =
|
|
|
|
"XDP multi redirect tool, using BPF_MAP_TYPE_DEVMAP and BPF_F_BROADCAST flag for bpf_redirect_map\n"
|
|
|
|
"Usage: xdp_redirect_map_multi <IFINDEX|IFNAME> <IFINDEX|IFNAME> ... <IFINDEX|IFNAME>\n";
|
|
|
|
|
2021-05-19 09:07:46 +00:00
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/if_link.h>
|
|
|
|
#include <assert.h>
|
2021-08-21 00:20:10 +00:00
|
|
|
#include <getopt.h>
|
2021-05-19 09:07:46 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2021-08-21 00:20:10 +00:00
|
|
|
#include <linux/if_ether.h>
|
2021-05-19 09:07:46 +00:00
|
|
|
#include <bpf/bpf.h>
|
|
|
|
#include <bpf/libbpf.h>
|
2021-08-21 00:20:10 +00:00
|
|
|
#include "bpf_util.h"
|
|
|
|
#include "xdp_sample_user.h"
|
|
|
|
#include "xdp_redirect_map_multi.skel.h"
|
2021-05-19 09:07:46 +00:00
|
|
|
|
|
|
|
#define MAX_IFACE_NUM 32
|
|
|
|
static int ifaces[MAX_IFACE_NUM] = {};
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
static int mask = SAMPLE_RX_CNT | SAMPLE_REDIRECT_ERR_MAP_CNT |
|
|
|
|
SAMPLE_EXCEPTION_CNT | SAMPLE_DEVMAP_XMIT_CNT |
|
|
|
|
SAMPLE_DEVMAP_XMIT_CNT_MULTI | SAMPLE_SKIP_HEADING;
|
2021-05-19 09:07:46 +00:00
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
DEFINE_SAMPLE_INIT(xdp_redirect_map_multi);
|
2021-05-19 09:07:46 +00:00
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "skb-mode", no_argument, NULL, 'S' },
|
|
|
|
{ "force", no_argument, NULL, 'F' },
|
|
|
|
{ "load-egress", no_argument, NULL, 'X' },
|
|
|
|
{ "stats", no_argument, NULL, 's' },
|
|
|
|
{ "interval", required_argument, NULL, 'i' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{}
|
|
|
|
};
|
2021-05-19 09:07:46 +00:00
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
static int update_mac_map(struct bpf_map *map)
|
2021-05-19 09:07:46 +00:00
|
|
|
{
|
2021-08-21 00:20:10 +00:00
|
|
|
int mac_map_fd = bpf_map__fd(map);
|
2021-05-19 09:07:46 +00:00
|
|
|
unsigned char mac_addr[6];
|
|
|
|
unsigned int ifindex;
|
2021-08-21 00:20:10 +00:00
|
|
|
int i, ret = -1;
|
2021-05-19 09:07:46 +00:00
|
|
|
|
|
|
|
for (i = 0; ifaces[i] > 0; i++) {
|
|
|
|
ifindex = ifaces[i];
|
|
|
|
|
|
|
|
ret = get_mac_addr(ifindex, mac_addr);
|
|
|
|
if (ret < 0) {
|
2021-08-21 00:20:10 +00:00
|
|
|
fprintf(stderr, "get interface %d mac failed\n",
|
|
|
|
ifindex);
|
2021-05-19 09:07:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bpf_map_update_elem(mac_map_fd, &ifindex, mac_addr, 0);
|
2021-08-21 00:20:10 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to update mac address for ifindex %d\n",
|
|
|
|
ifindex);
|
2021-05-19 09:07:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2021-08-21 00:20:10 +00:00
|
|
|
struct bpf_devmap_val devmap_val = {};
|
|
|
|
struct xdp_redirect_map_multi *skel;
|
|
|
|
struct bpf_program *ingress_prog;
|
|
|
|
bool xdp_devmap_attached = false;
|
|
|
|
struct bpf_map *forward_map;
|
|
|
|
int ret = EXIT_FAIL_OPTION;
|
|
|
|
unsigned long interval = 2;
|
2021-05-19 09:07:46 +00:00
|
|
|
char ifname[IF_NAMESIZE];
|
|
|
|
unsigned int ifindex;
|
2021-08-21 00:20:10 +00:00
|
|
|
bool generic = false;
|
|
|
|
bool force = false;
|
|
|
|
bool tried = false;
|
|
|
|
bool error = true;
|
|
|
|
int i, opt;
|
|
|
|
|
|
|
|
while ((opt = getopt_long(argc, argv, "hSFXi:vs",
|
|
|
|
long_options, NULL)) != -1) {
|
2021-05-19 09:07:46 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'S':
|
2021-08-21 00:20:10 +00:00
|
|
|
generic = true;
|
|
|
|
/* devmap_xmit tracepoint not available */
|
|
|
|
mask &= ~(SAMPLE_DEVMAP_XMIT_CNT |
|
|
|
|
SAMPLE_DEVMAP_XMIT_CNT_MULTI);
|
2021-05-19 09:07:46 +00:00
|
|
|
break;
|
|
|
|
case 'F':
|
2021-08-21 00:20:10 +00:00
|
|
|
force = true;
|
2021-05-19 09:07:46 +00:00
|
|
|
break;
|
|
|
|
case 'X':
|
2021-08-21 00:20:10 +00:00
|
|
|
xdp_devmap_attached = true;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
interval = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
sample_switch_mode();
|
2021-05-19 09:07:46 +00:00
|
|
|
break;
|
2021-08-21 00:20:10 +00:00
|
|
|
case 's':
|
|
|
|
mask |= SAMPLE_REDIRECT_MAP_CNT;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
error = false;
|
2021-05-19 09:07:46 +00:00
|
|
|
default:
|
2021-08-21 00:20:10 +00:00
|
|
|
sample_usage(argv, long_options, __doc__, mask, error);
|
|
|
|
return ret;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
if (argc <= optind + 1) {
|
|
|
|
sample_usage(argv, long_options, __doc__, mask, error);
|
|
|
|
return ret;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
skel = xdp_redirect_map_multi__open();
|
|
|
|
if (!skel) {
|
|
|
|
fprintf(stderr, "Failed to xdp_redirect_map_multi__open: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
ret = EXIT_FAIL_BPF;
|
|
|
|
goto end;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
ret = sample_init_pre_load(skel);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
|
|
|
|
ret = EXIT_FAIL_BPF;
|
|
|
|
goto end_destroy;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = EXIT_FAIL_OPTION;
|
2021-05-19 09:07:46 +00:00
|
|
|
for (i = 0; i < MAX_IFACE_NUM && argv[optind + i]; i++) {
|
|
|
|
ifaces[i] = if_nametoindex(argv[optind + i]);
|
|
|
|
if (!ifaces[i])
|
|
|
|
ifaces[i] = strtoul(argv[optind + i], NULL, 0);
|
|
|
|
if (!if_indextoname(ifaces[i], ifname)) {
|
2021-08-21 00:20:10 +00:00
|
|
|
fprintf(stderr, "Bad interface index or name\n");
|
|
|
|
sample_usage(argv, long_options, __doc__, mask, true);
|
|
|
|
goto end_destroy;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
skel->rodata->from_match[i] = ifaces[i];
|
|
|
|
skel->rodata->to_match[i] = ifaces[i];
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
ret = xdp_redirect_map_multi__load(skel);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to xdp_redirect_map_multi__load: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
ret = EXIT_FAIL_BPF;
|
|
|
|
goto end_destroy;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
if (xdp_devmap_attached) {
|
2021-05-19 09:07:46 +00:00
|
|
|
/* Update mac_map with all egress interfaces' mac addr */
|
2021-08-21 00:20:10 +00:00
|
|
|
if (update_mac_map(skel->maps.mac_map) < 0) {
|
|
|
|
fprintf(stderr, "Updating mac address failed\n");
|
|
|
|
ret = EXIT_FAIL;
|
|
|
|
goto end_destroy;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
2021-08-21 00:20:10 +00:00
|
|
|
}
|
2021-05-19 09:07:46 +00:00
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
ret = sample_init(skel, mask);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
|
|
|
|
ret = EXIT_FAIL;
|
|
|
|
goto end_destroy;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
ingress_prog = skel->progs.xdp_redirect_map_native;
|
|
|
|
forward_map = skel->maps.forward_map_native;
|
2021-05-19 09:07:46 +00:00
|
|
|
|
|
|
|
for (i = 0; ifaces[i] > 0; i++) {
|
|
|
|
ifindex = ifaces[i];
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
ret = EXIT_FAIL_XDP;
|
|
|
|
restart:
|
2021-05-19 09:07:46 +00:00
|
|
|
/* bind prog_fd to each interface */
|
2021-08-21 00:20:10 +00:00
|
|
|
if (sample_install_xdp(ingress_prog, ifindex, generic, force) < 0) {
|
|
|
|
if (generic && !tried) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Trying fallback to sizeof(int) as value_size for devmap in generic mode\n");
|
|
|
|
ingress_prog = skel->progs.xdp_redirect_map_general;
|
|
|
|
forward_map = skel->maps.forward_map_general;
|
|
|
|
tried = true;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
goto end_destroy;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add all the interfaces to forward group and attach
|
2021-08-21 00:20:10 +00:00
|
|
|
* egress devmap program if exist
|
2021-05-19 09:07:46 +00:00
|
|
|
*/
|
|
|
|
devmap_val.ifindex = ifindex;
|
2021-08-21 00:20:10 +00:00
|
|
|
if (xdp_devmap_attached)
|
|
|
|
devmap_val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_devmap_prog);
|
|
|
|
ret = bpf_map_update_elem(bpf_map__fd(forward_map), &ifindex, &devmap_val, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to update devmap value: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
ret = EXIT_FAIL_BPF;
|
|
|
|
goto end_destroy;
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 00:20:10 +00:00
|
|
|
ret = sample_run(interval, NULL, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
|
|
|
|
ret = EXIT_FAIL;
|
|
|
|
goto end_destroy;
|
|
|
|
}
|
|
|
|
ret = EXIT_OK;
|
|
|
|
end_destroy:
|
|
|
|
xdp_redirect_map_multi__destroy(skel);
|
|
|
|
end:
|
|
|
|
sample_exit(ret);
|
2021-05-19 09:07:46 +00:00
|
|
|
}
|