]>
git.proxmox.com Git - ovs.git/blob - build-aux/extract-ofp-actions
9 # Map from OpenFlow version number to version ID used in ofp_header.
10 version_map
= {"1.0": 0x01,
16 version_reverse_map
= dict((v
, k
) for (k
, v
) in version_map
.iteritems())
18 # Map from vendor name to the length of the action header.
19 vendor_map
= {"OF": (0x00000000, 4),
20 "ONF": (0x4f4e4600, 10),
21 "NX": (0x00002320, 10)}
23 # Basic types used in action arguments.
25 types
['uint8_t'] = {"size": 1, "align": 1, "ntoh": None, "hton": None}
26 types
['ovs_be16'] = {"size": 2, "align": 2, "ntoh": "ntohs", "hton": "htons"}
27 types
['ovs_be32'] = {"size": 4, "align": 4, "ntoh": "ntohl", "hton": "htonl"}
28 types
['ovs_be64'] = {"size": 8, "align": 8, "ntoh": "ntohll", "hton": "htonll"}
29 types
['uint16_t'] = {"size": 2, "align": 2, "ntoh": None, "hton": None}
30 types
['uint32_t'] = {"size": 4, "align": 4, "ntoh": None, "hton": None}
31 types
['uint64_t'] = {"size": 8, "align": 8, "ntoh": None, "hton": None}
38 return (x
+ (y
- 1)) / y
* y
45 input_file
= open(file_name
)
52 line
= input_file
.readline()
55 fatal("unexpected end of input")
61 sys
.stderr
.write("%s:%d: %s\n" % (file_name
, line_number
, msg
))
69 argv0
= os
.path
.basename(sys
.argv
[0])
71 %(argv0)s, for extracting OpenFlow action data
72 usage: %(argv0)s OFP_ACTIONS.C [--prototypes | --definitions]
74 This program reads ofp-actions.c to obtain information about OpenFlow
75 actions. With --prototypes, it outputs on stdout a set of prototypes to
76 #include early in ofp-actions.c. With --definitions, it outputs on stdout
77 a set of definitions to #include late in ofp-actions.c
79 OFP_ACTIONS.C should point to lib/ofp-actions.c.\
80 ''' % {"argv0": argv0
})
83 def extract_ofp_actions(fn
, definitions
):
89 for code
, size
in vendor_map
.values():
99 if re
.match('enum ofp_raw_action_type {', line
):
104 if line
.startswith('/*') or not line
or line
.isspace():
106 elif re
.match('}', line
):
109 if not line
.lstrip().startswith('/*'):
110 fatal("unexpected syntax between actions")
112 comment
= line
.lstrip()[2:].strip()
113 while not comment
.endswith('*/'):
115 if line
.startswith('/*') or not line
or line
.isspace():
116 fatal("unexpected syntax within action")
117 comment
+= ' %s' % line
.lstrip('* \t').rstrip(' \t\r\n')
118 comment
= re
.sub('\[[^]]*\]', '', comment
)
119 comment
= comment
[:-2].rstrip()
121 m
= re
.match('([^:]+):\s+(.*)$', comment
)
123 fatal("unexpected syntax between actions")
126 argtype
= m
.group(2).strip().replace('.', '', 1)
129 m
= re
.match(r
'\s+(([A-Z]+)_RAW([0-9]*)_([A-Z0-9_]+)),?', line
)
131 fatal("syntax error expecting enum value")
135 fatal("%s specified twice" % enum
)
139 for dst
in dsts
.split(', '):
140 m
= re
.match(r
'([A-Z]+)([0-9.]+)(\+|-[0-9.]+)?(?:\((\d+)\))(?: is deprecated \(([^)]+)\))?$', dst
)
142 fatal("%r: syntax error in destination" % dst
)
143 vendor_name
= m
.group(1)
144 version1_name
= m
.group(2)
145 version2_name
= m
.group(3)
146 type_
= int(m
.group(4))
147 deprecation
= m
.group(5)
149 if vendor_name
not in vendor_map
:
150 fatal("%s: unknown vendor" % vendor_name
)
151 vendor
= vendor_map
[vendor_name
][0]
153 if version1_name
not in version_map
:
154 fatal("%s: unknown OpenFlow version" % version1_name
)
155 v1
= version_map
[version1_name
]
157 if version2_name
is None:
159 elif version2_name
== "+":
160 v2
= max(version_map
.values())
161 elif version2_name
[1:] not in version_map
:
162 fatal("%s: unknown OpenFlow version" % version2_name
[1:])
164 v2
= version_map
[version2_name
[1:]]
167 fatal("%s%s: %s precedes %s"
168 % (version1_name
, version2_name
,
169 version2_name
, version1_name
))
171 for version
in range(v1
, v2
+ 1):
172 domain
[vendor
].setdefault(type_
, {})
173 if version
in domain
[vendor
][type_
]:
174 v
= domain
[vendor
][type_
][version
]
175 msg
= "%#x,%d in OF%s means both %s and %s" % (
176 vendor
, type_
, version_reverse_map
[version
],
178 error("%s: %s." % (dst
, msg
))
179 sys
.stderr
.write("%s:%d: %s: Here is the location "
180 "of the previous definition.\n"
181 % (v
["file_name"], v
["line_number"],
185 header_len
= vendor_map
[vendor_name
][1]
187 base_argtype
= argtype
.replace(', ..', '', 1)
188 if base_argtype
in types
:
189 arg_align
= types
[base_argtype
]['align']
190 arg_len
= types
[base_argtype
]['size']
191 arg_ofs
= round_up(header_len
, arg_align
)
192 min_length
= round_up(arg_ofs
+ arg_len
,
194 elif base_argtype
== 'void':
195 min_length
= round_up(header_len
, OFP_ACTION_ALIGN
)
198 elif re
.match(r
'struct [a-zA-Z0-9_]+$', base_argtype
):
199 min_length
= 'sizeof(%s)' % base_argtype
200 arg_structs
.add(base_argtype
)
203 # should also emit OFP_ACTION_ALIGN assertion
205 fatal("bad argument type %s" % argtype
)
207 ellipsis
= argtype
!= base_argtype
209 max_length
= '65536 - OFP_ACTION_ALIGN'
211 max_length
= min_length
213 info
= {"enum": enum
, # 0
214 "deprecation": deprecation
, # 1
215 "file_name": file_name
, # 2
216 "line_number": line_number
, # 3
217 "min_length": min_length
, # 4
218 "max_length": max_length
, # 5
219 "arg_ofs": arg_ofs
, # 6
220 "arg_len": arg_len
, # 7
221 "base_argtype": base_argtype
, # 8
222 "version": version
, # 9
224 domain
[vendor
][type_
][version
] = info
226 enums
.setdefault(enum
, [])
227 enums
[enum
].append(info
)
235 /* Generated automatically; do not modify! -*- buffer-read-only: t -*- */
239 print "/* Verify that structs used as actions are reasonable sizes. */"
240 for s
in sorted(arg_structs
):
241 print "BUILD_ASSERT_DECL(sizeof(%s) %% OFP_ACTION_ALIGN == 0);" % s
243 print "\nstatic struct ofpact_raw_instance all_raw_instances[] = {"
244 for vendor
in domain
:
245 for type_
in domain
[vendor
]:
246 for version
in domain
[vendor
][type_
]:
247 d
= domain
[vendor
][type_
][version
]
248 print " { { 0x%08x, %2d, 0x%02x }, " % (
249 vendor
, type_
, version
)
250 print " %s," % d
["enum"]
251 print " HMAP_NODE_NULL_INITIALIZER,"
252 print " HMAP_NODE_NULL_INITIALIZER,"
253 print " %s," % d
["min_length"]
254 print " %s," % d
["max_length"]
255 print " %s," % d
["arg_ofs"]
256 print " %s," % d
["arg_len"]
257 print " \"%s\"," % re
.sub('_RAW[0-9]*', '', d
["enum"], 1)
259 print " \"%s\"," % re
.sub(r
'(["\\])', r
'\\\1', d
["deprecation"])
265 for versions
in enums
.values():
266 need_ofp_version
= False
268 assert v
["arg_len"] == versions
[0]["arg_len"]
269 assert v
["base_argtype"] == versions
[0]["base_argtype"]
270 if (v
["min_length"] != versions
[0]["min_length"] or
271 v
["arg_ofs"] != versions
[0]["arg_ofs"] or
272 v
["type"] != versions
[0]["type"]):
273 need_ofp_version
= True
274 base_argtype
= versions
[0]["base_argtype"]
276 decl
= "static inline "
277 if base_argtype
.startswith('struct'):
278 decl
+= "%s *" %base_argtype
281 decl
+= "\nput_%s(struct ofpbuf *openflow" % versions
[0]["enum"].replace('_RAW', '', 1)
283 decl
+= ", enum ofp_version version"
284 if base_argtype
!= 'void' and not base_argtype
.startswith('struct'):
285 decl
+= ", %s arg" % base_argtype
290 if base_argtype
.startswith('struct'):
292 decl
+= "ofpact_put_raw(openflow, "
296 decl
+= "%s" % versions
[0]["version"]
297 decl
+= ", %s, " % versions
[0]["enum"]
298 if base_argtype
.startswith('struct') or base_argtype
== 'void':
301 ntoh
= types
[base_argtype
]['ntoh']
303 decl
+= "%s(arg)" % ntoh
316 ofpact_decode(const struct ofp_action_header *a, enum ofp_raw_action_type raw,
317 uint64_t arg, struct ofpbuf *out)
321 for versions
in enums
.values():
322 enum
= versions
[0]["enum"]
323 print " case %s:" % enum
324 base_argtype
= versions
[0]["base_argtype"]
325 if base_argtype
== 'void':
326 print " return decode_%s(out);" % enum
328 if base_argtype
.startswith('struct'):
329 arg
= "ALIGNED_CAST(const %s *, a)" % base_argtype
331 hton
= types
[base_argtype
]['hton']
333 arg
= "%s(arg)" % hton
336 print " return decode_%s(%s, out);" % (enum
, arg
)
345 for versions
in enums
.values():
346 enum
= versions
[0]["enum"]
347 prototype
= "static enum ofperr decode_%s(" % enum
348 base_argtype
= versions
[0]["base_argtype"]
349 if base_argtype
!= 'void':
350 if base_argtype
.startswith('struct'):
351 prototype
+= "const %s *, " % base_argtype
353 prototype
+= "%s, " % base_argtype
354 prototype
+= "struct ofpbuf *);"
358 static enum ofperr ofpact_decode(const struct ofp_action_header *,
359 enum ofp_raw_action_type raw,
360 uint64_t arg, struct ofpbuf *out);
363 if __name__
== '__main__':
364 if '--help' in sys
.argv
:
366 elif len(sys
.argv
) != 3:
367 sys
.stderr
.write("exactly two arguments required; "
368 "use --help for help\n")
370 elif sys
.argv
[2] == '--prototypes':
371 extract_ofp_actions(sys
.argv
[1], False)
372 elif sys
.argv
[2] == '--definitions':
373 extract_ofp_actions(sys
.argv
[1], True)
375 sys
.stderr
.write("invalid arguments; use --help for help\n")