Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Neale Ranns | 59fa121 | 2019-05-22 13:26:39 +0000 | [diff] [blame] | 2 | |
| 3 | from socket import inet_pton, inet_ntop, AF_INET, AF_INET6 |
| 4 | import unittest |
| 5 | |
| 6 | from framework import VppTestCase, VppTestRunner |
| 7 | from vpp_ip import DpoProto |
| 8 | from vpp_ip_route import VppIpRoute, VppRoutePath, VppMplsLabel, VppIpTable |
| 9 | |
| 10 | from scapy.packet import Raw |
| 11 | from scapy.layers.l2 import Ether |
| 12 | from scapy.layers.inet import IP, UDP |
| 13 | from scapy.layers.inet6 import IPv6 |
| 14 | |
| 15 | from vpp_object import VppObject |
| 16 | |
| 17 | NUM_PKTS = 67 |
| 18 | |
| 19 | |
| 20 | def find_l3xc(test, sw_if_index, dump_sw_if_index=None): |
| 21 | if not dump_sw_if_index: |
| 22 | dump_sw_if_index = sw_if_index |
| 23 | xcs = test.vapi.l3xc_dump(dump_sw_if_index) |
| 24 | for xc in xcs: |
| 25 | if sw_if_index == xc.l3xc.sw_if_index: |
| 26 | return True |
| 27 | return False |
| 28 | |
| 29 | |
| 30 | class VppL3xc(VppObject): |
| 31 | |
| 32 | def __init__(self, test, intf, paths, is_ip6=False): |
| 33 | self._test = test |
| 34 | self.intf = intf |
| 35 | self.is_ip6 = is_ip6 |
| 36 | self.paths = paths |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 37 | self.encoded_paths = [] |
| 38 | for path in self.paths: |
| 39 | self.encoded_paths.append(path.encode()) |
Neale Ranns | 59fa121 | 2019-05-22 13:26:39 +0000 | [diff] [blame] | 40 | |
| 41 | def add_vpp_config(self): |
| 42 | self._test.vapi.l3xc_update( |
| 43 | l3xc={ |
| 44 | 'is_ip6': self.is_ip6, |
| 45 | 'sw_if_index': self.intf.sw_if_index, |
| 46 | 'n_paths': len(self.paths), |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 47 | 'paths': self.encoded_paths |
Neale Ranns | 59fa121 | 2019-05-22 13:26:39 +0000 | [diff] [blame] | 48 | }) |
| 49 | self._test.registry.register(self, self._test.logger) |
| 50 | |
| 51 | def remove_vpp_config(self): |
| 52 | self._test.vapi.l3xc_del( |
| 53 | is_ip6=self.is_ip6, |
| 54 | sw_if_index=self.intf.sw_if_index) |
| 55 | |
| 56 | def query_vpp_config(self): |
| 57 | return find_l3xc(self._test, self.intf.sw_if_index) |
| 58 | |
| 59 | def object_id(self): |
| 60 | return ("l3xc-%d" % self.intf.sw_if_index) |
| 61 | |
| 62 | |
| 63 | class TestL3xc(VppTestCase): |
| 64 | """ L3XC Test Case """ |
| 65 | |
| 66 | @classmethod |
| 67 | def setUpClass(cls): |
| 68 | super(TestL3xc, cls).setUpClass() |
| 69 | |
| 70 | @classmethod |
| 71 | def tearDownClass(cls): |
| 72 | super(TestL3xc, cls).tearDownClass() |
| 73 | |
| 74 | def setUp(self): |
| 75 | super(TestL3xc, self).setUp() |
| 76 | |
| 77 | self.create_pg_interfaces(range(6)) |
| 78 | |
| 79 | for i in self.pg_interfaces: |
| 80 | i.admin_up() |
| 81 | i.config_ip4() |
| 82 | i.resolve_arp() |
| 83 | i.config_ip6() |
| 84 | i.resolve_ndp() |
| 85 | |
| 86 | def tearDown(self): |
| 87 | for i in self.pg_interfaces: |
| 88 | i.unconfig_ip4() |
| 89 | i.unconfig_ip6() |
Neale Ranns | 59fa121 | 2019-05-22 13:26:39 +0000 | [diff] [blame] | 90 | i.admin_down() |
| 91 | super(TestL3xc, self).tearDown() |
| 92 | |
| 93 | def send_and_expect_load_balancing(self, input, pkts, outputs): |
| 94 | self.pg_send(input, pkts) |
| 95 | rxs = [] |
| 96 | for oo in outputs: |
| 97 | rx = oo._get_capture(1) |
| 98 | self.assertNotEqual(0, len(rx)) |
| 99 | for r in rx: |
| 100 | rxs.append(r) |
| 101 | return rxs |
| 102 | |
| 103 | def test_l3xc4(self): |
| 104 | """ IPv4 X-Connect """ |
| 105 | |
| 106 | # |
| 107 | # x-connect pg0 to pg1 and pg2 to pg3->5 |
| 108 | # |
| 109 | l3xc_1 = VppL3xc(self, self.pg0, |
| 110 | [VppRoutePath(self.pg1.remote_ip4, |
| 111 | self.pg1.sw_if_index)]) |
| 112 | l3xc_1.add_vpp_config() |
| 113 | l3xc_2 = VppL3xc(self, self.pg2, |
| 114 | [VppRoutePath(self.pg3.remote_ip4, |
| 115 | self.pg3.sw_if_index), |
| 116 | VppRoutePath(self.pg4.remote_ip4, |
| 117 | self.pg4.sw_if_index), |
| 118 | VppRoutePath(self.pg5.remote_ip4, |
| 119 | self.pg5.sw_if_index)]) |
| 120 | l3xc_2.add_vpp_config() |
| 121 | |
| 122 | self.assertTrue(find_l3xc(self, self.pg2.sw_if_index, 0xffffffff)) |
| 123 | |
| 124 | self.logger.info(self.vapi.cli("sh l3xc")) |
| 125 | |
| 126 | # |
| 127 | # fire in packets. If it's forwarded then the L3XC was successful, |
| 128 | # since default routing will drop it |
| 129 | # |
| 130 | p_1 = (Ether(src=self.pg0.remote_mac, |
| 131 | dst=self.pg0.local_mac) / |
| 132 | IP(src="1.1.1.1", dst="1.1.1.2") / |
| 133 | UDP(sport=1234, dport=1234) / |
Ole Troan | 770a0de | 2019-11-07 13:52:21 +0100 | [diff] [blame] | 134 | Raw(b'\xa5' * 100)) |
Neale Ranns | 59fa121 | 2019-05-22 13:26:39 +0000 | [diff] [blame] | 135 | # self.send_and_expect(self.pg0, p_1*NUM_PKTS, self.pg1) |
| 136 | |
| 137 | p_2 = [] |
| 138 | for ii in range(NUM_PKTS): |
| 139 | p_2.append(Ether(src=self.pg0.remote_mac, |
| 140 | dst=self.pg0.local_mac) / |
| 141 | IP(src="1.1.1.1", dst="1.1.1.2") / |
| 142 | UDP(sport=1000 + ii, dport=1234) / |
Ole Troan | 770a0de | 2019-11-07 13:52:21 +0100 | [diff] [blame] | 143 | Raw(b'\xa5' * 100)) |
Neale Ranns | 59fa121 | 2019-05-22 13:26:39 +0000 | [diff] [blame] | 144 | self.send_and_expect_load_balancing(self.pg2, p_2, |
| 145 | [self.pg3, self.pg4, self.pg5]) |
| 146 | |
| 147 | l3xc_2.remove_vpp_config() |
| 148 | self.send_and_assert_no_replies(self.pg2, p_2) |
| 149 | |
| 150 | |
| 151 | if __name__ == '__main__': |
| 152 | unittest.main(testRunner=VppTestRunner) |