Scott Branden | 3cd046f | 2020-02-25 12:54:26 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
Quentin Monnet | 748c7c8 | 2019-05-10 15:51:22 +0100 | [diff] [blame] | 4 | # Copyright (C) 2018-2019 Netronome Systems, Inc. |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 5 | |
| 6 | # In case user attempts to run with Python 2. |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import argparse |
| 10 | import re |
| 11 | import sys, os |
| 12 | |
| 13 | class NoHelperFound(BaseException): |
| 14 | pass |
| 15 | |
| 16 | class ParsingError(BaseException): |
| 17 | def __init__(self, line='<line not provided>', reader=None): |
| 18 | if reader: |
| 19 | BaseException.__init__(self, |
| 20 | 'Error at file offset %d, parsing line: %s' % |
| 21 | (reader.tell(), line)) |
| 22 | else: |
| 23 | BaseException.__init__(self, 'Error parsing line: %s' % line) |
| 24 | |
| 25 | class Helper(object): |
| 26 | """ |
| 27 | An object representing the description of an eBPF helper function. |
| 28 | @proto: function prototype of the helper function |
| 29 | @desc: textual description of the helper function |
| 30 | @ret: description of the return value of the helper function |
| 31 | """ |
| 32 | def __init__(self, proto='', desc='', ret=''): |
| 33 | self.proto = proto |
| 34 | self.desc = desc |
| 35 | self.ret = ret |
| 36 | |
| 37 | def proto_break_down(self): |
| 38 | """ |
| 39 | Break down helper function protocol into smaller chunks: return type, |
| 40 | name, distincts arguments. |
| 41 | """ |
Quentin Monnet | 748c7c8 | 2019-05-10 15:51:22 +0100 | [diff] [blame] | 42 | arg_re = re.compile('((\w+ )*?(\w+|...))( (\**)(\w+))?$') |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 43 | res = {} |
Quentin Monnet | 6f96674 | 2018-05-02 14:20:24 +0100 | [diff] [blame] | 44 | proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$') |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 45 | |
| 46 | capture = proto_re.match(self.proto) |
| 47 | res['ret_type'] = capture.group(1) |
| 48 | res['ret_star'] = capture.group(2) |
| 49 | res['name'] = capture.group(3) |
| 50 | res['args'] = [] |
| 51 | |
| 52 | args = capture.group(4).split(', ') |
| 53 | for a in args: |
| 54 | capture = arg_re.match(a) |
| 55 | res['args'].append({ |
| 56 | 'type' : capture.group(1), |
Quentin Monnet | 748c7c8 | 2019-05-10 15:51:22 +0100 | [diff] [blame] | 57 | 'star' : capture.group(5), |
| 58 | 'name' : capture.group(6) |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 59 | }) |
| 60 | |
| 61 | return res |
| 62 | |
| 63 | class HeaderParser(object): |
| 64 | """ |
| 65 | An object used to parse a file in order to extract the documentation of a |
| 66 | list of eBPF helper functions. All the helpers that can be retrieved are |
| 67 | stored as Helper object, in the self.helpers() array. |
| 68 | @filename: name of file to parse, usually include/uapi/linux/bpf.h in the |
| 69 | kernel tree |
| 70 | """ |
| 71 | def __init__(self, filename): |
| 72 | self.reader = open(filename, 'r') |
| 73 | self.line = '' |
| 74 | self.helpers = [] |
| 75 | |
| 76 | def parse_helper(self): |
| 77 | proto = self.parse_proto() |
| 78 | desc = self.parse_desc() |
| 79 | ret = self.parse_ret() |
| 80 | return Helper(proto=proto, desc=desc, ret=ret) |
| 81 | |
| 82 | def parse_proto(self): |
| 83 | # Argument can be of shape: |
| 84 | # - "void" |
| 85 | # - "type name" |
| 86 | # - "type *name" |
| 87 | # - Same as above, with "const" and/or "struct" in front of type |
| 88 | # - "..." (undefined number of arguments, for bpf_trace_printk()) |
| 89 | # There is at least one term ("void"), and at most five arguments. |
Quentin Monnet | 6f96674 | 2018-05-02 14:20:24 +0100 | [diff] [blame] | 90 | p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$') |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 91 | capture = p.match(self.line) |
| 92 | if not capture: |
| 93 | raise NoHelperFound |
| 94 | self.line = self.reader.readline() |
| 95 | return capture.group(1) |
| 96 | |
| 97 | def parse_desc(self): |
Quentin Monnet | eeacb71 | 2018-05-17 13:43:56 +0100 | [diff] [blame] | 98 | p = re.compile(' \* ?(?:\t| {5,8})Description$') |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 99 | capture = p.match(self.line) |
| 100 | if not capture: |
| 101 | # Helper can have empty description and we might be parsing another |
| 102 | # attribute: return but do not consume. |
| 103 | return '' |
| 104 | # Description can be several lines, some of them possibly empty, and it |
| 105 | # stops when another subsection title is met. |
| 106 | desc = '' |
| 107 | while True: |
| 108 | self.line = self.reader.readline() |
| 109 | if self.line == ' *\n': |
| 110 | desc += '\n' |
| 111 | else: |
Quentin Monnet | eeacb71 | 2018-05-17 13:43:56 +0100 | [diff] [blame] | 112 | p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)') |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 113 | capture = p.match(self.line) |
| 114 | if capture: |
| 115 | desc += capture.group(1) + '\n' |
| 116 | else: |
| 117 | break |
| 118 | return desc |
| 119 | |
| 120 | def parse_ret(self): |
Quentin Monnet | eeacb71 | 2018-05-17 13:43:56 +0100 | [diff] [blame] | 121 | p = re.compile(' \* ?(?:\t| {5,8})Return$') |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 122 | capture = p.match(self.line) |
| 123 | if not capture: |
| 124 | # Helper can have empty retval and we might be parsing another |
| 125 | # attribute: return but do not consume. |
| 126 | return '' |
| 127 | # Return value description can be several lines, some of them possibly |
| 128 | # empty, and it stops when another subsection title is met. |
| 129 | ret = '' |
| 130 | while True: |
| 131 | self.line = self.reader.readline() |
| 132 | if self.line == ' *\n': |
| 133 | ret += '\n' |
| 134 | else: |
Quentin Monnet | eeacb71 | 2018-05-17 13:43:56 +0100 | [diff] [blame] | 135 | p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)') |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 136 | capture = p.match(self.line) |
| 137 | if capture: |
| 138 | ret += capture.group(1) + '\n' |
| 139 | else: |
| 140 | break |
| 141 | return ret |
| 142 | |
| 143 | def run(self): |
| 144 | # Advance to start of helper function descriptions. |
| 145 | offset = self.reader.read().find('* Start of BPF helper function descriptions:') |
| 146 | if offset == -1: |
| 147 | raise Exception('Could not find start of eBPF helper descriptions list') |
| 148 | self.reader.seek(offset) |
| 149 | self.reader.readline() |
| 150 | self.reader.readline() |
| 151 | self.line = self.reader.readline() |
| 152 | |
| 153 | while True: |
| 154 | try: |
| 155 | helper = self.parse_helper() |
| 156 | self.helpers.append(helper) |
| 157 | except NoHelperFound: |
| 158 | break |
| 159 | |
| 160 | self.reader.close() |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 161 | |
| 162 | ############################################################################### |
| 163 | |
| 164 | class Printer(object): |
| 165 | """ |
| 166 | A generic class for printers. Printers should be created with an array of |
| 167 | Helper objects, and implement a way to print them in the desired fashion. |
| 168 | @helpers: array of Helper objects to print to standard output |
| 169 | """ |
| 170 | def __init__(self, helpers): |
| 171 | self.helpers = helpers |
| 172 | |
| 173 | def print_header(self): |
| 174 | pass |
| 175 | |
| 176 | def print_footer(self): |
| 177 | pass |
| 178 | |
| 179 | def print_one(self, helper): |
| 180 | pass |
| 181 | |
| 182 | def print_all(self): |
| 183 | self.print_header() |
| 184 | for helper in self.helpers: |
| 185 | self.print_one(helper) |
| 186 | self.print_footer() |
| 187 | |
| 188 | class PrinterRST(Printer): |
| 189 | """ |
| 190 | A printer for dumping collected information about helpers as a ReStructured |
| 191 | Text page compatible with the rst2man program, which can be used to |
| 192 | generate a manual page for the helpers. |
| 193 | @helpers: array of Helper objects to print to standard output |
| 194 | """ |
| 195 | def print_header(self): |
| 196 | header = '''\ |
| 197 | .. Copyright (C) All BPF authors and contributors from 2014 to present. |
| 198 | .. See git log include/uapi/linux/bpf.h in kernel tree for details. |
| 199 | .. |
| 200 | .. %%%LICENSE_START(VERBATIM) |
| 201 | .. Permission is granted to make and distribute verbatim copies of this |
| 202 | .. manual provided the copyright notice and this permission notice are |
| 203 | .. preserved on all copies. |
| 204 | .. |
| 205 | .. Permission is granted to copy and distribute modified versions of this |
| 206 | .. manual under the conditions for verbatim copying, provided that the |
| 207 | .. entire resulting derived work is distributed under the terms of a |
| 208 | .. permission notice identical to this one. |
| 209 | .. |
| 210 | .. Since the Linux kernel and libraries are constantly changing, this |
| 211 | .. manual page may be incorrect or out-of-date. The author(s) assume no |
| 212 | .. responsibility for errors or omissions, or for damages resulting from |
| 213 | .. the use of the information contained herein. The author(s) may not |
| 214 | .. have taken the same level of care in the production of this manual, |
| 215 | .. which is licensed free of charge, as they might when working |
| 216 | .. professionally. |
| 217 | .. |
| 218 | .. Formatted or processed versions of this manual, if unaccompanied by |
| 219 | .. the source, must acknowledge the copyright and authors of this work. |
| 220 | .. %%%LICENSE_END |
| 221 | .. |
| 222 | .. Please do not edit this file. It was generated from the documentation |
| 223 | .. located in file include/uapi/linux/bpf.h of the Linux kernel sources |
| 224 | .. (helpers description), and from scripts/bpf_helpers_doc.py in the same |
| 225 | .. repository (header and footer). |
| 226 | |
| 227 | =========== |
| 228 | BPF-HELPERS |
| 229 | =========== |
| 230 | ------------------------------------------------------------------------------- |
| 231 | list of eBPF helper functions |
| 232 | ------------------------------------------------------------------------------- |
| 233 | |
| 234 | :Manual section: 7 |
| 235 | |
| 236 | DESCRIPTION |
| 237 | =========== |
| 238 | |
| 239 | The extended Berkeley Packet Filter (eBPF) subsystem consists in programs |
| 240 | written in a pseudo-assembly language, then attached to one of the several |
| 241 | kernel hooks and run in reaction of specific events. This framework differs |
| 242 | from the older, "classic" BPF (or "cBPF") in several aspects, one of them being |
| 243 | the ability to call special functions (or "helpers") from within a program. |
| 244 | These functions are restricted to a white-list of helpers defined in the |
| 245 | kernel. |
| 246 | |
| 247 | These helpers are used by eBPF programs to interact with the system, or with |
| 248 | the context in which they work. For instance, they can be used to print |
| 249 | debugging messages, to get the time since the system was booted, to interact |
| 250 | with eBPF maps, or to manipulate network packets. Since there are several eBPF |
| 251 | program types, and that they do not run in the same context, each program type |
| 252 | can only call a subset of those helpers. |
| 253 | |
| 254 | Due to eBPF conventions, a helper can not have more than five arguments. |
| 255 | |
| 256 | Internally, eBPF programs call directly into the compiled helper functions |
| 257 | without requiring any foreign-function interface. As a result, calling helpers |
| 258 | introduces no overhead, thus offering excellent performance. |
| 259 | |
| 260 | This document is an attempt to list and document the helpers available to eBPF |
| 261 | developers. They are sorted by chronological order (the oldest helpers in the |
| 262 | kernel at the top). |
| 263 | |
| 264 | HELPERS |
| 265 | ======= |
| 266 | ''' |
| 267 | print(header) |
| 268 | |
| 269 | def print_footer(self): |
| 270 | footer = ''' |
| 271 | EXAMPLES |
| 272 | ======== |
| 273 | |
| 274 | Example usage for most of the eBPF helpers listed in this manual page are |
| 275 | available within the Linux kernel sources, at the following locations: |
| 276 | |
| 277 | * *samples/bpf/* |
| 278 | * *tools/testing/selftests/bpf/* |
| 279 | |
| 280 | LICENSE |
| 281 | ======= |
| 282 | |
| 283 | eBPF programs can have an associated license, passed along with the bytecode |
| 284 | instructions to the kernel when the programs are loaded. The format for that |
| 285 | string is identical to the one in use for kernel modules (Dual licenses, such |
| 286 | as "Dual BSD/GPL", may be used). Some helper functions are only accessible to |
| 287 | programs that are compatible with the GNU Privacy License (GPL). |
| 288 | |
| 289 | In order to use such helpers, the eBPF program must be loaded with the correct |
| 290 | license string passed (via **attr**) to the **bpf**\ () system call, and this |
| 291 | generally translates into the C source code of the program containing a line |
| 292 | similar to the following: |
| 293 | |
| 294 | :: |
| 295 | |
| 296 | char ____license[] __attribute__((section("license"), used)) = "GPL"; |
| 297 | |
| 298 | IMPLEMENTATION |
| 299 | ============== |
| 300 | |
| 301 | This manual page is an effort to document the existing eBPF helper functions. |
| 302 | But as of this writing, the BPF sub-system is under heavy development. New eBPF |
| 303 | program or map types are added, along with new helper functions. Some helpers |
| 304 | are occasionally made available for additional program types. So in spite of |
| 305 | the efforts of the community, this page might not be up-to-date. If you want to |
| 306 | check by yourself what helper functions exist in your kernel, or what types of |
| 307 | programs they can support, here are some files among the kernel tree that you |
| 308 | may be interested in: |
| 309 | |
| 310 | * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list |
| 311 | of all helper functions, as well as many other BPF definitions including most |
| 312 | of the flags, structs or constants used by the helpers. |
| 313 | * *net/core/filter.c* contains the definition of most network-related helper |
| 314 | functions, and the list of program types from which they can be used. |
| 315 | * *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related |
| 316 | helpers. |
| 317 | * *kernel/bpf/verifier.c* contains the functions used to check that valid types |
| 318 | of eBPF maps are used with a given helper function. |
| 319 | * *kernel/bpf/* directory contains other files in which additional helpers are |
| 320 | defined (for cgroups, sockmaps, etc.). |
Quentin Monnet | ab8d780 | 2020-05-11 17:15:35 +0100 | [diff] [blame] | 321 | * The bpftool utility can be used to probe the availability of helper functions |
| 322 | on the system (as well as supported program and map types, and a number of |
| 323 | other parameters). To do so, run **bpftool feature probe** (see |
| 324 | **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to |
| 325 | list features available to unprivileged users. |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 326 | |
| 327 | Compatibility between helper functions and program types can generally be found |
| 328 | in the files where helper functions are defined. Look for the **struct |
| 329 | bpf_func_proto** objects and for functions returning them: these functions |
| 330 | contain a list of helpers that a given program type can call. Note that the |
| 331 | **default:** label of the **switch ... case** used to filter helpers can call |
| 332 | other functions, themselves allowing access to additional helpers. The |
| 333 | requirement for GPL license is also in those **struct bpf_func_proto**. |
| 334 | |
| 335 | Compatibility between helper functions and map types can be found in the |
| 336 | **check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*. |
| 337 | |
| 338 | Helper functions that invalidate the checks on **data** and **data_end** |
| 339 | pointers for network processing are listed in function |
| 340 | **bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*. |
| 341 | |
| 342 | SEE ALSO |
| 343 | ======== |
| 344 | |
| 345 | **bpf**\ (2), |
Quentin Monnet | ab8d780 | 2020-05-11 17:15:35 +0100 | [diff] [blame] | 346 | **bpftool**\ (8), |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 347 | **cgroups**\ (7), |
| 348 | **ip**\ (8), |
| 349 | **perf_event_open**\ (2), |
| 350 | **sendmsg**\ (2), |
| 351 | **socket**\ (7), |
| 352 | **tc-bpf**\ (8)''' |
| 353 | print(footer) |
| 354 | |
| 355 | def print_proto(self, helper): |
| 356 | """ |
| 357 | Format function protocol with bold and italics markers. This makes RST |
| 358 | file less readable, but gives nice results in the manual page. |
| 359 | """ |
| 360 | proto = helper.proto_break_down() |
| 361 | |
| 362 | print('**%s %s%s(' % (proto['ret_type'], |
| 363 | proto['ret_star'].replace('*', '\\*'), |
| 364 | proto['name']), |
| 365 | end='') |
| 366 | |
| 367 | comma = '' |
| 368 | for a in proto['args']: |
| 369 | one_arg = '{}{}'.format(comma, a['type']) |
| 370 | if a['name']: |
| 371 | if a['star']: |
| 372 | one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*')) |
| 373 | else: |
| 374 | one_arg += '** ' |
| 375 | one_arg += '*{}*\\ **'.format(a['name']) |
| 376 | comma = ', ' |
| 377 | print(one_arg, end='') |
| 378 | |
| 379 | print(')**') |
| 380 | |
| 381 | def print_one(self, helper): |
| 382 | self.print_proto(helper) |
| 383 | |
| 384 | if (helper.desc): |
| 385 | print('\tDescription') |
| 386 | # Do not strip all newline characters: formatted code at the end of |
| 387 | # a section must be followed by a blank line. |
| 388 | for line in re.sub('\n$', '', helper.desc, count=1).split('\n'): |
| 389 | print('{}{}'.format('\t\t' if line else '', line)) |
| 390 | |
| 391 | if (helper.ret): |
| 392 | print('\tReturn') |
| 393 | for line in helper.ret.rstrip().split('\n'): |
| 394 | print('{}{}'.format('\t\t' if line else '', line)) |
| 395 | |
| 396 | print('') |
| 397 | |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 398 | class PrinterHelpers(Printer): |
| 399 | """ |
| 400 | A printer for dumping collected information about helpers as C header to |
| 401 | be included from BPF program. |
| 402 | @helpers: array of Helper objects to print to standard output |
| 403 | """ |
| 404 | |
| 405 | type_fwds = [ |
| 406 | 'struct bpf_fib_lookup', |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 407 | 'struct bpf_sk_lookup', |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 408 | 'struct bpf_perf_event_data', |
| 409 | 'struct bpf_perf_event_value', |
Carlos Neira | 5996a58 | 2020-03-13 12:46:50 -0300 | [diff] [blame] | 410 | 'struct bpf_pidns_info', |
Andrii Nakryiko | 821f5c9 | 2020-10-28 11:12:04 -0700 | [diff] [blame] | 411 | 'struct bpf_redir_neigh', |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 412 | 'struct bpf_sock', |
| 413 | 'struct bpf_sock_addr', |
| 414 | 'struct bpf_sock_ops', |
| 415 | 'struct bpf_sock_tuple', |
| 416 | 'struct bpf_spin_lock', |
| 417 | 'struct bpf_sysctl', |
| 418 | 'struct bpf_tcp_sock', |
| 419 | 'struct bpf_tunnel_key', |
| 420 | 'struct bpf_xfrm_state', |
KP Singh | 3f6719c | 2020-11-17 23:29:28 +0000 | [diff] [blame] | 421 | 'struct linux_binprm', |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 422 | 'struct pt_regs', |
| 423 | 'struct sk_reuseport_md', |
| 424 | 'struct sockaddr', |
| 425 | 'struct tcphdr', |
Yonghong Song | 492e639 | 2020-05-09 10:59:14 -0700 | [diff] [blame] | 426 | 'struct seq_file', |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 427 | 'struct tcp6_sock', |
Yonghong Song | 478cfbd | 2020-06-23 16:08:11 -0700 | [diff] [blame] | 428 | 'struct tcp_sock', |
| 429 | 'struct tcp_timewait_sock', |
| 430 | 'struct tcp_request_sock', |
Yonghong Song | 0d4fad3 | 2020-06-23 16:08:15 -0700 | [diff] [blame] | 431 | 'struct udp6_sock', |
Song Liu | fa28dcb | 2020-06-29 23:28:44 -0700 | [diff] [blame] | 432 | 'struct task_struct', |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 433 | |
| 434 | 'struct __sk_buff', |
| 435 | 'struct sk_msg_md', |
Andrii Nakryiko | e0b68fb | 2019-10-09 21:25:34 -0700 | [diff] [blame] | 436 | 'struct xdp_md', |
Jiri Olsa | 6e22ab9 | 2020-08-25 21:21:20 +0200 | [diff] [blame] | 437 | 'struct path', |
Alan Maguire | c4d0bfb | 2020-09-28 12:31:05 +0100 | [diff] [blame] | 438 | 'struct btf_ptr', |
KP Singh | 27672f0 | 2020-11-24 15:12:09 +0000 | [diff] [blame] | 439 | 'struct inode', |
Florent Revest | 4f19cab | 2020-12-04 12:36:05 +0100 | [diff] [blame] | 440 | 'struct socket', |
| 441 | 'struct file', |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 442 | ] |
| 443 | known_types = { |
| 444 | '...', |
| 445 | 'void', |
| 446 | 'const void', |
| 447 | 'char', |
| 448 | 'const char', |
| 449 | 'int', |
| 450 | 'long', |
| 451 | 'unsigned long', |
| 452 | |
| 453 | '__be16', |
| 454 | '__be32', |
| 455 | '__wsum', |
| 456 | |
| 457 | 'struct bpf_fib_lookup', |
| 458 | 'struct bpf_perf_event_data', |
| 459 | 'struct bpf_perf_event_value', |
Carlos Neira | b4490c5 | 2020-03-04 17:41:56 -0300 | [diff] [blame] | 460 | 'struct bpf_pidns_info', |
Toke Høiland-Jørgensen | ba452c9 | 2020-10-20 23:25:56 +0200 | [diff] [blame] | 461 | 'struct bpf_redir_neigh', |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 462 | 'struct bpf_sk_lookup', |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 463 | 'struct bpf_sock', |
| 464 | 'struct bpf_sock_addr', |
| 465 | 'struct bpf_sock_ops', |
| 466 | 'struct bpf_sock_tuple', |
| 467 | 'struct bpf_spin_lock', |
| 468 | 'struct bpf_sysctl', |
| 469 | 'struct bpf_tcp_sock', |
| 470 | 'struct bpf_tunnel_key', |
| 471 | 'struct bpf_xfrm_state', |
KP Singh | 3f6719c | 2020-11-17 23:29:28 +0000 | [diff] [blame] | 472 | 'struct linux_binprm', |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 473 | 'struct pt_regs', |
| 474 | 'struct sk_reuseport_md', |
| 475 | 'struct sockaddr', |
| 476 | 'struct tcphdr', |
Yonghong Song | 492e639 | 2020-05-09 10:59:14 -0700 | [diff] [blame] | 477 | 'struct seq_file', |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 478 | 'struct tcp6_sock', |
Yonghong Song | 478cfbd | 2020-06-23 16:08:11 -0700 | [diff] [blame] | 479 | 'struct tcp_sock', |
| 480 | 'struct tcp_timewait_sock', |
| 481 | 'struct tcp_request_sock', |
Yonghong Song | 0d4fad3 | 2020-06-23 16:08:15 -0700 | [diff] [blame] | 482 | 'struct udp6_sock', |
Song Liu | fa28dcb | 2020-06-29 23:28:44 -0700 | [diff] [blame] | 483 | 'struct task_struct', |
Jiri Olsa | 6e22ab9 | 2020-08-25 21:21:20 +0200 | [diff] [blame] | 484 | 'struct path', |
Alan Maguire | c4d0bfb | 2020-09-28 12:31:05 +0100 | [diff] [blame] | 485 | 'struct btf_ptr', |
KP Singh | 27672f0 | 2020-11-24 15:12:09 +0000 | [diff] [blame] | 486 | 'struct inode', |
Florent Revest | 4f19cab | 2020-12-04 12:36:05 +0100 | [diff] [blame] | 487 | 'struct socket', |
| 488 | 'struct file', |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 489 | } |
| 490 | mapped_types = { |
| 491 | 'u8': '__u8', |
| 492 | 'u16': '__u16', |
| 493 | 'u32': '__u32', |
| 494 | 'u64': '__u64', |
| 495 | 's8': '__s8', |
| 496 | 's16': '__s16', |
| 497 | 's32': '__s32', |
| 498 | 's64': '__s64', |
| 499 | 'size_t': 'unsigned long', |
| 500 | 'struct bpf_map': 'void', |
| 501 | 'struct sk_buff': 'struct __sk_buff', |
| 502 | 'const struct sk_buff': 'const struct __sk_buff', |
| 503 | 'struct sk_msg_buff': 'struct sk_msg_md', |
| 504 | 'struct xdp_buff': 'struct xdp_md', |
| 505 | } |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 506 | # Helpers overloaded for different context types. |
| 507 | overloaded_helpers = [ |
| 508 | 'bpf_get_socket_cookie', |
| 509 | 'bpf_sk_assign', |
| 510 | ] |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 511 | |
| 512 | def print_header(self): |
| 513 | header = '''\ |
| 514 | /* This is auto-generated file. See bpf_helpers_doc.py for details. */ |
| 515 | |
| 516 | /* Forward declarations of BPF structs */''' |
| 517 | |
| 518 | print(header) |
| 519 | for fwd in self.type_fwds: |
| 520 | print('%s;' % fwd) |
| 521 | print('') |
| 522 | |
| 523 | def print_footer(self): |
| 524 | footer = '' |
| 525 | print(footer) |
| 526 | |
| 527 | def map_type(self, t): |
| 528 | if t in self.known_types: |
| 529 | return t |
| 530 | if t in self.mapped_types: |
| 531 | return self.mapped_types[t] |
Jakub Sitnicki | ab81e20 | 2019-10-20 13:23:44 +0200 | [diff] [blame] | 532 | print("Unrecognized type '%s', please add it to known types!" % t, |
| 533 | file=sys.stderr) |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 534 | sys.exit(1) |
| 535 | |
| 536 | seen_helpers = set() |
| 537 | |
| 538 | def print_one(self, helper): |
| 539 | proto = helper.proto_break_down() |
| 540 | |
| 541 | if proto['name'] in self.seen_helpers: |
| 542 | return |
| 543 | self.seen_helpers.add(proto['name']) |
| 544 | |
| 545 | print('/*') |
| 546 | print(" * %s" % proto['name']) |
| 547 | print(" *") |
| 548 | if (helper.desc): |
| 549 | # Do not strip all newline characters: formatted code at the end of |
| 550 | # a section must be followed by a blank line. |
| 551 | for line in re.sub('\n$', '', helper.desc, count=1).split('\n'): |
| 552 | print(' *{}{}'.format(' \t' if line else '', line)) |
| 553 | |
| 554 | if (helper.ret): |
| 555 | print(' *') |
| 556 | print(' * Returns') |
| 557 | for line in helper.ret.rstrip().split('\n'): |
| 558 | print(' *{}{}'.format(' \t' if line else '', line)) |
| 559 | |
| 560 | print(' */') |
| 561 | print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']), |
| 562 | proto['ret_star'], proto['name']), end='') |
| 563 | comma = '' |
| 564 | for i, a in enumerate(proto['args']): |
| 565 | t = a['type'] |
| 566 | n = a['name'] |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 567 | if proto['name'] in self.overloaded_helpers and i == 0: |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 568 | t = 'void' |
| 569 | n = 'ctx' |
| 570 | one_arg = '{}{}'.format(comma, self.map_type(t)) |
| 571 | if n: |
| 572 | if a['star']: |
| 573 | one_arg += ' {}'.format(a['star']) |
| 574 | else: |
| 575 | one_arg += ' ' |
| 576 | one_arg += '{}'.format(n) |
| 577 | comma = ', ' |
| 578 | print(one_arg, end='') |
| 579 | |
| 580 | print(') = (void *) %d;' % len(self.seen_helpers)) |
| 581 | print('') |
| 582 | |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 583 | ############################################################################### |
| 584 | |
| 585 | # If script is launched from scripts/ from kernel tree and can access |
| 586 | # ../include/uapi/linux/bpf.h, use it as a default name for the file to parse, |
| 587 | # otherwise the --filename argument will be required from the command line. |
| 588 | script = os.path.abspath(sys.argv[0]) |
| 589 | linuxRoot = os.path.dirname(os.path.dirname(script)) |
| 590 | bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h') |
| 591 | |
| 592 | argParser = argparse.ArgumentParser(description=""" |
| 593 | Parse eBPF header file and generate documentation for eBPF helper functions. |
| 594 | The RST-formatted output produced can be turned into a manual page with the |
| 595 | rst2man utility. |
| 596 | """) |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 597 | argParser.add_argument('--header', action='store_true', |
| 598 | help='generate C header file') |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 599 | if (os.path.isfile(bpfh)): |
| 600 | argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h', |
| 601 | default=bpfh) |
| 602 | else: |
| 603 | argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h') |
| 604 | args = argParser.parse_args() |
| 605 | |
| 606 | # Parse file. |
| 607 | headerParser = HeaderParser(args.filename) |
| 608 | headerParser.run() |
| 609 | |
| 610 | # Print formatted output to standard output. |
Andrii Nakryiko | 7a387be | 2019-10-06 20:07:37 -0700 | [diff] [blame] | 611 | if args.header: |
| 612 | printer = PrinterHelpers(headerParser.helpers) |
| 613 | else: |
| 614 | printer = PrinterRST(headerParser.helpers) |
Quentin Monnet | 56a092c | 2018-04-25 18:16:52 +0100 | [diff] [blame] | 615 | printer.print_all() |