1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # vim: tabstop=4 shiftwidth=4 softtabstop=4
20 from ryu.ofproto import ofproto_v1_2
21 from ryu.ofproto import ether
22 from ryu.ofproto import inet
23 from ryu.tests.integrated import tester
25 LOG = logging.getLogger(__name__)
28 class RunTest(tester.TestFlowBase):
29 """ Test case for add flows of Actions
31 OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION]
33 def __init__(self, *args, **kwargs):
34 super(RunTest, self).__init__(*args, **kwargs)
38 def add_apply_actions(self, dp, actions, match=None):
39 inst = [dp.ofproto_parser.OFPInstructionActions(
40 dp.ofproto.OFPIT_APPLY_ACTIONS, actions)]
42 match = dp.ofproto_parser.OFPMatch()
43 m = dp.ofproto_parser.OFPFlowMod(dp, 0, 0, 0,
45 0, 0, 0xff, 0xffffffff,
51 def add_set_field_action(self, dp, field, value, match=None):
52 self._verify = [dp.ofproto.OFPAT_SET_FIELD,
53 'field', field, value]
54 f = dp.ofproto_parser.OFPMatchField.make(field, value)
55 actions = [dp.ofproto_parser.OFPActionSetField(f), ]
56 self.add_apply_actions(dp, actions, match=match)
58 def verify_default(self, dp, stats):
62 type_ = name = field = value = None
65 elif len(verify) == 3:
66 (type_, name, value) = verify
67 elif len(verify) == 4:
68 (type_, name, field, value) = verify
70 return "self._verify is invalid."
73 action = stats[0].instructions[0].actions[0]
74 if action.cls_action_type != type_:
75 return "Action type error. send:%s, val:%s" \
76 % (type_, action.cls_action_type)
78 return "Action is not setting."
83 s_val = getattr(action, name)
84 except AttributeError:
88 if s_val.header != field:
89 return "Field error. send:%s val:%s" \
90 % (field, s_val.header)
93 if name and s_val != value:
94 return "Value error. send:%s=%s val:%s" \
95 % (name, value, s_val)
99 def verify_action_drop(self, dp, stats):
101 for i in s.instructions:
103 return "has actions. %s" % (i.actions)
106 # Test of General Actions
107 def test_action_output(self, dp):
109 self._verify = [dp.ofproto.OFPAT_OUTPUT,
112 actions = [dp.ofproto_parser.OFPActionOutput(out_port, 0), ]
113 self.add_apply_actions(dp, actions)
115 def test_action_drop(self, dp):
116 self.add_apply_actions(dp, [])
118 # Test of Push-Tag/Pop-Tag Actions
119 def test_action_push_vlan(self, dp):
120 ethertype = ether.ETH_TYPE_8021Q
121 self._verify = [dp.ofproto.OFPAT_PUSH_VLAN,
122 'ethertype', ethertype]
124 actions = [dp.ofproto_parser.OFPActionPushVlan(ethertype)]
125 self.add_apply_actions(dp, actions)
127 def test_action_pop_vlan(self, dp):
128 self._verify = [dp.ofproto.OFPAT_POP_VLAN, ]
130 actions = [dp.ofproto_parser.OFPActionPopVlan(), ]
131 match = dp.ofproto_parser.OFPMatch()
132 match.set_vlan_vid(1)
133 self.add_apply_actions(dp, actions, match)
135 def test_action_push_mpls(self, dp):
136 ethertype = ether.ETH_TYPE_MPLS
137 self._verify = [dp.ofproto.OFPAT_PUSH_MPLS,
138 'ethertype', ethertype]
140 actions = [dp.ofproto_parser.OFPActionPushMpls(ethertype), ]
141 self.add_apply_actions(dp, actions)
143 def test_action_pop_mpls(self, dp):
144 ethertype = ether.ETH_TYPE_8021Q
145 self._verify = [dp.ofproto.OFPAT_POP_MPLS,
146 'ethertype', ethertype]
147 actions = [dp.ofproto_parser.OFPActionPopMpls(ethertype), ]
148 match = dp.ofproto_parser.OFPMatch()
149 match.set_dl_type(ether.ETH_TYPE_MPLS)
150 self.add_apply_actions(dp, actions, match)
152 # Test of Set-Filed Actions
153 def test_action_set_field_dl_dst(self, dp):
154 field = dp.ofproto.OXM_OF_ETH_DST
155 dl_dst = 'e2:7a:09:79:0b:0f'
156 value = self.haddr_to_bin(dl_dst)
158 self.add_set_field_action(dp, field, value)
160 def test_action_set_field_dl_src(self, dp):
161 field = dp.ofproto.OXM_OF_ETH_SRC
162 dl_src = '08:82:63:b6:62:05'
163 value = self.haddr_to_bin(dl_src)
165 self.add_set_field_action(dp, field, value)
167 def test_action_set_field_dl_type(self, dp):
168 field = dp.ofproto.OXM_OF_ETH_TYPE
169 value = ether.ETH_TYPE_IPV6
171 self.add_set_field_action(dp, field, value)
173 def test_action_set_field_vlan_vid(self, dp):
174 field = dp.ofproto.OXM_OF_VLAN_VID
177 match = dp.ofproto_parser.OFPMatch()
178 match.set_vlan_vid(1)
180 self.add_set_field_action(dp, field, value, match)
182 def test_action_set_field_vlan_pcp(self, dp):
183 field = dp.ofproto.OXM_OF_VLAN_PCP
186 match = dp.ofproto_parser.OFPMatch()
187 match.set_vlan_vid(1)
189 self.add_set_field_action(dp, field, value, match)
191 def test_action_set_field_nw_dscp(self, dp):
192 field = dp.ofproto.OXM_OF_IP_DSCP
195 match = dp.ofproto_parser.OFPMatch()
196 match.set_dl_type(ether.ETH_TYPE_IP)
198 self.add_set_field_action(dp, field, value, match)
200 def test_action_set_field_nw_ecn(self, dp):
201 field = dp.ofproto.OXM_OF_IP_ECN
204 match = dp.ofproto_parser.OFPMatch()
205 match.set_dl_type(ether.ETH_TYPE_IP)
207 self.add_set_field_action(dp, field, value, match)
209 def test_action_set_field_ip_proto(self, dp):
210 field = dp.ofproto.OXM_OF_IP_PROTO
211 value = inet.IPPROTO_TCP
213 match = dp.ofproto_parser.OFPMatch()
214 match.set_dl_type(ether.ETH_TYPE_IP)
216 self.add_set_field_action(dp, field, value, match)
218 def test_action_set_field_ipv4_src(self, dp):
219 field = dp.ofproto.OXM_OF_IPV4_SRC
220 ipv4_src = '192.168.3.92'
221 value = self.ipv4_to_int(ipv4_src)
223 match = dp.ofproto_parser.OFPMatch()
224 match.set_dl_type(ether.ETH_TYPE_IP)
226 self.add_set_field_action(dp, field, value, match)
228 def test_action_set_field_ipv4_dst(self, dp):
229 field = dp.ofproto.OXM_OF_IPV4_DST
230 ipv4_dst = '192.168.74.122'
231 value = self.ipv4_to_int(ipv4_dst)
233 match = dp.ofproto_parser.OFPMatch()
234 match.set_dl_type(ether.ETH_TYPE_IP)
236 self.add_set_field_action(dp, field, value, match)
238 def test_action_set_field_tcp_src(self, dp):
239 field = dp.ofproto.OXM_OF_TCP_SRC
242 match = dp.ofproto_parser.OFPMatch()
243 match.set_dl_type(ether.ETH_TYPE_IP)
244 match.set_ip_proto(inet.IPPROTO_TCP)
246 self.add_set_field_action(dp, field, value, match)
248 def test_action_set_field_tcp_dst(self, dp):
249 field = dp.ofproto.OXM_OF_TCP_DST
252 match = dp.ofproto_parser.OFPMatch()
253 match.set_dl_type(ether.ETH_TYPE_IP)
254 match.set_ip_proto(inet.IPPROTO_TCP)
256 self.add_set_field_action(dp, field, value, match)
258 def test_action_set_field_udp_src(self, dp):
259 field = dp.ofproto.OXM_OF_UDP_SRC
262 match = dp.ofproto_parser.OFPMatch()
263 match.set_dl_type(ether.ETH_TYPE_IP)
264 match.set_ip_proto(inet.IPPROTO_UDP)
266 self.add_set_field_action(dp, field, value, match)
268 def test_action_set_field_udp_dst(self, dp):
269 field = dp.ofproto.OXM_OF_UDP_DST
272 match = dp.ofproto_parser.OFPMatch()
273 match.set_dl_type(ether.ETH_TYPE_IP)
274 match.set_ip_proto(inet.IPPROTO_UDP)
276 self.add_set_field_action(dp, field, value, match)
278 def test_action_set_field_icmpv4_type(self, dp):
279 field = dp.ofproto.OXM_OF_ICMPV4_TYPE
282 match = dp.ofproto_parser.OFPMatch()
283 match.set_ip_proto(inet.IPPROTO_ICMP)
285 self.add_set_field_action(dp, field, value, match)
287 def test_action_set_field_icmpv4_code(self, dp):
288 field = dp.ofproto.OXM_OF_ICMPV4_CODE
291 match = dp.ofproto_parser.OFPMatch()
292 match.set_ip_proto(inet.IPPROTO_ICMP)
294 self.add_set_field_action(dp, field, value, match)
296 def test_action_set_field_arp_op(self, dp):
297 field = dp.ofproto.OXM_OF_ARP_OP
300 match = dp.ofproto_parser.OFPMatch()
301 match.set_dl_type(ether.ETH_TYPE_ARP)
302 self.add_set_field_action(dp, field, value, match)
304 def test_action_set_field_arp_spa(self, dp):
305 field = dp.ofproto.OXM_OF_ARP_SPA
306 nw_src = '192.168.132.179'
307 value = self.ipv4_to_int(nw_src)
309 match = dp.ofproto_parser.OFPMatch()
310 match.set_dl_type(ether.ETH_TYPE_ARP)
311 self.add_set_field_action(dp, field, value, match)
313 def test_action_set_field_arp_tpa(self, dp):
314 field = dp.ofproto.OXM_OF_ARP_TPA
315 nw_dst = '192.168.118.85'
316 value = self.ipv4_to_int(nw_dst)
318 match = dp.ofproto_parser.OFPMatch()
319 match.set_dl_type(ether.ETH_TYPE_ARP)
320 self.add_set_field_action(dp, field, value, match)
322 def test_action_set_field_arp_sha(self, dp):
323 field = dp.ofproto.OXM_OF_ARP_SHA
324 arp_sha = '50:29:e7:7f:6c:7f'
325 value = self.haddr_to_bin(arp_sha)
327 match = dp.ofproto_parser.OFPMatch()
328 match.set_dl_type(ether.ETH_TYPE_ARP)
329 self.add_set_field_action(dp, field, value, match)
331 def test_action_set_field_arp_tha(self, dp):
332 field = dp.ofproto.OXM_OF_ARP_THA
333 arp_tha = '71:c8:72:2f:47:fd'
334 value = self.haddr_to_bin(arp_tha)
336 match = dp.ofproto_parser.OFPMatch()
337 match.set_dl_type(ether.ETH_TYPE_ARP)
338 self.add_set_field_action(dp, field, value, match)
340 def test_action_set_field_ipv6_src(self, dp):
341 field = dp.ofproto.OXM_OF_IPV6_SRC
342 ipv6_src = '7527:c798:c772:4a18:117a:14ff:c1b6:e4ef'
343 value = self.ipv6_to_int(ipv6_src)
345 match = dp.ofproto_parser.OFPMatch()
346 match.set_dl_type(0x86dd)
348 self.add_set_field_action(dp, field, value, match)
350 def test_action_set_field_ipv6_dst(self, dp):
351 field = dp.ofproto.OXM_OF_IPV6_DST
352 ipv6_dst = '8893:65b3:6b49:3bdb:3d2:9401:866c:c96'
353 value = self.ipv6_to_int(ipv6_dst)
355 match = dp.ofproto_parser.OFPMatch()
356 match.set_dl_type(0x86dd)
358 self.add_set_field_action(dp, field, value, match)
360 def test_action_set_field_ipv6_flabel(self, dp):
361 field = dp.ofproto.OXM_OF_IPV6_FLABEL
364 self.add_set_field_action(dp, field, value)
366 def test_action_set_field_icmpv6_type(self, dp):
367 field = dp.ofproto.OXM_OF_ICMPV6_TYPE
370 self.add_set_field_action(dp, field, value)
372 def test_action_set_field_icmpv6_code(self, dp):
373 field = dp.ofproto.OXM_OF_ICMPV6_CODE
376 self.add_set_field_action(dp, field, value)
378 def test_action_set_field_ipv6_nd_target(self, dp):
379 field = dp.ofproto.OXM_OF_IPV6_ND_TARGET
380 target = "5420:db3f:921b:3e33:2791:98f:dd7f:2e19"
381 value = self.ipv6_to_int(target)
383 self.add_set_field_action(dp, field, value)
385 def test_action_set_field_ipv6_nd_sll(self, dp):
386 field = dp.ofproto.OXM_OF_IPV6_ND_SLL
387 sll = "54:db:3f:3e:27:19"
388 value = self.haddr_to_bin(sll)
390 self.add_set_field_action(dp, field, value)
392 def test_action_set_field_ipv6_nd_tll(self, dp):
393 field = dp.ofproto.OXM_OF_IPV6_ND_TLL
394 tll = "83:13:48:1e:d0:b0"
395 value = self.haddr_to_bin(tll)
397 self.add_set_field_action(dp, field, value)
399 def test_action_set_field_mpls_label(self, dp):
400 field = dp.ofproto.OXM_OF_MPLS_LABEL
403 match = dp.ofproto_parser.OFPMatch()
404 match.set_dl_type(ether.ETH_TYPE_MPLS)
406 self.add_set_field_action(dp, field, value, match)
408 def test_action_set_field_mpls_tc(self, dp):
409 field = dp.ofproto.OXM_OF_MPLS_TC
412 match = dp.ofproto_parser.OFPMatch()
413 match.set_dl_type(ether.ETH_TYPE_MPLS)
415 self.add_set_field_action(dp, field, value, match)
417 # Test of Change-TTL Actions
418 def test_action_set_mpls_ttl(self, dp):
420 self._verify = [dp.ofproto.OFPAT_SET_MPLS_TTL,
421 'mpls_ttl', mpls_ttl]
422 actions = [dp.ofproto_parser.OFPActionSetMplsTtl(mpls_ttl), ]
423 match = dp.ofproto_parser.OFPMatch()
424 match.set_dl_type(ether.ETH_TYPE_MPLS)
425 self.add_apply_actions(dp, actions, match)
427 def test_action_dec_mpls_ttl(self, dp):
428 self._verify = [dp.ofproto.OFPAT_DEC_MPLS_TTL]
429 actions = [dp.ofproto_parser.OFPActionDecMplsTtl(), ]
430 match = dp.ofproto_parser.OFPMatch()
431 match.set_dl_type(ether.ETH_TYPE_MPLS)
432 self.add_apply_actions(dp, actions, match)
434 def test_action_set_nw_ttl_ipv4(self, dp):
436 self._verify = [dp.ofproto.OFPAT_SET_NW_TTL,
438 actions = [dp.ofproto_parser.OFPActionSetNwTtl(nw_ttl), ]
439 match = dp.ofproto_parser.OFPMatch()
440 match.set_dl_type(0x0800)
441 self.add_apply_actions(dp, actions, match)
443 def test_action_set_nw_ttl_ipv6(self, dp):
445 self._verify = [dp.ofproto.OFPAT_SET_NW_TTL,
447 actions = [dp.ofproto_parser.OFPActionSetNwTtl(nw_ttl), ]
448 match = dp.ofproto_parser.OFPMatch()
449 match.set_dl_type(0x86dd)
450 self.add_apply_actions(dp, actions, match)
452 def test_action_dec_nw_ttl_ipv4(self, dp):
453 self._verify = [dp.ofproto.OFPAT_DEC_NW_TTL]
454 actions = [dp.ofproto_parser.OFPActionDecNwTtl(), ]
455 match = dp.ofproto_parser.OFPMatch()
456 match.set_dl_type(0x0800)
457 self.add_apply_actions(dp, actions, match)
459 def test_action_dec_nw_ttl_ipv6(self, dp):
460 self._verify = [dp.ofproto.OFPAT_DEC_NW_TTL]
461 actions = [dp.ofproto_parser.OFPActionDecNwTtl(), ]
462 match = dp.ofproto_parser.OFPMatch()
463 match.set_dl_type(0x86dd)
464 self.add_apply_actions(dp, actions, match)
466 def test_action_copy_ttl_out(self, dp):
467 self._verify = [dp.ofproto.OFPAT_COPY_TTL_OUT]
468 actions = [dp.ofproto_parser.OFPActionCopyTtlOut(), ]
469 self.add_apply_actions(dp, actions)
471 def test_action_copy_ttl_in(self, dp):
472 self._verify = [dp.ofproto.OFPAT_COPY_TTL_IN]
473 actions = [dp.ofproto_parser.OFPActionCopyTtlIn(), ]
474 self.add_apply_actions(dp, actions)
476 def is_supported(self, t):
477 # Open vSwitch 1.10 does not support MPLS yet.
479 'test_action_set_field_ip_proto',
480 'test_action_set_field_dl_type',
481 'test_action_set_field_icmp',
482 'test_action_set_field_icmpv6_code',
483 'test_action_set_field_icmpv6_type',
484 'test_action_set_field_ipv6_flabel',
485 'test_action_set_field_ipv6_nd_sll',
486 'test_action_set_field_ipv6_nd_target',
487 'test_action_set_field_ipv6_nd_tll',
488 'test_action_copy_ttl_in',
489 'test_action_copy_ttl_out'
491 for u in unsupported: