~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/bpf_doc.py

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/bpf_doc.py (Version linux-6.11.5) and /scripts/bpf_doc.py (Version linux-5.19.17)


  1 #!/usr/bin/env python3                              1 #!/usr/bin/env python3
  2 # SPDX-License-Identifier: GPL-2.0-only             2 # SPDX-License-Identifier: GPL-2.0-only
  3 #                                                   3 #
  4 # Copyright (C) 2018-2019 Netronome Systems, I      4 # Copyright (C) 2018-2019 Netronome Systems, Inc.
  5 # Copyright (C) 2021 Isovalent, Inc.                5 # Copyright (C) 2021 Isovalent, Inc.
  6                                                     6 
  7 # In case user attempts to run with Python 2.       7 # In case user attempts to run with Python 2.
  8 from __future__ import print_function               8 from __future__ import print_function
  9                                                     9 
 10 import argparse                                    10 import argparse
 11 import re                                          11 import re
 12 import sys, os                                     12 import sys, os
 13 import subprocess                              << 
 14                                                << 
 15 helpersDocStart = 'Start of BPF helper functio << 
 16                                                    13 
 17 class NoHelperFound(BaseException):                14 class NoHelperFound(BaseException):
 18     pass                                           15     pass
 19                                                    16 
 20 class NoSyscallCommandFound(BaseException):        17 class NoSyscallCommandFound(BaseException):
 21     pass                                           18     pass
 22                                                    19 
 23 class ParsingError(BaseException):                 20 class ParsingError(BaseException):
 24     def __init__(self, line='<line not provide     21     def __init__(self, line='<line not provided>', reader=None):
 25         if reader:                                 22         if reader:
 26             BaseException.__init__(self,           23             BaseException.__init__(self,
 27                                    'Error at f     24                                    'Error at file offset %d, parsing line: %s' %
 28                                    (reader.tel     25                                    (reader.tell(), line))
 29         else:                                      26         else:
 30             BaseException.__init__(self, 'Erro     27             BaseException.__init__(self, 'Error parsing line: %s' % line)
 31                                                    28 
 32                                                    29 
 33 class APIElement(object):                          30 class APIElement(object):
 34     """                                            31     """
 35     An object representing the description of      32     An object representing the description of an aspect of the eBPF API.
 36     @proto: prototype of the API symbol            33     @proto: prototype of the API symbol
 37     @desc: textual description of the symbol       34     @desc: textual description of the symbol
 38     @ret: (optional) description of any associ     35     @ret: (optional) description of any associated return value
 39     """                                            36     """
 40     def __init__(self, proto='', desc='', ret=     37     def __init__(self, proto='', desc='', ret=''):
 41         self.proto = proto                         38         self.proto = proto
 42         self.desc = desc                           39         self.desc = desc
 43         self.ret = ret                             40         self.ret = ret
 44                                                    41 
 45                                                    42 
 46 class Helper(APIElement):                          43 class Helper(APIElement):
 47     """                                            44     """
 48     An object representing the description of      45     An object representing the description of an eBPF helper function.
 49     @proto: function prototype of the helper f     46     @proto: function prototype of the helper function
 50     @desc: textual description of the helper f     47     @desc: textual description of the helper function
 51     @ret: description of the return value of t     48     @ret: description of the return value of the helper function
 52     """                                            49     """
 53     def __init__(self, *args, **kwargs):       << 
 54         super().__init__(*args, **kwargs)      << 
 55         self.enum_val = None                   << 
 56                                                << 
 57     def proto_break_down(self):                    50     def proto_break_down(self):
 58         """                                        51         """
 59         Break down helper function protocol in     52         Break down helper function protocol into smaller chunks: return type,
 60         name, distincts arguments.                 53         name, distincts arguments.
 61         """                                        54         """
 62         arg_re = re.compile(r'((\w+ )*?(\w+|.. !!  55         arg_re = re.compile('((\w+ )*?(\w+|...))( (\**)(\w+))?$')
 63         res = {}                                   56         res = {}
 64         proto_re = re.compile(r'(.+) (\**)(\w+ !!  57         proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')
 65                                                    58 
 66         capture = proto_re.match(self.proto)       59         capture = proto_re.match(self.proto)
 67         res['ret_type'] = capture.group(1)         60         res['ret_type'] = capture.group(1)
 68         res['ret_star'] = capture.group(2)         61         res['ret_star'] = capture.group(2)
 69         res['name']     = capture.group(3)         62         res['name']     = capture.group(3)
 70         res['args'] = []                           63         res['args'] = []
 71                                                    64 
 72         args    = capture.group(4).split(', ')     65         args    = capture.group(4).split(', ')
 73         for a in args:                             66         for a in args:
 74             capture = arg_re.match(a)              67             capture = arg_re.match(a)
 75             res['args'].append({                   68             res['args'].append({
 76                 'type' : capture.group(1),         69                 'type' : capture.group(1),
 77                 'star' : capture.group(5),         70                 'star' : capture.group(5),
 78                 'name' : capture.group(6)          71                 'name' : capture.group(6)
 79             })                                     72             })
 80                                                    73 
 81         return res                                 74         return res
 82                                                    75 
 83                                                    76 
 84 class HeaderParser(object):                        77 class HeaderParser(object):
 85     """                                            78     """
 86     An object used to parse a file in order to     79     An object used to parse a file in order to extract the documentation of a
 87     list of eBPF helper functions. All the hel     80     list of eBPF helper functions. All the helpers that can be retrieved are
 88     stored as Helper object, in the self.helpe     81     stored as Helper object, in the self.helpers() array.
 89     @filename: name of file to parse, usually      82     @filename: name of file to parse, usually include/uapi/linux/bpf.h in the
 90                kernel tree                         83                kernel tree
 91     """                                            84     """
 92     def __init__(self, filename):                  85     def __init__(self, filename):
 93         self.reader = open(filename, 'r')          86         self.reader = open(filename, 'r')
 94         self.line = ''                             87         self.line = ''
 95         self.helpers = []                          88         self.helpers = []
 96         self.commands = []                         89         self.commands = []
 97         self.desc_unique_helpers = set()           90         self.desc_unique_helpers = set()
 98         self.define_unique_helpers = []            91         self.define_unique_helpers = []
 99         self.helper_enum_vals = {}             << 
100         self.helper_enum_pos = {}              << 
101         self.desc_syscalls = []                    92         self.desc_syscalls = []
102         self.enum_syscalls = []                    93         self.enum_syscalls = []
103                                                    94 
104     def parse_element(self):                       95     def parse_element(self):
105         proto    = self.parse_symbol()             96         proto    = self.parse_symbol()
106         desc     = self.parse_desc(proto)          97         desc     = self.parse_desc(proto)
107         ret      = self.parse_ret(proto)           98         ret      = self.parse_ret(proto)
108         return APIElement(proto=proto, desc=de     99         return APIElement(proto=proto, desc=desc, ret=ret)
109                                                   100 
110     def parse_helper(self):                       101     def parse_helper(self):
111         proto    = self.parse_proto()             102         proto    = self.parse_proto()
112         desc     = self.parse_desc(proto)         103         desc     = self.parse_desc(proto)
113         ret      = self.parse_ret(proto)          104         ret      = self.parse_ret(proto)
114         return Helper(proto=proto, desc=desc,     105         return Helper(proto=proto, desc=desc, ret=ret)
115                                                   106 
116     def parse_symbol(self):                       107     def parse_symbol(self):
117         p = re.compile(r' \* ?(BPF\w+)$')      !! 108         p = re.compile(' \* ?(BPF\w+)$')
118         capture = p.match(self.line)              109         capture = p.match(self.line)
119         if not capture:                           110         if not capture:
120             raise NoSyscallCommandFound           111             raise NoSyscallCommandFound
121         end_re = re.compile(r' \* ?NOTES$')    !! 112         end_re = re.compile(' \* ?NOTES$')
122         end = end_re.match(self.line)             113         end = end_re.match(self.line)
123         if end:                                   114         if end:
124             raise NoSyscallCommandFound           115             raise NoSyscallCommandFound
125         self.line = self.reader.readline()        116         self.line = self.reader.readline()
126         return capture.group(1)                   117         return capture.group(1)
127                                                   118 
128     def parse_proto(self):                        119     def parse_proto(self):
129         # Argument can be of shape:               120         # Argument can be of shape:
130         #   - "void"                              121         #   - "void"
131         #   - "type  name"                        122         #   - "type  name"
132         #   - "type *name"                        123         #   - "type *name"
133         #   - Same as above, with "const" and/    124         #   - Same as above, with "const" and/or "struct" in front of type
134         #   - "..." (undefined number of argum    125         #   - "..." (undefined number of arguments, for bpf_trace_printk())
135         # There is at least one term ("void"),    126         # There is at least one term ("void"), and at most five arguments.
136         p = re.compile(r' \* ?((.+) \**\w+\((( !! 127         p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')
137         capture = p.match(self.line)              128         capture = p.match(self.line)
138         if not capture:                           129         if not capture:
139             raise NoHelperFound                   130             raise NoHelperFound
140         self.line = self.reader.readline()        131         self.line = self.reader.readline()
141         return capture.group(1)                   132         return capture.group(1)
142                                                   133 
143     def parse_desc(self, proto):                  134     def parse_desc(self, proto):
144         p = re.compile(r' \* ?(?:\t| {5,8})Des !! 135         p = re.compile(' \* ?(?:\t| {5,8})Description$')
145         capture = p.match(self.line)              136         capture = p.match(self.line)
146         if not capture:                           137         if not capture:
147             raise Exception("No description se    138             raise Exception("No description section found for " + proto)
148         # Description can be several lines, so    139         # Description can be several lines, some of them possibly empty, and it
149         # stops when another subsection title     140         # stops when another subsection title is met.
150         desc = ''                                 141         desc = ''
151         desc_present = False                      142         desc_present = False
152         while True:                               143         while True:
153             self.line = self.reader.readline()    144             self.line = self.reader.readline()
154             if self.line == ' *\n':               145             if self.line == ' *\n':
155                 desc += '\n'                      146                 desc += '\n'
156             else:                                 147             else:
157                 p = re.compile(r' \* ?(?:\t| { !! 148                 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
158                 capture = p.match(self.line)      149                 capture = p.match(self.line)
159                 if capture:                       150                 if capture:
160                     desc_present = True           151                     desc_present = True
161                     desc += capture.group(1) +    152                     desc += capture.group(1) + '\n'
162                 else:                             153                 else:
163                     break                         154                     break
164                                                   155 
165         if not desc_present:                      156         if not desc_present:
166             raise Exception("No description fo    157             raise Exception("No description found for " + proto)
167         return desc                               158         return desc
168                                                   159 
169     def parse_ret(self, proto):                   160     def parse_ret(self, proto):
170         p = re.compile(r' \* ?(?:\t| {5,8})Ret !! 161         p = re.compile(' \* ?(?:\t| {5,8})Return$')
171         capture = p.match(self.line)              162         capture = p.match(self.line)
172         if not capture:                           163         if not capture:
173             raise Exception("No return section    164             raise Exception("No return section found for " + proto)
174         # Return value description can be seve    165         # Return value description can be several lines, some of them possibly
175         # empty, and it stops when another sub    166         # empty, and it stops when another subsection title is met.
176         ret = ''                                  167         ret = ''
177         ret_present = False                       168         ret_present = False
178         while True:                               169         while True:
179             self.line = self.reader.readline()    170             self.line = self.reader.readline()
180             if self.line == ' *\n':               171             if self.line == ' *\n':
181                 ret += '\n'                       172                 ret += '\n'
182             else:                                 173             else:
183                 p = re.compile(r' \* ?(?:\t| { !! 174                 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
184                 capture = p.match(self.line)      175                 capture = p.match(self.line)
185                 if capture:                       176                 if capture:
186                     ret_present = True            177                     ret_present = True
187                     ret += capture.group(1) +     178                     ret += capture.group(1) + '\n'
188                 else:                             179                 else:
189                     break                         180                     break
190                                                   181 
191         if not ret_present:                       182         if not ret_present:
192             raise Exception("No return found f    183             raise Exception("No return found for " + proto)
193         return ret                                184         return ret
194                                                   185 
195     def seek_to(self, target, help_message, di    186     def seek_to(self, target, help_message, discard_lines = 1):
196         self.reader.seek(0)                       187         self.reader.seek(0)
197         offset = self.reader.read().find(targe    188         offset = self.reader.read().find(target)
198         if offset == -1:                          189         if offset == -1:
199             raise Exception(help_message)         190             raise Exception(help_message)
200         self.reader.seek(offset)                  191         self.reader.seek(offset)
201         self.reader.readline()                    192         self.reader.readline()
202         for _ in range(discard_lines):            193         for _ in range(discard_lines):
203             self.reader.readline()                194             self.reader.readline()
204         self.line = self.reader.readline()        195         self.line = self.reader.readline()
205                                                   196 
206     def parse_desc_syscall(self):                 197     def parse_desc_syscall(self):
207         self.seek_to('* DOC: eBPF Syscall Comm    198         self.seek_to('* DOC: eBPF Syscall Commands',
208                      'Could not find start of     199                      'Could not find start of eBPF syscall descriptions list')
209         while True:                               200         while True:
210             try:                                  201             try:
211                 command = self.parse_element()    202                 command = self.parse_element()
212                 self.commands.append(command)     203                 self.commands.append(command)
213                 self.desc_syscalls.append(comm    204                 self.desc_syscalls.append(command.proto)
214                                                   205 
215             except NoSyscallCommandFound:         206             except NoSyscallCommandFound:
216                 break                             207                 break
217                                                   208 
218     def parse_enum_syscall(self):                 209     def parse_enum_syscall(self):
219         self.seek_to('enum bpf_cmd {',            210         self.seek_to('enum bpf_cmd {',
220                      'Could not find start of     211                      'Could not find start of bpf_cmd enum', 0)
221         # Searches for either one or more BPF\    212         # Searches for either one or more BPF\w+ enums
222         bpf_p = re.compile(r'\s*(BPF\w+)+')    !! 213         bpf_p = re.compile('\s*(BPF\w+)+')
223         # Searches for an enum entry assigned     214         # Searches for an enum entry assigned to another entry,
224         # for e.g. BPF_PROG_RUN = BPF_PROG_TES    215         # for e.g. BPF_PROG_RUN = BPF_PROG_TEST_RUN, which is
225         # not documented hence should be skipp    216         # not documented hence should be skipped in check to
226         # determine if the right number of sys    217         # determine if the right number of syscalls are documented
227         assign_p = re.compile(r'\s*(BPF\w+)\s* !! 218         assign_p = re.compile('\s*(BPF\w+)\s*=\s*(BPF\w+)')
228         bpf_cmd_str = ''                          219         bpf_cmd_str = ''
229         while True:                               220         while True:
230             capture = assign_p.match(self.line    221             capture = assign_p.match(self.line)
231             if capture:                           222             if capture:
232                 # Skip line if an enum entry i    223                 # Skip line if an enum entry is assigned to another entry
233                 self.line = self.reader.readli    224                 self.line = self.reader.readline()
234                 continue                          225                 continue
235             capture = bpf_p.match(self.line)      226             capture = bpf_p.match(self.line)
236             if capture:                           227             if capture:
237                 bpf_cmd_str += self.line          228                 bpf_cmd_str += self.line
238             else:                                 229             else:
239                 break                             230                 break
240             self.line = self.reader.readline()    231             self.line = self.reader.readline()
241         # Find the number of occurences of BPF    232         # Find the number of occurences of BPF\w+
242         self.enum_syscalls = re.findall(r'(BPF !! 233         self.enum_syscalls = re.findall('(BPF\w+)+', bpf_cmd_str)
243                                                   234 
244     def parse_desc_helpers(self):                 235     def parse_desc_helpers(self):
245         self.seek_to(helpersDocStart,          !! 236         self.seek_to('* Start of BPF helper function descriptions:',
246                      'Could not find start of     237                      'Could not find start of eBPF helper descriptions list')
247         while True:                               238         while True:
248             try:                                  239             try:
249                 helper = self.parse_helper()      240                 helper = self.parse_helper()
250                 self.helpers.append(helper)       241                 self.helpers.append(helper)
251                 proto = helper.proto_break_dow    242                 proto = helper.proto_break_down()
252                 self.desc_unique_helpers.add(p    243                 self.desc_unique_helpers.add(proto['name'])
253             except NoHelperFound:                 244             except NoHelperFound:
254                 break                             245                 break
255                                                   246 
256     def parse_define_helpers(self):               247     def parse_define_helpers(self):
257         # Parse FN(...) in #define ___BPF_FUNC !! 248         # Parse the number of FN(...) in #define __BPF_FUNC_MAPPER to compare
258         # number of unique function names pres !! 249         # later with the number of unique function names present in description.
259         # correct enumeration value.           << 
260         # Note: seek_to(..) discards the first    250         # Note: seek_to(..) discards the first line below the target search text,
261         # resulting in FN(unspec, 0, ##ctx) be !! 251         # resulting in FN(unspec) being skipped and not added to self.define_unique_helpers.
262         # self.define_unique_helpers.          !! 252         self.seek_to('#define __BPF_FUNC_MAPPER(FN)',
263         self.seek_to('#define ___BPF_FUNC_MAPP << 
264                      'Could not find start of     253                      'Could not find start of eBPF helper definition list')
265         # Searches for one FN(\w+) define or a !! 254         # Searches for either one or more FN(\w+) defines or a backslash for newline
266         p = re.compile(r'\s*FN\((\w+), (\d+),  !! 255         p = re.compile('\s*(FN\(\w+\))+|\\\\')
267         fn_defines_str = ''                       256         fn_defines_str = ''
268         i = 0                                  << 
269         while True:                               257         while True:
270             capture = p.match(self.line)          258             capture = p.match(self.line)
271             if capture:                           259             if capture:
272                 fn_defines_str += self.line       260                 fn_defines_str += self.line
273                 helper_name = capture.expand(r << 
274                 self.helper_enum_vals[helper_n << 
275                 self.helper_enum_pos[helper_na << 
276                 i += 1                         << 
277             else:                                 261             else:
278                 break                             262                 break
279             self.line = self.reader.readline()    263             self.line = self.reader.readline()
280         # Find the number of occurences of FN(    264         # Find the number of occurences of FN(\w+)
281         self.define_unique_helpers = re.findal !! 265         self.define_unique_helpers = re.findall('FN\(\w+\)', fn_defines_str)
282                                                << 
283     def validate_helpers(self):                << 
284         last_helper = ''                       << 
285         seen_helpers = set()                   << 
286         seen_enum_vals = set()                 << 
287         i = 0                                  << 
288         for helper in self.helpers:            << 
289             proto = helper.proto_break_down()  << 
290             name = proto['name']               << 
291             try:                               << 
292                 enum_val = self.helper_enum_va << 
293                 enum_pos = self.helper_enum_po << 
294             except KeyError:                   << 
295                 raise Exception("Helper %s is  << 
296                                                << 
297             if name in seen_helpers:           << 
298                 if last_helper != name:        << 
299                     raise Exception("Helper %s << 
300                 continue                       << 
301                                                << 
302             # Enforce current practice of havi << 
303             # by enum value.                   << 
304             if enum_pos != i:                  << 
305                 raise Exception("Helper %s (ID << 
306             if enum_val in seen_enum_vals:     << 
307                 raise Exception("Helper %s has << 
308                                                << 
309             seen_helpers.add(name)             << 
310             last_helper = name                 << 
311             seen_enum_vals.add(enum_val)       << 
312                                                << 
313             helper.enum_val = enum_val         << 
314             i += 1                             << 
315                                                   266 
316     def run(self):                                267     def run(self):
317         self.parse_desc_syscall()                 268         self.parse_desc_syscall()
318         self.parse_enum_syscall()                 269         self.parse_enum_syscall()
319         self.parse_desc_helpers()                 270         self.parse_desc_helpers()
320         self.parse_define_helpers()               271         self.parse_define_helpers()
321         self.validate_helpers()                << 
322         self.reader.close()                       272         self.reader.close()
323                                                   273 
324 ##############################################    274 ###############################################################################
325                                                   275 
326 class Printer(object):                            276 class Printer(object):
327     """                                           277     """
328     A generic class for printers. Printers sho    278     A generic class for printers. Printers should be created with an array of
329     Helper objects, and implement a way to pri    279     Helper objects, and implement a way to print them in the desired fashion.
330     @parser: A HeaderParser with objects to pr    280     @parser: A HeaderParser with objects to print to standard output
331     """                                           281     """
332     def __init__(self, parser):                   282     def __init__(self, parser):
333         self.parser = parser                      283         self.parser = parser
334         self.elements = []                        284         self.elements = []
335                                                   285 
336     def print_header(self):                       286     def print_header(self):
337         pass                                      287         pass
338                                                   288 
339     def print_footer(self):                       289     def print_footer(self):
340         pass                                      290         pass
341                                                   291 
342     def print_one(self, helper):                  292     def print_one(self, helper):
343         pass                                      293         pass
344                                                   294 
345     def print_all(self):                          295     def print_all(self):
346         self.print_header()                       296         self.print_header()
347         for elem in self.elements:                297         for elem in self.elements:
348             self.print_one(elem)                  298             self.print_one(elem)
349         self.print_footer()                       299         self.print_footer()
350                                                   300 
351     def elem_number_check(self, desc_unique_el    301     def elem_number_check(self, desc_unique_elem, define_unique_elem, type, instance):
352         """                                       302         """
353         Checks the number of helpers/syscalls     303         Checks the number of helpers/syscalls documented within the header file
354         description with those defined as part    304         description with those defined as part of enum/macro and raise an
355         Exception if they don't match.            305         Exception if they don't match.
356         """                                       306         """
357         nr_desc_unique_elem = len(desc_unique_    307         nr_desc_unique_elem = len(desc_unique_elem)
358         nr_define_unique_elem = len(define_uni    308         nr_define_unique_elem = len(define_unique_elem)
359         if nr_desc_unique_elem != nr_define_un    309         if nr_desc_unique_elem != nr_define_unique_elem:
360             exception_msg = '''                   310             exception_msg = '''
361 The number of unique %s in description (%d) do    311 The number of unique %s in description (%d) doesn\'t match the number of unique %s defined in %s (%d)
362 ''' % (type, nr_desc_unique_elem, type, instan    312 ''' % (type, nr_desc_unique_elem, type, instance, nr_define_unique_elem)
363             if nr_desc_unique_elem < nr_define    313             if nr_desc_unique_elem < nr_define_unique_elem:
364                 # Function description is pars    314                 # Function description is parsed until no helper is found (which can be due to
365                 # misformatting). Hence, only     315                 # misformatting). Hence, only print the first missing/misformatted helper/enum.
366                 exception_msg += '''              316                 exception_msg += '''
367 The description for %s is not present or forma    317 The description for %s is not present or formatted correctly.
368 ''' % (define_unique_elem[nr_desc_unique_elem]    318 ''' % (define_unique_elem[nr_desc_unique_elem])
369             raise Exception(exception_msg)        319             raise Exception(exception_msg)
370                                                   320 
371 class PrinterRST(Printer):                        321 class PrinterRST(Printer):
372     """                                           322     """
373     A generic class for printers that print Re    323     A generic class for printers that print ReStructured Text. Printers should
374     be created with a HeaderParser object, and    324     be created with a HeaderParser object, and implement a way to print API
375     elements in the desired fashion.              325     elements in the desired fashion.
376     @parser: A HeaderParser with objects to pr    326     @parser: A HeaderParser with objects to print to standard output
377     """                                           327     """
378     def __init__(self, parser):                   328     def __init__(self, parser):
379         self.parser = parser                      329         self.parser = parser
380                                                   330 
381     def print_license(self):                      331     def print_license(self):
382         license = '''\                            332         license = '''\
383 .. Copyright (C) All BPF authors and contribut    333 .. Copyright (C) All BPF authors and contributors from 2014 to present.
384 .. See git log include/uapi/linux/bpf.h in ker    334 .. See git log include/uapi/linux/bpf.h in kernel tree for details.
385 ..                                                335 .. 
386 .. SPDX-License-Identifier: Linux-man-pages-co !! 336 .. %%%LICENSE_START(VERBATIM)
                                                   >> 337 .. Permission is granted to make and distribute verbatim copies of this
                                                   >> 338 .. manual provided the copyright notice and this permission notice are
                                                   >> 339 .. preserved on all copies.
                                                   >> 340 .. 
                                                   >> 341 .. Permission is granted to copy and distribute modified versions of this
                                                   >> 342 .. manual under the conditions for verbatim copying, provided that the
                                                   >> 343 .. entire resulting derived work is distributed under the terms of a
                                                   >> 344 .. permission notice identical to this one.
                                                   >> 345 .. 
                                                   >> 346 .. Since the Linux kernel and libraries are constantly changing, this
                                                   >> 347 .. manual page may be incorrect or out-of-date.  The author(s) assume no
                                                   >> 348 .. responsibility for errors or omissions, or for damages resulting from
                                                   >> 349 .. the use of the information contained herein.  The author(s) may not
                                                   >> 350 .. have taken the same level of care in the production of this manual,
                                                   >> 351 .. which is licensed free of charge, as they might when working
                                                   >> 352 .. professionally.
                                                   >> 353 .. 
                                                   >> 354 .. Formatted or processed versions of this manual, if unaccompanied by
                                                   >> 355 .. the source, must acknowledge the copyright and authors of this work.
                                                   >> 356 .. %%%LICENSE_END
387 ..                                                357 .. 
388 .. Please do not edit this file. It was genera    358 .. Please do not edit this file. It was generated from the documentation
389 .. located in file include/uapi/linux/bpf.h of    359 .. located in file include/uapi/linux/bpf.h of the Linux kernel sources
390 .. (helpers description), and from scripts/bpf    360 .. (helpers description), and from scripts/bpf_doc.py in the same
391 .. repository (header and footer).                361 .. repository (header and footer).
392 '''                                               362 '''
393         print(license)                            363         print(license)
394                                                   364 
395     def print_elem(self, elem):                   365     def print_elem(self, elem):
396         if (elem.desc):                           366         if (elem.desc):
397             print('\tDescription')                367             print('\tDescription')
398             # Do not strip all newline charact    368             # Do not strip all newline characters: formatted code at the end of
399             # a section must be followed by a     369             # a section must be followed by a blank line.
400             for line in re.sub('\n$', '', elem    370             for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
401                 print('{}{}'.format('\t\t' if     371                 print('{}{}'.format('\t\t' if line else '', line))
402                                                   372 
403         if (elem.ret):                            373         if (elem.ret):
404             print('\tReturn')                     374             print('\tReturn')
405             for line in elem.ret.rstrip().spli    375             for line in elem.ret.rstrip().split('\n'):
406                 print('{}{}'.format('\t\t' if     376                 print('{}{}'.format('\t\t' if line else '', line))
407                                                   377 
408         print('')                                 378         print('')
409                                                   379 
410     def get_kernel_version(self):              << 
411         try:                                   << 
412             version = subprocess.run(['git', ' << 
413                                      capture_o << 
414             version = version.stdout.decode(). << 
415         except:                                << 
416             try:                               << 
417                 version = subprocess.run(['mak << 
418                                          cwd=l << 
419                 version = version.stdout.decod << 
420             except:                            << 
421                 return 'Linux'                 << 
422         return 'Linux {version}'.format(versio << 
423                                                << 
424     def get_last_doc_update(self, delimiter):  << 
425         try:                                   << 
426             cmd = ['git', 'log', '-1', '--pret << 
427                    '-L',                       << 
428                    '/{}/,/\\*\\//:include/uapi << 
429             date = subprocess.run(cmd, cwd=lin << 
430                                   capture_outp << 
431             return date.stdout.decode().rstrip << 
432         except:                                << 
433             return ''                          << 
434                                                << 
435 class PrinterHelpersRST(PrinterRST):              380 class PrinterHelpersRST(PrinterRST):
436     """                                           381     """
437     A printer for dumping collected informatio    382     A printer for dumping collected information about helpers as a ReStructured
438     Text page compatible with the rst2man prog    383     Text page compatible with the rst2man program, which can be used to
439     generate a manual page for the helpers.       384     generate a manual page for the helpers.
440     @parser: A HeaderParser with Helper object    385     @parser: A HeaderParser with Helper objects to print to standard output
441     """                                           386     """
442     def __init__(self, parser):                   387     def __init__(self, parser):
443         self.elements = parser.helpers            388         self.elements = parser.helpers
444         self.elem_number_check(parser.desc_uni !! 389         self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
445                                                   390 
446     def print_header(self):                       391     def print_header(self):
447         header = '''\                             392         header = '''\
448 ===========                                       393 ===========
449 BPF-HELPERS                                       394 BPF-HELPERS
450 ===========                                       395 ===========
451 ----------------------------------------------    396 -------------------------------------------------------------------------------
452 list of eBPF helper functions                     397 list of eBPF helper functions
453 ----------------------------------------------    398 -------------------------------------------------------------------------------
454                                                   399 
455 :Manual section: 7                                400 :Manual section: 7
456 :Version: {version}                            << 
457 {date_field}{date}                             << 
458                                                   401 
459 DESCRIPTION                                       402 DESCRIPTION
460 ===========                                       403 ===========
461                                                   404 
462 The extended Berkeley Packet Filter (eBPF) sub    405 The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
463 written in a pseudo-assembly language, then at    406 written in a pseudo-assembly language, then attached to one of the several
464 kernel hooks and run in reaction of specific e    407 kernel hooks and run in reaction of specific events. This framework differs
465 from the older, "classic" BPF (or "cBPF") in s    408 from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
466 the ability to call special functions (or "hel    409 the ability to call special functions (or "helpers") from within a program.
467 These functions are restricted to a white-list    410 These functions are restricted to a white-list of helpers defined in the
468 kernel.                                           411 kernel.
469                                                   412 
470 These helpers are used by eBPF programs to int    413 These helpers are used by eBPF programs to interact with the system, or with
471 the context in which they work. For instance,     414 the context in which they work. For instance, they can be used to print
472 debugging messages, to get the time since the     415 debugging messages, to get the time since the system was booted, to interact
473 with eBPF maps, or to manipulate network packe    416 with eBPF maps, or to manipulate network packets. Since there are several eBPF
474 program types, and that they do not run in the    417 program types, and that they do not run in the same context, each program type
475 can only call a subset of those helpers.          418 can only call a subset of those helpers.
476                                                   419 
477 Due to eBPF conventions, a helper can not have    420 Due to eBPF conventions, a helper can not have more than five arguments.
478                                                   421 
479 Internally, eBPF programs call directly into t    422 Internally, eBPF programs call directly into the compiled helper functions
480 without requiring any foreign-function interfa    423 without requiring any foreign-function interface. As a result, calling helpers
481 introduces no overhead, thus offering excellen    424 introduces no overhead, thus offering excellent performance.
482                                                   425 
483 This document is an attempt to list and docume    426 This document is an attempt to list and document the helpers available to eBPF
484 developers. They are sorted by chronological o    427 developers. They are sorted by chronological order (the oldest helpers in the
485 kernel at the top).                               428 kernel at the top).
486                                                   429 
487 HELPERS                                           430 HELPERS
488 =======                                           431 =======
489 '''                                               432 '''
490         kernelVersion = self.get_kernel_versio << 
491         lastUpdate = self.get_last_doc_update( << 
492                                                << 
493         PrinterRST.print_license(self)            433         PrinterRST.print_license(self)
494         print(header.format(version=kernelVers !! 434         print(header)
495                             date_field = ':Dat << 
496                             date=lastUpdate))  << 
497                                                   435 
498     def print_footer(self):                       436     def print_footer(self):
499         footer = '''                              437         footer = '''
500 EXAMPLES                                          438 EXAMPLES
501 ========                                          439 ========
502                                                   440 
503 Example usage for most of the eBPF helpers lis    441 Example usage for most of the eBPF helpers listed in this manual page are
504 available within the Linux kernel sources, at     442 available within the Linux kernel sources, at the following locations:
505                                                   443 
506 * *samples/bpf/*                                  444 * *samples/bpf/*
507 * *tools/testing/selftests/bpf/*                  445 * *tools/testing/selftests/bpf/*
508                                                   446 
509 LICENSE                                           447 LICENSE
510 =======                                           448 =======
511                                                   449 
512 eBPF programs can have an associated license,     450 eBPF programs can have an associated license, passed along with the bytecode
513 instructions to the kernel when the programs a    451 instructions to the kernel when the programs are loaded. The format for that
514 string is identical to the one in use for kern    452 string is identical to the one in use for kernel modules (Dual licenses, such
515 as "Dual BSD/GPL", may be used). Some helper f    453 as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
516 programs that are compatible with the GNU Gene !! 454 programs that are compatible with the GNU Privacy License (GPL).
517                                                   455 
518 In order to use such helpers, the eBPF program    456 In order to use such helpers, the eBPF program must be loaded with the correct
519 license string passed (via **attr**) to the ** !! 457 license string passed (via **attr**) to the **bpf**\ () system call, and this
520 generally translates into the C source code of    458 generally translates into the C source code of the program containing a line
521 similar to the following:                         459 similar to the following:
522                                                   460 
523 ::                                                461 ::
524                                                   462 
525         char ____license[] __attribute__((sect    463         char ____license[] __attribute__((section("license"), used)) = "GPL";
526                                                   464 
527 IMPLEMENTATION                                    465 IMPLEMENTATION
528 ==============                                    466 ==============
529                                                   467 
530 This manual page is an effort to document the     468 This manual page is an effort to document the existing eBPF helper functions.
531 But as of this writing, the BPF sub-system is     469 But as of this writing, the BPF sub-system is under heavy development. New eBPF
532 program or map types are added, along with new    470 program or map types are added, along with new helper functions. Some helpers
533 are occasionally made available for additional    471 are occasionally made available for additional program types. So in spite of
534 the efforts of the community, this page might     472 the efforts of the community, this page might not be up-to-date. If you want to
535 check by yourself what helper functions exist     473 check by yourself what helper functions exist in your kernel, or what types of
536 programs they can support, here are some files    474 programs they can support, here are some files among the kernel tree that you
537 may be interested in:                             475 may be interested in:
538                                                   476 
539 * *include/uapi/linux/bpf.h* is the main BPF h    477 * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
540   of all helper functions, as well as many oth    478   of all helper functions, as well as many other BPF definitions including most
541   of the flags, structs or constants used by t    479   of the flags, structs or constants used by the helpers.
542 * *net/core/filter.c* contains the definition     480 * *net/core/filter.c* contains the definition of most network-related helper
543   functions, and the list of program types fro    481   functions, and the list of program types from which they can be used.
544 * *kernel/trace/bpf_trace.c* is the equivalent    482 * *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
545   helpers.                                        483   helpers.
546 * *kernel/bpf/verifier.c* contains the functio    484 * *kernel/bpf/verifier.c* contains the functions used to check that valid types
547   of eBPF maps are used with a given helper fu    485   of eBPF maps are used with a given helper function.
548 * *kernel/bpf/* directory contains other files    486 * *kernel/bpf/* directory contains other files in which additional helpers are
549   defined (for cgroups, sockmaps, etc.).          487   defined (for cgroups, sockmaps, etc.).
550 * The bpftool utility can be used to probe the    488 * The bpftool utility can be used to probe the availability of helper functions
551   on the system (as well as supported program     489   on the system (as well as supported program and map types, and a number of
552   other parameters). To do so, run **bpftool f    490   other parameters). To do so, run **bpftool feature probe** (see
553   **bpftool-feature**\\ (8) for details). Add  !! 491   **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to
554   list features available to unprivileged user    492   list features available to unprivileged users.
555                                                   493 
556 Compatibility between helper functions and pro    494 Compatibility between helper functions and program types can generally be found
557 in the files where helper functions are define    495 in the files where helper functions are defined. Look for the **struct
558 bpf_func_proto** objects and for functions ret    496 bpf_func_proto** objects and for functions returning them: these functions
559 contain a list of helpers that a given program    497 contain a list of helpers that a given program type can call. Note that the
560 **default:** label of the **switch ... case**     498 **default:** label of the **switch ... case** used to filter helpers can call
561 other functions, themselves allowing access to    499 other functions, themselves allowing access to additional helpers. The
562 requirement for GPL license is also in those *    500 requirement for GPL license is also in those **struct bpf_func_proto**.
563                                                   501 
564 Compatibility between helper functions and map    502 Compatibility between helper functions and map types can be found in the
565 **check_map_func_compatibility**\\ () function !! 503 **check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*.
566                                                   504 
567 Helper functions that invalidate the checks on    505 Helper functions that invalidate the checks on **data** and **data_end**
568 pointers for network processing are listed in     506 pointers for network processing are listed in function
569 **bpf_helper_changes_pkt_data**\\ () in file * !! 507 **bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*.
570                                                   508 
571 SEE ALSO                                          509 SEE ALSO
572 ========                                          510 ========
573                                                   511 
574 **bpf**\\ (2),                                 !! 512 **bpf**\ (2),
575 **bpftool**\\ (8),                             !! 513 **bpftool**\ (8),
576 **cgroups**\\ (7),                             !! 514 **cgroups**\ (7),
577 **ip**\\ (8),                                  !! 515 **ip**\ (8),
578 **perf_event_open**\\ (2),                     !! 516 **perf_event_open**\ (2),
579 **sendmsg**\\ (2),                             !! 517 **sendmsg**\ (2),
580 **socket**\\ (7),                              !! 518 **socket**\ (7),
581 **tc-bpf**\\ (8)'''                            !! 519 **tc-bpf**\ (8)'''
582         print(footer)                             520         print(footer)
583                                                   521 
584     def print_proto(self, helper):                522     def print_proto(self, helper):
585         """                                       523         """
586         Format function protocol with bold and    524         Format function protocol with bold and italics markers. This makes RST
587         file less readable, but gives nice res    525         file less readable, but gives nice results in the manual page.
588         """                                       526         """
589         proto = helper.proto_break_down()         527         proto = helper.proto_break_down()
590                                                   528 
591         print('**%s %s%s(' % (proto['ret_type'    529         print('**%s %s%s(' % (proto['ret_type'],
592                               proto['ret_star'    530                               proto['ret_star'].replace('*', '\\*'),
593                               proto['name']),     531                               proto['name']),
594               end='')                             532               end='')
595                                                   533 
596         comma = ''                                534         comma = ''
597         for a in proto['args']:                   535         for a in proto['args']:
598             one_arg = '{}{}'.format(comma, a['    536             one_arg = '{}{}'.format(comma, a['type'])
599             if a['name']:                         537             if a['name']:
600                 if a['star']:                     538                 if a['star']:
601                     one_arg += ' {}**\\ '.form !! 539                     one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*'))
602                 else:                             540                 else:
603                     one_arg += '** '              541                     one_arg += '** '
604                 one_arg += '*{}*\\ **'.format(    542                 one_arg += '*{}*\\ **'.format(a['name'])
605             comma = ', '                          543             comma = ', '
606             print(one_arg, end='')                544             print(one_arg, end='')
607                                                   545 
608         print(')**')                              546         print(')**')
609                                                   547 
610     def print_one(self, helper):                  548     def print_one(self, helper):
611         self.print_proto(helper)                  549         self.print_proto(helper)
612         self.print_elem(helper)                   550         self.print_elem(helper)
613                                                   551 
614                                                   552 
615 class PrinterSyscallRST(PrinterRST):              553 class PrinterSyscallRST(PrinterRST):
616     """                                           554     """
617     A printer for dumping collected informatio    555     A printer for dumping collected information about the syscall API as a
618     ReStructured Text page compatible with the    556     ReStructured Text page compatible with the rst2man program, which can be
619     used to generate a manual page for the sys    557     used to generate a manual page for the syscall.
620     @parser: A HeaderParser with APIElement ob    558     @parser: A HeaderParser with APIElement objects to print to standard
621              output                               559              output
622     """                                           560     """
623     def __init__(self, parser):                   561     def __init__(self, parser):
624         self.elements = parser.commands           562         self.elements = parser.commands
625         self.elem_number_check(parser.desc_sys    563         self.elem_number_check(parser.desc_syscalls, parser.enum_syscalls, 'syscall', 'bpf_cmd')
626                                                   564 
627     def print_header(self):                       565     def print_header(self):
628         header = '''\                             566         header = '''\
629 ===                                               567 ===
630 bpf                                               568 bpf
631 ===                                               569 ===
632 ----------------------------------------------    570 -------------------------------------------------------------------------------
633 Perform a command on an extended BPF object       571 Perform a command on an extended BPF object
634 ----------------------------------------------    572 -------------------------------------------------------------------------------
635                                                   573 
636 :Manual section: 2                                574 :Manual section: 2
637                                                   575 
638 COMMANDS                                          576 COMMANDS
639 ========                                          577 ========
640 '''                                               578 '''
641         PrinterRST.print_license(self)            579         PrinterRST.print_license(self)
642         print(header)                             580         print(header)
643                                                   581 
644     def print_one(self, command):                 582     def print_one(self, command):
645         print('**%s**' % (command.proto))         583         print('**%s**' % (command.proto))
646         self.print_elem(command)                  584         self.print_elem(command)
647                                                   585 
648                                                   586 
649 class PrinterHelpers(Printer):                    587 class PrinterHelpers(Printer):
650     """                                           588     """
651     A printer for dumping collected informatio    589     A printer for dumping collected information about helpers as C header to
652     be included from BPF program.                 590     be included from BPF program.
653     @parser: A HeaderParser with Helper object    591     @parser: A HeaderParser with Helper objects to print to standard output
654     """                                           592     """
655     def __init__(self, parser):                   593     def __init__(self, parser):
656         self.elements = parser.helpers            594         self.elements = parser.helpers
657         self.elem_number_check(parser.desc_uni !! 595         self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
658                                                   596 
659     type_fwds = [                                 597     type_fwds = [
660             'struct bpf_fib_lookup',              598             'struct bpf_fib_lookup',
661             'struct bpf_sk_lookup',               599             'struct bpf_sk_lookup',
662             'struct bpf_perf_event_data',         600             'struct bpf_perf_event_data',
663             'struct bpf_perf_event_value',        601             'struct bpf_perf_event_value',
664             'struct bpf_pidns_info',              602             'struct bpf_pidns_info',
665             'struct bpf_redir_neigh',             603             'struct bpf_redir_neigh',
666             'struct bpf_sock',                    604             'struct bpf_sock',
667             'struct bpf_sock_addr',               605             'struct bpf_sock_addr',
668             'struct bpf_sock_ops',                606             'struct bpf_sock_ops',
669             'struct bpf_sock_tuple',              607             'struct bpf_sock_tuple',
670             'struct bpf_spin_lock',               608             'struct bpf_spin_lock',
671             'struct bpf_sysctl',                  609             'struct bpf_sysctl',
672             'struct bpf_tcp_sock',                610             'struct bpf_tcp_sock',
673             'struct bpf_tunnel_key',              611             'struct bpf_tunnel_key',
674             'struct bpf_xfrm_state',              612             'struct bpf_xfrm_state',
675             'struct linux_binprm',                613             'struct linux_binprm',
676             'struct pt_regs',                     614             'struct pt_regs',
677             'struct sk_reuseport_md',             615             'struct sk_reuseport_md',
678             'struct sockaddr',                    616             'struct sockaddr',
679             'struct tcphdr',                      617             'struct tcphdr',
680             'struct seq_file',                    618             'struct seq_file',
681             'struct tcp6_sock',                   619             'struct tcp6_sock',
682             'struct tcp_sock',                    620             'struct tcp_sock',
683             'struct tcp_timewait_sock',           621             'struct tcp_timewait_sock',
684             'struct tcp_request_sock',            622             'struct tcp_request_sock',
685             'struct udp6_sock',                   623             'struct udp6_sock',
686             'struct unix_sock',                   624             'struct unix_sock',
687             'struct task_struct',                 625             'struct task_struct',
688             'struct cgroup',                   << 
689                                                   626 
690             'struct __sk_buff',                   627             'struct __sk_buff',
691             'struct sk_msg_md',                   628             'struct sk_msg_md',
692             'struct xdp_md',                      629             'struct xdp_md',
693             'struct path',                        630             'struct path',
694             'struct btf_ptr',                     631             'struct btf_ptr',
695             'struct inode',                       632             'struct inode',
696             'struct socket',                      633             'struct socket',
697             'struct file',                        634             'struct file',
698             'struct bpf_timer',                   635             'struct bpf_timer',
699             'struct mptcp_sock',                  636             'struct mptcp_sock',
700             'struct bpf_dynptr',                  637             'struct bpf_dynptr',
701             'struct iphdr',                    << 
702             'struct ipv6hdr',                  << 
703     ]                                             638     ]
704     known_types = {                               639     known_types = {
705             '...',                                640             '...',
706             'void',                               641             'void',
707             'const void',                         642             'const void',
708             'char',                               643             'char',
709             'const char',                         644             'const char',
710             'int',                                645             'int',
711             'long',                               646             'long',
712             'unsigned long',                      647             'unsigned long',
713                                                   648 
714             '__be16',                             649             '__be16',
715             '__be32',                             650             '__be32',
716             '__wsum',                             651             '__wsum',
717                                                   652 
718             'struct bpf_fib_lookup',              653             'struct bpf_fib_lookup',
719             'struct bpf_perf_event_data',         654             'struct bpf_perf_event_data',
720             'struct bpf_perf_event_value',        655             'struct bpf_perf_event_value',
721             'struct bpf_pidns_info',              656             'struct bpf_pidns_info',
722             'struct bpf_redir_neigh',             657             'struct bpf_redir_neigh',
723             'struct bpf_sk_lookup',               658             'struct bpf_sk_lookup',
724             'struct bpf_sock',                    659             'struct bpf_sock',
725             'struct bpf_sock_addr',               660             'struct bpf_sock_addr',
726             'struct bpf_sock_ops',                661             'struct bpf_sock_ops',
727             'struct bpf_sock_tuple',              662             'struct bpf_sock_tuple',
728             'struct bpf_spin_lock',               663             'struct bpf_spin_lock',
729             'struct bpf_sysctl',                  664             'struct bpf_sysctl',
730             'struct bpf_tcp_sock',                665             'struct bpf_tcp_sock',
731             'struct bpf_tunnel_key',              666             'struct bpf_tunnel_key',
732             'struct bpf_xfrm_state',              667             'struct bpf_xfrm_state',
733             'struct linux_binprm',                668             'struct linux_binprm',
734             'struct pt_regs',                     669             'struct pt_regs',
735             'struct sk_reuseport_md',             670             'struct sk_reuseport_md',
736             'struct sockaddr',                    671             'struct sockaddr',
737             'struct tcphdr',                      672             'struct tcphdr',
738             'struct seq_file',                    673             'struct seq_file',
739             'struct tcp6_sock',                   674             'struct tcp6_sock',
740             'struct tcp_sock',                    675             'struct tcp_sock',
741             'struct tcp_timewait_sock',           676             'struct tcp_timewait_sock',
742             'struct tcp_request_sock',            677             'struct tcp_request_sock',
743             'struct udp6_sock',                   678             'struct udp6_sock',
744             'struct unix_sock',                   679             'struct unix_sock',
745             'struct task_struct',                 680             'struct task_struct',
746             'struct cgroup',                   << 
747             'struct path',                        681             'struct path',
748             'struct btf_ptr',                     682             'struct btf_ptr',
749             'struct inode',                       683             'struct inode',
750             'struct socket',                      684             'struct socket',
751             'struct file',                        685             'struct file',
752             'struct bpf_timer',                   686             'struct bpf_timer',
753             'struct mptcp_sock',                  687             'struct mptcp_sock',
754             'struct bpf_dynptr',                  688             'struct bpf_dynptr',
755             'const struct bpf_dynptr',         << 
756             'struct iphdr',                    << 
757             'struct ipv6hdr',                  << 
758     }                                             689     }
759     mapped_types = {                              690     mapped_types = {
760             'u8': '__u8',                         691             'u8': '__u8',
761             'u16': '__u16',                       692             'u16': '__u16',
762             'u32': '__u32',                       693             'u32': '__u32',
763             'u64': '__u64',                       694             'u64': '__u64',
764             's8': '__s8',                         695             's8': '__s8',
765             's16': '__s16',                       696             's16': '__s16',
766             's32': '__s32',                       697             's32': '__s32',
767             's64': '__s64',                       698             's64': '__s64',
768             'size_t': 'unsigned long',            699             'size_t': 'unsigned long',
769             'struct bpf_map': 'void',             700             'struct bpf_map': 'void',
770             'struct sk_buff': 'struct __sk_buf    701             'struct sk_buff': 'struct __sk_buff',
771             'const struct sk_buff': 'const str    702             'const struct sk_buff': 'const struct __sk_buff',
772             'struct sk_msg_buff': 'struct sk_m    703             'struct sk_msg_buff': 'struct sk_msg_md',
773             'struct xdp_buff': 'struct xdp_md'    704             'struct xdp_buff': 'struct xdp_md',
774     }                                             705     }
775     # Helpers overloaded for different context    706     # Helpers overloaded for different context types.
776     overloaded_helpers = [                        707     overloaded_helpers = [
777         'bpf_get_socket_cookie',                  708         'bpf_get_socket_cookie',
778         'bpf_sk_assign',                          709         'bpf_sk_assign',
779     ]                                             710     ]
780                                                   711 
781     def print_header(self):                       712     def print_header(self):
782         header = '''\                             713         header = '''\
783 /* This is auto-generated file. See bpf_doc.py    714 /* This is auto-generated file. See bpf_doc.py for details. */
784                                                   715 
785 /* Forward declarations of BPF structs */'''      716 /* Forward declarations of BPF structs */'''
786                                                   717 
787         print(header)                             718         print(header)
788         for fwd in self.type_fwds:                719         for fwd in self.type_fwds:
789             print('%s;' % fwd)                    720             print('%s;' % fwd)
790         print('')                                 721         print('')
791                                                   722 
792     def print_footer(self):                       723     def print_footer(self):
793         footer = ''                               724         footer = ''
794         print(footer)                             725         print(footer)
795                                                   726 
796     def map_type(self, t):                        727     def map_type(self, t):
797         if t in self.known_types:                 728         if t in self.known_types:
798             return t                              729             return t
799         if t in self.mapped_types:                730         if t in self.mapped_types:
800             return self.mapped_types[t]           731             return self.mapped_types[t]
801         print("Unrecognized type '%s', please     732         print("Unrecognized type '%s', please add it to known types!" % t,
802               file=sys.stderr)                    733               file=sys.stderr)
803         sys.exit(1)                               734         sys.exit(1)
804                                                   735 
805     seen_helpers = set()                          736     seen_helpers = set()
806                                                   737 
807     def print_one(self, helper):                  738     def print_one(self, helper):
808         proto = helper.proto_break_down()         739         proto = helper.proto_break_down()
809                                                   740 
810         if proto['name'] in self.seen_helpers:    741         if proto['name'] in self.seen_helpers:
811             return                                742             return
812         self.seen_helpers.add(proto['name'])      743         self.seen_helpers.add(proto['name'])
813                                                   744 
814         print('/*')                               745         print('/*')
815         print(" * %s" % proto['name'])            746         print(" * %s" % proto['name'])
816         print(" *")                               747         print(" *")
817         if (helper.desc):                         748         if (helper.desc):
818             # Do not strip all newline charact    749             # Do not strip all newline characters: formatted code at the end of
819             # a section must be followed by a     750             # a section must be followed by a blank line.
820             for line in re.sub('\n$', '', help    751             for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
821                 print(' *{}{}'.format(' \t' if    752                 print(' *{}{}'.format(' \t' if line else '', line))
822                                                   753 
823         if (helper.ret):                          754         if (helper.ret):
824             print(' *')                           755             print(' *')
825             print(' * Returns')                   756             print(' * Returns')
826             for line in helper.ret.rstrip().sp    757             for line in helper.ret.rstrip().split('\n'):
827                 print(' *{}{}'.format(' \t' if    758                 print(' *{}{}'.format(' \t' if line else '', line))
828                                                   759 
829         print(' */')                              760         print(' */')
830         print('static %s %s(* const %s)(' % (s !! 761         print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
831                                       proto['r    762                                       proto['ret_star'], proto['name']), end='')
832         comma = ''                                763         comma = ''
833         for i, a in enumerate(proto['args']):     764         for i, a in enumerate(proto['args']):
834             t = a['type']                         765             t = a['type']
835             n = a['name']                         766             n = a['name']
836             if proto['name'] in self.overloade    767             if proto['name'] in self.overloaded_helpers and i == 0:
837                     t = 'void'                    768                     t = 'void'
838                     n = 'ctx'                     769                     n = 'ctx'
839             one_arg = '{}{}'.format(comma, sel    770             one_arg = '{}{}'.format(comma, self.map_type(t))
840             if n:                                 771             if n:
841                 if a['star']:                     772                 if a['star']:
842                     one_arg += ' {}'.format(a[    773                     one_arg += ' {}'.format(a['star'])
843                 else:                             774                 else:
844                     one_arg += ' '                775                     one_arg += ' '
845                 one_arg += '{}'.format(n)         776                 one_arg += '{}'.format(n)
846             comma = ', '                          777             comma = ', '
847             print(one_arg, end='')                778             print(one_arg, end='')
848                                                   779 
849         print(') = (void *) %d;' % helper.enum !! 780         print(') = (void *) %d;' % len(self.seen_helpers))
850         print('')                                 781         print('')
851                                                   782 
852 ##############################################    783 ###############################################################################
853                                                   784 
854 # If script is launched from scripts/ from ker    785 # If script is launched from scripts/ from kernel tree and can access
855 # ../include/uapi/linux/bpf.h, use it as a def    786 # ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
856 # otherwise the --filename argument will be re    787 # otherwise the --filename argument will be required from the command line.
857 script = os.path.abspath(sys.argv[0])             788 script = os.path.abspath(sys.argv[0])
858 linuxRoot = os.path.dirname(os.path.dirname(sc    789 linuxRoot = os.path.dirname(os.path.dirname(script))
859 bpfh = os.path.join(linuxRoot, 'include/uapi/l    790 bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
860                                                   791 
861 printers = {                                      792 printers = {
862         'helpers': PrinterHelpersRST,             793         'helpers': PrinterHelpersRST,
863         'syscall': PrinterSyscallRST,             794         'syscall': PrinterSyscallRST,
864 }                                                 795 }
865                                                   796 
866 argParser = argparse.ArgumentParser(descriptio    797 argParser = argparse.ArgumentParser(description="""
867 Parse eBPF header file and generate documentat    798 Parse eBPF header file and generate documentation for the eBPF API.
868 The RST-formatted output produced can be turne    799 The RST-formatted output produced can be turned into a manual page with the
869 rst2man utility.                                  800 rst2man utility.
870 """)                                              801 """)
871 argParser.add_argument('--header', action='sto    802 argParser.add_argument('--header', action='store_true',
872                        help='generate C header    803                        help='generate C header file')
873 if (os.path.isfile(bpfh)):                        804 if (os.path.isfile(bpfh)):
874     argParser.add_argument('--filename', help=    805     argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
875                            default=bpfh)          806                            default=bpfh)
876 else:                                             807 else:
877     argParser.add_argument('--filename', help=    808     argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
878 argParser.add_argument('target', nargs='?', de    809 argParser.add_argument('target', nargs='?', default='helpers',
879                        choices=printers.keys()    810                        choices=printers.keys(), help='eBPF API target')
880 args = argParser.parse_args()                     811 args = argParser.parse_args()
881                                                   812 
882 # Parse file.                                     813 # Parse file.
883 headerParser = HeaderParser(args.filename)        814 headerParser = HeaderParser(args.filename)
884 headerParser.run()                                815 headerParser.run()
885                                                   816 
886 # Print formatted output to standard output.      817 # Print formatted output to standard output.
887 if args.header:                                   818 if args.header:
888     if args.target != 'helpers':                  819     if args.target != 'helpers':
889         raise NotImplementedError('Only helper    820         raise NotImplementedError('Only helpers header generation is supported')
890     printer = PrinterHelpers(headerParser)        821     printer = PrinterHelpers(headerParser)
891 else:                                             822 else:
892     printer = printers[args.target](headerPars    823     printer = printers[args.target](headerParser)
893 printer.print_all()                               824 printer.print_all()
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php