xdrgen: Emit maxsize macros
Subsystems (3)
[email protected]
maintainercover 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.
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
[email protected]
maintainerPATCH 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
@@ -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
[email protected]
maintainerPATCH 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
@@ -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
[email protected]
maintainerPATCH 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
@@ -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
[email protected]
maintainerPATCH 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
@@ -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
[email protected]
maintainerPATCH 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
@@ -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
[email protected]
maintainerPATCH 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
@@ -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
[email protected]
maintainerPATCH 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
@@ -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
[email protected]
maintainerPATCH 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
@@ -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
[email protected]
maintainerPATCH 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
@@ -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