-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathflow.cpp
268 lines (241 loc) · 10.2 KB
/
flow.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
*
* Copyright (C) Max <[email protected]>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "flow.h"
#include "params.h"
#include "cfg.h"
#include <rte_ip.h>
#include <rte_udp.h>
#include <rte_tcp.h>
#include <Poco/Util/ServerApplication.h>
//#ifdef RTE_MACHINE_CPUFLAG_SSE4_2
#include <rte_hash_crc.h>
#define DEFAULT_HASH_FUNC rte_hash_crc
//#else
//#include <rte_jhash.h>
//#define DEFAULT_HASH_FUNC rte_jhash
//#endif
rte_xmm_t mask0 = {.u32 = {BIT_8_TO_15, ALL_32_BITS, ALL_32_BITS, ALL_32_BITS} };
rte_xmm_t mask1 = {.u32 = {BIT_16_TO_23, ALL_32_BITS, ALL_32_BITS, ALL_32_BITS} };
rte_xmm_t mask2 = {.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
static int compare_ipv4(const void *key1, const void *key2, size_t key_len)
{
ipv4_5tuple_host *flow = (ipv4_5tuple_host *) key1;
ipv4_5tuple_host *pkt_infos = (ipv4_5tuple_host *) key2;
return !((flow->ip_src == pkt_infos->ip_src &&
flow->ip_dst == pkt_infos->ip_dst &&
flow->port_src == pkt_infos->port_src &&
flow->port_dst == pkt_infos->port_dst) ||
(flow->ip_src == pkt_infos->ip_dst &&
flow->ip_dst == pkt_infos->ip_src &&
flow->port_src == pkt_infos->port_dst &&
flow->port_dst == pkt_infos->port_src)) &&
flow->proto == pkt_infos->proto;
}
static int compare_ipv6(const void *key1, const void *key2, size_t key_len)
{
ipv6_5tuple_host *flow = (ipv6_5tuple_host *) key1;
ipv6_5tuple_host *pkt_infos = (ipv6_5tuple_host *) key2;
u_int8_t i;
/*1: src=src and dst=dst. 2: src=dst and dst=src. */
u_int8_t direction=0;
for(i=0; i< 16; i++)
{
if(direction!=2 &&
pkt_infos->ip_src[i] == flow->ip_src[i] &&
pkt_infos->ip_dst[i] == flow->ip_dst[i])
{
direction=1;
}else if(direction!=1 &&
pkt_infos->ip_src[i] == flow->ip_dst[i] &&
pkt_infos->ip_dst[i] == flow->ip_src[i])
{
direction=2;
}else
return 1;
}
if(direction==1)
return !(flow->port_src == pkt_infos->port_src &&
flow->port_dst == pkt_infos->port_dst &&
flow->proto == pkt_infos->proto);
else if(direction==2)
return !(flow->port_src == pkt_infos->port_dst &&
flow->port_dst == pkt_infos->port_src &&
flow->proto == pkt_infos->proto);
else
return 1;
}
void initFlowStorages()
{
int socketid = 0;
Poco::Util::Application& app = Poco::Util::Application::instance();
// allocate mempool for all flows
app.logger().information("Allocating %Lu bytes (%u entries) for ipv4 flow pool", (uint64_t) ((global_prm->memory_configs.ipv4.flows_number)*sizeof(struct ext_dpi_flow_info_ipv4)), global_prm->memory_configs.ipv4.flows_number);
rte_mempool *flows_pool_ipv4 = rte_mempool_create("ipv4_flows_p", global_prm->memory_configs.ipv4.flows_number, sizeof(struct ext_dpi_flow_info_ipv4), 0, 0, NULL, NULL, NULL, NULL, socketid, 0);
if(flows_pool_ipv4 == nullptr)
{
app.logger().fatal("Not enough memory for ipv4 flows pool. Tried to allocate %Lu bytes on socket %d", (uint64_t) ((global_prm->memory_configs.ipv4.flows_number)*sizeof(struct ext_dpi_flow_info_ipv4)), socketid);
throw Poco::Exception("Not enough memory for flows pool");
}
app.logger().information("Allocating %Lu bytes (%u entries) for ipv6 flow pool", (uint64_t) ((global_prm->memory_configs.ipv6.flows_number)*sizeof(struct ext_dpi_flow_info_ipv6)), global_prm->memory_configs.ipv6.flows_number);
rte_mempool *flows_pool_ipv6 = rte_mempool_create("ipv6_flows_p", (global_prm->memory_configs.ipv6.flows_number), sizeof(struct ext_dpi_flow_info_ipv6), 0, 0, NULL, NULL, NULL, NULL, socketid, 0);
if(flows_pool_ipv6 == nullptr)
{
app.logger().fatal("Not enough memory for ipv6 flows pool. Tried to allocate %Lu bytes on socket %d", (uint64_t) ((global_prm->memory_configs.ipv6.flows_number )*sizeof(struct ext_dpi_flow_info_ipv6)), socketid);
throw Poco::Exception("Not enough memory for flows pool");
}
flow_storage_params_t prm;
prm.p_lifetime = global_prm->flow_lifetime;
for(int i=0; i < global_prm->workers_number; i++)
{
prm.worker_id = i;
prm.mempool = flows_pool_ipv4;
prm.recs_number = global_prm->memory_configs.ipv4.recs_number / global_prm->memory_configs.ipv4.parts_of_flow;
flow_storage_t *flows = &worker_params[i].flows_ipv4;
if(global_prm->memory_configs.ipv4.parts_of_flow > 0)
{
flows->flows = new (std::nothrow) FlowStorage*[global_prm->memory_configs.ipv4.parts_of_flow];
if(!flows->flows)
{
app.logger().fatal("Not enough memory for FlowStorage pointers");
throw Poco::Exception("Not enough memory for FlowStorage pointers");
}
memset(flows->flows, 0, sizeof(FlowStorage *)*global_prm->memory_configs.ipv4.parts_of_flow);
for(int z=0; z < global_prm->memory_configs.ipv4.parts_of_flow; z++)
{
prm.part_no = z;
flows->flows[z] = new FlowStorageIPV4(&prm);
if(flows->flows[z]->init(&prm))
{
app.logger().fatal("Unable to init FlowStorageIPV4");
throw Poco::Exception("Unable to init FlowStorageIPV4");
}
}
}
prm.mempool = flows_pool_ipv6;
prm.recs_number = global_prm->memory_configs.ipv6.recs_number / global_prm->memory_configs.ipv6.parts_of_flow;
flows = &worker_params[i].flows_ipv6;
// setup ipv6
if(global_prm->memory_configs.ipv6.parts_of_flow > 0)
{
flows->flows = new (std::nothrow) FlowStorage*[global_prm->memory_configs.ipv6.parts_of_flow];
if(!flows->flows)
{
app.logger().fatal("Not enough memory for FlowStorage pointers");
throw Poco::Exception("Not enough memory for FlowStorage pointers");
}
memset(flows->flows, 0, sizeof(FlowStorage *)*global_prm->memory_configs.ipv6.parts_of_flow);
for(int z=0; z < global_prm->memory_configs.ipv6.parts_of_flow; z++)
{
prm.part_no = z;
flows->flows[z] = new FlowStorageIPV6(&prm);
if(flows->flows[z]->init(&prm))
{
app.logger().fatal("Unable to init FlowStorageIPV6");
throw Poco::Exception("Unable to init FlowStorageIPV6");
}
}
}
}
}
// ipv4
FlowStorageIPV4::FlowStorageIPV4(flow_storage_params_t *prm) : FlowStorage(prm->mempool),
_logger(Poco::Logger::get("FlowStorageIPV4"))
{
// init hash
_logger.information("Allocate %d bytes (%d entries, element size %z) for flow hash ipv4", (int)(prm->recs_number * sizeof(union ipv4_5tuple_host)), (int)prm->recs_number, sizeof(union ipv4_5tuple_host));
struct rte_hash_parameters ipv4_hash_params = {0};
ipv4_hash_params.entries = prm->recs_number;
ipv4_hash_params.key_len = sizeof(union ipv4_5tuple_host);
ipv4_hash_params.hash_func = ipv4_hash_crc;
ipv4_hash_params.hash_func_init_val = 0;
std::string hash_name("ipv4_fh" + std::to_string(prm->worker_id) + "_" + std::to_string(prm->part_no));
ipv4_hash_params.name = hash_name.c_str();
hash = rte_hash_create(&ipv4_hash_params);
if(!hash)
{
_logger.fatal("Unable to create ipv4 flow hash");
throw Poco::Exception("Unable to create ipv4 flow hash");
}
rte_hash_set_cmp_func(hash, compare_ipv4);
// init pointers for hash data
std::string mem_name("ipv4_f" + std::to_string(prm->worker_id) + "_" + std::to_string(prm->part_no));
_logger.information("Allocating %d bytes (%d entries) for ipv4_flows", (int) (sizeof(struct ext_dpi_flow_info_ipv4 *) * prm->recs_number), (int)prm->recs_number);
data = (struct ext_dpi_flow_info_ipv4 **)rte_zmalloc(mem_name.c_str(), prm->recs_number * sizeof(struct ext_dpi_flow_info_ipv4 *), RTE_CACHE_LINE_SIZE);
if(data == nullptr)
{
_logger.fatal("Not enough memory for ipv4 flows");
throw Poco::Exception("Not enough memory for ipv4 flows");
}
}
FlowStorageIPV4::~FlowStorageIPV4()
{
// delete hash
rte_hash_free(hash);
// delete data
rte_free(data);
}
int FlowStorageIPV4::init(flow_storage_params_t *prm)
{
if(short_alfs.init(prm->recs_number, prm->worker_id, en_alfs_short, prm->p_lifetime[0], 32))
return -1;
return long_alfs.init(prm->recs_number, prm->worker_id, en_alfs_long, prm->p_lifetime[1], 8);
}
// ipv6
FlowStorageIPV6::FlowStorageIPV6(flow_storage_params_t *prm) : FlowStorage(prm->mempool),
_logger(Poco::Logger::get("FlowStorageIPV6"))
{
// init hash
_logger.information("Allocate %d bytes (%d entries, element size %z) for flow hash ipv6", (int)(prm->recs_number * sizeof(union ipv6_5tuple_host)), (int)prm->recs_number, sizeof(union ipv6_5tuple_host));
struct rte_hash_parameters ipv6_hash_params = {0};
ipv6_hash_params.entries = prm->recs_number;
ipv6_hash_params.key_len = sizeof(union ipv6_5tuple_host);
ipv6_hash_params.hash_func = ipv6_hash_crc;
ipv6_hash_params.hash_func_init_val = 0;
std::string hash_name("ipv6_fh" + std::to_string(prm->worker_id) + "_" + std::to_string(prm->part_no));
ipv6_hash_params.name = hash_name.c_str();
hash = rte_hash_create(&ipv6_hash_params);
if(!hash)
{
_logger.fatal("Unable to create ipv6 flow hash");
throw Poco::Exception("Unable to create ipv6 flow hash");
}
rte_hash_set_cmp_func(hash, compare_ipv6);
// init pointers for hash data
std::string mem_name("ipv6_f" + std::to_string(prm->worker_id) + "_" + std::to_string(prm->part_no));
_logger.information("Allocating %d bytes (%d entries) for ipv6_flows", (int) (sizeof(struct ext_dpi_flow_info_ipv6 *) * prm->recs_number), (int)prm->recs_number);
data = (struct ext_dpi_flow_info_ipv6 **)rte_zmalloc(mem_name.c_str(), prm->recs_number * sizeof(struct ext_dpi_flow_info_ipv6 *), RTE_CACHE_LINE_SIZE);
if(data == nullptr)
{
_logger.fatal("Not enough memory for ipv6 flows");
throw Poco::Exception("Not enough memory for ipv6 flows");
}
}
FlowStorageIPV6::~FlowStorageIPV6()
{
// delete hash
rte_hash_free(hash);
// delete data
rte_free(data);
}
int FlowStorageIPV6::init(flow_storage_params_t *prm)
{
if(short_alfs.init(prm->recs_number, prm->worker_id, en_alfs_short, prm->p_lifetime[0], 32))
return -1;
return long_alfs.init(prm->recs_number, prm->worker_id, en_alfs_long, prm->p_lifetime[1], 8);
}