blob: df93c2b5c7990811f361c725f4f9391e41953c9f [file] [log] [blame]
Andreas Wundsam542a13c2013-11-15 13:28:55 -08001# 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 Utilities involving LOXI naming conventions
30
31Utility functions for OpenFlow class generation
32
33These may need to be sorted out into language specific functions
34"""
35
36import sys
37import c_gen.of_g_legacy as of_g
38import tenjin
39from generic_utils import find, memoize
40
41def class_signature(members):
42 """
43 Generate a signature string for a class in canonical form
44
45 @param cls The class whose signature is to be generated
46 """
47 return ";".join([",".join([x["m_type"], x["name"], str(x["offset"])])
48 for x in members])
49
50def type_dec_to_count_base(m_type):
51 """
52 Resolve a type declaration like uint8_t[4] to a count (4) and base_type
53 (uint8_t)
54
55 @param m_type The string type declaration to process
56 """
57 count = 1
58 chk_ar = m_type.split('[')
59 if len(chk_ar) > 1:
60 count_str = chk_ar[1].split(']')[0]
61 if count_str in of_g.ofp_constants:
62 count = of_g.ofp_constants[count_str]
63 else:
64 count = int(count_str)
65 base_type = chk_ar[0]
66 else:
67 base_type = m_type
68 return count, base_type
69
70##
71# Class types:
72#
73# Virtual
74# A virtual class is one which does not have an explicit wire
75# representation. For example, an inheritance super class
76# or a list type.
77#
78# List
79# A list of objects of some other type
80#
81# TLV16
82# The wire represenation starts with 16-bit type and length fields
83#
84# OXM
85# An extensible match object
86#
87# Message
88# A top level OpenFlow message
89#
90#
91
92def class_is_message(cls):
93 """
94 Return True if cls is a message object based on info in unified
95 """
96 return "xid" in of_g.unified[cls]["union"] and cls != "of_header"
97
98def class_is_tlv16(cls):
99 """
100 Return True if cls_name is an object which uses uint16 for type and length
101 """
102 if cls.find("of_action") == 0: # Includes of_action_id classes
103 return True
104 if cls.find("of_instruction") == 0:
105 return True
106 if cls.find("of_queue_prop") == 0:
107 return True
108 if cls.find("of_table_feature_prop") == 0:
109 return True
110 # *sigh*
111 if cls.find("of_meter_band_stats") == 0: # NOT A TLV
112 return False
113 if cls.find("of_meter_band") == 0:
114 return True
115 if cls.find("of_hello_elem") == 0:
116 return True
117 if cls == "of_match_v3":
118 return True
119 if cls == "of_match_v4":
120 return True
Rich Lane713d9282013-12-30 15:21:35 -0800121 if cls.find("of_bsn_tlv") == 0:
122 return True
Andreas Wundsam542a13c2013-11-15 13:28:55 -0800123 return False
124
125def class_is_u16_len(cls):
126 """
127 Return True if cls_name is an object which uses initial uint16 length
128 """
129 return cls in ["of_group_desc_stats_entry", "of_group_stats_entry",
xinwuf08ef682013-12-05 18:29:20 -0800130 "of_flow_stats_entry", "of_bucket", "of_table_features",
Rich Lane713d9282013-12-30 15:21:35 -0800131 "of_bsn_port_counter_stats_entry", "of_bsn_vlan_counter_stats_entry",
132 "of_bsn_gentable_entry_desc_stats_entry", "of_bsn_gentable_entry_stats_entry",
133 "of_bsn_gentable_desc_stats_entry"]
Andreas Wundsam542a13c2013-11-15 13:28:55 -0800134
135def class_is_oxm(cls):
136 """
137 Return True if cls_name is an OXM object
138 """
139 if cls.find("of_oxm") == 0:
140 return True
141 return False
142
143def class_is_action(cls):
144 """
145 Return True if cls_name is an action object
146
147 Note that action_id is not an action object, though it has
148 the same header. It looks like an action header, but the type
149 is used to identify a kind of action, it does not indicate the
150 type of the object following.
151 """
152 if cls.find("of_action_id") == 0:
153 return False
154 if cls.find("of_action") == 0:
155 return True
156
157 # For each vendor, check for vendor specific action
158 for exp in of_g.experimenter_name_to_id:
159 if cls.find("of_action" + exp) == 0:
160 return True
161
162 return False
163
164def class_is_action_id(cls):
165 """
166 Return True if cls_name is an action object
167
168 Note that action_id is not an action object, though it has
169 the same header. It looks like an action header, but the type
170 is used to identify a kind of action, it does not indicate the
171 type of the object following.
172 """
173 if cls.find("of_action_id") == 0:
174 return True
175
176 # For each vendor, check for vendor specific action
177 for exp in of_g.experimenter_name_to_id:
178 if cls.find("of_action_id_" + exp) == 0:
179 return True
180
181 return False
182
183def class_is_instruction(cls):
184 """
185 Return True if cls_name is an instruction object
186 """
Rich Lane4def6972013-12-09 17:44:43 -0800187 if cls.find("of_instruction_id") == 0:
188 return False
Andreas Wundsam542a13c2013-11-15 13:28:55 -0800189 if cls.find("of_instruction") == 0:
190 return True
191
192 # For each vendor, check for vendor specific action
193 for exp in of_g.experimenter_name_to_id:
Jonathan Stoutd2752282014-03-03 17:21:00 -0500194 if cls.find("of_instruction" + exp) == 0:
Andreas Wundsam542a13c2013-11-15 13:28:55 -0800195 return True
196
197 return False
198
Jonathan Stout83cedcc2014-03-03 16:38:00 -0500199def class_is_instruction_id(cls):
200 """
201 Return True if cls_name is an action object
202
203 Note that instruction_id is not an instruction object, though it has
204 the same header. It looks like an instruction header, but the type
205 is used to identify a kind of instruction, it does not indicate the
206 type of the object following.
207 """
208 if cls.find("of_instruction_id") == 0:
209 return True
210
211 # For each vendor, check for vendor specific action
212 for exp in of_g.experimenter_name_to_id:
213 if cls.find("of_instruction_id_" + exp) == 0:
214 return True
215
216 return False
217
Andreas Wundsam542a13c2013-11-15 13:28:55 -0800218def class_is_meter_band(cls):
219 """
220 Return True if cls_name is an instruction object
221 """
222 # meter_band_stats is not a member of meter_band class hierarchy
223 if cls.find("of_meter_band_stats") == 0:
224 return False
225 if cls.find("of_meter_band") == 0:
226 return True
227 return False
228
229def class_is_hello_elem(cls):
230 """
231 Return True if cls_name is an instruction object
232 """
233 if cls.find("of_hello_elem") == 0:
234 return True
235 return False
236
237def class_is_queue_prop(cls):
238 """
239 Return True if cls_name is a queue_prop object
240 """
241 if cls.find("of_queue_prop") == 0:
242 return True
243
244 # For each vendor, check for vendor specific action
245 for exp in of_g.experimenter_name_to_id:
246 if cls.find("of_queue_prop_" + exp) == 0:
247 return True
248
249 return False
250
251def class_is_table_feature_prop(cls):
252 """
253 Return True if cls_name is a queue_prop object
254 """
255 if cls.find("of_table_feature_prop") == 0:
256 return True
257 return False
258
259def class_is_stats_message(cls):
260 """
261 Return True if cls_name is a message object based on info in unified
262 """
263
264 return "stats_type" in of_g.unified[cls]["union"]
265
266def class_is_list(cls):
267 """
268 Return True if cls_name is a list object
269 """
270 return (cls.find("of_list_") == 0)
271
Rich Lane713d9282013-12-30 15:21:35 -0800272def class_is_bsn_tlv(cls):
273 """
274 Return True if cls_name is a BSN TLV object
275 """
276 if cls.find("of_bsn_tlv") == 0:
277 return True
278 return False
279
Andreas Wundsam542a13c2013-11-15 13:28:55 -0800280def type_is_of_object(m_type):
281 """
282 Return True if m_type is an OF object type
283 """
284 # Remove _t from the type id and see if key for unified class
285 if m_type[-2:] == "_t":
286 m_type = m_type[:-2]
287 return m_type in of_g.unified
288
289def list_to_entry_type(cls):
290 """
291 Return the entry type for a list
292 """
293 slen = len("of_list_")
294 return "of_" + cls[slen:]
295
296def type_to_short_name(m_type):
297 if m_type in of_g.of_base_types:
298 tname = of_g.of_base_types[m_type]["short_name"]
299 elif m_type in of_g.of_mixed_types:
300 tname = of_g.of_mixed_types[m_type]["short_name"]
301 else:
302 tname = "unknown"
303 return tname
304
305def type_to_name_type(cls, member_name):
306 """
307 Generate the root name of a member for accessor functions, etc
308 @param cls The class name
309 @param member_name The member name
310 """
311 members = of_g.unified[cls]["union"]
312 if not member_name in members:
313 debug("Error: %s is not in class %s for acc_name defn" %
314 (member_name, cls))
315 os.exit()
316
317 mem = members[member_name]
318 m_type = mem["m_type"]
319 id = mem["memid"]
320 tname = type_to_short_name(m_type)
321
322 return "o%d_m%d_%s" % (of_g.unified[cls]["object_id"], id, tname)
323
324
325def member_to_index(m_name, members):
326 """
327 Given a member name, return the index in the members dict
328 @param m_name The name of the data member to search for
329 @param members The dict of members
330 @return Index if found, -1 not found
331
332 Note we could generate an index when processing the original input
333 """
334 count = 0
335 for d in members:
336 if d["name"] == m_name:
337 return count
338 count += 1
339 return -1
340
341def member_base_type(cls, m_name):
342 """
343 Map a member to its of_ type
344 @param cls The class name
345 @param m_name The name of the member being gotten
346 @return The of_ type of the member
347 """
348 rv = of_g.unified[cls]["union"][m_name]["m_type"]
349 if rv[-2:] == "_t":
350 return rv
351 return rv + "_t"
352
353def member_type_is_octets(cls, m_name):
354 return member_base_type(cls, m_name) == "of_octets_t"
355
Andreas Wundsam542a13c2013-11-15 13:28:55 -0800356def h_file_to_define(name):
357 """
358 Convert a .h file name to the define used for the header
359 """
360 h_name = name[:-2].upper()
361 h_name = "_" + h_name + "_H_"
362 return h_name
363
364def type_to_cof_type(m_type):
365 if m_type in of_g.of_base_types:
366 if "cof_type" in of_g.of_base_types[m_type]:
367 return of_g.of_base_types[m_type]["cof_type"]
368 return m_type
369
370
371def member_is_scalar(cls, m_name):
372 return of_g.unified[cls]["union"][m_name]["m_type"] in of_g.of_scalar_types
373
374def type_is_scalar(m_type):
375 return m_type in of_g.of_scalar_types
376
377def skip_member_name(name):
378 return name.find("pad") == 0 or name in of_g.skip_members
379
380def enum_name(cls):
381 """
382 Return the name used for an enum identifier for the given class
383 @param cls The class name
384 """
385 return cls.upper()
386
387def class_in_version(cls, ver):
388 """
389 Return boolean indicating if cls is defined for wire version ver
390 """
391
392 return (cls, ver) in of_g.base_length
393
394def instance_to_class(instance, parent):
395 """
396 Return the name of the class for an instance of inheritance type parent
397 """
398 return parent + "_" + instance
399
400def sub_class_to_var_name(cls):
401 """
402 Given a subclass name like of_action_output, generate the
403 name of a variable like 'output'
404 @param cls The class name
405 """
406 pass
407
408def class_is_var_len(cls, version):
409 # Match is special case. Only version 1.2 (wire version 3) is var
410 if cls == "of_match":
411 return version == 3
412
413 return not (cls, version) in of_g.is_fixed_length
414
415def base_type_to_length(base_type, version):
416 if base_type + "_t" in of_g.of_base_types:
417 inst_len = of_g.of_base_types[base_type + "_t"]["bytes"]
418 else:
419 inst_len = of_g.base_length[(base_type, version)]
420
421def version_to_name(version):
422 """
423 Convert an integer version to the C macro name
424 """
425 return "OF_" + of_g.version_names[version]
426
427##
428# Is class a flow modify of some sort?
429
430def cls_is_flow_mod(cls):
431 return cls in ["of_flow_mod", "of_flow_modify", "of_flow_add", "of_flow_delete",
432 "of_flow_modify_strict", "of_flow_delete_strict"]
433
434
435def all_member_types_get(cls, version):
436 """
437 Get the members and list of types for members of a given class
438 @param cls The class name to process
439 @param version The version for the class
440 """
441 member_types = []
442
443 if not version in of_g.unified[cls]:
444 return ([], [])
445
446 if "use_version" in of_g.unified[cls][version]:
447 v = of_g.unified[cls][version]["use_version"]
448 members = of_g.unified[cls][v]["members"]
449 else:
450 members = of_g.unified[cls][version]["members"]
451 # Accumulate variables that are supported
452 for member in members:
453 m_type = member["m_type"]
454 m_name = member["name"]
455 if skip_member_name(m_name):
456 continue
457 if not m_type in member_types:
458 member_types.append(m_type)
459
460 return (members, member_types)
461
462def list_name_extract(list_type):
463 """
464 Return the base name for a list object of the given type
465 @param list_type The type of the list as appears in the input,
466 for example list(of_port_desc_t).
467 @return A pair, (list-name, base-type) where list-name is the
468 base name for the list, for example of_list_port_desc, and base-type
469 is the type of list elements like of_port_desc_t
470 """
471 base_type = list_type[5:-1]
472 list_name = base_type
473 if list_name.find("of_") == 0:
474 list_name = list_name[3:]
475 if list_name[-2:] == "_t":
476 list_name = list_name[:-2]
477 list_name = "of_list_" + list_name
478 return (list_name, base_type)
479
480def version_to_name(version):
481 """
482 Convert an integer version to the C macro name
483 """
484 return "OF_" + of_g.version_names[version]
485
486def gen_c_copy_license(out):
487 """
488 Generate the top comments for copyright and license
489 """
490 import c_gen.util
491 c_gen.util.render_template(out, '_copyright.c')
492
493def accessor_returns_error(a_type, m_type):
494 is_var_len = (not type_is_scalar(m_type)) and \
495 [x for x in of_g.of_version_range if class_is_var_len(m_type[:-2], x)] != []
496 if a_type == "set" and is_var_len:
497 return True
498 elif m_type == "of_match_t":
499 return True
500 else:
501 return False
502
503def render_template(out, name, path, context, prefix = None):
504 """
505 Render a template using tenjin.
506 out: a file-like object
507 name: name of the template
508 path: array of directories to search for the template
509 context: dictionary of variables to pass to the template
510 prefix: optional prefix to use for embedding (for other languages than python)
511 """
512 pp = [ tenjin.PrefixedLinePreprocessor(prefix=prefix) if prefix else tenjin.PrefixedLinePreprocessor() ] # support "::" syntax
513 template_globals = { "to_str": str, "escape": str } # disable HTML escaping
514 engine = TemplateEngine(path=path, pp=pp)
515 out.write(engine.render(name, context, template_globals))
516
517def render_static(out, name, path):
518 """
519 Write out a static template.
520 out: a file-like object
521 name: name of the template
522 path: array of directories to search for the template
523 """
524 # Reuse the tenjin logic for finding the template
525 template_filename = tenjin.FileSystemLoader().find(name, path)
526 if not template_filename:
527 raise ValueError("template %s not found" % name)
528 with open(template_filename) as infile:
529 out.write(infile.read())