blob: e5a41efa99d6a2fe32083a394cdc4a9e21a9535d [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{
461 of_oxm_t oxm_entry;
462
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)) {
469 of_oxm_%(key)s_masked_t *elt;
470 elt = &oxm_entry.%(key)s_masked;
471
472 of_oxm_%(key)s_masked_init(elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700473 oxm_list->version, -1, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700474 of_list_oxm_append_bind(oxm_list, &oxm_entry);
Andreas Wundsam53256162013-05-02 14:05:53 -0700475 of_oxm_%(key)s_masked_value_set(elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700476 src->fields.%(key)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700477 of_oxm_%(key)s_masked_value_mask_set(elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700478 src->masks.%(key)s);
479 } else { /* Active, but not masked */
480 of_oxm_%(key)s_t *elt;
481 elt = &oxm_entry.%(key)s;
482 of_oxm_%(key)s_init(elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700483 oxm_list->version, -1, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700484 of_list_oxm_append_bind(oxm_list, &oxm_entry);
485 of_oxm_%(key)s_value_set(elt, src->fields.%(key)s);
486 }
487 }
488""" % dict(key=key, ku=key.upper()))
489 out.write("""
490 return OF_ERROR_NONE;
491}
492
493/**
494 * Convert a generic match object to an OF_VERSION_1_2 object
495 * @param src Pointer to the generic match object source
496 * @param dst Pointer to the OF 1.2 wire structure
497 *
498 * The wire structure is initialized by this function if the object
499 * id is not correct in the object
500 */
501
502int
503of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst)
504{
505 int rv = OF_ERROR_NONE;
506 of_list_oxm_t *oxm_list;
507
508 if ((src == NULL) || (dst == NULL)) {
509 return OF_ERROR_PARAM;
510 }
511 if (dst->object_id != OF_MATCH_V3) {
Rich Lanecfd4ce02013-07-12 16:37:14 -0700512 of_match_v3_init(dst, OF_VERSION_1_2, 0, 0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700513 }
Rich Lanecfd4ce02013-07-12 16:37:14 -0700514 if ((oxm_list = of_list_oxm_new(dst->version)) == NULL) {
Rich Lanea06d0c32013-03-25 08:52:03 -0700515 return OF_ERROR_RESOURCE;
516 }
517
518 rv = populate_oxm_list(src, oxm_list);
519
520 if (rv == OF_ERROR_NONE) {
521 rv = of_match_v3_oxm_list_set(dst, oxm_list);
522 }
523
524 of_list_oxm_delete(oxm_list);
525
526 return rv;
527}
528""")
529
530def gen_v1_to_unified_match(out):
531 """
532 Generate the code that maps a v1 wire format match object
533 to a unified match object
534 """
535 # for each v1 member, if not in wildcards
536 # translate to unified. Treat nw_src/dst specially
537 out.write("""
538
539/**
540 * Convert an OF_VERSION_1_0 object to a generic match object
541 * @param src Pointer to the OF 1.0 wire structure source
542 * @param dst Pointer to the generic match object destination
543 *
544 * The wire structure is initialized by this function.
545 */
546
547int
548of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst)
549{
550 of_wc_bmap_t wc;
551 int count;
552
553 MEMSET(dst, 0, sizeof(*dst));
554 dst->version = src->version;
555
556 of_match_v1_wildcards_get(src, &wc);
557""")
Rich Lanea06d0c32013-03-25 08:52:03 -0700558 for key in sorted(match.of_v1_keys):
559 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
560 out.write("""
561 count = OF_MATCH_V1_WC_%(ku)s_GET(wc);
562 dst->masks.%(key)s = of_ip_index_to_mask(count);
Rich Lanea06d0c32013-03-25 08:52:03 -0700563 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
Dan Talaycofb50d382013-08-05 16:00:17 -0700564 /* Clear the bits not indicated by mask; IP addrs are special for 1.0 */
565 dst->fields.%(key)s &= dst->masks.%(key)s;
Rich Lanea06d0c32013-03-25 08:52:03 -0700566""" % dict(ku=key.upper(), key=key))
567 else:
568 out.write("""
569 if (!(OF_MATCH_V1_WC_%(ku)s_TEST(wc))) {
570 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
571 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
572 }
573""" % dict(ku=key.upper(), key=key))
574
575 out.write("""
576 return OF_ERROR_NONE;
577}
578""")
579
580def gen_v2_to_unified_match(out):
581 """
582 Generate the code that maps a v2 wire format match object
583 to a unified match object
584 """
585 out.write("""
586int
587of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst)
588{
589 of_wc_bmap_t wc;
590
591 MEMSET(dst, 0, sizeof(*dst));
592 dst->version = src->version;
593
594 of_match_v2_wildcards_get(src, &wc);
595""")
596 for key in match.of_v2_keys:
597 if key in match.of_v2_full_mask:
598 out.write("""
599 of_match_v2_%(key)s_mask_get(src, &dst->masks.%(key)s);
600 if (OF_VARIABLE_IS_NON_ZERO(&dst->masks.%(key)s)) { /* Matching something */
601 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
602 }
Rich Lane054a8182014-04-16 14:42:41 -0700603 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700604""" % dict(ku=key.upper(), key=key))
605 else:
606 out.write("""
607 if (!(OF_MATCH_V2_WC_%(ku)s_TEST(wc))) {
608 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
609 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
610 }
611""" % dict(ku=key.upper(), key=key))
612
613 out.write("""
Dan Talaycofb50d382013-08-05 16:00:17 -0700614
Rich Lanea06d0c32013-03-25 08:52:03 -0700615 return OF_ERROR_NONE;
616}
617""")
618
619
620def gen_v3_to_unified_match(out):
621 """
622 Generate the code that maps a v3 wire format match object
623 to a unified match object
624 """
625 # Iterate thru the OXM list members
626 out.write("""
627int
628of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst)
629{
630 int rv;
631 of_list_oxm_t oxm_list;
632 of_oxm_t oxm_entry;
633""")
634# for key in match.of_match_members:
635# out.write(" of_oxm_%s_t *%s;\n" % (key, key))
636# out.write(" of_oxm_%s_masked_t *%s_masked;\n" % (key, key))
637
638 out.write("""
639 MEMSET(dst, 0, sizeof(*dst));
640 dst->version = src->version;
641
642 of_match_v3_oxm_list_bind(src, &oxm_list);
643 rv = of_list_oxm_first(&oxm_list, &oxm_entry);
644
645 while (rv == OF_ERROR_NONE) {
646 switch (oxm_entry.header.object_id) { /* What kind of entry is this */
647""")
648 for key in match.of_match_members:
649 out.write("""
650 case OF_OXM_%(ku)s_MASKED:
651 of_oxm_%(key)s_masked_value_mask_get(
652 &oxm_entry.%(key)s_masked,
653 &dst->masks.%(key)s);
654 of_oxm_%(key)s_masked_value_get(
655 &oxm_entry.%(key)s,
656 &dst->fields.%(key)s);
Rich Laned5f23452014-04-03 01:16:37 -0700657 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700658 break;
659 case OF_OXM_%(ku)s:
660 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
661 of_oxm_%(key)s_value_get(
662 &oxm_entry.%(key)s,
663 &dst->fields.%(key)s);
664 break;
665""" % (dict(ku=key.upper(), key=key)))
666
667 out.write("""
668 default:
669 /* @fixme Add debug statement */
670 return OF_ERROR_PARSE;
671 } /* end switch */
672 rv = of_list_oxm_next(&oxm_list, &oxm_entry);
673 } /* end OXM iteration */
674
675 return OF_ERROR_NONE;
676}
677""")
678
679def gen_serialize(out):
680 out.write("""
681/**
682 * Serialize a match structure according to the version passed
683 * @param version The version to use for serialization protocol
684 * @param match Pointer to the structure to serialize
685 * @param octets Pointer to an octets object to fill out
686 *
687 * A buffer is allocated using normal internal ALLOC/FREE semantics
688 * and pointed to by the octets object. The length of the resulting
689 * serialization is in octets->bytes.
690 *
691 * For 1.2 matches, returns the padded serialized structure
692 *
693 * Note that FREE must be called on octets->data when processing of
694 * the object is complete.
695 */
696
697int
698of_match_serialize(of_version_t version, of_match_t *match, of_octets_t *octets)
699{
700 int rv;
701
702 switch (version) {
703""")
704 for version in of_g.of_version_range:
705 out.write("""
706 case %(ver_name)s:
707 {
708 of_match_v%(version)s_t *wire_match;
709 wire_match = of_match_v%(version)s_new(version);
710 if (wire_match == NULL) {
711 return OF_ERROR_RESOURCE;
712 }
713 if ((rv = of_match_to_wire_match_v%(version)s(match, wire_match)) < 0) {
714 of_match_v%(version)s_delete(wire_match);
715 return rv;
716 }
Rich Lanedc9bc7f2014-04-05 11:19:20 -0700717 of_wire_buffer_grow(wire_match->wbuf, OF_MATCH_BYTES(wire_match->length));
718 octets->bytes = wire_match->wbuf->current_bytes;
Rich Lanea06d0c32013-03-25 08:52:03 -0700719 of_object_wire_buffer_steal((of_object_t *)wire_match,
720 &octets->data);
721 of_match_v%(version)s_delete(wire_match);
722 }
723 break;
724""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
725 out.write("""
726 default:
727 return OF_ERROR_COMPAT;
728 }
729
730 return OF_ERROR_NONE;
731}
732""")
733
734
735def gen_deserialize(out):
736 out.write("""
737/**
738 * Deserialize a match structure according to the version passed
739 * @param version The version to use for deserialization protocol
740 * @param match Pointer to the structure to fill out
741 * @param octets Pointer to an octets object holding serial buffer
742 *
743 * Normally the octets object will point to a part of a wire buffer.
744 */
745
746int
747of_match_deserialize(of_version_t version, of_match_t *match,
Rich Lanee0b70cc2014-06-12 15:02:22 -0700748 of_object_t *parent, int offset, int length)
Rich Lanea06d0c32013-03-25 08:52:03 -0700749{
Rich Lanee0b70cc2014-06-12 15:02:22 -0700750 of_object_t obj;
Rich Lanea06d0c32013-03-25 08:52:03 -0700751
752 switch (version) {
753""")
754 for version in of_g.of_version_range:
755 out.write("""
756 case %(ver_name)s:
Rich Lanee0b70cc2014-06-12 15:02:22 -0700757 of_match_v%(version)d_init(&obj, %(ver_name)s, length, 1);
758 of_object_attach(parent, &obj, offset, length);
759 OF_TRY(of_match_v%(version)d_to_match(&obj, match));
Rich Lanea06d0c32013-03-25 08:52:03 -0700760 break;
761""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
762
763 out.write("""
764 default:
765 return OF_ERROR_COMPAT;
766 }
767
768 return OF_ERROR_NONE;
769}
770""")
771
772def gen_match_comp(out=sys.stdout):
773 """
774 Generate match comparison functions
775 """
776 out.write("""
777/**
778 * Determine "more specific" relationship between mac addrs
779 * @return true if v1 is equal to or more specific than v2
780 *
781 * @todo Could be optimized
782 *
783 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
784 * That is, return false if there is a bit set in v2 and not in v1.
785 */
786
787static inline int
788of_more_specific_ipv6(of_ipv6_t *v1, of_ipv6_t *v2) {
789 int idx;
790
791 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
792 /* If there's a bit set in v2 that is clear in v1, return false */
793 if (~v1->addr[idx] & v2->addr[idx]) {
794 return 0;
795 }
796 }
797
798 return 1;
799}
800
801/**
802 * Boolean test if two values agree when restricted to a mask
803 */
804
805static inline int
806of_restricted_match_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, of_ipv6_t *mask) {
807 int idx;
808
809 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700810 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700811 (v2->addr[idx] & mask->addr[idx])) {
812 return 0;
813 }
814 }
815
816 return 1;
817}
818
819/**
820 * Boolean test if two values "overlap" (agree on common masks)
821 */
822
823static inline int
824of_overlap_ipv6(of_ipv6_t *v1, of_ipv6_t *v2,
825 of_ipv6_t *m1, of_ipv6_t *m2) {
826 int idx;
827
828 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700829 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700830 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
831 return 0;
832 }
833 }
834
835 return 1;
836}
837
838#define OF_MORE_SPECIFIC_IPV6(v1, v2) of_more_specific_ipv6((v1), (v2))
839
840#define OF_RESTRICTED_MATCH_IPV6(v1, v2, mask) \\
841 of_restricted_match_ipv6((v1), (v2), (mask))
842
843#define OF_OVERLAP_IPV6(v1, v2, m1, m2) of_overlap_ipv6((v1), (v2), (m1), (m2))
844
845/**
846 * Determine "more specific" relationship between mac addrs
847 * @return true if v1 is equal to or more specific than v2
848 *
849 * @todo Could be optimized
850 *
851 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
852 * That is, return false if there is a bit set in v2 and not in v1.
853 */
854static inline int
855of_more_specific_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2) {
856 int idx;
857
858 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
859 /* If there's a bit set in v2 that is clear in v1, return false */
860 if (~v1->addr[idx] & v2->addr[idx]) {
861 return 0;
862 }
863 }
864
865 return 1;
866}
867
868/**
869 * Boolean test if two values agree when restricted to a mask
870 */
871static inline int
Andreas Wundsam53256162013-05-02 14:05:53 -0700872of_restricted_match_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
Rich Lanea06d0c32013-03-25 08:52:03 -0700873 of_mac_addr_t *mask) {
874 int idx;
875
876 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700877 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700878 (v2->addr[idx] & mask->addr[idx])) {
879 return 0;
880 }
881 }
882
883 return 1;
884}
885
886/**
887 * Boolean test if two values "overlap" (agree on common masks)
888 */
889
890static inline int
891of_overlap_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
892 of_mac_addr_t *m1, of_mac_addr_t *m2) {
893 int idx;
894
895 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700896 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700897 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
898 return 0;
899 }
900 }
901
902 return 1;
903}
904
905#define OF_MORE_SPECIFIC_MAC_ADDR(v1, v2) of_more_specific_mac_addr((v1), (v2))
906
907#define OF_RESTRICTED_MATCH_MAC_ADDR(v1, v2, mask) \\
908 of_restricted_match_mac_addr((v1), (v2), (mask))
909
910#define OF_OVERLAP_MAC_ADDR(v1, v2, m1, m2) \\
911 of_overlap_mac_addr((v1), (v2), (m1), (m2))
912
Rich Lane3b2fd832013-09-24 13:44:08 -0700913#define OF_MORE_SPECIFIC_BITMAP_128(v1, v2) \\
914 (OF_MORE_SPECIFIC_INT((v1)->lo, (v2)->lo) && OF_MORE_SPECIFIC_INT((v1)->hi, (v2)->hi))
915
916#define OF_RESTRICTED_MATCH_BITMAP_128(v1, v2, mask) \\
917 (OF_RESTRICTED_MATCH_INT((v1)->lo, (v2)->lo, (mask)->lo) && OF_RESTRICTED_MATCH_INT((v1)->hi, (v2)->hi, (mask)->hi))
918
919#define OF_OVERLAP_BITMAP_128(v1, v2, m1, m2) \\
920 (OF_OVERLAP_INT((v1)->lo, (v2)->lo, (m1)->lo, (m2)->lo) && OF_OVERLAP_INT((v1)->hi, (v2)->hi, (m1)->hi, (m2)->hi))
921
Rich Lanea06d0c32013-03-25 08:52:03 -0700922/**
923 * More-specific-than macro for integer types; see above
924 * @return true if v1 is equal to or more specific than v2
925 *
926 * If there is a bit that is set in v2 and not in v1, return false.
927 */
928#define OF_MORE_SPECIFIC_INT(v1, v2) (!(~(v1) & (v2)))
929
930/**
931 * Boolean test if two values agree when restricted to a mask
932 */
933#define OF_RESTRICTED_MATCH_INT(v1, v2, mask) \\
934 (((v1) & (mask)) == ((v2) & (mask)))
935
936
937#define OF_OVERLAP_INT(v1, v2, m1, m2) \\
938 ((((v1) & (m1)) & (m2)) == (((v2) & (m1)) & (m2)))
939""")
940
941 out.write("""
942/**
943 * Compare two match structures for exact equality
944 *
945 * We just do memcmp assuming structs were memset to 0 on init
946 */
947static inline int
948of_match_eq(of_match_t *match1, of_match_t *match2)
949{
950 return (MEMCMP(match1, match2, sizeof(of_match_t)) == 0);
951}
952
953/**
954 * Is the entry match more specific than (or equal to) the query match?
955 * @param entry Match expected to be more specific (subset of query)
956 * @param query Match expected to be less specific (superset of entry)
957 * @returns Boolean, see below
958 *
959 * The assumption is that a query is being done for a non-strict
960 * match against an entry in a table. The result is true if the
961 * entry match indicates a more specific (but compatible) flow space
962 * specification than that in the query match. This means that the
963 * values agree between the two where they overlap, and that each mask
964 * for the entry is more specific than that of the query.
965 *
966 * The query has the less specific mask (fewer mask bits) so it is
967 * used for the mask when checking values.
968 */
969
970static inline int
971of_match_more_specific(of_match_t *entry, of_match_t *query)
972{
973 of_match_fields_t *q_m, *e_m; /* Short hand for masks, fields */
974 of_match_fields_t *q_f, *e_f;
975
976 q_m = &query->masks;
977 e_m = &entry->masks;
978 q_f = &query->fields;
979 e_f = &entry->fields;
980""")
981 for key, entry in match.of_match_members.items():
982 q_m = "&q_m->%s" % key
983 e_m = "&e_m->%s" % key
984 q_f = "&q_f->%s" % key
985 e_f = "&e_f->%s" % key
986 if entry["m_type"] == "of_ipv6_t":
987 comp = "OF_MORE_SPECIFIC_IPV6"
988 match_type = "OF_RESTRICTED_MATCH_IPV6"
989 elif entry["m_type"] == "of_mac_addr_t":
990 comp = "OF_MORE_SPECIFIC_MAC_ADDR"
991 match_type = "OF_RESTRICTED_MATCH_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -0700992 elif entry["m_type"] == "of_bitmap_128_t":
993 comp = "OF_MORE_SPECIFIC_BITMAP_128"
994 match_type = "OF_RESTRICTED_MATCH_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -0700995 else: # Integer
996 comp = "OF_MORE_SPECIFIC_INT"
997 match_type = "OF_RESTRICTED_MATCH_INT"
998 q_m = "q_m->%s" % key
999 e_m = "e_m->%s" % key
1000 q_f = "q_f->%s" % key
1001 e_f = "e_f->%s" % key
1002 out.write("""
1003 /* Mask and values for %(key)s */
1004 if (!%(comp)s(%(e_m)s, %(q_m)s)) {
1005 return 0;
1006 }
1007 if (!%(match_type)s(%(e_f)s, %(q_f)s,
1008 %(q_m)s)) {
1009 return 0;
1010 }
Andreas Wundsam53256162013-05-02 14:05:53 -07001011""" % dict(match_type=match_type, comp=comp, q_f=q_f, e_f=e_f,
Rich Lanea06d0c32013-03-25 08:52:03 -07001012 q_m=q_m, e_m=e_m, key=key))
1013
1014 out.write("""
1015 return 1;
1016}
1017""")
1018
1019 out.write("""
1020
1021/**
1022 * Do two entries overlap?
1023 * @param match1 One match struct
1024 * @param match2 Another match struct
1025 * @returns Boolean: true if there is a packet that would match both
1026 *
1027 */
1028
1029static inline int
1030of_match_overlap(of_match_t *match1, of_match_t *match2)
1031{
1032 of_match_fields_t *m1, *m2; /* Short hand for masks, fields */
1033 of_match_fields_t *f1, *f2;
1034
1035 m1 = &match1->masks;
1036 m2 = &match2->masks;
1037 f1 = &match1->fields;
1038 f2 = &match2->fields;
1039""")
1040 for key, entry in match.of_match_members.items():
1041 m1 = "&m1->%s" % key
1042 m2 = "&m2->%s" % key
1043 f1 = "&f1->%s" % key
1044 f2 = "&f2->%s" % key
1045 if entry["m_type"] == "of_ipv6_t":
1046 check = "OF_OVERLAP_IPV6"
1047 elif entry["m_type"] == "of_mac_addr_t":
1048 check = "OF_OVERLAP_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -07001049 elif entry["m_type"] == "of_bitmap_128_t":
1050 check = "OF_OVERLAP_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -07001051 else: # Integer
1052 check = "OF_OVERLAP_INT"
1053 m1 = "m1->%s" % key
1054 m2 = "m2->%s" % key
1055 f1 = "f1->%s" % key
1056 f2 = "f2->%s" % key
1057 out.write("""
1058 /* Check overlap for %(key)s */
Andreas Wundsam53256162013-05-02 14:05:53 -07001059 if (!%(check)s(%(f1)s, %(f2)s,
Rich Lanea06d0c32013-03-25 08:52:03 -07001060 %(m2)s, %(m1)s)) {
1061 return 0; /* This field differentiates; all done */
1062 }
1063""" % dict(check=check, f1=f1, f2=f2, m1=m1, m2=m2, key=key))
1064
1065 out.write("""
1066 return 1; /* No field differentiates matches */
1067}
1068""")
1069
1070def gen_match_conversions(out=sys.stdout):
1071 match.match_sanity_check()
Rich Lanea06d0c32013-03-25 08:52:03 -07001072 out.write("""
1073/**
Rich Lanea06d0c32013-03-25 08:52:03 -07001074 * @brief Return the index (used as the WC field in 1.0 match) given the mask
1075 */
1076
1077int
1078of_ip_mask_to_index(uint32_t mask)
1079{
1080 int idx;
Rich Lane70c70942014-05-06 15:31:47 -07001081 uint32_t cmask;
Rich Lanea06d0c32013-03-25 08:52:03 -07001082
1083 /* Handle most common cases directly */
Rich Lane70c70942014-05-06 15:31:47 -07001084 if (mask == 0) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001085 return 63;
1086 }
Rich Lane70c70942014-05-06 15:31:47 -07001087 if (mask == 0xffffffff) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001088 return 0;
1089 }
1090
Rich Lane70c70942014-05-06 15:31:47 -07001091 if ((~mask + 1) & ~mask) {
1092 LOCI_LOG_INFO("OF 1.0: Could not map IP addr mask 0x%x", mask);
1093 return 63;
Rich Lanea06d0c32013-03-25 08:52:03 -07001094 }
1095
Rich Lane70c70942014-05-06 15:31:47 -07001096 idx = 0;
1097 cmask = ~mask;
1098 while (cmask) {
1099 cmask >>= 1;
1100 idx += 1;
1101 }
1102
1103 return idx;
Rich Lanea06d0c32013-03-25 08:52:03 -07001104}
1105
1106/**
1107 * @brief Return the mask for the given index
1108 */
1109
1110uint32_t
1111of_ip_index_to_mask(int index)
1112{
Rich Lane70c70942014-05-06 15:31:47 -07001113 if (index >= 32) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001114 return 0;
Rich Lane70c70942014-05-06 15:31:47 -07001115 } else {
1116 return 0xffffffff << index;
Rich Lanea06d0c32013-03-25 08:52:03 -07001117 }
Rich Lanea06d0c32013-03-25 08:52:03 -07001118}
1119
1120""")
Rich Lanea06d0c32013-03-25 08:52:03 -07001121 gen_unified_match_to_v1(out)
1122 gen_unified_match_to_v2(out)
1123 gen_unified_match_to_v3(out)
1124 gen_v1_to_unified_match(out)
1125 gen_v2_to_unified_match(out)
1126 gen_v3_to_unified_match(out)