|
| 1 | +package bpf |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "golang.org/x/sys/unix" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "runtime" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +func GetMapFDByPin(filename string) (MapFD, error) { |
| 13 | + fd, err := ObjGet(filename) |
| 14 | + return MapFD(fd), err |
| 15 | +} |
| 16 | + |
| 17 | +type MapInfo struct { |
| 18 | + Type int |
| 19 | + KeySize int |
| 20 | + ValueSize int |
| 21 | + MaxEntries int |
| 22 | +} |
| 23 | +type bpfAttrObjInfo struct { |
| 24 | + Fd uint32 |
| 25 | + InfoLen uint32 |
| 26 | + Info uint64 |
| 27 | +} |
| 28 | +type bpfMapInfo struct { |
| 29 | + MapType uint32 |
| 30 | + MapID uint32 |
| 31 | + SizeKey uint32 |
| 32 | + SizeValue uint32 |
| 33 | + MaxEntries uint32 |
| 34 | + Flags uint32 |
| 35 | +} |
| 36 | + |
| 37 | +// GetMapInfo INFO: https://github.com/cilium/cilium/blob/1.8.1/pkg/datapath/connector/ipvlan.go#L117-L136 根据 napID 获取 MapInfo |
| 38 | +func GetMapInfo(fd MapFD) (*MapInfo, error) { |
| 39 | + info := bpfMapInfo{} |
| 40 | + bpfAttrInfo := bpfAttrObjInfo{ |
| 41 | + Fd: uint32(fd), |
| 42 | + InfoLen: uint32(unsafe.Sizeof(info)), |
| 43 | + Info: uint64(uintptr(unsafe.Pointer(&info))), |
| 44 | + } |
| 45 | + bpfAttr2 := struct { |
| 46 | + info bpfAttrObjInfo |
| 47 | + }{ |
| 48 | + info: bpfAttrInfo, |
| 49 | + } |
| 50 | + _, _, errno := unix.Syscall( |
| 51 | + unix.SYS_BPF, |
| 52 | + unix.BPF_OBJ_GET_INFO_BY_FD, |
| 53 | + uintptr(unsafe.Pointer(&bpfAttr2)), |
| 54 | + unsafe.Sizeof(bpfAttr2), |
| 55 | + ) |
| 56 | + if errno != 0 { |
| 57 | + return nil, errno |
| 58 | + } |
| 59 | + |
| 60 | + return &MapInfo{ |
| 61 | + Type: int(info.MapType), |
| 62 | + KeySize: int(info.SizeKey), |
| 63 | + ValueSize: int(info.SizeValue), |
| 64 | + MaxEntries: int(info.MaxEntries), |
| 65 | + }, nil |
| 66 | +} |
| 67 | + |
| 68 | +func UpdateMapEntry(fd MapFD, k, v []byte) error { |
| 69 | + return UpdateElement(int(fd), unsafe.Pointer(&k), unsafe.Pointer(&v), unix.BPF_ANY) |
| 70 | +} |
| 71 | + |
| 72 | +// This struct must be in sync with union bpf_attr's anonymous struct used by |
| 73 | +// BPF_OBJ_*_ commands |
| 74 | +type bpfAttrObjOp struct { |
| 75 | + pathname uint64 |
| 76 | + fd uint32 |
| 77 | + pad0 [4]byte |
| 78 | +} |
| 79 | + |
| 80 | +// ObjGet INFO: 根据 map filename 获取 bpf 虚拟文件系统 fd |
| 81 | +// @see https://github.com/cilium/cilium/blob/v1.11.6/pkg/bpf/bpf_linux.go#L320-L355 |
| 82 | +func ObjGet(pathname string) (int, error) { |
| 83 | + pathStr, err := unix.BytePtrFromString(pathname) |
| 84 | + if err != nil { |
| 85 | + return 0, fmt.Errorf("unable to convert pathname %q to byte pointer: %w", pathname, err) |
| 86 | + } |
| 87 | + bpfAttr := bpfAttrObjOp{ |
| 88 | + pathname: uint64(uintptr(unsafe.Pointer(pathStr))), |
| 89 | + } |
| 90 | + |
| 91 | + fd, _, errno := unix.Syscall( |
| 92 | + unix.SYS_BPF, |
| 93 | + unix.BPF_OBJ_GET, |
| 94 | + uintptr(unsafe.Pointer(&bpfAttr)), |
| 95 | + unsafe.Sizeof(bpfAttr), |
| 96 | + ) |
| 97 | + runtime.KeepAlive(pathStr) |
| 98 | + runtime.KeepAlive(&bpfAttr) |
| 99 | + |
| 100 | + if fd == 0 || errno != 0 { |
| 101 | + return 0, &os.PathError{ |
| 102 | + Op: "Unable to get object", |
| 103 | + Err: errno, |
| 104 | + Path: pathname, |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return int(fd), nil |
| 109 | +} |
| 110 | + |
| 111 | +type bpfAttrFdFromId struct { |
| 112 | + ID uint32 |
| 113 | + NextID uint32 |
| 114 | + Flags uint32 |
| 115 | +} |
| 116 | + |
| 117 | +// MapFdFromID INFO: 根据 mapID 获取 bpf 虚拟文件系统 fd |
| 118 | +// @see https://github.com/cilium/cilium/blob/v1.11.6/pkg/bpf/bpf_linux.go#L363-L389 |
| 119 | +func MapFdFromID(id int) (int, error) { |
| 120 | + bpfAttr := bpfAttrFdFromId{ |
| 121 | + ID: uint32(id), |
| 122 | + } |
| 123 | + fd, _, err := unix.Syscall( |
| 124 | + unix.SYS_BPF, |
| 125 | + unix.BPF_MAP_GET_FD_BY_ID, |
| 126 | + uintptr(unsafe.Pointer(&bpfAttr)), |
| 127 | + unsafe.Sizeof(bpfAttr), |
| 128 | + ) |
| 129 | + runtime.KeepAlive(&bpfAttr) |
| 130 | + |
| 131 | + if fd == 0 || err != 0 { |
| 132 | + return 0, fmt.Errorf("Unable to get object fd from id %d: %s", id, err) |
| 133 | + } |
| 134 | + |
| 135 | + return int(fd), nil |
| 136 | +} |
| 137 | + |
| 138 | +// This struct must be in sync with union bpf_attr's anonymous struct used by |
| 139 | +// BPF_MAP_*_ELEM commands |
| 140 | +type bpfAttrMapOpElem struct { |
| 141 | + mapFd uint32 |
| 142 | + pad0 [4]byte |
| 143 | + key uint64 |
| 144 | + value uint64 // union: value or next_key |
| 145 | + flags uint64 |
| 146 | +} |
| 147 | + |
| 148 | +// UpdateElement INFO: https://github.com/cilium/cilium/blob/v1.11.6/pkg/bpf/bpf_linux.go#L121-L139 |
| 149 | +func UpdateElement(fd int, key, value unsafe.Pointer, flags uint64) error { |
| 150 | + bpfAttr := bpfAttrMapOpElem{ |
| 151 | + mapFd: uint32(fd), |
| 152 | + key: uint64(uintptr(key)), |
| 153 | + value: uint64(uintptr(value)), |
| 154 | + flags: uint64(flags), |
| 155 | + } |
| 156 | + |
| 157 | + ret := UpdateElementFromPointers(fd, unsafe.Pointer(&bpfAttr), unsafe.Sizeof(bpfAttr)) |
| 158 | + runtime.KeepAlive(key) |
| 159 | + runtime.KeepAlive(value) |
| 160 | + return ret |
| 161 | +} |
| 162 | + |
| 163 | +// UpdateElementFromPointers updates the map in fd with the given value in the given key. |
| 164 | +func UpdateElementFromPointers(fd int, structPtr unsafe.Pointer, sizeOfStruct uintptr) error { |
| 165 | + ret, _, err := unix.Syscall( |
| 166 | + unix.SYS_BPF, |
| 167 | + unix.BPF_MAP_UPDATE_ELEM, |
| 168 | + uintptr(structPtr), |
| 169 | + sizeOfStruct, |
| 170 | + ) |
| 171 | + runtime.KeepAlive(structPtr) |
| 172 | + if ret != 0 || err != 0 { |
| 173 | + return fmt.Errorf("Unable to update element for map with file descriptor %d: %s", fd, err) |
| 174 | + } |
| 175 | + |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +func GetMapEntry(fd MapFD, key []byte, valueSize int) ([]byte, error) { |
| 180 | + value := make([]byte, valueSize) |
| 181 | + err := LookupElement(int(fd), unsafe.Pointer(&key), unsafe.Pointer(&value)) |
| 182 | + if err != nil { |
| 183 | + return nil, err |
| 184 | + } |
| 185 | + |
| 186 | + return value, nil |
| 187 | +} |
| 188 | + |
| 189 | +// LookupElement INFO: 从 map fd 中查找 key 对应的 value |
| 190 | +func LookupElement(fd int, key, value unsafe.Pointer) error { |
| 191 | + uba := bpfAttrMapOpElem{ |
| 192 | + mapFd: uint32(fd), |
| 193 | + key: uint64(uintptr(key)), |
| 194 | + value: uint64(uintptr(value)), |
| 195 | + } |
| 196 | + |
| 197 | + ret := LookupElementFromPointers(fd, unsafe.Pointer(&uba), unsafe.Sizeof(uba)) |
| 198 | + runtime.KeepAlive(key) |
| 199 | + runtime.KeepAlive(value) |
| 200 | + return ret |
| 201 | +} |
| 202 | +func LookupElementFromPointers(fd int, structPtr unsafe.Pointer, sizeOfStruct uintptr) error { |
| 203 | + ret, _, err := unix.Syscall( |
| 204 | + unix.SYS_BPF, |
| 205 | + unix.BPF_MAP_LOOKUP_ELEM, |
| 206 | + uintptr(structPtr), |
| 207 | + sizeOfStruct, |
| 208 | + ) |
| 209 | + runtime.KeepAlive(structPtr) |
| 210 | + |
| 211 | + if ret != 0 || err != 0 { |
| 212 | + return fmt.Errorf("Unable to lookup element in map with file descriptor %d: %s", fd, err) |
| 213 | + } |
| 214 | + |
| 215 | + return nil |
| 216 | +} |
| 217 | + |
| 218 | +func DeleteMapEntry(mapFD MapFD, k []byte, valueSize int) error { |
| 219 | + return DeleteElement(int(mapFD), unsafe.Pointer(&k)) |
| 220 | +} |
| 221 | + |
| 222 | +// DeleteElement deletes the map element with the given key. |
| 223 | +func DeleteElement(fd int, key unsafe.Pointer) error { |
| 224 | + ret, err := deleteElement(fd, key) |
| 225 | + |
| 226 | + if ret != 0 || err != 0 { |
| 227 | + return fmt.Errorf("unable to delete element from map with file descriptor %d: %s", fd, err) |
| 228 | + } |
| 229 | + |
| 230 | + return nil |
| 231 | +} |
| 232 | +func deleteElement(fd int, key unsafe.Pointer) (uintptr, unix.Errno) { |
| 233 | + bpfAttr := bpfAttrMapOpElem{ |
| 234 | + mapFd: uint32(fd), |
| 235 | + key: uint64(uintptr(key)), |
| 236 | + } |
| 237 | + ret, _, err := unix.Syscall( |
| 238 | + unix.SYS_BPF, |
| 239 | + unix.BPF_MAP_DELETE_ELEM, |
| 240 | + uintptr(unsafe.Pointer(&bpfAttr)), |
| 241 | + unsafe.Sizeof(bpfAttr), |
| 242 | + ) |
| 243 | + runtime.KeepAlive(key) |
| 244 | + runtime.KeepAlive(&bpfAttr) |
| 245 | + |
| 246 | + return ret, err |
| 247 | +} |
| 248 | + |
| 249 | +// GetNextKeyFromPointers stores, in nextKey, the next key after the key of the |
| 250 | +// map in fd. When there are no more keys, io.EOF is returned. |
| 251 | +func GetNextKeyFromPointers(fd int, structPtr unsafe.Pointer, sizeOfStruct uintptr) error { |
| 252 | + ret, _, err := unix.Syscall( |
| 253 | + unix.SYS_BPF, |
| 254 | + unix.BPF_MAP_GET_NEXT_KEY, |
| 255 | + uintptr(structPtr), |
| 256 | + sizeOfStruct, |
| 257 | + ) |
| 258 | + runtime.KeepAlive(structPtr) |
| 259 | + |
| 260 | + // BPF_MAP_GET_NEXT_KEY returns ENOENT when all keys have been iterated |
| 261 | + // translate that to io.EOF to signify there are no next keys |
| 262 | + if err == unix.ENOENT { |
| 263 | + return io.EOF |
| 264 | + } |
| 265 | + |
| 266 | + if ret != 0 || err != 0 { |
| 267 | + return fmt.Errorf("unable to get next key from map with file descriptor %d: %s", fd, err) |
| 268 | + } |
| 269 | + |
| 270 | + return nil |
| 271 | +} |
| 272 | + |
| 273 | +// GetFirstKey fetches the first key in the map. If there are no keys in the |
| 274 | +// map, io.EOF is returned. |
| 275 | +func GetFirstKey(fd int, nextKey unsafe.Pointer) error { |
| 276 | + bpfAttr := bpfAttrMapOpElem{ |
| 277 | + mapFd: uint32(fd), |
| 278 | + key: 0, // NULL -> Get first element |
| 279 | + value: uint64(uintptr(nextKey)), |
| 280 | + } |
| 281 | + |
| 282 | + ret := GetNextKeyFromPointers(fd, unsafe.Pointer(&bpfAttr), unsafe.Sizeof(bpfAttr)) |
| 283 | + runtime.KeepAlive(nextKey) |
| 284 | + return ret |
| 285 | +} |
0 commit comments