Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 3 | import unittest |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 4 | import socket |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 5 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 6 | from framework import VppTestCase, VppTestRunner |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 7 | from vpp_sub_interface import VppSubInterface, VppDot1QSubint |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 8 | |
| 9 | from scapy.packet import Raw |
| 10 | from scapy.layers.l2 import Ether, Dot1Q |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 11 | from scapy.layers.inet6 import IPv6, UDP |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 12 | from util import ppp |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 13 | |
| 14 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 15 | class TestIPv6(VppTestCase): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 16 | """ IPv6 Test Case """ |
| 17 | |
| 18 | @classmethod |
| 19 | def setUpClass(cls): |
| 20 | super(TestIPv6, cls).setUpClass() |
| 21 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 22 | def setUp(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 23 | """ |
| 24 | Perform test setup before test case. |
| 25 | |
| 26 | **Config:** |
| 27 | - create 3 pg interfaces |
| 28 | - untagged pg0 interface |
| 29 | - Dot1Q subinterface on pg1 |
| 30 | - Dot1AD subinterface on pg2 |
| 31 | - setup interfaces: |
| 32 | - put it into UP state |
| 33 | - set IPv6 addresses |
| 34 | - resolve neighbor address using NDP |
| 35 | - configure 200 fib entries |
| 36 | |
| 37 | :ivar list interfaces: pg interfaces and subinterfaces. |
| 38 | :ivar dict flows: IPv4 packet flows in test. |
| 39 | :ivar list pg_if_packet_sizes: packet sizes in test. |
| 40 | |
| 41 | *TODO:* Create AD sub interface |
| 42 | """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 43 | super(TestIPv6, self).setUp() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 44 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 45 | # create 3 pg interfaces |
| 46 | self.create_pg_interfaces(range(3)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 47 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 48 | # create 2 subinterfaces for p1 and pg2 |
| 49 | self.sub_interfaces = [ |
| 50 | VppDot1QSubint(self, self.pg1, 100), |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 51 | VppDot1QSubint(self, self.pg2, 200) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 52 | # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400) |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 53 | ] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 54 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 55 | # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc. |
| 56 | self.flows = dict() |
| 57 | self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if] |
| 58 | self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if] |
| 59 | self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 60 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 61 | # packet sizes |
| 62 | self.pg_if_packet_sizes = [64, 512, 1518, 9018] |
| 63 | self.sub_if_packet_sizes = [64, 512, 1518 + 4, 9018 + 4] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 64 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 65 | self.interfaces = list(self.pg_interfaces) |
| 66 | self.interfaces.extend(self.sub_interfaces) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 67 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 68 | # setup all interfaces |
| 69 | for i in self.interfaces: |
| 70 | i.admin_up() |
| 71 | i.config_ip6() |
| 72 | i.resolve_ndp() |
| 73 | |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 74 | # config 2M FIB entries |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 75 | self.config_fib_entries(200) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 76 | |
| 77 | def tearDown(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 78 | """Run standard test teardown and log ``show ip6 neighbors``.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 79 | super(TestIPv6, self).tearDown() |
| 80 | if not self.vpp_dead: |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 81 | self.logger.info(self.vapi.cli("show ip6 neighbors")) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 82 | # info(self.vapi.cli("show ip6 fib")) # many entries |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 83 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 84 | def config_fib_entries(self, count): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 85 | """For each interface add to the FIB table *count* routes to |
| 86 | "fd02::1/128" destination with interface's local address as next-hop |
| 87 | address. |
| 88 | |
| 89 | :param int count: Number of FIB entries. |
| 90 | |
| 91 | - *TODO:* check if the next-hop address shouldn't be remote address |
| 92 | instead of local address. |
| 93 | """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 94 | n_int = len(self.interfaces) |
| 95 | percent = 0 |
| 96 | counter = 0.0 |
| 97 | dest_addr = socket.inet_pton(socket.AF_INET6, "fd02::1") |
| 98 | dest_addr_len = 128 |
| 99 | for i in self.interfaces: |
| 100 | next_hop_address = i.local_ip6n |
| 101 | for j in range(count / n_int): |
| 102 | self.vapi.ip_add_del_route( |
| 103 | dest_addr, dest_addr_len, next_hop_address, is_ipv6=1) |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 104 | counter += 1 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 105 | if counter / count * 100 > percent: |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 106 | self.logger.info("Configure %d FIB entries .. %d%% done" % |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 107 | (count, percent)) |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 108 | percent += 1 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 109 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 110 | def create_stream(self, src_if, packet_sizes): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 111 | """Create input packet stream for defined interface. |
| 112 | |
| 113 | :param VppInterface src_if: Interface to create packet stream for. |
| 114 | :param list packet_sizes: Required packet sizes. |
| 115 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 116 | pkts = [] |
| 117 | for i in range(0, 257): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 118 | dst_if = self.flows[src_if][i % 2] |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 119 | info = self.create_packet_info(src_if, dst_if) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 120 | payload = self.info_to_payload(info) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 121 | p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / |
| 122 | IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6) / |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 123 | UDP(sport=1234, dport=1234) / |
| 124 | Raw(payload)) |
| 125 | info.data = p.copy() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 126 | if isinstance(src_if, VppSubInterface): |
| 127 | p = src_if.add_dot1_layer(p) |
| 128 | size = packet_sizes[(i // 2) % len(packet_sizes)] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 129 | self.extend_packet(p, size) |
| 130 | pkts.append(p) |
| 131 | return pkts |
| 132 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 133 | def verify_capture(self, dst_if, capture): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 134 | """Verify captured input packet stream for defined interface. |
| 135 | |
| 136 | :param VppInterface dst_if: Interface to verify captured packet stream |
| 137 | for. |
| 138 | :param list capture: Captured packet stream. |
| 139 | """ |
| 140 | self.logger.info("Verifying capture on interface %s" % dst_if.name) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 141 | last_info = dict() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 142 | for i in self.interfaces: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 143 | last_info[i.sw_if_index] = None |
| 144 | is_sub_if = False |
| 145 | dst_sw_if_index = dst_if.sw_if_index |
| 146 | if hasattr(dst_if, 'parent'): |
| 147 | is_sub_if = True |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 148 | for packet in capture: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 149 | if is_sub_if: |
| 150 | # Check VLAN tags and Ethernet header |
| 151 | packet = dst_if.remove_dot1_layer(packet) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 152 | self.assertTrue(Dot1Q not in packet) |
| 153 | try: |
| 154 | ip = packet[IPv6] |
| 155 | udp = packet[UDP] |
| 156 | payload_info = self.payload_to_info(str(packet[Raw])) |
| 157 | packet_index = payload_info.index |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 158 | self.assertEqual(payload_info.dst, dst_sw_if_index) |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 159 | self.logger.debug("Got packet on port %s: src=%u (id=%u)" % |
| 160 | (dst_if.name, payload_info.src, packet_index)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 161 | next_info = self.get_next_packet_info_for_interface2( |
| 162 | payload_info.src, dst_sw_if_index, |
| 163 | last_info[payload_info.src]) |
| 164 | last_info[payload_info.src] = next_info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 165 | self.assertTrue(next_info is not None) |
| 166 | self.assertEqual(packet_index, next_info.index) |
| 167 | saved_packet = next_info.data |
| 168 | # Check standard fields |
| 169 | self.assertEqual(ip.src, saved_packet[IPv6].src) |
| 170 | self.assertEqual(ip.dst, saved_packet[IPv6].dst) |
| 171 | self.assertEqual(udp.sport, saved_packet[UDP].sport) |
| 172 | self.assertEqual(udp.dport, saved_packet[UDP].dport) |
| 173 | except: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 174 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 175 | raise |
| 176 | for i in self.interfaces: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 177 | remaining_packet = self.get_next_packet_info_for_interface2( |
| 178 | i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index]) |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 179 | self.assertTrue(remaining_packet is None, |
| 180 | "Interface %s: Packet expected from interface %s " |
| 181 | "didn't arrive" % (dst_if.name, i.name)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 182 | |
| 183 | def test_fib(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 184 | """ IPv6 FIB test |
| 185 | |
| 186 | Test scenario: |
| 187 | - Create IPv6 stream for pg0 interface |
| 188 | - Create IPv6 tagged streams for pg1's and pg2's subinterface. |
| 189 | - Send and verify received packets on each interface. |
| 190 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 191 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 192 | pkts = self.create_stream(self.pg0, self.pg_if_packet_sizes) |
| 193 | self.pg0.add_stream(pkts) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 194 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 195 | for i in self.sub_interfaces: |
| 196 | pkts = self.create_stream(i, self.sub_if_packet_sizes) |
| 197 | i.parent.add_stream(pkts) |
| 198 | |
| 199 | self.pg_enable_capture(self.pg_interfaces) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 200 | self.pg_start() |
| 201 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 202 | pkts = self.pg0.get_capture() |
| 203 | self.verify_capture(self.pg0, pkts) |
| 204 | |
| 205 | for i in self.sub_interfaces: |
| 206 | pkts = i.parent.get_capture() |
| 207 | self.verify_capture(i, pkts) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 208 | |
| 209 | |
| 210 | if __name__ == '__main__': |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 211 | unittest.main(testRunner=VppTestRunner) |