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/ethernet/ethernet.h>
25 #include <vnet/gso/gso.h>
26 #include <vnet/ip/ip4_packet.h>
27 #include <vnet/ip/ip6_packet.h>
28 #include <vnet/tcp/tcp_packet.h>
29 #include <vnet/udp/udp_packet.h>
30 #include <vnet/devices/virtio/virtio.h>
32 #define foreach_virtio_tx_func_error \
33 _(NO_FREE_SLOTS, "no free tx slots") \
34 _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
35 _(PENDING_MSGS, "pending msgs in tx ring") \
36 _(NO_TX_QUEUES, "no tx queues") \
37 _(OUT_OF_ORDER, "out-of-order buffers in used ring")
41 #define _(f,s) VIRTIO_TX_ERROR_##f,
42 foreach_virtio_tx_func_error
45 } virtio_tx_func_error_t;
47 static char *virtio_tx_func_error_strings[] = {
49 foreach_virtio_tx_func_error
54 format_virtio_device (u8 * s, va_list * args)
56 u32 dev_instance = va_arg (*args, u32);
57 int verbose = va_arg (*args, int);
58 u32 indent = format_get_indent (s);
60 s = format (s, "VIRTIO interface");
63 s = format (s, "\n%U instance %u", format_white_space, indent + 2,
70 format_virtio_tx_trace (u8 * s, va_list * args)
72 s = format (s, "Unimplemented...");
76 static_always_inline void
77 virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers)
79 ASSERT (n_buffers <= ring_size);
81 if (PREDICT_TRUE (start + n_buffers <= ring_size))
83 clib_memset_u32 (ring + start, ~0, n_buffers);
87 clib_memset_u32 (ring + start, ~0, ring_size - start);
88 clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
92 static_always_inline void
93 virtio_free_used_device_desc (vlib_main_t * vm, virtio_vring_t * vring,
96 u16 used = vring->desc_in_use;
99 u16 last = vring->last_used_idx;
100 u16 n_left = vring->used->idx - last;
101 u16 out_of_order_count = 0;
108 struct vring_used_elem *e = &vring->used->ring[last & mask];
110 slot = n_buffers = e->id;
112 while (e->id == (n_buffers & mask))
119 e = &vring->used->ring[last & mask];
121 vlib_buffer_free_from_ring (vm, vring->buffers, slot,
122 sz, (n_buffers - slot));
123 virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
124 used -= (n_buffers - slot);
128 vlib_buffer_free (vm, &vring->buffers[e->id], 1);
129 vring->buffers[e->id] = ~0;
133 out_of_order_count++;
134 vring->flags |= VRING_TX_OUT_OF_ORDER;
139 * Some vhost-backends give buffers back in out-of-order fashion in used ring.
140 * It impacts the overall virtio-performance.
142 if (out_of_order_count)
143 vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
146 vring->desc_in_use = used;
147 vring->last_used_idx = last;
150 static_always_inline void
151 set_checksum_offsets (vlib_main_t * vm, virtio_if_t * vif, vlib_buffer_t * b,
152 struct virtio_net_hdr_v1 *hdr)
154 if (b->flags & VNET_BUFFER_F_IS_IP4)
157 gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 0);
158 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
159 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
160 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
162 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
164 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
166 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
170 * virtio devices do not support IP4 checksum offload. So driver takes care
171 * of it while doing tx.
174 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
175 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
176 ip4->checksum = ip4_header_checksum (ip4);
178 else if (b->flags & VNET_BUFFER_F_IS_IP6)
180 gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 1);
181 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
182 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
183 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
185 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
187 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
189 hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
194 static_always_inline u16
195 add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
196 virtio_vring_t * vring, u32 bi, u16 avail, u16 next,
197 u16 mask, int do_gso, int csum_offload)
200 int hdr_sz = vif->virtio_net_hdr_sz;
201 struct vring_desc *d;
202 d = &vring->desc[next];
203 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
204 struct virtio_net_hdr_v1 *hdr = vlib_buffer_get_current (b) - hdr_sz;
206 clib_memset (hdr, 0, hdr_sz);
208 if (do_gso && (b->flags & VNET_BUFFER_F_GSO))
210 if (b->flags & VNET_BUFFER_F_IS_IP4)
213 gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 0);
214 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
215 hdr->gso_size = vnet_buffer2 (b)->gso_size;
216 hdr->hdr_len = gho.l4_hdr_offset + gho.l4_hdr_sz;
217 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
218 hdr->csum_start = gho.l4_hdr_offset; // 0x22;
219 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
221 (ip4_header_t *) (vlib_buffer_get_current (b) +
224 * virtio devices do not support IP4 checksum offload. So driver takes care
225 * of it while doing tx.
227 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
228 ip4->checksum = ip4_header_checksum (ip4);
230 else if (b->flags & VNET_BUFFER_F_IS_IP6)
232 gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 1);
233 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
234 hdr->gso_size = vnet_buffer2 (b)->gso_size;
235 hdr->hdr_len = gho.l4_hdr_offset + gho.l4_hdr_sz;
236 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
237 hdr->csum_start = gho.l4_hdr_offset; // 0x36;
238 hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
241 else if (csum_offload
242 && (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
243 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)))
245 set_checksum_offsets (vm, vif, b, hdr);
248 if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
251 ((vif->type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
253 pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
254 d->len = b->current_length + hdr_sz;
260 * We are using single vlib_buffer_t for indirect descriptor(s)
261 * chain. Single descriptor is 16 bytes and vlib_buffer_t
262 * has 2048 bytes space. So maximum long chain can have 128
263 * (=2048/16) indirect descriptors.
264 * It can easily support 65535 bytes of Jumbo frames with
265 * each data buffer size of 512 bytes minimum.
267 u32 indirect_buffer = 0;
268 if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
271 vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
272 indirect_desc->current_data = 0;
273 indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
274 indirect_desc->next_buffer = bi;
275 bi = indirect_buffer;
277 struct vring_desc *id =
278 (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
280 if (vif->type == VIRTIO_IF_TYPE_PCI)
282 d->addr = vlib_physmem_get_pa (vm, id);
283 id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
286 * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
287 * should be presented in separate descriptor and data will start
288 * from next descriptor.
291 (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
292 id->len = b->current_length + hdr_sz;
296 id->flags = VRING_DESC_F_NEXT;
300 id->addr = vlib_buffer_get_current_pa (vm, b);
301 id->len = b->current_length;
303 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
305 id->flags = VRING_DESC_F_NEXT;
309 b = vlib_get_buffer (vm, b->next_buffer);
310 id->addr = vlib_buffer_get_current_pa (vm, b);
311 id->len = b->current_length;
314 else /* VIRTIO_IF_TYPE_TAP */
316 d->addr = pointer_to_uword (id);
317 /* first buffer in chain */
318 id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
319 id->len = b->current_length + hdr_sz;
321 while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
323 id->flags = VRING_DESC_F_NEXT;
327 b = vlib_get_buffer (vm, b->next_buffer);
328 id->addr = pointer_to_uword (vlib_buffer_get_current (b));
329 id->len = b->current_length;
334 d->len = count * sizeof (struct vring_desc);
335 d->flags = VRING_DESC_F_INDIRECT;
337 vring->buffers[next] = bi;
338 vring->avail->ring[avail & mask] = next;
343 static_always_inline void
344 virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
345 u16 req, u16 next, u32 * first_free_desc_index,
346 u16 * free_desc_count)
349 /* next is used as hint: from where to start looking */
350 for (u16 i = 0; i < size; i++, next++)
352 if (vring->buffers[next & mask] == ~0)
354 if (*first_free_desc_index == ~0)
356 *first_free_desc_index = (next & mask);
358 (*free_desc_count)++;
365 if (start + *free_desc_count == i)
367 (*free_desc_count)++;
379 static_always_inline uword
380 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
381 vlib_frame_t * frame, virtio_if_t * vif,
382 int do_gso, int csum_offload)
384 u16 n_left = frame->n_vectors;
385 virtio_vring_t *vring;
386 u16 qid = vm->thread_index % vif->num_txqs;
387 vring = vec_elt_at_index (vif->txq_vrings, qid);
388 u16 used, next, avail;
389 u16 sz = vring->size;
392 u32 *buffers = vlib_frame_vector_args (frame);
394 clib_spinlock_lock_if_init (&vring->lockp);
396 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
397 (vring->last_kick_avail_idx != vring->avail->idx))
398 virtio_kick (vm, vring, vif);
401 /* free consumed buffers */
402 virtio_free_used_device_desc (vm, vring, node->node_index);
404 used = vring->desc_in_use;
405 next = vring->desc_next;
406 avail = vring->avail->idx;
408 u16 free_desc_count = 0;
410 if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
412 u32 first_free_desc_index = ~0;
414 virtio_find_free_desc (vring, sz, mask, n_left, next,
415 &first_free_desc_index, &free_desc_count);
418 next = first_free_desc_index;
421 free_desc_count = sz - used;
423 while (n_left && free_desc_count)
427 add_buffer_to_slot (vm, vif, vring, buffers[0], avail, next, mask,
428 do_gso, csum_offload);
432 next = (next + n_added) & mask;
439 if (n_left != frame->n_vectors)
441 CLIB_MEMORY_STORE_BARRIER ();
442 vring->avail->idx = avail;
443 vring->desc_next = next;
444 vring->desc_in_use = used;
445 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
446 virtio_kick (vm, vring, vif);
454 vlib_error_count (vm, node->node_index, VIRTIO_TX_ERROR_NO_FREE_SLOTS,
456 vlib_buffer_free (vm, buffers, n_left);
459 clib_spinlock_unlock_if_init (&vring->lockp);
461 return frame->n_vectors - n_left;
464 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
465 vlib_node_runtime_t * node,
466 vlib_frame_t * frame)
468 vnet_main_t *vnm = vnet_get_main ();
469 virtio_main_t *nm = &virtio_main;
470 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
471 virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
472 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
474 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
475 return virtio_interface_tx_inline (vm, node, frame, vif, 1 /* do_gso */ ,
477 else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
478 return virtio_interface_tx_inline (vm, node, frame, vif,
479 0 /* no do_gso */ , 1);
481 return virtio_interface_tx_inline (vm, node, frame, vif,
482 0 /* no do_gso */ , 0);
486 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
489 virtio_main_t *apm = &virtio_main;
490 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
491 virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
493 /* Shut off redirection */
494 if (node_index == ~0)
496 vif->per_interface_next_index = node_index;
500 vif->per_interface_next_index =
501 vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
506 virtio_clear_hw_interface_counters (u32 instance)
508 /* Nothing for now */
511 static clib_error_t *
512 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
513 vnet_hw_interface_rx_mode mode)
515 virtio_main_t *mm = &virtio_main;
516 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
517 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
518 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
520 if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
522 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
523 return clib_error_return (0, "interrupt mode is not supported");
526 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
527 vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
529 vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
534 static clib_error_t *
535 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
537 virtio_main_t *mm = &virtio_main;
538 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
539 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
541 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
543 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
544 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
545 VNET_HW_INTERFACE_FLAG_LINK_UP);
549 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
550 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
555 static clib_error_t *
556 virtio_subif_add_del_function (vnet_main_t * vnm,
558 struct vnet_sw_interface_t *st, int is_add)
560 /* Nothing for now */
565 VNET_DEVICE_CLASS (virtio_device_class) = {
567 .format_device_name = format_virtio_device_name,
568 .format_device = format_virtio_device,
569 .format_tx_trace = format_virtio_tx_trace,
570 .tx_function_n_errors = VIRTIO_TX_N_ERROR,
571 .tx_function_error_strings = virtio_tx_func_error_strings,
572 .rx_redirect_to_node = virtio_set_interface_next_node,
573 .clear_counters = virtio_clear_hw_interface_counters,
574 .admin_up_down_function = virtio_interface_admin_up_down,
575 .subif_add_del_function = virtio_subif_add_del_function,
576 .rx_mode_change_function = virtio_interface_rx_mode_change,
581 * fd.io coding-style-patch-verification: ON
584 * eval: (c-set-style "gnu")