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