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