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 | ## |
| 29 | # @brief C code generation for LOXI type related maps |
| 30 | # |
| 31 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 32 | import c_gen.of_g_legacy as of_g |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 33 | import sys |
| 34 | from generic_utils import * |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 35 | import c_gen.type_maps as type_maps |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | # Some number larger than small type values, but less then |
| 39 | # reserved values like 0xffff |
| 40 | max_type_value = 1000 |
| 41 | |
| 42 | def gen_object_id_to_type(out): |
| 43 | out.write(""" |
| 44 | /** |
| 45 | * Map from object ID to primary wire type |
| 46 | * |
| 47 | * For messages, this is the header type; in particular for stats, this is |
| 48 | * the common stats request/response type. For per-stats types, use the |
| 49 | * stats type map. For things like actions, instructions or queue-props, |
| 50 | * this gives the "sub type". |
| 51 | */ |
| 52 | """) |
| 53 | for version in of_g.of_version_range: |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 54 | out.write("static const int\nof_object_to_type_map_v%d[OF_OBJECT_COUNT] = {\n" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 55 | %version) |
| 56 | out.write(" -1, /* of_object, not a valid specific type */\n") |
| 57 | for j, cls in enumerate(of_g.all_class_order): |
| 58 | comma = "" |
| 59 | if j < len(of_g.all_class_order) - 1: # Avoid ultimate comma |
| 60 | comma = "," |
| 61 | |
| 62 | if cls in type_maps.stats_reply_list: |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 63 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 64 | (type_maps.type_val[("of_stats_reply", version)], |
| 65 | comma, cls)) |
| 66 | elif cls in type_maps.stats_request_list: |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 67 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 68 | (type_maps.type_val[("of_stats_request", version)], |
| 69 | comma, cls)) |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 70 | elif cls in type_maps.error_msg_list: |
| 71 | out.write(" %d%s /* %s */\n" % |
| 72 | (type_maps.type_val[("of_error_msg", version)], |
| 73 | comma, cls)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 74 | elif cls in type_maps.flow_mod_list: |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 75 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 76 | (type_maps.type_val[("of_flow_mod", version)], |
| 77 | comma, cls)) |
Rich Lane | b8fc0b7 | 2013-10-24 16:55:25 -0700 | [diff] [blame] | 78 | elif (cls, version) in type_maps.type_val and \ |
| 79 | type_maps.type_val[(cls, version)] != type_maps.invalid_type: |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 80 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 81 | (type_maps.type_val[(cls, version)], comma, cls)) |
| 82 | elif type_maps.message_is_extension(cls, version): |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 83 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 84 | (type_maps.type_val[("of_experimenter", version)], |
| 85 | comma, cls)) |
| 86 | elif type_maps.action_is_extension(cls, version): |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 87 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 88 | (type_maps.type_val[("of_action_experimenter", |
| 89 | version)], |
| 90 | comma, cls)) |
| 91 | elif type_maps.action_id_is_extension(cls, version): |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 92 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 93 | (type_maps.type_val[("of_action_id_experimenter", |
| 94 | version)], |
| 95 | comma, cls)) |
| 96 | elif type_maps.instruction_is_extension(cls, version): |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 97 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 98 | (type_maps.type_val[("of_instruction_experimenter", |
| 99 | version)], |
| 100 | comma, cls)) |
| 101 | elif type_maps.queue_prop_is_extension(cls, version): |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 102 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 103 | (type_maps.type_val[("of_queue_prop_experimenter", |
| 104 | version)], |
| 105 | comma, cls)) |
| 106 | elif type_maps.table_feature_prop_is_extension(cls, version): |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 107 | out.write(" %d%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 108 | (type_maps.type_val[("of_table_feature_prop_experimenter", |
| 109 | version)], |
| 110 | comma, cls)) |
| 111 | else: |
| 112 | out.write(" -1%s /* %s (invalid) */\n" % (comma, cls)) |
| 113 | out.write("};\n\n") |
| 114 | |
| 115 | out.write(""" |
| 116 | /** |
| 117 | * Unified map, indexed by wire version which is 1-based. |
| 118 | */ |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 119 | const int *const of_object_to_type_map[OF_VERSION_ARRAY_MAX] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 120 | NULL, |
| 121 | """) |
| 122 | for version in of_g.of_version_range: |
| 123 | out.write(" of_object_to_type_map_v%d,\n" % version) |
| 124 | out.write(""" |
| 125 | }; |
| 126 | """) |
| 127 | |
| 128 | def gen_object_id_to_extension_data(out): |
| 129 | out.write(""" |
| 130 | /** |
| 131 | * Extension data. |
| 132 | * @fixme There must be a better way to represent this data |
| 133 | */ |
| 134 | """) |
| 135 | for version in of_g.of_version_range: |
| 136 | out.write(""" |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 137 | static const of_experimenter_data_t |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 138 | of_object_to_extension_data_v%d[OF_OBJECT_COUNT] = { |
| 139 | """ % version) |
| 140 | out.write(" {0, 0, 0}, /* of_object, not a valid specific type */\n") |
| 141 | for j, cls in enumerate(of_g.all_class_order): |
| 142 | comma = "" |
| 143 | if j < len(of_g.all_class_order) - 1: # Avoid ultimate comma |
| 144 | comma = "," |
| 145 | |
| 146 | if type_maps.class_is_extension(cls, version): |
| 147 | exp_name = type_maps.extension_to_experimenter_macro_name(cls) |
| 148 | subtype = type_maps.extension_to_subtype(cls, version) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 149 | out.write(" {1, %s, %d}%s /* %s */\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 150 | (exp_name, subtype, comma, cls)) |
| 151 | else: |
| 152 | out.write(" {0, 0, 0}%s /* %s (non-extension) */\n" % |
| 153 | (comma, cls)) |
| 154 | out.write("};\n\n") |
| 155 | |
| 156 | out.write(""" |
| 157 | /** |
| 158 | * Unified map, indexed by wire version which is 1-based. |
| 159 | */ |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 160 | const of_experimenter_data_t *const of_object_to_extension_data[OF_VERSION_ARRAY_MAX] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 161 | NULL, |
| 162 | """) |
| 163 | for version in of_g.of_version_range: |
| 164 | out.write(" of_object_to_extension_data_v%d,\n" % version) |
| 165 | out.write(""" |
| 166 | }; |
| 167 | """) |
| 168 | |
| 169 | def gen_type_to_object_id(out, type_str, prefix, template, |
| 170 | value_array, max_val): |
| 171 | """ |
| 172 | Generate C maps from various message class groups to object ids |
| 173 | |
| 174 | For each version, create an array mapping the type info to the |
| 175 | object ID. Then define an array containing those pointers. |
| 176 | """ |
| 177 | |
| 178 | # Create unified arrays and get length |
| 179 | arr_len = type_maps.type_array_len(value_array, max_val) |
| 180 | all_ars = [] |
| 181 | for version, val_dict in value_array.items(): # Per version dict |
| 182 | ar = type_maps.dict_to_array(val_dict, max_val, type_maps.invalid_type) |
| 183 | all_ars.append(ar) |
| 184 | |
| 185 | len_name = "%s_ITEM_COUNT" % prefix |
| 186 | |
| 187 | for i, ar in enumerate(all_ars): |
| 188 | version = i + 1 |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 189 | out.write("static const of_object_id_t\nof_%s_v%d[%s] = {\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 190 | (type_str, version, len_name)) |
| 191 | for i in range(arr_len): |
| 192 | comma = "" |
| 193 | if i < arr_len - 1: # Avoid ultimate comma |
| 194 | comma = "," |
| 195 | |
| 196 | # Per-version length check |
| 197 | if i < len(ar): |
| 198 | v = ar[i] |
| 199 | else: |
| 200 | v = type_maps.invalid_type |
| 201 | |
| 202 | if v == type_maps.invalid_type: |
| 203 | out.write(" %-30s /* %d (Invalid) */\n" % |
| 204 | ("OF_OBJECT_INVALID" + comma, i)) |
| 205 | else: |
| 206 | name = (template % v.upper()) + comma |
| 207 | out.write(" %-30s /* %d */\n" % (name, i)) |
| 208 | out.write("};\n") |
| 209 | |
| 210 | out.write(""" |
| 211 | /** |
| 212 | * Maps from %(c_name)s wire type values to LOCI object ids |
| 213 | * |
| 214 | * Indexed by wire version which is 1-based. |
| 215 | */ |
| 216 | |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 217 | const of_object_id_t *const of_%(name)s[OF_VERSION_ARRAY_MAX] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 218 | NULL, |
| 219 | """ % dict(name=type_str, c_name=prefix.lower())) |
| 220 | for version in of_g.of_version_range: |
| 221 | out.write(" of_%(name)s_v%(version)d,\n" % dict(name=type_str, |
| 222 | version=version)) |
| 223 | out.write(""" |
| 224 | }; |
| 225 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 226 | """ % dict(name=type_str, u_name=type_str.upper(), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 227 | max_val=max_val, c_name=prefix.lower())) |
| 228 | |
| 229 | def gen_type_maps(out): |
| 230 | """ |
| 231 | Generate various type maps |
| 232 | @param out The file handle to write to |
| 233 | """ |
| 234 | |
| 235 | out.write("#include <loci/loci.h>\n\n") |
| 236 | |
| 237 | # Generate maps from wire type values to object IDs |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 238 | gen_type_to_object_id(out, "error_msg_type_to_id", "OF_ERROR_MSG", |
| 239 | "OF_%s_ERROR_MSG", type_maps.error_types, |
| 240 | max_type_value) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 241 | gen_type_to_object_id(out, "action_type_to_id", "OF_ACTION", |
| 242 | "OF_ACTION_%s", type_maps.action_types, |
| 243 | max_type_value) |
| 244 | gen_type_to_object_id(out, "action_id_type_to_id", "OF_ACTION_ID", |
| 245 | "OF_ACTION_ID_%s", type_maps.action_id_types, |
| 246 | max_type_value) |
| 247 | gen_type_to_object_id(out, "instruction_type_to_id", "OF_INSTRUCTION", |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 248 | "OF_INSTRUCTION_%s", type_maps.instruction_types, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 249 | max_type_value) |
| 250 | gen_type_to_object_id(out, "queue_prop_type_to_id", "OF_QUEUE_PROP", |
| 251 | "OF_QUEUE_PROP_%s", type_maps.queue_prop_types, |
| 252 | max_type_value) |
| 253 | gen_type_to_object_id(out, "table_feature_prop_type_to_id", |
| 254 | "OF_TABLE_FEATURE_PROP", |
| 255 | "OF_TABLE_FEATURE_PROP_%s", |
| 256 | type_maps.table_feature_prop_types, |
| 257 | max_type_value) |
| 258 | gen_type_to_object_id(out, "meter_band_type_to_id", "OF_METER_BAND", |
| 259 | "OF_METER_BAND_%s", type_maps.meter_band_types, |
| 260 | max_type_value) |
| 261 | gen_type_to_object_id(out, "hello_elem_type_to_id", "OF_HELLO_ELEM", |
| 262 | "OF_HELLO_ELEM_%s", type_maps.hello_elem_types, |
| 263 | max_type_value) |
| 264 | |
| 265 | # FIXME: Multipart re-organization |
| 266 | gen_type_to_object_id(out, "stats_request_type_to_id", "OF_STATS_REQUEST", |
| 267 | "OF_%s_STATS_REQUEST", type_maps.stats_types, |
| 268 | max_type_value) |
| 269 | gen_type_to_object_id(out, "stats_reply_type_to_id", "OF_STATS_REPLY", |
| 270 | "OF_%s_STATS_REPLY", type_maps.stats_types, |
| 271 | max_type_value) |
| 272 | gen_type_to_object_id(out, "flow_mod_type_to_id", "OF_FLOW_MOD", |
| 273 | "OF_FLOW_%s", type_maps.flow_mod_types, |
| 274 | max_type_value) |
| 275 | gen_type_to_object_id(out, "oxm_type_to_id", "OF_OXM", |
| 276 | "OF_OXM_%s", type_maps.oxm_types, max_type_value) |
| 277 | gen_type_to_object_id(out, "message_type_to_id", "OF_MESSAGE", |
| 278 | "OF_%s", type_maps.message_types, max_type_value) |
| 279 | |
| 280 | gen_object_id_to_type(out) |
| 281 | gen_object_id_to_extension_data(out) |
| 282 | # Don't need array mapping ID to stats types right now; handled directly |
| 283 | # gen_object_id_to_stats_type(out) |
| 284 | |
| 285 | |
| 286 | def gen_type_to_obj_map_functions(out): |
| 287 | """ |
| 288 | Generate the templated static inline type map functions |
| 289 | @param out The file handle to write to |
| 290 | """ |
| 291 | |
| 292 | ################################################################ |
| 293 | # Generate all type-to-object-ID maps in a common way |
| 294 | ################################################################ |
| 295 | map_template = """ |
| 296 | /** |
| 297 | * %(name)s wire type to object ID array. |
| 298 | * Treat as private; use function accessor below |
| 299 | */ |
| 300 | |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 301 | extern const of_object_id_t *const of_%(name)s_type_to_id[OF_VERSION_ARRAY_MAX]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 302 | |
| 303 | #define OF_%(u_name)s_ITEM_COUNT %(ar_len)d\n |
| 304 | |
| 305 | /** |
| 306 | * Map an %(name)s wire value to an OF object |
| 307 | * @param %(name)s The %(name)s type wire value |
| 308 | * @param version The version associated with the check |
| 309 | * @return The %(name)s OF object type |
| 310 | * @return OF_OBJECT_INVALID if type does not map to an object |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 311 | * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 312 | */ |
| 313 | static inline of_object_id_t |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 314 | of_%(name)s_to_object_id(int %(name)s, of_version_t version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 315 | { |
| 316 | if (!OF_VERSION_OKAY(version)) { |
| 317 | return OF_OBJECT_INVALID; |
| 318 | } |
| 319 | if (%(name)s < 0 || %(name)s >= OF_%(u_name)s_ITEM_COUNT) { |
| 320 | return OF_OBJECT_INVALID; |
| 321 | } |
| 322 | |
| 323 | return of_%(name)s_type_to_id[version][%(name)s]; |
| 324 | } |
| 325 | """ |
| 326 | map_with_experimenter_template = """ |
| 327 | /** |
| 328 | * %(name)s wire type to object ID array. |
| 329 | * Treat as private; use function accessor below |
| 330 | */ |
| 331 | |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 332 | extern const of_object_id_t *const of_%(name)s_type_to_id[OF_VERSION_ARRAY_MAX]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 333 | |
| 334 | #define OF_%(u_name)s_ITEM_COUNT %(ar_len)d\n |
| 335 | |
| 336 | /** |
| 337 | * Map an %(name)s wire value to an OF object |
| 338 | * @param %(name)s The %(name)s type wire value |
| 339 | * @param version The version associated with the check |
| 340 | * @return The %(name)s OF object type |
| 341 | * @return OF_OBJECT_INVALID if type does not map to an object |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 342 | * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 343 | */ |
| 344 | static inline of_object_id_t |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 345 | of_%(name)s_to_object_id(int %(name)s, of_version_t version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 346 | { |
| 347 | if (!OF_VERSION_OKAY(version)) { |
| 348 | return OF_OBJECT_INVALID; |
| 349 | } |
| 350 | if (%(name)s == OF_EXPERIMENTER_TYPE) { |
| 351 | return OF_%(u_name)s_EXPERIMENTER; |
| 352 | } |
| 353 | if (%(name)s < 0 || %(name)s >= OF_%(u_name)s_ITEM_COUNT) { |
| 354 | return OF_OBJECT_INVALID; |
| 355 | } |
| 356 | |
| 357 | return of_%(name)s_type_to_id[version][%(name)s]; |
| 358 | } |
| 359 | """ |
| 360 | |
| 361 | stats_template = """ |
| 362 | /** |
| 363 | * %(name)s wire type to object ID array. |
| 364 | * Treat as private; use function accessor below |
| 365 | */ |
| 366 | |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 367 | extern const of_object_id_t *const of_%(name)s_type_to_id[OF_VERSION_ARRAY_MAX]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 368 | |
| 369 | #define OF_%(u_name)s_ITEM_COUNT %(ar_len)d\n |
| 370 | |
| 371 | /** |
| 372 | * Map an %(name)s wire value to an OF object |
| 373 | * @param %(name)s The %(name)s type wire value |
| 374 | * @param version The version associated with the check |
| 375 | * @return The %(name)s OF object type |
| 376 | * @return OF_OBJECT_INVALID if type does not map to an object |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 377 | * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 378 | */ |
| 379 | static inline of_object_id_t |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 380 | of_%(name)s_to_object_id(int %(name)s, of_version_t version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 381 | { |
| 382 | if (!OF_VERSION_OKAY(version)) { |
| 383 | return OF_OBJECT_INVALID; |
| 384 | } |
| 385 | if (%(name)s == OF_EXPERIMENTER_TYPE) { |
| 386 | return OF_EXPERIMENTER_%(u_name)s; |
| 387 | } |
| 388 | if (%(name)s < 0 || %(name)s >= OF_%(u_name)s_ITEM_COUNT) { |
| 389 | return OF_OBJECT_INVALID; |
| 390 | } |
| 391 | |
| 392 | return of_%(name)s_type_to_id[version][%(name)s]; |
| 393 | } |
| 394 | """ |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 395 | |
| 396 | error_msg_template = """ |
| 397 | /** |
| 398 | * %(name)s wire type to object ID array. |
| 399 | * Treat as private; use function accessor below |
| 400 | */ |
| 401 | |
| 402 | extern const of_object_id_t *const of_%(name)s_type_to_id[OF_VERSION_ARRAY_MAX]; |
| 403 | |
| 404 | #define OF_%(u_name)s_ITEM_COUNT %(ar_len)d\n |
| 405 | |
| 406 | /** |
| 407 | * Map an %(name)s wire value to an OF object |
| 408 | * @param %(name)s The %(name)s type wire value |
| 409 | * @param version The version associated with the check |
| 410 | * @return The %(name)s OF object type |
| 411 | * @return OF_OBJECT_INVALID if type does not map to an object |
| 412 | * |
| 413 | */ |
| 414 | static inline of_object_id_t |
| 415 | of_error_msg_to_object_id(uint16_t %(name)s, of_version_t version) |
| 416 | { |
| 417 | if (!OF_VERSION_OKAY(version)) { |
| 418 | return OF_OBJECT_INVALID; |
| 419 | } |
| 420 | if (%(name)s == OF_EXPERIMENTER_TYPE) { |
| 421 | return OF_EXPERIMENTER_ERROR_MSG; |
| 422 | } |
| 423 | if (%(name)s < 0 || %(name)s >= OF_%(u_name)s_ITEM_COUNT) { |
| 424 | return OF_OBJECT_INVALID; |
| 425 | } |
| 426 | |
| 427 | return of_%(name)s_type_to_id[version][%(name)s]; |
| 428 | } |
| 429 | """ |
| 430 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 431 | # Experimenter mapping functions |
| 432 | # Currently we support very few candidates, so we just do a |
| 433 | # list of if/elses |
| 434 | experimenter_function = """ |
| 435 | /** |
| 436 | * @brief Map a message known to be an exp msg to the proper object |
| 437 | * |
| 438 | * Assume that the message is a vendor/experimenter message. Determine |
| 439 | * the specific object type for the message. |
| 440 | * @param msg An OF message object (uint8_t *) |
| 441 | * @param length The number of bytes in the message (for error checking) |
| 442 | * @param version Version of message |
| 443 | * @returns object ID of specific type if recognized or OF_EXPERIMENTER if not |
| 444 | * |
| 445 | * @todo put OF_EXPERIMENTER_<name> in loci_base.h |
| 446 | */ |
| 447 | |
| 448 | static inline of_object_id_t |
| 449 | of_message_experimenter_to_object_id(of_message_t msg, of_version_t version) { |
| 450 | uint32_t experimenter_id; |
| 451 | uint32_t subtype; |
| 452 | |
| 453 | /* Extract experimenter and subtype value; look for match from type maps */ |
| 454 | experimenter_id = of_message_experimenter_id_get(msg); |
| 455 | subtype = of_message_experimenter_subtype_get(msg); |
| 456 | |
| 457 | /* Do a simple if/else search for the ver, experimenter and subtype */ |
| 458 | """ |
| 459 | first = True |
| 460 | for version, experimenter_lists in type_maps.extension_message_subtype.items(): |
| 461 | for exp, subtypes in experimenter_lists.items(): |
| 462 | experimenter_function += """ |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 463 | if ((experimenter_id == OF_EXPERIMENTER_ID_%(exp_name)s) && |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 464 | (version == %(ver_name)s)) { |
| 465 | """ % dict(exp_name=exp.upper(), ver_name=of_g.wire_ver_map[version]) |
| 466 | for ext_msg, subtype in subtypes.items(): |
| 467 | experimenter_function += """ |
| 468 | if (subtype == %(subtype)s) { |
| 469 | return %(ext_msg)s; |
| 470 | } |
| 471 | """ % dict(subtype=subtype, ext_msg=ext_msg.upper()) |
| 472 | experimenter_function += """ |
| 473 | } |
| 474 | """ |
| 475 | experimenter_function += """ |
| 476 | return OF_EXPERIMENTER; |
| 477 | } |
| 478 | """ |
| 479 | |
| 480 | # Message need different handling |
| 481 | msg_template = """ |
| 482 | /** |
| 483 | * %(name)s wire type to object ID array. |
| 484 | * Treat as private; use function accessor below |
| 485 | */ |
| 486 | |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 487 | extern const of_object_id_t *const of_%(name)s_type_to_id[OF_VERSION_ARRAY_MAX]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 488 | |
| 489 | #define OF_%(u_name)s_ITEM_COUNT %(ar_len)d\n |
| 490 | |
| 491 | /** |
| 492 | * Extract the type info from the message and determine its object type |
| 493 | * @param msg An OF message object (uint8_t *) |
| 494 | * @param length The number of bytes in the message (for error checking) |
| 495 | * @returns object ID or OF_OBJECT_INVALID if parse error |
| 496 | */ |
| 497 | |
| 498 | static inline of_object_id_t |
| 499 | of_message_to_object_id(of_message_t msg, int length) { |
| 500 | uint8_t type; |
| 501 | of_version_t ver; |
| 502 | of_object_id_t obj_id; |
| 503 | uint16_t stats_type; |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 504 | uint16_t err_type; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 505 | uint8_t flow_mod_cmd; |
Rich Lane | 353a79f | 2013-11-13 10:39:56 -0800 | [diff] [blame] | 506 | uint32_t experimenter, subtype; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 507 | |
| 508 | if (length < OF_MESSAGE_MIN_LENGTH) { |
| 509 | return OF_OBJECT_INVALID; |
| 510 | } |
| 511 | type = of_message_type_get(msg); |
| 512 | ver = of_message_version_get(msg); |
| 513 | if (!OF_VERSION_OKAY(ver)) { |
| 514 | return OF_OBJECT_INVALID; |
| 515 | } |
| 516 | |
| 517 | if (type >= OF_MESSAGE_ITEM_COUNT) { |
| 518 | return OF_OBJECT_INVALID; |
| 519 | } |
| 520 | |
| 521 | obj_id = of_message_type_to_id[ver][type]; |
| 522 | |
| 523 | /* Remap to specific message if known */ |
| 524 | if (obj_id == OF_EXPERIMENTER) { |
| 525 | if (length < OF_MESSAGE_EXPERIMENTER_MIN_LENGTH) { |
| 526 | return OF_OBJECT_INVALID; |
| 527 | } |
| 528 | return of_message_experimenter_to_object_id(msg, ver); |
| 529 | } |
| 530 | |
| 531 | /* Remap to add/delete/strict version */ |
| 532 | if (obj_id == OF_FLOW_MOD) { |
| 533 | if (length < OF_MESSAGE_MIN_FLOW_MOD_LENGTH(ver)) { |
| 534 | return OF_OBJECT_INVALID; |
| 535 | } |
| 536 | flow_mod_cmd = of_message_flow_mod_command_get(msg, ver); |
| 537 | obj_id = of_flow_mod_to_object_id(flow_mod_cmd, ver); |
| 538 | } |
| 539 | |
| 540 | if ((obj_id == OF_STATS_REQUEST) || (obj_id == OF_STATS_REPLY)) { |
| 541 | if (length < OF_MESSAGE_MIN_STATS_LENGTH) { |
| 542 | return OF_OBJECT_INVALID; |
| 543 | } |
| 544 | stats_type = of_message_stats_type_get(msg); |
Rich Lane | 353a79f | 2013-11-13 10:39:56 -0800 | [diff] [blame] | 545 | if (stats_type == OF_STATS_TYPE_EXPERIMENTER) { |
| 546 | if (length < OF_MESSAGE_STATS_EXPERIMENTER_MIN_LENGTH) { |
| 547 | return OF_OBJECT_INVALID; |
| 548 | } |
| 549 | experimenter = of_message_stats_experimenter_id_get(msg); |
| 550 | subtype = of_message_stats_experimenter_subtype_get(msg); |
| 551 | if (obj_id == OF_STATS_REQUEST) { |
| 552 | obj_id = of_experimenter_stats_request_to_object_id(experimenter, subtype, ver); |
| 553 | } else { |
| 554 | obj_id = of_experimenter_stats_reply_to_object_id(experimenter, subtype, ver); |
| 555 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 556 | } else { |
Rich Lane | 353a79f | 2013-11-13 10:39:56 -0800 | [diff] [blame] | 557 | if (obj_id == OF_STATS_REQUEST) { |
| 558 | obj_id = of_stats_request_to_object_id(stats_type, ver); |
| 559 | } else { |
| 560 | obj_id = of_stats_reply_to_object_id(stats_type, ver); |
| 561 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 565 | if (obj_id == OF_ERROR_MSG) { |
| 566 | if (length < OF_MESSAGE_MIN_ERROR_LENGTH) { |
| 567 | return OF_OBJECT_INVALID; |
| 568 | } |
| 569 | err_type = of_message_error_type_get(msg); |
| 570 | obj_id = of_error_msg_to_object_id(err_type, ver); |
| 571 | } |
| 572 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 573 | return obj_id; |
| 574 | } |
| 575 | """ |
| 576 | |
Rich Lane | d8d29c9 | 2013-09-24 13:46:42 -0700 | [diff] [blame] | 577 | oxm_template = """ |
| 578 | /** |
| 579 | * oxm wire type to object ID array. |
| 580 | * Treat as private; use function accessor below |
| 581 | */ |
| 582 | |
| 583 | extern const of_object_id_t *const of_oxm_type_to_id[OF_VERSION_ARRAY_MAX]; |
| 584 | |
| 585 | #define OF_OXM_ITEM_COUNT %(ar_len)d\n |
| 586 | |
| 587 | /** |
| 588 | * Map an oxm wire value to an OF object |
| 589 | * @param oxm The oxm type wire value |
| 590 | * @param version The version associated with the check |
| 591 | * @return The oxm OF object type |
| 592 | * @return OF_OBJECT_INVALID if type does not map to an object |
| 593 | * |
| 594 | */ |
| 595 | static inline of_object_id_t |
| 596 | of_oxm_to_object_id(uint32_t type_len, of_version_t version) |
| 597 | { |
| 598 | if (!OF_VERSION_OKAY(version)) { |
| 599 | return OF_OBJECT_INVALID; |
| 600 | } |
| 601 | |
| 602 | uint16_t class = (type_len >> 16) & 0xffff; |
| 603 | uint8_t masked_type = (type_len >> 8) & 0xff; |
| 604 | |
| 605 | if (class == 0x8000) { |
| 606 | if (masked_type < 0 || masked_type >= OF_OXM_ITEM_COUNT) { |
| 607 | return OF_OBJECT_INVALID; |
| 608 | } |
| 609 | |
| 610 | return of_oxm_type_to_id[version][masked_type]; |
| 611 | } else if (class == 0x0003) { |
| 612 | switch (masked_type) { |
| 613 | case 0x00: return OF_OXM_BSN_IN_PORTS_128; |
| 614 | case 0x01: return OF_OXM_BSN_IN_PORTS_128_MASKED; |
Rich Lane | 6171836 | 2013-10-24 16:59:42 -0700 | [diff] [blame] | 615 | case 0x02: return OF_OXM_BSN_LAG_ID; |
| 616 | case 0x03: return OF_OXM_BSN_LAG_ID_MASKED; |
Rich Lane | eb21c4f | 2013-10-28 17:34:41 -0700 | [diff] [blame] | 617 | case 0x04: return OF_OXM_BSN_VRF; |
| 618 | case 0x05: return OF_OXM_BSN_VRF_MASKED; |
| 619 | case 0x06: return OF_OXM_BSN_GLOBAL_VRF_ALLOWED; |
| 620 | case 0x07: return OF_OXM_BSN_GLOBAL_VRF_ALLOWED_MASKED; |
| 621 | case 0x08: return OF_OXM_BSN_L3_INTERFACE_CLASS_ID; |
| 622 | case 0x09: return OF_OXM_BSN_L3_INTERFACE_CLASS_ID_MASKED; |
| 623 | case 0x0a: return OF_OXM_BSN_L3_SRC_CLASS_ID; |
| 624 | case 0x0b: return OF_OXM_BSN_L3_SRC_CLASS_ID_MASKED; |
| 625 | case 0x0c: return OF_OXM_BSN_L3_DST_CLASS_ID; |
| 626 | case 0x0d: return OF_OXM_BSN_L3_DST_CLASS_ID_MASKED; |
Rich Lane | d8d29c9 | 2013-09-24 13:46:42 -0700 | [diff] [blame] | 627 | default: return OF_OBJECT_INVALID; |
| 628 | } |
| 629 | } else { |
| 630 | return OF_OBJECT_INVALID; |
| 631 | } |
| 632 | } |
| 633 | """ |
| 634 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 635 | # Action types array gen |
| 636 | ar_len = type_maps.type_array_len(type_maps.action_types, max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 637 | out.write(map_with_experimenter_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 638 | dict(name="action", u_name="ACTION", ar_len=ar_len)) |
| 639 | |
| 640 | # Action ID types array gen |
| 641 | ar_len = type_maps.type_array_len(type_maps.action_id_types, max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 642 | out.write(map_with_experimenter_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 643 | dict(name="action_id", u_name="ACTION_ID", ar_len=ar_len)) |
| 644 | |
| 645 | # Instruction types array gen |
| 646 | ar_len = type_maps.type_array_len(type_maps.instruction_types, |
| 647 | max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 648 | out.write(map_with_experimenter_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 649 | dict(name="instruction", u_name="INSTRUCTION", ar_len=ar_len)) |
| 650 | |
| 651 | # Queue prop types array gen |
| 652 | ar_len = type_maps.type_array_len(type_maps.queue_prop_types, |
| 653 | max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 654 | out.write(map_with_experimenter_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 655 | dict(name="queue_prop", u_name="QUEUE_PROP", ar_len=ar_len)) |
| 656 | |
| 657 | # Table feature prop types array gen |
| 658 | ar_len = type_maps.type_array_len(type_maps.table_feature_prop_types, |
| 659 | max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 660 | out.write(map_with_experimenter_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 661 | dict(name="table_feature_prop", u_name="TABLE_FEATURE_PROP", |
| 662 | ar_len=ar_len)) |
| 663 | |
| 664 | # Meter band types array gen |
| 665 | ar_len = type_maps.type_array_len(type_maps.meter_band_types, |
| 666 | max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 667 | out.write(map_with_experimenter_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 668 | dict(name="meter_band", u_name="METER_BAND", ar_len=ar_len)) |
| 669 | |
| 670 | # Hello elem types array gen |
| 671 | ar_len = type_maps.type_array_len(type_maps.hello_elem_types, |
| 672 | max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 673 | out.write(map_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 674 | dict(name="hello_elem", u_name="HELLO_ELEM", ar_len=ar_len)) |
| 675 | |
| 676 | # Stats types array gen |
| 677 | ar_len = type_maps.type_array_len(type_maps.stats_types, |
| 678 | max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 679 | out.write(stats_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 680 | dict(name="stats_reply", u_name="STATS_REPLY", ar_len=ar_len)) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 681 | out.write(stats_template % |
| 682 | dict(name="stats_request", u_name="STATS_REQUEST", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 683 | ar_len=ar_len)) |
| 684 | |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 685 | ar_len = type_maps.type_array_len(type_maps.error_types, |
| 686 | max_type_value) |
| 687 | out.write(error_msg_template % |
| 688 | dict(name="error_msg", u_name="ERROR_MSG", ar_len=ar_len)) |
| 689 | # out.write(error_msg_function) |
| 690 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 691 | ar_len = type_maps.type_array_len(type_maps.flow_mod_types, max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 692 | out.write(map_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 693 | dict(name="flow_mod", u_name="FLOW_MOD", ar_len=ar_len)) |
| 694 | |
Rich Lane | d8d29c9 | 2013-09-24 13:46:42 -0700 | [diff] [blame] | 695 | # OXM |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 696 | ar_len = type_maps.type_array_len(type_maps.oxm_types, max_type_value) |
| 697 | out.write(""" |
| 698 | /* NOTE: We could optimize the OXM and only generate OF 1.2 versions. */ |
| 699 | """) |
Rich Lane | d8d29c9 | 2013-09-24 13:46:42 -0700 | [diff] [blame] | 700 | out.write(oxm_template % dict(ar_len=ar_len)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 701 | |
Rich Lane | d8d29c9 | 2013-09-24 13:46:42 -0700 | [diff] [blame] | 702 | # Messages |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 703 | out.write(experimenter_function) |
| 704 | # Must follow stats reply/request |
| 705 | ar_len = type_maps.type_array_len(type_maps.message_types, max_type_value) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 706 | out.write(msg_template % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 707 | dict(name="message", u_name="MESSAGE", ar_len=ar_len)) |
| 708 | |
| 709 | def gen_obj_to_type_map_functions(out): |
| 710 | """ |
| 711 | Generate the static line maps from object IDs to types |
| 712 | @param out The file handle to write to |
| 713 | """ |
| 714 | |
| 715 | ################################################################ |
| 716 | # Generate object ID to primary type map |
| 717 | ################################################################ |
| 718 | |
| 719 | out.write(""" |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 720 | extern const int *const of_object_to_type_map[OF_VERSION_ARRAY_MAX]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 721 | |
| 722 | /** |
| 723 | * Map an object ID to its primary wire type value |
| 724 | * @param id An object ID |
| 725 | * @return For message objects, the type value in the OpenFlow header |
| 726 | * @return For non-message objects such as actions, instructions, OXMs |
| 727 | * returns the type value that appears in the respective sub-header |
| 728 | * @return -1 For improper version or out of bounds input |
| 729 | * |
| 730 | * NOTE that for stats request/reply, returns the header type, not the |
| 731 | * sub-type |
| 732 | * |
| 733 | * Also, note that the value is returned as a signed integer. So -1 is |
| 734 | * an error code, while 0xffff is the usual "experimenter" code. |
| 735 | */ |
| 736 | static inline int |
| 737 | of_object_to_wire_type(of_object_id_t id, of_version_t version) |
| 738 | { |
| 739 | if (!OF_VERSION_OKAY(version)) { |
| 740 | return -1; |
| 741 | } |
| 742 | if (id < 0 || id >= OF_OBJECT_COUNT) { |
| 743 | return -1; |
| 744 | } |
| 745 | return of_object_to_type_map[version][id]; |
| 746 | } |
| 747 | |
| 748 | """) |
| 749 | |
| 750 | # Now for experimenter ids |
| 751 | out.write(""" |
| 752 | /** |
| 753 | * Map from object ID to a triple, (is_extension, experimenter id, subtype) |
| 754 | */ |
| 755 | """) |
| 756 | out.write(""" |
| 757 | typedef struct of_experimenter_data_s { |
| 758 | int is_extension; /* Boolean indication that this is an extension */ |
| 759 | uint32_t experimenter_id; |
| 760 | uint32_t subtype; |
| 761 | } of_experimenter_data_t; |
| 762 | |
| 763 | """) |
| 764 | |
| 765 | out.write(""" |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 766 | extern const of_experimenter_data_t *const of_object_to_extension_data[OF_VERSION_ARRAY_MAX]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 767 | |
| 768 | /** |
| 769 | * Map from the object ID of an extension to the experimenter ID |
| 770 | */ |
| 771 | static inline uint32_t |
| 772 | of_extension_to_experimenter_id(of_object_id_t obj_id, of_version_t ver) |
| 773 | { |
| 774 | if (obj_id < 0 || obj_id > OF_OBJECT_COUNT) { |
| 775 | return (uint32_t) -1; |
| 776 | } |
| 777 | /* @fixme: Verify ver? */ |
| 778 | return of_object_to_extension_data[ver][obj_id].experimenter_id; |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * Map from the object ID of an extension to the experimenter subtype |
| 783 | */ |
| 784 | static inline uint32_t |
| 785 | of_extension_to_experimenter_subtype(of_object_id_t obj_id, of_version_t ver) |
| 786 | { |
| 787 | if (obj_id < 0 || obj_id > OF_OBJECT_COUNT) { |
| 788 | return (uint32_t) -1; |
| 789 | } |
| 790 | /* @fixme: Verify ver? */ |
| 791 | return of_object_to_extension_data[ver][obj_id].subtype; |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Boolean function indicating the the given object ID/version |
| 796 | * is recognized as a supported (decode-able) extension. |
| 797 | */ |
| 798 | static inline int |
| 799 | of_object_id_is_extension(of_object_id_t obj_id, of_version_t ver) |
| 800 | { |
| 801 | if (obj_id < 0 || obj_id > OF_OBJECT_COUNT) { |
| 802 | return (uint32_t) -1; |
| 803 | } |
| 804 | /* @fixme: Verify ver? */ |
| 805 | return of_object_to_extension_data[ver][obj_id].is_extension; |
| 806 | } |
| 807 | """) |
| 808 | |
| 809 | ################################################################ |
| 810 | # Generate object ID to the stats sub-type map |
| 811 | ################################################################ |
| 812 | |
| 813 | out.write(""" |
| 814 | /** |
| 815 | * Map an object ID to a stats type |
| 816 | * @param id An object ID |
| 817 | * @return The wire value for the stats type |
| 818 | * @return -1 if not supported for this version |
| 819 | * @return -1 if id is not a specific stats type ID |
| 820 | * |
| 821 | * Note that the value is returned as a signed integer. So -1 is |
| 822 | * an error code, while 0xffff is the usual "experimenter" code. |
| 823 | */ |
| 824 | |
| 825 | static inline int |
| 826 | of_object_to_stats_type(of_object_id_t id, of_version_t version) |
| 827 | { |
| 828 | if (!OF_VERSION_OKAY(version)) { |
| 829 | return -1; |
| 830 | } |
| 831 | switch (id) { |
| 832 | """) |
| 833 | # Assumes 1.2 contains all stats types and type values are |
| 834 | # the same across all versions |
| 835 | stats_names = dict() |
| 836 | for ver in of_g.of_version_range: |
| 837 | for name, value in type_maps.stats_types[ver].items(): |
| 838 | if name in stats_names and (not value == stats_names[name]): |
| 839 | print "ERROR stats type differ violating assumption" |
| 840 | sys.exit(1) |
| 841 | stats_names[name] = value |
| 842 | |
| 843 | for name, value in stats_names.items(): |
| 844 | out.write(" case OF_%s_STATS_REPLY:\n" % name.upper()) |
| 845 | out.write(" case OF_%s_STATS_REQUEST:\n" % name.upper()) |
| 846 | for version in of_g.of_version_range: |
| 847 | if not name in type_maps.stats_types[version]: |
| 848 | out.write(" if (version == %s) break;\n" % |
| 849 | of_g.of_version_wire2name[version]) |
| 850 | out.write(" return %d;\n" % value) |
| 851 | out.write(""" |
| 852 | default: |
| 853 | break; |
| 854 | } |
| 855 | return -1; /* Not recognized as stats type object for this version */ |
| 856 | } |
| 857 | """) |
| 858 | |
| 859 | ################################################################ |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 860 | # Generate object ID to the error sub-type map |
| 861 | ################################################################ |
| 862 | out.write(""" |
| 863 | /** |
| 864 | * Map an object ID to an error type |
| 865 | * @param id An object ID |
| 866 | * @return The wire value for the error type |
| 867 | * @return -1 if not supported for this version |
| 868 | * @return -1 if id is not a specific error type ID |
| 869 | * |
| 870 | * Note that the value is returned as a signed integer. So -1 is |
| 871 | * an error code, while 0xffff is the usual "experimenter" code. |
| 872 | */ |
| 873 | |
| 874 | static inline int |
| 875 | of_object_to_error_type(of_object_id_t id, of_version_t version) |
| 876 | { |
| 877 | if (!OF_VERSION_OKAY(version)) { |
| 878 | return -1; |
| 879 | } |
| 880 | switch (id) {""") |
| 881 | error_names = set() |
| 882 | for ver in of_g.of_version_range: |
| 883 | for name in type_maps.error_types[ver]: |
| 884 | error_names.add(name) |
| 885 | for name in error_names: |
| 886 | out.write(""" |
| 887 | case OF_%(name)s_ERROR_MSG: |
| 888 | if (OF_ERROR_TYPE_%(name)s_SUPPORTED(version)) |
| 889 | return OF_ERROR_TYPE_%(name)s_BY_VERSION(version); |
| 890 | break;""" % {"name": name.upper()}) |
| 891 | out.write(""" |
| 892 | default: |
| 893 | break; |
| 894 | } |
| 895 | return -1; /* Not recognized as error type object for this version */ |
| 896 | } |
| 897 | """) |
| 898 | |
| 899 | ################################################################ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 900 | # Generate object ID to the flow mod sub-type map |
| 901 | ################################################################ |
| 902 | |
| 903 | out.write(""" |
| 904 | /** |
| 905 | * Map an object ID to a flow-mod command value |
| 906 | * @param id An object ID |
| 907 | * @return The wire value for the flow-mod command |
| 908 | * @return -1 if not supported for this version |
| 909 | * @return -1 if id is not a specific stats type ID |
| 910 | * |
| 911 | * Note that the value is returned as a signed integer. So -1 is |
| 912 | * an error code, while 0xffff is the usual "experimenter" code. |
| 913 | */ |
| 914 | |
| 915 | static inline int |
| 916 | of_object_to_flow_mod_command(of_object_id_t id, of_version_t version) |
| 917 | { |
| 918 | if (!OF_VERSION_OKAY(version)) { |
| 919 | return -1; |
| 920 | } |
| 921 | switch (id) { |
| 922 | """) |
| 923 | # Assumes 1.2 contains all stats types and type values are |
| 924 | # the same across all versions |
| 925 | flow_mod_names = dict() |
| 926 | for ver in of_g.of_version_range: |
| 927 | for name, value in type_maps.flow_mod_types[ver].items(): |
| 928 | if name in flow_mod_names and \ |
| 929 | (not value == flow_mod_names[name]): |
| 930 | print "ERROR flow mod command differ violating assumption" |
| 931 | sys.exit(1) |
| 932 | flow_mod_names[name] = value |
| 933 | |
| 934 | for name, value in flow_mod_names.items(): |
| 935 | out.write(" case OF_FLOW_%s:\n" % name.upper()) |
| 936 | for version in of_g.of_version_range: |
| 937 | if not name in type_maps.flow_mod_types[version]: |
| 938 | out.write(" if (version == %s) break;\n" % |
| 939 | of_g.of_version_wire2name[version]) |
| 940 | out.write(" return %d;\n" % value) |
| 941 | out.write(""" |
| 942 | default: |
| 943 | break; |
| 944 | } |
| 945 | return -1; /* Not recognized as flow mod type object for this version */ |
| 946 | } |
| 947 | |
| 948 | """) |
| 949 | |
| 950 | def gen_type_maps_header(out): |
| 951 | """ |
| 952 | Generate various header file declarations for type maps |
| 953 | @param out The file handle to write to |
| 954 | """ |
| 955 | |
| 956 | out.write(""" |
| 957 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 958 | * Generic experimenter type value. Applies to all except |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 959 | * top level message: Action, instruction, error, stats, queue_props, oxm |
| 960 | */ |
| 961 | #define OF_EXPERIMENTER_TYPE 0xffff |
Rich Lane | 353a79f | 2013-11-13 10:39:56 -0800 | [diff] [blame] | 962 | |
| 963 | int of_experimenter_stats_request_to_object_id(uint32_t experimenter, uint32_t subtype, int ver); |
| 964 | int of_experimenter_stats_reply_to_object_id(uint32_t experimenter, uint32_t subtype, int ver); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 965 | """) |
| 966 | gen_type_to_obj_map_functions(out) |
| 967 | gen_obj_to_type_map_functions(out) |
| 968 | |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 969 | out.write("extern const int *const of_object_fixed_len[OF_VERSION_ARRAY_MAX];\n") |
Rich Lane | f70be94 | 2013-07-18 13:33:14 -0700 | [diff] [blame] | 970 | out.write("extern const int *const of_object_extra_len[OF_VERSION_ARRAY_MAX];\n") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 971 | |
| 972 | out.write(""" |
| 973 | /** |
| 974 | * Map a message in a wire buffer object to its OF object id. |
| 975 | * @param wbuf Pointer to a wire buffer object, populated with an OF message |
| 976 | * @returns The object ID of the message |
| 977 | * @returns OF_OBJECT_INVALID if unable to parse the message type |
| 978 | */ |
| 979 | |
| 980 | static inline of_object_id_t |
| 981 | of_wire_object_id_get(of_wire_buffer_t *wbuf) |
| 982 | { |
| 983 | of_message_t msg; |
| 984 | |
| 985 | msg = (of_message_t)WBUF_BUF(wbuf); |
| 986 | return of_message_to_object_id(msg, WBUF_CURRENT_BYTES(wbuf)); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Use the type/length from the wire buffer and init the object |
| 991 | * @param obj The object being initialized |
| 992 | * @param base_object_id If > 0, this indicates the base object |
| 993 | * @param max_len If > 0, the max length to expect for the obj |
| 994 | * type for inheritance checking |
| 995 | * @return OF_ERROR_ |
| 996 | * |
| 997 | * Used for inheritance type objects such as actions and OXMs |
| 998 | * The type is checked and if valid, the object is initialized. |
| 999 | * Then the length is taken from the buffer. |
| 1000 | * |
| 1001 | * Note that the object version must already be properly set. |
| 1002 | */ |
| 1003 | static inline int |
| 1004 | of_object_wire_init(of_object_t *obj, of_object_id_t base_object_id, |
| 1005 | int max_len) |
| 1006 | { |
| 1007 | if (obj->wire_type_get != NULL) { |
| 1008 | of_object_id_t id; |
| 1009 | obj->wire_type_get(obj, &id); |
| 1010 | if (!of_wire_id_valid(id, base_object_id)) { |
| 1011 | return OF_ERROR_PARSE; |
| 1012 | } |
| 1013 | obj->object_id = id; |
| 1014 | /* Call the init function for this object type; do not push to wire */ |
| 1015 | of_object_init_map[id]((of_object_t *)(obj), obj->version, -1, 0); |
| 1016 | } |
| 1017 | if (obj->wire_length_get != NULL) { |
| 1018 | int length; |
| 1019 | obj->wire_length_get(obj, &length); |
| 1020 | if (length < 0 || (max_len > 0 && length > max_len)) { |
| 1021 | return OF_ERROR_PARSE; |
| 1022 | } |
| 1023 | obj->length = length; |
| 1024 | } else { |
| 1025 | /* @fixme Does this cover everything else? */ |
| 1026 | obj->length = of_object_fixed_len[obj->version][base_object_id]; |
| 1027 | } |
| 1028 | |
| 1029 | return OF_ERROR_NONE; |
| 1030 | } |
| 1031 | |
| 1032 | """) |
| 1033 | |
| 1034 | # Generate the function that sets the object type fields |
| 1035 | out.write(""" |
| 1036 | |
| 1037 | /** |
| 1038 | * Map a message in a wire buffer object to its OF object id. |
| 1039 | * @param wbuf Pointer to a wire buffer object, populated with an OF message |
| 1040 | * @returns The object ID of the message |
| 1041 | * @returns OF_OBJECT_INVALID if unable to parse the message type |
| 1042 | * |
| 1043 | * Version must be set in the buffer prior to calling this routine |
| 1044 | */ |
| 1045 | |
| 1046 | static inline int |
| 1047 | of_wire_message_object_id_set(of_wire_buffer_t *wbuf, of_object_id_t id) |
| 1048 | { |
| 1049 | int type; |
| 1050 | of_version_t ver; |
| 1051 | of_message_t msg; |
| 1052 | |
| 1053 | msg = (of_message_t)WBUF_BUF(wbuf); |
| 1054 | |
| 1055 | ver = of_message_version_get(msg); |
| 1056 | |
| 1057 | /* ASSERT(id is a message object) */ |
| 1058 | |
| 1059 | if ((type = of_object_to_wire_type(id, ver)) < 0) { |
| 1060 | return OF_ERROR_PARAM; |
| 1061 | } |
| 1062 | of_message_type_set(msg, type); |
| 1063 | |
| 1064 | if ((type = of_object_to_stats_type(id, ver)) >= 0) { |
| 1065 | /* It's a stats obj */ |
| 1066 | of_message_stats_type_set(msg, type); |
Rich Lane | 353a79f | 2013-11-13 10:39:56 -0800 | [diff] [blame] | 1067 | if (type == OF_STATS_TYPE_EXPERIMENTER) { |
| 1068 | switch (id) { |
| 1069 | case OF_BSN_LACP_STATS_REQUEST: |
| 1070 | case OF_BSN_LACP_STATS_REPLY: |
| 1071 | of_message_stats_experimenter_id_set(msg, OF_EXPERIMENTER_ID_BSN); |
| 1072 | of_message_stats_experimenter_subtype_set(msg, 1); |
| 1073 | break; |
| 1074 | default: |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1078 | } |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 1079 | if ((type = of_object_to_error_type(id, ver)) >= 0) { |
Rob Vaterlaus | b5e8c83 | 2013-10-04 12:50:23 -0700 | [diff] [blame] | 1080 | /* It's an error obj */ |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 1081 | of_message_error_type_set(msg, type); |
| 1082 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1083 | if ((type = of_object_to_flow_mod_command(id, ver)) >= 0) { |
| 1084 | /* It's a flow mod obj */ |
| 1085 | of_message_flow_mod_command_set(msg, ver, type); |
| 1086 | } |
| 1087 | if (of_object_id_is_extension(id, ver)) { |
| 1088 | uint32_t val32; |
| 1089 | |
| 1090 | /* Set the experimenter and subtype codes */ |
| 1091 | val32 = of_extension_to_experimenter_id(id, ver); |
| 1092 | of_message_experimenter_id_set(msg, val32); |
| 1093 | val32 = of_extension_to_experimenter_subtype(id, ver); |
| 1094 | of_message_experimenter_subtype_set(msg, val32); |
| 1095 | } |
| 1096 | |
| 1097 | return OF_ERROR_NONE; |
| 1098 | } |
| 1099 | """) |
| 1100 | |
| 1101 | def gen_type_data_header(out): |
| 1102 | |
| 1103 | out.write(""" |
| 1104 | /**************************************************************** |
| 1105 | * |
| 1106 | * The following declarations are for type and length calculations. |
| 1107 | * Implementations may be found in of_type_maps.c |
| 1108 | * |
| 1109 | ****************************************************************/ |
| 1110 | /* |
| 1111 | * Special case length functions for objects with |
| 1112 | */ |
| 1113 | """) |
| 1114 | for ((cls, name), prev) in of_g.special_offsets.items(): |
| 1115 | s_cls = cls[3:] # take off of_ |
| 1116 | out.write(""" |
| 1117 | /** |
| 1118 | * Special length calculation for %(cls)s->%(name)s. |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1119 | * @param obj An object of type %(cls)s to check for |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1120 | * length of %(name)s |
| 1121 | * @param bytes[out] Where to store the calculated length |
| 1122 | * |
| 1123 | * Preceding data member is %(prev)s. |
| 1124 | */ |
| 1125 | extern int of_length_%(s_cls)s_%(name)s_get( |
| 1126 | %(cls)s_t *obj, int *bytes); |
| 1127 | |
| 1128 | /** |
| 1129 | * Special offset calculation for %(cls)s->%(name)s. |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1130 | * @param obj An object of type %(cls)s to check for |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1131 | * length of %(name)s |
| 1132 | * @param offset[out] Where to store the calculated length |
| 1133 | * |
| 1134 | * Preceding data member is %(prev)s. |
| 1135 | */ |
| 1136 | extern int of_offset_%(s_cls)s_%(name)s_get( |
| 1137 | %(cls)s_t *obj, int *offset); |
| 1138 | """ % dict(cls=cls, s_cls=s_cls, name=name, prev=prev)) |
| 1139 | |
| 1140 | # NOT NEEDED YET |
| 1141 | # # For non-message, variable length objects, give a fun that |
| 1142 | # # calculates the length |
| 1143 | # for cls in of_g.standard_class_order: |
| 1144 | # s_cls = cls[3:] # take off of_ |
| 1145 | # if !type_is_var_len(cls, version): |
| 1146 | # continue |
| 1147 | # out.write(""" |
| 1148 | # /** |
| 1149 | # * Special length calculation for variable length object %(cls)s |
| 1150 | # * @param obj An object of type %(cls)s whose length is being calculated |
| 1151 | # * @param bytes[out] Where to store the calculated length |
| 1152 | # * |
| 1153 | # * The assumption is that the length member of the object is not |
| 1154 | # * valid and the length needs to be calculated from other information |
| 1155 | # * such as the parent. |
| 1156 | # */ |
| 1157 | # extern int of_length_%(s_cls)s_get( |
| 1158 | # %(cls)s_t *obj, int *bytes); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1159 | # """ % dict(cls=cls, s_cls=s_cls)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1160 | |
| 1161 | out.write(""" |
| 1162 | /**************************************************************** |
| 1163 | * Wire type/length functions. |
| 1164 | ****************************************************************/ |
| 1165 | |
| 1166 | extern void of_object_message_wire_length_get(of_object_t *obj, int *bytes); |
| 1167 | extern void of_object_message_wire_length_set(of_object_t *obj, int bytes); |
| 1168 | |
| 1169 | extern void of_oxm_wire_length_get(of_object_t *obj, int *bytes); |
| 1170 | extern void of_oxm_wire_length_set(of_object_t *obj, int bytes); |
| 1171 | extern void of_oxm_wire_object_id_get(of_object_t *obj, of_object_id_t *id); |
| 1172 | extern void of_oxm_wire_object_id_set(of_object_t *obj, of_object_id_t id); |
| 1173 | |
| 1174 | extern void of_tlv16_wire_length_get(of_object_t *obj, int *bytes); |
| 1175 | extern void of_tlv16_wire_length_set(of_object_t *obj, int bytes); |
| 1176 | |
| 1177 | extern void of_tlv16_wire_object_id_set(of_object_t *obj, of_object_id_t id); |
| 1178 | |
| 1179 | /* Wire length is uint16 at front of structure */ |
| 1180 | extern void of_u16_len_wire_length_get(of_object_t *obj, int *bytes); |
| 1181 | extern void of_u16_len_wire_length_set(of_object_t *obj, int bytes); |
| 1182 | |
| 1183 | extern void of_action_wire_object_id_get(of_object_t *obj, of_object_id_t *id); |
| 1184 | extern void of_action_id_wire_object_id_get(of_object_t *obj, of_object_id_t *id); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1185 | extern void of_instruction_wire_object_id_get(of_object_t *obj, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1186 | of_object_id_t *id); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1187 | extern void of_queue_prop_wire_object_id_get(of_object_t *obj, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1188 | of_object_id_t *id); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1189 | extern void of_table_feature_prop_wire_object_id_get(of_object_t *obj, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1190 | of_object_id_t *id); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1191 | extern void of_meter_band_wire_object_id_get(of_object_t *obj, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1192 | of_object_id_t *id); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1193 | extern void of_hello_elem_wire_object_id_get(of_object_t *obj, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1194 | of_object_id_t *id); |
| 1195 | |
Rich Lane | 4708572 | 2013-07-12 16:27:04 -0700 | [diff] [blame] | 1196 | #define OF_OXM_LENGTH_GET(hdr) (((hdr) & 0xff) + 4) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1197 | #define OF_OXM_LENGTH_SET(hdr, val) \\ |
Rich Lane | 4708572 | 2013-07-12 16:27:04 -0700 | [diff] [blame] | 1198 | (hdr) = ((hdr) & 0xffffff00) + (((val) - 4) & 0xff) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1199 | |
| 1200 | extern void of_packet_queue_wire_length_get(of_object_t *obj, int *bytes); |
| 1201 | extern void of_packet_queue_wire_length_set(of_object_t *obj, int bytes); |
| 1202 | |
| 1203 | extern void of_list_meter_band_stats_wire_length_get(of_object_t *obj, |
| 1204 | int *bytes); |
| 1205 | extern void of_meter_stats_wire_length_get(of_object_t *obj, int *bytes); |
| 1206 | extern void of_meter_stats_wire_length_set(of_object_t *obj, int bytes); |
| 1207 | extern int of_extension_object_wire_push(of_object_t *obj); |
| 1208 | |
| 1209 | """) |
| 1210 | |
| 1211 | |
| 1212 | def gen_length_array(out): |
| 1213 | """ |
| 1214 | Generate an array giving the lengths of all objects/versions |
| 1215 | @param out The file handle to which to write |
| 1216 | """ |
| 1217 | out.write(""" |
| 1218 | /** |
| 1219 | * An array with the number of bytes in the fixed length part |
| 1220 | * of each OF object |
| 1221 | */ |
| 1222 | """) |
| 1223 | |
| 1224 | for version in of_g.of_version_range: |
| 1225 | out.write(""" |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 1226 | static const int\nof_object_fixed_len_v%d[OF_OBJECT_COUNT] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1227 | -1, /* of_object is not instantiable */ |
| 1228 | """ % version) |
| 1229 | for i, cls in enumerate(of_g.all_class_order): |
| 1230 | comma = "," |
| 1231 | if i == len(of_g.all_class_order) - 1: |
| 1232 | comma = "" |
| 1233 | val = "-1" + comma |
| 1234 | if (cls, version) in of_g.base_length: |
| 1235 | val = str(of_g.base_length[(cls, version)]) + comma |
| 1236 | out.write(" %-5s /* %d: %s */\n" % (val, i + 1, cls)) |
| 1237 | out.write("};\n") |
| 1238 | |
| 1239 | out.write(""" |
| 1240 | /** |
| 1241 | * Unified map of fixed length part of each object |
| 1242 | */ |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 1243 | const int *const of_object_fixed_len[OF_VERSION_ARRAY_MAX] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1244 | NULL, |
| 1245 | """) |
| 1246 | for version in of_g.of_version_range: |
| 1247 | out.write(" of_object_fixed_len_v%d,\n" % version) |
| 1248 | out.write(""" |
| 1249 | }; |
| 1250 | """) |
| 1251 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1252 | |
Rich Lane | f70be94 | 2013-07-18 13:33:14 -0700 | [diff] [blame] | 1253 | def gen_extra_length_array(out): |
| 1254 | """ |
| 1255 | Generate an array giving the extra lengths of all objects/versions |
| 1256 | @param out The file handle to which to write |
| 1257 | """ |
| 1258 | out.write(""" |
| 1259 | /** |
| 1260 | * An array with the number of bytes in the extra length part |
| 1261 | * of each OF object |
| 1262 | */ |
| 1263 | """) |
| 1264 | |
| 1265 | for version in of_g.of_version_range: |
| 1266 | out.write(""" |
| 1267 | static const int\nof_object_extra_len_v%d[OF_OBJECT_COUNT] = { |
| 1268 | -1, /* of_object is not instantiable */ |
| 1269 | """ % version) |
| 1270 | for i, cls in enumerate(of_g.all_class_order): |
| 1271 | comma = "," |
| 1272 | if i == len(of_g.all_class_order) - 1: |
| 1273 | comma = "" |
| 1274 | val = "-1" + comma |
| 1275 | if (cls, version) in of_g.base_length: |
| 1276 | val = str(of_g.extra_length.get((cls, version), 0)) + comma |
| 1277 | out.write(" %-5s /* %d: %s */\n" % (val, i + 1, cls)) |
| 1278 | out.write("};\n") |
| 1279 | |
| 1280 | out.write(""" |
| 1281 | /** |
| 1282 | * Unified map of extra length part of each object |
| 1283 | */ |
| 1284 | const int *const of_object_extra_len[OF_VERSION_ARRAY_MAX] = { |
| 1285 | NULL, |
| 1286 | """) |
| 1287 | for version in of_g.of_version_range: |
| 1288 | out.write(" of_object_extra_len_v%d,\n" % version) |
| 1289 | out.write(""" |
| 1290 | }; |
| 1291 | """) |
| 1292 | |
| 1293 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1294 | ################################################################ |
| 1295 | ################################################################ |
| 1296 | |
| 1297 | # THIS IS PROBABLY NOT NEEDED AND MAY NOT BE CALLED CURRENTLY |
| 1298 | def gen_object_id_to_stats_type(out): |
| 1299 | out.write(""" |
| 1300 | /** |
| 1301 | * Map from message object ID to stats type |
| 1302 | * |
| 1303 | * All message object IDs are mapped for simplicity |
| 1304 | */ |
| 1305 | """) |
| 1306 | for version in of_g.of_version_range: |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 1307 | out.write("const int *of_object_to_stats_type_map_v%d = {\n" % (i+1)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1308 | out.write(" -1, /* of_object (invalid) */\n"); |
| 1309 | for cls in of_g.ordered_messages: |
| 1310 | name = cls[3:] |
| 1311 | name = name[:name.find("_stats")] |
| 1312 | if (((cls in type_maps.stats_reply_list) or |
| 1313 | (cls in type_maps.stats_request_list)) and |
| 1314 | name in type_maps.stats_types[i]): |
| 1315 | out.write(" %d, /* %s */\n" % |
| 1316 | (type_maps.stats_types[i][name], cls)) |
| 1317 | else: |
| 1318 | out.write(" -1, /* %s (invalid) */\n" % cls) |
| 1319 | out.write("};\n\n") |
| 1320 | |
| 1321 | out.write(""" |
| 1322 | /** |
| 1323 | * Unified map, indexed by wire version which is 1-based. |
| 1324 | */ |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 1325 | const int *of_object_to_stats_type_map[OF_VERSION_ARRAY_MAX] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1326 | NULL, |
| 1327 | """) |
| 1328 | for version in of_g.of_version_range: |
| 1329 | out.write(" of_object_to_stats_type_map_v%d,\n" % version) |
| 1330 | out.write(""" |
| 1331 | }; |
| 1332 | """) |
| 1333 | |