net/vhost: move statistics into a structure
[dpdk.git] / drivers / net / vhost / rte_eth_vhost.c
index 395b892..57ede23 100644 (file)
@@ -41,7 +41,7 @@
 #include <rte_ethdev.h>
 #include <rte_malloc.h>
 #include <rte_memcpy.h>
-#include <rte_dev.h>
+#include <rte_vdev.h>
 #include <rte_kvargs.h>
 #include <rte_virtio_net.h>
 #include <rte_spinlock.h>
 
 #define ETH_VHOST_IFACE_ARG            "iface"
 #define ETH_VHOST_QUEUES_ARG           "queues"
+#define ETH_VHOST_CLIENT_ARG           "client"
 
 static const char *drivername = "VHOST PMD";
 
 static const char *valid_arguments[] = {
        ETH_VHOST_IFACE_ARG,
        ETH_VHOST_QUEUES_ARG,
+       ETH_VHOST_CLIENT_ARG,
        NULL
 };
 
@@ -70,6 +72,12 @@ static struct ether_addr base_eth_addr = {
        }
 };
 
+struct vhost_stats {
+       uint64_t pkts;
+       uint64_t bytes;
+       uint64_t missed_pkts;
+};
+
 struct vhost_queue {
        int vid;
        rte_atomic32_t allow_queuing;
@@ -78,17 +86,14 @@ struct vhost_queue {
        struct rte_mempool *mb_pool;
        uint8_t port;
        uint16_t virtqueue_id;
-       uint64_t rx_pkts;
-       uint64_t tx_pkts;
-       uint64_t missed_pkts;
-       uint64_t rx_bytes;
-       uint64_t tx_bytes;
+       struct vhost_stats stats;
 };
 
 struct pmd_internal {
        char *dev_name;
        char *iface_name;
        uint16_t max_queues;
+       uint64_t flags;
 
        volatile uint16_t once;
 };
@@ -142,11 +147,11 @@ eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
        nb_rx = rte_vhost_dequeue_burst(r->vid,
                        r->virtqueue_id, r->mb_pool, bufs, nb_bufs);
 
-       r->rx_pkts += nb_rx;
+       r->stats.pkts += nb_rx;
 
        for (i = 0; likely(i < nb_rx); i++) {
                bufs[i]->port = r->port;
-               r->rx_bytes += bufs[i]->pkt_len;
+               r->stats.bytes += bufs[i]->pkt_len;
        }
 
 out:
@@ -173,11 +178,11 @@ eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
        nb_tx = rte_vhost_enqueue_burst(r->vid,
                        r->virtqueue_id, bufs, nb_bufs);
 
-       r->tx_pkts += nb_tx;
-       r->missed_pkts += nb_bufs - nb_tx;
+       r->stats.pkts += nb_tx;
+       r->stats.missed_pkts += nb_bufs - nb_tx;
 
        for (i = 0; likely(i < nb_tx); i++)
-               r->tx_bytes += bufs[i]->pkt_len;
+               r->stats.bytes += bufs[i]->pkt_len;
 
        for (i = 0; likely(i < nb_tx); i++)
                rte_pktmbuf_free(bufs[i]);
@@ -300,6 +305,7 @@ destroy_device(int vid)
        struct internal_list *list;
        char ifname[PATH_MAX];
        unsigned i;
+       struct rte_vhost_vring_state *state;
 
        rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
        list = find_internal_resource(ifname);
@@ -342,6 +348,15 @@ destroy_device(int vid)
                vq->vid = -1;
        }
 
+       state = vring_states[eth_dev->data->port_id];
+       rte_spinlock_lock(&state->lock);
+       for (i = 0; i <= state->max_vring; i++) {
+               state->cur[i] = false;
+               state->seen[i] = false;
+       }
+       state->max_vring = 0;
+       rte_spinlock_unlock(&state->lock);
+
        RTE_LOG(INFO, PMD, "Connection closed\n");
 
        _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
@@ -415,6 +430,35 @@ rte_eth_vhost_get_queue_event(uint8_t port_id,
        return -1;
 }
 
+int
+rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
+{
+       struct internal_list *list;
+       struct rte_eth_dev *eth_dev;
+       struct vhost_queue *vq;
+       int vid = -1;
+
+       if (!rte_eth_dev_is_valid_port(port_id))
+               return -1;
+
+       pthread_mutex_lock(&internal_list_lock);
+
+       TAILQ_FOREACH(list, &internal_list, next) {
+               eth_dev = list->eth_dev;
+               if (eth_dev->data->port_id == port_id) {
+                       vq = eth_dev->data->rx_queues[0];
+                       if (vq) {
+                               vid = vq->vid;
+                       }
+                       break;
+               }
+       }
+
+       pthread_mutex_unlock(&internal_list_lock);
+
+       return vid;
+}
+
 static void *
 vhost_driver_session(void *param __rte_unused)
 {
@@ -467,7 +511,8 @@ eth_dev_start(struct rte_eth_dev *dev)
        int ret = 0;
 
        if (rte_atomic16_cmpset(&internal->once, 0, 1)) {
-               ret = rte_vhost_driver_register(internal->iface_name);
+               ret = rte_vhost_driver_register(internal->iface_name,
+                                               internal->flags);
                if (ret)
                        return ret;
        }
@@ -568,10 +613,10 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
                if (dev->data->rx_queues[i] == NULL)
                        continue;
                vq = dev->data->rx_queues[i];
-               stats->q_ipackets[i] = vq->rx_pkts;
+               stats->q_ipackets[i] = vq->stats.pkts;
                rx_total += stats->q_ipackets[i];
 
-               stats->q_ibytes[i] = vq->rx_bytes;
+               stats->q_ibytes[i] = vq->stats.bytes;
                rx_total_bytes += stats->q_ibytes[i];
        }
 
