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 | # @brief Generate wire to generic match conversion functions |
| 29 | # |
| 30 | # @fixme This has lots of C specific code that should be moved into c_gen |
| 31 | |
| 32 | # of_match_to_wire_match(match, wire_match) |
| 33 | # of_wire_match_to_match(wire_match, match) |
| 34 | # Version is taken from the source in each case |
| 35 | # |
| 36 | # name |
| 37 | # type |
| 38 | # conditions |
| 39 | # v3 ident |
| 40 | # takes mask |
| 41 | |
| 42 | import sys |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 43 | import c_gen.of_g_legacy as of_g |
| 44 | import c_gen.match as match |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 45 | import c_code_gen |
| 46 | |
| 47 | def match_c_top_matter(out, name): |
| 48 | """ |
| 49 | Generate top matter for match C file |
| 50 | |
| 51 | @param name The name of the output file |
| 52 | @param out The output file object |
| 53 | """ |
| 54 | c_code_gen.common_top_matter(out, name) |
| 55 | out.write("#include \"loci_log.h\"\n") |
| 56 | out.write("#include <loci/loci.h>\n") |
| 57 | |
| 58 | def match_h_top_matter(out, name): |
| 59 | """ |
| 60 | Generate top matter for the C file |
| 61 | |
| 62 | @param name The name of the output file |
| 63 | @param ih_name The name of the internal header file |
| 64 | @param out The output file object |
| 65 | """ |
| 66 | c_code_gen.common_top_matter(out, name) |
| 67 | out.write(""" |
| 68 | #include <loci/loci_base.h> |
| 69 | """) |
| 70 | |
| 71 | def gen_declarations(out): |
| 72 | out.write(""" |
| 73 | /* |
| 74 | * Match serialize/deserialize declarations |
| 75 | * Wire match conversion function declarations |
| 76 | */ |
| 77 | extern int of_match_serialize(of_version_t version, of_match_t *match, |
| 78 | of_octets_t *octets); |
| 79 | extern int of_match_deserialize(of_version_t version, of_match_t *match, |
| 80 | of_octets_t *octets); |
| 81 | extern int of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst); |
| 82 | extern int of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst); |
| 83 | extern int of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst); |
| 84 | extern int of_match_to_wire_match_v1(of_match_t *src, of_match_v1_t *dst); |
| 85 | extern int of_match_to_wire_match_v2(of_match_t *src, of_match_v2_t *dst); |
| 86 | extern int of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst); |
| 87 | """) |
| 88 | |
| 89 | def gen_v4_match_compat(out): |
| 90 | """ |
| 91 | Code for coercing version 1.3 matches to 1.2 matches |
| 92 | |
| 93 | @FIXME This is a stopgap and needs to get cleaned up. |
| 94 | """ |
| 95 | out.write(""" |
| 96 | /** |
| 97 | * Definitions to coerce v4 match (version 1.3) to v3 matches |
| 98 | * (version 1.2). |
| 99 | * @FIXME This is a stopgap and needs to get cleaned up. |
| 100 | */ |
| 101 | #define of_match_v4_t of_match_v3_t |
| 102 | #define of_match_v4_init of_match_v3_init |
| 103 | #define of_match_v4_new of_match_v3_new |
| 104 | #define of_match_v4_to_match of_match_v3_to_match |
| 105 | #define of_match_to_wire_match_v4 of_match_to_wire_match_v3 |
| 106 | #define of_match_v4_delete of_match_v3_delete |
| 107 | """) |
| 108 | |
| 109 | def gen_match_macros(out): |
| 110 | out.write(""" |
| 111 | |
| 112 | /** |
| 113 | * Definitions for wildcard macros for OF_VERSION_1_0 |
| 114 | */ |
| 115 | |
| 116 | """) |
| 117 | for key in match.of_v1_keys: |
| 118 | entry = match.of_match_members[key] |
| 119 | if "v1_wc_shift" in entry: |
| 120 | if key in ["ipv4_src", "ipv4_dst"]: |
| 121 | out.write(""" |
| 122 | #define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d |
| 123 | #define OF_MATCH_V1_WC_%(ku)s_MASK (0x3f << %(val)d) |
| 124 | #define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(0x3f << %(val)d)) |
| 125 | #define OF_MATCH_V1_WC_%(ku)s_SET(wc, value) do { \\ |
| 126 | OF_MATCH_V1_WC_%(ku)s_CLEAR(wc); \\ |
| 127 | ((wc) |= (((value) & 0x3f) << %(val)d)); \\ |
| 128 | } while (0) |
| 129 | #define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (0x3f << %(val)d)) |
| 130 | #define OF_MATCH_V1_WC_%(ku)s_GET(wc) (((wc) >> %(val)d) & 0x3f) |
| 131 | """ % dict(ku=key.upper(), val=entry["v1_wc_shift"])) |
| 132 | else: |
| 133 | out.write(""" |
| 134 | #define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d |
| 135 | #define OF_MATCH_V1_WC_%(ku)s_MASK (1 << %(val)d) |
| 136 | #define OF_MATCH_V1_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d)) |
| 137 | #define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d)) |
| 138 | #define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d)) |
| 139 | """ % dict(ku=key.upper(), val=entry["v1_wc_shift"])) |
| 140 | |
| 141 | out.write(""" |
| 142 | |
| 143 | /** |
| 144 | * Definitions for wildcard macros for OF_VERSION_1_1 |
| 145 | */ |
| 146 | """) |
| 147 | |
| 148 | for key in sorted(match.of_v2_keys): |
| 149 | entry = match.of_match_members[key] |
| 150 | if "v2_wc_shift" in entry: |
| 151 | out.write(""" |
| 152 | #define OF_MATCH_V2_WC_%(ku)s_SHIFT %(val)d |
| 153 | #define OF_MATCH_V2_WC_%(ku)s_MASK (1 << %(val)d) |
| 154 | #define OF_MATCH_V2_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d)) |
| 155 | #define OF_MATCH_V2_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d)) |
| 156 | #define OF_MATCH_V2_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d)) |
| 157 | """ % dict(ku=key.upper(), val=entry["v2_wc_shift"])) |
| 158 | |
| 159 | |
| 160 | def gen_match_struct(out=sys.stdout): |
| 161 | out.write("/* Unified, flat OpenFlow match structure based on OF 1.2 */\n") |
| 162 | out.write("typedef struct of_match_fields_s {\n") |
| 163 | out.write(" /* Version 1.2 is used for field names */\n") |
| 164 | for name in match.match_keys_sorted: |
| 165 | entry = match.of_match_members[name] |
| 166 | out.write(" %-20s %s;\n" % (entry["m_type"], entry["name"])) |
| 167 | out.write(""" |
| 168 | } of_match_fields_t; |
| 169 | |
| 170 | /** |
| 171 | * @brief The LOCI match structure. |
| 172 | */ |
| 173 | |
| 174 | typedef struct of_match_s { |
| 175 | of_version_t version; |
| 176 | of_match_fields_t fields; |
| 177 | of_match_fields_t masks; |
| 178 | } of_match_t; |
| 179 | |
| 180 | /** |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 181 | * Mask the values in the match structure according to its fields |
| 182 | */ |
| 183 | static inline void of_match_values_mask(of_match_t *match) |
| 184 | { |
| 185 | int idx; |
| 186 | |
| 187 | for (idx = 0; idx < sizeof(of_match_fields_t); idx++) { |
| 188 | ((uint8_t *)&match->fields)[idx] &= ((uint8_t *)&match->masks)[idx]; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 193 | * IP Mask map. IP maks wildcards from OF 1.0 are interpretted as |
| 194 | * indices into the map below. |
| 195 | * |
| 196 | * of_ip_mask_map: Array mapping index to mask |
| 197 | * of_ip_mask_use_map: Boolean indication set when map is initialized |
| 198 | * of_ip_mask_map_init: Initialize to default values; set "use map". |
| 199 | */ |
| 200 | #define OF_IP_MASK_MAP_COUNT 64 |
| 201 | extern uint32_t of_ip_mask_map[OF_IP_MASK_MAP_COUNT]; |
| 202 | extern int of_ip_mask_map_init_done; |
| 203 | |
| 204 | #define OF_IP_MASK_INIT_CHECK \ |
| 205 | if (!of_ip_mask_map_init_done) of_ip_mask_map_init() |
| 206 | |
| 207 | /** |
| 208 | * Initialize map |
| 209 | */ |
| 210 | extern void of_ip_mask_map_init(void); |
| 211 | |
| 212 | extern int of_ip_mask_map_set(int index, uint32_t mask); |
| 213 | extern int of_ip_mask_map_get(int index, uint32_t *mask); |
| 214 | |
| 215 | /** |
| 216 | * @brief Map from mask to index |
| 217 | */ |
| 218 | |
| 219 | extern int of_ip_mask_to_index(uint32_t mask); |
| 220 | |
| 221 | /** |
| 222 | * @brief Map from index to mask |
| 223 | */ |
| 224 | |
| 225 | extern uint32_t of_ip_index_to_mask(int index); |
| 226 | |
| 227 | /** |
| 228 | * The signalling of an untagged packet varies by OF version. |
| 229 | * Use this macro to set the field value. |
| 230 | */ |
| 231 | #define OF_MATCH_UNTAGGED_VLAN_ID(version) \\ |
| 232 | ((version) == OF_VERSION_1_0 ? 0xffff : \\ |
| 233 | ((version) == OF_VERSION_1_1 ? 0xffff : 0)) |
| 234 | |
| 235 | /** |
| 236 | * Version 1.1 had the notion of "any" vlan but must be set |
| 237 | */ |
| 238 | #define OF_MATCH_VLAN_TAG_PRESENT_ANY_ID(version) \\ |
| 239 | ((version) == OF_VERSION_1_0 ? 0 /* @fixme */ : \\ |
| 240 | ((version) == OF_VERSION_1_1 ? 0xfffe : 0x1000)) |
| 241 | """) |
| 242 | |
| 243 | def gen_oxm_defines(out): |
| 244 | """ |
| 245 | Generate verbatim definitions for OXM |
| 246 | """ |
| 247 | out.write(""" |
| 248 | |
| 249 | /* These are from the OpenFlow 1.2 header file */ |
| 250 | |
| 251 | /* OXM index values for bitmaps and parsing */ |
| 252 | enum of_oxm_index_e { |
| 253 | OF_OXM_INDEX_IN_PORT = 0, /* Switch input port. */ |
| 254 | OF_OXM_INDEX_IN_PHY_PORT = 1, /* Switch physical input port. */ |
| 255 | OF_OXM_INDEX_METADATA = 2, /* Metadata passed between tables. */ |
| 256 | OF_OXM_INDEX_ETH_DST = 3, /* Ethernet destination address. */ |
| 257 | OF_OXM_INDEX_ETH_SRC = 4, /* Ethernet source address. */ |
| 258 | OF_OXM_INDEX_ETH_TYPE = 5, /* Ethernet frame type. */ |
| 259 | OF_OXM_INDEX_VLAN_VID = 6, /* VLAN id. */ |
| 260 | OF_OXM_INDEX_VLAN_PCP = 7, /* VLAN priority. */ |
| 261 | OF_OXM_INDEX_IP_DSCP = 8, /* IP DSCP (6 bits in ToS field). */ |
| 262 | OF_OXM_INDEX_IP_ECN = 9, /* IP ECN (2 bits in ToS field). */ |
| 263 | OF_OXM_INDEX_IP_PROTO = 10, /* IP protocol. */ |
| 264 | OF_OXM_INDEX_IPV4_SRC = 11, /* IPv4 source address. */ |
| 265 | OF_OXM_INDEX_IPV4_DST = 12, /* IPv4 destination address. */ |
| 266 | OF_OXM_INDEX_TCP_SRC = 13, /* TCP source port. */ |
| 267 | OF_OXM_INDEX_TCP_DST = 14, /* TCP destination port. */ |
| 268 | OF_OXM_INDEX_UDP_SRC = 15, /* UDP source port. */ |
| 269 | OF_OXM_INDEX_UDP_DST = 16, /* UDP destination port. */ |
| 270 | OF_OXM_INDEX_SCTP_SRC = 17, /* SCTP source port. */ |
| 271 | OF_OXM_INDEX_SCTP_DST = 18, /* SCTP destination port. */ |
| 272 | OF_OXM_INDEX_ICMPV4_TYPE = 19, /* ICMP type. */ |
| 273 | OF_OXM_INDEX_ICMPV4_CODE = 20, /* ICMP code. */ |
| 274 | OF_OXM_INDEX_ARP_OP = 21, /* ARP opcode. */ |
| 275 | OF_OXM_INDEX_ARP_SPA = 22, /* ARP source IPv4 address. */ |
| 276 | OF_OXM_INDEX_ARP_TPA = 23, /* ARP target IPv4 address. */ |
| 277 | OF_OXM_INDEX_ARP_SHA = 24, /* ARP source hardware address. */ |
| 278 | OF_OXM_INDEX_ARP_THA = 25, /* ARP target hardware address. */ |
| 279 | OF_OXM_INDEX_IPV6_SRC = 26, /* IPv6 source address. */ |
| 280 | OF_OXM_INDEX_IPV6_DST = 27, /* IPv6 destination address. */ |
| 281 | OF_OXM_INDEX_IPV6_FLABEL = 28, /* IPv6 Flow Label */ |
| 282 | OF_OXM_INDEX_ICMPV6_TYPE = 29, /* ICMPv6 type. */ |
| 283 | OF_OXM_INDEX_ICMPV6_CODE = 30, /* ICMPv6 code. */ |
| 284 | OF_OXM_INDEX_IPV6_ND_TARGET = 31, /* Target address for ND. */ |
| 285 | OF_OXM_INDEX_IPV6_ND_SLL = 32, /* Source link-layer for ND. */ |
| 286 | OF_OXM_INDEX_IPV6_ND_TLL = 33, /* Target link-layer for ND. */ |
| 287 | OF_OXM_INDEX_MPLS_LABEL = 34, /* MPLS label. */ |
| 288 | OF_OXM_INDEX_MPLS_TC = 35, /* MPLS TC. */ |
Rich Lane | d8d29c9 | 2013-09-24 13:46:42 -0700 | [diff] [blame] | 289 | |
| 290 | OF_OXM_INDEX_BSN_IN_PORTS_128 = 36, |
Rich Lane | 6171836 | 2013-10-24 16:59:42 -0700 | [diff] [blame] | 291 | OF_OXM_INDEX_BSN_LAG_ID = 37, |
Rich Lane | eb21c4f | 2013-10-28 17:34:41 -0700 | [diff] [blame] | 292 | OF_OXM_INDEX_BSN_VRF = 38, |
| 293 | OF_OXM_INDEX_BSN_GLOBAL_VRF_ALLOWED = 39, |
| 294 | OF_OXM_INDEX_BSN_L3_INTERFACE_CLASS_ID = 40, |
| 295 | OF_OXM_INDEX_BSN_L3_SRC_CLASS_ID = 41, |
| 296 | OF_OXM_INDEX_BSN_L3_DST_CLASS_ID = 42, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | #define OF_OXM_BIT(index) (((uint64_t) 1) << (index)) |
| 300 | |
| 301 | /* |
| 302 | * The generic match structure uses the OXM bit indices for it's |
| 303 | * bitmasks for active and masked values |
| 304 | */ |
| 305 | """) |
| 306 | for key, entry in match.of_match_members.items(): |
| 307 | out.write(""" |
| 308 | /* Mask/value check/set macros for %(key)s */ |
| 309 | |
| 310 | /** |
| 311 | * Set the mask for an exact match of %(key)s |
| 312 | */ |
| 313 | #define OF_MATCH_MASK_%(ku)s_EXACT_SET(_match) \\ |
| 314 | MEMSET(&(_match)->masks.%(key)s, 0xff, \\ |
| 315 | sizeof(((_match)->masks).%(key)s)) |
| 316 | |
| 317 | /** |
| 318 | * Clear the mask for %(key)s making that field inactive for the match |
| 319 | */ |
| 320 | #define OF_MATCH_MASK_%(ku)s_CLEAR(_match) \\ |
| 321 | MEMSET(&(_match)->masks.%(key)s, 0, \\ |
| 322 | sizeof(((_match)->masks).%(key)s)) |
| 323 | |
| 324 | /** |
| 325 | * Test whether the match is exact for %(key)s |
| 326 | */ |
| 327 | #define OF_MATCH_MASK_%(ku)s_EXACT_TEST(_match) \\ |
| 328 | OF_VARIABLE_IS_ALL_ONES(&(((_match)->masks).%(key)s)) |
| 329 | |
| 330 | /** |
| 331 | * Test whether key %(key)s is being checked in the match |
| 332 | */ |
| 333 | #define OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(_match) \\ |
| 334 | OF_VARIABLE_IS_NON_ZERO(&(((_match)->masks).%(key)s)) |
| 335 | |
| 336 | """ % dict(key=key, bit=match.oxm_index(key), ku=key.upper())) |
| 337 | |
| 338 | def gen_incompat_members(out=sys.stdout): |
| 339 | """ |
| 340 | Generate a macro that lists all the unified fields which are |
| 341 | incompatible with v1 matches |
| 342 | """ |
| 343 | out.write(""" |
| 344 | /* Identify bits in unified match that are incompatible with V1, V2 matches */ |
| 345 | #define OF_MATCH_V1_INCOMPAT ( (uint64_t)0 """) |
| 346 | for key in match.of_match_members: |
| 347 | if key in match.of_v1_keys: |
| 348 | continue |
| 349 | out.write("\\\n | ((uint64_t)1 << %s)" % match.oxm_index(key)) |
| 350 | out.write(")\n\n") |
| 351 | |
| 352 | out.write("#define OF_MATCH_V2_INCOMPAT ( (uint64_t)0 ") |
| 353 | for key in match.of_match_members: |
| 354 | if key in match.of_v2_keys: |
| 355 | continue |
| 356 | out.write("\\\n | ((uint64_t)1 << %s)" % match.oxm_index(key)) |
| 357 | out.write(""") |
| 358 | |
| 359 | /* Indexed by version number */ |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 360 | extern const uint64_t of_match_incompat[4]; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 361 | """) |
| 362 | |
| 363 | |
| 364 | # # FIXME: Make these version specific |
| 365 | # def name_to_index(a, name, key="name"): |
| 366 | # """ |
| 367 | # Given an array, a, with each entry a dict, and a name, |
| 368 | # find the entry with key matching name and return the index |
| 369 | # """ |
| 370 | # count = 0 |
| 371 | # for e in a: |
| 372 | # if e[key] == name: |
| 373 | # return count |
| 374 | # count += 1 |
| 375 | # return -1 |
| 376 | |
| 377 | def gen_wc_convert_literal(out): |
| 378 | """ |
| 379 | A bunch of literal C code that's associated with match conversions |
| 380 | @param out The output file handle |
| 381 | """ |
| 382 | out.write(""" |
| 383 | |
| 384 | /* Some internal macros and utility functions */ |
| 385 | |
| 386 | /* For counting bits in a uint32 */ |
| 387 | #define _VAL_AND_5s(v) ((v) & 0x55555555) |
| 388 | #define _VAL_EVERY_OTHER(v) (_VAL_AND_5s(v) + _VAL_AND_5s(v >> 1)) |
| 389 | #define _VAL_AND_3s(v) ((v) & 0x33333333) |
| 390 | #define _VAL_PAIRS(v) (_VAL_AND_3s(v) + _VAL_AND_3s(v >> 2)) |
| 391 | #define _VAL_QUADS(v) (((val) + ((val) >> 4)) & 0x0F0F0F0F) |
| 392 | #define _VAL_BYTES(v) ((val) + ((val) >> 8)) |
| 393 | |
| 394 | /** |
| 395 | * Counts the number of bits set in an integer |
| 396 | */ |
| 397 | static inline int |
| 398 | _COUNT_BITS(unsigned int val) |
| 399 | { |
| 400 | val = _VAL_EVERY_OTHER(val); |
| 401 | val = _VAL_PAIRS(val); |
| 402 | val = _VAL_QUADS(val); |
| 403 | val = _VAL_BYTES(val); |
| 404 | |
| 405 | return (val & 0XFF) + ((val >> 16) & 0xFF); |
| 406 | } |
| 407 | |
| 408 | /* Indexed by version number */ |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 409 | const uint64_t of_match_incompat[4] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 410 | -1, |
| 411 | OF_MATCH_V1_INCOMPAT, |
| 412 | OF_MATCH_V2_INCOMPAT, |
| 413 | 0 |
| 414 | }; |
| 415 | |
| 416 | """) |
| 417 | |
| 418 | |
| 419 | def gen_unified_match_to_v1(out): |
| 420 | """ |
| 421 | Generate C code to convert a unified match structure to a V1 match struct |
| 422 | @param out The output file handle |
| 423 | """ |
| 424 | |
| 425 | out.write(""" |
| 426 | /** |
| 427 | * Check if match is compatible with OF 1.0 |
| 428 | * @param match The match being checked |
| 429 | */ |
| 430 | static inline int |
| 431 | of_match_v1_compat_check(of_match_t *match) |
| 432 | { |
| 433 | """) |
| 434 | for key in match.of_match_members: |
| 435 | if key in match.of_v1_keys: |
| 436 | continue |
| 437 | out.write(""" |
| 438 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) { |
| 439 | return 0; |
| 440 | } |
| 441 | """ % dict(ku=key.upper())) |
| 442 | |
| 443 | out.write(""" |
| 444 | return 1; |
| 445 | } |
| 446 | """) |
| 447 | |
| 448 | out.write(""" |
| 449 | /** |
| 450 | * Convert a generic match object to an OF_VERSION_1_0 object |
| 451 | * @param src Pointer to the generic match object source |
| 452 | * @param dst Pointer to the OF 1.0 wire structure |
| 453 | * |
| 454 | * The wire structure is initialized by this function if it doesn't |
| 455 | * not have the proper object ID. |
| 456 | */ |
| 457 | |
| 458 | int |
| 459 | of_match_to_wire_match_v1(of_match_t *src, of_match_v1_t *dst) |
| 460 | { |
| 461 | of_wc_bmap_t wildcards = 0; |
| 462 | int ip_mask_index; |
| 463 | |
| 464 | if ((src == NULL) || (dst == NULL)) { |
| 465 | return OF_ERROR_PARAM; |
| 466 | } |
| 467 | if (!of_match_v1_compat_check(src)) { |
| 468 | return OF_ERROR_COMPAT; |
| 469 | } |
| 470 | if (dst->object_id != OF_MATCH_V1) { |
| 471 | of_match_v1_init(dst, OF_VERSION_1_0, 0, 0); |
| 472 | } |
| 473 | """) |
| 474 | for key in sorted(match.of_v1_keys): |
| 475 | if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here |
| 476 | out.write(""" |
| 477 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 478 | ip_mask_index = of_ip_mask_to_index(src->masks.%(key)s); |
| 479 | of_match_v1_%(key)s_set(dst, src->fields.%(key)s); |
| 480 | } else { /* Wildcarded, look for 0 mask */ |
| 481 | ip_mask_index = of_ip_mask_to_index(0); |
| 482 | } |
| 483 | OF_MATCH_V1_WC_%(ku)s_SET(wildcards, ip_mask_index); |
| 484 | """ % dict(key=key, ku=key.upper())) |
| 485 | else: |
| 486 | out.write(""" |
| 487 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 488 | of_match_v1_%(key)s_set(dst, src->fields.%(key)s); |
| 489 | } else { |
| 490 | OF_MATCH_V1_WC_%(ku)s_SET(wildcards); |
| 491 | } |
| 492 | """ % dict(key=key, ku=key.upper())) |
| 493 | |
| 494 | out.write(""" |
| 495 | of_match_v1_wildcards_set(dst, wildcards); |
| 496 | |
| 497 | return OF_ERROR_NONE; |
| 498 | } |
| 499 | """) |
| 500 | |
| 501 | def all_ones_mask(d_type): |
| 502 | if d_type == "of_mac_addr_t": |
| 503 | return "of_mac_addr_all_ones" |
| 504 | else: |
| 505 | return "((%s) -1)" % d_type |
| 506 | |
| 507 | def gen_unified_match_to_v2(out): |
| 508 | """ |
| 509 | Generate C code to convert a unified match structure to a V2 match struct |
| 510 | @param out The output file handle |
| 511 | """ |
| 512 | |
| 513 | out.write(""" |
| 514 | /** |
| 515 | * Check if match is compatible with OF 1.0 |
| 516 | * @param match The match being checked |
| 517 | */ |
| 518 | static inline int |
| 519 | of_match_v2_compat_check(of_match_t *match) |
| 520 | { |
| 521 | """) |
| 522 | for key in match.of_match_members: |
| 523 | if key in match.of_v2_keys: |
| 524 | continue |
| 525 | out.write(""" |
| 526 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) { |
| 527 | return 0; |
| 528 | } |
| 529 | """ % dict(ku=key.upper())) |
| 530 | |
| 531 | out.write(""" |
| 532 | return 1; |
| 533 | } |
| 534 | """) |
| 535 | |
| 536 | out.write(""" |
| 537 | /** |
| 538 | * Convert a generic match object to an OF_VERSION_1_1 object |
| 539 | * @param src Pointer to the generic match object source |
| 540 | * @param dst Pointer to the OF 1.1 wire structure |
| 541 | * |
| 542 | * The wire structure is initialized by this function. |
| 543 | */ |
| 544 | |
| 545 | int |
| 546 | of_match_to_wire_match_v2(of_match_t *src, of_match_v2_t *dst) |
| 547 | { |
| 548 | of_wc_bmap_t wildcards = 0; |
| 549 | |
| 550 | if ((src == NULL) || (dst == NULL)) { |
| 551 | return OF_ERROR_PARAM; |
| 552 | } |
| 553 | if (!of_match_v2_compat_check(src)) { |
| 554 | return OF_ERROR_COMPAT; |
| 555 | } |
| 556 | if (dst->object_id != OF_MATCH_V2) { |
| 557 | of_match_v2_init(dst, OF_VERSION_1_1, 0, 0); |
| 558 | } |
| 559 | """) |
| 560 | for key in match.of_v2_keys: |
| 561 | if key in match.of_v2_full_mask: |
| 562 | ones_mask = all_ones_mask(match.of_match_members[key]["m_type"]) |
| 563 | out.write(""" |
| 564 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 565 | if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) { |
| 566 | of_match_v2_%(key)s_mask_set(dst, |
| 567 | src->masks.%(key)s); |
| 568 | } else { /* Exact match; use all ones mask */ |
| 569 | of_match_v2_%(key)s_mask_set(dst, |
| 570 | %(ones_mask)s); |
| 571 | } |
| 572 | of_match_v2_%(key)s_set(dst, src->fields.%(key)s); |
| 573 | } |
| 574 | |
| 575 | """ % dict(key=key, ku=key.upper(), ones_mask=ones_mask)) |
| 576 | else: |
| 577 | out.write(""" |
| 578 | if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) { |
| 579 | return OF_ERROR_COMPAT; |
| 580 | } |
| 581 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 582 | of_match_v2_%(key)s_set(dst, src->fields.%(key)s); |
| 583 | } else { |
| 584 | OF_MATCH_V2_WC_%(ku)s_SET(wildcards); |
| 585 | } |
| 586 | """ % dict(key=key, ku=key.upper(), |
| 587 | wc_bit="OF_MATCH_WC_V2_%s" % key.upper())) |
| 588 | |
| 589 | out.write(""" |
| 590 | of_match_v2_wildcards_set(dst, wildcards); |
| 591 | |
| 592 | return OF_ERROR_NONE; |
| 593 | } |
| 594 | """) |
| 595 | |
| 596 | def gen_unified_match_to_v3(out): |
| 597 | """ |
| 598 | Generate C code to convert a unified match structure to a V3 match |
| 599 | |
| 600 | This is much easier as the unified struct is based on V3 |
| 601 | @param out The output file handle |
| 602 | """ |
| 603 | out.write(""" |
| 604 | static int |
| 605 | populate_oxm_list(of_match_t *src, of_list_oxm_t *oxm_list) |
| 606 | { |
| 607 | of_oxm_t oxm_entry; |
| 608 | |
| 609 | /* For each active member, add an OXM entry to the list */ |
| 610 | """) |
Rich Lane | 4964d54 | 2013-10-14 18:13:47 -0700 | [diff] [blame] | 611 | for key in match.match_keys_sorted: |
| 612 | entry = match.of_match_members[key] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 613 | out.write("""\ |
| 614 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 615 | if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) { |
| 616 | of_oxm_%(key)s_masked_t *elt; |
| 617 | elt = &oxm_entry.%(key)s_masked; |
| 618 | |
| 619 | of_oxm_%(key)s_masked_init(elt, |
Rich Lane | cfd4ce0 | 2013-07-12 16:37:14 -0700 | [diff] [blame] | 620 | oxm_list->version, -1, 1); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 621 | of_list_oxm_append_bind(oxm_list, &oxm_entry); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 622 | of_oxm_%(key)s_masked_value_set(elt, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 623 | src->fields.%(key)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 624 | of_oxm_%(key)s_masked_value_mask_set(elt, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 625 | src->masks.%(key)s); |
| 626 | } else { /* Active, but not masked */ |
| 627 | of_oxm_%(key)s_t *elt; |
| 628 | elt = &oxm_entry.%(key)s; |
| 629 | of_oxm_%(key)s_init(elt, |
Rich Lane | cfd4ce0 | 2013-07-12 16:37:14 -0700 | [diff] [blame] | 630 | oxm_list->version, -1, 1); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 631 | of_list_oxm_append_bind(oxm_list, &oxm_entry); |
| 632 | of_oxm_%(key)s_value_set(elt, src->fields.%(key)s); |
| 633 | } |
| 634 | } |
| 635 | """ % dict(key=key, ku=key.upper())) |
| 636 | out.write(""" |
| 637 | return OF_ERROR_NONE; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Convert a generic match object to an OF_VERSION_1_2 object |
| 642 | * @param src Pointer to the generic match object source |
| 643 | * @param dst Pointer to the OF 1.2 wire structure |
| 644 | * |
| 645 | * The wire structure is initialized by this function if the object |
| 646 | * id is not correct in the object |
| 647 | */ |
| 648 | |
| 649 | int |
| 650 | of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst) |
| 651 | { |
| 652 | int rv = OF_ERROR_NONE; |
| 653 | of_list_oxm_t *oxm_list; |
| 654 | |
| 655 | if ((src == NULL) || (dst == NULL)) { |
| 656 | return OF_ERROR_PARAM; |
| 657 | } |
| 658 | if (dst->object_id != OF_MATCH_V3) { |
Rich Lane | cfd4ce0 | 2013-07-12 16:37:14 -0700 | [diff] [blame] | 659 | of_match_v3_init(dst, OF_VERSION_1_2, 0, 0); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 660 | } |
Rich Lane | cfd4ce0 | 2013-07-12 16:37:14 -0700 | [diff] [blame] | 661 | if ((oxm_list = of_list_oxm_new(dst->version)) == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 662 | return OF_ERROR_RESOURCE; |
| 663 | } |
| 664 | |
| 665 | rv = populate_oxm_list(src, oxm_list); |
| 666 | |
| 667 | if (rv == OF_ERROR_NONE) { |
| 668 | rv = of_match_v3_oxm_list_set(dst, oxm_list); |
| 669 | } |
| 670 | |
| 671 | of_list_oxm_delete(oxm_list); |
| 672 | |
| 673 | return rv; |
| 674 | } |
| 675 | """) |
| 676 | |
| 677 | def gen_v1_to_unified_match(out): |
| 678 | """ |
| 679 | Generate the code that maps a v1 wire format match object |
| 680 | to a unified match object |
| 681 | """ |
| 682 | # for each v1 member, if not in wildcards |
| 683 | # translate to unified. Treat nw_src/dst specially |
| 684 | out.write(""" |
| 685 | |
| 686 | /** |
| 687 | * Convert an OF_VERSION_1_0 object to a generic match object |
| 688 | * @param src Pointer to the OF 1.0 wire structure source |
| 689 | * @param dst Pointer to the generic match object destination |
| 690 | * |
| 691 | * The wire structure is initialized by this function. |
| 692 | */ |
| 693 | |
| 694 | int |
| 695 | of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst) |
| 696 | { |
| 697 | of_wc_bmap_t wc; |
| 698 | int count; |
| 699 | |
| 700 | MEMSET(dst, 0, sizeof(*dst)); |
| 701 | dst->version = src->version; |
| 702 | |
| 703 | of_match_v1_wildcards_get(src, &wc); |
| 704 | """) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 705 | for key in sorted(match.of_v1_keys): |
| 706 | if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here |
| 707 | out.write(""" |
| 708 | count = OF_MATCH_V1_WC_%(ku)s_GET(wc); |
| 709 | dst->masks.%(key)s = of_ip_index_to_mask(count); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 710 | of_match_v1_%(key)s_get(src, &dst->fields.%(key)s); |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 711 | /* Clear the bits not indicated by mask; IP addrs are special for 1.0 */ |
| 712 | dst->fields.%(key)s &= dst->masks.%(key)s; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 713 | """ % dict(ku=key.upper(), key=key)) |
| 714 | else: |
| 715 | out.write(""" |
| 716 | if (!(OF_MATCH_V1_WC_%(ku)s_TEST(wc))) { |
| 717 | of_match_v1_%(key)s_get(src, &dst->fields.%(key)s); |
| 718 | OF_MATCH_MASK_%(ku)s_EXACT_SET(dst); |
| 719 | } |
| 720 | """ % dict(ku=key.upper(), key=key)) |
| 721 | |
| 722 | out.write(""" |
| 723 | return OF_ERROR_NONE; |
| 724 | } |
| 725 | """) |
| 726 | |
| 727 | def gen_v2_to_unified_match(out): |
| 728 | """ |
| 729 | Generate the code that maps a v2 wire format match object |
| 730 | to a unified match object |
| 731 | """ |
| 732 | out.write(""" |
| 733 | int |
| 734 | of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst) |
| 735 | { |
| 736 | of_wc_bmap_t wc; |
| 737 | |
| 738 | MEMSET(dst, 0, sizeof(*dst)); |
| 739 | dst->version = src->version; |
| 740 | |
| 741 | of_match_v2_wildcards_get(src, &wc); |
| 742 | """) |
| 743 | for key in match.of_v2_keys: |
| 744 | if key in match.of_v2_full_mask: |
| 745 | out.write(""" |
| 746 | of_match_v2_%(key)s_mask_get(src, &dst->masks.%(key)s); |
| 747 | if (OF_VARIABLE_IS_NON_ZERO(&dst->masks.%(key)s)) { /* Matching something */ |
| 748 | of_match_v2_%(key)s_get(src, &dst->fields.%(key)s); |
| 749 | } |
| 750 | """ % dict(ku=key.upper(), key=key)) |
| 751 | else: |
| 752 | out.write(""" |
| 753 | if (!(OF_MATCH_V2_WC_%(ku)s_TEST(wc))) { |
| 754 | of_match_v2_%(key)s_get(src, &dst->fields.%(key)s); |
| 755 | OF_MATCH_MASK_%(ku)s_EXACT_SET(dst); |
| 756 | } |
| 757 | """ % dict(ku=key.upper(), key=key)) |
| 758 | |
| 759 | out.write(""" |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 760 | /* Clear values outside of masks */ |
| 761 | of_match_values_mask(dst); |
| 762 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 763 | return OF_ERROR_NONE; |
| 764 | } |
| 765 | """) |
| 766 | |
| 767 | |
| 768 | def gen_v3_to_unified_match(out): |
| 769 | """ |
| 770 | Generate the code that maps a v3 wire format match object |
| 771 | to a unified match object |
| 772 | """ |
| 773 | # Iterate thru the OXM list members |
| 774 | out.write(""" |
| 775 | int |
| 776 | of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst) |
| 777 | { |
| 778 | int rv; |
| 779 | of_list_oxm_t oxm_list; |
| 780 | of_oxm_t oxm_entry; |
| 781 | """) |
| 782 | # for key in match.of_match_members: |
| 783 | # out.write(" of_oxm_%s_t *%s;\n" % (key, key)) |
| 784 | # out.write(" of_oxm_%s_masked_t *%s_masked;\n" % (key, key)) |
| 785 | |
| 786 | out.write(""" |
| 787 | MEMSET(dst, 0, sizeof(*dst)); |
| 788 | dst->version = src->version; |
| 789 | |
| 790 | of_match_v3_oxm_list_bind(src, &oxm_list); |
| 791 | rv = of_list_oxm_first(&oxm_list, &oxm_entry); |
| 792 | |
| 793 | while (rv == OF_ERROR_NONE) { |
| 794 | switch (oxm_entry.header.object_id) { /* What kind of entry is this */ |
| 795 | """) |
| 796 | for key in match.of_match_members: |
| 797 | out.write(""" |
| 798 | case OF_OXM_%(ku)s_MASKED: |
| 799 | of_oxm_%(key)s_masked_value_mask_get( |
| 800 | &oxm_entry.%(key)s_masked, |
| 801 | &dst->masks.%(key)s); |
| 802 | of_oxm_%(key)s_masked_value_get( |
| 803 | &oxm_entry.%(key)s, |
| 804 | &dst->fields.%(key)s); |
| 805 | break; |
| 806 | case OF_OXM_%(ku)s: |
| 807 | OF_MATCH_MASK_%(ku)s_EXACT_SET(dst); |
| 808 | of_oxm_%(key)s_value_get( |
| 809 | &oxm_entry.%(key)s, |
| 810 | &dst->fields.%(key)s); |
| 811 | break; |
| 812 | """ % (dict(ku=key.upper(), key=key))) |
| 813 | |
| 814 | out.write(""" |
| 815 | default: |
| 816 | /* @fixme Add debug statement */ |
| 817 | return OF_ERROR_PARSE; |
| 818 | } /* end switch */ |
| 819 | rv = of_list_oxm_next(&oxm_list, &oxm_entry); |
| 820 | } /* end OXM iteration */ |
| 821 | |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 822 | /* Clear values outside of masks */ |
| 823 | of_match_values_mask(dst); |
| 824 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 825 | return OF_ERROR_NONE; |
| 826 | } |
| 827 | """) |
| 828 | |
| 829 | def gen_serialize(out): |
| 830 | out.write(""" |
| 831 | /** |
| 832 | * Serialize a match structure according to the version passed |
| 833 | * @param version The version to use for serialization protocol |
| 834 | * @param match Pointer to the structure to serialize |
| 835 | * @param octets Pointer to an octets object to fill out |
| 836 | * |
| 837 | * A buffer is allocated using normal internal ALLOC/FREE semantics |
| 838 | * and pointed to by the octets object. The length of the resulting |
| 839 | * serialization is in octets->bytes. |
| 840 | * |
| 841 | * For 1.2 matches, returns the padded serialized structure |
| 842 | * |
| 843 | * Note that FREE must be called on octets->data when processing of |
| 844 | * the object is complete. |
| 845 | */ |
| 846 | |
| 847 | int |
| 848 | of_match_serialize(of_version_t version, of_match_t *match, of_octets_t *octets) |
| 849 | { |
| 850 | int rv; |
| 851 | |
| 852 | switch (version) { |
| 853 | """) |
| 854 | for version in of_g.of_version_range: |
| 855 | out.write(""" |
| 856 | case %(ver_name)s: |
| 857 | { |
| 858 | of_match_v%(version)s_t *wire_match; |
| 859 | wire_match = of_match_v%(version)s_new(version); |
| 860 | if (wire_match == NULL) { |
| 861 | return OF_ERROR_RESOURCE; |
| 862 | } |
| 863 | if ((rv = of_match_to_wire_match_v%(version)s(match, wire_match)) < 0) { |
| 864 | of_match_v%(version)s_delete(wire_match); |
| 865 | return rv; |
| 866 | } |
| 867 | octets->bytes = OF_MATCH_BYTES(wire_match->length); |
| 868 | of_object_wire_buffer_steal((of_object_t *)wire_match, |
| 869 | &octets->data); |
| 870 | of_match_v%(version)s_delete(wire_match); |
| 871 | } |
| 872 | break; |
| 873 | """ % dict(version=version, ver_name=of_g.of_version_wire2name[version])) |
| 874 | out.write(""" |
| 875 | default: |
| 876 | return OF_ERROR_COMPAT; |
| 877 | } |
| 878 | |
| 879 | return OF_ERROR_NONE; |
| 880 | } |
| 881 | """) |
| 882 | |
| 883 | |
| 884 | def gen_deserialize(out): |
| 885 | out.write(""" |
| 886 | /** |
| 887 | * Deserialize a match structure according to the version passed |
| 888 | * @param version The version to use for deserialization protocol |
| 889 | * @param match Pointer to the structure to fill out |
| 890 | * @param octets Pointer to an octets object holding serial buffer |
| 891 | * |
| 892 | * Normally the octets object will point to a part of a wire buffer. |
| 893 | */ |
| 894 | |
| 895 | int |
| 896 | of_match_deserialize(of_version_t version, of_match_t *match, |
| 897 | of_octets_t *octets) |
| 898 | { |
| 899 | if (octets->bytes == 0) { /* No match specified means all wildcards */ |
| 900 | MEMSET(match, 0, sizeof(*match)); |
| 901 | match->version = version; |
| 902 | |
| 903 | return OF_ERROR_NONE; |
| 904 | } |
| 905 | |
| 906 | switch (version) { |
| 907 | """) |
| 908 | for version in of_g.of_version_range: |
| 909 | out.write(""" |
| 910 | case %(ver_name)s: |
| 911 | { /* FIXME: check init bytes */ |
| 912 | uint8_t *tmp; |
| 913 | of_match_v%(version)d_t wire_match; |
| 914 | of_match_v%(version)d_init(&wire_match, |
| 915 | %(ver_name)s, -1, 1); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 916 | of_object_buffer_bind((of_object_t *)&wire_match, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 917 | octets->data, octets->bytes, NULL); |
| 918 | OF_TRY(of_match_v%(version)d_to_match(&wire_match, match)); |
| 919 | |
| 920 | /* Free the wire buffer control block without freeing |
| 921 | * octets->bytes. */ |
| 922 | of_wire_buffer_steal(wire_match.wire_object.wbuf, &tmp); |
| 923 | } |
| 924 | break; |
| 925 | """ % dict(version=version, ver_name=of_g.of_version_wire2name[version])) |
| 926 | |
| 927 | out.write(""" |
| 928 | default: |
| 929 | return OF_ERROR_COMPAT; |
| 930 | } |
| 931 | |
| 932 | return OF_ERROR_NONE; |
| 933 | } |
| 934 | """) |
| 935 | |
| 936 | def gen_match_comp(out=sys.stdout): |
| 937 | """ |
| 938 | Generate match comparison functions |
| 939 | """ |
| 940 | out.write(""" |
| 941 | /** |
| 942 | * Determine "more specific" relationship between mac addrs |
| 943 | * @return true if v1 is equal to or more specific than v2 |
| 944 | * |
| 945 | * @todo Could be optimized |
| 946 | * |
| 947 | * Check: Every bit in v2 is set in v1; v1 may have add'l bits set. |
| 948 | * That is, return false if there is a bit set in v2 and not in v1. |
| 949 | */ |
| 950 | |
| 951 | static inline int |
| 952 | of_more_specific_ipv6(of_ipv6_t *v1, of_ipv6_t *v2) { |
| 953 | int idx; |
| 954 | |
| 955 | for (idx = 0; idx < OF_IPV6_BYTES; idx++) { |
| 956 | /* If there's a bit set in v2 that is clear in v1, return false */ |
| 957 | if (~v1->addr[idx] & v2->addr[idx]) { |
| 958 | return 0; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | return 1; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Boolean test if two values agree when restricted to a mask |
| 967 | */ |
| 968 | |
| 969 | static inline int |
| 970 | of_restricted_match_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, of_ipv6_t *mask) { |
| 971 | int idx; |
| 972 | |
| 973 | for (idx = 0; idx < OF_IPV6_BYTES; idx++) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 974 | if ((v1->addr[idx] & mask->addr[idx]) != |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 975 | (v2->addr[idx] & mask->addr[idx])) { |
| 976 | return 0; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Boolean test if two values "overlap" (agree on common masks) |
| 985 | */ |
| 986 | |
| 987 | static inline int |
| 988 | of_overlap_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, |
| 989 | of_ipv6_t *m1, of_ipv6_t *m2) { |
| 990 | int idx; |
| 991 | |
| 992 | for (idx = 0; idx < OF_IPV6_BYTES; idx++) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 993 | if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) != |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 994 | ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) { |
| 995 | return 0; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | return 1; |
| 1000 | } |
| 1001 | |
| 1002 | #define OF_MORE_SPECIFIC_IPV6(v1, v2) of_more_specific_ipv6((v1), (v2)) |
| 1003 | |
| 1004 | #define OF_RESTRICTED_MATCH_IPV6(v1, v2, mask) \\ |
| 1005 | of_restricted_match_ipv6((v1), (v2), (mask)) |
| 1006 | |
| 1007 | #define OF_OVERLAP_IPV6(v1, v2, m1, m2) of_overlap_ipv6((v1), (v2), (m1), (m2)) |
| 1008 | |
| 1009 | /** |
| 1010 | * Determine "more specific" relationship between mac addrs |
| 1011 | * @return true if v1 is equal to or more specific than v2 |
| 1012 | * |
| 1013 | * @todo Could be optimized |
| 1014 | * |
| 1015 | * Check: Every bit in v2 is set in v1; v1 may have add'l bits set. |
| 1016 | * That is, return false if there is a bit set in v2 and not in v1. |
| 1017 | */ |
| 1018 | static inline int |
| 1019 | of_more_specific_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2) { |
| 1020 | int idx; |
| 1021 | |
| 1022 | for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) { |
| 1023 | /* If there's a bit set in v2 that is clear in v1, return false */ |
| 1024 | if (~v1->addr[idx] & v2->addr[idx]) { |
| 1025 | return 0; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return 1; |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * Boolean test if two values agree when restricted to a mask |
| 1034 | */ |
| 1035 | static inline int |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1036 | of_restricted_match_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1037 | of_mac_addr_t *mask) { |
| 1038 | int idx; |
| 1039 | |
| 1040 | for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1041 | if ((v1->addr[idx] & mask->addr[idx]) != |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1042 | (v2->addr[idx] & mask->addr[idx])) { |
| 1043 | return 0; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | return 1; |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * Boolean test if two values "overlap" (agree on common masks) |
| 1052 | */ |
| 1053 | |
| 1054 | static inline int |
| 1055 | of_overlap_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2, |
| 1056 | of_mac_addr_t *m1, of_mac_addr_t *m2) { |
| 1057 | int idx; |
| 1058 | |
| 1059 | for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1060 | if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) != |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1061 | ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) { |
| 1062 | return 0; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | return 1; |
| 1067 | } |
| 1068 | |
| 1069 | #define OF_MORE_SPECIFIC_MAC_ADDR(v1, v2) of_more_specific_mac_addr((v1), (v2)) |
| 1070 | |
| 1071 | #define OF_RESTRICTED_MATCH_MAC_ADDR(v1, v2, mask) \\ |
| 1072 | of_restricted_match_mac_addr((v1), (v2), (mask)) |
| 1073 | |
| 1074 | #define OF_OVERLAP_MAC_ADDR(v1, v2, m1, m2) \\ |
| 1075 | of_overlap_mac_addr((v1), (v2), (m1), (m2)) |
| 1076 | |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 1077 | #define OF_MORE_SPECIFIC_BITMAP_128(v1, v2) \\ |
| 1078 | (OF_MORE_SPECIFIC_INT((v1)->lo, (v2)->lo) && OF_MORE_SPECIFIC_INT((v1)->hi, (v2)->hi)) |
| 1079 | |
| 1080 | #define OF_RESTRICTED_MATCH_BITMAP_128(v1, v2, mask) \\ |
| 1081 | (OF_RESTRICTED_MATCH_INT((v1)->lo, (v2)->lo, (mask)->lo) && OF_RESTRICTED_MATCH_INT((v1)->hi, (v2)->hi, (mask)->hi)) |
| 1082 | |
| 1083 | #define OF_OVERLAP_BITMAP_128(v1, v2, m1, m2) \\ |
| 1084 | (OF_OVERLAP_INT((v1)->lo, (v2)->lo, (m1)->lo, (m2)->lo) && OF_OVERLAP_INT((v1)->hi, (v2)->hi, (m1)->hi, (m2)->hi)) |
| 1085 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1086 | /** |
| 1087 | * More-specific-than macro for integer types; see above |
| 1088 | * @return true if v1 is equal to or more specific than v2 |
| 1089 | * |
| 1090 | * If there is a bit that is set in v2 and not in v1, return false. |
| 1091 | */ |
| 1092 | #define OF_MORE_SPECIFIC_INT(v1, v2) (!(~(v1) & (v2))) |
| 1093 | |
| 1094 | /** |
| 1095 | * Boolean test if two values agree when restricted to a mask |
| 1096 | */ |
| 1097 | #define OF_RESTRICTED_MATCH_INT(v1, v2, mask) \\ |
| 1098 | (((v1) & (mask)) == ((v2) & (mask))) |
| 1099 | |
| 1100 | |
| 1101 | #define OF_OVERLAP_INT(v1, v2, m1, m2) \\ |
| 1102 | ((((v1) & (m1)) & (m2)) == (((v2) & (m1)) & (m2))) |
| 1103 | """) |
| 1104 | |
| 1105 | out.write(""" |
| 1106 | /** |
| 1107 | * Compare two match structures for exact equality |
| 1108 | * |
| 1109 | * We just do memcmp assuming structs were memset to 0 on init |
| 1110 | */ |
| 1111 | static inline int |
| 1112 | of_match_eq(of_match_t *match1, of_match_t *match2) |
| 1113 | { |
| 1114 | return (MEMCMP(match1, match2, sizeof(of_match_t)) == 0); |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * Is the entry match more specific than (or equal to) the query match? |
| 1119 | * @param entry Match expected to be more specific (subset of query) |
| 1120 | * @param query Match expected to be less specific (superset of entry) |
| 1121 | * @returns Boolean, see below |
| 1122 | * |
| 1123 | * The assumption is that a query is being done for a non-strict |
| 1124 | * match against an entry in a table. The result is true if the |
| 1125 | * entry match indicates a more specific (but compatible) flow space |
| 1126 | * specification than that in the query match. This means that the |
| 1127 | * values agree between the two where they overlap, and that each mask |
| 1128 | * for the entry is more specific than that of the query. |
| 1129 | * |
| 1130 | * The query has the less specific mask (fewer mask bits) so it is |
| 1131 | * used for the mask when checking values. |
| 1132 | */ |
| 1133 | |
| 1134 | static inline int |
| 1135 | of_match_more_specific(of_match_t *entry, of_match_t *query) |
| 1136 | { |
| 1137 | of_match_fields_t *q_m, *e_m; /* Short hand for masks, fields */ |
| 1138 | of_match_fields_t *q_f, *e_f; |
| 1139 | |
| 1140 | q_m = &query->masks; |
| 1141 | e_m = &entry->masks; |
| 1142 | q_f = &query->fields; |
| 1143 | e_f = &entry->fields; |
| 1144 | """) |
| 1145 | for key, entry in match.of_match_members.items(): |
| 1146 | q_m = "&q_m->%s" % key |
| 1147 | e_m = "&e_m->%s" % key |
| 1148 | q_f = "&q_f->%s" % key |
| 1149 | e_f = "&e_f->%s" % key |
| 1150 | if entry["m_type"] == "of_ipv6_t": |
| 1151 | comp = "OF_MORE_SPECIFIC_IPV6" |
| 1152 | match_type = "OF_RESTRICTED_MATCH_IPV6" |
| 1153 | elif entry["m_type"] == "of_mac_addr_t": |
| 1154 | comp = "OF_MORE_SPECIFIC_MAC_ADDR" |
| 1155 | match_type = "OF_RESTRICTED_MATCH_MAC_ADDR" |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 1156 | elif entry["m_type"] == "of_bitmap_128_t": |
| 1157 | comp = "OF_MORE_SPECIFIC_BITMAP_128" |
| 1158 | match_type = "OF_RESTRICTED_MATCH_BITMAP_128" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1159 | else: # Integer |
| 1160 | comp = "OF_MORE_SPECIFIC_INT" |
| 1161 | match_type = "OF_RESTRICTED_MATCH_INT" |
| 1162 | q_m = "q_m->%s" % key |
| 1163 | e_m = "e_m->%s" % key |
| 1164 | q_f = "q_f->%s" % key |
| 1165 | e_f = "e_f->%s" % key |
| 1166 | out.write(""" |
| 1167 | /* Mask and values for %(key)s */ |
| 1168 | if (!%(comp)s(%(e_m)s, %(q_m)s)) { |
| 1169 | return 0; |
| 1170 | } |
| 1171 | if (!%(match_type)s(%(e_f)s, %(q_f)s, |
| 1172 | %(q_m)s)) { |
| 1173 | return 0; |
| 1174 | } |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1175 | """ % dict(match_type=match_type, comp=comp, q_f=q_f, e_f=e_f, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1176 | q_m=q_m, e_m=e_m, key=key)) |
| 1177 | |
| 1178 | out.write(""" |
| 1179 | return 1; |
| 1180 | } |
| 1181 | """) |
| 1182 | |
| 1183 | out.write(""" |
| 1184 | |
| 1185 | /** |
| 1186 | * Do two entries overlap? |
| 1187 | * @param match1 One match struct |
| 1188 | * @param match2 Another match struct |
| 1189 | * @returns Boolean: true if there is a packet that would match both |
| 1190 | * |
| 1191 | */ |
| 1192 | |
| 1193 | static inline int |
| 1194 | of_match_overlap(of_match_t *match1, of_match_t *match2) |
| 1195 | { |
| 1196 | of_match_fields_t *m1, *m2; /* Short hand for masks, fields */ |
| 1197 | of_match_fields_t *f1, *f2; |
| 1198 | |
| 1199 | m1 = &match1->masks; |
| 1200 | m2 = &match2->masks; |
| 1201 | f1 = &match1->fields; |
| 1202 | f2 = &match2->fields; |
| 1203 | """) |
| 1204 | for key, entry in match.of_match_members.items(): |
| 1205 | m1 = "&m1->%s" % key |
| 1206 | m2 = "&m2->%s" % key |
| 1207 | f1 = "&f1->%s" % key |
| 1208 | f2 = "&f2->%s" % key |
| 1209 | if entry["m_type"] == "of_ipv6_t": |
| 1210 | check = "OF_OVERLAP_IPV6" |
| 1211 | elif entry["m_type"] == "of_mac_addr_t": |
| 1212 | check = "OF_OVERLAP_MAC_ADDR" |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 1213 | elif entry["m_type"] == "of_bitmap_128_t": |
| 1214 | check = "OF_OVERLAP_BITMAP_128" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1215 | else: # Integer |
| 1216 | check = "OF_OVERLAP_INT" |
| 1217 | m1 = "m1->%s" % key |
| 1218 | m2 = "m2->%s" % key |
| 1219 | f1 = "f1->%s" % key |
| 1220 | f2 = "f2->%s" % key |
| 1221 | out.write(""" |
| 1222 | /* Check overlap for %(key)s */ |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1223 | if (!%(check)s(%(f1)s, %(f2)s, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1224 | %(m2)s, %(m1)s)) { |
| 1225 | return 0; /* This field differentiates; all done */ |
| 1226 | } |
| 1227 | """ % dict(check=check, f1=f1, f2=f2, m1=m1, m2=m2, key=key)) |
| 1228 | |
| 1229 | out.write(""" |
| 1230 | return 1; /* No field differentiates matches */ |
| 1231 | } |
| 1232 | """) |
| 1233 | |
| 1234 | def gen_match_conversions(out=sys.stdout): |
| 1235 | match.match_sanity_check() |
| 1236 | gen_wc_convert_literal(out) |
| 1237 | out.write(""" |
| 1238 | /** |
| 1239 | * IP Mask map. IP maks wildcards from OF 1.0 are interpretted as |
| 1240 | * indices into the map below. |
| 1241 | */ |
| 1242 | |
| 1243 | int of_ip_mask_map_init_done = 0; |
| 1244 | uint32_t of_ip_mask_map[OF_IP_MASK_MAP_COUNT]; |
| 1245 | void |
| 1246 | of_ip_mask_map_init(void) |
| 1247 | { |
| 1248 | int idx; |
| 1249 | |
| 1250 | MEMSET(of_ip_mask_map, 0, sizeof(of_ip_mask_map)); |
| 1251 | for (idx = 0; idx < 32; idx++) { |
| 1252 | of_ip_mask_map[idx] = ~((1U << idx) - 1); |
| 1253 | } |
| 1254 | |
| 1255 | of_ip_mask_map_init_done = 1; |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * @brief Set non-default IP mask for given index |
| 1260 | */ |
| 1261 | int |
| 1262 | of_ip_mask_map_set(int index, uint32_t mask) |
| 1263 | { |
| 1264 | OF_IP_MASK_INIT_CHECK; |
| 1265 | |
| 1266 | if ((index < 0) || (index >= OF_IP_MASK_MAP_COUNT)) { |
| 1267 | return OF_ERROR_RANGE; |
| 1268 | } |
| 1269 | of_ip_mask_map[index] = mask; |
| 1270 | |
| 1271 | return OF_ERROR_NONE; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
| 1275 | * @brief Get a non-default IP mask for given index |
| 1276 | */ |
| 1277 | int |
| 1278 | of_ip_mask_map_get(int index, uint32_t *mask) |
| 1279 | { |
| 1280 | OF_IP_MASK_INIT_CHECK; |
| 1281 | |
| 1282 | if ((mask == NULL) || (index < 0) || (index >= OF_IP_MASK_MAP_COUNT)) { |
| 1283 | return OF_ERROR_RANGE; |
| 1284 | } |
| 1285 | *mask = of_ip_mask_map[index]; |
| 1286 | |
| 1287 | return OF_ERROR_NONE; |
| 1288 | } |
| 1289 | |
| 1290 | /** |
| 1291 | * @brief Return the index (used as the WC field in 1.0 match) given the mask |
| 1292 | */ |
| 1293 | |
| 1294 | int |
| 1295 | of_ip_mask_to_index(uint32_t mask) |
| 1296 | { |
| 1297 | int idx; |
| 1298 | |
| 1299 | OF_IP_MASK_INIT_CHECK; |
| 1300 | |
| 1301 | /* Handle most common cases directly */ |
| 1302 | if ((mask == 0) && (of_ip_mask_map[63] == 0)) { |
| 1303 | return 63; |
| 1304 | } |
| 1305 | if ((mask == 0xffffffff) && (of_ip_mask_map[0] == 0xffffffff)) { |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | for (idx = 0; idx < OF_IP_MASK_MAP_COUNT; idx++) { |
| 1310 | if (mask == of_ip_mask_map[idx]) { |
| 1311 | return idx; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | LOCI_LOG_INFO("OF 1.0: Could not map IP addr mask 0x%x", mask); |
| 1316 | return 0x3f; |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * @brief Return the mask for the given index |
| 1321 | */ |
| 1322 | |
| 1323 | uint32_t |
| 1324 | of_ip_index_to_mask(int index) |
| 1325 | { |
| 1326 | OF_IP_MASK_INIT_CHECK; |
| 1327 | |
| 1328 | if (index >= OF_IP_MASK_MAP_COUNT) { |
| 1329 | LOCI_LOG_INFO("IP index to map: bad index %d", index); |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | return of_ip_mask_map[index]; |
| 1334 | } |
| 1335 | |
| 1336 | """) |
| 1337 | |
| 1338 | gen_unified_match_to_v1(out) |
| 1339 | gen_unified_match_to_v2(out) |
| 1340 | gen_unified_match_to_v3(out) |
| 1341 | gen_v1_to_unified_match(out) |
| 1342 | gen_v2_to_unified_match(out) |
| 1343 | gen_v3_to_unified_match(out) |
| 1344 | return |