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)
338 def test_bier_e2e(self):
339 """ BIER end-to-end """
342 # Add a BIER table for sub-domain 0, set 0, and BSL 256
344 bti = VppBierTableID(0, 0, BIERLength.BIER_LEN_256)
345 bt = VppBierTable(self, bti, 77)
349 # Impostion Sets bit string 101010101....
352 bi = VppBierImp(self, bti, 333, chr(0x5) * 32)
356 # Add a multicast route that will forward into the BIER doamin
358 route_ing_232_1_1_1 = VppIpMRoute(
362 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
363 paths=[VppMRoutePath(self.pg0.sw_if_index,
364 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
365 VppMRoutePath(0xffffffff,
366 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
367 proto=DpoProto.DPO_PROTO_BIER,
368 bier_imp=bi.bi_index)])
369 route_ing_232_1_1_1.add_vpp_config()
372 # disposition table 8
374 bdt = VppBierDispTable(self, 8)
378 # BIER route in table that's for-us, resolving through
381 bier_route_1 = VppBierRoute(self, bti, 1,
382 [VppRoutePath("0.0.0.0",
385 bier_route_1.add_vpp_config()
388 # An entry in the disposition table for sender 333
391 bier_de_1 = VppBierDispEntry(self, bdt.id, 333,
392 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
393 "0.0.0.0", 10, rpf_id=8192)
394 bier_de_1.add_vpp_config()
397 # Add a multicast route that will forward the traffic
400 route_eg_232_1_1_1 = VppIpMRoute(
404 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
406 paths=[VppMRoutePath(self.pg1.sw_if_index,
407 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
408 route_eg_232_1_1_1.add_vpp_config()
409 route_eg_232_1_1_1.update_rpf_id(8192)
412 # inject a packet in VRF-0. We expect it to be BIER encapped,
413 # replicated, then hit the disposition and be forwarded
414 # out of VRF 10, i.e. on pg1
416 p = (Ether(dst=self.pg0.local_mac,
417 src=self.pg0.remote_mac) /
418 IP(src="1.1.1.1", dst="232.1.1.1") /
419 UDP(sport=1234, dport=1234))
421 rx = self.send_and_expect(self.pg0, p*65, self.pg1)
426 self.assertEqual(rx[0][IP].src, "1.1.1.1")
427 self.assertEqual(rx[0][IP].dst, "232.1.1.1")
429 def test_bier_head_o_udp(self):
430 """BIER head over UDP"""
433 # Add a BIER table for sub-domain 1, set 0, and BSL 256
435 bti = VppBierTableID(1, 0, BIERLength.BIER_LEN_256)
436 bt = VppBierTable(self, bti, 77)
440 # 1 bit positions via 1 next hops
443 ip_route = VppIpRoute(self, nh1, 32,
444 [VppRoutePath(self.pg1.remote_ip4,
445 self.pg1.sw_if_index,
447 ip_route.add_vpp_config()
449 udp_encap = VppUdpEncap(self, 4,
453 udp_encap.add_vpp_config()
455 bier_route = VppBierRoute(self, bti, 1,
456 [VppRoutePath("0.0.0.0",
460 bier_route.add_vpp_config()
463 # An imposition object with all bit-positions set
465 bi = VppBierImp(self, bti, 333, chr(0xff) * 32)
469 # Add a multicast route that will forward into the BIER doamin
471 route_ing_232_1_1_1 = VppIpMRoute(
475 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
476 paths=[VppMRoutePath(self.pg0.sw_if_index,
477 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
478 VppMRoutePath(0xffffffff,
479 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD,
480 proto=DpoProto.DPO_PROTO_BIER,
481 bier_imp=bi.bi_index)])
482 route_ing_232_1_1_1.add_vpp_config()
485 # inject a packet an IP. We expect it to be BIER and UDP encapped,
487 p = (Ether(dst=self.pg0.local_mac,
488 src=self.pg0.remote_mac) /
489 IP(src="1.1.1.1", dst="232.1.1.1") /
490 UDP(sport=1234, dport=1234))
492 self.pg0.add_stream([p])
493 self.pg_enable_capture(self.pg_interfaces)
496 rx = self.pg1.get_capture(1)
499 # Encap Stack is, eth, IP, UDP, BIFT, BIER
501 self.assertEqual(rx[0][IP].src, self.pg0.local_ip4)
502 self.assertEqual(rx[0][IP].dst, nh1)
503 self.assertEqual(rx[0][UDP].sport, 330)
504 self.assertEqual(rx[0][UDP].dport, 8138)
505 self.assertEqual(rx[0][BIFT].bsl, 2)
506 self.assertEqual(rx[0][BIFT].sd, 1)
507 self.assertEqual(rx[0][BIFT].set, 0)
508 self.assertEqual(rx[0][BIFT].ttl, 64)
509 self.assertEqual(rx[0][BIER].length, 2)
511 def test_bier_tail_o_udp(self):
512 """BIER Tail over UDP"""
515 # Add a BIER table for sub-domain 0, set 0, and BSL 256
517 bti = VppBierTableID(1, 0, BIERLength.BIER_LEN_256)
518 bt = VppBierTable(self, bti, MPLS_LABEL_INVALID)
524 bdt = VppBierDispTable(self, 8)
528 # BIER route in table that's for-us
530 bier_route_1 = VppBierRoute(self, bti, 1,
531 [VppRoutePath("0.0.0.0",
534 bier_route_1.add_vpp_config()
537 # An entry in the disposition table
539 bier_de_1 = VppBierDispEntry(self, bdt.id, 99,
540 BIER_HDR_PAYLOAD.BIER_HDR_PROTO_IPV4,
541 "0.0.0.0", 0, rpf_id=8192)
542 bier_de_1.add_vpp_config()
545 # A multicast route to forward post BIER disposition
547 route_eg_232_1_1_1 = VppIpMRoute(
551 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
552 paths=[VppMRoutePath(self.pg1.sw_if_index,
553 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
554 route_eg_232_1_1_1.add_vpp_config()
555 route_eg_232_1_1_1.update_rpf_id(8192)
558 # A packet with all bits set gets spat out to BP:1
560 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
561 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
562 UDP(sport=333, dport=8138) /
563 BIFT(sd=1, set=0, bsl=2, ttl=255) /
564 BIER(length=BIERLength.BIER_LEN_256, BFRID=99) /
565 IP(src="1.1.1.1", dst="232.1.1.1") /
566 UDP(sport=1234, dport=1234) /
569 rx = self.send_and_expect(self.pg0, [p], self.pg1)
572 if __name__ == '__main__':
573 unittest.main(testRunner=VppTestRunner)