/
bgp_config_ifmap.cc
2007 lines (1818 loc) · 69 KB
/
bgp_config_ifmap.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/bgp_config_ifmap.h"
#include <boost/foreach.hpp>
#include "base/task_annotations.h"
#include "bgp/bgp_config_listener.h"
#include "bgp/bgp_log.h"
#include "ifmap/ifmap_node.h"
#include "ifmap/ifmap_table.h"
#include "schema/bgp_schema_types.h"
#include "schema/vnc_cfg_types.h"
using namespace std;
using boost::iequals;
const int BgpIfmapConfigManager::kConfigTaskInstanceId = 0;
int BgpIfmapConfigManager::config_task_id_ = -1;
static BgpNeighborConfig::AddressFamilyList default_addr_family_list;
void DefaultAddressFamilyInit() {
default_addr_family_list.push_back("inet");
default_addr_family_list.push_back("inet-vpn");
}
MODULE_INITIALIZER(DefaultAddressFamilyInit);
static string IdentifierParent(const string &identifier) {
string parent;
size_t last;
last = identifier.rfind(':');
if (last == 0 || last == string::npos) {
return parent;
}
parent = identifier.substr(0, last);
return parent;
}
static uint32_t IpAddressToBgpIdentifier(const IpAddress &address) {
return htonl(address.to_v4().to_ulong());
}
static std::string BgpIdentifierToString(uint32_t identifier) {
Ip4Address addr(ntohl(identifier));
return addr.to_string();
}
BgpIfmapPeeringConfig::BgpIfmapPeeringConfig(BgpIfmapInstanceConfig *instance)
: instance_(instance) {
}
BgpIfmapPeeringConfig::~BgpIfmapPeeringConfig() {
STLDeleteElements(&neighbors_);
}
void BgpIfmapPeeringConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
if (proxy != NULL) {
node_proxy_.Swap(proxy);
name_ = node_proxy_.node()->name();
}
}
BgpIfmapRoutingPolicyLinkConfig::BgpIfmapRoutingPolicyLinkConfig(
BgpIfmapInstanceConfig *rti, BgpIfmapRoutingPolicyConfig *rtp) :
instance_(rti), policy_(rtp) {
}
BgpIfmapRoutingPolicyLinkConfig::~BgpIfmapRoutingPolicyLinkConfig() {
}
void BgpIfmapRoutingPolicyLinkConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
if (proxy != NULL) {
node_proxy_.Swap(proxy);
name_ = node_proxy_.node()->name();
}
}
bool BgpIfmapRoutingPolicyLinkConfig::GetRoutingInstanceRoutingPolicyPair(
DBGraph *graph, IFMapNode *node, pair<IFMapNode *, IFMapNode *> *pair) {
IFMapNode *routing_instance = NULL;
IFMapNode *routing_policy = NULL;
for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
if (strcmp(adj->table()->Typename(), "routing-instance") == 0)
routing_instance = adj;
if (strcmp(adj->table()->Typename(), "routing-policy") == 0)
routing_policy = adj;
}
if (routing_policy == NULL || routing_instance == NULL) {
return false;
}
pair->first = routing_instance;
pair->second = routing_policy;
return true;
}
void BgpIfmapRoutingPolicyLinkConfig::Update(BgpIfmapConfigManager *manager,
const autogen::RoutingInstanceRoutingPolicy *ri_rp) {
ri_rp_link_.reset(ri_rp);
}
void BgpIfmapRoutingPolicyLinkConfig::Delete(BgpIfmapConfigManager *manager) {
ri_rp_link_.reset();
}
static AuthenticationData::KeyType KeyChainType(const std::string &value) {
// Case-insensitive comparison
if (boost::iequals(value, "md5")) {
return AuthenticationData::MD5;
}
return AuthenticationData::NIL;
}
static void BuildKeyChain(BgpNeighborConfig *neighbor,
const autogen::AuthenticationData &values) {
AuthenticationData keydata;
keydata.set_key_type(KeyChainType(values.key_type));
AuthenticationKey key;
for (std::vector<autogen::AuthenticationKeyItem>::const_iterator iter =
values.key_items.begin(); iter != values.key_items.end(); ++iter) {
key.id = iter->key_id;
key.value = iter->key;
key.start_time = 0;
keydata.AddKeyToKeyChain(key);
}
neighbor->set_keydata(keydata);
}
//
// Check if the family is allowed to be configured for BgpNeighborConfig.
// Only families inet and inet6 are allowed on non-master instances.
//
static bool AddressFamilyIsValid(BgpNeighborConfig *neighbor,
const string &family) {
if (neighbor->instance_name() == BgpConfigManager::kMasterInstance)
return true;
return (family == "inet" || family == "inet6");
}
//
// Build list of BgpFamilyAttributesConfig elements from the list of address
// families. This is provided for backward compatibility with configurations
// that represent each family with a simple string.
//
static void BuildFamilyAttributesList(BgpNeighborConfig *neighbor,
const BgpNeighborConfig::AddressFamilyList &family_list) {
BgpNeighborConfig::FamilyAttributesList family_attributes_list;
BOOST_FOREACH(const string &family, family_list) {
if (!AddressFamilyIsValid(neighbor, family))
continue;
BgpFamilyAttributesConfig family_attributes(family);
family_attributes_list.push_back(family_attributes);
}
neighbor->set_family_attributes_list(family_attributes_list);
}
//
// Build list of BgpFamilyAttributesConfig elements from BgpFamilyAttributes
// list in BgpSessionAttributes.
//
// Implement backward compatibility by also adding BgpFamilyAttributesConfig
// elements for families that are not in BgpFamilyAttributes list but are in
// the address_families list.
//
static void BuildFamilyAttributesList(BgpNeighborConfig *neighbor,
const autogen::BgpSessionAttributes *attributes) {
set<string> family_set;
BgpNeighborConfig::FamilyAttributesList family_attributes_list;
BOOST_FOREACH(const autogen::BgpFamilyAttributes &family_config,
attributes->family_attributes) {
if (!AddressFamilyIsValid(neighbor, family_config.address_family))
continue;
BgpFamilyAttributesConfig family_attributes(
family_config.address_family);
family_attributes.loop_count = family_config.loop_count;
family_attributes.prefix_limit = family_config.prefix_limit.maximum;
family_attributes_list.push_back(family_attributes);
family_set.insert(family_config.address_family);
}
BOOST_FOREACH(const string &family, attributes->address_families.family) {
if (family_set.find(family) != family_set.end())
continue;
BgpFamilyAttributesConfig family_attributes(family);
family_attributes_list.push_back(family_attributes);
}
neighbor->set_family_attributes_list(family_attributes_list);
}
//
// Set the autogen::BgpSessionAttributes for this BgpNeighborConfig.
//
// The autogen::BgpSession will have up to 3 session attributes - one that
// applies to the local router, one that applies the remote router and one
// that applies to both.
//
static void NeighborSetSessionAttributes(
BgpNeighborConfig *neighbor, const string &localname,
const autogen::BgpSession *session) {
typedef vector<autogen::BgpSessionAttributes> AttributeVec;
const autogen::BgpSessionAttributes *common = NULL;
const autogen::BgpSessionAttributes *local = NULL;
for (AttributeVec::const_iterator iter = session->attributes.begin();
iter != session->attributes.end(); ++iter) {
const autogen::BgpSessionAttributes *attr = iter.operator->();
if (attr->bgp_router.empty()) {
common = attr;
} else if (neighbor->router_type() != "bgpaas-client" &&
attr->bgp_router == localname) {
local = attr;
} else if (neighbor->router_type() == "bgpaas-client" &&
attr->bgp_router == "bgpaas-server") {
local = attr;
}
}
// TODO: local should override rather than replace common.
const autogen::BgpSessionAttributes *attributes = NULL;
if (common != NULL) {
attributes = common;
} else if (local != NULL) {
attributes = local;
}
if (attributes != NULL) {
neighbor->set_passive(attributes->passive);
neighbor->set_loop_count(attributes->loop_count);
if (attributes->admin_down) {
neighbor->set_admin_down(true);
}
if (attributes->hold_time) {
neighbor->set_hold_time(attributes->hold_time);
}
BuildFamilyAttributesList(neighbor, attributes);
BuildKeyChain(neighbor, attributes->auth_data);
}
}
static BgpNeighborConfig *MakeBgpNeighborConfig(
const BgpIfmapInstanceConfig *instance,
const BgpIfmapInstanceConfig *master_instance,
const string &local_name,
const string &remote_name,
const autogen::BgpRouter *local_router,
const autogen::BgpRouter *remote_router,
const autogen::BgpSession *session) {
BgpNeighborConfig *neighbor = new BgpNeighborConfig();
neighbor->set_instance_name(instance->name());
// If the autogen::BgpSession has a uuid, we append it to the remote
// bgp-router's name to make the BgpNeighborConfig's name unique.
if (session && !session->uuid.empty()) {
neighbor->set_uuid(session->uuid);
neighbor->set_name(remote_name + ":" + session->uuid);
} else {
neighbor->set_name(remote_name);
}
// Store a copy of the remote bgp-router's autogen::BgpRouterParams and
// derive the autogen::BgpSessionAttributes for the session.
const autogen::BgpRouterParams ¶ms = remote_router->parameters();
neighbor->set_router_type(params.router_type);
if (params.admin_down) {
neighbor->set_admin_down(true);
}
if (params.local_autonomous_system) {
neighbor->set_peer_as(params.local_autonomous_system);
} else {
neighbor->set_peer_as(params.autonomous_system);
}
boost::system::error_code err;
neighbor->set_peer_address(
IpAddress::from_string(params.address, err));
if (err) {
BGP_LOG_STR(BgpConfig, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
"Invalid peer address " << params.address <<
" for neighbor " << neighbor->name());
}
Ip4Address identifier = Ip4Address::from_string(params.identifier, err);
if (err) {
BGP_LOG_STR(BgpConfig, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
"Invalid peer identifier " << params.identifier <<
" for neighbor " << neighbor->name());
}
neighbor->set_peer_identifier(IpAddressToBgpIdentifier(identifier));
neighbor->set_port(params.port);
neighbor->set_hold_time(params.hold_time);
if (session != NULL) {
NeighborSetSessionAttributes(neighbor, local_name, session);
}
// Get the local identifier and local as from the master protocol config.
const BgpIfmapProtocolConfig *master_protocol =
master_instance->protocol_config();
if (master_protocol && master_protocol->bgp_router()) {
const autogen::BgpRouterParams ¶ms =
master_protocol->router_params();
if (params.admin_down) {
neighbor->set_admin_down(true);
}
Ip4Address localid = Ip4Address::from_string(params.identifier, err);
if (err == 0) {
neighbor->set_local_identifier(IpAddressToBgpIdentifier(localid));
}
if (params.local_autonomous_system) {
neighbor->set_local_as(params.local_autonomous_system);
} else {
neighbor->set_local_as(params.autonomous_system);
}
if (instance != master_instance) {
neighbor->set_passive(true);
}
}
// Get other parameters from the instance protocol config.
// Note that there's no instance protocol config for non-master instances.
const BgpIfmapProtocolConfig *protocol = instance->protocol_config();
if (protocol && protocol->bgp_router()) {
const autogen::BgpRouterParams ¶ms = protocol->router_params();
if (neighbor->family_attributes_list().empty()) {
BuildFamilyAttributesList(neighbor, params.address_families.family);
}
if (neighbor->auth_data().Empty()) {
const autogen::BgpRouterParams &lp = local_router->parameters();
BuildKeyChain(neighbor, lp.auth_data);
}
}
if (neighbor->family_attributes_list().empty()) {
BuildFamilyAttributesList(neighbor, default_addr_family_list);
}
return neighbor;
}
//
// Build map of BgpNeighborConfigs based on the data in autogen::BgpPeering.
//
void BgpIfmapPeeringConfig::BuildNeighbors(BgpIfmapConfigManager *manager,
const autogen::BgpRouter *local_rt_config,
const string &peername, const autogen::BgpRouter *remote_rt_config,
const autogen::BgpPeering *peering, NeighborMap *map) {
const BgpIfmapInstanceConfig *master_instance =
manager->config()->FindInstance(BgpConfigManager::kMasterInstance);
// If there are one or more autogen::BgpSessions for the peering, use
// those to create the BgpNeighborConfigs.
const autogen::BgpPeeringAttributes &attr = peering->data();
for (autogen::BgpPeeringAttributes::const_iterator iter = attr.begin();
iter != attr.end(); ++iter) {
BgpNeighborConfig *neighbor = MakeBgpNeighborConfig(
instance_, master_instance, manager->localname(), peername,
local_rt_config, remote_rt_config, iter.operator->());
map->insert(make_pair(neighbor->name(), neighbor));
}
// When no sessions are present, create a single BgpNeighborConfig with
// no per-session configuration.
if (map->empty()) {
BgpNeighborConfig *neighbor = MakeBgpNeighborConfig(
instance_, master_instance, manager->localname(), peername,
local_rt_config, remote_rt_config, NULL);
map->insert(make_pair(neighbor->name(), neighbor));
}
}
//
// Update BgpIfmapPeeringConfig based on updated autogen::BgpPeering.
//
// This mainly involves building future BgpNeighborConfigs and doing a diff of
// the current and future BgpNeighborConfigs. Note that BgpIfmapInstanceConfig
// also has references to BgpNeighborConfigs, so it also needs to be updated as
// part of the process.
//
void BgpIfmapPeeringConfig::Update(BgpIfmapConfigManager *manager,
const autogen::BgpPeering *peering) {
IFMapNode *node = node_proxy_.node();
assert(node != NULL);
bgp_peering_.reset(peering);
// Build the future NeighborMap. The future map should be empty if the
// bgp-peering is deleted or if the parameters for the remote bgp-router
// are not available.
NeighborMap future;
pair<IFMapNode *, IFMapNode *> routers;
if (!node->IsDeleted() &&
GetRouterPair(manager->graph(), manager->localname(), node, &routers)) {
const autogen::BgpRouter *local_rt_config =
static_cast<const autogen::BgpRouter *>(
routers.first->GetObject());
const autogen::BgpRouter *remote_rt_config =
static_cast<const autogen::BgpRouter *>(
routers.second->GetObject());
if (local_rt_config &&
local_rt_config->IsPropertySet(autogen::BgpRouter::PARAMETERS) &&
remote_rt_config &&
remote_rt_config->IsPropertySet(autogen::BgpRouter::PARAMETERS)) {
BuildNeighbors(manager, local_rt_config, routers.second->name(),
remote_rt_config, peering, &future);
}
}
// Swap out the NeighborMap in preparation for doing a diff.
NeighborMap current;
current.swap(neighbors_);
// Do a diff on the current and future BgpNeighborConfigs, making sure
// that the BgpIfmapInstanceConfig is updated accordingly. We add any new
// BgpNeighborConfigs to our NeighborMap.
NeighborMap::iterator it1 = current.begin();
NeighborMap::iterator it2 = future.begin();
while (it1 != current.end() && it2 != future.end()) {
if (it1->first < it2->first) {
BgpNeighborConfig *prev = it1->second;
instance_->DeleteNeighbor(manager, prev);
++it1;
} else if (it1->first > it2->first) {
BgpNeighborConfig *neighbor = it2->second;
instance_->AddNeighbor(manager, neighbor);
neighbors_.insert(*it2);
it2->second = NULL;
++it2;
} else {
BgpNeighborConfig *neighbor = it1->second;
BgpNeighborConfig *update = it2->second;
if (*neighbor != *update) {
instance_->ChangeNeighbor(manager, update);
neighbors_.insert(*it2);
it2->second = NULL;
} else {
neighbors_.insert(*it1);
it1->second = NULL;
}
++it1;
++it2;
}
}
for (; it1 != current.end(); ++it1) {
BgpNeighborConfig *prev = it1->second;
instance_->DeleteNeighbor(manager, prev);
}
for (; it2 != future.end(); ++it2) {
BgpNeighborConfig *neighbor = it2->second;
instance_->AddNeighbor(manager, neighbor);
neighbors_.insert(*it2);
it2->second = NULL;
}
// Get rid of the current and future NeighborMaps and destroy any mapped
// BgpNeighborConfigs. Note that we have carefully reset mapped values to
// NULL above when we don't want a BgpNeighborConfig to get destroyed.
STLDeleteElements(¤t);
STLDeleteElements(&future);
}
//
// Delete all state for the given BgpPeeringConfig.
//
// This mainly involves getting rid of BgpNeighborConfigs in the NeighborMap.
//
void BgpIfmapPeeringConfig::Delete(BgpIfmapConfigManager *manager) {
NeighborMap current;
current.swap(neighbors_);
for (NeighborMap::iterator iter = current.begin();
iter != current.end(); ++iter) {
instance_->DeleteNeighbor(manager, iter->second);
}
STLDeleteElements(¤t);
bgp_peering_.reset();
}
//
// Find the IFMapNodes for a bgp-peering.
//
// The "node" is the IFMapNode for the bgp-peering link. The bgp-peering is
// interesting only if one of the bgp-routers is the local node.
//
// Return true if both bgp-routers for the bgp-peering exist and one of them
// is the local one. Also fill in the local and remote IFMapNode pointers if
// we return true.
//
bool BgpIfmapPeeringConfig::GetRouterPair(DBGraph *db_graph,
const string &localname, IFMapNode *node,
pair<IFMapNode *, IFMapNode *> *pair) {
IFMapNode *local = NULL;
IFMapNode *remote = NULL;
for (DBGraphVertex::adjacency_iterator iter = node->begin(db_graph);
iter != node->end(db_graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
if (strcmp(adj->table()->Typename(), "bgp-router") != 0)
continue;
autogen::BgpRouter *router =
static_cast<autogen::BgpRouter *>(adj->GetObject());
if (!router)
continue;
const autogen::BgpRouterParams ¶ms = router->parameters();
string instance_name(IdentifierParent(adj->name()));
string name = adj->name().substr(instance_name.size() + 1);
if (name == localname && params.router_type != "bgpaas-client") {
local = adj;
} else if (instance_name != BgpConfigManager::kMasterInstance &&
params.router_type == "bgpaas-server") {
local = adj;
} else {
remote = adj;
}
}
if (local == NULL || remote == NULL) {
return false;
}
pair->first = local;
pair->second = remote;
return true;
}
//
// Constructor for BgpIfmapProtocolConfig.
//
BgpIfmapProtocolConfig::BgpIfmapProtocolConfig(BgpIfmapInstanceConfig *instance)
: instance_(instance),
data_(instance->name()) {
}
//
// Destructor for BgpIfmapProtocolConfig.
//
BgpIfmapProtocolConfig::~BgpIfmapProtocolConfig() {
}
const autogen::BgpRouterParams &BgpIfmapProtocolConfig::router_params() const {
return bgp_router_->parameters();
}
//
// Set the IFMapNodeProxy for the BgpIfmapProtocolConfig.
//
void BgpIfmapProtocolConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
if (proxy != NULL) {
node_proxy_.Swap(proxy);
}
}
//
// Update autogen::BgpRouter object for this BgpIfmapProtocolConfig.
//
void BgpIfmapProtocolConfig::Update(BgpIfmapConfigManager *manager,
const autogen::BgpRouter *router) {
bgp_router_.reset(router);
const autogen::BgpRouterParams ¶ms = router->parameters();
data_.set_admin_down(params.admin_down);
data_.set_autonomous_system(params.autonomous_system);
data_.set_local_autonomous_system(params.local_autonomous_system);
boost::system::error_code err;
IpAddress identifier = IpAddress::from_string(params.identifier, err);
if (err == 0) {
data_.set_identifier(IpAddressToBgpIdentifier(identifier));
}
data_.set_hold_time(params.hold_time);
}
//
// Delete autogen::BgpRouter object for this BgpIfmapProtocolConfig.
//
void BgpIfmapProtocolConfig::Delete(BgpIfmapConfigManager *manager) {
manager->Notify(&data_, BgpConfigManager::CFG_DELETE);
bgp_router_.reset();
}
const string &BgpIfmapProtocolConfig::InstanceName() const {
return instance_->name();
}
//
// Constructor for BgpIfmapInstanceConfig.
//
BgpIfmapInstanceConfig::BgpIfmapInstanceConfig(const string &name)
: name_(name),
data_(name),
protocol_(NULL) {
}
//
// Destructor for BgpIfmapInstanceConfig.
//
BgpIfmapInstanceConfig::~BgpIfmapInstanceConfig() {
}
//
// Set the IFMapNodeProxy for the BgpIfmapInstanceConfig.
//
void BgpIfmapInstanceConfig::SetNodeProxy(IFMapNodeProxy *proxy) {
if (proxy != NULL) {
node_proxy_.Swap(proxy);
}
}
//
// Get the BgpIfmapProtocolConfig for this BgpIfmapInstanceConfig, create
// it if needed.
//
BgpIfmapProtocolConfig *BgpIfmapInstanceConfig::LocateProtocol() {
if (protocol_.get() == NULL) {
protocol_.reset(new BgpIfmapProtocolConfig(this));
}
return protocol_.get();
}
//
// Delete the BgpIfmapProtocolConfig for this BgpIfmapInstanceConfig.
//
void BgpIfmapInstanceConfig::ResetProtocol() {
protocol_.reset();
}
//
// Get the route-target for an instance-target. The input IFMapNode is the
// midnode that represents the instance-target link. We traverse the graph
// the graph edges till we find the adjacency to the route-target.
//
// Return true and fill in the target string if we find the route-target.
//
static bool GetInstanceTargetRouteTarget(DBGraph *graph, IFMapNode *node,
string *target) {
for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
if (strcmp(adj->table()->Typename(), "route-target") == 0) {
*target = adj->name();
return true;
}
}
return false;
}
//
// Fill in all the export route targets for a routing-instance. The input
// IFMapNode represents the routing-instance. We traverse the graph edges
// and look for instance-target adjacencies. If the instance-target has is
// an export target i.e. it's not import-only, add the route-target to the
// vector.
//
static void GetRoutingInstanceExportTargets(DBGraph *graph, IFMapNode *node,
vector<string> *target_list) {
for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
string target;
if ((strcmp(adj->table()->Typename(), "instance-target") == 0) &&
(GetInstanceTargetRouteTarget(graph, adj, &target))) {
const autogen::InstanceTarget *itarget =
dynamic_cast<autogen::InstanceTarget *>(adj->GetObject());
if (!itarget)
continue;
const autogen::InstanceTargetType &itt = itarget->data();
if (itt.import_export != "import")
target_list->push_back(target);
}
}
}
//
// Fill in all the routing-policies for a routing-instance. The input
// IFMapNode represents the routing-instance-routing-policy. We traverse to
// graph edges and look for routing-policy adjacency
//
static void GetRoutingInstanceRoutingPolicy(DBGraph *graph, IFMapNode *node,
RoutingPolicyAttachInfo *routing_policy) {
std::string sequence;
const autogen::RoutingInstanceRoutingPolicy *policy =
static_cast<autogen::RoutingInstanceRoutingPolicy *>(node->GetObject());
const autogen::RoutingPolicyType &attach_info = policy->data();
routing_policy->sequence_ = attach_info.sequence;
for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
if (strcmp(adj->table()->Typename(), "routing-policy") == 0) {
routing_policy->routing_policy_ = adj->name();
return;
}
}
}
//
// Get the network id for a virtual-network. The input IFMapNode represents
// the virtual-network.
//
static int GetVirtualNetworkIndex(DBGraph *graph, IFMapNode *node) {
const autogen::VirtualNetwork *vn =
static_cast<autogen::VirtualNetwork *>(node->GetObject());
if (vn && vn->IsPropertySet(autogen::VirtualNetwork::NETWORK_ID))
return vn->network_id();
if (vn && vn->IsPropertySet(autogen::VirtualNetwork::PROPERTIES))
return vn->properties().network_id;
return 0;
}
//
// Check if a virtual-network allows transit. The input IFMapNode represents
// the virtual-network.
//
static bool GetVirtualNetworkAllowTransit(DBGraph *graph, IFMapNode *node) {
const autogen::VirtualNetwork *vn =
static_cast<autogen::VirtualNetwork *>(node->GetObject());
if (vn && vn->IsPropertySet(autogen::VirtualNetwork::PROPERTIES))
return vn->properties().allow_transit;
return false;
}
//
// Get the vxlan id for a virtual-network. The input IFMapNode represents
// the virtual-network.
//
// The vxlan_network_identifier is 0 when automatic mode is in use. In that
// case, the network_id is used as vxlan id.
//
static int GetVirtualNetworkVxlanId(DBGraph *graph, IFMapNode *node) {
const autogen::VirtualNetwork *vn =
static_cast<autogen::VirtualNetwork *>(node->GetObject());
if (vn && vn->IsPropertySet(autogen::VirtualNetwork::PROPERTIES)) {
if (vn->properties().vxlan_network_identifier) {
return vn->properties().vxlan_network_identifier;
} else {
return vn->properties().network_id;
}
}
return 0;
}
static void SetStaticRouteConfig(BgpInstanceConfig *rti,
const autogen::RoutingInstance *config) {
BgpInstanceConfig::StaticRouteList inet_list;
BgpInstanceConfig::StaticRouteList inet6_list;
BOOST_FOREACH(const autogen::StaticRouteType &route,
config->static_route_entries()) {
boost::system::error_code ec;
StaticRouteConfig item;
item.nexthop = IpAddress::from_string(route.next_hop, ec);
if (ec != 0)
continue;
item.route_target = route.route_target;
if (item.nexthop.is_v4()) {
Ip4Address address;
ec = Ip4SubnetParse(route.prefix, &address, &item.prefix_length);
if (ec != 0)
continue;
item.address = address;
inet_list.push_back(item);
} else {
Ip6Address address;
ec = Inet6SubnetParse(route.prefix, &address, &item.prefix_length);
if (ec != 0)
continue;
item.address = address;
inet6_list.push_back(item);
}
}
rti->swap_static_routes(Address::INET, &inet_list);
rti->swap_static_routes(Address::INET6, &inet6_list);
}
static void SetServiceChainConfig(BgpInstanceConfig *rti,
const autogen::RoutingInstance *config) {
BgpInstanceConfig::ServiceChainList list;
const autogen::ServiceChainInfo &inet_chain =
config->service_chain_information();
if (config->IsPropertySet(
autogen::RoutingInstance::SERVICE_CHAIN_INFORMATION)) {
ServiceChainConfig item = {
Address::INET,
inet_chain.routing_instance,
inet_chain.prefix,
inet_chain.service_chain_address,
inet_chain.service_instance,
inet_chain.source_routing_instance
};
list.push_back(item);
}
const autogen::ServiceChainInfo &inet6_chain =
config->ipv6_service_chain_information();
if (config->IsPropertySet(
autogen::RoutingInstance::IPV6_SERVICE_CHAIN_INFORMATION)) {
ServiceChainConfig item = {
Address::INET6,
inet6_chain.routing_instance,
inet6_chain.prefix,
inet6_chain.service_chain_address,
inet6_chain.service_instance,
inet6_chain.source_routing_instance
};
list.push_back(item);
}
rti->swap_service_chain_list(&list);
}
void BgpIfmapConfigManager::ProcessRoutingPolicyLink(const BgpConfigDelta &delta) {
CHECK_CONCURRENCY("bgp::Config");
BgpIfmapRoutingPolicyLinkConfig *ri_rp_link
= config_->FindRoutingPolicyLink(delta.id_name);
if (ri_rp_link == NULL) {
IFMapNodeProxy *proxy = delta.node.get();
if (proxy == NULL) {
return;
}
IFMapNode *node = proxy->node();
if (node == NULL || delta.obj.get() == NULL) {
return;
}
pair<IFMapNode *, IFMapNode *> ri_rp_pair;
if (!BgpIfmapRoutingPolicyLinkConfig::GetRoutingInstanceRoutingPolicyPair(
db_graph_, node, &ri_rp_pair)) {
return;
}
std::string instance_name = ri_rp_pair.first->name();
std::string policy_name = ri_rp_pair.second->name();
BgpIfmapInstanceConfig *rti =
config_->FindInstance(instance_name);
BgpIfmapRoutingPolicyConfig *rtp =
config_->FindRoutingPolicy(policy_name);
if (!rti || !rtp) {
return;
}
ri_rp_link = config_->CreateRoutingPolicyLink(rti, rtp, proxy);
} else {
const IFMapNode *node = ri_rp_link->node();
assert(node != NULL);
if (delta.obj.get() == NULL) {
BgpIfmapRoutingPolicyConfig *rtp = ri_rp_link->policy();
BgpIfmapInstanceConfig *rti = ri_rp_link->instance();
config_->DeleteRoutingPolicyLink(ri_rp_link);
if (rtp->DeleteIfEmpty(this)) {
config_->DeleteRoutingPolicy(rtp);
}
if (rti->DeleteIfEmpty(this)) {
BGP_CONFIG_LOG_INSTANCE(Delete, server(), rti,
SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL);
config_->DeleteInstance(rti);
}
return;
}
}
autogen::RoutingInstanceRoutingPolicy *ri_rp_link_cfg =
static_cast<autogen::RoutingInstanceRoutingPolicy *>(delta.obj.get());
ri_rp_link->Update(this, ri_rp_link_cfg);
}
//
// Update BgpIfmapInstanceConfig based on a new autogen::RoutingInstance object.
//
// Rebuild the import and export route target lists and update the virtual
// network information.
//
// Targets that are configured on this routing-instance (which corresponds
// to all adjacencies to instance-target) are added to the import list or
// export list or both depending on the import_export attribute.
//
// Export targets for all other routing-instances that we are connected to
// are added to our import list.
//
void BgpIfmapInstanceConfig::Update(BgpIfmapConfigManager *manager,
const autogen::RoutingInstance *config) {
BgpInstanceConfig::RouteTargetList import_list, export_list;
BgpInstanceConfig::RoutingPolicyList policy_list;
data_.Clear();
DBGraph *graph = manager->graph();
IFMapNode *node = node_proxy_.node();
for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
if (strcmp(adj->table()->Typename(), "instance-target") == 0) {
string target;
if (GetInstanceTargetRouteTarget(graph, adj, &target)) {
const autogen::InstanceTarget *itarget =
dynamic_cast<autogen::InstanceTarget *>(adj->GetObject());
if (!itarget)
continue;
const autogen::InstanceTargetType &itt = itarget->data();
if (itt.import_export == "import") {
import_list.insert(target);
} else if (itt.import_export == "export") {
export_list.insert(target);
} else {
import_list.insert(target);
export_list.insert(target);
}
}
} else if (strcmp(adj->table()->Typename(),
"routing-instance-routing-policy") == 0) {
RoutingPolicyAttachInfo policy_info;
GetRoutingInstanceRoutingPolicy(graph, adj, &policy_info);
policy_list.push_back(policy_info);
} else if (strcmp(adj->table()->Typename(), "routing-instance") == 0) {
vector<string> target_list;
GetRoutingInstanceExportTargets(graph, adj, &target_list);
BOOST_FOREACH(string target, target_list) {
import_list.insert(target);
}
} else if (strcmp(adj->table()->Typename(), "virtual-network") == 0) {
data_.set_virtual_network(adj->name());
data_.set_virtual_network_index(GetVirtualNetworkIndex(graph, adj));
data_.set_virtual_network_allow_transit(
GetVirtualNetworkAllowTransit(graph, adj));
data_.set_vxlan_id(GetVirtualNetworkVxlanId(graph, adj));
}
}
data_.set_import_list(import_list);
data_.set_export_list(export_list);
data_.swap_routing_policy_list(&policy_list);
if (config) {
data_.set_has_pnf(config->has_pnf());
SetStaticRouteConfig(&data_, config);
SetServiceChainConfig(&data_, config);
}
}
//
// Reset IFMap related state in the BgpIfmapInstanceConfig.
//
void BgpIfmapInstanceConfig::ResetConfig() {
node_proxy_.Clear();
}
//
// Return true if the BgpIfmapInstanceConfig is ready to be deleted. The caller is
// responsible for actually deleting it.
//
bool BgpIfmapInstanceConfig::DeleteIfEmpty(BgpConfigManager *manager) {
if (name_ == BgpConfigManager::kMasterInstance) {
return false;
}
if (node() != NULL || protocol_.get() != NULL) {
return false;
}
if (!neighbors_.empty() || !peerings_.empty() ||
!routing_policies_.empty()) {
return false;
}
manager->Notify(&data_, BgpConfigManager::CFG_DELETE);
return true;
}
//
// Add a BgpNeighborConfig to this BgpIfmapInstanceConfig.
//
// The BgpNeighborConfig is added to the NeighborMap and the BgpConfigManager
// is notified.
//
void BgpIfmapInstanceConfig::AddNeighbor(BgpConfigManager *manager,
BgpNeighborConfig *neighbor) {
BGP_CONFIG_LOG_NEIGHBOR(
Create, manager->server(), neighbor, SandeshLevel::SYS_DEBUG,
BGP_LOG_FLAG_ALL,
neighbor->admin_down(),
neighbor->passive(),
BgpIdentifierToString(neighbor->local_identifier()),
neighbor->local_as(),
neighbor->peer_address().to_string(), neighbor->peer_as(),
neighbor->GetAddressFamilies(), neighbor->AuthKeyTypeToString(),
neighbor->AuthKeysToString());
neighbors_.insert(make_pair(neighbor->name(), neighbor));
manager->Notify(neighbor, BgpConfigManager::CFG_ADD);
}
//
// Change a BgpNeighborConfig that's already in this BgpIfmapInstanceConfig.
//
void BgpIfmapInstanceConfig::ChangeNeighbor(BgpConfigManager *manager,
BgpNeighborConfig *neighbor) {
NeighborMap::iterator loc = neighbors_.find(neighbor->name());
assert(loc != neighbors_.end());