| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import array |
| 5 |
import ctypes |
| 6 |
import os |
| 7 |
import re |
| 8 |
import sys |
| 9 |
from typing import NamedTuple, Tuple |
| 10 |
|
| 11 |
from _drgn_util.platform import SYS |
| 12 |
from tests.linux_kernel import _check_ctypes_syscall, _syscall |
| 13 |
|
| 14 |
# enum bpf_cmd |
| 15 |
BPF_MAP_CREATE = 0 |
| 16 |
BPF_MAP_LOOKUP_ELEM = 1 |
| 17 |
BPF_MAP_UPDATE_ELEM = 2 |
| 18 |
BPF_MAP_DELETE_ELEM = 3 |
| 19 |
BPF_MAP_GET_NEXT_KEY = 4 |
| 20 |
BPF_PROG_LOAD = 5 |
| 21 |
BPF_OBJ_PIN = 6 |
| 22 |
BPF_OBJ_GET = 7 |
| 23 |
BPF_PROG_ATTACH = 8 |
| 24 |
BPF_PROG_DETACH = 9 |
| 25 |
BPF_PROG_TEST_RUN = 10 |
| 26 |
BPF_PROG_RUN = BPF_PROG_TEST_RUN |
| 27 |
BPF_PROG_GET_NEXT_ID = 11 |
| 28 |
BPF_MAP_GET_NEXT_ID = 12 |
| 29 |
BPF_PROG_GET_FD_BY_ID = 13 |
| 30 |
BPF_MAP_GET_FD_BY_ID = 14 |
| 31 |
BPF_OBJ_GET_INFO_BY_FD = 15 |
| 32 |
BPF_PROG_QUERY = 16 |
| 33 |
BPF_RAW_TRACEPOINT_OPEN = 17 |
| 34 |
BPF_BTF_LOAD = 18 |
| 35 |
BPF_BTF_GET_FD_BY_ID = 19 |
| 36 |
BPF_TASK_FD_QUERY = 20 |
| 37 |
BPF_MAP_LOOKUP_AND_DELETE_ELEM = 21 |
| 38 |
BPF_MAP_FREEZE = 22 |
| 39 |
BPF_BTF_GET_NEXT_ID = 23 |
| 40 |
BPF_MAP_LOOKUP_BATCH = 24 |
| 41 |
BPF_MAP_LOOKUP_AND_DELETE_BATCH = 25 |
| 42 |
BPF_MAP_UPDATE_BATCH = 26 |
| 43 |
BPF_MAP_DELETE_BATCH = 27 |
| 44 |
BPF_LINK_CREATE = 28 |
| 45 |
BPF_LINK_UPDATE = 29 |
| 46 |
BPF_LINK_GET_FD_BY_ID = 30 |
| 47 |
BPF_LINK_GET_NEXT_ID = 31 |
| 48 |
BPF_ENABLE_STATS = 32 |
| 49 |
BPF_ITER_CREATE = 33 |
| 50 |
BPF_LINK_DETACH = 34 |
| 51 |
BPF_PROG_BIND_MAP = 35 |
| 52 |
|
| 53 |
# enum bpf_map_type |
| 54 |
BPF_MAP_TYPE_UNSPEC = 0 |
| 55 |
BPF_MAP_TYPE_HASH = 1 |
| 56 |
BPF_MAP_TYPE_ARRAY = 2 |
| 57 |
BPF_MAP_TYPE_PROG_ARRAY = 3 |
| 58 |
BPF_MAP_TYPE_PERF_EVENT_ARRAY = 4 |
| 59 |
BPF_MAP_TYPE_PERCPU_HASH = 5 |
| 60 |
BPF_MAP_TYPE_PERCPU_ARRAY = 6 |
| 61 |
BPF_MAP_TYPE_STACK_TRACE = 7 |
| 62 |
BPF_MAP_TYPE_CGROUP_ARRAY = 8 |
| 63 |
BPF_MAP_TYPE_LRU_HASH = 9 |
| 64 |
BPF_MAP_TYPE_LRU_PERCPU_HASH = 10 |
| 65 |
BPF_MAP_TYPE_LPM_TRIE = 11 |
| 66 |
BPF_MAP_TYPE_ARRAY_OF_MAPS = 12 |
| 67 |
BPF_MAP_TYPE_HASH_OF_MAPS = 13 |
| 68 |
BPF_MAP_TYPE_DEVMAP = 14 |
| 69 |
BPF_MAP_TYPE_SOCKMAP = 15 |
| 70 |
BPF_MAP_TYPE_CPUMAP = 16 |
| 71 |
BPF_MAP_TYPE_XSKMAP = 17 |
| 72 |
BPF_MAP_TYPE_SOCKHASH = 18 |
| 73 |
BPF_MAP_TYPE_CGROUP_STORAGE = 19 |
| 74 |
BPF_MAP_TYPE_REUSEPORT_SOCKARRAY = 20 |
| 75 |
BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE = 21 |
| 76 |
BPF_MAP_TYPE_QUEUE = 22 |
| 77 |
BPF_MAP_TYPE_STACK = 23 |
| 78 |
BPF_MAP_TYPE_SK_STORAGE = 24 |
| 79 |
BPF_MAP_TYPE_DEVMAP_HASH = 25 |
| 80 |
BPF_MAP_TYPE_STRUCT_OPS = 26 |
| 81 |
BPF_MAP_TYPE_RINGBUF = 27 |
| 82 |
BPF_MAP_TYPE_INODE_STORAGE = 28 |
| 83 |
BPF_MAP_TYPE_TASK_STORAGE = 29 |
| 84 |
BPF_MAP_TYPE_BLOOM_FILTER = 30 |
| 85 |
|
| 86 |
# enum bpf_prog_type |
| 87 |
BPF_PROG_TYPE_UNSPEC = 0 |
| 88 |
BPF_PROG_TYPE_SOCKET_FILTER = 1 |
| 89 |
BPF_PROG_TYPE_KPROBE = 2 |
| 90 |
BPF_PROG_TYPE_SCHED_CLS = 3 |
| 91 |
BPF_PROG_TYPE_SCHED_ACT = 4 |
| 92 |
BPF_PROG_TYPE_TRACEPOINT = 5 |
| 93 |
BPF_PROG_TYPE_XDP = 6 |
| 94 |
BPF_PROG_TYPE_PERF_EVENT = 7 |
| 95 |
BPF_PROG_TYPE_CGROUP_SKB = 8 |
| 96 |
BPF_PROG_TYPE_CGROUP_SOCK = 9 |
| 97 |
BPF_PROG_TYPE_LWT_IN = 10 |
| 98 |
BPF_PROG_TYPE_LWT_OUT = 11 |
| 99 |
BPF_PROG_TYPE_LWT_XMIT = 12 |
| 100 |
BPF_PROG_TYPE_SOCK_OPS = 13 |
| 101 |
BPF_PROG_TYPE_SK_SKB = 14 |
| 102 |
BPF_PROG_TYPE_CGROUP_DEVICE = 15 |
| 103 |
BPF_PROG_TYPE_SK_MSG = 16 |
| 104 |
BPF_PROG_TYPE_RAW_TRACEPOINT = 17 |
| 105 |
BPF_PROG_TYPE_CGROUP_SOCK_ADDR = 18 |
| 106 |
BPF_PROG_TYPE_LWT_SEG6LOCAL = 19 |
| 107 |
BPF_PROG_TYPE_LIRC_MODE2 = 20 |
| 108 |
BPF_PROG_TYPE_SK_REUSEPORT = 21 |
| 109 |
BPF_PROG_TYPE_FLOW_DISSECTOR = 22 |
| 110 |
BPF_PROG_TYPE_CGROUP_SYSCTL = 23 |
| 111 |
BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE = 24 |
| 112 |
BPF_PROG_TYPE_CGROUP_SOCKOPT = 25 |
| 113 |
BPF_PROG_TYPE_TRACING = 26 |
| 114 |
BPF_PROG_TYPE_STRUCT_OPS = 27 |
| 115 |
BPF_PROG_TYPE_EXT = 28 |
| 116 |
BPF_PROG_TYPE_LSM = 29 |
| 117 |
BPF_PROG_TYPE_SK_LOOKUP = 30 |
| 118 |
BPF_PROG_TYPE_SYSCALL = 31 |
| 119 |
|
| 120 |
# enum bpf_attach_type |
| 121 |
BPF_CGROUP_INET_INGRESS = 0 |
| 122 |
BPF_CGROUP_INET_EGRESS = 1 |
| 123 |
BPF_CGROUP_INET_SOCK_CREATE = 2 |
| 124 |
BPF_CGROUP_SOCK_OPS = 3 |
| 125 |
BPF_SK_SKB_STREAM_PARSER = 4 |
| 126 |
BPF_SK_SKB_STREAM_VERDICT = 5 |
| 127 |
BPF_CGROUP_DEVICE = 6 |
| 128 |
BPF_SK_MSG_VERDICT = 7 |
| 129 |
BPF_CGROUP_INET4_BIND = 8 |
| 130 |
BPF_CGROUP_INET6_BIND = 9 |
| 131 |
BPF_CGROUP_INET4_CONNECT = 10 |
| 132 |
BPF_CGROUP_INET6_CONNECT = 11 |
| 133 |
BPF_CGROUP_INET4_POST_BIND = 12 |
| 134 |
BPF_CGROUP_INET6_POST_BIND = 13 |
| 135 |
BPF_CGROUP_UDP4_SENDMSG = 14 |
| 136 |
BPF_CGROUP_UDP6_SENDMSG = 15 |
| 137 |
BPF_LIRC_MODE2 = 16 |
| 138 |
BPF_FLOW_DISSECTOR = 17 |
| 139 |
BPF_CGROUP_SYSCTL = 18 |
| 140 |
BPF_CGROUP_UDP4_RECVMSG = 19 |
| 141 |
BPF_CGROUP_UDP6_RECVMSG = 20 |
| 142 |
BPF_CGROUP_GETSOCKOPT = 21 |
| 143 |
BPF_CGROUP_SETSOCKOPT = 22 |
| 144 |
BPF_TRACE_RAW_TP = 23 |
| 145 |
BPF_TRACE_FENTRY = 24 |
| 146 |
BPF_TRACE_FEXIT = 25 |
| 147 |
BPF_MODIFY_RETURN = 26 |
| 148 |
BPF_LSM_MAC = 27 |
| 149 |
BPF_TRACE_ITER = 28 |
| 150 |
BPF_CGROUP_INET4_GETPEERNAME = 29 |
| 151 |
BPF_CGROUP_INET6_GETPEERNAME = 30 |
| 152 |
BPF_CGROUP_INET4_GETSOCKNAME = 31 |
| 153 |
BPF_CGROUP_INET6_GETSOCKNAME = 32 |
| 154 |
BPF_XDP_DEVMAP = 33 |
| 155 |
BPF_CGROUP_INET_SOCK_RELEASE = 34 |
| 156 |
BPF_XDP_CPUMAP = 35 |
| 157 |
BPF_SK_LOOKUP = 36 |
| 158 |
BPF_XDP = 37 |
| 159 |
BPF_SK_SKB_VERDICT = 38 |
| 160 |
BPF_SK_REUSEPORT_SELECT = 39 |
| 161 |
BPF_SK_REUSEPORT_SELECT_OR_MIGRATE = 40 |
| 162 |
BPF_PERF_EVENT = 41 |
| 163 |
|
| 164 |
# Flags for BPF_PROG_ATTACH. |
| 165 |
BPF_F_ALLOW_OVERRIDE = 1 << 0 |
| 166 |
BPF_F_ALLOW_MULTI = 1 << 1 |
| 167 |
BPF_F_REPLACE = 1 << 2 |
| 168 |
|
| 169 |
|
| 170 |
# Instruction classes. |
| 171 |
BPF_LD = 0x00 |
| 172 |
BPF_LDX = 0x01 |
| 173 |
BPF_ST = 0x02 |
| 174 |
BPF_STX = 0x03 |
| 175 |
BPF_ALU = 0x04 |
| 176 |
BPF_JMP = 0x05 |
| 177 |
BPF_RET = 0x06 |
| 178 |
BPF_JMP32 = 0x06 |
| 179 |
BPF_MISC = 0x07 |
| 180 |
BPF_ALU64 = 0x07 |
| 181 |
|
| 182 |
# ld/ldx fields. |
| 183 |
BPF_W = 0x00 |
| 184 |
BPF_H = 0x08 |
| 185 |
BPF_B = 0x10 |
| 186 |
BPF_DW = 0x18 |
| 187 |
BPF_IMM = 0x00 |
| 188 |
BPF_ABS = 0x20 |
| 189 |
BPF_IND = 0x40 |
| 190 |
BPF_MEM = 0x60 |
| 191 |
BPF_LEN = 0x80 |
| 192 |
BPF_MSH = 0xA0 |
| 193 |
BPF_MEMSX = 0x80 |
| 194 |
BPF_ATOMIC = 0xC0 |
| 195 |
BPF_XADD = 0xC0 |
| 196 |
|
| 197 |
# alu/jmp fields. |
| 198 |
BPF_ADD = 0x00 |
| 199 |
BPF_SUB = 0x10 |
| 200 |
BPF_MUL = 0x20 |
| 201 |
BPF_DIV = 0x30 |
| 202 |
BPF_OR = 0x40 |
| 203 |
BPF_AND = 0x50 |
| 204 |
BPF_LSH = 0x60 |
| 205 |
BPF_RSH = 0x70 |
| 206 |
BPF_NEG = 0x80 |
| 207 |
BPF_MOD = 0x90 |
| 208 |
BPF_XOR = 0xA0 |
| 209 |
|
| 210 |
BPF_JA = 0x00 |
| 211 |
BPF_JEQ = 0x10 |
| 212 |
BPF_JGT = 0x20 |
| 213 |
BPF_JGE = 0x30 |
| 214 |
BPF_JSET = 0x40 |
| 215 |
BPF_K = 0x00 |
| 216 |
BPF_X = 0x08 |
| 217 |
|
| 218 |
BPF_MOV = 0xB0 |
| 219 |
BPF_ARSH = 0xC0 |
| 220 |
|
| 221 |
# Change endianness of a register. |
| 222 |
BPF_END = 0xD0 |
| 223 |
BPF_TO_LE = 0x00 |
| 224 |
BPF_TO_BE = 0x08 |
| 225 |
BPF_FROM_LE = BPF_TO_LE |
| 226 |
BPF_FROM_BE = BPF_TO_BE |
| 227 |
|
| 228 |
# jmp encodings. |
| 229 |
BPF_JNE = 0x50 |
| 230 |
BPF_JLT = 0xA0 |
| 231 |
BPF_JLE = 0xB0 |
| 232 |
BPF_JSGT = 0x60 |
| 233 |
BPF_JSGE = 0x70 |
| 234 |
BPF_JSLT = 0xC0 |
| 235 |
BPF_JSLE = 0xD0 |
| 236 |
BPF_JCOND = 0xE0 |
| 237 |
BPF_CALL = 0x80 |
| 238 |
BPF_EXIT = 0x90 |
| 239 |
|
| 240 |
# atomic op type fields. |
| 241 |
BPF_FETCH = 0x01 |
| 242 |
BPF_XCHG = 0xE0 | BPF_FETCH |
| 243 |
BPF_CMPXCHG = 0xF0 | BPF_FETCH |
| 244 |
|
| 245 |
BPF_LOAD_ACQ = 0x100 |
| 246 |
BPF_STORE_REL = 0x110 |
| 247 |
|
| 248 |
# Register numbers. |
| 249 |
BPF_REG_0 = 0 |
| 250 |
BPF_REG_1 = 1 |
| 251 |
BPF_REG_2 = 2 |
| 252 |
BPF_REG_3 = 3 |
| 253 |
BPF_REG_4 = 4 |
| 254 |
BPF_REG_5 = 5 |
| 255 |
BPF_REG_6 = 6 |
| 256 |
BPF_REG_7 = 7 |
| 257 |
BPF_REG_8 = 8 |
| 258 |
BPF_REG_9 = 9 |
| 259 |
BPF_REG_10 = 10 |
| 260 |
|
| 261 |
MAX_BPF_REG = 11 |
| 262 |
|
| 263 |
|
| 264 |
# Instruction encoding. |
| 265 |
def bpf_insn(*, code: int, dst_reg: int, src_reg: int, off: int, imm: int) -> int: |
| 266 |
if code < 0 or code > 255: |
| 267 |
raise ValueError(f"code {code} is out of range") |
| 268 |
if dst_reg < 0 or dst_reg > 15: |
| 269 |
raise ValueError(f"dst_reg {dst_reg} is out of range") |
| 270 |
if src_reg < 0 or src_reg > 15: |
| 271 |
raise ValueError(f"src_reg {src_reg} is out of range") |
| 272 |
if off < -32768 or off > 32767: |
| 273 |
raise ValueError(f"off {off} is out of range") |
| 274 |
# Allow signed and unsigned 32-bit values. |
| 275 |
if imm < -(2**31) or imm >= 2**32: |
| 276 |
raise ValueError(f"imm {imm} is out of range") |
| 277 |
if sys.byteorder == "little": |
| 278 |
return ( |
| 279 |
code |
| 280 |
| (dst_reg << 8) |
| 281 |
| (src_reg << 12) |
| 282 |
| ((off & 0xFFFF) << 16) |
| 283 |
| ((imm & 0xFFFFFFFF) << 32) |
| 284 |
) |
| 285 |
else: |
| 286 |
return ( |
| 287 |
(code << 56) |
| 288 |
| (dst_reg << 52) |
| 289 |
| (src_reg << 48) |
| 290 |
| ((off & 0xFFFF) << 32) |
| 291 |
| (imm & 0xFFFFFFFF) |
| 292 |
) |
| 293 |
|
| 294 |
|
| 295 |
# Instructions. |
| 296 |
def BPF_MOV64_IMM(dst: int, imm: int) -> int: |
| 297 |
return bpf_insn( |
| 298 |
code=BPF_ALU64 | BPF_MOV | BPF_K, |
| 299 |
dst_reg=dst, |
| 300 |
src_reg=0, |
| 301 |
off=0, |
| 302 |
imm=imm, |
| 303 |
) |
| 304 |
|
| 305 |
|
| 306 |
BPF_PSEUDO_MAP_FD = 1 |
| 307 |
BPF_PSEUDO_MAP_IDX = 5 |
| 308 |
BPF_PSEUDO_MAP_VALUE = 2 |
| 309 |
BPF_PSEUDO_MAP_IDX_VALUE = 6 |
| 310 |
BPF_PSEUDO_BTF_ID = 3 |
| 311 |
BPF_PSEUDO_FUNC = 4 |
| 312 |
|
| 313 |
|
| 314 |
def BPF_LD_IMM64_RAW(dst: int, src: int, imm: int) -> Tuple[int, int]: |
| 315 |
# Allow signed and unsigned 64-bit values. |
| 316 |
if imm < -(2**63) or imm >= 2**64: |
| 317 |
raise ValueError(f"imm {imm} is out of range") |
| 318 |
return ( |
| 319 |
bpf_insn( |
| 320 |
code=BPF_LD | BPF_DW | BPF_IMM, |
| 321 |
dst_reg=dst, |
| 322 |
src_reg=src, |
| 323 |
off=0, |
| 324 |
imm=imm & 0xFFFFFFFF, |
| 325 |
), |
| 326 |
bpf_insn(code=0, dst_reg=0, src_reg=0, off=0, imm=(imm >> 32) & 0xFFFFFFFF), |
| 327 |
) |
| 328 |
|
| 329 |
|
| 330 |
def BPF_LD_MAP_FD(dst: int, map_fd: int) -> int: |
| 331 |
return BPF_LD_IMM64_RAW(dst, BPF_PSEUDO_MAP_FD, map_fd) |
| 332 |
|
| 333 |
|
| 334 |
def BPF_EXIT_INSN() -> int: |
| 335 |
return bpf_insn( |
| 336 |
code=BPF_JMP | BPF_EXIT, |
| 337 |
dst_reg=0, |
| 338 |
src_reg=0, |
| 339 |
off=0, |
| 340 |
imm=0, |
| 341 |
) |
| 342 |
|
| 343 |
|
| 344 |
class _bpf_attr_map_create(ctypes.Structure): |
| 345 |
_fields_ = ( |
| 346 |
("map_type", ctypes.c_uint32), |
| 347 |
("key_size", ctypes.c_uint32), |
| 348 |
("value_size", ctypes.c_uint32), |
| 349 |
("max_entries", ctypes.c_uint32), |
| 350 |
) |
| 351 |
|
| 352 |
|
| 353 |
_BPF_OBJ_NAME_LEN = 16 |
| 354 |
|
| 355 |
|
| 356 |
class _bpf_attr_prog_load(ctypes.Structure): |
| 357 |
_fields_ = ( |
| 358 |
("prog_type", ctypes.c_uint32), |
| 359 |
("insn_cnt", ctypes.c_uint32), |
| 360 |
("insns", ctypes.c_uint64), |
| 361 |
("license", ctypes.c_uint64), |
| 362 |
("log_level", ctypes.c_uint32), |
| 363 |
("log_size", ctypes.c_uint32), |
| 364 |
("log_buf", ctypes.c_uint64), |
| 365 |
("kern_version", ctypes.c_uint32), |
| 366 |
("prog_flags", ctypes.c_uint32), |
| 367 |
("prog_name", ctypes.c_char * _BPF_OBJ_NAME_LEN), |
| 368 |
("prog_ifindex", ctypes.c_uint32), |
| 369 |
("expected_attach_type", ctypes.c_uint32), |
| 370 |
) |
| 371 |
|
| 372 |
|
| 373 |
class _bpf_attr_attach(ctypes.Structure): |
| 374 |
_fields_ = ( |
| 375 |
("target_fd", ctypes.c_uint32), |
| 376 |
("attach_bpf_fd", ctypes.c_uint32), |
| 377 |
("attach_type", ctypes.c_uint32), |
| 378 |
("attach_flags", ctypes.c_uint32), |
| 379 |
("replace_bpf_fd", ctypes.c_uint32), |
| 380 |
) |
| 381 |
|
| 382 |
|
| 383 |
class _bpf_attr_get_id(ctypes.Structure): |
| 384 |
_fields_ = ( |
| 385 |
("id", ctypes.c_uint32), |
| 386 |
("next_id", ctypes.c_uint32), |
| 387 |
("open_flags", ctypes.c_uint32), |
| 388 |
) |
| 389 |
|
| 390 |
|
| 391 |
class _bpf_attr_obj_get_info_by_fd(ctypes.Structure): |
| 392 |
_fields_ = ( |
| 393 |
("bpf_fd", ctypes.c_uint32), |
| 394 |
("info_len", ctypes.c_uint32), |
| 395 |
("info", ctypes.c_uint64), |
| 396 |
) |
| 397 |
|
| 398 |
|
| 399 |
class _bpf_attr_link_create(ctypes.Structure): |
| 400 |
_fields_ = ( |
| 401 |
("prog_fd", ctypes.c_uint32), |
| 402 |
("target_fd", ctypes.c_uint32), |
| 403 |
("attach_type", ctypes.c_uint32), |
| 404 |
("flags", ctypes.c_uint32), |
| 405 |
) |
| 406 |
|
| 407 |
|
| 408 |
class _bpf_attr(ctypes.Union): |
| 409 |
_fields_ = ( |
| 410 |
("_map_create", _bpf_attr_map_create), |
| 411 |
("_prog_load", _bpf_attr_prog_load), |
| 412 |
("_attach", _bpf_attr_attach), |
| 413 |
("_get_id", _bpf_attr_get_id), |
| 414 |
("_obj_get_info_by_fd", _bpf_attr_obj_get_info_by_fd), |
| 415 |
("link_create", _bpf_attr_link_create), |
| 416 |
) |
| 417 |
_anonymous_ = ( |
| 418 |
"_map_create", |
| 419 |
"_prog_load", |
| 420 |
"_attach", |
| 421 |
"_get_id", |
| 422 |
"_obj_get_info_by_fd", |
| 423 |
) |
| 424 |
|
| 425 |
|
| 426 |
try: |
| 427 |
_SYS_bpf = ctypes.c_long(SYS["bpf"]) |
| 428 |
except KeyError: |
| 429 |
_SYS_bpf = None |
| 430 |
_sizeof_bpf_attr = ctypes.c_uint(ctypes.sizeof(_bpf_attr)) |
| 431 |
|
| 432 |
|
| 433 |
def _bpf(cmd, attr): |
| 434 |
return _check_ctypes_syscall( |
| 435 |
_syscall(_SYS_bpf, ctypes.c_int(cmd), ctypes.byref(attr), _sizeof_bpf_attr) |
| 436 |
) |
| 437 |
|
| 438 |
|
| 439 |
def bpf_map_create(map_type, key_size, value_size, max_entries): |
| 440 |
attr = _bpf_attr() |
| 441 |
attr.map_type = map_type |
| 442 |
attr.key_size = key_size |
| 443 |
attr.value_size = value_size |
| 444 |
attr.max_entries = max_entries |
| 445 |
return _bpf(BPF_MAP_CREATE, attr) |
| 446 |
|
| 447 |
|
| 448 |
_LOG_BUF_SIZE = 65536 |
| 449 |
|
| 450 |
|
| 451 |
class BpfVerifierError(OSError): |
| 452 |
def __init__(self, *args, log) -> None: |
| 453 |
super().__init__(*args) |
| 454 |
self.log = log |
| 455 |
|
| 456 |
def __str__(self) -> str: |
| 457 |
return f"{super().__str__()}\n{self.log}" |
| 458 |
|
| 459 |
|
| 460 |
def bpf_prog_load(prog_type, insns, license, expected_attach_type=0): |
| 461 |
attr = _bpf_attr() |
| 462 |
attr.prog_type = prog_type |
| 463 |
insns_array = array.array("Q", insns) |
| 464 |
attr.insns, attr.insn_cnt = insns_array.buffer_info() |
| 465 |
ctypes_license = ctypes.c_char_p(license) |
| 466 |
attr.license = ctypes.cast(ctypes_license, ctypes.c_void_p).value |
| 467 |
|
| 468 |
attr.log_level = 1 |
| 469 |
attr.log_size = _LOG_BUF_SIZE |
| 470 |
log_buf = ctypes.create_string_buffer(_LOG_BUF_SIZE) |
| 471 |
attr.log_buf = ctypes.addressof(log_buf) |
| 472 |
|
| 473 |
if prog_type == BPF_PROG_TYPE_KPROBE: |
| 474 |
# Before Linux kernel commit 6c4fc209fcf9 ("bpf: remove useless version |
| 475 |
# check for prog load") (in v5.0), for BPF_PROG_TYPE_KPROBE, |
| 476 |
# kern_version must match the kernel's LINUX_VERSION_CODE. |
| 477 |
match = re.match(r"([0-9]+)\.([0-9]+)\.([0-9]+)", os.uname().release) |
| 478 |
if match: |
| 479 |
attr.kern_version = ( |
| 480 |
(int(match.group(1)) << 16) |
| 481 |
| (int(match.group(2)) << 8) |
| 482 |
| min(int(match.group(3)), 255) |
| 483 |
) |
| 484 |
|
| 485 |
attr.expected_attach_type = expected_attach_type |
| 486 |
|
| 487 |
ret = _syscall( |
| 488 |
_SYS_bpf, ctypes.c_int(BPF_PROG_LOAD), ctypes.byref(attr), _sizeof_bpf_attr |
| 489 |
) |
| 490 |
if ret == -1: |
| 491 |
errno = ctypes.get_errno() |
| 492 |
log = log_buf.value |
| 493 |
if log: |
| 494 |
raise BpfVerifierError(errno, os.strerror(errno), log=log.decode()) |
| 495 |
else: |
| 496 |
raise OSError(errno, os.strerror(errno)) |
| 497 |
return ret |
| 498 |
|
| 499 |
|
| 500 |
def bpf_prog_attach(target_fd, attach_bpf_fd, attach_type, attach_flags=0): |
| 501 |
attr = _bpf_attr() |
| 502 |
attr.target_fd = target_fd |
| 503 |
attr.attach_bpf_fd = attach_bpf_fd |
| 504 |
attr.attach_type = attach_type |
| 505 |
attr.attach_flags = attach_flags |
| 506 |
_bpf(BPF_PROG_ATTACH, attr) |
| 507 |
|
| 508 |
|
| 509 |
def _bpf_get_ids(cmd): |
| 510 |
attr = _bpf_attr() |
| 511 |
attr.id = 0 |
| 512 |
while True: |
| 513 |
try: |
| 514 |
_bpf(cmd, attr) |
| 515 |
except FileNotFoundError: |
| 516 |
break |
| 517 |
yield attr.next_id |
| 518 |
attr.id = attr.next_id |
| 519 |
|
| 520 |
|
| 521 |
def bpf_map_ids(): |
| 522 |
return _bpf_get_ids(BPF_MAP_GET_NEXT_ID) |
| 523 |
|
| 524 |
|
| 525 |
def bpf_prog_ids(): |
| 526 |
return _bpf_get_ids(BPF_PROG_GET_NEXT_ID) |
| 527 |
|
| 528 |
|
| 529 |
def bpf_btf_ids(): |
| 530 |
return _bpf_get_ids(BPF_BTF_GET_NEXT_ID) |
| 531 |
|
| 532 |
|
| 533 |
def bpf_link_ids(): |
| 534 |
return _bpf_get_ids(BPF_LINK_GET_NEXT_ID) |
| 535 |
|
| 536 |
|
| 537 |
_BPF_TAG_SIZE = 8 |
| 538 |
|
| 539 |
|
| 540 |
class _bpf_prog_info(ctypes.Structure): |
| 541 |
_fields_ = ( |
| 542 |
("type", ctypes.c_uint32), |
| 543 |
("id", ctypes.c_uint32), |
| 544 |
("tag", ctypes.c_uint8 * _BPF_TAG_SIZE), |
| 545 |
("jited_prog_len", ctypes.c_uint32), |
| 546 |
("xlated_prog_len", ctypes.c_uint32), |
| 547 |
("jited_prog_insns", ctypes.c_uint64), |
| 548 |
("xlated_prog_insns", ctypes.c_uint64), |
| 549 |
) |
| 550 |
|
| 551 |
|
| 552 |
class BpfProgInfo(NamedTuple): |
| 553 |
type: int |
| 554 |
id: int |
| 555 |
tag: bytes |
| 556 |
|
| 557 |
|
| 558 |
def bpf_prog_get_info_by_fd(bpf_fd): |
| 559 |
attr = _bpf_attr() |
| 560 |
attr.bpf_fd = bpf_fd |
| 561 |
info = _bpf_prog_info() |
| 562 |
attr.info_len = ctypes.sizeof(info) |
| 563 |
attr.info = ctypes.addressof(info) |
| 564 |
_bpf(BPF_OBJ_GET_INFO_BY_FD, attr) |
| 565 |
return BpfProgInfo(type=info.type, id=info.id, tag=bytes(info.tag)) |
| 566 |
|
| 567 |
|
| 568 |
class _bpf_map_info(ctypes.Structure): |
| 569 |
_fields_ = ( |
| 570 |
("type", ctypes.c_uint32), |
| 571 |
("id", ctypes.c_uint32), |
| 572 |
("key_size", ctypes.c_uint32), |
| 573 |
("value_size", ctypes.c_uint32), |
| 574 |
("max_entries", ctypes.c_uint32), |
| 575 |
("map_flags", ctypes.c_uint32), |
| 576 |
("name", ctypes.c_char * _BPF_OBJ_NAME_LEN), |
| 577 |
("ifindex", ctypes.c_uint32), |
| 578 |
("btf_vmlinux_value_type_id", ctypes.c_uint32), |
| 579 |
("netns_dev", ctypes.c_uint64), |
| 580 |
("netns_ino", ctypes.c_uint64), |
| 581 |
("btf_id", ctypes.c_uint32), |
| 582 |
("btf_key_type_id", ctypes.c_uint32), |
| 583 |
("btf_value_type_id", ctypes.c_uint32), |
| 584 |
("btf_vmlinux_id", ctypes.c_uint32), |
| 585 |
("map_extra", ctypes.c_uint64), |
| 586 |
("hash", ctypes.c_uint64), |
| 587 |
("hash_size", ctypes.c_uint32), |
| 588 |
) |
| 589 |
|
| 590 |
|
| 591 |
class BpfMapInfo(NamedTuple): |
| 592 |
type: int |
| 593 |
id: int |
| 594 |
key_size: int |
| 595 |
value_size: int |
| 596 |
max_entries: int |
| 597 |
map_flags: int |
| 598 |
name: bytes |
| 599 |
ifindex: int |
| 600 |
btf_vmlinux_value_type_id: int |
| 601 |
netns_dev: int |
| 602 |
netns_ino: int |
| 603 |
btf_id: int |
| 604 |
btf_key_type_id: int |
| 605 |
btf_value_type_id: int |
| 606 |
btf_vmlinux_id: int |
| 607 |
map_extra: int |
| 608 |
hash: int |
| 609 |
hash_size: int |
| 610 |
|
| 611 |
|
| 612 |
def bpf_map_get_info_by_fd(bpf_fd): |
| 613 |
attr = _bpf_attr() |
| 614 |
attr.bpf_fd = bpf_fd |
| 615 |
info = _bpf_map_info() |
| 616 |
attr.info_len = ctypes.sizeof(info) |
| 617 |
attr.info = ctypes.addressof(info) |
| 618 |
_bpf(BPF_OBJ_GET_INFO_BY_FD, attr) |
| 619 |
return BpfMapInfo( |
| 620 |
type=info.type, |
| 621 |
id=info.id, |
| 622 |
key_size=info.key_size, |
| 623 |
value_size=info.value_size, |
| 624 |
max_entries=info.max_entries, |
| 625 |
map_flags=info.map_flags, |
| 626 |
name=info.name, |
| 627 |
ifindex=info.ifindex, |
| 628 |
btf_vmlinux_value_type_id=info.btf_vmlinux_value_type_id, |
| 629 |
netns_dev=info.netns_dev, |
| 630 |
netns_ino=info.netns_ino, |
| 631 |
btf_id=info.btf_id, |
| 632 |
btf_key_type_id=info.btf_key_type_id, |
| 633 |
btf_value_type_id=info.btf_value_type_id, |
| 634 |
btf_vmlinux_id=info.btf_vmlinux_id, |
| 635 |
map_extra=info.map_extra, |
| 636 |
hash=info.hash, |
| 637 |
hash_size=info.hash_size, |
| 638 |
) |
| 639 |
|
| 640 |
|
| 641 |
def bpf_link_create(prog_fd, target_fd, attach_type, flags=0): |
| 642 |
attr = _bpf_attr() |
| 643 |
attr.link_create.prog_fd = prog_fd |
| 644 |
attr.link_create.target_fd = target_fd |
| 645 |
attr.link_create.attach_type = attach_type |
| 646 |
attr.link_create.flags = flags |
| 647 |
return _bpf(BPF_LINK_CREATE, attr) |