Check that frames cross the ring intact in both directions when the
block size is not a multiple of the frame size, that the largest frame
that fits a ring slot is sent while one byte more is dropped, and that
a packet of the maximum Rx length is received intact.

Signed-off-by: Scott Mitchell <[email protected]>
---
 app/test/test_pmd_af_packet.c | 388 ++++++++++++++++++++++++++++++++++
 1 file changed, 388 insertions(+)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index 81ab19031e..6a4806acc7 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -8,11 +8,13 @@
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <linux/if.h>
+#include <linux/if_packet.h>
 #include <linux/if_tun.h>
 #include <linux/sockios.h>
 #include <pthread.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
@@ -77,6 +79,18 @@ struct tag_count {
 #define AF_PACKET_DEV_NAME "net_af_packet_test"
 #define TAP_DEV_NAME "dpdkafptest"
 
+/* Frames built by fill_seq_frame(), with an IEEE local experimental EtherType 
*/
+#define TEST_ETHER_TYPE 0x88B5
+#define TAP_READ_TIMEOUT_US 200000    /* 200ms to read frames from the TAP */
+#define SEQ_TX_BURST 8
+
+/* Frame layout tests: page-sized blocks leave an unused tail after the last 
frame */
+#define LAYOUT_FRAMESZ 1536
+#define LAYOUT_MIN_FRAMECNT 32
+#define LAYOUT_FRAME_LEN 1400
+/* Largest packet a LAYOUT_FRAMESZ frame holds on Tx */
+#define LAYOUT_TX_MAX_LEN (LAYOUT_FRAMESZ - (TPACKET2_HDRLEN - sizeof(struct 
sockaddr_ll)))
+
 static struct rte_mempool *mp;
 static uint16_t port_id = RTE_MAX_ETHPORTS;
 static int tap_fd = -1;
@@ -382,6 +396,269 @@ wait_for_stats_update(uint16_t pid, uint64_t 
expected_opackets,
        return -1;
 }
 
