5 from framework import VppTestCase, VppTestRunner
6 from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint
7 from vpp_ip_route import VppIpMRoute, VppMRoutePath, VppMFibSignal, \
8 MRouteItfFlags, MRouteEntryFlags
10 from scapy.packet import Raw
11 from scapy.layers.l2 import Ether
12 from scapy.layers.inet import IP, UDP, getmacbyip, ICMP
13 from scapy.layers.inet6 import IPv6, getmacbyip6
17 # The number of packets sent is set to 90 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
25 class TestMFIB(VppTestCase):
26 """ MFIB Test Case """
29 super(TestMFIB, self).setUp()
32 """ MFIB Unit Tests """
33 error = self.vapi.cli("test mfib")
36 self.logger.critical(error)
37 self.assertEqual(error.find("Failed"), -1)
40 class TestIPMcast(VppTestCase):
41 """ IP Multicast Test Case """
44 super(TestIPMcast, self).setUp()
46 # create 8 pg interfaces
47 self.create_pg_interfaces(range(8))
50 for i in self.pg_interfaces:
57 def create_stream_ip4(self, src_if, src_ip, dst_ip, payload_size=0):
59 # default to small packet sizes
60 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
61 IP(src=src_ip, dst=dst_ip) /
62 UDP(sport=1234, dport=1234))
64 payload_size = 64 - len(p)
65 p = p / Raw('\xa5' * payload_size)
67 for i in range(0, N_PKTS_IN_STREAM):
71 def create_stream_ip6(self, src_if, src_ip, dst_ip):
73 for i in range(0, N_PKTS_IN_STREAM):
74 info = self.create_packet_info(src_if, src_if)
75 payload = self.info_to_payload(info)
76 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
77 IPv6(src=src_ip, dst=dst_ip) /
78 UDP(sport=1234, dport=1234) /
84 def verify_filter(self, capture, sent):
85 if not len(capture) == len(sent):
86 # filter out any IPv6 RAs from the captur
88 if (p.haslayer(IPv6)):
92 def verify_capture_ip4(self, src_if, sent):
93 rxd = self.pg1.get_capture(N_PKTS_IN_STREAM)
96 capture = self.verify_filter(rxd, sent)
98 self.assertEqual(len(capture), len(sent))
100 for i in range(len(capture)):
105 self.assertEqual(eth.type, 0x800)
110 # check the MAC address on the RX'd packet is correctly formed
111 self.assertEqual(eth.dst, getmacbyip(rx_ip.dst))
113 self.assertEqual(rx_ip.src, tx_ip.src)
114 self.assertEqual(rx_ip.dst, tx_ip.dst)
115 # IP processing post pop has decremented the TTL
116 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
121 def verify_capture_ip6(self, src_if, sent):
122 capture = self.pg1.get_capture(N_PKTS_IN_STREAM)
124 self.assertEqual(len(capture), len(sent))
126 for i in range(len(capture)):
131 self.assertEqual(eth.type, 0x86DD)
136 # check the MAC address on the RX'd packet is correctly formed
137 self.assertEqual(eth.dst, getmacbyip6(rx_ip.dst))
139 self.assertEqual(rx_ip.src, tx_ip.src)
140 self.assertEqual(rx_ip.dst, tx_ip.dst)
141 # IP processing post pop has decremented the TTL
142 self.assertEqual(rx_ip.hlim + 1, tx_ip.hlim)
144 def test_ip_mcast(self):
145 """ IP Multicast Replication """
148 # a stream that matches the default route. gets dropped.
150 self.vapi.cli("clear trace")
151 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
152 self.pg0.add_stream(tx)
154 self.pg_enable_capture(self.pg_interfaces)
157 self.pg0.assert_nothing_captured(
158 remark="IP multicast packets forwarded on default route")
162 # one accepting interface, pg0, 7 forwarding interfaces
163 # many forwarding interfaces test the case where the replicare DPO
164 # needs to use extra cache lines for the buckets.
166 route_232_1_1_1 = VppIpMRoute(
170 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
171 [VppMRoutePath(self.pg0.sw_if_index,
172 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
173 VppMRoutePath(self.pg1.sw_if_index,
174 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
175 VppMRoutePath(self.pg2.sw_if_index,
176 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
177 VppMRoutePath(self.pg3.sw_if_index,
178 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
179 VppMRoutePath(self.pg4.sw_if_index,
180 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
181 VppMRoutePath(self.pg5.sw_if_index,
182 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
183 VppMRoutePath(self.pg6.sw_if_index,
184 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
185 VppMRoutePath(self.pg7.sw_if_index,
186 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
187 route_232_1_1_1.add_vpp_config()
191 # one accepting interface, pg0, 2 forwarding interfaces
193 route_1_1_1_1_232_1_1_1 = VppIpMRoute(
197 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
198 [VppMRoutePath(self.pg0.sw_if_index,
199 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
200 VppMRoutePath(self.pg1.sw_if_index,
201 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
202 VppMRoutePath(self.pg2.sw_if_index,
203 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
204 route_1_1_1_1_232_1_1_1.add_vpp_config()
208 # one accepting interface, pg0, 1 forwarding interfaces
210 route_232 = VppIpMRoute(
214 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
215 [VppMRoutePath(self.pg0.sw_if_index,
216 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
217 VppMRoutePath(self.pg1.sw_if_index,
218 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
219 route_232.add_vpp_config()
222 # a stream that matches the route for (1.1.1.1,232.1.1.1)
225 self.vapi.cli("clear trace")
226 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
227 self.pg0.add_stream(tx)
229 self.pg_enable_capture(self.pg_interfaces)
232 # We expect replications on Pg1->7
233 self.verify_capture_ip4(self.pg1, tx)
234 self.verify_capture_ip4(self.pg2, tx)
235 self.verify_capture_ip4(self.pg3, tx)
236 self.verify_capture_ip4(self.pg4, tx)
237 self.verify_capture_ip4(self.pg5, tx)
238 self.verify_capture_ip4(self.pg6, tx)
239 self.verify_capture_ip4(self.pg7, tx)
241 # no replications on Pg0
242 self.pg0.assert_nothing_captured(
243 remark="IP multicast packets forwarded on PG0")
244 self.pg3.assert_nothing_captured(
245 remark="IP multicast packets forwarded on PG3")
248 # a stream that matches the route for (1.1.1.1,232.1.1.1)
251 self.vapi.cli("clear trace")
252 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1",
254 self.pg0.add_stream(tx)
256 self.pg_enable_capture(self.pg_interfaces)
259 # We expect replications on Pg1->7
260 self.verify_capture_ip4(self.pg1, tx)
261 self.verify_capture_ip4(self.pg2, tx)
262 self.verify_capture_ip4(self.pg3, tx)
263 self.verify_capture_ip4(self.pg4, tx)
264 self.verify_capture_ip4(self.pg5, tx)
265 self.verify_capture_ip4(self.pg6, tx)
266 self.verify_capture_ip4(self.pg7, tx)
268 # no replications on Pg0
269 self.pg0.assert_nothing_captured(
270 remark="IP multicast packets forwarded on PG0")
271 self.pg3.assert_nothing_captured(
272 remark="IP multicast packets forwarded on PG3")
275 # a stream that matches the route for (*,232.0.0.0/8)
276 # Send packets with the 9th bit set so we test the correct clearing
277 # of that bit in the mac rewrite
279 self.vapi.cli("clear trace")
280 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.255.255.255")
281 self.pg0.add_stream(tx)
283 self.pg_enable_capture(self.pg_interfaces)
286 # We expect replications on Pg1 only
287 self.verify_capture_ip4(self.pg1, tx)
289 # no replications on Pg0, Pg2 not Pg3
290 self.pg0.assert_nothing_captured(
291 remark="IP multicast packets forwarded on PG0")
292 self.pg2.assert_nothing_captured(
293 remark="IP multicast packets forwarded on PG2")
294 self.pg3.assert_nothing_captured(
295 remark="IP multicast packets forwarded on PG3")
298 # a stream that matches the route for (*,232.1.1.1)
300 self.vapi.cli("clear trace")
301 tx = self.create_stream_ip4(self.pg0, "1.1.1.2", "232.1.1.1")
302 self.pg0.add_stream(tx)
304 self.pg_enable_capture(self.pg_interfaces)
307 # We expect replications on Pg1, 2, 3.
308 self.verify_capture_ip4(self.pg1, tx)
309 self.verify_capture_ip4(self.pg2, tx)
310 self.verify_capture_ip4(self.pg3, tx)
312 # no replications on Pg0
313 self.pg0.assert_nothing_captured(
314 remark="IP multicast packets forwarded on PG0")
316 route_232_1_1_1.remove_vpp_config()
317 route_1_1_1_1_232_1_1_1.remove_vpp_config()
318 route_232.remove_vpp_config()
320 def test_ip6_mcast(self):
321 """ IPv6 Multicast Replication """
324 # a stream that matches the default route. gets dropped.
326 self.vapi.cli("clear trace")
327 tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
328 self.pg0.add_stream(tx)
330 self.pg_enable_capture(self.pg_interfaces)
333 self.pg0.assert_nothing_captured(
334 remark="IPv6 multicast packets forwarded on default route")
338 # one accepting interface, pg0, 3 forwarding interfaces
340 route_ff01_1 = VppIpMRoute(
344 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
345 [VppMRoutePath(self.pg0.sw_if_index,
346 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
347 VppMRoutePath(self.pg1.sw_if_index,
348 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
349 VppMRoutePath(self.pg2.sw_if_index,
350 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
351 VppMRoutePath(self.pg3.sw_if_index,
352 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
354 route_ff01_1.add_vpp_config()
358 # one accepting interface, pg0, 2 forwarding interfaces
360 route_2001_ff01_1 = VppIpMRoute(
364 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
365 [VppMRoutePath(self.pg0.sw_if_index,
366 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
367 VppMRoutePath(self.pg1.sw_if_index,
368 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
369 VppMRoutePath(self.pg2.sw_if_index,
370 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
372 route_2001_ff01_1.add_vpp_config()
376 # one accepting interface, pg0, 1 forwarding interface
378 route_ff01 = VppIpMRoute(
382 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
383 [VppMRoutePath(self.pg0.sw_if_index,
384 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
385 VppMRoutePath(self.pg1.sw_if_index,
386 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
388 route_ff01.add_vpp_config()
391 # a stream that matches the route for (*, ff01::/16)
393 self.vapi.cli("clear trace")
394 tx = self.create_stream_ip6(self.pg0, "2002::1", "ff01:2::255")
395 self.pg0.add_stream(tx)
397 self.pg_enable_capture(self.pg_interfaces)
400 # We expect replications on Pg1
401 self.verify_capture_ip6(self.pg1, tx)
403 # no replications on Pg0, Pg3
404 self.pg0.assert_nothing_captured(
405 remark="IP multicast packets forwarded on PG0")
406 self.pg2.assert_nothing_captured(
407 remark="IP multicast packets forwarded on PG2")
408 self.pg3.assert_nothing_captured(
409 remark="IP multicast packets forwarded on PG3")
412 # a stream that matches the route for (*,ff01::1)
414 self.vapi.cli("clear trace")
415 tx = self.create_stream_ip6(self.pg0, "2002::2", "ff01::1")
416 self.pg0.add_stream(tx)
418 self.pg_enable_capture(self.pg_interfaces)
421 # We expect replications on Pg1, 2, 3.
422 self.verify_capture_ip6(self.pg1, tx)
423 self.verify_capture_ip6(self.pg2, tx)
424 self.verify_capture_ip6(self.pg3, tx)
426 # no replications on Pg0
427 self.pg0.assert_nothing_captured(
428 remark="IPv6 multicast packets forwarded on PG0")
431 # a stream that matches the route for (2001::1, ff00::1)
433 self.vapi.cli("clear trace")
434 tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
435 self.pg0.add_stream(tx)
437 self.pg_enable_capture(self.pg_interfaces)
440 # We expect replications on Pg1, 2,
441 self.verify_capture_ip6(self.pg1, tx)
442 self.verify_capture_ip6(self.pg2, tx)
444 # no replications on Pg0, Pg3
445 self.pg0.assert_nothing_captured(
446 remark="IP multicast packets forwarded on PG0")
447 self.pg3.assert_nothing_captured(
448 remark="IP multicast packets forwarded on PG3")
450 route_ff01.remove_vpp_config()
451 route_ff01_1.remove_vpp_config()
452 route_2001_ff01_1.remove_vpp_config()
454 def _mcast_connected_send_stream(self, dst_ip):
455 self.vapi.cli("clear trace")
456 tx = self.create_stream_ip4(self.pg0,
459 self.pg0.add_stream(tx)
461 self.pg_enable_capture(self.pg_interfaces)
464 # We expect replications on Pg1.
465 self.verify_capture_ip4(self.pg1, tx)
469 def test_ip_mcast_connected(self):
470 """ IP Multicast Connected Source check """
474 # one accepting interface, pg0, 1 forwarding interfaces
476 route_232_1_1_1 = VppIpMRoute(
480 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
481 [VppMRoutePath(self.pg0.sw_if_index,
482 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
483 VppMRoutePath(self.pg1.sw_if_index,
484 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
486 route_232_1_1_1.add_vpp_config()
487 route_232_1_1_1.update_entry_flags(
488 MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
491 # Now the (*,G) is present, send from connected source
493 tx = self._mcast_connected_send_stream("232.1.1.1")
496 # Constrct a representation of the signal we expect on pg0
498 signal_232_1_1_1_itf_0 = VppMFibSignal(self,
500 self.pg0.sw_if_index,
504 # read the only expected signal
506 signals = self.vapi.mfib_signal_dump()
508 self.assertEqual(1, len(signals))
510 signal_232_1_1_1_itf_0.compare(signals[0])
513 # reading the signal allows for the generation of another
514 # so send more packets and expect the next signal
516 tx = self._mcast_connected_send_stream("232.1.1.1")
518 signals = self.vapi.mfib_signal_dump()
519 self.assertEqual(1, len(signals))
520 signal_232_1_1_1_itf_0.compare(signals[0])
523 # A Second entry with connected check
524 # one accepting interface, pg0, 1 forwarding interfaces
526 route_232_1_1_2 = VppIpMRoute(
530 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
531 [VppMRoutePath(self.pg0.sw_if_index,
532 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
533 VppMRoutePath(self.pg1.sw_if_index,
534 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
536 route_232_1_1_2.add_vpp_config()
537 route_232_1_1_2.update_entry_flags(
538 MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
541 # Send traffic to both entries. One read should net us two signals
543 signal_232_1_1_2_itf_0 = VppMFibSignal(self,
545 self.pg0.sw_if_index,
547 tx = self._mcast_connected_send_stream("232.1.1.1")
548 tx2 = self._mcast_connected_send_stream("232.1.1.2")
551 # read the only expected signal
553 signals = self.vapi.mfib_signal_dump()
555 self.assertEqual(2, len(signals))
557 signal_232_1_1_1_itf_0.compare(signals[1])
558 signal_232_1_1_2_itf_0.compare(signals[0])
560 route_232_1_1_1.remove_vpp_config()
561 route_232_1_1_2.remove_vpp_config()
563 def test_ip_mcast_signal(self):
564 """ IP Multicast Signal """
568 # one accepting interface, pg0, 1 forwarding interfaces
570 route_232_1_1_1 = VppIpMRoute(
574 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
575 [VppMRoutePath(self.pg0.sw_if_index,
576 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
577 VppMRoutePath(self.pg1.sw_if_index,
578 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
580 route_232_1_1_1.add_vpp_config()
581 route_232_1_1_1.update_entry_flags(
582 MRouteEntryFlags.MFIB_ENTRY_FLAG_SIGNAL)
585 # Now the (*,G) is present, send from connected source
587 tx = self._mcast_connected_send_stream("232.1.1.1")
590 # Constrct a representation of the signal we expect on pg0
592 signal_232_1_1_1_itf_0 = VppMFibSignal(self,
594 self.pg0.sw_if_index,
598 # read the only expected signal
600 signals = self.vapi.mfib_signal_dump()
602 self.assertEqual(1, len(signals))
604 signal_232_1_1_1_itf_0.compare(signals[0])
607 # reading the signal allows for the generation of another
608 # so send more packets and expect the next signal
610 tx = self._mcast_connected_send_stream("232.1.1.1")
612 signals = self.vapi.mfib_signal_dump()
613 self.assertEqual(1, len(signals))
614 signal_232_1_1_1_itf_0.compare(signals[0])
617 # Set the negate-signal on the accepting interval - the signals
620 route_232_1_1_1.update_path_flags(
621 self.pg0.sw_if_index,
622 (MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
623 MRouteItfFlags.MFIB_ITF_FLAG_NEGATE_SIGNAL))
625 tx = self._mcast_connected_send_stream("232.1.1.1")
627 signals = self.vapi.mfib_signal_dump()
628 self.assertEqual(0, len(signals))
631 # Clear the SIGNAL flag on the entry and the signals should
632 # come back since the interface is still NEGATE-SIGNAL
634 route_232_1_1_1.update_entry_flags(
635 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE)
637 tx = self._mcast_connected_send_stream("232.1.1.1")
639 signals = self.vapi.mfib_signal_dump()
640 self.assertEqual(1, len(signals))
641 signal_232_1_1_1_itf_0.compare(signals[0])
644 # Lastly remove the NEGATE-SIGNAL from the interface and the
645 # signals should stop
647 route_232_1_1_1.update_path_flags(self.pg0.sw_if_index,
648 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT)
650 tx = self._mcast_connected_send_stream("232.1.1.1")
651 signals = self.vapi.mfib_signal_dump()
652 self.assertEqual(0, len(signals))
657 route_232_1_1_1.remove_vpp_config()
660 if __name__ == '__main__':
661 unittest.main(testRunner=VppTestRunner)