X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=test%2Ftest_ip_ecmp.py;h=360959baaaa23caf391473b30abacc9fc4e1e79f;hb=853cc9f2ad3ee52cbdd891fb09d51c25678baed0;hp=c6a33c815b879b6333e26b72afa0c712cdee4568;hpb=a5b2eec0535f9025319a752891d77ff9948ae0df;p=vpp.git diff --git a/test/test_ip_ecmp.py b/test/test_ip_ecmp.py index c6a33c815b8..360959baaaa 100644 --- a/test/test_ip_ecmp.py +++ b/test/test_ip_ecmp.py @@ -1,17 +1,18 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import unittest import random -import socket from ipaddress import IPv4Address, IPv6Address, AddressValueError -from framework import VppTestCase, VppTestRunner +from framework import VppTestCase +from asfframework import VppTestRunner from util import ppp from scapy.packet import Raw from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP from scapy.layers.inet6 import IPv6 +from vpp_ip_route import VppIpRoute, VppRoutePath try: text_type = unicode @@ -25,7 +26,7 @@ N_PKTS_IN_STREAM = 300 class TestECMP(VppTestCase): - """ Equal-cost multi-path routing Test Case """ + """Equal-cost multi-path routing Test Case""" @classmethod def setUpClass(cls): @@ -72,9 +73,10 @@ class TestECMP(VppTestCase): Show various debug prints after each test. """ super(TestECMP, self).tearDown() - if not self.vpp_dead: - self.logger.info(self.vapi.ppcli("show ip arp")) - self.logger.info(self.vapi.ppcli("show ip6 neighbors")) + + def show_commands_at_teardown(self): + self.logger.info(self.vapi.ppcli("show ip4 neighbors")) + self.logger.info(self.vapi.ppcli("show ip6 neighbors")) def get_ip_address(self, ip_addr_start, ip_prefix_len): """ @@ -90,11 +92,11 @@ class TestECMP(VppTestCase): ip_addr = IPv6Address(text_type(ip_addr_start)) ip_max_len = 128 - return str(ip_addr + - random.randint(0, 2 ** (ip_max_len - ip_prefix_len) - 2)) + return str(ip_addr + random.randint(0, 2 ** (ip_max_len - ip_prefix_len) - 2)) - def create_stream(self, src_if, src_ip_start, dst_ip_start, - ip_prefix_len, packet_sizes, ip_l=IP): + def create_stream( + self, src_if, src_ip_start, dst_ip_start, ip_prefix_len, packet_sizes, ip_l=IP + ): """Create input packet stream for defined interfaces. :param VppInterface src_if: Source Interface for packet stream. @@ -110,10 +112,12 @@ class TestECMP(VppTestCase): payload = self.info_to_payload(info) src_ip = self.get_ip_address(src_ip_start, ip_prefix_len) dst_ip = self.get_ip_address(dst_ip_start, ip_prefix_len) - p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / - ip_l(src=src_ip, dst=dst_ip) / - UDP(sport=1234, dport=1234) / - Raw(payload)) + p = ( + Ether(dst=src_if.local_mac, src=src_if.remote_mac) + / ip_l(src=src_ip, dst=dst_ip) + / UDP(sport=1234, dport=1234) + / Raw(payload) + ) info.data = p.copy() size = random.choice(packet_sizes) self.extend_packet(p, size) @@ -140,12 +144,17 @@ class TestECMP(VppTestCase): payload_info = self.payload_to_info(packet[Raw]) packet_index = payload_info.index ip_sent = self._packet_infos[packet_index].data[ip_l] - self.logger.debug("Got packet on port %s: src=%u (id=%u)" % - (rx_if.name, payload_info.src, packet_index)) + self.logger.debug( + "Got packet on port %s: src=%u (id=%u)" + % (rx_if.name, payload_info.src, packet_index) + ) # Check standard fields - self.assertIn(packet.dst, rx_if._hosts_by_mac, - "Destination MAC address %s shouldn't be routed " - "via interface %s" % (packet.dst, rx_if.name)) + self.assertIn( + packet.dst, + rx_if._hosts_by_mac, + "Destination MAC address %s shouldn't be routed " + "via interface %s" % (packet.dst, rx_if.name), + ) self.assertEqual(packet.src, rx_if.local_mac) self.assertEqual(ip_received.src, ip_sent.src) self.assertEqual(ip_received.dst, ip_sent.dst) @@ -159,13 +168,15 @@ class TestECMP(VppTestCase): # We expect packet routed via all host of pg interface for host_mac in host_counters: nr = host_counters[host_mac] - self.assertNotEqual( - nr, 0, "No packet routed via host %s" % host_mac) - self.logger.info("%u packets routed via host %s of %s interface" % - (nr, host_mac, rx_if.name)) + self.assertNotEqual(nr, 0, "No packet routed via host %s" % host_mac) + self.logger.info( + "%u packets routed via host %s of %s interface" + % (nr, host_mac, rx_if.name) + ) count += nr - self.logger.info("Total amount of %u packets routed via %s interface" % - (count, rx_if.name)) + self.logger.info( + "Total amount of %u packets routed via %s interface" % (count, rx_if.name) + ) return count @@ -177,37 +188,32 @@ class TestECMP(VppTestCase): :param int dst_prefix_len: IP address prefix length. :param int is_ipv6: 0 if an ip4 route, else ip6 """ - af = socket.AF_INET if is_ipv6 == 0 else socket.AF_INET6 - dst_ip = socket.inet_pton(af, dst_ip_net) + paths = [] for pg_if in self.pg_interfaces[1:]: for nh_host in pg_if.remote_hosts: nh_host_ip = nh_host.ip4 if is_ipv6 == 0 else nh_host.ip6 - next_hop_address = socket.inet_pton(af, nh_host_ip) - next_hop_sw_if_index = pg_if.sw_if_index - self.vapi.ip_add_del_route( - dst_address=dst_ip, - dst_address_length=dst_prefix_len, - next_hop_address=next_hop_address, - next_hop_sw_if_index=next_hop_sw_if_index, - is_ipv6=is_ipv6, is_multipath=1) - self.logger.info("Route via %s on %s created" % - (nh_host_ip, pg_if.name)) + paths.append(VppRoutePath(nh_host_ip, pg_if.sw_if_index)) + + rip = VppIpRoute(self, dst_ip_net, dst_prefix_len, paths) + rip.add_vpp_config() + self.logger.info("Route via %s on %s created" % (nh_host_ip, pg_if.name)) self.logger.debug(self.vapi.ppcli("show ip fib")) self.logger.debug(self.vapi.ppcli("show ip6 fib")) def test_ip_ecmp(self): - """ IP equal-cost multi-path routing test """ + """IP equal-cost multi-path routing test""" - src_ip_net = '16.0.0.1' - dst_ip_net = '32.0.0.1' + src_ip_net = "16.0.0.1" + dst_ip_net = "32.0.0.1" ip_prefix_len = 24 self.create_ip_routes(dst_ip_net, ip_prefix_len) - pkts = self.create_stream(self.pg0, src_ip_net, dst_ip_net, - ip_prefix_len, self.pg_if_packet_sizes) + pkts = self.create_stream( + self.pg0, src_ip_net, dst_ip_net, ip_prefix_len, self.pg_if_packet_sizes + ) self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) @@ -218,7 +224,8 @@ class TestECMP(VppTestCase): for pg_if in self.pg_interfaces[1:]: capture = pg_if._get_capture(timeout=1) self.assertNotEqual( - len(capture), 0, msg="No packets captured on %s" % pg_if.name) + len(capture), 0, msg="No packets captured on %s" % pg_if.name + ) rx_count += self.verify_capture(pg_if, capture) self.pg0.assert_nothing_captured(remark="IP packets forwarded on pg0") @@ -226,17 +233,22 @@ class TestECMP(VppTestCase): self.assertEqual(rx_count, len(pkts)) def test_ip6_ecmp(self): - """ IPv6 equal-cost multi-path routing test """ + """IPv6 equal-cost multi-path routing test""" - src_ip_net = '3ffe:51::1' - dst_ip_net = '3ffe:71::1' + src_ip_net = "3ffe:51::1" + dst_ip_net = "3ffe:71::1" ip_prefix_len = 64 self.create_ip_routes(dst_ip_net, ip_prefix_len, is_ipv6=1) pkts = self.create_stream( - self.pg0, src_ip_net, dst_ip_net, - ip_prefix_len, self.pg_if_packet_sizes, ip_l=IPv6) + self.pg0, + src_ip_net, + dst_ip_net, + ip_prefix_len, + self.pg_if_packet_sizes, + ip_l=IPv6, + ) self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) @@ -247,7 +259,8 @@ class TestECMP(VppTestCase): for pg_if in self.pg_interfaces[1:]: capture = pg_if._get_capture(timeout=1) self.assertNotEqual( - len(capture), 0, msg="No packets captured on %s" % pg_if.name) + len(capture), 0, msg="No packets captured on %s" % pg_if.name + ) rx_count += self.verify_capture(pg_if, capture, ip_l=IPv6) self.pg0.assert_nothing_captured(remark="IP packets forwarded on pg0") @@ -255,5 +268,5 @@ class TestECMP(VppTestCase): self.assertEqual(rx_count, len(pkts)) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main(testRunner=VppTestRunner)