mbuf: add namespace to offload flags
[dpdk.git] / drivers / net / af_packet / rte_eth_af_packet.c
index f5806bf..559f5a0 100644 (file)
@@ -8,8 +8,8 @@
 
 #include <rte_string_fns.h>
 #include <rte_mbuf.h>
-#include <rte_ethdev_driver.h>
-#include <rte_ethdev_vdev.h>
+#include <ethdev_driver.h>
+#include <ethdev_vdev.h>
 #include <rte_malloc.h>
 #include <rte_kvargs.h>
 #include <rte_bus_vdev.h>
@@ -19,6 +19,7 @@
 #include <linux/if_packet.h>
 #include <arpa/inet.h>
 #include <net/if.h>
+#include <net/if_arp.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
@@ -37,8 +38,6 @@
 #define DFLT_FRAME_SIZE                (1 << 11)
 #define DFLT_FRAME_COUNT       (1 << 9)
 
-#define RTE_PMD_AF_PACKET_MAX_RINGS 16
-
 struct pkt_rx_queue {
        int sockfd;
 
@@ -49,6 +48,7 @@ struct pkt_rx_queue {
 
        struct rte_mempool *mb_pool;
        uint16_t in_port;
+       uint8_t vlan_strip;
 
        volatile unsigned long rx_pkts;
        volatile unsigned long rx_bytes;
@@ -77,8 +77,9 @@ struct pmd_internals {
 
        struct tpacket_req req;
 
-       struct pkt_rx_queue rx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
-       struct pkt_tx_queue tx_queue[RTE_PMD_AF_PACKET_MAX_RINGS];
+       struct pkt_rx_queue *rx_queue;
+       struct pkt_tx_queue *tx_queue;
+       uint8_t vlan_strip;
 };
 
 static const char *valid_arguments[] = {
@@ -92,13 +93,13 @@ static const char *valid_arguments[] = {
 };
 
 static 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_FIXED,
+       .link_speed = RTE_ETH_SPEED_NUM_10G,
+       .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
+       .link_status = RTE_ETH_LINK_DOWN,
+       .link_autoneg = RTE_ETH_LINK_FIXED,
 };
 
-static int af_packet_logtype;
+RTE_LOG_REGISTER_DEFAULT(af_packet_logtype, NOTICE);
 
 #define PMD_LOG(level, fmt, args...) \
        rte_log(RTE_LOG_ ## level, af_packet_logtype, \
@@ -148,7 +149,10 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                /* check for vlan info */
                if (ppd->tp_status & TP_STATUS_VLAN_VALID) {
                        mbuf->vlan_tci = ppd->tp_vlan_tci;
-                       mbuf->ol_flags |= (PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED);
+                       mbuf->ol_flags |= (RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED);
+
+                       if (!pkt_q->vlan_strip && rte_vlan_insert(&mbuf))
+                               PMD_LOG(ERR, "Failed to reinsert VLAN tag");
                }
 
                /* release incoming frame and advance ring buffer */
@@ -168,6 +172,26 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
        return num_rx;
 }
 
+/*
+ * Check if there is an available frame in the ring
+ */
+static inline bool
+tx_ring_status_available(uint32_t tp_status)
+{
+       /*
+        * We eliminate the timestamp status from the packet status.
+        * This should only matter if timestamping is enabled on the socket,
+        * but there is a bug in the kernel which is fixed in newer releases.
+        *
+        * See the following kernel commit for reference:
+        *     commit 171c3b151118a2fe0fc1e2a9d1b5a1570cfe82d2
+        *     net: packetmmap: fix only tx timestamp on request
+        */
+       tp_status &= ~(TP_STATUS_TS_SOFTWARE | TP_STATUS_TS_RAW_HARDWARE);
+
+       return tp_status == TP_STATUS_AVAILABLE;
+}
+
 /*
  * Callback to handle sending packets through a real NIC.
  */
@@ -205,7 +229,7 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                }
 
                /* insert vlan info if necessary */
-               if (mbuf->ol_flags & PKT_TX_VLAN_PKT) {
+               if (mbuf->ol_flags & RTE_MBUF_F_TX_VLAN) {
                        if (rte_vlan_insert(&mbuf)) {
                                rte_pktmbuf_free(mbuf);
                                continue;
@@ -213,8 +237,8 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                }
 
                /* point at the next incoming frame */
-               if ((ppd->tp_status != TP_STATUS_AVAILABLE) &&
-                   (poll(&pfd, 1, -1) < 0))
+               if (!tx_ring_status_available(ppd->tp_status) &&
+                   poll(&pfd, 1, -1) < 0)
                        break;
 
                /* copy the tx frame data */
@@ -266,14 +290,14 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 static int
 eth_dev_start(struct rte_eth_dev *dev)
 {
-       dev->data->dev_link.link_status = ETH_LINK_UP;
+       dev->data->dev_link.link_status = RTE_ETH_LINK_UP;
        return 0;
 }
 
 /*
  * This function gets called when the current port gets stopped.
  */
-static void
+static int
 eth_dev_stop(struct rte_eth_dev *dev)
 {
        unsigned i;
@@ -296,12 +320,18 @@ eth_dev_stop(struct rte_eth_dev *dev)
                internals->tx_queue[i].sockfd = -1;
        }
 
-       dev->data->dev_link.link_status = ETH_LINK_DOWN;
+       dev->data->dev_link.link_status = RTE_ETH_LINK_DOWN;
+       return 0;
 }
 
 static int
 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
 {
+       struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
+       const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
+       struct pmd_internals *internals = dev->data->dev_private;
+
+       internals->vlan_strip = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
        return 0;
 }
 
@@ -316,8 +346,9 @@ eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
        dev_info->max_rx_queues = (uint16_t)internals->nb_queues;
        dev_info->max_tx_queues = (uint16_t)internals->nb_queues;
        dev_info->min_rx_bufsize = 0;
-       dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
-               DEV_TX_OFFLOAD_VLAN_INSERT;
+       dev_info->tx_offload_capa = RTE_ETH_TX_OFFLOAD_MULTI_SEGS |
+               RTE_ETH_TX_OFFLOAD_VLAN_INSERT;
+       dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
 
        return 0;
 }
@@ -377,14 +408,34 @@ eth_stats_reset(struct rte_eth_dev *dev)
        return 0;
 }
 
