Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 1 | /* |
| 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 Marion | 7dc4146 | 2016-11-15 19:47:58 +0100 | [diff] [blame] | 16 | #include <vnet/vnet.h> |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 17 | #include <vnet/devices/devices.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 18 | #include <vnet/feature/feature.h> |
Damjan Marion | 7dc4146 | 2016-11-15 19:47:58 +0100 | [diff] [blame] | 19 | #include <vnet/ip/ip.h> |
| 20 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 21 | #include <vlib/stats/stats.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 22 | |
Damjan Marion | b3bb101 | 2017-02-28 21:55:28 +0100 | [diff] [blame] | 23 | vnet_device_main_t vnet_device_main; |
| 24 | |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 25 | static uword |
| 26 | device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 27 | vlib_frame_t * frame) |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 32 | /* *INDENT-OFF* */ |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 33 | VLIB_REGISTER_NODE (device_input_node) = { |
| 34 | .function = device_input_fn, |
| 35 | .name = "device-input", |
Mohammed Hawari | b85b0df | 2021-01-08 17:19:09 +0100 | [diff] [blame] | 36 | .runtime_data_bytes = sizeof (vnet_hw_if_rx_node_runtime_t), |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 37 | .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 | |
Damjan Marion | 7dc4146 | 2016-11-15 19:47:58 +0100 | [diff] [blame] | 43 | /* Table defines how much we need to advance current data pointer |
| 44 | in the buffer if we shortcut to l3 nodes */ |
| 45 | |
| 46 | const u32 __attribute__((aligned (CLIB_CACHE_LINE_BYTES))) |
| 47 | device_input_next_node_advance[((VNET_DEVICE_INPUT_N_NEXT_NODES / |
| 48 | CLIB_CACHE_LINE_BYTES) +1) * CLIB_CACHE_LINE_BYTES] = |
| 49 | { |
| 50 | [VNET_DEVICE_INPUT_NEXT_IP4_INPUT] = sizeof (ethernet_header_t), |
Sergio Gonzalez Monroy | 767b2f5 | 2016-12-20 15:20:32 +0000 | [diff] [blame] | 51 | [VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT] = sizeof (ethernet_header_t), |
Damjan Marion | 7dc4146 | 2016-11-15 19:47:58 +0100 | [diff] [blame] | 52 | [VNET_DEVICE_INPUT_NEXT_IP6_INPUT] = sizeof (ethernet_header_t), |
| 53 | [VNET_DEVICE_INPUT_NEXT_MPLS_INPUT] = sizeof (ethernet_header_t), |
| 54 | }; |
| 55 | |
Damjan Marion | bd846cd | 2017-11-21 13:12:41 +0100 | [diff] [blame] | 56 | const u32 __attribute__((aligned (CLIB_CACHE_LINE_BYTES))) |
| 57 | device_input_next_node_flags[((VNET_DEVICE_INPUT_N_NEXT_NODES / |
| 58 | CLIB_CACHE_LINE_BYTES) +1) * CLIB_CACHE_LINE_BYTES] = |
| 59 | { |
| 60 | [VNET_DEVICE_INPUT_NEXT_IP4_INPUT] = VNET_BUFFER_F_L3_HDR_OFFSET_VALID, |
| 61 | [VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT] = VNET_BUFFER_F_L3_HDR_OFFSET_VALID, |
| 62 | [VNET_DEVICE_INPUT_NEXT_IP6_INPUT] = VNET_BUFFER_F_L3_HDR_OFFSET_VALID, |
| 63 | [VNET_DEVICE_INPUT_NEXT_MPLS_INPUT] = VNET_BUFFER_F_L3_HDR_OFFSET_VALID, |
| 64 | }; |
| 65 | |
Dave Barach | 9f6186e | 2016-11-08 12:12:12 -0500 | [diff] [blame] | 66 | VNET_FEATURE_ARC_INIT (device_input, static) = |
| 67 | { |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 68 | .arc_name = "device-input", |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 69 | .start_nodes = VNET_FEATURES ("device-input"), |
Dave Barach | a25def7 | 2018-11-26 11:04:45 -0500 | [diff] [blame] | 70 | .last_in_arc = "ethernet-input", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 71 | .arc_index_ptr = &feature_main.device_input_feature_arc_index, |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | VNET_FEATURE_INIT (l2_patch, static) = { |
| 75 | .arc_name = "device-input", |
| 76 | .node_name = "l2-patch", |
| 77 | .runs_before = VNET_FEATURES ("ethernet-input"), |
| 78 | }; |
| 79 | |
| 80 | VNET_FEATURE_INIT (worker_handoff, static) = { |
| 81 | .arc_name = "device-input", |
| 82 | .node_name = "worker-handoff", |
| 83 | .runs_before = VNET_FEATURES ("ethernet-input"), |
| 84 | }; |
| 85 | |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 86 | VNET_FEATURE_INIT (span_input, static) = { |
| 87 | .arc_name = "device-input", |
| 88 | .node_name = "span-input", |
| 89 | .runs_before = VNET_FEATURES ("ethernet-input"), |
| 90 | }; |
| 91 | |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 92 | VNET_FEATURE_INIT (p2p_ethernet_node, static) = { |
| 93 | .arc_name = "device-input", |
| 94 | .node_name = "p2p-ethernet-input", |
| 95 | .runs_before = VNET_FEATURES ("ethernet-input"), |
| 96 | }; |
| 97 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 98 | VNET_FEATURE_INIT (ethernet_input, static) = { |
| 99 | .arc_name = "device-input", |
| 100 | .node_name = "ethernet-input", |
| 101 | .runs_before = 0, /* not before any other features */ |
| 102 | }; |
| 103 | /* *INDENT-ON* */ |
| 104 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 105 | static void |
| 106 | input_rate_collector_fn (vlib_stats_collector_data_t *d) |
| 107 | { |
| 108 | vlib_stats_segment_t *sm = vlib_stats_get_segment (); |
| 109 | vlib_stats_entry_t *e2 = sm->directory_vector + d->private_data; |
| 110 | static u64 last_input_packets = 0; |
| 111 | f64 dt, now; |
| 112 | |
| 113 | now = vlib_time_now (vlib_get_main ()); |
| 114 | u64 input_packets = vnet_get_aggregate_rx_packets (); |
| 115 | |
| 116 | dt = now - e2->value; |
| 117 | d->entry->value = (f64) (input_packets - last_input_packets) / dt; |
| 118 | last_input_packets = input_packets; |
| 119 | e2->value = now; |
| 120 | } |
| 121 | |
Damjan Marion | b3bb101 | 2017-02-28 21:55:28 +0100 | [diff] [blame] | 122 | static clib_error_t * |
| 123 | vnet_device_init (vlib_main_t * vm) |
| 124 | { |
| 125 | vnet_device_main_t *vdm = &vnet_device_main; |
| 126 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 127 | vlib_thread_registration_t *tr; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 128 | vlib_stats_collector_reg_t reg = {}; |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 129 | uword *p; |
Damjan Marion | b3bb101 | 2017-02-28 21:55:28 +0100 | [diff] [blame] | 130 | |
| 131 | vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1, |
| 132 | CLIB_CACHE_LINE_BYTES); |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 133 | |
| 134 | p = hash_get_mem (tm->thread_registrations_by_name, "workers"); |
| 135 | tr = p ? (vlib_thread_registration_t *) p[0] : 0; |
| 136 | if (tr && tr->count > 0) |
| 137 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 138 | vdm->first_worker_thread_index = tr->first_index; |
| 139 | vdm->next_worker_thread_index = tr->first_index; |
| 140 | vdm->last_worker_thread_index = tr->first_index + tr->count - 1; |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 141 | } |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 142 | |
| 143 | reg.private_data = vlib_stats_add_timestamp ("/sys/last_update"); |
| 144 | reg.entry_index = vlib_stats_add_gauge ("/sys/input_rate"); |
| 145 | reg.collect_fn = input_rate_collector_fn; |
| 146 | vlib_stats_register_collector_fn (®); |
| 147 | |
Damjan Marion | b3bb101 | 2017-02-28 21:55:28 +0100 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | VLIB_INIT_FUNCTION (vnet_device_init); |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 152 | |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 153 | /* |
| 154 | * fd.io coding-style-patch-verification: ON |
| 155 | * |
| 156 | * Local Variables: |
| 157 | * eval: (c-set-style "gnu") |
| 158 | * End: |
| 159 | */ |