mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-13 14:24:11 +08:00
samples: bpf: Convert xdp_redirect_map to XDP samples helper
Use the libbpf skeleton facility and other utilities provided by XDP samples helper. Since get_mac_addr is already provided by XDP samples helper, we drop it. Also convert to XDP samples helper similar to prior samples to minimize duplication of code. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210821002010.845777-21-memxor@gmail.com
This commit is contained in:
parent
54af769db9
commit
bbe65865aa
@ -39,7 +39,6 @@ tprogs-y += lwt_len_hist
|
|||||||
tprogs-y += xdp_tx_iptunnel
|
tprogs-y += xdp_tx_iptunnel
|
||||||
tprogs-y += test_map_in_map
|
tprogs-y += test_map_in_map
|
||||||
tprogs-y += per_socket_stats_example
|
tprogs-y += per_socket_stats_example
|
||||||
tprogs-y += xdp_redirect_map
|
|
||||||
tprogs-y += xdp_redirect_map_multi
|
tprogs-y += xdp_redirect_map_multi
|
||||||
tprogs-y += xdp_rxq_info
|
tprogs-y += xdp_rxq_info
|
||||||
tprogs-y += syscall_tp
|
tprogs-y += syscall_tp
|
||||||
@ -55,6 +54,7 @@ tprogs-y += ibumad
|
|||||||
tprogs-y += hbm
|
tprogs-y += hbm
|
||||||
|
|
||||||
tprogs-y += xdp_redirect_cpu
|
tprogs-y += xdp_redirect_cpu
|
||||||
|
tprogs-y += xdp_redirect_map
|
||||||
tprogs-y += xdp_redirect
|
tprogs-y += xdp_redirect
|
||||||
tprogs-y += xdp_monitor
|
tprogs-y += xdp_monitor
|
||||||
|
|
||||||
@ -100,7 +100,6 @@ lwt_len_hist-objs := lwt_len_hist_user.o
|
|||||||
xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
|
xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
|
||||||
test_map_in_map-objs := test_map_in_map_user.o
|
test_map_in_map-objs := test_map_in_map_user.o
|
||||||
per_socket_stats_example-objs := cookie_uid_helper_example.o
|
per_socket_stats_example-objs := cookie_uid_helper_example.o
|
||||||
xdp_redirect_map-objs := xdp_redirect_map_user.o
|
|
||||||
xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o
|
xdp_redirect_map_multi-objs := xdp_redirect_map_multi_user.o
|
||||||
xdp_rxq_info-objs := xdp_rxq_info_user.o
|
xdp_rxq_info-objs := xdp_rxq_info_user.o
|
||||||
syscall_tp-objs := syscall_tp_user.o
|
syscall_tp-objs := syscall_tp_user.o
|
||||||
@ -116,6 +115,7 @@ ibumad-objs := ibumad_user.o
|
|||||||
hbm-objs := hbm.o $(CGROUP_HELPERS)
|
hbm-objs := hbm.o $(CGROUP_HELPERS)
|
||||||
|
|
||||||
xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o $(XDP_SAMPLE)
|
xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o $(XDP_SAMPLE)
|
||||||
|
xdp_redirect_map-objs := xdp_redirect_map_user.o $(XDP_SAMPLE)
|
||||||
xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
|
xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
|
||||||
xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
|
xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
|
||||||
|
|
||||||
@ -311,6 +311,7 @@ $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
|
|||||||
$(src)/*.c: verify_target_bpf $(LIBBPF)
|
$(src)/*.c: verify_target_bpf $(LIBBPF)
|
||||||
|
|
||||||
$(obj)/xdp_redirect_cpu_user.o: $(obj)/xdp_redirect_cpu.skel.h
|
$(obj)/xdp_redirect_cpu_user.o: $(obj)/xdp_redirect_cpu.skel.h
|
||||||
|
$(obj)/xdp_redirect_map_user.o: $(obj)/xdp_redirect_map.skel.h
|
||||||
$(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
|
$(obj)/xdp_redirect_user.o: $(obj)/xdp_redirect.skel.h
|
||||||
$(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
|
$(obj)/xdp_monitor_user.o: $(obj)/xdp_monitor.skel.h
|
||||||
|
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0-only
|
// SPDX-License-Identifier: GPL-2.0-only
|
||||||
/* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
|
/* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
|
||||||
*/
|
*/
|
||||||
|
static const char *__doc__ =
|
||||||
|
"XDP redirect tool, using BPF_MAP_TYPE_DEVMAP\n"
|
||||||
|
"Usage: xdp_redirect_map <IFINDEX|IFNAME>_IN <IFINDEX|IFNAME>_OUT\n";
|
||||||
|
|
||||||
#include <linux/bpf.h>
|
#include <linux/bpf.h>
|
||||||
#include <linux/if_link.h>
|
#include <linux/if_link.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@ -13,165 +17,83 @@
|
|||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <sys/resource.h>
|
#include <getopt.h>
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
|
|
||||||
#include "bpf_util.h"
|
|
||||||
#include <bpf/bpf.h>
|
#include <bpf/bpf.h>
|
||||||
#include <bpf/libbpf.h>
|
#include <bpf/libbpf.h>
|
||||||
|
#include "bpf_util.h"
|
||||||
|
#include "xdp_sample_user.h"
|
||||||
|
#include "xdp_redirect_map.skel.h"
|
||||||
|
|
||||||
static int ifindex_in;
|
static int mask = SAMPLE_RX_CNT | SAMPLE_REDIRECT_ERR_MAP_CNT |
|
||||||
static int ifindex_out;
|
SAMPLE_EXCEPTION_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
|
||||||
static bool ifindex_out_xdp_dummy_attached = true;
|
|
||||||
static bool xdp_devmap_attached;
|
|
||||||
static __u32 prog_id;
|
|
||||||
static __u32 dummy_prog_id;
|
|
||||||
|
|
||||||
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
|
DEFINE_SAMPLE_INIT(xdp_redirect_map);
|
||||||
static int rxcnt_map_fd;
|
|
||||||
|
|
||||||
static void int_exit(int sig)
|
static const struct option long_options[] = {
|
||||||
{
|
{ "help", no_argument, NULL, 'h' },
|
||||||
__u32 curr_prog_id = 0;
|
{ "skb-mode", no_argument, NULL, 'S' },
|
||||||
|
{ "force", no_argument, NULL, 'F' },
|
||||||
if (bpf_get_link_xdp_id(ifindex_in, &curr_prog_id, xdp_flags)) {
|
{ "load-egress", no_argument, NULL, 'X' },
|
||||||
printf("bpf_get_link_xdp_id failed\n");
|
{ "stats", no_argument, NULL, 's' },
|
||||||
exit(1);
|
{ "interval", required_argument, NULL, 'i' },
|
||||||
}
|
{ "verbose", no_argument, NULL, 'v' },
|
||||||
if (prog_id == curr_prog_id)
|
{}
|
||||||
bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
|
};
|
||||||
else if (!curr_prog_id)
|
|
||||||
printf("couldn't find a prog id on iface IN\n");
|
|
||||||
else
|
|
||||||
printf("program on iface IN changed, not removing\n");
|
|
||||||
|
|
||||||
if (ifindex_out_xdp_dummy_attached) {
|
|
||||||
curr_prog_id = 0;
|
|
||||||
if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id,
|
|
||||||
xdp_flags)) {
|
|
||||||
printf("bpf_get_link_xdp_id failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (dummy_prog_id == curr_prog_id)
|
|
||||||
bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
|
|
||||||
else if (!curr_prog_id)
|
|
||||||
printf("couldn't find a prog id on iface OUT\n");
|
|
||||||
else
|
|
||||||
printf("program on iface OUT changed, not removing\n");
|
|
||||||
}
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void poll_stats(int interval, int ifindex)
|
|
||||||
{
|
|
||||||
unsigned int nr_cpus = bpf_num_possible_cpus();
|
|
||||||
__u64 values[nr_cpus], prev[nr_cpus];
|
|
||||||
|
|
||||||
memset(prev, 0, sizeof(prev));
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
__u64 sum = 0;
|
|
||||||
__u32 key = 0;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
sleep(interval);
|
|
||||||
assert(bpf_map_lookup_elem(rxcnt_map_fd, &key, values) == 0);
|
|
||||||
for (i = 0; i < nr_cpus; i++)
|
|
||||||
sum += (values[i] - prev[i]);
|
|
||||||
if (sum)
|
|
||||||
printf("ifindex %i: %10llu pkt/s\n",
|
|
||||||
ifindex, sum / interval);
|
|
||||||
memcpy(prev, values, sizeof(values));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int get_mac_addr(unsigned int ifindex_out, void *mac_addr)
|
|
||||||
{
|
|
||||||
char ifname[IF_NAMESIZE];
|
|
||||||
struct ifreq ifr;
|
|
||||||
int fd, ret = -1;
|
|
||||||
|
|
||||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
if (fd < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
if (!if_indextoname(ifindex_out, ifname))
|
|
||||||
goto err_out;
|
|
||||||
|
|
||||||
strcpy(ifr.ifr_name, ifname);
|
|
||||||
|
|
||||||
if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0)
|
|
||||||
goto err_out;
|
|
||||||
|
|
||||||
memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, 6 * sizeof(char));
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
err_out:
|
|
||||||
close(fd);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void usage(const char *prog)
|
|
||||||
{
|
|
||||||
fprintf(stderr,
|
|
||||||
"usage: %s [OPTS] <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n\n"
|
|
||||||
"OPTS:\n"
|
|
||||||
" -S use skb-mode\n"
|
|
||||||
" -N enforce native mode\n"
|
|
||||||
" -F force loading prog\n"
|
|
||||||
" -X load xdp program on egress\n",
|
|
||||||
prog);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct bpf_prog_load_attr prog_load_attr = {
|
struct bpf_devmap_val devmap_val = {};
|
||||||
.prog_type = BPF_PROG_TYPE_UNSPEC,
|
bool xdp_devmap_attached = false;
|
||||||
};
|
struct xdp_redirect_map *skel;
|
||||||
struct bpf_program *prog, *dummy_prog, *devmap_prog;
|
char str[2 * IF_NAMESIZE + 1];
|
||||||
int prog_fd, dummy_prog_fd, devmap_prog_fd = 0;
|
char ifname_out[IF_NAMESIZE];
|
||||||
int tx_port_map_fd, tx_mac_map_fd;
|
struct bpf_map *tx_port_map;
|
||||||
struct bpf_devmap_val devmap_val;
|
char ifname_in[IF_NAMESIZE];
|
||||||
struct bpf_prog_info info = {};
|
int ifindex_in, ifindex_out;
|
||||||
__u32 info_len = sizeof(info);
|
unsigned long interval = 2;
|
||||||
const char *optstr = "FSNX";
|
int ret = EXIT_FAIL_OPTION;
|
||||||
struct bpf_object *obj;
|
struct bpf_program *prog;
|
||||||
int ret, opt, key = 0;
|
bool generic = false;
|
||||||
char filename[256];
|
bool force = false;
|
||||||
|
bool tried = false;
|
||||||
|
bool error = true;
|
||||||
|
int opt, key = 0;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, optstr)) != -1) {
|
while ((opt = getopt_long(argc, argv, "hSFXi:vs",
|
||||||
|
long_options, NULL)) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'S':
|
case 'S':
|
||||||
xdp_flags |= XDP_FLAGS_SKB_MODE;
|
generic = true;
|
||||||
break;
|
/* devmap_xmit tracepoint not available */
|
||||||
case 'N':
|
mask &= ~(SAMPLE_DEVMAP_XMIT_CNT |
|
||||||
/* default, set below */
|
SAMPLE_DEVMAP_XMIT_CNT_MULTI);
|
||||||
break;
|
break;
|
||||||
case 'F':
|
case 'F':
|
||||||
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
|
force = true;
|
||||||
break;
|
break;
|
||||||
case 'X':
|
case 'X':
|
||||||
xdp_devmap_attached = true;
|
xdp_devmap_attached = true;
|
||||||
break;
|
break;
|
||||||
|
case 'i':
|
||||||
|
interval = strtoul(optarg, NULL, 0);
|
||||||
|
break;
|
||||||
|
case 'v':
|
||||||
|
sample_switch_mode();
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
mask |= SAMPLE_REDIRECT_MAP_CNT;
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
error = false;
|
||||||
default:
|
default:
|
||||||
usage(basename(argv[0]));
|
sample_usage(argv, long_options, __doc__, mask, error);
|
||||||
return 1;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) {
|
if (argc <= optind + 1) {
|
||||||
xdp_flags |= XDP_FLAGS_DRV_MODE;
|
sample_usage(argv, long_options, __doc__, mask, true);
|
||||||
} else if (xdp_devmap_attached) {
|
goto end;
|
||||||
printf("Load xdp program on egress with SKB mode not supported yet\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (optind == argc) {
|
|
||||||
printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ifindex_in = if_nametoindex(argv[optind]);
|
ifindex_in = if_nametoindex(argv[optind]);
|
||||||
@ -182,107 +104,116 @@ int main(int argc, char **argv)
|
|||||||
if (!ifindex_out)
|
if (!ifindex_out)
|
||||||
ifindex_out = strtoul(argv[optind + 1], NULL, 0);
|
ifindex_out = strtoul(argv[optind + 1], NULL, 0);
|
||||||
|
|
||||||
printf("input: %d output: %d\n", ifindex_in, ifindex_out);
|
if (!ifindex_in || !ifindex_out) {
|
||||||
|
fprintf(stderr, "Bad interface index or name\n");
|
||||||
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
sample_usage(argv, long_options, __doc__, mask, true);
|
||||||
prog_load_attr.file = filename;
|
goto end;
|
||||||
|
|
||||||
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
if (xdp_flags & XDP_FLAGS_SKB_MODE) {
|
|
||||||
prog = bpf_object__find_program_by_name(obj, "xdp_redirect_map_general");
|
|
||||||
tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port_general");
|
|
||||||
} else {
|
|
||||||
prog = bpf_object__find_program_by_name(obj, "xdp_redirect_map_native");
|
|
||||||
tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port_native");
|
|
||||||
}
|
|
||||||
dummy_prog = bpf_object__find_program_by_name(obj, "xdp_redirect_dummy_prog");
|
|
||||||
if (!prog || dummy_prog < 0 || tx_port_map_fd < 0) {
|
|
||||||
printf("finding prog/dummy_prog/tx_port_map in obj file failed\n");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
prog_fd = bpf_program__fd(prog);
|
|
||||||
dummy_prog_fd = bpf_program__fd(dummy_prog);
|
|
||||||
if (prog_fd < 0 || dummy_prog_fd < 0 || tx_port_map_fd < 0) {
|
|
||||||
printf("bpf_prog_load_xattr: %s\n", strerror(errno));
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tx_mac_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_mac");
|
skel = xdp_redirect_map__open();
|
||||||
rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
|
if (!skel) {
|
||||||
if (tx_mac_map_fd < 0 || rxcnt_map_fd < 0) {
|
fprintf(stderr, "Failed to xdp_redirect_map__open: %s\n",
|
||||||
printf("bpf_object__find_map_fd_by_name failed\n");
|
strerror(errno));
|
||||||
return 1;
|
ret = EXIT_FAIL_BPF;
|
||||||
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bpf_set_link_xdp_fd(ifindex_in, prog_fd, xdp_flags) < 0) {
|
ret = sample_init_pre_load(skel);
|
||||||
printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
|
if (ret < 0) {
|
||||||
return 1;
|
fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
|
||||||
|
ret = EXIT_FAIL_BPF;
|
||||||
|
goto end_destroy;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
|
|
||||||
if (ret) {
|
|
||||||
printf("can't get prog info - %s\n", strerror(errno));
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
prog_id = info.id;
|
|
||||||
|
|
||||||
/* Loading dummy XDP prog on out-device */
|
|
||||||
if (bpf_set_link_xdp_fd(ifindex_out, dummy_prog_fd,
|
|
||||||
(xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
|
|
||||||
printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
|
|
||||||
ifindex_out_xdp_dummy_attached = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&info, 0, sizeof(info));
|
|
||||||
ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len);
|
|
||||||
if (ret) {
|
|
||||||
printf("can't get prog info - %s\n", strerror(errno));
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
dummy_prog_id = info.id;
|
|
||||||
|
|
||||||
/* Load 2nd xdp prog on egress. */
|
/* Load 2nd xdp prog on egress. */
|
||||||
if (xdp_devmap_attached) {
|
if (xdp_devmap_attached) {
|
||||||
unsigned char mac_addr[6];
|
ret = get_mac_addr(ifindex_out, skel->rodata->tx_mac_addr);
|
||||||
|
if (ret < 0) {
|
||||||
devmap_prog = bpf_object__find_program_by_name(obj, "xdp_redirect_map_egress");
|
fprintf(stderr, "Failed to get interface %d mac address: %s\n",
|
||||||
if (!devmap_prog) {
|
ifindex_out, strerror(-ret));
|
||||||
printf("finding devmap_prog in obj file failed\n");
|
ret = EXIT_FAIL;
|
||||||
goto out;
|
goto end_destroy;
|
||||||
}
|
|
||||||
devmap_prog_fd = bpf_program__fd(devmap_prog);
|
|
||||||
if (devmap_prog_fd < 0) {
|
|
||||||
printf("finding devmap_prog fd failed\n");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (get_mac_addr(ifindex_out, mac_addr) < 0) {
|
|
||||||
printf("get interface %d mac failed\n", ifindex_out);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bpf_map_update_elem(tx_mac_map_fd, &key, mac_addr, 0);
|
|
||||||
if (ret) {
|
|
||||||
perror("bpf_update_elem tx_mac_map_fd");
|
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
signal(SIGINT, int_exit);
|
skel->rodata->from_match[0] = ifindex_in;
|
||||||
signal(SIGTERM, int_exit);
|
skel->rodata->to_match[0] = ifindex_out;
|
||||||
|
|
||||||
|
ret = xdp_redirect_map__load(skel);
|
||||||
|
if (ret < 0) {
|
||||||
|
fprintf(stderr, "Failed to xdp_redirect_map__load: %s\n",
|
||||||
|
strerror(errno));
|
||||||
|
ret = EXIT_FAIL_BPF;
|
||||||
|
goto end_destroy;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = sample_init(skel, mask);
|
||||||
|
if (ret < 0) {
|
||||||
|
fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
|
||||||
|
ret = EXIT_FAIL;
|
||||||
|
goto end_destroy;
|
||||||
|
}
|
||||||
|
|
||||||
|
prog = skel->progs.xdp_redirect_map_native;
|
||||||
|
tx_port_map = skel->maps.tx_port_native;
|
||||||
|
restart:
|
||||||
|
if (sample_install_xdp(prog, ifindex_in, generic, force) < 0) {
|
||||||
|
/* First try with struct bpf_devmap_val as value for generic
|
||||||
|
* mode, then fallback to sizeof(int) for older kernels.
|
||||||
|
*/
|
||||||
|
fprintf(stderr,
|
||||||
|
"Trying fallback to sizeof(int) as value_size for devmap in generic mode\n");
|
||||||
|
if (generic && !tried) {
|
||||||
|
prog = skel->progs.xdp_redirect_map_general;
|
||||||
|
tx_port_map = skel->maps.tx_port_general;
|
||||||
|
tried = true;
|
||||||
|
goto restart;
|
||||||
|
}
|
||||||
|
ret = EXIT_FAIL_XDP;
|
||||||
|
goto end_destroy;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Loading dummy XDP prog on out-device */
|
||||||
|
sample_install_xdp(skel->progs.xdp_redirect_dummy_prog, ifindex_out, generic, force);
|
||||||
|
|
||||||
devmap_val.ifindex = ifindex_out;
|
devmap_val.ifindex = ifindex_out;
|
||||||
devmap_val.bpf_prog.fd = devmap_prog_fd;
|
if (xdp_devmap_attached)
|
||||||
ret = bpf_map_update_elem(tx_port_map_fd, &key, &devmap_val, 0);
|
devmap_val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_redirect_map_egress);
|
||||||
if (ret) {
|
ret = bpf_map_update_elem(bpf_map__fd(tx_port_map), &key, &devmap_val, 0);
|
||||||
perror("bpf_update_elem");
|
if (ret < 0) {
|
||||||
goto out;
|
fprintf(stderr, "Failed to update devmap value: %s\n",
|
||||||
|
strerror(errno));
|
||||||
|
ret = EXIT_FAIL_BPF;
|
||||||
|
goto end_destroy;
|
||||||
}
|
}
|
||||||
|
|
||||||
poll_stats(2, ifindex_out);
|
ret = EXIT_FAIL;
|
||||||
|
if (!if_indextoname(ifindex_in, ifname_in)) {
|
||||||
|
fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_in,
|
||||||
|
strerror(errno));
|
||||||
|
goto end_destroy;
|
||||||
|
}
|
||||||
|
|
||||||
out:
|
if (!if_indextoname(ifindex_out, ifname_out)) {
|
||||||
return 0;
|
fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_out,
|
||||||
|
strerror(errno));
|
||||||
|
goto end_destroy;
|
||||||
|
}
|
||||||
|
|
||||||
|
safe_strncpy(str, get_driver_name(ifindex_in), sizeof(str));
|
||||||
|
printf("Redirecting from %s (ifindex %d; driver %s) to %s (ifindex %d; driver %s)\n",
|
||||||
|
ifname_in, ifindex_in, str, ifname_out, ifindex_out, get_driver_name(ifindex_out));
|
||||||
|
snprintf(str, sizeof(str), "%s->%s", ifname_in, ifname_out);
|
||||||
|
|
||||||
|
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__destroy(skel);
|
||||||
|
end:
|
||||||
|
sample_exit(ret);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user