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 Dump function generation |
| 30 | |
| 31 | Generates dump 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 |
| 41 | import c_gen.loxi_utils_legacy as loxi_utils |
| 42 | import c_gen.identifiers as identifiers |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 43 | from c_test_gen import var_name_map |
| 44 | |
| 45 | def 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 Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 52 | * Header file for object dumping. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 53 | */ |
| 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 Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 78 | * Dump any OF object. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 79 | */ |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 80 | int of_object_dump(loci_writer_f writer, void* cookie, of_object_t* obj); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | """) |
| 88 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 89 | 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 |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 93 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 94 | continue |
| 95 | out.write("""\ |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 96 | int %(cls)s_%(ver_name)s_dump(loci_writer_f writer, void* cookie, of_object_t *obj); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 97 | """ % dict(cls=cls, ver_name=loxi_utils.version_to_name(version))) |
| 98 | |
| 99 | out.write(""" |
| 100 | #endif /* _LOCI_OBJ_DUMP_H_ */ |
| 101 | """) |
| 102 | |
| 103 | def 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 Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 110 | * Source file for object dumping. |
| 111 | * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 112 | */ |
| 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 | |
| 119 | static int |
| 120 | unknown_dump(loci_writer_f writer, void* cookie, of_object_t *obj) |
| 121 | { |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 122 | 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] | 123 | obj->object_id, obj->version); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 124 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 125 | """) |
| 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 |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 132 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 133 | continue |
| 134 | out.write(""" |
| 135 | int |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 136 | %(cls)s_%(ver_name)s_dump(loci_writer_f writer, void* cookie, of_object_t *obj) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 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) |
Rich Lane | 7fdaa5c | 2014-10-27 18:14:59 -0700 | [diff] [blame] | 153 | out.write(" of_object_t elt;\n int rv;\n") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 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); |
Rich Lane | 8bd4c93 | 2014-11-07 09:31:02 -0800 | [diff] [blame] | 187 | out += of_object_dump(writer, cookie, &%(v_name)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 188 | """ % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 189 | 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 | */ |
| 199 | int |
| 200 | loci_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 Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 228 | static const loci_obj_dump_f dump_funs_v%(version)s[OF_OBJECT_COUNT] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 229 | """ % 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 Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 236 | if (not loxi_utils.class_in_version(cls, version) or |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 237 | type_maps.class_is_virtual(cls)): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 238 | out.write(" unknown_dump%s\n" % comma); |
| 239 | else: |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 240 | out.write(" %s_%s_dump%s\n" % |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 241 | (cls, loxi_utils.version_to_name(version), comma)) |
| 242 | out.write("};\n\n") |
| 243 | |
| 244 | out.write(""" |
Rich Lane | 463e152 | 2014-10-14 11:20:52 -0700 | [diff] [blame] | 245 | static const loci_obj_dump_f *const dump_funs[] = { |
| 246 | """) |
| 247 | |
| 248 | for version in of_g.of_version_range: |
| 249 | out.write(" [%(v)d] = dump_funs_v%(v)d,\n" % dict(v=version)) |
| 250 | |
| 251 | out.write("""\ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | int |
| 255 | of_object_dump(loci_writer_f writer, void* cookie, of_object_t *obj) |
| 256 | { |
| 257 | if ((obj->object_id > 0) && (obj->object_id < OF_OBJECT_COUNT)) { |
Rich Lane | 463e152 | 2014-10-14 11:20:52 -0700 | [diff] [blame] | 258 | if (OF_VERSION_OKAY(obj->version)) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 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 | |