BPF – in-kernel virtual machine
1
BPF in-kernel virtual machine 1 BPF is Berkeley Packet Filter - - PowerPoint PPT Presentation
BPF in-kernel virtual machine 1 BPF is Berkeley Packet Filter low level instruction set kernel infrastructure around it interpreter JITs maps helper functions Agenda status and new use cases architecture
1
struct bpf_map_def SEC("maps") my_hist_map = { .type = BPF_MAP_TYPE_ARRAY, .key_size = sizeof(u32), .value_size = sizeof(u64), .max_entries = 64, }; SEC("events/syscalls/sys_enter_write") int bpf_prog(struct bpf_context *ctx) { u64 write_size = ctx->arg3; u32 index = log2(write_size); u64 *value; value = bpf_map_lookup_elem(&my_hist_map, &index); if (value) __sync_fetch_and_add(value, 1); return 0; }
u64 data[64] = {}; u32 key; u64 value; for (key = 0; key < 64; key++) { bpf_lookup_elem(fd, &key, &value); data[key] = value; if (value && key > max_ind) max_ind = key; if (value > max_value) max_value = value; } printf("syscall write() stats\n");
syscall write() stats byte_size : count distribution 1 -> 1 : 9 |*************************** | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 2 |***** | 16 -> 31 : 0 | | 32 -> 63 : 10 |****************************** | 64 -> 127 : 12 |************************************* | 128 -> 255 : 1 |** | 256 -> 511 : 2 |***** |
41