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