Open dingrui37 opened 2 years ago
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
static const char *pin_basedir = "/sys/fs/bpf";
int main(int argc, char **argv) {
int prog_fd, ifindex;
char filename[256];
if (argc != 3) {
fprintf(stderr, "Usage: %s --dev <ifname>\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "--dev") != 0) {
fprintf(stderr, "Usage: %s --dev <ifname>\n", argv[0]);
return 1;
}
ifindex = if_nametoindex(argv[2]);
if (!ifindex) {
perror("if_nametoindex");
return 1;
}
snprintf(filename, sizeof(filename), "%s/%s", pin_basedir, "xdp_pass_kern.o");
prog_fd = bpf_prog_load(filename, BPF_PROG_TYPE_XDP, NULL, NULL);
if (prog_fd < 0) {
fprintf(stderr, "bpf_prog_load: %s\n", strerror(errno));
return 1;
}
if (bpf_set_link_xdp_fd(ifindex, prog_fd, XDP_FLAGS_SKB_MODE) < 0) {
fprintf(stderr, "link set xdp fd failed: %s\n", strerror(errno));
return 1;
}
printf("Success: Loading XDP prog name:xdp_prog_simple on device:%s(ifindex:%d)\n", argv[2], ifindex);
return 0;
}