-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathgateway_linux.go
1675 lines (1503 loc) · 62.6 KB
/
gateway_linux.go
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
package daemon
import (
"context"
"errors"
"fmt"
"net"
"os"
"slices"
"sort"
"strconv"
"strings"
"syscall"
"github.com/kubeovn/felix/ipsets"
"github.com/kubeovn/go-iptables/iptables"
"github.com/scylladb/go-set/strset"
"github.com/vishvananda/netlink"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
"k8s.io/utils/set"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/ovs"
"github.com/kubeovn/kube-ovn/pkg/util"
)
const (
ServiceSet = "services"
SubnetSet = "subnets"
SubnetNatSet = "subnets-nat"
SubnetDistributedGwSet = "subnets-distributed-gw"
LocalPodSet = "local-pod-ip-nat"
OtherNodeSet = "other-node"
IPSetPrefix = "ovn"
NatOutGoingPolicySubnetSet = "subnets-nat-policy"
NatOutGoingPolicyRuleSet = "natpr-"
)
const (
NAT = util.NAT
MANGLE = util.Mangle
Prerouting = util.Prerouting
Postrouting = util.Postrouting
Output = util.Output
OvnPrerouting = util.OvnPrerouting
OvnPostrouting = util.OvnPostrouting
OvnOutput = util.OvnOutput
OvnMasquerade = util.OvnMasquerade
OvnNatOutGoingPolicy = util.OvnNatOutGoingPolicy
OvnNatOutGoingPolicySubnet = util.OvnNatOutGoingPolicySubnet
)
const (
OnOutGoingNatMark = "0x90001/0x90001"
OnOutGoingForwardMark = "0x90002/0x90002"
TProxyOutputMark = util.TProxyOutputMark
TProxyOutputMask = util.TProxyOutputMask
TProxyPreroutingMark = util.TProxyPreroutingMark
TProxyPreroutingMask = util.TProxyPreroutingMask
)
var (
tProxyOutputMarkMask = fmt.Sprintf("%#x/%#x", TProxyOutputMark, TProxyOutputMask)
tProxyPreRoutingMarkMask = fmt.Sprintf("%#x/%#x", TProxyPreroutingMark, TProxyPreroutingMask)
)
type policyRouteMeta struct {
family int
source string
gateway string
tableID uint32
priority uint32
}
func (c *Controller) setIPSet() error {
protocols := make([]string, 2)
if c.protocol == kubeovnv1.ProtocolDual {
protocols[0] = kubeovnv1.ProtocolIPv4
protocols[1] = kubeovnv1.ProtocolIPv6
} else {
protocols[0] = c.protocol
}
for _, protocol := range protocols {
if c.ipsets[protocol] == nil {
continue
}
services := c.getServicesCIDR(protocol)
subnets, _, err := c.getDefaultVpcSubnetsCIDR(protocol)
if err != nil {
klog.Errorf("get subnets failed, %+v", err)
return err
}
subnetsNeedNat, err := c.getSubnetsNeedNAT(protocol)
if err != nil {
klog.Errorf("get need nat subnets failed, %+v", err)
return err
}
subnetsDistributedGateway, err := c.getSubnetsDistributedGateway(protocol)
if err != nil {
klog.Errorf("failed to get subnets with centralized gateway: %v", err)
return err
}
otherNode, err := c.getOtherNodes(protocol)
if err != nil {
klog.Errorf("failed to get node, %+v", err)
return err
}
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: ServiceSet,
Type: ipsets.IPSetTypeHashNet,
}, services)
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: SubnetSet,
Type: ipsets.IPSetTypeHashNet,
}, subnets)
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: LocalPodSet,
Type: ipsets.IPSetTypeHashIP,
}, nil)
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: SubnetNatSet,
Type: ipsets.IPSetTypeHashNet,
}, subnetsNeedNat)
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: SubnetDistributedGwSet,
Type: ipsets.IPSetTypeHashNet,
}, subnetsDistributedGateway)
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: OtherNodeSet,
Type: ipsets.IPSetTypeHashNet,
}, otherNode)
c.reconcileNatOutGoingPolicyIPset(protocol)
c.ipsets[protocol].ApplyUpdates()
}
return nil
}
func (c *Controller) gcIPSet() {
protocols := make([]string, 2)
if c.protocol == kubeovnv1.ProtocolDual {
protocols[0] = kubeovnv1.ProtocolIPv4
protocols[1] = kubeovnv1.ProtocolIPv6
} else {
protocols[0] = c.protocol
}
for _, protocol := range protocols {
if c.ipsets[protocol] == nil {
continue
}
c.ipsets[protocol].ApplyDeletions()
}
}
func (c *Controller) addNatOutGoingPolicyRuleIPset(rule kubeovnv1.NatOutgoingPolicyRuleStatus, protocol string) {
if rule.Match.SrcIPs != "" {
ipsetName := getNatOutGoingPolicyRuleIPSetName(rule.RuleID, "src", "", false)
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: ipsetName,
Type: ipsets.IPSetTypeHashNet,
}, strings.Split(rule.Match.SrcIPs, ","))
}
if rule.Match.DstIPs != "" {
ipsetName := getNatOutGoingPolicyRuleIPSetName(rule.RuleID, "dst", "", false)
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: ipsetName,
Type: ipsets.IPSetTypeHashNet,
}, strings.Split(rule.Match.DstIPs, ","))
}
}
func (c *Controller) removeNatOutGoingPolicyRuleIPset(protocol string, natPolicyRuleIDs *strset.Set) {
sets, err := c.k8sipsets.ListSets()
if err != nil {
klog.Errorf("failed to list ipsets: %v", err)
return
}
for _, set := range sets {
if isNatOutGoingPolicyRuleIPSet(set) {
ruleID, _ := getNatOutGoingPolicyRuleIPSetItem(set)
if !natPolicyRuleIDs.Has(ruleID) {
c.ipsets[protocol].RemoveIPSet(formatIPsetUnPrefix(set))
}
}
}
}
func (c *Controller) reconcileNatOutGoingPolicyIPset(protocol string) {
subnets, err := c.getSubnetsNatOutGoingPolicy(protocol)
if err != nil {
klog.Errorf("failed to get subnets with NAT outgoing policy rule: %v", err)
return
}
subnetCidrs := make([]string, 0, len(subnets))
natPolicyRuleIDs := strset.New()
for _, subnet := range subnets {
cidrBlock, err := getCidrByProtocol(subnet.Spec.CIDRBlock, protocol)
if err != nil {
klog.Errorf("failed to get subnet %s CIDR block by protocol: %v", subnet.Name, err)
continue
}
if cidrBlock != "" {
subnetCidrs = append(subnetCidrs, cidrBlock)
}
for _, rule := range subnet.Status.NatOutgoingPolicyRules {
if rule.RuleID == "" {
klog.Errorf("unexpected empty ID for NAT outgoing rule %q of subnet %s", rule.NatOutgoingPolicyRule, subnet.Name)
continue
}
natPolicyRuleIDs.Add(rule.RuleID)
c.addNatOutGoingPolicyRuleIPset(rule, protocol)
}
}
c.ipsets[protocol].AddOrReplaceIPSet(ipsets.IPSetMetadata{
MaxSize: 1048576,
SetID: NatOutGoingPolicySubnetSet,
Type: ipsets.IPSetTypeHashNet,
}, subnetCidrs)
c.removeNatOutGoingPolicyRuleIPset(protocol, natPolicyRuleIDs)
}
func (c *Controller) setPolicyRouting() error {
protocols := make([]string, 2)
if c.protocol == kubeovnv1.ProtocolDual {
protocols[0] = kubeovnv1.ProtocolIPv4
protocols[1] = kubeovnv1.ProtocolIPv6
} else {
protocols[0] = c.protocol
}
for _, protocol := range protocols {
if c.ipsets[protocol] == nil {
continue
}
localPodIPs, err := c.getLocalPodIPsNeedPR(protocol)
if err != nil {
klog.Errorf("failed to get local pod ips failed: %+v", err)
return err
}
subnetsNeedPR, err := c.getSubnetsNeedPR(protocol)
if err != nil {
klog.Errorf("failed to get subnets that need policy routing: %+v", err)
return err
}
family, err := util.ProtocolToFamily(protocol)
if err != nil {
klog.Error(err)
return err
}
for meta, ips := range localPodIPs {
if err = c.addPolicyRouting(family, meta.gateway, meta.priority, meta.tableID, ips...); err != nil {
klog.Errorf("failed to add policy routing for local pods: %+v", err)
return err
}
}
for meta, cidr := range subnetsNeedPR {
if err = c.addPolicyRouting(family, meta.gateway, meta.priority, meta.tableID, cidr); err != nil {
klog.Errorf("failed to add policy routing for subnet: %+v", err)
return err
}
}
}
return nil
}
func (c *Controller) addPodPolicyRouting(podProtocol, externalEgressGateway string, priority, tableID uint32, ips []string) error {
egw := strings.Split(externalEgressGateway, ",")
prMetas := make([]policyRouteMeta, 0, 2)
if len(egw) == 1 {
family, _ := util.ProtocolToFamily(util.CheckProtocol(egw[0]))
if family == netlink.FAMILY_V4 || podProtocol != kubeovnv1.ProtocolDual {
prMetas = append(prMetas, policyRouteMeta{family: family, source: ips[0], gateway: egw[0]})
} else {
prMetas = append(prMetas, policyRouteMeta{family: family, source: ips[1], gateway: egw[0]})
}
} else {
prMetas = append(prMetas, policyRouteMeta{family: netlink.FAMILY_V4, source: ips[0], gateway: egw[0]})
prMetas = append(prMetas, policyRouteMeta{family: netlink.FAMILY_V6, source: ips[1], gateway: egw[1]})
}
for _, meta := range prMetas {
if err := c.addPolicyRouting(meta.family, meta.gateway, priority, tableID, meta.source); err != nil {
klog.Errorf("failed to add policy routing for pod: %+v", err)
return err
}
}
return nil
}
func (c *Controller) deletePodPolicyRouting(podProtocol, externalEgressGateway string, priority, tableID uint32, ips []string) error {
egw := strings.Split(externalEgressGateway, ",")
prMetas := make([]policyRouteMeta, 0, 2)
if len(egw) == 1 {
family, _ := util.ProtocolToFamily(util.CheckProtocol(egw[0]))
if family == netlink.FAMILY_V4 || podProtocol != kubeovnv1.ProtocolDual {
prMetas = append(prMetas, policyRouteMeta{family: family, source: ips[0], gateway: egw[0]})
} else {
prMetas = append(prMetas, policyRouteMeta{family: family, source: ips[1], gateway: egw[0]})
}
} else {
prMetas = append(prMetas, policyRouteMeta{family: netlink.FAMILY_V4, source: ips[0], gateway: egw[0]})
prMetas = append(prMetas, policyRouteMeta{family: netlink.FAMILY_V6, source: ips[1], gateway: egw[1]})
}
for _, meta := range prMetas {
if err := c.deletePolicyRouting(meta.family, meta.gateway, priority, tableID, meta.source); err != nil {
klog.Errorf("failed to delete policy routing for pod: %+v", err)
return err
}
}
return nil
}
func (c *Controller) addPolicyRouting(family int, gateway string, priority, tableID uint32, ips ...string) error {
route := &netlink.Route{
Protocol: netlink.RouteProtocol(syscall.RTPROT_STATIC),
Gw: net.ParseIP(gateway),
Table: int(tableID),
}
if err := netlink.RouteReplace(route); err != nil && !errors.Is(err, syscall.EEXIST) {
err = fmt.Errorf("failed to replace route in table %d: %w", tableID, err)
klog.Error(err)
return err
}
maskBits := 32
if family == netlink.FAMILY_V6 {
maskBits = 128
}
rule := netlink.NewRule()
rule.Family = family
rule.Table = int(tableID)
rule.Priority = int(priority)
mask := net.CIDRMask(maskBits, maskBits)
for _, ip := range ips {
if strings.ContainsRune(ip, '/') {
var err error
if rule.Src, err = netlink.ParseIPNet(ip); err != nil {
klog.Errorf("unexpected CIDR: %s", ip)
err = fmt.Errorf("failed to add route in table %d: %w", tableID, err)
klog.Error(err)
return err
}
} else {
rule.Src = &net.IPNet{IP: net.ParseIP(ip), Mask: mask}
}
if err := netlink.RuleAdd(rule); err != nil && !errors.Is(err, syscall.EEXIST) {
err = fmt.Errorf("failed to add network rule: %w", err)
klog.Error(err)
return err
}
}
return nil
}
func (c *Controller) deletePolicyRouting(family int, _ string, priority, tableID uint32, ips ...string) error {
maskBits := 32
if family == netlink.FAMILY_V6 {
maskBits = 128
}
rule := netlink.NewRule()
rule.Family = family
rule.Table = int(tableID)
rule.Priority = int(priority)
mask := net.CIDRMask(maskBits, maskBits)
for _, ip := range ips {
if strings.ContainsRune(ip, '/') {
var err error
if rule.Src, err = netlink.ParseIPNet(ip); err != nil {
klog.Errorf("unexpected CIDR: %s", ip)
err = fmt.Errorf("failed to delete route in table %d: %w", tableID, err)
klog.Error(err)
return err
}
} else {
rule.Src = &net.IPNet{IP: net.ParseIP(ip), Mask: mask}
}
if err := netlink.RuleDel(rule); err != nil && !errors.Is(err, syscall.ENOENT) {
err = fmt.Errorf("failed to delete network rule: %w", err)
klog.Error(err)
return err
}
}
// routes may be used by other Pods so delete rules only
return nil
}
func (c *Controller) createIptablesRule(ipt *iptables.IPTables, rule util.IPTableRule) error {
exists, err := ipt.Exists(rule.Table, rule.Chain, rule.Rule...)
if err != nil {
klog.Errorf("failed to check iptables rule existence: %v", err)
return err
}
s := strings.Join(rule.Rule, " ")
if exists {
if rule.Table == NAT && rule.Chain == Prerouting {
// make sure the nat prerouting iptable rule is in the first position
natPreroutingRules, err := ipt.List(rule.Table, rule.Chain)
if err != nil {
klog.Errorf("failed to list iptables rules: %v", err)
return err
}
for i, r := range natPreroutingRules {
ruleSpec := util.DoubleQuotedFields(r)
if i == 0 || len(ruleSpec) < 3 {
continue
}
if i == 1 {
if slices.Equal(ruleSpec[2:], rule.Rule) {
klog.V(3).Infof("the first nat prerouting rule is %q", rule.Rule)
continue
}
// iptables -t nat -F could cause this case, auto fix it
klog.Infof("insert nat prerouting rule: %q", rule.Rule)
if err = ipt.Insert(rule.Table, rule.Chain, 1, rule.Rule...); err != nil {
klog.Errorf(`failed to insert iptables rule %q: %v`, s, err)
return err
}
return nil
}
if slices.Equal(ruleSpec[2:], rule.Rule) {
rule.Pos = strconv.Itoa(i)
klog.Warningf("delete the nat prerouting rule: %v", rule)
if err = deleteIptablesRule(ipt, rule); err != nil {
klog.Errorf("failed to delete rule %v: %v", rule, err)
return err
}
}
}
}
return nil
}
klog.Infof("creating iptables rule in table %s chain %s at position %d: %q", rule.Table, rule.Chain, 1, s)
if err = ipt.Insert(rule.Table, rule.Chain, 1, rule.Rule...); err != nil {
klog.Errorf(`failed to insert iptables rule "%s": %v`, s, err)
return err
}
return nil
}
func (c *Controller) updateIptablesChain(ipt *iptables.IPTables, table, chain, parent string, rules []util.IPTableRule) error {
ok, err := ipt.ChainExists(table, chain)
if err != nil {
klog.Errorf("failed to check existence of iptables chain %s in table %s: %v", chain, table, err)
return err
}
if !ok {
if err = ipt.NewChain(table, chain); err != nil {
klog.Errorf("failed to create iptables chain %s in table %s: %v", chain, table, err)
return err
}
klog.Infof("created iptables chain %s in table %s", chain, table)
}
if parent != "" {
comment := fmt.Sprintf("kube-ovn %s rules", strings.ToLower(parent))
rule := util.IPTableRule{
Table: table,
Chain: parent,
Rule: []string{"-m", "comment", "--comment", comment, "-j", chain},
}
if err = c.createIptablesRule(ipt, rule); err != nil {
klog.Errorf("failed to create iptables rule: %v", err)
return err
}
}
// list existing rules
ruleList, err := ipt.List(table, chain)
if err != nil {
klog.Errorf("failed to list iptables rules in chain %s/%s: %v", table, chain, err)
return err
}
// filter the heading default chain policy: -N OVN-POSTROUTING
ruleList = ruleList[1:]
// trim prefix: "-A OVN-POSTROUTING "
prefixLen := 4 + len(chain)
existingRules := make([][]string, 0, len(ruleList))
for _, r := range ruleList {
existingRules = append(existingRules, util.DoubleQuotedFields(r[prefixLen:]))
}
var added int
for i, rule := range rules {
if i-added < len(existingRules) && slices.Equal(existingRules[i-added], rule.Rule) {
klog.V(5).Infof("iptables rule %v already exists", rule.Rule)
continue
}
klog.Infof("creating iptables rule in table %s chain %s at position %d: %q", table, chain, i+1, strings.Join(rule.Rule, " "))
if err = ipt.Insert(table, chain, i+1, rule.Rule...); err != nil {
klog.Errorf(`failed to insert iptables rule %v: %v`, rule.Rule, err)
return err
}
added++
}
for i := len(existingRules) - 1; i >= len(rules)-added; i-- {
if err = ipt.Delete(table, chain, strconv.Itoa(i+added+1)); err != nil {
klog.Errorf(`failed to delete iptables rule %v: %v`, existingRules[i], err)
return err
}
klog.Infof("deleted iptables rule in table %s chain %s: %q", table, chain, strings.Join(existingRules[i], " "))
}
return nil
}
func (c *Controller) setIptables() error {
klog.V(3).Infoln("start to set up iptables")
node, err := c.nodesLister.Get(c.config.NodeName)
if err != nil {
klog.Errorf("failed to get node %s, %v", c.config.NodeName, err)
return err
}
nodeIPv4, nodeIPv6 := util.GetNodeInternalIP(*node)
nodeIPs := map[string]string{
kubeovnv1.ProtocolIPv4: nodeIPv4,
kubeovnv1.ProtocolIPv6: nodeIPv6,
}
centralGwNatIPs, err := c.getEgressNatIPByNode(c.config.NodeName)
if err != nil {
klog.Errorf("failed to get centralized subnets nat ips on node %s, %v", c.config.NodeName, err)
return err
}
klog.V(3).Infof("centralized subnets nat ips %v", centralGwNatIPs)
var (
v4Rules = []util.IPTableRule{
// mark packets from pod to service
{Table: NAT, Chain: OvnPrerouting, Rule: strings.Fields(`-i ovn0 -m set --match-set ovn40subnets src -m set --match-set ovn40services dst -j MARK --set-xmark 0x4000/0x4000`)},
// nat packets marked by kube-proxy or kube-ovn
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m mark --mark 0x4000/0x4000 -j ` + OvnMasquerade)},
// nat service traffic
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m set --match-set ovn40subnets src -m set --match-set ovn40subnets dst -j ` + OvnMasquerade)},
// do not nat node port service traffic with external traffic policy set to local
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m mark --mark 0x80000/0x80000 -m set --match-set ovn40subnets-distributed-gw dst -j RETURN`)},
// nat node port service traffic with external traffic policy set to local for subnets with centralized gateway
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m mark --mark 0x80000/0x80000 -j ` + OvnMasquerade)},
// do not nat reply packets in direct routing
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-p tcp -m tcp --tcp-flags SYN NONE -m conntrack --ctstate NEW -j RETURN`)},
// do not nat route traffic
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m set ! --match-set ovn40subnets src -m set ! --match-set ovn40other-node src -m set --match-set ovn40subnets-nat dst -j RETURN`)},
// nat outgoing policy rules
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(fmt.Sprintf(`-m set --match-set ovn40subnets-nat-policy src -m set ! --match-set ovn40subnets dst -j %s`, OvnNatOutGoingPolicy))},
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(fmt.Sprintf(`-m mark --mark %s -j %s`, OnOutGoingNatMark, OvnMasquerade))},
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(fmt.Sprintf(`-m mark --mark %s -j RETURN`, OnOutGoingForwardMark))},
// default nat outgoing rules
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m set --match-set ovn40subnets-nat src -m set ! --match-set ovn40subnets dst -j ` + OvnMasquerade)},
// clear mark
{Table: NAT, Chain: OvnMasquerade, Rule: strings.Fields(`-j MARK --set-xmark 0x0/0xffffffff`)},
// do masquerade
{Table: NAT, Chain: OvnMasquerade, Rule: strings.Fields(`-j MASQUERADE`)},
// Input Accept
{Table: "filter", Chain: "INPUT", Rule: strings.Fields(`-m set --match-set ovn40subnets src -j ACCEPT`)},
{Table: "filter", Chain: "INPUT", Rule: strings.Fields(`-m set --match-set ovn40subnets dst -j ACCEPT`)},
{Table: "filter", Chain: "INPUT", Rule: strings.Fields(`-m set --match-set ovn40services src -j ACCEPT`)},
{Table: "filter", Chain: "INPUT", Rule: strings.Fields(`-m set --match-set ovn40services dst -j ACCEPT`)},
// Forward Accept
{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(`-m set --match-set ovn40subnets src -j ACCEPT`)},
{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(`-m set --match-set ovn40subnets dst -j ACCEPT`)},
{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(`-m set --match-set ovn40services src -j ACCEPT`)},
{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(`-m set --match-set ovn40services dst -j ACCEPT`)},
// Output unmark to bypass kernel nat checksum issue https://github.com/flannel-io/flannel/issues/1279
{Table: "filter", Chain: "OUTPUT", Rule: strings.Fields(`-p udp -m udp --dport 6081 -j MARK --set-xmark 0x0`)},
{Table: "filter", Chain: "OUTPUT", Rule: strings.Fields(`-p udp -m udp --dport 4789 -j MARK --set-xmark 0x0`)},
// Drop invalid rst
{Table: MANGLE, Chain: OvnPostrouting, Rule: strings.Fields(`-p tcp -m set --match-set ovn40subnets src -m tcp --tcp-flags RST RST -m state --state INVALID -j DROP`)},
}
v6Rules = []util.IPTableRule{
// mark packets from pod to service
{Table: NAT, Chain: OvnPrerouting, Rule: strings.Fields(`-i ovn0 -m set --match-set ovn60subnets src -m set --match-set ovn60services dst -j MARK --set-xmark 0x4000/0x4000`)},
// nat packets marked by kube-proxy or kube-ovn
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m mark --mark 0x4000/0x4000 -j ` + OvnMasquerade)},
// nat service traffic
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m set --match-set ovn60subnets src -m set --match-set ovn60subnets dst -j ` + OvnMasquerade)},
// do not nat node port service traffic with external traffic policy set to local
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m mark --mark 0x80000/0x80000 -m set --match-set ovn60subnets-distributed-gw dst -j RETURN`)},
// nat node port service traffic with external traffic policy set to local for subnets with centralized gateway
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m mark --mark 0x80000/0x80000 -j ` + OvnMasquerade)},
// do not nat reply packets in direct routing
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-p tcp -m tcp --tcp-flags SYN NONE -m conntrack --ctstate NEW -j RETURN`)},
// do not nat route traffic
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m set ! --match-set ovn60subnets src -m set ! --match-set ovn60other-node src -m set --match-set ovn60subnets-nat dst -j RETURN`)},
// nat outgoing policy rules
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(fmt.Sprintf(`-m set --match-set ovn60subnets-nat-policy src -m set ! --match-set ovn60subnets dst -j %s`, OvnNatOutGoingPolicy))},
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(fmt.Sprintf(`-m mark --mark %s -j %s`, OnOutGoingNatMark, OvnMasquerade))},
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(fmt.Sprintf(`-m mark --mark %s -j RETURN`, OnOutGoingForwardMark))},
{Table: NAT, Chain: OvnPostrouting, Rule: strings.Fields(`-m set --match-set ovn60subnets-nat src -m set ! --match-set ovn60subnets dst -j ` + OvnMasquerade)},
// clear mark
{Table: NAT, Chain: OvnMasquerade, Rule: strings.Fields(`-j MARK --set-xmark 0x0/0xffffffff`)},
// do masquerade
{Table: NAT, Chain: OvnMasquerade, Rule: strings.Fields(`-j MASQUERADE`)},
// Input Accept
{Table: "filter", Chain: "INPUT", Rule: strings.Fields(`-m set --match-set ovn60subnets src -j ACCEPT`)},
{Table: "filter", Chain: "INPUT", Rule: strings.Fields(`-m set --match-set ovn60subnets dst -j ACCEPT`)},
{Table: "filter", Chain: "INPUT", Rule: strings.Fields(`-m set --match-set ovn60services src -j ACCEPT`)},
{Table: "filter", Chain: "INPUT", Rule: strings.Fields(`-m set --match-set ovn60services dst -j ACCEPT`)},
// Forward Accept
{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(`-m set --match-set ovn60subnets src -j ACCEPT`)},
{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(`-m set --match-set ovn60subnets dst -j ACCEPT`)},
{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(`-m set --match-set ovn60services src -j ACCEPT`)},
{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(`-m set --match-set ovn60services dst -j ACCEPT`)},
// Output unmark to bypass kernel nat checksum issue https://github.com/flannel-io/flannel/issues/1279
{Table: "filter", Chain: "OUTPUT", Rule: strings.Fields(`-p udp -m udp --dport 6081 -j MARK --set-xmark 0x0`)},
{Table: "filter", Chain: "OUTPUT", Rule: strings.Fields(`-p udp -m udp --dport 4789 -j MARK --set-xmark 0x0`)},
// Drop invalid rst
{Table: MANGLE, Chain: OvnPostrouting, Rule: strings.Fields(`-p tcp -m set --match-set ovn60subnets src -m tcp --tcp-flags RST RST -m state --state INVALID -j DROP`)},
}
)
protocols := make([]string, 0, 2)
if c.protocol == kubeovnv1.ProtocolDual {
protocols = append(protocols, kubeovnv1.ProtocolIPv4, kubeovnv1.ProtocolIPv6)
} else {
protocols = append(protocols, c.protocol)
}
for _, protocol := range protocols {
ipt := c.iptables[protocol]
if ipt == nil {
continue
}
var kubeProxyIpsetProtocol, matchset, svcMatchset, nodeMatchSet string
var obsoleteRules, iptablesRules []util.IPTableRule
if protocol == kubeovnv1.ProtocolIPv4 {
iptablesRules = v4Rules
matchset, svcMatchset, nodeMatchSet = "ovn40subnets", "ovn40services", "ovn40"+OtherNodeSet
} else {
iptablesRules = v6Rules
kubeProxyIpsetProtocol, matchset, svcMatchset, nodeMatchSet = "6-", "ovn60subnets", "ovn60services", "ovn60"+OtherNodeSet
}
ipset := fmt.Sprintf("KUBE-%sCLUSTER-IP", kubeProxyIpsetProtocol)
ipsetExists, err := c.ipsetExists(ipset)
if err != nil {
klog.Errorf("failed to check existence of ipset %s: %v", ipset, err)
return err
}
if ipsetExists {
iptablesRules[0].Rule = strings.Fields(fmt.Sprintf(`-i ovn0 -m set --match-set %s src -m set --match-set %s dst,dst -j MARK --set-xmark 0x4000/0x4000`, matchset, ipset))
rejectRule := strings.Fields(fmt.Sprintf(`-p tcp -m mark ! --mark 0x4000/0x4000 -m set --match-set %s dst -m conntrack --ctstate NEW -j REJECT`, svcMatchset))
obsoleteRejectRule := strings.Fields(fmt.Sprintf(`-m mark ! --mark 0x4000/0x4000 -m set --match-set %s dst -m conntrack --ctstate NEW -j REJECT`, svcMatchset))
iptablesRules = append(iptablesRules,
util.IPTableRule{Table: "filter", Chain: "INPUT", Rule: rejectRule},
util.IPTableRule{Table: "filter", Chain: "OUTPUT", Rule: rejectRule},
)
obsoleteRejectRules := []util.IPTableRule{
{Table: "filter", Chain: "INPUT", Rule: obsoleteRejectRule},
{Table: "filter", Chain: "OUTPUT", Rule: obsoleteRejectRule},
}
for _, rule := range obsoleteRejectRules {
if err = deleteIptablesRule(ipt, rule); err != nil {
klog.Errorf("failed to delete obsolete iptables rule %v: %v", rule, err)
return err
}
}
}
if nodeIP := nodeIPs[protocol]; nodeIP != "" {
obsoleteRules = []util.IPTableRule{
{Table: NAT, Chain: Postrouting, Rule: strings.Fields(fmt.Sprintf(`! -s %s -m set --match-set %s dst -j MASQUERADE`, nodeIP, matchset))},
{Table: NAT, Chain: Postrouting, Rule: strings.Fields(fmt.Sprintf(`! -s %s -m mark --mark 0x4000/0x4000 -j MASQUERADE`, nodeIP))},
{Table: NAT, Chain: Postrouting, Rule: strings.Fields(fmt.Sprintf(`! -s %s -m set ! --match-set %s src -m set --match-set %s dst -j MASQUERADE`, nodeIP, matchset, matchset))},
}
rules := make([]util.IPTableRule, len(iptablesRules)+1)
copy(rules, iptablesRules[:1])
copy(rules[2:], iptablesRules[1:])
rules[1] = util.IPTableRule{
Table: NAT,
Chain: OvnPostrouting,
Rule: strings.Fields(fmt.Sprintf(`-m set --match-set %s src -m set --match-set %s dst -m mark --mark 0x4000/0x4000 -j SNAT --to-source %s`, svcMatchset, matchset, nodeIP)),
}
iptablesRules = rules
for _, p := range [...]string{"tcp", "udp"} {
ipset := fmt.Sprintf("KUBE-%sNODE-PORT-LOCAL-%s", kubeProxyIpsetProtocol, strings.ToUpper(p))
ipsetExists, err := c.ipsetExists(ipset)
if err != nil {
klog.Errorf("failed to check existence of ipset %s: %v", ipset, err)
return err
}
if !ipsetExists {
klog.V(5).Infof("ipset %s does not exist", ipset)
continue
}
rule := fmt.Sprintf("-p %s -m addrtype --dst-type LOCAL -m set --match-set %s dst -j MARK --set-xmark 0x80000/0x80000", p, ipset)
rule2 := fmt.Sprintf("-p %s -m set --match-set %s src -m set --match-set %s dst -j MARK --set-xmark 0x4000/0x4000", p, nodeMatchSet, ipset)
obsoleteRules = append(obsoleteRules, util.IPTableRule{Table: NAT, Chain: Prerouting, Rule: strings.Fields(rule)})
iptablesRules = append(iptablesRules,
util.IPTableRule{Table: NAT, Chain: OvnPrerouting, Rule: strings.Fields(rule)},
util.IPTableRule{Table: NAT, Chain: OvnPrerouting, Rule: strings.Fields(rule2)},
)
}
}
_, subnetCidrs, err := c.getDefaultVpcSubnetsCIDR(protocol)
if err != nil {
klog.Errorf("get subnets failed, %+v", err)
return err
}
subnetNames := set.New[string]()
for name, subnetCidr := range subnetCidrs {
subnetNames.Insert(name)
iptablesRules = append(iptablesRules,
util.IPTableRule{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(fmt.Sprintf(`-m comment --comment %s,%s -s %s`, util.OvnSubnetGatewayIptables, name, subnetCidr))},
util.IPTableRule{Table: "filter", Chain: "FORWARD", Rule: strings.Fields(fmt.Sprintf(`-m comment --comment %s,%s -d %s`, util.OvnSubnetGatewayIptables, name, subnetCidr))},
)
}
rules, err := ipt.List("filter", "FORWARD")
if err != nil {
klog.Errorf(`failed to list iptables rule table "filter" chain "FORWARD" with err %v `, err)
return err
}
pattern := fmt.Sprintf(`-m comment --comment "%s,`, util.OvnSubnetGatewayIptables)
for _, rule := range rules {
if !strings.Contains(rule, pattern) {
continue
}
fields := util.DoubleQuotedFields(rule)
// -A FORWARD -d 10.16.0.0/16 -m comment --comment "ovn-subnet-gateway,ovn-default"
if len(fields) != 8 || fields[6] != "--comment" {
continue
}
commentFields := strings.Split(fields[7], ",")
if len(commentFields) != 2 {
continue
}
if subnetNames.Has(commentFields[1]) {
continue
}
// use fields[2:] to skip prefix "-A FORWARD"
if err = deleteIptablesRule(ipt, util.IPTableRule{Table: "filter", Chain: "FORWARD", Rule: fields[2:]}); err != nil {
klog.Error(err)
return err
}
}
var natPreroutingRules, natPostroutingRules, ovnMasqueradeRules, manglePostroutingRules []util.IPTableRule
for _, rule := range iptablesRules {
if rule.Table == NAT {
if c.k8siptables[protocol].HasRandomFully() &&
(rule.Rule[len(rule.Rule)-1] == "MASQUERADE" || slices.Contains(rule.Rule, "SNAT")) {
rule.Rule = append(rule.Rule, "--random-fully")
}
switch rule.Chain {
case OvnPrerouting:
natPreroutingRules = append(natPreroutingRules, rule)
continue
case OvnPostrouting:
natPostroutingRules = append(natPostroutingRules, rule)
continue
case OvnMasquerade:
ovnMasqueradeRules = append(ovnMasqueradeRules, rule)
continue
}
} else if rule.Table == MANGLE {
if rule.Chain == OvnPostrouting {
manglePostroutingRules = append(manglePostroutingRules, rule)
continue
}
}
if err = c.createIptablesRule(ipt, rule); err != nil {
klog.Errorf(`failed to create iptables rule "%s": %v`, strings.Join(rule.Rule, " "), err)
return err
}
}
var randomFully string
if c.k8siptables[protocol].HasRandomFully() {
randomFully = "--random-fully"
}
// add iptables rule for nat gw with designative ip in centralized subnet
for cidr, ip := range centralGwNatIPs {
if util.CheckProtocol(cidr) != protocol {
continue
}
s := fmt.Sprintf("-s %s -m set ! --match-set %s dst -j SNAT --to-source %s %s", cidr, matchset, ip, randomFully)
rule := util.IPTableRule{
Table: NAT,
Chain: OvnPostrouting,
Rule: util.DoubleQuotedFields(s),
}
// insert the rule before the one for nat outgoing
n := len(natPostroutingRules)
natPostroutingRules = append(natPostroutingRules[:n-1], rule, natPostroutingRules[n-1])
}
if err = c.reconcileNatOutgoingPolicyIptablesChain(protocol); err != nil {
klog.Error(err)
return err
}
if err = c.reconcileTProxyIPTableRules(protocol); err != nil {
klog.Error(err)
return err
}
if err = c.updateIptablesChain(ipt, NAT, OvnPrerouting, Prerouting, natPreroutingRules); err != nil {
klog.Errorf("failed to update chain %s/%s: %v", NAT, OvnPrerouting, err)
return err
}
if err = c.updateIptablesChain(ipt, NAT, OvnMasquerade, "", ovnMasqueradeRules); err != nil {
klog.Errorf("failed to update chain %s/%s: %v", NAT, OvnMasquerade, err)
return err
}
if err = c.updateIptablesChain(ipt, NAT, OvnPostrouting, Postrouting, natPostroutingRules); err != nil {
klog.Errorf("failed to update chain %s/%s: %v", NAT, OvnPostrouting, err)
return err
}
if err = c.updateIptablesChain(ipt, MANGLE, OvnPostrouting, Postrouting, manglePostroutingRules); err != nil {
klog.Errorf("failed to update chain %s/%s: %v", MANGLE, OvnPostrouting, err)
return err
}
if err = c.cleanObsoleteIptablesRules(protocol, obsoleteRules); err != nil {
klog.Errorf("failed to clean legacy iptables rules: %v", err)
return err
}
}
return nil
}
func (c *Controller) reconcileTProxyIPTableRules(protocol string) error {
if !c.config.EnableTProxy {
return nil
}
ipt := c.iptables[protocol]
tproxyPreRoutingRules := make([]util.IPTableRule, 0)
tproxyOutputRules := make([]util.IPTableRule, 0)
pods, err := c.getTProxyConditionPod(true)
if err != nil {
klog.Error(err)
return err
}
for _, pod := range pods {
var podIP string
for _, ip := range pod.Status.PodIPs {
if util.CheckProtocol(ip.IP) == protocol {
podIP = ip.IP
break
}
}
if podIP == "" {
continue
}
ports := getProbePorts(pod)
if ports.Len() == 0 {
continue
}
for _, probePort := range ports.SortedList() {
hostIP := c.config.NodeIPv4
prefixLen := 32
if protocol == kubeovnv1.ProtocolIPv6 {
prefixLen = 128
hostIP = c.config.NodeIPv6
}
tproxyOutputRules = append(tproxyOutputRules, util.IPTableRule{Table: MANGLE, Chain: OvnOutput, Rule: strings.Fields(fmt.Sprintf(`-d %s/%d -p tcp -m tcp --dport %d -j MARK --set-xmark %s`, podIP, prefixLen, probePort, tProxyOutputMarkMask))})
tproxyPreRoutingRules = append(tproxyPreRoutingRules, util.IPTableRule{Table: MANGLE, Chain: OvnPrerouting, Rule: strings.Fields(fmt.Sprintf(`-d %s/%d -p tcp -m tcp --dport %d -j TPROXY --on-port %d --on-ip %s --tproxy-mark %s`, podIP, prefixLen, probePort, util.TProxyListenPort, hostIP, tProxyPreRoutingMarkMask))})
}
}
if err := c.updateIptablesChain(ipt, MANGLE, OvnPrerouting, Prerouting, tproxyPreRoutingRules); err != nil {
klog.Errorf("failed to update chain %s with rules %v: %v", OvnPrerouting, tproxyPreRoutingRules, err)
return err
}
if err := c.updateIptablesChain(ipt, MANGLE, OvnOutput, Output, tproxyOutputRules); err != nil {
klog.Errorf("failed to update chain %s with rules %v: %v", OvnOutput, tproxyOutputRules, err)
return err
}
return nil
}
func (c *Controller) cleanTProxyIPTableRules(protocol string) {
ipt := c.iptables[protocol]
if ipt == nil {
return
}
for _, chain := range [2]string{OvnPrerouting, OvnOutput} {
if err := ipt.ClearChain(MANGLE, chain); err != nil {
klog.Errorf("failed to clear iptables chain %v in table %v, %+v", chain, MANGLE, err)
return
}
}
}
func (c *Controller) reconcileNatOutgoingPolicyIptablesChain(protocol string) error {
ipt := c.iptables[protocol]
natPolicySubnetIptables, natPolicyRuleIptablesMap, gcNatPolicySubnetChains, err := c.generateNatOutgoingPolicyChainRules(protocol)
if err != nil {
klog.Errorf(`failed to get nat policy post routing rules with err %v `, err)
return err
}
for chainName, natPolicyRuleIptableRules := range natPolicyRuleIptablesMap {
if err = c.updateIptablesChain(ipt, NAT, chainName, "", natPolicyRuleIptableRules); err != nil {
klog.Errorf("failed to update chain %s with rules %v: %v", chainName, natPolicyRuleIptableRules, err)
return err
}
}
if err = c.updateIptablesChain(ipt, NAT, OvnNatOutGoingPolicy, "", natPolicySubnetIptables); err != nil {
klog.Errorf("failed to update chain %s: %v", OvnNatOutGoingPolicy, err)
return err
}
for _, gcNatPolicySubnetChain := range gcNatPolicySubnetChains {
if err = ipt.ClearAndDeleteChain(NAT, gcNatPolicySubnetChain); err != nil {
klog.Errorf("failed to delete iptables chain %q in table %s: %v", gcNatPolicySubnetChain, NAT, err)
return err
}
klog.Infof("deleted iptables chain %s in table %s", gcNatPolicySubnetChain, NAT)
}
return nil
}
func (c *Controller) generateNatOutgoingPolicyChainRules(protocol string) ([]util.IPTableRule, map[string][]util.IPTableRule, []string, error) {
natPolicySubnetIptables := make([]util.IPTableRule, 0)
natPolicyRuleIptablesMap := make(map[string][]util.IPTableRule)
natPolicySubnetUIDs := strset.New()
gcNatPolicySubnetChains := make([]string, 0)
subnetNames := make([]string, 0)
subnetMap := make(map[string]*kubeovnv1.Subnet)
subnets, err := c.getSubnetsNatOutGoingPolicy(protocol)
if err != nil {
klog.Errorf("failed to get subnets with NAT outgoing policy rule: %v", err)
return nil, nil, nil, err
}
for _, subnet := range subnets {
subnetNames = append(subnetNames, subnet.Name)
subnetMap[subnet.Name] = subnet
}
// To ensure the iptable rule order
sort.Strings(subnetNames)
getMatchProtocol := func(ips string) string {
ip := strings.Split(ips, ",")[0]
return util.CheckProtocol(ip)
}
for _, subnetName := range subnetNames {
subnet := subnetMap[subnetName]
var natPolicyRuleIptables []util.IPTableRule
natPolicySubnetUIDs.Add(util.GetTruncatedUID(string(subnet.GetUID())))