+ data = rte_pktmbuf_mtod(mbuf, uint8_t *);
+
+ for (i = 0; i < pkt_len; i++)
+ if (data[i] != (i % 0x0ff)) {
+ printf("Incorrect data in linearized mbuf\n");
+ goto fail;
+ }
+
+ rte_pktmbuf_free(mbuf);
+ return 0;
+
+fail:
+ if (mbuf)
+ rte_pktmbuf_free(mbuf);
+ return -1;
+}
+
+static int
+test_mbuf_linearize_check(struct rte_mempool *pktmbuf_pool)
+{
+ struct test_mbuf_array {
+ int size;
+ int nb_segs;
+ } mbuf_array[] = {
+ { 128, 1 },
+ { 64, 64 },
+ { 512, 10 },
+ { 250, 11 },
+ { 123, 8 },
+ };
+ unsigned int i;
+
+ printf("Test mbuf linearize API\n");
+
+ for (i = 0; i < RTE_DIM(mbuf_array); i++)
+ if (test_mbuf_linearize(pktmbuf_pool, mbuf_array[i].size,
+ mbuf_array[i].nb_segs)) {
+ printf("Test failed for %d, %d\n", mbuf_array[i].size,
+ mbuf_array[i].nb_segs);
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * Helper function for test_tx_ofload
+ */
+static inline void
+set_tx_offload(struct rte_mbuf *mb, uint64_t il2, uint64_t il3, uint64_t il4,
+ uint64_t tso, uint64_t ol3, uint64_t ol2)
+{
+ mb->l2_len = il2;
+ mb->l3_len = il3;
+ mb->l4_len = il4;
+ mb->tso_segsz = tso;
+ mb->outer_l3_len = ol3;
+ mb->outer_l2_len = ol2;
+}
+
+static int
+test_tx_offload(void)
+{
+ struct rte_mbuf *mb;
+ uint64_t tm, v1, v2;
+ size_t sz;
+ uint32_t i;
+
+ static volatile struct {
+ uint16_t l2;
+ uint16_t l3;
+ uint16_t l4;
+ uint16_t tso;
+ } txof;
+
+ const uint32_t num = 0x10000;
+
+ txof.l2 = rte_rand() % (1 << RTE_MBUF_L2_LEN_BITS);
+ txof.l3 = rte_rand() % (1 << RTE_MBUF_L3_LEN_BITS);
+ txof.l4 = rte_rand() % (1 << RTE_MBUF_L4_LEN_BITS);
+ txof.tso = rte_rand() % (1 << RTE_MBUF_TSO_SEGSZ_BITS);
+
+ printf("%s started, tx_offload = {\n"
+ "\tl2_len=%#hx,\n"
+ "\tl3_len=%#hx,\n"
+ "\tl4_len=%#hx,\n"
+ "\ttso_segsz=%#hx,\n"
+ "\touter_l3_len=%#x,\n"
+ "\touter_l2_len=%#x,\n"
+ "};\n",
+ __func__,
+ txof.l2, txof.l3, txof.l4, txof.tso, txof.l3, txof.l2);
+
+ sz = sizeof(*mb) * num;
+ mb = rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
+ if (mb == NULL) {
+ printf("%s failed, out of memory\n", __func__);
+ return -ENOMEM;
+ }
+
+ memset(mb, 0, sz);
+ tm = rte_rdtsc_precise();
+
+ for (i = 0; i != num; i++)
+ set_tx_offload(mb + i, txof.l2, txof.l3, txof.l4,
+ txof.tso, txof.l3, txof.l2);
+
+ tm = rte_rdtsc_precise() - tm;
+ printf("%s set tx_offload by bit-fields: %u iterations, %"
+ PRIu64 " cycles, %#Lf cycles/iter\n",
+ __func__, num, tm, (long double)tm / num);
+
+ v1 = mb[rte_rand() % num].tx_offload;
+
+ memset(mb, 0, sz);
+ tm = rte_rdtsc_precise();
+
+ for (i = 0; i != num; i++)
+ mb[i].tx_offload = rte_mbuf_tx_offload(txof.l2, txof.l3,
+ txof.l4, txof.tso, txof.l3, txof.l2, 0);
+
+ tm = rte_rdtsc_precise() - tm;
+ printf("%s set raw tx_offload: %u iterations, %"
+ PRIu64 " cycles, %#Lf cycles/iter\n",
+ __func__, num, tm, (long double)tm / num);
+
+ v2 = mb[rte_rand() % num].tx_offload;
+
+ rte_free(mb);
+
+ printf("%s finished\n"
+ "expected tx_offload value: 0x%" PRIx64 ";\n"
+ "rte_mbuf_tx_offload value: 0x%" PRIx64 ";\n",
+ __func__, v1, v2);
+
+ return (v1 == v2) ? 0 : -EINVAL;
+}
+
+static int
+test_get_rx_ol_flag_list(void)
+{
+ int len = 6, ret = 0;
+ char buf[256] = "";
+ int buflen = 0;
+
+ /* Test case to check with null buffer */
+ ret = rte_get_rx_ol_flag_list(0, NULL, 0);
+ if (ret != -1)
+ GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
+
+ /* Test case to check with zero buffer len */
+ ret = rte_get_rx_ol_flag_list(PKT_RX_L4_CKSUM_MASK, buf, 0);
+ if (ret != -1)
+ GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
+
+ buflen = strlen(buf);
+ if (buflen != 0)
+ GOTO_FAIL("%s buffer should be empty, received = %d\n",
+ __func__, buflen);
+
+ /* Test case to check with reduced buffer len */
+ ret = rte_get_rx_ol_flag_list(0, buf, len);
+ if (ret != -1)
+ GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
+
+ buflen = strlen(buf);
+ if (buflen != (len - 1))
+ GOTO_FAIL("%s invalid buffer length retrieved, expected: %d,"
+ "received = %d\n", __func__,
+ (len - 1), buflen);
+
+ /* Test case to check with zero mask value */
+ ret = rte_get_rx_ol_flag_list(0, buf, sizeof(buf));
+ if (ret != 0)
+ GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
+
+ buflen = strlen(buf);
+ if (buflen == 0)
+ GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
+ "non-zero, buffer should not be empty");
+
+ /* Test case to check with valid mask value */
+ ret = rte_get_rx_ol_flag_list(PKT_RX_SEC_OFFLOAD, buf, sizeof(buf));
+ if (ret != 0)
+ GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
+
+ buflen = strlen(buf);
+ if (buflen == 0)
+ GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
+ "non-zero, buffer should not be empty");
+
+ return 0;
+fail:
+ return -1;
+}
+
+static int
+test_get_tx_ol_flag_list(void)
+{
+ int len = 6, ret = 0;
+ char buf[256] = "";
+ int buflen = 0;
+
+ /* Test case to check with null buffer */
+ ret = rte_get_tx_ol_flag_list(0, NULL, 0);
+ if (ret != -1)
+ GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
+
+ /* Test case to check with zero buffer len */
+ ret = rte_get_tx_ol_flag_list(PKT_TX_IP_CKSUM, buf, 0);
+ if (ret != -1)
+ GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
+
+ buflen = strlen(buf);
+ if (buflen != 0) {
+ GOTO_FAIL("%s buffer should be empty, received = %d\n",
+ __func__, buflen);
+ }
+
+ /* Test case to check with reduced buffer len */
+ ret = rte_get_tx_ol_flag_list(0, buf, len);
+ if (ret != -1)
+ GOTO_FAIL("%s expected: -1, received = %d\n", __func__, ret);
+
+ buflen = strlen(buf);
+ if (buflen != (len - 1))
+ GOTO_FAIL("%s invalid buffer length retrieved, expected: %d,"
+ "received = %d\n", __func__,
+ (len - 1), buflen);
+
+ /* Test case to check with zero mask value */
+ ret = rte_get_tx_ol_flag_list(0, buf, sizeof(buf));
+ if (ret != 0)
+ GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
+
+ buflen = strlen(buf);
+ if (buflen == 0)
+ GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
+ "non-zero, buffer should not be empty");
+
+ /* Test case to check with valid mask value */
+ ret = rte_get_tx_ol_flag_list(PKT_TX_UDP_CKSUM, buf, sizeof(buf));
+ if (ret != 0)
+ GOTO_FAIL("%s expected: 0, received = %d\n", __func__, ret);
+
+ buflen = strlen(buf);
+ if (buflen == 0)
+ GOTO_FAIL("%s expected: %s, received length = 0\n", __func__,
+ "non-zero, buffer should not be empty");
+
+ return 0;
+fail:
+ return -1;
+
+}
+
+struct flag_name {
+ uint64_t flag;
+ const char *name;
+};
+
+static int
+test_get_rx_ol_flag_name(void)
+{
+ uint16_t i;
+ const char *flag_str = NULL;
+ const struct flag_name rx_flags[] = {
+ VAL_NAME(PKT_RX_VLAN),
+ VAL_NAME(PKT_RX_RSS_HASH),
+ VAL_NAME(PKT_RX_FDIR),
+ VAL_NAME(PKT_RX_L4_CKSUM_BAD),
+ VAL_NAME(PKT_RX_L4_CKSUM_GOOD),
+ VAL_NAME(PKT_RX_L4_CKSUM_NONE),
+ VAL_NAME(PKT_RX_IP_CKSUM_BAD),
+ VAL_NAME(PKT_RX_IP_CKSUM_GOOD),
+ VAL_NAME(PKT_RX_IP_CKSUM_NONE),
+ VAL_NAME(PKT_RX_EIP_CKSUM_BAD),
+ VAL_NAME(PKT_RX_VLAN_STRIPPED),
+ VAL_NAME(PKT_RX_IEEE1588_PTP),
+ VAL_NAME(PKT_RX_IEEE1588_TMST),
+ VAL_NAME(PKT_RX_FDIR_ID),
+ VAL_NAME(PKT_RX_FDIR_FLX),
+ VAL_NAME(PKT_RX_QINQ_STRIPPED),
+ VAL_NAME(PKT_RX_LRO),
+ VAL_NAME(PKT_RX_TIMESTAMP),
+ VAL_NAME(PKT_RX_SEC_OFFLOAD),
+ VAL_NAME(PKT_RX_SEC_OFFLOAD_FAILED),
+ VAL_NAME(PKT_RX_OUTER_L4_CKSUM_BAD),
+ VAL_NAME(PKT_RX_OUTER_L4_CKSUM_GOOD),
+ VAL_NAME(PKT_RX_OUTER_L4_CKSUM_INVALID),
+ };
+
+ /* Test case to check with valid flag */
+ for (i = 0; i < RTE_DIM(rx_flags); i++) {
+ flag_str = rte_get_rx_ol_flag_name(rx_flags[i].flag);
+ if (flag_str == NULL)
+ GOTO_FAIL("%s: Expected flagname = %s; received null\n",
+ __func__, rx_flags[i].name);
+ if (strcmp(flag_str, rx_flags[i].name) != 0)
+ GOTO_FAIL("%s: Expected flagname = %s; received = %s\n",
+ __func__, rx_flags[i].name, flag_str);
+ }
+ /* Test case to check with invalid flag */
+ flag_str = rte_get_rx_ol_flag_name(0);
+ if (flag_str != NULL) {
+ GOTO_FAIL("%s: Expected flag name = null; received = %s\n",
+ __func__, flag_str);
+ }
+
+ return 0;
+fail:
+ return -1;
+}
+
+static int
+test_get_tx_ol_flag_name(void)
+{
+ uint16_t i;
+ const char *flag_str = NULL;
+ const struct flag_name tx_flags[] = {
+ VAL_NAME(PKT_TX_VLAN),
+ VAL_NAME(PKT_TX_IP_CKSUM),
+ VAL_NAME(PKT_TX_TCP_CKSUM),
+ VAL_NAME(PKT_TX_SCTP_CKSUM),
+ VAL_NAME(PKT_TX_UDP_CKSUM),
+ VAL_NAME(PKT_TX_IEEE1588_TMST),
+ VAL_NAME(PKT_TX_TCP_SEG),
+ VAL_NAME(PKT_TX_IPV4),
+ VAL_NAME(PKT_TX_IPV6),
+ VAL_NAME(PKT_TX_OUTER_IP_CKSUM),
+ VAL_NAME(PKT_TX_OUTER_IPV4),
+ VAL_NAME(PKT_TX_OUTER_IPV6),
+ VAL_NAME(PKT_TX_TUNNEL_VXLAN),
+ VAL_NAME(PKT_TX_TUNNEL_GRE),
+ VAL_NAME(PKT_TX_TUNNEL_IPIP),
+ VAL_NAME(PKT_TX_TUNNEL_GENEVE),
+ VAL_NAME(PKT_TX_TUNNEL_MPLSINUDP),
+ VAL_NAME(PKT_TX_TUNNEL_VXLAN_GPE),
+ VAL_NAME(PKT_TX_TUNNEL_IP),
+ VAL_NAME(PKT_TX_TUNNEL_UDP),
+ VAL_NAME(PKT_TX_QINQ),
+ VAL_NAME(PKT_TX_MACSEC),
+ VAL_NAME(PKT_TX_SEC_OFFLOAD),
+ VAL_NAME(PKT_TX_UDP_SEG),
+ VAL_NAME(PKT_TX_OUTER_UDP_CKSUM),
+ };
+
+ /* Test case to check with valid flag */
+ for (i = 0; i < RTE_DIM(tx_flags); i++) {
+ flag_str = rte_get_tx_ol_flag_name(tx_flags[i].flag);
+ if (flag_str == NULL)
+ GOTO_FAIL("%s: Expected flagname = %s; received null\n",
+ __func__, tx_flags[i].name);
+ if (strcmp(flag_str, tx_flags[i].name) != 0)
+ GOTO_FAIL("%s: Expected flagname = %s; received = %s\n",
+ __func__, tx_flags[i].name, flag_str);
+ }
+ /* Test case to check with invalid flag */
+ flag_str = rte_get_tx_ol_flag_name(0);
+ if (flag_str != NULL) {
+ GOTO_FAIL("%s: Expected flag name = null; received = %s\n",
+ __func__, flag_str);
+ }
+
+ return 0;
+fail:
+ return -1;
+
+}
+
+static int
+test_mbuf_validate_tx_offload(const char *test_name,
+ struct rte_mempool *pktmbuf_pool,
+ uint64_t ol_flags,
+ uint16_t segsize,
+ int expected_retval)
+{
+ struct rte_mbuf *m = NULL;
+ int ret = 0;
+
+ /* alloc a mbuf and do sanity check */
+ m = rte_pktmbuf_alloc(pktmbuf_pool);
+ if (m == NULL)
+ GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
+ if (rte_pktmbuf_pkt_len(m) != 0)
+ GOTO_FAIL("%s: Bad packet length\n", __func__);
+ rte_mbuf_sanity_check(m, 0);
+ m->ol_flags = ol_flags;
+ m->tso_segsz = segsize;
+ ret = rte_validate_tx_offload(m);
+ if (ret != expected_retval)
+ GOTO_FAIL("%s(%s): expected ret val: %d; received: %d\n",
+ __func__, test_name, expected_retval, ret);
+ rte_pktmbuf_free(m);
+ m = NULL;
+ return 0;
+fail:
+ if (m) {
+ rte_pktmbuf_free(m);
+ m = NULL;
+ }
+ return -1;
+}
+
+static int
+test_mbuf_validate_tx_offload_one(struct rte_mempool *pktmbuf_pool)
+{
+ /* test to validate tx offload flags */
+ uint64_t ol_flags = 0;
+
+ /* test to validate if IP checksum is counted only for IPV4 packet */
+ /* set both IP checksum and IPV6 flags */
+ ol_flags |= PKT_TX_IP_CKSUM;
+ ol_flags |= PKT_TX_IPV6;
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_CKSUM_IPV6_SET",
+ pktmbuf_pool,
+ ol_flags, 0, -EINVAL) < 0)
+ GOTO_FAIL("%s failed: IP cksum is set incorrect.\n", __func__);
+ /* resetting ol_flags for next testcase */
+ ol_flags = 0;
+
+ /* test to validate if IP type is set when required */
+ ol_flags |= PKT_TX_L4_MASK;
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_NOT_SET",
+ pktmbuf_pool,
+ ol_flags, 0, -EINVAL) < 0)
+ GOTO_FAIL("%s failed: IP type is not set.\n", __func__);
+
+ /* test if IP type is set when TCP SEG is on */
+ ol_flags |= PKT_TX_TCP_SEG;
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_NOT_SET",
+ pktmbuf_pool,
+ ol_flags, 0, -EINVAL) < 0)
+ GOTO_FAIL("%s failed: IP type is not set.\n", __func__);
+
+ ol_flags = 0;
+ /* test to confirm IP type (IPV4/IPV6) is set */
+ ol_flags = PKT_TX_L4_MASK;
+ ol_flags |= PKT_TX_IPV6;
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_IP_TYPE_SET",
+ pktmbuf_pool,
+ ol_flags, 0, 0) < 0)
+ GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
+
+ ol_flags = 0;
+ /* test to check TSO segment size is non-zero */
+ ol_flags |= PKT_TX_IPV4;
+ ol_flags |= PKT_TX_TCP_SEG;
+ /* set 0 tso segment size */
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_NULL_TSO_SEGSZ",
+ pktmbuf_pool,
+ ol_flags, 0, -EINVAL) < 0)
+ GOTO_FAIL("%s failed: tso segment size is null.\n", __func__);
+
+ /* retain IPV4 and PKT_TX_TCP_SEG mask */
+ /* set valid tso segment size but IP CKSUM not set */
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IP_CKSUM_NOT_SET",
+ pktmbuf_pool,
+ ol_flags, 512, -EINVAL) < 0)
+ GOTO_FAIL("%s failed: IP CKSUM is not set.\n", __func__);
+
+ /* test to validate if IP checksum is set for TSO capability */
+ /* retain IPV4, TCP_SEG, tso_seg size */
+ ol_flags |= PKT_TX_IP_CKSUM;
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IP_CKSUM_SET",
+ pktmbuf_pool,
+ ol_flags, 512, 0) < 0)
+ GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
+
+ /* test to confirm TSO for IPV6 type */
+ ol_flags = 0;
+ ol_flags |= PKT_TX_IPV6;
+ ol_flags |= PKT_TX_TCP_SEG;
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_TSO_IPV6_SET",
+ pktmbuf_pool,
+ ol_flags, 512, 0) < 0)
+ GOTO_FAIL("%s failed: TSO req not met.\n", __func__);
+
+ ol_flags = 0;
+ /* test if outer IP checksum set for non outer IPv4 packet */
+ ol_flags |= PKT_TX_IPV6;
+ ol_flags |= PKT_TX_OUTER_IP_CKSUM;
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_OUTER_IPV4_NOT_SET",
+ pktmbuf_pool,
+ ol_flags, 512, -EINVAL) < 0)
+ GOTO_FAIL("%s failed: Outer IP cksum set.\n", __func__);
+
+ ol_flags = 0;
+ /* test to confirm outer IP checksum is set for outer IPV4 packet */
+ ol_flags |= PKT_TX_OUTER_IP_CKSUM;
+ ol_flags |= PKT_TX_OUTER_IPV4;
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_OUTER_IPV4_SET",
+ pktmbuf_pool,
+ ol_flags, 512, 0) < 0)
+ GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
+
+ ol_flags = 0;
+ /* test to confirm if packets with no TX_OFFLOAD_MASK are skipped */
+ if (test_mbuf_validate_tx_offload("MBUF_TEST_OL_MASK_NOT_SET",
+ pktmbuf_pool,
+ ol_flags, 512, 0) < 0)
+ GOTO_FAIL("%s failed: tx offload flag error.\n", __func__);
+ return 0;
+fail:
+ return -1;
+}
+
+/*
+ * Test for allocating a bulk of mbufs
+ * define an array with positive sizes for mbufs allocations.
+ */
+static int
+test_pktmbuf_alloc_bulk(struct rte_mempool *pktmbuf_pool)
+{
+ int ret = 0;
+ unsigned int idx, loop;
+ unsigned int alloc_counts[] = {
+ 0,
+ MEMPOOL_CACHE_SIZE - 1,
+ MEMPOOL_CACHE_SIZE + 1,
+ MEMPOOL_CACHE_SIZE * 1.5,
+ MEMPOOL_CACHE_SIZE * 2,
+ MEMPOOL_CACHE_SIZE * 2 - 1,
+ MEMPOOL_CACHE_SIZE * 2 + 1,
+ MEMPOOL_CACHE_SIZE,
+ };
+
+ /* allocate a large array of mbuf pointers */
+ struct rte_mbuf *mbufs[NB_MBUF] = { 0 };
+ for (idx = 0; idx < RTE_DIM(alloc_counts); idx++) {
+ ret = rte_pktmbuf_alloc_bulk(pktmbuf_pool, mbufs,
+ alloc_counts[idx]);
+ if (ret == 0) {
+ for (loop = 0; loop < alloc_counts[idx] &&
+ mbufs[loop] != NULL; loop++)
+ rte_pktmbuf_free(mbufs[loop]);
+ } else if (ret != 0) {
+ printf("%s: Bulk alloc failed count(%u); ret val(%d)\n",
+ __func__, alloc_counts[idx], ret);
+ return -1;
+ }
+ }
+ return 0;
+}
+
+/*
+ * Negative testing for allocating a bulk of mbufs
+ */
+static int
+test_neg_pktmbuf_alloc_bulk(struct rte_mempool *pktmbuf_pool)
+{
+ int ret = 0;
+ unsigned int idx, loop;
+ unsigned int neg_alloc_counts[] = {
+ MEMPOOL_CACHE_SIZE - NB_MBUF,
+ NB_MBUF + 1,
+ NB_MBUF * 8,
+ UINT_MAX
+ };
+ struct rte_mbuf *mbufs[NB_MBUF * 8] = { 0 };
+
+ for (idx = 0; idx < RTE_DIM(neg_alloc_counts); idx++) {
+ ret = rte_pktmbuf_alloc_bulk(pktmbuf_pool, mbufs,
+ neg_alloc_counts[idx]);
+ if (ret == 0) {
+ printf("%s: Bulk alloc must fail! count(%u); ret(%d)\n",
+ __func__, neg_alloc_counts[idx], ret);
+ for (loop = 0; loop < neg_alloc_counts[idx] &&
+ mbufs[loop] != NULL; loop++)
+ rte_pktmbuf_free(mbufs[loop]);
+ return -1;
+ }
+ }
+ return 0;
+}
+
+/*
+ * Test to read mbuf packet using rte_pktmbuf_read
+ */
+static int
+test_pktmbuf_read(struct rte_mempool *pktmbuf_pool)
+{
+ struct rte_mbuf *m = NULL;
+ char *data = NULL;
+ const char *data_copy = NULL;
+ int off;
+
+ /* alloc a mbuf */
+ m = rte_pktmbuf_alloc(pktmbuf_pool);
+ if (m == NULL)
+ GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
+ if (rte_pktmbuf_pkt_len(m) != 0)
+ GOTO_FAIL("%s: Bad packet length\n", __func__);
+ rte_mbuf_sanity_check(m, 0);
+
+ data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
+ if (data == NULL)
+ GOTO_FAIL("%s: Cannot append data\n", __func__);
+ if (rte_pktmbuf_pkt_len(m) != MBUF_TEST_DATA_LEN2)
+ GOTO_FAIL("%s: Bad packet length\n", __func__);
+ memset(data, 0xfe, MBUF_TEST_DATA_LEN2);
+
+ /* read the data from mbuf */
+ data_copy = rte_pktmbuf_read(m, 0, MBUF_TEST_DATA_LEN2, NULL);
+ if (data_copy == NULL)
+ GOTO_FAIL("%s: Error in reading data!\n", __func__);
+ for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
+ if (data_copy[off] != (char)0xfe)
+ GOTO_FAIL("Data corrupted at offset %u", off);
+ }
+ rte_pktmbuf_free(m);
+ m = NULL;
+
+ return 0;
+fail:
+ if (m) {
+ rte_pktmbuf_free(m);
+ m = NULL;
+ }
+ return -1;
+}
+
+/*
+ * Test to read mbuf packet data from offset
+ */
+static int
+test_pktmbuf_read_from_offset(struct rte_mempool *pktmbuf_pool)
+{
+ struct rte_mbuf *m = NULL;
+ struct ether_hdr *hdr = NULL;
+ char *data = NULL;
+ const char *data_copy = NULL;
+ unsigned int off;
+ unsigned int hdr_len = sizeof(struct rte_ether_hdr);
+
+ /* alloc a mbuf */
+ m = rte_pktmbuf_alloc(pktmbuf_pool);
+ if (m == NULL)
+ GOTO_FAIL("%s: mbuf allocation failed!\n", __func__);
+
+ if (rte_pktmbuf_pkt_len(m) != 0)
+ GOTO_FAIL("%s: Bad packet length\n", __func__);
+ rte_mbuf_sanity_check(m, 0);
+
+ /* prepend an ethernet header */
+ hdr = (struct ether_hdr *)rte_pktmbuf_prepend(m, hdr_len);
+ if (hdr == NULL)
+ GOTO_FAIL("%s: Cannot prepend header\n", __func__);
+ if (rte_pktmbuf_pkt_len(m) != hdr_len)
+ GOTO_FAIL("%s: Bad pkt length", __func__);
+ if (rte_pktmbuf_data_len(m) != hdr_len)
+ GOTO_FAIL("%s: Bad data length", __func__);
+ memset(hdr, 0xde, hdr_len);
+
+ /* read mbuf header info from 0 offset */
+ data_copy = rte_pktmbuf_read(m, 0, hdr_len, NULL);
+ if (data_copy == NULL)
+ GOTO_FAIL("%s: Error in reading header!\n", __func__);
+ for (off = 0; off < hdr_len; off++) {
+ if (data_copy[off] != (char)0xde)
+ GOTO_FAIL("Header info corrupted at offset %u", off);
+ }
+
+ /* append sample data after ethernet header */
+ data = rte_pktmbuf_append(m, MBUF_TEST_DATA_LEN2);
+ if (data == NULL)
+ GOTO_FAIL("%s: Cannot append data\n", __func__);
+ if (rte_pktmbuf_pkt_len(m) != hdr_len + MBUF_TEST_DATA_LEN2)
+ GOTO_FAIL("%s: Bad packet length\n", __func__);
+ if (rte_pktmbuf_data_len(m) != hdr_len + MBUF_TEST_DATA_LEN2)
+ GOTO_FAIL("%s: Bad data length\n", __func__);
+ memset(data, 0xcc, MBUF_TEST_DATA_LEN2);
+
+ /* read mbuf data after header info */
+ data_copy = rte_pktmbuf_read(m, hdr_len, MBUF_TEST_DATA_LEN2, NULL);
+ if (data_copy == NULL)
+ GOTO_FAIL("%s: Error in reading header data!\n", __func__);
+ for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
+ if (data_copy[off] != (char)0xcc)
+ GOTO_FAIL("Data corrupted at offset %u", off);
+ }
+
+ /* partial reading of mbuf data */
+ data_copy = rte_pktmbuf_read(m, hdr_len + 5, MBUF_TEST_DATA_LEN2 - 5,
+ NULL);
+ if (data_copy == NULL)
+ GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
+ if (strlen(data_copy) != MBUF_TEST_DATA_LEN2 - 5)
+ GOTO_FAIL("%s: Incorrect data length!\n", __func__);
+ for (off = 0; off < MBUF_TEST_DATA_LEN2 - 5; off++) {
+ if (data_copy[off] != (char)0xcc)
+ GOTO_FAIL("Data corrupted at offset %u", off);
+ }
+
+ /* read length greater than mbuf data_len */
+ if (rte_pktmbuf_read(m, hdr_len, rte_pktmbuf_data_len(m) + 1,
+ NULL) != NULL)
+ GOTO_FAIL("%s: Requested len is larger than mbuf data len!\n",
+ __func__);
+
+ /* read length greater than mbuf pkt_len */
+ if (rte_pktmbuf_read(m, hdr_len, rte_pktmbuf_pkt_len(m) + 1,
+ NULL) != NULL)
+ GOTO_FAIL("%s: Requested len is larger than mbuf pkt len!\n",
+ __func__);
+
+ /* read data of zero len from valid offset */
+ data_copy = rte_pktmbuf_read(m, hdr_len, 0, NULL);
+ if (data_copy == NULL)
+ GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
+ if (strlen(data_copy) != MBUF_TEST_DATA_LEN2)
+ GOTO_FAIL("%s: Corrupted data content!\n", __func__);
+ for (off = 0; off < MBUF_TEST_DATA_LEN2; off++) {
+ if (data_copy[off] != (char)0xcc)
+ GOTO_FAIL("Data corrupted at offset %u", off);
+ }
+
+ /* read data of zero length from zero offset */
+ data_copy = rte_pktmbuf_read(m, 0, 0, NULL);
+ if (data_copy == NULL)
+ GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
+ /* check if the received address is the beginning of header info */
+ if (hdr != (const struct ether_hdr *)data_copy)
+ GOTO_FAIL("%s: Corrupted data address!\n", __func__);
+
+ /* read data of max length from valid offset */
+ data_copy = rte_pktmbuf_read(m, hdr_len, UINT_MAX, NULL);
+ if (data_copy == NULL)
+ GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
+ /* check if the received address is the beginning of data segment */
+ if (data_copy != data)
+ GOTO_FAIL("%s: Corrupted data address!\n", __func__);
+
+ /* try to read from mbuf with max size offset */
+ data_copy = rte_pktmbuf_read(m, UINT_MAX, 0, NULL);
+ if (data_copy != NULL)
+ GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
+
+ /* try to read from mbuf with max size offset and len */
+ data_copy = rte_pktmbuf_read(m, UINT_MAX, UINT_MAX, NULL);
+ if (data_copy != NULL)
+ GOTO_FAIL("%s: Error in reading packet data!\n", __func__);
+
+ rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
+
+ rte_pktmbuf_free(m);
+ m = NULL;
+
+ return 0;
+fail:
+ if (m) {
+ rte_pktmbuf_free(m);
+ m = NULL;