-
Notifications
You must be signed in to change notification settings - Fork 5
/
ebpf_wrapper.cc
210 lines (168 loc) · 6.63 KB
/
ebpf_wrapper.cc
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <string.h>
#include <cassert>
#include <fstream>
#include <vector>
#include <thread>
#include <chrono>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "BPF.h"
using namespace std;
#ifdef USERSPACE
#include "hashmap.c"
#include "openstate.h"
#include "jhash.h"
#include "ebpf.c"
static uint64_t hash_fn(const void *k, void *ctx)
{
// XXX: This is bad since it only returns 32 bits
return (uint64_t) jhash(k, sizeof(XFSMTableKey), 0);
}
static bool equal_fn(const void *a, const void *b, void *ctx)
{
XFSMTableKey* as = (XFSMTableKey*) a;
XFSMTableKey* bs = (XFSMTableKey*) b;
return as->l4_proto == bs->l4_proto && as->ip_src == bs->ip_src && as->ip_dst == bs->ip_dst && as->src_port == bs->src_port && as->dst_port == bs->dst_port;
}
#include <unistd.h>
#include <fcntl.h>
bool set_blocking_mode(int socket)
{
bool ret = true;
const int flags = fcntl(socket, F_GETFL, 0);
// if ((flags & O_NONBLOCK) && !is_blocking) { info("set_blocking_mode(): socket was already in non-blocking mode"); return ret; }
// if (!(flags & O_NONBLOCK) && is_blocking) { info("set_blocking_mode(): socket was already in blocking mode"); return ret; }
// ret = 0 == fcntl(socket, F_SETFL, is_blocking ? flags ^ O_NONBLOCK : flags | O_NONBLOCK));
ret = 0 == fcntl(socket, F_SETFL, flags & (~O_NONBLOCK));
return ret;
}
#endif
double time_to_run;
const char interface[] = "eth0";
string prefix_path = "./decision_tree/Dec29_19-15-36_hyperion_0_3/";
std::vector<int64_t> read_file(string filename)
{
// open the file:
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
std::vector<int64_t> fileData(fileSize/sizeof(int64_t));
file.read((char*) &fileData[0], fileSize);
return fileData;
}
int main(int argc, char *argv[])
{
auto starttime = std::chrono::system_clock::now();
assert(argc >= 2);
sscanf(argv[1],"%lf",&time_to_run);
vector<int64_t> children_left = read_file(prefix_path + "/childrenLeft");
vector<int64_t> children_right = read_file(prefix_path + "/childrenRight");
vector<int64_t> value = read_file(prefix_path + "/value");
vector<int64_t> feature = read_file(prefix_path + "/feature");
vector<int64_t> threshold = read_file(prefix_path + "/threshold");
#ifndef USERSPACE
string maps_string = string("#include \"openstate.h\"\n") +
"BPF_TABLE(\"lru_hash\", struct XFSMTableKey, struct XFSMTableLeaf, xfsm_table, 10000);" +
"BPF_ARRAY(num_processed, u64, 1);" +
"BPF_ARRAY(all_features, s64, 12);" +
"BPF_ARRAY(children_left, s64, " + to_string(children_left.size()) + ");" +
"BPF_ARRAY(children_right, s64, " + to_string(children_right.size()) + ");" +
"BPF_ARRAY(value, s64, " + to_string(value.size()) + ");" +
"BPF_ARRAY(feature, s64, " + to_string(feature.size()) + ");" +
"BPF_ARRAY(threshold, s64, " + to_string(threshold.size()) + ");\n";
std::ifstream source_stream("ebpf.c");
std::string ebpf_program((std::istreambuf_iterator<char>(source_stream)),
std::istreambuf_iterator<char>());
ebpf_program = maps_string + ebpf_program;
ebpf::BPF bpf;
auto res = bpf.init(ebpf_program);
if (res.code() != 0) {
std::cerr << res.msg() << std::endl;
return 1;
}
ebpf::BPFArrayTable<int64_t> children_left_table = bpf.get_array_table<int64_t>("children_left");
for (size_t i = 0; i < children_left.size(); i++)
{
res = children_left_table.update_value(i, children_left[i]);
assert(res.code() == 0);
}
ebpf::BPFArrayTable<int64_t> children_right_table = bpf.get_array_table<int64_t>("children_right");
for (size_t i = 0; i < children_right.size(); i++)
{
res = children_right_table.update_value(i, children_right[i]);
assert(res.code() == 0);
}
ebpf::BPFArrayTable<int64_t> value_table = bpf.get_array_table<int64_t>("value");
for (size_t i = 0; i < value.size(); i++)
{
res = value_table.update_value(i, value[i]);
assert(res.code() == 0);
}
ebpf::BPFArrayTable<int64_t> threshold_table = bpf.get_array_table<int64_t>("threshold");
for (size_t i = 0; i < threshold.size(); i++)
{
res = threshold_table.update_value(i, threshold[i]);
assert(res.code() == 0);
}
ebpf::BPFArrayTable<int64_t> feature_table = bpf.get_array_table<int64_t>("feature");
for (size_t i = 0; i < feature.size(); i++)
{
res = feature_table.update_value(i, feature[i]);
assert(res.code() == 0);
}
int fd;
res = bpf.load_func("filter", BPF_PROG_TYPE_SOCKET_FILTER, fd);
assert(res.code() == 0);
int sd = -1;
sd = bpf_open_raw_sock("eth0");
int ret;
ret = setsockopt(sd, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
assert(ret>=0);
auto current_time = std::chrono::system_clock::now();
starttime = std::chrono::system_clock::now();
std::this_thread::sleep_for(std::chrono::duration<double>(time_to_run) - (current_time - starttime));
ebpf::BPFArrayTable<uint64_t> num_processed_table = bpf.get_array_table<uint64_t>("num_processed");
uint64_t actual_num_processed;
res = num_processed_table.get_value(0, actual_num_processed);
assert(res.code() == 0);
cout << "Finished kernel" << endl << flush;
#else
hashmap* map = hashmap__new(hash_fn, equal_fn, NULL);
struct shared_struct ss = {map, 0, &children_left[0], children_left.size(), &children_right[0], children_right.size(), &value[0], value.size(), &feature[0], feature.size(), &threshold[0], threshold.size()};
void* buffer = (void*)malloc(65536);
int sd = -1;
sd = bpf_open_raw_sock("eth0");
set_blocking_mode(sd);
auto duration = std::chrono::duration<double>(time_to_run);
starttime = std::chrono::system_clock::now();
// cout << "Initialized everything" << endl << flush;
for(size_t i=0;;i++) {
int length = 0; /*length of the received frame*/
length = recv(sd, buffer, 65536, 0);
if (length == -1) { perror("recv"); }
// cout << "before" << endl << flush;
filter(buffer, &ss);
// cout << "after" << endl << flush;
if (i % 1000 == 0) {
auto current_time = std::chrono::system_clock::now();
// cout << "duration " << duration.count() << " difference " << ((double) (current_time - starttime).count())/1000000000 << endl << flush;
if(duration.count() < ((double) (current_time - starttime).count())/1000000000) {
// cout << "Breaking" << endl << flush;
break;
}
}
}
cout << "Finished userspace" << endl << flush;
uint64_t actual_num_processed = ss.num_processed;
#endif
auto final_time = std::chrono::system_clock::now();
cout << "Ran for " << ((double) (final_time-starttime).count())/1000000000 << "s, processed " << actual_num_processed << " packets" << endl << flush;
}