blob: e9211cd1b8677885bd277637e81d603e89988fdf [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
289def gen_wc_convert_literal(out):
290 """
291 A bunch of literal C code that's associated with match conversions
292 @param out The output file handle
293 """
294 out.write("""
295
296/* Some internal macros and utility functions */
297
298/* For counting bits in a uint32 */
299#define _VAL_AND_5s(v) ((v) & 0x55555555)
300#define _VAL_EVERY_OTHER(v) (_VAL_AND_5s(v) + _VAL_AND_5s(v >> 1))
301#define _VAL_AND_3s(v) ((v) & 0x33333333)
302#define _VAL_PAIRS(v) (_VAL_AND_3s(v) + _VAL_AND_3s(v >> 2))
303#define _VAL_QUADS(v) (((val) + ((val) >> 4)) & 0x0F0F0F0F)
304#define _VAL_BYTES(v) ((val) + ((val) >> 8))
305
306/**
307 * Counts the number of bits set in an integer
308 */
309static inline int
310_COUNT_BITS(unsigned int val)
311{
312 val = _VAL_EVERY_OTHER(val);
313 val = _VAL_PAIRS(val);
314 val = _VAL_QUADS(val);
315 val = _VAL_BYTES(val);
316
317 return (val & 0XFF) + ((val >> 16) & 0xFF);
318}
Rich Lanea06d0c32013-03-25 08:52:03 -0700319""")
320
321
322def gen_unified_match_to_v1(out):
323 """
324 Generate C code to convert a unified match structure to a V1 match struct
325 @param out The output file handle
326 """
327
328 out.write("""
329/**
330 * Check if match is compatible with OF 1.0
331 * @param match The match being checked
332 */
333static inline int
334of_match_v1_compat_check(of_match_t *match)
335{
336""")
337 for key in match.of_match_members:
338 if key in match.of_v1_keys:
339 continue
340 out.write("""
341 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
342 return 0;
343 }
344""" % dict(ku=key.upper()))
345
346 out.write("""
347 return 1;
348}
349""")
350
351 out.write("""
352/**
353 * Convert a generic match object to an OF_VERSION_1_0 object
354 * @param src Pointer to the generic match object source
355 * @param dst Pointer to the OF 1.0 wire structure
356 *
357 * The wire structure is initialized by this function if it doesn't
358 * not have the proper object ID.
359 */
360
361int
362of_match_to_wire_match_v1(of_match_t *src, of_match_v1_t *dst)
363{
364 of_wc_bmap_t wildcards = 0;
365 int ip_mask_index;
366
367 if ((src == NULL) || (dst == NULL)) {
368 return OF_ERROR_PARAM;
369 }
370 if (!of_match_v1_compat_check(src)) {
371 return OF_ERROR_COMPAT;
372 }
373 if (dst->object_id != OF_MATCH_V1) {
374 of_match_v1_init(dst, OF_VERSION_1_0, 0, 0);
375 }
376""")
377 for key in sorted(match.of_v1_keys):
378 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
379 out.write("""
380 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
381 ip_mask_index = of_ip_mask_to_index(src->masks.%(key)s);
382 of_match_v1_%(key)s_set(dst, src->fields.%(key)s);
383 } else { /* Wildcarded, look for 0 mask */
384 ip_mask_index = of_ip_mask_to_index(0);
385 }
386 OF_MATCH_V1_WC_%(ku)s_SET(wildcards, ip_mask_index);
387""" % dict(key=key, ku=key.upper()))
388 else:
389 out.write("""
390 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
391 of_match_v1_%(key)s_set(dst, src->fields.%(key)s);
392 } else {
393 OF_MATCH_V1_WC_%(ku)s_SET(wildcards);
394 }
395""" % dict(key=key, ku=key.upper()))
396
397 out.write("""
398 of_match_v1_wildcards_set(dst, wildcards);
399
400 return OF_ERROR_NONE;
401}
402""")
403
404def all_ones_mask(d_type):
405 if d_type == "of_mac_addr_t":
406 return "of_mac_addr_all_ones"
407 else:
408 return "((%s) -1)" % d_type
409
410def gen_unified_match_to_v2(out):
411 """
412 Generate C code to convert a unified match structure to a V2 match struct
413 @param out The output file handle
414 """
415
416 out.write("""
417/**
418 * Check if match is compatible with OF 1.0
419 * @param match The match being checked
420 */
421static inline int
422of_match_v2_compat_check(of_match_t *match)
423{
424""")
425 for key in match.of_match_members:
426 if key in match.of_v2_keys:
427 continue
428 out.write("""
429 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
430 return 0;
431 }
432""" % dict(ku=key.upper()))
433
434 out.write("""
435 return 1;
436}
437""")
438
439 out.write("""
440/**
441 * Convert a generic match object to an OF_VERSION_1_1 object
442 * @param src Pointer to the generic match object source
443 * @param dst Pointer to the OF 1.1 wire structure
444 *
445 * The wire structure is initialized by this function.
446 */
447
448int
449of_match_to_wire_match_v2(of_match_t *src, of_match_v2_t *dst)
450{
451 of_wc_bmap_t wildcards = 0;
452
453 if ((src == NULL) || (dst == NULL)) {
454 return OF_ERROR_PARAM;
455 }
456 if (!of_match_v2_compat_check(src)) {
457 return OF_ERROR_COMPAT;
458 }
459 if (dst->object_id != OF_MATCH_V2) {
460 of_match_v2_init(dst, OF_VERSION_1_1, 0, 0);
461 }
462""")
463 for key in match.of_v2_keys:
464 if key in match.of_v2_full_mask:
465 ones_mask = all_ones_mask(match.of_match_members[key]["m_type"])
466 out.write("""
467 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
468 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
469 of_match_v2_%(key)s_mask_set(dst,
470 src->masks.%(key)s);
471 } else { /* Exact match; use all ones mask */
472 of_match_v2_%(key)s_mask_set(dst,
473 %(ones_mask)s);
474 }
475 of_match_v2_%(key)s_set(dst, src->fields.%(key)s);
476 }
477
478""" % dict(key=key, ku=key.upper(), ones_mask=ones_mask))
479 else:
480 out.write("""
481 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
482 return OF_ERROR_COMPAT;
483 }
484 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
485 of_match_v2_%(key)s_set(dst, src->fields.%(key)s);
486 } else {
487 OF_MATCH_V2_WC_%(ku)s_SET(wildcards);
488 }
489""" % dict(key=key, ku=key.upper(),
490 wc_bit="OF_MATCH_WC_V2_%s" % key.upper()))
491
492 out.write("""
493 of_match_v2_wildcards_set(dst, wildcards);
494
495 return OF_ERROR_NONE;
496}
497""")
498
499def gen_unified_match_to_v3(out):
500 """
501 Generate C code to convert a unified match structure to a V3 match
502
503 This is much easier as the unified struct is based on V3
504 @param out The output file handle
505 """
506 out.write("""
507static int
508populate_oxm_list(of_match_t *src, of_list_oxm_t *oxm_list)
509{
510 of_oxm_t oxm_entry;
511
512 /* For each active member, add an OXM entry to the list */
513""")
Rich Lane4964d542013-10-14 18:13:47 -0700514 for key in match.match_keys_sorted:
Rich Lanea06d0c32013-03-25 08:52:03 -0700515 out.write("""\
516 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(src)) {
517 if (!OF_MATCH_MASK_%(ku)s_EXACT_TEST(src)) {
518 of_oxm_%(key)s_masked_t *elt;
519 elt = &oxm_entry.%(key)s_masked;
520
521 of_oxm_%(key)s_masked_init(elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700522 oxm_list->version, -1, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700523 of_list_oxm_append_bind(oxm_list, &oxm_entry);
Andreas Wundsam53256162013-05-02 14:05:53 -0700524 of_oxm_%(key)s_masked_value_set(elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700525 src->fields.%(key)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700526 of_oxm_%(key)s_masked_value_mask_set(elt,
Rich Lanea06d0c32013-03-25 08:52:03 -0700527 src->masks.%(key)s);
528 } else { /* Active, but not masked */
529 of_oxm_%(key)s_t *elt;
530 elt = &oxm_entry.%(key)s;
531 of_oxm_%(key)s_init(elt,
Rich Lanecfd4ce02013-07-12 16:37:14 -0700532 oxm_list->version, -1, 1);
Rich Lanea06d0c32013-03-25 08:52:03 -0700533 of_list_oxm_append_bind(oxm_list, &oxm_entry);
534 of_oxm_%(key)s_value_set(elt, src->fields.%(key)s);
535 }
536 }
537""" % dict(key=key, ku=key.upper()))
538 out.write("""
539 return OF_ERROR_NONE;
540}
541
542/**
543 * Convert a generic match object to an OF_VERSION_1_2 object
544 * @param src Pointer to the generic match object source
545 * @param dst Pointer to the OF 1.2 wire structure
546 *
547 * The wire structure is initialized by this function if the object
548 * id is not correct in the object
549 */
550
551int
552of_match_to_wire_match_v3(of_match_t *src, of_match_v3_t *dst)
553{
554 int rv = OF_ERROR_NONE;
555 of_list_oxm_t *oxm_list;
556
557 if ((src == NULL) || (dst == NULL)) {
558 return OF_ERROR_PARAM;
559 }
560 if (dst->object_id != OF_MATCH_V3) {
Rich Lanecfd4ce02013-07-12 16:37:14 -0700561 of_match_v3_init(dst, OF_VERSION_1_2, 0, 0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700562 }
Rich Lanecfd4ce02013-07-12 16:37:14 -0700563 if ((oxm_list = of_list_oxm_new(dst->version)) == NULL) {
Rich Lanea06d0c32013-03-25 08:52:03 -0700564 return OF_ERROR_RESOURCE;
565 }
566
567 rv = populate_oxm_list(src, oxm_list);
568
569 if (rv == OF_ERROR_NONE) {
570 rv = of_match_v3_oxm_list_set(dst, oxm_list);
571 }
572
573 of_list_oxm_delete(oxm_list);
574
575 return rv;
576}
577""")
578
579def gen_v1_to_unified_match(out):
580 """
581 Generate the code that maps a v1 wire format match object
582 to a unified match object
583 """
584 # for each v1 member, if not in wildcards
585 # translate to unified. Treat nw_src/dst specially
586 out.write("""
587
588/**
589 * Convert an OF_VERSION_1_0 object to a generic match object
590 * @param src Pointer to the OF 1.0 wire structure source
591 * @param dst Pointer to the generic match object destination
592 *
593 * The wire structure is initialized by this function.
594 */
595
596int
597of_match_v1_to_match(of_match_v1_t *src, of_match_t *dst)
598{
599 of_wc_bmap_t wc;
600 int count;
601
602 MEMSET(dst, 0, sizeof(*dst));
603 dst->version = src->version;
604
605 of_match_v1_wildcards_get(src, &wc);
606""")
Rich Lanea06d0c32013-03-25 08:52:03 -0700607 for key in sorted(match.of_v1_keys):
608 if key in ["ipv4_src", "ipv4_dst"]: # Special cases for masks here
609 out.write("""
610 count = OF_MATCH_V1_WC_%(ku)s_GET(wc);
611 dst->masks.%(key)s = of_ip_index_to_mask(count);
Rich Lanea06d0c32013-03-25 08:52:03 -0700612 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
Dan Talaycofb50d382013-08-05 16:00:17 -0700613 /* Clear the bits not indicated by mask; IP addrs are special for 1.0 */
614 dst->fields.%(key)s &= dst->masks.%(key)s;
Rich Lanea06d0c32013-03-25 08:52:03 -0700615""" % dict(ku=key.upper(), key=key))
616 else:
617 out.write("""
618 if (!(OF_MATCH_V1_WC_%(ku)s_TEST(wc))) {
619 of_match_v1_%(key)s_get(src, &dst->fields.%(key)s);
620 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
621 }
622""" % dict(ku=key.upper(), key=key))
623
624 out.write("""
625 return OF_ERROR_NONE;
626}
627""")
628
629def gen_v2_to_unified_match(out):
630 """
631 Generate the code that maps a v2 wire format match object
632 to a unified match object
633 """
634 out.write("""
635int
636of_match_v2_to_match(of_match_v2_t *src, of_match_t *dst)
637{
638 of_wc_bmap_t wc;
639
640 MEMSET(dst, 0, sizeof(*dst));
641 dst->version = src->version;
642
643 of_match_v2_wildcards_get(src, &wc);
644""")
645 for key in match.of_v2_keys:
646 if key in match.of_v2_full_mask:
647 out.write("""
648 of_match_v2_%(key)s_mask_get(src, &dst->masks.%(key)s);
649 if (OF_VARIABLE_IS_NON_ZERO(&dst->masks.%(key)s)) { /* Matching something */
650 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
651 }
Rich Lane054a8182014-04-16 14:42:41 -0700652 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700653""" % dict(ku=key.upper(), key=key))
654 else:
655 out.write("""
656 if (!(OF_MATCH_V2_WC_%(ku)s_TEST(wc))) {
657 of_match_v2_%(key)s_get(src, &dst->fields.%(key)s);
658 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
659 }
660""" % dict(ku=key.upper(), key=key))
661
662 out.write("""
Dan Talaycofb50d382013-08-05 16:00:17 -0700663
Rich Lanea06d0c32013-03-25 08:52:03 -0700664 return OF_ERROR_NONE;
665}
666""")
667
668
669def gen_v3_to_unified_match(out):
670 """
671 Generate the code that maps a v3 wire format match object
672 to a unified match object
673 """
674 # Iterate thru the OXM list members
675 out.write("""
676int
677of_match_v3_to_match(of_match_v3_t *src, of_match_t *dst)
678{
679 int rv;
680 of_list_oxm_t oxm_list;
681 of_oxm_t oxm_entry;
682""")
683# for key in match.of_match_members:
684# out.write(" of_oxm_%s_t *%s;\n" % (key, key))
685# out.write(" of_oxm_%s_masked_t *%s_masked;\n" % (key, key))
686
687 out.write("""
688 MEMSET(dst, 0, sizeof(*dst));
689 dst->version = src->version;
690
691 of_match_v3_oxm_list_bind(src, &oxm_list);
692 rv = of_list_oxm_first(&oxm_list, &oxm_entry);
693
694 while (rv == OF_ERROR_NONE) {
695 switch (oxm_entry.header.object_id) { /* What kind of entry is this */
696""")
697 for key in match.of_match_members:
698 out.write("""
699 case OF_OXM_%(ku)s_MASKED:
700 of_oxm_%(key)s_masked_value_mask_get(
701 &oxm_entry.%(key)s_masked,
702 &dst->masks.%(key)s);
703 of_oxm_%(key)s_masked_value_get(
704 &oxm_entry.%(key)s,
705 &dst->fields.%(key)s);
Rich Laned5f23452014-04-03 01:16:37 -0700706 of_memmask(&dst->fields.%(key)s, &dst->masks.%(key)s, sizeof(&dst->fields.%(key)s));
Rich Lanea06d0c32013-03-25 08:52:03 -0700707 break;
708 case OF_OXM_%(ku)s:
709 OF_MATCH_MASK_%(ku)s_EXACT_SET(dst);
710 of_oxm_%(key)s_value_get(
711 &oxm_entry.%(key)s,
712 &dst->fields.%(key)s);
713 break;
714""" % (dict(ku=key.upper(), key=key)))
715
716 out.write("""
717 default:
718 /* @fixme Add debug statement */
719 return OF_ERROR_PARSE;
720 } /* end switch */
721 rv = of_list_oxm_next(&oxm_list, &oxm_entry);
722 } /* end OXM iteration */
723
724 return OF_ERROR_NONE;
725}
726""")
727
728def gen_serialize(out):
729 out.write("""
730/**
731 * Serialize a match structure according to the version passed
732 * @param version The version to use for serialization protocol
733 * @param match Pointer to the structure to serialize
734 * @param octets Pointer to an octets object to fill out
735 *
736 * A buffer is allocated using normal internal ALLOC/FREE semantics
737 * and pointed to by the octets object. The length of the resulting
738 * serialization is in octets->bytes.
739 *
740 * For 1.2 matches, returns the padded serialized structure
741 *
742 * Note that FREE must be called on octets->data when processing of
743 * the object is complete.
744 */
745
746int
747of_match_serialize(of_version_t version, of_match_t *match, of_octets_t *octets)
748{
749 int rv;
750
751 switch (version) {
752""")
753 for version in of_g.of_version_range:
754 out.write("""
755 case %(ver_name)s:
756 {
757 of_match_v%(version)s_t *wire_match;
758 wire_match = of_match_v%(version)s_new(version);
759 if (wire_match == NULL) {
760 return OF_ERROR_RESOURCE;
761 }
762 if ((rv = of_match_to_wire_match_v%(version)s(match, wire_match)) < 0) {
763 of_match_v%(version)s_delete(wire_match);
764 return rv;
765 }
Rich Lanedc9bc7f2014-04-05 11:19:20 -0700766 of_wire_buffer_grow(wire_match->wbuf, OF_MATCH_BYTES(wire_match->length));
767 octets->bytes = wire_match->wbuf->current_bytes;
Rich Lanea06d0c32013-03-25 08:52:03 -0700768 of_object_wire_buffer_steal((of_object_t *)wire_match,
769 &octets->data);
770 of_match_v%(version)s_delete(wire_match);
771 }
772 break;
773""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
774 out.write("""
775 default:
776 return OF_ERROR_COMPAT;
777 }
778
779 return OF_ERROR_NONE;
780}
781""")
782
783
784def gen_deserialize(out):
785 out.write("""
786/**
787 * Deserialize a match structure according to the version passed
788 * @param version The version to use for deserialization protocol
789 * @param match Pointer to the structure to fill out
790 * @param octets Pointer to an octets object holding serial buffer
791 *
792 * Normally the octets object will point to a part of a wire buffer.
793 */
794
795int
796of_match_deserialize(of_version_t version, of_match_t *match,
797 of_octets_t *octets)
798{
799 if (octets->bytes == 0) { /* No match specified means all wildcards */
800 MEMSET(match, 0, sizeof(*match));
801 match->version = version;
802
803 return OF_ERROR_NONE;
804 }
805
806 switch (version) {
807""")
808 for version in of_g.of_version_range:
809 out.write("""
810 case %(ver_name)s:
811 { /* FIXME: check init bytes */
812 uint8_t *tmp;
813 of_match_v%(version)d_t wire_match;
814 of_match_v%(version)d_init(&wire_match,
815 %(ver_name)s, -1, 1);
Andreas Wundsam53256162013-05-02 14:05:53 -0700816 of_object_buffer_bind((of_object_t *)&wire_match,
Rich Lanea06d0c32013-03-25 08:52:03 -0700817 octets->data, octets->bytes, NULL);
818 OF_TRY(of_match_v%(version)d_to_match(&wire_match, match));
819
820 /* Free the wire buffer control block without freeing
821 * octets->bytes. */
Rich Lanecdd542d2014-04-03 16:13:12 -0700822 of_wire_buffer_steal(wire_match.wbuf, &tmp);
Rich Lanea06d0c32013-03-25 08:52:03 -0700823 }
824 break;
825""" % dict(version=version, ver_name=of_g.of_version_wire2name[version]))
826
827 out.write("""
828 default:
829 return OF_ERROR_COMPAT;
830 }
831
832 return OF_ERROR_NONE;
833}
834""")
835
836def gen_match_comp(out=sys.stdout):
837 """
838 Generate match comparison functions
839 """
840 out.write("""
841/**
842 * Determine "more specific" relationship between mac addrs
843 * @return true if v1 is equal to or more specific than v2
844 *
845 * @todo Could be optimized
846 *
847 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
848 * That is, return false if there is a bit set in v2 and not in v1.
849 */
850
851static inline int
852of_more_specific_ipv6(of_ipv6_t *v1, of_ipv6_t *v2) {
853 int idx;
854
855 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
856 /* If there's a bit set in v2 that is clear in v1, return false */
857 if (~v1->addr[idx] & v2->addr[idx]) {
858 return 0;
859 }
860 }
861
862 return 1;
863}
864
865/**
866 * Boolean test if two values agree when restricted to a mask
867 */
868
869static inline int
870of_restricted_match_ipv6(of_ipv6_t *v1, of_ipv6_t *v2, of_ipv6_t *mask) {
871 int idx;
872
873 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700874 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700875 (v2->addr[idx] & mask->addr[idx])) {
876 return 0;
877 }
878 }
879
880 return 1;
881}
882
883/**
884 * Boolean test if two values "overlap" (agree on common masks)
885 */
886
887static inline int
888of_overlap_ipv6(of_ipv6_t *v1, of_ipv6_t *v2,
889 of_ipv6_t *m1, of_ipv6_t *m2) {
890 int idx;
891
892 for (idx = 0; idx < OF_IPV6_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700893 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700894 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
895 return 0;
896 }
897 }
898
899 return 1;
900}
901
902#define OF_MORE_SPECIFIC_IPV6(v1, v2) of_more_specific_ipv6((v1), (v2))
903
904#define OF_RESTRICTED_MATCH_IPV6(v1, v2, mask) \\
905 of_restricted_match_ipv6((v1), (v2), (mask))
906
907#define OF_OVERLAP_IPV6(v1, v2, m1, m2) of_overlap_ipv6((v1), (v2), (m1), (m2))
908
909/**
910 * Determine "more specific" relationship between mac addrs
911 * @return true if v1 is equal to or more specific than v2
912 *
913 * @todo Could be optimized
914 *
915 * Check: Every bit in v2 is set in v1; v1 may have add'l bits set.
916 * That is, return false if there is a bit set in v2 and not in v1.
917 */
918static inline int
919of_more_specific_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2) {
920 int idx;
921
922 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
923 /* If there's a bit set in v2 that is clear in v1, return false */
924 if (~v1->addr[idx] & v2->addr[idx]) {
925 return 0;
926 }
927 }
928
929 return 1;
930}
931
932/**
933 * Boolean test if two values agree when restricted to a mask
934 */
935static inline int
Andreas Wundsam53256162013-05-02 14:05:53 -0700936of_restricted_match_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
Rich Lanea06d0c32013-03-25 08:52:03 -0700937 of_mac_addr_t *mask) {
938 int idx;
939
940 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700941 if ((v1->addr[idx] & mask->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700942 (v2->addr[idx] & mask->addr[idx])) {
943 return 0;
944 }
945 }
946
947 return 1;
948}
949
950/**
951 * Boolean test if two values "overlap" (agree on common masks)
952 */
953
954static inline int
955of_overlap_mac_addr(of_mac_addr_t *v1, of_mac_addr_t *v2,
956 of_mac_addr_t *m1, of_mac_addr_t *m2) {
957 int idx;
958
959 for (idx = 0; idx < OF_MAC_ADDR_BYTES; idx++) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700960 if (((v1->addr[idx] & m1->addr[idx]) & m2->addr[idx]) !=
Rich Lanea06d0c32013-03-25 08:52:03 -0700961 ((v2->addr[idx] & m1->addr[idx]) & m2->addr[idx])) {
962 return 0;
963 }
964 }
965
966 return 1;
967}
968
969#define OF_MORE_SPECIFIC_MAC_ADDR(v1, v2) of_more_specific_mac_addr((v1), (v2))
970
971#define OF_RESTRICTED_MATCH_MAC_ADDR(v1, v2, mask) \\
972 of_restricted_match_mac_addr((v1), (v2), (mask))
973
974#define OF_OVERLAP_MAC_ADDR(v1, v2, m1, m2) \\
975 of_overlap_mac_addr((v1), (v2), (m1), (m2))
976
Rich Lane3b2fd832013-09-24 13:44:08 -0700977#define OF_MORE_SPECIFIC_BITMAP_128(v1, v2) \\
978 (OF_MORE_SPECIFIC_INT((v1)->lo, (v2)->lo) && OF_MORE_SPECIFIC_INT((v1)->hi, (v2)->hi))
979
980#define OF_RESTRICTED_MATCH_BITMAP_128(v1, v2, mask) \\
981 (OF_RESTRICTED_MATCH_INT((v1)->lo, (v2)->lo, (mask)->lo) && OF_RESTRICTED_MATCH_INT((v1)->hi, (v2)->hi, (mask)->hi))
982
983#define OF_OVERLAP_BITMAP_128(v1, v2, m1, m2) \\
984 (OF_OVERLAP_INT((v1)->lo, (v2)->lo, (m1)->lo, (m2)->lo) && OF_OVERLAP_INT((v1)->hi, (v2)->hi, (m1)->hi, (m2)->hi))
985
Rich Lanea06d0c32013-03-25 08:52:03 -0700986/**
987 * More-specific-than macro for integer types; see above
988 * @return true if v1 is equal to or more specific than v2
989 *
990 * If there is a bit that is set in v2 and not in v1, return false.
991 */
992#define OF_MORE_SPECIFIC_INT(v1, v2) (!(~(v1) & (v2)))
993
994/**
995 * Boolean test if two values agree when restricted to a mask
996 */
997#define OF_RESTRICTED_MATCH_INT(v1, v2, mask) \\
998 (((v1) & (mask)) == ((v2) & (mask)))
999
1000
1001#define OF_OVERLAP_INT(v1, v2, m1, m2) \\
1002 ((((v1) & (m1)) & (m2)) == (((v2) & (m1)) & (m2)))
1003""")
1004
1005 out.write("""
1006/**
1007 * Compare two match structures for exact equality
1008 *
1009 * We just do memcmp assuming structs were memset to 0 on init
1010 */
1011static inline int
1012of_match_eq(of_match_t *match1, of_match_t *match2)
1013{
1014 return (MEMCMP(match1, match2, sizeof(of_match_t)) == 0);
1015}
1016
1017/**
1018 * Is the entry match more specific than (or equal to) the query match?
1019 * @param entry Match expected to be more specific (subset of query)
1020 * @param query Match expected to be less specific (superset of entry)
1021 * @returns Boolean, see below
1022 *
1023 * The assumption is that a query is being done for a non-strict
1024 * match against an entry in a table. The result is true if the
1025 * entry match indicates a more specific (but compatible) flow space
1026 * specification than that in the query match. This means that the
1027 * values agree between the two where they overlap, and that each mask
1028 * for the entry is more specific than that of the query.
1029 *
1030 * The query has the less specific mask (fewer mask bits) so it is
1031 * used for the mask when checking values.
1032 */
1033
1034static inline int
1035of_match_more_specific(of_match_t *entry, of_match_t *query)
1036{
1037 of_match_fields_t *q_m, *e_m; /* Short hand for masks, fields */
1038 of_match_fields_t *q_f, *e_f;
1039
1040 q_m = &query->masks;
1041 e_m = &entry->masks;
1042 q_f = &query->fields;
1043 e_f = &entry->fields;
1044""")
1045 for key, entry in match.of_match_members.items():
1046 q_m = "&q_m->%s" % key
1047 e_m = "&e_m->%s" % key
1048 q_f = "&q_f->%s" % key
1049 e_f = "&e_f->%s" % key
1050 if entry["m_type"] == "of_ipv6_t":
1051 comp = "OF_MORE_SPECIFIC_IPV6"
1052 match_type = "OF_RESTRICTED_MATCH_IPV6"
1053 elif entry["m_type"] == "of_mac_addr_t":
1054 comp = "OF_MORE_SPECIFIC_MAC_ADDR"
1055 match_type = "OF_RESTRICTED_MATCH_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -07001056 elif entry["m_type"] == "of_bitmap_128_t":
1057 comp = "OF_MORE_SPECIFIC_BITMAP_128"
1058 match_type = "OF_RESTRICTED_MATCH_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -07001059 else: # Integer
1060 comp = "OF_MORE_SPECIFIC_INT"
1061 match_type = "OF_RESTRICTED_MATCH_INT"
1062 q_m = "q_m->%s" % key
1063 e_m = "e_m->%s" % key
1064 q_f = "q_f->%s" % key
1065 e_f = "e_f->%s" % key
1066 out.write("""
1067 /* Mask and values for %(key)s */
1068 if (!%(comp)s(%(e_m)s, %(q_m)s)) {
1069 return 0;
1070 }
1071 if (!%(match_type)s(%(e_f)s, %(q_f)s,
1072 %(q_m)s)) {
1073 return 0;
1074 }
Andreas Wundsam53256162013-05-02 14:05:53 -07001075""" % dict(match_type=match_type, comp=comp, q_f=q_f, e_f=e_f,
Rich Lanea06d0c32013-03-25 08:52:03 -07001076 q_m=q_m, e_m=e_m, key=key))
1077
1078 out.write("""
1079 return 1;
1080}
1081""")
1082
1083 out.write("""
1084
1085/**
1086 * Do two entries overlap?
1087 * @param match1 One match struct
1088 * @param match2 Another match struct
1089 * @returns Boolean: true if there is a packet that would match both
1090 *
1091 */
1092
1093static inline int
1094of_match_overlap(of_match_t *match1, of_match_t *match2)
1095{
1096 of_match_fields_t *m1, *m2; /* Short hand for masks, fields */
1097 of_match_fields_t *f1, *f2;
1098
1099 m1 = &match1->masks;
1100 m2 = &match2->masks;
1101 f1 = &match1->fields;
1102 f2 = &match2->fields;
1103""")
1104 for key, entry in match.of_match_members.items():
1105 m1 = "&m1->%s" % key
1106 m2 = "&m2->%s" % key
1107 f1 = "&f1->%s" % key
1108 f2 = "&f2->%s" % key
1109 if entry["m_type"] == "of_ipv6_t":
1110 check = "OF_OVERLAP_IPV6"
1111 elif entry["m_type"] == "of_mac_addr_t":
1112 check = "OF_OVERLAP_MAC_ADDR"
Rich Lane3b2fd832013-09-24 13:44:08 -07001113 elif entry["m_type"] == "of_bitmap_128_t":
1114 check = "OF_OVERLAP_BITMAP_128"
Rich Lanea06d0c32013-03-25 08:52:03 -07001115 else: # Integer
1116 check = "OF_OVERLAP_INT"
1117 m1 = "m1->%s" % key
1118 m2 = "m2->%s" % key
1119 f1 = "f1->%s" % key
1120 f2 = "f2->%s" % key
1121 out.write("""
1122 /* Check overlap for %(key)s */
Andreas Wundsam53256162013-05-02 14:05:53 -07001123 if (!%(check)s(%(f1)s, %(f2)s,
Rich Lanea06d0c32013-03-25 08:52:03 -07001124 %(m2)s, %(m1)s)) {
1125 return 0; /* This field differentiates; all done */
1126 }
1127""" % dict(check=check, f1=f1, f2=f2, m1=m1, m2=m2, key=key))
1128
1129 out.write("""
1130 return 1; /* No field differentiates matches */
1131}
1132""")
1133
1134def gen_match_conversions(out=sys.stdout):
1135 match.match_sanity_check()
1136 gen_wc_convert_literal(out)
1137 out.write("""
1138/**
1139 * IP Mask map. IP maks wildcards from OF 1.0 are interpretted as
1140 * indices into the map below.
1141 */
1142
1143int of_ip_mask_map_init_done = 0;
1144uint32_t of_ip_mask_map[OF_IP_MASK_MAP_COUNT];
1145void
1146of_ip_mask_map_init(void)
1147{
1148 int idx;
1149
1150 MEMSET(of_ip_mask_map, 0, sizeof(of_ip_mask_map));
1151 for (idx = 0; idx < 32; idx++) {
1152 of_ip_mask_map[idx] = ~((1U << idx) - 1);
1153 }
1154
1155 of_ip_mask_map_init_done = 1;
1156}
1157
1158/**
1159 * @brief Set non-default IP mask for given index
1160 */
1161int
1162of_ip_mask_map_set(int index, uint32_t mask)
1163{
1164 OF_IP_MASK_INIT_CHECK;
1165
1166 if ((index < 0) || (index >= OF_IP_MASK_MAP_COUNT)) {
1167 return OF_ERROR_RANGE;
1168 }
1169 of_ip_mask_map[index] = mask;
1170
1171 return OF_ERROR_NONE;
1172}
1173
1174/**
1175 * @brief Get a non-default IP mask for given index
1176 */
1177int
1178of_ip_mask_map_get(int index, uint32_t *mask)
1179{
1180 OF_IP_MASK_INIT_CHECK;
1181
1182 if ((mask == NULL) || (index < 0) || (index >= OF_IP_MASK_MAP_COUNT)) {
1183 return OF_ERROR_RANGE;
1184 }
1185 *mask = of_ip_mask_map[index];
1186
1187 return OF_ERROR_NONE;
1188}
1189
1190/**
1191 * @brief Return the index (used as the WC field in 1.0 match) given the mask
1192 */
1193
1194int
1195of_ip_mask_to_index(uint32_t mask)
1196{
1197 int idx;
1198
1199 OF_IP_MASK_INIT_CHECK;
1200
1201 /* Handle most common cases directly */
1202 if ((mask == 0) && (of_ip_mask_map[63] == 0)) {
1203 return 63;
1204 }
1205 if ((mask == 0xffffffff) && (of_ip_mask_map[0] == 0xffffffff)) {
1206 return 0;
1207 }
1208
1209 for (idx = 0; idx < OF_IP_MASK_MAP_COUNT; idx++) {
1210 if (mask == of_ip_mask_map[idx]) {
1211 return idx;
1212 }
1213 }
1214
1215 LOCI_LOG_INFO("OF 1.0: Could not map IP addr mask 0x%x", mask);
1216 return 0x3f;
1217}
1218
1219/**
1220 * @brief Return the mask for the given index
1221 */
1222
1223uint32_t
1224of_ip_index_to_mask(int index)
1225{
1226 OF_IP_MASK_INIT_CHECK;
1227
1228 if (index >= OF_IP_MASK_MAP_COUNT) {
1229 LOCI_LOG_INFO("IP index to map: bad index %d", index);
1230 return 0;
1231 }
1232
1233 return of_ip_mask_map[index];
1234}
1235
1236""")
1237
1238 gen_unified_match_to_v1(out)
1239 gen_unified_match_to_v2(out)
1240 gen_unified_match_to_v3(out)
1241 gen_v1_to_unified_match(out)
1242 gen_v2_to_unified_match(out)
1243 gen_v3_to_unified_match(out)
1244 return