9 # Maps from user-friendly version number to its protocol encoding.
10 VERSION = {"1.0": 0x01,
17 TYPES = {"u8": (1, False),
24 FORMATTING = {"decimal": ("MFS_DECIMAL", 1, 8),
25 "hexadecimal": ("MFS_HEXADECIMAL", 1, 127),
26 "Ethernet": ("MFS_ETHERNET", 6, 6),
27 "IPv4": ("MFS_IPV4", 4, 4),
28 "IPv6": ("MFS_IPV6", 16, 16),
29 "OpenFlow 1.0 port": ("MFS_OFP_PORT", 2, 2),
30 "OpenFlow 1.1+ port": ("MFS_OFP_PORT_OXM", 4, 4),
31 "frag": ("MFS_FRAG", 1, 1),
32 "tunnel flags": ("MFS_TNL_FLAGS", 2, 2),
33 "TCP flags": ("MFS_TCP_FLAGS", 2, 2)}
35 PREREQS = {"none": "MFP_NONE",
37 "VLAN VID": "MFP_VLAN_VID",
40 "IPv4/IPv6": "MFP_IP_ANY",
45 "ICMPv4": "MFP_ICMPV4",
46 "ICMPv6": "MFP_ICMPV6",
48 "ND solicit": "MFP_ND_SOLICIT",
49 "ND advert": "MFP_ND_ADVERT"}
51 # Maps a name prefix into an (experimenter ID, class) pair, so:
53 # - Standard OXM classes are written as (0, <oxm_class>)
55 # - Experimenter OXM classes are written as (<oxm_vender>, 0xffff)
57 # If a name matches more than one prefix, the longest one is used.
58 OXM_CLASSES = {"NXM_OF_": (0, 0x0000),
59 "NXM_NX_": (0, 0x0001),
60 "OXM_OF_": (0, 0x8000),
61 "OXM_OF_PKT_REG": (0, 0x8001),
62 "ONFOXM_ET_": (0x4f4e4600, 0xffff),
64 # This is the experimenter OXM class for Nicira, which is the
65 # one that OVS would be using instead of NXM_OF_ and NXM_NX_
66 # if OVS didn't have those grandfathered in. It is currently
67 # used only to test support for experimenter OXM, since there
68 # are barely any real uses of experimenter OXM in the wild.
69 "NXOXM_ET_": (0x00002320, 0xffff)}
72 def oxm_name_to_class(name):
75 for p, c in OXM_CLASSES.items():
76 if name.startswith(p) and len(p) > len(prefix):
82 def decode_version_range(range):
84 return (VERSION[range], VERSION[range])
85 elif range.endswith('+'):
86 return (VERSION[range[:-1]], max(VERSION.values()))
88 a, b = re.match(r'^([^-]+)-([^-]+)$', range).groups()
89 return (VERSION[a], VERSION[b])
95 line = input_file.readline()
98 fatal("unexpected end of input")
106 sys.stderr.write("%s:%d: %s\n" % (file_name, line_number, msg))
116 argv0 = os.path.basename(sys.argv[0])
118 %(argv0)s, for extracting OpenFlow field properties from meta-flow.h
119 usage: %(argv0)s INPUT [--meta-flow | --nx-match]
120 where INPUT points to lib/meta-flow.h in the source directory.
121 Depending on the option given, the output written to stdout is intended to be
122 saved either as lib/meta-flow.inc or lib/nx-match.inc for the respective C
124 ''' % {"argv0": argv0})
129 m = re.match(r'(.*) up to (.*)', s)
131 struct, member = m.groups()
132 return "offsetof(%s, %s)" % (struct, member)
134 return "sizeof(%s)" % s
137 def parse_oxms(s, prefix, n_bytes):
141 return tuple(parse_oxm(s2.strip(), prefix, n_bytes) for s2 in s.split(','))
147 def parse_oxm(s, prefix, n_bytes):
150 m = re.match('([A-Z0-9_]+)\(([0-9]+)\) since(?: OF(1\.[0-9]+) and)? v([12]\.[0-9]+)$', s)
152 fatal("%s: syntax error parsing %s" % (s, prefix))
154 name, oxm_type, of_version, ovs_version = m.groups()
156 class_ = oxm_name_to_class(name)
158 fatal("unknown OXM class for %s" % name)
159 oxm_vendor, oxm_class = class_
161 if class_ in match_types:
162 if oxm_type in match_types[class_]:
163 fatal("duplicate match type for %s (conflicts with %s)" %
164 (name, match_types[class_][oxm_type]))
166 match_types[class_] = dict()
167 match_types[class_][oxm_type] = name
169 # Normally the oxm_length is the size of the field, but for experimenter
170 # OXMs oxm_length also includes the 4-byte experimenter ID.
172 if oxm_class == 0xffff:
175 header = ("NXM_HEADER(0x%x,0x%x,%s,0,%d)"
176 % (oxm_vendor, oxm_class, oxm_type, oxm_length))
179 if of_version not in VERSION:
180 fatal("%s: unknown OpenFlow version %s" % (name, of_version))
181 of_version_nr = VERSION[of_version]
182 if of_version_nr < VERSION['1.2']:
183 fatal("%s: claimed version %s predates OXM" % (name, of_version))
187 return (header, name, of_version_nr, ovs_version)
190 def parse_field(mff, comment):
193 # First line of comment is the field name.
194 m = re.match(r'"([^"]+)"(?:\s+\(aka "([^"]+)"\))?(?:\s+\(.*\))?\.', comment[0])
196 fatal("%s lacks field name" % mff)
197 f['name'], f['extra_name'] = m.groups()
199 # Find the last blank line the comment. The field definitions
202 for i in range(len(comment)):
206 fatal("%s: missing blank line in comment" % mff)
209 for key in ("Type", "Maskable", "Formatting", "Prerequisites",
210 "Access", "Prefix lookup member",
211 "OXM", "NXM", "OF1.0", "OF1.1"):
213 for fline in comment[blank + 1:]:
214 m = re.match(r'([^:]+):\s+(.*)\.$', fline)
216 fatal("%s: syntax error parsing key-value pair as part of %s"
218 key, value = m.groups()
220 fatal("%s: unknown key" % key)
221 elif key == 'Code point':
223 elif d[key] is not None:
224 fatal("%s: duplicate key" % key)
226 for key, value in d.items():
227 if not value and key not in ("OF1.0", "OF1.1",
228 "Prefix lookup member", "Notes"):
229 fatal("%s: missing %s" % (mff, key))
231 m = re.match(r'([a-zA-Z0-9]+)(?: \(low ([0-9]+) bits\))?$', d['Type'])
233 fatal("%s: syntax error in type" % mff)
235 if type_ not in TYPES:
236 fatal("%s: unknown type %s" % (mff, d['Type']))
238 f['n_bytes'] = TYPES[type_][0]
240 f['n_bits'] = int(m.group(2))
241 if f['n_bits'] > f['n_bytes'] * 8:
242 fatal("%s: more bits (%d) than field size (%d)"
243 % (mff, f['n_bits'], 8 * f['n_bytes']))
245 f['n_bits'] = 8 * f['n_bytes']
246 f['variable'] = TYPES[type_][1]
248 if d['Maskable'] == 'no':
249 f['mask'] = 'MFM_NONE'
250 elif d['Maskable'] == 'bitwise':
251 f['mask'] = 'MFM_FULLY'
253 fatal("%s: unknown maskable %s" % (mff, d['Maskable']))
255 fmt = FORMATTING.get(d['Formatting'])
257 fatal("%s: unknown format %s" % (mff, d['Formatting']))
258 if f['n_bytes'] < fmt[1] or f['n_bytes'] > fmt[2]:
259 fatal("%s: %d-byte field can't be formatted as %s"
260 % (mff, f['n_bytes'], d['Formatting']))
263 f['prereqs'] = PREREQS.get(d['Prerequisites'])
265 fatal("%s: unknown prerequisites %s" % (mff, d['Prerequisites']))
267 if d['Access'] == 'read-only':
268 f['writable'] = False
269 elif d['Access'] == 'read/write':
272 fatal("%s: unknown access %s" % (mff, d['Access']))
274 f['OF1.0'] = d['OF1.0']
275 if not d['OF1.0'] in (None, 'exact match', 'CIDR mask'):
276 fatal("%s: unknown OF1.0 match type %s" % (mff, d['OF1.0']))
278 f['OF1.1'] = d['OF1.1']
279 if not d['OF1.1'] in (None, 'exact match', 'bitwise mask'):
280 fatal("%s: unknown OF1.1 match type %s" % (mff, d['OF1.1']))
282 f['OXM'] = (parse_oxms(d['OXM'], 'OXM', f['n_bytes']) +
283 parse_oxms(d['NXM'], 'NXM', f['n_bytes']))
285 f['prefix'] = d["Prefix lookup member"]
290 def protocols_to_c(protocols):
291 if protocols == set(['of10', 'of11', 'oxm']):
292 return 'OFPUTIL_P_ANY'
293 elif protocols == set(['of11', 'oxm']):
294 return 'OFPUTIL_P_NXM_OF11_UP'
295 elif protocols == set(['oxm']):
296 return 'OFPUTIL_P_NXM_OXM_ANY'
297 elif protocols == set([]):
298 return 'OFPUTIL_P_NONE'
303 def make_meta_flow(fields):
307 output += [" %s," % f['mff']]
309 output += [" \"%s\", \"%s\"," % (f['name'], f['extra_name'])]
311 output += [" \"%s\", NULL," % f['name']]
317 output += [" %d, %d, %s," % (f['n_bytes'], f['n_bits'], variable)]
323 output += [" %s, %s, %s, %s,"
324 % (f['mask'], f['string'], f['prereqs'], rw)]
329 if f['mff'] in ('MFF_DL_VLAN', 'MFF_DL_VLAN_PCP'):
330 # MFF_DL_VLAN and MFF_DL_VLAN_PCP don't exactly correspond to
331 # OF1.1, nor do they have NXM or OXM assignments, but their
332 # meanings can be expressed in every protocol, which is the goal of
334 protocols = set(["of10", "of11", "oxm"])
338 protocols |= set(["of10"])
340 protocols |= set(["of11"])
342 protocols |= set(["oxm"])
344 if f['mask'] == 'MFM_FULLY':
345 cidr_protocols = protocols.copy()
346 bitwise_protocols = protocols.copy()
348 if of10 == 'exact match':
349 bitwise_protocols -= set(['of10'])
350 cidr_protocols -= set(['of10'])
351 elif of10 == 'CIDR mask':
352 bitwise_protocols -= set(['of10'])
356 if of11 == 'exact match':
357 bitwise_protocols -= set(['of11'])
358 cidr_protocols -= set(['of11'])
360 assert of11 in (None, 'bitwise mask')
362 assert f['mask'] == 'MFM_NONE'
363 cidr_protocols = set([])
364 bitwise_protocols = set([])
366 output += [" %s," % protocols_to_c(protocols)]
367 output += [" %s," % protocols_to_c(cidr_protocols)]
368 output += [" %s," % protocols_to_c(bitwise_protocols)]
371 output += [" FLOW_U32OFS(%s)," % f['prefix']]
373 output += [" -1, /* not usable for prefix lookup */"]
379 def make_nx_match(fields):
381 print("static struct nxm_field_index all_nxm_fields[] = {")
383 # Sort by OpenFlow version number (nx-match.c depends on this).
384 for oxm in sorted(f['OXM'], key=lambda x: x[2]):
385 print("""{ .nf = { %s, %d, "%s", %s } },""" % (
386 oxm[0], oxm[2], oxm[1], f['mff']))
391 def extract_ofp_fields(mode):
398 if re.match('enum.*mf_field_id', line):
403 first_line_number = line_number
404 here = '%s:%d' % (file_name, line_number)
405 if (line.startswith('/*')
406 or line.startswith(' *')
407 or line.startswith('#')
411 elif re.match('}', line) or re.match('\s+MFF_N_IDS', line):
414 # Parse the comment preceding an MFF_ constant into 'comment',
415 # one line to an array element.
417 if not line.startswith('/*'):
418 fatal("unexpected syntax between fields")
424 if line.startswith('*/'):
427 if not line.startswith('*'):
428 fatal("unexpected syntax within field")
431 if line.startswith(' '):
433 if line.startswith(' ') and comment:
439 if line.endswith('*/'):
440 line = line[:-2].rstrip()
446 comment[-1] += " " + line
451 # Drop blank lines at each end of comment.
452 while comment and not comment[0]:
453 comment = comment[1:]
454 while comment and not comment[-1]:
455 comment = comment[:-1]
457 # Parse the MFF_ constant(s).
460 m = re.match('\s+(MFF_[A-Z0-9_]+),?\s?$', line)
466 fatal("unexpected syntax looking for MFF_ constants")
468 if len(mffs) > 1 or '<N>' in comment[0]:
470 # Extract trailing integer.
471 m = re.match('.*[^0-9]([0-9]+)$', mff)
473 fatal("%s lacks numeric suffix in register group" % mff)
476 # Search-and-replace <N> within the comment,
477 # and drop lines that have <x> for x != n.
480 y = x.replace('<N>', n)
481 if re.search('<[0-9]+>', y):
482 if ('<%s>' % n) not in y:
484 y = re.sub('<[0-9]+>', '', y)
485 instance += [y.strip()]
486 fields += [parse_field(mff, instance)]
488 fields += [parse_field(mffs[0], comment)]
497 /* Generated automatically; do not modify! "-*- buffer-read-only: t -*- */
500 if mode == '--meta-flow':
501 output = make_meta_flow(fields)
502 elif mode == '--nx-match':
503 output = make_nx_match(fields)
510 if __name__ == '__main__':
511 if '--help' in sys.argv:
513 elif len(sys.argv) != 3:
514 sys.stderr.write("exactly two arguments required; "
515 "use --help for help\n")
517 elif sys.argv[2] in ('--meta-flow', '--nx-match'):
521 file_name = sys.argv[1]
522 input_file = open(file_name)
525 for oline in extract_ofp_fields(sys.argv[2]):
528 sys.stderr.write("invalid arguments; use --help for help\n")