/
vn.cc
1297 lines (1138 loc) · 43.9 KB
/
vn.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 <algorithm>
#include <boost/uuid/uuid_io.hpp>
#include <cmn/agent_cmn.h>
#include <base/parse_object.h>
#include <base/util.h>
#include <ifmap/ifmap_link.h>
#include <ifmap/ifmap_table.h>
#include <vnc_cfg_types.h>
#include <agent_types.h>
#include <cfg/cfg_init.h>
#include <cfg/cfg_interface.h>
#include <cfg/cfg_mirror.h>
#include <cfg/cfg_listener.h>
#include <oper/route_common.h>
#include <oper/interface_common.h>
#include <oper/vn.h>
#include <oper/nexthop.h>
#include <oper/mpls.h>
#include <oper/mirror_table.h>
#include <oper/agent_sandesh.h>
#include <oper/oper_dhcp_options.h>
#include <oper/physical_device_vn.h>
#include <filter/acl.h>
#include "net/address_util.h"
using namespace autogen;
using namespace std;
using namespace boost;
using boost::assign::map_list_of;
using boost::assign::list_of;
VnTable *VnTable::vn_table_;
VnIpam::VnIpam(const std::string& ip, uint32_t len, const std::string& gw,
const std::string& dns, bool dhcp, std::string &name,
const autogen::IpamType &ipam_type,
const std::vector<autogen::DhcpOptionType> &dhcp_options,
const std::vector<autogen::RouteType> &host_routes)
: plen(len), installed(false), dhcp_enable(dhcp), ipam_name(name),
ipam(ipam_type) {
boost::system::error_code ec;
ip_prefix = IpAddress::from_string(ip, ec);
default_gw = IpAddress::from_string(gw, ec);
dns_server = IpAddress::from_string(dns, ec);
oper_dhcp_options.set_options(dhcp_options);
oper_dhcp_options.set_host_routes(host_routes);
}
Ip4Address VnIpam::GetBroadcastAddress() const {
if (ip_prefix.is_v4()) {
Ip4Address broadcast(ip_prefix.to_v4().to_ulong() |
~(0xFFFFFFFF << (32 - plen)));
return broadcast;
}
return Ip4Address(0);
}
Ip4Address VnIpam::GetSubnetAddress() const {
if (ip_prefix.is_v4()) {
return Address::GetIp4SubnetAddress(ip_prefix.to_v4(), plen);
}
return Ip4Address(0);
}
Ip6Address VnIpam::GetV6SubnetAddress() const {
if (ip_prefix.is_v6()) {
return Address::GetIp6SubnetAddress(ip_prefix.to_v6(), plen);
}
return Ip6Address();
}
bool VnIpam::IsSubnetMember(const IpAddress &ip) const {
if (ip_prefix.is_v4() && ip.is_v4()) {
return ((ip_prefix.to_v4().to_ulong() |
~(0xFFFFFFFF << (32 - plen))) ==
(ip.to_v4().to_ulong() | ~(0xFFFFFFFF << (32 - plen))));
} else if (ip_prefix.is_v6() && ip.is_v6()) {
return IsIp6SubnetMember(ip.to_v6(), ip_prefix.to_v6(), plen);
}
return false;
}
VnEntry::VnEntry(Agent *agent, uuid id) :
AgentOperDBEntry(), uuid_(id), vxlan_id_(0), vnid_(0),
bridging_(true), layer3_forwarding_(true), admin_state_(true),
table_label_(0), enable_rpf_(true), flood_unknown_unicast_(false) {
}
VnEntry::~VnEntry() {
}
bool VnEntry::IsLess(const DBEntry &rhs) const {
const VnEntry &a = static_cast<const VnEntry &>(rhs);
return (uuid_ < a.uuid_);
}
string VnEntry::ToString() const {
std::stringstream uuidstring;
uuidstring << uuid_;
return uuidstring.str();
}
DBEntryBase::KeyPtr VnEntry::GetDBRequestKey() const {
VnKey *key = new VnKey(uuid_);
return DBEntryBase::KeyPtr(key);
}
void VnEntry::SetKey(const DBRequestKey *key) {
const VnKey *k = static_cast<const VnKey *>(key);
uuid_ = k->uuid_;
}
AgentDBTable *VnEntry::DBToTable() const {
return VnTable::GetInstance();
}
bool VnEntry::GetVnHostRoutes(const std::string &ipam,
std::vector<OperDhcpOptions::Subnet> *routes) const {
VnData::VnIpamDataMap::const_iterator it = vn_ipam_data_.find(ipam);
if (it != vn_ipam_data_.end()) {
*routes = it->second.oper_dhcp_options_.host_routes();
return true;
}
return false;
}
bool VnEntry::GetIpamData(const IpAddress &vm_addr,
std::string *ipam_name,
autogen::IpamType *ipam) const {
for (unsigned int i = 0; i < ipam_.size(); i++) {
if (ipam_[i].IsSubnetMember(vm_addr)) {
*ipam_name = ipam_[i].ipam_name;
*ipam = ipam_[i].ipam;
return true;
}
}
return false;
}
const VnIpam *VnEntry::GetIpam(const IpAddress &ip) const {
for (unsigned int i = 0; i < ipam_.size(); i++) {
if (ipam_[i].IsSubnetMember(ip)) {
return &ipam_[i];
}
}
return NULL;
}
bool VnEntry::GetIpamVdnsData(const IpAddress &vm_addr,
autogen::IpamType *ipam_type,
autogen::VirtualDnsType *vdns_type) const {
std::string ipam_name;
if (!GetIpamData(vm_addr, &ipam_name, ipam_type) ||
ipam_type->ipam_dns_method != "virtual-dns-server")
return false;
if (!Agent::GetInstance()->domain_config_table()->GetVDns(
ipam_type->ipam_dns_server.virtual_dns_server_name, vdns_type))
return false;
return true;
}
bool VnEntry::GetPrefix(const Ip6Address &ip, Ip6Address *prefix,
uint8_t *plen) const {
for (uint32_t i = 0; i < ipam_.size(); ++i) {
if (!ipam_[i].IsV6()) {
continue;
}
if (IsIp6SubnetMember(ip, ipam_[i].ip_prefix.to_v6(),
ipam_[i].plen)) {
*prefix = ipam_[i].ip_prefix.to_v6();
*plen = (uint8_t)ipam_[i].plen;
return true;
}
}
return false;
}
std::string VnEntry::GetProject() const {
// TODO: update to get the project name from project-vn link.
// Currently, this info doesnt come to the agent
std::size_t start_pos = name_.find(":") + 1;
std::size_t end_pos = name_.find(":", start_pos);
if (end_pos == std::string::npos)
return "";
return name_.substr(start_pos, end_pos - start_pos);
}
int VnEntry::GetVxLanId() const {
if (Agent::GetInstance()->vxlan_network_identifier_mode() ==
Agent::CONFIGURED) {
return vxlan_id_;
} else {
return vnid_;
}
}
int VnEntry::ComputeEthernetTag() const {
int vxlan_id = GetVxLanId();
if (TunnelType::ComputeType(TunnelType::AllType()) != TunnelType::VXLAN) {
return 0;
}
return vxlan_id;
}
void VnEntry::RebakeVxLan(int vxlan_id) {
VxLanId::Create(vxlan_id, GetVrf()->GetName(), flood_unknown_unicast_);
VxLanId *vxlan_id_entry = NULL;
VxLanIdKey vxlan_key(vxlan_id);
vxlan_id_entry = static_cast<VxLanId *>(Agent::GetInstance()->
vxlan_table()->FindActiveEntry(&vxlan_key));
vxlan_id_ref_ = vxlan_id_entry;
}
bool VnEntry::ReEvaluateVxlan(VrfEntry *old_vrf, int new_vxlan_id, int new_vnid,
bool new_bridging,
bool vxlan_network_identifier_mode_changed,
bool new_flood_unknown_unicast) {
bool ret = false;
bool rebake_vxlan = false;
//Two cases in which VXLAN needs to be rebaked.
// - Firstly In case of global vxlan network identifier mode change
// - Secondly in case of VN config change which can impact VXLAN.
if (vxlan_network_identifier_mode_changed) {
rebake_vxlan = true;
ret = true;
} else {
if (old_vrf != GetVrf()) {
rebake_vxlan = true;
}
if (new_bridging != bridging_) {
bridging_ = new_bridging;
rebake_vxlan = true;
}
if (new_vxlan_id != vxlan_id_) {
//Ignore rebake if mode is not configured as user configured vxlan
//is not in use.
if (Agent::GetInstance()->vxlan_network_identifier_mode() ==
Agent::CONFIGURED) {
rebake_vxlan = true;
}
vxlan_id_ = new_vxlan_id;
ret = true;
}
if (new_vnid != vnid_) {
//Ignore rebake if mode is not automatic as auto assigned vxlan
//is not in use.
if (Agent::GetInstance()->vxlan_network_identifier_mode() ==
Agent::AUTOMATIC) {
rebake_vxlan = true;
}
vnid_ = new_vnid;
ret = true;
}
if (flood_unknown_unicast_ != new_flood_unknown_unicast) {
flood_unknown_unicast_ = new_flood_unknown_unicast;
rebake_vxlan = true;
ret = true;
}
}
if (!GetVrf()) {
vxlan_id_ref_ = NULL;
ret = true;
} else {
if (rebake_vxlan) {
int active_vxlan_id = GetVxLanId();
if (active_vxlan_id) {
RebakeVxLan(active_vxlan_id);
} else {
vxlan_id_ref_ = NULL;
}
ret = true;
}
}
return ret;
}
bool VnEntry::VxLanNetworkIdentifierChanged() {
//No change in VN config.
//Need to pick vxlan based on config mode
return ReEvaluateVxlan(NULL, 0, 0, true, true, flood_unknown_unicast_);
}
bool VnEntry::Resync() {
return VxLanNetworkIdentifierChanged();
}
bool VnTable::OperDBResync(DBEntry *entry, const DBRequest *req) {
VnEntry *vn = static_cast<VnEntry *>(entry);
bool ret = vn->Resync();
return ret;
}
bool VnTable::VnEntryWalk(DBTablePartBase *partition, DBEntryBase *entry) {
VnEntry *vn_entry = static_cast<VnEntry *>(entry);
if (vn_entry->GetVrf()) {
VnKey *key = new VnKey(vn_entry->GetUuid());
key->sub_op_ = AgentKey::RESYNC;
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
req.key.reset(key);
req.data.reset(NULL);
Enqueue(&req);
}
return true;
}
void VnTable::VnEntryWalkDone(DBTableBase *partition) {
walkid_ = DBTableWalker::kInvalidWalkerId;
agent()->interface_table()->
UpdateVxLanNetworkIdentifierMode();
agent()->physical_device_vn_table()->
UpdateVxLanNetworkIdentifierMode();
}
void VnTable::UpdateVxLanNetworkIdentifierMode() {
DBTableWalker *walker = Agent::GetInstance()->db()->GetWalker();
if (walkid_ != DBTableWalker::kInvalidWalkerId) {
walker->WalkCancel(walkid_);
}
walkid_ = walker->WalkTable(VnTable::GetInstance(), NULL,
boost::bind(&VnTable::VnEntryWalk,
this, _1, _2),
boost::bind(&VnTable::VnEntryWalkDone, this, _1));
}
std::auto_ptr<DBEntry> VnTable::AllocEntry(const DBRequestKey *k) const {
const VnKey *key = static_cast<const VnKey *>(k);
VnEntry *vn = new VnEntry(agent(), key->uuid_);
return std::auto_ptr<DBEntry>(static_cast<DBEntry *>(vn));
}
DBEntry *VnTable::OperDBAdd(const DBRequest *req) {
VnKey *key = static_cast<VnKey *>(req->key.get());
VnData *data = static_cast<VnData *>(req->data.get());
VnEntry *vn = new VnEntry(agent(), key->uuid_);
vn->name_ = data->name_;
ChangeHandler(vn, req);
vn->SendObjectLog(AgentLogEvent::ADD);
return vn;
}
bool VnTable::OperDBOnChange(DBEntry *entry, const DBRequest *req) {
bool ret = ChangeHandler(entry, req);
VnEntry *vn = static_cast<VnEntry *>(entry);
vn->SendObjectLog(AgentLogEvent::CHANGE);
return ret;
}
bool VnTable::ChangeHandler(DBEntry *entry, const DBRequest *req) {
bool ret = false;
VnEntry *vn = static_cast<VnEntry *>(entry);
VnData *data = static_cast<VnData *>(req->data.get());
VrfEntry *old_vrf = vn->vrf_.get();
AclKey key(data->acl_id_);
AclDBEntry *acl = static_cast<AclDBEntry *>
(Agent::GetInstance()->acl_table()->FindActiveEntry(&key));
if (vn->acl_.get() != acl) {
vn->acl_ = acl;
ret = true;
}
AclKey mirror_key(data->mirror_acl_id_);
AclDBEntry *mirror_acl = static_cast<AclDBEntry *>
(Agent::GetInstance()->acl_table()->FindActiveEntry(&mirror_key));
if (vn->mirror_acl_.get() != mirror_acl) {
vn->mirror_acl_ = mirror_acl;
ret = true;
}
AclKey mirror_cfg_acl_key(data->mirror_cfg_acl_id_);
AclDBEntry *mirror_cfg_acl = static_cast<AclDBEntry *>
(Agent::GetInstance()->acl_table()->FindActiveEntry(&mirror_cfg_acl_key));
if (vn->mirror_cfg_acl_.get() != mirror_cfg_acl) {
vn->mirror_cfg_acl_ = mirror_cfg_acl;
ret = true;
}
VrfKey vrf_key(data->vrf_name_);
VrfEntry *vrf = static_cast<VrfEntry *>
(Agent::GetInstance()->vrf_table()->FindActiveEntry(&vrf_key));
if (vrf != old_vrf) {
if (!vrf) {
DeleteAllIpamRoutes(vn);
}
vn->vrf_ = vrf;
ret = true;
}
if (vn->layer3_forwarding_ != data->layer3_forwarding_) {
vn->layer3_forwarding_ = data->layer3_forwarding_;
ret = true;
}
if (vn->admin_state_ != data->admin_state_) {
vn->admin_state_ = data->admin_state_;
ret = true;
}
//Ignore IPAM changes if layer3 is not enabled
if (!vn->layer3_forwarding_) {
data->ipam_.clear();
data->vn_ipam_data_.clear();
}
if (IpamChangeNotify(vn->ipam_, data->ipam_, vn)) {
vn->ipam_ = data->ipam_;
ret = true;
}
if (vn->vn_ipam_data_ != data->vn_ipam_data_) {
vn->vn_ipam_data_ = data->vn_ipam_data_;
ret = true;
}
ret |= vn->ReEvaluateVxlan(old_vrf, data->vxlan_id_, data->vnid_,
data->bridging_, false,
data->flood_unknown_unicast_);
if (vn->enable_rpf_ != data->enable_rpf_) {
vn->enable_rpf_ = data->enable_rpf_;
ret = true;
}
return ret;
}
bool VnTable::OperDBDelete(DBEntry *entry, const DBRequest *req) {
VnEntry *vn = static_cast<VnEntry *>(entry);
DeleteAllIpamRoutes(vn);
vn->SendObjectLog(AgentLogEvent::DELETE);
return true;
}
VnEntry *VnTable::Find(const boost::uuids::uuid &u) {
VnKey key(u);
return static_cast<VnEntry *>(FindActiveEntry(&key));
}
DBTableBase *VnTable::CreateTable(DB *db, const std::string &name) {
vn_table_ = new VnTable(db, name);
vn_table_->Init();
return vn_table_;
};
void VnTable::RegisterDBClients(IFMapDependencyManager *dep) {
typedef IFMapDependencyTracker::PropagateList PropagateList;
typedef IFMapDependencyTracker::ReactionMap ReactionMap;
ReactionMap react_vn = map_list_of<string, PropagateList>
(("self"),
list_of("virtual-network-virtual-machine-interface")
("virtual-machine-interface-virtual-network"))
("virtual-network-network-ipam",
list_of("virtual-machine-interface-virtual-network"));
dep->RegisterReactionMap("virtual-network", react_vn);
dep->Register("virtual-network",
boost::bind(&AgentOperDBTable::ConfigEventHandler, this, _1));
}
/*
* IsVRFServiceChainingInstance
* Helper function to identify the service chain vrf.
* In VN-VRF mapping config can send multiple VRF for same VN.
* Since assumption had been made that VN to VRF is always 1:1 this
* situation needs to be handled.
* This is a temporary solution in which all VRF other than primary VRF
* is ignored.
* Example:
* VN name: domain:vn1
* VRF 1: domain:vn1:vn1 ----> Primary
* VRF 2: domain:vn1:service-vn1_vn2 ----> Second vrf linked to vn1
* Break the VN and VRF name into tokens based on ':'
* So in primary vrf last and second-last token will be same and will be
* equal to last token of VN name. Keep this VRF.
* The second VRF last and second-last token are not same so it will be ignored.
*
*/
bool IsVRFServiceChainingInstance(const string &vn_name, const string &vrf_name)
{
vector<string> vn_token_result;
vector<string> vrf_token_result;
uint32_t vrf_token_result_size = 0;
split(vn_token_result, vn_name, is_any_of(":"), token_compress_on);
split(vrf_token_result, vrf_name, is_any_of(":"), token_compress_on);
vrf_token_result_size = vrf_token_result.size();
/*
* This check is to handle test cases where vrf and vn name
* are single word without discriminator ':'
* e.g. vrf1 or vn1. In these cases we dont ignore and use
* the VRF
*/
if (vrf_token_result_size == 1) {
return false;
}
if ((vrf_token_result.at(vrf_token_result_size - 1) ==
vrf_token_result.at(vrf_token_result_size - 2)) &&
(vn_token_result.at(vn_token_result.size() - 1) ==
vrf_token_result.at(vrf_token_result_size - 1))) {
return false;
}
return true;
}
IFMapNode *VnTable::FindTarget(IFMapAgentTable *table, IFMapNode *node,
std::string node_type) {
for (DBGraphVertex::adjacency_iterator it = node->begin(table->GetGraph());
it != node->end(table->GetGraph()); ++it) {
IFMapNode *adj_node = static_cast<IFMapNode *>(it.operator->());
if (adj_node->table()->Typename() == node_type)
return adj_node;
}
return NULL;
}
VnData *VnTable::BuildData(IFMapNode *node) {
VirtualNetwork *cfg = static_cast <VirtualNetwork *> (node->GetObject());
assert(cfg);
uuid acl_uuid = nil_uuid();
uuid mirror_cfg_acl_uuid = nil_uuid();
string vrf_name = "";
std::vector<VnIpam> vn_ipam;
VnData::VnIpamDataMap vn_ipam_data;
std::string ipam_name;
// Find link with ACL / VRF adjacency
IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
DBGraph *graph = table->GetGraph();
for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(table->GetGraph()); ++iter) {
IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
if (agent()->cfg_listener()->SkipNode(adj_node)) {
continue;
}
if (adj_node->table() == Agent::GetInstance()->cfg()->cfg_acl_table()) {
AccessControlList *acl_cfg = static_cast<AccessControlList *>
(adj_node->GetObject());
assert(acl_cfg);
autogen::IdPermsType id_perms = acl_cfg->id_perms();
if (acl_cfg->entries().dynamic) {
CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
mirror_cfg_acl_uuid);
} else {
CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong,
acl_uuid);
}
}
if ((adj_node->table() == agent()->cfg()->cfg_vrf_table()) &&
(!IsVRFServiceChainingInstance(node->name(), adj_node->name()))) {
vrf_name = adj_node->name();
}
if (adj_node->table() == agent()->cfg()->cfg_vn_network_ipam_table()) {
if (IFMapNode *ipam_node = FindTarget(table, adj_node,
"network-ipam")) {
ipam_name = ipam_node->name();
autogen::NetworkIpam *network_ipam =
static_cast <autogen::NetworkIpam *> (ipam_node->GetObject());
autogen::IpamType ipam_type;
if (network_ipam)
ipam_type = network_ipam->mgmt();
VirtualNetworkNetworkIpam *ipam =
static_cast<VirtualNetworkNetworkIpam *>
(adj_node->GetObject());
assert(ipam);
const VnSubnetsType &subnets = ipam->data();
for (unsigned int i = 0; i < subnets.ipam_subnets.size(); ++i) {
// if the DNS server address is not specified, set this
// to be the same as the GW address
std::string dns_server_address =
subnets.ipam_subnets[i].dns_server_address;
boost::system::error_code ec;
IpAddress dns_server =
IpAddress::from_string(dns_server_address, ec);
if (ec.value() || dns_server.is_unspecified()) {
dns_server_address =
subnets.ipam_subnets[i].default_gateway;
}
vn_ipam.push_back
(VnIpam(subnets.ipam_subnets[i].subnet.ip_prefix,
subnets.ipam_subnets[i].subnet.ip_prefix_len,
subnets.ipam_subnets[i].default_gateway,
dns_server_address,
subnets.ipam_subnets[i].enable_dhcp, ipam_name,
ipam_type,
subnets.ipam_subnets[i].dhcp_option_list.dhcp_option,
subnets.ipam_subnets[i].host_routes.route));
}
VnIpamLinkData ipam_data;
ipam_data.oper_dhcp_options_.set_host_routes(subnets.host_routes.route);
vn_ipam_data.insert(VnData::VnIpamDataPair(ipam_name, ipam_data));
}
}
}
uuid mirror_acl_uuid = agent()->mirror_cfg_table()->GetMirrorUuid(node->name());
std::sort(vn_ipam.begin(), vn_ipam.end());
// Fetch VN Properties
bool enable_rpf = true;
if (cfg->properties().rpf == "disable") {
enable_rpf = false;
}
bool bridging = true;
bool layer3_forwarding = true;
autogen::VirtualNetworkType properties = cfg->properties();
if (properties.forwarding_mode == "l2") {
layer3_forwarding = false;
}
int network_id;
if (cfg->IsPropertySet(autogen::VirtualNetwork::NETWORK_ID))
network_id = cfg->network_id();
else
network_id = cfg->properties().network_id;
bool flood_unknown_unicast = cfg->flood_unknown_unicast();
return new VnData(agent(), node->name(), acl_uuid, vrf_name, mirror_acl_uuid,
mirror_cfg_acl_uuid, vn_ipam, vn_ipam_data,
cfg->properties().vxlan_network_identifier,
network_id, bridging, layer3_forwarding,
cfg->id_perms().enable, enable_rpf, flood_unknown_unicast);
}
// Change to ACL referernce can result in change of Policy flag
// on interfaces. Find all interfaces on this VN and RESYNC them.
// This is also required to check changes to admin_state and to
// the enable_dhcp flag in the VN subnets (VN Ipam).
// TODO: Check if there is change in VRF
void VnTable::ResyncVmInterface(IFMapNode *node) {
IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
DBGraph *graph = table->GetGraph();
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
// Find link with VM-Port adjacency
for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(graph); ++iter) {
IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
if (agent()->cfg_listener()->SkipNode
(adj_node, agent()->cfg()->cfg_vm_interface_table())) {
continue;
}
if (adj_node->GetObject() == NULL) {
continue;
}
if (agent()->interface_table()->IFNodeToReq(adj_node, req)) {
agent()->interface_table()->Enqueue(&req);
}
}
}
bool VnTable::IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u) {
VirtualNetwork *cfg = static_cast <VirtualNetwork *> (node->GetObject());
assert(cfg);
autogen::IdPermsType id_perms = cfg->id_perms();
CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u);
return true;
}
bool VnTable::IFLinkToReq(IFMapLink *link, IFMapNode *node,
const string &peer_type, IFMapNode *peer,
DBRequest &req) {
// Add/Delete of link other than VMInterface will most likely need re-eval
// of VN.
if (peer_type != "virtual-machine-interface") {
VirtualNetwork *cfg = static_cast <VirtualNetwork *>(node->GetObject());
assert(cfg);
boost::uuids::uuid u;
agent()->cfg_listener()->GetCfgDBStateUuid(node, u);
req.key.reset(new VnKey(u));
req.data.reset(BuildData(node));
Enqueue(&req);
}
// If peer is VMI, invoke re-eval if peer node is present
if (peer && peer->table() == agent()->cfg()->cfg_vm_interface_table()) {
DBRequest vmi_req;
if (agent()->interface_table()->IFNodeToReq(peer, vmi_req) == true) {
LOG(DEBUG, "VN change sync for Port " << peer->name());
agent()->interface_table()->Enqueue(&vmi_req);
}
return false;
}
// Any change to ACL/IPAM will need re-eval of all VMInterface on this VN
if (peer_type == "virtual-network-network-ipam" ||
peer_type == "access-control-list") {
ResyncVmInterface(node);
return false;
}
// If peer is known and is floating-ip pool, propogate change to it
if (peer && peer->table() == agent()->cfg()->cfg_floatingip_pool_table()) {
VmInterface::FloatingIpPoolSync(agent()->interface_table(), peer);
return false;
}
return false;
}
bool VnTable::IFNodeToReq(IFMapNode *node, DBRequest &req) {
VirtualNetwork *cfg = static_cast <VirtualNetwork *> (node->GetObject());
assert(cfg);
autogen::IdPermsType id_perms = cfg->id_perms();
boost::uuids::uuid u;
if (agent()->cfg_listener()->GetCfgDBStateUuid(node, u) == false)
return false;
req.key.reset(new VnKey(u));
VnData *data = NULL;
if (node->IsDeleted()) {
req.oper = DBRequest::DB_ENTRY_DELETE;
} else {
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
data = BuildData(node);
req.data.reset(data);
data->SetIFMapNode(node);
}
Enqueue(&req);
if (node->IsDeleted()) {
return false;
}
// Change to ACL referernce can result in change of Policy flag
// on interfaces. Find all interfaces on this VN and RESYNC them.
// This is also required to check changes to admin_state and to
// the enable_dhcp flag in the VN subnets (VN Ipam).
// TODO: Check if there is change in VRF
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
// Find link with VM-Port adjacency
IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
for (DBGraphVertex::adjacency_iterator iter =
node->begin(table->GetGraph());
iter != node->end(table->GetGraph()); ++iter) {
IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
if (agent()->cfg_listener()->SkipNode
(adj_node, agent()->cfg()->cfg_vm_interface_table())) {
continue;
}
if (adj_node->GetObject() == NULL) {
continue;
}
if (agent()->interface_table()->IFNodeToReq(adj_node, req)) {
agent()->interface_table()->Enqueue(&req);
}
}
// Trigger Floating-IP resync
VmInterface::FloatingIpVnSync(agent()->interface_table(), node);
VmInterface::VnSync(agent()->interface_table(), node);
return false;
}
void VnTable::AddVn(const uuid &vn_uuid, const string &name,
const uuid &acl_id, const string &vrf_name,
const std::vector<VnIpam> &ipam,
const VnData::VnIpamDataMap &vn_ipam_data, int vn_id,
int vxlan_id, bool admin_state, bool enable_rpf,
bool flood_unknown_unicast) {
DBRequest req;
VnKey *key = new VnKey(vn_uuid);
VnData *data = new VnData(agent(), name, acl_id, vrf_name, nil_uuid(),
nil_uuid(), ipam, vn_ipam_data,
vn_id, vxlan_id, true, true,
admin_state, enable_rpf,
flood_unknown_unicast);
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
req.key.reset(key);
req.data.reset(data);
Enqueue(&req);
}
void VnTable::DelVn(const uuid &vn_uuid) {
DBRequest req;
VnKey *key = new VnKey(vn_uuid);
req.oper = DBRequest::DB_ENTRY_DELETE;
req.key.reset(key);
req.data.reset(NULL);
Enqueue(&req);
}
void VnTable::IpamVnSync(IFMapNode *node) {
if (node->IsDeleted()) {
return;
}
IFMapAgentTable *table = static_cast<IFMapAgentTable *> (node->table());
DBGraph *graph = table->GetGraph();
for (DBGraphVertex::adjacency_iterator iter = node->begin(graph);
iter != node->end(graph); ++iter) {
IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
if (Agent::GetInstance()->cfg_listener()->SkipNode
(adj_node, Agent::GetInstance()->cfg()->cfg_vn_table())) {
continue;
}
DBRequest req;
req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
Agent::GetInstance()->vn_table()->IFNodeToReq(adj_node, req);
}
return;
}
void VnTable::UpdateHostRoute(const IpAddress &old_address,
const IpAddress &new_address,
VnEntry *vn) {
VrfEntry *vrf = vn->GetVrf();
if (vrf && (vrf->GetName() != Agent::GetInstance()->
linklocal_vrf_name())) {
AddHostRoute(vn, new_address);
DelHostRoute(vn, old_address);
}
}
// Check the old and new Ipam data and update receive routes for GW addresses
bool VnTable::IpamChangeNotify(std::vector<VnIpam> &old_ipam,
std::vector<VnIpam> &new_ipam,
VnEntry *vn) {
bool change = false;
std::sort(old_ipam.begin(), old_ipam.end());
std::sort(new_ipam.begin(), new_ipam.end());
std::vector<VnIpam>::iterator it_old = old_ipam.begin();
std::vector<VnIpam>::iterator it_new = new_ipam.begin();
while (it_old != old_ipam.end() && it_new != new_ipam.end()) {
if (*it_old < *it_new) {
// old entry is deleted
DelIPAMRoutes(vn, *it_old);
change = true;
it_old++;
} else if (*it_new < *it_old) {
// new entry
AddIPAMRoutes(vn, *it_new);
change = true;
it_new++;
} else {
//Evaluate non key members of IPAM for changes.
// no change in entry
bool gateway_changed = ((*it_old).default_gw !=
(*it_new).default_gw);
bool service_address_changed = ((*it_old).dns_server != (*it_new).dns_server);
if ((*it_old).installed) {
(*it_new).installed = true;
// VNIPAM comparator does not check for gateway.
// If gateway is changed then take appropriate actions.
IpAddress unspecified;
if (gateway_changed) {
if (IsGwHostRouteRequired()) {
UpdateHostRoute((*it_old).default_gw,
(*it_new).default_gw, vn);
}
}
if (service_address_changed) {
UpdateHostRoute((*it_old).dns_server,
(*it_new).dns_server, vn);
}
} else {
AddIPAMRoutes(vn, *it_new);
(*it_old).installed = (*it_new).installed;
}
if (gateway_changed) {
(*it_old).default_gw = (*it_new).default_gw;
}
if (service_address_changed) {
(*it_old).dns_server = (*it_new).dns_server;
}
// update DHCP options
(*it_old).oper_dhcp_options = (*it_new).oper_dhcp_options;
if ((*it_old).dhcp_enable != (*it_new).dhcp_enable) {
(*it_old).dhcp_enable = (*it_new).dhcp_enable;
}
it_old++;
it_new++;
}
}
// delete remaining old entries
for (; it_old != old_ipam.end(); ++it_old) {
DelIPAMRoutes(vn, *it_old);
change = true;
}
// add remaining new entries
for (; it_new != new_ipam.end(); ++it_new) {
AddIPAMRoutes(vn, *it_new);
change = true;
}
std::sort(new_ipam.begin(), new_ipam.end());
return change;
}
void VnTable::DeleteAllIpamRoutes(VnEntry *vn) {
std::vector<VnIpam> &ipam = vn->ipam_;
for (unsigned int i = 0; i < ipam.size(); ++i) {
DelIPAMRoutes(vn, ipam[i]);
}
}
void VnTable::AddIPAMRoutes(VnEntry *vn, VnIpam &ipam) {
VrfEntry *vrf = vn->GetVrf();
if (vrf) {
// Do not let the gateway configuration overwrite the receive nh.
if (vrf->GetName() == Agent::GetInstance()->linklocal_vrf_name()) {
return;
}
if (IsGwHostRouteRequired())
AddHostRoute(vn, ipam.default_gw);
AddHostRoute(vn, ipam.dns_server);
AddSubnetRoute(vn, ipam);
ipam.installed = true;
}
}
void VnTable::DelIPAMRoutes(VnEntry *vn, VnIpam &ipam) {
VrfEntry *vrf = vn->GetVrf();
if (vrf && ipam.installed) {
if (IsGwHostRouteRequired())
DelHostRoute(vn, ipam.default_gw);
DelHostRoute(vn, ipam.dns_server);
DelSubnetRoute(vn, ipam);
ipam.installed = false;
}
}
bool VnTable::IsGwHostRouteRequired() {
return (!agent()->tsn_enabled());
}
// Add receive route for default gw
void VnTable::AddHostRoute(VnEntry *vn, const IpAddress &address) {
VrfEntry *vrf = vn->GetVrf();
if (address.is_v4()) {
static_cast<InetUnicastAgentRouteTable *>(vrf->
GetInet4UnicastRouteTable())->AddHostRoute(vrf->GetName(),
address.to_v4(), 32, vn->GetName());
} else if (address.is_v6()) {
static_cast<InetUnicastAgentRouteTable *>(vrf->
GetInet6UnicastRouteTable())->AddHostRoute(vrf->GetName(),
address.to_v6(), 128, vn->GetName());
}
}
// Del receive route for default gw
void VnTable::DelHostRoute(VnEntry *vn, const IpAddress &address) {
VrfEntry *vrf = vn->GetVrf();
if (address.is_v4()) {
static_cast<InetUnicastAgentRouteTable *>
(vrf->GetInet4UnicastRouteTable())->DeleteReq
(Agent::GetInstance()->local_peer(), vrf->GetName(),
address.to_v4(), 32, NULL);
} else if (address.is_v6()) {
static_cast<InetUnicastAgentRouteTable *>
(vrf->GetInet6UnicastRouteTable())->DeleteReq
(Agent::GetInstance()->local_peer(), vrf->GetName(),
address.to_v6(), 128, NULL);
}
}
void VnTable::AddSubnetRoute(VnEntry *vn, VnIpam &ipam) {