2 *------------------------------------------------------------------
3 * Copyright (c) 2017 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 <linux/if_tun.h>
23 #include <sys/ioctl.h>
24 #include <sys/eventfd.h>
26 #include <vlib/vlib.h>
27 #include <vlib/pci/pci.h>
28 #include <vlib/unix/unix.h>
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/ip/ip4_packet.h>
31 #include <vnet/ip/ip6_packet.h>
32 #include <vnet/devices/virtio/virtio.h>
33 #include <vnet/devices/virtio/pci.h>
34 #include <vnet/interface/rx_queue_funcs.h>
36 virtio_main_t virtio_main;
38 #define _IOCTL(fd,a,...) \
39 if (ioctl (fd, a, __VA_ARGS__) < 0) \
41 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
46 call_read_ready (clib_file_t * uf)
48 vnet_main_t *vnm = vnet_get_main ();
51 CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
52 vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
59 virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz)
61 virtio_vring_t *vring;
65 return clib_error_return (0, "ring size must be power of 2");
68 return clib_error_return (0, "ring size must be 32768 or lower");
75 vlib_thread_main_t *thm = vlib_get_thread_main ();
76 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx),
77 CLIB_CACHE_LINE_BYTES);
78 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
79 if (thm->n_vlib_mains > vif->num_txqs)
80 clib_spinlock_init (&vring->lockp);
84 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx),
85 CLIB_CACHE_LINE_BYTES);
86 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
88 i = sizeof (vring_desc_t) * sz;
89 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
90 vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
91 clib_memset (vring->desc, 0, i);
93 i = sizeof (vring_avail_t) + sz * sizeof (vring->avail->ring[0]);
94 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
95 vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
96 clib_memset (vring->avail, 0, i);
97 // tell kernel that we don't need interrupt
98 vring->avail->flags = VRING_AVAIL_F_NO_INTERRUPT;
100 i = sizeof (vring_used_t) + sz * sizeof (vring_used_elem_t);
101 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
102 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
103 clib_memset (vring->used, 0, i);
105 vring->queue_id = idx;
106 ASSERT (vring->buffers == 0);
107 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
111 clib_memset_u32 (vring->buffers, ~0, sz);
115 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
116 vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
117 virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx,
118 vring->size, vring->call_fd, vring->kick_fd);
124 virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring)
126 u16 used = vring->desc_in_use;
127 u16 last = vring->last_used_idx;
128 u16 mask = vring->size - 1;
132 vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
139 virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
141 virtio_vring_t *vring =
142 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
144 clib_file_del_by_index (&file_main, vring->call_file_index);
145 close (vring->kick_fd);
146 close (vring->call_fd);
149 virtio_free_buffers (vm, vring);
150 clib_mem_free (vring->used);
153 clib_mem_free (vring->desc);
155 clib_mem_free (vring->avail);
156 vec_free (vring->buffers);
161 virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
163 virtio_vring_t *vring =
164 vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
166 clib_file_del_by_index (&file_main, vring->call_file_index);
167 close (vring->kick_fd);
168 close (vring->call_fd);
171 virtio_free_buffers (vm, vring);
172 clib_mem_free (vring->used);
175 clib_mem_free (vring->desc);
177 clib_mem_free (vring->avail);
178 vec_free (vring->buffers);
179 gro_flow_table_free (vring->flow_table);
180 virtio_vring_buffering_free (vm, vring->buffering);
181 clib_spinlock_free (&vring->lockp);
186 virtio_set_packet_coalesce (virtio_if_t * vif)
188 vnet_main_t *vnm = vnet_get_main ();
189 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
190 virtio_vring_t *vring;
191 vif->packet_coalesce = 1;
192 vec_foreach (vring, vif->txq_vrings)
194 gro_flow_table_init (&vring->flow_table,
195 vif->type & (VIRTIO_IF_TYPE_TAP |
196 VIRTIO_IF_TYPE_PCI), hw->tx_node_index);
201 virtio_set_packet_buffering (virtio_if_t * vif, u16 buffering_size)
203 vnet_main_t *vnm = vnet_get_main ();
204 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
205 virtio_vring_t *vring;
206 clib_error_t *error = 0;
207 vif->packet_buffering = 1;
209 vec_foreach (vring, vif->txq_vrings)
212 virtio_vring_buffering_init (&vring->buffering, hw->tx_node_index,
223 virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif)
225 vnet_main_t *vnm = vnet_get_main ();
226 virtio_vring_t *vring;
228 vnet_hw_if_set_input_node (vnm, vif->hw_if_index, virtio_input_node.index);
230 vec_foreach (vring, vif->rxq_vrings)
232 vring->queue_index = vnet_hw_if_register_rx_queue (
233 vnm, vif->hw_if_index, RX_QUEUE_ACCESS (vring->queue_id),
234 VNET_HW_IF_RXQ_THREAD_ANY);
235 vring->buffer_pool_index = vlib_buffer_pool_get_default_for_numa (
236 vm, vnet_hw_if_get_rx_queue_numa_node (vnm, vring->queue_index));
237 if (vif->type == VIRTIO_IF_TYPE_TAP || vif->type == VIRTIO_IF_TYPE_TUN)
241 .read_function = call_read_ready,
242 .flags = UNIX_FILE_EVENT_EDGE_TRIGGERED,
243 .file_descriptor = vring->call_fd,
244 .private_data = vring->queue_index,
245 .description = format (0, "%U vring %u", format_virtio_device_name,
246 vif->dev_instance, vring->queue_id),
249 vring->call_file_index = clib_file_add (&file_main, &f);
250 vnet_hw_if_set_rx_queue_file_index (vnm, vring->queue_index,
251 vring->call_file_index);
254 vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index);
258 virtio_set_net_hdr_size (virtio_if_t * vif)
260 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
261 vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
262 vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_v1_t);
264 vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_t);
268 virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
270 u32 i, j, hw_if_index;
272 vnet_main_t *vnm = &vnet_main;
273 virtio_main_t *mm = &virtio_main;
274 virtio_vring_t *vring;
280 struct feat_struct *feat_entry;
282 static struct feat_struct feat_array[] = {
283 #define _(s,b) { .str = #s, .bit = b, },
284 foreach_virtio_net_features
289 struct feat_struct *flag_entry;
290 static struct feat_struct flags_array[] = {
291 #define _(b,e,s) { .bit = b, .str = s, },
292 foreach_virtio_if_flag
300 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
302 vnet_hw_interface_t *hi =
303 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
304 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
305 if (vif->type != type)
307 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
308 format_vnet_hw_if_index_name, vnm,
309 hw_if_indices[hw_if_index], vif->hw_if_index);
310 if (type == VIRTIO_IF_TYPE_PCI)
312 vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
315 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
318 if (vif->host_if_name)
319 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
321 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
322 if (vif->host_mtu_size)
323 vlib_cli_output (vm, " host-mtu-size \"%d\"",
325 if (type == VIRTIO_IF_TYPE_TAP)
326 vlib_cli_output (vm, " host-mac-addr: %U",
327 format_ethernet_address, vif->host_mac_addr);
328 vlib_cli_output (vm, " host-carrier-up: %u", vif->host_carrier_up);
330 vec_foreach_index (i, vif->vhost_fds)
331 str = format (str, " %d", vif->vhost_fds[i]);
332 vlib_cli_output (vm, " vhost-fds%v", str);
334 vec_foreach_index (i, vif->tap_fds)
335 str = format (str, " %d", vif->tap_fds[i]);
336 vlib_cli_output (vm, " tap-fds%v", str);
339 vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
340 vlib_cli_output (vm, " csum-enabled %d", vif->csum_offload_enabled);
341 vlib_cli_output (vm, " packet-coalesce %d", vif->packet_coalesce);
342 vlib_cli_output (vm, " packet-buffering %d", vif->packet_buffering);
343 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI))
344 vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
346 vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
347 vlib_cli_output (vm, " flags 0x%x", vif->flags);
348 flag_entry = (struct feat_struct *) &flags_array;
349 while (flag_entry->str)
351 if (vif->flags & (1ULL << flag_entry->bit))
352 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
356 if (type == VIRTIO_IF_TYPE_PCI)
358 device_status (vm, vif);
360 vlib_cli_output (vm, " features 0x%lx", vif->features);
361 feat_entry = (struct feat_struct *) &feat_array;
362 while (feat_entry->str)
364 if (vif->features & (1ULL << feat_entry->bit))
365 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
369 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
370 feat_entry = (struct feat_struct *) &feat_array;
371 while (feat_entry->str)
373 if (vif->remote_features & (1ULL << feat_entry->bit))
374 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
378 vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs);
379 vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs);
380 if (vif->cxq_vring != NULL
381 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
382 vlib_cli_output (vm, " Number of CTRL Virtqueue 1");
383 vec_foreach_index (i, vif->rxq_vrings)
385 vring = vec_elt_at_index (vif->rxq_vrings, i);
386 vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id);
388 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
389 vring->size, vring->last_used_idx, vring->desc_next,
394 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
395 vring->driver_event->flags,
396 vring->driver_event->off_wrap,
397 vring->device_event->flags,
398 vring->device_event->off_wrap);
400 " avail wrap counter %d, used wrap counter %d",
401 vring->avail_wrap_counter,
402 vring->used_wrap_counter);
406 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
407 vring->avail->flags, vring->avail->idx,
408 vring->used->flags, vring->used->idx);
409 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
411 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
416 vlib_cli_output (vm, "\n descriptor table:\n");
418 " id addr len flags next/id user_addr\n");
420 " ===== ================== ===== ====== ======= ==================\n");
421 for (j = 0; j < vring->size; j++)
425 vring_packed_desc_t *desc = &vring->packed_desc[j];
427 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
430 desc->flags, desc->id, desc->addr);
434 vring_desc_t *desc = &vring->desc[j];
436 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
439 desc->flags, desc->next, desc->addr);
444 vec_foreach_index (i, vif->txq_vrings)
446 vring = vec_elt_at_index (vif->txq_vrings, i);
447 vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id);
449 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
450 vring->size, vring->last_used_idx, vring->desc_next,
455 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
456 vring->driver_event->flags,
457 vring->driver_event->off_wrap,
458 vring->device_event->flags,
459 vring->device_event->off_wrap);
461 " avail wrap counter %d, used wrap counter %d",
462 vring->avail_wrap_counter,
463 vring->used_wrap_counter);
467 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
468 vring->avail->flags, vring->avail->idx,
469 vring->used->flags, vring->used->idx);
470 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
472 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
475 if (vring->flow_table)
477 vlib_cli_output (vm, " %U", gro_flow_table_format,
480 if (vif->packet_buffering)
482 vlib_cli_output (vm, " %U", virtio_vring_buffering_format,
487 vlib_cli_output (vm, "\n descriptor table:\n");
489 " id addr len flags next/id user_addr\n");
491 " ===== ================== ===== ====== ======== ==================\n");
492 for (j = 0; j < vring->size; j++)
496 vring_packed_desc_t *desc = &vring->packed_desc[j];
498 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
501 desc->flags, desc->id, desc->addr);
505 vring_desc_t *desc = &vring->desc[j];
507 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
510 desc->flags, desc->next, desc->addr);
515 if (vif->cxq_vring != NULL
516 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
518 vring = vif->cxq_vring;
519 vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id);
521 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
522 vring->size, vring->last_used_idx,
523 vring->desc_next, vring->desc_in_use);
527 " driver_event.flags 0x%x driver_event.off_wrap %d device_event.flags 0x%x device_event.off_wrap %d",
528 vring->driver_event->flags,
529 vring->driver_event->off_wrap,
530 vring->device_event->flags,
531 vring->device_event->off_wrap);
533 " avail wrap counter %d, used wrap counter %d",
534 vring->avail_wrap_counter,
535 vring->used_wrap_counter);
540 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
541 vring->avail->flags, vring->avail->idx,
542 vring->used->flags, vring->used->idx);
544 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
546 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
551 vlib_cli_output (vm, "\n descriptor table:\n");
553 " id addr len flags next/id user_addr\n");
555 " ===== ================== ===== ====== ======== ==================\n");
556 for (j = 0; j < vring->size; j++)
560 vring_packed_desc_t *desc = &vring->packed_desc[j];
562 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
565 desc->flags, desc->id, desc->addr);
569 vring_desc_t *desc = &vring->desc[j];
571 " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n",
574 desc->flags, desc->next, desc->addr);
584 static clib_error_t *
585 virtio_init (vlib_main_t * vm)
587 virtio_main_t *vim = &virtio_main;
588 clib_error_t *error = 0;
590 vim->log_default = vlib_log_register_class ("virtio", 0);
591 vlib_log_debug (vim->log_default, "initialized");
596 VLIB_INIT_FUNCTION (virtio_init);
599 * fd.io coding-style-patch-verification: ON
602 * eval: (c-set-style "gnu")