/
flow_proto.cc
437 lines (380 loc) · 13.9 KB
/
flow_proto.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <net/address_util.h>
#include <init/agent_param.h>
#include <cmn/agent_stats.h>
#include <oper/agent_profile.h>
#include <vrouter/ksync/flowtable_ksync.h>
#include <vrouter/ksync/ksync_init.h>
#include <vrouter/ksync/ksync_flow_index_manager.h>
#include "vrouter/flow_stats/flow_stats_collector.h"
#include "flow_proto.h"
#include "flow_mgmt_dbclient.h"
#include "flow_mgmt.h"
#include "flow_event.h"
static void UpdateStats(FlowEvent::Event event, FlowStats *stats);
FlowProto::FlowProto(Agent *agent, boost::asio::io_service &io) :
Proto(agent, kTaskFlowEvent, PktHandler::FLOW, io),
flow_update_queue_(agent->task_scheduler()->GetTaskId(kTaskFlowUpdate), 0,
boost::bind(&FlowProto::FlowEventHandler, this, _1,
static_cast<FlowTable *>(NULL))),
stats_() {
flow_update_queue_.set_name("Flow update queue");
agent->SetFlowProto(this);
set_trace(false);
uint16_t table_count = agent->flow_thread_count();
assert(table_count >= kMinTableCount && table_count <= kMaxTableCount);
for (uint8_t i = 0; i < table_count; i++) {
flow_table_list_.push_back(new FlowTable(agent_, i));
}
TaskScheduler *scheduler = agent_->task_scheduler();
uint32_t task_id = scheduler->GetTaskId(kTaskFlowEvent);
for (uint32_t i = 0; i < table_count; i++) {
flow_event_queue_.push_back
(new FlowEventQueue(task_id, i,
boost::bind(&FlowProto::FlowEventHandler, this,
_1, flow_table_list_[i])));
}
}
FlowProto::~FlowProto() {
STLDeleteValues(&flow_event_queue_);
STLDeleteValues(&flow_table_list_);
}
void FlowProto::Init() {
agent_->stats()->RegisterFlowCountFn(boost::bind(&FlowProto::FlowCount,
this));
for (uint16_t i = 0; i < flow_table_list_.size(); i++) {
flow_table_list_[i]->Init();
}
AgentProfile *profile = agent_->oper_db()->agent_profile();
profile->RegisterPktFlowStatsCb(boost::bind(&FlowProto::SetProfileData,
this, _1));
}
void FlowProto::InitDone() {
for (uint16_t i = 0; i < flow_table_list_.size(); i++) {
flow_table_list_[i]->InitDone();
}
}
void FlowProto::Shutdown() {
for (uint16_t i = 0; i < flow_table_list_.size(); i++) {
flow_table_list_[i]->Shutdown();
}
for (uint32_t i = 0; i < flow_event_queue_.size(); i++) {
flow_event_queue_[i]->Shutdown();
}
flow_update_queue_.Shutdown();
}
FlowHandler *FlowProto::AllocProtoHandler(boost::shared_ptr<PktInfo> info,
boost::asio::io_service &io) {
uint32_t index = FlowTableIndex(info->sport, info->dport);
return new FlowHandler(agent(), info, io, this, index);
}
bool FlowProto::Validate(PktInfo *msg) {
if (msg->l3_forwarding && msg->ip == NULL && msg->ip6 == NULL &&
msg->type != PktType::MESSAGE) {
if (msg->family == Address::INET) {
FLOW_TRACE(DetailErr, msg->agent_hdr.cmd_param,
msg->agent_hdr.ifindex,
msg->agent_hdr.vrf,
msg->ip_saddr.to_v4().to_ulong(),
msg->ip_daddr.to_v4().to_ulong(),
"Flow : Non-IP packet. Dropping",
msg->l3_forwarding, 0, 0, 0, 0);
} else if (msg->family == Address::INET6) {
uint64_t sip[2], dip[2];
Ip6AddressToU64Array(msg->ip_saddr.to_v6(), sip, 2);
Ip6AddressToU64Array(msg->ip_daddr.to_v6(), dip, 2);
FLOW_TRACE(DetailErr, msg->agent_hdr.cmd_param,
msg->agent_hdr.ifindex,
msg->agent_hdr.vrf, -1, -1,
"Flow : Non-IP packet. Dropping",
msg->l3_forwarding,
sip[0], sip[1], dip[0], dip[1]);
} else {
assert(0);
}
return false;
}
return true;
}
uint16_t FlowProto::FlowTableIndex(uint16_t sport, uint16_t dport) const {
return (sport ^ dport) % (flow_event_queue_.size());
}
FlowTable *FlowProto::GetFlowTable(const FlowKey &key) const {
uint16_t index = FlowTableIndex(key.src_port, key.dst_port);
return flow_table_list_[index];
}
bool FlowProto::Enqueue(boost::shared_ptr<PktInfo> msg) {
if (Validate(msg.get()) == false) {
return true;
}
FreeBuffer(msg.get());
FlowEvent event(FlowEvent::VROUTER_FLOW_MSG, msg);
EnqueueFlowEvent(event);
return true;
}
void FlowProto::DisableFlowEventQueue(uint32_t index, bool disabled) {
flow_event_queue_[index]->set_disable(disabled);
}
void FlowProto::DisableFlowMgmtQueue(bool disabled) {
flow_update_queue_.set_disable(disabled);
}
/////////////////////////////////////////////////////////////////////////////
// FlowTable related routines
/////////////////////////////////////////////////////////////////////////////
void FlowProto::FlushFlows() {
for (uint16_t i = 0; i < flow_table_list_.size(); i++) {
flow_table_list_[i]->DeleteAll();
}
}
FlowTable *FlowProto::GetTable(uint16_t index) const {
return flow_table_list_[index];
}
uint32_t FlowProto::FlowCount() const {
uint32_t count = 0;
for (uint16_t i = 0; i < flow_table_list_.size(); i++) {
count += flow_table_list_[i]->Size();
}
return count;
}
void FlowProto::VnFlowCounters(const VnEntry *vn, uint32_t *in_count,
uint32_t *out_count) {
*in_count = 0;
*out_count = 0;
if (vn == NULL)
return;
agent_->pkt()->flow_mgmt_manager()->VnFlowCounters(vn, in_count, out_count);
}
FlowEntry *FlowProto::Find(const FlowKey &key) const {
return GetFlowTable(key)->Find(key);
}
bool FlowProto::AddFlow(FlowEntry *flow) {
FlowTable *table = flow->flow_table();
table->Add(flow, flow->reverse_flow_entry());
return true;
}
bool FlowProto::UpdateFlow(FlowEntry *flow) {
FlowTable *table = flow->flow_table();
table->Update(flow, flow->reverse_flow_entry());
return true;
}
/////////////////////////////////////////////////////////////////////////////
// Flow Control Event routines
/////////////////////////////////////////////////////////////////////////////
void FlowProto::EnqueueEvent(const FlowEvent &event, FlowTable *table) {
flow_event_queue_[table->table_index()]->Enqueue(event);
}
void FlowProto::EnqueueFlowEvent(const FlowEvent &event) {
// Keep UpdateStats in-sync on add of new events
UpdateStats(event.event(), &stats_);
switch (event.event()) {
case FlowEvent::VROUTER_FLOW_MSG: {
PktInfo *info = event.pkt_info().get();
uint32_t index = FlowTableIndex(info->sport, info->dport);
flow_event_queue_[index]->Enqueue(event);
break;
}
case FlowEvent::DELETE_FLOW: {
FlowTable *table = GetFlowTable(event.get_flow_key());
flow_event_queue_[table->table_index()]->Enqueue(event);
break;
}
case FlowEvent::AUDIT_FLOW:
case FlowEvent::DELETE_DBENTRY:
case FlowEvent::EVICT_FLOW:
case FlowEvent::RETRY_INDEX_ACQUIRE:
case FlowEvent::REVALUATE_FLOW:
case FlowEvent::FREE_FLOW_REF: {
FlowEntry *flow = event.flow();
FlowTable *table = flow->flow_table();
flow_event_queue_[table->table_index()]->Enqueue(event);
break;
}
case FlowEvent::FREE_DBENTRY:
case FlowEvent::REVALUATE_DBENTRY: {
flow_update_queue_.Enqueue(event);
break;
}
case FlowEvent::GROW_FREE_LIST: {
FlowTable *table = GetFlowTable(event.get_flow_key());
flow_event_queue_[table->table_index()]->Enqueue(event);
break;
}
case FlowEvent::FLOW_HANDLE_UPDATE:
case FlowEvent::KSYNC_VROUTER_ERROR:
case FlowEvent::KSYNC_EVENT: {
FlowTableKSyncObject *ksync_obj = static_cast<FlowTableKSyncObject *>
(event.ksync_entry()->GetObject());
FlowTable *table = ksync_obj->flow_table();
flow_event_queue_[table->table_index()]->Enqueue(event);
break;
}
default:
assert(0);
break;
}
return;
}
bool FlowProto::FlowEventHandler(const FlowEvent &req, FlowTable *table) {
switch (req.event()) {
case FlowEvent::VROUTER_FLOW_MSG: {
ProcessProto(req.pkt_info());
break;
}
case FlowEvent::DELETE_FLOW: {
FlowTable *table = GetFlowTable(req.get_flow_key());
table->Delete(req.get_flow_key(), req.get_del_rev_flow());
break;
}
case FlowEvent::AUDIT_FLOW: {
FlowEntry *flow = req.flow();
flow->flow_table()->Add(flow, NULL);
break;
}
// Check if flow-handle changed. This can happen if vrouter tries to
// setup the flow which was evicted earlier
case FlowEvent::EVICT_FLOW: {
FlowEntry *flow = req.flow();
if (flow->flow_handle() != req.flow_handle())
break;
flow->flow_table()->DeleteMessage(flow);
break;
}
// Flow was waiting for an index. Index is available now. Retry acquiring
// the index
case FlowEvent::RETRY_INDEX_ACQUIRE: {
FlowEntry *flow = req.flow();
if (flow->flow_handle() != req.flow_handle())
break;
flow->flow_table()->UpdateKSync(flow, false);
break;
}
case FlowEvent::FREE_FLOW_REF:
break;
case FlowEvent::FREE_DBENTRY: {
FlowMgmtManager *mgr = agent()->pkt()->flow_mgmt_manager();
mgr->flow_mgmt_dbclient()->FreeDBState(req.db_entry(), req.gen_id());
break;
}
case FlowEvent::DELETE_DBENTRY:
case FlowEvent::REVALUATE_DBENTRY:
case FlowEvent::REVALUATE_FLOW: {
FlowEntry *flow = req.flow();
flow->flow_table()->FlowResponseHandler(&req);
break;
}
case FlowEvent::GROW_FREE_LIST: {
FlowTable *table = GetFlowTable(req.get_flow_key());
table->GrowFreeList();
break;
}
case FlowEvent::FLOW_HANDLE_UPDATE: {
FlowTableKSyncEntry *ksync_entry =
(static_cast<FlowTableKSyncEntry *> (req.ksync_entry()));
FlowEntry *flow = ksync_entry->flow_entry().get();
table->KSyncSetFlowHandle(flow, req.flow_handle());
break;
}
case FlowEvent::KSYNC_EVENT: {
FlowTableKSyncEntry *ksync_entry =
(static_cast<FlowTableKSyncEntry *> (req.ksync_entry()));
FlowTableKSyncObject *ksync_object = static_cast<FlowTableKSyncObject *>
(ksync_entry->GetObject());
ksync_object->GenerateKSyncEvent(ksync_entry, req.ksync_event());
break;
}
case FlowEvent::KSYNC_VROUTER_ERROR: {
FlowTableKSyncEntry *ksync_entry =
(static_cast<FlowTableKSyncEntry *> (req.ksync_entry()));
// Mark the flow entry as short flow and update ksync error event
// to ksync index manager
FlowEntry *flow = ksync_entry->flow_entry().get();
flow->MakeShortFlow(FlowEntry::SHORT_FAILED_VROUTER_INSTALL);
KSyncFlowIndexManager *mgr =
agent()->ksync()->ksync_flow_index_manager();
mgr->UpdateKSyncError(flow);
// Enqueue Add request to flow-stats-collector
// to update flow flags in stats collector
FlowEntryPtr flow_ptr(flow);
agent()->flow_stats_manager()->AddEvent(flow_ptr);
break;
}
default: {
assert(0);
break;
}
}
return true;
}
void FlowProto::DeleteFlowRequest(const FlowKey &flow_key, bool del_rev_flow) {
EnqueueFlowEvent(FlowEvent(FlowEvent::DELETE_FLOW, flow_key, del_rev_flow));
return;
}
void FlowProto::EvictFlowRequest(FlowEntry *flow, uint32_t flow_handle) {
EnqueueFlowEvent(FlowEvent(FlowEvent::EVICT_FLOW, flow, flow_handle));
return;
}
void FlowProto::RetryIndexAcquireRequest(FlowEntry *flow, uint32_t flow_handle){
EnqueueFlowEvent(FlowEvent(FlowEvent::RETRY_INDEX_ACQUIRE, flow,
flow_handle));
return;
}
void FlowProto::CreateAuditEntry(FlowEntry *flow) {
EnqueueFlowEvent(FlowEvent(FlowEvent::AUDIT_FLOW, flow));
return;
}
void FlowProto::GrowFreeListRequest(const FlowKey &key) {
EnqueueFlowEvent(FlowEvent(FlowEvent::GROW_FREE_LIST, key, false));
return;
}
void FlowProto::KSyncEventRequest(KSyncEntry *ksync_entry,
KSyncEntry::KSyncEvent event) {
EnqueueFlowEvent(FlowEvent(ksync_entry, event));
return;
}
void FlowProto::KSyncFlowHandleRequest(KSyncEntry *ksync_entry,
uint32_t flow_handle) {
EnqueueFlowEvent(FlowEvent(ksync_entry, flow_handle));
return;
}
void FlowProto::KSyncFlowErrorRequest(KSyncEntry *ksync_entry) {
EnqueueFlowEvent(FlowEvent(ksync_entry));
return;
}
//////////////////////////////////////////////////////////////////////////////
// Set profile information
//////////////////////////////////////////////////////////////////////////////
void UpdateStats(FlowEvent::Event event, FlowStats *stats) {
switch (event) {
case FlowEvent::VROUTER_FLOW_MSG:
stats->add_count_++;
break;
case FlowEvent::DELETE_FLOW:
stats->delete_count_++;
break;
case FlowEvent::AUDIT_FLOW:
stats->audit_count_++;
break;
case FlowEvent::REVALUATE_FLOW:
stats->revaluate_count_++;
break;
case FlowEvent::FLOW_HANDLE_UPDATE:
stats->handle_update_++;
break;
case FlowEvent::KSYNC_VROUTER_ERROR:
stats->vrouter_error_++;
break;
default:
break;
}
}
void FlowProto::SetProfileData(ProfileData *data) {
data->flow_.flow_count_ = FlowCount();
data->flow_.add_count_ = stats_.add_count_;
data->flow_.del_count_ = stats_.delete_count_;
data->flow_.audit_count_ = stats_.audit_count_;
data->flow_.reval_count_ = stats_.revaluate_count_;
data->flow_.handle_update_ = stats_.handle_update_;
data->flow_.vrouter_error_ = stats_.vrouter_error_;
}