backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / integrated / test_add_flow_v12_actions.py
1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # vim: tabstop=4 shiftwidth=4 softtabstop=4
17
18 import logging
19
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
24
25 LOG = logging.getLogger(__name__)
26
27
28 class RunTest(tester.TestFlowBase):
29     """ Test case for add flows of Actions
30     """
31     OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION]
32
33     def __init__(self, *args, **kwargs):
34         super(RunTest, self).__init__(*args, **kwargs)
35
36         self._verify = []
37
38     def add_apply_actions(self, dp, actions, match=None):
39         inst = [dp.ofproto_parser.OFPInstructionActions(
40                 dp.ofproto.OFPIT_APPLY_ACTIONS, actions)]
41         if match is None:
42             match = dp.ofproto_parser.OFPMatch()
43         m = dp.ofproto_parser.OFPFlowMod(dp, 0, 0, 0,
44                                          dp.ofproto.OFPFC_ADD,
45                                          0, 0, 0xff, 0xffffffff,
46                                          dp.ofproto.OFPP_ANY,
47                                          dp.ofproto.OFPG_ANY,
48                                          0, match, inst)
49         dp.send_msg(m)
50
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)
57
58     def verify_default(self, dp, stats):
59         verify = self._verify
60         self._verify = []
61
62         type_ = name = field = value = None
63         if len(verify) == 1:
64             (type_, ) = verify
65         elif len(verify) == 3:
66             (type_, name, value) = verify
67         elif len(verify) == 4:
68             (type_, name, field, value) = verify
69         else:
70             return "self._verify is invalid."
71
72         try:
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)
77         except IndexError:
78             return "Action is not setting."
79
80         s_val = None
81         if name:
82             try:
83                 s_val = getattr(action, name)
84             except AttributeError:
85                 pass
86
87         if name == 'field':
88             if s_val.header != field:
89                 return "Field error. send:%s val:%s" \
90                     % (field, s_val.header)
91             s_val = s_val.value
92
93         if name and s_val != value:
94             return "Value error. send:%s=%s val:%s" \
95                 % (name, value, s_val)
96
97         return True
98
99     def verify_action_drop(self, dp, stats):
100         for s in stats:
101             for i in s.instructions:
102                 if len(i.actions):
103                     return "has actions. %s" % (i.actions)
104         return True
105
106     # Test of General Actions
107     def test_action_output(self, dp):
108         out_port = 255
109         self._verify = [dp.ofproto.OFPAT_OUTPUT,
110                         'port', out_port]
111
112         actions = [dp.ofproto_parser.OFPActionOutput(out_port, 0), ]
113         self.add_apply_actions(dp, actions)
114
115     def test_action_drop(self, dp):
116         self.add_apply_actions(dp, [])
117
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]
123
124         actions = [dp.ofproto_parser.OFPActionPushVlan(ethertype)]
125         self.add_apply_actions(dp, actions)
126
127     def test_action_pop_vlan(self, dp):
128         self._verify = [dp.ofproto.OFPAT_POP_VLAN, ]
129
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)
134
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]
139
140         actions = [dp.ofproto_parser.OFPActionPushMpls(ethertype), ]
141         self.add_apply_actions(dp, actions)
142
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)
151
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)
157
158         self.add_set_field_action(dp, field, value)
159
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)
164
165         self.add_set_field_action(dp, field, value)
166
167     def test_action_set_field_dl_type(self, dp):
168         field = dp.ofproto.OXM_OF_ETH_TYPE
169         value = ether.ETH_TYPE_IPV6
170
171         self.add_set_field_action(dp, field, value)
172
173     def test_action_set_field_vlan_vid(self, dp):
174         field = dp.ofproto.OXM_OF_VLAN_VID
175         value = 0x1e4
176
177         match = dp.ofproto_parser.OFPMatch()
178         match.set_vlan_vid(1)
179
180         self.add_set_field_action(dp, field, value, match)
181
182     def test_action_set_field_vlan_pcp(self, dp):
183         field = dp.ofproto.OXM_OF_VLAN_PCP
184         value = 3
185
186         match = dp.ofproto_parser.OFPMatch()
187         match.set_vlan_vid(1)
188
189         self.add_set_field_action(dp, field, value, match)
190
191     def test_action_set_field_nw_dscp(self, dp):
192         field = dp.ofproto.OXM_OF_IP_DSCP
193         value = 32
194
195         match = dp.ofproto_parser.OFPMatch()
196         match.set_dl_type(ether.ETH_TYPE_IP)
197
198         self.add_set_field_action(dp, field, value, match)
199
200     def test_action_set_field_nw_ecn(self, dp):
201         field = dp.ofproto.OXM_OF_IP_ECN
202         value = 1
203
204         match = dp.ofproto_parser.OFPMatch()
205         match.set_dl_type(ether.ETH_TYPE_IP)
206
207         self.add_set_field_action(dp, field, value, match)
208
209     def test_action_set_field_ip_proto(self, dp):
210         field = dp.ofproto.OXM_OF_IP_PROTO
211         value = inet.IPPROTO_TCP
212
213         match = dp.ofproto_parser.OFPMatch()
214         match.set_dl_type(ether.ETH_TYPE_IP)
215
216         self.add_set_field_action(dp, field, value, match)
217
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)
222
223         match = dp.ofproto_parser.OFPMatch()
224         match.set_dl_type(ether.ETH_TYPE_IP)
225
226         self.add_set_field_action(dp, field, value, match)
227
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)
232
233         match = dp.ofproto_parser.OFPMatch()
234         match.set_dl_type(ether.ETH_TYPE_IP)
235
236         self.add_set_field_action(dp, field, value, match)
237
238     def test_action_set_field_tcp_src(self, dp):
239         field = dp.ofproto.OXM_OF_TCP_SRC
240         value = 105
241
242         match = dp.ofproto_parser.OFPMatch()
243         match.set_dl_type(ether.ETH_TYPE_IP)
244         match.set_ip_proto(inet.IPPROTO_TCP)
245
246         self.add_set_field_action(dp, field, value, match)
247
248     def test_action_set_field_tcp_dst(self, dp):
249         field = dp.ofproto.OXM_OF_TCP_DST
250         value = 75
251
252         match = dp.ofproto_parser.OFPMatch()
253         match.set_dl_type(ether.ETH_TYPE_IP)
254         match.set_ip_proto(inet.IPPROTO_TCP)
255
256         self.add_set_field_action(dp, field, value, match)
257
258     def test_action_set_field_udp_src(self, dp):
259         field = dp.ofproto.OXM_OF_UDP_SRC
260         value = 197
261
262         match = dp.ofproto_parser.OFPMatch()
263         match.set_dl_type(ether.ETH_TYPE_IP)
264         match.set_ip_proto(inet.IPPROTO_UDP)
265
266         self.add_set_field_action(dp, field, value, match)
267
268     def test_action_set_field_udp_dst(self, dp):
269         field = dp.ofproto.OXM_OF_UDP_DST
270         value = 17
271
272         match = dp.ofproto_parser.OFPMatch()
273         match.set_dl_type(ether.ETH_TYPE_IP)
274         match.set_ip_proto(inet.IPPROTO_UDP)
275
276         self.add_set_field_action(dp, field, value, match)
277
278     def test_action_set_field_icmpv4_type(self, dp):
279         field = dp.ofproto.OXM_OF_ICMPV4_TYPE
280         value = 8
281
282         match = dp.ofproto_parser.OFPMatch()
283         match.set_ip_proto(inet.IPPROTO_ICMP)
284
285         self.add_set_field_action(dp, field, value, match)
286
287     def test_action_set_field_icmpv4_code(self, dp):
288         field = dp.ofproto.OXM_OF_ICMPV4_CODE
289         value = 2
290
291         match = dp.ofproto_parser.OFPMatch()
292         match.set_ip_proto(inet.IPPROTO_ICMP)
293
294         self.add_set_field_action(dp, field, value, match)
295
296     def test_action_set_field_arp_op(self, dp):
297         field = dp.ofproto.OXM_OF_ARP_OP
298         value = 2
299
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)
303
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)
308
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)
312
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)
317
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)
321
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)
326
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)
330
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)
335
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)
339
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)
344
345         match = dp.ofproto_parser.OFPMatch()
346         match.set_dl_type(0x86dd)
347
348         self.add_set_field_action(dp, field, value, match)
349
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)
354
355         match = dp.ofproto_parser.OFPMatch()
356         match.set_dl_type(0x86dd)
357
358         self.add_set_field_action(dp, field, value, match)
359
360     def test_action_set_field_ipv6_flabel(self, dp):
361         field = dp.ofproto.OXM_OF_IPV6_FLABEL
362         value = 0x2c12
363
364         self.add_set_field_action(dp, field, value)
365
366     def test_action_set_field_icmpv6_type(self, dp):
367         field = dp.ofproto.OXM_OF_ICMPV6_TYPE
368         value = 129
369
370         self.add_set_field_action(dp, field, value)
371
372     def test_action_set_field_icmpv6_code(self, dp):
373         field = dp.ofproto.OXM_OF_ICMPV6_CODE
374         value = 2
375
376         self.add_set_field_action(dp, field, value)
377
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)
382
383         self.add_set_field_action(dp, field, value)
384
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)
389
390         self.add_set_field_action(dp, field, value)
391
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)
396
397         self.add_set_field_action(dp, field, value)
398
399     def test_action_set_field_mpls_label(self, dp):
400         field = dp.ofproto.OXM_OF_MPLS_LABEL
401         value = 0x4c
402
403         match = dp.ofproto_parser.OFPMatch()
404         match.set_dl_type(ether.ETH_TYPE_MPLS)
405
406         self.add_set_field_action(dp, field, value, match)
407
408     def test_action_set_field_mpls_tc(self, dp):
409         field = dp.ofproto.OXM_OF_MPLS_TC
410         value = 0b101
411
412         match = dp.ofproto_parser.OFPMatch()
413         match.set_dl_type(ether.ETH_TYPE_MPLS)
414
415         self.add_set_field_action(dp, field, value, match)
416
417     # Test of Change-TTL Actions
418     def test_action_set_mpls_ttl(self, dp):
419         mpls_ttl = 8
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)
426
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)
433
434     def test_action_set_nw_ttl_ipv4(self, dp):
435         nw_ttl = 64
436         self._verify = [dp.ofproto.OFPAT_SET_NW_TTL,
437                         'nw_ttl', 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)
442
443     def test_action_set_nw_ttl_ipv6(self, dp):
444         nw_ttl = 64
445         self._verify = [dp.ofproto.OFPAT_SET_NW_TTL,
446                         'nw_ttl', 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)
451
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)
458
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)
465
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)
470
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)
475
476     def is_supported(self, t):
477         # Open vSwitch 1.10 does not support MPLS yet.
478         unsupported = [
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'
490         ]
491         for u in unsupported:
492             if t.find(u) != -1:
493                 return False
494
495         return True