blob: ccc503d8b072bcdc9a9a834f2fc77e494e85853a [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,
Rich Lanee0b70cc2014-06-12 15:02:22 -070080 of_object_t *parent, int offset, int length);
Rich Lanea06d0c32013-03-25 08:52:03 -070081extern 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
Rich Lane398bc9e2014-10-14 11:47:56 -0700107
108#define of_match_v5_t of_match_v3_t
109#define of_match_v5_init of_match_v3_init
110#define of_match_v5_new of_match_v3_new
111#define of_match_v5_to_match of_match_v3_to_match
112#define of_match_to_wire_match_v5 of_match_to_wire_match_v3
113#define of_match_v5_delete of_match_v3_delete
Rich Lanea06d0c32013-03-25 08:52:03 -0700114""")
115
116def gen_match_macros(out):
117 out.write("""
118
119/**
120 * Definitions for wildcard macros for OF_VERSION_1_0
121 */
122
123""")
124 for key in match.of_v1_keys:
125 entry = match.of_match_members[key]
126 if "v1_wc_shift" in entry:
127 if key in ["ipv4_src", "ipv4_dst"]:
128 out.write("""
129#define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d
130#define OF_MATCH_V1_WC_%(ku)s_MASK (0x3f << %(val)d)
131#define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(0x3f << %(val)d))
132#define OF_MATCH_V1_WC_%(ku)s_SET(wc, value) do { \\
133 OF_MATCH_V1_WC_%(ku)s_CLEAR(wc); \\
134 ((wc) |= (((value) & 0x3f) << %(val)d)); \\
135 } while (0)
136#define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (0x3f << %(val)d))
137#define OF_MATCH_V1_WC_%(ku)s_GET(wc) (((wc) >> %(val)d) & 0x3f)
138""" % dict(ku=key.upper(), val=entry["v1_wc_shift"]))
139 else:
140 out.write("""
141#define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d
142#define OF_MATCH_V1_WC_%(ku)s_MASK (1 << %(val)d)
143#define OF_MATCH_V1_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d))
144#define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d))
145#define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d))
146""" % dict(ku=key.upper(), val=entry["v1_wc_shift"]))
147
148 out.write("""
149
150/**
151 * Definitions for wildcard macros for OF_VERSION_1_1
152 */
153""")
154
155 for key in sorted(match.of_v2_keys):
156 entry = match.of_match_members[key]
157 if "v2_wc_shift" in entry:
158 out.write("""
159#define OF_MATCH_V2_WC_%(ku)s_SHIFT %(val)d
160#define OF_MATCH_V2_WC_%(ku)s_MASK (1 << %(val)d)
161#define OF_MATCH_V2_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d))
162#define OF_MATCH_V2_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d))
163#define OF_MATCH_V2_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d))
164""" % dict(ku=key.upper(), val=entry["v2_wc_shift"]))
165
166
167def gen_match_struct(out=sys.stdout):
168 out.write("/* Unified, flat OpenFlow match structure based on OF 1.2 */\n")
169 out.write("typedef struct of_match_fields_s {\n")
170 out.write(" /* Version 1.2 is used for field names */\n")
171 for name in match.match_keys_sorted:
172 entry = match.of_match_members[name]
173 out.write(" %-20s %s;\n" % (entry["m_type"], entry["name"]))
174 out.write("""
175} of_match_fields_t;
176
177/**
178 * @brief The LOCI match structure.
179 */
180
181typedef struct of_match_s {
182 of_version_t version;
183 of_match_fields_t fields;
184 of_match_fields_t masks;
185} of_match_t;
186
Rich Lane68798a52014-04-16 14:57:52 -0700187/*
188 * AND 'len' bytes starting from 'value' with the corresponding byte in
189 * 'mask'.
Dan Talaycofb50d382013-08-05 16:00:17 -0700190 */
Rich Laned5f23452014-04-03 01:16:37 -0700191static inline void
Rich Lane68798a52014-04-16 14:57:52 -0700192of_memmask(void *value, const void *mask, size_t len)
Dan Talaycofb50d382013-08-05 16:00:17 -0700193{
Rich Lane68798a52014-04-16 14:57:52 -0700194 int i;
195 uint8_t *v = value;
196 const uint8_t *m = mask;
Dan Talaycofb50d382013-08-05 16:00:17 -0700197
Rich Lane68798a52014-04-16 14:57:52 -0700198 for (i = 0; i < len; i++) {
199 v[i] &= m[i];
Dan Talaycofb50d382013-08-05 16:00:17 -0700200 }
201}
202
203/**
Rich Lane70c70942014-05-06 15:31:47 -0700204 * @brief Map from mask to OF 1.0 wildcard
Rich Lanea06d0c32013-03-25 08:52:03 -0700205 */
206
207extern int of_ip_mask_to_index(uint32_t mask);
208
209/**
Rich Lane70c70942014-05-06 15:31:47 -0700210 * @brief Map from OF 1.0 wildcard to mask
Rich Lanea06d0c32013-03-25 08:52:03 -0700211 */
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("""
Rich Lanea06d0c32013-03-25 08:52:03 -0700236/*
237 * The generic match structure uses the OXM bit indices for it's
238 * bitmasks for active and masked values
239 */
240""")
241 for key, entry in match.of_match_members.items():
242 out.write("""
243/* Mask/value check/set macros for %(key)s */
244
245/**
246 * Set the mask for an exact match of %(key)s
247 */
248#define OF_MATCH_MASK_%(ku)s_EXACT_SET(_match) \\
249 MEMSET(&(_match)->masks.%(key)s, 0xff, \\
250 sizeof(((_match)->masks).%(key)s))
251
252/**
253 * Clear the mask for %(key)s making that field inactive for the match
254 */
255#define OF_MATCH_MASK_%(ku)s_CLEAR(_match) \\
256 MEMSET(&(_match)->masks.%(key)s, 0, \\
257 sizeof(((_match)->masks).%(key)s))
258
259/**
260 * Test whether the match is exact for %(key)s
261 */
262#define OF_MATCH_MASK_%(ku)s_EXACT_TEST(_match) \\
263 OF_VARIABLE_IS_ALL_ONES(&(((_match)->masks).%(key)s))
264
265/**
266 * Test whether key %(key)s is being checked in the match
267 */
268#define OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(_match) \\
269 OF_VARIABLE_IS_NON_ZERO(&(((_match)->masks).%(key)s))
270
Rich Laned56f8d22014-05-06 14:52:55 -0700271""" % dict(key=key, ku=key.upper()))
Rich Lanea06d0c32013-03-25 08:52:03 -0700272
Rich Lanea06d0c32013-03-25 08:52:03 -0700273def gen_unified_match_to_v1(out):
274 """
275 Generate C code to convert a unified match structure to a V1 match struct
276 @param out The output file handle
277 """
278
279 out.write("""
280/**
281 * Check if match is compatible with OF 1.0
282 * @param match The match being checked
283 */
284static inline int
285of_match_v1_compat_check(of_match_t *match)
286{
287""")
288 for key in match.of_match_members:
289 if key in match.of_v1_keys:
290 continue
291 out.write("""
292 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
293 return 0;
294 }
295""" % dict(ku=key.upper()))
296
297 out.write("""
298 return 1;
299}
300""")
301
302 out.write("""
303/**
304 * Convert a generic match object to an OF_VERSION_1_0 object
305 * @param src Pointer to the generic match object source
306 * @param dst Pointer to the OF 1.0 wire structure
307 *
308 * The wire structure is initialized by this function if it doesn't
309 * not have the proper object ID.
310 */
311
312int
313of_match_to_wire_match_v1(of_match_t *src, of_match_v1_t *dst)
314{
315 of_wc_bmap_t wildcards = 0;
316 int ip_mask_index;
317
318 if ((src == NULL) || (dst == NULL)) {
319 return OF_ERROR_PARAM;
320 }
321 if (!of_match_v1_compat_check(src)) {
322 return OF_ERROR_COMPAT;
323 }
324 if (dst->object_id != OF_MATCH_V1) {
325 of_match_v1_init(dst, OF_VERSION_1_0, 0, 0);
326 }
327""")
328 for key in sorted(match.of_v1_keys):
329 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
330 out.write("""
331 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
332 ip_mask_index = of_ip_mask_to_index(src->masks.%(key)s);
333 of_match_v1_%(key)s_set(dst, src->fields.%(key)s);
334 } else { /* Wildcarded, look for 0 mask */
335 ip_mask_index = of_ip_mask_to_index(0);
336 }
337 OF_MATCH_V1_WC_%(ku)s_SET(wildcards, ip_mask_index);
338""" % dict(key=key, ku=key.upper()))
339 else:
340 out.write("""
341 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
342 of_match_v1_%(key)s_set(dst, src->fields.%(key)s);
343 } else {
344 OF_MATCH_V1_WC_%(ku)s_SET(wildcards);
345 }
346""" % dict(key=key, ku=key.upper()))
347
348 out.write("""
349 of_match_v1_wildcards_set(dst, wildcards);
350
351 return OF_ERROR_NONE;
352}
353""")
354
355def all_ones_mask(d_type):
356 if d_type == "of_mac_addr_t":
357 return "of_mac_addr_all_ones"
358 else:
359 return "((%s) -1)" % d_type
360
361def gen_unified_match_to_v2(out):
362 """
363 Generate C code to convert a unified match structure to a V2 match struct
364 @param out The output file handle
365 """
366
367 out.write("""
368/**
369 * Check if match is compatible with OF 1.0
370 * @param match The match being checked
371 */
372static inline int
373of_match_v2_compat_check(of_match_t *match)
374{
375""")
376 for key in match.of_match_members:
377 if key in match.of_v2_keys:
378 continue
379 out.write("""
380 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
381 return 0;
382 }
383""" % dict(ku=key.upper()))
384
385 out.write("""
386 return 1;
387}
388""")
389
390 out.write("""
391/**
392 * Convert a generic match object to an OF_VERSION_1_1 object
393 * @param src Pointer to the generic match object source
394 * @param dst Pointer to the OF 1.1 wire structure
395 *
396 * The wire structure is initialized by this function.
397 */
398
399int
400of_match_to_wire_match_v2(of_match_t *src, of_match_v2_t *dst)
401{
402 of_wc_bmap_t wildcards = 0;
403
404 if ((src == NULL) || (dst == NULL)) {
405 return OF_ERROR_PARAM;
406 }
407 if (!of_match_v2_compat_check(src)) {
408 return OF_ERROR_COMPAT;
409 }
410 if (dst->object_id != OF_MATCH_V2) {
411 of_match_v2_init(dst, OF_VERSION_1_1, 0, 0);
412 }
413""")
414 for key in match.of_v2_keys:
415 if key in match.of_v2_full_mask:
416 ones_mask = all_ones_mask(match.of_match_members[key]["m_type"])
417 out.write("""
418 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
419 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
420 of_match_v2_%(key)s_mask_set(dst,
421 src->masks.%(key)s);
422 } else { /* Exact match; use all ones mask */
423 of_match_v2_%(key)s_mask_set(dst,
424 %(ones_mask)s);
425 }
426 of_match_v2_%(key)s_set(dst, src->fields.%(key)s);
427 }
428
429""" % dict(key=key, ku=key.upper(), ones_mask=ones_mask))
430 else:
431 out.write("""
432 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
433 return OF_ERROR_COMPAT;
434 }
435 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
436 of_match_v2_%(key)s_set(dst, src->fields.%(key)s);
437 } else {
438 OF_MATCH_V2_WC_%(ku)s_SET(wildcards);
439 }
440""" % dict(key=key, ku=key.upper(),
441 wc_bit="OF_MATCH_WC_V2_%s" % key.upper()))
442
443 out.write("""
444 of_match_v2_wildcards_set(dst, wildcards);
445
446 return OF_ERROR_NONE;
447}
448""")
449
450def gen_unified_match_to_v3(out):
451 """
452 Generate C code to convert a unified match structure to a V3 match
453
454 This is much easier as the unified struct is based on V3
455 @param out The output file handle
456 """
457 out.write("""
458static int
459populate_oxm_list(of_match_t *src, of_list_oxm_t *oxm_list)
460{
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700461 of_object_t elt;
Rich Lanea06d0c32013-03-25 08:52:03 -0700462
463 /* For each active member, add an OXM entry to the list */
464""")
Rich Lane4964d542013-10-14 18:13:47 -0700465 for key in match.match_keys_sorted:
Rich Lanea06d0c32013-03-25 08:52:03 -0700466 out.write("""\
467 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
468 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700469 of_oxm_%(key)s_masked_init(&elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700470 oxm_list->version, -1, 1);
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700471 of_list_oxm_append_bind(oxm_list, &elt);
472 of_oxm_%(key)s_masked_value_set(&elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700473 src->fields.%(key)s);
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700474 of_oxm_%(key)s_masked_value_mask_set(&elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700475 src->masks.%(key)s);
476 } else { /* Active, but not masked */
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700477 of_oxm_%(key)s_init(&elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700478 oxm_list->version, -1, 1);
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700479 of_list_oxm_append_bind(oxm_list, &elt);
480 of_oxm_%(key)s_value_set(&elt, src->fields.%(key)s);
Rich Lanea06d0c32013-03-25 08:52:03 -0700481 }
482 }
483""" % dict(key=key, ku=key.upper()))
484 out.write("""
485 return OF_ERROR_NONE;
486}
487
488/**
489 * Convert a generic match object to an OF_VERSION_1_2 object
490 * @param src Pointer to the generic match object source
491 * @param dst Pointer to the OF 1.2 wire structure
492 *
493 * The wire structure is initialized by this function if the object
494 * id is not correct in the object
495 */
496
497int
498of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst)
499{
500 int rv = OF_ERROR_NONE;
501 of_list_oxm_t *oxm_list;
502
503 if ((src == NULL) || (dst == NULL)) {
504 return OF_ERROR_PARAM;
505 }
506 if (dst->object_id != OF_MATCH_V3) {
Rich Lanecfd4ce02013-07-12 16:37:14 -0700507 of_match_v3_init(dst, OF_VERSION_1_2, 0, 0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700508 }
Rich Lanecfd4ce02013-07-12 16:37:14 -0700509 if ((oxm_list = of_list_oxm_new(dst->version)) == NULL) {
Rich Lanea06d0c32013-03-25 08:52:03 -0700510 return OF_ERROR_RESOURCE;
511 }
512
513 rv = populate_oxm_list(src, oxm_list);
514
515 if (rv == OF_ERROR_NONE) {
516 rv = of_match_v3_oxm_list_set(dst, oxm_list);
517 }
518
519 of_list_oxm_delete(oxm_list);
520
521 return rv;
522}
523""")
524
525def gen_v1_to_unified_match(out):
526 """
527 Generate the code that maps a v1 wire format match object
528 to a unified match object
529 """
530 # for each v1 member, if not in wildcards
531 # translate to unified. Treat nw_src/dst specially
532 out.write("""
533
534/**
535 * Convert an OF_VERSION_1_0 object to a generic match object
536 * @param src Pointer to the OF 1.0 wire structure source
537 * @param dst Pointer to the generic match object destination
538 *
539 * The wire structure is initialized by this function.
540 */
541
542int
543of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst)
544{
545 of_wc_bmap_t wc;
546 int count;
547
548 MEMSET(dst, 0, sizeof(*dst));
549 dst->version = src->version;
550
551 of_match_v1_wildcards_get(src, &wc);
552""")
Rich Lanea06d0c32013-03-25 08:52:03 -0700553 for key in sorted(match.of_v1_keys):
554 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
555 out.write("""
556 count = OF_MATCH_V1_WC_%(ku)s_GET(wc);
557 dst->masks.%(key)s = of_ip_index_to_mask(count);
Rich Lanea06d0c32013-03-25 08:52:03 -0700558 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
Dan Talaycofb50d382013-08-05 16:00:17 -0700559 /* Clear the bits not indicated by mask; IP addrs are special for 1.0 */
560 dst->fields.%(key)s &= dst->masks.%(key)s;
Rich Lanea06d0c32013-03-25 08:52:03 -0700561""" % dict(ku=key.upper(), key=key))
562 else:
563 out.write("""
564 if (!(OF_MATCH_V1_WC_%(ku)s_TEST(wc))) {
565 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
566 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
567 }
568""" % dict(ku=key.upper(), key=key))
569
570 out.write("""
571 return OF_ERROR_NONE;
572}
573""")
574
575def gen_v2_to_unified_match(out):
576 """
577 Generate the code that maps a v2 wire format match object
578 to a unified match object
579 """
580 out.write("""
581int
582of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst)
583{
584 of_wc_bmap_t wc;
585
586 MEMSET(dst, 0, sizeof(*dst));
587 dst->version = src->version;
588
589 of_match_v2_wildcards_get(src, &wc);
590""")
591 for key in match.of_v2_keys:
592 if key in match.of_v2_full_mask:
593 out.write("""
594 of_match_v2_%(key)s_mask_get(src, &dst->masks.%(key)s);
595 if (OF_VARIABLE_IS_NON_ZERO(&dst->masks.%(key)s)) { /* Matching something */
596 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
597 }
Rich Lane054a8182014-04-16 14:42:41 -0700598 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700599""" % dict(ku=key.upper(), key=key))
600 else:
601 out.write("""
602 if (!(OF_MATCH_V2_WC_%(ku)s_TEST(wc))) {
603 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
604 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
605 }
606""" % dict(ku=key.upper(), key=key))
607
608 out.write("""
Dan Talaycofb50d382013-08-05 16:00:17 -0700609
Rich Lanea06d0c32013-03-25 08:52:03 -0700610 return OF_ERROR_NONE;
611}
612""")
613
614
615def gen_v3_to_unified_match(out):
616 """
617 Generate the code that maps a v3 wire format match object
618 to a unified match object
619 """
620 # Iterate thru the OXM list members
621 out.write("""
622int
623of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst)
624{
625 int rv;
626 of_list_oxm_t oxm_list;
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700627 of_object_t oxm_entry;
Rich Lanea06d0c32013-03-25 08:52:03 -0700628""")
629# for key in match.of_match_members:
630# out.write(" of_oxm_%s_t *%s;\n" % (key, key))
631# out.write(" of_oxm_%s_masked_t *%s_masked;\n" % (key, key))
632
633 out.write("""
634 MEMSET(dst, 0, sizeof(*dst));
635 dst->version = src->version;
636
637 of_match_v3_oxm_list_bind(src, &oxm_list);
638 rv = of_list_oxm_first(&oxm_list, &oxm_entry);
639
640 while (rv == OF_ERROR_NONE) {
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700641 switch (oxm_entry.object_id) { /* What kind of entry is this */
Rich Lanea06d0c32013-03-25 08:52:03 -0700642""")
643 for key in match.of_match_members:
644 out.write("""
645 case OF_OXM_%(ku)s_MASKED:
646 of_oxm_%(key)s_masked_value_mask_get(
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700647 &oxm_entry,
Rich Lanea06d0c32013-03-25 08:52:03 -0700648 &dst->masks.%(key)s);
649 of_oxm_%(key)s_masked_value_get(
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700650 &oxm_entry,
Rich Lanea06d0c32013-03-25 08:52:03 -0700651 &dst->fields.%(key)s);
Rich Laned5f23452014-04-03 01:16:37 -0700652 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700653 break;
654 case OF_OXM_%(ku)s:
655 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
656 of_oxm_%(key)s_value_get(
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700657 &oxm_entry,
Rich Lanea06d0c32013-03-25 08:52:03 -0700658 &dst->fields.%(key)s);
659 break;
660""" % (dict(ku=key.upper(), key=key)))
661
662 out.write("""
663 default:
664 /* @fixme Add debug statement */
665 return OF_ERROR_PARSE;
666 } /* end switch */
667 rv = of_list_oxm_next(&oxm_list, &oxm_entry);
668 } /* end OXM iteration */
669
670 return OF_ERROR_NONE;
671}
672""")
673
674def gen_serialize(out):
675 out.write("""
676/**
677 * Serialize a match structure according to the version passed
678 * @param version The version to use for serialization protocol
679 * @param match Pointer to the structure to serialize
680 * @param octets Pointer to an octets object to fill out
681 *
682 * A buffer is allocated using normal internal ALLOC/FREE semantics
683 * and pointed to by the octets object. The length of the resulting
684 * serialization is in octets->bytes.
685 *
686 * For 1.2 matches, returns the padded serialized structure
687 *
688 * Note that FREE must be called on octets->data when processing of
689 * the object is complete.
690 */
691
692int
693of_match_serialize(of_version_t version, of_match_t *match, of_octets_t *octets)
694{
695 int rv;
696
697 switch (version) {
698""")
699 for version in of_g.of_version_range:
700 out.write("""
701 case %(ver_name)s:
702 {
703 of_match_v%(version)s_t *wire_match;
704 wire_match = of_match_v%(version)s_new(version);
705 if (wire_match == NULL) {
706 return OF_ERROR_RESOURCE;
707 }
708 if ((rv = of_match_to_wire_match_v%(version)s(match, wire_match)) < 0) {
709 of_match_v%(version)s_delete(wire_match);
710 return rv;
711 }
Rich Lanedc9bc7f2014-04-05 11:19:20 -0700712 of_wire_buffer_grow(wire_match->wbuf, OF_MATCH_BYTES(wire_match->length));
713 octets->bytes = wire_match->wbuf->current_bytes;
Rich Lanea06d0c32013-03-25 08:52:03 -0700714 of_object_wire_buffer_steal((of_object_t *)wire_match,
715 &octets->data);
716 of_match_v%(version)s_delete(wire_match);
717 }
718 break;
719""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
720 out.write("""
721 default:
722 return OF_ERROR_COMPAT;
723 }
724
725 return OF_ERROR_NONE;
726}
727""")
728
729
730def gen_deserialize(out):
731 out.write("""
732/**
733 * Deserialize a match structure according to the version passed
734 * @param version The version to use for deserialization protocol
735 * @param match Pointer to the structure to fill out
736 * @param octets Pointer to an octets object holding serial buffer
737 *
738 * Normally the octets object will point to a part of a wire buffer.
739 */
740
741int
742of_match_deserialize(of_version_t version, of_match_t *match,
Rich Lanee0b70cc2014-06-12 15:02:22 -0700743 of_object_t *parent, int offset, int length)
Rich Lanea06d0c32013-03-25 08:52:03 -0700744{
Rich Lanee0b70cc2014-06-12 15:02:22 -0700745 of_object_t obj;
Rich Lanea06d0c32013-03-25 08:52:03 -0700746
747 switch (version) {
748""")
749 for version in of_g.of_version_range:
750 out.write("""
751 case %(ver_name)s:
Rich Lanee0b70cc2014-06-12 15:02:22 -0700752 of_match_v%(version)d_init(&obj, %(ver_name)s, length, 1);
753 of_object_attach(parent, &obj, offset, length);
754 OF_TRY(of_match_v%(version)d_to_match(&obj, match));
Rich Lanea06d0c32013-03-25 08:52:03 -0700755 break;
756""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
757
758 out.write("""
759 default:
760 return OF_ERROR_COMPAT;
761 }
762
763 return OF_ERROR_NONE;
764}
765""")
766
767def gen_match_comp(out=sys.stdout):
768 """
769 Generate match comparison functions
770 """
771 out.write("""
772/**
773 * Determine "more specific" relationship between mac addrs
774 * @return true if v1 is equal to or more specific than v2
775 *
776 * @todo Could be optimized
777 *
778 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
779 * That is, return false if there is a bit set in v2 and not in v1.
780 */
781
782static inline int
783of_more_specific_ipv6(of_ipv6_t *v1, of_ipv6_t *v2) {
784 int idx;
785
786 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
787 /* If there's a bit set in v2 that is clear in v1, return false */
788 if (~v1->addr[idx] & v2->addr[idx]) {
789 return 0;
790 }
791 }
792
793 return 1;
794}
795
796/**
797 * Boolean test if two values agree when restricted to a mask
798 */
799
800static inline int
801of_restricted_match_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, of_ipv6_t *mask) {
802 int idx;
803
804 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700805 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700806 (v2->addr[idx] & mask->addr[idx])) {
807 return 0;
808 }
809 }
810
811 return 1;
812}
813
814/**
815 * Boolean test if two values "overlap" (agree on common masks)
816 */
817
818static inline int
819of_overlap_ipv6(of_ipv6_t *v1, of_ipv6_t *v2,
820 of_ipv6_t *m1, of_ipv6_t *m2) {
821 int idx;
822
823 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700824 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700825 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
826 return 0;
827 }
828 }
829
830 return 1;
831}
832
833#define OF_MORE_SPECIFIC_IPV6(v1, v2) of_more_specific_ipv6((v1), (v2))
834
835#define OF_RESTRICTED_MATCH_IPV6(v1, v2, mask) \\
836 of_restricted_match_ipv6((v1), (v2), (mask))
837
838#define OF_OVERLAP_IPV6(v1, v2, m1, m2) of_overlap_ipv6((v1), (v2), (m1), (m2))
839
840/**
841 * Determine "more specific" relationship between mac addrs
842 * @return true if v1 is equal to or more specific than v2
843 *
844 * @todo Could be optimized
845 *
846 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
847 * That is, return false if there is a bit set in v2 and not in v1.
848 */
849static inline int
850of_more_specific_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2) {
851 int idx;
852
853 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
854 /* If there's a bit set in v2 that is clear in v1, return false */
855 if (~v1->addr[idx] & v2->addr[idx]) {
856 return 0;
857 }
858 }
859
860 return 1;
861}
862
863/**
864 * Boolean test if two values agree when restricted to a mask
865 */
866static inline int
Andreas Wundsam53256162013-05-02 14:05:53 -0700867of_restricted_match_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
Rich Lanea06d0c32013-03-25 08:52:03 -0700868 of_mac_addr_t *mask) {
869 int idx;
870
871 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700872 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700873 (v2->addr[idx] & mask->addr[idx])) {
874 return 0;
875 }
876 }
877
878 return 1;
879}
880
881/**
882 * Boolean test if two values "overlap" (agree on common masks)
883 */
884
885static inline int
886of_overlap_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
887 of_mac_addr_t *m1, of_mac_addr_t *m2) {
888 int idx;
889
890 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700891 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700892 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
893 return 0;
894 }
895 }
896
897 return 1;
898}
899
900#define OF_MORE_SPECIFIC_MAC_ADDR(v1, v2) of_more_specific_mac_addr((v1), (v2))
901
902#define OF_RESTRICTED_MATCH_MAC_ADDR(v1, v2, mask) \\
903 of_restricted_match_mac_addr((v1), (v2), (mask))
904
905#define OF_OVERLAP_MAC_ADDR(v1, v2, m1, m2) \\
906 of_overlap_mac_addr((v1), (v2), (m1), (m2))
907
Rich Lane3b2fd832013-09-24 13:44:08 -0700908#define OF_MORE_SPECIFIC_BITMAP_128(v1, v2) \\
909 (OF_MORE_SPECIFIC_INT((v1)->lo, (v2)->lo) && OF_MORE_SPECIFIC_INT((v1)->hi, (v2)->hi))
910
911#define OF_RESTRICTED_MATCH_BITMAP_128(v1, v2, mask) \\
912 (OF_RESTRICTED_MATCH_INT((v1)->lo, (v2)->lo, (mask)->lo) && OF_RESTRICTED_MATCH_INT((v1)->hi, (v2)->hi, (mask)->hi))
913
914#define OF_OVERLAP_BITMAP_128(v1, v2, m1, m2) \\
915 (OF_OVERLAP_INT((v1)->lo, (v2)->lo, (m1)->lo, (m2)->lo) && OF_OVERLAP_INT((v1)->hi, (v2)->hi, (m1)->hi, (m2)->hi))
916
Rich Lanea06d0c32013-03-25 08:52:03 -0700917/**
918 * More-specific-than macro for integer types; see above
919 * @return true if v1 is equal to or more specific than v2
920 *
921 * If there is a bit that is set in v2 and not in v1, return false.
922 */
923#define OF_MORE_SPECIFIC_INT(v1, v2) (!(~(v1) & (v2)))
924
925/**
926 * Boolean test if two values agree when restricted to a mask
927 */
928#define OF_RESTRICTED_MATCH_INT(v1, v2, mask) \\
929 (((v1) & (mask)) == ((v2) & (mask)))
930
931
932#define OF_OVERLAP_INT(v1, v2, m1, m2) \\
933 ((((v1) & (m1)) & (m2)) == (((v2) & (m1)) & (m2)))
934""")
935
936 out.write("""
937/**
938 * Compare two match structures for exact equality
939 *
940 * We just do memcmp assuming structs were memset to 0 on init
941 */
942static inline int
943of_match_eq(of_match_t *match1, of_match_t *match2)
944{
945 return (MEMCMP(match1, match2, sizeof(of_match_t)) == 0);
946}
947
948/**
949 * Is the entry match more specific than (or equal to) the query match?
950 * @param entry Match expected to be more specific (subset of query)
951 * @param query Match expected to be less specific (superset of entry)
952 * @returns Boolean, see below
953 *
954 * The assumption is that a query is being done for a non-strict
955 * match against an entry in a table. The result is true if the
956 * entry match indicates a more specific (but compatible) flow space
957 * specification than that in the query match. This means that the
958 * values agree between the two where they overlap, and that each mask
959 * for the entry is more specific than that of the query.
960 *
961 * The query has the less specific mask (fewer mask bits) so it is
962 * used for the mask when checking values.
963 */
964
965static inline int
Rich Lanec4893612014-12-01 13:41:49 -0800966of_match_more_specific(const of_match_t *entry, const of_match_t *query)
Rich Lanea06d0c32013-03-25 08:52:03 -0700967{
Rich Lanec4893612014-12-01 13:41:49 -0800968 LOCI_ASSERT(sizeof(of_match_fields_t) % sizeof(uint8_t) == 0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700969
Rich Lanec4893612014-12-01 13:41:49 -0800970 /* Short hand for masks, fields */
971 const uint8_t *qm = (const uint8_t *)&query->masks;
972 const uint8_t *em = (const uint8_t *)&entry->masks;
973 const uint8_t *qf = (const uint8_t *)&query->fields;
974 const uint8_t *ef = (const uint8_t *)&entry->fields;
Rich Lanea06d0c32013-03-25 08:52:03 -0700975
Rich Lanec4893612014-12-01 13:41:49 -0800976 int i;
977 for (i = 0; i < sizeof(of_match_fields_t)/sizeof(uint8_t); i++) {
978 if (qm[i] & ~em[i]) {
979 /* Query mask has a bit set that isn't set in the entry mask */
980 return 0;
981 }
982
983 if ((qf[i] ^ ef[i]) & qm[i]) {
984 /* Query and entry disagree on a field bit */
985 return 0;
986 }
987 }
988
Rich Lanea06d0c32013-03-25 08:52:03 -0700989 return 1;
990}
Rich Lanea06d0c32013-03-25 08:52:03 -0700991
992/**
993 * Do two entries overlap?
994 * @param match1 One match struct
995 * @param match2 Another match struct
996 * @returns Boolean: true if there is a packet that would match both
997 *
998 */
999
1000static inline int
Rich Lanec4893612014-12-01 13:41:49 -08001001of_match_overlap(const of_match_t *match1, const of_match_t *match2)
Rich Lanea06d0c32013-03-25 08:52:03 -07001002{
Rich Lanec4893612014-12-01 13:41:49 -08001003 LOCI_ASSERT(sizeof(of_match_fields_t) % sizeof(uint8_t) == 0);
Rich Lanea06d0c32013-03-25 08:52:03 -07001004
Rich Lanec4893612014-12-01 13:41:49 -08001005 /* Short hand for masks, fields */
1006 const uint8_t *m1 = (const uint8_t *)&match1->masks;
1007 const uint8_t *m2 = (const uint8_t *)&match2->masks;
1008 const uint8_t *f1 = (const uint8_t *)&match1->fields;
1009 const uint8_t *f2 = (const uint8_t *)&match2->fields;
1010
1011 int i;
1012 for (i = 0; i < sizeof(of_match_fields_t)/sizeof(uint8_t); i++) {
1013 if ((f1[i] ^ f2[i]) & (m1[i] & m2[i])) {
1014 /* Matches disagree on a field bit they both qualify on */
1015 return 0;
1016 }
Rich Lanea06d0c32013-03-25 08:52:03 -07001017 }
Rich Lanea06d0c32013-03-25 08:52:03 -07001018
Rich Lanea06d0c32013-03-25 08:52:03 -07001019 return 1; /* No field differentiates matches */
1020}
1021""")
1022
1023def gen_match_conversions(out=sys.stdout):
1024 match.match_sanity_check()
Rich Lanea06d0c32013-03-25 08:52:03 -07001025 out.write("""
1026/**
Rich Lanea06d0c32013-03-25 08:52:03 -07001027 * @brief Return the index (used as the WC field in 1.0 match) given the mask
1028 */
1029
1030int
1031of_ip_mask_to_index(uint32_t mask)
1032{
1033 int idx;
Rich Lane70c70942014-05-06 15:31:47 -07001034 uint32_t cmask;
Rich Lanea06d0c32013-03-25 08:52:03 -07001035
1036 /* Handle most common cases directly */
Rich Lane70c70942014-05-06 15:31:47 -07001037 if (mask == 0) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001038 return 63;
1039 }
Rich Lane70c70942014-05-06 15:31:47 -07001040 if (mask == 0xffffffff) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001041 return 0;
1042 }
1043
Rich Lane70c70942014-05-06 15:31:47 -07001044 if ((~mask + 1) & ~mask) {
1045 LOCI_LOG_INFO("OF 1.0: Could not map IP addr mask 0x%x", mask);
1046 return 63;
Rich Lanea06d0c32013-03-25 08:52:03 -07001047 }
1048
Rich Lane70c70942014-05-06 15:31:47 -07001049 idx = 0;
1050 cmask = ~mask;
1051 while (cmask) {
1052 cmask >>= 1;
1053 idx += 1;
1054 }
1055
1056 return idx;
Rich Lanea06d0c32013-03-25 08:52:03 -07001057}
1058
1059/**
1060 * @brief Return the mask for the given index
1061 */
1062
1063uint32_t
1064of_ip_index_to_mask(int index)
1065{
Rich Lane70c70942014-05-06 15:31:47 -07001066 if (index >= 32) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001067 return 0;
Rich Lane70c70942014-05-06 15:31:47 -07001068 } else {
1069 return 0xffffffff << index;
Rich Lanea06d0c32013-03-25 08:52:03 -07001070 }
Rich Lanea06d0c32013-03-25 08:52:03 -07001071}
1072
1073""")
Rich Lanea06d0c32013-03-25 08:52:03 -07001074 gen_unified_match_to_v1(out)
1075 gen_unified_match_to_v2(out)
1076 gen_unified_match_to_v3(out)
1077 gen_v1_to_unified_match(out)
1078 gen_v2_to_unified_match(out)
1079 gen_v3_to_unified_match(out)