-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
fragmap.go
61 lines (49 loc) · 1.6 KB
/
fragmap.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package fragmap
import (
"fmt"
"github.com/cilium/ebpf"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/types"
)
const (
// MapName is the name of the map used to retrieve L4 ports associated
// to the datagram to which an IPv4 belongs.
MapName = "cilium_ipv4_frag_datagrams"
)
// FragmentKey must match 'struct ipv4_frag_id' in "bpf/lib/ipv4.h".
type FragmentKey struct {
destAddr types.IPv4 `align:"daddr"`
sourceAddr types.IPv4 `align:"saddr"`
id uint16 `align:"id"`
proto uint8 `align:"proto"`
_ uint8
}
// FragmentValue must match 'struct ipv4_frag_l4ports' in "bpf/lib/ipv4.h".
type FragmentValue struct {
sourcePort uint16 `align:"sport"`
destPort uint16 `align:"dport"`
}
// String converts the key into a human readable string format.
func (k *FragmentKey) String() string {
return fmt.Sprintf("%s --> %s, %d, %d", k.sourceAddr, k.destAddr, k.proto, k.id)
}
func (k *FragmentKey) New() bpf.MapKey { return &FragmentKey{} }
// String converts the value into a human readable string format.
func (v *FragmentValue) String() string {
return fmt.Sprintf("%d, %d", v.destPort, v.sourcePort)
}
func (v *FragmentValue) New() bpf.MapValue { return &FragmentValue{} }
// InitMap creates the signal map in the kernel.
func InitMap(mapEntries int) error {
fragMap := bpf.NewMap(MapName,
ebpf.LRUHash,
&FragmentKey{},
&FragmentValue{},
mapEntries,
0,
).WithEvents(option.Config.GetEventBufferConfig(MapName))
return fragMap.Create()
}