5 from framework import VppTestCase
6 from asfframework import VppTestRunner
7 from vpp_sub_interface import VppDot1QSubint
8 from vpp_ip_route import (
17 from scapy.packet import Raw
18 from scapy.layers.l2 import Ether, Dot1Q
19 from scapy.layers.inet import IP, UDP
20 from scapy.layers.inet6 import IPv6
21 from scapy.contrib.mpls import MPLS
22 from vpp_papi import VppEnum
23 from vpp_qos import VppQosRecord, VppQosEgressMap, VppQosMark, VppQosStore
28 class TestQOS(VppTestCase):
31 # Note: Since the enums aren't created dynamically until after
32 # the papi client attaches to VPP, we put it in a property to
33 # ensure it is the value at runtime, not at module load time.
36 return VppEnum.vl_api_qos_source_t
40 super(TestQOS, cls).setUpClass()
43 def tearDownClass(cls):
44 super(TestQOS, cls).tearDownClass()
47 super(TestQOS, self).setUp()
49 self.create_pg_interfaces(range(5))
51 tbl = VppMplsTable(self, 0)
54 for i in self.pg_interfaces:
63 for i in self.pg_interfaces:
68 super(TestQOS, self).tearDown()
70 def test_qos_ip(self):
71 """QoS Mark/Record/Store IP"""
74 # for table 1 map the n=0xff possible values of input QoS mark,
77 output = [scapy.compat.chb(0)] * 256
78 for i in range(0, 255):
79 output[i] = scapy.compat.chb(255 - i)
81 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
83 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
86 # For table 2 (and up) use the value n for everything
88 output = [scapy.compat.chb(2)] * 256
90 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
92 qem2 = VppQosEgressMap(self, 2, rows).add_vpp_config()
94 output = [scapy.compat.chb(3)] * 256
96 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
98 qem3 = VppQosEgressMap(self, 3, rows).add_vpp_config()
100 output = [scapy.compat.chb(4)] * 256
101 os = b"".join(output)
102 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
104 qem4 = VppQosEgressMap(self, 4, rows).add_vpp_config()
105 qem5 = VppQosEgressMap(self, 5, rows).add_vpp_config()
106 qem6 = VppQosEgressMap(self, 6, rows).add_vpp_config()
107 qem7 = VppQosEgressMap(self, 7, rows).add_vpp_config()
109 self.assertTrue(qem7.query_vpp_config())
110 self.logger.info(self.vapi.cli("sh qos eg map"))
113 # Bind interface pgN to table n
116 self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_IP
119 self, self.pg2, qem2, self.QOS_SOURCE.QOS_API_SOURCE_IP
122 self, self.pg3, qem3, self.QOS_SOURCE.QOS_API_SOURCE_IP
125 self, self.pg4, qem4, self.QOS_SOURCE.QOS_API_SOURCE_IP
127 self.assertTrue(qm3.query_vpp_config())
129 self.logger.info(self.vapi.cli("sh qos mark"))
132 # packets ingress on Pg0
135 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
136 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, tos=1)
137 / UDP(sport=1234, dport=1234)
138 / Raw(scapy.compat.chb(100) * NUM_PKTS)
141 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
142 / IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6, tc=1)
143 / UDP(sport=1234, dport=1234)
144 / Raw(scapy.compat.chb(100) * NUM_PKTS)
148 # Since we have not yet enabled the recording of the input QoS
149 # from the input iP header, the egress packet's ToS will be unchanged
151 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
153 self.assertEqual(p[IP].tos, 1)
154 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1)
156 self.assertEqual(p[IPv6].tc, 1)
159 # Enable QoS recording on IP input for pg0
161 qr1 = VppQosRecord(self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_IP)
163 self.logger.info(self.vapi.cli("sh qos record"))
166 # send the same packets, this time expect the input TOS of 1
167 # to be mapped to pg1's egress value of 254
169 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
171 self.assertEqual(p[IP].tos, 254)
172 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1)
174 self.assertEqual(p[IPv6].tc, 254)
177 # different input ToS to test the mapping
180 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
182 self.assertEqual(p[IP].tos, 128)
184 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1)
186 self.assertEqual(p[IPv6].tc, 128)
189 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
191 self.assertEqual(p[IP].tos, 1)
193 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1)
195 self.assertEqual(p[IPv6].tc, 1)
198 # send packets out the other interfaces to test the maps are
201 p_v4[IP].dst = self.pg2.remote_ip4
202 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg2)
204 self.assertEqual(p[IP].tos, 2)
206 p_v4[IP].dst = self.pg3.remote_ip4
207 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg3)
209 self.assertEqual(p[IP].tos, 3)
211 p_v6[IPv6].dst = self.pg3.remote_ip6
212 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg3)
214 self.assertEqual(p[IPv6].tc, 3)
217 # remove the map on pg2 and pg3, now expect an unchanged IP tos
219 qm2.remove_vpp_config()
220 qm3.remove_vpp_config()
221 self.logger.info(self.vapi.cli("sh qos mark"))
223 self.assertFalse(qm3.query_vpp_config())
224 self.logger.info(self.vapi.cli("sh int feat pg2"))
226 p_v4[IP].dst = self.pg2.remote_ip4
227 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg2)
229 self.assertEqual(p[IP].tos, 254)
231 p_v4[IP].dst = self.pg3.remote_ip4
232 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg3)
234 self.assertEqual(p[IP].tos, 254)
237 # still mapping out of pg1
239 p_v4[IP].dst = self.pg1.remote_ip4
240 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
242 self.assertEqual(p[IP].tos, 1)
245 # disable the input recording on pg0
247 self.assertTrue(qr1.query_vpp_config())
248 qr1.remove_vpp_config()
251 # back to an unchanged TOS value
253 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
255 self.assertEqual(p[IP].tos, 254)
258 # enable QoS stroe instead of record
261 self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_IP, 5
263 self.logger.info(self.vapi.cli("sh qos store"))
265 p_v4[IP].dst = self.pg1.remote_ip4
266 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
268 self.assertEqual(p[IP].tos, 250)
271 # disable the input storing on pg0
273 self.assertTrue(qst1.query_vpp_config())
274 qst1.remove_vpp_config()
277 # back to an unchanged TOS value
279 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
281 self.assertEqual(p[IP].tos, 254)
284 # disable the egress map on pg1 and pg4
286 qm1.remove_vpp_config()
287 qm4.remove_vpp_config()
290 # unchanged Tos on pg1
292 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
294 self.assertEqual(p[IP].tos, 254)
296 def test_qos_mpls(self):
297 """QoS Mark/Record MPLS"""
300 # 255 QoS for all input values
306 output = [scapy.compat.chb(from_ext)] * 256
307 os1 = b"".join(output)
308 output = [scapy.compat.chb(from_vlan)] * 256
309 os2 = b"".join(output)
310 output = [scapy.compat.chb(from_mpls)] * 256
311 os3 = b"".join(output)
312 output = [scapy.compat.chb(from_ip)] * 256
313 os4 = b"".join(output)
314 rows = [{"outputs": os1}, {"outputs": os2}, {"outputs": os3}, {"outputs": os4}]
316 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
319 # a route with 1 MPLS label
321 route_10_0_0_1 = VppIpRoute(
325 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index, labels=[32])],
327 route_10_0_0_1.add_vpp_config()
330 # a route with 3 MPLS labels
332 route_10_0_0_3 = VppIpRoute(
338 self.pg1.remote_ip4, self.pg1.sw_if_index, labels=[63, 33, 34]
342 route_10_0_0_3.add_vpp_config()
345 # enable IP QoS recording on the input Pg0 and MPLS egress marking
349 self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_IP
352 self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_MPLS
356 # packet that will get one label added and 3 labels added resp.
359 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
360 / IP(src=self.pg0.remote_ip4, dst="10.0.0.1", tos=1)
361 / UDP(sport=1234, dport=1234)
362 / Raw(scapy.compat.chb(100) * NUM_PKTS)
365 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
366 / IP(src=self.pg0.remote_ip4, dst="10.0.0.3", tos=1)
367 / UDP(sport=1234, dport=1234)
368 / Raw(scapy.compat.chb(100) * NUM_PKTS)
371 rx = self.send_and_expect(self.pg0, p_1 * NUM_PKTS, self.pg1)
374 # only 3 bits of ToS value in MPLS make sure tos is correct
375 # and the label and EOS bit have not been corrupted
378 self.assertEqual(p[MPLS].cos, from_ip)
379 self.assertEqual(p[MPLS].label, 32)
380 self.assertEqual(p[MPLS].s, 1)
381 rx = self.send_and_expect(self.pg0, p_3 * NUM_PKTS, self.pg1)
383 self.assertEqual(p[MPLS].cos, from_ip)
384 self.assertEqual(p[MPLS].label, 63)
385 self.assertEqual(p[MPLS].s, 0)
387 self.assertEqual(h[MPLS].cos, from_ip)
388 self.assertEqual(h[MPLS].label, 33)
389 self.assertEqual(h[MPLS].s, 0)
391 self.assertEqual(h[MPLS].cos, from_ip)
392 self.assertEqual(h[MPLS].label, 34)
393 self.assertEqual(h[MPLS].s, 1)
396 # enable MPLS QoS recording on the input Pg0 and IP egress marking
400 self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_MPLS
403 self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_IP
407 # MPLS x-connect - COS according to pg1 map
409 route_32_eos = VppMplsRoute(
415 self.pg1.remote_ip4, self.pg1.sw_if_index, labels=[VppMplsLabel(33)]
419 route_32_eos.add_vpp_config()
422 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
423 / MPLS(label=32, cos=3, ttl=2)
424 / IP(src=self.pg0.remote_ip4, dst="10.0.0.1", tos=1)
425 / UDP(sport=1234, dport=1234)
426 / Raw(scapy.compat.chb(100) * NUM_PKTS)
429 rx = self.send_and_expect(self.pg0, p_m1 * NUM_PKTS, self.pg1)
431 self.assertEqual(p[MPLS].cos, from_mpls)
432 self.assertEqual(p[MPLS].label, 33)
433 self.assertEqual(p[MPLS].s, 1)
436 # MPLS deag - COS is copied from MPLS to IP
438 route_33_eos = VppMplsRoute(
439 self, 33, 1, [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=0)]
441 route_33_eos.add_vpp_config()
443 route_10_0_0_4 = VppIpRoute(
447 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
449 route_10_0_0_4.add_vpp_config()
452 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
453 / MPLS(label=33, ttl=2, cos=3)
454 / IP(src=self.pg0.remote_ip4, dst="10.0.0.4", tos=1)
455 / UDP(sport=1234, dport=1234)
456 / Raw(scapy.compat.chb(100) * NUM_PKTS)
459 rx = self.send_and_expect(self.pg0, p_m2 * NUM_PKTS, self.pg1)
462 self.assertEqual(p[IP].tos, from_mpls)
464 def test_qos_vlan(self):
465 """QoS mark/record VLAN"""
468 # QoS for all input values
470 output = [scapy.compat.chb(0)] * 256
471 for i in range(0, 255):
472 output[i] = scapy.compat.chb(255 - i)
473 os = b"".join(output)
474 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
476 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
478 sub_if = VppDot1QSubint(self, self.pg0, 11)
487 # enable VLAN QoS recording/marking on the input Pg0 subinterface and
490 self, sub_if, self.QOS_SOURCE.QOS_API_SOURCE_VLAN
493 self, sub_if, qem1, self.QOS_SOURCE.QOS_API_SOURCE_VLAN
497 # IP marking/recording on pg1
499 qr_ip = VppQosRecord(
500 self, self.pg1, self.QOS_SOURCE.QOS_API_SOURCE_IP
503 self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_IP
507 # a routes to/from sub-interface
509 route_10_0_0_1 = VppIpRoute(
510 self, "10.0.0.1", 32, [VppRoutePath(sub_if.remote_ip4, sub_if.sw_if_index)]
512 route_10_0_0_1.add_vpp_config()
513 route_10_0_0_2 = VppIpRoute(
517 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
519 route_10_0_0_2.add_vpp_config()
520 route_2001_1 = VppIpRoute(
521 self, "2001::1", 128, [VppRoutePath(sub_if.remote_ip6, sub_if.sw_if_index)]
523 route_2001_1.add_vpp_config()
524 route_2001_2 = VppIpRoute(
528 [VppRoutePath(self.pg1.remote_ip6, self.pg1.sw_if_index)],
530 route_2001_2.add_vpp_config()
533 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
534 / Dot1Q(vlan=11, prio=1)
535 / IP(src="1.1.1.1", dst="10.0.0.2", tos=1)
536 / UDP(sport=1234, dport=1234)
537 / Raw(scapy.compat.chb(100) * NUM_PKTS)
541 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
542 / IP(src="1.1.1.1", dst="10.0.0.1", tos=1)
543 / UDP(sport=1234, dport=1234)
544 / Raw(scapy.compat.chb(100) * NUM_PKTS)
548 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
549 / Dot1Q(vlan=11, prio=1, id=1)
550 / IP(src="1.1.1.1", dst="10.0.0.2", tos=2)
551 / UDP(sport=1234, dport=1234)
552 / Raw(scapy.compat.chb(100) * NUM_PKTS)
555 rx = self.send_and_expect(self.pg1, p_v2 * NUM_PKTS, self.pg0)
558 self.assertEqual(p[Dot1Q].prio, 7)
559 self.assertEqual(p[Dot1Q].id, 0)
561 rx = self.send_and_expect(self.pg0, p_v3 * NUM_PKTS, self.pg1)
564 self.assertEqual(p[IP].tos, 252)
566 rx = self.send_and_expect(self.pg0, p_v1 * NUM_PKTS, self.pg1)
569 self.assertEqual(p[IP].tos, 253)
572 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
573 / Dot1Q(vlan=11, prio=2)
574 / IPv6(src="2001::1", dst="2001::2", tc=1)
575 / UDP(sport=1234, dport=1234)
576 / Raw(scapy.compat.chb(100) * NUM_PKTS)
580 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
581 / IPv6(src="3001::1", dst="2001::1", tc=1)
582 / UDP(sport=1234, dport=1234)
583 / Raw(scapy.compat.chb(100) * NUM_PKTS)
586 rx = self.send_and_expect(self.pg1, p_v2 * NUM_PKTS, self.pg0)
589 self.assertEqual(p[Dot1Q].prio, 7)
590 self.assertEqual(p[Dot1Q].id, 0)
592 rx = self.send_and_expect(self.pg0, p_v1 * NUM_PKTS, self.pg1)
595 self.assertEqual(p[IPv6].tc, 251)
600 sub_if.unconfig_ip4()
601 sub_if.unconfig_ip6()
604 if __name__ == "__main__":
605 unittest.main(testRunner=VppTestRunner)