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