IGMP: proxy device
[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 util import mactobinary
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     def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
36         p2p = VppP2PSubint(self, parent_if, sub_id, mactobinary(remote_mac))
37         self.p2p_sub_ifs.append(p2p)
38
39     def delete_p2p_ethernet(self, parent_if, remote_mac):
40         self.vapi.delete_p2pethernet_subif(parent_if.sw_if_index,
41                                            mactobinary(remote_mac))
42
43     def test_api(self):
44         """delete/create p2p subif"""
45         self.logger.info("FFP_TEST_START_0000")
46
47         self.create_p2p_ethernet(self.pg0, 1, "de:ad:00:00:00:01")
48         self.create_p2p_ethernet(self.pg0, 2, "de:ad:00:00:00:02")
49         intfs = self.vapi.cli("show interface")
50
51         self.assertNotEqual(intfs.find('pg0.1'), -1)
52         self.assertNotEqual(intfs.find('pg0.2'), -1)
53         self.assertEqual(intfs.find('pg0.5'), -1)
54
55         # create pg2.5 subif
56         self.create_p2p_ethernet(self.pg0, 5, "de:ad:00:00:00:ff")
57         intfs = self.vapi.cli("show interface")
58         self.assertNotEqual(intfs.find('pg0.5'), -1)
59         # delete pg2.5 subif
60         self.delete_p2p_ethernet(self.pg0, "de:ad:00:00:00:ff")
61
62         intfs = self.vapi.cli("show interface")
63
64         self.assertNotEqual(intfs.find('pg0.1'), -1)
65         self.assertNotEqual(intfs.find('pg0.2'), -1)
66         self.assertEqual(intfs.find('pg0.5'), -1)
67
68         self.logger.info("FFP_TEST_FINISH_0000")
69
70     def test_p2p_subif_creation_1k(self):
71         """create 1k of p2p subifs"""
72         self.logger.info("FFP_TEST_START_0001")
73
74         macs = []
75         clients = 1000
76         mac = int("dead00000000", 16)
77
78         for i in range(1, clients+1):
79             try:
80                 macs.append(':'.join(re.findall('..', '{:02x}'.format(mac+i))))
81                 self.vapi.create_p2pethernet_subif(self.pg2.sw_if_index,
82                                                    mactobinary(macs[i-1]),
83                                                    i)
84             except Exception:
85                 print "Failed to create subif %d %s" % (i, macs[i-1])
86                 raise
87
88         intfs = self.vapi.cli("show interface").split("\n")
89         count = 0
90         for intf in intfs:
91             if intf.startswith('pg2.'):
92                 count += 1
93         self.assertEqual(count, clients)
94
95         self.logger.info("FFP_TEST_FINISH_0001")
96
97
98 class P2PEthernetIPV6(VppTestCase):
99     """P2P Ethernet IPv6 tests"""
100
101     p2p_sub_ifs = []
102     packets = []
103
104     @classmethod
105     def setUpClass(cls):
106         super(P2PEthernetIPV6, cls).setUpClass()
107
108         # Create pg interfaces
109         cls.create_pg_interfaces(range(3))
110
111         # Packet sizes
112         cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
113
114         # Set up all interfaces
115         for i in cls.pg_interfaces:
116             i.admin_up()
117
118         cls.pg0.generate_remote_hosts(3)
119         cls.pg0.configure_ipv6_neighbors()
120
121         cls.pg1.config_ip6()
122         cls.pg1.generate_remote_hosts(3)
123         cls.pg1.configure_ipv6_neighbors()
124         cls.pg1.disable_ipv6_ra()
125
126     def setUp(self):
127         super(P2PEthernetIPV6, self).setUp()
128         for p in self.packets:
129             self.packets.remove(p)
130         self.p2p_sub_ifs.append(
131             self.create_p2p_ethernet(self.pg0, 1,
132                                      self.pg0._remote_hosts[0].mac))
133         self.p2p_sub_ifs.append(
134             self.create_p2p_ethernet(self.pg0, 2,
135                                      self.pg0._remote_hosts[1].mac))
136         self.vapi.cli("trace add p2p-ethernet-input 50")
137
138     def tearDown(self):
139         while len(self.p2p_sub_ifs):
140             p2p = self.p2p_sub_ifs.pop()
141             self.delete_p2p_ethernet(p2p)
142
143         super(P2PEthernetIPV6, self).tearDown()
144
145     def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
146         p2p = VppP2PSubint(self, parent_if, sub_id, mactobinary(remote_mac))
147         p2p.admin_up()
148         p2p.config_ip6()
149         p2p.disable_ipv6_ra()
150         return p2p
151
152     def delete_p2p_ethernet(self, p2p):
153         p2p.unconfig_ip6()
154         p2p.admin_down()
155         self.vapi.delete_p2pethernet_subif(p2p.parent.sw_if_index,
156                                            p2p.p2p_remote_mac)
157
158     def create_stream(self, src_mac=None, dst_mac=None,
159                       src_ip=None, dst_ip=None, size=None):
160         pkt_size = size
161         if size is None:
162             pkt_size = random.choice(self.pg_if_packet_sizes)
163         p = Ether(src=src_mac, dst=dst_mac)
164         p /= IPv6(src=src_ip, dst=dst_ip)
165         p /= (UDP(sport=1234, dport=4321) / Raw('\xa5' * 20))
166         self.extend_packet(p, pkt_size)
167         return p
168
169     def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
170         self.pg_enable_capture([dst_if])
171         if packets is None:
172             packets = self.packets
173         src_if.add_stream(packets)
174         self.pg_start()
175         if count is None:
176             count = len(packets)
177         return dst_if.get_capture(count)
178
179     def verify_counters(self, counter_id, expected_value):
180         counters = self.vapi.cli("sh errors").split('\n')
181         counter_value = -1
182         for i in range(1, len(counters)-1):
183             results = counters[i].split()
184             if results[1] == counter_id:
185                 counter_value = int(results[0])
186                 break
187         self.assertEqual(counter_value, expected_value)
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.verify_counters('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     def setUp(self):
362         super(P2PEthernetIPV4, self).setUp()
363         for p in self.packets:
364             self.packets.remove(p)
365         self.p2p_sub_ifs.append(
366             self.create_p2p_ethernet(self.pg0, 1,
367                                      self.pg0._remote_hosts[0].mac))
368         self.p2p_sub_ifs.append(
369             self.create_p2p_ethernet(self.pg0, 2,
370                                      self.pg0._remote_hosts[1].mac))
371         self.vapi.cli("trace add p2p-ethernet-input 50")
372
373     def tearDown(self):
374         while len(self.p2p_sub_ifs):
375             p2p = self.p2p_sub_ifs.pop()
376             self.delete_p2p_ethernet(p2p)
377         super(P2PEthernetIPV4, self).tearDown()
378
379     def create_stream(self, src_mac=None, dst_mac=None,
380                       src_ip=None, dst_ip=None, size=None):
381         pkt_size = size
382         if size is None:
383             pkt_size = random.choice(self.pg_if_packet_sizes)
384         p = Ether(src=src_mac, dst=dst_mac)
385         p /= IP(src=src_ip, dst=dst_ip)
386         p /= (UDP(sport=1234, dport=4321) / Raw('\xa5' * 20))
387         self.extend_packet(p, pkt_size)
388         return p
389
390     def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
391         self.pg_enable_capture([dst_if])
392         if packets is None:
393             packets = self.packets
394         src_if.add_stream(packets)
395         self.pg_start()
396         if count is None:
397             count = len(packets)
398         return dst_if.get_capture(count)
399
400     def verify_counters(self, counter_id, expected_value):
401         counters = self.vapi.cli("sh errors").split('\n')
402         counter_value = -1
403         for i in range(1, len(counters)-1):
404             results = counters[i].split()
405             if results[1] == counter_id:
406                 counter_value = int(results[0])
407                 break
408         self.assertEqual(counter_value, expected_value)
409
410     def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
411         p2p = VppP2PSubint(self, parent_if, sub_id, mactobinary(remote_mac))
412         p2p.admin_up()
413         p2p.config_ip4()
414         return p2p
415
416     def delete_p2p_ethernet(self, p2p):
417         p2p.unconfig_ip4()
418         p2p.admin_down()
419         self.vapi.delete_p2pethernet_subif(p2p.parent.sw_if_index,
420                                            p2p.p2p_remote_mac)
421
422     def test_ip4_rx_p2p_subif(self):
423         """receive ipv4 packet via p2p subinterface"""
424         self.logger.info("FFP_TEST_START_0002")
425
426         route_9000 = VppIpRoute(self, "9.0.0.0", 16,
427                                 [VppRoutePath(self.pg1.remote_ip4,
428                                               self.pg1.sw_if_index)])
429         route_9000.add_vpp_config()
430
431         self.packets.append(
432             self.create_stream(src_mac=self.pg0._remote_hosts[0].mac,
433                                dst_mac=self.pg0.local_mac,
434                                src_ip=self.p2p_sub_ifs[0].remote_ip4,
435                                dst_ip="9.0.0.100"))
436
437         self.send_packets(self.pg0, self.pg1, self.packets)
438
439         self.verify_counters('p2p-ethernet-input', 1)
440
441         route_9000.remove_vpp_config()
442         self.logger.info("FFP_TEST_FINISH_0002")
443
444     def test_ip4_rx_p2p_subif_route(self):
445         """route rx packet not matching p2p subinterface"""
446         self.logger.info("FFP_TEST_START_0003")
447
448         route_9001 = VppIpRoute(self, "9.0.0.0", 24,
449                                 [VppRoutePath(self.pg1.remote_ip4,
450                                               self.pg1.sw_if_index)])
451         route_9001.add_vpp_config()
452
453         self.packets.append(
454             self.create_stream(src_mac="02:01:00:00:ff:ff",
455                                dst_mac=self.pg0.local_mac,
456                                src_ip="8.0.0.100",
457                                dst_ip="9.0.0.100"))
458
459         self.send_packets(self.pg0, self.pg1)
460
461         route_9001.remove_vpp_config()
462
463         self.logger.info("FFP_TEST_FINISH_0003")
464
465     def test_ip4_tx_p2p_subif(self):
466         """send ip4 packet via p2p subinterface"""
467         self.logger.info("FFP_TEST_START_0005")
468
469         route_9100 = VppIpRoute(self, "9.1.0.100", 24,
470                                 [VppRoutePath(self.pg0.remote_ip4,
471                                               self.pg0.sw_if_index,
472                                               )])
473         route_9100.add_vpp_config()
474         route_9200 = VppIpRoute(self, "9.2.0.100", 24,
475                                 [VppRoutePath(self.p2p_sub_ifs[0].remote_ip4,
476                                               self.p2p_sub_ifs[0].sw_if_index,
477                                               )])
478         route_9200.add_vpp_config()
479         route_9300 = VppIpRoute(self, "9.3.0.100", 24,
480                                 [VppRoutePath(self.p2p_sub_ifs[1].remote_ip4,
481                                               self.p2p_sub_ifs[1].sw_if_index
482                                               )])
483         route_9300.add_vpp_config()
484
485         for i in range(0, 3):
486             self.packets.append(
487                 self.create_stream(src_mac=self.pg1.remote_mac,
488                                    dst_mac=self.pg1.local_mac,
489                                    src_ip=self.pg1.remote_ip4,
490                                    dst_ip="9.%d.0.100" % (i+1)))
491
492         self.send_packets(self.pg1, self.pg0)
493
494         # route_7000.remove_vpp_config()
495         route_9100.remove_vpp_config()
496         route_9200.remove_vpp_config()
497         route_9300.remove_vpp_config()
498
499         self.logger.info("FFP_TEST_FINISH_0005")
500
501     def test_ip4_tx_p2p_subif_drop(self):
502         """drop tx ip4 packet not matching p2p subinterface"""
503         self.logger.info("FFP_TEST_START_0006")
504
505         self.packets.append(
506             self.create_stream(src_mac="02:01:00:00:ff:ff",
507                                dst_mac=self.pg0.local_mac,
508                                src_ip="8.0.0.100",
509                                dst_ip="9.0.0.100"))
510
511         # no packet received
512         self.send_packets(self.pg0, self.pg1, count=0)
513         self.logger.info("FFP_TEST_FINISH_0006")
514
515
516 if __name__ == '__main__':
517     unittest.main(testRunner=VppTestRunner)