-static void
-eth_dev_close(struct rte_eth_dev *dev __rte_unused)
+static int
+eth_dev_close(struct rte_eth_dev *dev)
 {
-}
+       struct pmd_internals *internals;
+       struct tpacket_req *req;
+       unsigned int q;
 
-static void
-eth_queue_release(void *q __rte_unused)
-{
+       if (rte_eal_process_type() != RTE_PROC_PRIMARY)
+               return 0;
+
+       PMD_LOG(INFO, "Closing AF_PACKET ethdev on NUMA socket %u",
+               rte_socket_id());
+
+       internals = dev->data->dev_private;
+       req = &internals->req;
+       for (q = 0; q < internals->nb_queues; q++) {
+               munmap(internals->rx_queue[q].map,
+                       2 * req->tp_block_size * req->tp_block_nr);
+               rte_free(internals->rx_queue[q].rd);
+               rte_free(internals->tx_queue[q].rd);
+       }
+       free(internals->if_name);
+       rte_free(internals->rx_queue);
+       rte_free(internals->tx_queue);
+
+       /* mac_addrs must not be freed alone because part of dev_private */
+       dev->data->mac_addrs = NULL;
+       return 0;
 }
 
 static int
@@ -423,6 +474,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev,
 
        dev->data->rx_queues[rx_queue_id] = pkt_q;
        pkt_q->in_port = dev->data->port_id;
+       pkt_q->vlan_strip = internals->vlan_strip;
 
        return 0;
 }
@@ -468,6 +520,32 @@ eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
        return 0;
 }
 
