bpf.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <linux/bpf_common.h>
  11. /* Extended instruction set based on top of classic BPF */
  12. /* instruction classes */
  13. #define BPF_ALU64 0x07 /* alu mode in double word width */
  14. /* ld/ldx fields */
  15. #define BPF_DW 0x18 /* double word */
  16. #define BPF_XADD 0xc0 /* exclusive add */
  17. /* alu/jmp fields */
  18. #define BPF_MOV 0xb0 /* mov reg to reg */
  19. #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
  20. /* change endianness of a register */
  21. #define BPF_END 0xd0 /* flags for endianness conversion: */
  22. #define BPF_TO_LE 0x00 /* convert to little-endian */
  23. #define BPF_TO_BE 0x08 /* convert to big-endian */
  24. #define BPF_FROM_LE BPF_TO_LE
  25. #define BPF_FROM_BE BPF_TO_BE
  26. #define BPF_JNE 0x50 /* jump != */
  27. #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
  28. #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
  29. #define BPF_CALL 0x80 /* function call */
  30. #define BPF_EXIT 0x90 /* function return */
  31. /* Register numbers */
  32. enum {
  33. BPF_REG_0 = 0,
  34. BPF_REG_1,
  35. BPF_REG_2,
  36. BPF_REG_3,
  37. BPF_REG_4,
  38. BPF_REG_5,
  39. BPF_REG_6,
  40. BPF_REG_7,
  41. BPF_REG_8,
  42. BPF_REG_9,
  43. BPF_REG_10,
  44. __MAX_BPF_REG,
  45. };
  46. /* BPF has 10 general purpose 64-bit registers and stack frame. */
  47. #define MAX_BPF_REG __MAX_BPF_REG
  48. struct bpf_insn {
  49. __u8 code; /* opcode */
  50. __u8 dst_reg:4; /* dest register */
  51. __u8 src_reg:4; /* source register */
  52. __s16 off; /* signed offset */
  53. __s32 imm; /* signed immediate constant */
  54. };
  55. /* BPF syscall commands */
  56. enum bpf_cmd {
  57. /* create a map with given type and attributes
  58. * fd = bpf(BPF_MAP_CREATE, union bpf_attr *, u32 size)
  59. * returns fd or negative error
  60. * map is deleted when fd is closed
  61. */
  62. BPF_MAP_CREATE,
  63. /* lookup key in a given map
  64. * err = bpf(BPF_MAP_LOOKUP_ELEM, union bpf_attr *attr, u32 size)
  65. * Using attr->map_fd, attr->key, attr->value
  66. * returns zero and stores found elem into value
  67. * or negative error
  68. */
  69. BPF_MAP_LOOKUP_ELEM,
  70. /* create or update key/value pair in a given map
  71. * err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size)
  72. * Using attr->map_fd, attr->key, attr->value, attr->flags
  73. * returns zero or negative error
  74. */
  75. BPF_MAP_UPDATE_ELEM,
  76. /* find and delete elem by key in a given map
  77. * err = bpf(BPF_MAP_DELETE_ELEM, union bpf_attr *attr, u32 size)
  78. * Using attr->map_fd, attr->key
  79. * returns zero or negative error
  80. */
  81. BPF_MAP_DELETE_ELEM,
  82. /* lookup key in a given map and return next key
  83. * err = bpf(BPF_MAP_GET_NEXT_KEY, union bpf_attr *attr, u32 size)
  84. * Using attr->map_fd, attr->key, attr->next_key
  85. * returns zero and stores next key or negative error
  86. */
  87. BPF_MAP_GET_NEXT_KEY,
  88. /* verify and load eBPF program
  89. * prog_fd = bpf(BPF_PROG_LOAD, union bpf_attr *attr, u32 size)
  90. * Using attr->prog_type, attr->insns, attr->license
  91. * returns fd or negative error
  92. */
  93. BPF_PROG_LOAD,
  94. };
  95. enum bpf_map_type {
  96. BPF_MAP_TYPE_UNSPEC,
  97. BPF_MAP_TYPE_HASH,
  98. BPF_MAP_TYPE_ARRAY,
  99. };
  100. enum bpf_prog_type {
  101. BPF_PROG_TYPE_UNSPEC,
  102. BPF_PROG_TYPE_SOCKET_FILTER,
  103. BPF_PROG_TYPE_SCHED_CLS,
  104. };
  105. #define BPF_PSEUDO_MAP_FD 1
  106. /* flags for BPF_MAP_UPDATE_ELEM command */
  107. #define BPF_ANY 0 /* create new element or update existing */
  108. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  109. #define BPF_EXIST 2 /* update existing element */
  110. union bpf_attr {
  111. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  112. __u32 map_type; /* one of enum bpf_map_type */
  113. __u32 key_size; /* size of key in bytes */
  114. __u32 value_size; /* size of value in bytes */
  115. __u32 max_entries; /* max number of entries in a map */
  116. };
  117. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  118. __u32 map_fd;
  119. __aligned_u64 key;
  120. union {
  121. __aligned_u64 value;
  122. __aligned_u64 next_key;
  123. };
  124. __u64 flags;
  125. };
  126. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  127. __u32 prog_type; /* one of enum bpf_prog_type */
  128. __u32 insn_cnt;
  129. __aligned_u64 insns;
  130. __aligned_u64 license;
  131. __u32 log_level; /* verbosity level of verifier */
  132. __u32 log_size; /* size of user buffer */
  133. __aligned_u64 log_buf; /* user supplied buffer */
  134. };
  135. } __attribute__((aligned(8)));
  136. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  137. * function eBPF program intends to call
  138. */
  139. enum bpf_func_id {
  140. BPF_FUNC_unspec,
  141. BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */
  142. BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
  143. BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
  144. BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */
  145. BPF_FUNC_get_smp_processor_id, /* u32 raw_smp_processor_id(void) */
  146. __BPF_FUNC_MAX_ID,
  147. };
  148. /* user accessible mirror of in-kernel sk_buff.
  149. * new fields can only be added to the end of this structure
  150. */
  151. struct __sk_buff {
  152. __u32 len;
  153. __u32 pkt_type;
  154. __u32 mark;
  155. __u32 queue_mapping;
  156. };
  157. #endif /* _UAPI__LINUX_BPF_H__ */