/
route_ksync.cc
935 lines (796 loc) · 29.8 KB
/
route_ksync.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <base/logging.h>
#include <db/db_entry.h>
#include <db/db_table.h>
#include <db/db_table_partition.h>
#include <ksync/ksync_index.h>
#include <ksync/ksync_entry.h>
#include <ksync/ksync_object.h>
#include <ksync/ksync_netlink.h>
#include <ksync/ksync_sock.h>
#include "cmn/agent.h"
#include "oper/interface_common.h"
#include "oper/nexthop.h"
#include "oper/route_common.h"
#include "oper/mirror_table.h"
#include "vrouter/ksync/interface_ksync.h"
#include "vrouter/ksync/nexthop_ksync.h"
#include "vrouter/ksync/route_ksync.h"
#include "ksync_init.h"
#include "vr_types.h"
#include "vr_defs.h"
#include "vr_nexthop.h"
#if defined(__FreeBSD__)
#include "vr_os.h"
#endif
RouteKSyncEntry::RouteKSyncEntry(RouteKSyncObject* obj,
const RouteKSyncEntry *entry,
uint32_t index) :
KSyncNetlinkDBEntry(index), ksync_obj_(obj),
rt_type_(entry->rt_type_), vrf_id_(entry->vrf_id_),
addr_(entry->addr_), src_addr_(entry->src_addr_), mac_(entry->mac_),
prefix_len_(entry->prefix_len_), nh_(entry->nh_), label_(entry->label_),
proxy_arp_(false), flood_dhcp_(entry->flood_dhcp_),
address_string_(entry->address_string_),
tunnel_type_(entry->tunnel_type_),
wait_for_traffic_(entry->wait_for_traffic_),
local_vm_peer_route_(entry->local_vm_peer_route_),
flood_(entry->flood_), ethernet_tag_(entry->ethernet_tag_) {
}
RouteKSyncEntry::RouteKSyncEntry(RouteKSyncObject* obj, const AgentRoute *rt) :
KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
vrf_id_(rt->vrf_id()), mac_(), nh_(NULL), label_(0), proxy_arp_(false),
flood_dhcp_(false), tunnel_type_(TunnelType::DefaultType()),
wait_for_traffic_(false), local_vm_peer_route_(false),
flood_(false), ethernet_tag_(0) {
boost::system::error_code ec;
rt_type_ = rt->GetTableType();
switch (rt_type_) {
case Agent::INET4_UNICAST: {
const InetUnicastRouteEntry *uc_rt =
static_cast<const InetUnicastRouteEntry *>(rt);
addr_ = uc_rt->addr();
src_addr_ = IpAddress::from_string("0.0.0.0", ec).to_v4();
prefix_len_ = uc_rt->plen();
break;
}
case Agent::INET6_UNICAST: {
const InetUnicastRouteEntry *uc_rt =
static_cast<const InetUnicastRouteEntry *>(rt);
addr_ = uc_rt->addr();
src_addr_ = Ip6Address();
prefix_len_ = uc_rt->plen();
break;
}
case Agent::INET4_MULTICAST: {
const Inet4MulticastRouteEntry *mc_rt =
static_cast<const Inet4MulticastRouteEntry *>(rt);
addr_ = mc_rt->dest_ip_addr();
src_addr_ = mc_rt->src_ip_addr();
prefix_len_ = 32;
break;
}
case Agent::BRIDGE: {
const BridgeRouteEntry *l2_rt =
static_cast<const BridgeRouteEntry *>(rt);
mac_ = l2_rt->mac();
prefix_len_ = 0;
break;
}
default: {
assert(0);
}
}
address_string_ = rt->GetAddressString();
}
RouteKSyncEntry::~RouteKSyncEntry() {
}
KSyncDBObject *RouteKSyncEntry::GetObject() {
return ksync_obj_;
}
bool RouteKSyncEntry::UcIsLess(const KSyncEntry &rhs) const {
const RouteKSyncEntry &entry = static_cast<const RouteKSyncEntry &>(rhs);
if (vrf_id_ != entry.vrf_id_) {
return vrf_id_ < entry.vrf_id_;
}
if (addr_ != entry.addr_) {
return addr_ < entry.addr_;
}
return (prefix_len_ < entry.prefix_len_);
}
bool RouteKSyncEntry::McIsLess(const KSyncEntry &rhs) const {
const RouteKSyncEntry &entry = static_cast<const RouteKSyncEntry &>(rhs);
if (vrf_id_ != entry.vrf_id_) {
return vrf_id_ < entry.vrf_id_;
}
if (src_addr_ != entry.src_addr_) {
return src_addr_ < entry.src_addr_;
}
return (addr_ < entry.addr_);
}
bool RouteKSyncEntry::L2IsLess(const KSyncEntry &rhs) const {
const RouteKSyncEntry &entry = static_cast<const RouteKSyncEntry &>(rhs);
if (vrf_id_ != entry.vrf_id_) {
return vrf_id_ < entry.vrf_id_;
}
return mac_ < entry.mac_;
}
bool RouteKSyncEntry::IsLess(const KSyncEntry &rhs) const {
const RouteKSyncEntry &entry = static_cast<const RouteKSyncEntry &>(rhs);
if (rt_type_ != entry.rt_type_)
return rt_type_ < entry.rt_type_;
//First unicast
if ((rt_type_ == Agent::INET4_UNICAST) ||
(rt_type_ == Agent::INET6_UNICAST)) {
return UcIsLess(rhs);
}
if (rt_type_ == Agent::BRIDGE) {
return L2IsLess(rhs);
}
return McIsLess(rhs);
}
static std::string RouteTypeToString(Agent::RouteTableType type) {
switch (type) {
case Agent::INET4_UNICAST:
return "INET4_UNICAST";
break;
case Agent::INET6_UNICAST:
return "INET6_UNICAST";
break;
case Agent::INET4_MULTICAST:
return "INET_MULTICAST";
break;
case Agent::BRIDGE:
return "BRIDGE";
break;
default:
break;
}
assert(0);
return "";
}
std::string RouteKSyncEntry::ToString() const {
std::stringstream s;
NHKSyncEntry *nexthop;
nexthop = nh();
s << "Type : " << RouteTypeToString(rt_type_) << " ";
const VrfEntry* vrf =
ksync_obj_->ksync()->agent()->vrf_table()->FindVrfFromId(vrf_id_);
if (vrf) {
s << "Route Vrf : " << vrf->GetName() << " ";
}
s << address_string_ << "/" << prefix_len_ << " Type:" << rt_type_;
s << " Label : " << label_;
s << " Tunnel Type: " << tunnel_type_;
if (nexthop) {
s << nexthop->ToString();
} else {
s << " NextHop : <NULL>";
}
s << " Mac: " << mac_.ToString();
s << " Flood DHCP:" << flood_dhcp_;
return s.str();
}
// Check if NH points to a service-chain interface or a Gateway interface
static bool IsGatewayOrServiceInterface(const NextHop *nh) {
if (nh->GetType() != NextHop::INTERFACE &&
nh->GetType() != NextHop::VLAN && nh->GetType() != NextHop::ARP)
return false;
const Interface *intf = NULL;
if (nh->GetType() == NextHop::INTERFACE) {
intf = (static_cast<const InterfaceNH *>(nh))->GetInterface();
if (intf->type() == Interface::PACKET)
return true;
} else if (nh->GetType() == NextHop::VLAN) {
intf = (static_cast<const VlanNH *>(nh))->GetInterface();
} else if (nh->GetType() == NextHop::ARP) {
intf = (static_cast<const ArpNH *>(nh))->GetInterface();
}
const VmInterface *vmi = dynamic_cast<const VmInterface *>(intf);
if (vmi == NULL)
return false;
if (vmi->HasServiceVlan())
return true;
if (vmi->device_type() == VmInterface::LOCAL_DEVICE)
return true;
return false;
}
// Set the flood_ and proxy_arp_ flag for the route
// flood_ flag says that ARP packets hitting route should be flooded
// proxy_arp_ flag says VRouter should do proxy ARP
//
// The flags are set based on NH and Interface-type
bool RouteKSyncEntry::BuildArpFlags(const DBEntry *e, const AgentPath *path,
const MacAddress &mac) {
bool ret = false;
//Route flags for inet4 and inet6
if ((rt_type_ != Agent::INET6_UNICAST) &&
(rt_type_ != Agent::INET4_UNICAST))
return false;
Agent *agent = ksync_obj_->ksync()->agent();
const InetUnicastRouteEntry *rt =
static_cast<const InetUnicastRouteEntry *>(e);
// Assume no flood and proxy_arp by default
bool flood = false;
bool proxy_arp = false;
const NextHop *nh = rt->GetActiveNextHop();
switch (nh->GetType()) {
case NextHop::RESOLVE:
// RESOLVE NH can be used by Gateway Interface or Fabric VRF
// VRouter does not honour flood_ and proxy_arp_ flag for Fabric VRF
// We dont want to flood ARP on Gateway Interface
if (rt->vrf()->GetName() != agent->fabric_vrf_name()) {
proxy_arp = true;
}
break;
case NextHop::COMPOSITE:
// ECMP flows have composite NH. We want to do routing for ECMP flows
// So, set proxy_arp flag
proxy_arp = true;
// There is an exception for ECMP routes
// The subnet route configured on a VN can potentially be exported by
// gateway route also. If gateway are redundant, then the subnet route
// can be an ECMP route.
if (rt->ipam_subnet_route()) {
proxy_arp = false;
flood = true;
}
break;
default:
if (mac != MacAddress::ZeroMac()) {
// Proxy-ARP without flood if mac-stitching is present
proxy_arp = true;
flood = false;
break;
}
// MAC stitching not present.
// If interface belongs to service-chain or Gateway, we want packet to
// be routed. Following config ensures routing,
// - Enable Proxy
// - Disable Flood-bit
// - Dont do MAC Stitching (in RouteNeedsMacBinding)
if (IsGatewayOrServiceInterface(nh) == true) {
proxy_arp = true;
flood = false;
break;
}
if (rt->FindLocalVmPortPath()) {
// Local port without MAC stitching should only be a transition
// case. In the meanwhile, flood ARP so that VM can respond
proxy_arp_ = false;
flood = true;
} else {
// Non local-route. Set flags based on the route
proxy_arp = rt->proxy_arp();
flood = rt->ipam_subnet_route();
}
break;
}
// If the route crosses a VN, we want packet to be routed. So, override
// the flags set above and set only Proxy flag
VnEntry *vn= rt->vrf()->vn();
if (vn == NULL || (path->dest_vn_name() != vn->GetName())) {
proxy_arp = true;
flood = false;
}
// VRouter does not honour flood/proxy_arp flags for fabric-vrf
if (rt->vrf()->GetName() == agent->fabric_vrf_name()) {
proxy_arp = false;
flood = false;
}
if (proxy_arp != proxy_arp_) {
proxy_arp_ = proxy_arp;
ret = true;
}
if (flood != flood_) {
flood_ = flood;
ret = true;
}
return ret;
}
//Uses internal API to extract path.
const NextHop *RouteKSyncEntry::GetActiveNextHop(const AgentRoute *route) const {
const AgentPath *path = GetActivePath(route);
if (path == NULL)
return NULL;
return path->ComputeNextHop(ksync_obj_->ksync()->agent());
}
//Returns the usable path.
//In case of bridge tables the path contains reference path which
//has all the data. This path known as evpn_path is a level of indirection
//to leaked path from MAC+IP route.
const AgentPath *RouteKSyncEntry::GetActivePath(const AgentRoute *route) const {
const AgentPath *path = route->GetActivePath();
return path;
}
bool RouteKSyncEntry::Sync(DBEntry *e) {
bool ret = false;
Agent *agent = ksync_obj_->ksync()->agent();
const AgentRoute *route = static_cast<AgentRoute *>(e);
const AgentPath *path = GetActivePath(route);
if (path->peer() == agent->local_vm_peer())
local_vm_peer_route_ = true;
else
local_vm_peer_route_ = false;
NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
NHKSyncEntry *old_nh = nh();
const NextHop *tmp = NULL;
tmp = GetActiveNextHop(route);
if (tmp == NULL) {
DiscardNHKey key;
tmp = static_cast<NextHop *>(agent->nexthop_table()->
FindActiveEntry(&key));
}
NHKSyncEntry nexthop(nh_object, tmp);
nh_ = static_cast<NHKSyncEntry *>(nh_object->GetReference(&nexthop));
if (old_nh != nh()) {
ret = true;
}
//Bother for label for unicast and bridge routes
if (rt_type_ != Agent::INET4_MULTICAST) {
uint32_t old_label = label_;
if (route->is_multicast()) {
label_ = path->vxlan_id();
} else {
label_ = path->GetActiveLabel();
}
if (label_ != old_label) {
ret = true;
}
if (tunnel_type_ != path->GetTunnelType()) {
tunnel_type_ = path->GetTunnelType();
ret = true;
}
}
if (wait_for_traffic_ != route->WaitForTraffic()) {
wait_for_traffic_ = route->WaitForTraffic();
ret = true;
}
if (rt_type_ == Agent::INET4_UNICAST || rt_type_ == Agent::INET6_UNICAST) {
VrfKSyncObject *obj = ksync_obj_->ksync()->vrf_ksync_obj();
const InetUnicastRouteEntry *uc_rt =
static_cast<const InetUnicastRouteEntry *>(e);
MacAddress mac;
if (obj->RouteNeedsMacBinding(uc_rt)) {
mac = obj->GetIpMacBinding(uc_rt->vrf(), addr_);
}
if (mac != mac_) {
mac_ = mac;
ret = true;
}
if (BuildArpFlags(e, path, mac_))
ret = true;
}
if (rt_type_ == Agent::BRIDGE) {
const BridgeRouteEntry *l2_rt =
static_cast<const BridgeRouteEntry *>(route);
//First search for v4
const MacVmBindingPath *dhcp_path = dynamic_cast<const MacVmBindingPath *>
(l2_rt->FindMacVmBindingPath());
bool flood_dhcp = true; // Flood DHCP if MacVmBindingPath is not present
if (dhcp_path)
flood_dhcp = dhcp_path->flood_dhcp();
if (flood_dhcp_ != flood_dhcp) {
flood_dhcp_ = flood_dhcp;
ret = true;
}
}
return ret;
}
void RouteKSyncEntry::FillObjectLog(sandesh_op::type type,
KSyncRouteInfo &info) const {
if (type == sandesh_op::ADD) {
info.set_operation("ADD/CHANGE");
} else {
info.set_operation("DELETE");
}
info.set_addr(address_string_);
info.set_plen(prefix_len_);
info.set_vrf(vrf_id_);
if (nh()) {
info.set_nh_idx(nh()->nh_id());
if (nh()->type() == NextHop::TUNNEL) {
info.set_label(label_);
}
} else {
info.set_nh_idx(NH_DISCARD_ID);
}
info.set_mac(mac_.ToString());
info.set_type(RouteTypeToString(rt_type_));
}
int RouteKSyncEntry::Encode(sandesh_op::type op, uint8_t replace_plen,
char *buf, int buf_len) {
vr_route_req encoder;
int encode_len, error;
NHKSyncEntry *nexthop = nh();
encoder.set_h_op(op);
encoder.set_rtr_rid(0);
encoder.set_rtr_vrf_id(vrf_id_);
if (rt_type_ != Agent::BRIDGE) {
if (addr_.is_v4()) {
encoder.set_rtr_family(AF_INET);
boost::array<unsigned char, 4> bytes = addr_.to_v4().to_bytes();
std::vector<int8_t> rtr_prefix(bytes.begin(), bytes.end());
encoder.set_rtr_prefix(rtr_prefix);
} else if (addr_.is_v6()) {
encoder.set_rtr_family(AF_INET6);
boost::array<unsigned char, 16> bytes = addr_.to_v6().to_bytes();
std::vector<int8_t> rtr_prefix(bytes.begin(), bytes.end());
encoder.set_rtr_prefix(rtr_prefix);
}
encoder.set_rtr_prefix_len(prefix_len_);
if (mac_ != MacAddress::ZeroMac()) {
if ((addr_.is_v4() && prefix_len_ != 32) ||
(addr_.is_v6() && prefix_len_ != 128)) {
LOG(ERROR, "Unexpected MAC stitching for route "
<< ToString());
mac_ = MacAddress::ZeroMac();
}
std::vector<int8_t> mac((int8_t *)mac_,
(int8_t *)mac_ + mac_.size());
encoder.set_rtr_mac(mac);
}
} else {
encoder.set_rtr_family(AF_BRIDGE);
//TODO add support for mac
std::vector<int8_t> mac((int8_t *)mac_,
(int8_t *)mac_ + mac_.size());
encoder.set_rtr_mac(mac);
}
int label = 0;
int flags = 0;
if (rt_type_ != Agent::INET4_MULTICAST) {
if (nexthop != NULL && nexthop->type() == NextHop::TUNNEL) {
label = label_;
flags |= VR_RT_LABEL_VALID_FLAG;
}
}
if (rt_type_ == Agent::BRIDGE) {
label = label_;
if (nexthop != NULL && ((nexthop->type() == NextHop::COMPOSITE) ||
(nexthop->type() == NextHop::TUNNEL))) {
flags |= VR_BE_LABEL_VALID_FLAG;
}
if (flood_dhcp_)
flags |= VR_BE_FLOOD_DHCP_FLAG;
} else {
if (proxy_arp_) {
flags |= VR_RT_ARP_PROXY_FLAG;
}
if (wait_for_traffic_) {
flags |= VR_RT_ARP_TRAP_FLAG;
}
if (flood_) {
flags |= VR_RT_ARP_FLOOD_FLAG;
}
}
encoder.set_rtr_label_flags(flags);
encoder.set_rtr_label(label);
if (nexthop != NULL) {
encoder.set_rtr_nh_id(nexthop->nh_id());
} else {
encoder.set_rtr_nh_id(NH_DISCARD_ID);
}
if (op == sandesh_op::DELETE) {
encoder.set_rtr_replace_plen(replace_plen);
}
encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
return encode_len;
}
int RouteKSyncEntry::AddMsg(char *buf, int buf_len) {
KSyncRouteInfo info;
FillObjectLog(sandesh_op::ADD, info);
KSYNC_TRACE(Route, info);
return Encode(sandesh_op::ADD, 0, buf, buf_len);
}
int RouteKSyncEntry::ChangeMsg(char *buf, int buf_len){
KSyncRouteInfo info;
FillObjectLog(sandesh_op::ADD, info);
KSYNC_TRACE(Route, info);
return Encode(sandesh_op::ADD, 0, buf, buf_len);
}
int RouteKSyncEntry::DeleteMsg(char *buf, int buf_len) {
RouteKSyncEntry key(ksync_obj_, this, KSyncEntry::kInvalidIndex);
KSyncEntry *found = NULL;
RouteKSyncEntry *route = NULL;
NHKSyncEntry *ksync_nh = NULL;
// IF multicast or bridge delete unconditionally
if ((rt_type_ == Agent::BRIDGE) ||
(rt_type_ == Agent::INET4_MULTICAST)) {
return DeleteInternal(nh(), NULL, buf, buf_len);
}
// For INET routes, we need to give replacement NH and prefixlen
for (int plen = (prefix_len() - 1); plen >= 0; plen--) {
if (addr_.is_v4()) {
Ip4Address addr = Address::GetIp4SubnetAddress(addr_.to_v4(), plen);
key.set_ip(addr);
} else if (addr_.is_v6()) {
Ip6Address addr = Address::GetIp6SubnetAddress(addr_.to_v6(), plen);
key.set_ip(addr);
}
key.set_prefix_len(plen);
found = GetObject()->Find(&key);
if (found) {
route = static_cast<RouteKSyncEntry *>(found);
if (route->IsResolved()) {
ksync_nh = route->nh();
if(ksync_nh && ksync_nh->IsResolved()) {
return DeleteInternal(ksync_nh, route, buf, buf_len);
}
ksync_nh = NULL;
}
}
}
/* If better route is not found, send discardNH for route */
return DeleteInternal(NULL, NULL, buf, buf_len);
}
uint8_t RouteKSyncEntry::CopyReplacementData(NHKSyncEntry *nexthop,
RouteKSyncEntry *new_rt) {
uint8_t new_plen = 0;
nh_ = nexthop;
if (new_rt == NULL) {
label_ = 0;
proxy_arp_ = false;
flood_ = false;
wait_for_traffic_ = false;
// mac_ is key for bridge entries and modifying it will corrut the
// KSync tree. So, reset mac in case of non-bridge entries only
if (rt_type_ != Agent::BRIDGE) {
mac_ = MacAddress::ZeroMac();
}
} else {
label_ = new_rt->label();
new_plen = new_rt->prefix_len();
proxy_arp_ = new_rt->proxy_arp();
flood_ = new_rt->flood();
wait_for_traffic_ = new_rt->wait_for_traffic();
mac_ = new_rt->mac();
}
return new_plen;
}
int RouteKSyncEntry::DeleteInternal(NHKSyncEntry *nexthop,
RouteKSyncEntry *new_rt,
char *buf, int buf_len) {
uint8_t replace_plen = CopyReplacementData(nexthop, new_rt);
KSyncRouteInfo info;
FillObjectLog(sandesh_op::DELETE, info);
KSYNC_TRACE(Route, info);
return Encode(sandesh_op::DELETE, replace_plen, buf, buf_len);
}
KSyncEntry *RouteKSyncEntry::UnresolvedReference() {
NHKSyncEntry *nexthop = nh();
if (!nexthop->IsResolved()) {
return nexthop;
}
if ((rt_type_ == Agent::INET4_UNICAST) ||
(rt_type_ == Agent::INET6_UNICAST)) {
if (!mac_.IsZero()) {
//Get Vrf and bridge ksync object
VrfKSyncObject *vrf_obj = ksync_obj_->ksync()->vrf_ksync_obj();
VrfEntry* vrf =
ksync_obj_->ksync()->agent()->vrf_table()->FindVrfFromId(vrf_id_);
if (vrf) {
VrfKSyncObject::VrfState *state =
static_cast<VrfKSyncObject::VrfState *>
(vrf->GetState(vrf->get_table(),
vrf_obj->vrf_listener_id()));
RouteKSyncObject* bridge_ksync_obj = state->bridge_route_table_;
BridgeRouteEntry tmp_l2_rt(vrf, mac_, Peer::EVPN_PEER, false);
RouteKSyncEntry key(bridge_ksync_obj, &tmp_l2_rt);
RouteKSyncEntry *mac_route_reference =
static_cast<RouteKSyncEntry *>(bridge_ksync_obj->
GetReference(&key));
//Get the ksync entry for stitched mac
//else mark dependancy on same.
if (!mac_route_reference->IsResolved())
return mac_route_reference;
} else {
// clear the mac_ to avoid failure in programming vrouter
mac_ = MacAddress::ZeroMac();
}
}
}
return NULL;
}
RouteKSyncObject::RouteKSyncObject(KSync *ksync, AgentRouteTable *rt_table):
KSyncDBObject(), ksync_(ksync), marked_delete_(false),
table_delete_ref_(this, rt_table->deleter()) {
rt_table_ = rt_table;
RegisterDb(rt_table);
}
RouteKSyncObject::~RouteKSyncObject() {
UnregisterDb(GetDBTable());
table_delete_ref_.Reset(NULL);
}
KSyncEntry *RouteKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
const RouteKSyncEntry *route = static_cast<const RouteKSyncEntry *>(entry);
RouteKSyncEntry *ksync = new RouteKSyncEntry(this, route, index);
return static_cast<KSyncEntry *>(ksync);
}
KSyncEntry *RouteKSyncObject::DBToKSyncEntry(const DBEntry *e) {
const AgentRoute *route = static_cast<const AgentRoute *>(e);
RouteKSyncEntry *key = new RouteKSyncEntry(this, route);
return static_cast<KSyncEntry *>(key);
}
void RouteKSyncObject::Unregister() {
if (IsEmpty() == true && marked_delete_ == true) {
KSYNC_TRACE(Trace, "Destroying ksync object: " + rt_table_->name());
KSyncObjectManager::Unregister(this);
}
}
void RouteKSyncObject::ManagedDelete() {
marked_delete_ = true;
Unregister();
}
void RouteKSyncObject::EmptyTable() {
if (marked_delete_ == true) {
Unregister();
}
}
void VrfKSyncObject::VrfNotify(DBTablePartBase *partition, DBEntryBase *e) {
VrfEntry *vrf = static_cast<VrfEntry *>(e);
VrfState *state = static_cast<VrfState *>
(vrf->GetState(partition->parent(), vrf_listener_id_));
if (vrf->IsDeleted()) {
if (state) {
UnRegisterEvpnRouteTableListener(vrf, state);
vrf->ClearState(partition->parent(), vrf_listener_id_);
delete state;
}
return;
}
if (state == NULL) {
KSYNC_TRACE(Trace, "Subscribing to route table " + vrf->GetName());
state = new VrfState();
state->seen_ = true;
vrf->SetState(partition->parent(), vrf_listener_id_, state);
// Get Inet4 Route table and register with KSync
AgentRouteTable *rt_table = static_cast<AgentRouteTable *>(vrf->
GetInet4UnicastRouteTable());
state->inet4_uc_route_table_ = new RouteKSyncObject(ksync_, rt_table);
// Get Inet6 Route table and register with KSync
rt_table = static_cast<AgentRouteTable *>(vrf->
GetInet6UnicastRouteTable());
state->inet6_uc_route_table_ = new RouteKSyncObject(ksync_, rt_table);
// Get Layer 2 Route table and register with KSync
rt_table = static_cast<AgentRouteTable *>(vrf->
GetBridgeRouteTable());
state->bridge_route_table_ = new RouteKSyncObject(ksync_, rt_table);
//Now for multicast table. Ksync object for multicast table is
//not maintained in vrf list
//TODO Enhance ksyncobject for UC/MC, currently there is only one entry
//in MC so just use the UC object for time being.
rt_table = static_cast<AgentRouteTable *>(vrf->
GetInet4MulticastRouteTable());
state->inet4_mc_route_table_ = new RouteKSyncObject(ksync_, rt_table);
//Add EVPN route table listener to update IP MAC binding table.
rt_table = static_cast<AgentRouteTable *>(vrf->
GetEvpnRouteTable());
if (state->evpn_rt_table_listener_id_ == DBTableBase::kInvalidId) {
state->evpn_rt_table_listener_id_ =
rt_table->Register(boost::bind(&VrfKSyncObject::EvpnRouteTableNotify,
this, _1, _2));
}
}
}
void VrfKSyncObject::EvpnRouteTableNotify(DBTablePartBase *partition,
DBEntryBase *e) {
const EvpnRouteEntry *evpn_rt = static_cast<const EvpnRouteEntry *>(e);
if (evpn_rt->IsDeleted()) {
DelIpMacBinding(evpn_rt->vrf(), evpn_rt->ip_addr(),
evpn_rt->mac());
} else {
AddIpMacBinding(evpn_rt->vrf(), evpn_rt->ip_addr(),
evpn_rt->mac());
}
return;
}
void VrfKSyncObject::UnRegisterEvpnRouteTableListener(const VrfEntry *vrf,
VrfState *state) {
if (state->evpn_rt_table_listener_id_ == DBTableBase::kInvalidId)
return;
AgentRouteTable *rt_table = static_cast<AgentRouteTable *>(vrf->
GetEvpnRouteTable());
rt_table->Unregister(state->evpn_rt_table_listener_id_);
state->evpn_rt_table_listener_id_ = DBTableBase::kInvalidId;
}
VrfKSyncObject::VrfKSyncObject(KSync *ksync)
: ksync_(ksync) {
}
VrfKSyncObject::~VrfKSyncObject() {
}
void VrfKSyncObject::RegisterDBClients() {
vrf_listener_id_ = ksync_->agent()->vrf_table()->Register
(boost::bind(&VrfKSyncObject::VrfNotify, this, _1, _2));
}
void VrfKSyncObject::Shutdown() {
ksync_->agent()->vrf_table()->Unregister(vrf_listener_id_);
vrf_listener_id_ = -1;
}
void vr_route_req::Process(SandeshContext *context) {
AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
ioc->RouteMsgHandler(this);
}
/****************************************************************************
* Methods to stitch IP and MAC addresses. The KSync object maintains mapping
* between IP <-> MAC in ip_mac_binding_ tree. The table is built based on
* Evpn routes.
*
* When an Inet route is notified, if it needs MAC Stitching, the MAC to
* stitch is found from the ip_mac_binding_ tree
*
* Any change to ip_mac_binding_ tree will also result in re-evaluation of
* Inet4/Inet6 route that may potentially have stitching changed
****************************************************************************/
// Compute if a route needs IP-MAC binding. A route needs IP-MAC binding if,
// 1. The NH points to interface or Tunnel-NH
// 2. NH does not belong to Service-Chain Interface
// 3. NH does not belong to Gateway Interface
// to interface or tunnel-nh
bool VrfKSyncObject::RouteNeedsMacBinding(const InetUnicastRouteEntry *rt) {
if (rt->addr().is_v4() && rt->plen() != 32)
return false;
if (rt->addr().is_v6() && rt->plen() != 128)
return false;
const NextHop *nh = rt->GetActiveNextHop();
if (nh == NULL)
return false;
if (nh->GetType() != NextHop::INTERFACE &&
nh->GetType() != NextHop::TUNNEL &&
nh->GetType() != NextHop::VLAN)
return false;
if (IsGatewayOrServiceInterface(nh) == true)
return false;
return true;
}
// Notify change to KSync entry of InetUnicast Route
void VrfKSyncObject::NotifyUcRoute(VrfEntry *vrf, VrfState *state,
const IpAddress &ip) {
InetUnicastAgentRouteTable *table = NULL;
if (ip.is_v4()) {
table = vrf->GetInet4UnicastRouteTable();
} else {
table = vrf->GetInet6UnicastRouteTable();
}
InetUnicastRouteEntry *rt = table->FindLPM(ip);
if (rt == NULL || rt->IsDeleted())
return;
if (rt->GetTableType() == Agent::INET4_UNICAST) {
state->inet4_uc_route_table_->Notify(rt->get_table_partition(), rt);
} else if (rt->GetTableType() == Agent::INET6_UNICAST) {
state->inet6_uc_route_table_->Notify(rt->get_table_partition(), rt);
}
}
void VrfKSyncObject::AddIpMacBinding(VrfEntry *vrf, const IpAddress &ip,
const MacAddress &mac) {
VrfState *state = static_cast<VrfState *>
(vrf->GetState(vrf->get_table(), vrf_listener_id_));
if (state == NULL)
return;
state->ip_mac_binding_[ip] = mac;
NotifyUcRoute(vrf, state, ip);
}
void VrfKSyncObject::DelIpMacBinding(VrfEntry *vrf, const IpAddress &ip,
const MacAddress &mac) {
VrfState *state = static_cast<VrfState *>
(vrf->GetState(vrf->get_table(), vrf_listener_id_));
if (state == NULL)
return;
state->ip_mac_binding_.erase(ip);
NotifyUcRoute(vrf, state, ip);
}
MacAddress VrfKSyncObject::GetIpMacBinding(VrfEntry *vrf,
const IpAddress &ip) const {
VrfState *state = static_cast<VrfState *>
(vrf->GetState(vrf->get_table(), vrf_listener_id_));
if (state == NULL)
return MacAddress::ZeroMac();
IpToMacBinding::const_iterator it = state->ip_mac_binding_.find(ip);
if (it == state->ip_mac_binding_.end())
return MacAddress::ZeroMac();
return it->second;
}