+/* Helper: Stop, close and remove a port made by create_test_port(). */
+static void
+destroy_test_port(const char *name, uint16_t pid)
+{
+       rte_eth_dev_stop(pid);
+       rte_eth_dev_close(pid);
+       rte_vdev_uninit(name);
+}
+
+/*
+ * Helper: Format devargs for a ring of LAYOUT_FRAMESZ frames in page-sized
+ * blocks, with at least two blocks. Returns the frame count.
+ */
+static unsigned int
+layout_devargs(char *buf, size_t size, const char *extra)
+{
+       const unsigned int pagesize = getpagesize();
+       const unsigned int fpb = pagesize / LAYOUT_FRAMESZ;
+       const unsigned int framecnt = fpb * RTE_MAX(2u, LAYOUT_MIN_FRAMECNT / 
fpb);
+
+       snprintf(buf, size, "iface=%s,blocksz=%u,framesz=%d,framecnt=%u%s",
+                TAP_DEV_NAME, pagesize, LAYOUT_FRAMESZ, framecnt, extra);
+       return framecnt;
+}
+
+/* Helper: Create, configure and start an extra af_packet port on the TAP. */
+static int
+create_test_port(const char *name, const char *args, uint64_t tx_offloads,
+                uint16_t *pid)
+{
+       struct rte_eth_conf conf = { .txmode.offloads = tx_offloads };
+       int ret;
+
+       ret = create_af_packet_port(name, args, pid);
+       if (ret != 0)
+               return ret;
+
+       ret = rte_eth_dev_configure(*pid, 1, 1, &conf);
+       if (ret == 0)
+               ret = rte_eth_rx_queue_setup(*pid, 0, RING_SIZE,
+                                            rte_eth_dev_socket_id(*pid), NULL, 
mp);
+       if (ret == 0)
+               ret = rte_eth_tx_queue_setup(*pid, 0, RING_SIZE,
+                                            rte_eth_dev_socket_id(*pid), NULL);
+       if (ret == 0)
+               ret = rte_eth_dev_start(*pid);
+       if (ret != 0) {
+               printf("Failed to set up port '%s': %d\n", name, ret);
+               destroy_test_port(name, *pid);
+       }
+       return ret;
+}
+
+/*
+ * Helper: Build a TEST_ETHER_TYPE frame of len bytes carrying seq and a
+ * payload pattern derived from it.
+ */
+static void
+fill_seq_frame(uint8_t *frame, size_t len, const struct rte_ether_addr *src,
+              uint32_t seq)
+{
+       struct rte_ether_hdr *eth = (struct rte_ether_hdr *)frame;
+       size_t i;
+
+       memset(&eth->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN);
+       rte_ether_addr_copy(src, &eth->src_addr);
+       eth->ether_type = rte_cpu_to_be_16(TEST_ETHER_TYPE);
+       memcpy(frame + sizeof(*eth), &seq, sizeof(seq));
+       for (i = sizeof(*eth) + sizeof(seq); i < len; i++)
+               frame[i] = (uint8_t)(seq * 31 + i);
+}
+
+/*
+ * Helper: Check a frame built by fill_seq_frame().
+ * Returns its seq, -1 if it is some other frame, -2 if it is corrupt.
+ */
+static int64_t
+check_seq_frame(const uint8_t *frame, size_t len,
+               const struct rte_ether_addr *src, size_t expect_len)
+{
+       const struct rte_ether_hdr *eth = (const struct rte_ether_hdr *)frame;
+       uint32_t seq;
+       size_t i;
+
+       if (len < sizeof(*eth) + sizeof(seq) ||
+           eth->ether_type != rte_cpu_to_be_16(TEST_ETHER_TYPE) ||
+           !rte_is_same_ether_addr(&eth->src_addr, src))
+               return -1;
+
+       memcpy(&seq, frame + sizeof(*eth), sizeof(seq));
+       if (len != expect_len)
+               return -2;
+       for (i = sizeof(*eth) + sizeof(seq); i < len; i++)
+               if (frame[i] != (uint8_t)(seq * 31 + i))
+                       return -2;
+       return seq;
+}
+
+/*
+ * Helper: Record a checked frame in seen[]. Returns false if it was corrupt.
+ */
+static bool
+record_seq(int64_t seq, bool *seen, unsigned int n, unsigned int *nb_seen)
+{
+       if (seq == -1)
+               return true;
+       if (seq < 0 || seq >= n) {
+               printf("Corrupt frame (seq %" PRId64 ")\n", seq);
+               return false;
+       }
+       if (!seen[seq]) {
+               seen[seq] = true;
+               (*nb_seen)++;
+       }
+       return true;
+}
+
+/* Helper: Discard frames queued on the (non-blocking) TAP fd. */
+static void
+tap_drain(void)
+{
+       uint8_t buf[RTE_MBUF_DEFAULT_BUF_SIZE];
+
+       while (read(tap_fd, buf, sizeof(buf)) > 0)
+               ;
+}
+
+/*
+ * Helper: Read frames from the (non-blocking) TAP fd until want frames
+ * built by fill_seq_frame() have been seen, or timeout.
+ * Returns the number seen, -1 on a corrupt frame.
+ */
+static int
+tap_collect_seq(bool *seen, unsigned int n, unsigned int *nb_seen,
+               unsigned int want, size_t frame_len, uint64_t timeout_us)
+{
+       uint8_t buf[RTE_MBUF_DEFAULT_BUF_SIZE];
+       uint64_t elapsed = 0;
+       ssize_t len;
+
+       while (*nb_seen < want && elapsed < timeout_us) {
+               len = read(tap_fd, buf, sizeof(buf));
+               if (len > 0) {
+                       if (!record_seq(check_seq_frame(buf, len, &DEV_MAC_TAG,
+                                                       frame_len),
+                                       seen, n, nb_seen))
+                               return -1;
+                       continue;
+               }
+               rte_delay_us_block(STATS_POLL_INTERVAL_US);
+               elapsed += STATS_POLL_INTERVAL_US;
+       }
+       return *nb_seen;
+}
+
+/*
+ * Helper: Transmit n frames of frame_len bytes in small bursts and verify
+ * each one arrives intact on the TAP fd.
+ */
+static int
+check_tx_seq(uint16_t pid, unsigned int n, size_t frame_len)
+{
+       struct rte_mbuf *bufs[SEQ_TX_BURST];
+       unsigned int seq, i, burst, nb_seen = 0;
+       bool *seen;
+       int ret = TEST_FAILED;
+
+       seen = calloc(n, sizeof(*seen));
+       if (seen == NULL)
+               return TEST_FAILED;
+
+       for (seq = 0; seq < n; seq += burst) {
+               burst = RTE_MIN(n - seq, (unsigned int)SEQ_TX_BURST);
+               if (rte_pktmbuf_alloc_bulk(mp, bufs, burst) != 0) {
+                       printf("Could not allocate mbufs\n");
+                       goto out;
+               }
+               for (i = 0; i < burst; i++) {
+                       uint8_t *data = (uint8_t *)rte_pktmbuf_append(bufs[i], 
frame_len);
+
+                       if (data == NULL) {
+                               rte_pktmbuf_free_bulk(bufs, burst);
+                               goto out;
+                       }
+                       fill_seq_frame(data, frame_len, &DEV_MAC_TAG, seq + i);
+               }
+
+               if (do_tx_burst(pid, 0, bufs, burst) != burst) {
+                       printf("TX burst at seq %u was short\n", seq);
+                       goto out;
+               }
+               if (tap_collect_seq(seen, n, &nb_seen, seq + burst, frame_len,
+                                   TAP_READ_TIMEOUT_US) != (int)(seq + burst)) 
{
+                       printf("TX: %u of %u frames arrived intact\n", nb_seen, 
seq + burst);
+                       goto out;
+               }
+       }
+       ret = TEST_SUCCESS;
+out:
+       free(seen);
+       return ret;
+}
+
+/*
+ * Helper: Write n frames of frame_len bytes to the TAP fd and verify each one
+ * is received intact on pid.
+ */
+static int
+check_rx_seq(uint16_t pid, unsigned int n, size_t frame_len)
+{
+       struct rte_mbuf *bufs[BURST_SIZE];
+       uint8_t frame[RTE_MBUF_DEFAULT_BUF_SIZE];
+       unsigned int seq, nb_seen = 0;
+       uint64_t elapsed;
+       uint16_t i, nb_rx;
+       bool *seen, ok = true;
+       int ret = TEST_FAILED;
+
+       if (frame_len > sizeof(frame))
+               return TEST_FAILED;
+       seen = calloc(n, sizeof(*seen));
+       if (seen == NULL)
+               return TEST_FAILED;
+
+       for (seq = 0; seq < n; seq++) {
+               fill_seq_frame(frame, frame_len, &FD_MAC_TAG, seq);
+               if (write(tap_fd, frame, frame_len) != (ssize_t)frame_len) {
+                       printf("TAP write failed: %s\n", strerror(errno));
+                       goto out;
+               }
+               if ((seq + 1) % SEQ_TX_BURST != 0 && seq + 1 != n)
+                       continue;
+
+               for (elapsed = 0; ok && nb_seen < seq + 1 &&
+                    elapsed < TAP_READ_TIMEOUT_US;
+                    elapsed += STATS_POLL_INTERVAL_US) {
+                       nb_rx = rte_eth_rx_burst(pid, 0, bufs, BURST_SIZE);
+                       for (i = 0; i < nb_rx; i++) {
+                               const uint8_t *data;
+
+                               data = rte_pktmbuf_read(bufs[i], 0,
+                                       rte_pktmbuf_pkt_len(bufs[i]), frame);
+                               ok = ok && data != NULL &&
+                                       record_seq(check_seq_frame(data,
+                                               rte_pktmbuf_pkt_len(bufs[i]),
+                                               &FD_MAC_TAG, frame_len),
+                                               seen, n, &nb_seen);
+                       }
+                       rte_pktmbuf_free_bulk(bufs, nb_rx);
+                       if (nb_rx == 0)
+                               rte_delay_us_block(STATS_POLL_INTERVAL_US);
+               }
+               if (!ok || nb_seen < seq + 1) {
+                       printf("RX: %u of %u frames arrived intact\n", nb_seen, 
seq + 1);
+                       goto out;
+               }
+       }
+       ret = TEST_SUCCESS;
+out:
+       free(seen);
+       return ret;
+}
+
 static int
 test_af_packet_setup(void)
 {
@@ -1382,6 +1659,115 @@ test_af_packet_ignore_outgoing_multi_queue(void)
        return test_af_packet_ignore_outgoing(2, false);
 }
 
