3 from framework import VppTestCase, VppTestRunner
5 from vpp_udp_encap import find_udp_encap, VppUdpEncap
6 from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable, VppMplsLabel, \
9 from scapy.packet import Raw
10 from scapy.layers.l2 import Ether
11 from scapy.layers.inet import IP, UDP
12 from scapy.layers.inet6 import IPv6
13 from scapy.contrib.mpls import MPLS
18 class TestUdpEncap(VppTestCase):
19 """ UDP Encap Test Case """
23 super(TestUdpEncap, cls).setUpClass()
26 def tearDownClass(cls):
27 super(TestUdpEncap, cls).tearDownClass()
30 super(TestUdpEncap, self).setUp()
32 # create 2 pg interfaces
33 self.create_pg_interfaces(range(4))
36 # assign them different tables.
40 for i in self.pg_interfaces:
44 tbl = VppIpTable(self, table_id)
46 self.tables.append(tbl)
47 tbl = VppIpTable(self, table_id, is_ip6=1)
49 self.tables.append(tbl)
51 i.set_table_ip4(table_id)
52 i.set_table_ip6(table_id)
60 for i in self.pg_interfaces:
66 super(TestUdpEncap, self).tearDown()
68 def validate_outer4(self, rx, encap_obj):
69 self.assertEqual(rx[IP].src, encap_obj.src_ip_s)
70 self.assertEqual(rx[IP].dst, encap_obj.dst_ip_s)
71 self.assertEqual(rx[UDP].sport, encap_obj.src_port)
72 self.assertEqual(rx[UDP].dport, encap_obj.dst_port)
74 def validate_outer6(self, rx, encap_obj):
75 self.assertEqual(rx[IPv6].src, encap_obj.src_ip_s)
76 self.assertEqual(rx[IPv6].dst, encap_obj.dst_ip_s)
77 self.assertEqual(rx[UDP].sport, encap_obj.src_port)
78 self.assertEqual(rx[UDP].dport, encap_obj.dst_port)
80 def validate_inner4(self, rx, tx, ttl=None):
81 self.assertEqual(rx[IP].src, tx[IP].src)
82 self.assertEqual(rx[IP].dst, tx[IP].dst)
84 self.assertEqual(rx[IP].ttl, ttl)
86 self.assertEqual(rx[IP].ttl, tx[IP].ttl)
88 def validate_inner6(self, rx, tx):
89 self.assertEqual(rx.src, tx[IPv6].src)
90 self.assertEqual(rx.dst, tx[IPv6].dst)
91 self.assertEqual(rx.hlim, tx[IPv6].hlim)
93 def test_udp_encap(self):
98 # construct a UDP encap object through each of the peers
99 # v4 through the first two peers, v6 through the second.
101 udp_encap_0 = VppUdpEncap(self,
105 udp_encap_1 = VppUdpEncap(self,
110 udp_encap_2 = VppUdpEncap(self,
115 udp_encap_3 = VppUdpEncap(self,
120 udp_encap_0.add_vpp_config()
121 udp_encap_1.add_vpp_config()
122 udp_encap_2.add_vpp_config()
123 udp_encap_3.add_vpp_config()
125 self.logger.info(self.vapi.cli("sh udp encap"))
127 self.assertTrue(find_udp_encap(self, udp_encap_2))
128 self.assertTrue(find_udp_encap(self, udp_encap_3))
129 self.assertTrue(find_udp_encap(self, udp_encap_0))
130 self.assertTrue(find_udp_encap(self, udp_encap_1))
133 # Routes via each UDP encap object - all combinations of v4 and v6.
135 route_4o4 = VppIpRoute(
137 [VppRoutePath("0.0.0.0",
139 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
140 next_hop_id=udp_encap_0.id)])
141 route_4o6 = VppIpRoute(
143 [VppRoutePath("0.0.0.0",
145 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
146 next_hop_id=udp_encap_2.id)])
147 route_6o4 = VppIpRoute(
148 self, "2001::1", 128,
149 [VppRoutePath("0.0.0.0",
151 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
152 next_hop_id=udp_encap_1.id)])
153 route_6o6 = VppIpRoute(
154 self, "2001::3", 128,
155 [VppRoutePath("0.0.0.0",
157 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
158 next_hop_id=udp_encap_3.id)])
159 route_4o6.add_vpp_config()
160 route_6o6.add_vpp_config()
161 route_6o4.add_vpp_config()
162 route_4o4.add_vpp_config()
167 p_4o4 = (Ether(src=self.pg0.remote_mac,
168 dst=self.pg0.local_mac) /
169 IP(src="2.2.2.2", dst="1.1.0.1") /
170 UDP(sport=1234, dport=1234) /
172 rx = self.send_and_expect(self.pg0, p_4o4*NUM_PKTS, self.pg0)
174 self.validate_outer4(p, udp_encap_0)
175 p = IP(p["UDP"].payload.load)
176 self.validate_inner4(p, p_4o4)
177 self.assertEqual(udp_encap_0.get_stats()['packets'], NUM_PKTS)
182 p_4o6 = (Ether(src=self.pg0.remote_mac,
183 dst=self.pg0.local_mac) /
184 IP(src="2.2.2.2", dst="1.1.2.1") /
185 UDP(sport=1234, dport=1234) /
187 rx = self.send_and_expect(self.pg0, p_4o6*NUM_PKTS, self.pg2)
189 self.validate_outer6(p, udp_encap_2)
190 p = IP(p["UDP"].payload.load)
191 self.validate_inner4(p, p_4o6)
192 self.assertEqual(udp_encap_2.get_stats()['packets'], NUM_PKTS)
197 p_6o4 = (Ether(src=self.pg0.remote_mac,
198 dst=self.pg0.local_mac) /
199 IPv6(src="2001::100", dst="2001::1") /
200 UDP(sport=1234, dport=1234) /
202 rx = self.send_and_expect(self.pg0, p_6o4*NUM_PKTS, self.pg1)
204 self.validate_outer4(p, udp_encap_1)
205 p = IPv6(p["UDP"].payload.load)
206 self.validate_inner6(p, p_6o4)
207 self.assertEqual(udp_encap_1.get_stats()['packets'], NUM_PKTS)
212 p_6o6 = (Ether(src=self.pg0.remote_mac,
213 dst=self.pg0.local_mac) /
214 IPv6(src="2001::100", dst="2001::3") /
215 UDP(sport=1234, dport=1234) /
217 rx = self.send_and_expect(self.pg0, p_6o6*NUM_PKTS, self.pg3)
219 self.validate_outer6(p, udp_encap_3)
220 p = IPv6(p["UDP"].payload.load)
221 self.validate_inner6(p, p_6o6)
222 self.assertEqual(udp_encap_3.get_stats()['packets'], NUM_PKTS)
225 # A route with an output label
226 # the TTL of the inner packet is decremented on LSP ingress
228 route_4oMPLSo4 = VppIpRoute(
229 self, "1.1.2.22", 32,
230 [VppRoutePath("0.0.0.0",
232 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
234 labels=[VppMplsLabel(66)])])
235 route_4oMPLSo4.add_vpp_config()
237 p_4omo4 = (Ether(src=self.pg0.remote_mac,
238 dst=self.pg0.local_mac) /
239 IP(src="2.2.2.2", dst="1.1.2.22") /
240 UDP(sport=1234, dport=1234) /
242 rx = self.send_and_expect(self.pg0, p_4omo4*NUM_PKTS, self.pg1)
244 self.validate_outer4(p, udp_encap_1)
245 p = MPLS(p["UDP"].payload.load)
246 self.validate_inner4(p, p_4omo4, ttl=63)
247 self.assertEqual(udp_encap_1.get_stats()['packets'], 2*NUM_PKTS)
250 class TestUDP(VppTestCase):
251 """ UDP Test Case """
255 super(TestUDP, cls).setUpClass()
258 def tearDownClass(cls):
259 super(TestUDP, cls).tearDownClass()
262 super(TestUDP, self).setUp()
263 self.vapi.session_enable_disable(is_enabled=1)
264 self.create_loopback_interfaces(2)
268 for i in self.lo_interfaces:
272 tbl = VppIpTable(self, table_id)
275 i.set_table_ip4(table_id)
279 # Configure namespaces
280 self.vapi.app_namespace_add_del(namespace_id="0",
281 sw_if_index=self.loop0.sw_if_index)
282 self.vapi.app_namespace_add_del(namespace_id="1",
283 sw_if_index=self.loop1.sw_if_index)
286 for i in self.lo_interfaces:
290 self.vapi.session_enable_disable(is_enabled=0)
291 super(TestUDP, self).tearDown()
293 def test_udp_transfer(self):
294 """ UDP echo client/server transfer """
296 # Add inter-table routes
297 ip_t01 = VppIpRoute(self, self.loop1.local_ip4, 32,
298 [VppRoutePath("0.0.0.0",
301 ip_t10 = VppIpRoute(self, self.loop0.local_ip4, 32,
302 [VppRoutePath("0.0.0.0",
304 nh_table_id=0)], table_id=1)
305 ip_t01.add_vpp_config()
306 ip_t10.add_vpp_config()
308 # Start builtin server and client
309 uri = "udp://" + self.loop0.local_ip4 + "/1234"
310 error = self.vapi.cli("test echo server appns 0 fifo-size 4 no-echo" +
313 self.logger.critical(error)
314 self.assertNotIn("failed", error)
316 error = self.vapi.cli("test echo client mbytes 10 appns 1 " +
317 "fifo-size 4 no-output test-bytes " +
318 "syn-timeout 2 no-return uri " + uri)
320 self.logger.critical(error)
321 self.assertNotIn("failed", error)
323 # Delete inter-table routes
324 ip_t01.remove_vpp_config()
325 ip_t10.remove_vpp_config()
328 if __name__ == '__main__':
329 unittest.main(testRunner=VppTestRunner)