blob: 39369d043518342e4f3eca09e1e4a7358d664768 [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)