blob: 76b7fd7f1d93b1aeb528a8e9c902d83ddd4215b8 [file] [log] [blame]
Neale Ranns6197cb72021-06-03 14:43:21 +00001#!/usr/bin/env python3
2
3import unittest
4
5import scapy.compat
6from scapy.packet import Raw
7from scapy.layers.l2 import Ether
8from scapy.layers.inet import IP, UDP
9from scapy.layers.inet6 import IPv6
10
11from framework import VppTestCase, VppTestRunner
12
13
14class TestPgTun(VppTestCase):
15 """ PG Test Case """
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):
44 """ IP[46] Tunnel Mode PG """
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
53 p = (IP(src=self.pg1.remote_ip4, dst=self.pg0.remote_ip4) /
54 UDP(sport=1234, dport=1234) /
55 Raw('0' * 48))
56
57 rxs = self.send_and_expect(self.pg1, p * N_PKTS, self.pg0)
58 for rx in rxs:
59 self.assertEqual(rx[Ether].dst, self.pg0.remote_mac)
60 self.assertEqual(rx[IP].dst, self.pg0.remote_ip4)
61
62 # v6 tun to ethernet
63 p = (IPv6(src=self.pg2.remote_ip6, dst=self.pg0.remote_ip6) /
64 UDP(sport=1234, dport=1234) /
65 Raw('0' * 48))
66
67 rxs = self.send_and_expect(self.pg2, p * N_PKTS, self.pg0)
68 for rx in rxs:
69 self.assertEqual(rx[Ether].dst, self.pg0.remote_mac)
70 self.assertEqual(rx[IPv6].dst, self.pg0.remote_ip6)
71
72 # eth to v4 tun
73 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
74 IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
75 UDP(sport=1234, dport=1234) /
76 Raw('0' * 48))
77
78 rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg1)
79 for rx in rxs:
80 rx = IP(rx)
81 self.assertFalse(rx.haslayer(Ether))
82 self.assertEqual(rx[IP].dst, self.pg1.remote_ip4)
83
84 # eth to v6 tun
85 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
86 IPv6(src=self.pg0.remote_ip6, dst=self.pg2.remote_ip6) /
87 UDP(sport=1234, dport=1234) /
88 Raw('0' * 48))
89
90 rxs = self.send_and_expect(self.pg0, p * N_PKTS, self.pg2)
91 for rx in rxs:
92 rx = IPv6(rx)
93 self.assertFalse(rx.haslayer(Ether))
94 self.assertEqual(rx[IPv6].dst, self.pg2.remote_ip6)
95
96
97if __name__ == '__main__':
98 unittest.main(testRunner=VppTestRunner)