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 C code generation for LOXI type related maps |
| 30 | # |
| 31 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 32 | import c_gen.of_g_legacy as of_g |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 33 | import sys |
| 34 | from generic_utils import * |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 35 | import c_gen.type_maps as type_maps |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 36 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 37 | def gen_type_data_header(out): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 38 | out.write(""" |
| 39 | /**************************************************************** |
| 40 | * Wire type/length functions. |
| 41 | ****************************************************************/ |
| 42 | |
| 43 | extern void of_object_message_wire_length_get(of_object_t *obj, int *bytes); |
| 44 | extern void of_object_message_wire_length_set(of_object_t *obj, int bytes); |
| 45 | |
| 46 | extern void of_oxm_wire_length_get(of_object_t *obj, int *bytes); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 47 | |
| 48 | extern void of_tlv16_wire_length_get(of_object_t *obj, int *bytes); |
| 49 | extern void of_tlv16_wire_length_set(of_object_t *obj, int bytes); |
| 50 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 51 | /* Wire length is uint16 at front of structure */ |
| 52 | extern void of_u16_len_wire_length_get(of_object_t *obj, int *bytes); |
| 53 | extern void of_u16_len_wire_length_set(of_object_t *obj, int bytes); |
| 54 | |
Rich Lane | 4708572 | 2013-07-12 16:27:04 -0700 | [diff] [blame] | 55 | #define OF_OXM_LENGTH_GET(hdr) (((hdr) & 0xff) + 4) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 56 | #define OF_OXM_LENGTH_SET(hdr, val) \\ |
Rich Lane | 4708572 | 2013-07-12 16:27:04 -0700 | [diff] [blame] | 57 | (hdr) = ((hdr) & 0xffffff00) + (((val) - 4) & 0xff) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 58 | |
| 59 | extern void of_packet_queue_wire_length_get(of_object_t *obj, int *bytes); |
| 60 | extern void of_packet_queue_wire_length_set(of_object_t *obj, int bytes); |
| 61 | |
| 62 | extern void of_list_meter_band_stats_wire_length_get(of_object_t *obj, |
| 63 | int *bytes); |
| 64 | extern void of_meter_stats_wire_length_get(of_object_t *obj, int *bytes); |
| 65 | extern void of_meter_stats_wire_length_set(of_object_t *obj, int bytes); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 66 | |
| 67 | """) |
| 68 | |
| 69 | |
| 70 | def gen_length_array(out): |
| 71 | """ |
| 72 | Generate an array giving the lengths of all objects/versions |
| 73 | @param out The file handle to which to write |
| 74 | """ |
| 75 | out.write(""" |
| 76 | /** |
| 77 | * An array with the number of bytes in the fixed length part |
| 78 | * of each OF object |
| 79 | */ |
| 80 | """) |
| 81 | |
| 82 | for version in of_g.of_version_range: |
| 83 | out.write(""" |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 84 | static const int\nof_object_fixed_len_v%d[OF_OBJECT_COUNT] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 85 | -1, /* of_object is not instantiable */ |
| 86 | """ % version) |
| 87 | for i, cls in enumerate(of_g.all_class_order): |
| 88 | comma = "," |
| 89 | if i == len(of_g.all_class_order) - 1: |
| 90 | comma = "" |
| 91 | val = "-1" + comma |
| 92 | if (cls, version) in of_g.base_length: |
| 93 | val = str(of_g.base_length[(cls, version)]) + comma |
| 94 | out.write(" %-5s /* %d: %s */\n" % (val, i + 1, cls)) |
| 95 | out.write("};\n") |
| 96 | |
| 97 | out.write(""" |
| 98 | /** |
| 99 | * Unified map of fixed length part of each object |
| 100 | */ |
Rich Lane | b157b0f | 2013-03-27 13:55:28 -0700 | [diff] [blame] | 101 | const int *const of_object_fixed_len[OF_VERSION_ARRAY_MAX] = { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 102 | NULL, |
| 103 | """) |
| 104 | for version in of_g.of_version_range: |
| 105 | out.write(" of_object_fixed_len_v%d,\n" % version) |
| 106 | out.write(""" |
| 107 | }; |
| 108 | """) |
| 109 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 110 | |
Rich Lane | f70be94 | 2013-07-18 13:33:14 -0700 | [diff] [blame] | 111 | def gen_extra_length_array(out): |
| 112 | """ |
| 113 | Generate an array giving the extra lengths of all objects/versions |
| 114 | @param out The file handle to which to write |
| 115 | """ |
| 116 | out.write(""" |
| 117 | /** |
| 118 | * An array with the number of bytes in the extra length part |
| 119 | * of each OF object |
| 120 | */ |
| 121 | """) |
| 122 | |
| 123 | for version in of_g.of_version_range: |
| 124 | out.write(""" |
| 125 | static const int\nof_object_extra_len_v%d[OF_OBJECT_COUNT] = { |
| 126 | -1, /* of_object is not instantiable */ |
| 127 | """ % version) |
| 128 | for i, cls in enumerate(of_g.all_class_order): |
| 129 | comma = "," |
| 130 | if i == len(of_g.all_class_order) - 1: |
| 131 | comma = "" |
| 132 | val = "-1" + comma |
| 133 | if (cls, version) in of_g.base_length: |
| 134 | val = str(of_g.extra_length.get((cls, version), 0)) + comma |
| 135 | out.write(" %-5s /* %d: %s */\n" % (val, i + 1, cls)) |
| 136 | out.write("};\n") |
| 137 | |
| 138 | out.write(""" |
| 139 | /** |
| 140 | * Unified map of extra length part of each object |
| 141 | */ |
| 142 | const int *const of_object_extra_len[OF_VERSION_ARRAY_MAX] = { |
| 143 | NULL, |
| 144 | """) |
| 145 | for version in of_g.of_version_range: |
| 146 | out.write(" of_object_extra_len_v%d,\n" % version) |
| 147 | out.write(""" |
| 148 | }; |
| 149 | """) |