blob: 1b5a6a6987889fc9850fa5572dba0d0932f4713b [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Rich Lane93820592013-10-24 11:20:23 -070017# Distributed under the OpenFlow Software License (see LICENSE)
18# Copyright (c) 2012, 2013 Big Switch Networks, Inc.
19"""
20Packet-in match test cases
21
22Checks the match sent in packet-in messages.
23"""
24
25import logging
26
27from oftest import config
28import oftest.base_tests as base_tests
29import ofp
30
31from oftest.testutils import *
32
33class PktinMatchTest(base_tests.SimpleDataPlane):
34 """
35 Base class for packet-in match tests
36 """
37
38 def setUp(self):
39 base_tests.SimpleDataPlane.setUp(self)
40 delete_all_flows(self.controller)
41
42 def verify_pktin_match(self, pkt, expected_oxm, optional=False):
43 """
44 Cause a packet-in and verify that the expected OXM is present
45 (unless optional) and equal
46 """
47
48 in_port, = openflow_ports(1)
49 pktstr = str(pkt)
50
51 logging.debug("Inserting match-all flow sending packets to controller")
52 request = ofp.message.flow_add(
Wilson Ng6f539642013-10-28 18:17:44 -070053 table_id=test_param_get("table", 0),
Rich Lane93820592013-10-24 11:20:23 -070054 instructions=[
55 ofp.instruction.apply_actions(
56 actions=[
57 ofp.action.output(
58 port=ofp.OFPP_CONTROLLER,
59 max_len=ofp.OFPCML_NO_BUFFER)])],
60 buffer_id=ofp.OFP_NO_BUFFER,
61 priority=0)
62 self.controller.message_send(request)
63 do_barrier(self.controller)
64
65 logging.debug("Sending packet")
66 self.dataplane.send(in_port, pktstr)
67
68 logging.debug("Expecting packet-in")
69 msg = verify_packet_in(self, pktstr, in_port, ofp.OFPR_NO_MATCH)
70 oxms = { type(oxm): oxm for oxm in msg.match.oxm_list }
71 oxm = oxms.get(type(expected_oxm))
72 if oxm:
73 self.assertEquals(oxm, expected_oxm, "Received %s != expected %s" % (oxm.show(), expected_oxm.show()))
74 elif optional:
75 logging.info("Optional OXM not received")
76 else:
77 raise AssertionError("Required OXM not received")
78
79class VlanAbsent(PktinMatchTest):
80 """
81 Absent VLAN tag
82 """
83 def runTest(self):
84 self.verify_pktin_match(
85 simple_tcp_packet(),
86 ofp.oxm.vlan_vid(0),
87 optional=True)
88
89class VlanVid(PktinMatchTest):
90 """
91 VLAN tag
92 """
93 def runTest(self):
94 self.verify_pktin_match(
95 simple_tcp_packet(dl_vlan_enable=True, vlan_vid=1),
96 ofp.oxm.vlan_vid(ofp.OFPVID_PRESENT|1),
97 optional=True)