blob: 9d844778a6feb4fb7d349b4f214327ac468f7f42 [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 Show function generation
30
31Generates show 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
Rich Lanea06d0c32013-03-25 08:52:03 -070041import loxi_utils.loxi_utils as loxi_utils
Andreas Wundsam542a13c2013-11-15 13:28:55 -080042import c_gen.loxi_utils_legacy as loxi_utils
43import c_gen.identifiers as identifiers
Rich Lanea06d0c32013-03-25 08:52:03 -070044from c_test_gen import var_name_map
45
46def gen_obj_show_h(out, name):
47 loxi_utils.gen_c_copy_license(out)
48 out.write("""
49/**
50 *
51 * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen.
52 *
Andreas Wundsam53256162013-05-02 14:05:53 -070053 * Header file for object showing.
Rich Lanea06d0c32013-03-25 08:52:03 -070054 */
55
56/**
57 * Show object declarations
58 *
59 * Routines that emit a human-readable dump of each object.
60 *
61 */
62
63#if !defined(_LOCI_OBJ_SHOW_H_)
64#define _LOCI_OBJ_SHOW_H_
65
66#include <loci/loci.h>
67#include <stdio.h>
68
69/* g++ requires this to pick up PRI, etc.
70 * See http://gcc.gnu.org/ml/gcc-help/2006-10/msg00223.html
71 */
72#if !defined(__STDC_FORMAT_MACROS)
73#define __STDC_FORMAT_MACROS
74#endif
75#include <inttypes.h>
76
77
78/**
Andreas Wundsam53256162013-05-02 14:05:53 -070079 * Show any OF object.
Rich Lanea06d0c32013-03-25 08:52:03 -070080 */
Andreas Wundsam53256162013-05-02 14:05:53 -070081int of_object_show(loci_writer_f writer, void* cookie, of_object_t* obj);
Rich Lanea06d0c32013-03-25 08:52:03 -070082
83
84
85
86
87
88""")
89
Rich Lanea06d0c32013-03-25 08:52:03 -070090 for version in of_g.of_version_range:
91 for cls in of_g.standard_class_order:
92 if not loxi_utils.class_in_version(cls, version):
93 continue
94 if cls in type_maps.inheritance_map:
95 continue
96 out.write("""\
97int %(cls)s_%(ver_name)s_show(loci_writer_f writer, void* cookie, %(cls)s_t *obj);
98""" % dict(cls=cls, ver_name=loxi_utils.version_to_name(version)))
99
100 out.write("""
101#endif /* _LOCI_OBJ_SHOW_H_ */
102""")
103
104def gen_obj_show_c(out, name):
105 loxi_utils.gen_c_copy_license(out)
106 out.write("""
107/**
108 *
109 * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen.
110 *
Andreas Wundsam53256162013-05-02 14:05:53 -0700111 * Source file for object showing.
112 *
Rich Lanea06d0c32013-03-25 08:52:03 -0700113 */
114
115#define DISABLE_WARN_UNUSED_RESULT
116#include <loci/loci.h>
117#include <loci/loci_show.h>
118#include <loci/loci_obj_show.h>
119
120static int
121unknown_show(loci_writer_f writer, void* cookie, of_object_t *obj)
122{
Andreas Wundsam53256162013-05-02 14:05:53 -0700123 return writer(cookie, "Unable to print object of type %d, version %d\\n",
Rich Lanea06d0c32013-03-25 08:52:03 -0700124 obj->object_id, obj->version);
Andreas Wundsam53256162013-05-02 14:05:53 -0700125}
Rich Lanea06d0c32013-03-25 08:52:03 -0700126""")
127
128 for version in of_g.of_version_range:
129 ver_name = loxi_utils.version_to_name(version)
130 for cls in of_g.standard_class_order:
131 if not loxi_utils.class_in_version(cls, version):
132 continue
133 if cls in type_maps.inheritance_map:
134 continue
135 out.write("""
136int
137%(cls)s_%(ver_name)s_show(loci_writer_f writer, void* cookie, %(cls)s_t *obj)
138{
139 int out = 0;
140""" % dict(cls=cls, ver_name=ver_name))
141
142 members, member_types = loxi_utils.all_member_types_get(cls, version)
143 for m_type in member_types:
144 if loxi_utils.type_is_scalar(m_type) or m_type in \
145 ["of_match_t", "of_octets_t"]:
146 # Declare instance of these
147 out.write(" %s %s;\n" % (m_type, var_name_map(m_type)))
148 else:
149 out.write("""
150 %(m_type)s %(v_name)s;
151""" % dict(m_type=m_type, v_name=var_name_map(m_type)))
152 if loxi_utils.class_is_list(m_type):
153 base_type = loxi_utils.list_to_entry_type(m_type)
154 out.write(" %s elt;\n int rv;\n" % base_type)
155 for member in members:
156 m_type = member["m_type"]
157 m_name = member["name"]
158 #emitter = "LOCI_SHOW_" + loxi_utils.type_to_short_name(m_type)
Andreas Wundsam53256162013-05-02 14:05:53 -0700159 emitter = "LOCI_SHOW_" + loxi_utils.type_to_short_name(m_type) + "_" + m_name;
Rich Lanea06d0c32013-03-25 08:52:03 -0700160 if loxi_utils.skip_member_name(m_name):
161 continue
162 if (loxi_utils.type_is_scalar(m_type) or
163 m_type in ["of_match_t", "of_octets_t"]):
164 out.write("""
165 %(cls)s_%(m_name)s_get(obj, &%(v_name)s);
166 out += writer(cookie, "%(m_name)s=");
167 out += %(emitter)s(writer, cookie, %(v_name)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700168 out += writer(cookie, " ");
Rich Lanea06d0c32013-03-25 08:52:03 -0700169""" % dict(cls=cls, m_name=m_name, m_type=m_type,
170 v_name=var_name_map(m_type), emitter=emitter))
171 elif loxi_utils.class_is_list(m_type):
172 sub_cls = m_type[:-2] # Trim _t
173 elt_type = loxi_utils.list_to_entry_type(m_type)
174 out.write("""
175 out += writer(cookie, "%(elt_type)s={ ");
176 %(cls)s_%(m_name)s_bind(obj, &%(v_name)s);
177 %(u_type)s_ITER(&%(v_name)s, &elt, rv) {
178 of_object_show(writer, cookie, (of_object_t *)&elt);
179 }
Andreas Wundsam53256162013-05-02 14:05:53 -0700180 out += writer(cookie, "} ");
Rich Lanea06d0c32013-03-25 08:52:03 -0700181""" % 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_show(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_show_match(loci_writer_f writer, void* cookie, of_match_t *match)
201{
202 int out = 0;
203""")
204
205 for key, entry in match.of_match_members.items():
206 m_type = entry["m_type"]
207 #emitter = "LOCI_SHOW_" + loxi_utils.type_to_short_name(m_type)
Andreas Wundsam53256162013-05-02 14:05:53 -0700208 emitter = "LOCI_SHOW_" + loxi_utils.type_to_short_name(m_type) + "_" + key;
Rich Lanea06d0c32013-03-25 08:52:03 -0700209 out.write("""
210 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700211 out += writer(cookie, "%(key)s active=");
Rich Lanea06d0c32013-03-25 08:52:03 -0700212 out += %(emitter)s(writer, cookie, match->fields.%(key)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700213 out += writer(cookie, "/");
Rich Lanea06d0c32013-03-25 08:52:03 -0700214 out += %(emitter)s(writer, cookie, match->masks.%(key)s);
215 out += writer(cookie, " ");
216 }
217""" % dict(key=key, ku=key.upper(), emitter=emitter, m_type=m_type))
218
219 out.write("""
220 return out;
221}
222""")
223
224 # Generate big table indexed by version and object
225 for version in of_g.of_version_range:
226 out.write("""
Rich Laneb157b0f2013-03-27 13:55:28 -0700227static const loci_obj_show_f show_funs_v%(version)s[OF_OBJECT_COUNT] = {
Rich Lanea06d0c32013-03-25 08:52:03 -0700228""" % dict(version=version))
229 out.write(" unknown_show, /* of_object, not a valid specific type */\n")
230 for j, cls in enumerate(of_g.all_class_order):
231 comma = ""
232 if j < len(of_g.all_class_order) - 1: # Avoid ultimate comma
233 comma = ","
234
Andreas Wundsam53256162013-05-02 14:05:53 -0700235 if (not loxi_utils.class_in_version(cls, version) or
Rich Lanea06d0c32013-03-25 08:52:03 -0700236 cls in type_maps.inheritance_map):
237 out.write(" unknown_show%s\n" % comma);
238 else:
Andreas Wundsam53256162013-05-02 14:05:53 -0700239 out.write(" %s_%s_show%s\n" %
Rich Lanea06d0c32013-03-25 08:52:03 -0700240 (cls, loxi_utils.version_to_name(version), comma))
241 out.write("};\n\n")
242
243 out.write("""
Rich Laneb157b0f2013-03-27 13:55:28 -0700244static const loci_obj_show_f *const show_funs[5] = {
Rich Lanea06d0c32013-03-25 08:52:03 -0700245 NULL,
246 show_funs_v1,
247 show_funs_v2,
248 show_funs_v3,
249 show_funs_v4
250};
251
252int
253of_object_show(loci_writer_f writer, void* cookie, of_object_t *obj)
254{
255 if ((obj->object_id > 0) && (obj->object_id < OF_OBJECT_COUNT)) {
256 if (((obj)->version > 0) && ((obj)->version <= OF_VERSION_1_2)) {
257 /* @fixme VERSION */
258 return show_funs[obj->version][obj->object_id](writer, cookie, (of_object_t *)obj);
259 } else {
260 return writer(cookie, "Bad version %d\\n", obj->version);
261 }
262 }
263 return writer(cookie, "Bad object id %d\\n", obj->object_id);
264}
265""")
266