6 from framework import VppTestCase, VppTestRunner, running_extended_tests
7 from vpp_ip_route import VppIpRoute, VppRoutePath, VppMplsRoute, \
8 VppMplsTable, VppIpMRoute, VppMRoutePath, VppIpTable, \
9 MRouteEntryFlags, MRouteItfFlags, MPLS_LABEL_INVALID, DpoProto, \
11 from vpp_bier import *
12 from vpp_udp_encap import *
14 from scapy.packet import Raw
15 from scapy.layers.l2 import Ether
16 from scapy.layers.inet import IP, UDP, ICMP
17 from scapy.layers.inet6 import IPv6
18 from scapy.contrib.mpls import MPLS
19 from scapy.contrib.bier import *
22 class TestBFIB(VppTestCase):
23 """ BIER FIB Test Case """
26 """ BFIB Unit Tests """
27 error = self.vapi.cli("test bier")
30 self.logger.critical(error)
31 self.assertEqual(error.find("Failed"), -1)
34 class TestBier(VppTestCase):
35 """ BIER Test Case """
38 super(TestBier, self).setUp()
40 # create 2 pg interfaces
41 self.create_pg_interfaces(range(3))
43 # create the default MPLS table
45 tbl = VppMplsTable(self, 0)
47 self.tables.append(tbl)
49 tbl = VppIpTable(self, 10)
51 self.tables.append(tbl)
53 # setup both interfaces
54 for i in self.pg_interfaces:
63 for i in self.pg_interfaces:
68 super(TestBier, self).tearDown()
70 def bier_midpoint(self, hdr_len_id, n_bytes, max_bp):
74 # Add a BIER table for sub-domain 0, set 0, and BSL 256
76 bti = VppBierTableID(0, 0, hdr_len_id)
77 bt = VppBierTable(self, bti, 77)
81 # A packet with no bits set gets dropped
83 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
84 MPLS(label=77, ttl=255) /
85 BIER(length=hdr_len_id) /
86 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
87 UDP(sport=1234, dport=1234) /
91 self.send_and_assert_no_replies(self.pg0, pkts,
95 # Add a BIER route for each bit-position in the table via a different
96 # next-hop. Testing whether the BIER walk and replicate forwarding
97 # function works for all bit posisitons.
101 for i in range(1, max_bp+1):
102 nh = "10.0.%d.%d" % (i / 255, i % 255)
104 VppIpRoute(self, nh, 32,
105 [VppRoutePath(self.pg1.remote_ip4,
106 self.pg1.sw_if_index,
107 labels=[VppMplsLabel(2000+i)])]))
108 nh_routes[-1].add_vpp_config()
111 VppBierRoute(self, bti, i,
112 [VppRoutePath(nh, 0xffffffff,
113 labels=[VppMplsLabel(100+i)])]))
114 bier_routes[-1].add_vpp_config()
117 # A packet with all bits set gets replicated once for each bit
119 pkt_sizes = [64, 1400]
121 for pkt_size in pkt_sizes:
122 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
123 MPLS(label=77, ttl=255) /
124 BIER(length=hdr_len_id, BitString=chr(255)*n_bytes) /
125 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
126 UDP(sport=1234, dport=1234) /
127 Raw(chr(5) * pkt_size))
130 self.pg0.add_stream(pkts)
131 self.pg_enable_capture(self.pg_interfaces)
134 rx = self.pg1.get_capture(max_bp)
138 # The packets are not required to be sent in bit-position order
139 # when we setup the routes above we used the bit-position to
140 # construct the out-label. so use that here to determine the BP
143 bp = olabel.label - 2000
145 blabel = olabel[MPLS].payload
146 self.assertEqual(blabel.label, 100+bp)
147 self.assertEqual(blabel.ttl, 254)
149 bier_hdr = blabel[MPLS].payload
151 self.assertEqual(bier_hdr.id, 5)
152 self.assertEqual(bier_hdr.version, 0)
153 self.assertEqual(bier_hdr.length, hdr_len_id)
154 self.assertEqual(bier_hdr.entropy, 0)
155 self.assertEqual(bier_hdr.OAM, 0)
156 self.assertEqual(bier_hdr.RSV, 0)
157 self.assertEqual(bier_hdr.DSCP, 0)
158 self.assertEqual(bier_hdr.Proto, 5)
160 # The bit-string should consist only of the BP given by i.
161 byte_array = ['\0'] * (n_bytes)
162 byte_val = chr(1 << (bp - 1) % 8)
163 byte_pos = n_bytes - (((bp - 1) / 8) + 1)
164 byte_array[byte_pos] = byte_val
165 bitstring = ''.join(byte_array)
167 self.assertEqual(len(bitstring), len(bier_hdr.BitString))
168 self.assertEqual(bitstring, bier_hdr.BitString)
171 # cleanup. not strictly necessary, but it's much quicker this way
172 # becuase the bier_fib_dump and ip_fib_dump will be empty when the
173 # auto-cleanup kicks in
175 for br in bier_routes:
176 br.remove_vpp_config()
177 for nhr in nh_routes:
178 nhr.remove_vpp_config()
180 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
181 def test_bier_midpoint_1024(self):
182 """BIER midpoint BSL:1024"""
183 self.bier_midpoint(BIERLength.BIER_LEN_1024, 128, 1024)
185 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
186 def test_bier_midpoint_512(self):
187 """BIER midpoint BSL:512"""
188 self.bier_midpoint(BIERLength.BIER_LEN_512, 64, 512)
190 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
191 def test_bier_midpoint_256(self):
192 """BIER midpoint BSL:256"""
193 self.bier_midpoint(BIERLength.BIER_LEN_256, 32, 256)
195 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
196 def test_bier_midpoint_128(self):
197 """BIER midpoint BSL:128"""
198 self.bier_midpoint(BIERLength.BIER_LEN_128, 16, 128)
200 def test_bier_midpoint_64(self):
201 """BIER midpoint BSL:64"""
202 self.bier_midpoint(BIERLength.BIER_LEN_64, 8, 64)
204 def test_bier_head(self):
208 # Add a BIER table for sub-domain 0, set 0, and BSL 256
210 bti = VppBierTableID(0, 0, BIERLength.BIER_LEN_256)
211 bt = VppBierTable(self, bti, 77)
215 # 2 bit positions via two next hops
219 ip_route_1 = VppIpRoute(self, nh1, 32,
220 [VppRoutePath(self.pg1.remote_ip4,
221 self.pg1.sw_if_index,
222 labels=[VppMplsLabel(2001)])])
223 ip_route_2 = VppIpRoute(self, nh2, 32,
224 [VppRoutePath(self.pg1.remote_ip4,
225 self.pg1.sw_if_index,
226 labels=[VppMplsLabel(2002)])])
227 ip_route_1.add_vpp_config()
228 ip_route_2.add_vpp_config()
230 bier_route_1 = VppBierRoute(self, bti, 1,
231 [VppRoutePath(nh1, 0xffffffff,
232 labels=[VppMplsLabel(101)])])
233 bier_route_2 = VppBierRoute(self, bti, 2,
234 [VppRoutePath(nh2, 0xffffffff,
235 labels=[VppMplsLabel(102)])])
236 bier_route_1.add_vpp_config()
237 bier_route_2.add_vpp_config()
240 # An imposition object with both bit-positions set
242 bi = VppBierImp(self, bti, 333, chr(0x3) * 32)
246 # Add a multicast route that will forward into the BIER doamin
248 route_ing_232_1_1_1 = VppIpMRoute(
252 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
253 paths=[VppMRoutePath(self.pg0.sw_if_index,
254 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
255 VppMRoutePath(0xffffffff,
256 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
257 proto=DpoProto.DPO_PROTO_BIER,
258 bier_imp=bi.bi_index)])
259 route_ing_232_1_1_1.add_vpp_config()
262 # inject an IP packet. We expect it to be BIER encapped and
265 p = (Ether(dst=self.pg0.local_mac,
266 src=self.pg0.remote_mac) /
267 IP(src="1.1.1.1", dst="232.1.1.1") /
268 UDP(sport=1234, dport=1234))
270 self.pg0.add_stream([p])
271 self.pg_enable_capture(self.pg_interfaces)
274 rx = self.pg1.get_capture(2)
277 # Encap Stack is; eth, MPLS, MPLS, BIER
279 igp_mpls = rx[0][MPLS]
280 self.assertEqual(igp_mpls.label, 2001)
281 self.assertEqual(igp_mpls.ttl, 64)
282 self.assertEqual(igp_mpls.s, 0)
283 bier_mpls = igp_mpls[MPLS].payload
284 self.assertEqual(bier_mpls.label, 101)
285 self.assertEqual(bier_mpls.ttl, 64)
286 self.assertEqual(bier_mpls.s, 1)
287 self.assertEqual(rx[0][BIER].length, 2)
289 igp_mpls = rx[1][MPLS]
290 self.assertEqual(igp_mpls.label, 2002)
291 self.assertEqual(igp_mpls.ttl, 64)
292 self.assertEqual(igp_mpls.s, 0)
293 bier_mpls = igp_mpls[MPLS].payload
294 self.assertEqual(bier_mpls.label, 102)
295 self.assertEqual(bier_mpls.ttl, 64)
296 self.assertEqual(bier_mpls.s, 1)
297 self.assertEqual(rx[0][BIER].length, 2)
299 def test_bier_tail(self):
303 # Add a BIER table for sub-domain 0, set 0, and BSL 256
305 bti = VppBierTableID(0, 0, BIERLength.BIER_LEN_256)
306 bt = VppBierTable(self, bti, 77)
312 bdt = VppBierDispTable(self, 8)
316 # BIER route in table that's for-us
318 bier_route_1 = VppBierRoute(
320 [VppRoutePath("0.0.0.0",
322 proto=DpoProto.DPO_PROTO_BIER,
324 bier_route_1.add_vpp_config()
327 # An entry in the disposition table
329 bier_de_1 = VppBierDispEntry(self, bdt.id, 99,
330 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
331 DpoProto.DPO_PROTO_BIER,
332 "0.0.0.0", 0, rpf_id=8192)
333 bier_de_1.add_vpp_config()
336 # A multicast route to forward post BIER disposition
338 route_eg_232_1_1_1 = VppIpMRoute(
342 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
343 paths=[VppMRoutePath(self.pg1.sw_if_index,
344 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
345 route_eg_232_1_1_1.add_vpp_config()
346 route_eg_232_1_1_1.update_rpf_id(8192)
349 # A packet with all bits set gets spat out to BP:1
351 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
352 MPLS(label=77, ttl=255) /
353 BIER(length=BIERLength.BIER_LEN_256,
354 BitString=chr(255)*32,
356 IP(src="1.1.1.1", dst="232.1.1.1") /
357 UDP(sport=1234, dport=1234) /
360 self.send_and_expect(self.pg0, [p], self.pg1)
363 # A packet that does not match the Disposition entry gets dropped
365 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
366 MPLS(label=77, ttl=255) /
367 BIER(length=BIERLength.BIER_LEN_256,
368 BitString=chr(255)*32,
370 IP(src="1.1.1.1", dst="232.1.1.1") /
371 UDP(sport=1234, dport=1234) /
373 self.send_and_assert_no_replies(self.pg0, p*2,
374 "no matching disposition entry")
377 # Add the default route to the disposition table
379 bier_de_2 = VppBierDispEntry(self, bdt.id, 0,
380 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
381 DpoProto.DPO_PROTO_BIER,
382 "0.0.0.0", 0, rpf_id=8192)
383 bier_de_2.add_vpp_config()
386 # now the previous packet is forwarded
388 self.send_and_expect(self.pg0, [p], self.pg1)
390 def bier_e2e(self, hdr_len_id, n_bytes, max_bp):
391 """ BIER end-to-end"""
394 # Add a BIER table for sub-domain 0, set 0, and BSL 256
396 bti = VppBierTableID(0, 0, hdr_len_id)
397 bt = VppBierTable(self, bti, 77)
400 lowest = ['\0'] * (n_bytes)
402 highest = ['\0'] * (n_bytes)
403 highest[0] = chr(128)
406 # Impostion Sets bit strings
408 bi_low = VppBierImp(self, bti, 333, lowest)
409 bi_low.add_vpp_config()
410 bi_high = VppBierImp(self, bti, 334, highest)
411 bi_high.add_vpp_config()
414 # Add a multicast route that will forward into the BIER doamin
416 route_ing_232_1_1_1 = VppIpMRoute(
420 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
421 paths=[VppMRoutePath(self.pg0.sw_if_index,
422 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
423 VppMRoutePath(0xffffffff,
424 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
425 proto=DpoProto.DPO_PROTO_BIER,
426 bier_imp=bi_low.bi_index)])
427 route_ing_232_1_1_1.add_vpp_config()
428 route_ing_232_1_1_2 = VppIpMRoute(
432 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
433 paths=[VppMRoutePath(self.pg0.sw_if_index,
434 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
435 VppMRoutePath(0xffffffff,
436 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
437 proto=DpoProto.DPO_PROTO_BIER,
438 bier_imp=bi_high.bi_index)])
439 route_ing_232_1_1_2.add_vpp_config()
442 # disposition table 8
444 bdt = VppBierDispTable(self, 8)
448 # BIER routes in table that are for-us, resolving through
451 bier_route_1 = VppBierRoute(
453 [VppRoutePath("0.0.0.0",
455 proto=DpoProto.DPO_PROTO_BIER,
457 bier_route_1.add_vpp_config()
458 bier_route_max = VppBierRoute(self, bti, max_bp,
459 [VppRoutePath("0.0.0.0",
462 bier_route_max.add_vpp_config()
465 # An entry in the disposition table for sender 333
468 bier_de_1 = VppBierDispEntry(self, bdt.id, 333,
469 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
470 DpoProto.DPO_PROTO_BIER,
471 "0.0.0.0", 10, rpf_id=8192)
472 bier_de_1.add_vpp_config()
473 bier_de_1 = VppBierDispEntry(self, bdt.id, 334,
474 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
475 DpoProto.DPO_PROTO_BIER,
476 "0.0.0.0", 10, rpf_id=8193)
477 bier_de_1.add_vpp_config()
480 # Add a multicast routes that will forward the traffic
483 route_eg_232_1_1_1 = VppIpMRoute(
487 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
489 paths=[VppMRoutePath(self.pg1.sw_if_index,
490 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
491 route_eg_232_1_1_1.add_vpp_config()
492 route_eg_232_1_1_1.update_rpf_id(8192)
493 route_eg_232_1_1_2 = VppIpMRoute(
497 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
499 paths=[VppMRoutePath(self.pg1.sw_if_index,
500 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
501 route_eg_232_1_1_2.add_vpp_config()
502 route_eg_232_1_1_2.update_rpf_id(8193)
505 # inject a packet in VRF-0. We expect it to be BIER encapped,
506 # replicated, then hit the disposition and be forwarded
507 # out of VRF 10, i.e. on pg1
509 p = (Ether(dst=self.pg0.local_mac,
510 src=self.pg0.remote_mac) /
511 IP(src="1.1.1.1", dst="232.1.1.1") /
512 UDP(sport=1234, dport=1234) /
515 rx = self.send_and_expect(self.pg0, p*65, self.pg1)
517 self.assertEqual(rx[0][IP].src, "1.1.1.1")
518 self.assertEqual(rx[0][IP].dst, "232.1.1.1")
520 p = (Ether(dst=self.pg0.local_mac,
521 src=self.pg0.remote_mac) /
522 IP(src="1.1.1.1", dst="232.1.1.2") /
523 UDP(sport=1234, dport=1234) /
526 rx = self.send_and_expect(self.pg0, p*65, self.pg1)
527 self.assertEqual(rx[0][IP].src, "1.1.1.1")
528 self.assertEqual(rx[0][IP].dst, "232.1.1.2")
530 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
531 def test_bier_e2e_1024(self):
532 """ BIER end-to-end BSL:1024"""
533 self.bier_e2e(BIERLength.BIER_LEN_1024, 128, 1024)
535 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
536 def test_bier_e2e_512(self):
537 """ BIER end-to-end BSL:512"""
538 self.bier_e2e(BIERLength.BIER_LEN_512, 64, 512)
540 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
541 def test_bier_e2e_256(self):
542 """ BIER end-to-end BSL:256"""
543 self.bier_e2e(BIERLength.BIER_LEN_256, 32, 256)
545 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
546 def test_bier_e2e_128(self):
547 """ BIER end-to-end BSL:128"""
548 self.bier_e2e(BIERLength.BIER_LEN_128, 16, 128)
550 def test_bier_e2e_64(self):
551 """ BIER end-to-end BSL:64"""
552 self.bier_e2e(BIERLength.BIER_LEN_64, 8, 64)
554 def test_bier_head_o_udp(self):
555 """BIER head over UDP"""
558 # Add a BIER table for sub-domain 1, set 0, and BSL 256
560 bti = VppBierTableID(1, 0, BIERLength.BIER_LEN_256)
561 bt = VppBierTable(self, bti, 77)
565 # 1 bit positions via 1 next hops
568 ip_route = VppIpRoute(self, nh1, 32,
569 [VppRoutePath(self.pg1.remote_ip4,
570 self.pg1.sw_if_index,
571 labels=[VppMplsLabel(2001)])])
572 ip_route.add_vpp_config()
574 udp_encap = VppUdpEncap(self, 4,
578 udp_encap.add_vpp_config()
580 bier_route = VppBierRoute(
582 [VppRoutePath("0.0.0.0",
586 bier_route.add_vpp_config()
589 # An 2 imposition objects with all bit-positions set
590 # only use the second, but creating 2 tests with a non-zero
591 # value index in the route add
593 bi = VppBierImp(self, bti, 333, chr(0xff) * 32)
595 bi2 = VppBierImp(self, bti, 334, chr(0xff) * 32)
599 # Add a multicast route that will forward into the BIER doamin
601 route_ing_232_1_1_1 = VppIpMRoute(
605 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
606 paths=[VppMRoutePath(self.pg0.sw_if_index,
607 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
608 VppMRoutePath(0xffffffff,
609 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
610 proto=DpoProto.DPO_PROTO_BIER,
611 bier_imp=bi2.bi_index)])
612 route_ing_232_1_1_1.add_vpp_config()
615 # inject a packet an IP. We expect it to be BIER and UDP encapped,
617 p = (Ether(dst=self.pg0.local_mac,
618 src=self.pg0.remote_mac) /
619 IP(src="1.1.1.1", dst="232.1.1.1") /
620 UDP(sport=1234, dport=1234))
622 self.pg0.add_stream([p])
623 self.pg_enable_capture(self.pg_interfaces)
626 rx = self.pg1.get_capture(1)
629 # Encap Stack is, eth, IP, UDP, BIFT, BIER
631 self.assertEqual(rx[0][IP].src, self.pg0.local_ip4)
632 self.assertEqual(rx[0][IP].dst, nh1)
633 self.assertEqual(rx[0][UDP].sport, 330)
634 self.assertEqual(rx[0][UDP].dport, 8138)
635 self.assertEqual(rx[0][BIFT].bsl, 2)
636 self.assertEqual(rx[0][BIFT].sd, 1)
637 self.assertEqual(rx[0][BIFT].set, 0)
638 self.assertEqual(rx[0][BIFT].ttl, 64)
639 self.assertEqual(rx[0][BIER].length, 2)
641 def test_bier_tail_o_udp(self):
642 """BIER Tail over UDP"""
645 # Add a BIER table for sub-domain 0, set 0, and BSL 256
647 bti = VppBierTableID(1, 0, BIERLength.BIER_LEN_256)
648 bt = VppBierTable(self, bti, MPLS_LABEL_INVALID)
654 bdt = VppBierDispTable(self, 8)
658 # BIER route in table that's for-us
660 bier_route_1 = VppBierRoute(
662 [VppRoutePath("0.0.0.0",
664 proto=DpoProto.DPO_PROTO_BIER,
666 bier_route_1.add_vpp_config()
669 # An entry in the disposition table
671 bier_de_1 = VppBierDispEntry(self, bdt.id, 99,
672 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
673 DpoProto.DPO_PROTO_BIER,
674 "0.0.0.0", 0, rpf_id=8192)
675 bier_de_1.add_vpp_config()
678 # A multicast route to forward post BIER disposition
680 route_eg_232_1_1_1 = VppIpMRoute(
684 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
685 paths=[VppMRoutePath(self.pg1.sw_if_index,
686 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
687 route_eg_232_1_1_1.add_vpp_config()
688 route_eg_232_1_1_1.update_rpf_id(8192)
691 # A packet with all bits set gets spat out to BP:1
693 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
694 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
695 UDP(sport=333, dport=8138) /
696 BIFT(sd=1, set=0, bsl=2, ttl=255) /
697 BIER(length=BIERLength.BIER_LEN_256,
698 BitString=chr(255)*32,
700 IP(src="1.1.1.1", dst="232.1.1.1") /
701 UDP(sport=1234, dport=1234) /
704 rx = self.send_and_expect(self.pg0, [p], self.pg1)
707 if __name__ == '__main__':
708 unittest.main(testRunner=VppTestRunner)