blob: d2e656df58b8fb72858d801c76632a754a2fa728 [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()
41 super(TestPgTun, self).tearDown()
42
43 def test_pg_tun(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020044 """IP[46] Tunnel Mode PG"""
Neale Ranns6197cb72021-06-03 14:43:21 +000045
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 Sekerad9b0c6f2022-04-26 19:02:15 +020053 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 Ranns6197cb72021-06-03 14:43:21 +000058
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 Sekerad9b0c6f2022-04-26 19:02:15 +020065 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 Ranns6197cb72021-06-03 14:43:21 +000070
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 Sekerad9b0c6f2022-04-26 19:02:15 +020077 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 Ranns6197cb72021-06-03 14:43:21 +000083
84 rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg1)
85 for rx in rxs:
86 rx = IP(rx)
87 self.assertFalse(rx.haslayer(Ether))
88 self.assertEqual(rx[IP].dst, self.pg1.remote_ip4)
89
90 # eth to v6 tun
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020091 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 Ranns6197cb72021-06-03 14:43:21 +000097
98 rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg2)
99 for rx in rxs:
100 rx = IPv6(rx)
101 self.assertFalse(rx.haslayer(Ether))
102 self.assertEqual(rx[IPv6].dst, self.pg2.remote_ip6)
103
104
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200105if __name__ == "__main__":
Neale Ranns6197cb72021-06-03 14:43:21 +0000106 unittest.main(testRunner=VppTestRunner)