From patchwork Wed Apr 1 13:18:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 67580 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id C2C90A057B; Wed, 1 Apr 2020 15:19:10 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 71BE51BE99; Wed, 1 Apr 2020 15:19:03 +0200 (CEST) Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by dpdk.org (Postfix) with ESMTP id 4FF70F12 for ; Wed, 1 Apr 2020 15:19:01 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585747140; 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=HhtkXaIAKVIRMRLgl9jv/au+32wzVtF4coqo9/EZ80M=; b=MAz+pvGvQm8FoiBkyJm91ilnR7CjyBavXc4uLpLukJKRI/rTS8BYIszG38pK6WpZ3hkWsT ykuTCWPcZWY5ljJr6HFcxs8zyxW7C2m+83c9/nBmZVsYy9VcBf8oVHEutqR4MsoUYfb+hC KoKF2nLC+TrcxylimOyCYPnDut8rEYs= 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-454-7N39NNd-MZacQJ4yEA3gnQ-1; Wed, 01 Apr 2020 09:18:59 -0400 X-MC-Unique: 7N39NNd-MZacQJ4yEA3gnQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D2CF68017FD; Wed, 1 Apr 2020 13:18:57 +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 7A5ED1001B07; Wed, 1 Apr 2020 13:18:56 +0000 (UTC) From: Aaron Conole To: dev@dpdk.org Cc: Konstantin Ananyev , Sunil Kumar Kori , Allain Legacy , Anatoly Burakov , Chas Williams , Piotr Azarewicz , Bruce Richardson , David Marchand Date: Wed, 1 Apr 2020 09:18:46 -0400 Message-Id: <20200401131849.2209336-2-aconole@redhat.com> In-Reply-To: <20200401131849.2209336-1-aconole@redhat.com> References: <20200331160714.697790-1-aconole@redhat.com> <20200401131849.2209336-1-aconole@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH v2 1/4] ip_frag: ensure minimum v4 fragmentation length X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The IPv4 specification says that each fragment must at least the size of an IP header plus 8 octets. When attempting to run ipfrag using a smaller size, the fragment library will return successful completion, even though it is a violation of RFC791 (and updates). Signed-off-by: Aaron Conole --- lib/librte_ip_frag/rte_ipv4_fragmentation.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/librte_ip_frag/rte_ipv4_fragmentation.c b/lib/librte_ip_frag/rte_ipv4_fragmentation.c index 9e9f986cc5..4baaf6355c 100644 --- a/lib/librte_ip_frag/rte_ipv4_fragmentation.c +++ b/lib/librte_ip_frag/rte_ipv4_fragmentation.c @@ -76,6 +76,12 @@ rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in, uint16_t fragment_offset, flag_offset, frag_size; uint16_t frag_bytes_remaining; + /* + * Ensure the IP fragmentation size is at least iphdr length + 8 octets + */ + if (unlikely(mtu_size < (sizeof(struct rte_ipv4_hdr) + 8*sizeof(char)))) + return -EINVAL; + /* * Ensure the IP payload length of all fragments is aligned to a * multiple of 8 bytes as per RFC791 section 2.3. From patchwork Wed Apr 1 13:18:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 67581 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 934E6A057B; Wed, 1 Apr 2020 15:19:19 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id C46B51BEAA; Wed, 1 Apr 2020 15:19:06 +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 77EF71BDAC for ; Wed, 1 Apr 2020 15:19:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585747144; 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=wb0D5HgZ66vFFP8s3SSRe6vjYfLc1IdHq3fIMPsHgZ0=; b=Zh9yleJfYnYkIMTjWELyt9oEHecUKYuEbdvPKthW8oCKZknY+uDlkER5O92glCKxD2CGGq DzxYtdFAp7Lge3SZ9F4kRRyDbTtgMafotFcbyVy8CH9XlabZ9DM9/v95ra//YV6SH9U7CC e3kyGqOEoUnHxK87jLBQjLB3NcMPCIc= 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-447-Mg3eBJ45PGSrsnCEyJjUzA-1; Wed, 01 Apr 2020 09:19:01 -0400 X-MC-Unique: Mg3eBJ45PGSrsnCEyJjUzA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5AD19800D4E; Wed, 1 Apr 2020 13:18:59 +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 0A6671001B07; Wed, 1 Apr 2020 13:18:57 +0000 (UTC) From: Aaron Conole To: dev@dpdk.org Cc: Konstantin Ananyev , Sunil Kumar Kori , Allain Legacy , Anatoly Burakov , Chas Williams , Piotr Azarewicz , Bruce Richardson , David Marchand Date: Wed, 1 Apr 2020 09:18:47 -0400 Message-Id: <20200401131849.2209336-3-aconole@redhat.com> In-Reply-To: <20200401131849.2209336-1-aconole@redhat.com> References: <20200331160714.697790-1-aconole@redhat.com> <20200401131849.2209336-1-aconole@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH v2 2/4] ip_frag: ensure minimum v6 fragmentation length X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Similar to v4, we should ensure that at least a minimum fragmentation length can be achieved. Signed-off-by: Aaron Conole --- lib/librte_ip_frag/rte_ipv6_fragmentation.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/librte_ip_frag/rte_ipv6_fragmentation.c b/lib/librte_ip_frag/rte_ipv6_fragmentation.c index 43449970e5..820a5dc725 100644 --- a/lib/librte_ip_frag/rte_ipv6_fragmentation.c +++ b/lib/librte_ip_frag/rte_ipv6_fragmentation.c @@ -79,6 +79,17 @@ rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in, uint16_t fragment_offset, frag_size; uint64_t frag_bytes_remaining; + /* + * The rules on ipv6 fragmentation means we need at least to be + * greater than the smallest fragment possible - ipv6hdr + frag header + * + min octets. We rely on the ipv6_extension_fragment header to + * include 'data' octets. + */ + if (unlikely(mtu_size < + (sizeof(struct rte_ipv6_hdr) + + sizeof(struct ipv6_extension_fragment)))) + return -EINVAL; + /* * Ensure the IP payload length of all fragments (except the * the last fragment) are a multiple of 8 bytes per RFC2460. From patchwork Wed Apr 1 13:18:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 67582 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 641EBA057B; Wed, 1 Apr 2020 15:19:28 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 1FEA51BEB1; Wed, 1 Apr 2020 15:19:08 +0200 (CEST) Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by dpdk.org (Postfix) with ESMTP id 769FC1BDAC for ; Wed, 1 Apr 2020 15:19:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585747145; 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=9Gu+DaDU6fMH6fC79P8/HOXRb2kwB5vp5ne34KqEE50=; b=ecKpk5fNqL642LusB/29jIOCR2nz7ElbdNxmCXu+zZSkSmZBwn64285B7ssUpNL5uEyKeV BFKwzmbDx2A+wlzVSBhxSMmkYH2c7kFvRWYbaJ441bTIBMjmfjHH5lmJskSJH+966ShwL5 1nj/L9++Azb1TQ+wQVHZDOQxM7IxiVM= 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-96-8RvyJwBJMo-7ZDazHGGgaw-1; Wed, 01 Apr 2020 09:19:02 -0400 X-MC-Unique: 8RvyJwBJMo-7ZDazHGGgaw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9EE78802563; Wed, 1 Apr 2020 13:19:00 +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 8392910016EB; Wed, 1 Apr 2020 13:18:59 +0000 (UTC) From: Aaron Conole To: dev@dpdk.org Cc: Konstantin Ananyev , Sunil Kumar Kori , Allain Legacy , Anatoly Burakov , Chas Williams , Piotr Azarewicz , Bruce Richardson , David Marchand Date: Wed, 1 Apr 2020 09:18:48 -0400 Message-Id: <20200401131849.2209336-4-aconole@redhat.com> In-Reply-To: <20200401131849.2209336-1-aconole@redhat.com> References: <20200331160714.697790-1-aconole@redhat.com> <20200401131849.2209336-1-aconole@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH v2 3/4] ip_frag: ipv6 fragments must not be resubmitted to fragmentation X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" IPv6 only allows traffic source nodes to fragment, so submitting a packet with next header of IPPROTO_FRAGMENT would be invalid. Signed-off-by: Aaron Conole --- lib/librte_ip_frag/rte_ipv6_fragmentation.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/librte_ip_frag/rte_ipv6_fragmentation.c b/lib/librte_ip_frag/rte_ipv6_fragmentation.c index 820a5dc725..aebcfa4325 100644 --- a/lib/librte_ip_frag/rte_ipv6_fragmentation.c +++ b/lib/librte_ip_frag/rte_ipv6_fragmentation.c @@ -106,6 +106,10 @@ rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in, in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv6_hdr *); + /* Fragmenting a fragmented packet?! */ + if (unlikely(in_hdr->proto == IPPROTO_FRAGMENT)) + return -ENOTSUP; + in_seg = pkt_in; in_seg_data_pos = sizeof(struct rte_ipv6_hdr); out_pkt_pos = 0; From patchwork Wed Apr 1 13:18:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 67583 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4B087A057B; Wed, 1 Apr 2020 15:19:38 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 78ECE1BEBA; Wed, 1 Apr 2020 15:19:09 +0200 (CEST) Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by dpdk.org (Postfix) with ESMTP id EDB4E1BEAE for ; Wed, 1 Apr 2020 15:19:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585747147; 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=LTay9qTf5nB2ox3wAIrG+AVCDiEy4VI1pnE/RCTqqRE=; b=iEk2W79wlCVwoOLNtwJFigq4vooQsqNy89n71A3zw+WTELGjBDJQJEqCSg676ayg1vLeZA xCQ8sHKDbI4sTYhuFXiSeA0iUoSBjctm0b4pMnCiyB9C++GlPSXHT1touW9MQXBdwWzjID 8jq3FT3RHIViNuSfj2CXAZCFcWGBBe8= 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-212-mc3DbAulPUqowPTsKni4IA-1; Wed, 01 Apr 2020 09:19:03 -0400 X-MC-Unique: mc3DbAulPUqowPTsKni4IA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 56A81107ACC7; Wed, 1 Apr 2020 13:19:02 +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 CABD810016EB; Wed, 1 Apr 2020 13:19:00 +0000 (UTC) From: Aaron Conole To: dev@dpdk.org Cc: Konstantin Ananyev , Sunil Kumar Kori , Allain Legacy , Anatoly Burakov , Chas Williams , Piotr Azarewicz , Bruce Richardson , David Marchand Date: Wed, 1 Apr 2020 09:18:49 -0400 Message-Id: <20200401131849.2209336-5-aconole@redhat.com> In-Reply-To: <20200401131849.2209336-1-aconole@redhat.com> References: <20200331160714.697790-1-aconole@redhat.com> <20200401131849.2209336-1-aconole@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH v2 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Initial IP fragmentation unit test. Signed-off-by: Aaron Conole --- MAINTAINERS | 1 + app/test/meson.build | 2 + app/test/test_ipfrag.c | 276 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 279 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 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 = files('commands.c', 'test_hash_perf.c', 'test_hash_readwrite_lf_perf.c', 'test_interrupts.c', + 'test_ipfrag.c', 'test_ipsec.c', 'test_ipsec_sad.c', 'test_kni.c', @@ -187,6 +188,7 @@ fast_tests = [ ['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..4cdc1dcd54 --- /dev/null +++ b/app/test/test_ipfrag.c @@ -0,0 +1,276 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2020 Red Hat, Inc. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "test.h" + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#endif + +static struct rte_mempool *pkt_pool, + *direct_pool, + *indirect_pool; + +static int +setup_buf_pool(void) +{ +#define NUM_MBUFS (128) +#define BURST 32 + + if (!pkt_pool) + pkt_pool = rte_pktmbuf_pool_create("FRAG_MBUF_POOL", + NUM_MBUFS, BURST, 0, + RTE_MBUF_DEFAULT_BUF_SIZE, + SOCKET_ID_ANY); + if (pkt_pool == NULL) { + printf("%s: Error creating pkt mempool\n", __func__); + goto bad_setup; + } + + if (!direct_pool) + direct_pool = rte_pktmbuf_pool_create("FRAG_D_MBUF_POOL", + NUM_MBUFS, BURST, 0, + RTE_MBUF_DEFAULT_BUF_SIZE, + SOCKET_ID_ANY); + if (!direct_pool) { + printf("%s: Error creating direct mempool\n", __func__); + goto bad_setup; + } + + if (!indirect_pool) + indirect_pool = rte_pktmbuf_pool_create("FRAG_I_MBUF_POOL", + NUM_MBUFS, BURST, 0, + 0, SOCKET_ID_ANY); + if (!indirect_pool) { + printf("%s: Error creating indirect mempool\n", __func__); + goto bad_setup; + } + + return 0; + +bad_setup: + if (pkt_pool) + rte_mempool_free(pkt_pool); + + if (direct_pool) + rte_mempool_free(direct_pool); + + return TEST_FAILED; +} + +static int testsuite_setup(void) +{ + if (setup_buf_pool()) + return TEST_FAILED; + return TEST_SUCCESS; +} + +static void testsuite_teardown(void) +{ + if (pkt_pool) + rte_mempool_free(pkt_pool); + + if (direct_pool) + rte_mempool_free(direct_pool); + + if (indirect_pool) + rte_mempool_free(indirect_pool); + + pkt_pool = NULL; +} + +static int ut_setup(void) +{ + return TEST_SUCCESS; +} + +static void ut_teardown(void) +{ +} + +static int +v4_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, int df, + uint8_t ttl, uint8_t proto, uint16_t pktid) +{ + /* Create a packet, 2k bytes long */ + b->data_off = 0; + char *data = rte_pktmbuf_mtod(b, char *); + + memset(data, fill, sizeof(struct rte_ipv4_hdr) + s); + + struct rte_ipv4_hdr *hdr = (struct rte_ipv4_hdr *)data; + + hdr->version_ihl = 0x45; /* standard IP header... */ + hdr->type_of_service = 0; + b->pkt_len = s + sizeof(struct rte_ipv4_hdr); + b->data_len = b->pkt_len; + hdr->total_length = htonl(b->pkt_len); + hdr->packet_id = htons(pktid); + hdr->fragment_offset = 0; + if (df) + hdr->fragment_offset = htons(0x4000); + + if (!ttl) + ttl = 64; /* default to 64 */ + + if (!proto) + proto = 1; /* icmp */ + + hdr->time_to_live = ttl; + hdr->next_proto_id = proto; + hdr->hdr_checksum = 0; + hdr->src_addr = htonl(0x8080808); + hdr->dst_addr = htonl(0x8080404); + + return 0; +} + +static int +v6_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, uint8_t ttl, + uint8_t proto, uint16_t pktid) +{ + /* Create a packet, 2k bytes long */ + b->data_off = 0; + char *data = rte_pktmbuf_mtod(b, char *); + + memset(data, fill, sizeof(struct rte_ipv6_hdr) + s); + + struct rte_ipv6_hdr *hdr = (struct rte_ipv6_hdr *)data; + b->pkt_len = s + sizeof(struct rte_ipv6_hdr); + b->data_len = b->pkt_len; + + /* basic v6 header */ + hdr->vtc_flow = htonl(0x60 << 24 | pktid); + hdr->payload_len = htons(b->pkt_len); + hdr->proto = proto; + hdr->hop_limits = ttl; + + memset(hdr->src_addr, 0x08, sizeof(hdr->src_addr)); + memset(hdr->dst_addr, 0x04, sizeof(hdr->src_addr)); + + return 0; +} + +static inline void +test_free_fragments(struct rte_mbuf *mb[], uint32_t num) +{ + uint32_t i; + for (i = 0; i < num; i++) + rte_pktmbuf_free(mb[i]); +} + +static int +test_ip_frag(void) +{ + int result = TEST_SUCCESS; + + struct test_ip_frags { + int ipv; + size_t mtu_size; + size_t pkt_size; + int set_df; + int ttl; + uint8_t proto; + int pkt_id; + int expected_frags; + } tests[] = { + {4, 1280, 1400, 0, 64, IPPROTO_ICMP, -1, 2}, + {4, 1280, 1400, 0, 64, IPPROTO_ICMP, 0, 2}, + {4, 600, 1400, 0, 64, IPPROTO_ICMP, -1, 3}, + {4, 4, 1400, 0, 64, IPPROTO_ICMP, -1, -EINVAL}, + {4, 600, 1400, 1, 64, IPPROTO_ICMP, -1, -ENOTSUP}, + {4, 600, 1400, 0, 0, IPPROTO_ICMP, -1, 3}, + + {6, 1280, 1400, 0, 64, IPPROTO_ICMP, -1, 2}, + {6, 1300, 1400, 0, 64, IPPROTO_ICMP, -1, 2}, + {6, 4, 1400, 0, 64, IPPROTO_ICMP, -1, -EINVAL}, + {6, 1300, 1400, 0, 0, IPPROTO_ICMP, -1, 2}, + {6, 1280, 1400, 0, 64, IPPROTO_FRAGMENT, -1, -ENOTSUP}, + }; + + for (size_t i = 0; i < ARRAY_SIZE(tests); i++) { + int32_t len; + uint16_t pktid = tests[i].pkt_id; + struct rte_mbuf *pkts_out[BURST]; + struct rte_mbuf *b = rte_pktmbuf_alloc(pkt_pool); + + if (!b) + return TEST_FAILED; /* Serious error.. abort here */ + + if (tests[i].pkt_id == -1) + pktid = rte_rand_max(UINT16_MAX); + + if (tests[i].ipv == 4) { + if (v4_allocate_packet_of(b, 0x41414141, + tests[i].pkt_size, + tests[i].set_df, + tests[i].ttl, + tests[i].proto, + pktid)) + result = TEST_FAILED; + } else if (tests[i].ipv == 6) { + if (v6_allocate_packet_of(b, 0x41414141, + tests[i].pkt_size, + tests[i].ttl, + tests[i].proto, + pktid)) + result = TEST_FAILED; + } + + if (tests[i].ipv == 4) + len = rte_ipv4_fragment_packet(b, pkts_out, BURST, + tests[i].mtu_size, + direct_pool, + indirect_pool); + else + len = rte_ipv6_fragment_packet(b, pkts_out, BURST, + tests[i].mtu_size, + direct_pool, + indirect_pool); + + rte_pktmbuf_free(b); + + if (len > 0) + test_free_fragments(pkts_out, len); + + printf("%d: checking %d with %d\n", (int)i, len, + (int)tests[i].expected_frags); + RTE_TEST_ASSERT_EQUAL(len, tests[i].expected_frags, + "Failed case %u\n", (unsigned int)i); + + } + + return result; +} + +static struct unit_test_suite ipfrag_testsuite = { + .suite_name = "IP Frag Unit Test Suite", + .setup = testsuite_setup, + .teardown = testsuite_teardown, + .unit_test_cases = { + TEST_CASE_ST(ut_setup, ut_teardown, + test_ip_frag), + + TEST_CASES_END() /**< NULL terminate unit test array */ + } +}; + +static int +test_ipfrag(void) +{ + return unit_test_suite_runner(&ipfrag_testsuite); +} + +REGISTER_TEST_COMMAND(ipfrag_autotest, test_ipfrag);