/
flow_mgmt.cc
1619 lines (1378 loc) · 53.2 KB
/
flow_mgmt.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <bitset>
#include <boost/uuid/uuid_io.hpp>
#include "cmn/agent.h"
#include "controller/controller_init.h"
#include "oper/bgp_as_service.h"
#include "pkt/flow_proto.h"
#include "pkt/flow_mgmt.h"
#include "pkt/flow_mgmt_request.h"
#include "pkt/flow_mgmt_dbclient.h"
#include "uve/flow_ace_stats_request.h"
#include "uve/agent_uve_stats.h"
#include "vrouter/flow_stats/flow_stats_collector.h"
const string FlowMgmtManager::kFlowMgmtTask = "Flow::Management";
/////////////////////////////////////////////////////////////////////////////
// FlowMgmtManager methods
/////////////////////////////////////////////////////////////////////////////
FlowMgmtManager::FlowMgmtManager(Agent *agent) :
agent_(agent),
acl_flow_mgmt_tree_(this),
interface_flow_mgmt_tree_(this),
vn_flow_mgmt_tree_(this),
ip4_route_flow_mgmt_tree_(this),
ip6_route_flow_mgmt_tree_(this),
bridge_route_flow_mgmt_tree_(this),
vrf_flow_mgmt_tree_(this),
nh_flow_mgmt_tree_(this),
flow_mgmt_dbclient_(new FlowMgmtDbClient(agent, this)),
request_queue_(agent_->task_scheduler()->GetTaskId(kFlowMgmtTask), 0,
boost::bind(&FlowMgmtManager::RequestHandler, this, _1)),
db_event_queue_(agent_->task_scheduler()->GetTaskId(kFlowMgmtTask), 0,
boost::bind(&FlowMgmtManager::DBRequestHandler, this, _1),
db_event_queue_.kMaxSize, 1),
log_queue_(agent_->task_scheduler()->GetTaskId(kFlowMgmtTask), 1,
boost::bind(&FlowMgmtManager::LogHandler, this, _1)) {
request_queue_.set_name("Flow management");
db_event_queue_.set_name("Flow DB Event Queue");
log_queue_.set_name("Flow Log Queue");
for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++) {
bgp_as_a_service_flow_mgmt_tree_[count].reset(
new BgpAsAServiceFlowMgmtTree(this));
}
}
void FlowMgmtManager::Init() {
flow_mgmt_dbclient_->Init();
agent_->acl_table()->set_ace_flow_sandesh_data_cb
(boost::bind(&FlowMgmtManager::SetAceSandeshData, this, _1, _2, _3));
agent_->acl_table()->set_acl_flow_sandesh_data_cb
(boost::bind(&FlowMgmtManager::SetAclFlowSandeshData, this, _1, _2,
_3));
// If BGP service is deleted then flush off all the flows for the VMI.
agent_->oper_db()->bgp_as_a_service()->RegisterServiceDeleteCb(boost::bind
(&FlowMgmtManager::BgpAsAServiceNotify, this, _1, _2));
// If control node goes off delete all flows frmo its tree.
agent_->controller()->RegisterControllerChangeCallback(boost::bind
(&FlowMgmtManager::ControllerNotify, this, _1));
}
void FlowMgmtManager::Shutdown() {
request_queue_.Shutdown();
db_event_queue_.Shutdown();
log_queue_.Shutdown();
flow_mgmt_dbclient_->Shutdown();
}
/////////////////////////////////////////////////////////////////////////////
// BGP as a service callbacks
/////////////////////////////////////////////////////////////////////////////
void FlowMgmtManager::BgpAsAServiceNotify(const boost::uuids::uuid &vm_uuid,
uint32_t source_port) {
FlowMgmtRequestPtr req(new BgpAsAServiceFlowMgmtRequest(vm_uuid,
source_port));
request_queue_.Enqueue(req);
}
void FlowMgmtManager::ControllerNotify(uint8_t index) {
FlowMgmtRequestPtr req(new BgpAsAServiceFlowMgmtRequest(index));
request_queue_.Enqueue(req);
}
/////////////////////////////////////////////////////////////////////////////
// Introspect routines
/////////////////////////////////////////////////////////////////////////////
void FlowMgmtManager::SetAceSandeshData(const AclDBEntry *acl,
AclFlowCountResp &data,
int ace_id) {
AclFlowMgmtKey key(acl, NULL);
AclFlowMgmtEntry *entry = static_cast<AclFlowMgmtEntry *>
(acl_flow_mgmt_tree_.Find(&key));
if (entry == NULL) {
return;
}
entry->FillAceFlowSandeshInfo(acl, data, ace_id);
}
void FlowMgmtManager::SetAclFlowSandeshData(const AclDBEntry *acl,
AclFlowResp &data,
const int last_count) {
AclFlowMgmtKey key(acl, NULL);
AclFlowMgmtEntry *entry = static_cast<AclFlowMgmtEntry *>
(acl_flow_mgmt_tree_.Find(&key));
if (entry == NULL) {
return;
}
entry->FillAclFlowSandeshInfo(acl, data, last_count, agent_);
}
/////////////////////////////////////////////////////////////////////////////
// Utility methods to enqueue events into work-queue
/////////////////////////////////////////////////////////////////////////////
void FlowMgmtManager::AddEvent(FlowEntry *flow) {
FlowEntryPtr flow_ptr(flow);
FlowMgmtRequestPtr req(new FlowMgmtRequest(FlowMgmtRequest::ADD_FLOW,
flow_ptr));
request_queue_.Enqueue(req);
}
void FlowMgmtManager::DeleteEvent(FlowEntry *flow,
const RevFlowDepParams ¶ms) {
FlowEntryPtr flow_ptr(flow);
FlowMgmtRequestPtr req(new FlowMgmtRequest(FlowMgmtRequest::DELETE_FLOW,
flow_ptr, params));
request_queue_.Enqueue(req);
}
void FlowMgmtManager::FlowStatsUpdateEvent(FlowEntry *flow, uint32_t bytes,
uint32_t packets,
uint32_t oflow_bytes) {
if (bytes == 0 && packets == 0 && oflow_bytes == 0) {
return;
}
/* Ignore StatsUpdate request in TSN mode as we don't export flows */
if (agent_->tsn_enabled()) {
return;
}
FlowEntryPtr flow_ptr(flow);
FlowMgmtRequestPtr req(new FlowMgmtRequest
(FlowMgmtRequest::UPDATE_FLOW_STATS, flow_ptr,
bytes, packets, oflow_bytes));
request_queue_.Enqueue(req);
}
void FlowMgmtManager::RetryVrfDeleteEvent(const VrfEntry *vrf) {
FlowMgmtRequestPtr req(new FlowMgmtRequest
(FlowMgmtRequest::RETRY_DELETE_VRF, vrf, 0));
request_queue_.Enqueue(req);
}
void FlowMgmtManager::DummyEvent() {
FlowMgmtRequestPtr req(new FlowMgmtRequest(FlowMgmtRequest::DUMMY));
request_queue_.Enqueue(req);
}
void FlowMgmtManager::AddDBEntryEvent(const DBEntry *entry, uint32_t gen_id) {
FlowMgmtRequestPtr req(new FlowMgmtRequest(FlowMgmtRequest::ADD_DBENTRY,
entry, gen_id));
db_event_queue_.Enqueue(req);
}
void FlowMgmtManager::ChangeDBEntryEvent(const DBEntry *entry,
uint32_t gen_id) {
FlowMgmtRequestPtr req(new FlowMgmtRequest(FlowMgmtRequest::CHANGE_DBENTRY,
entry, gen_id));
db_event_queue_.Enqueue(req);
}
void FlowMgmtManager::DeleteDBEntryEvent(const DBEntry *entry,
uint32_t gen_id) {
FlowMgmtRequestPtr req(new FlowMgmtRequest(FlowMgmtRequest::DELETE_DBENTRY,
entry, gen_id));
db_event_queue_.Enqueue(req);
}
void FlowMgmtManager::EnqueueFlowEvent(FlowEvent *event) {
agent_->pkt()->get_flow_proto()->EnqueueFlowEvent(event);
}
void FlowMgmtManager::NonOperEntryEvent(FlowEvent::Event event,
FlowEntry *flow) {
FlowEvent *flow_resp = new FlowEvent(event, flow->key(), true,
FlowTable::kPortNatFlowTableInstance);
flow_resp->set_flow(flow);
EnqueueFlowEvent(flow_resp);
}
void FlowMgmtManager::DBEntryEvent(FlowEvent::Event event, FlowMgmtKey *key,
FlowEntry *flow) {
FlowEvent *flow_resp = new FlowEvent(event, NULL, key->db_entry());
key->KeyToFlowRequest(flow_resp);
flow_resp->set_flow(flow);
EnqueueFlowEvent(flow_resp);
}
void FlowMgmtManager::FreeDBEntryEvent(FlowEvent::Event event, FlowMgmtKey *key,
uint32_t gen_id) {
FlowEvent *flow_resp = new FlowEvent(event, key->db_entry(), gen_id);
EnqueueFlowEvent(flow_resp);
}
void FlowMgmtManager::FlowUpdateQueueDisable(bool disabled) {
request_queue_.set_disable(disabled);
db_event_queue_.set_disable(disabled);
}
size_t FlowMgmtManager::FlowUpdateQueueLength() {
return request_queue_.Length();
}
size_t FlowMgmtManager::FlowDBQueueLength() {
return db_event_queue_.Length();
}
/////////////////////////////////////////////////////////////////////////////
// Handlers for events from the work-queue
/////////////////////////////////////////////////////////////////////////////
static bool ProcessEvent(FlowMgmtRequest *req, FlowMgmtKey *key,
FlowMgmtTree *tree) {
switch (req->event()) {
case FlowMgmtRequest::ADD_DBENTRY:
tree->OperEntryAdd(req, key);
break;
case FlowMgmtRequest::CHANGE_DBENTRY:
tree->OperEntryChange(req, key);
break;
case FlowMgmtRequest::DELETE_DBENTRY:
tree->OperEntryDelete(req, key);
break;
default:
assert(0);
break;
}
return true;
}
bool FlowMgmtManager::DBRequestHandler(FlowMgmtRequest *req,
const DBEntry *entry) {
const Interface *intf = dynamic_cast<const Interface *>(entry);
if (intf) {
InterfaceFlowMgmtKey key(intf);
return ProcessEvent(req, &key, &interface_flow_mgmt_tree_);
}
const VnEntry *vn = dynamic_cast<const VnEntry *>(entry);
if (vn) {
VnFlowMgmtKey key(vn);
return ProcessEvent(req, &key, &vn_flow_mgmt_tree_);
}
const AclDBEntry *acl = dynamic_cast<const AclDBEntry *>(entry);
if (acl) {
AclFlowMgmtKey key(acl, NULL);
return ProcessEvent(req, &key, &acl_flow_mgmt_tree_);
}
const NextHop *nh = dynamic_cast<const NextHop *>(entry);
if (nh) {
NhFlowMgmtKey key(static_cast<const NextHop *>(req->db_entry()));
return ProcessEvent(req, &key, &nh_flow_mgmt_tree_);
}
const InetUnicastRouteEntry *inet_uc_rt =
dynamic_cast<const InetUnicastRouteEntry *>(entry);
if (inet_uc_rt) {
InetRouteFlowMgmtKey key(inet_uc_rt);
if (inet_uc_rt->addr().is_v4()) {
return ProcessEvent(req, &key, &ip4_route_flow_mgmt_tree_);
}
if (inet_uc_rt->addr().is_v6()) {
return ProcessEvent(req, &key, &ip6_route_flow_mgmt_tree_);
}
}
const BridgeRouteEntry *bridge =
dynamic_cast<const BridgeRouteEntry *>(entry);
if (bridge) {
BridgeRouteFlowMgmtKey key(bridge);
return ProcessEvent(req, &key, &bridge_route_flow_mgmt_tree_);
}
const VrfEntry *vrf = dynamic_cast<const VrfEntry *>(entry);
if (vrf) {
VrfFlowMgmtKey key(vrf);
return ProcessEvent(req, &key, &vrf_flow_mgmt_tree_);
}
assert(0);
return true;
}
/////////////////////////////////////////////////////////////////////////////
// Bgp as a service flow management
/////////////////////////////////////////////////////////////////////////////
bool BgpAsAServiceFlowMgmtEntry::NonOperEntryDelete(FlowMgmtManager *mgr,
const FlowMgmtRequest *req,
FlowMgmtKey *key) {
oper_state_ = OPER_DEL_SEEN;
gen_id_ = req->gen_id();
FlowEvent::Event event = req->GetResponseEvent();
if (event == FlowEvent::INVALID)
return false;
Tree::iterator it = tree_.begin();
while (it != tree_.end()) {
mgr->NonOperEntryEvent(event, *it);
it++;
}
return true;
}
void BgpAsAServiceFlowMgmtTree::FreeNotify(FlowMgmtKey *key, uint32_t gen_id) {
assert(key->db_entry() == NULL);
}
void BgpAsAServiceFlowMgmtTree::ExtractKeys(FlowEntry *flow,
FlowMgmtKeyTree *tree) {
if (flow->is_flags_set(FlowEntry::BgpRouterService) == false)
return;
const VmInterface *vm_intf =
dynamic_cast<const VmInterface *>(flow->intf_entry());
if (!vm_intf || (flow->bgp_as_a_service_port() == 0))
return;
BgpAsAServiceFlowMgmtKey *key =
new BgpAsAServiceFlowMgmtKey(vm_intf->GetUuid(),
flow->bgp_as_a_service_port());
AddFlowMgmtKey(tree, key);
}
FlowMgmtEntry *BgpAsAServiceFlowMgmtTree::Allocate(const FlowMgmtKey *key) {
return new BgpAsAServiceFlowMgmtEntry();
}
bool BgpAsAServiceFlowMgmtTree::BgpAsAServiceDelete
(BgpAsAServiceFlowMgmtKey &key, const FlowMgmtRequest *req) {
FlowMgmtEntry *entry = Find(&key);
if (entry == NULL) {
return true;
}
entry->NonOperEntryDelete(mgr_, req, &key);
return TryDelete(&key, entry);
}
void BgpAsAServiceFlowMgmtTree::DeleteAll() {
Tree::iterator it = tree_.begin();
while (it != tree_.end()) {
BgpAsAServiceFlowMgmtKey *key =
static_cast<BgpAsAServiceFlowMgmtKey *>(it->first);
mgr_->BgpAsAServiceNotify(key->uuid(), key->source_port());
it++;
}
}
bool
FlowMgmtManager::BgpAsAServiceRequestHandler(FlowMgmtRequest *req) {
BgpAsAServiceFlowMgmtRequest *bgp_as_a_service_request =
dynamic_cast<BgpAsAServiceFlowMgmtRequest *>(req);
if (bgp_as_a_service_request->type() == BgpAsAServiceFlowMgmtRequest::VMI) {
BgpAsAServiceFlowMgmtKey key(bgp_as_a_service_request->vm_uuid(),
bgp_as_a_service_request->source_port());
//Delete it for for all CN trees
for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++) {
bgp_as_a_service_flow_mgmt_tree_[count].get()->
BgpAsAServiceDelete(key, req);
}
} else if (bgp_as_a_service_request->type() ==
BgpAsAServiceFlowMgmtRequest::CONTROLLER) {
bgp_as_a_service_flow_mgmt_tree_[bgp_as_a_service_request->index()].get()->
DeleteAll();
}
return true;
}
bool FlowMgmtManager::RequestHandler(FlowMgmtRequestPtr req) {
switch (req->event()) {
case FlowMgmtRequest::ADD_FLOW: {
log_queue_.Enqueue(req);
//Handle the Add request for flow-mgmt
AddFlow(req->flow());
break;
}
case FlowMgmtRequest::DELETE_FLOW: {
log_queue_.Enqueue(req);
//Handle the Delete request for flow-mgmt
DeleteFlow(req->flow(), req->params());
break;
}
case FlowMgmtRequest::UPDATE_FLOW_STATS: {
//Handle Flow stats update for flow-mgmt
UpdateFlowStats(req->flow(), req->bytes(), req->packets(),
req->oflow_bytes());
break;
}
case FlowMgmtRequest::RETRY_DELETE_VRF: {
RetryVrfDelete(req->vrf_id());
break;
}
case FlowMgmtRequest::DELETE_BGP_AAS_FLOWS: {
BgpAsAServiceRequestHandler(req.get());
break;
}
case FlowMgmtRequest::DUMMY:
break;
default:
assert(0);
}
return true;
}
bool FlowMgmtManager::DBRequestHandler(FlowMgmtRequestPtr req) {
switch (req->event()) {
case FlowMgmtRequest::ADD_DBENTRY:
case FlowMgmtRequest::CHANGE_DBENTRY:
case FlowMgmtRequest::DELETE_DBENTRY: {
DBRequestHandler(req.get(), req->db_entry());
break;
}
default:
assert(0);
}
return true;
}
bool FlowMgmtManager::LogHandler(FlowMgmtRequestPtr req) {
FlowEntry *flow = req->flow().get();
tbb::mutex::scoped_lock mutex(flow->mutex());
switch (req->event()) {
case FlowMgmtRequest::ADD_FLOW: {
LogFlowUnlocked(flow, "ADD");
//Enqueue Add request to flow-stats-collector
agent_->flow_stats_manager()->AddEvent(req->flow());
//Enqueue Add request to UVE module for ACE stats
EnqueueUveAddEvent(flow);
break;
}
case FlowMgmtRequest::DELETE_FLOW: {
LogFlowUnlocked(flow, "DEL");
//Enqueue Delete request to flow-stats-collector
agent_->flow_stats_manager()->DeleteEvent(flow, req->params());
//Enqueue Delete request to UVE module for ACE stats
EnqueueUveDeleteEvent(flow);
break;
}
default:
assert(0);
}
return true;
}
void FlowMgmtManager::RetryVrfDelete(uint32_t vrf_id) {
vrf_flow_mgmt_tree_.RetryDelete(vrf_id);
}
// Extract all the FlowMgmtKey for a flow
void FlowMgmtManager::LogFlowUnlocked(FlowEntry *flow, const std::string &op) {
if (flow->trace() == false)
return;
FlowInfo trace;
flow->FillFlowInfo(trace);
FLOW_TRACE(Trace, op, trace);
}
// Extract all the FlowMgmtKey for a flow
void FlowMgmtManager::MakeFlowMgmtKeyTree(FlowEntry *flow,
FlowMgmtKeyTree *tree) {
tbb::mutex::scoped_lock mutex(flow->mutex());
acl_flow_mgmt_tree_.ExtractKeys(flow, tree);
interface_flow_mgmt_tree_.ExtractKeys(flow, tree);
vn_flow_mgmt_tree_.ExtractKeys(flow, tree);
ip4_route_flow_mgmt_tree_.ExtractKeys(flow, tree);
ip6_route_flow_mgmt_tree_.ExtractKeys(flow, tree);
bridge_route_flow_mgmt_tree_.ExtractKeys(flow, tree);
nh_flow_mgmt_tree_.ExtractKeys(flow, tree);
for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++) {
bgp_as_a_service_flow_mgmt_tree_[count].get()->
ExtractKeys(flow, tree);
}
}
void FlowMgmtManager::EnqueueUveAddEvent(const FlowEntry *flow) const {
AgentUveStats *uve = dynamic_cast<AgentUveStats *>(agent_->uve());
if (uve) {
const Interface *itf = flow->intf_entry();
const VmInterface *vmi = dynamic_cast<const VmInterface *>(itf);
const VnEntry *vn = flow->vn_entry();
string vn_name = vn? vn->GetName() : "";
string itf_name = vmi? vmi->cfg_name() : "";
if ((!itf_name.empty() && !flow->sg_rule_uuid().empty()) ||
(!vn_name.empty() && !flow->nw_ace_uuid().empty())) {
boost::shared_ptr<FlowAceStatsRequest> req(new FlowAceStatsRequest
(FlowAceStatsRequest::ADD_FLOW, flow->uuid(), itf_name,
flow->sg_rule_uuid(), vn_name, flow->nw_ace_uuid()));
uve->stats_manager()->EnqueueEvent(req);
}
}
}
void FlowMgmtManager::EnqueueUveDeleteEvent(const FlowEntry *flow) const {
AgentUveStats *uve = dynamic_cast<AgentUveStats *>(agent_->uve());
if (uve) {
boost::shared_ptr<FlowAceStatsRequest> req(new FlowAceStatsRequest
(FlowAceStatsRequest::DELETE_FLOW, flow->uuid()));
uve->stats_manager()->EnqueueEvent(req);
}
}
void FlowMgmtManager::AddFlow(FlowEntryPtr &flow) {
// Trace the flow add/change
FlowMgmtKeyTree new_tree;
MakeFlowMgmtKeyTree(flow.get(), &new_tree);
// Get old FlowMgmtKeyTree
FlowEntryInfo *old_info = LocateFlowEntryInfo(flow);
FlowMgmtKeyTree *old_tree = &old_info->tree_;
assert(old_tree);
old_info->count_++;
// Apply the difference in old and new key tree
FlowMgmtKeyTree::iterator new_it = new_tree.begin();
FlowMgmtKeyTree::iterator old_it = old_tree->begin();
while (new_it != new_tree.end() && old_it != old_tree->end()) {
FlowMgmtKey *new_key = *new_it;
FlowMgmtKey *old_key = *old_it;
if (new_key->IsLess(old_key)) {
AddFlowMgmtKey(flow.get(), old_info, new_key, NULL);
new_it++;
} else if (old_key->IsLess(new_key)) {
DeleteFlowMgmtKey(flow.get(), old_info, old_key);
FlowMgmtKeyTree::iterator tmp = old_it++;
FlowMgmtKey *key = *tmp;
old_tree->erase(tmp);
delete key;
} else {
AddFlowMgmtKey(flow.get(), old_info, new_key, old_key);
old_it++;
new_it++;
}
}
while (new_it != new_tree.end()) {
FlowMgmtKey *new_key = *new_it;
AddFlowMgmtKey(flow.get(), old_info, new_key, NULL);
new_it++;
}
while (old_it != old_tree->end()) {
FlowMgmtKey *old_key = *old_it;
DeleteFlowMgmtKey(flow.get(), old_info, old_key);
FlowMgmtKeyTree::iterator tmp = old_it++;
FlowMgmtKey *key = *tmp;
old_tree->erase(tmp);
delete key;
}
new_it = new_tree.begin();
while (new_it != new_tree.end()) {
FlowMgmtKeyTree::iterator tmp = new_it++;
FlowMgmtKey *key = *tmp;
new_tree.erase(tmp);
delete key;
}
}
void FlowMgmtManager::DeleteFlow(FlowEntryPtr &flow,
const RevFlowDepParams ¶ms) {
// Delete entries for flow from the tree
FlowEntryInfo *old_info = FindFlowEntryInfo(flow);
if (old_info == NULL)
return;
FlowMgmtKeyTree *old_tree = &old_info->tree_;
assert(old_tree);
old_info->count_++;
FlowMgmtKeyTree::iterator old_it = old_tree->begin();
while (old_it != old_tree->end()) {
DeleteFlowMgmtKey(flow.get(), old_info, *old_it);
FlowMgmtKeyTree::iterator tmp = old_it++;
FlowMgmtKey *key = *tmp;
old_tree->erase(tmp);
delete key;
}
assert(old_tree->size() == 0);
DeleteFlowEntryInfo(flow);
}
void FlowMgmtManager::UpdateFlowStats(FlowEntryPtr &flow, uint32_t bytes,
uint32_t packets, uint32_t oflow_bytes) {
//Enqueue Flow Index Update Event request to flow-stats-collector
agent_->flow_stats_manager()->UpdateStatsEvent(flow, bytes, packets,
oflow_bytes);
}
bool FlowMgmtManager::HasVrfFlows(uint32_t vrf_id) {
if (ip4_route_flow_mgmt_tree_.HasVrfFlows(vrf_id, Agent::INET4_UNICAST)) {
return true;
}
if (ip6_route_flow_mgmt_tree_.HasVrfFlows(vrf_id, Agent::INET6_UNICAST)) {
return true;
}
if (bridge_route_flow_mgmt_tree_.HasVrfFlows(vrf_id, Agent::BRIDGE)) {
return true;
}
return false;
}
void FlowMgmtManager::VnFlowCounters(const VnEntry *vn, uint32_t *ingress_flow_count,
uint32_t *egress_flow_count) {
vn_flow_mgmt_tree_.VnFlowCounters(vn, ingress_flow_count,
egress_flow_count);
}
FlowMgmtManager::FlowEntryInfo *
FlowMgmtManager::FindFlowEntryInfo(const FlowEntryPtr &flow) {
FlowEntryTree::iterator it = flow_tree_.find(flow);
if (it == flow_tree_.end()) {
return NULL;
}
return &it->second;
}
FlowMgmtManager::FlowEntryInfo *
FlowMgmtManager::LocateFlowEntryInfo(FlowEntryPtr &flow) {
FlowEntryInfo *info = FindFlowEntryInfo(flow);
if (info != NULL)
return info;
flow_tree_.insert(make_pair(flow, FlowEntryInfo()));
return FindFlowEntryInfo(flow);
}
void FlowMgmtManager::DeleteFlowEntryInfo(FlowEntryPtr &flow) {
FlowEntryTree::iterator it = flow_tree_.find(flow);
if (it == flow_tree_.end())
return;
assert(it->second.tree_.size() == 0);
flow_tree_.erase(it);
return;
}
/////////////////////////////////////////////////////////////////////////////
// Routines to add/delete Flow and FlowMgmtKey in different trees
/////////////////////////////////////////////////////////////////////////////
// Add a FlowMgmtKey into FlowMgmtKeyTree for an object
// The FlowMgmtKeyTree for object is passed as argument
void FlowMgmtManager::AddFlowMgmtKey(FlowEntry *flow, FlowEntryInfo *info,
FlowMgmtKey *key, FlowMgmtKey *old_key) {
FlowMgmtKey *tmp = key->Clone();
std::pair<FlowMgmtKeyTree::iterator, bool> ret = info->tree_.insert(tmp);
if (ret.second == false) {
delete tmp;
if (key->type() == FlowMgmtKey::ACL) {
/* Copy the ACE Id list to existing key from new Key */
FlowMgmtKey *existing_key = *(ret.first);
AclFlowMgmtKey *akey = static_cast<AclFlowMgmtKey *>(existing_key);
AclFlowMgmtKey *new_key = static_cast<AclFlowMgmtKey *>(key);
akey->set_ace_id_list(new_key->ace_id_list());
}
}
switch (key->type()) {
case FlowMgmtKey::INTERFACE:
interface_flow_mgmt_tree_.Add(key, flow);
break;
case FlowMgmtKey::ACL:
acl_flow_mgmt_tree_.Add(key, flow, old_key);
break;
case FlowMgmtKey::VN: {
bool new_flow = vn_flow_mgmt_tree_.Add(key, flow);
VnFlowMgmtEntry *entry = static_cast<VnFlowMgmtEntry *>
(vn_flow_mgmt_tree_.Find(key));
entry->UpdateCounterOnAdd(flow, new_flow, info->local_flow_,
info->ingress_);
info->local_flow_ = flow->is_flags_set(FlowEntry::LocalFlow);
info->ingress_ = flow->is_flags_set(FlowEntry::IngressDir);
break;
}
case FlowMgmtKey::INET4:
ip4_route_flow_mgmt_tree_.Add(key, flow);
break;
case FlowMgmtKey::INET6:
ip6_route_flow_mgmt_tree_.Add(key, flow);
break;
case FlowMgmtKey::BRIDGE:
bridge_route_flow_mgmt_tree_.Add(key, flow);
break;
case FlowMgmtKey::NH:
nh_flow_mgmt_tree_.Add(key, flow);
break;
case FlowMgmtKey::BGPASASERVICE:
for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++)
bgp_as_a_service_flow_mgmt_tree_[count].get()->Add(key, flow);
break;
default:
assert(0);
}
}
// Delete a FlowMgmtKey from FlowMgmtKeyTree for an object
// The FlowMgmtKeyTree for object is passed as argument
void FlowMgmtManager::DeleteFlowMgmtKey(FlowEntry *flow, FlowEntryInfo *info,
FlowMgmtKey *key) {
FlowMgmtKeyTree::iterator it = info->tree_.find(key);
assert(it != info->tree_.end());
switch (key->type()) {
case FlowMgmtKey::INTERFACE:
interface_flow_mgmt_tree_.Delete(key, flow);
break;
case FlowMgmtKey::ACL:
acl_flow_mgmt_tree_.Delete(key, flow);
break;
case FlowMgmtKey::VN: {
vn_flow_mgmt_tree_.Delete(key, flow);
VnFlowMgmtEntry *entry = static_cast<VnFlowMgmtEntry *>
(vn_flow_mgmt_tree_.Find(key));
if (entry)
entry->UpdateCounterOnDel(flow, info->local_flow_, info->ingress_);
info->local_flow_ = flow->is_flags_set(FlowEntry::LocalFlow);
info->ingress_ = flow->is_flags_set(FlowEntry::IngressDir);
break;
}
case FlowMgmtKey::INET4:
ip4_route_flow_mgmt_tree_.Delete(key, flow);
break;
case FlowMgmtKey::INET6:
ip6_route_flow_mgmt_tree_.Delete(key, flow);
break;
case FlowMgmtKey::BRIDGE:
bridge_route_flow_mgmt_tree_.Delete(key, flow);
break;
case FlowMgmtKey::NH:
nh_flow_mgmt_tree_.Delete(key, flow);
break;
case FlowMgmtKey::BGPASASERVICE:
for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++)
bgp_as_a_service_flow_mgmt_tree_[count].get()->Delete(key, flow);
break;
default:
assert(0);
}
}
/////////////////////////////////////////////////////////////////////////////
// FlowMgmtKey routines
/////////////////////////////////////////////////////////////////////////////
// Event to be enqueued to free an object
FlowEvent::Event FlowMgmtKey::FreeDBEntryEvent() const {
FlowEvent::Event event = FlowEvent::INVALID;
switch (type_) {
case INTERFACE:
case ACL:
case VN:
case INET4:
case INET6:
case BRIDGE:
case NH:
case VRF:
event = FlowEvent::FREE_DBENTRY;
break;
case ACE_ID:
case VM:
event = FlowEvent::INVALID;
break;
case BGPASASERVICE:
event = FlowEvent::INVALID;
default:
assert(0);
}
return event;
}
/////////////////////////////////////////////////////////////////////////////
// Routines on FlowMgmtTree structure within the FlowMgmtManager
// Generic code for all FlowMgmtTrees
/////////////////////////////////////////////////////////////////////////////
FlowMgmtEntry *FlowMgmtTree::Find(FlowMgmtKey *key) {
Tree::iterator it = tree_.find(key);
if (it == tree_.end())
return NULL;
return it->second;
}
FlowMgmtEntry *FlowMgmtTree::Locate(FlowMgmtKey *key) {
FlowMgmtEntry *entry = Find(key);
if (entry == NULL) {
entry = Allocate(key);
tree_[key->Clone()] = entry;
}
return entry;
}
FlowMgmtKey *FlowMgmtTree::LowerBound(FlowMgmtKey *key) {
Tree::iterator it = tree_.lower_bound(key);
if (it == tree_.end())
return NULL;
return it->first;
}
bool FlowMgmtTree::TryDelete(FlowMgmtKey *key, FlowMgmtEntry *entry) {
if (entry->CanDelete() == false)
return false;
// Send message only if we have seen DELETE message from FlowTable
if (entry->oper_state() == FlowMgmtEntry::OPER_DEL_SEEN) {
FreeNotify(key, entry->gen_id());
}
Tree::iterator it = tree_.find(key);
assert(it != tree_.end());
FlowMgmtKey *first = it->first;
tree_.erase(it);
delete entry;
delete first;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// Generic Event handler on tree for add/delete of a flow
/////////////////////////////////////////////////////////////////////////////
bool FlowMgmtTree::AddFlowMgmtKey(FlowMgmtKeyTree *tree, FlowMgmtKey *key) {
std::pair<FlowMgmtKeyTree::iterator, bool> ret = tree->insert(key);
if (ret.second == false)
delete key;
return ret.second;
}
// Adds Flow to a FlowMgmtEntry defined by key. Does not allocate FlowMgmtEntry
// if its not already present
bool FlowMgmtTree::Add(FlowMgmtKey *key, FlowEntry *flow) {
FlowMgmtEntry *entry = Locate(key);
if (entry == NULL) {
return false;
}
return entry->Add(flow);
}
bool FlowMgmtTree::Delete(FlowMgmtKey *key, FlowEntry *flow) {
Tree::iterator it = tree_.find(key);
if (it == tree_.end()) {
return false;
}
FlowMgmtEntry *entry = it->second;
bool ret = entry->Delete(flow);
TryDelete(it->first, entry);
return ret;
}
/////////////////////////////////////////////////////////////////////////////
// Event handler for add/delete/change of an object
/////////////////////////////////////////////////////////////////////////////
// Send DBEntry Free message to DB Client module
void FlowMgmtTree::FreeNotify(FlowMgmtKey *key, uint32_t gen_id) {
assert(key->db_entry() != NULL);
FlowEvent::Event event = key->FreeDBEntryEvent();
if (event == FlowEvent::INVALID)
return;
mgr_->FreeDBEntryEvent(event, key, gen_id);
}
// An object is added/updated. Enqueue REVALUATE for flows dependent on it
bool FlowMgmtTree::OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key) {
FlowMgmtEntry *entry = Locate(key);
entry->OperEntryAdd(mgr_, req, key);
return true;
}
bool FlowMgmtTree::OperEntryChange(const FlowMgmtRequest *req,
FlowMgmtKey *key) {
FlowMgmtEntry *entry = Find(key);
if (entry) {
entry->OperEntryChange(mgr_, req, key);
}
return true;
}
// Send DELETE Entry message to FlowTable module
bool FlowMgmtTree::OperEntryDelete(const FlowMgmtRequest *req,
FlowMgmtKey *key) {
FlowMgmtEntry *entry = Find(key);
if (entry == NULL) {
FreeNotify(key, req->gen_id());
return true;
}
entry->OperEntryDelete(mgr_, req, key);
return TryDelete(key, entry);
}
bool FlowMgmtTree::RetryDelete(FlowMgmtKey *key) {
FlowMgmtEntry *entry = Find(key);
if (entry == NULL) {
return true;
}
return TryDelete(key, entry);
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Object Entry code
/////////////////////////////////////////////////////////////////////////////
bool FlowMgmtEntry::Add(FlowEntry *flow) {
std::pair<Tree::iterator, bool> ret = tree_.insert(flow);
return ret.second;
}
bool FlowMgmtEntry::Delete(FlowEntry *flow) {
tree_.erase(flow);
return tree_.size();
}
// An entry *cannot* be deleted if
// - It contains flows
// - It has seen ADD but not seen any DELETE
bool FlowMgmtEntry::CanDelete() const {
assert(oper_state_ != INVALID);
if (tree_.size())
return false;
return (oper_state_ != OPER_ADD_SEEN);
}
// Handle Add/Change event for DBEntry
bool FlowMgmtEntry::OperEntryAdd(FlowMgmtManager *mgr,
const FlowMgmtRequest *req, FlowMgmtKey *key) {
oper_state_ = OPER_ADD_SEEN;
FlowEvent::Event event = req->GetResponseEvent();
if (event == FlowEvent::INVALID)
return false;
Tree::iterator it = tree_.begin();
while (it != tree_.end()) {
mgr->DBEntryEvent(event, key, *it);
it++;
}