bpf.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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, 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. BPF_MAP_TYPE_PERCPU_HASH,
  73. BPF_MAP_TYPE_PERCPU_ARRAY,
  74. BPF_MAP_TYPE_STACK_TRACE,
  75. BPF_MAP_TYPE_CGROUP_ARRAY,
  76. };
  77. enum bpf_prog_type {
  78. BPF_PROG_TYPE_UNSPEC,
  79. BPF_PROG_TYPE_SOCKET_FILTER,
  80. BPF_PROG_TYPE_KPROBE,
  81. BPF_PROG_TYPE_SCHED_CLS,
  82. BPF_PROG_TYPE_SCHED_ACT,
  83. BPF_PROG_TYPE_TRACEPOINT,
  84. BPF_PROG_TYPE_XDP,
  85. };
  86. #define BPF_PSEUDO_MAP_FD 1
  87. /* flags for BPF_MAP_UPDATE_ELEM command */
  88. #define BPF_ANY 0 /* create new element or update existing */
  89. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  90. #define BPF_EXIST 2 /* update existing element */
  91. #define BPF_F_NO_PREALLOC (1U << 0)
  92. union bpf_attr {
  93. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  94. __u32 map_type; /* one of enum bpf_map_type */
  95. __u32 key_size; /* size of key in bytes */
  96. __u32 value_size; /* size of value in bytes */
  97. __u32 max_entries; /* max number of entries in a map */
  98. __u32 map_flags; /* prealloc or not */
  99. };
  100. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  101. __u32 map_fd;
  102. __aligned_u64 key;
  103. union {
  104. __aligned_u64 value;
  105. __aligned_u64 next_key;
  106. };
  107. __u64 flags;
  108. };
  109. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  110. __u32 prog_type; /* one of enum bpf_prog_type */
  111. __u32 insn_cnt;
  112. __aligned_u64 insns;
  113. __aligned_u64 license;
  114. __u32 log_level; /* verbosity level of verifier */
  115. __u32 log_size; /* size of user buffer */
  116. __aligned_u64 log_buf; /* user supplied buffer */
  117. __u32 kern_version; /* checked when prog_type=kprobe */
  118. };
  119. struct { /* anonymous struct used by BPF_OBJ_* commands */
  120. __aligned_u64 pathname;
  121. __u32 bpf_fd;
  122. };
  123. } __attribute__((aligned(8)));
  124. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  125. * function eBPF program intends to call
  126. */
  127. enum bpf_func_id {
  128. BPF_FUNC_unspec,
  129. BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */
  130. BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
  131. BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
  132. BPF_FUNC_probe_read, /* int bpf_probe_read(void *dst, int size, void *src) */
  133. BPF_FUNC_ktime_get_ns, /* u64 bpf_ktime_get_ns(void) */
  134. BPF_FUNC_trace_printk, /* int bpf_trace_printk(const char *fmt, int fmt_size, ...) */
  135. BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */
  136. BPF_FUNC_get_smp_processor_id, /* u32 raw_smp_processor_id(void) */
  137. /**
  138. * skb_store_bytes(skb, offset, from, len, flags) - store bytes into packet
  139. * @skb: pointer to skb
  140. * @offset: offset within packet from skb->mac_header
  141. * @from: pointer where to copy bytes from
  142. * @len: number of bytes to store into packet
  143. * @flags: bit 0 - if true, recompute skb->csum
  144. * other bits - reserved
  145. * Return: 0 on success
  146. */
  147. BPF_FUNC_skb_store_bytes,
  148. /**
  149. * l3_csum_replace(skb, offset, from, to, flags) - recompute IP checksum
  150. * @skb: pointer to skb
  151. * @offset: offset within packet where IP checksum is located
  152. * @from: old value of header field
  153. * @to: new value of header field
  154. * @flags: bits 0-3 - size of header field
  155. * other bits - reserved
  156. * Return: 0 on success
  157. */
  158. BPF_FUNC_l3_csum_replace,
  159. /**
  160. * l4_csum_replace(skb, offset, from, to, flags) - recompute TCP/UDP checksum
  161. * @skb: pointer to skb
  162. * @offset: offset within packet where TCP/UDP checksum is located
  163. * @from: old value of header field
  164. * @to: new value of header field
  165. * @flags: bits 0-3 - size of header field
  166. * bit 4 - is pseudo header
  167. * other bits - reserved
  168. * Return: 0 on success
  169. */
  170. BPF_FUNC_l4_csum_replace,
  171. /**
  172. * bpf_tail_call(ctx, prog_array_map, index) - jump into another BPF program
  173. * @ctx: context pointer passed to next program
  174. * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
  175. * @index: index inside array that selects specific program to run
  176. * Return: 0 on success
  177. */
  178. BPF_FUNC_tail_call,
  179. /**
  180. * bpf_clone_redirect(skb, ifindex, flags) - redirect to another netdev
  181. * @skb: pointer to skb
  182. * @ifindex: ifindex of the net device
  183. * @flags: bit 0 - if set, redirect to ingress instead of egress
  184. * other bits - reserved
  185. * Return: 0 on success
  186. */
  187. BPF_FUNC_clone_redirect,
  188. /**
  189. * u64 bpf_get_current_pid_tgid(void)
  190. * Return: current->tgid << 32 | current->pid
  191. */
  192. BPF_FUNC_get_current_pid_tgid,
  193. /**
  194. * u64 bpf_get_current_uid_gid(void)
  195. * Return: current_gid << 32 | current_uid
  196. */
  197. BPF_FUNC_get_current_uid_gid,
  198. /**
  199. * bpf_get_current_comm(char *buf, int size_of_buf)
  200. * stores current->comm into buf
  201. * Return: 0 on success
  202. */
  203. BPF_FUNC_get_current_comm,
  204. /**
  205. * bpf_get_cgroup_classid(skb) - retrieve a proc's classid
  206. * @skb: pointer to skb
  207. * Return: classid if != 0
  208. */
  209. BPF_FUNC_get_cgroup_classid,
  210. BPF_FUNC_skb_vlan_push, /* bpf_skb_vlan_push(skb, vlan_proto, vlan_tci) */
  211. BPF_FUNC_skb_vlan_pop, /* bpf_skb_vlan_pop(skb) */
  212. /**
  213. * bpf_skb_[gs]et_tunnel_key(skb, key, size, flags)
  214. * retrieve or populate tunnel metadata
  215. * @skb: pointer to skb
  216. * @key: pointer to 'struct bpf_tunnel_key'
  217. * @size: size of 'struct bpf_tunnel_key'
  218. * @flags: room for future extensions
  219. * Retrun: 0 on success
  220. */
  221. BPF_FUNC_skb_get_tunnel_key,
  222. BPF_FUNC_skb_set_tunnel_key,
  223. BPF_FUNC_perf_event_read, /* u64 bpf_perf_event_read(&map, index) */
  224. /**
  225. * bpf_redirect(ifindex, flags) - redirect to another netdev
  226. * @ifindex: ifindex of the net device
  227. * @flags: bit 0 - if set, redirect to ingress instead of egress
  228. * other bits - reserved
  229. * Return: TC_ACT_REDIRECT
  230. */
  231. BPF_FUNC_redirect,
  232. /**
  233. * bpf_get_route_realm(skb) - retrieve a dst's tclassid
  234. * @skb: pointer to skb
  235. * Return: realm if != 0
  236. */
  237. BPF_FUNC_get_route_realm,
  238. /**
  239. * bpf_perf_event_output(ctx, map, index, data, size) - output perf raw sample
  240. * @ctx: struct pt_regs*
  241. * @map: pointer to perf_event_array map
  242. * @index: index of event in the map
  243. * @data: data on stack to be output as raw data
  244. * @size: size of data
  245. * Return: 0 on success
  246. */
  247. BPF_FUNC_perf_event_output,
  248. BPF_FUNC_skb_load_bytes,
  249. /**
  250. * bpf_get_stackid(ctx, map, flags) - walk user or kernel stack and return id
  251. * @ctx: struct pt_regs*
  252. * @map: pointer to stack_trace map
  253. * @flags: bits 0-7 - numer of stack frames to skip
  254. * bit 8 - collect user stack instead of kernel
  255. * bit 9 - compare stacks by hash only
  256. * bit 10 - if two different stacks hash into the same stackid
  257. * discard old
  258. * other bits - reserved
  259. * Return: >= 0 stackid on success or negative error
  260. */
  261. BPF_FUNC_get_stackid,
  262. /**
  263. * bpf_csum_diff(from, from_size, to, to_size, seed) - calculate csum diff
  264. * @from: raw from buffer
  265. * @from_size: length of from buffer
  266. * @to: raw to buffer
  267. * @to_size: length of to buffer
  268. * @seed: optional seed
  269. * Return: csum result
  270. */
  271. BPF_FUNC_csum_diff,
  272. /**
  273. * bpf_skb_[gs]et_tunnel_opt(skb, opt, size)
  274. * retrieve or populate tunnel options metadata
  275. * @skb: pointer to skb
  276. * @opt: pointer to raw tunnel option data
  277. * @size: size of @opt
  278. * Return: 0 on success for set, option size for get
  279. */
  280. BPF_FUNC_skb_get_tunnel_opt,
  281. BPF_FUNC_skb_set_tunnel_opt,
  282. /**
  283. * bpf_skb_change_proto(skb, proto, flags)
  284. * Change protocol of the skb. Currently supported is
  285. * v4 -> v6, v6 -> v4 transitions. The helper will also
  286. * resize the skb. eBPF program is expected to fill the
  287. * new headers via skb_store_bytes and lX_csum_replace.
  288. * @skb: pointer to skb
  289. * @proto: new skb->protocol type
  290. * @flags: reserved
  291. * Return: 0 on success or negative error
  292. */
  293. BPF_FUNC_skb_change_proto,
  294. /**
  295. * bpf_skb_change_type(skb, type)
  296. * Change packet type of skb.
  297. * @skb: pointer to skb
  298. * @type: new skb->pkt_type type
  299. * Return: 0 on success or negative error
  300. */
  301. BPF_FUNC_skb_change_type,
  302. /**
  303. * bpf_skb_under_cgroup(skb, map, index) - Check cgroup2 membership of skb
  304. * @skb: pointer to skb
  305. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  306. * @index: index of the cgroup in the bpf_map
  307. * Return:
  308. * == 0 skb failed the cgroup2 descendant test
  309. * == 1 skb succeeded the cgroup2 descendant test
  310. * < 0 error
  311. */
  312. BPF_FUNC_skb_under_cgroup,
  313. /**
  314. * bpf_get_hash_recalc(skb)
  315. * Retrieve and possibly recalculate skb->hash.
  316. * @skb: pointer to skb
  317. * Return: hash
  318. */
  319. BPF_FUNC_get_hash_recalc,
  320. /**
  321. * u64 bpf_get_current_task(void)
  322. * Returns current task_struct
  323. * Return: current
  324. */
  325. BPF_FUNC_get_current_task,
  326. /**
  327. * bpf_probe_write_user(void *dst, void *src, int len)
  328. * safely attempt to write to a location
  329. * @dst: destination address in userspace
  330. * @src: source address on stack
  331. * @len: number of bytes to copy
  332. * Return: 0 on success or negative error
  333. */
  334. BPF_FUNC_probe_write_user,
  335. __BPF_FUNC_MAX_ID,
  336. };
  337. /* All flags used by eBPF helper functions, placed here. */
  338. /* BPF_FUNC_skb_store_bytes flags. */
  339. #define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
  340. #define BPF_F_INVALIDATE_HASH (1ULL << 1)
  341. /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
  342. * First 4 bits are for passing the header field size.
  343. */
  344. #define BPF_F_HDR_FIELD_MASK 0xfULL
  345. /* BPF_FUNC_l4_csum_replace flags. */
  346. #define BPF_F_PSEUDO_HDR (1ULL << 4)
  347. #define BPF_F_MARK_MANGLED_0 (1ULL << 5)
  348. /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
  349. #define BPF_F_INGRESS (1ULL << 0)
  350. /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
  351. #define BPF_F_TUNINFO_IPV6 (1ULL << 0)
  352. /* BPF_FUNC_get_stackid flags. */
  353. #define BPF_F_SKIP_FIELD_MASK 0xffULL
  354. #define BPF_F_USER_STACK (1ULL << 8)
  355. #define BPF_F_FAST_STACK_CMP (1ULL << 9)
  356. #define BPF_F_REUSE_STACKID (1ULL << 10)
  357. /* BPF_FUNC_skb_set_tunnel_key flags. */
  358. #define BPF_F_ZERO_CSUM_TX (1ULL << 1)
  359. #define BPF_F_DONT_FRAGMENT (1ULL << 2)
  360. /* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
  361. #define BPF_F_INDEX_MASK 0xffffffffULL
  362. #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
  363. /* BPF_FUNC_perf_event_output for sk_buff input context. */
  364. #define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
  365. /* user accessible mirror of in-kernel sk_buff.
  366. * new fields can only be added to the end of this structure
  367. */
  368. struct __sk_buff {
  369. __u32 len;
  370. __u32 pkt_type;
  371. __u32 mark;
  372. __u32 queue_mapping;
  373. __u32 protocol;
  374. __u32 vlan_present;
  375. __u32 vlan_tci;
  376. __u32 vlan_proto;
  377. __u32 priority;
  378. __u32 ingress_ifindex;
  379. __u32 ifindex;
  380. __u32 tc_index;
  381. __u32 cb[5];
  382. __u32 hash;
  383. __u32 tc_classid;
  384. __u32 data;
  385. __u32 data_end;
  386. };
  387. struct bpf_tunnel_key {
  388. __u32 tunnel_id;
  389. union {
  390. __u32 remote_ipv4;
  391. __u32 remote_ipv6[4];
  392. };
  393. __u8 tunnel_tos;
  394. __u8 tunnel_ttl;
  395. __u16 tunnel_ext;
  396. __u32 tunnel_label;
  397. };
  398. /* User return codes for XDP prog type.
  399. * A valid XDP program must return one of these defined values. All other
  400. * return codes are reserved for future use. Unknown return codes will result
  401. * in packet drop.
  402. */
  403. enum xdp_action {
  404. XDP_ABORTED = 0,
  405. XDP_DROP,
  406. XDP_PASS,
  407. XDP_TX,
  408. };
  409. /* user accessible metadata for XDP packet hook
  410. * new fields must be added to the end of this structure
  411. */
  412. struct xdp_md {
  413. __u32 data;
  414. __u32 data_end;
  415. };
  416. #endif /* _UAPI__LINUX_BPF_H__ */