+static int
+test_mbuf_linearize(struct rte_mempool *pktmbuf_pool, int pkt_len,
+ int nb_segs)
+{
+
+ struct rte_mbuf *m = NULL, *mbuf = NULL;
+ uint8_t *data;
+ int data_len = 0;
+ int remain;
+ int seg, seg_len;
+ int i;
+
+ if (pkt_len < 1) {
+ printf("Packet size must be 1 or more (is %d)\n", pkt_len);
+ return -1;
+ }
+
+ if (nb_segs < 1) {
+ printf("Number of segments must be 1 or more (is %d)\n",
+ nb_segs);
+ return -1;
+ }
+
+ seg_len = pkt_len / nb_segs;
+ if (seg_len == 0)
+ seg_len = 1;
+
+ remain = pkt_len;
+
+ /* Create chained mbuf_src and fill it generated data */
+ for (seg = 0; remain > 0; seg++) {
+
+ m = rte_pktmbuf_alloc(pktmbuf_pool);
+ if (m == NULL) {
+ printf("Cannot create segment for source mbuf");
+ goto fail;
+ }
+
+ /* Make sure if tailroom is zeroed */
+ memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
+ rte_pktmbuf_tailroom(m));
+
+ data_len = remain;
+ if (data_len > seg_len)
+ data_len = seg_len;
+
+ data = (uint8_t *)rte_pktmbuf_append(m, data_len);
+ if (data == NULL) {
+ printf("Cannot append %d bytes to the mbuf\n",
+ data_len);
+ goto fail;
+ }
+
+ for (i = 0; i < data_len; i++)
+ data[i] = (seg * seg_len + i) % 0x0ff;
+
+ if (seg == 0)
+ mbuf = m;
+ else
+ rte_pktmbuf_chain(mbuf, m);
+
+ remain -= data_len;
+ }
+
+ /* Create destination buffer to store coalesced data */
+ if (rte_pktmbuf_linearize(mbuf)) {
+ printf("Mbuf linearization failed\n");
+ goto fail;
+ }
+
+ if (!rte_pktmbuf_is_contiguous(mbuf)) {
+ printf("Source buffer should be contiguous after "
+ "linearization\n");
+ goto fail;
+ }
+
+ 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;
+}