blob: a6ce048c792efadb4d50db83b026dedc26360aa5 [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
Rich Laneb8330b22014-10-13 21:13:19 -070046show_override = {
47 ('uint32_t', 'arp_tpa'): 'ipv4',
48 ('uint32_t', 'arp_spa'): 'ipv4',
49 ('uint32_t', 'nw_addr'): 'ipv4',
50 ('uint32_t', 'dst'): 'ipv4',
51}
52
53show_hex = set([
54 ('uint8_t', 'icmpv6_code'),
55 ('uint8_t', 'mpls_tc'),
56 ('uint16_t', 'eth_type'),
57 ('uint8_t', 'ip_dscp'),
58 ('uint64_t', 'metadata'),
59 ('uint16_t', 'ingress_tpid'),
60 ('uint16_t', 'egress_tpid'),
61 ('uint32_t', 'xid'),
62 ('uint16_t', 'flags'),
63 ('uint32_t', 'experimenter'),
64 ('uint32_t', 'mask'),
65 ('uint8_t', 'report_mirror_ports'),
66 ('uint64_t', 'datapath_id'),
67 ('uint32_t', 'capabilities'),
68 ('uint32_t', 'actions'),
69 ('uint64_t', 'cookie'),
70 ('uint8_t', 'reason'),
71 ('uint32_t', 'role'),
72 ('uint32_t', 'config'),
73 ('uint32_t', 'advertise'),
74 ('uint32_t', 'advertised'),
75 ('uint32_t', 'supported'),
76 ('uint32_t', 'peer'),
77 ('uint64_t', 'cookie_mask'),
78 ('uint32_t', 'reserved'),
79 ('uint16_t', 'ethertype'),
80 ('uint64_t', 'metadata_mask'),
81 ('uint32_t', 'instructions'),
82 ('uint32_t', 'write_actions'),
83 ('uint32_t', 'apply_actions'),
84 ('uint32_t', 'types'),
85 ('uint32_t', 'actions_all'),
86 ('uint32_t', 'actions_select'),
87 ('uint32_t', 'actions_indirect'),
88 ('uint32_t', 'actions_ff'),
89 ('uint64_t', 'generation_id'),
90 ('uint16_t', 'value_mask'),
91 ('uint32_t', 'value_mask'),
92 ('uint32_t', 'oxm_header'),
93 ('uint8_t', 'value_mask'),
94 ('uint64_t', 'value_mask'),
95 ('uint64_t', 'write_setfields'),
96 ('uint64_t', 'apply_setfields'),
97 ('uint64_t', 'metadata_match'),
98 ('uint64_t', 'metadata_write'),
99 ('uint32_t', 'packet_in_mask_equal_master'),
100 ('uint32_t', 'packet_in_mask_slave'),
101 ('uint32_t', 'port_status_mask_equal_master'),
102 ('uint32_t', 'port_status_mask_slave'),
103 ('uint32_t', 'flow_removed_mask_equal_master'),
104 ('uint32_t', 'flow_removed_mask_slave'),
105 ('uint32_t', 'band_types'),
106 ('uint16_t', 'bsn_tcp_flags'),
Wilson Nga3483d62014-11-05 13:50:37 -0800107 ('uint8_t', 'bsn_l2_cache_hit'),
Rich Laneb8330b22014-10-13 21:13:19 -0700108])
109
110def gen_emitter(cls, m_name, m_type):
111 if (m_type, m_name) in show_override:
112 short_type = show_override[(m_type, m_name)]
113 elif (m_type, m_name) in show_hex:
114 short_type = loxi_utils.type_to_short_name(m_type).replace('u', 'x')
115 else:
116 short_type = loxi_utils.type_to_short_name(m_type)
117 return "LOCI_SHOW_" + short_type;
118
Rich Lanea06d0c32013-03-25 08:52:03 -0700119def gen_obj_show_h(out, name):
120 loxi_utils.gen_c_copy_license(out)
121 out.write("""
122/**
123 *
124 * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen.
125 *
Andreas Wundsam53256162013-05-02 14:05:53 -0700126 * Header file for object showing.
Rich Lanea06d0c32013-03-25 08:52:03 -0700127 */
128
129/**
130 * Show object declarations
131 *
132 * Routines that emit a human-readable dump of each object.
133 *
134 */
135
136#if !defined(_LOCI_OBJ_SHOW_H_)
137#define _LOCI_OBJ_SHOW_H_
138
139#include <loci/loci.h>
140#include <stdio.h>
141
142/* g++ requires this to pick up PRI, etc.
143 * See http://gcc.gnu.org/ml/gcc-help/2006-10/msg00223.html
144 */
145#if !defined(__STDC_FORMAT_MACROS)
146#define __STDC_FORMAT_MACROS
147#endif
148#include <inttypes.h>
149
150
151/**
Andreas Wundsam53256162013-05-02 14:05:53 -0700152 * Show any OF object.
Rich Lanea06d0c32013-03-25 08:52:03 -0700153 */
Andreas Wundsam53256162013-05-02 14:05:53 -0700154int of_object_show(loci_writer_f writer, void* cookie, of_object_t* obj);
Rich Lanea06d0c32013-03-25 08:52:03 -0700155
156
157
158
159
160
161""")
162
Rich Lanea06d0c32013-03-25 08:52:03 -0700163 for version in of_g.of_version_range:
164 for cls in of_g.standard_class_order:
165 if not loxi_utils.class_in_version(cls, version):
166 continue
Rich Lane8841f352014-10-12 19:18:36 -0700167 if type_maps.class_is_inheritance_root(cls):
Rich Lanea06d0c32013-03-25 08:52:03 -0700168 continue
169 out.write("""\
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700170int %(cls)s_%(ver_name)s_show(loci_writer_f writer, void* cookie, of_object_t *obj);
Rich Lanea06d0c32013-03-25 08:52:03 -0700171""" % dict(cls=cls, ver_name=loxi_utils.version_to_name(version)))
172
173 out.write("""
174#endif /* _LOCI_OBJ_SHOW_H_ */
175""")
176
177def gen_obj_show_c(out, name):
178 loxi_utils.gen_c_copy_license(out)
179 out.write("""
180/**
181 *
182 * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen.
183 *
Andreas Wundsam53256162013-05-02 14:05:53 -0700184 * Source file for object showing.
185 *
Rich Lanea06d0c32013-03-25 08:52:03 -0700186 */
187
188#define DISABLE_WARN_UNUSED_RESULT
189#include <loci/loci.h>
190#include <loci/loci_show.h>
191#include <loci/loci_obj_show.h>
192
193static int
194unknown_show(loci_writer_f writer, void* cookie, of_object_t *obj)
195{
Andreas Wundsam53256162013-05-02 14:05:53 -0700196 return writer(cookie, "Unable to print object of type %d, version %d\\n",
Rich Lanea06d0c32013-03-25 08:52:03 -0700197 obj->object_id, obj->version);
Andreas Wundsam53256162013-05-02 14:05:53 -0700198}
Rich Lanea06d0c32013-03-25 08:52:03 -0700199""")
200
201 for version in of_g.of_version_range:
202 ver_name = loxi_utils.version_to_name(version)
203 for cls in of_g.standard_class_order:
204 if not loxi_utils.class_in_version(cls, version):
205 continue
Rich Lane8841f352014-10-12 19:18:36 -0700206 if type_maps.class_is_inheritance_root(cls):
Rich Lanea06d0c32013-03-25 08:52:03 -0700207 continue
208 out.write("""
209int
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700210%(cls)s_%(ver_name)s_show(loci_writer_f writer, void* cookie, of_object_t *obj)
Rich Lanea06d0c32013-03-25 08:52:03 -0700211{
212 int out = 0;
213""" % dict(cls=cls, ver_name=ver_name))
214
215 members, member_types = loxi_utils.all_member_types_get(cls, version)
216 for m_type in member_types:
217 if loxi_utils.type_is_scalar(m_type) or m_type in \
218 ["of_match_t", "of_octets_t"]:
219 # Declare instance of these
220 out.write(" %s %s;\n" % (m_type, var_name_map(m_type)))
221 else:
222 out.write("""
223 %(m_type)s %(v_name)s;
224""" % dict(m_type=m_type, v_name=var_name_map(m_type)))
225 if loxi_utils.class_is_list(m_type):
Rich Lane7fdaa5c2014-10-27 18:14:59 -0700226 out.write(" of_object_t elt;\n int rv;\n")
Rich Lanea06d0c32013-03-25 08:52:03 -0700227 for member in members:
228 m_type = member["m_type"]
229 m_name = member["name"]
Rich Laneb8330b22014-10-13 21:13:19 -0700230 emitter = gen_emitter(cls, m_name, m_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700231 if loxi_utils.skip_member_name(m_name):
232 continue
233 if (loxi_utils.type_is_scalar(m_type) or
234 m_type in ["of_match_t", "of_octets_t"]):
235 out.write("""
236 %(cls)s_%(m_name)s_get(obj, &%(v_name)s);
237 out += writer(cookie, "%(m_name)s=");
238 out += %(emitter)s(writer, cookie, %(v_name)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700239 out += writer(cookie, " ");
Rich Lanea06d0c32013-03-25 08:52:03 -0700240""" % dict(cls=cls, m_name=m_name, m_type=m_type,
241 v_name=var_name_map(m_type), emitter=emitter))
242 elif loxi_utils.class_is_list(m_type):
243 sub_cls = m_type[:-2] # Trim _t
244 elt_type = loxi_utils.list_to_entry_type(m_type)
245 out.write("""
246 out += writer(cookie, "%(elt_type)s={ ");
247 %(cls)s_%(m_name)s_bind(obj, &%(v_name)s);
248 %(u_type)s_ITER(&%(v_name)s, &elt, rv) {
249 of_object_show(writer, cookie, (of_object_t *)&elt);
250 }
Andreas Wundsam53256162013-05-02 14:05:53 -0700251 out += writer(cookie, "} ");
Rich Lanea06d0c32013-03-25 08:52:03 -0700252""" % dict(sub_cls=sub_cls, u_type=sub_cls.upper(), v_name=var_name_map(m_type),
253 elt_type=elt_type, cls=cls, m_name=m_name, m_type=m_type))
254 else:
255 sub_cls = m_type[:-2] # Trim _t
256 out.write("""
257 %(cls)s_%(m_name)s_bind(obj, &%(v_name)s);
258 out += %(sub_cls)s_%(ver_name)s_show(writer, cookie, &%(v_name)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700259""" % dict(cls=cls, sub_cls=sub_cls, m_name=m_name,
Rich Lanea06d0c32013-03-25 08:52:03 -0700260 v_name=var_name_map(m_type), ver_name=ver_name))
261
262 out.write("""
263 return out;
264}
265""")
266 out.write("""
267/**
268 * Log a match entry
269 */
270int
271loci_show_match(loci_writer_f writer, void* cookie, of_match_t *match)
272{
273 int out = 0;
274""")
275
276 for key, entry in match.of_match_members.items():
277 m_type = entry["m_type"]
Rich Laneb8330b22014-10-13 21:13:19 -0700278 emitter = gen_emitter('of_match', key, m_type)
Rich Lanea06d0c32013-03-25 08:52:03 -0700279 out.write("""
280 if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) {
Andreas Wundsam53256162013-05-02 14:05:53 -0700281 out += writer(cookie, "%(key)s active=");
Rich Lanea06d0c32013-03-25 08:52:03 -0700282 out += %(emitter)s(writer, cookie, match->fields.%(key)s);
Andreas Wundsam53256162013-05-02 14:05:53 -0700283 out += writer(cookie, "/");
Rich Lanea06d0c32013-03-25 08:52:03 -0700284 out += %(emitter)s(writer, cookie, match->masks.%(key)s);
285 out += writer(cookie, " ");
286 }
287""" % dict(key=key, ku=key.upper(), emitter=emitter, m_type=m_type))
288
289 out.write("""
290 return out;
291}
292""")
293
294 # Generate big table indexed by version and object
295 for version in of_g.of_version_range:
296 out.write("""
Rich Laneb157b0f2013-03-27 13:55:28 -0700297static const loci_obj_show_f show_funs_v%(version)s[OF_OBJECT_COUNT] = {
Rich Lanea06d0c32013-03-25 08:52:03 -0700298""" % dict(version=version))
299 out.write(" unknown_show, /* of_object, not a valid specific type */\n")
300 for j, cls in enumerate(of_g.all_class_order):
301 comma = ""
302 if j < len(of_g.all_class_order) - 1: # Avoid ultimate comma
303 comma = ","
304
Andreas Wundsam53256162013-05-02 14:05:53 -0700305 if (not loxi_utils.class_in_version(cls, version) or
Rich Lane8841f352014-10-12 19:18:36 -0700306 type_maps.class_is_inheritance_root(cls)):
Rich Lanea06d0c32013-03-25 08:52:03 -0700307 out.write(" unknown_show%s\n" % comma);
308 else:
Andreas Wundsam53256162013-05-02 14:05:53 -0700309 out.write(" %s_%s_show%s\n" %
Rich Lanea06d0c32013-03-25 08:52:03 -0700310 (cls, loxi_utils.version_to_name(version), comma))
311 out.write("};\n\n")
312
313 out.write("""
Rich Lane463e1522014-10-14 11:20:52 -0700314static const loci_obj_show_f *const show_funs[] = {
315""")
316
317 for version in of_g.of_version_range:
318 out.write(" [%(v)d] = show_funs_v%(v)d,\n" % dict(v=version))
319
320 out.write("""\
Rich Lanea06d0c32013-03-25 08:52:03 -0700321};
322
323int
324of_object_show(loci_writer_f writer, void* cookie, of_object_t *obj)
325{
326 if ((obj->object_id > 0) && (obj->object_id < OF_OBJECT_COUNT)) {
Rich Lane463e1522014-10-14 11:20:52 -0700327 if (OF_VERSION_OKAY(obj->version)) {
Rich Lanea06d0c32013-03-25 08:52:03 -0700328 return show_funs[obj->version][obj->object_id](writer, cookie, (of_object_t *)obj);
329 } else {
330 return writer(cookie, "Bad version %d\\n", obj->version);
331 }
332 }
333 return writer(cookie, "Bad object id %d\\n", obj->object_id);
334}
335""")
336