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, rx_if, sent):
93 rxd = rx_if.get_capture(len(sent))
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, rx_if, sent):
122 capture = rx_if.get_capture(len(sent))
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)
236 # no replications on Pg0
237 self.pg0.assert_nothing_captured(
238 remark="IP multicast packets forwarded on PG0")
239 self.pg3.assert_nothing_captured(
240 remark="IP multicast packets forwarded on PG3")
243 # a stream that matches the route for (1.1.1.1,232.1.1.1)
246 self.vapi.cli("clear trace")
247 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1",
249 self.pg0.add_stream(tx)
251 self.pg_enable_capture(self.pg_interfaces)
254 # We expect replications on Pg1->7
255 self.verify_capture_ip4(self.pg1, tx)
256 self.verify_capture_ip4(self.pg2, tx)
258 # no replications on Pg0
259 self.pg0.assert_nothing_captured(
260 remark="IP multicast packets forwarded on PG0")
261 self.pg3.assert_nothing_captured(
262 remark="IP multicast packets forwarded on PG3")
265 # a stream that matches the route for (*,232.0.0.0/8)
266 # Send packets with the 9th bit set so we test the correct clearing
267 # of that bit in the mac rewrite
269 self.vapi.cli("clear trace")
270 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.255.255.255")
271 self.pg0.add_stream(tx)
273 self.pg_enable_capture(self.pg_interfaces)
276 # We expect replications on Pg1 only
277 self.verify_capture_ip4(self.pg1, tx)
279 # no replications on Pg0, Pg2 not Pg3
280 self.pg0.assert_nothing_captured(
281 remark="IP multicast packets forwarded on PG0")
282 self.pg2.assert_nothing_captured(
283 remark="IP multicast packets forwarded on PG2")
284 self.pg3.assert_nothing_captured(
285 remark="IP multicast packets forwarded on PG3")
288 # a stream that matches the route for (*,232.1.1.1)
290 self.vapi.cli("clear trace")
291 tx = self.create_stream_ip4(self.pg0, "1.1.1.2", "232.1.1.1")
292 self.pg0.add_stream(tx)
294 self.pg_enable_capture(self.pg_interfaces)
297 # We expect replications on Pg1, 2, 3.
298 self.verify_capture_ip4(self.pg1, tx)
299 self.verify_capture_ip4(self.pg2, tx)
300 self.verify_capture_ip4(self.pg3, tx)
301 self.verify_capture_ip4(self.pg4, tx)
302 self.verify_capture_ip4(self.pg5, tx)
303 self.verify_capture_ip4(self.pg6, tx)
304 self.verify_capture_ip4(self.pg7, tx)
306 route_232_1_1_1.remove_vpp_config()
307 route_1_1_1_1_232_1_1_1.remove_vpp_config()
308 route_232.remove_vpp_config()
310 def test_ip6_mcast(self):
311 """ IPv6 Multicast Replication """
314 # a stream that matches the default route. gets dropped.
316 self.vapi.cli("clear trace")
317 tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
318 self.pg0.add_stream(tx)
320 self.pg_enable_capture(self.pg_interfaces)
323 self.pg0.assert_nothing_captured(
324 remark="IPv6 multicast packets forwarded on default route")
328 # one accepting interface, pg0, 3 forwarding interfaces
330 route_ff01_1 = VppIpMRoute(
334 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
335 [VppMRoutePath(self.pg0.sw_if_index,
336 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
337 VppMRoutePath(self.pg1.sw_if_index,
338 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
339 VppMRoutePath(self.pg2.sw_if_index,
340 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
341 VppMRoutePath(self.pg3.sw_if_index,
342 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
344 route_ff01_1.add_vpp_config()
348 # one accepting interface, pg0, 2 forwarding interfaces
350 route_2001_ff01_1 = VppIpMRoute(
354 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
355 [VppMRoutePath(self.pg0.sw_if_index,
356 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
357 VppMRoutePath(self.pg1.sw_if_index,
358 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
359 VppMRoutePath(self.pg2.sw_if_index,
360 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
362 route_2001_ff01_1.add_vpp_config()
366 # one accepting interface, pg0, 1 forwarding interface
368 route_ff01 = VppIpMRoute(
372 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
373 [VppMRoutePath(self.pg0.sw_if_index,
374 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
375 VppMRoutePath(self.pg1.sw_if_index,
376 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
378 route_ff01.add_vpp_config()
381 # a stream that matches the route for (*, ff01::/16)
383 self.vapi.cli("clear trace")
384 tx = self.create_stream_ip6(self.pg0, "2002::1", "ff01:2::255")
385 self.pg0.add_stream(tx)
387 self.pg_enable_capture(self.pg_interfaces)
390 # We expect replications on Pg1
391 self.verify_capture_ip6(self.pg1, tx)
393 # no replications on Pg0, Pg3
394 self.pg0.assert_nothing_captured(
395 remark="IP multicast packets forwarded on PG0")
396 self.pg2.assert_nothing_captured(
397 remark="IP multicast packets forwarded on PG2")
398 self.pg3.assert_nothing_captured(
399 remark="IP multicast packets forwarded on PG3")
402 # Bounce the interface and it should still work
404 self.pg1.admin_down()
405 self.pg0.add_stream(tx)
406 self.pg_enable_capture(self.pg_interfaces)
408 self.pg1.assert_nothing_captured(
409 remark="IP multicast packets forwarded on down PG1")
412 self.pg0.add_stream(tx)
413 self.pg_enable_capture(self.pg_interfaces)
415 self.verify_capture_ip6(self.pg1, tx)
418 # a stream that matches the route for (*,ff01::1)
420 self.vapi.cli("clear trace")
421 tx = self.create_stream_ip6(self.pg0, "2002::2", "ff01::1")
422 self.pg0.add_stream(tx)
424 self.pg_enable_capture(self.pg_interfaces)
427 # We expect replications on Pg1, 2, 3.
428 self.verify_capture_ip6(self.pg1, tx)
429 self.verify_capture_ip6(self.pg2, tx)
430 self.verify_capture_ip6(self.pg3, tx)
432 # no replications on Pg0
433 self.pg0.assert_nothing_captured(
434 remark="IPv6 multicast packets forwarded on PG0")
437 # a stream that matches the route for (2001::1, ff00::1)
439 self.vapi.cli("clear trace")
440 tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
441 self.pg0.add_stream(tx)
443 self.pg_enable_capture(self.pg_interfaces)
446 # We expect replications on Pg1, 2,
447 self.verify_capture_ip6(self.pg1, tx)
448 self.verify_capture_ip6(self.pg2, tx)
450 # no replications on Pg0, Pg3
451 self.pg0.assert_nothing_captured(
452 remark="IP multicast packets forwarded on PG0")
453 self.pg3.assert_nothing_captured(
454 remark="IP multicast packets forwarded on PG3")
456 route_ff01.remove_vpp_config()
457 route_ff01_1.remove_vpp_config()
458 route_2001_ff01_1.remove_vpp_config()
460 def _mcast_connected_send_stream(self, dst_ip):
461 self.vapi.cli("clear trace")
462 tx = self.create_stream_ip4(self.pg0,
465 self.pg0.add_stream(tx)
467 self.pg_enable_capture(self.pg_interfaces)
470 # We expect replications on Pg1.
471 self.verify_capture_ip4(self.pg1, tx)
475 def test_ip_mcast_connected(self):
476 """ IP Multicast Connected Source check """
480 # one accepting interface, pg0, 1 forwarding interfaces
482 route_232_1_1_1 = VppIpMRoute(
486 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
487 [VppMRoutePath(self.pg0.sw_if_index,
488 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
489 VppMRoutePath(self.pg1.sw_if_index,
490 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
492 route_232_1_1_1.add_vpp_config()
493 route_232_1_1_1.update_entry_flags(
494 MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
497 # Now the (*,G) is present, send from connected source
499 tx = self._mcast_connected_send_stream("232.1.1.1")
502 # Constrct a representation of the signal we expect on pg0
504 signal_232_1_1_1_itf_0 = VppMFibSignal(self,
506 self.pg0.sw_if_index,
510 # read the only expected signal
512 signals = self.vapi.mfib_signal_dump()
514 self.assertEqual(1, len(signals))
516 signal_232_1_1_1_itf_0.compare(signals[0])
519 # reading the signal allows for the generation of another
520 # so send more packets and expect the next signal
522 tx = self._mcast_connected_send_stream("232.1.1.1")
524 signals = self.vapi.mfib_signal_dump()
525 self.assertEqual(1, len(signals))
526 signal_232_1_1_1_itf_0.compare(signals[0])
529 # A Second entry with connected check
530 # one accepting interface, pg0, 1 forwarding interfaces
532 route_232_1_1_2 = VppIpMRoute(
536 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
537 [VppMRoutePath(self.pg0.sw_if_index,
538 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
539 VppMRoutePath(self.pg1.sw_if_index,
540 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
542 route_232_1_1_2.add_vpp_config()
543 route_232_1_1_2.update_entry_flags(
544 MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
547 # Send traffic to both entries. One read should net us two signals
549 signal_232_1_1_2_itf_0 = VppMFibSignal(self,
551 self.pg0.sw_if_index,
553 tx = self._mcast_connected_send_stream("232.1.1.1")
554 tx2 = self._mcast_connected_send_stream("232.1.1.2")
557 # read the only expected signal
559 signals = self.vapi.mfib_signal_dump()
561 self.assertEqual(2, len(signals))
563 signal_232_1_1_1_itf_0.compare(signals[1])
564 signal_232_1_1_2_itf_0.compare(signals[0])
566 route_232_1_1_1.remove_vpp_config()
567 route_232_1_1_2.remove_vpp_config()
569 def test_ip_mcast_signal(self):
570 """ IP Multicast Signal """
574 # one accepting interface, pg0, 1 forwarding interfaces
576 route_232_1_1_1 = VppIpMRoute(
580 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
581 [VppMRoutePath(self.pg0.sw_if_index,
582 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
583 VppMRoutePath(self.pg1.sw_if_index,
584 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
586 route_232_1_1_1.add_vpp_config()
587 route_232_1_1_1.update_entry_flags(
588 MRouteEntryFlags.MFIB_ENTRY_FLAG_SIGNAL)
591 # Now the (*,G) is present, send from connected source
593 tx = self._mcast_connected_send_stream("232.1.1.1")
596 # Constrct a representation of the signal we expect on pg0
598 signal_232_1_1_1_itf_0 = VppMFibSignal(self,
600 self.pg0.sw_if_index,
604 # read the only expected signal
606 signals = self.vapi.mfib_signal_dump()
608 self.assertEqual(1, len(signals))
610 signal_232_1_1_1_itf_0.compare(signals[0])
613 # reading the signal allows for the generation of another
614 # so send more packets and expect the next signal
616 tx = self._mcast_connected_send_stream("232.1.1.1")
618 signals = self.vapi.mfib_signal_dump()
619 self.assertEqual(1, len(signals))
620 signal_232_1_1_1_itf_0.compare(signals[0])
623 # Set the negate-signal on the accepting interval - the signals
626 route_232_1_1_1.update_path_flags(
627 self.pg0.sw_if_index,
628 (MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
629 MRouteItfFlags.MFIB_ITF_FLAG_NEGATE_SIGNAL))
631 self.vapi.cli("clear trace")
632 tx = self._mcast_connected_send_stream("232.1.1.1")
634 signals = self.vapi.mfib_signal_dump()
635 self.assertEqual(0, len(signals))
638 # Clear the SIGNAL flag on the entry and the signals should
639 # come back since the interface is still NEGATE-SIGNAL
641 route_232_1_1_1.update_entry_flags(
642 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE)
644 tx = self._mcast_connected_send_stream("232.1.1.1")
646 signals = self.vapi.mfib_signal_dump()
647 self.assertEqual(1, len(signals))
648 signal_232_1_1_1_itf_0.compare(signals[0])
651 # Lastly remove the NEGATE-SIGNAL from the interface and the
652 # signals should stop
654 route_232_1_1_1.update_path_flags(self.pg0.sw_if_index,
655 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT)
657 tx = self._mcast_connected_send_stream("232.1.1.1")
658 signals = self.vapi.mfib_signal_dump()
659 self.assertEqual(0, len(signals))
664 route_232_1_1_1.remove_vpp_config()
667 if __name__ == '__main__':
668 unittest.main(testRunner=VppTestRunner)