blob: 5796b1d2150c93458642af0822dcb75927fc685f [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{
285 if (b->flags & VNET_BUFFER_F_IS_IP4)
286 {
287 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000288 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200289 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
290 0 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000291 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
292 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
293 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000294 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000295 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
296 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000297 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000298 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000299 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
300 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000301
302 /*
303 * virtio devices do not support IP4 checksum offload. So driver takes care
304 * of it while doing tx.
305 */
306 ip4 =
307 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
308 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
309 ip4->checksum = ip4_header_checksum (ip4);
310 }
311 else if (b->flags & VNET_BUFFER_F_IS_IP6)
312 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000313 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200314 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
315 1 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000316 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
317 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
318 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000319 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000320 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
321 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000322 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000323 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000324 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
325 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000326 }
327}
328
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200329static_always_inline void
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100330set_gso_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr,
331 const int is_l2)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200332{
333 if (b->flags & VNET_BUFFER_F_IS_IP4)
334 {
335 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000336 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200337 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
338 0 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200339 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
340 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000341 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200342 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
343 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
344 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
345 ip4 =
346 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
347 /*
348 * virtio devices do not support IP4 checksum offload. So driver takes care
349 * of it while doing tx.
350 */
351 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
352 ip4->checksum = ip4_header_checksum (ip4);
353 }
354 else if (b->flags & VNET_BUFFER_F_IS_IP6)
355 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000356 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200357 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
358 1 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200359 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
360 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000361 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200362 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
363 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
364 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
365 }
366}
367
Damjan Marion8389fb92017-10-13 18:29:53 +0200368static_always_inline u16
Mohsin Kazmi83143712020-10-07 13:23:55 +0200369add_buffer_to_slot (vlib_main_t * vm, vlib_node_runtime_t * node,
Mohsin Kazmi83143712020-10-07 13:23:55 +0200370 virtio_vring_t * vring, u32 bi, u16 free_desc_count,
Damjan Mariond4a54712020-10-31 23:40:51 +0100371 u16 avail, u16 next, u16 mask, int hdr_sz, int do_gso,
372 int csum_offload, int is_pci, int is_tun, int is_indirect,
373 int is_any_layout)
Damjan Marion8389fb92017-10-13 18:29:53 +0200374{
375 u16 n_added = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200376 vring_desc_t *d;
Damjan Mariond4a54712020-10-31 23:40:51 +0100377 int is_l2 = !is_tun;
Damjan Marion8389fb92017-10-13 18:29:53 +0200378 d = &vring->desc[next];
379 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200380 virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
Damjan Mariond4a54712020-10-31 23:40:51 +0100381 u32 drop_inline = ~0;
Damjan Marion508cabc2018-02-08 19:49:22 +0100382
Damjan Mariond4a54712020-10-31 23:40:51 +0100383 clib_memset_u8 (hdr, 0, hdr_sz);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000384
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200385 if (b->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200386 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200387 if (do_gso)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200388 set_gso_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200389 else
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200390 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100391 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
392 goto done;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200393 }
394 }
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200395 else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
396 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000397 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200398 if (csum_offload)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200399 set_checksum_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200400 else
401 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100402 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
403 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200404 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000405 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200406
Mohsin Kazmi83143712020-10-07 13:23:55 +0200407 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
408 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100409 virtio_tx_trace (vm, node, b, bi, is_tun);
Mohsin Kazmi83143712020-10-07 13:23:55 +0200410 }
411
Damjan Marion8389fb92017-10-13 18:29:53 +0200412 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
413 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100414 d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) :
415 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200416 d->len = b->current_length + hdr_sz;
417 d->flags = 0;
418 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100419 else if (is_indirect)
Damjan Marion8389fb92017-10-13 18:29:53 +0200420 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200421 /*
422 * We are using single vlib_buffer_t for indirect descriptor(s)
423 * chain. Single descriptor is 16 bytes and vlib_buffer_t
424 * has 2048 bytes space. So maximum long chain can have 128
425 * (=2048/16) indirect descriptors.
426 * It can easily support 65535 bytes of Jumbo frames with
427 * each data buffer size of 512 bytes minimum.
428 */
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200429 u32 indirect_buffer = 0;
430 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200431 {
Damjan Mariond4a54712020-10-31 23:40:51 +0100432 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
433 goto done;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200434 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200435
436 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200437 indirect_desc->current_data = 0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200438 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
439 indirect_desc->next_buffer = bi;
440 bi = indirect_buffer;
Damjan Marion8389fb92017-10-13 18:29:53 +0200441
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200442 vring_desc_t *id =
443 (vring_desc_t *) vlib_buffer_get_current (indirect_desc);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200444 u32 count = 1;
Damjan Mariond4a54712020-10-31 23:40:51 +0100445 if (is_pci)
Damjan Marion8389fb92017-10-13 18:29:53 +0200446 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200447 d->addr = vlib_physmem_get_pa (vm, id);
448 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200449
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200450 /*
451 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
452 * should be presented in separate descriptor and data will start
453 * from next descriptor.
454 */
Damjan Mariond4a54712020-10-31 23:40:51 +0100455 if (is_any_layout)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200456 id->len = b->current_length + hdr_sz;
457 else
458 {
459 id->len = hdr_sz;
460 id->flags = VRING_DESC_F_NEXT;
461 id->next = count;
462 count++;
463 id++;
464 id->addr = vlib_buffer_get_current_pa (vm, b);
465 id->len = b->current_length;
466 }
467 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
468 {
469 id->flags = VRING_DESC_F_NEXT;
470 id->next = count;
471 count++;
472 id++;
473 b = vlib_get_buffer (vm, b->next_buffer);
474 id->addr = vlib_buffer_get_current_pa (vm, b);
475 id->len = b->current_length;
476 }
477 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200478 else /* VIRTIO_IF_TYPE_[TAP | TUN] */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200479 {
480 d->addr = pointer_to_uword (id);
481 /* first buffer in chain */
482 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
483 id->len = b->current_length + hdr_sz;
484
485 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
486 {
487 id->flags = VRING_DESC_F_NEXT;
488 id->next = count;
489 count++;
490 id++;
491 b = vlib_get_buffer (vm, b->next_buffer);
492 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
493 id->len = b->current_length;
494 }
495 }
496 id->flags = 0;
497 id->next = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200498 d->len = count * sizeof (vring_desc_t);
Damjan Marion8389fb92017-10-13 18:29:53 +0200499 d->flags = VRING_DESC_F_INDIRECT;
500 }
Damjan Mariond4a54712020-10-31 23:40:51 +0100501 else if (is_pci)
Mohsin Kazmice215992020-04-10 14:57:06 +0000502 {
503 u16 count = next;
504 vlib_buffer_t *b_temp = b;
505 u16 n_buffers_in_chain = 1;
506
507 /*
508 * Check the length of the chain for the required number of
509 * descriptors. Return from here, retry to get more descriptors,
510 * if chain length is greater than available descriptors.
511 */
512 while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
513 {
514 n_buffers_in_chain++;
515 b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
516 }
517
518 if (n_buffers_in_chain > free_desc_count)
519 return n_buffers_in_chain;
520
521 d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
522 d->len = b->current_length + hdr_sz;
523
524 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
525 {
526 d->flags = VRING_DESC_F_NEXT;
527 vring->buffers[count] = bi;
528 b->flags &=
529 ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
530 bi = b->next_buffer;
531 b->next_buffer = 0;
532 n_added++;
533 count = (count + 1) & mask;
534 d->next = count;
535 d = &vring->desc[count];
536 b = vlib_get_buffer (vm, bi);
537 d->addr = vlib_buffer_get_current_pa (vm, b);
538 d->len = b->current_length;
539 }
540 d->flags = 0;
541 vring->buffers[count] = bi;
542 vring->avail->ring[avail & mask] = next;
543 n_added++;
544 return n_added;
545 }
546 else
547 {
548 ASSERT (0);
549 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200550 vring->buffers[next] = bi;
551 vring->avail->ring[avail & mask] = next;
552 n_added++;
Damjan Mariond4a54712020-10-31 23:40:51 +0100553
554done:
555 if (drop_inline != ~0)
556 virtio_interface_drop_inline (vm, node->node_index, &bi, 1, drop_inline);
557
Damjan Marion8389fb92017-10-13 18:29:53 +0200558 return n_added;
559}
560
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100561static_always_inline u16
562add_buffer_to_slot_packed (vlib_main_t * vm, vlib_node_runtime_t * node,
563 virtio_vring_t * vring, u32 bi, u16 next,
564 int hdr_sz, int do_gso, int csum_offload,
565 int is_pci, int is_tun, int is_indirect,
566 int is_any_layout)
567{
568 u16 n_added = 0, flags = 0;
569 int is_l2 = !is_tun;
570 vring_packed_desc_t *d = &vring->packed_desc[next];
571 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
572 virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
573 u32 drop_inline = ~0;
574
575 clib_memset (hdr, 0, hdr_sz);
576
577 if (b->flags & VNET_BUFFER_F_GSO)
578 {
579 if (do_gso)
580 set_gso_offsets (b, hdr, is_l2);
581 else
582 {
583 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
584 goto done;
585 }
586 }
587 else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
588 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
589 {
590 if (csum_offload)
591 set_checksum_offsets (b, hdr, is_l2);
592 else
593 {
594 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
595 goto done;
596 }
597 }
598 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
599 {
600 virtio_tx_trace (vm, node, b, bi, is_tun);
601 }
602
603 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
604 {
605 d->addr =
606 ((is_pci) ? vlib_buffer_get_current_pa (vm,
607 b) :
608 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
609 d->len = b->current_length + hdr_sz;
610 }
611 else if (is_indirect)
612 {
613 /*
614 * We are using single vlib_buffer_t for indirect descriptor(s)
615 * chain. Single descriptor is 16 bytes and vlib_buffer_t
616 * has 2048 bytes space. So maximum long chain can have 128
617 * (=2048/16) indirect descriptors.
618 * It can easily support 65535 bytes of Jumbo frames with
619 * each data buffer size of 512 bytes minimum.
620 */
621 u32 indirect_buffer = 0;
622 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
623 {
624 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
625 goto done;
626 }
627
628 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
629 indirect_desc->current_data = 0;
630 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
631 indirect_desc->next_buffer = bi;
632 bi = indirect_buffer;
633
634 vring_packed_desc_t *id =
635 (vring_packed_desc_t *) vlib_buffer_get_current (indirect_desc);
636 u32 count = 1;
637 if (is_pci)
638 {
639 d->addr = vlib_physmem_get_pa (vm, id);
640 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
641
642 /*
643 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
644 * should be presented in separate descriptor and data will start
645 * from next descriptor.
646 */
647 if (is_any_layout)
648 id->len = b->current_length + hdr_sz;
649 else
650 {
651 id->len = hdr_sz;
652 id->flags = 0;
653 id->id = 0;
654 count++;
655 id++;
656 id->addr = vlib_buffer_get_current_pa (vm, b);
657 id->len = b->current_length;
658 }
659 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
660 {
661 id->flags = 0;
662 id->id = 0;
663 count++;
664 id++;
665 b = vlib_get_buffer (vm, b->next_buffer);
666 id->addr = vlib_buffer_get_current_pa (vm, b);
667 id->len = b->current_length;
668 }
669 }
670 id->flags = 0;
671 id->id = 0;
672 d->len = count * sizeof (vring_packed_desc_t);
673 flags = VRING_DESC_F_INDIRECT;
674 }
675 else
676 {
677 ASSERT (0);
678 }
679 if (vring->avail_wrap_counter)
680 {
681 flags |= VRING_DESC_F_AVAIL;
682 flags &= ~VRING_DESC_F_USED;
683 }
684 else
685 {
686 flags &= ~VRING_DESC_F_AVAIL;
687 flags |= VRING_DESC_F_USED;
688 }
689
690 d->id = next;
691 d->flags = flags;
692 vring->buffers[next] = bi;
693 n_added++;
694
695done:
696 if (drop_inline != ~0)
697 virtio_interface_drop_inline (vm, node->node_index, &bi, 1, drop_inline);
698
699 return n_added;
700}
701
702static_always_inline uword
703virtio_interface_tx_packed_gso_inline (vlib_main_t * vm,
704 vlib_node_runtime_t * node,
705 virtio_if_t * vif,
706 virtio_if_type_t type,
707 virtio_vring_t * vring,
708 u32 * buffers, u16 n_left,
709 const int do_gso,
710 const int csum_offload)
711{
712 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
713 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
714 int is_indirect =
715 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
716 int is_any_layout =
717 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
718 const int hdr_sz = vif->virtio_net_hdr_sz;
719 u16 sz = vring->size;
720 u16 used, next, n_buffers = 0, n_buffers_left = 0;
721 u16 n_vectors = n_left;
722
723
724 used = vring->desc_in_use;
725 next = vring->desc_next;
726
727 if (vif->packet_buffering)
728 {
729 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
730
731 while (n_buffers_left && used < sz)
732 {
733 u16 n_added = 0;
734
735 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
736 if (bi == ~0)
737 break;
738 n_added = add_buffer_to_slot_packed (vm, node,
739 vring, bi, next,
740 hdr_sz, do_gso, csum_offload,
741 is_pci, is_tun, is_indirect,
742 is_any_layout);
743 n_buffers_left--;
744 if (PREDICT_FALSE (n_added == 0))
745 continue;
746
747 used++;
748 next++;
749 if (next >= sz)
750 {
751 next = 0;
752 vring->avail_wrap_counter ^= 1;
753 }
754 }
755 }
756
757 while (n_left && used < sz)
758 {
759 u16 n_added = 0;
760
761 n_added = add_buffer_to_slot_packed (vm, node,
762 vring, buffers[0], next,
763 hdr_sz, do_gso, csum_offload,
764 is_pci, is_tun, is_indirect,
765 is_any_layout);
766 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
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100793static_always_inline void
794virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
795 u16 req, u16 next, u32 * first_free_desc_index,
796 u16 * free_desc_count)
797{
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
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100829static_always_inline u16
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100830virtio_interface_tx_split_gso_inline (vlib_main_t * vm,
831 vlib_node_runtime_t * node,
832 virtio_if_t * vif,
833 virtio_if_type_t type,
834 virtio_vring_t * vring, u32 * buffers,
835 u16 n_left, int do_gso,
836 int csum_offload)
Damjan Marion8389fb92017-10-13 18:29:53 +0200837{
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000838 u16 used, next, avail, n_buffers = 0, n_buffers_left = 0;
Damjan Mariond4a54712020-10-31 23:40:51 +0100839 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
840 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
841 int is_indirect =
842 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
843 int is_any_layout =
844 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
Damjan Marion8389fb92017-10-13 18:29:53 +0200845 u16 sz = vring->size;
Damjan Mariond4a54712020-10-31 23:40:51 +0100846 int hdr_sz = vif->virtio_net_hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200847 u16 mask = sz - 1;
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100848 u16 n_vectors = n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200849
850 used = vring->desc_in_use;
851 next = vring->desc_next;
852 avail = vring->avail->idx;
853
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100854 u16 free_desc_count = 0;
855
856 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
857 {
858 u32 first_free_desc_index = ~0;
859
860 virtio_find_free_desc (vring, sz, mask, n_left, next,
861 &first_free_desc_index, &free_desc_count);
862
863 if (free_desc_count)
864 next = first_free_desc_index;
865 }
866 else
867 free_desc_count = sz - used;
868
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000869 if (vif->packet_buffering)
870 {
871 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
872
873 while (n_buffers_left && free_desc_count)
874 {
875 u16 n_added = 0;
876
877 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
878 if (bi == ~0)
879 break;
Mohsin Kazmi83143712020-10-07 13:23:55 +0200880
Damjan Mariond4a54712020-10-31 23:40:51 +0100881 n_added = add_buffer_to_slot (vm, node, vring, bi, free_desc_count,
882 avail, next, mask, hdr_sz, do_gso,
883 csum_offload, is_pci, is_tun,
884 is_indirect, is_any_layout);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000885 if (PREDICT_FALSE (n_added == 0))
886 {
887 n_buffers_left--;
888 continue;
889 }
890 else if (PREDICT_FALSE (n_added > free_desc_count))
891 break;
892
893 avail++;
894 next = (next + n_added) & mask;
895 used += n_added;
896 n_buffers_left--;
897 free_desc_count -= n_added;
898 }
899 }
900
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100901 while (n_left && free_desc_count)
Damjan Marion8389fb92017-10-13 18:29:53 +0200902 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200903 u16 n_added = 0;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +0000904
Damjan Mariond4a54712020-10-31 23:40:51 +0100905 n_added = add_buffer_to_slot (vm, node, vring, buffers[0],
906 free_desc_count, avail, next, mask,
907 hdr_sz, do_gso, csum_offload, is_pci,
908 is_tun, is_indirect, is_any_layout);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200909
910 if (PREDICT_FALSE (n_added == 0))
911 {
912 buffers++;
913 n_left--;
914 continue;
915 }
Mohsin Kazmice215992020-04-10 14:57:06 +0000916 else if (PREDICT_FALSE (n_added > free_desc_count))
917 break;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200918
Mohsin Kazmice215992020-04-10 14:57:06 +0000919 avail++;
Damjan Marion8389fb92017-10-13 18:29:53 +0200920 next = (next + n_added) & mask;
921 used += n_added;
922 buffers++;
923 n_left--;
Mohsin Kazmice215992020-04-10 14:57:06 +0000924 free_desc_count -= n_added;
Damjan Marion8389fb92017-10-13 18:29:53 +0200925 }
926
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100927 if (n_left != n_vectors || n_buffers != n_buffers_left)
Damjan Marion8389fb92017-10-13 18:29:53 +0200928 {
929 CLIB_MEMORY_STORE_BARRIER ();
930 vring->avail->idx = avail;
931 vring->desc_next = next;
932 vring->desc_in_use = used;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200933 if ((vring->used->flags & VRING_USED_F_NO_NOTIFY) == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200934 virtio_kick (vm, vring, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200935 }
936
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100937 return n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +0200938}
939
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100940static_always_inline u16
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100941virtio_interface_tx_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
942 virtio_if_t * vif,
943 virtio_if_type_t type, virtio_vring_t * vring,
944 u32 * buffers, u16 n_left, int packed,
945 int do_gso, int csum_offload)
946{
947 if (packed)
948 return virtio_interface_tx_packed_gso_inline (vm, node, vif, type, vring,
949 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +0100950 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100951 else
952 return virtio_interface_tx_split_gso_inline (vm, node, vif, type, vring,
953 buffers, n_left,
Mohsin Kazmib18b1902020-12-03 16:35:20 +0100954 do_gso, csum_offload);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100955}
956
957static_always_inline u16
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200958virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100959 virtio_if_t * vif,
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +0200960 virtio_vring_t * vring, virtio_if_type_t type,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100961 u32 * buffers, u16 n_left, int packed)
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200962{
963 vnet_main_t *vnm = vnet_get_main ();
964 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
965
966 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100967 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100968 buffers, n_left, packed,
969 1 /* do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200970 1 /* checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200971 else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100972 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100973 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200974 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200975 1 /* checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200976 else
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +0100977 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100978 buffers, n_left, packed,
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200979 0 /* no do_gso */ ,
Damjan Mariondc181242020-10-20 12:11:06 +0200980 0 /* no checksum offload */ );
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200981}
982
Filip Tehlaraee73642019-03-13 05:50:44 -0700983VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
984 vlib_node_runtime_t * node,
985 vlib_frame_t * frame)
Damjan Marion8389fb92017-10-13 18:29:53 +0200986{
987 virtio_main_t *nm = &virtio_main;
988 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
989 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
Damjan Mariondc181242020-10-20 12:11:06 +0200990 u16 qid = vm->thread_index % vif->num_txqs;
991 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +0200992 u16 n_left = frame->n_vectors;
993 u32 *buffers = vlib_frame_vector_args (frame);
994 u32 to[GRO_TO_VECTOR_SIZE (n_left)];
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100995 int packed = vif->is_packed;
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +0100996 u16 n_vectors = frame->n_vectors;
Damjan Mariondc181242020-10-20 12:11:06 +0200997
998 clib_spinlock_lock_if_init (&vring->lockp);
999
Mohsin Kazmi24841862020-12-02 16:39:42 +01001000 if (packed && (vring->device_event->flags != VRING_EVENT_F_DISABLE))
1001 virtio_kick (vm, vring, vif);
1002 else if ((vring->used->flags & VRING_USED_F_NO_NOTIFY) == 0 &&
1003 (vring->last_kick_avail_idx != vring->avail->idx))
Damjan Mariondc181242020-10-20 12:11:06 +02001004 virtio_kick (vm, vring, vif);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001005
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001006 if (vif->packet_coalesce)
1007 {
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001008 n_vectors = n_left =
1009 vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
Mohsin Kazmiee1e30f2020-10-23 11:23:52 +02001010 buffers = to;
1011 }
1012
Damjan Marion587f9132020-10-31 22:47:01 +01001013 u16 retry_count = 2;
1014
1015retry:
1016 /* free consumed buffers */
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001017 virtio_free_used_device_desc (vm, vring, node->node_index, packed);
Damjan Marion587f9132020-10-31 22:47:01 +01001018
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001019 if (vif->type == VIRTIO_IF_TYPE_TAP)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001020 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001021 VIRTIO_IF_TYPE_TAP,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001022 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001023 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001024 else if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001025 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001026 VIRTIO_IF_TYPE_PCI,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001027 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001028 n_left, packed);
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001029 else if (vif->type == VIRTIO_IF_TYPE_TUN)
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001030 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001031 VIRTIO_IF_TYPE_TUN,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001032 &buffers[n_vectors - n_left],
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001033 n_left, packed);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001034 else
Mohsin Kazmi38b09682020-06-03 18:20:17 +02001035 ASSERT (0);
1036
Damjan Marion587f9132020-10-31 22:47:01 +01001037 if (n_left && retry_count--)
1038 goto retry;
1039
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001040 if (vif->packet_buffering && n_left)
1041 {
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001042 u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering,
1043 &buffers
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001044 [n_vectors
Mohsin Kazmi81dac062020-11-11 20:28:35 +01001045 - n_left],
1046 n_left);
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001047 n_left -= n_buffered;
1048 }
1049 if (n_left)
1050 virtio_interface_drop_inline (vm, node->node_index,
Mohsin Kazmie2e9fe52020-12-17 15:54:03 +01001051 &buffers[n_vectors - n_left], n_left,
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001052 VIRTIO_TX_ERROR_NO_FREE_SLOTS);
1053
Damjan Mariondc181242020-10-20 12:11:06 +02001054 clib_spinlock_unlock_if_init (&vring->lockp);
1055
Mohsin Kazmi2e6f6242020-10-26 12:41:41 +01001056 return frame->n_vectors - n_left;
Damjan Marion8389fb92017-10-13 18:29:53 +02001057}
1058
1059static void
1060virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1061 u32 node_index)
1062{
1063 virtio_main_t *apm = &virtio_main;
1064 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1065 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
1066
1067 /* Shut off redirection */
1068 if (node_index == ~0)
1069 {
1070 vif->per_interface_next_index = node_index;
1071 return;
1072 }
1073
1074 vif->per_interface_next_index =
1075 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
1076 node_index);
1077}
1078
1079static void
1080virtio_clear_hw_interface_counters (u32 instance)
1081{
1082 /* Nothing for now */
1083}
1084
Mohsin Kazmi24841862020-12-02 16:39:42 +01001085static_always_inline void
1086virtio_set_rx_interrupt (virtio_if_t * vif, virtio_vring_t * vring)
1087{
1088 if (vif->is_packed)
1089 vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE;
1090 else
1091 vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
1092}
1093
1094static_always_inline void
1095virtio_set_rx_polling (virtio_if_t * vif, virtio_vring_t * vring)
1096{
1097 if (vif->is_packed)
1098 vring->driver_event->flags |= VRING_EVENT_F_DISABLE;
1099 else
1100 vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
1101}
1102
Damjan Marion8389fb92017-10-13 18:29:53 +02001103static clib_error_t *
1104virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
Damjan Marioneabd4242020-10-07 20:59:07 +02001105 vnet_hw_if_rx_mode mode)
Damjan Marion8389fb92017-10-13 18:29:53 +02001106{
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001107 vlib_main_t *vm = vnm->vlib_main;
Damjan Marion8389fb92017-10-13 18:29:53 +02001108 virtio_main_t *mm = &virtio_main;
1109 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1110 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001111 virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
Damjan Marion8389fb92017-10-13 18:29:53 +02001112
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001113 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
1114 {
Mohsin Kazmi24841862020-12-02 16:39:42 +01001115 virtio_set_rx_polling (vif, rx_vring);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001116 return clib_error_return (0, "interrupt mode is not supported");
1117 }
1118
Damjan Marioneabd4242020-10-07 20:59:07 +02001119 if (mode == VNET_HW_IF_RX_MODE_POLLING)
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001120 {
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001121 if (vif->packet_coalesce || vif->packet_buffering)
1122 {
1123 if (mm->interrupt_queues_count > 0)
1124 mm->interrupt_queues_count--;
1125 if (mm->interrupt_queues_count == 0)
1126 vlib_process_signal_event (vm,
1127 virtio_send_interrupt_node.index,
1128 VIRTIO_EVENT_STOP_TIMER, 0);
1129 }
Mohsin Kazmi24841862020-12-02 16:39:42 +01001130 virtio_set_rx_polling (vif, rx_vring);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001131 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001132 else
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001133 {
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001134 if (vif->packet_coalesce || vif->packet_buffering)
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001135 {
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001136 mm->interrupt_queues_count++;
1137 if (mm->interrupt_queues_count == 1)
1138 vlib_process_signal_event (vm,
1139 virtio_send_interrupt_node.index,
1140 VIRTIO_EVENT_START_TIMER, 0);
Mohsin Kazmi1017a1d2020-09-25 15:36:19 +02001141 }
Mohsin Kazmi24841862020-12-02 16:39:42 +01001142 virtio_set_rx_interrupt (vif, rx_vring);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +02001143 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001144
Mohsin Kazmia5203b52020-10-12 13:01:24 +02001145 rx_vring->mode = mode;
1146
Damjan Marion8389fb92017-10-13 18:29:53 +02001147 return 0;
1148}
1149
1150static clib_error_t *
1151virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1152{
1153 virtio_main_t *mm = &virtio_main;
1154 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1155 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +02001156
1157 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001158 {
1159 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1160 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1161 VNET_HW_INTERFACE_FLAG_LINK_UP);
1162 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001163 else
Mohsin Kazmi529f0e92020-03-19 14:03:31 +01001164 {
1165 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
1166 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1167 }
Damjan Marion8389fb92017-10-13 18:29:53 +02001168 return 0;
1169}
1170
1171static clib_error_t *
1172virtio_subif_add_del_function (vnet_main_t * vnm,
1173 u32 hw_if_index,
1174 struct vnet_sw_interface_t *st, int is_add)
1175{
1176 /* Nothing for now */
1177 return 0;
1178}
1179
1180/* *INDENT-OFF* */
1181VNET_DEVICE_CLASS (virtio_device_class) = {
1182 .name = "virtio",
Damjan Marion8389fb92017-10-13 18:29:53 +02001183 .format_device_name = format_virtio_device_name,
1184 .format_device = format_virtio_device,
1185 .format_tx_trace = format_virtio_tx_trace,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001186 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +02001187 .tx_function_error_strings = virtio_tx_func_error_strings,
1188 .rx_redirect_to_node = virtio_set_interface_next_node,
1189 .clear_counters = virtio_clear_hw_interface_counters,
1190 .admin_up_down_function = virtio_interface_admin_up_down,
1191 .subif_add_del_function = virtio_subif_add_del_function,
1192 .rx_mode_change_function = virtio_interface_rx_mode_change,
1193};
Mohsin Kazmi206acf82020-04-06 14:19:54 +02001194
Damjan Marion8389fb92017-10-13 18:29:53 +02001195/* *INDENT-ON* */
1196
1197/*
1198 * fd.io coding-style-patch-verification: ON
1199 *
1200 * Local Variables:
1201 * eval: (c-set-style "gnu")
1202 * End:
1203 */