[ovs-dev] [PATCH v1 05/18] build-aux: generate ofp field decoders

Adrian Moreno amorenoz at redhat.com
Mon Nov 22 11:22:43 UTC 2021


Based on meta-field information extracted by extract_ofp_fields,
autogenerate the right decoder to be used

Signed-off-by: Adrian Moreno <amorenoz at redhat.com>
---
 build-aux/automake.mk            |  3 +-
 build-aux/gen_ofp_field_decoders | 73 ++++++++++++++++++++++++++++++++
 python/.gitignore                |  1 +
 python/automake.mk               |  7 +++
 4 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100755 build-aux/gen_ofp_field_decoders

diff --git a/build-aux/automake.mk b/build-aux/automake.mk
index 6267ccd7c..a8bb0acfd 100644
--- a/build-aux/automake.mk
+++ b/build-aux/automake.mk
@@ -9,7 +9,8 @@ EXTRA_DIST += \
 	build-aux/sodepends.py \
 	build-aux/soexpand.py \
 	build-aux/text2c \
-	build-aux/xml2nroff
+	build-aux/xml2nroff \
+	build-aux/gen_ofp_field_decoders
 
 FLAKE8_PYFILES += \
     $(srcdir)/build-aux/xml2nroff \
diff --git a/build-aux/gen_ofp_field_decoders b/build-aux/gen_ofp_field_decoders
new file mode 100755
index 000000000..e60410af8
--- /dev/null
+++ b/build-aux/gen_ofp_field_decoders
@@ -0,0 +1,73 @@
+#!/bin/env python
+
+import argparse
+import re
+import os
+import sys
+import importlib
+
+import build.extract_ofp_fields as extract_fields
+
+
+def main():
+    parser = argparse.ArgumentParser(
+        description="Tool to generate python ofproto field decoders from"
+        "meta-flow information"
+    )
+    parser.add_argument(
+        "metaflow",
+        metavar="FILE",
+        type=str,
+        help="Read meta-flow info from file",
+    )
+
+    args = parser.parse_args()
+
+    fields = extract_fields.extract_ofp_fields(args.metaflow)
+
+    field_decoders = {}
+    for field in fields:
+        decoder = get_decoder(field)
+        field_decoders[field.get("name")] = decoder
+        if field.get("extra_name"):
+            field_decoders[field.get("extra_name")] = decoder
+
+    code = """
+# This file is auto-generated. Do not edit
+
+import functools
+from ovs.flows import decoders
+
+field_decoders = {{
+{decoders}
+}}
+""".format(
+        decoders="\n".join(
+            [
+                "    '{name}': {decoder},".format(name=name, decoder=decoder)
+                for name, decoder in field_decoders.items()
+            ]
+        )
+    )
+    print(code)
+
+
+def get_decoder(field):
+    formatting = field.get("formatting")
+    if formatting in ["decimal", "hexadecimal"]:
+        if field.get("mask") == "MFM_NONE":
+            return "decoders.decode_int"
+        else:
+            if field.get("n_bits") in [8, 16, 32, 64, 128, 992]:
+                return "decoders.Mask{}".format(field.get("n_bits"))
+            return "decoders.decode_mask({})".format(field.get("n_bits"))
+    elif formatting in ["IPv4", "IPv6"]:
+        return "decoders.IPMask"
+    elif formatting == "Ethernet":
+        return "decoders.EthMask"
+    else:
+        return "decoders.decode_default"
+
+
+if __name__ == "__main__":
+    main()
diff --git a/python/.gitignore b/python/.gitignore
index 60ace6f05..c8ffd4574 100644
--- a/python/.gitignore
+++ b/python/.gitignore
@@ -1,2 +1,3 @@
 dist/
 *.egg-info
+ovs/flows/ofp_fields.py
diff --git a/python/automake.mk b/python/automake.mk
index b869eb355..9dfc62fce 100644
--- a/python/automake.mk
+++ b/python/automake.mk
@@ -123,3 +123,10 @@ $(srcdir)/python/ovs/dirs.py: python/ovs/dirs.py.template
 	mv $@.tmp $@
 EXTRA_DIST += python/ovs/dirs.py.template
 CLEANFILES += python/ovs/dirs.py
+
+$(srcdir)/python/ovs/flows/ofp_fields.py: $(srcdir)/build-aux/gen_ofp_field_decoders include/openvswitch/meta-flow.h
+	$(AM_V_GEN)$(run_python) $< $(srcdir)/include/openvswitch/meta-flow.h > $@.tmp
+	$(AM_V_at)mv $@.tmp $@
+EXTRA_DIST += python/ovs/flows/ofp_fields.py
+CLEANFILES += python/ovs/flows/ofp_fields.py
+
-- 
2.31.1



More information about the dev mailing list