blob: 77c1d94d2689834641db1d6c8000b6598e86a4a9 [file] [log] [blame]
Rich Laneb626a9c2013-08-05 16:45:50 -07001# Distributed under the OpenFlow Software License (see LICENSE)
2# Copyright (c) 2010 The Board of Trustees of The Leland Stanford Junior University
3# Copyright (c) 2012, 2013 Big Switch Networks, Inc.
4"""
5Action test cases
6
7These tests check the behavior of each type of action. The matches used are
8exact-match, to satisfy the OXM prerequisites of the set-field actions.
9These tests use a single apply-actions instruction.
10"""
11
12import logging
13
14from oftest import config
15import oftest.base_tests as base_tests
16import ofp
17from loxi.pp import pp
18
19from oftest.testutils import *
20from oftest.parse import parse_ipv6
21
22class Output(base_tests.SimpleDataPlane):
23 """
24 Output to a single port
25 """
26 def runTest(self):
Rich Lane045db072013-08-06 13:16:30 -070027 in_port, out_port = openflow_ports(2)
Rich Laneb626a9c2013-08-05 16:45:50 -070028
29 actions = [ofp.action.output(out_port)]
30
31 pkt = simple_tcp_packet()
32
33 logging.info("Running actions test for %s", pp(actions))
34
35 delete_all_flows(self.controller)
36
37 logging.info("Inserting flow")
38 request = ofp.message.flow_add(
Wilson Ng42df57a2013-10-28 17:54:57 -070039 table_id=test_param_get_table(),
Rich Laneb626a9c2013-08-05 16:45:50 -070040 match=packet_to_flow_match(self, pkt),
41 instructions=[
42 ofp.instruction.apply_actions(actions)],
43 buffer_id=ofp.OFP_NO_BUFFER,
44 priority=1000)
45 self.controller.message_send(request)
46
47 do_barrier(self.controller)
48
49 pktstr = str(pkt)
50
51 logging.info("Sending packet, expecting output to port %d", out_port)
52 self.dataplane.send(in_port, pktstr)
Rich Lanee4b384d2013-09-13 14:33:40 -070053 verify_packets(self, pktstr, [out_port])
Rich Laneb626a9c2013-08-05 16:45:50 -070054
55class OutputMultiple(base_tests.SimpleDataPlane):
56 """
57 Output to three ports
58 """
59 def runTest(self):
Rich Lane045db072013-08-06 13:16:30 -070060 ports = openflow_ports(4)
Rich Laneb626a9c2013-08-05 16:45:50 -070061 in_port = ports[0]
62 out_ports = ports[1:4]
63
64 actions = [ofp.action.output(x) for x in out_ports]
65
66 pkt = simple_tcp_packet()
67
68 logging.info("Running actions test for %s", pp(actions))
69
70 delete_all_flows(self.controller)
71
72 logging.info("Inserting flow")
73 request = ofp.message.flow_add(
Wilson Ng42df57a2013-10-28 17:54:57 -070074 table_id=test_param_get_table(),
Rich Laneb626a9c2013-08-05 16:45:50 -070075 match=packet_to_flow_match(self, pkt),
76 instructions=[
77 ofp.instruction.apply_actions(actions)],
78 buffer_id=ofp.OFP_NO_BUFFER,
79 priority=1000)
80 self.controller.message_send(request)
81
82 do_barrier(self.controller)
83
84 pktstr = str(pkt)
85
86 logging.info("Sending packet, expecting output to ports %r", out_ports)
87 self.dataplane.send(in_port, pktstr)
Rich Lanee4b384d2013-09-13 14:33:40 -070088 verify_packets(self, pktstr, out_ports)
Rich Laneb626a9c2013-08-05 16:45:50 -070089
90class BaseModifyPacketTest(base_tests.SimpleDataPlane):
91 """
92 Base class for action tests that modify a packet
93 """
94
95 def verify_modify(self, actions, pkt, exp_pkt):
Rich Lane045db072013-08-06 13:16:30 -070096 in_port, out_port = openflow_ports(2)
Rich Laneb626a9c2013-08-05 16:45:50 -070097
98 actions = actions + [ofp.action.output(out_port)]
99
100 logging.info("Running actions test for %s", pp(actions))
101
102 delete_all_flows(self.controller)
103
104 logging.info("Inserting flow")
105 request = ofp.message.flow_add(
Wilson Ng42df57a2013-10-28 17:54:57 -0700106 table_id=test_param_get_table(),
Rich Laneb626a9c2013-08-05 16:45:50 -0700107 match=packet_to_flow_match(self, pkt),
108 instructions=[
109 ofp.instruction.apply_actions(actions)],
110 buffer_id=ofp.OFP_NO_BUFFER,
111 priority=1000)
112 self.controller.message_send(request)
113
114 do_barrier(self.controller)
115
116 logging.info("Sending packet, expecting output to port %d", out_port)
117 self.dataplane.send(in_port, str(pkt))
Rich Lanee4b384d2013-09-13 14:33:40 -0700118 verify_packets(self, str(exp_pkt), [out_port])
119
Rich Laneb626a9c2013-08-05 16:45:50 -0700120
121class PushVlan(BaseModifyPacketTest):
122 """
123 Push a vlan tag (vid=0, pcp=0)
124 """
125 def runTest(self):
126 actions = [ofp.action.push_vlan(ethertype=0x8100)]
127 pkt = simple_tcp_packet()
128 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
129 self.verify_modify(actions, pkt, exp_pkt)
130
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700131class PushVlanVid(BaseModifyPacketTest):
132 """
133 Push a vlan tag (vid=2, pcp=0)
134 """
135 def runTest(self):
136 actions = [ofp.action.push_vlan(ethertype=0x8100),
137 ofp.action.set_field(ofp.oxm.vlan_vid(2))]
138 pkt = simple_tcp_packet()
139 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2, pktlen=104)
140 self.verify_modify(actions, pkt, exp_pkt)
141
142class PushVlanVidPcp(BaseModifyPacketTest):
143 """
144 Push a vlan tag (vid=2, pcp=3)
145 """
146 def runTest(self):
147 actions = [ofp.action.push_vlan(ethertype=0x8100),
148 ofp.action.set_field(ofp.oxm.vlan_vid(2)),
149 ofp.action.set_field(ofp.oxm.vlan_pcp(3))]
150 pkt = simple_tcp_packet()
151 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2, vlan_pcp=3, pktlen=104)
152 self.verify_modify(actions, pkt, exp_pkt)
153
154class PushVlanPcp(BaseModifyPacketTest):
155 """
156 Push a vlan tag (vid=0, pcp=3)
157 """
158 def runTest(self):
159 actions = [ofp.action.push_vlan(ethertype=0x8100),
160 ofp.action.set_field(ofp.oxm.vlan_pcp(3))]
161 pkt = simple_tcp_packet()
162 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=0, vlan_pcp=3, pktlen=104)
163 self.verify_modify(actions, pkt, exp_pkt)
164
Rich Laneb626a9c2013-08-05 16:45:50 -0700165class PopVlan(BaseModifyPacketTest):
166 """
167 Pop a vlan tag
168 """
169 def runTest(self):
170 actions = [ofp.action.pop_vlan()]
171 pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
172 exp_pkt = simple_tcp_packet()
173 self.verify_modify(actions, pkt, exp_pkt)
174
175class SetVlanVid(BaseModifyPacketTest):
176 """
177 Set the vlan vid
178 """
179 def runTest(self):
180 actions = [ofp.action.set_field(ofp.oxm.vlan_vid(2))]
181 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=1)
182 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2)
183 self.verify_modify(actions, pkt, exp_pkt)
184
185class SetVlanPcp(BaseModifyPacketTest):
186 """
187 Set the vlan priority
188 """
189 def runTest(self):
190 actions = [ofp.action.set_field(ofp.oxm.vlan_pcp(2))]
191 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=1)
192 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=2)
193 self.verify_modify(actions, pkt, exp_pkt)
Kiran Poolad06998a2013-08-07 14:51:50 -0700194
195class SetEthDst(BaseModifyPacketTest):
196 """
197 Set Eth Dst address
198 """
199 def runTest(self):
200 actions = [ofp.action.set_field(ofp.oxm.eth_dst([0x00,0xA1,0xCD,0x53,0xC6,0x55]))]
201 pkt = simple_tcp_packet()
202 exp_pkt = simple_tcp_packet(eth_dst="00:A1:CD:53:C6:55")
203 self.verify_modify(actions, pkt, exp_pkt)
204
205class SetEthSrc(BaseModifyPacketTest):
206 """
207 Set Eth Src address
208 """
209 def runTest(self):
210 actions = [ofp.action.set_field(ofp.oxm.eth_src([0x00,0xA1,0xCD,0x53,0xC6,0x55]))]
211 pkt = simple_tcp_packet()
212 exp_pkt = simple_tcp_packet(eth_src="00:A1:CD:53:C6:55")
213 self.verify_modify(actions, pkt, exp_pkt)
214
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700215class SetIpv4Dscp(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700216 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700217 Set IPv4 DSCP
Kiran Poolad06998a2013-08-07 14:51:50 -0700218 """
219 def runTest(self):
220 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
221 pkt = simple_tcp_packet()
222 exp_pkt = simple_tcp_packet(ip_tos=0x04)
223 self.verify_modify(actions, pkt, exp_pkt)
224
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700225class SetIpv4ECN(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700226 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700227 Set IPv4 ECN
Kiran Poolad06998a2013-08-07 14:51:50 -0700228 """
229 def runTest(self):
230 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x01))]
231 pkt = simple_tcp_packet()
232 exp_pkt = simple_tcp_packet(ip_tos=0x01)
233 self.verify_modify(actions, pkt, exp_pkt)
234
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700235class SetIpv4DSCP_NonZeroECN(BaseModifyPacketTest):
Kiran Poolafb523e52013-08-07 22:57:51 -0700236 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700237 Set IPv4 DSCP and make sure ECN is not modified
Kiran Poolafb523e52013-08-07 22:57:51 -0700238 """
239 def runTest(self):
240 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
241 pkt = simple_tcp_packet(ip_tos=0x11)
242 exp_pkt = simple_tcp_packet(ip_tos=0x05)
243 self.verify_modify(actions, pkt, exp_pkt)
244
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700245class SetIpv4ECN_NonZeroDSCP(BaseModifyPacketTest):
Kiran Poolafb523e52013-08-07 22:57:51 -0700246 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700247 Set IPv4 ECN and make sure DSCP is not modified
Kiran Poolafb523e52013-08-07 22:57:51 -0700248 """
249 def runTest(self):
250 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x02))]
251 pkt = simple_tcp_packet(ip_tos=0x11)
252 exp_pkt = simple_tcp_packet(ip_tos=0x12)
253 self.verify_modify(actions, pkt, exp_pkt)
254
Kiran Poolad06998a2013-08-07 14:51:50 -0700255class SetIPv4Src(BaseModifyPacketTest):
256 """
257 Set IPv4 srouce address
258 """
259 def runTest(self):
260 actions = [ofp.action.set_field(ofp.oxm.ipv4_src(167772161))]
261 pkt = simple_tcp_packet()
262 exp_pkt = simple_tcp_packet(ip_src="10.0.0.1")
263 self.verify_modify(actions, pkt, exp_pkt)
264
265class SetIPv4Dst(BaseModifyPacketTest):
266 """
267 Set IPv4 destination address
268 """
269 def runTest(self):
270 actions = [ofp.action.set_field(ofp.oxm.ipv4_dst(167772161))]
271 pkt = simple_tcp_packet()
272 exp_pkt = simple_tcp_packet(ip_dst="10.0.0.1")
273 self.verify_modify(actions, pkt, exp_pkt)
274
275class SetTCPSrc(BaseModifyPacketTest):
276 """
277 Set TCP source port
278 """
279 def runTest(self):
280 actions = [ofp.action.set_field(ofp.oxm.tcp_src(800))]
281 pkt = simple_tcp_packet()
282 exp_pkt = simple_tcp_packet(tcp_sport=800)
283 self.verify_modify(actions, pkt, exp_pkt)
284
285class SetTCPDst(BaseModifyPacketTest):
286 """
287 Set TCP destination port
288 """
289 def runTest(self):
290 actions = [ofp.action.set_field(ofp.oxm.tcp_dst(800))]
291 pkt = simple_tcp_packet()
292 exp_pkt = simple_tcp_packet(tcp_dport=800)
293 self.verify_modify(actions, pkt, exp_pkt)
294
295class SetUDPSrc(BaseModifyPacketTest):
296 """
297 Set UDP source port
298 """
299 def runTest(self):
300 actions = [ofp.action.set_field(ofp.oxm.udp_src(800))]
301 pkt = simple_udp_packet()
302 exp_pkt = simple_udp_packet(udp_sport=800)
303 self.verify_modify(actions, pkt, exp_pkt)
304
305class SetUDPDst(BaseModifyPacketTest):
306 """
307 Set UDP destination port
308 """
309 def runTest(self):
310 actions = [ofp.action.set_field(ofp.oxm.udp_dst(800))]
311 pkt = simple_udp_packet()
312 exp_pkt = simple_udp_packet(udp_dport=800)
313 self.verify_modify(actions, pkt, exp_pkt)
314
315class SetIPv6Src(BaseModifyPacketTest):
316 """
317 Set IPv6 source address
318 """
319 def runTest(self):
320 actions = [ofp.action.set_field(ofp.oxm.ipv6_src("\x20\x01\xab\xb1\x34\x56\xbc\xcb\x00\x00\x00\x00\x03\x70\x73\x36"))]
321 pkt = simple_tcpv6_packet()
322 exp_pkt = simple_tcpv6_packet(ipv6_src="2001:abb1:3456:bccb:0000:0000:0370:7336")
323 self.verify_modify(actions, pkt, exp_pkt)
324
325class SetIPv6Dst(BaseModifyPacketTest):
326 """
327 Set IPv6 destination address
328 """
329 def runTest(self):
330 actions = [ofp.action.set_field(ofp.oxm.ipv6_dst("\x20\x01\xab\xb1\x34\x56\xbc\xcb\x00\x00\x00\x00\x03\x70\x73\x36"))]
331 pkt = simple_tcpv6_packet()
332 exp_pkt = simple_tcpv6_packet(ipv6_dst="2001:abb1:3456:bccb:0000:0000:0370:7336")
333 self.verify_modify(actions, pkt, exp_pkt)
334
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700335class SetIpv6Dscp(BaseModifyPacketTest):
336 """
337 Set IPv6 DSCP
338 """
339 def runTest(self):
340 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
341 pkt = simple_tcpv6_packet()
342 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x04)
343 self.verify_modify(actions, pkt, exp_pkt)
344
345class SetIpv6ECN(BaseModifyPacketTest):
346 """
347 Set IPv6 ECN
348 """
349 def runTest(self):
350 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x01))]
351 pkt = simple_tcpv6_packet()
352 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x01)
353 self.verify_modify(actions, pkt, exp_pkt)
354
Kiran Poolad06998a2013-08-07 14:51:50 -0700355class SetIPv6Flabel(BaseModifyPacketTest):
356 """
357 Set IPv6 Flabel
358 """
359 def runTest(self):
Kiran Poolafb523e52013-08-07 22:57:51 -0700360 actions = [ofp.action.set_field(ofp.oxm.ipv6_flabel(10))]
Kiran Poolad06998a2013-08-07 14:51:50 -0700361 pkt = simple_tcpv6_packet()
Kiran Poolafb523e52013-08-07 22:57:51 -0700362 exp_pkt = simple_tcpv6_packet(ipv6_fl=10)
Kiran Poolad06998a2013-08-07 14:51:50 -0700363 self.verify_modify(actions, pkt, exp_pkt)
364
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700365class SetIpv6DSCP_NonZeroECNandFlabel(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700366 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700367 Set IPv6 DSCP and make sure ECN is not modified
368 """
369 def runTest(self):
370 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
371 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
372 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x05, ipv6_fl=10)
373 self.verify_modify(actions, pkt, exp_pkt)
374
375class SetIpv6ECN_NonZeroDSCPandFlabel(BaseModifyPacketTest):
376 """
377 Set IPv6 ECN and make sure DSCP is not modified
378 """
379 def runTest(self):
380 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x02))]
381 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
382 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x12, ipv6_fl=10)
383 self.verify_modify(actions, pkt, exp_pkt)
384
385class SetIPv6Flabel_NonZeroDSCPandECN(BaseModifyPacketTest):
386 """
387 Set IPv6 Flabel
388 """
389 def runTest(self):
390 actions = [ofp.action.set_field(ofp.oxm.ipv6_flabel(10))]
391 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=9)
392 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
393 self.verify_modify(actions, pkt, exp_pkt)
394
395class SetIpv4TTL(BaseModifyPacketTest):
396 """
397 Set IPv4 TTL
Kiran Poolad06998a2013-08-07 14:51:50 -0700398 """
399 def runTest(self):
400 actions = [ofp.action.set_nw_ttl(10)]
401 pkt = simple_tcp_packet()
402 exp_pkt = simple_tcp_packet(ip_ttl=10)
403 self.verify_modify(actions, pkt, exp_pkt)
404
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700405class SetIpv6HopLimit(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700406 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700407 Set Ipv6 Hop Limit
408 """
409 def runTest(self):
410 actions = [ofp.action.set_nw_ttl(10)]
411 pkt = simple_tcpv6_packet()
412 exp_pkt = simple_tcpv6_packet(ipv6_hlim=10)
413 self.verify_modify(actions, pkt, exp_pkt)
414
415class DecIpv4TTL(BaseModifyPacketTest):
416 """
417 Decrement Ipv4 TTL
Kiran Poolad06998a2013-08-07 14:51:50 -0700418 """
419 def runTest(self):
420 actions = [ofp.action.dec_nw_ttl()]
421 pkt = simple_tcp_packet(ip_ttl=10)
422 exp_pkt = simple_tcp_packet(ip_ttl=9)
423 self.verify_modify(actions, pkt, exp_pkt)
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700424
425class DecIpv6HopLimit(BaseModifyPacketTest):
426 """
427 Decrement Ipv6 Hop Limit
428 """
429 def runTest(self):
430 actions = [ofp.action.dec_nw_ttl()]
431 pkt = simple_tcpv6_packet(ipv6_hlim=10)
432 exp_pkt = simple_tcpv6_packet(ipv6_hlim=9)
433 self.verify_modify(actions, pkt, exp_pkt)