bpf.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 __LINUX_BPF_H__
  8. #define __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, see bpf(2) man-page for details. */
  56. enum bpf_cmd {
  57. BPF_MAP_CREATE,
  58. BPF_MAP_LOOKUP_ELEM,
  59. BPF_MAP_UPDATE_ELEM,
  60. BPF_MAP_DELETE_ELEM,
  61. BPF_MAP_GET_NEXT_KEY,
  62. BPF_PROG_LOAD,
  63. BPF_OBJ_PIN,
  64. BPF_OBJ_GET,
  65. };
  66. enum bpf_map_type {
  67. BPF_MAP_TYPE_UNSPEC,
  68. BPF_MAP_TYPE_HASH,
  69. BPF_MAP_TYPE_ARRAY,
  70. BPF_MAP_TYPE_PROG_ARRAY,
  71. BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  72. };
  73. enum bpf_prog_type {
  74. BPF_PROG_TYPE_UNSPEC,
  75. BPF_PROG_TYPE_SOCKET_FILTER,
  76. BPF_PROG_TYPE_KPROBE,
  77. BPF_PROG_TYPE_SCHED_CLS,
  78. BPF_PROG_TYPE_SCHED_ACT,
  79. };
  80. #define BPF_PSEUDO_MAP_FD 1
  81. /* flags for BPF_MAP_UPDATE_ELEM command */
  82. #define BPF_ANY 0 /* create new element or update existing */
  83. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  84. #define BPF_EXIST 2 /* update existing element */
  85. union bpf_attr {
  86. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  87. __u32 map_type; /* one of enum bpf_map_type */
  88. __u32 key_size; /* size of key in bytes */
  89. __u32 value_size; /* size of value in bytes */
  90. __u32 max_entries; /* max number of entries in a map */
  91. };
  92. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  93. __u32 map_fd;
  94. __aligned_u64 key;
  95. union {
  96. __aligned_u64 value;
  97. __aligned_u64 next_key;
  98. };
  99. __u64 flags;
  100. };
  101. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  102. __u32 prog_type; /* one of enum bpf_prog_type */
  103. __u32 insn_cnt;
  104. __aligned_u64 insns;
  105. __aligned_u64 license;
  106. __u32 log_level; /* verbosity level of verifier */
  107. __u32 log_size; /* size of user buffer */
  108. __aligned_u64 log_buf; /* user supplied buffer */
  109. __u32 kern_version; /* checked when prog_type=kprobe */
  110. };
  111. struct { /* anonymous struct used by BPF_OBJ_* commands */
  112. __aligned_u64 pathname;
  113. __u32 bpf_fd;
  114. };
  115. } __attribute__((aligned(8)));
  116. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  117. * function eBPF program intends to call
  118. */
  119. enum bpf_func_id {
  120. BPF_FUNC_unspec,
  121. BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */
  122. BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
  123. BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
  124. BPF_FUNC_probe_read, /* int bpf_probe_read(void *dst, int size, void *src) */
  125. BPF_FUNC_ktime_get_ns, /* u64 bpf_ktime_get_ns(void) */
  126. BPF_FUNC_trace_printk, /* int bpf_trace_printk(const char *fmt, int fmt_size, ...) */
  127. BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */
  128. BPF_FUNC_get_smp_processor_id, /* u32 raw_smp_processor_id(void) */
  129. /**
  130. * skb_store_bytes(skb, offset, from, len, flags) - store bytes into packet
  131. * @skb: pointer to skb
  132. * @offset: offset within packet from skb->mac_header
  133. * @from: pointer where to copy bytes from
  134. * @len: number of bytes to store into packet
  135. * @flags: bit 0 - if true, recompute skb->csum
  136. * other bits - reserved
  137. * Return: 0 on success
  138. */
  139. BPF_FUNC_skb_store_bytes,
  140. /**
  141. * l3_csum_replace(skb, offset, from, to, flags) - recompute IP checksum
  142. * @skb: pointer to skb
  143. * @offset: offset within packet where IP checksum is located
  144. * @from: old value of header field
  145. * @to: new value of header field
  146. * @flags: bits 0-3 - size of header field
  147. * other bits - reserved
  148. * Return: 0 on success
  149. */
  150. BPF_FUNC_l3_csum_replace,
  151. /**
  152. * l4_csum_replace(skb, offset, from, to, flags) - recompute TCP/UDP checksum
  153. * @skb: pointer to skb
  154. * @offset: offset within packet where TCP/UDP checksum is located
  155. * @from: old value of header field
  156. * @to: new value of header field
  157. * @flags: bits 0-3 - size of header field
  158. * bit 4 - is pseudo header
  159. * other bits - reserved
  160. * Return: 0 on success
  161. */
  162. BPF_FUNC_l4_csum_replace,
  163. /**
  164. * bpf_tail_call(ctx, prog_array_map, index) - jump into another BPF program
  165. * @ctx: context pointer passed to next program
  166. * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
  167. * @index: index inside array that selects specific program to run
  168. * Return: 0 on success
  169. */
  170. BPF_FUNC_tail_call,
  171. /**
  172. * bpf_clone_redirect(skb, ifindex, flags) - redirect to another netdev
  173. * @skb: pointer to skb
  174. * @ifindex: ifindex of the net device
  175. * @flags: bit 0 - if set, redirect to ingress instead of egress
  176. * other bits - reserved
  177. * Return: 0 on success
  178. */
  179. BPF_FUNC_clone_redirect,
  180. /**
  181. * u64 bpf_get_current_pid_tgid(void)
  182. * Return: current->tgid << 32 | current->pid
  183. */
  184. BPF_FUNC_get_current_pid_tgid,
  185. /**
  186. * u64 bpf_get_current_uid_gid(void)
  187. * Return: current_gid << 32 | current_uid
  188. */
  189. BPF_FUNC_get_current_uid_gid,
  190. /**
  191. * bpf_get_current_comm(char *buf, int size_of_buf)
  192. * stores current->comm into buf
  193. * Return: 0 on success
  194. */
  195. BPF_FUNC_get_current_comm,
  196. /**
  197. * bpf_get_cgroup_classid(skb) - retrieve a proc's classid
  198. * @skb: pointer to skb
  199. * Return: classid if != 0
  200. */
  201. BPF_FUNC_get_cgroup_classid,
  202. BPF_FUNC_skb_vlan_push, /* bpf_skb_vlan_push(skb, vlan_proto, vlan_tci) */
  203. BPF_FUNC_skb_vlan_pop, /* bpf_skb_vlan_pop(skb) */
  204. /**
  205. * bpf_skb_[gs]et_tunnel_key(skb, key, size, flags)
  206. * retrieve or populate tunnel metadata
  207. * @skb: pointer to skb
  208. * @key: pointer to 'struct bpf_tunnel_key'
  209. * @size: size of 'struct bpf_tunnel_key'
  210. * @flags: room for future extensions
  211. * Retrun: 0 on success
  212. */
  213. BPF_FUNC_skb_get_tunnel_key,
  214. BPF_FUNC_skb_set_tunnel_key,
  215. BPF_FUNC_perf_event_read, /* u64 bpf_perf_event_read(&map, index) */
  216. /**
  217. * bpf_redirect(ifindex, flags) - redirect to another netdev
  218. * @ifindex: ifindex of the net device
  219. * @flags: bit 0 - if set, redirect to ingress instead of egress
  220. * other bits - reserved
  221. * Return: TC_ACT_REDIRECT
  222. */
  223. BPF_FUNC_redirect,
  224. /**
  225. * bpf_get_route_realm(skb) - retrieve a dst's tclassid
  226. * @skb: pointer to skb
  227. * Return: realm if != 0
  228. */
  229. BPF_FUNC_get_route_realm,
  230. /**
  231. * bpf_perf_event_output(ctx, map, index, data, size) - output perf raw sample
  232. * @ctx: struct pt_regs*
  233. * @map: pointer to perf_event_array map
  234. * @index: index of event in the map
  235. * @data: data on stack to be output as raw data
  236. * @size: size of data
  237. * Return: 0 on success
  238. */
  239. BPF_FUNC_perf_event_output,
  240. __BPF_FUNC_MAX_ID,
  241. };
  242. /* user accessible mirror of in-kernel sk_buff.
  243. * new fields can only be added to the end of this structure
  244. */
  245. struct __sk_buff {
  246. __u32 len;
  247. __u32 pkt_type;
  248. __u32 mark;
  249. __u32 queue_mapping;
  250. __u32 protocol;
  251. __u32 vlan_present;
  252. __u32 vlan_tci;
  253. __u32 vlan_proto;
  254. __u32 priority;
  255. __u32 ingress_ifindex;
  256. __u32 ifindex;
  257. __u32 tc_index;
  258. __u32 cb[5];
  259. __u32 hash;
  260. __u32 tc_classid;
  261. };
  262. struct bpf_tunnel_key {
  263. __u32 tunnel_id;
  264. __u32 remote_ipv4;
  265. };
  266. #endif /* __LINUX_BPF_H__ */