-
Notifications
You must be signed in to change notification settings - Fork 355
/
externalnode_connectivity.go
229 lines (207 loc) · 7.61 KB
/
externalnode_connectivity.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
// Copyright 2022 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openflow
import (
"net"
"antrea.io/libOpenflow/openflow15"
"antrea.io/antrea/pkg/agent/openflow/cookie"
binding "antrea.io/antrea/pkg/ovs/openflow"
)
const (
policyBypassFlowsKey = "policyBypassFlows"
)
type featureExternalNodeConnectivity struct {
cookieAllocator cookie.Allocator
ipProtocols []binding.Protocol
ctZones map[binding.Protocol]int
category cookie.Category
uplinkFlowCache *flowCategoryCache
}
func (f *featureExternalNodeConnectivity) getFeatureName() string {
return "ExternalNodeConnectivity"
}
func newFeatureExternalNodeConnectivity(
cookieAllocator cookie.Allocator,
ipProtocols []binding.Protocol) *featureExternalNodeConnectivity {
ctZones := make(map[binding.Protocol]int)
for _, ipProtocol := range ipProtocols {
if ipProtocol == binding.ProtocolIP {
ctZones[ipProtocol] = CtZone
} else if ipProtocol == binding.ProtocolIPv6 {
ctZones[ipProtocol] = CtZoneV6
}
}
return &featureExternalNodeConnectivity{
cookieAllocator: cookieAllocator,
ipProtocols: ipProtocols,
uplinkFlowCache: newFlowCategoryCache(),
ctZones: ctZones,
category: cookie.ExternalNodeConnectivity,
}
}
func (f *featureExternalNodeConnectivity) vmUplinkFlows(hostOFPort, uplinkOFPort uint32) []binding.Flow {
cookieID := f.cookieAllocator.Request(f.category).Raw()
return []binding.Flow{
// Set the output port number with the uplink port if the IP packet enters OVS from the
// paired host internal port, and then enforce the packet to go through the IP pipeline.
L2ForwardingCalcTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchProtocol(binding.ProtocolIP).
MatchInPort(hostOFPort).
Action().LoadRegMark(OutputToOFPortRegMark).
Action().LoadToRegField(TargetOFPortField, uplinkOFPort).
Action().NextTable().
Done(),
// Set the output port number with the paired host internal port if the IP packet enters the OVS from
// the uplink port, and then enforce the packet to go through the IP pipeline.
L2ForwardingCalcTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchInPort(uplinkOFPort).
MatchProtocol(binding.ProtocolIP).
Action().LoadRegMark(OutputToOFPortRegMark).
Action().LoadToRegField(TargetOFPortField, hostOFPort).
Action().NextTable().
Done(),
// Output the packet to the uplink port if it is not using IP protocol, and enters OVS from the
// paired host internal port.
NonIPTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchInPort(hostOFPort).
Action().Output(uplinkOFPort).
Done(),
// Output the packet to the uplink port if it is not using IP protocol, and enters OVS from the
// paired host internal port.
NonIPTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchInPort(uplinkOFPort).
Action().Output(hostOFPort).
Done(),
}
}
func (f *featureExternalNodeConnectivity) initFlows() []*openflow15.FlowMod {
cookieID := f.cookieAllocator.Request(f.category).Raw()
flows := []binding.Flow{
OutputTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchRegMark(OutputToOFPortRegMark).
Action().OutputToRegField(TargetOFPortField).
Done(),
}
for _, ipProtocol := range f.ipProtocols {
ctZone := f.ctZones[ipProtocol]
flows = append(flows,
// This generates the flow to maintain tracked connections in CT zone.
ConntrackTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchProtocol(ipProtocol).
Action().CT(false, ConntrackTable.ofTable.GetNext(), ctZone, nil).
CTDone().
Done(),
ConntrackStateTable.ofTable.BuildFlow(priorityHigh).
Cookie(cookieID).
MatchProtocol(ipProtocol).
MatchCTStateInv(true).
MatchCTStateTrk(true).
Action().Drop().
Done(),
ConntrackCommitTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchProtocol(ipProtocol).
MatchCTStateNew(true).
MatchCTStateTrk(true).
Action().CT(true, ConntrackCommitTable.GetNext(), ctZone, nil).CTDone().
Done(),
)
}
return GetFlowModMessages(flows, binding.AddMessage)
}
func (f *featureExternalNodeConnectivity) replayFlows() []*openflow15.FlowMod {
var flows []*openflow15.FlowMod
rangeFunc := func(key, value interface{}) bool {
cachedFlows := value.([]*openflow15.FlowMod)
for _, flow := range cachedFlows {
flows = append(flows, flow)
}
return true
}
f.uplinkFlowCache.Range(rangeFunc)
return flows
}
func (f *featureExternalNodeConnectivity) initGroups() []binding.OFEntry {
return nil
}
func (f *featureExternalNodeConnectivity) replayGroups() []binding.OFEntry {
return nil
}
func (f *featureExternalNodeConnectivity) policyBypassFlow(protocol binding.Protocol, ipNet *net.IPNet, port uint16, isIngress bool) binding.Flow {
cookieID := f.cookieAllocator.Request(f.category).Raw()
var flowBuilder binding.FlowBuilder
var nextTable *Table
if isIngress {
flowBuilder = IngressSecurityClassifierTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchProtocol(protocol).
MatchCTStateNew(true).
MatchCTStateTrk(true).
MatchSrcIPNet(*ipNet)
nextTable = IngressMetricTable
} else {
flowBuilder = EgressSecurityClassifierTable.ofTable.BuildFlow(priorityNormal).
Cookie(cookieID).
MatchProtocol(protocol).
MatchCTStateNew(true).
MatchCTStateTrk(true).
MatchDstIPNet(*ipNet)
nextTable = EgressMetricTable
}
return flowBuilder.MatchDstPort(port, nil).
Action().GotoTable(nextTable.ofTable.GetID()).
Done()
}
func (f *featureExternalNodeConnectivity) addPolicyBypassFlows(flow binding.Flow) error {
var allFlows []binding.Flow
obj, ok := f.uplinkFlowCache.Load(policyBypassFlowsKey)
if !ok {
allFlows = []binding.Flow{flow}
} else {
existingFlows := obj.([]binding.Flow)
allFlows = append(existingFlows, flow)
}
f.uplinkFlowCache.Store(policyBypassFlowsKey, allFlows)
return nil
}
func (c *client) InstallVMUplinkFlows(hostIFName string, hostPort int32, uplinkPort int32) error {
flows := c.featureExternalNodeConnectivity.vmUplinkFlows(uint32(hostPort), uint32(uplinkPort))
return c.addFlows(c.featureExternalNodeConnectivity.uplinkFlowCache, hostIFName, flows)
}
func (c *client) UninstallVMUplinkFlows(hostIFName string) error {
return c.deleteFlows(c.featureExternalNodeConnectivity.uplinkFlowCache, hostIFName)
}
func (c *client) InstallPolicyBypassFlows(protocol binding.Protocol, ipNet *net.IPNet, port uint16, isIngress bool) error {
flow := c.featureExternalNodeConnectivity.policyBypassFlow(protocol, ipNet, port, isIngress)
flowMessages := GetFlowModMessages([]binding.Flow{flow}, binding.AddMessage)
if err := c.ofEntryOperations.AddAll(flowMessages); err != nil {
return err
}
return c.featureExternalNodeConnectivity.addPolicyBypassFlows(flow)
}
// nonIPPipelineClassifyFlow generates a flow in PipelineClassifierTable to resubmit packets not using IP protocols to
// pipelineNonIP.
func nonIPPipelineClassifyFlow(cookieID uint64, pipeline binding.Pipeline) binding.Flow {
targetTable := pipeline.GetFirstTable()
return PipelineRootClassifierTable.ofTable.BuildFlow(priorityLow).
Cookie(cookieID).
Action().GotoTable(targetTable.GetID()).
Done()
}