Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 2 | import random |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 3 | import socket |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 4 | import unittest |
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 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 7 | from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame^] | 8 | from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpMRoute, \ |
| 9 | VppMRoutePath, MRouteItfFlags, MRouteEntryFlags |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 10 | |
| 11 | from scapy.packet import Raw |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 12 | from scapy.layers.l2 import Ether, Dot1Q |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 13 | from scapy.layers.inet import IP, UDP, ICMP, icmptypes, icmpcodes |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 14 | from util import ppp |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 15 | |
| 16 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 17 | class TestIPv4(VppTestCase): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 18 | """ IPv4 Test Case """ |
| 19 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 20 | def setUp(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 21 | """ |
| 22 | Perform test setup before test case. |
| 23 | |
| 24 | **Config:** |
| 25 | - create 3 pg interfaces |
| 26 | - untagged pg0 interface |
| 27 | - Dot1Q subinterface on pg1 |
| 28 | - Dot1AD subinterface on pg2 |
| 29 | - setup interfaces: |
| 30 | - put it into UP state |
| 31 | - set IPv4 addresses |
| 32 | - resolve neighbor address using ARP |
| 33 | - configure 200 fib entries |
| 34 | |
| 35 | :ivar list interfaces: pg interfaces and subinterfaces. |
| 36 | :ivar dict flows: IPv4 packet flows in test. |
| 37 | :ivar list pg_if_packet_sizes: packet sizes in test. |
| 38 | """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 39 | super(TestIPv4, self).setUp() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 40 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 41 | # create 3 pg interfaces |
| 42 | self.create_pg_interfaces(range(3)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 43 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 44 | # create 2 subinterfaces for pg1 and pg2 |
| 45 | self.sub_interfaces = [ |
| 46 | VppDot1QSubint(self, self.pg1, 100), |
| 47 | VppDot1ADSubint(self, self.pg2, 200, 300, 400)] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 48 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 49 | # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc. |
| 50 | self.flows = dict() |
| 51 | self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if] |
| 52 | self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if] |
| 53 | self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if] |
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 sizes |
| 56 | self.pg_if_packet_sizes = [64, 512, 1518, 9018] |
| 57 | self.sub_if_packet_sizes = [64, 512, 1518 + 4, 9018 + 4] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 58 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 59 | self.interfaces = list(self.pg_interfaces) |
| 60 | self.interfaces.extend(self.sub_interfaces) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 61 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 62 | # setup all interfaces |
| 63 | for i in self.interfaces: |
| 64 | i.admin_up() |
| 65 | i.config_ip4() |
| 66 | i.resolve_arp() |
| 67 | |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 68 | # config 2M FIB entries |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 69 | self.config_fib_entries(200) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 70 | |
| 71 | def tearDown(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 72 | """Run standard test teardown and log ``show ip arp``.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 73 | super(TestIPv4, self).tearDown() |
| 74 | if not self.vpp_dead: |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 75 | self.logger.info(self.vapi.cli("show ip arp")) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 76 | # info(self.vapi.cli("show ip fib")) # many entries |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 77 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 78 | def config_fib_entries(self, count): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 79 | """For each interface add to the FIB table *count* routes to |
| 80 | "10.0.0.1/32" destination with interface's local address as next-hop |
| 81 | address. |
| 82 | |
| 83 | :param int count: Number of FIB entries. |
| 84 | |
| 85 | - *TODO:* check if the next-hop address shouldn't be remote address |
| 86 | instead of local address. |
| 87 | """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 88 | n_int = len(self.interfaces) |
| 89 | percent = 0 |
| 90 | counter = 0.0 |
| 91 | dest_addr = socket.inet_pton(socket.AF_INET, "10.0.0.1") |
| 92 | dest_addr_len = 32 |
| 93 | for i in self.interfaces: |
| 94 | next_hop_address = i.local_ip4n |
| 95 | for j in range(count / n_int): |
| 96 | self.vapi.ip_add_del_route( |
| 97 | dest_addr, dest_addr_len, next_hop_address) |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 98 | counter += 1 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 99 | if counter / count * 100 > percent: |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 100 | self.logger.info("Configure %d FIB entries .. %d%% done" % |
| 101 | (count, percent)) |
| 102 | percent += 1 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 103 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 104 | def create_stream(self, src_if, packet_sizes): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 105 | """Create input packet stream for defined interface. |
| 106 | |
| 107 | :param VppInterface src_if: Interface to create packet stream for. |
| 108 | :param list packet_sizes: Required packet sizes. |
| 109 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 110 | pkts = [] |
| 111 | for i in range(0, 257): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 112 | dst_if = self.flows[src_if][i % 2] |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 113 | info = self.create_packet_info(src_if, dst_if) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 114 | payload = self.info_to_payload(info) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 115 | p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / |
| 116 | IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4) / |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 117 | UDP(sport=1234, dport=1234) / |
| 118 | Raw(payload)) |
| 119 | info.data = p.copy() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 120 | if isinstance(src_if, VppSubInterface): |
| 121 | p = src_if.add_dot1_layer(p) |
| 122 | size = packet_sizes[(i // 2) % len(packet_sizes)] |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 123 | self.extend_packet(p, size) |
| 124 | pkts.append(p) |
| 125 | return pkts |
| 126 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 127 | def verify_capture(self, dst_if, capture): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 128 | """Verify captured input packet stream for defined interface. |
| 129 | |
| 130 | :param VppInterface dst_if: Interface to verify captured packet stream |
| 131 | for. |
| 132 | :param list capture: Captured packet stream. |
| 133 | """ |
| 134 | self.logger.info("Verifying capture on interface %s" % dst_if.name) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 135 | last_info = dict() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 136 | for i in self.interfaces: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 137 | last_info[i.sw_if_index] = None |
| 138 | is_sub_if = False |
| 139 | dst_sw_if_index = dst_if.sw_if_index |
| 140 | if hasattr(dst_if, 'parent'): |
| 141 | is_sub_if = True |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 142 | for packet in capture: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 143 | if is_sub_if: |
| 144 | # Check VLAN tags and Ethernet header |
| 145 | packet = dst_if.remove_dot1_layer(packet) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 146 | self.assertTrue(Dot1Q not in packet) |
| 147 | try: |
| 148 | ip = packet[IP] |
| 149 | udp = packet[UDP] |
| 150 | payload_info = self.payload_to_info(str(packet[Raw])) |
| 151 | packet_index = payload_info.index |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 152 | self.assertEqual(payload_info.dst, dst_sw_if_index) |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 153 | self.logger.debug( |
| 154 | "Got packet on port %s: src=%u (id=%u)" % |
| 155 | (dst_if.name, payload_info.src, packet_index)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 156 | next_info = self.get_next_packet_info_for_interface2( |
| 157 | payload_info.src, dst_sw_if_index, |
| 158 | last_info[payload_info.src]) |
| 159 | last_info[payload_info.src] = next_info |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 160 | self.assertTrue(next_info is not None) |
| 161 | self.assertEqual(packet_index, next_info.index) |
| 162 | saved_packet = next_info.data |
| 163 | # Check standard fields |
| 164 | self.assertEqual(ip.src, saved_packet[IP].src) |
| 165 | self.assertEqual(ip.dst, saved_packet[IP].dst) |
| 166 | self.assertEqual(udp.sport, saved_packet[UDP].sport) |
| 167 | self.assertEqual(udp.dport, saved_packet[UDP].dport) |
| 168 | except: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 169 | self.logger.error(ppp("Unexpected or invalid packet:", packet)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 170 | raise |
| 171 | for i in self.interfaces: |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 172 | remaining_packet = self.get_next_packet_info_for_interface2( |
| 173 | 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] | 174 | self.assertTrue(remaining_packet is None, |
| 175 | "Interface %s: Packet expected from interface %s " |
| 176 | "didn't arrive" % (dst_if.name, i.name)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 177 | |
| 178 | def test_fib(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 179 | """ IPv4 FIB test |
| 180 | |
| 181 | Test scenario: |
| 182 | |
| 183 | - Create IPv4 stream for pg0 interface |
| 184 | - Create IPv4 tagged streams for pg1's and pg2's subinterface. |
| 185 | - Send and verify received packets on each interface. |
| 186 | """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 187 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 188 | pkts = self.create_stream(self.pg0, self.pg_if_packet_sizes) |
| 189 | self.pg0.add_stream(pkts) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 190 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 191 | for i in self.sub_interfaces: |
| 192 | pkts = self.create_stream(i, self.sub_if_packet_sizes) |
| 193 | i.parent.add_stream(pkts) |
| 194 | |
| 195 | self.pg_enable_capture(self.pg_interfaces) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 196 | self.pg_start() |
| 197 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 198 | pkts = self.pg0.get_capture() |
| 199 | self.verify_capture(self.pg0, pkts) |
| 200 | |
| 201 | for i in self.sub_interfaces: |
| 202 | pkts = i.parent.get_capture() |
| 203 | self.verify_capture(i, pkts) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 204 | |
| 205 | |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 206 | class TestIPv4FibCrud(VppTestCase): |
| 207 | """ FIB - add/update/delete - ip4 routes |
| 208 | |
| 209 | Test scenario: |
| 210 | - add 1k, |
| 211 | - del 100, |
| 212 | - add new 1k, |
| 213 | - del 1.5k |
| 214 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 215 | ..note:: Python API is too slow to add many routes, needs replacement. |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 216 | """ |
| 217 | |
| 218 | def config_fib_many_to_one(self, start_dest_addr, next_hop_addr, count): |
| 219 | """ |
| 220 | |
| 221 | :param start_dest_addr: |
| 222 | :param next_hop_addr: |
| 223 | :param count: |
| 224 | :return list: added ips with 32 prefix |
| 225 | """ |
| 226 | added_ips = [] |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 227 | dest_addr = int(socket.inet_pton(socket.AF_INET, |
| 228 | start_dest_addr).encode('hex'), |
| 229 | 16) |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 230 | dest_addr_len = 32 |
| 231 | n_next_hop_addr = socket.inet_pton(socket.AF_INET, next_hop_addr) |
| 232 | for _ in range(count): |
| 233 | n_dest_addr = '{:08x}'.format(dest_addr).decode('hex') |
| 234 | self.vapi.ip_add_del_route(n_dest_addr, dest_addr_len, |
| 235 | n_next_hop_addr) |
| 236 | added_ips.append(socket.inet_ntoa(n_dest_addr)) |
| 237 | dest_addr += 1 |
| 238 | return added_ips |
| 239 | |
| 240 | def unconfig_fib_many_to_one(self, start_dest_addr, next_hop_addr, count): |
| 241 | |
| 242 | removed_ips = [] |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 243 | dest_addr = int(socket.inet_pton(socket.AF_INET, |
| 244 | start_dest_addr).encode('hex'), |
| 245 | 16) |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 246 | dest_addr_len = 32 |
| 247 | n_next_hop_addr = socket.inet_pton(socket.AF_INET, next_hop_addr) |
| 248 | for _ in range(count): |
| 249 | n_dest_addr = '{:08x}'.format(dest_addr).decode('hex') |
| 250 | self.vapi.ip_add_del_route(n_dest_addr, dest_addr_len, |
| 251 | n_next_hop_addr, is_add=0) |
| 252 | removed_ips.append(socket.inet_ntoa(n_dest_addr)) |
| 253 | dest_addr += 1 |
| 254 | return removed_ips |
| 255 | |
| 256 | def create_stream(self, src_if, dst_if, dst_ips, count): |
| 257 | pkts = [] |
| 258 | |
| 259 | for _ in range(count): |
| 260 | dst_addr = random.choice(dst_ips) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 261 | info = self.create_packet_info(src_if, dst_if) |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 262 | payload = self.info_to_payload(info) |
| 263 | p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / |
| 264 | IP(src=src_if.remote_ip4, dst=dst_addr) / |
| 265 | UDP(sport=1234, dport=1234) / |
| 266 | Raw(payload)) |
| 267 | info.data = p.copy() |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 268 | self.extend_packet(p, random.choice(self.pg_if_packet_sizes)) |
| 269 | pkts.append(p) |
| 270 | |
| 271 | return pkts |
| 272 | |
| 273 | def _find_ip_match(self, find_in, pkt): |
| 274 | for p in find_in: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 275 | if self.payload_to_info(str(p[Raw])) == \ |
| 276 | self.payload_to_info(str(pkt[Raw])): |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 277 | if p[IP].src != pkt[IP].src: |
| 278 | break |
| 279 | if p[IP].dst != pkt[IP].dst: |
| 280 | break |
| 281 | if p[UDP].sport != pkt[UDP].sport: |
| 282 | break |
| 283 | if p[UDP].dport != pkt[UDP].dport: |
| 284 | break |
| 285 | return p |
| 286 | return None |
| 287 | |
| 288 | @staticmethod |
| 289 | def _match_route_detail(route_detail, ip, address_length=32, table_id=0): |
| 290 | if route_detail.address == socket.inet_pton(socket.AF_INET, ip): |
| 291 | if route_detail.table_id != table_id: |
| 292 | return False |
| 293 | elif route_detail.address_length != address_length: |
| 294 | return False |
| 295 | else: |
| 296 | return True |
| 297 | else: |
| 298 | return False |
| 299 | |
| 300 | def verify_capture(self, dst_interface, received_pkts, expected_pkts): |
| 301 | self.assertEqual(len(received_pkts), len(expected_pkts)) |
| 302 | to_verify = list(expected_pkts) |
| 303 | for p in received_pkts: |
| 304 | self.assertEqual(p.src, dst_interface.local_mac) |
| 305 | self.assertEqual(p.dst, dst_interface.remote_mac) |
| 306 | x = self._find_ip_match(to_verify, p) |
| 307 | to_verify.remove(x) |
| 308 | self.assertListEqual(to_verify, []) |
| 309 | |
| 310 | def verify_route_dump(self, fib_dump, ips): |
| 311 | |
| 312 | def _ip_in_route_dump(ip, fib_dump): |
| 313 | return next((route for route in fib_dump |
| 314 | if self._match_route_detail(route, ip)), |
| 315 | False) |
| 316 | |
| 317 | for ip in ips: |
| 318 | self.assertTrue(_ip_in_route_dump(ip, fib_dump), |
| 319 | 'IP {} is not in fib dump.'.format(ip)) |
| 320 | |
| 321 | def verify_not_in_route_dump(self, fib_dump, ips): |
| 322 | |
| 323 | def _ip_in_route_dump(ip, fib_dump): |
| 324 | return next((route for route in fib_dump |
| 325 | if self._match_route_detail(route, ip)), |
| 326 | False) |
| 327 | |
| 328 | for ip in ips: |
| 329 | self.assertFalse(_ip_in_route_dump(ip, fib_dump), |
| 330 | 'IP {} is in fib dump.'.format(ip)) |
| 331 | |
| 332 | @classmethod |
| 333 | def setUpClass(cls): |
| 334 | """ |
| 335 | #. Create and initialize 3 pg interfaces. |
| 336 | #. initialize class attributes configured_routes and deleted_routes |
| 337 | to store information between tests. |
| 338 | """ |
| 339 | super(TestIPv4FibCrud, cls).setUpClass() |
| 340 | |
| 341 | try: |
| 342 | # create 3 pg interfaces |
| 343 | cls.create_pg_interfaces(range(3)) |
| 344 | |
| 345 | cls.interfaces = list(cls.pg_interfaces) |
| 346 | |
| 347 | # setup all interfaces |
| 348 | for i in cls.interfaces: |
| 349 | i.admin_up() |
| 350 | i.config_ip4() |
| 351 | i.resolve_arp() |
| 352 | |
| 353 | cls.configured_routes = [] |
| 354 | cls.deleted_routes = [] |
| 355 | cls.pg_if_packet_sizes = [64, 512, 1518, 9018] |
| 356 | |
| 357 | except Exception: |
| 358 | super(TestIPv4FibCrud, cls).tearDownClass() |
| 359 | raise |
| 360 | |
| 361 | def setUp(self): |
| 362 | super(TestIPv4FibCrud, self).setUp() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 363 | self.reset_packet_infos() |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 364 | |
| 365 | def test_1_add_routes(self): |
| 366 | """ Add 1k routes |
| 367 | |
| 368 | - add 100 routes check with traffic script. |
| 369 | """ |
| 370 | # config 1M FIB entries |
| 371 | self.configured_routes.extend(self.config_fib_many_to_one( |
| 372 | "10.0.0.0", self.pg0.remote_ip4, 100)) |
| 373 | |
| 374 | fib_dump = self.vapi.ip_fib_dump() |
| 375 | self.verify_route_dump(fib_dump, self.configured_routes) |
| 376 | |
| 377 | self.stream_1 = self.create_stream( |
| 378 | self.pg1, self.pg0, self.configured_routes, 100) |
| 379 | self.stream_2 = self.create_stream( |
| 380 | self.pg2, self.pg0, self.configured_routes, 100) |
| 381 | self.pg1.add_stream(self.stream_1) |
| 382 | self.pg2.add_stream(self.stream_2) |
| 383 | |
| 384 | self.pg_enable_capture(self.pg_interfaces) |
| 385 | self.pg_start() |
| 386 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 387 | pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2)) |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 388 | self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2) |
| 389 | |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 390 | def test_2_del_routes(self): |
| 391 | """ Delete 100 routes |
| 392 | |
| 393 | - delete 10 routes check with traffic script. |
| 394 | """ |
| 395 | self.deleted_routes.extend(self.unconfig_fib_many_to_one( |
| 396 | "10.0.0.10", self.pg0.remote_ip4, 10)) |
| 397 | for x in self.deleted_routes: |
| 398 | self.configured_routes.remove(x) |
| 399 | |
| 400 | fib_dump = self.vapi.ip_fib_dump() |
| 401 | self.verify_route_dump(fib_dump, self.configured_routes) |
| 402 | |
| 403 | self.stream_1 = self.create_stream( |
| 404 | self.pg1, self.pg0, self.configured_routes, 100) |
| 405 | self.stream_2 = self.create_stream( |
| 406 | self.pg2, self.pg0, self.configured_routes, 100) |
| 407 | self.stream_3 = self.create_stream( |
| 408 | self.pg1, self.pg0, self.deleted_routes, 100) |
| 409 | self.stream_4 = self.create_stream( |
| 410 | self.pg2, self.pg0, self.deleted_routes, 100) |
| 411 | self.pg1.add_stream(self.stream_1 + self.stream_3) |
| 412 | self.pg2.add_stream(self.stream_2 + self.stream_4) |
| 413 | self.pg_enable_capture(self.pg_interfaces) |
| 414 | self.pg_start() |
| 415 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 416 | pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2)) |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 417 | self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2) |
| 418 | |
| 419 | def test_3_add_new_routes(self): |
| 420 | """ Add 1k routes |
| 421 | |
| 422 | - re-add 5 routes check with traffic script. |
| 423 | - add 100 routes check with traffic script. |
| 424 | """ |
| 425 | tmp = self.config_fib_many_to_one( |
| 426 | "10.0.0.10", self.pg0.remote_ip4, 5) |
| 427 | self.configured_routes.extend(tmp) |
| 428 | for x in tmp: |
| 429 | self.deleted_routes.remove(x) |
| 430 | |
| 431 | self.configured_routes.extend(self.config_fib_many_to_one( |
| 432 | "10.0.1.0", self.pg0.remote_ip4, 100)) |
| 433 | |
| 434 | fib_dump = self.vapi.ip_fib_dump() |
| 435 | self.verify_route_dump(fib_dump, self.configured_routes) |
| 436 | |
| 437 | self.stream_1 = self.create_stream( |
| 438 | self.pg1, self.pg0, self.configured_routes, 300) |
| 439 | self.stream_2 = self.create_stream( |
| 440 | self.pg2, self.pg0, self.configured_routes, 300) |
| 441 | self.stream_3 = self.create_stream( |
| 442 | self.pg1, self.pg0, self.deleted_routes, 100) |
| 443 | self.stream_4 = self.create_stream( |
| 444 | self.pg2, self.pg0, self.deleted_routes, 100) |
| 445 | |
| 446 | self.pg1.add_stream(self.stream_1 + self.stream_3) |
| 447 | self.pg2.add_stream(self.stream_2 + self.stream_4) |
| 448 | self.pg_enable_capture(self.pg_interfaces) |
| 449 | self.pg_start() |
| 450 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 451 | pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2)) |
Matej Klotton | 16a14cd | 2016-12-07 15:09:13 +0100 | [diff] [blame] | 452 | self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2) |
| 453 | |
| 454 | def test_4_del_routes(self): |
| 455 | """ Delete 1.5k routes |
| 456 | |
| 457 | - delete 5 routes check with traffic script. |
| 458 | - add 100 routes check with traffic script. |
| 459 | """ |
| 460 | self.deleted_routes.extend(self.unconfig_fib_many_to_one( |
| 461 | "10.0.0.0", self.pg0.remote_ip4, 15)) |
| 462 | self.deleted_routes.extend(self.unconfig_fib_many_to_one( |
| 463 | "10.0.0.20", self.pg0.remote_ip4, 85)) |
| 464 | self.deleted_routes.extend(self.unconfig_fib_many_to_one( |
| 465 | "10.0.1.0", self.pg0.remote_ip4, 100)) |
| 466 | fib_dump = self.vapi.ip_fib_dump() |
| 467 | self.verify_not_in_route_dump(fib_dump, self.deleted_routes) |
| 468 | |
| 469 | |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 470 | class TestIPNull(VppTestCase): |
| 471 | """ IPv4 routes via NULL """ |
| 472 | |
| 473 | def setUp(self): |
| 474 | super(TestIPNull, self).setUp() |
| 475 | |
| 476 | # create 2 pg interfaces |
| 477 | self.create_pg_interfaces(range(1)) |
| 478 | |
| 479 | for i in self.pg_interfaces: |
| 480 | i.admin_up() |
| 481 | i.config_ip4() |
| 482 | i.resolve_arp() |
| 483 | |
| 484 | def tearDown(self): |
| 485 | super(TestIPNull, self).tearDown() |
| 486 | for i in self.pg_interfaces: |
| 487 | i.unconfig_ip4() |
| 488 | i.admin_down() |
| 489 | |
| 490 | def test_ip_null(self): |
| 491 | """ IP NULL route """ |
| 492 | |
| 493 | # |
| 494 | # A route via IP NULL that will reply with ICMP unreachables |
| 495 | # |
| 496 | ip_unreach = VppIpRoute(self, "10.0.0.1", 32, [], is_unreach=1) |
| 497 | ip_unreach.add_vpp_config() |
| 498 | |
| 499 | p_unreach = (Ether(src=self.pg0.remote_mac, |
| 500 | dst=self.pg0.local_mac) / |
| 501 | IP(src=self.pg0.remote_ip4, dst="10.0.0.1") / |
| 502 | UDP(sport=1234, dport=1234) / |
| 503 | Raw('\xa5' * 100)) |
| 504 | |
| 505 | self.pg0.add_stream(p_unreach) |
| 506 | self.pg_enable_capture(self.pg_interfaces) |
| 507 | self.pg_start() |
| 508 | |
| 509 | rx = self.pg0.get_capture(1) |
| 510 | rx = rx[0] |
| 511 | icmp = rx[ICMP] |
| 512 | |
| 513 | self.assertEqual(icmptypes[icmp.type], "dest-unreach") |
| 514 | self.assertEqual(icmpcodes[icmp.type][icmp.code], "host-unreachable") |
| 515 | self.assertEqual(icmp.src, self.pg0.remote_ip4) |
| 516 | self.assertEqual(icmp.dst, "10.0.0.1") |
| 517 | |
| 518 | # |
| 519 | # ICMP replies are rate limited. so sit and spin. |
| 520 | # |
| 521 | self.sleep(1) |
| 522 | |
| 523 | # |
| 524 | # A route via IP NULL that will reply with ICMP prohibited |
| 525 | # |
| 526 | ip_prohibit = VppIpRoute(self, "10.0.0.2", 32, [], is_prohibit=1) |
| 527 | ip_prohibit.add_vpp_config() |
| 528 | |
| 529 | p_prohibit = (Ether(src=self.pg0.remote_mac, |
| 530 | dst=self.pg0.local_mac) / |
| 531 | IP(src=self.pg0.remote_ip4, dst="10.0.0.2") / |
| 532 | UDP(sport=1234, dport=1234) / |
| 533 | Raw('\xa5' * 100)) |
| 534 | |
| 535 | self.pg0.add_stream(p_prohibit) |
| 536 | self.pg_enable_capture(self.pg_interfaces) |
| 537 | self.pg_start() |
| 538 | |
| 539 | rx = self.pg0.get_capture(1) |
| 540 | |
| 541 | rx = rx[0] |
| 542 | icmp = rx[ICMP] |
| 543 | |
| 544 | self.assertEqual(icmptypes[icmp.type], "dest-unreach") |
| 545 | self.assertEqual(icmpcodes[icmp.type][icmp.code], "host-prohibited") |
| 546 | self.assertEqual(icmp.src, self.pg0.remote_ip4) |
| 547 | self.assertEqual(icmp.dst, "10.0.0.2") |
| 548 | |
| 549 | |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame^] | 550 | class TestIPDisabled(VppTestCase): |
| 551 | """ IPv4 disabled """ |
| 552 | |
| 553 | def setUp(self): |
| 554 | super(TestIPDisabled, self).setUp() |
| 555 | |
| 556 | # create 2 pg interfaces |
| 557 | self.create_pg_interfaces(range(2)) |
| 558 | |
| 559 | # PG0 is IP enalbed |
| 560 | self.pg0.admin_up() |
| 561 | self.pg0.config_ip4() |
| 562 | self.pg0.resolve_arp() |
| 563 | |
| 564 | # PG 1 is not IP enabled |
| 565 | self.pg1.admin_up() |
| 566 | |
| 567 | def tearDown(self): |
| 568 | super(TestIPDisabled, self).tearDown() |
| 569 | for i in self.pg_interfaces: |
| 570 | i.unconfig_ip4() |
| 571 | i.admin_down() |
| 572 | |
| 573 | def send_and_assert_no_replies(self, intf, pkts, remark): |
| 574 | intf.add_stream(pkts) |
| 575 | self.pg_enable_capture(self.pg_interfaces) |
| 576 | self.pg_start() |
| 577 | for i in self.pg_interfaces: |
| 578 | i.get_capture(0) |
| 579 | i.assert_nothing_captured(remark=remark) |
| 580 | |
| 581 | def test_ip_disabled(self): |
| 582 | """ IP Disabled """ |
| 583 | |
| 584 | # |
| 585 | # An (S,G). |
| 586 | # one accepting interface, pg0, 2 forwarding interfaces |
| 587 | # |
| 588 | route_232_1_1_1 = VppIpMRoute( |
| 589 | self, |
| 590 | "0.0.0.0", |
| 591 | "232.1.1.1", 32, |
| 592 | MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE, |
| 593 | [VppMRoutePath(self.pg1.sw_if_index, |
| 594 | MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT), |
| 595 | VppMRoutePath(self.pg0.sw_if_index, |
| 596 | MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)]) |
| 597 | route_232_1_1_1.add_vpp_config() |
| 598 | |
| 599 | pu = (Ether(src=self.pg1.remote_mac, |
| 600 | dst=self.pg1.local_mac) / |
| 601 | IP(src="10.10.10.10", dst=self.pg0.remote_ip4) / |
| 602 | UDP(sport=1234, dport=1234) / |
| 603 | Raw('\xa5' * 100)) |
| 604 | pm = (Ether(src=self.pg1.remote_mac, |
| 605 | dst=self.pg1.local_mac) / |
| 606 | IP(src="10.10.10.10", dst="232.1.1.1") / |
| 607 | UDP(sport=1234, dport=1234) / |
| 608 | Raw('\xa5' * 100)) |
| 609 | |
| 610 | # |
| 611 | # PG1 does not forward IP traffic |
| 612 | # |
| 613 | self.send_and_assert_no_replies(self.pg1, pu, "IP disabled") |
| 614 | self.send_and_assert_no_replies(self.pg1, pm, "IP disabled") |
| 615 | |
| 616 | # |
| 617 | # IP enable PG1 |
| 618 | # |
| 619 | self.pg1.config_ip4() |
| 620 | |
| 621 | # |
| 622 | # Now we get packets through |
| 623 | # |
| 624 | self.pg1.add_stream(pu) |
| 625 | self.pg_enable_capture(self.pg_interfaces) |
| 626 | self.pg_start() |
| 627 | rx = self.pg0.get_capture(1) |
| 628 | |
| 629 | self.pg1.add_stream(pm) |
| 630 | self.pg_enable_capture(self.pg_interfaces) |
| 631 | self.pg_start() |
| 632 | rx = self.pg0.get_capture(1) |
| 633 | |
| 634 | # |
| 635 | # Disable PG1 |
| 636 | # |
| 637 | self.pg1.unconfig_ip4() |
| 638 | |
| 639 | # |
| 640 | # PG1 does not forward IP traffic |
| 641 | # |
| 642 | self.send_and_assert_no_replies(self.pg1, pu, "IP disabled") |
| 643 | self.send_and_assert_no_replies(self.pg1, pm, "IP disabled") |
| 644 | |
| 645 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 646 | if __name__ == '__main__': |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 647 | unittest.main(testRunner=VppTestRunner) |