blob: e8b12f688d7abc0b57f250539663b0155d3f95ff [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Damjan Marionf56b77a2016-10-03 19:44:57 +02003import unittest
Klement Sekeraf62ae122016-10-11 11:47:09 +02004import socket
Klement Sekeraf62ae122016-10-11 11:47:09 +02005
Damjan Marionf56b77a2016-10-03 19:44:57 +02006from framework import VppTestCase, VppTestRunner
Matej Klotton86d87c42016-11-11 11:38:55 +01007from vpp_sub_interface import VppSubInterface, VppDot1QSubint
Damjan Marionf56b77a2016-10-03 19:44:57 +02008
9from scapy.packet import Raw
10from scapy.layers.l2 import Ether, Dot1Q
Matej Klotton86d87c42016-11-11 11:38:55 +010011from scapy.layers.inet6 import IPv6, UDP
Klement Sekera7bb873a2016-11-18 07:38:42 +010012from util import ppp
Damjan Marionf56b77a2016-10-03 19:44:57 +020013
14
Klement Sekeraf62ae122016-10-11 11:47:09 +020015class TestIPv6(VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020016 """ IPv6 Test Case """
17
18 @classmethod
19 def setUpClass(cls):
20 super(TestIPv6, cls).setUpClass()
21
Klement Sekeraf62ae122016-10-11 11:47:09 +020022 def setUp(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010023 """
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 Sekeraf62ae122016-10-11 11:47:09 +020043 super(TestIPv6, self).setUp()
Damjan Marionf56b77a2016-10-03 19:44:57 +020044
Klement Sekeraf62ae122016-10-11 11:47:09 +020045 # create 3 pg interfaces
46 self.create_pg_interfaces(range(3))
Damjan Marionf56b77a2016-10-03 19:44:57 +020047
Klement Sekeraf62ae122016-10-11 11:47:09 +020048 # create 2 subinterfaces for p1 and pg2
49 self.sub_interfaces = [
50 VppDot1QSubint(self, self.pg1, 100),
Matej Klotton86d87c42016-11-11 11:38:55 +010051 VppDot1QSubint(self, self.pg2, 200)
Klement Sekeraf62ae122016-10-11 11:47:09 +020052 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
Matej Klotton86d87c42016-11-11 11:38:55 +010053 ]
Damjan Marionf56b77a2016-10-03 19:44:57 +020054
Klement Sekeraf62ae122016-10-11 11:47:09 +020055 # 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 Marionf56b77a2016-10-03 19:44:57 +020060
Klement Sekeraf62ae122016-10-11 11:47:09 +020061 # 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 Marionf56b77a2016-10-03 19:44:57 +020064
Klement Sekeraf62ae122016-10-11 11:47:09 +020065 self.interfaces = list(self.pg_interfaces)
66 self.interfaces.extend(self.sub_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +020067
Klement Sekeraf62ae122016-10-11 11:47:09 +020068 # setup all interfaces
69 for i in self.interfaces:
70 i.admin_up()
71 i.config_ip6()
72 i.resolve_ndp()
73
Matej Klotton86d87c42016-11-11 11:38:55 +010074 # config 2M FIB entries
Klement Sekeraf62ae122016-10-11 11:47:09 +020075 self.config_fib_entries(200)
Damjan Marionf56b77a2016-10-03 19:44:57 +020076
77 def tearDown(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010078 """Run standard test teardown and log ``show ip6 neighbors``."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020079 super(TestIPv6, self).tearDown()
80 if not self.vpp_dead:
Matej Klotton86d87c42016-11-11 11:38:55 +010081 self.logger.info(self.vapi.cli("show ip6 neighbors"))
Klement Sekeraf62ae122016-10-11 11:47:09 +020082 # info(self.vapi.cli("show ip6 fib")) # many entries
Damjan Marionf56b77a2016-10-03 19:44:57 +020083
Klement Sekeraf62ae122016-10-11 11:47:09 +020084 def config_fib_entries(self, count):
Matej Klotton86d87c42016-11-11 11:38:55 +010085 """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 Sekeraf62ae122016-10-11 11:47:09 +020094 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 Klotton86d87c42016-11-11 11:38:55 +0100104 counter += 1
Klement Sekeraf62ae122016-10-11 11:47:09 +0200105 if counter / count * 100 > percent:
Matej Klotton86d87c42016-11-11 11:38:55 +0100106 self.logger.info("Configure %d FIB entries .. %d%% done" %
Klement Sekera7bb873a2016-11-18 07:38:42 +0100107 (count, percent))
Matej Klotton86d87c42016-11-11 11:38:55 +0100108 percent += 1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200109
Klement Sekeraf62ae122016-10-11 11:47:09 +0200110 def create_stream(self, src_if, packet_sizes):
Matej Klotton86d87c42016-11-11 11:38:55 +0100111 """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 Marionf56b77a2016-10-03 19:44:57 +0200116 pkts = []
117 for i in range(0, 257):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200118 dst_if = self.flows[src_if][i % 2]
Klement Sekeradab231a2016-12-21 08:50:14 +0100119 info = self.create_packet_info(src_if, dst_if)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200120 payload = self.info_to_payload(info)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200121 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 Marionf56b77a2016-10-03 19:44:57 +0200123 UDP(sport=1234, dport=1234) /
124 Raw(payload))
125 info.data = p.copy()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200126 if isinstance(src_if, VppSubInterface):
127 p = src_if.add_dot1_layer(p)
128 size = packet_sizes[(i // 2) % len(packet_sizes)]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200129 self.extend_packet(p, size)
130 pkts.append(p)
131 return pkts
132
Klement Sekeraf62ae122016-10-11 11:47:09 +0200133 def verify_capture(self, dst_if, capture):
Matej Klotton86d87c42016-11-11 11:38:55 +0100134 """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 Sekeraf62ae122016-10-11 11:47:09 +0200141 last_info = dict()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200142 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200143 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 Marionf56b77a2016-10-03 19:44:57 +0200148 for packet in capture:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200149 if is_sub_if:
150 # Check VLAN tags and Ethernet header
151 packet = dst_if.remove_dot1_layer(packet)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200152 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 Sekeraf62ae122016-10-11 11:47:09 +0200158 self.assertEqual(payload_info.dst, dst_sw_if_index)
Matej Klotton86d87c42016-11-11 11:38:55 +0100159 self.logger.debug("Got packet on port %s: src=%u (id=%u)" %
160 (dst_if.name, payload_info.src, packet_index))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200161 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 Marionf56b77a2016-10-03 19:44:57 +0200165 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 Sekera7bb873a2016-11-18 07:38:42 +0100174 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200175 raise
176 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200177 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 Sekera7bb873a2016-11-18 07:38:42 +0100179 self.assertTrue(remaining_packet is None,
180 "Interface %s: Packet expected from interface %s "
181 "didn't arrive" % (dst_if.name, i.name))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200182
183 def test_fib(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100184 """ 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 Marionf56b77a2016-10-03 19:44:57 +0200191
Klement Sekeraf62ae122016-10-11 11:47:09 +0200192 pkts = self.create_stream(self.pg0, self.pg_if_packet_sizes)
193 self.pg0.add_stream(pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200194
Klement Sekeraf62ae122016-10-11 11:47:09 +0200195 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 Marionf56b77a2016-10-03 19:44:57 +0200200 self.pg_start()
201
Klement Sekeraf62ae122016-10-11 11:47:09 +0200202 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 Marionf56b77a2016-10-03 19:44:57 +0200208
209
210if __name__ == '__main__':
Klement Sekeraf62ae122016-10-11 11:47:09 +0200211 unittest.main(testRunner=VppTestRunner)