bpf.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. #define BPF_PSEUDO_MAP_FD 1
  182. /* flags for BPF_MAP_UPDATE_ELEM command */
  183. #define BPF_ANY 0 /* create new element or update existing */
  184. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  185. #define BPF_EXIST 2 /* update existing element */
  186. /* flags for BPF_MAP_CREATE command */
  187. #define BPF_F_NO_PREALLOC (1U << 0)
  188. /* Instead of having one common LRU list in the
  189. * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
  190. * which can scale and perform better.
  191. * Note, the LRU nodes (including free nodes) cannot be moved
  192. * across different LRU lists.
  193. */
  194. #define BPF_F_NO_COMMON_LRU (1U << 1)
  195. /* Specify numa node during map creation */
  196. #define BPF_F_NUMA_NODE (1U << 2)
  197. /* flags for BPF_PROG_QUERY */
  198. #define BPF_F_QUERY_EFFECTIVE (1U << 0)
  199. #define BPF_OBJ_NAME_LEN 16U
  200. /* Flags for accessing BPF object */
  201. #define BPF_F_RDONLY (1U << 3)
  202. #define BPF_F_WRONLY (1U << 4)
  203. union bpf_attr {
  204. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  205. __u32 map_type; /* one of enum bpf_map_type */
  206. __u32 key_size; /* size of key in bytes */
  207. __u32 value_size; /* size of value in bytes */
  208. __u32 max_entries; /* max number of entries in a map */
  209. __u32 map_flags; /* BPF_MAP_CREATE related
  210. * flags defined above.
  211. */
  212. __u32 inner_map_fd; /* fd pointing to the inner map */
  213. __u32 numa_node; /* numa node (effective only if
  214. * BPF_F_NUMA_NODE is set).
  215. */
  216. char map_name[BPF_OBJ_NAME_LEN];
  217. };
  218. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  219. __u32 map_fd;
  220. __aligned_u64 key;
  221. union {
  222. __aligned_u64 value;
  223. __aligned_u64 next_key;
  224. };
  225. __u64 flags;
  226. };
  227. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  228. __u32 prog_type; /* one of enum bpf_prog_type */
  229. __u32 insn_cnt;
  230. __aligned_u64 insns;
  231. __aligned_u64 license;
  232. __u32 log_level; /* verbosity level of verifier */
  233. __u32 log_size; /* size of user buffer */
  234. __aligned_u64 log_buf; /* user supplied buffer */
  235. __u32 kern_version; /* checked when prog_type=kprobe */
  236. __u32 prog_flags;
  237. char prog_name[BPF_OBJ_NAME_LEN];
  238. __u32 prog_ifindex; /* ifindex of netdev to prep for */
  239. };
  240. struct { /* anonymous struct used by BPF_OBJ_* commands */
  241. __aligned_u64 pathname;
  242. __u32 bpf_fd;
  243. __u32 file_flags;
  244. };
  245. struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
  246. __u32 target_fd; /* container object to attach to */
  247. __u32 attach_bpf_fd; /* eBPF program to attach */
  248. __u32 attach_type;
  249. __u32 attach_flags;
  250. };
  251. struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
  252. __u32 prog_fd;
  253. __u32 retval;
  254. __u32 data_size_in;
  255. __u32 data_size_out;
  256. __aligned_u64 data_in;
  257. __aligned_u64 data_out;
  258. __u32 repeat;
  259. __u32 duration;
  260. } test;
  261. struct { /* anonymous struct used by BPF_*_GET_*_ID */
  262. union {
  263. __u32 start_id;
  264. __u32 prog_id;
  265. __u32 map_id;
  266. };
  267. __u32 next_id;
  268. __u32 open_flags;
  269. };
  270. struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
  271. __u32 bpf_fd;
  272. __u32 info_len;
  273. __aligned_u64 info;
  274. } info;
  275. struct { /* anonymous struct used by BPF_PROG_QUERY command */
  276. __u32 target_fd; /* container object to query */
  277. __u32 attach_type;
  278. __u32 query_flags;
  279. __u32 attach_flags;
  280. __aligned_u64 prog_ids;
  281. __u32 prog_cnt;
  282. } query;
  283. } __attribute__((aligned(8)));
  284. /* BPF helper function descriptions:
  285. *
  286. * void *bpf_map_lookup_elem(&map, &key)
  287. * Return: Map value or NULL
  288. *
  289. * int bpf_map_update_elem(&map, &key, &value, flags)
  290. * Return: 0 on success or negative error
  291. *
  292. * int bpf_map_delete_elem(&map, &key)
  293. * Return: 0 on success or negative error
  294. *
  295. * int bpf_probe_read(void *dst, int size, void *src)
  296. * Return: 0 on success or negative error
  297. *
  298. * u64 bpf_ktime_get_ns(void)
  299. * Return: current ktime
  300. *
  301. * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
  302. * Return: length of buffer written or negative error
  303. *
  304. * u32 bpf_prandom_u32(void)
  305. * Return: random value
  306. *
  307. * u32 bpf_raw_smp_processor_id(void)
  308. * Return: SMP processor ID
  309. *
  310. * int bpf_skb_store_bytes(skb, offset, from, len, flags)
  311. * store bytes into packet
  312. * @skb: pointer to skb
  313. * @offset: offset within packet from skb->mac_header
  314. * @from: pointer where to copy bytes from
  315. * @len: number of bytes to store into packet
  316. * @flags: bit 0 - if true, recompute skb->csum
  317. * other bits - reserved
  318. * Return: 0 on success or negative error
  319. *
  320. * int bpf_l3_csum_replace(skb, offset, from, to, flags)
  321. * recompute IP checksum
  322. * @skb: pointer to skb
  323. * @offset: offset within packet where IP checksum is located
  324. * @from: old value of header field
  325. * @to: new value of header field
  326. * @flags: bits 0-3 - size of header field
  327. * other bits - reserved
  328. * Return: 0 on success or negative error
  329. *
  330. * int bpf_l4_csum_replace(skb, offset, from, to, flags)
  331. * recompute TCP/UDP checksum
  332. * @skb: pointer to skb
  333. * @offset: offset within packet where TCP/UDP checksum is located
  334. * @from: old value of header field
  335. * @to: new value of header field
  336. * @flags: bits 0-3 - size of header field
  337. * bit 4 - is pseudo header
  338. * other bits - reserved
  339. * Return: 0 on success or negative error
  340. *
  341. * int bpf_tail_call(ctx, prog_array_map, index)
  342. * jump into another BPF program
  343. * @ctx: context pointer passed to next program
  344. * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
  345. * @index: 32-bit index inside array that selects specific program to run
  346. * Return: 0 on success or negative error
  347. *
  348. * int bpf_clone_redirect(skb, ifindex, flags)
  349. * redirect to another netdev
  350. * @skb: pointer to skb
  351. * @ifindex: ifindex of the net device
  352. * @flags: bit 0 - if set, redirect to ingress instead of egress
  353. * other bits - reserved
  354. * Return: 0 on success or negative error
  355. *
  356. * u64 bpf_get_current_pid_tgid(void)
  357. * Return: current->tgid << 32 | current->pid
  358. *
  359. * u64 bpf_get_current_uid_gid(void)
  360. * Return: current_gid << 32 | current_uid
  361. *
  362. * int bpf_get_current_comm(char *buf, int size_of_buf)
  363. * stores current->comm into buf
  364. * Return: 0 on success or negative error
  365. *
  366. * u32 bpf_get_cgroup_classid(skb)
  367. * retrieve a proc's classid
  368. * @skb: pointer to skb
  369. * Return: classid if != 0
  370. *
  371. * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
  372. * Return: 0 on success or negative error
  373. *
  374. * int bpf_skb_vlan_pop(skb)
  375. * Return: 0 on success or negative error
  376. *
  377. * int bpf_skb_get_tunnel_key(skb, key, size, flags)
  378. * int bpf_skb_set_tunnel_key(skb, key, size, flags)
  379. * retrieve or populate tunnel metadata
  380. * @skb: pointer to skb
  381. * @key: pointer to 'struct bpf_tunnel_key'
  382. * @size: size of 'struct bpf_tunnel_key'
  383. * @flags: room for future extensions
  384. * Return: 0 on success or negative error
  385. *
  386. * u64 bpf_perf_event_read(map, flags)
  387. * read perf event counter value
  388. * @map: pointer to perf_event_array map
  389. * @flags: index of event in the map or bitmask flags
  390. * Return: value of perf event counter read or error code
  391. *
  392. * int bpf_redirect(ifindex, flags)
  393. * redirect to another netdev
  394. * @ifindex: ifindex of the net device
  395. * @flags:
  396. * cls_bpf:
  397. * bit 0 - if set, redirect to ingress instead of egress
  398. * other bits - reserved
  399. * xdp_bpf:
  400. * all bits - reserved
  401. * Return: cls_bpf: TC_ACT_REDIRECT on success or TC_ACT_SHOT on error
  402. * xdp_bfp: XDP_REDIRECT on success or XDP_ABORT on error
  403. * int bpf_redirect_map(map, key, flags)
  404. * redirect to endpoint in map
  405. * @map: pointer to dev map
  406. * @key: index in map to lookup
  407. * @flags: --
  408. * Return: XDP_REDIRECT on success or XDP_ABORT on error
  409. *
  410. * u32 bpf_get_route_realm(skb)
  411. * retrieve a dst's tclassid
  412. * @skb: pointer to skb
  413. * Return: realm if != 0
  414. *
  415. * int bpf_perf_event_output(ctx, map, flags, data, size)
  416. * output perf raw sample
  417. * @ctx: struct pt_regs*
  418. * @map: pointer to perf_event_array map
  419. * @flags: index of event in the map or bitmask flags
  420. * @data: data on stack to be output as raw data
  421. * @size: size of data
  422. * Return: 0 on success or negative error
  423. *
  424. * int bpf_get_stackid(ctx, map, flags)
  425. * walk user or kernel stack and return id
  426. * @ctx: struct pt_regs*
  427. * @map: pointer to stack_trace map
  428. * @flags: bits 0-7 - numer of stack frames to skip
  429. * bit 8 - collect user stack instead of kernel
  430. * bit 9 - compare stacks by hash only
  431. * bit 10 - if two different stacks hash into the same stackid
  432. * discard old
  433. * other bits - reserved
  434. * Return: >= 0 stackid on success or negative error
  435. *
  436. * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
  437. * calculate csum diff
  438. * @from: raw from buffer
  439. * @from_size: length of from buffer
  440. * @to: raw to buffer
  441. * @to_size: length of to buffer
  442. * @seed: optional seed
  443. * Return: csum result or negative error code
  444. *
  445. * int bpf_skb_get_tunnel_opt(skb, opt, size)
  446. * retrieve tunnel options metadata
  447. * @skb: pointer to skb
  448. * @opt: pointer to raw tunnel option data
  449. * @size: size of @opt
  450. * Return: option size
  451. *
  452. * int bpf_skb_set_tunnel_opt(skb, opt, size)
  453. * populate tunnel options metadata
  454. * @skb: pointer to skb
  455. * @opt: pointer to raw tunnel option data
  456. * @size: size of @opt
  457. * Return: 0 on success or negative error
  458. *
  459. * int bpf_skb_change_proto(skb, proto, flags)
  460. * Change protocol of the skb. Currently supported is v4 -> v6,
  461. * v6 -> v4 transitions. The helper will also resize the skb. eBPF
  462. * program is expected to fill the new headers via skb_store_bytes
  463. * and lX_csum_replace.
  464. * @skb: pointer to skb
  465. * @proto: new skb->protocol type
  466. * @flags: reserved
  467. * Return: 0 on success or negative error
  468. *
  469. * int bpf_skb_change_type(skb, type)
  470. * Change packet type of skb.
  471. * @skb: pointer to skb
  472. * @type: new skb->pkt_type type
  473. * Return: 0 on success or negative error
  474. *
  475. * int bpf_skb_under_cgroup(skb, map, index)
  476. * Check cgroup2 membership of skb
  477. * @skb: pointer to skb
  478. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  479. * @index: index of the cgroup in the bpf_map
  480. * Return:
  481. * == 0 skb failed the cgroup2 descendant test
  482. * == 1 skb succeeded the cgroup2 descendant test
  483. * < 0 error
  484. *
  485. * u32 bpf_get_hash_recalc(skb)
  486. * Retrieve and possibly recalculate skb->hash.
  487. * @skb: pointer to skb
  488. * Return: hash
  489. *
  490. * u64 bpf_get_current_task(void)
  491. * Returns current task_struct
  492. * Return: current
  493. *
  494. * int bpf_probe_write_user(void *dst, void *src, int len)
  495. * safely attempt to write to a location
  496. * @dst: destination address in userspace
  497. * @src: source address on stack
  498. * @len: number of bytes to copy
  499. * Return: 0 on success or negative error
  500. *
  501. * int bpf_current_task_under_cgroup(map, index)
  502. * Check cgroup2 membership of current task
  503. * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
  504. * @index: index of the cgroup in the bpf_map
  505. * Return:
  506. * == 0 current failed the cgroup2 descendant test
  507. * == 1 current succeeded the cgroup2 descendant test
  508. * < 0 error
  509. *
  510. * int bpf_skb_change_tail(skb, len, flags)
  511. * The helper will resize the skb to the given new size, to be used f.e.
  512. * with control messages.
  513. * @skb: pointer to skb
  514. * @len: new skb length
  515. * @flags: reserved
  516. * Return: 0 on success or negative error
  517. *
  518. * int bpf_skb_pull_data(skb, len)
  519. * The helper will pull in non-linear data in case the skb is non-linear
  520. * and not all of len are part of the linear section. Only needed for
  521. * read/write with direct packet access.
  522. * @skb: pointer to skb
  523. * @len: len to make read/writeable
  524. * Return: 0 on success or negative error
  525. *
  526. * s64 bpf_csum_update(skb, csum)
  527. * Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
  528. * @skb: pointer to skb
  529. * @csum: csum to add
  530. * Return: csum on success or negative error
  531. *
  532. * void bpf_set_hash_invalid(skb)
  533. * Invalidate current skb->hash.
  534. * @skb: pointer to skb
  535. *
  536. * int bpf_get_numa_node_id()
  537. * Return: Id of current NUMA node.
  538. *
  539. * int bpf_skb_change_head()
  540. * Grows headroom of skb and adjusts MAC header offset accordingly.
  541. * Will extends/reallocae as required automatically.
  542. * May change skb data pointer and will thus invalidate any check
  543. * performed for direct packet access.
  544. * @skb: pointer to skb
  545. * @len: length of header to be pushed in front
  546. * @flags: Flags (unused for now)
  547. * Return: 0 on success or negative error
  548. *
  549. * int bpf_xdp_adjust_head(xdp_md, delta)
  550. * Adjust the xdp_md.data by delta
  551. * @xdp_md: pointer to xdp_md
  552. * @delta: An positive/negative integer to be added to xdp_md.data
  553. * Return: 0 on success or negative on error
  554. *
  555. * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
  556. * Copy a NUL terminated string from unsafe address. In case the string
  557. * length is smaller than size, the target is not padded with further NUL
  558. * bytes. In case the string length is larger than size, just count-1
  559. * bytes are copied and the last byte is set to NUL.
  560. * @dst: destination address
  561. * @size: maximum number of bytes to copy, including the trailing NUL
  562. * @unsafe_ptr: unsafe address
  563. * Return:
  564. * > 0 length of the string including the trailing NUL on success
  565. * < 0 error
  566. *
  567. * u64 bpf_get_socket_cookie(skb)
  568. * Get the cookie for the socket stored inside sk_buff.
  569. * @skb: pointer to skb
  570. * Return: 8 Bytes non-decreasing number on success or 0 if the socket
  571. * field is missing inside sk_buff
  572. *
  573. * u32 bpf_get_socket_uid(skb)
  574. * Get the owner uid of the socket stored inside sk_buff.
  575. * @skb: pointer to skb
  576. * Return: uid of the socket owner on success or overflowuid if failed.
  577. *
  578. * u32 bpf_set_hash(skb, hash)
  579. * Set full skb->hash.
  580. * @skb: pointer to skb
  581. * @hash: hash to set
  582. *
  583. * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen)
  584. * Calls setsockopt. Not all opts are available, only those with
  585. * integer optvals plus TCP_CONGESTION.
  586. * Supported levels: SOL_SOCKET and IPPROTO_TCP
  587. * @bpf_socket: pointer to bpf_socket
  588. * @level: SOL_SOCKET or IPPROTO_TCP
  589. * @optname: option name
  590. * @optval: pointer to option value
  591. * @optlen: length of optval in bytes
  592. * Return: 0 or negative error
  593. *
  594. * int bpf_getsockopt(bpf_socket, level, optname, optval, optlen)
  595. * Calls getsockopt. Not all opts are available.
  596. * Supported levels: IPPROTO_TCP
  597. * @bpf_socket: pointer to bpf_socket
  598. * @level: IPPROTO_TCP
  599. * @optname: option name
  600. * @optval: pointer to option value
  601. * @optlen: length of optval in bytes
  602. * Return: 0 or negative error
  603. *
  604. * int bpf_skb_adjust_room(skb, len_diff, mode, flags)
  605. * Grow or shrink room in sk_buff.
  606. * @skb: pointer to skb
  607. * @len_diff: (signed) amount of room to grow/shrink
  608. * @mode: operation mode (enum bpf_adj_room_mode)
  609. * @flags: reserved for future use
  610. * Return: 0 on success or negative error code
  611. *
  612. * int bpf_sk_redirect_map(map, key, flags)
  613. * Redirect skb to a sock in map using key as a lookup key for the
  614. * sock in map.
  615. * @map: pointer to sockmap
  616. * @key: key to lookup sock in map
  617. * @flags: reserved for future use
  618. * Return: SK_PASS
  619. *
  620. * int bpf_sock_map_update(skops, map, key, flags)
  621. * @skops: pointer to bpf_sock_ops
  622. * @map: pointer to sockmap to update
  623. * @key: key to insert/update sock in map
  624. * @flags: same flags as map update elem
  625. *
  626. * int bpf_xdp_adjust_meta(xdp_md, delta)
  627. * Adjust the xdp_md.data_meta by delta
  628. * @xdp_md: pointer to xdp_md
  629. * @delta: An positive/negative integer to be added to xdp_md.data_meta
  630. * Return: 0 on success or negative on error
  631. *
  632. * int bpf_perf_event_read_value(map, flags, buf, buf_size)
  633. * read perf event counter value and perf event enabled/running time
  634. * @map: pointer to perf_event_array map
  635. * @flags: index of event in the map or bitmask flags
  636. * @buf: buf to fill
  637. * @buf_size: size of the buf
  638. * Return: 0 on success or negative error code
  639. *
  640. * int bpf_perf_prog_read_value(ctx, buf, buf_size)
  641. * read perf prog attached perf event counter and enabled/running time
  642. * @ctx: pointer to ctx
  643. * @buf: buf to fill
  644. * @buf_size: size of the buf
  645. * Return : 0 on success or negative error code
  646. */
  647. #define __BPF_FUNC_MAPPER(FN) \
  648. FN(unspec), \
  649. FN(map_lookup_elem), \
  650. FN(map_update_elem), \
  651. FN(map_delete_elem), \
  652. FN(probe_read), \
  653. FN(ktime_get_ns), \
  654. FN(trace_printk), \
  655. FN(get_prandom_u32), \
  656. FN(get_smp_processor_id), \
  657. FN(skb_store_bytes), \
  658. FN(l3_csum_replace), \
  659. FN(l4_csum_replace), \
  660. FN(tail_call), \
  661. FN(clone_redirect), \
  662. FN(get_current_pid_tgid), \
  663. FN(get_current_uid_gid), \
  664. FN(get_current_comm), \
  665. FN(get_cgroup_classid), \
  666. FN(skb_vlan_push), \
  667. FN(skb_vlan_pop), \
  668. FN(skb_get_tunnel_key), \
  669. FN(skb_set_tunnel_key), \
  670. FN(perf_event_read), \
  671. FN(redirect), \
  672. FN(get_route_realm), \
  673. FN(perf_event_output), \
  674. FN(skb_load_bytes), \
  675. FN(get_stackid), \
  676. FN(csum_diff), \
  677. FN(skb_get_tunnel_opt), \
  678. FN(skb_set_tunnel_opt), \
  679. FN(skb_change_proto), \
  680. FN(skb_change_type), \
  681. FN(skb_under_cgroup), \
  682. FN(get_hash_recalc), \
  683. FN(get_current_task), \
  684. FN(probe_write_user), \
  685. FN(current_task_under_cgroup), \
  686. FN(skb_change_tail), \
  687. FN(skb_pull_data), \
  688. FN(csum_update), \
  689. FN(set_hash_invalid), \
  690. FN(get_numa_node_id), \
  691. FN(skb_change_head), \
  692. FN(xdp_adjust_head), \
  693. FN(probe_read_str), \
  694. FN(get_socket_cookie), \
  695. FN(get_socket_uid), \
  696. FN(set_hash), \
  697. FN(setsockopt), \
  698. FN(skb_adjust_room), \
  699. FN(redirect_map), \
  700. FN(sk_redirect_map), \
  701. FN(sock_map_update), \
  702. FN(xdp_adjust_meta), \
  703. FN(perf_event_read_value), \
  704. FN(perf_prog_read_value), \
  705. FN(getsockopt),
  706. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  707. * function eBPF program intends to call
  708. */
  709. #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
  710. enum bpf_func_id {
  711. __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
  712. __BPF_FUNC_MAX_ID,
  713. };
  714. #undef __BPF_ENUM_FN
  715. /* All flags used by eBPF helper functions, placed here. */
  716. /* BPF_FUNC_skb_store_bytes flags. */
  717. #define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
  718. #define BPF_F_INVALIDATE_HASH (1ULL << 1)
  719. /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
  720. * First 4 bits are for passing the header field size.
  721. */
  722. #define BPF_F_HDR_FIELD_MASK 0xfULL
  723. /* BPF_FUNC_l4_csum_replace flags. */
  724. #define BPF_F_PSEUDO_HDR (1ULL << 4)
  725. #define BPF_F_MARK_MANGLED_0 (1ULL << 5)
  726. #define BPF_F_MARK_ENFORCE (1ULL << 6)
  727. /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
  728. #define BPF_F_INGRESS (1ULL << 0)
  729. /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
  730. #define BPF_F_TUNINFO_IPV6 (1ULL << 0)
  731. /* BPF_FUNC_get_stackid flags. */
  732. #define BPF_F_SKIP_FIELD_MASK 0xffULL
  733. #define BPF_F_USER_STACK (1ULL << 8)
  734. #define BPF_F_FAST_STACK_CMP (1ULL << 9)
  735. #define BPF_F_REUSE_STACKID (1ULL << 10)
  736. /* BPF_FUNC_skb_set_tunnel_key flags. */
  737. #define BPF_F_ZERO_CSUM_TX (1ULL << 1)
  738. #define BPF_F_DONT_FRAGMENT (1ULL << 2)
  739. /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and
  740. * BPF_FUNC_perf_event_read_value flags.
  741. */
  742. #define BPF_F_INDEX_MASK 0xffffffffULL
  743. #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
  744. /* BPF_FUNC_perf_event_output for sk_buff input context. */
  745. #define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
  746. /* Mode for BPF_FUNC_skb_adjust_room helper. */
  747. enum bpf_adj_room_mode {
  748. BPF_ADJ_ROOM_NET,
  749. };
  750. /* user accessible mirror of in-kernel sk_buff.
  751. * new fields can only be added to the end of this structure
  752. */
  753. struct __sk_buff {
  754. __u32 len;
  755. __u32 pkt_type;
  756. __u32 mark;
  757. __u32 queue_mapping;
  758. __u32 protocol;
  759. __u32 vlan_present;
  760. __u32 vlan_tci;
  761. __u32 vlan_proto;
  762. __u32 priority;
  763. __u32 ingress_ifindex;
  764. __u32 ifindex;
  765. __u32 tc_index;
  766. __u32 cb[5];
  767. __u32 hash;
  768. __u32 tc_classid;
  769. __u32 data;
  770. __u32 data_end;
  771. __u32 napi_id;
  772. /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */
  773. __u32 family;
  774. __u32 remote_ip4; /* Stored in network byte order */
  775. __u32 local_ip4; /* Stored in network byte order */
  776. __u32 remote_ip6[4]; /* Stored in network byte order */
  777. __u32 local_ip6[4]; /* Stored in network byte order */
  778. __u32 remote_port; /* Stored in network byte order */
  779. __u32 local_port; /* stored in host byte order */
  780. /* ... here. */
  781. __u32 data_meta;
  782. };
  783. struct bpf_tunnel_key {
  784. __u32 tunnel_id;
  785. union {
  786. __u32 remote_ipv4;
  787. __u32 remote_ipv6[4];
  788. };
  789. __u8 tunnel_tos;
  790. __u8 tunnel_ttl;
  791. __u16 tunnel_ext;
  792. __u32 tunnel_label;
  793. };
  794. /* Generic BPF return codes which all BPF program types may support.
  795. * The values are binary compatible with their TC_ACT_* counter-part to
  796. * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
  797. * programs.
  798. *
  799. * XDP is handled seprately, see XDP_*.
  800. */
  801. enum bpf_ret_code {
  802. BPF_OK = 0,
  803. /* 1 reserved */
  804. BPF_DROP = 2,
  805. /* 3-6 reserved */
  806. BPF_REDIRECT = 7,
  807. /* >127 are reserved for prog type specific return codes */
  808. };
  809. struct bpf_sock {
  810. __u32 bound_dev_if;
  811. __u32 family;
  812. __u32 type;
  813. __u32 protocol;
  814. __u32 mark;
  815. __u32 priority;
  816. };
  817. #define XDP_PACKET_HEADROOM 256
  818. /* User return codes for XDP prog type.
  819. * A valid XDP program must return one of these defined values. All other
  820. * return codes are reserved for future use. Unknown return codes will
  821. * result in packet drops and a warning via bpf_warn_invalid_xdp_action().
  822. */
  823. enum xdp_action {
  824. XDP_ABORTED = 0,
  825. XDP_DROP,
  826. XDP_PASS,
  827. XDP_TX,
  828. XDP_REDIRECT,
  829. };
  830. /* user accessible metadata for XDP packet hook
  831. * new fields must be added to the end of this structure
  832. */
  833. struct xdp_md {
  834. __u32 data;
  835. __u32 data_end;
  836. __u32 data_meta;
  837. };
  838. enum sk_action {
  839. SK_DROP = 0,
  840. SK_PASS,
  841. };
  842. #define BPF_TAG_SIZE 8
  843. struct bpf_prog_info {
  844. __u32 type;
  845. __u32 id;
  846. __u8 tag[BPF_TAG_SIZE];
  847. __u32 jited_prog_len;
  848. __u32 xlated_prog_len;
  849. __aligned_u64 jited_prog_insns;
  850. __aligned_u64 xlated_prog_insns;
  851. __u64 load_time; /* ns since boottime */
  852. __u32 created_by_uid;
  853. __u32 nr_map_ids;
  854. __aligned_u64 map_ids;
  855. char name[BPF_OBJ_NAME_LEN];
  856. } __attribute__((aligned(8)));
  857. struct bpf_map_info {
  858. __u32 type;
  859. __u32 id;
  860. __u32 key_size;
  861. __u32 value_size;
  862. __u32 max_entries;
  863. __u32 map_flags;
  864. char name[BPF_OBJ_NAME_LEN];
  865. } __attribute__((aligned(8)));
  866. /* User bpf_sock_ops struct to access socket values and specify request ops
  867. * and their replies.
  868. * Some of this fields are in network (bigendian) byte order and may need
  869. * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h).
  870. * New fields can only be added at the end of this structure
  871. */
  872. struct bpf_sock_ops {
  873. __u32 op;
  874. union {
  875. __u32 reply;
  876. __u32 replylong[4];
  877. };
  878. __u32 family;
  879. __u32 remote_ip4; /* Stored in network byte order */
  880. __u32 local_ip4; /* Stored in network byte order */
  881. __u32 remote_ip6[4]; /* Stored in network byte order */
  882. __u32 local_ip6[4]; /* Stored in network byte order */
  883. __u32 remote_port; /* Stored in network byte order */
  884. __u32 local_port; /* stored in host byte order */
  885. };
  886. /* List of known BPF sock_ops operators.
  887. * New entries can only be added at the end
  888. */
  889. enum {
  890. BPF_SOCK_OPS_VOID,
  891. BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or
  892. * -1 if default value should be used
  893. */
  894. BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized
  895. * window (in packets) or -1 if default
  896. * value should be used
  897. */
  898. BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an
  899. * active connection is initialized
  900. */
  901. BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an
  902. * active connection is
  903. * established
  904. */
  905. BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a
  906. * passive connection is
  907. * established
  908. */
  909. BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control
  910. * needs ECN
  911. */
  912. BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is
  913. * based on the path and may be
  914. * dependent on the congestion control
  915. * algorithm. In general it indicates
  916. * a congestion threshold. RTTs above
  917. * this indicate congestion
  918. */
  919. };
  920. #define TCP_BPF_IW 1001 /* Set TCP initial congestion window */
  921. #define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */
  922. struct bpf_perf_event_value {
  923. __u64 counter;
  924. __u64 enabled;
  925. __u64 running;
  926. };
  927. #define BPF_DEVCG_ACC_MKNOD (1ULL << 0)
  928. #define BPF_DEVCG_ACC_READ (1ULL << 1)
  929. #define BPF_DEVCG_ACC_WRITE (1ULL << 2)
  930. #define BPF_DEVCG_DEV_BLOCK (1ULL << 0)
  931. #define BPF_DEVCG_DEV_CHAR (1ULL << 1)
  932. struct bpf_cgroup_dev_ctx {
  933. __u32 access_type; /* (access << 16) | type */
  934. __u32 major;
  935. __u32 minor;
  936. };
  937. #endif /* _UAPI__LINUX_BPF_H__ */