github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/map_stack.go (about) 1 package gobpfld 2 3 import ( 4 "fmt" 5 6 "github.com/dylandreimerink/gobpfld/bpfsys" 7 "github.com/dylandreimerink/gobpfld/bpftypes" 8 ) 9 10 var _ BPFMap = (*StackMap)(nil) 11 12 // StackMap is a specialized map type, it has no key type, only a value type. It works like any other FILO stack. 13 type StackMap struct { 14 AbstractMap 15 } 16 17 func (m *StackMap) Load() error { 18 if m.Definition.Type != bpftypes.BPF_MAP_TYPE_STACK { 19 return fmt.Errorf("map type in definition must be BPF_MAP_TYPE_STACK when using an StackMap") 20 } 21 22 err := m.load(nil) 23 if err != nil { 24 return err 25 } 26 27 err = mapRegister.add(m) 28 if err != nil { 29 return fmt.Errorf("map register: %w", err) 30 } 31 32 return nil 33 } 34 35 // Close closes the file descriptor associate with the map, this will cause the map to unload from the kernel 36 // if it is not still in use by a eBPF program, bpf FS, or a userspace program still holding a fd to the map. 37 func (m *StackMap) Close() error { 38 err := mapRegister.delete(m) 39 if err != nil { 40 return fmt.Errorf("map register: %w", err) 41 } 42 43 return m.close() 44 } 45 46 // Push pushes a new value onto the stack 47 func (m *StackMap) Push(value interface{}) error { 48 return m.set(nil, value, bpfsys.BPFMapElemAny) 49 } 50 51 // Peek peeks at the value at the top of the stack without removing it. 52 func (m *StackMap) Peek(value interface{}) error { 53 return m.get(nil, value) 54 } 55 56 // Pop returns the top value of the stack, removing it in the process. 57 func (m *StackMap) Pop(value interface{}) error { 58 return m.lookupAndDelete(nil, value, bpfsys.BPFMapElemAny) 59 } 60 61 // Iterator returns a map iterator which can be used to loop over all values of the map. 62 // Looping over the stack will consume all values. 63 func (m *StackMap) Iterator() MapIterator { 64 return &lookupAndDeleteIterator{ 65 BPFMap: m, 66 } 67 }