blob: 1d6611515196e2586d311cb9efd6c0843b69f5e0 [file] [log] [blame]
Andreas Wundsamd30c1072013-11-15 13:36:57 -08001## List of mixed data types
2#
3# This is a list of data types which require special treatment
4# because the underlying datatype has changed between versions.
5# The main example is port which went from 16 to 32 bits. We
6# define per-version accessors for these types and those are
7# used in place of the normal ones.
8#
9# The wire protocol number is used to identify versions. For now,
10# the value is the name of the type to use for that version
11#
12# This is the map between the external type (like of_port_no_t)
13# which is used by customers of this code and the internal
14# datatypes (like uint16_t) that appear on the wire for a
15# particular version.
16#
17from collections import namedtuple
18import logging
19
20import loxi_front_end.frontend_ir as fe
21import loxi_ir.ir
22
23ofp_constants = dict(
24 OF_MAX_TABLE_NAME_LEN = 32,
25 OF_MAX_PORT_NAME_LEN = 16,
26 OF_ETH_ALEN = 6,
27 OF_DESC_STR_LEN = 256,
Praseed Balakrishnan7f718782014-09-18 17:02:48 -070028 OF_SERIAL_NUM_LEN = 32,
29 OF_APP_CODE_LEN = 15
Andreas Wundsamd30c1072013-11-15 13:36:57 -080030)
31
32
33of_mixed_types = dict(
34 of_port_no_t = {
35 1: "uint16_t",
36 2: "uint32_t",
37 3: "uint32_t",
38 4: "uint32_t",
39 "short_name":"port_no"
40 },
41 of_port_desc_t = {
42 1: "of_port_desc_t",
43 2: "of_port_desc_t",
44 3: "of_port_desc_t",
45 4: "of_port_desc_t",
46 "short_name":"port_desc"
47 },
48 of_bsn_vport_t = {
49 1: "of_bsn_vport_t",
50 2: "of_bsn_vport_t",
51 3: "of_bsn_vport_t",
52 4: "of_bsn_vport_t",
53 "short_name":"bsn_vport"
54 },
55 of_fm_cmd_t = { # Flow mod command went from u16 to u8
56 1: "uint16_t",
57 2: "uint8_t",
58 3: "uint8_t",
59 4: "uint8_t",
60 "short_name":"fm_cmd"
61 },
62 of_wc_bmap_t = { # Wildcard bitmap
63 1: "uint32_t",
64 2: "uint32_t",
65 3: "uint64_t",
66 4: "uint64_t",
67 "short_name":"wc_bmap"
68 },
69 of_match_bmap_t = { # Match bitmap
70 1: "uint32_t",
71 2: "uint32_t",
72 3: "uint64_t",
73 4: "uint64_t",
74 "short_name":"match_bmap"
75 },
76 of_match_t = { # Match object
77 1: "of_match_v1_t",
78 2: "of_match_v2_t",
79 3: "of_match_v3_t",
80 4: "of_match_v3_t", # Currently uses same match as 1.2 (v3).
81 "short_name":"match"
82 },
83)
84
85## basic lengths
86of_base_lengths = dict(
87 char = (1, True),
88 uint8_t = (1, True),
89 uint16_t = (2, True),
90 uint32_t = (4, True),
91 uint64_t = (8, True),
92 of_mac_addr_t = (6, True),
93 of_ipv4_t = (4, True),
94 of_ipv6_t = (16, True),
95 of_port_name_t = (ofp_constants["OF_MAX_PORT_NAME_LEN"], True),
96 of_table_name_t = (ofp_constants["OF_MAX_TABLE_NAME_LEN"], True),
97 of_desc_str_t = (ofp_constants["OF_DESC_STR_LEN"], True),
98 of_serial_num_t = (ofp_constants["OF_SERIAL_NUM_LEN"], True),
Rich Lanef8a3d002014-03-19 13:33:52 -070099 of_str64_t = (64, True),
Andreas Wundsamd30c1072013-11-15 13:36:57 -0800100 of_match_v1_t = (40, True),
101 of_match_v2_t = (88, True),
102 of_match_v3_t = (8, False),
103 of_octets_t = (0, False),
104 of_bitmap_128_t = (16, True),
Rich Lane51833982013-12-03 13:02:45 -0800105 of_checksum_128_t = (16, True),
Praseed Balakrishnan7f718782014-09-18 17:02:48 -0700106 of_app_code_t = (15,True),
Praseed Balakrishnan2ed6da02014-09-18 17:02:48 -0700107 of_sig_id_t = (6, True),
Andreas Wundsamd30c1072013-11-15 13:36:57 -0800108)
109
110def type_dec_to_count_base(m_type):
111 """
112 Resolve a type declaration like uint8_t[4] to a count (4) and base_type
113 (uint8_t)
114
115 @param m_type The string type declaration to process
116 """
117 count = 1
118 chk_ar = m_type.split('[')
119 if len(chk_ar) > 1:
120 count_str = chk_ar[1].split(']')[0]
121 if count_str in ofp_constants:
122 count = ofp_constants[count_str]
123 else:
124 count = int(count_str)
125 base_type = chk_ar[0]
126 else:
127 base_type = m_type
128 return count, base_type
129
130
131LengthInfo = namedtuple("LengthInfo", ("offset", "base_length", "is_fixed_length"))
132
133def calc_lengths(version, fe_class, existing_classes, existing_enums):
134 offset_fixed = True
135 offset = 0
136
137 member_infos = {}
138 for member in fe_class.members:
139 member_offset = offset if offset_fixed else None
140
141 if isinstance(member, fe.OFPadMember):
142 member_base_length = member.length
143 member_fixed_length = True
144 else:
145 m_type = member.oftype
146 name = member.name
147
148 member_base_length = 0
149 if m_type.find("list(") == 0:
150 member_fixed_length = False
151 elif m_type.find("struct") == 0:
152 raise Exception("Error: recursive struct found: {}, {}"
153 .format(fe_class.name, name))
154 elif m_type == "octets":
155 member_fixed_length = False
156 else:
157 member_base_length, member_fixed_length = member_length(version, fe_class, member, existing_classes, existing_enums)
158
159 if not member_fixed_length:
160 offset_fixed = False
161
162 member_infos[member] = LengthInfo(member_offset, member_base_length,
163 member_fixed_length)
164 offset += member_base_length
165
166 base_length = offset
167 fixed_length = offset_fixed if not fe_class.virtual else False
168 return (base_length, fixed_length, member_infos)
169
170def member_length(version, fe_class, fe_member, existing_classes, existing_enums):
171 """
172 return the length of an ir member.
173
174 @return tuple (base_length, length_fixed)
175 """
176 count, base_type = type_dec_to_count_base(fe_member.oftype)
177
Andreas Wundsamd30c1072013-11-15 13:36:57 -0800178 if base_type in of_mixed_types:
179 base_type = of_mixed_types[base_type][version.wire_version]
180
181 base_class = base_type[:-2]
182 if base_class in existing_classes:
183 member_ir_class = existing_classes[base_class]
184 bytes = member_ir_class.base_length
185 length_fixed = member_ir_class.is_fixed_length
186 else:
187 if base_type in existing_enums:
188 enum = existing_enums[base_type]
189 base_type = enum.wire_type
190
191 if base_type in of_base_lengths:
192 bytes, length_fixed = of_base_lengths[base_type]
193 else:
194 raise Exception("Unknown type for {}.{}: {}".format(fe_class.name, fe_member.name, base_type))
195
196 return (count * bytes), length_fixed