6 from framework import VppTestCase, VppTestRunner
7 from vpp_ip_route import VppIpRoute, VppRoutePath, VppMplsRoute, \
10 from scapy.packet import Raw
11 from scapy.layers.l2 import Ether
12 from scapy.layers.inet import IP, UDP, ICMP
13 from scapy.layers.inet6 import IPv6
14 from scapy.contrib.mpls import MPLS
17 class TestMPLS(VppTestCase):
18 """ MPLS Test Case """
21 super(TestMPLS, self).setUp()
23 # create 2 pg interfaces
24 self.create_pg_interfaces(range(2))
26 # setup both interfaces
27 # assign them different tables.
30 for i in self.pg_interfaces:
32 i.set_table_ip4(table_id)
33 i.set_table_ip6(table_id)
42 super(TestMPLS, self).tearDown()
43 for i in self.pg_interfaces:
49 # the default of 64 matches the IP packet TTL default
50 def create_stream_labelled_ip4(
57 self.reset_packet_infos()
59 for i in range(0, 257):
60 info = self.create_packet_info(src_if, src_if)
61 payload = self.info_to_payload(info)
62 p = Ether(dst=src_if.local_mac, src=src_if.remote_mac)
64 for ii in range(len(mpls_labels)):
65 if ii == len(mpls_labels) - 1:
66 p = p / MPLS(label=mpls_labels[ii], ttl=mpls_ttl, s=1)
68 p = p / MPLS(label=mpls_labels[ii], ttl=mpls_ttl, s=0)
70 p = (p / IP(src=src_if.local_ip4, dst=src_if.remote_ip4) /
71 UDP(sport=1234, dport=1234) /
74 p = (p / IP(src=ip_itf.remote_ip4,
75 dst=ip_itf.local_ip4) /
82 def create_stream_ip4(self, src_if, dst_ip):
83 self.reset_packet_infos()
85 for i in range(0, 257):
86 info = self.create_packet_info(src_if, src_if)
87 payload = self.info_to_payload(info)
88 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
89 IP(src=src_if.remote_ip4, dst=dst_ip) /
90 UDP(sport=1234, dport=1234) /
96 def create_stream_labelled_ip6(self, src_if, mpls_label, mpls_ttl):
97 self.reset_packet_infos()
99 for i in range(0, 257):
100 info = self.create_packet_info(src_if, src_if)
101 payload = self.info_to_payload(info)
102 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
103 MPLS(label=mpls_label, ttl=mpls_ttl) /
104 IPv6(src=src_if.remote_ip6, dst=src_if.remote_ip6) /
105 UDP(sport=1234, dport=1234) /
112 def verify_filter(capture, sent):
113 if not len(capture) == len(sent):
114 # filter out any IPv6 RAs from the capture
120 def verify_capture_ip4(self, src_if, capture, sent, ping_resp=0):
122 capture = self.verify_filter(capture, sent)
124 self.assertEqual(len(capture), len(sent))
126 for i in range(len(capture)):
130 # the rx'd packet has the MPLS label popped
132 self.assertEqual(eth.type, 0x800)
138 self.assertEqual(rx_ip.src, tx_ip.src)
139 self.assertEqual(rx_ip.dst, tx_ip.dst)
140 # IP processing post pop has decremented the TTL
141 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
143 self.assertEqual(rx_ip.src, tx_ip.dst)
144 self.assertEqual(rx_ip.dst, tx_ip.src)
149 def verify_mpls_stack(self, rx, mpls_labels, ttl=255, num=0):
150 # the rx'd packet has the MPLS label popped
152 self.assertEqual(eth.type, 0x8847)
156 for ii in range(len(mpls_labels)):
157 self.assertEqual(rx_mpls.label, mpls_labels[ii])
158 self.assertEqual(rx_mpls.cos, 0)
160 self.assertEqual(rx_mpls.ttl, ttl)
162 self.assertEqual(rx_mpls.ttl, 255)
164 if ii == len(mpls_labels) - 1:
165 self.assertEqual(rx_mpls.s, 1)
168 self.assertEqual(rx_mpls.s, 0)
169 # pop the label to expose the next
170 rx_mpls = rx_mpls[MPLS].payload
172 def verify_capture_labelled_ip4(self, src_if, capture, sent,
175 capture = self.verify_filter(capture, sent)
177 self.assertEqual(len(capture), len(sent))
179 for i in range(len(capture)):
185 # the MPLS TTL is copied from the IP
186 self.verify_mpls_stack(
187 rx, mpls_labels, rx_ip.ttl, len(mpls_labels) - 1)
189 self.assertEqual(rx_ip.src, tx_ip.src)
190 self.assertEqual(rx_ip.dst, tx_ip.dst)
191 # IP processing post pop has decremented the TTL
192 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
197 def verify_capture_tunneled_ip4(self, src_if, capture, sent, mpls_labels):
199 capture = self.verify_filter(capture, sent)
201 self.assertEqual(len(capture), len(sent))
203 for i in range(len(capture)):
209 # the MPLS TTL is 255 since it enters a new tunnel
210 self.verify_mpls_stack(
211 rx, mpls_labels, 255, len(mpls_labels) - 1)
213 self.assertEqual(rx_ip.src, tx_ip.src)
214 self.assertEqual(rx_ip.dst, tx_ip.dst)
215 # IP processing post pop has decremented the TTL
216 self.assertEqual(rx_ip.ttl + 1, tx_ip.ttl)
221 def verify_capture_labelled(self, src_if, capture, sent,
222 mpls_labels, ttl=254, num=0):
224 capture = self.verify_filter(capture, sent)
226 self.assertEqual(len(capture), len(sent))
228 for i in range(len(capture)):
230 self.verify_mpls_stack(rx, mpls_labels, ttl, num)
234 def verify_capture_ip6(self, src_if, capture, sent):
236 self.assertEqual(len(capture), len(sent))
238 for i in range(len(capture)):
242 # the rx'd packet has the MPLS label popped
244 self.assertEqual(eth.type, 0x86DD)
249 self.assertEqual(rx_ip.src, tx_ip.src)
250 self.assertEqual(rx_ip.dst, tx_ip.dst)
251 # IP processing post pop has decremented the TTL
252 self.assertEqual(rx_ip.hlim + 1, tx_ip.hlim)
258 """ MPLS label swap tests """
261 # A simple MPLS xconnect - eos label in label out
263 route_32_eos = VppMplsRoute(self, 32, 1,
264 [VppRoutePath(self.pg0.remote_ip4,
265 self.pg0.sw_if_index,
267 route_32_eos.add_vpp_config()
270 # a stream that matches the route for 10.0.0.1
271 # PG0 is in the default table
273 self.vapi.cli("clear trace")
274 tx = self.create_stream_labelled_ip4(self.pg0, [32])
275 self.pg0.add_stream(tx)
277 self.pg_enable_capture(self.pg_interfaces)
280 rx = self.pg0.get_capture()
281 self.verify_capture_labelled_ip4(self.pg0, rx, tx, [33])
284 # A simple MPLS xconnect - non-eos label in label out
286 route_32_neos = VppMplsRoute(self, 32, 0,
287 [VppRoutePath(self.pg0.remote_ip4,
288 self.pg0.sw_if_index,
290 route_32_neos.add_vpp_config()
293 # a stream that matches the route for 10.0.0.1
294 # PG0 is in the default table
296 self.vapi.cli("clear trace")
297 tx = self.create_stream_labelled_ip4(self.pg0, [32, 99])
298 self.pg0.add_stream(tx)
300 self.pg_enable_capture(self.pg_interfaces)
303 rx = self.pg0.get_capture()
304 self.verify_capture_labelled(self.pg0, rx, tx, [33, 99])
307 # An MPLS xconnect - EOS label in IP out
309 route_33_eos = VppMplsRoute(self, 33, 1,
310 [VppRoutePath(self.pg0.remote_ip4,
311 self.pg0.sw_if_index,
313 route_33_eos.add_vpp_config()
315 self.vapi.cli("clear trace")
316 tx = self.create_stream_labelled_ip4(self.pg0, [33])
317 self.pg0.add_stream(tx)
319 self.pg_enable_capture(self.pg_interfaces)
322 rx = self.pg0.get_capture()
323 self.verify_capture_ip4(self.pg0, rx, tx)
326 # An MPLS xconnect - non-EOS label in IP out - an invalid configuration
327 # so this traffic should be dropped.
329 route_33_neos = VppMplsRoute(self, 33, 0,
330 [VppRoutePath(self.pg0.remote_ip4,
331 self.pg0.sw_if_index,
333 route_33_neos.add_vpp_config()
335 self.vapi.cli("clear trace")
336 tx = self.create_stream_labelled_ip4(self.pg0, [33, 99])
337 self.pg0.add_stream(tx)
339 self.pg_enable_capture(self.pg_interfaces)
341 self.pg0.assert_nothing_captured(
342 remark="MPLS non-EOS packets popped and forwarded")
345 # A recursive EOS x-connect, which resolves through another x-connect
347 route_34_eos = VppMplsRoute(self, 34, 1,
348 [VppRoutePath("0.0.0.0",
352 route_34_eos.add_vpp_config()
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 = VppMplsRoute(self, 34, 0,
368 [VppRoutePath("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 = VppIpRoute(self, "10.0.0.1", 32,
390 [VppRoutePath("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 = VppIpRoute(self, "10.0.0.1", 32,
421 [VppRoutePath(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 = VppMplsIpBind(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 = VppIpRoute(self, "10.0.0.1", 32,
476 [VppRoutePath(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 = VppIpRoute(self, "10.0.0.2", 32,
499 [VppRoutePath(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 = VppIpRoute(self, "11.0.0.1", 32,
522 [VppRoutePath("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 = VppIpRoute(self, "11.0.0.2", 32,
545 [VppRoutePath("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
590 self.vapi.sw_interface_set_flags(reply.sw_if_index, admin_up_down=1)
593 # add an unlabelled route through the new tunnel
595 route_10_0_0_3 = VppIpRoute(self, "10.0.0.3", 32,
596 [VppRoutePath("0.0.0.0",
598 route_10_0_0_3.add_vpp_config()
600 self.vapi.cli("clear trace")
601 tx = self.create_stream_ip4(self.pg0, "10.0.0.3")
602 self.pg0.add_stream(tx)
604 self.pg_enable_capture(self.pg_interfaces)
607 rx = self.pg0.get_capture()
608 self.verify_capture_tunneled_ip4(self.pg0, rx, tx, [44, 46])
610 def test_v4_exp_null(self):
611 """ MPLS V4 Explicit NULL test """
614 # The first test case has an MPLS TTL of 0
615 # all packet should be dropped
617 tx = self.create_stream_labelled_ip4(self.pg0, [0], 0)
618 self.pg0.add_stream(tx)
620 self.pg_enable_capture(self.pg_interfaces)
623 self.pg0.assert_nothing_captured(remark="MPLS TTL=0 packets forwarded")
626 # a stream with a non-zero MPLS TTL
627 # PG0 is in the default table
629 tx = self.create_stream_labelled_ip4(self.pg0, [0])
630 self.pg0.add_stream(tx)
632 self.pg_enable_capture(self.pg_interfaces)
635 rx = self.pg0.get_capture()
636 self.verify_capture_ip4(self.pg0, rx, tx)
639 # a stream with a non-zero MPLS TTL
641 # we are ensuring the post-pop lookup occurs in the VRF table
643 self.vapi.cli("clear trace")
644 tx = self.create_stream_labelled_ip4(self.pg1, [0])
645 self.pg1.add_stream(tx)
647 self.pg_enable_capture(self.pg_interfaces)
650 rx = self.pg1.get_capture()
651 self.verify_capture_ip4(self.pg0, rx, tx)
653 def test_v6_exp_null(self):
654 """ MPLS V6 Explicit NULL test """
657 # a stream with a non-zero MPLS TTL
658 # PG0 is in the default table
660 self.vapi.cli("clear trace")
661 tx = self.create_stream_labelled_ip6(self.pg0, 2, 2)
662 self.pg0.add_stream(tx)
664 self.pg_enable_capture(self.pg_interfaces)
667 rx = self.pg0.get_capture()
668 self.verify_capture_ip6(self.pg0, rx, tx)
671 # a stream with a non-zero MPLS TTL
673 # we are ensuring the post-pop lookup occurs in the VRF table
675 self.vapi.cli("clear trace")
676 tx = self.create_stream_labelled_ip6(self.pg1, 2, 2)
677 self.pg1.add_stream(tx)
679 self.pg_enable_capture(self.pg_interfaces)
682 rx = self.pg1.get_capture()
683 self.verify_capture_ip6(self.pg0, rx, tx)
689 # A de-agg route - next-hop lookup in default table
691 route_34_eos = VppMplsRoute(self, 34, 1,
692 [VppRoutePath("0.0.0.0",
695 route_34_eos.add_vpp_config()
698 # ping an interface in the default table
699 # PG0 is in the default table
701 self.vapi.cli("clear trace")
702 tx = self.create_stream_labelled_ip4(self.pg0, [34], ping=1,
704 self.pg0.add_stream(tx)
706 self.pg_enable_capture(self.pg_interfaces)
709 rx = self.pg0.get_capture()
710 self.verify_capture_ip4(self.pg0, rx, tx, ping_resp=1)
713 # A de-agg route - next-hop lookup in non-default table
715 route_35_eos = VppMplsRoute(self, 35, 1,
716 [VppRoutePath("0.0.0.0",
719 route_35_eos.add_vpp_config()
722 # ping an interface in the non-default table
723 # PG0 is in the default table. packet arrive labelled in the
724 # default table and egress unlabelled in the non-default
726 self.vapi.cli("clear trace")
727 tx = self.create_stream_labelled_ip4(
728 self.pg0, [35], ping=1, ip_itf=self.pg1)
729 self.pg0.add_stream(tx)
731 self.pg_enable_capture(self.pg_interfaces)
734 packet_count = self.get_packet_count_for_if_idx(self.pg0.sw_if_index)
735 rx = self.pg1.get_capture(packet_count)
736 self.verify_capture_ip4(self.pg1, rx, tx, ping_resp=1)
738 route_35_eos.remove_vpp_config()
739 route_34_eos.remove_vpp_config()
741 if __name__ == '__main__':
742 unittest.main(testRunner=VppTestRunner)