bpf.h 26 KB

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