blob: d4a016c38bd5c1d4ff13668f122d4ae450127c5e [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@brief Dump function generation
30
31Generates dump function files.
32
33"""
34
35import sys
Andreas Wundsam76db0062013-11-15 13:34:41 -080036import c_gen.of_g_legacy as of_g
Andreas Wundsam542a13c2013-11-15 13:28:55 -080037import c_gen.match as match
38import c_gen.flags as flags
Rich Lanea06d0c32013-03-25 08:52:03 -070039from generic_utils import *
Andreas Wundsam542a13c2013-11-15 13:28:55 -080040import c_gen.type_maps as type_maps
41import c_gen.loxi_utils_legacy as loxi_utils
42import c_gen.identifiers as identifiers
Rich Lanea06d0c32013-03-25 08:52:03 -070043from c_test_gen import var_name_map
44
45def gen_obj_dump_h(out, name):
46 loxi_utils.gen_c_copy_license(out)
47 out.write("""
48/**
49 *
50 * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen.
51 *
Andreas Wundsam53256162013-05-02 14:05:53 -070052 * Header file for object dumping.
Rich Lanea06d0c32013-03-25 08:52:03 -070053 */
54
55/**
56 * Dump object declarations
57 *
58 * Routines that emit a dump of each object.
59 *
60 */
61
62#if !defined(_LOCI_OBJ_DUMP_H_)
63#define _LOCI_OBJ_DUMP_H_
64
65#include <loci/loci.h>
66#include <stdio.h>
67
68/* g++ requires this to pick up PRI, etc.
69 * See http://gcc.gnu.org/ml/gcc-help/2006-10/msg00223.html
70 */
71#if !defined(__STDC_FORMAT_MACROS)
72#define __STDC_FORMAT_MACROS
73#endif
74#include <inttypes.h>
75
76
77/**
Andreas Wundsam53256162013-05-02 14:05:53 -070078 * Dump any OF object.
Rich Lanea06d0c32013-03-25 08:52:03 -070079 */
Andreas Wundsam53256162013-05-02 14:05:53 -070080int of_object_dump(loci_writer_f writer, void* cookie, of_object_t* obj);
Rich Lanea06d0c32013-03-25 08:52:03 -070081
82
83
84
85
86
87""")
88
Rich Lanea06d0c32013-03-25 08:52:03 -070089 for version in of_g.of_version_range:
90 for cls in of_g.standard_class_order:
91 if not loxi_utils.class_in_version(cls, version):
92 continue
93 if cls in type_maps.inheritance_map:
94 continue
95 out.write("""\
96int %(cls)s_%(ver_name)s_dump(loci_writer_f writer, void* cookie, %(cls)s_t *obj);
97""" % dict(cls=cls, ver_name=loxi_utils.version_to_name(version)))
98
99 out.write("""
100#endif /* _LOCI_OBJ_DUMP_H_ */
101""")
102
103def gen_obj_dump_c(out, name):
104 loxi_utils.gen_c_copy_license(out)
105 out.write("""
106/**
107 *
108 * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen.
109 *
Andreas Wundsam53256162013-05-02 14:05:53 -0700110 * Source file for object dumping.
111 *
Rich Lanea06d0c32013-03-25 08:52:03 -0700112 */
113
114#define DISABLE_WARN_UNUSED_RESULT
115#include <loci/loci.h>
116#include <loci/loci_dump.h>
117#include <loci/loci_obj_dump.h>
118
119static int
120unknown_dump(loci_writer_f writer, void* cookie, of_object_t *obj)
121{
Andreas Wundsam53256162013-05-02 14:05:53 -0700122 return writer(cookie, "Unable to print object of type %d, version %d\\n",
Rich Lanea06d0c32013-03-25 08:52:03 -0700123 obj->object_id, obj->version);
Andreas Wundsam53256162013-05-02 14:05:53 -0700124}
Rich Lanea06d0c32013-03-25 08:52:03 -0700125""")
126
127 for version in of_g.of_version_range:
128 ver_name = loxi_utils.version_to_name(version)
129 for cls in of_g.standard_class_order:
130 if not loxi_utils.class_in_version(cls, version):
131 continue
132 if cls in type_maps.inheritance_map:
133 continue
134 out.write("""
135int
136%(cls)s_%(ver_name)s_dump(loci_writer_f writer, void* cookie, %(cls)s_t *obj)
137{
138 int out = 0;
139""" % dict(cls=cls, ver_name=ver_name))
140
141 members, member_types = loxi_utils.all_member_types_get(cls, version)
142 for m_type in member_types:
143 if loxi_utils.type_is_scalar(m_type) or m_type in \
144 ["of_match_t", "of_octets_t"]:
145 # Declare instance of these
146 out.write(" %s %s;\n" % (m_type, var_name_map(m_type)))
147 else:
148 out.write("""
149 %(m_type)s %(v_name)s;
150""" % dict(m_type=m_type, v_name=var_name_map(m_type)))
151 if loxi_utils.class_is_list(m_type):
152 base_type = loxi_utils.list_to_entry_type(m_type)
153 out.write(" %s elt;\n int rv;\n" % base_type)
154 out.write("""
155 out += writer(cookie, "Object of type %(cls)s\\n");
156""" % dict(cls=cls))
157 for member in members:
158 m_type = member["m_type"]
159 m_name = member["name"]
160 emitter = "LOCI_DUMP_" + loxi_utils.type_to_short_name(m_type)
161 if loxi_utils.skip_member_name(m_name):
162 continue
163 if (loxi_utils.type_is_scalar(m_type) or
164 m_type in ["of_match_t", "of_octets_t"]):
165 out.write("""
166 %(cls)s_%(m_name)s_get(obj, &%(v_name)s);
167 out += writer(cookie, " %(m_name)s (%(m_type)s): ");
168 out += %(emitter)s(writer, cookie, %(v_name)s);
169 out += writer(cookie, "\\n");
170""" % dict(cls=cls, m_name=m_name, m_type=m_type,
171 v_name=var_name_map(m_type), emitter=emitter))
172 elif loxi_utils.class_is_list(m_type):
173 sub_cls = m_type[:-2] # Trim _t
174 elt_type = loxi_utils.list_to_entry_type(m_type)
175 out.write("""
176 out += writer(cookie, "List of %(elt_type)s\\n");
177 %(cls)s_%(m_name)s_bind(obj, &%(v_name)s);
178 %(u_type)s_ITER(&%(v_name)s, &elt, rv) {
179 of_object_dump(writer, cookie, (of_object_t *)&elt);
180 }
181""" % dict(sub_cls=sub_cls, u_type=sub_cls.upper(), v_name=var_name_map(m_type),
182 elt_type=elt_type, cls=cls, m_name=m_name, m_type=m_type))
183 else:
184 sub_cls = m_type[:-2] # Trim _t
185 out.write("""
186 %(cls)s_%(m_name)s_bind(obj, &%(v_name)s);
187 out += %(sub_cls)s_%(ver_name)s_dump(writer, cookie, &%(v_name)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700188""" % dict(cls=cls, sub_cls=sub_cls, m_name=m_name,
Rich Lanea06d0c32013-03-25 08:52:03 -0700189 v_name=var_name_map(m_type), ver_name=ver_name))
190
191 out.write("""
192 return out;
193}
194""")
195 out.write("""
196/**
197 * Log a match entry
198 */
199int
200loci_dump_match(loci_writer_f writer, void* cookie, of_match_t *match)
201{
202 int out = 0;
203
204 out += writer(cookie, "Match obj, version %d.\\n", match->version);
205""")
206
207 for key, entry in match.of_match_members.items():
208 m_type = entry["m_type"]
209 emitter = "LOCI_DUMP_" + loxi_utils.type_to_short_name(m_type)
210 out.write("""
211 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
212 out += writer(cookie, " %(key)s (%(m_type)s) active: Value ");
213 out += %(emitter)s(writer, cookie, match->fields.%(key)s);
214 out += writer(cookie, "\\n Mask ");
215 out += %(emitter)s(writer, cookie, match->masks.%(key)s);
216 out += writer(cookie, "\\n");
217 }
218""" % dict(key=key, ku=key.upper(), emitter=emitter, m_type=m_type))
219
220 out.write("""
221 return out;
222}
223""")
224
225 # Generate big table indexed by version and object
226 for version in of_g.of_version_range:
227 out.write("""
Rich Laneb157b0f2013-03-27 13:55:28 -0700228static const loci_obj_dump_f dump_funs_v%(version)s[OF_OBJECT_COUNT] = {
Rich Lanea06d0c32013-03-25 08:52:03 -0700229""" % dict(version=version))
230 out.write(" unknown_dump, /* of_object, not a valid specific type */\n")
231 for j, cls in enumerate(of_g.all_class_order):
232 comma = ""
233 if j < len(of_g.all_class_order) - 1: # Avoid ultimate comma
234 comma = ","
235
Andreas Wundsam53256162013-05-02 14:05:53 -0700236 if (not loxi_utils.class_in_version(cls, version) or
Rich Lanea06d0c32013-03-25 08:52:03 -0700237 cls in type_maps.inheritance_map):
238 out.write(" unknown_dump%s\n" % comma);
239 else:
Andreas Wundsam53256162013-05-02 14:05:53 -0700240 out.write(" %s_%s_dump%s\n" %
Rich Lanea06d0c32013-03-25 08:52:03 -0700241 (cls, loxi_utils.version_to_name(version), comma))
242 out.write("};\n\n")
243
244 out.write("""
Rich Laneb157b0f2013-03-27 13:55:28 -0700245static const loci_obj_dump_f *const dump_funs[5] = {
Rich Lanea06d0c32013-03-25 08:52:03 -0700246 NULL,
247 dump_funs_v1,
248 dump_funs_v2,
249 dump_funs_v3,
250 dump_funs_v4
251};
252
253int
254of_object_dump(loci_writer_f writer, void* cookie, of_object_t *obj)
255{
256 if ((obj->object_id > 0) && (obj->object_id < OF_OBJECT_COUNT)) {
Rich Lane6c89d2e2013-07-12 16:31:01 -0700257 if (((obj)->version > 0) && ((obj)->version <= OF_VERSION_1_3)) {
Rich Lanea06d0c32013-03-25 08:52:03 -0700258 /* @fixme VERSION */
259 return dump_funs[obj->version][obj->object_id](writer, cookie, (of_object_t *)obj);
260 } else {
261 return writer(cookie, "Bad version %d\\n", obj->version);
262 }
263 }
264 return writer(cookie, "Bad object id %d\\n", obj->object_id);
265}
266""")
267