examples/ioat: add option to control stats print interval
[dpdk.git] / examples / ioat / ioatfwd.c
index 2fc0b11..995ba7b 100644 (file)
@@ -24,6 +24,9 @@
 #define CMD_LINE_OPT_NB_QUEUE "nb-queue"
 #define CMD_LINE_OPT_COPY_TYPE "copy-type"
 #define CMD_LINE_OPT_RING_SIZE "ring-size"
+#define CMD_LINE_OPT_BATCH_SIZE "dma-batch-size"
+#define CMD_LINE_OPT_FRAME_SIZE "max-frame-size"
+#define CMD_LINE_OPT_STATS_INTERVAL "stats-interval"
 
 /* configurable number of RX/TX ring descriptors */
 #define RX_DEFAULT_RINGSIZE 1024
@@ -42,11 +45,30 @@ struct rxtx_port_config {
        uint16_t ioat_ids[MAX_RX_QUEUES_COUNT];
 };
 
+/* Configuring ports and number of assigned lcores in struct. 8< */
 struct rxtx_transmission_config {
        struct rxtx_port_config ports[RTE_MAX_ETHPORTS];
        uint16_t nb_ports;
        uint16_t nb_lcores;
 };
+/* >8 End of configuration of ports and number of assigned lcores. */
+
+/* per-port statistics struct */
+struct ioat_port_statistics {
+       uint64_t rx[RTE_MAX_ETHPORTS];
+       uint64_t tx[RTE_MAX_ETHPORTS];
+       uint64_t tx_dropped[RTE_MAX_ETHPORTS];
+       uint64_t copy_dropped[RTE_MAX_ETHPORTS];
+};
+struct ioat_port_statistics port_statistics;
+
+struct total_statistics {
+       uint64_t total_packets_dropped;
+       uint64_t total_packets_tx;
+       uint64_t total_packets_rx;
+       uint64_t total_successful_enqueues;
+       uint64_t total_failed_enqueues;
+};
 
 typedef enum copy_mode_t {
 #define COPY_MODE_SW "sw"
@@ -74,6 +96,9 @@ static copy_mode_t copy_mode = COPY_MODE_IOAT_NUM;
  */
 static unsigned short ring_size = 2048;
 
+/* interval, in seconds, between stats prints */
+static unsigned short stats_interval = 1;
+
 /* global transmission config */
 struct rxtx_transmission_config cfg;
 
@@ -83,12 +108,218 @@ static uint16_t nb_txd = TX_DEFAULT_RINGSIZE;
 
 static volatile bool force_quit;
 
+static uint32_t ioat_batch_sz = MAX_PKT_BURST;
+static uint32_t max_frame_size = RTE_ETHER_MAX_LEN;
+
 /* ethernet addresses of ports */
 static struct rte_ether_addr ioat_ports_eth_addr[RTE_MAX_ETHPORTS];
 
 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
 struct rte_mempool *ioat_pktmbuf_pool;
 
+/* Print out statistics for one port. */
+static void
+print_port_stats(uint16_t port_id)
+{
+       printf("\nStatistics for port %u ------------------------------"
+               "\nPackets sent: %34"PRIu64
+               "\nPackets received: %30"PRIu64
+               "\nPackets dropped on tx: %25"PRIu64
+               "\nPackets dropped on copy: %23"PRIu64,
+               port_id,
+               port_statistics.tx[port_id],
+               port_statistics.rx[port_id],
+               port_statistics.tx_dropped[port_id],
+               port_statistics.copy_dropped[port_id]);
+}
+
+/* Print out statistics for one IOAT rawdev device. */
+static void
+print_rawdev_stats(uint32_t dev_id, uint64_t *xstats,
+       unsigned int *ids_xstats, uint16_t nb_xstats,
+       struct rte_rawdev_xstats_name *names_xstats)
+{
+       uint16_t i;
+
+       printf("\nIOAT channel %u", dev_id);
+       for (i = 0; i < nb_xstats; i++)
+               printf("\n\t %s: %*"PRIu64,
+                       names_xstats[ids_xstats[i]].name,
+                       (int)(37 - strlen(names_xstats[ids_xstats[i]].name)),
+                       xstats[i]);
+}
+
+static void
+print_total_stats(struct total_statistics *ts)
+{
+       printf("\nAggregate statistics ==============================="
+               "\nTotal packets Tx: %24"PRIu64" [pps]"
+               "\nTotal packets Rx: %24"PRIu64" [pps]"
+               "\nTotal packets dropped: %19"PRIu64" [pps]",
+               ts->total_packets_tx / stats_interval,
+               ts->total_packets_rx / stats_interval,
+               ts->total_packets_dropped / stats_interval);
+
+       if (copy_mode == COPY_MODE_IOAT_NUM) {
+               printf("\nTotal IOAT successful enqueues: %8"PRIu64" [enq/s]"
+                       "\nTotal IOAT failed enqueues: %12"PRIu64" [enq/s]",
+                       ts->total_successful_enqueues / stats_interval,
+                       ts->total_failed_enqueues / stats_interval);
+       }
+
+       printf("\n====================================================\n");
+}
+
+/* Print out statistics on packets dropped. */
+static void
+print_stats(char *prgname)
+{
+       struct total_statistics ts, delta_ts;
+       uint32_t i, port_id, dev_id;
+       struct rte_rawdev_xstats_name *names_xstats;
+       uint64_t *xstats;
+       unsigned int *ids_xstats, nb_xstats;
+       char status_string[255]; /* to print at the top of the output */
+       int status_strlen;
+       int ret;
+
+       const char clr[] = { 27, '[', '2', 'J', '\0' };
+       const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
+
+       status_strlen = snprintf(status_string, sizeof(status_string),
+               "%s, ", prgname);
+       status_strlen += snprintf(status_string + status_strlen,
+               sizeof(status_string) - status_strlen,
+               "Worker Threads = %d, ",
+               rte_lcore_count() > 2 ? 2 : 1);
+       status_strlen += snprintf(status_string + status_strlen,
+               sizeof(status_string) - status_strlen,
+               "Copy Mode = %s,\n", copy_mode == COPY_MODE_SW_NUM ?
+               COPY_MODE_SW : COPY_MODE_IOAT);
+       status_strlen += snprintf(status_string + status_strlen,
+               sizeof(status_string) - status_strlen,
+               "Updating MAC = %s, ", mac_updating ?
+               "enabled" : "disabled");
+       status_strlen += snprintf(status_string + status_strlen,
+               sizeof(status_string) - status_strlen,
+               "Rx Queues = %d, ", nb_queues);
+       status_strlen += snprintf(status_string + status_strlen,
+               sizeof(status_string) - status_strlen,
+               "Ring Size = %d", ring_size);
+
+       /* Allocate memory for xstats names and values */
+       ret = rte_rawdev_xstats_names_get(
+               cfg.ports[0].ioat_ids[0], NULL, 0);
+       if (ret < 0)
+               return;
+       nb_xstats = (unsigned int)ret;
+
+       names_xstats = malloc(sizeof(*names_xstats) * nb_xstats);
+       if (names_xstats == NULL) {
+               rte_exit(EXIT_FAILURE,
+                       "Error allocating xstat names memory\n");
+       }
+       rte_rawdev_xstats_names_get(cfg.ports[0].ioat_ids[0],
+               names_xstats, nb_xstats);
+
+       ids_xstats = malloc(sizeof(*ids_xstats) * 2);
+       if (ids_xstats == NULL) {
+               rte_exit(EXIT_FAILURE,
+                       "Error allocating xstat ids_xstats memory\n");
+       }
+
+       xstats = malloc(sizeof(*xstats) * 2);
+       if (xstats == NULL) {
+               rte_exit(EXIT_FAILURE,
+                       "Error allocating xstat memory\n");
+       }
+
+       /* Get failed/successful enqueues stats index */
+       ids_xstats[0] = ids_xstats[1] = nb_xstats;
+       for (i = 0; i < nb_xstats; i++) {
+               if (!strcmp(names_xstats[i].name, "failed_enqueues"))
+                       ids_xstats[0] = i;
+               else if (!strcmp(names_xstats[i].name, "successful_enqueues"))
+                       ids_xstats[1] = i;
+               if (ids_xstats[0] < nb_xstats && ids_xstats[1] < nb_xstats)
+                       break;
+       }
+       if (ids_xstats[0] == nb_xstats || ids_xstats[1] == nb_xstats) {
+               rte_exit(EXIT_FAILURE,
+                       "Error getting failed/successful enqueues stats index\n");
+       }
+
+       memset(&ts, 0, sizeof(struct total_statistics));
+
+       while (!force_quit) {
+               /* Sleep for "stats_interval" seconds each round - init sleep allows reading
+                * messages from app startup.
+                */
+               sleep(stats_interval);
+
+               /* Clear screen and move to top left */
+               printf("%s%s", clr, topLeft);
+
+               memset(&delta_ts, 0, sizeof(struct total_statistics));
+
+               printf("%s\n", status_string);
+
+               for (i = 0; i < cfg.nb_ports; i++) {
+                       port_id = cfg.ports[i].rxtx_port;
+                       print_port_stats(port_id);
+
+                       delta_ts.total_packets_dropped +=
+                               port_statistics.tx_dropped[port_id]
+                               + port_statistics.copy_dropped[port_id];
+                       delta_ts.total_packets_tx +=
+                               port_statistics.tx[port_id];
+                       delta_ts.total_packets_rx +=
+                               port_statistics.rx[port_id];
+
+                       if (copy_mode == COPY_MODE_IOAT_NUM) {
+                               uint32_t j;
+
+                               for (j = 0; j < cfg.ports[i].nb_queues; j++) {
+                                       dev_id = cfg.ports[i].ioat_ids[j];
+                                       rte_rawdev_xstats_get(dev_id,
+                                               ids_xstats, xstats, 2);
+
+                                       print_rawdev_stats(dev_id, xstats,
+                                               ids_xstats, 2, names_xstats);
+
+                                       delta_ts.total_failed_enqueues +=
+                                               xstats[ids_xstats[0]];
+                                       delta_ts.total_successful_enqueues +=
+                                               xstats[ids_xstats[1]];
+                               }
+                       }
+               }
+
+               delta_ts.total_packets_tx -= ts.total_packets_tx;
+               delta_ts.total_packets_rx -= ts.total_packets_rx;
+               delta_ts.total_packets_dropped -= ts.total_packets_dropped;
+               delta_ts.total_failed_enqueues -= ts.total_failed_enqueues;
+               delta_ts.total_successful_enqueues -=
+                       ts.total_successful_enqueues;
+
+               printf("\n");
+               print_total_stats(&delta_ts);
+
+               fflush(stdout);
+
+               ts.total_packets_tx += delta_ts.total_packets_tx;
+               ts.total_packets_rx += delta_ts.total_packets_rx;
+               ts.total_packets_dropped += delta_ts.total_packets_dropped;
+               ts.total_failed_enqueues += delta_ts.total_failed_enqueues;
+               ts.total_successful_enqueues +=
+                       delta_ts.total_successful_enqueues;
+       }
+
+       free(names_xstats);
+       free(xstats);
+       free(ids_xstats);
+}
+
 static void
 update_mac_addrs(struct rte_mbuf *m, uint32_t dest_portid)
 {
@@ -100,76 +331,107 @@ update_mac_addrs(struct rte_mbuf *m, uint32_t dest_portid)
        /* 02:00:00:00:00:xx - overwriting 2 bytes of source address but
         * it's acceptable cause it gets overwritten by rte_ether_addr_copy
         */
-       tmp = &eth->d_addr.addr_bytes[0];
+       tmp = &eth->dst_addr.addr_bytes[0];
        *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
 
        /* src addr */
-       rte_ether_addr_copy(&ioat_ports_eth_addr[dest_portid], &eth->s_addr);
+       rte_ether_addr_copy(&ioat_ports_eth_addr[dest_portid], &eth->src_addr);
 }
 
+/* Perform packet copy there is a user-defined function. 8< */
 static inline void
-pktmbuf_sw_copy(struct rte_mbuf *src, struct rte_mbuf *dst)
+pktmbuf_metadata_copy(const struct rte_mbuf *src, struct rte_mbuf *dst)
 {
-       /* Copy packet metadata */
-       rte_memcpy(&dst->rearm_data,
-               &src->rearm_data,
-               offsetof(struct rte_mbuf, cacheline1)
-               - offsetof(struct rte_mbuf, rearm_data));
+       dst->data_off = src->data_off;
+       memcpy(&dst->rx_descriptor_fields1, &src->rx_descriptor_fields1,
+               offsetof(struct rte_mbuf, buf_len) -
+               offsetof(struct rte_mbuf, rx_descriptor_fields1));
+}
 
-       /* Copy packet data */
+/* Copy packet data */
+static inline void
+pktmbuf_sw_copy(struct rte_mbuf *src, struct rte_mbuf *dst)
+{
        rte_memcpy(rte_pktmbuf_mtod(dst, char *),
                rte_pktmbuf_mtod(src, char *), src->data_len);
 }
+/* >8 End of perform packet copy there is a user-defined function. */
 
 static uint32_t
-ioat_enqueue_packets(struct rte_mbuf **pkts,
+ioat_enqueue_packets(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
        uint32_t nb_rx, uint16_t dev_id)
 {
        int ret;
        uint32_t i;
-       struct rte_mbuf *pkts_copy[MAX_PKT_BURST];
-
-       const uint64_t addr_offset = RTE_PTR_DIFF(pkts[0]->buf_addr,
-               &pkts[0]->rearm_data);
-
-       ret = rte_mempool_get_bulk(ioat_pktmbuf_pool,
-               (void *)pkts_copy, nb_rx);
-
-       if (unlikely(ret < 0))
-               rte_exit(EXIT_FAILURE, "Unable to allocate memory.\n");
 
        for (i = 0; i < nb_rx; i++) {
                /* Perform data copy */
                ret = rte_ioat_enqueue_copy(dev_id,
-                       pkts[i]->buf_iova
-                       - addr_offset,
-                       pkts_copy[i]->buf_iova
-                       - addr_offset,
-                       rte_pktmbuf_data_len(pkts[i])
-                       + addr_offset,
+                       rte_pktmbuf_iova(pkts[i]),
+                       rte_pktmbuf_iova(pkts_copy[i]),
+                       rte_pktmbuf_data_len(pkts[i]),
                        (uintptr_t)pkts[i],
-                       (uintptr_t)pkts_copy[i],
-                       0 /* nofence */);
+                       (uintptr_t)pkts_copy[i]);
 
                if (ret != 1)
                        break;
        }
 
        ret = i;
-       /* Free any not enqueued packets. */
-       rte_mempool_put_bulk(ioat_pktmbuf_pool, (void *)&pkts[i], nb_rx - i);
-       rte_mempool_put_bulk(ioat_pktmbuf_pool, (void *)&pkts_copy[i],
-               nb_rx - i);
-
        return ret;
 }
 
-/* Receive packets on one port and enqueue to IOAT rawdev or rte_ring. */
+static inline uint32_t
+ioat_enqueue(struct rte_mbuf *pkts[], struct rte_mbuf *pkts_copy[],
+               uint32_t num, uint32_t step, uint16_t dev_id)
+{
+       uint32_t i, k, m, n;
+
+       k = 0;
+       for (i = 0; i < num; i += m) {
+
+               m = RTE_MIN(step, num - i);
+               n = ioat_enqueue_packets(pkts + i, pkts_copy + i, m, dev_id);
+               k += n;
+               if (n > 0)
+                       rte_ioat_perform_ops(dev_id);
+
+               /* don't try to enqueue more if HW queue is full */
+               if (n != m)
+                       break;
+       }
+
+       return k;
+}
+
+static inline uint32_t
+ioat_dequeue(struct rte_mbuf *src[], struct rte_mbuf *dst[], uint32_t num,
+       uint16_t dev_id)
+{
+       int32_t rc;
+       /* Dequeue the mbufs from IOAT device. Since all memory
+        * is DPDK pinned memory and therefore all addresses should
+        * be valid, we don't check for copy errors
+        */
+       rc = rte_ioat_completed_ops(dev_id, num, NULL, NULL,
+               (void *)src, (void *)dst);
+       if (rc < 0) {
+               RTE_LOG(CRIT, IOAT,
+                       "rte_ioat_completed_ops(%hu) failedi, error: %d\n",
+                       dev_id, rte_errno);
+               rc = 0;
+       }
+       return rc;
+}
+
+/* Receive packets on one port and enqueue to IOAT rawdev or rte_ring. 8< */
 static void
 ioat_rx_port(struct rxtx_port_config *rx_config)
 {
+       int32_t ret;
        uint32_t nb_rx, nb_enq, i, j;
        struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
+       struct rte_mbuf *pkts_burst_copy[MAX_PKT_BURST];
 
        for (i = 0; i < rx_config->nb_queues; i++) {
 
@@ -179,89 +441,97 @@ ioat_rx_port(struct rxtx_port_config *rx_config)
                if (nb_rx == 0)
                        continue;
 
-               if (copy_mode == COPY_MODE_IOAT_NUM) {
-                       /* Perform packet hardware copy */
-                       nb_enq = ioat_enqueue_packets(pkts_burst,
-                               nb_rx, rx_config->ioat_ids[i]);
-                       if (nb_enq > 0)
-                               rte_ioat_do_copies(rx_config->ioat_ids[i]);
-               } else {
-                       /* Perform packet software copy, free source packets */
-                       int ret;
-                       struct rte_mbuf *pkts_burst_copy[MAX_PKT_BURST];
+               port_statistics.rx[rx_config->rxtx_port] += nb_rx;
 
-                       ret = rte_mempool_get_bulk(ioat_pktmbuf_pool,
-                               (void *)pkts_burst_copy, nb_rx);
+               ret = rte_mempool_get_bulk(ioat_pktmbuf_pool,
+                       (void *)pkts_burst_copy, nb_rx);
 
-                       if (unlikely(ret < 0))
-                               rte_exit(EXIT_FAILURE,
-                                       "Unable to allocate memory.\n");
+               if (unlikely(ret < 0))
+                       rte_exit(EXIT_FAILURE,
+                               "Unable to allocate memory.\n");
 
-                       for (j = 0; j < nb_rx; j++)
-                               pktmbuf_sw_copy(pkts_burst[j],
-                                       pkts_burst_copy[j]);
+               for (j = 0; j < nb_rx; j++)
+                       pktmbuf_metadata_copy(pkts_burst[j],
+                               pkts_burst_copy[j]);
 
-                       rte_mempool_put_bulk(ioat_pktmbuf_pool,
-                               (void *)pkts_burst, nb_rx);
+               if (copy_mode == COPY_MODE_IOAT_NUM) {
 
-                       nb_enq = rte_ring_enqueue_burst(
-                               rx_config->rx_to_tx_ring,
-                               (void *)pkts_burst_copy, nb_rx, NULL);
+                       /* enqueue packets for  hardware copy */
+                       nb_enq = ioat_enqueue(pkts_burst, pkts_burst_copy,
+                               nb_rx, ioat_batch_sz, rx_config->ioat_ids[i]);
 
-                       /* Free any not enqueued packets. */
+                       /* free any not enqueued packets. */
+                       rte_mempool_put_bulk(ioat_pktmbuf_pool,
+                               (void *)&pkts_burst[nb_enq],
+                               nb_rx - nb_enq);
                        rte_mempool_put_bulk(ioat_pktmbuf_pool,
                                (void *)&pkts_burst_copy[nb_enq],
                                nb_rx - nb_enq);
+
+                       port_statistics.copy_dropped[rx_config->rxtx_port] +=
+                               (nb_rx - nb_enq);
+
+                       /* get completed copies */
+                       nb_rx = ioat_dequeue(pkts_burst, pkts_burst_copy,
+                               MAX_PKT_BURST, rx_config->ioat_ids[i]);
+               } else {
+                       /* Perform packet software copy, free source packets */
+                       for (j = 0; j < nb_rx; j++)
+                               pktmbuf_sw_copy(pkts_burst[j],
+                                       pkts_burst_copy[j]);
                }
+
+               rte_mempool_put_bulk(ioat_pktmbuf_pool,
+                       (void *)pkts_burst, nb_rx);
+
+               nb_enq = rte_ring_enqueue_burst(rx_config->rx_to_tx_ring,
+                       (void *)pkts_burst_copy, nb_rx, NULL);
+
+               /* Free any not enqueued packets. */
+               rte_mempool_put_bulk(ioat_pktmbuf_pool,
+                       (void *)&pkts_burst_copy[nb_enq],
+                       nb_rx - nb_enq);
+
+               port_statistics.copy_dropped[rx_config->rxtx_port] +=
+                       (nb_rx - nb_enq);
        }
 }
+/* >8 End of receive packets on one port and enqueue to IOAT rawdev or rte_ring. */
 
-/* Transmit packets from IOAT rawdev/rte_ring for one port. */
+/* Transmit packets from IOAT rawdev/rte_ring for one port. 8< */
 static void
 ioat_tx_port(struct rxtx_port_config *tx_config)
 {
-       uint32_t i, j, nb_dq = 0;
-       struct rte_mbuf *mbufs_src[MAX_PKT_BURST];
-       struct rte_mbuf *mbufs_dst[MAX_PKT_BURST];
+       uint32_t i, j, nb_dq, nb_tx;
+       struct rte_mbuf *mbufs[MAX_PKT_BURST];
 
        for (i = 0; i < tx_config->nb_queues; i++) {
-               if (copy_mode == COPY_MODE_IOAT_NUM) {
-                       /* Deque the mbufs from IOAT device. */
-                       nb_dq = rte_ioat_completed_copies(
-                               tx_config->ioat_ids[i], MAX_PKT_BURST,
-                               (void *)mbufs_src, (void *)mbufs_dst);
-               } else {
-                       /* Deque the mbufs from rx_to_tx_ring. */
-                       nb_dq = rte_ring_dequeue_burst(
-                               tx_config->rx_to_tx_ring, (void *)mbufs_dst,
-                               MAX_PKT_BURST, NULL);
-               }
 
+               /* Dequeue the mbufs from rx_to_tx_ring. */
+               nb_dq = rte_ring_dequeue_burst(tx_config->rx_to_tx_ring,
+                               (void *)mbufs, MAX_PKT_BURST, NULL);
                if (nb_dq == 0)
-                       return;
-
-               if (copy_mode == COPY_MODE_IOAT_NUM)
-                       rte_mempool_put_bulk(ioat_pktmbuf_pool,
-                               (void *)mbufs_src, nb_dq);
+                       continue;
 
                /* Update macs if enabled */
                if (mac_updating) {
                        for (j = 0; j < nb_dq; j++)
-                               update_mac_addrs(mbufs_dst[j],
+                               update_mac_addrs(mbufs[j],
                                        tx_config->rxtx_port);
                }
 
-               const uint16_t nb_tx = rte_eth_tx_burst(
-                       tx_config->rxtx_port, 0,
-                       (void *)mbufs_dst, nb_dq);
+               nb_tx = rte_eth_tx_burst(tx_config->rxtx_port, 0,
+                               (void *)mbufs, nb_dq);
+
+               port_statistics.tx[tx_config->rxtx_port] += nb_tx;
 
                /* Free any unsent packets. */
                if (unlikely(nb_tx < nb_dq))
                        rte_mempool_put_bulk(ioat_pktmbuf_pool,
-                       (void *)&mbufs_dst[nb_tx],
-                               nb_dq - nb_tx);
+                       (void *)&mbufs[nb_tx], nb_dq - nb_tx);
        }
 }
+/* >8 End of transmitting packets from IOAT. */
 
 /* Main rx processing loop for IOAT rawdev. */
 static void
@@ -293,7 +563,7 @@ tx_main_loop(void)
                        ioat_tx_port(&cfg.ports[i]);
 }
 
-/* Main rx and tx loop if only one slave lcore available */
+/* Main rx and tx loop if only one worker lcore available */
 static void
 rxtx_main_loop(void)
 {
@@ -310,6 +580,7 @@ rxtx_main_loop(void)
                }
 }
 
