blob: 666cd1631cfcc6099a4c68bfd64c80e4115f56aa [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 Kazmi6d4af892020-01-03 15:11:53 +000030#include <vnet/tcp/tcp_packet.h>
31#include <vnet/udp/udp_packet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020032#include <vnet/devices/virtio/virtio.h>
33
34#define foreach_virtio_tx_func_error \
35_(NO_FREE_SLOTS, "no free tx slots") \
36_(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
37_(PENDING_MSGS, "pending msgs in tx ring") \
Mohsin Kazmif50ef402020-04-09 22:11:29 +020038_(INDIRECT_DESC_ALLOC_FAILED, "indirect descriptor allocation failed - packet drop") \
39_(OUT_OF_ORDER, "out-of-order buffers in used ring") \
40_(GSO_PACKET_DROP, "gso disabled on itf -- gso packet drop") \
41_(CSUM_OFFLOAD_PACKET_DROP, "checksum offload disabled on itf -- csum offload packet drop")
Damjan Marion8389fb92017-10-13 18:29:53 +020042
43typedef enum
44{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020045#define _(f,s) VIRTIO_TX_ERROR_##f,
Damjan Marion8389fb92017-10-13 18:29:53 +020046 foreach_virtio_tx_func_error
47#undef _
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020048 VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +020049} virtio_tx_func_error_t;
50
51static char *virtio_tx_func_error_strings[] = {
52#define _(n,s) s,
53 foreach_virtio_tx_func_error
54#undef _
55};
56
Damjan Marion8389fb92017-10-13 18:29:53 +020057static u8 *
58format_virtio_device (u8 * s, va_list * args)
59{
60 u32 dev_instance = va_arg (*args, u32);
61 int verbose = va_arg (*args, int);
62 u32 indent = format_get_indent (s);
63
64 s = format (s, "VIRTIO interface");
65 if (verbose)
66 {
67 s = format (s, "\n%U instance %u", format_white_space, indent + 2,
68 dev_instance);
69 }
70 return s;
71}
72
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000073typedef struct
Damjan Marion8389fb92017-10-13 18:29:53 +020074{
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000075 u32 buffer_index;
76 u32 sw_if_index;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000077 generic_header_offset_t gho;
Damjan Marion8c698872020-10-10 19:17:58 +020078 vlib_buffer_t buffer;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000079} virtio_tx_trace_t;
80
81static u8 *
82format_virtio_tx_trace (u8 * s, va_list * va)
83{
84 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
85 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
86 virtio_tx_trace_t *t = va_arg (*va, virtio_tx_trace_t *);
87 u32 indent = format_get_indent (s);
88
Mohsin Kazmib3a8f4e2020-09-15 16:41:44 +000089 s = format (s, "%Ubuffer 0x%x: %U\n",
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000090 format_white_space, indent,
91 t->buffer_index, format_vnet_buffer, &t->buffer);
Mohsin Kazmib3a8f4e2020-09-15 16:41:44 +000092 s =
93 format (s, "%U%U\n", format_white_space, indent,
94 format_generic_header_offset, &t->gho);
95 s =
96 format (s, "%U%U", format_white_space, indent,
97 format_ethernet_header_with_length, t->buffer.pre_data,
98 sizeof (t->buffer.pre_data));
Damjan Marion8389fb92017-10-13 18:29:53 +020099 return s;
100}
101
Damjan Marion809d5c92021-03-24 12:24:58 +0100102static void
103virtio_tx_trace (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b0,
104 u32 bi, int is_tun)
Mohsin Kazmi03b26952020-09-16 16:53:16 +0000105{
106 virtio_tx_trace_t *t;
107 t = vlib_add_trace (vm, node, b0, sizeof (t[0]));
108 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
109 t->buffer_index = bi;
Damjan Mariond4a54712020-10-31 23:40:51 +0100110 if (is_tun)
Mohsin Kazmi03b26952020-09-16 16:53:16 +0000111 {
112 int is_ip4 = 0, is_ip6 = 0;
113
114 switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0)
115 {
116 case 0x40:
117 is_ip4 = 1;
118 break;
119 case 0x60:
120 is_ip6 = 1;
121 break;
122 default:
123 break;
124 }
125 vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4, is_ip6);
126 }
127 else
128 vnet_generic_header_offset_parser (b0, &t->gho, 1,
129 b0->flags &
130 VNET_BUFFER_F_IS_IP4,
131 b0->flags & VNET_BUFFER_F_IS_IP6);
132
133 clib_memcpy_fast (&t->buffer, b0, sizeof (*b0) - sizeof (b0->pre_data));
134 clib_memcpy_fast (t->buffer.pre_data, vlib_buffer_get_current (b0),
135 sizeof (t->buffer.pre_data));
136}
137
Damjan Marion809d5c92021-03-24 12:24:58 +0100138static void
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200139virtio_interface_drop_inline (vlib_main_t *vm, virtio_if_t *vif,
140 uword node_index, u32 *buffers, u16 n,
141 virtio_tx_func_error_t error)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200142{
143 vlib_error_count (vm, node_index, error, n);
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200144 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
145 VNET_INTERFACE_COUNTER_DROP,
146 vm->thread_index, vif->sw_if_index, n);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200147 vlib_buffer_free (vm, buffers, n);
148}
149
Damjan Marion809d5c92021-03-24 12:24:58 +0100150static void
151virtio_memset_ring_u32 (u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100152{
153 ASSERT (n_buffers <= ring_size);
154
155 if (PREDICT_TRUE (start + n_buffers <= ring_size))
156 {
157 clib_memset_u32 (ring + start, ~0, n_buffers);
158 }
159 else
160 {
161 clib_memset_u32 (ring + start, ~0, ring_size - start);
162 clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
163 }
164}
165
Damjan Marion809d5c92021-03-24 12:24:58 +0100166static void
167virtio_free_used_device_desc_split (vlib_main_t *vm, virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100168 uword node_index)
Damjan Marion8389fb92017-10-13 18:29:53 +0200169{
170 u16 used = vring->desc_in_use;
171 u16 sz = vring->size;
172 u16 mask = sz - 1;
173 u16 last = vring->last_used_idx;
174 u16 n_left = vring->used->idx - last;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100175 u16 out_of_order_count = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200176
177 if (n_left == 0)
178 return;
179
180 while (n_left)
181 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200182 vring_used_elem_t *e = &vring->used->ring[last & mask];
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200183 u16 slot, n_buffers;
184 slot = n_buffers = e->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200185
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100186 while (e->id == (n_buffers & mask))
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200187 {
188 n_left--;
189 last++;
190 n_buffers++;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200191 vring_desc_t *d = &vring->desc[e->id];
Mohsin Kazmice215992020-04-10 14:57:06 +0000192 u16 next;
193 while (d->flags & VRING_DESC_F_NEXT)
194 {
195 n_buffers++;
196 next = d->next;
197 d = &vring->desc[next];
198 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200199 if (n_left == 0)
200 break;
201 e = &vring->used->ring[last & mask];
202 }
203 vlib_buffer_free_from_ring (vm, vring->buffers, slot,
204 sz, (n_buffers - slot));
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100205 virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200206 used -= (n_buffers - slot);
207
208 if (n_left > 0)
209 {
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100210 vlib_buffer_free (vm, &vring->buffers[e->id], 1);
211 vring->buffers[e->id] = ~0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200212 used--;
213 last++;
214 n_left--;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100215 out_of_order_count++;
216 vring->flags |= VRING_TX_OUT_OF_ORDER;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200217 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200218 }
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100219
220 /*
221 * Some vhost-backends give buffers back in out-of-order fashion in used ring.
222 * It impacts the overall virtio-performance.
223 */
224 if (out_of_order_count)
225 vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
226 out_of_order_count);
227
Damjan Marion8389fb92017-10-13 18:29:53 +0200228 vring->desc_in_use = used;
229 vring->last_used_idx = last;
230}
231
Damjan Marion809d5c92021-03-24 12:24:58 +0100232static void
233virtio_free_used_device_desc_packed (vlib_main_t *vm, virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100234 uword node_index)
235{
236 vring_packed_desc_t *d;
237 u16 sz = vring->size;
238 u16 last = vring->last_used_idx;
239 u16 n_buffers = 0, start;
240 u16 flags;
241
242 if (vring->desc_in_use == 0)
243 return;
244
245 d = &vring->packed_desc[last];
246 flags = d->flags;
247 start = d->id;
248
249 while ((flags & VRING_DESC_F_AVAIL) == (vring->used_wrap_counter << 7) &&
250 (flags & VRING_DESC_F_USED) == (vring->used_wrap_counter << 15))
251 {
252 last++;
253 n_buffers++;
254
255 if (last >= sz)
256 {
257 last = 0;
258 vring->used_wrap_counter ^= 1;
259 }
260 d = &vring->packed_desc[last];
261 flags = d->flags;
262 }
263
264 if (n_buffers)
265 {
266 vlib_buffer_free_from_ring (vm, vring->buffers, start, sz, n_buffers);
267 virtio_memset_ring_u32 (vring->buffers, start, sz, n_buffers);
268 vring->desc_in_use -= n_buffers;
269 vring->last_used_idx = last;
270 }
271}
272
Damjan Marion809d5c92021-03-24 12:24:58 +0100273static void
274virtio_free_used_device_desc (vlib_main_t *vm, virtio_vring_t *vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100275 uword node_index, int packed)
276{
277 if (packed)
278 virtio_free_used_device_desc_packed (vm, vring, node_index);
279 else
280 virtio_free_used_device_desc_split (vm, vring, node_index);
281
282}
283
Damjan Marion809d5c92021-03-24 12:24:58 +0100284static void
285set_checksum_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100286 const int is_l2)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000287{
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +0200288 vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100289
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000290 if (b->flags & VNET_BUFFER_F_IS_IP4)
291 {
292 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000293 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200294 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
295 0 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000296 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
297 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100298 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000299 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000300 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
301 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100302 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000303 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000304 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
305 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000306
307 /*
308 * virtio devices do not support IP4 checksum offload. So driver takes care
309 * of it while doing tx.
310 */
311 ip4 =
312 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
Mohsin Kazmi68095382021-02-10 11:26:24 +0100313 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000314 ip4->checksum = ip4_header_checksum (ip4);
315 }
316 else if (b->flags & VNET_BUFFER_F_IS_IP6)
317 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000318 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200319 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
320 1 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000321 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
322 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100323 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000324 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000325 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
326 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100327 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000328 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000329 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
330 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000331 }
332}
333
Damjan Marion809d5c92021-03-24 12:24:58 +0100334static void
335set_gso_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr, const int is_l2)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200336{
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +0200337 vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100338
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200339 if (b->flags & VNET_BUFFER_F_IS_IP4)
340 {
341 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000342 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200343 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
344 0 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200345 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
346 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000347 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200348 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
349 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
350 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
351 ip4 =
352 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
353 /*
354 * virtio devices do not support IP4 checksum offload. So driver takes care
355 * of it while doing tx.
356 */
Mohsin Kazmi68095382021-02-10 11:26:24 +0100357 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200358 ip4->checksum = ip4_header_checksum (ip4);
359 }
360 else if (b->flags & VNET_BUFFER_F_IS_IP6)
361 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000362 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200363 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
364 1 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200365 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
366 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000367 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200368 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
369 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
370 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
371 }
372}
373
Damjan Marion809d5c92021-03-24 12:24:58 +0100374static u16
375add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node,
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200376 virtio_if_t *vif, virtio_vring_t *vring, u32 bi,
377 u16 free_desc_count, u16 avail, u16 next, u16 mask,
378 int hdr_sz, int do_gso, int csum_offload, int is_pci,
379 int is_tun, int is_indirect, int is_any_layout)
Damjan Marion8389fb92017-10-13 18:29:53 +0200380{
381 u16 n_added = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200382 vring_desc_t *d;
Damjan Mariond4a54712020-10-31 23:40:51 +0100383 int is_l2 = !is_tun;
Damjan Marion8389fb92017-10-13 18:29:53 +0200384 d = &vring->desc[next];
385 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200386 virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
Damjan Mariond4a54712020-10-31 23:40:51 +0100387 u32 drop_inline = ~0;
Damjan Marion508cabc2018-02-08 19:49:22 +0100388
Damjan Mariond4a54712020-10-31 23:40:51 +0100389 clib_memset_u8 (hdr, 0, hdr_sz);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000390
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200391 if (b->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200392 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200393 if (do_gso)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200394 set_gso_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200395 else
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200396 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100397 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
398 goto done;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200399 }
400 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100401 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000402 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200403 if (csum_offload)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200404 set_checksum_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200405 else
406 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100407 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
408 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200409 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000410 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200411
Mohsin Kazmi83143712020-10-07 13:23:55 +0200412 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
413 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100414 virtio_tx_trace (vm, node, b, bi, is_tun);
Mohsin Kazmi83143712020-10-07 13:23:55 +0200415 }
416
Damjan Marion8389fb92017-10-13 18:29:53 +0200417 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
418 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100419 d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) :
420 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200421 d->len = b->current_length + hdr_sz;
422 d->flags = 0;
423 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100424 else if (is_indirect)
Damjan Marion8389fb92017-10-13 18:29:53 +0200425 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200426 /*
427 * We are using single vlib_buffer_t for indirect descriptor(s)
428 * chain. Single descriptor is 16 bytes and vlib_buffer_t
429 * has 2048 bytes space. So maximum long chain can have 128
430 * (=2048/16) indirect descriptors.
431 * It can easily support 65535 bytes of Jumbo frames with
432 * each data buffer size of 512 bytes minimum.
433 */
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200434 u32 indirect_buffer = 0;
435 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200436 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100437 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
438 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200439 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200440
441 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200442 indirect_desc->current_data = 0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200443 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
444 indirect_desc->next_buffer = bi;
445 bi = indirect_buffer;
Damjan Marion8389fb92017-10-13 18:29:53 +0200446
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200447 vring_desc_t *id =
448 (vring_desc_t *) vlib_buffer_get_current (indirect_desc);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200449 u32 count = 1;
Damjan Mariond4a54712020-10-31 23:40:51 +0100450 if (is_pci)
Damjan Marion8389fb92017-10-13 18:29:53 +0200451 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200452 d->addr = vlib_physmem_get_pa (vm, id);
453 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200454
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200455 /*
456 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
457 * should be presented in separate descriptor and data will start
458 * from next descriptor.
459 */
Damjan Mariond4a54712020-10-31 23:40:51 +0100460 if (is_any_layout)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200461 id->len = b->current_length + hdr_sz;
462 else
463 {
464 id->len = hdr_sz;
465 id->flags = VRING_DESC_F_NEXT;
466 id->next = count;
467 count++;
468 id++;
469 id->addr = vlib_buffer_get_current_pa (vm, b);
470 id->len = b->current_length;
471 }
472 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
473 {
474 id->flags = VRING_DESC_F_NEXT;
475 id->next = count;
476 count++;
477 id++;
478 b = vlib_get_buffer (vm, b->next_buffer);
479 id->addr = vlib_buffer_get_current_pa (vm, b);
480 id->len = b->current_length;
481 }
482 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200483 else /* VIRTIO_IF_TYPE_[TAP | TUN] */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200484 {
485 d->addr = pointer_to_uword (id);
486 /* first buffer in chain */
487 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
488 id->len = b->current_length + hdr_sz;
489
490 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
491 {
492 id->flags = VRING_DESC_F_NEXT;
493 id->next = count;
494 count++;
495 id++;
496 b = vlib_get_buffer (vm, b->next_buffer);
497 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
498 id->len = b->current_length;
499 }
500 }
501 id->flags = 0;
502 id->next = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200503 d->len = count * sizeof (vring_desc_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200504 d->flags = VRING_DESC_F_INDIRECT;
505 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100506 else if (is_pci)
Mohsin Kazmice215992020-04-10 14:57:06 +0000507 {
508 u16 count = next;
509 vlib_buffer_t *b_temp = b;
510 u16 n_buffers_in_chain = 1;
511
512 /*
513 * Check the length of the chain for the required number of
514 * descriptors. Return from here, retry to get more descriptors,
515 * if chain length is greater than available descriptors.
516 */
517 while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
518 {
519 n_buffers_in_chain++;
520 b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
521 }
522
523 if (n_buffers_in_chain > free_desc_count)
524 return n_buffers_in_chain;
525
526 d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
527 d->len = b->current_length + hdr_sz;
528
529 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
530 {
531 d->flags = VRING_DESC_F_NEXT;
532 vring->buffers[count] = bi;
533 b->flags &=
534 ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
535 bi = b->next_buffer;
536 b->next_buffer = 0;
537 n_added++;
538 count = (count + 1) & mask;
539 d->next = count;
540 d = &vring->desc[count];
541 b = vlib_get_buffer (vm, bi);
542 d->addr = vlib_buffer_get_current_pa (vm, b);
543 d->len = b->current_length;
544 }
545 d->flags = 0;
546 vring->buffers[count] = bi;
547 vring->avail->ring[avail & mask] = next;
548 n_added++;
549 return n_added;
550 }
551 else
552 {
553 ASSERT (0);
554 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200555 vring->buffers[next] = bi;
556 vring->avail->ring[avail & mask] = next;
557 n_added++;
Damjan Mariond4a54712020-10-31 23:40:51 +0100558
559done:
560 if (drop_inline != ~0)
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200561 virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
562 drop_inline);
Damjan Mariond4a54712020-10-31 23:40:51 +0100563
Damjan Marion8389fb92017-10-13 18:29:53 +0200564 return n_added;
565}
566
Damjan Marion809d5c92021-03-24 12:24:58 +0100567static u16
568add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node,
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200569 virtio_if_t *vif, virtio_vring_t *vring, u32 bi,
570 u16 next, int hdr_sz, int do_gso, int csum_offload,
571 int is_pci, int is_tun, int is_indirect,
572 int is_any_layout)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100573{
574 u16 n_added = 0, flags = 0;
575 int is_l2 = !is_tun;
576 vring_packed_desc_t *d = &vring->packed_desc[next];
577 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
578 virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
579 u32 drop_inline = ~0;
580
581 clib_memset (hdr, 0, hdr_sz);
582
583 if (b->flags & VNET_BUFFER_F_GSO)
584 {
585 if (do_gso)
586 set_gso_offsets (b, hdr, is_l2);
587 else
588 {
589 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
590 goto done;
591 }
592 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100593 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100594 {
595 if (csum_offload)
596 set_checksum_offsets (b, hdr, is_l2);
597 else
598 {
599 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
600 goto done;
601 }
602 }
603 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
604 {
605 virtio_tx_trace (vm, node, b, bi, is_tun);
606 }
607
608 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
609 {
610 d->addr =
611 ((is_pci) ? vlib_buffer_get_current_pa (vm,
612 b) :
613 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
614 d->len = b->current_length + hdr_sz;
615 }
616 else if (is_indirect)
617 {
618 /*
619 * We are using single vlib_buffer_t for indirect descriptor(s)
620 * chain. Single descriptor is 16 bytes and vlib_buffer_t
621 * has 2048 bytes space. So maximum long chain can have 128
622 * (=2048/16) indirect descriptors.
623 * It can easily support 65535 bytes of Jumbo frames with
624 * each data buffer size of 512 bytes minimum.
625 */
626 u32 indirect_buffer = 0;
627 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
628 {
629 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
630 goto done;
631 }
632
633 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
634 indirect_desc->current_data = 0;
635 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
636 indirect_desc->next_buffer = bi;
637 bi = indirect_buffer;
638
639 vring_packed_desc_t *id =
640 (vring_packed_desc_t *) vlib_buffer_get_current (indirect_desc);
641 u32 count = 1;
642 if (is_pci)
643 {
644 d->addr = vlib_physmem_get_pa (vm, id);
645 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
646
647 /*
648 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
649 * should be presented in separate descriptor and data will start
650 * from next descriptor.
651 */
652 if (is_any_layout)
653 id->len = b->current_length + hdr_sz;
654 else
655 {
656 id->len = hdr_sz;
657 id->flags = 0;
658 id->id = 0;
659 count++;
660 id++;
661 id->addr = vlib_buffer_get_current_pa (vm, b);
662 id->len = b->current_length;
663 }
664 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
665 {
666 id->flags = 0;
667 id->id = 0;
668 count++;
669 id++;
670 b = vlib_get_buffer (vm, b->next_buffer);
671 id->addr = vlib_buffer_get_current_pa (vm, b);
672 id->len = b->current_length;
673 }
674 }
675 id->flags = 0;
676 id->id = 0;
677 d->len = count * sizeof (vring_packed_desc_t);
678 flags = VRING_DESC_F_INDIRECT;
679 }
680 else
681 {
682 ASSERT (0);
683 }
684 if (vring->avail_wrap_counter)
685 {
686 flags |= VRING_DESC_F_AVAIL;
687 flags &= ~VRING_DESC_F_USED;
688 }
689 else
690 {
691 flags &= ~VRING_DESC_F_AVAIL;
692 flags |= VRING_DESC_F_USED;
693 }
694
695 d->id = next;
696 d->flags = flags;
697 vring->buffers[next] = bi;
698 n_added++;
699
700done:
701 if (drop_inline != ~0)
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200702 virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
703 drop_inline);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100704
705 return n_added;
706}
707
Damjan Marion809d5c92021-03-24 12:24:58 +0100708static uword
709virtio_interface_tx_packed_gso_inline (vlib_main_t *vm,
710 vlib_node_runtime_t *node,
711 virtio_if_t *vif, virtio_if_type_t type,
712 virtio_vring_t *vring, u32 *buffers,
713 u16 n_left, const int do_gso,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100714 const int csum_offload)
715{
716 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
717 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
718 int is_indirect =
719 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
720 int is_any_layout =
721 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
722 const int hdr_sz = vif->virtio_net_hdr_sz;
723 u16 sz = vring->size;
724 u16 used, next, n_buffers = 0, n_buffers_left = 0;
725 u16 n_vectors = n_left;
726
727
728 used = vring->desc_in_use;
729 next = vring->desc_next;
730
731 if (vif->packet_buffering)
732 {
733 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
734
735 while (n_buffers_left && used < sz)
736 {
737 u16 n_added = 0;
738
739 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
740 if (bi == ~0)
741 break;
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200742 n_added = add_buffer_to_slot_packed (
743 vm, node, vif, vring, bi, next, hdr_sz, do_gso, csum_offload,
744 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100745 n_buffers_left--;
746 if (PREDICT_FALSE (n_added == 0))
747 continue;
748
749 used++;
750 next++;
751 if (next >= sz)
752 {
753 next = 0;
754 vring->avail_wrap_counter ^= 1;
755 }
756 }
757 }
758
759 while (n_left && used < sz)
760 {
761 u16 n_added = 0;
762
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200763 n_added = add_buffer_to_slot_packed (
764 vm, node, vif, vring, buffers[0], next, hdr_sz, do_gso, csum_offload,
765 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100766 buffers++;
767 n_left--;
768 if (PREDICT_FALSE (n_added == 0))
769 continue;
770
771 used++;
772 next++;
773 if (next >= sz)
774 {
775 next = 0;
776 vring->avail_wrap_counter ^= 1;
777 }
778 }
779
780 if (n_left != n_vectors || n_buffers != n_buffers_left)
781 {
782 CLIB_MEMORY_STORE_BARRIER ();
783 vring->desc_next = next;
784 vring->desc_in_use = used;
785 CLIB_MEMORY_BARRIER ();
786 if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
787 virtio_kick (vm, vring, vif);
788 }
789
790 return n_left;
791}
792
Damjan Marion809d5c92021-03-24 12:24:58 +0100793static void
794virtio_find_free_desc (virtio_vring_t *vring, u16 size, u16 mask, u16 req,
795 u16 next, u32 *first_free_desc_index,
796 u16 *free_desc_count)
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100797{
798 u16 start = 0;
799 /* next is used as hint: from where to start looking */
800 for (u16 i = 0; i < size; i++, next++)
801 {
802 if (vring->buffers[next & mask] == ~0)
803 {
804 if (*first_free_desc_index == ~0)
805 {
806 *first_free_desc_index = (next & mask);
807 start = i;
808 (*free_desc_count)++;
809 req--;
810 if (req == 0)
811 break;
812 }
813 else
814 {
815 if (start + *free_desc_count == i)
816 {
817 (*free_desc_count)++;
818 req--;
819 if (req == 0)
820 break;
821 }
822 else
823 break;
824 }
825 }
826 }
827}
828
Damjan Marion809d5c92021-03-24 12:24:58 +0100829static u16
830virtio_interface_tx_split_gso_inline (vlib_main_t *vm,
831 vlib_node_runtime_t *node,
832 virtio_if_t *vif, virtio_if_type_t type,
833 virtio_vring_t *vring, u32 *buffers,
834 u16 n_left, int do_gso, int csum_offload)
Damjan Marion8389fb92017-10-13 18:29:53 +0200835{
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000836 u16 used, next, avail, n_buffers = 0, n_buffers_left = 0;
Damjan Mariond4a54712020-10-31 23:40:51 +0100837 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
838 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
839 int is_indirect =
840 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
841 int is_any_layout =
842 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
Damjan Marion8389fb92017-10-13 18:29:53 +0200843 u16 sz = vring->size;
Damjan Mariond4a54712020-10-31 23:40:51 +0100844 int hdr_sz = vif->virtio_net_hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200845 u16 mask = sz - 1;
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100846 u16 n_vectors = n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200847
848 used = vring->desc_in_use;
849 next = vring->desc_next;
850 avail = vring->avail->idx;
851
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100852 u16 free_desc_count = 0;
853
854 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
855 {
856 u32 first_free_desc_index = ~0;
857
858 virtio_find_free_desc (vring, sz, mask, n_left, next,
859 &first_free_desc_index, &free_desc_count);
860
861 if (free_desc_count)
862 next = first_free_desc_index;
863 }
864 else
865 free_desc_count = sz - used;
866
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000867 if (vif->packet_buffering)
868 {
869 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
870
871 while (n_buffers_left && free_desc_count)
872 {
873 u16 n_added = 0;
874
875 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
876 if (bi == ~0)
877 break;
Mohsin Kazmi83143712020-10-07 13:23:55 +0200878
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200879 n_added = add_buffer_to_slot (vm, node, vif, vring, bi,
880 free_desc_count, avail, next, mask,
881 hdr_sz, do_gso, csum_offload, is_pci,
882 is_tun, is_indirect, is_any_layout);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000883 if (PREDICT_FALSE (n_added == 0))
884 {
885 n_buffers_left--;
886 continue;
887 }
888 else if (PREDICT_FALSE (n_added > free_desc_count))
889 break;
890
891 avail++;
892 next = (next + n_added) & mask;
893 used += n_added;
894 n_buffers_left--;
895 free_desc_count -= n_added;
896 }
897 }
898
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100899 while (n_left && free_desc_count)
Damjan Marion8389fb92017-10-13 18:29:53 +0200900 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200901 u16 n_added = 0;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +0000902
Mohsin Kazmia02ad342021-05-04 16:36:09 +0200903 n_added =
904 add_buffer_to_slot (vm, node, vif, vring, buffers[0], free_desc_count,
905 avail, next, mask, hdr_sz, do_gso, csum_offload,
906 is_pci, is_tun, is_indirect, is_any_layout);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200907
908 if (PREDICT_FALSE (n_added == 0))
909 {
910 buffers++;
911 n_left--;
912 continue;
913 }
Mohsin Kazmice215992020-04-10 14:57:06 +0000914 else if (PREDICT_FALSE (n_added > free_desc_count))
915 break;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200916
Mohsin Kazmice215992020-04-10 14:57:06 +0000917 avail++;
Damjan Marion8389fb92017-10-13 18:29:53 +0200918 next = (next + n_added) & mask;
919 used += n_added;
920 buffers++;
921 n_left--;
Mohsin Kazmice215992020-04-10 14:57:06 +0000922 free_desc_count -= n_added;
Damjan Marion8389fb92017-10-13 18:29:53 +0200923 }
924
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100925 if (n_left != n_vectors || n_buffers != n_buffers_left)
Damjan Marion8389fb92017-10-13 18:29:53 +0200926 {
Mohsin Kazmid8b7dec2020-12-10 09:49:21 +0000927 clib_atomic_store_seq_cst (&vring->avail->idx, avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200928 vring->desc_next = next;
929 vring->desc_in_use = used;
Mohsin Kazmid8b7dec2020-12-10 09:49:21 +0000930 if ((clib_atomic_load_seq_cst (&vring->used->flags) &
931 VRING_USED_F_NO_NOTIFY) == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200932 virtio_kick (vm, vring, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200933 }
934
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100935 return n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200936}
937
Damjan Marion809d5c92021-03-24 12:24:58 +0100938static u16
939virtio_interface_tx_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
940 virtio_if_t *vif, virtio_if_type_t type,
941 virtio_vring_t *vring, u32 *buffers,
942 u16 n_left, int packed, int do_gso,
943 int csum_offload)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100944{
945 if (packed)
946 return virtio_interface_tx_packed_gso_inline (vm, node, vif, type, vring,
947 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +0100948 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100949 else
950 return virtio_interface_tx_split_gso_inline (vm, node, vif, type, vring,
951 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +0100952 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100953}
954
Damjan Marion809d5c92021-03-24 12:24:58 +0100955static u16
956virtio_interface_tx_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
957 virtio_if_t *vif, virtio_vring_t *vring,
958 virtio_if_type_t type, u32 *buffers, u16 n_left,
959 int packed)
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200960{
961 vnet_main_t *vnm = vnet_get_main ();
962 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
963
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100964 if (hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100965 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100966 buffers, n_left, packed,
967 1 /* do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200968 1 /* checksum offload */ );
Mohsin Kazmi5b3f5232021-02-10 12:03:53 +0100969 else if (hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100970 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100971 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200972 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200973 1 /* checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200974 else
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100975 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100976 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200977 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200978 0 /* no checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200979}
980
Filip Tehlaraee73642019-03-13 05:50:44 -0700981VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
982 vlib_node_runtime_t * node,
983 vlib_frame_t * frame)
Damjan Marion8389fb92017-10-13 18:29:53 +0200984{
985 virtio_main_t *nm = &virtio_main;
986 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
987 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
Damjan Mariondc181242020-10-20 12:11:06 +0200988 u16 qid = vm->thread_index % vif->num_txqs;
989 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +0200990 u16 n_left = frame->n_vectors;
991 u32 *buffers = vlib_frame_vector_args (frame);
992 u32 to[GRO_TO_VECTOR_SIZE (n_left)];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100993 int packed = vif->is_packed;
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +0100994 u16 n_vectors = frame->n_vectors;
Damjan Mariondc181242020-10-20 12:11:06 +0200995
996 clib_spinlock_lock_if_init (&vring->lockp);
997
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +0200998 if (vif->packet_coalesce)
999 {
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001000 n_vectors = n_left =
1001 vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001002 buffers = to;
1003 }
1004
Damjan Marion587f9132020-10-31 22:47:01 +01001005 u16 retry_count = 2;
1006
1007retry:
1008 /* free consumed buffers */
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001009 virtio_free_used_device_desc (vm, vring, node->node_index, packed);
Damjan Marion587f9132020-10-31 22:47:01 +01001010
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001011 if (vif->type == VIRTIO_IF_TYPE_TAP)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001012 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001013 VIRTIO_IF_TYPE_TAP,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001014 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001015 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001016 else if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001017 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001018 VIRTIO_IF_TYPE_PCI,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001019 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001020 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001021 else if (vif->type == VIRTIO_IF_TYPE_TUN)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001022 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001023 VIRTIO_IF_TYPE_TUN,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001024 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001025 n_left, packed);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001026 else
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001027 ASSERT (0);
1028
Damjan Marion587f9132020-10-31 22:47:01 +01001029 if (n_left && retry_count--)
1030 goto retry;
1031
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001032 if (vif->packet_buffering && n_left)
1033 {
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001034 u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering,
1035 &buffers
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001036 [n_vectors
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001037 - n_left],
1038 n_left);
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001039 n_left -= n_buffered;
1040 }
1041 if (n_left)
Mohsin Kazmia02ad342021-05-04 16:36:09 +02001042 virtio_interface_drop_inline (vm, vif, node->node_index,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001043 &buffers[n_vectors - n_left], n_left,
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001044 VIRTIO_TX_ERROR_NO_FREE_SLOTS);
1045
Damjan Mariondc181242020-10-20 12:11:06 +02001046 clib_spinlock_unlock_if_init (&vring->lockp);
1047
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001048 return frame->n_vectors - n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +02001049}
1050
1051static void
1052virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1053 u32 node_index)
1054{
1055 virtio_main_t *apm = &virtio_main;
1056 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1057 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
1058
1059 /* Shut off redirection */
1060 if (node_index == ~0)
1061 {
1062 vif->per_interface_next_index = node_index;
1063 return;
1064 }
1065
1066 vif->per_interface_next_index =
1067 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
1068 node_index);
1069}
1070
1071static void
1072virtio_clear_hw_interface_counters (u32 instance)
1073{
1074 /* Nothing for now */
1075}
1076
Damjan Marion809d5c92021-03-24 12:24:58 +01001077static void
1078virtio_set_rx_interrupt (virtio_if_t *vif, virtio_vring_t *vring)
Mohsin Kazmi24841862020-12-02 16:39:42 +01001079{
1080 if (vif->is_packed)
1081 vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE;
1082 else
1083 vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
1084}
1085
Damjan Marion809d5c92021-03-24 12:24:58 +01001086static void
1087virtio_set_rx_polling (virtio_if_t *vif, virtio_vring_t *vring)
Mohsin Kazmi24841862020-12-02 16:39:42 +01001088{
1089 if (vif->is_packed)
1090 vring->driver_event->flags |= VRING_EVENT_F_DISABLE;
1091 else
1092 vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
1093}
1094
Damjan Marion8389fb92017-10-13 18:29:53 +02001095static clib_error_t *
1096virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
Damjan Marioneabd4242020-10-07 20:59:07 +02001097 vnet_hw_if_rx_mode mode)
Damjan Marion8389fb92017-10-13 18:29:53 +02001098{
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001099 vlib_main_t *vm = vnm->vlib_main;
Damjan Marion8389fb92017-10-13 18:29:53 +02001100 virtio_main_t *mm = &virtio_main;
1101 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1102 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001103 virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
Damjan Marion8389fb92017-10-13 18:29:53 +02001104
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001105 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
1106 {
Mohsin Kazmi24841862020-12-02 16:39:42 +01001107 virtio_set_rx_polling (vif, rx_vring);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001108 return clib_error_return (0, "interrupt mode is not supported");
1109 }
1110
Damjan Marioneabd4242020-10-07 20:59:07 +02001111 if (mode == VNET_HW_IF_RX_MODE_POLLING)
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001112 {
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001113 if (vif->packet_coalesce || vif->packet_buffering)
1114 {
1115 if (mm->interrupt_queues_count > 0)
1116 mm->interrupt_queues_count--;
1117 if (mm->interrupt_queues_count == 0)
1118 vlib_process_signal_event (vm,
1119 virtio_send_interrupt_node.index,
1120 VIRTIO_EVENT_STOP_TIMER, 0);
1121 }
Mohsin Kazmi24841862020-12-02 16:39:42 +01001122 virtio_set_rx_polling (vif, rx_vring);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001123 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001124 else
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001125 {
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001126 if (vif->packet_coalesce || vif->packet_buffering)
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001127 {
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001128 mm->interrupt_queues_count++;
1129 if (mm->interrupt_queues_count == 1)
1130 vlib_process_signal_event (vm,
1131 virtio_send_interrupt_node.index,
1132 VIRTIO_EVENT_START_TIMER, 0);
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001133 }
Mohsin Kazmi24841862020-12-02 16:39:42 +01001134 virtio_set_rx_interrupt (vif, rx_vring);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001135 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001136
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001137 rx_vring->mode = mode;
1138
Damjan Marion8389fb92017-10-13 18:29:53 +02001139 return 0;
1140}
1141
1142static clib_error_t *
1143virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1144{
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);
Damjan Marion8389fb92017-10-13 18:29:53 +02001148
1149 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001150 {
1151 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1152 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1153 VNET_HW_INTERFACE_FLAG_LINK_UP);
1154 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001155 else
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001156 {
1157 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
1158 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1159 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001160 return 0;
1161}
1162
1163static clib_error_t *
1164virtio_subif_add_del_function (vnet_main_t * vnm,
1165 u32 hw_if_index,
1166 struct vnet_sw_interface_t *st, int is_add)
1167{
1168 /* Nothing for now */
1169 return 0;
1170}
1171
1172/* *INDENT-OFF* */
1173VNET_DEVICE_CLASS (virtio_device_class) = {
1174 .name = "virtio",
Damjan Marion8389fb92017-10-13 18:29:53 +02001175 .format_device_name = format_virtio_device_name,
1176 .format_device = format_virtio_device,
1177 .format_tx_trace = format_virtio_tx_trace,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001178 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +02001179 .tx_function_error_strings = virtio_tx_func_error_strings,
1180 .rx_redirect_to_node = virtio_set_interface_next_node,
1181 .clear_counters = virtio_clear_hw_interface_counters,
1182 .admin_up_down_function = virtio_interface_admin_up_down,
1183 .subif_add_del_function = virtio_subif_add_del_function,
1184 .rx_mode_change_function = virtio_interface_rx_mode_change,
1185};
Mohsin Kazmi206acf82020-04-06 14:19:54 +02001186
Damjan Marion8389fb92017-10-13 18:29:53 +02001187/* *INDENT-ON* */
1188
1189/*
1190 * fd.io coding-style-patch-verification: ON
1191 *
1192 * Local Variables:
1193 * eval: (c-set-style "gnu")
1194 * End:
1195 */