9 # Maps from user-friendly version number to its protocol encoding.
10 VERSION = {"1.0": 0x01,
24 FORMATTING = {"decimal": ("MFS_DECIMAL", 1, 8),
25 "hexadecimal": ("MFS_HEXADECIMAL", 1, 8),
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),
63 # This is the experimenter OXM class for Nicira, which is the
64 # one that OVS would be using instead of NXM_OF_ and NXM_NX_
65 # if OVS didn't have those grandfathered in. It is currently
66 # used only to test support for experimenter OXM, since there
67 # are barely any real uses of experimenter OXM in the wild.
68 "NXOXM_ET_": (0x00002320, 0xffff)}
69 def oxm_name_to_class(name):
72 for p, c in OXM_CLASSES.iteritems():
73 if name.startswith(p) and len(p) > len(prefix):
78 def decode_version_range(range):
80 return (VERSION[range], VERSION[range])
81 elif range.endswith('+'):
82 return (VERSION[range[:-1]], max(VERSION.values()))
84 a, b = re.match(r'^([^-]+)-([^-]+)$', range).groups()
85 return (VERSION[a], VERSION[b])
90 line = input_file.readline()
93 fatal("unexpected end of input")
98 sys.stderr.write("%s:%d: %s\n" % (file_name, line_number, msg))
106 argv0 = os.path.basename(sys.argv[0])
108 %(argv0)s, for extracting OpenFlow field properties from meta-flow.h
109 usage: %(argv0)s INPUT [--meta-flow | --nx-match]
110 where INPUT points to lib/meta-flow.h in the source directory.
111 Depending on the option given, the output written to stdout is intended to be
112 saved either as lib/meta-flow.inc or lib/nx-match.inc for the respective C
114 ''' % {"argv0": argv0}
118 m = re.match(r'(.*) up to (.*)', s)
120 struct, member = m.groups()
121 return "offsetof(%s, %s)" % (struct, member)
123 return "sizeof(%s)" % s
125 def parse_oxms(s, prefix, n_bytes):
129 return tuple(parse_oxm(s2.strip(), prefix, n_bytes) for s2 in s.split(','))
131 def parse_oxm(s, prefix, n_bytes):
132 m = re.match('([A-Z0-9_]+)\(([0-9]+)\) since(?: OF(1\.[0-9]+) and)? v([12]\.[0-9]+)$', s)
134 fatal("%s: syntax error parsing %s" % (s, prefix))
136 name, oxm_type, of_version, ovs_version = m.groups()
138 class_ = oxm_name_to_class(name)
140 fatal("unknown OXM class for %s" % name)
141 oxm_vendor, oxm_class = class_
143 # Normally the oxm_length is the size of the field, but for experimenter
144 # OXMs oxm_length also includes the 4-byte experimenter ID.
146 if oxm_class == 0xffff:
149 header = ("NXM_HEADER(0x%x,0x%x,%s,0,%d)"
150 % (oxm_vendor, oxm_class, oxm_type, oxm_length))
153 if of_version not in VERSION:
154 fatal("%s: unknown OpenFlow version %s" % (name, of_version))
155 of_version_nr = VERSION[of_version]
156 if of_version_nr < VERSION['1.2']:
157 fatal("%s: claimed version %s predates OXM" % (name, of_version))
161 return (header, name, of_version_nr, ovs_version)
163 def parse_field(mff, comment):
166 # First line of comment is the field name.
167 m = re.match(r'"([^"]+)"(?:\s+\(aka "([^"]+)"\))?(?:\s+\(.*\))?\.', comment[0])
169 fatal("%s lacks field name" % mff)
170 f['name'], f['extra_name'] = m.groups()
172 # Find the last blank line the comment. The field definitions
175 for i in range(len(comment)):
179 fatal("%s: missing blank line in comment" % mff)
182 for key in ("Type", "Maskable", "Formatting", "Prerequisites",
183 "Access", "Prefix lookup member",
184 "OXM", "NXM", "OF1.0", "OF1.1"):
186 for fline in comment[blank + 1:]:
187 m = re.match(r'([^:]+):\s+(.*)\.$', fline)
189 fatal("%s: syntax error parsing key-value pair as part of %s"
191 key, value = m.groups()
193 fatal("%s: unknown key" % key)
194 elif key == 'Code point':
196 elif d[key] is not None:
197 fatal("%s: duplicate key" % key)
199 for key, value in d.iteritems():
200 if not value and key not in ("OF1.0", "OF1.1",
201 "Prefix lookup member", "Notes"):
202 fatal("%s: missing %s" % (mff, key))
204 m = re.match(r'([a-zA-Z0-9]+)(?: \(low ([0-9]+) bits\))?$', d['Type'])
206 fatal("%s: syntax error in type" % mff)
208 if type_ not in TYPES:
209 fatal("%s: unknown type %s" % (mff, d['Type']))
210 f['n_bytes'] = TYPES[type_]
212 f['n_bits'] = int(m.group(2))
213 if f['n_bits'] > f['n_bytes'] * 8:
214 fatal("%s: more bits (%d) than field size (%d)"
215 % (mff, f['n_bits'], 8 * f['n_bytes']))
217 f['n_bits'] = 8 * f['n_bytes']
219 if d['Maskable'] == 'no':
220 f['mask'] = 'MFM_NONE'
221 elif d['Maskable'] == 'bitwise':
222 f['mask'] = 'MFM_FULLY'
224 fatal("%s: unknown maskable %s" % (mff, d['Maskable']))
226 fmt = FORMATTING.get(d['Formatting'])
228 fatal("%s: unknown format %s" % (mff, d['Formatting']))
229 if f['n_bytes'] < fmt[1] or f['n_bytes'] > fmt[2]:
230 fatal("%s: %d-byte field can't be formatted as %s"
231 % (mff, f['n_bytes'], d['Formatting']))
234 f['prereqs'] = PREREQS.get(d['Prerequisites'])
236 fatal("%s: unknown prerequisites %s" % (mff, d['Prerequisites']))
238 if d['Access'] == 'read-only':
239 f['writable'] = False
240 elif d['Access'] == 'read/write':
243 fatal("%s: unknown access %s" % (mff, d['Access']))
245 f['OF1.0'] = d['OF1.0']
246 if not d['OF1.0'] in (None, 'exact match', 'CIDR mask'):
247 fatal("%s: unknown OF1.0 match type %s" % (mff, d['OF1.0']))
249 f['OF1.1'] = d['OF1.1']
250 if not d['OF1.1'] in (None, 'exact match', 'bitwise mask'):
251 fatal("%s: unknown OF1.1 match type %s" % (mff, d['OF1.1']))
253 f['OXM'] = (parse_oxms(d['OXM'], 'OXM', f['n_bytes']) +
254 parse_oxms(d['NXM'], 'NXM', f['n_bytes']))
256 f['prefix'] = d["Prefix lookup member"]
260 def protocols_to_c(protocols):
261 if protocols == set(['of10', 'of11', 'oxm']):
262 return 'OFPUTIL_P_ANY'
263 elif protocols == set(['of11', 'oxm']):
264 return 'OFPUTIL_P_NXM_OF11_UP'
265 elif protocols == set(['oxm']):
266 return 'OFPUTIL_P_NXM_OXM_ANY'
267 elif protocols == set([]):
268 return 'OFPUTIL_P_NONE'
272 def make_meta_flow(fields):
276 output += [" %s," % f['mff']]
278 output += [" \"%s\", \"%s\"," % (f['name'], f['extra_name'])]
280 output += [" \"%s\", NULL," % f['name']]
281 output += [" %d, %d," % (f['n_bytes'], f['n_bits'])]
287 output += [" %s, %s, %s, %s,"
288 % (f['mask'], f['string'], f['prereqs'], rw)]
293 if f['mff'] in ('MFF_DL_VLAN', 'MFF_DL_VLAN_PCP'):
294 # MFF_DL_VLAN and MFF_DL_VLAN_PCP don't exactly correspond to
295 # OF1.1, nor do they have NXM or OXM assignments, but their
296 # meanings can be expressed in every protocol, which is the goal of
298 protocols = set(["of10", "of11", "oxm"])
302 protocols |= set(["of10"])
304 protocols |= set(["of11"])
306 protocols |= set(["oxm"])
308 if f['mask'] == 'MFM_FULLY':
309 cidr_protocols = protocols.copy()
310 bitwise_protocols = protocols.copy()
312 if of10 == 'exact match':
313 bitwise_protocols -= set(['of10'])
314 cidr_protocols -= set(['of10'])
315 elif of10 == 'CIDR mask':
316 bitwise_protocols -= set(['of10'])
320 if of11 == 'exact match':
321 bitwise_protocols -= set(['of11'])
322 cidr_protocols -= set(['of11'])
324 assert of11 in (None, 'bitwise mask')
326 assert f['mask'] == 'MFM_NONE'
327 cidr_protocols = set([])
328 bitwise_protocols = set([])
330 output += [" %s," % protocols_to_c(protocols)]
331 output += [" %s," % protocols_to_c(cidr_protocols)]
332 output += [" %s," % protocols_to_c(bitwise_protocols)]
335 output += [" FLOW_U32OFS(%s)," % f['prefix']]
337 output += [" -1, /* not usable for prefix lookup */"]
342 def make_nx_match(fields):
344 print "static struct nxm_field_index all_nxm_fields[] = {";
346 # Sort by OpenFlow version number (nx-match.c depends on this).
347 for oxm in sorted(f['OXM'], key=lambda x: x[2]):
348 print """{ .nf = { %s, %d, "%s", %s } },""" % (
349 oxm[0], oxm[2], oxm[1], f['mff'])
353 def extract_ofp_fields(mode):
360 if re.match('enum.*mf_field_id', line):
365 first_line_number = line_number
366 here = '%s:%d' % (file_name, line_number)
367 if (line.startswith('/*')
368 or line.startswith(' *')
369 or line.startswith('#')
373 elif re.match('}', line) or re.match('\s+MFF_N_IDS', line):
376 # Parse the comment preceding an MFF_ constant into 'comment',
377 # one line to an array element.
379 if not line.startswith('/*'):
380 fatal("unexpected syntax between fields")
386 if line.startswith('*/'):
389 if not line.startswith('*'):
390 fatal("unexpected syntax within field")
393 if line.startswith(' '):
395 if line.startswith(' ') and comment:
401 if line.endswith('*/'):
402 line = line[:-2].rstrip()
408 comment[-1] += " " + line
413 # Drop blank lines at each end of comment.
414 while comment and not comment[0]:
415 comment = comment[1:]
416 while comment and not comment[-1]:
417 comment = comment[:-1]
419 # Parse the MFF_ constant(s).
422 m = re.match('\s+(MFF_[A-Z0-9_]+),?\s?$', line)
428 fatal("unexpected syntax looking for MFF_ constants")
430 if len(mffs) > 1 or '<N>' in comment[0]:
432 # Extract trailing integer.
433 m = re.match('.*[^0-9]([0-9]+)$', mff)
435 fatal("%s lacks numeric suffix in register group" % mff)
438 # Search-and-replace <N> within the comment,
439 # and drop lines that have <x> for x != n.
442 y = x.replace('<N>', n)
443 if re.search('<[0-9]+>', y):
444 if ('<%s>' % n) not in y:
446 y = re.sub('<[0-9]+>', '', y)
447 instance += [y.strip()]
448 fields += [parse_field(mff, instance)]
450 fields += [parse_field(mffs[0], comment)]
459 /* Generated automatically; do not modify! "-*- buffer-read-only: t -*- */
462 if mode == '--meta-flow':
463 output = make_meta_flow(fields)
464 elif mode == '--nx-match':
465 output = make_nx_match(fields)
472 if __name__ == '__main__':
473 if '--help' in sys.argv:
475 elif len(sys.argv) != 3:
476 sys.stderr.write("exactly two arguments required; "
477 "use --help for help\n")
479 elif sys.argv[2] in ('--meta-flow', '--nx-match'):
483 file_name = sys.argv[1]
484 input_file = open(file_name)
487 for oline in extract_ofp_fields(sys.argv[2]):
490 sys.stderr.write("invalid arguments; use --help for help\n")