6 from framework import VppTestCase, VppTestRunner
7 from vpp_ip_route import IpRoute, RoutePath, MplsRoute, MplsIpBind
9 from scapy.packet import Raw
10 from scapy.layers.l2 import Ether
11 from scapy.layers.inet import IP, UDP, ICMP
12 from scapy.layers.inet6 import IPv6
13 from scapy.contrib.mpls import MPLS
17 class TestMPLS(VppTestCase):
18 """ MPLS Test Case """
22 super(TestMPLS, cls).setUpClass()
25 super(TestMPLS, self).setUp()
27 # create 2 pg interfaces
28 self.create_pg_interfaces(range(2))
30 # setup both interfaces
31 # assign them different tables.
34 for i in self.pg_interfaces:
36 i.set_table_ip4(table_id)
37 i.set_table_ip6(table_id)
46 super(TestMPLS, self).tearDown()
48 # the default of 64 matches the IP packet TTL default
49 def create_stream_labelled_ip4(self, src_if, mpls_labels, mpls_ttl=255, ping=0, ip_itf=None):
51 for i in range(0, 257):
52 info = self.create_packet_info(src_if.sw_if_index,
54 payload = self.info_to_payload(info)
55 p = Ether(dst=src_if.local_mac, src=src_if.remote_mac)
57 for ii in range(len(mpls_labels)):
58 if ii == len(mpls_labels) - 1:
59 p = p / MPLS(label=mpls_labels[ii], ttl=mpls_ttl, s=1)
61 p = p / MPLS(label=mpls_labels[ii], ttl=mpls_ttl, s=0)
63 p = (p / IP(src=src_if.local_ip4, dst=src_if.remote_ip4) /
64 UDP(sport=1234, dport=1234) /
67 p = (p / IP(src=ip_itf.remote_ip4,
68 dst=ip_itf.local_ip4) /
75 def create_stream_ip4(self, src_if, dst_ip):
77 for i in range(0, 257):
78 info = self.create_packet_info(src_if.sw_if_index,
80 payload = self.info_to_payload(info)
81 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
82 IP(src=src_if.remote_ip4, dst=dst_ip) /
83 UDP(sport=1234, dport=1234) /
89 def create_stream_labelled_ip6(self, src_if, mpls_label, mpls_ttl):
91 for i in range(0, 257):
92 info = self.create_packet_info(src_if.sw_if_index,
94 payload = self.info_to_payload(info)
95 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
96 MPLS(label=mpls_label, ttl=mpls_ttl) /
97 IPv6(src=src_if.remote_ip6, dst=src_if.remote_ip6) /
98 UDP(sport=1234, dport=1234) /
105 def verify_filter(capture, sent):
106 if not len(capture) == len(sent):
107 # filter out any IPv6 RAs from the capture
113 def verify_capture_ip4(self, src_if, capture, sent, ping_resp=0):
115 capture = self.verify_filter(capture, sent)
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, 0x800)
131 self.assertEqual(rx_ip.src, tx_ip.src)
132 self.assertEqual(rx_ip.dst, tx_ip.dst)
133 # IP processing post pop has decremented the TTL
134 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
136 self.assertEqual(rx_ip.src, tx_ip.dst)
137 self.assertEqual(rx_ip.dst, tx_ip.src)
142 def verify_mpls_stack(self, rx, mpls_labels, ttl=255, num=0):
143 # the rx'd packet has the MPLS label popped
145 self.assertEqual(eth.type, 0x8847)
149 for ii in range(len(mpls_labels)):
150 self.assertEqual(rx_mpls.label, mpls_labels[ii])
151 self.assertEqual(rx_mpls.cos, 0)
153 self.assertEqual(rx_mpls.ttl, ttl)
155 self.assertEqual(rx_mpls.ttl, 255)
157 if ii == len(mpls_labels) - 1:
158 self.assertEqual(rx_mpls.s, 1)
161 self.assertEqual(rx_mpls.s, 0)
162 # pop the label to expose the next
163 rx_mpls = rx_mpls[MPLS].payload
165 def verify_capture_labelled_ip4(self, src_if, capture, sent,
168 capture = self.verify_filter(capture, sent)
170 self.assertEqual(len(capture), len(sent))
172 for i in range(len(capture)):
178 # the MPLS TTL is copied from the IP
179 self.verify_mpls_stack(
180 rx, mpls_labels, rx_ip.ttl, len(mpls_labels) - 1)
182 self.assertEqual(rx_ip.src, tx_ip.src)
183 self.assertEqual(rx_ip.dst, tx_ip.dst)
184 # IP processing post pop has decremented the TTL
185 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
190 def verify_capture_tunneled_ip4(self, src_if, capture, sent, mpls_labels):
192 capture = self.verify_filter(capture, sent)
194 self.assertEqual(len(capture), len(sent))
196 for i in range(len(capture)):
202 # the MPLS TTL is 255 since it enters a new tunnel
203 self.verify_mpls_stack(
204 rx, mpls_labels, 255, len(mpls_labels) - 1)
206 self.assertEqual(rx_ip.src, tx_ip.src)
207 self.assertEqual(rx_ip.dst, tx_ip.dst)
208 # IP processing post pop has decremented the TTL
209 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
214 def verify_capture_labelled(self, src_if, capture, sent,
215 mpls_labels, ttl=254, num=0):
217 capture = self.verify_filter(capture, sent)
219 self.assertEqual(len(capture), len(sent))
221 for i in range(len(capture)):
223 self.verify_mpls_stack(rx, mpls_labels, ttl, num)
227 def verify_capture_ip6(self, src_if, capture, sent):
229 self.assertEqual(len(capture), len(sent))
231 for i in range(len(capture)):
235 # the rx'd packet has the MPLS label popped
237 self.assertEqual(eth.type, 0x86DD)
242 self.assertEqual(rx_ip.src, tx_ip.src)
243 self.assertEqual(rx_ip.dst, tx_ip.dst)
244 # IP processing post pop has decremented the TTL
245 self.assertEqual(rx_ip.hlim + 1, tx_ip.hlim)
251 """ MPLS label swap tests """
254 # A simple MPLS xconnect - eos label in label out
256 route_32_eos = MplsRoute(self, 32, 1,
257 [RoutePath(self.pg0.remote_ip4,
258 self.pg0.sw_if_index,
260 route_32_eos.add_vpp_config()
263 # a stream that matches the route for 10.0.0.1
264 # PG0 is in the default table
266 self.vapi.cli("clear trace")
267 tx = self.create_stream_labelled_ip4(self.pg0, [32])
268 self.pg0.add_stream(tx)
270 self.pg_enable_capture(self.pg_interfaces)
273 rx = self.pg0.get_capture()
274 self.verify_capture_labelled_ip4(self.pg0, rx, tx, [33])
277 # A simple MPLS xconnect - non-eos label in label out
279 route_32_neos = MplsRoute(self, 32, 0,
280 [RoutePath(self.pg0.remote_ip4,
281 self.pg0.sw_if_index,
283 route_32_neos.add_vpp_config()
286 # a stream that matches the route for 10.0.0.1
287 # PG0 is in the default table
289 self.vapi.cli("clear trace")
290 tx = self.create_stream_labelled_ip4(self.pg0, [32, 99])
291 self.pg0.add_stream(tx)
293 self.pg_enable_capture(self.pg_interfaces)
296 rx = self.pg0.get_capture()
297 self.verify_capture_labelled(self.pg0, rx, tx, [33, 99])
300 # An MPLS xconnect - EOS label in IP out
302 route_33_eos = MplsRoute(self, 33, 1,
303 [RoutePath(self.pg0.remote_ip4,
304 self.pg0.sw_if_index,
306 route_33_eos.add_vpp_config()
308 self.vapi.cli("clear trace")
309 tx = self.create_stream_labelled_ip4(self.pg0, [33])
310 self.pg0.add_stream(tx)
312 self.pg_enable_capture(self.pg_interfaces)
315 rx = self.pg0.get_capture()
316 self.verify_capture_ip4(self.pg0, rx, tx)
319 # An MPLS xconnect - non-EOS label in IP out - an invalid configuration
320 # so this traffic should be dropped.
322 route_33_neos = MplsRoute(self, 33, 0,
323 [RoutePath(self.pg0.remote_ip4,
324 self.pg0.sw_if_index,
326 route_33_neos.add_vpp_config()
328 self.vapi.cli("clear trace")
329 tx = self.create_stream_labelled_ip4(self.pg0, [33, 99])
330 self.pg0.add_stream(tx)
332 self.pg_enable_capture(self.pg_interfaces)
335 rx = self.pg0.get_capture()
337 self.assertEqual(0, len(rx))
339 self.logger.error("MPLS non-EOS packets popped and forwarded")
340 self.logger.error(ppp("", rx))
344 # A recursive EOS x-connect, which resolves through another x-connect
346 route_34_eos = MplsRoute(self, 34, 1,
347 [RoutePath("0.0.0.0",
351 route_34_eos.add_vpp_config()
353 self.vapi.cli("clear trace")
354 tx = self.create_stream_labelled_ip4(self.pg0, [34])
355 self.pg0.add_stream(tx)
357 self.pg_enable_capture(self.pg_interfaces)
360 rx = self.pg0.get_capture()
361 self.verify_capture_labelled_ip4(self.pg0, rx, tx, [33, 44, 45])
364 # A recursive non-EOS x-connect, which resolves through another
367 route_34_neos = MplsRoute(self, 34, 0,
368 [RoutePath("0.0.0.0",
372 route_34_neos.add_vpp_config()
374 self.vapi.cli("clear trace")
375 tx = self.create_stream_labelled_ip4(self.pg0, [34, 99])
376 self.pg0.add_stream(tx)
378 self.pg_enable_capture(self.pg_interfaces)
381 rx = self.pg0.get_capture()
382 # it's the 2nd (counting from 0) label in the stack that is swapped
383 self.verify_capture_labelled(self.pg0, rx, tx, [33, 44, 46, 99], num=2)
386 # an recursive IP route that resolves through the recursive non-eos
389 ip_10_0_0_1 = IpRoute(self, "10.0.0.1", 32,
390 [RoutePath("0.0.0.0",
394 ip_10_0_0_1.add_vpp_config()
396 self.vapi.cli("clear trace")
397 tx = self.create_stream_ip4(self.pg0, "10.0.0.1")
398 self.pg0.add_stream(tx)
400 self.pg_enable_capture(self.pg_interfaces)
403 rx = self.pg0.get_capture()
404 self.verify_capture_labelled_ip4(self.pg0, rx, tx, [33, 44, 46, 55])
406 ip_10_0_0_1.remove_vpp_config()
407 route_34_neos.remove_vpp_config()
408 route_34_eos.remove_vpp_config()
409 route_33_neos.remove_vpp_config()
410 route_33_eos.remove_vpp_config()
411 route_32_neos.remove_vpp_config()
412 route_32_eos.remove_vpp_config()
415 """ MPLS Local Label Binding test """
418 # Add a non-recursive route with a single out label
420 route_10_0_0_1 = IpRoute(self, "10.0.0.1", 32,
421 [RoutePath(self.pg0.remote_ip4,
422 self.pg0.sw_if_index,
424 route_10_0_0_1.add_vpp_config()
426 # bind a local label to the route
427 binding = MplsIpBind(self, 44, "10.0.0.1", 32)
428 binding.add_vpp_config()
431 self.vapi.cli("clear trace")
432 tx = self.create_stream_labelled_ip4(self.pg0, [44, 99])
433 self.pg0.add_stream(tx)
435 self.pg_enable_capture(self.pg_interfaces)
438 rx = self.pg0.get_capture()
439 self.verify_capture_labelled(self.pg0, rx, tx, [45, 99])
442 self.vapi.cli("clear trace")
443 tx = self.create_stream_labelled_ip4(self.pg0, [44])
444 self.pg0.add_stream(tx)
446 self.pg_enable_capture(self.pg_interfaces)
449 rx = self.pg0.get_capture()
450 self.verify_capture_labelled(self.pg0, rx, tx, [45])
453 self.vapi.cli("clear trace")
454 tx = self.create_stream_ip4(self.pg0, "10.0.0.1")
455 self.pg0.add_stream(tx)
457 self.pg_enable_capture(self.pg_interfaces)
460 rx = self.pg0.get_capture()
461 self.verify_capture_labelled_ip4(self.pg0, rx, tx, [45])
466 binding.remove_vpp_config()
467 route_10_0_0_1.remove_vpp_config()
469 def test_imposition(self):
470 """ MPLS label imposition test """
473 # Add a non-recursive route with a single out label
475 route_10_0_0_1 = IpRoute(self, "10.0.0.1", 32,
476 [RoutePath(self.pg0.remote_ip4,
477 self.pg0.sw_if_index,
479 route_10_0_0_1.add_vpp_config()
482 # a stream that matches the route for 10.0.0.1
483 # PG0 is in the default table
485 self.vapi.cli("clear trace")
486 tx = self.create_stream_ip4(self.pg0, "10.0.0.1")
487 self.pg0.add_stream(tx)
489 self.pg_enable_capture(self.pg_interfaces)
492 rx = self.pg0.get_capture()
493 self.verify_capture_labelled_ip4(self.pg0, rx, tx, [32])
496 # Add a non-recursive route with a 3 out labels
498 route_10_0_0_2 = IpRoute(self, "10.0.0.2", 32,
499 [RoutePath(self.pg0.remote_ip4,
500 self.pg0.sw_if_index,
501 labels=[32, 33, 34])])
502 route_10_0_0_2.add_vpp_config()
505 # a stream that matches the route for 10.0.0.1
506 # PG0 is in the default table
508 self.vapi.cli("clear trace")
509 tx = self.create_stream_ip4(self.pg0, "10.0.0.2")
510 self.pg0.add_stream(tx)
512 self.pg_enable_capture(self.pg_interfaces)
515 rx = self.pg0.get_capture()
516 self.verify_capture_labelled_ip4(self.pg0, rx, tx, [32, 33, 34])
519 # add a recursive path, with output label, via the 1 label route
521 route_11_0_0_1 = IpRoute(self, "11.0.0.1", 32,
522 [RoutePath("10.0.0.1",
525 route_11_0_0_1.add_vpp_config()
528 # a stream that matches the route for 11.0.0.1, should pick up
529 # the label stack for 11.0.0.1 and 10.0.0.1
531 self.vapi.cli("clear trace")
532 tx = self.create_stream_ip4(self.pg0, "11.0.0.1")
533 self.pg0.add_stream(tx)
535 self.pg_enable_capture(self.pg_interfaces)
538 rx = self.pg0.get_capture()
539 self.verify_capture_labelled_ip4(self.pg0, rx, tx, [32, 44])
542 # add a recursive path, with 2 labels, via the 3 label route
544 route_11_0_0_2 = IpRoute(self, "11.0.0.2", 32,
545 [RoutePath("10.0.0.2",
548 route_11_0_0_2.add_vpp_config()
551 # a stream that matches the route for 11.0.0.1, should pick up
552 # the label stack for 11.0.0.1 and 10.0.0.1
554 self.vapi.cli("clear trace")
555 tx = self.create_stream_ip4(self.pg0, "11.0.0.2")
556 self.pg0.add_stream(tx)
558 self.pg_enable_capture(self.pg_interfaces)
561 rx = self.pg0.get_capture()
562 self.verify_capture_labelled_ip4(
563 self.pg0, rx, tx, [32, 33, 34, 44, 45])
568 route_11_0_0_2.remove_vpp_config()
569 route_11_0_0_1.remove_vpp_config()
570 route_10_0_0_2.remove_vpp_config()
571 route_10_0_0_1.remove_vpp_config()
573 def test_tunnel(self):
574 """ MPLS Tunnel Tests """
577 # Create a tunnel with a single out label
579 nh_addr = socket.inet_pton(socket.AF_INET, self.pg0.remote_ip4)
581 reply = self.vapi.mpls_tunnel_add_del(
582 0xffffffff, # don't know the if index yet
585 self.pg0.sw_if_index,
586 0, # next-hop-table-id
591 self.vapi.sw_interface_set_flags(reply.sw_if_index, admin_up_down=1)
594 # add an unlabelled route through the new tunnel
596 dest_addr = socket.inet_pton(socket.AF_INET, "10.0.0.3")
597 nh_addr = socket.inet_pton(socket.AF_INET, "0.0.0.0")
600 self.vapi.ip_add_del_route(
603 nh_addr, # all zeros next-hop - tunnel is p2p
604 reply.sw_if_index, # sw_if_index of the new tunnel
606 0, # next-hop-table-id
612 self.vapi.cli("clear trace")
613 tx = self.create_stream_ip4(self.pg0, "10.0.0.3")
614 self.pg0.add_stream(tx)
616 self.pg_enable_capture(self.pg_interfaces)
619 rx = self.pg0.get_capture()
620 self.verify_capture_tunneled_ip4(self.pg0, rx, tx, [44, 46])
622 def test_v4_exp_null(self):
623 """ MPLS V4 Explicit NULL test """
626 # The first test case has an MPLS TTL of 0
627 # all packet should be dropped
629 tx = self.create_stream_labelled_ip4(self.pg0, [0], 0)
630 self.pg0.add_stream(tx)
632 self.pg_enable_capture(self.pg_interfaces)
635 rx = self.pg0.get_capture()
638 self.assertEqual(0, len(rx))
640 self.logger.error("MPLS TTL=0 packets forwarded")
641 self.logger.error(ppp("", rx))
645 # a stream with a non-zero MPLS TTL
646 # PG0 is in the default table
648 self.vapi.cli("clear trace")
649 tx = self.create_stream_labelled_ip4(self.pg0, [0])
650 self.pg0.add_stream(tx)
652 self.pg_enable_capture(self.pg_interfaces)
655 rx = self.pg0.get_capture()
656 self.verify_capture_ip4(self.pg0, rx, tx)
659 # a stream with a non-zero MPLS TTL
661 # we are ensuring the post-pop lookup occurs in the VRF table
663 self.vapi.cli("clear trace")
664 tx = self.create_stream_labelled_ip4(self.pg1, [0])
665 self.pg1.add_stream(tx)
667 self.pg_enable_capture(self.pg_interfaces)
670 rx = self.pg1.get_capture()
671 self.verify_capture_ip4(self.pg0, rx, tx)
673 def test_v6_exp_null(self):
674 """ MPLS V6 Explicit NULL test """
677 # a stream with a non-zero MPLS TTL
678 # PG0 is in the default table
680 self.vapi.cli("clear trace")
681 tx = self.create_stream_labelled_ip6(self.pg0, 2, 2)
682 self.pg0.add_stream(tx)
684 self.pg_enable_capture(self.pg_interfaces)
687 rx = self.pg0.get_capture()
688 self.verify_capture_ip6(self.pg0, rx, tx)
691 # a stream with a non-zero MPLS TTL
693 # we are ensuring the post-pop lookup occurs in the VRF table
695 self.vapi.cli("clear trace")
696 tx = self.create_stream_labelled_ip6(self.pg1, 2, 2)
697 self.pg1.add_stream(tx)
699 self.pg_enable_capture(self.pg_interfaces)
702 rx = self.pg1.get_capture()
703 self.verify_capture_ip6(self.pg0, rx, tx)
709 # A de-agg route - next-hop lookup in default table
711 route_34_eos = MplsRoute(self, 34, 1,
712 [RoutePath("0.0.0.0",
715 route_34_eos.add_vpp_config()
718 # ping an interface in the default table
719 # PG0 is in the default table
721 self.vapi.cli("clear trace")
722 tx = self.create_stream_labelled_ip4(self.pg0, [34], ping=1,
724 self.pg0.add_stream(tx)
726 self.pg_enable_capture(self.pg_interfaces)
729 rx = self.pg0.get_capture()
730 self.verify_capture_ip4(self.pg0, rx, tx, ping_resp=1)
733 # A de-agg route - next-hop lookup in non-default table
735 route_35_eos = MplsRoute(self, 35, 1,
736 [RoutePath("0.0.0.0",
739 route_35_eos.add_vpp_config()
742 # ping an interface in the non-default table
743 # PG0 is in the default table. packet arrive labelled in the
744 # default table and egress unlabelled in the non-default
746 self.vapi.cli("clear trace")
747 tx = self.create_stream_labelled_ip4(self.pg0, [35], ping=1, ip_itf=self.pg1)
748 self.pg0.add_stream(tx)
750 self.pg_enable_capture(self.pg_interfaces)
753 rx = self.pg1.get_capture()
754 self.verify_capture_ip4(self.pg1, rx, tx, ping_resp=1)
756 route_35_eos.remove_vpp_config()
757 route_34_eos.remove_vpp_config()
759 if __name__ == '__main__':
760 unittest.main(testRunner=VppTestRunner)