-
Notifications
You must be signed in to change notification settings - Fork 5
/
bpf.c
186 lines (147 loc) · 4.1 KB
/
bpf.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2022 Felix Fietkau <[email protected]>
*/
#include <sys/resource.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <glob.h>
#include <unistd.h>
#include "bridger.h"
static struct bpf_object *obj;
static struct bpf_program *prog, *tx_prog;
static int map_pending = -1;
static int map_offload = -1;
static int map_policy = -1;
static struct uloop_timeout poll_timer;
int bridger_bpf_prog_fd = -1;
int bridger_bpf_tx_prog_fd = -1;
static int bridger_bpf_pr(enum libbpf_print_level level, const char *format,
va_list args)
{
return vfprintf(stderr, format, args);
}
static void bridger_init_env(void)
{
struct rlimit limit = {
.rlim_cur = RLIM_INFINITY,
.rlim_max = RLIM_INFINITY,
};
setrlimit(RLIMIT_MEMLOCK, &limit);
}
void bridger_bpf_flow_upload(struct bridger_flow *flow)
{
struct bridger_offload_flow val;
if (bpf_map_lookup_elem(map_offload, &flow->key, &val) == 0)
flow->offload.packets = val.packets;
bpf_map_update_elem(map_offload, &flow->key, &flow->offload, BPF_ANY);
}
void bridger_bpf_flow_update(struct bridger_flow *flow)
{
uint64_t prev_packets = flow->offload.packets;
bpf_map_lookup_elem(map_offload, &flow->key, &flow->offload);
flow->cur_packets = flow->offload.packets - prev_packets;
bridger_ewma(&flow->avg_packets, flow->cur_packets);
}
void bridger_bpf_flow_delete(struct bridger_flow *flow)
{
bpf_map_delete_elem(map_offload, &flow->key);
}
void bridger_bpf_dev_policy_set(struct device *dev)
{
struct bridger_policy_flow val = {};
unsigned int ifindex = device_ifindex(dev);
struct device *rdev = NULL;
if (dev->redirect_dev)
rdev = device_get(dev->redirect_dev);
if (!rdev) {
bpf_map_delete_elem(map_policy, &ifindex);
return;
}
val.flow.vlan = device_vlan_get_output(rdev, dev->pvid);
val.flow.target_port = device_ifindex(rdev);
if (dev->master)
memcpy(val.bridge_mac, dev->master->addr, ETH_ALEN);
bpf_map_update_elem(map_policy, &ifindex, &val, BPF_ANY);
}
static void bridger_bpf_poll_pending(struct uloop_timeout *timeout)
{
struct bridger_flow_key key = {};
struct bridger_pending_flow val;
while (bpf_map_get_next_key(map_pending, &key, &key) == 0) {
if (bpf_map_lookup_elem(map_pending, &key, &val))
continue;
bpf_map_delete_elem(map_pending, &key);
bridger_check_pending_flow(&key, &val);
}
uloop_timeout_set(timeout, 1000);
}
static bool
bridger_get_map_fd(int *fd, const char *name)
{
*fd = bpf_object__find_map_fd_by_name(obj, name);
if (*fd < 0) {
D("Could not find map '%s'\n", name);
return false;
}
return true;
}
static int
bridger_create_program(void)
{
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
.pin_root_path = BRIDGER_DATA_PATH,
);
int err;
obj = bpf_object__open_file(BRIDGER_PROG_PATH, &opts);
err = libbpf_get_error(obj);
if (err) {
perror("bpf_object__open_file");
return -1;
}
prog = bpf_object__find_program_by_name(obj, "bridger_input");
tx_prog = bpf_object__find_program_by_name(obj, "bridger_output");
if (!prog || !tx_prog) {
D("Can't find classifier prog\n");
return -1;
}
bpf_program__set_type(tx_prog, BPF_PROG_TYPE_SCHED_CLS);
bpf_program__set_type(prog, BPF_PROG_TYPE_SCHED_CLS);
err = bpf_object__load(obj);
if (err) {
perror("bpf_object__load");
return -1;
}
libbpf_set_print(NULL);
unlink(BRIDGER_PIN_PATH);
err = bpf_program__pin(prog, BRIDGER_PIN_PATH);
if (err) {
D("Failed to pin program to %s: %s\n",
BRIDGER_PIN_PATH, strerror(-err));
}
if (!bridger_get_map_fd(&map_pending, "pending_flows") ||
!bridger_get_map_fd(&map_offload, "offload_flows") ||
!bridger_get_map_fd(&map_policy, "dev_policy"))
return -1;
bridger_bpf_prog_fd = bpf_program__fd(prog);
bridger_bpf_tx_prog_fd = bpf_program__fd(tx_prog);
return 0;
}
int bridger_bpf_init(void)
{
glob_t g;
int ret;
int i;
if (glob(BRIDGER_DATA_PATH "/*", 0, NULL, &g) == 0) {
for (i = 0; i < g.gl_pathc; i++)
unlink(g.gl_pathv[i]);
}
libbpf_set_print(bridger_bpf_pr);
bridger_init_env();
ret = bridger_create_program();
if (ret)
return ret;
poll_timer.cb = bridger_bpf_poll_pending;
bridger_bpf_poll_pending(&poll_timer);
return 0;
}