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 | @file code_gen.py |
| 30 | Code generation functions for LOCI |
| 31 | """ |
| 32 | |
| 33 | import sys |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 34 | import c_gen.of_g_legacy as of_g |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 35 | import c_match |
| 36 | from generic_utils import * |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 37 | from c_gen import flags, type_maps, c_type_maps |
| 38 | import c_gen.loxi_utils_legacy as loxi_utils |
Rich Lane | 92feca8 | 2013-12-10 15:57:13 -0800 | [diff] [blame] | 39 | import loxi_globals |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 40 | |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 41 | import c_gen.identifiers as identifiers |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 42 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 43 | # 'property' is for queues. Could be trouble |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 44 | |
| 45 | ################################################################ |
| 46 | # |
| 47 | # Misc helper functions |
| 48 | # |
| 49 | ################################################################ |
| 50 | |
| 51 | def h_file_to_define(name): |
| 52 | """ |
| 53 | Convert a .h file name to the define used for the header |
| 54 | """ |
| 55 | h_name = name[:-2].upper() |
| 56 | h_name = "_" + h_name + "_H_" |
| 57 | return h_name |
| 58 | |
| 59 | def enum_name(cls): |
| 60 | """ |
| 61 | Return the name used for an enum identifier for the given class |
| 62 | @param cls The class name |
| 63 | """ |
| 64 | return loxi_utils.enum_name(cls) |
| 65 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 66 | # TODO serialize match outside accessor? |
| 67 | def accessor_return_type(a_type, m_type): |
| 68 | if loxi_utils.accessor_returns_error(a_type, m_type): |
| 69 | return "int WARN_UNUSED_RESULT" |
| 70 | else: |
| 71 | return "void" |
| 72 | |
| 73 | def accessor_return_success(a_type, m_type): |
| 74 | if loxi_utils.accessor_returns_error(a_type, m_type): |
| 75 | return "OF_ERROR_NONE" |
| 76 | else: |
| 77 | return "" |
| 78 | |
| 79 | ################################################################ |
| 80 | # |
| 81 | # Per-file generators, mapped to jump table below |
| 82 | # |
| 83 | ################################################################ |
| 84 | |
| 85 | def base_h_gen(out, name): |
| 86 | """ |
| 87 | Generate code for base header file |
| 88 | @param out The file handle to write to |
| 89 | @param name The name of the file |
| 90 | """ |
| 91 | common_top_matter(out, name) |
| 92 | base_h_content(out) |
| 93 | gen_object_enum(out) |
| 94 | out.write(""" |
| 95 | /**************************************************************** |
| 96 | * |
| 97 | * Experimenter IDs |
| 98 | * |
| 99 | ****************************************************************/ |
| 100 | |
| 101 | """) |
| 102 | for name, val in of_g.experimenter_name_to_id.items(): |
| 103 | out.write("#define OF_EXPERIMENTER_ID_%s 0x%08x\n" % |
| 104 | (name.upper(), val)) |
| 105 | |
| 106 | out.write(""" |
| 107 | /**************************************************************** |
| 108 | * |
| 109 | * OpenFlow Match version specific and generic defines |
| 110 | * |
| 111 | ****************************************************************/ |
| 112 | """) |
| 113 | c_match.gen_v4_match_compat(out) |
| 114 | c_match.gen_match_macros(out) |
| 115 | c_match.gen_oxm_defines(out) |
| 116 | out.write("\n#endif /* Base header file */\n") |
| 117 | |
| 118 | def identifiers_gen(out, filename): |
| 119 | """ |
| 120 | Generate the macros for LOCI identifiers |
| 121 | @param out The file handle to write to |
| 122 | @param filename The name of the file |
| 123 | """ |
| 124 | common_top_matter(out, filename) |
| 125 | out.write(""" |
| 126 | /** |
| 127 | * For each identifier from an OpenFlow header file, a Loxi version |
| 128 | * of the identifier is generated. For example, ofp_port_flood becomes |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 129 | * OF_PORT_DEST_FLOOD. Loxi provides the following macros related to |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 130 | * OpenFlow identifiers (using OF_IDENT_ as an example below): |
| 131 | * OF_IDENT_BY_VERSION(version) Get the value for the specific version |
| 132 | * OF_IDENT_SUPPORTED(version) Boolean: Is OF_IDENT defined for version |
| 133 | * OF_IDENT The common value across all versions if defined |
| 134 | * OF_IDENT_GENERIC A unique value across all OF identifiers |
| 135 | * |
| 136 | * For identifiers marked as flags, the following are also defined |
| 137 | * OF_IDENT_SET(flags, version) |
| 138 | * OF_IDENT_CLEAR(flags, version) |
| 139 | * OF_IDENT_TEST(flags, version) |
| 140 | * |
| 141 | * Notes: |
| 142 | * |
| 143 | * OF_IDENT_BY_VERSION(version) returns an undefined value |
| 144 | * if the passed version does not define OF_IDENT. It does not generate an |
| 145 | * error, nor record anything to the log file. If the value is the same |
| 146 | * across all defined versions, the version is ignored. |
| 147 | * |
| 148 | * OF_IDENT is only defined if the value is the same across all |
| 149 | * target LOXI versions FOR WHICH IT IS DEFINED. No error checking is |
| 150 | * done. This allows code to be written without requiring the version |
| 151 | * to be known or referenced when it doesn't matter. It does mean |
| 152 | * that when porting to a new version of OpenFlow, compile errors may |
| 153 | * occur. However, this is an indication that the existing code must |
| 154 | * be updated to account for a change in the semantics with the newly |
| 155 | * supported OpenFlow version. |
| 156 | * |
| 157 | * @fixme Currently we do not handle multi-bit flags or field values; for |
| 158 | * example, OF_TABLE_CONFIG_TABLE_MISS_CONTROLLER is the meaning for |
| 159 | * a zero value in the bits indicated by OF_TABLE_CONFIG_TABLE_MISS_MASK. |
| 160 | * |
| 161 | * @fixme Need to decide (or make a code gen option) on the requirement |
| 162 | * for defining OF_IDENT: Is it that all target versions define it and |
| 163 | * the agree? Or only that the versions which define it agree? |
| 164 | */ |
| 165 | """) |
| 166 | |
| 167 | # Build value-by-version parameters and c_code |
| 168 | if len(of_g.target_version_list) > 1: # Supporting more than one version |
| 169 | vbv_params = [] |
| 170 | vbv_code = "" |
| 171 | first = True |
| 172 | for version in of_g.target_version_list: |
| 173 | vbv_params.append("value_%s" % of_g.short_version_names[version]) |
| 174 | if not first: |
| 175 | vbv_code += "\\\n " |
| 176 | else: |
| 177 | first = False |
| 178 | last_value = "value_%s" % of_g.short_version_names[version] |
| 179 | vbv_code += "((version) == %s) ? (%s) : " % \ |
| 180 | (of_g.of_version_wire2name[version], last_value) |
| 181 | # @todo Using last value, can optimize out last ? |
| 182 | vbv_code += "(%s)" % last_value |
| 183 | |
| 184 | out.write(""" |
| 185 | /** |
| 186 | * @brief True for the special case of all versions supported |
| 187 | */ |
| 188 | #define OF_IDENT_IN_ALL_VERSIONS 1 /* Indicates identifier in all versions */ |
| 189 | |
| 190 | /** |
| 191 | * @brief General macro to map version to value where values given as params |
| 192 | * |
| 193 | * If unknown version is passed, use the latest version's value |
| 194 | */ |
| 195 | #define OF_VALUE_BY_VERSION(version, %s) \\ |
| 196 | (%s) |
| 197 | |
| 198 | /** |
| 199 | * @brief Generic set a flag |
| 200 | */ |
| 201 | #define OF_FLAG_SET(flags, mask) (flags) = (flags) | (mask) |
| 202 | |
| 203 | /** |
| 204 | * @brief Generic test if a flag is set |
| 205 | */ |
| 206 | #define OF_FLAG_CLEAR(flags, mask) (flags) = (flags) & ~(mask) |
| 207 | |
| 208 | /** |
| 209 | * @brief Generic test if a flag is set |
| 210 | */ |
| 211 | #define OF_FLAG_TEST(flags, mask) ((flags) & (mask) ? 1 : 0) |
| 212 | |
| 213 | /** |
| 214 | * @brief Set a flag where the value is an enum indication of bit shift |
| 215 | */ |
| 216 | #define OF_FLAG_ENUM_SET(flags, e_val) OF_FLAG_SET(flags, 1 << (e_val)) |
| 217 | |
| 218 | /** |
| 219 | * @brief Clear a flag where the value is an enum indication of bit shift |
| 220 | */ |
| 221 | #define OF_FLAG_ENUM_CLEAR(flags, e_val) OF_FLAG_CLEAR(flags, 1 << (e_val)) |
| 222 | |
| 223 | /** |
| 224 | * @brief Test a flag where the value is an enum indication of bit shift |
| 225 | */ |
| 226 | #define OF_FLAG_ENUM_TEST(flags, e_val) OF_FLAG_TEST(flags, 1 << (e_val)) |
| 227 | """ % (", ".join(vbv_params), vbv_code)) |
| 228 | |
| 229 | # For each group of identifiers, bunch ident defns |
| 230 | count = 1 |
| 231 | keys = of_g.identifiers_by_group.keys() |
| 232 | keys.sort() |
| 233 | for group in keys: |
| 234 | idents = of_g.identifiers_by_group[group] |
| 235 | idents.sort() |
| 236 | out.write(""" |
| 237 | /**************************************************************** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 238 | * Identifiers from %s |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 239 | *****************************************************************/ |
| 240 | """ % group) |
| 241 | for ident in idents: |
| 242 | info = of_g.identifiers[ident] |
| 243 | |
| 244 | keys = info["values_by_version"].keys() |
| 245 | keys.sort() |
| 246 | |
| 247 | out.write(""" |
| 248 | /* |
| 249 | * Defines for %(ident)s |
| 250 | * Original name %(ofp_name)s |
| 251 | */ |
| 252 | """ % dict(ident=ident, ofp_name=info["ofp_name"])) |
| 253 | |
| 254 | # Generate supported versions macro |
| 255 | if len(keys) == len(of_g.target_version_list): # Defined for all |
| 256 | out.write("""\ |
| 257 | #define %(ident)s_SUPPORTED(version) OF_IDENT_IN_ALL_VERSIONS |
| 258 | """ % dict(ident=ident)) |
| 259 | else: # Undefined for some version |
| 260 | sup_list = [] |
| 261 | for version in keys: |
| 262 | sup_list.append("((version) == %s)" % |
| 263 | of_g.of_version_wire2name[version]) |
| 264 | out.write("""\ |
| 265 | #define %(ident)s_SUPPORTED(version) \\ |
| 266 | (%(sup_str)s) |
| 267 | """ % dict(ident=ident, sup_str=" || \\\n ".join(sup_list))) |
| 268 | |
| 269 | # Generate value macro |
| 270 | if identifiers.defined_versions_agree(of_g.identifiers, |
| 271 | of_g.target_version_list, |
| 272 | ident): |
| 273 | out.write("""\ |
Rich Lane | f3dc396 | 2013-05-10 16:16:48 -0700 | [diff] [blame] | 274 | #define %(ident)s (%(value)#x) |
| 275 | #define %(ident)s_BY_VERSION(version) (%(value)#x) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 276 | """ % dict(ident=ident,value=info["common_value"])) |
| 277 | else: # Values differ between versions |
| 278 | # Generate version check and value by version |
| 279 | val_list = [] |
| 280 | # Order of params matters |
| 281 | for version in of_g.target_version_list: |
| 282 | if version in info["values_by_version"]: |
| 283 | value = info["values_by_version"][version] |
| 284 | else: |
| 285 | value = identifiers.UNDEFINED_IDENT_VALUE |
Rich Lane | f3dc396 | 2013-05-10 16:16:48 -0700 | [diff] [blame] | 286 | val_list.append("%#x" % value) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 287 | out.write("""\ |
| 288 | #define %(ident)s_BY_VERSION(version) \\ |
| 289 | OF_VALUE_BY_VERSION(version, %(val_str)s) |
| 290 | """ % dict(ident=ident, val_str=", ".join(val_list))) |
| 291 | if flags.ident_is_flag(ident): |
| 292 | log("Treating %s as a flag" % ident) |
| 293 | out.write(""" |
| 294 | #define %(ident)s_SET(flags, version) \\ |
| 295 | OF_FLAG_SET(flags, %(ident)s_BY_VERSION(version)) |
| 296 | #define %(ident)s_TEST(flags, version) \\ |
| 297 | OF_FLAG_TEST(flags, %(ident)s_BY_VERSION(version)) |
| 298 | #define %(ident)s_CLEAR(flags, version) \\ |
| 299 | OF_FLAG_CLEAR(flags, %(ident)s_BY_VERSION(version)) |
| 300 | """ % dict(ident=ident)) |
| 301 | |
| 302 | out.write("#define %(ident)s_GENERIC %(count)d\n" |
| 303 | % dict(ident=ident, count=count)) |
| 304 | count += 1 # This count should probably be promoted higher |
| 305 | |
| 306 | log("Generated %d identifiers" % (count - 1)) |
| 307 | out.write("\n#endif /* Loci identifiers header file */\n") |
| 308 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 309 | def match_h_gen(out, name): |
| 310 | """ |
| 311 | Generate code for |
| 312 | @param out The file handle to write to |
| 313 | @param name The name of the file |
| 314 | """ |
| 315 | c_match.match_h_top_matter(out, name) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 316 | c_match.gen_match_struct(out) |
| 317 | c_match.gen_match_comp(out) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 318 | out.write("\n#endif /* Match header file */\n") |
| 319 | |
| 320 | def top_h_gen(out, name): |
| 321 | """ |
| 322 | Generate code for |
| 323 | @param out The file handle to write to |
| 324 | @param name The name of the file |
| 325 | """ |
| 326 | external_h_top_matter(out, name) |
| 327 | out.write(""" |
| 328 | |
| 329 | typedef enum loci_log_level { |
| 330 | LOCI_LOG_LEVEL_TRACE, |
| 331 | LOCI_LOG_LEVEL_VERBOSE, |
| 332 | LOCI_LOG_LEVEL_INFO, |
| 333 | LOCI_LOG_LEVEL_WARN, |
| 334 | LOCI_LOG_LEVEL_ERROR, |
| 335 | LOCI_LOG_LEVEL_MSG |
| 336 | } loci_log_level_t; |
| 337 | |
| 338 | /** |
| 339 | * @brief Output a log message. |
| 340 | * @param level The log level. |
| 341 | * @param fname The function name. |
| 342 | * @param file The file name. |
| 343 | * @param line The line number. |
| 344 | * @param format The message format string. |
| 345 | */ |
| 346 | typedef int (*loci_logger_f)(loci_log_level_t level, |
| 347 | const char *fname, const char *file, int line, |
| 348 | const char *format, ...); |
| 349 | |
| 350 | /* |
| 351 | * This variable should be set by the user of the library to redirect logs to |
| 352 | * their log infrastructure. The default drops all logs. |
| 353 | */ |
| 354 | extern loci_logger_f loci_logger; |
| 355 | |
| 356 | /** |
| 357 | * Map a generic object to the underlying wire buffer |
| 358 | * |
| 359 | * Treat as private |
| 360 | */ |
| 361 | #define OF_OBJECT_TO_MESSAGE(obj) \\ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 362 | ((of_message_t)(WBUF_BUF((obj)->wbuf))) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * Macro for the fixed length part of an object |
| 366 | * |
| 367 | * @param obj The object whose extended length is being calculated |
| 368 | * @returns length in bytes of non-variable part of the object |
| 369 | */ |
| 370 | #define OF_OBJECT_FIXED_LENGTH(obj) \\ |
| 371 | (of_object_fixed_len[(obj)->version][(obj)->object_id]) |
| 372 | |
| 373 | /** |
| 374 | * Return the length of the object beyond its fixed length |
| 375 | * |
| 376 | * @param obj The object whose extended length is being calculated |
| 377 | * @returns length in bytes of non-variable part of the object |
| 378 | * |
| 379 | * Most variable length fields are alone at the end of a structure. |
| 380 | * Their length is a simple calculation, just the total length of |
| 381 | * the parent minus the length of the non-variable part of the |
| 382 | * parent's class type. |
| 383 | */ |
| 384 | |
| 385 | #define OF_OBJECT_VARIABLE_LENGTH(obj) \\ |
| 386 | ((obj)->length - OF_OBJECT_FIXED_LENGTH(obj)) |
| 387 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 388 | extern int of_wire_buffer_of_match_get(of_object_t *obj, int offset, |
| 389 | of_match_t *match); |
| 390 | extern int of_wire_buffer_of_match_set(of_object_t *obj, int offset, |
| 391 | of_match_t *match, int cur_len); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 392 | """) |
| 393 | |
| 394 | # gen_base_types(out) |
| 395 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 396 | out.write(""" |
| 397 | /**************************************************************** |
| 398 | * |
| 399 | * Declarations of maps between on-the-wire type values and LOCI identifiers |
| 400 | * |
| 401 | ****************************************************************/ |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 402 | |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 403 | int of_object_wire_init(of_object_t *obj, of_object_id_t base_object_id, int max_len); |
| 404 | |
| 405 | extern const int *const of_object_fixed_len[OF_VERSION_ARRAY_MAX]; |
| 406 | extern const int *const of_object_extra_len[OF_VERSION_ARRAY_MAX]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 407 | """) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 408 | c_type_maps.gen_type_data_header(out) |
| 409 | c_match.gen_declarations(out) |
| 410 | # @fixme Move debug stuff to own fn |
| 411 | out.write(""" |
| 412 | /** |
| 413 | * Macro to check consistency of length for top level objects |
| 414 | * |
| 415 | * If the object has no parent then its length should match the |
| 416 | * underlying wire buffer's current bytes. |
| 417 | */ |
| 418 | #define OF_LENGTH_CHECK_ASSERT(obj) \\ |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 419 | LOCI_ASSERT(((obj)->parent != NULL) || \\ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 420 | ((obj)->wbuf == NULL) || \\ |
| 421 | (WBUF_CURRENT_BYTES((obj)->wbuf) == (obj)->length)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 422 | |
| 423 | #define OF_DEBUG_DUMP |
| 424 | #if defined(OF_DEBUG_DUMP) |
| 425 | extern void dump_match(of_match_t *match); |
| 426 | #endif /* OF_DEBUG_DUMP */ |
| 427 | """) |
| 428 | |
Rich Lane | 1f03f67 | 2014-08-05 00:10:00 -0700 | [diff] [blame] | 429 | out.write(""" |
| 430 | static inline const char * |
| 431 | of_class_name(of_object_t *obj) |
| 432 | { |
| 433 | return of_object_id_str[obj->object_id]; |
| 434 | } |
| 435 | """) |
| 436 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 437 | out.write("\n#endif /* Top header file */\n") |
| 438 | |
| 439 | def match_c_gen(out, name): |
| 440 | """ |
| 441 | Generate code for |
| 442 | @param out The file handle to write to |
| 443 | @param name The name of the file |
| 444 | """ |
| 445 | c_match.match_c_top_matter(out, name) |
| 446 | c_match.gen_match_conversions(out) |
| 447 | c_match.gen_serialize(out) |
| 448 | c_match.gen_deserialize(out) |
| 449 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 450 | ################################################################ |
| 451 | # Top Matter |
| 452 | ################################################################ |
| 453 | |
| 454 | def common_top_matter(out, name): |
| 455 | loxi_utils.gen_c_copy_license(out) |
| 456 | out.write("""\ |
Rich Lane | d983aa5 | 2013-06-13 11:48:37 -0700 | [diff] [blame] | 457 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 458 | /**************************************************************** |
| 459 | * File: %s |
| 460 | * |
| 461 | * DO NOT EDIT |
| 462 | * |
| 463 | * This file is automatically generated |
| 464 | * |
| 465 | ****************************************************************/ |
| 466 | |
| 467 | """ % name) |
| 468 | |
| 469 | if name[-2:] == ".h": |
| 470 | out.write(""" |
| 471 | #if !defined(%(h)s) |
| 472 | #define %(h)s |
| 473 | |
| 474 | """ % dict(h=h_file_to_define(name))) |
| 475 | |
| 476 | def base_h_content(out): |
| 477 | """ |
| 478 | Generate base header file content |
| 479 | |
| 480 | @param out The output file object |
| 481 | """ |
| 482 | |
| 483 | # @fixme Supported version should be generated based on input to LoxiGen |
| 484 | |
| 485 | out.write(""" |
| 486 | /* |
| 487 | * Base OpenFlow definitions. These depend only on standard C headers |
| 488 | */ |
| 489 | #include <string.h> |
| 490 | #include <stdint.h> |
| 491 | |
| 492 | /* g++ requires this to pick up PRI, etc. |
| 493 | * See http://gcc.gnu.org/ml/gcc-help/2006-10/msg00223.html |
| 494 | */ |
| 495 | #if !defined(__STDC_FORMAT_MACROS) |
| 496 | #define __STDC_FORMAT_MACROS |
| 497 | #endif |
| 498 | #include <inttypes.h> |
| 499 | |
| 500 | #include <stdlib.h> |
| 501 | #include <assert.h> |
| 502 | #include <loci/loci_idents.h> |
| 503 | |
| 504 | /** |
| 505 | * Macro to enable debugging for LOCI. |
| 506 | * |
| 507 | * This enables debug output to stdout. |
| 508 | */ |
| 509 | #define OF_DEBUG_ENABLE |
| 510 | |
| 511 | #if defined(OF_DEBUG_ENABLE) |
| 512 | #include <stdio.h> /* Currently for debugging */ |
| 513 | #define FIXME(str) do { \\ |
| 514 | fprintf(stderr, "%s\\n", str); \\ |
| 515 | exit(1); \\ |
| 516 | } while (0) |
| 517 | #define debug printf |
| 518 | #else |
| 519 | #define FIXME(str) |
| 520 | #define debug(str, ...) |
| 521 | #endif /* OF_DEBUG_ENABLE */ |
| 522 | |
| 523 | /** |
| 524 | * The type of a function used by the LOCI dump/show functions to |
| 525 | * output text. Essentially the same signature as fprintf. May |
| 526 | * be called many times per invocation of e.g. of_object_show(). |
| 527 | */ |
| 528 | typedef int (*loci_writer_f)(void *cookie, const char *fmt, ...); |
| 529 | |
| 530 | /** |
| 531 | * Check if a version is supported |
| 532 | */ |
| 533 | #define OF_VERSION_OKAY(v) ((v) >= OF_VERSION_1_0 && (v) <= OF_VERSION_1_3) |
| 534 | |
| 535 | """) |
| 536 | gen_version_enum(out) |
| 537 | out.write("\n") |
| 538 | |
| 539 | # for c_name in of_g.ofp_constants: |
| 540 | # val = str(of_g.ofp_constants[c_name]) |
| 541 | # out.write("#define %s %s\n" % (c_name, val)) |
| 542 | # out.write("\n") |
| 543 | |
| 544 | out.write(""" |
| 545 | typedef enum of_error_codes_e { |
| 546 | OF_ERROR_NONE = 0, |
| 547 | OF_ERROR_RESOURCE = -1, /* Could not allocate space */ |
| 548 | OF_ERROR_PARAM = -2, /* Bad parameter */ |
| 549 | OF_ERROR_VERSION = -3, /* Version not supported */ |
| 550 | OF_ERROR_RANGE = -4, /* End of list indication */ |
| 551 | OF_ERROR_COMPAT = -5, /* Incompatible assignment */ |
| 552 | OF_ERROR_PARSE = -6, /* Error in parsing data */ |
| 553 | OF_ERROR_INIT = -7, /* Uninitialized data */ |
| 554 | OF_ERROR_UNKNOWN = -8 /* Unknown error */ |
| 555 | } of_error_codes_t; |
| 556 | |
| 557 | #define OF_ERROR_STRINGS "none", \\ |
| 558 | "resource", \\ |
| 559 | "parameter", \\ |
| 560 | "version", \\ |
| 561 | "range", \\ |
| 562 | "incompatible", \\ |
| 563 | "parse", \\ |
| 564 | "init", \\ |
| 565 | "unknown" |
| 566 | |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 567 | extern const char *const of_error_strings[]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 568 | |
Rich Lane | a8b5463 | 2014-02-19 11:17:47 -0800 | [diff] [blame] | 569 | #ifdef __GNUC__ |
| 570 | #define LOCI_NORETURN_ATTR __attribute__((__noreturn__)) |
| 571 | #else |
| 572 | #define LOCI_NORETURN_ATTR |
| 573 | #endif |
| 574 | |
| 575 | extern void loci_assert_fail( |
| 576 | const char *cond, |
| 577 | const char *file, |
| 578 | unsigned int line) LOCI_NORETURN_ATTR; |
| 579 | |
Rich Lane | 5375773 | 2013-02-23 17:00:10 -0800 | [diff] [blame] | 580 | #ifndef NDEBUG |
Rich Lane | a8b5463 | 2014-02-19 11:17:47 -0800 | [diff] [blame] | 581 | #define LOCI_ASSERT(val) ((val) ? (void)0 : loci_assert_fail(#val, __FILE__, __LINE__)) |
Rich Lane | 5375773 | 2013-02-23 17:00:10 -0800 | [diff] [blame] | 582 | #else |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 583 | #define LOCI_ASSERT(val) |
Rich Lane | 5375773 | 2013-02-23 17:00:10 -0800 | [diff] [blame] | 584 | #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * Some LOCI object accessors can fail, and it's easy to forget to check. |
| 588 | * On certain compilers we can trigger a warning if the error code |
| 589 | * is ignored. |
| 590 | */ |
| 591 | #ifndef DISABLE_WARN_UNUSED_RESULT |
| 592 | #ifdef __GNUC__ |
| 593 | #define WARN_UNUSED_RESULT __attribute__ ((warn_unused_result)) |
| 594 | #else |
| 595 | #define WARN_UNUSED_RESULT |
| 596 | #endif |
| 597 | #else |
| 598 | #define WARN_UNUSED_RESULT |
| 599 | #endif |
| 600 | |
| 601 | typedef union of_generic_u of_generic_t; |
| 602 | typedef struct of_object_s of_object_t; |
| 603 | |
| 604 | /* Define ipv4 address as uint32 */ |
| 605 | typedef uint32_t of_ipv4_t; |
| 606 | |
| 607 | /* Table ID is the OF standard uint8 */ |
| 608 | typedef uint8_t of_table_id_t; |
| 609 | |
| 610 | #define OF_MAC_ADDR_BYTES 6 |
| 611 | typedef struct of_mac_addr_s { |
| 612 | uint8_t addr[OF_MAC_ADDR_BYTES]; |
| 613 | } of_mac_addr_t; |
| 614 | |
| 615 | #define OF_IPV6_BYTES 16 |
| 616 | typedef struct of_ipv6_s { |
| 617 | uint8_t addr[OF_IPV6_BYTES]; |
| 618 | } of_ipv6_t; |
| 619 | |
| 620 | extern const of_mac_addr_t of_mac_addr_all_ones; |
| 621 | extern const of_mac_addr_t of_mac_addr_all_zeros; |
| 622 | |
| 623 | extern const of_ipv6_t of_ipv6_all_ones; |
| 624 | extern const of_ipv6_t of_ipv6_all_zeros; |
| 625 | |
| 626 | /** |
| 627 | * Generic zero and all-ones values of size 16 bytes. |
| 628 | * |
| 629 | * IPv6 is longest data type we worry about for comparisons |
| 630 | */ |
| 631 | #define of_all_zero_value of_ipv6_all_zeros |
| 632 | #define of_all_ones_value of_ipv6_all_ones |
| 633 | |
| 634 | /** |
| 635 | * Non-zero/all ones check for arbitrary type of size <= 16 bytes |
| 636 | */ |
| 637 | #define OF_VARIABLE_IS_NON_ZERO(_ptr) \\ |
| 638 | (MEMCMP(&of_all_zero_value, (_ptr), sizeof(*(_ptr)))) |
| 639 | #define OF_VARIABLE_IS_ALL_ONES(_ptr) \\ |
| 640 | (!MEMCMP(&of_all_ones_value, (_ptr), sizeof(*(_ptr)))) |
| 641 | |
| 642 | /* The octets object is a struct holding pointer and length */ |
| 643 | typedef struct of_octets_s { |
| 644 | uint8_t *data; |
| 645 | int bytes; |
| 646 | } of_octets_t; |
| 647 | |
| 648 | /* Macro to convert an octet object to a pointer; currently trivial */ |
| 649 | #define OF_OCTETS_POINTER_GET(octet_ptr) ((octet_ptr)->data) |
| 650 | #define OF_OCTETS_POINTER_SET(octet_ptr, ptr) (octet_ptr)->data = (ptr) |
| 651 | #define OF_OCTETS_BYTES_GET(octet_ptr) ((octet_ptr)->bytes) |
| 652 | #define OF_OCTETS_BYTES_SET(octet_ptr, bytes) (octet_ptr)->bytes = (bytes) |
| 653 | |
| 654 | /* Currently these are categorized as scalars */ |
| 655 | typedef char of_port_name_t[OF_MAX_PORT_NAME_LEN]; |
| 656 | typedef char of_table_name_t[OF_MAX_TABLE_NAME_LEN]; |
| 657 | typedef char of_desc_str_t[OF_DESC_STR_LEN]; |
| 658 | typedef char of_serial_num_t[OF_SERIAL_NUM_LEN]; |
Rich Lane | f8a3d00 | 2014-03-19 13:33:52 -0700 | [diff] [blame] | 659 | typedef char of_str64_t[64]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 660 | |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 661 | typedef struct of_bitmap_128_s { |
| 662 | uint64_t hi; |
| 663 | uint64_t lo; |
| 664 | } of_bitmap_128_t; |
| 665 | |
Rich Lane | fab0c82 | 2013-12-30 11:46:48 -0800 | [diff] [blame] | 666 | typedef struct of_checksum_128_s { |
| 667 | uint64_t hi; |
| 668 | uint64_t lo; |
| 669 | } of_checksum_128_t; |
| 670 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 671 | /* These are types which change across versions. */ |
| 672 | typedef uint32_t of_port_no_t; |
| 673 | typedef uint16_t of_fm_cmd_t; |
| 674 | typedef uint64_t of_wc_bmap_t; |
| 675 | typedef uint64_t of_match_bmap_t; |
| 676 | |
| 677 | #define MEMMOVE(dest, src, bytes) memmove(dest, src, bytes) |
| 678 | #define MEMSET(dest, val, bytes) memset(dest, val, bytes) |
| 679 | #define MEMCPY(dest, src, bytes) memcpy(dest, src, bytes) |
| 680 | #define MEMCMP(a, b, bytes) memcmp(a, b, bytes) |
| 681 | #define MALLOC(bytes) malloc(bytes) |
| 682 | #define FREE(ptr) free(ptr) |
| 683 | |
| 684 | /** Try an operation and return on failure. */ |
| 685 | #define OF_TRY(op) do { \\ |
| 686 | int _rv; \\ |
| 687 | if ((_rv = (op)) < 0) { \\ |
| 688 | LOCI_LOG_ERROR("ERROR %d at %s:%d\\n", _rv, __FILE__, __LINE__); \\ |
| 689 | return _rv; \\ |
| 690 | } \\ |
| 691 | } while (0) |
| 692 | |
| 693 | /* The extent of an OF match object is determined by its length field, but |
| 694 | * aligned to 8 bytes |
| 695 | */ |
| 696 | |
| 697 | #define OF_MATCH_BYTES(length) (((length) + 7) & 0xfff8) |
| 698 | |
Rich Lane | dfcafae | 2014-03-06 16:56:49 -0800 | [diff] [blame] | 699 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 700 | #define U16_NTOH(val) (val) |
| 701 | #define U32_NTOH(val) (val) |
| 702 | #define U64_NTOH(val) (val) |
| 703 | #define IPV6_NTOH(dst, src) /* NOTE different syntax; currently no-op */ |
| 704 | #define U16_HTON(val) (val) |
| 705 | #define U32_HTON(val) (val) |
| 706 | #define U64_HTON(val) (val) |
| 707 | #define IPV6_HTON(dst, src) /* NOTE different syntax; currently no-op */ |
| 708 | #else /* Little Endian */ |
Rich Lane | 69e247e | 2014-03-05 10:27:22 -0800 | [diff] [blame] | 709 | #define U16_NTOH(val) (((val) >> 8) | (((val) & 0xff) << 8)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 710 | #define U32_NTOH(val) ((((val) & 0xff000000) >> 24) | \\ |
| 711 | (((val) & 0x00ff0000) >> 8) | \\ |
| 712 | (((val) & 0x0000ff00) << 8) | \\ |
| 713 | (((val) & 0x000000ff) << 24)) |
| 714 | #define U64_NTOH(val) ((((val) & 0xff00000000000000LL) >> 56) | \\ |
| 715 | (((val) & 0x00ff000000000000LL) >> 40) | \\ |
| 716 | (((val) & 0x0000ff0000000000LL) >> 24) | \\ |
| 717 | (((val) & 0x000000ff00000000LL) >> 8) | \\ |
| 718 | (((val) & 0x00000000ff000000LL) << 8) | \\ |
| 719 | (((val) & 0x0000000000ff0000LL) << 24) | \\ |
| 720 | (((val) & 0x000000000000ff00LL) << 40) | \\ |
| 721 | (((val) & 0x00000000000000ffLL) << 56)) |
| 722 | #define IPV6_NTOH(dst, src) /* NOTE different syntax; currently no-op */ |
| 723 | #define U16_HTON(val) U16_NTOH(val) |
| 724 | #define U32_HTON(val) U32_NTOH(val) |
| 725 | #define U64_HTON(val) U64_NTOH(val) |
| 726 | #define IPV6_HTON(dst, src) /* NOTE different syntax; currently no-op */ |
| 727 | #endif |
| 728 | |
| 729 | /**************************************************************** |
| 730 | * |
| 731 | * The following are internal definitions used by the automatically |
| 732 | * generated code. Users should not reference these definitions |
| 733 | * as they may change between versions of this code |
| 734 | * |
| 735 | ****************************************************************/ |
| 736 | |
| 737 | #define OF_MESSAGE_IN_MATCH_POINTER(obj) \\ |
| 738 | (WIRE_BUF_POINTER(&((obj)->wire_buffer), OF_MESSAGE_IN_MATCH_OFFSET)) |
| 739 | #define OF_MESSAGE_IN_MATCH_LEN(ptr) BUF_U16_GET(&ptr[2]) |
| 740 | #define OF_MESSAGE_IN_DATA_OFFSET(obj) \\ |
| 741 | (FIXED_LEN + OF_MESSAGE_IN_MATCH_LEN(OF_MESSAGE_IN_MATCH_POINTER(obj)) + 2) |
| 742 | |
| 743 | #define OF_MESSAGE_OUT_DATA_OFFSET(obj) \\ |
| 744 | (FIXED_LEN + of_message_out_actions_len_get(obj)) |
| 745 | |
| 746 | """) |
| 747 | |
| 748 | def external_h_top_matter(out, name): |
| 749 | """ |
| 750 | Generate top matter for external header file |
| 751 | |
| 752 | @param name The name of the output file |
| 753 | @param out The output file object |
| 754 | """ |
| 755 | common_top_matter(out, name) |
| 756 | out.write(""" |
| 757 | #include <loci/loci_base.h> |
| 758 | #include <loci/of_message.h> |
| 759 | #include <loci/of_match.h> |
| 760 | #include <loci/of_object.h> |
Rich Lane | def2e51 | 2013-12-15 15:54:02 -0800 | [diff] [blame] | 761 | #include <loci/loci_classes.h> |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 762 | #include <loci/loci_class_metadata.h> |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 763 | |
| 764 | /**************************************************************** |
| 765 | * |
| 766 | * This file is divided into the following sections. |
| 767 | * |
| 768 | * A few object specific macros |
| 769 | * Class typedefs (no struct definitions) |
| 770 | * Per-data type accessor function typedefs |
| 771 | * Per-class new/delete function typedefs |
| 772 | * Per-class static delete functions |
| 773 | * Per-class, per-member accessor declarations |
| 774 | * Per-class structure definitions |
| 775 | * Generic union (inheritance) definitions |
| 776 | * Pointer set function declarations |
| 777 | * Some special case macros |
| 778 | * |
| 779 | ****************************************************************/ |
| 780 | """) |
| 781 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 782 | ################################################################ |
| 783 | # |
| 784 | ################################################################ |
| 785 | |
| 786 | def gen_version_enum(out): |
| 787 | """ |
| 788 | Generate the enumerated type for versions in LoxiGen |
| 789 | @param out The file object to which to write the decs |
| 790 | |
| 791 | This just uses the wire versions for now |
| 792 | """ |
| 793 | out.write(""" |
| 794 | /** |
| 795 | * Enumeration of OpenFlow versions |
| 796 | * |
| 797 | * The wire protocol numbers are currently used for values of the corresponding |
| 798 | * version identifiers. |
| 799 | */ |
| 800 | typedef enum of_version_e { |
| 801 | OF_VERSION_UNKNOWN = 0, |
| 802 | """) |
| 803 | |
| 804 | is_first = True |
| 805 | max = 0 |
| 806 | for v in of_g.wire_ver_map: |
| 807 | if is_first: |
| 808 | is_first = False |
| 809 | else: |
| 810 | out.write(",\n") |
| 811 | if v > max: |
| 812 | max = v |
| 813 | out.write(" %s = %d" % (of_g.wire_ver_map[v], v)) |
| 814 | |
| 815 | out.write(""" |
| 816 | } of_version_t; |
| 817 | |
| 818 | /** |
| 819 | * @brief Use this when declaring arrays indexed by wire version |
| 820 | */ |
| 821 | #define OF_VERSION_ARRAY_MAX %d |
| 822 | """ % (max + 1)) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 823 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 824 | def gen_object_enum(out): |
| 825 | """ |
| 826 | Generate the enumerated type for object identification in LoxiGen |
| 827 | @param out The file object to which to write the decs |
| 828 | """ |
| 829 | out.write(""" |
| 830 | |
| 831 | /** |
| 832 | * Enumeration of OpenFlow objects |
| 833 | * |
| 834 | * We enumerate the OpenFlow objects used internally. Note that some |
| 835 | * message types are determined both by an outer type (message type like |
| 836 | * stats_request) and an inner type (port stats). These are different |
| 837 | * messages in ofC. |
| 838 | * |
| 839 | * These values are for internal use only. They will change with |
| 840 | * different versions of ofC. |
| 841 | */ |
| 842 | |
| 843 | typedef enum of_object_id_e { |
| 844 | /* Root object type */ |
| 845 | OF_OBJECT_INVALID = -1, /* "invalid" return value for mappings */ |
| 846 | OF_OBJECT = 0, /* Generic, untyped object */ |
| 847 | |
| 848 | /* OpenFlow message objects */ |
| 849 | """) |
| 850 | last = 0 |
| 851 | msg_count = 0 |
| 852 | for cls in of_g.ordered_messages: |
| 853 | out.write(" %s = %d,\n" % (enum_name(cls), |
| 854 | of_g.unified[cls]["object_id"])) |
| 855 | msg_count = of_g.unified[cls]["object_id"] + 1 |
| 856 | |
| 857 | out.write("\n /* Non-message objects */\n") |
| 858 | for cls in of_g.ordered_non_messages: |
| 859 | out.write(" %s = %d,\n" % (enum_name(cls), |
| 860 | of_g.unified[cls]["object_id"])) |
| 861 | last = of_g.unified[cls]["object_id"] |
| 862 | out.write("\n /* List objects */\n") |
| 863 | for cls in of_g.ordered_list_objects: |
| 864 | out.write(" %s = %d,\n" % (enum_name(cls), |
| 865 | of_g.unified[cls]["object_id"])) |
| 866 | last = of_g.unified[cls]["object_id"] |
| 867 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 868 | out.write(""" |
| 869 | OF_OBJECT_COUNT = %d |
| 870 | } of_object_id_t; |
| 871 | |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 872 | extern const char *const of_object_id_str[]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 873 | |
| 874 | #define OF_MESSAGE_OBJECT_COUNT %d |
| 875 | """ % ((last + 1), msg_count)) |
| 876 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 877 | ################################################################ |
| 878 | # |
| 879 | # Internal Utility Functions |
| 880 | # |
| 881 | ################################################################ |
| 882 | |
| 883 | |
| 884 | def acc_name(cls, m_name): |
| 885 | """ |
| 886 | Generate the root name of an accessor function for typedef |
| 887 | @param cls The class name |
| 888 | @param m_name The member name |
| 889 | """ |
| 890 | (m_type, get_rv) = get_acc_rv(cls, m_name) |
| 891 | return "%s_%s" % (cls, m_type) |
| 892 | |
| 893 | def get_acc_rv(cls, m_name): |
| 894 | """ |
| 895 | Determine the data type and return type for a get accessor. |
| 896 | |
| 897 | The return type may be "void" or it may be the accessor type |
| 898 | depending on the system configuration and on the data type. |
| 899 | |
| 900 | @param cls The class name |
| 901 | @param m_name The member name |
| 902 | @return A pair (m_type, rv) where m_type is the unified type of the |
| 903 | member and rv is the get_accessor return type |
| 904 | """ |
| 905 | member = of_g.unified[cls]["union"][m_name] |
| 906 | m_type = member["m_type"] |
| 907 | rv = "int" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 908 | if m_type[-2:] == "_t": |
| 909 | m_type = m_type[:-2] |
| 910 | |
| 911 | return (m_type, rv) |
| 912 | |
| 913 | def param_list(cls, m_name, a_type): |
| 914 | """ |
| 915 | Generate the parameter list (no parens) for an a_type accessor |
| 916 | @param cls The class name |
| 917 | @param m_name The member name |
| 918 | @param a_type One of "set" or "get" or TBD |
| 919 | """ |
| 920 | member = of_g.unified[cls]["union"][m_name] |
| 921 | m_type = member["m_type"] |
| 922 | params = ["%s_t *obj" % cls] |
| 923 | if a_type == "set": |
| 924 | if loxi_utils.type_is_scalar(m_type): |
| 925 | params.append("%s %s" % (m_type, m_name)) |
| 926 | else: |
| 927 | params.append("%s *%s" % (m_type, m_name)) |
| 928 | elif a_type in ["get", "bind"]: |
| 929 | params.append("%s *%s" % (m_type, m_name)) |
| 930 | else: |
| 931 | debug("Class %s, name %s Bad param list a_type: %s" % |
| 932 | (cls, m_name, a_type)) |
| 933 | sys.exit(1) |
| 934 | return params |
| 935 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 936 | def field_ver_get(cls, m_name): |
| 937 | """ |
| 938 | Generate a dict, indexed by wire version, giving a pair (type, offset) |
| 939 | |
| 940 | @param cls The class name |
| 941 | @param m_name The name of the class member |
| 942 | |
| 943 | If offset is not known for m_name, the type |
| 944 | A dict is used for more convenient indexing. |
| 945 | """ |
| 946 | result = {} |
| 947 | |
| 948 | for ver in of_g.unified[cls]: |
| 949 | if type(ver) == type(0): # It's a version |
| 950 | if "use_version" in of_g.unified[cls][ver]: # deref version ref |
| 951 | ref_ver = of_g.unified[cls][ver]["use_version"] |
| 952 | members = of_g.unified[cls][ref_ver]["members"] |
| 953 | else: |
| 954 | members = of_g.unified[cls][ver]["members"] |
| 955 | idx = loxi_utils.member_to_index(m_name, members) |
| 956 | if (idx < 0): |
| 957 | continue # Member not in this version |
| 958 | m_type = members[idx]["m_type"] |
| 959 | offset = members[idx]["offset"] |
| 960 | |
| 961 | # If m_type is mixed, get wire version type from global data |
| 962 | if m_type in of_g.of_mixed_types and \ |
| 963 | ver in of_g.of_mixed_types[m_type]: |
| 964 | m_type = of_g.of_mixed_types[m_type][ver] |
| 965 | |
| 966 | # add version to result list |
| 967 | result[ver] = (m_type, offset) |
| 968 | |
| 969 | return result |
| 970 | |
| 971 | def v3_match_offset_get(cls): |
| 972 | """ |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 973 | Return the offset of an OF 1.2 match in an object if it has such; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 974 | otherwise return -1 |
| 975 | """ |
| 976 | result = field_ver_get(cls, "match") |
| 977 | if of_g.VERSION_1_2 in result: |
| 978 | if result[of_g.VERSION_1_2][0] == "of_match_v3_t": |
| 979 | return result[of_g.VERSION_1_2][1] |
| 980 | return -1 |
| 981 | |
| 982 | ################################################################ |
| 983 | # |
| 984 | # OpenFlow Object Definitions |
| 985 | # |
| 986 | ################################################################ |
| 987 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 988 | def gen_generics(out): |
| 989 | for (cls, subclasses) in type_maps.inheritance_map.items(): |
| 990 | out.write(""" |
| 991 | /** |
| 992 | * Inheritance super class for %(cls)s |
| 993 | * |
| 994 | * This class is the union of %(cls)s classes. You can refer |
| 995 | * to it untyped by refering to the member 'header' whose structure |
| 996 | * is common across all sub-classes. |
| 997 | */ |
| 998 | |
| 999 | union %(cls)s_u { |
| 1000 | %(cls)s_header_t header; /* Generic instance */ |
| 1001 | """ % dict(cls=cls)) |
| 1002 | for subcls in sorted(subclasses): |
Rich Lane | ee60d4e | 2014-10-12 20:08:24 -0700 | [diff] [blame] | 1003 | instance = loxi_utils.class_to_instance(subcls, cls) |
| 1004 | out.write(" %s_%s_t %s;\n" % (cls, instance, instance)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1005 | out.write("};\n") |
| 1006 | |
| 1007 | def gen_struct_typedefs(out): |
| 1008 | """ |
| 1009 | Generate typedefs for all struct objects |
| 1010 | @param out The file for output, already open |
| 1011 | """ |
| 1012 | |
| 1013 | out.write("\n/* LOCI inheritance parent typedefs */\n") |
| 1014 | for cls in type_maps.inheritance_map: |
| 1015 | out.write("typedef union %(cls)s_u %(cls)s_t;\n" % dict(cls=cls)) |
| 1016 | out.write("\n/* LOCI object typedefs */\n") |
| 1017 | for cls in of_g.standard_class_order: |
Rich Lane | 8841f35 | 2014-10-12 19:18:36 -0700 | [diff] [blame] | 1018 | if type_maps.class_is_inheritance_root(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1019 | continue |
Rich Lane | 8576787 | 2013-12-15 16:24:42 -0800 | [diff] [blame] | 1020 | template = "typedef of_object_t %(cls)s_t;\n" |
| 1021 | out.write(template % dict(cls=cls)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1022 | |
| 1023 | out.write(""" |
| 1024 | /**************************************************************** |
| 1025 | * |
| 1026 | * Additional of_object defines |
| 1027 | * These are needed for some static inline ops, so we put them here. |
| 1028 | * |
| 1029 | ****************************************************************/ |
| 1030 | |
| 1031 | /* Delete an OpenFlow object without reference to its type */ |
| 1032 | extern void of_object_delete(of_object_t *obj); |
| 1033 | |
| 1034 | """) |
| 1035 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1036 | ################################################################ |
| 1037 | # |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1038 | # Accessor Functions |
| 1039 | # |
| 1040 | ################################################################ |
| 1041 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1042 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1043 | def gen_accessor_declarations(out): |
| 1044 | """ |
| 1045 | Generate the declaration of each version independent accessor |
| 1046 | |
| 1047 | @param out The file to which to write the decs |
| 1048 | """ |
| 1049 | |
| 1050 | out.write(""" |
| 1051 | /**************************************************************** |
| 1052 | * |
| 1053 | * Unified, per-member accessor function declarations |
| 1054 | * |
| 1055 | ****************************************************************/ |
| 1056 | """) |
| 1057 | for cls in of_g.standard_class_order: |
Rich Lane | 8841f35 | 2014-10-12 19:18:36 -0700 | [diff] [blame] | 1058 | if type_maps.class_is_inheritance_root(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1059 | continue |
| 1060 | out.write("\n/* Unified accessor functions for %s */\n" % cls) |
| 1061 | for m_name in of_g.ordered_members[cls]: |
| 1062 | if m_name in of_g.skip_members: |
| 1063 | continue |
| 1064 | m_type = loxi_utils.member_base_type(cls, m_name) |
| 1065 | base_name = "%s_%s" % (cls, m_name) |
| 1066 | gparams = ",\n ".join(param_list(cls, m_name, "get")) |
| 1067 | get_ret_type = accessor_return_type("get", m_type) |
| 1068 | sparams = ",\n ".join(param_list(cls, m_name, "set")) |
| 1069 | set_ret_type = accessor_return_type("set", m_type) |
| 1070 | bparams = ",\n ".join(param_list(cls, m_name, "bind")) |
| 1071 | bind_ret_type = accessor_return_type("bind", m_type) |
| 1072 | |
| 1073 | if loxi_utils.type_is_of_object(m_type): |
| 1074 | # Generate bind accessors, but not get accessor |
| 1075 | out.write(""" |
| 1076 | extern %(set_ret_type)s %(base_name)s_set( |
| 1077 | %(sparams)s); |
| 1078 | extern %(bind_ret_type)s %(base_name)s_bind( |
| 1079 | %(bparams)s); |
| 1080 | extern %(m_type)s *%(cls)s_%(m_name)s_get( |
| 1081 | %(cls)s_t *obj); |
| 1082 | """ % dict(base_name=base_name, sparams=sparams, bparams=bparams, |
| 1083 | m_name=m_name, m_type=m_type, cls=cls, |
| 1084 | set_ret_type=set_ret_type, bind_ret_type=bind_ret_type)) |
| 1085 | else: |
| 1086 | out.write(""" |
| 1087 | extern %(set_ret_type)s %(base_name)s_set( |
| 1088 | %(sparams)s); |
| 1089 | extern %(get_ret_type)s %(base_name)s_get( |
| 1090 | %(gparams)s); |
| 1091 | """ % dict(base_name=base_name, gparams=gparams, sparams=sparams, |
| 1092 | get_ret_type=get_ret_type, set_ret_type=set_ret_type)) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1093 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1094 | if loxi_utils.class_is_list(cls): |
| 1095 | e_type = loxi_utils.list_to_entry_type(cls) |
| 1096 | out.write(""" |
| 1097 | extern int %(cls)s_first( |
| 1098 | %(cls)s_t *list, %(e_type)s_t *obj); |
| 1099 | extern int %(cls)s_next( |
| 1100 | %(cls)s_t *list, %(e_type)s_t *obj); |
| 1101 | extern int %(cls)s_append_bind( |
| 1102 | %(cls)s_t *list, %(e_type)s_t *obj); |
| 1103 | extern int %(cls)s_append( |
| 1104 | %(cls)s_t *list, %(e_type)s_t *obj); |
| 1105 | |
| 1106 | /** |
| 1107 | * Iteration macro for list of type %(cls)s |
| 1108 | * @param list Pointer to the list being iterated over of |
| 1109 | * type %(cls)s |
| 1110 | * @param elt Pointer to an element of type %(e_type)s |
| 1111 | * @param rv On exiting the loop will have the value OF_ERROR_RANGE. |
| 1112 | */ |
| 1113 | #define %(u_cls)s_ITER(list, elt, rv) \\ |
| 1114 | for ((rv) = %(cls)s_first((list), (elt)); \\ |
| 1115 | (rv) == OF_ERROR_NONE; \\ |
| 1116 | (rv) = %(cls)s_next((list), (elt))) |
| 1117 | """ % dict(u_cls=cls.upper(), cls=cls, e_type=e_type)) |
| 1118 | |
| 1119 | |
| 1120 | def wire_accessor(m_type, a_type): |
| 1121 | """ |
| 1122 | Returns the name of the a_type accessor for low level wire buff offset |
| 1123 | @param m_type The member type |
| 1124 | @param a_type The accessor type (set or get) |
| 1125 | """ |
| 1126 | # Strip off _t if present |
| 1127 | if m_type in of_g.of_base_types: |
| 1128 | m_type = of_g.of_base_types[m_type]["short_name"] |
| 1129 | if m_type in of_g.of_mixed_types: |
| 1130 | m_type = of_g.of_mixed_types[m_type]["short_name"] |
| 1131 | if m_type[-2:] == "_t": |
| 1132 | m_type = m_type[:-2] |
| 1133 | if m_type == "octets": |
| 1134 | m_type = "octets_data" |
| 1135 | return "of_wire_buffer_%s_%s" % (m_type, a_type) |
| 1136 | |
Rich Lane | 713d928 | 2013-12-30 15:21:35 -0800 | [diff] [blame] | 1137 | def get_len_macro(cls, m_name, m_type, version): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1138 | """ |
| 1139 | Get the length macro for m_type in cls |
| 1140 | """ |
| 1141 | if m_type.find("of_match") == 0: |
| 1142 | return "_WIRE_MATCH_PADDED_LEN(obj, offset)" |
| 1143 | if m_type.find("of_list_oxm") == 0: |
| 1144 | return "wire_match_len(obj, 0) - 4" |
| 1145 | if loxi_utils.class_is_tlv16(m_type): |
| 1146 | return "_TLV16_LEN(obj, offset)" |
| 1147 | if cls == "of_packet_out" and m_type == "of_list_action_t": |
| 1148 | return "_PACKET_OUT_ACTION_LEN(obj)" |
Rich Lane | 713d928 | 2013-12-30 15:21:35 -0800 | [diff] [blame] | 1149 | if cls == "of_bsn_gentable_entry_add" and m_name == "key": |
| 1150 | return "of_object_u16_get(obj, 18)" |
| 1151 | if cls == "of_bsn_gentable_entry_desc_stats_entry" and m_name == "key": |
| 1152 | return "of_object_u16_get(obj, 2)" |
| 1153 | if cls == "of_bsn_gentable_entry_stats_entry" and m_name == "key": |
| 1154 | return "of_object_u16_get(obj, 2)" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1155 | # Default is everything to the end of the object |
| 1156 | return "_END_LEN(obj, offset)" |
| 1157 | |
| 1158 | def gen_accessor_offsets(out, cls, m_name, version, a_type, m_type, offset): |
| 1159 | """ |
| 1160 | Generate the sub-object offset and length calculations for accessors |
| 1161 | @param out File being written |
| 1162 | @param m_name Name of member |
| 1163 | @param version Wire version being processed |
| 1164 | @param a_type The accessor type (set or get) |
| 1165 | @param m_type The original member type |
| 1166 | @param offset The offset of the object or -1 if not fixed |
| 1167 | """ |
| 1168 | # determine offset |
| 1169 | o_str = "%d" % offset # Default is fixed length |
| 1170 | if offset == -1: |
| 1171 | # There are currently 4 special cases for this |
| 1172 | # In general, get offset and length of predecessor |
| 1173 | if (loxi_utils.cls_is_flow_mod(cls) and m_name == "instructions"): |
| 1174 | pass |
| 1175 | elif (cls == "of_flow_stats_entry" and m_name == "instructions"): |
| 1176 | pass |
| 1177 | elif (cls == "of_packet_in" and m_name == "data"): |
| 1178 | pass |
| 1179 | elif (cls == "of_packet_out" and m_name == "data"): |
| 1180 | pass |
Rich Lane | 713d928 | 2013-12-30 15:21:35 -0800 | [diff] [blame] | 1181 | elif (cls == "of_bsn_gentable_entry_add" and m_name == "value"): |
| 1182 | pass |
| 1183 | elif (cls == "of_bsn_gentable_entry_desc_stats_entry" and m_name == "value"): |
| 1184 | pass |
| 1185 | elif (cls == "of_bsn_gentable_entry_stats_entry" and m_name == "stats"): |
| 1186 | pass |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1187 | else: |
| 1188 | debug("Error: Unknown member with offset == -1") |
| 1189 | debug(" cls %s, m_name %s, version %d" % (cls, m_name, version)) |
| 1190 | sys.exit(1) |
| 1191 | o_str = "_%s_%s_OFFSET(obj)" % (cls.upper()[3:], m_name.upper()) |
| 1192 | |
| 1193 | out.write("""\ |
| 1194 | offset = %s; |
| 1195 | """ % o_str); |
| 1196 | |
| 1197 | # This could be moved to main body but for version check |
| 1198 | if not loxi_utils.type_is_scalar(m_type): |
| 1199 | if loxi_utils.class_is_var_len(m_type[:-2], version) or \ |
| 1200 | m_type == "of_match_t": |
Rich Lane | 713d928 | 2013-12-30 15:21:35 -0800 | [diff] [blame] | 1201 | len_macro = get_len_macro(cls, m_name, m_type, version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1202 | else: |
| 1203 | len_macro = "%d" % of_g.base_length[(m_type[:-2], version)] |
| 1204 | out.write(" cur_len = %s;\n" % len_macro) |
| 1205 | out.write(" break;\n") |
| 1206 | |
| 1207 | def length_of(m_type, version): |
| 1208 | """ |
| 1209 | Return the length of a type based on the version |
| 1210 | """ |
| 1211 | if m_type in of_g.of_mixed_types: |
| 1212 | m_type = of_g.of_mixed_types[m_type][version] |
| 1213 | if m_type in of_g.of_base_types: |
| 1214 | return of_g.of_base_types[m_type]["bytes"] |
| 1215 | if (m_type[:-2], version) in of_g.base_length: |
| 1216 | return of_g.base_length[(m_type[:-2], version)] |
| 1217 | print "Unknown length request", m_type, version |
| 1218 | sys.exit(1) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1219 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1220 | |
| 1221 | def gen_get_accessor_body(out, cls, m_type, m_name): |
| 1222 | """ |
| 1223 | Generate the common operations for a get accessor |
| 1224 | """ |
| 1225 | if loxi_utils.type_is_scalar(m_type): |
| 1226 | ver = "" # See if version required for scalar update |
| 1227 | if m_type in of_g.of_mixed_types: |
| 1228 | ver = "ver, " |
| 1229 | out.write("""\ |
| 1230 | %(wa)s(%(ver)swbuf, abs_offset, %(m_name)s); |
| 1231 | """ % dict(wa=wire_accessor(m_type, "get"), ver=ver, m_name=m_name)) |
| 1232 | |
| 1233 | if m_type == "of_port_no_t": |
| 1234 | out.write(" OF_PORT_NO_VALUE_CHECK(*%s, ver);\n" % m_name) |
| 1235 | elif m_type == "of_octets_t": |
| 1236 | out.write("""\ |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1237 | LOCI_ASSERT(cur_len + abs_offset <= WBUF_CURRENT_BYTES(wbuf)); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1238 | %(m_name)s->bytes = cur_len; |
| 1239 | %(m_name)s->data = OF_WIRE_BUFFER_INDEX(wbuf, abs_offset); |
| 1240 | """ % dict(m_name=m_name)) |
| 1241 | elif m_type == "of_match_t": |
| 1242 | out.write(""" |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1243 | LOCI_ASSERT(cur_len + abs_offset <= WBUF_CURRENT_BYTES(wbuf)); |
Rich Lane | e0b70cc | 2014-06-12 15:02:22 -0700 | [diff] [blame] | 1244 | OF_TRY(of_match_deserialize(ver, %(m_name)s, obj, offset, cur_len)); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1245 | """ % dict(m_name=m_name)) |
Rich Lane | 90020b4 | 2014-04-07 12:05:45 -0700 | [diff] [blame] | 1246 | elif m_type == "of_oxm_header_t": |
| 1247 | out.write(""" |
| 1248 | /* Initialize child */ |
| 1249 | %(m_type)s_init(%(m_name)s, obj->version, 0, 1); |
| 1250 | /* Attach to parent */ |
Rich Lane | d1fe697 | 2014-06-12 14:53:24 -0700 | [diff] [blame] | 1251 | of_object_attach(obj, %(m_name)s, offset, cur_len); |
Rich Lane | 90020b4 | 2014-04-07 12:05:45 -0700 | [diff] [blame] | 1252 | of_object_wire_init(%(m_name)s, OF_OXM, 0); |
| 1253 | """ % dict(m_type=m_type[:-2], m_name=m_name)) |
Wilson Ng | d618188 | 2014-04-14 16:28:35 -0700 | [diff] [blame] | 1254 | elif m_type == "of_bsn_vport_header_t": |
| 1255 | out.write(""" |
| 1256 | /* Initialize child */ |
| 1257 | %(m_type)s_init(%(m_name)s, obj->version, 0, 1); |
| 1258 | /* Attach to parent */ |
Rich Lane | d1fe697 | 2014-06-12 14:53:24 -0700 | [diff] [blame] | 1259 | of_object_attach(obj, %(m_name)s, offset, cur_len); |
Wilson Ng | d618188 | 2014-04-14 16:28:35 -0700 | [diff] [blame] | 1260 | of_object_wire_init(%(m_name)s, OF_BSN_VPORT, 0); |
| 1261 | """ % dict(m_type=m_type[:-2], m_name=m_name)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1262 | else: |
| 1263 | out.write(""" |
| 1264 | /* Initialize child */ |
| 1265 | %(m_type)s_init(%(m_name)s, obj->version, 0, 1); |
| 1266 | /* Attach to parent */ |
Rich Lane | d1fe697 | 2014-06-12 14:53:24 -0700 | [diff] [blame] | 1267 | of_object_attach(obj, %(m_name)s, offset, cur_len); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1268 | """ % dict(m_type=m_type[:-2], m_name=m_name)) |
| 1269 | |
| 1270 | |
| 1271 | def gen_set_accessor_body(out, cls, m_type, m_name): |
| 1272 | """ |
| 1273 | Generate the contents of a set accessor |
| 1274 | """ |
| 1275 | if loxi_utils.type_is_scalar(m_type) or m_type == "of_octets_t": |
| 1276 | ver = "" # See if version required for scalar update |
| 1277 | if m_type in of_g.of_mixed_types: |
| 1278 | ver = "ver, " |
| 1279 | cur_len = "" # See if version required for scalar update |
| 1280 | if m_type == "of_octets_t": |
| 1281 | cur_len = ", cur_len" |
| 1282 | out.write("""\ |
| 1283 | new_len = %(m_name)s->bytes; |
| 1284 | of_wire_buffer_grow(wbuf, abs_offset + (new_len - cur_len)); |
| 1285 | """ % dict(m_name=m_name)) |
| 1286 | out.write("""\ |
| 1287 | %(wa)s(%(ver)swbuf, abs_offset, %(m_name)s%(cur_len)s); |
| 1288 | """ % dict(wa=wire_accessor(m_type, "set"), ver=ver, cur_len=cur_len, |
| 1289 | m_name=m_name)) |
| 1290 | |
| 1291 | elif m_type == "of_match_t": |
| 1292 | out.write(""" |
Rich Lane | e0b70cc | 2014-06-12 15:02:22 -0700 | [diff] [blame] | 1293 | { |
| 1294 | /* Match object */ |
| 1295 | of_octets_t match_octets; |
| 1296 | OF_TRY(of_match_serialize(ver, %(m_name)s, &match_octets)); |
| 1297 | new_len = match_octets.bytes; |
| 1298 | of_wire_buffer_replace_data(wbuf, abs_offset, cur_len, |
| 1299 | match_octets.data, new_len); |
| 1300 | /* Free match serialized octets */ |
| 1301 | FREE(match_octets.data); |
| 1302 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1303 | """ % dict(m_name=m_name)) |
| 1304 | |
| 1305 | else: # Other object type |
| 1306 | out.write("\n /* LOCI object type */") |
| 1307 | # Need to special case OXM list |
| 1308 | out.write(""" |
| 1309 | new_len = %(m_name)s->length; |
| 1310 | /* If underlying buffer already shared; nothing to do */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 1311 | if (obj->wbuf == %(m_name)s->wbuf) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1312 | of_wire_buffer_grow(wbuf, abs_offset + new_len); |
| 1313 | /* Verify that the offsets are correct */ |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1314 | LOCI_ASSERT(abs_offset == OF_OBJECT_ABSOLUTE_OFFSET(%(m_name)s, 0)); |
| 1315 | /* LOCI_ASSERT(new_len == cur_len); */ /* fixme: may fail for OXM lists */ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1316 | return %(ret_success)s; |
| 1317 | } |
| 1318 | |
| 1319 | /* Otherwise, replace existing object in data buffer */ |
| 1320 | of_wire_buffer_replace_data(wbuf, abs_offset, cur_len, |
| 1321 | OF_OBJECT_BUFFER_INDEX(%(m_name)s, 0), new_len); |
| 1322 | """ % dict(m_name=m_name, ret_success=accessor_return_success("set", m_type))) |
| 1323 | |
| 1324 | if not loxi_utils.type_is_scalar(m_type): |
| 1325 | if cls == "of_packet_out" and m_type == "of_list_action_t": |
| 1326 | out.write(""" |
| 1327 | /* Special case for setting action lengths */ |
| 1328 | _PACKET_OUT_ACTION_LEN_SET(obj, %(m_name)s->length); |
| 1329 | """ % dict(m_name=m_name)) |
Rich Lane | 713d928 | 2013-12-30 15:21:35 -0800 | [diff] [blame] | 1330 | elif cls == "of_bsn_gentable_entry_add" and m_name == "key": |
| 1331 | out.write(""" |
| 1332 | /* Special case for setting key length */ |
| 1333 | of_object_u16_set(obj, 18, %(m_name)s->length); |
| 1334 | """ % dict(m_name=m_name)) |
| 1335 | elif cls in ["of_bsn_gentable_entry_desc_stats_entry", "of_bsn_gentable_entry_stats_entry"] and m_name == "key": |
| 1336 | out.write(""" |
| 1337 | /* Special case for setting key length */ |
| 1338 | of_object_u16_set(obj, 2, %(m_name)s->length); |
| 1339 | """ % dict(m_name=m_name)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1340 | elif m_type not in ["of_match_t", "of_octets_t"]: |
| 1341 | out.write(""" |
| 1342 | /* @fixme Shouldn't this precede copying value's data to buffer? */ |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 1343 | of_object_wire_length_set((of_object_t *)%(m_name)s, %(m_name)s->length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1344 | """ % dict(m_name=m_name)) |
| 1345 | out.write(""" |
| 1346 | /* Not scalar, update lengths if needed */ |
| 1347 | delta = new_len - cur_len; |
| 1348 | if (delta != 0) { |
| 1349 | /* Update parent(s) */ |
| 1350 | of_object_parent_length_update((of_object_t *)obj, delta); |
| 1351 | } |
| 1352 | """) |
| 1353 | |
| 1354 | def obj_assert_check(cls): |
| 1355 | """ |
| 1356 | The body of the assert check for an accessor |
| 1357 | We allow all versions of add/delete/modify to use the same accessors |
| 1358 | """ |
| 1359 | if cls in ["of_flow_modify", "of_flow_modify_strict", |
| 1360 | "of_flow_delete", "of_flow_delete_strict", |
| 1361 | "of_flow_add"]: |
| 1362 | return "IS_FLOW_MOD_SUBTYPE(obj->object_id)" |
| 1363 | else: |
| 1364 | return "obj->object_id == %s" % cls.upper() |
| 1365 | |
| 1366 | def gen_of_object_get(out, cls, m_name, m_type): |
| 1367 | sub_cls = m_type[:-2] |
| 1368 | out.write(""" |
| 1369 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1370 | * Create a copy of %(m_name)s into a new variable of type %(m_type)s from |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1371 | * a %(cls)s instance. |
| 1372 | * |
| 1373 | * @param obj Pointer to the source of type %(cls)s_t |
| 1374 | * @returns A pointer to a new instance of type %(m_type)s whose contents |
| 1375 | * match that of %(m_name)s from source |
| 1376 | * @returns NULL if an error occurs |
| 1377 | */ |
| 1378 | %(m_type)s * |
| 1379 | %(cls)s_%(m_name)s_get(%(cls)s_t *obj) { |
| 1380 | %(m_type)s _%(m_name)s; |
| 1381 | %(m_type)s *_%(m_name)s_ptr; |
| 1382 | |
| 1383 | %(cls)s_%(m_name)s_bind(obj, &_%(m_name)s); |
| 1384 | _%(m_name)s_ptr = (%(m_type)s *)of_object_dup(&_%(m_name)s); |
| 1385 | return _%(m_name)s_ptr; |
| 1386 | } |
| 1387 | """ % dict(m_name=m_name, m_type=m_type, cls=cls, sub_cls=sub_cls)) |
| 1388 | |
| 1389 | def gen_unified_acc_body(out, cls, m_name, ver_type_map, a_type, m_type): |
| 1390 | """ |
| 1391 | Generate the body of a set or get accessor function |
| 1392 | |
| 1393 | @param out The file to which to write the decs |
| 1394 | @param cls The class name |
| 1395 | @param m_name The member name |
| 1396 | @param ver_type_map Maps (type, offset) pairs to a list of versions |
| 1397 | @param a_type The accessor type, set or get |
| 1398 | @param m_type The original member type |
| 1399 | |
| 1400 | The type values in ver_type_map are now ignored as we've pushed down |
| 1401 | the type munging to the lower level. |
| 1402 | |
| 1403 | This is unified because the version switch case processing is the |
| 1404 | same for both set and get |
| 1405 | """ |
| 1406 | out.write("""{ |
| 1407 | of_wire_buffer_t *wbuf; |
| 1408 | int offset = 0; /* Offset of value relative to the start obj */ |
| 1409 | int abs_offset; /* Offset of value relative to start of wbuf */ |
| 1410 | of_version_t ver; |
| 1411 | """) |
| 1412 | |
| 1413 | if not loxi_utils.type_is_scalar(m_type): |
| 1414 | out.write("""\ |
| 1415 | int cur_len = 0; /* Current length of object data */ |
| 1416 | """) |
| 1417 | if a_type == "set": |
| 1418 | out.write("""\ |
| 1419 | int new_len, delta; /* For set, need new length and delta */ |
| 1420 | """) |
| 1421 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1422 | out.write(""" |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1423 | LOCI_ASSERT(%(assert_str)s); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1424 | ver = obj->version; |
| 1425 | wbuf = OF_OBJECT_TO_WBUF(obj); |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1426 | LOCI_ASSERT(wbuf != NULL); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1427 | |
| 1428 | /* By version, determine offset and current length (where needed) */ |
| 1429 | switch (ver) { |
| 1430 | """ % dict(assert_str=obj_assert_check(cls))) |
| 1431 | |
| 1432 | for first in sorted(ver_type_map): |
| 1433 | (prev_t, prev_o) = ver_type_map[first] |
| 1434 | prev_len = length_of(prev_t, first) |
| 1435 | prev = first |
| 1436 | out.write(" case %s:\n" % of_g.wire_ver_map[first]) |
| 1437 | break |
| 1438 | |
| 1439 | for next in sorted(ver_type_map): |
| 1440 | if next == first: |
| 1441 | continue |
| 1442 | |
| 1443 | (t, o) = ver_type_map[next] |
| 1444 | cur_len = length_of(t, next) |
| 1445 | if o == prev_o and cur_len == prev_len: |
| 1446 | out.write(" case %s:\n" % of_g.wire_ver_map[next]) |
| 1447 | continue |
| 1448 | gen_accessor_offsets(out, cls, m_name, prev, a_type, m_type, prev_o) |
| 1449 | out.write(" case %s:\n" % of_g.wire_ver_map[next]) |
| 1450 | (prev_t, prev_o, prev_len, prev) = (t, o, cur_len, next) |
| 1451 | |
| 1452 | gen_accessor_offsets(out, cls, m_name, next, a_type, m_type, prev_o) |
| 1453 | out.write("""\ |
| 1454 | default: |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1455 | LOCI_ASSERT(0); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | abs_offset = OF_OBJECT_ABSOLUTE_OFFSET(obj, offset); |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1459 | LOCI_ASSERT(abs_offset >= 0); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1460 | """) |
| 1461 | if not loxi_utils.type_is_scalar(m_type): |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1462 | out.write(" LOCI_ASSERT(cur_len >= 0 && cur_len < 64 * 1024);\n") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1463 | |
| 1464 | # Now generate the common accessor code |
| 1465 | if a_type in ["get", "bind"]: |
| 1466 | gen_get_accessor_body(out, cls, m_type, m_name) |
| 1467 | else: |
| 1468 | gen_set_accessor_body(out, cls, m_type, m_name) |
| 1469 | |
| 1470 | out.write(""" |
| 1471 | OF_LENGTH_CHECK_ASSERT(obj); |
| 1472 | |
| 1473 | return %s; |
| 1474 | } |
| 1475 | """ % accessor_return_success(a_type, m_type)) |
| 1476 | |
| 1477 | def gen_of_obj_bind(out, cls, m_name, m_type, ver_type_map): |
| 1478 | """ |
| 1479 | For generating the bind call for OF sub-objects |
| 1480 | """ |
| 1481 | |
| 1482 | params = ",\n ".join(param_list(cls, m_name, "bind")) |
| 1483 | out.write(""" |
| 1484 | /** |
| 1485 | * Bind an object of type %(m_type)s to the parent of type %(cls)s for |
| 1486 | * member %(m_name)s |
| 1487 | * @param obj Pointer to an object of type %(cls)s. |
| 1488 | * @param %(m_name)s Pointer to the child object of type |
| 1489 | * %(m_type)s to be filled out. |
| 1490 | * \ingroup %(cls)s |
| 1491 | * |
| 1492 | * The parameter %(m_name)s is filled out to point to the same underlying |
| 1493 | * wire buffer as its parent. |
| 1494 | * |
| 1495 | */ |
| 1496 | """ % dict(m_name=m_name, cls=cls, m_type=m_type)) |
| 1497 | |
| 1498 | ret_type = accessor_return_type("bind", m_type) |
| 1499 | out.write("%s\n%s_%s_bind(\n %s)\n" % (ret_type, cls, m_name, params)) |
| 1500 | gen_unified_acc_body(out, cls, m_name, ver_type_map, "bind", m_type) |
| 1501 | |
| 1502 | def gen_get_accessor(out, cls, m_name, m_type, ver_type_map): |
| 1503 | """ |
| 1504 | For generating the get call for non- OF sub-objects |
| 1505 | """ |
| 1506 | params = ",\n ".join(param_list(cls, m_name, "get")) |
| 1507 | out.write(""" |
| 1508 | /** |
| 1509 | * Get %(m_name)s from an object of type %(cls)s. |
| 1510 | * @param obj Pointer to an object of type %(cls)s. |
| 1511 | * @param %(m_name)s Pointer to the child object of type |
| 1512 | * %(m_type)s to be filled out. |
| 1513 | * |
| 1514 | */ |
| 1515 | """ % dict(m_name=m_name, cls=cls, m_type=m_type)) |
| 1516 | |
| 1517 | ret_type = accessor_return_type("get", m_type) |
| 1518 | out.write("%s\n%s_%s_get(\n %s)\n" % (ret_type, cls, m_name, params)) |
| 1519 | gen_unified_acc_body(out, cls, m_name, ver_type_map, "get", m_type) |
| 1520 | |
Rich Lane | ce2e464 | 2013-12-15 12:05:45 -0800 | [diff] [blame] | 1521 | def gen_accessor_definitions(out, cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1522 | for m_name in of_g.ordered_members[cls]: |
| 1523 | if m_name in of_g.skip_members: |
| 1524 | continue |
| 1525 | m_type = loxi_utils.member_base_type(cls, m_name) |
| 1526 | ver_type_map = field_ver_get(cls, m_name) |
| 1527 | |
| 1528 | # Generate get/bind pending on member type |
| 1529 | # FIXME: Does this do the right thing for match? |
| 1530 | if loxi_utils.type_is_of_object(m_type): |
| 1531 | gen_of_obj_bind(out, cls, m_name, m_type, ver_type_map) |
| 1532 | gen_of_object_get(out, cls, m_name, m_type) |
| 1533 | else: |
| 1534 | gen_get_accessor(out, cls, m_name, m_type, ver_type_map) |
| 1535 | |
| 1536 | # Now generate set accessor for all objects |
| 1537 | params = ",\n ".join(param_list(cls, m_name, "set")) |
| 1538 | out.write(""" |
| 1539 | /** |
| 1540 | * Set %(m_name)s in an object of type %(cls)s. |
| 1541 | * @param obj Pointer to an object of type %(cls)s. |
| 1542 | """ % dict(m_name=m_name, cls=cls, m_type=m_type)) |
| 1543 | if loxi_utils.type_is_scalar(m_type) or m_type == "of_octets_t": |
| 1544 | out.write("""\ |
| 1545 | * @param %(m_name)s The value to write into the object |
| 1546 | */ |
| 1547 | """ % dict(m_name=m_name, cls=cls, m_type=m_type)) |
| 1548 | else: |
| 1549 | out.write("""\ |
| 1550 | * @param %(m_name)s Pointer to the child of type %(m_type)s. |
| 1551 | * |
| 1552 | * If the child's wire buffer is the same as the parent's, then |
| 1553 | * nothing is done as the changes have already been registered in the |
| 1554 | * parent. Otherwise, the data in the child's wire buffer is inserted |
| 1555 | * into the parent's and the appropriate lengths are updated. |
| 1556 | */ |
| 1557 | """ % dict(m_name=m_name, cls=cls, m_type=m_type)) |
| 1558 | ret_type = accessor_return_type("set", m_type) |
| 1559 | out.write("%s\n%s_%s_set(\n %s)\n" % (ret_type, cls, m_name, params)) |
| 1560 | gen_unified_acc_body(out, cls, m_name, ver_type_map, "set", m_type) |
| 1561 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1562 | ################################################################ |
| 1563 | # |
| 1564 | # New/Delete Function Definitions |
| 1565 | # |
| 1566 | ################################################################ |
| 1567 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1568 | ################################################################ |
| 1569 | # Routines to generate the body of new/delete functions |
| 1570 | ################################################################ |
| 1571 | |
| 1572 | def gen_init_fn_body(cls, out): |
| 1573 | """ |
| 1574 | Generate function body for init function |
| 1575 | @param cls The class name for the function |
| 1576 | @param out The file to which to write |
| 1577 | """ |
Rich Lane | 8841f35 | 2014-10-12 19:18:36 -0700 | [diff] [blame] | 1578 | if type_maps.class_is_inheritance_root(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1579 | param = "obj_p" |
| 1580 | else: |
| 1581 | param = "obj" |
| 1582 | |
| 1583 | out.write(""" |
| 1584 | /** |
| 1585 | * Initialize an object of type %(cls)s. |
| 1586 | * |
| 1587 | * @param obj Pointer to the object to initialize |
| 1588 | * @param version The wire version to use for the object |
| 1589 | * @param bytes How many bytes in the object |
| 1590 | * @param clean_wire Boolean: If true, clear the wire object control struct |
| 1591 | * |
| 1592 | * If bytes < 0, then the default fixed length is used for the object |
| 1593 | * |
| 1594 | * This is a "coerce" function that sets up the pointers for the |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1595 | * accessors properly. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1596 | * |
| 1597 | * If anything other than 0 is passed in for the buffer size, the underlying |
| 1598 | * wire buffer will have 'grow' called. |
| 1599 | */ |
| 1600 | |
| 1601 | void |
| 1602 | %(cls)s_init(%(cls)s_t *%(param)s, |
| 1603 | of_version_t version, int bytes, int clean_wire) |
| 1604 | { |
| 1605 | """ % dict(cls=cls, param=param)) |
| 1606 | |
| 1607 | # Use an extra pointer to deal with inheritance classes |
Rich Lane | 8841f35 | 2014-10-12 19:18:36 -0700 | [diff] [blame] | 1608 | if type_maps.class_is_inheritance_root(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1609 | out.write("""\ |
| 1610 | %s_header_t *obj; |
| 1611 | |
| 1612 | obj = &obj_p->header; /* Need instantiable subclass */ |
| 1613 | """ % cls) |
| 1614 | |
| 1615 | out.write(""" |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 1616 | LOCI_ASSERT(of_object_fixed_len[version][%(enum)s] >= 0); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1617 | if (clean_wire) { |
| 1618 | MEMSET(obj, 0, sizeof(*obj)); |
| 1619 | } |
| 1620 | if (bytes < 0) { |
Rich Lane | f70be94 | 2013-07-18 13:33:14 -0700 | [diff] [blame] | 1621 | bytes = of_object_fixed_len[version][%(enum)s] + of_object_extra_len[version][%(enum)s]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1622 | } |
| 1623 | obj->version = version; |
| 1624 | obj->length = bytes; |
| 1625 | obj->object_id = %(enum)s; |
| 1626 | """ % dict(cls=cls, enum=enum_name(cls))) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1627 | |
| 1628 | out.write(""" |
| 1629 | /* Grow the wire buffer */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 1630 | if (obj->wbuf != NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1631 | int tot_bytes; |
| 1632 | |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 1633 | tot_bytes = bytes + obj->obj_offset; |
| 1634 | of_wire_buffer_grow(obj->wbuf, tot_bytes); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | """) |
| 1639 | |
| 1640 | ## @fixme This should also be updated once there is a map from |
| 1641 | # class instance to wire length/type accessors |
| 1642 | def gen_wire_push_fn(cls, out): |
| 1643 | """ |
| 1644 | Generate the calls to push values into the wire buffer |
| 1645 | """ |
| 1646 | if type_maps.class_is_virtual(cls): |
| 1647 | print "Push fn gen called for virtual class " + cls |
| 1648 | return |
| 1649 | |
| 1650 | out.write(""" |
| 1651 | /** |
| 1652 | * Helper function to push values into the wire buffer |
| 1653 | */ |
| 1654 | static inline int |
| 1655 | %(cls)s_push_wire_values(%(cls)s_t *obj) |
| 1656 | { |
| 1657 | """ % dict(cls=cls)) |
| 1658 | |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 1659 | import loxi_globals |
| 1660 | uclass = loxi_globals.unified.class_by_name(cls) |
| 1661 | if uclass and not uclass.virtual and uclass.has_type_members: |
| 1662 | out.write(""" |
| 1663 | %(cls)s_push_wire_types(obj); |
| 1664 | """ % dict(cls=cls)) |
| 1665 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1666 | if loxi_utils.class_is_message(cls): |
| 1667 | out.write(""" |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 1668 | /* Message obj; set length */ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1669 | of_message_t msg; |
| 1670 | |
| 1671 | if ((msg = OF_OBJECT_TO_MESSAGE(obj)) != NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1672 | of_message_length_set(msg, obj->length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1673 | } |
| 1674 | """ % dict(name = enum_name(cls))) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1675 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1676 | else: # Not a message |
| 1677 | if loxi_utils.class_is_tlv16(cls): |
| 1678 | out.write(""" |
Rich Lane | bdd8e29 | 2013-12-06 17:37:39 -0800 | [diff] [blame] | 1679 | /* TLV obj; set length */ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1680 | of_tlv16_wire_length_set((of_object_t *)obj, obj->length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1681 | """ % dict(enum=enum_name(cls))) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1682 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1683 | if loxi_utils.class_is_u16_len(cls) or cls == "of_packet_queue": |
| 1684 | out.write(""" |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 1685 | of_object_wire_length_set((of_object_t *)obj, obj->length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1686 | """) |
| 1687 | |
| 1688 | if cls == "of_meter_stats": |
| 1689 | out.write(""" |
| 1690 | of_meter_stats_wire_length_set((of_object_t *)obj, obj->length); |
| 1691 | """ % dict(enum=enum_name(cls))) |
| 1692 | |
| 1693 | out.write(""" |
| 1694 | return OF_ERROR_NONE; |
| 1695 | } |
| 1696 | """) |
| 1697 | |
| 1698 | def gen_new_fn_body(cls, out): |
| 1699 | """ |
| 1700 | Generate function body for new function |
| 1701 | @param cls The class name for the function |
| 1702 | @param out The file to which to write |
| 1703 | """ |
| 1704 | |
| 1705 | out.write(""" |
| 1706 | /** |
| 1707 | * \\defgroup %(cls)s %(cls)s |
| 1708 | */ |
| 1709 | """ % dict(cls=cls)) |
| 1710 | |
| 1711 | if not type_maps.class_is_virtual(cls): |
| 1712 | gen_wire_push_fn(cls, out) |
| 1713 | |
Rich Lane | 37911fd | 2014-04-01 22:11:22 -0700 | [diff] [blame] | 1714 | uclass = loxi_globals.unified.class_by_name(cls) |
| 1715 | is_fixed_length = uclass and uclass.is_fixed_length |
| 1716 | max_length = is_fixed_length and "bytes" or "OF_WIRE_BUFFER_MAX_LENGTH" |
| 1717 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1718 | out.write(""" |
| 1719 | /** |
| 1720 | * Create a new %(cls)s object |
| 1721 | * |
| 1722 | * @param version The wire version to use for the object |
| 1723 | * @return Pointer to the newly create object or NULL on error |
| 1724 | * |
| 1725 | * Initializes the new object with it's default fixed length associating |
| 1726 | * a new underlying wire buffer. |
| 1727 | * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1728 | * \\ingroup %(cls)s |
| 1729 | */ |
| 1730 | |
| 1731 | %(cls)s_t * |
Rich Lane | cd6ef15 | 2013-12-15 16:42:18 -0800 | [diff] [blame] | 1732 | %(cls)s_new(of_version_t version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1733 | { |
| 1734 | %(cls)s_t *obj; |
| 1735 | int bytes; |
| 1736 | |
Rich Lane | f70be94 | 2013-07-18 13:33:14 -0700 | [diff] [blame] | 1737 | bytes = of_object_fixed_len[version][%(enum)s] + of_object_extra_len[version][%(enum)s]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1738 | |
Rich Lane | 37911fd | 2014-04-01 22:11:22 -0700 | [diff] [blame] | 1739 | if ((obj = (%(cls)s_t *)of_object_new(%(max_length)s)) == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1740 | return NULL; |
| 1741 | } |
| 1742 | |
| 1743 | %(cls)s_init(obj, version, bytes, 0); |
Rich Lane | 37911fd | 2014-04-01 22:11:22 -0700 | [diff] [blame] | 1744 | """ % dict(cls=cls, enum=enum_name(cls), max_length=max_length)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1745 | if not type_maps.class_is_virtual(cls): |
| 1746 | out.write(""" |
| 1747 | if (%(cls)s_push_wire_values(obj) < 0) { |
| 1748 | FREE(obj); |
| 1749 | return NULL; |
| 1750 | } |
| 1751 | """ % dict(cls=cls)) |
| 1752 | |
| 1753 | match_offset = v3_match_offset_get(cls) |
| 1754 | if match_offset >= 0: |
| 1755 | # Init length field for match object |
| 1756 | out.write(""" |
| 1757 | /* Initialize match TLV for 1.2 */ |
| 1758 | /* FIXME: Check 1.3 below */ |
| 1759 | if ((version == OF_VERSION_1_2) || (version == OF_VERSION_1_3)) { |
| 1760 | of_object_u16_set((of_object_t *)obj, %(match_offset)d + 2, 4); |
| 1761 | } |
| 1762 | """ % dict(match_offset=match_offset)) |
| 1763 | out.write(""" |
| 1764 | return obj; |
| 1765 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1766 | """ % dict(cls=cls)) |
| 1767 | |
| 1768 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1769 | ################################################################ |
| 1770 | # Now the top level generator functions |
| 1771 | ################################################################ |
| 1772 | |
| 1773 | def gen_new_function_declarations(out): |
| 1774 | """ |
| 1775 | Gerenate the header file declarations for new operators for all classes |
| 1776 | @param out The file to which to write the decs |
| 1777 | """ |
| 1778 | |
| 1779 | out.write(""" |
| 1780 | /**************************************************************** |
| 1781 | * |
| 1782 | * New operator declarations |
| 1783 | * |
| 1784 | * _new: Create a new object for writing; includes init |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1785 | * _init: Initialize and optionally allocate buffer space for an |
| 1786 | * automatic instance |
| 1787 | * |
Rich Lane | bb8f17c | 2014-06-12 13:14:09 -0700 | [diff] [blame] | 1788 | * _new and requires a delete operation to be called on the object. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1789 | * |
| 1790 | ****************************************************************/ |
| 1791 | """) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1792 | |
| 1793 | for cls in of_g.standard_class_order: |
| 1794 | out.write(""" |
| 1795 | extern %(cls)s_t * |
Rich Lane | cd6ef15 | 2013-12-15 16:42:18 -0800 | [diff] [blame] | 1796 | %(cls)s_new(of_version_t version); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1797 | """ % dict(cls=cls)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1798 | out.write("""extern void %(cls)s_init( |
| 1799 | %(cls)s_t *obj, of_version_t version, int bytes, int clean_wire); |
| 1800 | """ % dict(cls=cls)) |
| 1801 | |
| 1802 | out.write(""" |
| 1803 | /**************************************************************** |
| 1804 | * |
| 1805 | * Delete operator static inline definitions. |
| 1806 | * These are here for type checking purposes only |
| 1807 | * |
| 1808 | ****************************************************************/ |
| 1809 | """) |
| 1810 | for cls in of_g.standard_class_order: |
Rich Lane | 8841f35 | 2014-10-12 19:18:36 -0700 | [diff] [blame] | 1811 | # if type_maps.class_is_inheritance_root(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1812 | # continue |
| 1813 | out.write(""" |
| 1814 | /** |
| 1815 | * Delete an object of type %(cls)s_t |
| 1816 | * @param obj An instance of type %(cls)s_t |
| 1817 | * |
| 1818 | * \ingroup %(cls)s |
| 1819 | */ |
| 1820 | static inline void |
| 1821 | %(cls)s_delete(%(cls)s_t *obj) { |
| 1822 | of_object_delete((of_object_t *)(obj)); |
| 1823 | } |
| 1824 | """ % dict(cls=cls)) |
| 1825 | |
| 1826 | out.write(""" |
| 1827 | typedef void (*of_object_init_f)(of_object_t *obj, of_version_t version, |
| 1828 | int bytes, int clean_wire); |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 1829 | extern const of_object_init_f of_object_init_map[]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1830 | """) |
| 1831 | |
| 1832 | out.write(""" |
| 1833 | /**************************************************************** |
| 1834 | * |
| 1835 | * Function pointer initialization functions |
| 1836 | * These are part of the "coerce" type casting for objects |
| 1837 | * |
| 1838 | ****************************************************************/ |
| 1839 | """) |
| 1840 | |
Rich Lane | b604e33 | 2013-12-15 13:23:51 -0800 | [diff] [blame] | 1841 | def gen_new_function_definitions(out, cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1842 | """ |
| 1843 | Generate the new operator for all classes |
| 1844 | |
| 1845 | @param out The file to which to write the functions |
| 1846 | """ |
| 1847 | |
Rich Lane | b604e33 | 2013-12-15 13:23:51 -0800 | [diff] [blame] | 1848 | gen_new_fn_body(cls, out) |
| 1849 | gen_init_fn_body(cls, out) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1850 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1851 | """ |
| 1852 | Document generation functions |
| 1853 | |
| 1854 | The main reason this is here is to generate documentation per |
| 1855 | accessor that indicates the versions that support the interface. |
| 1856 | """ |
| 1857 | |
| 1858 | |
| 1859 | def gen_accessor_doc(out, name): |
| 1860 | """ |
| 1861 | Generate documentation for each accessor function that indicates |
| 1862 | the versions supporting the accessor. |
| 1863 | """ |
| 1864 | |
| 1865 | common_top_matter(out, name) |
| 1866 | |
| 1867 | out.write("/* DOCUMENTATION ONLY */\n") |
| 1868 | |
| 1869 | for cls in of_g.standard_class_order: |
Rich Lane | 8841f35 | 2014-10-12 19:18:36 -0700 | [diff] [blame] | 1870 | if type_maps.class_is_inheritance_root(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1871 | pass # Check this |
| 1872 | |
| 1873 | out.write(""" |
| 1874 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1875 | * Structure for %(cls)s object. Get/set |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1876 | * accessors available in all versions unless noted otherwise |
| 1877 | * |
| 1878 | """ % dict(cls=cls)) |
| 1879 | if loxi_utils.class_is_list(cls): |
| 1880 | out.write("""\ |
| 1881 | * @param first Function of type %(cls)s_first_f. |
| 1882 | * Setup a TBD class object to the first entry in the list |
| 1883 | * @param next Function of type %(cls)s_next_f. |
| 1884 | * Advance a TBD class object to the next entry in the list |
| 1885 | * @param append_bind Function of type %(cls)s_append_bind_f |
| 1886 | * Setup a TBD class object for append to the end of the current list |
| 1887 | * @param append Function of type @ref %(cls)s_append_f. |
| 1888 | * Copy an item to the end of a list |
| 1889 | """ % dict(cls=cls)) |
| 1890 | |
| 1891 | for m_name in of_g.ordered_members[cls]: |
| 1892 | if m_name in of_g.skip_members: |
| 1893 | continue |
| 1894 | ver_type_map = field_ver_get(cls, m_name) |
| 1895 | (m_type, get_rv) = get_acc_rv(cls, m_name) |
| 1896 | if len(ver_type_map) == 3: |
| 1897 | # ver_string = "Available in all versions" |
| 1898 | ver_string = "" |
| 1899 | else: |
| 1900 | ver_string = "(" |
| 1901 | for ver in sorted(ver_type_map): |
| 1902 | ver_string += " " + of_g.short_version_names[ver] |
| 1903 | ver_string += ")." |
| 1904 | |
| 1905 | f_name = acc_name(cls, m_name) |
| 1906 | out.write("""\ |
| 1907 | * @param %(m_name)s_get/set %(ver_string)s |
| 1908 | * Accessors for %(m_name)s, a variable of type %(m_type)s. Functions |
| 1909 | * are of type %(f_name)s_get_f and _set_f. |
| 1910 | * |
| 1911 | """ % dict(f_name=f_name, m_name=m_name, ver_string=ver_string, m_type=m_type)) |
| 1912 | |
| 1913 | out.write("""\ |
| 1914 | */ |
| 1915 | typedef struct %(cls)s_s %(cls)s_t; |
| 1916 | """ % dict(cls=cls)) |
| 1917 | |
| 1918 | out.write("#endif /* _LOCI_DOC_H_ */\n") |