blob: 19bdbaff24d0cceadd5c996d77e5b1400f363775 [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
29import of_g
30import loxi_front_end.type_maps as type_maps
31import loxi_utils.loxi_utils as utils
32import util
33import oftype
34
35OFClass = namedtuple('OFClass', ['name', 'pyname',
36 'members', 'length_member', 'type_members',
37 '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'])
41
42def get_type_values(cls, version):
43 """
44 Returns a map from the name of the type member to its value.
45 """
46 type_values = {}
47
48 # Primary wire type
49 if utils.class_is_message(cls):
50 type_values['version'] = 'const.OFP_VERSION'
51 type_values['type'] = util.constant_for_value(version, "ofp_type", util.primary_wire_type(cls, version))
52 if cls in type_maps.flow_mod_list:
53 type_values['_command'] = util.constant_for_value(version, "ofp_flow_mod_command",
54 type_maps.flow_mod_types[version][cls[8:]])
55 if cls in type_maps.stats_request_list:
56 type_values['stats_type'] = util.constant_for_value(version, "ofp_stats_types",
57 type_maps.stats_types[version][cls[3:-14]])
58 if cls in type_maps.stats_reply_list:
59 type_values['stats_type'] = util.constant_for_value(version, "ofp_stats_types",
60 type_maps.stats_types[version][cls[3:-12]])
61 if type_maps.message_is_extension(cls, version):
62 type_values['experimenter'] = '%#x' % type_maps.extension_to_experimenter_id(cls)
63 type_values['subtype'] = type_maps.extension_message_to_subtype(cls, version)
64 elif utils.class_is_action(cls):
65 type_values['type'] = util.constant_for_value(version, "ofp_action_type", util.primary_wire_type(cls, version))
66 if type_maps.action_is_extension(cls, version):
67 type_values['experimenter'] = '%#x' % type_maps.extension_to_experimenter_id(cls)
68 type_values['subtype'] = type_maps.extension_action_to_subtype(cls, version)
69 elif utils.class_is_queue_prop(cls):
70 type_values['type'] = util.constant_for_value(version, "ofp_queue_properties", util.primary_wire_type(cls, version))
Rich Laneea693752013-03-18 11:05:45 -070071 elif utils.class_is_oxm(cls):
72 oxm_class = 0x8000
73 oxm_type = util.primary_wire_type(cls, version)
74 oxm_masked = cls.find('masked') != -1 and 1 or 0
75 oxm_len = of_g.base_length[(cls, version)]
76 type_values['type_len'] = '%#x' % (oxm_class << 16 | oxm_type << 8 | \
77 oxm_masked << 8 | oxm_len)
Rich Lanea06d0c32013-03-25 08:52:03 -070078
79 return type_values
80
81# Create intermediate representation
82def build_ofclasses(version):
83 blacklist = ["of_action", "of_action_header", "of_header", "of_queue_prop",
Rich Lane3f075972013-03-15 22:56:29 -070084 "of_queue_prop_header", "of_experimenter", "of_action_experimenter",
Rich Laneea693752013-03-18 11:05:45 -070085 "of_oxm", "of_oxm_header", "of_oxm_experimenter_header"]
Rich Lanea06d0c32013-03-25 08:52:03 -070086 ofclasses = []
87 for cls in of_g.standard_class_order:
88 if version not in of_g.unified[cls] or cls in blacklist:
89 continue
90 unified_class = util.lookup_unified_class(cls, version)
91
92 # Name for the generated Python class
93 if utils.class_is_action(cls):
94 pyname = cls[10:]
Rich Laneea693752013-03-18 11:05:45 -070095 elif utils.class_is_oxm(cls):
96 pyname = cls[7:]
Rich Lanea06d0c32013-03-25 08:52:03 -070097 else:
98 pyname = cls[3:]
99
100 type_values = get_type_values(cls, version)
101 members = []
102
103 length_member = None
104 type_members = []
Rich Lanec58a2322013-03-15 23:28:52 -0700105 pad_count = 0
Rich Lanea06d0c32013-03-25 08:52:03 -0700106
107 for member in unified_class['members']:
108 if member['name'] in ['length', 'len']:
109 length_member = LengthMember(name=member['name'],
110 offset=member['offset'],
111 oftype=oftype.OFType(member['m_type'], version))
112 elif member['name'] in type_values:
113 type_members.append(TypeMember(name=member['name'],
114 offset=member['offset'],
115 oftype=oftype.OFType(member['m_type'], version),
116 value=type_values[member['name']]))
117 else:
118 # HACK ensure member names are unique
Rich Lanec58a2322013-03-15 23:28:52 -0700119 if member['name'].startswith("pad"):
120 if pad_count == 0:
121 m_name = 'pad'
122 else:
123 m_name = "pad%d" % pad_count
124 pad_count += 1
Rich Lanea06d0c32013-03-25 08:52:03 -0700125 else:
126 m_name = member['name']
127 members.append(Member(name=m_name,
128 oftype=oftype.OFType(member['m_type'], version),
129 offset=member['offset'],
130 skip=member['name'] in of_g.skip_members))
131
132 ofclasses.append(
133 OFClass(name=cls,
134 pyname=pyname,
135 members=members,
136 length_member=length_member,
137 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')