blob: d2e60f79f91a1c26f5c0daa08cbf7a7fe4404b79 [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 Wundsam7cfeac32013-09-17 13:53:48 -07007from generic_utils import memoize
8import loxi_utils.loxi_utils as loxi_utils
9import of_g
10
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 Wundsam46d230f2013-08-02 22:24:06 -070056 max = (1 << bits)-1
57 if value > max:
58 raise Exception("Value %d to large for type %s" % (value, t))
59
60 if signed:
61 max_pos = (1 << (bits-1)) - 1
62
63 if value > max_pos:
64 if t == "long":
65 return str((1 << bits) - value)
66 else:
67 return "(%s) 0x%x" % (t, value)
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070068 return "%s0x%x%s" % ("(%s) " % t if cast_needed else "", value, "L" if t=="long" else "")
69
Yotam Harchold7b84202013-07-26 16:08:10 -070070
Andreas Wundsame916d6f2013-07-30 11:33:58 -070071ANY = 0xFFFFFFFFFFFFFFFF
Yotam Harchold7b84202013-07-26 16:08:10 -070072
73class VersionOp:
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070074 def __init__(self, version=ANY, read=None, write=None, default=None, funnel=None):
Yotam Harchold7b84202013-07-26 16:08:10 -070075 self.version = version
76 self.read = read
77 self.write = write
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -070078 self.default = default
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070079 self.funnel = funnel
Andreas Wundsame916d6f2013-07-30 11:33:58 -070080
Yotam Harchold7b84202013-07-26 16:08:10 -070081 def __str__(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070082 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 -070083
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070084### FIXME: This class should really be cleaned up
Andreas Wundsam27303462013-07-16 12:52:35 -070085class JType(object):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070086 """ Wrapper class to hold C to Java type conversion information. JTypes can have a 'public'
87 and or 'private' java type associated with them and can define how those types can be
88 read from and written to ChannelBuffers.
89
90 """
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -070091 def __init__(self, pub_type, priv_type=None):
Andreas Wundsam27303462013-07-16 12:52:35 -070092 self.pub_type = pub_type # the type we expose externally, e.g. 'U8'
93 if priv_type is None:
94 priv_type = pub_type
95 self.priv_type = priv_type # the internal storage type
Yotam Harchold7b84202013-07-26 16:08:10 -070096 self.ops = {}
Yotam Harchold7b84202013-07-26 16:08:10 -070097
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070098 def set_priv_type(self, priv_type):
99 self.priv_type = priv_type
100 return self
101
102 def op(self, version=ANY, read=None, write=None, default=None, funnel=None, pub_type=ANY):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700103 """
104 define operations to be performed for reading and writing this type
105 (when read_op, write_op is called). The operations 'read' and 'write'
106 can either be strings ($name, and $version and $length will be replaced),
107 or callables (name, version and length) will be passed.
108
109 @param version int OF version to define operation for, or ANY for all
110 @param pub_type boolean whether to define operations for the public type (True), the
111 private type(False) or both (ALL)
112 @param read read expression (either string or callable)s
113 @param write write expression (either string or callable)
114 """
115
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700116 pub_types = [ pub_type ] if pub_type is not ANY else [ False, True ]
117 for pub_type in pub_types:
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700118 self.ops[(version, pub_type)] = VersionOp(version, read, write, default, funnel)
Yotam Harchold7b84202013-07-26 16:08:10 -0700119 return self
Andreas Wundsam27303462013-07-16 12:52:35 -0700120
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700121 def format_value(self, value, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700122 # Format a constant value of this type, for inclusion in the java source code
123 # For primitive types, takes care of casting the value appropriately, to
124 # cope with java's signedness limitation
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700125 t = self.pub_type if pub_type else self.priv_type
126 if t in java_primitive_types:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700127 return format_primitive_literal(t, value)
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700128 else:
129 return value
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700130
Andreas Wundsam27303462013-07-16 12:52:35 -0700131 @property
132 def public_type(self):
133 """ return the public type """
134 return self.pub_type
135
136 def priv(self):
137 """ return the public type """
138 return self.priv_type
139
140 def has_priv(self):
141 """ Is the private type different from the public one?"""
142 return self.pub_type != self.priv_type
143
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700144 def get_op(self, op_type, version, pub_type, default_value, arguments):
145 ver = ANY if version is None else version.int_version
146
147 if not "version" in arguments:
148 arguments["version"] = version.of_version
149
150 def lookup(ver, pub_type):
151 if (ver, pub_type) in self.ops:
152 return getattr(self.ops[(ver, pub_type)], op_type)
153 else:
154 return None
155
156 _op = lookup(ver, pub_type) or lookup(ANY, pub_type) or default_value
157 if callable(_op):
158 return _op(**arguments)
159 else:
160 return reduce(lambda a,repl: a.replace("$%s" % repl[0], str(repl[1])), arguments.items(), _op)
161
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700162 def read_op(self, version=None, length=None, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700163 """ return a Java stanza that reads a value of this JType from ChannelBuffer bb.
164 @param version int - OF wire version to generate expression for
165 @param pub_type boolean use this JTypes 'public' (True), or private (False) representation
166 @param length string, for operations that need it (e.g., read a list of unknown length)
167 Java expression evaluating to the byte length to be read. Defaults to the remainig
168 length of the message.
169 @return string containing generated Java expression.
170 """
Andreas Wundsam27303462013-07-16 12:52:35 -0700171 if length is None:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700172 # assumes that
173 # (1) length of the message has been read to 'length'
174 # (2) readerIndex at the start of the message has been stored in 'start'
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700175 length = "length - (bb.readerIndex() - start)"
Andreas Wundsam27303462013-07-16 12:52:35 -0700176
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700177 return self.get_op("read", version, pub_type,
178 default_value='ChannelUtilsVer$version.read%s(bb)' % self.pub_type,
179 arguments=dict(length=length)
180 )
Andreas Wundsam27303462013-07-16 12:52:35 -0700181
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700182 def write_op(self, version=None, name=None, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700183 """ return a Java stanza that writes a value of this JType contained in Java expression
184 'name' to ChannelBuffer bb.
185 @param name string containing Java expression that evaluations to the value to be written
186 @param version int - OF wire version to generate expression for
187 @param pub_type boolean use this JTypes 'public' (True), or private (False) representation
188 @return string containing generated Java expression.
189 """
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700190
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700191 return self.get_op("write", version, pub_type,
192 default_value='ChannelUtilsVer$version.write%s(bb, $name)' % self.pub_type,
193 arguments=dict(name=name)
194 )
195
Andreas Wundsam27303462013-07-16 12:52:35 -0700196
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700197 def default_op(self, version=None, pub_type=True):
198 """ return a Java stanza that returns a default value of this JType.
199 @param version JavaOFVersion
200 @return string containing generated Java expression.
201 """
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700202 return self.get_op("default", version, pub_type,
203 arguments = dict(),
204 default_value = self.format_value(0) if self.is_primitive else "null"
205 )
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700206
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700207 def skip_op(self, version=None, length=None):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700208 """ return a java stanza that skips an instance of JType in the input ChannelBuffer 'bb'.
209 This is used in the Reader implementations for virtual classes (because after the
210 discriminator field, the concrete Reader instance will re-read all the fields)
211 Currently just delegates to read_op + throws away the result."""
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700212 return self.read_op(version, length)
213
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700214 def funnel_op(self, version=None, name=None, pub_type=True):
215 t = self.pub_type if pub_type else self.priv_type
216 return self.get_op("funnel", version, pub_type,
217 arguments = dict(name=name),
218 default_value = '$name.putTo(sink)' if not self._is_primitive(pub_type) else "sink.put{}($name)".format(t[0].upper() + t[1:])
219 )
220
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700221 @property
222 def is_primitive(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700223 return self._is_primitive()
224
225 def _is_primitive(self, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700226 """ return true if the pub_type is a java primitive type (and thus needs
227 special treatment, because it doesn't have methods)"""
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700228 t = self.pub_type if pub_type else self.priv_type
229 return t in java_primitive_types
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700230
231 @property
232 def is_array(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700233 return self._is_array()
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700234
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700235 def _is_array(self, pub_type=True):
236 t = self.pub_type if pub_type else self.priv_type
237 return t.endswith("[]")
238
239# Create a default mapping for a list type. Type defauls to List<${java_mapping_of_name}>
240def gen_enum_jtype(java_name, is_bitmask=False):
241 if is_bitmask:
242 java_type = "Set<{}>".format(java_name)
243 default_value = "ImmutableSet.<{}>of()".format(java_name)
244 else:
245 java_type = java_name
246 default_value = "null"
247
248 serializer = "{}SerializerVer$version".format(java_name)
249
250 return JType(java_type)\
251 .op(read="{}.readFrom(bb)".format(serializer),
252 write="{}.writeTo(bb, $name)".format(serializer),
253 default=default_value,
254 funnel="{}.putTo($name, sink)".format(serializer)
255 )
256
257def gen_list_jtype(java_base_name):
258 # read op assumes the class has a public final static field READER that implements
259 # OFMessageReader<$class> i.e., can deserialize an instance of class from a ChannelBuffer
260 # write op assumes class implements Writeable
261 return JType("List<{}>".format(java_base_name)) \
262 .op(
263 read= 'ChannelUtils.readList(bb, $length, {}Ver$version.READER)'.format(java_base_name), \
264 write='ChannelUtils.writeList(bb, $name)',
265 default="ImmutableList.<{}>of()".format(java_base_name),
266 funnel='FunnelUtils.putList($name, sink)'
267 )
268
269def gen_fixed_length_string_jtype(length):
270 return JType('String').op(
271 read='ChannelUtils.readFixedLengthString(bb, {})'.format(length),
272 write='ChannelUtils.writeFixedLengthString(bb, $name, {})'.format(length),
273 default='""',
274 funnel='sink.putUnencodedChars($name)'
275 )
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700276
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700277##### Predefined JType mappings
278# FIXME: This list needs to be pruned / cleaned up. Most of these are schematic.
279
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700280u8 = JType('short', 'byte') \
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700281 .op(read='U8.f(bb.readByte())', write='bb.writeByte(U8.t($name))', pub_type=True) \
282 .op(read='bb.readByte()', write='bb.writeByte($name)', pub_type=False)
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700283u8_list = JType('List<U8>') \
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700284 .op(read='ChannelUtils.readList(bb, $length, U8.READER)',
285 write='ChannelUtils.writeList(bb, $name)',
286 default='ImmutableList.<U8>of()',
287 funnel='FunnelUtils.putList($name, sink)'
288 )
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700289u16 = JType('int', 'short') \
290 .op(read='U16.f(bb.readShort())', write='bb.writeShort(U16.t($name))', pub_type=True) \
291 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
292u32 = JType('long', 'int') \
293 .op(read='U32.f(bb.readInt())', write='bb.writeInt(U32.t($name))', pub_type=True) \
294 .op(read='bb.readInt()', write='bb.writeInt($name)', pub_type=False)
295u32_list = JType('List<U32>', 'int[]') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700296 .op(
297 read='ChannelUtils.readList(bb, $length, U32.READER)',
298 write='ChannelUtils.writeList(bb, $name)',
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700299 default="ImmutableList.<U32>of()",
300 funnel="FunnelUtils.putList($name, sink)")
Yotam Harchol5804f772013-08-21 17:35:31 -0700301u8obj = JType('U8', 'U8') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700302 .op(read='U8.of(bb.readByte())', write='bb.writeByte($name.getRaw())', default="U8.ZERO")
Yotam Harchol5804f772013-08-21 17:35:31 -0700303u32obj = JType('U32', 'U32') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700304 .op(read='U32.of(bb.readInt())', write='bb.writeInt($name.getRaw())', default="U32.ZERO")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700305u64 = JType('U64', 'U64') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700306 .op(read='U64.ofRaw(bb.readLong())', write='bb.writeLong($name.getValue())', default="U64.ZERO")
Yotam Harchold7b84202013-07-26 16:08:10 -0700307of_port = JType("OFPort") \
Andreas Wundsamad499c92013-09-28 18:56:49 -0700308 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ANY") \
309 .op(version=ANY, read="OFPort.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFPort.ANY")
Andreas Wundsame962d372013-10-02 18:15:58 -0700310# the same OFPort, but with a default value of ZERO, only for OF10 match
311of_port_match_v1 = JType("OFPort") \
312 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ZERO")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700313actions_list = gen_list_jtype("OFAction")
314instructions_list = gen_list_jtype("OFInstruction")
315buckets_list = gen_list_jtype("OFBucket")
316port_desc_list = gen_list_jtype("OFPortDesc")
317packet_queue_list = gen_list_jtype("OFPacketQueue")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700318port_desc = JType('OFPortDesc') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700319 .op(read='OFPortDescVer$version.READER.readFrom(bb)', \
Yotam Harchold7b84202013-07-26 16:08:10 -0700320 write='$name.writeTo(bb)')
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700321octets = JType('byte[]')\
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700322 .op(read='ChannelUtils.readBytes(bb, $length)', \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700323 write='bb.writeBytes($name)', \
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700324 default="new byte[0]",
325 funnel="sink.putBytes($name)"
326 );
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700327of_match = JType('Match') \
Andreas Wundsam951ada32013-08-01 22:05:38 -0700328 .op(read='ChannelUtilsVer$version.readOFMatch(bb)', \
Andreas Wundsame962d372013-10-02 18:15:58 -0700329 write='$name.writeTo(bb)',
330 default="OFFactoryVer$version.MATCH_WILDCARD_ALL");
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700331flow_mod_cmd = JType('OFFlowModCommand', 'short') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700332 .op(version=1, read="bb.readShort()", write="bb.writeShort($name)") \
333 .op(version=ANY, read="bb.readByte()", write="bb.writeByte($name)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700334mac_addr = JType('MacAddress') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700335 .op(read="MacAddress.read6Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700336 write="$name.write6Bytes(bb)",
337 default="MacAddress.NONE")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700338
339port_name = gen_fixed_length_string_jtype(16)
340desc_str = gen_fixed_length_string_jtype(256)
341serial_num = gen_fixed_length_string_jtype(32)
342table_name = gen_fixed_length_string_jtype(32)
Yotam Harchola289d552013-09-16 10:10:40 -0700343ipv4 = JType("IPv4Address") \
344 .op(read="IPv4Address.read4Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700345 write="$name.write4Bytes(bb)",
346 default='IPv4Address.NONE')
Yotam Harchola289d552013-09-16 10:10:40 -0700347ipv6 = JType("IPv6Address") \
348 .op(read="IPv6Address.read16Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700349 write="$name.write16Bytes(bb)",
350 default='IPv6Address.NONE')
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700351packetin_reason = gen_enum_jtype("OFPacketInReason")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700352transport_port = JType("TransportPort")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700353 .op(read="TransportPort.read2Bytes(bb)",
354 write="$name.write2Bytes(bb)",
355 default="TransportPort.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700356eth_type = JType("EthType")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700357 .op(read="EthType.read2Bytes(bb)",
358 write="$name.write2Bytes(bb)",
359 default="EthType.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700360vlan_vid = JType("VlanVid")\
Andreas Wundsam55b71ce2013-10-01 19:26:46 -0700361 .op(version=1, read="VlanVid.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="VlanVid.NONE") \
362 .op(version=2, read="VlanVid.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="VlanVid.NONE") \
363 .op(version=ANY, read="VlanVid.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="VlanVid.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700364vlan_pcp = JType("VlanPcp")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700365 .op(read="VlanPcp.readByte(bb)",
366 write="$name.writeByte(bb)",
367 default="VlanPcp.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700368ip_dscp = JType("IpDscp")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700369 .op(read="IpDscp.readByte(bb)",
370 write="$name.writeByte(bb)",
371 default="IpDscp.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700372ip_ecn = JType("IpEcn")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700373 .op(read="IpEcn.readByte(bb)",
374 write="$name.writeByte(bb)",
375 default="IpEcn.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700376ip_proto = JType("IpProtocol")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700377 .op(read="IpProtocol.readByte(bb)",
378 write="$name.writeByte(bb)",
379 default="IpProtocol.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700380icmpv4_type = JType("ICMPv4Type")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700381 .op(read="ICMPv4Type.readByte(bb)",
382 write="$name.writeByte(bb)",
383 default="ICMPv4Type.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700384icmpv4_code = JType("ICMPv4Code")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700385 .op(read="ICMPv4Code.readByte(bb)",
386 write="$name.writeByte(bb)",
387 default="ICMPv4Code.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700388arp_op = JType("ArpOpcode")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700389 .op(read="ArpOpcode.read2Bytes(bb)",
390 write="$name.write2Bytes(bb)",
391 default="ArpOpcode.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700392ipv6_flabel = JType("IPv6FlowLabel")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700393 .op(read="IPv6FlowLabel.read4Bytes(bb)",
394 write="$name.write4Bytes(bb)",
395 default="IPv6FlowLabel.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700396metadata = JType("OFMetadata")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700397 .op(read="OFMetadata.read8Bytes(bb)",
398 write="$name.write8Bytes(bb)",
399 default="OFMetadata.NONE")
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700400oxm = JType("OFOxm<?>")\
401 .op( read="OFOxmVer$version.READER.readFrom(bb)",
402 write="$name.writeTo(bb)")
403oxm_list = JType("OFOxmList") \
404 .op(
405 read= 'OFOxmList.readFrom(bb, $length, OFOxmVer$version.READER)', \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700406 write='$name.writeTo(bb)',
407 default="OFOxmList.EMPTY")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700408meter_features = JType("OFMeterFeatures")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700409 .op(read="OFMeterFeaturesVer$version.READER.readFrom(bb)",
410 write="$name.writeTo(bb)")
411flow_wildcards = JType("int") \
412 .op(read='bb.readInt()',
413 write='bb.writeInt($name)',
414 default="OFFlowWildcardsSerializerVer$version.ALL_VAL")
415table_stats_wildcards = JType("int") \
416 .op(read='bb.readInt()',
417 write='bb.writeInt($name)')
Yotam Harchol2c535582013-10-01 15:50:20 -0700418port_bitmap = JType('OFBitMask128') \
419 .op(read='OFBitMask128.read16Bytes(bb)',
Yotam Harchola11f38b2013-09-26 15:38:17 -0700420 write='$name.write16Bytes(bb)',
Yotam Harchol2c535582013-10-01 15:50:20 -0700421 default='OFBitMask128.NONE')
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700422table_id = JType("TableId") \
423 .op(read='TableId.readByte(bb)',
424 write='$name.writeByte(bb)',
425 default='TableId.ALL')
Andreas Wundsam45c95f82013-10-08 15:04:23 -0700426table_id_default_zero = JType("TableId") \
427 .op(read='TableId.readByte(bb)',
428 write='$name.writeByte(bb)',
429 default='TableId.ZERO')
Andreas Wundsama0981022013-10-02 18:15:06 -0700430of_version = JType("OFVersion", 'byte') \
431 .op(read='bb.readByte()', write='bb.writeByte($name)')
Rob Vaterlausb10ae552013-09-23 14:39:39 -0700432
Andreas Wundsam8ec3bcc2013-09-16 19:44:00 -0700433port_speed = JType("PortSpeed")
Rob Vaterlaus4d311942013-09-24 13:41:44 -0700434error_type = JType("OFErrorType")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700435of_type = JType("OFType", 'byte') \
436 .op(read='bb.readByte()', write='bb.writeByte($name)')
437action_type= gen_enum_jtype("OFActionType")\
438 .set_priv_type("short")\
439 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
440instruction_type = gen_enum_jtype("OFInstructionType")\
441 .set_priv_type('short') \
442 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
443buffer_id = JType("OFBufferId") \
444 .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700445boolean = JType("boolean", "byte") \
446 .op(read='(bb.readByte() != 0)',
447 write='bb.writeByte($name ? 1 : 0)',
448 default="false")
449datapath_id = JType("DatapathId") \
450 .op(read='DatapathId.of(bb.readLong())',
451 write='bb.writeLong($name.getLong())',
452 default='DatapathId.NONE')
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -0700453action_type_set = JType("Set<OFActionType>") \
454 .op(read='ChannelUtilsVer10.readSupportedActions(bb)',
455 write='ChannelUtilsVer10.writeSupportedActions(bb, $name)',
456 default='ImmutableSet.<OFActionType>of()',
457 funnel='ChannelUtilsVer10.putSupportedActionsTo($name, sink)')
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700458of_group = JType("OFGroup") \
459 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ALL")
460# the outgroup field of of_flow_stats_request has a special default value
461of_group_default_any = JType("OFGroup") \
462 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ANY")
463buffer_id = JType("OFBufferId") \
464 .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
Rich Lane376cafe2013-10-27 21:49:14 -0700465lag_id = JType("LagId") \
466 .op(version=ANY, read="LagId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="LagId.NONE")
Rich Laneeb21c4f2013-10-28 17:34:41 -0700467vrf = JType("VRF") \
468 .op(version=ANY, read="VRF.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="VRF.ZERO")
469class_id = JType("ClassId") \
470 .op(version=ANY, read="ClassId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="ClassId.NONE")
Rich Lane9c27b5d2013-10-30 17:14:32 -0700471boolean_value = JType('OFBooleanValue', 'OFBooleanValue') \
472 .op(read='OFBooleanValue.of(bb.readByte() != 0)', write='bb.writeByte($name.getInt())', default="OFBooleanValue.FALSE")
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700473
474generic_t = JType("T")
475
Andreas Wundsam27303462013-07-16 12:52:35 -0700476
477default_mtype_to_jtype_convert_map = {
478 'uint8_t' : u8,
479 'uint16_t' : u16,
480 'uint32_t' : u32,
481 'uint64_t' : u64,
Andreas Wundsam27303462013-07-16 12:52:35 -0700482 'of_port_no_t' : of_port,
483 'list(of_action_t)' : actions_list,
484 'list(of_instruction_t)' : instructions_list,
485 'list(of_bucket_t)': buckets_list,
486 'list(of_port_desc_t)' : port_desc_list,
487 'list(of_packet_queue_t)' : packet_queue_list,
488 'list(of_uint32_t)' : u32_list,
489 'list(of_uint8_t)' : u8_list,
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700490 'list(of_oxm_t)' : oxm_list,
Andreas Wundsam27303462013-07-16 12:52:35 -0700491 'of_octets_t' : octets,
492 'of_match_t': of_match,
493 'of_fm_cmd_t': flow_mod_cmd,
494 'of_mac_addr_t': mac_addr,
495 'of_port_desc_t': port_desc,
496 'of_desc_str_t': desc_str,
497 'of_serial_num_t': serial_num,
498 'of_port_name_t': port_name,
499 'of_table_name_t': table_name,
500 'of_ipv4_t': ipv4,
501 'of_ipv6_t': ipv6,
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700502 'of_wc_bmap_t': flow_wildcards,
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700503 'of_oxm_t': oxm,
504 'of_meter_features_t': meter_features,
Yotam Harchol595c6442013-09-27 16:29:08 -0700505 'of_bitmap_128_t': port_bitmap
Andreas Wundsam27303462013-07-16 12:52:35 -0700506 }
507
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700508## Map that defines exceptions from the standard loxi->java mapping scheme
509# map of {<loxi_class_name> : { <loxi_member_name> : <JType instance> } }
Andreas Wundsam27303462013-07-16 12:52:35 -0700510exceptions = {
Yotam Harcholc742e202013-08-15 12:16:24 -0700511 'of_packet_in': { 'data' : octets, 'reason': packetin_reason },
512 'of_oxm_tcp_src' : { 'value' : transport_port },
513 'of_oxm_tcp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
514 'of_oxm_tcp_dst' : { 'value' : transport_port },
515 'of_oxm_tcp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
516 'of_oxm_udp_src' : { 'value' : transport_port },
517 'of_oxm_udp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
518 'of_oxm_udp_dst' : { 'value' : transport_port },
519 'of_oxm_udp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
520 'of_oxm_sctp_src' : { 'value' : transport_port },
521 'of_oxm_sctp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
522 'of_oxm_sctp_dst' : { 'value' : transport_port },
523 'of_oxm_sctp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
524 'of_oxm_eth_type' : { 'value' : eth_type },
525 'of_oxm_eth_type_masked' : { 'value' : eth_type, 'value_mask' : eth_type },
526 'of_oxm_vlan_vid' : { 'value' : vlan_vid },
527 'of_oxm_vlan_vid_masked' : { 'value' : vlan_vid, 'value_mask' : vlan_vid },
528 'of_oxm_vlan_pcp' : { 'value' : vlan_pcp },
529 'of_oxm_vlan_pcp_masked' : { 'value' : vlan_pcp, 'value_mask' : vlan_pcp },
530 'of_oxm_ip_dscp' : { 'value' : ip_dscp },
531 'of_oxm_ip_dscp_masked' : { 'value' : ip_dscp, 'value_mask' : ip_dscp },
532 'of_oxm_ip_ecn' : { 'value' : ip_ecn },
533 'of_oxm_ip_ecn_masked' : { 'value' : ip_ecn, 'value_mask' : ip_ecn },
534 'of_oxm_ip_proto' : { 'value' : ip_proto },
535 'of_oxm_ip_proto_masked' : { 'value' : ip_proto, 'value_mask' : ip_proto },
536 'of_oxm_icmpv4_type' : { 'value' : icmpv4_type },
537 'of_oxm_icmpv4_type_masked' : { 'value' : icmpv4_type, 'value_mask' : icmpv4_type },
538 'of_oxm_icmpv4_code' : { 'value' : icmpv4_code },
539 'of_oxm_icmpv4_code_masked' : { 'value' : icmpv4_code, 'value_mask' : icmpv4_code },
540 'of_oxm_arp_op' : { 'value' : arp_op },
541 'of_oxm_arp_op_masked' : { 'value' : arp_op, 'value_mask' : arp_op },
542 'of_oxm_arp_spa' : { 'value' : ipv4 },
543 'of_oxm_arp_spa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 },
544 'of_oxm_arp_tpa' : { 'value' : ipv4 },
545 'of_oxm_arp_tpa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 },
546 'of_oxm_ipv6_flabel' : { 'value' : ipv6_flabel },
547 'of_oxm_ipv6_flabel_masked' : { 'value' : ipv6_flabel, 'value_mask' : ipv6_flabel },
548 'of_oxm_metadata' : { 'value' : metadata },
549 'of_oxm_metadata_masked' : { 'value' : metadata, 'value_mask' : metadata },
Yotam Harchol5804f772013-08-21 17:35:31 -0700550
551 'of_oxm_icmpv6_code' : { 'value' : u8obj },
552 'of_oxm_icmpv6_code_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
553 'of_oxm_icmpv6_type' : { 'value' : u8obj },
554 'of_oxm_icmpv6_type_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
555 'of_oxm_mpls_label' : { 'value' : u32obj },
556 'of_oxm_mpls_label_masked' : { 'value' : u32obj, 'value_mask' : u32obj },
557 'of_oxm_mpls_tc' : { 'value' : u8obj },
558 'of_oxm_mpls_tc_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700559
Yotam Harchol595c6442013-09-27 16:29:08 -0700560 'of_oxm_bsn_in_ports_128' : { 'value': port_bitmap },
561 'of_oxm_bsn_in_ports_128_masked' : { 'value': port_bitmap, 'value_mask': port_bitmap },
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700562
Rich Lane376cafe2013-10-27 21:49:14 -0700563 'of_oxm_bsn_lag_id' : { 'value' : lag_id },
564 'of_oxm_bsn_lag_id_masked' : { 'value' : lag_id, 'value_mask' : lag_id },
Rich Lane1424d0b2013-10-24 17:16:24 -0700565
Rich Laneeb21c4f2013-10-28 17:34:41 -0700566 'of_oxm_bsn_vrf' : { 'value' : vrf },
567 'of_oxm_bsn_vrf_masked' : { 'value' : vrf, 'value_mask' : vrf },
568
Rich Lane9c27b5d2013-10-30 17:14:32 -0700569 'of_oxm_bsn_global_vrf_allowed' : { 'value' : boolean_value },
570 'of_oxm_bsn_global_vrf_allowed_masked' : { 'value' : boolean_value, 'value_mask' : boolean_value },
Rich Laneeb21c4f2013-10-28 17:34:41 -0700571
572 'of_oxm_bsn_l3_interface_class_id' : { 'value' : class_id },
573 'of_oxm_bsn_l3_interface_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
574
575 'of_oxm_bsn_l3_src_class_id' : { 'value' : class_id },
576 'of_oxm_bsn_l3_src_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
577
578 'of_oxm_bsn_l3_dst_class_id' : { 'value' : class_id },
579 'of_oxm_bsn_l3_dst_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
580
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700581 'of_table_stats_entry': { 'wildcards': table_stats_wildcards },
582 'of_match_v1': { 'vlan_vid' : vlan_vid, 'vlan_pcp': vlan_pcp,
583 'eth_type': eth_type, 'ip_dscp': ip_dscp, 'ip_proto': ip_proto,
Andreas Wundsame962d372013-10-02 18:15:58 -0700584 'tcp_src': transport_port, 'tcp_dst': transport_port,
585 'in_port': of_port_match_v1
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700586 },
587 'of_bsn_set_l2_table_request': { 'l2_table_enable': boolean },
588 'of_bsn_set_l2_table_reply': { 'l2_table_enable': boolean },
Rob Vaterlause1b86842013-10-18 13:29:19 -0700589 'of_bsn_set_pktin_suppression_request': { 'enabled': boolean },
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700590 'of_flow_stats_request': { 'out_group': of_group_default_any },
Andreas Wundsam27303462013-07-16 12:52:35 -0700591}
592
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700593
594@memoize
595def enum_java_types():
596 enum_types = {}
597
598 for protocol in of_g.ir.values():
599 for enum in protocol.enums:
600 java_name = name_c_to_caps_camel(re.sub(r'_t$', "", enum.name))
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700601
602 enum_types[enum.name] = gen_enum_jtype(java_name, enum.is_bitmask)
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700603 return enum_types
604
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700605def make_match_field_jtype(sub_type_name="?"):
606 return JType("MatchField<{}>".format(sub_type_name))
607
Andreas Wundsam661a2222013-11-05 17:18:59 -0800608def make_oxm_jtype(sub_type_name="?"):
609 return JType("OFOxm<{}>".format(sub_type_name))
Andreas Wundsam27303462013-07-16 12:52:35 -0700610
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700611def list_cname_to_java_name(c_type):
Andreas Wundsam27303462013-07-16 12:52:35 -0700612 m = re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type)
613 if not m:
614 raise Exception("Not a recgonized standard list type declaration: %s" % c_type)
615 base_name = m.group(1)
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700616 return "OF" + name_c_to_caps_camel(base_name)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700617
618#### main entry point for conversion of LOXI types (c_types) Java types.
619# FIXME: This badly needs a refactoring
620
Andreas Wundsam27303462013-07-16 12:52:35 -0700621def convert_to_jtype(obj_name, field_name, c_type):
622 """ Convert from a C type ("uint_32") to a java type ("U32")
623 and return a JType object with the size, internal type, and marshalling functions"""
624 if obj_name in exceptions and field_name in exceptions[obj_name]:
625 return exceptions[obj_name][field_name]
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700626 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 -0700627 return of_type
Andreas Wundsam27303462013-07-16 12:52:35 -0700628 elif field_name == "type" and re.match(r'of_action.*', obj_name):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700629 return action_type
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700630 elif field_name == "err_type":
631 return JType("OFErrorType", 'short') \
632 .op(read='bb.readShort()', write='bb.writeShort($name)')
633 elif field_name == "stats_type":
634 return JType("OFStatsType", 'short') \
635 .op(read='bb.readShort()', write='bb.writeShort($name)')
Andreas Wundsam999c0732013-10-01 19:29:16 -0700636 elif field_name == "type" and re.match(r'of_instruction.*', obj_name):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700637 return instruction_type
Andreas Wundsam45c95f82013-10-08 15:04:23 -0700638 elif obj_name in ("of_flow_add", "of_flow_modify", "of_flow_modify_strict", "of_delete_strict") and field_name == "table_id" and c_type == "uint8_t":
639 return table_id_default_zero
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700640 elif field_name == "table_id" and c_type == "uint8_t":
641 return table_id
Andreas Wundsam27303462013-07-16 12:52:35 -0700642 elif field_name == "version" and c_type == "uint8_t":
Andreas Wundsama0981022013-10-02 18:15:06 -0700643 return of_version
Rob Vaterlausb10ae552013-09-23 14:39:39 -0700644 elif field_name == "buffer_id" and c_type == "uint32_t":
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700645 return buffer_id
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700646 elif field_name == "group_id" and c_type == "uint32_t":
647 return of_group
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700648 elif field_name == 'datapath_id':
649 return datapath_id
650 elif field_name == 'actions' and obj_name == 'of_features_reply':
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -0700651 return action_type_set
Andreas Wundsam27303462013-07-16 12:52:35 -0700652 elif c_type in default_mtype_to_jtype_convert_map:
653 return default_mtype_to_jtype_convert_map[c_type]
654 elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700655 return gen_list_jtype(list_cname_to_java_name(c_type))
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700656 elif c_type in enum_java_types():
657 return enum_java_types()[c_type]
Andreas Wundsam27303462013-07-16 12:52:35 -0700658 else:
659 print "WARN: Couldn't find java type conversion for '%s' in %s:%s" % (c_type, obj_name, field_name)
660 jtype = name_c_to_caps_camel(re.sub(r'_t$', "", c_type))
661 return JType(jtype)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700662
663
664#### Enum specific wiretype definitions
665enum_wire_types = {
666 "uint8_t": JType("byte").op(read="bb.readByte()", write="bb.writeByte($name)"),
667 "uint16_t": JType("short").op(read="bb.readShort()", write="bb.writeShort($name)"),
668 "uint32_t": JType("int").op(read="bb.readInt()", write="bb.writeInt($name)"),
669 "uint64_t": JType("long").op(read="bb.readLong()", write="bb.writeLong($name)"),
670}
671
672def convert_enum_wire_type_to_jtype(wire_type):
673 return enum_wire_types[wire_type]