blob: e93adaf7d36e0e4c498aca3548ce0c69f42c00b5 [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 Lanea06d0c32013-03-25 08:52:03 -0700197 * IP Mask map. IP maks wildcards from OF 1.0 are interpretted as
198 * indices into the map below.
199 *
200 * of_ip_mask_map: Array mapping index to mask
201 * of_ip_mask_use_map: Boolean indication set when map is initialized
202 * of_ip_mask_map_init: Initialize to default values; set "use map".
203 */
204#define OF_IP_MASK_MAP_COUNT 64
205extern uint32_t of_ip_mask_map[OF_IP_MASK_MAP_COUNT];
206extern int of_ip_mask_map_init_done;
207
208#define OF_IP_MASK_INIT_CHECK \
209 if (!of_ip_mask_map_init_done) of_ip_mask_map_init()
210
211/**
212 * Initialize map
213 */
214extern void of_ip_mask_map_init(void);
215
216extern int of_ip_mask_map_set(int index, uint32_t mask);
217extern int of_ip_mask_map_get(int index, uint32_t *mask);
218
219/**
220 * @brief Map from mask to index
221 */
222
223extern int of_ip_mask_to_index(uint32_t mask);
224
225/**
226 * @brief Map from index to mask
227 */
228
229extern uint32_t of_ip_index_to_mask(int index);
230
231/**
232 * The signalling of an untagged packet varies by OF version.
233 * Use this macro to set the field value.
234 */
235#define OF_MATCH_UNTAGGED_VLAN_ID(version) \\
236 ((version) == OF_VERSION_1_0 ? 0xffff : \\
237 ((version) == OF_VERSION_1_1 ? 0xffff : 0))
238
239/**
240 * Version 1.1 had the notion of "any" vlan but must be set
241 */
242#define OF_MATCH_VLAN_TAG_PRESENT_ANY_ID(version) \\
243 ((version) == OF_VERSION_1_0 ? 0 /* @fixme */ : \\
244 ((version) == OF_VERSION_1_1 ? 0xfffe : 0x1000))
245""")
246
247def gen_oxm_defines(out):
248 """
249 Generate verbatim definitions for OXM
250 """
251 out.write("""
Rich Lanea06d0c32013-03-25 08:52:03 -0700252/*
253 * The generic match structure uses the OXM bit indices for it's
254 * bitmasks for active and masked values
255 */
256""")
257 for key, entry in match.of_match_members.items():
258 out.write("""
259/* Mask/value check/set macros for %(key)s */
260
261/**
262 * Set the mask for an exact match of %(key)s
263 */
264#define OF_MATCH_MASK_%(ku)s_EXACT_SET(_match) \\
265 MEMSET(&(_match)->masks.%(key)s, 0xff, \\
266 sizeof(((_match)->masks).%(key)s))
267
268/**
269 * Clear the mask for %(key)s making that field inactive for the match
270 */
271#define OF_MATCH_MASK_%(ku)s_CLEAR(_match) \\
272 MEMSET(&(_match)->masks.%(key)s, 0, \\
273 sizeof(((_match)->masks).%(key)s))
274
275/**
276 * Test whether the match is exact for %(key)s
277 */
278#define OF_MATCH_MASK_%(ku)s_EXACT_TEST(_match) \\
279 OF_VARIABLE_IS_ALL_ONES(&(((_match)->masks).%(key)s))
280
281/**
282 * Test whether key %(key)s is being checked in the match
283 */
284#define OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(_match) \\
285 OF_VARIABLE_IS_NON_ZERO(&(((_match)->masks).%(key)s))
286
Rich Laned56f8d22014-05-06 14:52:55 -0700287""" % dict(key=key, ku=key.upper()))
Rich Lanea06d0c32013-03-25 08:52:03 -0700288
Rich Lanea06d0c32013-03-25 08:52:03 -0700289def gen_unified_match_to_v1(out):
290 """
291 Generate C code to convert a unified match structure to a V1 match struct
292 @param out The output file handle
293 """
294
295 out.write("""
296/**
297 * Check if match is compatible with OF 1.0
298 * @param match The match being checked
299 */
300static inline int
301of_match_v1_compat_check(of_match_t *match)
302{
303""")
304 for key in match.of_match_members:
305 if key in match.of_v1_keys:
306 continue
307 out.write("""
308 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
309 return 0;
310 }
311""" % dict(ku=key.upper()))
312
313 out.write("""
314 return 1;
315}
316""")
317
318 out.write("""
319/**
320 * Convert a generic match object to an OF_VERSION_1_0 object
321 * @param src Pointer to the generic match object source
322 * @param dst Pointer to the OF 1.0 wire structure
323 *
324 * The wire structure is initialized by this function if it doesn't
325 * not have the proper object ID.
326 */
327
328int
329of_match_to_wire_match_v1(of_match_t *src, of_match_v1_t *dst)
330{
331 of_wc_bmap_t wildcards = 0;
332 int ip_mask_index;
333
334 if ((src == NULL) || (dst == NULL)) {
335 return OF_ERROR_PARAM;
336 }
337 if (!of_match_v1_compat_check(src)) {
338 return OF_ERROR_COMPAT;
339 }
340 if (dst->object_id != OF_MATCH_V1) {
341 of_match_v1_init(dst, OF_VERSION_1_0, 0, 0);
342 }
343""")
344 for key in sorted(match.of_v1_keys):
345 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
346 out.write("""
347 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
348 ip_mask_index = of_ip_mask_to_index(src->masks.%(key)s);
349 of_match_v1_%(key)s_set(dst, src->fields.%(key)s);
350 } else { /* Wildcarded, look for 0 mask */
351 ip_mask_index = of_ip_mask_to_index(0);
352 }
353 OF_MATCH_V1_WC_%(ku)s_SET(wildcards, ip_mask_index);
354""" % dict(key=key, ku=key.upper()))
355 else:
356 out.write("""
357 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
358 of_match_v1_%(key)s_set(dst, src->fields.%(key)s);
359 } else {
360 OF_MATCH_V1_WC_%(ku)s_SET(wildcards);
361 }
362""" % dict(key=key, ku=key.upper()))
363
364 out.write("""
365 of_match_v1_wildcards_set(dst, wildcards);
366
367 return OF_ERROR_NONE;
368}
369""")
370
371def all_ones_mask(d_type):
372 if d_type == "of_mac_addr_t":
373 return "of_mac_addr_all_ones"
374 else:
375 return "((%s) -1)" % d_type
376
377def gen_unified_match_to_v2(out):
378 """
379 Generate C code to convert a unified match structure to a V2 match struct
380 @param out The output file handle
381 """
382
383 out.write("""
384/**
385 * Check if match is compatible with OF 1.0
386 * @param match The match being checked
387 */
388static inline int
389of_match_v2_compat_check(of_match_t *match)
390{
391""")
392 for key in match.of_match_members:
393 if key in match.of_v2_keys:
394 continue
395 out.write("""
396 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
397 return 0;
398 }
399""" % dict(ku=key.upper()))
400
401 out.write("""
402 return 1;
403}
404""")
405
406 out.write("""
407/**
408 * Convert a generic match object to an OF_VERSION_1_1 object
409 * @param src Pointer to the generic match object source
410 * @param dst Pointer to the OF 1.1 wire structure
411 *
412 * The wire structure is initialized by this function.
413 */
414
415int
416of_match_to_wire_match_v2(of_match_t *src, of_match_v2_t *dst)
417{
418 of_wc_bmap_t wildcards = 0;
419
420 if ((src == NULL) || (dst == NULL)) {
421 return OF_ERROR_PARAM;
422 }
423 if (!of_match_v2_compat_check(src)) {
424 return OF_ERROR_COMPAT;
425 }
426 if (dst->object_id != OF_MATCH_V2) {
427 of_match_v2_init(dst, OF_VERSION_1_1, 0, 0);
428 }
429""")
430 for key in match.of_v2_keys:
431 if key in match.of_v2_full_mask:
432 ones_mask = all_ones_mask(match.of_match_members[key]["m_type"])
433 out.write("""
434 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
435 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
436 of_match_v2_%(key)s_mask_set(dst,
437 src->masks.%(key)s);
438 } else { /* Exact match; use all ones mask */
439 of_match_v2_%(key)s_mask_set(dst,
440 %(ones_mask)s);
441 }
442 of_match_v2_%(key)s_set(dst, src->fields.%(key)s);
443 }
444
445""" % dict(key=key, ku=key.upper(), ones_mask=ones_mask))
446 else:
447 out.write("""
448 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
449 return OF_ERROR_COMPAT;
450 }
451 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
452 of_match_v2_%(key)s_set(dst, src->fields.%(key)s);
453 } else {
454 OF_MATCH_V2_WC_%(ku)s_SET(wildcards);
455 }
456""" % dict(key=key, ku=key.upper(),
457 wc_bit="OF_MATCH_WC_V2_%s" % key.upper()))
458
459 out.write("""
460 of_match_v2_wildcards_set(dst, wildcards);
461
462 return OF_ERROR_NONE;
463}
464""")
465
466def gen_unified_match_to_v3(out):
467 """
468 Generate C code to convert a unified match structure to a V3 match
469
470 This is much easier as the unified struct is based on V3
471 @param out The output file handle
472 """
473 out.write("""
474static int
475populate_oxm_list(of_match_t *src, of_list_oxm_t *oxm_list)
476{
477 of_oxm_t oxm_entry;
478
479 /* For each active member, add an OXM entry to the list */
480""")
Rich Lane4964d542013-10-14 18:13:47 -0700481 for key in match.match_keys_sorted:
Rich Lanea06d0c32013-03-25 08:52:03 -0700482 out.write("""\
483 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
484 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
485 of_oxm_%(key)s_masked_t *elt;
486 elt = &oxm_entry.%(key)s_masked;
487
488 of_oxm_%(key)s_masked_init(elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700489 oxm_list->version, -1, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700490 of_list_oxm_append_bind(oxm_list, &oxm_entry);
Andreas Wundsam53256162013-05-02 14:05:53 -0700491 of_oxm_%(key)s_masked_value_set(elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700492 src->fields.%(key)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700493 of_oxm_%(key)s_masked_value_mask_set(elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700494 src->masks.%(key)s);
495 } else { /* Active, but not masked */
496 of_oxm_%(key)s_t *elt;
497 elt = &oxm_entry.%(key)s;
498 of_oxm_%(key)s_init(elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700499 oxm_list->version, -1, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700500 of_list_oxm_append_bind(oxm_list, &oxm_entry);
501 of_oxm_%(key)s_value_set(elt, src->fields.%(key)s);
502 }
503 }
504""" % dict(key=key, ku=key.upper()))
505 out.write("""
506 return OF_ERROR_NONE;
507}
508
509/**
510 * Convert a generic match object to an OF_VERSION_1_2 object
511 * @param src Pointer to the generic match object source
512 * @param dst Pointer to the OF 1.2 wire structure
513 *
514 * The wire structure is initialized by this function if the object
515 * id is not correct in the object
516 */
517
518int
519of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst)
520{
521 int rv = OF_ERROR_NONE;
522 of_list_oxm_t *oxm_list;
523
524 if ((src == NULL) || (dst == NULL)) {
525 return OF_ERROR_PARAM;
526 }
527 if (dst->object_id != OF_MATCH_V3) {
Rich Lanecfd4ce02013-07-12 16:37:14 -0700528 of_match_v3_init(dst, OF_VERSION_1_2, 0, 0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700529 }
Rich Lanecfd4ce02013-07-12 16:37:14 -0700530 if ((oxm_list = of_list_oxm_new(dst->version)) == NULL) {
Rich Lanea06d0c32013-03-25 08:52:03 -0700531 return OF_ERROR_RESOURCE;
532 }
533
534 rv = populate_oxm_list(src, oxm_list);
535
536 if (rv == OF_ERROR_NONE) {
537 rv = of_match_v3_oxm_list_set(dst, oxm_list);
538 }
539
540 of_list_oxm_delete(oxm_list);
541
542 return rv;
543}
544""")
545
546def gen_v1_to_unified_match(out):
547 """
548 Generate the code that maps a v1 wire format match object
549 to a unified match object
550 """
551 # for each v1 member, if not in wildcards
552 # translate to unified. Treat nw_src/dst specially
553 out.write("""
554
555/**
556 * Convert an OF_VERSION_1_0 object to a generic match object
557 * @param src Pointer to the OF 1.0 wire structure source
558 * @param dst Pointer to the generic match object destination
559 *
560 * The wire structure is initialized by this function.
561 */
562
563int
564of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst)
565{
566 of_wc_bmap_t wc;
567 int count;
568
569 MEMSET(dst, 0, sizeof(*dst));
570 dst->version = src->version;
571
572 of_match_v1_wildcards_get(src, &wc);
573""")
Rich Lanea06d0c32013-03-25 08:52:03 -0700574 for key in sorted(match.of_v1_keys):
575 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
576 out.write("""
577 count = OF_MATCH_V1_WC_%(ku)s_GET(wc);
578 dst->masks.%(key)s = of_ip_index_to_mask(count);
Rich Lanea06d0c32013-03-25 08:52:03 -0700579 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
Dan Talaycofb50d382013-08-05 16:00:17 -0700580 /* Clear the bits not indicated by mask; IP addrs are special for 1.0 */
581 dst->fields.%(key)s &= dst->masks.%(key)s;
Rich Lanea06d0c32013-03-25 08:52:03 -0700582""" % dict(ku=key.upper(), key=key))
583 else:
584 out.write("""
585 if (!(OF_MATCH_V1_WC_%(ku)s_TEST(wc))) {
586 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
587 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
588 }
589""" % dict(ku=key.upper(), key=key))
590
591 out.write("""
592 return OF_ERROR_NONE;
593}
594""")
595
596def gen_v2_to_unified_match(out):
597 """
598 Generate the code that maps a v2 wire format match object
599 to a unified match object
600 """
601 out.write("""
602int
603of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst)
604{
605 of_wc_bmap_t wc;
606
607 MEMSET(dst, 0, sizeof(*dst));
608 dst->version = src->version;
609
610 of_match_v2_wildcards_get(src, &wc);
611""")
612 for key in match.of_v2_keys:
613 if key in match.of_v2_full_mask:
614 out.write("""
615 of_match_v2_%(key)s_mask_get(src, &dst->masks.%(key)s);
616 if (OF_VARIABLE_IS_NON_ZERO(&dst->masks.%(key)s)) { /* Matching something */
617 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
618 }
Rich Lane054a8182014-04-16 14:42:41 -0700619 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700620""" % dict(ku=key.upper(), key=key))
621 else:
622 out.write("""
623 if (!(OF_MATCH_V2_WC_%(ku)s_TEST(wc))) {
624 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
625 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
626 }
627""" % dict(ku=key.upper(), key=key))
628
629 out.write("""
Dan Talaycofb50d382013-08-05 16:00:17 -0700630
Rich Lanea06d0c32013-03-25 08:52:03 -0700631 return OF_ERROR_NONE;
632}
633""")
634
635
636def gen_v3_to_unified_match(out):
637 """
638 Generate the code that maps a v3 wire format match object
639 to a unified match object
640 """
641 # Iterate thru the OXM list members
642 out.write("""
643int
644of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst)
645{
646 int rv;
647 of_list_oxm_t oxm_list;
648 of_oxm_t oxm_entry;
649""")
650# for key in match.of_match_members:
651# out.write(" of_oxm_%s_t *%s;\n" % (key, key))
652# out.write(" of_oxm_%s_masked_t *%s_masked;\n" % (key, key))
653
654 out.write("""
655 MEMSET(dst, 0, sizeof(*dst));
656 dst->version = src->version;
657
658 of_match_v3_oxm_list_bind(src, &oxm_list);
659 rv = of_list_oxm_first(&oxm_list, &oxm_entry);
660
661 while (rv == OF_ERROR_NONE) {
662 switch (oxm_entry.header.object_id) { /* What kind of entry is this */
663""")
664 for key in match.of_match_members:
665 out.write("""
666 case OF_OXM_%(ku)s_MASKED:
667 of_oxm_%(key)s_masked_value_mask_get(
668 &oxm_entry.%(key)s_masked,
669 &dst->masks.%(key)s);
670 of_oxm_%(key)s_masked_value_get(
671 &oxm_entry.%(key)s,
672 &dst->fields.%(key)s);
Rich Laned5f23452014-04-03 01:16:37 -0700673 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700674 break;
675 case OF_OXM_%(ku)s:
676 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
677 of_oxm_%(key)s_value_get(
678 &oxm_entry.%(key)s,
679 &dst->fields.%(key)s);
680 break;
681""" % (dict(ku=key.upper(), key=key)))
682
683 out.write("""
684 default:
685 /* @fixme Add debug statement */
686 return OF_ERROR_PARSE;
687 } /* end switch */
688 rv = of_list_oxm_next(&oxm_list, &oxm_entry);
689 } /* end OXM iteration */
690
691 return OF_ERROR_NONE;
692}
693""")
694
695def gen_serialize(out):
696 out.write("""
697/**
698 * Serialize a match structure according to the version passed
699 * @param version The version to use for serialization protocol
700 * @param match Pointer to the structure to serialize
701 * @param octets Pointer to an octets object to fill out
702 *
703 * A buffer is allocated using normal internal ALLOC/FREE semantics
704 * and pointed to by the octets object. The length of the resulting
705 * serialization is in octets->bytes.
706 *
707 * For 1.2 matches, returns the padded serialized structure
708 *
709 * Note that FREE must be called on octets->data when processing of
710 * the object is complete.
711 */
712
713int
714of_match_serialize(of_version_t version, of_match_t *match, of_octets_t *octets)
715{
716 int rv;
717
718 switch (version) {
719""")
720 for version in of_g.of_version_range:
721 out.write("""
722 case %(ver_name)s:
723 {
724 of_match_v%(version)s_t *wire_match;
725 wire_match = of_match_v%(version)s_new(version);
726 if (wire_match == NULL) {
727 return OF_ERROR_RESOURCE;
728 }
729 if ((rv = of_match_to_wire_match_v%(version)s(match, wire_match)) < 0) {
730 of_match_v%(version)s_delete(wire_match);
731 return rv;
732 }
Rich Lanedc9bc7f2014-04-05 11:19:20 -0700733 of_wire_buffer_grow(wire_match->wbuf, OF_MATCH_BYTES(wire_match->length));
734 octets->bytes = wire_match->wbuf->current_bytes;
Rich Lanea06d0c32013-03-25 08:52:03 -0700735 of_object_wire_buffer_steal((of_object_t *)wire_match,
736 &octets->data);
737 of_match_v%(version)s_delete(wire_match);
738 }
739 break;
740""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
741 out.write("""
742 default:
743 return OF_ERROR_COMPAT;
744 }
745
746 return OF_ERROR_NONE;
747}
748""")
749
750
751def gen_deserialize(out):
752 out.write("""
753/**
754 * Deserialize a match structure according to the version passed
755 * @param version The version to use for deserialization protocol
756 * @param match Pointer to the structure to fill out
757 * @param octets Pointer to an octets object holding serial buffer
758 *
759 * Normally the octets object will point to a part of a wire buffer.
760 */
761
762int
763of_match_deserialize(of_version_t version, of_match_t *match,
764 of_octets_t *octets)
765{
766 if (octets->bytes == 0) { /* No match specified means all wildcards */
767 MEMSET(match, 0, sizeof(*match));
768 match->version = version;
769
770 return OF_ERROR_NONE;
771 }
772
773 switch (version) {
774""")
775 for version in of_g.of_version_range:
776 out.write("""
777 case %(ver_name)s:
778 { /* FIXME: check init bytes */
779 uint8_t *tmp;
780 of_match_v%(version)d_t wire_match;
781 of_match_v%(version)d_init(&wire_match,
782 %(ver_name)s, -1, 1);
Andreas Wundsam53256162013-05-02 14:05:53 -0700783 of_object_buffer_bind((of_object_t *)&wire_match,
Rich Lanea06d0c32013-03-25 08:52:03 -0700784 octets->data, octets->bytes, NULL);
785 OF_TRY(of_match_v%(version)d_to_match(&wire_match, match));
786
787 /* Free the wire buffer control block without freeing
788 * octets->bytes. */
Rich Lanecdd542d2014-04-03 16:13:12 -0700789 of_wire_buffer_steal(wire_match.wbuf, &tmp);
Rich Lanea06d0c32013-03-25 08:52:03 -0700790 }
791 break;
792""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
793
794 out.write("""
795 default:
796 return OF_ERROR_COMPAT;
797 }
798
799 return OF_ERROR_NONE;
800}
801""")
802
803def gen_match_comp(out=sys.stdout):
804 """
805 Generate match comparison functions
806 """
807 out.write("""
808/**
809 * Determine "more specific" relationship between mac addrs
810 * @return true if v1 is equal to or more specific than v2
811 *
812 * @todo Could be optimized
813 *
814 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
815 * That is, return false if there is a bit set in v2 and not in v1.
816 */
817
818static inline int
819of_more_specific_ipv6(of_ipv6_t *v1, of_ipv6_t *v2) {
820 int idx;
821
822 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
823 /* If there's a bit set in v2 that is clear in v1, return false */
824 if (~v1->addr[idx] & v2->addr[idx]) {
825 return 0;
826 }
827 }
828
829 return 1;
830}
831
832/**
833 * Boolean test if two values agree when restricted to a mask
834 */
835
836static inline int
837of_restricted_match_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, of_ipv6_t *mask) {
838 int idx;
839
840 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700841 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700842 (v2->addr[idx] & mask->addr[idx])) {
843 return 0;
844 }
845 }
846
847 return 1;
848}
849
850/**
851 * Boolean test if two values "overlap" (agree on common masks)
852 */
853
854static inline int
855of_overlap_ipv6(of_ipv6_t *v1, of_ipv6_t *v2,
856 of_ipv6_t *m1, of_ipv6_t *m2) {
857 int idx;
858
859 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700860 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700861 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
862 return 0;
863 }
864 }
865
866 return 1;
867}
868
869#define OF_MORE_SPECIFIC_IPV6(v1, v2) of_more_specific_ipv6((v1), (v2))
870
871#define OF_RESTRICTED_MATCH_IPV6(v1, v2, mask) \\
872 of_restricted_match_ipv6((v1), (v2), (mask))
873
874#define OF_OVERLAP_IPV6(v1, v2, m1, m2) of_overlap_ipv6((v1), (v2), (m1), (m2))
875
876/**
877 * Determine "more specific" relationship between mac addrs
878 * @return true if v1 is equal to or more specific than v2
879 *
880 * @todo Could be optimized
881 *
882 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
883 * That is, return false if there is a bit set in v2 and not in v1.
884 */
885static inline int
886of_more_specific_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2) {
887 int idx;
888
889 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
890 /* If there's a bit set in v2 that is clear in v1, return false */
891 if (~v1->addr[idx] & v2->addr[idx]) {
892 return 0;
893 }
894 }
895
896 return 1;
897}
898
899/**
900 * Boolean test if two values agree when restricted to a mask
901 */
902static inline int
Andreas Wundsam53256162013-05-02 14:05:53 -0700903of_restricted_match_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
Rich Lanea06d0c32013-03-25 08:52:03 -0700904 of_mac_addr_t *mask) {
905 int idx;
906
907 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700908 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700909 (v2->addr[idx] & mask->addr[idx])) {
910 return 0;
911 }
912 }
913
914 return 1;
915}
916
917/**
918 * Boolean test if two values "overlap" (agree on common masks)
919 */
920
921static inline int
922of_overlap_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
923 of_mac_addr_t *m1, of_mac_addr_t *m2) {
924 int idx;
925
926 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700927 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700928 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
929 return 0;
930 }
931 }
932
933 return 1;
934}
935
936#define OF_MORE_SPECIFIC_MAC_ADDR(v1, v2) of_more_specific_mac_addr((v1), (v2))
937
938#define OF_RESTRICTED_MATCH_MAC_ADDR(v1, v2, mask) \\
939 of_restricted_match_mac_addr((v1), (v2), (mask))
940
941#define OF_OVERLAP_MAC_ADDR(v1, v2, m1, m2) \\
942 of_overlap_mac_addr((v1), (v2), (m1), (m2))
943
Rich Lane3b2fd832013-09-24 13:44:08 -0700944#define OF_MORE_SPECIFIC_BITMAP_128(v1, v2) \\
945 (OF_MORE_SPECIFIC_INT((v1)->lo, (v2)->lo) && OF_MORE_SPECIFIC_INT((v1)->hi, (v2)->hi))
946
947#define OF_RESTRICTED_MATCH_BITMAP_128(v1, v2, mask) \\
948 (OF_RESTRICTED_MATCH_INT((v1)->lo, (v2)->lo, (mask)->lo) && OF_RESTRICTED_MATCH_INT((v1)->hi, (v2)->hi, (mask)->hi))
949
950#define OF_OVERLAP_BITMAP_128(v1, v2, m1, m2) \\
951 (OF_OVERLAP_INT((v1)->lo, (v2)->lo, (m1)->lo, (m2)->lo) && OF_OVERLAP_INT((v1)->hi, (v2)->hi, (m1)->hi, (m2)->hi))
952
Rich Lanea06d0c32013-03-25 08:52:03 -0700953/**
954 * More-specific-than macro for integer types; see above
955 * @return true if v1 is equal to or more specific than v2
956 *
957 * If there is a bit that is set in v2 and not in v1, return false.
958 */
959#define OF_MORE_SPECIFIC_INT(v1, v2) (!(~(v1) & (v2)))
960
961/**
962 * Boolean test if two values agree when restricted to a mask
963 */
964#define OF_RESTRICTED_MATCH_INT(v1, v2, mask) \\
965 (((v1) & (mask)) == ((v2) & (mask)))
966
967
968#define OF_OVERLAP_INT(v1, v2, m1, m2) \\
969 ((((v1) & (m1)) & (m2)) == (((v2) & (m1)) & (m2)))
970""")
971
972 out.write("""
973/**
974 * Compare two match structures for exact equality
975 *
976 * We just do memcmp assuming structs were memset to 0 on init
977 */
978static inline int
979of_match_eq(of_match_t *match1, of_match_t *match2)
980{
981 return (MEMCMP(match1, match2, sizeof(of_match_t)) == 0);
982}
983
984/**
985 * Is the entry match more specific than (or equal to) the query match?
986 * @param entry Match expected to be more specific (subset of query)
987 * @param query Match expected to be less specific (superset of entry)
988 * @returns Boolean, see below
989 *
990 * The assumption is that a query is being done for a non-strict
991 * match against an entry in a table. The result is true if the
992 * entry match indicates a more specific (but compatible) flow space
993 * specification than that in the query match. This means that the
994 * values agree between the two where they overlap, and that each mask
995 * for the entry is more specific than that of the query.
996 *
997 * The query has the less specific mask (fewer mask bits) so it is
998 * used for the mask when checking values.
999 */
1000
1001static inline int
1002of_match_more_specific(of_match_t *entry, of_match_t *query)
1003{
1004 of_match_fields_t *q_m, *e_m; /* Short hand for masks, fields */
1005 of_match_fields_t *q_f, *e_f;
1006
1007 q_m = &query->masks;
1008 e_m = &entry->masks;
1009 q_f = &query->fields;
1010 e_f = &entry->fields;
1011""")
1012 for key, entry in match.of_match_members.items():
1013 q_m = "&q_m->%s" % key
1014 e_m = "&e_m->%s" % key
1015 q_f = "&q_f->%s" % key
1016 e_f = "&e_f->%s" % key
1017 if entry["m_type"] == "of_ipv6_t":
1018 comp = "OF_MORE_SPECIFIC_IPV6"
1019 match_type = "OF_RESTRICTED_MATCH_IPV6"
1020 elif entry["m_type"] == "of_mac_addr_t":
1021 comp = "OF_MORE_SPECIFIC_MAC_ADDR"
1022 match_type = "OF_RESTRICTED_MATCH_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -07001023 elif entry["m_type"] == "of_bitmap_128_t":
1024 comp = "OF_MORE_SPECIFIC_BITMAP_128"
1025 match_type = "OF_RESTRICTED_MATCH_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -07001026 else: # Integer
1027 comp = "OF_MORE_SPECIFIC_INT"
1028 match_type = "OF_RESTRICTED_MATCH_INT"
1029 q_m = "q_m->%s" % key
1030 e_m = "e_m->%s" % key
1031 q_f = "q_f->%s" % key
1032 e_f = "e_f->%s" % key
1033 out.write("""
1034 /* Mask and values for %(key)s */
1035 if (!%(comp)s(%(e_m)s, %(q_m)s)) {
1036 return 0;
1037 }
1038 if (!%(match_type)s(%(e_f)s, %(q_f)s,
1039 %(q_m)s)) {
1040 return 0;
1041 }
Andreas Wundsam53256162013-05-02 14:05:53 -07001042""" % dict(match_type=match_type, comp=comp, q_f=q_f, e_f=e_f,
Rich Lanea06d0c32013-03-25 08:52:03 -07001043 q_m=q_m, e_m=e_m, key=key))
1044
1045 out.write("""
1046 return 1;
1047}
1048""")
1049
1050 out.write("""
1051
1052/**
1053 * Do two entries overlap?
1054 * @param match1 One match struct
1055 * @param match2 Another match struct
1056 * @returns Boolean: true if there is a packet that would match both
1057 *
1058 */
1059
1060static inline int
1061of_match_overlap(of_match_t *match1, of_match_t *match2)
1062{
1063 of_match_fields_t *m1, *m2; /* Short hand for masks, fields */
1064 of_match_fields_t *f1, *f2;
1065
1066 m1 = &match1->masks;
1067 m2 = &match2->masks;
1068 f1 = &match1->fields;
1069 f2 = &match2->fields;
1070""")
1071 for key, entry in match.of_match_members.items():
1072 m1 = "&m1->%s" % key
1073 m2 = "&m2->%s" % key
1074 f1 = "&f1->%s" % key
1075 f2 = "&f2->%s" % key
1076 if entry["m_type"] == "of_ipv6_t":
1077 check = "OF_OVERLAP_IPV6"
1078 elif entry["m_type"] == "of_mac_addr_t":
1079 check = "OF_OVERLAP_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -07001080 elif entry["m_type"] == "of_bitmap_128_t":
1081 check = "OF_OVERLAP_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -07001082 else: # Integer
1083 check = "OF_OVERLAP_INT"
1084 m1 = "m1->%s" % key
1085 m2 = "m2->%s" % key
1086 f1 = "f1->%s" % key
1087 f2 = "f2->%s" % key
1088 out.write("""
1089 /* Check overlap for %(key)s */
Andreas Wundsam53256162013-05-02 14:05:53 -07001090 if (!%(check)s(%(f1)s, %(f2)s,
Rich Lanea06d0c32013-03-25 08:52:03 -07001091 %(m2)s, %(m1)s)) {
1092 return 0; /* This field differentiates; all done */
1093 }
1094""" % dict(check=check, f1=f1, f2=f2, m1=m1, m2=m2, key=key))
1095
1096 out.write("""
1097 return 1; /* No field differentiates matches */
1098}
1099""")
1100
1101def gen_match_conversions(out=sys.stdout):
1102 match.match_sanity_check()
Rich Lanea06d0c32013-03-25 08:52:03 -07001103 out.write("""
1104/**
1105 * IP Mask map. IP maks wildcards from OF 1.0 are interpretted as
1106 * indices into the map below.
1107 */
1108
1109int of_ip_mask_map_init_done = 0;
1110uint32_t of_ip_mask_map[OF_IP_MASK_MAP_COUNT];
1111void
1112of_ip_mask_map_init(void)
1113{
1114 int idx;
1115
1116 MEMSET(of_ip_mask_map, 0, sizeof(of_ip_mask_map));
1117 for (idx = 0; idx < 32; idx++) {
1118 of_ip_mask_map[idx] = ~((1U << idx) - 1);
1119 }
1120
1121 of_ip_mask_map_init_done = 1;
1122}
1123
1124/**
1125 * @brief Set non-default IP mask for given index
1126 */
1127int
1128of_ip_mask_map_set(int index, uint32_t mask)
1129{
1130 OF_IP_MASK_INIT_CHECK;
1131
1132 if ((index < 0) || (index >= OF_IP_MASK_MAP_COUNT)) {
1133 return OF_ERROR_RANGE;
1134 }
1135 of_ip_mask_map[index] = mask;
1136
1137 return OF_ERROR_NONE;
1138}
1139
1140/**
1141 * @brief Get a non-default IP mask for given index
1142 */
1143int
1144of_ip_mask_map_get(int index, uint32_t *mask)
1145{
1146 OF_IP_MASK_INIT_CHECK;
1147
1148 if ((mask == NULL) || (index < 0) || (index >= OF_IP_MASK_MAP_COUNT)) {
1149 return OF_ERROR_RANGE;
1150 }
1151 *mask = of_ip_mask_map[index];
1152
1153 return OF_ERROR_NONE;
1154}
1155
1156/**
1157 * @brief Return the index (used as the WC field in 1.0 match) given the mask
1158 */
1159
1160int
1161of_ip_mask_to_index(uint32_t mask)
1162{
1163 int idx;
1164
1165 OF_IP_MASK_INIT_CHECK;
1166
1167 /* Handle most common cases directly */
1168 if ((mask == 0) && (of_ip_mask_map[63] == 0)) {
1169 return 63;
1170 }
1171 if ((mask == 0xffffffff) && (of_ip_mask_map[0] == 0xffffffff)) {
1172 return 0;
1173 }
1174
1175 for (idx = 0; idx < OF_IP_MASK_MAP_COUNT; idx++) {
1176 if (mask == of_ip_mask_map[idx]) {
1177 return idx;
1178 }
1179 }
1180
1181 LOCI_LOG_INFO("OF 1.0: Could not map IP addr mask 0x%x", mask);
1182 return 0x3f;
1183}
1184
1185/**
1186 * @brief Return the mask for the given index
1187 */
1188
1189uint32_t
1190of_ip_index_to_mask(int index)
1191{
1192 OF_IP_MASK_INIT_CHECK;
1193
1194 if (index >= OF_IP_MASK_MAP_COUNT) {
1195 LOCI_LOG_INFO("IP index to map: bad index %d", index);
1196 return 0;
1197 }
1198
1199 return of_ip_mask_map[index];
1200}
1201
1202""")
1203
1204 gen_unified_match_to_v1(out)
1205 gen_unified_match_to_v2(out)
1206 gen_unified_match_to_v3(out)
1207 gen_v1_to_unified_match(out)
1208 gen_v2_to_unified_match(out)
1209 gen_v3_to_unified_match(out)
1210 return