+/* Start processing for each lcore. 8< */
 static void start_forwarding_cores(void)
 {
        uint32_t lcore_id = rte_lcore_id();
@@ -331,12 +602,15 @@ static void start_forwarding_cores(void)
                        lcore_id);
        }
 }
+/* >8 End of starting to processfor each lcore. */
 
 /* Display usage */
 static void
 ioat_usage(const char *prgname)
 {
        printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
+               "  -b --dma-batch-size: number of requests per DMA batch\n"
+               "  -f --max-frame-size: max frame size\n"
                "  -p --portmask: hexadecimal bitmask of ports to configure\n"
                "  -q NQ: number of RX queues per port (default is 1)\n"
                "  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
@@ -344,7 +618,8 @@ ioat_usage(const char *prgname)
                "       - The source MAC address is replaced by the TX port MAC address\n"
                "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
                "  -c --copy-type CT: type of copy: sw|hw\n"
-               "  -s --ring-size RS: size of IOAT rawdev ring for hardware copy mode or rte_ring for software copy mode\n",
+               "  -s --ring-size RS: size of IOAT rawdev ring for hardware copy mode or rte_ring for software copy mode\n"
+               "  -i --stats-interval SI: interval, in seconds, between stats prints (default is 1)\n",
                        prgname);
 }
 
