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