怎麼開始

graph TD;
  subgraph user[User Space]
    bpf_prog[BPF Program]
    prog_bpf[prog.bpf Bytecode]
    app[Application]
    bpf_prog --> |"LLVM/clang"| prog_bpf
  end

  subgraph kernel[Kernel]
    subgraph eBPF
      bpf_byte[BPF Bytecode]
      jit[verifier + JIT]
      native[Native Code]
      prog_bpf --> |"BPF()"| bpf_byte
      bpf_byte --> jit
      jit --> native
    end

    bpf_maps[BPF Maps]
    kernel_funcs[Kernel Functions]
    native --> |Hooks| kernel_funcs
    eBPF <--> bpf_maps
    app --> |"BPF()"| bpf_maps
  end

一些短語

from eBPF

  • probes: 探針
    • kprobes: kernel space probes
    • uprobes: user space probes
    • kertprobes: kernel return probes
    • uretprobes: userspace return probes
    • perf event
    • socket filter
    • XDP
    • tracepoint
  • CO-RE: Compile Once Run Everywhere
  • hook point: 啟動點

from C code

  • __bpf_ntohs:Network To Host Short,將網路位元組順序 (Network Byte Order,為 Big-Endian) 轉換為主機位元組順序 (Host Byte Order,x86 通常為 Little-Endian)
    • htons:Host TO Network Short integer
    • htonlntohl 依此類推
  • ntop:Network to Presentation,將數字格式的 IP 位址轉換為字串格式
  • bpf_skb_load_bytes:從網路封包的 socket buffer (sk_buff) 中讀取資料
  • ETH_P_IP:frame type field in the Ethernet frame, where 0x0800 indicates IPv4.
  • IP_MF:“More Fragments” flag. If this flag is set to 1, it indicates that the packet is part of a fragmented sequence and more fragments are expected. 可參考:network-packet-structure

參考資料