interface: Add interface monitor cli
[vpp.git] / src / vnet / interface / monitor.c
1 /*
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
19 static clib_error_t *
20 monitor_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 (
93         vm, "rx: %Upps %Ubps tx: %Upps %Ubps%c", format_base10,
94         (u32) ((vrx[spin].packets - vrx[spin ^ 1].packets) / tsd),
95         format_base10, (u32) ((vrx[spin].bytes - vrx[spin ^ 1].bytes) / tsd),
96         format_base10,
97         (u32) ((vtx[spin].packets - vtx[spin ^ 1].packets) / tsd),
98         format_base10, (u32) ((vtx[spin].bytes - vtx[spin ^ 1].bytes) / tsd));
99     }
100
101 done:
102   return error;
103 }
104
105 VLIB_CLI_COMMAND (monitor_interface_command, static) = {
106   .path = "monitor interface",
107   .short_help =
108     "monitor interface <interface> [interval <intv>] [count <count>]",
109   .function = monitor_interface_command_fn,
110   .is_mp_safe = 1,
111 };
112
113 /*
114  * fd.io coding-style-patch-verification: ON
115  *
116  * Local Variables:
117  * eval: (c-set-style "gnu")
118  * End:
119  */