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 | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 7 | from generic_utils import memoize |
| 8 | import loxi_utils.loxi_utils as loxi_utils |
| 9 | import of_g |
| 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 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 36 | java_primitive_types = set("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 = { |
| 41 | 'byte' : (True, 8), |
| 42 | 'char' : (False, 16), |
| 43 | 'short' : (True, 16), |
| 44 | 'int' : (True, 32), |
| 45 | 'long' : (True, 64), |
| 46 | } |
| 47 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 48 | def format_primitive_literal(t, value): |
| 49 | """ Format a primitive numeric literal for inclusion in the |
| 50 | java source code. Takes care of casting the literal |
| 51 | apropriately for correct representation despite Java's |
| 52 | signed-craziness |
| 53 | """ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 54 | signed, bits = java_primitives_info[t] |
| 55 | max = (1 << bits)-1 |
| 56 | if value > max: |
| 57 | raise Exception("Value %d to large for type %s" % (value, t)) |
| 58 | |
| 59 | if signed: |
| 60 | max_pos = (1 << (bits-1)) - 1 |
| 61 | |
| 62 | if value > max_pos: |
| 63 | if t == "long": |
| 64 | return str((1 << bits) - value) |
| 65 | else: |
| 66 | return "(%s) 0x%x" % (t, value) |
| 67 | else: |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 68 | return "0x%x%s" % (value, "L" if t=="long" else "") |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 69 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 70 | ANY = 0xFFFFFFFFFFFFFFFF |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 71 | |
| 72 | class VersionOp: |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 73 | def __init__(self, version=ANY, read=None, write=None, default=None): |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 74 | self.version = version |
| 75 | self.read = read |
| 76 | self.write = write |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 77 | self.default = default |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 78 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 79 | def __str__(self): |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 80 | return "[Version: %d, Read: '%s', Write: '%s', Default: '%s' ]" % (self.version, self.read, self.write, self.default ) |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 81 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 82 | ### FIXME: This class should really be cleaned up |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 83 | class JType(object): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 84 | """ Wrapper class to hold C to Java type conversion information. JTypes can have a 'public' |
| 85 | and or 'private' java type associated with them and can define how those types can be |
| 86 | read from and written to ChannelBuffers. |
| 87 | |
| 88 | """ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 89 | def __init__(self, pub_type, priv_type=None): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 90 | self.pub_type = pub_type # the type we expose externally, e.g. 'U8' |
| 91 | if priv_type is None: |
| 92 | priv_type = pub_type |
| 93 | self.priv_type = priv_type # the internal storage type |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 94 | self.ops = {} |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 95 | |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 96 | def op(self, version=ANY, read=None, write=None, default=None, pub_type=ANY): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 97 | """ |
| 98 | define operations to be performed for reading and writing this type |
| 99 | (when read_op, write_op is called). The operations 'read' and 'write' |
| 100 | can either be strings ($name, and $version and $length will be replaced), |
| 101 | or callables (name, version and length) will be passed. |
| 102 | |
| 103 | @param version int OF version to define operation for, or ANY for all |
| 104 | @param pub_type boolean whether to define operations for the public type (True), the |
| 105 | private type(False) or both (ALL) |
| 106 | @param read read expression (either string or callable)s |
| 107 | @param write write expression (either string or callable) |
| 108 | """ |
| 109 | |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 110 | pub_types = [ pub_type ] if pub_type is not ANY else [ False, True ] |
| 111 | for pub_type in pub_types: |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 112 | self.ops[(version, pub_type)] = VersionOp(version, read, write, default) |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 113 | return self |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 114 | |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 115 | def format_value(self, value, pub_type=True): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 116 | # Format a constant value of this type, for inclusion in the java source code |
| 117 | # For primitive types, takes care of casting the value appropriately, to |
| 118 | # cope with java's signedness limitation |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 119 | t = self.pub_type if pub_type else self.priv_type |
| 120 | if t in java_primitive_types: |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 121 | return format_primitive_literal(t, value) |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 122 | else: |
| 123 | return value |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 124 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 125 | @property |
| 126 | def public_type(self): |
| 127 | """ return the public type """ |
| 128 | return self.pub_type |
| 129 | |
| 130 | def priv(self): |
| 131 | """ return the public type """ |
| 132 | return self.priv_type |
| 133 | |
| 134 | def has_priv(self): |
| 135 | """ Is the private type different from the public one?""" |
| 136 | return self.pub_type != self.priv_type |
| 137 | |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 138 | def read_op(self, version=None, length=None, pub_type=True): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 139 | """ return a Java stanza that reads a value of this JType from ChannelBuffer bb. |
| 140 | @param version int - OF wire version to generate expression for |
| 141 | @param pub_type boolean use this JTypes 'public' (True), or private (False) representation |
| 142 | @param length string, for operations that need it (e.g., read a list of unknown length) |
| 143 | Java expression evaluating to the byte length to be read. Defaults to the remainig |
| 144 | length of the message. |
| 145 | @return string containing generated Java expression. |
| 146 | """ |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 147 | if length is None: |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 148 | # assumes that |
| 149 | # (1) length of the message has been read to 'length' |
| 150 | # (2) readerIndex at the start of the message has been stored in 'start' |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 151 | length = "length - (bb.readerIndex() - start)"; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 152 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 153 | ver = ANY if version is None else version.int_version |
| 154 | _read_op = None |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 155 | if (ver, pub_type) in self.ops: |
| 156 | _read_op = self.ops[(ver, pub_type)].read or self.ops[(ANY, pub_type)].read |
| 157 | elif (ANY, pub_type) in self.ops: |
| 158 | _read_op = self.ops[(ANY, pub_type)].read |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 159 | if _read_op is None: |
Andreas Wundsam | 951ada3 | 2013-08-01 22:05:38 -0700 | [diff] [blame] | 160 | _read_op = 'ChannelUtilsVer$version.read%s(bb)' % self.pub_type |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 161 | if callable(_read_op): |
| 162 | return _read_op(version) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 163 | else: |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 164 | return _read_op.replace("$length", str(length)).replace("$version", version.of_version) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 165 | |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 166 | def write_op(self, version=None, name=None, pub_type=True): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 167 | """ return a Java stanza that writes a value of this JType contained in Java expression |
| 168 | 'name' to ChannelBuffer bb. |
| 169 | @param name string containing Java expression that evaluations to the value to be written |
| 170 | @param version int - OF wire version to generate expression for |
| 171 | @param pub_type boolean use this JTypes 'public' (True), or private (False) representation |
| 172 | @return string containing generated Java expression. |
| 173 | """ |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 174 | ver = ANY if version is None else version.int_version |
| 175 | _write_op = None |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 176 | if (ver, pub_type) in self.ops: |
| 177 | _write_op = self.ops[(ver, pub_type)].write or self.ops[(ANY, pub_type)].write |
| 178 | elif (ANY, pub_type) in self.ops: |
| 179 | _write_op = self.ops[(ANY, pub_type)].write |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 180 | if _write_op is None: |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 181 | |
Andreas Wundsam | 951ada3 | 2013-08-01 22:05:38 -0700 | [diff] [blame] | 182 | _write_op = 'ChannelUtilsVer$version.write%s(bb, $name)' % self.pub_type |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 183 | if callable(_write_op): |
| 184 | return _write_op(version, name) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 185 | else: |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 186 | return _write_op.replace("$name", str(name)).replace("$version", version.of_version) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 187 | |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 188 | def default_op(self, version=None, pub_type=True): |
| 189 | """ return a Java stanza that returns a default value of this JType. |
| 190 | @param version JavaOFVersion |
| 191 | @return string containing generated Java expression. |
| 192 | """ |
| 193 | ver = ANY if version is None else version.int_version |
| 194 | _default_op = None |
| 195 | if (ver, pub_type) in self.ops: |
| 196 | _default_op = self.ops[(ver, pub_type)].default or self.ops[(ANY, pub_type)].default |
| 197 | elif (ANY, pub_type) in self.ops: |
| 198 | _default_op = self.ops[(ANY, pub_type)].default |
| 199 | if _default_op is None: |
| 200 | _default_op = self.format_value(0) if self.is_primitive else "null" |
| 201 | if callable(_default_op): |
| 202 | return _default_op(version, name) |
| 203 | else: |
| 204 | return _default_op.replace("$version", version.of_version) |
| 205 | |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 206 | def skip_op(self, version=None, length=None): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 207 | """ return a java stanza that skips an instance of JType in the input ChannelBuffer 'bb'. |
| 208 | This is used in the Reader implementations for virtual classes (because after the |
| 209 | discriminator field, the concrete Reader instance will re-read all the fields) |
| 210 | Currently just delegates to read_op + throws away the result.""" |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 211 | return self.read_op(version, length) |
| 212 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 213 | @property |
| 214 | def is_primitive(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 215 | """ return true if the pub_type is a java primitive type (and thus needs |
| 216 | special treatment, because it doesn't have methods)""" |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 217 | return self.pub_type in java_primitive_types |
| 218 | |
| 219 | @property |
| 220 | def is_array(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 221 | """ return true iff the pub_type is a Java array (and thus requires special |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 222 | treatment for equals / toString etc.) """ |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 223 | return self.pub_type.endswith("[]") |
| 224 | |
| 225 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 226 | ##### Predefined JType mappings |
| 227 | # FIXME: This list needs to be pruned / cleaned up. Most of these are schematic. |
| 228 | |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 229 | u8 = JType('short', 'byte') \ |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 230 | .op(read='U8.f(bb.readByte())', write='bb.writeByte(U8.t($name))', pub_type=True) \ |
| 231 | .op(read='bb.readByte()', write='bb.writeByte($name)', pub_type=False) |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 232 | u8_list = JType('List<U8>') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 233 | .op(read='ChannelUtils.readList(bb, $length, U8.READER)', write='ChannelUtils.writeList(bb, $name)') |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 234 | u16 = JType('int', 'short') \ |
| 235 | .op(read='U16.f(bb.readShort())', write='bb.writeShort(U16.t($name))', pub_type=True) \ |
| 236 | .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False) |
| 237 | u32 = JType('long', 'int') \ |
| 238 | .op(read='U32.f(bb.readInt())', write='bb.writeInt(U32.t($name))', pub_type=True) \ |
| 239 | .op(read='bb.readInt()', write='bb.writeInt($name)', pub_type=False) |
| 240 | u32_list = JType('List<U32>', 'int[]') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 241 | .op( |
| 242 | read='ChannelUtils.readList(bb, $length, U32.READER)', |
| 243 | write='ChannelUtils.writeList(bb, $name)', |
| 244 | default="ImmutableList.<U32>of()"); |
Yotam Harchol | 5804f77 | 2013-08-21 17:35:31 -0700 | [diff] [blame] | 245 | u8obj = JType('U8', 'U8') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 246 | .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] | 247 | u32obj = JType('U32', 'U32') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 248 | .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] | 249 | u64 = JType('U64', 'U64') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 250 | .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] | 251 | of_port = JType("OFPort") \ |
Andreas Wundsam | ad499c9 | 2013-09-28 18:56:49 -0700 | [diff] [blame] | 252 | .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ANY") \ |
| 253 | .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] | 254 | # the same OFPort, but with a default value of ZERO, only for OF10 match |
| 255 | of_port_match_v1 = JType("OFPort") \ |
| 256 | .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ZERO") |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 257 | actions_list = JType('List<OFAction>') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 258 | .op(read='ChannelUtils.readList(bb, $length, OFActionVer$version.READER)', |
| 259 | write='ChannelUtils.writeList(bb, $name);', |
| 260 | default='ImmutableList.<OFAction>of()') |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 261 | instructions_list = JType('List<OFInstruction>') \ |
| 262 | .op(read='ChannelUtils.readList(bb, $length, OFInstructionVer$version.READER)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 263 | write='ChannelUtils.writeList(bb, $name)', |
| 264 | default='ImmutableList.<OFInstruction>of()') |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 265 | buckets_list = JType('List<OFBucket>') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 266 | .op(read='ChannelUtils.readList(bb, $length, OFBucketVer$version.READER)', |
| 267 | write='ChannelUtils.writeList(bb, $name)', |
| 268 | default='ImmutableList.<OFBucket>of()') |
Rob Vaterlaus | 7db900a | 2013-09-17 14:18:49 -0700 | [diff] [blame] | 269 | port_desc_list = JType('List<OFPortDesc>') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 270 | .op(read='ChannelUtils.readList(bb, $length, OFPortDescVer$version.READER)', |
| 271 | write='ChannelUtils.writeList(bb, $name)', |
| 272 | default='ImmutableList.<OFPortDesc>of()') |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 273 | port_desc = JType('OFPortDesc') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 274 | .op(read='OFPortDescVer$version.READER.readFrom(bb)', \ |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 275 | write='$name.writeTo(bb)') |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 276 | packet_queue_list = JType('List<OFPacketQueue>') \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 277 | .op(read='ChannelUtils.readList(bb, $length, OFPacketQueueVer$version.READER)', |
| 278 | write='ChannelUtils.writeList(bb, $name);', |
| 279 | default='ImmutableList.<OFPacketQueue>of()') |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 280 | octets = JType('byte[]') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 281 | .op(read='ChannelUtils.readBytes(bb, $length)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 282 | write='bb.writeBytes($name)', \ |
| 283 | default="new byte[0]"); |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 284 | of_match = JType('Match') \ |
Andreas Wundsam | 951ada3 | 2013-08-01 22:05:38 -0700 | [diff] [blame] | 285 | .op(read='ChannelUtilsVer$version.readOFMatch(bb)', \ |
Andreas Wundsam | e962d37 | 2013-10-02 18:15:58 -0700 | [diff] [blame] | 286 | write='$name.writeTo(bb)', |
| 287 | default="OFFactoryVer$version.MATCH_WILDCARD_ALL"); |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 288 | flow_mod_cmd = JType('OFFlowModCommand', 'short') \ |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 289 | .op(version=1, read="bb.readShort()", write="bb.writeShort($name)") \ |
| 290 | .op(version=ANY, read="bb.readByte()", write="bb.writeByte($name)") |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 291 | mac_addr = JType('MacAddress') \ |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 292 | .op(read="MacAddress.read6Bytes(bb)", \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 293 | write="$name.write6Bytes(bb)", |
| 294 | default="MacAddress.NONE") |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 295 | port_name = JType('String') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 296 | .op(read='ChannelUtils.readFixedLengthString(bb, 16)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 297 | write='ChannelUtils.writeFixedLengthString(bb, $name, 16)', |
| 298 | default='""') |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 299 | desc_str = JType('String') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 300 | .op(read='ChannelUtils.readFixedLengthString(bb, 256)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 301 | write='ChannelUtils.writeFixedLengthString(bb, $name, 256)', |
| 302 | default='""') |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 303 | serial_num = JType('String') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 304 | .op(read='ChannelUtils.readFixedLengthString(bb, 32)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 305 | write='ChannelUtils.writeFixedLengthString(bb, $name, 32)', |
| 306 | default='""') |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 307 | table_name = JType('String') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 308 | .op(read='ChannelUtils.readFixedLengthString(bb, 32)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 309 | write='ChannelUtils.writeFixedLengthString(bb, $name, 32)', |
| 310 | default='""') |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 311 | ipv4 = JType("IPv4Address") \ |
| 312 | .op(read="IPv4Address.read4Bytes(bb)", \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 313 | write="$name.write4Bytes(bb)", |
| 314 | default='IPv4Address.NONE') |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 315 | ipv6 = JType("IPv6Address") \ |
| 316 | .op(read="IPv6Address.read16Bytes(bb)", \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 317 | write="$name.write16Bytes(bb)", |
| 318 | default='IPv6Address.NONE') |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 319 | packetin_reason = JType("OFPacketInReason")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 320 | .op(read="OFPacketInReasonSerializerVer$version.readFrom(bb)", |
| 321 | write="OFPacketInReasonSerializerVer$version.writeTo(bb, $name)") |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 322 | transport_port = JType("TransportPort")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 323 | .op(read="TransportPort.read2Bytes(bb)", |
| 324 | write="$name.write2Bytes(bb)", |
| 325 | default="TransportPort.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 326 | eth_type = JType("EthType")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 327 | .op(read="EthType.read2Bytes(bb)", |
| 328 | write="$name.write2Bytes(bb)", |
| 329 | default="EthType.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 330 | vlan_vid = JType("VlanVid")\ |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 331 | .op(version=1, read="VlanVid.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="VlanVid.NONE") \ |
| 332 | .op(version=2, read="VlanVid.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="VlanVid.NONE") \ |
| 333 | .op(version=ANY, read="VlanVid.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="VlanVid.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 334 | vlan_pcp = JType("VlanPcp")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 335 | .op(read="VlanPcp.readByte(bb)", |
| 336 | write="$name.writeByte(bb)", |
| 337 | default="VlanPcp.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 338 | ip_dscp = JType("IpDscp")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 339 | .op(read="IpDscp.readByte(bb)", |
| 340 | write="$name.writeByte(bb)", |
| 341 | default="IpDscp.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 342 | ip_ecn = JType("IpEcn")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 343 | .op(read="IpEcn.readByte(bb)", |
| 344 | write="$name.writeByte(bb)", |
| 345 | default="IpEcn.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 346 | ip_proto = JType("IpProtocol")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 347 | .op(read="IpProtocol.readByte(bb)", |
| 348 | write="$name.writeByte(bb)", |
| 349 | default="IpProtocol.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 350 | icmpv4_type = JType("ICMPv4Type")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 351 | .op(read="ICMPv4Type.readByte(bb)", |
| 352 | write="$name.writeByte(bb)", |
| 353 | default="ICMPv4Type.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 354 | icmpv4_code = JType("ICMPv4Code")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 355 | .op(read="ICMPv4Code.readByte(bb)", |
| 356 | write="$name.writeByte(bb)", |
| 357 | default="ICMPv4Code.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 358 | arp_op = JType("ArpOpcode")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 359 | .op(read="ArpOpcode.read2Bytes(bb)", |
| 360 | write="$name.write2Bytes(bb)", |
| 361 | default="ArpOpcode.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 362 | ipv6_flabel = JType("IPv6FlowLabel")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 363 | .op(read="IPv6FlowLabel.read4Bytes(bb)", |
| 364 | write="$name.write4Bytes(bb)", |
| 365 | default="IPv6FlowLabel.NONE") |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 366 | metadata = JType("OFMetadata")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 367 | .op(read="OFMetadata.read8Bytes(bb)", |
| 368 | write="$name.write8Bytes(bb)", |
| 369 | default="OFMetadata.NONE") |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 370 | oxm = JType("OFOxm<?>")\ |
| 371 | .op( read="OFOxmVer$version.READER.readFrom(bb)", |
| 372 | write="$name.writeTo(bb)") |
| 373 | oxm_list = JType("OFOxmList") \ |
| 374 | .op( |
| 375 | read= 'OFOxmList.readFrom(bb, $length, OFOxmVer$version.READER)', \ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 376 | write='$name.writeTo(bb)', |
| 377 | default="OFOxmList.EMPTY") |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 378 | meter_features = JType("OFMeterFeatures")\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 379 | .op(read="OFMeterFeaturesVer$version.READER.readFrom(bb)", |
| 380 | write="$name.writeTo(bb)") |
| 381 | flow_wildcards = JType("int") \ |
| 382 | .op(read='bb.readInt()', |
| 383 | write='bb.writeInt($name)', |
| 384 | default="OFFlowWildcardsSerializerVer$version.ALL_VAL") |
| 385 | table_stats_wildcards = JType("int") \ |
| 386 | .op(read='bb.readInt()', |
| 387 | write='bb.writeInt($name)') |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 388 | port_bitmap = JType('OFBitMask128') \ |
| 389 | .op(read='OFBitMask128.read16Bytes(bb)', |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 390 | write='$name.write16Bytes(bb)', |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 391 | default='OFBitMask128.NONE') |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 392 | table_id = JType("TableId") \ |
| 393 | .op(read='TableId.readByte(bb)', |
| 394 | write='$name.writeByte(bb)', |
| 395 | default='TableId.ALL') |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 396 | of_version = JType("OFVersion", 'byte') \ |
| 397 | .op(read='bb.readByte()', write='bb.writeByte($name)') |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 398 | |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 399 | port_speed = JType("PortSpeed") |
Rob Vaterlaus | 4d31194 | 2013-09-24 13:41:44 -0700 | [diff] [blame] | 400 | error_type = JType("OFErrorType") |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 401 | boolean = JType("boolean").op(default="false") |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 402 | |
| 403 | generic_t = JType("T") |
| 404 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 405 | |
| 406 | default_mtype_to_jtype_convert_map = { |
| 407 | 'uint8_t' : u8, |
| 408 | 'uint16_t' : u16, |
| 409 | 'uint32_t' : u32, |
| 410 | 'uint64_t' : u64, |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 411 | 'of_port_no_t' : of_port, |
| 412 | 'list(of_action_t)' : actions_list, |
| 413 | 'list(of_instruction_t)' : instructions_list, |
| 414 | 'list(of_bucket_t)': buckets_list, |
| 415 | 'list(of_port_desc_t)' : port_desc_list, |
| 416 | 'list(of_packet_queue_t)' : packet_queue_list, |
| 417 | 'list(of_uint32_t)' : u32_list, |
| 418 | 'list(of_uint8_t)' : u8_list, |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 419 | 'list(of_oxm_t)' : oxm_list, |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 420 | 'of_octets_t' : octets, |
| 421 | 'of_match_t': of_match, |
| 422 | 'of_fm_cmd_t': flow_mod_cmd, |
| 423 | 'of_mac_addr_t': mac_addr, |
| 424 | 'of_port_desc_t': port_desc, |
| 425 | 'of_desc_str_t': desc_str, |
| 426 | 'of_serial_num_t': serial_num, |
| 427 | 'of_port_name_t': port_name, |
| 428 | 'of_table_name_t': table_name, |
| 429 | 'of_ipv4_t': ipv4, |
| 430 | 'of_ipv6_t': ipv6, |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 431 | 'of_wc_bmap_t': flow_wildcards, |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 432 | 'of_oxm_t': oxm, |
| 433 | 'of_meter_features_t': meter_features, |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 434 | 'of_bitmap_128_t': port_bitmap |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 437 | ## Map that defines exceptions from the standard loxi->java mapping scheme |
| 438 | # map of {<loxi_class_name> : { <loxi_member_name> : <JType instance> } } |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 439 | exceptions = { |
Yotam Harchol | c742e20 | 2013-08-15 12:16:24 -0700 | [diff] [blame] | 440 | 'of_packet_in': { 'data' : octets, 'reason': packetin_reason }, |
| 441 | 'of_oxm_tcp_src' : { 'value' : transport_port }, |
| 442 | 'of_oxm_tcp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 443 | 'of_oxm_tcp_dst' : { 'value' : transport_port }, |
| 444 | 'of_oxm_tcp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 445 | 'of_oxm_udp_src' : { 'value' : transport_port }, |
| 446 | 'of_oxm_udp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 447 | 'of_oxm_udp_dst' : { 'value' : transport_port }, |
| 448 | 'of_oxm_udp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 449 | 'of_oxm_sctp_src' : { 'value' : transport_port }, |
| 450 | 'of_oxm_sctp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 451 | 'of_oxm_sctp_dst' : { 'value' : transport_port }, |
| 452 | 'of_oxm_sctp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port }, |
| 453 | 'of_oxm_eth_type' : { 'value' : eth_type }, |
| 454 | 'of_oxm_eth_type_masked' : { 'value' : eth_type, 'value_mask' : eth_type }, |
| 455 | 'of_oxm_vlan_vid' : { 'value' : vlan_vid }, |
| 456 | 'of_oxm_vlan_vid_masked' : { 'value' : vlan_vid, 'value_mask' : vlan_vid }, |
| 457 | 'of_oxm_vlan_pcp' : { 'value' : vlan_pcp }, |
| 458 | 'of_oxm_vlan_pcp_masked' : { 'value' : vlan_pcp, 'value_mask' : vlan_pcp }, |
| 459 | 'of_oxm_ip_dscp' : { 'value' : ip_dscp }, |
| 460 | 'of_oxm_ip_dscp_masked' : { 'value' : ip_dscp, 'value_mask' : ip_dscp }, |
| 461 | 'of_oxm_ip_ecn' : { 'value' : ip_ecn }, |
| 462 | 'of_oxm_ip_ecn_masked' : { 'value' : ip_ecn, 'value_mask' : ip_ecn }, |
| 463 | 'of_oxm_ip_proto' : { 'value' : ip_proto }, |
| 464 | 'of_oxm_ip_proto_masked' : { 'value' : ip_proto, 'value_mask' : ip_proto }, |
| 465 | 'of_oxm_icmpv4_type' : { 'value' : icmpv4_type }, |
| 466 | 'of_oxm_icmpv4_type_masked' : { 'value' : icmpv4_type, 'value_mask' : icmpv4_type }, |
| 467 | 'of_oxm_icmpv4_code' : { 'value' : icmpv4_code }, |
| 468 | 'of_oxm_icmpv4_code_masked' : { 'value' : icmpv4_code, 'value_mask' : icmpv4_code }, |
| 469 | 'of_oxm_arp_op' : { 'value' : arp_op }, |
| 470 | 'of_oxm_arp_op_masked' : { 'value' : arp_op, 'value_mask' : arp_op }, |
| 471 | 'of_oxm_arp_spa' : { 'value' : ipv4 }, |
| 472 | 'of_oxm_arp_spa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 }, |
| 473 | 'of_oxm_arp_tpa' : { 'value' : ipv4 }, |
| 474 | 'of_oxm_arp_tpa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 }, |
| 475 | 'of_oxm_ipv6_flabel' : { 'value' : ipv6_flabel }, |
| 476 | 'of_oxm_ipv6_flabel_masked' : { 'value' : ipv6_flabel, 'value_mask' : ipv6_flabel }, |
| 477 | 'of_oxm_metadata' : { 'value' : metadata }, |
| 478 | 'of_oxm_metadata_masked' : { 'value' : metadata, 'value_mask' : metadata }, |
Yotam Harchol | 5804f77 | 2013-08-21 17:35:31 -0700 | [diff] [blame] | 479 | |
| 480 | 'of_oxm_icmpv6_code' : { 'value' : u8obj }, |
| 481 | 'of_oxm_icmpv6_code_masked' : { 'value' : u8obj, 'value_mask' : u8obj }, |
| 482 | 'of_oxm_icmpv6_type' : { 'value' : u8obj }, |
| 483 | 'of_oxm_icmpv6_type_masked' : { 'value' : u8obj, 'value_mask' : u8obj }, |
| 484 | 'of_oxm_mpls_label' : { 'value' : u32obj }, |
| 485 | 'of_oxm_mpls_label_masked' : { 'value' : u32obj, 'value_mask' : u32obj }, |
| 486 | 'of_oxm_mpls_tc' : { 'value' : u8obj }, |
| 487 | 'of_oxm_mpls_tc_masked' : { 'value' : u8obj, 'value_mask' : u8obj }, |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 488 | |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 489 | 'of_oxm_bsn_in_ports_128' : { 'value': port_bitmap }, |
| 490 | '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] | 491 | |
| 492 | 'of_table_stats_entry': { 'wildcards': table_stats_wildcards }, |
| 493 | 'of_match_v1': { 'vlan_vid' : vlan_vid, 'vlan_pcp': vlan_pcp, |
| 494 | 'eth_type': eth_type, 'ip_dscp': ip_dscp, 'ip_proto': ip_proto, |
Andreas Wundsam | e962d37 | 2013-10-02 18:15:58 -0700 | [diff] [blame] | 495 | 'tcp_src': transport_port, 'tcp_dst': transport_port, |
| 496 | 'in_port': of_port_match_v1 |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 497 | } |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 500 | |
| 501 | @memoize |
| 502 | def enum_java_types(): |
| 503 | enum_types = {} |
| 504 | |
| 505 | for protocol in of_g.ir.values(): |
| 506 | for enum in protocol.enums: |
| 507 | java_name = name_c_to_caps_camel(re.sub(r'_t$', "", enum.name)) |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 508 | if enum.is_bitmask: |
| 509 | java_type = "Set<{}>".format(java_name) |
| 510 | default_value = "ImmutableSet.<{}>of()".format(java_name) |
| 511 | else: |
| 512 | java_type = java_name |
| 513 | default_value = "null" |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 514 | enum_types[enum.name] = \ |
| 515 | JType(java_type)\ |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 516 | .op(read="{}SerializerVer$version.readFrom(bb)".format(java_name), |
| 517 | write="{}SerializerVer$version.writeTo(bb, $name)".format(java_name), |
| 518 | default=default_value) |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 519 | return enum_types |
| 520 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 521 | def make_match_field_jtype(sub_type_name="?"): |
| 522 | return JType("MatchField<{}>".format(sub_type_name)) |
| 523 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 524 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 525 | # Create a default mapping for a list type. Type defauls to List<${java_mapping_of_name}> |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 526 | def make_standard_list_jtype(c_type): |
| 527 | m = re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type) |
| 528 | if not m: |
| 529 | raise Exception("Not a recgonized standard list type declaration: %s" % c_type) |
| 530 | base_name = m.group(1) |
| 531 | java_base_name = name_c_to_caps_camel(base_name) |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 532 | |
| 533 | # read op assumes the class has a public final static field READER that implements |
| 534 | # OFMessageReader<$class> i.e., can deserialize an instance of class from a ChannelBuffer |
| 535 | # write op assumes class implements Writeable |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 536 | return JType("List<OF{}>".format(java_base_name)) \ |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 537 | .op( |
Andreas Wundsam | b3ed3ff | 2013-09-23 14:46:29 -0700 | [diff] [blame] | 538 | read= 'ChannelUtils.readList(bb, $length, OF{}Ver$version.READER)'.format(java_base_name), \ |
| 539 | write='ChannelUtils.writeList(bb, $name)', |
| 540 | default="ImmutableList.<OF{}>of()".format(java_base_name) |
| 541 | ) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 542 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 543 | |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 544 | |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 545 | #### main entry point for conversion of LOXI types (c_types) Java types. |
| 546 | # FIXME: This badly needs a refactoring |
| 547 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 548 | def convert_to_jtype(obj_name, field_name, c_type): |
| 549 | """ Convert from a C type ("uint_32") to a java type ("U32") |
| 550 | and return a JType object with the size, internal type, and marshalling functions""" |
| 551 | if obj_name in exceptions and field_name in exceptions[obj_name]: |
| 552 | return exceptions[obj_name][field_name] |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 553 | elif ( obj_name == "of_header" or loxi_utils.class_is_message(obj_name)) and field_name == "type" and c_type == "uint8_t": |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 554 | return JType("OFType", 'byte') \ |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 555 | .op(read='bb.readByte()', write='bb.writeByte($name)') |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 556 | elif field_name == "type" and re.match(r'of_action.*', obj_name): |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 557 | return JType("OFActionType", 'short') \ |
Andreas Wundsam | 46d230f | 2013-08-02 22:24:06 -0700 | [diff] [blame] | 558 | .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)\ |
| 559 | .op(read="OFActionTypeSerializerVer$version.readFrom(bb)", write="OFActionTypeSerializerVer$version.writeTo(bb, $name)", pub_type=True) |
Andreas Wundsam | 999c073 | 2013-10-01 19:29:16 -0700 | [diff] [blame] | 560 | elif field_name == "type" and re.match(r'of_instruction.*', obj_name): |
| 561 | return JType("OFInstructionType", 'short') \ |
| 562 | .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)\ |
| 563 | .op(read="OFInstructionTypeSerializerVer$version.readFrom(bb)", write="OFInstructionTypeSerializerVer$version.writeTo(bb, $name)", pub_type=True) |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 564 | elif field_name == "table_id" and c_type == "uint8_t": |
| 565 | return table_id |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 566 | elif field_name == "version" and c_type == "uint8_t": |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 567 | return of_version |
Rob Vaterlaus | b10ae55 | 2013-09-23 14:39:39 -0700 | [diff] [blame] | 568 | elif field_name == "buffer_id" and c_type == "uint32_t": |
| 569 | return JType("OFBufferId") \ |
Rob Vaterlaus | fbd5b6b | 2013-09-24 15:55:47 -0700 | [diff] [blame] | 570 | .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER") |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 571 | elif c_type in default_mtype_to_jtype_convert_map: |
| 572 | return default_mtype_to_jtype_convert_map[c_type] |
| 573 | elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type): |
| 574 | return make_standard_list_jtype(c_type) |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 575 | elif c_type in enum_java_types(): |
| 576 | return enum_java_types()[c_type] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 577 | else: |
| 578 | print "WARN: Couldn't find java type conversion for '%s' in %s:%s" % (c_type, obj_name, field_name) |
| 579 | jtype = name_c_to_caps_camel(re.sub(r'_t$', "", c_type)) |
| 580 | return JType(jtype) |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 581 | |
| 582 | |
| 583 | #### Enum specific wiretype definitions |
| 584 | enum_wire_types = { |
| 585 | "uint8_t": JType("byte").op(read="bb.readByte()", write="bb.writeByte($name)"), |
| 586 | "uint16_t": JType("short").op(read="bb.readShort()", write="bb.writeShort($name)"), |
| 587 | "uint32_t": JType("int").op(read="bb.readInt()", write="bb.writeInt($name)"), |
| 588 | "uint64_t": JType("long").op(read="bb.readLong()", write="bb.writeLong($name)"), |
| 589 | } |
| 590 | |
| 591 | def convert_enum_wire_type_to_jtype(wire_type): |
| 592 | return enum_wire_types[wire_type] |