Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <fcntl.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 21 | |
| 22 | #include <vlib/vlib.h> |
| 23 | #include <vlib/unix/unix.h> |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 24 | #include <vnet/vnet.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 25 | #include <vnet/ethernet/ethernet.h> |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 26 | #include <vnet/gso/gro_func.h> |
Mohsin Kazmi | 0b04209 | 2020-04-17 16:50:56 +0000 | [diff] [blame] | 27 | #include <vnet/gso/hdr_offset_parser.h> |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 28 | #include <vnet/ip/ip4_packet.h> |
| 29 | #include <vnet/ip/ip6_packet.h> |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 30 | #include <vnet/tcp/tcp_packet.h> |
| 31 | #include <vnet/udp/udp_packet.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 32 | #include <vnet/devices/virtio/virtio.h> |
| 33 | |
| 34 | #define foreach_virtio_tx_func_error \ |
| 35 | _(NO_FREE_SLOTS, "no free tx slots") \ |
| 36 | _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \ |
| 37 | _(PENDING_MSGS, "pending msgs in tx ring") \ |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 38 | _(INDIRECT_DESC_ALLOC_FAILED, "indirect descriptor allocation failed - packet drop") \ |
| 39 | _(OUT_OF_ORDER, "out-of-order buffers in used ring") \ |
| 40 | _(GSO_PACKET_DROP, "gso disabled on itf -- gso packet drop") \ |
| 41 | _(CSUM_OFFLOAD_PACKET_DROP, "checksum offload disabled on itf -- csum offload packet drop") |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 42 | |
| 43 | typedef enum |
| 44 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 45 | #define _(f,s) VIRTIO_TX_ERROR_##f, |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 46 | foreach_virtio_tx_func_error |
| 47 | #undef _ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 48 | VIRTIO_TX_N_ERROR, |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 49 | } virtio_tx_func_error_t; |
| 50 | |
| 51 | static char *virtio_tx_func_error_strings[] = { |
| 52 | #define _(n,s) s, |
| 53 | foreach_virtio_tx_func_error |
| 54 | #undef _ |
| 55 | }; |
| 56 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 57 | static u8 * |
| 58 | format_virtio_device (u8 * s, va_list * args) |
| 59 | { |
| 60 | u32 dev_instance = va_arg (*args, u32); |
| 61 | int verbose = va_arg (*args, int); |
| 62 | u32 indent = format_get_indent (s); |
| 63 | |
| 64 | s = format (s, "VIRTIO interface"); |
| 65 | if (verbose) |
| 66 | { |
| 67 | s = format (s, "\n%U instance %u", format_white_space, indent + 2, |
| 68 | dev_instance); |
| 69 | } |
| 70 | return s; |
| 71 | } |
| 72 | |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 73 | typedef struct |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 74 | { |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 75 | u32 buffer_index; |
| 76 | u32 sw_if_index; |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 77 | generic_header_offset_t gho; |
Damjan Marion | 8c69887 | 2020-10-10 19:17:58 +0200 | [diff] [blame] | 78 | vlib_buffer_t buffer; |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 79 | } virtio_tx_trace_t; |
| 80 | |
| 81 | static u8 * |
| 82 | format_virtio_tx_trace (u8 * s, va_list * va) |
| 83 | { |
| 84 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 85 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
| 86 | virtio_tx_trace_t *t = va_arg (*va, virtio_tx_trace_t *); |
| 87 | u32 indent = format_get_indent (s); |
| 88 | |
Mohsin Kazmi | b3a8f4e | 2020-09-15 16:41:44 +0000 | [diff] [blame] | 89 | s = format (s, "%Ubuffer 0x%x: %U\n", |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 90 | format_white_space, indent, |
| 91 | t->buffer_index, format_vnet_buffer, &t->buffer); |
Mohsin Kazmi | b3a8f4e | 2020-09-15 16:41:44 +0000 | [diff] [blame] | 92 | s = |
| 93 | format (s, "%U%U\n", format_white_space, indent, |
| 94 | format_generic_header_offset, &t->gho); |
| 95 | s = |
| 96 | format (s, "%U%U", format_white_space, indent, |
| 97 | format_ethernet_header_with_length, t->buffer.pre_data, |
| 98 | sizeof (t->buffer.pre_data)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 99 | return s; |
| 100 | } |
| 101 | |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 102 | static_always_inline void |
Mohsin Kazmi | 03b2695 | 2020-09-16 16:53:16 +0000 | [diff] [blame] | 103 | virtio_tx_trace (vlib_main_t * vm, vlib_node_runtime_t * node, |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 104 | vlib_buffer_t * b0, u32 bi, int is_tun) |
Mohsin Kazmi | 03b2695 | 2020-09-16 16:53:16 +0000 | [diff] [blame] | 105 | { |
| 106 | virtio_tx_trace_t *t; |
| 107 | t = vlib_add_trace (vm, node, b0, sizeof (t[0])); |
| 108 | t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; |
| 109 | t->buffer_index = bi; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 110 | if (is_tun) |
Mohsin Kazmi | 03b2695 | 2020-09-16 16:53:16 +0000 | [diff] [blame] | 111 | { |
| 112 | int is_ip4 = 0, is_ip6 = 0; |
| 113 | |
| 114 | switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0) |
| 115 | { |
| 116 | case 0x40: |
| 117 | is_ip4 = 1; |
| 118 | break; |
| 119 | case 0x60: |
| 120 | is_ip6 = 1; |
| 121 | break; |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4, is_ip6); |
| 126 | } |
| 127 | else |
| 128 | vnet_generic_header_offset_parser (b0, &t->gho, 1, |
| 129 | b0->flags & |
| 130 | VNET_BUFFER_F_IS_IP4, |
| 131 | b0->flags & VNET_BUFFER_F_IS_IP6); |
| 132 | |
| 133 | clib_memcpy_fast (&t->buffer, b0, sizeof (*b0) - sizeof (b0->pre_data)); |
| 134 | clib_memcpy_fast (t->buffer.pre_data, vlib_buffer_get_current (b0), |
| 135 | sizeof (t->buffer.pre_data)); |
| 136 | } |
| 137 | |
| 138 | static_always_inline void |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 139 | virtio_interface_drop_inline (vlib_main_t * vm, uword node_index, |
| 140 | u32 * buffers, u16 n, |
| 141 | virtio_tx_func_error_t error) |
| 142 | { |
| 143 | vlib_error_count (vm, node_index, error, n); |
| 144 | vlib_buffer_free (vm, buffers, n); |
| 145 | } |
| 146 | |
| 147 | static_always_inline void |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 148 | virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers) |
| 149 | { |
| 150 | ASSERT (n_buffers <= ring_size); |
| 151 | |
| 152 | if (PREDICT_TRUE (start + n_buffers <= ring_size)) |
| 153 | { |
| 154 | clib_memset_u32 (ring + start, ~0, n_buffers); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | clib_memset_u32 (ring + start, ~0, ring_size - start); |
| 159 | clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start)); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static_always_inline void |
| 164 | virtio_free_used_device_desc (vlib_main_t * vm, virtio_vring_t * vring, |
| 165 | uword node_index) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 166 | { |
| 167 | u16 used = vring->desc_in_use; |
| 168 | u16 sz = vring->size; |
| 169 | u16 mask = sz - 1; |
| 170 | u16 last = vring->last_used_idx; |
| 171 | u16 n_left = vring->used->idx - last; |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 172 | u16 out_of_order_count = 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 173 | |
| 174 | if (n_left == 0) |
| 175 | return; |
| 176 | |
| 177 | while (n_left) |
| 178 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 179 | vring_used_elem_t *e = &vring->used->ring[last & mask]; |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 180 | u16 slot, n_buffers; |
| 181 | slot = n_buffers = e->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 182 | |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 183 | while (e->id == (n_buffers & mask)) |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 184 | { |
| 185 | n_left--; |
| 186 | last++; |
| 187 | n_buffers++; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 188 | vring_desc_t *d = &vring->desc[e->id]; |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 189 | u16 next; |
| 190 | while (d->flags & VRING_DESC_F_NEXT) |
| 191 | { |
| 192 | n_buffers++; |
| 193 | next = d->next; |
| 194 | d = &vring->desc[next]; |
| 195 | } |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 196 | if (n_left == 0) |
| 197 | break; |
| 198 | e = &vring->used->ring[last & mask]; |
| 199 | } |
| 200 | vlib_buffer_free_from_ring (vm, vring->buffers, slot, |
| 201 | sz, (n_buffers - slot)); |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 202 | virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot)); |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 203 | used -= (n_buffers - slot); |
| 204 | |
| 205 | if (n_left > 0) |
| 206 | { |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 207 | vlib_buffer_free (vm, &vring->buffers[e->id], 1); |
| 208 | vring->buffers[e->id] = ~0; |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 209 | used--; |
| 210 | last++; |
| 211 | n_left--; |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 212 | out_of_order_count++; |
| 213 | vring->flags |= VRING_TX_OUT_OF_ORDER; |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 214 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 215 | } |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Some vhost-backends give buffers back in out-of-order fashion in used ring. |
| 219 | * It impacts the overall virtio-performance. |
| 220 | */ |
| 221 | if (out_of_order_count) |
| 222 | vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER, |
| 223 | out_of_order_count); |
| 224 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 225 | vring->desc_in_use = used; |
| 226 | vring->last_used_idx = last; |
| 227 | } |
| 228 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 229 | static_always_inline void |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 230 | set_checksum_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr, int is_l2) |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 231 | { |
| 232 | if (b->flags & VNET_BUFFER_F_IS_IP4) |
| 233 | { |
| 234 | ip4_header_t *ip4; |
Mohsin Kazmi | 0b04209 | 2020-04-17 16:50:56 +0000 | [diff] [blame] | 235 | generic_header_offset_t gho = { 0 }; |
Mohsin Kazmi | 84f91fa | 2020-04-23 17:59:49 +0200 | [diff] [blame] | 236 | vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ , |
| 237 | 0 /* ip6 */ ); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 238 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 239 | hdr->csum_start = gho.l4_hdr_offset; // 0x22; |
| 240 | if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM) |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 241 | { |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 242 | hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); |
| 243 | } |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 244 | else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 245 | { |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 246 | hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum); |
| 247 | } |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * virtio devices do not support IP4 checksum offload. So driver takes care |
| 251 | * of it while doing tx. |
| 252 | */ |
| 253 | ip4 = |
| 254 | (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset); |
| 255 | if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM) |
| 256 | ip4->checksum = ip4_header_checksum (ip4); |
| 257 | } |
| 258 | else if (b->flags & VNET_BUFFER_F_IS_IP6) |
| 259 | { |
Mohsin Kazmi | 0b04209 | 2020-04-17 16:50:56 +0000 | [diff] [blame] | 260 | generic_header_offset_t gho = { 0 }; |
Mohsin Kazmi | 84f91fa | 2020-04-23 17:59:49 +0200 | [diff] [blame] | 261 | vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ , |
| 262 | 1 /* ip6 */ ); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 263 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 264 | hdr->csum_start = gho.l4_hdr_offset; // 0x36; |
| 265 | if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM) |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 266 | { |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 267 | hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); |
| 268 | } |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 269 | else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 270 | { |
Mohsin Kazmi | 0937fdf | 2020-03-25 20:37:16 +0000 | [diff] [blame] | 271 | hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum); |
| 272 | } |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 276 | static_always_inline void |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 277 | set_gso_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr, int is_l2) |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 278 | { |
| 279 | if (b->flags & VNET_BUFFER_F_IS_IP4) |
| 280 | { |
| 281 | ip4_header_t *ip4; |
Mohsin Kazmi | 0b04209 | 2020-04-17 16:50:56 +0000 | [diff] [blame] | 282 | generic_header_offset_t gho = { 0 }; |
Mohsin Kazmi | 84f91fa | 2020-04-23 17:59:49 +0200 | [diff] [blame] | 283 | vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ , |
| 284 | 0 /* ip6 */ ); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 285 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 286 | hdr->gso_size = vnet_buffer2 (b)->gso_size; |
Mohsin Kazmi | 0b04209 | 2020-04-17 16:50:56 +0000 | [diff] [blame] | 287 | hdr->hdr_len = gho.hdr_sz; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 288 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 289 | hdr->csum_start = gho.l4_hdr_offset; // 0x22; |
| 290 | hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); |
| 291 | ip4 = |
| 292 | (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset); |
| 293 | /* |
| 294 | * virtio devices do not support IP4 checksum offload. So driver takes care |
| 295 | * of it while doing tx. |
| 296 | */ |
| 297 | if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM) |
| 298 | ip4->checksum = ip4_header_checksum (ip4); |
| 299 | } |
| 300 | else if (b->flags & VNET_BUFFER_F_IS_IP6) |
| 301 | { |
Mohsin Kazmi | 0b04209 | 2020-04-17 16:50:56 +0000 | [diff] [blame] | 302 | generic_header_offset_t gho = { 0 }; |
Mohsin Kazmi | 84f91fa | 2020-04-23 17:59:49 +0200 | [diff] [blame] | 303 | vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ , |
| 304 | 1 /* ip6 */ ); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 305 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
| 306 | hdr->gso_size = vnet_buffer2 (b)->gso_size; |
Mohsin Kazmi | 0b04209 | 2020-04-17 16:50:56 +0000 | [diff] [blame] | 307 | hdr->hdr_len = gho.hdr_sz; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 308 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 309 | hdr->csum_start = gho.l4_hdr_offset; // 0x36; |
| 310 | hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); |
| 311 | } |
| 312 | } |
| 313 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 314 | static_always_inline u16 |
Mohsin Kazmi | 8314371 | 2020-10-07 13:23:55 +0200 | [diff] [blame] | 315 | add_buffer_to_slot (vlib_main_t * vm, vlib_node_runtime_t * node, |
Mohsin Kazmi | 8314371 | 2020-10-07 13:23:55 +0200 | [diff] [blame] | 316 | virtio_vring_t * vring, u32 bi, u16 free_desc_count, |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 317 | u16 avail, u16 next, u16 mask, int hdr_sz, int do_gso, |
| 318 | int csum_offload, int is_pci, int is_tun, int is_indirect, |
| 319 | int is_any_layout) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 320 | { |
| 321 | u16 n_added = 0; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 322 | vring_desc_t *d; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 323 | int is_l2 = !is_tun; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 324 | d = &vring->desc[next]; |
| 325 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 326 | 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] | 327 | u32 drop_inline = ~0; |
Damjan Marion | 508cabc | 2018-02-08 19:49:22 +0100 | [diff] [blame] | 328 | |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 329 | clib_memset_u8 (hdr, 0, hdr_sz); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 330 | |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 331 | if (b->flags & VNET_BUFFER_F_GSO) |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 332 | { |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 333 | if (do_gso) |
Mohsin Kazmi | 84f91fa | 2020-04-23 17:59:49 +0200 | [diff] [blame] | 334 | set_gso_offsets (b, hdr, is_l2); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 335 | else |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 336 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 337 | drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP; |
| 338 | goto done; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 339 | } |
| 340 | } |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 341 | else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM | |
| 342 | VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)) |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 343 | { |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 344 | if (csum_offload) |
Mohsin Kazmi | 84f91fa | 2020-04-23 17:59:49 +0200 | [diff] [blame] | 345 | set_checksum_offsets (b, hdr, is_l2); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 346 | else |
| 347 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 348 | drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP; |
| 349 | goto done; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 350 | } |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 351 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 352 | |
Mohsin Kazmi | 8314371 | 2020-10-07 13:23:55 +0200 | [diff] [blame] | 353 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 354 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 355 | virtio_tx_trace (vm, node, b, bi, is_tun); |
Mohsin Kazmi | 8314371 | 2020-10-07 13:23:55 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 358 | if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0)) |
| 359 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 360 | d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) : |
| 361 | pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 362 | d->len = b->current_length + hdr_sz; |
| 363 | d->flags = 0; |
| 364 | } |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 365 | else if (is_indirect) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 366 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 367 | /* |
| 368 | * We are using single vlib_buffer_t for indirect descriptor(s) |
| 369 | * chain. Single descriptor is 16 bytes and vlib_buffer_t |
| 370 | * has 2048 bytes space. So maximum long chain can have 128 |
| 371 | * (=2048/16) indirect descriptors. |
| 372 | * It can easily support 65535 bytes of Jumbo frames with |
| 373 | * each data buffer size of 512 bytes minimum. |
| 374 | */ |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 375 | u32 indirect_buffer = 0; |
| 376 | if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0)) |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 377 | { |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 378 | drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED; |
| 379 | goto done; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 380 | } |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 381 | |
| 382 | vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 383 | indirect_desc->current_data = 0; |
Mohsin Kazmi | aea0df3 | 2019-05-23 14:32:58 +0200 | [diff] [blame] | 384 | indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 385 | indirect_desc->next_buffer = bi; |
| 386 | bi = indirect_buffer; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 387 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 388 | vring_desc_t *id = |
| 389 | (vring_desc_t *) vlib_buffer_get_current (indirect_desc); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 390 | u32 count = 1; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 391 | if (is_pci) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 392 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 393 | d->addr = vlib_physmem_get_pa (vm, id); |
| 394 | id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 395 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 396 | /* |
| 397 | * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr |
| 398 | * should be presented in separate descriptor and data will start |
| 399 | * from next descriptor. |
| 400 | */ |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 401 | if (is_any_layout) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 402 | id->len = b->current_length + hdr_sz; |
| 403 | else |
| 404 | { |
| 405 | id->len = hdr_sz; |
| 406 | id->flags = VRING_DESC_F_NEXT; |
| 407 | id->next = count; |
| 408 | count++; |
| 409 | id++; |
| 410 | id->addr = vlib_buffer_get_current_pa (vm, b); |
| 411 | id->len = b->current_length; |
| 412 | } |
| 413 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 414 | { |
| 415 | id->flags = VRING_DESC_F_NEXT; |
| 416 | id->next = count; |
| 417 | count++; |
| 418 | id++; |
| 419 | b = vlib_get_buffer (vm, b->next_buffer); |
| 420 | id->addr = vlib_buffer_get_current_pa (vm, b); |
| 421 | id->len = b->current_length; |
| 422 | } |
| 423 | } |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 424 | else /* VIRTIO_IF_TYPE_[TAP | TUN] */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 425 | { |
| 426 | d->addr = pointer_to_uword (id); |
| 427 | /* first buffer in chain */ |
| 428 | id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz; |
| 429 | id->len = b->current_length + hdr_sz; |
| 430 | |
| 431 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 432 | { |
| 433 | id->flags = VRING_DESC_F_NEXT; |
| 434 | id->next = count; |
| 435 | count++; |
| 436 | id++; |
| 437 | b = vlib_get_buffer (vm, b->next_buffer); |
| 438 | id->addr = pointer_to_uword (vlib_buffer_get_current (b)); |
| 439 | id->len = b->current_length; |
| 440 | } |
| 441 | } |
| 442 | id->flags = 0; |
| 443 | id->next = 0; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 444 | d->len = count * sizeof (vring_desc_t); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 445 | d->flags = VRING_DESC_F_INDIRECT; |
| 446 | } |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 447 | else if (is_pci) |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 448 | { |
| 449 | u16 count = next; |
| 450 | vlib_buffer_t *b_temp = b; |
| 451 | u16 n_buffers_in_chain = 1; |
| 452 | |
| 453 | /* |
| 454 | * Check the length of the chain for the required number of |
| 455 | * descriptors. Return from here, retry to get more descriptors, |
| 456 | * if chain length is greater than available descriptors. |
| 457 | */ |
| 458 | while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 459 | { |
| 460 | n_buffers_in_chain++; |
| 461 | b_temp = vlib_get_buffer (vm, b_temp->next_buffer); |
| 462 | } |
| 463 | |
| 464 | if (n_buffers_in_chain > free_desc_count) |
| 465 | return n_buffers_in_chain; |
| 466 | |
| 467 | d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz; |
| 468 | d->len = b->current_length + hdr_sz; |
| 469 | |
| 470 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 471 | { |
| 472 | d->flags = VRING_DESC_F_NEXT; |
| 473 | vring->buffers[count] = bi; |
| 474 | b->flags &= |
| 475 | ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID); |
| 476 | bi = b->next_buffer; |
| 477 | b->next_buffer = 0; |
| 478 | n_added++; |
| 479 | count = (count + 1) & mask; |
| 480 | d->next = count; |
| 481 | d = &vring->desc[count]; |
| 482 | b = vlib_get_buffer (vm, bi); |
| 483 | d->addr = vlib_buffer_get_current_pa (vm, b); |
| 484 | d->len = b->current_length; |
| 485 | } |
| 486 | d->flags = 0; |
| 487 | vring->buffers[count] = bi; |
| 488 | vring->avail->ring[avail & mask] = next; |
| 489 | n_added++; |
| 490 | return n_added; |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | ASSERT (0); |
| 495 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 496 | vring->buffers[next] = bi; |
| 497 | vring->avail->ring[avail & mask] = next; |
| 498 | n_added++; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 499 | |
| 500 | done: |
| 501 | if (drop_inline != ~0) |
| 502 | virtio_interface_drop_inline (vm, node->node_index, &bi, 1, drop_inline); |
| 503 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 504 | return n_added; |
| 505 | } |
| 506 | |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 507 | static_always_inline void |
| 508 | virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask, |
| 509 | u16 req, u16 next, u32 * first_free_desc_index, |
| 510 | u16 * free_desc_count) |
| 511 | { |
| 512 | u16 start = 0; |
| 513 | /* next is used as hint: from where to start looking */ |
| 514 | for (u16 i = 0; i < size; i++, next++) |
| 515 | { |
| 516 | if (vring->buffers[next & mask] == ~0) |
| 517 | { |
| 518 | if (*first_free_desc_index == ~0) |
| 519 | { |
| 520 | *first_free_desc_index = (next & mask); |
| 521 | start = i; |
| 522 | (*free_desc_count)++; |
| 523 | req--; |
| 524 | if (req == 0) |
| 525 | break; |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | if (start + *free_desc_count == i) |
| 530 | { |
| 531 | (*free_desc_count)++; |
| 532 | req--; |
| 533 | if (req == 0) |
| 534 | break; |
| 535 | } |
| 536 | else |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 543 | static_always_inline u16 |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 544 | virtio_interface_tx_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 545 | virtio_if_t * vif, |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 546 | virtio_if_type_t type, virtio_vring_t * vring, |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 547 | u32 * buffers, u16 n_left, int do_gso, |
| 548 | int csum_offload) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 549 | { |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 550 | u16 used, next, avail, n_buffers = 0, n_buffers_left = 0; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 551 | int is_pci = (type == VIRTIO_IF_TYPE_PCI); |
| 552 | int is_tun = (type == VIRTIO_IF_TYPE_TUN); |
| 553 | int is_indirect = |
| 554 | ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0); |
| 555 | int is_any_layout = |
| 556 | ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 557 | u16 sz = vring->size; |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 558 | int hdr_sz = vif->virtio_net_hdr_sz; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 559 | u16 mask = sz - 1; |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 560 | u16 n_vectors = n_left; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 561 | |
| 562 | used = vring->desc_in_use; |
| 563 | next = vring->desc_next; |
| 564 | avail = vring->avail->idx; |
| 565 | |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 566 | u16 free_desc_count = 0; |
| 567 | |
| 568 | if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER)) |
| 569 | { |
| 570 | u32 first_free_desc_index = ~0; |
| 571 | |
| 572 | virtio_find_free_desc (vring, sz, mask, n_left, next, |
| 573 | &first_free_desc_index, &free_desc_count); |
| 574 | |
| 575 | if (free_desc_count) |
| 576 | next = first_free_desc_index; |
| 577 | } |
| 578 | else |
| 579 | free_desc_count = sz - used; |
| 580 | |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 581 | if (vif->packet_buffering) |
| 582 | { |
| 583 | n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering); |
| 584 | |
| 585 | while (n_buffers_left && free_desc_count) |
| 586 | { |
| 587 | u16 n_added = 0; |
| 588 | |
| 589 | u32 bi = virtio_vring_buffering_read_from_front (vring->buffering); |
| 590 | if (bi == ~0) |
| 591 | break; |
Mohsin Kazmi | 8314371 | 2020-10-07 13:23:55 +0200 | [diff] [blame] | 592 | |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 593 | n_added = add_buffer_to_slot (vm, node, vring, bi, free_desc_count, |
| 594 | avail, next, mask, hdr_sz, do_gso, |
| 595 | csum_offload, is_pci, is_tun, |
| 596 | is_indirect, is_any_layout); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 597 | if (PREDICT_FALSE (n_added == 0)) |
| 598 | { |
| 599 | n_buffers_left--; |
| 600 | continue; |
| 601 | } |
| 602 | else if (PREDICT_FALSE (n_added > free_desc_count)) |
| 603 | break; |
| 604 | |
| 605 | avail++; |
| 606 | next = (next + n_added) & mask; |
| 607 | used += n_added; |
| 608 | n_buffers_left--; |
| 609 | free_desc_count -= n_added; |
| 610 | } |
| 611 | } |
| 612 | |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 613 | while (n_left && free_desc_count) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 614 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 615 | u16 n_added = 0; |
Mohsin Kazmi | b5ca559 | 2020-06-26 13:48:26 +0000 | [diff] [blame] | 616 | |
Damjan Marion | d4a5471 | 2020-10-31 23:40:51 +0100 | [diff] [blame] | 617 | n_added = add_buffer_to_slot (vm, node, vring, buffers[0], |
| 618 | free_desc_count, avail, next, mask, |
| 619 | hdr_sz, do_gso, csum_offload, is_pci, |
| 620 | is_tun, is_indirect, is_any_layout); |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 621 | |
| 622 | if (PREDICT_FALSE (n_added == 0)) |
| 623 | { |
| 624 | buffers++; |
| 625 | n_left--; |
| 626 | continue; |
| 627 | } |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 628 | else if (PREDICT_FALSE (n_added > free_desc_count)) |
| 629 | break; |
Mohsin Kazmi | f50ef40 | 2020-04-09 22:11:29 +0200 | [diff] [blame] | 630 | |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 631 | avail++; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 632 | next = (next + n_added) & mask; |
| 633 | used += n_added; |
| 634 | buffers++; |
| 635 | n_left--; |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 636 | free_desc_count -= n_added; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 639 | if (n_left != n_vectors || n_buffers != n_buffers_left) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 640 | { |
| 641 | CLIB_MEMORY_STORE_BARRIER (); |
| 642 | vring->avail->idx = avail; |
| 643 | vring->desc_next = next; |
| 644 | vring->desc_in_use = used; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 645 | if ((vring->used->flags & VRING_USED_F_NO_NOTIFY) == 0) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 646 | virtio_kick (vm, vring, vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 647 | } |
| 648 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 649 | return n_left; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 652 | static_always_inline u16 |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 653 | virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 654 | virtio_if_t * vif, |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 655 | virtio_vring_t * vring, virtio_if_type_t type, |
| 656 | u32 * buffers, u16 n_left) |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 657 | { |
| 658 | vnet_main_t *vnm = vnet_get_main (); |
| 659 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 660 | |
| 661 | if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 662 | return virtio_interface_tx_gso_inline (vm, node, vif, type, vring, |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 663 | buffers, n_left, 1 /* do_gso */ , |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 664 | 1 /* checksum offload */ ); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 665 | else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 666 | return virtio_interface_tx_gso_inline (vm, node, vif, type, vring, |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 667 | buffers, n_left, |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 668 | 0 /* no do_gso */ , |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 669 | 1 /* checksum offload */ ); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 670 | else |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 671 | return virtio_interface_tx_gso_inline (vm, node, vif, type, vring, |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 672 | buffers, n_left, |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 673 | 0 /* no do_gso */ , |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 674 | 0 /* no checksum offload */ ); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Filip Tehlar | aee7364 | 2019-03-13 05:50:44 -0700 | [diff] [blame] | 677 | VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm, |
| 678 | vlib_node_runtime_t * node, |
| 679 | vlib_frame_t * frame) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 680 | { |
| 681 | virtio_main_t *nm = &virtio_main; |
| 682 | vnet_interface_output_runtime_t *rund = (void *) node->runtime_data; |
| 683 | 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] | 684 | u16 qid = vm->thread_index % vif->num_txqs; |
| 685 | virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid); |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 686 | u16 n_left = frame->n_vectors; |
| 687 | u32 *buffers = vlib_frame_vector_args (frame); |
| 688 | u32 to[GRO_TO_VECTOR_SIZE (n_left)]; |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 689 | |
| 690 | clib_spinlock_lock_if_init (&vring->lockp); |
| 691 | |
| 692 | if ((vring->used->flags & VRING_USED_F_NO_NOTIFY) == 0 && |
| 693 | (vring->last_kick_avail_idx != vring->avail->idx)) |
| 694 | virtio_kick (vm, vring, vif); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 695 | |
Mohsin Kazmi | ee1e30f | 2020-10-23 11:23:52 +0200 | [diff] [blame] | 696 | if (vif->packet_coalesce) |
| 697 | { |
| 698 | n_left = vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to); |
| 699 | buffers = to; |
| 700 | } |
| 701 | |
Damjan Marion | 587f913 | 2020-10-31 22:47:01 +0100 | [diff] [blame] | 702 | u16 retry_count = 2; |
| 703 | |
| 704 | retry: |
| 705 | /* free consumed buffers */ |
| 706 | virtio_free_used_device_desc (vm, vring, node->node_index); |
| 707 | |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 708 | if (vif->type == VIRTIO_IF_TYPE_TAP) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 709 | n_left = virtio_interface_tx_inline (vm, node, vif, vring, |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 710 | VIRTIO_IF_TYPE_TAP, |
| 711 | &buffers[frame->n_vectors - n_left], |
| 712 | n_left); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 713 | else if (vif->type == VIRTIO_IF_TYPE_PCI) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 714 | n_left = virtio_interface_tx_inline (vm, node, vif, vring, |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 715 | VIRTIO_IF_TYPE_PCI, |
| 716 | &buffers[frame->n_vectors - n_left], |
| 717 | n_left); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 718 | else if (vif->type == VIRTIO_IF_TYPE_TUN) |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 719 | n_left = virtio_interface_tx_inline (vm, node, vif, vring, |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 720 | VIRTIO_IF_TYPE_TUN, |
| 721 | &buffers[frame->n_vectors - n_left], |
| 722 | n_left); |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 723 | else |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 724 | ASSERT (0); |
| 725 | |
Damjan Marion | 587f913 | 2020-10-31 22:47:01 +0100 | [diff] [blame] | 726 | if (n_left && retry_count--) |
| 727 | goto retry; |
| 728 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 729 | if (vif->packet_buffering && n_left) |
| 730 | { |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 731 | u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering, |
| 732 | &buffers |
| 733 | [frame->n_vectors |
| 734 | - n_left], |
| 735 | n_left); |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 736 | buffers += n_buffered; |
| 737 | n_left -= n_buffered; |
| 738 | } |
| 739 | if (n_left) |
| 740 | virtio_interface_drop_inline (vm, node->node_index, |
Mohsin Kazmi | 81dac06 | 2020-11-11 20:28:35 +0100 | [diff] [blame] | 741 | &buffers[frame->n_vectors - n_left], n_left, |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 742 | VIRTIO_TX_ERROR_NO_FREE_SLOTS); |
| 743 | |
Damjan Marion | dc18124 | 2020-10-20 12:11:06 +0200 | [diff] [blame] | 744 | clib_spinlock_unlock_if_init (&vring->lockp); |
| 745 | |
Mohsin Kazmi | 2e6f624 | 2020-10-26 12:41:41 +0100 | [diff] [blame] | 746 | return frame->n_vectors - n_left; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | static void |
| 750 | virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index, |
| 751 | u32 node_index) |
| 752 | { |
| 753 | virtio_main_t *apm = &virtio_main; |
| 754 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 755 | virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance); |
| 756 | |
| 757 | /* Shut off redirection */ |
| 758 | if (node_index == ~0) |
| 759 | { |
| 760 | vif->per_interface_next_index = node_index; |
| 761 | return; |
| 762 | } |
| 763 | |
| 764 | vif->per_interface_next_index = |
| 765 | vlib_node_add_next (vlib_get_main (), virtio_input_node.index, |
| 766 | node_index); |
| 767 | } |
| 768 | |
| 769 | static void |
| 770 | virtio_clear_hw_interface_counters (u32 instance) |
| 771 | { |
| 772 | /* Nothing for now */ |
| 773 | } |
| 774 | |
| 775 | static clib_error_t * |
| 776 | 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] | 777 | vnet_hw_if_rx_mode mode) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 778 | { |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 779 | vlib_main_t *vm = vnm->vlib_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 780 | virtio_main_t *mm = &virtio_main; |
| 781 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 782 | 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] | 783 | 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] | 784 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 785 | if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode)) |
| 786 | { |
Mohsin Kazmi | 1017a1d | 2020-09-25 15:36:19 +0200 | [diff] [blame] | 787 | rx_vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 788 | return clib_error_return (0, "interrupt mode is not supported"); |
| 789 | } |
| 790 | |
Damjan Marion | eabd424 | 2020-10-07 20:59:07 +0200 | [diff] [blame] | 791 | if (mode == VNET_HW_IF_RX_MODE_POLLING) |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 792 | { |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 793 | if (vif->packet_coalesce || vif->packet_buffering) |
| 794 | { |
| 795 | if (mm->interrupt_queues_count > 0) |
| 796 | mm->interrupt_queues_count--; |
| 797 | if (mm->interrupt_queues_count == 0) |
| 798 | vlib_process_signal_event (vm, |
| 799 | virtio_send_interrupt_node.index, |
| 800 | VIRTIO_EVENT_STOP_TIMER, 0); |
| 801 | } |
Mohsin Kazmi | 1017a1d | 2020-09-25 15:36:19 +0200 | [diff] [blame] | 802 | rx_vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 803 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 804 | else |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 805 | { |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 806 | if (vif->packet_coalesce || vif->packet_buffering) |
Mohsin Kazmi | 1017a1d | 2020-09-25 15:36:19 +0200 | [diff] [blame] | 807 | { |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 808 | mm->interrupt_queues_count++; |
| 809 | if (mm->interrupt_queues_count == 1) |
| 810 | vlib_process_signal_event (vm, |
| 811 | virtio_send_interrupt_node.index, |
| 812 | VIRTIO_EVENT_START_TIMER, 0); |
Mohsin Kazmi | 1017a1d | 2020-09-25 15:36:19 +0200 | [diff] [blame] | 813 | } |
| 814 | rx_vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 815 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 816 | |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 817 | rx_vring->mode = mode; |
| 818 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static clib_error_t * |
| 823 | virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 824 | { |
| 825 | virtio_main_t *mm = &virtio_main; |
| 826 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 827 | 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] | 828 | |
| 829 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
Mohsin Kazmi | 529f0e9 | 2020-03-19 14:03:31 +0100 | [diff] [blame] | 830 | { |
| 831 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 832 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 833 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 834 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 835 | else |
Mohsin Kazmi | 529f0e9 | 2020-03-19 14:03:31 +0100 | [diff] [blame] | 836 | { |
| 837 | vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP; |
| 838 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 839 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static clib_error_t * |
| 844 | virtio_subif_add_del_function (vnet_main_t * vnm, |
| 845 | u32 hw_if_index, |
| 846 | struct vnet_sw_interface_t *st, int is_add) |
| 847 | { |
| 848 | /* Nothing for now */ |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | /* *INDENT-OFF* */ |
| 853 | VNET_DEVICE_CLASS (virtio_device_class) = { |
| 854 | .name = "virtio", |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 855 | .format_device_name = format_virtio_device_name, |
| 856 | .format_device = format_virtio_device, |
| 857 | .format_tx_trace = format_virtio_tx_trace, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 858 | .tx_function_n_errors = VIRTIO_TX_N_ERROR, |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 859 | .tx_function_error_strings = virtio_tx_func_error_strings, |
| 860 | .rx_redirect_to_node = virtio_set_interface_next_node, |
| 861 | .clear_counters = virtio_clear_hw_interface_counters, |
| 862 | .admin_up_down_function = virtio_interface_admin_up_down, |
| 863 | .subif_add_del_function = virtio_subif_add_del_function, |
| 864 | .rx_mode_change_function = virtio_interface_rx_mode_change, |
| 865 | }; |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 866 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 867 | /* *INDENT-ON* */ |
| 868 | |
| 869 | /* |
| 870 | * fd.io coding-style-patch-verification: ON |
| 871 | * |
| 872 | * Local Variables: |
| 873 | * eval: (c-set-style "gnu") |
| 874 | * End: |
| 875 | */ |