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; |
| 66 | u32 n_threads = vec_len (vlib_mains); |
| 67 | int something_changed = 0; |
| 68 | clib_bitmap_t *pending_int = 0; |
| 69 | int last_int = -1; |
| 70 | |
| 71 | log_debug ("update node '%U' triggered by interface %v", |
| 72 | format_vlib_node_name, vm, node_index, hi->name); |
| 73 | |
| 74 | vec_validate (d, n_threads - 1); |
| 75 | vec_validate_init_empty (per_thread_node_state, n_threads - 1, |
| 76 | VLIB_NODE_STATE_DISABLED); |
| 77 | |
| 78 | /* find out desired node state on each thread */ |
| 79 | pool_foreach (rxq, im->hw_if_rx_queues) |
| 80 | { |
| 81 | u32 ti = rxq->thread_index; |
| 82 | |
| 83 | ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_UNKNOWN); |
| 84 | ASSERT (rxq->mode != VNET_HW_IF_RX_MODE_DEFAULT); |
| 85 | |
| 86 | hi = vnet_get_hw_interface (vnm, rxq->hw_if_index); |
| 87 | |
| 88 | if (hi->input_node_index != node_index) |
| 89 | continue; |
| 90 | |
| 91 | if (rxq->mode == VNET_HW_IF_RX_MODE_POLLING) |
| 92 | per_thread_node_state[ti] = VLIB_NODE_STATE_POLLING; |
| 93 | |
| 94 | if (per_thread_node_state[ti] == VLIB_NODE_STATE_POLLING) |
| 95 | continue; |
| 96 | |
| 97 | if (rxq->mode == VNET_HW_IF_RX_MODE_INTERRUPT || |
| 98 | rxq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) |
| 99 | per_thread_node_state[ti] = VLIB_NODE_STATE_INTERRUPT; |
| 100 | } |
| 101 | |
| 102 | /* construct per-thread polling vectors */ |
| 103 | pool_foreach (rxq, im->hw_if_rx_queues) |
| 104 | { |
| 105 | u32 ti = rxq->thread_index; |
| 106 | uword flags; |
| 107 | |
| 108 | hi = vnet_get_hw_interface (vnm, rxq->hw_if_index); |
| 109 | |
| 110 | if (hi->input_node_index != node_index) |
| 111 | continue; |
| 112 | |
| 113 | flags = vnet_sw_interface_get_flags (vnm, hi->sw_if_index); |
| 114 | if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0) |
| 115 | { |
| 116 | log_debug ("skip interface %v (admin down)", hi->name); |
| 117 | continue; |
| 118 | } |
| 119 | |
| 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 | { |
| 136 | vlib_node_state_t old_state; |
| 137 | vec_sort_with_function (d[i], poll_data_sort); |
| 138 | |
| 139 | old_state = vlib_node_get_state (vlib_mains[i], node_index); |
| 140 | if (per_thread_node_state[i] != old_state) |
| 141 | { |
| 142 | something_changed = 1; |
| 143 | log_debug ("state changed for node %U on thread %u from %s to %s", |
| 144 | format_vlib_node_name, vm, node_index, i, |
| 145 | node_state_str[old_state], |
| 146 | node_state_str[per_thread_node_state[i]]); |
| 147 | } |
| 148 | |
| 149 | /* check if something changed */ |
| 150 | if (something_changed == 0) |
| 151 | { |
| 152 | vnet_hw_if_rx_node_runtime_t *rt; |
| 153 | rt = vlib_node_get_runtime_data (vlib_mains[i], node_index); |
| 154 | if (vec_len (rt->rxq_poll_vector) != vec_len (d[i])) |
| 155 | something_changed = 1; |
| 156 | else if (memcmp (d[i], rt->rxq_poll_vector, |
| 157 | vec_len (d[i]) * sizeof (*d))) |
| 158 | something_changed = 1; |
| 159 | if (clib_interrupt_get_n_int (rt->rxq_interrupts) != last_int + 1) |
| 160 | something_changed = 1; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (something_changed) |
| 165 | { |
| 166 | int with_barrier; |
| 167 | |
| 168 | if (vlib_worker_thread_barrier_held ()) |
| 169 | { |
| 170 | with_barrier = 0; |
| 171 | log_debug ("%s", "already running under the barrier"); |
| 172 | } |
| 173 | else |
| 174 | with_barrier = 1; |
| 175 | |
| 176 | if (with_barrier) |
| 177 | vlib_worker_thread_barrier_sync (vm); |
| 178 | |
| 179 | for (int i = 0; i < n_threads; i++) |
| 180 | { |
| 181 | vlib_main_t *vm = vlib_mains[i]; |
| 182 | vnet_hw_if_rx_node_runtime_t *rt; |
| 183 | rt = vlib_node_get_runtime_data (vm, node_index); |
| 184 | pv = rt->rxq_poll_vector; |
| 185 | rt->rxq_poll_vector = d[i]; |
| 186 | d[i] = pv; |
| 187 | |
| 188 | if (rt->rxq_interrupts) |
| 189 | { |
| 190 | void *in = rt->rxq_interrupts; |
| 191 | int int_num = -1; |
| 192 | while ((int_num = clib_interrupt_get_next (in, int_num)) != -1) |
| 193 | { |
| 194 | clib_interrupt_clear (in, int_num); |
| 195 | pending_int = clib_bitmap_set (pending_int, int_num, 1); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | vlib_node_set_state (vm, node_index, per_thread_node_state[i]); |
| 200 | |
| 201 | if (last_int >= 0) |
| 202 | clib_interrupt_resize (&rt->rxq_interrupts, last_int + 1); |
| 203 | else |
| 204 | clib_interrupt_free (&rt->rxq_interrupts); |
| 205 | } |
| 206 | |
| 207 | if (with_barrier) |
| 208 | vlib_worker_thread_barrier_release (vm); |
| 209 | } |
| 210 | else |
| 211 | log_debug ("skipping update of node '%U', no changes detected", |
| 212 | format_vlib_node_name, vm, node_index); |
| 213 | |
| 214 | if (pending_int) |
| 215 | { |
| 216 | int i; |
| 217 | clib_bitmap_foreach (i, pending_int) |
| 218 | { |
| 219 | vnet_hw_if_rx_queue_set_int_pending (vnm, i); |
| 220 | } |
| 221 | clib_bitmap_free (pending_int); |
| 222 | } |
| 223 | |
| 224 | for (int i = 0; i < n_threads; i++) |
| 225 | vec_free (d[i]); |
| 226 | |
| 227 | vec_free (d); |
| 228 | vec_free (per_thread_node_state); |
| 229 | } |