blob: 14e149b5bcfcc69cc21cf97e0cba510ab0554e30 [file] [log] [blame]
Neale Ranns6197cb72021-06-03 14:43:21 +00001#!/usr/bin/env python3
2
3import unittest
4
Neale Ranns6197cb72021-06-03 14:43:21 +00005from scapy.packet import Raw
6from scapy.layers.l2 import Ether
7from scapy.layers.inet import IP, UDP
8from scapy.layers.inet6 import IPv6
9
Dave Wallace8800f732023-08-31 00:47:44 -040010from framework import VppTestCase
11from asfframework import VppTestRunner
Neale Ranns6197cb72021-06-03 14:43:21 +000012
13
14class TestPgTun(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020015 """PG Test Case"""
Neale Ranns6197cb72021-06-03 14:43:21 +000016
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 Kazmi0b1bd9d2024-10-03 23:18:17 +000041 i.remove_vpp_config()
Neale Ranns6197cb72021-06-03 14:43:21 +000042 super(TestPgTun, self).tearDown()
43
44 def test_pg_tun(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020045 """IP[46] Tunnel Mode PG"""
Neale Ranns6197cb72021-06-03 14:43:21 +000046
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 Sekerad9b0c6f2022-04-26 19:02:15 +020054 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 Ranns6197cb72021-06-03 14:43:21 +000059
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 Sekerad9b0c6f2022-04-26 19:02:15 +020066 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 Ranns6197cb72021-06-03 14:43:21 +000071
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 Sekerad9b0c6f2022-04-26 19:02:15 +020078 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 Ranns6197cb72021-06-03 14:43:21 +000084
85 rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg1)
86 for rx in rxs:
Dave Wallacecf9356d2024-07-23 01:28:19 -040087 rx = IP(bytes(rx))
Neale Ranns6197cb72021-06-03 14:43:21 +000088 self.assertFalse(rx.haslayer(Ether))
89 self.assertEqual(rx[IP].dst, self.pg1.remote_ip4)
90
91 # eth to v6 tun
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020092 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 Ranns6197cb72021-06-03 14:43:21 +000098
99 rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg2)
100 for rx in rxs:
Dave Wallacecf9356d2024-07-23 01:28:19 -0400101 rx = IPv6(bytes(rx))
Neale Ranns6197cb72021-06-03 14:43:21 +0000102 self.assertFalse(rx.haslayer(Ether))
103 self.assertEqual(rx[IPv6].dst, self.pg2.remote_ip6)
104
105
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200106if __name__ == "__main__":
Neale Ranns6197cb72021-06-03 14:43:21 +0000107 unittest.main(testRunner=VppTestRunner)