Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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 <vnet/devices/devices.h> |
| 18 | #include <vnet/feature/feature.h> |
| 19 | #include <vnet/ip/ip.h> |
| 20 | #include <vnet/ethernet/ethernet.h> |
| 21 | #include <vnet/interface/rx_queue_funcs.h> |
| 22 | #include <vlib/unix/unix.h> |
| 23 | |
| 24 | VLIB_REGISTER_LOG_CLASS (if_rxq_log, static) = { |
| 25 | .class_name = "interface", |
| 26 | .subclass_name = "runtime", |
| 27 | }; |
| 28 | |
| 29 | #define log_debug(fmt, ...) vlib_log_debug (if_rxq_log.class, fmt, __VA_ARGS__) |
| 30 | #define log_err(fmt, ...) vlib_log_err (if_rxq_log.class, fmt, __VA_ARGS__) |
| 31 | |
| 32 | static char *node_state_str[] = { |
| 33 | [VLIB_NODE_STATE_DISABLED] = "disabled", |
| 34 | [VLIB_NODE_STATE_POLLING] = "polling", |
| 35 | [VLIB_NODE_STATE_INTERRUPT] = "interrupt", |
| 36 | }; |
| 37 | |
| 38 | static int |
| 39 | poll_data_sort (void *a1, void *a2) |
| 40 | { |
| 41 | vnet_hw_if_rxq_poll_vector_t *pv1 = a1; |
| 42 | vnet_hw_if_rxq_poll_vector_t *pv2 = a2; |
| 43 | |
| 44 | if (pv1->dev_instance > pv2->dev_instance) |
| 45 | return 1; |
| 46 | else if (pv1->dev_instance < pv2->dev_instance) |
| 47 | return -1; |
| 48 | else if (pv1->queue_id > pv2->queue_id) |
| 49 | return 1; |
| 50 | else if (pv1->queue_id < pv2->queue_id) |
| 51 | return -1; |
| 52 | else |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | vnet_hw_if_update_runtime_data (vnet_main_t *vnm, u32 hw_if_index) |
| 58 | { |
| 59 | vlib_main_t *vm = vlib_get_main (); |
| 60 | vnet_interface_main_t *im = &vnm->interface_main; |
| 61 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 62 | u32 node_index = hi->input_node_index; |
| 63 | vnet_hw_if_rx_queue_t *rxq; |
| 64 | vnet_hw_if_rxq_poll_vector_t *pv, **d = 0; |
| 65 | vlib_node_state_t *per_thread_node_state = 0; |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 66 | u32 n_threads = vlib_get_n_threads (); |
Florin Coras | 982e44f | 2021-03-19 13:12:41 -0700 | [diff] [blame] | 67 | u16 *per_thread_node_adaptive = 0; |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 68 | int something_changed = 0; |
| 69 | clib_bitmap_t *pending_int = 0; |
| 70 | int last_int = -1; |
| 71 | |
| 72 | log_debug ("update node '%U' triggered by interface %v", |
| 73 | format_vlib_node_name, vm, node_index, hi->name); |
| 74 | |
| 75 | vec_validate (d, n_threads - 1); |
| 76 | vec_validate_init_empty (per_thread_node_state, n_threads - 1, |
| 77 | VLIB_NODE_STATE_DISABLED); |
Florin Coras | 982e44f | 2021-03-19 13:12:41 -0700 | [diff] [blame] | 78 | vec_validate_init_empty (per_thread_node_adaptive, n_threads - 1, 0); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 79 | |
| 80 | /* find out desired node state on each thread */ |
| 81 | pool_foreach (rxq, im->hw_if_rx_queues) |
| 82 | { |
| 83 | u32 ti = rxq->thread_index; |
| 84 | |
| 85 | ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN); |
| 86 | ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT); |
| 87 | |
| 88 | hi = vnet_get_hw_interface (vnm, rxq->hw_if_index); |
| 89 | |
| 90 | if (hi->input_node_index != node_index) |
| 91 | continue; |
| 92 | |
| 93 | if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING) |
Florin Coras | 982e44f | 2021-03-19 13:12:41 -0700 | [diff] [blame] | 94 | { |
| 95 | per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING; |
| 96 | per_thread_node_adaptive[ti] = 0; |
| 97 | } |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 98 | |
| 99 | if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING) |
| 100 | continue; |
| 101 | |
| 102 | if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT || |
| 103 | rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) |
| 104 | per_thread_node_state[ti] = VLIB_NODE_STATE_INTERRUPT; |
Florin Coras | 982e44f | 2021-03-19 13:12:41 -0700 | [diff] [blame] | 105 | |
| 106 | if (rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) |
| 107 | per_thread_node_adaptive[ti] = 1; |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* construct per-thread polling vectors */ |
| 111 | pool_foreach (rxq, im->hw_if_rx_queues) |
| 112 | { |
| 113 | u32 ti = rxq->thread_index; |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 114 | |
| 115 | hi = vnet_get_hw_interface (vnm, rxq->hw_if_index); |
| 116 | |
| 117 | if (hi->input_node_index != node_index) |
| 118 | continue; |
| 119 | |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 120 | if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT || |
| 121 | rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) |
| 122 | last_int = clib_max (last_int, rxq - im->hw_if_rx_queues); |
| 123 | |
| 124 | if (per_thread_node_state[ti] != VLIB_NODE_STATE_POLLING) |
| 125 | continue; |
| 126 | |
| 127 | vec_add2_aligned (d[ti], pv, 1, CLIB_CACHE_LINE_BYTES); |
| 128 | pv->dev_instance = rxq->dev_instance; |
| 129 | pv->queue_id = rxq->queue_id; |
| 130 | } |
| 131 | |
| 132 | /* sort poll vectors and compare them with active ones to avoid |
| 133 | * unnecesary barrier */ |
| 134 | for (int i = 0; i < n_threads; i++) |
| 135 | { |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 136 | vlib_main_t *ovm = vlib_get_main_by_index (i); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 137 | vlib_node_state_t old_state; |
| 138 | vec_sort_with_function (d[i], poll_data_sort); |
| 139 | |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 140 | old_state = vlib_node_get_state (ovm, node_index); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 141 | if (per_thread_node_state[i] != old_state) |
| 142 | { |
| 143 | something_changed = 1; |
| 144 | log_debug ("state changed for node %U on thread %u from %s to %s", |
| 145 | format_vlib_node_name, vm, node_index, i, |
| 146 | node_state_str[old_state], |
| 147 | node_state_str[per_thread_node_state[i]]); |
| 148 | } |
| 149 | |
| 150 | /* check if something changed */ |
| 151 | if (something_changed == 0) |
| 152 | { |
| 153 | vnet_hw_if_rx_node_runtime_t *rt; |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 154 | rt = vlib_node_get_runtime_data (ovm, node_index); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 155 | if (vec_len (rt->rxq_poll_vector) != vec_len (d[i])) |
| 156 | something_changed = 1; |
| 157 | else if (memcmp (d[i], rt->rxq_poll_vector, |
| 158 | vec_len (d[i]) * sizeof (*d))) |
| 159 | something_changed = 1; |
| 160 | if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1) |
| 161 | something_changed = 1; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (something_changed) |
| 166 | { |
| 167 | int with_barrier; |
| 168 | |
| 169 | if (vlib_worker_thread_barrier_held ()) |
| 170 | { |
| 171 | with_barrier = 0; |
| 172 | log_debug ("%s", "already running under the barrier"); |
| 173 | } |
| 174 | else |
| 175 | with_barrier = 1; |
| 176 | |
| 177 | if (with_barrier) |
| 178 | vlib_worker_thread_barrier_sync (vm); |
| 179 | |
| 180 | for (int i = 0; i < n_threads; i++) |
| 181 | { |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 182 | vlib_main_t *vm = vlib_get_main_by_index (i); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 183 | vnet_hw_if_rx_node_runtime_t *rt; |
| 184 | rt = vlib_node_get_runtime_data (vm, node_index); |
| 185 | pv = rt->rxq_poll_vector; |
| 186 | rt->rxq_poll_vector = d[i]; |
| 187 | d[i] = pv; |
| 188 | |
| 189 | if (rt->rxq_interrupts) |
| 190 | { |
| 191 | void *in = rt->rxq_interrupts; |
| 192 | int int_num = -1; |
| 193 | while ((int_num = clib_interrupt_get_next (in, int_num)) != -1) |
| 194 | { |
| 195 | clib_interrupt_clear (in, int_num); |
| 196 | pending_int = clib_bitmap_set (pending_int, int_num, 1); |
Mohammed Hawari | 313380f | 2020-12-22 15:12:27 +0100 | [diff] [blame] | 197 | last_int = clib_max (last_int, int_num); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | vlib_node_set_state (vm, node_index, per_thread_node_state[i]); |
Florin Coras | 982e44f | 2021-03-19 13:12:41 -0700 | [diff] [blame] | 202 | vlib_node_set_flag (vm, node_index, VLIB_NODE_FLAG_ADAPTIVE_MODE, |
| 203 | per_thread_node_adaptive[i]); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 204 | |
| 205 | if (last_int >= 0) |
| 206 | clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1); |
| 207 | else |
| 208 | clib_interrupt_free (&rt->rxq_interrupts); |
| 209 | } |
| 210 | |
| 211 | if (with_barrier) |
| 212 | vlib_worker_thread_barrier_release (vm); |
| 213 | } |
| 214 | else |
| 215 | log_debug ("skipping update of node '%U', no changes detected", |
| 216 | format_vlib_node_name, vm, node_index); |
| 217 | |
| 218 | if (pending_int) |
| 219 | { |
| 220 | int i; |
| 221 | clib_bitmap_foreach (i, pending_int) |
| 222 | { |
| 223 | vnet_hw_if_rx_queue_set_int_pending (vnm, i); |
| 224 | } |
| 225 | clib_bitmap_free (pending_int); |
| 226 | } |
| 227 | |
| 228 | for (int i = 0; i < n_threads; i++) |
| 229 | vec_free (d[i]); |
| 230 | |
| 231 | vec_free (d); |
| 232 | vec_free (per_thread_node_state); |
Florin Coras | 982e44f | 2021-03-19 13:12:41 -0700 | [diff] [blame] | 233 | vec_free (per_thread_node_adaptive); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 234 | } |