blob: ce785c97bf9403759248f5ea6d3443ecbfc544d9 [file] [log] [blame]
Ed Swierk76cce912012-07-11 18:59:34 -07001"""
2"""
3import struct
4
5import logging
6
Rich Lane477f4812012-10-04 22:49:00 -07007from oftest import config
Ed Swierk76cce912012-07-11 18:59:34 -07008import oftest.controller as controller
9import oftest.cstruct as ofp
10import oftest.message as message
11import basic
12
Rich Laneda3b5ad2012-10-03 09:05:32 -070013from oftest.testutils import *
Ed Swierk76cce912012-07-11 18:59:34 -070014
Ed Swierk76cce912012-07-11 18:59:34 -070015def normal_ip_mask(index):
16 """
17 Return the IP mask for the given wildcard index 0 - 63 per the OF 1.0 spec
18 """
19 if index < 32:
20 return ((1 << 32) - 1) ^ ((1 << index) - 1)
21 else:
22 return 0
23
24def fancy_ip_mask(index):
25 """
26 Return the IP mask for the given wildcard index 0 - 31 per the OF 1.0 spec.
27 For wildcard index 32 - 63, return a "negative" IP mask:
28 32 : wildcard the first bit, mask 127.255.255.255
29 33 : wildcard all first 2 bits, mask 63.255.255.255
30 ...
31 62 : wildcard all but last bit, mask 0.0.0.1
32 63 : wildcard all bits, mask 0.0.0.0
33 """
34 if index < 32:
35 return ((1 << 32) - 1) ^ ((1 << index) - 1)
36 else:
37 return (1 << (63 - index)) - 1
38
39class BSNConfigIPMask(basic.SimpleDataPlane):
40 """
41 Exercise BSN vendor extension for configuring IP source/dest match mask
42 """
Rich Laned1d9c282012-10-04 22:07:10 -070043
44 priority = -1
45
Ed Swierk76cce912012-07-11 18:59:34 -070046 def bsn_set_ip_mask(self, index, mask):
47 """
48 Use the BSN_SET_IP_MASK vendor command to change the IP mask for the
49 given wildcard index
50 """
Rich Lane9a003812012-10-04 17:17:59 -070051 logging.info("Setting index %d to mask is %s" % (index, mask))
Ed Swierk76cce912012-07-11 18:59:34 -070052 m = message.vendor()
53 m.vendor = 0x005c16c7
54 m.data = struct.pack("!LBBBBL", 0, index, 0, 0, 0, mask)
55 rc = self.controller.message_send(m)
56 self.assertNotEqual(rc, -1, "Error sending set IP mask command")
57
58 def bsn_get_ip_mask(self, index):
59 """
Ed Swierkaba42582012-09-14 06:40:49 -070060 Use the BSN_GET_IP_MASK_REQUEST vendor command to get the current IP mask
61 for the given wildcard index
Ed Swierk76cce912012-07-11 18:59:34 -070062 """
63 m = message.vendor()
64 m.vendor = 0x005c16c7
65 m.data = struct.pack( "!LBBBBL", 1, index, 0, 0, 0, 0 )
66 rc = self.controller.message_send(m)
67 self.assertNotEqual(rc, -1, "Error sending get IP mask command")
Dan Talaycoc689a792012-09-28 14:22:53 -070068 m, r = self.controller.poll(ofp.OFPT_VENDOR)
Ed Swierk76cce912012-07-11 18:59:34 -070069 self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
70 x = struct.unpack("!LBBBBL", m.data)
Ed Swierkaba42582012-09-14 06:40:49 -070071 self.assertEqual(x[0], 2, "Wrong subtype")
Ed Swierk76cce912012-07-11 18:59:34 -070072 self.assertEqual(x[1], index, "Wrong index")
73 return x[5]
74
75 def runTest(self):
Ed Swierk99a74de2012-08-22 06:40:54 -070076 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_DST_ALL,
77 "IP dst must be wildcarded")
78 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_SRC_ALL,
79 "IP src must be wildcarded")
Ed Swierk76cce912012-07-11 18:59:34 -070080 for index in range(0, 64):
81 mask = self.bsn_get_ip_mask(index)
Rich Lane9a003812012-10-04 17:17:59 -070082 logging.info("Index %d mask is %s" %
Ed Swierk76cce912012-07-11 18:59:34 -070083 (index, scapy.utils.ltoa(mask)))
84 self.assertEqual(mask, normal_ip_mask(index), "Unexpected IP mask")
85
86 for index in range(0, 64):
87 mask = normal_ip_mask(index)
88 if mask == 0:
Rich Lane9a003812012-10-04 17:17:59 -070089 logging.info("Skipping IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -070090 else:
Rich Lane9a003812012-10-04 17:17:59 -070091 logging.info("Testing IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -070092 self.check_ip_mask(True, index, mask)
93 self.check_ip_mask(False, index, mask)
94
Rich Lane9a003812012-10-04 17:17:59 -070095 logging.info("Setting fancy IP masks")
Ed Swierk76cce912012-07-11 18:59:34 -070096 for index in range(0, 64):
97 self.bsn_set_ip_mask(index, fancy_ip_mask(index))
98 for index in range(0, 64):
99 mask = self.bsn_get_ip_mask(index)
Rich Lane9a003812012-10-04 17:17:59 -0700100 logging.info("Index %d mask is %s" %
Ed Swierk76cce912012-07-11 18:59:34 -0700101 (index, scapy.utils.ltoa(mask)))
102 self.assertEqual(mask, fancy_ip_mask(index), "Unexpected IP mask")
103
104 for index in range(0, 64):
105 mask = fancy_ip_mask(index)
106 if mask == 0:
Rich Lane9a003812012-10-04 17:17:59 -0700107 logging.info("Skipping IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -0700108 else:
Rich Lane9a003812012-10-04 17:17:59 -0700109 logging.info("Testing IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -0700110 self.check_ip_mask(True, index, mask)
111 self.check_ip_mask(False, index, mask)
112
113 def check_ip_mask(self, source, index, mask):
Rich Lane477f4812012-10-04 22:49:00 -0700114 ports = config["port_map"].keys()
Ed Swierk76cce912012-07-11 18:59:34 -0700115
116 # For each mask we install two flow entries, one which matches
117 # on IP source or dest addr all-0s (modulo the mask) and
118 # outputs to port 1, the other which matches on all-1s (modulo
119 # the mask) and outputs to port 2.
120
121 # Then we construct four packets: The first two are the same
122 # as the two flow entry matches (all 0s and all 1s), and we
123 # check that the packets go to ports 1 and 2, respectively.
124 # For the second set of packets, we flip the un-masked bits
125 # and check that only the masked bits are matched.
126
127 ip0 = scapy.utils.ltoa(0x00000000)
128 ip1 = scapy.utils.ltoa(0xffffffff)
129 ip2 = scapy.utils.ltoa(0xffffffff ^ mask)
130 ip3 = scapy.utils.ltoa(0x00000000 ^ mask)
131
132 if source:
133 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_SRC_MASK)
134 | (index << ofp.OFPFW_NW_SRC_SHIFT))
135 pkt0 = simple_tcp_packet(ip_src=ip0)
136 pkt1 = simple_tcp_packet(ip_src=ip1)
137 pkt2 = simple_tcp_packet(ip_src=ip2)
138 pkt3 = simple_tcp_packet(ip_src=ip3)
Rich Lane9a003812012-10-04 17:17:59 -0700139 msg = lambda ip: logging.info("Testing source IP %s" % ip)
Ed Swierk76cce912012-07-11 18:59:34 -0700140 else:
141 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_DST_MASK)
142 | (index << ofp.OFPFW_NW_DST_SHIFT))
143 pkt0 = simple_tcp_packet(ip_dst=ip0)
144 pkt1 = simple_tcp_packet(ip_dst=ip1)
145 pkt2 = simple_tcp_packet(ip_dst=ip2)
146 pkt3 = simple_tcp_packet(ip_dst=ip3)
Rich Lane9a003812012-10-04 17:17:59 -0700147 msg = lambda ip: logging.info("Testing dest IP %s" % ip)
Ed Swierk76cce912012-07-11 18:59:34 -0700148
Rich Lane9a003812012-10-04 17:17:59 -0700149 rc = delete_all_flows(self.controller)
Ed Swierk76cce912012-07-11 18:59:34 -0700150 self.assertEqual(rc, 0, "Failed to delete all flows")
151
152 rc = self.controller.message_send(flow_msg_create(
153 self, pkt0, ing_port=ports[0], egr_ports=[ports[1]],
154 wildcards=wildcards))
155 self.assertNotEqual(rc, -1, "Error inserting flow entry 0")
156 rc = self.controller.message_send(flow_msg_create(
157 self, pkt1, ing_port=ports[0], egr_ports=[ports[2]],
158 wildcards=wildcards))
159 self.assertNotEqual(rc, -1, "Error inserting flow entry 1")
160
161 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
162
163 msg(ip0)
164 self.dataplane.send(ports[0], str(pkt0))
165 receive_pkt_verify(self, [ports[1]], pkt0, ports[0])
166
167 msg(ip1)
168 self.dataplane.send(ports[0], str(pkt1))
169 receive_pkt_verify(self, [ports[2]], pkt1, ports[0])
170
171 msg(ip2)
172 self.dataplane.send(ports[0], str(pkt2))
173 receive_pkt_verify(self, [ports[1]], pkt2, ports[0])
174
175 msg(ip3)
176 self.dataplane.send(ports[0], str(pkt3))
177 receive_pkt_verify(self, [ports[2]], pkt3, ports[0])