bpf.h 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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. BPF_BTF_GET_FD_BY_ID,
  88. };
  89. enum bpf_map_type {
  90. BPF_MAP_TYPE_UNSPEC,
  91. BPF_MAP_TYPE_HASH,
  92. BPF_MAP_TYPE_ARRAY,
  93. BPF_MAP_TYPE_PROG_ARRAY,
  94. BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  95. BPF_MAP_TYPE_PERCPU_HASH,
  96. BPF_MAP_TYPE_PERCPU_ARRAY,
  97. BPF_MAP_TYPE_STACK_TRACE,
  98. BPF_MAP_TYPE_CGROUP_ARRAY,
  99. BPF_MAP_TYPE_LRU_HASH,
  100. BPF_MAP_TYPE_LRU_PERCPU_HASH,
  101. BPF_MAP_TYPE_LPM_TRIE,
  102. BPF_MAP_TYPE_ARRAY_OF_MAPS,
  103. BPF_MAP_TYPE_HASH_OF_MAPS,
  104. BPF_MAP_TYPE_DEVMAP,
  105. BPF_MAP_TYPE_SOCKMAP,
  106. BPF_MAP_TYPE_CPUMAP,
  107. BPF_MAP_TYPE_XSKMAP,
  108. BPF_MAP_TYPE_SOCKHASH,
  109. };
  110. enum bpf_prog_type {
  111. BPF_PROG_TYPE_UNSPEC,
  112. BPF_PROG_TYPE_SOCKET_FILTER,
  113. BPF_PROG_TYPE_KPROBE,
  114. BPF_PROG_TYPE_SCHED_CLS,
  115. BPF_PROG_TYPE_SCHED_ACT,
  116. BPF_PROG_TYPE_TRACEPOINT,
  117. BPF_PROG_TYPE_XDP,
  118. BPF_PROG_TYPE_PERF_EVENT,
  119. BPF_PROG_TYPE_CGROUP_SKB,
  120. BPF_PROG_TYPE_CGROUP_SOCK,
  121. BPF_PROG_TYPE_LWT_IN,
  122. BPF_PROG_TYPE_LWT_OUT,
  123. BPF_PROG_TYPE_LWT_XMIT,
  124. BPF_PROG_TYPE_SOCK_OPS,
  125. BPF_PROG_TYPE_SK_SKB,
  126. BPF_PROG_TYPE_CGROUP_DEVICE,
  127. BPF_PROG_TYPE_SK_MSG,
  128. BPF_PROG_TYPE_RAW_TRACEPOINT,
  129. BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
  130. };
  131. enum bpf_attach_type {
  132. BPF_CGROUP_INET_INGRESS,
  133. BPF_CGROUP_INET_EGRESS,
  134. BPF_CGROUP_INET_SOCK_CREATE,
  135. BPF_CGROUP_SOCK_OPS,
  136. BPF_SK_SKB_STREAM_PARSER,
  137. BPF_SK_SKB_STREAM_VERDICT,
  138. BPF_CGROUP_DEVICE,
  139. BPF_SK_MSG_VERDICT,
  140. BPF_CGROUP_INET4_BIND,
  141. BPF_CGROUP_INET6_BIND,
  142. BPF_CGROUP_INET4_CONNECT,
  143. BPF_CGROUP_INET6_CONNECT,
  144. BPF_CGROUP_INET4_POST_BIND,
  145. BPF_CGROUP_INET6_POST_BIND,
  146. __MAX_BPF_ATTACH_TYPE
  147. };
  148. #define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
  149. /* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
  150. *
  151. * NONE(default): No further bpf programs allowed in the subtree.
  152. *
  153. * BPF_F_ALLOW_OVERRIDE: If a sub-cgroup installs some bpf program,
  154. * the program in this cgroup yields to sub-cgroup program.
  155. *
  156. * BPF_F_ALLOW_MULTI: If a sub-cgroup installs some bpf program,
  157. * that cgroup program gets run in addition to the program in this cgroup.
  158. *
  159. * Only one program is allowed to be attached to a cgroup with
  160. * NONE or BPF_F_ALLOW_OVERRIDE flag.
  161. * Attaching another program on top of NONE or BPF_F_ALLOW_OVERRIDE will
  162. * release old program and attach the new one. Attach flags has to match.
  163. *
  164. * Multiple programs are allowed to be attached to a cgroup with
  165. * BPF_F_ALLOW_MULTI flag. They are executed in FIFO order
  166. * (those that were attached first, run first)
  167. * The programs of sub-cgroup are executed first, then programs of
  168. * this cgroup and then programs of parent cgroup.
  169. * When children program makes decision (like picking TCP CA or sock bind)
  170. * parent program has a chance to override it.
  171. *
  172. * A cgroup with MULTI or OVERRIDE flag allows any attach flags in sub-cgroups.
  173. * A cgroup with NONE doesn't allow any programs in sub-cgroups.
  174. * Ex1:
  175. * cgrp1 (MULTI progs A, B) ->
  176. * cgrp2 (OVERRIDE prog C) ->
  177. * cgrp3 (MULTI prog D) ->
  178. * cgrp4 (OVERRIDE prog E) ->
  179. * cgrp5 (NONE prog F)
  180. * the event in cgrp5 triggers execution of F,D,A,B in that order.
  181. * if prog F is detached, the execution is E,D,A,B
  182. * if prog F and D are detached, the execution is E,A,B
  183. * if prog F, E and D are detached, the execution is C,A,B
  184. *
  185. * All eligible programs are executed regardless of return code from
  186. * earlier programs.
  187. */
  188. #define BPF_F_ALLOW_OVERRIDE (1U << 0)
  189. #define BPF_F_ALLOW_MULTI (1U << 1)
  190. /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
  191. * verifier will perform strict alignment checking as if the kernel
  192. * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
  193. * and NET_IP_ALIGN defined to 2.
  194. */
  195. #define BPF_F_STRICT_ALIGNMENT (1U << 0)
  196. /* when bpf_ldimm64->src_reg == BPF_PSEUDO_MAP_FD, bpf_ldimm64->imm == fd */
  197. #define BPF_PSEUDO_MAP_FD 1
  198. /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
  199. * offset to another bpf function
  200. */
  201. #define BPF_PSEUDO_CALL 1
  202. /* flags for BPF_MAP_UPDATE_ELEM command */
  203. #define BPF_ANY 0 /* create new element or update existing */
  204. #define BPF_NOEXIST 1 /* create new element if it didn't exist */
  205. #define BPF_EXIST 2 /* update existing element */
  206. /* flags for BPF_MAP_CREATE command */
  207. #define BPF_F_NO_PREALLOC (1U << 0)
  208. /* Instead of having one common LRU list in the
  209. * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
  210. * which can scale and perform better.
  211. * Note, the LRU nodes (including free nodes) cannot be moved
  212. * across different LRU lists.
  213. */
  214. #define BPF_F_NO_COMMON_LRU (1U << 1)
  215. /* Specify numa node during map creation */
  216. #define BPF_F_NUMA_NODE (1U << 2)
  217. /* flags for BPF_PROG_QUERY */
  218. #define BPF_F_QUERY_EFFECTIVE (1U << 0)
  219. #define BPF_OBJ_NAME_LEN 16U
  220. /* Flags for accessing BPF object */
  221. #define BPF_F_RDONLY (1U << 3)
  222. #define BPF_F_WRONLY (1U << 4)
  223. /* Flag for stack_map, store build_id+offset instead of pointer */
  224. #define BPF_F_STACK_BUILD_ID (1U << 5)
  225. enum bpf_stack_build_id_status {
  226. /* user space need an empty entry to identify end of a trace */
  227. BPF_STACK_BUILD_ID_EMPTY = 0,
  228. /* with valid build_id and offset */
  229. BPF_STACK_BUILD_ID_VALID = 1,
  230. /* couldn't get build_id, fallback to ip */
  231. BPF_STACK_BUILD_ID_IP = 2,
  232. };
  233. #define BPF_BUILD_ID_SIZE 20
  234. struct bpf_stack_build_id {
  235. __s32 status;
  236. unsigned char build_id[BPF_BUILD_ID_SIZE];
  237. union {
  238. __u64 offset;
  239. __u64 ip;
  240. };
  241. };
  242. union bpf_attr {
  243. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  244. __u32 map_type; /* one of enum bpf_map_type */
  245. __u32 key_size; /* size of key in bytes */
  246. __u32 value_size; /* size of value in bytes */
  247. __u32 max_entries; /* max number of entries in a map */
  248. __u32 map_flags; /* BPF_MAP_CREATE related
  249. * flags defined above.
  250. */
  251. __u32 inner_map_fd; /* fd pointing to the inner map */
  252. __u32 numa_node; /* numa node (effective only if
  253. * BPF_F_NUMA_NODE is set).
  254. */
  255. char map_name[BPF_OBJ_NAME_LEN];
  256. __u32 map_ifindex; /* ifindex of netdev to create on */
  257. __u32 btf_fd; /* fd pointing to a BTF type data */
  258. __u32 btf_key_type_id; /* BTF type_id of the key */
  259. __u32 btf_value_type_id; /* BTF type_id of the value */
  260. };
  261. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  262. __u32 map_fd;
  263. __aligned_u64 key;
  264. union {
  265. __aligned_u64 value;
  266. __aligned_u64 next_key;
  267. };
  268. __u64 flags;
  269. };
  270. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  271. __u32 prog_type; /* one of enum bpf_prog_type */
  272. __u32 insn_cnt;
  273. __aligned_u64 insns;
  274. __aligned_u64 license;
  275. __u32 log_level; /* verbosity level of verifier */
  276. __u32 log_size; /* size of user buffer */
  277. __aligned_u64 log_buf; /* user supplied buffer */
  278. __u32 kern_version; /* checked when prog_type=kprobe */
  279. __u32 prog_flags;
  280. char prog_name[BPF_OBJ_NAME_LEN];
  281. __u32 prog_ifindex; /* ifindex of netdev to prep for */
  282. /* For some prog types expected attach type must be known at
  283. * load time to verify attach type specific parts of prog
  284. * (context accesses, allowed helpers, etc).
  285. */
  286. __u32 expected_attach_type;
  287. };
  288. struct { /* anonymous struct used by BPF_OBJ_* commands */
  289. __aligned_u64 pathname;
  290. __u32 bpf_fd;
  291. __u32 file_flags;
  292. };
  293. struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
  294. __u32 target_fd; /* container object to attach to */
  295. __u32 attach_bpf_fd; /* eBPF program to attach */
  296. __u32 attach_type;
  297. __u32 attach_flags;
  298. };
  299. struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
  300. __u32 prog_fd;
  301. __u32 retval;
  302. __u32 data_size_in;
  303. __u32 data_size_out;
  304. __aligned_u64 data_in;
  305. __aligned_u64 data_out;
  306. __u32 repeat;
  307. __u32 duration;
  308. } test;
  309. struct { /* anonymous struct used by BPF_*_GET_*_ID */
  310. union {
  311. __u32 start_id;
  312. __u32 prog_id;
  313. __u32 map_id;
  314. __u32 btf_id;
  315. };
  316. __u32 next_id;
  317. __u32 open_flags;
  318. };
  319. struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
  320. __u32 bpf_fd;
  321. __u32 info_len;
  322. __aligned_u64 info;
  323. } info;
  324. struct { /* anonymous struct used by BPF_PROG_QUERY command */
  325. __u32 target_fd; /* container object to query */
  326. __u32 attach_type;
  327. __u32 query_flags;
  328. __u32 attach_flags;
  329. __aligned_u64 prog_ids;
  330. __u32 prog_cnt;
  331. } query;
  332. struct {
  333. __u64 name;
  334. __u32 prog_fd;
  335. } raw_tracepoint;
  336. struct { /* anonymous struct for BPF_BTF_LOAD */
  337. __aligned_u64 btf;
  338. __aligned_u64 btf_log_buf;
  339. __u32 btf_size;
  340. __u32 btf_log_size;
  341. __u32 btf_log_level;
  342. };
  343. } __attribute__((aligned(8)));
  344. /* The description below is an attempt at providing documentation to eBPF
  345. * developers about the multiple available eBPF helper functions. It can be
  346. * parsed and used to produce a manual page. The workflow is the following,
  347. * and requires the rst2man utility:
  348. *
  349. * $ ./scripts/bpf_helpers_doc.py \
  350. * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst
  351. * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7
  352. * $ man /tmp/bpf-helpers.7
  353. *
  354. * Note that in order to produce this external documentation, some RST
  355. * formatting is used in the descriptions to get "bold" and "italics" in
  356. * manual pages. Also note that the few trailing white spaces are
  357. * intentional, removing them would break paragraphs for rst2man.
  358. *
  359. * Start of BPF helper function descriptions:
  360. *
  361. * void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
  362. * Description
  363. * Perform a lookup in *map* for an entry associated to *key*.
  364. * Return
  365. * Map value associated to *key*, or **NULL** if no entry was
  366. * found.
  367. *
  368. * int bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)
  369. * Description
  370. * Add or update the value of the entry associated to *key* in
  371. * *map* with *value*. *flags* is one of:
  372. *
  373. * **BPF_NOEXIST**
  374. * The entry for *key* must not exist in the map.
  375. * **BPF_EXIST**
  376. * The entry for *key* must already exist in the map.
  377. * **BPF_ANY**
  378. * No condition on the existence of the entry for *key*.
  379. *
  380. * Flag value **BPF_NOEXIST** cannot be used for maps of types
  381. * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all
  382. * elements always exist), the helper would return an error.
  383. * Return
  384. * 0 on success, or a negative error in case of failure.
  385. *
  386. * int bpf_map_delete_elem(struct bpf_map *map, const void *key)
  387. * Description
  388. * Delete entry with *key* from *map*.
  389. * Return
  390. * 0 on success, or a negative error in case of failure.
  391. *
  392. * int bpf_probe_read(void *dst, u32 size, const void *src)
  393. * Description
  394. * For tracing programs, safely attempt to read *size* bytes from
  395. * address *src* and store the data in *dst*.
  396. * Return
  397. * 0 on success, or a negative error in case of failure.
  398. *
  399. * u64 bpf_ktime_get_ns(void)
  400. * Description
  401. * Return the time elapsed since system boot, in nanoseconds.
  402. * Return
  403. * Current *ktime*.
  404. *
  405. * int bpf_trace_printk(const char *fmt, u32 fmt_size, ...)
  406. * Description
  407. * This helper is a "printk()-like" facility for debugging. It
  408. * prints a message defined by format *fmt* (of size *fmt_size*)
  409. * to file *\/sys/kernel/debug/tracing/trace* from DebugFS, if
  410. * available. It can take up to three additional **u64**
  411. * arguments (as an eBPF helpers, the total number of arguments is
  412. * limited to five).
  413. *
  414. * Each time the helper is called, it appends a line to the trace.
  415. * The format of the trace is customizable, and the exact output
  416. * one will get depends on the options set in
  417. * *\/sys/kernel/debug/tracing/trace_options* (see also the
  418. * *README* file under the same directory). However, it usually
  419. * defaults to something like:
  420. *
  421. * ::
  422. *
  423. * telnet-470 [001] .N.. 419421.045894: 0x00000001: <formatted msg>
  424. *
  425. * In the above:
  426. *
  427. * * ``telnet`` is the name of the current task.
  428. * * ``470`` is the PID of the current task.
  429. * * ``001`` is the CPU number on which the task is
  430. * running.
  431. * * In ``.N..``, each character refers to a set of
  432. * options (whether irqs are enabled, scheduling
  433. * options, whether hard/softirqs are running, level of
  434. * preempt_disabled respectively). **N** means that
  435. * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED**
  436. * are set.
  437. * * ``419421.045894`` is a timestamp.
  438. * * ``0x00000001`` is a fake value used by BPF for the
  439. * instruction pointer register.
  440. * * ``<formatted msg>`` is the message formatted with
  441. * *fmt*.
  442. *
  443. * The conversion specifiers supported by *fmt* are similar, but
  444. * more limited than for printk(). They are **%d**, **%i**,
  445. * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**,
  446. * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size
  447. * of field, padding with zeroes, etc.) is available, and the
  448. * helper will return **-EINVAL** (but print nothing) if it
  449. * encounters an unknown specifier.
  450. *
  451. * Also, note that **bpf_trace_printk**\ () is slow, and should
  452. * only be used for debugging purposes. For this reason, a notice
  453. * bloc (spanning several lines) is printed to kernel logs and
  454. * states that the helper should not be used "for production use"
  455. * the first time this helper is used (or more precisely, when
  456. * **trace_printk**\ () buffers are allocated). For passing values
  457. * to user space, perf events should be preferred.
  458. * Return
  459. * The number of bytes written to the buffer, or a negative error
  460. * in case of failure.
  461. *
  462. * u32 bpf_get_prandom_u32(void)
  463. * Description
  464. * Get a pseudo-random number.
  465. *
  466. * From a security point of view, this helper uses its own
  467. * pseudo-random internal state, and cannot be used to infer the
  468. * seed of other random functions in the kernel. However, it is
  469. * essential to note that the generator used by the helper is not
  470. * cryptographically secure.
  471. * Return
  472. * A random 32-bit unsigned value.
  473. *
  474. * u32 bpf_get_smp_processor_id(void)
  475. * Description
  476. * Get the SMP (symmetric multiprocessing) processor id. Note that
  477. * all programs run with preemption disabled, which means that the
  478. * SMP processor id is stable during all the execution of the
  479. * program.
  480. * Return
  481. * The SMP id of the processor running the program.
  482. *
  483. * int bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags)
  484. * Description
  485. * Store *len* bytes from address *from* into the packet
  486. * associated to *skb*, at *offset*. *flags* are a combination of
  487. * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the
  488. * checksum for the packet after storing the bytes) and
  489. * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\
  490. * **->swhash** and *skb*\ **->l4hash** to 0).
  491. *
  492. * A call to this helper is susceptible to change the underlaying
  493. * packet buffer. Therefore, at load time, all checks on pointers
  494. * previously done by the verifier are invalidated and must be
  495. * performed again, if the helper is used in combination with
  496. * direct packet access.
  497. * Return
  498. * 0 on success, or a negative error in case of failure.
  499. *
  500. * int bpf_l3_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 size)
  501. * Description
  502. * Recompute the layer 3 (e.g. IP) checksum for the packet
  503. * associated to *skb*. Computation is incremental, so the helper
  504. * must know the former value of the header field that was
  505. * modified (*from*), the new value of this field (*to*), and the
  506. * number of bytes (2 or 4) for this field, stored in *size*.
  507. * Alternatively, it is possible to store the difference between
  508. * the previous and the new values of the header field in *to*, by
  509. * setting *from* and *size* to 0. For both methods, *offset*
  510. * indicates the location of the IP checksum within the packet.
  511. *
  512. * This helper works in combination with **bpf_csum_diff**\ (),
  513. * which does not update the checksum in-place, but offers more
  514. * flexibility and can handle sizes larger than 2 or 4 for the
  515. * checksum to update.
  516. *
  517. * A call to this helper is susceptible to change the underlaying
  518. * packet buffer. Therefore, at load time, all checks on pointers
  519. * previously done by the verifier are invalidated and must be
  520. * performed again, if the helper is used in combination with
  521. * direct packet access.
  522. * Return
  523. * 0 on success, or a negative error in case of failure.
  524. *
  525. * int bpf_l4_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 flags)
  526. * Description
  527. * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the
  528. * packet associated to *skb*. Computation is incremental, so the
  529. * helper must know the former value of the header field that was
  530. * modified (*from*), the new value of this field (*to*), and the
  531. * number of bytes (2 or 4) for this field, stored on the lowest
  532. * four bits of *flags*. Alternatively, it is possible to store
  533. * the difference between the previous and the new values of the
  534. * header field in *to*, by setting *from* and the four lowest
  535. * bits of *flags* to 0. For both methods, *offset* indicates the
  536. * location of the IP checksum within the packet. In addition to
  537. * the size of the field, *flags* can be added (bitwise OR) actual
  538. * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left
  539. * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and
  540. * for updates resulting in a null checksum the value is set to
  541. * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates
  542. * the checksum is to be computed against a pseudo-header.
  543. *
  544. * This helper works in combination with **bpf_csum_diff**\ (),
  545. * which does not update the checksum in-place, but offers more
  546. * flexibility and can handle sizes larger than 2 or 4 for the
  547. * checksum to update.
  548. *
  549. * A call to this helper is susceptible to change the underlaying
  550. * packet buffer. Therefore, at load time, all checks on pointers
  551. * previously done by the verifier are invalidated and must be
  552. * performed again, if the helper is used in combination with
  553. * direct packet access.
  554. * Return
  555. * 0 on success, or a negative error in case of failure.
  556. *
  557. * int bpf_tail_call(void *ctx, struct bpf_map *prog_array_map, u32 index)
  558. * Description
  559. * This special helper is used to trigger a "tail call", or in
  560. * other words, to jump into another eBPF program. The same stack
  561. * frame is used (but values on stack and in registers for the
  562. * caller are not accessible to the callee). This mechanism allows
  563. * for program chaining, either for raising the maximum number of
  564. * available eBPF instructions, or to execute given programs in
  565. * conditional blocks. For security reasons, there is an upper
  566. * limit to the number of successive tail calls that can be
  567. * performed.
  568. *
  569. * Upon call of this helper, the program attempts to jump into a
  570. * program referenced at index *index* in *prog_array_map*, a
  571. * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes
  572. * *ctx*, a pointer to the context.
  573. *
  574. * If the call succeeds, the kernel immediately runs the first
  575. * instruction of the new program. This is not a function call,
  576. * and it never returns to the previous program. If the call
  577. * fails, then the helper has no effect, and the caller continues
  578. * to run its subsequent instructions. A call can fail if the
  579. * destination program for the jump does not exist (i.e. *index*
  580. * is superior to the number of entries in *prog_array_map*), or
  581. * if the maximum number of tail calls has been reached for this
  582. * chain of programs. This limit is defined in the kernel by the
  583. * macro **MAX_TAIL_CALL_CNT** (not accessible to user space),
  584. * which is currently set to 32.
  585. * Return
  586. * 0 on success, or a negative error in case of failure.
  587. *
  588. * int bpf_clone_redirect(struct sk_buff *skb, u32 ifindex, u64 flags)
  589. * Description
  590. * Clone and redirect the packet associated to *skb* to another
  591. * net device of index *ifindex*. Both ingress and egress
  592. * interfaces can be used for redirection. The **BPF_F_INGRESS**
  593. * value in *flags* is used to make the distinction (ingress path
  594. * is selected if the flag is present, egress path otherwise).
  595. * This is the only flag supported for now.
  596. *
  597. * In comparison with **bpf_redirect**\ () helper,
  598. * **bpf_clone_redirect**\ () has the associated cost of
  599. * duplicating the packet buffer, but this can be executed out of
  600. * the eBPF program. Conversely, **bpf_redirect**\ () is more
  601. * efficient, but it is handled through an action code where the
  602. * redirection happens only after the eBPF program has returned.
  603. *
  604. * A call to this helper is susceptible to change the underlaying
  605. * packet buffer. Therefore, at load time, all checks on pointers
  606. * previously done by the verifier are invalidated and must be
  607. * performed again, if the helper is used in combination with
  608. * direct packet access.
  609. * Return
  610. * 0 on success, or a negative error in case of failure.
  611. *
  612. * u64 bpf_get_current_pid_tgid(void)
  613. * Return
  614. * A 64-bit integer containing the current tgid and pid, and
  615. * created as such:
  616. * *current_task*\ **->tgid << 32 \|**
  617. * *current_task*\ **->pid**.
  618. *
  619. * u64 bpf_get_current_uid_gid(void)
  620. * Return
  621. * A 64-bit integer containing the current GID and UID, and
  622. * created as such: *current_gid* **<< 32 \|** *current_uid*.
  623. *
  624. * int bpf_get_current_comm(char *buf, u32 size_of_buf)
  625. * Description
  626. * Copy the **comm** attribute of the current task into *buf* of
  627. * *size_of_buf*. The **comm** attribute contains the name of
  628. * the executable (excluding the path) for the current task. The
  629. * *size_of_buf* must be strictly positive. On success, the
  630. * helper makes sure that the *buf* is NUL-terminated. On failure,
  631. * it is filled with zeroes.
  632. * Return
  633. * 0 on success, or a negative error in case of failure.
  634. *
  635. * u32 bpf_get_cgroup_classid(struct sk_buff *skb)
  636. * Description
  637. * Retrieve the classid for the current task, i.e. for the net_cls
  638. * cgroup to which *skb* belongs.
  639. *
  640. * This helper can be used on TC egress path, but not on ingress.
  641. *
  642. * The net_cls cgroup provides an interface to tag network packets
  643. * based on a user-provided identifier for all traffic coming from
  644. * the tasks belonging to the related cgroup. See also the related
  645. * kernel documentation, available from the Linux sources in file
  646. * *Documentation/cgroup-v1/net_cls.txt*.
  647. *
  648. * The Linux kernel has two versions for cgroups: there are
  649. * cgroups v1 and cgroups v2. Both are available to users, who can
  650. * use a mixture of them, but note that the net_cls cgroup is for
  651. * cgroup v1 only. This makes it incompatible with BPF programs
  652. * run on cgroups, which is a cgroup-v2-only feature (a socket can
  653. * only hold data for one version of cgroups at a time).
  654. *
  655. * This helper is only available is the kernel was compiled with
  656. * the **CONFIG_CGROUP_NET_CLASSID** configuration option set to
  657. * "**y**" or to "**m**".
  658. * Return
  659. * The classid, or 0 for the default unconfigured classid.
  660. *
  661. * int bpf_skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
  662. * Description
  663. * Push a *vlan_tci* (VLAN tag control information) of protocol
  664. * *vlan_proto* to the packet associated to *skb*, then update
  665. * the checksum. Note that if *vlan_proto* is different from
  666. * **ETH_P_8021Q** and **ETH_P_8021AD**, it is considered to
  667. * be **ETH_P_8021Q**.
  668. *
  669. * A call to this helper is susceptible to change the underlaying
  670. * packet buffer. Therefore, at load time, all checks on pointers
  671. * previously done by the verifier are invalidated and must be
  672. * performed again, if the helper is used in combination with
  673. * direct packet access.
  674. * Return
  675. * 0 on success, or a negative error in case of failure.
  676. *
  677. * int bpf_skb_vlan_pop(struct sk_buff *skb)
  678. * Description
  679. * Pop a VLAN header from the packet associated to *skb*.
  680. *
  681. * A call to this helper is susceptible to change the underlaying
  682. * packet buffer. Therefore, at load time, all checks on pointers
  683. * previously done by the verifier are invalidated and must be
  684. * performed again, if the helper is used in combination with
  685. * direct packet access.
  686. * Return
  687. * 0 on success, or a negative error in case of failure.
  688. *
  689. * int bpf_skb_get_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags)
  690. * Description
  691. * Get tunnel metadata. This helper takes a pointer *key* to an
  692. * empty **struct bpf_tunnel_key** of **size**, that will be
  693. * filled with tunnel metadata for the packet associated to *skb*.
  694. * The *flags* can be set to **BPF_F_TUNINFO_IPV6**, which
  695. * indicates that the tunnel is based on IPv6 protocol instead of
  696. * IPv4.
  697. *
  698. * The **struct bpf_tunnel_key** is an object that generalizes the
  699. * principal parameters used by various tunneling protocols into a
  700. * single struct. This way, it can be used to easily make a
  701. * decision based on the contents of the encapsulation header,
  702. * "summarized" in this struct. In particular, it holds the IP
  703. * address of the remote end (IPv4 or IPv6, depending on the case)
  704. * in *key*\ **->remote_ipv4** or *key*\ **->remote_ipv6**. Also,
  705. * this struct exposes the *key*\ **->tunnel_id**, which is
  706. * generally mapped to a VNI (Virtual Network Identifier), making
  707. * it programmable together with the **bpf_skb_set_tunnel_key**\
  708. * () helper.
  709. *
  710. * Let's imagine that the following code is part of a program
  711. * attached to the TC ingress interface, on one end of a GRE
  712. * tunnel, and is supposed to filter out all messages coming from
  713. * remote ends with IPv4 address other than 10.0.0.1:
  714. *
  715. * ::
  716. *
  717. * int ret;
  718. * struct bpf_tunnel_key key = {};
  719. *
  720. * ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  721. * if (ret < 0)
  722. * return TC_ACT_SHOT; // drop packet
  723. *
  724. * if (key.remote_ipv4 != 0x0a000001)
  725. * return TC_ACT_SHOT; // drop packet
  726. *
  727. * return TC_ACT_OK; // accept packet
  728. *
  729. * This interface can also be used with all encapsulation devices
  730. * that can operate in "collect metadata" mode: instead of having
  731. * one network device per specific configuration, the "collect
  732. * metadata" mode only requires a single device where the
  733. * configuration can be extracted from this helper.
  734. *
  735. * This can be used together with various tunnels such as VXLan,
  736. * Geneve, GRE or IP in IP (IPIP).
  737. * Return
  738. * 0 on success, or a negative error in case of failure.
  739. *
  740. * int bpf_skb_set_tunnel_key(struct sk_buff *skb, struct bpf_tunnel_key *key, u32 size, u64 flags)
  741. * Description
  742. * Populate tunnel metadata for packet associated to *skb.* The
  743. * tunnel metadata is set to the contents of *key*, of *size*. The
  744. * *flags* can be set to a combination of the following values:
  745. *
  746. * **BPF_F_TUNINFO_IPV6**
  747. * Indicate that the tunnel is based on IPv6 protocol
  748. * instead of IPv4.
  749. * **BPF_F_ZERO_CSUM_TX**
  750. * For IPv4 packets, add a flag to tunnel metadata
  751. * indicating that checksum computation should be skipped
  752. * and checksum set to zeroes.
  753. * **BPF_F_DONT_FRAGMENT**
  754. * Add a flag to tunnel metadata indicating that the
  755. * packet should not be fragmented.
  756. * **BPF_F_SEQ_NUMBER**
  757. * Add a flag to tunnel metadata indicating that a
  758. * sequence number should be added to tunnel header before
  759. * sending the packet. This flag was added for GRE
  760. * encapsulation, but might be used with other protocols
  761. * as well in the future.
  762. *
  763. * Here is a typical usage on the transmit path:
  764. *
  765. * ::
  766. *
  767. * struct bpf_tunnel_key key;
  768. * populate key ...
  769. * bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0);
  770. * bpf_clone_redirect(skb, vxlan_dev_ifindex, 0);
  771. *
  772. * See also the description of the **bpf_skb_get_tunnel_key**\ ()
  773. * helper for additional information.
  774. * Return
  775. * 0 on success, or a negative error in case of failure.
  776. *
  777. * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags)
  778. * Description
  779. * Read the value of a perf event counter. This helper relies on a
  780. * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of
  781. * the perf event counter is selected when *map* is updated with
  782. * perf event file descriptors. The *map* is an array whose size
  783. * is the number of available CPUs, and each cell contains a value
  784. * relative to one CPU. The value to retrieve is indicated by
  785. * *flags*, that contains the index of the CPU to look up, masked
  786. * with **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to
  787. * **BPF_F_CURRENT_CPU** to indicate that the value for the
  788. * current CPU should be retrieved.
  789. *
  790. * Note that before Linux 4.13, only hardware perf event can be
  791. * retrieved.
  792. *
  793. * Also, be aware that the newer helper
  794. * **bpf_perf_event_read_value**\ () is recommended over
  795. * **bpf_perf_event_read**\ () in general. The latter has some ABI
  796. * quirks where error and counter value are used as a return code
  797. * (which is wrong to do since ranges may overlap). This issue is
  798. * fixed with **bpf_perf_event_read_value**\ (), which at the same
  799. * time provides more features over the **bpf_perf_event_read**\
  800. * () interface. Please refer to the description of
  801. * **bpf_perf_event_read_value**\ () for details.
  802. * Return
  803. * The value of the perf event counter read from the map, or a
  804. * negative error code in case of failure.
  805. *
  806. * int bpf_redirect(u32 ifindex, u64 flags)
  807. * Description
  808. * Redirect the packet to another net device of index *ifindex*.
  809. * This helper is somewhat similar to **bpf_clone_redirect**\
  810. * (), except that the packet is not cloned, which provides
  811. * increased performance.
  812. *
  813. * Except for XDP, both ingress and egress interfaces can be used
  814. * for redirection. The **BPF_F_INGRESS** value in *flags* is used
  815. * to make the distinction (ingress path is selected if the flag
  816. * is present, egress path otherwise). Currently, XDP only
  817. * supports redirection to the egress interface, and accepts no
  818. * flag at all.
  819. *
  820. * The same effect can be attained with the more generic
  821. * **bpf_redirect_map**\ (), which requires specific maps to be
  822. * used but offers better performance.
  823. * Return
  824. * For XDP, the helper returns **XDP_REDIRECT** on success or
  825. * **XDP_ABORTED** on error. For other program types, the values
  826. * are **TC_ACT_REDIRECT** on success or **TC_ACT_SHOT** on
  827. * error.
  828. *
  829. * u32 bpf_get_route_realm(struct sk_buff *skb)
  830. * Description
  831. * Retrieve the realm or the route, that is to say the
  832. * **tclassid** field of the destination for the *skb*. The
  833. * indentifier retrieved is a user-provided tag, similar to the
  834. * one used with the net_cls cgroup (see description for
  835. * **bpf_get_cgroup_classid**\ () helper), but here this tag is
  836. * held by a route (a destination entry), not by a task.
  837. *
  838. * Retrieving this identifier works with the clsact TC egress hook
  839. * (see also **tc-bpf(8)**), or alternatively on conventional
  840. * classful egress qdiscs, but not on TC ingress path. In case of
  841. * clsact TC egress hook, this has the advantage that, internally,
  842. * the destination entry has not been dropped yet in the transmit
  843. * path. Therefore, the destination entry does not need to be
  844. * artificially held via **netif_keep_dst**\ () for a classful
  845. * qdisc until the *skb* is freed.
  846. *
  847. * This helper is available only if the kernel was compiled with
  848. * **CONFIG_IP_ROUTE_CLASSID** configuration option.
  849. * Return
  850. * The realm of the route for the packet associated to *skb*, or 0
  851. * if none was found.
  852. *
  853. * int bpf_perf_event_output(struct pt_reg *ctx, struct bpf_map *map, u64 flags, void *data, u64 size)
  854. * Description
  855. * Write raw *data* blob into a special BPF perf event held by
  856. * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf
  857. * event must have the following attributes: **PERF_SAMPLE_RAW**
  858. * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and
  859. * **PERF_COUNT_SW_BPF_OUTPUT** as **config**.
  860. *
  861. * The *flags* are used to indicate the index in *map* for which
  862. * the value must be put, masked with **BPF_F_INDEX_MASK**.
  863. * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU**
  864. * to indicate that the index of the current CPU core should be
  865. * used.
  866. *
  867. * The value to write, of *size*, is passed through eBPF stack and
  868. * pointed by *data*.
  869. *
  870. * The context of the program *ctx* needs also be passed to the
  871. * helper.
  872. *
  873. * On user space, a program willing to read the values needs to
  874. * call **perf_event_open**\ () on the perf event (either for
  875. * one or for all CPUs) and to store the file descriptor into the
  876. * *map*. This must be done before the eBPF program can send data
  877. * into it. An example is available in file
  878. * *samples/bpf/trace_output_user.c* in the Linux kernel source
  879. * tree (the eBPF program counterpart is in
  880. * *samples/bpf/trace_output_kern.c*).
  881. *
  882. * **bpf_perf_event_output**\ () achieves better performance
  883. * than **bpf_trace_printk**\ () for sharing data with user
  884. * space, and is much better suitable for streaming data from eBPF
  885. * programs.
  886. *
  887. * Note that this helper is not restricted to tracing use cases
  888. * and can be used with programs attached to TC or XDP as well,
  889. * where it allows for passing data to user space listeners. Data
  890. * can be:
  891. *
  892. * * Only custom structs,
  893. * * Only the packet payload, or
  894. * * A combination of both.
  895. * Return
  896. * 0 on success, or a negative error in case of failure.
  897. *
  898. * int bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len)
  899. * Description
  900. * This helper was provided as an easy way to load data from a
  901. * packet. It can be used to load *len* bytes from *offset* from
  902. * the packet associated to *skb*, into the buffer pointed by
  903. * *to*.
  904. *
  905. * Since Linux 4.7, usage of this helper has mostly been replaced
  906. * by "direct packet access", enabling packet data to be
  907. * manipulated with *skb*\ **->data** and *skb*\ **->data_end**
  908. * pointing respectively to the first byte of packet data and to
  909. * the byte after the last byte of packet data. However, it
  910. * remains useful if one wishes to read large quantities of data
  911. * at once from a packet into the eBPF stack.
  912. * Return
  913. * 0 on success, or a negative error in case of failure.
  914. *
  915. * int bpf_get_stackid(struct pt_reg *ctx, struct bpf_map *map, u64 flags)
  916. * Description
  917. * Walk a user or a kernel stack and return its id. To achieve
  918. * this, the helper needs *ctx*, which is a pointer to the context
  919. * on which the tracing program is executed, and a pointer to a
  920. * *map* of type **BPF_MAP_TYPE_STACK_TRACE**.
  921. *
  922. * The last argument, *flags*, holds the number of stack frames to
  923. * skip (from 0 to 255), masked with
  924. * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set
  925. * a combination of the following flags:
  926. *
  927. * **BPF_F_USER_STACK**
  928. * Collect a user space stack instead of a kernel stack.
  929. * **BPF_F_FAST_STACK_CMP**
  930. * Compare stacks by hash only.
  931. * **BPF_F_REUSE_STACKID**
  932. * If two different stacks hash into the same *stackid*,
  933. * discard the old one.
  934. *
  935. * The stack id retrieved is a 32 bit long integer handle which
  936. * can be further combined with other data (including other stack
  937. * ids) and used as a key into maps. This can be useful for
  938. * generating a variety of graphs (such as flame graphs or off-cpu
  939. * graphs).
  940. *
  941. * For walking a stack, this helper is an improvement over
  942. * **bpf_probe_read**\ (), which can be used with unrolled loops
  943. * but is not efficient and consumes a lot of eBPF instructions.
  944. * Instead, **bpf_get_stackid**\ () can collect up to
  945. * **PERF_MAX_STACK_DEPTH** both kernel and user frames. Note that
  946. * this limit can be controlled with the **sysctl** program, and
  947. * that it should be manually increased in order to profile long
  948. * user stacks (such as stacks for Java programs). To do so, use:
  949. *
  950. * ::
  951. *
  952. * # sysctl kernel.perf_event_max_stack=<new value>
  953. *
  954. * Return
  955. * The positive or null stack id on success, or a negative error
  956. * in case of failure.
  957. *
  958. * s64 bpf_csum_diff(__be32 *from, u32 from_size, __be32 *to, u32 to_size, __wsum seed)
  959. * Description
  960. * Compute a checksum difference, from the raw buffer pointed by
  961. * *from*, of length *from_size* (that must be a multiple of 4),
  962. * towards the raw buffer pointed by *to*, of size *to_size*
  963. * (same remark). An optional *seed* can be added to the value
  964. * (this can be cascaded, the seed may come from a previous call
  965. * to the helper).
  966. *
  967. * This is flexible enough to be used in several ways:
  968. *
  969. * * With *from_size* == 0, *to_size* > 0 and *seed* set to
  970. * checksum, it can be used when pushing new data.
  971. * * With *from_size* > 0, *to_size* == 0 and *seed* set to
  972. * checksum, it can be used when removing data from a packet.
  973. * * With *from_size* > 0, *to_size* > 0 and *seed* set to 0, it
  974. * can be used to compute a diff. Note that *from_size* and
  975. * *to_size* do not need to be equal.
  976. *
  977. * This helper can be used in combination with
  978. * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ (), to
  979. * which one can feed in the difference computed with
  980. * **bpf_csum_diff**\ ().
  981. * Return
  982. * The checksum result, or a negative error code in case of
  983. * failure.
  984. *
  985. * int bpf_skb_get_tunnel_opt(struct sk_buff *skb, u8 *opt, u32 size)
  986. * Description
  987. * Retrieve tunnel options metadata for the packet associated to
  988. * *skb*, and store the raw tunnel option data to the buffer *opt*
  989. * of *size*.
  990. *
  991. * This helper can be used with encapsulation devices that can
  992. * operate in "collect metadata" mode (please refer to the related
  993. * note in the description of **bpf_skb_get_tunnel_key**\ () for
  994. * more details). A particular example where this can be used is
  995. * in combination with the Geneve encapsulation protocol, where it
  996. * allows for pushing (with **bpf_skb_get_tunnel_opt**\ () helper)
  997. * and retrieving arbitrary TLVs (Type-Length-Value headers) from
  998. * the eBPF program. This allows for full customization of these
  999. * headers.
  1000. * Return
  1001. * The size of the option data retrieved.
  1002. *
  1003. * int bpf_skb_set_tunnel_opt(struct sk_buff *skb, u8 *opt, u32 size)
  1004. * Description
  1005. * Set tunnel options metadata for the packet associated to *skb*
  1006. * to the option data contained in the raw buffer *opt* of *size*.
  1007. *
  1008. * See also the description of the **bpf_skb_get_tunnel_opt**\ ()
  1009. * helper for additional information.
  1010. * Return
  1011. * 0 on success, or a negative error in case of failure.
  1012. *
  1013. * int bpf_skb_change_proto(struct sk_buff *skb, __be16 proto, u64 flags)
  1014. * Description
  1015. * Change the protocol of the *skb* to *proto*. Currently
  1016. * supported are transition from IPv4 to IPv6, and from IPv6 to
  1017. * IPv4. The helper takes care of the groundwork for the
  1018. * transition, including resizing the socket buffer. The eBPF
  1019. * program is expected to fill the new headers, if any, via
  1020. * **skb_store_bytes**\ () and to recompute the checksums with
  1021. * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\
  1022. * (). The main case for this helper is to perform NAT64
  1023. * operations out of an eBPF program.
  1024. *
  1025. * Internally, the GSO type is marked as dodgy so that headers are
  1026. * checked and segments are recalculated by the GSO/GRO engine.
  1027. * The size for GSO target is adapted as well.
  1028. *
  1029. * All values for *flags* are reserved for future usage, and must
  1030. * be left at zero.
  1031. *
  1032. * A call to this helper is susceptible to change the underlaying
  1033. * packet buffer. Therefore, at load time, all checks on pointers
  1034. * previously done by the verifier are invalidated and must be
  1035. * performed again, if the helper is used in combination with
  1036. * direct packet access.
  1037. * Return
  1038. * 0 on success, or a negative error in case of failure.
  1039. *
  1040. * int bpf_skb_change_type(struct sk_buff *skb, u32 type)
  1041. * Description
  1042. * Change the packet type for the packet associated to *skb*. This
  1043. * comes down to setting *skb*\ **->pkt_type** to *type*, except
  1044. * the eBPF program does not have a write access to *skb*\
  1045. * **->pkt_type** beside this helper. Using a helper here allows
  1046. * for graceful handling of errors.
  1047. *
  1048. * The major use case is to change incoming *skb*s to
  1049. * **PACKET_HOST** in a programmatic way instead of having to
  1050. * recirculate via **redirect**\ (..., **BPF_F_INGRESS**), for
  1051. * example.
  1052. *
  1053. * Note that *type* only allows certain values. At this time, they
  1054. * are:
  1055. *
  1056. * **PACKET_HOST**
  1057. * Packet is for us.
  1058. * **PACKET_BROADCAST**
  1059. * Send packet to all.
  1060. * **PACKET_MULTICAST**
  1061. * Send packet to group.
  1062. * **PACKET_OTHERHOST**
  1063. * Send packet to someone else.
  1064. * Return
  1065. * 0 on success, or a negative error in case of failure.
  1066. *
  1067. * int bpf_skb_under_cgroup(struct sk_buff *skb, struct bpf_map *map, u32 index)
  1068. * Description
  1069. * Check whether *skb* is a descendant of the cgroup2 held by
  1070. * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*.
  1071. * Return
  1072. * The return value depends on the result of the test, and can be:
  1073. *
  1074. * * 0, if the *skb* failed the cgroup2 descendant test.
  1075. * * 1, if the *skb* succeeded the cgroup2 descendant test.
  1076. * * A negative error code, if an error occurred.
  1077. *
  1078. * u32 bpf_get_hash_recalc(struct sk_buff *skb)
  1079. * Description
  1080. * Retrieve the hash of the packet, *skb*\ **->hash**. If it is
  1081. * not set, in particular if the hash was cleared due to mangling,
  1082. * recompute this hash. Later accesses to the hash can be done
  1083. * directly with *skb*\ **->hash**.
  1084. *
  1085. * Calling **bpf_set_hash_invalid**\ (), changing a packet
  1086. * prototype with **bpf_skb_change_proto**\ (), or calling
  1087. * **bpf_skb_store_bytes**\ () with the
  1088. * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear
  1089. * the hash and to trigger a new computation for the next call to
  1090. * **bpf_get_hash_recalc**\ ().
  1091. * Return
  1092. * The 32-bit hash.
  1093. *
  1094. * u64 bpf_get_current_task(void)
  1095. * Return
  1096. * A pointer to the current task struct.
  1097. *
  1098. * int bpf_probe_write_user(void *dst, const void *src, u32 len)
  1099. * Description
  1100. * Attempt in a safe way to write *len* bytes from the buffer
  1101. * *src* to *dst* in memory. It only works for threads that are in
  1102. * user context, and *dst* must be a valid user space address.
  1103. *
  1104. * This helper should not be used to implement any kind of
  1105. * security mechanism because of TOC-TOU attacks, but rather to
  1106. * debug, divert, and manipulate execution of semi-cooperative
  1107. * processes.
  1108. *
  1109. * Keep in mind that this feature is meant for experiments, and it
  1110. * has a risk of crashing the system and running programs.
  1111. * Therefore, when an eBPF program using this helper is attached,
  1112. * a warning including PID and process name is printed to kernel
  1113. * logs.
  1114. * Return
  1115. * 0 on success, or a negative error in case of failure.
  1116. *
  1117. * int bpf_current_task_under_cgroup(struct bpf_map *map, u32 index)
  1118. * Description
  1119. * Check whether the probe is being run is the context of a given
  1120. * subset of the cgroup2 hierarchy. The cgroup2 to test is held by
  1121. * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*.
  1122. * Return
  1123. * The return value depends on the result of the test, and can be:
  1124. *
  1125. * * 0, if the *skb* task belongs to the cgroup2.
  1126. * * 1, if the *skb* task does not belong to the cgroup2.
  1127. * * A negative error code, if an error occurred.
  1128. *
  1129. * int bpf_skb_change_tail(struct sk_buff *skb, u32 len, u64 flags)
  1130. * Description
  1131. * Resize (trim or grow) the packet associated to *skb* to the
  1132. * new *len*. The *flags* are reserved for future usage, and must
  1133. * be left at zero.
  1134. *
  1135. * The basic idea is that the helper performs the needed work to
  1136. * change the size of the packet, then the eBPF program rewrites
  1137. * the rest via helpers like **bpf_skb_store_bytes**\ (),
  1138. * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ ()
  1139. * and others. This helper is a slow path utility intended for
  1140. * replies with control messages. And because it is targeted for
  1141. * slow path, the helper itself can afford to be slow: it
  1142. * implicitly linearizes, unclones and drops offloads from the
  1143. * *skb*.
  1144. *
  1145. * A call to this helper is susceptible to change the underlaying
  1146. * packet buffer. Therefore, at load time, all checks on pointers
  1147. * previously done by the verifier are invalidated and must be
  1148. * performed again, if the helper is used in combination with
  1149. * direct packet access.
  1150. * Return
  1151. * 0 on success, or a negative error in case of failure.
  1152. *
  1153. * int bpf_skb_pull_data(struct sk_buff *skb, u32 len)
  1154. * Description
  1155. * Pull in non-linear data in case the *skb* is non-linear and not
  1156. * all of *len* are part of the linear section. Make *len* bytes
  1157. * from *skb* readable and writable. If a zero value is passed for
  1158. * *len*, then the whole length of the *skb* is pulled.
  1159. *
  1160. * This helper is only needed for reading and writing with direct
  1161. * packet access.
  1162. *
  1163. * For direct packet access, testing that offsets to access
  1164. * are within packet boundaries (test on *skb*\ **->data_end**) is
  1165. * susceptible to fail if offsets are invalid, or if the requested
  1166. * data is in non-linear parts of the *skb*. On failure the
  1167. * program can just bail out, or in the case of a non-linear
  1168. * buffer, use a helper to make the data available. The
  1169. * **bpf_skb_load_bytes**\ () helper is a first solution to access
  1170. * the data. Another one consists in using **bpf_skb_pull_data**
  1171. * to pull in once the non-linear parts, then retesting and
  1172. * eventually access the data.
  1173. *
  1174. * At the same time, this also makes sure the *skb* is uncloned,
  1175. * which is a necessary condition for direct write. As this needs
  1176. * to be an invariant for the write part only, the verifier
  1177. * detects writes and adds a prologue that is calling
  1178. * **bpf_skb_pull_data()** to effectively unclone the *skb* from
  1179. * the very beginning in case it is indeed cloned.
  1180. *
  1181. * A call to this helper is susceptible to change the underlaying
  1182. * packet buffer. Therefore, at load time, all checks on pointers
  1183. * previously done by the verifier are invalidated and must be
  1184. * performed again, if the helper is used in combination with
  1185. * direct packet access.
  1186. * Return
  1187. * 0 on success, or a negative error in case of failure.
  1188. *
  1189. * s64 bpf_csum_update(struct sk_buff *skb, __wsum csum)
  1190. * Description
  1191. * Add the checksum *csum* into *skb*\ **->csum** in case the
  1192. * driver has supplied a checksum for the entire packet into that
  1193. * field. Return an error otherwise. This helper is intended to be
  1194. * used in combination with **bpf_csum_diff**\ (), in particular
  1195. * when the checksum needs to be updated after data has been
  1196. * written into the packet through direct packet access.
  1197. * Return
  1198. * The checksum on success, or a negative error code in case of
  1199. * failure.
  1200. *
  1201. * void bpf_set_hash_invalid(struct sk_buff *skb)
  1202. * Description
  1203. * Invalidate the current *skb*\ **->hash**. It can be used after
  1204. * mangling on headers through direct packet access, in order to
  1205. * indicate that the hash is outdated and to trigger a
  1206. * recalculation the next time the kernel tries to access this
  1207. * hash or when the **bpf_get_hash_recalc**\ () helper is called.
  1208. *
  1209. * int bpf_get_numa_node_id(void)
  1210. * Description
  1211. * Return the id of the current NUMA node. The primary use case
  1212. * for this helper is the selection of sockets for the local NUMA
  1213. * node, when the program is attached to sockets using the
  1214. * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**),
  1215. * but the helper is also available to other eBPF program types,
  1216. * similarly to **bpf_get_smp_processor_id**\ ().
  1217. * Return
  1218. * The id of current NUMA node.
  1219. *
  1220. * int bpf_skb_change_head(struct sk_buff *skb, u32 len, u64 flags)
  1221. * Description
  1222. * Grows headroom of packet associated to *skb* and adjusts the
  1223. * offset of the MAC header accordingly, adding *len* bytes of
  1224. * space. It automatically extends and reallocates memory as
  1225. * required.
  1226. *
  1227. * This helper can be used on a layer 3 *skb* to push a MAC header
  1228. * for redirection into a layer 2 device.
  1229. *
  1230. * All values for *flags* are reserved for future usage, and must
  1231. * be left at zero.
  1232. *
  1233. * A call to this helper is susceptible to change the underlaying
  1234. * packet buffer. Therefore, at load time, all checks on pointers
  1235. * previously done by the verifier are invalidated and must be
  1236. * performed again, if the helper is used in combination with
  1237. * direct packet access.
  1238. * Return
  1239. * 0 on success, or a negative error in case of failure.
  1240. *
  1241. * int bpf_xdp_adjust_head(struct xdp_buff *xdp_md, int delta)
  1242. * Description
  1243. * Adjust (move) *xdp_md*\ **->data** by *delta* bytes. Note that
  1244. * it is possible to use a negative value for *delta*. This helper
  1245. * can be used to prepare the packet for pushing or popping
  1246. * headers.
  1247. *
  1248. * A call to this helper is susceptible to change the underlaying
  1249. * packet buffer. Therefore, at load time, all checks on pointers
  1250. * previously done by the verifier are invalidated and must be
  1251. * performed again, if the helper is used in combination with
  1252. * direct packet access.
  1253. * Return
  1254. * 0 on success, or a negative error in case of failure.
  1255. *
  1256. * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
  1257. * Description
  1258. * Copy a NUL terminated string from an unsafe address
  1259. * *unsafe_ptr* to *dst*. The *size* should include the
  1260. * terminating NUL byte. In case the string length is smaller than
  1261. * *size*, the target is not padded with further NUL bytes. If the
  1262. * string length is larger than *size*, just *size*-1 bytes are
  1263. * copied and the last byte is set to NUL.
  1264. *
  1265. * On success, the length of the copied string is returned. This
  1266. * makes this helper useful in tracing programs for reading
  1267. * strings, and more importantly to get its length at runtime. See
  1268. * the following snippet:
  1269. *
  1270. * ::
  1271. *
  1272. * SEC("kprobe/sys_open")
  1273. * void bpf_sys_open(struct pt_regs *ctx)
  1274. * {
  1275. * char buf[PATHLEN]; // PATHLEN is defined to 256
  1276. * int res = bpf_probe_read_str(buf, sizeof(buf),
  1277. * ctx->di);
  1278. *
  1279. * // Consume buf, for example push it to
  1280. * // userspace via bpf_perf_event_output(); we
  1281. * // can use res (the string length) as event
  1282. * // size, after checking its boundaries.
  1283. * }
  1284. *
  1285. * In comparison, using **bpf_probe_read()** helper here instead
  1286. * to read the string would require to estimate the length at
  1287. * compile time, and would often result in copying more memory
  1288. * than necessary.
  1289. *
  1290. * Another useful use case is when parsing individual process
  1291. * arguments or individual environment variables navigating
  1292. * *current*\ **->mm->arg_start** and *current*\
  1293. * **->mm->env_start**: using this helper and the return value,
  1294. * one can quickly iterate at the right offset of the memory area.
  1295. * Return
  1296. * On success, the strictly positive length of the string,
  1297. * including the trailing NUL character. On error, a negative
  1298. * value.
  1299. *
  1300. * u64 bpf_get_socket_cookie(struct sk_buff *skb)
  1301. * Description
  1302. * If the **struct sk_buff** pointed by *skb* has a known socket,
  1303. * retrieve the cookie (generated by the kernel) of this socket.
  1304. * If no cookie has been set yet, generate a new cookie. Once
  1305. * generated, the socket cookie remains stable for the life of the
  1306. * socket. This helper can be useful for monitoring per socket
  1307. * networking traffic statistics as it provides a unique socket
  1308. * identifier per namespace.
  1309. * Return
  1310. * A 8-byte long non-decreasing number on success, or 0 if the
  1311. * socket field is missing inside *skb*.
  1312. *
  1313. * u32 bpf_get_socket_uid(struct sk_buff *skb)
  1314. * Return
  1315. * The owner UID of the socket associated to *skb*. If the socket
  1316. * is **NULL**, or if it is not a full socket (i.e. if it is a
  1317. * time-wait or a request socket instead), **overflowuid** value
  1318. * is returned (note that **overflowuid** might also be the actual
  1319. * UID value for the socket).
  1320. *
  1321. * u32 bpf_set_hash(struct sk_buff *skb, u32 hash)
  1322. * Description
  1323. * Set the full hash for *skb* (set the field *skb*\ **->hash**)
  1324. * to value *hash*.
  1325. * Return
  1326. * 0
  1327. *
  1328. * int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, char *optval, int optlen)
  1329. * Description
  1330. * Emulate a call to **setsockopt()** on the socket associated to
  1331. * *bpf_socket*, which must be a full socket. The *level* at
  1332. * which the option resides and the name *optname* of the option
  1333. * must be specified, see **setsockopt(2)** for more information.
  1334. * The option value of length *optlen* is pointed by *optval*.
  1335. *
  1336. * This helper actually implements a subset of **setsockopt()**.
  1337. * It supports the following *level*\ s:
  1338. *
  1339. * * **SOL_SOCKET**, which supports the following *optname*\ s:
  1340. * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**,
  1341. * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**.
  1342. * * **IPPROTO_TCP**, which supports the following *optname*\ s:
  1343. * **TCP_CONGESTION**, **TCP_BPF_IW**,
  1344. * **TCP_BPF_SNDCWND_CLAMP**.
  1345. * * **IPPROTO_IP**, which supports *optname* **IP_TOS**.
  1346. * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**.
  1347. * Return
  1348. * 0 on success, or a negative error in case of failure.
  1349. *
  1350. * int bpf_skb_adjust_room(struct sk_buff *skb, u32 len_diff, u32 mode, u64 flags)
  1351. * Description
  1352. * Grow or shrink the room for data in the packet associated to
  1353. * *skb* by *len_diff*, and according to the selected *mode*.
  1354. *
  1355. * There is a single supported mode at this time:
  1356. *
  1357. * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer
  1358. * (room space is added or removed below the layer 3 header).
  1359. *
  1360. * All values for *flags* are reserved for future usage, and must
  1361. * be left at zero.
  1362. *
  1363. * A call to this helper is susceptible to change the underlaying
  1364. * packet buffer. Therefore, at load time, all checks on pointers
  1365. * previously done by the verifier are invalidated and must be
  1366. * performed again, if the helper is used in combination with
  1367. * direct packet access.
  1368. * Return
  1369. * 0 on success, or a negative error in case of failure.
  1370. *
  1371. * int bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)
  1372. * Description
  1373. * Redirect the packet to the endpoint referenced by *map* at
  1374. * index *key*. Depending on its type, this *map* can contain
  1375. * references to net devices (for forwarding packets through other
  1376. * ports), or to CPUs (for redirecting XDP frames to another CPU;
  1377. * but this is only implemented for native XDP (with driver
  1378. * support) as of this writing).
  1379. *
  1380. * All values for *flags* are reserved for future usage, and must
  1381. * be left at zero.
  1382. *
  1383. * When used to redirect packets to net devices, this helper
  1384. * provides a high performance increase over **bpf_redirect**\ ().
  1385. * This is due to various implementation details of the underlying
  1386. * mechanisms, one of which is the fact that **bpf_redirect_map**\
  1387. * () tries to send packet as a "bulk" to the device.
  1388. * Return
  1389. * **XDP_REDIRECT** on success, or **XDP_ABORTED** on error.
  1390. *
  1391. * int bpf_sk_redirect_map(struct bpf_map *map, u32 key, u64 flags)
  1392. * Description
  1393. * Redirect the packet to the socket referenced by *map* (of type
  1394. * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and
  1395. * egress interfaces can be used for redirection. The
  1396. * **BPF_F_INGRESS** value in *flags* is used to make the
  1397. * distinction (ingress path is selected if the flag is present,
  1398. * egress path otherwise). This is the only flag supported for now.
  1399. * Return
  1400. * **SK_PASS** on success, or **SK_DROP** on error.
  1401. *
  1402. * int bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags)
  1403. * Description
  1404. * Add an entry to, or update a *map* referencing sockets. The
  1405. * *skops* is used as a new value for the entry associated to
  1406. * *key*. *flags* is one of:
  1407. *
  1408. * **BPF_NOEXIST**
  1409. * The entry for *key* must not exist in the map.
  1410. * **BPF_EXIST**
  1411. * The entry for *key* must already exist in the map.
  1412. * **BPF_ANY**
  1413. * No condition on the existence of the entry for *key*.
  1414. *
  1415. * If the *map* has eBPF programs (parser and verdict), those will
  1416. * be inherited by the socket being added. If the socket is
  1417. * already attached to eBPF programs, this results in an error.
  1418. * Return
  1419. * 0 on success, or a negative error in case of failure.
  1420. *
  1421. * int bpf_xdp_adjust_meta(struct xdp_buff *xdp_md, int delta)
  1422. * Description
  1423. * Adjust the address pointed by *xdp_md*\ **->data_meta** by
  1424. * *delta* (which can be positive or negative). Note that this
  1425. * operation modifies the address stored in *xdp_md*\ **->data**,
  1426. * so the latter must be loaded only after the helper has been
  1427. * called.
  1428. *
  1429. * The use of *xdp_md*\ **->data_meta** is optional and programs
  1430. * are not required to use it. The rationale is that when the
  1431. * packet is processed with XDP (e.g. as DoS filter), it is
  1432. * possible to push further meta data along with it before passing
  1433. * to the stack, and to give the guarantee that an ingress eBPF
  1434. * program attached as a TC classifier on the same device can pick
  1435. * this up for further post-processing. Since TC works with socket
  1436. * buffers, it remains possible to set from XDP the **mark** or
  1437. * **priority** pointers, or other pointers for the socket buffer.
  1438. * Having this scratch space generic and programmable allows for
  1439. * more flexibility as the user is free to store whatever meta
  1440. * data they need.
  1441. *
  1442. * A call to this helper is susceptible to change the underlaying
  1443. * packet buffer. Therefore, at load time, all checks on pointers
  1444. * previously done by the verifier are invalidated and must be
  1445. * performed again, if the helper is used in combination with
  1446. * direct packet access.
  1447. * Return
  1448. * 0 on success, or a negative error in case of failure.
  1449. *
  1450. * int bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size)
  1451. * Description
  1452. * Read the value of a perf event counter, and store it into *buf*
  1453. * of size *buf_size*. This helper relies on a *map* of type
  1454. * **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of the perf event
  1455. * counter is selected when *map* is updated with perf event file
  1456. * descriptors. The *map* is an array whose size is the number of
  1457. * available CPUs, and each cell contains a value relative to one
  1458. * CPU. The value to retrieve is indicated by *flags*, that
  1459. * contains the index of the CPU to look up, masked with
  1460. * **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to
  1461. * **BPF_F_CURRENT_CPU** to indicate that the value for the
  1462. * current CPU should be retrieved.
  1463. *
  1464. * This helper behaves in a way close to
  1465. * **bpf_perf_event_read**\ () helper, save that instead of
  1466. * just returning the value observed, it fills the *buf*
  1467. * structure. This allows for additional data to be retrieved: in
  1468. * particular, the enabled and running times (in *buf*\
  1469. * **->enabled** and *buf*\ **->running**, respectively) are
  1470. * copied. In general, **bpf_perf_event_read_value**\ () is
  1471. * recommended over **bpf_perf_event_read**\ (), which has some
  1472. * ABI issues and provides fewer functionalities.
  1473. *
  1474. * These values are interesting, because hardware PMU (Performance
  1475. * Monitoring Unit) counters are limited resources. When there are
  1476. * more PMU based perf events opened than available counters,
  1477. * kernel will multiplex these events so each event gets certain
  1478. * percentage (but not all) of the PMU time. In case that
  1479. * multiplexing happens, the number of samples or counter value
  1480. * will not reflect the case compared to when no multiplexing
  1481. * occurs. This makes comparison between different runs difficult.
  1482. * Typically, the counter value should be normalized before
  1483. * comparing to other experiments. The usual normalization is done
  1484. * as follows.
  1485. *
  1486. * ::
  1487. *
  1488. * normalized_counter = counter * t_enabled / t_running
  1489. *
  1490. * Where t_enabled is the time enabled for event and t_running is
  1491. * the time running for event since last normalization. The
  1492. * enabled and running times are accumulated since the perf event
  1493. * open. To achieve scaling factor between two invocations of an
  1494. * eBPF program, users can can use CPU id as the key (which is
  1495. * typical for perf array usage model) to remember the previous
  1496. * value and do the calculation inside the eBPF program.
  1497. * Return
  1498. * 0 on success, or a negative error in case of failure.
  1499. *
  1500. * int bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size)
  1501. * Description
  1502. * For en eBPF program attached to a perf event, retrieve the
  1503. * value of the event counter associated to *ctx* and store it in
  1504. * the structure pointed by *buf* and of size *buf_size*. Enabled
  1505. * and running times are also stored in the structure (see
  1506. * description of helper **bpf_perf_event_read_value**\ () for
  1507. * more details).
  1508. * Return
  1509. * 0 on success, or a negative error in case of failure.
  1510. *
  1511. * int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, char *optval, int optlen)
  1512. * Description
  1513. * Emulate a call to **getsockopt()** on the socket associated to
  1514. * *bpf_socket*, which must be a full socket. The *level* at
  1515. * which the option resides and the name *optname* of the option
  1516. * must be specified, see **getsockopt(2)** for more information.
  1517. * The retrieved value is stored in the structure pointed by
  1518. * *opval* and of length *optlen*.
  1519. *
  1520. * This helper actually implements a subset of **getsockopt()**.
  1521. * It supports the following *level*\ s:
  1522. *
  1523. * * **IPPROTO_TCP**, which supports *optname*
  1524. * **TCP_CONGESTION**.
  1525. * * **IPPROTO_IP**, which supports *optname* **IP_TOS**.
  1526. * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**.
  1527. * Return
  1528. * 0 on success, or a negative error in case of failure.
  1529. *
  1530. * int bpf_override_return(struct pt_reg *regs, u64 rc)
  1531. * Description
  1532. * Used for error injection, this helper uses kprobes to override
  1533. * the return value of the probed function, and to set it to *rc*.
  1534. * The first argument is the context *regs* on which the kprobe
  1535. * works.
  1536. *
  1537. * This helper works by setting setting the PC (program counter)
  1538. * to an override function which is run in place of the original
  1539. * probed function. This means the probed function is not run at
  1540. * all. The replacement function just returns with the required
  1541. * value.
  1542. *
  1543. * This helper has security implications, and thus is subject to
  1544. * restrictions. It is only available if the kernel was compiled
  1545. * with the **CONFIG_BPF_KPROBE_OVERRIDE** configuration
  1546. * option, and in this case it only works on functions tagged with
  1547. * **ALLOW_ERROR_INJECTION** in the kernel code.
  1548. *
  1549. * Also, the helper is only available for the architectures having
  1550. * the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing,
  1551. * x86 architecture is the only one to support this feature.
  1552. * Return
  1553. * 0
  1554. *
  1555. * int bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *bpf_sock, int argval)
  1556. * Description
  1557. * Attempt to set the value of the **bpf_sock_ops_cb_flags** field
  1558. * for the full TCP socket associated to *bpf_sock_ops* to
  1559. * *argval*.
  1560. *
  1561. * The primary use of this field is to determine if there should
  1562. * be calls to eBPF programs of type
  1563. * **BPF_PROG_TYPE_SOCK_OPS** at various points in the TCP
  1564. * code. A program of the same type can change its value, per
  1565. * connection and as necessary, when the connection is
  1566. * established. This field is directly accessible for reading, but
  1567. * this helper must be used for updates in order to return an
  1568. * error if an eBPF program tries to set a callback that is not
  1569. * supported in the current kernel.
  1570. *
  1571. * The supported callback values that *argval* can combine are:
  1572. *
  1573. * * **BPF_SOCK_OPS_RTO_CB_FLAG** (retransmission time out)
  1574. * * **BPF_SOCK_OPS_RETRANS_CB_FLAG** (retransmission)
  1575. * * **BPF_SOCK_OPS_STATE_CB_FLAG** (TCP state change)
  1576. *
  1577. * Here are some examples of where one could call such eBPF
  1578. * program:
  1579. *
  1580. * * When RTO fires.
  1581. * * When a packet is retransmitted.
  1582. * * When the connection terminates.
  1583. * * When a packet is sent.
  1584. * * When a packet is received.
  1585. * Return
  1586. * Code **-EINVAL** if the socket is not a full TCP socket;
  1587. * otherwise, a positive number containing the bits that could not
  1588. * be set is returned (which comes down to 0 if all bits were set
  1589. * as required).
  1590. *
  1591. * int bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags)
  1592. * Description
  1593. * This helper is used in programs implementing policies at the
  1594. * socket level. If the message *msg* is allowed to pass (i.e. if
  1595. * the verdict eBPF program returns **SK_PASS**), redirect it to
  1596. * the socket referenced by *map* (of type
  1597. * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and
  1598. * egress interfaces can be used for redirection. The
  1599. * **BPF_F_INGRESS** value in *flags* is used to make the
  1600. * distinction (ingress path is selected if the flag is present,
  1601. * egress path otherwise). This is the only flag supported for now.
  1602. * Return
  1603. * **SK_PASS** on success, or **SK_DROP** on error.
  1604. *
  1605. * int bpf_msg_apply_bytes(struct sk_msg_buff *msg, u32 bytes)
  1606. * Description
  1607. * For socket policies, apply the verdict of the eBPF program to
  1608. * the next *bytes* (number of bytes) of message *msg*.
  1609. *
  1610. * For example, this helper can be used in the following cases:
  1611. *
  1612. * * A single **sendmsg**\ () or **sendfile**\ () system call
  1613. * contains multiple logical messages that the eBPF program is
  1614. * supposed to read and for which it should apply a verdict.
  1615. * * An eBPF program only cares to read the first *bytes* of a
  1616. * *msg*. If the message has a large payload, then setting up
  1617. * and calling the eBPF program repeatedly for all bytes, even
  1618. * though the verdict is already known, would create unnecessary
  1619. * overhead.
  1620. *
  1621. * When called from within an eBPF program, the helper sets a
  1622. * counter internal to the BPF infrastructure, that is used to
  1623. * apply the last verdict to the next *bytes*. If *bytes* is
  1624. * smaller than the current data being processed from a
  1625. * **sendmsg**\ () or **sendfile**\ () system call, the first
  1626. * *bytes* will be sent and the eBPF program will be re-run with
  1627. * the pointer for start of data pointing to byte number *bytes*
  1628. * **+ 1**. If *bytes* is larger than the current data being
  1629. * processed, then the eBPF verdict will be applied to multiple
  1630. * **sendmsg**\ () or **sendfile**\ () calls until *bytes* are
  1631. * consumed.
  1632. *
  1633. * Note that if a socket closes with the internal counter holding
  1634. * a non-zero value, this is not a problem because data is not
  1635. * being buffered for *bytes* and is sent as it is received.
  1636. * Return
  1637. * 0
  1638. *
  1639. * int bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes)
  1640. * Description
  1641. * For socket policies, prevent the execution of the verdict eBPF
  1642. * program for message *msg* until *bytes* (byte number) have been
  1643. * accumulated.
  1644. *
  1645. * This can be used when one needs a specific number of bytes
  1646. * before a verdict can be assigned, even if the data spans
  1647. * multiple **sendmsg**\ () or **sendfile**\ () calls. The extreme
  1648. * case would be a user calling **sendmsg**\ () repeatedly with
  1649. * 1-byte long message segments. Obviously, this is bad for
  1650. * performance, but it is still valid. If the eBPF program needs
  1651. * *bytes* bytes to validate a header, this helper can be used to
  1652. * prevent the eBPF program to be called again until *bytes* have
  1653. * been accumulated.
  1654. * Return
  1655. * 0
  1656. *
  1657. * int bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags)
  1658. * Description
  1659. * For socket policies, pull in non-linear data from user space
  1660. * for *msg* and set pointers *msg*\ **->data** and *msg*\
  1661. * **->data_end** to *start* and *end* bytes offsets into *msg*,
  1662. * respectively.
  1663. *
  1664. * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a
  1665. * *msg* it can only parse data that the (**data**, **data_end**)
  1666. * pointers have already consumed. For **sendmsg**\ () hooks this
  1667. * is likely the first scatterlist element. But for calls relying
  1668. * on the **sendpage** handler (e.g. **sendfile**\ ()) this will
  1669. * be the range (**0**, **0**) because the data is shared with
  1670. * user space and by default the objective is to avoid allowing
  1671. * user space to modify data while (or after) eBPF verdict is
  1672. * being decided. This helper can be used to pull in data and to
  1673. * set the start and end pointer to given values. Data will be
  1674. * copied if necessary (i.e. if data was not linear and if start
  1675. * and end pointers do not point to the same chunk).
  1676. *
  1677. * A call to this helper is susceptible to change the underlaying
  1678. * packet buffer. Therefore, at load time, all checks on pointers
  1679. * previously done by the verifier are invalidated and must be
  1680. * performed again, if the helper is used in combination with
  1681. * direct packet access.
  1682. *
  1683. * All values for *flags* are reserved for future usage, and must
  1684. * be left at zero.
  1685. * Return
  1686. * 0 on success, or a negative error in case of failure.
  1687. *
  1688. * int bpf_bind(struct bpf_sock_addr *ctx, struct sockaddr *addr, int addr_len)
  1689. * Description
  1690. * Bind the socket associated to *ctx* to the address pointed by
  1691. * *addr*, of length *addr_len*. This allows for making outgoing
  1692. * connection from the desired IP address, which can be useful for
  1693. * example when all processes inside a cgroup should use one
  1694. * single IP address on a host that has multiple IP configured.
  1695. *
  1696. * This helper works for IPv4 and IPv6, TCP and UDP sockets. The
  1697. * domain (*addr*\ **->sa_family**) must be **AF_INET** (or
  1698. * **AF_INET6**). Looking for a free port to bind to can be
  1699. * expensive, therefore binding to port is not permitted by the
  1700. * helper: *addr*\ **->sin_port** (or **sin6_port**, respectively)
  1701. * must be set to zero.
  1702. * Return
  1703. * 0 on success, or a negative error in case of failure.
  1704. *
  1705. * int bpf_xdp_adjust_tail(struct xdp_buff *xdp_md, int delta)
  1706. * Description
  1707. * Adjust (move) *xdp_md*\ **->data_end** by *delta* bytes. It is
  1708. * only possible to shrink the packet as of this writing,
  1709. * therefore *delta* must be a negative integer.
  1710. *
  1711. * A call to this helper is susceptible to change the underlaying
  1712. * packet buffer. Therefore, at load time, all checks on pointers
  1713. * previously done by the verifier are invalidated and must be
  1714. * performed again, if the helper is used in combination with
  1715. * direct packet access.
  1716. * Return
  1717. * 0 on success, or a negative error in case of failure.
  1718. *
  1719. * int bpf_skb_get_xfrm_state(struct sk_buff *skb, u32 index, struct bpf_xfrm_state *xfrm_state, u32 size, u64 flags)
  1720. * Description
  1721. * Retrieve the XFRM state (IP transform framework, see also
  1722. * **ip-xfrm(8)**) at *index* in XFRM "security path" for *skb*.
  1723. *
  1724. * The retrieved value is stored in the **struct bpf_xfrm_state**
  1725. * pointed by *xfrm_state* and of length *size*.
  1726. *
  1727. * All values for *flags* are reserved for future usage, and must
  1728. * be left at zero.
  1729. *
  1730. * This helper is available only if the kernel was compiled with
  1731. * **CONFIG_XFRM** configuration option.
  1732. * Return
  1733. * 0 on success, or a negative error in case of failure.
  1734. *
  1735. * int bpf_get_stack(struct pt_regs *regs, void *buf, u32 size, u64 flags)
  1736. * Description
  1737. * Return a user or a kernel stack in bpf program provided buffer.
  1738. * To achieve this, the helper needs *ctx*, which is a pointer
  1739. * to the context on which the tracing program is executed.
  1740. * To store the stacktrace, the bpf program provides *buf* with
  1741. * a nonnegative *size*.
  1742. *
  1743. * The last argument, *flags*, holds the number of stack frames to
  1744. * skip (from 0 to 255), masked with
  1745. * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set
  1746. * the following flags:
  1747. *
  1748. * **BPF_F_USER_STACK**
  1749. * Collect a user space stack instead of a kernel stack.
  1750. * **BPF_F_USER_BUILD_ID**
  1751. * Collect buildid+offset instead of ips for user stack,
  1752. * only valid if **BPF_F_USER_STACK** is also specified.
  1753. *
  1754. * **bpf_get_stack**\ () can collect up to
  1755. * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject
  1756. * to sufficient large buffer size. Note that
  1757. * this limit can be controlled with the **sysctl** program, and
  1758. * that it should be manually increased in order to profile long
  1759. * user stacks (such as stacks for Java programs). To do so, use:
  1760. *
  1761. * ::
  1762. *
  1763. * # sysctl kernel.perf_event_max_stack=<new value>
  1764. *
  1765. * Return
  1766. * a non-negative value equal to or less than size on success, or
  1767. * a negative error in case of failure.
  1768. *
  1769. * int skb_load_bytes_relative(const struct sk_buff *skb, u32 offset, void *to, u32 len, u32 start_header)
  1770. * Description
  1771. * This helper is similar to **bpf_skb_load_bytes**\ () in that
  1772. * it provides an easy way to load *len* bytes from *offset*
  1773. * from the packet associated to *skb*, into the buffer pointed
  1774. * by *to*. The difference to **bpf_skb_load_bytes**\ () is that
  1775. * a fifth argument *start_header* exists in order to select a
  1776. * base offset to start from. *start_header* can be one of:
  1777. *
  1778. * **BPF_HDR_START_MAC**
  1779. * Base offset to load data from is *skb*'s mac header.
  1780. * **BPF_HDR_START_NET**
  1781. * Base offset to load data from is *skb*'s network header.
  1782. *
  1783. * In general, "direct packet access" is the preferred method to
  1784. * access packet data, however, this helper is in particular useful
  1785. * in socket filters where *skb*\ **->data** does not always point
  1786. * to the start of the mac header and where "direct packet access"
  1787. * is not available.
  1788. *
  1789. * Return
  1790. * 0 on success, or a negative error in case of failure.
  1791. *
  1792. * int bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags)
  1793. * Description
  1794. * Do FIB lookup in kernel tables using parameters in *params*.
  1795. * If lookup is successful and result shows packet is to be
  1796. * forwarded, the neighbor tables are searched for the nexthop.
  1797. * If successful (ie., FIB lookup shows forwarding and nexthop
  1798. * is resolved), the nexthop address is returned in ipv4_dst,
  1799. * ipv6_dst or mpls_out based on family, smac is set to mac
  1800. * address of egress device, dmac is set to nexthop mac address,
  1801. * rt_metric is set to metric from route.
  1802. *
  1803. * *plen* argument is the size of the passed in struct.
  1804. * *flags* argument can be one or more BPF_FIB_LOOKUP_ flags:
  1805. *
  1806. * **BPF_FIB_LOOKUP_DIRECT** means do a direct table lookup vs
  1807. * full lookup using FIB rules
  1808. * **BPF_FIB_LOOKUP_OUTPUT** means do lookup from an egress
  1809. * perspective (default is ingress)
  1810. *
  1811. * *ctx* is either **struct xdp_md** for XDP programs or
  1812. * **struct sk_buff** tc cls_act programs.
  1813. *
  1814. * Return
  1815. * Egress device index on success, 0 if packet needs to continue
  1816. * up the stack for further processing or a negative error in case
  1817. * of failure.
  1818. *
  1819. * int bpf_sock_hash_update(struct bpf_sock_ops_kern *skops, struct bpf_map *map, void *key, u64 flags)
  1820. * Description
  1821. * Add an entry to, or update a sockhash *map* referencing sockets.
  1822. * The *skops* is used as a new value for the entry associated to
  1823. * *key*. *flags* is one of:
  1824. *
  1825. * **BPF_NOEXIST**
  1826. * The entry for *key* must not exist in the map.
  1827. * **BPF_EXIST**
  1828. * The entry for *key* must already exist in the map.
  1829. * **BPF_ANY**
  1830. * No condition on the existence of the entry for *key*.
  1831. *
  1832. * If the *map* has eBPF programs (parser and verdict), those will
  1833. * be inherited by the socket being added. If the socket is
  1834. * already attached to eBPF programs, this results in an error.
  1835. * Return
  1836. * 0 on success, or a negative error in case of failure.
  1837. *
  1838. * int bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags)
  1839. * Description
  1840. * This helper is used in programs implementing policies at the
  1841. * socket level. If the message *msg* is allowed to pass (i.e. if
  1842. * the verdict eBPF program returns **SK_PASS**), redirect it to
  1843. * the socket referenced by *map* (of type
  1844. * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and
  1845. * egress interfaces can be used for redirection. The
  1846. * **BPF_F_INGRESS** value in *flags* is used to make the
  1847. * distinction (ingress path is selected if the flag is present,
  1848. * egress path otherwise). This is the only flag supported for now.
  1849. * Return
  1850. * **SK_PASS** on success, or **SK_DROP** on error.
  1851. *
  1852. * int bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags)
  1853. * Description
  1854. * This helper is used in programs implementing policies at the
  1855. * skb socket level. If the sk_buff *skb* is allowed to pass (i.e.
  1856. * if the verdeict eBPF program returns **SK_PASS**), redirect it
  1857. * to the socket referenced by *map* (of type
  1858. * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and
  1859. * egress interfaces can be used for redirection. The
  1860. * **BPF_F_INGRESS** value in *flags* is used to make the
  1861. * distinction (ingress path is selected if the flag is present,
  1862. * egress otherwise). This is the only flag supported for now.
  1863. * Return
  1864. * **SK_PASS** on success, or **SK_DROP** on error.
  1865. */
  1866. #define __BPF_FUNC_MAPPER(FN) \
  1867. FN(unspec), \
  1868. FN(map_lookup_elem), \
  1869. FN(map_update_elem), \
  1870. FN(map_delete_elem), \
  1871. FN(probe_read), \
  1872. FN(ktime_get_ns), \
  1873. FN(trace_printk), \
  1874. FN(get_prandom_u32), \
  1875. FN(get_smp_processor_id), \
  1876. FN(skb_store_bytes), \
  1877. FN(l3_csum_replace), \
  1878. FN(l4_csum_replace), \
  1879. FN(tail_call), \
  1880. FN(clone_redirect), \
  1881. FN(get_current_pid_tgid), \
  1882. FN(get_current_uid_gid), \
  1883. FN(get_current_comm), \
  1884. FN(get_cgroup_classid), \
  1885. FN(skb_vlan_push), \
  1886. FN(skb_vlan_pop), \
  1887. FN(skb_get_tunnel_key), \
  1888. FN(skb_set_tunnel_key), \
  1889. FN(perf_event_read), \
  1890. FN(redirect), \
  1891. FN(get_route_realm), \
  1892. FN(perf_event_output), \
  1893. FN(skb_load_bytes), \
  1894. FN(get_stackid), \
  1895. FN(csum_diff), \
  1896. FN(skb_get_tunnel_opt), \
  1897. FN(skb_set_tunnel_opt), \
  1898. FN(skb_change_proto), \
  1899. FN(skb_change_type), \
  1900. FN(skb_under_cgroup), \
  1901. FN(get_hash_recalc), \
  1902. FN(get_current_task), \
  1903. FN(probe_write_user), \
  1904. FN(current_task_under_cgroup), \
  1905. FN(skb_change_tail), \
  1906. FN(skb_pull_data), \
  1907. FN(csum_update), \
  1908. FN(set_hash_invalid), \
  1909. FN(get_numa_node_id), \
  1910. FN(skb_change_head), \
  1911. FN(xdp_adjust_head), \
  1912. FN(probe_read_str), \
  1913. FN(get_socket_cookie), \
  1914. FN(get_socket_uid), \
  1915. FN(set_hash), \
  1916. FN(setsockopt), \
  1917. FN(skb_adjust_room), \
  1918. FN(redirect_map), \
  1919. FN(sk_redirect_map), \
  1920. FN(sock_map_update), \
  1921. FN(xdp_adjust_meta), \
  1922. FN(perf_event_read_value), \
  1923. FN(perf_prog_read_value), \
  1924. FN(getsockopt), \
  1925. FN(override_return), \
  1926. FN(sock_ops_cb_flags_set), \
  1927. FN(msg_redirect_map), \
  1928. FN(msg_apply_bytes), \
  1929. FN(msg_cork_bytes), \
  1930. FN(msg_pull_data), \
  1931. FN(bind), \
  1932. FN(xdp_adjust_tail), \
  1933. FN(skb_get_xfrm_state), \
  1934. FN(get_stack), \
  1935. FN(skb_load_bytes_relative), \
  1936. FN(fib_lookup), \
  1937. FN(sock_hash_update), \
  1938. FN(msg_redirect_hash), \
  1939. FN(sk_redirect_hash),
  1940. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  1941. * function eBPF program intends to call
  1942. */
  1943. #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
  1944. enum bpf_func_id {
  1945. __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
  1946. __BPF_FUNC_MAX_ID,
  1947. };
  1948. #undef __BPF_ENUM_FN
  1949. /* All flags used by eBPF helper functions, placed here. */
  1950. /* BPF_FUNC_skb_store_bytes flags. */
  1951. #define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
  1952. #define BPF_F_INVALIDATE_HASH (1ULL << 1)
  1953. /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
  1954. * First 4 bits are for passing the header field size.
  1955. */
  1956. #define BPF_F_HDR_FIELD_MASK 0xfULL
  1957. /* BPF_FUNC_l4_csum_replace flags. */
  1958. #define BPF_F_PSEUDO_HDR (1ULL << 4)
  1959. #define BPF_F_MARK_MANGLED_0 (1ULL << 5)
  1960. #define BPF_F_MARK_ENFORCE (1ULL << 6)
  1961. /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
  1962. #define BPF_F_INGRESS (1ULL << 0)
  1963. /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
  1964. #define BPF_F_TUNINFO_IPV6 (1ULL << 0)
  1965. /* flags for both BPF_FUNC_get_stackid and BPF_FUNC_get_stack. */
  1966. #define BPF_F_SKIP_FIELD_MASK 0xffULL
  1967. #define BPF_F_USER_STACK (1ULL << 8)
  1968. /* flags used by BPF_FUNC_get_stackid only. */
  1969. #define BPF_F_FAST_STACK_CMP (1ULL << 9)
  1970. #define BPF_F_REUSE_STACKID (1ULL << 10)
  1971. /* flags used by BPF_FUNC_get_stack only. */
  1972. #define BPF_F_USER_BUILD_ID (1ULL << 11)
  1973. /* BPF_FUNC_skb_set_tunnel_key flags. */
  1974. #define BPF_F_ZERO_CSUM_TX (1ULL << 1)
  1975. #define BPF_F_DONT_FRAGMENT (1ULL << 2)
  1976. #define BPF_F_SEQ_NUMBER (1ULL << 3)
  1977. /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and
  1978. * BPF_FUNC_perf_event_read_value flags.
  1979. */
  1980. #define BPF_F_INDEX_MASK 0xffffffffULL
  1981. #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
  1982. /* BPF_FUNC_perf_event_output for sk_buff input context. */
  1983. #define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
  1984. /* Mode for BPF_FUNC_skb_adjust_room helper. */
  1985. enum bpf_adj_room_mode {
  1986. BPF_ADJ_ROOM_NET,
  1987. };
  1988. /* Mode for BPF_FUNC_skb_load_bytes_relative helper. */
  1989. enum bpf_hdr_start_off {
  1990. BPF_HDR_START_MAC,
  1991. BPF_HDR_START_NET,
  1992. };
  1993. /* user accessible mirror of in-kernel sk_buff.
  1994. * new fields can only be added to the end of this structure
  1995. */
  1996. struct __sk_buff {
  1997. __u32 len;
  1998. __u32 pkt_type;
  1999. __u32 mark;
  2000. __u32 queue_mapping;
  2001. __u32 protocol;
  2002. __u32 vlan_present;
  2003. __u32 vlan_tci;
  2004. __u32 vlan_proto;
  2005. __u32 priority;
  2006. __u32 ingress_ifindex;
  2007. __u32 ifindex;
  2008. __u32 tc_index;
  2009. __u32 cb[5];
  2010. __u32 hash;
  2011. __u32 tc_classid;
  2012. __u32 data;
  2013. __u32 data_end;
  2014. __u32 napi_id;
  2015. /* Accessed by BPF_PROG_TYPE_sk_skb types from here to ... */
  2016. __u32 family;
  2017. __u32 remote_ip4; /* Stored in network byte order */
  2018. __u32 local_ip4; /* Stored in network byte order */
  2019. __u32 remote_ip6[4]; /* Stored in network byte order */
  2020. __u32 local_ip6[4]; /* Stored in network byte order */
  2021. __u32 remote_port; /* Stored in network byte order */
  2022. __u32 local_port; /* stored in host byte order */
  2023. /* ... here. */
  2024. __u32 data_meta;
  2025. };
  2026. struct bpf_tunnel_key {
  2027. __u32 tunnel_id;
  2028. union {
  2029. __u32 remote_ipv4;
  2030. __u32 remote_ipv6[4];
  2031. };
  2032. __u8 tunnel_tos;
  2033. __u8 tunnel_ttl;
  2034. __u16 tunnel_ext;
  2035. __u32 tunnel_label;
  2036. };
  2037. /* user accessible mirror of in-kernel xfrm_state.
  2038. * new fields can only be added to the end of this structure
  2039. */
  2040. struct bpf_xfrm_state {
  2041. __u32 reqid;
  2042. __u32 spi; /* Stored in network byte order */
  2043. __u16 family;
  2044. union {
  2045. __u32 remote_ipv4; /* Stored in network byte order */
  2046. __u32 remote_ipv6[4]; /* Stored in network byte order */
  2047. };
  2048. };
  2049. /* Generic BPF return codes which all BPF program types may support.
  2050. * The values are binary compatible with their TC_ACT_* counter-part to
  2051. * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
  2052. * programs.
  2053. *
  2054. * XDP is handled seprately, see XDP_*.
  2055. */
  2056. enum bpf_ret_code {
  2057. BPF_OK = 0,
  2058. /* 1 reserved */
  2059. BPF_DROP = 2,
  2060. /* 3-6 reserved */
  2061. BPF_REDIRECT = 7,
  2062. /* >127 are reserved for prog type specific return codes */
  2063. };
  2064. struct bpf_sock {
  2065. __u32 bound_dev_if;
  2066. __u32 family;
  2067. __u32 type;
  2068. __u32 protocol;
  2069. __u32 mark;
  2070. __u32 priority;
  2071. __u32 src_ip4; /* Allows 1,2,4-byte read.
  2072. * Stored in network byte order.
  2073. */
  2074. __u32 src_ip6[4]; /* Allows 1,2,4-byte read.
  2075. * Stored in network byte order.
  2076. */
  2077. __u32 src_port; /* Allows 4-byte read.
  2078. * Stored in host byte order
  2079. */
  2080. };
  2081. #define XDP_PACKET_HEADROOM 256
  2082. /* User return codes for XDP prog type.
  2083. * A valid XDP program must return one of these defined values. All other
  2084. * return codes are reserved for future use. Unknown return codes will
  2085. * result in packet drops and a warning via bpf_warn_invalid_xdp_action().
  2086. */
  2087. enum xdp_action {
  2088. XDP_ABORTED = 0,
  2089. XDP_DROP,
  2090. XDP_PASS,
  2091. XDP_TX,
  2092. XDP_REDIRECT,
  2093. };
  2094. /* user accessible metadata for XDP packet hook
  2095. * new fields must be added to the end of this structure
  2096. */
  2097. struct xdp_md {
  2098. __u32 data;
  2099. __u32 data_end;
  2100. __u32 data_meta;
  2101. /* Below access go through struct xdp_rxq_info */
  2102. __u32 ingress_ifindex; /* rxq->dev->ifindex */
  2103. __u32 rx_queue_index; /* rxq->queue_index */
  2104. };
  2105. enum sk_action {
  2106. SK_DROP = 0,
  2107. SK_PASS,
  2108. };
  2109. /* user accessible metadata for SK_MSG packet hook, new fields must
  2110. * be added to the end of this structure
  2111. */
  2112. struct sk_msg_md {
  2113. void *data;
  2114. void *data_end;
  2115. __u32 family;
  2116. __u32 remote_ip4; /* Stored in network byte order */
  2117. __u32 local_ip4; /* Stored in network byte order */
  2118. __u32 remote_ip6[4]; /* Stored in network byte order */
  2119. __u32 local_ip6[4]; /* Stored in network byte order */
  2120. __u32 remote_port; /* Stored in network byte order */
  2121. __u32 local_port; /* stored in host byte order */
  2122. };
  2123. #define BPF_TAG_SIZE 8
  2124. struct bpf_prog_info {
  2125. __u32 type;
  2126. __u32 id;
  2127. __u8 tag[BPF_TAG_SIZE];
  2128. __u32 jited_prog_len;
  2129. __u32 xlated_prog_len;
  2130. __aligned_u64 jited_prog_insns;
  2131. __aligned_u64 xlated_prog_insns;
  2132. __u64 load_time; /* ns since boottime */
  2133. __u32 created_by_uid;
  2134. __u32 nr_map_ids;
  2135. __aligned_u64 map_ids;
  2136. char name[BPF_OBJ_NAME_LEN];
  2137. __u32 ifindex;
  2138. __u32 gpl_compatible:1;
  2139. __u64 netns_dev;
  2140. __u64 netns_ino;
  2141. } __attribute__((aligned(8)));
  2142. struct bpf_map_info {
  2143. __u32 type;
  2144. __u32 id;
  2145. __u32 key_size;
  2146. __u32 value_size;
  2147. __u32 max_entries;
  2148. __u32 map_flags;
  2149. char name[BPF_OBJ_NAME_LEN];
  2150. __u32 ifindex;
  2151. __u64 netns_dev;
  2152. __u64 netns_ino;
  2153. __u32 btf_id;
  2154. __u32 btf_key_type_id;
  2155. __u32 btf_value_type_id;
  2156. } __attribute__((aligned(8)));
  2157. struct bpf_btf_info {
  2158. __aligned_u64 btf;
  2159. __u32 btf_size;
  2160. __u32 id;
  2161. } __attribute__((aligned(8)));
  2162. /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
  2163. * by user and intended to be used by socket (e.g. to bind to, depends on
  2164. * attach attach type).
  2165. */
  2166. struct bpf_sock_addr {
  2167. __u32 user_family; /* Allows 4-byte read, but no write. */
  2168. __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write.
  2169. * Stored in network byte order.
  2170. */
  2171. __u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
  2172. * Stored in network byte order.
  2173. */
  2174. __u32 user_port; /* Allows 4-byte read and write.
  2175. * Stored in network byte order
  2176. */
  2177. __u32 family; /* Allows 4-byte read, but no write */
  2178. __u32 type; /* Allows 4-byte read, but no write */
  2179. __u32 protocol; /* Allows 4-byte read, but no write */
  2180. };
  2181. /* User bpf_sock_ops struct to access socket values and specify request ops
  2182. * and their replies.
  2183. * Some of this fields are in network (bigendian) byte order and may need
  2184. * to be converted before use (bpf_ntohl() defined in samples/bpf/bpf_endian.h).
  2185. * New fields can only be added at the end of this structure
  2186. */
  2187. struct bpf_sock_ops {
  2188. __u32 op;
  2189. union {
  2190. __u32 args[4]; /* Optionally passed to bpf program */
  2191. __u32 reply; /* Returned by bpf program */
  2192. __u32 replylong[4]; /* Optionally returned by bpf prog */
  2193. };
  2194. __u32 family;
  2195. __u32 remote_ip4; /* Stored in network byte order */
  2196. __u32 local_ip4; /* Stored in network byte order */
  2197. __u32 remote_ip6[4]; /* Stored in network byte order */
  2198. __u32 local_ip6[4]; /* Stored in network byte order */
  2199. __u32 remote_port; /* Stored in network byte order */
  2200. __u32 local_port; /* stored in host byte order */
  2201. __u32 is_fullsock; /* Some TCP fields are only valid if
  2202. * there is a full socket. If not, the
  2203. * fields read as zero.
  2204. */
  2205. __u32 snd_cwnd;
  2206. __u32 srtt_us; /* Averaged RTT << 3 in usecs */
  2207. __u32 bpf_sock_ops_cb_flags; /* flags defined in uapi/linux/tcp.h */
  2208. __u32 state;
  2209. __u32 rtt_min;
  2210. __u32 snd_ssthresh;
  2211. __u32 rcv_nxt;
  2212. __u32 snd_nxt;
  2213. __u32 snd_una;
  2214. __u32 mss_cache;
  2215. __u32 ecn_flags;
  2216. __u32 rate_delivered;
  2217. __u32 rate_interval_us;
  2218. __u32 packets_out;
  2219. __u32 retrans_out;
  2220. __u32 total_retrans;
  2221. __u32 segs_in;
  2222. __u32 data_segs_in;
  2223. __u32 segs_out;
  2224. __u32 data_segs_out;
  2225. __u32 lost_out;
  2226. __u32 sacked_out;
  2227. __u32 sk_txhash;
  2228. __u64 bytes_received;
  2229. __u64 bytes_acked;
  2230. };
  2231. /* Definitions for bpf_sock_ops_cb_flags */
  2232. #define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0)
  2233. #define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1)
  2234. #define BPF_SOCK_OPS_STATE_CB_FLAG (1<<2)
  2235. #define BPF_SOCK_OPS_ALL_CB_FLAGS 0x7 /* Mask of all currently
  2236. * supported cb flags
  2237. */
  2238. /* List of known BPF sock_ops operators.
  2239. * New entries can only be added at the end
  2240. */
  2241. enum {
  2242. BPF_SOCK_OPS_VOID,
  2243. BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or
  2244. * -1 if default value should be used
  2245. */
  2246. BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized
  2247. * window (in packets) or -1 if default
  2248. * value should be used
  2249. */
  2250. BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an
  2251. * active connection is initialized
  2252. */
  2253. BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an
  2254. * active connection is
  2255. * established
  2256. */
  2257. BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a
  2258. * passive connection is
  2259. * established
  2260. */
  2261. BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control
  2262. * needs ECN
  2263. */
  2264. BPF_SOCK_OPS_BASE_RTT, /* Get base RTT. The correct value is
  2265. * based on the path and may be
  2266. * dependent on the congestion control
  2267. * algorithm. In general it indicates
  2268. * a congestion threshold. RTTs above
  2269. * this indicate congestion
  2270. */
  2271. BPF_SOCK_OPS_RTO_CB, /* Called when an RTO has triggered.
  2272. * Arg1: value of icsk_retransmits
  2273. * Arg2: value of icsk_rto
  2274. * Arg3: whether RTO has expired
  2275. */
  2276. BPF_SOCK_OPS_RETRANS_CB, /* Called when skb is retransmitted.
  2277. * Arg1: sequence number of 1st byte
  2278. * Arg2: # segments
  2279. * Arg3: return value of
  2280. * tcp_transmit_skb (0 => success)
  2281. */
  2282. BPF_SOCK_OPS_STATE_CB, /* Called when TCP changes state.
  2283. * Arg1: old_state
  2284. * Arg2: new_state
  2285. */
  2286. };
  2287. /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
  2288. * changes between the TCP and BPF versions. Ideally this should never happen.
  2289. * If it does, we need to add code to convert them before calling
  2290. * the BPF sock_ops function.
  2291. */
  2292. enum {
  2293. BPF_TCP_ESTABLISHED = 1,
  2294. BPF_TCP_SYN_SENT,
  2295. BPF_TCP_SYN_RECV,
  2296. BPF_TCP_FIN_WAIT1,
  2297. BPF_TCP_FIN_WAIT2,
  2298. BPF_TCP_TIME_WAIT,
  2299. BPF_TCP_CLOSE,
  2300. BPF_TCP_CLOSE_WAIT,
  2301. BPF_TCP_LAST_ACK,
  2302. BPF_TCP_LISTEN,
  2303. BPF_TCP_CLOSING, /* Now a valid state */
  2304. BPF_TCP_NEW_SYN_RECV,
  2305. BPF_TCP_MAX_STATES /* Leave at the end! */
  2306. };
  2307. #define TCP_BPF_IW 1001 /* Set TCP initial congestion window */
  2308. #define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */
  2309. struct bpf_perf_event_value {
  2310. __u64 counter;
  2311. __u64 enabled;
  2312. __u64 running;
  2313. };
  2314. #define BPF_DEVCG_ACC_MKNOD (1ULL << 0)
  2315. #define BPF_DEVCG_ACC_READ (1ULL << 1)
  2316. #define BPF_DEVCG_ACC_WRITE (1ULL << 2)
  2317. #define BPF_DEVCG_DEV_BLOCK (1ULL << 0)
  2318. #define BPF_DEVCG_DEV_CHAR (1ULL << 1)
  2319. struct bpf_cgroup_dev_ctx {
  2320. /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */
  2321. __u32 access_type;
  2322. __u32 major;
  2323. __u32 minor;
  2324. };
  2325. struct bpf_raw_tracepoint_args {
  2326. __u64 args[0];
  2327. };
  2328. /* DIRECT: Skip the FIB rules and go to FIB table associated with device
  2329. * OUTPUT: Do lookup from egress perspective; default is ingress
  2330. */
  2331. #define BPF_FIB_LOOKUP_DIRECT BIT(0)
  2332. #define BPF_FIB_LOOKUP_OUTPUT BIT(1)
  2333. struct bpf_fib_lookup {
  2334. /* input */
  2335. __u8 family; /* network family, AF_INET, AF_INET6, AF_MPLS */
  2336. /* set if lookup is to consider L4 data - e.g., FIB rules */
  2337. __u8 l4_protocol;
  2338. __be16 sport;
  2339. __be16 dport;
  2340. /* total length of packet from network header - used for MTU check */
  2341. __u16 tot_len;
  2342. __u32 ifindex; /* L3 device index for lookup */
  2343. union {
  2344. /* inputs to lookup */
  2345. __u8 tos; /* AF_INET */
  2346. __be32 flowlabel; /* AF_INET6 */
  2347. /* output: metric of fib result */
  2348. __u32 rt_metric;
  2349. };
  2350. union {
  2351. __be32 mpls_in;
  2352. __be32 ipv4_src;
  2353. __u32 ipv6_src[4]; /* in6_addr; network order */
  2354. };
  2355. /* input to bpf_fib_lookup, *dst is destination address.
  2356. * output: bpf_fib_lookup sets to gateway address
  2357. */
  2358. union {
  2359. /* return for MPLS lookups */
  2360. __be32 mpls_out[4]; /* support up to 4 labels */
  2361. __be32 ipv4_dst;
  2362. __u32 ipv6_dst[4]; /* in6_addr; network order */
  2363. };
  2364. /* output */
  2365. __be16 h_vlan_proto;
  2366. __be16 h_vlan_TCI;
  2367. __u8 smac[6]; /* ETH_ALEN */
  2368. __u8 dmac[6]; /* ETH_ALEN */
  2369. };
  2370. #endif /* _UAPI__LINUX_BPF_H__ */