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