Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
| 4 | |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 5 | from scapy.packet import Raw |
| 6 | from scapy.layers.l2 import Ether |
| 7 | from scapy.layers.inet import IP, UDP |
| 8 | from scapy.layers.inet6 import IPv6 |
| 9 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 10 | from framework import VppTestCase |
| 11 | from asfframework import VppTestRunner |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class TestPgTun(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 15 | """PG Test Case""" |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 16 | |
| 17 | def setUp(self): |
| 18 | super(TestPgTun, self).setUp() |
| 19 | |
| 20 | # create 3 pg interfaces - one each ethernet, ip4-tun, ip6-tun. |
| 21 | self.create_pg_interfaces(range(0, 1)) |
| 22 | self.pg_interfaces += self.create_pg_ip4_interfaces(range(1, 2)) |
| 23 | self.pg_interfaces += self.create_pg_ip6_interfaces(range(2, 3)) |
| 24 | |
| 25 | for i in self.pg_interfaces: |
| 26 | i.admin_up() |
| 27 | |
| 28 | for i in [self.pg0, self.pg1]: |
| 29 | i.config_ip4() |
| 30 | |
| 31 | for i in [self.pg0, self.pg2]: |
| 32 | i.config_ip6() |
| 33 | |
| 34 | self.pg0.resolve_arp() |
| 35 | self.pg0.resolve_ndp() |
| 36 | |
| 37 | def tearDown(self): |
| 38 | for i in self.pg_interfaces: |
| 39 | i.unconfig_ip4() |
| 40 | i.admin_down() |
Mohsin Kazmi | 0b1bd9d | 2024-10-03 23:18:17 +0000 | [diff] [blame] | 41 | i.remove_vpp_config() |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 42 | super(TestPgTun, self).tearDown() |
| 43 | |
| 44 | def test_pg_tun(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 45 | """IP[46] Tunnel Mode PG""" |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 46 | |
| 47 | # |
| 48 | # test that we can send and receive IP encap'd packets on the |
| 49 | # tun interfaces |
| 50 | # |
| 51 | N_PKTS = 31 |
| 52 | |
| 53 | # v4 tun to ethernet |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 54 | p = ( |
| 55 | IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) |
| 56 | / UDP(sport=1234, dport=1234) |
| 57 | / Raw("0" * 48) |
| 58 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 59 | |
| 60 | rxs = self.send_and_expect(self.pg1, p * N_PKTS, self.pg0) |
| 61 | for rx in rxs: |
| 62 | self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) |
| 63 | self.assertEqual(rx[IP].dst, self.pg0.remote_ip4) |
| 64 | |
| 65 | # v6 tun to ethernet |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 66 | p = ( |
| 67 | IPv6(src=self.pg2.remote_ip6, dst=self.pg0.remote_ip6) |
| 68 | / UDP(sport=1234, dport=1234) |
| 69 | / Raw("0" * 48) |
| 70 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 71 | |
| 72 | rxs = self.send_and_expect(self.pg2, p * N_PKTS, self.pg0) |
| 73 | for rx in rxs: |
| 74 | self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) |
| 75 | self.assertEqual(rx[IPv6].dst, self.pg0.remote_ip6) |
| 76 | |
| 77 | # eth to v4 tun |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 78 | p = ( |
| 79 | Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) |
| 80 | / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) |
| 81 | / UDP(sport=1234, dport=1234) |
| 82 | / Raw("0" * 48) |
| 83 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 84 | |
| 85 | rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg1) |
| 86 | for rx in rxs: |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 87 | rx = IP(bytes(rx)) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 88 | self.assertFalse(rx.haslayer(Ether)) |
| 89 | self.assertEqual(rx[IP].dst, self.pg1.remote_ip4) |
| 90 | |
| 91 | # eth to v6 tun |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 92 | p = ( |
| 93 | Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) |
| 94 | / IPv6(src=self.pg0.remote_ip6, dst=self.pg2.remote_ip6) |
| 95 | / UDP(sport=1234, dport=1234) |
| 96 | / Raw("0" * 48) |
| 97 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 98 | |
| 99 | rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg2) |
| 100 | for rx in rxs: |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 101 | rx = IPv6(bytes(rx)) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 102 | self.assertFalse(rx.haslayer(Ether)) |
| 103 | self.assertEqual(rx[IPv6].dst, self.pg2.remote_ip6) |
| 104 | |
| 105 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 106 | if __name__ == "__main__": |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 107 | unittest.main(testRunner=VppTestRunner) |