18c2ecf20Sopenharmony_ci#!/usr/bin/env python3
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
38c2ecf20Sopenharmony_ci#
48c2ecf20Sopenharmony_ci# Copyright (C) 2018-2019 Netronome Systems, Inc.
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci# In case user attempts to run with Python 2.
78c2ecf20Sopenharmony_cifrom __future__ import print_function
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciimport argparse
108c2ecf20Sopenharmony_ciimport re
118c2ecf20Sopenharmony_ciimport sys, os
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciclass NoHelperFound(BaseException):
148c2ecf20Sopenharmony_ci    pass
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciclass ParsingError(BaseException):
178c2ecf20Sopenharmony_ci    def __init__(self, line='<line not provided>', reader=None):
188c2ecf20Sopenharmony_ci        if reader:
198c2ecf20Sopenharmony_ci            BaseException.__init__(self,
208c2ecf20Sopenharmony_ci                                   'Error at file offset %d, parsing line: %s' %
218c2ecf20Sopenharmony_ci                                   (reader.tell(), line))
228c2ecf20Sopenharmony_ci        else:
238c2ecf20Sopenharmony_ci            BaseException.__init__(self, 'Error parsing line: %s' % line)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciclass Helper(object):
268c2ecf20Sopenharmony_ci    """
278c2ecf20Sopenharmony_ci    An object representing the description of an eBPF helper function.
288c2ecf20Sopenharmony_ci    @proto: function prototype of the helper function
298c2ecf20Sopenharmony_ci    @desc: textual description of the helper function
308c2ecf20Sopenharmony_ci    @ret: description of the return value of the helper function
318c2ecf20Sopenharmony_ci    """
328c2ecf20Sopenharmony_ci    def __init__(self, proto='', desc='', ret=''):
338c2ecf20Sopenharmony_ci        self.proto = proto
348c2ecf20Sopenharmony_ci        self.desc = desc
358c2ecf20Sopenharmony_ci        self.ret = ret
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci    def proto_break_down(self):
388c2ecf20Sopenharmony_ci        """
398c2ecf20Sopenharmony_ci        Break down helper function protocol into smaller chunks: return type,
408c2ecf20Sopenharmony_ci        name, distincts arguments.
418c2ecf20Sopenharmony_ci        """
428c2ecf20Sopenharmony_ci        arg_re = re.compile('((\w+ )*?(\w+|...))( (\**)(\w+))?$')
438c2ecf20Sopenharmony_ci        res = {}
448c2ecf20Sopenharmony_ci        proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci        capture = proto_re.match(self.proto)
478c2ecf20Sopenharmony_ci        res['ret_type'] = capture.group(1)
488c2ecf20Sopenharmony_ci        res['ret_star'] = capture.group(2)
498c2ecf20Sopenharmony_ci        res['name']     = capture.group(3)
508c2ecf20Sopenharmony_ci        res['args'] = []
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci        args    = capture.group(4).split(', ')
538c2ecf20Sopenharmony_ci        for a in args:
548c2ecf20Sopenharmony_ci            capture = arg_re.match(a)
558c2ecf20Sopenharmony_ci            res['args'].append({
568c2ecf20Sopenharmony_ci                'type' : capture.group(1),
578c2ecf20Sopenharmony_ci                'star' : capture.group(5),
588c2ecf20Sopenharmony_ci                'name' : capture.group(6)
598c2ecf20Sopenharmony_ci            })
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci        return res
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciclass HeaderParser(object):
648c2ecf20Sopenharmony_ci    """
658c2ecf20Sopenharmony_ci    An object used to parse a file in order to extract the documentation of a
668c2ecf20Sopenharmony_ci    list of eBPF helper functions. All the helpers that can be retrieved are
678c2ecf20Sopenharmony_ci    stored as Helper object, in the self.helpers() array.
688c2ecf20Sopenharmony_ci    @filename: name of file to parse, usually include/uapi/linux/bpf.h in the
698c2ecf20Sopenharmony_ci               kernel tree
708c2ecf20Sopenharmony_ci    """
718c2ecf20Sopenharmony_ci    def __init__(self, filename):
728c2ecf20Sopenharmony_ci        self.reader = open(filename, 'r')
738c2ecf20Sopenharmony_ci        self.line = ''
748c2ecf20Sopenharmony_ci        self.helpers = []
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci    def parse_helper(self):
778c2ecf20Sopenharmony_ci        proto    = self.parse_proto()
788c2ecf20Sopenharmony_ci        desc     = self.parse_desc()
798c2ecf20Sopenharmony_ci        ret      = self.parse_ret()
808c2ecf20Sopenharmony_ci        return Helper(proto=proto, desc=desc, ret=ret)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci    def parse_proto(self):
838c2ecf20Sopenharmony_ci        # Argument can be of shape:
848c2ecf20Sopenharmony_ci        #   - "void"
858c2ecf20Sopenharmony_ci        #   - "type  name"
868c2ecf20Sopenharmony_ci        #   - "type *name"
878c2ecf20Sopenharmony_ci        #   - Same as above, with "const" and/or "struct" in front of type
888c2ecf20Sopenharmony_ci        #   - "..." (undefined number of arguments, for bpf_trace_printk())
898c2ecf20Sopenharmony_ci        # There is at least one term ("void"), and at most five arguments.
908c2ecf20Sopenharmony_ci        p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')
918c2ecf20Sopenharmony_ci        capture = p.match(self.line)
928c2ecf20Sopenharmony_ci        if not capture:
938c2ecf20Sopenharmony_ci            raise NoHelperFound
948c2ecf20Sopenharmony_ci        self.line = self.reader.readline()
958c2ecf20Sopenharmony_ci        return capture.group(1)
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci    def parse_desc(self):
988c2ecf20Sopenharmony_ci        p = re.compile(' \* ?(?:\t| {5,8})Description$')
998c2ecf20Sopenharmony_ci        capture = p.match(self.line)
1008c2ecf20Sopenharmony_ci        if not capture:
1018c2ecf20Sopenharmony_ci            # Helper can have empty description and we might be parsing another
1028c2ecf20Sopenharmony_ci            # attribute: return but do not consume.
1038c2ecf20Sopenharmony_ci            return ''
1048c2ecf20Sopenharmony_ci        # Description can be several lines, some of them possibly empty, and it
1058c2ecf20Sopenharmony_ci        # stops when another subsection title is met.
1068c2ecf20Sopenharmony_ci        desc = ''
1078c2ecf20Sopenharmony_ci        while True:
1088c2ecf20Sopenharmony_ci            self.line = self.reader.readline()
1098c2ecf20Sopenharmony_ci            if self.line == ' *\n':
1108c2ecf20Sopenharmony_ci                desc += '\n'
1118c2ecf20Sopenharmony_ci            else:
1128c2ecf20Sopenharmony_ci                p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
1138c2ecf20Sopenharmony_ci                capture = p.match(self.line)
1148c2ecf20Sopenharmony_ci                if capture:
1158c2ecf20Sopenharmony_ci                    desc += capture.group(1) + '\n'
1168c2ecf20Sopenharmony_ci                else:
1178c2ecf20Sopenharmony_ci                    break
1188c2ecf20Sopenharmony_ci        return desc
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci    def parse_ret(self):
1218c2ecf20Sopenharmony_ci        p = re.compile(' \* ?(?:\t| {5,8})Return$')
1228c2ecf20Sopenharmony_ci        capture = p.match(self.line)
1238c2ecf20Sopenharmony_ci        if not capture:
1248c2ecf20Sopenharmony_ci            # Helper can have empty retval and we might be parsing another
1258c2ecf20Sopenharmony_ci            # attribute: return but do not consume.
1268c2ecf20Sopenharmony_ci            return ''
1278c2ecf20Sopenharmony_ci        # Return value description can be several lines, some of them possibly
1288c2ecf20Sopenharmony_ci        # empty, and it stops when another subsection title is met.
1298c2ecf20Sopenharmony_ci        ret = ''
1308c2ecf20Sopenharmony_ci        while True:
1318c2ecf20Sopenharmony_ci            self.line = self.reader.readline()
1328c2ecf20Sopenharmony_ci            if self.line == ' *\n':
1338c2ecf20Sopenharmony_ci                ret += '\n'
1348c2ecf20Sopenharmony_ci            else:
1358c2ecf20Sopenharmony_ci                p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
1368c2ecf20Sopenharmony_ci                capture = p.match(self.line)
1378c2ecf20Sopenharmony_ci                if capture:
1388c2ecf20Sopenharmony_ci                    ret += capture.group(1) + '\n'
1398c2ecf20Sopenharmony_ci                else:
1408c2ecf20Sopenharmony_ci                    break
1418c2ecf20Sopenharmony_ci        return ret
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci    def run(self):
1448c2ecf20Sopenharmony_ci        # Advance to start of helper function descriptions.
1458c2ecf20Sopenharmony_ci        offset = self.reader.read().find('* Start of BPF helper function descriptions:')
1468c2ecf20Sopenharmony_ci        if offset == -1:
1478c2ecf20Sopenharmony_ci            raise Exception('Could not find start of eBPF helper descriptions list')
1488c2ecf20Sopenharmony_ci        self.reader.seek(offset)
1498c2ecf20Sopenharmony_ci        self.reader.readline()
1508c2ecf20Sopenharmony_ci        self.reader.readline()
1518c2ecf20Sopenharmony_ci        self.line = self.reader.readline()
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci        while True:
1548c2ecf20Sopenharmony_ci            try:
1558c2ecf20Sopenharmony_ci                helper = self.parse_helper()
1568c2ecf20Sopenharmony_ci                self.helpers.append(helper)
1578c2ecf20Sopenharmony_ci            except NoHelperFound:
1588c2ecf20Sopenharmony_ci                break
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci        self.reader.close()
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci###############################################################################
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciclass Printer(object):
1658c2ecf20Sopenharmony_ci    """
1668c2ecf20Sopenharmony_ci    A generic class for printers. Printers should be created with an array of
1678c2ecf20Sopenharmony_ci    Helper objects, and implement a way to print them in the desired fashion.
1688c2ecf20Sopenharmony_ci    @helpers: array of Helper objects to print to standard output
1698c2ecf20Sopenharmony_ci    """
1708c2ecf20Sopenharmony_ci    def __init__(self, helpers):
1718c2ecf20Sopenharmony_ci        self.helpers = helpers
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci    def print_header(self):
1748c2ecf20Sopenharmony_ci        pass
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci    def print_footer(self):
1778c2ecf20Sopenharmony_ci        pass
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci    def print_one(self, helper):
1808c2ecf20Sopenharmony_ci        pass
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci    def print_all(self):
1838c2ecf20Sopenharmony_ci        self.print_header()
1848c2ecf20Sopenharmony_ci        for helper in self.helpers:
1858c2ecf20Sopenharmony_ci            self.print_one(helper)
1868c2ecf20Sopenharmony_ci        self.print_footer()
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ciclass PrinterRST(Printer):
1898c2ecf20Sopenharmony_ci    """
1908c2ecf20Sopenharmony_ci    A printer for dumping collected information about helpers as a ReStructured
1918c2ecf20Sopenharmony_ci    Text page compatible with the rst2man program, which can be used to
1928c2ecf20Sopenharmony_ci    generate a manual page for the helpers.
1938c2ecf20Sopenharmony_ci    @helpers: array of Helper objects to print to standard output
1948c2ecf20Sopenharmony_ci    """
1958c2ecf20Sopenharmony_ci    def print_header(self):
1968c2ecf20Sopenharmony_ci        header = '''\
1978c2ecf20Sopenharmony_ci.. Copyright (C) All BPF authors and contributors from 2014 to present.
1988c2ecf20Sopenharmony_ci.. See git log include/uapi/linux/bpf.h in kernel tree for details.
1998c2ecf20Sopenharmony_ci..
2008c2ecf20Sopenharmony_ci.. %%%LICENSE_START(VERBATIM)
2018c2ecf20Sopenharmony_ci.. Permission is granted to make and distribute verbatim copies of this
2028c2ecf20Sopenharmony_ci.. manual provided the copyright notice and this permission notice are
2038c2ecf20Sopenharmony_ci.. preserved on all copies.
2048c2ecf20Sopenharmony_ci..
2058c2ecf20Sopenharmony_ci.. Permission is granted to copy and distribute modified versions of this
2068c2ecf20Sopenharmony_ci.. manual under the conditions for verbatim copying, provided that the
2078c2ecf20Sopenharmony_ci.. entire resulting derived work is distributed under the terms of a
2088c2ecf20Sopenharmony_ci.. permission notice identical to this one.
2098c2ecf20Sopenharmony_ci..
2108c2ecf20Sopenharmony_ci.. Since the Linux kernel and libraries are constantly changing, this
2118c2ecf20Sopenharmony_ci.. manual page may be incorrect or out-of-date.  The author(s) assume no
2128c2ecf20Sopenharmony_ci.. responsibility for errors or omissions, or for damages resulting from
2138c2ecf20Sopenharmony_ci.. the use of the information contained herein.  The author(s) may not
2148c2ecf20Sopenharmony_ci.. have taken the same level of care in the production of this manual,
2158c2ecf20Sopenharmony_ci.. which is licensed free of charge, as they might when working
2168c2ecf20Sopenharmony_ci.. professionally.
2178c2ecf20Sopenharmony_ci..
2188c2ecf20Sopenharmony_ci.. Formatted or processed versions of this manual, if unaccompanied by
2198c2ecf20Sopenharmony_ci.. the source, must acknowledge the copyright and authors of this work.
2208c2ecf20Sopenharmony_ci.. %%%LICENSE_END
2218c2ecf20Sopenharmony_ci..
2228c2ecf20Sopenharmony_ci.. Please do not edit this file. It was generated from the documentation
2238c2ecf20Sopenharmony_ci.. located in file include/uapi/linux/bpf.h of the Linux kernel sources
2248c2ecf20Sopenharmony_ci.. (helpers description), and from scripts/bpf_helpers_doc.py in the same
2258c2ecf20Sopenharmony_ci.. repository (header and footer).
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci===========
2288c2ecf20Sopenharmony_ciBPF-HELPERS
2298c2ecf20Sopenharmony_ci===========
2308c2ecf20Sopenharmony_ci-------------------------------------------------------------------------------
2318c2ecf20Sopenharmony_cilist of eBPF helper functions
2328c2ecf20Sopenharmony_ci-------------------------------------------------------------------------------
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci:Manual section: 7
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ciDESCRIPTION
2378c2ecf20Sopenharmony_ci===========
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ciThe extended Berkeley Packet Filter (eBPF) subsystem consists in programs
2408c2ecf20Sopenharmony_ciwritten in a pseudo-assembly language, then attached to one of the several
2418c2ecf20Sopenharmony_cikernel hooks and run in reaction of specific events. This framework differs
2428c2ecf20Sopenharmony_cifrom the older, "classic" BPF (or "cBPF") in several aspects, one of them being
2438c2ecf20Sopenharmony_cithe ability to call special functions (or "helpers") from within a program.
2448c2ecf20Sopenharmony_ciThese functions are restricted to a white-list of helpers defined in the
2458c2ecf20Sopenharmony_cikernel.
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ciThese helpers are used by eBPF programs to interact with the system, or with
2488c2ecf20Sopenharmony_cithe context in which they work. For instance, they can be used to print
2498c2ecf20Sopenharmony_cidebugging messages, to get the time since the system was booted, to interact
2508c2ecf20Sopenharmony_ciwith eBPF maps, or to manipulate network packets. Since there are several eBPF
2518c2ecf20Sopenharmony_ciprogram types, and that they do not run in the same context, each program type
2528c2ecf20Sopenharmony_cican only call a subset of those helpers.
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciDue to eBPF conventions, a helper can not have more than five arguments.
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ciInternally, eBPF programs call directly into the compiled helper functions
2578c2ecf20Sopenharmony_ciwithout requiring any foreign-function interface. As a result, calling helpers
2588c2ecf20Sopenharmony_ciintroduces no overhead, thus offering excellent performance.
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ciThis document is an attempt to list and document the helpers available to eBPF
2618c2ecf20Sopenharmony_cidevelopers. They are sorted by chronological order (the oldest helpers in the
2628c2ecf20Sopenharmony_cikernel at the top).
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ciHELPERS
2658c2ecf20Sopenharmony_ci=======
2668c2ecf20Sopenharmony_ci'''
2678c2ecf20Sopenharmony_ci        print(header)
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci    def print_footer(self):
2708c2ecf20Sopenharmony_ci        footer = '''
2718c2ecf20Sopenharmony_ciEXAMPLES
2728c2ecf20Sopenharmony_ci========
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ciExample usage for most of the eBPF helpers listed in this manual page are
2758c2ecf20Sopenharmony_ciavailable within the Linux kernel sources, at the following locations:
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci* *samples/bpf/*
2788c2ecf20Sopenharmony_ci* *tools/testing/selftests/bpf/*
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ciLICENSE
2818c2ecf20Sopenharmony_ci=======
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cieBPF programs can have an associated license, passed along with the bytecode
2848c2ecf20Sopenharmony_ciinstructions to the kernel when the programs are loaded. The format for that
2858c2ecf20Sopenharmony_cistring is identical to the one in use for kernel modules (Dual licenses, such
2868c2ecf20Sopenharmony_cias "Dual BSD/GPL", may be used). Some helper functions are only accessible to
2878c2ecf20Sopenharmony_ciprograms that are compatible with the GNU Privacy License (GPL).
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ciIn order to use such helpers, the eBPF program must be loaded with the correct
2908c2ecf20Sopenharmony_cilicense string passed (via **attr**) to the **bpf**\ () system call, and this
2918c2ecf20Sopenharmony_cigenerally translates into the C source code of the program containing a line
2928c2ecf20Sopenharmony_cisimilar to the following:
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci::
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	char ____license[] __attribute__((section("license"), used)) = "GPL";
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ciIMPLEMENTATION
2998c2ecf20Sopenharmony_ci==============
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ciThis manual page is an effort to document the existing eBPF helper functions.
3028c2ecf20Sopenharmony_ciBut as of this writing, the BPF sub-system is under heavy development. New eBPF
3038c2ecf20Sopenharmony_ciprogram or map types are added, along with new helper functions. Some helpers
3048c2ecf20Sopenharmony_ciare occasionally made available for additional program types. So in spite of
3058c2ecf20Sopenharmony_cithe efforts of the community, this page might not be up-to-date. If you want to
3068c2ecf20Sopenharmony_cicheck by yourself what helper functions exist in your kernel, or what types of
3078c2ecf20Sopenharmony_ciprograms they can support, here are some files among the kernel tree that you
3088c2ecf20Sopenharmony_cimay be interested in:
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci* *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
3118c2ecf20Sopenharmony_ci  of all helper functions, as well as many other BPF definitions including most
3128c2ecf20Sopenharmony_ci  of the flags, structs or constants used by the helpers.
3138c2ecf20Sopenharmony_ci* *net/core/filter.c* contains the definition of most network-related helper
3148c2ecf20Sopenharmony_ci  functions, and the list of program types from which they can be used.
3158c2ecf20Sopenharmony_ci* *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
3168c2ecf20Sopenharmony_ci  helpers.
3178c2ecf20Sopenharmony_ci* *kernel/bpf/verifier.c* contains the functions used to check that valid types
3188c2ecf20Sopenharmony_ci  of eBPF maps are used with a given helper function.
3198c2ecf20Sopenharmony_ci* *kernel/bpf/* directory contains other files in which additional helpers are
3208c2ecf20Sopenharmony_ci  defined (for cgroups, sockmaps, etc.).
3218c2ecf20Sopenharmony_ci* The bpftool utility can be used to probe the availability of helper functions
3228c2ecf20Sopenharmony_ci  on the system (as well as supported program and map types, and a number of
3238c2ecf20Sopenharmony_ci  other parameters). To do so, run **bpftool feature probe** (see
3248c2ecf20Sopenharmony_ci  **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to
3258c2ecf20Sopenharmony_ci  list features available to unprivileged users.
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ciCompatibility between helper functions and program types can generally be found
3288c2ecf20Sopenharmony_ciin the files where helper functions are defined. Look for the **struct
3298c2ecf20Sopenharmony_cibpf_func_proto** objects and for functions returning them: these functions
3308c2ecf20Sopenharmony_cicontain a list of helpers that a given program type can call. Note that the
3318c2ecf20Sopenharmony_ci**default:** label of the **switch ... case** used to filter helpers can call
3328c2ecf20Sopenharmony_ciother functions, themselves allowing access to additional helpers. The
3338c2ecf20Sopenharmony_cirequirement for GPL license is also in those **struct bpf_func_proto**.
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ciCompatibility between helper functions and map types can be found in the
3368c2ecf20Sopenharmony_ci**check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*.
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ciHelper functions that invalidate the checks on **data** and **data_end**
3398c2ecf20Sopenharmony_cipointers for network processing are listed in function
3408c2ecf20Sopenharmony_ci**bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*.
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ciSEE ALSO
3438c2ecf20Sopenharmony_ci========
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci**bpf**\ (2),
3468c2ecf20Sopenharmony_ci**bpftool**\ (8),
3478c2ecf20Sopenharmony_ci**cgroups**\ (7),
3488c2ecf20Sopenharmony_ci**ip**\ (8),
3498c2ecf20Sopenharmony_ci**perf_event_open**\ (2),
3508c2ecf20Sopenharmony_ci**sendmsg**\ (2),
3518c2ecf20Sopenharmony_ci**socket**\ (7),
3528c2ecf20Sopenharmony_ci**tc-bpf**\ (8)'''
3538c2ecf20Sopenharmony_ci        print(footer)
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci    def print_proto(self, helper):
3568c2ecf20Sopenharmony_ci        """
3578c2ecf20Sopenharmony_ci        Format function protocol with bold and italics markers. This makes RST
3588c2ecf20Sopenharmony_ci        file less readable, but gives nice results in the manual page.
3598c2ecf20Sopenharmony_ci        """
3608c2ecf20Sopenharmony_ci        proto = helper.proto_break_down()
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci        print('**%s %s%s(' % (proto['ret_type'],
3638c2ecf20Sopenharmony_ci                              proto['ret_star'].replace('*', '\\*'),
3648c2ecf20Sopenharmony_ci                              proto['name']),
3658c2ecf20Sopenharmony_ci              end='')
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci        comma = ''
3688c2ecf20Sopenharmony_ci        for a in proto['args']:
3698c2ecf20Sopenharmony_ci            one_arg = '{}{}'.format(comma, a['type'])
3708c2ecf20Sopenharmony_ci            if a['name']:
3718c2ecf20Sopenharmony_ci                if a['star']:
3728c2ecf20Sopenharmony_ci                    one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*'))
3738c2ecf20Sopenharmony_ci                else:
3748c2ecf20Sopenharmony_ci                    one_arg += '** '
3758c2ecf20Sopenharmony_ci                one_arg += '*{}*\\ **'.format(a['name'])
3768c2ecf20Sopenharmony_ci            comma = ', '
3778c2ecf20Sopenharmony_ci            print(one_arg, end='')
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci        print(')**')
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci    def print_one(self, helper):
3828c2ecf20Sopenharmony_ci        self.print_proto(helper)
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci        if (helper.desc):
3858c2ecf20Sopenharmony_ci            print('\tDescription')
3868c2ecf20Sopenharmony_ci            # Do not strip all newline characters: formatted code at the end of
3878c2ecf20Sopenharmony_ci            # a section must be followed by a blank line.
3888c2ecf20Sopenharmony_ci            for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
3898c2ecf20Sopenharmony_ci                print('{}{}'.format('\t\t' if line else '', line))
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci        if (helper.ret):
3928c2ecf20Sopenharmony_ci            print('\tReturn')
3938c2ecf20Sopenharmony_ci            for line in helper.ret.rstrip().split('\n'):
3948c2ecf20Sopenharmony_ci                print('{}{}'.format('\t\t' if line else '', line))
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci        print('')
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ciclass PrinterHelpers(Printer):
3998c2ecf20Sopenharmony_ci    """
4008c2ecf20Sopenharmony_ci    A printer for dumping collected information about helpers as C header to
4018c2ecf20Sopenharmony_ci    be included from BPF program.
4028c2ecf20Sopenharmony_ci    @helpers: array of Helper objects to print to standard output
4038c2ecf20Sopenharmony_ci    """
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci    type_fwds = [
4068c2ecf20Sopenharmony_ci            'struct bpf_fib_lookup',
4078c2ecf20Sopenharmony_ci            'struct bpf_sk_lookup',
4088c2ecf20Sopenharmony_ci            'struct bpf_perf_event_data',
4098c2ecf20Sopenharmony_ci            'struct bpf_perf_event_value',
4108c2ecf20Sopenharmony_ci            'struct bpf_pidns_info',
4118c2ecf20Sopenharmony_ci            'struct bpf_redir_neigh',
4128c2ecf20Sopenharmony_ci            'struct bpf_sock',
4138c2ecf20Sopenharmony_ci            'struct bpf_sock_addr',
4148c2ecf20Sopenharmony_ci            'struct bpf_sock_ops',
4158c2ecf20Sopenharmony_ci            'struct bpf_sock_tuple',
4168c2ecf20Sopenharmony_ci            'struct bpf_spin_lock',
4178c2ecf20Sopenharmony_ci            'struct bpf_sysctl',
4188c2ecf20Sopenharmony_ci            'struct bpf_tcp_sock',
4198c2ecf20Sopenharmony_ci            'struct bpf_tunnel_key',
4208c2ecf20Sopenharmony_ci            'struct bpf_xfrm_state',
4218c2ecf20Sopenharmony_ci            'struct pt_regs',
4228c2ecf20Sopenharmony_ci            'struct sk_reuseport_md',
4238c2ecf20Sopenharmony_ci            'struct sockaddr',
4248c2ecf20Sopenharmony_ci            'struct tcphdr',
4258c2ecf20Sopenharmony_ci            'struct seq_file',
4268c2ecf20Sopenharmony_ci            'struct tcp6_sock',
4278c2ecf20Sopenharmony_ci            'struct tcp_sock',
4288c2ecf20Sopenharmony_ci            'struct tcp_timewait_sock',
4298c2ecf20Sopenharmony_ci            'struct tcp_request_sock',
4308c2ecf20Sopenharmony_ci            'struct udp6_sock',
4318c2ecf20Sopenharmony_ci            'struct task_struct',
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci            'struct __sk_buff',
4348c2ecf20Sopenharmony_ci            'struct sk_msg_md',
4358c2ecf20Sopenharmony_ci            'struct xdp_md',
4368c2ecf20Sopenharmony_ci            'struct path',
4378c2ecf20Sopenharmony_ci            'struct btf_ptr',
4388c2ecf20Sopenharmony_ci    ]
4398c2ecf20Sopenharmony_ci    known_types = {
4408c2ecf20Sopenharmony_ci            '...',
4418c2ecf20Sopenharmony_ci            'void',
4428c2ecf20Sopenharmony_ci            'const void',
4438c2ecf20Sopenharmony_ci            'char',
4448c2ecf20Sopenharmony_ci            'const char',
4458c2ecf20Sopenharmony_ci            'int',
4468c2ecf20Sopenharmony_ci            'long',
4478c2ecf20Sopenharmony_ci            'unsigned long',
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci            '__be16',
4508c2ecf20Sopenharmony_ci            '__be32',
4518c2ecf20Sopenharmony_ci            '__wsum',
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci            'struct bpf_fib_lookup',
4548c2ecf20Sopenharmony_ci            'struct bpf_perf_event_data',
4558c2ecf20Sopenharmony_ci            'struct bpf_perf_event_value',
4568c2ecf20Sopenharmony_ci            'struct bpf_pidns_info',
4578c2ecf20Sopenharmony_ci            'struct bpf_redir_neigh',
4588c2ecf20Sopenharmony_ci            'struct bpf_sk_lookup',
4598c2ecf20Sopenharmony_ci            'struct bpf_sock',
4608c2ecf20Sopenharmony_ci            'struct bpf_sock_addr',
4618c2ecf20Sopenharmony_ci            'struct bpf_sock_ops',
4628c2ecf20Sopenharmony_ci            'struct bpf_sock_tuple',
4638c2ecf20Sopenharmony_ci            'struct bpf_spin_lock',
4648c2ecf20Sopenharmony_ci            'struct bpf_sysctl',
4658c2ecf20Sopenharmony_ci            'struct bpf_tcp_sock',
4668c2ecf20Sopenharmony_ci            'struct bpf_tunnel_key',
4678c2ecf20Sopenharmony_ci            'struct bpf_xfrm_state',
4688c2ecf20Sopenharmony_ci            'struct pt_regs',
4698c2ecf20Sopenharmony_ci            'struct sk_reuseport_md',
4708c2ecf20Sopenharmony_ci            'struct sockaddr',
4718c2ecf20Sopenharmony_ci            'struct tcphdr',
4728c2ecf20Sopenharmony_ci            'struct seq_file',
4738c2ecf20Sopenharmony_ci            'struct tcp6_sock',
4748c2ecf20Sopenharmony_ci            'struct tcp_sock',
4758c2ecf20Sopenharmony_ci            'struct tcp_timewait_sock',
4768c2ecf20Sopenharmony_ci            'struct tcp_request_sock',
4778c2ecf20Sopenharmony_ci            'struct udp6_sock',
4788c2ecf20Sopenharmony_ci            'struct task_struct',
4798c2ecf20Sopenharmony_ci            'struct path',
4808c2ecf20Sopenharmony_ci            'struct btf_ptr',
4818c2ecf20Sopenharmony_ci    }
4828c2ecf20Sopenharmony_ci    mapped_types = {
4838c2ecf20Sopenharmony_ci            'u8': '__u8',
4848c2ecf20Sopenharmony_ci            'u16': '__u16',
4858c2ecf20Sopenharmony_ci            'u32': '__u32',
4868c2ecf20Sopenharmony_ci            'u64': '__u64',
4878c2ecf20Sopenharmony_ci            's8': '__s8',
4888c2ecf20Sopenharmony_ci            's16': '__s16',
4898c2ecf20Sopenharmony_ci            's32': '__s32',
4908c2ecf20Sopenharmony_ci            's64': '__s64',
4918c2ecf20Sopenharmony_ci            'size_t': 'unsigned long',
4928c2ecf20Sopenharmony_ci            'struct bpf_map': 'void',
4938c2ecf20Sopenharmony_ci            'struct sk_buff': 'struct __sk_buff',
4948c2ecf20Sopenharmony_ci            'const struct sk_buff': 'const struct __sk_buff',
4958c2ecf20Sopenharmony_ci            'struct sk_msg_buff': 'struct sk_msg_md',
4968c2ecf20Sopenharmony_ci            'struct xdp_buff': 'struct xdp_md',
4978c2ecf20Sopenharmony_ci    }
4988c2ecf20Sopenharmony_ci    # Helpers overloaded for different context types.
4998c2ecf20Sopenharmony_ci    overloaded_helpers = [
5008c2ecf20Sopenharmony_ci        'bpf_get_socket_cookie',
5018c2ecf20Sopenharmony_ci        'bpf_sk_assign',
5028c2ecf20Sopenharmony_ci    ]
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci    def print_header(self):
5058c2ecf20Sopenharmony_ci        header = '''\
5068c2ecf20Sopenharmony_ci/* This is auto-generated file. See bpf_helpers_doc.py for details. */
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci/* Forward declarations of BPF structs */'''
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci        print(header)
5118c2ecf20Sopenharmony_ci        for fwd in self.type_fwds:
5128c2ecf20Sopenharmony_ci            print('%s;' % fwd)
5138c2ecf20Sopenharmony_ci        print('')
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci    def print_footer(self):
5168c2ecf20Sopenharmony_ci        footer = ''
5178c2ecf20Sopenharmony_ci        print(footer)
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci    def map_type(self, t):
5208c2ecf20Sopenharmony_ci        if t in self.known_types:
5218c2ecf20Sopenharmony_ci            return t
5228c2ecf20Sopenharmony_ci        if t in self.mapped_types:
5238c2ecf20Sopenharmony_ci            return self.mapped_types[t]
5248c2ecf20Sopenharmony_ci        print("Unrecognized type '%s', please add it to known types!" % t,
5258c2ecf20Sopenharmony_ci              file=sys.stderr)
5268c2ecf20Sopenharmony_ci        sys.exit(1)
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci    seen_helpers = set()
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci    def print_one(self, helper):
5318c2ecf20Sopenharmony_ci        proto = helper.proto_break_down()
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci        if proto['name'] in self.seen_helpers:
5348c2ecf20Sopenharmony_ci            return
5358c2ecf20Sopenharmony_ci        self.seen_helpers.add(proto['name'])
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci        print('/*')
5388c2ecf20Sopenharmony_ci        print(" * %s" % proto['name'])
5398c2ecf20Sopenharmony_ci        print(" *")
5408c2ecf20Sopenharmony_ci        if (helper.desc):
5418c2ecf20Sopenharmony_ci            # Do not strip all newline characters: formatted code at the end of
5428c2ecf20Sopenharmony_ci            # a section must be followed by a blank line.
5438c2ecf20Sopenharmony_ci            for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
5448c2ecf20Sopenharmony_ci                print(' *{}{}'.format(' \t' if line else '', line))
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci        if (helper.ret):
5478c2ecf20Sopenharmony_ci            print(' *')
5488c2ecf20Sopenharmony_ci            print(' * Returns')
5498c2ecf20Sopenharmony_ci            for line in helper.ret.rstrip().split('\n'):
5508c2ecf20Sopenharmony_ci                print(' *{}{}'.format(' \t' if line else '', line))
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci        print(' */')
5538c2ecf20Sopenharmony_ci        print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
5548c2ecf20Sopenharmony_ci                                      proto['ret_star'], proto['name']), end='')
5558c2ecf20Sopenharmony_ci        comma = ''
5568c2ecf20Sopenharmony_ci        for i, a in enumerate(proto['args']):
5578c2ecf20Sopenharmony_ci            t = a['type']
5588c2ecf20Sopenharmony_ci            n = a['name']
5598c2ecf20Sopenharmony_ci            if proto['name'] in self.overloaded_helpers and i == 0:
5608c2ecf20Sopenharmony_ci                    t = 'void'
5618c2ecf20Sopenharmony_ci                    n = 'ctx'
5628c2ecf20Sopenharmony_ci            one_arg = '{}{}'.format(comma, self.map_type(t))
5638c2ecf20Sopenharmony_ci            if n:
5648c2ecf20Sopenharmony_ci                if a['star']:
5658c2ecf20Sopenharmony_ci                    one_arg += ' {}'.format(a['star'])
5668c2ecf20Sopenharmony_ci                else:
5678c2ecf20Sopenharmony_ci                    one_arg += ' '
5688c2ecf20Sopenharmony_ci                one_arg += '{}'.format(n)
5698c2ecf20Sopenharmony_ci            comma = ', '
5708c2ecf20Sopenharmony_ci            print(one_arg, end='')
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci        print(') = (void *) %d;' % len(self.seen_helpers))
5738c2ecf20Sopenharmony_ci        print('')
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci###############################################################################
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci# If script is launched from scripts/ from kernel tree and can access
5788c2ecf20Sopenharmony_ci# ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
5798c2ecf20Sopenharmony_ci# otherwise the --filename argument will be required from the command line.
5808c2ecf20Sopenharmony_ciscript = os.path.abspath(sys.argv[0])
5818c2ecf20Sopenharmony_cilinuxRoot = os.path.dirname(os.path.dirname(script))
5828c2ecf20Sopenharmony_cibpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ciargParser = argparse.ArgumentParser(description="""
5858c2ecf20Sopenharmony_ciParse eBPF header file and generate documentation for eBPF helper functions.
5868c2ecf20Sopenharmony_ciThe RST-formatted output produced can be turned into a manual page with the
5878c2ecf20Sopenharmony_cirst2man utility.
5888c2ecf20Sopenharmony_ci""")
5898c2ecf20Sopenharmony_ciargParser.add_argument('--header', action='store_true',
5908c2ecf20Sopenharmony_ci                       help='generate C header file')
5918c2ecf20Sopenharmony_ciif (os.path.isfile(bpfh)):
5928c2ecf20Sopenharmony_ci    argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
5938c2ecf20Sopenharmony_ci                           default=bpfh)
5948c2ecf20Sopenharmony_cielse:
5958c2ecf20Sopenharmony_ci    argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
5968c2ecf20Sopenharmony_ciargs = argParser.parse_args()
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci# Parse file.
5998c2ecf20Sopenharmony_ciheaderParser = HeaderParser(args.filename)
6008c2ecf20Sopenharmony_ciheaderParser.run()
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci# Print formatted output to standard output.
6038c2ecf20Sopenharmony_ciif args.header:
6048c2ecf20Sopenharmony_ci    printer = PrinterHelpers(headerParser.helpers)
6058c2ecf20Sopenharmony_cielse:
6068c2ecf20Sopenharmony_ci    printer = PrinterRST(headerParser.helpers)
6078c2ecf20Sopenharmony_ciprinter.print_all()
608