blob: 0ae41141a5ae82f9161a9fc65a877625335f38bf [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
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 Marion8389fb92017-10-13 18:29:53 +020021
22#include <vlib/vlib.h>
23#include <vlib/unix/unix.h>
Mohsin Kazmi206acf82020-04-06 14:19:54 +020024#include <vnet/vnet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020025#include <vnet/ethernet/ethernet.h>
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +020026#include <vnet/gso/gro_func.h>
Mohsin Kazmi0b042092020-04-17 16:50:56 +000027#include <vnet/gso/hdr_offset_parser.h>
Milan Lenco73e7f422017-12-14 10:04:25 +010028#include <vnet/ip/ip4_packet.h>
29#include <vnet/ip/ip6_packet.h>
Mohsin Kazmif5462362021-02-23 15:55:04 +010030#include <vnet/ip/ip_psh_cksum.h>
Mohsin Kazmi6d4af892020-01-03 15:11:53 +000031#include <vnet/tcp/tcp_packet.h>
32#include <vnet/udp/udp_packet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020033#include <vnet/devices/virtio/virtio.h>
34
Mohsin Kazmi637c97c2021-05-12 14:51:29 +020035#define VIRTIO_TX_MAX_CHAIN_LEN 127
36
Damjan Marion8389fb92017-10-13 18:29:53 +020037#define foreach_virtio_tx_func_error \
38_(NO_FREE_SLOTS, "no free tx slots") \
39_(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
40_(PENDING_MSGS, "pending msgs in tx ring") \
Mohsin Kazmif50ef402020-04-09 22:11:29 +020041_(INDIRECT_DESC_ALLOC_FAILED, "indirect descriptor allocation failed - packet drop") \
42_(OUT_OF_ORDER, "out-of-order buffers in used ring") \
43_(GSO_PACKET_DROP, "gso disabled on itf -- gso packet drop") \
44_(CSUM_OFFLOAD_PACKET_DROP, "checksum offload disabled on itf -- csum offload packet drop")
Damjan Marion8389fb92017-10-13 18:29:53 +020045
46typedef enum
47{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020048#define _(f,s) VIRTIO_TX_ERROR_##f,
Damjan Marion8389fb92017-10-13 18:29:53 +020049 foreach_virtio_tx_func_error
50#undef _
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020051 VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +020052} virtio_tx_func_error_t;
53
54static char *virtio_tx_func_error_strings[] = {
55#define _(n,s) s,
56 foreach_virtio_tx_func_error
57#undef _
58};
59
Damjan Marion8389fb92017-10-13 18:29:53 +020060static u8 *
61format_virtio_device (u8 * s, va_list * args)
62{
63 u32 dev_instance = va_arg (*args, u32);
64 int verbose = va_arg (*args, int);
65 u32 indent = format_get_indent (s);
66
67 s = format (s, "VIRTIO interface");
68 if (verbose)
69 {
70 s = format (s, "\n%U instance %u", format_white_space, indent + 2,
71 dev_instance);
72 }
73 return s;
74}
75
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000076typedef struct
Damjan Marion8389fb92017-10-13 18:29:53 +020077{
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000078 u32 buffer_index;
79 u32 sw_if_index;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000080 generic_header_offset_t gho;
Damjan Marion8c698872020-10-10 19:17:58 +020081 vlib_buffer_t buffer;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000082} virtio_tx_trace_t;
83
84static u8 *
85format_virtio_tx_trace (u8 * s, va_list * va)
86{
87 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
88 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
89 virtio_tx_trace_t *t = va_arg (*va, virtio_tx_trace_t *);
90 u32 indent = format_get_indent (s);
91
BenoƮt Ganne03f2a012021-07-20 16:49:13 +020092 s = format (s, "%Ubuffer 0x%x: %U\n", format_white_space, indent,
93 t->buffer_index, format_vnet_buffer_no_chain, &t->buffer);
Mohsin Kazmib3a8f4e2020-09-15 16:41:44 +000094 s =
95 format (s, "%U%U\n", format_white_space, indent,
96 format_generic_header_offset, &t->gho);
97 s =
98 format (s, "%U%U", format_white_space, indent,
99 format_ethernet_header_with_length, t->buffer.pre_data,
100 sizeof (t->buffer.pre_data));
Damjan Marion8389fb92017-10-13 18:29:53 +0200101 return s;
102}
103
Damjan Marion809d5c92021-03-24 12:24:58 +0100104static void
105virtio_tx_trace (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b0,
106 u32 bi, int is_tun)
Mohsin Kazmi03b26952020-09-16 16:53:16 +0000107{
108 virtio_tx_trace_t *t;
109 t = vlib_add_trace (vm, node, b0, sizeof (t[0]));
110 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
111 t->buffer_index = bi;
Vladislav Grishenko62fc7872021-11-20 14:52:53 +0500112 clib_memset (&t->gho, 0, sizeof (t->gho));
Damjan Mariond4a54712020-10-31 23:40:51 +0100113 if (is_tun)
Mohsin Kazmi03b26952020-09-16 16:53:16 +0000114 {
115 int is_ip4 = 0, is_ip6 = 0;
116
117 switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0)
118 {
119 case 0x40:
120 is_ip4 = 1;
121 break;
122 case 0x60:
123 is_ip6 = 1;
124 break;
125 default:
126 break;
127 }
128 vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4, is_ip6);
129 }
130 else
131 vnet_generic_header_offset_parser (b0, &t->gho, 1,
132 b0->flags &
133 VNET_BUFFER_F_IS_IP4,
134 b0->flags & VNET_BUFFER_F_IS_IP6);
135
136 clib_memcpy_fast (&t->buffer, b0, sizeof (*b0) - sizeof (b0->pre_data));
137 clib_memcpy_fast (t->buffer.pre_data, vlib_buffer_get_current (b0),
138 sizeof (t->buffer.pre_data));
139}
140
Damjan Marion809d5c92021-03-24 12:24:58 +0100141static void
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200142virtio_interface_drop_inline (vlib_main_t *vm, virtio_if_t *vif,
143 uword node_index, u32 *buffers, u16 n,
144 virtio_tx_func_error_t error)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200145{
146 vlib_error_count (vm, node_index, error, n);
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200147 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
148 VNET_INTERFACE_COUNTER_DROP,
149 vm->thread_index, vif->sw_if_index, n);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200150 vlib_buffer_free (vm, buffers, n);
151}
152
Damjan Marion809d5c92021-03-24 12:24:58 +0100153static void
154virtio_memset_ring_u32 (u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100155{
156 ASSERT (n_buffers <= ring_size);
157
158 if (PREDICT_TRUE (start + n_buffers <= ring_size))
159 {
160 clib_memset_u32 (ring + start, ~0, n_buffers);
161 }
162 else
163 {
164 clib_memset_u32 (ring + start, ~0, ring_size - start);
165 clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
166 }
167}
168
Damjan Marion809d5c92021-03-24 12:24:58 +0100169static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000170virtio_free_used_device_desc_split (vlib_main_t *vm,
171 vnet_virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100172 uword node_index)
Damjan Marion8389fb92017-10-13 18:29:53 +0200173{
174 u16 used = vring->desc_in_use;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000175 u16 sz = vring->queue_size;
Damjan Marion8389fb92017-10-13 18:29:53 +0200176 u16 mask = sz - 1;
177 u16 last = vring->last_used_idx;
178 u16 n_left = vring->used->idx - last;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100179 u16 out_of_order_count = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200180
181 if (n_left == 0)
182 return;
183
184 while (n_left)
185 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000186 vnet_virtio_vring_used_elem_t *e = &vring->used->ring[last & mask];
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200187 u16 slot, n_buffers;
188 slot = n_buffers = e->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200189
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100190 while (e->id == (n_buffers & mask))
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200191 {
192 n_left--;
193 last++;
194 n_buffers++;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000195 vnet_virtio_vring_desc_t *d = &vring->desc[e->id];
Mohsin Kazmice215992020-04-10 14:57:06 +0000196 u16 next;
197 while (d->flags & VRING_DESC_F_NEXT)
198 {
199 n_buffers++;
200 next = d->next;
201 d = &vring->desc[next];
202 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200203 if (n_left == 0)
204 break;
205 e = &vring->used->ring[last & mask];
206 }
207 vlib_buffer_free_from_ring (vm, vring->buffers, slot,
208 sz, (n_buffers - slot));
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100209 virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200210 used -= (n_buffers - slot);
211
212 if (n_left > 0)
213 {
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100214 vlib_buffer_free (vm, &vring->buffers[e->id], 1);
215 vring->buffers[e->id] = ~0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200216 used--;
217 last++;
218 n_left--;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100219 out_of_order_count++;
220 vring->flags |= VRING_TX_OUT_OF_ORDER;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200221 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200222 }
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100223
224 /*
225 * Some vhost-backends give buffers back in out-of-order fashion in used ring.
226 * It impacts the overall virtio-performance.
227 */
228 if (out_of_order_count)
229 vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
230 out_of_order_count);
231
Damjan Marion8389fb92017-10-13 18:29:53 +0200232 vring->desc_in_use = used;
233 vring->last_used_idx = last;
234}
235
Damjan Marion809d5c92021-03-24 12:24:58 +0100236static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000237virtio_free_used_device_desc_packed (vlib_main_t *vm,
238 vnet_virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100239 uword node_index)
240{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000241 vnet_virtio_vring_packed_desc_t *d;
242 u16 sz = vring->queue_size;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100243 u16 last = vring->last_used_idx;
244 u16 n_buffers = 0, start;
245 u16 flags;
246
247 if (vring->desc_in_use == 0)
248 return;
249
250 d = &vring->packed_desc[last];
251 flags = d->flags;
252 start = d->id;
253
254 while ((flags & VRING_DESC_F_AVAIL) == (vring->used_wrap_counter << 7) &&
255 (flags & VRING_DESC_F_USED) == (vring->used_wrap_counter << 15))
256 {
257 last++;
258 n_buffers++;
259
260 if (last >= sz)
261 {
262 last = 0;
263 vring->used_wrap_counter ^= 1;
264 }
265 d = &vring->packed_desc[last];
266 flags = d->flags;
267 }
268
269 if (n_buffers)
270 {
271 vlib_buffer_free_from_ring (vm, vring->buffers, start, sz, n_buffers);
272 virtio_memset_ring_u32 (vring->buffers, start, sz, n_buffers);
273 vring->desc_in_use -= n_buffers;
274 vring->last_used_idx = last;
275 }
276}
277
Damjan Marion809d5c92021-03-24 12:24:58 +0100278static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000279virtio_free_used_device_desc (vlib_main_t *vm, vnet_virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100280 uword node_index, int packed)
281{
282 if (packed)
283 virtio_free_used_device_desc_packed (vm, vring, node_index);
284 else
285 virtio_free_used_device_desc_split (vm, vring, node_index);
286
287}
288
Damjan Marion809d5c92021-03-24 12:24:58 +0100289static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000290set_checksum_offsets (vlib_buffer_t *b, vnet_virtio_net_hdr_v1_t *hdr,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100291 const int is_l2)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000292{
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +0200293 vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100294
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000295 if (b->flags & VNET_BUFFER_F_IS_IP4)
296 {
297 ip4_header_t *ip4;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000298 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000299 hdr->csum_start = vnet_buffer (b)->l4_hdr_offset; // 0x22;
Mohsin Kazmif5462362021-02-23 15:55:04 +0100300
301 /*
302 * virtio devices do not support IP4 checksum offload. So driver takes
303 * care of it while doing tx.
304 */
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000305 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
Mohsin Kazmif5462362021-02-23 15:55:04 +0100306 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
307 ip4->checksum = ip4_header_checksum (ip4);
308
309 /*
310 * virtio devices assume the l4 header is set to the checksum of the
311 * l3 pseudo-header, so we compute it before tx-ing
312 */
Mohsin Kazmi68095382021-02-10 11:26:24 +0100313 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000314 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100315 tcp_header_t *tcp =
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000316 (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
Mohsin Kazmif5462362021-02-23 15:55:04 +0100317 tcp->checksum = ip4_pseudo_header_cksum (ip4);
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000318 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
319 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100320 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000321 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100322 udp_header_t *udp =
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000323 (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
Mohsin Kazmif5462362021-02-23 15:55:04 +0100324 udp->checksum = ip4_pseudo_header_cksum (ip4);
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000325 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
326 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000327 }
328 else if (b->flags & VNET_BUFFER_F_IS_IP6)
329 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100330 ip6_header_t *ip6;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000331 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000332 hdr->csum_start = vnet_buffer (b)->l4_hdr_offset; // 0x36;
333 ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
Mohsin Kazmif5462362021-02-23 15:55:04 +0100334
335 /*
336 * virtio devices assume the l4 header is set to the checksum of the
337 * l3 pseudo-header, so we compute it before tx-ing
338 */
Mohsin Kazmi68095382021-02-10 11:26:24 +0100339 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000340 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100341 tcp_header_t *tcp =
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000342 (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
Mohsin Kazmif5462362021-02-23 15:55:04 +0100343 tcp->checksum = ip6_pseudo_header_cksum (ip6);
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000344 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
345 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100346 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000347 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100348 udp_header_t *udp =
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000349 (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
Mohsin Kazmif5462362021-02-23 15:55:04 +0100350 udp->checksum = ip6_pseudo_header_cksum (ip6);
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000351 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
352 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000353 }
354}
355
Damjan Marion809d5c92021-03-24 12:24:58 +0100356static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000357set_gso_offsets (vlib_buffer_t *b, vnet_virtio_net_hdr_v1_t *hdr,
358 const int is_l2)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200359{
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +0200360 vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100361
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200362 if (b->flags & VNET_BUFFER_F_IS_IP4)
363 {
364 ip4_header_t *ip4;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200365 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
366 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000367 hdr->hdr_len = vnet_buffer (b)->l4_hdr_offset -
368 vnet_buffer (b)->l2_hdr_offset +
369 vnet_buffer2 (b)->gso_l4_hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200370 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000371 hdr->csum_start = vnet_buffer (b)->l4_hdr_offset; // 0x22;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200372 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000373 ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200374 /*
375 * virtio devices do not support IP4 checksum offload. So driver takes care
376 * of it while doing tx.
377 */
Mohsin Kazmi68095382021-02-10 11:26:24 +0100378 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200379 ip4->checksum = ip4_header_checksum (ip4);
380 }
381 else if (b->flags & VNET_BUFFER_F_IS_IP6)
382 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200383 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
384 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000385 hdr->hdr_len = vnet_buffer (b)->l4_hdr_offset -
386 vnet_buffer (b)->l2_hdr_offset +
387 vnet_buffer2 (b)->gso_l4_hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200388 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Mohsin Kazmi096c8cc2022-05-18 16:51:57 +0000389 hdr->csum_start = vnet_buffer (b)->l4_hdr_offset; // 0x36;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200390 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
391 }
392}
393
Damjan Marion809d5c92021-03-24 12:24:58 +0100394static u16
395add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000396 virtio_if_t *vif, vnet_virtio_vring_t *vring, u32 bi,
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200397 u16 free_desc_count, u16 avail, u16 next, u16 mask,
398 int hdr_sz, int do_gso, int csum_offload, int is_pci,
399 int is_tun, int is_indirect, int is_any_layout)
Damjan Marion8389fb92017-10-13 18:29:53 +0200400{
401 u16 n_added = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000402 vnet_virtio_vring_desc_t *d;
Damjan Mariond4a54712020-10-31 23:40:51 +0100403 int is_l2 = !is_tun;
Damjan Marion8389fb92017-10-13 18:29:53 +0200404 d = &vring->desc[next];
405 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000406 vnet_virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
Damjan Mariond4a54712020-10-31 23:40:51 +0100407 u32 drop_inline = ~0;
Damjan Marion508cabc2018-02-08 19:49:22 +0100408
Damjan Mariond4a54712020-10-31 23:40:51 +0100409 clib_memset_u8 (hdr, 0, hdr_sz);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000410
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200411 if (b->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200412 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200413 if (do_gso)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200414 set_gso_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200415 else
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200416 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100417 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
418 goto done;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200419 }
420 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100421 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000422 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200423 if (csum_offload)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200424 set_checksum_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200425 else
426 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100427 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
428 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200429 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000430 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200431
Mohsin Kazmi83143712020-10-07 13:23:55 +0200432 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
433 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100434 virtio_tx_trace (vm, node, b, bi, is_tun);
Mohsin Kazmi83143712020-10-07 13:23:55 +0200435 }
436
Damjan Marion8389fb92017-10-13 18:29:53 +0200437 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
438 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100439 d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) :
440 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200441 d->len = b->current_length + hdr_sz;
442 d->flags = 0;
443 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100444 else if (is_indirect)
Damjan Marion8389fb92017-10-13 18:29:53 +0200445 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200446 /*
447 * We are using single vlib_buffer_t for indirect descriptor(s)
448 * chain. Single descriptor is 16 bytes and vlib_buffer_t
449 * has 2048 bytes space. So maximum long chain can have 128
450 * (=2048/16) indirect descriptors.
451 * It can easily support 65535 bytes of Jumbo frames with
452 * each data buffer size of 512 bytes minimum.
453 */
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200454 u32 indirect_buffer = 0;
455 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200456 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100457 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
458 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200459 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200460
461 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200462 indirect_desc->current_data = 0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200463 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
464 indirect_desc->next_buffer = bi;
465 bi = indirect_buffer;
Damjan Marion8389fb92017-10-13 18:29:53 +0200466
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000467 vnet_virtio_vring_desc_t *id =
468 (vnet_virtio_vring_desc_t *) vlib_buffer_get_current (indirect_desc);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200469 u32 count = 1;
Damjan Mariond4a54712020-10-31 23:40:51 +0100470 if (is_pci)
Damjan Marion8389fb92017-10-13 18:29:53 +0200471 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200472 d->addr = vlib_physmem_get_pa (vm, id);
473 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200474
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200475 /*
476 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
477 * should be presented in separate descriptor and data will start
478 * from next descriptor.
479 */
Damjan Mariond4a54712020-10-31 23:40:51 +0100480 if (is_any_layout)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200481 id->len = b->current_length + hdr_sz;
482 else
483 {
484 id->len = hdr_sz;
485 id->flags = VRING_DESC_F_NEXT;
486 id->next = count;
487 count++;
488 id++;
489 id->addr = vlib_buffer_get_current_pa (vm, b);
490 id->len = b->current_length;
491 }
492 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
493 {
494 id->flags = VRING_DESC_F_NEXT;
495 id->next = count;
496 count++;
497 id++;
498 b = vlib_get_buffer (vm, b->next_buffer);
499 id->addr = vlib_buffer_get_current_pa (vm, b);
500 id->len = b->current_length;
Mohsin Kazmi637c97c2021-05-12 14:51:29 +0200501 if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN))
502 {
503 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
504 vlib_error_count (vm, node->node_index,
505 VIRTIO_TX_ERROR_TRUNC_PACKET, 1);
506 break;
507 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200508 }
509 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200510 else /* VIRTIO_IF_TYPE_[TAP | TUN] */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200511 {
512 d->addr = pointer_to_uword (id);
513 /* first buffer in chain */
514 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
515 id->len = b->current_length + hdr_sz;
516
517 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
518 {
519 id->flags = VRING_DESC_F_NEXT;
520 id->next = count;
521 count++;
522 id++;
523 b = vlib_get_buffer (vm, b->next_buffer);
524 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
525 id->len = b->current_length;
Mohsin Kazmi637c97c2021-05-12 14:51:29 +0200526 if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN))
527 {
528 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
529 vlib_error_count (vm, node->node_index,
530 VIRTIO_TX_ERROR_TRUNC_PACKET, 1);
531 break;
532 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200533 }
534 }
535 id->flags = 0;
536 id->next = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000537 d->len = count * sizeof (vnet_virtio_vring_desc_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200538 d->flags = VRING_DESC_F_INDIRECT;
539 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100540 else if (is_pci)
Mohsin Kazmice215992020-04-10 14:57:06 +0000541 {
542 u16 count = next;
543 vlib_buffer_t *b_temp = b;
544 u16 n_buffers_in_chain = 1;
545
546 /*
547 * Check the length of the chain for the required number of
548 * descriptors. Return from here, retry to get more descriptors,
549 * if chain length is greater than available descriptors.
550 */
551 while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
552 {
553 n_buffers_in_chain++;
554 b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
555 }
556
557 if (n_buffers_in_chain > free_desc_count)
558 return n_buffers_in_chain;
559
560 d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
561 d->len = b->current_length + hdr_sz;
562
563 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
564 {
565 d->flags = VRING_DESC_F_NEXT;
566 vring->buffers[count] = bi;
567 b->flags &=
568 ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
569 bi = b->next_buffer;
570 b->next_buffer = 0;
571 n_added++;
572 count = (count + 1) & mask;
573 d->next = count;
574 d = &vring->desc[count];
575 b = vlib_get_buffer (vm, bi);
576 d->addr = vlib_buffer_get_current_pa (vm, b);
577 d->len = b->current_length;
578 }
579 d->flags = 0;
580 vring->buffers[count] = bi;
581 vring->avail->ring[avail & mask] = next;
582 n_added++;
583 return n_added;
584 }
585 else
586 {
587 ASSERT (0);
588 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200589 vring->buffers[next] = bi;
590 vring->avail->ring[avail & mask] = next;
591 n_added++;
Damjan Mariond4a54712020-10-31 23:40:51 +0100592
593done:
594 if (drop_inline != ~0)
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200595 virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
596 drop_inline);
Damjan Mariond4a54712020-10-31 23:40:51 +0100597
Damjan Marion8389fb92017-10-13 18:29:53 +0200598 return n_added;
599}
600
Damjan Marion809d5c92021-03-24 12:24:58 +0100601static u16
602add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000603 virtio_if_t *vif, vnet_virtio_vring_t *vring,
604 u32 bi, u16 next, int hdr_sz, int do_gso,
605 int csum_offload, int is_pci, int is_tun,
606 int is_indirect, int is_any_layout)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100607{
608 u16 n_added = 0, flags = 0;
609 int is_l2 = !is_tun;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000610 vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[next];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100611 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000612 vnet_virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100613 u32 drop_inline = ~0;
614
615 clib_memset (hdr, 0, hdr_sz);
616
617 if (b->flags & VNET_BUFFER_F_GSO)
618 {
619 if (do_gso)
620 set_gso_offsets (b, hdr, is_l2);
621 else
622 {
623 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
624 goto done;
625 }
626 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100627 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100628 {
629 if (csum_offload)
630 set_checksum_offsets (b, hdr, is_l2);
631 else
632 {
633 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
634 goto done;
635 }
636 }
637 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
638 {
639 virtio_tx_trace (vm, node, b, bi, is_tun);
640 }
641
642 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
643 {
644 d->addr =
645 ((is_pci) ? vlib_buffer_get_current_pa (vm,
646 b) :
647 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
648 d->len = b->current_length + hdr_sz;
649 }
650 else if (is_indirect)
651 {
652 /*
653 * We are using single vlib_buffer_t for indirect descriptor(s)
654 * chain. Single descriptor is 16 bytes and vlib_buffer_t
655 * has 2048 bytes space. So maximum long chain can have 128
656 * (=2048/16) indirect descriptors.
657 * It can easily support 65535 bytes of Jumbo frames with
658 * each data buffer size of 512 bytes minimum.
659 */
660 u32 indirect_buffer = 0;
661 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
662 {
663 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
664 goto done;
665 }
666
667 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
668 indirect_desc->current_data = 0;
669 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
670 indirect_desc->next_buffer = bi;
671 bi = indirect_buffer;
672
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000673 vnet_virtio_vring_packed_desc_t *id =
674 (vnet_virtio_vring_packed_desc_t *) vlib_buffer_get_current (
675 indirect_desc);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100676 u32 count = 1;
677 if (is_pci)
678 {
679 d->addr = vlib_physmem_get_pa (vm, id);
680 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
681
682 /*
683 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
684 * should be presented in separate descriptor and data will start
685 * from next descriptor.
686 */
687 if (is_any_layout)
688 id->len = b->current_length + hdr_sz;
689 else
690 {
691 id->len = hdr_sz;
692 id->flags = 0;
693 id->id = 0;
694 count++;
695 id++;
696 id->addr = vlib_buffer_get_current_pa (vm, b);
697 id->len = b->current_length;
698 }
699 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
700 {
701 id->flags = 0;
702 id->id = 0;
703 count++;
704 id++;
705 b = vlib_get_buffer (vm, b->next_buffer);
706 id->addr = vlib_buffer_get_current_pa (vm, b);
707 id->len = b->current_length;
Mohsin Kazmi637c97c2021-05-12 14:51:29 +0200708 if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN))
709 {
710 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
711 vlib_error_count (vm, node->node_index,
712 VIRTIO_TX_ERROR_TRUNC_PACKET, 1);
713 break;
714 }
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100715 }
716 }
717 id->flags = 0;
718 id->id = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000719 d->len = count * sizeof (vnet_virtio_vring_packed_desc_t);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100720 flags = VRING_DESC_F_INDIRECT;
721 }
722 else
723 {
724 ASSERT (0);
725 }
726 if (vring->avail_wrap_counter)
727 {
728 flags |= VRING_DESC_F_AVAIL;
729 flags &= ~VRING_DESC_F_USED;
730 }
731 else
732 {
733 flags &= ~VRING_DESC_F_AVAIL;
734 flags |= VRING_DESC_F_USED;
735 }
736
737 d->id = next;
738 d->flags = flags;
739 vring->buffers[next] = bi;
740 n_added++;
741
742done:
743 if (drop_inline != ~0)
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200744 virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
745 drop_inline);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100746
747 return n_added;
748}
749
Damjan Marion809d5c92021-03-24 12:24:58 +0100750static uword
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000751virtio_interface_tx_packed_gso_inline (
752 vlib_main_t *vm, vlib_node_runtime_t *node, virtio_if_t *vif,
753 virtio_if_type_t type, vnet_virtio_vring_t *vring, u32 *buffers, u16 n_left,
754 const int do_gso, const int csum_offload)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100755{
756 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
757 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
758 int is_indirect =
759 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
760 int is_any_layout =
761 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
762 const int hdr_sz = vif->virtio_net_hdr_sz;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000763 u16 sz = vring->queue_size;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100764 u16 used, next, n_buffers = 0, n_buffers_left = 0;
765 u16 n_vectors = n_left;
766
767
768 used = vring->desc_in_use;
769 next = vring->desc_next;
770
771 if (vif->packet_buffering)
772 {
773 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
774
775 while (n_buffers_left && used < sz)
776 {
777 u16 n_added = 0;
778
779 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
780 if (bi == ~0)
781 break;
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200782 n_added = add_buffer_to_slot_packed (
783 vm, node, vif, vring, bi, next, hdr_sz, do_gso, csum_offload,
784 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100785 n_buffers_left--;
786 if (PREDICT_FALSE (n_added == 0))
787 continue;
788
789 used++;
790 next++;
791 if (next >= sz)
792 {
793 next = 0;
794 vring->avail_wrap_counter ^= 1;
795 }
796 }
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000797 virtio_txq_clear_scheduled (vring);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100798 }
799
800 while (n_left && used < sz)
801 {
802 u16 n_added = 0;
803
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200804 n_added = add_buffer_to_slot_packed (
805 vm, node, vif, vring, buffers[0], next, hdr_sz, do_gso, csum_offload,
806 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100807 buffers++;
808 n_left--;
809 if (PREDICT_FALSE (n_added == 0))
810 continue;
811
812 used++;
813 next++;
814 if (next >= sz)
815 {
816 next = 0;
817 vring->avail_wrap_counter ^= 1;
818 }
819 }
820
821 if (n_left != n_vectors || n_buffers != n_buffers_left)
822 {
823 CLIB_MEMORY_STORE_BARRIER ();
824 vring->desc_next = next;
825 vring->desc_in_use = used;
826 CLIB_MEMORY_BARRIER ();
827 if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
828 virtio_kick (vm, vring, vif);
829 }
830
831 return n_left;
832}
833
Damjan Marion809d5c92021-03-24 12:24:58 +0100834static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000835virtio_find_free_desc (vnet_virtio_vring_t *vring, u16 size, u16 mask, u16 req,
Damjan Marion809d5c92021-03-24 12:24:58 +0100836 u16 next, u32 *first_free_desc_index,
837 u16 *free_desc_count)
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100838{
839 u16 start = 0;
840 /* next is used as hint: from where to start looking */
841 for (u16 i = 0; i < size; i++, next++)
842 {
843 if (vring->buffers[next & mask] == ~0)
844 {
845 if (*first_free_desc_index == ~0)
846 {
847 *first_free_desc_index = (next & mask);
848 start = i;
849 (*free_desc_count)++;
850 req--;
851 if (req == 0)
852 break;
853 }
854 else
855 {
856 if (start + *free_desc_count == i)
857 {
858 (*free_desc_count)++;
859 req--;
860 if (req == 0)
861 break;
862 }
863 else
864 break;
865 }
866 }
867 }
868}
869
Damjan Marion809d5c92021-03-24 12:24:58 +0100870static u16
871virtio_interface_tx_split_gso_inline (vlib_main_t *vm,
872 vlib_node_runtime_t *node,
873 virtio_if_t *vif, virtio_if_type_t type,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000874 vnet_virtio_vring_t *vring, u32 *buffers,
Damjan Marion809d5c92021-03-24 12:24:58 +0100875 u16 n_left, int do_gso, int csum_offload)
Damjan Marion8389fb92017-10-13 18:29:53 +0200876{
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000877 u16 used, next, avail, n_buffers = 0, n_buffers_left = 0;
Damjan Mariond4a54712020-10-31 23:40:51 +0100878 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
879 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
880 int is_indirect =
881 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
882 int is_any_layout =
883 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000884 u16 sz = vring->queue_size;
Damjan Mariond4a54712020-10-31 23:40:51 +0100885 int hdr_sz = vif->virtio_net_hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200886 u16 mask = sz - 1;
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100887 u16 n_vectors = n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200888
889 used = vring->desc_in_use;
890 next = vring->desc_next;
891 avail = vring->avail->idx;
892
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100893 u16 free_desc_count = 0;
894
895 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
896 {
897 u32 first_free_desc_index = ~0;
898
899 virtio_find_free_desc (vring, sz, mask, n_left, next,
900 &first_free_desc_index, &free_desc_count);
901
902 if (free_desc_count)
903 next = first_free_desc_index;
904 }
905 else
906 free_desc_count = sz - used;
907
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000908 if (vif->packet_buffering)
909 {
910 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
911
912 while (n_buffers_left && free_desc_count)
913 {
914 u16 n_added = 0;
915
916 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
917 if (bi == ~0)
918 break;
Mohsin Kazmi83143712020-10-07 13:23:55 +0200919
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200920 n_added = add_buffer_to_slot (vm, node, vif, vring, bi,
921 free_desc_count, avail, next, mask,
922 hdr_sz, do_gso, csum_offload, is_pci,
923 is_tun, is_indirect, is_any_layout);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000924 if (PREDICT_FALSE (n_added == 0))
925 {
926 n_buffers_left--;
927 continue;
928 }
929 else if (PREDICT_FALSE (n_added > free_desc_count))
930 break;
931
932 avail++;
933 next = (next + n_added) & mask;
934 used += n_added;
935 n_buffers_left--;
936 free_desc_count -= n_added;
937 }
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000938 virtio_txq_clear_scheduled (vring);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000939 }
940
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100941 while (n_left && free_desc_count)
Damjan Marion8389fb92017-10-13 18:29:53 +0200942 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200943 u16 n_added = 0;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +0000944
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200945 n_added =
946 add_buffer_to_slot (vm, node, vif, vring, buffers[0], free_desc_count,
947 avail, next, mask, hdr_sz, do_gso, csum_offload,
948 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200949
950 if (PREDICT_FALSE (n_added == 0))
951 {
952 buffers++;
953 n_left--;
954 continue;
955 }
Mohsin Kazmice215992020-04-10 14:57:06 +0000956 else if (PREDICT_FALSE (n_added > free_desc_count))
957 break;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200958
Mohsin Kazmice215992020-04-10 14:57:06 +0000959 avail++;
Damjan Marion8389fb92017-10-13 18:29:53 +0200960 next = (next + n_added) & mask;
961 used += n_added;
962 buffers++;
963 n_left--;
Mohsin Kazmice215992020-04-10 14:57:06 +0000964 free_desc_count -= n_added;
Damjan Marion8389fb92017-10-13 18:29:53 +0200965 }
966
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100967 if (n_left != n_vectors || n_buffers != n_buffers_left)
Damjan Marion8389fb92017-10-13 18:29:53 +0200968 {
Mohsin Kazmid8b7dec2020-12-10 09:49:21 +0000969 clib_atomic_store_seq_cst (&vring->avail->idx, avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200970 vring->desc_next = next;
971 vring->desc_in_use = used;
Mohsin Kazmid8b7dec2020-12-10 09:49:21 +0000972 if ((clib_atomic_load_seq_cst (&vring->used->flags) &
973 VRING_USED_F_NO_NOTIFY) == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200974 virtio_kick (vm, vring, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200975 }
976
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100977 return n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200978}
979
Damjan Marion809d5c92021-03-24 12:24:58 +0100980static u16
981virtio_interface_tx_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
982 virtio_if_t *vif, virtio_if_type_t type,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000983 vnet_virtio_vring_t *vring, u32 *buffers,
Damjan Marion809d5c92021-03-24 12:24:58 +0100984 u16 n_left, int packed, int do_gso,
985 int csum_offload)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100986{
987 if (packed)
988 return virtio_interface_tx_packed_gso_inline (vm, node, vif, type, vring,
989 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +0100990 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100991 else
992 return virtio_interface_tx_split_gso_inline (vm, node, vif, type, vring,
993 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +0100994 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100995}
996
Damjan Marion809d5c92021-03-24 12:24:58 +0100997static u16
998virtio_interface_tx_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000999 virtio_if_t *vif, vnet_virtio_vring_t *vring,
Damjan Marion809d5c92021-03-24 12:24:58 +01001000 virtio_if_type_t type, u32 *buffers, u16 n_left,
1001 int packed)
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001002{
1003 vnet_main_t *vnm = vnet_get_main ();
1004 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1005
Damjan Mariond4f88cc2022-01-05 01:52:38 +01001006 if (hw->caps & VNET_HW_IF_CAP_TCP_GSO)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001007 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001008 buffers, n_left, packed,
1009 1 /* do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +02001010 1 /* checksum offload */ );
Damjan Mariond4f88cc2022-01-05 01:52:38 +01001011 else if (hw->caps & VNET_HW_IF_CAP_L4_TX_CKSUM)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001012 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001013 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001014 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +02001015 1 /* checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001016 else
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001017 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001018 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001019 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +02001020 0 /* no checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001021}
1022
Filip Tehlaraee73642019-03-13 05:50:44 -07001023VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
1024 vlib_node_runtime_t * node,
1025 vlib_frame_t * frame)
Damjan Marion8389fb92017-10-13 18:29:53 +02001026{
1027 virtio_main_t *nm = &virtio_main;
1028 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
1029 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001030 vnet_hw_if_tx_frame_t *tf = vlib_frame_scalar_args (frame);
1031 u16 qid = tf->queue_id;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001032 vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001033 u16 n_left = frame->n_vectors;
1034 u32 *buffers = vlib_frame_vector_args (frame);
1035 u32 to[GRO_TO_VECTOR_SIZE (n_left)];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001036 int packed = vif->is_packed;
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001037 u16 n_vectors = frame->n_vectors;
Damjan Mariondc181242020-10-20 12:11:06 +02001038
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001039 if (tf->shared_queue)
1040 clib_spinlock_lock (&vring->lockp);
Damjan Mariondc181242020-10-20 12:11:06 +02001041
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001042 if (vif->packet_coalesce)
1043 {
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001044 n_vectors = n_left =
1045 vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001046 buffers = to;
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001047 virtio_txq_clear_scheduled (vring);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001048 }
1049
Damjan Marion587f9132020-10-31 22:47:01 +01001050 u16 retry_count = 2;
1051
1052retry:
1053 /* free consumed buffers */
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001054 virtio_free_used_device_desc (vm, vring, node->node_index, packed);
Damjan Marion587f9132020-10-31 22:47:01 +01001055
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001056 if (vif->type == VIRTIO_IF_TYPE_TAP)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001057 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001058 VIRTIO_IF_TYPE_TAP,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001059 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001060 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001061 else if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001062 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001063 VIRTIO_IF_TYPE_PCI,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001064 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001065 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001066 else if (vif->type == VIRTIO_IF_TYPE_TUN)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001067 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001068 VIRTIO_IF_TYPE_TUN,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001069 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001070 n_left, packed);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001071 else
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001072 ASSERT (0);
1073
Damjan Marion587f9132020-10-31 22:47:01 +01001074 if (n_left && retry_count--)
1075 goto retry;
1076
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001077 if (vif->packet_buffering && n_left)
1078 {
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001079 u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering,
1080 &buffers
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001081 [n_vectors
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001082 - n_left],
1083 n_left);
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001084 n_left -= n_buffered;
1085 }
1086 if (n_left)
Mohsin Kazmia02ad342021-05-04 16:36:09 +02001087 virtio_interface_drop_inline (vm, vif, node->node_index,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001088 &buffers[n_vectors - n_left], n_left,
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001089 VIRTIO_TX_ERROR_NO_FREE_SLOTS);
1090
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001091 if (tf->shared_queue)
1092 clib_spinlock_unlock (&vring->lockp);
Damjan Mariondc181242020-10-20 12:11:06 +02001093
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001094 return frame->n_vectors - n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +02001095}
1096
1097static void
1098virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1099 u32 node_index)
1100{
1101 virtio_main_t *apm = &virtio_main;
1102 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1103 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
1104
1105 /* Shut off redirection */
1106 if (node_index == ~0)
1107 {
1108 vif->per_interface_next_index = node_index;
1109 return;
1110 }
1111
1112 vif->per_interface_next_index =
1113 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
1114 node_index);
1115}
1116
1117static void
1118virtio_clear_hw_interface_counters (u32 instance)
1119{
1120 /* Nothing for now */
1121}
1122
Damjan Marion809d5c92021-03-24 12:24:58 +01001123static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001124virtio_set_rx_interrupt (virtio_if_t *vif, vnet_virtio_vring_t *vring)
Mohsin Kazmi24841862020-12-02 16:39:42 +01001125{
1126 if (vif->is_packed)
1127 vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE;
1128 else
1129 vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
1130}
1131
Damjan Marion809d5c92021-03-24 12:24:58 +01001132static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001133virtio_set_rx_polling (virtio_if_t *vif, vnet_virtio_vring_t *vring)
Mohsin Kazmi24841862020-12-02 16:39:42 +01001134{
1135 if (vif->is_packed)
1136 vring->driver_event->flags |= VRING_EVENT_F_DISABLE;
1137 else
1138 vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
1139}
1140
Damjan Marion8389fb92017-10-13 18:29:53 +02001141static clib_error_t *
1142virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
Damjan Marioneabd4242020-10-07 20:59:07 +02001143 vnet_hw_if_rx_mode mode)
Damjan Marion8389fb92017-10-13 18:29:53 +02001144{
1145 virtio_main_t *mm = &virtio_main;
1146 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1147 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001148 vnet_virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
Damjan Marion8389fb92017-10-13 18:29:53 +02001149
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001150 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
1151 {
Mohsin Kazmi24841862020-12-02 16:39:42 +01001152 virtio_set_rx_polling (vif, rx_vring);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001153 return clib_error_return (0, "interrupt mode is not supported");
1154 }
1155
Damjan Marioneabd4242020-10-07 20:59:07 +02001156 if (mode == VNET_HW_IF_RX_MODE_POLLING)
Mohsin Kazmi24841862020-12-02 16:39:42 +01001157 virtio_set_rx_polling (vif, rx_vring);
Damjan Marion8389fb92017-10-13 18:29:53 +02001158 else
Mohsin Kazmi24841862020-12-02 16:39:42 +01001159 virtio_set_rx_interrupt (vif, rx_vring);
Damjan Marion8389fb92017-10-13 18:29:53 +02001160
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001161 rx_vring->mode = mode;
1162
Damjan Marion8389fb92017-10-13 18:29:53 +02001163 return 0;
1164}
1165
1166static clib_error_t *
1167virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1168{
1169 virtio_main_t *mm = &virtio_main;
1170 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1171 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +02001172
1173 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001174 {
1175 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1176 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1177 VNET_HW_INTERFACE_FLAG_LINK_UP);
1178 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001179 else
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001180 {
1181 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
1182 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1183 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001184 return 0;
1185}
1186
1187static clib_error_t *
1188virtio_subif_add_del_function (vnet_main_t * vnm,
1189 u32 hw_if_index,
1190 struct vnet_sw_interface_t *st, int is_add)
1191{
1192 /* Nothing for now */
1193 return 0;
1194}
1195
1196/* *INDENT-OFF* */
1197VNET_DEVICE_CLASS (virtio_device_class) = {
1198 .name = "virtio",
Damjan Marion8389fb92017-10-13 18:29:53 +02001199 .format_device_name = format_virtio_device_name,
1200 .format_device = format_virtio_device,
1201 .format_tx_trace = format_virtio_tx_trace,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001202 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +02001203 .tx_function_error_strings = virtio_tx_func_error_strings,
1204 .rx_redirect_to_node = virtio_set_interface_next_node,
1205 .clear_counters = virtio_clear_hw_interface_counters,
1206 .admin_up_down_function = virtio_interface_admin_up_down,
1207 .subif_add_del_function = virtio_subif_add_del_function,
1208 .rx_mode_change_function = virtio_interface_rx_mode_change,
1209};
Mohsin Kazmi206acf82020-04-06 14:19:54 +02001210
Damjan Marion8389fb92017-10-13 18:29:53 +02001211/* *INDENT-ON* */
1212
1213/*
1214 * fd.io coding-style-patch-verification: ON
1215 *
1216 * Local Variables:
1217 * eval: (c-set-style "gnu")
1218 * End:
1219 */