bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Stringer <joe@wand.net.nz>
To: bpf@vger.kernel.org
Cc: Joe Stringer <joe@cilium.io>,
	netdev@vger.kernel.org, daniel@iogearbox.net, ast@kernel.org,
	mtk.manpages@gmail.com, Quentin Monnet <quentin@isovalent.com>
Subject: [PATCH bpf-next 10/17] scripts/bpf: Abstract eBPF API target parameter
Date: Tue, 16 Feb 2021 17:08:14 -0800	[thread overview]
Message-ID: <20210217010821.1810741-11-joe@wand.net.nz> (raw)
In-Reply-To: <20210217010821.1810741-1-joe@wand.net.nz>

From: Joe Stringer <joe@cilium.io>

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.

Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Joe Stringer <joe@cilium.io>
---
 scripts/bpf_doc.py | 87 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 61 insertions(+), 26 deletions(-)

diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
index ca6e7559d696..5a4f68aab335 100755
--- a/scripts/bpf_doc.py
+++ b/scripts/bpf_doc.py
@@ -2,6 +2,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 #
 # Copyright (C) 2018-2019 Netronome Systems, Inc.
+# Copyright (C) 2021 Isovalent, Inc.
 
 # In case user attempts to run with Python 2.
 from __future__ import print_function
@@ -165,10 +166,11 @@ class Printer(object):
     """
     A generic class for printers. Printers should be created with an array of
     Helper objects, and implement a way to print them in the desired fashion.
-    @helpers: array of Helper objects to print to standard output
+    @parser: A HeaderParser with objects to print to standard output
     """
-    def __init__(self, helpers):
-        self.helpers = helpers
+    def __init__(self, parser):
+        self.parser = parser
+        self.elements = []
 
     def print_header(self):
         pass
@@ -181,19 +183,23 @@ class Printer(object):
 
     def print_all(self):
         self.print_header()
-        for helper in self.helpers:
-            self.print_one(helper)
+        for elem in self.elements:
+            self.print_one(elem)
         self.print_footer()
 
+
 class PrinterRST(Printer):
     """
-    A printer for dumping collected information about helpers as a ReStructured
-    Text page compatible with the rst2man program, which can be used to
-    generate a manual page for the helpers.
-    @helpers: array of Helper objects to print to standard output
+    A generic class for printers that print ReStructured Text. Printers should
+    be created with a HeaderParser object, and implement a way to print API
+    elements in the desired fashion.
+    @parser: A HeaderParser with objects to print to standard output
     """
-    def print_header(self):
-        header = '''\
+    def __init__(self, parser):
+        self.parser = parser
+
+    def print_license(self):
+        license = '''\
 .. Copyright (C) All BPF authors and contributors from 2014 to present.
 .. See git log include/uapi/linux/bpf.h in kernel tree for details.
 .. 
@@ -223,7 +229,37 @@ class PrinterRST(Printer):
 .. located in file include/uapi/linux/bpf.h of the Linux kernel sources
 .. (helpers description), and from scripts/bpf_doc.py in the same
 .. repository (header and footer).
+'''
+        print(license)
+
+    def print_elem(self, elem):
+        if (elem.desc):
+            print('\tDescription')
+            # Do not strip all newline characters: formatted code at the end of
+            # a section must be followed by a blank line.
+            for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
+                print('{}{}'.format('\t\t' if line else '', line))
+
+        if (elem.ret):
+            print('\tReturn')
+            for line in elem.ret.rstrip().split('\n'):
+                print('{}{}'.format('\t\t' if line else '', line))
+
+        print('')
 
+
+class PrinterHelpersRST(PrinterRST):
+    """
+    A printer for dumping collected information about helpers as a ReStructured
+    Text page compatible with the rst2man program, which can be used to
+    generate a manual page for the helpers.
+    @parser: A HeaderParser with Helper objects to print to standard output
+    """
+    def __init__(self, parser):
+        self.elements = parser.helpers
+
+    def print_header(self):
+        header = '''\
 ===========
 BPF-HELPERS
 ===========
