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/hdr_offset_parser.h>
27 #include <vnet/ip/ip4_packet.h>
28 #include <vnet/ip/ip6_packet.h>
29 #include <vnet/tcp/tcp_packet.h>
30 #include <vnet/udp/udp_packet.h>
31 #include <vnet/devices/virtio/virtio.h>
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") \
37 _(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")
44 #define _(f,s) VIRTIO_TX_ERROR_##f,
45 foreach_virtio_tx_func_error
48 } virtio_tx_func_error_t;
50 static char *virtio_tx_func_error_strings[] = {
52 foreach_virtio_tx_func_error
57 format_virtio_device (u8 * s, va_list * args)
59 u32 dev_instance = va_arg (*args, u32);
60 int verbose = va_arg (*args, int);
61 u32 indent = format_get_indent (s);
63 s = format (s, "VIRTIO interface");
66 s = format (s, "\n%U instance %u", format_white_space, indent + 2,
73 format_virtio_tx_trace (u8 * s, va_list * args)
75 s = format (s, "Unimplemented...");
79 static_always_inline void
80 virtio_interface_drop_inline (vlib_main_t * vm, uword node_index,
82 virtio_tx_func_error_t error)
84 vlib_error_count (vm, node_index, error, n);
85 vlib_buffer_free (vm, buffers, n);
88 static_always_inline void
89 virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers)
91 ASSERT (n_buffers <= ring_size);
93 if (PREDICT_TRUE (start + n_buffers <= ring_size))
95 clib_memset_u32 (ring + start, ~0, n_buffers);
99 clib_memset_u32 (ring + start, ~0, ring_size - start);
100 clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
104 static_always_inline void
105 virtio_free_used_device_desc (vlib_main_t * vm, virtio_vring_t * vring,
108 u16 used = vring->desc_in_use;
109 u16 sz = vring->size;
111 u16 last = vring->last_used_idx;
112 u16 n_left = vring->used->idx - last;
113 u16 out_of_order_count = 0;
120 struct vring_used_elem *e = &vring->used->ring[last & mask];
122 slot = n_buffers = e->id;
124 while (e->id == (n_buffers & mask))
129 struct vring_desc *d = &vring->desc[e->id];
131 while (d->flags & VRING_DESC_F_NEXT)
135 d = &vring->desc[next];
139 e = &vring->used->ring[last & mask];
141 vlib_buffer_free_from_ring (vm, vring->buffers, slot,
142 sz, (n_buffers - slot));
143 virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
144 used -= (n_buffers - slot);
148 vlib_buffer_free (vm, &vring->buffers[e->id], 1);
149 vring->buffers[e->id] = ~0;
153 out_of_order_count++;
154 vring->flags |= VRING_TX_OUT_OF_ORDER;
159 * Some vhost-backends give buffers back in out-of-order fashion in used ring.
160 * It impacts the overall virtio-performance.
162 if (out_of_order_count)
163 vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
166 vring->desc_in_use = used;
167 vring->last_used_idx = last;
170 static_always_inline void
171 set_checksum_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr)
173 if (b->flags & VNET_BUFFER_F_IS_IP4)
176 generic_header_offset_t gho = { 0 };
177 vnet_generic_header_offset_parser (b, &gho);
178 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
179 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
180 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
182 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
184 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
186 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
190 * virtio devices do not support IP4 checksum offload. So driver takes care
191 * of it while doing tx.
194 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
195 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
196 ip4->checksum = ip4_header_checksum (ip4);
198 else if (b->flags & VNET_BUFFER_F_IS_IP6)
200 generic_header_offset_t gho = { 0 };
201 vnet_generic_header_offset_parser (b, &gho);
202 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
203 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
204 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
206 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
208 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
210 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
215 static_always_inline void
216 set_gso_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr)
218 if (b->flags & VNET_BUFFER_F_IS_IP4)
221 generic_header_offset_t gho = { 0 };
222 vnet_generic_header_offset_parser (b, &gho);
223 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
224 hdr->gso_size = vnet_buffer2 (b)->gso_size;
225 hdr->hdr_len = gho.hdr_sz;
226 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
227 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
228 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
230 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
232 * virtio devices do not support IP4 checksum offload. So driver takes care
233 * of it while doing tx.
235 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
236 ip4->checksum = ip4_header_checksum (ip4);
238 else if (b->flags & VNET_BUFFER_F_IS_IP6)
240 generic_header_offset_t gho = { 0 };
241 vnet_generic_header_offset_parser (b, &gho);
242 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
243 hdr->gso_size = vnet_buffer2 (b)->gso_size;
244 hdr->hdr_len = gho.hdr_sz;
245 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
246 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
247 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
251 static_always_inline u16
252 add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
253 virtio_vring_t * vring, u32 bi, u16 free_desc_count,
254 u16 avail, u16 next, u16 mask, int do_gso,
255 int csum_offload, uword node_index)
258 int hdr_sz = vif->virtio_net_hdr_sz;
259 struct vring_desc *d;
260 d = &vring->desc[next];
261 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
262 struct virtio_net_hdr_v1 *hdr = vlib_buffer_get_current (b) - hdr_sz;
264 clib_memset (hdr, 0, hdr_sz);
266 if (b->flags & VNET_BUFFER_F_GSO)
269 set_gso_offsets (b, hdr);
272 virtio_interface_drop_inline (vm, node_index, &bi, 1,
273 VIRTIO_TX_ERROR_GSO_PACKET_DROP);
277 else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
278 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
281 set_checksum_offsets (b, hdr);
284 virtio_interface_drop_inline (vm, node_index, &bi, 1,
285 VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP);
290 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
293 ((vif->type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
295 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
296 d->len = b->current_length + hdr_sz;
299 else if (vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC))
302 * We are using single vlib_buffer_t for indirect descriptor(s)
303 * chain. Single descriptor is 16 bytes and vlib_buffer_t
304 * has 2048 bytes space. So maximum long chain can have 128
305 * (=2048/16) indirect descriptors.
306 * It can easily support 65535 bytes of Jumbo frames with
307 * each data buffer size of 512 bytes minimum.
309 u32 indirect_buffer = 0;
310 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
312 virtio_interface_drop_inline (vm, node_index, &bi, 1,
313 VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED);
317 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
318 indirect_desc->current_data = 0;
319 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
320 indirect_desc->next_buffer = bi;
321 bi = indirect_buffer;
323 struct vring_desc *id =
324 (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
326 if (vif->type == VIRTIO_IF_TYPE_PCI)
328 d->addr = vlib_physmem_get_pa (vm, id);
329 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
332 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
333 * should be presented in separate descriptor and data will start
334 * from next descriptor.
337 (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
338 id->len = b->current_length + hdr_sz;
342 id->flags = VRING_DESC_F_NEXT;
346 id->addr = vlib_buffer_get_current_pa (vm, b);
347 id->len = b->current_length;
349 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
351 id->flags = VRING_DESC_F_NEXT;
355 b = vlib_get_buffer (vm, b->next_buffer);
356 id->addr = vlib_buffer_get_current_pa (vm, b);
357 id->len = b->current_length;
360 else /* VIRTIO_IF_TYPE_[TAP | TUN] */
362 d->addr = pointer_to_uword (id);
363 /* first buffer in chain */
364 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
365 id->len = b->current_length + hdr_sz;
367 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
369 id->flags = VRING_DESC_F_NEXT;
373 b = vlib_get_buffer (vm, b->next_buffer);
374 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
375 id->len = b->current_length;
380 d->len = count * sizeof (struct vring_desc);
381 d->flags = VRING_DESC_F_INDIRECT;
383 else if (vif->type == VIRTIO_IF_TYPE_PCI)
386 vlib_buffer_t *b_temp = b;
387 u16 n_buffers_in_chain = 1;
390 * Check the length of the chain for the required number of
391 * descriptors. Return from here, retry to get more descriptors,
392 * if chain length is greater than available descriptors.
394 while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
396 n_buffers_in_chain++;
397 b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
400 if (n_buffers_in_chain > free_desc_count)
401 return n_buffers_in_chain;
403 d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
404 d->len = b->current_length + hdr_sz;
406 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
408 d->flags = VRING_DESC_F_NEXT;
409 vring->buffers[count] = bi;
411 ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
415 count = (count + 1) & mask;
417 d = &vring->desc[count];
418 b = vlib_get_buffer (vm, bi);
419 d->addr = vlib_buffer_get_current_pa (vm, b);
420 d->len = b->current_length;
423 vring->buffers[count] = bi;
424 vring->avail->ring[avail & mask] = next;
432 vring->buffers[next] = bi;
433 vring->avail->ring[avail & mask] = next;
438 static_always_inline void
439 virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
440 u16 req, u16 next, u32 * first_free_desc_index,
441 u16 * free_desc_count)
444 /* next is used as hint: from where to start looking */
445 for (u16 i = 0; i < size; i++, next++)
447 if (vring->buffers[next & mask] == ~0)
449 if (*first_free_desc_index == ~0)
451 *first_free_desc_index = (next & mask);
453 (*free_desc_count)++;
460 if (start + *free_desc_count == i)
462 (*free_desc_count)++;
474 static_always_inline uword
475 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
476 vlib_frame_t * frame, virtio_if_t * vif,
477 int do_gso, int csum_offload)
479 u16 n_left = frame->n_vectors;
480 virtio_vring_t *vring;
481 u16 qid = vm->thread_index % vif->num_txqs;
482 vring = vec_elt_at_index (vif->txq_vrings, qid);
483 u16 used, next, avail;
484 u16 sz = vring->size;
487 u32 *buffers = vlib_frame_vector_args (frame);
489 clib_spinlock_lock_if_init (&vring->lockp);
491 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
492 (vring->last_kick_avail_idx != vring->avail->idx))
493 virtio_kick (vm, vring, vif);
496 /* free consumed buffers */
497 virtio_free_used_device_desc (vm, vring, node->node_index);
499 used = vring->desc_in_use;
500 next = vring->desc_next;
501 avail = vring->avail->idx;
503 u16 free_desc_count = 0;
505 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
507 u32 first_free_desc_index = ~0;
509 virtio_find_free_desc (vring, sz, mask, n_left, next,
510 &first_free_desc_index, &free_desc_count);
513 next = first_free_desc_index;
516 free_desc_count = sz - used;
518 while (n_left && free_desc_count)
522 add_buffer_to_slot (vm, vif, vring, buffers[0], free_desc_count,
523 avail, next, mask, do_gso, csum_offload,
526 if (PREDICT_FALSE (n_added == 0))
532 else if (PREDICT_FALSE (n_added > free_desc_count))
536 next = (next + n_added) & mask;
540 free_desc_count -= n_added;
543 if (n_left != frame->n_vectors)
545 CLIB_MEMORY_STORE_BARRIER ();
546 vring->avail->idx = avail;
547 vring->desc_next = next;
548 vring->desc_in_use = used;
549 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
550 virtio_kick (vm, vring, vif);
558 virtio_interface_drop_inline (vm, node->node_index, buffers, n_left,
559 VIRTIO_TX_ERROR_NO_FREE_SLOTS);
562 clib_spinlock_unlock_if_init (&vring->lockp);
564 return frame->n_vectors - n_left;
567 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
568 vlib_node_runtime_t * node,
569 vlib_frame_t * frame)
571 vnet_main_t *vnm = vnet_get_main ();
572 virtio_main_t *nm = &virtio_main;
573 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
574 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
575 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
577 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
578 return virtio_interface_tx_inline (vm, node, frame, vif, 1 /* do_gso */ ,
580 else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
581 return virtio_interface_tx_inline (vm, node, frame, vif,
582 0 /* no do_gso */ , 1);
584 return virtio_interface_tx_inline (vm, node, frame, vif,
585 0 /* no do_gso */ , 0);
589 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
592 virtio_main_t *apm = &virtio_main;
593 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
594 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
596 /* Shut off redirection */
597 if (node_index == ~0)
599 vif->per_interface_next_index = node_index;
603 vif->per_interface_next_index =
604 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
609 virtio_clear_hw_interface_counters (u32 instance)
611 /* Nothing for now */
614 static clib_error_t *
615 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
616 vnet_hw_interface_rx_mode mode)
618 virtio_main_t *mm = &virtio_main;
619 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
620 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
621 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
623 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
625 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
626 return clib_error_return (0, "interrupt mode is not supported");
629 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
630 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
632 vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
637 static clib_error_t *
638 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
640 virtio_main_t *mm = &virtio_main;
641 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
642 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
644 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
646 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
647 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
648 VNET_HW_INTERFACE_FLAG_LINK_UP);
652 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
653 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
658 static clib_error_t *
659 virtio_subif_add_del_function (vnet_main_t * vnm,
661 struct vnet_sw_interface_t *st, int is_add)
663 /* Nothing for now */
668 VNET_DEVICE_CLASS (virtio_device_class) = {
670 .format_device_name = format_virtio_device_name,
671 .format_device = format_virtio_device,
672 .format_tx_trace = format_virtio_tx_trace,
673 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
674 .tx_function_error_strings = virtio_tx_func_error_strings,
675 .rx_redirect_to_node = virtio_set_interface_next_node,
676 .clear_counters = virtio_clear_hw_interface_counters,
677 .admin_up_down_function = virtio_interface_admin_up_down,
678 .subif_add_del_function = virtio_subif_add_del_function,
679 .rx_mode_change_function = virtio_interface_rx_mode_change,
685 * fd.io coding-style-patch-verification: ON
688 * eval: (c-set-style "gnu")