blob: 9bd9a4588ea62e94052c595eea79f4282e535f38 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
Matej Klotton16a14cd2016-12-07 15:09:13 +01002import random
Klement Sekeraf62ae122016-10-11 11:47:09 +02003import socket
Matej Klotton16a14cd2016-12-07 15:09:13 +01004import unittest
Klement Sekeraf62ae122016-10-11 11:47:09 +02005
Damjan Marionf56b77a2016-10-03 19:44:57 +02006from framework import VppTestCase, VppTestRunner
Klement Sekeraf62ae122016-10-11 11:47:09 +02007from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint
Damjan Marionf56b77a2016-10-03 19:44:57 +02008
9from scapy.packet import Raw
Matej Klotton86d87c42016-11-11 11:38:55 +010010from scapy.layers.l2 import Ether, Dot1Q
Damjan Marionf56b77a2016-10-03 19:44:57 +020011from scapy.layers.inet import IP, 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 TestIPv4(VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020016 """ IPv4 Test Case """
17
Klement Sekeraf62ae122016-10-11 11:47:09 +020018 def setUp(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010019 """
20 Perform test setup before test case.
21
22 **Config:**
23 - create 3 pg interfaces
24 - untagged pg0 interface
25 - Dot1Q subinterface on pg1
26 - Dot1AD subinterface on pg2
27 - setup interfaces:
28 - put it into UP state
29 - set IPv4 addresses
30 - resolve neighbor address using ARP
31 - configure 200 fib entries
32
33 :ivar list interfaces: pg interfaces and subinterfaces.
34 :ivar dict flows: IPv4 packet flows in test.
35 :ivar list pg_if_packet_sizes: packet sizes in test.
36 """
Klement Sekeraf62ae122016-10-11 11:47:09 +020037 super(TestIPv4, self).setUp()
Damjan Marionf56b77a2016-10-03 19:44:57 +020038
Klement Sekeraf62ae122016-10-11 11:47:09 +020039 # create 3 pg interfaces
40 self.create_pg_interfaces(range(3))
Damjan Marionf56b77a2016-10-03 19:44:57 +020041
Klement Sekeraf62ae122016-10-11 11:47:09 +020042 # create 2 subinterfaces for pg1 and pg2
43 self.sub_interfaces = [
44 VppDot1QSubint(self, self.pg1, 100),
45 VppDot1ADSubint(self, self.pg2, 200, 300, 400)]
Damjan Marionf56b77a2016-10-03 19:44:57 +020046
Klement Sekeraf62ae122016-10-11 11:47:09 +020047 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
48 self.flows = dict()
49 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
50 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
51 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
Damjan Marionf56b77a2016-10-03 19:44:57 +020052
Klement Sekeraf62ae122016-10-11 11:47:09 +020053 # packet sizes
54 self.pg_if_packet_sizes = [64, 512, 1518, 9018]
55 self.sub_if_packet_sizes = [64, 512, 1518 + 4, 9018 + 4]
Damjan Marionf56b77a2016-10-03 19:44:57 +020056
Klement Sekeraf62ae122016-10-11 11:47:09 +020057 self.interfaces = list(self.pg_interfaces)
58 self.interfaces.extend(self.sub_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +020059
Klement Sekeraf62ae122016-10-11 11:47:09 +020060 # setup all interfaces
61 for i in self.interfaces:
62 i.admin_up()
63 i.config_ip4()
64 i.resolve_arp()
65
Matej Klotton86d87c42016-11-11 11:38:55 +010066 # config 2M FIB entries
Klement Sekeraf62ae122016-10-11 11:47:09 +020067 self.config_fib_entries(200)
Damjan Marionf56b77a2016-10-03 19:44:57 +020068
69 def tearDown(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010070 """Run standard test teardown and log ``show ip arp``."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020071 super(TestIPv4, self).tearDown()
72 if not self.vpp_dead:
Matej Klotton86d87c42016-11-11 11:38:55 +010073 self.logger.info(self.vapi.cli("show ip arp"))
Klement Sekeraf62ae122016-10-11 11:47:09 +020074 # info(self.vapi.cli("show ip fib")) # many entries
Damjan Marionf56b77a2016-10-03 19:44:57 +020075
Klement Sekeraf62ae122016-10-11 11:47:09 +020076 def config_fib_entries(self, count):
Matej Klotton86d87c42016-11-11 11:38:55 +010077 """For each interface add to the FIB table *count* routes to
78 "10.0.0.1/32" destination with interface's local address as next-hop
79 address.
80
81 :param int count: Number of FIB entries.
82
83 - *TODO:* check if the next-hop address shouldn't be remote address
84 instead of local address.
85 """
Klement Sekeraf62ae122016-10-11 11:47:09 +020086 n_int = len(self.interfaces)
87 percent = 0
88 counter = 0.0
89 dest_addr = socket.inet_pton(socket.AF_INET, "10.0.0.1")
90 dest_addr_len = 32
91 for i in self.interfaces:
92 next_hop_address = i.local_ip4n
93 for j in range(count / n_int):
94 self.vapi.ip_add_del_route(
95 dest_addr, dest_addr_len, next_hop_address)
Matej Klotton86d87c42016-11-11 11:38:55 +010096 counter += 1
Klement Sekeraf62ae122016-10-11 11:47:09 +020097 if counter / count * 100 > percent:
Matej Klotton86d87c42016-11-11 11:38:55 +010098 self.logger.info("Configure %d FIB entries .. %d%% done" %
99 (count, percent))
100 percent += 1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200101
Klement Sekeraf62ae122016-10-11 11:47:09 +0200102 def create_stream(self, src_if, packet_sizes):
Matej Klotton86d87c42016-11-11 11:38:55 +0100103 """Create input packet stream for defined interface.
104
105 :param VppInterface src_if: Interface to create packet stream for.
106 :param list packet_sizes: Required packet sizes.
107 """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200108 pkts = []
109 for i in range(0, 257):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200110 dst_if = self.flows[src_if][i % 2]
Klement Sekeradab231a2016-12-21 08:50:14 +0100111 info = self.create_packet_info(src_if, dst_if)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200112 payload = self.info_to_payload(info)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200113 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
114 IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4) /
Damjan Marionf56b77a2016-10-03 19:44:57 +0200115 UDP(sport=1234, dport=1234) /
116 Raw(payload))
117 info.data = p.copy()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200118 if isinstance(src_if, VppSubInterface):
119 p = src_if.add_dot1_layer(p)
120 size = packet_sizes[(i // 2) % len(packet_sizes)]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200121 self.extend_packet(p, size)
122 pkts.append(p)
123 return pkts
124
Klement Sekeraf62ae122016-10-11 11:47:09 +0200125 def verify_capture(self, dst_if, capture):
Matej Klotton86d87c42016-11-11 11:38:55 +0100126 """Verify captured input packet stream for defined interface.
127
128 :param VppInterface dst_if: Interface to verify captured packet stream
129 for.
130 :param list capture: Captured packet stream.
131 """
132 self.logger.info("Verifying capture on interface %s" % dst_if.name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200133 last_info = dict()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200134 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200135 last_info[i.sw_if_index] = None
136 is_sub_if = False
137 dst_sw_if_index = dst_if.sw_if_index
138 if hasattr(dst_if, 'parent'):
139 is_sub_if = True
Damjan Marionf56b77a2016-10-03 19:44:57 +0200140 for packet in capture:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200141 if is_sub_if:
142 # Check VLAN tags and Ethernet header
143 packet = dst_if.remove_dot1_layer(packet)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200144 self.assertTrue(Dot1Q not in packet)
145 try:
146 ip = packet[IP]
147 udp = packet[UDP]
148 payload_info = self.payload_to_info(str(packet[Raw]))
149 packet_index = payload_info.index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200150 self.assertEqual(payload_info.dst, dst_sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100151 self.logger.debug(
152 "Got packet on port %s: src=%u (id=%u)" %
153 (dst_if.name, payload_info.src, packet_index))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200154 next_info = self.get_next_packet_info_for_interface2(
155 payload_info.src, dst_sw_if_index,
156 last_info[payload_info.src])
157 last_info[payload_info.src] = next_info
Damjan Marionf56b77a2016-10-03 19:44:57 +0200158 self.assertTrue(next_info is not None)
159 self.assertEqual(packet_index, next_info.index)
160 saved_packet = next_info.data
161 # Check standard fields
162 self.assertEqual(ip.src, saved_packet[IP].src)
163 self.assertEqual(ip.dst, saved_packet[IP].dst)
164 self.assertEqual(udp.sport, saved_packet[UDP].sport)
165 self.assertEqual(udp.dport, saved_packet[UDP].dport)
166 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100167 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200168 raise
169 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200170 remaining_packet = self.get_next_packet_info_for_interface2(
171 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
Klement Sekera7bb873a2016-11-18 07:38:42 +0100172 self.assertTrue(remaining_packet is None,
173 "Interface %s: Packet expected from interface %s "
174 "didn't arrive" % (dst_if.name, i.name))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200175
176 def test_fib(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100177 """ IPv4 FIB test
178
179 Test scenario:
180
181 - Create IPv4 stream for pg0 interface
182 - Create IPv4 tagged streams for pg1's and pg2's subinterface.
183 - Send and verify received packets on each interface.
184 """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200185
Klement Sekeraf62ae122016-10-11 11:47:09 +0200186 pkts = self.create_stream(self.pg0, self.pg_if_packet_sizes)
187 self.pg0.add_stream(pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200188
Klement Sekeraf62ae122016-10-11 11:47:09 +0200189 for i in self.sub_interfaces:
190 pkts = self.create_stream(i, self.sub_if_packet_sizes)
191 i.parent.add_stream(pkts)
192
193 self.pg_enable_capture(self.pg_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200194 self.pg_start()
195
Klement Sekeraf62ae122016-10-11 11:47:09 +0200196 pkts = self.pg0.get_capture()
197 self.verify_capture(self.pg0, pkts)
198
199 for i in self.sub_interfaces:
200 pkts = i.parent.get_capture()
201 self.verify_capture(i, pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200202
203
Matej Klotton16a14cd2016-12-07 15:09:13 +0100204class TestIPv4FibCrud(VppTestCase):
205 """ FIB - add/update/delete - ip4 routes
206
207 Test scenario:
208 - add 1k,
209 - del 100,
210 - add new 1k,
211 - del 1.5k
212
Klement Sekerada505f62017-01-04 12:58:53 +0100213 ..note:: Python API is too slow to add many routes, needs replacement.
Matej Klotton16a14cd2016-12-07 15:09:13 +0100214 """
215
216 def config_fib_many_to_one(self, start_dest_addr, next_hop_addr, count):
217 """
218
219 :param start_dest_addr:
220 :param next_hop_addr:
221 :param count:
222 :return list: added ips with 32 prefix
223 """
224 added_ips = []
Klement Sekerada505f62017-01-04 12:58:53 +0100225 dest_addr = int(socket.inet_pton(socket.AF_INET,
226 start_dest_addr).encode('hex'),
227 16)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100228 dest_addr_len = 32
229 n_next_hop_addr = socket.inet_pton(socket.AF_INET, next_hop_addr)
230 for _ in range(count):
231 n_dest_addr = '{:08x}'.format(dest_addr).decode('hex')
232 self.vapi.ip_add_del_route(n_dest_addr, dest_addr_len,
233 n_next_hop_addr)
234 added_ips.append(socket.inet_ntoa(n_dest_addr))
235 dest_addr += 1
236 return added_ips
237
238 def unconfig_fib_many_to_one(self, start_dest_addr, next_hop_addr, count):
239
240 removed_ips = []
Klement Sekerada505f62017-01-04 12:58:53 +0100241 dest_addr = int(socket.inet_pton(socket.AF_INET,
242 start_dest_addr).encode('hex'),
243 16)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100244 dest_addr_len = 32
245 n_next_hop_addr = socket.inet_pton(socket.AF_INET, next_hop_addr)
246 for _ in range(count):
247 n_dest_addr = '{:08x}'.format(dest_addr).decode('hex')
248 self.vapi.ip_add_del_route(n_dest_addr, dest_addr_len,
249 n_next_hop_addr, is_add=0)
250 removed_ips.append(socket.inet_ntoa(n_dest_addr))
251 dest_addr += 1
252 return removed_ips
253
254 def create_stream(self, src_if, dst_if, dst_ips, count):
255 pkts = []
256
257 for _ in range(count):
258 dst_addr = random.choice(dst_ips)
Klement Sekeradab231a2016-12-21 08:50:14 +0100259 info = self.create_packet_info(src_if, dst_if)
Matej Klotton16a14cd2016-12-07 15:09:13 +0100260 payload = self.info_to_payload(info)
261 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
262 IP(src=src_if.remote_ip4, dst=dst_addr) /
263 UDP(sport=1234, dport=1234) /
264 Raw(payload))
265 info.data = p.copy()
Matej Klotton16a14cd2016-12-07 15:09:13 +0100266 self.extend_packet(p, random.choice(self.pg_if_packet_sizes))
267 pkts.append(p)
268
269 return pkts
270
271 def _find_ip_match(self, find_in, pkt):
272 for p in find_in:
Klement Sekeradab231a2016-12-21 08:50:14 +0100273 if self.payload_to_info(str(p[Raw])) == \
274 self.payload_to_info(str(pkt[Raw])):
Matej Klotton16a14cd2016-12-07 15:09:13 +0100275 if p[IP].src != pkt[IP].src:
276 break
277 if p[IP].dst != pkt[IP].dst:
278 break
279 if p[UDP].sport != pkt[UDP].sport:
280 break
281 if p[UDP].dport != pkt[UDP].dport:
282 break
283 return p
284 return None
285
286 @staticmethod
287 def _match_route_detail(route_detail, ip, address_length=32, table_id=0):
288 if route_detail.address == socket.inet_pton(socket.AF_INET, ip):
289 if route_detail.table_id != table_id:
290 return False
291 elif route_detail.address_length != address_length:
292 return False
293 else:
294 return True
295 else:
296 return False
297
298 def verify_capture(self, dst_interface, received_pkts, expected_pkts):
299 self.assertEqual(len(received_pkts), len(expected_pkts))
300 to_verify = list(expected_pkts)
301 for p in received_pkts:
302 self.assertEqual(p.src, dst_interface.local_mac)
303 self.assertEqual(p.dst, dst_interface.remote_mac)
304 x = self._find_ip_match(to_verify, p)
305 to_verify.remove(x)
306 self.assertListEqual(to_verify, [])
307
308 def verify_route_dump(self, fib_dump, ips):
309
310 def _ip_in_route_dump(ip, fib_dump):
311 return next((route for route in fib_dump
312 if self._match_route_detail(route, ip)),
313 False)
314
315 for ip in ips:
316 self.assertTrue(_ip_in_route_dump(ip, fib_dump),
317 'IP {} is not in fib dump.'.format(ip))
318
319 def verify_not_in_route_dump(self, fib_dump, ips):
320
321 def _ip_in_route_dump(ip, fib_dump):
322 return next((route for route in fib_dump
323 if self._match_route_detail(route, ip)),
324 False)
325
326 for ip in ips:
327 self.assertFalse(_ip_in_route_dump(ip, fib_dump),
328 'IP {} is in fib dump.'.format(ip))
329
330 @classmethod
331 def setUpClass(cls):
332 """
333 #. Create and initialize 3 pg interfaces.
334 #. initialize class attributes configured_routes and deleted_routes
335 to store information between tests.
336 """
337 super(TestIPv4FibCrud, cls).setUpClass()
338
339 try:
340 # create 3 pg interfaces
341 cls.create_pg_interfaces(range(3))
342
343 cls.interfaces = list(cls.pg_interfaces)
344
345 # setup all interfaces
346 for i in cls.interfaces:
347 i.admin_up()
348 i.config_ip4()
349 i.resolve_arp()
350
351 cls.configured_routes = []
352 cls.deleted_routes = []
353 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
354
355 except Exception:
356 super(TestIPv4FibCrud, cls).tearDownClass()
357 raise
358
359 def setUp(self):
360 super(TestIPv4FibCrud, self).setUp()
Klement Sekeradab231a2016-12-21 08:50:14 +0100361 self.reset_packet_infos()
Matej Klotton16a14cd2016-12-07 15:09:13 +0100362
363 def test_1_add_routes(self):
364 """ Add 1k routes
365
366 - add 100 routes check with traffic script.
367 """
368 # config 1M FIB entries
369 self.configured_routes.extend(self.config_fib_many_to_one(
370 "10.0.0.0", self.pg0.remote_ip4, 100))
371
372 fib_dump = self.vapi.ip_fib_dump()
373 self.verify_route_dump(fib_dump, self.configured_routes)
374
375 self.stream_1 = self.create_stream(
376 self.pg1, self.pg0, self.configured_routes, 100)
377 self.stream_2 = self.create_stream(
378 self.pg2, self.pg0, self.configured_routes, 100)
379 self.pg1.add_stream(self.stream_1)
380 self.pg2.add_stream(self.stream_2)
381
382 self.pg_enable_capture(self.pg_interfaces)
383 self.pg_start()
384
Klement Sekeradab231a2016-12-21 08:50:14 +0100385 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100386 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
387
Matej Klotton16a14cd2016-12-07 15:09:13 +0100388 def test_2_del_routes(self):
389 """ Delete 100 routes
390
391 - delete 10 routes check with traffic script.
392 """
393 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
394 "10.0.0.10", self.pg0.remote_ip4, 10))
395 for x in self.deleted_routes:
396 self.configured_routes.remove(x)
397
398 fib_dump = self.vapi.ip_fib_dump()
399 self.verify_route_dump(fib_dump, self.configured_routes)
400
401 self.stream_1 = self.create_stream(
402 self.pg1, self.pg0, self.configured_routes, 100)
403 self.stream_2 = self.create_stream(
404 self.pg2, self.pg0, self.configured_routes, 100)
405 self.stream_3 = self.create_stream(
406 self.pg1, self.pg0, self.deleted_routes, 100)
407 self.stream_4 = self.create_stream(
408 self.pg2, self.pg0, self.deleted_routes, 100)
409 self.pg1.add_stream(self.stream_1 + self.stream_3)
410 self.pg2.add_stream(self.stream_2 + self.stream_4)
411 self.pg_enable_capture(self.pg_interfaces)
412 self.pg_start()
413
Klement Sekeradab231a2016-12-21 08:50:14 +0100414 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100415 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
416
417 def test_3_add_new_routes(self):
418 """ Add 1k routes
419
420 - re-add 5 routes check with traffic script.
421 - add 100 routes check with traffic script.
422 """
423 tmp = self.config_fib_many_to_one(
424 "10.0.0.10", self.pg0.remote_ip4, 5)
425 self.configured_routes.extend(tmp)
426 for x in tmp:
427 self.deleted_routes.remove(x)
428
429 self.configured_routes.extend(self.config_fib_many_to_one(
430 "10.0.1.0", self.pg0.remote_ip4, 100))
431
432 fib_dump = self.vapi.ip_fib_dump()
433 self.verify_route_dump(fib_dump, self.configured_routes)
434
435 self.stream_1 = self.create_stream(
436 self.pg1, self.pg0, self.configured_routes, 300)
437 self.stream_2 = self.create_stream(
438 self.pg2, self.pg0, self.configured_routes, 300)
439 self.stream_3 = self.create_stream(
440 self.pg1, self.pg0, self.deleted_routes, 100)
441 self.stream_4 = self.create_stream(
442 self.pg2, self.pg0, self.deleted_routes, 100)
443
444 self.pg1.add_stream(self.stream_1 + self.stream_3)
445 self.pg2.add_stream(self.stream_2 + self.stream_4)
446 self.pg_enable_capture(self.pg_interfaces)
447 self.pg_start()
448
Klement Sekeradab231a2016-12-21 08:50:14 +0100449 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
Matej Klotton16a14cd2016-12-07 15:09:13 +0100450 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
451
452 def test_4_del_routes(self):
453 """ Delete 1.5k routes
454
455 - delete 5 routes check with traffic script.
456 - add 100 routes check with traffic script.
457 """
458 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
459 "10.0.0.0", self.pg0.remote_ip4, 15))
460 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
461 "10.0.0.20", self.pg0.remote_ip4, 85))
462 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
463 "10.0.1.0", self.pg0.remote_ip4, 100))
464 fib_dump = self.vapi.ip_fib_dump()
465 self.verify_not_in_route_dump(fib_dump, self.deleted_routes)
466
467
Damjan Marionf56b77a2016-10-03 19:44:57 +0200468if __name__ == '__main__':
Klement Sekeraf62ae122016-10-11 11:47:09 +0200469 unittest.main(testRunner=VppTestRunner)