5 from framework import VppTestCase, VppTestRunner
6 from vpp_sub_interface import VppDot1QSubint
7 from vpp_ip import DpoProto
8 from vpp_ip_route import (
18 from scapy.packet import Raw
19 from scapy.layers.l2 import Ether, Dot1Q
20 from scapy.layers.inet import IP, UDP
21 from scapy.layers.inet6 import IPv6
22 from scapy.contrib.mpls import MPLS
23 from vpp_papi import VppEnum
24 from vpp_qos import VppQosRecord, VppQosEgressMap, VppQosMark, VppQosStore
29 class TestQOS(VppTestCase):
32 # Note: Since the enums aren't created dynamically until after
33 # the papi client attaches to VPP, we put it in a property to
34 # ensure it is the value at runtime, not at module load time.
37 return VppEnum.vl_api_qos_source_t
41 super(TestQOS, cls).setUpClass()
44 def tearDownClass(cls):
45 super(TestQOS, cls).tearDownClass()
48 super(TestQOS, self).setUp()
50 self.create_pg_interfaces(range(5))
52 tbl = VppMplsTable(self, 0)
55 for i in self.pg_interfaces:
64 for i in self.pg_interfaces:
69 super(TestQOS, self).tearDown()
71 def test_qos_ip(self):
72 """QoS Mark/Record/Store IP"""
75 # for table 1 map the n=0xff possible values of input QoS mark,
78 output = [scapy.compat.chb(0)] * 256
79 for i in range(0, 255):
80 output[i] = scapy.compat.chb(255 - i)
82 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
84 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
87 # For table 2 (and up) use the value n for everything
89 output = [scapy.compat.chb(2)] * 256
91 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
93 qem2 = VppQosEgressMap(self, 2, rows).add_vpp_config()
95 output = [scapy.compat.chb(3)] * 256
97 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
99 qem3 = VppQosEgressMap(self, 3, rows).add_vpp_config()
101 output = [scapy.compat.chb(4)] * 256
102 os = b"".join(output)
103 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
105 qem4 = VppQosEgressMap(self, 4, rows).add_vpp_config()
106 qem5 = VppQosEgressMap(self, 5, rows).add_vpp_config()
107 qem6 = VppQosEgressMap(self, 6, rows).add_vpp_config()
108 qem7 = VppQosEgressMap(self, 7, rows).add_vpp_config()
110 self.assertTrue(qem7.query_vpp_config())
111 self.logger.info(self.vapi.cli("sh qos eg map"))
114 # Bind interface pgN to table n
117 self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_IP
120 self, self.pg2, qem2, self.QOS_SOURCE.QOS_API_SOURCE_IP
123 self, self.pg3, qem3, self.QOS_SOURCE.QOS_API_SOURCE_IP
126 self, self.pg4, qem4, self.QOS_SOURCE.QOS_API_SOURCE_IP
128 self.assertTrue(qm3.query_vpp_config())
130 self.logger.info(self.vapi.cli("sh qos mark"))
133 # packets ingress on Pg0
136 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
137 / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, tos=1)
138 / UDP(sport=1234, dport=1234)
139 / Raw(scapy.compat.chb(100) * NUM_PKTS)
142 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
143 / IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6, tc=1)
144 / UDP(sport=1234, dport=1234)
145 / Raw(scapy.compat.chb(100) * NUM_PKTS)
149 # Since we have not yet enabled the recording of the input QoS
150 # from the input iP header, the egress packet's ToS will be unchanged
152 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
154 self.assertEqual(p[IP].tos, 1)
155 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1)
157 self.assertEqual(p[IPv6].tc, 1)
160 # Enable QoS recording on IP input for pg0
162 qr1 = VppQosRecord(self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_IP)
164 self.logger.info(self.vapi.cli("sh qos record"))
167 # send the same packets, this time expect the input TOS of 1
168 # to be mapped to pg1's egress value of 254
170 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
172 self.assertEqual(p[IP].tos, 254)
173 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1)
175 self.assertEqual(p[IPv6].tc, 254)
178 # different input ToS to test the mapping
181 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
183 self.assertEqual(p[IP].tos, 128)
185 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1)
187 self.assertEqual(p[IPv6].tc, 128)
190 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
192 self.assertEqual(p[IP].tos, 1)
194 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg1)
196 self.assertEqual(p[IPv6].tc, 1)
199 # send packets out the other interfaces to test the maps are
202 p_v4[IP].dst = self.pg2.remote_ip4
203 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg2)
205 self.assertEqual(p[IP].tos, 2)
207 p_v4[IP].dst = self.pg3.remote_ip4
208 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg3)
210 self.assertEqual(p[IP].tos, 3)
212 p_v6[IPv6].dst = self.pg3.remote_ip6
213 rx = self.send_and_expect(self.pg0, p_v6 * NUM_PKTS, self.pg3)
215 self.assertEqual(p[IPv6].tc, 3)
218 # remove the map on pg2 and pg3, now expect an unchanged IP tos
220 qm2.remove_vpp_config()
221 qm3.remove_vpp_config()
222 self.logger.info(self.vapi.cli("sh qos mark"))
224 self.assertFalse(qm3.query_vpp_config())
225 self.logger.info(self.vapi.cli("sh int feat pg2"))
227 p_v4[IP].dst = self.pg2.remote_ip4
228 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg2)
230 self.assertEqual(p[IP].tos, 254)
232 p_v4[IP].dst = self.pg3.remote_ip4
233 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg3)
235 self.assertEqual(p[IP].tos, 254)
238 # still mapping out of pg1
240 p_v4[IP].dst = self.pg1.remote_ip4
241 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
243 self.assertEqual(p[IP].tos, 1)
246 # disable the input recording on pg0
248 self.assertTrue(qr1.query_vpp_config())
249 qr1.remove_vpp_config()
252 # back to an unchanged TOS value
254 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
256 self.assertEqual(p[IP].tos, 254)
259 # enable QoS stroe instead of record
262 self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_IP, 5
264 self.logger.info(self.vapi.cli("sh qos store"))
266 p_v4[IP].dst = self.pg1.remote_ip4
267 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
269 self.assertEqual(p[IP].tos, 250)
272 # disable the input storing on pg0
274 self.assertTrue(qst1.query_vpp_config())
275 qst1.remove_vpp_config()
278 # back to an unchanged TOS value
280 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
282 self.assertEqual(p[IP].tos, 254)
285 # disable the egress map on pg1 and pg4
287 qm1.remove_vpp_config()
288 qm4.remove_vpp_config()
291 # unchanged Tos on pg1
293 rx = self.send_and_expect(self.pg0, p_v4 * NUM_PKTS, self.pg1)
295 self.assertEqual(p[IP].tos, 254)
297 def test_qos_mpls(self):
298 """QoS Mark/Record MPLS"""
301 # 255 QoS for all input values
307 output = [scapy.compat.chb(from_ext)] * 256
308 os1 = b"".join(output)
309 output = [scapy.compat.chb(from_vlan)] * 256
310 os2 = b"".join(output)
311 output = [scapy.compat.chb(from_mpls)] * 256
312 os3 = b"".join(output)
313 output = [scapy.compat.chb(from_ip)] * 256
314 os4 = b"".join(output)
315 rows = [{"outputs": os1}, {"outputs": os2}, {"outputs": os3}, {"outputs": os4}]
317 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
320 # a route with 1 MPLS label
322 route_10_0_0_1 = VppIpRoute(
326 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index, labels=[32])],
328 route_10_0_0_1.add_vpp_config()
331 # a route with 3 MPLS labels
333 route_10_0_0_3 = VppIpRoute(
339 self.pg1.remote_ip4, self.pg1.sw_if_index, labels=[63, 33, 34]
343 route_10_0_0_3.add_vpp_config()
346 # enable IP QoS recording on the input Pg0 and MPLS egress marking
350 self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_IP
353 self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_MPLS
357 # packet that will get one label added and 3 labels added resp.
360 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
361 / IP(src=self.pg0.remote_ip4, dst="10.0.0.1", tos=1)
362 / UDP(sport=1234, dport=1234)
363 / Raw(scapy.compat.chb(100) * NUM_PKTS)
366 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
367 / IP(src=self.pg0.remote_ip4, dst="10.0.0.3", tos=1)
368 / UDP(sport=1234, dport=1234)
369 / Raw(scapy.compat.chb(100) * NUM_PKTS)
372 rx = self.send_and_expect(self.pg0, p_1 * NUM_PKTS, self.pg1)
375 # only 3 bits of ToS value in MPLS make sure tos is correct
376 # and the label and EOS bit have not been corrupted
379 self.assertEqual(p[MPLS].cos, from_ip)
380 self.assertEqual(p[MPLS].label, 32)
381 self.assertEqual(p[MPLS].s, 1)
382 rx = self.send_and_expect(self.pg0, p_3 * NUM_PKTS, self.pg1)
384 self.assertEqual(p[MPLS].cos, from_ip)
385 self.assertEqual(p[MPLS].label, 63)
386 self.assertEqual(p[MPLS].s, 0)
388 self.assertEqual(h[MPLS].cos, from_ip)
389 self.assertEqual(h[MPLS].label, 33)
390 self.assertEqual(h[MPLS].s, 0)
392 self.assertEqual(h[MPLS].cos, from_ip)
393 self.assertEqual(h[MPLS].label, 34)
394 self.assertEqual(h[MPLS].s, 1)
397 # enable MPLS QoS recording on the input Pg0 and IP egress marking
401 self, self.pg0, self.QOS_SOURCE.QOS_API_SOURCE_MPLS
404 self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_IP
408 # MPLS x-connect - COS according to pg1 map
410 route_32_eos = VppMplsRoute(
416 self.pg1.remote_ip4, self.pg1.sw_if_index, labels=[VppMplsLabel(33)]
420 route_32_eos.add_vpp_config()
423 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
424 / MPLS(label=32, cos=3, ttl=2)
425 / IP(src=self.pg0.remote_ip4, dst="10.0.0.1", tos=1)
426 / UDP(sport=1234, dport=1234)
427 / Raw(scapy.compat.chb(100) * NUM_PKTS)
430 rx = self.send_and_expect(self.pg0, p_m1 * NUM_PKTS, self.pg1)
432 self.assertEqual(p[MPLS].cos, from_mpls)
433 self.assertEqual(p[MPLS].label, 33)
434 self.assertEqual(p[MPLS].s, 1)
437 # MPLS deag - COS is copied from MPLS to IP
439 route_33_eos = VppMplsRoute(
440 self, 33, 1, [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=0)]
442 route_33_eos.add_vpp_config()
444 route_10_0_0_4 = VppIpRoute(
448 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
450 route_10_0_0_4.add_vpp_config()
453 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
454 / MPLS(label=33, ttl=2, cos=3)
455 / IP(src=self.pg0.remote_ip4, dst="10.0.0.4", tos=1)
456 / UDP(sport=1234, dport=1234)
457 / Raw(scapy.compat.chb(100) * NUM_PKTS)
460 rx = self.send_and_expect(self.pg0, p_m2 * NUM_PKTS, self.pg1)
463 self.assertEqual(p[IP].tos, from_mpls)
465 def test_qos_vlan(self):
466 """QoS mark/record VLAN"""
469 # QoS for all input values
471 output = [scapy.compat.chb(0)] * 256
472 for i in range(0, 255):
473 output[i] = scapy.compat.chb(255 - i)
474 os = b"".join(output)
475 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
477 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
479 sub_if = VppDot1QSubint(self, self.pg0, 11)
488 # enable VLAN QoS recording/marking on the input Pg0 subinterface and
491 self, sub_if, self.QOS_SOURCE.QOS_API_SOURCE_VLAN
494 self, sub_if, qem1, self.QOS_SOURCE.QOS_API_SOURCE_VLAN
498 # IP marking/recording on pg1
500 qr_ip = VppQosRecord(
501 self, self.pg1, self.QOS_SOURCE.QOS_API_SOURCE_IP
504 self, self.pg1, qem1, self.QOS_SOURCE.QOS_API_SOURCE_IP
508 # a routes to/from sub-interface
510 route_10_0_0_1 = VppIpRoute(
511 self, "10.0.0.1", 32, [VppRoutePath(sub_if.remote_ip4, sub_if.sw_if_index)]
513 route_10_0_0_1.add_vpp_config()
514 route_10_0_0_2 = VppIpRoute(
518 [VppRoutePath(self.pg1.remote_ip4, self.pg1.sw_if_index)],
520 route_10_0_0_2.add_vpp_config()
521 route_2001_1 = VppIpRoute(
522 self, "2001::1", 128, [VppRoutePath(sub_if.remote_ip6, sub_if.sw_if_index)]
524 route_2001_1.add_vpp_config()
525 route_2001_2 = VppIpRoute(
529 [VppRoutePath(self.pg1.remote_ip6, self.pg1.sw_if_index)],
531 route_2001_2.add_vpp_config()
534 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
535 / Dot1Q(vlan=11, prio=1)
536 / IP(src="1.1.1.1", dst="10.0.0.2", tos=1)
537 / UDP(sport=1234, dport=1234)
538 / Raw(scapy.compat.chb(100) * NUM_PKTS)
542 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
543 / IP(src="1.1.1.1", dst="10.0.0.1", tos=1)
544 / UDP(sport=1234, dport=1234)
545 / Raw(scapy.compat.chb(100) * NUM_PKTS)
549 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
550 / Dot1Q(vlan=11, prio=1, id=1)
551 / IP(src="1.1.1.1", dst="10.0.0.2", tos=2)
552 / UDP(sport=1234, dport=1234)
553 / Raw(scapy.compat.chb(100) * NUM_PKTS)
556 rx = self.send_and_expect(self.pg1, p_v2 * NUM_PKTS, self.pg0)
559 self.assertEqual(p[Dot1Q].prio, 7)
560 self.assertEqual(p[Dot1Q].id, 0)
562 rx = self.send_and_expect(self.pg0, p_v3 * NUM_PKTS, self.pg1)
565 self.assertEqual(p[IP].tos, 252)
567 rx = self.send_and_expect(self.pg0, p_v1 * NUM_PKTS, self.pg1)
570 self.assertEqual(p[IP].tos, 253)
573 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
574 / Dot1Q(vlan=11, prio=2)
575 / IPv6(src="2001::1", dst="2001::2", tc=1)
576 / UDP(sport=1234, dport=1234)
577 / Raw(scapy.compat.chb(100) * NUM_PKTS)
581 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
582 / IPv6(src="3001::1", dst="2001::1", tc=1)
583 / UDP(sport=1234, dport=1234)
584 / Raw(scapy.compat.chb(100) * NUM_PKTS)
587 rx = self.send_and_expect(self.pg1, p_v2 * NUM_PKTS, self.pg0)
590 self.assertEqual(p[Dot1Q].prio, 7)
591 self.assertEqual(p[Dot1Q].id, 0)
593 rx = self.send_and_expect(self.pg0, p_v1 * NUM_PKTS, self.pg1)
596 self.assertEqual(p[IPv6].tc, 251)
601 sub_if.unconfig_ip4()
602 sub_if.unconfig_ip6()
605 if __name__ == "__main__":
606 unittest.main(testRunner=VppTestRunner)