blob: d213bebd4e3288f3a1f126cbdb0c34b75c0ff583 [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):
27 ports = sorted(config["port_map"].keys())
28 in_port = ports[0]
29 out_port = ports[1]
30
31 actions = [ofp.action.output(out_port)]
32
33 pkt = simple_tcp_packet()
34
35 logging.info("Running actions test for %s", pp(actions))
36
37 delete_all_flows(self.controller)
38
39 logging.info("Inserting flow")
40 request = ofp.message.flow_add(
41 table_id=0,
42 match=packet_to_flow_match(self, pkt),
43 instructions=[
44 ofp.instruction.apply_actions(actions)],
45 buffer_id=ofp.OFP_NO_BUFFER,
46 priority=1000)
47 self.controller.message_send(request)
48
49 do_barrier(self.controller)
50
51 pktstr = str(pkt)
52
53 logging.info("Sending packet, expecting output to port %d", out_port)
54 self.dataplane.send(in_port, pktstr)
55 receive_pkt_check(self.dataplane, pktstr, [out_port], set(ports) - set([out_port]), self)
56
57class OutputMultiple(base_tests.SimpleDataPlane):
58 """
59 Output to three ports
60 """
61 def runTest(self):
62 ports = sorted(config["port_map"].keys())
63 in_port = ports[0]
64 out_ports = ports[1:4]
65
66 actions = [ofp.action.output(x) for x in out_ports]
67
68 pkt = simple_tcp_packet()
69
70 logging.info("Running actions test for %s", pp(actions))
71
72 delete_all_flows(self.controller)
73
74 logging.info("Inserting flow")
75 request = ofp.message.flow_add(
76 table_id=0,
77 match=packet_to_flow_match(self, pkt),
78 instructions=[
79 ofp.instruction.apply_actions(actions)],
80 buffer_id=ofp.OFP_NO_BUFFER,
81 priority=1000)
82 self.controller.message_send(request)
83
84 do_barrier(self.controller)
85
86 pktstr = str(pkt)
87
88 logging.info("Sending packet, expecting output to ports %r", out_ports)
89 self.dataplane.send(in_port, pktstr)
90 receive_pkt_check(self.dataplane, pktstr, out_ports, set(ports) - set(out_ports), self)
91
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):
98 ports = sorted(config["port_map"].keys())
99 in_port = ports[0]
100 out_port = ports[1]
101
102 actions = actions + [ofp.action.output(out_port)]
103
104 logging.info("Running actions test for %s", pp(actions))
105
106 delete_all_flows(self.controller)
107
108 logging.info("Inserting flow")
109 request = ofp.message.flow_add(
110 table_id=0,
111 match=packet_to_flow_match(self, pkt),
112 instructions=[
113 ofp.instruction.apply_actions(actions)],
114 buffer_id=ofp.OFP_NO_BUFFER,
115 priority=1000)
116 self.controller.message_send(request)
117
118 do_barrier(self.controller)
119
120 logging.info("Sending packet, expecting output to port %d", out_port)
121 self.dataplane.send(in_port, str(pkt))
122 receive_pkt_check(self.dataplane, str(exp_pkt), [out_port], set(ports) - set([out_port]), self)
123
124class PushVlan(BaseModifyPacketTest):
125 """
126 Push a vlan tag (vid=0, pcp=0)
127 """
128 def runTest(self):
129 actions = [ofp.action.push_vlan(ethertype=0x8100)]
130 pkt = simple_tcp_packet()
131 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
132 self.verify_modify(actions, pkt, exp_pkt)
133
134class PopVlan(BaseModifyPacketTest):
135 """
136 Pop a vlan tag
137 """
138 def runTest(self):
139 actions = [ofp.action.pop_vlan()]
140 pkt = simple_tcp_packet(dl_vlan_enable=True, pktlen=104)
141 exp_pkt = simple_tcp_packet()
142 self.verify_modify(actions, pkt, exp_pkt)
143
144class SetVlanVid(BaseModifyPacketTest):
145 """
146 Set the vlan vid
147 """
148 def runTest(self):
149 actions = [ofp.action.set_field(ofp.oxm.vlan_vid(2))]
150 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=1)
151 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_vid=2)
152 self.verify_modify(actions, pkt, exp_pkt)
153
154class SetVlanPcp(BaseModifyPacketTest):
155 """
156 Set the vlan priority
157 """
158 def runTest(self):
159 actions = [ofp.action.set_field(ofp.oxm.vlan_pcp(2))]
160 pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=1)
161 exp_pkt = simple_tcp_packet(dl_vlan_enable=True, vlan_pcp=2)
162 self.verify_modify(actions, pkt, exp_pkt)