net/pcap: use a struct to pass user options
[dpdk.git] / drivers / net / pcap / rte_eth_pcap.c
index 9fd9327..764e599 100644 (file)
@@ -11,7 +11,7 @@
 #include <sys/ioctl.h>
 #include <unistd.h>
 
-#if defined(RTE_EXEC_ENV_BSDAPP)
+#if defined(RTE_EXEC_ENV_FREEBSD)
 #include <sys/sysctl.h>
 #include <net/if_dl.h>
 #endif
@@ -28,7 +28,7 @@
 #include <rte_string_fns.h>
 
 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
-#define RTE_ETH_PCAP_SNAPLEN ETHER_MAX_JUMBO_FRAME_LEN
+#define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN
 #define RTE_ETH_PCAP_PROMISC 1
 #define RTE_ETH_PCAP_TIMEOUT -1
 
@@ -78,7 +78,7 @@ struct pmd_internals {
        struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
        struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
        char devargs[ETH_PCAP_ARG_MAXLEN];
-       struct ether_addr eth_addr;
+       struct rte_ether_addr eth_addr;
        int if_index;
        int single_iface;
        int phy_mac;
@@ -101,6 +101,14 @@ struct pmd_devargs {
        int phy_mac;
 };
 
+struct pmd_devargs_all {
+       struct pmd_devargs rx_queues;
+       struct pmd_devargs tx_queues;
+       int single_iface;
+       unsigned int is_tx_pcap;
+       unsigned int is_tx_iface;
+};
+
 static const char *valid_arguments[] = {
        ETH_PCAP_RX_PCAP_ARG,
        ETH_PCAP_TX_PCAP_ARG,
@@ -287,7 +295,7 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                        pcap_dump((u_char *)dumper, &header,
                                  rte_pktmbuf_mtod(mbuf, void*));
                } else {
-                       if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
+                       if (mbuf->pkt_len <= RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
                                eth_pcap_gather_data(tx_pcap_data, mbuf);
                                pcap_dump((u_char *)dumper, &header,
                                          tx_pcap_data);
@@ -295,7 +303,7 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                                PMD_LOG(ERR,
                                        "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
                                        mbuf->pkt_len,
-                                       ETHER_MAX_JUMBO_FRAME_LEN);
+                                       RTE_ETHER_MAX_JUMBO_FRAME_LEN);
 
                                rte_pktmbuf_free(mbuf);
                                break;
@@ -349,7 +357,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                                        rte_pktmbuf_mtod(mbuf, u_char *),
                                        mbuf->pkt_len);
                } else {
-                       if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
+                       if (mbuf->pkt_len <= RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
                                eth_pcap_gather_data(tx_pcap_data, mbuf);
                                ret = pcap_sendpacket(pcap,
                                                tx_pcap_data, mbuf->pkt_len);
@@ -357,7 +365,7 @@ eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
                                PMD_LOG(ERR,
                                        "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
                                        mbuf->pkt_len,
-                                       ETHER_MAX_JUMBO_FRAME_LEN);
+                                       RTE_ETHER_MAX_JUMBO_FRAME_LEN);
 
                                rte_pktmbuf_free(mbuf);
                                break;
@@ -605,10 +613,9 @@ eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
                        i < dev->data->nb_tx_queues; i++) {
                stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
                stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
-               stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
                tx_packets_total += stats->q_opackets[i];
                tx_bytes_total += stats->q_obytes[i];
-               tx_packets_err_total += stats->q_errors[i];
+               tx_packets_err_total += internal->tx_queue[i].tx_stat.err_pkts;
        }
 
        stats->ipackets = rx_packets_total;
