blob: bd06d0fb64804f67cf2605e1b5d734c5be6a2200 [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;
Damjan Mariond4a54712020-10-31 23:40:51 +0100112 if (is_tun)
Mohsin Kazmi03b26952020-09-16 16:53:16 +0000113 {
114 int is_ip4 = 0, is_ip6 = 0;
115
116 switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0)
117 {
118 case 0x40:
119 is_ip4 = 1;
120 break;
121 case 0x60:
122 is_ip6 = 1;
123 break;
124 default:
125 break;
126 }
127 vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4, is_ip6);
128 }
129 else
130 vnet_generic_header_offset_parser (b0, &t->gho, 1,
131 b0->flags &
132 VNET_BUFFER_F_IS_IP4,
133 b0->flags & VNET_BUFFER_F_IS_IP6);
134
135 clib_memcpy_fast (&t->buffer, b0, sizeof (*b0) - sizeof (b0->pre_data));
136 clib_memcpy_fast (t->buffer.pre_data, vlib_buffer_get_current (b0),
137 sizeof (t->buffer.pre_data));
138}
139
Damjan Marion809d5c92021-03-24 12:24:58 +0100140static void
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200141virtio_interface_drop_inline (vlib_main_t *vm, virtio_if_t *vif,
142 uword node_index, u32 *buffers, u16 n,
143 virtio_tx_func_error_t error)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200144{
145 vlib_error_count (vm, node_index, error, n);
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200146 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
147 VNET_INTERFACE_COUNTER_DROP,
148 vm->thread_index, vif->sw_if_index, n);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200149 vlib_buffer_free (vm, buffers, n);
150}
151
Damjan Marion809d5c92021-03-24 12:24:58 +0100152static void
153virtio_memset_ring_u32 (u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100154{
155 ASSERT (n_buffers <= ring_size);
156
157 if (PREDICT_TRUE (start + n_buffers <= ring_size))
158 {
159 clib_memset_u32 (ring + start, ~0, n_buffers);
160 }
161 else
162 {
163 clib_memset_u32 (ring + start, ~0, ring_size - start);
164 clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
165 }
166}
167
Damjan Marion809d5c92021-03-24 12:24:58 +0100168static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000169virtio_free_used_device_desc_split (vlib_main_t *vm,
170 vnet_virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100171 uword node_index)
Damjan Marion8389fb92017-10-13 18:29:53 +0200172{
173 u16 used = vring->desc_in_use;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000174 u16 sz = vring->queue_size;
Damjan Marion8389fb92017-10-13 18:29:53 +0200175 u16 mask = sz - 1;
176 u16 last = vring->last_used_idx;
177 u16 n_left = vring->used->idx - last;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100178 u16 out_of_order_count = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200179
180 if (n_left == 0)
181 return;
182
183 while (n_left)
184 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000185 vnet_virtio_vring_used_elem_t *e = &vring->used->ring[last & mask];
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200186 u16 slot, n_buffers;
187 slot = n_buffers = e->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200188
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100189 while (e->id == (n_buffers & mask))
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200190 {
191 n_left--;
192 last++;
193 n_buffers++;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000194 vnet_virtio_vring_desc_t *d = &vring->desc[e->id];
Mohsin Kazmice215992020-04-10 14:57:06 +0000195 u16 next;
196 while (d->flags & VRING_DESC_F_NEXT)
197 {
198 n_buffers++;
199 next = d->next;
200 d = &vring->desc[next];
201 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200202 if (n_left == 0)
203 break;
204 e = &vring->used->ring[last & mask];
205 }
206 vlib_buffer_free_from_ring (vm, vring->buffers, slot,
207 sz, (n_buffers - slot));
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100208 virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200209 used -= (n_buffers - slot);
210
211 if (n_left > 0)
212 {
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100213 vlib_buffer_free (vm, &vring->buffers[e->id], 1);
214 vring->buffers[e->id] = ~0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200215 used--;
216 last++;
217 n_left--;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100218 out_of_order_count++;
219 vring->flags |= VRING_TX_OUT_OF_ORDER;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200220 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200221 }
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100222
223 /*
224 * Some vhost-backends give buffers back in out-of-order fashion in used ring.
225 * It impacts the overall virtio-performance.
226 */
227 if (out_of_order_count)
228 vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
229 out_of_order_count);
230
Damjan Marion8389fb92017-10-13 18:29:53 +0200231 vring->desc_in_use = used;
232 vring->last_used_idx = last;
233}
234
Damjan Marion809d5c92021-03-24 12:24:58 +0100235static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000236virtio_free_used_device_desc_packed (vlib_main_t *vm,
237 vnet_virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100238 uword node_index)
239{
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000240 vnet_virtio_vring_packed_desc_t *d;
241 u16 sz = vring->queue_size;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100242 u16 last = vring->last_used_idx;
243 u16 n_buffers = 0, start;
244 u16 flags;
245
246 if (vring->desc_in_use == 0)
247 return;
248
249 d = &vring->packed_desc[last];
250 flags = d->flags;
251 start = d->id;
252
253 while ((flags & VRING_DESC_F_AVAIL) == (vring->used_wrap_counter << 7) &&
254 (flags & VRING_DESC_F_USED) == (vring->used_wrap_counter << 15))
255 {
256 last++;
257 n_buffers++;
258
259 if (last >= sz)
260 {
261 last = 0;
262 vring->used_wrap_counter ^= 1;
263 }
264 d = &vring->packed_desc[last];
265 flags = d->flags;
266 }
267
268 if (n_buffers)
269 {
270 vlib_buffer_free_from_ring (vm, vring->buffers, start, sz, n_buffers);
271 virtio_memset_ring_u32 (vring->buffers, start, sz, n_buffers);
272 vring->desc_in_use -= n_buffers;
273 vring->last_used_idx = last;
274 }
275}
276
Damjan Marion809d5c92021-03-24 12:24:58 +0100277static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000278virtio_free_used_device_desc (vlib_main_t *vm, vnet_virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100279 uword node_index, int packed)
280{
281 if (packed)
282 virtio_free_used_device_desc_packed (vm, vring, node_index);
283 else
284 virtio_free_used_device_desc_split (vm, vring, node_index);
285
286}
287
Damjan Marion809d5c92021-03-24 12:24:58 +0100288static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000289set_checksum_offsets (vlib_buffer_t *b, vnet_virtio_net_hdr_v1_t *hdr,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100290 const int is_l2)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000291{
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +0200292 vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100293
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000294 if (b->flags & VNET_BUFFER_F_IS_IP4)
295 {
296 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000297 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200298 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
299 0 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000300 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
301 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
Mohsin Kazmif5462362021-02-23 15:55:04 +0100302
303 /*
304 * virtio devices do not support IP4 checksum offload. So driver takes
305 * care of it while doing tx.
306 */
307 ip4 = (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
308 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
309 ip4->checksum = ip4_header_checksum (ip4);
310
311 /*
312 * virtio devices assume the l4 header is set to the checksum of the
313 * l3 pseudo-header, so we compute it before tx-ing
314 */
Mohsin Kazmi68095382021-02-10 11:26:24 +0100315 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000316 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100317 tcp_header_t *tcp =
318 (tcp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
319 tcp->checksum = ip4_pseudo_header_cksum (ip4);
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000320 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
321 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100322 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000323 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100324 udp_header_t *udp =
325 (udp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
326 udp->checksum = ip4_pseudo_header_cksum (ip4);
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000327 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
328 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000329 }
330 else if (b->flags & VNET_BUFFER_F_IS_IP6)
331 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100332 ip6_header_t *ip6;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000333 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200334 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
335 1 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000336 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
337 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
Mohsin Kazmif5462362021-02-23 15:55:04 +0100338 ip6 = (ip6_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
339
340 /*
341 * virtio devices assume the l4 header is set to the checksum of the
342 * l3 pseudo-header, so we compute it before tx-ing
343 */
Mohsin Kazmi68095382021-02-10 11:26:24 +0100344 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000345 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100346 tcp_header_t *tcp =
347 (tcp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
348 tcp->checksum = ip6_pseudo_header_cksum (ip6);
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000349 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
350 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100351 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000352 {
Mohsin Kazmif5462362021-02-23 15:55:04 +0100353 udp_header_t *udp =
354 (udp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
355 udp->checksum = ip6_pseudo_header_cksum (ip6);
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000356 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
357 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000358 }
359}
360
Damjan Marion809d5c92021-03-24 12:24:58 +0100361static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000362set_gso_offsets (vlib_buffer_t *b, vnet_virtio_net_hdr_v1_t *hdr,
363 const int is_l2)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200364{
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +0200365 vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100366
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200367 if (b->flags & VNET_BUFFER_F_IS_IP4)
368 {
369 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000370 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200371 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
372 0 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200373 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
374 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000375 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200376 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
377 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
378 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
379 ip4 =
380 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
381 /*
382 * virtio devices do not support IP4 checksum offload. So driver takes care
383 * of it while doing tx.
384 */
Mohsin Kazmi68095382021-02-10 11:26:24 +0100385 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200386 ip4->checksum = ip4_header_checksum (ip4);
387 }
388 else if (b->flags & VNET_BUFFER_F_IS_IP6)
389 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000390 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200391 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
392 1 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200393 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
394 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000395 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200396 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
397 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
398 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
399 }
400}
401
Damjan Marion809d5c92021-03-24 12:24:58 +0100402static u16
403add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000404 virtio_if_t *vif, vnet_virtio_vring_t *vring, u32 bi,
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200405 u16 free_desc_count, u16 avail, u16 next, u16 mask,
406 int hdr_sz, int do_gso, int csum_offload, int is_pci,
407 int is_tun, int is_indirect, int is_any_layout)
Damjan Marion8389fb92017-10-13 18:29:53 +0200408{
409 u16 n_added = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000410 vnet_virtio_vring_desc_t *d;
Damjan Mariond4a54712020-10-31 23:40:51 +0100411 int is_l2 = !is_tun;
Damjan Marion8389fb92017-10-13 18:29:53 +0200412 d = &vring->desc[next];
413 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000414 vnet_virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
Damjan Mariond4a54712020-10-31 23:40:51 +0100415 u32 drop_inline = ~0;
Damjan Marion508cabc2018-02-08 19:49:22 +0100416
Damjan Mariond4a54712020-10-31 23:40:51 +0100417 clib_memset_u8 (hdr, 0, hdr_sz);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000418
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200419 if (b->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200420 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200421 if (do_gso)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200422 set_gso_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200423 else
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200424 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100425 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
426 goto done;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200427 }
428 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100429 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000430 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200431 if (csum_offload)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200432 set_checksum_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200433 else
434 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100435 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
436 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200437 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000438 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200439
Mohsin Kazmi83143712020-10-07 13:23:55 +0200440 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
441 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100442 virtio_tx_trace (vm, node, b, bi, is_tun);
Mohsin Kazmi83143712020-10-07 13:23:55 +0200443 }
444
Damjan Marion8389fb92017-10-13 18:29:53 +0200445 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
446 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100447 d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) :
448 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200449 d->len = b->current_length + hdr_sz;
450 d->flags = 0;
451 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100452 else if (is_indirect)
Damjan Marion8389fb92017-10-13 18:29:53 +0200453 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200454 /*
455 * We are using single vlib_buffer_t for indirect descriptor(s)
456 * chain. Single descriptor is 16 bytes and vlib_buffer_t
457 * has 2048 bytes space. So maximum long chain can have 128
458 * (=2048/16) indirect descriptors.
459 * It can easily support 65535 bytes of Jumbo frames with
460 * each data buffer size of 512 bytes minimum.
461 */
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200462 u32 indirect_buffer = 0;
463 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200464 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100465 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
466 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200467 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200468
469 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200470 indirect_desc->current_data = 0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200471 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
472 indirect_desc->next_buffer = bi;
473 bi = indirect_buffer;
Damjan Marion8389fb92017-10-13 18:29:53 +0200474
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000475 vnet_virtio_vring_desc_t *id =
476 (vnet_virtio_vring_desc_t *) vlib_buffer_get_current (indirect_desc);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200477 u32 count = 1;
Damjan Mariond4a54712020-10-31 23:40:51 +0100478 if (is_pci)
Damjan Marion8389fb92017-10-13 18:29:53 +0200479 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200480 d->addr = vlib_physmem_get_pa (vm, id);
481 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200482
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200483 /*
484 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
485 * should be presented in separate descriptor and data will start
486 * from next descriptor.
487 */
Damjan Mariond4a54712020-10-31 23:40:51 +0100488 if (is_any_layout)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200489 id->len = b->current_length + hdr_sz;
490 else
491 {
492 id->len = hdr_sz;
493 id->flags = VRING_DESC_F_NEXT;
494 id->next = count;
495 count++;
496 id++;
497 id->addr = vlib_buffer_get_current_pa (vm, b);
498 id->len = b->current_length;
499 }
500 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
501 {
502 id->flags = VRING_DESC_F_NEXT;
503 id->next = count;
504 count++;
505 id++;
506 b = vlib_get_buffer (vm, b->next_buffer);
507 id->addr = vlib_buffer_get_current_pa (vm, b);
508 id->len = b->current_length;
Mohsin Kazmi637c97c2021-05-12 14:51:29 +0200509 if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN))
510 {
511 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
512 vlib_error_count (vm, node->node_index,
513 VIRTIO_TX_ERROR_TRUNC_PACKET, 1);
514 break;
515 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200516 }
517 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200518 else /* VIRTIO_IF_TYPE_[TAP | TUN] */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200519 {
520 d->addr = pointer_to_uword (id);
521 /* first buffer in chain */
522 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
523 id->len = b->current_length + hdr_sz;
524
525 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
526 {
527 id->flags = VRING_DESC_F_NEXT;
528 id->next = count;
529 count++;
530 id++;
531 b = vlib_get_buffer (vm, b->next_buffer);
532 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
533 id->len = b->current_length;
Mohsin Kazmi637c97c2021-05-12 14:51:29 +0200534 if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN))
535 {
536 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
537 vlib_error_count (vm, node->node_index,
538 VIRTIO_TX_ERROR_TRUNC_PACKET, 1);
539 break;
540 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200541 }
542 }
543 id->flags = 0;
544 id->next = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000545 d->len = count * sizeof (vnet_virtio_vring_desc_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200546 d->flags = VRING_DESC_F_INDIRECT;
547 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100548 else if (is_pci)
Mohsin Kazmice215992020-04-10 14:57:06 +0000549 {
550 u16 count = next;
551 vlib_buffer_t *b_temp = b;
552 u16 n_buffers_in_chain = 1;
553
554 /*
555 * Check the length of the chain for the required number of
556 * descriptors. Return from here, retry to get more descriptors,
557 * if chain length is greater than available descriptors.
558 */
559 while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
560 {
561 n_buffers_in_chain++;
562 b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
563 }
564
565 if (n_buffers_in_chain > free_desc_count)
566 return n_buffers_in_chain;
567
568 d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
569 d->len = b->current_length + hdr_sz;
570
571 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
572 {
573 d->flags = VRING_DESC_F_NEXT;
574 vring->buffers[count] = bi;
575 b->flags &=
576 ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
577 bi = b->next_buffer;
578 b->next_buffer = 0;
579 n_added++;
580 count = (count + 1) & mask;
581 d->next = count;
582 d = &vring->desc[count];
583 b = vlib_get_buffer (vm, bi);
584 d->addr = vlib_buffer_get_current_pa (vm, b);
585 d->len = b->current_length;
586 }
587 d->flags = 0;
588 vring->buffers[count] = bi;
589 vring->avail->ring[avail & mask] = next;
590 n_added++;
591 return n_added;
592 }
593 else
594 {
595 ASSERT (0);
596 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200597 vring->buffers[next] = bi;
598 vring->avail->ring[avail & mask] = next;
599 n_added++;
Damjan Mariond4a54712020-10-31 23:40:51 +0100600
601done:
602 if (drop_inline != ~0)
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200603 virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
604 drop_inline);
Damjan Mariond4a54712020-10-31 23:40:51 +0100605
Damjan Marion8389fb92017-10-13 18:29:53 +0200606 return n_added;
607}
608
Damjan Marion809d5c92021-03-24 12:24:58 +0100609static u16
610add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000611 virtio_if_t *vif, vnet_virtio_vring_t *vring,
612 u32 bi, u16 next, int hdr_sz, int do_gso,
613 int csum_offload, int is_pci, int is_tun,
614 int is_indirect, int is_any_layout)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100615{
616 u16 n_added = 0, flags = 0;
617 int is_l2 = !is_tun;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000618 vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[next];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100619 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000620 vnet_virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100621 u32 drop_inline = ~0;
622
623 clib_memset (hdr, 0, hdr_sz);
624
625 if (b->flags & VNET_BUFFER_F_GSO)
626 {
627 if (do_gso)
628 set_gso_offsets (b, hdr, is_l2);
629 else
630 {
631 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
632 goto done;
633 }
634 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100635 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100636 {
637 if (csum_offload)
638 set_checksum_offsets (b, hdr, is_l2);
639 else
640 {
641 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
642 goto done;
643 }
644 }
645 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
646 {
647 virtio_tx_trace (vm, node, b, bi, is_tun);
648 }
649
650 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
651 {
652 d->addr =
653 ((is_pci) ? vlib_buffer_get_current_pa (vm,
654 b) :
655 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
656 d->len = b->current_length + hdr_sz;
657 }
658 else if (is_indirect)
659 {
660 /*
661 * We are using single vlib_buffer_t for indirect descriptor(s)
662 * chain. Single descriptor is 16 bytes and vlib_buffer_t
663 * has 2048 bytes space. So maximum long chain can have 128
664 * (=2048/16) indirect descriptors.
665 * It can easily support 65535 bytes of Jumbo frames with
666 * each data buffer size of 512 bytes minimum.
667 */
668 u32 indirect_buffer = 0;
669 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
670 {
671 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
672 goto done;
673 }
674
675 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
676 indirect_desc->current_data = 0;
677 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
678 indirect_desc->next_buffer = bi;
679 bi = indirect_buffer;
680
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000681 vnet_virtio_vring_packed_desc_t *id =
682 (vnet_virtio_vring_packed_desc_t *) vlib_buffer_get_current (
683 indirect_desc);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100684 u32 count = 1;
685 if (is_pci)
686 {
687 d->addr = vlib_physmem_get_pa (vm, id);
688 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
689
690 /*
691 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
692 * should be presented in separate descriptor and data will start
693 * from next descriptor.
694 */
695 if (is_any_layout)
696 id->len = b->current_length + hdr_sz;
697 else
698 {
699 id->len = hdr_sz;
700 id->flags = 0;
701 id->id = 0;
702 count++;
703 id++;
704 id->addr = vlib_buffer_get_current_pa (vm, b);
705 id->len = b->current_length;
706 }
707 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
708 {
709 id->flags = 0;
710 id->id = 0;
711 count++;
712 id++;
713 b = vlib_get_buffer (vm, b->next_buffer);
714 id->addr = vlib_buffer_get_current_pa (vm, b);
715 id->len = b->current_length;
Mohsin Kazmi637c97c2021-05-12 14:51:29 +0200716 if (PREDICT_FALSE (count == VIRTIO_TX_MAX_CHAIN_LEN))
717 {
718 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
719 vlib_error_count (vm, node->node_index,
720 VIRTIO_TX_ERROR_TRUNC_PACKET, 1);
721 break;
722 }
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100723 }
724 }
725 id->flags = 0;
726 id->id = 0;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000727 d->len = count * sizeof (vnet_virtio_vring_packed_desc_t);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100728 flags = VRING_DESC_F_INDIRECT;
729 }
730 else
731 {
732 ASSERT (0);
733 }
734 if (vring->avail_wrap_counter)
735 {
736 flags |= VRING_DESC_F_AVAIL;
737 flags &= ~VRING_DESC_F_USED;
738 }
739 else
740 {
741 flags &= ~VRING_DESC_F_AVAIL;
742 flags |= VRING_DESC_F_USED;
743 }
744
745 d->id = next;
746 d->flags = flags;
747 vring->buffers[next] = bi;
748 n_added++;
749
750done:
751 if (drop_inline != ~0)
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200752 virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
753 drop_inline);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100754
755 return n_added;
756}
757
Damjan Marion809d5c92021-03-24 12:24:58 +0100758static uword
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000759virtio_interface_tx_packed_gso_inline (
760 vlib_main_t *vm, vlib_node_runtime_t *node, virtio_if_t *vif,
761 virtio_if_type_t type, vnet_virtio_vring_t *vring, u32 *buffers, u16 n_left,
762 const int do_gso, const int csum_offload)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100763{
764 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
765 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
766 int is_indirect =
767 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
768 int is_any_layout =
769 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
770 const int hdr_sz = vif->virtio_net_hdr_sz;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000771 u16 sz = vring->queue_size;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100772 u16 used, next, n_buffers = 0, n_buffers_left = 0;
773 u16 n_vectors = n_left;
774
775
776 used = vring->desc_in_use;
777 next = vring->desc_next;
778
779 if (vif->packet_buffering)
780 {
781 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
782
783 while (n_buffers_left && used < sz)
784 {
785 u16 n_added = 0;
786
787 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
788 if (bi == ~0)
789 break;
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200790 n_added = add_buffer_to_slot_packed (
791 vm, node, vif, vring, bi, next, hdr_sz, do_gso, csum_offload,
792 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100793 n_buffers_left--;
794 if (PREDICT_FALSE (n_added == 0))
795 continue;
796
797 used++;
798 next++;
799 if (next >= sz)
800 {
801 next = 0;
802 vring->avail_wrap_counter ^= 1;
803 }
804 }
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000805 virtio_txq_clear_scheduled (vring);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100806 }
807
808 while (n_left && used < sz)
809 {
810 u16 n_added = 0;
811
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200812 n_added = add_buffer_to_slot_packed (
813 vm, node, vif, vring, buffers[0], next, hdr_sz, do_gso, csum_offload,
814 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100815 buffers++;
816 n_left--;
817 if (PREDICT_FALSE (n_added == 0))
818 continue;
819
820 used++;
821 next++;
822 if (next >= sz)
823 {
824 next = 0;
825 vring->avail_wrap_counter ^= 1;
826 }
827 }
828
829 if (n_left != n_vectors || n_buffers != n_buffers_left)
830 {
831 CLIB_MEMORY_STORE_BARRIER ();
832 vring->desc_next = next;
833 vring->desc_in_use = used;
834 CLIB_MEMORY_BARRIER ();
835 if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
836 virtio_kick (vm, vring, vif);
837 }
838
839 return n_left;
840}
841
Damjan Marion809d5c92021-03-24 12:24:58 +0100842static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000843virtio_find_free_desc (vnet_virtio_vring_t *vring, u16 size, u16 mask, u16 req,
Damjan Marion809d5c92021-03-24 12:24:58 +0100844 u16 next, u32 *first_free_desc_index,
845 u16 *free_desc_count)
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100846{
847 u16 start = 0;
848 /* next is used as hint: from where to start looking */
849 for (u16 i = 0; i < size; i++, next++)
850 {
851 if (vring->buffers[next & mask] == ~0)
852 {
853 if (*first_free_desc_index == ~0)
854 {
855 *first_free_desc_index = (next & mask);
856 start = i;
857 (*free_desc_count)++;
858 req--;
859 if (req == 0)
860 break;
861 }
862 else
863 {
864 if (start + *free_desc_count == i)
865 {
866 (*free_desc_count)++;
867 req--;
868 if (req == 0)
869 break;
870 }
871 else
872 break;
873 }
874 }
875 }
876}
877
Damjan Marion809d5c92021-03-24 12:24:58 +0100878static u16
879virtio_interface_tx_split_gso_inline (vlib_main_t *vm,
880 vlib_node_runtime_t *node,
881 virtio_if_t *vif, virtio_if_type_t type,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000882 vnet_virtio_vring_t *vring, u32 *buffers,
Damjan Marion809d5c92021-03-24 12:24:58 +0100883 u16 n_left, int do_gso, int csum_offload)
Damjan Marion8389fb92017-10-13 18:29:53 +0200884{
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000885 u16 used, next, avail, n_buffers = 0, n_buffers_left = 0;
Damjan Mariond4a54712020-10-31 23:40:51 +0100886 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
887 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
888 int is_indirect =
889 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
890 int is_any_layout =
891 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000892 u16 sz = vring->queue_size;
Damjan Mariond4a54712020-10-31 23:40:51 +0100893 int hdr_sz = vif->virtio_net_hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200894 u16 mask = sz - 1;
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100895 u16 n_vectors = n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200896
897 used = vring->desc_in_use;
898 next = vring->desc_next;
899 avail = vring->avail->idx;
900
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100901 u16 free_desc_count = 0;
902
903 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
904 {
905 u32 first_free_desc_index = ~0;
906
907 virtio_find_free_desc (vring, sz, mask, n_left, next,
908 &first_free_desc_index, &free_desc_count);
909
910 if (free_desc_count)
911 next = first_free_desc_index;
912 }
913 else
914 free_desc_count = sz - used;
915
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000916 if (vif->packet_buffering)
917 {
918 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
919
920 while (n_buffers_left && free_desc_count)
921 {
922 u16 n_added = 0;
923
924 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
925 if (bi == ~0)
926 break;
Mohsin Kazmi83143712020-10-07 13:23:55 +0200927
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200928 n_added = add_buffer_to_slot (vm, node, vif, vring, bi,
929 free_desc_count, avail, next, mask,
930 hdr_sz, do_gso, csum_offload, is_pci,
931 is_tun, is_indirect, is_any_layout);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000932 if (PREDICT_FALSE (n_added == 0))
933 {
934 n_buffers_left--;
935 continue;
936 }
937 else if (PREDICT_FALSE (n_added > free_desc_count))
938 break;
939
940 avail++;
941 next = (next + n_added) & mask;
942 used += n_added;
943 n_buffers_left--;
944 free_desc_count -= n_added;
945 }
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000946 virtio_txq_clear_scheduled (vring);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000947 }
948
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100949 while (n_left && free_desc_count)
Damjan Marion8389fb92017-10-13 18:29:53 +0200950 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200951 u16 n_added = 0;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +0000952
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200953 n_added =
954 add_buffer_to_slot (vm, node, vif, vring, buffers[0], free_desc_count,
955 avail, next, mask, hdr_sz, do_gso, csum_offload,
956 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200957
958 if (PREDICT_FALSE (n_added == 0))
959 {
960 buffers++;
961 n_left--;
962 continue;
963 }
Mohsin Kazmice215992020-04-10 14:57:06 +0000964 else if (PREDICT_FALSE (n_added > free_desc_count))
965 break;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200966
Mohsin Kazmice215992020-04-10 14:57:06 +0000967 avail++;
Damjan Marion8389fb92017-10-13 18:29:53 +0200968 next = (next + n_added) & mask;
969 used += n_added;
970 buffers++;
971 n_left--;
Mohsin Kazmice215992020-04-10 14:57:06 +0000972 free_desc_count -= n_added;
Damjan Marion8389fb92017-10-13 18:29:53 +0200973 }
974
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100975 if (n_left != n_vectors || n_buffers != n_buffers_left)
Damjan Marion8389fb92017-10-13 18:29:53 +0200976 {
Mohsin Kazmid8b7dec2020-12-10 09:49:21 +0000977 clib_atomic_store_seq_cst (&vring->avail->idx, avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200978 vring->desc_next = next;
979 vring->desc_in_use = used;
Mohsin Kazmid8b7dec2020-12-10 09:49:21 +0000980 if ((clib_atomic_load_seq_cst (&vring->used->flags) &
981 VRING_USED_F_NO_NOTIFY) == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200982 virtio_kick (vm, vring, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200983 }
984
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100985 return n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200986}
987
Damjan Marion809d5c92021-03-24 12:24:58 +0100988static u16
989virtio_interface_tx_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
990 virtio_if_t *vif, virtio_if_type_t type,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000991 vnet_virtio_vring_t *vring, u32 *buffers,
Damjan Marion809d5c92021-03-24 12:24:58 +0100992 u16 n_left, int packed, int do_gso,
993 int csum_offload)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100994{
995 if (packed)
996 return virtio_interface_tx_packed_gso_inline (vm, node, vif, type, vring,
997 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +0100998 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100999 else
1000 return virtio_interface_tx_split_gso_inline (vm, node, vif, type, vring,
1001 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +01001002 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001003}
1004
Damjan Marion809d5c92021-03-24 12:24:58 +01001005static u16
1006virtio_interface_tx_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001007 virtio_if_t *vif, vnet_virtio_vring_t *vring,
Damjan Marion809d5c92021-03-24 12:24:58 +01001008 virtio_if_type_t type, u32 *buffers, u16 n_left,
1009 int packed)
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001010{
1011 vnet_main_t *vnm = vnet_get_main ();
1012 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1013
Damjan Mariond4f88cc2022-01-05 01:52:38 +01001014 if (hw->caps & VNET_HW_IF_CAP_TCP_GSO)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001015 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001016 buffers, n_left, packed,
1017 1 /* do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +02001018 1 /* checksum offload */ );
Damjan Mariond4f88cc2022-01-05 01:52:38 +01001019 else if (hw->caps & VNET_HW_IF_CAP_L4_TX_CKSUM)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001020 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001021 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001022 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +02001023 1 /* checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001024 else
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001025 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001026 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001027 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +02001028 0 /* no checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001029}
1030
Filip Tehlaraee73642019-03-13 05:50:44 -07001031VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
1032 vlib_node_runtime_t * node,
1033 vlib_frame_t * frame)
Damjan Marion8389fb92017-10-13 18:29:53 +02001034{
1035 virtio_main_t *nm = &virtio_main;
1036 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
1037 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001038 vnet_hw_if_tx_frame_t *tf = vlib_frame_scalar_args (frame);
1039 u16 qid = tf->queue_id;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001040 vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001041 u16 n_left = frame->n_vectors;
1042 u32 *buffers = vlib_frame_vector_args (frame);
1043 u32 to[GRO_TO_VECTOR_SIZE (n_left)];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001044 int packed = vif->is_packed;
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001045 u16 n_vectors = frame->n_vectors;
Damjan Mariondc181242020-10-20 12:11:06 +02001046
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001047 if (tf->shared_queue)
1048 clib_spinlock_lock (&vring->lockp);
Damjan Mariondc181242020-10-20 12:11:06 +02001049
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001050 if (vif->packet_coalesce)
1051 {
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001052 n_vectors = n_left =
1053 vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001054 buffers = to;
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001055 virtio_txq_clear_scheduled (vring);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001056 }
1057
Damjan Marion587f9132020-10-31 22:47:01 +01001058 u16 retry_count = 2;
1059
1060retry:
1061 /* free consumed buffers */
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001062 virtio_free_used_device_desc (vm, vring, node->node_index, packed);
Damjan Marion587f9132020-10-31 22:47:01 +01001063
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001064 if (vif->type == VIRTIO_IF_TYPE_TAP)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001065 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001066 VIRTIO_IF_TYPE_TAP,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001067 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001068 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001069 else if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001070 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001071 VIRTIO_IF_TYPE_PCI,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001072 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001073 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001074 else if (vif->type == VIRTIO_IF_TYPE_TUN)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001075 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001076 VIRTIO_IF_TYPE_TUN,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001077 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001078 n_left, packed);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001079 else
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001080 ASSERT (0);
1081
Damjan Marion587f9132020-10-31 22:47:01 +01001082 if (n_left && retry_count--)
1083 goto retry;
1084
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001085 if (vif->packet_buffering && n_left)
1086 {
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001087 u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering,
1088 &buffers
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001089 [n_vectors
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001090 - n_left],
1091 n_left);
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001092 n_left -= n_buffered;
1093 }
1094 if (n_left)
Mohsin Kazmia02ad342021-05-04 16:36:09 +02001095 virtio_interface_drop_inline (vm, vif, node->node_index,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001096 &buffers[n_vectors - n_left], n_left,
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001097 VIRTIO_TX_ERROR_NO_FREE_SLOTS);
1098
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001099 if (tf->shared_queue)
1100 clib_spinlock_unlock (&vring->lockp);
Damjan Mariondc181242020-10-20 12:11:06 +02001101
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001102 return frame->n_vectors - n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +02001103}
1104
1105static void
1106virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1107 u32 node_index)
1108{
1109 virtio_main_t *apm = &virtio_main;
1110 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1111 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
1112
1113 /* Shut off redirection */
1114 if (node_index == ~0)
1115 {
1116 vif->per_interface_next_index = node_index;
1117 return;
1118 }
1119
1120 vif->per_interface_next_index =
1121 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
1122 node_index);
1123}
1124
1125static void
1126virtio_clear_hw_interface_counters (u32 instance)
1127{
1128 /* Nothing for now */
1129}
1130
Damjan Marion809d5c92021-03-24 12:24:58 +01001131static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001132virtio_set_rx_interrupt (virtio_if_t *vif, vnet_virtio_vring_t *vring)
Mohsin Kazmi24841862020-12-02 16:39:42 +01001133{
1134 if (vif->is_packed)
1135 vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE;
1136 else
1137 vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
1138}
1139
Damjan Marion809d5c92021-03-24 12:24:58 +01001140static void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001141virtio_set_rx_polling (virtio_if_t *vif, vnet_virtio_vring_t *vring)
Mohsin Kazmi24841862020-12-02 16:39:42 +01001142{
1143 if (vif->is_packed)
1144 vring->driver_event->flags |= VRING_EVENT_F_DISABLE;
1145 else
1146 vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
1147}
1148
Damjan Marion8389fb92017-10-13 18:29:53 +02001149static clib_error_t *
1150virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
Damjan Marioneabd4242020-10-07 20:59:07 +02001151 vnet_hw_if_rx_mode mode)
Damjan Marion8389fb92017-10-13 18:29:53 +02001152{
1153 virtio_main_t *mm = &virtio_main;
1154 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1155 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +00001156 vnet_virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
Damjan Marion8389fb92017-10-13 18:29:53 +02001157
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001158 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
1159 {
Mohsin Kazmi24841862020-12-02 16:39:42 +01001160 virtio_set_rx_polling (vif, rx_vring);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001161 return clib_error_return (0, "interrupt mode is not supported");
1162 }
1163
Damjan Marioneabd4242020-10-07 20:59:07 +02001164 if (mode == VNET_HW_IF_RX_MODE_POLLING)
Mohsin Kazmi24841862020-12-02 16:39:42 +01001165 virtio_set_rx_polling (vif, rx_vring);
Damjan Marion8389fb92017-10-13 18:29:53 +02001166 else
Mohsin Kazmi24841862020-12-02 16:39:42 +01001167 virtio_set_rx_interrupt (vif, rx_vring);
Damjan Marion8389fb92017-10-13 18:29:53 +02001168
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001169 rx_vring->mode = mode;
1170
Damjan Marion8389fb92017-10-13 18:29:53 +02001171 return 0;
1172}
1173
1174static clib_error_t *
1175virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1176{
1177 virtio_main_t *mm = &virtio_main;
1178 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1179 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +02001180
1181 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001182 {
1183 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1184 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1185 VNET_HW_INTERFACE_FLAG_LINK_UP);
1186 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001187 else
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001188 {
1189 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
1190 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1191 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001192 return 0;
1193}
1194
1195static clib_error_t *
1196virtio_subif_add_del_function (vnet_main_t * vnm,
1197 u32 hw_if_index,
1198 struct vnet_sw_interface_t *st, int is_add)
1199{
1200 /* Nothing for now */
1201 return 0;
1202}
1203
1204/* *INDENT-OFF* */
1205VNET_DEVICE_CLASS (virtio_device_class) = {
1206 .name = "virtio",
Damjan Marion8389fb92017-10-13 18:29:53 +02001207 .format_device_name = format_virtio_device_name,
1208 .format_device = format_virtio_device,
1209 .format_tx_trace = format_virtio_tx_trace,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001210 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +02001211 .tx_function_error_strings = virtio_tx_func_error_strings,
1212 .rx_redirect_to_node = virtio_set_interface_next_node,
1213 .clear_counters = virtio_clear_hw_interface_counters,
1214 .admin_up_down_function = virtio_interface_admin_up_down,
1215 .subif_add_del_function = virtio_subif_add_del_function,
1216 .rx_mode_change_function = virtio_interface_rx_mode_change,
1217};
Mohsin Kazmi206acf82020-04-06 14:19:54 +02001218
Damjan Marion8389fb92017-10-13 18:29:53 +02001219/* *INDENT-ON* */
1220
1221/*
1222 * fd.io coding-style-patch-verification: ON
1223 *
1224 * Local Variables:
1225 * eval: (c-set-style "gnu")
1226 * End:
1227 */