bpf.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. */
  8. #ifndef _UAPI__LINUX_BPF_H__
  9. #define _UAPI__LINUX_BPF_H__
  10. #include <linux/types.h>
  11. #include <linux/bpf_common.h>
  12. /* Extended instruction set based on top of classic BPF */
  13. /* instruction classes */
  14. #define BPF_ALU64 0x07 /* alu mode in double word width */
  15. /* ld/ldx fields */
  16. #define BPF_DW 0x18 /* double word */
  17. #define BPF_XADD 0xc0 /* exclusive add */
  18. /* alu/jmp fields */
  19. #define BPF_MOV 0xb0 /* mov reg to reg */
  20. #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
  21. /* change endianness of a register */
  22. #define BPF_END 0xd0 /* flags for endianness conversion: */
  23. #define BPF_TO_LE 0x00 /* convert to little-endian */
  24. #define BPF_TO_BE 0x08 /* convert to big-endian */
  25. #define BPF_FROM_LE BPF_TO_LE
  26. #define BPF_FROM_BE BPF_TO_BE
  27. /* jmp encodings */
  28. #define BPF_JNE 0x50 /* jump != */
  29. #define BPF_JLT 0xa0 /* LT is unsigned, '<' */
  30. #define BPF_JLE 0xb0 /* LE is unsigned, '<=' */
  31. #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
  32. #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
  33. #define BPF_JSLT 0xc0 /* SLT is signed, '<' */
  34. #define BPF_JSLE 0xd0 /* SLE is signed, '<=' */
  35. #define BPF_CALL 0x80 /* function call */
  36. #define BPF_EXIT 0x90 /* function return */
  37. /* Register numbers */
  38. enum {
  39. BPF_REG_0 = 0,
  40. BPF_REG_1,
  41. BPF_REG_2,
  42. BPF_REG_3,
  43. BPF_REG_4,
  44. BPF_REG_5,
  45. BPF_REG_6,
  46. BPF_REG_7,
  47. BPF_REG_8,
  48. BPF_REG_9,
  49. BPF_REG_10,
  50. __MAX_BPF_REG,
  51. };
  52. /* BPF has 10 general purpose 64-bit registers and stack frame. */
  53. #define MAX_BPF_REG __MAX_BPF_REG
  54. struct bpf_insn {
  55. __u8 code; /* opcode */
  56. __u8 dst_reg:4; /* dest register */
  57. __u8 src_reg:4; /* source register */
  58. __s16 off; /* signed offset */
  59. __s32 imm; /* signed immediate constant */
  60. };
  61. /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
  62. struct bpf_lpm_trie_key {
  63. __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
  64. __u8 data[0]; /* Arbitrary size */
  65. };
  66. /* BPF syscall commands, see bpf(2) man-page for details. */
  67. enum bpf_cmd {
  68. BPF_MAP_CREATE,
  69. BPF_MAP_LOOKUP_ELEM,
  70. BPF_MAP_UPDATE_ELEM,
  71. BPF_MAP_DELETE_ELEM,
  72. BPF_MAP_GET_NEXT_KEY,
  73. BPF_PROG_LOAD,
  74. BPF_OBJ_PIN,
  75. BPF_OBJ_GET,
  76. BPF_PROG_ATTACH,
  77. BPF_PROG_DETACH,
  78. BPF_PROG_TEST_RUN,
  79. BPF_PROG_GET_NEXT_ID,
  80. BPF_MAP_GET_NEXT_ID,
  81. BPF_PROG_GET_FD_BY_ID,
  82. BPF_MAP_GET_FD_BY_ID,
  83. BPF_OBJ_GET_INFO_BY_FD,
  84. BPF_PROG_QUERY,
  85. };
  86. enum bpf_map_type {
  87. BPF_MAP_TYPE_UNSPEC,
  88. BPF_MAP_TYPE_HASH,
  89. BPF_MAP_TYPE_ARRAY,
  90. BPF_MAP_TYPE_PROG_ARRAY,
  91. BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  92. BPF_MAP_TYPE_PERCPU_HASH,
  93. BPF_MAP_TYPE_PERCPU_ARRAY,
  94. BPF_MAP_TYPE_STACK_TRACE,
  95. BPF_MAP_TYPE_CGROUP_ARRAY,
  96. BPF_MAP_TYPE_LRU_HASH,
  97. BPF_MAP_TYPE_LRU_PERCPU_HASH,
  98. BPF_MAP_TYPE_LPM_TRIE,
  99. BPF_MAP_TYPE_ARRAY_OF_MAPS,
  100. BPF_MAP_TYPE_HASH_OF_MAPS,
  101. BPF_MAP_TYPE_DEVMAP,
  102. BPF_MAP_TYPE_SOCKMAP,
  103. BPF_MAP_TYPE_CPUMAP,
  104. };
  105. enum bpf_prog_type {
  106. BPF_PROG_TYPE_UNSPEC,
  107. BPF_PROG_TYPE_SOCKET_FILTER,
  108. BPF_PROG_TYPE_KPROBE,
  109. BPF_PROG_TYPE_SCHED_CLS,
  110. BPF_PROG_TYPE_SCHED_ACT,
  111. BPF_PROG_TYPE_TRACEPOINT,
  112. BPF_PROG_TYPE_XDP,
  113. BPF_PROG_TYPE_PERF_EVENT,
  114. BPF_PROG_TYPE_CGROUP_SKB,
  115. BPF_PROG_TYPE_CGROUP_SOCK,
  116. BPF_PROG_TYPE_LWT_IN,
  117. BPF_PROG_TYPE_LWT_OUT,
  118. BPF_PROG_TYPE_LWT_XMIT,
  119. BPF_PROG_TYPE_SOCK_OPS,
  120. BPF_PROG_TYPE_SK_SKB,
  121. BPF_PROG_TYPE_CGROUP_DEVICE,
  122. };
  123. enum bpf_attach_type {
  124. BPF_CGROUP_INET_INGRESS,
  125. BPF_CGROUP_INET_EGRESS,
  126. BPF_CGROUP_INET_SOCK_CREATE,
  127. BPF_CGROUP_SOCK_OPS,
  128. BPF_SK_SKB_STREAM_PARSER,
  129. BPF_SK_SKB_STREAM_VERDICT,
  130. BPF_CGROUP_DEVICE,
  131. __MAX_BPF_ATTACH_TYPE
  132. };
  133. #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
  134. /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
  135. *
  136. * NONE(default): No further bpf programs allowed in the subtree.
  137. *
  138. * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program,
  139. * the program in this cgroup yields to sub-cgroup program.
  140. *
  141. * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program,
  142. * that cgroup program gets run in addition to the program in this cgroup.
  143. *
  144. * Only one program is allowed to be attached to a cgroup with
  145. * NONE or BPF_F_ALLOW_OVERRIDE flag.
  146. * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will
  147. * release old program and attach the new one. Attach flags has to match.
  148. *
  149. * Multiple programs are allowed to be attached to a cgroup with
  150. * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order
  151. * (those that were attached first, run first)
  152. * The programs of sub-cgroup are executed first, then programs of
  153. * this cgroup and then programs of parent cgroup.
  154. * When children program makes decision (like picking TCP CA or sock bind)
  155. * parent program has a chance to override it.
  156. *
  157. * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups.
  158. * A cgroup with NONE doesn't allow any programs in sub-cgroups.
  159. * Ex1:
  160. * cgrp1 (MULTI progs A, B) ->
  161. * cgrp2 (OVERRIDE prog C) ->
  162. * cgrp3 (MULTI prog D) ->
  163. * cgrp4 (OVERRIDE prog E) ->
  164. * cgrp5 (NONE prog F)
  165. * the event in cgrp5 triggers execution of F,D,A,B in that order.
  166. * if prog F is detached, the execution is E,D,A,B
  167. * if prog F and D are detached, the execution is E,A,B
  168. * if prog F, E and D are detached, the execution is C,A,B
  169. *
  170. * All eligible programs are executed regardless of return code from
  171. * earlier programs.
  172. */
  173. #define BPF_F_ALLOW_OVERRIDE (1U << 0)
  174. #define BPF_F_ALLOW_MULTI (1U << 1)
  175. /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
  176. * verifier will perform strict alignment checking as if the kernel
  177. * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
  178. * and NET_IP_ALIGN defined to 2.
  179. */
  180. #define BPF_F_STRICT_ALIGNMENT (1U << 0)
  181. /* when bpf_ldimm64->src_reg == BPF_PSEUDO_MAP_FD, bpf_ldimm64->imm == fd */
  182. #define BPF_PSEUDO_MAP_FD 1
  183. /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
  184. * offset to another bpf function
  185. */
  186. #define BPF_PSEUDO_CALL 1
  187. /* flags for BPF_MAP_UPDATE_ELEM command */
  188. #define BPF_ANY 0 /* create new element or update existing */
  189. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  190. #define BPF_EXIST 2 /* update existing element */
  191. /* flags for BPF_MAP_CREATE command */
  192. #define BPF_F_NO_PREALLOC (1U << 0)
  193. /* Instead of having one common LRU list in the
  194. * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
  195. * which can scale and perform better.
  196. * Note, the LRU nodes (including free nodes) cannot be moved
  197. * across different LRU lists.
  198. */
  199. #define BPF_F_NO_COMMON_LRU (1U << 1)
  200. /* Specify numa node during map creation */
  201. #define BPF_F_NUMA_NODE (1U << 2)
  202. /* flags for BPF_PROG_QUERY */
  203. #define BPF_F_QUERY_EFFECTIVE (1U << 0)
  204. #define BPF_OBJ_NAME_LEN 16U
  205. /* Flags for accessing BPF object */
  206. #define BPF_F_RDONLY (1U << 3)
  207. #define BPF_F_WRONLY (1U << 4)
  208. union bpf_attr {
  209. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  210. __u32 map_type; /* one of enum bpf_map_type */
  211. __u32 key_size; /* size of key in bytes */
  212. __u32 value_size; /* size of value in bytes */
  213. __u32 max_entries; /* max number of entries in a map */
  214. __u32 map_flags; /* BPF_MAP_CREATE related
  215. * flags defined above.
  216. */
  217. __u32 inner_map_fd; /* fd pointing to the inner map */
  218. __u32 numa_node; /* numa node (effective only if
  219. * BPF_F_NUMA_NODE is set).
  220. */
  221. char map_name[BPF_OBJ_NAME_LEN];
  222. };
  223. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  224. __u32 map_fd;
  225. __aligned_u64 key;
  226. union {
  227. __aligned_u64 value;
  228. __aligned_u64 next_key;
  229. };
  230. __u64 flags;
  231. };
  232. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  233. __u32 prog_type; /* one of enum bpf_prog_type */
  234. __u32 insn_cnt;
  235. __aligned_u64 insns;
  236. __aligned_u64 license;
  237. __u32 log_level; /* verbosity level of verifier */
  238. __u32 log_size; /* size of user buffer */
  239. __aligned_u64 log_buf; /* user supplied buffer */
  240. __u32 kern_version; /* checked when prog_type=kprobe */
  241. __u32 prog_flags;
  242. char prog_name[BPF_OBJ_NAME_LEN];
  243. __u32 prog_ifindex; /* ifindex of netdev to prep for */
  244. };
  245. struct { /* anonymous struct used by BPF_OBJ_* commands */
  246. __aligned_u64 pathname;
  247. __u32 bpf_fd;
  248. __u32 file_flags;
  249. };
  250. struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
  251. __u32 target_fd; /* container object to attach to */
  252. __u32 attach_bpf_fd; /* eBPF program to attach */
  253. __u32 attach_type;
  254. __u32 attach_flags;
  255. };
  256. struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
  257. __u32 prog_fd;
  258. __u32 retval;
  259. __u32 data_size_in;
  260. __u32 data_size_out;
  261. __aligned_u64 data_in;
  262. __aligned_u64 data_out;
  263. __u32 repeat;
  264. __u32 duration;
  265. } test;
  266. struct { /* anonymous struct used by BPF_*_GET_*_ID */
  267. union {
  268. __u32 start_id;
  269. __u32 prog_id;
  270. __u32 map_id;
  271. };
  272. __u32 next_id;
  273. __u32 open_flags;
  274. };
  275. struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
  276. __u32 bpf_fd;
  277. __u32 info_len;
  278. __aligned_u64 info;
  279. } info;
  280. struct { /* anonymous struct used by BPF_PROG_QUERY command */
  281. __u32 target_fd; /* container object to query */
  282. __u32 attach_type;
  283. __u32 query_flags;
  284. __u32 attach_flags;
  285. __aligned_u64 prog_ids;
  286. __u32 prog_cnt;
  287. } query;
  288. } __attribute__((aligned(8)));
  289. /* BPF helper function descriptions:
  290. *
  291. * void *bpf_map_lookup_elem(&map, &key)
  292. * Return: Map value or NULL
  293. *
  294. * int bpf_map_update_elem(&map, &key, &value, flags)
  295. * Return: 0 on success or negative error
  296. *
  297. * int bpf_map_delete_elem(&map, &key)
  298. * Return: 0 on success or negative error
  299. *
  300. * int bpf_probe_read(void *dst, int size, void *src)
  301. * Return: 0 on success or negative error
  302. *
  303. * u64 bpf_ktime_get_ns(void)
  304. * Return: current ktime
  305. *
  306. * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
  307. * Return: length of buffer written or negative error
  308. *
  309. * u32 bpf_prandom_u32(void)
  310. * Return: random value
  311. *
  312. * u32 bpf_raw_smp_processor_id(void)
  313. * Return: SMP processor ID
  314. *
  315. * int bpf_skb_store_bytes(skb, offset, from, len, flags)
  316. * store bytes into packet
  317. * @skb: pointer to skb
  318. * @offset: offset within packet from skb->mac_header
  319. * @from: pointer where to copy bytes from
  320. * @len: number of bytes to store into packet
  321. * @flags: bit 0 - if true, recompute skb->csum
  322. * other bits - reserved
  323. * Return: 0 on success or negative error
  324. *
  325. * int bpf_l3_csum_replace(skb, offset, from, to, flags)
  326. * recompute IP checksum
  327. * @skb: pointer to skb
  328. * @offset: offset within packet where IP checksum is located
  329. * @from: old value of header field
  330. * @to: new value of header field
  331. * @flags: bits 0-3 - size of header field
  332. * other bits - reserved
  333. * Return: 0 on success or negative error
  334. *
  335. * int bpf_l4_csum_replace(skb, offset, from, to, flags)
  336. * recompute TCP/UDP checksum
  337. * @skb: pointer to skb
  338. * @offset: offset within packet where TCP/UDP checksum is located
  339. * @from: old value of header field
  340. * @to: new value of header field
  341. * @flags: bits 0-3 - size of header field
  342. * bit 4 - is pseudo header
  343. * other bits - reserved
  344. * Return: 0 on success or negative error
  345. *
  346. * int bpf_tail_call(ctx, prog_array_map, index)
  347. * jump into another BPF program
  348. * @ctx: context pointer passed to next program
  349. * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
  350. * @index: 32-bit index inside array that selects specific program to run
  351. * Return: 0 on success or negative error
  352. *
  353. * int bpf_clone_redirect(skb, ifindex, flags)
  354. * redirect to another netdev
  355. * @skb: pointer to skb
  356. * @ifindex: ifindex of the net device
  357. * @flags: bit 0 - if set, redirect to ingress instead of egress
  358. * other bits - reserved
  359. * Return: 0 on success or negative error
  360. *
  361. * u64 bpf_get_current_pid_tgid(void)
  362. * Return: current->tgid << 32 | current->pid
  363. *
  364. * u64 bpf_get_current_uid_gid(void)
  365. * Return: current_gid << 32 | current_uid
  366. *
  367. * int bpf_get_current_comm(char *buf, int size_of_buf)
  368. * stores current->comm into buf
  369. * Return: 0 on success or negative error
  370. *
  371. * u32 bpf_get_cgroup_classid(skb)
  372. * retrieve a proc's classid
  373. * @skb: pointer to skb
  374. * Return: classid if != 0
  375. *
  376. * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
  377. * Return: 0 on success or negative error
  378. *
  379. * int bpf_skb_vlan_pop(skb)
  380. * Return: 0 on success or negative error
  381. *
  382. * int bpf_skb_get_tunnel_key(skb, key, size, flags)
  383. * int bpf_skb_set_tunnel_key(skb, key, size, flags)
  384. * retrieve or populate tunnel metadata
  385. * @skb: pointer to skb
  386. * @key: pointer to 'struct bpf_tunnel_key'
  387. * @size: size of 'struct bpf_tunnel_key'
  388. * @flags: room for future extensions
  389. * Return: 0 on success or negative error
  390. *
  391. * u64 bpf_perf_event_read(map, flags)
  392. * read perf event counter value
  393. * @map: pointer to perf_event_array map
  394. * @flags: index of event in the map or bitmask flags
  395. * Return: value of perf event counter read or error code
  396. *
  397. * int bpf_redirect(ifindex, flags)
  398. * redirect to another netdev
  399. * @ifindex: ifindex of the net device
  400. * @flags:
  401. * cls_bpf:
  402. * bit 0 - if set, redirect to ingress instead of egress
  403. * other bits - reserved
  404. * xdp_bpf:
  405. * all bits - reserved
  406. * Return: cls_bpf: TC_ACT_REDIRECT on success or TC_ACT_SHOT on error
  407. * xdp_bfp: XDP_REDIRECT on success or XDP_ABORT on error
  408. * int bpf_redirect_map(map, key, flags)
  409. * redirect to endpoint in map
  410. * @map: pointer to dev map
  411. * @key: index in map to lookup
  412. * @flags: --
  413. * Return: XDP_REDIRECT on success or XDP_ABORT on error
  414. *
  415. * u32 bpf_get_route_realm(skb)
  416. * retrieve a dst's tclassid
  417. * @skb: pointer to skb
  418. * Return: realm if != 0
  419. *
  420. * int bpf_perf_event_output(ctx, map, flags, data, size)
  421. * output perf raw sample
  422. * @ctx: struct pt_regs*
  423. * @map: pointer to perf_event_array map
  424. * @flags: index of event in the map or bitmask flags
  425. * @data: data on stack to be output as raw data
  426. * @size: size of data
  427. * Return: 0 on success or negative error
  428. *
  429. * int bpf_get_stackid(ctx, map, flags)
  430. * walk user or kernel stack and return id
  431. * @ctx: struct pt_regs*
  432. * @map: pointer to stack_trace map
  433. * @flags: bits 0-7 - numer of stack frames to skip
  434. * bit 8 - collect user stack instead of kernel
  435. * bit 9 - compare stacks by hash only
  436. * bit 10 - if two different stacks hash into the same stackid
  437. * discard old
  438. * other bits - reserved
  439. * Return: >= 0 stackid on success or negative error
  440. *
  441. * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
  442. * calculate csum diff
  443. * @from: raw from buffer
  444. * @from_size: length of from buffer
  445. * @to: raw to buffer
  446. * @to_size: length of to buffer
  447. * @seed: optional seed
  448. * Return: csum result or negative error code
  449. *
  450. * int bpf_skb_get_tunnel_opt(skb, opt, size)
  451. * retrieve tunnel options metadata
  452. * @skb: pointer to skb
  453. * @opt: pointer to raw tunnel option data
  454. * @size: size of @opt
  455. * Return: option size
  456. *
  457. * int bpf_skb_set_tunnel_opt(skb, opt, size)
  458. * populate tunnel options metadata
  459. * @skb: pointer to skb
  460. * @opt: pointer to raw tunnel option data
  461. * @size: size of @opt
  462. * Return: 0 on success or negative error
  463. *
  464. * int bpf_skb_change_proto(skb, proto, flags)
  465. * Change protocol of the skb. Currently supported is v4 -> v6,
  466. * v6 -> v4 transitions. The helper will also resize the skb. eBPF
  467. * program is expected to fill the new headers via skb_store_bytes
  468. * and lX_csum_replace.
  469. * @skb: pointer to skb
  470. * @proto: new skb->protocol type
  471. * @flags: reserved
  472. * Return: 0 on success or negative error
  473. *
  474. * int bpf_skb_change_type(skb, type)
  475. * Change packet type of skb.
  476. * @skb: pointer to skb
  477. * @type: new skb->pkt_type type
  478. * Return: 0 on success or negative error
  479. *
  480. * int bpf_skb_under_cgroup(skb, map, index)
  481. * Check cgroup2 membership of skb
  482. * @skb: pointer to skb
  483. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  484. * @index: index of the cgroup in the bpf_map
  485. * Return:
  486. * == 0 skb failed the cgroup2 descendant test
  487. * == 1 skb succeeded the cgroup2 descendant test
  488. * < 0 error
  489. *
  490. * u32 bpf_get_hash_recalc(skb)
  491. * Retrieve and possibly recalculate skb->hash.
  492. * @skb: pointer to skb
  493. * Return: hash
  494. *
  495. * u64 bpf_get_current_task(void)
  496. * Returns current task_struct
  497. * Return: current
  498. *
  499. * int bpf_probe_write_user(void *dst, void *src, int len)
  500. * safely attempt to write to a location
  501. * @dst: destination address in userspace
  502. * @src: source address on stack
  503. * @len: number of bytes to copy
  504. * Return: 0 on success or negative error
  505. *
  506. * int bpf_current_task_under_cgroup(map, index)
  507. * Check cgroup2 membership of current task
  508. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  509. * @index: index of the cgroup in the bpf_map
  510. * Return:
  511. * == 0 current failed the cgroup2 descendant test
  512. * == 1 current succeeded the cgroup2 descendant test
  513. * < 0 error
  514. *
  515. * int bpf_skb_change_tail(skb, len, flags)
  516. * The helper will resize the skb to the given new size, to be used f.e.
  517. * with control messages.
  518. * @skb: pointer to skb
  519. * @len: new skb length
  520. * @flags: reserved
  521. * Return: 0 on success or negative error
  522. *
  523. * int bpf_skb_pull_data(skb, len)
  524. * The helper will pull in non-linear data in case the skb is non-linear
  525. * and not all of len are part of the linear section. Only needed for
  526. * read/write with direct packet access.
  527. * @skb: pointer to skb
  528. * @len: len to make read/writeable
  529. * Return: 0 on success or negative error
  530. *
  531. * s64 bpf_csum_update(skb, csum)
  532. * Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
  533. * @skb: pointer to skb
  534. * @csum: csum to add
  535. * Return: csum on success or negative error
  536. *
  537. * void bpf_set_hash_invalid(skb)
  538. * Invalidate current skb->hash.
  539. * @skb: pointer to skb
  540. *
  541. * int bpf_get_numa_node_id()
  542. * Return: Id of current NUMA node.
  543. *
  544. * int bpf_skb_change_head()
  545. * Grows headroom of skb and adjusts MAC header offset accordingly.
  546. * Will extends/reallocae as required automatically.
  547. * May change skb data pointer and will thus invalidate any check
  548. * performed for direct packet access.
  549. * @skb: pointer to skb
  550. * @len: length of header to be pushed in front
  551. * @flags: Flags (unused for now)
  552. * Return: 0 on success or negative error
  553. *
  554. * int bpf_xdp_adjust_head(xdp_md, delta)
  555. * Adjust the xdp_md.data by delta
  556. * @xdp_md: pointer to xdp_md
  557. * @delta: An positive/negative integer to be added to xdp_md.data
  558. * Return: 0 on success or negative on error
  559. *
  560. * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
  561. * Copy a NUL terminated string from unsafe address. In case the string
  562. * length is smaller than size, the target is not padded with further NUL
  563. * bytes. In case the string length is larger than size, just count-1
  564. * bytes are copied and the last byte is set to NUL.
  565. * @dst: destination address
  566. * @size: maximum number of bytes to copy, including the trailing NUL
  567. * @unsafe_ptr: unsafe address
  568. * Return:
  569. * > 0 length of the string including the trailing NUL on success
  570. * < 0 error
  571. *
  572. * u64 bpf_get_socket_cookie(skb)
  573. * Get the cookie for the socket stored inside sk_buff.
  574. * @skb: pointer to skb
  575. * Return: 8 Bytes non-decreasing number on success or 0 if the socket
  576. * field is missing inside sk_buff
  577. *
  578. * u32 bpf_get_socket_uid(skb)
  579. * Get the owner uid of the socket stored inside sk_buff.
  580. * @skb: pointer to skb
  581. * Return: uid of the socket owner on success or overflowuid if failed.
  582. *
  583. * u32 bpf_set_hash(skb, hash)
  584. * Set full skb->hash.
  585. * @skb: pointer to skb
  586. * @hash: hash to set
  587. *
  588. * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen)
  589. * Calls setsockopt. Not all opts are available, only those with
  590. * integer optvals plus TCP_CONGESTION.
  591. * Supported levels: SOL_SOCKET and IPPROTO_TCP
  592. * @bpf_socket: pointer to bpf_socket
  593. * @level: SOL_SOCKET or IPPROTO_TCP
  594. * @optname: option name
  595. * @optval: pointer to option value
  596. * @optlen: length of optval in bytes
  597. * Return: 0 or negative error
  598. *
  599. * int bpf_getsockopt(bpf_socket, level, optname, optval, optlen)
  600. * Calls getsockopt. Not all opts are available.
  601. * Supported levels: IPPROTO_TCP
  602. * @bpf_socket: pointer to bpf_socket
  603. * @level: IPPROTO_TCP
  604. * @optname: option name
  605. * @optval: pointer to option value
  606. * @optlen: length of optval in bytes
  607. * Return: 0 or negative error
  608. *
  609. * int bpf_skb_adjust_room(skb, len_diff, mode, flags)
  610. * Grow or shrink room in sk_buff.
  611. * @skb: pointer to skb
  612. * @len_diff: (signed) amount of room to grow/shrink
  613. * @mode: operation mode (enum bpf_adj_room_mode)
  614. * @flags: reserved for future use
  615. * Return: 0 on success or negative error code
  616. *
  617. * int bpf_sk_redirect_map(map, key, flags)
  618. * Redirect skb to a sock in map using key as a lookup key for the
  619. * sock in map.
  620. * @map: pointer to sockmap
  621. * @key: key to lookup sock in map
  622. * @flags: reserved for future use
  623. * Return: SK_PASS
  624. *
  625. * int bpf_sock_map_update(skops, map, key, flags)
  626. * @skops: pointer to bpf_sock_ops
  627. * @map: pointer to sockmap to update
  628. * @key: key to insert/update sock in map
  629. * @flags: same flags as map update elem
  630. *
  631. * int bpf_xdp_adjust_meta(xdp_md, delta)
  632. * Adjust the xdp_md.data_meta by delta
  633. * @xdp_md: pointer to xdp_md
  634. * @delta: An positive/negative integer to be added to xdp_md.data_meta
  635. * Return: 0 on success or negative on error
  636. *
  637. * int bpf_perf_event_read_value(map, flags, buf, buf_size)
  638. * read perf event counter value and perf event enabled/running time
  639. * @map: pointer to perf_event_array map
  640. * @flags: index of event in the map or bitmask flags
  641. * @buf: buf to fill
  642. * @buf_size: size of the buf
  643. * Return: 0 on success or negative error code
  644. *
  645. * int bpf_perf_prog_read_value(ctx, buf, buf_size)
  646. * read perf prog attached perf event counter and enabled/running time
  647. * @ctx: pointer to ctx
  648. * @buf: buf to fill
  649. * @buf_size: size of the buf
  650. * Return : 0 on success or negative error code
  651. *
  652. * int bpf_override_return(pt_regs, rc)
  653. * @pt_regs: pointer to struct pt_regs
  654. * @rc: the return value to set
  655. */
  656. #define __BPF_FUNC_MAPPER(FN) \
  657. FN(unspec), \
  658. FN(map_lookup_elem), \
  659. FN(map_update_elem), \
  660. FN(map_delete_elem), \
  661. FN(probe_read), \
  662. FN(ktime_get_ns), \
  663. FN(trace_printk), \
  664. FN(get_prandom_u32), \
  665. FN(get_smp_processor_id), \
  666. FN(skb_store_bytes), \
  667. FN(l3_csum_replace), \
  668. FN(l4_csum_replace), \
  669. FN(tail_call), \
  670. FN(clone_redirect), \
  671. FN(get_current_pid_tgid), \
  672. FN(get_current_uid_gid), \
  673. FN(get_current_comm), \
  674. FN(get_cgroup_classid), \
  675. FN(skb_vlan_push), \
  676. FN(skb_vlan_pop), \
  677. FN(skb_get_tunnel_key), \
  678. FN(skb_set_tunnel_key), \
  679. FN(perf_event_read), \
  680. FN(redirect), \
  681. FN(get_route_realm), \
  682. FN(perf_event_output), \
  683. FN(skb_load_bytes), \
  684. FN(get_stackid), \
  685. FN(csum_diff), \
  686. FN(skb_get_tunnel_opt), \
  687. FN(skb_set_tunnel_opt), \
  688. FN(skb_change_proto), \
  689. FN(skb_change_type), \
  690. FN(skb_under_cgroup), \
  691. FN(get_hash_recalc), \
  692. FN(get_current_task), \
  693. FN(probe_write_user), \
  694. FN(current_task_under_cgroup), \
  695. FN(skb_change_tail), \
  696. FN(skb_pull_data), \
  697. FN(csum_update), \
  698. FN(set_hash_invalid), \
  699. FN(get_numa_node_id), \
  700. FN(skb_change_head), \
  701. FN(xdp_adjust_head), \
  702. FN(probe_read_str), \
  703. FN(get_socket_cookie), \
  704. FN(get_socket_uid), \
  705. FN(set_hash), \
  706. FN(setsockopt), \
  707. FN(skb_adjust_room), \
  708. FN(redirect_map), \
  709. FN(sk_redirect_map), \
  710. FN(sock_map_update), \
  711. FN(xdp_adjust_meta), \
  712. FN(perf_event_read_value), \
  713. FN(perf_prog_read_value), \
  714. FN(getsockopt), \
  715. FN(override_return),
  716. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  717. * function eBPF program intends to call
  718. */
  719. #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
  720. enum bpf_func_id {
  721. __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
  722. __BPF_FUNC_MAX_ID,
  723. };
  724. #undef __BPF_ENUM_FN
  725. /* All flags used by eBPF helper functions, placed here. */
  726. /* BPF_FUNC_skb_store_bytes flags. */
  727. #define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
  728. #define BPF_F_INVALIDATE_HASH (1ULL << 1)
  729. /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
  730. * First 4 bits are for passing the header field size.
  731. */
  732. #define BPF_F_HDR_FIELD_MASK 0xfULL
  733. /* BPF_FUNC_l4_csum_replace flags. */
  734. #define BPF_F_PSEUDO_HDR (1ULL << 4)
  735. #define BPF_F_MARK_MANGLED_0 (1ULL << 5)
  736. #define BPF_F_MARK_ENFORCE (1ULL << 6)
  737. /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
  738. #define BPF_F_INGRESS (1ULL << 0)
  739. /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
  740. #define BPF_F_TUNINFO_IPV6 (1ULL << 0)
  741. /* BPF_FUNC_get_stackid flags. */
  742. #define BPF_F_SKIP_FIELD_MASK 0xffULL
  743. #define BPF_F_USER_STACK (1ULL << 8)
  744. #define BPF_F_FAST_STACK_CMP (1ULL << 9)
  745. #define BPF_F_REUSE_STACKID (1ULL << 10)
  746. /* BPF_FUNC_skb_set_tunnel_key flags. */
  747. #define BPF_F_ZERO_CSUM_TX (1ULL << 1)
  748. #define BPF_F_DONT_FRAGMENT (1ULL << 2)
  749. /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and
  750. * BPF_FUNC_perf_event_read_value flags.
  751. */
  752. #define BPF_F_INDEX_MASK 0xffffffffULL
  753. #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
  754. /* BPF_FUNC_perf_event_output for sk_buff input context. */
  755. #define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
  756. /* Mode for BPF_FUNC_skb_adjust_room helper. */
  757. enum bpf_adj_room_mode {
  758. BPF_ADJ_ROOM_NET,
  759. };
  760. /* user accessible mirror of in-kernel sk_buff.
  761. * new fields can only be added to the end of this structure
  762. */
  763. struct __sk_buff {
  764. __u32 len;
  765. __u32 pkt_type;
  766. __u32 mark;
  767. __u32 queue_mapping;
  768. __u32 protocol;
  769. __u32 vlan_present;
  770. __u32 vlan_tci;
  771. __u32 vlan_proto;
  772. __u32 priority;
  773. __u32 ingress_ifindex;
  774. __u32 ifindex;
  775. __u32 tc_index;
  776. __u32 cb[5];
  777. __u32 hash;
  778. __u32 tc_classid;
  779. __u32 data;
  780. __u32 data_end;
  781. __u32 napi_id;
  782. /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */
  783. __u32 family;
  784. __u32 remote_ip4; /* Stored in network byte order */
  785. __u32 local_ip4; /* Stored in network byte order */
  786. __u32 remote_ip6[4]; /* Stored in network byte order */
  787. __u32 local_ip6[4]; /* Stored in network byte order */
  788. __u32 remote_port; /* Stored in network byte order */
  789. __u32 local_port; /* stored in host byte order */
  790. /* ... here. */
  791. __u32 data_meta;
  792. };
  793. struct bpf_tunnel_key {
  794. __u32 tunnel_id;
  795. union {
  796. __u32 remote_ipv4;
  797. __u32 remote_ipv6[4];
  798. };
  799. __u8 tunnel_tos;
  800. __u8 tunnel_ttl;
  801. __u16 tunnel_ext;
  802. __u32 tunnel_label;
  803. };
  804. /* Generic BPF return codes which all BPF program types may support.
  805. * The values are binary compatible with their TC_ACT_* counter-part to
  806. * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
  807. * programs.
  808. *
  809. * XDP is handled seprately, see XDP_*.
  810. */
  811. enum bpf_ret_code {
  812. BPF_OK = 0,
  813. /* 1 reserved */
  814. BPF_DROP = 2,
  815. /* 3-6 reserved */
  816. BPF_REDIRECT = 7,
  817. /* >127 are reserved for prog type specific return codes */
  818. };
  819. struct bpf_sock {
  820. __u32 bound_dev_if;
  821. __u32 family;
  822. __u32 type;
  823. __u32 protocol;
  824. __u32 mark;
  825. __u32 priority;
  826. };
  827. #define XDP_PACKET_HEADROOM 256
  828. /* User return codes for XDP prog type.
  829. * A valid XDP program must return one of these defined values. All other
  830. * return codes are reserved for future use. Unknown return codes will
  831. * result in packet drops and a warning via bpf_warn_invalid_xdp_action().
  832. */
  833. enum xdp_action {
  834. XDP_ABORTED = 0,
  835. XDP_DROP,
  836. XDP_PASS,
  837. XDP_TX,
  838. XDP_REDIRECT,
  839. };
  840. /* user accessible metadata for XDP packet hook
  841. * new fields must be added to the end of this structure
  842. */
  843. struct xdp_md {
  844. __u32 data;
  845. __u32 data_end;
  846. __u32 data_meta;
  847. };
  848. enum sk_action {
  849. SK_DROP = 0,
  850. SK_PASS,
  851. };
  852. #define BPF_TAG_SIZE 8
  853. struct bpf_prog_info {
  854. __u32 type;
  855. __u32 id;
  856. __u8 tag[BPF_TAG_SIZE];
  857. __u32 jited_prog_len;
  858. __u32 xlated_prog_len;
  859. __aligned_u64 jited_prog_insns;
  860. __aligned_u64 xlated_prog_insns;
  861. __u64 load_time; /* ns since boottime */
  862. __u32 created_by_uid;
  863. __u32 nr_map_ids;
  864. __aligned_u64 map_ids;
  865. char name[BPF_OBJ_NAME_LEN];
  866. } __attribute__((aligned(8)));
  867. struct bpf_map_info {
  868. __u32 type;
  869. __u32 id;
  870. __u32 key_size;
  871. __u32 value_size;
  872. __u32 max_entries;
  873. __u32 map_flags;
  874. char name[BPF_OBJ_NAME_LEN];
  875. } __attribute__((aligned(8)));
  876. /* User bpf_sock_ops struct to access socket values and specify request ops
  877. * and their replies.
  878. * Some of this fields are in network (bigendian) byte order and may need
  879. * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h).
  880. * New fields can only be added at the end of this structure
  881. */
  882. struct bpf_sock_ops {
  883. __u32 op;
  884. union {
  885. __u32 reply;
  886. __u32 replylong[4];
  887. };
  888. __u32 family;
  889. __u32 remote_ip4; /* Stored in network byte order */
  890. __u32 local_ip4; /* Stored in network byte order */
  891. __u32 remote_ip6[4]; /* Stored in network byte order */
  892. __u32 local_ip6[4]; /* Stored in network byte order */
  893. __u32 remote_port; /* Stored in network byte order */
  894. __u32 local_port; /* stored in host byte order */
  895. __u32 is_fullsock; /* Some TCP fields are only valid if
  896. * there is a full socket. If not, the
  897. * fields read as zero.
  898. */
  899. __u32 snd_cwnd;
  900. __u32 srtt_us; /* Averaged RTT << 3 in usecs */
  901. };
  902. /* List of known BPF sock_ops operators.
  903. * New entries can only be added at the end
  904. */
  905. enum {
  906. BPF_SOCK_OPS_VOID,
  907. BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or
  908. * -1 if default value should be used
  909. */
  910. BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized
  911. * window (in packets) or -1 if default
  912. * value should be used
  913. */
  914. BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an
  915. * active connection is initialized
  916. */
  917. BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an
  918. * active connection is
  919. * established
  920. */
  921. BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a
  922. * passive connection is
  923. * established
  924. */
  925. BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control
  926. * needs ECN
  927. */
  928. BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is
  929. * based on the path and may be
  930. * dependent on the congestion control
  931. * algorithm. In general it indicates
  932. * a congestion threshold. RTTs above
  933. * this indicate congestion
  934. */
  935. };
  936. #define TCP_BPF_IW 1001 /* Set TCP initial congestion window */
  937. #define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */
  938. struct bpf_perf_event_value {
  939. __u64 counter;
  940. __u64 enabled;
  941. __u64 running;
  942. };
  943. #define BPF_DEVCG_ACC_MKNOD (1ULL << 0)
  944. #define BPF_DEVCG_ACC_READ (1ULL << 1)
  945. #define BPF_DEVCG_ACC_WRITE (1ULL << 2)
  946. #define BPF_DEVCG_DEV_BLOCK (1ULL << 0)
  947. #define BPF_DEVCG_DEV_CHAR (1ULL << 1)
  948. struct bpf_cgroup_dev_ctx {
  949. /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */
  950. __u32 access_type;
  951. __u32 major;
  952. __u32 minor;
  953. };
  954. #endif /* _UAPI__LINUX_BPF_H__ */