blob: bc05747b81e379c0f7005c5575f46c07f061a31c [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,
80 of_octets_t *octets);
81extern int of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst);
82extern int of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst);
83extern int of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst);
84extern int of_match_to_wire_match_v1(of_match_t *src, of_match_v1_t *dst);
85extern int of_match_to_wire_match_v2(of_match_t *src, of_match_v2_t *dst);
86extern int of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst);
87""")
88
89def gen_v4_match_compat(out):
90 """
91 Code for coercing version 1.3 matches to 1.2 matches
92
93 @FIXME This is a stopgap and needs to get cleaned up.
94 """
95 out.write("""
96/**
97 * Definitions to coerce v4 match (version 1.3) to v3 matches
98 * (version 1.2).
99 * @FIXME This is a stopgap and needs to get cleaned up.
100 */
101#define of_match_v4_t of_match_v3_t
102#define of_match_v4_init of_match_v3_init
103#define of_match_v4_new of_match_v3_new
104#define of_match_v4_to_match of_match_v3_to_match
105#define of_match_to_wire_match_v4 of_match_to_wire_match_v3
106#define of_match_v4_delete of_match_v3_delete
107""")
108
109def gen_match_macros(out):
110 out.write("""
111
112/**
113 * Definitions for wildcard macros for OF_VERSION_1_0
114 */
115
116""")
117 for key in match.of_v1_keys:
118 entry = match.of_match_members[key]
119 if "v1_wc_shift" in entry:
120 if key in ["ipv4_src", "ipv4_dst"]:
121 out.write("""
122#define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d
123#define OF_MATCH_V1_WC_%(ku)s_MASK (0x3f << %(val)d)
124#define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(0x3f << %(val)d))
125#define OF_MATCH_V1_WC_%(ku)s_SET(wc, value) do { \\
126 OF_MATCH_V1_WC_%(ku)s_CLEAR(wc); \\
127 ((wc) |= (((value) & 0x3f) << %(val)d)); \\
128 } while (0)
129#define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (0x3f << %(val)d))
130#define OF_MATCH_V1_WC_%(ku)s_GET(wc) (((wc) >> %(val)d) & 0x3f)
131""" % dict(ku=key.upper(), val=entry["v1_wc_shift"]))
132 else:
133 out.write("""
134#define OF_MATCH_V1_WC_%(ku)s_SHIFT %(val)d
135#define OF_MATCH_V1_WC_%(ku)s_MASK (1 << %(val)d)
136#define OF_MATCH_V1_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d))
137#define OF_MATCH_V1_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d))
138#define OF_MATCH_V1_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d))
139""" % dict(ku=key.upper(), val=entry["v1_wc_shift"]))
140
141 out.write("""
142
143/**
144 * Definitions for wildcard macros for OF_VERSION_1_1
145 */
146""")
147
148 for key in sorted(match.of_v2_keys):
149 entry = match.of_match_members[key]
150 if "v2_wc_shift" in entry:
151 out.write("""
152#define OF_MATCH_V2_WC_%(ku)s_SHIFT %(val)d
153#define OF_MATCH_V2_WC_%(ku)s_MASK (1 << %(val)d)
154#define OF_MATCH_V2_WC_%(ku)s_SET(wc) ((wc) |= (1 << %(val)d))
155#define OF_MATCH_V2_WC_%(ku)s_CLEAR(wc) ((wc) &= ~(1 << %(val)d))
156#define OF_MATCH_V2_WC_%(ku)s_TEST(wc) ((wc) & (1 << %(val)d))
157""" % dict(ku=key.upper(), val=entry["v2_wc_shift"]))
158
159
160def gen_match_struct(out=sys.stdout):
161 out.write("/* Unified, flat OpenFlow match structure based on OF 1.2 */\n")
162 out.write("typedef struct of_match_fields_s {\n")
163 out.write(" /* Version 1.2 is used for field names */\n")
164 for name in match.match_keys_sorted:
165 entry = match.of_match_members[name]
166 out.write(" %-20s %s;\n" % (entry["m_type"], entry["name"]))
167 out.write("""
168} of_match_fields_t;
169
170/**
171 * @brief The LOCI match structure.
172 */
173
174typedef struct of_match_s {
175 of_version_t version;
176 of_match_fields_t fields;
177 of_match_fields_t masks;
178} of_match_t;
179
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,
741 of_octets_t *octets)
742{
743 if (octets->bytes == 0) { /* No match specified means all wildcards */
744 MEMSET(match, 0, sizeof(*match));
745 match->version = version;
746
747 return OF_ERROR_NONE;
748 }
749
750 switch (version) {
751""")
752 for version in of_g.of_version_range:
753 out.write("""
754 case %(ver_name)s:
755 { /* FIXME: check init bytes */
756 uint8_t *tmp;
757 of_match_v%(version)d_t wire_match;
758 of_match_v%(version)d_init(&wire_match,
759 %(ver_name)s, -1, 1);
Andreas Wundsam53256162013-05-02 14:05:53 -0700760 of_object_buffer_bind((of_object_t *)&wire_match,
Rich Lanea06d0c32013-03-25 08:52:03 -0700761 octets->data, octets->bytes, NULL);
762 OF_TRY(of_match_v%(version)d_to_match(&wire_match, match));
763
764 /* Free the wire buffer control block without freeing
765 * octets->bytes. */
Rich Lanecdd542d2014-04-03 16:13:12 -0700766 of_wire_buffer_steal(wire_match.wbuf, &tmp);
Rich Lanea06d0c32013-03-25 08:52:03 -0700767 }
768 break;
769""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
770
771 out.write("""
772 default:
773 return OF_ERROR_COMPAT;
774 }
775
776 return OF_ERROR_NONE;
777}
778""")
779
780def gen_match_comp(out=sys.stdout):
781 """
782 Generate match comparison functions
783 """
784 out.write("""
785/**
786 * Determine "more specific" relationship between mac addrs
787 * @return true if v1 is equal to or more specific than v2
788 *
789 * @todo Could be optimized
790 *
791 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
792 * That is, return false if there is a bit set in v2 and not in v1.
793 */
794
795static inline int
796of_more_specific_ipv6(of_ipv6_t *v1, of_ipv6_t *v2) {
797 int idx;
798
799 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
800 /* If there's a bit set in v2 that is clear in v1, return false */
801 if (~v1->addr[idx] & v2->addr[idx]) {
802 return 0;
803 }
804 }
805
806 return 1;
807}
808
809/**
810 * Boolean test if two values agree when restricted to a mask
811 */
812
813static inline int
814of_restricted_match_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, of_ipv6_t *mask) {
815 int idx;
816
817 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700818 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700819 (v2->addr[idx] & mask->addr[idx])) {
820 return 0;
821 }
822 }
823
824 return 1;
825}
826
827/**
828 * Boolean test if two values "overlap" (agree on common masks)
829 */
830
831static inline int
832of_overlap_ipv6(of_ipv6_t *v1, of_ipv6_t *v2,
833 of_ipv6_t *m1, of_ipv6_t *m2) {
834 int idx;
835
836 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700837 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700838 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
839 return 0;
840 }
841 }
842
843 return 1;
844}
845
846#define OF_MORE_SPECIFIC_IPV6(v1, v2) of_more_specific_ipv6((v1), (v2))
847
848#define OF_RESTRICTED_MATCH_IPV6(v1, v2, mask) \\
849 of_restricted_match_ipv6((v1), (v2), (mask))
850
851#define OF_OVERLAP_IPV6(v1, v2, m1, m2) of_overlap_ipv6((v1), (v2), (m1), (m2))
852
853/**
854 * Determine "more specific" relationship between mac addrs
855 * @return true if v1 is equal to or more specific than v2
856 *
857 * @todo Could be optimized
858 *
859 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
860 * That is, return false if there is a bit set in v2 and not in v1.
861 */
862static inline int
863of_more_specific_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2) {
864 int idx;
865
866 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
867 /* If there's a bit set in v2 that is clear in v1, return false */
868 if (~v1->addr[idx] & v2->addr[idx]) {
869 return 0;
870 }
871 }
872
873 return 1;
874}
875
876/**
877 * Boolean test if two values agree when restricted to a mask
878 */
879static inline int
Andreas Wundsam53256162013-05-02 14:05:53 -0700880of_restricted_match_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
Rich Lanea06d0c32013-03-25 08:52:03 -0700881 of_mac_addr_t *mask) {
882 int idx;
883
884 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700885 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700886 (v2->addr[idx] & mask->addr[idx])) {
887 return 0;
888 }
889 }
890
891 return 1;
892}
893
894/**
895 * Boolean test if two values "overlap" (agree on common masks)
896 */
897
898static inline int
899of_overlap_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
900 of_mac_addr_t *m1, of_mac_addr_t *m2) {
901 int idx;
902
903 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700904 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700905 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
906 return 0;
907 }
908 }
909
910 return 1;
911}
912
913#define OF_MORE_SPECIFIC_MAC_ADDR(v1, v2) of_more_specific_mac_addr((v1), (v2))
914
915#define OF_RESTRICTED_MATCH_MAC_ADDR(v1, v2, mask) \\
916 of_restricted_match_mac_addr((v1), (v2), (mask))
917
918#define OF_OVERLAP_MAC_ADDR(v1, v2, m1, m2) \\
919 of_overlap_mac_addr((v1), (v2), (m1), (m2))
920
Rich Lane3b2fd832013-09-24 13:44:08 -0700921#define OF_MORE_SPECIFIC_BITMAP_128(v1, v2) \\
922 (OF_MORE_SPECIFIC_INT((v1)->lo, (v2)->lo) && OF_MORE_SPECIFIC_INT((v1)->hi, (v2)->hi))
923
924#define OF_RESTRICTED_MATCH_BITMAP_128(v1, v2, mask) \\
925 (OF_RESTRICTED_MATCH_INT((v1)->lo, (v2)->lo, (mask)->lo) && OF_RESTRICTED_MATCH_INT((v1)->hi, (v2)->hi, (mask)->hi))
926
927#define OF_OVERLAP_BITMAP_128(v1, v2, m1, m2) \\
928 (OF_OVERLAP_INT((v1)->lo, (v2)->lo, (m1)->lo, (m2)->lo) && OF_OVERLAP_INT((v1)->hi, (v2)->hi, (m1)->hi, (m2)->hi))
929
Rich Lanea06d0c32013-03-25 08:52:03 -0700930/**
931 * More-specific-than macro for integer types; see above
932 * @return true if v1 is equal to or more specific than v2
933 *
934 * If there is a bit that is set in v2 and not in v1, return false.
935 */
936#define OF_MORE_SPECIFIC_INT(v1, v2) (!(~(v1) & (v2)))
937
938/**
939 * Boolean test if two values agree when restricted to a mask
940 */
941#define OF_RESTRICTED_MATCH_INT(v1, v2, mask) \\
942 (((v1) & (mask)) == ((v2) & (mask)))
943
944
945#define OF_OVERLAP_INT(v1, v2, m1, m2) \\
946 ((((v1) & (m1)) & (m2)) == (((v2) & (m1)) & (m2)))
947""")
948
949 out.write("""
950/**
951 * Compare two match structures for exact equality
952 *
953 * We just do memcmp assuming structs were memset to 0 on init
954 */
955static inline int
956of_match_eq(of_match_t *match1, of_match_t *match2)
957{
958 return (MEMCMP(match1, match2, sizeof(of_match_t)) == 0);
959}
960
961/**
962 * Is the entry match more specific than (or equal to) the query match?
963 * @param entry Match expected to be more specific (subset of query)
964 * @param query Match expected to be less specific (superset of entry)
965 * @returns Boolean, see below
966 *
967 * The assumption is that a query is being done for a non-strict
968 * match against an entry in a table. The result is true if the
969 * entry match indicates a more specific (but compatible) flow space
970 * specification than that in the query match. This means that the
971 * values agree between the two where they overlap, and that each mask
972 * for the entry is more specific than that of the query.
973 *
974 * The query has the less specific mask (fewer mask bits) so it is
975 * used for the mask when checking values.
976 */
977
978static inline int
979of_match_more_specific(of_match_t *entry, of_match_t *query)
980{
981 of_match_fields_t *q_m, *e_m; /* Short hand for masks, fields */
982 of_match_fields_t *q_f, *e_f;
983
984 q_m = &query->masks;
985 e_m = &entry->masks;
986 q_f = &query->fields;
987 e_f = &entry->fields;
988""")
989 for key, entry in match.of_match_members.items():
990 q_m = "&q_m->%s" % key
991 e_m = "&e_m->%s" % key
992 q_f = "&q_f->%s" % key
993 e_f = "&e_f->%s" % key
994 if entry["m_type"] == "of_ipv6_t":
995 comp = "OF_MORE_SPECIFIC_IPV6"
996 match_type = "OF_RESTRICTED_MATCH_IPV6"
997 elif entry["m_type"] == "of_mac_addr_t":
998 comp = "OF_MORE_SPECIFIC_MAC_ADDR"
999 match_type = "OF_RESTRICTED_MATCH_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -07001000 elif entry["m_type"] == "of_bitmap_128_t":
1001 comp = "OF_MORE_SPECIFIC_BITMAP_128"
1002 match_type = "OF_RESTRICTED_MATCH_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -07001003 else: # Integer
1004 comp = "OF_MORE_SPECIFIC_INT"
1005 match_type = "OF_RESTRICTED_MATCH_INT"
1006 q_m = "q_m->%s" % key
1007 e_m = "e_m->%s" % key
1008 q_f = "q_f->%s" % key
1009 e_f = "e_f->%s" % key
1010 out.write("""
1011 /* Mask and values for %(key)s */
1012 if (!%(comp)s(%(e_m)s, %(q_m)s)) {
1013 return 0;
1014 }
1015 if (!%(match_type)s(%(e_f)s, %(q_f)s,
1016 %(q_m)s)) {
1017 return 0;
1018 }
Andreas Wundsam53256162013-05-02 14:05:53 -07001019""" % dict(match_type=match_type, comp=comp, q_f=q_f, e_f=e_f,
Rich Lanea06d0c32013-03-25 08:52:03 -07001020 q_m=q_m, e_m=e_m, key=key))
1021
1022 out.write("""
1023 return 1;
1024}
1025""")
1026
1027 out.write("""
1028
1029/**
1030 * Do two entries overlap?
1031 * @param match1 One match struct
1032 * @param match2 Another match struct
1033 * @returns Boolean: true if there is a packet that would match both
1034 *
1035 */
1036
1037static inline int
1038of_match_overlap(of_match_t *match1, of_match_t *match2)
1039{
1040 of_match_fields_t *m1, *m2; /* Short hand for masks, fields */
1041 of_match_fields_t *f1, *f2;
1042
1043 m1 = &match1->masks;
1044 m2 = &match2->masks;
1045 f1 = &match1->fields;
1046 f2 = &match2->fields;
1047""")
1048 for key, entry in match.of_match_members.items():
1049 m1 = "&m1->%s" % key
1050 m2 = "&m2->%s" % key
1051 f1 = "&f1->%s" % key
1052 f2 = "&f2->%s" % key
1053 if entry["m_type"] == "of_ipv6_t":
1054 check = "OF_OVERLAP_IPV6"
1055 elif entry["m_type"] == "of_mac_addr_t":
1056 check = "OF_OVERLAP_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -07001057 elif entry["m_type"] == "of_bitmap_128_t":
1058 check = "OF_OVERLAP_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -07001059 else: # Integer
1060 check = "OF_OVERLAP_INT"
1061 m1 = "m1->%s" % key
1062 m2 = "m2->%s" % key
1063 f1 = "f1->%s" % key
1064 f2 = "f2->%s" % key
1065 out.write("""
1066 /* Check overlap for %(key)s */
Andreas Wundsam53256162013-05-02 14:05:53 -07001067 if (!%(check)s(%(f1)s, %(f2)s,
Rich Lanea06d0c32013-03-25 08:52:03 -07001068 %(m2)s, %(m1)s)) {
1069 return 0; /* This field differentiates; all done */
1070 }
1071""" % dict(check=check, f1=f1, f2=f2, m1=m1, m2=m2, key=key))
1072
1073 out.write("""
1074 return 1; /* No field differentiates matches */
1075}
1076""")
1077
1078def gen_match_conversions(out=sys.stdout):
1079 match.match_sanity_check()
Rich Lanea06d0c32013-03-25 08:52:03 -07001080 out.write("""
1081/**
Rich Lanea06d0c32013-03-25 08:52:03 -07001082 * @brief Return the index (used as the WC field in 1.0 match) given the mask
1083 */
1084
1085int
1086of_ip_mask_to_index(uint32_t mask)
1087{
1088 int idx;
Rich Lane70c70942014-05-06 15:31:47 -07001089 uint32_t cmask;
Rich Lanea06d0c32013-03-25 08:52:03 -07001090
1091 /* Handle most common cases directly */
Rich Lane70c70942014-05-06 15:31:47 -07001092 if (mask == 0) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001093 return 63;
1094 }
Rich Lane70c70942014-05-06 15:31:47 -07001095 if (mask == 0xffffffff) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001096 return 0;
1097 }
1098
Rich Lane70c70942014-05-06 15:31:47 -07001099 if ((~mask + 1) & ~mask) {
1100 LOCI_LOG_INFO("OF 1.0: Could not map IP addr mask 0x%x", mask);
1101 return 63;
Rich Lanea06d0c32013-03-25 08:52:03 -07001102 }
1103
Rich Lane70c70942014-05-06 15:31:47 -07001104 idx = 0;
1105 cmask = ~mask;
1106 while (cmask) {
1107 cmask >>= 1;
1108 idx += 1;
1109 }
1110
1111 return idx;
Rich Lanea06d0c32013-03-25 08:52:03 -07001112}
1113
1114/**
1115 * @brief Return the mask for the given index
1116 */
1117
1118uint32_t
1119of_ip_index_to_mask(int index)
1120{
Rich Lane70c70942014-05-06 15:31:47 -07001121 if (index >= 32) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001122 return 0;
Rich Lane70c70942014-05-06 15:31:47 -07001123 } else {
1124 return 0xffffffff << index;
Rich Lanea06d0c32013-03-25 08:52:03 -07001125 }
Rich Lanea06d0c32013-03-25 08:52:03 -07001126}
1127
1128""")
Rich Lanea06d0c32013-03-25 08:52:03 -07001129 gen_unified_match_to_v1(out)
1130 gen_unified_match_to_v2(out)
1131 gen_unified_match_to_v3(out)
1132 gen_v1_to_unified_match(out)
1133 gen_v2_to_unified_match(out)
1134 gen_v3_to_unified_match(out)