interface: Add interface monitor cli
This adds an interface CLI 'monitor
interface <interface>' that periodically
reports rx/tx pps & bandwidth for a
given interface.
Type: feature
Change-Id: Ia9d59b3443913520a52b38d7bda012190be6f167
Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
diff --git a/src/vnet/interface/monitor.c b/src/vnet/interface/monitor.c
new file mode 100644
index 0000000..6d79154
--- /dev/null
+++ b/src/vnet/interface/monitor.c
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2021 Cisco and/or its affiliates.
+ * 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/vnet.h>
+#include <vlib/vlib.h>
+
+static clib_error_t *
+monitor_interface_command_fn (vlib_main_t *vm, unformat_input_t *input,
+ vlib_cli_command_t *cmd)
+{
+ const vnet_main_t *vnm = vnet_get_main ();
+ const vlib_combined_counter_main_t *counters =
+ vnm->interface_main.combined_sw_if_counters;
+ f64 refresh_interval = 1.0;
+ u32 refresh_count = ~0;
+ clib_error_t *error = 0;
+ vlib_counter_t vrx[2], vtx[2];
+ f64 ts[2];
+ u32 hw_if_index = ~0;
+ u8 spin = 0;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "%U", unformat_vnet_hw_interface, vnm,
+ &hw_if_index))
+ ;
+ else if (unformat (input, "interval %f", &refresh_interval))
+ ;
+ else if (unformat (input, "count %u", &refresh_count))
+ ;
+ else
+ {
+ error = clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ goto done;
+ }
+ }
+
+ if (hw_if_index == ~0)
+ {
+ error = clib_error_return (0, "no interface passed");
+ goto done;
+ }
+
+ vlib_get_combined_counter (counters + VNET_INTERFACE_COUNTER_RX, hw_if_index,
+ &vrx[spin]);
+ vlib_get_combined_counter (counters + VNET_INTERFACE_COUNTER_TX, hw_if_index,
+ &vtx[spin]);
+ ts[spin] = vlib_time_now (vm);
+
+ while (refresh_count--)
+ {
+ f64 sleep_interval, tsd;
+
+ while (((sleep_interval =
+ ts[spin] + refresh_interval - vlib_time_now (vm)) > 0.0))
+ {
+ uword event_type, *event_data = 0;
+ vlib_process_wait_for_event_or_clock (vm, sleep_interval);
+ event_type = vlib_process_get_events (vm, &event_data);
+ switch (event_type)
+ {
+ case ~0: /* no events => timeout */
+ break;
+ default:
+ /* someone pressed a key, abort */
+ vlib_cli_output (vm, "Aborted due to a keypress.");
+ goto done;
+ }
+ vec_free (event_data);
+ }
+ spin ^= 1;
+ vlib_get_combined_counter (counters + VNET_INTERFACE_COUNTER_RX,
+ hw_if_index, &vrx[spin]);
+ vlib_get_combined_counter (counters + VNET_INTERFACE_COUNTER_TX,
+ hw_if_index, &vtx[spin]);
+ ts[spin] = vlib_time_now (vm);
+
+ tsd = ts[spin] - ts[spin ^ 1];
+ vlib_cli_output (
+ vm, "rx: %Upps %Ubps tx: %Upps %Ubps%c", format_base10,
+ (u32) ((vrx[spin].packets - vrx[spin ^ 1].packets) / tsd),
+ format_base10, (u32) ((vrx[spin].bytes - vrx[spin ^ 1].bytes) / tsd),
+ format_base10,
+ (u32) ((vtx[spin].packets - vtx[spin ^ 1].packets) / tsd),
+ format_base10, (u32) ((vtx[spin].bytes - vtx[spin ^ 1].bytes) / tsd));
+ }
+
+done:
+ return error;
+}
+
+VLIB_CLI_COMMAND (monitor_interface_command, static) = {
+ .path = "monitor interface",
+ .short_help =
+ "monitor interface <interface> [interval <intv>] [count <count>]",
+ .function = monitor_interface_command_fn,
+ .is_mp_safe = 1,
+};
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */