blob: e640089237725a753237c105323affca46ba265a [file] [log] [blame]
Ed Swierk76cce912012-07-11 18:59:34 -07001"""
2"""
3import struct
Ed Swierk76cce912012-07-11 18:59:34 -07004import logging
Rich Lane3c7cf7f2013-01-11 18:04:56 -08005import scapy
Ed Swierk76cce912012-07-11 18:59:34 -07006
Rich Lane477f4812012-10-04 22:49:00 -07007from oftest import config
Ed Swierk76cce912012-07-11 18:59:34 -07008import oftest.controller as controller
Rich Laned7b0ffa2013-03-08 15:53:42 -08009import ofp
Rich Laneb90a1c42012-10-05 09:16:05 -070010import oftest.base_tests as base_tests
Ed Swierk76cce912012-07-11 18:59:34 -070011
Rich Laneda3b5ad2012-10-03 09:05:32 -070012from oftest.testutils import *
Ed Swierk76cce912012-07-11 18:59:34 -070013
Ed Swierk76cce912012-07-11 18:59:34 -070014def normal_ip_mask(index):
15 """
16 Return the IP mask for the given wildcard index 0 - 63 per the OF 1.0 spec
17 """
18 if index < 32:
19 return ((1 << 32) - 1) ^ ((1 << index) - 1)
20 else:
21 return 0
22
23def fancy_ip_mask(index):
24 """
25 Return the IP mask for the given wildcard index 0 - 31 per the OF 1.0 spec.
26 For wildcard index 32 - 63, return a "negative" IP mask:
27 32 : wildcard the first bit, mask 127.255.255.255
28 33 : wildcard all first 2 bits, mask 63.255.255.255
29 ...
30 62 : wildcard all but last bit, mask 0.0.0.1
31 63 : wildcard all bits, mask 0.0.0.0
32 """
33 if index < 32:
34 return ((1 << 32) - 1) ^ ((1 << index) - 1)
35 else:
36 return (1 << (63 - index)) - 1
37
Rich Lane0a4f6372013-01-02 14:40:22 -080038@nonstandard
Rich Laneb90a1c42012-10-05 09:16:05 -070039class BSNConfigIPMask(base_tests.SimpleDataPlane):
Ed Swierk76cce912012-07-11 18:59:34 -070040 """
41 Exercise BSN vendor extension for configuring IP source/dest match mask
42 """
Rich Laned1d9c282012-10-04 22:07:10 -070043
Ed Swierk76cce912012-07-11 18:59:34 -070044 def bsn_set_ip_mask(self, index, mask):
45 """
46 Use the BSN_SET_IP_MASK vendor command to change the IP mask for the
47 given wildcard index
48 """
Rich Lane9a003812012-10-04 17:17:59 -070049 logging.info("Setting index %d to mask is %s" % (index, mask))
Rich Lane28fa9272013-03-08 16:00:25 -080050 m = ofp.message.vendor()
Ed Swierk76cce912012-07-11 18:59:34 -070051 m.vendor = 0x005c16c7
52 m.data = struct.pack("!LBBBBL", 0, index, 0, 0, 0, mask)
Rich Lane5c3151c2013-01-03 17:15:41 -080053 self.controller.message_send(m)
Ed Swierk76cce912012-07-11 18:59:34 -070054
55 def bsn_get_ip_mask(self, index):
56 """
Ed Swierkaba42582012-09-14 06:40:49 -070057 Use the BSN_GET_IP_MASK_REQUEST vendor command to get the current IP mask
58 for the given wildcard index
Ed Swierk76cce912012-07-11 18:59:34 -070059 """
Rich Lane28fa9272013-03-08 16:00:25 -080060 m = ofp.message.vendor()
Ed Swierk76cce912012-07-11 18:59:34 -070061 m.vendor = 0x005c16c7
62 m.data = struct.pack( "!LBBBBL", 1, index, 0, 0, 0, 0 )
Rich Lane5c3151c2013-01-03 17:15:41 -080063 self.controller.message_send(m)
Dan Talaycoc689a792012-09-28 14:22:53 -070064 m, r = self.controller.poll(ofp.OFPT_VENDOR)
Ed Swierk76cce912012-07-11 18:59:34 -070065 self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID")
66 x = struct.unpack("!LBBBBL", m.data)
Ed Swierkaba42582012-09-14 06:40:49 -070067 self.assertEqual(x[0], 2, "Wrong subtype")
Ed Swierk76cce912012-07-11 18:59:34 -070068 self.assertEqual(x[1], index, "Wrong index")
69 return x[5]
70
71 def runTest(self):
Ed Swierk99a74de2012-08-22 06:40:54 -070072 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_DST_ALL,
73 "IP dst must be wildcarded")
74 self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_SRC_ALL,
75 "IP src must be wildcarded")
Ed Swierk76cce912012-07-11 18:59:34 -070076 for index in range(0, 64):
77 mask = self.bsn_get_ip_mask(index)
Rich Lane9a003812012-10-04 17:17:59 -070078 logging.info("Index %d mask is %s" %
Ed Swierk76cce912012-07-11 18:59:34 -070079 (index, scapy.utils.ltoa(mask)))
80 self.assertEqual(mask, normal_ip_mask(index), "Unexpected IP mask")
81
82 for index in range(0, 64):
83 mask = normal_ip_mask(index)
84 if mask == 0:
Rich Lane9a003812012-10-04 17:17:59 -070085 logging.info("Skipping IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -070086 else:
Rich Lane9a003812012-10-04 17:17:59 -070087 logging.info("Testing IP wildcard index %d" % index)
Ed Swierk76cce912012-07-11 18:59:34 -070088 self.check_ip_mask(True, index, mask)
89 self.check_ip_mask(False, index, mask)
90
Rich Lane9a003812012-10-04 17:17:59 -070091 logging.info("Setting fancy IP masks")
Ed Swierk76cce912012-07-11 18:59:34 -070092 for index in range(0, 64):
93 self.bsn_set_ip_mask(index, fancy_ip_mask(index))
94 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, fancy_ip_mask(index), "Unexpected IP mask")
99
100 for index in range(0, 64):
101 mask = fancy_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
109 def check_ip_mask(self, source, index, mask):
Rich Lane477f4812012-10-04 22:49:00 -0700110 ports = config["port_map"].keys()
Ed Swierk76cce912012-07-11 18:59:34 -0700111
112 # For each mask we install two flow entries, one which matches
113 # on IP source or dest addr all-0s (modulo the mask) and
114 # outputs to port 1, the other which matches on all-1s (modulo
115 # the mask) and outputs to port 2.
116
117 # Then we construct four packets: The first two are the same
118 # as the two flow entry matches (all 0s and all 1s), and we
119 # check that the packets go to ports 1 and 2, respectively.
120 # For the second set of packets, we flip the un-masked bits
121 # and check that only the masked bits are matched.
122
123 ip0 = scapy.utils.ltoa(0x00000000)
124 ip1 = scapy.utils.ltoa(0xffffffff)
125 ip2 = scapy.utils.ltoa(0xffffffff ^ mask)
126 ip3 = scapy.utils.ltoa(0x00000000 ^ mask)
127
128 if source:
129 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_SRC_MASK)
130 | (index << ofp.OFPFW_NW_SRC_SHIFT))
131 pkt0 = simple_tcp_packet(ip_src=ip0)
132 pkt1 = simple_tcp_packet(ip_src=ip1)
133 pkt2 = simple_tcp_packet(ip_src=ip2)
134 pkt3 = simple_tcp_packet(ip_src=ip3)
Rich Lane9a003812012-10-04 17:17:59 -0700135 msg = lambda ip: logging.info("Testing source IP %s" % ip)
Ed Swierk76cce912012-07-11 18:59:34 -0700136 else:
137 wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_DST_MASK)
138 | (index << ofp.OFPFW_NW_DST_SHIFT))
139 pkt0 = simple_tcp_packet(ip_dst=ip0)
140 pkt1 = simple_tcp_packet(ip_dst=ip1)
141 pkt2 = simple_tcp_packet(ip_dst=ip2)
142 pkt3 = simple_tcp_packet(ip_dst=ip3)
Rich Lane9a003812012-10-04 17:17:59 -0700143 msg = lambda ip: logging.info("Testing dest IP %s" % ip)
Ed Swierk76cce912012-07-11 18:59:34 -0700144
Rich Lane32bf9482013-01-03 17:26:30 -0800145 delete_all_flows(self.controller)
Ed Swierk76cce912012-07-11 18:59:34 -0700146
Rich Lane5c3151c2013-01-03 17:15:41 -0800147 self.controller.message_send(flow_msg_create(
Ed Swierk76cce912012-07-11 18:59:34 -0700148 self, pkt0, ing_port=ports[0], egr_ports=[ports[1]],
149 wildcards=wildcards))
Rich Lane5c3151c2013-01-03 17:15:41 -0800150 self.controller.message_send(flow_msg_create(
Ed Swierk76cce912012-07-11 18:59:34 -0700151 self, pkt1, ing_port=ports[0], egr_ports=[ports[2]],
152 wildcards=wildcards))
Ed Swierk76cce912012-07-11 18:59:34 -0700153
Rich Lane3a261d52013-01-03 17:45:08 -0800154 do_barrier(self.controller)
Ed Swierk76cce912012-07-11 18:59:34 -0700155
156 msg(ip0)
157 self.dataplane.send(ports[0], str(pkt0))
158 receive_pkt_verify(self, [ports[1]], pkt0, ports[0])
159
160 msg(ip1)
161 self.dataplane.send(ports[0], str(pkt1))
162 receive_pkt_verify(self, [ports[2]], pkt1, ports[0])
163
164 msg(ip2)
165 self.dataplane.send(ports[0], str(pkt2))
166 receive_pkt_verify(self, [ports[1]], pkt2, ports[0])
167
168 msg(ip3)
169 self.dataplane.send(ports[0], str(pkt3))
170 receive_pkt_verify(self, [ports[2]], pkt3, ports[0])