bpf.h 28 KB

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