blob: 01dd782aa81a4a2e835ab95ac6fcf8ef0d9898d8 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001# Copyright 2013, Big Switch Networks, Inc.
2#
3# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4# the following special exception:
5#
6# LOXI Exception
7#
8# As a special exception to the terms of the EPL, you may distribute libraries
9# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10# that copyright and licensing notices generated by LoxiGen are not altered or removed
11# from the LoxiGen Libraries and the notice provided below is (i) included in
12# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13# documentation for the LoxiGen Libraries, if distributed in binary form.
14#
15# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16#
17# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18# a copy of the EPL at:
19#
20# http://www.eclipse.org/legal/epl-v10.html
21#
22# Unless required by applicable law or agreed to in writing, software
23# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25# EPL for the specific language governing permissions and limitations
26# under the EPL.
27
28from collections import namedtuple
Rich Lanec2685792013-04-30 14:08:33 -070029import struct
Rich Lanea06d0c32013-03-25 08:52:03 -070030import of_g
31import loxi_front_end.type_maps as type_maps
32import loxi_utils.loxi_utils as utils
33import util
34import oftype
35
Rich Lane8ca3b772013-04-30 13:36:55 -070036OFClass = namedtuple('OFClass', ['name', 'pyname', 'members', 'type_members',
Rich Lanea06d0c32013-03-25 08:52:03 -070037 'min_length', 'is_fixed_length'])
38Member = namedtuple('Member', ['name', 'oftype', 'offset', 'skip'])
39LengthMember = namedtuple('LengthMember', ['name', 'oftype', 'offset'])
40TypeMember = namedtuple('TypeMember', ['name', 'oftype', 'offset', 'value'])
Rich Lanec2685792013-04-30 14:08:33 -070041PadMember = namedtuple('PadMember', ['offset', 'length'])
Rich Lanea06d0c32013-03-25 08:52:03 -070042
43def get_type_values(cls, version):
44 """
45 Returns a map from the name of the type member to its value.
46 """
47 type_values = {}
48
49 # Primary wire type
50 if utils.class_is_message(cls):
51 type_values['version'] = 'const.OFP_VERSION'
52 type_values['type'] = util.constant_for_value(version, "ofp_type", util.primary_wire_type(cls, version))
53 if cls in type_maps.flow_mod_list:
54 type_values['_command'] = util.constant_for_value(version, "ofp_flow_mod_command",
55 type_maps.flow_mod_types[version][cls[8:]])
56 if cls in type_maps.stats_request_list:
57 type_values['stats_type'] = util.constant_for_value(version, "ofp_stats_types",
58 type_maps.stats_types[version][cls[3:-14]])
59 if cls in type_maps.stats_reply_list:
60 type_values['stats_type'] = util.constant_for_value(version, "ofp_stats_types",
61 type_maps.stats_types[version][cls[3:-12]])
62 if type_maps.message_is_extension(cls, version):
63 type_values['experimenter'] = '%#x' % type_maps.extension_to_experimenter_id(cls)
64 type_values['subtype'] = type_maps.extension_message_to_subtype(cls, version)
65 elif utils.class_is_action(cls):
66 type_values['type'] = util.constant_for_value(version, "ofp_action_type", util.primary_wire_type(cls, version))
67 if type_maps.action_is_extension(cls, version):
68 type_values['experimenter'] = '%#x' % type_maps.extension_to_experimenter_id(cls)
69 type_values['subtype'] = type_maps.extension_action_to_subtype(cls, version)
70 elif utils.class_is_queue_prop(cls):
71 type_values['type'] = util.constant_for_value(version, "ofp_queue_properties", util.primary_wire_type(cls, version))
Rich Lanee90685c2013-04-05 17:27:41 -070072 elif utils.class_is_hello_elem(cls):
73 type_values['type'] = util.constant_for_value(version, "ofp_hello_elem_type", util.primary_wire_type(cls, version))
Rich Laneea693752013-03-18 11:05:45 -070074 elif utils.class_is_oxm(cls):
75 oxm_class = 0x8000
76 oxm_type = util.primary_wire_type(cls, version)
77 oxm_masked = cls.find('masked') != -1 and 1 or 0
78 oxm_len = of_g.base_length[(cls, version)]
79 type_values['type_len'] = '%#x' % (oxm_class << 16 | oxm_type << 8 | \
80 oxm_masked << 8 | oxm_len)
Rich Lanea06d0c32013-03-25 08:52:03 -070081
82 return type_values
83
84# Create intermediate representation
85def build_ofclasses(version):
86 blacklist = ["of_action", "of_action_header", "of_header", "of_queue_prop",
Rich Lane3f075972013-03-15 22:56:29 -070087 "of_queue_prop_header", "of_experimenter", "of_action_experimenter",
Rich Lanee90685c2013-04-05 17:27:41 -070088 "of_oxm", "of_oxm_header", "of_oxm_experimenter_header",
89 "of_hello_elem", "of_hello_elem_header"]
Rich Lanea06d0c32013-03-25 08:52:03 -070090 ofclasses = []
91 for cls in of_g.standard_class_order:
92 if version not in of_g.unified[cls] or cls in blacklist:
93 continue
94 unified_class = util.lookup_unified_class(cls, version)
95
96 # Name for the generated Python class
97 if utils.class_is_action(cls):
98 pyname = cls[10:]
Rich Laneea693752013-03-18 11:05:45 -070099 elif utils.class_is_oxm(cls):
100 pyname = cls[7:]
Rich Lanea06d0c32013-03-25 08:52:03 -0700101 else:
102 pyname = cls[3:]
103
104 type_values = get_type_values(cls, version)
105 members = []
Rich Lanea06d0c32013-03-25 08:52:03 -0700106 type_members = []
Rich Lane8ca3b772013-04-30 13:36:55 -0700107
Rich Lanec58a2322013-03-15 23:28:52 -0700108 pad_count = 0
Rich Lanea06d0c32013-03-25 08:52:03 -0700109
110 for member in unified_class['members']:
111 if member['name'] in ['length', 'len']:
Rich Lane8ca3b772013-04-30 13:36:55 -0700112 members.append(LengthMember(name=member['name'],
113 offset=member['offset'],
114 oftype=oftype.OFType(member['m_type'], version)))
Rich Lanea06d0c32013-03-25 08:52:03 -0700115 elif member['name'] in type_values:
Rich Lane8ca3b772013-04-30 13:36:55 -0700116 members.append(TypeMember(name=member['name'],
117 offset=member['offset'],
118 oftype=oftype.OFType(member['m_type'], version),
119 value=type_values[member['name']]))
120 type_members.append(members[-1])
Rich Lanec2685792013-04-30 14:08:33 -0700121 elif member['name'].startswith("pad"):
122 # HACK this should be moved to the frontend
123 pad_oftype = oftype.OFType(member['m_type'], version)
124 length = struct.calcsize("!" + pad_oftype._pack_fmt())
125 if pad_oftype.is_array: length *= pad_oftype.array_length
126 members.append(PadMember(offset=member['offset'], length=length))
127 else:
128 members.append(Member(name=member['name'],
Rich Lanea06d0c32013-03-25 08:52:03 -0700129 oftype=oftype.OFType(member['m_type'], version),
130 offset=member['offset'],
131 skip=member['name'] in of_g.skip_members))
132
133 ofclasses.append(
134 OFClass(name=cls,
135 pyname=pyname,
136 members=members,
Rich Lanea06d0c32013-03-25 08:52:03 -0700137 type_members=type_members,
138 min_length=of_g.base_length[(cls, version)],
139 is_fixed_length=(cls, version) in of_g.is_fixed_length))
140 return ofclasses
141
142def generate_init(out, name, version):
Rich Laneea693752013-03-18 11:05:45 -0700143 util.render_template(out, 'init.py', version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700144
145def generate_action(out, name, version):
146 ofclasses = [x for x in build_ofclasses(version)
147 if utils.class_is_action(x.name)]
Rich Lane3f075972013-03-15 22:56:29 -0700148 util.render_template(out, 'action.py', ofclasses=ofclasses, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700149
Rich Laneea693752013-03-18 11:05:45 -0700150def generate_oxm(out, name, version):
151 ofclasses = [x for x in build_ofclasses(version)
152 if utils.class_is_oxm(x.name)]
153 util.render_template(out, 'oxm.py', ofclasses=ofclasses, version=version)
154
Rich Lanea06d0c32013-03-25 08:52:03 -0700155def generate_common(out, name, version):
156 ofclasses = [x for x in build_ofclasses(version)
157 if not utils.class_is_message(x.name)
158 and not utils.class_is_action(x.name)
Rich Laneea693752013-03-18 11:05:45 -0700159 and not utils.class_is_oxm(x.name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700160 and not utils.class_is_list(x.name)]
Rich Lane3f075972013-03-15 22:56:29 -0700161 util.render_template(out, 'common.py', ofclasses=ofclasses, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700162
163def generate_const(out, name, version):
164 groups = {}
165 for (group, idents) in of_g.identifiers_by_group.items():
166 items = []
167 for ident in idents:
168 info = of_g.identifiers[ident]
169 if version in info["values_by_version"]:
170 items.append((info["ofp_name"], info["values_by_version"][version]))
171 if items:
172 groups[group] = items
173 util.render_template(out, 'const.py', version=version, groups=groups)
174
175def generate_message(out, name, version):
176 ofclasses = [x for x in build_ofclasses(version)
177 if utils.class_is_message(x.name)]
178 util.render_template(out, 'message.py', ofclasses=ofclasses, version=version)
179
180def generate_pp(out, name, version):
181 util.render_template(out, 'pp.py')
182
183def generate_util(out, name, version):
184 util.render_template(out, 'util.py')