loxi_ir: Moved to its own package
to group ir and related functionality. Also introduce a dedicated
frontend_ir model, and adds a unit test
diff --git a/loxi_front_end/frontend.py b/loxi_front_end/frontend.py
index f4dece5..1e1b1de 100644
--- a/loxi_front_end/frontend.py
+++ b/loxi_front_end/frontend.py
@@ -28,9 +28,8 @@
from generic_utils import find
from collections import namedtuple
import copy
-import of_g
-import loxi_front_end.type_maps as type_maps
-from loxi_ir import *
+import loxi_globals
+import loxi_front_end.frontend_ir as ir
class InputError(Exception):
pass
@@ -46,23 +45,23 @@
def create_member(m_ast, ctx):
if m_ast[0] == 'pad':
- return OFPadMember(length=m_ast[1])
+ return ir.OFPadMember(length=m_ast[1])
elif m_ast[0] == 'type':
- return OFTypeMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), value=m_ast[3])
+ return ir.OFTypeMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), value=m_ast[3])
elif m_ast[0] == 'data':
if m_ast[2] == 'length' or m_ast[2] == 'len': # Should be moved to parser
- return OFLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
+ return ir.OFLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
elif m_ast[2] == 'actions_len':
# HACK only usage so far
- return OFFieldLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), field_name='actions')
+ return ir.OFFieldLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), field_name='actions')
else:
- return OFDataMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
+ return ir.OFDataMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
elif m_ast[0] == 'discriminator':
- return OFDiscriminatorMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
+ return ir.OFDiscriminatorMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx))
else:
raise InputError("Dont know how to create member: %s" % m_ast[0])
-def create_ofinput(ast):
+def create_ofinput(filename, ast):
"""
Create an OFInput from an AST
@@ -72,7 +71,7 @@
@returns An OFInput object
"""
ctx = FrontendCtx(set())
- ofinput = OFInput(wire_versions=set(), classes=[], enums=[])
+ ofinput = ir.OFInput(filename, wire_versions=set(), classes=[], enums=[])
for decl_ast in ast:
if decl_ast[0] == 'struct':
@@ -84,11 +83,11 @@
superclass = decl_ast[3]
members = [create_member(m_ast, ctx) for m_ast in decl_ast[4]]
- discriminators = [ m for m in members if isinstance(m, OFDiscriminatorMember) ]
+ discriminators = [ m for m in members if isinstance(m, ir.OFDiscriminatorMember) ]
if len(discriminators) > 1:
raise InputError("%s: Cannot support more than one discriminator by class - got %s" %
(decl_ast[1], repr(discriminators)))
- ofclass = OFClass(name=decl_ast[1], members=members, superclass=superclass,
+ ofclass = ir.OFClass(name=decl_ast[1], members=members, superclass=superclass,
virtual = len(discriminators) > 0,
params = { param: value for param, value in decl_ast[2] })
ofinput.classes.append(ofclass)
@@ -97,16 +96,16 @@
# 1: name
# 2: potentially list of [param_name, param_value]
# 3: list of [constant_name, constant_value]+
- enum = OFEnum(name=decl_ast[1],
- entries=[OFEnumEntry(name=x[0], value=x[2], params={param:value for param, value in x[1] }) for x in decl_ast[3]],
+ enum = ir.OFEnum(name=decl_ast[1],
+ entries=[ir.OFEnumEntry(name=x[0], value=x[2], params={param:value for param, value in x[1] }) for x in decl_ast[3]],
params = { param: value for param, value in decl_ast[2] }
)
ofinput.enums.append(enum)
elif decl_ast[0] == 'metadata':
if decl_ast[1] == 'version':
if decl_ast[2] == 'any':
- ofinput.wire_versions.update(of_g.wire_ver_map.keys())
- elif int(decl_ast[2]) in of_g.supported_wire_protos:
+ ofinput.wire_versions.update(v.wire_version for v in loxi_globals.OFVersions.all_supported)
+ elif int(decl_ast[2]) in loxi_globals.OFVersions.wire_version_map:
ofinput.wire_versions.add(int(decl_ast[2]))
else:
raise InputError("Unrecognized wire protocol version %r" % decl_ast[2])
diff --git a/loxi_front_end/frontend_ir.py b/loxi_front_end/frontend_ir.py
new file mode 100644
index 0000000..a927f94
--- /dev/null
+++ b/loxi_front_end/frontend_ir.py
@@ -0,0 +1,141 @@
+# Copyright 2013, Big Switch Networks, Inc.
+#
+# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
+# the following special exception:
+#
+# LOXI Exception
+#
+# As a special exception to the terms of the EPL, you may distribute libraries
+# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
+# that copyright and licensing notices generated by LoxiGen are not altered or removed
+# from the LoxiGen Libraries and the notice provided below is (i) included in
+# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
+# documentation for the LoxiGen Libraries, if distributed in binary form.
+#
+# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
+#
+# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
+# a copy of the EPL at:
+#
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# EPL for the specific language governing permissions and limitations
+# under the EPL.
+
+from generic_utils import find
+from collections import namedtuple
+
+# This module is represents the frontend IR.
+__all__ = [
+ 'OFInput',
+ 'OFClass',
+ 'OFDataMember',
+ 'OFTypeMember',
+ 'OFDiscriminatorMember',
+ 'OFLengthMember',
+ 'OFFieldLengthMember',
+ 'OFPadMember',
+ 'OFEnum',
+ 'OFEnumEntry'
+]
+
+"""
+One input file
+
+@param wire_versions Set of integer wire versions this file applies to
+@param classes List of OFClass objects in the same order as in the file
+@param enums List of Enum objects in the same order as in the file
+"""
+OFInput = namedtuple('OFInput', ['filename', 'wire_versions', 'classes', 'enums'])
+
+"""
+An OpenFlow class
+
+All compound objects like messages, actions, instructions, etc are
+uniformly represented by this class.
+
+The members are in the same order as on the wire.
+
+@param name
+@param superclass name of the super class
+@param members List of *Member objects
+@param params optional dictionary of parameters
+"""
+OFClass = namedtuple('OFClass', ['name', 'superclass', 'members', 'virtual', 'params'])
+
+"""
+Normal field
+
+@param name
+@param oftype C-like type string
+
+Example: packet_in.buffer_id
+"""
+OFDataMember = namedtuple('OFDataMember', ['name', 'oftype'])
+
+"""
+Field that declares that this is an abstract super-class and
+that the sub classes will be discriminated based on this field.
+E.g., 'type' is the discriminator member of the abstract superclass
+of_action.
+
+@param name
+"""
+OFDiscriminatorMember = namedtuple('OFDiscriminatorMember', ['name', 'oftype'])
+
+"""
+Field used to determine the type of an OpenFlow object
+
+@param name
+@param oftype C-like type string
+@param value Fixed type value
+
+Example: packet_in.type, flow_add._command
+"""
+OFTypeMember = namedtuple('OFTypeMember', ['name', 'oftype', 'value'])
+
+"""
+Field with the length of the containing object
+
+@param name
+@param oftype C-like type string
+
+Example: packet_in.length, action_output.len
+"""
+OFLengthMember = namedtuple('OFLengthMember', ['name', 'oftype'])
+
+"""
+Field with the length of another field in the containing object
+
+@param name
+@param oftype C-like type string
+@param field_name Peer field whose length this field contains
+
+Example: packet_out.actions_len (only usage)
+"""
+OFFieldLengthMember = namedtuple('OFFieldLengthMember', ['name', 'oftype', 'field_name'])
+
+"""
+Zero-filled padding
+
+@param length Length in bytes
+
+Example: packet_in.pad
+"""
+OFPadMember = namedtuple('OFPadMember', ['length'])
+
+"""
+An OpenFlow enumeration
+
+All values are Python ints.
+
+@param name
+@param entries List of OFEnumEntry objects in input order
+@params dict of optional params. Currently defined:
+ - wire_type: the low_level type of the enum values (uint8,...)
+"""
+OFEnum = namedtuple('OFEnum', ['name', 'entries', 'params'])
+OFEnumEntry = namedtuple('OFEnumEntry', ['name', 'value', 'params'])