blob: 00721ec90a097ab070e54d39c2e5f29de9df0fd5 [file] [log] [blame]
Ole Troan46c1c112018-03-14 20:39:40 +01001#!/usr/bin/env python
2"""IP{4,6} over IP{v,6} tunnel functional tests"""
Ole Troan298c6952018-03-08 12:30:43 +01003
4import unittest
Damjan Marionfe7d4a22018-04-13 19:43:39 +02005from scapy.layers.inet6 import IPv6, Ether, IP, UDP
Ole Troan46c1c112018-03-14 20:39:40 +01006from framework import VppTestCase, VppTestRunner
Ole Troan298c6952018-03-08 12:30:43 +01007from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto
Ole Troan46c1c112018-03-14 20:39:40 +01008from socket import AF_INET, AF_INET6, inet_pton
Ole Troan298c6952018-03-08 12:30:43 +01009
10""" Testipip is a subclass of VPPTestCase classes.
11
12IPIP tests.
13
14"""
15
16
17class TestIPIP(VppTestCase):
18 """ IPIP Test Case """
19
20 @classmethod
21 def setUpClass(cls):
22 super(TestIPIP, cls).setUpClass()
Ole Troan46c1c112018-03-14 20:39:40 +010023 cls.create_pg_interfaces(range(2))
24 cls.interfaces = list(cls.pg_interfaces)
Ole Troan298c6952018-03-08 12:30:43 +010025
26 def setUp(cls):
27 super(TestIPIP, cls).setUp()
Ole Troan46c1c112018-03-14 20:39:40 +010028 for i in cls.interfaces:
29 i.admin_up()
30 i.config_ip4()
31 i.config_ip6()
32 i.disable_ipv6_ra()
33 i.resolve_arp()
34 i.resolve_ndp()
Ole Troan298c6952018-03-08 12:30:43 +010035
36 def tearDown(self):
37 super(TestIPIP, self).tearDown()
38 if not self.vpp_dead:
Ole Troan46c1c112018-03-14 20:39:40 +010039 for i in self.pg_interfaces:
40 i.unconfig_ip4()
41 i.unconfig_ip6()
42 i.admin_down()
Ole Troan298c6952018-03-08 12:30:43 +010043
44 def validate(self, rx, expected):
Ole Troan46c1c112018-03-14 20:39:40 +010045 self.assertEqual(rx, expected.__class__(str(expected)))
Ole Troan298c6952018-03-08 12:30:43 +010046
47 def test_ipip4(self):
48 """ ip{v4,v6} over ip4 test """
49 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
Ole Troand57f6362018-05-24 13:21:43 +020050 p_ip6 = IPv6(src="1::1", dst="DEAD::1", nh='UDP', tc=42)
51 p_ip4 = IP(src="1.2.3.4", dst="130.67.0.1", tos=42)
Damjan Marionfe7d4a22018-04-13 19:43:39 +020052 p_payload = UDP(sport=1234, dport=1234)
Ole Troan298c6952018-03-08 12:30:43 +010053
54 # IPv4 transport
55 rv = self.vapi.ipip_add_tunnel(
Ole Troan46c1c112018-03-14 20:39:40 +010056 src_address=self.pg0.local_ip4n,
57 dst_address=self.pg1.remote_ip4n,
Ole Troand57f6362018-05-24 13:21:43 +020058 is_ipv6=0, tc_tos=0xFF)
Ole Troan298c6952018-03-08 12:30:43 +010059 sw_if_index = rv.sw_if_index
60
61 # Set interface up and enable IP on it
Ole Troan46c1c112018-03-14 20:39:40 +010062 self.vapi.sw_interface_set_flags(sw_if_index, 1)
63 self.vapi.sw_interface_set_unnumbered(
Ole Troan298c6952018-03-08 12:30:43 +010064 ip_sw_if_index=self.pg0.sw_if_index,
65 sw_if_index=sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +010066
67 # Add IPv4 and IPv6 routes via tunnel interface
68 ip4_via_tunnel = VppIpRoute(
69 self, "130.67.0.0", 16,
70 [VppRoutePath("0.0.0.0",
71 sw_if_index,
72 proto=DpoProto.DPO_PROTO_IP4)], is_ip6=0)
73 ip4_via_tunnel.add_vpp_config()
74
75 ip6_via_tunnel = VppIpRoute(
76 self, "dead::", 16,
77 [VppRoutePath("::",
78 sw_if_index,
79 proto=DpoProto.DPO_PROTO_IP6)], is_ip6=1)
80 ip6_via_tunnel.add_vpp_config()
81
82 # IPv6 in to IPv4 tunnel
83 p6 = (p_ether / p_ip6 / p_payload)
84 p_inner_ip6 = p_ip6
85 p_inner_ip6.hlim -= 1
86 p6_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
Ole Troand57f6362018-05-24 13:21:43 +020087 proto='ipv6', id=0, tos=42) / p_inner_ip6 / p_payload)
Ole Troan298c6952018-03-08 12:30:43 +010088 p6_reply.ttl -= 1
89 rx = self.send_and_expect(self.pg0, p6*10, self.pg1)
90 for p in rx:
91 self.validate(p[1], p6_reply)
92
93 # IPv4 in to IPv4 tunnel
94 p4 = (p_ether / p_ip4 / p_payload)
95 p_ip4_inner = p_ip4
96 p_ip4_inner.ttl -= 1
Ole Troand57f6362018-05-24 13:21:43 +020097 p4_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
98 tos=42) /
99 p_ip4_inner / p_payload)
Ole Troan298c6952018-03-08 12:30:43 +0100100 p4_reply.ttl -= 1
101 p4_reply.id = 0
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200102 rx = self.send_and_expect(self.pg0, p4*10, self.pg1)
Ole Troan298c6952018-03-08 12:30:43 +0100103 for p in rx:
104 self.validate(p[1], p4_reply)
105
106 # Decapsulation
107 p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
108
109 # IPv4 tunnel to IPv4
110 p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4)
111 p4 = (p_ether / IP(src=self.pg1.remote_ip4,
112 dst=self.pg0.local_ip4) / p_ip4 / p_payload)
113 p4_reply = (p_ip4 / p_payload)
114 p4_reply.ttl -= 1
115 rx = self.send_and_expect(self.pg1, p4*10, self.pg0)
116 for p in rx:
117 self.validate(p[1], p4_reply)
118
119 # IPv4 tunnel to IPv6
120 p_ip6 = IPv6(src="1:2:3::4", dst=self.pg0.remote_ip6)
121 p6 = (p_ether / IP(src=self.pg1.remote_ip4,
122 dst=self.pg0.local_ip4) / p_ip6 / p_payload)
123 p6_reply = (p_ip6 / p_payload)
124 p6_reply.hlim = 63
125 rx = self.send_and_expect(self.pg1, p6*10, self.pg0)
126 for p in rx:
127 self.validate(p[1], p6_reply)
128
129 def test_ipip6(self):
130 """ ip{v4,v6} over ip6 test """
131 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
Ole Troand57f6362018-05-24 13:21:43 +0200132 p_ip6 = IPv6(src="1::1", dst="DEAD::1", tc=42, nh='UDP')
133 p_ip4 = IP(src="1.2.3.4", dst="130.67.0.1", tos=42)
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200134 p_payload = UDP(sport=1234, dport=1234)
Ole Troan298c6952018-03-08 12:30:43 +0100135
136 # IPv6 transport
137 rv = self.vapi.ipip_add_tunnel(
Ole Troan46c1c112018-03-14 20:39:40 +0100138 src_address=self.pg0.local_ip6n,
Ole Troand57f6362018-05-24 13:21:43 +0200139 dst_address=self.pg1.remote_ip6n, tc_tos=255)
Ole Troan298c6952018-03-08 12:30:43 +0100140
141 sw_if_index = rv.sw_if_index
142
Ole Troan46c1c112018-03-14 20:39:40 +0100143 self.vapi.sw_interface_set_flags(sw_if_index, 1)
144 self.vapi.sw_interface_set_unnumbered(
Ole Troan298c6952018-03-08 12:30:43 +0100145 ip_sw_if_index=self.pg0.sw_if_index, sw_if_index=sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100146
147 # Add IPv4 and IPv6 routes via tunnel interface
148 ip4_via_tunnel = VppIpRoute(
149 self, "130.67.0.0", 16,
150 [VppRoutePath("0.0.0.0",
151 sw_if_index,
152 proto=DpoProto.DPO_PROTO_IP4)], is_ip6=0)
153 ip4_via_tunnel.add_vpp_config()
154
155 ip6_via_tunnel = VppIpRoute(
156 self, "dead::", 16,
157 [VppRoutePath("::",
158 sw_if_index,
159 proto=DpoProto.DPO_PROTO_IP6)], is_ip6=1)
160 ip6_via_tunnel.add_vpp_config()
161
162 # Encapsulation
163
164 # IPv6 in to IPv6 tunnel
165 p6 = (p_ether / p_ip6 / p_payload)
Ole Troand57f6362018-05-24 13:21:43 +0200166 p6_reply = (IPv6(src=self.pg0.local_ip6, dst=self.pg1.remote_ip6,
167 hlim=63, tc=42) /
168 p_ip6 / p_payload)
Ole Troan298c6952018-03-08 12:30:43 +0100169 p6_reply[1].hlim -= 1
Ole Troand57f6362018-05-24 13:21:43 +0200170 rx = self.send_and_expect(self.pg0, p6*11, self.pg1)
Ole Troan298c6952018-03-08 12:30:43 +0100171 for p in rx:
172 self.validate(p[1], p6_reply)
173
174 # IPv4 in to IPv6 tunnel
175 p4 = (p_ether / p_ip4 / p_payload)
176 p4_reply = (IPv6(src=self.pg0.local_ip6,
Ole Troand57f6362018-05-24 13:21:43 +0200177 dst=self.pg1.remote_ip6, hlim=63, tc=42) /
178 p_ip4 / p_payload)
Ole Troan298c6952018-03-08 12:30:43 +0100179 p4_reply[1].ttl -= 1
Ole Troand57f6362018-05-24 13:21:43 +0200180 rx = self.send_and_expect(self.pg0, p4*11, self.pg1)
Ole Troan298c6952018-03-08 12:30:43 +0100181 for p in rx:
182 self.validate(p[1], p4_reply)
183
184 # Decapsulation
185
186 p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
187
188 # IPv6 tunnel to IPv4
189 p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4)
190 p4 = (p_ether / IPv6(src=self.pg1.remote_ip6,
191 dst=self.pg0.local_ip6) / p_ip4 / p_payload)
192 p4_reply = (p_ip4 / p_payload)
193 p4_reply.ttl -= 1
Ole Troand57f6362018-05-24 13:21:43 +0200194 rx = self.send_and_expect(self.pg1, p4*11, self.pg0)
Ole Troan298c6952018-03-08 12:30:43 +0100195 for p in rx:
196 self.validate(p[1], p4_reply)
197
198 # IPv6 tunnel to IPv6
199 p_ip6 = IPv6(src="1:2:3::4", dst=self.pg0.remote_ip6)
200 p6 = (p_ether / IPv6(src=self.pg1.remote_ip6,
201 dst=self.pg0.local_ip6) / p_ip6 / p_payload)
202 p6_reply = (p_ip6 / p_payload)
203 p6_reply.hlim = 63
Ole Troand57f6362018-05-24 13:21:43 +0200204 rx = self.send_and_expect(self.pg1, p6*11, self.pg0)
Ole Troan298c6952018-03-08 12:30:43 +0100205 for p in rx:
206 self.validate(p[1], p6_reply)
207
208 def test_ipip_create(self):
209 """ ipip create / delete interface test """
210 rv = self.vapi.ipip_add_tunnel(
Ole Troan46c1c112018-03-14 20:39:40 +0100211 src_address=inet_pton(AF_INET, '1.2.3.4'),
212 dst_address=inet_pton(AF_INET, '2.3.4.5'), is_ipv6=0)
Ole Troan298c6952018-03-08 12:30:43 +0100213 sw_if_index = rv.sw_if_index
Ole Troan46c1c112018-03-14 20:39:40 +0100214 self.vapi.ipip_del_tunnel(sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100215
216
217if __name__ == '__main__':
218 unittest.main(testRunner=VppTestRunner)