bpf.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
  56. struct bpf_lpm_trie_key {
  57. __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
  58. __u8 data[0]; /* Arbitrary size */
  59. };
  60. /* BPF syscall commands, see bpf(2) man-page for details. */
  61. enum bpf_cmd {
  62. BPF_MAP_CREATE,
  63. BPF_MAP_LOOKUP_ELEM,
  64. BPF_MAP_UPDATE_ELEM,
  65. BPF_MAP_DELETE_ELEM,
  66. BPF_MAP_GET_NEXT_KEY,
  67. BPF_PROG_LOAD,
  68. BPF_OBJ_PIN,
  69. BPF_OBJ_GET,
  70. BPF_PROG_ATTACH,
  71. BPF_PROG_DETACH,
  72. BPF_PROG_TEST_RUN,
  73. };
  74. enum bpf_map_type {
  75. BPF_MAP_TYPE_UNSPEC,
  76. BPF_MAP_TYPE_HASH,
  77. BPF_MAP_TYPE_ARRAY,
  78. BPF_MAP_TYPE_PROG_ARRAY,
  79. BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  80. BPF_MAP_TYPE_PERCPU_HASH,
  81. BPF_MAP_TYPE_PERCPU_ARRAY,
  82. BPF_MAP_TYPE_STACK_TRACE,
  83. BPF_MAP_TYPE_CGROUP_ARRAY,
  84. BPF_MAP_TYPE_LRU_HASH,
  85. BPF_MAP_TYPE_LRU_PERCPU_HASH,
  86. BPF_MAP_TYPE_LPM_TRIE,
  87. BPF_MAP_TYPE_ARRAY_OF_MAPS,
  88. BPF_MAP_TYPE_HASH_OF_MAPS,
  89. };
  90. enum bpf_prog_type {
  91. BPF_PROG_TYPE_UNSPEC,
  92. BPF_PROG_TYPE_SOCKET_FILTER,
  93. BPF_PROG_TYPE_KPROBE,
  94. BPF_PROG_TYPE_SCHED_CLS,
  95. BPF_PROG_TYPE_SCHED_ACT,
  96. BPF_PROG_TYPE_TRACEPOINT,
  97. BPF_PROG_TYPE_XDP,
  98. BPF_PROG_TYPE_PERF_EVENT,
  99. BPF_PROG_TYPE_CGROUP_SKB,
  100. BPF_PROG_TYPE_CGROUP_SOCK,
  101. BPF_PROG_TYPE_LWT_IN,
  102. BPF_PROG_TYPE_LWT_OUT,
  103. BPF_PROG_TYPE_LWT_XMIT,
  104. };
  105. enum bpf_attach_type {
  106. BPF_CGROUP_INET_INGRESS,
  107. BPF_CGROUP_INET_EGRESS,
  108. BPF_CGROUP_INET_SOCK_CREATE,
  109. __MAX_BPF_ATTACH_TYPE
  110. };
  111. #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
  112. /* If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
  113. * to the given target_fd cgroup the descendent cgroup will be able to
  114. * override effective bpf program that was inherited from this cgroup
  115. */
  116. #define BPF_F_ALLOW_OVERRIDE (1U << 0)
  117. /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
  118. * verifier will perform strict alignment checking as if the kernel
  119. * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
  120. * and NET_IP_ALIGN defined to 2.
  121. */
  122. #define BPF_F_STRICT_ALIGNMENT (1U << 0)
  123. #define BPF_PSEUDO_MAP_FD 1
  124. /* flags for BPF_MAP_UPDATE_ELEM command */
  125. #define BPF_ANY 0 /* create new element or update existing */
  126. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  127. #define BPF_EXIST 2 /* update existing element */
  128. #define BPF_F_NO_PREALLOC (1U << 0)
  129. /* Instead of having one common LRU list in the
  130. * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
  131. * which can scale and perform better.
  132. * Note, the LRU nodes (including free nodes) cannot be moved
  133. * across different LRU lists.
  134. */
  135. #define BPF_F_NO_COMMON_LRU (1U << 1)
  136. union bpf_attr {
  137. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  138. __u32 map_type; /* one of enum bpf_map_type */
  139. __u32 key_size; /* size of key in bytes */
  140. __u32 value_size; /* size of value in bytes */
  141. __u32 max_entries; /* max number of entries in a map */
  142. __u32 map_flags; /* prealloc or not */
  143. __u32 inner_map_fd; /* fd pointing to the inner map */
  144. };
  145. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  146. __u32 map_fd;
  147. __aligned_u64 key;
  148. union {
  149. __aligned_u64 value;
  150. __aligned_u64 next_key;
  151. };
  152. __u64 flags;
  153. };
  154. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  155. __u32 prog_type; /* one of enum bpf_prog_type */
  156. __u32 insn_cnt;
  157. __aligned_u64 insns;
  158. __aligned_u64 license;
  159. __u32 log_level; /* verbosity level of verifier */
  160. __u32 log_size; /* size of user buffer */
  161. __aligned_u64 log_buf; /* user supplied buffer */
  162. __u32 kern_version; /* checked when prog_type=kprobe */
  163. __u32 prog_flags;
  164. };
  165. struct { /* anonymous struct used by BPF_OBJ_* commands */
  166. __aligned_u64 pathname;
  167. __u32 bpf_fd;
  168. };
  169. struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
  170. __u32 target_fd; /* container object to attach to */
  171. __u32 attach_bpf_fd; /* eBPF program to attach */
  172. __u32 attach_type;
  173. __u32 attach_flags;
  174. };
  175. struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
  176. __u32 prog_fd;
  177. __u32 retval;
  178. __u32 data_size_in;
  179. __u32 data_size_out;
  180. __aligned_u64 data_in;
  181. __aligned_u64 data_out;
  182. __u32 repeat;
  183. __u32 duration;
  184. } test;
  185. } __attribute__((aligned(8)));
  186. /* BPF helper function descriptions:
  187. *
  188. * void *bpf_map_lookup_elem(&map, &key)
  189. * Return: Map value or NULL
  190. *
  191. * int bpf_map_update_elem(&map, &key, &value, flags)
  192. * Return: 0 on success or negative error
  193. *
  194. * int bpf_map_delete_elem(&map, &key)
  195. * Return: 0 on success or negative error
  196. *
  197. * int bpf_probe_read(void *dst, int size, void *src)
  198. * Return: 0 on success or negative error
  199. *
  200. * u64 bpf_ktime_get_ns(void)
  201. * Return: current ktime
  202. *
  203. * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
  204. * Return: length of buffer written or negative error
  205. *
  206. * u32 bpf_prandom_u32(void)
  207. * Return: random value
  208. *
  209. * u32 bpf_raw_smp_processor_id(void)
  210. * Return: SMP processor ID
  211. *
  212. * int bpf_skb_store_bytes(skb, offset, from, len, flags)
  213. * store bytes into packet
  214. * @skb: pointer to skb
  215. * @offset: offset within packet from skb->mac_header
  216. * @from: pointer where to copy bytes from
  217. * @len: number of bytes to store into packet
  218. * @flags: bit 0 - if true, recompute skb->csum
  219. * other bits - reserved
  220. * Return: 0 on success or negative error
  221. *
  222. * int bpf_l3_csum_replace(skb, offset, from, to, flags)
  223. * recompute IP checksum
  224. * @skb: pointer to skb
  225. * @offset: offset within packet where IP checksum is located
  226. * @from: old value of header field
  227. * @to: new value of header field
  228. * @flags: bits 0-3 - size of header field
  229. * other bits - reserved
  230. * Return: 0 on success or negative error
  231. *
  232. * int bpf_l4_csum_replace(skb, offset, from, to, flags)
  233. * recompute TCP/UDP checksum
  234. * @skb: pointer to skb
  235. * @offset: offset within packet where TCP/UDP checksum is located
  236. * @from: old value of header field
  237. * @to: new value of header field
  238. * @flags: bits 0-3 - size of header field
  239. * bit 4 - is pseudo header
  240. * other bits - reserved
  241. * Return: 0 on success or negative error
  242. *
  243. * int bpf_tail_call(ctx, prog_array_map, index)
  244. * jump into another BPF program
  245. * @ctx: context pointer passed to next program
  246. * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
  247. * @index: index inside array that selects specific program to run
  248. * Return: 0 on success or negative error
  249. *
  250. * int bpf_clone_redirect(skb, ifindex, flags)
  251. * redirect to another netdev
  252. * @skb: pointer to skb
  253. * @ifindex: ifindex of the net device
  254. * @flags: bit 0 - if set, redirect to ingress instead of egress
  255. * other bits - reserved
  256. * Return: 0 on success or negative error
  257. *
  258. * u64 bpf_get_current_pid_tgid(void)
  259. * Return: current->tgid << 32 | current->pid
  260. *
  261. * u64 bpf_get_current_uid_gid(void)
  262. * Return: current_gid << 32 | current_uid
  263. *
  264. * int bpf_get_current_comm(char *buf, int size_of_buf)
  265. * stores current->comm into buf
  266. * Return: 0 on success or negative error
  267. *
  268. * u32 bpf_get_cgroup_classid(skb)
  269. * retrieve a proc's classid
  270. * @skb: pointer to skb
  271. * Return: classid if != 0
  272. *
  273. * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
  274. * Return: 0 on success or negative error
  275. *
  276. * int bpf_skb_vlan_pop(skb)
  277. * Return: 0 on success or negative error
  278. *
  279. * int bpf_skb_get_tunnel_key(skb, key, size, flags)
  280. * int bpf_skb_set_tunnel_key(skb, key, size, flags)
  281. * retrieve or populate tunnel metadata
  282. * @skb: pointer to skb
  283. * @key: pointer to 'struct bpf_tunnel_key'
  284. * @size: size of 'struct bpf_tunnel_key'
  285. * @flags: room for future extensions
  286. * Return: 0 on success or negative error
  287. *
  288. * u64 bpf_perf_event_read(map, flags)
  289. * read perf event counter value
  290. * @map: pointer to perf_event_array map
  291. * @flags: index of event in the map or bitmask flags
  292. * Return: value of perf event counter read or error code
  293. *
  294. * int bpf_redirect(ifindex, flags)
  295. * redirect to another netdev
  296. * @ifindex: ifindex of the net device
  297. * @flags: bit 0 - if set, redirect to ingress instead of egress
  298. * other bits - reserved
  299. * Return: TC_ACT_REDIRECT
  300. *
  301. * u32 bpf_get_route_realm(skb)
  302. * retrieve a dst's tclassid
  303. * @skb: pointer to skb
  304. * Return: realm if != 0
  305. *
  306. * int bpf_perf_event_output(ctx, map, flags, data, size)
  307. * output perf raw sample
  308. * @ctx: struct pt_regs*
  309. * @map: pointer to perf_event_array map
  310. * @flags: index of event in the map or bitmask flags
  311. * @data: data on stack to be output as raw data
  312. * @size: size of data
  313. * Return: 0 on success or negative error
  314. *
  315. * int bpf_get_stackid(ctx, map, flags)
  316. * walk user or kernel stack and return id
  317. * @ctx: struct pt_regs*
  318. * @map: pointer to stack_trace map
  319. * @flags: bits 0-7 - numer of stack frames to skip
  320. * bit 8 - collect user stack instead of kernel
  321. * bit 9 - compare stacks by hash only
  322. * bit 10 - if two different stacks hash into the same stackid
  323. * discard old
  324. * other bits - reserved
  325. * Return: >= 0 stackid on success or negative error
  326. *
  327. * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
  328. * calculate csum diff
  329. * @from: raw from buffer
  330. * @from_size: length of from buffer
  331. * @to: raw to buffer
  332. * @to_size: length of to buffer
  333. * @seed: optional seed
  334. * Return: csum result or negative error code
  335. *
  336. * int bpf_skb_get_tunnel_opt(skb, opt, size)
  337. * retrieve tunnel options metadata
  338. * @skb: pointer to skb
  339. * @opt: pointer to raw tunnel option data
  340. * @size: size of @opt
  341. * Return: option size
  342. *
  343. * int bpf_skb_set_tunnel_opt(skb, opt, size)
  344. * populate tunnel options metadata
  345. * @skb: pointer to skb
  346. * @opt: pointer to raw tunnel option data
  347. * @size: size of @opt
  348. * Return: 0 on success or negative error
  349. *
  350. * int bpf_skb_change_proto(skb, proto, flags)
  351. * Change protocol of the skb. Currently supported is v4 -> v6,
  352. * v6 -> v4 transitions. The helper will also resize the skb. eBPF
  353. * program is expected to fill the new headers via skb_store_bytes
  354. * and lX_csum_replace.
  355. * @skb: pointer to skb
  356. * @proto: new skb->protocol type
  357. * @flags: reserved
  358. * Return: 0 on success or negative error
  359. *
  360. * int bpf_skb_change_type(skb, type)
  361. * Change packet type of skb.
  362. * @skb: pointer to skb
  363. * @type: new skb->pkt_type type
  364. * Return: 0 on success or negative error
  365. *
  366. * int bpf_skb_under_cgroup(skb, map, index)
  367. * Check cgroup2 membership of skb
  368. * @skb: pointer to skb
  369. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  370. * @index: index of the cgroup in the bpf_map
  371. * Return:
  372. * == 0 skb failed the cgroup2 descendant test
  373. * == 1 skb succeeded the cgroup2 descendant test
  374. * < 0 error
  375. *
  376. * u32 bpf_get_hash_recalc(skb)
  377. * Retrieve and possibly recalculate skb->hash.
  378. * @skb: pointer to skb
  379. * Return: hash
  380. *
  381. * u64 bpf_get_current_task(void)
  382. * Returns current task_struct
  383. * Return: current
  384. *
  385. * int bpf_probe_write_user(void *dst, void *src, int len)
  386. * safely attempt to write to a location
  387. * @dst: destination address in userspace
  388. * @src: source address on stack
  389. * @len: number of bytes to copy
  390. * Return: 0 on success or negative error
  391. *
  392. * int bpf_current_task_under_cgroup(map, index)
  393. * Check cgroup2 membership of current task
  394. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  395. * @index: index of the cgroup in the bpf_map
  396. * Return:
  397. * == 0 current failed the cgroup2 descendant test
  398. * == 1 current succeeded the cgroup2 descendant test
  399. * < 0 error
  400. *
  401. * int bpf_skb_change_tail(skb, len, flags)
  402. * The helper will resize the skb to the given new size, to be used f.e.
  403. * with control messages.
  404. * @skb: pointer to skb
  405. * @len: new skb length
  406. * @flags: reserved
  407. * Return: 0 on success or negative error
  408. *
  409. * int bpf_skb_pull_data(skb, len)
  410. * The helper will pull in non-linear data in case the skb is non-linear
  411. * and not all of len are part of the linear section. Only needed for
  412. * read/write with direct packet access.
  413. * @skb: pointer to skb
  414. * @len: len to make read/writeable
  415. * Return: 0 on success or negative error
  416. *
  417. * s64 bpf_csum_update(skb, csum)
  418. * Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
  419. * @skb: pointer to skb
  420. * @csum: csum to add
  421. * Return: csum on success or negative error
  422. *
  423. * void bpf_set_hash_invalid(skb)
  424. * Invalidate current skb->hash.
  425. * @skb: pointer to skb
  426. *
  427. * int bpf_get_numa_node_id()
  428. * Return: Id of current NUMA node.
  429. *
  430. * int bpf_skb_change_head()
  431. * Grows headroom of skb and adjusts MAC header offset accordingly.
  432. * Will extends/reallocae as required automatically.
  433. * May change skb data pointer and will thus invalidate any check
  434. * performed for direct packet access.
  435. * @skb: pointer to skb
  436. * @len: length of header to be pushed in front
  437. * @flags: Flags (unused for now)
  438. * Return: 0 on success or negative error
  439. *
  440. * int bpf_xdp_adjust_head(xdp_md, delta)
  441. * Adjust the xdp_md.data by delta
  442. * @xdp_md: pointer to xdp_md
  443. * @delta: An positive/negative integer to be added to xdp_md.data
  444. * Return: 0 on success or negative on error
  445. *
  446. * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
  447. * Copy a NUL terminated string from unsafe address. In case the string
  448. * length is smaller than size, the target is not padded with further NUL
  449. * bytes. In case the string length is larger than size, just count-1
  450. * bytes are copied and the last byte is set to NUL.
  451. * @dst: destination address
  452. * @size: maximum number of bytes to copy, including the trailing NUL
  453. * @unsafe_ptr: unsafe address
  454. * Return:
  455. * > 0 length of the string including the trailing NUL on success
  456. * < 0 error
  457. *
  458. * u64 bpf_get_socket_cookie(skb)
  459. * Get the cookie for the socket stored inside sk_buff.
  460. * @skb: pointer to skb
  461. * Return: 8 Bytes non-decreasing number on success or 0 if the socket
  462. * field is missing inside sk_buff
  463. *
  464. * u32 bpf_get_socket_uid(skb)
  465. * Get the owner uid of the socket stored inside sk_buff.
  466. * @skb: pointer to skb
  467. * Return: uid of the socket owner on success or overflowuid if failed.
  468. */
  469. #define __BPF_FUNC_MAPPER(FN) \
  470. FN(unspec), \
  471. FN(map_lookup_elem), \
  472. FN(map_update_elem), \
  473. FN(map_delete_elem), \
  474. FN(probe_read), \
  475. FN(ktime_get_ns), \
  476. FN(trace_printk), \
  477. FN(get_prandom_u32), \
  478. FN(get_smp_processor_id), \
  479. FN(skb_store_bytes), \
  480. FN(l3_csum_replace), \
  481. FN(l4_csum_replace), \
  482. FN(tail_call), \
  483. FN(clone_redirect), \
  484. FN(get_current_pid_tgid), \
  485. FN(get_current_uid_gid), \
  486. FN(get_current_comm), \
  487. FN(get_cgroup_classid), \
  488. FN(skb_vlan_push), \
  489. FN(skb_vlan_pop), \
  490. FN(skb_get_tunnel_key), \
  491. FN(skb_set_tunnel_key), \
  492. FN(perf_event_read), \
  493. FN(redirect), \
  494. FN(get_route_realm), \
  495. FN(perf_event_output), \
  496. FN(skb_load_bytes), \
  497. FN(get_stackid), \
  498. FN(csum_diff), \
  499. FN(skb_get_tunnel_opt), \
  500. FN(skb_set_tunnel_opt), \
  501. FN(skb_change_proto), \
  502. FN(skb_change_type), \
  503. FN(skb_under_cgroup), \
  504. FN(get_hash_recalc), \
  505. FN(get_current_task), \
  506. FN(probe_write_user), \
  507. FN(current_task_under_cgroup), \
  508. FN(skb_change_tail), \
  509. FN(skb_pull_data), \
  510. FN(csum_update), \
  511. FN(set_hash_invalid), \
  512. FN(get_numa_node_id), \
  513. FN(skb_change_head), \
  514. FN(xdp_adjust_head), \
  515. FN(probe_read_str), \
  516. FN(get_socket_cookie), \
  517. FN(get_socket_uid),
  518. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  519. * function eBPF program intends to call
  520. */
  521. #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
  522. enum bpf_func_id {
  523. __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
  524. __BPF_FUNC_MAX_ID,
  525. };
  526. #undef __BPF_ENUM_FN
  527. /* All flags used by eBPF helper functions, placed here. */
  528. /* BPF_FUNC_skb_store_bytes flags. */
  529. #define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
  530. #define BPF_F_INVALIDATE_HASH (1ULL << 1)
  531. /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
  532. * First 4 bits are for passing the header field size.
  533. */
  534. #define BPF_F_HDR_FIELD_MASK 0xfULL
  535. /* BPF_FUNC_l4_csum_replace flags. */
  536. #define BPF_F_PSEUDO_HDR (1ULL << 4)
  537. #define BPF_F_MARK_MANGLED_0 (1ULL << 5)
  538. #define BPF_F_MARK_ENFORCE (1ULL << 6)
  539. /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
  540. #define BPF_F_INGRESS (1ULL << 0)
  541. /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
  542. #define BPF_F_TUNINFO_IPV6 (1ULL << 0)
  543. /* BPF_FUNC_get_stackid flags. */
  544. #define BPF_F_SKIP_FIELD_MASK 0xffULL
  545. #define BPF_F_USER_STACK (1ULL << 8)
  546. #define BPF_F_FAST_STACK_CMP (1ULL << 9)
  547. #define BPF_F_REUSE_STACKID (1ULL << 10)
  548. /* BPF_FUNC_skb_set_tunnel_key flags. */
  549. #define BPF_F_ZERO_CSUM_TX (1ULL << 1)
  550. #define BPF_F_DONT_FRAGMENT (1ULL << 2)
  551. /* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
  552. #define BPF_F_INDEX_MASK 0xffffffffULL
  553. #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
  554. /* BPF_FUNC_perf_event_output for sk_buff input context. */
  555. #define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
  556. /* user accessible mirror of in-kernel sk_buff.
  557. * new fields can only be added to the end of this structure
  558. */
  559. struct __sk_buff {
  560. __u32 len;
  561. __u32 pkt_type;
  562. __u32 mark;
  563. __u32 queue_mapping;
  564. __u32 protocol;
  565. __u32 vlan_present;
  566. __u32 vlan_tci;
  567. __u32 vlan_proto;
  568. __u32 priority;
  569. __u32 ingress_ifindex;
  570. __u32 ifindex;
  571. __u32 tc_index;
  572. __u32 cb[5];
  573. __u32 hash;
  574. __u32 tc_classid;
  575. __u32 data;
  576. __u32 data_end;
  577. __u32 napi_id;
  578. };
  579. struct bpf_tunnel_key {
  580. __u32 tunnel_id;
  581. union {
  582. __u32 remote_ipv4;
  583. __u32 remote_ipv6[4];
  584. };
  585. __u8 tunnel_tos;
  586. __u8 tunnel_ttl;
  587. __u16 tunnel_ext;
  588. __u32 tunnel_label;
  589. };
  590. /* Generic BPF return codes which all BPF program types may support.
  591. * The values are binary compatible with their TC_ACT_* counter-part to
  592. * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
  593. * programs.
  594. *
  595. * XDP is handled seprately, see XDP_*.
  596. */
  597. enum bpf_ret_code {
  598. BPF_OK = 0,
  599. /* 1 reserved */
  600. BPF_DROP = 2,
  601. /* 3-6 reserved */
  602. BPF_REDIRECT = 7,
  603. /* >127 are reserved for prog type specific return codes */
  604. };
  605. struct bpf_sock {
  606. __u32 bound_dev_if;
  607. __u32 family;
  608. __u32 type;
  609. __u32 protocol;
  610. };
  611. #define XDP_PACKET_HEADROOM 256
  612. /* User return codes for XDP prog type.
  613. * A valid XDP program must return one of these defined values. All other
  614. * return codes are reserved for future use. Unknown return codes will result
  615. * in packet drop.
  616. */
  617. enum xdp_action {
  618. XDP_ABORTED = 0,
  619. XDP_DROP,
  620. XDP_PASS,
  621. XDP_TX,
  622. };
  623. /* user accessible metadata for XDP packet hook
  624. * new fields must be added to the end of this structure
  625. */
  626. struct xdp_md {
  627. __u32 data;
  628. __u32 data_end;
  629. };
  630. #endif /* _UAPI__LINUX_BPF_H__ */