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