blob: 61896185bb98e56e2ed182e86a3b3136ad0c3569 [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
Ed Swierk76cce912012-07-11 18:59:34 -070017"""
18"""
19import struct
Ed Swierk76cce912012-07-11 18:59:34 -070020import logging
Rich Lane3c7cf7f2013-01-11 18:04:56 -080021import scapy
Ed Swierk76cce912012-07-11 18:59:34 -070022
Rich Lane477f4812012-10-04 22:49:00 -070023from oftest import config
Ed Swierk76cce912012-07-11 18:59:34 -070024import oftest.controller as controller
Rich Laned7b0ffa2013-03-08 15:53:42 -080025import ofp
Rich Laneb90a1c42012-10-05 09:16:05 -070026import oftest.base_tests as base_tests
Ed Swierk76cce912012-07-11 18:59:34 -070027
Rich Laneda3b5ad2012-10-03 09:05:32 -070028from oftest.testutils import *
Ed Swierk76cce912012-07-11 18:59:34 -070029
Ed Swierk76cce912012-07-11 18:59:34 -070030def normal_ip_mask(index):
31 """
32 Return the IP mask for the given wildcard index 0 - 63 per the OF 1.0 spec
33 """
34 if index < 32:
35 return ((1 << 32) - 1) ^ ((1 << index) - 1)
36 else:
37 return 0
38
39def fancy_ip_mask(index):
40 """
41 Return the IP mask for the given wildcard index 0 - 31 per the OF 1.0 spec.
42 For wildcard index 32 - 63, return a "negative" IP mask:
43 32 : wildcard the first bit, mask 127.255.255.255
44 33 : wildcard all first 2 bits, mask 63.255.255.255
45 ...
46 62 : wildcard all but last bit, mask 0.0.0.1
47 63 : wildcard all bits, mask 0.0.0.0
48 """
49 if index < 32:
50 return ((1 << 32) - 1) ^ ((1 << index) - 1)
51 else:
52 return (1 << (63 - index)) - 1
53
Rich Lane0a4f6372013-01-02 14:40:22 -080054@nonstandard
Rich Laneb90a1c42012-10-05 09:16:05 -070055class BSNConfigIPMask(base_tests.SimpleDataPlane):
Ed Swierk76cce912012-07-11 18:59:34 -070056 """
57 Exercise BSN vendor extension for configuring IP source/dest match mask
58 """
Rich Laned1d9c282012-10-04 22:07:10 -070059
Ed Swierk76cce912012-07-11 18:59:34 -070060 def bsn_set_ip_mask(self, index, mask):
61 """
62 Use the BSN_SET_IP_MASK vendor command to change the IP mask for the
63 given wildcard index
64 """
Rich Lane9a003812012-10-04 17:17:59 -070065 logging.info("Setting index %d to mask is %s" % (index, mask))
Rich Lane4b601452013-03-11 23:37:06 -070066 m = ofp.message.bsn_set_ip_mask(index=index, mask=mask)
Rich Lane5c3151c2013-01-03 17:15:41 -080067 self.controller.message_send(m)
Ed Swierk76cce912012-07-11 18:59:34 -070068
69 def bsn_get_ip_mask(self, index):
70 """
Ed Swierkaba42582012-09-14 06:40:49 -070071 Use the BSN_GET_IP_MASK_REQUEST vendor command to get the current IP mask
72 for the given wildcard index
Ed Swierk76cce912012-07-11 18:59:34 -070073 """
Rich Lane4b601452013-03-11 23:37:06 -070074 request = ofp.message.bsn_get_ip_mask_request(index=index)
75 reply, _ = self.controller.transact(request)
76 self.assertTrue(isinstance(reply, ofp.message.bsn_get_ip_mask_reply), "Wrong reply type")
77 self.assertEqual(reply.index, index, "Wrong index")
78 return reply.mask
Ed Swierk76cce912012-07-11 18:59:34 -070079
80 def runTest(self):
Ed Swierk99a74de2012-08-22 06:40:54 -070081 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_DST_ALL,
82 "IP dst must be wildcarded")
83 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_SRC_ALL,
84 "IP src must be wildcarded")
Ed Swierk76cce912012-07-11 18:59:34 -070085 for index in range(0, 64):
86 mask = self.bsn_get_ip_mask(index)
Rich Lane9a003812012-10-04 17:17:59 -070087 logging.info("Index %d mask is %s" %
Ed Swierk76cce912012-07-11 18:59:34 -070088 (index, scapy.utils.ltoa(mask)))
89 self.assertEqual(mask, normal_ip_mask(index), "Unexpected IP mask")
90
91 for index in range(0, 64):
92 mask = normal_ip_mask(index)
93 if mask == 0:
Rich Lane9a003812012-10-04 17:17:59 -070094 logging.info("Skipping IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -070095 else:
Rich Lane9a003812012-10-04 17:17:59 -070096 logging.info("Testing IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -070097 self.check_ip_mask(True, index, mask)
98 self.check_ip_mask(False, index, mask)
99
Rich Lane9a003812012-10-04 17:17:59 -0700100 logging.info("Setting fancy IP masks")
Ed Swierk76cce912012-07-11 18:59:34 -0700101 for index in range(0, 64):
102 self.bsn_set_ip_mask(index, fancy_ip_mask(index))
103 for index in range(0, 64):
104 mask = self.bsn_get_ip_mask(index)
Rich Lane9a003812012-10-04 17:17:59 -0700105 logging.info("Index %d mask is %s" %
Ed Swierk76cce912012-07-11 18:59:34 -0700106 (index, scapy.utils.ltoa(mask)))
107 self.assertEqual(mask, fancy_ip_mask(index), "Unexpected IP mask")
108
109 for index in range(0, 64):
110 mask = fancy_ip_mask(index)
111 if mask == 0:
Rich Lane9a003812012-10-04 17:17:59 -0700112 logging.info("Skipping IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -0700113 else:
Rich Lane9a003812012-10-04 17:17:59 -0700114 logging.info("Testing IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -0700115 self.check_ip_mask(True, index, mask)
116 self.check_ip_mask(False, index, mask)
117
118 def check_ip_mask(self, source, index, mask):
Rich Lane477f4812012-10-04 22:49:00 -0700119 ports = config["port_map"].keys()
Ed Swierk76cce912012-07-11 18:59:34 -0700120
121 # For each mask we install two flow entries, one which matches
122 # on IP source or dest addr all-0s (modulo the mask) and
123 # outputs to port 1, the other which matches on all-1s (modulo
124 # the mask) and outputs to port 2.
125
126 # Then we construct four packets: The first two are the same
127 # as the two flow entry matches (all 0s and all 1s), and we
128 # check that the packets go to ports 1 and 2, respectively.
129 # For the second set of packets, we flip the un-masked bits
130 # and check that only the masked bits are matched.
131
132 ip0 = scapy.utils.ltoa(0x00000000)
133 ip1 = scapy.utils.ltoa(0xffffffff)
134 ip2 = scapy.utils.ltoa(0xffffffff ^ mask)
135 ip3 = scapy.utils.ltoa(0x00000000 ^ mask)
136
137 if source:
138 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_SRC_MASK)
139 | (index << ofp.OFPFW_NW_SRC_SHIFT))
140 pkt0 = simple_tcp_packet(ip_src=ip0)
141 pkt1 = simple_tcp_packet(ip_src=ip1)
142 pkt2 = simple_tcp_packet(ip_src=ip2)
143 pkt3 = simple_tcp_packet(ip_src=ip3)
Rich Lane9a003812012-10-04 17:17:59 -0700144 msg = lambda ip: logging.info("Testing source IP %s" % ip)
Ed Swierk76cce912012-07-11 18:59:34 -0700145 else:
146 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_DST_MASK)
147 | (index << ofp.OFPFW_NW_DST_SHIFT))
148 pkt0 = simple_tcp_packet(ip_dst=ip0)
149 pkt1 = simple_tcp_packet(ip_dst=ip1)
150 pkt2 = simple_tcp_packet(ip_dst=ip2)
151 pkt3 = simple_tcp_packet(ip_dst=ip3)
Rich Lane9a003812012-10-04 17:17:59 -0700152 msg = lambda ip: logging.info("Testing dest IP %s" % ip)
Ed Swierk76cce912012-07-11 18:59:34 -0700153
Rich Lane32bf9482013-01-03 17:26:30 -0800154 delete_all_flows(self.controller)
Ed Swierk76cce912012-07-11 18:59:34 -0700155
Rich Lane5c3151c2013-01-03 17:15:41 -0800156 self.controller.message_send(flow_msg_create(
Ed Swierk76cce912012-07-11 18:59:34 -0700157 self, pkt0, ing_port=ports[0], egr_ports=[ports[1]],
158 wildcards=wildcards))
Rich Lane5c3151c2013-01-03 17:15:41 -0800159 self.controller.message_send(flow_msg_create(
Ed Swierk76cce912012-07-11 18:59:34 -0700160 self, pkt1, ing_port=ports[0], egr_ports=[ports[2]],
161 wildcards=wildcards))
Ed Swierk76cce912012-07-11 18:59:34 -0700162
Rich Lane3a261d52013-01-03 17:45:08 -0800163 do_barrier(self.controller)
Ed Swierk76cce912012-07-11 18:59:34 -0700164
165 msg(ip0)
166 self.dataplane.send(ports[0], str(pkt0))
Rich Lanee4b384d2013-09-13 14:33:40 -0700167 verify_packets(self, str(pkt0), [ports[1]])
Ed Swierk76cce912012-07-11 18:59:34 -0700168
169 msg(ip1)
170 self.dataplane.send(ports[0], str(pkt1))
Rich Lanee4b384d2013-09-13 14:33:40 -0700171 verify_packets(self, str(pkt1), [ports[2]])
Ed Swierk76cce912012-07-11 18:59:34 -0700172
173 msg(ip2)
174 self.dataplane.send(ports[0], str(pkt2))
Rich Lanee4b384d2013-09-13 14:33:40 -0700175 verify_packets(self, str(pkt2), [ports[1]])
Ed Swierk76cce912012-07-11 18:59:34 -0700176
177 msg(ip3)
178 self.dataplane.send(ports[0], str(pkt3))
Rich Lanee4b384d2013-09-13 14:33:40 -0700179 verify_packets(self, str(pkt3), [ports[2]])