@@ -357,7 +632,7 @@ ioat_parse_portmask(const char *portmask)
        /* Parse hexadecimal string */
        pm = strtoul(portmask, &end, 16);
        if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
-               return -1;
+               return 0;
 
        return pm;
 }
@@ -378,10 +653,13 @@ static int
 ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
 {
        static const char short_options[] =
+               "b:"  /* dma batch size */
+               "c:"  /* copy type (sw|hw) */
+               "f:"  /* max frame size */
                "p:"  /* portmask */
                "q:"  /* number of RX queues per port */
-               "c:"  /* copy type (sw|hw) */
                "s:"  /* ring size */
+               "i:"  /* interval, in seconds, between stats prints */
                ;
 
        static const struct option lgopts[] = {
@@ -391,6 +669,9 @@ ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
                {CMD_LINE_OPT_NB_QUEUE, required_argument, NULL, 'q'},
                {CMD_LINE_OPT_COPY_TYPE, required_argument, NULL, 'c'},
                {CMD_LINE_OPT_RING_SIZE, required_argument, NULL, 's'},
+               {CMD_LINE_OPT_BATCH_SIZE, required_argument, NULL, 'b'},
+               {CMD_LINE_OPT_FRAME_SIZE, required_argument, NULL, 'f'},
+               {CMD_LINE_OPT_STATS_INTERVAL, required_argument, NULL, 'i'},
                {NULL, 0, 0, 0}
        };
 
@@ -407,6 +688,23 @@ ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
                        lgopts, &option_index)) != EOF) {
 
                switch (opt) {
+               case 'b':
+                       ioat_batch_sz = atoi(optarg);
+                       if (ioat_batch_sz > MAX_PKT_BURST) {
+                               printf("Invalid dma batch size, %s.\n", optarg);
+                               ioat_usage(prgname);
+                               return -1;
+                       }
+                       break;
+               case 'f':
+                       max_frame_size = atoi(optarg);
+                       if (max_frame_size > RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
+                               printf("Invalid max frame size, %s.\n", optarg);
+                               ioat_usage(prgname);
+                               return -1;
+                       }
+                       break;
+
                /* portmask */
                case 'p':
                        ioat_enabled_port_mask = ioat_parse_portmask(optarg);
@@ -447,6 +745,14 @@ ioat_parse_args(int argc, char **argv, unsigned int nb_ports)
                        }
                        break;
 
+               case 'i':
+                       stats_interval = atoi(optarg);
+                       if (stats_interval == 0) {
+                               printf("Invalid stats interval, setting to 1\n");
+                               stats_interval = 1;     /* set to default */
+                       }
+                       break;
+
                /* long options */
                case 0:
                        break;
@@ -472,7 +778,8 @@ check_link_status(uint32_t port_mask)
 {
        uint16_t portid;
        struct rte_eth_link link;
-       int retval = 0;
+       int ret, link_status = 0;
+       char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
 
        printf("\nChecking link status\n");
        RTE_ETH_FOREACH_DEV(portid) {
@@ -480,29 +787,35 @@ check_link_status(uint32_t port_mask)
                        continue;
 
                memset(&link, 0, sizeof(link));
-               rte_eth_link_get(portid, &link);
+               ret = rte_eth_link_get(portid, &link);
+               if (ret < 0) {
+                       printf("Port %u link get failed: err=%d\n",
+                                       portid, ret);
+                       continue;
+               }
 
                /* Print link status */
-               if (link.link_status) {
-                       printf(
-                               "Port %d Link Up. Speed %u Mbps - %s\n",
-                               portid, link.link_speed,
-                               (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
-                               ("full-duplex") : ("half-duplex\n"));
-                       retval = 1;
-               } else
-                       printf("Port %d Link Down\n", portid);
+               rte_eth_link_to_str(link_status_text,
+                       sizeof(link_status_text), &link);
+               printf("Port %d %s\n", portid, link_status_text);
+
+               if (link.link_status)
+                       link_status = 1;
        }
-       return retval;
+       return link_status;
 }
 
+/* Configuration of device. 8< */
 static void
 configure_rawdev_queue(uint32_t dev_id)
 {
-       struct rte_ioat_rawdev_config dev_config = { .ring_size = ring_size };
+       struct rte_ioat_rawdev_config dev_config = {
+                       .ring_size = ring_size,
+                       .no_prefetch_completions = (cfg.nb_lcores > 1),
+       };
        struct rte_rawdev_info info = { .dev_private = &dev_config };
 
-       if (rte_rawdev_configure(dev_id, &info) != 0) {
+       if (rte_rawdev_configure(dev_id, &info, sizeof(dev_config)) != 0) {
                rte_exit(EXIT_FAILURE,
                        "Error with rte_rawdev_configure()\n");
        }
@@ -511,7 +824,9 @@ configure_rawdev_queue(uint32_t dev_id)
                        "Error with rte_rawdev_start()\n");
        }
 }
+/* >8 End of configuration of device. */
 
+/* Using IOAT rawdev API functions. 8< */
 static void
 assign_rawdevs(void)
 {
@@ -525,9 +840,10 @@ assign_rawdevs(void)
                        do {
                                if (rdev_id == rte_rawdev_count())
                                        goto end;
-                               rte_rawdev_info_get(rdev_id++, &rdev_info);
-                       } while (strcmp(rdev_info.driver_name,
-                               IOAT_PMD_RAWDEV_NAME_STR) != 0);
+                               rte_rawdev_info_get(rdev_id++, &rdev_info, 0);
+                       } while (rdev_info.driver_name == NULL ||
+                                       strcmp(rdev_info.driver_name,
+                                               IOAT_PMD_RAWDEV_NAME_STR) != 0);
 
                        cfg.ports[i].ioat_ids[j] = rdev_id - 1;
                        configure_rawdev_queue(cfg.ports[i].ioat_ids[j]);
@@ -541,7 +857,9 @@ end:
                        nb_rawdev, cfg.nb_ports * cfg.ports[0].nb_queues);
        RTE_LOG(INFO, IOAT, "Number of used rawdevs: %u.\n", nb_rawdev);
 }
