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 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", &input_colour); /* input colour if aware */
38 total_bytes = (rate_kbps * burst) / 8;
39 num_pkts = total_bytes / PKT_LEN;
41 cpu_speed = (double) os_cpu_clock_frequency ();
42 cpu_ticks_per_byte = cpu_speed / (rate_kbps * 125);
43 cpu_ticks_per_pkt = cpu_ticks_per_byte * PKT_LEN;
45 pol = &pm->policers[policer_index];
47 for (i = 0; i < num_pkts; i++)
49 time += cpu_ticks_per_pkt;
50 policer_time = ((uint64_t) time) >> POLICER_TICKS_PER_PERIOD_SHIFT;
51 result = vnet_police_packet (pol, PKT_LEN, input_colour, policer_time);
52 vlib_increment_combined_counter (&policer_counters[result], 0,
53 policer_index, 1, PKT_LEN);
59 VLIB_CLI_COMMAND (test_policer_command, static) = {
60 .path = "test policing",
61 .short_help = "policer unit test helper - DO NOT RUN ON A LIVE SYSTEM",
62 .function = policer_test,
66 policer_test_init (vlib_main_t *vm)
71 VLIB_INIT_FUNCTION (policer_test_init);