/
deny_connections.go
157 lines (144 loc) · 5.81 KB
/
deny_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
// 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 (
"fmt"
"time"
"k8s.io/klog/v2"
"antrea.io/antrea/pkg/agent/flowexporter"
"antrea.io/antrea/pkg/agent/flowexporter/priorityqueue"
"antrea.io/antrea/pkg/agent/metrics"
"antrea.io/antrea/pkg/agent/openflow"
"antrea.io/antrea/pkg/agent/proxy"
"antrea.io/antrea/pkg/util/ip"
"antrea.io/antrea/pkg/util/podstore"
)
type DenyConnectionStore struct {
connectionStore
}
func NewDenyConnectionStore(podStore podstore.Interface, proxier proxy.Proxier, o *flowexporter.FlowExporterOptions) *DenyConnectionStore {
return &DenyConnectionStore{
connectionStore: NewConnectionStore(podStore, proxier, o),
}
}
func (ds *DenyConnectionStore) RunPeriodicDeletion(stopCh <-chan struct{}) {
pollTicker := time.NewTicker(periodicDeleteInterval)
defer pollTicker.Stop()
for {
select {
case <-stopCh:
break
case <-pollTicker.C:
deleteIfStaleConn := func(key flowexporter.ConnectionKey, conn *flowexporter.Connection) error {
if conn.ReadyToDelete || time.Since(conn.LastExportTime) >= ds.staleConnectionTimeout {
if removedItem := ds.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 ds pq due to stale timeout",
"key", key, "conn", removedItem.Conn)
}
if err := ds.deleteConnWithoutLock(key); err != nil {
return err
}
}
return nil
}
ds.ForAllConnectionsDo(deleteIfStaleConn)
klog.V(2).Infof("Stale connections in the Deny Connection Store are successfully deleted.")
}
}
}
// 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 (ds *DenyConnectionStore) AddOrUpdateConn(conn *flowexporter.Connection, timeSeen time.Time, bytes uint64) {
connKey := flowexporter.NewConnectionKey(conn)
ds.mutex.Lock()
defer ds.mutex.Unlock()
if _, exist := ds.connections[connKey]; exist {
if conn.ReadyToDelete {
return
}
conn.OriginalBytes += bytes
conn.OriginalPackets += 1
conn.StopTime = timeSeen
conn.IsActive = true
existingItem, exists := ds.expirePriorityQueue.KeyToItem[connKey]
if !exists {
ds.expirePriorityQueue.WriteItemToQueue(connKey, conn)
} else {
ds.connectionStore.expirePriorityQueue.Update(existingItem, existingItem.ActiveExpireTime,
time.Now().Add(ds.connectionStore.expirePriorityQueue.IdleFlowTimeout))
}
klog.V(4).InfoS("Deny connection has been updated", "connection", conn)
} else {
conn.StartTime = timeSeen
conn.StopTime = timeSeen
conn.LastExportTime = timeSeen
conn.OriginalBytes = bytes
conn.OriginalPackets = uint64(1)
ds.fillPodInfo(conn)
if conn.SourcePodName == "" && conn.DestinationPodName == "" {
// We don't add connections to connection map or expirePriorityQueue if we can't find the pod
// information for both srcPod and dstPod
klog.V(5).InfoS("Skip this connection as we cannot map any of the connection IPs to a local Pod", "srcIP", conn.FlowKey.SourceAddress.String(), "dstIP", conn.FlowKey.DestinationAddress.String())
return
}
protocolStr := ip.IPProtocolNumberToString(conn.FlowKey.Protocol, "UnknownProtocol")
serviceStr := fmt.Sprintf("%s:%d/%s", conn.DestinationServiceAddress, conn.DestinationServicePort, protocolStr)
if conn.Mark&openflow.ServiceCTMark.GetRange().ToNXRange().ToUint32Mask() == openflow.ServiceCTMark.GetValue() {
ds.fillServiceInfo(conn, serviceStr)
}
metrics.TotalDenyConnections.Inc()
conn.IsActive = true
ds.connections[connKey] = conn
ds.expirePriorityQueue.WriteItemToQueue(connKey, conn)
klog.V(4).InfoS("New deny connection added", "connection", conn)
}
}
func (ds *DenyConnectionStore) GetExpiredConns(expiredConns []flowexporter.Connection, currTime time.Time, maxSize int) ([]flowexporter.Connection, time.Duration) {
ds.AcquireConnStoreLock()
defer ds.ReleaseConnStoreLock()
for i := 0; i < maxSize; i++ {
pqItem := ds.connectionStore.expirePriorityQueue.GetTopExpiredItem(currTime)
if pqItem == nil {
break
}
expiredConns = append(expiredConns, *pqItem.Conn)
if pqItem.IdleExpireTime.Before(currTime) {
// If a deny connection item is idle time out, we set the ReadyToDelete
// flag to true to do the deletion later.
pqItem.Conn.ReadyToDelete = true
}
if pqItem.Conn.OriginalPackets <= pqItem.Conn.PrevPackets {
// If a deny connection doesn't have increase in packet count,
// we consider the connection to be inactive.
pqItem.Conn.IsActive = false
}
ds.UpdateConnAndQueue(pqItem, currTime)
}
return expiredConns, ds.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 (ds *DenyConnectionStore) deleteConnWithoutLock(connKey flowexporter.ConnectionKey) error {
_, exists := ds.connections[connKey]
if !exists {
return fmt.Errorf("connection with key %v doesn't exist in map", connKey)
}
delete(ds.connections, connKey)
metrics.TotalDenyConnections.Dec()
return nil
}
func (ds *DenyConnectionStore) GetPriorityQueue() *priorityqueue.ExpirePriorityQueue {
return ds.connectionStore.expirePriorityQueue
}