+static int
+eth_dev_macaddr_set(struct rte_eth_dev *dev, struct rte_ether_addr *addr)
+{
+       struct pmd_internals *internals = dev->data->dev_private;
+       struct ifreq ifr = { };
+       int sockfd = internals->rx_queue[0].sockfd;
+       int ret;
+
+       if (sockfd == -1) {
+               PMD_LOG(ERR, "receive socket not found");
+               return -EINVAL;
+       }
+
+       strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
+       ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+       memcpy(ifr.ifr_hwaddr.sa_data, addr, sizeof(*addr));
+       ret = ioctl(sockfd, SIOCSIFHWADDR, &ifr);
+
+       if (ret < 0) {
+               PMD_LOG_ERRNO(ERR, "ioctl(SIOCSIFHWADDR) failed");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 static int
 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
 {
@@ -517,13 +595,12 @@ static const struct eth_dev_ops ops = {
        .dev_close = eth_dev_close,
        .dev_configure = eth_dev_configure,
        .dev_infos_get = eth_dev_info,
+       .mac_addr_set = eth_dev_macaddr_set,
        .mtu_set = eth_dev_mtu_set,
        .promiscuous_enable = eth_dev_promiscuous_enable,
        .promiscuous_disable = eth_dev_promiscuous_disable,
        .rx_queue_setup = eth_rx_queue_setup,
        .tx_queue_setup = eth_tx_queue_setup,
-       .rx_queue_release = eth_queue_release,
-       .tx_queue_release = eth_queue_release,
        .link_update = eth_link_update,
        .stats_get = eth_stats_get,
        .stats_reset = eth_stats_reset,
@@ -601,9 +678,24 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
        if (*internals == NULL)
                return -1;
 
+
+       (*internals)->rx_queue = rte_calloc_socket("af_packet_rx",
+                                               nb_queues,
+                                               sizeof(struct pkt_rx_queue),
+                                               0, numa_node);
+       (*internals)->tx_queue = rte_calloc_socket("af_packet_tx",
+                                               nb_queues,
+                                               sizeof(struct pkt_tx_queue),
+                                               0, numa_node);
+       if (!(*internals)->rx_queue || !(*internals)->tx_queue) {
+               goto free_internals;
+       }
+
        for (q = 0; q < nb_queues; q++) {
                (*internals)->rx_queue[q].map = MAP_FAILED;
                (*internals)->tx_queue[q].map = MAP_FAILED;
+               (*internals)->rx_queue[q].sockfd = -1;
+               (*internals)->tx_queue[q].sockfd = -1;
        }
 
        req = &((*internals)->req);
@@ -621,20 +713,20 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                PMD_LOG(ERR,
                        "%s: I/F name too long (%s)",
                        name, pair->value);
-               return -1;
+               goto free_internals;
        }
        if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) {
                PMD_LOG_ERRNO(ERR, "%s: ioctl failed (SIOCGIFINDEX)", name);
-               return -1;
+               goto free_internals;
        }
        (*internals)->if_name = strdup(pair->value);
        if ((*internals)->if_name == NULL)
-               return -1;
+               goto free_internals;
        (*internals)->if_index = ifr.ifr_ifindex;
 
        if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
                PMD_LOG_ERRNO(ERR, "%s: ioctl failed (SIOCGIFHWADDR)", name);
-               return -1;
+               goto free_internals;
        }
        memcpy(&(*internals)->eth_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
 
@@ -658,7 +750,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                        PMD_LOG_ERRNO(ERR,
                                "%s: could not open AF_PACKET socket",
                                name);
-                       return -1;
+                       goto error;
                }
 
                tpver = TPACKET_V2;
@@ -681,18 +773,18 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                        goto error;
                }
 
+               if (qdisc_bypass) {
 #if defined(PACKET_QDISC_BYPASS)
-               rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS,
-                               &qdisc_bypass, sizeof(qdisc_bypass));
-               if (rc == -1) {
-                       PMD_LOG_ERRNO(ERR,
-                               "%s: could not set PACKET_QDISC_BYPASS on AF_PACKET socket for %s",
-                               name, pair->value);
-                       goto error;
-               }
-#else
-               RTE_SET_USED(qdisc_bypass);
+                       rc = setsockopt(qsockfd, SOL_PACKET, PACKET_QDISC_BYPASS,
+                                       &qdisc_bypass, sizeof(qdisc_bypass));
+                       if (rc == -1) {
+                               PMD_LOG_ERRNO(ERR,
+                                       "%s: could not set PACKET_QDISC_BYPASS on AF_PACKET socket for %s",
+                                       name, pair->value);
+                               goto error;
+                       }
 #endif
