blob: e5b1e3a1f5722e8319dd920a59f9f78118433399 [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(
39 table_id=0,
40 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 Lane045db072013-08-06 13:16:30 -070053 receive_pkt_check(self.dataplane, pktstr, [out_port],
54 set(openflow_ports()) - set([out_port]), self)
Rich Laneb626a9c2013-08-05 16:45:50 -070055
56class OutputMultiple(base_tests.SimpleDataPlane):
57 """
58 Output to three ports
59 """
60 def runTest(self):
Rich Lane045db072013-08-06 13:16:30 -070061 ports = openflow_ports(4)
Rich Laneb626a9c2013-08-05 16:45:50 -070062 in_port = ports[0]
63 out_ports = ports[1:4]
64
65 actions = [ofp.action.output(x) for x in out_ports]
66
67 pkt = simple_tcp_packet()
68
69 logging.info("Running actions test for %s", pp(actions))
70
71 delete_all_flows(self.controller)
72
73 logging.info("Inserting flow")
74 request = ofp.message.flow_add(
75 table_id=0,
76 match=packet_to_flow_match(self, pkt),
77 instructions=[
78 ofp.instruction.apply_actions(actions)],
79 buffer_id=ofp.OFP_NO_BUFFER,
80 priority=1000)
81 self.controller.message_send(request)
82
83 do_barrier(self.controller)
84
85 pktstr = str(pkt)
86
87 logging.info("Sending packet, expecting output to ports %r", out_ports)
88 self.dataplane.send(in_port, pktstr)
Rich Lane045db072013-08-06 13:16:30 -070089 receive_pkt_check(self.dataplane, pktstr, out_ports,
90 set(openflow_ports()) - set(out_ports), self)
Rich Laneb626a9c2013-08-05 16:45:50 -070091
92class BaseModifyPacketTest(base_tests.SimpleDataPlane):
93 """
94 Base class for action tests that modify a packet
95 """
96
97 def verify_modify(self, actions, pkt, exp_pkt):
Rich Lane045db072013-08-06 13:16:30 -070098 in_port, out_port = openflow_ports(2)
Rich Laneb626a9c2013-08-05 16:45:50 -070099
100 actions = actions + [ofp.action.output(out_port)]
101
102 logging.info("Running actions test for %s", pp(actions))
103
104 delete_all_flows(self.controller)
105
106 logging.info("Inserting flow")
107 request = ofp.message.flow_add(
108 table_id=0,
109 match=packet_to_flow_match(self, pkt),
110 instructions=[
111 ofp.instruction.apply_actions(actions)],
112 buffer_id=ofp.OFP_NO_BUFFER,
113 priority=1000)
114 self.controller.message_send(request)
115
116 do_barrier(self.controller)
117
118 logging.info("Sending packet, expecting output to port %d", out_port)
119 self.dataplane.send(in_port, str(pkt))
Rich Lane045db072013-08-06 13:16:30 -0700120 receive_pkt_check(self.dataplane, str(exp_pkt), [out_port],
121 set(openflow_ports()) - set([out_port]), self)
Rich Laneb626a9c2013-08-05 16:45:50 -0700122
123class PushVlan(BaseModifyPacketTest):
124 """
125 Push a vlan tag (vid=0, pcp=0)
126 """
127 def runTest(self):
128 actions = [ofp.action.push_vlan(ethertype=0x8100)]
129 pkt = simple_tcp_packet()
130 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
131 self.verify_modify(actions, pkt, exp_pkt)
132
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700133class PushVlanVid(BaseModifyPacketTest):
134 """
135 Push a vlan tag (vid=2, pcp=0)
136 """
137 def runTest(self):
138 actions = [ofp.action.push_vlan(ethertype=0x8100),
139 ofp.action.set_field(ofp.oxm.vlan_vid(2))]
140 pkt = simple_tcp_packet()
141 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2, pktlen=104)
142 self.verify_modify(actions, pkt, exp_pkt)
143
144class PushVlanVidPcp(BaseModifyPacketTest):
145 """
146 Push a vlan tag (vid=2, pcp=3)
147 """
148 def runTest(self):
149 actions = [ofp.action.push_vlan(ethertype=0x8100),
150 ofp.action.set_field(ofp.oxm.vlan_vid(2)),
151 ofp.action.set_field(ofp.oxm.vlan_pcp(3))]
152 pkt = simple_tcp_packet()
153 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2, vlan_pcp=3, pktlen=104)
154 self.verify_modify(actions, pkt, exp_pkt)
155
156class PushVlanPcp(BaseModifyPacketTest):
157 """
158 Push a vlan tag (vid=0, pcp=3)
159 """
160 def runTest(self):
161 actions = [ofp.action.push_vlan(ethertype=0x8100),
162 ofp.action.set_field(ofp.oxm.vlan_pcp(3))]
163 pkt = simple_tcp_packet()
164 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=0, vlan_pcp=3, pktlen=104)
165 self.verify_modify(actions, pkt, exp_pkt)
166
Rich Laneb626a9c2013-08-05 16:45:50 -0700167class PopVlan(BaseModifyPacketTest):
168 """
169 Pop a vlan tag
170 """
171 def runTest(self):
172 actions = [ofp.action.pop_vlan()]
173 pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
174 exp_pkt = simple_tcp_packet()
175 self.verify_modify(actions, pkt, exp_pkt)
176
177class SetVlanVid(BaseModifyPacketTest):
178 """
179 Set the vlan vid
180 """
181 def runTest(self):
182 actions = [ofp.action.set_field(ofp.oxm.vlan_vid(2))]
183 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=1)
184 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2)
185 self.verify_modify(actions, pkt, exp_pkt)
186
187class SetVlanPcp(BaseModifyPacketTest):
188 """
189 Set the vlan priority
190 """
191 def runTest(self):
192 actions = [ofp.action.set_field(ofp.oxm.vlan_pcp(2))]
193 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=1)
194 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=2)
195 self.verify_modify(actions, pkt, exp_pkt)
Kiran Poolad06998a2013-08-07 14:51:50 -0700196
197class SetEthDst(BaseModifyPacketTest):
198 """
199 Set Eth Dst address
200 """
201 def runTest(self):
202 actions = [ofp.action.set_field(ofp.oxm.eth_dst([0x00,0xA1,0xCD,0x53,0xC6,0x55]))]
203 pkt = simple_tcp_packet()
204 exp_pkt = simple_tcp_packet(eth_dst="00:A1:CD:53:C6:55")
205 self.verify_modify(actions, pkt, exp_pkt)
206
207class SetEthSrc(BaseModifyPacketTest):
208 """
209 Set Eth Src address
210 """
211 def runTest(self):
212 actions = [ofp.action.set_field(ofp.oxm.eth_src([0x00,0xA1,0xCD,0x53,0xC6,0x55]))]
213 pkt = simple_tcp_packet()
214 exp_pkt = simple_tcp_packet(eth_src="00:A1:CD:53:C6:55")
215 self.verify_modify(actions, pkt, exp_pkt)
216
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700217class SetIpv4Dscp(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700218 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700219 Set IPv4 DSCP
Kiran Poolad06998a2013-08-07 14:51:50 -0700220 """
221 def runTest(self):
222 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
223 pkt = simple_tcp_packet()
224 exp_pkt = simple_tcp_packet(ip_tos=0x04)
225 self.verify_modify(actions, pkt, exp_pkt)
226
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700227class SetIpv4ECN(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700228 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700229 Set IPv4 ECN
Kiran Poolad06998a2013-08-07 14:51:50 -0700230 """
231 def runTest(self):
232 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x01))]
233 pkt = simple_tcp_packet()
234 exp_pkt = simple_tcp_packet(ip_tos=0x01)
235 self.verify_modify(actions, pkt, exp_pkt)
236
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700237class SetIpv4DSCP_NonZeroECN(BaseModifyPacketTest):
Kiran Poolafb523e52013-08-07 22:57:51 -0700238 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700239 Set IPv4 DSCP and make sure ECN is not modified
Kiran Poolafb523e52013-08-07 22:57:51 -0700240 """
241 def runTest(self):
242 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
243 pkt = simple_tcp_packet(ip_tos=0x11)
244 exp_pkt = simple_tcp_packet(ip_tos=0x05)
245 self.verify_modify(actions, pkt, exp_pkt)
246
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700247class SetIpv4ECN_NonZeroDSCP(BaseModifyPacketTest):
Kiran Poolafb523e52013-08-07 22:57:51 -0700248 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700249 Set IPv4 ECN and make sure DSCP is not modified
Kiran Poolafb523e52013-08-07 22:57:51 -0700250 """
251 def runTest(self):
252 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x02))]
253 pkt = simple_tcp_packet(ip_tos=0x11)
254 exp_pkt = simple_tcp_packet(ip_tos=0x12)
255 self.verify_modify(actions, pkt, exp_pkt)
256
Kiran Poolad06998a2013-08-07 14:51:50 -0700257class SetIPv4Src(BaseModifyPacketTest):
258 """
259 Set IPv4 srouce address
260 """
261 def runTest(self):
262 actions = [ofp.action.set_field(ofp.oxm.ipv4_src(167772161))]
263 pkt = simple_tcp_packet()
264 exp_pkt = simple_tcp_packet(ip_src="10.0.0.1")
265 self.verify_modify(actions, pkt, exp_pkt)
266
267class SetIPv4Dst(BaseModifyPacketTest):
268 """
269 Set IPv4 destination address
270 """
271 def runTest(self):
272 actions = [ofp.action.set_field(ofp.oxm.ipv4_dst(167772161))]
273 pkt = simple_tcp_packet()
274 exp_pkt = simple_tcp_packet(ip_dst="10.0.0.1")
275 self.verify_modify(actions, pkt, exp_pkt)
276
277class SetTCPSrc(BaseModifyPacketTest):
278 """
279 Set TCP source port
280 """
281 def runTest(self):
282 actions = [ofp.action.set_field(ofp.oxm.tcp_src(800))]
283 pkt = simple_tcp_packet()
284 exp_pkt = simple_tcp_packet(tcp_sport=800)
285 self.verify_modify(actions, pkt, exp_pkt)
286
287class SetTCPDst(BaseModifyPacketTest):
288 """
289 Set TCP destination port
290 """
291 def runTest(self):
292 actions = [ofp.action.set_field(ofp.oxm.tcp_dst(800))]
293 pkt = simple_tcp_packet()
294 exp_pkt = simple_tcp_packet(tcp_dport=800)
295 self.verify_modify(actions, pkt, exp_pkt)
296
297class SetUDPSrc(BaseModifyPacketTest):
298 """
299 Set UDP source port
300 """
301 def runTest(self):
302 actions = [ofp.action.set_field(ofp.oxm.udp_src(800))]
303 pkt = simple_udp_packet()
304 exp_pkt = simple_udp_packet(udp_sport=800)
305 self.verify_modify(actions, pkt, exp_pkt)
306
307class SetUDPDst(BaseModifyPacketTest):
308 """
309 Set UDP destination port
310 """
311 def runTest(self):
312 actions = [ofp.action.set_field(ofp.oxm.udp_dst(800))]
313 pkt = simple_udp_packet()
314 exp_pkt = simple_udp_packet(udp_dport=800)
315 self.verify_modify(actions, pkt, exp_pkt)
316
317class SetIPv6Src(BaseModifyPacketTest):
318 """
319 Set IPv6 source address
320 """
321 def runTest(self):
322 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"))]
323 pkt = simple_tcpv6_packet()
324 exp_pkt = simple_tcpv6_packet(ipv6_src="2001:abb1:3456:bccb:0000:0000:0370:7336")
325 self.verify_modify(actions, pkt, exp_pkt)
326
327class SetIPv6Dst(BaseModifyPacketTest):
328 """
329 Set IPv6 destination address
330 """
331 def runTest(self):
332 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"))]
333 pkt = simple_tcpv6_packet()
334 exp_pkt = simple_tcpv6_packet(ipv6_dst="2001:abb1:3456:bccb:0000:0000:0370:7336")
335 self.verify_modify(actions, pkt, exp_pkt)
336
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700337class SetIpv6Dscp(BaseModifyPacketTest):
338 """
339 Set IPv6 DSCP
340 """
341 def runTest(self):
342 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
343 pkt = simple_tcpv6_packet()
344 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x04)
345 self.verify_modify(actions, pkt, exp_pkt)
346
347class SetIpv6ECN(BaseModifyPacketTest):
348 """
349 Set IPv6 ECN
350 """
351 def runTest(self):
352 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x01))]
353 pkt = simple_tcpv6_packet()
354 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x01)
355 self.verify_modify(actions, pkt, exp_pkt)
356
Kiran Poolad06998a2013-08-07 14:51:50 -0700357class SetIPv6Flabel(BaseModifyPacketTest):
358 """
359 Set IPv6 Flabel
360 """
361 def runTest(self):
Kiran Poolafb523e52013-08-07 22:57:51 -0700362 actions = [ofp.action.set_field(ofp.oxm.ipv6_flabel(10))]
Kiran Poolad06998a2013-08-07 14:51:50 -0700363 pkt = simple_tcpv6_packet()
Kiran Poolafb523e52013-08-07 22:57:51 -0700364 exp_pkt = simple_tcpv6_packet(ipv6_fl=10)
Kiran Poolad06998a2013-08-07 14:51:50 -0700365 self.verify_modify(actions, pkt, exp_pkt)
366
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700367class SetIpv6DSCP_NonZeroECNandFlabel(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700368 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700369 Set IPv6 DSCP and make sure ECN is not modified
370 """
371 def runTest(self):
372 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
373 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
374 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x05, ipv6_fl=10)
375 self.verify_modify(actions, pkt, exp_pkt)
376
377class SetIpv6ECN_NonZeroDSCPandFlabel(BaseModifyPacketTest):
378 """
379 Set IPv6 ECN and make sure DSCP is not modified
380 """
381 def runTest(self):
382 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x02))]
383 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
384 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x12, ipv6_fl=10)
385 self.verify_modify(actions, pkt, exp_pkt)
386
387class SetIPv6Flabel_NonZeroDSCPandECN(BaseModifyPacketTest):
388 """
389 Set IPv6 Flabel
390 """
391 def runTest(self):
392 actions = [ofp.action.set_field(ofp.oxm.ipv6_flabel(10))]
393 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=9)
394 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
395 self.verify_modify(actions, pkt, exp_pkt)
396
397class SetIpv4TTL(BaseModifyPacketTest):
398 """
399 Set IPv4 TTL
Kiran Poolad06998a2013-08-07 14:51:50 -0700400 """
401 def runTest(self):
402 actions = [ofp.action.set_nw_ttl(10)]
403 pkt = simple_tcp_packet()
404 exp_pkt = simple_tcp_packet(ip_ttl=10)
405 self.verify_modify(actions, pkt, exp_pkt)
406
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700407class SetIpv6HopLimit(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700408 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700409 Set Ipv6 Hop Limit
410 """
411 def runTest(self):
412 actions = [ofp.action.set_nw_ttl(10)]
413 pkt = simple_tcpv6_packet()
414 exp_pkt = simple_tcpv6_packet(ipv6_hlim=10)
415 self.verify_modify(actions, pkt, exp_pkt)
416
417class DecIpv4TTL(BaseModifyPacketTest):
418 """
419 Decrement Ipv4 TTL
Kiran Poolad06998a2013-08-07 14:51:50 -0700420 """
421 def runTest(self):
422 actions = [ofp.action.dec_nw_ttl()]
423 pkt = simple_tcp_packet(ip_ttl=10)
424 exp_pkt = simple_tcp_packet(ip_ttl=9)
425 self.verify_modify(actions, pkt, exp_pkt)
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700426
427class DecIpv6HopLimit(BaseModifyPacketTest):
428 """
429 Decrement Ipv6 Hop Limit
430 """
431 def runTest(self):
432 actions = [ofp.action.dec_nw_ttl()]
433 pkt = simple_tcpv6_packet(ipv6_hlim=10)
434 exp_pkt = simple_tcpv6_packet(ipv6_hlim=9)
435 self.verify_modify(actions, pkt, exp_pkt)