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