blob: de57592e0390a4792f0299fb7179ef02bedb1214 [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
133class PopVlan(BaseModifyPacketTest):
134 """
135 Pop a vlan tag
136 """
137 def runTest(self):
138 actions = [ofp.action.pop_vlan()]
139 pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
140 exp_pkt = simple_tcp_packet()
141 self.verify_modify(actions, pkt, exp_pkt)
142
143class SetVlanVid(BaseModifyPacketTest):
144 """
145 Set the vlan vid
146 """
147 def runTest(self):
148 actions = [ofp.action.set_field(ofp.oxm.vlan_vid(2))]
149 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=1)
150 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2)
151 self.verify_modify(actions, pkt, exp_pkt)
152
153class SetVlanPcp(BaseModifyPacketTest):
154 """
155 Set the vlan priority
156 """
157 def runTest(self):
158 actions = [ofp.action.set_field(ofp.oxm.vlan_pcp(2))]
159 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=1)
160 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=2)
161 self.verify_modify(actions, pkt, exp_pkt)
Kiran Poolad06998a2013-08-07 14:51:50 -0700162
163class SetEthDst(BaseModifyPacketTest):
164 """
165 Set Eth Dst address
166 """
167 def runTest(self):
168 actions = [ofp.action.set_field(ofp.oxm.eth_dst([0x00,0xA1,0xCD,0x53,0xC6,0x55]))]
169 pkt = simple_tcp_packet()
170 exp_pkt = simple_tcp_packet(eth_dst="00:A1:CD:53:C6:55")
171 self.verify_modify(actions, pkt, exp_pkt)
172
173class SetEthSrc(BaseModifyPacketTest):
174 """
175 Set Eth Src address
176 """
177 def runTest(self):
178 actions = [ofp.action.set_field(ofp.oxm.eth_src([0x00,0xA1,0xCD,0x53,0xC6,0x55]))]
179 pkt = simple_tcp_packet()
180 exp_pkt = simple_tcp_packet(eth_src="00:A1:CD:53:C6:55")
181 self.verify_modify(actions, pkt, exp_pkt)
182
183class SetIpDscp(BaseModifyPacketTest):
184 """
185 Set IP DSCP
186 """
187 def runTest(self):
188 actions = [ofp.action.set_field(ofp.oxm.ip_dscp(0x01))]
189 pkt = simple_tcp_packet()
190 exp_pkt = simple_tcp_packet(ip_tos=0x04)
191 self.verify_modify(actions, pkt, exp_pkt)
192
193class SetIpECN(BaseModifyPacketTest):
194 """
195 Set IP ECN
196 """
197 def runTest(self):
198 actions = [ofp.action.set_field(ofp.oxm.ip_ecn(0x01))]
199 pkt = simple_tcp_packet()
200 exp_pkt = simple_tcp_packet(ip_tos=0x01)
201 self.verify_modify(actions, pkt, exp_pkt)
202
203class SetIPv4Src(BaseModifyPacketTest):
204 """
205 Set IPv4 srouce address
206 """
207 def runTest(self):
208 actions = [ofp.action.set_field(ofp.oxm.ipv4_src(167772161))]
209 pkt = simple_tcp_packet()
210 exp_pkt = simple_tcp_packet(ip_src="10.0.0.1")
211 self.verify_modify(actions, pkt, exp_pkt)
212
213class SetIPv4Dst(BaseModifyPacketTest):
214 """
215 Set IPv4 destination address
216 """
217 def runTest(self):
218 actions = [ofp.action.set_field(ofp.oxm.ipv4_dst(167772161))]
219 pkt = simple_tcp_packet()
220 exp_pkt = simple_tcp_packet(ip_dst="10.0.0.1")
221 self.verify_modify(actions, pkt, exp_pkt)
222
223class SetTCPSrc(BaseModifyPacketTest):
224 """
225 Set TCP source port
226 """
227 def runTest(self):
228 actions = [ofp.action.set_field(ofp.oxm.tcp_src(800))]
229 pkt = simple_tcp_packet()
230 exp_pkt = simple_tcp_packet(tcp_sport=800)
231 self.verify_modify(actions, pkt, exp_pkt)
232
233class SetTCPDst(BaseModifyPacketTest):
234 """
235 Set TCP destination port
236 """
237 def runTest(self):
238 actions = [ofp.action.set_field(ofp.oxm.tcp_dst(800))]
239 pkt = simple_tcp_packet()
240 exp_pkt = simple_tcp_packet(tcp_dport=800)
241 self.verify_modify(actions, pkt, exp_pkt)
242
243class SetUDPSrc(BaseModifyPacketTest):
244 """
245 Set UDP source port
246 """
247 def runTest(self):
248 actions = [ofp.action.set_field(ofp.oxm.udp_src(800))]
249 pkt = simple_udp_packet()
250 exp_pkt = simple_udp_packet(udp_sport=800)
251 self.verify_modify(actions, pkt, exp_pkt)
252
253class SetUDPDst(BaseModifyPacketTest):
254 """
255 Set UDP destination port
256 """
257 def runTest(self):
258 actions = [ofp.action.set_field(ofp.oxm.udp_dst(800))]
259 pkt = simple_udp_packet()
260 exp_pkt = simple_udp_packet(udp_dport=800)
261 self.verify_modify(actions, pkt, exp_pkt)
262
263class SetIPv6Src(BaseModifyPacketTest):
264 """
265 Set IPv6 source address
266 """
267 def runTest(self):
268 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"))]
269 pkt = simple_tcpv6_packet()
270 exp_pkt = simple_tcpv6_packet(ipv6_src="2001:abb1:3456:bccb:0000:0000:0370:7336")
271 self.verify_modify(actions, pkt, exp_pkt)
272
273class SetIPv6Dst(BaseModifyPacketTest):
274 """
275 Set IPv6 destination address
276 """
277 def runTest(self):
278 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"))]
279 pkt = simple_tcpv6_packet()
280 exp_pkt = simple_tcpv6_packet(ipv6_dst="2001:abb1:3456:bccb:0000:0000:0370:7336")
281 self.verify_modify(actions, pkt, exp_pkt)
282
283class SetIPv6Flabel(BaseModifyPacketTest):
284 """
285 Set IPv6 Flabel
286 """
287 def runTest(self):
288 actions = [ofp.action.set_field(ofp.oxm.ipv6_flabel(0))]
289 pkt = simple_tcpv6_packet()
290 exp_pkt = simple_tcpv6_packet(ipv6_fl=0)
291 self.verify_modify(actions, pkt, exp_pkt)
292
293class SetNwTTL(BaseModifyPacketTest):
294 """
295 Set Nw TTL
296 """
297 def runTest(self):
298 actions = [ofp.action.set_nw_ttl(10)]
299 pkt = simple_tcp_packet()
300 exp_pkt = simple_tcp_packet(ip_ttl=10)
301 self.verify_modify(actions, pkt, exp_pkt)
302
303class DecNwTTL(BaseModifyPacketTest):
304 """
305 Decrement Nw TTL
306 """
307 def runTest(self):
308 actions = [ofp.action.dec_nw_ttl()]
309 pkt = simple_tcp_packet(ip_ttl=10)
310 exp_pkt = simple_tcp_packet(ip_ttl=9)
311 self.verify_modify(actions, pkt, exp_pkt)