-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
node_map.go
116 lines (97 loc) · 2.44 KB
/
node_map.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package nodemap
import (
"net"
"sync"
"unsafe"
"golang.org/x/sys/unix"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/ebpf"
"github.com/cilium/cilium/pkg/types"
)
const (
MapName = "cilium_node_map"
MaxEntries = 16384
)
var (
nodeMap *Map
nodeMapInit = &sync.Once{}
)
// SetNodeMap sets the node map. Only used for testing.
func SetNodeMap(m *Map) {
nodeMap = m
}
func NodeMap() *Map {
nodeMapInit.Do(func() {
nodeMap = NewNodeMap(MapName)
})
return nodeMap
}
type Map struct {
*ebpf.Map
}
type NodeKey struct {
Pad1 uint16 `align:"pad1"`
Pad2 uint8 `align:"pad2"`
Family uint8 `align:"family"`
// represents both IPv6 and IPv4 (in the lowest four bytes)
IP types.IPv6 `align:"$union0"`
}
func (k *NodeKey) String() string {
switch k.Family {
case bpf.EndpointKeyIPv4:
return net.IP(k.IP[:net.IPv4len]).String()
case bpf.EndpointKeyIPv6:
return k.IP.String()
}
return "<unknown>"
}
func newNodeKey(ip net.IP) NodeKey {
result := NodeKey{}
if ip4 := ip.To4(); ip4 != nil {
result.Family = bpf.EndpointKeyIPv4
copy(result.IP[:], ip4)
} else {
result.Family = bpf.EndpointKeyIPv6
copy(result.IP[:], ip)
}
return result
}
type NodeValue struct {
NodeID uint16
}
func NewNodeMap(mapName string) *Map {
return &Map{Map: ebpf.NewMap(&ebpf.MapSpec{
Name: mapName,
Type: ebpf.Hash,
KeySize: uint32(unsafe.Sizeof(NodeKey{})),
ValueSize: uint32(unsafe.Sizeof(NodeValue{})),
MaxEntries: uint32(MaxEntries),
Flags: unix.BPF_F_NO_PREALLOC,
Pinning: ebpf.PinByName,
})}
}
func (m *Map) Update(ip net.IP, nodeID uint16) error {
key := newNodeKey(ip)
val := NodeValue{NodeID: nodeID}
return m.Map.Update(key, val, 0)
}
func (m *Map) Delete(ip net.IP) error {
key := newNodeKey(ip)
return m.Map.Delete(key)
}
// NodeIterateCallback represents the signature of the callback function
// expected by the IterateWithCallback method, which in turn is used to iterate
// all the keys/values of a node map.
type NodeIterateCallback func(*NodeKey, *NodeValue)
// IterateWithCallback iterates through all the keys/values of a node map,
// passing each key/value pair to the cb callback.
func (m Map) IterateWithCallback(cb NodeIterateCallback) error {
return m.Map.IterateWithCallback(&NodeKey{}, &NodeValue{},
func(k, v interface{}) {
key := k.(*NodeKey)
value := v.(*NodeValue)
cb(key, value)
})
}