forked from iovisor/gobpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
199 lines (176 loc) · 4.92 KB
/
table.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
// +build linux
// Copyright 2016 Cilium Project
// Copyright 2016 Sylvain Afchain
// Copyright 2016 Kinvolk
//
// 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 elf
import (
"fmt"
"syscall"
"unsafe"
)
/*
#include <linux/bpf.h>
#include <linux/unistd.h>
extern __u64 ptr_to_u64(void *);
// from https://github.com/cilium/cilium/blob/master/pkg/bpf/bpf.go
// Apache License, Version 2.0
static void create_bpf_update_elem(int fd, void *key, void *value,
unsigned long long flags, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->map_fd = fd;
ptr_bpf_attr->key = ptr_to_u64(key);
ptr_bpf_attr->value = ptr_to_u64(value);
ptr_bpf_attr->flags = flags;
}
static void create_bpf_lookup_elem(int fd, void *key, void *value, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->map_fd = fd;
ptr_bpf_attr->key = ptr_to_u64(key);
ptr_bpf_attr->value = ptr_to_u64(value);
}
static int next_bpf_elem(int fd, void *key, void *next_key, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->map_fd = fd;
ptr_bpf_attr->key = ptr_to_u64(key);
ptr_bpf_attr->next_key = ptr_to_u64(next_key);
}
*/
import "C"
// UpdateElement stores value in key in the map stored in mp.
// The flags can have the following values (if you include "uapi/linux/bpf.h"):
// C.BPF_ANY to create new element or update existing;
// C.BPF_NOEXIST to create new element if it didn't exist;
// C.BPF_EXIST to update existing element.
func (b *Module) UpdateElement(mp *Map, key, value unsafe.Pointer, flags uint64) error {
uba := C.union_bpf_attr{}
C.create_bpf_update_elem(
C.int(mp.m.fd),
key,
value,
C.ulonglong(flags),
unsafe.Pointer(&uba),
)
ret, _, err := syscall.Syscall(
C.__NR_bpf,
C.BPF_MAP_UPDATE_ELEM,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("unable to update element: %s", err)
}
return nil
}
// LookupElement looks up the given key in the the map stored in mp.
// The value is stored in the value unsafe.Pointer.
func (b *Module) LookupElement(mp *Map, key, value unsafe.Pointer) error {
uba := C.union_bpf_attr{}
C.create_bpf_lookup_elem(
C.int(mp.m.fd),
key,
value,
unsafe.Pointer(&uba),
)
ret, _, err := syscall.Syscall(
C.__NR_bpf,
C.BPF_MAP_LOOKUP_ELEM,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("unable to lookup element: %s", err)
}
return nil
}
// LookupAndDeleteElement picks up and delete the element in the the map stored in mp.
// The value is stored in the value unsafe.Pointer.
func (b *Module) LookupAndDeleteElement(mp *Map, value unsafe.Pointer) error {
uba := C.union_bpf_attr{}
C.create_bpf_lookup_elem(
C.int(mp.m.fd),
unsafe.Pointer(nil),
value,
unsafe.Pointer(&uba),
)
ret, _, err := syscall.Syscall(
C.__NR_bpf,
C.BPF_MAP_LOOKUP_AND_DELETE_ELEM,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("unable to lookup and delete element: %s", err)
}
return nil
}
// DeleteElement deletes the given key in the the map stored in mp.
// The key is stored in the key unsafe.Pointer.
func (b *Module) DeleteElement(mp *Map, key unsafe.Pointer) error {
uba := C.union_bpf_attr{}
value := unsafe.Pointer(nil)
C.create_bpf_lookup_elem(
C.int(mp.m.fd),
key,
value,
unsafe.Pointer(&uba),
)
ret, _, err := syscall.Syscall(
C.__NR_bpf,
C.BPF_MAP_DELETE_ELEM,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("unable to delete element: %s", err)
}
return nil
}
// LookupNextElement looks up the next element in mp using the given key.
// The next key and the value are stored in the nextKey and value parameter.
// Returns false at the end of the mp.
func (b *Module) LookupNextElement(mp *Map, key, nextKey, value unsafe.Pointer) (bool, error) {
uba := C.union_bpf_attr{}
C.next_bpf_elem(
C.int(mp.m.fd),
key,
nextKey,
unsafe.Pointer(&uba),
)
ret, _, err := syscall.Syscall(
C.__NR_bpf,
C.BPF_MAP_GET_NEXT_KEY,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if err == syscall.ENOENT {
return false, nil
}
if err != 0 {
return false, fmt.Errorf("unable to find next element: %s", err)
}
if ret != 0 {
return false, nil
}
if err := b.LookupElement(mp, nextKey, value); err != nil {
return false, err
}
return true, nil
}