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>
35 virtio_main_t virtio_main;
37 #define _IOCTL(fd,a,...) \
38 if (ioctl (fd, a, __VA_ARGS__) < 0) \
40 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
45 call_read_ready (clib_file_t * uf)
47 virtio_main_t *nm = &virtio_main;
48 vnet_main_t *vnm = vnet_get_main ();
49 u16 qid = uf->private_data & 0xFFFF;
51 vec_elt_at_index (nm->interfaces, uf->private_data >> 16);
54 CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
56 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index,
57 RX_QUEUE_ACCESS (qid));
64 virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz)
66 virtio_vring_t *vring;
67 clib_file_t t = { 0 };
71 return clib_error_return (0, "ring size must be power of 2");
74 return clib_error_return (0, "ring size must be 32768 or lower");
81 vlib_thread_main_t *thm = vlib_get_thread_main ();
82 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx),
83 CLIB_CACHE_LINE_BYTES);
84 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
85 if (thm->n_vlib_mains > vif->num_txqs)
86 clib_spinlock_init (&vring->lockp);
90 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx),
91 CLIB_CACHE_LINE_BYTES);
92 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
94 i = sizeof (vring_desc_t) * sz;
95 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
96 vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
97 clib_memset (vring->desc, 0, i);
99 i = sizeof (vring_avail_t) + sz * sizeof (vring->avail->ring[0]);
100 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
101 vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
102 clib_memset (vring->avail, 0, i);
103 // tell kernel that we don't need interrupt
104 vring->avail->flags = VRING_AVAIL_F_NO_INTERRUPT;
106 i = sizeof (vring_used_t) + sz * sizeof (vring_used_elem_t);
107 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
108 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
109 clib_memset (vring->used, 0, i);
111 vring->queue_id = idx;
112 ASSERT (vring->buffers == 0);
113 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
117 clib_memset_u32 (vring->buffers, ~0, sz);
121 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
122 vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
123 virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx,
124 vring->size, vring->call_fd, vring->kick_fd);
126 t.read_function = call_read_ready;
127 t.file_descriptor = vring->call_fd;
128 t.private_data = vif->dev_instance << 16 | idx;
129 t.description = format (0, "%U vring %u", format_virtio_device_name,
130 vif->dev_instance, idx);
131 vring->call_file_index = clib_file_add (&file_main, &t);
137 virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring)
139 u16 used = vring->desc_in_use;
140 u16 last = vring->last_used_idx;
141 u16 mask = vring->size - 1;
145 vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
152 virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
154 virtio_vring_t *vring =
155 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
157 clib_file_del_by_index (&file_main, vring->call_file_index);
158 close (vring->kick_fd);
159 close (vring->call_fd);
162 virtio_free_rx_buffers (vm, vring);
163 clib_mem_free (vring->used);
166 clib_mem_free (vring->desc);
168 clib_mem_free (vring->avail);
169 vec_free (vring->buffers);
174 virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring)
176 u16 used = vring->desc_in_use;
177 u16 sz = vring->size;
179 u16 last = vring->last_used_idx;
180 u16 n_left = vring->used->idx - last;
187 vring_used_elem_t *e = &vring->used->ring[last & mask];
190 vlib_buffer_free (vm, &vring->buffers[slot], 1);
195 vring->desc_in_use = used;
196 vring->last_used_idx = last;
200 virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
202 virtio_vring_t *vring =
203 vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
205 clib_file_del_by_index (&file_main, vring->call_file_index);
206 close (vring->kick_fd);
207 close (vring->call_fd);
210 virtio_free_used_desc (vm, vring);
211 clib_mem_free (vring->used);
214 clib_mem_free (vring->desc);
216 clib_mem_free (vring->avail);
217 vec_free (vring->buffers);
218 gro_flow_table_free (vring->flow_table);
219 virtio_vring_buffering_free (vm, vring->buffering);
220 clib_spinlock_free (&vring->lockp);
225 virtio_set_packet_coalesce (virtio_if_t * vif)
227 vnet_main_t *vnm = vnet_get_main ();
228 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
229 virtio_vring_t *vring;
230 vif->packet_coalesce = 1;
231 vec_foreach (vring, vif->txq_vrings)
233 gro_flow_table_init (&vring->flow_table,
234 vif->type & (VIRTIO_IF_TYPE_TAP |
235 VIRTIO_IF_TYPE_PCI), hw->tx_node_index);
240 virtio_set_packet_buffering (virtio_if_t * vif, u16 buffering_size)
242 vnet_main_t *vnm = vnet_get_main ();
243 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
244 virtio_vring_t *vring;
245 clib_error_t *error = 0;
246 vif->packet_buffering = 1;
248 vec_foreach (vring, vif->txq_vrings)
251 virtio_vring_buffering_init (&vring->buffering, hw->tx_node_index,
262 virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
264 vnet_main_t *vnm = vnet_get_main ();
266 virtio_vring_t *vring =
267 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
269 vnet_get_device_input_thread_index (vnm, vif->hw_if_index,
270 RX_QUEUE_ACCESS (idx));
271 vring->buffer_pool_index =
272 vlib_buffer_pool_get_default_for_numa (vm,
274 [thread_index]->numa_node);
278 virtio_set_net_hdr_size (virtio_if_t * vif)
280 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
281 vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
282 vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_v1_t);
284 vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_t);
288 virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
290 u32 i, j, hw_if_index;
292 vnet_main_t *vnm = &vnet_main;
293 virtio_main_t *mm = &virtio_main;
294 virtio_vring_t *vring;
300 struct feat_struct *feat_entry;
302 static struct feat_struct feat_array[] = {
303 #define _(s,b) { .str = #s, .bit = b, },
304 foreach_virtio_net_features
309 struct feat_struct *flag_entry;
310 static struct feat_struct flags_array[] = {
311 #define _(b,e,s) { .bit = b, .str = s, },
312 foreach_virtio_if_flag
320 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
322 vnet_hw_interface_t *hi =
323 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
324 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
325 if (vif->type != type)
327 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
328 format_vnet_hw_if_index_name, vnm,
329 hw_if_indices[hw_if_index], vif->hw_if_index);
330 if (type == VIRTIO_IF_TYPE_PCI)
332 vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
335 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
338 if (vif->host_if_name)
339 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
341 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
342 if (vif->host_mtu_size)
343 vlib_cli_output (vm, " host-mtu-size \"%d\"",
345 if (type == VIRTIO_IF_TYPE_TAP)
346 vlib_cli_output (vm, " host-mac-addr: %U",
347 format_ethernet_address, vif->host_mac_addr);
348 vlib_cli_output (vm, " host-carrier-up: %u", vif->host_carrier_up);
350 vec_foreach_index (i, vif->vhost_fds)
351 str = format (str, " %d", vif->vhost_fds[i]);
352 vlib_cli_output (vm, " vhost-fds%v", str);
354 vec_foreach_index (i, vif->tap_fds)
355 str = format (str, " %d", vif->tap_fds[i]);
356 vlib_cli_output (vm, " tap-fds%v", str);
359 vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
360 vlib_cli_output (vm, " csum-enabled %d", vif->csum_offload_enabled);
361 vlib_cli_output (vm, " packet-coalesce %d", vif->packet_coalesce);
362 vlib_cli_output (vm, " packet-buffering %d", vif->packet_buffering);
363 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI))
364 vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
366 vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
367 vlib_cli_output (vm, " flags 0x%x", vif->flags);
368 flag_entry = (struct feat_struct *) &flags_array;
369 while (flag_entry->str)
371 if (vif->flags & (1ULL << flag_entry->bit))
372 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
376 if (type == VIRTIO_IF_TYPE_PCI)
378 device_status (vm, vif);
380 vlib_cli_output (vm, " features 0x%lx", vif->features);
381 feat_entry = (struct feat_struct *) &feat_array;
382 while (feat_entry->str)
384 if (vif->features & (1ULL << feat_entry->bit))
385 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
389 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
390 feat_entry = (struct feat_struct *) &feat_array;
391 while (feat_entry->str)
393 if (vif->remote_features & (1ULL << feat_entry->bit))
394 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
398 vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs);
399 vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs);
400 if (vif->cxq_vring != NULL
401 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
402 vlib_cli_output (vm, " Number of CTRL Virtqueue 1");
403 vec_foreach_index (i, vif->rxq_vrings)
405 vring = vec_elt_at_index (vif->rxq_vrings, i);
406 vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id);
408 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
409 vring->size, vring->last_used_idx, vring->desc_next,
412 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
413 vring->avail->flags, vring->avail->idx,
414 vring->used->flags, vring->used->idx);
415 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
417 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
422 vlib_cli_output (vm, "\n descriptor table:\n");
424 " id addr len flags next user_addr\n");
426 " ===== ================== ===== ====== ===== ==================\n");
427 for (j = 0; j < vring->size; j++)
429 vring_desc_t *desc = &vring->desc[j];
431 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
434 desc->flags, desc->next, desc->addr);
438 vec_foreach_index (i, vif->txq_vrings)
440 vring = vec_elt_at_index (vif->txq_vrings, i);
441 vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id);
443 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
444 vring->size, vring->last_used_idx, vring->desc_next,
447 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
448 vring->avail->flags, vring->avail->idx,
449 vring->used->flags, vring->used->idx);
450 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
452 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
455 if (vring->flow_table)
457 vlib_cli_output (vm, " %U", gro_flow_table_format,
460 if (vif->packet_buffering)
462 vlib_cli_output (vm, " %U", virtio_vring_buffering_format,
467 vlib_cli_output (vm, "\n descriptor table:\n");
469 " id addr len flags next user_addr\n");
471 " ===== ================== ===== ====== ===== ==================\n");
472 for (j = 0; j < vring->size; j++)
474 vring_desc_t *desc = &vring->desc[j];
476 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
479 desc->flags, desc->next, desc->addr);
483 if (vif->cxq_vring != NULL
484 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
486 vring = vif->cxq_vring;
487 vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id);
489 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
490 vring->size, vring->last_used_idx,
491 vring->desc_next, vring->desc_in_use);
493 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
494 vring->avail->flags, vring->avail->idx,
495 vring->used->flags, vring->used->idx);
496 if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
498 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
503 vlib_cli_output (vm, "\n descriptor table:\n");
505 " id addr len flags next user_addr\n");
507 " ===== ================== ===== ====== ===== ==================\n");
508 for (j = 0; j < vring->size; j++)
510 vring_desc_t *desc = &vring->desc[j];
512 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
515 desc->flags, desc->next, desc->addr);
524 static clib_error_t *
525 virtio_init (vlib_main_t * vm)
527 virtio_main_t *vim = &virtio_main;
528 clib_error_t *error = 0;
530 vim->log_default = vlib_log_register_class ("virtio", 0);
531 vlib_log_debug (vim->log_default, "initialized");
536 VLIB_INIT_FUNCTION (virtio_init);
539 * fd.io coding-style-patch-verification: ON
542 * eval: (c-set-style "gnu")