bpf.h 38 KB

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