bpf.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. BPF_MAP_TYPE_PROG_ARRAY,
  100. };
  101. enum bpf_prog_type {
  102. BPF_PROG_TYPE_UNSPEC,
  103. BPF_PROG_TYPE_SOCKET_FILTER,
  104. BPF_PROG_TYPE_KPROBE,
  105. BPF_PROG_TYPE_SCHED_CLS,
  106. BPF_PROG_TYPE_SCHED_ACT,
  107. };
  108. #define BPF_PSEUDO_MAP_FD 1
  109. /* flags for BPF_MAP_UPDATE_ELEM command */
  110. #define BPF_ANY 0 /* create new element or update existing */
  111. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  112. #define BPF_EXIST 2 /* update existing element */
  113. union bpf_attr {
  114. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  115. __u32 map_type; /* one of enum bpf_map_type */
  116. __u32 key_size; /* size of key in bytes */
  117. __u32 value_size; /* size of value in bytes */
  118. __u32 max_entries; /* max number of entries in a map */
  119. };
  120. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  121. __u32 map_fd;
  122. __aligned_u64 key;
  123. union {
  124. __aligned_u64 value;
  125. __aligned_u64 next_key;
  126. };
  127. __u64 flags;
  128. };
  129. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  130. __u32 prog_type; /* one of enum bpf_prog_type */
  131. __u32 insn_cnt;
  132. __aligned_u64 insns;
  133. __aligned_u64 license;
  134. __u32 log_level; /* verbosity level of verifier */
  135. __u32 log_size; /* size of user buffer */
  136. __aligned_u64 log_buf; /* user supplied buffer */
  137. __u32 kern_version; /* checked when prog_type=kprobe */
  138. };
  139. } __attribute__((aligned(8)));
  140. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  141. * function eBPF program intends to call
  142. */
  143. enum bpf_func_id {
  144. BPF_FUNC_unspec,
  145. BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */
  146. BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
  147. BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
  148. BPF_FUNC_probe_read, /* int bpf_probe_read(void *dst, int size, void *src) */
  149. BPF_FUNC_ktime_get_ns, /* u64 bpf_ktime_get_ns(void) */
  150. BPF_FUNC_trace_printk, /* int bpf_trace_printk(const char *fmt, int fmt_size, ...) */
  151. BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */
  152. BPF_FUNC_get_smp_processor_id, /* u32 raw_smp_processor_id(void) */
  153. /**
  154. * skb_store_bytes(skb, offset, from, len, flags) - store bytes into packet
  155. * @skb: pointer to skb
  156. * @offset: offset within packet from skb->mac_header
  157. * @from: pointer where to copy bytes from
  158. * @len: number of bytes to store into packet
  159. * @flags: bit 0 - if true, recompute skb->csum
  160. * other bits - reserved
  161. * Return: 0 on success
  162. */
  163. BPF_FUNC_skb_store_bytes,
  164. /**
  165. * l3_csum_replace(skb, offset, from, to, flags) - recompute IP checksum
  166. * @skb: pointer to skb
  167. * @offset: offset within packet where IP checksum is located
  168. * @from: old value of header field
  169. * @to: new value of header field
  170. * @flags: bits 0-3 - size of header field
  171. * other bits - reserved
  172. * Return: 0 on success
  173. */
  174. BPF_FUNC_l3_csum_replace,
  175. /**
  176. * l4_csum_replace(skb, offset, from, to, flags) - recompute TCP/UDP checksum
  177. * @skb: pointer to skb
  178. * @offset: offset within packet where TCP/UDP checksum is located
  179. * @from: old value of header field
  180. * @to: new value of header field
  181. * @flags: bits 0-3 - size of header field
  182. * bit 4 - is pseudo header
  183. * other bits - reserved
  184. * Return: 0 on success
  185. */
  186. BPF_FUNC_l4_csum_replace,
  187. /**
  188. * bpf_tail_call(ctx, prog_array_map, index) - jump into another BPF program
  189. * @ctx: context pointer passed to next program
  190. * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
  191. * @index: index inside array that selects specific program to run
  192. * Return: 0 on success
  193. */
  194. BPF_FUNC_tail_call,
  195. /**
  196. * bpf_clone_redirect(skb, ifindex, flags) - redirect to another netdev
  197. * @skb: pointer to skb
  198. * @ifindex: ifindex of the net device
  199. * @flags: bit 0 - if set, redirect to ingress instead of egress
  200. * other bits - reserved
  201. * Return: 0 on success
  202. */
  203. BPF_FUNC_clone_redirect,
  204. /**
  205. * u64 bpf_get_current_pid_tgid(void)
  206. * Return: current->tgid << 32 | current->pid
  207. */
  208. BPF_FUNC_get_current_pid_tgid,
  209. /**
  210. * u64 bpf_get_current_uid_gid(void)
  211. * Return: current_gid << 32 | current_uid
  212. */
  213. BPF_FUNC_get_current_uid_gid,
  214. /**
  215. * bpf_get_current_comm(char *buf, int size_of_buf)
  216. * stores current->comm into buf
  217. * Return: 0 on success
  218. */
  219. BPF_FUNC_get_current_comm,
  220. __BPF_FUNC_MAX_ID,
  221. };
  222. /* user accessible mirror of in-kernel sk_buff.
  223. * new fields can only be added to the end of this structure
  224. */
  225. struct __sk_buff {
  226. __u32 len;
  227. __u32 pkt_type;
  228. __u32 mark;
  229. __u32 queue_mapping;
  230. __u32 protocol;
  231. __u32 vlan_present;
  232. __u32 vlan_tci;
  233. __u32 vlan_proto;
  234. __u32 priority;
  235. __u32 ingress_ifindex;
  236. __u32 ifindex;
  237. __u32 tc_index;
  238. __u32 cb[5];
  239. };
  240. #endif /* _UAPI__LINUX_BPF_H__ */