Skip to contentSkip to conversation

bpf/scripts: assert helper enum value is aligned with comment order

PATCH Superseded Eyal Birger 2 participants 2 messages Aug 24, 2022 BPF

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf/scripts: assert helper enum value is aligned with comment order

The helper value is ABI as defined by enum bpf_func_id.
As bpf_helper_defs.h is used for the userpace part, it must be consistent with this enum.

Before this change the comments order was used by the bpf_doc script in order to set the helper values defined in the helpers file.

When adding new helpers it is very puzzling when the userspace application breaks in weird places if the comment is inserted instead of appended - because the generated helper ABI is incorrect and shifted.

This commit sets the helper value to the enum value.

In addition it is currently the practice to have the comments appended and kept in the same order as the enum. As such, add an assertion validating the comment order is consistent with enum value.

In case a different comments ordering is desired, this assertion can be lifted.

---

v2: based on feedback from Quentin Monnet:
- assert the current comment ordering
- match only one FN in each line

---
 scripts/bpf_doc.py | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
scripts/bpf_doc.py +21 -10
index f4f3e7ec6d44..80dfb230459d 100755
@@ -91,7 +91,7 @@ class HeaderParser(object):
         self.helpers = []
         self.commands = []
         self.desc_unique_helpers = set()
-        self.define_unique_helpers = []
+        self.define_unique_helpers = {}
         self.desc_syscalls = []
         self.enum_syscalls = []
 
@@ -248,24 +248,24 @@ class HeaderParser(object):
                 break
 
     def parse_define_helpers(self):
-        # Parse the number of FN(...) in #define __BPF_FUNC_MAPPER to compare
-        # later with the number of unique function names present in description.
+        # Parse FN(...) in #define __BPF_FUNC_MAPPER to compare later with the
+        # number of unique function names present in description and use the
+        # correct enumeration value.
         # Note: seek_to(..) discards the first line below the target search text,
         # resulting in FN(unspec) being skipped and not added to self.define_unique_helpers.
         self.seek_to('#define __BPF_FUNC_MAPPER(FN)',
                      'Could not find start of eBPF helper definition list')
-        # Searches for either one or more FN(\w+) defines or a backslash for newline
-        p = re.compile('\s*(FN\(\w+\))+|\\\\')
-        fn_defines_str = ''
+        # Searches for one FN(\w+) define or a backslash for newline
+        p = re.compile('\s*FN\((\w+)\)|\\\\')
+        i = 1  # 'unspec' is skipped as mentioned above
         while True:
             capture = p.match(self.line)
             if capture:
-                fn_defines_str += self.line
+                self.define_unique_helpers[capture.expand(r'bpf_\1')] = i
+                i += 1
             else:
                 break
             self.line = self.reader.readline()
-        # Find the number of occurences of FN(\w+)
-        self.define_unique_helpers = re.findall('FN\(\w+\)', fn_defines_str)
 
     def run(self):
         self.parse_desc_syscall()
@@ -608,6 +608,7 @@ class PrinterHelpers(Printer):
     def __init__(self, parser):
         self.elements = parser.helpers
         self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
