-
Notifications
You must be signed in to change notification settings - Fork 390
/
service_chaining.cc
1064 lines (942 loc) · 41.4 KB
/
service_chaining.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/routing-instance/service_chaining.h"
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include "base/queue_task.h"
#include "base/task.h"
#include "base/task_annotations.h"
#include "bgp/bgp_condition_listener.h"
#include "bgp/bgp_config.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_path.h"
#include "bgp/bgp_peer.h"
#include "bgp/bgp_peer_membership.h"
#include "bgp/bgp_route.h"
#include "bgp/bgp_sandesh.h"
#include "bgp/bgp_server.h"
#include "bgp/bgp_table.h"
#include "bgp/l3vpn/inetvpn_route.h"
#include "bgp/origin-vn/origin_vn.h"
#include "bgp/routing-instance/routing_instance.h"
#include "db/db_table_partition.h"
#include "net/address.h"
using boost::system::error_code;
int ServiceChainMgr::service_chain_task_id_ = -1;
ServiceChain::ServiceChain(RoutingInstance *src, RoutingInstance *dest,
RoutingInstance *connected,
const std::vector<std::string> &subnets,
IpAddress addr)
: src_(src), dest_(dest), connected_(connected), connected_route_(NULL),
service_chain_addr_(addr), connected_table_unregistered_(false),
dest_table_unregistered_(false),
aggregate_(false), src_table_delete_ref_(this, src_table()->deleter()) {
for(std::vector<std::string>::const_iterator it = subnets.begin();
it != subnets.end(); it++) {
error_code ec;
Ip4Prefix ipam_subnet = Ip4Prefix::FromString(*it, &ec);
assert(ec == 0);
prefix_to_routelist_map_[ipam_subnet] = RouteList();
}
}
// Compare config and return whether cfg has updated
bool
ServiceChain::CompareServiceChainCfg(const autogen::ServiceChainInfo &cfg) {
if (cfg.routing_instance != dest_->name()) {
return false;
}
if (cfg.source_routing_instance != connected_->name()) {
return false;
}
if (service_chain_addr_.to_string() != cfg.service_chain_address) {
return false;
}
if (prefix_to_routelist_map_.size() != cfg.prefix.size()) {
return false;
}
for (std::vector<std::string>::const_iterator it = cfg.prefix.begin();
it != cfg.prefix.end(); it++) {
error_code ec;
Ip4Prefix ipam_subnet = Ip4Prefix::FromString(*it, &ec);
if (prefix_to_routelist_map_.find(ipam_subnet)
== prefix_to_routelist_map_.end()) {
return false;
}
}
return true;
}
static int GetOriginVnIndex(const BgpRoute *route) {
const BgpPath *path = route->BestPath();
if (!path)
return 0;
const BgpAttr *attr = path->GetAttr();
const ExtCommunity *ext_community = attr->ext_community();
if (!ext_community)
return 0;
BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
ext_community->communities()) {
if (!ExtCommunity::is_origin_vn(comm))
continue;
OriginVn origin_vn(comm);
return origin_vn.vn_index();
}
return 0;
}
// Match function called from BgpConditionListener
// Concurrency : db::DBTable
// For the purpose of route aggregation, two condition needs to be matched
// 1. More specific route present in any of the Dest BgpTable partition
// 2. Connected route(for nexthop) present in Src BgpTable
bool ServiceChain::Match(BgpServer *server, BgpTable *table,
BgpRoute *route, bool deleted) {
CHECK_CONCURRENCY("db::DBTable");
ServiceChainRequest::RequestType type;
Ip4Prefix aggregate_match;
if (table == dest_table() && !dest_table_unregistered()) {
if (is_connected_route(route)) {
return false;
}
if (is_aggregate(route)) {
return false;
}
if (aggregate_enable() && is_more_specific(route, &aggregate_match)) {
// More specific
if (deleted) {
type = ServiceChainRequest::MORE_SPECIFIC_DELETE;
} else {
type = ServiceChainRequest::MORE_SPECIFIC_ADD_CHG;
}
} else {
// External connecting routes
if (!deleted) {
if (!route->BestPath() || !route->BestPath()->IsFeasible()) {
deleted = true;
} else {
const BgpAttr *attr = route->BestPath()->GetAttr();
const Community *comm = attr ? attr->community() : NULL;
if (comm && comm->ContainsValue(Community::NoAdvertise))
deleted = true;
int vn_index = GetOriginVnIndex(route);
int src_vn_index = src_->virtual_network_index();
int dest_vn_index = dest_->virtual_network_index();
if (!vn_index || dest_vn_index != vn_index) {
if (src_vn_index == vn_index)
deleted = true;
if (!dest_->virtual_network_allow_transit())
deleted = true;
if (!dest_vn_index)
deleted = true;
}
}
}
if (deleted) {
type = ServiceChainRequest::EXT_CONNECT_ROUTE_DELETE;
} else {
type = ServiceChainRequest::EXT_CONNECT_ROUTE_ADD_CHG;
}
}
} else if ((table == connected_table()) &&
!connected_table_unregistered() &&
is_connected_route(route)) {
if (!deleted) {
if (!route->IsValid() ||
route->BestPath()->GetSource() != BgpPath::BGP_XMPP) {
deleted = true;
}
}
// Connected route for service chain
if (deleted) {
type = ServiceChainRequest::CONNECTED_ROUTE_DELETE;
} else {
type = ServiceChainRequest::CONNECTED_ROUTE_ADD_CHG;
}
} else {
return false;
}
BgpConditionListener *listener = server->condition_listener();
ServiceChainState *state =
static_cast<ServiceChainState *>(listener->GetMatchState(table, route,
this));
if (!deleted) {
// MatchState is added to the Route to ensure that DBEntry is not
// deleted before the ServiceChain module processes the WorkQueue
// request.
if (!state) {
state = new ServiceChainState(ServiceChainPtr(this));
listener->SetMatchState(table, route, this, state);
}
} else {
// MatchState is set on all the Routes that matches the conditions
// Retrieve to check and ignore delete of unseen Add Match
if (state == NULL) {
// Not seen ADD ignore DELETE
return false;
}
}
// The MatchState reference is taken to ensure that the route is not
// deleted when request is still present in the queue
// This is to handle the case where MatchState already exists and
// deleted entry gets reused or reused entry gets deleted.
state->IncrementRefCnt();
// Post the Match result to ServiceChain task to take Action
// More_Specific_Present + Connected_Route_exists ==> Add Aggregate Route
// and stitch the nexthop from connected route
ServiceChainRequest *req =
new ServiceChainRequest(type, table, route,
aggregate_match, ServiceChainPtr(this));
server->service_chain_mgr()->Enqueue(req);
return true;
}
bool ServiceChain::is_more_specific(BgpRoute *route,
Ip4Prefix *aggregate_match) {
unsigned long broadcast = 0xFFFFFFFF;
InetRoute *inet_route = dynamic_cast<InetRoute *>(route);
Ip4Address address = inet_route->GetPrefix().ip4_addr();
for(PrefixToRouteListMap::iterator it = prefix_to_route_list_map()->begin();
it != prefix_to_route_list_map()->end(); it++) {
unsigned long shift_mask = (32 - it->first.prefixlen());
if ((it->first.prefixlen() != inet_route->GetPrefix().prefixlen()) &&
((address.to_ulong() & (broadcast << shift_mask)) ==
(it->first.ip4_addr().to_ulong() & (broadcast << shift_mask)))) {
*aggregate_match = it->first;
return true;
}
}
return false;
}
bool ServiceChain::is_aggregate(BgpRoute *route) {
InetRoute *inet_route = dynamic_cast<InetRoute *>(route);
for (PrefixToRouteListMap::iterator it = prefix_to_route_list_map()->begin();
it != prefix_to_route_list_map()->end(); it++) {
if (it->first == inet_route->GetPrefix())
return true;
}
return false;
}
// RemoveServiceChainRoute
void ServiceChain::RemoveServiceChainRoute(Ip4Prefix prefix, bool aggregate) {
CHECK_CONCURRENCY("bgp::ServiceChain");
BgpTable *bgptable = src_table();
InetRoute rt_key(prefix);
DBTablePartition *partition =
static_cast<DBTablePartition *>(bgptable->GetTablePartition(&rt_key));
BgpRoute *service_chain_route =
static_cast<BgpRoute *>(partition->Find(&rt_key));
if (!service_chain_route || service_chain_route->IsDeleted())
return;
for (ConnectedPathIdList::const_iterator it = ConnectedPathIds().begin();
it != ConnectedPathIds().end(); ++it) {
uint32_t path_id = *it;
service_chain_route->RemovePath(BgpPath::ServiceChain, NULL, path_id);
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
"Removed " << (aggregate ? "Aggregate" : "ExtConnected") <<
" ServiceChain path " << service_chain_route->ToString() <<
" path_id " << BgpPath::PathIdString(path_id) <<
" in table " << bgptable->name());
}
if (!service_chain_route->BestPath()) {
partition->Delete(service_chain_route);
} else {
partition->Notify(service_chain_route);
}
}
// AddServiceChainRoute
void ServiceChain::AddServiceChainRoute(Ip4Prefix prefix,
const InetRoute *orig_route, const ConnectedPathIdList &old_path_ids,
bool aggregate) {
CHECK_CONCURRENCY("bgp::ServiceChain");
BgpTable *bgptable = src_table();
InetRoute rt_key(prefix);
DBTablePartition *partition =
static_cast<DBTablePartition *>(bgptable->GetTablePartition(&rt_key));
BgpRoute *service_chain_route =
static_cast<BgpRoute *>(partition->Find(&rt_key));
if (service_chain_route == NULL) {
service_chain_route = new InetRoute(prefix);
partition->Add(service_chain_route);
} else {
service_chain_route->ClearDelete();
}
int vn_index = dest_routing_instance()->virtual_network_index();
BgpServer *server = dest_routing_instance()->server();
OriginVn origin_vn(server->autonomous_system(), vn_index);
ExtCommunity::ExtCommunityList sgid_list;
if (orig_route) {
const BgpPath *orig_path = orig_route->BestPath();
const BgpAttr *orig_attr = NULL;
const ExtCommunity *ext_community = NULL;
if (orig_path)
orig_attr = orig_path->GetAttr();
if (orig_attr)
ext_community = orig_attr->ext_community();
if (ext_community) {
BOOST_FOREACH(const ExtCommunity::ExtCommunityValue &comm,
ext_community->communities()) {
if (ExtCommunity::is_security_group(comm))
sgid_list.push_back(comm);
}
}
}
BgpAttrDB *attr_db = src_->server()->attr_db();
ExtCommunityDB *extcomm_db = src_->server()->extcomm_db();
PeerRibMembershipManager *membership_mgr = src_->server()->membership_mgr();
ConnectedPathIdList new_path_ids;
for (Route::PathList::iterator it =
connected_route()->GetPathList().begin();
it != connected_route()->GetPathList().end(); ++it) {
BgpPath *connected_path = static_cast<BgpPath *>(it.operator->());
// Infeasible paths are not considered
if (!connected_path->IsFeasible())
break;
// take snapshot of all ECMP paths
if (connected_route()->BestPath()->PathCompare(*connected_path, true))
break;
// Skip paths with duplicate forwarding information. This ensures
// that we generate only one path with any given next hop and label
// when there are multiple connected paths from the original source
// received via different peers e.g. directly via XMPP and via BGP.
if (connected_route()->DuplicateForwardingPath(connected_path))
continue;
const BgpAttr *attr = connected_path->GetAttr();
ExtCommunityPtr new_ext_community;
// Strip any RouteTargets from the connected attributes.
new_ext_community = extcomm_db->ReplaceRTargetAndLocate(
attr->ext_community(), ExtCommunity::ExtCommunityList());
// Replace the SGID list with the list from the original route.
new_ext_community = extcomm_db->ReplaceSGIDListAndLocate(
new_ext_community.get(), sgid_list);
// Replace the OriginVn with the value from the original route
// or the value associated with the dest routing instance.
new_ext_community = extcomm_db->ReplaceOriginVnAndLocate(
new_ext_community.get(), origin_vn.GetExtCommunity());
BgpAttrPtr new_attr = attr_db->ReplaceExtCommunityAndLocate(
attr, new_ext_community);
// If the connected path is learnt via XMPP, construct RD based on
// the id registered with source table instead of connected table.
// This allows chaining of multiple in-network service instances
// that are on the same compute node.
const IPeer *peer = connected_path->GetPeer();
if (src_ != connected_ && peer && peer->IsXmppPeer()) {
int id = membership_mgr->GetRegistrationId(peer, bgptable);
if (id < 0)
continue;
RouteDistinguisher connected_rd = attr->source_rd();
if (connected_rd.Type() != RouteDistinguisher::TypeIpAddressBased)
continue;
RouteDistinguisher rd(connected_rd.GetAddress(), id);
new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(), rd);
}
// Replace the source rd if the connected path is a secondary path
// of a primary path in the l3vpn table. Use the RD of the primary.
if (connected_path->IsReplicated()) {
const BgpSecondaryPath *spath =
static_cast<const BgpSecondaryPath *>(connected_path);
const RoutingInstance *ri = spath->src_table()->routing_instance();
if (ri->IsDefaultRoutingInstance()) {
const InetVpnRoute *vpn_route =
static_cast<const InetVpnRoute *>(spath->src_rt());
new_attr = attr_db->ReplaceSourceRdAndLocate(new_attr.get(),
vpn_route->GetPrefix().route_distinguisher());
}
}
// Check whether we already have a path with the associated path id.
uint32_t path_id =
connected_path->GetAttr()->nexthop().to_v4().to_ulong();
BgpPath *existing_path =
service_chain_route->FindPath(BgpPath::ServiceChain, NULL,
path_id);
bool is_stale = false;
bool path_updated = false;
if (existing_path != NULL) {
// Existing path can be reused.
if ((new_attr.get() == existing_path->GetAttr()) &&
(connected_path->GetLabel() == existing_path->GetLabel())) {
new_path_ids.insert(path_id);
continue;
}
// Remove existing path, new path will be added below.
path_updated = true;
is_stale = existing_path->IsStale();
service_chain_route->RemovePath(
BgpPath::ServiceChain, NULL, path_id);
}
BgpPath *new_path =
new BgpPath(path_id, BgpPath::ServiceChain, new_attr.get(),
connected_path->GetFlags(), connected_path->GetLabel());
if (is_stale)
new_path->SetStale();
new_path_ids.insert(path_id);
service_chain_route->InsertPath(new_path);
partition->Notify(service_chain_route);
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
(path_updated ? "Updated " : "Added ") <<
(aggregate ? "Aggregate" : "ExtConnected") <<
" ServiceChain path " << service_chain_route->ToString() <<
" path_id " << BgpPath::PathIdString(path_id) <<
" in table " << bgptable->name());
}
// Remove stale paths.
for (ConnectedPathIdList::const_iterator it = old_path_ids.begin();
it != old_path_ids.end(); ++it) {
uint32_t path_id = *it;
if (new_path_ids.find(path_id) != new_path_ids.end())
continue;
service_chain_route->RemovePath(BgpPath::ServiceChain, NULL, path_id);
partition->Notify(service_chain_route);
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
"Removed " << (aggregate ? "Aggregate" : "ExtConnected") <<
" ServiceChain path " << service_chain_route->ToString() <<
" path_id " << BgpPath::PathIdString(path_id) <<
" in table " << bgptable->name());
}
// Delete the route if there's no paths.
if (!service_chain_route->BestPath())
partition->Delete(service_chain_route);
}
static void RemoveMatchState(BgpConditionListener *listener, ServiceChain *info,
BgpTable *table, BgpRoute *route,
ServiceChainState *state) {
if (info->deleted() || route->IsDeleted()) {
// At this point we are ready to release the MatchState on the DBEntry
// So mark it as deleted.. Actual removal of the state is done when
// ref count is 0
state->set_deleted();
}
}
static void FillServiceChainConfigInfo(ShowServicechainInfo &info,
const autogen::ServiceChainInfo &sci) {
info.set_dest_virtual_network(GetVNFromRoutingInstance(sci.routing_instance));
info.set_service_instance(sci.service_instance);
}
struct ServiceChainInfoComp {
bool operator()(const ShowServicechainInfo &lhs, const ShowServicechainInfo &rhs) {
std::string lnetwork;
std::string rnetwork;
if (lhs.src_virtual_network < lhs.dest_virtual_network) {
lnetwork = lhs.src_virtual_network + lhs.dest_virtual_network;
} else {
lnetwork = lhs.dest_virtual_network + lhs.src_virtual_network;
}
if (rhs.src_virtual_network < rhs.dest_virtual_network) {
rnetwork = rhs.src_virtual_network + rhs.dest_virtual_network;
} else {
rnetwork = rhs.dest_virtual_network + rhs.src_virtual_network;
}
if (lnetwork != rnetwork) {
return (lnetwork < rnetwork);
}
if (lhs.src_virtual_network != rhs.src_virtual_network) {
return (lhs.src_virtual_network < rhs.src_virtual_network);
}
if (lhs.service_instance != rhs.service_instance) {
return (lhs.service_instance < rhs.service_instance);
}
return false;
}
};
bool ServiceChainMgr::RequestHandler(ServiceChainRequest *req) {
CHECK_CONCURRENCY("bgp::ServiceChain");
BgpTable *table = NULL;
BgpRoute *route = NULL;
Ip4Prefix aggregate_match = req->aggregate_match_;
ServiceChain *info = NULL;
if (req->type_ != ServiceChainRequest::SHOW_SERVICE_CHAIN &&
req->type_ != ServiceChainRequest::SHOW_PENDING_CHAIN) {
table = req->table_;
route = req->rt_;
info = static_cast<ServiceChain *>(req->info_.get());
// Table where the aggregate route needs to be added
aggregate_match = req->aggregate_match_;
}
BgpConditionListener *listener = server()->condition_listener();
ServiceChainState *state = NULL;
if (route) {
state = static_cast<ServiceChainState *>
(listener->GetMatchState(table, route, info));
}
switch (req->type_) {
case ServiceChainRequest::MORE_SPECIFIC_ADD_CHG: {
assert(state);
if (state->deleted()) {
state->reset_deleted();
}
if (info->add_more_specific(aggregate_match, route) &&
info->connected_route_valid()) {
// Add the aggregate route
ServiceChain::ConnectedPathIdList path_ids;
info->AddServiceChainRoute(
aggregate_match, NULL, path_ids, true);
}
break;
}
case ServiceChainRequest::MORE_SPECIFIC_DELETE: {
assert(state);
if (info->delete_more_specific(aggregate_match, route)) {
// Delete the aggregate route
info->RemoveServiceChainRoute(aggregate_match, true);
}
RemoveMatchState(listener, info, table, route, state);
break;
}
case ServiceChainRequest::CONNECTED_ROUTE_ADD_CHG: {
assert(state);
if (route->IsDeleted() || !route->BestPath() ||
!route->BestPath()->IsFeasible()) {
break;
}
if (state->deleted()) {
state->reset_deleted();
}
// Store the old path id list and populate the new one.
ServiceChain::ConnectedPathIdList path_ids =
info->ConnectedPathIds();
info->set_connected_route(route);
ServiceChain::PrefixToRouteListMap *vnprefix_list =
info->prefix_to_route_list_map();
for (ServiceChain::PrefixToRouteListMap::iterator it =
vnprefix_list->begin(); it != vnprefix_list->end(); it++) {
// Add aggregate route.. Or if the route exists
// sync the path and purge old paths
if (!it->second.empty())
info->AddServiceChainRoute(it->first, NULL, path_ids, true);
}
for (ServiceChain::ExtConnectRouteList::iterator it =
info->ext_connecting_routes()->begin();
it != info->ext_connecting_routes()->end(); it++) {
// Add ServiceChain route for external connecting route
InetRoute *ext_route = static_cast<InetRoute *>(*it);
info->AddServiceChainRoute(
ext_route->GetPrefix(), ext_route, path_ids, false);
}
break;
}
case ServiceChainRequest::CONNECTED_ROUTE_DELETE: {
assert(state);
ServiceChain::PrefixToRouteListMap *vnprefix_list
= info->prefix_to_route_list_map();
for (ServiceChain::PrefixToRouteListMap::iterator it
= vnprefix_list->begin(); it != vnprefix_list->end(); it++) {
// Delete the aggregate route
info->RemoveServiceChainRoute(it->first, true);
}
for (ServiceChain::ExtConnectRouteList::iterator it =
info->ext_connecting_routes()->begin();
it != info->ext_connecting_routes()->end(); it++) {
// Delete ServiceChain route for external connecting route
InetRoute *ext_route = static_cast<InetRoute *>(*it);
info->RemoveServiceChainRoute(ext_route->GetPrefix(), false);
}
RemoveMatchState(listener, info, table, route, state);
info->set_connected_route(NULL);
break;
}
case ServiceChainRequest::EXT_CONNECT_ROUTE_ADD_CHG: {
assert(state);
if (state->deleted()) {
state->reset_deleted();
}
info->ext_connecting_routes()->insert(route);
if (info->connected_route_valid()) {
InetRoute *ext_route = dynamic_cast<InetRoute *>(route);
ServiceChain::ConnectedPathIdList path_ids;
info->AddServiceChainRoute(
ext_route->GetPrefix(), ext_route, path_ids, false);
}
break;
}
case ServiceChainRequest::EXT_CONNECT_ROUTE_DELETE: {
assert(state);
if (info->ext_connecting_routes()->erase(route)) {
InetRoute *inet_route = dynamic_cast<InetRoute *>(route);
info->RemoveServiceChainRoute(inet_route->GetPrefix(), false);
}
RemoveMatchState(listener, info, table, route, state);
break;
}
case ServiceChainRequest::UPDATE_ALL_ROUTES: {
if (info->dest_table_unregistered())
break;
if (info->connected_table_unregistered())
break;
if (!info->connected_route())
break;
ServiceChain::ConnectedPathIdList path_ids =
info->ConnectedPathIds();
ServiceChain::PrefixToRouteListMap *vnprefix_list =
info->prefix_to_route_list_map();
for (ServiceChain::PrefixToRouteListMap::iterator it =
vnprefix_list->begin(); it != vnprefix_list->end(); it++) {
// Add aggregate route.. Or if the route exists
// sync the path and purge old paths
if (!it->second.empty())
info->AddServiceChainRoute(
it->first, NULL, path_ids, true);
}
for (ServiceChain::ExtConnectRouteList::iterator it =
info->ext_connecting_routes()->begin();
it != info->ext_connecting_routes()->end(); it++) {
// Add ServiceChain route for external connecting route
InetRoute *ext_route = static_cast<InetRoute *>(*it);
info->AddServiceChainRoute(
ext_route->GetPrefix(), ext_route, path_ids, false);
}
break;
}
case ServiceChainRequest::STOP_CHAIN_DONE: {
if (table == info->connected_table()) {
info->set_connected_table_unregistered();
if (!info->num_matchstate()) {
listener->UnregisterCondition(table, info);
}
}
if (table == info->dest_table()) {
info->set_dest_table_unregistered();
if (!info->num_matchstate()) {
listener->UnregisterCondition(table, info);
}
}
if (info->unregistered()) {
chain_set_.erase(info->src_routing_instance());
StartResolve();
}
break;
}
case ServiceChainRequest::SHOW_SERVICE_CHAIN: {
ShowServiceChainResp *resp =
static_cast<ShowServiceChainResp *>(req->snh_resp_);
std::vector<ShowServicechainInfo> list;
RoutingInstanceMgr::RoutingInstanceIterator rit =
server()->routing_instance_mgr()->begin();
for (;rit != server()->routing_instance_mgr()->end(); rit++) {
if (rit->deleted()) continue;
ShowServicechainInfo info;
const autogen::RoutingInstance *rti = rit->config()->instance_config();
if (rti && rti->IsPropertySet(autogen::RoutingInstance::SERVICE_CHAIN_INFORMATION)) {
info.set_src_virtual_network(rit->virtual_network());
const autogen::ServiceChainInfo &sci = rti->service_chain_information();
FillServiceChainConfigInfo(info, sci);
ServiceChain *chain = FindServiceChain(rit->name());
if (chain) {
if (chain->deleted())
info.set_state("Deleted");
else
info.set_state("Active");
chain->FillServiceChainInfo(info);
} else {
info.set_state("Pending");
}
list.push_back(info);
}
}
ServiceChainInfoComp comp;
std::sort(list.begin(), list.end(), comp);
resp->set_service_chain_list(list);
resp->Response();
break;
}
case ServiceChainRequest::SHOW_PENDING_CHAIN: {
ShowPendingServiceChainResp *resp =
static_cast<ShowPendingServiceChainResp *>(req->snh_resp_);
std::vector<std::string> pending_list;
for(UnresolvedServiceChainList::const_iterator it
= pending_chains().begin(); it != pending_chains().end(); it++)
pending_list.push_back((*it)->name());
resp->set_pending_chains(pending_list);
resp->Response();
break;
}
default: {
assert(0);
break;
}
}
if (state) {
state->DecrementRefCnt();
if (state->refcnt() == 0 && state->deleted()) {
listener->RemoveMatchState(table, route, info);
delete state;
if (!info->num_matchstate()) {
if (info->dest_table_unregistered()) {
listener->UnregisterCondition(info->dest_table(), info);
}
if (info->connected_table_unregistered()) {
listener->UnregisterCondition(info->connected_table(), info);
}
if (info->unregistered()) {
chain_set_.erase(info->src_routing_instance());
StartResolve();
}
}
}
}
delete req;
return true;
}
ServiceChainMgr::ServiceChainMgr(BgpServer *server) : server_(server),
resolve_trigger_(new TaskTrigger(
boost::bind(&ServiceChainMgr::ResolvePendingServiceChain, this),
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0)),
aggregate_host_route_(false) {
if (service_chain_task_id_ == -1) {
TaskScheduler *scheduler = TaskScheduler::GetInstance();
service_chain_task_id_ = scheduler->GetTaskId("bgp::ServiceChain");
}
process_queue_ =
new WorkQueue<ServiceChainRequest *>(service_chain_task_id_, 0,
boost::bind(&ServiceChainMgr::RequestHandler, this, _1));
id_ = server->routing_instance_mgr()->RegisterInstanceOpCallback(
boost::bind(&ServiceChainMgr::RoutingInstanceCallback, this, _1, _2));
PeerRibMembershipManager *membership_mgr = server->membership_mgr();
registration_id_ = membership_mgr->RegisterPeerRegistrationCallback(
boost::bind(&ServiceChainMgr::PeerRegistrationCallback, this, _1, _2, _3));
}
ServiceChainMgr::~ServiceChainMgr() {
delete process_queue_;
server()->routing_instance_mgr()->UnregisterInstanceOpCallback(id_);
PeerRibMembershipManager *membership_mgr = server()->membership_mgr();
membership_mgr->UnregisterPeerRegistrationCallback(registration_id_);
}
void ServiceChainMgr::Enqueue(ServiceChainRequest *req) {
process_queue_->Enqueue(req);
}
bool ServiceChainMgr::LocateServiceChain(RoutingInstance *rtinstance,
const autogen::ServiceChainInfo &cfg) {
CHECK_CONCURRENCY("bgp::Config");
// Verify whether the entry already exists
ServiceChainMap::iterator it = chain_set_.find(rtinstance);
if (it != chain_set_.end()) {
ServiceChain *chain = static_cast<ServiceChain *>(it->second.get());
if (chain->CompareServiceChainCfg(cfg)) {
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_TRACE,
"No update in ServiceChain config : " << rtinstance->name());
return true;
}
// Entry already exists. Update of match condition
// The routing instance to pending resolve such that
// service chain is created after stop done cb
AddPendingServiceChain(rtinstance);
if (it->second->deleted()) {
// Wait for the delete complete cb
return false;
}
BgpConditionListener::RequestDoneCb callback =
boost::bind(&ServiceChainMgr::StopServiceChainDone, this, _1, _2);
server()->condition_listener()->RemoveMatchCondition(
chain->dest_table(), it->second.get(), callback);
server()->condition_listener()->RemoveMatchCondition(
chain->connected_table(), it->second.get(), callback);
return true;
}
RoutingInstanceMgr *mgr = server()->routing_instance_mgr();
RoutingInstance *dest = mgr->GetRoutingInstance(cfg.routing_instance);
// Destination routing instance is not yet created.
if (dest == NULL || dest->deleted()) {
// Wait for the creation of RoutingInstance
AddPendingServiceChain(rtinstance);
return false;
}
RoutingInstance *connected_ri = NULL;
if (cfg.source_routing_instance == "") {
connected_ri = rtinstance;
assert(!rtinstance->deleted());
} else {
connected_ri = mgr->GetRoutingInstance(cfg.source_routing_instance);
}
// routing instance to search for connected route is not yet created.
if (connected_ri == NULL || connected_ri->deleted()) {
// Wait for the creation of RoutingInstance where connected route
// will be published
AddPendingServiceChain(rtinstance);
return false;
}
// Get the service chain address
error_code ec;
IpAddress chain_addr =
Ip4Address::from_string(cfg.service_chain_address, ec);
assert(ec == 0);
// Get the BGP Tables to add condition
BgpTable *connected_table = connected_ri->GetTable(Address::INET);
assert(connected_table);
BgpTable *dest_table = dest->GetTable(Address::INET);
assert(dest_table);
// Allocate the new service chain and verify whether one already exists
ServiceChainPtr chain
= ServiceChainPtr(new ServiceChain(rtinstance, dest, connected_ri,
cfg.prefix, chain_addr));
if (aggregate_host_route()) {
ServiceChain *obj = static_cast<ServiceChain *>(chain.get());
obj->set_aggregate_enable();
}
// Add the new service chain request
chain_set_.insert(std::make_pair(rtinstance, chain));
server()->condition_listener()->AddMatchCondition(connected_table, chain.get(),
BgpConditionListener::RequestDoneCb());
server()->condition_listener()->AddMatchCondition(dest_table, chain.get(),
BgpConditionListener::RequestDoneCb());
return true;
}
bool ServiceChainMgr::ResolvePendingServiceChain() {
for (UnresolvedServiceChainList::iterator it = pending_chain_.begin(), next;
it != pending_chain_.end(); it = next) {
next = it;
++next;
RoutingInstance *rtinst = *it;
pending_chain_.erase(it);
LocateServiceChain(rtinst,
rtinst->config()->instance_config()->service_chain_information());
}
return true;
}
void ServiceChainMgr::RoutingInstanceCallback(std::string name, int op) {
if (op == RoutingInstanceMgr::INSTANCE_ADD) StartResolve();
}
void ServiceChainMgr::StartResolve() {
if (pending_chain_.empty() == false) {
resolve_trigger_->Set();
}
}
void ServiceChainMgr::StopServiceChainDone(BgpTable *table,
ConditionMatch *info) {
// Post the RequestDone event to ServiceChain task to take Action
ServiceChainRequest *req =
new ServiceChainRequest(ServiceChainRequest::STOP_CHAIN_DONE, table,
NULL, Ip4Prefix(), ServiceChainPtr(info));
server()->service_chain_mgr()->Enqueue(req);
return;
}
void ServiceChainMgr::StopServiceChain(RoutingInstance *src) {
// Remove the src routing instance from the pending_chain_
pending_chain_.erase(src);
ServiceChainMap::iterator it = chain_set_.find(src);
if (it == chain_set_.end()) {
return;
}
if (it->second->deleted()) {
return;
}
BgpConditionListener::RequestDoneCb callback =
boost::bind(&ServiceChainMgr::StopServiceChainDone, this, _1, _2);
ServiceChain *obj = static_cast<ServiceChain *>(it->second.get());
server()->condition_listener()->RemoveMatchCondition(obj->dest_table(), obj,
callback);
server()->condition_listener()->RemoveMatchCondition(obj->connected_table(), obj,
callback);
}
void ServiceChainMgr::PeerRegistrationCallback(IPeer *peer, BgpTable *table,
bool unregister) {
CHECK_CONCURRENCY("bgp::PeerMembership");
// Bail if it's not an XMPP peer.
if (!peer->IsXmppPeer())
return;
// Bail if there's no service chain for the instance.
ServiceChain *chain = FindServiceChain(table->routing_instance());
if (!chain)
return;
// Post event to ServiceChain task to update all routes.
ServiceChainRequest *req =
new ServiceChainRequest(ServiceChainRequest::UPDATE_ALL_ROUTES, NULL,
NULL, Ip4Prefix(), ServiceChainPtr(chain));
Enqueue(req);
}
void ServiceChain::FillServiceChainInfo(ShowServicechainInfo &info) const {
info.set_src_rt_instance(src_routing_instance()->name());
info.set_connected_rt_instance(connected_routing_instance()->name());
info.set_dest_rt_instance(dest_routing_instance()->name());
ConnectedRouteInfo connected_rt_info;
connected_rt_info.set_service_chain_addr(service_chain_addr().to_string());
if (connected_route()) {
ShowRoute show_route;
connected_route()->FillRouteInfo(connected_table(), &show_route);
connected_rt_info.set_connected_rt(show_route);
}
info.set_connected_route(connected_rt_info);
std::vector<PrefixToRouteListInfo> more_vec;
for(PrefixToRouteListMap::const_iterator it =
prefix_to_route_list_map().begin();
it != prefix_to_route_list_map().end(); it++) {
PrefixToRouteListInfo prefix_list_info;
prefix_list_info.set_prefix(it->first.ToString());
BgpTable *bgptable = src_table();
InetRoute rt_key(it->first);
BgpRoute *aggregate = static_cast<BgpRoute *>(bgptable->Find(&rt_key));
if (aggregate) {
prefix_list_info.set_aggregate(true);
ShowRoute show_route;
aggregate->FillRouteInfo(bgptable, &show_route);
prefix_list_info.set_aggregate_rt(show_route);
} else {
prefix_list_info.set_aggregate(false);
}
std::vector<std::string> rt_list;
for (RouteList::iterator rt_it = it->second.begin();
rt_it != it->second.end(); rt_it++) {
rt_list.push_back((*rt_it)->ToString());
}
prefix_list_info.set_more_specific_list(rt_list);
more_vec.push_back(prefix_list_info);
}
info.set_more_specifics(more_vec);
std::vector<ExtConnectRouteInfo> ext_connecting_rt_info_list;
for(ExtConnectRouteList::const_iterator it = ext_connecting_routes().begin();
it != ext_connecting_routes().end(); it++) {
ExtConnectRouteInfo ext_rt_info;
ext_rt_info.set_ext_rt_prefix((*it)->ToString());
BgpTable *bgptable = src_table();