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