Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <fcntl.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 21 | |
| 22 | #include <vlib/vlib.h> |
| 23 | #include <vlib/unix/unix.h> |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 24 | #include <vnet/vnet.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 25 | #include <vnet/ethernet/ethernet.h> |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 26 | #include <vnet/gso/gro_func.h> |
Mohsin Kazmi | 0b04209 | 2020-04-17 16:50:56 +0000 | [diff] [blame] | 27 | #include <vnet/gso/hdr_offset_parser.h> |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 28 | #include <vnet/ip/ip4_packet.h> |
| 29 | #include <vnet/ip/ip6_packet.h> |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 30 | #include <vnet/ip/ip_psh_cksum.h> |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 31 | #include <vnet/tcp/tcp_packet.h> |
| 32 | #include <vnet/udp/udp_packet.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 33 | #include <vnet/devices/virtio/virtio.h> |
| 34 | |
Mohsin Kazmi | 637c97c | 2021-05-12 14:51:29 +0200 | [diff] [blame] | 35 | #define VIRTIO_TX_MAX_CHAIN_LEN 127 |
| 36 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 37 | #define foreach_virtio_tx_func_error \ |
| 38 | _(NO_FREE_SLOTS, "no free tx slots") \ |
| 39 | _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \ |
| 40 | _(PENDING_MSGS, "pending msgs in tx ring") \ |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 41 | _(INDIRECT_DESC_ALLOC_FAILED, "indirect descriptor allocation failed - packet drop") \ |
| 42 | _(OUT_OF_ORDER, "out-of-order buffers in used ring") \ |
| 43 | _(GSO_PACKET_DROP, "gso disabled on itf -- gso packet drop") \ |
| 44 | _(CSUM_OFFLOAD_PACKET_DROP, "checksum offload disabled on itf -- csum offload packet drop") |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 45 | |
| 46 | typedef enum |
| 47 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 48 | #define _(f,s) VIRTIO_TX_ERROR_##f, |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 49 | foreach_virtio_tx_func_error |
| 50 | #undef _ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 51 | VIRTIO_TX_N_ERROR, |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 52 | } virtio_tx_func_error_t; |
| 53 | |
| 54 | static char *virtio_tx_func_error_strings[] = { |
| 55 | #define _(n,s) s, |
| 56 | foreach_virtio_tx_func_error |
| 57 | #undef _ |
| 58 | }; |
| 59 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 60 | static u8 * |
| 61 | format_virtio_device (u8 * s, va_list * args) |
| 62 | { |
| 63 | u32 dev_instance = va_arg (*args, u32); |
| 64 | int verbose = va_arg (*args, int); |
| 65 | u32 indent = format_get_indent (s); |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 66 | virtio_main_t *vim = &virtio_main; |
| 67 | virtio_if_t *vif = vec_elt_at_index (vim->interfaces, dev_instance); |
| 68 | vnet_virtio_vring_t *vring = 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 69 | |
| 70 | s = format (s, "VIRTIO interface"); |
| 71 | if (verbose) |
| 72 | { |
| 73 | s = format (s, "\n%U instance %u", format_white_space, indent + 2, |
| 74 | dev_instance); |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 75 | s = format (s, "\n%U RX QUEUE : Total Packets", format_white_space, |
| 76 | indent + 4); |
| 77 | vec_foreach (vring, vif->rxq_vrings) |
| 78 | { |
| 79 | s = format (s, "\n%U %8u : %llu", format_white_space, indent + 4, |
| 80 | RX_QUEUE_ACCESS (vring->queue_id), vring->total_packets); |
| 81 | } |
| 82 | s = format (s, "\n%U TX QUEUE : Total Packets", format_white_space, |
| 83 | indent + 4); |
| 84 | vec_foreach (vring, vif->txq_vrings) |
| 85 | { |
| 86 | s = format (s, "\n%U %8u : %llu", format_white_space, indent + 4, |
| 87 | TX_QUEUE_ACCESS (vring->queue_id), vring->total_packets); |
| 88 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 89 | } |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 90 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 91 | return s; |
| 92 | } |
| 93 | |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 94 | typedef struct |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 95 | { |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 96 | u32 buffer_index; |
| 97 | u32 sw_if_index; |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 98 | generic_header_offset_t gho; |
Damjan Marion | 8c69887 | 2020-10-10 19:17:58 +0200 | [diff] [blame] | 99 | vlib_buffer_t buffer; |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 100 | } virtio_tx_trace_t; |
| 101 | |
| 102 | static u8 * |
| 103 | format_virtio_tx_trace (u8 * s, va_list * va) |
| 104 | { |
| 105 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 106 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
| 107 | virtio_tx_trace_t *t = va_arg (*va, virtio_tx_trace_t *); |
| 108 | u32 indent = format_get_indent (s); |
| 109 | |
BenoƮt Ganne | 03f2a01 | 2021-07-20 16:49:13 +0200 | [diff] [blame] | 110 | s = format (s, "%Ubuffer 0x%x: %U\n", format_white_space, indent, |
| 111 | t->buffer_index, format_vnet_buffer_no_chain, &t->buffer); |
Mohsin Kazmi | b3a8f4e | 2020-09-15 16:41:44 +0000 | [diff] [blame] | 112 | s = |
| 113 | format (s, "%U%U\n", format_white_space, indent, |
| 114 | format_generic_header_offset, &t->gho); |
| 115 | s = |
| 116 | format (s, "%U%U", format_white_space, indent, |
| 117 | format_ethernet_header_with_length, t->buffer.pre_data, |
| 118 | sizeof (t->buffer.pre_data)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 119 | return s; |
| 120 | } |
| 121 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 122 | static void |
| 123 | virtio_tx_trace (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b0, |
| 124 | u32 bi, int is_tun) |
Mohsin Kazmi | 03b2695 | 2020-09-16 16:53:16 +0000 | [diff] [blame] | 125 | { |
| 126 | virtio_tx_trace_t *t; |
| 127 | t = vlib_add_trace (vm, node, b0, sizeof (t[0])); |
| 128 | t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; |
| 129 | t->buffer_index = bi; |
Vladislav Grishenko | 62fc787 | 2021-11-20 14:52:53 +0500 | [diff] [blame] | 130 | clib_memset (&t->gho, 0, sizeof (t->gho)); |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 131 | if (is_tun) |
Mohsin Kazmi | 03b2695 | 2020-09-16 16:53:16 +0000 | [diff] [blame] | 132 | { |
| 133 | int is_ip4 = 0, is_ip6 = 0; |
| 134 | |
| 135 | switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0) |
| 136 | { |
| 137 | case 0x40: |
| 138 | is_ip4 = 1; |
| 139 | break; |
| 140 | case 0x60: |
| 141 | is_ip6 = 1; |
| 142 | break; |
| 143 | default: |
| 144 | break; |
| 145 | } |
| 146 | vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4, is_ip6); |
| 147 | } |
| 148 | else |
| 149 | vnet_generic_header_offset_parser (b0, &t->gho, 1, |
| 150 | b0->flags & |
| 151 | VNET_BUFFER_F_IS_IP4, |
| 152 | b0->flags & VNET_BUFFER_F_IS_IP6); |
| 153 | |
| 154 | clib_memcpy_fast (&t->buffer, b0, sizeof (*b0) - sizeof (b0->pre_data)); |
| 155 | clib_memcpy_fast (t->buffer.pre_data, vlib_buffer_get_current (b0), |
| 156 | sizeof (t->buffer.pre_data)); |
| 157 | } |
| 158 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 159 | static void |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 160 | virtio_interface_drop_inline (vlib_main_t *vm, virtio_if_t *vif, |
| 161 | uword node_index, u32 *buffers, u16 n, |
| 162 | virtio_tx_func_error_t error) |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 163 | { |
| 164 | vlib_error_count (vm, node_index, error, n); |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 165 | vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters + |
| 166 | VNET_INTERFACE_COUNTER_DROP, |
| 167 | vm->thread_index, vif->sw_if_index, n); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 168 | vlib_buffer_free (vm, buffers, n); |
| 169 | } |
| 170 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 171 | static void |
| 172 | virtio_memset_ring_u32 (u32 *ring, u32 start, u32 ring_size, u32 n_buffers) |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 173 | { |
| 174 | ASSERT (n_buffers <= ring_size); |
| 175 | |
| 176 | if (PREDICT_TRUE (start + n_buffers <= ring_size)) |
| 177 | { |
| 178 | clib_memset_u32 (ring + start, ~0, n_buffers); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | clib_memset_u32 (ring + start, ~0, ring_size - start); |
| 183 | clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start)); |
| 184 | } |
| 185 | } |
| 186 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 187 | static void |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 188 | virtio_free_used_device_desc_split (vlib_main_t *vm, |
| 189 | vnet_virtio_vring_t *vring, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 190 | uword node_index) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 191 | { |
| 192 | u16 used = vring->desc_in_use; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 193 | u16 sz = vring->queue_size; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 194 | u16 mask = sz - 1; |
| 195 | u16 last = vring->last_used_idx; |
| 196 | u16 n_left = vring->used->idx - last; |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 197 | u16 out_of_order_count = 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 198 | |
| 199 | if (n_left == 0) |
| 200 | return; |
| 201 | |
| 202 | while (n_left) |
| 203 | { |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 204 | vnet_virtio_vring_used_elem_t *e = &vring->used->ring[last & mask]; |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 205 | u16 slot, n_buffers; |
| 206 | slot = n_buffers = e->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 207 | |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 208 | while (e->id == (n_buffers & mask)) |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 209 | { |
| 210 | n_left--; |
| 211 | last++; |
| 212 | n_buffers++; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 213 | vnet_virtio_vring_desc_t *d = &vring->desc[e->id]; |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 214 | u16 next; |
| 215 | while (d->flags & VRING_DESC_F_NEXT) |
| 216 | { |
| 217 | n_buffers++; |
| 218 | next = d->next; |
| 219 | d = &vring->desc[next]; |
| 220 | } |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 221 | if (n_left == 0) |
| 222 | break; |
| 223 | e = &vring->used->ring[last & mask]; |
| 224 | } |
| 225 | vlib_buffer_free_from_ring (vm, vring->buffers, slot, |
| 226 | sz, (n_buffers - slot)); |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 227 | virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot)); |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 228 | used -= (n_buffers - slot); |
| 229 | |
| 230 | if (n_left > 0) |
| 231 | { |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 232 | vlib_buffer_free (vm, &vring->buffers[e->id], 1); |
| 233 | vring->buffers[e->id] = ~0; |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 234 | used--; |
| 235 | last++; |
| 236 | n_left--; |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 237 | out_of_order_count++; |
| 238 | vring->flags |= VRING_TX_OUT_OF_ORDER; |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 239 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 240 | } |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * Some vhost-backends give buffers back in out-of-order fashion in used ring. |
| 244 | * It impacts the overall virtio-performance. |
| 245 | */ |
| 246 | if (out_of_order_count) |
| 247 | vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER, |
| 248 | out_of_order_count); |
| 249 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 250 | vring->desc_in_use = used; |
| 251 | vring->last_used_idx = last; |
| 252 | } |
| 253 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 254 | static void |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 255 | virtio_free_used_device_desc_packed (vlib_main_t *vm, |
| 256 | vnet_virtio_vring_t *vring, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 257 | uword node_index) |
| 258 | { |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 259 | vnet_virtio_vring_packed_desc_t *d; |
| 260 | u16 sz = vring->queue_size; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 261 | u16 last = vring->last_used_idx; |
| 262 | u16 n_buffers = 0, start; |
| 263 | u16 flags; |
| 264 | |
| 265 | if (vring->desc_in_use == 0) |
| 266 | return; |
| 267 | |
| 268 | d = &vring->packed_desc[last]; |
| 269 | flags = d->flags; |
| 270 | start = d->id; |
| 271 | |
| 272 | while ((flags & VRING_DESC_F_AVAIL) == (vring->used_wrap_counter << 7) && |
| 273 | (flags & VRING_DESC_F_USED) == (vring->used_wrap_counter << 15)) |
| 274 | { |
| 275 | last++; |
| 276 | n_buffers++; |
| 277 | |
| 278 | if (last >= sz) |
| 279 | { |
| 280 | last = 0; |
| 281 | vring->used_wrap_counter ^= 1; |
| 282 | } |
| 283 | d = &vring->packed_desc[last]; |
| 284 | flags = d->flags; |
| 285 | } |
| 286 | |
| 287 | if (n_buffers) |
| 288 | { |
| 289 | vlib_buffer_free_from_ring (vm, vring->buffers, start, sz, n_buffers); |
| 290 | virtio_memset_ring_u32 (vring->buffers, start, sz, n_buffers); |
| 291 | vring->desc_in_use -= n_buffers; |
| 292 | vring->last_used_idx = last; |
| 293 | } |
| 294 | } |
| 295 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 296 | static void |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 297 | virtio_free_used_device_desc (vlib_main_t *vm, vnet_virtio_vring_t *vring, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 298 | uword node_index, int packed) |
| 299 | { |
| 300 | if (packed) |
| 301 | virtio_free_used_device_desc_packed (vm, vring, node_index); |
| 302 | else |
| 303 | virtio_free_used_device_desc_split (vm, vring, node_index); |
| 304 | |
| 305 | } |
| 306 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 307 | static void |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 308 | set_checksum_offsets (vlib_buffer_t *b, vnet_virtio_net_hdr_v1_t *hdr, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 309 | const int is_l2) |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 310 | { |
Mohsin Kazmi | 36f7a6a | 2021-05-05 14:26:38 +0200 | [diff] [blame] | 311 | vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags; |
Mohsin Kazmi | 1cc592b | 2022-10-17 18:16:57 +0000 | [diff] [blame^] | 312 | i16 l4_hdr_offset = |
| 313 | vnet_buffer (b)->l4_hdr_offset - vnet_buffer (b)->l2_hdr_offset; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 314 | if (b->flags & VNET_BUFFER_F_IS_IP4) |
| 315 | { |
| 316 | ip4_header_t *ip4; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 317 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
Mohsin Kazmi | 1cc592b | 2022-10-17 18:16:57 +0000 | [diff] [blame^] | 318 | hdr->csum_start = l4_hdr_offset; // 0x22; |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 319 | |
| 320 | /* |
| 321 | * virtio devices do not support IP4 checksum offload. So driver takes |
| 322 | * care of it while doing tx. |
| 323 | */ |
Mohsin Kazmi | 096c8cc | 2022-05-18 16:51:57 +0000 | [diff] [blame] | 324 | ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset); |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 325 | if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM) |
| 326 | ip4->checksum = ip4_header_checksum (ip4); |
| 327 | |
| 328 | /* |
| 329 | * virtio devices assume the l4 header is set to the checksum of the |
| 330 | * l3 pseudo-header, so we compute it before tx-ing |
| 331 | */ |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 332 | if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM) |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 333 | { |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 334 | tcp_header_t *tcp = |
Mohsin Kazmi | 096c8cc | 2022-05-18 16:51:57 +0000 | [diff] [blame] | 335 | (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset); |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 336 | tcp->checksum = ip4_pseudo_header_cksum (ip4); |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 337 | hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); |
| 338 | } |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 339 | else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM) |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 340 | { |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 341 | udp_header_t *udp = |
Mohsin Kazmi | 096c8cc | 2022-05-18 16:51:57 +0000 | [diff] [blame] | 342 | (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset); |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 343 | udp->checksum = ip4_pseudo_header_cksum (ip4); |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 344 | hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum); |
| 345 | } |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 346 | } |
| 347 | else if (b->flags & VNET_BUFFER_F_IS_IP6) |
| 348 | { |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 349 | ip6_header_t *ip6; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 350 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
Mohsin Kazmi | 1cc592b | 2022-10-17 18:16:57 +0000 | [diff] [blame^] | 351 | hdr->csum_start = l4_hdr_offset; // 0x36; |
Mohsin Kazmi | 096c8cc | 2022-05-18 16:51:57 +0000 | [diff] [blame] | 352 | ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset); |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * virtio devices assume the l4 header is set to the checksum of the |
| 356 | * l3 pseudo-header, so we compute it before tx-ing |
| 357 | */ |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 358 | if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM) |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 359 | { |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 360 | tcp_header_t *tcp = |
Mohsin Kazmi | 096c8cc | 2022-05-18 16:51:57 +0000 | [diff] [blame] | 361 | (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset); |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 362 | tcp->checksum = ip6_pseudo_header_cksum (ip6); |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 363 | hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); |
| 364 | } |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 365 | else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM) |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 366 | { |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 367 | udp_header_t *udp = |
Mohsin Kazmi | 096c8cc | 2022-05-18 16:51:57 +0000 | [diff] [blame] | 368 | (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset); |
Mohsin Kazmi | f546236 | 2021-02-23 15:55:04 +0100 | [diff] [blame] | 369 | udp->checksum = ip6_pseudo_header_cksum (ip6); |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 370 | hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum); |
| 371 | } |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 375 | static void |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 376 | set_gso_offsets (vlib_buffer_t *b, vnet_virtio_net_hdr_v1_t *hdr, |
| 377 | const int is_l2) |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 378 | { |
Mohsin Kazmi | 36f7a6a | 2021-05-05 14:26:38 +0200 | [diff] [blame] | 379 | vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags; |
Mohsin Kazmi | 1cc592b | 2022-10-17 18:16:57 +0000 | [diff] [blame^] | 380 | i16 l4_hdr_offset = |
| 381 | vnet_buffer (b)->l4_hdr_offset - vnet_buffer (b)->l2_hdr_offset; |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 382 | |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 383 | if (b->flags & VNET_BUFFER_F_IS_IP4) |
| 384 | { |
| 385 | ip4_header_t *ip4; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 386 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 387 | hdr->gso_size = vnet_buffer2 (b)->gso_size; |
Mohsin Kazmi | 1cc592b | 2022-10-17 18:16:57 +0000 | [diff] [blame^] | 388 | hdr->hdr_len = l4_hdr_offset + vnet_buffer2 (b)->gso_l4_hdr_sz; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 389 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
Mohsin Kazmi | 1cc592b | 2022-10-17 18:16:57 +0000 | [diff] [blame^] | 390 | hdr->csum_start = l4_hdr_offset; // 0x22; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 391 | hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); |
Mohsin Kazmi | 096c8cc | 2022-05-18 16:51:57 +0000 | [diff] [blame] | 392 | ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 393 | /* |
| 394 | * virtio devices do not support IP4 checksum offload. So driver takes care |
| 395 | * of it while doing tx. |
| 396 | */ |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 397 | if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM) |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 398 | ip4->checksum = ip4_header_checksum (ip4); |
| 399 | } |
| 400 | else if (b->flags & VNET_BUFFER_F_IS_IP6) |
| 401 | { |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 402 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
| 403 | hdr->gso_size = vnet_buffer2 (b)->gso_size; |
Mohsin Kazmi | 1cc592b | 2022-10-17 18:16:57 +0000 | [diff] [blame^] | 404 | hdr->hdr_len = l4_hdr_offset + vnet_buffer2 (b)->gso_l4_hdr_sz; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 405 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
Mohsin Kazmi | 1cc592b | 2022-10-17 18:16:57 +0000 | [diff] [blame^] | 406 | hdr->csum_start = l4_hdr_offset; // 0x36; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 407 | hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); |
| 408 | } |
| 409 | } |
| 410 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 411 | static u16 |
| 412 | add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node, |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 413 | virtio_if_t *vif, vnet_virtio_vring_t *vring, u32 bi, |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 414 | u16 free_desc_count, u16 avail, u16 next, u16 mask, |
| 415 | int hdr_sz, int do_gso, int csum_offload, int is_pci, |
| 416 | int is_tun, int is_indirect, int is_any_layout) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 417 | { |
| 418 | u16 n_added = 0; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 419 | vnet_virtio_vring_desc_t *d; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 420 | int is_l2 = !is_tun; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 421 | d = &vring->desc[next]; |
| 422 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 423 | vnet_virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 424 | u32 drop_inline = ~0; |
Damjan Marion | 508cabc | 2018-02-08 19:49:22 +0100 | [diff] [blame] | 425 | |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 426 | clib_memset_u8 (hdr, 0, hdr_sz); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 427 | |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 428 | vring->total_packets++; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 429 | if (b->flags & VNET_BUFFER_F_GSO) |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 430 | { |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 431 | if (do_gso) |
Mohsin Kazmi | 84f91fa | 2020-04-23 17:59:49 +0200 | [diff] [blame] | 432 | set_gso_offsets (b, hdr, is_l2); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 433 | else |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 434 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 435 | drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP; |
| 436 | goto done; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 437 | } |
| 438 | } |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 439 | else if (b->flags & VNET_BUFFER_F_OFFLOAD) |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 440 | { |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 441 | if (csum_offload) |
Mohsin Kazmi | 84f91fa | 2020-04-23 17:59:49 +0200 | [diff] [blame] | 442 | set_checksum_offsets (b, hdr, is_l2); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 443 | else |
| 444 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 445 | drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP; |
| 446 | goto done; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 447 | } |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 448 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 449 | |
Mohsin Kazmi | 8314371 | 2020-10-07 13:23:55 +0200 | [diff] [blame] | 450 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 451 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 452 | virtio_tx_trace (vm, node, b, bi, is_tun); |
Mohsin Kazmi | 8314371 | 2020-10-07 13:23:55 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 455 | if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0)) |
| 456 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 457 | d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) : |
| 458 | pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 459 | d->len = b->current_length + hdr_sz; |
| 460 | d->flags = 0; |
| 461 | } |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 462 | else if (is_indirect) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 463 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 464 | /* |
| 465 | * We are using single vlib_buffer_t for indirect descriptor(s) |
| 466 | * chain. Single descriptor is 16 bytes and vlib_buffer_t |
| 467 | * has 2048 bytes space. So maximum long chain can have 128 |
| 468 | * (=2048/16) indirect descriptors. |
| 469 | * It can easily support 65535 bytes of Jumbo frames with |
| 470 | * each data buffer size of 512 bytes minimum. |
| 471 | */ |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 472 | u32 indirect_buffer = 0; |
| 473 | if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0)) |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 474 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 475 | drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED; |
| 476 | goto done; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 477 | } |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 478 | |
| 479 | vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 480 | indirect_desc->current_data = 0; |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 481 | indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 482 | indirect_desc->next_buffer = bi; |
| 483 | bi = indirect_buffer; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 484 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 485 | vnet_virtio_vring_desc_t *id = |
| 486 | (vnet_virtio_vring_desc_t *) vlib_buffer_get_current (indirect_desc); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 487 | u32 count = 1; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 488 | if (is_pci) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 489 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 490 | d->addr = vlib_physmem_get_pa (vm, id); |
| 491 | id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 492 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 493 | /* |
| 494 | * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr |
| 495 | * should be presented in separate descriptor and data will start |
| 496 | * from next descriptor. |
| 497 | */ |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 498 | if (is_any_layout) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 499 | id->len = b->current_length + hdr_sz; |
| 500 | else |
| 501 | { |
| 502 | id->len = hdr_sz; |
| 503 | id->flags = VRING_DESC_F_NEXT; |
| 504 | id->next = count; |
| 505 | count++; |
| 506 | id++; |
| 507 | id->addr = vlib_buffer_get_current_pa (vm, b); |
| 508 | id->len = b->current_length; |
| 509 | } |
| 510 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 511 | { |
| 512 | id->flags = VRING_DESC_F_NEXT; |
| 513 | id->next = count; |
| 514 | count++; |
| 515 | id++; |
| 516 | b = vlib_get_buffer (vm, b->next_buffer); |
| 517 | id->addr = vlib_buffer_get_current_pa (vm, b); |
| 518 | id->len = b->current_length; |
Mohsin Kazmi | 637c97c | 2021-05-12 14:51:29 +0200 | [diff] [blame] | 519 | if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN)) |
| 520 | { |
| 521 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 522 | vlib_error_count (vm, node->node_index, |
| 523 | VIRTIO_TX_ERROR_TRUNC_PACKET, 1); |
| 524 | break; |
| 525 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 526 | } |
| 527 | } |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 528 | else /* VIRTIO_IF_TYPE_[TAP | TUN] */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 529 | { |
| 530 | d->addr = pointer_to_uword (id); |
| 531 | /* first buffer in chain */ |
| 532 | id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz; |
| 533 | id->len = b->current_length + hdr_sz; |
| 534 | |
| 535 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 536 | { |
| 537 | id->flags = VRING_DESC_F_NEXT; |
| 538 | id->next = count; |
| 539 | count++; |
| 540 | id++; |
| 541 | b = vlib_get_buffer (vm, b->next_buffer); |
| 542 | id->addr = pointer_to_uword (vlib_buffer_get_current (b)); |
| 543 | id->len = b->current_length; |
Mohsin Kazmi | 637c97c | 2021-05-12 14:51:29 +0200 | [diff] [blame] | 544 | if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN)) |
| 545 | { |
| 546 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 547 | vlib_error_count (vm, node->node_index, |
| 548 | VIRTIO_TX_ERROR_TRUNC_PACKET, 1); |
| 549 | break; |
| 550 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | id->flags = 0; |
| 554 | id->next = 0; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 555 | d->len = count * sizeof (vnet_virtio_vring_desc_t); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 556 | d->flags = VRING_DESC_F_INDIRECT; |
| 557 | } |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 558 | else if (is_pci) |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 559 | { |
| 560 | u16 count = next; |
| 561 | vlib_buffer_t *b_temp = b; |
| 562 | u16 n_buffers_in_chain = 1; |
| 563 | |
| 564 | /* |
| 565 | * Check the length of the chain for the required number of |
| 566 | * descriptors. Return from here, retry to get more descriptors, |
| 567 | * if chain length is greater than available descriptors. |
| 568 | */ |
| 569 | while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 570 | { |
| 571 | n_buffers_in_chain++; |
| 572 | b_temp = vlib_get_buffer (vm, b_temp->next_buffer); |
| 573 | } |
| 574 | |
| 575 | if (n_buffers_in_chain > free_desc_count) |
| 576 | return n_buffers_in_chain; |
| 577 | |
| 578 | d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz; |
| 579 | d->len = b->current_length + hdr_sz; |
| 580 | |
| 581 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 582 | { |
| 583 | d->flags = VRING_DESC_F_NEXT; |
| 584 | vring->buffers[count] = bi; |
| 585 | b->flags &= |
| 586 | ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID); |
| 587 | bi = b->next_buffer; |
| 588 | b->next_buffer = 0; |
| 589 | n_added++; |
| 590 | count = (count + 1) & mask; |
| 591 | d->next = count; |
| 592 | d = &vring->desc[count]; |
| 593 | b = vlib_get_buffer (vm, bi); |
| 594 | d->addr = vlib_buffer_get_current_pa (vm, b); |
| 595 | d->len = b->current_length; |
| 596 | } |
| 597 | d->flags = 0; |
| 598 | vring->buffers[count] = bi; |
| 599 | vring->avail->ring[avail & mask] = next; |
| 600 | n_added++; |
| 601 | return n_added; |
| 602 | } |
| 603 | else |
| 604 | { |
| 605 | ASSERT (0); |
| 606 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 607 | vring->buffers[next] = bi; |
| 608 | vring->avail->ring[avail & mask] = next; |
| 609 | n_added++; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 610 | |
| 611 | done: |
| 612 | if (drop_inline != ~0) |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 613 | virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1, |
| 614 | drop_inline); |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 615 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 616 | return n_added; |
| 617 | } |
| 618 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 619 | static u16 |
| 620 | add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node, |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 621 | virtio_if_t *vif, vnet_virtio_vring_t *vring, |
| 622 | u32 bi, u16 next, int hdr_sz, int do_gso, |
| 623 | int csum_offload, int is_pci, int is_tun, |
| 624 | int is_indirect, int is_any_layout) |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 625 | { |
| 626 | u16 n_added = 0, flags = 0; |
| 627 | int is_l2 = !is_tun; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 628 | vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[next]; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 629 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 630 | vnet_virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 631 | u32 drop_inline = ~0; |
| 632 | |
| 633 | clib_memset (hdr, 0, hdr_sz); |
| 634 | |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 635 | vring->total_packets++; |
| 636 | |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 637 | if (b->flags & VNET_BUFFER_F_GSO) |
| 638 | { |
| 639 | if (do_gso) |
| 640 | set_gso_offsets (b, hdr, is_l2); |
| 641 | else |
| 642 | { |
| 643 | drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP; |
| 644 | goto done; |
| 645 | } |
| 646 | } |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 647 | else if (b->flags & VNET_BUFFER_F_OFFLOAD) |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 648 | { |
| 649 | if (csum_offload) |
| 650 | set_checksum_offsets (b, hdr, is_l2); |
| 651 | else |
| 652 | { |
| 653 | drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP; |
| 654 | goto done; |
| 655 | } |
| 656 | } |
| 657 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 658 | { |
| 659 | virtio_tx_trace (vm, node, b, bi, is_tun); |
| 660 | } |
| 661 | |
| 662 | if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0)) |
| 663 | { |
| 664 | d->addr = |
| 665 | ((is_pci) ? vlib_buffer_get_current_pa (vm, |
| 666 | b) : |
| 667 | pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz; |
| 668 | d->len = b->current_length + hdr_sz; |
| 669 | } |
| 670 | else if (is_indirect) |
| 671 | { |
| 672 | /* |
| 673 | * We are using single vlib_buffer_t for indirect descriptor(s) |
| 674 | * chain. Single descriptor is 16 bytes and vlib_buffer_t |
| 675 | * has 2048 bytes space. So maximum long chain can have 128 |
| 676 | * (=2048/16) indirect descriptors. |
| 677 | * It can easily support 65535 bytes of Jumbo frames with |
| 678 | * each data buffer size of 512 bytes minimum. |
| 679 | */ |
| 680 | u32 indirect_buffer = 0; |
| 681 | if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0)) |
| 682 | { |
| 683 | drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED; |
| 684 | goto done; |
| 685 | } |
| 686 | |
| 687 | vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer); |
| 688 | indirect_desc->current_data = 0; |
| 689 | indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 690 | indirect_desc->next_buffer = bi; |
| 691 | bi = indirect_buffer; |
| 692 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 693 | vnet_virtio_vring_packed_desc_t *id = |
| 694 | (vnet_virtio_vring_packed_desc_t *) vlib_buffer_get_current ( |
| 695 | indirect_desc); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 696 | u32 count = 1; |
| 697 | if (is_pci) |
| 698 | { |
| 699 | d->addr = vlib_physmem_get_pa (vm, id); |
| 700 | id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz; |
| 701 | |
| 702 | /* |
| 703 | * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr |
| 704 | * should be presented in separate descriptor and data will start |
| 705 | * from next descriptor. |
| 706 | */ |
| 707 | if (is_any_layout) |
| 708 | id->len = b->current_length + hdr_sz; |
| 709 | else |
| 710 | { |
| 711 | id->len = hdr_sz; |
| 712 | id->flags = 0; |
| 713 | id->id = 0; |
| 714 | count++; |
| 715 | id++; |
| 716 | id->addr = vlib_buffer_get_current_pa (vm, b); |
| 717 | id->len = b->current_length; |
| 718 | } |
| 719 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 720 | { |
| 721 | id->flags = 0; |
| 722 | id->id = 0; |
| 723 | count++; |
| 724 | id++; |
| 725 | b = vlib_get_buffer (vm, b->next_buffer); |
| 726 | id->addr = vlib_buffer_get_current_pa (vm, b); |
| 727 | id->len = b->current_length; |
Mohsin Kazmi | 637c97c | 2021-05-12 14:51:29 +0200 | [diff] [blame] | 728 | if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN)) |
| 729 | { |
| 730 | if (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 731 | vlib_error_count (vm, node->node_index, |
| 732 | VIRTIO_TX_ERROR_TRUNC_PACKET, 1); |
| 733 | break; |
| 734 | } |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | id->flags = 0; |
| 738 | id->id = 0; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 739 | d->len = count * sizeof (vnet_virtio_vring_packed_desc_t); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 740 | flags = VRING_DESC_F_INDIRECT; |
| 741 | } |
| 742 | else |
| 743 | { |
| 744 | ASSERT (0); |
| 745 | } |
| 746 | if (vring->avail_wrap_counter) |
| 747 | { |
| 748 | flags |= VRING_DESC_F_AVAIL; |
| 749 | flags &= ~VRING_DESC_F_USED; |
| 750 | } |
| 751 | else |
| 752 | { |
| 753 | flags &= ~VRING_DESC_F_AVAIL; |
| 754 | flags |= VRING_DESC_F_USED; |
| 755 | } |
| 756 | |
| 757 | d->id = next; |
| 758 | d->flags = flags; |
| 759 | vring->buffers[next] = bi; |
| 760 | n_added++; |
| 761 | |
| 762 | done: |
| 763 | if (drop_inline != ~0) |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 764 | virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1, |
| 765 | drop_inline); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 766 | |
| 767 | return n_added; |
| 768 | } |
| 769 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 770 | static uword |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 771 | virtio_interface_tx_packed_gso_inline ( |
| 772 | vlib_main_t *vm, vlib_node_runtime_t *node, virtio_if_t *vif, |
| 773 | virtio_if_type_t type, vnet_virtio_vring_t *vring, u32 *buffers, u16 n_left, |
| 774 | const int do_gso, const int csum_offload) |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 775 | { |
| 776 | int is_pci = (type == VIRTIO_IF_TYPE_PCI); |
| 777 | int is_tun = (type == VIRTIO_IF_TYPE_TUN); |
| 778 | int is_indirect = |
| 779 | ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0); |
| 780 | int is_any_layout = |
| 781 | ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0); |
| 782 | const int hdr_sz = vif->virtio_net_hdr_sz; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 783 | u16 sz = vring->queue_size; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 784 | u16 used, next, n_buffers = 0, n_buffers_left = 0; |
| 785 | u16 n_vectors = n_left; |
| 786 | |
| 787 | |
| 788 | used = vring->desc_in_use; |
| 789 | next = vring->desc_next; |
| 790 | |
| 791 | if (vif->packet_buffering) |
| 792 | { |
| 793 | n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering); |
| 794 | |
| 795 | while (n_buffers_left && used < sz) |
| 796 | { |
| 797 | u16 n_added = 0; |
| 798 | |
| 799 | u32 bi = virtio_vring_buffering_read_from_front (vring->buffering); |
| 800 | if (bi == ~0) |
| 801 | break; |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 802 | n_added = add_buffer_to_slot_packed ( |
| 803 | vm, node, vif, vring, bi, next, hdr_sz, do_gso, csum_offload, |
| 804 | is_pci, is_tun, is_indirect, is_any_layout); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 805 | n_buffers_left--; |
| 806 | if (PREDICT_FALSE (n_added == 0)) |
| 807 | continue; |
| 808 | |
| 809 | used++; |
| 810 | next++; |
| 811 | if (next >= sz) |
| 812 | { |
| 813 | next = 0; |
| 814 | vring->avail_wrap_counter ^= 1; |
| 815 | } |
| 816 | } |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 817 | virtio_txq_clear_scheduled (vring); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | while (n_left && used < sz) |
| 821 | { |
| 822 | u16 n_added = 0; |
| 823 | |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 824 | n_added = add_buffer_to_slot_packed ( |
| 825 | vm, node, vif, vring, buffers[0], next, hdr_sz, do_gso, csum_offload, |
| 826 | is_pci, is_tun, is_indirect, is_any_layout); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 827 | buffers++; |
| 828 | n_left--; |
| 829 | if (PREDICT_FALSE (n_added == 0)) |
| 830 | continue; |
| 831 | |
| 832 | used++; |
| 833 | next++; |
| 834 | if (next >= sz) |
| 835 | { |
| 836 | next = 0; |
| 837 | vring->avail_wrap_counter ^= 1; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | if (n_left != n_vectors || n_buffers != n_buffers_left) |
| 842 | { |
| 843 | CLIB_MEMORY_STORE_BARRIER (); |
| 844 | vring->desc_next = next; |
| 845 | vring->desc_in_use = used; |
| 846 | CLIB_MEMORY_BARRIER (); |
| 847 | if (vring->device_event->flags != VRING_EVENT_F_DISABLE) |
| 848 | virtio_kick (vm, vring, vif); |
| 849 | } |
| 850 | |
| 851 | return n_left; |
| 852 | } |
| 853 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 854 | static void |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 855 | virtio_find_free_desc (vnet_virtio_vring_t *vring, u16 size, u16 mask, u16 req, |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 856 | u16 next, u32 *first_free_desc_index, |
| 857 | u16 *free_desc_count) |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 858 | { |
| 859 | u16 start = 0; |
| 860 | /* next is used as hint: from where to start looking */ |
| 861 | for (u16 i = 0; i < size; i++, next++) |
| 862 | { |
| 863 | if (vring->buffers[next & mask] == ~0) |
| 864 | { |
| 865 | if (*first_free_desc_index == ~0) |
| 866 | { |
| 867 | *first_free_desc_index = (next & mask); |
| 868 | start = i; |
| 869 | (*free_desc_count)++; |
| 870 | req--; |
| 871 | if (req == 0) |
| 872 | break; |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | if (start + *free_desc_count == i) |
| 877 | { |
| 878 | (*free_desc_count)++; |
| 879 | req--; |
| 880 | if (req == 0) |
| 881 | break; |
| 882 | } |
| 883 | else |
| 884 | break; |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 890 | static u16 |
| 891 | virtio_interface_tx_split_gso_inline (vlib_main_t *vm, |
| 892 | vlib_node_runtime_t *node, |
| 893 | virtio_if_t *vif, virtio_if_type_t type, |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 894 | vnet_virtio_vring_t *vring, u32 *buffers, |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 895 | u16 n_left, int do_gso, int csum_offload) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 896 | { |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 897 | u16 used, next, avail, n_buffers = 0, n_buffers_left = 0; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 898 | int is_pci = (type == VIRTIO_IF_TYPE_PCI); |
| 899 | int is_tun = (type == VIRTIO_IF_TYPE_TUN); |
| 900 | int is_indirect = |
| 901 | ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0); |
| 902 | int is_any_layout = |
| 903 | ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 904 | u16 sz = vring->queue_size; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 905 | int hdr_sz = vif->virtio_net_hdr_sz; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 906 | u16 mask = sz - 1; |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 907 | u16 n_vectors = n_left; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 908 | |
| 909 | used = vring->desc_in_use; |
| 910 | next = vring->desc_next; |
| 911 | avail = vring->avail->idx; |
| 912 | |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 913 | u16 free_desc_count = 0; |
| 914 | |
| 915 | if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER)) |
| 916 | { |
| 917 | u32 first_free_desc_index = ~0; |
| 918 | |
| 919 | virtio_find_free_desc (vring, sz, mask, n_left, next, |
| 920 | &first_free_desc_index, &free_desc_count); |
| 921 | |
| 922 | if (free_desc_count) |
| 923 | next = first_free_desc_index; |
| 924 | } |
| 925 | else |
| 926 | free_desc_count = sz - used; |
| 927 | |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 928 | if (vif->packet_buffering) |
| 929 | { |
| 930 | n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering); |
| 931 | |
| 932 | while (n_buffers_left && free_desc_count) |
| 933 | { |
| 934 | u16 n_added = 0; |
| 935 | |
| 936 | u32 bi = virtio_vring_buffering_read_from_front (vring->buffering); |
| 937 | if (bi == ~0) |
| 938 | break; |
Mohsin Kazmi | 8314371 | 2020-10-07 13:23:55 +0200 | [diff] [blame] | 939 | |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 940 | n_added = add_buffer_to_slot (vm, node, vif, vring, bi, |
| 941 | free_desc_count, avail, next, mask, |
| 942 | hdr_sz, do_gso, csum_offload, is_pci, |
| 943 | is_tun, is_indirect, is_any_layout); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 944 | if (PREDICT_FALSE (n_added == 0)) |
| 945 | { |
| 946 | n_buffers_left--; |
| 947 | continue; |
| 948 | } |
| 949 | else if (PREDICT_FALSE (n_added > free_desc_count)) |
| 950 | break; |
| 951 | |
| 952 | avail++; |
| 953 | next = (next + n_added) & mask; |
| 954 | used += n_added; |
| 955 | n_buffers_left--; |
| 956 | free_desc_count -= n_added; |
| 957 | } |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 958 | virtio_txq_clear_scheduled (vring); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 961 | while (n_left && free_desc_count) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 962 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 963 | u16 n_added = 0; |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 964 | |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 965 | n_added = |
| 966 | add_buffer_to_slot (vm, node, vif, vring, buffers[0], free_desc_count, |
| 967 | avail, next, mask, hdr_sz, do_gso, csum_offload, |
| 968 | is_pci, is_tun, is_indirect, is_any_layout); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 969 | |
| 970 | if (PREDICT_FALSE (n_added == 0)) |
| 971 | { |
| 972 | buffers++; |
| 973 | n_left--; |
| 974 | continue; |
| 975 | } |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 976 | else if (PREDICT_FALSE (n_added > free_desc_count)) |
| 977 | break; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 978 | |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 979 | avail++; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 980 | next = (next + n_added) & mask; |
| 981 | used += n_added; |
| 982 | buffers++; |
| 983 | n_left--; |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 984 | free_desc_count -= n_added; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 985 | } |
| 986 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 987 | if (n_left != n_vectors || n_buffers != n_buffers_left) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 988 | { |
Mohsin Kazmi | d8b7dec | 2020-12-10 09:49:21 +0000 | [diff] [blame] | 989 | clib_atomic_store_seq_cst (&vring->avail->idx, avail); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 990 | vring->desc_next = next; |
| 991 | vring->desc_in_use = used; |
Mohsin Kazmi | d8b7dec | 2020-12-10 09:49:21 +0000 | [diff] [blame] | 992 | if ((clib_atomic_load_seq_cst (&vring->used->flags) & |
| 993 | VRING_USED_F_NO_NOTIFY) == 0) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 994 | virtio_kick (vm, vring, vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 997 | return n_left; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 1000 | static u16 |
| 1001 | virtio_interface_tx_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 1002 | virtio_if_t *vif, virtio_if_type_t type, |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 1003 | vnet_virtio_vring_t *vring, u32 *buffers, |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 1004 | u16 n_left, int packed, int do_gso, |
| 1005 | int csum_offload) |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1006 | { |
| 1007 | if (packed) |
| 1008 | return virtio_interface_tx_packed_gso_inline (vm, node, vif, type, vring, |
| 1009 | buffers, n_left, |
Mohsin Kazmi | b18b190 | 2020-12-03 16:35:20 +0100 | [diff] [blame] | 1010 | do_gso, csum_offload); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1011 | else |
| 1012 | return virtio_interface_tx_split_gso_inline (vm, node, vif, type, vring, |
| 1013 | buffers, n_left, |
Mohsin Kazmi | b18b190 | 2020-12-03 16:35:20 +0100 | [diff] [blame] | 1014 | do_gso, csum_offload); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 1017 | static u16 |
| 1018 | virtio_interface_tx_inline (vlib_main_t *vm, vlib_node_runtime_t *node, |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 1019 | virtio_if_t *vif, vnet_virtio_vring_t *vring, |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 1020 | virtio_if_type_t type, u32 *buffers, u16 n_left, |
| 1021 | int packed) |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1022 | { |
| 1023 | vnet_main_t *vnm = vnet_get_main (); |
| 1024 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 1025 | |
Damjan Marion | d4f88cc | 2022-01-05 01:52:38 +0100 | [diff] [blame] | 1026 | if (hw->caps & VNET_HW_IF_CAP_TCP_GSO) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1027 | return virtio_interface_tx_gso_inline (vm, node, vif, type, vring, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1028 | buffers, n_left, packed, |
| 1029 | 1 /* do_gso */ , |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 1030 | 1 /* checksum offload */ ); |
Damjan Marion | d4f88cc | 2022-01-05 01:52:38 +0100 | [diff] [blame] | 1031 | else if (hw->caps & VNET_HW_IF_CAP_L4_TX_CKSUM) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1032 | return virtio_interface_tx_gso_inline (vm, node, vif, type, vring, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1033 | buffers, n_left, packed, |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1034 | 0 /* no do_gso */ , |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 1035 | 1 /* checksum offload */ ); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1036 | else |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1037 | return virtio_interface_tx_gso_inline (vm, node, vif, type, vring, |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1038 | buffers, n_left, packed, |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1039 | 0 /* no do_gso */ , |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 1040 | 0 /* no checksum offload */ ); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
Filip Tehlar | aee7364 | 2019-03-13 05:50:44 -0700 | [diff] [blame] | 1043 | VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm, |
| 1044 | vlib_node_runtime_t * node, |
| 1045 | vlib_frame_t * frame) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1046 | { |
| 1047 | virtio_main_t *nm = &virtio_main; |
| 1048 | vnet_interface_output_runtime_t *rund = (void *) node->runtime_data; |
| 1049 | virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance); |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 1050 | vnet_hw_if_tx_frame_t *tf = vlib_frame_scalar_args (frame); |
| 1051 | u16 qid = tf->queue_id; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 1052 | vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid); |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 1053 | u16 n_left = frame->n_vectors; |
| 1054 | u32 *buffers = vlib_frame_vector_args (frame); |
| 1055 | u32 to[GRO_TO_VECTOR_SIZE (n_left)]; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1056 | int packed = vif->is_packed; |
Mohsin Kazmi | e2e9fe5 | 2020-12-17 15:54:03 +0100 | [diff] [blame] | 1057 | u16 n_vectors = frame->n_vectors; |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 1058 | |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 1059 | if (tf->shared_queue) |
| 1060 | clib_spinlock_lock (&vring->lockp); |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 1061 | |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 1062 | if (vif->packet_coalesce) |
| 1063 | { |
Mohsin Kazmi | e2e9fe5 | 2020-12-17 15:54:03 +0100 | [diff] [blame] | 1064 | n_vectors = n_left = |
| 1065 | vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to); |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 1066 | buffers = to; |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 1067 | virtio_txq_clear_scheduled (vring); |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Damjan Marion | 587f913 | 2020-10-31 22:47:01 +0100 | [diff] [blame] | 1070 | u16 retry_count = 2; |
| 1071 | |
| 1072 | retry: |
| 1073 | /* free consumed buffers */ |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1074 | virtio_free_used_device_desc (vm, vring, node->node_index, packed); |
Damjan Marion | 587f913 | 2020-10-31 22:47:01 +0100 | [diff] [blame] | 1075 | |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1076 | if (vif->type == VIRTIO_IF_TYPE_TAP) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1077 | n_left = virtio_interface_tx_inline (vm, node, vif, vring, |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 1078 | VIRTIO_IF_TYPE_TAP, |
Mohsin Kazmi | e2e9fe5 | 2020-12-17 15:54:03 +0100 | [diff] [blame] | 1079 | &buffers[n_vectors - n_left], |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1080 | n_left, packed); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1081 | else if (vif->type == VIRTIO_IF_TYPE_PCI) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1082 | n_left = virtio_interface_tx_inline (vm, node, vif, vring, |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 1083 | VIRTIO_IF_TYPE_PCI, |
Mohsin Kazmi | e2e9fe5 | 2020-12-17 15:54:03 +0100 | [diff] [blame] | 1084 | &buffers[n_vectors - n_left], |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1085 | n_left, packed); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1086 | else if (vif->type == VIRTIO_IF_TYPE_TUN) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1087 | n_left = virtio_interface_tx_inline (vm, node, vif, vring, |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 1088 | VIRTIO_IF_TYPE_TUN, |
Mohsin Kazmi | e2e9fe5 | 2020-12-17 15:54:03 +0100 | [diff] [blame] | 1089 | &buffers[n_vectors - n_left], |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1090 | n_left, packed); |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 1091 | else |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 1092 | ASSERT (0); |
| 1093 | |
Damjan Marion | 587f913 | 2020-10-31 22:47:01 +0100 | [diff] [blame] | 1094 | if (n_left && retry_count--) |
| 1095 | goto retry; |
| 1096 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1097 | if (vif->packet_buffering && n_left) |
| 1098 | { |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 1099 | u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering, |
| 1100 | &buffers |
Mohsin Kazmi | e2e9fe5 | 2020-12-17 15:54:03 +0100 | [diff] [blame] | 1101 | [n_vectors |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 1102 | - n_left], |
| 1103 | n_left); |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1104 | n_left -= n_buffered; |
| 1105 | } |
| 1106 | if (n_left) |
Mohsin Kazmi | a02ad34 | 2021-05-04 16:36:09 +0200 | [diff] [blame] | 1107 | virtio_interface_drop_inline (vm, vif, node->node_index, |
Mohsin Kazmi | e2e9fe5 | 2020-12-17 15:54:03 +0100 | [diff] [blame] | 1108 | &buffers[n_vectors - n_left], n_left, |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1109 | VIRTIO_TX_ERROR_NO_FREE_SLOTS); |
| 1110 | |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 1111 | if (tf->shared_queue) |
| 1112 | clib_spinlock_unlock (&vring->lockp); |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 1113 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 1114 | return frame->n_vectors - n_left; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static void |
| 1118 | virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index, |
| 1119 | u32 node_index) |
| 1120 | { |
| 1121 | virtio_main_t *apm = &virtio_main; |
| 1122 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 1123 | virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance); |
| 1124 | |
| 1125 | /* Shut off redirection */ |
| 1126 | if (node_index == ~0) |
| 1127 | { |
| 1128 | vif->per_interface_next_index = node_index; |
| 1129 | return; |
| 1130 | } |
| 1131 | |
| 1132 | vif->per_interface_next_index = |
| 1133 | vlib_node_add_next (vlib_get_main (), virtio_input_node.index, |
| 1134 | node_index); |
| 1135 | } |
| 1136 | |
| 1137 | static void |
| 1138 | virtio_clear_hw_interface_counters (u32 instance) |
| 1139 | { |
| 1140 | /* Nothing for now */ |
| 1141 | } |
| 1142 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 1143 | static void |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 1144 | virtio_set_rx_interrupt (virtio_if_t *vif, vnet_virtio_vring_t *vring) |
Mohsin Kazmi | 2484186 | 2020-12-02 16:39:42 +0100 | [diff] [blame] | 1145 | { |
| 1146 | if (vif->is_packed) |
| 1147 | vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE; |
| 1148 | else |
| 1149 | vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; |
| 1150 | } |
| 1151 | |
Damjan Marion | 809d5c9 | 2021-03-24 12:24:58 +0100 | [diff] [blame] | 1152 | static void |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 1153 | virtio_set_rx_polling (virtio_if_t *vif, vnet_virtio_vring_t *vring) |
Mohsin Kazmi | 2484186 | 2020-12-02 16:39:42 +0100 | [diff] [blame] | 1154 | { |
| 1155 | if (vif->is_packed) |
| 1156 | vring->driver_event->flags |= VRING_EVENT_F_DISABLE; |
| 1157 | else |
| 1158 | vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; |
| 1159 | } |
| 1160 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1161 | static clib_error_t * |
| 1162 | virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid, |
Damjan Marion | eabd424 | 2020-10-07 20:59:07 +0200 | [diff] [blame] | 1163 | vnet_hw_if_rx_mode mode) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1164 | { |
| 1165 | virtio_main_t *mm = &virtio_main; |
| 1166 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 1167 | virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 1168 | vnet_virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1169 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1170 | if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode)) |
| 1171 | { |
Mohsin Kazmi | 2484186 | 2020-12-02 16:39:42 +0100 | [diff] [blame] | 1172 | virtio_set_rx_polling (vif, rx_vring); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1173 | return clib_error_return (0, "interrupt mode is not supported"); |
| 1174 | } |
| 1175 | |
Damjan Marion | eabd424 | 2020-10-07 20:59:07 +0200 | [diff] [blame] | 1176 | if (mode == VNET_HW_IF_RX_MODE_POLLING) |
Mohsin Kazmi | 2484186 | 2020-12-02 16:39:42 +0100 | [diff] [blame] | 1177 | virtio_set_rx_polling (vif, rx_vring); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1178 | else |
Mohsin Kazmi | 2484186 | 2020-12-02 16:39:42 +0100 | [diff] [blame] | 1179 | virtio_set_rx_interrupt (vif, rx_vring); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1180 | |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 1181 | rx_vring->mode = mode; |
| 1182 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | static clib_error_t * |
| 1187 | virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 1188 | { |
| 1189 | virtio_main_t *mm = &virtio_main; |
| 1190 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 1191 | virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1192 | |
| 1193 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
Mohsin Kazmi | 529f0e9 | 2020-03-19 14:03:31 +0100 | [diff] [blame] | 1194 | { |
| 1195 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 1196 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 1197 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 1198 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1199 | else |
Mohsin Kazmi | 529f0e9 | 2020-03-19 14:03:31 +0100 | [diff] [blame] | 1200 | { |
| 1201 | vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP; |
| 1202 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 1203 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | static clib_error_t * |
| 1208 | virtio_subif_add_del_function (vnet_main_t * vnm, |
| 1209 | u32 hw_if_index, |
| 1210 | struct vnet_sw_interface_t *st, int is_add) |
| 1211 | { |
| 1212 | /* Nothing for now */ |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | /* *INDENT-OFF* */ |
| 1217 | VNET_DEVICE_CLASS (virtio_device_class) = { |
| 1218 | .name = "virtio", |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1219 | .format_device_name = format_virtio_device_name, |
| 1220 | .format_device = format_virtio_device, |
| 1221 | .format_tx_trace = format_virtio_tx_trace, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1222 | .tx_function_n_errors = VIRTIO_TX_N_ERROR, |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1223 | .tx_function_error_strings = virtio_tx_func_error_strings, |
| 1224 | .rx_redirect_to_node = virtio_set_interface_next_node, |
| 1225 | .clear_counters = virtio_clear_hw_interface_counters, |
| 1226 | .admin_up_down_function = virtio_interface_admin_up_down, |
| 1227 | .subif_add_del_function = virtio_subif_add_del_function, |
| 1228 | .rx_mode_change_function = virtio_interface_rx_mode_change, |
| 1229 | }; |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 1230 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1231 | /* *INDENT-ON* */ |
| 1232 | |
| 1233 | /* |
| 1234 | * fd.io coding-style-patch-verification: ON |
| 1235 | * |
| 1236 | * Local Variables: |
| 1237 | * eval: (c-set-style "gnu") |
| 1238 | * End: |
| 1239 | */ |