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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
18 #include <sys/types.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/vnet.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/gso/gro_func.h>
27 #include <vnet/gso/hdr_offset_parser.h>
28 #include <vnet/ip/ip4_packet.h>
29 #include <vnet/ip/ip6_packet.h>
30 #include <vnet/tcp/tcp_packet.h>
31 #include <vnet/udp/udp_packet.h>
32 #include <vnet/devices/virtio/virtio.h>
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") \
38 _(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")
45 #define _(f,s) VIRTIO_TX_ERROR_##f,
46 foreach_virtio_tx_func_error
49 } virtio_tx_func_error_t;
51 static char *virtio_tx_func_error_strings[] = {
53 foreach_virtio_tx_func_error
58 format_virtio_device (u8 * s, va_list * args)
60 u32 dev_instance = va_arg (*args, u32);
61 int verbose = va_arg (*args, int);
62 u32 indent = format_get_indent (s);
64 s = format (s, "VIRTIO interface");
67 s = format (s, "\n%U instance %u", format_white_space, indent + 2,
77 generic_header_offset_t gho;
82 format_virtio_tx_trace (u8 * s, va_list * va)
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);
89 s = format (s, "%Ubuffer 0x%x: %U\n",
90 format_white_space, indent,
91 t->buffer_index, format_vnet_buffer, &t->buffer);
93 format (s, "%U%U\n", format_white_space, indent,
94 format_generic_header_offset, &t->gho);
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));
103 virtio_tx_trace (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b0,
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;
112 int is_ip4 = 0, is_ip6 = 0;
114 switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0)
125 vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4, is_ip6);
128 vnet_generic_header_offset_parser (b0, &t->gho, 1,
130 VNET_BUFFER_F_IS_IP4,
131 b0->flags & VNET_BUFFER_F_IS_IP6);
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));
139 virtio_interface_drop_inline (vlib_main_t *vm, virtio_if_t *vif,
140 uword node_index, u32 *buffers, u16 n,
141 virtio_tx_func_error_t error)
143 vlib_error_count (vm, node_index, error, n);
144 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
145 VNET_INTERFACE_COUNTER_DROP,
146 vm->thread_index, vif->sw_if_index, n);
147 vlib_buffer_free (vm, buffers, n);
151 virtio_memset_ring_u32 (u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
153 ASSERT (n_buffers <= ring_size);
155 if (PREDICT_TRUE (start + n_buffers <= ring_size))
157 clib_memset_u32 (ring + start, ~0, n_buffers);
161 clib_memset_u32 (ring + start, ~0, ring_size - start);
162 clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
167 virtio_free_used_device_desc_split (vlib_main_t *vm, virtio_vring_t *vring,
170 u16 used = vring->desc_in_use;
171 u16 sz = vring->size;
173 u16 last = vring->last_used_idx;
174 u16 n_left = vring->used->idx - last;
175 u16 out_of_order_count = 0;
182 vring_used_elem_t *e = &vring->used->ring[last & mask];
184 slot = n_buffers = e->id;
186 while (e->id == (n_buffers & mask))
191 vring_desc_t *d = &vring->desc[e->id];
193 while (d->flags & VRING_DESC_F_NEXT)
197 d = &vring->desc[next];
201 e = &vring->used->ring[last & mask];
203 vlib_buffer_free_from_ring (vm, vring->buffers, slot,
204 sz, (n_buffers - slot));
205 virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
206 used -= (n_buffers - slot);
210 vlib_buffer_free (vm, &vring->buffers[e->id], 1);
211 vring->buffers[e->id] = ~0;
215 out_of_order_count++;
216 vring->flags |= VRING_TX_OUT_OF_ORDER;
221 * Some vhost-backends give buffers back in out-of-order fashion in used ring.
222 * It impacts the overall virtio-performance.
224 if (out_of_order_count)
225 vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
228 vring->desc_in_use = used;
229 vring->last_used_idx = last;
233 virtio_free_used_device_desc_packed (vlib_main_t *vm, virtio_vring_t *vring,
236 vring_packed_desc_t *d;
237 u16 sz = vring->size;
238 u16 last = vring->last_used_idx;
239 u16 n_buffers = 0, start;
242 if (vring->desc_in_use == 0)
245 d = &vring->packed_desc[last];
249 while ((flags & VRING_DESC_F_AVAIL) == (vring->used_wrap_counter << 7) &&
250 (flags & VRING_DESC_F_USED) == (vring->used_wrap_counter << 15))
258 vring->used_wrap_counter ^= 1;
260 d = &vring->packed_desc[last];
266 vlib_buffer_free_from_ring (vm, vring->buffers, start, sz, n_buffers);
267 virtio_memset_ring_u32 (vring->buffers, start, sz, n_buffers);
268 vring->desc_in_use -= n_buffers;
269 vring->last_used_idx = last;
274 virtio_free_used_device_desc (vlib_main_t *vm, virtio_vring_t *vring,
275 uword node_index, int packed)
278 virtio_free_used_device_desc_packed (vm, vring, node_index);
280 virtio_free_used_device_desc_split (vm, vring, node_index);
285 set_checksum_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr,
288 vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
290 if (b->flags & VNET_BUFFER_F_IS_IP4)
293 generic_header_offset_t gho = { 0 };
294 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
296 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
297 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
298 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
300 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
302 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
304 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
308 * virtio devices do not support IP4 checksum offload. So driver takes care
309 * of it while doing tx.
312 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
313 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
314 ip4->checksum = ip4_header_checksum (ip4);
316 else if (b->flags & VNET_BUFFER_F_IS_IP6)
318 generic_header_offset_t gho = { 0 };
319 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
321 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
322 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
323 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
325 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
327 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
329 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
335 set_gso_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr, const int is_l2)
337 vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
339 if (b->flags & VNET_BUFFER_F_IS_IP4)
342 generic_header_offset_t gho = { 0 };
343 vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
345 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
346 hdr->gso_size = vnet_buffer2 (b)->gso_size;
347 hdr->hdr_len = gho.hdr_sz;
348 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
349 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
350 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
352 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
354 * virtio devices do not support IP4 checksum offload. So driver takes care
355 * of it while doing tx.
357 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
358 ip4->checksum = ip4_header_checksum (ip4);
360 else if (b->flags & VNET_BUFFER_F_IS_IP6)
362 generic_header_offset_t gho = { 0 };
363 vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
365 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
366 hdr->gso_size = vnet_buffer2 (b)->gso_size;
367 hdr->hdr_len = gho.hdr_sz;
368 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
369 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
370 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
375 add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node,
376 virtio_if_t *vif, virtio_vring_t *vring, u32 bi,
377 u16 free_desc_count, u16 avail, u16 next, u16 mask,
378 int hdr_sz, int do_gso, int csum_offload, int is_pci,
379 int is_tun, int is_indirect, int is_any_layout)
384 d = &vring->desc[next];
385 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
386 virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
387 u32 drop_inline = ~0;
389 clib_memset_u8 (hdr, 0, hdr_sz);
391 if (b->flags & VNET_BUFFER_F_GSO)
394 set_gso_offsets (b, hdr, is_l2);
397 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
401 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
404 set_checksum_offsets (b, hdr, is_l2);
407 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
412 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
414 virtio_tx_trace (vm, node, b, bi, is_tun);
417 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
419 d->addr = ((is_pci) ? vlib_buffer_get_current_pa (vm, b) :
420 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
421 d->len = b->current_length + hdr_sz;
424 else if (is_indirect)
427 * We are using single vlib_buffer_t for indirect descriptor(s)
428 * chain. Single descriptor is 16 bytes and vlib_buffer_t
429 * has 2048 bytes space. So maximum long chain can have 128
430 * (=2048/16) indirect descriptors.
431 * It can easily support 65535 bytes of Jumbo frames with
432 * each data buffer size of 512 bytes minimum.
434 u32 indirect_buffer = 0;
435 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
437 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
441 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
442 indirect_desc->current_data = 0;
443 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
444 indirect_desc->next_buffer = bi;
445 bi = indirect_buffer;
448 (vring_desc_t *) vlib_buffer_get_current (indirect_desc);
452 d->addr = vlib_physmem_get_pa (vm, id);
453 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
456 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
457 * should be presented in separate descriptor and data will start
458 * from next descriptor.
461 id->len = b->current_length + hdr_sz;
465 id->flags = VRING_DESC_F_NEXT;
469 id->addr = vlib_buffer_get_current_pa (vm, b);
470 id->len = b->current_length;
472 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
474 id->flags = VRING_DESC_F_NEXT;
478 b = vlib_get_buffer (vm, b->next_buffer);
479 id->addr = vlib_buffer_get_current_pa (vm, b);
480 id->len = b->current_length;
483 else /* VIRTIO_IF_TYPE_[TAP | TUN] */
485 d->addr = pointer_to_uword (id);
486 /* first buffer in chain */
487 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
488 id->len = b->current_length + hdr_sz;
490 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
492 id->flags = VRING_DESC_F_NEXT;
496 b = vlib_get_buffer (vm, b->next_buffer);
497 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
498 id->len = b->current_length;
503 d->len = count * sizeof (vring_desc_t);
504 d->flags = VRING_DESC_F_INDIRECT;
509 vlib_buffer_t *b_temp = b;
510 u16 n_buffers_in_chain = 1;
513 * Check the length of the chain for the required number of
514 * descriptors. Return from here, retry to get more descriptors,
515 * if chain length is greater than available descriptors.
517 while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
519 n_buffers_in_chain++;
520 b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
523 if (n_buffers_in_chain > free_desc_count)
524 return n_buffers_in_chain;
526 d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
527 d->len = b->current_length + hdr_sz;
529 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
531 d->flags = VRING_DESC_F_NEXT;
532 vring->buffers[count] = bi;
534 ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
538 count = (count + 1) & mask;
540 d = &vring->desc[count];
541 b = vlib_get_buffer (vm, bi);
542 d->addr = vlib_buffer_get_current_pa (vm, b);
543 d->len = b->current_length;
546 vring->buffers[count] = bi;
547 vring->avail->ring[avail & mask] = next;
555 vring->buffers[next] = bi;
556 vring->avail->ring[avail & mask] = next;
560 if (drop_inline != ~0)
561 virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
568 add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node,
569 virtio_if_t *vif, virtio_vring_t *vring, u32 bi,
570 u16 next, int hdr_sz, int do_gso, int csum_offload,
571 int is_pci, int is_tun, int is_indirect,
574 u16 n_added = 0, flags = 0;
576 vring_packed_desc_t *d = &vring->packed_desc[next];
577 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
578 virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
579 u32 drop_inline = ~0;
581 clib_memset (hdr, 0, hdr_sz);
583 if (b->flags & VNET_BUFFER_F_GSO)
586 set_gso_offsets (b, hdr, is_l2);
589 drop_inline = VIRTIO_TX_ERROR_GSO_PACKET_DROP;
593 else if (b->flags & VNET_BUFFER_F_OFFLOAD)
596 set_checksum_offsets (b, hdr, is_l2);
599 drop_inline = VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP;
603 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
605 virtio_tx_trace (vm, node, b, bi, is_tun);
608 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
611 ((is_pci) ? vlib_buffer_get_current_pa (vm,
613 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
614 d->len = b->current_length + hdr_sz;
616 else if (is_indirect)
619 * We are using single vlib_buffer_t for indirect descriptor(s)
620 * chain. Single descriptor is 16 bytes and vlib_buffer_t
621 * has 2048 bytes space. So maximum long chain can have 128
622 * (=2048/16) indirect descriptors.
623 * It can easily support 65535 bytes of Jumbo frames with
624 * each data buffer size of 512 bytes minimum.
626 u32 indirect_buffer = 0;
627 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
629 drop_inline = VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED;
633 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
634 indirect_desc->current_data = 0;
635 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
636 indirect_desc->next_buffer = bi;
637 bi = indirect_buffer;
639 vring_packed_desc_t *id =
640 (vring_packed_desc_t *) vlib_buffer_get_current (indirect_desc);
644 d->addr = vlib_physmem_get_pa (vm, id);
645 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
648 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
649 * should be presented in separate descriptor and data will start
650 * from next descriptor.
653 id->len = b->current_length + hdr_sz;
661 id->addr = vlib_buffer_get_current_pa (vm, b);
662 id->len = b->current_length;
664 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
670 b = vlib_get_buffer (vm, b->next_buffer);
671 id->addr = vlib_buffer_get_current_pa (vm, b);
672 id->len = b->current_length;
677 d->len = count * sizeof (vring_packed_desc_t);
678 flags = VRING_DESC_F_INDIRECT;
684 if (vring->avail_wrap_counter)
686 flags |= VRING_DESC_F_AVAIL;
687 flags &= ~VRING_DESC_F_USED;
691 flags &= ~VRING_DESC_F_AVAIL;
692 flags |= VRING_DESC_F_USED;
697 vring->buffers[next] = bi;
701 if (drop_inline != ~0)
702 virtio_interface_drop_inline (vm, vif, node->node_index, &bi, 1,
709 virtio_interface_tx_packed_gso_inline (vlib_main_t *vm,
710 vlib_node_runtime_t *node,
711 virtio_if_t *vif, virtio_if_type_t type,
712 virtio_vring_t *vring, u32 *buffers,
713 u16 n_left, const int do_gso,
714 const int csum_offload)
716 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
717 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
719 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
721 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
722 const int hdr_sz = vif->virtio_net_hdr_sz;
723 u16 sz = vring->size;
724 u16 used, next, n_buffers = 0, n_buffers_left = 0;
725 u16 n_vectors = n_left;
728 used = vring->desc_in_use;
729 next = vring->desc_next;
731 if (vif->packet_buffering)
733 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
735 while (n_buffers_left && used < sz)
739 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
742 n_added = add_buffer_to_slot_packed (
743 vm, node, vif, vring, bi, next, hdr_sz, do_gso, csum_offload,
744 is_pci, is_tun, is_indirect, is_any_layout);
746 if (PREDICT_FALSE (n_added == 0))
754 vring->avail_wrap_counter ^= 1;
759 while (n_left && used < sz)
763 n_added = add_buffer_to_slot_packed (
764 vm, node, vif, vring, buffers[0], next, hdr_sz, do_gso, csum_offload,
765 is_pci, is_tun, is_indirect, is_any_layout);
768 if (PREDICT_FALSE (n_added == 0))
776 vring->avail_wrap_counter ^= 1;
780 if (n_left != n_vectors || n_buffers != n_buffers_left)
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);
794 virtio_find_free_desc (virtio_vring_t *vring, u16 size, u16 mask, u16 req,
795 u16 next, u32 *first_free_desc_index,
796 u16 *free_desc_count)
799 /* next is used as hint: from where to start looking */
800 for (u16 i = 0; i < size; i++, next++)
802 if (vring->buffers[next & mask] == ~0)
804 if (*first_free_desc_index == ~0)
806 *first_free_desc_index = (next & mask);
808 (*free_desc_count)++;
815 if (start + *free_desc_count == i)
817 (*free_desc_count)++;
830 virtio_interface_tx_split_gso_inline (vlib_main_t *vm,
831 vlib_node_runtime_t *node,
832 virtio_if_t *vif, virtio_if_type_t type,
833 virtio_vring_t *vring, u32 *buffers,
834 u16 n_left, int do_gso, int csum_offload)
836 u16 used, next, avail, n_buffers = 0, n_buffers_left = 0;
837 int is_pci = (type == VIRTIO_IF_TYPE_PCI);
838 int is_tun = (type == VIRTIO_IF_TYPE_TUN);
840 ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0);
842 ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0);
843 u16 sz = vring->size;
844 int hdr_sz = vif->virtio_net_hdr_sz;
846 u16 n_vectors = n_left;
848 used = vring->desc_in_use;
849 next = vring->desc_next;
850 avail = vring->avail->idx;
852 u16 free_desc_count = 0;
854 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
856 u32 first_free_desc_index = ~0;
858 virtio_find_free_desc (vring, sz, mask, n_left, next,
859 &first_free_desc_index, &free_desc_count);
862 next = first_free_desc_index;
865 free_desc_count = sz - used;
867 if (vif->packet_buffering)
869 n_buffers = n_buffers_left = virtio_vring_n_buffers (vring->buffering);
871 while (n_buffers_left && free_desc_count)
875 u32 bi = virtio_vring_buffering_read_from_front (vring->buffering);
879 n_added = add_buffer_to_slot (vm, node, vif, vring, bi,
880 free_desc_count, avail, next, mask,
881 hdr_sz, do_gso, csum_offload, is_pci,
882 is_tun, is_indirect, is_any_layout);
883 if (PREDICT_FALSE (n_added == 0))
888 else if (PREDICT_FALSE (n_added > free_desc_count))
892 next = (next + n_added) & mask;
895 free_desc_count -= n_added;
899 while (n_left && free_desc_count)
904 add_buffer_to_slot (vm, node, vif, vring, buffers[0], free_desc_count,
905 avail, next, mask, hdr_sz, do_gso, csum_offload,
906 is_pci, is_tun, is_indirect, is_any_layout);
908 if (PREDICT_FALSE (n_added == 0))
914 else if (PREDICT_FALSE (n_added > free_desc_count))
918 next = (next + n_added) & mask;
922 free_desc_count -= n_added;
925 if (n_left != n_vectors || n_buffers != n_buffers_left)
927 clib_atomic_store_seq_cst (&vring->avail->idx, avail);
928 vring->desc_next = next;
929 vring->desc_in_use = used;
930 if ((clib_atomic_load_seq_cst (&vring->used->flags) &
931 VRING_USED_F_NO_NOTIFY) == 0)
932 virtio_kick (vm, vring, vif);
939 virtio_interface_tx_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
940 virtio_if_t *vif, virtio_if_type_t type,
941 virtio_vring_t *vring, u32 *buffers,
942 u16 n_left, int packed, int do_gso,
946 return virtio_interface_tx_packed_gso_inline (vm, node, vif, type, vring,
948 do_gso, csum_offload);
950 return virtio_interface_tx_split_gso_inline (vm, node, vif, type, vring,
952 do_gso, csum_offload);
956 virtio_interface_tx_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
957 virtio_if_t *vif, virtio_vring_t *vring,
958 virtio_if_type_t type, u32 *buffers, u16 n_left,
961 vnet_main_t *vnm = vnet_get_main ();
962 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
964 if (hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO)
965 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
966 buffers, n_left, packed,
968 1 /* checksum offload */ );
969 else if (hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM)
970 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
971 buffers, n_left, packed,
973 1 /* checksum offload */ );
975 return virtio_interface_tx_gso_inline (vm, node, vif, type, vring,
976 buffers, n_left, packed,
978 0 /* no checksum offload */ );
981 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
982 vlib_node_runtime_t * node,
983 vlib_frame_t * frame)
985 virtio_main_t *nm = &virtio_main;
986 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
987 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
988 u16 qid = vm->thread_index % vif->num_txqs;
989 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid);
990 u16 n_left = frame->n_vectors;
991 u32 *buffers = vlib_frame_vector_args (frame);
992 u32 to[GRO_TO_VECTOR_SIZE (n_left)];
993 int packed = vif->is_packed;
994 u16 n_vectors = frame->n_vectors;
996 clib_spinlock_lock_if_init (&vring->lockp);
998 if (vif->packet_coalesce)
1000 n_vectors = n_left =
1001 vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
1005 u16 retry_count = 2;
1008 /* free consumed buffers */
1009 virtio_free_used_device_desc (vm, vring, node->node_index, packed);
1011 if (vif->type == VIRTIO_IF_TYPE_TAP)
1012 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
1014 &buffers[n_vectors - n_left],
1016 else if (vif->type == VIRTIO_IF_TYPE_PCI)
1017 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
1019 &buffers[n_vectors - n_left],
1021 else if (vif->type == VIRTIO_IF_TYPE_TUN)
1022 n_left = virtio_interface_tx_inline (vm, node, vif, vring,
1024 &buffers[n_vectors - n_left],
1029 if (n_left && retry_count--)
1032 if (vif->packet_buffering && n_left)
1034 u16 n_buffered = virtio_vring_buffering_store_packets (vring->buffering,
1039 n_left -= n_buffered;
1042 virtio_interface_drop_inline (vm, vif, node->node_index,
1043 &buffers[n_vectors - n_left], n_left,
1044 VIRTIO_TX_ERROR_NO_FREE_SLOTS);
1046 clib_spinlock_unlock_if_init (&vring->lockp);
1048 return frame->n_vectors - n_left;
1052 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1055 virtio_main_t *apm = &virtio_main;
1056 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1057 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
1059 /* Shut off redirection */
1060 if (node_index == ~0)
1062 vif->per_interface_next_index = node_index;
1066 vif->per_interface_next_index =
1067 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
1072 virtio_clear_hw_interface_counters (u32 instance)
1074 /* Nothing for now */
1078 virtio_set_rx_interrupt (virtio_if_t *vif, virtio_vring_t *vring)
1081 vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE;
1083 vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
1087 virtio_set_rx_polling (virtio_if_t *vif, virtio_vring_t *vring)
1090 vring->driver_event->flags |= VRING_EVENT_F_DISABLE;
1092 vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
1095 static clib_error_t *
1096 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
1097 vnet_hw_if_rx_mode mode)
1099 vlib_main_t *vm = vnm->vlib_main;
1100 virtio_main_t *mm = &virtio_main;
1101 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1102 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
1103 virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
1105 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
1107 virtio_set_rx_polling (vif, rx_vring);
1108 return clib_error_return (0, "interrupt mode is not supported");
1111 if (mode == VNET_HW_IF_RX_MODE_POLLING)
1113 if (vif->packet_coalesce || vif->packet_buffering)
1115 if (mm->interrupt_queues_count > 0)
1116 mm->interrupt_queues_count--;
1117 if (mm->interrupt_queues_count == 0)
1118 vlib_process_signal_event (vm,
1119 virtio_send_interrupt_node.index,
1120 VIRTIO_EVENT_STOP_TIMER, 0);
1122 virtio_set_rx_polling (vif, rx_vring);
1126 if (vif->packet_coalesce || vif->packet_buffering)
1128 mm->interrupt_queues_count++;
1129 if (mm->interrupt_queues_count == 1)
1130 vlib_process_signal_event (vm,
1131 virtio_send_interrupt_node.index,
1132 VIRTIO_EVENT_START_TIMER, 0);
1134 virtio_set_rx_interrupt (vif, rx_vring);
1137 rx_vring->mode = mode;
1142 static clib_error_t *
1143 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1145 virtio_main_t *mm = &virtio_main;
1146 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1147 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
1149 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
1151 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1152 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1153 VNET_HW_INTERFACE_FLAG_LINK_UP);
1157 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
1158 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1163 static clib_error_t *
1164 virtio_subif_add_del_function (vnet_main_t * vnm,
1166 struct vnet_sw_interface_t *st, int is_add)
1168 /* Nothing for now */
1173 VNET_DEVICE_CLASS (virtio_device_class) = {
1175 .format_device_name = format_virtio_device_name,
1176 .format_device = format_virtio_device,
1177 .format_tx_trace = format_virtio_tx_trace,
1178 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
1179 .tx_function_error_strings = virtio_tx_func_error_strings,
1180 .rx_redirect_to_node = virtio_set_interface_next_node,
1181 .clear_counters = virtio_clear_hw_interface_counters,
1182 .admin_up_down_function = virtio_interface_admin_up_down,
1183 .subif_add_del_function = virtio_subif_add_del_function,
1184 .rx_mode_change_function = virtio_interface_rx_mode_change,
1190 * fd.io coding-style-patch-verification: ON
1193 * eval: (c-set-style "gnu")