Skip to contentSkip to conversation

xdrgen: Emit maxsize macros

PATCH New [email protected] 1 participant 17 messages Oct 3, 2024

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

v2 cover xdrgen: Emit maxsize macros

From: Chuck Lever <[email protected]>

This series implements the generation of "maxsize" values for each XDR data type defined in a specification. These are emitted as C pre-processor macros, following the lead of existing XDR functions in the kernel. The macros are added to the header file containing C type definitions.

This facility takes xdrgen a step closer to the generation of all XDR code needed for each RPC protocol implementation in the kernel.

Changes since v1:
- Resend including all 16 patches (d'oh)

Chuck Lever (16):
xdrgen: Refactor transformer arms
xdrgen: Track constant values
xdrgen: Keep track of on-the-wire data type widths
xdrgen: XDR widths for enum types
xdrgen: XDR width for fixed-length opaque
xdrgen: XDR width for variable-length opaque
xdrgen: XDR width for a string
xdrgen: XDR width for fixed-length array
xdrgen: XDR width for variable-length array
xdrgen: XDR width for optional_data type
xdrgen: XDR width for typedef
xdrgen: XDR width for struct types
xdrgen: XDR width for pointer types
xdrgen: XDR width for union types
xdrgen: Add generator code for XDR width macros
xdrgen: emit maxsize macros

 include/linux/sunrpc/xdrgen/_defs.h           |   9 +
 .../net/sunrpc/xdrgen/generators/__init__.py  |   4 +
 tools/net/sunrpc/xdrgen/generators/enum.py    |  13 +-
 tools/net/sunrpc/xdrgen/generators/pointer.py |  18 +-
 tools/net/sunrpc/xdrgen/generators/struct.py  |  18 +-
 tools/net/sunrpc/xdrgen/generators/typedef.py |  18 +-
 tools/net/sunrpc/xdrgen/generators/union.py   |  20 +-
 .../net/sunrpc/xdrgen/subcmds/definitions.py  |  24 +-
 tools/net/sunrpc/xdrgen/subcmds/source.py     |   3 +-
 .../xdrgen/templates/C/enum/maxsize/enum.j2   |   2 +
 .../templates/C/pointer/maxsize/pointer.j2    |   3 +
 .../templates/C/struct/maxsize/struct.j2      |   3 +
 .../templates/C/typedef/maxsize/basic.j2      |   3 +
 .../C/typedef/maxsize/fixed_length_opaque.j2  |   2 +
 .../templates/C/typedef/maxsize/string.j2     |   2 +
 .../typedef/maxsize/variable_length_array.j2  |   2 +
 .../typedef/maxsize/variable_length_opaque.j2 |   2 +
 .../xdrgen/templates/C/union/maxsize/union.j2 |   3 +
 tools/net/sunrpc/xdrgen/xdr_ast.py            | 292 ++++++++++++++++--
 19 files changed, 406 insertions(+), 35 deletions(-)
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/enum/maxsize/enum.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/pointer/maxsize/pointer.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/struct/maxsize/struct.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/basic.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/fixed_length_opaque.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/string.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/variable_length_array.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/variable_length_opaque.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/union/maxsize/union.j2
Signature · 1 line
2.46.2
author maintainer
#1 lore raw

PATCH v2 1/16 xdrgen: Refactor transformer arms

From: Chuck Lever <[email protected]>

Clean up: Add a __post_init__ function to the data classes that need to update the "structs" and "pass_by_reference" sets.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 57 +++++++++++++++++-------------
 1 file changed, 33 insertions(+), 24 deletions(-)
tools/net/sunrpc/xdrgen/xdr_ast.py +33 -24
index d5f0535ec84c..68f09945f2c4 100644
@@ -51,13 +51,17 @@ class _XdrTypeSpecifier(_XdrAst):
     """Corresponds to 'type_specifier' in the XDR language grammar"""
 
     type_name: str
-    c_classifier: str
+    c_classifier: str = ""
 
 
 @dataclass
 class _XdrDefinedType(_XdrTypeSpecifier):
     """Corresponds to a type defined by the input specification"""
 
+    def __post_init__(self):
+        if self.type_name in structs:
+            self.c_classifier = "struct "
+
 
 @dataclass
 class _XdrBuiltInType(_XdrTypeSpecifier):
@@ -124,6 +128,10 @@ class _XdrOptionalData(_XdrDeclaration):
     spec: _XdrTypeSpecifier
     template: str = "optional_data"
 
+    def __post_init__(self):
+        structs.add(self.name)
+        pass_by_reference.add(self.name)
+
 
 @dataclass
 class _XdrBasic(_XdrDeclaration):
@@ -174,6 +182,10 @@ class _XdrStruct(_XdrAst):
     name: str
     fields: List[_XdrDeclaration]
 
+    def __post_init__(self):
+        structs.add(self.name)
+        pass_by_reference.add(self.name)
+
 
 @dataclass
 class _XdrPointer(_XdrAst):
@@ -182,6 +194,10 @@ class _XdrPointer(_XdrAst):
     name: str
     fields: List[_XdrDeclaration]
 
+    def __post_init__(self):
+        structs.add(self.name)
+        pass_by_reference.add(self.name)
+
 
 @dataclass
 class _XdrTypedef(_XdrAst):
@@ -189,6 +205,15 @@ class _XdrTypedef(_XdrAst):
 
     declaration: _XdrDeclaration
 
+    def __post_init__(self):
+        if not isinstance(self.declaration, _XdrBasic):
+            return
+
+        new_type = self.declaration
+        if isinstance(new_type.spec, _XdrDefinedType):
+            if new_type.spec.type_name in pass_by_reference:
+                pass_by_reference.add(new_type.name)
+
 
 @dataclass
 class _XdrCaseSpec(_XdrAst):
@@ -216,6 +241,10 @@ class _XdrUnion(_XdrAst):
     cases: List[_XdrCaseSpec]
     default: _XdrDeclaration
 
+    def __post_init__(self):
+        structs.add(self.name)
+        pass_by_reference.add(self.name)
+
 
 @dataclass
 class _RpcProcedure(_XdrAst):
@@ -290,22 +319,13 @@ class ParseToAst(Transformer):
         return _XdrConstantValue(value)
 
     def type_specifier(self, children):
-        """Instantiate one type_specifier object"""
-        c_classifier = ""
+        """Instantiate one _XdrTypeSpecifier object"""
         if isinstance(children[0], _XdrIdentifier):
             name = children[0].symbol
-            if name in structs:
-                c_classifier = "struct "
-            return _XdrDefinedType(
-                type_name=name,
-                c_classifier=c_classifier,
-            )
+            return _XdrDefinedType(type_name=name)
 
         name = children[0].data.value
-        return _XdrBuiltInType(
-            type_name=name,
-            c_classifier=c_classifier,
-        )
+        return _XdrBuiltInType(type_name=name)
 
     def constant_def(self, children):
         """Instantiate one _XdrConstant object"""
@@ -380,8 +400,6 @@ class ParseToAst(Transformer):
         """Instantiate one _XdrOptionalData declaration object"""
         spec = children[0]
         name = children[1].symbol
-        structs.add(name)
-        pass_by_reference.add(name)
 
         return _XdrOptionalData(name, spec)
 
@@ -400,8 +418,6 @@ class ParseToAst(Transformer):
     def struct(self, children):
         """Instantiate one _XdrStruct object"""
         name = children[0].symbol
-        structs.add(name)
-        pass_by_reference.add(name)
         fields = children[1].children
 
         last_field = fields[-1]
@@ -416,11 +432,6 @@ class ParseToAst(Transformer):
     def typedef(self, children):
         """Instantiate one _XdrTypedef object"""
         new_type = children[0]
-        if isinstance(new_type, _XdrBasic) and isinstance(
-            new_type.spec, _XdrDefinedType
-        ):
-            if new_type.spec.type_name in pass_by_reference:
-                pass_by_reference.add(new_type.name)
 
         return _XdrTypedef(new_type)
 
@@ -442,8 +453,6 @@ class ParseToAst(Transformer):
     def union(self, children):
         """Instantiate one _XdrUnion object"""
         name = children[0].symbol
-        structs.add(name)
-        pass_by_reference.add(name)
 
         body = children[1]
         discriminant = body.children[0].children[0]
Signature · 1 line
2.46.2
author maintainer
#2 lore raw

PATCH v2 2/16 xdrgen: Track constant values

In reply to [email protected]

From: Chuck Lever <[email protected]>

In order to compute the numeric on-the-wire width of XDR types, xdrgen needs to keep track of the numeric value of constants that are defined in the input specification so it can perform calculations with those values.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +10 -0
index 68f09945f2c4..b7df45f47707 100644
@@ -19,6 +19,8 @@ public_apis = []
 structs = set()
 pass_by_reference = set()
 
+constants = {}
+
 
 @dataclass
 class _XdrAst(ast_utils.Ast):
@@ -156,6 +158,10 @@ class _XdrConstant(_XdrAst):
     name: str
     value: str
 
+    def __post_init__(self):
+        if self.value not in constants:
+            constants[self.name] = int(self.value, 0)
+
 
 @dataclass
 class _XdrEnumerator(_XdrAst):
@@ -164,6 +170,10 @@ class _XdrEnumerator(_XdrAst):
     name: str
     value: str
 
+    def __post_init__(self):
+        if self.value not in constants:
+            constants[self.name] = int(self.value, 0)
+
 
 @dataclass
 class _XdrEnum(_XdrAst):
Signature · 1 line
2.46.2
author maintainer
#3 lore raw

PATCH v2 3/16 xdrgen: Keep track of on-the-wire data type widths

In reply to [email protected]

From: Chuck Lever <[email protected]>

The generic parts of the RPC layer need to know the widths (in XDR_UNIT increments) of the XDR data types defined for each protocol.

As a first step, add dictionaries to keep track of the symbolic and actual maximum XDR width of XDR types.

This makes it straightforward to look up the width of a type by its name. The built-in dictionaries are pre-loaded with the widths of the built-in XDR types as defined in RFC 4506.

---
 include/linux/sunrpc/xdrgen/_defs.h |  9 ++++++
 tools/net/sunrpc/xdrgen/xdr_ast.py  | 43 +++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
include/linux/sunrpc/xdrgen/_defs.h +9 -0
index be9e62371758..20c7270aa64d 100644
@@ -23,4 +23,13 @@ typedef struct {
 	u8 *data;
 } opaque;
 
+#define XDR_void		(0)
+#define XDR_bool		(1)
+#define XDR_int			(1)
+#define XDR_unsigned_int	(1)
+#define XDR_long		(1)
+#define XDR_unsigned_long	(1)
+#define XDR_hyper		(2)
+#define XDR_unsigned_hyper	(2)
+
 #endif /* _SUNRPC_XDRGEN__DEFS_H_ */
tools/net/sunrpc/xdrgen/xdr_ast.py +43 -0

72 lines not on this page · raw message

Signature · 1 line
2.46.2
author maintainer
#4 lore raw

PATCH v2 4/16 xdrgen: XDR widths for enum types

In reply to [email protected]

From: Chuck Lever <[email protected]>

RFC 4506 says that an XDR enum is represented as a signed integer on the wire; thus its width is 1 XDR_UNIT.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +12 -0
index f1d93a1d0ed8..fbee954c7f70 100644
@@ -227,6 +227,18 @@ class _XdrEnum(_XdrAst):
     maximum: int
     enumerators: List[_XdrEnumerator]
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return 1
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        return ["XDR_int"]
+
+    def __post_init__(self):
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
+
 
 @dataclass
 class _XdrStruct(_XdrAst):
Signature · 1 line
2.46.2
author maintainer
#5 lore raw

PATCH v2 5/16 xdrgen: XDR width for fixed-length opaque

In reply to [email protected]

From: Chuck Lever <[email protected]>

The XDR width for a fixed-length opaque is the byte size of the opaque rounded up to the next XDR_UNIT, divided by XDR_UNIT.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +22 -0
index fbee954c7f70..9fe7fa688caa 100644
@@ -21,6 +21,16 @@ pass_by_reference = set()
 
 constants = {}
 
+
+def xdr_quadlen(val: str) -> int:
+    """Return integer XDR width of an XDR type"""
+    if val in constants:
+        octets = constants[val]
+    else:
+        octets = int(val)
+    return int((octets + 3) / 4)
+
+
 symbolic_widths = {
     "void": ["XDR_void"],
     "bool": ["XDR_bool"],
@@ -117,6 +127,18 @@ class _XdrFixedLengthOpaque(_XdrDeclaration):
     size: str
     template: str = "fixed_length_opaque"
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return xdr_quadlen(self.size)
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        return ["XDR_QUADLEN(" + self.size + ")"]
+
+    def __post_init__(self):
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
+
 
 @dataclass
 class _XdrVariableLengthOpaque(_XdrDeclaration):
Signature · 1 line
2.46.2
author maintainer
#6 lore raw

PATCH v2 6/16 xdrgen: XDR width for variable-length opaque

In reply to [email protected]

From: Chuck Lever <[email protected]>

The byte size of a variable-length opaque is conveyed in an unsigned integer. If there is a specified maximum size, that is included in the type's widths list.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +15 -0
index 9fe7fa688caa..94cdcfb36e77 100644
@@ -148,6 +148,21 @@ class _XdrVariableLengthOpaque(_XdrDeclaration):
     maxsize: str
     template: str = "variable_length_opaque"
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return 1 + xdr_quadlen(self.maxsize)
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        widths = ["XDR_unsigned_int"]
+        if self.maxsize != "0":
+            widths.append("XDR_QUADLEN(" + self.maxsize + ")")
+        return widths
+
+    def __post_init__(self):
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
+
 
 @dataclass
 class _XdrString(_XdrDeclaration):
Signature · 1 line
2.46.2
author maintainer
#7 lore raw

PATCH v2 7/16 xdrgen: XDR width for a string

In reply to [email protected]

From: Chuck Lever <[email protected]>

A string works like a variable-length opaque. See Section 4.11 of RFC 4506.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +15 -0
index 94cdcfb36e77..d5f48c094729 100644
@@ -172,6 +172,21 @@ class _XdrString(_XdrDeclaration):
     maxsize: str
     template: str = "string"
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return 1 + xdr_quadlen(self.maxsize)
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        widths = ["XDR_unsigned_int"]
+        if self.maxsize != "0":
+            widths.append("XDR_QUADLEN(" + self.maxsize + ")")
+        return widths
+
+    def __post_init__(self):
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
+
 
 @dataclass
 class _XdrFixedLengthArray(_XdrDeclaration):
Signature · 1 line
2.46.2
author maintainer
#8 lore raw

PATCH v2 8/16 xdrgen: XDR width for fixed-length array

In reply to [email protected]

From: Chuck Lever <[email protected]>

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +13 -0
index d5f48c094729..e9bc81e83b48 100644
@@ -197,6 +197,19 @@ class _XdrFixedLengthArray(_XdrDeclaration):
     size: str
     template: str = "fixed_length_array"
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return xdr_quadlen(self.size) * max_widths[self.spec.type_name]
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        item_width = " + ".join(symbolic_widths[self.spec.type_name])
+        return ["(" + self.size + " * (" + item_width + "))"]
+
+    def __post_init__(self):
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
+
 
 @dataclass
 class _XdrVariableLengthArray(_XdrDeclaration):
Signature · 1 line
2.46.2
author maintainer
#9 lore raw

PATCH v2 9/16 xdrgen: XDR width for variable-length array

In reply to [email protected]

From: Chuck Lever <[email protected]>

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +16 -0
index e9bc81e83b48..cb89d5d9987c 100644
@@ -220,6 +220,22 @@ class _XdrVariableLengthArray(_XdrDeclaration):
     maxsize: str
     template: str = "variable_length_array"
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return 1 + (xdr_quadlen(self.maxsize) * max_widths[self.spec.type_name])
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        widths = ["XDR_unsigned_int"]
+        if self.maxsize != "0":
+            item_width = " + ".join(symbolic_widths[self.spec.type_name])
+            widths.append("(" + self.maxsize + " * (" + item_width + "))")
+        return widths
+
+    def __post_init__(self):
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
+
 
 @dataclass
 class _XdrOptionalData(_XdrDeclaration):
Signature · 1 line
2.46.2
author maintainer
#10 lore raw

PATCH v2 10/16 xdrgen: XDR width for optional_data type

In reply to [email protected]

From: Chuck Lever <[email protected]>

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +10 -0
index cb89d5d9987c..f2ef78654e36 100644
@@ -245,9 +245,19 @@ class _XdrOptionalData(_XdrDeclaration):
     spec: _XdrTypeSpecifier
     template: str = "optional_data"
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return 1
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        return ["XDR_bool"]
+
     def __post_init__(self):
         structs.add(self.name)
         pass_by_reference.add(self.name)
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
 
 
 @dataclass
Signature · 1 line
2.46.2
author maintainer
#11 lore raw

PATCH v2 11/16 xdrgen: XDR width for typedef

In reply to [email protected]

From: Chuck Lever <[email protected]>

The XDR width of a typedef is the same as the width of the base type.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 34 ++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 7 deletions(-)
tools/net/sunrpc/xdrgen/xdr_ast.py +27 -7
index f2ef78654e36..8996f26cbd55 100644
@@ -268,6 +268,18 @@ class _XdrBasic(_XdrDeclaration):
     spec: _XdrTypeSpecifier
     template: str = "basic"
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return max_widths[self.spec.type_name]
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        return symbolic_widths[self.spec.type_name]
+
+    def __post_init__(self):
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
+
 
 @dataclass
 class _XdrVoid(_XdrDeclaration):
@@ -361,14 +373,22 @@ class _XdrTypedef(_XdrAst):
 
     declaration: _XdrDeclaration
 
-    def __post_init__(self):
-        if not isinstance(self.declaration, _XdrBasic):
-            return
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        return self.declaration.max_width()
 
-        new_type = self.declaration
-        if isinstance(new_type.spec, _XdrDefinedType):
-            if new_type.spec.type_name in pass_by_reference:
-                pass_by_reference.add(new_type.name)
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        return self.declaration.symbolic_width()
+
+    def __post_init__(self):
+        if isinstance(self.declaration, _XdrBasic):
+            new_type = self.declaration
+            if isinstance(new_type.spec, _XdrDefinedType):
+                if new_type.spec.type_name in pass_by_reference:
+                    pass_by_reference.add(new_type.name)
+                max_widths[new_type.name] = self.max_width()
+                symbolic_widths[new_type.name] = self.symbolic_width()
 
 
 @dataclass
Signature · 1 line
2.46.2
author maintainer
#12 lore raw

PATCH v2 12/16 xdrgen: XDR width for struct types

In reply to [email protected]

From: Chuck Lever <[email protected]>

The XDR width of a struct type is the sum of the widths of each of the struct's fields.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +16 -0
index 8996f26cbd55..f34b147c8dfd 100644
@@ -350,9 +350,25 @@ class _XdrStruct(_XdrAst):
     name: str
     fields: List[_XdrDeclaration]
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        width = 0
+        for field in self.fields:
+            width += field.max_width()
+        return width
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        widths = []
+        for field in self.fields:
+            widths += field.symbolic_width()
+        return widths
+
     def __post_init__(self):
         structs.add(self.name)
         pass_by_reference.add(self.name)
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
 
 
 @dataclass
Signature · 1 line
2.46.2
author maintainer
#13 lore raw

PATCH v2 13/16 xdrgen: XDR width for pointer types

In reply to [email protected]

From: Chuck Lever <[email protected]>

The XDR width of a pointer type is the sum of the widths of each of the struct's fields, except for the last field. The width of the implicit boolean "value follows" field is added as well.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +17 -0
index f34b147c8dfd..8d53c889eee8 100644
@@ -378,9 +378,26 @@ class _XdrPointer(_XdrAst):
     name: str
     fields: List[_XdrDeclaration]
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        width = 1
+        for field in self.fields[0:-1]:
+            width += field.max_width()
+        return width
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        widths = []
+        widths += ["XDR_bool"]
+        for field in self.fields[0:-1]:
+            widths += field.symbolic_width()
+        return widths
+
     def __post_init__(self):
         structs.add(self.name)
         pass_by_reference.add(self.name)
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
 
 
 @dataclass
Signature · 1 line
2.46.2
author maintainer
#14 lore raw

PATCH v2 14/16 xdrgen: XDR width for union types

In reply to [email protected]

From: Chuck Lever <[email protected]>

Not yet complete.

The tool doesn't do any math yet. Thus, even though the maximum XDR width of a union is the width of the union enumerator plus the width of its largest arm, we're using the sum of all the elements of the union for the moment.

This means that buffer size requirements are overestimated, and that the generated maxsize macro cannot yet be used for determining data element alignment in the XDR buffer.

---
 tools/net/sunrpc/xdrgen/xdr_ast.py | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
tools/net/sunrpc/xdrgen/xdr_ast.py +26 -0
index 8d53c889eee8..5233e73c7046 100644
@@ -450,9 +450,35 @@ class _XdrUnion(_XdrAst):
     cases: List[_XdrCaseSpec]
     default: _XdrDeclaration
 
+    def max_width(self) -> int:
+        """Return width of type in XDR_UNITS"""
+        max_width = 0
+        for case in self.cases:
+            if case.arm.max_width() > max_width:
+                max_width = case.arm.max_width()
+        if self.default:
+            if self.default.arm.max_width() > max_width:
+                max_width = self.default.arm.max_width()
+        return 1 + max_width
+
+    def symbolic_width(self) -> List:
+        """Return list containing XDR width of type's components"""
+        max_width = 0
+        for case in self.cases:
+            if case.arm.max_width() > max_width:
+                max_width = case.arm.max_width()
+                width = case.arm.symbolic_width()
+        if self.default:
+            if self.default.arm.max_width() > max_width:
+                max_width = self.default.arm.max_width()
+                width = self.default.arm.symbolic_width()
+        return symbolic_widths[self.discriminant.name] + width
+
     def __post_init__(self):
         structs.add(self.name)
         pass_by_reference.add(self.name)
+        max_widths[self.name] = self.max_width()
+        symbolic_widths[self.name] = self.symbolic_width()
 
 
 @dataclass
Signature · 1 line
2.46.2
author maintainer
#15 lore raw

PATCH v2 15/16 xdrgen: Add generator code for XDR width macros

In reply to [email protected]

From: Chuck Lever <[email protected]>

Introduce logic in the code generators to emit maxsize (XDR width) definitions. In C, these are pre-processor macros.

---
 .../net/sunrpc/xdrgen/generators/__init__.py  |  4 ++++
 tools/net/sunrpc/xdrgen/generators/enum.py    | 13 +++++++++++-
 tools/net/sunrpc/xdrgen/generators/pointer.py | 18 ++++++++++++++++-
 tools/net/sunrpc/xdrgen/generators/struct.py  | 18 ++++++++++++++++-
 tools/net/sunrpc/xdrgen/generators/typedef.py | 18 ++++++++++++++++-
 tools/net/sunrpc/xdrgen/generators/union.py   | 20 +++++++++++++++++--
 .../xdrgen/templates/C/enum/maxsize/enum.j2   |  2 ++
 .../templates/C/pointer/maxsize/pointer.j2    |  3 +++
 .../templates/C/struct/maxsize/struct.j2      |  3 +++
 .../templates/C/typedef/maxsize/basic.j2      |  3 +++
 .../C/typedef/maxsize/fixed_length_opaque.j2  |  2 ++
 .../templates/C/typedef/maxsize/string.j2     |  2 ++
 .../typedef/maxsize/variable_length_array.j2  |  2 ++
 .../typedef/maxsize/variable_length_opaque.j2 |  2 ++
 .../xdrgen/templates/C/union/maxsize/union.j2 |  3 +++
 15 files changed, 107 insertions(+), 6 deletions(-)
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/enum/maxsize/enum.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/pointer/maxsize/pointer.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/struct/maxsize/struct.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/basic.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/fixed_length_opaque.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/string.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/variable_length_array.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/variable_length_opaque.j2
 create mode 100644 tools/net/sunrpc/xdrgen/templates/C/union/maxsize/union.j2
tools/net/sunrpc/xdrgen/generators/__init__.py +4 -0
index fd2457461274..b98574a36a4a 100644
@@ -111,3 +111,7 @@ class SourceGenerator:
     def emit_encoder(self, node: _XdrAst) -> None:
         """Emit one encoder function for this XDR type"""
         raise NotImplementedError("Encoder generation not supported")
+
+    def emit_maxsize(self, node: _XdrAst) -> None:
+        """Emit one maxsize macro for this XDR type"""
+        raise NotImplementedError("Maxsize macro generation not supported")
tools/net/sunrpc/xdrgen/generators/enum.py +12 -1
index e63f45b8eb74..e62f715d3996 100644
@@ -4,7 +4,7 @@
 """Generate code to handle XDR enum types"""
 
 from generators import SourceGenerator, create_jinja2_environment
-from xdr_ast import _XdrEnum, public_apis, big_endian
+from xdr_ast import _XdrEnum, public_apis, big_endian, get_header_name
 
 
 class XdrEnumGenerator(SourceGenerator):
@@ -51,3 +51,14 @@ class XdrEnumGenerator(SourceGenerator):
         else:
             template = self.environment.get_template("encoder/enum.j2")
         print(template.render(name=node.name))
+
+    def emit_maxsize(self, node: _XdrEnum) -> None:
+        """Emit one maxsize macro for an XDR enum type"""
+        macro_name = get_header_name().upper() + "_" + node.name + "_sz"
+        template = self.environment.get_template("maxsize/enum.j2")
+        print(
+            template.render(
+                macro=macro_name,
+                width=" + ".join(node.symbolic_width()),
+            )
+        )
tools/net/sunrpc/xdrgen/generators/pointer.py +17 -1
index 0aa3d35203f5..6dbda60ad2db 100644
@@ -12,7 +12,7 @@ from xdr_ast import _XdrBasic, _XdrString
 from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
 from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
 from xdr_ast import _XdrOptionalData, _XdrPointer, _XdrDeclaration
-from xdr_ast import public_apis
+from xdr_ast import public_apis, get_header_name
 
 
 def emit_pointer_declaration(environment: Environment, node: _XdrPointer) -> None:
@@ -247,6 +247,18 @@ def emit_pointer_encoder(environment: Environment, node: _XdrPointer) -> None:
     print(template.render())
 
 
+def emit_pointer_maxsize(environment: Environment, node: _XdrPointer) -> None:
+    """Emit one maxsize macro for an XDR pointer type"""
+    macro_name = get_header_name().upper() + "_" + node.name + "_sz"
+    template = get_jinja2_template(environment, "maxsize", "pointer")
+    print(
+        template.render(
+            macro=macro_name,
+            width=" + ".join(node.symbolic_width()),
+        )
+    )
+
+
 class XdrPointerGenerator(SourceGenerator):
     """Generate source code for XDR pointer"""
 
@@ -270,3 +282,7 @@ class XdrPointerGenerator(SourceGenerator):
     def emit_encoder(self, node: _XdrPointer) -> None:
         """Emit one encoder function for an XDR pointer type"""
         emit_pointer_encoder(self.environment, node)
+
+    def emit_maxsize(self, node: _XdrPointer) -> None:
+        """Emit one maxsize macro for an XDR pointer type"""
+        emit_pointer_maxsize(self.environment, node)
tools/net/sunrpc/xdrgen/generators/struct.py +17 -1
index 6dd7f4d7cd53..64911de46f62 100644
@@ -12,7 +12,7 @@ from xdr_ast import _XdrBasic, _XdrString
 from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
 from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
 from xdr_ast import _XdrOptionalData, _XdrStruct, _XdrDeclaration
-from xdr_ast import public_apis
+from xdr_ast import public_apis, get_header_name
 
 
 def emit_struct_declaration(environment: Environment, node: _XdrStruct) -> None:
@@ -247,6 +247,18 @@ def emit_struct_encoder(environment: Environment, node: _XdrStruct) -> None:
     print(template.render())
 
 
+def emit_struct_maxsize(environment: Environment, node: _XdrStruct) -> None:
+    """Emit one maxsize macro for an XDR struct type"""
+    macro_name = get_header_name().upper() + "_" + node.name + "_sz"
+    template = get_jinja2_template(environment, "maxsize", "struct")
+    print(
+        template.render(
+            macro=macro_name,
+            width=" + ".join(node.symbolic_width()),
+        )
+    )
+
+
 class XdrStructGenerator(SourceGenerator):
     """Generate source code for XDR structs"""
 
@@ -270,3 +282,7 @@ class XdrStructGenerator(SourceGenerator):
     def emit_encoder(self, node: _XdrStruct) -> None:
         """Emit one encoder function for an XDR struct type"""
         emit_struct_encoder(self.environment, node)
+
+    def emit_maxsize(self, node: _XdrStruct) -> None:
+        """Emit one maxsize macro for an XDR struct type"""
+        emit_struct_maxsize(self.environment, node)
tools/net/sunrpc/xdrgen/generators/typedef.py +17 -1
index 6ea98445f5c8..fab72e9d6915 100644
@@ -12,7 +12,7 @@ from xdr_ast import _XdrBasic, _XdrTypedef, _XdrString
 from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
 from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
 from xdr_ast import _XdrOptionalData, _XdrVoid, _XdrDeclaration
-from xdr_ast import public_apis
+from xdr_ast import public_apis, get_header_name
 
 
 def emit_typedef_declaration(environment: Environment, node: _XdrDeclaration) -> None:
@@ -230,6 +230,18 @@ def emit_typedef_encoder(environment: Environment, node: _XdrDeclaration) -> Non
         raise NotImplementedError("typedef: type not recognized")
 
 
+def emit_typedef_maxsize(environment: Environment, node: _XdrDeclaration) -> None:
+    """Emit a maxsize macro for an XDR typedef"""
+    macro_name = get_header_name().upper() + "_" + node.name + "_sz"
+    template = get_jinja2_template(environment, "maxsize", node.template)
+    print(
+        template.render(
+            macro=macro_name,
+            width=" + ".join(node.symbolic_width()),
+        )
+    )
+
+
 class XdrTypedefGenerator(SourceGenerator):
     """Generate source code for XDR typedefs"""
 
@@ -253,3 +265,7 @@ class XdrTypedefGenerator(SourceGenerator):
     def emit_encoder(self, node: _XdrTypedef) -> None:
         """Emit one encoder function for an XDR typedef"""
         emit_typedef_encoder(self.environment, node.declaration)
+
+    def emit_maxsize(self, node: _XdrTypedef) -> None:
+        """Emit one maxsize macro for an XDR typedef"""
+        emit_typedef_maxsize(self.environment, node.declaration)
tools/net/sunrpc/xdrgen/generators/union.py +18 -2
index 4522a5b7a943..2cca00e279cd 100644
@@ -8,8 +8,8 @@ from jinja2 import Environment
 from generators import SourceGenerator
 from generators import create_jinja2_environment, get_jinja2_template
 
-from xdr_ast import _XdrBasic, _XdrUnion, _XdrVoid, big_endian
-from xdr_ast import _XdrDeclaration, _XdrCaseSpec, public_apis
+from xdr_ast import _XdrBasic, _XdrUnion, _XdrVoid, get_header_name
+from xdr_ast import _XdrDeclaration, _XdrCaseSpec, public_apis, big_endian
 
 
 def emit_union_declaration(environment: Environment, node: _XdrUnion) -> None:
@@ -234,6 +234,18 @@ def emit_union_encoder(environment, node: _XdrUnion) -> None:
     print(template.render())
 
 
+def emit_union_maxsize(environment: Environment, node: _XdrUnion) -> None:
+    """Emit one maxsize macro for an XDR union type"""
+    macro_name = get_header_name().upper() + "_" + node.name + "_sz"
+    template = get_jinja2_template(environment, "maxsize", "union")
+    print(
+        template.render(
+            macro=macro_name,
+            width=" + ".join(node.symbolic_width()),
+        )
+    )
+
+
 class XdrUnionGenerator(SourceGenerator):
     """Generate source code for XDR unions"""
 
@@ -257,3 +269,7 @@ class XdrUnionGenerator(SourceGenerator):
     def emit_encoder(self, node: _XdrUnion) -> None:
         """Emit one encoder function for an XDR union"""
         emit_union_encoder(self.environment, node)
+
+    def emit_maxsize(self, node: _XdrUnion) -> None:
+        """Emit one maxsize macro for an XDR union"""
+        emit_union_maxsize(self.environment, node)
tools/net/sunrpc/xdrgen/templates/C/enum/maxsize/enum.j2 new file +2 -0
new file mode 100644
index 000000000000..45c1d4c21b22
@@ -0,0 +1,2 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} ({{ width }})
tools/net/sunrpc/xdrgen/templates/C/pointer/maxsize/pointer.j2 new file +3 -0
new file mode 100644
index 000000000000..9f3bfb47d2f4
@@ -0,0 +1,3 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} \
+	({{ width }})
tools/net/sunrpc/xdrgen/templates/C/struct/maxsize/struct.j2 new file +3 -0
new file mode 100644
index 000000000000..9f3bfb47d2f4
@@ -0,0 +1,3 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} \
+	({{ width }})
tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/basic.j2 new file +3 -0
new file mode 100644
index 000000000000..9f3bfb47d2f4
@@ -0,0 +1,3 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} \
+	({{ width }})
tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/fixed_length_opaque.j2 new file +2 -0
new file mode 100644
index 000000000000..45c1d4c21b22
@@ -0,0 +1,2 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} ({{ width }})
tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/string.j2 new file +2 -0
new file mode 100644
index 000000000000..45c1d4c21b22
@@ -0,0 +1,2 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} ({{ width }})
tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/variable_length_array.j2 new file +2 -0
new file mode 100644
index 000000000000..45c1d4c21b22
@@ -0,0 +1,2 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} ({{ width }})
tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/variable_length_opaque.j2 new file +2 -0
new file mode 100644
index 000000000000..45c1d4c21b22
@@ -0,0 +1,2 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} ({{ width }})
tools/net/sunrpc/xdrgen/templates/C/union/maxsize/union.j2 new file +3 -0
new file mode 100644
index 000000000000..9f3bfb47d2f4
@@ -0,0 +1,3 @@
+{# SPDX-License-Identifier: GPL-2.0 #}
+#define {{ '{:<31}'.format(macro) }} \
+	({{ width }})
Signature · 1 line
2.46.2
author maintainer
#16 lore raw

PATCH v2 16/16 xdrgen: emit maxsize macros

In reply to [email protected]

From: Chuck Lever <[email protected]>

Add "definitions" subcommand logic to emit maxsize macros in generated code.

---
 .../net/sunrpc/xdrgen/subcmds/definitions.py  | 24 ++++++++++++++++---
 tools/net/sunrpc/xdrgen/subcmds/source.py     |  3 +--
 2 files changed, 22 insertions(+), 5 deletions(-)
tools/net/sunrpc/xdrgen/subcmds/definitions.py +21 -3
index 5cd13d53221f..c956e27f37c0 100644
@@ -28,9 +28,7 @@ from xdr_parse import xdr_parser, set_xdr_annotate
 logger.setLevel(logging.INFO)
 
 
-def emit_header_definitions(
-    root: Specification, language: str, peer: str
-) -> None:
+def emit_header_definitions(root: Specification, language: str, peer: str) -> None:
     """Emit header definitions"""
     for definition in root.definitions:
         if isinstance(definition.value, _XdrConstant):
@@ -52,6 +50,25 @@ def emit_header_definitions(
         gen.emit_definition(definition.value)
 
 
+def emit_header_maxsize(root: Specification, language: str, peer: str) -> None:
+    """Emit header maxsize macros"""
+    print("")
+    for definition in root.definitions:
+        if isinstance(definition.value, _XdrEnum):
+            gen = XdrEnumGenerator(language, peer)
+        elif isinstance(definition.value, _XdrPointer):
+            gen = XdrPointerGenerator(language, peer)
+        elif isinstance(definition.value, _XdrTypedef):
+            gen = XdrTypedefGenerator(language, peer)
+        elif isinstance(definition.value, _XdrStruct):
+            gen = XdrStructGenerator(language, peer)
+        elif isinstance(definition.value, _XdrUnion):
+            gen = XdrUnionGenerator(language, peer)
+        else:
+            continue
+        gen.emit_maxsize(definition.value)
+
+
 def handle_parse_error(e: UnexpectedInput) -> bool:
     """Simple parse error reporting, no recovery attempted"""
     print(e)
@@ -71,6 +88,7 @@ def subcmd(args: Namespace) -> int:
         gen.emit_definition(args.filename, ast)
 
         emit_header_definitions(ast, args.language, args.peer)
+        emit_header_maxsize(ast, args.language, args.peer)
 
         gen = XdrHeaderBottomGenerator(args.language, args.peer)
         gen.emit_definition(args.filename, ast)
tools/net/sunrpc/xdrgen/subcmds/source.py +1 -2
index 00c04ad15b89..2024954748f0 100644
@@ -83,8 +83,7 @@ def generate_client_source(filename: str, root: Specification, language: str) ->
     gen = XdrSourceTopGenerator(language, "client")
     gen.emit_source(filename, root)
 
-    # cel: todo: client needs XDR size macros
-
+    print("")
     for definition in root.definitions:
         emit_source_encoder(definition.value, language, "client")
     for definition in root.definitions:
Signature · 1 line
2.46.2