blob: 711125997d05c1cddb9b32b0c8d415e456288c8e [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
35from optparse import OptionParser
36# @fixme Replace with argparse
37
38################################################################
39#
40# Configuration global parameters
41#
42################################################################
43
44##
45# The map from wire protocol to enum identifier generated from input
46# This is built from the version-specific structs file info.
47# @fixme This should go away when the process structs file is updated
48wire_ver_map = {}
49
50##
51# Command line options
52options = {}
53
54##
55# Command line arguments
56args = []
57
58##@var config_default
59# The default configuration dictionary for LOXI code generation
60options_default = {
61 "lang" : "c",
62 "version-list" : "1.0 1.1 1.2 1.3",
63 "install-dir" : "loxi_output",
64}
65
66##
67# The list of wire versions which are to be supported
68target_version_list = []
69
70def lang_normalize(lang):
71 """
Andreas Wundsam53256162013-05-02 14:05:53 -070072 Normalize the representation of the language
Rich Lanea06d0c32013-03-25 08:52:03 -070073 """
74 return lang.lower()
75
76def version_list_normalize(vlist):
77 """
78 Normalize the version list and return as an array
79 """
80 out_list = []
81 # @fixme Map to OF version references
82 if vlist.find(',') > 0:
83 vlist = vlist.split(',')
84 else:
85 vlist = vlist.split()
86 vlist.sort()
87 for ver in vlist:
88 try:
89 out_list.append(of_param_version_map[ver])
90 except KeyError:
91 sys.stderr.write("Bad version input, %s" % str(ver))
92 sys.exit(1)
93
94 return out_list
95
96def process_commandline(default_vals=options_default):
97 """
98 Set up the options dictionary
99
100 @param cfg_dflt The default configuration dictionary
101 @return A pair (options, args) as per parser return
102 """
103 global options
104 global args
105 global target_version_list
106
107 parser = OptionParser(version="%prog 0.1")
108
109 #@todo Add options via dictionary
110 parser.add_option("--list-files", action="store_true", default=False,
111 help="List output files generated")
112 parser.add_option("-l", "--lang", "--language",
113 default=default_vals["lang"],
114 help="Select the target language: c, python")
115 parser.add_option("-i", "--install-dir",
116 default=default_vals["install-dir"],
117 help="Directory to install generated files to (default %s)" % default_vals["install-dir"])
Andreas Wundsam53256162013-05-02 14:05:53 -0700118 parser.add_option("-v", "--version-list",
Rich Lanea06d0c32013-03-25 08:52:03 -0700119 default=default_vals["version-list"],
120 help="Specify the versions to target as 1.0 1.1 etc")
121
122 (options, args) = parser.parse_args()
123
124 options.lang = lang_normalize(options.lang)
125 target_version_list = version_list_normalize(options.version_list)
126 target_version_list.sort()
127 return (options, args)
128
129##
130# The dictionary of config variables related to code
131#
132# @param gen_unified_fns Boolean; Generate top level function definitions for
Andreas Wundsam53256162013-05-02 14:05:53 -0700133# accessors which are independent of the version; the alternative is to only
Rich Lanea06d0c32013-03-25 08:52:03 -0700134# use the function pointers in the class definitions. These functions support
135# better inlining optimizations.
136#
137# @param gen_fn_ptrs Boolean; Generate the functions pointed to by pointer
138# in the class (struct) definitions; the alternative is to only use the
139# unified (use_) functions
140#
141# @param use_obj_id Use object IDs in struct defns CURRENTLY NOT SUPPORTED
Andreas Wundsam53256162013-05-02 14:05:53 -0700142#
Rich Lanea06d0c32013-03-25 08:52:03 -0700143# @param return_base_types For 'get' accessors, return values when possible.
144# Otherwise all values are returned thru a call by variable parameter
145#
146# @param use_static_inlines Generate low level accessors as static inline
147# and put them in header files rather than .c files.
148#
149# @param copy_semantics One of "read", "write" or "grow". This defines the
150# way that buffer references are managed. Currently on "read" is supported.
151#
152# @param encode_typedefs Use object and member IDs (rather than names)
153# when generating the names used for accessor function typedefs
154#
Andreas Wundsam53256162013-05-02 14:05:53 -0700155# @param get_returns One of "error", "value", or "void";
Rich Lanea06d0c32013-03-25 08:52:03 -0700156# CURRENTLY ONLY "error" IS SUPPORTED. "error" means
157# all get operations return an error code. "value" means return a base_type
158# value when possible or void if not. "void" means always return void
159# and use a call-by-variable parameter
160#
161
162# @fixme These are still very C specific and should probably either
163# go into lang_c.py or be swallowed by command line option parsing
164code_gen_config = dict(
165 gen_unified_fns=True,
166# gen_fn_ptrs=True, # WARNING: Haven't tested with this in a while
167 gen_fn_ptrs=False,
168 use_obj_id=False,
169 use_static_inlines=False,
170 copy_semantics="read", # Only read implemented: read, write, grow
171 encoded_typedefs=False,
172 get_returns="error", # Only error implemented; error, value, void
173)
174
175## These members do not get normal accessors
176
Rob Vaterlausb3f49d92013-10-01 17:57:31 -0700177skip_members = ["version", "type", "length", "err_type", "stats_type", "len",
Rich Lanea06d0c32013-03-25 08:52:03 -0700178 "type_len", "actions_len", "_command"]
179
180## Some OpenFlow string length constants
181#
182# These are a few length constants needed for array processing
183ofp_constants = dict(
184 OF_MAX_TABLE_NAME_LEN = 32,
185 OF_MAX_PORT_NAME_LEN = 16,
186 OF_ETH_ALEN = 6,
187 OF_DESC_STR_LEN = 256,
188 OF_SERIAL_NUM_LEN = 32
189)
190
191## List of mixed data types
192#
193# This is a list of data types which require special treatment
194# because the underlying datatype has changed between versions.
195# The main example is port which went from 16 to 32 bits. We
196# define per-version accessors for these types and those are
197# used in place of the normal ones.
198#
199# The wire protocol number is used to identify versions. For now,
200# the value is the name of the type to use for that version
201#
202# This is the map between the external type (like of_port_no_t)
Andreas Wundsam53256162013-05-02 14:05:53 -0700203# which is used by customers of this code and the internal
204# datatypes (like uint16_t) that appear on the wire for a
Rich Lanea06d0c32013-03-25 08:52:03 -0700205# particular version.
206#
207of_mixed_types = dict(
208 of_port_no_t = {
209 1: "uint16_t",
210 2: "uint32_t",
211 3: "uint32_t",
212 4: "uint32_t",
213 "short_name":"port_no"
214 },
215 of_port_desc_t = {
216 1: "of_port_desc_t",
217 2: "of_port_desc_t",
218 3: "of_port_desc_t",
219 4: "of_port_desc_t",
220 "short_name":"port_desc"
221 },
Dan Talaycoc0e802e2013-05-18 23:52:39 -0700222 of_bsn_vport_t = {
223 1: "of_bsn_vport_t",
224 2: "of_bsn_vport_t",
225 3: "of_bsn_vport_t",
226 4: "of_bsn_vport_t",
227 "short_name":"bsn_vport"
228 },
Rich Lanea06d0c32013-03-25 08:52:03 -0700229 of_fm_cmd_t = { # Flow mod command went from u16 to u8
230 1: "uint16_t",
231 2: "uint8_t",
232 3: "uint8_t",
233 4: "uint8_t",
234 "short_name":"fm_cmd"
235 },
236 of_wc_bmap_t = { # Wildcard bitmap
237 1: "uint32_t",
238 2: "uint32_t",
239 3: "uint64_t",
240 4: "uint64_t",
241 "short_name":"wc_bmap"
242 },
243 of_match_bmap_t = { # Match bitmap
244 1: "uint32_t",
245 2: "uint32_t",
246 3: "uint64_t",
247 4: "uint64_t",
248 "short_name":"match_bmap"
249 },
250 of_match_t = { # Match object
251 1: "of_match_v1_t",
252 2: "of_match_v2_t",
253 3: "of_match_v3_t",
254 4: "of_match_v3_t", # Currently uses same match as 1.2 (v3).
255 "short_name":"match"
256 },
257)
258
259## Base data types
260#
261# The basic types; Value is a list: bytes, to_wire, from_wire
262# The accessors deal with endian, alignment and any other host/network
263# considerations. These are common across all versions
264#
265# For get accessors, assume we memcpy from wire buf and then apply ntoh
266# For set accessors, assume we apply hton and then memcpy to wire buf
267#
268# to/from wire functions take a pointer to class and change in place
269of_base_types = dict(
270 char = dict(bytes=1, use_as_rv=1, short_name="char"),
271 uint8_t = dict(bytes=1, use_as_rv=1, short_name="u8"),
272 uint16_t = dict(bytes=2, to_w="u16_hton", from_w="u16_ntoh", use_as_rv=1,
273 short_name="u16"),
274 uint32_t = dict(bytes=4, to_w="u32_hton", from_w="u32_ntoh", use_as_rv=1,
275 short_name="u32"),
276 uint64_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1,
277 short_name="u64"),
278# of_cookie_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1#,
279# short_name="cookie"),
280# of_counter_t = dict(bytes=8, to_w="u64_hton", from_w="u64_ntoh", use_as_rv=1,
281# short_name="counter"),
282 of_mac_addr_t = dict(bytes=6, short_name="mac"),
Andreas Wundsamb566a162013-07-18 19:30:23 -0700283 of_ipv4_t = dict(bytes=4, short_name="ipv4"),
Rich Lanea06d0c32013-03-25 08:52:03 -0700284 of_ipv6_t = dict(bytes=16, short_name="ipv6"),
285 of_port_name_t = dict(bytes=ofp_constants["OF_MAX_PORT_NAME_LEN"],
286 short_name="port_name"),
287 of_table_name_t = dict(bytes=ofp_constants["OF_MAX_TABLE_NAME_LEN"],
288 short_name="tab_name"),
289 of_desc_str_t = dict(bytes=ofp_constants["OF_DESC_STR_LEN"],
290 short_name="desc_str"),
291 of_serial_num_t = dict(bytes=ofp_constants["OF_SERIAL_NUM_LEN"],
292 short_name="ser_num"),
Andreas Wundsam53256162013-05-02 14:05:53 -0700293 of_match_v1_t = dict(bytes=40, to_w="match_v1_hton",
294 from_w="match_v1_ntoh",
Rich Lanea06d0c32013-03-25 08:52:03 -0700295 short_name="match_v1"),
Andreas Wundsam53256162013-05-02 14:05:53 -0700296 of_match_v2_t = dict(bytes=88, to_w="match_v2_hton",
297 from_w="match_v2_ntoh",
Rich Lanea06d0c32013-03-25 08:52:03 -0700298 short_name="match_v2"),
Andreas Wundsam53256162013-05-02 14:05:53 -0700299 of_match_v3_t = dict(bytes=-1, to_w="match_v3_hton",
300 from_w="match_v3_ntoh",
Rich Lanea06d0c32013-03-25 08:52:03 -0700301 short_name="match_v3"),
Andreas Wundsam53256162013-05-02 14:05:53 -0700302# of_match_v4_t = dict(bytes=-1, to_w="match_v4_hton",
303# from_w="match_v4_ntoh",
Rich Lanea06d0c32013-03-25 08:52:03 -0700304# short_name="match_v4"),
Rich Lane3b2fd832013-09-24 13:44:08 -0700305 of_octets_t = dict(bytes=-1, short_name="octets"),
306 of_bitmap_128_t = dict(bytes=16, short_name="bitmap_128"),
Rich Lanea06d0c32013-03-25 08:52:03 -0700307)
308
309of_scalar_types = ["char", "uint8_t", "uint16_t", "uint32_t", "uint64_t",
310 "of_port_no_t", "of_fm_cmd_t", "of_wc_bmap_t",
311 "of_match_bmap_t", "of_port_name_t", "of_table_name_t",
Andreas Wundsam53256162013-05-02 14:05:53 -0700312 "of_desc_str_t", "of_serial_num_t", "of_mac_addr_t",
Rich Lane3b2fd832013-09-24 13:44:08 -0700313 "of_ipv6_t", "of_ipv4_t", "of_bitmap_128_t"]
Rich Lanea06d0c32013-03-25 08:52:03 -0700314
315base_object_members = """\
316 /* The control block for the underlying data buffer */
317 of_wire_object_t wire_object;
318 /* The LOCI type enum value of the object */
319 of_object_id_t object_id;
320
321 /*
322 * Objects need to track their "parent" so that updates to the
323 * object that affect its length can be pushed to the parent.
324 * Treat as private.
325 */
326 of_object_t *parent;
327
328 /*
329 * Not all objects have length and version on the wire so we keep
330 * them here. NOTE: Infrastructure manages length and version.
331 * Treat length as private and version as read only.
332 */
333 int length;
334 of_version_t version;
335
336 /*
337 * Many objects have a length and/or type represented in the wire buffer
338 * These accessors get and set those value when present. Treat as private.
339 */
340 of_wire_length_get_f wire_length_get;
341 of_wire_length_set_f wire_length_set;
342 of_wire_type_get_f wire_type_get;
343 of_wire_type_set_f wire_type_set;
344
345 of_object_track_info_t track_info;
346
347 /*
348 * Metadata available for applications. Ensure 8-byte alignment, but
349 * that buffer is at least as large as requested. This data is not used
350 * or inspected by LOCI.
351 */
352 uint64_t metadata[(OF_OBJECT_METADATA_BYTES + 7) / 8];
353"""
354
Rich Lanec9e111d2013-05-09 16:10:30 -0700355# LOXI intermediate representation
356# This is a hash from wire versions to OFProtocol objects.
357# See loxi_ir.py
358
359ir = {}
Rich Lanea06d0c32013-03-25 08:52:03 -0700360
361##
362# LOXI identifiers
363#
364# Dict indexed by identifier name. Each entry contains the information
365# as a DotDict with the following keys:
366# values: A dict indexed by wire version giving each verion's value or None
367# common: The common value to use for this identifier at the LOXI top level (TBD)
368# all_same: If True, all the values across all versions are the same
369# ofp_name: The original name for the identifier
370# ofp_group: The ofp enumerated type if defined
371
372identifiers = {}
373
374##
375# Identifiers by original group
376# Keys are the original group names. Value is a list of LOXI identifiers
377
378identifiers_by_group = {}
379
380## Ordered list of class names
381# This is per-wire-version and is a list of the classes in the order
382# they appear in the file. That is important because of the assumption
383# that data members are defined before they are included in a superclass.
384ordered_classes = {} # Indexed by wire version
385
386## Per class ordered list of member names
387ordered_members = {}
388
389## Ordered list of message classes
390ordered_messages = []
391
392## Ordered list of non message classes
393ordered_non_messages = []
394
395## The objects that need list support
396ordered_list_objects = []
397
398## Stats request/reply are pseudo objects
399ordered_pseudo_objects = []
400
401## Standard order is normally messages followed by non-messages
402standard_class_order = []
403
404## All classes in order, including psuedo classes for which most code
405# is not generated.
406all_class_order = []
407
408## Map from class, wire_version to size of fixed part of class
409base_length = {}
410
Rich Lanef70be942013-07-18 13:33:14 -0700411## Map from class, wire_version to size of variable-offset, fixed length part of class
412extra_length = {
413 ("of_packet_in", 3): 2,
414 ("of_packet_in", 4): 2,
415}
416
Andreas Wundsam53256162013-05-02 14:05:53 -0700417## Boolean indication of variable length, per class, wire_version,
Rich Lanea06d0c32013-03-25 08:52:03 -0700418is_fixed_length = set()
419
420## The global object ID counter
421object_id = 1 # Reserve 0 for root object
422
423## The unified view of all classes. See internal readme.
424unified = {}
425
426## Indicates data members with non-fixed start offsets
427# Indexed by (cls, version, member-name) and value is prev-member-name
428special_offsets = {}
429
430## Define Python variables with integer wire version values
431VERSION_1_0 = 1
432VERSION_1_1 = 2
433VERSION_1_2 = 3
434VERSION_1_3 = 4
435
436# Ignore version for some functions
437VERSION_ANY = -1
438
439## @var supported_wire_protos
440# The wire protocols this version of LoxiGen supports
441supported_wire_protos = set([1, 2, 3, 4])
442version_names = {1:"VERSION_1_0", 2:"VERSION_1_1", 3:"VERSION_1_2",
443 4:"VERSION_1_3"}
444short_version_names = {1:"OF_1_0", 2:"OF_1_1", 3:"OF_1_2", 4:"OF_1_3"}
445param_version_names = {1:"1.0", 2:"1.1", 3:"1.2", 4:"1.3"}
446
447##
448# Maps and ranges related to versioning
449
450# For parameter version indications
451of_param_version_map = {
452 "1.0":VERSION_1_0,
453 "1.1":VERSION_1_1,
454 "1.2":VERSION_1_2,
455 "1.3":VERSION_1_3
456 }
457
458# For parameter version indications
459of_version_map = {
460 "1.0":VERSION_1_0,
461 "1.1":VERSION_1_1,
462 "1.2":VERSION_1_2,
463 "1.3":VERSION_1_3
464 }
465
466# The iteration object that gives the wire versions supported
467of_version_range = [VERSION_1_0, VERSION_1_1, VERSION_1_2, VERSION_1_3]
468of_version_max = VERSION_1_3
469
470
471of_version_name2wire = dict(
472 OF_VERSION_1_0=VERSION_1_0,
473 OF_VERSION_1_1=VERSION_1_1,
474 OF_VERSION_1_2=VERSION_1_2,
475 OF_VERSION_1_3=VERSION_1_3
476 )
477
478of_version_wire2name = {
479 VERSION_1_0:"OF_VERSION_1_0",
480 VERSION_1_1:"OF_VERSION_1_1",
481 VERSION_1_2:"OF_VERSION_1_2",
482 VERSION_1_3:"OF_VERSION_1_3"
483 }
484
485
486################################################################
487#
488# Experimenters, vendors, extensions
489#
Andreas Wundsam53256162013-05-02 14:05:53 -0700490# Although the term "experimenter" is used for identifying
Rich Lanea06d0c32013-03-25 08:52:03 -0700491# external extension definitions, we generally use the term
492# extension when refering to the messages or objects themselves.
493#
494# Conventions:
495#
496# Extension messages should start with of_<experimenter>_
497# Extension actions should start with of_<experimenter>_action_
498# Extension instructions should start with of_<experimenter>_instructions_
499#
500# Currently, the above conventions are not enforced; the mapping
501# is done brute force in type_maps.py
502#
503################################################################
504
505# The map of known experimenters to their experimenter IDs
506experimenter_name_to_id = dict(
507 bsn = 0x005c16c7,
508 nicira = 0x00002320,
509 openflow = 0x000026e1
510 )
511
512def experimenter_name_lookup(experimenter_id):
513 """
514 Map an experimenter ID to its LOXI recognized name string
515 """
516 for name, id in of_g.experimenter_name_to_id.items():
517 if id == experimenter_id:
518 return name
519 return None
520
521################################################################
522#
523# Debug
524#
525################################################################
526
527loxigen_dbg_file = sys.stdout
528loxigen_log_file = sys.stdout