blob: bd3def9d3243606feef54cd909f8975ba4d1c321 [file] [log] [blame]
Damjan Marion22311502016-10-28 20:30:15 +02001/*
2 * Copyright (c) 2015 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
Damjan Marion7dc41462016-11-15 19:47:58 +010016#include <vnet/vnet.h>
Damjan Marion51327ac2016-11-09 11:59:42 +010017#include <vnet/devices/devices.h>
Damjan Marion22311502016-10-28 20:30:15 +020018#include <vnet/feature/feature.h>
Damjan Marion7dc41462016-11-15 19:47:58 +010019#include <vnet/ip/ip.h>
20#include <vnet/ethernet/ethernet.h>
Damjan Marion8973b072022-03-01 15:51:18 +010021#include <vlib/stats/stats.h>
Damjan Marion22311502016-10-28 20:30:15 +020022
Damjan Marionb3bb1012017-02-28 21:55:28 +010023vnet_device_main_t vnet_device_main;
24
Damjan Marion51327ac2016-11-09 11:59:42 +010025static uword
26device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
27 vlib_frame_t * frame)
28{
29 return 0;
30}
31
Damjan Marion22311502016-10-28 20:30:15 +020032/* *INDENT-OFF* */
Damjan Marion51327ac2016-11-09 11:59:42 +010033VLIB_REGISTER_NODE (device_input_node) = {
34 .function = device_input_fn,
35 .name = "device-input",
Mohammed Hawarib85b0df2021-01-08 17:19:09 +010036 .runtime_data_bytes = sizeof (vnet_hw_if_rx_node_runtime_t),
Damjan Marion51327ac2016-11-09 11:59:42 +010037 .type = VLIB_NODE_TYPE_INPUT,
38 .state = VLIB_NODE_STATE_DISABLED,
39 .n_next_nodes = VNET_DEVICE_INPUT_N_NEXT_NODES,
40 .next_nodes = VNET_DEVICE_INPUT_NEXT_NODES,
41};
42
Dave Barach9f6186e2016-11-08 12:12:12 -050043VNET_FEATURE_ARC_INIT (device_input, static) =
44{
Damjan Marion22311502016-10-28 20:30:15 +020045 .arc_name = "device-input",
Damjan Marion51327ac2016-11-09 11:59:42 +010046 .start_nodes = VNET_FEATURES ("device-input"),
Dave Baracha25def72018-11-26 11:04:45 -050047 .last_in_arc = "ethernet-input",
Damjan Marion8b3191e2016-11-09 19:54:20 +010048 .arc_index_ptr = &feature_main.device_input_feature_arc_index,
Damjan Marion22311502016-10-28 20:30:15 +020049};
50
51VNET_FEATURE_INIT (l2_patch, static) = {
52 .arc_name = "device-input",
53 .node_name = "l2-patch",
54 .runs_before = VNET_FEATURES ("ethernet-input"),
55};
56
57VNET_FEATURE_INIT (worker_handoff, static) = {
58 .arc_name = "device-input",
59 .node_name = "worker-handoff",
60 .runs_before = VNET_FEATURES ("ethernet-input"),
61};
62
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +010063VNET_FEATURE_INIT (span_input, static) = {
64 .arc_name = "device-input",
65 .node_name = "span-input",
66 .runs_before = VNET_FEATURES ("ethernet-input"),
67};
68
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020069VNET_FEATURE_INIT (p2p_ethernet_node, static) = {
70 .arc_name = "device-input",
71 .node_name = "p2p-ethernet-input",
72 .runs_before = VNET_FEATURES ("ethernet-input"),
73};
74
Damjan Marion22311502016-10-28 20:30:15 +020075VNET_FEATURE_INIT (ethernet_input, static) = {
76 .arc_name = "device-input",
77 .node_name = "ethernet-input",
78 .runs_before = 0, /* not before any other features */
79};
80/* *INDENT-ON* */
81
Damjan Marion8973b072022-03-01 15:51:18 +010082static void
83input_rate_collector_fn (vlib_stats_collector_data_t *d)
84{
85 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
86 vlib_stats_entry_t *e2 = sm->directory_vector + d->private_data;
87 static u64 last_input_packets = 0;
88 f64 dt, now;
89
90 now = vlib_time_now (vlib_get_main ());
91 u64 input_packets = vnet_get_aggregate_rx_packets ();
92
93 dt = now - e2->value;
94 d->entry->value = (f64) (input_packets - last_input_packets) / dt;
95 last_input_packets = input_packets;
96 e2->value = now;
97}
98
Damjan Marionb3bb1012017-02-28 21:55:28 +010099static clib_error_t *
100vnet_device_init (vlib_main_t * vm)
101{
102 vnet_device_main_t *vdm = &vnet_device_main;
103 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marioneb743fa2017-03-20 16:34:15 +0100104 vlib_thread_registration_t *tr;
Damjan Marion8973b072022-03-01 15:51:18 +0100105 vlib_stats_collector_reg_t reg = {};
Damjan Marioneb743fa2017-03-20 16:34:15 +0100106 uword *p;
Damjan Marionb3bb1012017-02-28 21:55:28 +0100107
108 vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1,
109 CLIB_CACHE_LINE_BYTES);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100110
111 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
112 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
113 if (tr && tr->count > 0)
114 {
Damjan Marion586afd72017-04-05 19:18:20 +0200115 vdm->first_worker_thread_index = tr->first_index;
116 vdm->next_worker_thread_index = tr->first_index;
117 vdm->last_worker_thread_index = tr->first_index + tr->count - 1;
Damjan Marioneb743fa2017-03-20 16:34:15 +0100118 }
Damjan Marion8973b072022-03-01 15:51:18 +0100119
120 reg.private_data = vlib_stats_add_timestamp ("/sys/last_update");
121 reg.entry_index = vlib_stats_add_gauge ("/sys/input_rate");
122 reg.collect_fn = input_rate_collector_fn;
123 vlib_stats_register_collector_fn (&reg);
124
Damjan Marionb3bb1012017-02-28 21:55:28 +0100125 return 0;
126}
127
128VLIB_INIT_FUNCTION (vnet_device_init);
Damjan Marioneb743fa2017-03-20 16:34:15 +0100129
Damjan Marion22311502016-10-28 20:30:15 +0200130/*
131 * fd.io coding-style-patch-verification: ON
132 *
133 * Local Variables:
134 * eval: (c-set-style "gnu")
135 * End:
136 */