blob: 45c390d415df8a59225aacd084c6a57d433d1208 [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 Kazmi0b042092020-04-17 16:50:56 +000026#include <vnet/gso/hdr_offset_parser.h>
Milan Lenco73e7f422017-12-14 10:04:25 +010027#include <vnet/ip/ip4_packet.h>
28#include <vnet/ip/ip6_packet.h>
Mohsin Kazmi6d4af892020-01-03 15:11:53 +000029#include <vnet/tcp/tcp_packet.h>
30#include <vnet/udp/udp_packet.h>
Damjan Marion8389fb92017-10-13 18:29:53 +020031#include <vnet/devices/virtio/virtio.h>
32
33#define foreach_virtio_tx_func_error \
34_(NO_FREE_SLOTS, "no free tx slots") \
35_(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
36_(PENDING_MSGS, "pending msgs in tx ring") \
Mohsin Kazmif50ef402020-04-09 22:11:29 +020037_(INDIRECT_DESC_ALLOC_FAILED, "indirect descriptor allocation failed - packet drop") \
38_(OUT_OF_ORDER, "out-of-order buffers in used ring") \
39_(GSO_PACKET_DROP, "gso disabled on itf -- gso packet drop") \
40_(CSUM_OFFLOAD_PACKET_DROP, "checksum offload disabled on itf -- csum offload packet drop")
Damjan Marion8389fb92017-10-13 18:29:53 +020041
42typedef enum
43{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020044#define _(f,s) VIRTIO_TX_ERROR_##f,
Damjan Marion8389fb92017-10-13 18:29:53 +020045 foreach_virtio_tx_func_error
46#undef _
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020047 VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +020048} virtio_tx_func_error_t;
49
50static char *virtio_tx_func_error_strings[] = {
51#define _(n,s) s,
52 foreach_virtio_tx_func_error
53#undef _
54};
55
Damjan Marion8389fb92017-10-13 18:29:53 +020056static u8 *
57format_virtio_device (u8 * s, va_list * args)
58{
59 u32 dev_instance = va_arg (*args, u32);
60 int verbose = va_arg (*args, int);
61 u32 indent = format_get_indent (s);
62
63 s = format (s, "VIRTIO interface");
64 if (verbose)
65 {
66 s = format (s, "\n%U instance %u", format_white_space, indent + 2,
67 dev_instance);
68 }
69 return s;
70}
71
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000072typedef struct
Damjan Marion8389fb92017-10-13 18:29:53 +020073{
Mohsin Kazmib5ca5592020-06-26 13:48:26 +000074 u32 buffer_index;
75 u32 sw_if_index;
76 vlib_buffer_t buffer;
77 generic_header_offset_t gho;
78} virtio_tx_trace_t;
79
80static u8 *
81format_virtio_tx_trace (u8 * s, va_list * va)
82{
83 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
84 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
85 virtio_tx_trace_t *t = va_arg (*va, virtio_tx_trace_t *);
86 u32 indent = format_get_indent (s);
87
88 s = format (s, "%U ", format_generic_header_offset, &t->gho);
89 s = format (s, "%Ubuffer 0x%x: %U",
90 format_white_space, indent,
91 t->buffer_index, format_vnet_buffer, &t->buffer);
92
93 s = format (s, "\n%U%U", format_white_space, indent,
94 format_ethernet_header_with_length, t->buffer.pre_data,
95 sizeof (t->buffer.pre_data));
Damjan Marion8389fb92017-10-13 18:29:53 +020096 return s;
97}
98
Mohsin Kazmiaea0df32019-05-23 14:32:58 +020099static_always_inline void
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200100virtio_interface_drop_inline (vlib_main_t * vm, uword node_index,
101 u32 * buffers, u16 n,
102 virtio_tx_func_error_t error)
103{
104 vlib_error_count (vm, node_index, error, n);
105 vlib_buffer_free (vm, buffers, n);
106}
107
108static_always_inline void
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100109virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers)
110{
111 ASSERT (n_buffers <= ring_size);
112
113 if (PREDICT_TRUE (start + n_buffers <= ring_size))
114 {
115 clib_memset_u32 (ring + start, ~0, n_buffers);
116 }
117 else
118 {
119 clib_memset_u32 (ring + start, ~0, ring_size - start);
120 clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
121 }
122}
123
124static_always_inline void
125virtio_free_used_device_desc (vlib_main_t * vm, virtio_vring_t * vring,
126 uword node_index)
Damjan Marion8389fb92017-10-13 18:29:53 +0200127{
128 u16 used = vring->desc_in_use;
129 u16 sz = vring->size;
130 u16 mask = sz - 1;
131 u16 last = vring->last_used_idx;
132 u16 n_left = vring->used->idx - last;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100133 u16 out_of_order_count = 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200134
135 if (n_left == 0)
136 return;
137
138 while (n_left)
139 {
140 struct vring_used_elem *e = &vring->used->ring[last & mask];
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200141 u16 slot, n_buffers;
142 slot = n_buffers = e->id;
Damjan Marion8389fb92017-10-13 18:29:53 +0200143
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100144 while (e->id == (n_buffers & mask))
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200145 {
146 n_left--;
147 last++;
148 n_buffers++;
Mohsin Kazmice215992020-04-10 14:57:06 +0000149 struct vring_desc *d = &vring->desc[e->id];
150 u16 next;
151 while (d->flags & VRING_DESC_F_NEXT)
152 {
153 n_buffers++;
154 next = d->next;
155 d = &vring->desc[next];
156 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200157 if (n_left == 0)
158 break;
159 e = &vring->used->ring[last & mask];
160 }
161 vlib_buffer_free_from_ring (vm, vring->buffers, slot,
162 sz, (n_buffers - slot));
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100163 virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200164 used -= (n_buffers - slot);
165
166 if (n_left > 0)
167 {
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100168 vlib_buffer_free (vm, &vring->buffers[e->id], 1);
169 vring->buffers[e->id] = ~0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200170 used--;
171 last++;
172 n_left--;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100173 out_of_order_count++;
174 vring->flags |= VRING_TX_OUT_OF_ORDER;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200175 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200176 }
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100177
178 /*
179 * Some vhost-backends give buffers back in out-of-order fashion in used ring.
180 * It impacts the overall virtio-performance.
181 */
182 if (out_of_order_count)
183 vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
184 out_of_order_count);
185
Damjan Marion8389fb92017-10-13 18:29:53 +0200186 vring->desc_in_use = used;
187 vring->last_used_idx = last;
188}
189
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000190static_always_inline void
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200191set_checksum_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr,
192 int is_l2)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000193{
194 if (b->flags & VNET_BUFFER_F_IS_IP4)
195 {
196 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000197 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200198 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
199 0 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000200 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
201 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
202 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000203 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000204 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
205 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000206 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000207 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000208 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
209 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000210
211 /*
212 * virtio devices do not support IP4 checksum offload. So driver takes care
213 * of it while doing tx.
214 */
215 ip4 =
216 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
217 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
218 ip4->checksum = ip4_header_checksum (ip4);
219 }
220 else if (b->flags & VNET_BUFFER_F_IS_IP6)
221 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000222 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200223 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
224 1 /* ip6 */ );
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000225 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
226 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
227 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000228 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000229 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
230 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000231 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000232 {
Mohsin Kazmi0937fdf2020-03-25 20:37:16 +0000233 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
234 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000235 }
236}
237
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200238static_always_inline void
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200239set_gso_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr, int is_l2)
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200240{
241 if (b->flags & VNET_BUFFER_F_IS_IP4)
242 {
243 ip4_header_t *ip4;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000244 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200245 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
246 0 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200247 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
248 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000249 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200250 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
251 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
252 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
253 ip4 =
254 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
255 /*
256 * virtio devices do not support IP4 checksum offload. So driver takes care
257 * of it while doing tx.
258 */
259 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
260 ip4->checksum = ip4_header_checksum (ip4);
261 }
262 else if (b->flags & VNET_BUFFER_F_IS_IP6)
263 {
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000264 generic_header_offset_t gho = { 0 };
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200265 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
266 1 /* ip6 */ );
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200267 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
268 hdr->gso_size = vnet_buffer2 (b)->gso_size;
Mohsin Kazmi0b042092020-04-17 16:50:56 +0000269 hdr->hdr_len = gho.hdr_sz;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200270 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
271 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
272 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
273 }
274}
275
Damjan Marion8389fb92017-10-13 18:29:53 +0200276static_always_inline u16
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200277add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200278 virtio_if_type_t type, virtio_vring_t * vring,
279 u32 bi, u16 free_desc_count,
Mohsin Kazmice215992020-04-10 14:57:06 +0000280 u16 avail, u16 next, u16 mask, int do_gso,
281 int csum_offload, uword node_index)
Damjan Marion8389fb92017-10-13 18:29:53 +0200282{
283 u16 n_added = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200284 int hdr_sz = vif->virtio_net_hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200285 struct vring_desc *d;
286 d = &vring->desc[next];
287 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Damjan Marion508cabc2018-02-08 19:49:22 +0100288 struct virtio_net_hdr_v1 *hdr = vlib_buffer_get_current (b) - hdr_sz;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200289 int is_l2 = (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI));
Damjan Marion508cabc2018-02-08 19:49:22 +0100290
Dave Barachb7b92992018-10-17 10:38:51 -0400291 clib_memset (hdr, 0, hdr_sz);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000292
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200293 if (b->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200294 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200295 if (do_gso)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200296 set_gso_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200297 else
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200298 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200299 virtio_interface_drop_inline (vm, node_index, &bi, 1,
300 VIRTIO_TX_ERROR_GSO_PACKET_DROP);
301 return n_added;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200302 }
303 }
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200304 else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
305 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000306 {
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200307 if (csum_offload)
Mohsin Kazmi84f91fa2020-04-23 17:59:49 +0200308 set_checksum_offsets (b, hdr, is_l2);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200309 else
310 {
311 virtio_interface_drop_inline (vm, node_index, &bi, 1,
312 VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP);
313 return n_added;
314 }
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000315 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200316
317 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
318 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200319 d->addr =
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200320 ((type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
321 b) :
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200322 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200323 d->len = b->current_length + hdr_sz;
324 d->flags = 0;
325 }
Mohsin Kazmice215992020-04-10 14:57:06 +0000326 else if (vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC))
Damjan Marion8389fb92017-10-13 18:29:53 +0200327 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200328 /*
329 * We are using single vlib_buffer_t for indirect descriptor(s)
330 * chain. Single descriptor is 16 bytes and vlib_buffer_t
331 * has 2048 bytes space. So maximum long chain can have 128
332 * (=2048/16) indirect descriptors.
333 * It can easily support 65535 bytes of Jumbo frames with
334 * each data buffer size of 512 bytes minimum.
335 */
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200336 u32 indirect_buffer = 0;
337 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200338 {
339 virtio_interface_drop_inline (vm, node_index, &bi, 1,
340 VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED);
341 return n_added;
342 }
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200343
344 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200345 indirect_desc->current_data = 0;
Mohsin Kazmiaea0df32019-05-23 14:32:58 +0200346 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
347 indirect_desc->next_buffer = bi;
348 bi = indirect_buffer;
Damjan Marion8389fb92017-10-13 18:29:53 +0200349
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200350 struct vring_desc *id =
351 (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
352 u32 count = 1;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200353 if (type == VIRTIO_IF_TYPE_PCI)
Damjan Marion8389fb92017-10-13 18:29:53 +0200354 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200355 d->addr = vlib_physmem_get_pa (vm, id);
356 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
Damjan Marion8389fb92017-10-13 18:29:53 +0200357
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200358 /*
359 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
360 * should be presented in separate descriptor and data will start
361 * from next descriptor.
362 */
363 if (PREDICT_TRUE
364 (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
365 id->len = b->current_length + hdr_sz;
366 else
367 {
368 id->len = hdr_sz;
369 id->flags = VRING_DESC_F_NEXT;
370 id->next = count;
371 count++;
372 id++;
373 id->addr = vlib_buffer_get_current_pa (vm, b);
374 id->len = b->current_length;
375 }
376 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
377 {
378 id->flags = VRING_DESC_F_NEXT;
379 id->next = count;
380 count++;
381 id++;
382 b = vlib_get_buffer (vm, b->next_buffer);
383 id->addr = vlib_buffer_get_current_pa (vm, b);
384 id->len = b->current_length;
385 }
386 }
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200387 else /* VIRTIO_IF_TYPE_[TAP | TUN] */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200388 {
389 d->addr = pointer_to_uword (id);
390 /* first buffer in chain */
391 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
392 id->len = b->current_length + hdr_sz;
393
394 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
395 {
396 id->flags = VRING_DESC_F_NEXT;
397 id->next = count;
398 count++;
399 id++;
400 b = vlib_get_buffer (vm, b->next_buffer);
401 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
402 id->len = b->current_length;
403 }
404 }
405 id->flags = 0;
406 id->next = 0;
407 d->len = count * sizeof (struct vring_desc);
Damjan Marion8389fb92017-10-13 18:29:53 +0200408 d->flags = VRING_DESC_F_INDIRECT;
409 }
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200410 else if (type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmice215992020-04-10 14:57:06 +0000411 {
412 u16 count = next;
413 vlib_buffer_t *b_temp = b;
414 u16 n_buffers_in_chain = 1;
415
416 /*
417 * Check the length of the chain for the required number of
418 * descriptors. Return from here, retry to get more descriptors,
419 * if chain length is greater than available descriptors.
420 */
421 while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
422 {
423 n_buffers_in_chain++;
424 b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
425 }
426
427 if (n_buffers_in_chain > free_desc_count)
428 return n_buffers_in_chain;
429
430 d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
431 d->len = b->current_length + hdr_sz;
432
433 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
434 {
435 d->flags = VRING_DESC_F_NEXT;
436 vring->buffers[count] = bi;
437 b->flags &=
438 ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
439 bi = b->next_buffer;
440 b->next_buffer = 0;
441 n_added++;
442 count = (count + 1) & mask;
443 d->next = count;
444 d = &vring->desc[count];
445 b = vlib_get_buffer (vm, bi);
446 d->addr = vlib_buffer_get_current_pa (vm, b);
447 d->len = b->current_length;
448 }
449 d->flags = 0;
450 vring->buffers[count] = bi;
451 vring->avail->ring[avail & mask] = next;
452 n_added++;
453 return n_added;
454 }
455 else
456 {
457 ASSERT (0);
458 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200459 vring->buffers[next] = bi;
460 vring->avail->ring[avail & mask] = next;
461 n_added++;
462 return n_added;
463}
464
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100465static_always_inline void
466virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
467 u16 req, u16 next, u32 * first_free_desc_index,
468 u16 * free_desc_count)
469{
470 u16 start = 0;
471 /* next is used as hint: from where to start looking */
472 for (u16 i = 0; i < size; i++, next++)
473 {
474 if (vring->buffers[next & mask] == ~0)
475 {
476 if (*first_free_desc_index == ~0)
477 {
478 *first_free_desc_index = (next & mask);
479 start = i;
480 (*free_desc_count)++;
481 req--;
482 if (req == 0)
483 break;
484 }
485 else
486 {
487 if (start + *free_desc_count == i)
488 {
489 (*free_desc_count)++;
490 req--;
491 if (req == 0)
492 break;
493 }
494 else
495 break;
496 }
497 }
498 }
499}
500
Damjan Marion8389fb92017-10-13 18:29:53 +0200501static_always_inline uword
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200502virtio_interface_tx_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
503 vlib_frame_t * frame, virtio_if_t * vif,
504 virtio_if_type_t type, int do_gso,
505 int csum_offload)
Damjan Marion8389fb92017-10-13 18:29:53 +0200506{
Damjan Marion8389fb92017-10-13 18:29:53 +0200507 u16 n_left = frame->n_vectors;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000508 virtio_vring_t *vring;
509 u16 qid = vm->thread_index % vif->num_txqs;
510 vring = vec_elt_at_index (vif->txq_vrings, qid);
Damjan Marion8389fb92017-10-13 18:29:53 +0200511 u16 used, next, avail;
512 u16 sz = vring->size;
513 u16 mask = sz - 1;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100514 u16 retry_count = 2;
Damjan Mariona3d59862018-11-10 10:23:00 +0100515 u32 *buffers = vlib_frame_vector_args (frame);
Damjan Marion8389fb92017-10-13 18:29:53 +0200516
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000517 clib_spinlock_lock_if_init (&vring->lockp);
Damjan Marion829ee532018-02-16 16:13:32 +0100518
Damjan Marione40231b2018-12-20 10:44:47 +0100519 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200520 (vring->last_kick_avail_idx != vring->avail->idx))
521 virtio_kick (vm, vring, vif);
Damjan Marione40231b2018-12-20 10:44:47 +0100522
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100523retry:
Damjan Marion8389fb92017-10-13 18:29:53 +0200524 /* free consumed buffers */
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100525 virtio_free_used_device_desc (vm, vring, node->node_index);
Damjan Marion8389fb92017-10-13 18:29:53 +0200526
527 used = vring->desc_in_use;
528 next = vring->desc_next;
529 avail = vring->avail->idx;
530
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100531 u16 free_desc_count = 0;
532
533 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
534 {
535 u32 first_free_desc_index = ~0;
536
537 virtio_find_free_desc (vring, sz, mask, n_left, next,
538 &first_free_desc_index, &free_desc_count);
539
540 if (free_desc_count)
541 next = first_free_desc_index;
542 }
543 else
544 free_desc_count = sz - used;
545
546 while (n_left && free_desc_count)
Damjan Marion8389fb92017-10-13 18:29:53 +0200547 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200548 u16 n_added = 0;
Mohsin Kazmib5ca5592020-06-26 13:48:26 +0000549 virtio_tx_trace_t *t;
550
551 vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[0]);
552 if (b0->flags & VLIB_BUFFER_IS_TRACED)
553 {
554 t = vlib_add_trace (vm, node, b0, sizeof (t[0]));
555 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
556 t->buffer_index = buffers[0];
557 if (type == VIRTIO_IF_TYPE_TUN)
558 {
559 int is_ip4 = 0, is_ip6 = 0;
560
561 switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0)
562 {
563 case 0x40:
564 is_ip4 = 1;
565 break;
566 case 0x60:
567 is_ip6 = 1;
568 break;
569 default:
570 break;
571 }
572 vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4,
573 is_ip6);
574 }
575 else
576 vnet_generic_header_offset_parser (b0, &t->gho, 1,
577 b0->flags &
578 VNET_BUFFER_F_IS_IP4,
579 b0->flags &
580 VNET_BUFFER_F_IS_IP6);
581
582 clib_memcpy_fast (&t->buffer, b0,
583 sizeof (*b0) - sizeof (b0->pre_data));
584 clib_memcpy_fast (t->buffer.pre_data, vlib_buffer_get_current (b0),
585 sizeof (t->buffer.pre_data));
586 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200587 n_added =
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200588 add_buffer_to_slot (vm, vif, type, vring, buffers[0], free_desc_count,
Mohsin Kazmice215992020-04-10 14:57:06 +0000589 avail, next, mask, do_gso, csum_offload,
590 node->node_index);
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200591
592 if (PREDICT_FALSE (n_added == 0))
593 {
594 buffers++;
595 n_left--;
596 continue;
597 }
Mohsin Kazmice215992020-04-10 14:57:06 +0000598 else if (PREDICT_FALSE (n_added > free_desc_count))
599 break;
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200600
Mohsin Kazmice215992020-04-10 14:57:06 +0000601 avail++;
Damjan Marion8389fb92017-10-13 18:29:53 +0200602 next = (next + n_added) & mask;
603 used += n_added;
604 buffers++;
605 n_left--;
Mohsin Kazmice215992020-04-10 14:57:06 +0000606 free_desc_count -= n_added;
Damjan Marion8389fb92017-10-13 18:29:53 +0200607 }
608
609 if (n_left != frame->n_vectors)
610 {
611 CLIB_MEMORY_STORE_BARRIER ();
612 vring->avail->idx = avail;
613 vring->desc_next = next;
614 vring->desc_in_use = used;
615 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200616 virtio_kick (vm, vring, vif);
Damjan Marion8389fb92017-10-13 18:29:53 +0200617 }
618
Damjan Marion8389fb92017-10-13 18:29:53 +0200619 if (n_left)
620 {
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100621 if (retry_count--)
622 goto retry;
623
Mohsin Kazmif50ef402020-04-09 22:11:29 +0200624 virtio_interface_drop_inline (vm, node->node_index, buffers, n_left,
625 VIRTIO_TX_ERROR_NO_FREE_SLOTS);
Damjan Marion8389fb92017-10-13 18:29:53 +0200626 }
627
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000628 clib_spinlock_unlock_if_init (&vring->lockp);
Damjan Marion829ee532018-02-16 16:13:32 +0100629
Damjan Marion8389fb92017-10-13 18:29:53 +0200630 return frame->n_vectors - n_left;
631}
632
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200633static_always_inline uword
634virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
635 vlib_frame_t * frame, virtio_if_t * vif,
636 virtio_if_type_t type)
637{
638 vnet_main_t *vnm = vnet_get_main ();
639 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
640
641 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
642 return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
643 1 /* do_gso */ ,
644 1 /* checksum offload */ );
645 else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
646 return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
647 0 /* no do_gso */ ,
648 1 /* checksum offload */ );
649 else
650 return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
651 0 /* no do_gso */ ,
652 0 /* no checksum offload */ );
653}
654
Filip Tehlaraee73642019-03-13 05:50:44 -0700655VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
656 vlib_node_runtime_t * node,
657 vlib_frame_t * frame)
Damjan Marion8389fb92017-10-13 18:29:53 +0200658{
659 virtio_main_t *nm = &virtio_main;
660 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
661 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000662
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200663 if (vif->type == VIRTIO_IF_TYPE_TAP)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000664 return virtio_interface_tx_inline (vm, node, frame, vif,
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200665 VIRTIO_IF_TYPE_TAP);
666 else if (vif->type == VIRTIO_IF_TYPE_PCI)
667 return virtio_interface_tx_inline (vm, node, frame, vif,
668 VIRTIO_IF_TYPE_PCI);
669 else if (vif->type == VIRTIO_IF_TYPE_TUN)
670 return virtio_interface_tx_inline (vm, node, frame, vif,
671 VIRTIO_IF_TYPE_TUN);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200672 else
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200673 ASSERT (0);
674
675 return 0;
Damjan Marion8389fb92017-10-13 18:29:53 +0200676}
677
678static void
679virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
680 u32 node_index)
681{
682 virtio_main_t *apm = &virtio_main;
683 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
684 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
685
686 /* Shut off redirection */
687 if (node_index == ~0)
688 {
689 vif->per_interface_next_index = node_index;
690 return;
691 }
692
693 vif->per_interface_next_index =
694 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
695 node_index);
696}
697
698static void
699virtio_clear_hw_interface_counters (u32 instance)
700{
701 /* Nothing for now */
702}
703
704static clib_error_t *
705virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
706 vnet_hw_interface_rx_mode mode)
707{
708 virtio_main_t *mm = &virtio_main;
709 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
710 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000711 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
Damjan Marion8389fb92017-10-13 18:29:53 +0200712
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000713 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
714 {
715 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
716 return clib_error_return (0, "interrupt mode is not supported");
717 }
718
Damjan Marion8389fb92017-10-13 18:29:53 +0200719 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
720 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
721 else
722 vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
723
724 return 0;
725}
726
727static clib_error_t *
728virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
729{
730 virtio_main_t *mm = &virtio_main;
731 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
732 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
Damjan Marion8389fb92017-10-13 18:29:53 +0200733
734 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
Mohsin Kazmi529f0e92020-03-19 14:03:31 +0100735 {
736 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
737 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
738 VNET_HW_INTERFACE_FLAG_LINK_UP);
739 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200740 else
Mohsin Kazmi529f0e92020-03-19 14:03:31 +0100741 {
742 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
743 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
744 }
Damjan Marion8389fb92017-10-13 18:29:53 +0200745 return 0;
746}
747
748static clib_error_t *
749virtio_subif_add_del_function (vnet_main_t * vnm,
750 u32 hw_if_index,
751 struct vnet_sw_interface_t *st, int is_add)
752{
753 /* Nothing for now */
754 return 0;
755}
756
757/* *INDENT-OFF* */
758VNET_DEVICE_CLASS (virtio_device_class) = {
759 .name = "virtio",
Damjan Marion8389fb92017-10-13 18:29:53 +0200760 .format_device_name = format_virtio_device_name,
761 .format_device = format_virtio_device,
762 .format_tx_trace = format_virtio_tx_trace,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200763 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
Damjan Marion8389fb92017-10-13 18:29:53 +0200764 .tx_function_error_strings = virtio_tx_func_error_strings,
765 .rx_redirect_to_node = virtio_set_interface_next_node,
766 .clear_counters = virtio_clear_hw_interface_counters,
767 .admin_up_down_function = virtio_interface_admin_up_down,
768 .subif_add_del_function = virtio_subif_add_del_function,
769 .rx_mode_change_function = virtio_interface_rx_mode_change,
770};
Mohsin Kazmi206acf82020-04-06 14:19:54 +0200771
Damjan Marion8389fb92017-10-13 18:29:53 +0200772/* *INDENT-ON* */
773
774/*
775 * fd.io coding-style-patch-verification: ON
776 *
777 * Local Variables:
778 * eval: (c-set-style "gnu")
779 * End:
780 */