blob: 3afc99caad094c8a226f5777f69f093f9714f823 [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
107""")
108
109def gen_match_macros(out):
110 out.write("""
111
112/**
113 * Definitions for wildcard macros for OF_VERSION_1_0
114 */
115
116""")
117 for key in match.of_v1_keys:
118 entry = match.of_match_members[key]
119 if "v1_wc_shift" in entry:
120 if key in ["ipv4_src", "ipv4_dst"]:
121 out.write("""
122#define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d
123#define OF_MATCH_V1_WC_%(ku)s_MASK (0x3f << %(val)d)
124#define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(0x3f << %(val)d))
125#define OF_MATCH_V1_WC_%(ku)s_SET(wc, value) do { \\
126 OF_MATCH_V1_WC_%(ku)s_CLEAR(wc); \\
127 ((wc) |= (((value) & 0x3f) << %(val)d)); \\
128 } while (0)
129#define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (0x3f << %(val)d))
130#define OF_MATCH_V1_WC_%(ku)s_GET(wc) (((wc) >> %(val)d) & 0x3f)
131""" % dict(ku=key.upper(), val=entry["v1_wc_shift"]))
132 else:
133 out.write("""
134#define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d
135#define OF_MATCH_V1_WC_%(ku)s_MASK (1 << %(val)d)
136#define OF_MATCH_V1_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d))
137#define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d))
138#define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d))
139""" % dict(ku=key.upper(), val=entry["v1_wc_shift"]))
140
141 out.write("""
142
143/**
144 * Definitions for wildcard macros for OF_VERSION_1_1
145 */
146""")
147
148 for key in sorted(match.of_v2_keys):
149 entry = match.of_match_members[key]
150 if "v2_wc_shift" in entry:
151 out.write("""
152#define OF_MATCH_V2_WC_%(ku)s_SHIFT %(val)d
153#define OF_MATCH_V2_WC_%(ku)s_MASK (1 << %(val)d)
154#define OF_MATCH_V2_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d))
155#define OF_MATCH_V2_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d))
156#define OF_MATCH_V2_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d))
157""" % dict(ku=key.upper(), val=entry["v2_wc_shift"]))
158
159
160def gen_match_struct(out=sys.stdout):
161 out.write("/* Unified, flat OpenFlow match structure based on OF 1.2 */\n")
162 out.write("typedef struct of_match_fields_s {\n")
163 out.write(" /* Version 1.2 is used for field names */\n")
164 for name in match.match_keys_sorted:
165 entry = match.of_match_members[name]
166 out.write(" %-20s %s;\n" % (entry["m_type"], entry["name"]))
167 out.write("""
168} of_match_fields_t;
169
170/**
171 * @brief The LOCI match structure.
172 */
173
174typedef struct of_match_s {
175 of_version_t version;
176 of_match_fields_t fields;
177 of_match_fields_t masks;
178} of_match_t;
179
Rich Lane68798a52014-04-16 14:57:52 -0700180/*
181 * AND 'len' bytes starting from 'value' with the corresponding byte in
182 * 'mask'.
Dan Talaycofb50d382013-08-05 16:00:17 -0700183 */
Rich Laned5f23452014-04-03 01:16:37 -0700184static inline void
Rich Lane68798a52014-04-16 14:57:52 -0700185of_memmask(void *value, const void *mask, size_t len)
Dan Talaycofb50d382013-08-05 16:00:17 -0700186{
Rich Lane68798a52014-04-16 14:57:52 -0700187 int i;
188 uint8_t *v = value;
189 const uint8_t *m = mask;
Dan Talaycofb50d382013-08-05 16:00:17 -0700190
Rich Lane68798a52014-04-16 14:57:52 -0700191 for (i = 0; i < len; i++) {
192 v[i] &= m[i];
Dan Talaycofb50d382013-08-05 16:00:17 -0700193 }
194}
195
196/**
Rich Lane70c70942014-05-06 15:31:47 -0700197 * @brief Map from mask to OF 1.0 wildcard
Rich Lanea06d0c32013-03-25 08:52:03 -0700198 */
199
200extern int of_ip_mask_to_index(uint32_t mask);
201
202/**
Rich Lane70c70942014-05-06 15:31:47 -0700203 * @brief Map from OF 1.0 wildcard to mask
Rich Lanea06d0c32013-03-25 08:52:03 -0700204 */
205
206extern uint32_t of_ip_index_to_mask(int index);
207
208/**
209 * The signalling of an untagged packet varies by OF version.
210 * Use this macro to set the field value.
211 */
212#define OF_MATCH_UNTAGGED_VLAN_ID(version) \\
213 ((version) == OF_VERSION_1_0 ? 0xffff : \\
214 ((version) == OF_VERSION_1_1 ? 0xffff : 0))
215
216/**
217 * Version 1.1 had the notion of "any" vlan but must be set
218 */
219#define OF_MATCH_VLAN_TAG_PRESENT_ANY_ID(version) \\
220 ((version) == OF_VERSION_1_0 ? 0 /* @fixme */ : \\
221 ((version) == OF_VERSION_1_1 ? 0xfffe : 0x1000))
222""")
223
224def gen_oxm_defines(out):
225 """
226 Generate verbatim definitions for OXM
227 """
228 out.write("""
Rich Lanea06d0c32013-03-25 08:52:03 -0700229/*
230 * The generic match structure uses the OXM bit indices for it's
231 * bitmasks for active and masked values
232 */
233""")
234 for key, entry in match.of_match_members.items():
235 out.write("""
236/* Mask/value check/set macros for %(key)s */
237
238/**
239 * Set the mask for an exact match of %(key)s
240 */
241#define OF_MATCH_MASK_%(ku)s_EXACT_SET(_match) \\
242 MEMSET(&(_match)->masks.%(key)s, 0xff, \\
243 sizeof(((_match)->masks).%(key)s))
244
245/**
246 * Clear the mask for %(key)s making that field inactive for the match
247 */
248#define OF_MATCH_MASK_%(ku)s_CLEAR(_match) \\
249 MEMSET(&(_match)->masks.%(key)s, 0, \\
250 sizeof(((_match)->masks).%(key)s))
251
252/**
253 * Test whether the match is exact for %(key)s
254 */
255#define OF_MATCH_MASK_%(ku)s_EXACT_TEST(_match) \\
256 OF_VARIABLE_IS_ALL_ONES(&(((_match)->masks).%(key)s))
257
258/**
259 * Test whether key %(key)s is being checked in the match
260 */
261#define OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(_match) \\
262 OF_VARIABLE_IS_NON_ZERO(&(((_match)->masks).%(key)s))
263
Rich Laned56f8d22014-05-06 14:52:55 -0700264""" % dict(key=key, ku=key.upper()))
Rich Lanea06d0c32013-03-25 08:52:03 -0700265
Rich Lanea06d0c32013-03-25 08:52:03 -0700266def gen_unified_match_to_v1(out):
267 """
268 Generate C code to convert a unified match structure to a V1 match struct
269 @param out The output file handle
270 """
271
272 out.write("""
273/**
274 * Check if match is compatible with OF 1.0
275 * @param match The match being checked
276 */
277static inline int
278of_match_v1_compat_check(of_match_t *match)
279{
280""")
281 for key in match.of_match_members:
282 if key in match.of_v1_keys:
283 continue
284 out.write("""
285 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
286 return 0;
287 }
288""" % dict(ku=key.upper()))
289
290 out.write("""
291 return 1;
292}
293""")
294
295 out.write("""
296/**
297 * Convert a generic match object to an OF_VERSION_1_0 object
298 * @param src Pointer to the generic match object source
299 * @param dst Pointer to the OF 1.0 wire structure
300 *
301 * The wire structure is initialized by this function if it doesn't
302 * not have the proper object ID.
303 */
304
305int
306of_match_to_wire_match_v1(of_match_t *src, of_match_v1_t *dst)
307{
308 of_wc_bmap_t wildcards = 0;
309 int ip_mask_index;
310
311 if ((src == NULL) || (dst == NULL)) {
312 return OF_ERROR_PARAM;
313 }
314 if (!of_match_v1_compat_check(src)) {
315 return OF_ERROR_COMPAT;
316 }
317 if (dst->object_id != OF_MATCH_V1) {
318 of_match_v1_init(dst, OF_VERSION_1_0, 0, 0);
319 }
320""")
321 for key in sorted(match.of_v1_keys):
322 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
323 out.write("""
324 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
325 ip_mask_index = of_ip_mask_to_index(src->masks.%(key)s);
326 of_match_v1_%(key)s_set(dst, src->fields.%(key)s);
327 } else { /* Wildcarded, look for 0 mask */
328 ip_mask_index = of_ip_mask_to_index(0);
329 }
330 OF_MATCH_V1_WC_%(ku)s_SET(wildcards, ip_mask_index);
331""" % dict(key=key, ku=key.upper()))
332 else:
333 out.write("""
334 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
335 of_match_v1_%(key)s_set(dst, src->fields.%(key)s);
336 } else {
337 OF_MATCH_V1_WC_%(ku)s_SET(wildcards);
338 }
339""" % dict(key=key, ku=key.upper()))
340
341 out.write("""
342 of_match_v1_wildcards_set(dst, wildcards);
343
344 return OF_ERROR_NONE;
345}
346""")
347
348def all_ones_mask(d_type):
349 if d_type == "of_mac_addr_t":
350 return "of_mac_addr_all_ones"
351 else:
352 return "((%s) -1)" % d_type
353
354def gen_unified_match_to_v2(out):
355 """
356 Generate C code to convert a unified match structure to a V2 match struct
357 @param out The output file handle
358 """
359
360 out.write("""
361/**
362 * Check if match is compatible with OF 1.0
363 * @param match The match being checked
364 */
365static inline int
366of_match_v2_compat_check(of_match_t *match)
367{
368""")
369 for key in match.of_match_members:
370 if key in match.of_v2_keys:
371 continue
372 out.write("""
373 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
374 return 0;
375 }
376""" % dict(ku=key.upper()))
377
378 out.write("""
379 return 1;
380}
381""")
382
383 out.write("""
384/**
385 * Convert a generic match object to an OF_VERSION_1_1 object
386 * @param src Pointer to the generic match object source
387 * @param dst Pointer to the OF 1.1 wire structure
388 *
389 * The wire structure is initialized by this function.
390 */
391
392int
393of_match_to_wire_match_v2(of_match_t *src, of_match_v2_t *dst)
394{
395 of_wc_bmap_t wildcards = 0;
396
397 if ((src == NULL) || (dst == NULL)) {
398 return OF_ERROR_PARAM;
399 }
400 if (!of_match_v2_compat_check(src)) {
401 return OF_ERROR_COMPAT;
402 }
403 if (dst->object_id != OF_MATCH_V2) {
404 of_match_v2_init(dst, OF_VERSION_1_1, 0, 0);
405 }
406""")
407 for key in match.of_v2_keys:
408 if key in match.of_v2_full_mask:
409 ones_mask = all_ones_mask(match.of_match_members[key]["m_type"])
410 out.write("""
411 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
412 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
413 of_match_v2_%(key)s_mask_set(dst,
414 src->masks.%(key)s);
415 } else { /* Exact match; use all ones mask */
416 of_match_v2_%(key)s_mask_set(dst,
417 %(ones_mask)s);
418 }
419 of_match_v2_%(key)s_set(dst, src->fields.%(key)s);
420 }
421
422""" % dict(key=key, ku=key.upper(), ones_mask=ones_mask))
423 else:
424 out.write("""
425 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
426 return OF_ERROR_COMPAT;
427 }
428 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
429 of_match_v2_%(key)s_set(dst, src->fields.%(key)s);
430 } else {
431 OF_MATCH_V2_WC_%(ku)s_SET(wildcards);
432 }
433""" % dict(key=key, ku=key.upper(),
434 wc_bit="OF_MATCH_WC_V2_%s" % key.upper()))
435
436 out.write("""
437 of_match_v2_wildcards_set(dst, wildcards);
438
439 return OF_ERROR_NONE;
440}
441""")
442
443def gen_unified_match_to_v3(out):
444 """
445 Generate C code to convert a unified match structure to a V3 match
446
447 This is much easier as the unified struct is based on V3
448 @param out The output file handle
449 """
450 out.write("""
451static int
452populate_oxm_list(of_match_t *src, of_list_oxm_t *oxm_list)
453{
454 of_oxm_t oxm_entry;
455
456 /* For each active member, add an OXM entry to the list */
457""")
Rich Lane4964d542013-10-14 18:13:47 -0700458 for key in match.match_keys_sorted:
Rich Lanea06d0c32013-03-25 08:52:03 -0700459 out.write("""\
460 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
461 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
462 of_oxm_%(key)s_masked_t *elt;
463 elt = &oxm_entry.%(key)s_masked;
464
465 of_oxm_%(key)s_masked_init(elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700466 oxm_list->version, -1, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700467 of_list_oxm_append_bind(oxm_list, &oxm_entry);
Andreas Wundsam53256162013-05-02 14:05:53 -0700468 of_oxm_%(key)s_masked_value_set(elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700469 src->fields.%(key)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700470 of_oxm_%(key)s_masked_value_mask_set(elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700471 src->masks.%(key)s);
472 } else { /* Active, but not masked */
473 of_oxm_%(key)s_t *elt;
474 elt = &oxm_entry.%(key)s;
475 of_oxm_%(key)s_init(elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700476 oxm_list->version, -1, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700477 of_list_oxm_append_bind(oxm_list, &oxm_entry);
478 of_oxm_%(key)s_value_set(elt, src->fields.%(key)s);
479 }
480 }
481""" % dict(key=key, ku=key.upper()))
482 out.write("""
483 return OF_ERROR_NONE;
484}
485
486/**
487 * Convert a generic match object to an OF_VERSION_1_2 object
488 * @param src Pointer to the generic match object source
489 * @param dst Pointer to the OF 1.2 wire structure
490 *
491 * The wire structure is initialized by this function if the object
492 * id is not correct in the object
493 */
494
495int
496of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst)
497{
498 int rv = OF_ERROR_NONE;
499 of_list_oxm_t *oxm_list;
500
501 if ((src == NULL) || (dst == NULL)) {
502 return OF_ERROR_PARAM;
503 }
504 if (dst->object_id != OF_MATCH_V3) {
Rich Lanecfd4ce02013-07-12 16:37:14 -0700505 of_match_v3_init(dst, OF_VERSION_1_2, 0, 0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700506 }
Rich Lanecfd4ce02013-07-12 16:37:14 -0700507 if ((oxm_list = of_list_oxm_new(dst->version)) == NULL) {
Rich Lanea06d0c32013-03-25 08:52:03 -0700508 return OF_ERROR_RESOURCE;
509 }
510
511 rv = populate_oxm_list(src, oxm_list);
512
513 if (rv == OF_ERROR_NONE) {
514 rv = of_match_v3_oxm_list_set(dst, oxm_list);
515 }
516
517 of_list_oxm_delete(oxm_list);
518
519 return rv;
520}
521""")
522
523def gen_v1_to_unified_match(out):
524 """
525 Generate the code that maps a v1 wire format match object
526 to a unified match object
527 """
528 # for each v1 member, if not in wildcards
529 # translate to unified. Treat nw_src/dst specially
530 out.write("""
531
532/**
533 * Convert an OF_VERSION_1_0 object to a generic match object
534 * @param src Pointer to the OF 1.0 wire structure source
535 * @param dst Pointer to the generic match object destination
536 *
537 * The wire structure is initialized by this function.
538 */
539
540int
541of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst)
542{
543 of_wc_bmap_t wc;
544 int count;
545
546 MEMSET(dst, 0, sizeof(*dst));
547 dst->version = src->version;
548
549 of_match_v1_wildcards_get(src, &wc);
550""")
Rich Lanea06d0c32013-03-25 08:52:03 -0700551 for key in sorted(match.of_v1_keys):
552 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
553 out.write("""
554 count = OF_MATCH_V1_WC_%(ku)s_GET(wc);
555 dst->masks.%(key)s = of_ip_index_to_mask(count);
Rich Lanea06d0c32013-03-25 08:52:03 -0700556 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
Dan Talaycofb50d382013-08-05 16:00:17 -0700557 /* Clear the bits not indicated by mask; IP addrs are special for 1.0 */
558 dst->fields.%(key)s &= dst->masks.%(key)s;
Rich Lanea06d0c32013-03-25 08:52:03 -0700559""" % dict(ku=key.upper(), key=key))
560 else:
561 out.write("""
562 if (!(OF_MATCH_V1_WC_%(ku)s_TEST(wc))) {
563 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
564 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
565 }
566""" % dict(ku=key.upper(), key=key))
567
568 out.write("""
569 return OF_ERROR_NONE;
570}
571""")
572
573def gen_v2_to_unified_match(out):
574 """
575 Generate the code that maps a v2 wire format match object
576 to a unified match object
577 """
578 out.write("""
579int
580of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst)
581{
582 of_wc_bmap_t wc;
583
584 MEMSET(dst, 0, sizeof(*dst));
585 dst->version = src->version;
586
587 of_match_v2_wildcards_get(src, &wc);
588""")
589 for key in match.of_v2_keys:
590 if key in match.of_v2_full_mask:
591 out.write("""
592 of_match_v2_%(key)s_mask_get(src, &dst->masks.%(key)s);
593 if (OF_VARIABLE_IS_NON_ZERO(&dst->masks.%(key)s)) { /* Matching something */
594 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
595 }
Rich Lane054a8182014-04-16 14:42:41 -0700596 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700597""" % dict(ku=key.upper(), key=key))
598 else:
599 out.write("""
600 if (!(OF_MATCH_V2_WC_%(ku)s_TEST(wc))) {
601 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
602 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
603 }
604""" % dict(ku=key.upper(), key=key))
605
606 out.write("""
Dan Talaycofb50d382013-08-05 16:00:17 -0700607
Rich Lanea06d0c32013-03-25 08:52:03 -0700608 return OF_ERROR_NONE;
609}
610""")
611
612
613def gen_v3_to_unified_match(out):
614 """
615 Generate the code that maps a v3 wire format match object
616 to a unified match object
617 """
618 # Iterate thru the OXM list members
619 out.write("""
620int
621of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst)
622{
623 int rv;
624 of_list_oxm_t oxm_list;
625 of_oxm_t oxm_entry;
626""")
627# for key in match.of_match_members:
628# out.write(" of_oxm_%s_t *%s;\n" % (key, key))
629# out.write(" of_oxm_%s_masked_t *%s_masked;\n" % (key, key))
630
631 out.write("""
632 MEMSET(dst, 0, sizeof(*dst));
633 dst->version = src->version;
634
635 of_match_v3_oxm_list_bind(src, &oxm_list);
636 rv = of_list_oxm_first(&oxm_list, &oxm_entry);
637
638 while (rv == OF_ERROR_NONE) {
639 switch (oxm_entry.header.object_id) { /* What kind of entry is this */
640""")
641 for key in match.of_match_members:
642 out.write("""
643 case OF_OXM_%(ku)s_MASKED:
644 of_oxm_%(key)s_masked_value_mask_get(
645 &oxm_entry.%(key)s_masked,
646 &dst->masks.%(key)s);
647 of_oxm_%(key)s_masked_value_get(
648 &oxm_entry.%(key)s,
649 &dst->fields.%(key)s);
Rich Laned5f23452014-04-03 01:16:37 -0700650 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700651 break;
652 case OF_OXM_%(ku)s:
653 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
654 of_oxm_%(key)s_value_get(
655 &oxm_entry.%(key)s,
656 &dst->fields.%(key)s);
657 break;
658""" % (dict(ku=key.upper(), key=key)))
659
660 out.write("""
661 default:
662 /* @fixme Add debug statement */
663 return OF_ERROR_PARSE;
664 } /* end switch */
665 rv = of_list_oxm_next(&oxm_list, &oxm_entry);
666 } /* end OXM iteration */
667
668 return OF_ERROR_NONE;
669}
670""")
671
672def gen_serialize(out):
673 out.write("""
674/**
675 * Serialize a match structure according to the version passed
676 * @param version The version to use for serialization protocol
677 * @param match Pointer to the structure to serialize
678 * @param octets Pointer to an octets object to fill out
679 *
680 * A buffer is allocated using normal internal ALLOC/FREE semantics
681 * and pointed to by the octets object. The length of the resulting
682 * serialization is in octets->bytes.
683 *
684 * For 1.2 matches, returns the padded serialized structure
685 *
686 * Note that FREE must be called on octets->data when processing of
687 * the object is complete.
688 */
689
690int
691of_match_serialize(of_version_t version, of_match_t *match, of_octets_t *octets)
692{
693 int rv;
694
695 switch (version) {
696""")
697 for version in of_g.of_version_range:
698 out.write("""
699 case %(ver_name)s:
700 {
701 of_match_v%(version)s_t *wire_match;
702 wire_match = of_match_v%(version)s_new(version);
703 if (wire_match == NULL) {
704 return OF_ERROR_RESOURCE;
705 }
706 if ((rv = of_match_to_wire_match_v%(version)s(match, wire_match)) < 0) {
707 of_match_v%(version)s_delete(wire_match);
708 return rv;
709 }
Rich Lanedc9bc7f2014-04-05 11:19:20 -0700710 of_wire_buffer_grow(wire_match->wbuf, OF_MATCH_BYTES(wire_match->length));
711 octets->bytes = wire_match->wbuf->current_bytes;
Rich Lanea06d0c32013-03-25 08:52:03 -0700712 of_object_wire_buffer_steal((of_object_t *)wire_match,
713 &octets->data);
714 of_match_v%(version)s_delete(wire_match);
715 }
716 break;
717""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
718 out.write("""
719 default:
720 return OF_ERROR_COMPAT;
721 }
722
723 return OF_ERROR_NONE;
724}
725""")
726
727
728def gen_deserialize(out):
729 out.write("""
730/**
731 * Deserialize a match structure according to the version passed
732 * @param version The version to use for deserialization protocol
733 * @param match Pointer to the structure to fill out
734 * @param octets Pointer to an octets object holding serial buffer
735 *
736 * Normally the octets object will point to a part of a wire buffer.
737 */
738
739int
740of_match_deserialize(of_version_t version, of_match_t *match,
Rich Lanee0b70cc2014-06-12 15:02:22 -0700741 of_object_t *parent, int offset, int length)
Rich Lanea06d0c32013-03-25 08:52:03 -0700742{
Rich Lanee0b70cc2014-06-12 15:02:22 -0700743 of_object_t obj;
Rich Lanea06d0c32013-03-25 08:52:03 -0700744
745 switch (version) {
746""")
747 for version in of_g.of_version_range:
748 out.write("""
749 case %(ver_name)s:
Rich Lanee0b70cc2014-06-12 15:02:22 -0700750 of_match_v%(version)d_init(&obj, %(ver_name)s, length, 1);
751 of_object_attach(parent, &obj, offset, length);
752 OF_TRY(of_match_v%(version)d_to_match(&obj, match));
Rich Lanea06d0c32013-03-25 08:52:03 -0700753 break;
754""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
755
756 out.write("""
757 default:
758 return OF_ERROR_COMPAT;
759 }
760
761 return OF_ERROR_NONE;
762}
763""")
764
765def gen_match_comp(out=sys.stdout):
766 """
767 Generate match comparison functions
768 """
769 out.write("""
770/**
771 * Determine "more specific" relationship between mac addrs
772 * @return true if v1 is equal to or more specific than v2
773 *
774 * @todo Could be optimized
775 *
776 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
777 * That is, return false if there is a bit set in v2 and not in v1.
778 */
779
780static inline int
781of_more_specific_ipv6(of_ipv6_t *v1, of_ipv6_t *v2) {
782 int idx;
783
784 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
785 /* If there's a bit set in v2 that is clear in v1, return false */
786 if (~v1->addr[idx] & v2->addr[idx]) {
787 return 0;
788 }
789 }
790
791 return 1;
792}
793
794/**
795 * Boolean test if two values agree when restricted to a mask
796 */
797
798static inline int
799of_restricted_match_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, of_ipv6_t *mask) {
800 int idx;
801
802 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700803 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700804 (v2->addr[idx] & mask->addr[idx])) {
805 return 0;
806 }
807 }
808
809 return 1;
810}
811
812/**
813 * Boolean test if two values "overlap" (agree on common masks)
814 */
815
816static inline int
817of_overlap_ipv6(of_ipv6_t *v1, of_ipv6_t *v2,
818 of_ipv6_t *m1, of_ipv6_t *m2) {
819 int idx;
820
821 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700822 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700823 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
824 return 0;
825 }
826 }
827
828 return 1;
829}
830
831#define OF_MORE_SPECIFIC_IPV6(v1, v2) of_more_specific_ipv6((v1), (v2))
832
833#define OF_RESTRICTED_MATCH_IPV6(v1, v2, mask) \\
834 of_restricted_match_ipv6((v1), (v2), (mask))
835
836#define OF_OVERLAP_IPV6(v1, v2, m1, m2) of_overlap_ipv6((v1), (v2), (m1), (m2))
837
838/**
839 * Determine "more specific" relationship between mac addrs
840 * @return true if v1 is equal to or more specific than v2
841 *
842 * @todo Could be optimized
843 *
844 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
845 * That is, return false if there is a bit set in v2 and not in v1.
846 */
847static inline int
848of_more_specific_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2) {
849 int idx;
850
851 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
852 /* If there's a bit set in v2 that is clear in v1, return false */
853 if (~v1->addr[idx] & v2->addr[idx]) {
854 return 0;
855 }
856 }
857
858 return 1;
859}
860
861/**
862 * Boolean test if two values agree when restricted to a mask
863 */
864static inline int
Andreas Wundsam53256162013-05-02 14:05:53 -0700865of_restricted_match_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
Rich Lanea06d0c32013-03-25 08:52:03 -0700866 of_mac_addr_t *mask) {
867 int idx;
868
869 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700870 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700871 (v2->addr[idx] & mask->addr[idx])) {
872 return 0;
873 }
874 }
875
876 return 1;
877}
878
879/**
880 * Boolean test if two values "overlap" (agree on common masks)
881 */
882
883static inline int
884of_overlap_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
885 of_mac_addr_t *m1, of_mac_addr_t *m2) {
886 int idx;
887
888 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700889 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700890 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
891 return 0;
892 }
893 }
894
895 return 1;
896}
897
898#define OF_MORE_SPECIFIC_MAC_ADDR(v1, v2) of_more_specific_mac_addr((v1), (v2))
899
900#define OF_RESTRICTED_MATCH_MAC_ADDR(v1, v2, mask) \\
901 of_restricted_match_mac_addr((v1), (v2), (mask))
902
903#define OF_OVERLAP_MAC_ADDR(v1, v2, m1, m2) \\
904 of_overlap_mac_addr((v1), (v2), (m1), (m2))
905
Rich Lane3b2fd832013-09-24 13:44:08 -0700906#define OF_MORE_SPECIFIC_BITMAP_128(v1, v2) \\
907 (OF_MORE_SPECIFIC_INT((v1)->lo, (v2)->lo) && OF_MORE_SPECIFIC_INT((v1)->hi, (v2)->hi))
908
909#define OF_RESTRICTED_MATCH_BITMAP_128(v1, v2, mask) \\
910 (OF_RESTRICTED_MATCH_INT((v1)->lo, (v2)->lo, (mask)->lo) && OF_RESTRICTED_MATCH_INT((v1)->hi, (v2)->hi, (mask)->hi))
911
912#define OF_OVERLAP_BITMAP_128(v1, v2, m1, m2) \\
913 (OF_OVERLAP_INT((v1)->lo, (v2)->lo, (m1)->lo, (m2)->lo) && OF_OVERLAP_INT((v1)->hi, (v2)->hi, (m1)->hi, (m2)->hi))
914
Rich Lanea06d0c32013-03-25 08:52:03 -0700915/**
916 * More-specific-than macro for integer types; see above
917 * @return true if v1 is equal to or more specific than v2
918 *
919 * If there is a bit that is set in v2 and not in v1, return false.
920 */
921#define OF_MORE_SPECIFIC_INT(v1, v2) (!(~(v1) & (v2)))
922
923/**
924 * Boolean test if two values agree when restricted to a mask
925 */
926#define OF_RESTRICTED_MATCH_INT(v1, v2, mask) \\
927 (((v1) & (mask)) == ((v2) & (mask)))
928
929
930#define OF_OVERLAP_INT(v1, v2, m1, m2) \\
931 ((((v1) & (m1)) & (m2)) == (((v2) & (m1)) & (m2)))
932""")
933
934 out.write("""
935/**
936 * Compare two match structures for exact equality
937 *
938 * We just do memcmp assuming structs were memset to 0 on init
939 */
940static inline int
941of_match_eq(of_match_t *match1, of_match_t *match2)
942{
943 return (MEMCMP(match1, match2, sizeof(of_match_t)) == 0);
944}
945
946/**
947 * Is the entry match more specific than (or equal to) the query match?
948 * @param entry Match expected to be more specific (subset of query)
949 * @param query Match expected to be less specific (superset of entry)
950 * @returns Boolean, see below
951 *
952 * The assumption is that a query is being done for a non-strict
953 * match against an entry in a table. The result is true if the
954 * entry match indicates a more specific (but compatible) flow space
955 * specification than that in the query match. This means that the
956 * values agree between the two where they overlap, and that each mask
957 * for the entry is more specific than that of the query.
958 *
959 * The query has the less specific mask (fewer mask bits) so it is
960 * used for the mask when checking values.
961 */
962
963static inline int
964of_match_more_specific(of_match_t *entry, of_match_t *query)
965{
966 of_match_fields_t *q_m, *e_m; /* Short hand for masks, fields */
967 of_match_fields_t *q_f, *e_f;
968
969 q_m = &query->masks;
970 e_m = &entry->masks;
971 q_f = &query->fields;
972 e_f = &entry->fields;
973""")
974 for key, entry in match.of_match_members.items():
975 q_m = "&q_m->%s" % key
976 e_m = "&e_m->%s" % key
977 q_f = "&q_f->%s" % key
978 e_f = "&e_f->%s" % key
979 if entry["m_type"] == "of_ipv6_t":
980 comp = "OF_MORE_SPECIFIC_IPV6"
981 match_type = "OF_RESTRICTED_MATCH_IPV6"
982 elif entry["m_type"] == "of_mac_addr_t":
983 comp = "OF_MORE_SPECIFIC_MAC_ADDR"
984 match_type = "OF_RESTRICTED_MATCH_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -0700985 elif entry["m_type"] == "of_bitmap_128_t":
986 comp = "OF_MORE_SPECIFIC_BITMAP_128"
987 match_type = "OF_RESTRICTED_MATCH_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -0700988 else: # Integer
989 comp = "OF_MORE_SPECIFIC_INT"
990 match_type = "OF_RESTRICTED_MATCH_INT"
991 q_m = "q_m->%s" % key
992 e_m = "e_m->%s" % key
993 q_f = "q_f->%s" % key
994 e_f = "e_f->%s" % key
995 out.write("""
996 /* Mask and values for %(key)s */
997 if (!%(comp)s(%(e_m)s, %(q_m)s)) {
998 return 0;
999 }
1000 if (!%(match_type)s(%(e_f)s, %(q_f)s,
1001 %(q_m)s)) {
1002 return 0;
1003 }
Andreas Wundsam53256162013-05-02 14:05:53 -07001004""" % dict(match_type=match_type, comp=comp, q_f=q_f, e_f=e_f,
Rich Lanea06d0c32013-03-25 08:52:03 -07001005 q_m=q_m, e_m=e_m, key=key))
1006
1007 out.write("""
1008 return 1;
1009}
1010""")
1011
1012 out.write("""
1013
1014/**
1015 * Do two entries overlap?
1016 * @param match1 One match struct
1017 * @param match2 Another match struct
1018 * @returns Boolean: true if there is a packet that would match both
1019 *
1020 */
1021
1022static inline int
1023of_match_overlap(of_match_t *match1, of_match_t *match2)
1024{
1025 of_match_fields_t *m1, *m2; /* Short hand for masks, fields */
1026 of_match_fields_t *f1, *f2;
1027
1028 m1 = &match1->masks;
1029 m2 = &match2->masks;
1030 f1 = &match1->fields;
1031 f2 = &match2->fields;
1032""")
1033 for key, entry in match.of_match_members.items():
1034 m1 = "&m1->%s" % key
1035 m2 = "&m2->%s" % key
1036 f1 = "&f1->%s" % key
1037 f2 = "&f2->%s" % key
1038 if entry["m_type"] == "of_ipv6_t":
1039 check = "OF_OVERLAP_IPV6"
1040 elif entry["m_type"] == "of_mac_addr_t":
1041 check = "OF_OVERLAP_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -07001042 elif entry["m_type"] == "of_bitmap_128_t":
1043 check = "OF_OVERLAP_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -07001044 else: # Integer
1045 check = "OF_OVERLAP_INT"
1046 m1 = "m1->%s" % key
1047 m2 = "m2->%s" % key
1048 f1 = "f1->%s" % key
1049 f2 = "f2->%s" % key
1050 out.write("""
1051 /* Check overlap for %(key)s */
Andreas Wundsam53256162013-05-02 14:05:53 -07001052 if (!%(check)s(%(f1)s, %(f2)s,
Rich Lanea06d0c32013-03-25 08:52:03 -07001053 %(m2)s, %(m1)s)) {
1054 return 0; /* This field differentiates; all done */
1055 }
1056""" % dict(check=check, f1=f1, f2=f2, m1=m1, m2=m2, key=key))
1057
1058 out.write("""
1059 return 1; /* No field differentiates matches */
1060}
1061""")
1062
1063def gen_match_conversions(out=sys.stdout):
1064 match.match_sanity_check()
Rich Lanea06d0c32013-03-25 08:52:03 -07001065 out.write("""
1066/**
Rich Lanea06d0c32013-03-25 08:52:03 -07001067 * @brief Return the index (used as the WC field in 1.0 match) given the mask
1068 */
1069
1070int
1071of_ip_mask_to_index(uint32_t mask)
1072{
1073 int idx;
Rich Lane70c70942014-05-06 15:31:47 -07001074 uint32_t cmask;
Rich Lanea06d0c32013-03-25 08:52:03 -07001075
1076 /* Handle most common cases directly */
Rich Lane70c70942014-05-06 15:31:47 -07001077 if (mask == 0) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001078 return 63;
1079 }
Rich Lane70c70942014-05-06 15:31:47 -07001080 if (mask == 0xffffffff) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001081 return 0;
1082 }
1083
Rich Lane70c70942014-05-06 15:31:47 -07001084 if ((~mask + 1) & ~mask) {
1085 LOCI_LOG_INFO("OF 1.0: Could not map IP addr mask 0x%x", mask);
1086 return 63;
Rich Lanea06d0c32013-03-25 08:52:03 -07001087 }
1088
Rich Lane70c70942014-05-06 15:31:47 -07001089 idx = 0;
1090 cmask = ~mask;
1091 while (cmask) {
1092 cmask >>= 1;
1093 idx += 1;
1094 }
1095
1096 return idx;
Rich Lanea06d0c32013-03-25 08:52:03 -07001097}
1098
1099/**
1100 * @brief Return the mask for the given index
1101 */
1102
1103uint32_t
1104of_ip_index_to_mask(int index)
1105{
Rich Lane70c70942014-05-06 15:31:47 -07001106 if (index >= 32) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001107 return 0;
Rich Lane70c70942014-05-06 15:31:47 -07001108 } else {
1109 return 0xffffffff << index;
Rich Lanea06d0c32013-03-25 08:52:03 -07001110 }
Rich Lanea06d0c32013-03-25 08:52:03 -07001111}
1112
1113""")
Rich Lanea06d0c32013-03-25 08:52:03 -07001114 gen_unified_match_to_v1(out)
1115 gen_unified_match_to_v2(out)
1116 gen_unified_match_to_v3(out)
1117 gen_v1_to_unified_match(out)
1118 gen_v2_to_unified_match(out)
1119 gen_v3_to_unified_match(out)