forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
539 lines (456 loc) · 13.9 KB
/
types.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Copyright 2016-2019 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"
"unsafe"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/tuple"
)
const (
// MapTypeIPv4TCPLocal and friends are MapTypes which correspond to a
// combination of the following attributes:
// * IPv4 or IPv6;
// * TCP or non-TCP (shortened to Any)
// * Local (endpoint-specific) or global (endpoint-oblivious).
MapTypeIPv4TCPLocal = iota
MapTypeIPv6TCPLocal
MapTypeIPv4TCPGlobal
MapTypeIPv6TCPGlobal
MapTypeIPv4AnyLocal
MapTypeIPv6AnyLocal
MapTypeIPv4AnyGlobal
MapTypeIPv6AnyGlobal
MapTypeMax
)
// MapType is a type of connection tracking map.
type MapType int
// String renders the map type into a user-readable string.
func (m MapType) String() string {
switch m {
case MapTypeIPv4TCPLocal:
return "Local IPv4 TCP CT map"
case MapTypeIPv6TCPLocal:
return "Local IPv6 TCP CT map"
case MapTypeIPv4TCPGlobal:
return "Global IPv4 TCP CT map"
case MapTypeIPv6TCPGlobal:
return "Global IPv6 TCP CT map"
case MapTypeIPv4AnyLocal:
return "Local IPv4 non-TCP CT map"
case MapTypeIPv6AnyLocal:
return "Local IPv6 non-TCP CT map"
case MapTypeIPv4AnyGlobal:
return "Global IPv4 non-TCP CT map"
case MapTypeIPv6AnyGlobal:
return "Global IPv6 non-TCP CT map"
}
return fmt.Sprintf("Unknown (%d)", int(m))
}
func (m MapType) isIPv4() bool {
switch m {
case MapTypeIPv4TCPLocal, MapTypeIPv4TCPGlobal, MapTypeIPv4AnyLocal, MapTypeIPv4AnyGlobal:
return true
}
return false
}
func (m MapType) isIPv6() bool {
switch m {
case MapTypeIPv6TCPLocal, MapTypeIPv6TCPGlobal, MapTypeIPv6AnyLocal, MapTypeIPv6AnyGlobal:
return true
}
return false
}
func (m MapType) isLocal() bool {
switch m {
case MapTypeIPv4TCPLocal, MapTypeIPv6TCPLocal, MapTypeIPv4AnyLocal, MapTypeIPv6AnyLocal:
return true
}
return false
}
func (m MapType) isGlobal() bool {
switch m {
case MapTypeIPv4TCPGlobal, MapTypeIPv6TCPGlobal, MapTypeIPv4AnyGlobal, MapTypeIPv6AnyGlobal:
return true
}
return false
}
func (m MapType) isTCP() bool {
switch m {
case MapTypeIPv4TCPLocal, MapTypeIPv6TCPLocal, MapTypeIPv4TCPGlobal, MapTypeIPv6TCPGlobal:
return true
}
return false
}
type CtKey interface {
bpf.MapKey
// ToNetwork converts fields to network byte order.
ToNetwork() CtKey
// ToHost converts fields to host byte order.
ToHost() CtKey
// Dump contents of key to buffer. Returns true if successful.
Dump(buffer *bytes.Buffer, reverse bool) bool
// GetFlags flags containing the direction of the CtKey.
GetFlags() uint8
GetTupleKey() tuple.TupleKey
}
// CtKey4 is needed to provide CtEntry type to Lookup values
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
type CtKey4 struct {
tuple.TupleKey4
}
// NewValue creates a new bpf.MapValue.
func (k *CtKey4) NewValue() bpf.MapValue { return &CtEntry{} }
// ToNetwork converts CtKey4 ports to network byte order.
func (k *CtKey4) ToNetwork() CtKey {
n := *k
n.SourcePort = byteorder.HostToNetwork(n.SourcePort).(uint16)
n.DestPort = byteorder.HostToNetwork(n.DestPort).(uint16)
return &n
}
// ToHost converts CtKey ports to host byte order.
func (k *CtKey4) ToHost() CtKey {
n := *k
n.SourcePort = byteorder.NetworkToHost(n.SourcePort).(uint16)
n.DestPort = byteorder.NetworkToHost(n.DestPort).(uint16)
return &n
}
// GetFlags returns the tuple's flags.
func (k *CtKey4) GetFlags() uint8 {
return k.Flags
}
func (k *CtKey4) String() string {
return fmt.Sprintf("%s:%d, %d, %d, %d", k.DestAddr, k.SourcePort, k.DestPort, k.NextHeader, k.Flags)
}
// GetKeyPtr returns the unsafe.Pointer for k.
func (k *CtKey4) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
// Dump writes the contents of key to buffer and returns true if the value for
// next header in the key is nonzero.
func (k *CtKey4) Dump(buffer *bytes.Buffer, reverse bool) bool {
var addrDest string
if k.NextHeader == 0 {
return false
}
// Addresses swapped, see issue #5848
if reverse {
addrDest = k.SourceAddr.IP().String()
} else {
addrDest = k.DestAddr.IP().String()
}
if k.Flags&TUPLE_F_IN != 0 {
buffer.WriteString(fmt.Sprintf("%s IN %s %d:%d ",
k.NextHeader.String(), addrDest, k.SourcePort,
k.DestPort),
)
} else {
buffer.WriteString(fmt.Sprintf("%s OUT %s %d:%d ",
k.NextHeader.String(), addrDest, k.DestPort,
k.SourcePort),
)
}
if k.Flags&TUPLE_F_RELATED != 0 {
buffer.WriteString("related ")
}
if k.Flags&TUPLE_F_SERVICE != 0 {
buffer.WriteString("service ")
}
return true
}
func (k *CtKey4) GetTupleKey() tuple.TupleKey {
return &k.TupleKey4
}
// CtKey4Global is needed to provide CtEntry type to Lookup values
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
type CtKey4Global struct {
tuple.TupleKey4Global
}
// NewValue creates a new bpf.MapValue.
func (k *CtKey4Global) NewValue() bpf.MapValue { return &CtEntry{} }
// ToNetwork converts ports to network byte order.
//
// This is necessary to prevent callers from implicitly converting
// the CtKey4Global type here into a local key type in the nested
// TupleKey4Global field.
func (k *CtKey4Global) ToNetwork() CtKey {
return &CtKey4Global{
TupleKey4Global: *k.TupleKey4Global.ToNetwork().(*tuple.TupleKey4Global),
}
}
// ToHost converts ports to host byte order.
//
// This is necessary to prevent callers from implicitly converting
// the CtKey4Global type here into a local key type in the nested
// TupleKey4Global field.
func (k *CtKey4Global) ToHost() CtKey {
return &CtKey4Global{
TupleKey4Global: *k.TupleKey4Global.ToHost().(*tuple.TupleKey4Global),
}
}
// GetFlags returns the tuple's flags.
func (k *CtKey4Global) GetFlags() uint8 {
return k.Flags
}
func (k *CtKey4Global) String() string {
return fmt.Sprintf("%s:%d --> %s:%d, %d, %d", k.SourceAddr, k.SourcePort, k.DestAddr, k.DestPort, k.NextHeader, k.Flags)
}
// GetKeyPtr returns the unsafe.Pointer for k.
func (k *CtKey4Global) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
// Dump writes the contents of key to buffer and returns true if the
// value for next header in the key is nonzero.
func (k *CtKey4Global) Dump(buffer *bytes.Buffer, reverse bool) bool {
var addrSource, addrDest string
if k.NextHeader == 0 {
return false
}
// Addresses swapped, see issue #5848
if reverse {
addrSource = k.DestAddr.IP().String()
addrDest = k.SourceAddr.IP().String()
} else {
addrSource = k.SourceAddr.IP().String()
addrDest = k.DestAddr.IP().String()
}
if k.Flags&TUPLE_F_IN != 0 {
buffer.WriteString(fmt.Sprintf("%s IN %s:%d -> %s:%d ",
k.NextHeader.String(), addrSource, k.SourcePort,
addrDest, k.DestPort),
)
} else {
buffer.WriteString(fmt.Sprintf("%s OUT %s:%d -> %s:%d ",
k.NextHeader.String(), addrSource, k.SourcePort,
addrDest, k.DestPort),
)
}
if k.Flags&TUPLE_F_RELATED != 0 {
buffer.WriteString("related ")
}
if k.Flags&TUPLE_F_SERVICE != 0 {
buffer.WriteString("service ")
}
return true
}
func (k *CtKey4Global) GetTupleKey() tuple.TupleKey {
return &k.TupleKey4Global
}
// CtKey6 is needed to provide CtEntry type to Lookup values
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
type CtKey6 struct {
tuple.TupleKey6
}
// NewValue creates a new bpf.MapValue.
func (k *CtKey6) NewValue() bpf.MapValue { return &CtEntry{} }
// ToNetwork converts CtKey6 ports to network byte order.
func (k *CtKey6) ToNetwork() CtKey {
return &CtKey6{
TupleKey6: *k.TupleKey6.ToNetwork().(*tuple.TupleKey6),
}
}
// ToHost converts CtKey ports to host byte order.
func (k *CtKey6) ToHost() CtKey {
return &CtKey6{
TupleKey6: *k.TupleKey6.ToHost().(*tuple.TupleKey6),
}
}
// GetFlags returns the tuple's flags.
func (k *CtKey6) GetFlags() uint8 {
return k.Flags
}
func (k *CtKey6) String() string {
return fmt.Sprintf("[%s]:%d, %d, %d, %d", k.DestAddr, k.SourcePort, k.DestPort, k.NextHeader, k.Flags)
}
// GetKeyPtr returns the unsafe.Pointer for k.
func (k *CtKey6) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
// Dump writes the contents of key to buffer and returns true if the value for
// next header in the key is nonzero.
func (k *CtKey6) Dump(buffer *bytes.Buffer, reverse bool) bool {
var addrDest string
if k.NextHeader == 0 {
return false
}
// Addresses swapped, see issue #5848
if reverse {
addrDest = k.SourceAddr.IP().String()
} else {
addrDest = k.DestAddr.IP().String()
}
if k.Flags&TUPLE_F_IN != 0 {
buffer.WriteString(fmt.Sprintf("%s IN %s %d:%d ",
k.NextHeader.String(), addrDest, k.SourcePort,
k.DestPort),
)
} else {
buffer.WriteString(fmt.Sprintf("%s OUT %s %d:%d ",
k.NextHeader.String(), addrDest, k.DestPort,
k.SourcePort),
)
}
if k.Flags&TUPLE_F_RELATED != 0 {
buffer.WriteString("related ")
}
if k.Flags&TUPLE_F_SERVICE != 0 {
buffer.WriteString("service ")
}
return true
}
func (k *CtKey6) GetTupleKey() tuple.TupleKey {
return &k.TupleKey6
}
// CtKey6Global is needed to provide CtEntry type to Lookup values
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
type CtKey6Global struct {
tuple.TupleKey6Global
}
// NewValue creates a new bpf.MapValue.
func (k *CtKey6Global) NewValue() bpf.MapValue { return &CtEntry{} }
// ToNetwork converts ports to network byte order.
//
// This is necessary to prevent callers from implicitly converting
// the CtKey6Global type here into a local key type in the nested
// TupleKey6Global field.
func (k *CtKey6Global) ToNetwork() CtKey {
return &CtKey6Global{
TupleKey6Global: *k.TupleKey6Global.ToNetwork().(*tuple.TupleKey6Global),
}
}
// ToHost converts ports to host byte order.
//
// This is necessary to prevent callers from implicitly converting
// the CtKey6Global type here into a local key type in the nested
// TupleKey6Global field.
func (k *CtKey6Global) ToHost() CtKey {
return &CtKey6Global{
TupleKey6Global: *k.TupleKey6Global.ToHost().(*tuple.TupleKey6Global),
}
}
// GetFlags returns the tuple's flags.
func (k *CtKey6Global) GetFlags() uint8 {
return k.Flags
}
func (k *CtKey6Global) String() string {
return fmt.Sprintf("[%s]:%d --> [%s]:%d, %d, %d", k.SourceAddr, k.SourcePort, k.DestAddr, k.DestPort, k.NextHeader, k.Flags)
}
// GetKeyPtr returns the unsafe.Pointer for k.
func (k *CtKey6Global) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
// Dump writes the contents of key to buffer and returns true if the
// value for next header in the key is nonzero.
func (k *CtKey6Global) Dump(buffer *bytes.Buffer, reverse bool) bool {
var addrSource, addrDest string
if k.NextHeader == 0 {
return false
}
// Addresses swapped, see issue #5848
if reverse {
addrSource = k.DestAddr.IP().String()
addrDest = k.SourceAddr.IP().String()
} else {
addrSource = k.SourceAddr.IP().String()
addrDest = k.DestAddr.IP().String()
}
if k.Flags&TUPLE_F_IN != 0 {
buffer.WriteString(fmt.Sprintf("%s IN %s:%d -> %s:%d ",
k.NextHeader.String(), addrSource, k.SourcePort,
addrDest, k.DestPort),
)
} else {
buffer.WriteString(fmt.Sprintf("%s OUT %s:%d -> %s:%d ",
k.NextHeader.String(), addrSource, k.SourcePort,
addrDest, k.DestPort),
)
}
if k.Flags&TUPLE_F_RELATED != 0 {
buffer.WriteString("related ")
}
if k.Flags&TUPLE_F_SERVICE != 0 {
buffer.WriteString("service ")
}
return true
}
func (k *CtKey6Global) GetTupleKey() tuple.TupleKey {
return &k.TupleKey6Global
}
// CtEntry represents an entry in the connection tracking table.
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue
type CtEntry struct {
RxPackets uint64 `align:"rx_packets"`
RxBytes uint64 `align:"rx_bytes"`
TxPackets uint64 `align:"tx_packets"`
TxBytes uint64 `align:"tx_bytes"`
Lifetime uint32 `align:"lifetime"`
Flags uint16 `align:"rx_closing"`
// RevNAT is in network byte order
RevNAT uint16 `align:"rev_nat_index"`
_ uint16 `align:"backend_id"`
TxFlagsSeen uint8 `align:"tx_flags_seen"`
RxFlagsSeen uint8 `align:"rx_flags_seen"`
SourceSecurityID uint32 `align:"src_sec_id"`
LastTxReport uint32 `align:"last_tx_report"`
LastRxReport uint32 `align:"last_rx_report"`
}
// GetValuePtr returns the unsafe.Pointer for s.
func (c *CtEntry) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(c) }
const (
RxClosing = 1 << 0
TxClosing = 1 << 1
Nat64 = 1 << 2
LBLoopback = 1 << 3
SeenNonSyn = 1 << 4
NodePort = 1 << 5
)
func (c *CtEntry) flagsString() string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("Flags=%#04x [ ", c.Flags))
if (c.Flags & RxClosing) != 0 {
buffer.WriteString("RxClosing ")
}
if (c.Flags & TxClosing) != 0 {
buffer.WriteString("TxClosing ")
}
if (c.Flags & Nat64) != 0 {
buffer.WriteString("Nat64 ")
}
if (c.Flags & LBLoopback) != 0 {
buffer.WriteString("LBLoopback ")
}
if (c.Flags & SeenNonSyn) != 0 {
buffer.WriteString("SeenNonSyn ")
}
if (c.Flags & NodePort) != 0 {
buffer.WriteString("NodePort ")
}
buffer.WriteString("]")
return buffer.String()
}
// String returns the readable format
func (c *CtEntry) String() string {
return fmt.Sprintf("expires=%d RxPackets=%d RxBytes=%d RxFlagsSeen=%#02x LastRxReport=%d TxPackets=%d TxBytes=%d TxFlagsSeen=%#02x LastTxReport=%d %s RevNAT=%d SourceSecurityID=%d \n",
c.Lifetime,
c.RxPackets,
c.RxBytes,
c.RxFlagsSeen,
c.LastRxReport,
c.TxPackets,
c.TxBytes,
c.TxFlagsSeen,
c.LastTxReport,
c.flagsString(),
byteorder.NetworkToHost(c.RevNAT),
c.SourceSecurityID)
}