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() |
| 41 | super(TestPgTun, self).tearDown() |
| 42 | |
| 43 | def test_pg_tun(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 44 | """IP[46] Tunnel Mode PG""" |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 45 | |
| 46 | # |
| 47 | # test that we can send and receive IP encap'd packets on the |
| 48 | # tun interfaces |
| 49 | # |
| 50 | N_PKTS = 31 |
| 51 | |
| 52 | # v4 tun to ethernet |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 53 | p = ( |
| 54 | IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) |
| 55 | / UDP(sport=1234, dport=1234) |
| 56 | / Raw("0" * 48) |
| 57 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 58 | |
| 59 | rxs = self.send_and_expect(self.pg1, p * N_PKTS, self.pg0) |
| 60 | for rx in rxs: |
| 61 | self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) |
| 62 | self.assertEqual(rx[IP].dst, self.pg0.remote_ip4) |
| 63 | |
| 64 | # v6 tun to ethernet |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 65 | p = ( |
| 66 | IPv6(src=self.pg2.remote_ip6, dst=self.pg0.remote_ip6) |
| 67 | / UDP(sport=1234, dport=1234) |
| 68 | / Raw("0" * 48) |
| 69 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 70 | |
| 71 | rxs = self.send_and_expect(self.pg2, p * N_PKTS, self.pg0) |
| 72 | for rx in rxs: |
| 73 | self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) |
| 74 | self.assertEqual(rx[IPv6].dst, self.pg0.remote_ip6) |
| 75 | |
| 76 | # eth to v4 tun |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 77 | p = ( |
| 78 | Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) |
| 79 | / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) |
| 80 | / UDP(sport=1234, dport=1234) |
| 81 | / Raw("0" * 48) |
| 82 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 83 | |
| 84 | rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg1) |
| 85 | for rx in rxs: |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 86 | rx = IP(bytes(rx)) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 87 | self.assertFalse(rx.haslayer(Ether)) |
| 88 | self.assertEqual(rx[IP].dst, self.pg1.remote_ip4) |
| 89 | |
| 90 | # eth to v6 tun |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 91 | p = ( |
| 92 | Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) |
| 93 | / IPv6(src=self.pg0.remote_ip6, dst=self.pg2.remote_ip6) |
| 94 | / UDP(sport=1234, dport=1234) |
| 95 | / Raw("0" * 48) |
| 96 | ) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 97 | |
| 98 | rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg2) |
| 99 | for rx in rxs: |
Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 100 | rx = IPv6(bytes(rx)) |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 101 | self.assertFalse(rx.haslayer(Ether)) |
| 102 | self.assertEqual(rx[IPv6].dst, self.pg2.remote_ip6) |
| 103 | |
| 104 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 105 | if __name__ == "__main__": |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 106 | unittest.main(testRunner=VppTestRunner) |