+/* >8 End of using IOAT rawdev API functions. */
 
+/* Assign ring structures for packet exchanging. 8< */
 static void
 assign_rings(void)
 {
@@ -561,6 +879,7 @@ assign_rings(void)
                                rte_strerror(rte_errno));
        }
 }
+/* >8 End of assigning ring structures for packet exchanging. */
 
 /*
  * Initializes a given port using global settings and with the RX buffers
@@ -569,19 +888,19 @@ assign_rings(void)
 static inline void
 port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
 {
-       /* configuring port to use RSS for multiple RX queues */
+       /* Configuring port to use RSS for multiple RX queues. 8< */
        static const struct rte_eth_conf port_conf = {
                .rxmode = {
-                       .mq_mode = ETH_MQ_RX_RSS,
-                       .max_rx_pkt_len = RTE_ETHER_MAX_LEN
+                       .mq_mode = RTE_ETH_MQ_RX_RSS,
                },
                .rx_adv_conf = {
                        .rss_conf = {
                                .rss_key = NULL,
-                               .rss_hf = ETH_RSS_PROTO_MASK,
+                               .rss_hf = RTE_ETH_RSS_PROTO_MASK,
                        }
                }
        };
+       /* >8 End of configuring port to use RSS for multiple RX queues. */
 
        struct rte_eth_rxconf rxq_conf;
        struct rte_eth_txconf txq_conf;
