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, |
Rich Lane | e0b70cc | 2014-06-12 15:02:22 -0700 | [diff] [blame] | 80 | of_object_t *parent, int offset, int length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 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 |
Rich Lane | 398bc9e | 2014-10-14 11:47:56 -0700 | [diff] [blame] | 107 | |
| 108 | #define of_match_v5_t of_match_v3_t |
| 109 | #define of_match_v5_init of_match_v3_init |
| 110 | #define of_match_v5_new of_match_v3_new |
| 111 | #define of_match_v5_to_match of_match_v3_to_match |
| 112 | #define of_match_to_wire_match_v5 of_match_to_wire_match_v3 |
| 113 | #define of_match_v5_delete of_match_v3_delete |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 114 | """) |
| 115 | |
| 116 | def gen_match_macros(out): |
| 117 | out.write(""" |
| 118 | |
| 119 | /** |
| 120 | * Definitions for wildcard macros for OF_VERSION_1_0 |
| 121 | */ |
| 122 | |
| 123 | """) |
| 124 | for key in match.of_v1_keys: |
| 125 | entry = match.of_match_members[key] |
| 126 | if "v1_wc_shift" in entry: |
| 127 | if key in ["ipv4_src", "ipv4_dst"]: |
| 128 | out.write(""" |
| 129 | #define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d |
| 130 | #define OF_MATCH_V1_WC_%(ku)s_MASK (0x3f << %(val)d) |
| 131 | #define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(0x3f << %(val)d)) |
| 132 | #define OF_MATCH_V1_WC_%(ku)s_SET(wc, value) do { \\ |
| 133 | OF_MATCH_V1_WC_%(ku)s_CLEAR(wc); \\ |
| 134 | ((wc) |= (((value) & 0x3f) << %(val)d)); \\ |
| 135 | } while (0) |
| 136 | #define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (0x3f << %(val)d)) |
| 137 | #define OF_MATCH_V1_WC_%(ku)s_GET(wc) (((wc) >> %(val)d) & 0x3f) |
| 138 | """ % dict(ku=key.upper(), val=entry["v1_wc_shift"])) |
| 139 | else: |
| 140 | out.write(""" |
| 141 | #define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d |
| 142 | #define OF_MATCH_V1_WC_%(ku)s_MASK (1 << %(val)d) |
| 143 | #define OF_MATCH_V1_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d)) |
| 144 | #define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d)) |
| 145 | #define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d)) |
| 146 | """ % dict(ku=key.upper(), val=entry["v1_wc_shift"])) |
| 147 | |
| 148 | out.write(""" |
| 149 | |
| 150 | /** |
| 151 | * Definitions for wildcard macros for OF_VERSION_1_1 |
| 152 | */ |
| 153 | """) |
| 154 | |
| 155 | for key in sorted(match.of_v2_keys): |
| 156 | entry = match.of_match_members[key] |
| 157 | if "v2_wc_shift" in entry: |
| 158 | out.write(""" |
| 159 | #define OF_MATCH_V2_WC_%(ku)s_SHIFT %(val)d |
| 160 | #define OF_MATCH_V2_WC_%(ku)s_MASK (1 << %(val)d) |
| 161 | #define OF_MATCH_V2_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d)) |
| 162 | #define OF_MATCH_V2_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d)) |
| 163 | #define OF_MATCH_V2_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d)) |
| 164 | """ % dict(ku=key.upper(), val=entry["v2_wc_shift"])) |
| 165 | |
| 166 | |
| 167 | def gen_match_struct(out=sys.stdout): |
| 168 | out.write("/* Unified, flat OpenFlow match structure based on OF 1.2 */\n") |
| 169 | out.write("typedef struct of_match_fields_s {\n") |
| 170 | out.write(" /* Version 1.2 is used for field names */\n") |
| 171 | for name in match.match_keys_sorted: |
| 172 | entry = match.of_match_members[name] |
| 173 | out.write(" %-20s %s;\n" % (entry["m_type"], entry["name"])) |
| 174 | out.write(""" |
| 175 | } of_match_fields_t; |
| 176 | |
| 177 | /** |
| 178 | * @brief The LOCI match structure. |
| 179 | */ |
| 180 | |
| 181 | typedef struct of_match_s { |
| 182 | of_version_t version; |
| 183 | of_match_fields_t fields; |
| 184 | of_match_fields_t masks; |
| 185 | } of_match_t; |
| 186 | |
Rich Lane | 68798a5 | 2014-04-16 14:57:52 -0700 | [diff] [blame] | 187 | /* |
| 188 | * AND 'len' bytes starting from 'value' with the corresponding byte in |
| 189 | * 'mask'. |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 190 | */ |
Rich Lane | d5f2345 | 2014-04-03 01:16:37 -0700 | [diff] [blame] | 191 | static inline void |
Rich Lane | 68798a5 | 2014-04-16 14:57:52 -0700 | [diff] [blame] | 192 | of_memmask(void *value, const void *mask, size_t len) |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 193 | { |
Rich Lane | 68798a5 | 2014-04-16 14:57:52 -0700 | [diff] [blame] | 194 | int i; |
| 195 | uint8_t *v = value; |
| 196 | const uint8_t *m = mask; |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 197 | |
Rich Lane | 68798a5 | 2014-04-16 14:57:52 -0700 | [diff] [blame] | 198 | for (i = 0; i < len; i++) { |
| 199 | v[i] &= m[i]; |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 204 | * @brief Map from mask to OF 1.0 wildcard |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 205 | */ |
| 206 | |
| 207 | extern int of_ip_mask_to_index(uint32_t mask); |
| 208 | |
| 209 | /** |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 210 | * @brief Map from OF 1.0 wildcard to mask |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 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(""" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 236 | /* |
| 237 | * The generic match structure uses the OXM bit indices for it's |
| 238 | * bitmasks for active and masked values |
| 239 | */ |
| 240 | """) |
| 241 | for key, entry in match.of_match_members.items(): |
| 242 | out.write(""" |
| 243 | /* Mask/value check/set macros for %(key)s */ |
| 244 | |
| 245 | /** |
| 246 | * Set the mask for an exact match of %(key)s |
| 247 | */ |
| 248 | #define OF_MATCH_MASK_%(ku)s_EXACT_SET(_match) \\ |
| 249 | MEMSET(&(_match)->masks.%(key)s, 0xff, \\ |
| 250 | sizeof(((_match)->masks).%(key)s)) |
| 251 | |
| 252 | /** |
| 253 | * Clear the mask for %(key)s making that field inactive for the match |
| 254 | */ |
| 255 | #define OF_MATCH_MASK_%(ku)s_CLEAR(_match) \\ |
| 256 | MEMSET(&(_match)->masks.%(key)s, 0, \\ |
| 257 | sizeof(((_match)->masks).%(key)s)) |
| 258 | |
| 259 | /** |
| 260 | * Test whether the match is exact for %(key)s |
| 261 | */ |
| 262 | #define OF_MATCH_MASK_%(ku)s_EXACT_TEST(_match) \\ |
| 263 | OF_VARIABLE_IS_ALL_ONES(&(((_match)->masks).%(key)s)) |
| 264 | |
| 265 | /** |
| 266 | * Test whether key %(key)s is being checked in the match |
| 267 | */ |
| 268 | #define OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(_match) \\ |
| 269 | OF_VARIABLE_IS_NON_ZERO(&(((_match)->masks).%(key)s)) |
| 270 | |
Rich Lane | d56f8d2 | 2014-05-06 14:52:55 -0700 | [diff] [blame] | 271 | """ % dict(key=key, ku=key.upper())) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 272 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 273 | def gen_unified_match_to_v1(out): |
| 274 | """ |
| 275 | Generate C code to convert a unified match structure to a V1 match struct |
| 276 | @param out The output file handle |
| 277 | """ |
| 278 | |
| 279 | out.write(""" |
| 280 | /** |
| 281 | * Check if match is compatible with OF 1.0 |
| 282 | * @param match The match being checked |
| 283 | */ |
| 284 | static inline int |
| 285 | of_match_v1_compat_check(of_match_t *match) |
| 286 | { |
| 287 | """) |
| 288 | for key in match.of_match_members: |
| 289 | if key in match.of_v1_keys: |
| 290 | continue |
| 291 | out.write(""" |
| 292 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) { |
| 293 | return 0; |
| 294 | } |
| 295 | """ % dict(ku=key.upper())) |
| 296 | |
| 297 | out.write(""" |
| 298 | return 1; |
| 299 | } |
| 300 | """) |
| 301 | |
| 302 | out.write(""" |
| 303 | /** |
| 304 | * Convert a generic match object to an OF_VERSION_1_0 object |
| 305 | * @param src Pointer to the generic match object source |
| 306 | * @param dst Pointer to the OF 1.0 wire structure |
| 307 | * |
| 308 | * The wire structure is initialized by this function if it doesn't |
| 309 | * not have the proper object ID. |
| 310 | */ |
| 311 | |
| 312 | int |
| 313 | of_match_to_wire_match_v1(of_match_t *src, of_match_v1_t *dst) |
| 314 | { |
| 315 | of_wc_bmap_t wildcards = 0; |
| 316 | int ip_mask_index; |
| 317 | |
| 318 | if ((src == NULL) || (dst == NULL)) { |
| 319 | return OF_ERROR_PARAM; |
| 320 | } |
| 321 | if (!of_match_v1_compat_check(src)) { |
| 322 | return OF_ERROR_COMPAT; |
| 323 | } |
| 324 | if (dst->object_id != OF_MATCH_V1) { |
| 325 | of_match_v1_init(dst, OF_VERSION_1_0, 0, 0); |
| 326 | } |
| 327 | """) |
| 328 | for key in sorted(match.of_v1_keys): |
| 329 | if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here |
| 330 | out.write(""" |
| 331 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 332 | ip_mask_index = of_ip_mask_to_index(src->masks.%(key)s); |
| 333 | of_match_v1_%(key)s_set(dst, src->fields.%(key)s); |
| 334 | } else { /* Wildcarded, look for 0 mask */ |
| 335 | ip_mask_index = of_ip_mask_to_index(0); |
| 336 | } |
| 337 | OF_MATCH_V1_WC_%(ku)s_SET(wildcards, ip_mask_index); |
| 338 | """ % dict(key=key, ku=key.upper())) |
| 339 | else: |
| 340 | out.write(""" |
| 341 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 342 | of_match_v1_%(key)s_set(dst, src->fields.%(key)s); |
| 343 | } else { |
| 344 | OF_MATCH_V1_WC_%(ku)s_SET(wildcards); |
| 345 | } |
| 346 | """ % dict(key=key, ku=key.upper())) |
| 347 | |
| 348 | out.write(""" |
| 349 | of_match_v1_wildcards_set(dst, wildcards); |
| 350 | |
| 351 | return OF_ERROR_NONE; |
| 352 | } |
| 353 | """) |
| 354 | |
| 355 | def all_ones_mask(d_type): |
| 356 | if d_type == "of_mac_addr_t": |
| 357 | return "of_mac_addr_all_ones" |
| 358 | else: |
| 359 | return "((%s) -1)" % d_type |
| 360 | |
| 361 | def gen_unified_match_to_v2(out): |
| 362 | """ |
| 363 | Generate C code to convert a unified match structure to a V2 match struct |
| 364 | @param out The output file handle |
| 365 | """ |
| 366 | |
| 367 | out.write(""" |
| 368 | /** |
| 369 | * Check if match is compatible with OF 1.0 |
| 370 | * @param match The match being checked |
| 371 | */ |
| 372 | static inline int |
| 373 | of_match_v2_compat_check(of_match_t *match) |
| 374 | { |
| 375 | """) |
| 376 | for key in match.of_match_members: |
| 377 | if key in match.of_v2_keys: |
| 378 | continue |
| 379 | out.write(""" |
| 380 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) { |
| 381 | return 0; |
| 382 | } |
| 383 | """ % dict(ku=key.upper())) |
| 384 | |
| 385 | out.write(""" |
| 386 | return 1; |
| 387 | } |
| 388 | """) |
| 389 | |
| 390 | out.write(""" |
| 391 | /** |
| 392 | * Convert a generic match object to an OF_VERSION_1_1 object |
| 393 | * @param src Pointer to the generic match object source |
| 394 | * @param dst Pointer to the OF 1.1 wire structure |
| 395 | * |
| 396 | * The wire structure is initialized by this function. |
| 397 | */ |
| 398 | |
| 399 | int |
| 400 | of_match_to_wire_match_v2(of_match_t *src, of_match_v2_t *dst) |
| 401 | { |
| 402 | of_wc_bmap_t wildcards = 0; |
| 403 | |
| 404 | if ((src == NULL) || (dst == NULL)) { |
| 405 | return OF_ERROR_PARAM; |
| 406 | } |
| 407 | if (!of_match_v2_compat_check(src)) { |
| 408 | return OF_ERROR_COMPAT; |
| 409 | } |
| 410 | if (dst->object_id != OF_MATCH_V2) { |
| 411 | of_match_v2_init(dst, OF_VERSION_1_1, 0, 0); |
| 412 | } |
| 413 | """) |
| 414 | for key in match.of_v2_keys: |
| 415 | if key in match.of_v2_full_mask: |
| 416 | ones_mask = all_ones_mask(match.of_match_members[key]["m_type"]) |
| 417 | out.write(""" |
| 418 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 419 | if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) { |
| 420 | of_match_v2_%(key)s_mask_set(dst, |
| 421 | src->masks.%(key)s); |
| 422 | } else { /* Exact match; use all ones mask */ |
| 423 | of_match_v2_%(key)s_mask_set(dst, |
| 424 | %(ones_mask)s); |
| 425 | } |
| 426 | of_match_v2_%(key)s_set(dst, src->fields.%(key)s); |
| 427 | } |
| 428 | |
| 429 | """ % dict(key=key, ku=key.upper(), ones_mask=ones_mask)) |
| 430 | else: |
| 431 | out.write(""" |
| 432 | if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) { |
| 433 | return OF_ERROR_COMPAT; |
| 434 | } |
| 435 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 436 | of_match_v2_%(key)s_set(dst, src->fields.%(key)s); |
| 437 | } else { |
| 438 | OF_MATCH_V2_WC_%(ku)s_SET(wildcards); |
| 439 | } |
| 440 | """ % dict(key=key, ku=key.upper(), |
| 441 | wc_bit="OF_MATCH_WC_V2_%s" % key.upper())) |
| 442 | |
| 443 | out.write(""" |
| 444 | of_match_v2_wildcards_set(dst, wildcards); |
| 445 | |
| 446 | return OF_ERROR_NONE; |
| 447 | } |
| 448 | """) |
| 449 | |
| 450 | def gen_unified_match_to_v3(out): |
| 451 | """ |
| 452 | Generate C code to convert a unified match structure to a V3 match |
| 453 | |
| 454 | This is much easier as the unified struct is based on V3 |
| 455 | @param out The output file handle |
| 456 | """ |
| 457 | out.write(""" |
| 458 | static int |
| 459 | populate_oxm_list(of_match_t *src, of_list_oxm_t *oxm_list) |
| 460 | { |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 461 | of_object_t elt; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 462 | |
| 463 | /* For each active member, add an OXM entry to the list */ |
| 464 | """) |
Rich Lane | 4964d54 | 2013-10-14 18:13:47 -0700 | [diff] [blame] | 465 | for key in match.match_keys_sorted: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 466 | out.write("""\ |
| 467 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) { |
| 468 | if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) { |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 469 | of_oxm_%(key)s_masked_init(&elt, |
Rich Lane | cfd4ce0 | 2013-07-12 16:37:14 -0700 | [diff] [blame] | 470 | oxm_list->version, -1, 1); |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 471 | of_list_oxm_append_bind(oxm_list, &elt); |
| 472 | of_oxm_%(key)s_masked_value_set(&elt, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 473 | src->fields.%(key)s); |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 474 | of_oxm_%(key)s_masked_value_mask_set(&elt, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 475 | src->masks.%(key)s); |
| 476 | } else { /* Active, but not masked */ |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 477 | of_oxm_%(key)s_init(&elt, |
Rich Lane | cfd4ce0 | 2013-07-12 16:37:14 -0700 | [diff] [blame] | 478 | oxm_list->version, -1, 1); |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 479 | of_list_oxm_append_bind(oxm_list, &elt); |
| 480 | of_oxm_%(key)s_value_set(&elt, src->fields.%(key)s); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | """ % dict(key=key, ku=key.upper())) |
| 484 | out.write(""" |
| 485 | return OF_ERROR_NONE; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Convert a generic match object to an OF_VERSION_1_2 object |
| 490 | * @param src Pointer to the generic match object source |
| 491 | * @param dst Pointer to the OF 1.2 wire structure |
| 492 | * |
| 493 | * The wire structure is initialized by this function if the object |
| 494 | * id is not correct in the object |
| 495 | */ |
| 496 | |
| 497 | int |
| 498 | of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst) |
| 499 | { |
| 500 | int rv = OF_ERROR_NONE; |
| 501 | of_list_oxm_t *oxm_list; |
| 502 | |
| 503 | if ((src == NULL) || (dst == NULL)) { |
| 504 | return OF_ERROR_PARAM; |
| 505 | } |
| 506 | if (dst->object_id != OF_MATCH_V3) { |
Rich Lane | cfd4ce0 | 2013-07-12 16:37:14 -0700 | [diff] [blame] | 507 | of_match_v3_init(dst, OF_VERSION_1_2, 0, 0); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 508 | } |
Rich Lane | cfd4ce0 | 2013-07-12 16:37:14 -0700 | [diff] [blame] | 509 | if ((oxm_list = of_list_oxm_new(dst->version)) == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 510 | return OF_ERROR_RESOURCE; |
| 511 | } |
| 512 | |
| 513 | rv = populate_oxm_list(src, oxm_list); |
| 514 | |
| 515 | if (rv == OF_ERROR_NONE) { |
| 516 | rv = of_match_v3_oxm_list_set(dst, oxm_list); |
| 517 | } |
| 518 | |
| 519 | of_list_oxm_delete(oxm_list); |
| 520 | |
| 521 | return rv; |
| 522 | } |
| 523 | """) |
| 524 | |
| 525 | def gen_v1_to_unified_match(out): |
| 526 | """ |
| 527 | Generate the code that maps a v1 wire format match object |
| 528 | to a unified match object |
| 529 | """ |
| 530 | # for each v1 member, if not in wildcards |
| 531 | # translate to unified. Treat nw_src/dst specially |
| 532 | out.write(""" |
| 533 | |
| 534 | /** |
| 535 | * Convert an OF_VERSION_1_0 object to a generic match object |
| 536 | * @param src Pointer to the OF 1.0 wire structure source |
| 537 | * @param dst Pointer to the generic match object destination |
| 538 | * |
| 539 | * The wire structure is initialized by this function. |
| 540 | */ |
| 541 | |
| 542 | int |
| 543 | of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst) |
| 544 | { |
| 545 | of_wc_bmap_t wc; |
| 546 | int count; |
| 547 | |
| 548 | MEMSET(dst, 0, sizeof(*dst)); |
| 549 | dst->version = src->version; |
| 550 | |
| 551 | of_match_v1_wildcards_get(src, &wc); |
| 552 | """) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 553 | for key in sorted(match.of_v1_keys): |
| 554 | if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here |
| 555 | out.write(""" |
| 556 | count = OF_MATCH_V1_WC_%(ku)s_GET(wc); |
| 557 | dst->masks.%(key)s = of_ip_index_to_mask(count); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 558 | of_match_v1_%(key)s_get(src, &dst->fields.%(key)s); |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 559 | /* Clear the bits not indicated by mask; IP addrs are special for 1.0 */ |
| 560 | dst->fields.%(key)s &= dst->masks.%(key)s; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 561 | """ % dict(ku=key.upper(), key=key)) |
| 562 | else: |
| 563 | out.write(""" |
| 564 | if (!(OF_MATCH_V1_WC_%(ku)s_TEST(wc))) { |
| 565 | of_match_v1_%(key)s_get(src, &dst->fields.%(key)s); |
| 566 | OF_MATCH_MASK_%(ku)s_EXACT_SET(dst); |
| 567 | } |
| 568 | """ % dict(ku=key.upper(), key=key)) |
| 569 | |
| 570 | out.write(""" |
| 571 | return OF_ERROR_NONE; |
| 572 | } |
| 573 | """) |
| 574 | |
| 575 | def gen_v2_to_unified_match(out): |
| 576 | """ |
| 577 | Generate the code that maps a v2 wire format match object |
| 578 | to a unified match object |
| 579 | """ |
| 580 | out.write(""" |
| 581 | int |
| 582 | of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst) |
| 583 | { |
| 584 | of_wc_bmap_t wc; |
| 585 | |
| 586 | MEMSET(dst, 0, sizeof(*dst)); |
| 587 | dst->version = src->version; |
| 588 | |
| 589 | of_match_v2_wildcards_get(src, &wc); |
| 590 | """) |
| 591 | for key in match.of_v2_keys: |
| 592 | if key in match.of_v2_full_mask: |
| 593 | out.write(""" |
| 594 | of_match_v2_%(key)s_mask_get(src, &dst->masks.%(key)s); |
| 595 | if (OF_VARIABLE_IS_NON_ZERO(&dst->masks.%(key)s)) { /* Matching something */ |
| 596 | of_match_v2_%(key)s_get(src, &dst->fields.%(key)s); |
| 597 | } |
Rich Lane | 054a818 | 2014-04-16 14:42:41 -0700 | [diff] [blame] | 598 | of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s)); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 599 | """ % dict(ku=key.upper(), key=key)) |
| 600 | else: |
| 601 | out.write(""" |
| 602 | if (!(OF_MATCH_V2_WC_%(ku)s_TEST(wc))) { |
| 603 | of_match_v2_%(key)s_get(src, &dst->fields.%(key)s); |
| 604 | OF_MATCH_MASK_%(ku)s_EXACT_SET(dst); |
| 605 | } |
| 606 | """ % dict(ku=key.upper(), key=key)) |
| 607 | |
| 608 | out.write(""" |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 609 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 610 | return OF_ERROR_NONE; |
| 611 | } |
| 612 | """) |
| 613 | |
| 614 | |
| 615 | def gen_v3_to_unified_match(out): |
| 616 | """ |
| 617 | Generate the code that maps a v3 wire format match object |
| 618 | to a unified match object |
| 619 | """ |
| 620 | # Iterate thru the OXM list members |
| 621 | out.write(""" |
| 622 | int |
| 623 | of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst) |
| 624 | { |
| 625 | int rv; |
| 626 | of_list_oxm_t oxm_list; |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 627 | of_object_t oxm_entry; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 628 | """) |
| 629 | # for key in match.of_match_members: |
| 630 | # out.write(" of_oxm_%s_t *%s;\n" % (key, key)) |
| 631 | # out.write(" of_oxm_%s_masked_t *%s_masked;\n" % (key, key)) |
| 632 | |
| 633 | out.write(""" |
| 634 | MEMSET(dst, 0, sizeof(*dst)); |
| 635 | dst->version = src->version; |
| 636 | |
| 637 | of_match_v3_oxm_list_bind(src, &oxm_list); |
| 638 | rv = of_list_oxm_first(&oxm_list, &oxm_entry); |
| 639 | |
| 640 | while (rv == OF_ERROR_NONE) { |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 641 | switch (oxm_entry.object_id) { /* What kind of entry is this */ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 642 | """) |
| 643 | for key in match.of_match_members: |
| 644 | out.write(""" |
| 645 | case OF_OXM_%(ku)s_MASKED: |
| 646 | of_oxm_%(key)s_masked_value_mask_get( |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 647 | &oxm_entry, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 648 | &dst->masks.%(key)s); |
| 649 | of_oxm_%(key)s_masked_value_get( |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 650 | &oxm_entry, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 651 | &dst->fields.%(key)s); |
Rich Lane | d5f2345 | 2014-04-03 01:16:37 -0700 | [diff] [blame] | 652 | of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s)); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 653 | break; |
| 654 | case OF_OXM_%(ku)s: |
| 655 | OF_MATCH_MASK_%(ku)s_EXACT_SET(dst); |
| 656 | of_oxm_%(key)s_value_get( |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 657 | &oxm_entry, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 658 | &dst->fields.%(key)s); |
| 659 | break; |
| 660 | """ % (dict(ku=key.upper(), key=key))) |
| 661 | |
| 662 | out.write(""" |
| 663 | default: |
| 664 | /* @fixme Add debug statement */ |
| 665 | return OF_ERROR_PARSE; |
| 666 | } /* end switch */ |
| 667 | rv = of_list_oxm_next(&oxm_list, &oxm_entry); |
| 668 | } /* end OXM iteration */ |
| 669 | |
| 670 | return OF_ERROR_NONE; |
| 671 | } |
| 672 | """) |
| 673 | |
| 674 | def gen_serialize(out): |
| 675 | out.write(""" |
| 676 | /** |
| 677 | * Serialize a match structure according to the version passed |
| 678 | * @param version The version to use for serialization protocol |
| 679 | * @param match Pointer to the structure to serialize |
| 680 | * @param octets Pointer to an octets object to fill out |
| 681 | * |
| 682 | * A buffer is allocated using normal internal ALLOC/FREE semantics |
| 683 | * and pointed to by the octets object. The length of the resulting |
| 684 | * serialization is in octets->bytes. |
| 685 | * |
| 686 | * For 1.2 matches, returns the padded serialized structure |
| 687 | * |
| 688 | * Note that FREE must be called on octets->data when processing of |
| 689 | * the object is complete. |
| 690 | */ |
| 691 | |
| 692 | int |
| 693 | of_match_serialize(of_version_t version, of_match_t *match, of_octets_t *octets) |
| 694 | { |
| 695 | int rv; |
| 696 | |
| 697 | switch (version) { |
| 698 | """) |
| 699 | for version in of_g.of_version_range: |
| 700 | out.write(""" |
| 701 | case %(ver_name)s: |
| 702 | { |
| 703 | of_match_v%(version)s_t *wire_match; |
| 704 | wire_match = of_match_v%(version)s_new(version); |
| 705 | if (wire_match == NULL) { |
| 706 | return OF_ERROR_RESOURCE; |
| 707 | } |
| 708 | if ((rv = of_match_to_wire_match_v%(version)s(match, wire_match)) < 0) { |
| 709 | of_match_v%(version)s_delete(wire_match); |
| 710 | return rv; |
| 711 | } |
Rich Lane | dc9bc7f | 2014-04-05 11:19:20 -0700 | [diff] [blame] | 712 | of_wire_buffer_grow(wire_match->wbuf, OF_MATCH_BYTES(wire_match->length)); |
| 713 | octets->bytes = wire_match->wbuf->current_bytes; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 714 | of_object_wire_buffer_steal((of_object_t *)wire_match, |
| 715 | &octets->data); |
| 716 | of_match_v%(version)s_delete(wire_match); |
| 717 | } |
| 718 | break; |
| 719 | """ % dict(version=version, ver_name=of_g.of_version_wire2name[version])) |
| 720 | out.write(""" |
| 721 | default: |
| 722 | return OF_ERROR_COMPAT; |
| 723 | } |
| 724 | |
| 725 | return OF_ERROR_NONE; |
| 726 | } |
| 727 | """) |
| 728 | |
| 729 | |
| 730 | def gen_deserialize(out): |
| 731 | out.write(""" |
| 732 | /** |
| 733 | * Deserialize a match structure according to the version passed |
| 734 | * @param version The version to use for deserialization protocol |
| 735 | * @param match Pointer to the structure to fill out |
| 736 | * @param octets Pointer to an octets object holding serial buffer |
| 737 | * |
| 738 | * Normally the octets object will point to a part of a wire buffer. |
| 739 | */ |
| 740 | |
| 741 | int |
| 742 | of_match_deserialize(of_version_t version, of_match_t *match, |
Rich Lane | e0b70cc | 2014-06-12 15:02:22 -0700 | [diff] [blame] | 743 | of_object_t *parent, int offset, int length) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 744 | { |
Rich Lane | e0b70cc | 2014-06-12 15:02:22 -0700 | [diff] [blame] | 745 | of_object_t obj; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 746 | |
| 747 | switch (version) { |
| 748 | """) |
| 749 | for version in of_g.of_version_range: |
| 750 | out.write(""" |
| 751 | case %(ver_name)s: |
Rich Lane | e0b70cc | 2014-06-12 15:02:22 -0700 | [diff] [blame] | 752 | of_match_v%(version)d_init(&obj, %(ver_name)s, length, 1); |
| 753 | of_object_attach(parent, &obj, offset, length); |
| 754 | OF_TRY(of_match_v%(version)d_to_match(&obj, match)); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 755 | break; |
| 756 | """ % dict(version=version, ver_name=of_g.of_version_wire2name[version])) |
| 757 | |
| 758 | out.write(""" |
| 759 | default: |
| 760 | return OF_ERROR_COMPAT; |
| 761 | } |
| 762 | |
| 763 | return OF_ERROR_NONE; |
| 764 | } |
| 765 | """) |
| 766 | |
| 767 | def gen_match_comp(out=sys.stdout): |
| 768 | """ |
| 769 | Generate match comparison functions |
| 770 | """ |
| 771 | out.write(""" |
| 772 | /** |
| 773 | * Determine "more specific" relationship between mac addrs |
| 774 | * @return true if v1 is equal to or more specific than v2 |
| 775 | * |
| 776 | * @todo Could be optimized |
| 777 | * |
| 778 | * Check: Every bit in v2 is set in v1; v1 may have add'l bits set. |
| 779 | * That is, return false if there is a bit set in v2 and not in v1. |
| 780 | */ |
| 781 | |
| 782 | static inline int |
| 783 | of_more_specific_ipv6(of_ipv6_t *v1, of_ipv6_t *v2) { |
| 784 | int idx; |
| 785 | |
| 786 | for (idx = 0; idx < OF_IPV6_BYTES; idx++) { |
| 787 | /* If there's a bit set in v2 that is clear in v1, return false */ |
| 788 | if (~v1->addr[idx] & v2->addr[idx]) { |
| 789 | return 0; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | return 1; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Boolean test if two values agree when restricted to a mask |
| 798 | */ |
| 799 | |
| 800 | static inline int |
| 801 | of_restricted_match_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, of_ipv6_t *mask) { |
| 802 | int idx; |
| 803 | |
| 804 | for (idx = 0; idx < OF_IPV6_BYTES; idx++) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 805 | if ((v1->addr[idx] & mask->addr[idx]) != |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 806 | (v2->addr[idx] & mask->addr[idx])) { |
| 807 | return 0; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return 1; |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Boolean test if two values "overlap" (agree on common masks) |
| 816 | */ |
| 817 | |
| 818 | static inline int |
| 819 | of_overlap_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, |
| 820 | of_ipv6_t *m1, of_ipv6_t *m2) { |
| 821 | int idx; |
| 822 | |
| 823 | for (idx = 0; idx < OF_IPV6_BYTES; idx++) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 824 | if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) != |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 825 | ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) { |
| 826 | return 0; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return 1; |
| 831 | } |
| 832 | |
| 833 | #define OF_MORE_SPECIFIC_IPV6(v1, v2) of_more_specific_ipv6((v1), (v2)) |
| 834 | |
| 835 | #define OF_RESTRICTED_MATCH_IPV6(v1, v2, mask) \\ |
| 836 | of_restricted_match_ipv6((v1), (v2), (mask)) |
| 837 | |
| 838 | #define OF_OVERLAP_IPV6(v1, v2, m1, m2) of_overlap_ipv6((v1), (v2), (m1), (m2)) |
| 839 | |
| 840 | /** |
| 841 | * Determine "more specific" relationship between mac addrs |
| 842 | * @return true if v1 is equal to or more specific than v2 |
| 843 | * |
| 844 | * @todo Could be optimized |
| 845 | * |
| 846 | * Check: Every bit in v2 is set in v1; v1 may have add'l bits set. |
| 847 | * That is, return false if there is a bit set in v2 and not in v1. |
| 848 | */ |
| 849 | static inline int |
| 850 | of_more_specific_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2) { |
| 851 | int idx; |
| 852 | |
| 853 | for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) { |
| 854 | /* If there's a bit set in v2 that is clear in v1, return false */ |
| 855 | if (~v1->addr[idx] & v2->addr[idx]) { |
| 856 | return 0; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | return 1; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Boolean test if two values agree when restricted to a mask |
| 865 | */ |
| 866 | static inline int |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 867 | 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] | 868 | of_mac_addr_t *mask) { |
| 869 | int idx; |
| 870 | |
| 871 | for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 872 | if ((v1->addr[idx] & mask->addr[idx]) != |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 873 | (v2->addr[idx] & mask->addr[idx])) { |
| 874 | return 0; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | return 1; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * Boolean test if two values "overlap" (agree on common masks) |
| 883 | */ |
| 884 | |
| 885 | static inline int |
| 886 | of_overlap_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2, |
| 887 | of_mac_addr_t *m1, of_mac_addr_t *m2) { |
| 888 | int idx; |
| 889 | |
| 890 | for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 891 | if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) != |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 892 | ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) { |
| 893 | return 0; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | return 1; |
| 898 | } |
| 899 | |
| 900 | #define OF_MORE_SPECIFIC_MAC_ADDR(v1, v2) of_more_specific_mac_addr((v1), (v2)) |
| 901 | |
| 902 | #define OF_RESTRICTED_MATCH_MAC_ADDR(v1, v2, mask) \\ |
| 903 | of_restricted_match_mac_addr((v1), (v2), (mask)) |
| 904 | |
| 905 | #define OF_OVERLAP_MAC_ADDR(v1, v2, m1, m2) \\ |
| 906 | of_overlap_mac_addr((v1), (v2), (m1), (m2)) |
| 907 | |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 908 | #define OF_MORE_SPECIFIC_BITMAP_128(v1, v2) \\ |
| 909 | (OF_MORE_SPECIFIC_INT((v1)->lo, (v2)->lo) && OF_MORE_SPECIFIC_INT((v1)->hi, (v2)->hi)) |
| 910 | |
| 911 | #define OF_RESTRICTED_MATCH_BITMAP_128(v1, v2, mask) \\ |
| 912 | (OF_RESTRICTED_MATCH_INT((v1)->lo, (v2)->lo, (mask)->lo) && OF_RESTRICTED_MATCH_INT((v1)->hi, (v2)->hi, (mask)->hi)) |
| 913 | |
| 914 | #define OF_OVERLAP_BITMAP_128(v1, v2, m1, m2) \\ |
| 915 | (OF_OVERLAP_INT((v1)->lo, (v2)->lo, (m1)->lo, (m2)->lo) && OF_OVERLAP_INT((v1)->hi, (v2)->hi, (m1)->hi, (m2)->hi)) |
| 916 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 917 | /** |
| 918 | * More-specific-than macro for integer types; see above |
| 919 | * @return true if v1 is equal to or more specific than v2 |
| 920 | * |
| 921 | * If there is a bit that is set in v2 and not in v1, return false. |
| 922 | */ |
| 923 | #define OF_MORE_SPECIFIC_INT(v1, v2) (!(~(v1) & (v2))) |
| 924 | |
| 925 | /** |
| 926 | * Boolean test if two values agree when restricted to a mask |
| 927 | */ |
| 928 | #define OF_RESTRICTED_MATCH_INT(v1, v2, mask) \\ |
| 929 | (((v1) & (mask)) == ((v2) & (mask))) |
| 930 | |
| 931 | |
| 932 | #define OF_OVERLAP_INT(v1, v2, m1, m2) \\ |
| 933 | ((((v1) & (m1)) & (m2)) == (((v2) & (m1)) & (m2))) |
| 934 | """) |
| 935 | |
| 936 | out.write(""" |
| 937 | /** |
| 938 | * Compare two match structures for exact equality |
| 939 | * |
| 940 | * We just do memcmp assuming structs were memset to 0 on init |
| 941 | */ |
| 942 | static inline int |
| 943 | of_match_eq(of_match_t *match1, of_match_t *match2) |
| 944 | { |
| 945 | return (MEMCMP(match1, match2, sizeof(of_match_t)) == 0); |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * Is the entry match more specific than (or equal to) the query match? |
| 950 | * @param entry Match expected to be more specific (subset of query) |
| 951 | * @param query Match expected to be less specific (superset of entry) |
| 952 | * @returns Boolean, see below |
| 953 | * |
| 954 | * The assumption is that a query is being done for a non-strict |
| 955 | * match against an entry in a table. The result is true if the |
| 956 | * entry match indicates a more specific (but compatible) flow space |
| 957 | * specification than that in the query match. This means that the |
| 958 | * values agree between the two where they overlap, and that each mask |
| 959 | * for the entry is more specific than that of the query. |
| 960 | * |
| 961 | * The query has the less specific mask (fewer mask bits) so it is |
| 962 | * used for the mask when checking values. |
| 963 | */ |
| 964 | |
| 965 | static inline int |
| 966 | of_match_more_specific(of_match_t *entry, of_match_t *query) |
| 967 | { |
| 968 | of_match_fields_t *q_m, *e_m; /* Short hand for masks, fields */ |
| 969 | of_match_fields_t *q_f, *e_f; |
| 970 | |
| 971 | q_m = &query->masks; |
| 972 | e_m = &entry->masks; |
| 973 | q_f = &query->fields; |
| 974 | e_f = &entry->fields; |
| 975 | """) |
| 976 | for key, entry in match.of_match_members.items(): |
| 977 | q_m = "&q_m->%s" % key |
| 978 | e_m = "&e_m->%s" % key |
| 979 | q_f = "&q_f->%s" % key |
| 980 | e_f = "&e_f->%s" % key |
| 981 | if entry["m_type"] == "of_ipv6_t": |
| 982 | comp = "OF_MORE_SPECIFIC_IPV6" |
| 983 | match_type = "OF_RESTRICTED_MATCH_IPV6" |
| 984 | elif entry["m_type"] == "of_mac_addr_t": |
| 985 | comp = "OF_MORE_SPECIFIC_MAC_ADDR" |
| 986 | match_type = "OF_RESTRICTED_MATCH_MAC_ADDR" |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 987 | elif entry["m_type"] == "of_bitmap_128_t": |
| 988 | comp = "OF_MORE_SPECIFIC_BITMAP_128" |
| 989 | match_type = "OF_RESTRICTED_MATCH_BITMAP_128" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 990 | else: # Integer |
| 991 | comp = "OF_MORE_SPECIFIC_INT" |
| 992 | match_type = "OF_RESTRICTED_MATCH_INT" |
| 993 | q_m = "q_m->%s" % key |
| 994 | e_m = "e_m->%s" % key |
| 995 | q_f = "q_f->%s" % key |
| 996 | e_f = "e_f->%s" % key |
| 997 | out.write(""" |
| 998 | /* Mask and values for %(key)s */ |
| 999 | if (!%(comp)s(%(e_m)s, %(q_m)s)) { |
| 1000 | return 0; |
| 1001 | } |
| 1002 | if (!%(match_type)s(%(e_f)s, %(q_f)s, |
| 1003 | %(q_m)s)) { |
| 1004 | return 0; |
| 1005 | } |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1006 | """ % 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] | 1007 | q_m=q_m, e_m=e_m, key=key)) |
| 1008 | |
| 1009 | out.write(""" |
| 1010 | return 1; |
| 1011 | } |
| 1012 | """) |
| 1013 | |
| 1014 | out.write(""" |
| 1015 | |
| 1016 | /** |
| 1017 | * Do two entries overlap? |
| 1018 | * @param match1 One match struct |
| 1019 | * @param match2 Another match struct |
| 1020 | * @returns Boolean: true if there is a packet that would match both |
| 1021 | * |
| 1022 | */ |
| 1023 | |
| 1024 | static inline int |
| 1025 | of_match_overlap(of_match_t *match1, of_match_t *match2) |
| 1026 | { |
| 1027 | of_match_fields_t *m1, *m2; /* Short hand for masks, fields */ |
| 1028 | of_match_fields_t *f1, *f2; |
| 1029 | |
| 1030 | m1 = &match1->masks; |
| 1031 | m2 = &match2->masks; |
| 1032 | f1 = &match1->fields; |
| 1033 | f2 = &match2->fields; |
| 1034 | """) |
| 1035 | for key, entry in match.of_match_members.items(): |
| 1036 | m1 = "&m1->%s" % key |
| 1037 | m2 = "&m2->%s" % key |
| 1038 | f1 = "&f1->%s" % key |
| 1039 | f2 = "&f2->%s" % key |
| 1040 | if entry["m_type"] == "of_ipv6_t": |
| 1041 | check = "OF_OVERLAP_IPV6" |
| 1042 | elif entry["m_type"] == "of_mac_addr_t": |
| 1043 | check = "OF_OVERLAP_MAC_ADDR" |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 1044 | elif entry["m_type"] == "of_bitmap_128_t": |
| 1045 | check = "OF_OVERLAP_BITMAP_128" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1046 | else: # Integer |
| 1047 | check = "OF_OVERLAP_INT" |
| 1048 | m1 = "m1->%s" % key |
| 1049 | m2 = "m2->%s" % key |
| 1050 | f1 = "f1->%s" % key |
| 1051 | f2 = "f2->%s" % key |
| 1052 | out.write(""" |
| 1053 | /* Check overlap for %(key)s */ |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1054 | if (!%(check)s(%(f1)s, %(f2)s, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1055 | %(m2)s, %(m1)s)) { |
| 1056 | return 0; /* This field differentiates; all done */ |
| 1057 | } |
| 1058 | """ % dict(check=check, f1=f1, f2=f2, m1=m1, m2=m2, key=key)) |
| 1059 | |
| 1060 | out.write(""" |
| 1061 | return 1; /* No field differentiates matches */ |
| 1062 | } |
| 1063 | """) |
| 1064 | |
| 1065 | def gen_match_conversions(out=sys.stdout): |
| 1066 | match.match_sanity_check() |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1067 | out.write(""" |
| 1068 | /** |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1069 | * @brief Return the index (used as the WC field in 1.0 match) given the mask |
| 1070 | */ |
| 1071 | |
| 1072 | int |
| 1073 | of_ip_mask_to_index(uint32_t mask) |
| 1074 | { |
| 1075 | int idx; |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 1076 | uint32_t cmask; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1077 | |
| 1078 | /* Handle most common cases directly */ |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 1079 | if (mask == 0) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1080 | return 63; |
| 1081 | } |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 1082 | if (mask == 0xffffffff) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1083 | return 0; |
| 1084 | } |
| 1085 | |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 1086 | if ((~mask + 1) & ~mask) { |
| 1087 | LOCI_LOG_INFO("OF 1.0: Could not map IP addr mask 0x%x", mask); |
| 1088 | return 63; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 1091 | idx = 0; |
| 1092 | cmask = ~mask; |
| 1093 | while (cmask) { |
| 1094 | cmask >>= 1; |
| 1095 | idx += 1; |
| 1096 | } |
| 1097 | |
| 1098 | return idx; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * @brief Return the mask for the given index |
| 1103 | */ |
| 1104 | |
| 1105 | uint32_t |
| 1106 | of_ip_index_to_mask(int index) |
| 1107 | { |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 1108 | if (index >= 32) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1109 | return 0; |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 1110 | } else { |
| 1111 | return 0xffffffff << index; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1112 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | """) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1116 | gen_unified_match_to_v1(out) |
| 1117 | gen_unified_match_to_v2(out) |
| 1118 | gen_unified_match_to_v3(out) |
| 1119 | gen_v1_to_unified_match(out) |
| 1120 | gen_v2_to_unified_match(out) |
| 1121 | gen_v3_to_unified_match(out) |