blob: c3688ba79d22c1cfd30ab5d7aeef3eecb3618f8a [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
Rich Laneda3b5ad2012-10-03 09:05:32 -070012from oftest.testutils import *
Ed Swierk76cce912012-07-11 18:59:34 -070013
14#@var port_map Local copy of the configuration map from OF port
15# numbers to OS interfaces
16im_port_map = None
Ed Swierk76cce912012-07-11 18:59:34 -070017#@var im_config Local copy of global configuration data
18im_config = None
19
Ed Swierk76cce912012-07-11 18:59:34 -070020def test_set_init(config):
21 basic.test_set_init(config)
22
23 global im_port_map
Ed Swierk76cce912012-07-11 18:59:34 -070024 global im_config
25
Ed Swierk76cce912012-07-11 18:59:34 -070026 im_port_map = config["port_map"]
27 im_config = config
28
29def normal_ip_mask(index):
30 """
31 Return the IP mask for the given wildcard index 0 - 63 per the OF 1.0 spec
32 """
33 if index < 32:
34 return ((1 << 32) - 1) ^ ((1 << index) - 1)
35 else:
36 return 0
37
38def fancy_ip_mask(index):
39 """
40 Return the IP mask for the given wildcard index 0 - 31 per the OF 1.0 spec.
41 For wildcard index 32 - 63, return a "negative" IP mask:
42 32 : wildcard the first bit, mask 127.255.255.255
43 33 : wildcard all first 2 bits, mask 63.255.255.255
44 ...
45 62 : wildcard all but last bit, mask 0.0.0.1
46 63 : wildcard all bits, mask 0.0.0.0
47 """
48 if index < 32:
49 return ((1 << 32) - 1) ^ ((1 << index) - 1)
50 else:
51 return (1 << (63 - index)) - 1
52
53class BSNConfigIPMask(basic.SimpleDataPlane):
54 """
55 Exercise BSN vendor extension for configuring IP source/dest match mask
56 """
Rich Laned1d9c282012-10-04 22:07:10 -070057
58 priority = -1
59
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))
Ed Swierk76cce912012-07-11 18:59:34 -070066 m = message.vendor()
67 m.vendor = 0x005c16c7
68 m.data = struct.pack("!LBBBBL", 0, index, 0, 0, 0, mask)
69 rc = self.controller.message_send(m)
70 self.assertNotEqual(rc, -1, "Error sending set IP mask command")
71
72 def bsn_get_ip_mask(self, index):
73 """
Ed Swierkaba42582012-09-14 06:40:49 -070074 Use the BSN_GET_IP_MASK_REQUEST vendor command to get the current IP mask
75 for the given wildcard index
Ed Swierk76cce912012-07-11 18:59:34 -070076 """
77 m = message.vendor()
78 m.vendor = 0x005c16c7
79 m.data = struct.pack( "!LBBBBL", 1, index, 0, 0, 0, 0 )
80 rc = self.controller.message_send(m)
81 self.assertNotEqual(rc, -1, "Error sending get IP mask command")
Dan Talaycoc689a792012-09-28 14:22:53 -070082 m, r = self.controller.poll(ofp.OFPT_VENDOR)
Ed Swierk76cce912012-07-11 18:59:34 -070083 self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
84 x = struct.unpack("!LBBBBL", m.data)
Ed Swierkaba42582012-09-14 06:40:49 -070085 self.assertEqual(x[0], 2, "Wrong subtype")
Ed Swierk76cce912012-07-11 18:59:34 -070086 self.assertEqual(x[1], index, "Wrong index")
87 return x[5]
88
89 def runTest(self):
Ed Swierk99a74de2012-08-22 06:40:54 -070090 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_DST_ALL,
91 "IP dst must be wildcarded")
92 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_SRC_ALL,
93 "IP src must be wildcarded")
Ed Swierk76cce912012-07-11 18:59:34 -070094 for index in range(0, 64):
95 mask = self.bsn_get_ip_mask(index)
Rich Lane9a003812012-10-04 17:17:59 -070096 logging.info("Index %d mask is %s" %
Ed Swierk76cce912012-07-11 18:59:34 -070097 (index, scapy.utils.ltoa(mask)))
98 self.assertEqual(mask, normal_ip_mask(index), "Unexpected IP mask")
99
100 for index in range(0, 64):
101 mask = normal_ip_mask(index)
102 if mask == 0:
Rich Lane9a003812012-10-04 17:17:59 -0700103 logging.info("Skipping IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -0700104 else:
Rich Lane9a003812012-10-04 17:17:59 -0700105 logging.info("Testing IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -0700106 self.check_ip_mask(True, index, mask)
107 self.check_ip_mask(False, index, mask)
108
Rich Lane9a003812012-10-04 17:17:59 -0700109 logging.info("Setting fancy IP masks")
Ed Swierk76cce912012-07-11 18:59:34 -0700110 for index in range(0, 64):
111 self.bsn_set_ip_mask(index, fancy_ip_mask(index))
112 for index in range(0, 64):
113 mask = self.bsn_get_ip_mask(index)
Rich Lane9a003812012-10-04 17:17:59 -0700114 logging.info("Index %d mask is %s" %
Ed Swierk76cce912012-07-11 18:59:34 -0700115 (index, scapy.utils.ltoa(mask)))
116 self.assertEqual(mask, fancy_ip_mask(index), "Unexpected IP mask")
117
118 for index in range(0, 64):
119 mask = fancy_ip_mask(index)
120 if mask == 0:
Rich Lane9a003812012-10-04 17:17:59 -0700121 logging.info("Skipping IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -0700122 else:
Rich Lane9a003812012-10-04 17:17:59 -0700123 logging.info("Testing IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -0700124 self.check_ip_mask(True, index, mask)
125 self.check_ip_mask(False, index, mask)
126
127 def check_ip_mask(self, source, index, mask):
128 ports = im_port_map.keys()
129
130 # For each mask we install two flow entries, one which matches
131 # on IP source or dest addr all-0s (modulo the mask) and
132 # outputs to port 1, the other which matches on all-1s (modulo
133 # the mask) and outputs to port 2.
134
135 # Then we construct four packets: The first two are the same
136 # as the two flow entry matches (all 0s and all 1s), and we
137 # check that the packets go to ports 1 and 2, respectively.
138 # For the second set of packets, we flip the un-masked bits
139 # and check that only the masked bits are matched.
140
141 ip0 = scapy.utils.ltoa(0x00000000)
142 ip1 = scapy.utils.ltoa(0xffffffff)
143 ip2 = scapy.utils.ltoa(0xffffffff ^ mask)
144 ip3 = scapy.utils.ltoa(0x00000000 ^ mask)
145
146 if source:
147 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_SRC_MASK)
148 | (index << ofp.OFPFW_NW_SRC_SHIFT))
149 pkt0 = simple_tcp_packet(ip_src=ip0)
150 pkt1 = simple_tcp_packet(ip_src=ip1)
151 pkt2 = simple_tcp_packet(ip_src=ip2)
152 pkt3 = simple_tcp_packet(ip_src=ip3)
Rich Lane9a003812012-10-04 17:17:59 -0700153 msg = lambda ip: logging.info("Testing source IP %s" % ip)
Ed Swierk76cce912012-07-11 18:59:34 -0700154 else:
155 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_DST_MASK)
156 | (index << ofp.OFPFW_NW_DST_SHIFT))
157 pkt0 = simple_tcp_packet(ip_dst=ip0)
158 pkt1 = simple_tcp_packet(ip_dst=ip1)
159 pkt2 = simple_tcp_packet(ip_dst=ip2)
160 pkt3 = simple_tcp_packet(ip_dst=ip3)
Rich Lane9a003812012-10-04 17:17:59 -0700161 msg = lambda ip: logging.info("Testing dest IP %s" % ip)
Ed Swierk76cce912012-07-11 18:59:34 -0700162
Rich Lane9a003812012-10-04 17:17:59 -0700163 rc = delete_all_flows(self.controller)
Ed Swierk76cce912012-07-11 18:59:34 -0700164 self.assertEqual(rc, 0, "Failed to delete all flows")
165
166 rc = self.controller.message_send(flow_msg_create(
167 self, pkt0, ing_port=ports[0], egr_ports=[ports[1]],
168 wildcards=wildcards))
169 self.assertNotEqual(rc, -1, "Error inserting flow entry 0")
170 rc = self.controller.message_send(flow_msg_create(
171 self, pkt1, ing_port=ports[0], egr_ports=[ports[2]],
172 wildcards=wildcards))
173 self.assertNotEqual(rc, -1, "Error inserting flow entry 1")
174
175 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
176
177 msg(ip0)
178 self.dataplane.send(ports[0], str(pkt0))
179 receive_pkt_verify(self, [ports[1]], pkt0, ports[0])
180
181 msg(ip1)
182 self.dataplane.send(ports[0], str(pkt1))
183 receive_pkt_verify(self, [ports[2]], pkt1, ports[0])
184
185 msg(ip2)
186 self.dataplane.send(ports[0], str(pkt2))
187 receive_pkt_verify(self, [ports[1]], pkt2, ports[0])
188
189 msg(ip3)
190 self.dataplane.send(ports[0], str(pkt3))
191 receive_pkt_verify(self, [ports[2]], pkt3, ports[0])