5 from framework import VppTestCase, VppTestRunner
6 from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint
7 from vpp_ip_route import VppIpMRoute, VppMRoutePath, VppMFibSignal
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 # The number of packets sent is set to 90 so that when we replicate more than 3
34 # times, which we do for some entries, we will generate more than 256 packets
35 # to the next node in the VLIB graph. Thus we are testing the code's
36 # correctness handling this over-flow
41 class TestMFIB(VppTestCase):
42 """ MFIB Test Case """
45 super(TestMFIB, self).setUp()
48 """ MFIB Unit Tests """
49 error = self.vapi.cli("test mfib")
52 self.logger.critical(error)
53 self.assertEqual(error.find("Failed"), -1)
56 class TestIPMcast(VppTestCase):
57 """ IP Multicast Test Case """
60 super(TestIPMcast, self).setUp()
62 # create 4 pg interfaces
63 self.create_pg_interfaces(range(4))
66 for i in self.pg_interfaces:
73 def create_stream_ip4(self, src_if, src_ip, dst_ip):
75 for i in range(0, N_PKTS_IN_STREAM):
76 info = self.create_packet_info(src_if, src_if)
77 payload = self.info_to_payload(info)
78 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
79 IP(src=src_ip, dst=dst_ip) /
80 UDP(sport=1234, dport=1234) /
86 def create_stream_ip6(self, src_if, src_ip, dst_ip):
88 for i in range(0, N_PKTS_IN_STREAM):
89 info = self.create_packet_info(src_if, src_if)
90 payload = self.info_to_payload(info)
91 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
92 IPv6(src=src_ip, dst=dst_ip) /
93 UDP(sport=1234, dport=1234) /
99 def verify_filter(self, capture, sent):
100 if not len(capture) == len(sent):
101 # filter out any IPv6 RAs from the captur
103 if (p.haslayer(IPv6)):
107 def verify_capture_ip4(self, src_if, sent):
108 rxd = self.pg1.get_capture(N_PKTS_IN_STREAM)
111 capture = self.verify_filter(rxd, sent)
113 self.assertEqual(len(capture), len(sent))
115 for i in range(len(capture)):
119 # the rx'd packet has the MPLS label popped
121 self.assertEqual(eth.type, 0x800)
126 # check the MAC address on the RX'd packet is correctly formed
127 self.assertEqual(eth.dst, getmacbyip(rx_ip.dst))
129 self.assertEqual(rx_ip.src, tx_ip.src)
130 self.assertEqual(rx_ip.dst, tx_ip.dst)
131 # IP processing post pop has decremented the TTL
132 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
137 def verify_capture_ip6(self, src_if, sent):
138 capture = self.pg1.get_capture(N_PKTS_IN_STREAM)
140 self.assertEqual(len(capture), len(sent))
142 for i in range(len(capture)):
146 # the rx'd packet has the MPLS label popped
148 self.assertEqual(eth.type, 0x86DD)
153 # check the MAC address on the RX'd packet is correctly formed
154 self.assertEqual(eth.dst, getmacbyip6(rx_ip.dst))
156 self.assertEqual(rx_ip.src, tx_ip.src)
157 self.assertEqual(rx_ip.dst, tx_ip.dst)
158 # IP processing post pop has decremented the TTL
159 self.assertEqual(rx_ip.hlim + 1, tx_ip.hlim)
161 def test_ip_mcast(self):
162 """ IP Multicast Replication """
165 # a stream that matches the default route. gets dropped.
167 self.vapi.cli("clear trace")
168 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
169 self.pg0.add_stream(tx)
171 self.pg_enable_capture(self.pg_interfaces)
174 self.pg0.assert_nothing_captured(
175 remark="IP multicast packets forwarded on default route")
179 # one accepting interface, pg0, 3 forwarding interfaces
181 route_232_1_1_1 = VppIpMRoute(
185 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
186 [VppMRoutePath(self.pg0.sw_if_index,
187 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
188 VppMRoutePath(self.pg1.sw_if_index,
189 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
190 VppMRoutePath(self.pg2.sw_if_index,
191 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
192 VppMRoutePath(self.pg3.sw_if_index,
193 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
194 route_232_1_1_1.add_vpp_config()
198 # one accepting interface, pg0, 2 forwarding interfaces
200 route_1_1_1_1_232_1_1_1 = VppIpMRoute(
204 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
205 [VppMRoutePath(self.pg0.sw_if_index,
206 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
207 VppMRoutePath(self.pg1.sw_if_index,
208 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
209 VppMRoutePath(self.pg2.sw_if_index,
210 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
211 route_1_1_1_1_232_1_1_1.add_vpp_config()
215 # one accepting interface, pg0, 1 forwarding interfaces
217 route_232 = VppIpMRoute(
221 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
222 [VppMRoutePath(self.pg0.sw_if_index,
223 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
224 VppMRoutePath(self.pg1.sw_if_index,
225 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
226 route_232.add_vpp_config()
229 # a stream that matches the route for (1.1.1.1,232.1.1.1)
231 self.vapi.cli("clear trace")
232 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.1.1.1")
233 self.pg0.add_stream(tx)
235 self.pg_enable_capture(self.pg_interfaces)
238 # We expect replications on Pg1, 2,
239 self.verify_capture_ip4(self.pg1, tx)
240 self.verify_capture_ip4(self.pg2, tx)
242 # no replications on Pg0
243 self.pg0.assert_nothing_captured(
244 remark="IP multicast packets forwarded on PG0")
245 self.pg3.assert_nothing_captured(
246 remark="IP multicast packets forwarded on PG3")
249 # a stream that matches the route for (*,232.0.0.0/8)
250 # Send packets with the 9th bit set so we test the correct clearing
251 # of that bit in the mac rewrite
253 self.vapi.cli("clear trace")
254 tx = self.create_stream_ip4(self.pg0, "1.1.1.1", "232.255.255.255")
255 self.pg0.add_stream(tx)
257 self.pg_enable_capture(self.pg_interfaces)
260 # We expect replications on Pg1 only
261 self.verify_capture_ip4(self.pg1, tx)
263 # no replications on Pg0, Pg2 not Pg3
264 self.pg0.assert_nothing_captured(
265 remark="IP multicast packets forwarded on PG0")
266 self.pg2.assert_nothing_captured(
267 remark="IP multicast packets forwarded on PG2")
268 self.pg3.assert_nothing_captured(
269 remark="IP multicast packets forwarded on PG3")
272 # a stream that matches the route for (*,232.1.1.1)
274 self.vapi.cli("clear trace")
275 tx = self.create_stream_ip4(self.pg0, "1.1.1.2", "232.1.1.1")
276 self.pg0.add_stream(tx)
278 self.pg_enable_capture(self.pg_interfaces)
281 # We expect replications on Pg1, 2, 3.
282 self.verify_capture_ip4(self.pg1, tx)
283 self.verify_capture_ip4(self.pg2, tx)
284 self.verify_capture_ip4(self.pg3, tx)
286 # no replications on Pg0
287 self.pg0.assert_nothing_captured(
288 remark="IP multicast packets forwarded on PG0")
290 route_232_1_1_1.remove_vpp_config()
291 route_1_1_1_1_232_1_1_1.remove_vpp_config()
292 route_232.remove_vpp_config()
294 def test_ip6_mcast(self):
295 """ IPv6 Multicast Replication """
298 # a stream that matches the default route. gets dropped.
300 self.vapi.cli("clear trace")
301 tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
302 self.pg0.add_stream(tx)
304 self.pg_enable_capture(self.pg_interfaces)
307 self.pg0.assert_nothing_captured(
308 remark="IPv6 multicast packets forwarded on default route")
312 # one accepting interface, pg0, 3 forwarding interfaces
314 route_ff01_1 = VppIpMRoute(
318 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
319 [VppMRoutePath(self.pg0.sw_if_index,
320 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
321 VppMRoutePath(self.pg1.sw_if_index,
322 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
323 VppMRoutePath(self.pg2.sw_if_index,
324 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
325 VppMRoutePath(self.pg3.sw_if_index,
326 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
328 route_ff01_1.add_vpp_config()
332 # one accepting interface, pg0, 2 forwarding interfaces
334 route_2001_ff01_1 = VppIpMRoute(
338 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
339 [VppMRoutePath(self.pg0.sw_if_index,
340 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
341 VppMRoutePath(self.pg1.sw_if_index,
342 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD),
343 VppMRoutePath(self.pg2.sw_if_index,
344 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
346 route_2001_ff01_1.add_vpp_config()
350 # one accepting interface, pg0, 1 forwarding interface
352 route_ff01 = VppIpMRoute(
356 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
357 [VppMRoutePath(self.pg0.sw_if_index,
358 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
359 VppMRoutePath(self.pg1.sw_if_index,
360 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
362 route_ff01.add_vpp_config()
365 # a stream that matches the route for (*, ff01::/16)
367 self.vapi.cli("clear trace")
368 tx = self.create_stream_ip6(self.pg0, "2002::1", "ff01:2::255")
369 self.pg0.add_stream(tx)
371 self.pg_enable_capture(self.pg_interfaces)
374 # We expect replications on Pg1
375 self.verify_capture_ip6(self.pg1, tx)
377 # no replications on Pg0, Pg3
378 self.pg0.assert_nothing_captured(
379 remark="IP multicast packets forwarded on PG0")
380 self.pg2.assert_nothing_captured(
381 remark="IP multicast packets forwarded on PG2")
382 self.pg3.assert_nothing_captured(
383 remark="IP multicast packets forwarded on PG3")
386 # a stream that matches the route for (*,ff01::1)
388 self.vapi.cli("clear trace")
389 tx = self.create_stream_ip6(self.pg0, "2002::2", "ff01::1")
390 self.pg0.add_stream(tx)
392 self.pg_enable_capture(self.pg_interfaces)
395 # We expect replications on Pg1, 2, 3.
396 self.verify_capture_ip6(self.pg1, tx)
397 self.verify_capture_ip6(self.pg2, tx)
398 self.verify_capture_ip6(self.pg3, tx)
400 # no replications on Pg0
401 self.pg0.assert_nothing_captured(
402 remark="IPv6 multicast packets forwarded on PG0")
405 # a stream that matches the route for (2001::1, ff00::1)
407 self.vapi.cli("clear trace")
408 tx = self.create_stream_ip6(self.pg0, "2001::1", "ff01::1")
409 self.pg0.add_stream(tx)
411 self.pg_enable_capture(self.pg_interfaces)
414 # We expect replications on Pg1, 2,
415 self.verify_capture_ip6(self.pg1, tx)
416 self.verify_capture_ip6(self.pg2, tx)
418 # no replications on Pg0, Pg3
419 self.pg0.assert_nothing_captured(
420 remark="IP multicast packets forwarded on PG0")
421 self.pg3.assert_nothing_captured(
422 remark="IP multicast packets forwarded on PG3")
424 route_ff01.remove_vpp_config()
425 route_ff01_1.remove_vpp_config()
426 route_2001_ff01_1.remove_vpp_config()
428 def _mcast_connected_send_stream(self, dst_ip):
429 self.vapi.cli("clear trace")
430 tx = self.create_stream_ip4(self.pg0,
433 self.pg0.add_stream(tx)
435 self.pg_enable_capture(self.pg_interfaces)
438 # We expect replications on Pg1.
439 self.verify_capture_ip4(self.pg1, tx)
443 def test_ip_mcast_connected(self):
444 """ IP Multicast Connected Source check """
448 # one accepting interface, pg0, 1 forwarding interfaces
450 route_232_1_1_1 = VppIpMRoute(
454 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
455 [VppMRoutePath(self.pg0.sw_if_index,
456 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
457 VppMRoutePath(self.pg1.sw_if_index,
458 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
460 route_232_1_1_1.add_vpp_config()
461 route_232_1_1_1.update_entry_flags(
462 MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
465 # Now the (*,G) is present, send from connected source
467 tx = self._mcast_connected_send_stream("232.1.1.1")
470 # Constrct a representation of the signal we expect on pg0
472 signal_232_1_1_1_itf_0 = VppMFibSignal(self,
474 self.pg0.sw_if_index,
478 # read the only expected signal
480 signals = self.vapi.mfib_signal_dump()
482 self.assertEqual(1, len(signals))
484 signal_232_1_1_1_itf_0.compare(signals[0])
487 # reading the signal allows for the generation of another
488 # so send more packets and expect the next signal
490 tx = self._mcast_connected_send_stream("232.1.1.1")
492 signals = self.vapi.mfib_signal_dump()
493 self.assertEqual(1, len(signals))
494 signal_232_1_1_1_itf_0.compare(signals[0])
497 # A Second entry with connected check
498 # one accepting interface, pg0, 1 forwarding interfaces
500 route_232_1_1_2 = VppIpMRoute(
504 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
505 [VppMRoutePath(self.pg0.sw_if_index,
506 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
507 VppMRoutePath(self.pg1.sw_if_index,
508 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
510 route_232_1_1_2.add_vpp_config()
511 route_232_1_1_2.update_entry_flags(
512 MRouteEntryFlags.MFIB_ENTRY_FLAG_CONNECTED)
515 # Send traffic to both entries. One read should net us two signals
517 signal_232_1_1_2_itf_0 = VppMFibSignal(self,
519 self.pg0.sw_if_index,
521 tx = self._mcast_connected_send_stream("232.1.1.1")
522 tx2 = self._mcast_connected_send_stream("232.1.1.2")
525 # read the only expected signal
527 signals = self.vapi.mfib_signal_dump()
529 self.assertEqual(2, len(signals))
531 signal_232_1_1_1_itf_0.compare(signals[1])
532 signal_232_1_1_2_itf_0.compare(signals[0])
534 route_232_1_1_1.remove_vpp_config()
535 route_232_1_1_2.remove_vpp_config()
537 def test_ip_mcast_signal(self):
538 """ IP Multicast Signal """
542 # one accepting interface, pg0, 1 forwarding interfaces
544 route_232_1_1_1 = VppIpMRoute(
548 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
549 [VppMRoutePath(self.pg0.sw_if_index,
550 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
551 VppMRoutePath(self.pg1.sw_if_index,
552 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
554 route_232_1_1_1.add_vpp_config()
555 route_232_1_1_1.update_entry_flags(
556 MRouteEntryFlags.MFIB_ENTRY_FLAG_SIGNAL)
559 # Now the (*,G) is present, send from connected source
561 tx = self._mcast_connected_send_stream("232.1.1.1")
564 # Constrct a representation of the signal we expect on pg0
566 signal_232_1_1_1_itf_0 = VppMFibSignal(self,
568 self.pg0.sw_if_index,
572 # read the only expected signal
574 signals = self.vapi.mfib_signal_dump()
576 self.assertEqual(1, len(signals))
578 signal_232_1_1_1_itf_0.compare(signals[0])
581 # reading the signal allows for the generation of another
582 # so send more packets and expect the next signal
584 tx = self._mcast_connected_send_stream("232.1.1.1")
586 signals = self.vapi.mfib_signal_dump()
587 self.assertEqual(1, len(signals))
588 signal_232_1_1_1_itf_0.compare(signals[0])
591 # Set the negate-signal on the accepting interval - the signals
594 route_232_1_1_1.update_path_flags(
595 self.pg0.sw_if_index,
596 (MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT |
597 MRouteItfFlags.MFIB_ITF_FLAG_NEGATE_SIGNAL))
599 tx = self._mcast_connected_send_stream("232.1.1.1")
601 signals = self.vapi.mfib_signal_dump()
602 self.assertEqual(0, len(signals))
605 # Clear the SIGNAL flag on the entry and the signals should
606 # come back since the interface is still NEGATE-SIGNAL
608 route_232_1_1_1.update_entry_flags(
609 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE)
611 tx = self._mcast_connected_send_stream("232.1.1.1")
613 signals = self.vapi.mfib_signal_dump()
614 self.assertEqual(1, len(signals))
615 signal_232_1_1_1_itf_0.compare(signals[0])
618 # Lastly remove the NEGATE-SIGNAL from the interface and the
619 # signals should stop
621 route_232_1_1_1.update_path_flags(self.pg0.sw_if_index,
622 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT)
624 tx = self._mcast_connected_send_stream("232.1.1.1")
625 signals = self.vapi.mfib_signal_dump()
626 self.assertEqual(0, len(signals))
631 route_232_1_1_1.remove_vpp_config()
634 if __name__ == '__main__':
635 unittest.main(testRunner=VppTestRunner)