blob: 3ae1fd29156b7dccd2a897f4579c97e1fd501d65 [file] [log] [blame]
Nathan Skrzypczak162ff5e2021-11-09 18:18:21 +01001/*
2 * Copyright (c) 2021 Cisco and/or its affiliates.
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
16#include <vnet/vnet.h>
17#include <vlib/vlib.h>
18
19static clib_error_t *
20monitor_interface_command_fn (vlib_main_t *vm, unformat_input_t *input,
21 vlib_cli_command_t *cmd)
22{
23 const vnet_main_t *vnm = vnet_get_main ();
24 const vlib_combined_counter_main_t *counters =
25 vnm->interface_main.combined_sw_if_counters;
26 f64 refresh_interval = 1.0;
27 u32 refresh_count = ~0;
28 clib_error_t *error = 0;
29 vlib_counter_t vrx[2], vtx[2];
30 f64 ts[2];
31 u32 hw_if_index = ~0;
32 u8 spin = 0;
33
34 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
35 {
36 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm,
37 &hw_if_index))
38 ;
39 else if (unformat (input, "interval %f", &refresh_interval))
40 ;
41 else if (unformat (input, "count %u", &refresh_count))
42 ;
43 else
44 {
45 error = clib_error_return (0, "unknown input `%U'",
46 format_unformat_error, input);
47 goto done;
48 }
49 }
50
51 if (hw_if_index == ~0)
52 {
53 error = clib_error_return (0, "no interface passed");
54 goto done;
55 }
56
57 vlib_get_combined_counter (counters + VNET_INTERFACE_COUNTER_RX, hw_if_index,
58 &vrx[spin]);
59 vlib_get_combined_counter (counters + VNET_INTERFACE_COUNTER_TX, hw_if_index,
60 &vtx[spin]);
61 ts[spin] = vlib_time_now (vm);
62
63 while (refresh_count--)
64 {
65 f64 sleep_interval, tsd;
66
67 while (((sleep_interval =
68 ts[spin] + refresh_interval - vlib_time_now (vm)) > 0.0))
69 {
70 uword event_type, *event_data = 0;
71 vlib_process_wait_for_event_or_clock (vm, sleep_interval);
72 event_type = vlib_process_get_events (vm, &event_data);
73 switch (event_type)
74 {
75 case ~0: /* no events => timeout */
76 break;
77 default:
78 /* someone pressed a key, abort */
79 vlib_cli_output (vm, "Aborted due to a keypress.");
80 goto done;
81 }
82 vec_free (event_data);
83 }
84 spin ^= 1;
85 vlib_get_combined_counter (counters + VNET_INTERFACE_COUNTER_RX,
86 hw_if_index, &vrx[spin]);
87 vlib_get_combined_counter (counters + VNET_INTERFACE_COUNTER_TX,
88 hw_if_index, &vtx[spin]);
89 ts[spin] = vlib_time_now (vm);
90
91 tsd = ts[spin] - ts[spin ^ 1];
92 vlib_cli_output (
Pim van Pelta5266c62022-07-19 08:32:22 +000093 vm, "rx: %Upps %Ubps tx: %Upps %Ubps", format_base10,
94 (u64) ((vrx[spin].packets - vrx[spin ^ 1].packets) / tsd),
Nathan Skrzypczak162ff5e2021-11-09 18:18:21 +010095 format_base10,
Pim van Pelta5266c62022-07-19 08:32:22 +000096 (u64) (8 * (vrx[spin].bytes - vrx[spin ^ 1].bytes) / tsd),
97 format_base10,
98 (u64) ((vtx[spin].packets - vtx[spin ^ 1].packets) / tsd),
99 format_base10,
100 (u64) (8 * (vtx[spin].bytes - vtx[spin ^ 1].bytes) / tsd));
Nathan Skrzypczak162ff5e2021-11-09 18:18:21 +0100101 }
102
103done:
104 return error;
105}
106
107VLIB_CLI_COMMAND (monitor_interface_command, static) = {
108 .path = "monitor interface",
109 .short_help =
110 "monitor interface <interface> [interval <intv>] [count <count>]",
111 .function = monitor_interface_command_fn,
112 .is_mp_safe = 1,
113};
114
115/*
116 * fd.io coding-style-patch-verification: ON
117 *
118 * Local Variables:
119 * eval: (c-set-style "gnu")
120 * End:
121 */