blob: 64794cf6e610fecdc75e41e604d4e3053d76ae3f [file] [log] [blame]
Yotam Harcholc742e202013-08-15 12:16:24 -07001import errno
Andreas Wundsam27303462013-07-16 12:52:35 -07002import os
Andreas Wundsam27303462013-07-16 12:52:35 -07003import re
4import subprocess
5import time
6
Andreas Wundsamc8912c12013-11-15 13:44:48 -08007import loxi_globals
Andreas Wundsam7cfeac32013-09-17 13:53:48 -07008from generic_utils import memoize
9import loxi_utils.loxi_utils as loxi_utils
Andreas Wundsam7cfeac32013-09-17 13:53:48 -070010
Andreas Wundsam2be7da52013-08-22 07:34:25 -070011def erase_type_annotation(class_name):
12 m=re.match(r'(.*)<.*>', class_name)
13 if m:
14 return m.group(1)
15 else:
16 return class_name
17
Andreas Wundsam27303462013-07-16 12:52:35 -070018def name_c_to_camel(name):
19 """ 'of_stats_reply' -> 'ofStatsReply' """
20 name = re.sub(r'^_','', name)
21 tokens = name.split('_')
22 for i in range(1, len(tokens)):
23 tokens[i] = tokens[i].title()
24 return "".join(tokens)
25
26def name_c_to_caps_camel(name):
27 """ 'of_stats_reply' to 'OFStatsReply' """
28 camel = name_c_to_camel(name.title())
Andreas Wundsam7cfeac32013-09-17 13:53:48 -070029 if camel.startswith('Ofp'):
30 return camel.replace('Ofp','OF',1)
31 elif camel.startswith('Of'):
Andreas Wundsam27303462013-07-16 12:52:35 -070032 return camel.replace('Of','OF',1)
33 else:
34 return camel
35
Rob Vaterlausfeee3712013-09-30 11:24:19 -070036java_primitive_types = set("boolean byte char short int long".split(" "))
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070037
38### info table about java primitive types, for casting literals in the source code
39# { name : (signed?, length_in_bits) }
Andreas Wundsam46d230f2013-08-02 22:24:06 -070040java_primitives_info = {
Andreas Wundsam438a9c32013-10-07 16:18:52 -070041 'boolean' : (False, 8, False),
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070042 'byte' : (True, 8, True),
43 'char' : (False, 16, True),
44 'short' : (True, 16, True),
45 'int' : (True, 32, False),
46 'long' : (True, 64, False),
Andreas Wundsam46d230f2013-08-02 22:24:06 -070047}
48
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070049def format_primitive_literal(t, value):
50 """ Format a primitive numeric literal for inclusion in the
51 java source code. Takes care of casting the literal
Rob Vaterlausfeee3712013-09-30 11:24:19 -070052 appropriately for correct representation despite Java's
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070053 signed-craziness
54 """
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070055 signed, bits, cast_needed = java_primitives_info[t]
Andreas Wundsamb1324402014-10-23 20:14:41 -070056 if t == 'boolean':
57 return "true" if bool(value) and value not in("False", "false") else "false"
58
Andreas Wundsam46d230f2013-08-02 22:24:06 -070059 max = (1 << bits)-1
60 if value > max:
Andreas Wundsamb1324402014-10-23 20:14:41 -070061 raise Exception("Value %s to large for type %s" % (value, t))
Andreas Wundsam46d230f2013-08-02 22:24:06 -070062
63 if signed:
64 max_pos = (1 << (bits-1)) - 1
65
66 if value > max_pos:
67 if t == "long":
68 return str((1 << bits) - value)
69 else:
70 return "(%s) 0x%x" % (t, value)
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070071 return "%s0x%x%s" % ("(%s) " % t if cast_needed else "", value, "L" if t=="long" else "")
72
Yotam Harchold7b84202013-07-26 16:08:10 -070073
Andreas Wundsame916d6f2013-07-30 11:33:58 -070074ANY = 0xFFFFFFFFFFFFFFFF
Yotam Harchold7b84202013-07-26 16:08:10 -070075
76class VersionOp:
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070077 def __init__(self, version=ANY, read=None, write=None, default=None, funnel=None):
Yotam Harchold7b84202013-07-26 16:08:10 -070078 self.version = version
79 self.read = read
80 self.write = write
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -070081 self.default = default
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070082 self.funnel = funnel
Andreas Wundsame916d6f2013-07-30 11:33:58 -070083
Yotam Harchold7b84202013-07-26 16:08:10 -070084 def __str__(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070085 return "[Version: %d, Read: '%s', Write: '%s', Default: '%s', Funnel: '%s' ]" % (self.version, self.read, self.write, self.default, self.funnel )
Yotam Harchold7b84202013-07-26 16:08:10 -070086
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070087### FIXME: This class should really be cleaned up
Andreas Wundsam27303462013-07-16 12:52:35 -070088class JType(object):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070089 """ Wrapper class to hold C to Java type conversion information. JTypes can have a 'public'
90 and or 'private' java type associated with them and can define how those types can be
91 read from and written to ChannelBuffers.
92
93 """
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -070094 def __init__(self, pub_type, priv_type=None):
Andreas Wundsam27303462013-07-16 12:52:35 -070095 self.pub_type = pub_type # the type we expose externally, e.g. 'U8'
96 if priv_type is None:
97 priv_type = pub_type
98 self.priv_type = priv_type # the internal storage type
Yotam Harchold7b84202013-07-26 16:08:10 -070099 self.ops = {}
Yotam Harchold7b84202013-07-26 16:08:10 -0700100
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700101 def set_priv_type(self, priv_type):
102 self.priv_type = priv_type
103 return self
104
105 def op(self, version=ANY, read=None, write=None, default=None, funnel=None, pub_type=ANY):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700106 """
107 define operations to be performed for reading and writing this type
108 (when read_op, write_op is called). The operations 'read' and 'write'
109 can either be strings ($name, and $version and $length will be replaced),
110 or callables (name, version and length) will be passed.
111
112 @param version int OF version to define operation for, or ANY for all
113 @param pub_type boolean whether to define operations for the public type (True), the
114 private type(False) or both (ALL)
115 @param read read expression (either string or callable)s
116 @param write write expression (either string or callable)
117 """
118
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700119 pub_types = [ pub_type ] if pub_type is not ANY else [ False, True ]
120 for pub_type in pub_types:
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700121 self.ops[(version, pub_type)] = VersionOp(version, read, write, default, funnel)
Yotam Harchold7b84202013-07-26 16:08:10 -0700122 return self
Andreas Wundsam27303462013-07-16 12:52:35 -0700123
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700124 def format_value(self, value, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700125 # Format a constant value of this type, for inclusion in the java source code
126 # For primitive types, takes care of casting the value appropriately, to
127 # cope with java's signedness limitation
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700128 t = self.pub_type if pub_type else self.priv_type
129 if t in java_primitive_types:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700130 return format_primitive_literal(t, value)
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700131 else:
132 return value
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700133
Andreas Wundsam27303462013-07-16 12:52:35 -0700134 @property
135 def public_type(self):
136 """ return the public type """
137 return self.pub_type
138
139 def priv(self):
140 """ return the public type """
141 return self.priv_type
142
143 def has_priv(self):
144 """ Is the private type different from the public one?"""
145 return self.pub_type != self.priv_type
146
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700147 def get_op(self, op_type, version, pub_type, default_value, arguments):
148 ver = ANY if version is None else version.int_version
149
150 if not "version" in arguments:
Andreas Wundsamc8912c12013-11-15 13:44:48 -0800151 arguments["version"] = version.dotless_version
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700152
153 def lookup(ver, pub_type):
154 if (ver, pub_type) in self.ops:
155 return getattr(self.ops[(ver, pub_type)], op_type)
156 else:
157 return None
158
159 _op = lookup(ver, pub_type) or lookup(ANY, pub_type) or default_value
160 if callable(_op):
161 return _op(**arguments)
162 else:
163 return reduce(lambda a,repl: a.replace("$%s" % repl[0], str(repl[1])), arguments.items(), _op)
164
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700165 def read_op(self, version=None, length=None, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700166 """ return a Java stanza that reads a value of this JType from ChannelBuffer bb.
167 @param version int - OF wire version to generate expression for
168 @param pub_type boolean use this JTypes 'public' (True), or private (False) representation
169 @param length string, for operations that need it (e.g., read a list of unknown length)
170 Java expression evaluating to the byte length to be read. Defaults to the remainig
171 length of the message.
172 @return string containing generated Java expression.
173 """
Andreas Wundsam27303462013-07-16 12:52:35 -0700174 if length is None:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700175 # assumes that
176 # (1) length of the message has been read to 'length'
177 # (2) readerIndex at the start of the message has been stored in 'start'
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700178 length = "length - (bb.readerIndex() - start)"
Andreas Wundsam27303462013-07-16 12:52:35 -0700179
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700180 return self.get_op("read", version, pub_type,
181 default_value='ChannelUtilsVer$version.read%s(bb)' % self.pub_type,
182 arguments=dict(length=length)
183 )
Andreas Wundsam27303462013-07-16 12:52:35 -0700184
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700185 def write_op(self, version=None, name=None, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700186 """ return a Java stanza that writes a value of this JType contained in Java expression
187 'name' to ChannelBuffer bb.
188 @param name string containing Java expression that evaluations to the value to be written
189 @param version int - OF wire version to generate expression for
190 @param pub_type boolean use this JTypes 'public' (True), or private (False) representation
191 @return string containing generated Java expression.
192 """
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700193
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700194 return self.get_op("write", version, pub_type,
195 default_value='ChannelUtilsVer$version.write%s(bb, $name)' % self.pub_type,
196 arguments=dict(name=name)
197 )
198
Andreas Wundsam27303462013-07-16 12:52:35 -0700199
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700200 def default_op(self, version=None, pub_type=True):
201 """ return a Java stanza that returns a default value of this JType.
202 @param version JavaOFVersion
203 @return string containing generated Java expression.
204 """
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700205 return self.get_op("default", version, pub_type,
206 arguments = dict(),
207 default_value = self.format_value(0) if self.is_primitive else "null"
208 )
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700209
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700210 def skip_op(self, version=None, length=None):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700211 """ return a java stanza that skips an instance of JType in the input ChannelBuffer 'bb'.
212 This is used in the Reader implementations for virtual classes (because after the
213 discriminator field, the concrete Reader instance will re-read all the fields)
214 Currently just delegates to read_op + throws away the result."""
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700215 return self.read_op(version, length)
216
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700217 def funnel_op(self, version=None, name=None, pub_type=True):
218 t = self.pub_type if pub_type else self.priv_type
219 return self.get_op("funnel", version, pub_type,
220 arguments = dict(name=name),
221 default_value = '$name.putTo(sink)' if not self._is_primitive(pub_type) else "sink.put{}($name)".format(t[0].upper() + t[1:])
222 )
223
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700224 @property
225 def is_primitive(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700226 return self._is_primitive()
227
228 def _is_primitive(self, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700229 """ return true if the pub_type is a java primitive type (and thus needs
230 special treatment, because it doesn't have methods)"""
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700231 t = self.pub_type if pub_type else self.priv_type
232 return t in java_primitive_types
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700233
234 @property
235 def is_array(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700236 return self._is_array()
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700237
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700238 def _is_array(self, pub_type=True):
239 t = self.pub_type if pub_type else self.priv_type
240 return t.endswith("[]")
241
242# Create a default mapping for a list type. Type defauls to List<${java_mapping_of_name}>
243def gen_enum_jtype(java_name, is_bitmask=False):
244 if is_bitmask:
245 java_type = "Set<{}>".format(java_name)
246 default_value = "ImmutableSet.<{}>of()".format(java_name)
247 else:
248 java_type = java_name
249 default_value = "null"
250
251 serializer = "{}SerializerVer$version".format(java_name)
252
253 return JType(java_type)\
254 .op(read="{}.readFrom(bb)".format(serializer),
255 write="{}.writeTo(bb, $name)".format(serializer),
256 default=default_value,
257 funnel="{}.putTo($name, sink)".format(serializer)
258 )
259
260def gen_list_jtype(java_base_name):
261 # read op assumes the class has a public final static field READER that implements
262 # OFMessageReader<$class> i.e., can deserialize an instance of class from a ChannelBuffer
263 # write op assumes class implements Writeable
264 return JType("List<{}>".format(java_base_name)) \
265 .op(
266 read= 'ChannelUtils.readList(bb, $length, {}Ver$version.READER)'.format(java_base_name), \
267 write='ChannelUtils.writeList(bb, $name)',
268 default="ImmutableList.<{}>of()".format(java_base_name),
269 funnel='FunnelUtils.putList($name, sink)'
270 )
271
272def gen_fixed_length_string_jtype(length):
273 return JType('String').op(
274 read='ChannelUtils.readFixedLengthString(bb, {})'.format(length),
275 write='ChannelUtils.writeFixedLengthString(bb, $name, {})'.format(length),
276 default='""',
277 funnel='sink.putUnencodedChars($name)'
278 )
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700279
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700280##### Predefined JType mappings
281# FIXME: This list needs to be pruned / cleaned up. Most of these are schematic.
282
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700283u8 = JType('short', 'byte') \
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700284 .op(read='U8.f(bb.readByte())', write='bb.writeByte(U8.t($name))', pub_type=True) \
285 .op(read='bb.readByte()', write='bb.writeByte($name)', pub_type=False)
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700286u8_list = JType('List<U8>') \
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700287 .op(read='ChannelUtils.readList(bb, $length, U8.READER)',
288 write='ChannelUtils.writeList(bb, $name)',
289 default='ImmutableList.<U8>of()',
290 funnel='FunnelUtils.putList($name, sink)'
291 )
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700292u16 = JType('int', 'short') \
293 .op(read='U16.f(bb.readShort())', write='bb.writeShort(U16.t($name))', pub_type=True) \
294 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
295u32 = JType('long', 'int') \
296 .op(read='U32.f(bb.readInt())', write='bb.writeInt(U32.t($name))', pub_type=True) \
297 .op(read='bb.readInt()', write='bb.writeInt($name)', pub_type=False)
298u32_list = JType('List<U32>', 'int[]') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700299 .op(
300 read='ChannelUtils.readList(bb, $length, U32.READER)',
301 write='ChannelUtils.writeList(bb, $name)',
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700302 default="ImmutableList.<U32>of()",
303 funnel="FunnelUtils.putList($name, sink)")
xinwuf08ef682013-12-05 18:29:20 -0800304u64_list = JType('List<U64>', 'int[]') \
305 .op(
306 read='ChannelUtils.readList(bb, $length, U64.READER)',
307 write='ChannelUtils.writeList(bb, $name)',
308 default="ImmutableList.<U64>of()",
309 funnel="FunnelUtils.putList($name, sink)")
Yotam Harchol5804f772013-08-21 17:35:31 -0700310u8obj = JType('U8', 'U8') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700311 .op(read='U8.of(bb.readByte())', write='bb.writeByte($name.getRaw())', default="U8.ZERO")
Rich Lane9b178072014-05-19 15:31:55 -0700312u16obj = JType('U16', 'U16') \
Rich Lane99f1b432014-05-22 11:15:15 -0700313 .op(read='U16.of(bb.readShort())', write='bb.writeShort($name.getRaw())', default="U16.ZERO")
Yotam Harchol5804f772013-08-21 17:35:31 -0700314u32obj = JType('U32', 'U32') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700315 .op(read='U32.of(bb.readInt())', write='bb.writeInt($name.getRaw())', default="U32.ZERO")
Sovietaced3d42dbe2014-01-23 15:55:48 -0800316u64 = JType('U64', 'long') \
317 .op(read='U64.ofRaw(bb.readLong())', write='bb.writeLong($name.getValue())', default="U64.ZERO", pub_type=True) \
318 .op(read='bb.readLong()', write='bb.writeLong($name)', pub_type=False)
Andreas Wundsam7c15b172014-04-24 19:02:40 -0700319u128 = JType("U128") \
320 .op(read='U128.read16Bytes(bb)',
321 write='$name.write16Bytes(bb)',
322 default='U128.ZERO')
Yotam Harchold7b84202013-07-26 16:08:10 -0700323of_port = JType("OFPort") \
Andreas Wundsamad499c92013-09-28 18:56:49 -0700324 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ANY") \
325 .op(version=ANY, read="OFPort.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFPort.ANY")
Andreas Wundsame962d372013-10-02 18:15:58 -0700326# the same OFPort, but with a default value of ZERO, only for OF10 match
327of_port_match_v1 = JType("OFPort") \
328 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ZERO")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700329actions_list = gen_list_jtype("OFAction")
330instructions_list = gen_list_jtype("OFInstruction")
331buckets_list = gen_list_jtype("OFBucket")
332port_desc_list = gen_list_jtype("OFPortDesc")
333packet_queue_list = gen_list_jtype("OFPacketQueue")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700334port_desc = JType('OFPortDesc') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700335 .op(read='OFPortDescVer$version.READER.readFrom(bb)', \
Yotam Harchold7b84202013-07-26 16:08:10 -0700336 write='$name.writeTo(bb)')
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700337octets = JType('byte[]')\
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700338 .op(read='ChannelUtils.readBytes(bb, $length)', \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700339 write='bb.writeBytes($name)', \
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700340 default="new byte[0]",
341 funnel="sink.putBytes($name)"
342 );
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700343of_match = JType('Match') \
Andreas Wundsam951ada32013-08-01 22:05:38 -0700344 .op(read='ChannelUtilsVer$version.readOFMatch(bb)', \
Andreas Wundsame962d372013-10-02 18:15:58 -0700345 write='$name.writeTo(bb)',
346 default="OFFactoryVer$version.MATCH_WILDCARD_ALL");
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800347group_mod_cmd = JType('OFGroupModCommand', 'short') \
348 .op(version=ANY, read="bb.readShort()", write="bb.writeShort($name)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700349flow_mod_cmd = JType('OFFlowModCommand', 'short') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700350 .op(version=1, read="bb.readShort()", write="bb.writeShort($name)") \
351 .op(version=ANY, read="bb.readByte()", write="bb.writeByte($name)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700352mac_addr = JType('MacAddress') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700353 .op(read="MacAddress.read6Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700354 write="$name.write6Bytes(bb)",
355 default="MacAddress.NONE")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700356
357port_name = gen_fixed_length_string_jtype(16)
358desc_str = gen_fixed_length_string_jtype(256)
359serial_num = gen_fixed_length_string_jtype(32)
360table_name = gen_fixed_length_string_jtype(32)
Rich Lanef8a3d002014-03-19 13:33:52 -0700361str64 = gen_fixed_length_string_jtype(64)
Yotam Harchola289d552013-09-16 10:10:40 -0700362ipv4 = JType("IPv4Address") \
363 .op(read="IPv4Address.read4Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700364 write="$name.write4Bytes(bb)",
365 default='IPv4Address.NONE')
Byungjoon Leeba92e292014-07-18 13:13:26 +0900366ipv4_list = JType('List<IPv4Address>') \
367 .op(read='ChannelUtils.readList(bb, $length, IPv4Address.READER)',
368 write='ChannelUtils.writeList(bb, $name)',
369 default='ImmutableList.<IPv4Address>of()',
370 funnel="FunnelUtils.putList($name, sink)")
Yotam Harchola289d552013-09-16 10:10:40 -0700371ipv6 = JType("IPv6Address") \
372 .op(read="IPv6Address.read16Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700373 write="$name.write16Bytes(bb)",
374 default='IPv6Address.NONE')
Byungjoon Leeba92e292014-07-18 13:13:26 +0900375ipv6_list = JType('List<IPv46ddress>') \
376 .op(read='ChannelUtils.readList(bb, $length, IPv6Address.READER)',
377 write='ChannelUtils.writeList(bb, $name)',
378 default='ImmutableList.<IPv6Address>of()',
379 funnel="FunnelUtils.putList($name, sink)")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700380packetin_reason = gen_enum_jtype("OFPacketInReason")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700381transport_port = JType("TransportPort")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700382 .op(read="TransportPort.read2Bytes(bb)",
383 write="$name.write2Bytes(bb)",
384 default="TransportPort.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700385eth_type = JType("EthType")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700386 .op(read="EthType.read2Bytes(bb)",
387 write="$name.write2Bytes(bb)",
388 default="EthType.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700389vlan_vid = JType("VlanVid")\
Andreas Wundsam98a18632013-11-05 11:34:24 -0800390 .op(version=ANY, read="VlanVid.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="VlanVid.ZERO")
391vlan_vid_match = JType("OFVlanVidMatch")\
392 .op(version=1, read="OFVlanVidMatch.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="OFVlanVidMatch.NONE") \
393 .op(version=2, read="OFVlanVidMatch.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="OFVlanVidMatch.NONE") \
394 .op(version=ANY, read="OFVlanVidMatch.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFVlanVidMatch.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700395vlan_pcp = JType("VlanPcp")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700396 .op(read="VlanPcp.readByte(bb)",
397 write="$name.writeByte(bb)",
398 default="VlanPcp.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700399ip_dscp = JType("IpDscp")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700400 .op(read="IpDscp.readByte(bb)",
401 write="$name.writeByte(bb)",
402 default="IpDscp.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700403ip_ecn = JType("IpEcn")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700404 .op(read="IpEcn.readByte(bb)",
405 write="$name.writeByte(bb)",
406 default="IpEcn.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700407ip_proto = JType("IpProtocol")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700408 .op(read="IpProtocol.readByte(bb)",
409 write="$name.writeByte(bb)",
410 default="IpProtocol.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700411icmpv4_type = JType("ICMPv4Type")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700412 .op(read="ICMPv4Type.readByte(bb)",
413 write="$name.writeByte(bb)",
414 default="ICMPv4Type.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700415icmpv4_code = JType("ICMPv4Code")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700416 .op(read="ICMPv4Code.readByte(bb)",
417 write="$name.writeByte(bb)",
418 default="ICMPv4Code.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700419arp_op = JType("ArpOpcode")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700420 .op(read="ArpOpcode.read2Bytes(bb)",
421 write="$name.write2Bytes(bb)",
422 default="ArpOpcode.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700423ipv6_flabel = JType("IPv6FlowLabel")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700424 .op(read="IPv6FlowLabel.read4Bytes(bb)",
425 write="$name.write4Bytes(bb)",
426 default="IPv6FlowLabel.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700427metadata = JType("OFMetadata")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700428 .op(read="OFMetadata.read8Bytes(bb)",
429 write="$name.write8Bytes(bb)",
430 default="OFMetadata.NONE")
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700431oxm = JType("OFOxm<?>")\
432 .op( read="OFOxmVer$version.READER.readFrom(bb)",
433 write="$name.writeTo(bb)")
434oxm_list = JType("OFOxmList") \
435 .op(
436 read= 'OFOxmList.readFrom(bb, $length, OFOxmVer$version.READER)', \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700437 write='$name.writeTo(bb)',
438 default="OFOxmList.EMPTY")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700439meter_features = JType("OFMeterFeatures")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700440 .op(read="OFMeterFeaturesVer$version.READER.readFrom(bb)",
441 write="$name.writeTo(bb)")
Wilson Ngd6181882014-04-14 16:28:35 -0700442bsn_vport = JType("OFBsnVport")\
443 .op(read="OFBsnVportVer$version.READER.readFrom(bb)",
Andreas Wundsam113d25b2014-01-22 20:17:37 -0800444 write="$name.writeTo(bb)")
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700445flow_wildcards = JType("int") \
446 .op(read='bb.readInt()',
447 write='bb.writeInt($name)',
448 default="OFFlowWildcardsSerializerVer$version.ALL_VAL")
449table_stats_wildcards = JType("int") \
450 .op(read='bb.readInt()',
451 write='bb.writeInt($name)')
Sovietaced25021ec2014-12-31 15:44:21 -0500452port_bitmap_128 = JType('OFBitMask128') \
Yotam Harchol2c535582013-10-01 15:50:20 -0700453 .op(read='OFBitMask128.read16Bytes(bb)',
Yotam Harchola11f38b2013-09-26 15:38:17 -0700454 write='$name.write16Bytes(bb)',
Yotam Harchol2c535582013-10-01 15:50:20 -0700455 default='OFBitMask128.NONE')
Sovietaced25021ec2014-12-31 15:44:21 -0500456port_bitmap_512 = JType('OFBitMask512') \
457 .op(read='OFBitMask512.read64Bytes(bb)',
458 write='$name.write64Bytes(bb)',
459 default='OFBitMask512.NONE')
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700460table_id = JType("TableId") \
461 .op(read='TableId.readByte(bb)',
462 write='$name.writeByte(bb)',
463 default='TableId.ALL')
Andreas Wundsam45c95f82013-10-08 15:04:23 -0700464table_id_default_zero = JType("TableId") \
465 .op(read='TableId.readByte(bb)',
466 write='$name.writeByte(bb)',
467 default='TableId.ZERO')
Sovietaced0f9dddf2014-01-28 19:45:07 -0800468of_aux_id = JType("OFAuxId") \
469 .op(read='OFAuxId.readByte(bb)',
Sovietaced92603b02014-01-28 18:23:24 -0800470 write='$name.writeByte(bb)',
Sovietaced0f9dddf2014-01-28 19:45:07 -0800471 default='OFAuxId.MAIN')
Andreas Wundsama0981022013-10-02 18:15:06 -0700472of_version = JType("OFVersion", 'byte') \
473 .op(read='bb.readByte()', write='bb.writeByte($name)')
Rob Vaterlausb10ae552013-09-23 14:39:39 -0700474
Andreas Wundsam8ec3bcc2013-09-16 19:44:00 -0700475port_speed = JType("PortSpeed")
Rob Vaterlaus4d311942013-09-24 13:41:44 -0700476error_type = JType("OFErrorType")
Andreas Wundsam73d7d672014-11-12 17:07:33 -0800477of_message = JType("OFMessage")\
478 .op(read="OFMessageVer$version.READER.readFrom(bb)",
479 write="$name.writeTo(bb)")
480
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700481of_type = JType("OFType", 'byte') \
482 .op(read='bb.readByte()', write='bb.writeByte($name)')
483action_type= gen_enum_jtype("OFActionType")\
484 .set_priv_type("short")\
485 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
486instruction_type = gen_enum_jtype("OFInstructionType")\
487 .set_priv_type('short') \
488 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
489buffer_id = JType("OFBufferId") \
490 .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700491boolean = JType("boolean", "byte") \
492 .op(read='(bb.readByte() != 0)',
493 write='bb.writeByte($name ? 1 : 0)',
494 default="false")
495datapath_id = JType("DatapathId") \
496 .op(read='DatapathId.of(bb.readLong())',
497 write='bb.writeLong($name.getLong())',
498 default='DatapathId.NONE')
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -0700499action_type_set = JType("Set<OFActionType>") \
500 .op(read='ChannelUtilsVer10.readSupportedActions(bb)',
501 write='ChannelUtilsVer10.writeSupportedActions(bb, $name)',
502 default='ImmutableSet.<OFActionType>of()',
503 funnel='ChannelUtilsVer10.putSupportedActionsTo($name, sink)')
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700504of_group = JType("OFGroup") \
505 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ALL")
Andreas Wundsamee8b42c2014-01-24 17:38:19 -0800506of_group_default_any = JType("OFGroup") \
507 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ANY")
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700508# the outgroup field of of_flow_stats_request has a special default value
509of_group_default_any = JType("OFGroup") \
510 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ANY")
511buffer_id = JType("OFBufferId") \
512 .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
Rich Lane376cafe2013-10-27 21:49:14 -0700513lag_id = JType("LagId") \
514 .op(version=ANY, read="LagId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="LagId.NONE")
Rich Laneeb21c4f2013-10-28 17:34:41 -0700515vrf = JType("VRF") \
516 .op(version=ANY, read="VRF.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="VRF.ZERO")
517class_id = JType("ClassId") \
518 .op(version=ANY, read="ClassId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="ClassId.NONE")
Rich Lane9c27b5d2013-10-30 17:14:32 -0700519boolean_value = JType('OFBooleanValue', 'OFBooleanValue') \
520 .op(read='OFBooleanValue.of(bb.readByte() != 0)', write='bb.writeByte($name.getInt())', default="OFBooleanValue.FALSE")
Andreas Wundsamd4b22692014-01-14 14:17:26 -0800521gen_table_id = JType("GenTableId") \
522 .op(read='GenTableId.read2Bytes(bb)',
523 write='$name.write2Bytes(bb)',
524 )
Andreas Wundsam73d7d672014-11-12 17:07:33 -0800525bundle_id = JType("BundleId") \
526 .op(read='BundleId.read4Bytes(bb)',
527 write='$name.write4Bytes(bb)',
528 )
Rich Lane53ddf5c2014-03-20 15:24:08 -0700529udf = JType("UDF") \
530 .op(version=ANY, read="UDF.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="UDF.ZERO")
Andreas Wundsam62cbcff2014-05-09 16:35:00 -0700531error_cause_data = JType("OFErrorCauseData") \
Andreas Wundsam39efd1c2014-05-13 19:41:51 -0700532 .op(version=ANY, read="OFErrorCauseData.read(bb, $length, OFVersion.OF_$version)", write="$name.writeTo(bb)", default="OFErrorCauseData.NONE");
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700533
Andreas Wundsame2fd7ce2014-06-26 20:56:42 -0700534var_string = JType('String').op(
535 read='ChannelUtils.readFixedLengthString(bb, $length)',
536 write='ChannelUtils.writeFixedLengthString(bb, $name, $name.length())',
537 default='""',
538 funnel='sink.putUnencodedChars($name)'
539 )
540
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700541generic_t = JType("T")
542
Rich Lane0b1698d2014-10-17 18:35:37 -0700543table_desc = JType('OFTableDesc') \
544 .op(read='OFTableDescVer$version.READER.readFrom(bb)', \
545 write='$name.writeTo(bb)')
546
Andreas Wundsam27303462013-07-16 12:52:35 -0700547
548default_mtype_to_jtype_convert_map = {
549 'uint8_t' : u8,
550 'uint16_t' : u16,
551 'uint32_t' : u32,
552 'uint64_t' : u64,
Andreas Wundsam27303462013-07-16 12:52:35 -0700553 'of_port_no_t' : of_port,
554 'list(of_action_t)' : actions_list,
555 'list(of_instruction_t)' : instructions_list,
556 'list(of_bucket_t)': buckets_list,
557 'list(of_port_desc_t)' : port_desc_list,
558 'list(of_packet_queue_t)' : packet_queue_list,
xinwuf08ef682013-12-05 18:29:20 -0800559 'list(of_uint64_t)' : u64_list,
Andreas Wundsam27303462013-07-16 12:52:35 -0700560 'list(of_uint32_t)' : u32_list,
561 'list(of_uint8_t)' : u8_list,
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700562 'list(of_oxm_t)' : oxm_list,
Byungjoon Leeba92e292014-07-18 13:13:26 +0900563 'list(of_ipv4_t)' : ipv4_list,
564 'list(of_ipv6_t)' : ipv6_list,
Andreas Wundsam27303462013-07-16 12:52:35 -0700565 'of_octets_t' : octets,
566 'of_match_t': of_match,
567 'of_fm_cmd_t': flow_mod_cmd,
568 'of_mac_addr_t': mac_addr,
569 'of_port_desc_t': port_desc,
570 'of_desc_str_t': desc_str,
571 'of_serial_num_t': serial_num,
572 'of_port_name_t': port_name,
573 'of_table_name_t': table_name,
Rich Lanef8a3d002014-03-19 13:33:52 -0700574 'of_str64_t': str64,
Andreas Wundsam27303462013-07-16 12:52:35 -0700575 'of_ipv4_t': ipv4,
576 'of_ipv6_t': ipv6,
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700577 'of_wc_bmap_t': flow_wildcards,
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700578 'of_oxm_t': oxm,
579 'of_meter_features_t': meter_features,
Sovietaced25021ec2014-12-31 15:44:21 -0500580 'of_bitmap_128_t': port_bitmap_128,
581 'of_bitmap_512_t': port_bitmap_512,
Andreas Wundsam7c15b172014-04-24 19:02:40 -0700582 'of_checksum_128_t': u128,
Wilson Ngd6181882014-04-14 16:28:35 -0700583 'of_bsn_vport_t': bsn_vport,
Rich Lane0b1698d2014-10-17 18:35:37 -0700584 'of_table_desc_t': table_desc,
Andreas Wundsam27303462013-07-16 12:52:35 -0700585 }
586
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700587## Map that defines exceptions from the standard loxi->java mapping scheme
588# map of {<loxi_class_name> : { <loxi_member_name> : <JType instance> } }
Andreas Wundsam27303462013-07-16 12:52:35 -0700589exceptions = {
Yotam Harcholc742e202013-08-15 12:16:24 -0700590 'of_packet_in': { 'data' : octets, 'reason': packetin_reason },
591 'of_oxm_tcp_src' : { 'value' : transport_port },
592 'of_oxm_tcp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
593 'of_oxm_tcp_dst' : { 'value' : transport_port },
594 'of_oxm_tcp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
595 'of_oxm_udp_src' : { 'value' : transport_port },
596 'of_oxm_udp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
597 'of_oxm_udp_dst' : { 'value' : transport_port },
598 'of_oxm_udp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
599 'of_oxm_sctp_src' : { 'value' : transport_port },
600 'of_oxm_sctp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
601 'of_oxm_sctp_dst' : { 'value' : transport_port },
602 'of_oxm_sctp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
603 'of_oxm_eth_type' : { 'value' : eth_type },
604 'of_oxm_eth_type_masked' : { 'value' : eth_type, 'value_mask' : eth_type },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800605 'of_oxm_vlan_vid' : { 'value' : vlan_vid_match },
606 'of_oxm_vlan_vid_masked' : { 'value' : vlan_vid_match, 'value_mask' : vlan_vid_match },
Yotam Harcholc742e202013-08-15 12:16:24 -0700607 'of_oxm_vlan_pcp' : { 'value' : vlan_pcp },
608 'of_oxm_vlan_pcp_masked' : { 'value' : vlan_pcp, 'value_mask' : vlan_pcp },
609 'of_oxm_ip_dscp' : { 'value' : ip_dscp },
610 'of_oxm_ip_dscp_masked' : { 'value' : ip_dscp, 'value_mask' : ip_dscp },
611 'of_oxm_ip_ecn' : { 'value' : ip_ecn },
612 'of_oxm_ip_ecn_masked' : { 'value' : ip_ecn, 'value_mask' : ip_ecn },
613 'of_oxm_ip_proto' : { 'value' : ip_proto },
614 'of_oxm_ip_proto_masked' : { 'value' : ip_proto, 'value_mask' : ip_proto },
615 'of_oxm_icmpv4_type' : { 'value' : icmpv4_type },
616 'of_oxm_icmpv4_type_masked' : { 'value' : icmpv4_type, 'value_mask' : icmpv4_type },
617 'of_oxm_icmpv4_code' : { 'value' : icmpv4_code },
618 'of_oxm_icmpv4_code_masked' : { 'value' : icmpv4_code, 'value_mask' : icmpv4_code },
619 'of_oxm_arp_op' : { 'value' : arp_op },
620 'of_oxm_arp_op_masked' : { 'value' : arp_op, 'value_mask' : arp_op },
621 'of_oxm_arp_spa' : { 'value' : ipv4 },
622 'of_oxm_arp_spa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 },
623 'of_oxm_arp_tpa' : { 'value' : ipv4 },
624 'of_oxm_arp_tpa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 },
625 'of_oxm_ipv6_flabel' : { 'value' : ipv6_flabel },
626 'of_oxm_ipv6_flabel_masked' : { 'value' : ipv6_flabel, 'value_mask' : ipv6_flabel },
627 'of_oxm_metadata' : { 'value' : metadata },
628 'of_oxm_metadata_masked' : { 'value' : metadata, 'value_mask' : metadata },
Yotam Harchol5804f772013-08-21 17:35:31 -0700629
630 'of_oxm_icmpv6_code' : { 'value' : u8obj },
631 'of_oxm_icmpv6_code_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
632 'of_oxm_icmpv6_type' : { 'value' : u8obj },
633 'of_oxm_icmpv6_type_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
634 'of_oxm_mpls_label' : { 'value' : u32obj },
635 'of_oxm_mpls_label_masked' : { 'value' : u32obj, 'value_mask' : u32obj },
636 'of_oxm_mpls_tc' : { 'value' : u8obj },
637 'of_oxm_mpls_tc_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
Saurav Dasbc266742014-10-09 16:45:27 -0700638 'of_oxm_mpls_bos' : { 'value' : boolean_value },
639 'of_oxm_mpls_bos_masked' : { 'value' : boolean_value, 'value_mask' : boolean_value },
Rich Lane9f595042014-10-31 15:56:34 -0700640 'of_oxm_ipv6_exthdr' : { 'value' : u16obj },
641 'of_oxm_ipv6_exthdr_masked' : { 'value' : u16obj, 'value_mask' : u16obj },
642 'of_oxm_pbb_uca' : { 'value' : boolean_value },
643 'of_oxm_pbb_uca_masked' : { 'value' : boolean_value, 'value_mask' : boolean_value },
Saurav Dasbc266742014-10-09 16:45:27 -0700644
Sovietaced25021ec2014-12-31 15:44:21 -0500645 'of_oxm_bsn_in_ports_128' : { 'value': port_bitmap_128 },
646 'of_oxm_bsn_in_ports_128_masked' : { 'value': port_bitmap_128, 'value_mask': port_bitmap_128 },
647
648 'of_oxm_bsn_in_ports_512' : { 'value': port_bitmap_512 },
649 'of_oxm_bsn_in_ports_512_masked' : { 'value': port_bitmap_512, 'value_mask': port_bitmap_512 },
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700650
Rich Lane376cafe2013-10-27 21:49:14 -0700651 'of_oxm_bsn_lag_id' : { 'value' : lag_id },
652 'of_oxm_bsn_lag_id_masked' : { 'value' : lag_id, 'value_mask' : lag_id },
Rich Lane1424d0b2013-10-24 17:16:24 -0700653
Rich Laneeb21c4f2013-10-28 17:34:41 -0700654 'of_oxm_bsn_vrf' : { 'value' : vrf },
655 'of_oxm_bsn_vrf_masked' : { 'value' : vrf, 'value_mask' : vrf },
656
Rich Lane9c27b5d2013-10-30 17:14:32 -0700657 'of_oxm_bsn_global_vrf_allowed' : { 'value' : boolean_value },
658 'of_oxm_bsn_global_vrf_allowed_masked' : { 'value' : boolean_value, 'value_mask' : boolean_value },
Saurav Das5ebb75b2014-10-09 19:04:44 -0700659
Rich Laneeb21c4f2013-10-28 17:34:41 -0700660 'of_oxm_bsn_l3_interface_class_id' : { 'value' : class_id },
661 'of_oxm_bsn_l3_interface_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
662
663 'of_oxm_bsn_l3_src_class_id' : { 'value' : class_id },
664 'of_oxm_bsn_l3_src_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
665
666 'of_oxm_bsn_l3_dst_class_id' : { 'value' : class_id },
667 'of_oxm_bsn_l3_dst_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
668
Rich Lane917bb9a2014-03-11 17:49:56 -0700669 'of_oxm_bsn_egr_port_group_id' : { 'value' : class_id },
670 'of_oxm_bsn_egr_port_group_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
671
Rich Lane75d235a2015-02-03 17:40:10 -0800672 'of_oxm_bsn_ingress_port_group_id' : { 'value' : class_id },
673 'of_oxm_bsn_ingress_port_group_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
674
Rich Lane53ddf5c2014-03-20 15:24:08 -0700675 'of_oxm_bsn_udf0' : { 'value' : udf },
676 'of_oxm_bsn_udf0_masked' : { 'value' : udf, 'value_mask' : udf },
677
678 'of_oxm_bsn_udf1' : { 'value' : udf },
679 'of_oxm_bsn_udf1_masked' : { 'value' : udf, 'value_mask' : udf },
680
681 'of_oxm_bsn_udf2' : { 'value' : udf },
682 'of_oxm_bsn_udf2_masked' : { 'value' : udf, 'value_mask' : udf },
683
684 'of_oxm_bsn_udf3' : { 'value' : udf },
685 'of_oxm_bsn_udf3_masked' : { 'value' : udf, 'value_mask' : udf },
686
687 'of_oxm_bsn_udf4' : { 'value' : udf },
688 'of_oxm_bsn_udf4_masked' : { 'value' : udf, 'value_mask' : udf },
689
690 'of_oxm_bsn_udf5' : { 'value' : udf },
691 'of_oxm_bsn_udf5_masked' : { 'value' : udf, 'value_mask' : udf },
692
693 'of_oxm_bsn_udf6' : { 'value' : udf },
694 'of_oxm_bsn_udf6_masked' : { 'value' : udf, 'value_mask' : udf },
695
696 'of_oxm_bsn_udf7' : { 'value' : udf },
697 'of_oxm_bsn_udf7_masked' : { 'value' : udf, 'value_mask' : udf },
698
Rich Lane9b178072014-05-19 15:31:55 -0700699 'of_oxm_bsn_tcp_flags' : { 'value' : u16obj },
700 'of_oxm_bsn_tcp_flags_masked' : { 'value' : u16obj, 'value_mask' : u16obj },
701
Rich Lanea5eeae32014-06-26 19:59:04 -0700702 'of_oxm_bsn_vlan_xlate_port_group_id' : { 'value' : class_id },
703 'of_oxm_bsn_vlan_xlate_port_group_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
704
Wilson Ng15efef92014-11-05 14:27:35 -0800705 'of_oxm_bsn_l2_cache_hit' : { 'value' : boolean_value },
706 'of_oxm_bsn_l2_cache_hit_masked' : { 'value' : boolean_value, 'value_mask' : boolean_value },
Wilson Nga3483d62014-11-05 13:50:37 -0800707
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700708 'of_table_stats_entry': { 'wildcards': table_stats_wildcards },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800709 'of_match_v1': { 'vlan_vid' : vlan_vid_match, 'vlan_pcp': vlan_pcp,
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700710 'eth_type': eth_type, 'ip_dscp': ip_dscp, 'ip_proto': ip_proto,
Andreas Wundsame962d372013-10-02 18:15:58 -0700711 'tcp_src': transport_port, 'tcp_dst': transport_port,
712 'in_port': of_port_match_v1
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700713 },
714 'of_bsn_set_l2_table_request': { 'l2_table_enable': boolean },
715 'of_bsn_set_l2_table_reply': { 'l2_table_enable': boolean },
Rob Vaterlause1b86842013-10-18 13:29:19 -0700716 'of_bsn_set_pktin_suppression_request': { 'enabled': boolean },
Sovietaced768c3e22014-04-04 16:46:48 -0700717 'of_bsn_controller_connection': { 'auxiliary_id' : of_aux_id},
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700718 'of_flow_stats_request': { 'out_group': of_group_default_any },
Andreas Wundsam384ccc12014-03-12 14:52:42 -0700719 'of_aggregate_stats_request': { 'out_group': of_group_default_any },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800720
721 'of_action_bsn_mirror': { 'dest_port': of_port },
722 'of_action_push_mpls': { 'ethertype': eth_type },
723 'of_action_push_pbb': { 'ethertype': eth_type },
724 'of_action_push_vlan': { 'ethertype': eth_type },
Ronald Li4fb8abb2013-11-21 19:53:02 -0800725 'of_action_pop_mpls': { 'ethertype': eth_type },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800726 'of_action_set_nw_dst': { 'nw_addr': ipv4 },
727 'of_action_set_nw_ecn': { 'nw_ecn': ip_ecn },
728 'of_action_set_nw_src': { 'nw_addr': ipv4 },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800729 'of_action_set_tp_dst': { 'tp_port': transport_port },
730 'of_action_set_tp_src': { 'tp_port': transport_port },
731 'of_action_set_vlan_pcp': { 'vlan_pcp': vlan_pcp },
732 'of_action_set_vlan_vid': { 'vlan_vid': vlan_vid },
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800733
734 'of_group_mod' : { 'command' : group_mod_cmd },
735 'of_group_add' : { 'command' : group_mod_cmd },
736 'of_group_modify' : { 'command' : group_mod_cmd },
737 'of_group_delete' : { 'command' : group_mod_cmd },
738
739 'of_bucket' : { 'watch_group': of_group },
Andreas Wundsamd4b22692014-01-14 14:17:26 -0800740
741 'of_bsn_tlv_vlan_vid' : { 'value' : vlan_vid },
Andreas Wundsamec1f6612014-06-25 15:48:12 -0700742 'of_bsn_table_set_buckets_size' : { 'table_id' : table_id },
Andreas Wundsamd4b22692014-01-14 14:17:26 -0800743 'of_bsn_gentable_entry_add' : { 'table_id' : gen_table_id },
Andreas Wundsame2fd7ce2014-06-26 20:56:42 -0700744 'of_bsn_log': { 'data': var_string },
Sovietaced92603b02014-01-28 18:23:24 -0800745
Sovietaced0f9dddf2014-01-28 19:45:07 -0800746 'of_features_reply' : { 'auxiliary_id' : of_aux_id},
Andreas Wundsam73d7d672014-11-12 17:07:33 -0800747
748 'of_bundle_add_msg' : { 'data' : of_message },
Andreas Wundsam27303462013-07-16 12:52:35 -0700749}
750
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700751
752@memoize
753def enum_java_types():
754 enum_types = {}
Andreas Wundsamc8912c12013-11-15 13:44:48 -0800755 for enum in loxi_globals.unified.enums:
756 java_name = name_c_to_caps_camel(re.sub(r'_t$', "", enum.name))
757 enum_types[enum.name] = gen_enum_jtype(java_name, enum.is_bitmask)
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700758 return enum_types
759
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700760def make_match_field_jtype(sub_type_name="?"):
761 return JType("MatchField<{}>".format(sub_type_name))
762
Andreas Wundsam661a2222013-11-05 17:18:59 -0800763def make_oxm_jtype(sub_type_name="?"):
764 return JType("OFOxm<{}>".format(sub_type_name))
Andreas Wundsam27303462013-07-16 12:52:35 -0700765
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700766def list_cname_to_java_name(c_type):
Andreas Wundsam27303462013-07-16 12:52:35 -0700767 m = re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type)
768 if not m:
769 raise Exception("Not a recgonized standard list type declaration: %s" % c_type)
770 base_name = m.group(1)
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700771 return "OF" + name_c_to_caps_camel(base_name)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700772
773#### main entry point for conversion of LOXI types (c_types) Java types.
774# FIXME: This badly needs a refactoring
775
Andreas Wundsam27303462013-07-16 12:52:35 -0700776def convert_to_jtype(obj_name, field_name, c_type):
777 """ Convert from a C type ("uint_32") to a java type ("U32")
778 and return a JType object with the size, internal type, and marshalling functions"""
779 if obj_name in exceptions and field_name in exceptions[obj_name]:
780 return exceptions[obj_name][field_name]
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700781 elif ( obj_name == "of_header" or loxi_utils.class_is_message(obj_name)) and field_name == "type" and c_type == "uint8_t":
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700782 return of_type
Andreas Wundsam27303462013-07-16 12:52:35 -0700783 elif field_name == "type" and re.match(r'of_action.*', obj_name):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700784 return action_type
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700785 elif field_name == "err_type":
786 return JType("OFErrorType", 'short') \
787 .op(read='bb.readShort()', write='bb.writeShort($name)')
Andreas Wundsam62cbcff2014-05-09 16:35:00 -0700788 elif loxi_utils.class_is(obj_name, "of_error_msg") and field_name == "data":
789 return error_cause_data
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700790 elif field_name == "stats_type":
791 return JType("OFStatsType", 'short') \
792 .op(read='bb.readShort()', write='bb.writeShort($name)')
Andreas Wundsam999c0732013-10-01 19:29:16 -0700793 elif field_name == "type" and re.match(r'of_instruction.*', obj_name):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700794 return instruction_type
Andreas Wundsamee8b42c2014-01-24 17:38:19 -0800795 elif loxi_utils.class_is(obj_name, "of_flow_mod") and field_name == "table_id" and c_type == "uint8_t":
Andreas Wundsam45c95f82013-10-08 15:04:23 -0700796 return table_id_default_zero
Andreas Wundsamee8b42c2014-01-24 17:38:19 -0800797 elif loxi_utils.class_is(obj_name, "of_flow_mod") and field_name == "out_group" and c_type == "uint32_t":
798 return of_group_default_any
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700799 elif field_name == "table_id" and c_type == "uint8_t":
800 return table_id
Andreas Wundsam27303462013-07-16 12:52:35 -0700801 elif field_name == "version" and c_type == "uint8_t":
Andreas Wundsama0981022013-10-02 18:15:06 -0700802 return of_version
Rob Vaterlausb10ae552013-09-23 14:39:39 -0700803 elif field_name == "buffer_id" and c_type == "uint32_t":
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700804 return buffer_id
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700805 elif field_name == "group_id" and c_type == "uint32_t":
806 return of_group
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700807 elif field_name == 'datapath_id':
808 return datapath_id
809 elif field_name == 'actions' and obj_name == 'of_features_reply':
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -0700810 return action_type_set
Andreas Wundsamd4b22692014-01-14 14:17:26 -0800811 elif field_name == "table_id" and re.match(r'of_bsn_gentable.*', obj_name):
812 return gen_table_id
Andreas Wundsam73d7d672014-11-12 17:07:33 -0800813 elif field_name == "bundle_id" and re.match(r'of_bundle_.*', obj_name):
814 return bundle_id
Andreas Wundsam27303462013-07-16 12:52:35 -0700815 elif c_type in default_mtype_to_jtype_convert_map:
816 return default_mtype_to_jtype_convert_map[c_type]
817 elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700818 return gen_list_jtype(list_cname_to_java_name(c_type))
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700819 elif c_type in enum_java_types():
820 return enum_java_types()[c_type]
Andreas Wundsam27303462013-07-16 12:52:35 -0700821 else:
822 print "WARN: Couldn't find java type conversion for '%s' in %s:%s" % (c_type, obj_name, field_name)
823 jtype = name_c_to_caps_camel(re.sub(r'_t$', "", c_type))
824 return JType(jtype)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700825
826
827#### Enum specific wiretype definitions
828enum_wire_types = {
829 "uint8_t": JType("byte").op(read="bb.readByte()", write="bb.writeByte($name)"),
830 "uint16_t": JType("short").op(read="bb.readShort()", write="bb.writeShort($name)"),
831 "uint32_t": JType("int").op(read="bb.readInt()", write="bb.writeInt($name)"),
832 "uint64_t": JType("long").op(read="bb.readLong()", write="bb.writeLong($name)"),
833}
834
835def convert_enum_wire_type_to_jtype(wire_type):
836 return enum_wire_types[wire_type]