blob: fa97a7944ffa8fe5c601ed4593b172063622719e [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001#!/usr/bin/env python
2# Copyright 2013, Big Switch Networks, Inc.
3#
4# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
5# the following special exception:
6#
7# LOXI Exception
8#
9# As a special exception to the terms of the EPL, you may distribute libraries
10# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
11# that copyright and licensing notices generated by LoxiGen are not altered or removed
12# from the LoxiGen Libraries and the notice provided below is (i) included in
13# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
14# documentation for the LoxiGen Libraries, if distributed in binary form.
15#
16# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
17#
18# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
19# a copy of the EPL at:
20#
21# http://www.eclipse.org/legal/epl-v10.html
22#
23# Unless required by applicable law or agreed to in writing, software
24# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26# EPL for the specific language governing permissions and limitations
27# under the EPL.
28import unittest
Rich Lane9a3f1fd2013-05-10 16:29:17 -070029import test_data
30from testutil import test_datafile
Rich Lanea06d0c32013-03-25 08:52:03 -070031
32try:
Rich Lanefb7de0e2013-03-29 16:50:29 -070033 import loxi.of10 as ofp
Rich Lane57026dc2013-05-01 10:13:16 -070034 from loxi.generic_util import OFReader
Rich Lanea06d0c32013-03-25 08:52:03 -070035except ImportError:
36 exit("loxi package not found. Try setting PYTHONPATH.")
37
38class TestImports(unittest.TestCase):
39 def test_toplevel(self):
40 import loxi
41 self.assertTrue(hasattr(loxi, "ProtocolError"))
Rich Lane00549ea2013-04-25 13:33:16 -070042 self.assertEquals(loxi.version_names[1], "1.0")
Rich Lanea06d0c32013-03-25 08:52:03 -070043 ofp = loxi.protocol(1)
44 self.assertEquals(ofp.OFP_VERSION, 1)
45 self.assertTrue(hasattr(ofp, "action"))
46 self.assertTrue(hasattr(ofp, "common"))
47 self.assertTrue(hasattr(ofp, "const"))
48 self.assertTrue(hasattr(ofp, "message"))
49
50 def test_version(self):
Rich Lanea94fb082013-04-04 17:04:17 -070051 import loxi
Rich Lanea06d0c32013-03-25 08:52:03 -070052 self.assertTrue(hasattr(loxi.of10, "ProtocolError"))
53 self.assertTrue(hasattr(loxi.of10, "OFP_VERSION"))
54 self.assertEquals(loxi.of10.OFP_VERSION, 1)
55 self.assertTrue(hasattr(loxi.of10, "action"))
56 self.assertTrue(hasattr(loxi.of10, "common"))
57 self.assertTrue(hasattr(loxi.of10, "const"))
58 self.assertTrue(hasattr(loxi.of10, "message"))
59
60class TestActions(unittest.TestCase):
Rich Lane9a3f1fd2013-05-10 16:29:17 -070061 def test_output(self):
62 test_datafile('action_output.data')
Rich Lanea06d0c32013-03-25 08:52:03 -070063
64 def test_output_equality(self):
Rich Lanea06d0c32013-03-25 08:52:03 -070065 action = ofp.action.output(port=1, max_len=0x1234)
66 action2 = ofp.action.output(port=1, max_len=0x1234)
67 self.assertEquals(action, action2)
68
69 action2.port = 2
70 self.assertNotEquals(action, action2)
71 action2.port = 1
72
73 action2.max_len = 0xffff
74 self.assertNotEquals(action, action2)
75 action2.max_len = 0x1234
76
Rich Lane9a3f1fd2013-05-10 16:29:17 -070077 def test_bsn_set_tunnel_dst(self):
78 test_datafile('of10/action_bsn_set_tunnel_dst.data')
Rich Lanea06d0c32013-03-25 08:52:03 -070079
80# Assumes action serialization/deserialization works
81class TestActionList(unittest.TestCase):
82 def test_normal(self):
Rich Lanea06d0c32013-03-25 08:52:03 -070083 expected = []
84 bufs = []
85
86 def add(action):
87 expected.append(action)
88 bufs.append(action.pack())
89
90 add(ofp.action.output(port=1, max_len=0xffff))
91 add(ofp.action.output(port=2, max_len=0xffff))
92 add(ofp.action.output(port=ofp.OFPP_IN_PORT, max_len=0xffff))
93 add(ofp.action.bsn_set_tunnel_dst(dst=0x12345678))
94 add(ofp.action.nicira_dec_ttl())
95
Rich Lane57026dc2013-05-01 10:13:16 -070096 actions = ofp.action.unpack_list(OFReader(''.join(bufs)))
Rich Lanea06d0c32013-03-25 08:52:03 -070097 self.assertEquals(actions, expected)
98
99 def test_empty_list(self):
Rich Lane57026dc2013-05-01 10:13:16 -0700100 self.assertEquals(ofp.action.unpack_list(OFReader('')), [])
Rich Lanea06d0c32013-03-25 08:52:03 -0700101
102 def test_invalid_list_length(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700103 buf = '\x00' * 9
Rich Lane57026dc2013-05-01 10:13:16 -0700104 with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'):
105 ofp.action.unpack_list(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700106
107 def test_invalid_action_length(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700108 buf = '\x00' * 8
Rich Lane57026dc2013-05-01 10:13:16 -0700109 with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'):
110 ofp.action.unpack_list(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700111
112 buf = '\x00\x00\x00\x04'
Rich Lane57026dc2013-05-01 10:13:16 -0700113 with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'):
114 ofp.action.unpack_list(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700115
116 buf = '\x00\x00\x00\x10\x00\x00\x00\x00'
Rich Lane57026dc2013-05-01 10:13:16 -0700117 with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'):
118 ofp.action.unpack_list(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700119
120 def test_invalid_action_type(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700121 buf = '\xff\xfe\x00\x08\x00\x00\x00\x00'
122 with self.assertRaisesRegexp(ofp.ProtocolError, 'unknown action type'):
Rich Lane57026dc2013-05-01 10:13:16 -0700123 ofp.action.unpack_list(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700124
125class TestConstants(unittest.TestCase):
126 def test_ports(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700127 self.assertEquals(0xffff, ofp.OFPP_NONE)
128
129 def test_wildcards(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700130 self.assertEquals(0xfc000, ofp.OFPFW_NW_DST_MASK)
131
132class TestCommon(unittest.TestCase):
Rich Lane9a3f1fd2013-05-10 16:29:17 -0700133 def test_port_desc(self):
134 test_datafile('of10/port_desc.data')
Rich Lanea06d0c32013-03-25 08:52:03 -0700135
Rich Lane9a3f1fd2013-05-10 16:29:17 -0700136 def test_table_stats_entry(self):
137 test_datafile('of10/table_stats_entry.data')
Rich Lanea06d0c32013-03-25 08:52:03 -0700138
Rich Lane9a3f1fd2013-05-10 16:29:17 -0700139 def test_flow_stats_entry(self):
140 test_datafile('of10/flow_stats_entry.data')
Rich Lanea06d0c32013-03-25 08:52:03 -0700141
142 def test_match(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700143 match = ofp.match()
144 self.assertEquals(match.wildcards, ofp.OFPFW_ALL)
145 self.assertEquals(match.tcp_src, 0)
146 buf = match.pack()
147 match2 = ofp.match.unpack(buf)
148 self.assertEquals(match, match2)
149
150class TestMessages(unittest.TestCase):
151 def test_hello_construction(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700152 msg = ofp.message.hello()
153 self.assertEquals(msg.version, ofp.OFP_VERSION)
154 self.assertEquals(msg.type, ofp.OFPT_HELLO)
155 self.assertEquals(msg.xid, None)
156
157 msg = ofp.message.hello(xid=123)
158 self.assertEquals(msg.xid, 123)
159
160 # 0 is a valid xid distinct from None
161 msg = ofp.message.hello(xid=0)
162 self.assertEquals(msg.xid, 0)
163
Rich Lane9a3f1fd2013-05-10 16:29:17 -0700164 def test_hello(self):
165 test_datafile('of10/hello.data')
Rich Lanea06d0c32013-03-25 08:52:03 -0700166
167 def test_echo_request_construction(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700168 msg = ofp.message.echo_request(data="abc")
169 self.assertEquals(msg.data, "abc")
170
Rich Lane9a3f1fd2013-05-10 16:29:17 -0700171 def test_echo_request(self):
172 test_datafile('of10/echo_request.data')
Rich Lanea06d0c32013-03-25 08:52:03 -0700173
174 # Invalid length
175 buf = "\x01\x02\x00\x07\x12\x34\x56"
176 with self.assertRaisesRegexp(ofp.ProtocolError, "buffer too short"):
177 ofp.message.echo_request.unpack(buf)
178
179 def test_echo_request_equality(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700180 msg = ofp.message.echo_request(xid=0x12345678, data="abc")
181 msg2 = ofp.message.echo_request(xid=0x12345678, data="abc")
182 #msg2 = ofp.message.echo_request.unpack(msg.pack())
183 self.assertEquals(msg, msg2)
184
185 msg2.xid = 1
186 self.assertNotEquals(msg, msg2)
187 msg2.xid = msg.xid
188
189 msg2.data = "a"
190 self.assertNotEquals(msg, msg2)
191 msg2.data = msg.data
192
Rich Lanea06d0c32013-03-25 08:52:03 -0700193 def test_flow_add(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700194 match = ofp.match()
195 msg = ofp.message.flow_add(xid=1,
196 match=match,
197 cookie=1,
198 idle_timeout=5,
199 flags=ofp.OFPFF_CHECK_OVERLAP,
200 actions=[
201 ofp.action.output(port=1),
202 ofp.action.output(port=2),
203 ofp.action.output(port=ofp.OFPP_CONTROLLER,
204 max_len=1024)])
205 buf = msg.pack()
206 msg2 = ofp.message.flow_add.unpack(buf)
207 self.assertEquals(msg, msg2)
208
209 def test_port_mod_pack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700210 msg = ofp.message.port_mod(xid=2,
211 port_no=ofp.OFPP_CONTROLLER,
212 hw_addr=[1,2,3,4,5,6],
213 config=0x90ABCDEF,
214 mask=0xFF11FF11,
215 advertise=0xCAFE6789)
216 expected = "\x01\x0f\x00\x20\x00\x00\x00\x02\xff\xfd\x01\x02\x03\x04\x05\x06\x90\xab\xcd\xef\xff\x11\xff\x11\xca\xfe\x67\x89\x00\x00\x00\x00"
217 self.assertEquals(expected, msg.pack())
218
219 def test_desc_stats_reply_pack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700220 msg = ofp.message.desc_stats_reply(xid=3,
221 flags=ofp.OFPSF_REPLY_MORE,
222 mfr_desc="The Indigo-2 Community",
223 hw_desc="Unknown server",
224 sw_desc="Indigo-2 LRI pre-release",
225 serial_num="11235813213455",
226 dp_desc="Indigo-2 LRI forwarding module")
227 expected = ''.join([
228 '\x01', '\x11', # version/type
229 '\x04\x2c', # length
230 '\x00\x00\x00\x03', # xid
231 '\x00\x00', # stats_type
232 '\x00\x01', # flags
233 'The Indigo-2 Community'.ljust(256, '\x00'), # mfr_desc
234 'Unknown server'.ljust(256, '\x00'), # hw_desc
235 'Indigo-2 LRI pre-release'.ljust(256, '\x00'), # sw_desc
236 '11235813213455'.ljust(32, '\x00'), # serial_num
237 'Indigo-2 LRI forwarding module'.ljust(256, '\x00'), # dp_desc
238 ])
239 self.assertEquals(expected, msg.pack())
240
241 def test_desc_stats_reply_unpack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700242 buf = ''.join([
243 '\x01', '\x11', # version/type
244 '\x04\x2c', # length
245 '\x00\x00\x00\x03', # xid
246 '\x00\x00', # stats_type
247 '\x00\x01', # flags
248 'The Indigo-2 Community'.ljust(256, '\x00'), # mfr_desc
249 'Unknown server'.ljust(256, '\x00'), # hw_desc
250 'Indigo-2 LRI pre-release'.ljust(256, '\x00'), # sw_desc
251 '11235813213455'.ljust(32, '\x00'), # serial_num
252 'Indigo-2 LRI forwarding module'.ljust(256, '\x00'), # dp_desc
253 ])
254 msg = ofp.message.desc_stats_reply.unpack(buf)
255 self.assertEquals('Indigo-2 LRI forwarding module', msg.dp_desc)
256 self.assertEquals('11235813213455', msg.serial_num)
257 self.assertEquals(ofp.OFPST_DESC, msg.stats_type)
258 self.assertEquals(ofp.OFPSF_REPLY_MORE, msg.flags)
259
260 def test_port_status_pack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700261 desc = ofp.port_desc(port_no=ofp.OFPP_CONTROLLER,
262 hw_addr=[1,2,3,4,5,6],
263 name="foo",
264 config=ofp.OFPPC_NO_FLOOD,
265 state=ofp.OFPPS_STP_FORWARD,
266 curr=ofp.OFPPF_10MB_HD,
267 advertised=ofp.OFPPF_1GB_FD,
268 supported=ofp.OFPPF_AUTONEG,
269 peer=ofp.OFPPF_PAUSE_ASYM)
270
271 msg = ofp.message.port_status(xid=4,
272 reason=ofp.OFPPR_DELETE,
273 desc=desc)
274 expected = ''.join([
275 '\x01', '\x0c', # version/type
276 '\x00\x40', # length
277 '\x00\x00\x00\x04', # xid
278 '\x01', # reason
279 '\x00\x00\x00\x00\x00\x00\x00' # pad
280 '\xff\xfd', # desc.port_no
281 '\x01\x02\x03\x04\x05\x06', # desc.hw_addr
282 'foo'.ljust(16, '\x00'), # desc.name
283 '\x00\x00\x00\x10', # desc.config
284 '\x00\x00\x02\x00', # desc.state
285 '\x00\x00\x00\x01', # desc.curr
286 '\x00\x00\x00\x20', # desc.advertised
287 '\x00\x00\x02\x00', # desc.supported
288 '\x00\x00\x08\x00', # desc.peer
289 ])
290 self.assertEquals(expected, msg.pack())
291
292 def test_port_status_unpack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700293 buf = ''.join([
294 '\x01', '\x0c', # version/type
295 '\x00\x40', # length
296 '\x00\x00\x00\x04', # xid
297 '\x01', # reason
298 '\x00\x00\x00\x00\x00\x00\x00' # pad
299 '\xff\xfd', # desc.port_no
300 '\x01\x02\x03\x04\x05\x06', # desc.hw_addr
301 'foo'.ljust(16, '\x00'), # desc.name
302 '\x00\x00\x00\x10', # desc.config
303 '\x00\x00\x02\x00', # desc.state
304 '\x00\x00\x00\x01', # desc.curr
305 '\x00\x00\x00\x20', # desc.advertised
306 '\x00\x00\x02\x00', # desc.supported
307 '\x00\x00\x08\x00', # desc.peer
308 ])
309 msg = ofp.message.port_status.unpack(buf)
310 self.assertEquals('foo', msg.desc.name)
311 self.assertEquals(ofp.OFPPF_PAUSE_ASYM, msg.desc.peer)
312
313 def test_port_stats_reply_pack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700314 msg = ofp.message.port_stats_reply(xid=5, flags=0, entries=[
315 ofp.port_stats_entry(port_no=1, rx_packets=56, collisions=5),
316 ofp.port_stats_entry(port_no=ofp.OFPP_LOCAL, rx_packets=1, collisions=1)])
317 expected = ''.join([
318 '\x01', '\x11', # version/type
319 '\x00\xdc', # length
320 '\x00\x00\x00\x05', # xid
321 '\x00\x04', # stats_type
322 '\x00\x00', # flags
323 '\x00\x01', # entries[0].port_no
324 '\x00\x00\x00\x00\x00\x00' # entries[0].pad
325 '\x00\x00\x00\x00\x00\x00\x00\x38', # entries[0].rx_packets
326 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].tx_packets
327 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_bytes
328 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].tx_bytes
329 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_dropped
330 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].tx_dropped
331 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_errors
332 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].tx_errors
333 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_frame_err
334 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_over_err
335 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_crc_err
336 '\x00\x00\x00\x00\x00\x00\x00\x05', # entries[0].collisions
337 '\xff\xfe', # entries[1].port_no
338 '\x00\x00\x00\x00\x00\x00' # entries[1].pad
339 '\x00\x00\x00\x00\x00\x00\x00\x01', # entries[1].rx_packets
340 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].tx_packets
341 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_bytes
342 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].tx_bytes
343 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_dropped
344 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].tx_dropped
345 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_errors
346 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].tx_errors
347 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_frame_err
348 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_over_err
349 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_crc_err
350 '\x00\x00\x00\x00\x00\x00\x00\x01', # entries[1].collisions
351 ])
352 self.assertEquals(expected, msg.pack())
353
354 def test_port_stats_reply_unpack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700355 buf = ''.join([
356 '\x01', '\x11', # version/type
357 '\x00\xdc', # length
358 '\x00\x00\x00\x05', # xid
359 '\x00\x04', # stats_type
360 '\x00\x00', # flags
361 '\x00\x01', # entries[0].port_no
362 '\x00\x00\x00\x00\x00\x00' # entries[0].pad
363 '\x00\x00\x00\x00\x00\x00\x00\x38', # entries[0].rx_packets
364 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].tx_packets
365 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_bytes
366 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].tx_bytes
367 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_dropped
368 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].tx_dropped
369 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_errors
370 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].tx_errors
371 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_frame_err
372 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_over_err
373 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[0].rx_crc_err
374 '\x00\x00\x00\x00\x00\x00\x00\x05', # entries[0].collisions
375 '\xff\xfe', # entries[1].port_no
376 '\x00\x00\x00\x00\x00\x00' # entries[1].pad
377 '\x00\x00\x00\x00\x00\x00\x00\x01', # entries[1].rx_packets
378 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].tx_packets
379 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_bytes
380 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].tx_bytes
381 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_dropped
382 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].tx_dropped
383 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_errors
384 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].tx_errors
385 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_frame_err
386 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_over_err
387 '\x00\x00\x00\x00\x00\x00\x00\x00', # entries[1].rx_crc_err
388 '\x00\x00\x00\x00\x00\x00\x00\x01', # entries[1].collisions
389 ])
390 msg = ofp.message.port_stats_reply.unpack(buf)
391 self.assertEquals(ofp.OFPST_PORT, msg.stats_type)
392 self.assertEquals(2, len(msg.entries))
393
394 sample_flow_stats_reply_buf = ''.join([
395 '\x01', '\x11', # version/type
396 '\x00\xe4', # length
397 '\x00\x00\x00\x06', # xid
398 '\x00\x01', # stats_type
399 '\x00\x00', # flags
400 '\x00\x68', # entries[0].length
401 '\x03', # entries[0].table_id
402 '\x00', # entries[0].pad
403 '\x00\x3f\xff\xff', # entries[0].match.wildcards
404 '\x00' * 36, # remaining match fields
405 '\x00\x00\x00\x01', # entries[0].duration_sec
406 '\x00\x00\x00\x02', # entries[0].duration_nsec
407 '\x00\x64', # entries[0].priority
408 '\x00\x05', # entries[0].idle_timeout
409 '\x00\x0a', # entries[0].hard_timeout
410 '\x00' * 6, # entries[0].pad2
411 '\x01\x23\x45\x67\x89\xab\xcd\xef', # entries[0].cookie
412 '\x00\x00\x00\x00\x00\x00\x00\x0a', # entries[0].packet_count
413 '\x00\x00\x00\x00\x00\x00\x03\xe8', # entries[0].byte_count
414 '\x00\x00', # entries[0].actions[0].type
415 '\x00\x08', # entries[0].actions[0].len
416 '\x00\x01', # entries[0].actions[0].port
417 '\x00\x00', # entries[0].actions[0].max_len
418 '\x00\x00', # entries[0].actions[1].type
419 '\x00\x08', # entries[0].actions[1].len
420 '\x00\x02', # entries[0].actions[1].port
421 '\x00\x00', # entries[0].actions[1].max_len
422 '\x00\x70', # entries[1].length
423 '\x04', # entries[1].table_id
424 '\x00', # entries[1].pad
425 '\x00\x3f\xff\xff', # entries[1].match.wildcards
426 '\x00' * 36, # remaining match fields
427 '\x00\x00\x00\x01', # entries[1].duration_sec
428 '\x00\x00\x00\x02', # entries[1].duration_nsec
429 '\x00\x64', # entries[1].priority
430 '\x00\x05', # entries[1].idle_timeout
431 '\x00\x0a', # entries[1].hard_timeout
432 '\x00' * 6, # entries[1].pad2
433 '\x01\x23\x45\x67\x89\xab\xcd\xef', # entries[1].cookie
434 '\x00\x00\x00\x00\x00\x00\x00\x0a', # entries[1].packet_count
435 '\x00\x00\x00\x00\x00\x00\x03\xe8', # entries[1].byte_count
436 '\x00\x00', # entries[1].actions[0].type
437 '\x00\x08', # entries[1].actions[0].len
438 '\x00\x01', # entries[1].actions[0].port
439 '\x00\x00', # entries[1].actions[0].max_len
440 '\x00\x00', # entries[1].actions[1].type
441 '\x00\x08', # entries[1].actions[1].len
442 '\x00\x02', # entries[1].actions[1].port
443 '\x00\x00', # entries[1].actions[1].max_len
444 '\x00\x00', # entries[1].actions[2].type
445 '\x00\x08', # entries[1].actions[2].len
446 '\x00\x03', # entries[1].actions[2].port
447 '\x00\x00', # entries[1].actions[2].max_len
448 ])
449
450 def test_flow_stats_reply_pack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700451 msg = ofp.message.flow_stats_reply(xid=6, flags=0, entries=[
452 ofp.flow_stats_entry(table_id=3,
453 match=ofp.match(),
454 duration_sec=1,
455 duration_nsec=2,
456 priority=100,
457 idle_timeout=5,
458 hard_timeout=10,
459 cookie=0x0123456789abcdef,
460 packet_count=10,
461 byte_count=1000,
462 actions=[ofp.action.output(port=1),
463 ofp.action.output(port=2)]),
464 ofp.flow_stats_entry(table_id=4,
465 match=ofp.match(),
466 duration_sec=1,
467 duration_nsec=2,
468 priority=100,
469 idle_timeout=5,
470 hard_timeout=10,
471 cookie=0x0123456789abcdef,
472 packet_count=10,
473 byte_count=1000,
474 actions=[ofp.action.output(port=1),
475 ofp.action.output(port=2),
476 ofp.action.output(port=3)])])
477 self.assertEquals(self.sample_flow_stats_reply_buf, msg.pack())
478
479 def test_flow_stats_reply_unpack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700480 msg = ofp.message.flow_stats_reply.unpack(self.sample_flow_stats_reply_buf)
481 self.assertEquals(ofp.OFPST_FLOW, msg.stats_type)
482 self.assertEquals(2, len(msg.entries))
483 self.assertEquals(2, len(msg.entries[0].actions))
484 self.assertEquals(3, len(msg.entries[1].actions))
485
486 def test_flow_add_show(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700487 expected = """\
488flow_add {
489 xid = None,
490 match = match_v1 {
491 wildcards = OFPFW_DL_SRC|OFPFW_DL_DST,
492 in_port = 3,
493 eth_src = 01:23:45:67:89:ab,
494 eth_dst = cd:ef:01:23:45:67,
495 vlan_vid = 0x0,
496 vlan_pcp = 0x0,
Rich Lanea06d0c32013-03-25 08:52:03 -0700497 eth_type = 0x0,
498 ip_dscp = 0x0,
499 ip_proto = 0x0,
Rich Lanea06d0c32013-03-25 08:52:03 -0700500 ipv4_src = 192.168.3.127,
501 ipv4_dst = 255.255.255.255,
502 tcp_src = 0x0,
503 tcp_dst = 0x0
504 },
505 cookie = 0x0,
506 idle_timeout = 0x0,
507 hard_timeout = 0x0,
508 priority = 0x0,
509 buffer_id = 0x0,
510 out_port = 0,
511 flags = 0x0,
512 actions = [
513 output { port = OFPP_FLOOD, max_len = 0x0 },
Rich Lanec2685792013-04-30 14:08:33 -0700514 nicira_dec_ttl { },
Rich Lanea06d0c32013-03-25 08:52:03 -0700515 bsn_set_tunnel_dst { dst = 0x0 }
516 ]
517}"""
518 msg = ofp.message.flow_add(
519 match=ofp.match(
520 wildcards=ofp.OFPFW_DL_SRC|ofp.OFPFW_DL_DST,
521 in_port=3,
522 ipv4_src=0xc0a8037f,
523 ipv4_dst=0xffffffff,
524 eth_src=[0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
525 eth_dst=[0xcd, 0xef, 0x01, 0x23, 0x45, 0x67]),
526 actions=[
527 ofp.action.output(port=ofp.OFPP_FLOOD),
528 ofp.action.nicira_dec_ttl(),
529 ofp.action.bsn_set_tunnel_dst()])
530 self.assertEquals(msg.show(), expected)
531
532 sample_packet_out_buf = ''.join([
533 '\x01', '\x0d', # version/type
534 '\x00\x23', # length
535 '\x12\x34\x56\x78', # xid
536 '\xab\xcd\xef\x01', # buffer_id
537 '\xff\xfe', # in_port
538 '\x00\x10', # actions_len
539 '\x00\x00', # actions[0].type
540 '\x00\x08', # actions[0].len
541 '\x00\x01', # actions[0].port
542 '\x00\x00', # actions[0].max_len
543 '\x00\x00', # actions[1].type
544 '\x00\x08', # actions[1].len
545 '\x00\x02', # actions[1].port
546 '\x00\x00', # actions[1].max_len
547 'abc' # data
548 ])
549
550 def test_packet_out_pack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700551 msg = ofp.message.packet_out(
552 xid=0x12345678,
553 buffer_id=0xabcdef01,
554 in_port=ofp.OFPP_LOCAL,
555 actions=[
556 ofp.action.output(port=1),
557 ofp.action.output(port=2)],
558 data='abc')
559 self.assertEquals(self.sample_packet_out_buf, msg.pack())
560
561 def test_packet_out_unpack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700562 msg = ofp.message.packet_out.unpack(self.sample_packet_out_buf)
563 self.assertEquals(0x12345678, msg.xid)
564 self.assertEquals(0xabcdef01, msg.buffer_id)
565 self.assertEquals(ofp.OFPP_LOCAL, msg.in_port)
566 self.assertEquals(2, len(msg.actions))
567 self.assertEquals(1, msg.actions[0].port)
568 self.assertEquals(2, msg.actions[1].port)
569 self.assertEquals('abc', msg.data)
570
571 sample_packet_in_buf = ''.join([
572 '\x01', '\x0a', # version/type
573 '\x00\x15', # length
574 '\x12\x34\x56\x78', # xid
575 '\xab\xcd\xef\x01', # buffer_id
576 '\x00\x09', # total_len
577 '\xff\xfe', # in_port
578 '\x01', # reason
579 '\x00', # pad
580 'abc', # data
581 ])
582
583 def test_packet_in_pack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700584 msg = ofp.message.packet_in(
585 xid=0x12345678,
586 buffer_id=0xabcdef01,
587 total_len=9,
588 in_port=ofp.OFPP_LOCAL,
589 reason=ofp.OFPR_ACTION,
590 data='abc')
591 self.assertEquals(self.sample_packet_in_buf, msg.pack())
592
593 def test_packet_in_unpack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700594 msg = ofp.message.packet_in.unpack(self.sample_packet_in_buf)
595 self.assertEquals(0x12345678, msg.xid)
596 self.assertEquals(0xabcdef01, msg.buffer_id)
597 self.assertEquals(9, msg.total_len)
598 self.assertEquals(ofp.OFPP_LOCAL, msg.in_port)
599 self.assertEquals(ofp.OFPR_ACTION, msg.reason)
600 self.assertEquals('abc', msg.data)
601
602 sample_queue_get_config_reply_buf = ''.join([
603 '\x01', '\x15', # version/type
604 '\x00\x50', # length
605 '\x12\x34\x56\x78', # xid
606 '\xff\xfe', # port
607 '\x00\x00\x00\x00\x00\x00', # pad
608 '\x00\x00\x00\x01', # queues[0].queue_id
609 '\x00\x18', # queues[0].len
610 '\x00\x00', # queues[0].pad
611 '\x00\x01', # queues[0].properties[0].type
612 '\x00\x10', # queues[0].properties[0].length
613 '\x00\x00\x00\x00', # queues[0].properties[0].pad
614 '\x00\x05', # queues[0].properties[0].rate
615 '\x00\x00\x00\x00\x00\x00', # queues[0].properties[0].pad2
616 '\x00\x00\x00\x02', # queues[1].queue_id
617 '\x00\x28', # queues[1].len
618 '\x00\x00', # queues[1].pad
619 '\x00\x01', # queues[1].properties[0].type
620 '\x00\x10', # queues[1].properties[0].length
621 '\x00\x00\x00\x00', # queues[1].properties[0].pad
622 '\x00\x06', # queues[1].properties[0].rate
623 '\x00\x00\x00\x00\x00\x00', # queues[1].properties[0].pad2
624 '\x00\x01', # queues[1].properties[1].type
625 '\x00\x10', # queues[1].properties[1].length
626 '\x00\x00\x00\x00', # queues[1].properties[1].pad
627 '\x00\x07', # queues[1].properties[1].rate
628 '\x00\x00\x00\x00\x00\x00', # queues[1].properties[1].pad2
629 ])
630
631 def test_queue_get_config_reply_pack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700632 msg = ofp.message.queue_get_config_reply(
633 xid=0x12345678,
634 port=ofp.OFPP_LOCAL,
635 queues=[
636 ofp.packet_queue(queue_id=1, properties=[
637 ofp.queue_prop_min_rate(rate=5)]),
638 ofp.packet_queue(queue_id=2, properties=[
639 ofp.queue_prop_min_rate(rate=6),
640 ofp.queue_prop_min_rate(rate=7)])])
641 self.assertEquals(self.sample_queue_get_config_reply_buf, msg.pack())
642
643 def test_queue_get_config_reply_unpack(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700644 msg = ofp.message.queue_get_config_reply.unpack(self.sample_queue_get_config_reply_buf)
645 self.assertEquals(ofp.OFPP_LOCAL, msg.port)
646 self.assertEquals(msg.queues[0].queue_id, 1)
647 self.assertEquals(msg.queues[0].properties[0].rate, 5)
648 self.assertEquals(msg.queues[1].queue_id, 2)
649 self.assertEquals(msg.queues[1].properties[0].rate, 6)
650 self.assertEquals(msg.queues[1].properties[1].rate, 7)
651
652class TestParse(unittest.TestCase):
653 def test_parse_header(self):
654 import loxi
Rich Lanea06d0c32013-03-25 08:52:03 -0700655
656 msg_ver, msg_type, msg_len, msg_xid = ofp.message.parse_header("\x01\x04\xAF\xE8\x12\x34\x56\x78")
657 self.assertEquals(1, msg_ver)
658 self.assertEquals(4, msg_type)
659 self.assertEquals(45032, msg_len)
660 self.assertEquals(0x12345678, msg_xid)
661
662 with self.assertRaisesRegexp(loxi.ProtocolError, "too short"):
663 ofp.message.parse_header("\x01\x04\xAF\xE8\x12\x34\x56")
664
665 def test_parse_message(self):
666 import loxi
667 import loxi.of10 as ofp
668
669 buf = "\x01\x00\x00\x08\x12\x34\x56\x78"
670 msg = ofp.message.parse_message(buf)
671 assert(msg.xid == 0x12345678)
672
673 # Get a list of all message classes
674 test_klasses = [x for x in ofp.message.__dict__.values()
675 if type(x) == type
676 and issubclass(x, ofp.message.Message)
677 and x != ofp.message.Message]
678
679 for klass in test_klasses:
680 self.assertIsInstance(ofp.message.parse_message(klass(xid=1).pack()), klass)
681
682class TestUtils(unittest.TestCase):
Rich Lanea06d0c32013-03-25 08:52:03 -0700683 def test_pretty_wildcards(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700684 self.assertEquals("OFPFW_ALL", ofp.util.pretty_wildcards(ofp.OFPFW_ALL))
685 self.assertEquals("0", ofp.util.pretty_wildcards(0))
686 self.assertEquals("OFPFW_DL_SRC|OFPFW_DL_DST",
687 ofp.util.pretty_wildcards(ofp.OFPFW_DL_SRC|ofp.OFPFW_DL_DST))
688 self.assertEquals("OFPFW_NW_SRC_MASK&0x2000",
689 ofp.util.pretty_wildcards(ofp.OFPFW_NW_SRC_ALL))
690 self.assertEquals("OFPFW_NW_SRC_MASK&0x1a00",
691 ofp.util.pretty_wildcards(0x00001a00))
692 self.assertEquals("OFPFW_IN_PORT|0x80000000",
693 ofp.util.pretty_wildcards(ofp.OFPFW_IN_PORT|0x80000000))
694
695class TestAll(unittest.TestCase):
696 """
697 Round-trips every class through serialization/deserialization.
698 Not a replacement for handcoded tests because it only uses the
699 default member values.
700 """
701
702 def setUp(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700703 mods = [ofp.action,ofp.message,ofp.common]
704 self.klasses = [klass for mod in mods
705 for klass in mod.__dict__.values()
706 if hasattr(klass, 'show')]
707 self.klasses.sort(key=lambda x: str(x))
708
709 def test_serialization(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700710 expected_failures = []
711 for klass in self.klasses:
712 def fn():
713 obj = klass()
714 if hasattr(obj, "xid"): obj.xid = 42
715 buf = obj.pack()
716 obj2 = klass.unpack(buf)
717 self.assertEquals(obj, obj2)
718 if klass in expected_failures:
719 self.assertRaises(Exception, fn)
720 else:
721 fn()
722
723 def test_show(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700724 expected_failures = []
725 for klass in self.klasses:
726 def fn():
727 obj = klass()
728 if hasattr(obj, "xid"): obj.xid = 42
729 obj.show()
730 if klass in expected_failures:
731 self.assertRaises(Exception, fn)
732 else:
733 fn()
734
735if __name__ == '__main__':
736 unittest.main()