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