1*7889a9feSDavid Wang #include <bpf/libbpf.h>
2*7889a9feSDavid Wang #include <linux/if_link.h>
3*7889a9feSDavid Wang #include <net/if.h>
4*7889a9feSDavid Wang #include <string.h>
5*7889a9feSDavid Wang #include <bpf/bpf.h>
6*7889a9feSDavid Wang #include "l3lb.h"
7*7889a9feSDavid Wang
8*7889a9feSDavid Wang
main(int argc,char ** argv)9*7889a9feSDavid Wang int main(int argc, char **argv) {
10*7889a9feSDavid Wang __u32 curr_prog_fd=0;
11*7889a9feSDavid Wang int prog_fd;
12*7889a9feSDavid Wang if (argc != 4) {
13*7889a9feSDavid Wang printf("Usage: %s [in_inf] [out_inf] [tx_addr]\n", argv[0]);
14*7889a9feSDavid Wang return 1;
15*7889a9feSDavid Wang }
16*7889a9feSDavid Wang int if1 = if_nametoindex(argv[1]);
17*7889a9feSDavid Wang if (!if1) { perror("Fail to get inf index"); return 1; }
18*7889a9feSDavid Wang if (bpf_get_link_xdp_id(if1, &curr_prog_fd, XDP_FLAGS_UPDATE_IF_NOEXIST)) { perror("bpf_get_link_xdp_id failed\n"); return 1; }
19*7889a9feSDavid Wang if (curr_prog_fd) {
20*7889a9feSDavid Wang printf("xdp prog id(%d) already linked to %s, removing it now..\n", curr_prog_fd, argv[1]);
21*7889a9feSDavid Wang bpf_set_link_xdp_fd(if1, -1, XDP_FLAGS_UPDATE_IF_NOEXIST);
22*7889a9feSDavid Wang }
23*7889a9feSDavid Wang int if2 = if_nametoindex(argv[2]);
24*7889a9feSDavid Wang if (!if2) { perror("Fail to get inf index"); return 1; }
25*7889a9feSDavid Wang if (bpf_get_link_xdp_id(if2, &curr_prog_fd, XDP_FLAGS_UPDATE_IF_NOEXIST)) { perror("bpf_get_link_xdp_id failed\n"); return 1; }
26*7889a9feSDavid Wang if (curr_prog_fd) {
27*7889a9feSDavid Wang printf("xdp prog id(%d) already linked to %s, removing it now..\n", curr_prog_fd, argv[2]);
28*7889a9feSDavid Wang bpf_set_link_xdp_fd(if2, -1, XDP_FLAGS_UPDATE_IF_NOEXIST);
29*7889a9feSDavid Wang }
30*7889a9feSDavid Wang unsigned int tx_addr = parse_ip(argv[3]);
31*7889a9feSDavid Wang if (tx_addr==0) { printf("invalid tx ip address %s\n", argv[3]); return 1; }
32*7889a9feSDavid Wang // load bpf object
33*7889a9feSDavid Wang struct bpf_prog_load_attr prog_load_attr = {
34*7889a9feSDavid Wang .prog_type = BPF_PROG_TYPE_XDP,
35*7889a9feSDavid Wang .file = "./l3lb_kern.o",
36*7889a9feSDavid Wang };
37*7889a9feSDavid Wang struct bpf_object *obj;
38*7889a9feSDavid Wang if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd)) {
39*7889a9feSDavid Wang perror("Fail to load bpf object");
40*7889a9feSDavid Wang return 1;
41*7889a9feSDavid Wang }
42*7889a9feSDavid Wang int map_fd = bpf_object__find_map_fd_by_name(obj, "tx_addr");
43*7889a9feSDavid Wang if (map_fd<0) { perror("Fail to locate tx addr map"); return 1; }
44*7889a9feSDavid Wang int key = 0;
45*7889a9feSDavid Wang int ret = bpf_map_update_elem(map_fd, &key, &tx_addr, 0);
46*7889a9feSDavid Wang if (ret) { perror("Fail to update tx addr"); return 1; }
47*7889a9feSDavid Wang
48*7889a9feSDavid Wang if (bpf_set_link_xdp_fd(if1, prog_fd, XDP_FLAGS_UPDATE_IF_NOEXIST) < 0) {
49*7889a9feSDavid Wang printf("fail to link if1\n"); return 1;
50*7889a9feSDavid Wang }
51*7889a9feSDavid Wang if (bpf_set_link_xdp_fd(if2, prog_fd, XDP_FLAGS_UPDATE_IF_NOEXIST) < 0) {
52*7889a9feSDavid Wang printf("fail to link if2\n"); return 1;
53*7889a9feSDavid Wang }
54*7889a9feSDavid Wang return 0;
55*7889a9feSDavid Wang }
56