Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 1 | """ |
| 2 | """ |
| 3 | import struct |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 4 | import logging |
Rich Lane | 3c7cf7f | 2013-01-11 18:04:56 -0800 | [diff] [blame] | 5 | import scapy |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 6 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 7 | from oftest import config |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 8 | import oftest.controller as controller |
| 9 | import oftest.cstruct as ofp |
| 10 | import oftest.message as message |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 11 | import oftest.base_tests as base_tests |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 12 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 13 | from oftest.testutils import * |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 14 | |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 15 | def 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 | |
| 24 | def 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 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 39 | @nonstandard |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 40 | class BSNConfigIPMask(base_tests.SimpleDataPlane): |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 41 | """ |
| 42 | Exercise BSN vendor extension for configuring IP source/dest match mask |
| 43 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 44 | |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 45 | def bsn_set_ip_mask(self, index, mask): |
| 46 | """ |
| 47 | Use the BSN_SET_IP_MASK vendor command to change the IP mask for the |
| 48 | given wildcard index |
| 49 | """ |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 50 | logging.info("Setting index %d to mask is %s" % (index, mask)) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 51 | m = message.vendor() |
| 52 | m.vendor = 0x005c16c7 |
| 53 | m.data = struct.pack("!LBBBBL", 0, index, 0, 0, 0, mask) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 54 | self.controller.message_send(m) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 55 | |
| 56 | def bsn_get_ip_mask(self, index): |
| 57 | """ |
Ed Swierk | aba4258 | 2012-09-14 06:40:49 -0700 | [diff] [blame] | 58 | Use the BSN_GET_IP_MASK_REQUEST vendor command to get the current IP mask |
| 59 | for the given wildcard index |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 60 | """ |
| 61 | m = message.vendor() |
| 62 | m.vendor = 0x005c16c7 |
| 63 | m.data = struct.pack( "!LBBBBL", 1, index, 0, 0, 0, 0 ) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 64 | self.controller.message_send(m) |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 65 | m, r = self.controller.poll(ofp.OFPT_VENDOR) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 66 | self.assertEqual(m.vendor, 0x005c16c7, "Wrong vendor ID") |
| 67 | x = struct.unpack("!LBBBBL", m.data) |
Ed Swierk | aba4258 | 2012-09-14 06:40:49 -0700 | [diff] [blame] | 68 | self.assertEqual(x[0], 2, "Wrong subtype") |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 69 | self.assertEqual(x[1], index, "Wrong index") |
| 70 | return x[5] |
| 71 | |
| 72 | def runTest(self): |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 73 | self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_DST_ALL, |
| 74 | "IP dst must be wildcarded") |
| 75 | self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_SRC_ALL, |
| 76 | "IP src must be wildcarded") |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 77 | for index in range(0, 64): |
| 78 | mask = self.bsn_get_ip_mask(index) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 79 | logging.info("Index %d mask is %s" % |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 80 | (index, scapy.utils.ltoa(mask))) |
| 81 | self.assertEqual(mask, normal_ip_mask(index), "Unexpected IP mask") |
| 82 | |
| 83 | for index in range(0, 64): |
| 84 | mask = normal_ip_mask(index) |
| 85 | if mask == 0: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 86 | logging.info("Skipping IP wildcard index %d" % index) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 87 | else: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 88 | logging.info("Testing IP wildcard index %d" % index) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 89 | self.check_ip_mask(True, index, mask) |
| 90 | self.check_ip_mask(False, index, mask) |
| 91 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 92 | logging.info("Setting fancy IP masks") |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 93 | for index in range(0, 64): |
| 94 | self.bsn_set_ip_mask(index, fancy_ip_mask(index)) |
| 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, fancy_ip_mask(index), "Unexpected IP mask") |
| 100 | |
| 101 | for index in range(0, 64): |
| 102 | mask = fancy_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 | |
| 110 | def check_ip_mask(self, source, index, mask): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 111 | ports = config["port_map"].keys() |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 112 | |
| 113 | # For each mask we install two flow entries, one which matches |
| 114 | # on IP source or dest addr all-0s (modulo the mask) and |
| 115 | # outputs to port 1, the other which matches on all-1s (modulo |
| 116 | # the mask) and outputs to port 2. |
| 117 | |
| 118 | # Then we construct four packets: The first two are the same |
| 119 | # as the two flow entry matches (all 0s and all 1s), and we |
| 120 | # check that the packets go to ports 1 and 2, respectively. |
| 121 | # For the second set of packets, we flip the un-masked bits |
| 122 | # and check that only the masked bits are matched. |
| 123 | |
| 124 | ip0 = scapy.utils.ltoa(0x00000000) |
| 125 | ip1 = scapy.utils.ltoa(0xffffffff) |
| 126 | ip2 = scapy.utils.ltoa(0xffffffff ^ mask) |
| 127 | ip3 = scapy.utils.ltoa(0x00000000 ^ mask) |
| 128 | |
| 129 | if source: |
| 130 | wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_SRC_MASK) |
| 131 | | (index << ofp.OFPFW_NW_SRC_SHIFT)) |
| 132 | pkt0 = simple_tcp_packet(ip_src=ip0) |
| 133 | pkt1 = simple_tcp_packet(ip_src=ip1) |
| 134 | pkt2 = simple_tcp_packet(ip_src=ip2) |
| 135 | pkt3 = simple_tcp_packet(ip_src=ip3) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 136 | msg = lambda ip: logging.info("Testing source IP %s" % ip) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 137 | else: |
| 138 | wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_DST_MASK) |
| 139 | | (index << ofp.OFPFW_NW_DST_SHIFT)) |
| 140 | pkt0 = simple_tcp_packet(ip_dst=ip0) |
| 141 | pkt1 = simple_tcp_packet(ip_dst=ip1) |
| 142 | pkt2 = simple_tcp_packet(ip_dst=ip2) |
| 143 | pkt3 = simple_tcp_packet(ip_dst=ip3) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 144 | msg = lambda ip: logging.info("Testing dest IP %s" % ip) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 145 | |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 146 | delete_all_flows(self.controller) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 147 | |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 148 | self.controller.message_send(flow_msg_create( |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 149 | self, pkt0, ing_port=ports[0], egr_ports=[ports[1]], |
| 150 | wildcards=wildcards)) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 151 | self.controller.message_send(flow_msg_create( |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 152 | self, pkt1, ing_port=ports[0], egr_ports=[ports[2]], |
| 153 | wildcards=wildcards)) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 154 | |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 155 | do_barrier(self.controller) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 156 | |
| 157 | msg(ip0) |
| 158 | self.dataplane.send(ports[0], str(pkt0)) |
| 159 | receive_pkt_verify(self, [ports[1]], pkt0, ports[0]) |
| 160 | |
| 161 | msg(ip1) |
| 162 | self.dataplane.send(ports[0], str(pkt1)) |
| 163 | receive_pkt_verify(self, [ports[2]], pkt1, ports[0]) |
| 164 | |
| 165 | msg(ip2) |
| 166 | self.dataplane.send(ports[0], str(pkt2)) |
| 167 | receive_pkt_verify(self, [ports[1]], pkt2, ports[0]) |
| 168 | |
| 169 | msg(ip3) |
| 170 | self.dataplane.send(ports[0], str(pkt3)) |
| 171 | receive_pkt_verify(self, [ports[2]], pkt3, ports[0]) |