+               }
 
                rc = setsockopt(qsockfd, SOL_PACKET, PACKET_RX_RING, req, sizeof(*req));
                if (rc == -1) {
@@ -793,6 +885,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
        data->nb_tx_queues = (uint16_t)nb_queues;
        data->dev_link = pmd_link;
        data->mac_addrs = &(*internals)->eth_addr;
+       data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
 
        (*eth_dev)->dev_ops = &ops;
 
@@ -802,15 +895,19 @@ error:
        if (qsockfd != -1)
                close(qsockfd);
        for (q = 0; q < nb_queues; q++) {
-               munmap((*internals)->rx_queue[q].map,
-                      2 * req->tp_block_size * req->tp_block_nr);
+               if ((*internals)->rx_queue[q].map != MAP_FAILED)
+                       munmap((*internals)->rx_queue[q].map,
+                              2 * req->tp_block_size * req->tp_block_nr);
 
                rte_free((*internals)->rx_queue[q].rd);
                rte_free((*internals)->tx_queue[q].rd);
-               if (((*internals)->rx_queue[q].sockfd != 0) &&
+               if (((*internals)->rx_queue[q].sockfd >= 0) &&
                        ((*internals)->rx_queue[q].sockfd != qsockfd))
                        close((*internals)->rx_queue[q].sockfd);
        }
+free_internals:
+       rte_free((*internals)->rx_queue);
+       rte_free((*internals)->tx_queue);
        free((*internals)->if_name);
        rte_free(*internals);
        return -1;
@@ -846,8 +943,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                pair = &kvlist->pairs[k_idx];
                if (strstr(pair->key, ETH_AF_PACKET_NUM_Q_ARG) != NULL) {
                        qpairs = atoi(pair->value);
-                       if (qpairs < 1 ||
-                           qpairs > RTE_PMD_AF_PACKET_MAX_RINGS) {
+                       if (qpairs < 1) {
                                PMD_LOG(ERR,
                                        "%s: invalid qpairs value",
                                        name);
@@ -988,13 +1084,7 @@ exit:
 static int
 rte_pmd_af_packet_remove(struct rte_vdev_device *dev)
 {
-       struct rte_eth_dev *eth_dev = NULL;
-       struct pmd_internals *internals;
-       struct tpacket_req *req;
-       unsigned q;
-
-       PMD_LOG(INFO, "Closing AF_PACKET ethdev on numa socket %u",
-               rte_socket_id());
+       struct rte_eth_dev *eth_dev;
 
        if (dev == NULL)
                return -1;
@@ -1002,24 +1092,9 @@ rte_pmd_af_packet_remove(struct rte_vdev_device *dev)
        /* find the ethdev entry */
        eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
        if (eth_dev == NULL)
-               return -1;
-
-       /* mac_addrs must not be freed alone because part of dev_private */
-       eth_dev->data->mac_addrs = NULL;
-
-       if (rte_eal_process_type() != RTE_PROC_PRIMARY)
-               return rte_eth_dev_release_port(eth_dev);
-
-       internals = eth_dev->data->dev_private;
-       req = &internals->req;
-       for (q = 0; q < internals->nb_queues; q++) {
-               munmap(internals->rx_queue[q].map,
-                       2 * req->tp_block_size * req->tp_block_nr);
-               rte_free(internals->rx_queue[q].rd);
-               rte_free(internals->tx_queue[q].rd);
-       }
-       free(internals->if_name);
+               return 0; /* port already released */
 
+       eth_dev_close(eth_dev);
        rte_eth_dev_release_port(eth_dev);
 
        return 0;
@@ -1039,10 +1114,3 @@ RTE_PMD_REGISTER_PARAM_STRING(net_af_packet,
        "framesz=<int> "
        "framecnt=<int> "
        "qdisc_bypass=<0|1>");
-
-RTE_INIT(af_packet_init_log)
-{
-       af_packet_logtype = rte_log_register("pmd.net.packet");
-       if (af_packet_logtype >= 0)
-               rte_log_set_level(af_packet_logtype, RTE_LOG_NOTICE);
-}