blob: 4d325f64ca719f39d55310d231c1966a199a88d7 [file] [log] [blame]
Ed Swierk76cce912012-07-11 18:59:34 -07001"""
2"""
3import struct
4
5import logging
6
7import oftest.controller as controller
8import oftest.cstruct as ofp
9import oftest.message as message
10import basic
11
12from testutils import *
13
14#@var port_map Local copy of the configuration map from OF port
15# numbers to OS interfaces
16im_port_map = None
17#@var im_logger Local logger object
18im_logger = None
19#@var im_config Local copy of global configuration data
20im_config = None
21
22# For test priority
23#@var test_prio Set test priority for local tests
24test_prio = {}
25
26def test_set_init(config):
27 basic.test_set_init(config)
28
29 global im_port_map
30 global im_logger
31 global im_config
32
33 im_logger = logging.getLogger("ipmask")
34 im_logger.info("Initializing test set")
35 im_port_map = config["port_map"]
36 im_config = config
37
38def normal_ip_mask(index):
39 """
40 Return the IP mask for the given wildcard index 0 - 63 per the OF 1.0 spec
41 """
42 if index < 32:
43 return ((1 << 32) - 1) ^ ((1 << index) - 1)
44 else:
45 return 0
46
47def fancy_ip_mask(index):
48 """
49 Return the IP mask for the given wildcard index 0 - 31 per the OF 1.0 spec.
50 For wildcard index 32 - 63, return a "negative" IP mask:
51 32 : wildcard the first bit, mask 127.255.255.255
52 33 : wildcard all first 2 bits, mask 63.255.255.255
53 ...
54 62 : wildcard all but last bit, mask 0.0.0.1
55 63 : wildcard all bits, mask 0.0.0.0
56 """
57 if index < 32:
58 return ((1 << 32) - 1) ^ ((1 << index) - 1)
59 else:
60 return (1 << (63 - index)) - 1
61
62class BSNConfigIPMask(basic.SimpleDataPlane):
63 """
64 Exercise BSN vendor extension for configuring IP source/dest match mask
65 """
66 def bsn_set_ip_mask(self, index, mask):
67 """
68 Use the BSN_SET_IP_MASK vendor command to change the IP mask for the
69 given wildcard index
70 """
71 m = message.vendor()
72 m.vendor = 0x005c16c7
73 m.data = struct.pack("!LBBBBL", 0, index, 0, 0, 0, mask)
74 rc = self.controller.message_send(m)
75 self.assertNotEqual(rc, -1, "Error sending set IP mask command")
76
77 def bsn_get_ip_mask(self, index):
78 """
79 Use the BSN_GET_IP_MASK vendor command to get the current IP mask for
80 the given wildcard index
81 """
82 m = message.vendor()
83 m.vendor = 0x005c16c7
84 m.data = struct.pack( "!LBBBBL", 1, index, 0, 0, 0, 0 )
85 rc = self.controller.message_send(m)
86 self.assertNotEqual(rc, -1, "Error sending get IP mask command")
87 m, r = self.controller.poll(ofp.OFPT_VENDOR, 2)
88 self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
89 x = struct.unpack("!LBBBBL", m.data)
90 self.assertEqual(x[0], 1, "Wrong subtype")
91 self.assertEqual(x[1], index, "Wrong index")
92 return x[5]
93
94 def runTest(self):
Ed Swierk99a74de2012-08-22 06:40:54 -070095 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_DST_ALL,
96 "IP dst must be wildcarded")
97 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_SRC_ALL,
98 "IP src must be wildcarded")
Ed Swierk76cce912012-07-11 18:59:34 -070099 for index in range(0, 64):
100 mask = self.bsn_get_ip_mask(index)
101 im_logger.info("Index %d mask is %s" %
102 (index, scapy.utils.ltoa(mask)))
103 self.assertEqual(mask, normal_ip_mask(index), "Unexpected IP mask")
104
105 for index in range(0, 64):
106 mask = normal_ip_mask(index)
107 if mask == 0:
108 im_logger.info("Skipping IP wildcard index %d" % index)
109 else:
110 im_logger.info("Testing IP wildcard index %d" % index)
111 self.check_ip_mask(True, index, mask)
112 self.check_ip_mask(False, index, mask)
113
114 im_logger.info("Setting fancy IP masks")
115 for index in range(0, 64):
116 self.bsn_set_ip_mask(index, fancy_ip_mask(index))
117 for index in range(0, 64):
118 mask = self.bsn_get_ip_mask(index)
119 im_logger.info("Index %d mask is %s" %
120 (index, scapy.utils.ltoa(mask)))
121 self.assertEqual(mask, fancy_ip_mask(index), "Unexpected IP mask")
122
123 for index in range(0, 64):
124 mask = fancy_ip_mask(index)
125 if mask == 0:
126 im_logger.info("Skipping IP wildcard index %d" % index)
127 else:
128 im_logger.info("Testing IP wildcard index %d" % index)
129 self.check_ip_mask(True, index, mask)
130 self.check_ip_mask(False, index, mask)
131
132 def check_ip_mask(self, source, index, mask):
133 ports = im_port_map.keys()
134
135 # For each mask we install two flow entries, one which matches
136 # on IP source or dest addr all-0s (modulo the mask) and
137 # outputs to port 1, the other which matches on all-1s (modulo
138 # the mask) and outputs to port 2.
139
140 # Then we construct four packets: The first two are the same
141 # as the two flow entry matches (all 0s and all 1s), and we
142 # check that the packets go to ports 1 and 2, respectively.
143 # For the second set of packets, we flip the un-masked bits
144 # and check that only the masked bits are matched.
145
146 ip0 = scapy.utils.ltoa(0x00000000)
147 ip1 = scapy.utils.ltoa(0xffffffff)
148 ip2 = scapy.utils.ltoa(0xffffffff ^ mask)
149 ip3 = scapy.utils.ltoa(0x00000000 ^ mask)
150
151 if source:
152 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_SRC_MASK)
153 | (index << ofp.OFPFW_NW_SRC_SHIFT))
154 pkt0 = simple_tcp_packet(ip_src=ip0)
155 pkt1 = simple_tcp_packet(ip_src=ip1)
156 pkt2 = simple_tcp_packet(ip_src=ip2)
157 pkt3 = simple_tcp_packet(ip_src=ip3)
158 msg = lambda ip: im_logger.info("Testing source IP %s" % ip)
159 else:
160 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_DST_MASK)
161 | (index << ofp.OFPFW_NW_DST_SHIFT))
162 pkt0 = simple_tcp_packet(ip_dst=ip0)
163 pkt1 = simple_tcp_packet(ip_dst=ip1)
164 pkt2 = simple_tcp_packet(ip_dst=ip2)
165 pkt3 = simple_tcp_packet(ip_dst=ip3)
166 msg = lambda ip: im_logger.info("Testing dest IP %s" % ip)
167
168 rc = delete_all_flows(self.controller, im_logger)
169 self.assertEqual(rc, 0, "Failed to delete all flows")
170
171 rc = self.controller.message_send(flow_msg_create(
172 self, pkt0, ing_port=ports[0], egr_ports=[ports[1]],
173 wildcards=wildcards))
174 self.assertNotEqual(rc, -1, "Error inserting flow entry 0")
175 rc = self.controller.message_send(flow_msg_create(
176 self, pkt1, ing_port=ports[0], egr_ports=[ports[2]],
177 wildcards=wildcards))
178 self.assertNotEqual(rc, -1, "Error inserting flow entry 1")
179
180 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
181
182 msg(ip0)
183 self.dataplane.send(ports[0], str(pkt0))
184 receive_pkt_verify(self, [ports[1]], pkt0, ports[0])
185
186 msg(ip1)
187 self.dataplane.send(ports[0], str(pkt1))
188 receive_pkt_verify(self, [ports[2]], pkt1, ports[0])
189
190 msg(ip2)
191 self.dataplane.send(ports[0], str(pkt2))
192 receive_pkt_verify(self, [ports[1]], pkt2, ports[0])
193
194 msg(ip3)
195 self.dataplane.send(ports[0], str(pkt3))
196 receive_pkt_verify(self, [ports[2]], pkt3, ports[0])
197
198# Don't run by default
199test_prio["BSNConfigIPMask"] = -1