2021-02-17 15:54:52 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2021 Graphiant, Inc.
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
from scapy.layers.inet import IP, UDP
|
|
|
|
from scapy.layers.l2 import Ether
|
|
|
|
from scapy.packet import Raw
|
2023-08-31 00:47:44 -04:00
|
|
|
from framework import VppTestCase
|
|
|
|
from asfframework import VppTestRunner
|
2021-02-17 15:54:52 +00:00
|
|
|
from vpp_papi import VppEnum
|
2022-04-05 19:23:12 +02:00
|
|
|
from vpp_policer import VppPolicer, PolicerAction, Dir
|
2021-02-17 15:54:52 +00:00
|
|
|
|
|
|
|
NUM_PKTS = 67
|
|
|
|
|
|
|
|
|
|
|
|
class TestPolicerInput(VppTestCase):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Policer on an interface"""
|
|
|
|
|
2021-03-15 16:58:10 +01:00
|
|
|
vpp_worker_count = 2
|
2021-02-17 15:54:52 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestPolicerInput, self).setUp()
|
|
|
|
|
|
|
|
self.create_pg_interfaces(range(2))
|
|
|
|
for i in self.pg_interfaces:
|
|
|
|
i.admin_up()
|
|
|
|
i.config_ip4()
|
|
|
|
i.resolve_arp()
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
self.pkt = (
|
|
|
|
Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
|
|
|
|
/ IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4)
|
|
|
|
/ UDP(sport=1234, dport=1234)
|
|
|
|
/ Raw(b"\xa5" * 100)
|
|
|
|
)
|
2021-02-17 15:54:52 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
for i in self.pg_interfaces:
|
|
|
|
i.unconfig_ip4()
|
|
|
|
i.admin_down()
|
|
|
|
super(TestPolicerInput, self).tearDown()
|
|
|
|
|
2022-04-05 19:23:12 +02:00
|
|
|
def policer_interface_test(self, dir: Dir):
|
2021-02-18 11:02:29 +00:00
|
|
|
pkts = self.pkt * NUM_PKTS
|
|
|
|
|
2021-02-17 15:54:52 +00:00
|
|
|
action_tx = PolicerAction(
|
2022-04-26 19:02:15 +02:00
|
|
|
VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
|
|
|
|
)
|
|
|
|
policer = VppPolicer(
|
|
|
|
self,
|
|
|
|
"pol1",
|
|
|
|
80,
|
|
|
|
0,
|
|
|
|
1000,
|
|
|
|
0,
|
|
|
|
conform_action=action_tx,
|
|
|
|
exceed_action=action_tx,
|
|
|
|
violate_action=action_tx,
|
|
|
|
)
|
2021-02-17 15:54:52 +00:00
|
|
|
policer.add_vpp_config()
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
sw_if_index = self.pg0.sw_if_index if dir == Dir.RX else self.pg1.sw_if_index
|
2022-04-05 19:23:12 +02:00
|
|
|
|
2021-02-17 15:54:52 +00:00
|
|
|
# Start policing on pg0
|
2022-04-05 19:23:12 +02:00
|
|
|
policer.apply_vpp_config(sw_if_index, dir, True)
|
2021-02-17 15:54:52 +00:00
|
|
|
|
2021-02-18 11:02:29 +00:00
|
|
|
rx = self.send_and_expect(self.pg0, pkts, self.pg1, worker=0)
|
2021-02-17 15:54:52 +00:00
|
|
|
stats = policer.get_stats()
|
|
|
|
|
|
|
|
# Single rate, 2 colour policer - expect conform, violate but no exceed
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertGreater(stats["conform_packets"], 0)
|
|
|
|
self.assertEqual(stats["exceed_packets"], 0)
|
|
|
|
self.assertGreater(stats["violate_packets"], 0)
|
2021-02-17 15:54:52 +00:00
|
|
|
|
|
|
|
# Stop policing on pg0
|
2022-04-05 19:23:12 +02:00
|
|
|
policer.apply_vpp_config(sw_if_index, dir, False)
|
2021-02-17 15:54:52 +00:00
|
|
|
|
2021-02-18 11:02:29 +00:00
|
|
|
rx = self.send_and_expect(self.pg0, pkts, self.pg1, worker=0)
|
|
|
|
|
2021-02-17 15:54:52 +00:00
|
|
|
statsnew = policer.get_stats()
|
|
|
|
|
|
|
|
# No new packets counted
|
|
|
|
self.assertEqual(stats, statsnew)
|
|
|
|
|
|
|
|
policer.remove_vpp_config()
|
|
|
|
|
2022-04-05 19:23:12 +02:00
|
|
|
def test_policer_input(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Input Policing"""
|
2022-04-05 19:23:12 +02:00
|
|
|
self.policer_interface_test(Dir.RX)
|
|
|
|
|
|
|
|
def test_policer_output(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Output Policing"""
|
2022-04-05 19:23:12 +02:00
|
|
|
self.policer_interface_test(Dir.TX)
|
|
|
|
|
2023-01-06 11:57:38 +00:00
|
|
|
def test_policer_reset(self):
|
|
|
|
"""Policer reset bucket"""
|
|
|
|
pkts = self.pkt * NUM_PKTS
|
|
|
|
|
|
|
|
action_tx = PolicerAction(
|
|
|
|
VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
|
|
|
|
)
|
|
|
|
policer = VppPolicer(
|
|
|
|
self,
|
|
|
|
"pol1",
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
10000,
|
|
|
|
0,
|
|
|
|
conform_action=action_tx,
|
|
|
|
exceed_action=action_tx,
|
|
|
|
violate_action=action_tx,
|
|
|
|
)
|
|
|
|
policer.add_vpp_config()
|
|
|
|
|
|
|
|
# Start policing on pg0
|
|
|
|
policer.apply_vpp_config(self.pg0.sw_if_index, Dir.RX, True)
|
|
|
|
|
|
|
|
self.send_and_expect(self.pg0, pkts, self.pg1, worker=0)
|
|
|
|
details = policer.get_details()
|
|
|
|
|
|
|
|
self.assertGreater(details.current_limit, details.current_bucket)
|
|
|
|
|
|
|
|
self.send_and_expect(self.pg0, pkts, self.pg1, worker=0)
|
|
|
|
self.vapi.policer_reset(policer_index=policer.policer_index)
|
|
|
|
details = policer.get_details()
|
|
|
|
|
|
|
|
self.assertEqual(details.current_limit, details.current_bucket)
|
|
|
|
|
|
|
|
policer.apply_vpp_config(self.pg0.sw_if_index, Dir.RX, False)
|
|
|
|
|
|
|
|
policer.remove_vpp_config()
|
|
|
|
|
|
|
|
def test_policer_update(self):
|
|
|
|
"""Policer update"""
|
|
|
|
pkts = self.pkt * NUM_PKTS
|
|
|
|
|
|
|
|
action_tx = PolicerAction(
|
|
|
|
VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
|
|
|
|
)
|
|
|
|
policer = VppPolicer(
|
|
|
|
self,
|
|
|
|
"pol1",
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
10000,
|
|
|
|
0,
|
|
|
|
conform_action=action_tx,
|
|
|
|
exceed_action=action_tx,
|
|
|
|
violate_action=action_tx,
|
|
|
|
)
|
|
|
|
policer.add_vpp_config()
|
|
|
|
|
|
|
|
# Start policing on pg0
|
|
|
|
policer.apply_vpp_config(self.pg0.sw_if_index, Dir.RX, True)
|
|
|
|
|
|
|
|
self.send_and_expect(self.pg0, pkts, self.pg1, worker=0)
|
|
|
|
details_before = policer.get_details()
|
|
|
|
|
|
|
|
self.assertGreater(details_before.current_limit, details_before.current_bucket)
|
|
|
|
|
|
|
|
policer.cir = 8000
|
|
|
|
policer.commited_burst = 100000
|
|
|
|
policer.update()
|
|
|
|
|
|
|
|
details_after = policer.get_details()
|
|
|
|
|
|
|
|
self.assertGreater(details_after.cir, details_before.cir)
|
|
|
|
self.assertGreater(details_after.cb, details_before.cb)
|
|
|
|
|
|
|
|
policer.apply_vpp_config(self.pg0.sw_if_index, Dir.RX, False)
|
|
|
|
|
|
|
|
policer.remove_vpp_config()
|
|
|
|
|
2022-04-05 19:23:12 +02:00
|
|
|
def policer_handoff_test(self, dir: Dir):
|
2021-02-18 11:02:29 +00:00
|
|
|
pkts = self.pkt * NUM_PKTS
|
|
|
|
|
|
|
|
action_tx = PolicerAction(
|
2022-04-26 19:02:15 +02:00
|
|
|
VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0
|
|
|
|
)
|
|
|
|
policer = VppPolicer(
|
|
|
|
self,
|
|
|
|
"pol2",
|
|
|
|
80,
|
|
|
|
0,
|
|
|
|
1000,
|
|
|
|
0,
|
|
|
|
conform_action=action_tx,
|
|
|
|
exceed_action=action_tx,
|
|
|
|
violate_action=action_tx,
|
|
|
|
)
|
2021-02-18 11:02:29 +00:00
|
|
|
policer.add_vpp_config()
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
sw_if_index = self.pg0.sw_if_index if dir == Dir.RX else self.pg1.sw_if_index
|
2022-04-05 19:23:12 +02:00
|
|
|
|
2021-02-18 11:02:29 +00:00
|
|
|
# Bind the policer to worker 1
|
|
|
|
policer.bind_vpp_config(1, True)
|
|
|
|
|
|
|
|
# Start policing on pg0
|
2022-04-05 19:23:12 +02:00
|
|
|
policer.apply_vpp_config(sw_if_index, dir, True)
|
2021-02-18 11:02:29 +00:00
|
|
|
|
|
|
|
for worker in [0, 1]:
|
|
|
|
self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker)
|
|
|
|
self.logger.debug(self.vapi.cli("show trace max 100"))
|
|
|
|
|
|
|
|
stats = policer.get_stats()
|
|
|
|
stats0 = policer.get_stats(worker=0)
|
|
|
|
stats1 = policer.get_stats(worker=1)
|
|
|
|
|
|
|
|
# Worker 1, should have done all the policing
|
|
|
|
self.assertEqual(stats, stats1)
|
|
|
|
|
|
|
|
# Worker 0, should have handed everything off
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertEqual(stats0["conform_packets"], 0)
|
|
|
|
self.assertEqual(stats0["exceed_packets"], 0)
|
|
|
|
self.assertEqual(stats0["violate_packets"], 0)
|
2021-02-18 11:02:29 +00:00
|
|
|
|
|
|
|
# Unbind the policer from worker 1 and repeat
|
|
|
|
policer.bind_vpp_config(1, False)
|
|
|
|
for worker in [0, 1]:
|
|
|
|
self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker)
|
|
|
|
self.logger.debug(self.vapi.cli("show trace max 100"))
|
|
|
|
|
|
|
|
# The policer should auto-bind to worker 0 when packets arrive
|
|
|
|
stats = policer.get_stats()
|
|
|
|
|
|
|
|
# The 2 workers should now have policed the same amount
|
|
|
|
stats = policer.get_stats()
|
|
|
|
stats0 = policer.get_stats(worker=0)
|
|
|
|
stats1 = policer.get_stats(worker=1)
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertGreater(stats0["conform_packets"], 0)
|
|
|
|
self.assertEqual(stats0["exceed_packets"], 0)
|
|
|
|
self.assertGreater(stats0["violate_packets"], 0)
|
2021-02-18 11:02:29 +00:00
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertGreater(stats1["conform_packets"], 0)
|
|
|
|
self.assertEqual(stats1["exceed_packets"], 0)
|
|
|
|
self.assertGreater(stats1["violate_packets"], 0)
|
2021-02-18 11:02:29 +00:00
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertEqual(
|
|
|
|
stats0["conform_packets"] + stats1["conform_packets"],
|
|
|
|
stats["conform_packets"],
|
|
|
|
)
|
2021-02-18 11:02:29 +00:00
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
self.assertEqual(
|
|
|
|
stats0["violate_packets"] + stats1["violate_packets"],
|
|
|
|
stats["violate_packets"],
|
|
|
|
)
|
2021-02-18 11:02:29 +00:00
|
|
|
|
|
|
|
# Stop policing on pg0
|
2022-04-05 19:23:12 +02:00
|
|
|
policer.apply_vpp_config(sw_if_index, dir, False)
|
2021-02-18 11:02:29 +00:00
|
|
|
|
|
|
|
policer.remove_vpp_config()
|
2021-02-17 15:54:52 +00:00
|
|
|
|
2022-04-05 19:23:12 +02:00
|
|
|
def test_policer_handoff_input(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Worker thread handoff policer input"""
|
2022-04-05 19:23:12 +02:00
|
|
|
self.policer_handoff_test(Dir.RX)
|
|
|
|
|
|
|
|
def test_policer_handoff_output(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Worker thread handoff policer output"""
|
2022-04-05 19:23:12 +02:00
|
|
|
self.policer_handoff_test(Dir.TX)
|
|
|
|
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
if __name__ == "__main__":
|
2021-02-17 15:54:52 +00:00
|
|
|
unittest.main(testRunner=VppTestRunner)
|