blob: 80d922c89e389d9be6c2d9a77915c72c7b18b835 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001# 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 Global data structs for LOXI code generation
30#
31# @fixme This needs to be refactored and brought into the 21st century.
32#
33
34import sys
Rich Lanea06d0c32013-03-25 08:52:03 -070035# @fixme Replace with argparse
36
37################################################################
38#
39# Configuration global parameters
40#
41################################################################
42
43##
44# The map from wire protocol to enum identifier generated from input
45# This is built from the version-specific structs file info.
46# @fixme This should go away when the process structs file is updated
47wire_ver_map = {}
48
49##
Rich Lanea06d0c32013-03-25 08:52:03 -070050# The list of wire versions which are to be supported
51target_version_list = []
52
Rich Lanea06d0c32013-03-25 08:52:03 -070053##
54# The dictionary of config variables related to code
55#
56# @param gen_unified_fns Boolean; Generate top level function definitions for
Andreas Wundsam53256162013-05-02 14:05:53 -070057# accessors which are independent of the version; the alternative is to only
Rich Lanea06d0c32013-03-25 08:52:03 -070058# use the function pointers in the class definitions. These functions support
59# better inlining optimizations.
60#
61# @param gen_fn_ptrs Boolean; Generate the functions pointed to by pointer
62# in the class (struct) definitions; the alternative is to only use the
63# unified (use_) functions
64#
65# @param use_obj_id Use object IDs in struct defns CURRENTLY NOT SUPPORTED
Andreas Wundsam53256162013-05-02 14:05:53 -070066#
Rich Lanea06d0c32013-03-25 08:52:03 -070067# @param return_base_types For 'get' accessors, return values when possible.
68# Otherwise all values are returned thru a call by variable parameter
69#
70# @param use_static_inlines Generate low level accessors as static inline
71# and put them in header files rather than .c files.
72#
73# @param copy_semantics One of "read", "write" or "grow". This defines the
74# way that buffer references are managed. Currently on "read" is supported.
75#
76# @param encode_typedefs Use object and member IDs (rather than names)
77# when generating the names used for accessor function typedefs
78#
Andreas Wundsam53256162013-05-02 14:05:53 -070079# @param get_returns One of "error", "value", or "void";
Rich Lanea06d0c32013-03-25 08:52:03 -070080# CURRENTLY ONLY "error" IS SUPPORTED. "error" means
81# all get operations return an error code. "value" means return a base_type
82# value when possible or void if not. "void" means always return void
83# and use a call-by-variable parameter
84#
85
86# @fixme These are still very C specific and should probably either
87# go into lang_c.py or be swallowed by command line option parsing
88code_gen_config = dict(
89 gen_unified_fns=True,
90# gen_fn_ptrs=True, # WARNING: Haven't tested with this in a while
91 gen_fn_ptrs=False,
92 use_obj_id=False,
93 use_static_inlines=False,
94 copy_semantics="read", # Only read implemented: read, write, grow
95 encoded_typedefs=False,
96 get_returns="error", # Only error implemented; error, value, void
97)
98
99## These members do not get normal accessors
100
Rob Vaterlausb3f49d92013-10-01 17:57:31 -0700101skip_members = ["version", "type", "length", "err_type", "stats_type", "len",
Rich Lanee3113672013-12-06 17:09:57 -0800102 "type_len", "actions_len", "_command", "command"]
Rich Lanea06d0c32013-03-25 08:52:03 -0700103
104## Some OpenFlow string length constants
105#
106# These are a few length constants needed for array processing
107ofp_constants = dict(
108 OF_MAX_TABLE_NAME_LEN = 32,
109 OF_MAX_PORT_NAME_LEN = 16,
110 OF_ETH_ALEN = 6,
111 OF_DESC_STR_LEN = 256,
112 OF_SERIAL_NUM_LEN = 32
113)
114
115## List of mixed data types
116#
117# This is a list of data types which require special treatment
118# because the underlying datatype has changed between versions.
119# The main example is port which went from 16 to 32 bits. We
120# define per-version accessors for these types and those are
121# used in place of the normal ones.
122#
123# The wire protocol number is used to identify versions. For now,
124# the value is the name of the type to use for that version
125#
126# This is the map between the external type (like of_port_no_t)
Andreas Wundsam53256162013-05-02 14:05:53 -0700127# which is used by customers of this code and the internal
128# datatypes (like uint16_t) that appear on the wire for a
Rich Lanea06d0c32013-03-25 08:52:03 -0700129# particular version.
130#
131of_mixed_types = dict(
132 of_port_no_t = {
133 1: "uint16_t",
134 2: "uint32_t",
135 3: "uint32_t",
136 4: "uint32_t",
137 "short_name":"port_no"
138 },
139 of_port_desc_t = {
140 1: "of_port_desc_t",
141 2: "of_port_desc_t",
142 3: "of_port_desc_t",
143 4: "of_port_desc_t",
144 "short_name":"port_desc"
145 },
Dan Talaycoc0e802e2013-05-18 23:52:39 -0700146 of_bsn_vport_t = {
147 1: "of_bsn_vport_t",
148 2: "of_bsn_vport_t",
149 3: "of_bsn_vport_t",
150 4: "of_bsn_vport_t",
151 "short_name":"bsn_vport"
152 },
Rich Lanea06d0c32013-03-25 08:52:03 -0700153 of_fm_cmd_t = { # Flow mod command went from u16 to u8
154 1: "uint16_t",
155 2: "uint8_t",
156 3: "uint8_t",
157 4: "uint8_t",
158 "short_name":"fm_cmd"
159 },
160 of_wc_bmap_t = { # Wildcard bitmap
161 1: "uint32_t",
162 2: "uint32_t",
163 3: "uint64_t",
164 4: "uint64_t",
165 "short_name":"wc_bmap"
166 },
167 of_match_bmap_t = { # Match bitmap
168 1: "uint32_t",
169 2: "uint32_t",
170 3: "uint64_t",
171 4: "uint64_t",
172 "short_name":"match_bmap"
173 },
174 of_match_t = { # Match object
175 1: "of_match_v1_t",
176 2: "of_match_v2_t",
177 3: "of_match_v3_t",
178 4: "of_match_v3_t", # Currently uses same match as 1.2 (v3).
179 "short_name":"match"
180 },
181)
182
183## Base data types
184#
185# The basic types; Value is a list: bytes, to_wire, from_wire
186# The accessors deal with endian, alignment and any other host/network
187# considerations. These are common across all versions
188#
189# For get accessors, assume we memcpy from wire buf and then apply ntoh
190# For set accessors, assume we apply hton and then memcpy to wire buf
191#
192# to/from wire functions take a pointer to class and change in place
193of_base_types = dict(
194 char = dict(bytes=1, use_as_rv=1, short_name="char"),
195 uint8_t = dict(bytes=1, use_as_rv=1, short_name="u8"),
196 uint16_t = dict(bytes=2, to_w="u16_hton", from_w="u16_ntoh", use_as_rv=1,
197 short_name="u16"),
198 uint32_t = dict(bytes=4, to_w="u32_hton", from_w="u32_ntoh", use_as_rv=1,
199 short_name="u32"),
200 uint64_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1,
201 short_name="u64"),
202# of_cookie_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1#,
203# short_name="cookie"),
204# of_counter_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1,
205# short_name="counter"),
206 of_mac_addr_t = dict(bytes=6, short_name="mac"),
Andreas Wundsamb566a162013-07-18 19:30:23 -0700207 of_ipv4_t = dict(bytes=4, short_name="ipv4"),
Rich Lanea06d0c32013-03-25 08:52:03 -0700208 of_ipv6_t = dict(bytes=16, short_name="ipv6"),
209 of_port_name_t = dict(bytes=ofp_constants["OF_MAX_PORT_NAME_LEN"],
210 short_name="port_name"),
211 of_table_name_t = dict(bytes=ofp_constants["OF_MAX_TABLE_NAME_LEN"],
212 short_name="tab_name"),
213 of_desc_str_t = dict(bytes=ofp_constants["OF_DESC_STR_LEN"],
214 short_name="desc_str"),
215 of_serial_num_t = dict(bytes=ofp_constants["OF_SERIAL_NUM_LEN"],
216 short_name="ser_num"),
Andreas Wundsam53256162013-05-02 14:05:53 -0700217 of_match_v1_t = dict(bytes=40, to_w="match_v1_hton",
218 from_w="match_v1_ntoh",
Rich Lanea06d0c32013-03-25 08:52:03 -0700219 short_name="match_v1"),
Andreas Wundsam53256162013-05-02 14:05:53 -0700220 of_match_v2_t = dict(bytes=88, to_w="match_v2_hton",
221 from_w="match_v2_ntoh",
Rich Lanea06d0c32013-03-25 08:52:03 -0700222 short_name="match_v2"),
Andreas Wundsam53256162013-05-02 14:05:53 -0700223 of_match_v3_t = dict(bytes=-1, to_w="match_v3_hton",
224 from_w="match_v3_ntoh",
Rich Lanea06d0c32013-03-25 08:52:03 -0700225 short_name="match_v3"),
Andreas Wundsam53256162013-05-02 14:05:53 -0700226# of_match_v4_t = dict(bytes=-1, to_w="match_v4_hton",
227# from_w="match_v4_ntoh",
Rich Lanea06d0c32013-03-25 08:52:03 -0700228# short_name="match_v4"),
Rich Lane3b2fd832013-09-24 13:44:08 -0700229 of_octets_t = dict(bytes=-1, short_name="octets"),
230 of_bitmap_128_t = dict(bytes=16, short_name="bitmap_128"),
Rich Lanea06d0c32013-03-25 08:52:03 -0700231)
232
233of_scalar_types = ["char", "uint8_t", "uint16_t", "uint32_t", "uint64_t",
234 "of_port_no_t", "of_fm_cmd_t", "of_wc_bmap_t",
235 "of_match_bmap_t", "of_port_name_t", "of_table_name_t",
Andreas Wundsam53256162013-05-02 14:05:53 -0700236 "of_desc_str_t", "of_serial_num_t", "of_mac_addr_t",
Rich Lane3b2fd832013-09-24 13:44:08 -0700237 "of_ipv6_t", "of_ipv4_t", "of_bitmap_128_t"]
Rich Lanea06d0c32013-03-25 08:52:03 -0700238
239base_object_members = """\
240 /* The control block for the underlying data buffer */
241 of_wire_object_t wire_object;
242 /* The LOCI type enum value of the object */
243 of_object_id_t object_id;
244
245 /*
246 * Objects need to track their "parent" so that updates to the
247 * object that affect its length can be pushed to the parent.
248 * Treat as private.
249 */
250 of_object_t *parent;
251
252 /*
253 * Not all objects have length and version on the wire so we keep
254 * them here. NOTE: Infrastructure manages length and version.
255 * Treat length as private and version as read only.
256 */
257 int length;
258 of_version_t version;
259
260 /*
261 * Many objects have a length and/or type represented in the wire buffer
262 * These accessors get and set those value when present. Treat as private.
263 */
264 of_wire_length_get_f wire_length_get;
265 of_wire_length_set_f wire_length_set;
266 of_wire_type_get_f wire_type_get;
267 of_wire_type_set_f wire_type_set;
268
269 of_object_track_info_t track_info;
270
271 /*
272 * Metadata available for applications. Ensure 8-byte alignment, but
273 * that buffer is at least as large as requested. This data is not used
274 * or inspected by LOCI.
275 */
276 uint64_t metadata[(OF_OBJECT_METADATA_BYTES + 7) / 8];
277"""
278
Rich Lanea06d0c32013-03-25 08:52:03 -0700279##
280# LOXI identifiers
281#
282# Dict indexed by identifier name. Each entry contains the information
283# as a DotDict with the following keys:
284# values: A dict indexed by wire version giving each verion's value or None
285# common: The common value to use for this identifier at the LOXI top level (TBD)
286# all_same: If True, all the values across all versions are the same
287# ofp_name: The original name for the identifier
288# ofp_group: The ofp enumerated type if defined
289
290identifiers = {}
291
292##
293# Identifiers by original group
294# Keys are the original group names. Value is a list of LOXI identifiers
295
296identifiers_by_group = {}
297
298## Ordered list of class names
299# This is per-wire-version and is a list of the classes in the order
300# they appear in the file. That is important because of the assumption
301# that data members are defined before they are included in a superclass.
302ordered_classes = {} # Indexed by wire version
303
304## Per class ordered list of member names
305ordered_members = {}
306
307## Ordered list of message classes
308ordered_messages = []
309
310## Ordered list of non message classes
311ordered_non_messages = []
312
313## The objects that need list support
314ordered_list_objects = []
315
316## Stats request/reply are pseudo objects
317ordered_pseudo_objects = []
318
319## Standard order is normally messages followed by non-messages
320standard_class_order = []
321
322## All classes in order, including psuedo classes for which most code
323# is not generated.
324all_class_order = []
325
326## Map from class, wire_version to size of fixed part of class
327base_length = {}
328
Rich Lanef70be942013-07-18 13:33:14 -0700329## Map from class, wire_version to size of variable-offset, fixed length part of class
330extra_length = {
331 ("of_packet_in", 3): 2,
332 ("of_packet_in", 4): 2,
333}
334
Andreas Wundsam53256162013-05-02 14:05:53 -0700335## Boolean indication of variable length, per class, wire_version,
Rich Lanea06d0c32013-03-25 08:52:03 -0700336is_fixed_length = set()
337
338## The global object ID counter
339object_id = 1 # Reserve 0 for root object
340
341## The unified view of all classes. See internal readme.
342unified = {}
343
344## Indicates data members with non-fixed start offsets
345# Indexed by (cls, version, member-name) and value is prev-member-name
346special_offsets = {}
347
348## Define Python variables with integer wire version values
349VERSION_1_0 = 1
350VERSION_1_1 = 2
351VERSION_1_2 = 3
352VERSION_1_3 = 4
353
354# Ignore version for some functions
355VERSION_ANY = -1
356
357## @var supported_wire_protos
358# The wire protocols this version of LoxiGen supports
359supported_wire_protos = set([1, 2, 3, 4])
360version_names = {1:"VERSION_1_0", 2:"VERSION_1_1", 3:"VERSION_1_2",
361 4:"VERSION_1_3"}
362short_version_names = {1:"OF_1_0", 2:"OF_1_1", 3:"OF_1_2", 4:"OF_1_3"}
363param_version_names = {1:"1.0", 2:"1.1", 3:"1.2", 4:"1.3"}
364
365##
366# Maps and ranges related to versioning
367
368# For parameter version indications
369of_param_version_map = {
370 "1.0":VERSION_1_0,
371 "1.1":VERSION_1_1,
372 "1.2":VERSION_1_2,
373 "1.3":VERSION_1_3
374 }
375
376# For parameter version indications
377of_version_map = {
378 "1.0":VERSION_1_0,
379 "1.1":VERSION_1_1,
380 "1.2":VERSION_1_2,
381 "1.3":VERSION_1_3
382 }
383
384# The iteration object that gives the wire versions supported
385of_version_range = [VERSION_1_0, VERSION_1_1, VERSION_1_2, VERSION_1_3]
386of_version_max = VERSION_1_3
387
388
389of_version_name2wire = dict(
390 OF_VERSION_1_0=VERSION_1_0,
391 OF_VERSION_1_1=VERSION_1_1,
392 OF_VERSION_1_2=VERSION_1_2,
393 OF_VERSION_1_3=VERSION_1_3
394 )
395
396of_version_wire2name = {
397 VERSION_1_0:"OF_VERSION_1_0",
398 VERSION_1_1:"OF_VERSION_1_1",
399 VERSION_1_2:"OF_VERSION_1_2",
400 VERSION_1_3:"OF_VERSION_1_3"
401 }
402
403
404################################################################
405#
406# Experimenters, vendors, extensions
407#
Andreas Wundsam53256162013-05-02 14:05:53 -0700408# Although the term "experimenter" is used for identifying
Rich Lanea06d0c32013-03-25 08:52:03 -0700409# external extension definitions, we generally use the term
410# extension when refering to the messages or objects themselves.
411#
412# Conventions:
413#
414# Extension messages should start with of_<experimenter>_
415# Extension actions should start with of_<experimenter>_action_
416# Extension instructions should start with of_<experimenter>_instructions_
417#
418# Currently, the above conventions are not enforced; the mapping
419# is done brute force in type_maps.py
420#
421################################################################
422
423# The map of known experimenters to their experimenter IDs
424experimenter_name_to_id = dict(
425 bsn = 0x005c16c7,
426 nicira = 0x00002320,
427 openflow = 0x000026e1
428 )
429
430def experimenter_name_lookup(experimenter_id):
431 """
432 Map an experimenter ID to its LOXI recognized name string
433 """
434 for name, id in of_g.experimenter_name_to_id.items():
435 if id == experimenter_id:
436 return name
437 return None
438
439################################################################
440#
441# Debug
442#
443################################################################
444
445loxigen_dbg_file = sys.stdout
446loxigen_log_file = sys.stdout