forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctmap.go
351 lines (292 loc) · 7.81 KB
/
ctmap.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2016-2017 Authors of Cilium
//
// 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 ctmap
import (
"bytes"
"fmt"
"net"
"unsafe"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/byteorder"
log "github.com/Sirupsen/logrus"
)
const (
MapName6 = "cilium_ct6_"
MapName4 = "cilium_ct4_"
MapName6Global = MapName6 + "global"
MapName4Global = MapName4 + "global"
MapNumEntriesLocal = 64000
MapNumEntriesGlobal = 1000000
TUPLE_F_OUT = 0
TUPLE_F_IN = 1
TUPLE_F_RELATED = 2
)
type CtType int
// CtKey is the interface describing keys to the conntrack maps.
type CtKey interface {
bpf.MapKey
// Returns human readable string representation
String() string
// ToNetwork converts fields to network byte order.
ToNetwork() CtKey
// ToHost converts fields to host byte order.
ToHost() CtKey
// Dumps contents of key to buffer. Returns true if successful.
Dump(buffer *bytes.Buffer) bool
}
// CtValue is the interface describing values in the conntrack maps.
type CtValue interface {
bpf.MapValue
// ToNetwork converts fields to network byte order.
ToNetwork() CtValue
// ToHost converts fields to host byte order.
ToHost() CtValue
}
// CtEntry represents an entry in the connection tracking table.
type CtEntry struct {
rx_packets uint64
rx_bytes uint64
tx_packets uint64
tx_bytes uint64
lifetime uint32
flags uint16
revnat uint16
proxy_port uint16
src_sec_id uint32
}
// GetValuePtr returns the unsafe.Pointer for s.
func (c *CtEntry) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(c) }
// CtEntryDump represents the key and value contained in the conntrack map.
type CtEntryDump struct {
Key CtKey
Value CtEntry
}
const (
// GCFilterByTime filters CT entries by time
GCFilterByTime = 1 << iota
// GCFilterByID filters CT entries by IP and IDsToRem
GCFilterByID
)
// GCFilterFlags is the type for the different filter flags
type GCFilterFlags uint
// GCFilter contains the necessary fields to filter the CT maps.
type GCFilter struct {
Time uint32
IP net.IP
IDsToRm map[uint32]bool
fType GCFilterFlags
}
// NewGCFilterBy creates a new GCFilter with the given flags.
func NewGCFilterBy(f GCFilterFlags) *GCFilter {
return &GCFilter{
fType: f,
IDsToRm: map[uint32]bool{},
}
}
// ToString iterates through Map m and writes the values of the ct entries in m
// to a string.
func ToString(m *bpf.Map, mapName string) (string, error) {
var buffer bytes.Buffer
entries, err := dumpToSlice(m, mapName)
if err != nil {
return "", err
}
for _, entry := range entries {
if !entry.Key.Dump(&buffer) {
continue
}
value := entry.Value
buffer.WriteString(
fmt.Sprintf(" expires=%d rx_packets=%d rx_bytes=%d tx_packets=%d tx_bytes=%d flags=%x revnat=%d proxyport=%d src_sec_id=%d\n",
value.lifetime,
value.rx_packets,
value.rx_bytes,
value.tx_packets,
value.tx_bytes,
value.flags,
byteorder.NetworkToHost(value.revnat),
byteorder.NetworkToHost(value.proxy_port),
value.src_sec_id,
),
)
}
return buffer.String(), nil
}
// DumpToSlice iterates through map m and returns a slice mapping each key to
// its value in m.
func dumpToSlice(m *bpf.Map, mapType string) ([]CtEntryDump, error) {
entries := []CtEntryDump{}
switch mapType {
case MapName6, MapName6Global:
var key, nextKey CtKey6Global
for {
err := m.GetNextKey(&key, &nextKey)
if err != nil {
break
}
entry, err := m.Lookup(&nextKey)
if err != nil {
return nil, err
}
ctEntry := entry.(*CtEntry)
nK := nextKey
eDump := CtEntryDump{Key: &nK, Value: *ctEntry}
entries = append(entries, eDump)
key = nextKey
}
case MapName4, MapName4Global:
var key, nextKey CtKey4Global
for {
err := m.GetNextKey(&key, &nextKey)
if err != nil {
break
}
entry, err := m.Lookup(&nextKey)
if err != nil {
return nil, err
}
ctEntry := entry.(*CtEntry)
nK := nextKey
eDump := CtEntryDump{Key: &nK, Value: *ctEntry}
entries = append(entries, eDump)
key = nextKey
}
}
return entries, nil
}
// doGC6 iterates through a CTv6 map and drops entries based on the given
// filter.
func doGC6(m *bpf.Map, filter *GCFilter) int {
var (
deleted int
nextKey, tmpKey CtKey6Global
del bool
)
err := m.GetNextKey(&tmpKey, &nextKey)
if err != nil {
return 0
}
for {
del = false
nextKeyValid := m.GetNextKey(&nextKey, &tmpKey)
entryMap, err := m.Lookup(&nextKey)
if err != nil {
log.Errorf("error during map Lookup: %s", err)
break
}
entry := entryMap.(*CtEntry)
// FIXME create a single function for doGC4 and doGC6
if filter.fType&GCFilterByTime != 0 &&
entry.lifetime < filter.Time {
del = true
log.Debugf("Deleting entry %v since it timeout", entry)
}
if filter.fType&GCFilterByID != 0 &&
// In CT's entries, saddr is the packet's receiver,
// which means, is the destination container IP.
nextKey.saddr.IP().Equal(filter.IP) {
// Check if the src_sec_id of that entry is not allowed
// to talk with the destination container IP.
if _, ok := filter.IDsToRm[entry.src_sec_id]; ok {
del = true
log.Debugf("Deleting entry since ID %d is no "+
"longer being consumed by %s", entry.src_sec_id, filter.IP)
}
}
if del {
err := m.Delete(&nextKey)
if err != nil {
log.Debugf("error during Delete: %s", err)
} else {
deleted++
}
}
if nextKeyValid != nil {
break
}
nextKey = tmpKey
}
return deleted
}
// doGC4 iterates through a CTv4 map and drops entries based on the given
// filter.
func doGC4(m *bpf.Map, filter *GCFilter) int {
var (
deleted int
nextKey, tmpKey CtKey4Global
del bool
)
err := m.GetNextKey(&tmpKey, &nextKey)
if err != nil {
return 0
}
for true {
del = false
nextKeyValid := m.GetNextKey(&nextKey, &tmpKey)
entryMap, err := m.Lookup(&nextKey)
if err != nil {
log.Errorf("error during map Lookup: %s", err)
break
}
entry := entryMap.(*CtEntry)
// FIXME create a single function for doGC4 and doGC6
if filter.fType&GCFilterByTime != 0 &&
entry.lifetime < filter.Time {
del = true
log.Debugf("Deleting entry %v since it timeout", entry)
}
if filter.fType&GCFilterByID != 0 &&
// In CT's entries, saddr is the packet's receiver,
// which means, is the destination container IP.
nextKey.saddr.IP().Equal(filter.IP) {
// Check if the src_sec_id of that entry is not allowed
// to talk with the destination container IP.
if _, ok := filter.IDsToRm[entry.src_sec_id]; ok {
del = true
log.Debugf("Deleting entry since ID %d is no "+
"longer being consumed by %s", entry.src_sec_id, filter.IP)
}
}
if del {
err := m.Delete(&nextKey)
if err != nil {
log.Debugf("error during Delete: %s", err)
} else {
deleted++
}
}
if nextKeyValid != nil {
break
}
nextKey = tmpKey
}
return deleted
}
// GC runs garbage collection for map m with name mapName with the given filter.
// It returns how many items were deleted from m.
func GC(m *bpf.Map, mapName string, filter *GCFilter) int {
if filter.fType&GCFilterByTime != 0 {
t, _ := bpf.GetMtime()
tsec := t / 1000000000
filter.Time = uint32(tsec)
}
switch mapName {
case MapName6, MapName6Global:
return doGC6(m, filter)
case MapName4, MapName4Global:
return doGC4(m, filter)
default:
return 0
}
}