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 | |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 28 | from collections import namedtuple |
| 29 | |
Andreas Wundsam | 69ecfdc | 2013-09-17 13:50:12 -0700 | [diff] [blame] | 30 | import loxi_utils.loxi_utils as loxi_utils |
| 31 | import of_g |
| 32 | |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 33 | OFTypeData = namedtuple("OFTypeData", ["init", "pack", "unpack"]) |
| 34 | |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 35 | # Map from LOXI type name to an object with templates for init, pack, and unpack |
| 36 | # Most types are defined using the convenience code below. This dict should |
| 37 | # only be used directly for special cases such as primitive types. |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 38 | type_data_map = { |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 39 | 'char': OFTypeData( |
| 40 | init='0', |
| 41 | pack='struct.pack("!B", %s)', |
| 42 | unpack='%s.read("!B")[0]'), |
| 43 | |
| 44 | 'uint8_t': OFTypeData( |
| 45 | init='0', |
| 46 | pack='struct.pack("!B", %s)', |
| 47 | unpack='%s.read("!B")[0]'), |
| 48 | |
| 49 | 'uint16_t': OFTypeData( |
| 50 | init='0', |
| 51 | pack='struct.pack("!H", %s)', |
| 52 | unpack='%s.read("!H")[0]'), |
| 53 | |
| 54 | 'uint32_t': OFTypeData( |
| 55 | init='0', |
| 56 | pack='struct.pack("!L", %s)', |
| 57 | unpack='%s.read("!L")[0]'), |
| 58 | |
| 59 | 'uint64_t': OFTypeData( |
| 60 | init='0', |
| 61 | pack='struct.pack("!Q", %s)', |
| 62 | unpack='%s.read("!Q")[0]'), |
| 63 | |
| 64 | 'of_port_no_t': OFTypeData( |
| 65 | init='0', |
| 66 | pack='util.pack_port_no(%s)', |
| 67 | unpack='util.unpack_port_no(%s)'), |
| 68 | |
| 69 | 'of_fm_cmd_t': OFTypeData( |
| 70 | init='0', |
| 71 | pack='util.pack_fm_cmd(%s)', |
| 72 | unpack='util.unpack_fm_cmd(%s)'), |
| 73 | |
| 74 | 'of_wc_bmap_t': OFTypeData( |
| 75 | init='util.init_wc_bmap()', |
| 76 | pack='util.pack_wc_bmap(%s)', |
| 77 | unpack='util.unpack_wc_bmap(%s)'), |
| 78 | |
| 79 | 'of_match_bmap_t': OFTypeData( |
| 80 | init='util.init_match_bmap()', |
| 81 | pack='util.pack_match_bmap(%s)', |
| 82 | unpack='util.unpack_match_bmap(%s)'), |
| 83 | |
Andreas Wundsam | b566a16 | 2013-07-18 19:30:23 -0700 | [diff] [blame] | 84 | 'of_ipv4_t': OFTypeData( |
| 85 | init='0', |
| 86 | pack='struct.pack("!L", %s)', |
| 87 | unpack='%s.read("!L")[0]'), |
| 88 | |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 89 | 'of_ipv6_t': OFTypeData( |
| 90 | init="'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'", |
| 91 | pack='struct.pack("!16s", %s)', |
| 92 | unpack="%s.read('!16s')[0]"), |
| 93 | |
| 94 | 'of_mac_addr_t': OFTypeData( |
| 95 | init='[0,0,0,0,0,0]', |
| 96 | pack='struct.pack("!6B", *%s)', |
| 97 | unpack="list(%s.read('!6B'))"), |
| 98 | |
| 99 | 'of_octets_t': OFTypeData( |
| 100 | init="''", |
| 101 | pack='%s', |
| 102 | unpack='str(%s.read_all())'), |
| 103 | |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 104 | # HACK need the match_v3 length field |
| 105 | 'list(of_oxm_t)': OFTypeData( |
| 106 | init='[]', |
| 107 | pack='util.pack_list(%s)', |
| 108 | unpack='oxm.unpack_list(%s.slice(_length-4))'), |
| 109 | |
Rich Lane | be90eae | 2013-07-22 16:44:26 -0700 | [diff] [blame] | 110 | 'of_oxm_t': OFTypeData( |
| 111 | init='None', |
| 112 | pack='%s.pack()', |
| 113 | unpack='oxm.unpack(%s)'), |
| 114 | |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 115 | # TODO implement unpack |
| 116 | 'list(of_table_features_t)': OFTypeData( |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 117 | init='[]', |
| 118 | pack='util.pack_list(%s)', |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 119 | unpack=None), |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 120 | |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 121 | # TODO implement unpack |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 122 | 'list(of_action_id_t)': OFTypeData( |
| 123 | init='[]', |
| 124 | pack='util.pack_list(%s)', |
| 125 | unpack=None), |
| 126 | |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 127 | # TODO implement unpack |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 128 | 'list(of_table_feature_prop_t)': OFTypeData( |
| 129 | init='[]', |
| 130 | pack='util.pack_list(%s)', |
| 131 | unpack=None), |
Rich Lane | 8a22cda | 2013-06-19 13:20:07 -0700 | [diff] [blame] | 132 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 133 | |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 134 | ## Fixed length strings |
| 135 | |
| 136 | # Map from class name to length |
| 137 | fixed_length_strings = { |
| 138 | 'of_port_name_t': 16, |
| 139 | 'of_table_name_t': 32, |
| 140 | 'of_serial_num_t': 32, |
| 141 | 'of_desc_str_t': 256, |
| 142 | } |
| 143 | |
| 144 | for (cls, length) in fixed_length_strings.items(): |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 145 | type_data_map[cls] = OFTypeData( |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 146 | init='""', |
| 147 | pack='struct.pack("!%ds", %%s)' % length, |
| 148 | unpack='%%s.read("!%ds")[0].rstrip("\\x00")' % length) |
| 149 | |
| 150 | ## Embedded structs |
| 151 | |
| 152 | # Map from class name to Python class name |
| 153 | embedded_structs = { |
| 154 | 'of_match_t': 'common.match', |
| 155 | 'of_port_desc_t': 'common.port_desc', |
| 156 | 'of_meter_features_t': 'common.meter_features', |
| 157 | 'of_bsn_vport_q_in_q_t': 'common.bsn_vport_q_in_q', |
| 158 | } |
| 159 | |
| 160 | for (cls, pyclass) in embedded_structs.items(): |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 161 | type_data_map[cls] = OFTypeData( |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 162 | init='%s()' % pyclass, |
| 163 | pack='%s.pack()', |
| 164 | unpack='%s.unpack(%%s)' % pyclass) |
| 165 | |
| 166 | ## Variable element length lists |
| 167 | |
| 168 | # Map from list class name to list deserializer |
| 169 | variable_elem_len_lists = { |
| 170 | 'list(of_action_t)': 'action.unpack_list', |
| 171 | 'list(of_bucket_t)': 'common.unpack_list_bucket', |
| 172 | 'list(of_flow_stats_entry_t)': 'common.unpack_list_flow_stats_entry', |
| 173 | 'list(of_group_desc_stats_entry_t)': 'common.unpack_list_group_desc_stats_entry', |
| 174 | 'list(of_group_stats_entry_t)': 'common.unpack_list_group_stats_entry', |
| 175 | 'list(of_hello_elem_t)': 'common.unpack_list_hello_elem', |
| 176 | 'list(of_instruction_t)': 'instruction.unpack_list', |
| 177 | 'list(of_meter_band_t)': 'meter_band.unpack_list', |
| 178 | 'list(of_meter_stats_t)': 'common.unpack_list_meter_stats', |
| 179 | 'list(of_packet_queue_t)': 'common.unpack_list_packet_queue', |
| 180 | 'list(of_queue_prop_t)': 'common.unpack_list_queue_prop', |
| 181 | } |
| 182 | |
| 183 | for (cls, deserializer) in variable_elem_len_lists.items(): |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 184 | type_data_map[cls] = OFTypeData( |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 185 | init='[]', |
| 186 | pack='util.pack_list(%s)', |
| 187 | unpack='%s(%%s)' % deserializer) |
| 188 | |
| 189 | ## Fixed element length lists |
| 190 | |
| 191 | # Map from list class name to list element deserializer |
| 192 | fixed_elem_len_lists = { |
| 193 | 'list(of_bsn_interface_t)': 'common.bsn_interface.unpack', |
| 194 | 'list(of_bucket_counter_t)': 'common.bucket_counter.unpack', |
| 195 | 'list(of_meter_band_stats_t)': 'common.meter_band_stats.unpack', |
| 196 | 'list(of_port_desc_t)': 'common.port_desc.unpack', |
| 197 | 'list(of_port_stats_entry_t)': 'common.port_stats_entry.unpack', |
| 198 | 'list(of_queue_stats_entry_t)': 'common.queue_stats_entry.unpack', |
| 199 | 'list(of_table_stats_entry_t)': 'common.table_stats_entry.unpack', |
| 200 | 'list(of_uint32_t)': 'common.uint32.unpack', |
| 201 | 'list(of_uint8_t)': 'common.uint8.unpack', |
| 202 | } |
| 203 | |
| 204 | for (cls, element_deserializer) in fixed_elem_len_lists.items(): |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 205 | type_data_map[cls] = OFTypeData( |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 206 | init='[]', |
| 207 | pack='util.pack_list(%s)', |
| 208 | unpack='loxi.generic_util.unpack_list(%%s, %s)' % element_deserializer) |
| 209 | |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 210 | ## Public interface |
Rich Lane | c854bae | 2013-06-19 14:39:51 -0700 | [diff] [blame] | 211 | |
Andreas Wundsam | 69ecfdc | 2013-09-17 13:50:12 -0700 | [diff] [blame] | 212 | def lookup_type_data(oftype, version): |
| 213 | return type_data_map.get(loxi_utils.lookup_ir_wiretype(oftype, version)) |
| 214 | |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 215 | # Return an initializer expression for the given oftype |
Andreas Wundsam | 69ecfdc | 2013-09-17 13:50:12 -0700 | [diff] [blame] | 216 | def gen_init_expr(oftype, version): |
| 217 | type_data = lookup_type_data(oftype, version) |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 218 | if type_data and type_data.init: |
| 219 | return type_data.init |
| 220 | else: |
| 221 | return "loxi.unimplemented('init %s')" % oftype |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 222 | |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 223 | # Return a pack expression for the given oftype |
| 224 | # |
| 225 | # 'value_expr' is a string of Python code which will evaluate to |
| 226 | # the value to be packed. |
Andreas Wundsam | 69ecfdc | 2013-09-17 13:50:12 -0700 | [diff] [blame] | 227 | def gen_pack_expr(oftype, value_expr, version): |
| 228 | type_data = lookup_type_data(oftype, version) |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 229 | if type_data and type_data.pack: |
| 230 | return type_data.pack % value_expr |
| 231 | else: |
| 232 | return "loxi.unimplemented('pack %s')" % oftype |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 233 | |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 234 | # Return an unpack expression for the given oftype |
| 235 | # |
| 236 | # 'reader_expr' is a string of Python code which will evaluate to |
| 237 | # the OFReader instance used for deserialization. |
Andreas Wundsam | 69ecfdc | 2013-09-17 13:50:12 -0700 | [diff] [blame] | 238 | def gen_unpack_expr(oftype, reader_expr, version): |
| 239 | type_data = lookup_type_data(oftype, version) |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 240 | if type_data and type_data.unpack: |
| 241 | return type_data.unpack % reader_expr |
| 242 | else: |
| 243 | return "loxi.unimplemented('unpack %s')" % oftype |