/
interface.cc
1041 lines (898 loc) · 33.2 KB
/
interface.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.
sact*/
#include "base/os.h"
#include <sys/types.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <boost/uuid/uuid_io.hpp>
#include <tbb/mutex.h>
#include "base/logging.h"
#include "db/db.h"
#include "db/db_entry.h"
#include "db/db_table.h"
#include "ifmap/ifmap_node.h"
#include "net/address.h"
#include <init/agent_param.h>
#include <cfg/cfg_init.h>
#include <cfg/cfg_interface.h>
#include <oper/operdb_init.h>
#include <oper/route_common.h>
#include <oper/vm.h>
#include <oper/vn.h>
#include <oper/vrf.h>
#include <oper/nexthop.h>
#include <oper/mpls.h>
#include <oper/mirror_table.h>
#include <oper/interface_common.h>
#include <oper/vrf_assign.h>
#include <oper/vxlan.h>
#include <oper/ifmap_dependency_manager.h>
#include <vnc_cfg_types.h>
#include <oper/agent_sandesh.h>
#include <oper/sg.h>
#include <sandesh/sandesh_trace.h>
#include <sandesh/common/vns_types.h>
#include <sandesh/common/vns_constants.h>
#include <filter/acl.h>
using namespace std;
using namespace boost::uuids;
using boost::assign::map_list_of;
using boost::assign::list_of;
InterfaceTable *InterfaceTable::interface_table_;
/////////////////////////////////////////////////////////////////////////////
// Interface Table routines
/////////////////////////////////////////////////////////////////////////////
void InterfaceTable::Init(OperDB *oper) {
operdb_ = oper;
agent_ = oper->agent();
}
void InterfaceTable::RegisterDBClients(IFMapDependencyManager *dep) {
typedef IFMapDependencyTracker::PropagateList PropagateList;
typedef IFMapDependencyTracker::ReactionMap ReactionMap;
ReactionMap physical_port_react = map_list_of<std::string, PropagateList>
("self", list_of("self") ("physical-router-physical-interface"))
("physical-interface-logical-interface",
list_of("physical-router-physical-interface"))
("physical-router-physical-interface", list_of("self"));
dep->RegisterReactionMap("physical-interface", physical_port_react);
dep->Register("physical-interface",
boost::bind(&AgentOperDBTable::ConfigEventHandler, this, _1));
agent()->cfg()->Register("physical-interface", this,
autogen::PhysicalInterface::ID_PERMS);
ReactionMap logical_port_react = map_list_of<std::string, PropagateList>
("self", list_of("self") ("physical-interface-logical-interface"))
("physical-interface-logical-interface",
list_of("physical-router-physical-interface"))
("logical-interface-virtual-machine-interface",
list_of("physical-interface-logical-interface") ("self"));
dep->RegisterReactionMap("logical-interface", logical_port_react);
dep->Register("logical-interface",
boost::bind(&AgentOperDBTable::ConfigEventHandler, this,
_1));
agent()->cfg()->Register("logical-interface", this,
autogen::LogicalInterface::ID_PERMS);
}
bool InterfaceTable::IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u) {
if (strcmp(node->table()->Typename(), "virtual-machine-interface") == 0) {
return VmiIFNodeToUuid(node, u);
}
if (strcmp(node->table()->Typename(), "logical-interface") == 0) {
return LogicalInterfaceIFNodeToUuid(node, u);
}
return false;
}
bool InterfaceTable::IFNodeToReq(IFMapNode *node, DBRequest &req) {
if (strcmp(node->table()->Typename(), "physical-interface") == 0) {
return PhysicalInterfaceIFNodeToReq(node, req);
}
if (strcmp(node->table()->Typename(), "logical-interface") == 0) {
return LogicalInterfaceIFNodeToReq(node, req);
}
if (strcmp(node->table()->Typename(), "virtual-machine-interface") == 0) {
return VmiIFNodeToReq(node, req);
}
return false;
}
std::auto_ptr<DBEntry> InterfaceTable::AllocEntry(const DBRequestKey *k) const{
const InterfaceKey *key = static_cast<const InterfaceKey *>(k);
return std::auto_ptr<DBEntry>(static_cast<DBEntry *>
(key->AllocEntry(this)));
}
DBEntry *InterfaceTable::OperDBAdd(const DBRequest *req) {
InterfaceKey *key = static_cast<InterfaceKey *>(req->key.get());
InterfaceData *data = static_cast<InterfaceData *>(req->data.get());
Interface *intf = key->AllocEntry(this, data);
if (intf == NULL)
return NULL;
if (intf->type_ == Interface::VM_INTERFACE) {
vmi_count_++;
} else if (intf->type_ == Interface::LOGICAL) {
li_count_++;
}
intf->id_ = index_table_.Insert(intf);
intf->transport_ = data->transport_;
// Get the os-ifindex and mac of interface
intf->GetOsParams(agent());
intf->Add();
intf->SendTrace(Interface::ADD);
return intf;
}
bool InterfaceTable::OperDBOnChange(DBEntry *entry, const DBRequest *req) {
bool ret = false;
InterfaceKey *key = static_cast<InterfaceKey *>(req->key.get());
switch (key->type_) {
case Interface::VM_INTERFACE: {
VmInterface *intf = static_cast<VmInterface *>(entry);
ret = intf->OnChange(static_cast<VmInterfaceData *>(req->data.get()));
break;
}
case Interface::INET: {
InetInterface *intf = static_cast<InetInterface *>(entry);
if (intf) {
// Get the os-ifindex and mac of interface
intf->GetOsParams(agent());
intf->OnChange(static_cast<InetInterfaceData *>(req->data.get()));
ret = true;
}
break;
}
case Interface::PHYSICAL: {
PhysicalInterface *intf = static_cast<PhysicalInterface *>(entry);
ret = intf->OnChange(this, static_cast<PhysicalInterfaceData *>
(req->data.get()));
break;
}
case Interface::REMOTE_PHYSICAL: {
RemotePhysicalInterface *intf =
static_cast<RemotePhysicalInterface *>(entry);
ret = intf->OnChange(this, static_cast<RemotePhysicalInterfaceData *>
(req->data.get()));
break;
}
case Interface::LOGICAL: {
LogicalInterface *intf = static_cast<LogicalInterface *>(entry);
ret = intf->OnChange(this, static_cast<LogicalInterfaceData *>
(req->data.get()));
break;
}
case Interface::PACKET: {
PacketInterface *intf = static_cast<PacketInterface *>(entry);
PacketInterfaceData *data = static_cast<PacketInterfaceData *>(req->data.get());
ret = intf->OnChange(data);
break;
}
default:
break;
}
return ret;
}
// RESYNC supported only for VM_INTERFACE
bool InterfaceTable::OperDBResync(DBEntry *entry, const DBRequest *req) {
InterfaceKey *key = static_cast<InterfaceKey *>(req->key.get());
if (key->type_ != Interface::VM_INTERFACE)
return false;
VmInterfaceData *vm_data = static_cast<VmInterfaceData *>(req->data.get());
VmInterface *intf = static_cast<VmInterface *>(entry);
return intf->Resync(this, vm_data);
}
bool InterfaceTable::OperDBDelete(DBEntry *entry, const DBRequest *req) {
Interface *intf = static_cast<Interface *>(entry);
bool ret = false;
if (intf->Delete(req)) {
intf->SendTrace(Interface::DELETE);
ret = true;
}
return ret;
}
VrfEntry *InterfaceTable::FindVrfRef(const string &name) const {
VrfKey key(name);
return static_cast<VrfEntry *>
(agent_->vrf_table()->FindActiveEntry(&key));
}
VmEntry *InterfaceTable::FindVmRef(const uuid &uuid) const {
VmKey key(uuid);
return static_cast<VmEntry *>(agent_->vm_table()->FindActiveEntry(&key));
}
VnEntry *InterfaceTable::FindVnRef(const uuid &uuid) const {
VnKey key(uuid);
return static_cast<VnEntry *>(agent_->vn_table()->FindActiveEntry(&key));
}
MirrorEntry *InterfaceTable::FindMirrorRef(const string &name) const {
MirrorEntryKey key(name);
return static_cast<MirrorEntry *>
(agent_->mirror_table()->FindActiveEntry(&key));
}
DBTableBase *InterfaceTable::CreateTable(DB *db, const std::string &name) {
interface_table_ = new InterfaceTable(db, name);
(static_cast<DBTable *>(interface_table_))->Init();
return interface_table_;
};
Interface *InterfaceTable::FindInterface(size_t index) {
Interface *intf = index_table_.At(index);
if (intf && intf->IsDeleted() != true) {
return intf;
}
return NULL;
}
bool InterfaceTable::FindVmUuidFromMetadataIp(const Ip4Address &ip,
std::string *vm_ip,
std::string *vm_uuid,
std::string *vm_project_uuid) {
Interface *intf = FindInterfaceFromMetadataIp(ip);
if (intf && intf->type() == Interface::VM_INTERFACE) {
const VmInterface *vintf = static_cast<const VmInterface *>(intf);
*vm_ip = vintf->ip_addr().to_string();
if (vintf->vm()) {
*vm_uuid = UuidToString(vintf->vm()->GetUuid());
*vm_project_uuid = UuidToString(vintf->vm_project_uuid());
return true;
}
}
return false;
}
Interface *InterfaceTable::FindInterfaceFromMetadataIp(const Ip4Address &ip) {
uint32_t addr = ip.to_ulong();
if ((addr & 0xFFFF0000) != (METADATA_IP_ADDR & 0xFFFF0000))
return NULL;
return index_table_.At(addr & 0xFFFF);
}
void InterfaceTable::VmPortToMetaDataIp(uint32_t index, uint32_t vrfid,
Ip4Address *addr) {
uint32_t ip = METADATA_IP_ADDR & 0xFFFF0000;
ip += (index & 0xFFFF);
*addr = Ip4Address(ip);
}
bool InterfaceTable::L2VmInterfaceWalk(DBTablePartBase *partition,
DBEntryBase *entry) {
Interface *intf = static_cast<Interface *>(entry);
if ((intf->type() != Interface::VM_INTERFACE) || intf->IsDeleted())
return true;
VmInterface *vm_intf = static_cast<VmInterface *>(entry);
//In case VN passed is null it is tunnel mode change,
//else its vxlan id change for a VN.
if (!vm_intf->l2_active())
return true;
if (vm_intf->bridging()) {
bool force_update = false;
if (vm_intf->vxlan_id() != vm_intf->vn()->GetVxLanId()) {
force_update = true;
}
vm_intf->UpdateL2(force_update);
}
return true;
}
void InterfaceTable::VmInterfaceWalkDone(DBTableBase *partition) {
walkid_ = DBTableWalker::kInvalidWalkerId;
}
void InterfaceTable::UpdateVxLanNetworkIdentifierMode() {
DBTableWalker *walker = agent_->db()->GetWalker();
if (walkid_ != DBTableWalker::kInvalidWalkerId) {
walker->WalkCancel(walkid_);
}
walkid_ = walker->WalkTable(this, NULL,
boost::bind(&InterfaceTable::L2VmInterfaceWalk,
this, _1, _2),
boost::bind(&InterfaceTable::VmInterfaceWalkDone,
this, _1));
}
/////////////////////////////////////////////////////////////////////////////
// Interface Base Entry routines
/////////////////////////////////////////////////////////////////////////////
Interface::Interface(Type type, const uuid &uuid, const string &name,
VrfEntry *vrf) :
type_(type), uuid_(uuid), name_(name),
vrf_(vrf), label_(MplsTable::kInvalidLabel),
l2_label_(MplsTable::kInvalidLabel), ipv4_active_(true), l2_active_(true),
id_(kInvalidIndex), dhcp_enabled_(true), dns_enabled_(true), mac_(),
os_index_(kInvalidIndex), admin_state_(true), test_oper_state_(true),
transport_(TRANSPORT_INVALID) {
}
Interface::~Interface() {
InterfaceTable *table = static_cast<InterfaceTable *>(get_table());
if (id_ != kInvalidIndex) {
table->FreeInterfaceId(id_);
if (type_ == VM_INTERFACE) {
table->decr_vmi_count();
} else if (type_ == LOGICAL) {
table->decr_li_count();
}
}
}
void Interface::SetPciIndex(Agent *agent) {
std::istringstream pci(agent->params()->physical_interface_pci_addr());
uint32_t domain, bus, device, function;
char c;
if (pci >> std::hex >> domain) {
pci >> c;
} else {
assert(0);
}
if (pci >> std::hex >> bus) {
pci >> c;
} else {
assert(0);
}
if (pci >> std::hex >> device) {
pci >> c;
} else {
assert(0);
}
pci >> std::hex >> function;
os_index_ = domain << 16 | bus << 8 | device << 3 | function;
os_oper_state_ = true;
}
void Interface::GetOsParams(Agent *agent) {
if (agent->test_mode()) {
static int dummy_ifindex = 0;
if (os_index_ == kInvalidIndex) {
os_index_ = ++dummy_ifindex;
mac_.Zero();
mac_.last_octet() = os_index_;
}
os_oper_state_ = test_oper_state_;
return;
}
std::string name = name_;
if (type_ == Interface::PHYSICAL) {
const PhysicalInterface *phy_intf =
static_cast<const PhysicalInterface *>(this);
name = phy_intf->display_name();
}
if (transport_ == TRANSPORT_PMD && type_ == PHYSICAL) {
//PCI address is the name of the interface
// os index from that
SetPciIndex(agent);
}
//In case of DPDK, set mac-address to the physical
//mac address set in configuration file, since
//agent cane query for mac address as physical interface
//will not be present
if (transport_ == TRANSPORT_PMD) {
if (type_ == PHYSICAL || type_ == INET) {
mac_ = *ether_aton(agent->params()->
physical_interface_mac_addr().c_str());
}
}
if (transport_ != TRANSPORT_ETHERNET) {
os_oper_state_ = true;
return;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name.c_str(), IF_NAMESIZE);
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
assert(fd >= 0);
if (ioctl(fd, SIOCGIFHWADDR, (void *)&ifr) < 0) {
LOG(ERROR, "Error <" << errno << ": " << strerror(errno) <<
"> querying mac-address for interface <" << name << ">");
os_oper_state_ = false;
close(fd);
return;
}
if (ioctl(fd, SIOCGIFFLAGS, (void *)&ifr) < 0) {
LOG(ERROR, "Error <" << errno << ": " << strerror(errno) <<
"> querying mac-address for interface <" << name << ">");
os_oper_state_ = false;
close(fd);
return;
}
os_oper_state_ = false;
if ((ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) {
os_oper_state_ = true;
}
close(fd);
#if defined(__linux__)
mac_ = ifr.ifr_hwaddr;
#elif defined(__FreeBSD__)
mac_ = ifr.ifr_addr;
#endif
if (os_index_ == kInvalidIndex) {
int idx = if_nametoindex(name.c_str());
if (idx)
os_index_ = idx;
}
}
void Interface::SetKey(const DBRequestKey *key) {
const InterfaceKey *k = static_cast<const InterfaceKey *>(key);
type_ = k->type_;
uuid_ = k->uuid_;
name_ = k->name_;
}
uint32_t Interface::vrf_id() const {
if (vrf_ == NULL) {
return VrfEntry::kInvalidIndex;
}
return vrf_->vrf_id();
}
void InterfaceTable::set_update_floatingip_cb(UpdateFloatingIpFn fn) {
update_floatingip_cb_ = fn;
}
const InterfaceTable::UpdateFloatingIpFn &InterfaceTable::update_floatingip_cb()
const {
return update_floatingip_cb_;
}
/////////////////////////////////////////////////////////////////////////////
// Pkt Interface routines
/////////////////////////////////////////////////////////////////////////////
PacketInterface::PacketInterface(const std::string &name) :
Interface(Interface::PACKET, nil_uuid(), name, NULL) {
}
PacketInterface::~PacketInterface() {
}
DBEntryBase::KeyPtr PacketInterface::GetDBRequestKey() const {
InterfaceKey *key = new PacketInterfaceKey(uuid_, name_);
return DBEntryBase::KeyPtr(key);
}
void PacketInterface::PostAdd() {
InterfaceNH::CreatePacketInterfaceNh(name_);
}
bool PacketInterface::Delete(const DBRequest *req) {
flow_key_nh_= NULL;
return true;
}
// Enqueue DBRequest to create a Pkt Interface
void PacketInterface::CreateReq(InterfaceTable *table,
const std::string &ifname,
Interface::Transport transport) {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new PacketInterfaceKey(nil_uuid(), ifname));
req.data.reset(new PacketInterfaceData(transport));
table->Enqueue(&req);
}
void PacketInterface::Create(InterfaceTable *table, const std::string &ifname,
Interface::Transport transport) {
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new PacketInterfaceKey(nil_uuid(), ifname));
req.data.reset(new PacketInterfaceData(transport));
table->Process(req);
}
// Enqueue DBRequest to delete a Pkt Interface
void PacketInterface::DeleteReq(InterfaceTable *table,
const std::string &ifname) {
DBRequest req(DBRequest::DB_ENTRY_DELETE);
req.key.reset(new PacketInterfaceKey(nil_uuid(), ifname));
req.data.reset(NULL);
table->Enqueue(&req);
}
void PacketInterface::Delete(InterfaceTable *table, const std::string &ifname) {
DBRequest req(DBRequest::DB_ENTRY_DELETE);
req.key.reset(new PacketInterfaceKey(nil_uuid(), ifname));
req.data.reset(NULL);
table->Process(req);
}
bool PacketInterface::OnChange(PacketInterfaceData *data) {
return false;
}
/////////////////////////////////////////////////////////////////////////////
// DHCP Snoop routines
// DHCP Snoop entry can be added from 3 different places,
// - Interface added from config
// - Address learnt from DHCP Snoop on fabric interface
// - Address learnt from vrouter when agent restarts
//
// DHCP Snoop entry is deleted from 2 places
// - Interface deleted from config
// - Audit of entries read from vrouter on restart and config table
/////////////////////////////////////////////////////////////////////////////
// Get DHCP IP address. First try to find entry in DHCP Snoop table.
// If no entry in DHCP Snoop table, query the InterfaceKScan table.
//
// InterfaceKScan table is populated on agent restart
const Ip4Address InterfaceTable::GetDhcpSnoopEntry(const std::string &ifname) {
tbb::mutex::scoped_lock lock(dhcp_snoop_mutex_);
const DhcpSnoopIterator it = dhcp_snoop_map_.find(ifname);
if (it != dhcp_snoop_map_.end()) {
return it->second.addr_;
}
return Ip4Address(0);
}
void InterfaceTable::DeleteDhcpSnoopEntry(const std::string &ifname) {
tbb::mutex::scoped_lock lock(dhcp_snoop_mutex_);
const DhcpSnoopIterator it = dhcp_snoop_map_.find(ifname);
if (it == dhcp_snoop_map_.end()) {
return;
}
return dhcp_snoop_map_.erase(it);
}
// Set config_seen_ flag in DHCP Snoop entry.
// Create the DHCP Snoop entry, if not already present
void InterfaceTable::DhcpSnoopSetConfigSeen(const std::string &ifname) {
tbb::mutex::scoped_lock lock(dhcp_snoop_mutex_);
const DhcpSnoopIterator it = dhcp_snoop_map_.find(ifname);
Ip4Address addr(0);
if (it != dhcp_snoop_map_.end()) {
addr = it->second.addr_;
}
dhcp_snoop_map_[ifname] = DhcpSnoopEntry(addr, true);
}
void InterfaceTable::AddDhcpSnoopEntry(const std::string &ifname,
const Ip4Address &addr) {
tbb::mutex::scoped_lock lock(dhcp_snoop_mutex_);
DhcpSnoopEntry entry(addr, false);
const DhcpSnoopIterator it = dhcp_snoop_map_.find(ifname);
if (it != dhcp_snoop_map_.end()) {
// Retain config_entry_ flag from old entry
if (it->second.config_entry_) {
entry.config_entry_ = true;
}
// If IP address is not specified, retain old IP address
if (addr.to_ulong() == 0) {
entry.addr_ = it->second.addr_;
}
}
dhcp_snoop_map_[ifname] = entry;
}
// Audit DHCP Snoop table. Delete the entries which are not seen from config
void InterfaceTable::AuditDhcpSnoopTable() {
tbb::mutex::scoped_lock lock(dhcp_snoop_mutex_);
DhcpSnoopIterator it = dhcp_snoop_map_.begin();
while (it != dhcp_snoop_map_.end()){
DhcpSnoopIterator del_it = it++;
if (del_it->second.config_entry_ == false) {
dhcp_snoop_map_.erase(del_it);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Sandesh routines
/////////////////////////////////////////////////////////////////////////////
static string DeviceTypeToString(VmInterface::DeviceType type) {
if (type == VmInterface::LOCAL_DEVICE) {
return "Gateway";
} else if (type == VmInterface::TOR) {
return "TOR";
} else if (type == VmInterface::VM_ON_TAP) {
return "Tap";
} else if (type == VmInterface::VM_VLAN_ON_VMI) {
return "VMI vlan-sub-if";
}
return "Invalid";
}
static string VmiTypeToString(VmInterface::VmiType type) {
if (type == VmInterface::INSTANCE) {
return "Virtual Machine";
} else if (type == VmInterface::SERVICE_CHAIN) {
return "Service Chain";
} else if (type == VmInterface::SERVICE_INSTANCE) {
return "Service Instance";
} else if (type == VmInterface::BAREMETAL) {
return "Baremetal";
} else if (type == VmInterface::GATEWAY) {
return "Gateway";
}
return "Invalid";
}
void Interface::SetItfSandeshData(ItfSandeshData &data) const {
data.set_index(id_);
data.set_name(name_);
data.set_uuid(UuidToString(uuid_));
if (vrf_)
data.set_vrf_name(vrf_->GetName());
else
data.set_vrf_name("--ERROR--");
if (ipv4_active_) {
data.set_active("Active");
} else {
data.set_active("Inactive");
}
if (ipv6_active_) {
data.set_ip6_active("Active");
} else {
data.set_ip6_active("Inactive");
}
if (l2_active_) {
data.set_l2_active("L2 Active");
} else {
data.set_l2_active("L2 Inactive");
}
if (dhcp_enabled_) {
data.set_dhcp_service("Enable");
} else {
data.set_dhcp_service("Disable");
}
if (dns_enabled_) {
data.set_dns_service("Enable");
} else {
data.set_dns_service("Disable");
}
data.set_label(label_);
data.set_l2_label(l2_label_);
if (flow_key_nh()) {
data.set_flow_key_idx(flow_key_nh()->id());
}
switch (type_) {
case Interface::PHYSICAL:
data.set_type("eth");
break;
case Interface::REMOTE_PHYSICAL:
data.set_type("remote-physical-port");
data.set_vrf_name("--NA--");
break;
case Interface::LOGICAL:
data.set_type("logical-port");
data.set_vrf_name("--NA--");
break;
case Interface::VM_INTERFACE: {
data.set_type("vport");
const VmInterface *vintf = static_cast<const VmInterface *>(this);
if (vintf->vn())
data.set_vn_name(vintf->vn()->GetName());
if (vintf->vm())
data.set_vm_uuid(UuidToString(vintf->vm()->GetUuid()));
data.set_ip_addr(vintf->ip_addr().to_string());
data.set_ip6_addr(vintf->ip6_addr().to_string());
data.set_mac_addr(vintf->vm_mac());
data.set_mdata_ip_addr(vintf->mdata_ip_addr().to_string());
data.set_vxlan_id(vintf->vxlan_id());
if (vintf->policy_enabled()) {
data.set_policy("Enable");
} else {
data.set_policy("Disable");
}
data.set_flood_unknown_unicast(vintf->flood_unknown_unicast());
if ((ipv4_active_ == false) ||
(l2_active_ == false)) {
string common_reason = "";
if (!vintf->admin_state()) {
common_reason += "admin-down ";
}
if (vintf->vn() == NULL) {
common_reason += "vn-null ";
} else if (!vintf->vn()->admin_state()) {
common_reason += "vn-admin-down ";
}
if (vintf->vrf() == NULL) {
common_reason += "vrf-null ";
}
if (vintf->NeedDevice()) {
if (vintf->os_index() == Interface::kInvalidIndex) {
common_reason += "no-dev ";
}
if (vintf->os_oper_state() == false) {
common_reason += "os-state-down ";
}
}
if (!ipv4_active_) {
if (vintf->layer3_forwarding() == false) {
common_reason += "l3-disabled";
}
string reason = "L3 Inactive < " + common_reason;
if (vintf->ip_addr().to_ulong() == 0) {
reason += "no-ip-addr ";
}
reason += " >";
data.set_active(reason);
}
if (!l2_active_) {
if (vintf->bridging() == false) {
common_reason += "l2-disabled";
}
string reason = "L2 Inactive < " + common_reason;
reason += " >";
data.set_l2_active(reason);
}
}
std::vector<FloatingIpSandeshList> fip_list;
VmInterface::FloatingIpSet::const_iterator it =
vintf->floating_ip_list().list_.begin();
while (it != vintf->floating_ip_list().list_.end()) {
const VmInterface::FloatingIp &ip = *it;
FloatingIpSandeshList entry;
entry.set_ip_addr(ip.floating_ip_.to_string());
if (ip.vrf_.get()) {
entry.set_vrf_name(ip.vrf_.get()->GetName());
} else {
entry.set_vrf_name("--ERROR--");
}
if (ip.installed_) {
entry.set_installed("Y");
} else {
entry.set_installed("N");
}
fip_list.push_back(entry);
it++;
}
data.set_fip_list(fip_list);
data.set_logical_interface_uuid(to_string(vintf->logical_interface()));
// Add Service VLAN list
std::vector<ServiceVlanSandeshList> vlan_list;
VmInterface::ServiceVlanSet::const_iterator vlan_it =
vintf->service_vlan_list().list_.begin();
while (vlan_it != vintf->service_vlan_list().list_.end()) {
const VmInterface::ServiceVlan *vlan = vlan_it.operator->();
ServiceVlanSandeshList entry;
entry.set_tag(vlan->tag_);
if (vlan->vrf_.get()) {
entry.set_vrf_name(vlan->vrf_.get()->GetName());
} else {
entry.set_vrf_name("--ERROR--");
}
entry.set_ip_addr(vlan->addr_.to_string());
entry.set_label(vlan->label_);
if (vlan->installed_) {
entry.set_installed("Y");
} else {
entry.set_installed("N");
}
vlan_list.push_back(entry);
vlan_it++;
}
std::vector<StaticRouteSandesh> static_route_list;
VmInterface::StaticRouteSet::iterator static_rt_it =
vintf->static_route_list().list_.begin();
while (static_rt_it != vintf->static_route_list().list_.end()) {
const VmInterface::StaticRoute &rt = *static_rt_it;
StaticRouteSandesh entry;
entry.set_vrf_name(rt.vrf_);
entry.set_ip_addr(rt.addr_.to_string());
entry.set_prefix(rt.plen_);
static_rt_it++;
static_route_list.push_back(entry);
}
data.set_static_route_list(static_route_list);
std::vector<StaticRouteSandesh> aap_list;
VmInterface::AllowedAddressPairSet::iterator aap_it =
vintf->allowed_address_pair_list().list_.begin();
while (aap_it != vintf->allowed_address_pair_list().list_.end()) {
const VmInterface::AllowedAddressPair &rt = *aap_it;
StaticRouteSandesh entry;
entry.set_vrf_name(rt.vrf_);
entry.set_ip_addr(rt.addr_.to_string());
entry.set_prefix(rt.plen_);
aap_it++;
aap_list.push_back(entry);
}
data.set_allowed_address_pair_list(aap_list);
if (vintf->fabric_port()) {
data.set_fabric_port("FabricPort");
} else {
data.set_fabric_port("NotFabricPort");
}
if (vintf->need_linklocal_ip()) {
data.set_alloc_linklocal_ip("LL-Enable");
} else {
data.set_alloc_linklocal_ip("LL-Disable");
}
data.set_service_vlan_list(vlan_list);
data.set_analyzer_name(vintf->GetAnalyzer());
data.set_config_name(vintf->cfg_name());
VmInterface::SecurityGroupEntrySet::const_iterator sgit;
std::vector<VmIntfSgUuid> intf_sg_uuid_l;
const VmInterface::SecurityGroupEntryList &sg_uuid_l = vintf->sg_list();
for (sgit = sg_uuid_l.list_.begin(); sgit != sg_uuid_l.list_.end();
++sgit) {
VmIntfSgUuid sg_id;
sg_id.set_sg_uuid(UuidToString(sgit->uuid_));
intf_sg_uuid_l.push_back(sg_id);
}
data.set_sg_uuid_list(intf_sg_uuid_l);
data.set_vm_name(vintf->vm_name());
data.set_vm_project_uuid(UuidToString(vintf->vm_project_uuid()));
data.set_local_preference(vintf->local_preference());
data.set_tx_vlan_id(vintf->tx_vlan_id());
data.set_rx_vlan_id(vintf->rx_vlan_id());
if (vintf->parent()) {
data.set_parent_interface(vintf->parent()->name());
}
if (vintf->subnet().to_ulong() != 0) {
std::ostringstream str;
str << vintf->subnet().to_string() << "/"
<< (int)vintf->subnet_plen();
data.set_subnet(str.str());
}
data.set_sub_type(DeviceTypeToString(vintf->device_type()));
data.set_vmi_type(VmiTypeToString(vintf->vmi_type()));
if (vintf->vrf_assign_acl()) {
std::string vrf_assign_acl;
vrf_assign_acl.assign(UuidToString(vintf->vrf_assign_acl()->GetUuid()));
data.set_vrf_assign_acl_uuid(vrf_assign_acl);
}
break;
}
case Interface::INET:
data.set_type("vhost");
break;
case Interface::PACKET:
data.set_type("pkt");
break;
default:
data.set_type("invalid");
break;
}
data.set_os_ifindex(os_index_);
if (admin_state_) {
data.set_admin_state("Enabled");
} else {
data.set_admin_state("Disabled");
}
switch (transport_) {
case TRANSPORT_ETHERNET:{
data.set_transport("Ethernet");
break;
}
case TRANSPORT_SOCKET: {
data.set_transport("Socket");
break;
}
case TRANSPORT_PMD: {
data.set_transport("PMD");
break;
}
default: {
data.set_transport("Unknown");
break;
}
}
}
bool Interface::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
ItfResp *resp = static_cast<ItfResp *>(sresp);
ItfSandeshData data;
SetItfSandeshData(data);
std::vector<ItfSandeshData> &list =
const_cast<std::vector<ItfSandeshData>&>(resp->get_itf_list());
list.push_back(data);
return true;
}
void ItfReq::HandleRequest() const {
AgentSandeshPtr sand(new AgentIntfSandesh(context(), get_type(),
get_name(), get_uuid(),
get_vn(), get_mac(),
get_ipv4_address(),
get_ipv6_address(),
get_parent_uuid()));
sand->DoSandesh(sand);
}
AgentSandeshPtr InterfaceTable::GetAgentSandesh(const AgentSandeshArguments *args,
const std::string &context) {
return AgentSandeshPtr
(new AgentIntfSandesh(context, args->GetString("type"),
args->GetString("name"), args->GetString("uuid"),
args->GetString("vn"), args->GetString("mac"),
args->GetString("ipv4"), args->GetString("ipv6"),
args->GetString("parent_uuid")));
}
void Interface::SendTrace(Trace event) const {
InterfaceInfo intf_info;
intf_info.set_name(name_);
intf_info.set_index(id_);
switch(event) {
case ADD:
intf_info.set_op("Add");