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