6 from framework import VppTestCase, VppTestRunner
7 from vpp_ip_route import VppIpRoute, VppRoutePath, VppMplsRoute, \
8 VppMplsTable, VppIpMRoute, VppMRoutePath, VppIpTable, \
9 MRouteEntryFlags, MRouteItfFlags, MPLS_LABEL_INVALID, DpoProto
10 from vpp_bier import *
11 from vpp_udp_encap import *
13 from scapy.packet import Raw
14 from scapy.layers.l2 import Ether
15 from scapy.layers.inet import IP, UDP, ICMP
16 from scapy.layers.inet6 import IPv6
17 from scapy.contrib.mpls import MPLS
18 from scapy.contrib.bier import *
21 class TestBFIB(VppTestCase):
22 """ BIER FIB Test Case """
25 """ BFIB Unit Tests """
26 error = self.vapi.cli("test bier")
29 self.logger.critical(error)
30 self.assertEqual(error.find("Failed"), -1)
33 class TestBier(VppTestCase):
34 """ BIER Test Case """
37 super(TestBier, self).setUp()
39 # create 2 pg interfaces
40 self.create_pg_interfaces(range(3))
42 # create the default MPLS table
44 tbl = VppMplsTable(self, 0)
46 self.tables.append(tbl)
48 tbl = VppIpTable(self, 10)
50 self.tables.append(tbl)
52 # setup both interfaces
53 for i in self.pg_interfaces:
62 for i in self.pg_interfaces:
67 super(TestBier, self).tearDown()
69 def send_and_assert_no_replies(self, intf, pkts, remark):
71 self.pg_enable_capture(self.pg_interfaces)
73 for i in self.pg_interfaces:
74 i.assert_nothing_captured(remark=remark)
76 def send_and_expect(self, input, pkts, output):
77 self.vapi.cli("trace add bier-mpls-lookup 10")
78 input.add_stream(pkts)
79 self.pg_enable_capture(self.pg_interfaces)
81 rx = output.get_capture(len(pkts))
84 def test_bier_midpoint(self):
88 # Add a BIER table for sub-domain 0, set 0, and BSL 256
90 bti = VppBierTableID(0, 0, BIERLength.BIER_LEN_256)
91 bt = VppBierTable(self, bti, 77)
95 # A packet with no bits set gets dropped
97 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
98 MPLS(label=77, ttl=255) /
99 BIER(length=BIERLength.BIER_LEN_256,
100 BitString=chr(0)*64) /
101 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
102 UDP(sport=1234, dport=1234) /
106 self.send_and_assert_no_replies(self.pg0, pkts,
110 # Add a BIER route for each bit-position in the table via a different
111 # next-hop. Testing whether the BIER walk and replicate forwarding
112 # function works for all bit posisitons.
116 for i in range(1, 256):
117 nh = "10.0.%d.%d" % (i / 255, i % 255)
118 nh_routes.append(VppIpRoute(self, nh, 32,
119 [VppRoutePath(self.pg1.remote_ip4,
120 self.pg1.sw_if_index,
122 nh_routes[-1].add_vpp_config()
124 bier_routes.append(VppBierRoute(self, bti, i,
125 [VppRoutePath(nh, 0xffffffff,
127 bier_routes[-1].add_vpp_config()
130 # A packet with all bits set gets spat out to BP:1
132 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
133 MPLS(label=77, ttl=255) /
134 BIER(length=BIERLength.BIER_LEN_256) /
135 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
136 UDP(sport=1234, dport=1234) /
140 self.pg0.add_stream(pkts)
141 self.pg_enable_capture(self.pg_interfaces)
144 rx = self.pg1.get_capture(255)
148 # The packets are not required to be sent in bit-position order
149 # when we setup the routes above we used the bit-position to
150 # construct the out-label. so use that here to determine the BP
153 bp = olabel.label - 2000
155 blabel = olabel[MPLS].payload
156 self.assertEqual(blabel.label, 100+bp)
157 self.assertEqual(blabel.ttl, 254)
159 bier_hdr = blabel[MPLS].payload
161 self.assertEqual(bier_hdr.id, 5)
162 self.assertEqual(bier_hdr.version, 0)
163 self.assertEqual(bier_hdr.length, BIERLength.BIER_LEN_256)
164 self.assertEqual(bier_hdr.entropy, 0)
165 self.assertEqual(bier_hdr.OAM, 0)
166 self.assertEqual(bier_hdr.RSV, 0)
167 self.assertEqual(bier_hdr.DSCP, 0)
168 self.assertEqual(bier_hdr.Proto, 5)
170 # The bit-string should consist only of the BP given by i.
175 bitstring = chr(0) + bitstring
177 bitstring = chr(1 << bpi % 8) + bitstring
179 while len(bitstring) < 32:
180 bitstring = chr(0) + bitstring
182 self.assertEqual(len(bitstring), len(bier_hdr.BitString))
183 self.assertEqual(bitstring, bier_hdr.BitString)
185 def test_bier_head(self):
189 # Add a BIER table for sub-domain 0, set 0, and BSL 256
191 bti = VppBierTableID(0, 0, BIERLength.BIER_LEN_256)
192 bt = VppBierTable(self, bti, 77)
196 # 2 bit positions via two next hops
200 ip_route_1 = VppIpRoute(self, nh1, 32,
201 [VppRoutePath(self.pg1.remote_ip4,
202 self.pg1.sw_if_index,
204 ip_route_2 = VppIpRoute(self, nh2, 32,
205 [VppRoutePath(self.pg1.remote_ip4,
206 self.pg1.sw_if_index,
208 ip_route_1.add_vpp_config()
209 ip_route_2.add_vpp_config()
211 bier_route_1 = VppBierRoute(self, bti, 1,
212 [VppRoutePath(nh1, 0xffffffff,
214 bier_route_2 = VppBierRoute(self, bti, 2,
215 [VppRoutePath(nh2, 0xffffffff,
217 bier_route_1.add_vpp_config()
218 bier_route_2.add_vpp_config()
221 # An imposition object with both bit-positions set
223 bi = VppBierImp(self, bti, 333, chr(0x3) * 32)
227 # Add a multicast route that will forward into the BIER doamin
229 route_ing_232_1_1_1 = VppIpMRoute(
233 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
234 paths=[VppMRoutePath(self.pg0.sw_if_index,
235 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
236 VppMRoutePath(0xffffffff,
237 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
238 proto=DpoProto.DPO_PROTO_BIER,
239 bier_imp=bi.bi_index)])
240 route_ing_232_1_1_1.add_vpp_config()
243 # inject an IP packet. We expect it to be BIER encapped and
246 p = (Ether(dst=self.pg0.local_mac,
247 src=self.pg0.remote_mac) /
248 IP(src="1.1.1.1", dst="232.1.1.1") /
249 UDP(sport=1234, dport=1234))
251 self.pg0.add_stream([p])
252 self.pg_enable_capture(self.pg_interfaces)
255 rx = self.pg1.get_capture(2)
258 # Encap Stack is; eth, MPLS, MPLS, BIER
260 igp_mpls = rx[0][MPLS]
261 self.assertEqual(igp_mpls.label, 2001)
262 self.assertEqual(igp_mpls.ttl, 64)
263 self.assertEqual(igp_mpls.s, 0)
264 bier_mpls = igp_mpls[MPLS].payload
265 self.assertEqual(bier_mpls.label, 101)
266 self.assertEqual(bier_mpls.ttl, 64)
267 self.assertEqual(bier_mpls.s, 1)
268 self.assertEqual(rx[0][BIER].length, 2)
270 igp_mpls = rx[1][MPLS]
271 self.assertEqual(igp_mpls.label, 2002)
272 self.assertEqual(igp_mpls.ttl, 64)
273 self.assertEqual(igp_mpls.s, 0)
274 bier_mpls = igp_mpls[MPLS].payload
275 self.assertEqual(bier_mpls.label, 102)
276 self.assertEqual(bier_mpls.ttl, 64)
277 self.assertEqual(bier_mpls.s, 1)
278 self.assertEqual(rx[0][BIER].length, 2)
280 def test_bier_tail(self):
284 # Add a BIER table for sub-domain 0, set 0, and BSL 256
286 bti = VppBierTableID(0, 0, BIERLength.BIER_LEN_256)
287 bt = VppBierTable(self, bti, 77)
293 bdt = VppBierDispTable(self, 8)
297 # BIER route in table that's for-us
299 bier_route_1 = VppBierRoute(self, bti, 1,
300 [VppRoutePath("0.0.0.0",
303 bier_route_1.add_vpp_config()
306 # An entry in the disposition table
308 bier_de_1 = VppBierDispEntry(self, bdt.id, 99,
309 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
310 "0.0.0.0", 0, rpf_id=8192)
311 bier_de_1.add_vpp_config()
314 # A multicast route to forward post BIER disposition
316 route_eg_232_1_1_1 = VppIpMRoute(
320 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
321 paths=[VppMRoutePath(self.pg1.sw_if_index,
322 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
323 route_eg_232_1_1_1.add_vpp_config()
324 route_eg_232_1_1_1.update_rpf_id(8192)
327 # A packet with all bits set gets spat out to BP:1
329 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
330 MPLS(label=77, ttl=255) /
331 BIER(length=BIERLength.BIER_LEN_256, BFRID=99) /
332 IP(src="1.1.1.1", dst="232.1.1.1") /
333 UDP(sport=1234, dport=1234) /
336 self.send_and_expect(self.pg0, [p], self.pg1)
339 # A packet that does not match the Disposition entry gets dropped
341 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
342 MPLS(label=77, ttl=255) /
343 BIER(length=BIERLength.BIER_LEN_256, BFRID=77) /
344 IP(src="1.1.1.1", dst="232.1.1.1") /
345 UDP(sport=1234, dport=1234) /
347 self.send_and_assert_no_replies(self.pg0, p*2,
348 "no matching disposition entry")
351 # Add the default route to the disposition table
353 bier_de_2 = VppBierDispEntry(self, bdt.id, 0,
354 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
355 "0.0.0.0", 0, rpf_id=8192)
356 bier_de_2.add_vpp_config()
359 # now the previous packet is forwarded
361 self.send_and_expect(self.pg0, [p], self.pg1)
363 def test_bier_e2e(self):
364 """ BIER end-to-end """
367 # Add a BIER table for sub-domain 0, set 0, and BSL 256
369 bti = VppBierTableID(0, 0, BIERLength.BIER_LEN_256)
370 bt = VppBierTable(self, bti, 77)
374 # Impostion Sets bit string 101010101....
377 bi = VppBierImp(self, bti, 333, chr(0x5) * 32)
381 # Add a multicast route that will forward into the BIER doamin
383 route_ing_232_1_1_1 = VppIpMRoute(
387 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
388 paths=[VppMRoutePath(self.pg0.sw_if_index,
389 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
390 VppMRoutePath(0xffffffff,
391 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
392 proto=DpoProto.DPO_PROTO_BIER,
393 bier_imp=bi.bi_index)])
394 route_ing_232_1_1_1.add_vpp_config()
397 # disposition table 8
399 bdt = VppBierDispTable(self, 8)
403 # BIER route in table that's for-us, resolving through
406 bier_route_1 = VppBierRoute(self, bti, 1,
407 [VppRoutePath("0.0.0.0",
410 bier_route_1.add_vpp_config()
413 # An entry in the disposition table for sender 333
416 bier_de_1 = VppBierDispEntry(self, bdt.id, 333,
417 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
418 "0.0.0.0", 10, rpf_id=8192)
419 bier_de_1.add_vpp_config()
422 # Add a multicast route that will forward the traffic
425 route_eg_232_1_1_1 = VppIpMRoute(
429 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
431 paths=[VppMRoutePath(self.pg1.sw_if_index,
432 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
433 route_eg_232_1_1_1.add_vpp_config()
434 route_eg_232_1_1_1.update_rpf_id(8192)
437 # inject a packet in VRF-0. We expect it to be BIER encapped,
438 # replicated, then hit the disposition and be forwarded
439 # out of VRF 10, i.e. on pg1
441 p = (Ether(dst=self.pg0.local_mac,
442 src=self.pg0.remote_mac) /
443 IP(src="1.1.1.1", dst="232.1.1.1") /
444 UDP(sport=1234, dport=1234))
446 rx = self.send_and_expect(self.pg0, p*65, self.pg1)
451 self.assertEqual(rx[0][IP].src, "1.1.1.1")
452 self.assertEqual(rx[0][IP].dst, "232.1.1.1")
454 def test_bier_head_o_udp(self):
455 """BIER head over UDP"""
458 # Add a BIER table for sub-domain 1, set 0, and BSL 256
460 bti = VppBierTableID(1, 0, BIERLength.BIER_LEN_256)
461 bt = VppBierTable(self, bti, 77)
465 # 1 bit positions via 1 next hops
468 ip_route = VppIpRoute(self, nh1, 32,
469 [VppRoutePath(self.pg1.remote_ip4,
470 self.pg1.sw_if_index,
472 ip_route.add_vpp_config()
474 udp_encap = VppUdpEncap(self, 4,
478 udp_encap.add_vpp_config()
480 bier_route = VppBierRoute(self, bti, 1,
481 [VppRoutePath("0.0.0.0",
485 bier_route.add_vpp_config()
488 # An imposition object with all bit-positions set
490 bi = VppBierImp(self, bti, 333, chr(0xff) * 32)
494 # Add a multicast route that will forward into the BIER doamin
496 route_ing_232_1_1_1 = VppIpMRoute(
500 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
501 paths=[VppMRoutePath(self.pg0.sw_if_index,
502 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
503 VppMRoutePath(0xffffffff,
504 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
505 proto=DpoProto.DPO_PROTO_BIER,
506 bier_imp=bi.bi_index)])
507 route_ing_232_1_1_1.add_vpp_config()
510 # inject a packet an IP. We expect it to be BIER and UDP encapped,
512 p = (Ether(dst=self.pg0.local_mac,
513 src=self.pg0.remote_mac) /
514 IP(src="1.1.1.1", dst="232.1.1.1") /
515 UDP(sport=1234, dport=1234))
517 self.pg0.add_stream([p])
518 self.pg_enable_capture(self.pg_interfaces)
521 rx = self.pg1.get_capture(1)
524 # Encap Stack is, eth, IP, UDP, BIFT, BIER
526 self.assertEqual(rx[0][IP].src, self.pg0.local_ip4)
527 self.assertEqual(rx[0][IP].dst, nh1)
528 self.assertEqual(rx[0][UDP].sport, 330)
529 self.assertEqual(rx[0][UDP].dport, 8138)
530 self.assertEqual(rx[0][BIFT].bsl, 2)
531 self.assertEqual(rx[0][BIFT].sd, 1)
532 self.assertEqual(rx[0][BIFT].set, 0)
533 self.assertEqual(rx[0][BIFT].ttl, 64)
534 self.assertEqual(rx[0][BIER].length, 2)
536 def test_bier_tail_o_udp(self):
537 """BIER Tail over UDP"""
540 # Add a BIER table for sub-domain 0, set 0, and BSL 256
542 bti = VppBierTableID(1, 0, BIERLength.BIER_LEN_256)
543 bt = VppBierTable(self, bti, MPLS_LABEL_INVALID)
549 bdt = VppBierDispTable(self, 8)
553 # BIER route in table that's for-us
555 bier_route_1 = VppBierRoute(self, bti, 1,
556 [VppRoutePath("0.0.0.0",
559 bier_route_1.add_vpp_config()
562 # An entry in the disposition table
564 bier_de_1 = VppBierDispEntry(self, bdt.id, 99,
565 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
566 "0.0.0.0", 0, rpf_id=8192)
567 bier_de_1.add_vpp_config()
570 # A multicast route to forward post BIER disposition
572 route_eg_232_1_1_1 = VppIpMRoute(
576 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
577 paths=[VppMRoutePath(self.pg1.sw_if_index,
578 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
579 route_eg_232_1_1_1.add_vpp_config()
580 route_eg_232_1_1_1.update_rpf_id(8192)
583 # A packet with all bits set gets spat out to BP:1
585 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
586 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
587 UDP(sport=333, dport=8138) /
588 BIFT(sd=1, set=0, bsl=2, ttl=255) /
589 BIER(length=BIERLength.BIER_LEN_256, BFRID=99) /
590 IP(src="1.1.1.1", dst="232.1.1.1") /
591 UDP(sport=1234, dport=1234) /
594 rx = self.send_and_expect(self.pg0, [p], self.pg1)
597 if __name__ == '__main__':
598 unittest.main(testRunner=VppTestRunner)