bpf.h 30 KB

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