Add a new virtual device named vhost-user, which can be used just like
eth_ring, eth_null, etc. To reuse the code of original virtio, we do
some adjustment in virtio_ethdev.c, such as remove key _static_ of
eth_virtio_dev_init() so that it can be reused in virtual device; and
we add some check to make sure it will not crash.

Configured parameters include:
  - queues (optional, 1 by default), number of queue pairs, multi-queue
    not supported for now.
  - cq (optional, 0 by default), not supported for now.
  - mac (optional), random value will be given if not specified.
  - queue_size (optional, 256 by default), size of virtqueues.
  - path (madatory), path of vhost, depends on the file type, vhost
    user if the given path points to a unix socket; vhost-net if the
    given path points to a char device.
  - ifname (optional), specify the name of backend tap device; only
    valid when backend is vhost-net.

When enable CONFIG_RTE_VIRTIO_VDEV (enabled by default), the compiled
library can be used in both VM and container environment.

Examples:
path_vhost=/dev/vhost-net # use vhost-net as a backend
path_vhost=<path_to_vhost_user> # use vhost-user as a backend

sudo ./examples/l2fwd/build/l2fwd -c 0x100000 -n 4 \
    --socket-mem 0,1024 --no-pci --file-prefix=l2fwd \
    --vdev=virtio-user0,mac=00:01:02:03:04:05,path=$path_vhost -- -p 0x1

Known issues:
 - Control queue and multi-queue are not supported yet.
 - Cannot work with --huge-unlink.
 - Cannot work with no-huge.
 - Cannot work when there are more than VHOST_MEMORY_MAX_NREGIONS(8)
   hugepages.
 - Root privilege is a must (mainly becase of sorting hugepages according
   to physical address).
 - Applications should not use file name like HUGEFILE_FMT ("%smap_%d").

Signed-off-by: Huawei Xie <huawei.xie at intel.com>
Signed-off-by: Jianfeng Tan <jianfeng.tan at intel.com>
Acked-by: Neil Horman <nhorman at tuxdriver.com>
---
 drivers/net/virtio/virtio_ethdev.c               |  19 +-
 drivers/net/virtio/virtio_ethdev.h               |   2 +
 drivers/net/virtio/virtio_user/virtio_user_dev.c | 309 +++++++++++++++++++++++
 3 files changed, 323 insertions(+), 7 deletions(-)

diff --git a/drivers/net/virtio/virtio_ethdev.c 
b/drivers/net/virtio/virtio_ethdev.c
index 1866afd..f8972f2 100644
--- a/drivers/net/virtio/virtio_ethdev.c
+++ b/drivers/net/virtio/virtio_ethdev.c
@@ -59,7 +59,6 @@
 #include "virtqueue.h"
 #include "virtio_rxtx.h"

-static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
 static int  virtio_dev_configure(struct rte_eth_dev *dev);
 static int  virtio_dev_start(struct rte_eth_dev *dev);
@@ -1038,7 +1037,7 @@ rx_func_get(struct rte_eth_dev *eth_dev)
  * This function is based on probe() function in virtio_pci.c
  * It returns 0 on success.
  */
-static int
+int
 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 {
        struct virtio_hw *hw = eth_dev->data->dev_private;
@@ -1069,9 +1068,11 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)

        pci_dev = eth_dev->pci_dev;

-       ret = vtpci_init(pci_dev, hw, &dev_flags);
-       if (ret)
-               return ret;
+       if (pci_dev) {
+               ret = vtpci_init(pci_dev, hw, &dev_flags);
+               if (ret)
+                       return ret;
+       }

        /* Reset the device although not necessary at startup */
        vtpci_reset(hw);
@@ -1163,7 +1164,8 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)

        PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d   hw->max_tx_queues=%d",
                        hw->max_rx_queues, hw->max_tx_queues);
-       PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
+       if (pci_dev)
+               PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
                        eth_dev->data->port_id, pci_dev->id.vendor_id,
                        pci_dev->id.device_id);

