Rich Lane | b626a9c | 2013-08-05 16:45:50 -0700 | [diff] [blame] | 1 | # 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 | """ |
| 5 | Action test cases |
| 6 | |
| 7 | These tests check the behavior of each type of action. The matches used are |
| 8 | exact-match, to satisfy the OXM prerequisites of the set-field actions. |
| 9 | These tests use a single apply-actions instruction. |
| 10 | """ |
| 11 | |
| 12 | import logging |
| 13 | |
| 14 | from oftest import config |
| 15 | import oftest.base_tests as base_tests |
| 16 | import ofp |
| 17 | from loxi.pp import pp |
| 18 | |
| 19 | from oftest.testutils import * |
| 20 | from oftest.parse import parse_ipv6 |
| 21 | |
| 22 | class Output(base_tests.SimpleDataPlane): |
| 23 | """ |
| 24 | Output to a single port |
| 25 | """ |
| 26 | def runTest(self): |
Rich Lane | 045db07 | 2013-08-06 13:16:30 -0700 | [diff] [blame] | 27 | in_port, out_port = openflow_ports(2) |
Rich Lane | b626a9c | 2013-08-05 16:45:50 -0700 | [diff] [blame] | 28 | |
| 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 Lane | 045db07 | 2013-08-06 13:16:30 -0700 | [diff] [blame] | 53 | receive_pkt_check(self.dataplane, pktstr, [out_port], |
| 54 | set(openflow_ports()) - set([out_port]), self) |
Rich Lane | b626a9c | 2013-08-05 16:45:50 -0700 | [diff] [blame] | 55 | |
| 56 | class OutputMultiple(base_tests.SimpleDataPlane): |
| 57 | """ |
| 58 | Output to three ports |
| 59 | """ |
| 60 | def runTest(self): |
Rich Lane | 045db07 | 2013-08-06 13:16:30 -0700 | [diff] [blame] | 61 | ports = openflow_ports(4) |
Rich Lane | b626a9c | 2013-08-05 16:45:50 -0700 | [diff] [blame] | 62 | 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 Lane | 045db07 | 2013-08-06 13:16:30 -0700 | [diff] [blame] | 89 | receive_pkt_check(self.dataplane, pktstr, out_ports, |
| 90 | set(openflow_ports()) - set(out_ports), self) |
Rich Lane | b626a9c | 2013-08-05 16:45:50 -0700 | [diff] [blame] | 91 | |
| 92 | class 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 Lane | 045db07 | 2013-08-06 13:16:30 -0700 | [diff] [blame] | 98 | in_port, out_port = openflow_ports(2) |
Rich Lane | b626a9c | 2013-08-05 16:45:50 -0700 | [diff] [blame] | 99 | |
| 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 Lane | 045db07 | 2013-08-06 13:16:30 -0700 | [diff] [blame] | 120 | receive_pkt_check(self.dataplane, str(exp_pkt), [out_port], |
| 121 | set(openflow_ports()) - set([out_port]), self) |
Rich Lane | b626a9c | 2013-08-05 16:45:50 -0700 | [diff] [blame] | 122 | |
| 123 | class 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 | |
| 133 | class 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 | |
| 143 | class 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 | |
| 153 | class 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) |