-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_queue.go
67 lines (53 loc) · 1.72 KB
/
map_queue.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
package gobpfld
import (
"fmt"
"github.com/dylandreimerink/gobpfld/bpfsys"
"github.com/dylandreimerink/gobpfld/bpftypes"
)
var _ BPFMap = (*QueueMap)(nil)
// QueueMap is a specialized map type, it has no key type, only a value type. It works like any other FIFO queue.
type QueueMap struct {
AbstractMap
}
func (m *QueueMap) Load() error {
if m.Definition.Type != bpftypes.BPF_MAP_TYPE_QUEUE {
return fmt.Errorf("map type in definition must be BPF_MAP_TYPE_QUEUE when using an QueueMap")
}
err := m.load(nil)
if err != nil {
return err
}
err = mapRegister.add(m)
if err != nil {
return fmt.Errorf("map register: %w", err)
}
return nil
}
// Close closes the file descriptor associate with the map, this will cause the map to unload from the kernel
// if it is not still in use by a eBPF program, bpf FS, or a userspace program still holding a fd to the map.
func (m *QueueMap) Close() error {
err := mapRegister.delete(m)
if err != nil {
return fmt.Errorf("map register: %w", err)
}
return m.close()
}
// Enqueue enqueues a new value
func (m *QueueMap) Enqueue(value interface{}) error {
return m.set(nil, value, bpfsys.BPFMapElemAny)
}
// Peek peeks at the first value in the queue without removing it.
func (m *QueueMap) Peek(value interface{}) error {
return m.get(nil, value)
}
// Dequeue returns the value of the from of the queue, removing it in the process.
func (m *QueueMap) Dequeue(value interface{}) error {
return m.lookupAndDelete(nil, value, bpfsys.BPFMapElemAny)
}
// Iterator returns a map iterator which can be used to loop over all values of the map.
// Looping over the queue will consume all values.
func (m *QueueMap) Iterator() MapIterator {
return &lookupAndDeleteIterator{
BPFMap: m,
}
}