Skip to content

Commit

Permalink
Add support for short-circuiting in AntreaProxy
Browse files Browse the repository at this point in the history
Short-circuiting is used to ensure that the traffic from Pod/Node clients to
external addresses behaves the same way as the traffic from external clients to
external addresses.

External clients do not need to consider which Nodes have local Endpoints, as the
load balancer handles this for them. However, for Pod/Node clients, when the
externalTrafficPolicy of the Service is set to "Local", it will not work on Nodes
without an Endpoint. With this PR, even when the externalTrafficPolicy is set
to "Local", Pod/Node clients without local Endpoints can still work by selecting
Endpoints from the cluster.

Signed-off-by: Hongliang Liu <lhongliang@vmware.com>
  • Loading branch information
hongliangl committed May 16, 2023
1 parent 8dbb48c commit 29a8b10
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 195 deletions.
11 changes: 8 additions & 3 deletions pkg/agent/openflow/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ type Client interface {
// installs the flow that uses the group/bucket to do Service LB. If the affinityTimeout is not zero, it also
// installs the flow which has a learn action to maintain the LB decision. The group with the groupID must be
// installed before, otherwise the installation will fail.
// When externalAddress is set and groupID != clusterGroupID, it also installs the flow to implement short-circuiting
// for external Service IPs.
// externalAddress indicates that whether the Service is externally accessible, like NodePort, LoadBalancer and ExternalIP.
// nested, when setting to true, indicates the Service's Endpoints are ClusterIPs of other Services.
InstallServiceFlows(groupID binding.GroupIDType, svcIP net.IP, svcPort uint16, protocol binding.Protocol, affinityTimeout uint16, externalAddress, nested bool) error
InstallServiceFlows(groupID, clusterGroupID binding.GroupIDType, svcIP net.IP, svcPort uint16, protocol binding.Protocol, affinityTimeout uint16, externalAddress, nested bool) error
// UninstallServiceFlows removes flows installed by InstallServiceFlows.
UninstallServiceFlows(svcIP net.IP, svcPort uint16, protocol binding.Protocol) error

Expand Down Expand Up @@ -752,18 +754,21 @@ func (c *client) UninstallEndpointFlows(protocol binding.Protocol, endpoints []p
return c.deleteFlowsWithMultipleKeys(c.featureService.cachedFlows, flowCacheKeys)
}

func (c *client) InstallServiceFlows(groupID binding.GroupIDType, svcIP net.IP, svcPort uint16, protocol binding.Protocol, affinityTimeout uint16, externalAddress, nested bool) error {
func (c *client) InstallServiceFlows(groupID, clusterGroupID binding.GroupIDType, svcIP net.IP, svcPort uint16, protocol binding.Protocol, affinityTimeout uint16, externalAddress, nested bool) error {
c.replayMutex.RLock()
defer c.replayMutex.RUnlock()
var flows []binding.Flow
nodePortAddress := svcIP.Equal(config.VirtualNodePortDNATIPv4) || svcIP.Equal(config.VirtualNodePortDNATIPv6)
flows = append(flows, c.featureService.serviceLBFlow(groupID, svcIP, svcPort, protocol, affinityTimeout != 0, externalAddress, nodePortAddress, nested))
flows = append(flows, c.featureService.serviceLBFlow(groupID, svcIP, svcPort, protocol, affinityTimeout != 0, externalAddress, nodePortAddress, nested, false))
if affinityTimeout != 0 {
flows = append(flows, c.featureService.serviceLearnFlow(groupID, svcIP, svcPort, protocol, affinityTimeout, externalAddress, nodePortAddress))
}
if !externalAddress && !nested {
flows = append(flows, c.featureService.endpointRedirectFlowForServiceIP(svcIP, svcPort, protocol, groupID))
}
if externalAddress && groupID != clusterGroupID {
flows = append(flows, c.featureService.serviceLBFlow(clusterGroupID, svcIP, svcPort, protocol, affinityTimeout != 0, true, nodePortAddress, false, true))
}
cacheKey := generateServicePortFlowCacheKey(svcIP, svcPort, protocol)
return c.addFlows(c.featureService.cachedFlows, cacheKey, flows)
}
Expand Down
47 changes: 45 additions & 2 deletions pkg/agent/openflow/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,12 +1059,15 @@ func Test_client_InstallEndpointFlows(t *testing.T) {

func Test_client_InstallServiceFlows(t *testing.T) {
groupID := binding.GroupIDType(100)
clusterGroupID := binding.GroupIDType(101)
svcIPv4 := net.ParseIP("10.96.0.100")
svcIPv6 := net.ParseIP("fec0:10:96::100")
port := uint16(80)

testCases := []struct {
name string
groupID binding.GroupIDType
clusterGroupID binding.GroupIDType
protocol binding.Protocol
svcIP net.IP
affinityTimeout uint16
Expand All @@ -1074,6 +1077,7 @@ func Test_client_InstallServiceFlows(t *testing.T) {
}{
{
name: "Service ClusterIP",
groupID: groupID,
protocol: binding.ProtocolTCP,
svcIP: svcIPv4,
expectedFlows: []string{
Expand All @@ -1083,6 +1087,7 @@ func Test_client_InstallServiceFlows(t *testing.T) {
},
{
name: "Service ClusterIP, nested",
groupID: groupID,
protocol: binding.ProtocolTCP,
svcIP: svcIPv4,
expectedFlows: []string{
Expand All @@ -1092,6 +1097,7 @@ func Test_client_InstallServiceFlows(t *testing.T) {
},
{
name: "Service ClusterIP,SessionAffinity",
groupID: groupID,
protocol: binding.ProtocolTCP,
svcIP: svcIPv4,
affinityTimeout: uint16(100),
Expand All @@ -1103,6 +1109,7 @@ func Test_client_InstallServiceFlows(t *testing.T) {
},
{
name: "Service ClusterIP,IPv6,SessionAffinity",
groupID: groupID,
protocol: binding.ProtocolTCPv6,
svcIP: svcIPv6,
affinityTimeout: uint16(100),
Expand All @@ -1113,6 +1120,8 @@ func Test_client_InstallServiceFlows(t *testing.T) {
},
{
name: "Service NodePort,SessionAffinity",
groupID: groupID,
clusterGroupID: groupID,
protocol: binding.ProtocolUDP,
svcIP: config.VirtualNodePortDNATIPv4,
affinityTimeout: uint16(100),
Expand All @@ -1124,6 +1133,8 @@ func Test_client_InstallServiceFlows(t *testing.T) {
},
{
name: "Service NodePort,IPv6,SessionAffinity",
groupID: groupID,
clusterGroupID: groupID,
protocol: binding.ProtocolUDPv6,
svcIP: config.VirtualNodePortDNATIPv6,
affinityTimeout: uint16(100),
Expand All @@ -1133,8 +1144,24 @@ func Test_client_InstallServiceFlows(t *testing.T) {
"cookie=0x1030000000064, table=ServiceLB, priority=190,udp6,reg4=0xb0000/0xf0000,tp_dst=80 actions=learn(table=SessionAffinity,hard_timeout=100,priority=200,delete_learned,cookie=0x1030000000064,eth_type=0x86dd,nw_proto=0x11,OXM_OF_UDP_DST[],NXM_NX_IPV6_DST[],NXM_NX_IPV6_SRC[],load:NXM_NX_XXREG3[]->NXM_NX_XXREG3[],load:NXM_NX_REG4[0..15]->NXM_NX_REG4[0..15],load:0x2->NXM_NX_REG4[16..18],load:0x1->NXM_NX_REG0[9],load:0x1->NXM_NX_REG4[21]),set_field:0x20000/0x70000->reg4,goto_table:EndpointDNAT",
},
},
{
name: "Service NodePort,SessionAffinity,Short-circuiting",
groupID: groupID,
clusterGroupID: clusterGroupID,
protocol: binding.ProtocolUDP,
svcIP: config.VirtualNodePortDNATIPv4,
affinityTimeout: uint16(100),
toExternalAddress: true,
expectedFlows: []string{
"cookie=0x1030000000000, table=ServiceLB, priority=210,udp,reg4=0x90000/0xf0000,nw_src=10.10.0.0/24,tp_dst=80 actions=set_field:0x200/0x200->reg0,set_field:0x30000/0x70000->reg4,set_field:0x200000/0x200000->reg4,set_field:0x65->reg7,group:101",
"cookie=0x1030000000000, table=ServiceLB, priority=200,udp,reg4=0x90000/0xf0000,tp_dst=80 actions=set_field:0x200/0x200->reg0,set_field:0x30000/0x70000->reg4,set_field:0x200000/0x200000->reg4,set_field:0x64->reg7,group:100",
"cookie=0x1030000000064, table=ServiceLB, priority=190,udp,reg4=0xb0000/0xf0000,tp_dst=80 actions=learn(table=SessionAffinity,hard_timeout=100,priority=200,delete_learned,cookie=0x1030000000064,eth_type=0x800,nw_proto=0x11,OXM_OF_UDP_DST[],NXM_OF_IP_DST[],NXM_OF_IP_SRC[],load:NXM_NX_REG3[]->NXM_NX_REG3[],load:NXM_NX_REG4[0..15]->NXM_NX_REG4[0..15],load:0x2->NXM_NX_REG4[16..18],load:0x1->NXM_NX_REG0[9],load:0x1->NXM_NX_REG4[21]),set_field:0x20000/0x70000->reg4,goto_table:EndpointDNAT",
},
},
{
name: "Service LoadBalancer,SessionAffinity",
groupID: groupID,
clusterGroupID: groupID,
protocol: binding.ProtocolSCTP,
svcIP: svcIPv4,
affinityTimeout: uint16(100),
Expand All @@ -1146,6 +1173,8 @@ func Test_client_InstallServiceFlows(t *testing.T) {
},
{
name: "Service LoadBalancer,IPv6,SessionAffinity",
groupID: groupID,
clusterGroupID: groupID,
protocol: binding.ProtocolSCTPv6,
svcIP: svcIPv6,
affinityTimeout: uint16(100),
Expand All @@ -1155,6 +1184,20 @@ func Test_client_InstallServiceFlows(t *testing.T) {
"cookie=0x1030000000064, table=ServiceLB, priority=190,sctp6,reg4=0x30000/0x70000,ipv6_dst=fec0:10:96::100,tp_dst=80 actions=learn(table=SessionAffinity,hard_timeout=100,priority=200,delete_learned,cookie=0x1030000000064,eth_type=0x86dd,nw_proto=0x84,OXM_OF_SCTP_DST[],NXM_NX_IPV6_DST[],NXM_NX_IPV6_SRC[],load:NXM_NX_XXREG3[]->NXM_NX_XXREG3[],load:NXM_NX_REG4[0..15]->NXM_NX_REG4[0..15],load:0x2->NXM_NX_REG4[16..18],load:0x1->NXM_NX_REG0[9],load:0x1->NXM_NX_REG4[21]),set_field:0x20000/0x70000->reg4,goto_table:EndpointDNAT",
},
},
{
name: "Service LoadBalancer,SessionAffinity,Short-circuiting",
groupID: groupID,
clusterGroupID: clusterGroupID,
protocol: binding.ProtocolSCTP,
svcIP: svcIPv4,
affinityTimeout: uint16(100),
toExternalAddress: true,
expectedFlows: []string{
"cookie=0x1030000000000, table=ServiceLB, priority=210,sctp,reg4=0x10000/0x70000,nw_src=10.10.0.0/24,nw_dst=10.96.0.100,tp_dst=80 actions=set_field:0x200/0x200->reg0,set_field:0x30000/0x70000->reg4,set_field:0x200000/0x200000->reg4,set_field:0x65->reg7,group:101",
"cookie=0x1030000000000, table=ServiceLB, priority=200,sctp,reg4=0x10000/0x70000,nw_dst=10.96.0.100,tp_dst=80 actions=set_field:0x200/0x200->reg0,set_field:0x30000/0x70000->reg4,set_field:0x200000/0x200000->reg4,set_field:0x64->reg7,group:100",
"cookie=0x1030000000064, table=ServiceLB, priority=190,sctp,reg4=0x30000/0x70000,nw_dst=10.96.0.100,tp_dst=80 actions=learn(table=SessionAffinity,hard_timeout=100,priority=200,delete_learned,cookie=0x1030000000064,eth_type=0x800,nw_proto=0x84,OXM_OF_SCTP_DST[],NXM_OF_IP_DST[],NXM_OF_IP_SRC[],load:NXM_NX_REG3[]->NXM_NX_REG3[],load:NXM_NX_REG4[0..15]->NXM_NX_REG4[0..15],load:0x2->NXM_NX_REG4[16..18],load:0x1->NXM_NX_REG0[9],load:0x1->NXM_NX_REG4[21]),set_field:0x20000/0x70000->reg4,goto_table:EndpointDNAT",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -1169,7 +1212,7 @@ func Test_client_InstallServiceFlows(t *testing.T) {

cacheKey := generateServicePortFlowCacheKey(tc.svcIP, port, tc.protocol)

assert.NoError(t, fc.InstallServiceFlows(groupID, tc.svcIP, port, tc.protocol, tc.affinityTimeout, tc.toExternalAddress, tc.nested))
assert.NoError(t, fc.InstallServiceFlows(tc.groupID, tc.clusterGroupID, tc.svcIP, port, tc.protocol, tc.affinityTimeout, tc.toExternalAddress, tc.nested))
fCacheI, ok := fc.featureService.cachedFlows.Load(cacheKey)
require.True(t, ok)
assert.ElementsMatch(t, tc.expectedFlows, getFlowStrings(fCacheI))
Expand Down Expand Up @@ -1198,7 +1241,7 @@ func Test_client_GetServiceFlowKeys(t *testing.T) {
proxy.NewBaseEndpointInfo("10.10.0.12", "", "", 80, true, true, false, false, nil),
}

assert.NoError(t, fc.InstallServiceFlows(groupID, svcIP, svcPort, bindingProtocol, 100, true, false))
assert.NoError(t, fc.InstallServiceFlows(groupID, groupID, svcIP, svcPort, bindingProtocol, 100, true, false))
assert.NoError(t, fc.InstallEndpointFlows(bindingProtocol, endpoints))
flowKeys := fc.GetServiceFlowKeys(svcIP, svcPort, bindingProtocol, endpoints)
expectedFlowKeys := []string{
Expand Down
21 changes: 16 additions & 5 deletions pkg/agent/openflow/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2377,11 +2377,22 @@ func (f *featureService) serviceLBFlow(groupID binding.GroupIDType,
withSessionAffinity bool,
externalAddress bool,
nodePortAddress bool,
nested bool) binding.Flow {
flowBuilder := ServiceLBTable.ofTable.BuildFlow(priorityNormal).
Cookie(f.cookieAllocator.Request(f.category).Raw()).
MatchProtocol(protocol).
MatchDstPort(svcPort, nil)
nested bool,
isShortCircuiting bool) binding.Flow {
var flowBuilder binding.FlowBuilder
if isShortCircuiting {
// For short-circuiting flow, an extra match condition matching packet from local Pod CIDR is added.
flowBuilder = ServiceLBTable.ofTable.BuildFlow(priorityHigh).
Cookie(f.cookieAllocator.Request(f.category).Raw()).
MatchProtocol(protocol).
MatchDstPort(svcPort, nil).
MatchSrcIPNet(f.localCIDRs[getIPProtocol(svcIP)])
} else {
flowBuilder = ServiceLBTable.ofTable.BuildFlow(priorityNormal).
Cookie(f.cookieAllocator.Request(f.category).Raw()).
MatchProtocol(protocol).
MatchDstPort(svcPort, nil)
}

// EpToSelectRegMark is required to match the packets that haven't undergone Endpoint selection yet.
regMarksToMatch := []*binding.RegMark{EpToSelectRegMark}
Expand Down
9 changes: 9 additions & 0 deletions pkg/agent/openflow/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type featureService struct {
gatewayMAC net.HardwareAddr
nodePortAddresses map[binding.Protocol][]net.IP
serviceCIDRs map[binding.Protocol]net.IPNet
localCIDRs map[binding.Protocol]net.IPNet
networkConfig *config.NetworkConfig
gatewayPort uint32

Expand Down Expand Up @@ -75,6 +76,7 @@ func newFeatureService(
snatCtZones := make(map[binding.Protocol]int)
nodePortAddresses := make(map[binding.Protocol][]net.IP)
serviceCIDRs := make(map[binding.Protocol]net.IPNet)
localCIDRs := make(map[binding.Protocol]net.IPNet)
for _, ipProtocol := range ipProtocols {
if ipProtocol == binding.ProtocolIP {
gatewayIPs[ipProtocol] = nodeConfig.GatewayConfig.IPv4
Expand All @@ -86,6 +88,9 @@ func newFeatureService(
if serviceConfig.ServiceCIDR != nil {
serviceCIDRs[ipProtocol] = *serviceConfig.ServiceCIDR
}
if nodeConfig.PodIPv4CIDR != nil {
localCIDRs[ipProtocol] = *nodeConfig.PodIPv4CIDR
}
} else if ipProtocol == binding.ProtocolIPv6 {
gatewayIPs[ipProtocol] = nodeConfig.GatewayConfig.IPv6
virtualIPs[ipProtocol] = config.VirtualServiceIPv6
Expand All @@ -96,6 +101,9 @@ func newFeatureService(
if serviceConfig.ServiceCIDRv6 != nil {
serviceCIDRs[ipProtocol] = *serviceConfig.ServiceCIDRv6
}
if nodeConfig.PodIPv6CIDR != nil {
localCIDRs[ipProtocol] = *nodeConfig.PodIPv6CIDR
}
}
}

Expand All @@ -112,6 +120,7 @@ func newFeatureService(
snatCtZones: snatCtZones,
nodePortAddresses: nodePortAddresses,
serviceCIDRs: serviceCIDRs,
localCIDRs: localCIDRs,
gatewayMAC: nodeConfig.GatewayConfig.MAC,
gatewayPort: nodeConfig.GatewayConfig.OFPort,
networkConfig: networkConfig,
Expand Down
8 changes: 4 additions & 4 deletions pkg/agent/openflow/testing/mock_openflow.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 29a8b10

Please sign in to comment.