linux-notes.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. .. contents::
  2. .. sectnum::
  3. ==========================
  4. Linux implementation notes
  5. ==========================
  6. This document provides more details specific to the Linux kernel implementation of the eBPF instruction set.
  7. Byte swap instructions
  8. ======================
  9. ``BPF_FROM_LE`` and ``BPF_FROM_BE`` exist as aliases for ``BPF_TO_LE`` and ``BPF_TO_BE`` respectively.
  10. Jump instructions
  11. =================
  12. ``BPF_CALL | BPF_X | BPF_JMP`` (0x8d), where the helper function
  13. integer would be read from a specified register, is not currently supported
  14. by the verifier. Any programs with this instruction will fail to load
  15. until such support is added.
  16. Maps
  17. ====
  18. Linux only supports the 'map_val(map)' operation on array maps with a single element.
  19. Linux uses an fd_array to store maps associated with a BPF program. Thus,
  20. map_by_idx(imm) uses the fd at that index in the array.
  21. Variables
  22. =========
  23. The following 64-bit immediate instruction specifies that a variable address,
  24. which corresponds to some integer stored in the 'imm' field, should be loaded:
  25. ========================= ====== === ========================================= =========== ==============
  26. opcode construction opcode src pseudocode imm type dst type
  27. ========================= ====== === ========================================= =========== ==============
  28. BPF_IMM | BPF_DW | BPF_LD 0x18 0x3 dst = var_addr(imm) variable id data pointer
  29. ========================= ====== === ========================================= =========== ==============
  30. On Linux, this integer is a BTF ID.
  31. Legacy BPF Packet access instructions
  32. =====================================
  33. As mentioned in the `ISA standard documentation
  34. <instruction-set.html#legacy-bpf-packet-access-instructions>`_,
  35. Linux has special eBPF instructions for access to packet data that have been
  36. carried over from classic BPF to retain the performance of legacy socket
  37. filters running in the eBPF interpreter.
  38. The instructions come in two forms: ``BPF_ABS | <size> | BPF_LD`` and
  39. ``BPF_IND | <size> | BPF_LD``.
  40. These instructions are used to access packet data and can only be used when
  41. the program context is a pointer to a networking packet. ``BPF_ABS``
  42. accesses packet data at an absolute offset specified by the immediate data
  43. and ``BPF_IND`` access packet data at an offset that includes the value of
  44. a register in addition to the immediate data.
  45. These instructions have seven implicit operands:
  46. * Register R6 is an implicit input that must contain a pointer to a
  47. struct sk_buff.
  48. * Register R0 is an implicit output which contains the data fetched from
  49. the packet.
  50. * Registers R1-R5 are scratch registers that are clobbered by the
  51. instruction.
  52. These instructions have an implicit program exit condition as well. If an
  53. eBPF program attempts access data beyond the packet boundary, the
  54. program execution will be aborted.
  55. ``BPF_ABS | BPF_W | BPF_LD`` (0x20) means::
  56. R0 = ntohl(*(u32 *) ((struct sk_buff *) R6->data + imm))
  57. where ``ntohl()`` converts a 32-bit value from network byte order to host byte order.
  58. ``BPF_IND | BPF_W | BPF_LD`` (0x40) means::
  59. R0 = ntohl(*(u32 *) ((struct sk_buff *) R6->data + src + imm))