Line data Source code
1 : /* 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: 6 : * 7 : * http://www.apache.org/licenses/LICENSE-2.0 8 : * 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. 14 : */ 15 : #include <vnet/policer/policer.h> 16 : 17 : #define PKT_LEN 500 18 : 19 : static clib_error_t * 20 13 : policer_test (vlib_main_t *vm, unformat_input_t *input, 21 : vlib_cli_command_t *cmd_arg) 22 : { 23 : int policer_index, i; 24 : uint rate_kbps, burst, num_pkts; 25 13 : double total_bytes, cpu_ticks_per_pkt, time = 0; 26 : double cpu_speed, cpu_ticks_per_byte; 27 13 : policer_result_e result, input_colour = POLICE_CONFORM; 28 : uint64_t policer_time; 29 : 30 : policer_t *pol; 31 13 : vnet_policer_main_t *pm = &vnet_policer_main; 32 : 33 26 : if (!unformat (input, "index %d", &policer_index) || /* policer to use */ 34 26 : !unformat (input, "rate %u", &rate_kbps) || /* rate to send at in kbps */ 35 26 : !unformat (input, "burst %u", &burst) || /* burst to send in ms */ 36 13 : !unformat (input, "colour %u", 37 : &input_colour)) /* input colour if aware */ 38 0 : return clib_error_return (0, "Policer test failed to parse params"); 39 : 40 13 : total_bytes = (rate_kbps * burst) / 8; 41 13 : num_pkts = total_bytes / PKT_LEN; 42 : 43 13 : cpu_speed = (double) os_cpu_clock_frequency (); 44 13 : cpu_ticks_per_byte = cpu_speed / (rate_kbps * 125); 45 13 : cpu_ticks_per_pkt = cpu_ticks_per_byte * PKT_LEN; 46 : 47 13 : pol = &pm->policers[policer_index]; 48 : 49 260013 : for (i = 0; i < num_pkts; i++) 50 : { 51 260000 : time += cpu_ticks_per_pkt; 52 260000 : policer_time = ((uint64_t) time) >> POLICER_TICKS_PER_PERIOD_SHIFT; 53 260000 : result = vnet_police_packet (pol, PKT_LEN, input_colour, policer_time); 54 260000 : vlib_increment_combined_counter (&policer_counters[result], 0, 55 : policer_index, 1, PKT_LEN); 56 : } 57 : 58 13 : return NULL; 59 : } 60 : 61 16239 : 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, 65 : }; 66 : 67 : clib_error_t * 68 559 : policer_test_init (vlib_main_t *vm) 69 : { 70 559 : return 0; 71 : } 72 : 73 5599 : VLIB_INIT_FUNCTION (policer_test_init);