forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_ofp_field_decoders
executable file
·82 lines (65 loc) · 2.02 KB
/
gen_ofp_field_decoders
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/env python
import argparse
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 = {}
aliases = {}
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
for nxm in field.get("OXM", []):
aliases[nxm[1]] = field.get("name")
code = """
# This file is auto-generated. Do not edit!
from ovs.flow import decoders
field_decoders = {{
{decoders}
}}
field_aliases = {{
{aliases}
}}""".format(
decoders="\n".join(
[
" '{name}': {decoder},".format(name=name, decoder=decoder)
for name, decoder in field_decoders.items()
]
),
aliases="\n".join(
[
" '{alias}': '{name}',".format(name=name, alias=alias)
for alias, name in aliases.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()