Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | # 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 | |
| 28 | from collections import namedtuple |
Rich Lane | c268579 | 2013-04-30 14:08:33 -0700 | [diff] [blame] | 29 | import struct |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 30 | import of_g |
| 31 | import loxi_front_end.type_maps as type_maps |
| 32 | import loxi_utils.loxi_utils as utils |
| 33 | import util |
| 34 | import oftype |
| 35 | |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 36 | OFClass = namedtuple('OFClass', ['name', 'pyname', 'members', 'type_members', |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 37 | 'min_length', 'is_fixed_length']) |
Rich Lane | ef0b887 | 2013-05-01 13:54:19 -0700 | [diff] [blame] | 38 | Member = namedtuple('Member', ['name', 'oftype']) |
| 39 | LengthMember = namedtuple('LengthMember', ['name', 'oftype']) |
| 40 | FieldLengthMember = namedtuple('FieldLengthMember', ['name', 'oftype', 'field_name']) |
| 41 | TypeMember = namedtuple('TypeMember', ['name', 'oftype', 'value']) |
| 42 | PadMember = namedtuple('PadMember', ['length']) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 43 | |
Rich Lane | 193317b | 2013-05-01 12:09:42 -0700 | [diff] [blame] | 44 | # XXX move to frontend |
| 45 | field_length_members = { |
| 46 | ('of_packet_out', 1, 'actions_len') : 'actions', |
| 47 | ('of_packet_out', 2, 'actions_len') : 'actions', |
| 48 | ('of_packet_out', 3, 'actions_len') : 'actions', |
| 49 | ('of_packet_out', 4, 'actions_len') : 'actions', |
| 50 | } |
| 51 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 52 | def get_type_values(cls, version): |
| 53 | """ |
| 54 | Returns a map from the name of the type member to its value. |
| 55 | """ |
| 56 | type_values = {} |
| 57 | |
| 58 | # Primary wire type |
| 59 | if utils.class_is_message(cls): |
| 60 | type_values['version'] = 'const.OFP_VERSION' |
| 61 | type_values['type'] = util.constant_for_value(version, "ofp_type", util.primary_wire_type(cls, version)) |
| 62 | if cls in type_maps.flow_mod_list: |
| 63 | type_values['_command'] = util.constant_for_value(version, "ofp_flow_mod_command", |
| 64 | type_maps.flow_mod_types[version][cls[8:]]) |
| 65 | if cls in type_maps.stats_request_list: |
| 66 | type_values['stats_type'] = util.constant_for_value(version, "ofp_stats_types", |
| 67 | type_maps.stats_types[version][cls[3:-14]]) |
| 68 | if cls in type_maps.stats_reply_list: |
| 69 | type_values['stats_type'] = util.constant_for_value(version, "ofp_stats_types", |
| 70 | type_maps.stats_types[version][cls[3:-12]]) |
| 71 | if type_maps.message_is_extension(cls, version): |
| 72 | type_values['experimenter'] = '%#x' % type_maps.extension_to_experimenter_id(cls) |
| 73 | type_values['subtype'] = type_maps.extension_message_to_subtype(cls, version) |
| 74 | elif utils.class_is_action(cls): |
| 75 | type_values['type'] = util.constant_for_value(version, "ofp_action_type", util.primary_wire_type(cls, version)) |
| 76 | if type_maps.action_is_extension(cls, version): |
| 77 | type_values['experimenter'] = '%#x' % type_maps.extension_to_experimenter_id(cls) |
| 78 | type_values['subtype'] = type_maps.extension_action_to_subtype(cls, version) |
| 79 | elif utils.class_is_queue_prop(cls): |
| 80 | type_values['type'] = util.constant_for_value(version, "ofp_queue_properties", util.primary_wire_type(cls, version)) |
Rich Lane | e90685c | 2013-04-05 17:27:41 -0700 | [diff] [blame] | 81 | elif utils.class_is_hello_elem(cls): |
| 82 | type_values['type'] = util.constant_for_value(version, "ofp_hello_elem_type", util.primary_wire_type(cls, version)) |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 83 | elif utils.class_is_oxm(cls): |
| 84 | oxm_class = 0x8000 |
| 85 | oxm_type = util.primary_wire_type(cls, version) |
| 86 | oxm_masked = cls.find('masked') != -1 and 1 or 0 |
Rich Lane | 82e9f6e | 2013-04-25 17:32:22 -0700 | [diff] [blame] | 87 | oxm_len = of_g.base_length[(cls, version)] - 4 |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 88 | type_values['type_len'] = '%#x' % (oxm_class << 16 | oxm_type << 8 | \ |
| 89 | oxm_masked << 8 | oxm_len) |
Rich Lane | 0b2ce25 | 2013-05-01 13:21:22 -0700 | [diff] [blame] | 90 | elif cls == "of_match_v2": |
| 91 | type_values['type'] = 0 |
Rich Lane | 3005cf9 | 2013-05-01 12:33:35 -0700 | [diff] [blame] | 92 | elif cls == "of_match_v3": |
| 93 | type_values['type'] = 1 |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 94 | |
| 95 | return type_values |
| 96 | |
| 97 | # Create intermediate representation |
| 98 | def build_ofclasses(version): |
| 99 | blacklist = ["of_action", "of_action_header", "of_header", "of_queue_prop", |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 100 | "of_queue_prop_header", "of_experimenter", "of_action_experimenter", |
Rich Lane | e90685c | 2013-04-05 17:27:41 -0700 | [diff] [blame] | 101 | "of_oxm", "of_oxm_header", "of_oxm_experimenter_header", |
| 102 | "of_hello_elem", "of_hello_elem_header"] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 103 | ofclasses = [] |
| 104 | for cls in of_g.standard_class_order: |
| 105 | if version not in of_g.unified[cls] or cls in blacklist: |
| 106 | continue |
| 107 | unified_class = util.lookup_unified_class(cls, version) |
| 108 | |
| 109 | # Name for the generated Python class |
| 110 | if utils.class_is_action(cls): |
| 111 | pyname = cls[10:] |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 112 | elif utils.class_is_oxm(cls): |
| 113 | pyname = cls[7:] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 114 | else: |
| 115 | pyname = cls[3:] |
| 116 | |
| 117 | type_values = get_type_values(cls, version) |
| 118 | members = [] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 119 | type_members = [] |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 120 | |
Rich Lane | c58a232 | 2013-03-15 23:28:52 -0700 | [diff] [blame] | 121 | pad_count = 0 |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 122 | |
| 123 | for member in unified_class['members']: |
| 124 | if member['name'] in ['length', 'len']: |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 125 | members.append(LengthMember(name=member['name'], |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 126 | oftype=oftype.OFType(member['m_type'], version))) |
Rich Lane | 193317b | 2013-05-01 12:09:42 -0700 | [diff] [blame] | 127 | elif (cls, version, member['name']) in field_length_members: |
| 128 | field_name = field_length_members[(cls, version, member['name'])] |
| 129 | members.append(FieldLengthMember(name=member['name'], |
Rich Lane | 193317b | 2013-05-01 12:09:42 -0700 | [diff] [blame] | 130 | oftype=oftype.OFType(member['m_type'], version), |
| 131 | field_name=field_name)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 132 | elif member['name'] in type_values: |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 133 | members.append(TypeMember(name=member['name'], |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 134 | oftype=oftype.OFType(member['m_type'], version), |
| 135 | value=type_values[member['name']])) |
| 136 | type_members.append(members[-1]) |
Rich Lane | c268579 | 2013-04-30 14:08:33 -0700 | [diff] [blame] | 137 | elif member['name'].startswith("pad"): |
| 138 | # HACK this should be moved to the frontend |
| 139 | pad_oftype = oftype.OFType(member['m_type'], version) |
| 140 | length = struct.calcsize("!" + pad_oftype._pack_fmt()) |
| 141 | if pad_oftype.is_array: length *= pad_oftype.array_length |
Rich Lane | ef0b887 | 2013-05-01 13:54:19 -0700 | [diff] [blame] | 142 | members.append(PadMember(length=length)) |
Rich Lane | c268579 | 2013-04-30 14:08:33 -0700 | [diff] [blame] | 143 | else: |
| 144 | members.append(Member(name=member['name'], |
Rich Lane | ef0b887 | 2013-05-01 13:54:19 -0700 | [diff] [blame] | 145 | oftype=oftype.OFType(member['m_type'], version))) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 146 | |
| 147 | ofclasses.append( |
| 148 | OFClass(name=cls, |
| 149 | pyname=pyname, |
| 150 | members=members, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 151 | type_members=type_members, |
| 152 | min_length=of_g.base_length[(cls, version)], |
| 153 | is_fixed_length=(cls, version) in of_g.is_fixed_length)) |
| 154 | return ofclasses |
| 155 | |
| 156 | def generate_init(out, name, version): |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 157 | util.render_template(out, 'init.py', version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 158 | |
| 159 | def generate_action(out, name, version): |
| 160 | ofclasses = [x for x in build_ofclasses(version) |
| 161 | if utils.class_is_action(x.name)] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 162 | util.render_template(out, 'action.py', ofclasses=ofclasses, version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 163 | |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 164 | def generate_oxm(out, name, version): |
| 165 | ofclasses = [x for x in build_ofclasses(version) |
| 166 | if utils.class_is_oxm(x.name)] |
| 167 | util.render_template(out, 'oxm.py', ofclasses=ofclasses, version=version) |
| 168 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 169 | def generate_common(out, name, version): |
| 170 | ofclasses = [x for x in build_ofclasses(version) |
| 171 | if not utils.class_is_message(x.name) |
| 172 | and not utils.class_is_action(x.name) |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 173 | and not utils.class_is_oxm(x.name) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 174 | and not utils.class_is_list(x.name)] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 175 | util.render_template(out, 'common.py', ofclasses=ofclasses, version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 176 | |
| 177 | def generate_const(out, name, version): |
| 178 | groups = {} |
| 179 | for (group, idents) in of_g.identifiers_by_group.items(): |
| 180 | items = [] |
| 181 | for ident in idents: |
| 182 | info = of_g.identifiers[ident] |
| 183 | if version in info["values_by_version"]: |
| 184 | items.append((info["ofp_name"], info["values_by_version"][version])) |
| 185 | if items: |
| 186 | groups[group] = items |
| 187 | util.render_template(out, 'const.py', version=version, groups=groups) |
| 188 | |
| 189 | def generate_message(out, name, version): |
| 190 | ofclasses = [x for x in build_ofclasses(version) |
| 191 | if utils.class_is_message(x.name)] |
| 192 | util.render_template(out, 'message.py', ofclasses=ofclasses, version=version) |
| 193 | |
| 194 | def generate_pp(out, name, version): |
| 195 | util.render_template(out, 'pp.py') |
| 196 | |
| 197 | def generate_util(out, name, version): |
| 198 | util.render_template(out, 'util.py') |