-
Notifications
You must be signed in to change notification settings - Fork 6
/
xdp_prog_load.c
96 lines (77 loc) · 2.38 KB
/
xdp_prog_load.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (C) 2019
* Authors: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation version 2.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/if_link.h>
#include "xdp_prog_load.h"
#include <stdlib.h>
#include <libbpf.h>
#include <bpf.h>
#include "xdp_sock.h"
#include <errno.h>
#include <signal.h>
#include "plget_args.h"
const char *xdp_file_name = "xsock_dispatch.o";
#define XDP_FLAGS_DRV_MODE (1U << 2)
void xdp_unload_prog(void)
{
if (!(plget->flags & PLF_PROG_LOADED))
return;
if (bpf_set_link_xdp_fd(plget->ifidx, -1, XDP_FLAGS_DRV_MODE))
perror("link unset xdp prog failed");
}
static void sig_exit(int sig)
{
if (plget->pkt_type == PKT_XDP)
xdp_unload_prog();
printf(" exit, prog unloaded\n");
exit(EXIT_SUCCESS);
}
int xdp_load_prog(void)
{
struct bpf_prog_load_attr prog_attr = {
.prog_type = BPF_PROG_TYPE_XDP,
};
int qidconf_map, xsks_map;
struct bpf_object *obj;
struct bpf_map *map;
int prog_fd, key = 0;
int ret;
if (plget->mod == TX_LAT)
return 0;
prog_attr.file = xdp_file_name;
signal(SIGINT, sig_exit);
signal(SIGTERM, sig_exit);
signal(SIGABRT, sig_exit);
plget->flags |= PLF_PROG_LOADED;
if (bpf_prog_load_xattr(&prog_attr, &obj, &prog_fd))
return perror("no program found"), -errno;
if (prog_fd < 0)
return perror("no program found"), -errno;
map = bpf_object__find_map_by_name(obj, "qidconf_map");
qidconf_map = bpf_map__fd(map);
if (qidconf_map < 0)
return perror("no qidconf map found"), -errno;
map = bpf_object__find_map_by_name(obj, "xsks_map");
xsks_map = bpf_map__fd(map);
if (xsks_map < 0)
return perror("no xsks map found"), -errno;
if (bpf_set_link_xdp_fd(plget->ifidx, prog_fd, XDP_FLAGS_DRV_MODE) < 0)
return perror("link set xdp fd failed"), -errno;
ret = bpf_map_update_elem(qidconf_map, &key, &plget->queue, 0);
if (ret)
return perror("bpf_map_update_elem qidconf"), -errno;
ret = bpf_map_update_elem(xsks_map, &key, &plget->xsk->sfd, 0);
if (ret)
return perror("bpf_map_update_sk elem xsks_map"), -errno;
return 0;
}