@@ -589,6 +908,9 @@ port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
        struct rte_eth_dev_info dev_info;
        int ret, i;
 
+       if (max_frame_size > local_port_conf.rxmode.mtu)
+               local_port_conf.rxmode.mtu = max_frame_size;
+
        /* Skip ports that are not enabled */
        if ((ioat_enabled_port_mask & (1 << portid)) == 0) {
                printf("Skipping disabled port %u\n", portid);
@@ -598,12 +920,13 @@ port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
        /* Init port */
        printf("Initializing port %u... ", portid);
        fflush(stdout);
-       rte_eth_dev_info_get(portid, &dev_info);
+       ret = rte_eth_dev_info_get(portid, &dev_info);
+       if (ret < 0)
+               rte_exit(EXIT_FAILURE, "Cannot get device info: %s, port=%u\n",
+                       rte_strerror(-ret), portid);
+
        local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
                dev_info.flow_type_rss_offloads;
-       if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
-               local_port_conf.txmode.offloads |=
-                       DEV_TX_OFFLOAD_MBUF_FAST_FREE;
        ret = rte_eth_dev_configure(portid, nb_queues, 1, &local_port_conf);
        if (ret < 0)
                rte_exit(EXIT_FAILURE, "Cannot configure device:"
@@ -653,23 +976,29 @@ port_init(uint16_t portid, struct rte_mempool *mbuf_pool, uint16_t nb_queues)
 
        rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
 
-       /* Start device */
+       ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
+               rte_eth_tx_buffer_count_callback,
+               &port_statistics.tx_dropped[portid]);
+       if (ret < 0)
+               rte_exit(EXIT_FAILURE,
+                       "Cannot set error callback for tx buffer on port %u\n",
+                       portid);
+
+       /* Start device. 8< */
        ret = rte_eth_dev_start(portid);
        if (ret < 0)
                rte_exit(EXIT_FAILURE,
                        "rte_eth_dev_start:err=%d, port=%u\n",
                        ret, portid);
+       /* >8 End of starting device. */
 
+       /* RX port is set in promiscuous mode. 8< */
        rte_eth_promiscuous_enable(portid);
+       /* >8 End of RX port is set in promiscuous mode. */
 
-       printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
+       printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
                        portid,
-                       ioat_ports_eth_addr[portid].addr_bytes[0],
-                       ioat_ports_eth_addr[portid].addr_bytes[1],
-                       ioat_ports_eth_addr[portid].addr_bytes[2],
-                       ioat_ports_eth_addr[portid].addr_bytes[3],
-                       ioat_ports_eth_addr[portid].addr_bytes[4],
-                       ioat_ports_eth_addr[portid].addr_bytes[5]);
+                       RTE_ETHER_ADDR_BYTES(&ioat_ports_eth_addr[portid]));
 
        cfg.ports[cfg.nb_ports].rxtx_port = portid;
        cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