@@ -1442,7 +1444,10 @@ virtio_dev_info_get(struct rte_eth_dev *dev, struct 
rte_eth_dev_info *dev_info)
 {
        struct virtio_hw *hw = dev->data->dev_private;

-       dev_info->driver_name = dev->driver->pci_drv.name;
+       if (dev->pci_dev)
+               dev_info->driver_name = dev->driver->pci_drv.name;
+       else
+               dev_info->driver_name = "virtio-user PMD";
        dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
        dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
        dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
diff --git a/drivers/net/virtio/virtio_ethdev.h 
b/drivers/net/virtio/virtio_ethdev.h
index 66423a0..284afaa 100644
--- a/drivers/net/virtio/virtio_ethdev.h
+++ b/drivers/net/virtio/virtio_ethdev.h
@@ -113,6 +113,8 @@ uint16_t virtio_recv_pkts_vec(void *rx_queue, struct 
rte_mbuf **rx_pkts,
 uint16_t virtio_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
                uint16_t nb_pkts);

+int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
+
 /*
  * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us
  * frames larger than 1514 bytes. We do not yet support software LRO
diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c 
b/drivers/net/virtio/virtio_user/virtio_user_dev.c
index 41d8ad1..5e4f60b 100644
--- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
+++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
@@ -166,3 +166,312 @@ int virtio_user_stop_device(struct virtio_user_dev *dev)
        return vhost_call(dev->vhostfd, dev->type, VHOST_MSG_RESET_OWNER, NULL);
 }

+static inline void parse_mac(struct virtio_user_dev *dev, const char *mac)
+{
+       int i, r;
+       uint32_t tmp[ETHER_ADDR_LEN];
+
+       if (!mac)
+               return;
+
+       r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
+                       &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
+       if (r == ETHER_ADDR_LEN) {
+               for (i = 0; i < ETHER_ADDR_LEN; ++i)
+                       dev->mac_addr[i] = (uint8_t)tmp[i];
+               dev->mac_specified = 1;
+       } else {
+               /* ignore the wrong mac, use random mac */
+               PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
+       }
+}
+
+static int
+virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
+                int queue_size, const char *mac, char *ifname)
+{
+       struct stat s;
+
+       strncpy(dev->path, path, PATH_MAX);
+       dev->max_queue_pairs = queues;
+       dev->queue_pairs = 1; /* mq disabled by default */
+       dev->queue_size = queue_size;
+       dev->mac_specified = 0;
+       parse_mac(dev, mac);
+       dev->vhostfd = -1;
+       dev->tapfd = -1;
+
+       if (stat(dev->path, &s) < 0) {
+               PMD_INIT_LOG(ERR, "stat: %s failed, %s", dev->path,
+                            strerror(errno));
+               return -1;
+       }
+
+       switch (s.st_mode & S_IFMT) {
+       case S_IFCHR:
+               dev->type = VHOST_KERNEL;
+               dev->vhostfd = vhost_kernel_setup(dev->path, ifname,
+                                                 &dev->tapfd);
+               break;
+       case S_IFSOCK:
+               dev->type = VHOST_USER;
+               dev->vhostfd = vhost_user_setup(dev->path);
+               break;
+       default:
+               PMD_INIT_LOG(ERR, "unknown file type of %s", dev->path);
+               return -1;
+       }
+       if (dev->vhostfd < 0) {
+               PMD_INIT_LOG(ERR, "backend set up fails");
+               return -1;
+       }
+       if (vhost_call(dev->vhostfd, dev->type,
+                       VHOST_MSG_SET_OWNER, NULL) < 0) {
+               PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
+               return -1;
+       }
+
+       if (vhost_call(dev->vhostfd, dev->type,
+                       VHOST_MSG_GET_FEATURES, &dev->features) < 0) {
+               PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
+               return -1;
+       }
+       if (dev->mac_specified)
+               dev->features |= (1ull << VIRTIO_NET_F_MAC);
+       /* disable it until we support CQ */
+       dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
+       dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
+
+       return 0;
+
+}
+
+static void
+virtio_user_dev_uninit(struct virtio_user_dev *dev)
+{
+       uint32_t i;
+
+       if (dev->type == VHOST_KERNEL)
+               close(dev->tapfd);
+
+       for (i = 0; i < dev->queue_pairs * 2; ++i) {
+               close(dev->callfds[i]);
+               close(dev->kickfds[i]);
+       }
+
+       close(dev->vhostfd);
+}
+
+static const char *valid_args[] = {
+#define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
+       VIRTIO_USER_ARG_QUEUES_NUM,
+#define VIRTIO_USER_ARG_CQ_NUM         "cq"
+       VIRTIO_USER_ARG_CQ_NUM,
+#define VIRTIO_USER_ARG_MAC            "mac"
+       VIRTIO_USER_ARG_MAC,
+#define VIRTIO_USER_ARG_PATH           "path"
+       VIRTIO_USER_ARG_PATH,
+#define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
+       VIRTIO_USER_ARG_QUEUE_SIZE,
+#define VIRTIO_USER_ARG_IFNAME         "ifname"
+       VIRTIO_USER_ARG_IFNAME,
+       NULL
+};
+
+#define VIRTIO_USER_DEF_CQ_EN  0
+#define VIRTIO_USER_DEF_Q_NUM  1
+#define VIRTIO_USER_DEF_Q_SZ   256
+
+static int
+get_string_arg(const char *key __rte_unused,
+              const char *value, void *extra_args)
+{
+       if (!value || !extra_args)
+               return -EINVAL;
+
+       *(char **)extra_args = strdup(value);
+
+       return 0;
+}
+
+static int
+get_integer_arg(const char *key __rte_unused,
+               const char *value, void *extra_args)
+{
+       if (!value || !extra_args)
+               return -EINVAL;
+
+       *(uint64_t *)extra_args = strtoull(value, NULL, 0);
+
+       return 0;
+}
+
+static struct rte_eth_dev *
+virtio_user_eth_dev_alloc(const char *name)
+{
+       struct rte_eth_dev *eth_dev;
+       struct rte_eth_dev_data *data;
+       struct virtio_hw *hw;
+       struct virtio_user_dev *dev;
+
+       eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
+       if (!eth_dev) {
+               PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
+               return NULL;
+       }
+
+       data = eth_dev->data;
+
+       hw = rte_zmalloc(NULL, sizeof(*hw), 0);
+       if (!hw) {
+               PMD_INIT_LOG(ERR, "malloc virtio_hw failed");
+               rte_eth_dev_release_port(eth_dev);
+               return NULL;
+       }
+
+       dev = rte_zmalloc(NULL, sizeof(*dev), 0);
+       if (!dev) {
+               PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
+               rte_eth_dev_release_port(eth_dev);
+               rte_free(hw);
+               return NULL;
+       }
+
+       hw->vtpci_ops = &vdev_ops;
+       hw->use_msix = 0;
+       hw->modern   = 0;
+       hw->virtio_user_dev = dev;
+       data->dev_private = hw;
+       data->numa_node = SOCKET_ID_ANY;
+       data->kdrv = RTE_KDRV_NONE;
+       data->dev_flags = RTE_ETH_DEV_DETACHABLE;
+       eth_dev->pci_dev = NULL;
+       eth_dev->driver = NULL;
+       return eth_dev;
+}
+
+/* Dev initialization routine. Invoked once for each virtio vdev at
+ * EAL init time, see rte_eal_dev_init().
+ * Returns 0 on success.
+ */
+static int
+virtio_user_pmd_devinit(const char *name, const char *params)
+{
+       struct rte_kvargs *kvlist;
+       struct rte_eth_dev *eth_dev;
+       struct virtio_hw *hw;
+       uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
+       uint64_t nb_cq = VIRTIO_USER_DEF_CQ_EN;
+       uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
+       char *path = NULL;
+       char *mac_addr = NULL;
+       char *ifname = NULL;
+       int ret = -1;
+
+       if (!params || params[0] == '\0') {
+               PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio-user",
+                         VIRTIO_USER_ARG_QUEUE_SIZE);
+               goto end;
+       }
+
+       kvlist = rte_kvargs_parse(params, valid_args);
+       if (!kvlist) {
+               PMD_INIT_LOG(ERR, "error when parsing param");
+               goto end;
+       }
+
+       if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1)
+               rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
+                                  &get_string_arg, &path);
+       else {
+               PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio-user\n",
+                         VIRTIO_USER_ARG_QUEUE_SIZE);
+               goto end;
+       }
+
+       if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1)
+               rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
+                                  &get_string_arg, &mac_addr);
+
+       if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IFNAME) == 1)
+               rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IFNAME,
+                                  &get_string_arg, &ifname);
+
+       if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1)
+               rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
+                                  &get_integer_arg, &queue_size);
+
+       if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1)
+               rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
+                                  &get_integer_arg, &queues);
+
+       if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1)
+               rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
+                                  &get_integer_arg, &nb_cq);
+
+       eth_dev = virtio_user_eth_dev_alloc(name);
+       if (!eth_dev) {
+               PMD_INIT_LOG(ERR, "virtio-user fails to alloc device");
+               goto end;
+       }
+
+       hw = eth_dev->data->dev_private;
+       if (virtio_user_dev_init(hw->virtio_user_dev, path, queues,
+                            queue_size, mac_addr, ifname) < 0)
+               goto end;
+
+       /* previously called by rte_eal_pci_probe() for physical dev */
+       if (eth_virtio_dev_init(eth_dev) < 0) {
+               PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
+               goto end;
+       }
+       ret = 0;
+
+end:
+       if (path)
+               free(path);
+       if (mac_addr)
+               free(mac_addr);
+       if (ifname)
+               free(ifname);
+       return ret;
+}
+
+/** Called by rte_eth_dev_detach() */
+static int
+virtio_user_pmd_devuninit(const char *name)
+{
+       struct rte_eth_dev *eth_dev;
+       struct virtio_hw *hw;
+       struct virtio_user_dev *dev;
+
+       if (!name)
+               return -EINVAL;
+
+       PMD_DRV_LOG(INFO, "Un-Initializing %s\n", name);
+       eth_dev = rte_eth_dev_allocated(name);
+       if (!eth_dev)
+               return -ENODEV;
+
+       /* make sure the device is stopped, queues freed */
+       rte_eth_dev_close(eth_dev->data->port_id);
+
+       hw = eth_dev->data->dev_private;
+       dev = hw->virtio_user_dev;
+       virtio_user_dev_uninit(dev);
+
+       rte_free(eth_dev->data->dev_private);
+       rte_free(eth_dev->data);
+       rte_eth_dev_release_port(eth_dev);
+
+       return 0;
+}
+
+static struct rte_driver virtio_user_driver = {
+       .name   = "virtio-user",
+       .type   = PMD_VDEV,
+       .init   = virtio_user_pmd_devinit,
+       .uninit = virtio_user_pmd_devuninit,
+};
+
+PMD_REGISTER_DRIVER(virtio_user_driver);
-- 
2.1.4

Reply via email to