-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
gc.go
280 lines (248 loc) · 8.51 KB
/
gc.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package gc
import (
"fmt"
"net"
"os"
"time"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/datapath/types"
"github.com/cilium/cilium/pkg/endpoint"
"github.com/cilium/cilium/pkg/inctimer"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maps/ctmap"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/signal"
)
var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "ct-gc")
// EndpointManager is any type which returns the list of Endpoints which are
// globally exposed on the current node.
type EndpointManager interface {
GetEndpoints() []*endpoint.Endpoint
}
// Enable enables the connection tracking garbage collection.
// The restored endpoints and local node addresses are used to avoid GCing
// connections that may still be in use: connections of active endpoints and,
// in case the host firewall is enabled, connections of the local host.
func Enable(ipv4, ipv6 bool, restoredEndpoints []*endpoint.Endpoint, mgr EndpointManager,
nodeAddressing types.NodeAddressing) {
var (
initialScan = true
initialScanComplete = make(chan struct{})
mapType bpf.MapType
)
go func() {
var wakeup = make(chan signal.SignalData)
ipv4Orig := ipv4
ipv6Orig := ipv6
triggeredBySignal := false
ctTimer, ctTimerDone := inctimer.New()
defer ctTimerDone()
for {
var (
maxDeleteRatio float64
// epsMap contains an IP -> EP mapping. It is used by EmitCTEntryCB to
// avoid doing mgr.LookupIP, which is more expensive.
epsMap = make(map[string]*endpoint.Endpoint)
// gcStart and emitEntryCB are used to populate DNSZombieMapping fields
// on endpoints. These hold IPs that are deletable in the DNS caches,
// but may be in use by connections. Each loop of this GC keeps those
// entries alive by touching them in emitEntryCB. We also need to
// record the start of each CT GC loop (further below in the
// goroutine). In all cases the timestamp used is the start of the GC
// loop. This simplifies the logic to determine if a marked connection
// was marked in the most recent GC loop or not: if the active
// timestamp is before the recorded start of the GC loop then it must
// mean the next iteration has completed and it is not in-use.
gcStart = time.Now()
// aliveTime is offset to the future by ToFQDNsIdleConnectionGracePeriod
// (default 0), allowing previously active connections to be considerred
// alive during idle periods of upto ToFQDNsIdleConnectionGracePeriod.
aliveTime = gcStart.Add(option.Config.ToFQDNsIdleConnectionGracePeriod)
emitEntryCB = func(srcIP, dstIP net.IP, srcPort, dstPort uint16, nextHdr, flags uint8, entry *ctmap.CtEntry) {
// FQDN related connections can only be outbound
if flags != ctmap.TUPLE_F_OUT {
return
}
if ep, exists := epsMap[srcIP.String()]; exists {
ep.MarkDNSCTEntry(dstIP, aliveTime)
}
}
)
eps := mgr.GetEndpoints()
for _, e := range eps {
epsMap[e.GetIPv4Address()] = e
epsMap[e.GetIPv6Address()] = e
}
if len(eps) > 0 || initialScan {
gcFilter := createGCFilter(initialScan, restoredEndpoints, emitEntryCB, nodeAddressing)
mapType, maxDeleteRatio = runGC(nil, ipv4, ipv6, triggeredBySignal, gcFilter)
}
for _, e := range eps {
if !e.ConntrackLocal() {
// Skip because GC was handled above.
continue
}
runGC(e, ipv4, ipv6, triggeredBySignal, &ctmap.GCFilter{RemoveExpired: true, EmitCTEntryCB: emitEntryCB})
}
// Mark the CT GC as over in each EP DNSZombies instance
for _, e := range eps {
e.MarkCTGCTime(gcStart)
}
if initialScan {
close(initialScanComplete)
initialScan = false
signal.RegisterChannel(signal.SignalWakeGC, wakeup)
signal.SetupSignalListener()
signal.MuteChannel(signal.SignalWakeGC)
}
triggeredBySignal = false
signal.UnmuteChannel(signal.SignalWakeGC)
select {
case x := <-wakeup:
triggeredBySignal = true
ipv4 = false
ipv6 = false
if x == signal.SignalProtoV4 {
ipv4 = true
} else if x == signal.SignalProtoV6 {
ipv6 = true
}
// Drain current queue since we just woke up anyway.
for len(wakeup) > 0 {
x := <-wakeup
if x == signal.SignalProtoV4 {
ipv4 = true
} else if x == signal.SignalProtoV6 {
ipv6 = true
}
}
case <-ctTimer.After(ctmap.GetInterval(mapType, maxDeleteRatio)):
ipv4 = ipv4Orig
ipv6 = ipv6Orig
}
signal.MuteChannel(signal.SignalWakeGC)
}
}()
select {
case <-initialScanComplete:
log.Info("Initial scan of connection tracking completed")
case <-time.After(30 * time.Second):
log.Fatal("Timeout while waiting for initial conntrack scan")
}
}
// runGC run CT's garbage collector for the given endpoint. `isLocal` refers if
// the CT map is set to local. If `isIPv6` is set specifies that is the IPv6
// map. `filter` represents the filter type to be used while looping all CT
// entries.
//
// The provided endpoint is optional; if it is provided, then its map will be
// garbage collected and any failures will be logged to the endpoint log.
// Otherwise it will garbage-collect the global map and use the global log.
func runGC(e *endpoint.Endpoint, ipv4, ipv6, triggeredBySignal bool, filter *ctmap.GCFilter) (mapType bpf.MapType, maxDeleteRatio float64) {
var maps []*ctmap.Map
if e == nil {
maps = ctmap.GlobalMaps(ipv4, ipv6)
} else {
maps = ctmap.LocalMaps(e, ipv4, ipv6)
}
for _, m := range maps {
path, err := m.Path()
if err == nil {
err = m.Open()
}
if err != nil {
msg := "Skipping CT garbage collection"
scopedLog := log.WithError(err).WithField(logfields.Path, path)
if os.IsNotExist(err) {
scopedLog.Debug(msg)
} else {
scopedLog.Warn(msg)
}
if e != nil {
e.LogStatus(endpoint.BPF, endpoint.Warning, fmt.Sprintf("%s: %s", msg, err))
}
continue
}
defer m.Close()
mapType = m.MapInfo.MapType
deleted := ctmap.GC(m, filter)
if deleted > 0 {
ratio := float64(deleted) / float64(m.MapInfo.MaxEntries)
if ratio > maxDeleteRatio {
maxDeleteRatio = ratio
}
log.WithFields(logrus.Fields{
logfields.Path: path,
"count": deleted,
}).Debug("Deleted filtered entries from map")
}
}
if e == nil && triggeredBySignal {
vsns := []ctmap.CTMapIPVersion{}
if ipv4 {
vsns = append(vsns, ctmap.CTMapIPv4)
}
if ipv6 {
vsns = append(vsns, ctmap.CTMapIPv6)
}
for _, vsn := range vsns {
ctMapTCP, ctMapAny := ctmap.FilterMapsByProto(maps, vsn)
stats := ctmap.PurgeOrphanNATEntries(ctMapTCP, ctMapAny)
if stats != nil && (stats.EgressDeleted != 0 || stats.IngressDeleted != 0) {
log.WithFields(logrus.Fields{
"ingressDeleted": stats.IngressDeleted,
"egressDeleted": stats.EgressDeleted,
"ingressAlive": stats.IngressAlive,
"ctMapIPVersion": vsn,
}).Info("Deleted orphan SNAT entries from map")
}
}
}
return
}
func createGCFilter(initialScan bool, restoredEndpoints []*endpoint.Endpoint,
emitEntryCB ctmap.EmitCTEntryCBFunc, nodeAddressing types.NodeAddressing) *ctmap.GCFilter {
filter := &ctmap.GCFilter{
RemoveExpired: true,
EmitCTEntryCB: emitEntryCB,
}
// On the initial scan, scrub all IPs from the conntrack table which do
// not belong to IPs of any endpoint that has been restored. No new
// endpoints can appear yet so we can assume that any other entry not
// belonging to a restored endpoint has become stale.
if initialScan {
filter.ValidIPs = map[string]struct{}{}
for _, ep := range restoredEndpoints {
if ep.IsHost() {
continue
}
filter.ValidIPs[ep.IPv6.String()] = struct{}{}
filter.ValidIPs[ep.IPv4.String()] = struct{}{}
}
// Once the host firewall is enabled, we will start tracking (and
// potentially enforcing policies) on all connections to and from the
// host IP addresses. Thus, we also need to avoid GCing the host IPs.
if option.Config.EnableHostFirewall {
addrs, err := nodeAddressing.IPv4().LocalAddresses()
if err != nil {
log.WithError(err).Warning("Unable to list local IPv4 addresses")
}
addrsV6, err := nodeAddressing.IPv6().LocalAddresses()
if err != nil {
log.WithError(err).Warning("Unable to list local IPv6 addresses")
}
addrs = append(addrs, addrsV6...)
for _, ip := range addrs {
if option.Config.IsExcludedLocalAddress(ip) {
continue
}
filter.ValidIPs[ip.String()] = struct{}{}
}
}
}
return filter
}