aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/interface/monitor.c
blob: 3ae1fd29156b7dccd2a897f4579c97e1fd501d65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * 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", format_base10,
	(u64) ((vrx[spin].packets - vrx[spin ^ 1].packets) / tsd),
	format_base10,
	(u64) (8 * (vrx[spin].bytes - vrx[spin ^ 1].bytes) / tsd),
	format_base10,
	(u64) ((vtx[spin].packets - vtx[spin ^ 1].packets) / tsd),
	format_base10,
	(u64) (8 * (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:
 */