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