History

scripts/bpf: Abstract eBPF API target parameter

Abstract out the target parameter so that upcoming commits, more than just the existing "helpers" target can be called to generate specific portions of docs from the eBPF UAPI headers.

Joe Stringer committed 5 years ago

Commit 923a9321 parent 0cb80457 files · +69 −33Browse files

Showing the changes to scripts (1 of 7 files). Show all

Unified · Split

scripts/bpf_doc.pyrenamed from scripts/bpf_helpers_doc.py+63−28
@@ -2,6 +2,7 @@ · 1 unmodified line
2# SPDX-License-Identifier: GPL-2.0-only2# SPDX-License-Identifier: GPL-2.0-only
3#3#
4# Copyright (C) 2018-2019 Netronome Systems, Inc.4# Copyright (C) 2018-2019 Netronome Systems, Inc.
5# Copyright (C) 2021 Isovalent, Inc.
5 6
6# In case user attempts to run with Python 2.7# In case user attempts to run with Python 2.
7from __future__ import print_function8from __future__ import print_function
@@ -165,10 +166,11 @@ class Printer(object): · 157 unmodified lines
165 """166 """
166 A generic class for printers. Printers should be created with an array of167 A generic class for printers. Printers should be created with an array of
167 Helper objects, and implement a way to print them in the desired fashion.168 Helper objects, and implement a way to print them in the desired fashion.
168 @helpers: array of Helper objects to print to standard output169 @parser: A HeaderParser with objects to print to standard output
169 """170 """
170 def __init__(self, helpers):171 def __init__(self, parser):
171 self.helpers = helpers172 self.parser = parser
173 self.elements = []
172 174
173 def print_header(self):175 def print_header(self):
174 pass176 pass
@@ -181,19 +183,23 @@ class Printer(object): · 6 unmodified lines
181 183
182 def print_all(self):184 def print_all(self):
183 self.print_header()185 self.print_header()
184 for helper in self.helpers:186 for elem in self.elements:
185 self.print_one(helper)187 self.print_one(elem)
186 self.print_footer()188 self.print_footer()
187 189
190
188class PrinterRST(Printer):191class PrinterRST(Printer):
189 """192 """
190 A printer for dumping collected information about helpers as a ReStructured193 A generic class for printers that print ReStructured Text. Printers should
191 Text page compatible with the rst2man program, which can be used to194 be created with a HeaderParser object, and implement a way to print API
192 generate a manual page for the helpers.195 elements in the desired fashion.
193 @helpers: array of Helper objects to print to standard output196 @parser: A HeaderParser with objects to print to standard output
194 """197 """
195 def print_header(self):198 def __init__(self, parser):
196 header = '''\199 self.parser = parser
200
201 def print_license(self):
202 license = '''\
197.. Copyright (C) All BPF authors and contributors from 2014 to present.203.. Copyright (C) All BPF authors and contributors from 2014 to present.
198.. See git log include/uapi/linux/bpf.h in kernel tree for details.204.. See git log include/uapi/linux/bpf.h in kernel tree for details.
199.. 205..
@@ -221,9 +227,39 @@ class PrinterRST(Printer): · 21 unmodified lines
221.. 227..
222.. Please do not edit this file. It was generated from the documentation228.. Please do not edit this file. It was generated from the documentation
223.. located in file include/uapi/linux/bpf.h of the Linux kernel sources229.. located in file include/uapi/linux/bpf.h of the Linux kernel sources
224.. (helpers description), and from scripts/bpf_helpers_doc.py in the same230.. (helpers description), and from scripts/bpf_doc.py in the same
225.. repository (header and footer).231.. repository (header and footer).
232'''
233 print(license)
234
235 def print_elem(self, elem):
236 if (elem.desc):
237 print('\tDescription')
238 # Do not strip all newline characters: formatted code at the end of
239 # a section must be followed by a blank line.
240 for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
241 print('{}{}'.format('\t\t' if line else '', line))
242
243 if (elem.ret):
244 print('\tReturn')
245 for line in elem.ret.rstrip().split('\n'):
246 print('{}{}'.format('\t\t' if line else '', line))
247
248 print('')
226 249
250
251class PrinterHelpersRST(PrinterRST):
252 """
253 A printer for dumping collected information about helpers as a ReStructured
254 Text page compatible with the rst2man program, which can be used to
255 generate a manual page for the helpers.
256 @parser: A HeaderParser with Helper objects to print to standard output
257 """
258 def __init__(self, parser):
259 self.elements = parser.helpers
260
261 def print_header(self):
262 header = '''\
227===========263===========
228BPF-HELPERS264BPF-HELPERS
229===========265===========
@@ -264,6 +300,7 @@ kernel at the top). · 34 unmodified lines
264HELPERS300HELPERS
265=======301=======
266'''302'''
303 PrinterRST.print_license(self)
267 print(header)304 print(header)
268 305
269 def print_footer(self):306 def print_footer(self):
@@ -380,27 +417,19 @@ SEE ALSO · 110 unmodified lines
380 417
381 def print_one(self, helper):418 def print_one(self, helper):
382 self.print_proto(helper)419 self.print_proto(helper)
420 self.print_elem(helper)
383 421
384 if (helper.desc):
385 print('\tDescription')
386 # Do not strip all newline characters: formatted code at the end of
387 # a section must be followed by a blank line.
388 for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
389 print('{}{}'.format('\t\t' if line else '', line))
390 422
391 if (helper.ret):
392 print('\tReturn')
393 for line in helper.ret.rstrip().split('\n'):
394 print('{}{}'.format('\t\t' if line else '', line))
395 423
396 print('')
397 424
398class PrinterHelpers(Printer):425class PrinterHelpers(Printer):
399 """426 """
400 A printer for dumping collected information about helpers as C header to427 A printer for dumping collected information about helpers as C header to
401 be included from BPF program.428 be included from BPF program.
402 @helpers: array of Helper objects to print to standard output429 @parser: A HeaderParser with Helper objects to print to standard output
403 """430 """
431 def __init__(self, parser):
432 self.elements = parser.helpers
404 433
405 type_fwds = [434 type_fwds = [
406 'struct bpf_fib_lookup',435 'struct bpf_fib_lookup',
@@ -511,7 +540,7 @@ class PrinterHelpers(Printer): · 104 unmodified lines
511 540
512 def print_header(self):541 def print_header(self):
513 header = '''\542 header = '''\
514/* This is auto-generated file. See bpf_helpers_doc.py for details. */543/* This is auto-generated file. See bpf_doc.py for details. */
515 544
516/* Forward declarations of BPF structs */'''545/* Forward declarations of BPF structs */'''
517 546
@@ -589,8 +618,12 @@ script = os.path.abspath(sys.argv[0]) · 71 unmodified lines
589linuxRoot = os.path.dirname(os.path.dirname(script))618linuxRoot = os.path.dirname(os.path.dirname(script))
590bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')619bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
591 620
621printers = {
622 'helpers': PrinterHelpersRST,
623}
624
592argParser = argparse.ArgumentParser(description="""625argParser = argparse.ArgumentParser(description="""
593Parse eBPF header file and generate documentation for eBPF helper functions.626Parse eBPF header file and generate documentation for the eBPF API.
594The RST-formatted output produced can be turned into a manual page with the627The RST-formatted output produced can be turned into a manual page with the
595rst2man utility.628rst2man utility.
596""")629""")
@@ -601,6 +634,8 @@ if (os.path.isfile(bpfh)): · 4 unmodified lines
601 default=bpfh)634 default=bpfh)
602else:635else:
603 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')636 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
637argParser.add_argument('target', nargs='?', default='helpers',
638 choices=printers.keys(), help='eBPF API target')
604args = argParser.parse_args()639args = argParser.parse_args()
605 640
606# Parse file.641# Parse file.
@@ -609,7 +644,7 @@ headerParser.run() · 2 unmodified lines
609 644
610# Print formatted output to standard output.645# Print formatted output to standard output.
611if args.header:646if args.header:
612 printer = PrinterHelpers(headerParser.helpers)647 printer = PrinterHelpers(headerParser)
613else:648else:
614 printer = PrinterRST(headerParser.helpers)649 printer = printers[args.target](headerParser)
615printer.print_all()650printer.print_all()