5 from framework import VppTestCase, VppTestRunner
6 from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint
7 from vpp_ip_route import IpMRoute, MRoutePath, MFibSignal
9 from scapy.packet import Raw
10 from scapy.layers.l2 import Ether
11 from scapy.layers.inet import IP, UDP, getmacbyip
12 from scapy.layers.inet6 import IPv6, getmacbyip6
17 MFIB_ITF_FLAG_NONE = 0
18 MFIB_ITF_FLAG_NEGATE_SIGNAL = 1
19 MFIB_ITF_FLAG_ACCEPT = 2
20 MFIB_ITF_FLAG_FORWARD = 4
21 MFIB_ITF_FLAG_SIGNAL_PRESENT = 8
22 MFIB_ITF_FLAG_INTERNAL_COPY = 16
25 class MRouteEntryFlags:
26 MFIB_ENTRY_FLAG_NONE = 0
27 MFIB_ENTRY_FLAG_SIGNAL = 1
28 MFIB_ENTRY_FLAG_DROP = 2
29 MFIB_ENTRY_FLAG_CONNECTED = 4
30 MFIB_ENTRY_FLAG_INHERIT_ACCEPT = 8
33 class TestIPMcast(VppTestCase):
34 """ IP Multicast Test Case """
37 super(TestIPMcast, self).setUp()
39 # create 4 pg interfaces
40 self.create_pg_interfaces(range(4))
43 for i in self.pg_interfaces:
50 def create_stream_ip4(self, src_if, src_ip, dst_ip):
52 for i in range(0, 65):
53 info = self.create_packet_info(src_if, src_if)
54 payload = self.info_to_payload(info)
55 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
56 IP(src=src_ip, dst=dst_ip) /
57 UDP(sport=1234, dport=1234) /
63 def create_stream_ip6(self, src_if, src_ip, dst_ip):
65 for i in range(0, 65):
66 info = self.create_packet_info(src_if, src_if)
67 payload = self.info_to_payload(info)
68 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
69 IPv6(src=src_ip, dst=dst_ip) /
70 UDP(sport=1234, dport=1234) /
76 def verify_filter(self, capture, sent):
77 if not len(capture) == len(sent):
78 # filter out any IPv6 RAs from the captur
80 if (p.haslayer(IPv6)):
84 def verify_capture_ip4(self, src_if, sent):
85 rxd = self.pg1.get_capture(65)
88 capture = self.verify_filter(rxd, sent)
90 self.assertEqual(len(capture), len(sent))
92 for i in range(len(capture)):
96 # the rx'd packet has the MPLS label popped
98 self.assertEqual(eth.type, 0x800)
103 # check the MAC address on the RX'd packet is correctly formed
104 self.assertEqual(eth.dst, getmacbyip(rx_ip.dst))
106 self.assertEqual(rx_ip.src, tx_ip.src)
107 self.assertEqual(rx_ip.dst, tx_ip.dst)
108 # IP processing post pop has decremented the TTL
109 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
114 def verify_capture_ip6(self, src_if, sent):
115 capture = self.pg1.get_capture(65)
117 self.assertEqual(len(capture), len(sent))
119 for i in range(len(capture)):
123 # the rx'd packet has the MPLS label popped
125 self.assertEqual(eth.type, 0x86DD)
130 # check the MAC address on the RX'd packet is correctly formed
131 self.assertEqual(eth.dst, getmacbyip6(rx_ip.dst))
133 self.assertEqual(rx_ip.src, tx_ip.src)
134 self.assertEqual(rx_ip.dst, tx_ip.dst)
135 # IP processing post pop has decremented the TTL
136 self.assertEqual(rx_ip.hlim + 1, tx_ip.hlim)
138 def test_ip_mcast(self):
139 """ IP Multicast Replication """
142 # a stream that matches the default route. gets dropped.
144 self.vapi.cli("clear trace")
145 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
146 self.pg0.add_stream(tx)
148 self.pg_enable_capture(self.pg_interfaces)
151 self.pg0.assert_nothing_captured(
152 remark="IP multicast packets forwarded on default route")
156 # one accepting interface, pg0, 3 forwarding interfaces
158 route_232_1_1_1 = IpMRoute(
162 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
163 [MRoutePath(self.pg0.sw_if_index,
164 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
165 MRoutePath(self.pg1.sw_if_index,
166 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
167 MRoutePath(self.pg2.sw_if_index,
168 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
169 MRoutePath(self.pg3.sw_if_index,
170 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
171 route_232_1_1_1.add_vpp_config()
175 # one accepting interface, pg0, 2 forwarding interfaces
177 route_1_1_1_1_232_1_1_1 = IpMRoute(
181 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
182 [MRoutePath(self.pg0.sw_if_index,
183 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
184 MRoutePath(self.pg1.sw_if_index,
185 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
186 MRoutePath(self.pg2.sw_if_index,
187 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
188 route_1_1_1_1_232_1_1_1.add_vpp_config()
192 # one accepting interface, pg0, 1 forwarding interfaces
194 route_232 = IpMRoute(
198 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
199 [MRoutePath(self.pg0.sw_if_index,
200 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
201 MRoutePath(self.pg1.sw_if_index,
202 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
203 route_232.add_vpp_config()
206 # a stream that matches the route for (1.1.1.1,232.1.1.1)
208 self.vapi.cli("clear trace")
209 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
210 self.pg0.add_stream(tx)
212 self.pg_enable_capture(self.pg_interfaces)
215 # We expect replications on Pg1, 2,
216 self.verify_capture_ip4(self.pg1, tx)
217 self.verify_capture_ip4(self.pg2, tx)
219 # no replications on Pg0
220 self.pg0.assert_nothing_captured(
221 remark="IP multicast packets forwarded on PG0")
222 self.pg3.assert_nothing_captured(
223 remark="IP multicast packets forwarded on PG3")
226 # a stream that matches the route for (*,232.0.0.0/8)
227 # Send packets with the 9th bit set so we test the correct clearing
228 # of that bit in the mac rewrite
230 self.vapi.cli("clear trace")
231 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.255.255.255")
232 self.pg0.add_stream(tx)
234 self.pg_enable_capture(self.pg_interfaces)
237 # We expect replications on Pg1 only
238 self.verify_capture_ip4(self.pg1, tx)
240 # no replications on Pg0, Pg2 not Pg3
241 self.pg0.assert_nothing_captured(
242 remark="IP multicast packets forwarded on PG0")
243 self.pg2.assert_nothing_captured(
244 remark="IP multicast packets forwarded on PG2")
245 self.pg3.assert_nothing_captured(
246 remark="IP multicast packets forwarded on PG3")
249 # a stream that matches the route for (*,232.1.1.1)
251 self.vapi.cli("clear trace")
252 tx = self.create_stream_ip4(self.pg0, "1.1.1.2", "232.1.1.1")
253 self.pg0.add_stream(tx)
255 self.pg_enable_capture(self.pg_interfaces)
258 # We expect replications on Pg1, 2, 3.
259 self.verify_capture_ip4(self.pg1, tx)
260 self.verify_capture_ip4(self.pg2, tx)
261 self.verify_capture_ip4(self.pg3, tx)
263 # no replications on Pg0
264 self.pg0.assert_nothing_captured(
265 remark="IP multicast packets forwarded on PG0")
267 route_232_1_1_1.remove_vpp_config()
268 route_1_1_1_1_232_1_1_1.remove_vpp_config()
269 route_232.remove_vpp_config()
271 def test_ip6_mcast(self):
272 """ IPv6 Multicast Replication """
275 # a stream that matches the default route. gets dropped.
277 self.vapi.cli("clear trace")
278 tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
279 self.pg0.add_stream(tx)
281 self.pg_enable_capture(self.pg_interfaces)
284 self.pg0.assert_nothing_captured(
285 remark="IPv6 multicast packets forwarded on default route")
289 # one accepting interface, pg0, 3 forwarding interfaces
291 route_ff01_1 = IpMRoute(
295 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
296 [MRoutePath(self.pg0.sw_if_index,
297 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
298 MRoutePath(self.pg1.sw_if_index,
299 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
300 MRoutePath(self.pg2.sw_if_index,
301 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
302 MRoutePath(self.pg3.sw_if_index,
303 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
305 route_ff01_1.add_vpp_config()
309 # one accepting interface, pg0, 2 forwarding interfaces
311 route_2001_ff01_1 = IpMRoute(
315 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
316 [MRoutePath(self.pg0.sw_if_index,
317 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
318 MRoutePath(self.pg1.sw_if_index,
319 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
320 MRoutePath(self.pg2.sw_if_index,
321 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
323 route_2001_ff01_1.add_vpp_config()
327 # one accepting interface, pg0, 1 forwarding interface
329 route_ff01 = IpMRoute(
333 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
334 [MRoutePath(self.pg0.sw_if_index,
335 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
336 MRoutePath(self.pg1.sw_if_index,
337 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
339 route_ff01.add_vpp_config()
342 # a stream that matches the route for (*, ff01::/16)
344 self.vapi.cli("clear trace")
345 tx = self.create_stream_ip6(self.pg0, "2002::1", "ff01:2::255")
346 self.pg0.add_stream(tx)
348 self.pg_enable_capture(self.pg_interfaces)
351 # We expect replications on Pg1
352 self.verify_capture_ip6(self.pg1, tx)
354 # no replications on Pg0, Pg3
355 self.pg0.assert_nothing_captured(
356 remark="IP multicast packets forwarded on PG0")
357 self.pg2.assert_nothing_captured(
358 remark="IP multicast packets forwarded on PG2")
359 self.pg3.assert_nothing_captured(
360 remark="IP multicast packets forwarded on PG3")
363 # a stream that matches the route for (*,ff01::1)
365 self.vapi.cli("clear trace")
366 tx = self.create_stream_ip6(self.pg0, "2002::2", "ff01::1")
367 self.pg0.add_stream(tx)
369 self.pg_enable_capture(self.pg_interfaces)
372 # We expect replications on Pg1, 2, 3.
373 self.verify_capture_ip6(self.pg1, tx)
374 self.verify_capture_ip6(self.pg2, tx)
375 self.verify_capture_ip6(self.pg3, tx)
377 # no replications on Pg0
378 self.pg0.assert_nothing_captured(
379 remark="IPv6 multicast packets forwarded on PG0")
382 # a stream that matches the route for (2001::1, ff00::1)
384 self.vapi.cli("clear trace")
385 tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
386 self.pg0.add_stream(tx)
388 self.pg_enable_capture(self.pg_interfaces)
391 # We expect replications on Pg1, 2,
392 self.verify_capture_ip6(self.pg1, tx)
393 self.verify_capture_ip6(self.pg2, tx)
395 # no replications on Pg0, Pg3
396 self.pg0.assert_nothing_captured(
397 remark="IP multicast packets forwarded on PG0")
398 self.pg3.assert_nothing_captured(
399 remark="IP multicast packets forwarded on PG3")
401 route_ff01.remove_vpp_config()
402 route_ff01_1.remove_vpp_config()
403 route_2001_ff01_1.remove_vpp_config()
405 def _mcast_connected_send_stream(self, dst_ip):
406 self.vapi.cli("clear trace")
407 tx = self.create_stream_ip4(self.pg0,
410 self.pg0.add_stream(tx)
412 self.pg_enable_capture(self.pg_interfaces)
415 # We expect replications on Pg1.
416 self.verify_capture_ip4(self.pg1, tx)
420 def test_ip_mcast_connected(self):
421 """ IP Multicast Connected Source check """
425 # one accepting interface, pg0, 1 forwarding interfaces
427 route_232_1_1_1 = IpMRoute(
431 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
432 [MRoutePath(self.pg0.sw_if_index,
433 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
434 MRoutePath(self.pg1.sw_if_index,
435 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
437 route_232_1_1_1.add_vpp_config()
438 route_232_1_1_1.update_entry_flags(
439 MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
442 # Now the (*,G) is present, send from connected source
444 tx = self._mcast_connected_send_stream("232.1.1.1")
447 # Constrct a representation of the signal we expect on pg0
449 signal_232_1_1_1_itf_0 = MFibSignal(self,
451 self.pg0.sw_if_index,
455 # read the only expected signal
457 signals = self.vapi.mfib_signal_dump()
459 self.assertEqual(1, len(signals))
461 signal_232_1_1_1_itf_0.compare(signals[0])
464 # reading the signal allows for the generation of another
465 # so send more packets and expect the next signal
467 tx = self._mcast_connected_send_stream("232.1.1.1")
469 signals = self.vapi.mfib_signal_dump()
470 self.assertEqual(1, len(signals))
471 signal_232_1_1_1_itf_0.compare(signals[0])
474 # A Second entry with connected check
475 # one accepting interface, pg0, 1 forwarding interfaces
477 route_232_1_1_2 = IpMRoute(
481 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
482 [MRoutePath(self.pg0.sw_if_index,
483 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
484 MRoutePath(self.pg1.sw_if_index,
485 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
487 route_232_1_1_2.add_vpp_config()
488 route_232_1_1_2.update_entry_flags(
489 MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
492 # Send traffic to both entries. One read should net us two signals
494 signal_232_1_1_2_itf_0 = MFibSignal(self,
496 self.pg0.sw_if_index,
498 tx = self._mcast_connected_send_stream("232.1.1.1")
499 tx2 = self._mcast_connected_send_stream("232.1.1.2")
502 # read the only expected signal
504 signals = self.vapi.mfib_signal_dump()
506 self.assertEqual(2, len(signals))
508 signal_232_1_1_1_itf_0.compare(signals[1])
509 signal_232_1_1_2_itf_0.compare(signals[0])
511 route_232_1_1_1.remove_vpp_config()
512 route_232_1_1_2.remove_vpp_config()
514 def test_ip_mcast_signal(self):
515 """ IP Multicast Signal """
519 # one accepting interface, pg0, 1 forwarding interfaces
521 route_232_1_1_1 = IpMRoute(
525 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
526 [MRoutePath(self.pg0.sw_if_index,
527 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
528 MRoutePath(self.pg1.sw_if_index,
529 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
531 route_232_1_1_1.add_vpp_config()
532 route_232_1_1_1.update_entry_flags(
533 MRouteEntryFlags.MFIB_ENTRY_FLAG_SIGNAL)
536 # Now the (*,G) is present, send from connected source
538 tx = self._mcast_connected_send_stream("232.1.1.1")
541 # Constrct a representation of the signal we expect on pg0
543 signal_232_1_1_1_itf_0 = MFibSignal(self,
545 self.pg0.sw_if_index,
549 # read the only expected signal
551 signals = self.vapi.mfib_signal_dump()
553 self.assertEqual(1, len(signals))
555 signal_232_1_1_1_itf_0.compare(signals[0])
558 # reading the signal allows for the generation of another
559 # so send more packets and expect the next signal
561 tx = self._mcast_connected_send_stream("232.1.1.1")
563 signals = self.vapi.mfib_signal_dump()
564 self.assertEqual(1, len(signals))
565 signal_232_1_1_1_itf_0.compare(signals[0])
568 # Set the negate-signal on the accepting interval - the signals
571 route_232_1_1_1.update_path_flags(
572 self.pg0.sw_if_index,
573 (MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
574 MRouteItfFlags.MFIB_ITF_FLAG_NEGATE_SIGNAL))
576 tx = self._mcast_connected_send_stream("232.1.1.1")
578 signals = self.vapi.mfib_signal_dump()
579 self.assertEqual(0, len(signals))
582 # Clear the SIGNAL flag on the entry and the signals should
583 # come back since the interface is still NEGATE-SIGNAL
585 route_232_1_1_1.update_entry_flags(
586 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE)
588 tx = self._mcast_connected_send_stream("232.1.1.1")
590 signals = self.vapi.mfib_signal_dump()
591 self.assertEqual(1, len(signals))
592 signal_232_1_1_1_itf_0.compare(signals[0])
595 # Lastly remove the NEGATE-SIGNAL from the interface and the
596 # signals should stop
598 route_232_1_1_1.update_path_flags(self.pg0.sw_if_index,
599 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT)
601 tx = self._mcast_connected_send_stream("232.1.1.1")
602 signals = self.vapi.mfib_signal_dump()
603 self.assertEqual(0, len(signals))
608 route_232_1_1_1.remove_vpp_config()
611 if __name__ == '__main__':
612 unittest.main(testRunner=VppTestRunner)