github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/map_queue.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 = (*QueueMap)(nil) 11 12 // QueueMap is a specialized map type, it has no key type, only a value type. It works like any other FIFO queue. 13 type QueueMap struct { 14 AbstractMap 15 } 16 17 func (m *QueueMap) Load() error { 18 if m.Definition.Type != bpftypes.BPF_MAP_TYPE_QUEUE { 19 return fmt.Errorf("map type in definition must be BPF_MAP_TYPE_QUEUE when using an QueueMap") 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 *QueueMap) 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 // Enqueue enqueues a new value 47 func (m *QueueMap) Enqueue(value interface{}) error { 48 return m.set(nil, value, bpfsys.BPFMapElemAny) 49 } 50 51 // Peek peeks at the first value in the queue without removing it. 52 func (m *QueueMap) Peek(value interface{}) error { 53 return m.get(nil, value) 54 } 55 56 // Dequeue returns the value of the from of the queue, removing it in the process. 57 func (m *QueueMap) Dequeue(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 queue will consume all values. 63 func (m *QueueMap) Iterator() MapIterator { 64 return &lookupAndDeleteIterator{ 65 BPFMap: m, 66 } 67 }