tests: add policer test helper 75/31175/2
authorBrian Russell <brian@graphiant.com>
Thu, 4 Feb 2021 17:23:44 +0000 (17:23 +0000)
committerNeale Ranns <neale@graphiant.com>
Wed, 10 Feb 2021 16:39:22 +0000 (16:39 +0000)
Add a helper CLI to exercise a policer pre-configured by the test
harness. The test harness will check the stats afterwards.

Type: test
Signed-off-by: Brian Russell <brian@graphiant.com>
Change-Id: I913dda4a9f8179c1c6b3061a68164bf1e698a392

src/plugins/unittest/CMakeLists.txt
src/plugins/unittest/policer_test.c [new file with mode: 0644]

index 64a7706..b6322cd 100644 (file)
@@ -38,6 +38,7 @@ add_vpp_plugin(unittest
   mem_bulk_test.c
   mfib_test.c
   mpcap_node.c
+  policer_test.c
   punt_test.c
   rbtree_test.c
   session_test.c
diff --git a/src/plugins/unittest/policer_test.c b/src/plugins/unittest/policer_test.c
new file mode 100644 (file)
index 0000000..862ea93
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2021 Graphiant, Inc.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <vnet/policer/policer.h>
+
+#define PKT_LEN 500
+
+static clib_error_t *
+policer_test (vlib_main_t *vm, unformat_input_t *input,
+             vlib_cli_command_t *cmd_arg)
+{
+  int policer_index, i;
+  uint rate_kbps, burst, num_pkts;
+  double total_bytes, cpu_ticks_per_pkt, time = 0;
+  double cpu_speed, cpu_ticks_per_byte;
+  policer_result_e result, input_colour = POLICE_CONFORM;
+  uint64_t policer_time;
+
+  policer_read_response_type_st *pol;
+  vnet_policer_main_t *pm = &vnet_policer_main;
+
+  unformat (input, "index %d", &policer_index); /* policer to use */
+  unformat (input, "rate %u", &rate_kbps);     /* rate to send at in kbps */
+  unformat (input, "burst %u", &burst);                /* burst to send in ms */
+  unformat (input, "colour %u", &input_colour); /* input colour if aware */
+
+  total_bytes = (rate_kbps * burst) / 8;
+  num_pkts = total_bytes / PKT_LEN;
+
+  cpu_speed = (double) os_cpu_clock_frequency ();
+  cpu_ticks_per_byte = cpu_speed / (rate_kbps * 125);
+  cpu_ticks_per_pkt = cpu_ticks_per_byte * PKT_LEN;
+
+  pol = &pm->policers[policer_index];
+
+  for (i = 0; i < num_pkts; i++)
+    {
+      time += cpu_ticks_per_pkt;
+      policer_time = ((uint64_t) time) >> POLICER_TICKS_PER_PERIOD_SHIFT;
+      result = vnet_police_packet (pol, PKT_LEN, input_colour, policer_time);
+      vlib_increment_combined_counter (&policer_counters[result], 0,
+                                      policer_index, 1, PKT_LEN);
+    }
+
+  return NULL;
+}
+
+VLIB_CLI_COMMAND (test_policer_command, static) = {
+  .path = "test policing",
+  .short_help = "policer unit test helper - DO NOT RUN ON A LIVE SYSTEM",
+  .function = policer_test,
+};
+
+clib_error_t *
+policer_test_init (vlib_main_t *vm)
+{
+  return 0;
+}
+
+VLIB_INIT_FUNCTION (policer_test_init);