net/kni: set packet input port in Rx
[dpdk.git] / drivers / net / kni / rte_eth_kni.c
index 35a6d3e..831fe96 100644 (file)
@@ -6,6 +6,7 @@
 #include <pthread.h>
 #include <unistd.h>
 
+#include <rte_string_fns.h>
 #include <rte_ethdev_driver.h>
 #include <rte_ethdev_vdev.h>
 #include <rte_kni.h>
 /* Only single queue supported */
 #define KNI_MAX_QUEUE_PER_PORT 1
 
-#define MAX_PACKET_SZ 2048
 #define MAX_KNI_PORTS 8
 
+#define KNI_ETHER_MTU(mbuf_size)       \
+       ((mbuf_size) - RTE_ETHER_HDR_LEN) /**< Ethernet MTU. */
+
 #define ETH_KNI_NO_REQUEST_THREAD_ARG  "no_request_thread"
 static const char * const valid_arguments[] = {
        ETH_KNI_NO_REQUEST_THREAD_ARG,
@@ -32,7 +35,6 @@ struct eth_kni_args {
 struct pmd_queue_stats {
        uint64_t pkts;
        uint64_t bytes;
-       uint64_t err_pkts;
 };
 
 struct pmd_queue {
@@ -45,13 +47,14 @@ struct pmd_queue {
 
 struct pmd_internals {
        struct rte_kni *kni;
+       uint16_t port_id;
        int is_kni_started;
 
        pthread_t thread;
        int stop_thread;
        int no_request_thread;
 
-       struct ether_addr eth_addr;
+       struct rte_ether_addr eth_addr;
 
        struct pmd_queue rx_queues[KNI_MAX_QUEUE_PER_PORT];
        struct pmd_queue tx_queues[KNI_MAX_QUEUE_PER_PORT];
@@ -61,21 +64,28 @@ static const struct rte_eth_link pmd_link = {
                .link_speed = ETH_SPEED_NUM_10G,
                .link_duplex = ETH_LINK_FULL_DUPLEX,
                .link_status = ETH_LINK_DOWN,
-               .link_autoneg = ETH_LINK_AUTONEG,
+               .link_autoneg = ETH_LINK_FIXED,
 };
 static int is_kni_initialized;
 
+RTE_LOG_REGISTER(eth_kni_logtype, pmd.net.kni, NOTICE);
+
+#define PMD_LOG(level, fmt, args...) \
+       rte_log(RTE_LOG_ ## level, eth_kni_logtype, \
+               "%s(): " fmt "\n", __func__, ##args)
 static uint16_t
 eth_kni_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
 {
        struct pmd_queue *kni_q = q;
        struct rte_kni *kni = kni_q->internals->kni;
        uint16_t nb_pkts;
+       int i;
 
        nb_pkts = rte_kni_rx_burst(kni, bufs, nb_bufs);
+       for (i = 0; i < nb_pkts; i++)
+               bufs[i]->port = kni_q->internals->port_id;
 
        kni_q->rx.pkts += nb_pkts;
-       kni_q->rx.err_pkts += nb_bufs - nb_pkts;
 
        return nb_pkts;
 }
@@ -90,7 +100,6 @@ eth_kni_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
        nb_pkts =  rte_kni_tx_burst(kni, bufs, nb_bufs);
 
        kni_q->tx.pkts += nb_pkts;
-       kni_q->tx.err_pkts += nb_bufs - nb_pkts;
 
        return nb_pkts;
 }
@@ -118,16 +127,18 @@ eth_kni_start(struct rte_eth_dev *dev)
        struct rte_kni_conf conf;
        const char *name = dev->device->name + 4; /* remove net_ */
 
-       snprintf(conf.name, RTE_KNI_NAMESIZE, "%s", name);
+       mb_pool = internals->rx_queues[0].mb_pool;
+       strlcpy(conf.name, name, RTE_KNI_NAMESIZE);
        conf.force_bind = 0;
        conf.group_id = port_id;
-       conf.mbuf_size = MAX_PACKET_SZ;
-       mb_pool = internals->rx_queues[0].mb_pool;
+       conf.mbuf_size =
+               rte_pktmbuf_data_room_size(mb_pool) - RTE_PKTMBUF_HEADROOM;
+       conf.mtu = KNI_ETHER_MTU(conf.mbuf_size);
 
        internals->kni = rte_kni_alloc(mb_pool, &conf, NULL);
        if (internals->kni == NULL) {
-               RTE_LOG(ERR, PMD,
-                       "Fail to create kni interface for port: %d\n",
+               PMD_LOG(ERR,
+                       "Fail to create kni interface for port: %d",
                        port_id);
                return -1;
        }
@@ -149,12 +160,14 @@ eth_kni_dev_start(struct rte_eth_dev *dev)
        }
 
        if (internals->no_request_thread == 0) {
+               internals->stop_thread = 0;
+
                ret = rte_ctrl_thread_create(&internals->thread,
                        "kni_handle_req", NULL,
                        kni_handle_request, internals);
                if (ret) {
-                       RTE_LOG(ERR, PMD,
-                               "Fail to create kni request thread\n");
+                       PMD_LOG(ERR,
+                               "Fail to create kni request thread");
                        return -1;
                }
        }
@@ -170,30 +183,46 @@ eth_kni_dev_stop(struct rte_eth_dev *dev)
        struct pmd_internals *internals = dev->data->dev_private;
        int ret;
 
-       if (internals->no_request_thread == 0) {
+       if (internals->no_request_thread == 0 && internals->stop_thread == 0) {
                internals->stop_thread = 1;
 
                ret = pthread_cancel(internals->thread);
                if (ret)
-                       RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
+                       PMD_LOG(ERR, "Can't cancel the thread");
 
                ret = pthread_join(internals->thread, NULL);
                if (ret)
-                       RTE_LOG(ERR, PMD, "Can't join the thread\n");
-
-               internals->stop_thread = 0;
+                       PMD_LOG(ERR, "Can't join the thread");
        }
 
        dev->data->dev_link.link_status = 0;
 }
 
+static void
+eth_kni_close(struct rte_eth_dev *eth_dev)
+{
+       struct pmd_internals *internals;
+       int ret;
+
+       eth_kni_dev_stop(eth_dev);
+
+       /* mac_addrs must not be freed alone because part of dev_private */
+       eth_dev->data->mac_addrs = NULL;
+
+       internals = eth_dev->data->dev_private;
+       ret = rte_kni_release(internals->kni);
+       if (ret)
+               PMD_LOG(WARNING, "Not able to release kni for %s",
+                       eth_dev->data->name);
+}
+
 static int
 eth_kni_dev_configure(struct rte_eth_dev *dev __rte_unused)
 {
        return 0;
 }
 
-static void
+static int
 eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
                struct rte_eth_dev_info *dev_info)
 {
@@ -202,6 +231,8 @@ eth_kni_dev_info(struct rte_eth_dev *dev __rte_unused,
        dev_info->max_rx_queues = KNI_MAX_QUEUE_PER_PORT;
        dev_info->max_tx_queues = KNI_MAX_QUEUE_PER_PORT;
        dev_info->min_rx_bufsize = 0;
+
+       return 0;
 }
 
 static int
@@ -260,7 +291,6 @@ eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
        unsigned long rx_packets_total = 0, rx_bytes_total = 0;
        unsigned long tx_packets_total = 0, tx_bytes_total = 0;
        struct rte_eth_dev_data *data = dev->data;
-       unsigned long tx_packets_err_total = 0;
        unsigned int i, num_stats;
        struct pmd_queue *q;
 
@@ -280,22 +310,19 @@ eth_kni_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
                q = data->tx_queues[i];
                stats->q_opackets[i] = q->tx.pkts;
                stats->q_obytes[i] = q->tx.bytes;
-               stats->q_errors[i] = q->tx.err_pkts;
                tx_packets_total += stats->q_opackets[i];
                tx_bytes_total += stats->q_obytes[i];
-               tx_packets_err_total += stats->q_errors[i];
        }
 
        stats->ipackets = rx_packets_total;
        stats->ibytes = rx_bytes_total;
        stats->opackets = tx_packets_total;
        stats->obytes = tx_bytes_total;
-       stats->oerrors = tx_packets_err_total;
 
        return 0;
 }
 
-static void
+static int
 eth_kni_stats_reset(struct rte_eth_dev *dev)
 {
        struct rte_eth_dev_data *data = dev->data;
@@ -311,13 +338,15 @@ eth_kni_stats_reset(struct rte_eth_dev *dev)
                q = data->tx_queues[i];
                q->tx.pkts = 0;
                q->tx.bytes = 0;
-               q->tx.err_pkts = 0;
        }
+
+       return 0;
 }
 
 static const struct eth_dev_ops eth_kni_ops = {
        .dev_start = eth_kni_dev_start,
        .dev_stop = eth_kni_dev_stop,
+       .dev_close = eth_kni_close,
        .dev_configure = eth_kni_dev_configure,
        .dev_infos_get = eth_kni_dev_info,
        .rx_queue_setup = eth_kni_rx_queue_setup,
@@ -338,7 +367,7 @@ eth_kni_create(struct rte_vdev_device *vdev,
        struct rte_eth_dev_data *data;
        struct rte_eth_dev *eth_dev;
 
-       RTE_LOG(INFO, PMD, "Creating kni ethdev on numa socket %u\n",
+       PMD_LOG(INFO, "Creating kni ethdev on numa socket %u",
                        numa_node);
 
        /* reserve an ethdev entry */
@@ -347,13 +376,18 @@ eth_kni_create(struct rte_vdev_device *vdev,
                return NULL;
 
        internals = eth_dev->data->dev_private;
+       internals->port_id = eth_dev->data->port_id;
        data = eth_dev->data;
        data->nb_rx_queues = 1;
        data->nb_tx_queues = 1;
        data->dev_link = pmd_link;
        data->mac_addrs = &internals->eth_addr;
+       data->promiscuous = 1;
+       data->all_multicast = 1;
+
+       data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
 
-       eth_random_addr(internals->eth_addr.addr_bytes);
+       rte_eth_random_addr(internals->eth_addr.addr_bytes);
 
        eth_dev->dev_ops = &eth_kni_ops;
 
@@ -403,17 +437,18 @@ eth_kni_probe(struct rte_vdev_device *vdev)
 
        name = rte_vdev_device_name(vdev);
        params = rte_vdev_device_args(vdev);
-       RTE_LOG(INFO, PMD, "Initializing eth_kni for %s\n", name);
+       PMD_LOG(INFO, "Initializing eth_kni for %s", name);
 
-       if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
-           strlen(params) == 0) {
+       if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
                eth_dev = rte_eth_dev_attach_secondary(name);
                if (!eth_dev) {
-                       RTE_LOG(ERR, PMD, "Failed to probe %s\n", name);
+                       PMD_LOG(ERR, "Failed to probe %s", name);
                        return -1;
                }
                /* TODO: request info from primary to set up Rx and Tx */
                eth_dev->dev_ops = &eth_kni_ops;
+               eth_dev->device = &vdev->device;
+               rte_eth_dev_probing_finish(eth_dev);
                return 0;
        }
 
@@ -432,6 +467,7 @@ eth_kni_probe(struct rte_vdev_device *vdev)
        eth_dev->rx_pkt_burst = eth_kni_rx;
        eth_dev->tx_pkt_burst = eth_kni_tx;
 
+       rte_eth_dev_probing_finish(eth_dev);
        return 0;
 
 kni_uninit:
@@ -445,24 +481,22 @@ static int
 eth_kni_remove(struct rte_vdev_device *vdev)
 {
        struct rte_eth_dev *eth_dev;
-       struct pmd_internals *internals;
        const char *name;
 
        name = rte_vdev_device_name(vdev);
-       RTE_LOG(INFO, PMD, "Un-Initializing eth_kni for %s\n", name);
+       PMD_LOG(INFO, "Un-Initializing eth_kni for %s", name);
 
        /* find the ethdev entry */
        eth_dev = rte_eth_dev_allocated(name);
        if (eth_dev == NULL)
                return -1;
 
-       eth_kni_dev_stop(eth_dev);
-
-       internals = eth_dev->data->dev_private;
-       rte_kni_release(internals->kni);
-
-       rte_free(internals);
+       if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
+               eth_kni_dev_stop(eth_dev);
+               return rte_eth_dev_release_port(eth_dev);
+       }
 
+       eth_kni_close(eth_dev);
        rte_eth_dev_release_port(eth_dev);
 
        is_kni_initialized--;