ip: IP6 incorrectly disabled on removing first ip6 prefix
[vpp.git] / test / test_ip_mcast.py
1 #!/usr/bin/env python3
2
3 import unittest
4
5 from framework import VppTestCase, VppTestRunner
6 from vpp_ip import DpoProto
7 from vpp_ip_route import VppIpMRoute, VppMRoutePath, VppMFibSignal, \
8     MRouteItfFlags, MRouteEntryFlags, VppIpTable, FibPathProto
9 from vpp_gre_interface import VppGreInterface
10
11 from scapy.packet import Raw
12 from scapy.layers.l2 import Ether, GRE
13 from scapy.layers.inet import IP, UDP, getmacbyip
14 from scapy.layers.inet6 import IPv6, getmacbyip6
15
16 #
17 # The number of packets sent is set to 91 so that when we replicate more than 3
18 # times, which we do for some entries, we will generate more than 256 packets
19 # to the next node in the VLIB graph. Thus we are testing the code's
20 # correctness handling this over-flow.
21 # It's also an odd number so we hit any single loops.
22 #
23 N_PKTS_IN_STREAM = 91
24
25
26 class TestMFIB(VppTestCase):
27     """ MFIB Test Case """
28
29     @classmethod
30     def setUpClass(cls):
31         super(TestMFIB, cls).setUpClass()
32
33     @classmethod
34     def tearDownClass(cls):
35         super(TestMFIB, cls).tearDownClass()
36
37     def setUp(self):
38         super(TestMFIB, self).setUp()
39
40     def test_mfib(self):
41         """ MFIB Unit Tests """
42         error = self.vapi.cli("test mfib")
43
44         if error:
45             self.logger.critical(error)
46         self.assertNotIn("Failed", error)
47
48
49 class TestIPMcast(VppTestCase):
50     """ IP Multicast Test Case """
51
52     @classmethod
53     def setUpClass(cls):
54         super(TestIPMcast, cls).setUpClass()
55
56     @classmethod
57     def tearDownClass(cls):
58         super(TestIPMcast, cls).tearDownClass()
59
60     def setUp(self):
61         super(TestIPMcast, self).setUp()
62
63         # create 8 pg interfaces
64         self.create_pg_interfaces(range(9))
65
66         # setup interfaces
67         for i in self.pg_interfaces[:8]:
68             i.admin_up()
69             i.config_ip4()
70             i.config_ip6()
71             i.resolve_arp()
72             i.resolve_ndp()
73
74         # one more in a vrf
75         tbl4 = VppIpTable(self, 10)
76         tbl4.add_vpp_config()
77         self.pg8.set_table_ip4(10)
78         self.pg8.config_ip4()
79
80         tbl6 = VppIpTable(self, 10, is_ip6=1)
81         tbl6.add_vpp_config()
82         self.pg8.set_table_ip6(10)
83         self.pg8.config_ip6()
84
85     def tearDown(self):
86         for i in self.pg_interfaces:
87             i.unconfig_ip4()
88             i.unconfig_ip6()
89             i.admin_down()
90
91         self.pg8.set_table_ip4(0)
92         self.pg8.set_table_ip6(0)
93         super(TestIPMcast, self).tearDown()
94
95     def create_stream_ip4(self, src_if, src_ip, dst_ip, payload_size=0):
96         pkts = []
97         # default to small packet sizes
98         p = (Ether(dst=getmacbyip(dst_ip), src=src_if.remote_mac) /
99              IP(src=src_ip, dst=dst_ip) /
100              UDP(sport=1234, dport=1234))
101         if not payload_size:
102             payload_size = 64 - len(p)
103             p = p / Raw(b'\xa5' * payload_size)
104
105         for i in range(0, N_PKTS_IN_STREAM):
106             pkts.append(p)
107         return pkts
108
109     def create_stream_ip6(self, src_if, src_ip, dst_ip):
110         pkts = []
111         for i in range(0, N_PKTS_IN_STREAM):
112             info = self.create_packet_info(src_if, src_if)
113             payload = self.info_to_payload(info)
114             p = (Ether(dst=getmacbyip6(dst_ip), src=src_if.remote_mac) /
115                  IPv6(src=src_ip, dst=dst_ip) /
116                  UDP(sport=1234, dport=1234) /
117                  Raw(payload))
118             info.data = p.copy()
119             pkts.append(p)
120         return pkts
121
122     def verify_filter(self, capture, sent):
123         if not len(capture) == len(sent):
124             # filter out any IPv6 RAs from the capture
125             for p in capture:
126                 if (p.haslayer(IPv6)):
127                     capture.remove(p)
128         return capture
129
130     def verify_capture_ip4(self, rx_if, sent, dst_mac=None):
131         rxd = rx_if.get_capture(len(sent))
132
133         try:
134             capture = self.verify_filter(rxd, sent)
135
136             self.assertEqual(len(capture), len(sent))
137
138             for i in range(len(capture)):
139                 tx = sent[i]
140                 rx = capture[i]
141
142                 eth = rx[Ether]
143                 self.assertEqual(eth.type, 0x800)
144
145                 tx_ip = tx[IP]
146                 rx_ip = rx[IP]
147
148                 if dst_mac is None:
149                     dst_mac = getmacbyip(rx_ip.dst)
150
151                 # check the MAC address on the RX'd packet is correctly formed
152                 self.assertEqual(eth.dst, dst_mac)
153
154                 self.assertEqual(rx_ip.src, tx_ip.src)
155                 self.assertEqual(rx_ip.dst, tx_ip.dst)
156                 # IP processing post pop has decremented the TTL
157                 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
158
159         except:
160             raise
161
162     def verify_capture_ip6(self, rx_if, sent):
163         capture = rx_if.get_capture(len(sent))
164
165         self.assertEqual(len(capture), len(sent))
166
167         for i in range(len(capture)):
168             tx = sent[i]
169             rx = capture[i]
170
171             eth = rx[Ether]
172             self.assertEqual(eth.type, 0x86DD)
173
174             tx_ip = tx[IPv6]
175             rx_ip = rx[IPv6]
176
177             # check the MAC address on the RX'd packet is correctly formed
178             self.assertEqual(eth.dst, getmacbyip6(rx_ip.dst))
179
180             self.assertEqual(rx_ip.src, tx_ip.src)
181             self.assertEqual(rx_ip.dst, tx_ip.dst)
182             # IP processing post pop has decremented the TTL
183             self.assertEqual(rx_ip.hlim + 1, tx_ip.hlim)
184
185     def test_ip_mcast(self):
186         """ IP Multicast Replication """
187
188         #
189         # a stream that matches the default route. gets dropped.
190         #
191         self.vapi.cli("clear trace")
192         self.vapi.cli("packet mac-filter pg0 on")
193         self.vapi.cli("packet mac-filter pg1 on")
194         self.vapi.cli("packet mac-filter pg2 on")
195         self.vapi.cli("packet mac-filter pg4 on")
196         self.vapi.cli("packet mac-filter pg5 on")
197         self.vapi.cli("packet mac-filter pg6 on")
198         self.vapi.cli("packet mac-filter pg7 on")
199
200         tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
201         self.pg0.add_stream(tx)
202
203         self.pg_enable_capture(self.pg_interfaces)
204         self.pg_start()
205
206         self.pg0.assert_nothing_captured(
207             remark="IP multicast packets forwarded on default route")
208
209         #
210         # A (*,G).
211         # one accepting interface, pg0, 7 forwarding interfaces
212         #  many forwarding interfaces test the case where the replicate DPO
213         #  needs to use extra cache lines for the buckets.
214         #
215         route_232_1_1_1 = VppIpMRoute(
216             self,
217             "0.0.0.0",
218             "232.1.1.1", 32,
219             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
220             [VppMRoutePath(self.pg0.sw_if_index,
221                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
222              VppMRoutePath(self.pg1.sw_if_index,
223                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
224              VppMRoutePath(self.pg2.sw_if_index,
225                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
226              VppMRoutePath(self.pg3.sw_if_index,
227                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
228              VppMRoutePath(self.pg4.sw_if_index,
229                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
230              VppMRoutePath(self.pg5.sw_if_index,
231                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
232              VppMRoutePath(self.pg6.sw_if_index,
233                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
234              VppMRoutePath(self.pg7.sw_if_index,
235                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
236         route_232_1_1_1.add_vpp_config()
237
238         #
239         # An (S,G).
240         # one accepting interface, pg0, 2 forwarding interfaces
241         #
242         route_1_1_1_1_232_1_1_1 = VppIpMRoute(
243             self,
244             "1.1.1.1",
245             "232.1.1.1", 27,  # any grp-len is ok when src is set
246             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
247             [VppMRoutePath(self.pg0.sw_if_index,
248                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
249              VppMRoutePath(self.pg1.sw_if_index,
250                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
251              VppMRoutePath(self.pg2.sw_if_index,
252                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
253         route_1_1_1_1_232_1_1_1.add_vpp_config()
254
255         #
256         # An (S,G).
257         # one accepting interface, pg0, 2 forwarding interfaces
258         # that use unicast next-hops
259         #
260         route_1_1_1_1_232_1_1_2 = VppIpMRoute(
261             self,
262             "1.1.1.1",
263             "232.1.1.2", 64,
264             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
265             [VppMRoutePath(self.pg0.sw_if_index,
266                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
267              VppMRoutePath(self.pg1.sw_if_index,
268                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
269                            nh=self.pg1.remote_ip4),
270              VppMRoutePath(self.pg2.sw_if_index,
271                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
272                            nh=self.pg2.remote_ip4)])
273         route_1_1_1_1_232_1_1_2.add_vpp_config()
274
275         #
276         # An (*,G/m).
277         # one accepting interface, pg0, 1 forwarding interfaces
278         #
279         route_232 = VppIpMRoute(
280             self,
281             "0.0.0.0",
282             "232.0.0.0", 8,
283             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
284             [VppMRoutePath(self.pg0.sw_if_index,
285                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
286              VppMRoutePath(self.pg1.sw_if_index,
287                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
288         route_232.add_vpp_config()
289
290         #
291         # a stream that matches the route for (1.1.1.1,232.1.1.1)
292         #  small packets
293         #
294         self.vapi.cli("clear trace")
295         tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
296         self.pg0.add_stream(tx)
297
298         self.pg_enable_capture(self.pg_interfaces)
299         self.pg_start()
300
301         self.assertEqual(route_1_1_1_1_232_1_1_1.get_stats()['packets'],
302                          len(tx))
303
304         # We expect replications on Pg1->7
305         self.verify_capture_ip4(self.pg1, tx)
306         self.verify_capture_ip4(self.pg2, tx)
307
308         # no replications on Pg0
309         self.pg0.assert_nothing_captured(
310             remark="IP multicast packets forwarded on PG0")
311         self.pg3.assert_nothing_captured(
312             remark="IP multicast packets forwarded on PG3")
313
314         #
315         # a stream that matches the route for (1.1.1.1,232.1.1.1)
316         #  large packets
317         #
318         self.vapi.cli("clear trace")
319         tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1",
320                                     payload_size=1024)
321         self.pg0.add_stream(tx)
322
323         self.pg_enable_capture(self.pg_interfaces)
324         self.pg_start()
325
326         # We expect replications on Pg1->7
327         self.verify_capture_ip4(self.pg1, tx)
328         self.verify_capture_ip4(self.pg2, tx)
329
330         self.assertEqual(route_1_1_1_1_232_1_1_1.get_stats()['packets'],
331                          2*len(tx))
332
333         # no replications on Pg0
334         self.pg0.assert_nothing_captured(
335             remark="IP multicast packets forwarded on PG0")
336         self.pg3.assert_nothing_captured(
337             remark="IP multicast packets forwarded on PG3")
338
339         #
340         # a stream to the unicast next-hops
341         #
342         self.vapi.cli("clear trace")
343         tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.2")
344         self.pg0.add_stream(tx)
345
346         self.pg_enable_capture(self.pg_interfaces)
347         self.pg_start()
348
349         # We expect replications on Pg1->7
350         self.verify_capture_ip4(self.pg1, tx, dst_mac=self.pg1.remote_mac)
351         self.verify_capture_ip4(self.pg2, tx, dst_mac=self.pg2.remote_mac)
352
353         # no replications on Pg0 nor pg3
354         self.pg0.assert_nothing_captured(
355             remark="IP multicast packets forwarded on PG0")
356         self.pg3.assert_nothing_captured(
357             remark="IP multicast packets forwarded on PG3")
358
359         #
360         # a stream that matches the route for (*,232.0.0.0/8)
361         # Send packets with the 9th bit set so we test the correct clearing
362         # of that bit in the mac rewrite
363         #
364         self.vapi.cli("clear trace")
365         tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.255.255.255")
366         self.pg0.add_stream(tx)
367
368         self.pg_enable_capture(self.pg_interfaces)
369         self.pg_start()
370
371         # We expect replications on Pg1 only
372         self.verify_capture_ip4(self.pg1, tx)
373         self.assertEqual(route_232.get_stats()['packets'], len(tx))
374
375         # no replications on Pg0, Pg2 not Pg3
376         self.pg0.assert_nothing_captured(
377             remark="IP multicast packets forwarded on PG0")
378         self.pg2.assert_nothing_captured(
379             remark="IP multicast packets forwarded on PG2")
380         self.pg3.assert_nothing_captured(
381             remark="IP multicast packets forwarded on PG3")
382
383         #
384         # a stream that matches the route for (*,232.1.1.1)
385         #
386         self.vapi.cli("clear trace")
387         tx = self.create_stream_ip4(self.pg0, "1.1.1.2", "232.1.1.1")
388         self.pg0.add_stream(tx)
389
390         self.pg_enable_capture(self.pg_interfaces)
391         self.pg_start()
392
393         # We expect replications on Pg1->7
394         self.verify_capture_ip4(self.pg1, tx)
395         self.verify_capture_ip4(self.pg2, tx)
396         self.verify_capture_ip4(self.pg3, tx)
397         self.verify_capture_ip4(self.pg4, tx)
398         self.verify_capture_ip4(self.pg5, tx)
399         self.verify_capture_ip4(self.pg6, tx)
400         self.verify_capture_ip4(self.pg7, tx)
401
402         # no replications on Pg0
403         self.pg0.assert_nothing_captured(
404             remark="IP multicast packets forwarded on PG0")
405
406         self.vapi.cli("packet mac-filter pg0 off")
407         self.vapi.cli("packet mac-filter pg1 off")
408         self.vapi.cli("packet mac-filter pg2 off")
409         self.vapi.cli("packet mac-filter pg4 off")
410         self.vapi.cli("packet mac-filter pg5 off")
411         self.vapi.cli("packet mac-filter pg6 off")
412         self.vapi.cli("packet mac-filter pg7 off")
413
414     def test_ip6_mcast(self):
415         """ IPv6 Multicast Replication """
416
417         self.vapi.cli("packet mac-filter pg0 on")
418         self.vapi.cli("packet mac-filter pg1 on")
419         self.vapi.cli("packet mac-filter pg2 on")
420         self.vapi.cli("packet mac-filter pg4 on")
421         self.vapi.cli("packet mac-filter pg5 on")
422         self.vapi.cli("packet mac-filter pg6 on")
423         self.vapi.cli("packet mac-filter pg7 on")
424         #
425         # a stream that matches the default route. gets dropped.
426         #
427         self.vapi.cli("clear trace")
428         tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
429         self.pg0.add_stream(tx)
430
431         self.pg_enable_capture(self.pg_interfaces)
432         self.pg_start()
433
434         self.pg0.assert_nothing_captured(
435             remark="IPv6 multicast packets forwarded on default route")
436
437         #
438         # A (*,G).
439         # one accepting interface, pg0, 3 forwarding interfaces
440         #
441         route_ff01_1 = VppIpMRoute(
442             self,
443             "::",
444             "ff01::1", 128,
445             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
446             [VppMRoutePath(self.pg0.sw_if_index,
447                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT,
448                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6),
449              VppMRoutePath(self.pg1.sw_if_index,
450                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
451                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6),
452              VppMRoutePath(self.pg2.sw_if_index,
453                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
454                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6),
455              VppMRoutePath(self.pg3.sw_if_index,
456                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
457                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)])
458         route_ff01_1.add_vpp_config()
459
460         #
461         # An (S,G).
462         # one accepting interface, pg0, 2 forwarding interfaces
463         #
464         route_2001_ff01_1 = VppIpMRoute(
465             self,
466             "2001::1",
467             "ff01::1", 0,  # any grp-len is ok when src is set
468             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
469             [VppMRoutePath(self.pg0.sw_if_index,
470                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT,
471                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6),
472              VppMRoutePath(self.pg1.sw_if_index,
473                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
474                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6),
475              VppMRoutePath(self.pg2.sw_if_index,
476                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
477                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)])
478         route_2001_ff01_1.add_vpp_config()
479
480         #
481         # An (*,G/m).
482         # one accepting interface, pg0, 1 forwarding interface
483         #
484         route_ff01 = VppIpMRoute(
485             self,
486             "::",
487             "ff01::", 16,
488             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
489             [VppMRoutePath(self.pg0.sw_if_index,
490                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT,
491                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6),
492              VppMRoutePath(self.pg1.sw_if_index,
493                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
494                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)])
495         route_ff01.add_vpp_config()
496
497         #
498         # a stream that matches the route for (*, ff01::/16)
499         # sent on the non-accepting interface
500         #
501         self.vapi.cli("clear trace")
502         tx = self.create_stream_ip6(self.pg1, "2002::1", "ff01:2::255")
503         self.send_and_assert_no_replies(self.pg1, tx, "RPF miss")
504
505         #
506         # a stream that matches the route for (*, ff01::/16)
507         # sent on the accepting interface
508         #
509         self.vapi.cli("clear trace")
510         tx = self.create_stream_ip6(self.pg0, "2002::1", "ff01:2::255")
511         self.pg0.add_stream(tx)
512
513         self.pg_enable_capture(self.pg_interfaces)
514         self.pg_start()
515
516         # We expect replications on Pg1
517         self.verify_capture_ip6(self.pg1, tx)
518
519         # no replications on Pg0, Pg3
520         self.pg0.assert_nothing_captured(
521             remark="IP multicast packets forwarded on PG0")
522         self.pg2.assert_nothing_captured(
523             remark="IP multicast packets forwarded on PG2")
524         self.pg3.assert_nothing_captured(
525             remark="IP multicast packets forwarded on PG3")
526
527         #
528         # Bounce the interface and it should still work
529         #
530         self.pg1.admin_down()
531         self.pg0.add_stream(tx)
532         self.pg_enable_capture(self.pg_interfaces)
533         self.pg_start()
534         self.pg1.assert_nothing_captured(
535             remark="IP multicast packets forwarded on down PG1")
536
537         self.pg1.admin_up()
538         self.pg0.add_stream(tx)
539         self.pg_enable_capture(self.pg_interfaces)
540         self.pg_start()
541         self.verify_capture_ip6(self.pg1, tx)
542
543         #
544         # a stream that matches the route for (*,ff01::1)
545         #
546         self.vapi.cli("clear trace")
547         tx = self.create_stream_ip6(self.pg0, "2002::2", "ff01::1")
548         self.pg0.add_stream(tx)
549
550         self.pg_enable_capture(self.pg_interfaces)
551         self.pg_start()
552
553         # We expect replications on Pg1, 2, 3.
554         self.verify_capture_ip6(self.pg1, tx)
555         self.verify_capture_ip6(self.pg2, tx)
556         self.verify_capture_ip6(self.pg3, tx)
557
558         # no replications on Pg0
559         self.pg0.assert_nothing_captured(
560             remark="IPv6 multicast packets forwarded on PG0")
561
562         #
563         # a stream that matches the route for (2001::1, ff00::1)
564         #
565         self.vapi.cli("clear trace")
566         tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
567         self.pg0.add_stream(tx)
568
569         self.pg_enable_capture(self.pg_interfaces)
570         self.pg_start()
571
572         # We expect replications on Pg1, 2,
573         self.verify_capture_ip6(self.pg1, tx)
574         self.verify_capture_ip6(self.pg2, tx)
575
576         # no replications on Pg0, Pg3
577         self.pg0.assert_nothing_captured(
578             remark="IP multicast packets forwarded on PG0")
579         self.pg3.assert_nothing_captured(
580             remark="IP multicast packets forwarded on PG3")
581
582         self.vapi.cli("packet mac-filter pg0 off")
583         self.vapi.cli("packet mac-filter pg1 off")
584         self.vapi.cli("packet mac-filter pg2 off")
585         self.vapi.cli("packet mac-filter pg4 off")
586         self.vapi.cli("packet mac-filter pg5 off")
587         self.vapi.cli("packet mac-filter pg6 off")
588         self.vapi.cli("packet mac-filter pg7 off")
589
590     def _mcast_connected_send_stream(self, dst_ip):
591         self.vapi.cli("clear trace")
592         tx = self.create_stream_ip4(self.pg0,
593                                     self.pg0.remote_ip4,
594                                     dst_ip)
595         self.pg0.add_stream(tx)
596
597         self.pg_enable_capture(self.pg_interfaces)
598         self.pg_start()
599
600         # We expect replications on Pg1.
601         self.verify_capture_ip4(self.pg1, tx)
602
603         return tx
604
605     def test_ip_mcast_connected(self):
606         """ IP Multicast Connected Source check """
607
608         #
609         # A (*,G).
610         # one accepting interface, pg0, 1 forwarding interfaces
611         #
612         route_232_1_1_1 = VppIpMRoute(
613             self,
614             "0.0.0.0",
615             "232.1.1.1", 32,
616             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
617             [VppMRoutePath(self.pg0.sw_if_index,
618                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
619              VppMRoutePath(self.pg1.sw_if_index,
620                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
621
622         route_232_1_1_1.add_vpp_config()
623         route_232_1_1_1.update_entry_flags(
624             MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
625
626         #
627         # Now the (*,G) is present, send from connected source
628         #
629         tx = self._mcast_connected_send_stream("232.1.1.1")
630
631         #
632         # Constrct a representation of the signal we expect on pg0
633         #
634         signal_232_1_1_1_itf_0 = VppMFibSignal(self,
635                                                route_232_1_1_1,
636                                                self.pg0.sw_if_index,
637                                                tx[0])
638
639         #
640         # read the only expected signal
641         #
642         signals = self.vapi.mfib_signal_dump()
643
644         self.assertEqual(1, len(signals))
645
646         signal_232_1_1_1_itf_0.compare(signals[0])
647
648         #
649         # reading the signal allows for the generation of another
650         # so send more packets and expect the next signal
651         #
652         tx = self._mcast_connected_send_stream("232.1.1.1")
653
654         signals = self.vapi.mfib_signal_dump()
655         self.assertEqual(1, len(signals))
656         signal_232_1_1_1_itf_0.compare(signals[0])
657
658         #
659         # A Second entry with connected check
660         # one accepting interface, pg0, 1 forwarding interfaces
661         #
662         route_232_1_1_2 = VppIpMRoute(
663             self,
664             "0.0.0.0",
665             "232.1.1.2", 32,
666             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
667             [VppMRoutePath(self.pg0.sw_if_index,
668                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
669              VppMRoutePath(self.pg1.sw_if_index,
670                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
671
672         route_232_1_1_2.add_vpp_config()
673         route_232_1_1_2.update_entry_flags(
674             MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
675
676         #
677         # Send traffic to both entries. One read should net us two signals
678         #
679         signal_232_1_1_2_itf_0 = VppMFibSignal(self,
680                                                route_232_1_1_2,
681                                                self.pg0.sw_if_index,
682                                                tx[0])
683         tx = self._mcast_connected_send_stream("232.1.1.1")
684         tx2 = self._mcast_connected_send_stream("232.1.1.2")
685
686         #
687         # read the only expected signal
688         #
689         signals = self.vapi.mfib_signal_dump()
690
691         self.assertEqual(2, len(signals))
692
693         signal_232_1_1_1_itf_0.compare(signals[1])
694         signal_232_1_1_2_itf_0.compare(signals[0])
695
696         route_232_1_1_1.update_entry_flags(
697             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE)
698         route_232_1_1_2.update_entry_flags(
699             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE)
700
701     def test_ip_mcast_signal(self):
702         """ IP Multicast Signal """
703
704         #
705         # A (*,G).
706         # one accepting interface, pg0, 1 forwarding interfaces
707         #
708         route_232_1_1_1 = VppIpMRoute(
709             self,
710             "0.0.0.0",
711             "232.1.1.1", 32,
712             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
713             [VppMRoutePath(self.pg0.sw_if_index,
714                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
715              VppMRoutePath(self.pg1.sw_if_index,
716                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
717
718         route_232_1_1_1.add_vpp_config()
719
720         route_232_1_1_1.update_entry_flags(
721             MRouteEntryFlags.MFIB_ENTRY_FLAG_SIGNAL)
722
723         #
724         # Now the (*,G) is present, send from connected source
725         #
726         tx = self._mcast_connected_send_stream("232.1.1.1")
727
728         #
729         # Constrct a representation of the signal we expect on pg0
730         #
731         signal_232_1_1_1_itf_0 = VppMFibSignal(self,
732                                                route_232_1_1_1,
733                                                self.pg0.sw_if_index,
734                                                tx[0])
735
736         #
737         # read the only expected signal
738         #
739         signals = self.vapi.mfib_signal_dump()
740
741         self.assertEqual(1, len(signals))
742
743         signal_232_1_1_1_itf_0.compare(signals[0])
744
745         #
746         # reading the signal allows for the generation of another
747         # so send more packets and expect the next signal
748         #
749         tx = self._mcast_connected_send_stream("232.1.1.1")
750
751         signals = self.vapi.mfib_signal_dump()
752         self.assertEqual(1, len(signals))
753         signal_232_1_1_1_itf_0.compare(signals[0])
754
755         #
756         # Set the negate-signal on the accepting interval - the signals
757         # should stop
758         #
759         route_232_1_1_1.update_path_flags(
760             self.pg0.sw_if_index,
761             (MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
762              MRouteItfFlags.MFIB_ITF_FLAG_NEGATE_SIGNAL))
763
764         self.vapi.cli("clear trace")
765         tx = self._mcast_connected_send_stream("232.1.1.1")
766
767         signals = self.vapi.mfib_signal_dump()
768         self.assertEqual(0, len(signals))
769
770         #
771         # Clear the SIGNAL flag on the entry and the signals should
772         # come back since the interface is still NEGATE-SIGNAL
773         #
774         route_232_1_1_1.update_entry_flags(
775             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE)
776
777         tx = self._mcast_connected_send_stream("232.1.1.1")
778
779         signals = self.vapi.mfib_signal_dump()
780         self.assertEqual(1, len(signals))
781         signal_232_1_1_1_itf_0.compare(signals[0])
782
783         #
784         # Lastly remove the NEGATE-SIGNAL from the interface and the
785         # signals should stop
786         #
787         route_232_1_1_1.update_path_flags(self.pg0.sw_if_index,
788                                           MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT)
789
790         tx = self._mcast_connected_send_stream("232.1.1.1")
791         signals = self.vapi.mfib_signal_dump()
792         self.assertEqual(0, len(signals))
793
794     def test_ip_mcast_vrf(self):
795         """ IP Multicast Replication in non-default table"""
796
797         #
798         # An (S,G).
799         # one accepting interface, pg0, 2 forwarding interfaces
800         #
801         route_1_1_1_1_232_1_1_1 = VppIpMRoute(
802             self,
803             "1.1.1.1",
804             "232.1.1.1", 64,
805             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
806             [VppMRoutePath(self.pg8.sw_if_index,
807                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
808              VppMRoutePath(self.pg1.sw_if_index,
809                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
810              VppMRoutePath(self.pg2.sw_if_index,
811                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
812             table_id=10)
813         route_1_1_1_1_232_1_1_1.add_vpp_config()
814
815         #
816         # a stream that matches the route for (1.1.1.1,232.1.1.1)
817         #  small packets
818         #
819         self.vapi.cli("clear trace")
820         tx = self.create_stream_ip4(self.pg8, "1.1.1.1", "232.1.1.1")
821         self.pg8.add_stream(tx)
822
823         self.pg_enable_capture(self.pg_interfaces)
824         self.pg_start()
825
826         # We expect replications on Pg1 & 2
827         self.verify_capture_ip4(self.pg1, tx)
828         self.verify_capture_ip4(self.pg2, tx)
829
830     def test_ip_mcast_gre(self):
831         """ IP Multicast Replication over GRE"""
832
833         gre_if_1 = VppGreInterface(
834             self,
835             self.pg1.local_ip4,
836             self.pg1.remote_ip4).add_vpp_config()
837         gre_if_2 = VppGreInterface(
838             self,
839             self.pg2.local_ip4,
840             self.pg2.remote_ip4).add_vpp_config()
841         gre_if_3 = VppGreInterface(
842             self,
843             self.pg3.local_ip4,
844             self.pg3.remote_ip4).add_vpp_config()
845
846         gre_if_1.admin_up()
847         gre_if_1.config_ip4()
848         gre_if_2.admin_up()
849         gre_if_2.config_ip4()
850         gre_if_3.admin_up()
851         gre_if_3.config_ip4()
852
853         #
854         # An (S,G).
855         # one accepting interface, pg0, 2 forwarding interfaces
856         #
857         route_1_1_1_1_232_1_1_1 = VppIpMRoute(
858             self,
859             "1.1.1.1",
860             "232.2.2.2", 64,
861             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
862             [VppMRoutePath(gre_if_1.sw_if_index,
863                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
864              VppMRoutePath(gre_if_2.sw_if_index,
865                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
866              VppMRoutePath(gre_if_3.sw_if_index,
867                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
868         route_1_1_1_1_232_1_1_1.add_vpp_config()
869
870         #
871         # a stream that matches the route for (1.1.1.1,232.2.2.2)
872         #  small packets
873         #
874         tx = (Ether(dst=self.pg1.local_mac,
875                     src=self.pg1.remote_mac) /
876               IP(src=self.pg1.remote_ip4,
877                  dst=self.pg1.local_ip4) /
878               GRE() /
879               IP(src="1.1.1.1", dst="232.2.2.2") /
880               UDP(sport=1234, dport=1234) /
881               Raw(b'\a5' * 64)) * 63
882
883         self.vapi.cli("clear trace")
884         self.pg1.add_stream(tx)
885
886         self.pg_enable_capture(self.pg_interfaces)
887         self.pg_start()
888
889         # We expect replications on Pg2 & 3
890         # check the encap headers are as expected based on the egress tunnel
891         rxs = self.pg2.get_capture(len(tx))
892         for rx in rxs:
893             self.assertEqual(rx[IP].src, gre_if_2.t_src)
894             self.assertEqual(rx[IP].dst, gre_if_2.t_dst)
895             self.assert_packet_checksums_valid(rx)
896
897         rxs = self.pg3.get_capture(len(tx))
898         for rx in rxs:
899             self.assertEqual(rx[IP].src, gre_if_3.t_src)
900             self.assertEqual(rx[IP].dst, gre_if_3.t_dst)
901             self.assert_packet_checksums_valid(rx)
902
903     def test_ip6_mcast_vrf(self):
904         """ IPv6 Multicast Replication in non-default table"""
905
906         #
907         # An (S,G).
908         # one accepting interface, pg0, 2 forwarding interfaces
909         #
910         route_2001_ff01_1 = VppIpMRoute(
911             self,
912             "2001::1",
913             "ff01::1", 256,
914             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
915             [VppMRoutePath(self.pg8.sw_if_index,
916                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT,
917                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6),
918              VppMRoutePath(self.pg1.sw_if_index,
919                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
920                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6),
921              VppMRoutePath(self.pg2.sw_if_index,
922                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
923                            proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)],
924             table_id=10)
925         route_2001_ff01_1.add_vpp_config()
926
927         #
928         # a stream that matches the route for (2001::1, ff00::1)
929         #
930         self.vapi.cli("clear trace")
931         tx = self.create_stream_ip6(self.pg8, "2001::1", "ff01::1")
932         self.pg8.add_stream(tx)
933
934         self.pg_enable_capture(self.pg_interfaces)
935         self.pg_start()
936
937         # We expect replications on Pg1, 2,
938         self.verify_capture_ip6(self.pg1, tx)
939         self.verify_capture_ip6(self.pg2, tx)
940
941     def test_bidir(self):
942         """ IP Multicast Bi-directional """
943
944         #
945         # A (*,G). The set of accepting interfaces matching the forwarding
946         #
947         route_232_1_1_1 = VppIpMRoute(
948             self,
949             "0.0.0.0",
950             "232.1.1.1", 32,
951             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
952             [VppMRoutePath(self.pg0.sw_if_index,
953                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
954                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
955              VppMRoutePath(self.pg1.sw_if_index,
956                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
957                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
958              VppMRoutePath(self.pg2.sw_if_index,
959                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
960                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
961              VppMRoutePath(self.pg3.sw_if_index,
962                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
963                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
964         route_232_1_1_1.add_vpp_config()
965
966         tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
967         self.pg0.add_stream(tx)
968
969         self.pg_enable_capture(self.pg_interfaces)
970         self.pg_start()
971
972         # We expect replications on Pg1, 2, 3, but not on pg0
973         self.verify_capture_ip4(self.pg1, tx)
974         self.verify_capture_ip4(self.pg2, tx)
975         self.verify_capture_ip4(self.pg3, tx)
976         self.pg0.assert_nothing_captured(
977             remark="IP multicast packets forwarded on PG0")
978
979
980 if __name__ == '__main__':
981     unittest.main(testRunner=VppTestRunner)