@@ -692,11 +1021,13 @@ main(int argc, char **argv)
        uint16_t nb_ports, portid;
        uint32_t i;
        unsigned int nb_mbufs;
+       size_t sz;
 
-       /* Init EAL */
+       /* Init EAL. 8< */
        ret = rte_eal_init(argc, argv);
        if (ret < 0)
                rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
+       /* >8 End of init EAL. */
        argc -= ret;
        argv += ret;
 
@@ -713,22 +1044,31 @@ main(int argc, char **argv)
        if (ret < 0)
                rte_exit(EXIT_FAILURE, "Invalid IOAT arguments\n");
 
+       /* Allocates mempool to hold the mbufs. 8< */
        nb_mbufs = RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
-               4 * MAX_PKT_BURST) + rte_lcore_count() * MEMPOOL_CACHE_SIZE),
+               4 * MAX_PKT_BURST + ring_size) + ring_size +
+               rte_lcore_count() * MEMPOOL_CACHE_SIZE),
                MIN_POOL_SIZE);
 
        /* Create the mbuf pool */
+       sz = max_frame_size + RTE_PKTMBUF_HEADROOM;
+       sz = RTE_MAX(sz, (size_t)RTE_MBUF_DEFAULT_BUF_SIZE);
        ioat_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
-               MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
-               rte_socket_id());
+               MEMPOOL_CACHE_SIZE, 0, sz, rte_socket_id());
        if (ioat_pktmbuf_pool == NULL)
                rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
