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 | |
| 46 | def 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 Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 53 | * Header file for object showing. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 54 | */ |
| 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 Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 79 | * Show any OF object. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 80 | */ |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 81 | 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] | 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | """) |
| 89 | |
| 90 | type_to_emitter = dict( |
| 91 | |
| 92 | ) |
| 93 | for version in of_g.of_version_range: |
| 94 | for cls in of_g.standard_class_order: |
| 95 | if not loxi_utils.class_in_version(cls, version): |
| 96 | continue |
| 97 | if cls in type_maps.inheritance_map: |
| 98 | continue |
| 99 | out.write("""\ |
| 100 | int %(cls)s_%(ver_name)s_show(loci_writer_f writer, void* cookie, %(cls)s_t *obj); |
| 101 | """ % dict(cls=cls, ver_name=loxi_utils.version_to_name(version))) |
| 102 | |
| 103 | out.write(""" |
| 104 | #endif /* _LOCI_OBJ_SHOW_H_ */ |
| 105 | """) |
| 106 | |
| 107 | def gen_obj_show_c(out, name): |
| 108 | loxi_utils.gen_c_copy_license(out) |
| 109 | out.write(""" |
| 110 | /** |
| 111 | * |
| 112 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 113 | * |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 114 | * Source file for object showing. |
| 115 | * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 116 | */ |
| 117 | |
| 118 | #define DISABLE_WARN_UNUSED_RESULT |
| 119 | #include <loci/loci.h> |
| 120 | #include <loci/loci_show.h> |
| 121 | #include <loci/loci_obj_show.h> |
| 122 | |
| 123 | static int |
| 124 | unknown_show(loci_writer_f writer, void* cookie, of_object_t *obj) |
| 125 | { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 126 | 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] | 127 | obj->object_id, obj->version); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 128 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 129 | """) |
| 130 | |
| 131 | for version in of_g.of_version_range: |
| 132 | ver_name = loxi_utils.version_to_name(version) |
| 133 | for cls in of_g.standard_class_order: |
| 134 | if not loxi_utils.class_in_version(cls, version): |
| 135 | continue |
| 136 | if cls in type_maps.inheritance_map: |
| 137 | continue |
| 138 | out.write(""" |
| 139 | int |
| 140 | %(cls)s_%(ver_name)s_show(loci_writer_f writer, void* cookie, %(cls)s_t *obj) |
| 141 | { |
| 142 | int out = 0; |
| 143 | """ % dict(cls=cls, ver_name=ver_name)) |
| 144 | |
| 145 | members, member_types = loxi_utils.all_member_types_get(cls, version) |
| 146 | for m_type in member_types: |
| 147 | if loxi_utils.type_is_scalar(m_type) or m_type in \ |
| 148 | ["of_match_t", "of_octets_t"]: |
| 149 | # Declare instance of these |
| 150 | out.write(" %s %s;\n" % (m_type, var_name_map(m_type))) |
| 151 | else: |
| 152 | out.write(""" |
| 153 | %(m_type)s %(v_name)s; |
| 154 | """ % dict(m_type=m_type, v_name=var_name_map(m_type))) |
| 155 | if loxi_utils.class_is_list(m_type): |
| 156 | base_type = loxi_utils.list_to_entry_type(m_type) |
| 157 | out.write(" %s elt;\n int rv;\n" % base_type) |
| 158 | for member in members: |
| 159 | m_type = member["m_type"] |
| 160 | m_name = member["name"] |
| 161 | #emitter = "LOCI_SHOW_" + loxi_utils.type_to_short_name(m_type) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 162 | emitter = "LOCI_SHOW_" + loxi_utils.type_to_short_name(m_type) + "_" + m_name; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 163 | if loxi_utils.skip_member_name(m_name): |
| 164 | continue |
| 165 | if (loxi_utils.type_is_scalar(m_type) or |
| 166 | m_type in ["of_match_t", "of_octets_t"]): |
| 167 | out.write(""" |
| 168 | %(cls)s_%(m_name)s_get(obj, &%(v_name)s); |
| 169 | out += writer(cookie, "%(m_name)s="); |
| 170 | out += %(emitter)s(writer, cookie, %(v_name)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 171 | out += writer(cookie, " "); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 172 | """ % dict(cls=cls, m_name=m_name, m_type=m_type, |
| 173 | v_name=var_name_map(m_type), emitter=emitter)) |
| 174 | elif loxi_utils.class_is_list(m_type): |
| 175 | sub_cls = m_type[:-2] # Trim _t |
| 176 | elt_type = loxi_utils.list_to_entry_type(m_type) |
| 177 | out.write(""" |
| 178 | out += writer(cookie, "%(elt_type)s={ "); |
| 179 | %(cls)s_%(m_name)s_bind(obj, &%(v_name)s); |
| 180 | %(u_type)s_ITER(&%(v_name)s, &elt, rv) { |
| 181 | of_object_show(writer, cookie, (of_object_t *)&elt); |
| 182 | } |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 183 | out += writer(cookie, "} "); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 184 | """ % dict(sub_cls=sub_cls, u_type=sub_cls.upper(), v_name=var_name_map(m_type), |
| 185 | elt_type=elt_type, cls=cls, m_name=m_name, m_type=m_type)) |
| 186 | else: |
| 187 | sub_cls = m_type[:-2] # Trim _t |
| 188 | out.write(""" |
| 189 | %(cls)s_%(m_name)s_bind(obj, &%(v_name)s); |
| 190 | out += %(sub_cls)s_%(ver_name)s_show(writer, cookie, &%(v_name)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 191 | """ % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 192 | v_name=var_name_map(m_type), ver_name=ver_name)) |
| 193 | |
| 194 | out.write(""" |
| 195 | return out; |
| 196 | } |
| 197 | """) |
| 198 | out.write(""" |
| 199 | /** |
| 200 | * Log a match entry |
| 201 | */ |
| 202 | int |
| 203 | loci_show_match(loci_writer_f writer, void* cookie, of_match_t *match) |
| 204 | { |
| 205 | int out = 0; |
| 206 | """) |
| 207 | |
| 208 | for key, entry in match.of_match_members.items(): |
| 209 | m_type = entry["m_type"] |
| 210 | #emitter = "LOCI_SHOW_" + loxi_utils.type_to_short_name(m_type) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 211 | emitter = "LOCI_SHOW_" + loxi_utils.type_to_short_name(m_type) + "_" + key; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 212 | out.write(""" |
| 213 | if (OF_MATCH_MASK_%(ku)s_ACTIVE_TEST(match)) { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 214 | out += writer(cookie, "%(key)s active="); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 215 | out += %(emitter)s(writer, cookie, match->fields.%(key)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 216 | out += writer(cookie, "/"); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 217 | out += %(emitter)s(writer, cookie, match->masks.%(key)s); |
| 218 | out += writer(cookie, " "); |
| 219 | } |
| 220 | """ % dict(key=key, ku=key.upper(), emitter=emitter, m_type=m_type)) |
| 221 | |
| 222 | out.write(""" |
| 223 | return out; |
| 224 | } |
| 225 | """) |
| 226 | |
| 227 | # Generate big table indexed by version and object |
| 228 | for version in of_g.of_version_range: |
| 229 | out.write(""" |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 230 | 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] | 231 | """ % dict(version=version)) |
| 232 | out.write(" unknown_show, /* of_object, not a valid specific type */\n") |
| 233 | for j, cls in enumerate(of_g.all_class_order): |
| 234 | comma = "" |
| 235 | if j < len(of_g.all_class_order) - 1: # Avoid ultimate comma |
| 236 | comma = "," |
| 237 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 238 | if (not loxi_utils.class_in_version(cls, version) or |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 239 | cls in type_maps.inheritance_map): |
| 240 | out.write(" unknown_show%s\n" % comma); |
| 241 | else: |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 242 | out.write(" %s_%s_show%s\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 243 | (cls, loxi_utils.version_to_name(version), comma)) |
| 244 | out.write("};\n\n") |
| 245 | |
| 246 | out.write(""" |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 247 | static const loci_obj_show_f *const show_funs[5] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 248 | NULL, |
| 249 | show_funs_v1, |
| 250 | show_funs_v2, |
| 251 | show_funs_v3, |
| 252 | show_funs_v4 |
| 253 | }; |
| 254 | |
| 255 | int |
| 256 | of_object_show(loci_writer_f writer, void* cookie, of_object_t *obj) |
| 257 | { |
| 258 | if ((obj->object_id > 0) && (obj->object_id < OF_OBJECT_COUNT)) { |
| 259 | if (((obj)->version > 0) && ((obj)->version <= OF_VERSION_1_2)) { |
| 260 | /* @fixme VERSION */ |
| 261 | return show_funs[obj->version][obj->object_id](writer, cookie, (of_object_t *)obj); |
| 262 | } else { |
| 263 | return writer(cookie, "Bad version %d\\n", obj->version); |
| 264 | } |
| 265 | } |
| 266 | return writer(cookie, "Bad object id %d\\n", obj->object_id); |
| 267 | } |
| 268 | """) |
| 269 | |