bpf.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #ifndef _UAPI__LINUX_BPF_H__
  8. #define _UAPI__LINUX_BPF_H__
  9. #include <linux/types.h>
  10. /* Extended instruction set based on top of classic BPF */
  11. /* instruction classes */
  12. #define BPF_ALU64 0x07 /* alu mode in double word width */
  13. /* ld/ldx fields */
  14. #define BPF_DW 0x18 /* double word */
  15. #define BPF_XADD 0xc0 /* exclusive add */
  16. /* alu/jmp fields */
  17. #define BPF_MOV 0xb0 /* mov reg to reg */
  18. #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
  19. /* change endianness of a register */
  20. #define BPF_END 0xd0 /* flags for endianness conversion: */
  21. #define BPF_TO_LE 0x00 /* convert to little-endian */
  22. #define BPF_TO_BE 0x08 /* convert to big-endian */
  23. #define BPF_FROM_LE BPF_TO_LE
  24. #define BPF_FROM_BE BPF_TO_BE
  25. #define BPF_JNE 0x50 /* jump != */
  26. #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
  27. #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
  28. #define BPF_CALL 0x80 /* function call */
  29. #define BPF_EXIT 0x90 /* function return */
  30. /* Register numbers */
  31. enum {
  32. BPF_REG_0 = 0,
  33. BPF_REG_1,
  34. BPF_REG_2,
  35. BPF_REG_3,
  36. BPF_REG_4,
  37. BPF_REG_5,
  38. BPF_REG_6,
  39. BPF_REG_7,
  40. BPF_REG_8,
  41. BPF_REG_9,
  42. BPF_REG_10,
  43. __MAX_BPF_REG,
  44. };
  45. /* BPF has 10 general purpose 64-bit registers and stack frame. */
  46. #define MAX_BPF_REG __MAX_BPF_REG
  47. struct bpf_insn {
  48. __u8 code; /* opcode */
  49. __u8 dst_reg:4; /* dest register */
  50. __u8 src_reg:4; /* source register */
  51. __s16 off; /* signed offset */
  52. __s32 imm; /* signed immediate constant */
  53. };
  54. /* BPF syscall commands */
  55. enum bpf_cmd {
  56. /* create a map with given type and attributes
  57. * fd = bpf(BPF_MAP_CREATE, union bpf_attr *, u32 size)
  58. * returns fd or negative error
  59. * map is deleted when fd is closed
  60. */
  61. BPF_MAP_CREATE,
  62. /* lookup key in a given map
  63. * err = bpf(BPF_MAP_LOOKUP_ELEM, union bpf_attr *attr, u32 size)
  64. * Using attr->map_fd, attr->key, attr->value
  65. * returns zero and stores found elem into value
  66. * or negative error
  67. */
  68. BPF_MAP_LOOKUP_ELEM,
  69. /* create or update key/value pair in a given map
  70. * err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size)
  71. * Using attr->map_fd, attr->key, attr->value
  72. * returns zero or negative error
  73. */
  74. BPF_MAP_UPDATE_ELEM,
  75. /* find and delete elem by key in a given map
  76. * err = bpf(BPF_MAP_DELETE_ELEM, union bpf_attr *attr, u32 size)
  77. * Using attr->map_fd, attr->key
  78. * returns zero or negative error
  79. */
  80. BPF_MAP_DELETE_ELEM,
  81. /* lookup key in a given map and return next key
  82. * err = bpf(BPF_MAP_GET_NEXT_KEY, union bpf_attr *attr, u32 size)
  83. * Using attr->map_fd, attr->key, attr->next_key
  84. * returns zero and stores next key or negative error
  85. */
  86. BPF_MAP_GET_NEXT_KEY,
  87. /* verify and load eBPF program
  88. * prog_fd = bpf(BPF_PROG_LOAD, union bpf_attr *attr, u32 size)
  89. * Using attr->prog_type, attr->insns, attr->license
  90. * returns fd or negative error
  91. */
  92. BPF_PROG_LOAD,
  93. };
  94. enum bpf_map_type {
  95. BPF_MAP_TYPE_UNSPEC,
  96. };
  97. enum bpf_prog_type {
  98. BPF_PROG_TYPE_UNSPEC,
  99. };
  100. union bpf_attr {
  101. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  102. __u32 map_type; /* one of enum bpf_map_type */
  103. __u32 key_size; /* size of key in bytes */
  104. __u32 value_size; /* size of value in bytes */
  105. __u32 max_entries; /* max number of entries in a map */
  106. };
  107. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  108. __u32 map_fd;
  109. __aligned_u64 key;
  110. union {
  111. __aligned_u64 value;
  112. __aligned_u64 next_key;
  113. };
  114. };
  115. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  116. __u32 prog_type; /* one of enum bpf_prog_type */
  117. __u32 insn_cnt;
  118. __aligned_u64 insns;
  119. __aligned_u64 license;
  120. __u32 log_level; /* verbosity level of verifier */
  121. __u32 log_size; /* size of user buffer */
  122. __aligned_u64 log_buf; /* user supplied buffer */
  123. };
  124. } __attribute__((aligned(8)));
  125. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  126. * function eBPF program intends to call
  127. */
  128. enum bpf_func_id {
  129. BPF_FUNC_unspec,
  130. __BPF_FUNC_MAX_ID,
  131. };
  132. #endif /* _UAPI__LINUX_BPF_H__ */