@@ -580,17 +625,17 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
                if (dev->data->tx_queues[i] == NULL)
                        continue;
                vq = dev->data->tx_queues[i];
-               stats->q_opackets[i] = vq->tx_pkts;
-               tx_missed_total += vq->missed_pkts;
+               stats->q_opackets[i] = vq->stats.pkts;
+               tx_missed_total += vq->stats.missed_pkts;
                tx_total += stats->q_opackets[i];
 
-               stats->q_obytes[i] = vq->tx_bytes;
+               stats->q_obytes[i] = vq->stats.bytes;
                tx_total_bytes += stats->q_obytes[i];
        }
 
        stats->ipackets = rx_total;
        stats->opackets = tx_total;
-       stats->imissed = tx_missed_total;
+       stats->oerrors = tx_missed_total;
        stats->ibytes = rx_total_bytes;
        stats->obytes = tx_total_bytes;
 }
@@ -605,16 +650,16 @@ eth_stats_reset(struct rte_eth_dev *dev)
                if (dev->data->rx_queues[i] == NULL)
                        continue;
                vq = dev->data->rx_queues[i];
-               vq->rx_pkts = 0;
-               vq->rx_bytes = 0;
+               vq->stats.pkts = 0;
+               vq->stats.bytes = 0;
        }
        for (i = 0; i < dev->data->nb_tx_queues; i++) {
                if (dev->data->tx_queues[i] == NULL)
                        continue;
                vq = dev->data->tx_queues[i];
-               vq->tx_pkts = 0;
-               vq->tx_bytes = 0;
-               vq->missed_pkts = 0;
+               vq->stats.pkts = 0;
+               vq->stats.bytes = 0;
+               vq->stats.missed_pkts = 0;
        }
 }
 
@@ -672,7 +717,7 @@ static const struct eth_dev_ops ops = {
 
 static int
 eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
-                    const unsigned numa_node)
+                    const unsigned numa_node, uint64_t flags)
 {
        struct rte_eth_dev_data *data = NULL;
        struct pmd_internal *internal = NULL;
@@ -700,7 +745,7 @@ eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
                goto error;
 
        /* reserve an ethdev entry */
-       eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
+       eth_dev = rte_eth_dev_allocate(name);
        if (eth_dev == NULL)
                goto error;
 
@@ -729,6 +774,7 @@ eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
        internal->iface_name = strdup(iface_name);
        if (internal->iface_name == NULL)
                goto error;
+       internal->flags = flags;
 
        list->eth_dev = eth_dev;
        pthread_mutex_lock(&internal_list_lock);
@@ -793,18 +839,15 @@ open_iface(const char *key __rte_unused, const char *value, void *extra_args)
 }
 
 static inline int
-open_queues(const char *key __rte_unused, const char *value, void *extra_args)
+open_int(const char *key __rte_unused, const char *value, void *extra_args)
 {
-       uint16_t *q = extra_args;
+       uint16_t *n = extra_args;
 
        if (value == NULL || extra_args == NULL)
                return -EINVAL;
 
-       *q = (uint16_t)strtoul(value, NULL, 0);
-       if (*q == USHRT_MAX && errno == ERANGE)
-               return -1;
-
-       if (*q > RTE_MAX_QUEUES_PER_PORT)
+       *n = (uint16_t)strtoul(value, NULL, 0);
+       if (*n == USHRT_MAX && errno == ERANGE)
                return -1;
 
        return 0;
@@ -817,6 +860,8 @@ rte_pmd_vhost_devinit(const char *name, const char *params)
        int ret = 0;
        char *iface_name;
        uint16_t queues;
+       uint64_t flags = 0;
+       int client_mode = 0;
 
        RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
 
@@ -836,14 +881,24 @@ rte_pmd_vhost_devinit(const char *name, const char *params)
 
        if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
                ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
-                                        &open_queues, &queues);
-               if (ret < 0)
+                                        &open_int, &queues);
+               if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
                        goto out_free;
 
        } else
                queues = 1;
 
-       eth_dev_vhost_create(name, iface_name, queues, rte_socket_id());
+       if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
+               ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
+                                        &open_int, &client_mode);
+               if (ret < 0)
+                       goto out_free;
+
+               if (client_mode)
+                       flags |= RTE_VHOST_USER_CLIENT;
+       }
+
+       eth_dev_vhost_create(name, iface_name, queues, rte_socket_id(), flags);
 
 out_free:
        rte_kvargs_free(kvlist);
@@ -900,11 +955,12 @@ rte_pmd_vhost_devuninit(const char *name)
        return 0;
 }
 
-static struct rte_driver pmd_vhost_drv = {
-       .name = "eth_vhost",
-       .type = PMD_VDEV,
+static struct rte_vdev_driver pmd_vhost_drv = {
        .init = rte_pmd_vhost_devinit,
        .uninit = rte_pmd_vhost_devuninit,
 };
 
-PMD_REGISTER_DRIVER(pmd_vhost_drv);
+DRIVER_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
+DRIVER_REGISTER_PARAM_STRING(net_vhost,
+       "iface=<ifc> "
+       "queues=<int>");