4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
38 #include <sys/socket.h>
40 #include <rte_malloc.h>
41 #include <rte_kvargs.h>
42 #include <rte_ethdev_vdev.h>
43 #include <rte_bus_vdev.h>
44 #include <rte_alarm.h>
46 #include "virtio_ethdev.h"
47 #include "virtio_logs.h"
48 #include "virtio_pci.h"
49 #include "virtqueue.h"
50 #include "virtio_rxtx.h"
51 #include "virtio_user/virtio_user_dev.h"
53 #define virtio_user_get_dev(hw) \
54 ((struct virtio_user_dev *)(hw)->virtio_user_dev)
57 virtio_user_delayed_handler(void *param)
59 struct virtio_hw *hw = (struct virtio_hw *)param;
60 struct rte_eth_dev *dev = &rte_eth_devices[hw->port_id];
62 rte_intr_callback_unregister(dev->intr_handle,
63 virtio_interrupt_handler,
68 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
69 void *dst, int length)
72 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
74 if (offset == offsetof(struct virtio_net_config, mac) &&
75 length == ETHER_ADDR_LEN) {
76 for (i = 0; i < ETHER_ADDR_LEN; ++i)
77 ((uint8_t *)dst)[i] = dev->mac_addr[i];
81 if (offset == offsetof(struct virtio_net_config, status)) {
84 if (dev->vhostfd >= 0) {
88 flags = fcntl(dev->vhostfd, F_GETFL);
89 if (fcntl(dev->vhostfd, F_SETFL,
90 flags | O_NONBLOCK) == -1) {
91 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
94 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
95 if (r == 0 || (r < 0 && errno != EAGAIN)) {
96 dev->status &= (~VIRTIO_NET_S_LINK_UP);
97 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
99 /* Only client mode is available now. Once the
100 * connection is broken, it can never be up
101 * again. Besides, this function could be called
102 * in the process of interrupt handling,
103 * callback cannot be unregistered here, set an
107 virtio_user_delayed_handler,
110 dev->status |= VIRTIO_NET_S_LINK_UP;
112 fcntl(dev->vhostfd, F_SETFL, flags & (~O_NONBLOCK));
114 *(uint16_t *)dst = dev->status;
117 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
118 *(uint16_t *)dst = dev->max_queue_pairs;
122 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
123 const void *src, int length)
126 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
128 if ((offset == offsetof(struct virtio_net_config, mac)) &&
129 (length == ETHER_ADDR_LEN))
130 for (i = 0; i < ETHER_ADDR_LEN; ++i)
131 dev->mac_addr[i] = ((const uint8_t *)src)[i];
133 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
138 virtio_user_reset(struct virtio_hw *hw)
140 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
142 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
143 virtio_user_stop_device(dev);
147 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
149 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
151 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
152 virtio_user_start_device(dev);
153 else if (status == VIRTIO_CONFIG_STATUS_RESET)
154 virtio_user_reset(hw);
155 dev->status = status;
159 virtio_user_get_status(struct virtio_hw *hw)
161 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
167 virtio_user_get_features(struct virtio_hw *hw)
169 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
171 /* unmask feature bits defined in vhost user protocol */
172 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
176 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
178 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
180 dev->features = features & dev->device_features;
184 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
186 /* rxq interrupts and config interrupt are separated in virtio-user,
187 * here we only report config change.
189 return VIRTIO_PCI_ISR_CONFIG;
193 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
194 uint16_t vec __rte_unused)
200 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
201 struct virtqueue *vq __rte_unused,
204 /* pretend we have done that */
208 /* This function is to get the queue size, aka, number of descs, of a specified
209 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
210 * max supported queues.
213 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
215 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
217 /* Currently, each queue has same queue size */
218 return dev->queue_size;
222 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
224 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
225 uint16_t queue_idx = vq->vq_queue_index;
226 uint64_t desc_addr, avail_addr, used_addr;
228 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
229 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
230 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
231 ring[vq->vq_nentries]),
232 VIRTIO_PCI_VRING_ALIGN);
234 dev->vrings[queue_idx].num = vq->vq_nentries;
235 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
236 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
237 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
243 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
245 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
246 * correspondingly stops the ioeventfds, and reset the status of
248 * For modern devices, set queue desc, avail, used in PCI bar to 0,
249 * not see any more behavior in QEMU.
251 * Here we just care about what information to deliver to vhost-user
252 * or vhost-kernel. So we just close ioeventfd for now.
254 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
256 close(dev->callfds[vq->vq_queue_index]);
257 close(dev->kickfds[vq->vq_queue_index]);
261 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
264 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
266 if (hw->cvq && (hw->cvq->vq == vq)) {
267 virtio_user_handle_cq(dev, vq->vq_queue_index);
271 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
272 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
276 const struct virtio_pci_ops virtio_user_ops = {
277 .read_dev_cfg = virtio_user_read_dev_config,
278 .write_dev_cfg = virtio_user_write_dev_config,
279 .reset = virtio_user_reset,
280 .get_status = virtio_user_get_status,
281 .set_status = virtio_user_set_status,
282 .get_features = virtio_user_get_features,
283 .set_features = virtio_user_set_features,
284 .get_isr = virtio_user_get_isr,
285 .set_config_irq = virtio_user_set_config_irq,
286 .set_queue_irq = virtio_user_set_queue_irq,
287 .get_queue_num = virtio_user_get_queue_num,
288 .setup_queue = virtio_user_setup_queue,
289 .del_queue = virtio_user_del_queue,
290 .notify_queue = virtio_user_notify_queue,
293 static const char *valid_args[] = {
294 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
295 VIRTIO_USER_ARG_QUEUES_NUM,
296 #define VIRTIO_USER_ARG_CQ_NUM "cq"
297 VIRTIO_USER_ARG_CQ_NUM,
298 #define VIRTIO_USER_ARG_MAC "mac"
300 #define VIRTIO_USER_ARG_PATH "path"
301 VIRTIO_USER_ARG_PATH,
302 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
303 VIRTIO_USER_ARG_QUEUE_SIZE,
304 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
305 VIRTIO_USER_ARG_INTERFACE_NAME,
309 #define VIRTIO_USER_DEF_CQ_EN 0
310 #define VIRTIO_USER_DEF_Q_NUM 1
311 #define VIRTIO_USER_DEF_Q_SZ 256
314 get_string_arg(const char *key __rte_unused,
315 const char *value, void *extra_args)
317 if (!value || !extra_args)
320 *(char **)extra_args = strdup(value);
322 if (!*(char **)extra_args)
329 get_integer_arg(const char *key __rte_unused,
330 const char *value, void *extra_args)
332 if (!value || !extra_args)
335 *(uint64_t *)extra_args = strtoull(value, NULL, 0);
340 static struct rte_vdev_driver virtio_user_driver;
342 static struct rte_eth_dev *
343 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
345 struct rte_eth_dev *eth_dev;
346 struct rte_eth_dev_data *data;
347 struct virtio_hw *hw;
348 struct virtio_user_dev *dev;
350 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
352 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
356 data = eth_dev->data;
357 hw = eth_dev->data->dev_private;
359 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
361 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
362 rte_eth_dev_release_port(eth_dev);
367 hw->port_id = data->port_id;
368 dev->port_id = data->port_id;
369 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
371 * MSIX is required to enable LSC (see virtio_init_device).
372 * Here just pretend that we support msix.
376 hw->use_simple_rx = 0;
377 hw->use_simple_tx = 0;
378 hw->virtio_user_dev = dev;
383 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
385 struct rte_eth_dev_data *data = eth_dev->data;
386 struct virtio_hw *hw = data->dev_private;
388 rte_free(hw->virtio_user_dev);
390 rte_eth_dev_release_port(eth_dev);
393 /* Dev initialization routine. Invoked once for each virtio vdev at
394 * EAL init time, see rte_bus_probe().
395 * Returns 0 on success.
398 virtio_user_pmd_probe(struct rte_vdev_device *dev)
400 struct rte_kvargs *kvlist = NULL;
401 struct rte_eth_dev *eth_dev;
402 struct virtio_hw *hw;
403 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
404 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
405 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
408 char *mac_addr = NULL;
411 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
413 PMD_INIT_LOG(ERR, "error when parsing param");
417 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
418 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
419 &get_string_arg, &path) < 0) {
420 PMD_INIT_LOG(ERR, "error to parse %s",
421 VIRTIO_USER_ARG_PATH);
425 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
426 VIRTIO_USER_ARG_QUEUE_SIZE);
430 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
431 if (is_vhost_user_by_type(path)) {
433 "arg %s applies only to vhost-kernel backend",
434 VIRTIO_USER_ARG_INTERFACE_NAME);
438 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
439 &get_string_arg, &ifname) < 0) {
440 PMD_INIT_LOG(ERR, "error to parse %s",
441 VIRTIO_USER_ARG_INTERFACE_NAME);
446 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
447 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
448 &get_string_arg, &mac_addr) < 0) {
449 PMD_INIT_LOG(ERR, "error to parse %s",
450 VIRTIO_USER_ARG_MAC);
455 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
456 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
457 &get_integer_arg, &queue_size) < 0) {
458 PMD_INIT_LOG(ERR, "error to parse %s",
459 VIRTIO_USER_ARG_QUEUE_SIZE);
464 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
465 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
466 &get_integer_arg, &queues) < 0) {
467 PMD_INIT_LOG(ERR, "error to parse %s",
468 VIRTIO_USER_ARG_QUEUES_NUM);
473 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
474 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
475 &get_integer_arg, &cq) < 0) {
476 PMD_INIT_LOG(ERR, "error to parse %s",
477 VIRTIO_USER_ARG_CQ_NUM);
480 } else if (queues > 1) {
484 if (queues > 1 && cq == 0) {
485 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
489 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
490 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
491 VIRTIO_USER_ARG_QUEUES_NUM, queues,
492 VIRTIO_MAX_VIRTQUEUE_PAIRS);
496 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
497 eth_dev = virtio_user_eth_dev_alloc(dev);
499 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
503 hw = eth_dev->data->dev_private;
504 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
505 queue_size, mac_addr, &ifname) < 0) {
506 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
507 virtio_user_eth_dev_free(eth_dev);
511 eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
516 /* previously called by rte_pci_probe() for physical dev */
517 if (eth_virtio_dev_init(eth_dev) < 0) {
518 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
519 virtio_user_eth_dev_free(eth_dev);
526 rte_kvargs_free(kvlist);
536 /** Called by rte_eth_dev_detach() */
538 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
541 struct rte_eth_dev *eth_dev;
542 struct virtio_hw *hw;
543 struct virtio_user_dev *dev;
548 name = rte_vdev_device_name(vdev);
549 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
550 eth_dev = rte_eth_dev_allocated(name);
554 /* make sure the device is stopped, queues freed */
555 rte_eth_dev_close(eth_dev->data->port_id);
557 hw = eth_dev->data->dev_private;
558 dev = hw->virtio_user_dev;
559 virtio_user_dev_uninit(dev);
561 rte_free(eth_dev->data->dev_private);
562 rte_eth_dev_release_port(eth_dev);
567 static struct rte_vdev_driver virtio_user_driver = {
568 .probe = virtio_user_pmd_probe,
569 .remove = virtio_user_pmd_remove,
572 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
573 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
574 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,