@@ -953,7 +960,7 @@ pmd_init_internals(struct rte_vdev_device *vdev,
         * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
         * where the middle 4 characters are converted to hex.
         */
-       (*internals)->eth_addr = (struct ether_addr) {
+       (*internals)->eth_addr = (struct rte_ether_addr) {
                .addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
        };
        (*internals)->phy_mac = 0;
@@ -979,7 +986,7 @@ static int
 eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
                const unsigned int numa_node)
 {
-#if defined(RTE_EXEC_ENV_LINUXAPP)
+#if defined(RTE_EXEC_ENV_LINUX)
        void *mac_addrs;
        struct ifreq ifr;
        int if_fd = socket(AF_INET, SOCK_DGRAM, 0);
@@ -993,7 +1000,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
                return -1;
        }
 
-       mac_addrs = rte_zmalloc_socket(NULL, ETHER_ADDR_LEN, 0, numa_node);
+       mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
        if (!mac_addrs) {
                close(if_fd);
                return -1;
@@ -1002,13 +1009,13 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
        PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
        eth_dev->data->mac_addrs = mac_addrs;
        rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
-                       ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
+                       ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
 
        close(if_fd);
 
        return 0;
 
-#elif defined(RTE_EXEC_ENV_BSDAPP)
+#elif defined(RTE_EXEC_ENV_FREEBSD)
        void *mac_addrs;
        struct if_msghdr *ifm;
        struct sockaddr_dl *sdl;
@@ -1040,7 +1047,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
        ifm = (struct if_msghdr *)buf;
        sdl = (struct sockaddr_dl *)(ifm + 1);
 
-       mac_addrs = rte_zmalloc_socket(NULL, ETHER_ADDR_LEN, 0, numa_node);
+       mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
        if (!mac_addrs) {
                rte_free(buf);
                return -1;
@@ -1049,7 +1056,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
        PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
        eth_dev->data->mac_addrs = mac_addrs;
        rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
-                       LLADDR(sdl), ETHER_ADDR_LEN);
+                       LLADDR(sdl), RTE_ETHER_ADDR_LEN);
 
        rte_free(buf);
 
@@ -1061,11 +1068,14 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
 
 static int
 eth_from_pcaps_common(struct rte_vdev_device *vdev,
-               struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
-               struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
+               struct pmd_devargs_all *devargs_all,
                struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
 {
        struct pmd_process_private *pp;
+       struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
+       struct pmd_devargs *tx_queues = &devargs_all->tx_queues;
+       const unsigned int nb_rx_queues = rx_queues->num_of_queue;
+       const unsigned int nb_tx_queues = tx_queues->num_of_queue;
        unsigned int i;
 
        /* do some parameter checking */
@@ -1084,8 +1094,8 @@ eth_from_pcaps_common(struct rte_vdev_device *vdev,
                struct devargs_queue *queue = &rx_queues->queue[i];
 
                pp->rx_pcap[i] = queue->pcap;
-               snprintf(rx->name, sizeof(rx->name), "%s", queue->name);
-               snprintf(rx->type, sizeof(rx->type), "%s", queue->type);
+               strlcpy(rx->name, queue->name, sizeof(rx->name));
+               strlcpy(rx->type, queue->type, sizeof(rx->type));
        }
 
        for (i = 0; i < nb_tx_queues; i++) {
@@ -1094,8 +1104,8 @@ eth_from_pcaps_common(struct rte_vdev_device *vdev,
 
                pp->tx_dumper[i] = queue->dumper;
                pp->tx_pcap[i] = queue->pcap;
-               snprintf(tx->name, sizeof(tx->name), "%s", queue->name);
-               snprintf(tx->type, sizeof(tx->type), "%s", queue->type);
+               strlcpy(tx->name, queue->name, sizeof(tx->name));
+               strlcpy(tx->type, queue->type, sizeof(tx->type));
        }
 
        return 0;
@@ -1103,16 +1113,15 @@ eth_from_pcaps_common(struct rte_vdev_device *vdev,
 
 static int
 eth_from_pcaps(struct rte_vdev_device *vdev,
-               struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
-               struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
-               int single_iface, unsigned int using_dumpers)
+               struct pmd_devargs_all *devargs_all)
 {
        struct pmd_internals *internals = NULL;
        struct rte_eth_dev *eth_dev = NULL;
+       struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
+       int single_iface = devargs_all->single_iface;
        int ret;
 
-       ret = eth_from_pcaps_common(vdev, rx_queues, nb_rx_queues,
-               tx_queues, nb_tx_queues, &internals, &eth_dev);
+       ret = eth_from_pcaps_common(vdev, devargs_all, &internals, &eth_dev);
 
        if (ret < 0)
                return ret;
@@ -1134,7 +1143,8 @@ eth_from_pcaps(struct rte_vdev_device *vdev,
 
        eth_dev->rx_pkt_burst = eth_pcap_rx;
 
-       if (using_dumpers)
+       /* Assign tx ops. */
+       if (devargs_all->is_tx_pcap)
                eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
        else
                eth_dev->tx_pkt_burst = eth_pcap_tx;
@@ -1147,15 +1157,20 @@ static int
 pmd_pcap_probe(struct rte_vdev_device *dev)
 {
        const char *name;
-       unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
+       unsigned int is_rx_pcap = 0;
        struct rte_kvargs *kvlist;
        struct pmd_devargs pcaps = {0};
        struct pmd_devargs dumpers = {0};
        struct rte_eth_dev *eth_dev =  NULL;
        struct pmd_internals *internal;
-       int single_iface = 0;
        int ret;
 
+       struct pmd_devargs_all devargs_all = {
+               .single_iface = 0,
+               .is_tx_pcap = 0,
+               .is_tx_iface = 0,
+       };
+
        name = rte_vdev_device_name(dev);
        PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
 
@@ -1202,7 +1217,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev)
 
                dumpers.phy_mac = pcaps.phy_mac;
 
-               single_iface = 1;
+               devargs_all.single_iface = 1;
                pcaps.num_of_queue = 1;
                dumpers.num_of_queue = 1;
 
@@ -1231,10 +1246,11 @@ pmd_pcap_probe(struct rte_vdev_device *dev)
         * We check whether we want to open a TX stream to a real NIC or a
         * pcap file
         */
-       is_tx_pcap = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
+       devargs_all.is_tx_pcap =
+               rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
        dumpers.num_of_queue = 0;
 
-       if (is_tx_pcap)
+       if (devargs_all.is_tx_pcap)
                ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
                                &open_tx_pcap, &dumpers);
        else
@@ -1258,7 +1274,8 @@ create_eth:
                if (pp == NULL) {
                        PMD_LOG(ERR,
                                "Failed to allocate memory for process private");
-                       return -1;
+                       ret = -1;
+                       goto free_kvlist;
                }
 
                eth_dev->dev_ops = &ops;
@@ -1275,17 +1292,19 @@ create_eth:
 
                eth_dev->process_private = pp;
                eth_dev->rx_pkt_burst = eth_pcap_rx;
-               if (is_tx_pcap)
+               if (devargs_all.is_tx_pcap)
                        eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
                else
                        eth_dev->tx_pkt_burst = eth_pcap_tx;
 
                rte_eth_dev_probing_finish(eth_dev);
-               return 0;
+               goto free_kvlist;
        }
 
-       ret = eth_from_pcaps(dev, &pcaps, pcaps.num_of_queue, &dumpers,
-               dumpers.num_of_queue, single_iface, is_tx_pcap);
+       devargs_all.rx_queues = pcaps;
+       devargs_all.tx_queues = dumpers;
+
+       ret = eth_from_pcaps(dev, &devargs_all);
 
 free_kvlist:
        rte_kvargs_free(kvlist);