FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
interface_stats.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vppinfra/error.h>
19 
20 #include <vnet/feature/feature.h>
21 #include <vnet/ethernet/ethernet.h>
22 
23 int
25 {
28  ethernet_main_t *em;
29  vnet_main_t *vnm;
30 
31  vnm = vnet_get_main ();
32  em = &ethernet_main;
33  si = vnet_get_sw_interface (vnm, sw_if_index);
34 
35  /*
36  * only ethernet HW interfaces are supported at this time
37  */
39  {
40  return (VNET_API_ERROR_INVALID_VALUE);
41  }
42 
43  eif = ethernet_get_interface (em, si->hw_if_index);
44 
45  if (!eif)
46  {
47  return (VNET_API_ERROR_FEATURE_DISABLED);
48  }
49 
50  vnet_feature_enable_disable ("device-input", "stats-collect-rx",
51  sw_if_index, enable, 0, 0);
52  vnet_feature_enable_disable ("interface-output", "stats-collect-tx",
53  sw_if_index, enable, 0, 0);
54 
55  return (0);
56 }
57 
58 static u8 *
59 format_stats_collect_trace (u8 * s, va_list * args)
60 {
61  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
63 
64  return s;
65 }
66 
67 #define inc_counter(ctype, rx_tx) \
68 { \
69 }
70 
73  vlib_node_runtime_t * node,
74  vlib_frame_t * frame, vlib_rx_or_tx_t rxtx)
75 {
77  u32 n_left_from, *from, *to_next;
78  u32 next_index;
79  u32 sw_if_index = 0;
80  u32 stats_n_packets[VNET_N_COMBINED_INTERFACE_COUNTER] = { 0 };
81  u64 stats_n_bytes[VNET_N_COMBINED_INTERFACE_COUNTER] = { 0 };
82 
83  from = vlib_frame_vector_args (frame);
84  n_left_from = frame->n_vectors;
85  next_index = node->cached_next_index;
86 
87  while (n_left_from > 0)
88  {
89  u32 n_left_to_next;
90 
91  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
92 
93  while (n_left_from > 0 && n_left_to_next > 0)
94  {
95  u32 bi0;
96  vlib_buffer_t *b0;
97  u32 next0 = 0;
98  int b0_ctype;
99 
100  /* speculatively enqueue b0 to the current next frame */
101  to_next[0] = bi0 = from[0];
102  to_next += 1;
103  n_left_to_next -= 1;
104  from += 1;
105  n_left_from -= 1;
106 
107  b0 = vlib_get_buffer (vm, bi0);
108  sw_if_index = vnet_buffer (b0)->sw_if_index[rxtx];
109 
110  if (VLIB_RX == rxtx)
111  {
112  b0_ctype =
114  }
115  else
116  {
117  b0_ctype =
119  }
120 
121  stats_n_bytes[b0_ctype] += vlib_buffer_length_in_chain (vm, b0);
122  stats_n_packets[b0_ctype] += 1;
123 
124  vnet_feature_next (sw_if_index, &next0, b0);
125 
126  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
127  n_left_to_next, bi0, next0);
128  }
129 
130  if (VLIB_RX == rxtx)
131  {
133  {
137  sw_if_index, stats_n_packets[ct], stats_n_bytes[ct]);
138  }
139  }
140  else
141  {
143  {
147  sw_if_index, stats_n_packets[ct], stats_n_bytes[ct]);
148  }
149  }
150 
151  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
152  }
153 
154  return frame->n_vectors;
155 }
156 
157 static uword
159  vlib_node_runtime_t * node, vlib_frame_t * frame)
160 {
161  return stats_collect_inline (vm, node, frame, VLIB_RX);
162 }
163 
164 static uword
166  vlib_node_runtime_t * node, vlib_frame_t * frame)
167 {
168  return stats_collect_inline (vm, node, frame, VLIB_TX);
169 }
170 
171 /* *INDENT-OFF* */
173  .vector_size = sizeof (u32),
174  .format_trace = format_stats_collect_trace,
175  .type = VLIB_NODE_TYPE_INTERNAL,
176  .n_errors = 0,
177  .n_next_nodes = 0,
178  .function = stats_collect_rx,
179  .name = "stats-collect-rx",
180 };
181 
183  .vector_size = sizeof (u32),
184  .format_trace = format_stats_collect_trace,
185  .type = VLIB_NODE_TYPE_INTERNAL,
186  .n_errors = 0,
187  .n_next_nodes = 0,
188  .function = stats_collect_tx,
189  .name = "stats-collect-tx",
190 };
191 
194 
196  .arc_name = "device-input",
197  .node_name = "stats-collect-rx",
198  .runs_before = VNET_FEATURES ("ethernet-input"),
199 };
200 
202  .arc_name = "interface-output",
203  .node_name = "stats-collect-tx",
204  .runs_before = VNET_FEATURES ("interface-tx"),
205 };
206 
207 /* *INDENT-ON* */
208 
209 static clib_error_t *
211 {
212  return 0;
213 }
214 
216 
217 
218 /*
219  * fd.io coding-style-patch-verification: ON
220  *
221  * Local Variables:
222  * eval: (c-set-style "gnu")
223  * End:
224  */
#define CLIB_UNUSED(x)
Definition: clib.h:79
vlib_node_registration_t stats_collect_rx_node
(constructor) VLIB_REGISTER_NODE (stats_collect_rx_node)
static u8 * format_stats_collect_trace(u8 *s, va_list *args)
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:211
static_always_inline uword stats_collect_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, vlib_rx_or_tx_t rxtx)
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
vnet_interface_main_t interface_main
Definition: vnet.h:56
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:107
vlib_rx_or_tx_t
Definition: defs.h:44
ethernet_main_t ethernet_main
Definition: init.c:45
#define static_always_inline
Definition: clib.h:93
static int eh_dst_addr_to_rx_ctype(ethernet_header_t *eh)
Definition: packet.h:92
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:718
unsigned long u64
Definition: types.h:89
int vnet_sw_interface_stats_collect_enable_disable(u32 sw_if_index, u8 enable)
VLIB_NODE_FUNCTION_MULTIARCH(stats_collect_rx_node, stats_collect_rx)
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:209
vnet_main_t vnet_main
Definition: misc.c:43
static_always_inline void vnet_feature_next(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:221
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u16 n_vectors
Definition: node.h:344
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:294
#define foreach_tx_combined_interface_counter(_x)
Definition: interface.h:663
VNET_FEATURE_INIT(stats_collect_rx_node, static)
static int eh_dst_addr_to_tx_ctype(ethernet_header_t *eh)
Definition: packet.h:110
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:454
vnet_interface_counter_type_t
Definition: interface.h:633
static uword stats_collect_rx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
unsigned int u32
Definition: types.h:88
#define foreach_rx_combined_interface_counter(_x)
Definition: interface.h:658
#define VNET_FEATURES(...)
Definition: feature.h:375
u64 uword
Definition: types.h:112
Definition: defs.h:47
ethernet_interface_t * ethernet_get_interface(ethernet_main_t *em, u32 hw_if_index)
Definition: interface.c:705
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
#define vnet_buffer(b)
Definition: buffer.h:372
vnet_sw_interface_type_t type
Definition: interface.h:583
static uword stats_collect_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
vlib_node_registration_t stats_collect_tx_node
(constructor) VLIB_REGISTER_NODE (stats_collect_tx_node)
static clib_error_t * stats_collect_init(vlib_main_t *vm)
Definition: defs.h:46
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:233