From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from dpdk.org (dpdk.org [92.243.14.124])
	by inbox.dpdk.org (Postfix) with ESMTP id F2F34A057B;
	Wed,  1 Apr 2020 20:40:03 +0200 (CEST)
Received: from [92.243.14.124] (localhost [127.0.0.1])
	by dpdk.org (Postfix) with ESMTP id 656F61C133;
	Wed,  1 Apr 2020 20:39:42 +0200 (CEST)
Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com
 [207.211.31.81]) by dpdk.org (Postfix) with ESMTP id E79331C115
 for <dev@dpdk.org>; Wed,  1 Apr 2020 20:39:37 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1585766377;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:content-type:content-type:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=w+/K5A22MotWX4xf/xpieFFbqlhp/MsllC2vE93p8C4=;
 b=ADriB/8Ma3fW4h8/J39+5KKPagvappC/v2xnkOoJTPq5K2XGVfRPmgoE6hzfzRIL72Qhzt
 1gMdS+qwDJpWpIi6nFiiAaH5Wdb6dc+Z3f3rEVgF115EZjr87ZszgKSeb+ERrMJhSxj7QA
 I3SB4iSj/7I66sl4nM+g+HHCtob5nJc=
Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com
 [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id
 us-mta-307-XGSS1Bj8MdKI0Z8GyboIig-1; Wed, 01 Apr 2020 14:39:35 -0400
X-MC-Unique: XGSS1Bj8MdKI0Z8GyboIig-1
Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com
 [10.5.11.14])
 (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
 (No client certificate requested)
 by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5DFAC8017F4;
 Wed,  1 Apr 2020 18:39:34 +0000 (UTC)
Received: from dhcp-25.97.bos.redhat.com (ovpn-116-136.phx2.redhat.com
 [10.3.116.136])
 by smtp.corp.redhat.com (Postfix) with ESMTP id 61F1B5D9CD;
 Wed,  1 Apr 2020 18:39:33 +0000 (UTC)
From: Aaron Conole <aconole@redhat.com>
To: dev@dpdk.org
Cc: Konstantin Ananyev <konstantin.ananyev@intel.com>,
 Sunil Kumar Kori <skori@marvell.com>,
 Anatoly Burakov <anatoly.burakov@intel.com>, Chas Williams <chas3@att.com>,
 Bruce Richardson <bruce.richardson@intel.com>,
 David Marchand <david.marchand@redhat.com>
Date: Wed,  1 Apr 2020 14:39:17 -0400
Message-Id: <20200401183917.3620845-5-aconole@redhat.com>
In-Reply-To: <20200401183917.3620845-1-aconole@redhat.com>
References: <20200401131849.2209336-1-aconole@redhat.com>
 <20200401183917.3620845-1-aconole@redhat.com>
MIME-Version: 1.0
X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14
X-Mimecast-Spam-Score: 0
X-Mimecast-Originator: redhat.com
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable
Subject: [dpdk-dev] [PATCH v3 4/4] ipfrag: add unit test case
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

Initial IP fragmentation unit test.

Signed-off-by: Aaron Conole <aconole@redhat.com>
---
 MAINTAINERS            |   1 +
 app/test/meson.build   |   2 +
 app/test/test_ipfrag.c | 277 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 280 insertions(+)
 create mode 100644 app/test/test_ipfrag.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 4800f6884a..c81f07ac9c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1230,6 +1230,7 @@ F: app/test/test_crc.c
 IP fragmentation & reassembly
 M: Konstantin Ananyev <konstantin.ananyev@intel.com>
 F: lib/librte_ip_frag/
+F: app/test/test_ipfrag.c
 F: doc/guides/prog_guide/ip_fragment_reassembly_lib.rst
 F: examples/ip_fragmentation/
 F: doc/guides/sample_app_ug/ip_frag.rst
diff --git a/app/test/meson.build b/app/test/meson.build
index 351d29cb65..c2b07fa62f 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -58,6 +58,7 @@ test_sources =3D files('commands.c',
 =09'test_hash_perf.c',
 =09'test_hash_readwrite_lf_perf.c',
 =09'test_interrupts.c',
+        'test_ipfrag.c',
 =09'test_ipsec.c',
 =09'test_ipsec_sad.c',
 =09'test_kni.c',
@@ -187,6 +188,7 @@ fast_tests =3D [
         ['flow_classify_autotest', false],
         ['hash_autotest', true],
         ['interrupt_autotest', true],
+        ['ipfrag_autotest', false],
         ['logs_autotest', true],
         ['lpm_autotest', true],
         ['lpm6_autotest', true],
diff --git a/app/test/test_ipfrag.c b/app/test/test_ipfrag.c
new file mode 100644
index 0000000000..1fa7b69e60
--- /dev/null
+++ b/app/test/test_ipfrag.c
@@ -0,0 +1,277 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2020 Red Hat, Inc.
+ */
+
+#include <time.h>
+
+#include <rte_common.h>
+#include <rte_cycles.h>
+#include <rte_hexdump.h>
+#include <rte_ip.h>
+#include <rte_ip_frag.h>
+#include <rte_mbuf.h>
+#include <rte_memcpy.h>
+#include <rte_random.h>
+
+#include "test.h"
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#endif
+
+static struct rte_mempool *pkt_pool,
+=09=09=09  *direct_pool,
+=09=09=09  *indirect_pool;
+
+static int
+setup_buf_pool(void)
+{
+#define NUM_MBUFS (128)
+#define BURST 32
+
+=09if (!pkt_pool)
+=09=09pkt_pool =3D rte_pktmbuf_pool_create("FRAG_MBUF_POOL",
+=09=09=09=09=09=09   NUM_MBUFS, BURST, 0,
+=09=09=09=09=09=09   RTE_MBUF_DEFAULT_BUF_SIZE,
+=09=09=09=09=09=09   SOCKET_ID_ANY);
+=09if (pkt_pool =3D=3D NULL) {
+=09=09printf("%s: Error creating pkt mempool\n", __func__);
+=09=09goto bad_setup;
+=09}
+
+=09if (!direct_pool)
+=09=09direct_pool =3D rte_pktmbuf_pool_create("FRAG_D_MBUF_POOL",
+=09=09=09=09=09=09      NUM_MBUFS, BURST, 0,
+=09=09=09=09=09=09      RTE_MBUF_DEFAULT_BUF_SIZE,
+=09=09=09=09=09=09      SOCKET_ID_ANY);
+=09if (!direct_pool) {
+=09=09printf("%s: Error creating direct mempool\n", __func__);
+=09=09goto bad_setup;
+=09}
+
+=09if (!indirect_pool)
+=09=09indirect_pool =3D rte_pktmbuf_pool_create("FRAG_I_MBUF_POOL",
+=09=09=09=09=09=09=09NUM_MBUFS, BURST, 0,
+=09=09=09=09=09=09=090, SOCKET_ID_ANY);
+=09if (!indirect_pool) {
+=09=09printf("%s: Error creating indirect mempool\n", __func__);
+=09=09goto bad_setup;
+=09}
+
+=09return 0;
+
+bad_setup:
+=09if (pkt_pool)
+=09=09rte_mempool_free(pkt_pool);
+
+=09if (direct_pool)
+=09=09rte_mempool_free(direct_pool);
+
+=09return TEST_FAILED;
+}
+
+static int testsuite_setup(void)
+{
+=09if (setup_buf_pool())
+=09=09return TEST_FAILED;
+=09return TEST_SUCCESS;
+}
+
+static void testsuite_teardown(void)
+{
+=09if (pkt_pool)
+=09=09rte_mempool_free(pkt_pool);
+
+=09if (direct_pool)
+=09=09rte_mempool_free(direct_pool);
+
+=09if (indirect_pool)
+=09=09rte_mempool_free(indirect_pool);
+
+=09pkt_pool =3D NULL;
+}
+
+static int ut_setup(void)
+{
+=09return TEST_SUCCESS;
+}
+
+static void ut_teardown(void)
+{
+}
+
+static int
+v4_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, int df,
+=09=09      uint8_t ttl, uint8_t proto, uint16_t pktid)
+{
+=09/* Create a packet, 2k bytes long */
+=09b->data_off =3D 0;
+=09char *data =3D rte_pktmbuf_mtod(b, char *);
+
+=09memset(data, fill, sizeof(struct rte_ipv4_hdr) + s);
+
+=09struct rte_ipv4_hdr *hdr =3D (struct rte_ipv4_hdr *)data;
+
+=09hdr->version_ihl =3D 0x45; /* standard IP header... */
+=09hdr->type_of_service =3D 0;
+=09b->pkt_len =3D s + sizeof(struct rte_ipv4_hdr);
+=09b->data_len =3D b->pkt_len;
+=09hdr->total_length =3D htonl(b->pkt_len);
+=09hdr->packet_id =3D htons(pktid);
+=09hdr->fragment_offset =3D 0;
+=09if (df)
+=09=09hdr->fragment_offset =3D htons(0x4000);
+
+=09if (!ttl)
+=09=09ttl =3D 64; /* default to 64 */
+
+=09if (!proto)
+=09=09proto =3D 1; /* icmp */
+
+=09hdr->time_to_live =3D ttl;
+=09hdr->next_proto_id =3D proto;
+=09hdr->hdr_checksum =3D 0;
+=09hdr->src_addr =3D htonl(0x8080808);
+=09hdr->dst_addr =3D htonl(0x8080404);
+
+=09return 0;
+}
+
+static int
+v6_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, uint8_t ttl,
+=09=09      uint8_t proto, uint16_t pktid)
+{
+=09/* Create a packet, 2k bytes long */
+=09b->data_off =3D 0;
+=09char *data =3D rte_pktmbuf_mtod(b, char *);
+
+=09memset(data, fill, sizeof(struct rte_ipv6_hdr) + s);
+
+=09struct rte_ipv6_hdr *hdr =3D (struct rte_ipv6_hdr *)data;
+=09b->pkt_len =3D s + sizeof(struct rte_ipv6_hdr);
+=09b->data_len =3D b->pkt_len;
+
+=09/* basic v6 header */
+=09hdr->vtc_flow =3D htonl(0x60 << 24 | pktid);
+=09hdr->payload_len =3D htons(b->pkt_len);
+=09hdr->proto =3D proto;
+=09hdr->hop_limits =3D ttl;
+
+=09memset(hdr->src_addr, 0x08, sizeof(hdr->src_addr));
+=09memset(hdr->dst_addr, 0x04, sizeof(hdr->src_addr));
+
+=09return 0;
+}
+
+static inline void
+test_free_fragments(struct rte_mbuf *mb[], uint32_t num)
+{
+=09uint32_t i;
+=09for (i =3D 0; i < num; i++)
+=09=09rte_pktmbuf_free(mb[i]);
+}
+
+static int
+test_ip_frag(void)
+{
+=09int result =3D TEST_SUCCESS;
+=09size_t i;
+
+=09struct test_ip_frags {
+=09=09int     ipv;
+=09=09size_t  mtu_size;
+=09=09size_t  pkt_size;
+=09=09int     set_df;
+=09=09int     ttl;
+=09=09uint8_t proto;
+=09=09int     pkt_id;
+=09=09int     expected_frags;
+=09} tests[] =3D {
+=09=09     {4, 1280, 1400, 0, 64, IPPROTO_ICMP, -1, 2},
+=09=09     {4, 1280, 1400, 0, 64, IPPROTO_ICMP,  0, 2},
+=09=09     {4,  600, 1400, 0, 64, IPPROTO_ICMP, -1, 3},
+=09=09     {4,    4, 1400, 0, 64, IPPROTO_ICMP, -1, -EINVAL},
+=09=09     {4,  600, 1400, 1, 64, IPPROTO_ICMP, -1, -ENOTSUP},
+=09=09     {4,  600, 1400, 0,  0, IPPROTO_ICMP, -1, 3},
+
+=09=09     {6, 1280, 1400, 0, 64, IPPROTO_ICMP, -1, 2},
+=09=09     {6, 1300, 1400, 0, 64, IPPROTO_ICMP, -1, 2},
+=09=09     {6,    4, 1400, 0, 64, IPPROTO_ICMP, -1, -EINVAL},
+=09=09     {6, 1300, 1400, 0,  0, IPPROTO_ICMP, -1, 2},
+=09=09     {6, 1280, 1400, 0, 64, IPPROTO_FRAGMENT, -1, -ENOTSUP},
+=09};
+
+=09for (i =3D 0; i < ARRAY_SIZE(tests); i++) {
+=09=09int32_t len;
+=09=09uint16_t pktid =3D tests[i].pkt_id;
+=09=09struct rte_mbuf *pkts_out[BURST];
+=09=09struct rte_mbuf *b =3D rte_pktmbuf_alloc(pkt_pool);
+
+=09=09if (!b)
+=09=09=09return TEST_FAILED; /* Serious error.. abort here */
+
+=09=09if (tests[i].pkt_id =3D=3D -1)
+=09=09=09pktid =3D rte_rand_max(UINT16_MAX);
+
+=09=09if (tests[i].ipv =3D=3D 4) {
+=09=09=09if (v4_allocate_packet_of(b, 0x41414141,
+=09=09=09=09=09=09  tests[i].pkt_size,
+=09=09=09=09=09=09  tests[i].set_df,
+=09=09=09=09=09=09  tests[i].ttl,
+=09=09=09=09=09=09  tests[i].proto,
+=09=09=09=09=09=09  pktid))
+=09=09=09=09result =3D TEST_FAILED;
+=09=09} else if (tests[i].ipv =3D=3D 6) {
+=09=09=09if (v6_allocate_packet_of(b, 0x41414141,
+=09=09=09=09=09=09  tests[i].pkt_size,
+=09=09=09=09=09=09  tests[i].ttl,
+=09=09=09=09=09=09  tests[i].proto,
+=09=09=09=09=09=09  pktid))
+=09=09=09=09result =3D TEST_FAILED;
+=09=09}
+
+=09=09if (tests[i].ipv =3D=3D 4)
+=09=09=09len =3D rte_ipv4_fragment_packet(b, pkts_out, BURST,
+=09=09=09=09=09=09       tests[i].mtu_size,
+=09=09=09=09=09=09       direct_pool,
+=09=09=09=09=09=09       indirect_pool);
+=09=09else
+=09=09=09len =3D rte_ipv6_fragment_packet(b, pkts_out, BURST,
+=09=09=09=09=09=09       tests[i].mtu_size,
+=09=09=09=09=09=09       direct_pool,
+=09=09=09=09=09=09       indirect_pool);
+
+=09=09rte_pktmbuf_free(b);
+
+=09=09if (len > 0)
+=09=09=09test_free_fragments(pkts_out, len);
+
+=09=09printf("%d: checking %d with %d\n", (int)i, len,
+=09=09       (int)tests[i].expected_frags);
+=09=09RTE_TEST_ASSERT_EQUAL(len, tests[i].expected_frags,
+=09=09=09=09      "Failed case %u\n", (unsigned int)i);
+
+=09}
+
+=09return result;
+}
+
+static struct unit_test_suite ipfrag_testsuite  =3D {
+=09.suite_name =3D "IP Frag Unit Test Suite",
+=09.setup =3D testsuite_setup,
+=09.teardown =3D testsuite_teardown,
+=09.unit_test_cases =3D {
+=09=09TEST_CASE_ST(ut_setup, ut_teardown,
+=09=09=09     test_ip_frag),
+
+=09=09TEST_CASES_END() /**< NULL terminate unit test array */
+=09}
+};
+
+static int
+test_ipfrag(void)
+{
+=09return unit_test_suite_runner(&ipfrag_testsuite);
+}
+
+REGISTER_TEST_COMMAND(ipfrag_autotest, test_ipfrag);
--=20
2.25.1