net/af_packet: prefer snprintf against strncpy
[dpdk.git] / drivers / net / af_packet / rte_eth_af_packet.c
index 2b01cb0..68de45c 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <rte_mbuf.h>
 #include <rte_ethdev.h>
+#include <rte_ethdev_vdev.h>
 #include <rte_malloc.h>
 #include <rte_kvargs.h>
 #include <rte_vdev.h>
@@ -161,6 +162,12 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                pbuf = (uint8_t *) ppd + ppd->tp_mac;
                memcpy(rte_pktmbuf_mtod(mbuf, void *), pbuf, rte_pktmbuf_data_len(mbuf));
 
+               /* 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 | PKT_RX_VLAN_STRIPPED);
+               }
+
                /* release incoming frame and advance ring buffer */
                ppd->tp_status = TP_STATUS_KERNEL;
                if (++framenum >= framecount)
@@ -214,6 +221,14 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                        continue;
                }
 
+               /* insert vlan info if necessary */
+               if (mbuf->ol_flags & PKT_TX_VLAN_PKT) {
+                       if (rte_vlan_insert(&mbuf)) {
+                               rte_pktmbuf_free(mbuf);
+                               continue;
+                       }
+               }
+
                /* point at the next incoming frame */
                if ((ppd->tp_status != TP_STATUS_AVAILABLE) &&
                    (poll(&pfd, 1, -1) < 0))
@@ -419,12 +434,80 @@ eth_tx_queue_setup(struct rte_eth_dev *dev,
        return 0;
 }
 