@@ -264,6 +300,7 @@ kernel at the top).
 HELPERS
 =======
 '''
+        PrinterRST.print_license(self)
         print(header)
 
     def print_footer(self):
@@ -380,27 +417,19 @@ SEE ALSO
 
     def print_one(self, helper):
         self.print_proto(helper)
+        self.print_elem(helper)
 
-        if (helper.desc):
-            print('\tDescription')
-            # Do not strip all newline characters: formatted code at the end of
-            # a section must be followed by a blank line.
-            for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
-                print('{}{}'.format('\t\t' if line else '', line))
 
-        if (helper.ret):
-            print('\tReturn')
-            for line in helper.ret.rstrip().split('\n'):
-                print('{}{}'.format('\t\t' if line else '', line))
 
-        print('')
 
 class PrinterHelpers(Printer):
     """
     A printer for dumping collected information about helpers as C header to
     be included from BPF program.
-    @helpers: array of Helper objects to print to standard output
+    @parser: A HeaderParser with Helper objects to print to standard output
     """
+    def __init__(self, parser):
+        self.elements = parser.helpers
 
     type_fwds = [
             'struct bpf_fib_lookup',
@@ -589,8 +618,12 @@ script = os.path.abspath(sys.argv[0])
 linuxRoot = os.path.dirname(os.path.dirname(script))
 bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
 
+printers = {
+        'helpers': PrinterHelpersRST,
+}
+
 argParser = argparse.ArgumentParser(description="""
-Parse eBPF header file and generate documentation for eBPF helper functions.
+Parse eBPF header file and generate documentation for the eBPF API.
 The RST-formatted output produced can be turned into a manual page with the
 rst2man utility.
 """)
@@ -601,6 +634,8 @@ if (os.path.isfile(bpfh)):
                            default=bpfh)
 else:
     argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
+argParser.add_argument('target', nargs='?', default='helpers',
+                       choices=printers.keys(), help='eBPF API target')
 args = argParser.parse_args()
 
 # Parse file.
@@ -609,7 +644,7 @@ headerParser.run()
 
 # Print formatted output to standard output.
 if args.header:
-    printer = PrinterHelpers(headerParser.helpers)
+    printer = PrinterHelpers(headerParser)
 else:
-    printer = PrinterRST(headerParser.helpers)
+    printer = printers[args.target](headerParser)
 printer.print_all()
-- 
2.27.0


  parent reply	other threads:[~2021-02-17  1:10 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-17  1:08 [PATCH bpf-next 00/17] Improve BPF syscall command documentation Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 01/17] bpf: Import syscall arg documentation Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 02/17] bpf: Add minimal bpf() command documentation Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 03/17] bpf: Document BPF_F_LOCK in syscall commands Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 04/17] bpf: Document BPF_PROG_PIN syscall command Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 05/17] bpf: Document BPF_PROG_ATTACH " Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 06/17] bpf: Document BPF_PROG_TEST_RUN " Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 07/17] bpf: Document BPF_PROG_QUERY " Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 08/17] bpf: Document BPF_MAP_*_BATCH syscall commands Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 09/17] scripts/bpf: Rename bpf_helpers_doc.py -> bpf_doc.py Joe Stringer
2021-02-17  1:08 ` Joe Stringer [this message]
2021-02-17  1:08 ` [PATCH bpf-next 11/17] scripts/bpf: Add syscall commands printer Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 12/17] tools/bpf: Rename Makefile.{helpers,docs} Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 13/17] tools/bpf: Templatize man page generation Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 14/17] tools/bpf: Build bpf-sycall.2 in Makefile.docs Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 15/17] selftests/bpf: Add docs target Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 16/17] docs/bpf: Add bpf() syscall command reference Joe Stringer
2021-02-17  1:08 ` [PATCH bpf-next 17/17] tools: Sync uapi bpf.h header with latest changes Joe Stringer
2021-02-17 13:55 ` [PATCH bpf-next 00/17] Improve BPF syscall command documentation Toke Høiland-Jørgensen
2021-02-18  4:08   ` Joe Stringer
2021-02-18 11:33     ` Toke Høiland-Jørgensen
2021-02-17 17:32 ` Jonathan Corbet
2021-02-18  4:22   ` Joe Stringer
2021-02-18 19:26     ` Jonathan Corbet
2021-02-18 21:53       ` Joe Stringer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210217010821.1810741-11-joe@wand.net.nz \
    --to=joe@wand.net.nz \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=joe@cilium.io \
    --cc=mtk.manpages@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=quentin@isovalent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).