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