+        self.define_unique_helpers = parser.define_unique_helpers
 
     type_fwds = [
             'struct bpf_fib_lookup',
@@ -796,7 +797,17 @@ class PrinterHelpers(Printer):
             comma = ', '
             print(one_arg, end='')
 
-        print(') = (void *) %d;' % len(self.seen_helpers))
+        helper_val = self.define_unique_helpers[proto['name']]
+
+        # Assert helper description order is aligned with the enum value
+        desc_val = len(self.seen_helpers)
+        if helper_val != desc_val:
+            print("Helper %s comment order (#%d) must be aligned with its enum "
+                  "order (#%d)" % (proto['name'], desc_val, helper_val),
+                  file=sys.stderr)
+            sys.exit(1)
+
+        print(') = (void *) %d;' % helper_val)
         print('')
 
 ###############################################################################
Signature · 1 line
2.34.1
#1 lore raw
Quoted 86 lines · Eyal Birger

The helper value is ABI as defined by enum bpf_func_id.
As bpf_helper_defs.h is used for the userpace part, it must be consistent with this enum.

Before this change the comments order was used by the bpf_doc script in order to set the helper values defined in the helpers file.

When adding new helpers it is very puzzling when the userspace application breaks in weird places if the comment is inserted instead of appended - because the generated helper ABI is incorrect and shifted.

This commit sets the helper value to the enum value.

In addition it is currently the practice to have the comments appended and kept in the same order as the enum. As such, add an assertion validating the comment order is consistent with enum value.

In case a different comments ordering is desired, this assertion can be lifted.

---

v2: based on feedback from Quentin Monnet:
- assert the current comment ordering
- match only one FN in each line

---
 scripts/bpf_doc.py | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
scripts/bpf_doc.py +10 -9
index f4f3e7ec6d44..80dfb230459d 100755
@@ -91,7 +91,7 @@ class HeaderParser(object):
         self.helpers = []
         self.commands = []
         self.desc_unique_helpers = set()
-        self.define_unique_helpers = []
+        self.define_unique_helpers = {}
         self.desc_syscalls = []
         self.enum_syscalls = []
 
@@ -248,24 +248,24 @@ class HeaderParser(object):
                 break
 
     def parse_define_helpers(self):
-        # Parse the number of FN(...) in #define __BPF_FUNC_MAPPER to compare
-        # later with the number of unique function names present in description.
+        # Parse FN(...) in #define __BPF_FUNC_MAPPER to compare later with the
+        # number of unique function names present in description and use the
+        # correct enumeration value.
         # Note: seek_to(..) discards the first line below the target search text,
         # resulting in FN(unspec) being skipped and not added to self.define_unique_helpers.
         self.seek_to('#define __BPF_FUNC_MAPPER(FN)',
                      'Could not find start of eBPF helper definition list')
-        # Searches for either one or more FN(\w+) defines or a backslash for newline
-        p = re.compile('\s*(FN\(\w+\))+|\\\\')
-        fn_defines_str = ''
+        # Searches for one FN(\w+) define or a backslash for newline
+        p = re.compile('\s*FN\((\w+)\)|\\\\')
+        i = 1  # 'unspec' is skipped as mentioned above
         while True:
             capture = p.match(self.line)
             if capture:
-                fn_defines_str += self.line
+                self.define_unique_helpers[capture.expand(r'bpf_\1')] = i
+                i += 1
             else:
                 break
             self.line = self.reader.readline()
-        # Find the number of occurences of FN(\w+)
-        self.define_unique_helpers = re.findall('FN\(\w+\)', fn_defines_str)
 
     def run(self):
         self.parse_desc_syscall()
@@ -608,6 +608,7 @@ class PrinterHelpers(Printer):
     def __init__(self, parser):
         self.elements = parser.helpers
         self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
+        self.define_unique_helpers = parser.define_unique_helpers
 
     type_fwds = [
             'struct bpf_fib_lookup',

scripts/bpf_doc.py class PrinterHelpers(Printer): · line 802

Quoted 7 lines
scripts/bpf_doc.py +3 -1
@@ -796,7 +797,17 @@ class PrinterHelpers(Printer):
             comma = ', '
             print(one_arg, end='')
 
-        print(') = (void *) %d;' % len(self.seen_helpers))
+        helper_val = self.define_unique_helpers[proto['name']]
+
+        # Assert helper description order is aligned with the enum value

Thanks! But this check should go in the parser, not in the printer. One reason is that it won't show when generating the man page, with this version; another reason is that it should return early and avoid generating an incomplete, non-functional header if the assertion fails.

scripts/bpf_doc.py class PrinterHelpers(Printer): · line 806

Quoted 4 lines
scripts/bpf_doc.py +4 -0
+        desc_val = len(self.seen_helpers)
+        if helper_val != desc_val:
+            print("Helper %s comment order (#%d) must be aligned with its enum "
+                  "order (#%d)" % (proto['name'], desc_val, helper_val),

Please don't split the string on two lines, never mind if it goes over 80 characters. Makes it harder to grep otherwise.

Suggestion on the message: "with its enum order (<nb>)" -> "with its position (<nb>) in enum bpf_func_id"?

scripts/bpf_doc.py class PrinterHelpers(Printer): · line 808

Quoted 2 lines
scripts/bpf_doc.py +2 -0
+                  file=sys.stderr)
+            sys.exit(1)

Can we raise an Exception instead of printing to stderr + exiting?

Quoted 5 lines
scripts/bpf_doc.py +2 -0
+
+        print(') = (void *) %d;' % helper_val)
         print('')
 
 ##############################################################################