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 <linux/virtio_net.h>
25 #include <linux/vhost.h>
26 #include <sys/eventfd.h>
28 #include <vlib/vlib.h>
29 #include <vlib/pci/pci.h>
30 #include <vlib/unix/unix.h>
31 #include <vnet/ethernet/ethernet.h>
32 #include <vnet/ip/ip4_packet.h>
33 #include <vnet/ip/ip6_packet.h>
34 #include <vnet/devices/virtio/virtio.h>
35 #include <vnet/devices/virtio/pci.h>
37 virtio_main_t virtio_main;
39 #define _IOCTL(fd,a,...) \
40 if (ioctl (fd, a, __VA_ARGS__) < 0) \
42 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
47 call_read_ready (clib_file_t * uf)
49 virtio_main_t *nm = &virtio_main;
50 vnet_main_t *vnm = vnet_get_main ();
51 u16 qid = uf->private_data & 0xFFFF;
53 vec_elt_at_index (nm->interfaces, uf->private_data >> 16);
56 CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
58 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
65 virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz)
67 clib_error_t *err = 0;
68 virtio_vring_t *vring;
69 struct vhost_vring_state state = { 0 };
70 struct vhost_vring_addr addr = { 0 };
71 struct vhost_vring_file file = { 0 };
72 clib_file_t t = { 0 };
76 return clib_error_return (0, "ring size must be power of 2");
79 return clib_error_return (0, "ring size must be 32768 or lower");
86 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx),
87 CLIB_CACHE_LINE_BYTES);
88 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
92 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (idx),
93 CLIB_CACHE_LINE_BYTES);
94 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
96 i = sizeof (struct vring_desc) * sz;
97 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
98 vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
99 clib_memset (vring->desc, 0, i);
101 i = sizeof (struct vring_avail) + sz * sizeof (vring->avail->ring[0]);
102 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
103 vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
104 clib_memset (vring->avail, 0, i);
105 // tell kernel that we don't need interrupt
106 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
108 i = sizeof (struct vring_used) + sz * sizeof (struct vring_used_elem);
109 i = round_pow2 (i, CLIB_CACHE_LINE_BYTES);
110 vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES);
111 clib_memset (vring->used, 0, i);
113 vring->queue_id = idx;
114 ASSERT (vring->buffers == 0);
115 vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES);
118 vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
119 vring->kick_fd = eventfd (0, EFD_CLOEXEC);
121 t.read_function = call_read_ready;
122 t.file_descriptor = vring->call_fd;
123 t.private_data = vif->dev_instance << 16 | idx;
124 t.description = format (0, "%U vring %u", format_virtio_device_name,
125 vif->dev_instance, idx);
126 vring->call_file_index = clib_file_add (&file_main, &t);
130 _IOCTL (vif->fd, VHOST_SET_VRING_NUM, &state);
134 addr.desc_user_addr = pointer_to_uword (vring->desc);
135 addr.avail_user_addr = pointer_to_uword (vring->avail);
136 addr.used_user_addr = pointer_to_uword (vring->used);
137 _IOCTL (vif->fd, VHOST_SET_VRING_ADDR, &addr);
140 file.fd = vring->kick_fd;
141 _IOCTL (vif->fd, VHOST_SET_VRING_KICK, &file);
142 file.fd = vring->call_fd;
143 _IOCTL (vif->fd, VHOST_SET_VRING_CALL, &file);
144 file.fd = vif->tap_fd;
145 _IOCTL (vif->fd, VHOST_NET_SET_BACKEND, &file);
152 virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring)
154 u16 used = vring->desc_in_use;
155 u16 last = vring->last_used_idx;
156 u16 mask = vring->size - 1;
160 vlib_buffer_free (vm, &vring->buffers[last & mask], 1);
167 virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
169 virtio_vring_t *vring =
170 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
172 clib_file_del_by_index (&file_main, vring->call_file_index);
173 close (vring->kick_fd);
174 close (vring->call_fd);
177 virtio_free_rx_buffers (vm, vring);
178 clib_mem_free (vring->used);
181 clib_mem_free (vring->desc);
183 clib_mem_free (vring->avail);
184 vec_free (vring->buffers);
189 virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring)
191 u16 used = vring->desc_in_use;
192 u16 sz = vring->size;
194 u16 last = vring->last_used_idx;
195 u16 n_left = vring->used->idx - last;
202 struct vring_used_elem *e = &vring->used->ring[last & mask];
205 vlib_buffer_free (vm, &vring->buffers[slot], 1);
210 vring->desc_in_use = used;
211 vring->last_used_idx = last;
215 virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
217 virtio_vring_t *vring =
218 vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx));
220 clib_file_del_by_index (&file_main, vring->call_file_index);
221 close (vring->kick_fd);
222 close (vring->call_fd);
225 virtio_free_used_desc (vm, vring);
226 clib_mem_free (vring->used);
229 clib_mem_free (vring->desc);
231 clib_mem_free (vring->avail);
232 vec_free (vring->buffers);
237 virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif, u32 idx)
239 vnet_main_t *vnm = vnet_get_main ();
241 virtio_vring_t *vring =
242 vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx));
244 vnet_get_device_input_thread_index (vnm, vif->hw_if_index,
245 RX_QUEUE_ACCESS (idx));
246 vring->buffer_pool_index =
247 vlib_buffer_pool_get_default_for_numa (vm,
249 [thread_index]->numa_node);
253 virtio_set_net_hdr_size (virtio_if_t * vif)
255 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) ||
256 vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1))
257 vif->virtio_net_hdr_sz = sizeof (struct virtio_net_hdr_v1);
259 vif->virtio_net_hdr_sz = sizeof (struct virtio_net_hdr);
263 virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
265 u32 i, j, hw_if_index;
267 vnet_main_t *vnm = &vnet_main;
268 virtio_main_t *mm = &virtio_main;
269 virtio_vring_t *vring;
275 struct feat_struct *feat_entry;
277 static struct feat_struct feat_array[] = {
278 #define _(s,b) { .str = #s, .bit = b, },
279 foreach_virtio_net_features
284 struct feat_struct *flag_entry;
285 static struct feat_struct flags_array[] = {
286 #define _(b,e,s) { .bit = b, .str = s, },
287 foreach_virtio_if_flag
295 for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
297 vnet_hw_interface_t *hi =
298 vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
299 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
300 if (vif->type != type)
302 vlib_cli_output (vm, "Interface: %U (ifindex %d)",
303 format_vnet_hw_if_index_name, vnm,
304 hw_if_indices[hw_if_index], vif->hw_if_index);
305 if (type == VIRTIO_IF_TYPE_PCI)
307 vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
310 if (type == VIRTIO_IF_TYPE_TAP)
312 if (vif->host_if_name)
313 vlib_cli_output (vm, " name \"%s\"", vif->host_if_name);
315 vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
316 vlib_cli_output (vm, " fd %d", vif->fd);
317 vlib_cli_output (vm, " tap-fd %d", vif->tap_fd);
318 vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
320 vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
322 vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
323 vlib_cli_output (vm, " flags 0x%x", vif->flags);
324 flag_entry = (struct feat_struct *) &flags_array;
325 while (flag_entry->str)
327 if (vif->flags & (1ULL << flag_entry->bit))
328 vlib_cli_output (vm, " %s (%d)", flag_entry->str,
332 if (type == VIRTIO_IF_TYPE_PCI)
334 device_status (vm, vif);
336 vlib_cli_output (vm, " features 0x%lx", vif->features);
337 feat_entry = (struct feat_struct *) &feat_array;
338 while (feat_entry->str)
340 if (vif->features & (1ULL << feat_entry->bit))
341 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
345 vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
346 feat_entry = (struct feat_struct *) &feat_array;
347 while (feat_entry->str)
349 if (vif->remote_features & (1ULL << feat_entry->bit))
350 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
354 vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs);
355 vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs);
356 if (vif->cxq_vring != NULL
357 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
358 vlib_cli_output (vm, " Number of CTRL Virtqueue 1");
359 vec_foreach_index (i, vif->rxq_vrings)
361 vring = vec_elt_at_index (vif->rxq_vrings, i);
362 vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id);
364 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
365 vring->size, vring->last_used_idx, vring->desc_next,
368 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
369 vring->avail->flags, vring->avail->idx,
370 vring->used->flags, vring->used->idx);
371 if (type == VIRTIO_IF_TYPE_TAP)
373 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
378 vlib_cli_output (vm, "\n descriptor table:\n");
380 " id addr len flags next user_addr\n");
382 " ===== ================== ===== ====== ===== ==================\n");
383 for (j = 0; j < vring->size; j++)
385 struct vring_desc *desc = &vring->desc[j];
387 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
390 desc->flags, desc->next, desc->addr);
394 vec_foreach_index (i, vif->txq_vrings)
396 vring = vec_elt_at_index (vif->txq_vrings, i);
397 vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id);
399 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
400 vring->size, vring->last_used_idx, vring->desc_next,
403 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
404 vring->avail->flags, vring->avail->idx,
405 vring->used->flags, vring->used->idx);
406 if (type == VIRTIO_IF_TYPE_TAP)
408 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
413 vlib_cli_output (vm, "\n descriptor table:\n");
415 " id addr len flags next user_addr\n");
417 " ===== ================== ===== ====== ===== ==================\n");
418 for (j = 0; j < vring->size; j++)
420 struct vring_desc *desc = &vring->desc[j];
422 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
425 desc->flags, desc->next, desc->addr);
429 if (vif->cxq_vring != NULL
430 && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
432 vring = vif->cxq_vring;
433 vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id);
435 " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d",
436 vring->size, vring->last_used_idx,
437 vring->desc_next, vring->desc_in_use);
439 " avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
440 vring->avail->flags, vring->avail->idx,
441 vring->used->flags, vring->used->idx);
442 if (type == VIRTIO_IF_TYPE_TAP)
444 vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
449 vlib_cli_output (vm, "\n descriptor table:\n");
451 " id addr len flags next user_addr\n");
453 " ===== ================== ===== ====== ===== ==================\n");
454 for (j = 0; j < vring->size; j++)
456 struct vring_desc *desc = &vring->desc[j];
458 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
461 desc->flags, desc->next, desc->addr);
471 * fd.io coding-style-patch-verification: ON
474 * eval: (c-set-style "gnu")