+       /* >8 End of allocates mempool to hold the mbufs. */
 
-       /* Initialise each port */
+       /* Initialize each port. 8< */
        cfg.nb_ports = 0;
        RTE_ETH_FOREACH_DEV(portid)
                port_init(portid, ioat_pktmbuf_pool, nb_queues);
+       /* >8 End of initializing each port. */
 
+       /* Initialize port xstats */
+       memset(&port_statistics, 0, sizeof(port_statistics));
+
+       /* Assigning each port resources. 8< */
        while (!check_link_status(ioat_enabled_port_mask) && !force_quit)
                sleep(1);
 
@@ -736,14 +1076,17 @@ main(int argc, char **argv)
        cfg.nb_lcores = rte_lcore_count() - 1;
        if (cfg.nb_lcores < 1)
                rte_exit(EXIT_FAILURE,
-                       "There should be at least one slave lcore.\n");
+                       "There should be at least one worker lcore.\n");
 
        if (copy_mode == COPY_MODE_IOAT_NUM)
                assign_rawdevs();
-       else /* copy_mode == COPY_MODE_SW_NUM */
-               assign_rings();
+
+       assign_rings();
+       /* >8 End of assigning each port resources. */
 
        start_forwarding_cores();
+       /* main core prints stats while other cores forward */
+       print_stats(argv[0]);
 
        /* force_quit is true when we get here */
        rte_eal_mp_wait_lcore();
@@ -751,7 +1094,11 @@ main(int argc, char **argv)
        uint32_t j;
        for (i = 0; i < cfg.nb_ports; i++) {
                printf("Closing port %d\n", cfg.ports[i].rxtx_port);
-               rte_eth_dev_stop(cfg.ports[i].rxtx_port);
+               ret = rte_eth_dev_stop(cfg.ports[i].rxtx_port);
+               if (ret != 0)
+                       RTE_LOG(ERR, IOAT, "rte_eth_dev_stop: err=%s, port=%u\n",
+                               rte_strerror(-ret), cfg.ports[i].rxtx_port);
+
                rte_eth_dev_close(cfg.ports[i].rxtx_port);
                if (copy_mode == COPY_MODE_IOAT_NUM) {
                        for (j = 0; j < cfg.ports[i].nb_queues; j++) {
@@ -763,6 +1110,9 @@ main(int argc, char **argv)
                        rte_ring_free(cfg.ports[i].rx_to_tx_ring);
        }
 
+       /* clean up the EAL */
+       rte_eal_cleanup();
+
        printf("Bye...\n");
        return 0;
 }