-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvproxy_xdp_util.c
160 lines (145 loc) · 5.07 KB
/
vproxy_xdp_util.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "vproxy_xdp.h"
#include <errno.h>
#include <string.h>
#include <net/if.h>
#include <linux/if_link.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <sys/utsname.h>
struct bpf_object* load_bpf_object_file(const char* filename, struct vp_bpf_map_reuse* maps) {
struct bpf_object_open_opts open_opts = { 0 };
open_opts.sz = sizeof(struct bpf_object_open_opts);
struct bpf_object* obj = bpf_object__open_file(filename, &open_opts);
if (obj == NULL) {
fprintf(stderr, "ERR: bpf_object__open_file(%s) failed: %d %s\n",
filename, errno, strerror(errno));
return NULL;
}
int mapfds[VP_BPF_MAP_REUSE_ARRAY_MAX];
int mapfd_offset = 0;
memset(mapfds, 0, sizeof(mapfds));
if (maps != NULL) {
for (int i = 0;;++i) {
struct vp_bpf_map_reuse* m = &maps[i];
if (m->name == NULL) {
break;
}
if (i >= VP_BPF_MAP_REUSE_ARRAY_MAX) {
fprintf(stderr, "ERR: reuse maps array len excceds limit: %d", i);
break;
}
int fd;
if (m->type == VP_BPF_MAP_REUSE_TYPE_MAP) {
fd = bpf_map__fd(m->map);
if (fd < 0) {
fprintf(stderr, "ERR: bpf_map__fd(%s) failed: %d %s\n",
m->name, -fd, strerror(-fd));
continue;
}
} else if (m->type == VP_BPF_MAP_REUSE_TYPE_PIN_PATH) {
fd = bpf_obj_get(m->pinpath);
if (fd < 0) {
fprintf(stderr, "ERR: bpf_obj_get(%s) failed: %d %s\n",
m->pinpath, -fd, strerror(-fd));
continue;
}
mapfds[mapfd_offset++] = fd;
} else {
fprintf(stderr, "ERR: unknown type(%d) for vp_bpf_map_reuse", m->type);
continue;
}
struct bpf_map* target = bpf_object__find_map_by_name(obj, m->name);
if (target == NULL) {
fprintf(stderr, "ERR: bpf_object__find_map_by_name(%s, %s) failed: %d %s\n",
filename, m->name, errno, strerror(errno));
continue;
}
int err = bpf_map__reuse_fd(target, fd);
if (err < 0) {
fprintf(stderr, "ERR: bpf_map__reuse_fd(%s/%s, %d) failed: %d %s\n",
filename, m->name, fd, -err, strerror(-err));
goto fail_release;
}
}
}
int err = bpf_object__load(obj);
if (err < 0) {
fprintf(stderr, "ERR: bpf_object__load(%s) failed: %d %s\n",
filename, -err, strerror(-err));
goto fail_release;
}
for (int i = 0; i < mapfd_offset; ++i) close(mapfds[i]);
return obj;
fail_release:
for (int i = 0; i < mapfd_offset; ++i) close(mapfds[i]);
bpf_object__close(obj);
return NULL;
}
int xdp_link_attach(int ifindex, __u32 xdp_flags, int prog_fd) {
struct bpf_xdp_attach_opts attach_opts = { 0 };
attach_opts.sz = sizeof(struct bpf_xdp_attach_opts);
int err = bpf_xdp_attach(ifindex, prog_fd, xdp_flags, &attach_opts);
if (err == -EEXIST && !(xdp_flags & XDP_FLAGS_UPDATE_IF_NOEXIST)) {
/* Force mode didn't work, probably because a program of the
* opposite type is loaded. Let's unload that and try loading
* again
*/
__u32 old_flags = xdp_flags;
xdp_flags &= ~XDP_FLAGS_MODES;
xdp_flags |= (old_flags & XDP_FLAGS_SKB_MODE) ? XDP_FLAGS_DRV_MODE : XDP_FLAGS_SKB_MODE;
err = bpf_xdp_detach(ifindex, xdp_flags, &attach_opts);
if (!err)
err = bpf_xdp_attach(ifindex, prog_fd, xdp_flags, &attach_opts);
}
if (err < 0) {
switch(-err) {
case EBUSY:
case EEXIST:
fprintf(stderr, "XDP already loaded on device\n");
break;
case EOPNOTSUPP:
fprintf(stderr, "Native-XDP not supported, use skb mode instead\n");
break;
default:
fprintf(stderr, "bpf_set_link_xdp_fd failed: %d %s\n",
-err, strerror(-err));
}
return 1;
}
return 0;
}
int vp_get_kernel_version(int* ver, int ver_len) {
struct utsname name;
int err = uname(&name);
if (err) {
return -1;
}
char* p = name.release;
int i = 0;
while (*p && i < ver_len) {
if (isdigit(*p)) {
ver[i] = strtol(p, &p, 10);
++i;
} else {
++p;
}
}
return i;
}
// 6.11+ or 6.10.3+
int vp_need_xdp_umem_tx_metadata_len_flag(void) {
int ver[3];
int len = vp_get_kernel_version(ver, 3);
if (len < 3) {
fprintf(stderr, "failed to get kernel version: %d %s\n",
errno, strerror(errno));
return 0;
}
if (ver[0] < 6) return 0;
if (ver[0] > 6) return 1;
if (ver[1] < 10) return 0;
if (ver[1] > 10) return 1;
if (ver[2] < 3) return 0;
return 1;
}