-
Notifications
You must be signed in to change notification settings - Fork 359
/
conntrack_connections.go
321 lines (296 loc) · 12.1 KB
/
conntrack_connections.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
// Copyright 2021 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 connections
import (
"encoding/binary"
"fmt"
"time"
"github.com/vmware/go-ipfix/pkg/registry"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"antrea.io/antrea/pkg/agent/flowexporter"
"antrea.io/antrea/pkg/agent/flowexporter/priorityqueue"
"antrea.io/antrea/pkg/agent/interfacestore"
"antrea.io/antrea/pkg/agent/metrics"
"antrea.io/antrea/pkg/agent/openflow"
"antrea.io/antrea/pkg/agent/proxy"
"antrea.io/antrea/pkg/querier"
)
var serviceProtocolMap = map[uint8]corev1.Protocol{
6: corev1.ProtocolTCP,
17: corev1.ProtocolUDP,
132: corev1.ProtocolSCTP,
}
type ConntrackConnectionStore struct {
connDumper ConnTrackDumper
v4Enabled bool
v6Enabled bool
networkPolicyQuerier querier.AgentNetworkPolicyInfoQuerier
pollInterval time.Duration
connectUplinkToBridge bool
connectionStore
}
func NewConntrackConnectionStore(
connTrackDumper ConnTrackDumper,
v4Enabled bool,
v6Enabled bool,
npQuerier querier.AgentNetworkPolicyInfoQuerier,
ifaceStore interfacestore.InterfaceStore,
proxier proxy.Proxier,
o *flowexporter.FlowExporterOptions,
) *ConntrackConnectionStore {
return &ConntrackConnectionStore{
connDumper: connTrackDumper,
v4Enabled: v4Enabled,
v6Enabled: v6Enabled,
networkPolicyQuerier: npQuerier,
pollInterval: o.PollInterval,
connectionStore: NewConnectionStore(ifaceStore, proxier, o),
connectUplinkToBridge: o.ConnectUplinkToBridge,
}
}
// Run enables the periodical polling of conntrack connections at a given flowPollInterval.
func (cs *ConntrackConnectionStore) Run(stopCh <-chan struct{}) {
klog.Infof("Starting conntrack polling")
pollTicker := time.NewTicker(cs.pollInterval)
defer pollTicker.Stop()
for {
select {
case <-stopCh:
break
case <-pollTicker.C:
_, err := cs.Poll()
if err != nil {
// Not failing here as errors can be transient and could be resolved in future poll cycles.
// TODO: Come up with a backoff/retry mechanism by increasing poll interval and adding retry timeout
klog.Errorf("Error during conntrack poll cycle: %v", err)
}
}
}
}
// Poll calls into conntrackDumper interface to dump conntrack flows. It returns the number of connections for each
// address family, as a slice. In dual-stack clusters, the slice will contain 2 values (number of IPv4 connections first,
// then number of IPv6 connections).
// TODO: As optimization, only poll invalid/closed connections during every poll, and poll the established connections right before the export.
func (cs *ConntrackConnectionStore) Poll() ([]int, error) {
klog.V(2).Infof("Polling conntrack")
var zones []uint16
var connsLens []int
if cs.v4Enabled {
if cs.connectUplinkToBridge {
zones = append(zones, uint16(openflow.IPCtZoneTypeRegMark.GetValue()<<12))
} else {
zones = append(zones, openflow.CtZone)
}
}
if cs.v6Enabled {
if cs.connectUplinkToBridge {
zones = append(zones, uint16(openflow.IPv6CtZoneTypeRegMark.GetValue()<<12))
} else {
zones = append(zones, openflow.CtZoneV6)
}
}
var totalConns int
var filteredConnsList []*flowexporter.Connection
for _, zone := range zones {
filteredConnsListPerZone, totalConnsPerZone, err := cs.connDumper.DumpFlows(zone)
if err != nil {
return []int{}, err
}
totalConns += totalConnsPerZone
filteredConnsList = append(filteredConnsList, filteredConnsListPerZone...)
connsLens = append(connsLens, len(filteredConnsList))
}
// Reset IsPresent flag for all connections in connection map before updating
// the dumped flows information in connection map. If the connection does not
// exist in conntrack table and has been exported, then we will delete it from
// connection map. In addition, if the connection was not exported for a specific
// time period, then we consider it to be stale and delete it.
deleteIfStaleOrResetConn := func(key flowexporter.ConnectionKey, conn *flowexporter.Connection) error {
if !conn.IsPresent {
// Delete the connection if it is ready to delete or it was not exported
// in the time period as specified by the stale connection timeout.
if conn.ReadyToDelete || time.Since(conn.LastExportTime) >= cs.staleConnectionTimeout {
if removedItem := cs.expirePriorityQueue.Remove(key); removedItem != nil {
// In case ReadyToDelete is true, item should already have been removed from pq
klog.V(4).InfoS("Conn removed from cs pq due to stale timeout",
"key", key, "conn", removedItem.Conn)
}
if err := cs.deleteConnWithoutLock(key); err != nil {
return err
}
}
} else {
conn.IsPresent = false
}
return nil
}
// Hold the lock until we verify whether the connection exist in conntrack table,
// and finish updating the connection store.
cs.AcquireConnStoreLock()
if err := cs.ForAllConnectionsDoWithoutLock(deleteIfStaleOrResetConn); err != nil {
cs.ReleaseConnStoreLock()
return []int{}, err
}
// Update only the Connection store. IPFIX records are generated based on Connection store.
for _, conn := range filteredConnsList {
cs.AddOrUpdateConn(conn)
}
cs.ReleaseConnStoreLock()
metrics.TotalConnectionsInConnTrackTable.Set(float64(totalConns))
maxConns, err := cs.connDumper.GetMaxConnections()
if err != nil {
return []int{}, err
}
metrics.MaxConnectionsInConnTrackTable.Set(float64(maxConns))
klog.V(2).Infof("Conntrack polling successful")
return connsLens, nil
}
func (cs *ConntrackConnectionStore) addNetworkPolicyMetadata(conn *flowexporter.Connection) {
// Retrieve NetworkPolicy Name and Namespace by using the ingress and egress
// IDs stored in the connection label.
if len(conn.Labels) != 0 {
klog.V(4).Infof("connection label: %x; label masks: %x", conn.Labels, conn.LabelsMask)
ingressOfID := binary.LittleEndian.Uint32(conn.Labels[:4])
egressOfID := binary.LittleEndian.Uint32(conn.Labels[4:8])
if ingressOfID != 0 {
policy := cs.networkPolicyQuerier.GetNetworkPolicyByRuleFlowID(ingressOfID)
rule := cs.networkPolicyQuerier.GetRuleByFlowID(ingressOfID)
if policy == nil || rule == nil {
// This should not happen because the rule flow ID to rule mapping is
// preserved for max(5s, flowPollInterval) even after the rule deletion.
klog.Warningf("Cannot find NetworkPolicy or rule with ingressOfID %v", ingressOfID)
} else {
conn.IngressNetworkPolicyName = policy.Name
conn.IngressNetworkPolicyNamespace = policy.Namespace
conn.IngressNetworkPolicyType = flowexporter.PolicyTypeToUint8(policy.Type)
conn.IngressNetworkPolicyRuleName = rule.Name
conn.IngressNetworkPolicyRuleAction = registry.NetworkPolicyRuleActionAllow
}
}
if egressOfID != 0 {
policy := cs.networkPolicyQuerier.GetNetworkPolicyByRuleFlowID(egressOfID)
rule := cs.networkPolicyQuerier.GetRuleByFlowID(egressOfID)
if policy == nil || rule == nil {
// This should not happen because the rule flow ID to rule mapping is
// preserved for max(5s, flowPollInterval) even after the rule deletion.
klog.Warningf("Cannot find NetworkPolicy or rule with egressOfID %v", egressOfID)
} else {
conn.EgressNetworkPolicyName = policy.Name
conn.EgressNetworkPolicyNamespace = policy.Namespace
conn.EgressNetworkPolicyType = flowexporter.PolicyTypeToUint8(policy.Type)
conn.EgressNetworkPolicyRuleName = rule.Name
conn.EgressNetworkPolicyRuleAction = registry.NetworkPolicyRuleActionAllow
}
}
}
}
// AddOrUpdateConn updates the connection if it is already present, i.e., update timestamp, counters etc.,
// or adds a new connection with the resolved K8s metadata.
func (cs *ConntrackConnectionStore) AddOrUpdateConn(conn *flowexporter.Connection) {
conn.IsPresent = true
connKey := flowexporter.NewConnectionKey(conn)
existingConn, exists := cs.connections[connKey]
if exists {
existingConn.IsPresent = conn.IsPresent
if flowexporter.IsConnectionDying(existingConn) {
return
}
// Update the necessary fields that are used in generating flow records.
// Can same 5-tuple flow get deleted and added to conntrack table? If so use ID.
existingConn.StopTime = conn.StopTime
existingConn.OriginalBytes = conn.OriginalBytes
existingConn.OriginalPackets = conn.OriginalPackets
existingConn.ReverseBytes = conn.ReverseBytes
existingConn.ReversePackets = conn.ReversePackets
existingConn.TCPState = conn.TCPState
existingConn.IsActive = flowexporter.CheckConntrackConnActive(existingConn)
if existingConn.IsActive {
existingItem, exists := cs.expirePriorityQueue.KeyToItem[connKey]
if !exists {
// If the connKey:pqItem pair does not exist in the map, it shows the
// conn was inactive, and was removed from PQ and map. Since it becomes
// active again now, we create a new pqItem and add it to PQ and map.
cs.expirePriorityQueue.WriteItemToQueue(connKey, existingConn)
} else {
cs.connectionStore.expirePriorityQueue.Update(existingItem, existingItem.ActiveExpireTime,
time.Now().Add(cs.connectionStore.expirePriorityQueue.IdleFlowTimeout))
}
}
klog.V(4).InfoS("Antrea flow updated", "connection", existingConn)
} else {
cs.fillPodInfo(conn)
if conn.Mark&openflow.ServiceCTMark.GetRange().ToNXRange().ToUint32Mask() == openflow.ServiceCTMark.GetValue() {
clusterIP := conn.DestinationServiceAddress.String()
svcPort := conn.DestinationServicePort
protocol, err := lookupServiceProtocol(conn.FlowKey.Protocol)
if err != nil {
klog.InfoS("Could not retrieve Service protocol", "error", err)
} else {
serviceStr := fmt.Sprintf("%s:%d/%s", clusterIP, svcPort, protocol)
cs.fillServiceInfo(conn, serviceStr)
}
}
cs.addNetworkPolicyMetadata(conn)
if conn.StartTime.IsZero() {
conn.StartTime = time.Now()
conn.StopTime = time.Now()
}
conn.LastExportTime = conn.StartTime
metrics.TotalAntreaConnectionsInConnTrackTable.Inc()
conn.IsActive = true
// Add new antrea connection to connection store and PQ.
cs.connections[connKey] = conn
cs.expirePriorityQueue.WriteItemToQueue(connKey, conn)
klog.V(4).InfoS("New Antrea flow added", "connection", conn)
}
}
func (cs *ConntrackConnectionStore) GetExpiredConns(expiredConns []flowexporter.Connection, currTime time.Time, maxSize int) ([]flowexporter.Connection, time.Duration) {
cs.AcquireConnStoreLock()
defer cs.ReleaseConnStoreLock()
for i := 0; i < maxSize; i++ {
pqItem := cs.connectionStore.expirePriorityQueue.GetTopExpiredItem(currTime)
if pqItem == nil {
break
}
expiredConns = append(expiredConns, *pqItem.Conn)
if flowexporter.IsConnectionDying(pqItem.Conn) {
// If a conntrack connection is in dying state or connection is not
// in the conntrack table, we set the ReadyToDelete flag to true to
// do the deletion later.
pqItem.Conn.ReadyToDelete = true
}
if pqItem.IdleExpireTime.Before(currTime) {
// No packets have been received during the idle timeout interval,
// the connection is therefore considered inactive.
pqItem.Conn.IsActive = false
}
cs.UpdateConnAndQueue(pqItem, currTime)
}
return expiredConns, cs.connectionStore.expirePriorityQueue.GetExpiryFromExpirePriorityQueue()
}
// deleteConnWithoutLock deletes the connection from the connection map given
// the connection key without grabbing the lock. Caller is expected to grab lock.
func (cs *ConntrackConnectionStore) deleteConnWithoutLock(connKey flowexporter.ConnectionKey) error {
_, exists := cs.connections[connKey]
if !exists {
return fmt.Errorf("connection with key %v doesn't exist in map", connKey)
}
delete(cs.connections, connKey)
metrics.TotalAntreaConnectionsInConnTrackTable.Dec()
return nil
}
func (cs *ConntrackConnectionStore) GetPriorityQueue() *priorityqueue.ExpirePriorityQueue {
return cs.connectionStore.expirePriorityQueue
}