Line data Source code
1 : #ifndef HEADER_fd_src_waltz_ebpf_fd_linux_bpf_h 2 : #define HEADER_fd_src_waltz_ebpf_fd_linux_bpf_h 3 : 4 : /* fd_linux_bpf.h provides BPF syscall wrappers. */ 5 : 6 : #include "../../util/fd_util_base.h" 7 : 8 : #if defined(__linux__) && defined(_DEFAULT_SOURCE) || defined(_BSD_SOURCE) 9 : 10 : #include <sys/syscall.h> 11 : #include <unistd.h> 12 : #include <linux/bpf.h> 13 : 14 : /* bpf Linux syscall */ 15 : 16 : static inline long 17 : bpf( int cmd, 18 : union bpf_attr * attr, 19 3 : ulong attr_sz ) { 20 3 : return syscall( SYS_bpf, cmd, attr, attr_sz ); 21 3 : } 22 : 23 : /* fd_bpf_map_get_next_key wraps bpf(2) op BPF_MAP_GET_NEXT_KEY. 24 : 25 : Given a BPF map file descriptor and a const ptr to the current key, 26 : finds and stores the next key into `next_key`. key and next_key must 27 : match the key size of the map object. If key does not exist, yields 28 : the first key of the map. When mutating a map while iterating, get 29 : the next key before deleting the current key to avoid iterator from 30 : restarting. Returns 0 on success and -1 on failure (sets errno). 31 : Sets errno to ENOENT if given key is last in map. */ 32 : 33 : static inline int 34 : fd_bpf_map_get_next_key( int map_fd, 35 : void const * key, 36 0 : void * next_key ) { 37 0 : union bpf_attr attr = { 38 0 : .map_fd = (uint)map_fd, 39 0 : .key = (ulong)key, 40 0 : .next_key = (ulong)next_key 41 0 : }; 42 0 : return (int)bpf( BPF_MAP_GET_NEXT_KEY, &attr, sizeof(union bpf_attr) ); 43 0 : } 44 : 45 : /* fd_bpf_map_update_elem wraps bpf(2) op BPF_MAP_UPDATE_ELEM. 46 : 47 : Creates or updates an entry in a BPF map. key and value point to 48 : the tuple to be inserted and must match the key/value size of the map 49 : object. flags is one of BPF_ANY (create or update), BPF_NOEXIST 50 : (create only), BPF_EXIST (update only). Returns 0 on success and -1 51 : on failure (sets errno). Reasons for failure include: E2BIG (max 52 : entry limit reached), EEXIST (BPF_NOEXIST requested but key exists), 53 : ENOENT (BPF_EXIST requested but key not found). */ 54 : 55 : static inline int 56 : fd_bpf_map_update_elem( int map_fd, 57 : void const * key, 58 : void const * value, 59 0 : ulong flags ) { 60 0 : union bpf_attr attr = { 61 0 : .map_fd = (uint)map_fd, 62 0 : .key = (ulong)key, 63 0 : .value = (ulong)value, 64 0 : .flags = flags 65 0 : }; 66 0 : return (int)bpf( BPF_MAP_UPDATE_ELEM, &attr, sizeof(union bpf_attr) ); 67 0 : } 68 : 69 : /* fd_bpf_map_delete_elem wraps bpf(2) op BPF_MAP_DELETE_ELEM. 70 : 71 : Deletes an entry in a BPF map. key points to the key to be deleted 72 : and must match the key size of the map object. Returns 0 on success 73 : and -1 on failure (sets errno). Reasons for failure include: ENOENT 74 : (no such key). */ 75 : 76 : static inline int 77 : fd_bpf_map_delete_elem( int map_fd, 78 0 : void const * key ) { 79 0 : union bpf_attr attr = { 80 0 : .map_fd = (uint)map_fd, 81 0 : .key = (ulong)key 82 0 : }; 83 0 : return (int)bpf( BPF_MAP_DELETE_ELEM, &attr, sizeof(union bpf_attr) ); 84 0 : } 85 : 86 : /* fd_bpf_obj_get wraps bpf(2) op BPF_OBJ_GET. 87 : 88 : Opens a BPF map at given filesystem path. Path must be within a 89 : valid bpffs mount and point to a BPF map pinned via BPF_OBJ_PIN. 90 : Returns fd number on success and negative integer on failure. */ 91 : 92 : static inline int 93 0 : fd_bpf_obj_get( char const * pathname ) { 94 0 : union bpf_attr attr = { 95 0 : .pathname = (ulong)pathname 96 0 : }; 97 0 : return (int)bpf( BPF_OBJ_GET, &attr, sizeof(union bpf_attr) ); 98 0 : } 99 : 100 : /* fd_bpf_obj_pin wraps bpf(2) op BPF_OBJ_PIN. 101 : 102 : Pins a bpf syscall API object at given filesystem path. Types of 103 : objects include: BPF map (BPF_MAP_CREATE), links (BPF_LINK_CREATE), 104 : programs (BPF_PROG_LOAD). Returns 0 on success and -1 on failure. */ 105 : 106 : static inline int 107 : fd_bpf_obj_pin( int bpf_fd, 108 0 : char const * pathname ) { 109 0 : union bpf_attr attr = { 110 0 : .bpf_fd = (uint)bpf_fd, 111 0 : .pathname = (ulong)pathname 112 0 : }; 113 0 : return (int)bpf( BPF_OBJ_PIN, &attr, sizeof(union bpf_attr) ); 114 0 : } 115 : 116 : #endif /* defined (__linux__) */ 117 : 118 : #endif /* HEADER_fd_src_waltz_ebpf_fd_linux_bpf_h */