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