Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | # 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 | |
| 31 | Generates show function files. |
| 32 | |
| 33 | """ |
| 34 | |
| 35 | import sys |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 36 | import c_gen.of_g_legacy as of_g |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 37 | import c_gen.match as match |
| 38 | import c_gen.flags as flags |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 39 | from generic_utils import * |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 40 | import c_gen.type_maps as type_maps |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 41 | import loxi_utils.loxi_utils as loxi_utils |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 42 | import c_gen.loxi_utils_legacy as loxi_utils |
| 43 | import c_gen.identifiers as identifiers |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 44 | from c_test_gen import var_name_map |
| 45 | |
Rich Lane | b8330b2 | 2014-10-13 21:13:19 -0700 | [diff] [blame] | 46 | show_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 | |
| 53 | show_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'), |
Rich Lane | b8330b2 | 2014-10-13 21:13:19 -0700 | [diff] [blame] | 92 | ('uint8_t', 'value_mask'), |
| 93 | ('uint64_t', 'value_mask'), |
| 94 | ('uint64_t', 'write_setfields'), |
| 95 | ('uint64_t', 'apply_setfields'), |
| 96 | ('uint64_t', 'metadata_match'), |
| 97 | ('uint64_t', 'metadata_write'), |
| 98 | ('uint32_t', 'packet_in_mask_equal_master'), |
| 99 | ('uint32_t', 'packet_in_mask_slave'), |
| 100 | ('uint32_t', 'port_status_mask_equal_master'), |
| 101 | ('uint32_t', 'port_status_mask_slave'), |
| 102 | ('uint32_t', 'flow_removed_mask_equal_master'), |
| 103 | ('uint32_t', 'flow_removed_mask_slave'), |
| 104 | ('uint32_t', 'band_types'), |
| 105 | ('uint16_t', 'bsn_tcp_flags'), |
Wilson Ng | a3483d6 | 2014-11-05 13:50:37 -0800 | [diff] [blame] | 106 | ('uint8_t', 'bsn_l2_cache_hit'), |
Rich Lane | b8330b2 | 2014-10-13 21:13:19 -0700 | [diff] [blame] | 107 | ]) |
| 108 | |
| 109 | def gen_emitter(cls, m_name, m_type): |
| 110 | if (m_type, m_name) in show_override: |
| 111 | short_type = show_override[(m_type, m_name)] |
| 112 | elif (m_type, m_name) in show_hex: |
| 113 | short_type = loxi_utils.type_to_short_name(m_type).replace('u', 'x') |
| 114 | else: |
| 115 | short_type = loxi_utils.type_to_short_name(m_type) |
| 116 | return "LOCI_SHOW_" + short_type; |
| 117 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 118 | def gen_obj_show_h(out, name): |
| 119 | loxi_utils.gen_c_copy_license(out) |
| 120 | out.write(""" |
| 121 | /** |
| 122 | * |
| 123 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 124 | * |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 125 | * Header file for object showing. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 126 | */ |
| 127 | |
| 128 | /** |
| 129 | * Show object declarations |
| 130 | * |
| 131 | * Routines that emit a human-readable dump of each object. |
| 132 | * |
| 133 | */ |
| 134 | |
| 135 | #if !defined(_LOCI_OBJ_SHOW_H_) |
| 136 | #define _LOCI_OBJ_SHOW_H_ |
| 137 | |
| 138 | #include <loci/loci.h> |
| 139 | #include <stdio.h> |
| 140 | |
| 141 | /* g++ requires this to pick up PRI, etc. |
| 142 | * See http://gcc.gnu.org/ml/gcc-help/2006-10/msg00223.html |
| 143 | */ |
| 144 | #if !defined(__STDC_FORMAT_MACROS) |
| 145 | #define __STDC_FORMAT_MACROS |
| 146 | #endif |
| 147 | #include <inttypes.h> |
| 148 | |
| 149 | |
| 150 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 151 | * Show any OF object. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 152 | */ |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 153 | int of_object_show(loci_writer_f writer, void* cookie, of_object_t* obj); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | """) |
| 161 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 162 | for version in of_g.of_version_range: |
| 163 | for cls in of_g.standard_class_order: |
| 164 | if not loxi_utils.class_in_version(cls, version): |
| 165 | continue |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 166 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 167 | continue |
| 168 | out.write("""\ |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 169 | int %(cls)s_%(ver_name)s_show(loci_writer_f writer, void* cookie, of_object_t *obj); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 170 | """ % dict(cls=cls, ver_name=loxi_utils.version_to_name(version))) |
| 171 | |
| 172 | out.write(""" |
| 173 | #endif /* _LOCI_OBJ_SHOW_H_ */ |
| 174 | """) |
| 175 | |
| 176 | def gen_obj_show_c(out, name): |
| 177 | loxi_utils.gen_c_copy_license(out) |
| 178 | out.write(""" |
| 179 | /** |
| 180 | * |
| 181 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 182 | * |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 183 | * Source file for object showing. |
| 184 | * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 185 | */ |
| 186 | |
| 187 | #define DISABLE_WARN_UNUSED_RESULT |
| 188 | #include <loci/loci.h> |
| 189 | #include <loci/loci_show.h> |
| 190 | #include <loci/loci_obj_show.h> |
| 191 | |
| 192 | static int |
| 193 | unknown_show(loci_writer_f writer, void* cookie, of_object_t *obj) |
| 194 | { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 195 | return writer(cookie, "Unable to print object of type %d, version %d\\n", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 196 | obj->object_id, obj->version); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 197 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 198 | """) |
| 199 | |
| 200 | for version in of_g.of_version_range: |
| 201 | ver_name = loxi_utils.version_to_name(version) |
| 202 | for cls in of_g.standard_class_order: |
| 203 | if not loxi_utils.class_in_version(cls, version): |
| 204 | continue |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 205 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 206 | continue |
| 207 | out.write(""" |
| 208 | int |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 209 | %(cls)s_%(ver_name)s_show(loci_writer_f writer, void* cookie, of_object_t *obj) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 210 | { |
| 211 | int out = 0; |
| 212 | """ % dict(cls=cls, ver_name=ver_name)) |
| 213 | |
| 214 | members, member_types = loxi_utils.all_member_types_get(cls, version) |
| 215 | for m_type in member_types: |
| 216 | if loxi_utils.type_is_scalar(m_type) or m_type in \ |
| 217 | ["of_match_t", "of_octets_t"]: |
| 218 | # Declare instance of these |
| 219 | out.write(" %s %s;\n" % (m_type, var_name_map(m_type))) |
| 220 | else: |
| 221 | out.write(""" |
| 222 | %(m_type)s %(v_name)s; |
| 223 | """ % dict(m_type=m_type, v_name=var_name_map(m_type))) |
| 224 | if loxi_utils.class_is_list(m_type): |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 225 | out.write(" of_object_t elt;\n int rv;\n") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 226 | for member in members: |
| 227 | m_type = member["m_type"] |
| 228 | m_name = member["name"] |
Rich Lane | b8330b2 | 2014-10-13 21:13:19 -0700 | [diff] [blame] | 229 | emitter = gen_emitter(cls, m_name, m_type) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 230 | if loxi_utils.skip_member_name(m_name): |
| 231 | continue |
| 232 | if (loxi_utils.type_is_scalar(m_type) or |
| 233 | m_type in ["of_match_t", "of_octets_t"]): |
| 234 | out.write(""" |
| 235 | %(cls)s_%(m_name)s_get(obj, &%(v_name)s); |
| 236 | out += writer(cookie, "%(m_name)s="); |
| 237 | out += %(emitter)s(writer, cookie, %(v_name)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 238 | out += writer(cookie, " "); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 239 | """ % dict(cls=cls, m_name=m_name, m_type=m_type, |
| 240 | v_name=var_name_map(m_type), emitter=emitter)) |
| 241 | elif loxi_utils.class_is_list(m_type): |
| 242 | sub_cls = m_type[:-2] # Trim _t |
| 243 | elt_type = loxi_utils.list_to_entry_type(m_type) |
| 244 | out.write(""" |
| 245 | out += writer(cookie, "%(elt_type)s={ "); |
| 246 | %(cls)s_%(m_name)s_bind(obj, &%(v_name)s); |
| 247 | %(u_type)s_ITER(&%(v_name)s, &elt, rv) { |
| 248 | of_object_show(writer, cookie, (of_object_t *)&elt); |
| 249 | } |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 250 | out += writer(cookie, "} "); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 251 | """ % dict(sub_cls=sub_cls, u_type=sub_cls.upper(), v_name=var_name_map(m_type), |
| 252 | elt_type=elt_type, cls=cls, m_name=m_name, m_type=m_type)) |
| 253 | else: |
| 254 | sub_cls = m_type[:-2] # Trim _t |
| 255 | out.write(""" |
| 256 | %(cls)s_%(m_name)s_bind(obj, &%(v_name)s); |
Rich Lane | 8bd4c93 | 2014-11-07 09:31:02 -0800 | [diff] [blame] | 257 | out += of_object_show(writer, cookie, &%(v_name)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 258 | """ % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 259 | v_name=var_name_map(m_type), ver_name=ver_name)) |
| 260 | |
| 261 | out.write(""" |
| 262 | return out; |
| 263 | } |
| 264 | """) |
| 265 | out.write(""" |
| 266 | /** |
| 267 | * Log a match entry |
| 268 | */ |
| 269 | int |
| 270 | loci_show_match(loci_writer_f writer, void* cookie, of_match_t *match) |
| 271 | { |
| 272 | int out = 0; |
| 273 | """) |
| 274 | |
| 275 | for key, entry in match.of_match_members.items(): |
| 276 | m_type = entry["m_type"] |
Rich Lane | b8330b2 | 2014-10-13 21:13:19 -0700 | [diff] [blame] | 277 | emitter = gen_emitter('of_match', key, m_type) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 278 | out.write(""" |
| 279 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 280 | out += writer(cookie, "%(key)s active="); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 281 | out += %(emitter)s(writer, cookie, match->fields.%(key)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 282 | out += writer(cookie, "/"); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 283 | out += %(emitter)s(writer, cookie, match->masks.%(key)s); |
| 284 | out += writer(cookie, " "); |
| 285 | } |
| 286 | """ % dict(key=key, ku=key.upper(), emitter=emitter, m_type=m_type)) |
| 287 | |
| 288 | out.write(""" |
| 289 | return out; |
| 290 | } |
| 291 | """) |
| 292 | |
| 293 | # Generate big table indexed by version and object |
| 294 | for version in of_g.of_version_range: |
| 295 | out.write(""" |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 296 | static const loci_obj_show_f show_funs_v%(version)s[OF_OBJECT_COUNT] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 297 | """ % dict(version=version)) |
| 298 | out.write(" unknown_show, /* of_object, not a valid specific type */\n") |
| 299 | for j, cls in enumerate(of_g.all_class_order): |
| 300 | comma = "" |
| 301 | if j < len(of_g.all_class_order) - 1: # Avoid ultimate comma |
| 302 | comma = "," |
| 303 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 304 | if (not loxi_utils.class_in_version(cls, version) or |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 305 | type_maps.class_is_virtual(cls)): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 306 | out.write(" unknown_show%s\n" % comma); |
| 307 | else: |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 308 | out.write(" %s_%s_show%s\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 309 | (cls, loxi_utils.version_to_name(version), comma)) |
| 310 | out.write("};\n\n") |
| 311 | |
| 312 | out.write(""" |
Rich Lane | 463e152 | 2014-10-14 11:20:52 -0700 | [diff] [blame] | 313 | static const loci_obj_show_f *const show_funs[] = { |
| 314 | """) |
| 315 | |
| 316 | for version in of_g.of_version_range: |
| 317 | out.write(" [%(v)d] = show_funs_v%(v)d,\n" % dict(v=version)) |
| 318 | |
| 319 | out.write("""\ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | int |
| 323 | of_object_show(loci_writer_f writer, void* cookie, of_object_t *obj) |
| 324 | { |
| 325 | if ((obj->object_id > 0) && (obj->object_id < OF_OBJECT_COUNT)) { |
Rich Lane | 463e152 | 2014-10-14 11:20:52 -0700 | [diff] [blame] | 326 | if (OF_VERSION_OKAY(obj->version)) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 327 | return show_funs[obj->version][obj->object_id](writer, cookie, (of_object_t *)obj); |
| 328 | } else { |
| 329 | return writer(cookie, "Bad version %d\\n", obj->version); |
| 330 | } |
| 331 | } |
| 332 | return writer(cookie, "Bad object id %d\\n", obj->object_id); |
| 333 | } |
| 334 | """) |
| 335 | |