Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 17 | """ |
| 18 | """ |
| 19 | import struct |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 20 | import logging |
Rich Lane | 3c7cf7f | 2013-01-11 18:04:56 -0800 | [diff] [blame] | 21 | import scapy |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 22 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 23 | from oftest import config |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 24 | import oftest.controller as controller |
Rich Lane | d7b0ffa | 2013-03-08 15:53:42 -0800 | [diff] [blame] | 25 | import ofp |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 26 | import oftest.base_tests as base_tests |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 27 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 28 | from oftest.testutils import * |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 29 | |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 30 | def normal_ip_mask(index): |
| 31 | """ |
| 32 | Return the IP mask for the given wildcard index 0 - 63 per the OF 1.0 spec |
| 33 | """ |
| 34 | if index < 32: |
| 35 | return ((1 << 32) - 1) ^ ((1 << index) - 1) |
| 36 | else: |
| 37 | return 0 |
| 38 | |
| 39 | def fancy_ip_mask(index): |
| 40 | """ |
| 41 | Return the IP mask for the given wildcard index 0 - 31 per the OF 1.0 spec. |
| 42 | For wildcard index 32 - 63, return a "negative" IP mask: |
| 43 | 32 : wildcard the first bit, mask 127.255.255.255 |
| 44 | 33 : wildcard all first 2 bits, mask 63.255.255.255 |
| 45 | ... |
| 46 | 62 : wildcard all but last bit, mask 0.0.0.1 |
| 47 | 63 : wildcard all bits, mask 0.0.0.0 |
| 48 | """ |
| 49 | if index < 32: |
| 50 | return ((1 << 32) - 1) ^ ((1 << index) - 1) |
| 51 | else: |
| 52 | return (1 << (63 - index)) - 1 |
| 53 | |
Rich Lane | 0a4f637 | 2013-01-02 14:40:22 -0800 | [diff] [blame] | 54 | @nonstandard |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 55 | class BSNConfigIPMask(base_tests.SimpleDataPlane): |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 56 | """ |
| 57 | Exercise BSN vendor extension for configuring IP source/dest match mask |
| 58 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 59 | |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 60 | def bsn_set_ip_mask(self, index, mask): |
| 61 | """ |
| 62 | Use the BSN_SET_IP_MASK vendor command to change the IP mask for the |
| 63 | given wildcard index |
| 64 | """ |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 65 | logging.info("Setting index %d to mask is %s" % (index, mask)) |
Rich Lane | 4b60145 | 2013-03-11 23:37:06 -0700 | [diff] [blame] | 66 | m = ofp.message.bsn_set_ip_mask(index=index, mask=mask) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 67 | self.controller.message_send(m) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 68 | |
| 69 | def bsn_get_ip_mask(self, index): |
| 70 | """ |
Ed Swierk | aba4258 | 2012-09-14 06:40:49 -0700 | [diff] [blame] | 71 | Use the BSN_GET_IP_MASK_REQUEST vendor command to get the current IP mask |
| 72 | for the given wildcard index |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 73 | """ |
Rich Lane | 4b60145 | 2013-03-11 23:37:06 -0700 | [diff] [blame] | 74 | request = ofp.message.bsn_get_ip_mask_request(index=index) |
| 75 | reply, _ = self.controller.transact(request) |
| 76 | self.assertTrue(isinstance(reply, ofp.message.bsn_get_ip_mask_reply), "Wrong reply type") |
| 77 | self.assertEqual(reply.index, index, "Wrong index") |
| 78 | return reply.mask |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 79 | |
| 80 | def runTest(self): |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 81 | self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_DST_ALL, |
| 82 | "IP dst must be wildcarded") |
| 83 | self.assertFalse(required_wildcards(self) & ofp.OFPFW_NW_SRC_ALL, |
| 84 | "IP src must be wildcarded") |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 85 | for index in range(0, 64): |
| 86 | mask = self.bsn_get_ip_mask(index) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 87 | logging.info("Index %d mask is %s" % |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 88 | (index, scapy.utils.ltoa(mask))) |
| 89 | self.assertEqual(mask, normal_ip_mask(index), "Unexpected IP mask") |
| 90 | |
| 91 | for index in range(0, 64): |
| 92 | mask = normal_ip_mask(index) |
| 93 | if mask == 0: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 94 | logging.info("Skipping IP wildcard index %d" % index) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 95 | else: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 96 | logging.info("Testing IP wildcard index %d" % index) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 97 | self.check_ip_mask(True, index, mask) |
| 98 | self.check_ip_mask(False, index, mask) |
| 99 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 100 | logging.info("Setting fancy IP masks") |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 101 | for index in range(0, 64): |
| 102 | self.bsn_set_ip_mask(index, fancy_ip_mask(index)) |
| 103 | for index in range(0, 64): |
| 104 | mask = self.bsn_get_ip_mask(index) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 105 | logging.info("Index %d mask is %s" % |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 106 | (index, scapy.utils.ltoa(mask))) |
| 107 | self.assertEqual(mask, fancy_ip_mask(index), "Unexpected IP mask") |
| 108 | |
| 109 | for index in range(0, 64): |
| 110 | mask = fancy_ip_mask(index) |
| 111 | if mask == 0: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 112 | logging.info("Skipping IP wildcard index %d" % index) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 113 | else: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 114 | logging.info("Testing IP wildcard index %d" % index) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 115 | self.check_ip_mask(True, index, mask) |
| 116 | self.check_ip_mask(False, index, mask) |
| 117 | |
| 118 | def check_ip_mask(self, source, index, mask): |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 119 | ports = config["port_map"].keys() |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 120 | |
| 121 | # For each mask we install two flow entries, one which matches |
| 122 | # on IP source or dest addr all-0s (modulo the mask) and |
| 123 | # outputs to port 1, the other which matches on all-1s (modulo |
| 124 | # the mask) and outputs to port 2. |
| 125 | |
| 126 | # Then we construct four packets: The first two are the same |
| 127 | # as the two flow entry matches (all 0s and all 1s), and we |
| 128 | # check that the packets go to ports 1 and 2, respectively. |
| 129 | # For the second set of packets, we flip the un-masked bits |
| 130 | # and check that only the masked bits are matched. |
| 131 | |
| 132 | ip0 = scapy.utils.ltoa(0x00000000) |
| 133 | ip1 = scapy.utils.ltoa(0xffffffff) |
| 134 | ip2 = scapy.utils.ltoa(0xffffffff ^ mask) |
| 135 | ip3 = scapy.utils.ltoa(0x00000000 ^ mask) |
| 136 | |
| 137 | if source: |
| 138 | wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_SRC_MASK) |
| 139 | | (index << ofp.OFPFW_NW_SRC_SHIFT)) |
| 140 | pkt0 = simple_tcp_packet(ip_src=ip0) |
| 141 | pkt1 = simple_tcp_packet(ip_src=ip1) |
| 142 | pkt2 = simple_tcp_packet(ip_src=ip2) |
| 143 | pkt3 = simple_tcp_packet(ip_src=ip3) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 144 | msg = lambda ip: logging.info("Testing source IP %s" % ip) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 145 | else: |
| 146 | wildcards = ((ofp.OFPFW_ALL ^ ofp.OFPFW_DL_TYPE ^ ofp.OFPFW_NW_DST_MASK) |
| 147 | | (index << ofp.OFPFW_NW_DST_SHIFT)) |
| 148 | pkt0 = simple_tcp_packet(ip_dst=ip0) |
| 149 | pkt1 = simple_tcp_packet(ip_dst=ip1) |
| 150 | pkt2 = simple_tcp_packet(ip_dst=ip2) |
| 151 | pkt3 = simple_tcp_packet(ip_dst=ip3) |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 152 | msg = lambda ip: logging.info("Testing dest IP %s" % ip) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 153 | |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 154 | delete_all_flows(self.controller) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 155 | |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 156 | self.controller.message_send(flow_msg_create( |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 157 | self, pkt0, ing_port=ports[0], egr_ports=[ports[1]], |
| 158 | wildcards=wildcards)) |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 159 | self.controller.message_send(flow_msg_create( |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 160 | self, pkt1, ing_port=ports[0], egr_ports=[ports[2]], |
| 161 | wildcards=wildcards)) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 162 | |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 163 | do_barrier(self.controller) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 164 | |
| 165 | msg(ip0) |
| 166 | self.dataplane.send(ports[0], str(pkt0)) |
Rich Lane | e4b384d | 2013-09-13 14:33:40 -0700 | [diff] [blame] | 167 | verify_packets(self, str(pkt0), [ports[1]]) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 168 | |
| 169 | msg(ip1) |
| 170 | self.dataplane.send(ports[0], str(pkt1)) |
Rich Lane | e4b384d | 2013-09-13 14:33:40 -0700 | [diff] [blame] | 171 | verify_packets(self, str(pkt1), [ports[2]]) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 172 | |
| 173 | msg(ip2) |
| 174 | self.dataplane.send(ports[0], str(pkt2)) |
Rich Lane | e4b384d | 2013-09-13 14:33:40 -0700 | [diff] [blame] | 175 | verify_packets(self, str(pkt2), [ports[1]]) |
Ed Swierk | 76cce91 | 2012-07-11 18:59:34 -0700 | [diff] [blame] | 176 | |
| 177 | msg(ip3) |
| 178 | self.dataplane.send(ports[0], str(pkt3)) |
Rich Lane | e4b384d | 2013-09-13 14:33:40 -0700 | [diff] [blame] | 179 | verify_packets(self, str(pkt3), [ports[2]]) |