Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [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 Utilities involving LOXI naming conventions |
| 30 | |
| 31 | Utility functions for OpenFlow class generation |
| 32 | |
| 33 | These may need to be sorted out into language specific functions |
| 34 | """ |
| 35 | |
| 36 | import sys |
| 37 | import c_gen.of_g_legacy as of_g |
| 38 | import tenjin |
| 39 | from generic_utils import find, memoize |
Rich Lane | 90409cc | 2014-10-13 11:18:02 -0700 | [diff] [blame] | 40 | import loxi_globals |
| 41 | from loxi_ir import ir |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 42 | |
| 43 | def class_signature(members): |
| 44 | """ |
| 45 | Generate a signature string for a class in canonical form |
| 46 | |
| 47 | @param cls The class whose signature is to be generated |
| 48 | """ |
| 49 | return ";".join([",".join([x["m_type"], x["name"], str(x["offset"])]) |
| 50 | for x in members]) |
| 51 | |
| 52 | def type_dec_to_count_base(m_type): |
| 53 | """ |
| 54 | Resolve a type declaration like uint8_t[4] to a count (4) and base_type |
| 55 | (uint8_t) |
| 56 | |
| 57 | @param m_type The string type declaration to process |
| 58 | """ |
| 59 | count = 1 |
| 60 | chk_ar = m_type.split('[') |
| 61 | if len(chk_ar) > 1: |
| 62 | count_str = chk_ar[1].split(']')[0] |
| 63 | if count_str in of_g.ofp_constants: |
| 64 | count = of_g.ofp_constants[count_str] |
| 65 | else: |
| 66 | count = int(count_str) |
| 67 | base_type = chk_ar[0] |
| 68 | else: |
| 69 | base_type = m_type |
| 70 | return count, base_type |
| 71 | |
| 72 | ## |
| 73 | # Class types: |
| 74 | # |
| 75 | # Virtual |
| 76 | # A virtual class is one which does not have an explicit wire |
| 77 | # representation. For example, an inheritance super class |
| 78 | # or a list type. |
| 79 | # |
| 80 | # List |
| 81 | # A list of objects of some other type |
| 82 | # |
| 83 | # TLV16 |
| 84 | # The wire represenation starts with 16-bit type and length fields |
| 85 | # |
| 86 | # OXM |
| 87 | # An extensible match object |
| 88 | # |
| 89 | # Message |
| 90 | # A top level OpenFlow message |
| 91 | # |
| 92 | # |
| 93 | |
| 94 | def class_is_message(cls): |
| 95 | """ |
| 96 | Return True if cls is a message object based on info in unified |
| 97 | """ |
Rich Lane | c9c54fe | 2014-04-23 15:15:09 -0700 | [diff] [blame] | 98 | return "xid" in of_g.unified[cls]["union"] |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 99 | |
| 100 | def class_is_tlv16(cls): |
| 101 | """ |
| 102 | Return True if cls_name is an object which uses uint16 for type and length |
| 103 | """ |
Rich Lane | 90409cc | 2014-10-13 11:18:02 -0700 | [diff] [blame] | 104 | |
| 105 | ofclass = loxi_globals.unified.class_by_name(cls) |
| 106 | if not ofclass: |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 107 | return False |
Rich Lane | 90409cc | 2014-10-13 11:18:02 -0700 | [diff] [blame] | 108 | |
| 109 | if len(ofclass.members) < 2: |
| 110 | return False |
| 111 | |
| 112 | m1 = ofclass.members[0] |
| 113 | m2 = ofclass.members[1] |
| 114 | |
| 115 | if not (isinstance(m1, ir.OFTypeMember) or isinstance(m1, ir.OFDiscriminatorMember)): |
| 116 | return False |
| 117 | |
| 118 | if not isinstance(m2, ir.OFLengthMember): |
| 119 | return False |
| 120 | |
| 121 | if m1.oftype != "uint16_t" or m2.oftype != "uint16_t": |
| 122 | return False |
| 123 | |
| 124 | return True |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 125 | |
| 126 | def class_is_u16_len(cls): |
| 127 | """ |
| 128 | Return True if cls_name is an object which uses initial uint16 length |
| 129 | """ |
Rich Lane | 06d3dc2 | 2014-10-13 11:21:53 -0700 | [diff] [blame] | 130 | |
| 131 | ofclass = loxi_globals.unified.class_by_name(cls) |
| 132 | if not ofclass: |
| 133 | return False |
| 134 | |
| 135 | if len(ofclass.members) < 1: |
| 136 | return False |
| 137 | |
| 138 | m = ofclass.members[0] |
| 139 | |
| 140 | if not isinstance(m, ir.OFLengthMember): |
| 141 | return False |
| 142 | |
| 143 | if m.oftype != "uint16_t": |
| 144 | return False |
| 145 | |
| 146 | return True |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 147 | |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 148 | def class_is_list(cls): |
| 149 | """ |
| 150 | Return True if cls_name is a list object |
| 151 | """ |
| 152 | return (cls.find("of_list_") == 0) |
| 153 | |
| 154 | def type_is_of_object(m_type): |
| 155 | """ |
| 156 | Return True if m_type is an OF object type |
| 157 | """ |
| 158 | # Remove _t from the type id and see if key for unified class |
| 159 | if m_type[-2:] == "_t": |
| 160 | m_type = m_type[:-2] |
| 161 | return m_type in of_g.unified |
| 162 | |
| 163 | def list_to_entry_type(cls): |
| 164 | """ |
| 165 | Return the entry type for a list |
| 166 | """ |
| 167 | slen = len("of_list_") |
| 168 | return "of_" + cls[slen:] |
| 169 | |
| 170 | def type_to_short_name(m_type): |
| 171 | if m_type in of_g.of_base_types: |
| 172 | tname = of_g.of_base_types[m_type]["short_name"] |
| 173 | elif m_type in of_g.of_mixed_types: |
| 174 | tname = of_g.of_mixed_types[m_type]["short_name"] |
| 175 | else: |
| 176 | tname = "unknown" |
| 177 | return tname |
| 178 | |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 179 | def member_to_index(m_name, members): |
| 180 | """ |
| 181 | Given a member name, return the index in the members dict |
| 182 | @param m_name The name of the data member to search for |
| 183 | @param members The dict of members |
| 184 | @return Index if found, -1 not found |
| 185 | |
| 186 | Note we could generate an index when processing the original input |
| 187 | """ |
| 188 | count = 0 |
| 189 | for d in members: |
| 190 | if d["name"] == m_name: |
| 191 | return count |
| 192 | count += 1 |
| 193 | return -1 |
| 194 | |
| 195 | def member_base_type(cls, m_name): |
| 196 | """ |
| 197 | Map a member to its of_ type |
| 198 | @param cls The class name |
| 199 | @param m_name The name of the member being gotten |
| 200 | @return The of_ type of the member |
| 201 | """ |
| 202 | rv = of_g.unified[cls]["union"][m_name]["m_type"] |
| 203 | if rv[-2:] == "_t": |
| 204 | return rv |
| 205 | return rv + "_t" |
| 206 | |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 207 | def type_is_scalar(m_type): |
| 208 | return m_type in of_g.of_scalar_types |
| 209 | |
| 210 | def skip_member_name(name): |
| 211 | return name.find("pad") == 0 or name in of_g.skip_members |
| 212 | |
| 213 | def enum_name(cls): |
| 214 | """ |
| 215 | Return the name used for an enum identifier for the given class |
| 216 | @param cls The class name |
| 217 | """ |
| 218 | return cls.upper() |
| 219 | |
| 220 | def class_in_version(cls, ver): |
| 221 | """ |
| 222 | Return boolean indicating if cls is defined for wire version ver |
| 223 | """ |
| 224 | |
| 225 | return (cls, ver) in of_g.base_length |
| 226 | |
| 227 | def instance_to_class(instance, parent): |
| 228 | """ |
| 229 | Return the name of the class for an instance of inheritance type parent |
| 230 | """ |
| 231 | return parent + "_" + instance |
| 232 | |
Rich Lane | ee60d4e | 2014-10-12 20:08:24 -0700 | [diff] [blame] | 233 | def class_to_instance(cls, base_cls): |
| 234 | assert cls.startswith(base_cls + '_') |
| 235 | return cls[len(base_cls)+1:] |
| 236 | |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 237 | def class_is_var_len(cls, version): |
| 238 | # Match is special case. Only version 1.2 (wire version 3) is var |
| 239 | if cls == "of_match": |
| 240 | return version == 3 |
| 241 | |
| 242 | return not (cls, version) in of_g.is_fixed_length |
| 243 | |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 244 | ## |
| 245 | # Is class a flow modify of some sort? |
| 246 | |
| 247 | def cls_is_flow_mod(cls): |
| 248 | return cls in ["of_flow_mod", "of_flow_modify", "of_flow_add", "of_flow_delete", |
| 249 | "of_flow_modify_strict", "of_flow_delete_strict"] |
| 250 | |
| 251 | |
| 252 | def all_member_types_get(cls, version): |
| 253 | """ |
| 254 | Get the members and list of types for members of a given class |
| 255 | @param cls The class name to process |
| 256 | @param version The version for the class |
| 257 | """ |
| 258 | member_types = [] |
| 259 | |
| 260 | if not version in of_g.unified[cls]: |
| 261 | return ([], []) |
| 262 | |
| 263 | if "use_version" in of_g.unified[cls][version]: |
| 264 | v = of_g.unified[cls][version]["use_version"] |
| 265 | members = of_g.unified[cls][v]["members"] |
| 266 | else: |
| 267 | members = of_g.unified[cls][version]["members"] |
| 268 | # Accumulate variables that are supported |
| 269 | for member in members: |
| 270 | m_type = member["m_type"] |
| 271 | m_name = member["name"] |
| 272 | if skip_member_name(m_name): |
| 273 | continue |
| 274 | if not m_type in member_types: |
| 275 | member_types.append(m_type) |
| 276 | |
| 277 | return (members, member_types) |
| 278 | |
| 279 | def list_name_extract(list_type): |
| 280 | """ |
| 281 | Return the base name for a list object of the given type |
| 282 | @param list_type The type of the list as appears in the input, |
| 283 | for example list(of_port_desc_t). |
| 284 | @return A pair, (list-name, base-type) where list-name is the |
| 285 | base name for the list, for example of_list_port_desc, and base-type |
| 286 | is the type of list elements like of_port_desc_t |
| 287 | """ |
| 288 | base_type = list_type[5:-1] |
| 289 | list_name = base_type |
| 290 | if list_name.find("of_") == 0: |
| 291 | list_name = list_name[3:] |
| 292 | if list_name[-2:] == "_t": |
| 293 | list_name = list_name[:-2] |
| 294 | list_name = "of_list_" + list_name |
| 295 | return (list_name, base_type) |
| 296 | |
| 297 | def version_to_name(version): |
| 298 | """ |
| 299 | Convert an integer version to the C macro name |
| 300 | """ |
Rich Lane | f1a8984 | 2014-10-12 10:04:13 -0700 | [diff] [blame] | 301 | return of_g.of_version_wire2name[version] |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 302 | |
| 303 | def gen_c_copy_license(out): |
| 304 | """ |
| 305 | Generate the top comments for copyright and license |
| 306 | """ |
| 307 | import c_gen.util |
| 308 | c_gen.util.render_template(out, '_copyright.c') |
| 309 | |
| 310 | def accessor_returns_error(a_type, m_type): |
| 311 | is_var_len = (not type_is_scalar(m_type)) and \ |
| 312 | [x for x in of_g.of_version_range if class_is_var_len(m_type[:-2], x)] != [] |
| 313 | if a_type == "set" and is_var_len: |
| 314 | return True |
| 315 | elif m_type == "of_match_t": |
| 316 | return True |
| 317 | else: |
| 318 | return False |