Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 1 | import errno |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 2 | import os |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 3 | import re |
| 4 | import subprocess |
| 5 | import time |
| 6 | |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 7 | import loxi_globals |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 8 | from generic_utils import memoize |
| 9 | import loxi_utils.loxi_utils as loxi_utils |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 10 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 11 | def 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 18 | def 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 | |
| 26 | def name_c_to_caps_camel(name): |
| 27 | """ 'of_stats_reply' to 'OFStatsReply' """ |
| 28 | camel = name_c_to_camel(name.title()) |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 29 | if camel.startswith('Ofp'): |
| 30 | return camel.replace('Ofp','OF',1) |
| 31 | elif camel.startswith('Of'): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 32 | return camel.replace('Of','OF',1) |
| 33 | else: |
| 34 | return camel |
| 35 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 36 | java_primitive_types = set("boolean byte char short int long".split(" ")) |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 37 | |
| 38 | ### info table about java primitive types, for casting literals in the source code |
| 39 | # { name : (signed?, length_in_bits) } |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 40 | java_primitives_info = { |
Andreas Wundsam | 438a9c3 | 2013-10-07 16:18:52 -0700 | [diff] [blame] | 41 | 'boolean' : (False, 8, False), |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 42 | '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 Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 49 | def 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 Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 52 | appropriately for correct representation despite Java's |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 53 | signed-craziness |
| 54 | """ |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 55 | signed, bits, cast_needed = java_primitives_info[t] |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 56 | 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 Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 68 | return "%s0x%x%s" % ("(%s) " % t if cast_needed else "", value, "L" if t=="long" else "") |
| 69 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 70 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 71 | ANY = 0xFFFFFFFFFFFFFFFF |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 72 | |
| 73 | class VersionOp: |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 74 | def __init__(self, version=ANY, read=None, write=None, default=None, funnel=None): |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 75 | self.version = version |
| 76 | self.read = read |
| 77 | self.write = write |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 78 | self.default = default |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 79 | self.funnel = funnel |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 80 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 81 | def __str__(self): |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 82 | return "[Version: %d, Read: '%s', Write: '%s', Default: '%s', Funnel: '%s' ]" % (self.version, self.read, self.write, self.default, self.funnel ) |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 83 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 84 | ### FIXME: This class should really be cleaned up |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 85 | class JType(object): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 86 | """ 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 Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 91 | def __init__(self, pub_type, priv_type=None): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 92 | 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 Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 96 | self.ops = {} |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 97 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 98 | 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 Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 103 | """ |
| 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 Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 116 | pub_types = [ pub_type ] if pub_type is not ANY else [ False, True ] |
| 117 | for pub_type in pub_types: |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 118 | self.ops[(version, pub_type)] = VersionOp(version, read, write, default, funnel) |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 119 | return self |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 120 | |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 121 | def format_value(self, value, pub_type=True): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 122 | # 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 Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 125 | t = self.pub_type if pub_type else self.priv_type |
| 126 | if t in java_primitive_types: |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 127 | return format_primitive_literal(t, value) |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 128 | else: |
| 129 | return value |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 130 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 131 | @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 Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 144 | 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: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 148 | arguments["version"] = version.dotless_version |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 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 Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 162 | def read_op(self, version=None, length=None, pub_type=True): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 163 | """ 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 171 | if length is None: |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 172 | # 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 Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 175 | length = "length - (bb.readerIndex() - start)" |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 176 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 177 | 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 181 | |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 182 | def write_op(self, version=None, name=None, pub_type=True): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 183 | """ 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 Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 190 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 191 | 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 196 | |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 197 | 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 Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 202 | 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 Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 206 | |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 207 | def skip_op(self, version=None, length=None): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 208 | """ 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 Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 212 | return self.read_op(version, length) |
| 213 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 214 | 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 Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 221 | @property |
| 222 | def is_primitive(self): |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 223 | return self._is_primitive() |
| 224 | |
| 225 | def _is_primitive(self, pub_type=True): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 226 | """ return true if the pub_type is a java primitive type (and thus needs |
| 227 | special treatment, because it doesn't have methods)""" |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 228 | t = self.pub_type if pub_type else self.priv_type |
| 229 | return t in java_primitive_types |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 230 | |
| 231 | @property |
| 232 | def is_array(self): |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 233 | return self._is_array() |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 234 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 235 | 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}> |
| 240 | def 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 | |
| 257 | def 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 | |
| 269 | def 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 Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 276 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 277 | ##### Predefined JType mappings |
| 278 | # FIXME: This list needs to be pruned / cleaned up. Most of these are schematic. |
| 279 | |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 280 | u8 = JType('short', 'byte') \ |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 281 | .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 Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 283 | u8_list = JType('List<U8>') \ |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 284 | .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 Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 289 | u16 = 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) |
| 292 | u32 = 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) |
| 295 | u32_list = JType('List<U32>', 'int[]') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 296 | .op( |
| 297 | read='ChannelUtils.readList(bb, $length, U32.READER)', |
| 298 | write='ChannelUtils.writeList(bb, $name)', |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 299 | default="ImmutableList.<U32>of()", |
| 300 | funnel="FunnelUtils.putList($name, sink)") |
Yotam Harchol | 5804f77 | 2013-08-21 17:35:31 -0700 | [diff] [blame] | 301 | u8obj = JType('U8', 'U8') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 302 | .op(read='U8.of(bb.readByte())', write='bb.writeByte($name.getRaw())', default="U8.ZERO") |
Yotam Harchol | 5804f77 | 2013-08-21 17:35:31 -0700 | [diff] [blame] | 303 | u32obj = JType('U32', 'U32') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 304 | .op(read='U32.of(bb.readInt())', write='bb.writeInt($name.getRaw())', default="U32.ZERO") |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 305 | u64 = JType('U64', 'U64') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 306 | .op(read='U64.ofRaw(bb.readLong())', write='bb.writeLong($name.getValue())', default="U64.ZERO") |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 307 | of_port = JType("OFPort") \ |
Andreas Wundsam | ad499c9 | 2013-09-28 18:56:49 -0700 | [diff] [blame] | 308 | .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 Wundsam | e962d37 | 2013-10-02 18:15:58 -0700 | [diff] [blame] | 310 | # the same OFPort, but with a default value of ZERO, only for OF10 match |
| 311 | of_port_match_v1 = JType("OFPort") \ |
| 312 | .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ZERO") |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 313 | actions_list = gen_list_jtype("OFAction") |
| 314 | instructions_list = gen_list_jtype("OFInstruction") |
| 315 | buckets_list = gen_list_jtype("OFBucket") |
| 316 | port_desc_list = gen_list_jtype("OFPortDesc") |
| 317 | packet_queue_list = gen_list_jtype("OFPacketQueue") |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 318 | port_desc = JType('OFPortDesc') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 319 | .op(read='OFPortDescVer$version.READER.readFrom(bb)', \ |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 320 | write='$name.writeTo(bb)') |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 321 | octets = JType('byte[]')\ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 322 | .op(read='ChannelUtils.readBytes(bb, $length)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 323 | write='bb.writeBytes($name)', \ |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 324 | default="new byte[0]", |
| 325 | funnel="sink.putBytes($name)" |
| 326 | ); |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 327 | of_match = JType('Match') \ |
Andreas Wundsam | 951ada3 | 2013-08-01 22:05:38 -0700 | [diff] [blame] | 328 | .op(read='ChannelUtilsVer$version.readOFMatch(bb)', \ |
Andreas Wundsam | e962d37 | 2013-10-02 18:15:58 -0700 | [diff] [blame] | 329 | write='$name.writeTo(bb)', |
| 330 | default="OFFactoryVer$version.MATCH_WILDCARD_ALL"); |
Andreas Wundsam | 5812cf3 | 2013-11-15 13:51:24 -0800 | [diff] [blame] | 331 | group_mod_cmd = JType('OFGroupModCommand', 'short') \ |
| 332 | .op(version=ANY, read="bb.readShort()", write="bb.writeShort($name)") |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 333 | flow_mod_cmd = JType('OFFlowModCommand', 'short') \ |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 334 | .op(version=1, read="bb.readShort()", write="bb.writeShort($name)") \ |
| 335 | .op(version=ANY, read="bb.readByte()", write="bb.writeByte($name)") |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 336 | mac_addr = JType('MacAddress') \ |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 337 | .op(read="MacAddress.read6Bytes(bb)", \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 338 | write="$name.write6Bytes(bb)", |
| 339 | default="MacAddress.NONE") |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 340 | |
| 341 | port_name = gen_fixed_length_string_jtype(16) |
| 342 | desc_str = gen_fixed_length_string_jtype(256) |
| 343 | serial_num = gen_fixed_length_string_jtype(32) |
| 344 | table_name = gen_fixed_length_string_jtype(32) |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 345 | ipv4 = JType("IPv4Address") \ |
| 346 | .op(read="IPv4Address.read4Bytes(bb)", \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 347 | write="$name.write4Bytes(bb)", |
| 348 | default='IPv4Address.NONE') |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 349 | ipv6 = JType("IPv6Address") \ |
| 350 | .op(read="IPv6Address.read16Bytes(bb)", \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 351 | write="$name.write16Bytes(bb)", |
| 352 | default='IPv6Address.NONE') |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 353 | packetin_reason = gen_enum_jtype("OFPacketInReason") |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 354 | transport_port = JType("TransportPort")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 355 | .op(read="TransportPort.read2Bytes(bb)", |
| 356 | write="$name.write2Bytes(bb)", |
| 357 | default="TransportPort.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 358 | eth_type = JType("EthType")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 359 | .op(read="EthType.read2Bytes(bb)", |
| 360 | write="$name.write2Bytes(bb)", |
| 361 | default="EthType.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 362 | vlan_vid = JType("VlanVid")\ |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 363 | .op(version=ANY, read="VlanVid.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="VlanVid.ZERO") |
| 364 | vlan_vid_match = JType("OFVlanVidMatch")\ |
| 365 | .op(version=1, read="OFVlanVidMatch.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="OFVlanVidMatch.NONE") \ |
| 366 | .op(version=2, read="OFVlanVidMatch.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="OFVlanVidMatch.NONE") \ |
| 367 | .op(version=ANY, read="OFVlanVidMatch.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFVlanVidMatch.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 368 | vlan_pcp = JType("VlanPcp")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 369 | .op(read="VlanPcp.readByte(bb)", |
| 370 | write="$name.writeByte(bb)", |
| 371 | default="VlanPcp.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 372 | ip_dscp = JType("IpDscp")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 373 | .op(read="IpDscp.readByte(bb)", |
| 374 | write="$name.writeByte(bb)", |
| 375 | default="IpDscp.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 376 | ip_ecn = JType("IpEcn")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 377 | .op(read="IpEcn.readByte(bb)", |
| 378 | write="$name.writeByte(bb)", |
| 379 | default="IpEcn.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 380 | ip_proto = JType("IpProtocol")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 381 | .op(read="IpProtocol.readByte(bb)", |
| 382 | write="$name.writeByte(bb)", |
| 383 | default="IpProtocol.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 384 | icmpv4_type = JType("ICMPv4Type")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 385 | .op(read="ICMPv4Type.readByte(bb)", |
| 386 | write="$name.writeByte(bb)", |
| 387 | default="ICMPv4Type.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 388 | icmpv4_code = JType("ICMPv4Code")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 389 | .op(read="ICMPv4Code.readByte(bb)", |
| 390 | write="$name.writeByte(bb)", |
| 391 | default="ICMPv4Code.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 392 | arp_op = JType("ArpOpcode")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 393 | .op(read="ArpOpcode.read2Bytes(bb)", |
| 394 | write="$name.write2Bytes(bb)", |
| 395 | default="ArpOpcode.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 396 | ipv6_flabel = JType("IPv6FlowLabel")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 397 | .op(read="IPv6FlowLabel.read4Bytes(bb)", |
| 398 | write="$name.write4Bytes(bb)", |
| 399 | default="IPv6FlowLabel.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 400 | metadata = JType("OFMetadata")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 401 | .op(read="OFMetadata.read8Bytes(bb)", |
| 402 | write="$name.write8Bytes(bb)", |
| 403 | default="OFMetadata.NONE") |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 404 | oxm = JType("OFOxm<?>")\ |
| 405 | .op( read="OFOxmVer$version.READER.readFrom(bb)", |
| 406 | write="$name.writeTo(bb)") |
| 407 | oxm_list = JType("OFOxmList") \ |
| 408 | .op( |
| 409 | read= 'OFOxmList.readFrom(bb, $length, OFOxmVer$version.READER)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 410 | write='$name.writeTo(bb)', |
| 411 | default="OFOxmList.EMPTY") |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 412 | meter_features = JType("OFMeterFeatures")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 413 | .op(read="OFMeterFeaturesVer$version.READER.readFrom(bb)", |
| 414 | write="$name.writeTo(bb)") |
| 415 | flow_wildcards = JType("int") \ |
| 416 | .op(read='bb.readInt()', |
| 417 | write='bb.writeInt($name)', |
| 418 | default="OFFlowWildcardsSerializerVer$version.ALL_VAL") |
| 419 | table_stats_wildcards = JType("int") \ |
| 420 | .op(read='bb.readInt()', |
| 421 | write='bb.writeInt($name)') |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 422 | port_bitmap = JType('OFBitMask128') \ |
| 423 | .op(read='OFBitMask128.read16Bytes(bb)', |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 424 | write='$name.write16Bytes(bb)', |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 425 | default='OFBitMask128.NONE') |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 426 | table_id = JType("TableId") \ |
| 427 | .op(read='TableId.readByte(bb)', |
| 428 | write='$name.writeByte(bb)', |
| 429 | default='TableId.ALL') |
Andreas Wundsam | 45c95f8 | 2013-10-08 15:04:23 -0700 | [diff] [blame] | 430 | table_id_default_zero = JType("TableId") \ |
| 431 | .op(read='TableId.readByte(bb)', |
| 432 | write='$name.writeByte(bb)', |
| 433 | default='TableId.ZERO') |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 434 | of_version = JType("OFVersion", 'byte') \ |
| 435 | .op(read='bb.readByte()', write='bb.writeByte($name)') |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 436 | |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 437 | port_speed = JType("PortSpeed") |
Rob Vaterlaus | 4d31194 | 2013-09-24 13:41:44 -0700 | [diff] [blame] | 438 | error_type = JType("OFErrorType") |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 439 | of_type = JType("OFType", 'byte') \ |
| 440 | .op(read='bb.readByte()', write='bb.writeByte($name)') |
| 441 | action_type= gen_enum_jtype("OFActionType")\ |
| 442 | .set_priv_type("short")\ |
| 443 | .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False) |
| 444 | instruction_type = gen_enum_jtype("OFInstructionType")\ |
| 445 | .set_priv_type('short') \ |
| 446 | .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False) |
| 447 | buffer_id = JType("OFBufferId") \ |
| 448 | .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER") |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 449 | boolean = JType("boolean", "byte") \ |
| 450 | .op(read='(bb.readByte() != 0)', |
| 451 | write='bb.writeByte($name ? 1 : 0)', |
| 452 | default="false") |
| 453 | datapath_id = JType("DatapathId") \ |
| 454 | .op(read='DatapathId.of(bb.readLong())', |
| 455 | write='bb.writeLong($name.getLong())', |
| 456 | default='DatapathId.NONE') |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 457 | action_type_set = JType("Set<OFActionType>") \ |
| 458 | .op(read='ChannelUtilsVer10.readSupportedActions(bb)', |
| 459 | write='ChannelUtilsVer10.writeSupportedActions(bb, $name)', |
| 460 | default='ImmutableSet.<OFActionType>of()', |
| 461 | funnel='ChannelUtilsVer10.putSupportedActionsTo($name, sink)') |
Andreas Wundsam | acd57d5 | 2013-10-18 17:35:01 -0700 | [diff] [blame] | 462 | of_group = JType("OFGroup") \ |
| 463 | .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ALL") |
| 464 | # the outgroup field of of_flow_stats_request has a special default value |
| 465 | of_group_default_any = JType("OFGroup") \ |
| 466 | .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ANY") |
| 467 | buffer_id = JType("OFBufferId") \ |
| 468 | .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER") |
Rich Lane | 376cafe | 2013-10-27 21:49:14 -0700 | [diff] [blame] | 469 | lag_id = JType("LagId") \ |
| 470 | .op(version=ANY, read="LagId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="LagId.NONE") |
Rich Lane | eb21c4f | 2013-10-28 17:34:41 -0700 | [diff] [blame] | 471 | vrf = JType("VRF") \ |
| 472 | .op(version=ANY, read="VRF.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="VRF.ZERO") |
| 473 | class_id = JType("ClassId") \ |
| 474 | .op(version=ANY, read="ClassId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="ClassId.NONE") |
Rich Lane | 9c27b5d | 2013-10-30 17:14:32 -0700 | [diff] [blame] | 475 | boolean_value = JType('OFBooleanValue', 'OFBooleanValue') \ |
| 476 | .op(read='OFBooleanValue.of(bb.readByte() != 0)', write='bb.writeByte($name.getInt())', default="OFBooleanValue.FALSE") |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 477 | |
| 478 | generic_t = JType("T") |
| 479 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 480 | |
| 481 | default_mtype_to_jtype_convert_map = { |
| 482 | 'uint8_t' : u8, |
| 483 | 'uint16_t' : u16, |
| 484 | 'uint32_t' : u32, |
| 485 | 'uint64_t' : u64, |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 486 | 'of_port_no_t' : of_port, |
| 487 | 'list(of_action_t)' : actions_list, |
| 488 | 'list(of_instruction_t)' : instructions_list, |
| 489 | 'list(of_bucket_t)': buckets_list, |
| 490 | 'list(of_port_desc_t)' : port_desc_list, |
| 491 | 'list(of_packet_queue_t)' : packet_queue_list, |
| 492 | 'list(of_uint32_t)' : u32_list, |
| 493 | 'list(of_uint8_t)' : u8_list, |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 494 | 'list(of_oxm_t)' : oxm_list, |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 495 | 'of_octets_t' : octets, |
| 496 | 'of_match_t': of_match, |
| 497 | 'of_fm_cmd_t': flow_mod_cmd, |
| 498 | 'of_mac_addr_t': mac_addr, |
| 499 | 'of_port_desc_t': port_desc, |
| 500 | 'of_desc_str_t': desc_str, |
| 501 | 'of_serial_num_t': serial_num, |
| 502 | 'of_port_name_t': port_name, |
| 503 | 'of_table_name_t': table_name, |
| 504 | 'of_ipv4_t': ipv4, |
| 505 | 'of_ipv6_t': ipv6, |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 506 | 'of_wc_bmap_t': flow_wildcards, |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 507 | 'of_oxm_t': oxm, |
| 508 | 'of_meter_features_t': meter_features, |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 509 | 'of_bitmap_128_t': port_bitmap |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 512 | ## Map that defines exceptions from the standard loxi->java mapping scheme |
| 513 | # map of {<loxi_class_name> : { <loxi_member_name> : <JType instance> } } |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 514 | exceptions = { |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 515 | 'of_packet_in': { 'data' : octets, 'reason': packetin_reason }, |
| 516 | 'of_oxm_tcp_src' : { 'value' : transport_port }, |
| 517 | 'of_oxm_tcp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 518 | 'of_oxm_tcp_dst' : { 'value' : transport_port }, |
| 519 | 'of_oxm_tcp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 520 | 'of_oxm_udp_src' : { 'value' : transport_port }, |
| 521 | 'of_oxm_udp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 522 | 'of_oxm_udp_dst' : { 'value' : transport_port }, |
| 523 | 'of_oxm_udp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 524 | 'of_oxm_sctp_src' : { 'value' : transport_port }, |
| 525 | 'of_oxm_sctp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 526 | 'of_oxm_sctp_dst' : { 'value' : transport_port }, |
| 527 | 'of_oxm_sctp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 528 | 'of_oxm_eth_type' : { 'value' : eth_type }, |
| 529 | 'of_oxm_eth_type_masked' : { 'value' : eth_type, 'value_mask' : eth_type }, |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 530 | 'of_oxm_vlan_vid' : { 'value' : vlan_vid_match }, |
| 531 | 'of_oxm_vlan_vid_masked' : { 'value' : vlan_vid_match, 'value_mask' : vlan_vid_match }, |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 532 | 'of_oxm_vlan_pcp' : { 'value' : vlan_pcp }, |
| 533 | 'of_oxm_vlan_pcp_masked' : { 'value' : vlan_pcp, 'value_mask' : vlan_pcp }, |
| 534 | 'of_oxm_ip_dscp' : { 'value' : ip_dscp }, |
| 535 | 'of_oxm_ip_dscp_masked' : { 'value' : ip_dscp, 'value_mask' : ip_dscp }, |
| 536 | 'of_oxm_ip_ecn' : { 'value' : ip_ecn }, |
| 537 | 'of_oxm_ip_ecn_masked' : { 'value' : ip_ecn, 'value_mask' : ip_ecn }, |
| 538 | 'of_oxm_ip_proto' : { 'value' : ip_proto }, |
| 539 | 'of_oxm_ip_proto_masked' : { 'value' : ip_proto, 'value_mask' : ip_proto }, |
| 540 | 'of_oxm_icmpv4_type' : { 'value' : icmpv4_type }, |
| 541 | 'of_oxm_icmpv4_type_masked' : { 'value' : icmpv4_type, 'value_mask' : icmpv4_type }, |
| 542 | 'of_oxm_icmpv4_code' : { 'value' : icmpv4_code }, |
| 543 | 'of_oxm_icmpv4_code_masked' : { 'value' : icmpv4_code, 'value_mask' : icmpv4_code }, |
| 544 | 'of_oxm_arp_op' : { 'value' : arp_op }, |
| 545 | 'of_oxm_arp_op_masked' : { 'value' : arp_op, 'value_mask' : arp_op }, |
| 546 | 'of_oxm_arp_spa' : { 'value' : ipv4 }, |
| 547 | 'of_oxm_arp_spa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 }, |
| 548 | 'of_oxm_arp_tpa' : { 'value' : ipv4 }, |
| 549 | 'of_oxm_arp_tpa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 }, |
| 550 | 'of_oxm_ipv6_flabel' : { 'value' : ipv6_flabel }, |
| 551 | 'of_oxm_ipv6_flabel_masked' : { 'value' : ipv6_flabel, 'value_mask' : ipv6_flabel }, |
| 552 | 'of_oxm_metadata' : { 'value' : metadata }, |
| 553 | 'of_oxm_metadata_masked' : { 'value' : metadata, 'value_mask' : metadata }, |
Yotam Harchol | 5804f77 | 2013-08-21 17:35:31 -0700 | [diff] [blame] | 554 | |
| 555 | 'of_oxm_icmpv6_code' : { 'value' : u8obj }, |
| 556 | 'of_oxm_icmpv6_code_masked' : { 'value' : u8obj, 'value_mask' : u8obj }, |
| 557 | 'of_oxm_icmpv6_type' : { 'value' : u8obj }, |
| 558 | 'of_oxm_icmpv6_type_masked' : { 'value' : u8obj, 'value_mask' : u8obj }, |
| 559 | 'of_oxm_mpls_label' : { 'value' : u32obj }, |
| 560 | 'of_oxm_mpls_label_masked' : { 'value' : u32obj, 'value_mask' : u32obj }, |
| 561 | 'of_oxm_mpls_tc' : { 'value' : u8obj }, |
| 562 | 'of_oxm_mpls_tc_masked' : { 'value' : u8obj, 'value_mask' : u8obj }, |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 563 | |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 564 | 'of_oxm_bsn_in_ports_128' : { 'value': port_bitmap }, |
| 565 | 'of_oxm_bsn_in_ports_128_masked' : { 'value': port_bitmap, 'value_mask': port_bitmap }, |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 566 | |
Rich Lane | 376cafe | 2013-10-27 21:49:14 -0700 | [diff] [blame] | 567 | 'of_oxm_bsn_lag_id' : { 'value' : lag_id }, |
| 568 | 'of_oxm_bsn_lag_id_masked' : { 'value' : lag_id, 'value_mask' : lag_id }, |
Rich Lane | 1424d0b | 2013-10-24 17:16:24 -0700 | [diff] [blame] | 569 | |
Rich Lane | eb21c4f | 2013-10-28 17:34:41 -0700 | [diff] [blame] | 570 | 'of_oxm_bsn_vrf' : { 'value' : vrf }, |
| 571 | 'of_oxm_bsn_vrf_masked' : { 'value' : vrf, 'value_mask' : vrf }, |
| 572 | |
Rich Lane | 9c27b5d | 2013-10-30 17:14:32 -0700 | [diff] [blame] | 573 | 'of_oxm_bsn_global_vrf_allowed' : { 'value' : boolean_value }, |
| 574 | 'of_oxm_bsn_global_vrf_allowed_masked' : { 'value' : boolean_value, 'value_mask' : boolean_value }, |
Rich Lane | eb21c4f | 2013-10-28 17:34:41 -0700 | [diff] [blame] | 575 | |
| 576 | 'of_oxm_bsn_l3_interface_class_id' : { 'value' : class_id }, |
| 577 | 'of_oxm_bsn_l3_interface_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id }, |
| 578 | |
| 579 | 'of_oxm_bsn_l3_src_class_id' : { 'value' : class_id }, |
| 580 | 'of_oxm_bsn_l3_src_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id }, |
| 581 | |
| 582 | 'of_oxm_bsn_l3_dst_class_id' : { 'value' : class_id }, |
| 583 | 'of_oxm_bsn_l3_dst_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id }, |
| 584 | |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 585 | 'of_table_stats_entry': { 'wildcards': table_stats_wildcards }, |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 586 | 'of_match_v1': { 'vlan_vid' : vlan_vid_match, 'vlan_pcp': vlan_pcp, |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 587 | 'eth_type': eth_type, 'ip_dscp': ip_dscp, 'ip_proto': ip_proto, |
Andreas Wundsam | e962d37 | 2013-10-02 18:15:58 -0700 | [diff] [blame] | 588 | 'tcp_src': transport_port, 'tcp_dst': transport_port, |
| 589 | 'in_port': of_port_match_v1 |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 590 | }, |
| 591 | 'of_bsn_set_l2_table_request': { 'l2_table_enable': boolean }, |
| 592 | 'of_bsn_set_l2_table_reply': { 'l2_table_enable': boolean }, |
Rob Vaterlaus | e1b8684 | 2013-10-18 13:29:19 -0700 | [diff] [blame] | 593 | 'of_bsn_set_pktin_suppression_request': { 'enabled': boolean }, |
Andreas Wundsam | acd57d5 | 2013-10-18 17:35:01 -0700 | [diff] [blame] | 594 | 'of_flow_stats_request': { 'out_group': of_group_default_any }, |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 595 | |
| 596 | 'of_action_bsn_mirror': { 'dest_port': of_port }, |
| 597 | 'of_action_push_mpls': { 'ethertype': eth_type }, |
| 598 | 'of_action_push_pbb': { 'ethertype': eth_type }, |
| 599 | 'of_action_push_vlan': { 'ethertype': eth_type }, |
| 600 | 'of_action_set_nw_dst': { 'nw_addr': ipv4 }, |
| 601 | 'of_action_set_nw_ecn': { 'nw_ecn': ip_ecn }, |
| 602 | 'of_action_set_nw_src': { 'nw_addr': ipv4 }, |
| 603 | 'of_action_set_nw_dst': { 'tp_port': transport_port }, |
| 604 | 'of_action_set_tp_dst': { 'tp_port': transport_port }, |
| 605 | 'of_action_set_tp_src': { 'tp_port': transport_port }, |
| 606 | 'of_action_set_vlan_pcp': { 'vlan_pcp': vlan_pcp }, |
| 607 | 'of_action_set_vlan_vid': { 'vlan_vid': vlan_vid }, |
Andreas Wundsam | 5812cf3 | 2013-11-15 13:51:24 -0800 | [diff] [blame] | 608 | |
| 609 | 'of_group_mod' : { 'command' : group_mod_cmd }, |
| 610 | 'of_group_add' : { 'command' : group_mod_cmd }, |
| 611 | 'of_group_modify' : { 'command' : group_mod_cmd }, |
| 612 | 'of_group_delete' : { 'command' : group_mod_cmd }, |
| 613 | |
| 614 | 'of_bucket' : { 'watch_group': of_group }, |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 617 | |
| 618 | @memoize |
| 619 | def enum_java_types(): |
| 620 | enum_types = {} |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 621 | for enum in loxi_globals.unified.enums: |
| 622 | java_name = name_c_to_caps_camel(re.sub(r'_t$', "", enum.name)) |
| 623 | enum_types[enum.name] = gen_enum_jtype(java_name, enum.is_bitmask) |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 624 | return enum_types |
| 625 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 626 | def make_match_field_jtype(sub_type_name="?"): |
| 627 | return JType("MatchField<{}>".format(sub_type_name)) |
| 628 | |
Andreas Wundsam | 661a222 | 2013-11-05 17:18:59 -0800 | [diff] [blame] | 629 | def make_oxm_jtype(sub_type_name="?"): |
| 630 | return JType("OFOxm<{}>".format(sub_type_name)) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 631 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 632 | def list_cname_to_java_name(c_type): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 633 | m = re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type) |
| 634 | if not m: |
| 635 | raise Exception("Not a recgonized standard list type declaration: %s" % c_type) |
| 636 | base_name = m.group(1) |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 637 | return "OF" + name_c_to_caps_camel(base_name) |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 638 | |
| 639 | #### main entry point for conversion of LOXI types (c_types) Java types. |
| 640 | # FIXME: This badly needs a refactoring |
| 641 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 642 | def convert_to_jtype(obj_name, field_name, c_type): |
| 643 | """ Convert from a C type ("uint_32") to a java type ("U32") |
| 644 | and return a JType object with the size, internal type, and marshalling functions""" |
| 645 | if obj_name in exceptions and field_name in exceptions[obj_name]: |
| 646 | return exceptions[obj_name][field_name] |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 647 | elif ( obj_name == "of_header" or loxi_utils.class_is_message(obj_name)) and field_name == "type" and c_type == "uint8_t": |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 648 | return of_type |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 649 | elif field_name == "type" and re.match(r'of_action.*', obj_name): |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 650 | return action_type |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 651 | elif field_name == "err_type": |
| 652 | return JType("OFErrorType", 'short') \ |
| 653 | .op(read='bb.readShort()', write='bb.writeShort($name)') |
| 654 | elif field_name == "stats_type": |
| 655 | return JType("OFStatsType", 'short') \ |
| 656 | .op(read='bb.readShort()', write='bb.writeShort($name)') |
Andreas Wundsam | 999c073 | 2013-10-01 19:29:16 -0700 | [diff] [blame] | 657 | elif field_name == "type" and re.match(r'of_instruction.*', obj_name): |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 658 | return instruction_type |
Andreas Wundsam | 45c95f8 | 2013-10-08 15:04:23 -0700 | [diff] [blame] | 659 | 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": |
| 660 | return table_id_default_zero |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 661 | elif field_name == "table_id" and c_type == "uint8_t": |
| 662 | return table_id |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 663 | elif field_name == "version" and c_type == "uint8_t": |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 664 | return of_version |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 665 | elif field_name == "buffer_id" and c_type == "uint32_t": |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 666 | return buffer_id |
Andreas Wundsam | acd57d5 | 2013-10-18 17:35:01 -0700 | [diff] [blame] | 667 | elif field_name == "group_id" and c_type == "uint32_t": |
| 668 | return of_group |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 669 | elif field_name == 'datapath_id': |
| 670 | return datapath_id |
| 671 | elif field_name == 'actions' and obj_name == 'of_features_reply': |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 672 | return action_type_set |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 673 | elif c_type in default_mtype_to_jtype_convert_map: |
| 674 | return default_mtype_to_jtype_convert_map[c_type] |
| 675 | elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type): |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 676 | return gen_list_jtype(list_cname_to_java_name(c_type)) |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 677 | elif c_type in enum_java_types(): |
| 678 | return enum_java_types()[c_type] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 679 | else: |
| 680 | print "WARN: Couldn't find java type conversion for '%s' in %s:%s" % (c_type, obj_name, field_name) |
| 681 | jtype = name_c_to_caps_camel(re.sub(r'_t$', "", c_type)) |
| 682 | return JType(jtype) |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 683 | |
| 684 | |
| 685 | #### Enum specific wiretype definitions |
| 686 | enum_wire_types = { |
| 687 | "uint8_t": JType("byte").op(read="bb.readByte()", write="bb.writeByte($name)"), |
| 688 | "uint16_t": JType("short").op(read="bb.readShort()", write="bb.writeShort($name)"), |
| 689 | "uint32_t": JType("int").op(read="bb.readInt()", write="bb.writeInt($name)"), |
| 690 | "uint64_t": JType("long").op(read="bb.readLong()", write="bb.writeLong($name)"), |
| 691 | } |
| 692 | |
| 693 | def convert_enum_wire_type_to_jtype(wire_type): |
| 694 | return enum_wire_types[wire_type] |