blob: a5239d84490cf0d7d4d8a37598fbedd94ea06ba5 [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Rich Laneb626a9c2013-08-05 16:45:50 -070017# Distributed under the OpenFlow Software License (see LICENSE)
18# Copyright (c) 2010 The Board of Trustees of The Leland Stanford Junior University
19# Copyright (c) 2012, 2013 Big Switch Networks, Inc.
20"""
21Action test cases
22
23These tests check the behavior of each type of action. The matches used are
24exact-match, to satisfy the OXM prerequisites of the set-field actions.
25These tests use a single apply-actions instruction.
26"""
27
28import logging
29
30from oftest import config
31import oftest.base_tests as base_tests
32import ofp
33from loxi.pp import pp
34
35from oftest.testutils import *
36from oftest.parse import parse_ipv6
37
38class Output(base_tests.SimpleDataPlane):
39 """
40 Output to a single port
41 """
42 def runTest(self):
Rich Lane045db072013-08-06 13:16:30 -070043 in_port, out_port = openflow_ports(2)
Rich Laneb626a9c2013-08-05 16:45:50 -070044
45 actions = [ofp.action.output(out_port)]
46
47 pkt = simple_tcp_packet()
48
49 logging.info("Running actions test for %s", pp(actions))
50
51 delete_all_flows(self.controller)
52
53 logging.info("Inserting flow")
54 request = ofp.message.flow_add(
Wilson Ng6f539642013-10-28 18:17:44 -070055 table_id=test_param_get("table", 0),
Rich Laneb626a9c2013-08-05 16:45:50 -070056 match=packet_to_flow_match(self, pkt),
57 instructions=[
58 ofp.instruction.apply_actions(actions)],
59 buffer_id=ofp.OFP_NO_BUFFER,
60 priority=1000)
61 self.controller.message_send(request)
62
63 do_barrier(self.controller)
64
65 pktstr = str(pkt)
66
67 logging.info("Sending packet, expecting output to port %d", out_port)
68 self.dataplane.send(in_port, pktstr)
Rich Lanee4b384d2013-09-13 14:33:40 -070069 verify_packets(self, pktstr, [out_port])
Rich Laneb626a9c2013-08-05 16:45:50 -070070
71class OutputMultiple(base_tests.SimpleDataPlane):
72 """
73 Output to three ports
74 """
75 def runTest(self):
Rich Lane045db072013-08-06 13:16:30 -070076 ports = openflow_ports(4)
Rich Laneb626a9c2013-08-05 16:45:50 -070077 in_port = ports[0]
78 out_ports = ports[1:4]
79
80 actions = [ofp.action.output(x) for x in out_ports]
81
82 pkt = simple_tcp_packet()
83
84 logging.info("Running actions test for %s", pp(actions))
85
86 delete_all_flows(self.controller)
87
88 logging.info("Inserting flow")
89 request = ofp.message.flow_add(
Wilson Ng6f539642013-10-28 18:17:44 -070090 table_id=test_param_get("table", 0),
Rich Laneb626a9c2013-08-05 16:45:50 -070091 match=packet_to_flow_match(self, pkt),
92 instructions=[
93 ofp.instruction.apply_actions(actions)],
94 buffer_id=ofp.OFP_NO_BUFFER,
95 priority=1000)
96 self.controller.message_send(request)
97
98 do_barrier(self.controller)
99
100 pktstr = str(pkt)
101
102 logging.info("Sending packet, expecting output to ports %r", out_ports)
103 self.dataplane.send(in_port, pktstr)
Rich Lanee4b384d2013-09-13 14:33:40 -0700104 verify_packets(self, pktstr, out_ports)
Rich Laneb626a9c2013-08-05 16:45:50 -0700105
106class BaseModifyPacketTest(base_tests.SimpleDataPlane):
107 """
108 Base class for action tests that modify a packet
109 """
110
111 def verify_modify(self, actions, pkt, exp_pkt):
Rich Lane045db072013-08-06 13:16:30 -0700112 in_port, out_port = openflow_ports(2)
Rich Laneb626a9c2013-08-05 16:45:50 -0700113
114 actions = actions + [ofp.action.output(out_port)]
115
116 logging.info("Running actions test for %s", pp(actions))
117
118 delete_all_flows(self.controller)
119
120 logging.info("Inserting flow")
121 request = ofp.message.flow_add(
Wilson Ng6f539642013-10-28 18:17:44 -0700122 table_id=test_param_get("table", 0),
Rich Laneb626a9c2013-08-05 16:45:50 -0700123 match=packet_to_flow_match(self, pkt),
124 instructions=[
125 ofp.instruction.apply_actions(actions)],
126 buffer_id=ofp.OFP_NO_BUFFER,
127 priority=1000)
128 self.controller.message_send(request)
129
130 do_barrier(self.controller)
131
132 logging.info("Sending packet, expecting output to port %d", out_port)
133 self.dataplane.send(in_port, str(pkt))
Rich Lanee4b384d2013-09-13 14:33:40 -0700134 verify_packets(self, str(exp_pkt), [out_port])
135
Rich Laneb626a9c2013-08-05 16:45:50 -0700136
137class PushVlan(BaseModifyPacketTest):
138 """
139 Push a vlan tag (vid=0, pcp=0)
140 """
141 def runTest(self):
142 actions = [ofp.action.push_vlan(ethertype=0x8100)]
143 pkt = simple_tcp_packet()
144 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
145 self.verify_modify(actions, pkt, exp_pkt)
146
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700147class PushVlanVid(BaseModifyPacketTest):
148 """
149 Push a vlan tag (vid=2, pcp=0)
150 """
151 def runTest(self):
152 actions = [ofp.action.push_vlan(ethertype=0x8100),
153 ofp.action.set_field(ofp.oxm.vlan_vid(2))]
154 pkt = simple_tcp_packet()
155 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2, pktlen=104)
156 self.verify_modify(actions, pkt, exp_pkt)
157
158class PushVlanVidPcp(BaseModifyPacketTest):
159 """
160 Push a vlan tag (vid=2, pcp=3)
161 """
162 def runTest(self):
163 actions = [ofp.action.push_vlan(ethertype=0x8100),
164 ofp.action.set_field(ofp.oxm.vlan_vid(2)),
165 ofp.action.set_field(ofp.oxm.vlan_pcp(3))]
166 pkt = simple_tcp_packet()
167 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2, vlan_pcp=3, pktlen=104)
168 self.verify_modify(actions, pkt, exp_pkt)
169
170class PushVlanPcp(BaseModifyPacketTest):
171 """
172 Push a vlan tag (vid=0, pcp=3)
173 """
174 def runTest(self):
175 actions = [ofp.action.push_vlan(ethertype=0x8100),
176 ofp.action.set_field(ofp.oxm.vlan_pcp(3))]
177 pkt = simple_tcp_packet()
178 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=0, vlan_pcp=3, pktlen=104)
179 self.verify_modify(actions, pkt, exp_pkt)
180
Rich Laneb626a9c2013-08-05 16:45:50 -0700181class PopVlan(BaseModifyPacketTest):
182 """
183 Pop a vlan tag
184 """
185 def runTest(self):
186 actions = [ofp.action.pop_vlan()]
187 pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
188 exp_pkt = simple_tcp_packet()
189 self.verify_modify(actions, pkt, exp_pkt)
190
191class SetVlanVid(BaseModifyPacketTest):
192 """
193 Set the vlan vid
194 """
195 def runTest(self):
196 actions = [ofp.action.set_field(ofp.oxm.vlan_vid(2))]
197 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=1)
198 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2)
199 self.verify_modify(actions, pkt, exp_pkt)
200
201class SetVlanPcp(BaseModifyPacketTest):
202 """
203 Set the vlan priority
204 """
205 def runTest(self):
206 actions = [ofp.action.set_field(ofp.oxm.vlan_pcp(2))]
207 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=1)
208 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=2)
209 self.verify_modify(actions, pkt, exp_pkt)
Kiran Poolad06998a2013-08-07 14:51:50 -0700210
211class SetEthDst(BaseModifyPacketTest):
212 """
213 Set Eth Dst address
214 """
215 def runTest(self):
216 actions = [ofp.action.set_field(ofp.oxm.eth_dst([0x00,0xA1,0xCD,0x53,0xC6,0x55]))]
217 pkt = simple_tcp_packet()
218 exp_pkt = simple_tcp_packet(eth_dst="00:A1:CD:53:C6:55")
219 self.verify_modify(actions, pkt, exp_pkt)
220
221class SetEthSrc(BaseModifyPacketTest):
222 """
223 Set Eth Src address
224 """
225 def runTest(self):
226 actions = [ofp.action.set_field(ofp.oxm.eth_src([0x00,0xA1,0xCD,0x53,0xC6,0x55]))]
227 pkt = simple_tcp_packet()
228 exp_pkt = simple_tcp_packet(eth_src="00:A1:CD:53:C6:55")
229 self.verify_modify(actions, pkt, exp_pkt)
230
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700231class SetIpv4Dscp(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700232 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700233 Set IPv4 DSCP
Kiran Poolad06998a2013-08-07 14:51:50 -0700234 """
235 def runTest(self):
236 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
237 pkt = simple_tcp_packet()
238 exp_pkt = simple_tcp_packet(ip_tos=0x04)
239 self.verify_modify(actions, pkt, exp_pkt)
240
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700241class SetIpv4ECN(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700242 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700243 Set IPv4 ECN
Kiran Poolad06998a2013-08-07 14:51:50 -0700244 """
245 def runTest(self):
246 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x01))]
247 pkt = simple_tcp_packet()
248 exp_pkt = simple_tcp_packet(ip_tos=0x01)
249 self.verify_modify(actions, pkt, exp_pkt)
250
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700251class SetIpv4DSCP_NonZeroECN(BaseModifyPacketTest):
Kiran Poolafb523e52013-08-07 22:57:51 -0700252 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700253 Set IPv4 DSCP and make sure ECN is not modified
Kiran Poolafb523e52013-08-07 22:57:51 -0700254 """
255 def runTest(self):
256 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
257 pkt = simple_tcp_packet(ip_tos=0x11)
258 exp_pkt = simple_tcp_packet(ip_tos=0x05)
259 self.verify_modify(actions, pkt, exp_pkt)
260
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700261class SetIpv4ECN_NonZeroDSCP(BaseModifyPacketTest):
Kiran Poolafb523e52013-08-07 22:57:51 -0700262 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700263 Set IPv4 ECN and make sure DSCP is not modified
Kiran Poolafb523e52013-08-07 22:57:51 -0700264 """
265 def runTest(self):
266 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x02))]
267 pkt = simple_tcp_packet(ip_tos=0x11)
268 exp_pkt = simple_tcp_packet(ip_tos=0x12)
269 self.verify_modify(actions, pkt, exp_pkt)
270
Kiran Poolad06998a2013-08-07 14:51:50 -0700271class SetIPv4Src(BaseModifyPacketTest):
272 """
273 Set IPv4 srouce address
274 """
275 def runTest(self):
276 actions = [ofp.action.set_field(ofp.oxm.ipv4_src(167772161))]
277 pkt = simple_tcp_packet()
278 exp_pkt = simple_tcp_packet(ip_src="10.0.0.1")
279 self.verify_modify(actions, pkt, exp_pkt)
280
281class SetIPv4Dst(BaseModifyPacketTest):
282 """
283 Set IPv4 destination address
284 """
285 def runTest(self):
286 actions = [ofp.action.set_field(ofp.oxm.ipv4_dst(167772161))]
287 pkt = simple_tcp_packet()
288 exp_pkt = simple_tcp_packet(ip_dst="10.0.0.1")
289 self.verify_modify(actions, pkt, exp_pkt)
290
291class SetTCPSrc(BaseModifyPacketTest):
292 """
293 Set TCP source port
294 """
295 def runTest(self):
296 actions = [ofp.action.set_field(ofp.oxm.tcp_src(800))]
297 pkt = simple_tcp_packet()
298 exp_pkt = simple_tcp_packet(tcp_sport=800)
299 self.verify_modify(actions, pkt, exp_pkt)
300
301class SetTCPDst(BaseModifyPacketTest):
302 """
303 Set TCP destination port
304 """
305 def runTest(self):
306 actions = [ofp.action.set_field(ofp.oxm.tcp_dst(800))]
307 pkt = simple_tcp_packet()
308 exp_pkt = simple_tcp_packet(tcp_dport=800)
309 self.verify_modify(actions, pkt, exp_pkt)
310
311class SetUDPSrc(BaseModifyPacketTest):
312 """
313 Set UDP source port
314 """
315 def runTest(self):
316 actions = [ofp.action.set_field(ofp.oxm.udp_src(800))]
317 pkt = simple_udp_packet()
318 exp_pkt = simple_udp_packet(udp_sport=800)
319 self.verify_modify(actions, pkt, exp_pkt)
320
321class SetUDPDst(BaseModifyPacketTest):
322 """
323 Set UDP destination port
324 """
325 def runTest(self):
326 actions = [ofp.action.set_field(ofp.oxm.udp_dst(800))]
327 pkt = simple_udp_packet()
328 exp_pkt = simple_udp_packet(udp_dport=800)
329 self.verify_modify(actions, pkt, exp_pkt)
330
331class SetIPv6Src(BaseModifyPacketTest):
332 """
333 Set IPv6 source address
334 """
335 def runTest(self):
336 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"))]
337 pkt = simple_tcpv6_packet()
338 exp_pkt = simple_tcpv6_packet(ipv6_src="2001:abb1:3456:bccb:0000:0000:0370:7336")
339 self.verify_modify(actions, pkt, exp_pkt)
340
341class SetIPv6Dst(BaseModifyPacketTest):
342 """
343 Set IPv6 destination address
344 """
345 def runTest(self):
346 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"))]
347 pkt = simple_tcpv6_packet()
348 exp_pkt = simple_tcpv6_packet(ipv6_dst="2001:abb1:3456:bccb:0000:0000:0370:7336")
349 self.verify_modify(actions, pkt, exp_pkt)
350
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700351class SetIpv6Dscp(BaseModifyPacketTest):
352 """
353 Set IPv6 DSCP
354 """
355 def runTest(self):
356 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
357 pkt = simple_tcpv6_packet()
358 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x04)
359 self.verify_modify(actions, pkt, exp_pkt)
360
361class SetIpv6ECN(BaseModifyPacketTest):
362 """
363 Set IPv6 ECN
364 """
365 def runTest(self):
366 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x01))]
367 pkt = simple_tcpv6_packet()
368 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x01)
369 self.verify_modify(actions, pkt, exp_pkt)
370
Kiran Poolad06998a2013-08-07 14:51:50 -0700371class SetIPv6Flabel(BaseModifyPacketTest):
372 """
373 Set IPv6 Flabel
374 """
375 def runTest(self):
Kiran Poolafb523e52013-08-07 22:57:51 -0700376 actions = [ofp.action.set_field(ofp.oxm.ipv6_flabel(10))]
Kiran Poolad06998a2013-08-07 14:51:50 -0700377 pkt = simple_tcpv6_packet()
Kiran Poolafb523e52013-08-07 22:57:51 -0700378 exp_pkt = simple_tcpv6_packet(ipv6_fl=10)
Kiran Poolad06998a2013-08-07 14:51:50 -0700379 self.verify_modify(actions, pkt, exp_pkt)
380
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700381class SetIpv6DSCP_NonZeroECNandFlabel(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700382 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700383 Set IPv6 DSCP and make sure ECN is not modified
384 """
385 def runTest(self):
386 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
387 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
388 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x05, ipv6_fl=10)
389 self.verify_modify(actions, pkt, exp_pkt)
390
391class SetIpv6ECN_NonZeroDSCPandFlabel(BaseModifyPacketTest):
392 """
393 Set IPv6 ECN and make sure DSCP is not modified
394 """
395 def runTest(self):
396 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x02))]
397 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
398 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x12, ipv6_fl=10)
399 self.verify_modify(actions, pkt, exp_pkt)
400
401class SetIPv6Flabel_NonZeroDSCPandECN(BaseModifyPacketTest):
402 """
403 Set IPv6 Flabel
404 """
405 def runTest(self):
406 actions = [ofp.action.set_field(ofp.oxm.ipv6_flabel(10))]
407 pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=9)
408 exp_pkt = simple_tcpv6_packet(ipv6_tc=0x11, ipv6_fl=10)
409 self.verify_modify(actions, pkt, exp_pkt)
410
411class SetIpv4TTL(BaseModifyPacketTest):
412 """
413 Set IPv4 TTL
Kiran Poolad06998a2013-08-07 14:51:50 -0700414 """
415 def runTest(self):
416 actions = [ofp.action.set_nw_ttl(10)]
417 pkt = simple_tcp_packet()
418 exp_pkt = simple_tcp_packet(ip_ttl=10)
419 self.verify_modify(actions, pkt, exp_pkt)
420
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700421class SetIpv6HopLimit(BaseModifyPacketTest):
Kiran Poolad06998a2013-08-07 14:51:50 -0700422 """
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700423 Set Ipv6 Hop Limit
424 """
425 def runTest(self):
426 actions = [ofp.action.set_nw_ttl(10)]
427 pkt = simple_tcpv6_packet()
428 exp_pkt = simple_tcpv6_packet(ipv6_hlim=10)
429 self.verify_modify(actions, pkt, exp_pkt)
430
431class DecIpv4TTL(BaseModifyPacketTest):
432 """
433 Decrement Ipv4 TTL
Kiran Poolad06998a2013-08-07 14:51:50 -0700434 """
435 def runTest(self):
436 actions = [ofp.action.dec_nw_ttl()]
437 pkt = simple_tcp_packet(ip_ttl=10)
438 exp_pkt = simple_tcp_packet(ip_ttl=9)
439 self.verify_modify(actions, pkt, exp_pkt)
Kiran Poola4ed39ce2013-08-08 21:46:17 -0700440
441class DecIpv6HopLimit(BaseModifyPacketTest):
442 """
443 Decrement Ipv6 Hop Limit
444 """
445 def runTest(self):
446 actions = [ofp.action.dec_nw_ttl()]
447 pkt = simple_tcpv6_packet(ipv6_hlim=10)
448 exp_pkt = simple_tcpv6_packet(ipv6_hlim=9)
449 self.verify_modify(actions, pkt, exp_pkt)