blob: 896e9add826b8b834beb8950069bdde6ee666699 [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"""
29@file code_gen.py
30Code generation functions for LOCI
31"""
32
33import sys
Andreas Wundsam76db0062013-11-15 13:34:41 -080034import c_gen.of_g_legacy as of_g
Rich Lanea06d0c32013-03-25 08:52:03 -070035import c_match
36from generic_utils import *
Andreas Wundsam542a13c2013-11-15 13:28:55 -080037from c_gen import flags, type_maps, c_type_maps
38import c_gen.loxi_utils_legacy as loxi_utils
Rich Lane92feca82013-12-10 15:57:13 -080039import loxi_globals
Rich Lanea06d0c32013-03-25 08:52:03 -070040
Andreas Wundsam542a13c2013-11-15 13:28:55 -080041import c_gen.identifiers as identifiers
Rich Lanea06d0c32013-03-25 08:52:03 -070042
Andreas Wundsam76db0062013-11-15 13:34:41 -080043# 'property' is for queues. Could be trouble
Rich Lanea06d0c32013-03-25 08:52:03 -070044
45################################################################
46#
47# Misc helper functions
48#
49################################################################
50
51def h_file_to_define(name):
52 """
53 Convert a .h file name to the define used for the header
54 """
55 h_name = name[:-2].upper()
56 h_name = "_" + h_name + "_H_"
57 return h_name
58
59def enum_name(cls):
60 """
61 Return the name used for an enum identifier for the given class
62 @param cls The class name
63 """
64 return loxi_utils.enum_name(cls)
65
Rich Lanea06d0c32013-03-25 08:52:03 -070066# TODO serialize match outside accessor?
67def accessor_return_type(a_type, m_type):
68 if loxi_utils.accessor_returns_error(a_type, m_type):
69 return "int WARN_UNUSED_RESULT"
70 else:
71 return "void"
72
73def accessor_return_success(a_type, m_type):
74 if loxi_utils.accessor_returns_error(a_type, m_type):
75 return "OF_ERROR_NONE"
76 else:
77 return ""
78
79################################################################
80#
81# Per-file generators, mapped to jump table below
82#
83################################################################
84
85def base_h_gen(out, name):
86 """
87 Generate code for base header file
88 @param out The file handle to write to
89 @param name The name of the file
90 """
91 common_top_matter(out, name)
92 base_h_content(out)
93 gen_object_enum(out)
94 out.write("""
95/****************************************************************
96 *
97 * Experimenter IDs
98 *
99 ****************************************************************/
100
101""")
102 for name, val in of_g.experimenter_name_to_id.items():
103 out.write("#define OF_EXPERIMENTER_ID_%s 0x%08x\n" %
104 (name.upper(), val))
105
106 out.write("""
107/****************************************************************
108 *
109 * OpenFlow Match version specific and generic defines
110 *
111 ****************************************************************/
112""")
113 c_match.gen_v4_match_compat(out)
114 c_match.gen_match_macros(out)
115 c_match.gen_oxm_defines(out)
116 out.write("\n#endif /* Base header file */\n")
117
118def identifiers_gen(out, filename):
119 """
120 Generate the macros for LOCI identifiers
121 @param out The file handle to write to
122 @param filename The name of the file
123 """
124 common_top_matter(out, filename)
125 out.write("""
126/**
127 * For each identifier from an OpenFlow header file, a Loxi version
128 * of the identifier is generated. For example, ofp_port_flood becomes
Andreas Wundsam53256162013-05-02 14:05:53 -0700129 * OF_PORT_DEST_FLOOD. Loxi provides the following macros related to
Rich Lanea06d0c32013-03-25 08:52:03 -0700130 * OpenFlow identifiers (using OF_IDENT_ as an example below):
131 * OF_IDENT_BY_VERSION(version) Get the value for the specific version
132 * OF_IDENT_SUPPORTED(version) Boolean: Is OF_IDENT defined for version
133 * OF_IDENT The common value across all versions if defined
134 * OF_IDENT_GENERIC A unique value across all OF identifiers
135 *
136 * For identifiers marked as flags, the following are also defined
137 * OF_IDENT_SET(flags, version)
138 * OF_IDENT_CLEAR(flags, version)
139 * OF_IDENT_TEST(flags, version)
140 *
141 * Notes:
142 *
143 * OF_IDENT_BY_VERSION(version) returns an undefined value
144 * if the passed version does not define OF_IDENT. It does not generate an
145 * error, nor record anything to the log file. If the value is the same
146 * across all defined versions, the version is ignored.
147 *
148 * OF_IDENT is only defined if the value is the same across all
149 * target LOXI versions FOR WHICH IT IS DEFINED. No error checking is
150 * done. This allows code to be written without requiring the version
151 * to be known or referenced when it doesn't matter. It does mean
152 * that when porting to a new version of OpenFlow, compile errors may
153 * occur. However, this is an indication that the existing code must
154 * be updated to account for a change in the semantics with the newly
155 * supported OpenFlow version.
156 *
157 * @fixme Currently we do not handle multi-bit flags or field values; for
158 * example, OF_TABLE_CONFIG_TABLE_MISS_CONTROLLER is the meaning for
159 * a zero value in the bits indicated by OF_TABLE_CONFIG_TABLE_MISS_MASK.
160 *
161 * @fixme Need to decide (or make a code gen option) on the requirement
162 * for defining OF_IDENT: Is it that all target versions define it and
163 * the agree? Or only that the versions which define it agree?
164 */
165""")
166
167 # Build value-by-version parameters and c_code
168 if len(of_g.target_version_list) > 1: # Supporting more than one version
169 vbv_params = []
170 vbv_code = ""
171 first = True
172 for version in of_g.target_version_list:
173 vbv_params.append("value_%s" % of_g.short_version_names[version])
174 if not first:
175 vbv_code += "\\\n "
176 else:
177 first = False
178 last_value = "value_%s" % of_g.short_version_names[version]
179 vbv_code += "((version) == %s) ? (%s) : " % \
180 (of_g.of_version_wire2name[version], last_value)
181 # @todo Using last value, can optimize out last ?
182 vbv_code += "(%s)" % last_value
183
184 out.write("""
185/**
186 * @brief True for the special case of all versions supported
187 */
188#define OF_IDENT_IN_ALL_VERSIONS 1 /* Indicates identifier in all versions */
189
190/**
191 * @brief General macro to map version to value where values given as params
192 *
193 * If unknown version is passed, use the latest version's value
194 */
195#define OF_VALUE_BY_VERSION(version, %s) \\
196 (%s)
197
198/**
199 * @brief Generic set a flag
200 */
201#define OF_FLAG_SET(flags, mask) (flags) = (flags) | (mask)
202
203/**
204 * @brief Generic test if a flag is set
205 */
206#define OF_FLAG_CLEAR(flags, mask) (flags) = (flags) & ~(mask)
207
208/**
209 * @brief Generic test if a flag is set
210 */
211#define OF_FLAG_TEST(flags, mask) ((flags) & (mask) ? 1 : 0)
212
213/**
214 * @brief Set a flag where the value is an enum indication of bit shift
215 */
216#define OF_FLAG_ENUM_SET(flags, e_val) OF_FLAG_SET(flags, 1 << (e_val))
217
218/**
219 * @brief Clear a flag where the value is an enum indication of bit shift
220 */
221#define OF_FLAG_ENUM_CLEAR(flags, e_val) OF_FLAG_CLEAR(flags, 1 << (e_val))
222
223/**
224 * @brief Test a flag where the value is an enum indication of bit shift
225 */
226#define OF_FLAG_ENUM_TEST(flags, e_val) OF_FLAG_TEST(flags, 1 << (e_val))
227""" % (", ".join(vbv_params), vbv_code))
228
229 # For each group of identifiers, bunch ident defns
230 count = 1
231 keys = of_g.identifiers_by_group.keys()
232 keys.sort()
233 for group in keys:
234 idents = of_g.identifiers_by_group[group]
235 idents.sort()
236 out.write("""
237/****************************************************************
Andreas Wundsam53256162013-05-02 14:05:53 -0700238 * Identifiers from %s
Rich Lanea06d0c32013-03-25 08:52:03 -0700239 *****************************************************************/
240""" % group)
241 for ident in idents:
242 info = of_g.identifiers[ident]
243
244 keys = info["values_by_version"].keys()
245 keys.sort()
246
247 out.write("""
248/*
249 * Defines for %(ident)s
250 * Original name %(ofp_name)s
251 */
252""" % dict(ident=ident, ofp_name=info["ofp_name"]))
253
254 # Generate supported versions macro
255 if len(keys) == len(of_g.target_version_list): # Defined for all
256 out.write("""\
257#define %(ident)s_SUPPORTED(version) OF_IDENT_IN_ALL_VERSIONS
258""" % dict(ident=ident))
259 else: # Undefined for some version
260 sup_list = []
261 for version in keys:
262 sup_list.append("((version) == %s)" %
263 of_g.of_version_wire2name[version])
264 out.write("""\
265#define %(ident)s_SUPPORTED(version) \\
266 (%(sup_str)s)
267""" % dict(ident=ident, sup_str=" || \\\n ".join(sup_list)))
268
269 # Generate value macro
270 if identifiers.defined_versions_agree(of_g.identifiers,
271 of_g.target_version_list,
272 ident):
273 out.write("""\
Rich Lanef3dc3962013-05-10 16:16:48 -0700274#define %(ident)s (%(value)#x)
275#define %(ident)s_BY_VERSION(version) (%(value)#x)
Rich Lanea06d0c32013-03-25 08:52:03 -0700276""" % dict(ident=ident,value=info["common_value"]))
277 else: # Values differ between versions
278 # Generate version check and value by version
279 val_list = []
280 # Order of params matters
281 for version in of_g.target_version_list:
282 if version in info["values_by_version"]:
283 value = info["values_by_version"][version]
284 else:
285 value = identifiers.UNDEFINED_IDENT_VALUE
Rich Lanef3dc3962013-05-10 16:16:48 -0700286 val_list.append("%#x" % value)
Rich Lanea06d0c32013-03-25 08:52:03 -0700287 out.write("""\
288#define %(ident)s_BY_VERSION(version) \\
289 OF_VALUE_BY_VERSION(version, %(val_str)s)
290""" % dict(ident=ident, val_str=", ".join(val_list)))
291 if flags.ident_is_flag(ident):
292 log("Treating %s as a flag" % ident)
293 out.write("""
294#define %(ident)s_SET(flags, version) \\
295 OF_FLAG_SET(flags, %(ident)s_BY_VERSION(version))
296#define %(ident)s_TEST(flags, version) \\
297 OF_FLAG_TEST(flags, %(ident)s_BY_VERSION(version))
298#define %(ident)s_CLEAR(flags, version) \\
299 OF_FLAG_CLEAR(flags, %(ident)s_BY_VERSION(version))
300""" % dict(ident=ident))
301
302 out.write("#define %(ident)s_GENERIC %(count)d\n"
303 % dict(ident=ident, count=count))
304 count += 1 # This count should probably be promoted higher
305
306 log("Generated %d identifiers" % (count - 1))
307 out.write("\n#endif /* Loci identifiers header file */\n")
308
Rich Lanea06d0c32013-03-25 08:52:03 -0700309def match_h_gen(out, name):
310 """
311 Generate code for
312 @param out The file handle to write to
313 @param name The name of the file
314 """
315 c_match.match_h_top_matter(out, name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700316 c_match.gen_match_struct(out)
317 c_match.gen_match_comp(out)
Rich Lanea06d0c32013-03-25 08:52:03 -0700318 out.write("\n#endif /* Match header file */\n")
319
320def top_h_gen(out, name):
321 """
322 Generate code for
323 @param out The file handle to write to
324 @param name The name of the file
325 """
326 external_h_top_matter(out, name)
327 out.write("""
328
329typedef enum loci_log_level {
330 LOCI_LOG_LEVEL_TRACE,
331 LOCI_LOG_LEVEL_VERBOSE,
332 LOCI_LOG_LEVEL_INFO,
333 LOCI_LOG_LEVEL_WARN,
334 LOCI_LOG_LEVEL_ERROR,
335 LOCI_LOG_LEVEL_MSG
336} loci_log_level_t;
337
338/**
339 * @brief Output a log message.
340 * @param level The log level.
341 * @param fname The function name.
342 * @param file The file name.
343 * @param line The line number.
344 * @param format The message format string.
345 */
346typedef int (*loci_logger_f)(loci_log_level_t level,
347 const char *fname, const char *file, int line,
348 const char *format, ...);
349
350/*
351 * This variable should be set by the user of the library to redirect logs to
352 * their log infrastructure. The default drops all logs.
353 */
354extern loci_logger_f loci_logger;
355
356/**
357 * Map a generic object to the underlying wire buffer
358 *
359 * Treat as private
360 */
361#define OF_OBJECT_TO_MESSAGE(obj) \\
Rich Lanecdd542d2014-04-03 16:13:12 -0700362 ((of_message_t)(WBUF_BUF((obj)->wbuf)))
Rich Lanea06d0c32013-03-25 08:52:03 -0700363
364/**
365 * Macro for the fixed length part of an object
366 *
367 * @param obj The object whose extended length is being calculated
368 * @returns length in bytes of non-variable part of the object
369 */
370#define OF_OBJECT_FIXED_LENGTH(obj) \\
371 (of_object_fixed_len[(obj)->version][(obj)->object_id])
372
373/**
374 * Return the length of the object beyond its fixed length
375 *
376 * @param obj The object whose extended length is being calculated
377 * @returns length in bytes of non-variable part of the object
378 *
379 * Most variable length fields are alone at the end of a structure.
380 * Their length is a simple calculation, just the total length of
381 * the parent minus the length of the non-variable part of the
382 * parent's class type.
383 */
384
385#define OF_OBJECT_VARIABLE_LENGTH(obj) \\
386 ((obj)->length - OF_OBJECT_FIXED_LENGTH(obj))
387
Rich Lanea06d0c32013-03-25 08:52:03 -0700388extern int of_wire_buffer_of_match_get(of_object_t *obj, int offset,
389 of_match_t *match);
390extern int of_wire_buffer_of_match_set(of_object_t *obj, int offset,
391 of_match_t *match, int cur_len);
Rich Lanea06d0c32013-03-25 08:52:03 -0700392""")
393
394 # gen_base_types(out)
395
Rich Lanea06d0c32013-03-25 08:52:03 -0700396 out.write("""
397/****************************************************************
398 *
399 * Declarations of maps between on-the-wire type values and LOCI identifiers
400 *
401 ****************************************************************/
Rich Lanec0e20ff2013-12-15 23:40:31 -0800402
Rich Lanec0e20ff2013-12-15 23:40:31 -0800403int of_object_wire_init(of_object_t *obj, of_object_id_t base_object_id, int max_len);
404
405extern const int *const of_object_fixed_len[OF_VERSION_ARRAY_MAX];
Rich Lanea06d0c32013-03-25 08:52:03 -0700406""")
Rich Lanea06d0c32013-03-25 08:52:03 -0700407 c_type_maps.gen_type_data_header(out)
408 c_match.gen_declarations(out)
409 # @fixme Move debug stuff to own fn
410 out.write("""
411/**
412 * Macro to check consistency of length for top level objects
413 *
414 * If the object has no parent then its length should match the
415 * underlying wire buffer's current bytes.
416 */
417#define OF_LENGTH_CHECK_ASSERT(obj) \\
Rich Lanee57f0432014-02-19 10:31:53 -0800418 LOCI_ASSERT(((obj)->parent != NULL) || \\
Rich Lanecdd542d2014-04-03 16:13:12 -0700419 ((obj)->wbuf == NULL) || \\
420 (WBUF_CURRENT_BYTES((obj)->wbuf) == (obj)->length))
Rich Lanea06d0c32013-03-25 08:52:03 -0700421
422#define OF_DEBUG_DUMP
423#if defined(OF_DEBUG_DUMP)
424extern void dump_match(of_match_t *match);
425#endif /* OF_DEBUG_DUMP */
426""")
427
Rich Lane1f03f672014-08-05 00:10:00 -0700428 out.write("""
429static inline const char *
430of_class_name(of_object_t *obj)
431{
432 return of_object_id_str[obj->object_id];
433}
434""")
435
Rich Lanea06d0c32013-03-25 08:52:03 -0700436 out.write("\n#endif /* Top header file */\n")
437
438def match_c_gen(out, name):
439 """
440 Generate code for
441 @param out The file handle to write to
442 @param name The name of the file
443 """
444 c_match.match_c_top_matter(out, name)
445 c_match.gen_match_conversions(out)
446 c_match.gen_serialize(out)
447 c_match.gen_deserialize(out)
448
Rich Lanea06d0c32013-03-25 08:52:03 -0700449################################################################
450# Top Matter
451################################################################
452
453def common_top_matter(out, name):
454 loxi_utils.gen_c_copy_license(out)
455 out.write("""\
Rich Laned983aa52013-06-13 11:48:37 -0700456
Rich Lanea06d0c32013-03-25 08:52:03 -0700457/****************************************************************
458 * File: %s
459 *
460 * DO NOT EDIT
461 *
462 * This file is automatically generated
463 *
464 ****************************************************************/
465
466""" % name)
467
468 if name[-2:] == ".h":
469 out.write("""
470#if !defined(%(h)s)
471#define %(h)s
472
473""" % dict(h=h_file_to_define(name)))
474
475def base_h_content(out):
476 """
477 Generate base header file content
478
479 @param out The output file object
480 """
481
482 # @fixme Supported version should be generated based on input to LoxiGen
483
484 out.write("""
485/*
486 * Base OpenFlow definitions. These depend only on standard C headers
487 */
488#include <string.h>
489#include <stdint.h>
490
491/* g++ requires this to pick up PRI, etc.
492 * See http://gcc.gnu.org/ml/gcc-help/2006-10/msg00223.html
493 */
494#if !defined(__STDC_FORMAT_MACROS)
495#define __STDC_FORMAT_MACROS
496#endif
497#include <inttypes.h>
498
499#include <stdlib.h>
500#include <assert.h>
501#include <loci/loci_idents.h>
502
503/**
504 * Macro to enable debugging for LOCI.
505 *
506 * This enables debug output to stdout.
507 */
508#define OF_DEBUG_ENABLE
509
510#if defined(OF_DEBUG_ENABLE)
511#include <stdio.h> /* Currently for debugging */
512#define FIXME(str) do { \\
513 fprintf(stderr, "%s\\n", str); \\
514 exit(1); \\
515 } while (0)
516#define debug printf
517#else
518#define FIXME(str)
519#define debug(str, ...)
520#endif /* OF_DEBUG_ENABLE */
521
522/**
523 * The type of a function used by the LOCI dump/show functions to
524 * output text. Essentially the same signature as fprintf. May
525 * be called many times per invocation of e.g. of_object_show().
526 */
527typedef int (*loci_writer_f)(void *cookie, const char *fmt, ...);
528
529/**
530 * Check if a version is supported
531 */
Rich Lane2b49df62014-10-14 11:41:28 -0700532#define OF_VERSION_OKAY(v) ((v) >= OF_VERSION_1_0 && (v) <= OF_VERSION_1_4)
Rich Lanea06d0c32013-03-25 08:52:03 -0700533
534""")
535 gen_version_enum(out)
536 out.write("\n")
537
538 # for c_name in of_g.ofp_constants:
539 # val = str(of_g.ofp_constants[c_name])
540 # out.write("#define %s %s\n" % (c_name, val))
541 # out.write("\n")
542
543 out.write("""
544typedef enum of_error_codes_e {
545 OF_ERROR_NONE = 0,
546 OF_ERROR_RESOURCE = -1, /* Could not allocate space */
547 OF_ERROR_PARAM = -2, /* Bad parameter */
548 OF_ERROR_VERSION = -3, /* Version not supported */
549 OF_ERROR_RANGE = -4, /* End of list indication */
550 OF_ERROR_COMPAT = -5, /* Incompatible assignment */
551 OF_ERROR_PARSE = -6, /* Error in parsing data */
552 OF_ERROR_INIT = -7, /* Uninitialized data */
553 OF_ERROR_UNKNOWN = -8 /* Unknown error */
554} of_error_codes_t;
555
556#define OF_ERROR_STRINGS "none", \\
557 "resource", \\
558 "parameter", \\
559 "version", \\
560 "range", \\
561 "incompatible", \\
562 "parse", \\
563 "init", \\
564 "unknown"
565
Rich Laneb157b0f2013-03-27 13:55:28 -0700566extern const char *const of_error_strings[];
Rich Lanea06d0c32013-03-25 08:52:03 -0700567
Rich Lanea8b54632014-02-19 11:17:47 -0800568#ifdef __GNUC__
569#define LOCI_NORETURN_ATTR __attribute__((__noreturn__))
570#else
571#define LOCI_NORETURN_ATTR
572#endif
573
574extern void loci_assert_fail(
575 const char *cond,
576 const char *file,
577 unsigned int line) LOCI_NORETURN_ATTR;
578
Rich Lane53757732013-02-23 17:00:10 -0800579#ifndef NDEBUG
Rich Lanea8b54632014-02-19 11:17:47 -0800580#define LOCI_ASSERT(val) ((val) ? (void)0 : loci_assert_fail(#val, __FILE__, __LINE__))
Rich Lane53757732013-02-23 17:00:10 -0800581#else
Rich Lanee57f0432014-02-19 10:31:53 -0800582#define LOCI_ASSERT(val)
Rich Lane53757732013-02-23 17:00:10 -0800583#endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700584
585/*
586 * Some LOCI object accessors can fail, and it's easy to forget to check.
587 * On certain compilers we can trigger a warning if the error code
588 * is ignored.
589 */
590#ifndef DISABLE_WARN_UNUSED_RESULT
591#ifdef __GNUC__
592#define WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
593#else
594#define WARN_UNUSED_RESULT
595#endif
596#else
597#define WARN_UNUSED_RESULT
598#endif
599
600typedef union of_generic_u of_generic_t;
601typedef struct of_object_s of_object_t;
602
603/* Define ipv4 address as uint32 */
604typedef uint32_t of_ipv4_t;
605
606/* Table ID is the OF standard uint8 */
607typedef uint8_t of_table_id_t;
608
609#define OF_MAC_ADDR_BYTES 6
610typedef struct of_mac_addr_s {
611 uint8_t addr[OF_MAC_ADDR_BYTES];
612} of_mac_addr_t;
613
614#define OF_IPV6_BYTES 16
615typedef struct of_ipv6_s {
616 uint8_t addr[OF_IPV6_BYTES];
617} of_ipv6_t;
618
Rich Lane119289c2014-12-01 13:44:08 -0800619typedef struct of_bitmap_512_s {
620 uint64_t words[8];
621} of_bitmap_512_t;
622
Rich Lanea06d0c32013-03-25 08:52:03 -0700623extern const of_mac_addr_t of_mac_addr_all_ones;
624extern const of_mac_addr_t of_mac_addr_all_zeros;
625
626extern const of_ipv6_t of_ipv6_all_ones;
627extern const of_ipv6_t of_ipv6_all_zeros;
628
Rich Lane119289c2014-12-01 13:44:08 -0800629extern const of_bitmap_512_t of_bitmap_512_all_ones;
630extern const of_bitmap_512_t of_bitmap_512_all_zeroes;
631
Rich Lanea06d0c32013-03-25 08:52:03 -0700632/**
633 * Generic zero and all-ones values of size 16 bytes.
634 *
Rich Lane119289c2014-12-01 13:44:08 -0800635 * bitmap_512 is longest data type we worry about for comparisons
Rich Lanea06d0c32013-03-25 08:52:03 -0700636 */
Rich Lane119289c2014-12-01 13:44:08 -0800637#define of_all_zero_value of_bitmap_512_all_zeroes
638#define of_all_ones_value of_bitmap_512_all_ones
Rich Lanea06d0c32013-03-25 08:52:03 -0700639
640/**
641 * Non-zero/all ones check for arbitrary type of size <= 16 bytes
642 */
643#define OF_VARIABLE_IS_NON_ZERO(_ptr) \\
644 (MEMCMP(&of_all_zero_value, (_ptr), sizeof(*(_ptr))))
645#define OF_VARIABLE_IS_ALL_ONES(_ptr) \\
646 (!MEMCMP(&of_all_ones_value, (_ptr), sizeof(*(_ptr))))
647
648/* The octets object is a struct holding pointer and length */
649typedef struct of_octets_s {
650 uint8_t *data;
651 int bytes;
652} of_octets_t;
653
654/* Macro to convert an octet object to a pointer; currently trivial */
655#define OF_OCTETS_POINTER_GET(octet_ptr) ((octet_ptr)->data)
656#define OF_OCTETS_POINTER_SET(octet_ptr, ptr) (octet_ptr)->data = (ptr)
657#define OF_OCTETS_BYTES_GET(octet_ptr) ((octet_ptr)->bytes)
658#define OF_OCTETS_BYTES_SET(octet_ptr, bytes) (octet_ptr)->bytes = (bytes)
659
660/* Currently these are categorized as scalars */
661typedef char of_port_name_t[OF_MAX_PORT_NAME_LEN];
Praseed Balakrishnan7f718782014-09-18 17:02:48 -0700662typedef char of_app_code_t[OF_APP_CODE_LEN];
Rich Lanea06d0c32013-03-25 08:52:03 -0700663typedef char of_table_name_t[OF_MAX_TABLE_NAME_LEN];
664typedef char of_desc_str_t[OF_DESC_STR_LEN];
665typedef char of_serial_num_t[OF_SERIAL_NUM_LEN];
Rich Lanef8a3d002014-03-19 13:33:52 -0700666typedef char of_str64_t[64];
Rich Lanea06d0c32013-03-25 08:52:03 -0700667
Rich Lane3b2fd832013-09-24 13:44:08 -0700668typedef struct of_bitmap_128_s {
669 uint64_t hi;
670 uint64_t lo;
671} of_bitmap_128_t;
672
Rich Lanefab0c822013-12-30 11:46:48 -0800673typedef struct of_checksum_128_s {
674 uint64_t hi;
675 uint64_t lo;
676} of_checksum_128_t;
677
Rich Lanea06d0c32013-03-25 08:52:03 -0700678/* These are types which change across versions. */
679typedef uint32_t of_port_no_t;
680typedef uint16_t of_fm_cmd_t;
681typedef uint64_t of_wc_bmap_t;
682typedef uint64_t of_match_bmap_t;
683
684#define MEMMOVE(dest, src, bytes) memmove(dest, src, bytes)
685#define MEMSET(dest, val, bytes) memset(dest, val, bytes)
686#define MEMCPY(dest, src, bytes) memcpy(dest, src, bytes)
687#define MEMCMP(a, b, bytes) memcmp(a, b, bytes)
688#define MALLOC(bytes) malloc(bytes)
689#define FREE(ptr) free(ptr)
690
691/** Try an operation and return on failure. */
692#define OF_TRY(op) do { \\
693 int _rv; \\
694 if ((_rv = (op)) < 0) { \\
695 LOCI_LOG_ERROR("ERROR %d at %s:%d\\n", _rv, __FILE__, __LINE__); \\
696 return _rv; \\
697 } \\
698 } while (0)
699
700/* The extent of an OF match object is determined by its length field, but
701 * aligned to 8 bytes
702 */
703
704#define OF_MATCH_BYTES(length) (((length) + 7) & 0xfff8)
705
Rich Lanedfcafae2014-03-06 16:56:49 -0800706#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
Rich Lanea06d0c32013-03-25 08:52:03 -0700707#define U16_NTOH(val) (val)
708#define U32_NTOH(val) (val)
709#define U64_NTOH(val) (val)
710#define IPV6_NTOH(dst, src) /* NOTE different syntax; currently no-op */
711#define U16_HTON(val) (val)
712#define U32_HTON(val) (val)
713#define U64_HTON(val) (val)
714#define IPV6_HTON(dst, src) /* NOTE different syntax; currently no-op */
715#else /* Little Endian */
Rich Lane69e247e2014-03-05 10:27:22 -0800716#define U16_NTOH(val) (((val) >> 8) | (((val) & 0xff) << 8))
Rich Lanea06d0c32013-03-25 08:52:03 -0700717#define U32_NTOH(val) ((((val) & 0xff000000) >> 24) | \\
718 (((val) & 0x00ff0000) >> 8) | \\
719 (((val) & 0x0000ff00) << 8) | \\
720 (((val) & 0x000000ff) << 24))
721#define U64_NTOH(val) ((((val) & 0xff00000000000000LL) >> 56) | \\
722 (((val) & 0x00ff000000000000LL) >> 40) | \\
723 (((val) & 0x0000ff0000000000LL) >> 24) | \\
724 (((val) & 0x000000ff00000000LL) >> 8) | \\
725 (((val) & 0x00000000ff000000LL) << 8) | \\
726 (((val) & 0x0000000000ff0000LL) << 24) | \\
727 (((val) & 0x000000000000ff00LL) << 40) | \\
728 (((val) & 0x00000000000000ffLL) << 56))
729#define IPV6_NTOH(dst, src) /* NOTE different syntax; currently no-op */
730#define U16_HTON(val) U16_NTOH(val)
731#define U32_HTON(val) U32_NTOH(val)
732#define U64_HTON(val) U64_NTOH(val)
733#define IPV6_HTON(dst, src) /* NOTE different syntax; currently no-op */
734#endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700735""")
736
737def external_h_top_matter(out, name):
738 """
739 Generate top matter for external header file
740
741 @param name The name of the output file
742 @param out The output file object
743 """
744 common_top_matter(out, name)
745 out.write("""
746#include <loci/loci_base.h>
747#include <loci/of_message.h>
748#include <loci/of_match.h>
749#include <loci/of_object.h>
Rich Lanedef2e512013-12-15 15:54:02 -0800750#include <loci/loci_classes.h>
Rich Lanedc46fe22014-04-03 15:10:38 -0700751#include <loci/loci_class_metadata.h>
Rich Lanea06d0c32013-03-25 08:52:03 -0700752
753/****************************************************************
754 *
755 * This file is divided into the following sections.
756 *
757 * A few object specific macros
758 * Class typedefs (no struct definitions)
759 * Per-data type accessor function typedefs
760 * Per-class new/delete function typedefs
761 * Per-class static delete functions
762 * Per-class, per-member accessor declarations
763 * Per-class structure definitions
Rich Lanea06d0c32013-03-25 08:52:03 -0700764 * Pointer set function declarations
765 * Some special case macros
766 *
767 ****************************************************************/
768""")
769
Rich Lanea06d0c32013-03-25 08:52:03 -0700770################################################################
771#
772################################################################
773
774def gen_version_enum(out):
775 """
776 Generate the enumerated type for versions in LoxiGen
777 @param out The file object to which to write the decs
778
779 This just uses the wire versions for now
780 """
781 out.write("""
782/**
783 * Enumeration of OpenFlow versions
784 *
785 * The wire protocol numbers are currently used for values of the corresponding
786 * version identifiers.
787 */
788typedef enum of_version_e {
789 OF_VERSION_UNKNOWN = 0,
790""")
791
792 is_first = True
793 max = 0
794 for v in of_g.wire_ver_map:
795 if is_first:
796 is_first = False
797 else:
798 out.write(",\n")
799 if v > max:
800 max = v
801 out.write(" %s = %d" % (of_g.wire_ver_map[v], v))
802
803 out.write("""
804} of_version_t;
805
806/**
807 * @brief Use this when declaring arrays indexed by wire version
808 */
809#define OF_VERSION_ARRAY_MAX %d
810""" % (max + 1))
Andreas Wundsam53256162013-05-02 14:05:53 -0700811
Rich Lanea06d0c32013-03-25 08:52:03 -0700812def gen_object_enum(out):
813 """
814 Generate the enumerated type for object identification in LoxiGen
815 @param out The file object to which to write the decs
816 """
817 out.write("""
818
819/**
820 * Enumeration of OpenFlow objects
821 *
822 * We enumerate the OpenFlow objects used internally. Note that some
823 * message types are determined both by an outer type (message type like
824 * stats_request) and an inner type (port stats). These are different
825 * messages in ofC.
826 *
827 * These values are for internal use only. They will change with
828 * different versions of ofC.
829 */
830
831typedef enum of_object_id_e {
832 /* Root object type */
833 OF_OBJECT_INVALID = -1, /* "invalid" return value for mappings */
834 OF_OBJECT = 0, /* Generic, untyped object */
835
836 /* OpenFlow message objects */
837""")
838 last = 0
839 msg_count = 0
840 for cls in of_g.ordered_messages:
841 out.write(" %s = %d,\n" % (enum_name(cls),
842 of_g.unified[cls]["object_id"]))
843 msg_count = of_g.unified[cls]["object_id"] + 1
844
845 out.write("\n /* Non-message objects */\n")
846 for cls in of_g.ordered_non_messages:
847 out.write(" %s = %d,\n" % (enum_name(cls),
848 of_g.unified[cls]["object_id"]))
849 last = of_g.unified[cls]["object_id"]
850 out.write("\n /* List objects */\n")
851 for cls in of_g.ordered_list_objects:
852 out.write(" %s = %d,\n" % (enum_name(cls),
853 of_g.unified[cls]["object_id"]))
854 last = of_g.unified[cls]["object_id"]
855
Rich Lanea06d0c32013-03-25 08:52:03 -0700856 out.write("""
857 OF_OBJECT_COUNT = %d
858} of_object_id_t;
859
Rich Laneb157b0f2013-03-27 13:55:28 -0700860extern const char *const of_object_id_str[];
Rich Lanea06d0c32013-03-25 08:52:03 -0700861
862#define OF_MESSAGE_OBJECT_COUNT %d
863""" % ((last + 1), msg_count))
864
Rich Lanea06d0c32013-03-25 08:52:03 -0700865################################################################
866#
867# Internal Utility Functions
868#
869################################################################
870
871
872def acc_name(cls, m_name):
873 """
874 Generate the root name of an accessor function for typedef
875 @param cls The class name
876 @param m_name The member name
877 """
878 (m_type, get_rv) = get_acc_rv(cls, m_name)
879 return "%s_%s" % (cls, m_type)
880
881def get_acc_rv(cls, m_name):
882 """
883 Determine the data type and return type for a get accessor.
884
885 The return type may be "void" or it may be the accessor type
886 depending on the system configuration and on the data type.
887
888 @param cls The class name
889 @param m_name The member name
890 @return A pair (m_type, rv) where m_type is the unified type of the
891 member and rv is the get_accessor return type
892 """
893 member = of_g.unified[cls]["union"][m_name]
894 m_type = member["m_type"]
895 rv = "int"
Rich Lanea06d0c32013-03-25 08:52:03 -0700896 if m_type[-2:] == "_t":
897 m_type = m_type[:-2]
898
899 return (m_type, rv)
900
901def param_list(cls, m_name, a_type):
902 """
903 Generate the parameter list (no parens) for an a_type accessor
904 @param cls The class name
905 @param m_name The member name
906 @param a_type One of "set" or "get" or TBD
907 """
908 member = of_g.unified[cls]["union"][m_name]
909 m_type = member["m_type"]
910 params = ["%s_t *obj" % cls]
911 if a_type == "set":
912 if loxi_utils.type_is_scalar(m_type):
913 params.append("%s %s" % (m_type, m_name))
914 else:
915 params.append("%s *%s" % (m_type, m_name))
916 elif a_type in ["get", "bind"]:
917 params.append("%s *%s" % (m_type, m_name))
918 else:
919 debug("Class %s, name %s Bad param list a_type: %s" %
920 (cls, m_name, a_type))
921 sys.exit(1)
922 return params
923
Rich Lanea06d0c32013-03-25 08:52:03 -0700924def field_ver_get(cls, m_name):
925 """
926 Generate a dict, indexed by wire version, giving a pair (type, offset)
927
928 @param cls The class name
929 @param m_name The name of the class member
930
931 If offset is not known for m_name, the type
932 A dict is used for more convenient indexing.
933 """
934 result = {}
935
936 for ver in of_g.unified[cls]:
937 if type(ver) == type(0): # It's a version
938 if "use_version" in of_g.unified[cls][ver]: # deref version ref
939 ref_ver = of_g.unified[cls][ver]["use_version"]
940 members = of_g.unified[cls][ref_ver]["members"]
941 else:
942 members = of_g.unified[cls][ver]["members"]
943 idx = loxi_utils.member_to_index(m_name, members)
944 if (idx < 0):
945 continue # Member not in this version
946 m_type = members[idx]["m_type"]
947 offset = members[idx]["offset"]
948
949 # If m_type is mixed, get wire version type from global data
950 if m_type in of_g.of_mixed_types and \
951 ver in of_g.of_mixed_types[m_type]:
952 m_type = of_g.of_mixed_types[m_type][ver]
953
954 # add version to result list
955 result[ver] = (m_type, offset)
956
957 return result
958
959def v3_match_offset_get(cls):
960 """
Andreas Wundsam53256162013-05-02 14:05:53 -0700961 Return the offset of an OF 1.2 match in an object if it has such;
Rich Lanea06d0c32013-03-25 08:52:03 -0700962 otherwise return -1
963 """
964 result = field_ver_get(cls, "match")
965 if of_g.VERSION_1_2 in result:
966 if result[of_g.VERSION_1_2][0] == "of_match_v3_t":
967 return result[of_g.VERSION_1_2][1]
968 return -1
969
970################################################################
971#
972# OpenFlow Object Definitions
973#
974################################################################
975
Rich Lanea06d0c32013-03-25 08:52:03 -0700976def gen_struct_typedefs(out):
977 """
978 Generate typedefs for all struct objects
979 @param out The file for output, already open
980 """
981
Rich Lanea06d0c32013-03-25 08:52:03 -0700982 out.write("\n/* LOCI object typedefs */\n")
983 for cls in of_g.standard_class_order:
Rich Lane85767872013-12-15 16:24:42 -0800984 template = "typedef of_object_t %(cls)s_t;\n"
985 out.write(template % dict(cls=cls))
Rich Lanea06d0c32013-03-25 08:52:03 -0700986
987 out.write("""
988/****************************************************************
989 *
990 * Additional of_object defines
991 * These are needed for some static inline ops, so we put them here.
992 *
993 ****************************************************************/
994
995/* Delete an OpenFlow object without reference to its type */
996extern void of_object_delete(of_object_t *obj);
997
998""")
999
Rich Lanea06d0c32013-03-25 08:52:03 -07001000################################################################
1001#
Rich Lanea06d0c32013-03-25 08:52:03 -07001002# Accessor Functions
1003#
1004################################################################
1005
Andreas Wundsam53256162013-05-02 14:05:53 -07001006
Rich Lanea06d0c32013-03-25 08:52:03 -07001007def gen_accessor_declarations(out):
1008 """
1009 Generate the declaration of each version independent accessor
1010
1011 @param out The file to which to write the decs
1012 """
1013
1014 out.write("""
1015/****************************************************************
1016 *
1017 * Unified, per-member accessor function declarations
1018 *
1019 ****************************************************************/
1020""")
1021 for cls in of_g.standard_class_order:
Rich Lane6171e632014-11-07 10:23:38 -08001022 if type_maps.class_is_virtual(cls) and not loxi_utils.class_is_list(cls):
Rich Lanea06d0c32013-03-25 08:52:03 -07001023 continue
1024 out.write("\n/* Unified accessor functions for %s */\n" % cls)
1025 for m_name in of_g.ordered_members[cls]:
1026 if m_name in of_g.skip_members:
1027 continue
1028 m_type = loxi_utils.member_base_type(cls, m_name)
1029 base_name = "%s_%s" % (cls, m_name)
1030 gparams = ",\n ".join(param_list(cls, m_name, "get"))
1031 get_ret_type = accessor_return_type("get", m_type)
1032 sparams = ",\n ".join(param_list(cls, m_name, "set"))
1033 set_ret_type = accessor_return_type("set", m_type)
1034 bparams = ",\n ".join(param_list(cls, m_name, "bind"))
1035 bind_ret_type = accessor_return_type("bind", m_type)
1036
1037 if loxi_utils.type_is_of_object(m_type):
1038 # Generate bind accessors, but not get accessor
1039 out.write("""
1040extern %(set_ret_type)s %(base_name)s_set(
1041 %(sparams)s);
1042extern %(bind_ret_type)s %(base_name)s_bind(
1043 %(bparams)s);
1044extern %(m_type)s *%(cls)s_%(m_name)s_get(
1045 %(cls)s_t *obj);
1046""" % dict(base_name=base_name, sparams=sparams, bparams=bparams,
1047 m_name=m_name, m_type=m_type, cls=cls,
1048 set_ret_type=set_ret_type, bind_ret_type=bind_ret_type))
1049 else:
1050 out.write("""
1051extern %(set_ret_type)s %(base_name)s_set(
1052 %(sparams)s);
1053extern %(get_ret_type)s %(base_name)s_get(
1054 %(gparams)s);
1055""" % dict(base_name=base_name, gparams=gparams, sparams=sparams,
1056 get_ret_type=get_ret_type, set_ret_type=set_ret_type))
Andreas Wundsam53256162013-05-02 14:05:53 -07001057
Rich Lanea06d0c32013-03-25 08:52:03 -07001058 if loxi_utils.class_is_list(cls):
1059 e_type = loxi_utils.list_to_entry_type(cls)
1060 out.write("""
1061extern int %(cls)s_first(
Rich Lane30943262014-11-07 09:04:13 -08001062 %(cls)s_t *list, of_object_t *iter);
Rich Lanea06d0c32013-03-25 08:52:03 -07001063extern int %(cls)s_next(
Rich Lane30943262014-11-07 09:04:13 -08001064 %(cls)s_t *list, of_object_t *iter);
Rich Lanea06d0c32013-03-25 08:52:03 -07001065extern int %(cls)s_append_bind(
Rich Lane30943262014-11-07 09:04:13 -08001066 %(cls)s_t *list, of_object_t *iter);
Rich Lanea06d0c32013-03-25 08:52:03 -07001067extern int %(cls)s_append(
Rich Lane30943262014-11-07 09:04:13 -08001068 %(cls)s_t *list, of_object_t *iter);
Rich Lanea06d0c32013-03-25 08:52:03 -07001069
1070/**
1071 * Iteration macro for list of type %(cls)s
1072 * @param list Pointer to the list being iterated over of
1073 * type %(cls)s
1074 * @param elt Pointer to an element of type %(e_type)s
1075 * @param rv On exiting the loop will have the value OF_ERROR_RANGE.
1076 */
1077#define %(u_cls)s_ITER(list, elt, rv) \\
1078 for ((rv) = %(cls)s_first((list), (elt)); \\
1079 (rv) == OF_ERROR_NONE; \\
1080 (rv) = %(cls)s_next((list), (elt)))
1081""" % dict(u_cls=cls.upper(), cls=cls, e_type=e_type))
1082
1083
1084def wire_accessor(m_type, a_type):
1085 """
1086 Returns the name of the a_type accessor for low level wire buff offset
1087 @param m_type The member type
1088 @param a_type The accessor type (set or get)
1089 """
1090 # Strip off _t if present
1091 if m_type in of_g.of_base_types:
1092 m_type = of_g.of_base_types[m_type]["short_name"]
1093 if m_type in of_g.of_mixed_types:
1094 m_type = of_g.of_mixed_types[m_type]["short_name"]
1095 if m_type[-2:] == "_t":
1096 m_type = m_type[:-2]
1097 if m_type == "octets":
1098 m_type = "octets_data"
1099 return "of_wire_buffer_%s_%s" % (m_type, a_type)
1100
Rich Lane713d9282013-12-30 15:21:35 -08001101def get_len_macro(cls, m_name, m_type, version):
Rich Lanea06d0c32013-03-25 08:52:03 -07001102 """
1103 Get the length macro for m_type in cls
1104 """
1105 if m_type.find("of_match") == 0:
1106 return "_WIRE_MATCH_PADDED_LEN(obj, offset)"
1107 if m_type.find("of_list_oxm") == 0:
1108 return "wire_match_len(obj, 0) - 4"
1109 if loxi_utils.class_is_tlv16(m_type):
1110 return "_TLV16_LEN(obj, offset)"
1111 if cls == "of_packet_out" and m_type == "of_list_action_t":
1112 return "_PACKET_OUT_ACTION_LEN(obj)"
Rich Lane713d9282013-12-30 15:21:35 -08001113 if cls == "of_bsn_gentable_entry_add" and m_name == "key":
1114 return "of_object_u16_get(obj, 18)"
1115 if cls == "of_bsn_gentable_entry_desc_stats_entry" and m_name == "key":
1116 return "of_object_u16_get(obj, 2)"
1117 if cls == "of_bsn_gentable_entry_stats_entry" and m_name == "key":
1118 return "of_object_u16_get(obj, 2)"
Rich Lanea06d0c32013-03-25 08:52:03 -07001119 # Default is everything to the end of the object
1120 return "_END_LEN(obj, offset)"
1121
1122def gen_accessor_offsets(out, cls, m_name, version, a_type, m_type, offset):
1123 """
1124 Generate the sub-object offset and length calculations for accessors
1125 @param out File being written
1126 @param m_name Name of member
1127 @param version Wire version being processed
1128 @param a_type The accessor type (set or get)
1129 @param m_type The original member type
1130 @param offset The offset of the object or -1 if not fixed
1131 """
1132 # determine offset
1133 o_str = "%d" % offset # Default is fixed length
1134 if offset == -1:
1135 # There are currently 4 special cases for this
1136 # In general, get offset and length of predecessor
1137 if (loxi_utils.cls_is_flow_mod(cls) and m_name == "instructions"):
1138 pass
1139 elif (cls == "of_flow_stats_entry" and m_name == "instructions"):
1140 pass
1141 elif (cls == "of_packet_in" and m_name == "data"):
1142 pass
1143 elif (cls == "of_packet_out" and m_name == "data"):
1144 pass
Rich Lane713d9282013-12-30 15:21:35 -08001145 elif (cls == "of_bsn_gentable_entry_add" and m_name == "value"):
1146 pass
1147 elif (cls == "of_bsn_gentable_entry_desc_stats_entry" and m_name == "value"):
1148 pass
1149 elif (cls == "of_bsn_gentable_entry_stats_entry" and m_name == "stats"):
1150 pass
Rich Lanea06d0c32013-03-25 08:52:03 -07001151 else:
1152 debug("Error: Unknown member with offset == -1")
1153 debug(" cls %s, m_name %s, version %d" % (cls, m_name, version))
1154 sys.exit(1)
1155 o_str = "_%s_%s_OFFSET(obj)" % (cls.upper()[3:], m_name.upper())
1156
1157 out.write("""\
1158 offset = %s;
1159""" % o_str);
1160
1161 # This could be moved to main body but for version check
1162 if not loxi_utils.type_is_scalar(m_type):
1163 if loxi_utils.class_is_var_len(m_type[:-2], version) or \
1164 m_type == "of_match_t":
Rich Lane713d9282013-12-30 15:21:35 -08001165 len_macro = get_len_macro(cls, m_name, m_type, version)
Rich Lanea06d0c32013-03-25 08:52:03 -07001166 else:
1167 len_macro = "%d" % of_g.base_length[(m_type[:-2], version)]
1168 out.write(" cur_len = %s;\n" % len_macro)
1169 out.write(" break;\n")
1170
1171def length_of(m_type, version):
1172 """
1173 Return the length of a type based on the version
1174 """
1175 if m_type in of_g.of_mixed_types:
1176 m_type = of_g.of_mixed_types[m_type][version]
1177 if m_type in of_g.of_base_types:
1178 return of_g.of_base_types[m_type]["bytes"]
1179 if (m_type[:-2], version) in of_g.base_length:
1180 return of_g.base_length[(m_type[:-2], version)]
1181 print "Unknown length request", m_type, version
1182 sys.exit(1)
Andreas Wundsam53256162013-05-02 14:05:53 -07001183
Rich Lanea06d0c32013-03-25 08:52:03 -07001184
1185def gen_get_accessor_body(out, cls, m_type, m_name):
1186 """
1187 Generate the common operations for a get accessor
1188 """
1189 if loxi_utils.type_is_scalar(m_type):
1190 ver = "" # See if version required for scalar update
1191 if m_type in of_g.of_mixed_types:
1192 ver = "ver, "
1193 out.write("""\
1194 %(wa)s(%(ver)swbuf, abs_offset, %(m_name)s);
1195""" % dict(wa=wire_accessor(m_type, "get"), ver=ver, m_name=m_name))
1196
1197 if m_type == "of_port_no_t":
1198 out.write(" OF_PORT_NO_VALUE_CHECK(*%s, ver);\n" % m_name)
1199 elif m_type == "of_octets_t":
1200 out.write("""\
Rich Lanee57f0432014-02-19 10:31:53 -08001201 LOCI_ASSERT(cur_len + abs_offset <= WBUF_CURRENT_BYTES(wbuf));
Rich Lanea06d0c32013-03-25 08:52:03 -07001202 %(m_name)s->bytes = cur_len;
1203 %(m_name)s->data = OF_WIRE_BUFFER_INDEX(wbuf, abs_offset);
1204""" % dict(m_name=m_name))
1205 elif m_type == "of_match_t":
1206 out.write("""
Rich Lanee57f0432014-02-19 10:31:53 -08001207 LOCI_ASSERT(cur_len + abs_offset <= WBUF_CURRENT_BYTES(wbuf));
Rich Lanee0b70cc2014-06-12 15:02:22 -07001208 OF_TRY(of_match_deserialize(ver, %(m_name)s, obj, offset, cur_len));
Rich Lanea06d0c32013-03-25 08:52:03 -07001209""" % dict(m_name=m_name))
Rich Lanec247ade2014-11-07 09:33:24 -08001210 elif m_type == "of_oxm_t":
Rich Lane90020b42014-04-07 12:05:45 -07001211 out.write("""
1212 /* Initialize child */
1213 %(m_type)s_init(%(m_name)s, obj->version, 0, 1);
1214 /* Attach to parent */
Rich Laned1fe6972014-06-12 14:53:24 -07001215 of_object_attach(obj, %(m_name)s, offset, cur_len);
Rich Lane90020b42014-04-07 12:05:45 -07001216 of_object_wire_init(%(m_name)s, OF_OXM, 0);
1217""" % dict(m_type=m_type[:-2], m_name=m_name))
Rich Lanec247ade2014-11-07 09:33:24 -08001218 elif m_type == "of_bsn_vport_t":
Wilson Ngd6181882014-04-14 16:28:35 -07001219 out.write("""
1220 /* Initialize child */
1221 %(m_type)s_init(%(m_name)s, obj->version, 0, 1);
1222 /* Attach to parent */
Rich Laned1fe6972014-06-12 14:53:24 -07001223 of_object_attach(obj, %(m_name)s, offset, cur_len);
Wilson Ngd6181882014-04-14 16:28:35 -07001224 of_object_wire_init(%(m_name)s, OF_BSN_VPORT, 0);
1225""" % dict(m_type=m_type[:-2], m_name=m_name))
Rich Lanea06d0c32013-03-25 08:52:03 -07001226 else:
1227 out.write("""
1228 /* Initialize child */
1229 %(m_type)s_init(%(m_name)s, obj->version, 0, 1);
1230 /* Attach to parent */
Rich Laned1fe6972014-06-12 14:53:24 -07001231 of_object_attach(obj, %(m_name)s, offset, cur_len);
Rich Lanea06d0c32013-03-25 08:52:03 -07001232""" % dict(m_type=m_type[:-2], m_name=m_name))
1233
1234
1235def gen_set_accessor_body(out, cls, m_type, m_name):
1236 """
1237 Generate the contents of a set accessor
1238 """
1239 if loxi_utils.type_is_scalar(m_type) or m_type == "of_octets_t":
1240 ver = "" # See if version required for scalar update
1241 if m_type in of_g.of_mixed_types:
1242 ver = "ver, "
1243 cur_len = "" # See if version required for scalar update
1244 if m_type == "of_octets_t":
1245 cur_len = ", cur_len"
1246 out.write("""\
1247 new_len = %(m_name)s->bytes;
1248 of_wire_buffer_grow(wbuf, abs_offset + (new_len - cur_len));
1249""" % dict(m_name=m_name))
1250 out.write("""\
1251 %(wa)s(%(ver)swbuf, abs_offset, %(m_name)s%(cur_len)s);
1252""" % dict(wa=wire_accessor(m_type, "set"), ver=ver, cur_len=cur_len,
1253 m_name=m_name))
1254
1255 elif m_type == "of_match_t":
1256 out.write("""
Rich Lanee0b70cc2014-06-12 15:02:22 -07001257 {
1258 /* Match object */
1259 of_octets_t match_octets;
1260 OF_TRY(of_match_serialize(ver, %(m_name)s, &match_octets));
1261 new_len = match_octets.bytes;
1262 of_wire_buffer_replace_data(wbuf, abs_offset, cur_len,
1263 match_octets.data, new_len);
1264 /* Free match serialized octets */
1265 FREE(match_octets.data);
1266 }
Rich Lanea06d0c32013-03-25 08:52:03 -07001267""" % dict(m_name=m_name))
1268
1269 else: # Other object type
1270 out.write("\n /* LOCI object type */")
1271 # Need to special case OXM list
1272 out.write("""
1273 new_len = %(m_name)s->length;
1274 /* If underlying buffer already shared; nothing to do */
Rich Lanecdd542d2014-04-03 16:13:12 -07001275 if (obj->wbuf == %(m_name)s->wbuf) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001276 of_wire_buffer_grow(wbuf, abs_offset + new_len);
1277 /* Verify that the offsets are correct */
Rich Lanee57f0432014-02-19 10:31:53 -08001278 LOCI_ASSERT(abs_offset == OF_OBJECT_ABSOLUTE_OFFSET(%(m_name)s, 0));
1279 /* LOCI_ASSERT(new_len == cur_len); */ /* fixme: may fail for OXM lists */
Rich Lanea06d0c32013-03-25 08:52:03 -07001280 return %(ret_success)s;
1281 }
1282
1283 /* Otherwise, replace existing object in data buffer */
1284 of_wire_buffer_replace_data(wbuf, abs_offset, cur_len,
1285 OF_OBJECT_BUFFER_INDEX(%(m_name)s, 0), new_len);
1286""" % dict(m_name=m_name, ret_success=accessor_return_success("set", m_type)))
1287
1288 if not loxi_utils.type_is_scalar(m_type):
1289 if cls == "of_packet_out" and m_type == "of_list_action_t":
1290 out.write("""
1291 /* Special case for setting action lengths */
1292 _PACKET_OUT_ACTION_LEN_SET(obj, %(m_name)s->length);
1293""" % dict(m_name=m_name))
Rich Lane713d9282013-12-30 15:21:35 -08001294 elif cls == "of_bsn_gentable_entry_add" and m_name == "key":
1295 out.write("""
1296 /* Special case for setting key length */
1297 of_object_u16_set(obj, 18, %(m_name)s->length);
1298""" % dict(m_name=m_name))
1299 elif cls in ["of_bsn_gentable_entry_desc_stats_entry", "of_bsn_gentable_entry_stats_entry"] and m_name == "key":
1300 out.write("""
1301 /* Special case for setting key length */
1302 of_object_u16_set(obj, 2, %(m_name)s->length);
1303""" % dict(m_name=m_name))
Rich Lanea06d0c32013-03-25 08:52:03 -07001304 elif m_type not in ["of_match_t", "of_octets_t"]:
1305 out.write("""
1306 /* @fixme Shouldn't this precede copying value's data to buffer? */
Rich Lanedc46fe22014-04-03 15:10:38 -07001307 of_object_wire_length_set((of_object_t *)%(m_name)s, %(m_name)s->length);
Rich Lanea06d0c32013-03-25 08:52:03 -07001308""" % dict(m_name=m_name))
1309 out.write("""
1310 /* Not scalar, update lengths if needed */
1311 delta = new_len - cur_len;
1312 if (delta != 0) {
1313 /* Update parent(s) */
1314 of_object_parent_length_update((of_object_t *)obj, delta);
1315 }
1316""")
1317
1318def obj_assert_check(cls):
1319 """
1320 The body of the assert check for an accessor
1321 We allow all versions of add/delete/modify to use the same accessors
1322 """
1323 if cls in ["of_flow_modify", "of_flow_modify_strict",
1324 "of_flow_delete", "of_flow_delete_strict",
1325 "of_flow_add"]:
1326 return "IS_FLOW_MOD_SUBTYPE(obj->object_id)"
1327 else:
1328 return "obj->object_id == %s" % cls.upper()
1329
1330def gen_of_object_get(out, cls, m_name, m_type):
1331 sub_cls = m_type[:-2]
1332 out.write("""
1333/**
Andreas Wundsam53256162013-05-02 14:05:53 -07001334 * Create a copy of %(m_name)s into a new variable of type %(m_type)s from
Rich Lanea06d0c32013-03-25 08:52:03 -07001335 * a %(cls)s instance.
1336 *
1337 * @param obj Pointer to the source of type %(cls)s_t
1338 * @returns A pointer to a new instance of type %(m_type)s whose contents
1339 * match that of %(m_name)s from source
1340 * @returns NULL if an error occurs
1341 */
1342%(m_type)s *
1343%(cls)s_%(m_name)s_get(%(cls)s_t *obj) {
1344 %(m_type)s _%(m_name)s;
1345 %(m_type)s *_%(m_name)s_ptr;
1346
1347 %(cls)s_%(m_name)s_bind(obj, &_%(m_name)s);
1348 _%(m_name)s_ptr = (%(m_type)s *)of_object_dup(&_%(m_name)s);
1349 return _%(m_name)s_ptr;
1350}
1351""" % dict(m_name=m_name, m_type=m_type, cls=cls, sub_cls=sub_cls))
1352
1353def gen_unified_acc_body(out, cls, m_name, ver_type_map, a_type, m_type):
1354 """
1355 Generate the body of a set or get accessor function
1356
1357 @param out The file to which to write the decs
1358 @param cls The class name
1359 @param m_name The member name
1360 @param ver_type_map Maps (type, offset) pairs to a list of versions
1361 @param a_type The accessor type, set or get
1362 @param m_type The original member type
1363
1364 The type values in ver_type_map are now ignored as we've pushed down
1365 the type munging to the lower level.
1366
1367 This is unified because the version switch case processing is the
1368 same for both set and get
1369 """
1370 out.write("""{
1371 of_wire_buffer_t *wbuf;
1372 int offset = 0; /* Offset of value relative to the start obj */
1373 int abs_offset; /* Offset of value relative to start of wbuf */
1374 of_version_t ver;
1375""")
1376
1377 if not loxi_utils.type_is_scalar(m_type):
1378 out.write("""\
1379 int cur_len = 0; /* Current length of object data */
1380""")
1381 if a_type == "set":
1382 out.write("""\
1383 int new_len, delta; /* For set, need new length and delta */
1384""")
1385
Rich Lanea06d0c32013-03-25 08:52:03 -07001386 out.write("""
Rich Lanee57f0432014-02-19 10:31:53 -08001387 LOCI_ASSERT(%(assert_str)s);
Rich Lanea06d0c32013-03-25 08:52:03 -07001388 ver = obj->version;
1389 wbuf = OF_OBJECT_TO_WBUF(obj);
Rich Lanee57f0432014-02-19 10:31:53 -08001390 LOCI_ASSERT(wbuf != NULL);
Rich Lanea06d0c32013-03-25 08:52:03 -07001391
1392 /* By version, determine offset and current length (where needed) */
1393 switch (ver) {
1394""" % dict(assert_str=obj_assert_check(cls)))
1395
1396 for first in sorted(ver_type_map):
1397 (prev_t, prev_o) = ver_type_map[first]
1398 prev_len = length_of(prev_t, first)
1399 prev = first
1400 out.write(" case %s:\n" % of_g.wire_ver_map[first])
1401 break
1402
1403 for next in sorted(ver_type_map):
1404 if next == first:
1405 continue
1406
1407 (t, o) = ver_type_map[next]
1408 cur_len = length_of(t, next)
1409 if o == prev_o and cur_len == prev_len:
1410 out.write(" case %s:\n" % of_g.wire_ver_map[next])
1411 continue
1412 gen_accessor_offsets(out, cls, m_name, prev, a_type, m_type, prev_o)
1413 out.write(" case %s:\n" % of_g.wire_ver_map[next])
1414 (prev_t, prev_o, prev_len, prev) = (t, o, cur_len, next)
1415
1416 gen_accessor_offsets(out, cls, m_name, next, a_type, m_type, prev_o)
1417 out.write("""\
1418 default:
Rich Lanee57f0432014-02-19 10:31:53 -08001419 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -07001420 }
1421
1422 abs_offset = OF_OBJECT_ABSOLUTE_OFFSET(obj, offset);
Rich Lanee57f0432014-02-19 10:31:53 -08001423 LOCI_ASSERT(abs_offset >= 0);
Rich Lanea06d0c32013-03-25 08:52:03 -07001424""")
1425 if not loxi_utils.type_is_scalar(m_type):
Rich Lanee57f0432014-02-19 10:31:53 -08001426 out.write(" LOCI_ASSERT(cur_len >= 0 && cur_len < 64 * 1024);\n")
Rich Lanea06d0c32013-03-25 08:52:03 -07001427
1428 # Now generate the common accessor code
1429 if a_type in ["get", "bind"]:
1430 gen_get_accessor_body(out, cls, m_type, m_name)
1431 else:
1432 gen_set_accessor_body(out, cls, m_type, m_name)
1433
1434 out.write("""
1435 OF_LENGTH_CHECK_ASSERT(obj);
1436
1437 return %s;
1438}
1439""" % accessor_return_success(a_type, m_type))
1440
1441def gen_of_obj_bind(out, cls, m_name, m_type, ver_type_map):
1442 """
1443 For generating the bind call for OF sub-objects
1444 """
1445
1446 params = ",\n ".join(param_list(cls, m_name, "bind"))
1447 out.write("""
1448/**
1449 * Bind an object of type %(m_type)s to the parent of type %(cls)s for
1450 * member %(m_name)s
1451 * @param obj Pointer to an object of type %(cls)s.
1452 * @param %(m_name)s Pointer to the child object of type
1453 * %(m_type)s to be filled out.
1454 * \ingroup %(cls)s
1455 *
1456 * The parameter %(m_name)s is filled out to point to the same underlying
1457 * wire buffer as its parent.
1458 *
1459 */
1460""" % dict(m_name=m_name, cls=cls, m_type=m_type))
1461
1462 ret_type = accessor_return_type("bind", m_type)
1463 out.write("%s\n%s_%s_bind(\n %s)\n" % (ret_type, cls, m_name, params))
1464 gen_unified_acc_body(out, cls, m_name, ver_type_map, "bind", m_type)
1465
1466def gen_get_accessor(out, cls, m_name, m_type, ver_type_map):
1467 """
1468 For generating the get call for non- OF sub-objects
1469 """
1470 params = ",\n ".join(param_list(cls, m_name, "get"))
1471 out.write("""
1472/**
1473 * Get %(m_name)s from an object of type %(cls)s.
1474 * @param obj Pointer to an object of type %(cls)s.
1475 * @param %(m_name)s Pointer to the child object of type
1476 * %(m_type)s to be filled out.
1477 *
1478 */
1479""" % dict(m_name=m_name, cls=cls, m_type=m_type))
1480
1481 ret_type = accessor_return_type("get", m_type)
1482 out.write("%s\n%s_%s_get(\n %s)\n" % (ret_type, cls, m_name, params))
1483 gen_unified_acc_body(out, cls, m_name, ver_type_map, "get", m_type)
1484
Rich Lanece2e4642013-12-15 12:05:45 -08001485def gen_accessor_definitions(out, cls):
Rich Lanea06d0c32013-03-25 08:52:03 -07001486 for m_name in of_g.ordered_members[cls]:
1487 if m_name in of_g.skip_members:
1488 continue
1489 m_type = loxi_utils.member_base_type(cls, m_name)
1490 ver_type_map = field_ver_get(cls, m_name)
1491
1492 # Generate get/bind pending on member type
1493 # FIXME: Does this do the right thing for match?
1494 if loxi_utils.type_is_of_object(m_type):
1495 gen_of_obj_bind(out, cls, m_name, m_type, ver_type_map)
1496 gen_of_object_get(out, cls, m_name, m_type)
1497 else:
1498 gen_get_accessor(out, cls, m_name, m_type, ver_type_map)
1499
1500 # Now generate set accessor for all objects
1501 params = ",\n ".join(param_list(cls, m_name, "set"))
1502 out.write("""
1503/**
1504 * Set %(m_name)s in an object of type %(cls)s.
1505 * @param obj Pointer to an object of type %(cls)s.
1506""" % dict(m_name=m_name, cls=cls, m_type=m_type))
1507 if loxi_utils.type_is_scalar(m_type) or m_type == "of_octets_t":
1508 out.write("""\
1509 * @param %(m_name)s The value to write into the object
1510 */
1511""" % dict(m_name=m_name, cls=cls, m_type=m_type))
1512 else:
1513 out.write("""\
1514 * @param %(m_name)s Pointer to the child of type %(m_type)s.
1515 *
1516 * If the child's wire buffer is the same as the parent's, then
1517 * nothing is done as the changes have already been registered in the
1518 * parent. Otherwise, the data in the child's wire buffer is inserted
1519 * into the parent's and the appropriate lengths are updated.
1520 */
1521""" % dict(m_name=m_name, cls=cls, m_type=m_type))
1522 ret_type = accessor_return_type("set", m_type)
1523 out.write("%s\n%s_%s_set(\n %s)\n" % (ret_type, cls, m_name, params))
1524 gen_unified_acc_body(out, cls, m_name, ver_type_map, "set", m_type)
1525
Rich Lanea06d0c32013-03-25 08:52:03 -07001526################################################################
1527#
1528# New/Delete Function Definitions
1529#
1530################################################################
1531
Rich Lanea06d0c32013-03-25 08:52:03 -07001532################################################################
1533# Routines to generate the body of new/delete functions
1534################################################################
1535
1536def gen_init_fn_body(cls, out):
1537 """
1538 Generate function body for init function
1539 @param cls The class name for the function
1540 @param out The file to which to write
1541 """
Rich Lanea06d0c32013-03-25 08:52:03 -07001542
1543 out.write("""
1544/**
1545 * Initialize an object of type %(cls)s.
1546 *
1547 * @param obj Pointer to the object to initialize
1548 * @param version The wire version to use for the object
1549 * @param bytes How many bytes in the object
1550 * @param clean_wire Boolean: If true, clear the wire object control struct
1551 *
1552 * If bytes < 0, then the default fixed length is used for the object
1553 *
1554 * This is a "coerce" function that sets up the pointers for the
Andreas Wundsam53256162013-05-02 14:05:53 -07001555 * accessors properly.
Rich Lanea06d0c32013-03-25 08:52:03 -07001556 *
1557 * If anything other than 0 is passed in for the buffer size, the underlying
1558 * wire buffer will have 'grow' called.
1559 */
1560
1561void
Rich Lane7fdaa5c2014-10-27 18:14:59 -07001562%(cls)s_init(of_object_t *obj,
Rich Lanea06d0c32013-03-25 08:52:03 -07001563 of_version_t version, int bytes, int clean_wire)
1564{
Rich Lanee57f0432014-02-19 10:31:53 -08001565 LOCI_ASSERT(of_object_fixed_len[version][%(enum)s] >= 0);
Rich Lanea06d0c32013-03-25 08:52:03 -07001566 if (clean_wire) {
1567 MEMSET(obj, 0, sizeof(*obj));
1568 }
1569 if (bytes < 0) {
Rich Lanee3d3fb52014-10-16 12:45:17 -07001570 bytes = of_object_fixed_len[version][%(enum)s];
Rich Lanea06d0c32013-03-25 08:52:03 -07001571 }
1572 obj->version = version;
1573 obj->length = bytes;
1574 obj->object_id = %(enum)s;
Rich Lanea06d0c32013-03-25 08:52:03 -07001575
Rich Lanea06d0c32013-03-25 08:52:03 -07001576 /* Grow the wire buffer */
Rich Lanecdd542d2014-04-03 16:13:12 -07001577 if (obj->wbuf != NULL) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001578 int tot_bytes;
1579
Rich Lanecdd542d2014-04-03 16:13:12 -07001580 tot_bytes = bytes + obj->obj_offset;
1581 of_wire_buffer_grow(obj->wbuf, tot_bytes);
Rich Lanea06d0c32013-03-25 08:52:03 -07001582 }
1583}
Rich Lane7fdaa5c2014-10-27 18:14:59 -07001584""" % dict(cls=cls, enum=enum_name(cls)))
Rich Lanea06d0c32013-03-25 08:52:03 -07001585
1586def gen_new_fn_body(cls, out):
1587 """
1588 Generate function body for new function
1589 @param cls The class name for the function
1590 @param out The file to which to write
1591 """
1592
1593 out.write("""
1594/**
1595 * \\defgroup %(cls)s %(cls)s
1596 */
1597""" % dict(cls=cls))
1598
Rich Lane37911fd2014-04-01 22:11:22 -07001599 uclass = loxi_globals.unified.class_by_name(cls)
1600 is_fixed_length = uclass and uclass.is_fixed_length
1601 max_length = is_fixed_length and "bytes" or "OF_WIRE_BUFFER_MAX_LENGTH"
1602
Rich Lanea06d0c32013-03-25 08:52:03 -07001603 out.write("""
1604/**
1605 * Create a new %(cls)s object
1606 *
1607 * @param version The wire version to use for the object
1608 * @return Pointer to the newly create object or NULL on error
1609 *
1610 * Initializes the new object with it's default fixed length associating
1611 * a new underlying wire buffer.
1612 *
Rich Lanea06d0c32013-03-25 08:52:03 -07001613 * \\ingroup %(cls)s
1614 */
1615
Rich Lane7fdaa5c2014-10-27 18:14:59 -07001616of_object_t *
Rich Lanecd6ef152013-12-15 16:42:18 -08001617%(cls)s_new(of_version_t version)
Rich Lanea06d0c32013-03-25 08:52:03 -07001618{
Rich Lane7fdaa5c2014-10-27 18:14:59 -07001619 of_object_t *obj;
Rich Lanea06d0c32013-03-25 08:52:03 -07001620 int bytes;
1621
Rich Lanee3d3fb52014-10-16 12:45:17 -07001622 bytes = of_object_fixed_len[version][%(enum)s];
Rich Lanea06d0c32013-03-25 08:52:03 -07001623
Rich Lane7fdaa5c2014-10-27 18:14:59 -07001624 if ((obj = of_object_new(%(max_length)s)) == NULL) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001625 return NULL;
1626 }
1627
1628 %(cls)s_init(obj, version, bytes, 0);
Rich Lane37911fd2014-04-01 22:11:22 -07001629""" % dict(cls=cls, enum=enum_name(cls), max_length=max_length))
Rich Lanea06d0c32013-03-25 08:52:03 -07001630 if not type_maps.class_is_virtual(cls):
Rich Lane7e003102014-10-14 10:40:48 -07001631 from codegen import class_metadata_dict
1632 metadata = class_metadata_dict[cls]
1633
1634 if metadata.wire_type_set != 'NULL':
1635 out.write("""\
1636 %s(obj);
1637""" % metadata.wire_type_set)
1638
1639 if metadata.wire_length_set != 'NULL':
1640 out.write("""\
1641 %s(obj, obj->length);
1642""" % metadata.wire_length_set)
Rich Lanea06d0c32013-03-25 08:52:03 -07001643
1644 match_offset = v3_match_offset_get(cls)
1645 if match_offset >= 0:
1646 # Init length field for match object
1647 out.write("""
1648 /* Initialize match TLV for 1.2 */
Rich Lanea926e312014-10-14 11:45:39 -07001649 if ((version >= OF_VERSION_1_2)) {
Rich Lanea06d0c32013-03-25 08:52:03 -07001650 of_object_u16_set((of_object_t *)obj, %(match_offset)d + 2, 4);
1651 }
1652""" % dict(match_offset=match_offset))
1653 out.write("""
1654 return obj;
1655}
Rich Lanea06d0c32013-03-25 08:52:03 -07001656""" % dict(cls=cls))
1657
1658
Rich Lanea06d0c32013-03-25 08:52:03 -07001659################################################################
1660# Now the top level generator functions
1661################################################################
1662
1663def gen_new_function_declarations(out):
1664 """
1665 Gerenate the header file declarations for new operators for all classes
1666 @param out The file to which to write the decs
1667 """
1668
1669 out.write("""
1670/****************************************************************
1671 *
1672 * New operator declarations
1673 *
1674 * _new: Create a new object for writing; includes init
Rich Lanea06d0c32013-03-25 08:52:03 -07001675 * _init: Initialize and optionally allocate buffer space for an
1676 * automatic instance
1677 *
Rich Lanebb8f17c2014-06-12 13:14:09 -07001678 * _new and requires a delete operation to be called on the object.
Rich Lanea06d0c32013-03-25 08:52:03 -07001679 *
1680 ****************************************************************/
1681""")
Rich Lanea06d0c32013-03-25 08:52:03 -07001682
1683 for cls in of_g.standard_class_order:
1684 out.write("""
Rich Lane7fdaa5c2014-10-27 18:14:59 -07001685extern of_object_t *
Rich Lanecd6ef152013-12-15 16:42:18 -08001686 %(cls)s_new(of_version_t version);
Rich Lanea06d0c32013-03-25 08:52:03 -07001687""" % dict(cls=cls))
Rich Lanea06d0c32013-03-25 08:52:03 -07001688 out.write("""extern void %(cls)s_init(
Rich Lane7fdaa5c2014-10-27 18:14:59 -07001689 of_object_t *obj, of_version_t version, int bytes, int clean_wire);
Rich Lanea06d0c32013-03-25 08:52:03 -07001690""" % dict(cls=cls))
1691
1692 out.write("""
1693/****************************************************************
1694 *
1695 * Delete operator static inline definitions.
1696 * These are here for type checking purposes only
1697 *
1698 ****************************************************************/
1699""")
1700 for cls in of_g.standard_class_order:
Rich Lanea06d0c32013-03-25 08:52:03 -07001701 out.write("""
1702/**
1703 * Delete an object of type %(cls)s_t
1704 * @param obj An instance of type %(cls)s_t
1705 *
1706 * \ingroup %(cls)s
1707 */
1708static inline void
Rich Lane7fdaa5c2014-10-27 18:14:59 -07001709%(cls)s_delete(of_object_t *obj) {
1710 of_object_delete(obj);
Rich Lanea06d0c32013-03-25 08:52:03 -07001711}
1712""" % dict(cls=cls))
1713
1714 out.write("""
1715typedef void (*of_object_init_f)(of_object_t *obj, of_version_t version,
1716 int bytes, int clean_wire);
Rich Laneb157b0f2013-03-27 13:55:28 -07001717extern const of_object_init_f of_object_init_map[];
Rich Lanea06d0c32013-03-25 08:52:03 -07001718""")
1719
1720 out.write("""
1721/****************************************************************
1722 *
1723 * Function pointer initialization functions
1724 * These are part of the "coerce" type casting for objects
1725 *
1726 ****************************************************************/
1727""")
1728
Rich Laneb604e332013-12-15 13:23:51 -08001729def gen_new_function_definitions(out, cls):
Rich Lanea06d0c32013-03-25 08:52:03 -07001730 """
1731 Generate the new operator for all classes
1732
1733 @param out The file to which to write the functions
1734 """
1735
Rich Laneb604e332013-12-15 13:23:51 -08001736 gen_new_fn_body(cls, out)
1737 gen_init_fn_body(cls, out)
Rich Lanea06d0c32013-03-25 08:52:03 -07001738
Rich Lanea06d0c32013-03-25 08:52:03 -07001739"""
1740Document generation functions
1741
1742The main reason this is here is to generate documentation per
1743accessor that indicates the versions that support the interface.
1744"""
1745
1746
1747def gen_accessor_doc(out, name):
1748 """
1749 Generate documentation for each accessor function that indicates
1750 the versions supporting the accessor.
1751 """
1752
1753 common_top_matter(out, name)
1754
1755 out.write("/* DOCUMENTATION ONLY */\n")
1756
1757 for cls in of_g.standard_class_order:
Rich Lane6171e632014-11-07 10:23:38 -08001758 if type_maps.class_is_virtual(cls):
1759 pass
Rich Lanea06d0c32013-03-25 08:52:03 -07001760
1761 out.write("""
1762/**
Andreas Wundsam53256162013-05-02 14:05:53 -07001763 * Structure for %(cls)s object. Get/set
Rich Lanea06d0c32013-03-25 08:52:03 -07001764 * accessors available in all versions unless noted otherwise
1765 *
1766""" % dict(cls=cls))
1767 if loxi_utils.class_is_list(cls):
1768 out.write("""\
1769 * @param first Function of type %(cls)s_first_f.
1770 * Setup a TBD class object to the first entry in the list
1771 * @param next Function of type %(cls)s_next_f.
1772 * Advance a TBD class object to the next entry in the list
1773 * @param append_bind Function of type %(cls)s_append_bind_f
1774 * Setup a TBD class object for append to the end of the current list
1775 * @param append Function of type @ref %(cls)s_append_f.
1776 * Copy an item to the end of a list
1777""" % dict(cls=cls))
1778
1779 for m_name in of_g.ordered_members[cls]:
1780 if m_name in of_g.skip_members:
1781 continue
1782 ver_type_map = field_ver_get(cls, m_name)
1783 (m_type, get_rv) = get_acc_rv(cls, m_name)
1784 if len(ver_type_map) == 3:
1785 # ver_string = "Available in all versions"
1786 ver_string = ""
1787 else:
1788 ver_string = "("
1789 for ver in sorted(ver_type_map):
1790 ver_string += " " + of_g.short_version_names[ver]
1791 ver_string += ")."
1792
1793 f_name = acc_name(cls, m_name)
1794 out.write("""\
1795 * @param %(m_name)s_get/set %(ver_string)s
1796 * Accessors for %(m_name)s, a variable of type %(m_type)s. Functions
1797 * are of type %(f_name)s_get_f and _set_f.
1798 *
1799""" % dict(f_name=f_name, m_name=m_name, ver_string=ver_string, m_type=m_type))
1800
1801 out.write("""\
1802 */
1803typedef struct %(cls)s_s %(cls)s_t;
1804""" % dict(cls=cls))
1805
1806 out.write("#endif /* _LOCI_DOC_H_ */\n")