2 * Copyright (c) 2021 Graphiant, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vnet/policer/policer.h>
20 policer_test (vlib_main_t *vm, unformat_input_t *input,
21 vlib_cli_command_t *cmd_arg)
24 uint rate_kbps, burst, num_pkts;
25 double total_bytes, cpu_ticks_per_pkt, time = 0;
26 double cpu_speed, cpu_ticks_per_byte;
27 policer_result_e result, input_colour = POLICE_CONFORM;
28 uint64_t policer_time;
31 vnet_policer_main_t *pm = &vnet_policer_main;
33 if (!unformat (input, "index %d", &policer_index) || /* policer to use */
34 !unformat (input, "rate %u", &rate_kbps) || /* rate to send at in kbps */
35 !unformat (input, "burst %u", &burst) || /* burst to send in ms */
36 !unformat (input, "colour %u",
37 &input_colour)) /* input colour if aware */
38 return clib_error_return (0, "Policer test failed to parse params");
40 total_bytes = (rate_kbps * burst) / 8;
41 num_pkts = total_bytes / PKT_LEN;
43 cpu_speed = (double) os_cpu_clock_frequency ();
44 cpu_ticks_per_byte = cpu_speed / (rate_kbps * 125);
45 cpu_ticks_per_pkt = cpu_ticks_per_byte * PKT_LEN;
47 pol = &pm->policers[policer_index];
49 for (i = 0; i < num_pkts; i++)
51 time += cpu_ticks_per_pkt;
52 policer_time = ((uint64_t) time) >> POLICER_TICKS_PER_PERIOD_SHIFT;
53 result = vnet_police_packet (pol, PKT_LEN, input_colour, policer_time);
54 vlib_increment_combined_counter (&policer_counters[result], 0,
55 policer_index, 1, PKT_LEN);
61 VLIB_CLI_COMMAND (test_policer_command, static) = {
62 .path = "test policing",
63 .short_help = "policer unit test helper - DO NOT RUN ON A LIVE SYSTEM",
64 .function = policer_test,
68 policer_test_init (vlib_main_t *vm)
73 VLIB_INIT_FUNCTION (policer_test_init);