blob: fb996d551faab6af4eeb1dc9ccb51e45eba9c2da [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
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200102static_always_inline void
Mohsin Kazmi03b26952020-09-16 16:53:16 +0000103virtio_tx_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
Damjan Mariond4a54712020-10-31 23:40:51 +0100104 vlib_buffer_t * b0, 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
138static_always_inline void
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200139virtio_interface_drop_inline (vlib_main_t * vm, uword node_index,
140 u32 * buffers, u16 n,
141 virtio_tx_func_error_t error)
142{
143 vlib_error_count (vm, node_index, error, n);
144 vlib_buffer_free (vm, buffers, n);
145}
146
147static_always_inline void
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100148virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers)
149{
150 ASSERT (n_buffers <= ring_size);
151
152 if (PREDICT_TRUE (start + n_buffers <= ring_size))
153 {
154 clib_memset_u32 (ring + start, ~0, n_buffers);
155 }
156 else
157 {
158 clib_memset_u32 (ring + start, ~0, ring_size - start);
159 clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
160 }
161}
162
163static_always_inline void
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100164virtio_free_used_device_desc_split (vlib_main_t * vm, virtio_vring_t * vring,
165 uword node_index)
Damjan Marion8389fb92017-10-13 18:29:53 +0200166{
167 u16 used = vring->desc_in_use;
168 u16 sz = vring->size;
169 u16 mask = sz - 1;
170 u16 last = vring->last_used_idx;
171 u16 n_left = vring->used->idx - last;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100172 u16 out_of_order_count = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200173
174 if (n_left == 0)
175 return;
176
177 while (n_left)
178 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200179 vring_used_elem_t *e = &vring->used->ring[last & mask];
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200180 u16 slot, n_buffers;
181 slot = n_buffers = e->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200182
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100183 while (e->id == (n_buffers & mask))
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200184 {
185 n_left--;
186 last++;
187 n_buffers++;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200188 vring_desc_t *d = &vring->desc[e->id];
Mohsin Kazmice215992020-04-10 14:57:06 +0000189 u16 next;
190 while (d->flags & VRING_DESC_F_NEXT)
191 {
192 n_buffers++;
193 next = d->next;
194 d = &vring->desc[next];
195 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200196 if (n_left == 0)
197 break;
198 e = &vring->used->ring[last & mask];
199 }
200 vlib_buffer_free_from_ring (vm, vring->buffers, slot,
201 sz, (n_buffers - slot));
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100202 virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200203 used -= (n_buffers - slot);
204
205 if (n_left > 0)
206 {
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100207 vlib_buffer_free (vm, &vring->buffers[e->id], 1);
208 vring->buffers[e->id] = ~0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200209 used--;
210 last++;
211 n_left--;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100212 out_of_order_count++;
213 vring->flags |= VRING_TX_OUT_OF_ORDER;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200214 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200215 }
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100216
217 /*
218 * Some vhost-backends give buffers back in out-of-order fashion in used ring.
219 * It impacts the overall virtio-performance.
220 */
221 if (out_of_order_count)
222 vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
223 out_of_order_count);
224
Damjan Marion8389fb92017-10-13 18:29:53 +0200225 vring->desc_in_use = used;
226 vring->last_used_idx = last;
227}
228
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000229static_always_inline void
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100230virtio_free_used_device_desc_packed (vlib_main_t * vm, virtio_vring_t * vring,
231 uword node_index)
232{
233 vring_packed_desc_t *d;
234 u16 sz = vring->size;
235 u16 last = vring->last_used_idx;
236 u16 n_buffers = 0, start;
237 u16 flags;
238
239 if (vring->desc_in_use == 0)
240 return;
241
242 d = &vring->packed_desc[last];
243 flags = d->flags;
244 start = d->id;
245
246 while ((flags & VRING_DESC_F_AVAIL) == (vring->used_wrap_counter << 7) &&
247 (flags & VRING_DESC_F_USED) == (vring->used_wrap_counter << 15))
248 {
249 last++;
250 n_buffers++;
251
252 if (last >= sz)
253 {
254 last = 0;
255 vring->used_wrap_counter ^= 1;
256 }
257 d = &vring->packed_desc[last];
258 flags = d->flags;
259 }
260
261 if (n_buffers)
262 {
263 vlib_buffer_free_from_ring (vm, vring->buffers, start, sz, n_buffers);
264 virtio_memset_ring_u32 (vring->buffers, start, sz, n_buffers);
265 vring->desc_in_use -= n_buffers;
266 vring->last_used_idx = last;
267 }
268}
269
270static_always_inline void
271virtio_free_used_device_desc (vlib_main_t * vm, virtio_vring_t * vring,
272 uword node_index, int packed)
273{
274 if (packed)
275 virtio_free_used_device_desc_packed (vm, vring, node_index);
276 else
277 virtio_free_used_device_desc_split (vm, vring, node_index);
278
279}
280
281static_always_inline void
282set_checksum_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr,
283 const int is_l2)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000284{
Mohsin Kazmi68095382021-02-10 11:26:24 +0100285 u32 oflags = vnet_buffer2 (b)->oflags;
286
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000287 if (b->flags & VNET_BUFFER_F_IS_IP4)
288 {
289 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000290 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200291 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
292 0 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000293 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
294 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100295 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000296 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000297 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
298 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100299 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000300 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000301 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
302 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000303
304 /*
305 * virtio devices do not support IP4 checksum offload. So driver takes care
306 * of it while doing tx.
307 */
308 ip4 =
309 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
Mohsin Kazmi68095382021-02-10 11:26:24 +0100310 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000311 ip4->checksum = ip4_header_checksum (ip4);
312 }
313 else if (b->flags & VNET_BUFFER_F_IS_IP6)
314 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000315 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200316 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
317 1 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000318 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
319 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
Mohsin Kazmi68095382021-02-10 11:26:24 +0100320 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000321 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000322 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
323 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100324 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000325 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000326 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
327 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000328 }
329}
330
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200331static_always_inline void
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100332set_gso_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr,
333 const int is_l2)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200334{
Mohsin Kazmi68095382021-02-10 11:26:24 +0100335 u32 oflags = vnet_buffer2 (b)->oflags;
336
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200337 if (b->flags & VNET_BUFFER_F_IS_IP4)
338 {
339 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000340 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200341 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
342 0 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200343 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
344 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000345 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200346 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
347 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
348 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
349 ip4 =
350 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
351 /*
352 * virtio devices do not support IP4 checksum offload. So driver takes care
353 * of it while doing tx.
354 */
Mohsin Kazmi68095382021-02-10 11:26:24 +0100355 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200356 ip4->checksum = ip4_header_checksum (ip4);
357 }
358 else if (b->flags & VNET_BUFFER_F_IS_IP6)
359 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000360 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200361 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
362 1 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200363 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
364 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000365 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200366 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
367 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
368 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
369 }
370}
371
Damjan Marion8389fb92017-10-13 18:29:53 +0200372static_always_inline u16
Mohsin Kazmi83143712020-10-07 13:23:55 +0200373add_buffer_to_slot (vlib_main_t * vm, vlib_node_runtime_t * node,
Mohsin Kazmi83143712020-10-07 13:23:55 +0200374 virtio_vring_t * vring, u32 bi, u16 free_desc_count,
Damjan Mariond4a54712020-10-31 23:40:51 +0100375 u16 avail, u16 next, u16 mask, int hdr_sz, int do_gso,
376 int csum_offload, int is_pci, int is_tun, int is_indirect,
377 int is_any_layout)
Damjan Marion8389fb92017-10-13 18:29:53 +0200378{
379 u16 n_added = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200380 vring_desc_t *d;
Damjan Mariond4a54712020-10-31 23:40:51 +0100381 int is_l2 = !is_tun;
Damjan Marion8389fb92017-10-13 18:29:53 +0200382 d = &vring->desc[next];
383 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200384 virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
Damjan Mariond4a54712020-10-31 23:40:51 +0100385 u32 drop_inline = ~0;
Damjan Marion508cabc2018-02-08 19:49:22 +0100386
Damjan Mariond4a54712020-10-31 23:40:51 +0100387 clib_memset_u8 (hdr, 0, hdr_sz);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000388
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200389 if (b->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200390 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200391 if (do_gso)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200392 set_gso_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200393 else
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200394 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100395 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
396 goto done;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200397 }
398 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100399 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000400 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200401 if (csum_offload)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200402 set_checksum_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200403 else
404 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100405 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
406 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200407 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000408 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200409
Mohsin Kazmi83143712020-10-07 13:23:55 +0200410 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
411 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100412 virtio_tx_trace (vm, node, b, bi, is_tun);
Mohsin Kazmi83143712020-10-07 13:23:55 +0200413 }
414
Damjan Marion8389fb92017-10-13 18:29:53 +0200415 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
416 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100417 d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) :
418 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200419 d->len = b->current_length + hdr_sz;
420 d->flags = 0;
421 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100422 else if (is_indirect)
Damjan Marion8389fb92017-10-13 18:29:53 +0200423 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200424 /*
425 * We are using single vlib_buffer_t for indirect descriptor(s)
426 * chain. Single descriptor is 16 bytes and vlib_buffer_t
427 * has 2048 bytes space. So maximum long chain can have 128
428 * (=2048/16) indirect descriptors.
429 * It can easily support 65535 bytes of Jumbo frames with
430 * each data buffer size of 512 bytes minimum.
431 */
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200432 u32 indirect_buffer = 0;
433 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200434 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100435 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
436 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200437 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200438
439 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200440 indirect_desc->current_data = 0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200441 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
442 indirect_desc->next_buffer = bi;
443 bi = indirect_buffer;
Damjan Marion8389fb92017-10-13 18:29:53 +0200444
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200445 vring_desc_t *id =
446 (vring_desc_t *) vlib_buffer_get_current (indirect_desc);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200447 u32 count = 1;
Damjan Mariond4a54712020-10-31 23:40:51 +0100448 if (is_pci)
Damjan Marion8389fb92017-10-13 18:29:53 +0200449 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200450 d->addr = vlib_physmem_get_pa (vm, id);
451 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200452
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200453 /*
454 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
455 * should be presented in separate descriptor and data will start
456 * from next descriptor.
457 */
Damjan Mariond4a54712020-10-31 23:40:51 +0100458 if (is_any_layout)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200459 id->len = b->current_length + hdr_sz;
460 else
461 {
462 id->len = hdr_sz;
463 id->flags = VRING_DESC_F_NEXT;
464 id->next = count;
465 count++;
466 id++;
467 id->addr = vlib_buffer_get_current_pa (vm, b);
468 id->len = b->current_length;
469 }
470 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
471 {
472 id->flags = VRING_DESC_F_NEXT;
473 id->next = count;
474 count++;
475 id++;
476 b = vlib_get_buffer (vm, b->next_buffer);
477 id->addr = vlib_buffer_get_current_pa (vm, b);
478 id->len = b->current_length;
479 }
480 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200481 else /* VIRTIO_IF_TYPE_[TAP | TUN] */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200482 {
483 d->addr = pointer_to_uword (id);
484 /* first buffer in chain */
485 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
486 id->len = b->current_length + hdr_sz;
487
488 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
489 {
490 id->flags = VRING_DESC_F_NEXT;
491 id->next = count;
492 count++;
493 id++;
494 b = vlib_get_buffer (vm, b->next_buffer);
495 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
496 id->len = b->current_length;
497 }
498 }
499 id->flags = 0;
500 id->next = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200501 d->len = count * sizeof (vring_desc_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200502 d->flags = VRING_DESC_F_INDIRECT;
503 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100504 else if (is_pci)
Mohsin Kazmice215992020-04-10 14:57:06 +0000505 {
506 u16 count = next;
507 vlib_buffer_t *b_temp = b;
508 u16 n_buffers_in_chain = 1;
509
510 /*
511 * Check the length of the chain for the required number of
512 * descriptors. Return from here, retry to get more descriptors,
513 * if chain length is greater than available descriptors.
514 */
515 while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
516 {
517 n_buffers_in_chain++;
518 b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
519 }
520
521 if (n_buffers_in_chain > free_desc_count)
522 return n_buffers_in_chain;
523
524 d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
525 d->len = b->current_length + hdr_sz;
526
527 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
528 {
529 d->flags = VRING_DESC_F_NEXT;
530 vring->buffers[count] = bi;
531 b->flags &=
532 ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
533 bi = b->next_buffer;
534 b->next_buffer = 0;
535 n_added++;
536 count = (count + 1) & mask;
537 d->next = count;
538 d = &vring->desc[count];
539 b = vlib_get_buffer (vm, bi);
540 d->addr = vlib_buffer_get_current_pa (vm, b);
541 d->len = b->current_length;
542 }
543 d->flags = 0;
544 vring->buffers[count] = bi;
545 vring->avail->ring[avail & mask] = next;
546 n_added++;
547 return n_added;
548 }
549 else
550 {
551 ASSERT (0);
552 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200553 vring->buffers[next] = bi;
554 vring->avail->ring[avail & mask] = next;
555 n_added++;
Damjan Mariond4a54712020-10-31 23:40:51 +0100556
557done:
558 if (drop_inline != ~0)
559 virtio_interface_drop_inline (vm, node->node_index, &bi, 1, drop_inline);
560
Damjan Marion8389fb92017-10-13 18:29:53 +0200561 return n_added;
562}
563
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100564static_always_inline u16
565add_buffer_to_slot_packed (vlib_main_t * vm, vlib_node_runtime_t * node,
566 virtio_vring_t * vring, u32 bi, u16 next,
567 int hdr_sz, int do_gso, int csum_offload,
568 int is_pci, int is_tun, int is_indirect,
569 int is_any_layout)
570{
571 u16 n_added = 0, flags = 0;
572 int is_l2 = !is_tun;
573 vring_packed_desc_t *d = &vring->packed_desc[next];
574 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
575 virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
576 u32 drop_inline = ~0;
577
578 clib_memset (hdr, 0, hdr_sz);
579
580 if (b->flags & VNET_BUFFER_F_GSO)
581 {
582 if (do_gso)
583 set_gso_offsets (b, hdr, is_l2);
584 else
585 {
586 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
587 goto done;
588 }
589 }
Mohsin Kazmi68095382021-02-10 11:26:24 +0100590 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100591 {
592 if (csum_offload)
593 set_checksum_offsets (b, hdr, is_l2);
594 else
595 {
596 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
597 goto done;
598 }
599 }
600 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
601 {
602 virtio_tx_trace (vm, node, b, bi, is_tun);
603 }
604
605 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
606 {
607 d->addr =
608 ((is_pci) ? vlib_buffer_get_current_pa (vm,
609 b) :
610 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
611 d->len = b->current_length + hdr_sz;
612 }
613 else if (is_indirect)
614 {
615 /*
616 * We are using single vlib_buffer_t for indirect descriptor(s)
617 * chain. Single descriptor is 16 bytes and vlib_buffer_t
618 * has 2048 bytes space. So maximum long chain can have 128
619 * (=2048/16) indirect descriptors.
620 * It can easily support 65535 bytes of Jumbo frames with
621 * each data buffer size of 512 bytes minimum.
622 */
623 u32 indirect_buffer = 0;
624 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
625 {
626 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
627 goto done;
628 }
629
630 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
631 indirect_desc->current_data = 0;
632 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
633 indirect_desc->next_buffer = bi;
634 bi = indirect_buffer;
635
636 vring_packed_desc_t *id =
637 (vring_packed_desc_t *) vlib_buffer_get_current (indirect_desc);
638 u32 count = 1;
639 if (is_pci)
640 {
641 d->addr = vlib_physmem_get_pa (vm, id);
642 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
643
644 /*
645 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
646 * should be presented in separate descriptor and data will start
647 * from next descriptor.
648 */
649 if (is_any_layout)
650 id->len = b->current_length + hdr_sz;
651 else
652 {
653 id->len = hdr_sz;
654 id->flags = 0;
655 id->id = 0;
656 count++;
657 id++;
658 id->addr = vlib_buffer_get_current_pa (vm, b);
659 id->len = b->current_length;
660 }
661 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
662 {
663 id->flags = 0;
664 id->id = 0;
665 count++;
666 id++;
667 b = vlib_get_buffer (vm, b->next_buffer);
668 id->addr = vlib_buffer_get_current_pa (vm, b);
669 id->len = b->current_length;
670 }
671 }
672 id->flags = 0;
673 id->id = 0;
674 d->len = count * sizeof (vring_packed_desc_t);
675 flags = VRING_DESC_F_INDIRECT;
676 }
677 else
678 {
679 ASSERT (0);
680 }
681 if (vring->avail_wrap_counter)
682 {
683 flags |= VRING_DESC_F_AVAIL;
684 flags &= ~VRING_DESC_F_USED;
685 }
686 else
687 {
688 flags &= ~VRING_DESC_F_AVAIL;
689 flags |= VRING_DESC_F_USED;
690 }
691
692 d->id = next;
693 d->flags = flags;
694 vring->buffers[next] = bi;
695 n_added++;
696
697done:
698 if (drop_inline != ~0)
699 virtio_interface_drop_inline (vm, node->node_index, &bi, 1, drop_inline);
700
701 return n_added;
702}
703
704static_always_inline uword
705virtio_interface_tx_packed_gso_inline (vlib_main_t * vm,
706 vlib_node_runtime_t * node,
707 virtio_if_t * vif,
708 virtio_if_type_t type,
709 virtio_vring_t * vring,
710 u32 * buffers, u16 n_left,
711 const int do_gso,
712 const int csum_offload)
713{
714 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
715 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
716 int is_indirect =
717 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
718 int is_any_layout =
719 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
720 const int hdr_sz = vif->virtio_net_hdr_sz;
721 u16 sz = vring->size;
722 u16 used, next, n_buffers = 0, n_buffers_left = 0;
723 u16 n_vectors = n_left;
724
725
726 used = vring->desc_in_use;
727 next = vring->desc_next;
728
729 if (vif->packet_buffering)
730 {
731 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
732
733 while (n_buffers_left && used < sz)
734 {
735 u16 n_added = 0;
736
737 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
738 if (bi == ~0)
739 break;
740 n_added = add_buffer_to_slot_packed (vm, node,
741 vring, bi, next,
742 hdr_sz, do_gso, csum_offload,
743 is_pci, is_tun, is_indirect,
744 is_any_layout);
745 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
763 n_added = add_buffer_to_slot_packed (vm, node,
764 vring, buffers[0], next,
765 hdr_sz, do_gso, csum_offload,
766 is_pci, is_tun, is_indirect,
767 is_any_layout);
768 buffers++;
769 n_left--;
770 if (PREDICT_FALSE (n_added == 0))
771 continue;
772
773 used++;
774 next++;
775 if (next >= sz)
776 {
777 next = 0;
778 vring->avail_wrap_counter ^= 1;
779 }
780 }
781
782 if (n_left != n_vectors || n_buffers != n_buffers_left)
783 {
784 CLIB_MEMORY_STORE_BARRIER ();
785 vring->desc_next = next;
786 vring->desc_in_use = used;
787 CLIB_MEMORY_BARRIER ();
788 if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
789 virtio_kick (vm, vring, vif);
790 }
791
792 return n_left;
793}
794
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100795static_always_inline void
796virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
797 u16 req, u16 next, u32 * first_free_desc_index,
798 u16 * free_desc_count)
799{
800 u16 start = 0;
801 /* next is used as hint: from where to start looking */
802 for (u16 i = 0; i < size; i++, next++)
803 {
804 if (vring->buffers[next & mask] == ~0)
805 {
806 if (*first_free_desc_index == ~0)
807 {
808 *first_free_desc_index = (next & mask);
809 start = i;
810 (*free_desc_count)++;
811 req--;
812 if (req == 0)
813 break;
814 }
815 else
816 {
817 if (start + *free_desc_count == i)
818 {
819 (*free_desc_count)++;
820 req--;
821 if (req == 0)
822 break;
823 }
824 else
825 break;
826 }
827 }
828 }
829}
830
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100831static_always_inline u16
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100832virtio_interface_tx_split_gso_inline (vlib_main_t * vm,
833 vlib_node_runtime_t * node,
834 virtio_if_t * vif,
835 virtio_if_type_t type,
836 virtio_vring_t * vring, u32 * buffers,
837 u16 n_left, int do_gso,
838 int csum_offload)
Damjan Marion8389fb92017-10-13 18:29:53 +0200839{
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000840 u16 used, next, avail, n_buffers = 0, n_buffers_left = 0;
Damjan Mariond4a54712020-10-31 23:40:51 +0100841 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
842 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
843 int is_indirect =
844 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
845 int is_any_layout =
846 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
Damjan Marion8389fb92017-10-13 18:29:53 +0200847 u16 sz = vring->size;
Damjan Mariond4a54712020-10-31 23:40:51 +0100848 int hdr_sz = vif->virtio_net_hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200849 u16 mask = sz - 1;
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100850 u16 n_vectors = n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200851
852 used = vring->desc_in_use;
853 next = vring->desc_next;
854 avail = vring->avail->idx;
855
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100856 u16 free_desc_count = 0;
857
858 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
859 {
860 u32 first_free_desc_index = ~0;
861
862 virtio_find_free_desc (vring, sz, mask, n_left, next,
863 &first_free_desc_index, &free_desc_count);
864
865 if (free_desc_count)
866 next = first_free_desc_index;
867 }
868 else
869 free_desc_count = sz - used;
870
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000871 if (vif->packet_buffering)
872 {
873 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
874
875 while (n_buffers_left && free_desc_count)
876 {
877 u16 n_added = 0;
878
879 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
880 if (bi == ~0)
881 break;
Mohsin Kazmi83143712020-10-07 13:23:55 +0200882
Damjan Mariond4a54712020-10-31 23:40:51 +0100883 n_added = add_buffer_to_slot (vm, node, vring, bi, free_desc_count,
884 avail, next, mask, hdr_sz, do_gso,
885 csum_offload, is_pci, is_tun,
886 is_indirect, is_any_layout);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000887 if (PREDICT_FALSE (n_added == 0))
888 {
889 n_buffers_left--;
890 continue;
891 }
892 else if (PREDICT_FALSE (n_added > free_desc_count))
893 break;
894
895 avail++;
896 next = (next + n_added) & mask;
897 used += n_added;
898 n_buffers_left--;
899 free_desc_count -= n_added;
900 }
901 }
902
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100903 while (n_left && free_desc_count)
Damjan Marion8389fb92017-10-13 18:29:53 +0200904 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200905 u16 n_added = 0;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +0000906
Damjan Mariond4a54712020-10-31 23:40:51 +0100907 n_added = add_buffer_to_slot (vm, node, vring, buffers[0],
908 free_desc_count, avail, next, mask,
909 hdr_sz, do_gso, csum_offload, is_pci,
910 is_tun, is_indirect, is_any_layout);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200911
912 if (PREDICT_FALSE (n_added == 0))
913 {
914 buffers++;
915 n_left--;
916 continue;
917 }
Mohsin Kazmice215992020-04-10 14:57:06 +0000918 else if (PREDICT_FALSE (n_added > free_desc_count))
919 break;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200920
Mohsin Kazmice215992020-04-10 14:57:06 +0000921 avail++;
Damjan Marion8389fb92017-10-13 18:29:53 +0200922 next = (next + n_added) & mask;
923 used += n_added;
924 buffers++;
925 n_left--;
Mohsin Kazmice215992020-04-10 14:57:06 +0000926 free_desc_count -= n_added;
Damjan Marion8389fb92017-10-13 18:29:53 +0200927 }
928
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100929 if (n_left != n_vectors || n_buffers != n_buffers_left)
Damjan Marion8389fb92017-10-13 18:29:53 +0200930 {
Mohsin Kazmid8b7dec2020-12-10 09:49:21 +0000931 clib_atomic_store_seq_cst (&vring->avail->idx, avail);
Damjan Marion8389fb92017-10-13 18:29:53 +0200932 vring->desc_next = next;
933 vring->desc_in_use = used;
Mohsin Kazmid8b7dec2020-12-10 09:49:21 +0000934 if ((clib_atomic_load_seq_cst (&vring->used->flags) &
935 VRING_USED_F_NO_NOTIFY) == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200936 virtio_kick (vm, vring, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200937 }
938
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100939 return n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200940}
941
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100942static_always_inline u16
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100943virtio_interface_tx_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
944 virtio_if_t * vif,
945 virtio_if_type_t type, virtio_vring_t * vring,
946 u32 * buffers, u16 n_left, int packed,
947 int do_gso, int csum_offload)
948{
949 if (packed)
950 return virtio_interface_tx_packed_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 else
954 return virtio_interface_tx_split_gso_inline (vm, node, vif, type, vring,
955 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +0100956 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100957}
958
959static_always_inline u16
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200960virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100961 virtio_if_t * vif,
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +0200962 virtio_vring_t * vring, virtio_if_type_t type,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100963 u32 * buffers, u16 n_left, int packed)
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200964{
965 vnet_main_t *vnm = vnet_get_main ();
966 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
967
968 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100969 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100970 buffers, n_left, packed,
971 1 /* do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200972 1 /* checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200973 else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100974 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100975 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200976 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200977 1 /* checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200978 else
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100979 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100980 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200981 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200982 0 /* no checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200983}
984
Filip Tehlaraee73642019-03-13 05:50:44 -0700985VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
986 vlib_node_runtime_t * node,
987 vlib_frame_t * frame)
Damjan Marion8389fb92017-10-13 18:29:53 +0200988{
989 virtio_main_t *nm = &virtio_main;
990 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
991 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
Damjan Mariondc181242020-10-20 12:11:06 +0200992 u16 qid = vm->thread_index % vif->num_txqs;
993 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +0200994 u16 n_left = frame->n_vectors;
995 u32 *buffers = vlib_frame_vector_args (frame);
996 u32 to[GRO_TO_VECTOR_SIZE (n_left)];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100997 int packed = vif->is_packed;
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +0100998 u16 n_vectors = frame->n_vectors;
Damjan Mariondc181242020-10-20 12:11:06 +0200999
1000 clib_spinlock_lock_if_init (&vring->lockp);
1001
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001002 if (vif->packet_coalesce)
1003 {
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001004 n_vectors = n_left =
1005 vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001006 buffers = to;
1007 }
1008
Damjan Marion587f9132020-10-31 22:47:01 +01001009 u16 retry_count = 2;
1010
1011retry:
1012 /* free consumed buffers */
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001013 virtio_free_used_device_desc (vm, vring, node->node_index, packed);
Damjan Marion587f9132020-10-31 22:47:01 +01001014
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001015 if (vif->type == VIRTIO_IF_TYPE_TAP)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001016 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001017 VIRTIO_IF_TYPE_TAP,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001018 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001019 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001020 else if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001021 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001022 VIRTIO_IF_TYPE_PCI,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001023 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001024 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001025 else if (vif->type == VIRTIO_IF_TYPE_TUN)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001026 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001027 VIRTIO_IF_TYPE_TUN,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001028 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001029 n_left, packed);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001030 else
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001031 ASSERT (0);
1032
Damjan Marion587f9132020-10-31 22:47:01 +01001033 if (n_left && retry_count--)
1034 goto retry;
1035
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001036 if (vif->packet_buffering && n_left)
1037 {
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001038 u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering,
1039 &buffers
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001040 [n_vectors
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001041 - n_left],
1042 n_left);
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001043 n_left -= n_buffered;
1044 }
1045 if (n_left)
1046 virtio_interface_drop_inline (vm, node->node_index,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001047 &buffers[n_vectors - n_left], n_left,
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001048 VIRTIO_TX_ERROR_NO_FREE_SLOTS);
1049
Damjan Mariondc181242020-10-20 12:11:06 +02001050 clib_spinlock_unlock_if_init (&vring->lockp);
1051
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001052 return frame->n_vectors - n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +02001053}
1054
1055static void
1056virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1057 u32 node_index)
1058{
1059 virtio_main_t *apm = &virtio_main;
1060 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1061 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
1062
1063 /* Shut off redirection */
1064 if (node_index == ~0)
1065 {
1066 vif->per_interface_next_index = node_index;
1067 return;
1068 }
1069
1070 vif->per_interface_next_index =
1071 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
1072 node_index);
1073}
1074
1075static void
1076virtio_clear_hw_interface_counters (u32 instance)
1077{
1078 /* Nothing for now */
1079}
1080
Mohsin Kazmi24841862020-12-02 16:39:42 +01001081static_always_inline void
1082virtio_set_rx_interrupt (virtio_if_t * vif, virtio_vring_t * vring)
1083{
1084 if (vif->is_packed)
1085 vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE;
1086 else
1087 vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
1088}
1089
1090static_always_inline void
1091virtio_set_rx_polling (virtio_if_t * vif, virtio_vring_t * vring)
1092{
1093 if (vif->is_packed)
1094 vring->driver_event->flags |= VRING_EVENT_F_DISABLE;
1095 else
1096 vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
1097}
1098
Damjan Marion8389fb92017-10-13 18:29:53 +02001099static clib_error_t *
1100virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
Damjan Marioneabd4242020-10-07 20:59:07 +02001101 vnet_hw_if_rx_mode mode)
Damjan Marion8389fb92017-10-13 18:29:53 +02001102{
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001103 vlib_main_t *vm = vnm->vlib_main;
Damjan Marion8389fb92017-10-13 18:29:53 +02001104 virtio_main_t *mm = &virtio_main;
1105 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1106 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001107 virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
Damjan Marion8389fb92017-10-13 18:29:53 +02001108
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001109 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
1110 {
Mohsin Kazmi24841862020-12-02 16:39:42 +01001111 virtio_set_rx_polling (vif, rx_vring);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001112 return clib_error_return (0, "interrupt mode is not supported");
1113 }
1114
Damjan Marioneabd4242020-10-07 20:59:07 +02001115 if (mode == VNET_HW_IF_RX_MODE_POLLING)
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001116 {
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001117 if (vif->packet_coalesce || vif->packet_buffering)
1118 {
1119 if (mm->interrupt_queues_count > 0)
1120 mm->interrupt_queues_count--;
1121 if (mm->interrupt_queues_count == 0)
1122 vlib_process_signal_event (vm,
1123 virtio_send_interrupt_node.index,
1124 VIRTIO_EVENT_STOP_TIMER, 0);
1125 }
Mohsin Kazmi24841862020-12-02 16:39:42 +01001126 virtio_set_rx_polling (vif, rx_vring);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001127 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001128 else
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001129 {
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001130 if (vif->packet_coalesce || vif->packet_buffering)
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001131 {
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001132 mm->interrupt_queues_count++;
1133 if (mm->interrupt_queues_count == 1)
1134 vlib_process_signal_event (vm,
1135 virtio_send_interrupt_node.index,
1136 VIRTIO_EVENT_START_TIMER, 0);
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001137 }
Mohsin Kazmi24841862020-12-02 16:39:42 +01001138 virtio_set_rx_interrupt (vif, rx_vring);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001139 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001140
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001141 rx_vring->mode = mode;
1142
Damjan Marion8389fb92017-10-13 18:29:53 +02001143 return 0;
1144}
1145
1146static clib_error_t *
1147virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1148{
1149 virtio_main_t *mm = &virtio_main;
1150 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1151 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +02001152
1153 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001154 {
1155 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1156 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1157 VNET_HW_INTERFACE_FLAG_LINK_UP);
1158 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001159 else
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001160 {
1161 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
1162 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1163 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001164 return 0;
1165}
1166
1167static clib_error_t *
1168virtio_subif_add_del_function (vnet_main_t * vnm,
1169 u32 hw_if_index,
1170 struct vnet_sw_interface_t *st, int is_add)
1171{
1172 /* Nothing for now */
1173 return 0;
1174}
1175
1176/* *INDENT-OFF* */
1177VNET_DEVICE_CLASS (virtio_device_class) = {
1178 .name = "virtio",
Damjan Marion8389fb92017-10-13 18:29:53 +02001179 .format_device_name = format_virtio_device_name,
1180 .format_device = format_virtio_device,
1181 .format_tx_trace = format_virtio_tx_trace,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001182 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +02001183 .tx_function_error_strings = virtio_tx_func_error_strings,
1184 .rx_redirect_to_node = virtio_set_interface_next_node,
1185 .clear_counters = virtio_clear_hw_interface_counters,
1186 .admin_up_down_function = virtio_interface_admin_up_down,
1187 .subif_add_del_function = virtio_subif_add_del_function,
1188 .rx_mode_change_function = virtio_interface_rx_mode_change,
1189};
Mohsin Kazmi206acf82020-04-06 14:19:54 +02001190
Damjan Marion8389fb92017-10-13 18:29:53 +02001191/* *INDENT-ON* */
1192
1193/*
1194 * fd.io coding-style-patch-verification: ON
1195 *
1196 * Local Variables:
1197 * eval: (c-set-style "gnu")
1198 * End:
1199 */