@@ -43,6 +43,7 @@ const (
43
43
Postrouting = "POSTROUTING"
44
44
OvnPrerouting = "OVN-PREROUTING"
45
45
OvnPostrouting = "OVN-POSTROUTING"
46
+ OvnMasquerade = "OVN-MASQUERADE"
46
47
OvnNatOutGoingPolicy = "OVN-NAT-POLICY"
47
48
OvnNatOutGoingPolicySubnet = "OVN-NAT-PSUBNET-"
48
49
)
@@ -513,24 +514,27 @@ func (c *Controller) setIptables() error {
513
514
// mark packets from pod to service
514
515
{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` )},
515
516
// nat packets marked by kube-proxy or kube-ovn
516
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x4000/0x4000 -j MASQUERADE` )},
517
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x4000/0x4000 -j ` + OvnMasquerade )},
517
518
// nat service traffic
518
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m set --match-set ovn40subnets src -m set --match-set ovn40subnets dst -j MASQUERADE` )},
519
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m set --match-set ovn40subnets src -m set --match-set ovn40subnets dst -j ` + OvnMasquerade )},
519
520
// do not nat node port service traffic with external traffic policy set to local
520
521
{Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x80000/0x80000 -m set --match-set ovn40subnets-distributed-gw dst -j RETURN` )},
521
522
// nat node port service traffic with external traffic policy set to local for subnets with centralized gateway
522
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x80000/0x80000 -j MASQUERADE` )},
523
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x80000/0x80000 -j ` + OvnMasquerade )},
523
524
// do not nat reply packets in direct routing
524
525
{Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-p tcp -m tcp --tcp-flags SYN NONE -m conntrack --ctstate NEW -j RETURN` )},
525
526
// do not nat route traffic
526
527
{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` )},
527
- // nat outgoing
528
528
// nat outgoing policy rules
529
529
{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 ))},
530
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (fmt .Sprintf (`-m mark --mark %s -j MASQUERADE ` , OnOutGoingNatMark ))},
530
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (fmt .Sprintf (`-m mark --mark %s -j %s ` , OnOutGoingNatMark , OvnMasquerade ))},
531
531
{Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (fmt .Sprintf (`-m mark --mark %s -j RETURN` , OnOutGoingForwardMark ))},
532
532
// default nat outgoing rules
533
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m set --match-set ovn40subnets-nat src -m set ! --match-set ovn40subnets dst -j MASQUERADE` )},
533
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m set --match-set ovn40subnets-nat src -m set ! --match-set ovn40subnets dst -j ` + OvnMasquerade )},
534
+ // clear mark
535
+ {Table : NAT , Chain : OvnMasquerade , Rule : strings .Fields (`-j MARK --set-xmark 0x0/0xffffffff` )},
536
+ // do masquerade
537
+ {Table : NAT , Chain : OvnMasquerade , Rule : strings .Fields (`-j MASQUERADE` )},
534
538
// Input Accept
535
539
{Table : "filter" , Chain : "INPUT" , Rule : strings .Fields (`-m set --match-set ovn40subnets src -j ACCEPT` )},
536
540
{Table : "filter" , Chain : "INPUT" , Rule : strings .Fields (`-m set --match-set ovn40subnets dst -j ACCEPT` )},
@@ -548,23 +552,26 @@ func (c *Controller) setIptables() error {
548
552
// mark packets from pod to service
549
553
{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` )},
550
554
// nat packets marked by kube-proxy or kube-ovn
551
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x4000/0x4000 -j MASQUERADE` )},
555
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x4000/0x4000 -j ` + OvnMasquerade )},
552
556
// nat service traffic
553
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m set --match-set ovn60subnets src -m set --match-set ovn60subnets dst -j MASQUERADE` )},
557
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m set --match-set ovn60subnets src -m set --match-set ovn60subnets dst -j ` + OvnMasquerade )},
554
558
// do not nat node port service traffic with external traffic policy set to local
555
559
{Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x80000/0x80000 -m set --match-set ovn60subnets-distributed-gw dst -j RETURN` )},
556
560
// nat node port service traffic with external traffic policy set to local for subnets with centralized gateway
557
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x80000/0x80000 -j MASQUERADE` )},
561
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m mark --mark 0x80000/0x80000 -j ` + OvnMasquerade )},
558
562
// do not nat reply packets in direct routing
559
563
{Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-p tcp -m tcp --tcp-flags SYN NONE -m conntrack --ctstate NEW -j RETURN` )},
560
564
// do not nat route traffic
561
565
{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` )},
562
- // nat outgoing
563
566
// nat outgoing policy rules
564
567
{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 ))},
565
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (fmt .Sprintf (`-m mark --mark %s -j MASQUERADE ` , OnOutGoingNatMark ))},
568
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (fmt .Sprintf (`-m mark --mark %s -j %s ` , OnOutGoingNatMark , OvnMasquerade ))},
566
569
{Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (fmt .Sprintf (`-m mark --mark %s -j RETURN` , OnOutGoingForwardMark ))},
567
- {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m set --match-set ovn60subnets-nat src -m set ! --match-set ovn60subnets dst -j MASQUERADE` )},
570
+ {Table : NAT , Chain : OvnPostrouting , Rule : strings .Fields (`-m set --match-set ovn60subnets-nat src -m set ! --match-set ovn60subnets dst -j ` + OvnMasquerade )},
571
+ // clear mark
572
+ {Table : NAT , Chain : OvnMasquerade , Rule : strings .Fields (`-j MARK --set-xmark 0x0/0xffffffff` )},
573
+ // do masquerade
574
+ {Table : NAT , Chain : OvnMasquerade , Rule : strings .Fields (`-j MASQUERADE` )},
568
575
// Input Accept
569
576
{Table : "filter" , Chain : "INPUT" , Rule : strings .Fields (`-m set --match-set ovn60subnets src -j ACCEPT` )},
570
577
{Table : "filter" , Chain : "INPUT" , Rule : strings .Fields (`-m set --match-set ovn60subnets dst -j ACCEPT` )},
@@ -682,23 +689,23 @@ func (c *Controller) setIptables() error {
682
689
}
683
690
}
684
691
685
- var natPreroutingRules , natPostroutingRules []util.IPTableRule
692
+ var natPreroutingRules , natPostroutingRules , ovnMasqueradeRules []util.IPTableRule
686
693
for _ , rule := range iptablesRules {
687
694
if rule .Table == NAT {
695
+ if c .k8siptables [protocol ].HasRandomFully () && rule .Rule [len (rule .Rule )- 1 ] == "MASQUERADE" {
696
+ rule .Rule = append (rule .Rule , "--random-fully" )
697
+ }
698
+
688
699
switch rule .Chain {
689
700
case OvnPrerouting :
690
701
natPreroutingRules = append (natPreroutingRules , rule )
691
702
continue
692
703
case OvnPostrouting :
693
- if util .ContainsString (rule .Rule , "MASQUERADE" ) && c .k8siptables [protocol ].HasRandomFully () {
694
- // https://github.com/kubeovn/kube-ovn/issues/2641
695
- // Work around Linux kernel bug that sometimes causes multiple flows to
696
- // get mapped to the same IP:PORT and consequently some suffer packet
697
- // drops.
698
- rule .Rule = append (rule .Rule , "--random-fully" )
699
- }
700
704
natPostroutingRules = append (natPostroutingRules , rule )
701
705
continue
706
+ case OvnMasquerade :
707
+ ovnMasqueradeRules = append (ovnMasqueradeRules , rule )
708
+ continue
702
709
}
703
710
}
704
711
@@ -733,6 +740,10 @@ func (c *Controller) setIptables() error {
733
740
klog .Errorf ("failed to update chain %s/%s: %v" , NAT , OvnPrerouting )
734
741
return err
735
742
}
743
+ if err = c .updateIptablesChain (ipt , NAT , OvnMasquerade , "" , ovnMasqueradeRules ); err != nil {
744
+ klog .Errorf ("failed to update chain %s/%s: %v" , NAT , OvnMasquerade )
745
+ return err
746
+ }
736
747
if err = c .updateIptablesChain (ipt , NAT , OvnPostrouting , Postrouting , natPostroutingRules ); err != nil {
737
748
klog .Errorf ("failed to update chain %s/%s: %v" , NAT , OvnPostrouting )
738
749
return err
0 commit comments