/
pkt_flow_info.cc
1562 lines (1388 loc) · 53.2 KB
/
pkt_flow_info.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 "base/os.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include "net/address_util.h"
#include "route/route.h"
#include "cmn/agent_cmn.h"
#include "init/agent_param.h"
#include "oper/interface_common.h"
#include "oper/nexthop.h"
#include "oper/route_common.h"
#include "oper/path_preference.h"
#include "oper/vrf.h"
#include "oper/sg.h"
#include "oper/global_vrouter.h"
#include "oper/operdb_init.h"
#include "oper/tunnel_nh.h"
#include "filter/packet_header.h"
#include "filter/acl.h"
#include "pkt/proto.h"
#include "pkt/proto_handler.h"
#include "pkt/pkt_handler.h"
#include "pkt/flow_table.h"
#include "pkt/flow_proto.h"
#include "pkt/pkt_sandesh_flow.h"
#include "cmn/agent_stats.h"
#include <vrouter/ksync/flowtable_ksync.h>
#include <vrouter/ksync/ksync_init.h>
static void LogError(const PktInfo *pkt, const char *str) {
if (pkt->family == Address::INET) {
FLOW_TRACE(DetailErr, pkt->agent_hdr.cmd_param, pkt->agent_hdr.ifindex,
pkt->agent_hdr.vrf, pkt->ip_saddr.to_v4().to_ulong(),
pkt->ip_daddr.to_v4().to_ulong(), str, pkt->l3_forwarding,
0, 0, 0, 0);
} else if (pkt->family == Address::INET6) {
uint64_t sip[2], dip[2];
Ip6AddressToU64Array(pkt->ip_saddr.to_v6(), sip, 2);
Ip6AddressToU64Array(pkt->ip_daddr.to_v6(), dip, 2);
FLOW_TRACE(DetailErr, pkt->agent_hdr.cmd_param, pkt->agent_hdr.ifindex,
pkt->agent_hdr.vrf, -1, -1, str, pkt->l3_forwarding,
sip[0], sip[1], dip[0], dip[1]);
} else {
assert(0);
}
}
// VRF changed for the packet. Treat it as Layer3 packet from now.
// Note:
// Features like service chain are supported only for Layer3. Bridge
// entries are not leaked into the new VRF and any bridge entry lookup
// into new VRF will also Fail. So, even VRouter will treat packets
// as L3 after VRF transaltion.
void PktFlowInfo::ChangeVrf(const PktInfo *pkt, PktControlInfo *info,
const VrfEntry *vrf) {
l3_flow = true;
pkt->l3_forwarding = true;
}
void PktFlowInfo::UpdateRoute(const AgentRoute **rt, const VrfEntry *vrf,
const IpAddress &ip, const MacAddress &mac,
FlowRouteRefMap &ref_map) {
if (*rt != NULL && (*rt)->GetTableType() != Agent::BRIDGE)
ref_map[(*rt)->vrf_id()] = RouteToPrefixLen(*rt);
if (l3_flow) {
*rt = flow_table->GetUcRoute(vrf, ip);
} else {
*rt = flow_table->GetL2Route(vrf, mac);
}
if (*rt == NULL)
ref_map[vrf->vrf_id()] = 0;
}
uint8_t PktFlowInfo::RouteToPrefixLen(const AgentRoute *route) {
if (route == NULL) {
return 0;
}
const InetUnicastRouteEntry *inet_rt =
dynamic_cast<const InetUnicastRouteEntry *>(route);
if (inet_rt != NULL) {
return inet_rt->plen();
}
const BridgeRouteEntry *l2_rt =
dynamic_cast<const BridgeRouteEntry *>(route);
if (l2_rt) {
return l2_rt->mac().bit_len();
}
assert(0);
return -1;
}
// Traffic from IPFabric to VM is treated as EGRESS
// Any other traffic is INGRESS
bool PktFlowInfo::ComputeDirection(const Interface *intf) {
bool ret = true;
if (intf->type() == Interface::PHYSICAL) {
ret = false;
}
return ret;
}
// Get VRF corresponding to a NH
static uint32_t NhToVrf(const NextHop *nh) {
const VrfEntry *vrf = NULL;
switch (nh->GetType()) {
case NextHop::COMPOSITE: {
vrf = (static_cast<const CompositeNH *>(nh))->vrf();
break;
}
case NextHop::NextHop::INTERFACE: {
const Interface *intf =
(static_cast<const InterfaceNH *>(nh))->GetInterface();
if (intf)
vrf = intf->vrf();
break;
}
default:
break;
}
if (vrf == NULL)
return VrfEntry::kInvalidIndex;
if (!vrf->IsActive())
return VrfEntry::kInvalidIndex;
return vrf->vrf_id();
}
static const NextHop* GetPolicyEnabledNH(NextHopTable *nh_table,
const NextHop *nh) {
if (nh->PolicyEnabled()) {
return nh;
}
DBEntryBase::KeyPtr key = nh->GetDBRequestKey();
NextHopKey *nh_key = static_cast<NextHopKey *>(key.get());
nh_key->SetPolicy(true);
return static_cast<const NextHop *>(nh_table->FindActiveEntry(key.get()));
}
static const NextHop* GetPolicyDisabledNH(NextHopTable *nh_table,
const NextHop *nh) {
if (nh->PolicyEnabled() == false) {
return nh;
}
DBEntryBase::KeyPtr key = nh->GetDBRequestKey();
NextHopKey *nh_key = static_cast<NextHopKey *>(key.get());
nh_key->SetPolicy(false);
return static_cast<const NextHop *>(nh_table->FindActiveEntry(key.get()));
}
static bool IsVgwOrVmInterface(const Interface *intf) {
if (intf->type() == Interface::VM_INTERFACE)
return true;
if (intf->type() == Interface::INET) {
const InetInterface *inet = static_cast<const InetInterface *>(intf);
if (inet->sub_type() == InetInterface::SIMPLE_GATEWAY)
return true;
}
return false;
}
// Get interface from a NH. Also, decode ECMP information from NH
// Responsible to set following fields,
// out->nh_ : outgoing Nexthop Index. Will also be used to set reverse flow-key
// out->vrf_ : VRF to be used after flow processing. Value set here can
// potentially get overridden later
// TODO: Revisit the use of vrf_, dest_vrf and nat_vrf
// force_vmport means, we want destination to be VM_INTERFACE only
// This is to avoid routing across fabric interface itself
static bool NhDecode(const NextHop *nh, const PktInfo *pkt, PktFlowInfo *info,
PktControlInfo *in, PktControlInfo *out,
bool force_vmport) {
bool ret = true;
if (!nh->IsActive())
return false;
// If its composite NH, find interface information from the component NH
const CompositeNH *comp_nh = NULL;
// Out NH points to Composite. We only expect ECMP Composite-NH
// Find the out_component_nh_idx
if (nh->GetType() == NextHop::COMPOSITE) {
// We dont expect L2 multicast packets. Return failure to drop packet
if (pkt->l3_forwarding == false)
return false;
comp_nh = static_cast<const CompositeNH *>(nh);
if (comp_nh->composite_nh_type() == Composite::ECMP ||
comp_nh->composite_nh_type() == Composite::LOCAL_ECMP) {
info->ecmp = true;
const CompositeNH *comp_nh = static_cast<const CompositeNH *>(nh);
// Compute out_component_nh_idx if,
// 1. out_compoenent_nh_idx was set, but points to a deleted NH
// This can happen if flow is trapped for ECMP resolution from
// vrouter
// 2. New flow being setup
// 3. If flow transitions from Non-ECMP to ECMP, the
// out_component_nh_idx is set in RewritePktInfo. We want to
// retain the index
if (info->out_component_nh_idx ==
CompositeNH::kInvalidComponentNHIdx ||
(comp_nh->GetNH(info->out_component_nh_idx) == NULL)) {
info->out_component_nh_idx = comp_nh->hash(pkt->hash());
}
nh = comp_nh->GetNH(info->out_component_nh_idx);
// TODO: Should we re-hash here?
if (nh->IsActive() == false) {
return false;
}
}
} else {
info->out_component_nh_idx = CompositeNH::kInvalidComponentNHIdx;
}
NextHopTable *nh_table = info->flow_table->agent()->nexthop_table();
// Pick out going attributes based on the NH selected above
switch (nh->GetType()) {
case NextHop::INTERFACE:
out->intf_ = static_cast<const InterfaceNH*>(nh)->GetInterface();
if (out->intf_->type() == Interface::VM_INTERFACE) {
//Local flow, pick destination interface
//nexthop as reverse flow key
out->nh_ = out->intf_->flow_key_nh()->id();
out->vrf_ = static_cast<const InterfaceNH*>(nh)->GetVrf();
} else if (out->intf_->type() == Interface::PACKET) {
//Packet destined to pkt interface, packet originating
//from pkt0 interface will use destination interface as key
out->nh_ = in->nh_;
} else {
// Most likely a GATEWAY interface.
// Remote flow, use source interface as nexthop key
out->nh_ = nh->id();
out->vrf_ = static_cast<const InterfaceNH*>(nh)->GetVrf();
}
break;
case NextHop::RECEIVE:
assert(pkt->l3_forwarding == true);
out->intf_ = static_cast<const ReceiveNH *>(nh)->GetInterface();
out->vrf_ = out->intf_->vrf();
out->nh_ = GetPolicyDisabledNH(nh_table, nh)->id();
break;
case NextHop::VLAN: {
assert(pkt->l3_forwarding == true);
const VlanNH *vlan_nh = static_cast<const VlanNH*>(nh);
out->intf_ = vlan_nh->GetInterface();
out->vlan_nh_ = true;
out->vlan_tag_ = vlan_nh->GetVlanTag();
out->vrf_ = vlan_nh->GetVrf();
out->nh_ = nh->id();
break;
}
// Destination present on remote-compute node. The reverse traffic will
// have MPLS label. The MPLS label can point to
// 1. In case of non-ECMP, label will points to local interface
// 2. In case of ECMP, label will point to ECMP of local-composite members
case NextHop::TUNNEL: {
if (pkt->l3_forwarding) {
const InetUnicastRouteEntry *rt =
static_cast<const InetUnicastRouteEntry *>(in->rt_);
if (rt != NULL && rt->GetLocalNextHop()) {
const NextHop *local_nh = rt->GetLocalNextHop();
out->nh_ = local_nh->id();
if (local_nh->GetType() == NextHop::INTERFACE) {
const Interface *local_intf =
static_cast<const InterfaceNH*>(local_nh)->GetInterface();
//Get policy enabled nexthop only for
//vm interface, in case of vgw or service interface in
//transparent mode we should still
//use policy disabled interface
if (local_intf &&
local_intf->type() == Interface::VM_INTERFACE) {
out->nh_ = GetPolicyEnabledNH(nh_table, local_nh)->id();
}
}
} else {
out->nh_ = in->nh_;
}
} else {
// Bridged flow. ECMP not supported for L2 flows
out->nh_ = in->nh_;
}
out->intf_ = NULL;
break;
}
// COMPOSITE is valid only for multicast traffic. We simply forward
// multicast traffic and its mostly unidirectional. nh_ used in reverse
// flow woule not matter really
case NextHop::COMPOSITE: {
out->nh_ = nh->id();
out->intf_ = NULL;
break;
}
// VRF Nexthop means traffic came as tunnelled packet and interface is
// gateway kind of interface. It also means ARP is not yet resolved for the
// dest-ip (otherwise we should have it ARP-NH). Let out->nh_ to be same as
// in->nh_. It will be modified later when ARP is resolved
case NextHop::VRF: {
const VrfNH *vrf_nh = static_cast<const VrfNH *>(nh);
out->vrf_ = vrf_nh->GetVrf();
break;
}
// ARP Nexthop means outgoing interface is gateway kind of interface with
// ARP already resolved
case NextHop::ARP: {
assert(pkt->l3_forwarding == true);
const ArpNH *arp_nh = static_cast<const ArpNH *>(nh);
if (in->intf_->type() == Interface::VM_INTERFACE) {
const VmInterface *vm_intf =
static_cast<const VmInterface *>(in->intf_);
if (vm_intf->device_type() == VmInterface::LOCAL_DEVICE) {
out->nh_ = arp_nh->id();
} else {
out->nh_ = arp_nh->GetInterface()->flow_key_nh()->id();
}
}
out->intf_ = arp_nh->GetInterface();
out->vrf_ = arp_nh->GetVrf();
break;
}
// RESOLVE Nexthop means traffic came from gateway interface and destined
// to another gateway interface
case NextHop::RESOLVE: {
assert(pkt->l3_forwarding == true);
const ResolveNH *rsl_nh = static_cast<const ResolveNH *>(nh);
out->nh_ = rsl_nh->interface()->flow_key_nh()->id();
out->intf_ = rsl_nh->interface();
break;
}
default:
out->intf_ = NULL;
break;
}
if (out->intf_) {
if (!out->intf_->IsActive()) {
out->intf_ = NULL;
ret = false;
} else if (force_vmport && IsVgwOrVmInterface(out->intf_) == false) {
out->intf_ = NULL;
out->vrf_ = NULL;
ret = true;
}
}
if (out->vrf_ && (out->vrf_->IsActive() == false)) {
out->vrf_ = NULL;
ret = false;
}
return ret;
}
// Decode route and get Interface / ECMP information
static bool RouteToOutInfo(const AgentRoute *rt, const PktInfo *pkt,
PktFlowInfo *info, PktControlInfo *in,
PktControlInfo *out) {
Agent *agent = static_cast<AgentRouteTable *>(rt->get_table())->agent();
const AgentPath *path = rt->GetActivePath();
if (path == NULL)
return false;
const NextHop *nh = static_cast<const NextHop *>(path->ComputeNextHop(agent));
if (nh == NULL)
return false;
if (nh->IsActive() == false) {
return false;
}
return NhDecode(nh, pkt, info, in, out, false);
}
static const VnEntry *InterfaceToVn(const Interface *intf) {
if (intf->type() != Interface::VM_INTERFACE)
return NULL;
const VmInterface *vm_port = static_cast<const VmInterface *>(intf);
return vm_port->vn();
}
static bool IntfHasFloatingIp(PktFlowInfo *pkt_info, const Interface *intf,
Address::Family family) {
if (pkt_info->l3_flow == false)
return false;
if (!intf || intf->type() != Interface::VM_INTERFACE)
return false;
return static_cast<const VmInterface *>(intf)->HasFloatingIp(family);
}
static bool IsLinkLocalRoute(Agent *agent, const AgentRoute *rt) {
const AgentPath *path = rt->GetActivePath();
if (path && path->peer() == agent->link_local_peer())
return true;
return false;
}
static const string *RouteToVn(const AgentRoute *rt) {
const AgentPath *path = NULL;
if (rt) {
path = rt->GetActivePath();
}
if (path == NULL) {
return &(Agent::NullString());
}
return &path->dest_vn_name();
}
static void SetInEcmpIndex(const PktInfo *pkt, PktFlowInfo *flow_info,
PktControlInfo *in, PktControlInfo *out) {
if (!in->rt_) {
return;
}
if (in->rt_->GetActiveNextHop()->GetType() != NextHop::COMPOSITE) {
return;
}
if (pkt->family == Address::INET6) {
//TODO::ECMP for v6
}
Agent *agent = static_cast<AgentRouteTable *>(in->rt_->get_table())->agent();
const InetUnicastRouteEntry *rt =
static_cast<const InetUnicastRouteEntry *>(in->rt_);
NextHop *component_nh_ptr = NULL;
uint32_t label;
//Frame key for component NH
if (flow_info->ingress) {
//Ingress flow
const VmInterface *vm_port = static_cast<const VmInterface *>(in->intf_);
const VrfEntry *vrf = agent->vrf_table()->FindVrfFromId(pkt->vrf);
if (vm_port->HasServiceVlan() && vm_port->vrf() != vrf) {
//Packet came on service VRF
label = vm_port->GetServiceVlanLabel(vrf);
uint32_t vlan = vm_port->GetServiceVlanTag(vrf);
VlanNHKey key(vm_port->GetUuid(), vlan);
component_nh_ptr =
static_cast<NextHop *>
(agent->nexthop_table()->FindActiveEntry(&key));
} else {
InterfaceNHKey key(static_cast<InterfaceKey *>
(vm_port->GetDBRequestKey().release()),
false, InterfaceNHFlags::INET4);
component_nh_ptr =
static_cast<NextHop *>
(agent->nexthop_table()->FindActiveEntry(&key));
label = vm_port->label();
}
} else {
//Packet from fabric
Ip4Address dest_ip(pkt->tunnel.ip_saddr);
TunnelNHKey key(agent->fabric_vrf_name(), agent->router_id(),
dest_ip, false, pkt->tunnel.type);
//Get component NH pointer
component_nh_ptr =
static_cast<NextHop *>
(agent->nexthop_table()->FindActiveEntry(&key));
//Get Label to be used to reach destination server
const CompositeNH *nh =
static_cast<const CompositeNH *>(rt->GetActiveNextHop());
label = nh->GetRemoteLabel(dest_ip);
}
ComponentNH component_nh(label, component_nh_ptr);
const NextHop *nh = NULL;
if (out->intf_) {
//Local destination, use active path
nh = rt->GetActiveNextHop();
} else {
//Destination on remote server
//Choose local path, which will also pointed by MPLS label
if (rt->FindPath(agent->ecmp_peer())) {
nh = rt->FindPath(agent->ecmp_peer())->ComputeNextHop(agent);
} else {
//Aggregarated routes may not have local path
//Derive local path
nh = rt->GetLocalNextHop();
}
}
if (nh && nh->GetType() == NextHop::COMPOSITE) {
const CompositeNH *comp_nh = static_cast<const CompositeNH *>(nh);
//Find component entry index in composite NH
uint32_t idx = 0;
if (comp_nh->GetIndex(component_nh, idx)) {
flow_info->in_component_nh_idx = idx;
flow_info->ecmp = true;
}
} else {
//Ideally this case is not ecmp, as on reverse flow we are hitting
//a interface NH and not composite NH, install reverse flow for consistency
flow_info->ecmp = true;
}
}
static bool RouteAllowNatLookup(Agent *agent, const AgentRoute *rt) {
// No NAT for bridge routes
if (dynamic_cast<const BridgeRouteEntry *>(rt) != NULL)
return false;
if (rt != NULL && IsLinkLocalRoute(agent, rt)) {
// skip NAT lookup if found route has link local peer.
return false;
}
return true;
}
void PktFlowInfo::SetEcmpFlowInfo(const PktInfo *pkt, const PktControlInfo *in,
const PktControlInfo *out) {
nat_ip_daddr = pkt->ip_daddr;
nat_ip_saddr = pkt->ip_saddr;
nat_dport = pkt->dport;
nat_sport = pkt->sport;
if (out->intf_ && out->intf_->type() == Interface::VM_INTERFACE) {
dest_vrf = out->vrf_->vrf_id();
} else {
dest_vrf = pkt->vrf;
}
nat_vrf = dest_vrf;
nat_dest_vrf = pkt->vrf;
}
void PktFlowInfo::CheckLinkLocal(const PktInfo *pkt) {
if (!l3_flow && pkt->ip_daddr.is_v4()) {
uint16_t nat_port;
Ip4Address nat_server;
std::string service_name;
Agent *agent = flow_table->agent();
if (agent->oper_db()->global_vrouter()->
FindLinkLocalService(pkt->ip_daddr.to_v4(), pkt->dport,
&service_name, &nat_server, &nat_port)) {
// it is link local service request, treat it as l3
l3_flow = true;
pkt->l3_forwarding = true;
}
}
}
// For link local services, we bind to a local port & use it as NAT source port.
// The socket is closed when the flow entry is deleted.
uint32_t PktFlowInfo::LinkLocalBindPort(const VmEntry *vm, uint8_t proto) {
if (vm == NULL)
return 0;
// Do not allow more than max link local flows
if (flow_table->linklocal_flow_count() >=
flow_table->agent()->params()->linklocal_system_flows())
return 0;
if (flow_table->VmLinkLocalFlowCount(vm) >=
flow_table->agent()->params()->linklocal_vm_flows())
return 0;
if (proto == IPPROTO_TCP) {
linklocal_src_port_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
} else if (proto == IPPROTO_UDP) {
linklocal_src_port_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
}
if (linklocal_src_port_fd == -1) {
return 0;
}
// allow the socket to be reused upon close
int optval = 1;
setsockopt(linklocal_src_port_fd, SOL_SOCKET, SO_REUSEADDR,
&optval, sizeof(optval));
struct sockaddr_in address;
memset(&address, '0', sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = 0;
address.sin_port = 0;
struct sockaddr_in bound_to;
socklen_t len = sizeof(bound_to);
if (bind(linklocal_src_port_fd, (struct sockaddr*) &address,
sizeof(address)) == -1) {
goto error;
}
if (getsockname(linklocal_src_port_fd, (struct sockaddr*) &bound_to,
&len) == -1) {
goto error;
}
return ntohs(bound_to.sin_port);
error:
if (linklocal_src_port_fd != kLinkLocalInvalidFd) {
close(linklocal_src_port_fd);
linklocal_src_port_fd = kLinkLocalInvalidFd;
}
return 0;
}
void PktFlowInfo::LinkLocalServiceFromVm(const PktInfo *pkt, PktControlInfo *in,
PktControlInfo *out) {
// Link local services supported only for IPv4 for now
if (pkt->family != Address::INET) {
in->rt_ = NULL;
out->rt_ = NULL;
return;
}
const VmInterface *vm_port =
static_cast<const VmInterface *>(in->intf_);
uint16_t nat_port;
Ip4Address nat_server;
std::string service_name;
if (!flow_table->agent()->oper_db()->global_vrouter()->
FindLinkLocalService(pkt->ip_daddr.to_v4(), pkt->dport,
&service_name, &nat_server, &nat_port)) {
// link local service not configured, drop the request
in->rt_ = NULL;
out->rt_ = NULL;
return;
}
out->vrf_ = flow_table->agent()->vrf_table()->
FindVrfFromName(flow_table->agent()->fabric_vrf_name());
dest_vrf = out->vrf_->vrf_id();
// Set NAT flow fields
linklocal_flow = true;
nat_done = true;
if (nat_server == flow_table->agent()->router_id()) {
// In case of metadata or when link local destination is local host,
// set VM's metadata address as NAT source address. This is required
// to avoid response from the linklocal service being looped back and
// the packet not coming to vrouter for reverse NAT.
// Destination would be local host (FindLinkLocalService returns this)
nat_ip_saddr = vm_port->mdata_ip_addr();
// Services such as metadata will run on compute_node_ip. Set nat
// address to compute_node_ip
nat_server = flow_table->agent()->compute_node_ip();
nat_sport = pkt->sport;
} else {
nat_ip_saddr = flow_table->agent()->router_id();
// we bind to a local port & use it as NAT source port (cannot use
// incoming src port); init here and bind in Add;
nat_sport = 0;
linklocal_bind_local_port = true;
}
nat_ip_daddr = nat_server;
nat_dport = nat_port;
nat_vrf = dest_vrf;
nat_dest_vrf = vm_port->vrf_id();
out->rt_ = flow_table->GetUcRoute(out->vrf_, nat_server);
return;
}
void PktFlowInfo::LinkLocalServiceFromHost(const PktInfo *pkt, PktControlInfo *in,
PktControlInfo *out) {
if (RouteToOutInfo(out->rt_, pkt, this, in, out) == false) {
return;
}
// Link local services supported only for IPv4 for now
if (pkt->family != Address::INET) {
in->rt_ = NULL;
out->rt_ = NULL;
return;
}
const VmInterface *vm_port =
static_cast<const VmInterface *>(out->intf_);
if (vm_port == NULL) {
// Force implicit deny
in->rt_ = NULL;
out->rt_ = NULL;
return;
}
// Check if packet is destined to metadata of interface
if (pkt->ip_daddr.to_v4() != vm_port->mdata_ip_addr()) {
// Force implicit deny
in->rt_ = NULL;
out->rt_ = NULL;
return;
}
dest_vrf = vm_port->vrf_id();
out->vrf_ = vm_port->vrf();
linklocal_flow = true;
nat_done = true;
nat_ip_saddr = Ip4Address(METADATA_IP_ADDR);
nat_ip_daddr = vm_port->primary_ip_addr();
nat_dport = pkt->dport;
if (pkt->sport == flow_table->agent()->metadata_server_port()) {
nat_sport = METADATA_NAT_PORT;
} else {
nat_sport = pkt->sport;
}
nat_vrf = dest_vrf;
nat_dest_vrf = pkt->vrf;
return;
}
void PktFlowInfo::LinkLocalServiceTranslate(const PktInfo *pkt, PktControlInfo *in,
PktControlInfo *out) {
if (in->intf_->type() == Interface::VM_INTERFACE) {
LinkLocalServiceFromVm(pkt, in, out);
} else {
LinkLocalServiceFromHost(pkt, in, out);
}
}
// DestNAT for packets entering into a VM with floating-ip.
// Can come here in two paths,
// - Packet originated on local vm.
// - Packet originated from remote vm
void PktFlowInfo::FloatingIpDNat(const PktInfo *pkt, PktControlInfo *in,
PktControlInfo *out) {
const VmInterface *vm_port =
static_cast<const VmInterface *>(out->intf_);
const VmInterface::FloatingIpSet &fip_list =
vm_port->floating_ip_list().list_;
// We must NAT if the IP-DA is not same as Primary-IP on interface
if (pkt->ip_daddr.to_v4() == vm_port->primary_ip_addr()) {
return;
}
// Look for matching floating-ip
VmInterface::FloatingIpSet::const_iterator it = fip_list.begin();
for ( ; it != fip_list.end(); ++it) {
if (it->vrf_.get() == NULL) {
continue;
}
if (pkt->ip_daddr.to_v4() == it->floating_ip_) {
break;
}
}
if (it == fip_list.end()) {
// No matching floating ip for destination-ip
return;
}
in->vn_ = NULL;
if (nat_done == false) {
UpdateRoute(&in->rt_, it->vrf_.get(), pkt->ip_saddr, pkt->smac,
flow_source_plen_map);
nat_dest_vrf = it->vrf_.get()->vrf_id();
}
UpdateRoute(&out->rt_, it->vrf_.get(), pkt->ip_daddr, pkt->dmac,
flow_dest_plen_map);
out->vn_ = it->vn_.get();
dest_vrf = out->intf_->vrf()->vrf_id();
// Translate the Dest-IP
if (nat_done == false)
nat_ip_saddr = pkt->ip_saddr;
nat_ip_daddr = vm_port->primary_ip_addr();
nat_sport = pkt->sport;
nat_dport = pkt->dport;
nat_vrf = dest_vrf;
nat_done = true;
if (in->rt_) {
flow_source_vrf = static_cast<const AgentRoute *>(in->rt_)->vrf_id();
} else {
flow_source_vrf = VrfEntry::kInvalidIndex;
}
flow_dest_vrf = it->vrf_.get()->vrf_id();
// Update fields required for floating-IP stats accounting
fip_dnat = true;
return;
}
void PktFlowInfo::FloatingIpSNat(const PktInfo *pkt, PktControlInfo *in,
PktControlInfo *out) {
if (pkt->family == Address::INET6) {
return;
//TODO: V6 FIP
}
const VmInterface *intf =
static_cast<const VmInterface *>(in->intf_);
const VmInterface::FloatingIpSet &fip_list = intf->floating_ip_list().list_;
VmInterface::FloatingIpSet::const_iterator it = fip_list.begin();
VmInterface::FloatingIpSet::const_iterator fip_it = fip_list.end();
const AgentRoute *rt = out->rt_;
bool change = false;
// Find Floating-IP matching destination-ip
for ( ; it != fip_list.end(); ++it) {
if (it->vrf_.get() == NULL) {
continue;
}
const AgentRoute *rt_match = flow_table->GetUcRoute(it->vrf_.get(),
pkt->ip_daddr);
if (rt_match == NULL) {
flow_dest_plen_map[it->vrf_.get()->vrf_id()] = 0;
continue;
}
uint8_t out_rt_plen = RouteToPrefixLen(out->rt_);
uint8_t rt_match_plen = RouteToPrefixLen(rt_match);
// found the route match
// prefer the route with longest prefix match
// if prefix length is same prefer route from floating IP
// if routes are from fip of difference VRF, prefer the one with lower name.
// if both the selected and current FIP is from same vrf prefer the one with lower ip addr.
if (out->rt_ == NULL || rt_match_plen > out_rt_plen) {
change = true;
} else if (rt_match_plen == out_rt_plen) {
if (fip_it == fip_list.end()) {
change = true;
} else if (rt_match->vrf()->GetName() < out->rt_->vrf()->GetName()) {
change = true;
} else if (rt_match->vrf()->GetName() == out->rt_->vrf()->GetName() &&
it->floating_ip_ < fip_it->floating_ip_) {
change = true;
}
}
if (change) {
if (out->rt_ != NULL) {
flow_dest_plen_map[out->rt_->vrf_id()] = RouteToPrefixLen(out->rt_);
}
out->rt_ = rt_match;
fip_it = it;
change = false;
} else {
flow_dest_plen_map[rt_match->vrf_id()] = RouteToPrefixLen(rt_match);
}
}
if (out->rt_ == rt) {
// No change in route, no floating-ip found
return;
}
//Populate in->vn, used for VRF translate ACL lookup
in->vn_ = fip_it->vn_.get();
// Floating-ip found. We will change src-ip to floating-ip. Recompute route
// for new source-ip. All policy decisions will be based on this new route
UpdateRoute(&in->rt_, fip_it->vrf_.get(), fip_it->floating_ip_, pkt->smac,
flow_source_plen_map);
if (in->rt_ == NULL) {
return;
}
if (VrfTranslate(pkt, in, out, fip_it->floating_ip_, true) == false) {
return;
}
if (out->rt_ == NULL || in->rt_ == NULL) {
//If After VRF translation, ingress route or
//egress route is NULL, mark the flow as short flow
return;
}
// Compute out-intf and ECMP info from out-route
if (RouteToOutInfo(out->rt_, pkt, this, in, out) == false) {
return;
}
dest_vrf = out->rt_->vrf_id();
// Setup reverse flow to translate sip.
nat_done = true;
nat_ip_saddr = fip_it->floating_ip_;
nat_ip_daddr = pkt->ip_daddr;
nat_sport = pkt->sport;
nat_dport = pkt->dport;
// Compute VRF for reverse flow
if (out->intf_) {
// Egress-vm present on same compute node, take VRF from vm-port
nat_vrf = out->vrf_->vrf_id();
out->vn_ = InterfaceToVn(out->intf_);
} else {
// Egress-vm is remote. Find VRF from the NH for source-ip
nat_vrf = NhToVrf(in->rt_->GetActiveNextHop());
}
// Dest VRF for reverse flow is In-Port VRF
nat_dest_vrf = intf->vrf_id();
flow_source_vrf = pkt->vrf;
if (out->rt_) {
flow_dest_vrf = dest_vrf;
} else {
flow_dest_vrf = VrfEntry::kInvalidIndex;
}
// Update fields required for floating-IP stats accounting
snat_fip = nat_ip_saddr;
fip_snat = true;
return;
}
bool PktFlowInfo::VrfTranslate(const PktInfo *pkt, PktControlInfo *in,
PktControlInfo *out, const IpAddress &src_ip,
bool nat_flow) {
const Interface *intf = NULL;
if (ingress) {
intf = in->intf_;
} else {
intf = out->intf_;
}
if (!intf || intf->type() != Interface::VM_INTERFACE) {
return true;
}
const VmInterface *vm_intf = static_cast<const VmInterface *>(intf);
//If interface has a VRF assign rule, choose the acl and match the
//packet, else get the acl attached to VN and try matching the packet to
//network acl
const AclDBEntry *acl = NULL;
if (nat_flow == false) {
acl = vm_intf->vrf_assign_acl();
}
//In case of floating IP translation, dont apply
//interface VRF assign rule
if (acl == NULL) {
if (ingress && in->vn_) {
//Check if the network ACL is present
acl = in->vn_->GetAcl();
} else if (out->vn_) {
acl = out->vn_->GetAcl();
}
}
if (!acl) {
return true;
}
PacketHeader hdr;
hdr.vrf = pkt->vrf;
hdr.src_ip = src_ip;
hdr.dst_ip = pkt->ip_daddr;
hdr.protocol = pkt->ip_proto;
if (hdr.protocol == IPPROTO_UDP || hdr.protocol == IPPROTO_TCP) {
hdr.src_port = pkt->sport;
hdr.dst_port = pkt->dport;
} else {
hdr.src_port = 0;
hdr.dst_port = 0;
}
hdr.src_policy_id = RouteToVn(in->rt_);
hdr.dst_policy_id = RouteToVn(out->rt_);
if (in->rt_) {
const AgentPath *path = in->rt_->GetActivePath();
hdr.src_sg_id_l = &(path->sg_list());
}
if (out->rt_) {
const AgentPath *path = out->rt_->GetActivePath();
hdr.dst_sg_id_l = &(path->sg_list());
}
MatchAclParams match_acl_param;
if (!acl->PacketMatch(hdr, match_acl_param, NULL)) {
return true;
}
if (match_acl_param.action_info.vrf_translate_action_.vrf_name() != "") {
VrfKey key(match_acl_param.action_info.vrf_translate_action_.vrf_name());
const VrfEntry *vrf = static_cast<const VrfEntry*>
(flow_table->agent()->vrf_table()->FindActiveEntry(&key));
if (vrf == NULL) {
short_flow = true;
short_flow_reason = FlowEntry::SHORT_UNAVIALABLE_VRF;
in->rt_ = NULL;
out->rt_ = NULL;
return false;