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 Global data structs for LOXI code generation |
| 30 | # |
| 31 | # @fixme This needs to be refactored and brought into the 21st century. |
| 32 | # |
| 33 | |
| 34 | import sys |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 35 | # @fixme Replace with argparse |
| 36 | |
| 37 | ################################################################ |
| 38 | # |
| 39 | # Configuration global parameters |
| 40 | # |
| 41 | ################################################################ |
| 42 | |
| 43 | ## |
| 44 | # The map from wire protocol to enum identifier generated from input |
| 45 | # This is built from the version-specific structs file info. |
| 46 | # @fixme This should go away when the process structs file is updated |
| 47 | wire_ver_map = {} |
| 48 | |
| 49 | ## |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 50 | # The list of wire versions which are to be supported |
| 51 | target_version_list = [] |
| 52 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 53 | ## |
| 54 | # The dictionary of config variables related to code |
| 55 | # |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 56 | # @param use_obj_id Use object IDs in struct defns CURRENTLY NOT SUPPORTED |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 57 | # |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 58 | # @param return_base_types For 'get' accessors, return values when possible. |
| 59 | # Otherwise all values are returned thru a call by variable parameter |
| 60 | # |
| 61 | # @param use_static_inlines Generate low level accessors as static inline |
| 62 | # and put them in header files rather than .c files. |
| 63 | # |
| 64 | # @param copy_semantics One of "read", "write" or "grow". This defines the |
| 65 | # way that buffer references are managed. Currently on "read" is supported. |
| 66 | # |
| 67 | # @param encode_typedefs Use object and member IDs (rather than names) |
| 68 | # when generating the names used for accessor function typedefs |
| 69 | # |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 70 | # @param get_returns One of "error", "value", or "void"; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 71 | # CURRENTLY ONLY "error" IS SUPPORTED. "error" means |
| 72 | # all get operations return an error code. "value" means return a base_type |
| 73 | # value when possible or void if not. "void" means always return void |
| 74 | # and use a call-by-variable parameter |
| 75 | # |
| 76 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 77 | ## These members do not get normal accessors |
| 78 | |
Rob Vaterlaus | b3f49d9 | 2013-10-01 17:57:31 -0700 | [diff] [blame] | 79 | skip_members = ["version", "type", "length", "err_type", "stats_type", "len", |
Rich Lane | e311367 | 2013-12-06 17:09:57 -0800 | [diff] [blame] | 80 | "type_len", "actions_len", "_command", "command"] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 81 | |
| 82 | ## Some OpenFlow string length constants |
| 83 | # |
| 84 | # These are a few length constants needed for array processing |
| 85 | ofp_constants = dict( |
| 86 | OF_MAX_TABLE_NAME_LEN = 32, |
| 87 | OF_MAX_PORT_NAME_LEN = 16, |
| 88 | OF_ETH_ALEN = 6, |
| 89 | OF_DESC_STR_LEN = 256, |
| 90 | OF_SERIAL_NUM_LEN = 32 |
| 91 | ) |
| 92 | |
| 93 | ## List of mixed data types |
| 94 | # |
| 95 | # This is a list of data types which require special treatment |
| 96 | # because the underlying datatype has changed between versions. |
| 97 | # The main example is port which went from 16 to 32 bits. We |
| 98 | # define per-version accessors for these types and those are |
| 99 | # used in place of the normal ones. |
| 100 | # |
| 101 | # The wire protocol number is used to identify versions. For now, |
| 102 | # the value is the name of the type to use for that version |
| 103 | # |
| 104 | # This is the map between the external type (like of_port_no_t) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 105 | # which is used by customers of this code and the internal |
| 106 | # datatypes (like uint16_t) that appear on the wire for a |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 107 | # particular version. |
| 108 | # |
| 109 | of_mixed_types = dict( |
| 110 | of_port_no_t = { |
| 111 | 1: "uint16_t", |
| 112 | 2: "uint32_t", |
| 113 | 3: "uint32_t", |
| 114 | 4: "uint32_t", |
| 115 | "short_name":"port_no" |
| 116 | }, |
| 117 | of_port_desc_t = { |
| 118 | 1: "of_port_desc_t", |
| 119 | 2: "of_port_desc_t", |
| 120 | 3: "of_port_desc_t", |
| 121 | 4: "of_port_desc_t", |
| 122 | "short_name":"port_desc" |
| 123 | }, |
Dan Talayco | c0e802e | 2013-05-18 23:52:39 -0700 | [diff] [blame] | 124 | of_bsn_vport_t = { |
| 125 | 1: "of_bsn_vport_t", |
| 126 | 2: "of_bsn_vport_t", |
| 127 | 3: "of_bsn_vport_t", |
| 128 | 4: "of_bsn_vport_t", |
| 129 | "short_name":"bsn_vport" |
| 130 | }, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 131 | of_fm_cmd_t = { # Flow mod command went from u16 to u8 |
| 132 | 1: "uint16_t", |
| 133 | 2: "uint8_t", |
| 134 | 3: "uint8_t", |
| 135 | 4: "uint8_t", |
| 136 | "short_name":"fm_cmd" |
| 137 | }, |
| 138 | of_wc_bmap_t = { # Wildcard bitmap |
| 139 | 1: "uint32_t", |
| 140 | 2: "uint32_t", |
| 141 | 3: "uint64_t", |
| 142 | 4: "uint64_t", |
| 143 | "short_name":"wc_bmap" |
| 144 | }, |
| 145 | of_match_bmap_t = { # Match bitmap |
| 146 | 1: "uint32_t", |
| 147 | 2: "uint32_t", |
| 148 | 3: "uint64_t", |
| 149 | 4: "uint64_t", |
| 150 | "short_name":"match_bmap" |
| 151 | }, |
| 152 | of_match_t = { # Match object |
| 153 | 1: "of_match_v1_t", |
| 154 | 2: "of_match_v2_t", |
| 155 | 3: "of_match_v3_t", |
| 156 | 4: "of_match_v3_t", # Currently uses same match as 1.2 (v3). |
| 157 | "short_name":"match" |
| 158 | }, |
| 159 | ) |
| 160 | |
| 161 | ## Base data types |
| 162 | # |
| 163 | # The basic types; Value is a list: bytes, to_wire, from_wire |
| 164 | # The accessors deal with endian, alignment and any other host/network |
| 165 | # considerations. These are common across all versions |
| 166 | # |
| 167 | # For get accessors, assume we memcpy from wire buf and then apply ntoh |
| 168 | # For set accessors, assume we apply hton and then memcpy to wire buf |
| 169 | # |
| 170 | # to/from wire functions take a pointer to class and change in place |
| 171 | of_base_types = dict( |
| 172 | char = dict(bytes=1, use_as_rv=1, short_name="char"), |
| 173 | uint8_t = dict(bytes=1, use_as_rv=1, short_name="u8"), |
| 174 | uint16_t = dict(bytes=2, to_w="u16_hton", from_w="u16_ntoh", use_as_rv=1, |
| 175 | short_name="u16"), |
| 176 | uint32_t = dict(bytes=4, to_w="u32_hton", from_w="u32_ntoh", use_as_rv=1, |
| 177 | short_name="u32"), |
| 178 | uint64_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1, |
| 179 | short_name="u64"), |
| 180 | # of_cookie_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1#, |
| 181 | # short_name="cookie"), |
| 182 | # of_counter_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1, |
| 183 | # short_name="counter"), |
| 184 | of_mac_addr_t = dict(bytes=6, short_name="mac"), |
Andreas Wundsam | b566a16 | 2013-07-18 19:30:23 -0700 | [diff] [blame] | 185 | of_ipv4_t = dict(bytes=4, short_name="ipv4"), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 186 | of_ipv6_t = dict(bytes=16, short_name="ipv6"), |
| 187 | of_port_name_t = dict(bytes=ofp_constants["OF_MAX_PORT_NAME_LEN"], |
| 188 | short_name="port_name"), |
| 189 | of_table_name_t = dict(bytes=ofp_constants["OF_MAX_TABLE_NAME_LEN"], |
| 190 | short_name="tab_name"), |
| 191 | of_desc_str_t = dict(bytes=ofp_constants["OF_DESC_STR_LEN"], |
| 192 | short_name="desc_str"), |
| 193 | of_serial_num_t = dict(bytes=ofp_constants["OF_SERIAL_NUM_LEN"], |
| 194 | short_name="ser_num"), |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 195 | of_match_v1_t = dict(bytes=40, to_w="match_v1_hton", |
| 196 | from_w="match_v1_ntoh", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 197 | short_name="match_v1"), |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 198 | of_match_v2_t = dict(bytes=88, to_w="match_v2_hton", |
| 199 | from_w="match_v2_ntoh", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 200 | short_name="match_v2"), |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 201 | of_match_v3_t = dict(bytes=-1, to_w="match_v3_hton", |
| 202 | from_w="match_v3_ntoh", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 203 | short_name="match_v3"), |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 204 | # of_match_v4_t = dict(bytes=-1, to_w="match_v4_hton", |
| 205 | # from_w="match_v4_ntoh", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 206 | # short_name="match_v4"), |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 207 | of_octets_t = dict(bytes=-1, short_name="octets"), |
| 208 | of_bitmap_128_t = dict(bytes=16, short_name="bitmap_128"), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 209 | ) |
| 210 | |
| 211 | of_scalar_types = ["char", "uint8_t", "uint16_t", "uint32_t", "uint64_t", |
| 212 | "of_port_no_t", "of_fm_cmd_t", "of_wc_bmap_t", |
| 213 | "of_match_bmap_t", "of_port_name_t", "of_table_name_t", |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 214 | "of_desc_str_t", "of_serial_num_t", "of_mac_addr_t", |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 215 | "of_ipv6_t", "of_ipv4_t", "of_bitmap_128_t"] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 216 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 217 | ## |
| 218 | # LOXI identifiers |
| 219 | # |
| 220 | # Dict indexed by identifier name. Each entry contains the information |
| 221 | # as a DotDict with the following keys: |
| 222 | # values: A dict indexed by wire version giving each verion's value or None |
| 223 | # common: The common value to use for this identifier at the LOXI top level (TBD) |
| 224 | # all_same: If True, all the values across all versions are the same |
| 225 | # ofp_name: The original name for the identifier |
| 226 | # ofp_group: The ofp enumerated type if defined |
| 227 | |
| 228 | identifiers = {} |
| 229 | |
| 230 | ## |
| 231 | # Identifiers by original group |
| 232 | # Keys are the original group names. Value is a list of LOXI identifiers |
| 233 | |
| 234 | identifiers_by_group = {} |
| 235 | |
| 236 | ## Ordered list of class names |
| 237 | # This is per-wire-version and is a list of the classes in the order |
| 238 | # they appear in the file. That is important because of the assumption |
| 239 | # that data members are defined before they are included in a superclass. |
| 240 | ordered_classes = {} # Indexed by wire version |
| 241 | |
| 242 | ## Per class ordered list of member names |
| 243 | ordered_members = {} |
| 244 | |
| 245 | ## Ordered list of message classes |
| 246 | ordered_messages = [] |
| 247 | |
| 248 | ## Ordered list of non message classes |
| 249 | ordered_non_messages = [] |
| 250 | |
| 251 | ## The objects that need list support |
| 252 | ordered_list_objects = [] |
| 253 | |
| 254 | ## Stats request/reply are pseudo objects |
| 255 | ordered_pseudo_objects = [] |
| 256 | |
| 257 | ## Standard order is normally messages followed by non-messages |
| 258 | standard_class_order = [] |
| 259 | |
| 260 | ## All classes in order, including psuedo classes for which most code |
| 261 | # is not generated. |
| 262 | all_class_order = [] |
| 263 | |
| 264 | ## Map from class, wire_version to size of fixed part of class |
| 265 | base_length = {} |
| 266 | |
Rich Lane | f70be94 | 2013-07-18 13:33:14 -0700 | [diff] [blame] | 267 | ## Map from class, wire_version to size of variable-offset, fixed length part of class |
| 268 | extra_length = { |
| 269 | ("of_packet_in", 3): 2, |
| 270 | ("of_packet_in", 4): 2, |
| 271 | } |
| 272 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 273 | ## Boolean indication of variable length, per class, wire_version, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 274 | is_fixed_length = set() |
| 275 | |
| 276 | ## The global object ID counter |
| 277 | object_id = 1 # Reserve 0 for root object |
| 278 | |
| 279 | ## The unified view of all classes. See internal readme. |
| 280 | unified = {} |
| 281 | |
| 282 | ## Indicates data members with non-fixed start offsets |
| 283 | # Indexed by (cls, version, member-name) and value is prev-member-name |
| 284 | special_offsets = {} |
| 285 | |
| 286 | ## Define Python variables with integer wire version values |
| 287 | VERSION_1_0 = 1 |
| 288 | VERSION_1_1 = 2 |
| 289 | VERSION_1_2 = 3 |
| 290 | VERSION_1_3 = 4 |
| 291 | |
| 292 | # Ignore version for some functions |
| 293 | VERSION_ANY = -1 |
| 294 | |
| 295 | ## @var supported_wire_protos |
| 296 | # The wire protocols this version of LoxiGen supports |
| 297 | supported_wire_protos = set([1, 2, 3, 4]) |
| 298 | version_names = {1:"VERSION_1_0", 2:"VERSION_1_1", 3:"VERSION_1_2", |
| 299 | 4:"VERSION_1_3"} |
| 300 | short_version_names = {1:"OF_1_0", 2:"OF_1_1", 3:"OF_1_2", 4:"OF_1_3"} |
| 301 | param_version_names = {1:"1.0", 2:"1.1", 3:"1.2", 4:"1.3"} |
| 302 | |
| 303 | ## |
| 304 | # Maps and ranges related to versioning |
| 305 | |
| 306 | # For parameter version indications |
| 307 | of_param_version_map = { |
| 308 | "1.0":VERSION_1_0, |
| 309 | "1.1":VERSION_1_1, |
| 310 | "1.2":VERSION_1_2, |
| 311 | "1.3":VERSION_1_3 |
| 312 | } |
| 313 | |
| 314 | # For parameter version indications |
| 315 | of_version_map = { |
| 316 | "1.0":VERSION_1_0, |
| 317 | "1.1":VERSION_1_1, |
| 318 | "1.2":VERSION_1_2, |
| 319 | "1.3":VERSION_1_3 |
| 320 | } |
| 321 | |
| 322 | # The iteration object that gives the wire versions supported |
| 323 | of_version_range = [VERSION_1_0, VERSION_1_1, VERSION_1_2, VERSION_1_3] |
| 324 | of_version_max = VERSION_1_3 |
| 325 | |
| 326 | |
| 327 | of_version_name2wire = dict( |
| 328 | OF_VERSION_1_0=VERSION_1_0, |
| 329 | OF_VERSION_1_1=VERSION_1_1, |
| 330 | OF_VERSION_1_2=VERSION_1_2, |
| 331 | OF_VERSION_1_3=VERSION_1_3 |
| 332 | ) |
| 333 | |
| 334 | of_version_wire2name = { |
| 335 | VERSION_1_0:"OF_VERSION_1_0", |
| 336 | VERSION_1_1:"OF_VERSION_1_1", |
| 337 | VERSION_1_2:"OF_VERSION_1_2", |
| 338 | VERSION_1_3:"OF_VERSION_1_3" |
| 339 | } |
| 340 | |
| 341 | |
| 342 | ################################################################ |
| 343 | # |
| 344 | # Experimenters, vendors, extensions |
| 345 | # |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 346 | # Although the term "experimenter" is used for identifying |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 347 | # external extension definitions, we generally use the term |
| 348 | # extension when refering to the messages or objects themselves. |
| 349 | # |
| 350 | # Conventions: |
| 351 | # |
| 352 | # Extension messages should start with of_<experimenter>_ |
| 353 | # Extension actions should start with of_<experimenter>_action_ |
| 354 | # Extension instructions should start with of_<experimenter>_instructions_ |
| 355 | # |
| 356 | # Currently, the above conventions are not enforced; the mapping |
| 357 | # is done brute force in type_maps.py |
| 358 | # |
| 359 | ################################################################ |
| 360 | |
| 361 | # The map of known experimenters to their experimenter IDs |
| 362 | experimenter_name_to_id = dict( |
| 363 | bsn = 0x005c16c7, |
| 364 | nicira = 0x00002320, |
| 365 | openflow = 0x000026e1 |
| 366 | ) |
| 367 | |
| 368 | def experimenter_name_lookup(experimenter_id): |
| 369 | """ |
| 370 | Map an experimenter ID to its LOXI recognized name string |
| 371 | """ |
| 372 | for name, id in of_g.experimenter_name_to_id.items(): |
| 373 | if id == experimenter_id: |
| 374 | return name |
| 375 | return None |
| 376 | |
| 377 | ################################################################ |
| 378 | # |
| 379 | # Debug |
| 380 | # |
| 381 | ################################################################ |
| 382 | |
| 383 | loxigen_dbg_file = sys.stdout |
| 384 | loxigen_log_file = sys.stdout |