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