Add the packages for 19.04
[vpp.git] / test / test_p2p_ethernet.py
1 #!/usr/bin/env python
2 import random
3 import unittest
4 import datetime
5 import re
6
7 from scapy.packet import Raw
8 from scapy.layers.l2 import Ether
9 from scapy.layers.inet import IP, UDP
10 from scapy.layers.inet6 import IPv6
11
12 from framework import VppTestCase, VppTestRunner
13 from vpp_sub_interface import VppP2PSubint
14 from vpp_ip import DpoProto
15 from vpp_ip_route import VppIpRoute, VppRoutePath
16 from vpp_papi import mac_pton
17
18
19 class P2PEthernetAPI(VppTestCase):
20     """P2P Ethernet tests"""
21
22     p2p_sub_ifs = []
23
24     @classmethod
25     def setUpClass(cls):
26         super(P2PEthernetAPI, cls).setUpClass()
27
28         # Create pg interfaces
29         cls.create_pg_interfaces(range(4))
30
31         # Set up all interfaces
32         for i in cls.pg_interfaces:
33             i.admin_up()
34
35     @classmethod
36     def tearDownClass(cls):
37         super(P2PEthernetAPI, cls).tearDownClass()
38
39     def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
40         p2p = VppP2PSubint(self, parent_if, sub_id, mac_pton(remote_mac))
41         self.p2p_sub_ifs.append(p2p)
42
43     def delete_p2p_ethernet(self, parent_if, remote_mac):
44         self.vapi.p2p_ethernet_del(parent_if.sw_if_index,
45                                    mac_pton(remote_mac))
46
47     def test_api(self):
48         """delete/create p2p subif"""
49         self.logger.info("FFP_TEST_START_0000")
50
51         self.create_p2p_ethernet(self.pg0, 1, "de:ad:00:00:00:01")
52         self.create_p2p_ethernet(self.pg0, 2, "de:ad:00:00:00:02")
53         intfs = self.vapi.cli("show interface")
54
55         self.assertIn('pg0.1', intfs)
56         self.assertIn('pg0.2', intfs)
57         self.assertNotIn('pg0.5', intfs)
58
59         # create pg2.5 subif
60         self.create_p2p_ethernet(self.pg0, 5, "de:ad:00:00:00:ff")
61         intfs = self.vapi.cli("show interface")
62         self.assertIn('pg0.5', intfs)
63         # delete pg2.5 subif
64         self.delete_p2p_ethernet(self.pg0, "de:ad:00:00:00:ff")
65
66         intfs = self.vapi.cli("show interface")
67
68         self.assertIn('pg0.1', intfs)
69         self.assertIn('pg0.2', intfs)
70         self.assertNotIn('pg0.5', intfs)
71
72         self.logger.info("FFP_TEST_FINISH_0000")
73
74     def test_p2p_subif_creation_1k(self):
75         """create 1k of p2p subifs"""
76         self.logger.info("FFP_TEST_START_0001")
77
78         macs = []
79         clients = 1000
80         mac = int("dead00000000", 16)
81
82         for i in range(1, clients+1):
83             try:
84                 macs.append(':'.join(re.findall('..', '{:02x}'.format(
85                     mac+i))))
86                 self.vapi.p2p_ethernet_add(self.pg2.sw_if_index,
87                                            mac_pton(macs[i-1]),
88                                            i)
89             except Exception:
90                 self.logger.info("Failed to create subif %d %s" % (
91                     i, macs[i-1]))
92                 raise
93
94         intfs = self.vapi.cli("show interface").split("\n")
95         count = 0
96         for intf in intfs:
97             if intf.startswith('pg2.'):
98                 count += 1
99         self.assertEqual(count, clients)
100
101         self.logger.info("FFP_TEST_FINISH_0001")
102
103
104 class P2PEthernetIPV6(VppTestCase):
105     """P2P Ethernet IPv6 tests"""
106
107     p2p_sub_ifs = []
108     packets = []
109
110     @classmethod
111     def setUpClass(cls):
112         super(P2PEthernetIPV6, cls).setUpClass()
113
114         # Create pg interfaces
115         cls.create_pg_interfaces(range(3))
116
117         # Packet sizes
118         cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
119
120         # Set up all interfaces
121         for i in cls.pg_interfaces:
122             i.admin_up()
123
124         cls.pg0.generate_remote_hosts(3)
125         cls.pg0.configure_ipv6_neighbors()
126
127         cls.pg1.config_ip6()
128         cls.pg1.generate_remote_hosts(3)
129         cls.pg1.configure_ipv6_neighbors()
130         cls.pg1.disable_ipv6_ra()
131
132     @classmethod
133     def tearDownClass(cls):
134         super(P2PEthernetIPV6, cls).tearDownClass()
135
136     def setUp(self):
137         super(P2PEthernetIPV6, self).setUp()
138         for p in self.packets:
139             self.packets.remove(p)
140         self.p2p_sub_ifs.append(
141             self.create_p2p_ethernet(self.pg0, 1,
142                                      self.pg0._remote_hosts[0].mac))
143         self.p2p_sub_ifs.append(
144             self.create_p2p_ethernet(self.pg0, 2,
145                                      self.pg0._remote_hosts[1].mac))
146         self.vapi.cli("trace add p2p-ethernet-input 50")
147
148     def tearDown(self):
149         while len(self.p2p_sub_ifs):
150             p2p = self.p2p_sub_ifs.pop()
151             self.delete_p2p_ethernet(p2p)
152
153         super(P2PEthernetIPV6, self).tearDown()
154
155     def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
156         p2p = VppP2PSubint(self, parent_if, sub_id, mac_pton(remote_mac))
157         p2p.admin_up()
158         p2p.config_ip6()
159         p2p.disable_ipv6_ra()
160         return p2p
161
162     def delete_p2p_ethernet(self, p2p):
163         p2p.unconfig_ip6()
164         p2p.admin_down()
165         self.vapi.p2p_ethernet_del(p2p.parent.sw_if_index,
166                                    p2p.p2p_remote_mac)
167
168     def create_stream(self, src_mac=None, dst_mac=None,
169                       src_ip=None, dst_ip=None, size=None):
170         pkt_size = size
171         if size is None:
172             pkt_size = random.choice(self.pg_if_packet_sizes)
173         p = Ether(src=src_mac, dst=dst_mac)
174         p /= IPv6(src=src_ip, dst=dst_ip)
175         p /= (UDP(sport=1234, dport=4321) / Raw('\xa5' * 20))
176         self.extend_packet(p, pkt_size)
177         return p
178
179     def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
180         self.pg_enable_capture([dst_if])
181         if packets is None:
182             packets = self.packets
183         src_if.add_stream(packets)
184         self.pg_start()
185         if count is None:
186             count = len(packets)
187         return dst_if.get_capture(count)
188
189     def test_no_p2p_subif(self):
190         """standard routing without p2p subinterfaces"""
191         self.logger.info("FFP_TEST_START_0001")
192
193         route_8000 = VppIpRoute(self, "8000::", 64,
194                                 [VppRoutePath(self.pg0.remote_ip6,
195                                               self.pg0.sw_if_index,
196                                               proto=DpoProto.DPO_PROTO_IP6)],
197                                 is_ip6=1)
198         route_8000.add_vpp_config()
199
200         self.packets = [(Ether(dst=self.pg1.local_mac,
201                                src=self.pg1.remote_mac) /
202                          IPv6(src="3001::1", dst="8000::100") /
203                          UDP(sport=1234, dport=1234) /
204                          Raw('\xa5' * 100))]
205         self.send_packets(self.pg1, self.pg0)
206
207         self.logger.info("FFP_TEST_FINISH_0001")
208
209     def test_ip6_rx_p2p_subif(self):
210         """receive ipv6 packet via p2p subinterface"""
211         self.logger.info("FFP_TEST_START_0002")
212
213         route_9001 = VppIpRoute(self, "9001::", 64,
214                                 [VppRoutePath(self.pg1.remote_ip6,
215                                               self.pg1.sw_if_index,
216                                               proto=DpoProto.DPO_PROTO_IP6)],
217                                 is_ip6=1)
218         route_9001.add_vpp_config()
219
220         self.packets.append(
221             self.create_stream(src_mac=self.pg0._remote_hosts[0].mac,
222                                dst_mac=self.pg0.local_mac,
223                                src_ip=self.p2p_sub_ifs[0].remote_ip6,
224                                dst_ip="9001::100"))
225
226         self.send_packets(self.pg0, self.pg1, self.packets)
227         self.assert_packet_counter_equal('p2p-ethernet-input', 1)
228
229         route_9001.remove_vpp_config()
230         self.logger.info("FFP_TEST_FINISH_0002")
231
232     def test_ip6_rx_p2p_subif_route(self):
233         """route rx ip6 packet not matching p2p subinterface"""
234         self.logger.info("FFP_TEST_START_0003")
235
236         self.pg0.config_ip6()
237
238         route_3 = VppIpRoute(self, "9000::", 64,
239                              [VppRoutePath(self.pg1._remote_hosts[0].ip6,
240                                            self.pg1.sw_if_index,
241                                            proto=DpoProto.DPO_PROTO_IP6)],
242                              is_ip6=1)
243         route_3.add_vpp_config()
244
245         self.packets.append(
246             self.create_stream(src_mac="02:03:00:00:ff:ff",
247                                dst_mac=self.pg0.local_mac,
248                                src_ip="a000::100",
249                                dst_ip="9000::100"))
250
251         self.send_packets(self.pg0, self.pg1)
252
253         self.pg0.unconfig_ip6()
254
255         route_3.remove_vpp_config()
256
257         self.logger.info("FFP_TEST_FINISH_0003")
258
259     def test_ip6_rx_p2p_subif_drop(self):
260         """drop rx packet not matching p2p subinterface"""
261         self.logger.info("FFP_TEST_START_0004")
262
263         route_9001 = VppIpRoute(self, "9000::", 64,
264                                 [VppRoutePath(self.pg1._remote_hosts[0].ip6,
265                                               self.pg1.sw_if_index,
266                                               proto=DpoProto.DPO_PROTO_IP6)],
267                                 is_ip6=1)
268         route_9001.add_vpp_config()
269
270         self.packets.append(
271             self.create_stream(src_mac="02:03:00:00:ff:ff",
272                                dst_mac=self.pg0.local_mac,
273                                src_ip="a000::100",
274                                dst_ip="9000::100"))
275
276         # no packet received
277         self.send_packets(self.pg0, self.pg1, count=0)
278         self.logger.info("FFP_TEST_FINISH_0004")
279
280     def test_ip6_tx_p2p_subif(self):
281         """send packet via p2p subinterface"""
282         self.logger.info("FFP_TEST_START_0005")
283
284         route_8000 = VppIpRoute(self, "8000::", 64,
285                                 [VppRoutePath(self.pg0.remote_ip6,
286                                               self.pg0.sw_if_index,
287                                               proto=DpoProto.DPO_PROTO_IP6)],
288                                 is_ip6=1)
289         route_8000.add_vpp_config()
290         route_8001 = VppIpRoute(self, "8001::", 64,
291                                 [VppRoutePath(self.p2p_sub_ifs[0].remote_ip6,
292                                               self.p2p_sub_ifs[0].sw_if_index,
293                                               proto=DpoProto.DPO_PROTO_IP6)],
294                                 is_ip6=1)
295         route_8001.add_vpp_config()
296         route_8002 = VppIpRoute(self, "8002::", 64,
297                                 [VppRoutePath(self.p2p_sub_ifs[1].remote_ip6,
298                                               self.p2p_sub_ifs[1].sw_if_index,
299                                               proto=DpoProto.DPO_PROTO_IP6)],
300                                 is_ip6=1)
301         route_8002.add_vpp_config()
302
303         for i in range(0, 3):
304             self.packets.append(
305                 self.create_stream(src_mac=self.pg1.remote_mac,
306                                    dst_mac=self.pg1.local_mac,
307                                    src_ip=self.pg1.remote_ip6,
308                                    dst_ip="800%d::100" % i))
309
310         self.send_packets(self.pg1, self.pg0, count=3)
311
312         route_8000.remove_vpp_config()
313         route_8001.remove_vpp_config()
314         route_8002.remove_vpp_config()
315
316         self.logger.info("FFP_TEST_FINISH_0005")
317
318     def test_ip6_tx_p2p_subif_drop(self):
319         """drop tx ip6 packet not matching p2p subinterface"""
320         self.logger.info("FFP_TEST_START_0006")
321
322         self.packets.append(
323             self.create_stream(src_mac="02:03:00:00:ff:ff",
324                                dst_mac=self.pg0.local_mac,
325                                src_ip="a000::100",
326                                dst_ip="9000::100"))
327
328         # no packet received
329         self.send_packets(self.pg0, self.pg1, count=0)
330         self.logger.info("FFP_TEST_FINISH_0006")
331
332
333 class P2PEthernetIPV4(VppTestCase):
334     """P2P Ethernet IPv4 tests"""
335
336     p2p_sub_ifs = []
337     packets = []
338
339     @classmethod
340     def setUpClass(cls):
341         super(P2PEthernetIPV4, cls).setUpClass()
342
343         # Create pg interfaces
344         cls.create_pg_interfaces(range(3))
345
346         # Packet sizes
347         cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
348
349         # Set up all interfaces
350         for i in cls.pg_interfaces:
351             i.admin_up()
352
353         cls.pg0.config_ip4()
354         cls.pg0.generate_remote_hosts(5)
355         cls.pg0.configure_ipv4_neighbors()
356
357         cls.pg1.config_ip4()
358         cls.pg1.generate_remote_hosts(5)
359         cls.pg1.configure_ipv4_neighbors()
360
361     @classmethod
362     def tearDownClass(cls):
363         super(P2PEthernetIPV4, cls).tearDownClass()
364
365     def setUp(self):
366         super(P2PEthernetIPV4, self).setUp()
367         for p in self.packets:
368             self.packets.remove(p)
369         self.p2p_sub_ifs.append(
370             self.create_p2p_ethernet(self.pg0, 1,
371                                      self.pg0._remote_hosts[0].mac))
372         self.p2p_sub_ifs.append(
373             self.create_p2p_ethernet(self.pg0, 2,
374                                      self.pg0._remote_hosts[1].mac))
375         self.vapi.cli("trace add p2p-ethernet-input 50")
376
377     def tearDown(self):
378         while len(self.p2p_sub_ifs):
379             p2p = self.p2p_sub_ifs.pop()
380             self.delete_p2p_ethernet(p2p)
381         super(P2PEthernetIPV4, self).tearDown()
382
383     def create_stream(self, src_mac=None, dst_mac=None,
384                       src_ip=None, dst_ip=None, size=None):
385         pkt_size = size
386         if size is None:
387             pkt_size = random.choice(self.pg_if_packet_sizes)
388         p = Ether(src=src_mac, dst=dst_mac)
389         p /= IP(src=src_ip, dst=dst_ip)
390         p /= (UDP(sport=1234, dport=4321) / Raw('\xa5' * 20))
391         self.extend_packet(p, pkt_size)
392         return p
393
394     def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
395         self.pg_enable_capture([dst_if])
396         if packets is None:
397             packets = self.packets
398         src_if.add_stream(packets)
399         self.pg_start()
400         if count is None:
401             count = len(packets)
402         return dst_if.get_capture(count)
403
404     def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
405         p2p = VppP2PSubint(self, parent_if, sub_id, mac_pton(remote_mac))
406         p2p.admin_up()
407         p2p.config_ip4()
408         return p2p
409
410     def delete_p2p_ethernet(self, p2p):
411         p2p.unconfig_ip4()
412         p2p.admin_down()
413         self.vapi.p2p_ethernet_del(p2p.parent.sw_if_index,
414                                    p2p.p2p_remote_mac)
415
416     def test_ip4_rx_p2p_subif(self):
417         """receive ipv4 packet via p2p subinterface"""
418         self.logger.info("FFP_TEST_START_0002")
419
420         route_9000 = VppIpRoute(self, "9.0.0.0", 16,
421                                 [VppRoutePath(self.pg1.remote_ip4,
422                                               self.pg1.sw_if_index)])
423         route_9000.add_vpp_config()
424
425         self.packets.append(
426             self.create_stream(src_mac=self.pg0._remote_hosts[0].mac,
427                                dst_mac=self.pg0.local_mac,
428                                src_ip=self.p2p_sub_ifs[0].remote_ip4,
429                                dst_ip="9.0.0.100"))
430
431         self.send_packets(self.pg0, self.pg1, self.packets)
432
433         self.assert_packet_counter_equal('p2p-ethernet-input', 1)
434
435         route_9000.remove_vpp_config()
436         self.logger.info("FFP_TEST_FINISH_0002")
437
438     def test_ip4_rx_p2p_subif_route(self):
439         """route rx packet not matching p2p subinterface"""
440         self.logger.info("FFP_TEST_START_0003")
441
442         route_9001 = VppIpRoute(self, "9.0.0.0", 24,
443                                 [VppRoutePath(self.pg1.remote_ip4,
444                                               self.pg1.sw_if_index)])
445         route_9001.add_vpp_config()
446
447         self.packets.append(
448             self.create_stream(src_mac="02:01:00:00:ff:ff",
449                                dst_mac=self.pg0.local_mac,
450                                src_ip="8.0.0.100",
451                                dst_ip="9.0.0.100"))
452
453         self.send_packets(self.pg0, self.pg1)
454
455         route_9001.remove_vpp_config()
456
457         self.logger.info("FFP_TEST_FINISH_0003")
458
459     def test_ip4_tx_p2p_subif(self):
460         """send ip4 packet via p2p subinterface"""
461         self.logger.info("FFP_TEST_START_0005")
462
463         route_9100 = VppIpRoute(self, "9.1.0.100", 24,
464                                 [VppRoutePath(self.pg0.remote_ip4,
465                                               self.pg0.sw_if_index,
466                                               )])
467         route_9100.add_vpp_config()
468         route_9200 = VppIpRoute(self, "9.2.0.100", 24,
469                                 [VppRoutePath(self.p2p_sub_ifs[0].remote_ip4,
470                                               self.p2p_sub_ifs[0].sw_if_index,
471                                               )])
472         route_9200.add_vpp_config()
473         route_9300 = VppIpRoute(self, "9.3.0.100", 24,
474                                 [VppRoutePath(self.p2p_sub_ifs[1].remote_ip4,
475                                               self.p2p_sub_ifs[1].sw_if_index
476                                               )])
477         route_9300.add_vpp_config()
478
479         for i in range(0, 3):
480             self.packets.append(
481                 self.create_stream(src_mac=self.pg1.remote_mac,
482                                    dst_mac=self.pg1.local_mac,
483                                    src_ip=self.pg1.remote_ip4,
484                                    dst_ip="9.%d.0.100" % (i+1)))
485
486         self.send_packets(self.pg1, self.pg0)
487
488         # route_7000.remove_vpp_config()
489         route_9100.remove_vpp_config()
490         route_9200.remove_vpp_config()
491         route_9300.remove_vpp_config()
492
493         self.logger.info("FFP_TEST_FINISH_0005")
494
495     def test_ip4_tx_p2p_subif_drop(self):
496         """drop tx ip4 packet not matching p2p subinterface"""
497         self.logger.info("FFP_TEST_START_0006")
498
499         self.packets.append(
500             self.create_stream(src_mac="02:01:00:00:ff:ff",
501                                dst_mac=self.pg0.local_mac,
502                                src_ip="8.0.0.100",
503                                dst_ip="9.0.0.100"))
504
505         # no packet received
506         self.send_packets(self.pg0, self.pg1, count=0)
507         self.logger.info("FFP_TEST_FINISH_0006")
508
509
510 if __name__ == '__main__':
511     unittest.main(testRunner=VppTestRunner)