+static int
+eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
+{
+       struct pmd_internals *internals = dev->data->dev_private;
+       struct ifreq ifr = { .ifr_mtu = mtu };
+       int ret;
+       int s;
+       unsigned int data_size = internals->req.tp_frame_size -
+                                TPACKET2_HDRLEN -
+                                sizeof(struct sockaddr_ll);
+
+       if (mtu > data_size)
+               return -EINVAL;
+
+       s = socket(PF_INET, SOCK_DGRAM, 0);
+       if (s < 0)
+               return -EINVAL;
+
+       snprintf(ifr.ifr_name, IFNAMSIZ, "%s", internals->if_name);
+       ret = ioctl(s, SIOCSIFMTU, &ifr);
+       close(s);
+
+       if (ret < 0)
+               return -EINVAL;
+
+       return 0;
+}
+
+static void
+eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
+{
+       struct ifreq ifr;
+       int s;
+
+       s = socket(PF_INET, SOCK_DGRAM, 0);
+       if (s < 0)
+               return;
+
+       snprintf(ifr.ifr_name, IFNAMSIZ, "%s", if_name);
+       if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
+               goto out;
+       ifr.ifr_flags &= mask;
+       ifr.ifr_flags |= flags;
+       if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
+               goto out;
+out:
+       close(s);
+}
+
+static void
+eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
+{
+       struct pmd_internals *internals = dev->data->dev_private;
+
+       eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
+}
+
+static void
+eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
+{
+       struct pmd_internals *internals = dev->data->dev_private;
+
+       eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
+}
+
 static const struct eth_dev_ops ops = {
        .dev_start = eth_dev_start,
        .dev_stop = eth_dev_stop,
        .dev_close = eth_dev_close,
        .dev_configure = eth_dev_configure,
        .dev_infos_get = eth_dev_info,
+       .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,
@@ -457,18 +540,19 @@ open_packet_iface(const char *key __rte_unused,
 static struct rte_vdev_driver pmd_af_packet_drv;
 
 static int
-rte_pmd_init_internals(const char *name,
+rte_pmd_init_internals(struct rte_vdev_device *dev,
                        const int sockfd,
                        const unsigned nb_queues,
                        unsigned int blocksize,
                        unsigned int blockcnt,
                        unsigned int framesize,
                        unsigned int framecnt,
-                       const unsigned numa_node,
                        struct pmd_internals **internals,
                        struct rte_eth_dev **eth_dev,
                        struct rte_kvargs *kvlist)
 {
+       const char *name = rte_vdev_device_name(dev);
+       const unsigned int numa_node = dev->device.numa_node;
        struct rte_eth_dev_data *data = NULL;
        struct rte_kvargs_pair *pair = NULL;
        struct ifreq ifr;
@@ -686,7 +770,7 @@ rte_pmd_init_internals(const char *name,
        }
 
        /* reserve an ethdev entry */
-       *eth_dev = rte_eth_dev_allocate(name);
+       *eth_dev = rte_eth_vdev_allocate(dev, 0);
        if (*eth_dev == NULL)
                goto error;
 
@@ -700,22 +784,16 @@ rte_pmd_init_internals(const char *name,
 
        (*internals)->nb_queues = nb_queues;
 
+       rte_memcpy(data, (*eth_dev)->data, sizeof(*data));
        data->dev_private = *internals;
-       data->port_id = (*eth_dev)->data->port_id;
        data->nb_rx_queues = (uint16_t)nb_queues;
        data->nb_tx_queues = (uint16_t)nb_queues;
        data->dev_link = pmd_link;
        data->mac_addrs = &(*internals)->eth_addr;
-       strncpy(data->name,
-               (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
 
        (*eth_dev)->data = data;
        (*eth_dev)->dev_ops = &ops;
-       (*eth_dev)->driver = NULL;
        (*eth_dev)->data->dev_flags = RTE_ETH_DEV_DETACHABLE;
-       (*eth_dev)->data->drv_name = pmd_af_packet_drv.driver.name;
-       (*eth_dev)->data->kdrv = RTE_KDRV_NONE;
-       (*eth_dev)->data->numa_node = numa_node;
 
        return 0;
 
@@ -740,11 +818,11 @@ error_early:
 }
 
 static int
-rte_eth_from_packet(const char *name,
+rte_eth_from_packet(struct rte_vdev_device *dev,
                     int const *sockfd,
-                    const unsigned numa_node,
                     struct rte_kvargs *kvlist)
 {
+       const char *name = rte_vdev_device_name(dev);
        struct pmd_internals *internals = NULL;
        struct rte_eth_dev *eth_dev = NULL;
        struct rte_kvargs_pair *pair = NULL;
@@ -827,11 +905,11 @@ rte_eth_from_packet(const char *name,
        RTE_LOG(INFO, PMD, "%s:\tframe size %d\n", name, framesize);
        RTE_LOG(INFO, PMD, "%s:\tframe count %d\n", name, framecount);
 
-       if (rte_pmd_init_internals(name, *sockfd, qpairs,
-                                  blocksize, blockcount,
-                                  framesize, framecount,
-                                  numa_node, &internals, &eth_dev,
-                                  kvlist) < 0)
+       if (rte_pmd_init_internals(dev, *sockfd, qpairs,
+                                  blocksize, blockcount,
+                                  framesize, framecount,
+                                  &internals, &eth_dev,
+                                  kvlist) < 0)
                return -1;
 
        eth_dev->rx_pkt_burst = eth_af_packet_rx;
@@ -841,18 +919,16 @@ rte_eth_from_packet(const char *name,
 }
 
 static int
-rte_pmd_af_packet_probe(const char *name, const char *params)
+rte_pmd_af_packet_probe(struct rte_vdev_device *dev)
 {
-       unsigned numa_node;
        int ret = 0;
        struct rte_kvargs *kvlist;
        int sockfd = -1;
 
-       RTE_LOG(INFO, PMD, "Initializing pmd_af_packet for %s\n", name);
+       RTE_LOG(INFO, PMD, "Initializing pmd_af_packet for %s\n",
+               rte_vdev_device_name(dev));
 
-       numa_node = rte_socket_id();
-
-       kvlist = rte_kvargs_parse(params, valid_arguments);
+       kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
        if (kvlist == NULL) {
                ret = -1;
                goto exit;
@@ -870,7 +946,10 @@ rte_pmd_af_packet_probe(const char *name, const char *params)
                        goto exit;
        }
 
-       ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
+       if (dev->device.numa_node == SOCKET_ID_ANY)
+               dev->device.numa_node = rte_socket_id();
+
+       ret = rte_eth_from_packet(dev, &sockfd, kvlist);
        close(sockfd); /* no longer needed */
 
 exit:
@@ -879,7 +958,7 @@ exit:
 }
 
 static int
-rte_pmd_af_packet_remove(const char *name)
+rte_pmd_af_packet_remove(struct rte_vdev_device *dev)
 {
        struct rte_eth_dev *eth_dev = NULL;
        struct pmd_internals *internals;
@@ -888,11 +967,11 @@ rte_pmd_af_packet_remove(const char *name)
        RTE_LOG(INFO, PMD, "Closing AF_PACKET ethdev on numa socket %u\n",
                        rte_socket_id());
 
-       if (name == NULL)
+       if (dev == NULL)
                return -1;
 
        /* find the ethdev entry */
-       eth_dev = rte_eth_dev_allocated(name);
+       eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
        if (eth_dev == NULL)
                return -1;