+/*
+ * Test: Frame addresses follow the kernel's block layout
+ *
+ * framesz=1536 in page-sized blocks leaves 1024 bytes unused after the last
+ * frame of each block, so sequential addressing (i * framesz) diverges from
+ * the kernel's layout from the second block on. Each frame must cross the
+ * ring intact.
+ */
+static int
+test_af_packet_frame_layout(void)
+{
+       const char *name = "net_af_packet_layout";
+       char devargs[256];
+       unsigned int framecnt;
+       uint16_t pid;
+       int flags, ret;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+
+       framecnt = layout_devargs(devargs, sizeof(devargs), "");
+       ret = create_test_port(name, devargs, 0, &pid);
+       TEST_ASSERT(ret == 0, "Failed to create port");
+
+       flags = fcntl(tap_fd, F_GETFL, 0);
+       fcntl(tap_fd, F_SETFL, flags | O_NONBLOCK);
+       tap_drain();
+
+       /* wrap around the ring three times */
+       ret = check_tx_seq(pid, 3 * framecnt, LAYOUT_FRAME_LEN);
+       if (ret == TEST_SUCCESS)
+               ret = check_rx_seq(pid, 3 * framecnt, LAYOUT_FRAME_LEN);
+
+       fcntl(tap_fd, F_SETFL, flags);
+       destroy_test_port(name, pid);
+       return ret;
+}
+
+/*
+ * Test: The largest frame that fits a ring slot is sent intact and one byte
+ * more is dropped and counted as an error. A frame of max_rx_pktlen is
+ * received intact.
+ */
+static int
+test_af_packet_frame_max_size(void)
+{
+       const char *name = "net_af_packet_maxsize";
+       struct rte_eth_dev_info dev_info;
+       struct rte_eth_stats stats;
+       struct rte_mbuf *m;
+       char devargs[256];
+       uint8_t *data;
+       const uint32_t max_len = LAYOUT_TX_MAX_LEN;
+       uint16_t pid;
+       int flags, ret;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+
+       /* Keep the slot below the TAP MTU so the kernel accepts the largest 
frame */
+       layout_devargs(devargs, sizeof(devargs), "");
+       ret = create_test_port(name, devargs, 0, &pid);
+       TEST_ASSERT(ret == 0, "Failed to create port");
+
+       flags = fcntl(tap_fd, F_GETFL, 0);
+       fcntl(tap_fd, F_SETFL, flags | O_NONBLOCK);
+       tap_drain();
+
+       ret = TEST_FAILED;
+       if (rte_eth_dev_info_get(pid, &dev_info) != 0)
+               goto out;
+       printf("framesz=%d max frame length: Tx %u Rx %u\n", LAYOUT_FRAMESZ, 
max_len,
+              dev_info.max_rx_pktlen);
+
+       if (check_tx_seq(pid, 1, max_len) != TEST_SUCCESS ||
+           check_rx_seq(pid, 1, dev_info.max_rx_pktlen) != TEST_SUCCESS)
+               goto out;
+
+       rte_eth_stats_reset(pid);
+       m = rte_pktmbuf_alloc(mp);
+       if (m == NULL)
+               goto out;
+       data = (uint8_t *)rte_pktmbuf_append(m, max_len + 1);
+       if (data == NULL) {
+               rte_pktmbuf_free(m);
+               goto out;
+       }
+       fill_seq_frame(data, max_len + 1, &DEV_MAC_TAG, 1);
+       if (do_tx_burst(pid, 0, &m, 1) != 1) {
+               printf("Oversized frame was not consumed\n");
+               goto out;
+       }
+       if (rte_eth_stats_get(pid, &stats) != 0 || stats.oerrors != 1 ||
+           stats.opackets != 0) {
+               printf("Oversized frame: opackets=%" PRIu64 " oerrors=%" PRIu64 
"\n",
+                      stats.opackets, stats.oerrors);
+               goto out;
+       }
+       ret = TEST_SUCCESS;
+out:
+       fcntl(tap_fd, F_SETFL, flags);
+       destroy_test_port(name, pid);
+       return ret;
+}
+
 static struct unit_test_suite af_packet_test_suite = {
        .suite_name = "AF_PACKET PMD Unit Test Suite",
        .setup = test_af_packet_setup,
@@ -1405,6 +1791,8 @@ static struct unit_test_suite af_packet_test_suite = {
                TEST_CASE(test_af_packet_invalid_bad_iface),
                TEST_CASE(test_af_packet_invalid_qpairs),
                TEST_CASE(test_af_packet_frame_config),
+               TEST_CASE(test_af_packet_frame_layout),
+               TEST_CASE(test_af_packet_frame_max_size),
                TEST_CASE(test_af_packet_qdisc_bypass),
                TEST_CASE(test_af_packet_multi_queue),
                TEST_CASE(test_af_packet_fanout_modes),
-- 
2.54.0 (Apple Git-157)

Reply via email to