3 from framework import tag_fixme_vpp_workers
4 from framework import VppTestCase, VppTestRunner
6 from vpp_udp_encap import find_udp_encap, VppUdpEncap
7 from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable, VppMplsLabel, \
10 from scapy.packet import Raw
11 from scapy.layers.l2 import Ether
12 from scapy.layers.inet import IP, UDP
13 from scapy.layers.inet6 import IPv6
14 from scapy.contrib.mpls import MPLS
19 @tag_fixme_vpp_workers
20 class TestUdpEncap(VppTestCase):
21 """ UDP Encap Test Case """
25 super(TestUdpEncap, cls).setUpClass()
28 def tearDownClass(cls):
29 super(TestUdpEncap, cls).tearDownClass()
32 super(TestUdpEncap, self).setUp()
34 # create 2 pg interfaces
35 self.create_pg_interfaces(range(4))
38 # assign them different tables.
42 for i in self.pg_interfaces:
46 tbl = VppIpTable(self, table_id)
48 self.tables.append(tbl)
49 tbl = VppIpTable(self, table_id, is_ip6=1)
51 self.tables.append(tbl)
53 i.set_table_ip4(table_id)
54 i.set_table_ip6(table_id)
62 for i in self.pg_interfaces:
68 super(TestUdpEncap, self).tearDown()
70 def validate_outer4(self, rx, encap_obj):
71 self.assertEqual(rx[IP].src, encap_obj.src_ip_s)
72 self.assertEqual(rx[IP].dst, encap_obj.dst_ip_s)
73 self.assertEqual(rx[UDP].sport, encap_obj.src_port)
74 self.assertEqual(rx[UDP].dport, encap_obj.dst_port)
76 def validate_outer6(self, rx, encap_obj):
77 self.assertEqual(rx[IPv6].src, encap_obj.src_ip_s)
78 self.assertEqual(rx[IPv6].dst, encap_obj.dst_ip_s)
79 self.assertEqual(rx[UDP].sport, encap_obj.src_port)
80 self.assertEqual(rx[UDP].dport, encap_obj.dst_port)
82 def validate_inner4(self, rx, tx, ttl=None):
83 self.assertEqual(rx[IP].src, tx[IP].src)
84 self.assertEqual(rx[IP].dst, tx[IP].dst)
86 self.assertEqual(rx[IP].ttl, ttl)
88 self.assertEqual(rx[IP].ttl, tx[IP].ttl)
90 def validate_inner6(self, rx, tx):
91 self.assertEqual(rx.src, tx[IPv6].src)
92 self.assertEqual(rx.dst, tx[IPv6].dst)
93 self.assertEqual(rx.hlim, tx[IPv6].hlim)
95 def test_udp_encap(self):
100 # construct a UDP encap object through each of the peers
101 # v4 through the first two peers, v6 through the second.
103 udp_encap_0 = VppUdpEncap(self,
107 udp_encap_1 = VppUdpEncap(self,
112 udp_encap_2 = VppUdpEncap(self,
117 udp_encap_3 = VppUdpEncap(self,
122 udp_encap_0.add_vpp_config()
123 udp_encap_1.add_vpp_config()
124 udp_encap_2.add_vpp_config()
125 udp_encap_3.add_vpp_config()
127 self.logger.info(self.vapi.cli("sh udp encap"))
129 self.assertTrue(find_udp_encap(self, udp_encap_2))
130 self.assertTrue(find_udp_encap(self, udp_encap_3))
131 self.assertTrue(find_udp_encap(self, udp_encap_0))
132 self.assertTrue(find_udp_encap(self, udp_encap_1))
135 # Routes via each UDP encap object - all combinations of v4 and v6.
137 route_4o4 = VppIpRoute(
139 [VppRoutePath("0.0.0.0",
141 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
142 next_hop_id=udp_encap_0.id)])
143 route_4o6 = VppIpRoute(
145 [VppRoutePath("0.0.0.0",
147 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
148 next_hop_id=udp_encap_2.id)])
149 route_6o4 = VppIpRoute(
150 self, "2001::1", 128,
151 [VppRoutePath("0.0.0.0",
153 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
154 next_hop_id=udp_encap_1.id)])
155 route_6o6 = VppIpRoute(
156 self, "2001::3", 128,
157 [VppRoutePath("0.0.0.0",
159 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
160 next_hop_id=udp_encap_3.id)])
161 route_4o6.add_vpp_config()
162 route_6o6.add_vpp_config()
163 route_6o4.add_vpp_config()
164 route_4o4.add_vpp_config()
169 p_4o4 = (Ether(src=self.pg0.remote_mac,
170 dst=self.pg0.local_mac) /
171 IP(src="2.2.2.2", dst="1.1.0.1") /
172 UDP(sport=1234, dport=1234) /
174 rx = self.send_and_expect(self.pg0, p_4o4*NUM_PKTS, self.pg0)
176 self.validate_outer4(p, udp_encap_0)
177 p = IP(p["UDP"].payload.load)
178 self.validate_inner4(p, p_4o4)
179 self.assertEqual(udp_encap_0.get_stats()['packets'], NUM_PKTS)
184 p_4o6 = (Ether(src=self.pg0.remote_mac,
185 dst=self.pg0.local_mac) /
186 IP(src="2.2.2.2", dst="1.1.2.1") /
187 UDP(sport=1234, dport=1234) /
189 rx = self.send_and_expect(self.pg0, p_4o6*NUM_PKTS, self.pg2)
191 self.validate_outer6(p, udp_encap_2)
192 p = IP(p["UDP"].payload.load)
193 self.validate_inner4(p, p_4o6)
194 self.assertEqual(udp_encap_2.get_stats()['packets'], NUM_PKTS)
199 p_6o4 = (Ether(src=self.pg0.remote_mac,
200 dst=self.pg0.local_mac) /
201 IPv6(src="2001::100", dst="2001::1") /
202 UDP(sport=1234, dport=1234) /
204 rx = self.send_and_expect(self.pg0, p_6o4*NUM_PKTS, self.pg1)
206 self.validate_outer4(p, udp_encap_1)
207 p = IPv6(p["UDP"].payload.load)
208 self.validate_inner6(p, p_6o4)
209 self.assertEqual(udp_encap_1.get_stats()['packets'], NUM_PKTS)
214 p_6o6 = (Ether(src=self.pg0.remote_mac,
215 dst=self.pg0.local_mac) /
216 IPv6(src="2001::100", dst="2001::3") /
217 UDP(sport=1234, dport=1234) /
219 rx = self.send_and_expect(self.pg0, p_6o6*NUM_PKTS, self.pg3)
221 self.validate_outer6(p, udp_encap_3)
222 p = IPv6(p["UDP"].payload.load)
223 self.validate_inner6(p, p_6o6)
224 self.assertEqual(udp_encap_3.get_stats()['packets'], NUM_PKTS)
227 # A route with an output label
228 # the TTL of the inner packet is decremented on LSP ingress
230 route_4oMPLSo4 = VppIpRoute(
231 self, "1.1.2.22", 32,
232 [VppRoutePath("0.0.0.0",
234 type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
236 labels=[VppMplsLabel(66)])])
237 route_4oMPLSo4.add_vpp_config()
239 p_4omo4 = (Ether(src=self.pg0.remote_mac,
240 dst=self.pg0.local_mac) /
241 IP(src="2.2.2.2", dst="1.1.2.22") /
242 UDP(sport=1234, dport=1234) /
244 rx = self.send_and_expect(self.pg0, p_4omo4*NUM_PKTS, self.pg1)
246 self.validate_outer4(p, udp_encap_1)
247 p = MPLS(p["UDP"].payload.load)
248 self.validate_inner4(p, p_4omo4, ttl=63)
249 self.assertEqual(udp_encap_1.get_stats()['packets'], 2*NUM_PKTS)
252 @tag_fixme_vpp_workers
253 class TestUDP(VppTestCase):
254 """ UDP Test Case """
258 super(TestUDP, cls).setUpClass()
261 def tearDownClass(cls):
262 super(TestUDP, cls).tearDownClass()
265 super(TestUDP, self).setUp()
266 self.vapi.session_enable_disable(is_enable=1)
267 self.create_loopback_interfaces(2)
271 for i in self.lo_interfaces:
275 tbl = VppIpTable(self, table_id)
278 i.set_table_ip4(table_id)
282 # Configure namespaces
283 self.vapi.app_namespace_add_del(namespace_id="0",
284 sw_if_index=self.loop0.sw_if_index)
285 self.vapi.app_namespace_add_del(namespace_id="1",
286 sw_if_index=self.loop1.sw_if_index)
289 for i in self.lo_interfaces:
293 self.vapi.session_enable_disable(is_enable=0)
294 super(TestUDP, self).tearDown()
296 def test_udp_transfer(self):
297 """ UDP echo client/server transfer """
299 # Add inter-table routes
300 ip_t01 = VppIpRoute(self, self.loop1.local_ip4, 32,
301 [VppRoutePath("0.0.0.0",
304 ip_t10 = VppIpRoute(self, self.loop0.local_ip4, 32,
305 [VppRoutePath("0.0.0.0",
307 nh_table_id=0)], table_id=1)
308 ip_t01.add_vpp_config()
309 ip_t10.add_vpp_config()
311 # Start builtin server and client
312 uri = "udp://" + self.loop0.local_ip4 + "/1234"
313 error = self.vapi.cli("test echo server appns 0 fifo-size 4 no-echo" +
316 self.logger.critical(error)
317 self.assertNotIn("failed", error)
319 error = self.vapi.cli("test echo client mbytes 10 appns 1 " +
320 "fifo-size 4 no-output test-bytes " +
321 "syn-timeout 2 no-return uri " + uri)
323 self.logger.critical(error)
324 self.assertNotIn("failed", error)
326 self.logger.debug(self.vapi.cli("show session verbose 2"))
328 # Delete inter-table routes
329 ip_t01.remove_vpp_config()
330 ip_t10.remove_vpp_config()
333 if __name__ == '__main__':
334 unittest.main(testRunner=VppTestRunner)