From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 57A9745834; Wed, 21 Aug 2024 18:28:33 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0453642D0C; Wed, 21 Aug 2024 18:28:12 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 3444F427C3 for ; Wed, 21 Aug 2024 18:28:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724257689; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GOLqDajYBsUBOCHoJnWTUK8g9sYF+lIh+vdVG0pCCeo=; b=a1/Ijjq+U3C81tNZyBo49p6ZQtW/oXxmaLPaOLyv3dukzZw508Hwggsl2XVNfRMSywG6A6 gzThs5HYsn0ekDOZJBTYbo9HMtyEBOzvu0eXsPS8z9rQ0+Mixb8ZEAIeTlpNiublU1uv6j TMb2xQRV+7maR/qTBCc8Ju1IeXiAaEo= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-249-wwRsxDfSPyGQgIYu2v7szw-1; Wed, 21 Aug 2024 12:28:08 -0400 X-MC-Unique: wwRsxDfSPyGQgIYu2v7szw-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D2B34190151E for ; Wed, 21 Aug 2024 16:28:03 +0000 (UTC) Received: from localhost.localdomain (unknown [10.39.208.21]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9372B18DD82E; Wed, 21 Aug 2024 16:27:32 +0000 (UTC) From: Robin Jarry To: dev@dpdk.org Subject: [PATCH dpdk v1 15/15] net: add utilities for well known ipv6 address types Date: Wed, 21 Aug 2024 18:25:32 +0200 Message-ID: <20240821162516.610624-32-rjarry@redhat.com> In-Reply-To: <20240821162516.610624-17-rjarry@redhat.com> References: <20240821162516.610624-17-rjarry@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Add more utilities to work with IPv6 addresses. These functions will be required in order to help building IPv6 routing applications. Signed-off-by: Robin Jarry --- app/test/test_net_ipv6.c | 74 ++++++++++++++++++++++++++++++ lib/net/rte_ip6.h | 98 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) diff --git a/app/test/test_net_ipv6.c b/app/test/test_net_ipv6.c index c2b42d67285e..b087b5c60d73 100644 --- a/app/test/test_net_ipv6.c +++ b/app/test/test_net_ipv6.c @@ -93,26 +93,97 @@ static int test_ipv6_addr_kind(void) { TEST_ASSERT(rte_ipv6_addr_is_unspec(&zero_addr), ""); + TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&zero_addr), ""); + TEST_ASSERT(!rte_ipv6_addr_is_loopback(&zero_addr), ""); + TEST_ASSERT(!rte_ipv6_addr_is_mcast(&zero_addr), ""); struct rte_ipv6_addr ucast = { "\x2a\x01\xcb\x00\x02\x54\x33\x00\x62\x39\xe1\xf4\x7a\x0b\x23\x71" }; TEST_ASSERT(!rte_ipv6_addr_is_unspec(&ucast), ""); + TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&ucast), ""); + TEST_ASSERT(!rte_ipv6_addr_is_loopback(&ucast), ""); + TEST_ASSERT(!rte_ipv6_addr_is_mcast(&ucast), ""); struct rte_ipv6_addr mcast = { "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" }; TEST_ASSERT(!rte_ipv6_addr_is_unspec(&mcast), ""); + TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&mcast), ""); + TEST_ASSERT(!rte_ipv6_addr_is_loopback(&mcast), ""); + TEST_ASSERT(rte_ipv6_addr_is_mcast(&mcast), ""); struct rte_ipv6_addr lo = { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" }; TEST_ASSERT(!rte_ipv6_addr_is_unspec(&lo), ""); + TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&lo), ""); + TEST_ASSERT(rte_ipv6_addr_is_loopback(&lo), ""); + TEST_ASSERT(!rte_ipv6_addr_is_mcast(&lo), ""); struct rte_ipv6_addr local = { "\xfe\x80\x00\x00\x00\x00\x00\x00\x5a\x84\xc5\x2c\x6a\xef\x46\x39" }; TEST_ASSERT(!rte_ipv6_addr_is_unspec(&local), ""); + TEST_ASSERT(rte_ipv6_addr_is_linklocal(&local), ""); + TEST_ASSERT(!rte_ipv6_addr_is_loopback(&local), ""); + TEST_ASSERT(!rte_ipv6_addr_is_mcast(&local), ""); + + return TEST_SUCCESS; +} + +static int +test_ipv6_llocal_from_ethernet(void) +{ + const struct rte_ether_addr local_mac = { "\x04\x7b\xcb\x5c\x08\x44" }; + const struct rte_ipv6_addr local_ip = { + "\xfe\x80\x00\x00\x00\x00\x00\x00\x04\x7b\xcb\xff\xfe\x5c\x08\x44" + }; + struct rte_ipv6_addr ip; + + rte_ipv6_llocal_from_ethernet(&ip, &local_mac); + TEST_ASSERT(rte_ipv6_addr_eq(&ip, &local_ip), ""); + + return TEST_SUCCESS; +} + +static int +test_ipv6_solnode_from_addr(void) +{ + struct rte_ipv6_addr sol; + + const struct rte_ipv6_addr llocal = { + "\xfe\x80\x00\x00\x00\x00\x00\x00\x04\x7b\xcb\xff\xfe\x5c\x08\x44" + }; + const struct rte_ipv6_addr llocal_sol = { + "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\x5c\x08\x44" + }; + rte_ipv6_solnode_from_addr(&sol, &llocal); + TEST_ASSERT(rte_ipv6_addr_eq(&sol, &llocal_sol), ""); + + const struct rte_ipv6_addr ucast = { + "\x2a\x01\xcb\x00\x02\x54\x33\x00\x1b\x9f\x80\x71\x67\xcd\xbf\x20" + }; + const struct rte_ipv6_addr ucast_sol = { + "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\xcd\xbf\x20" + }; + rte_ipv6_solnode_from_addr(&sol, &ucast); + TEST_ASSERT(rte_ipv6_addr_eq(&sol, &ucast_sol), ""); + + return TEST_SUCCESS; +} + +static int +test_ether_mcast_from_ipv6(void) +{ + const struct rte_ether_addr mcast_mac = { "\x33\x33\xd3\x00\x02\x01" }; + const struct rte_ipv6_addr mcast_ip = { + "\xff\x02\x00\x00\x00\x00\x02\x01\x00\x00\x00\x00\xd3\x00\x02\x01" + }; + struct rte_ether_addr mac; + + rte_ether_mcast_from_ipv6(&mac, &mcast_ip); + TEST_ASSERT(rte_is_same_ether_addr(&mac, &mcast_mac), ""); return TEST_SUCCESS; } @@ -123,6 +194,9 @@ test_net_ipv6(void) TEST_ASSERT_SUCCESS(test_ipv6_addr_mask(), ""); TEST_ASSERT_SUCCESS(test_ipv6_addr_eq_prefix(), ""); TEST_ASSERT_SUCCESS(test_ipv6_addr_kind(), ""); + TEST_ASSERT_SUCCESS(test_ipv6_llocal_from_ethernet(), ""); + TEST_ASSERT_SUCCESS(test_ipv6_solnode_from_addr(), ""); + TEST_ASSERT_SUCCESS(test_ether_mcast_from_ipv6(), ""); return TEST_SUCCESS; } diff --git a/lib/net/rte_ip6.h b/lib/net/rte_ip6.h index 6bc18a1c8dd6..d7eba63fe111 100644 --- a/lib/net/rte_ip6.h +++ b/lib/net/rte_ip6.h @@ -28,6 +28,7 @@ #include #endif +#include #include #include #include @@ -157,6 +158,103 @@ rte_ipv6_addr_is_unspec(const struct rte_ipv6_addr *ip) return rte_ipv6_addr_eq(ip, &unspec); } +/** + * Check if an IPv6 address is the loopback address as defined in RFC 4291, section 2.5.3. + */ +static inline bool +rte_ipv6_addr_is_loopback(const struct rte_ipv6_addr *ip) +{ + static const struct rte_ipv6_addr loopback = { + .a = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01} + }; + return rte_ipv6_addr_eq(ip, &loopback); +} + +/** + * Check if an IPv6 address is link-local as defined in RFC 4291, section 2.5.6. + */ +static inline bool +rte_ipv6_addr_is_linklocal(const struct rte_ipv6_addr *ip) +{ + static const struct rte_ipv6_addr local = { + .a = {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + }; + return rte_ipv6_addr_eq_prefix(ip, &local, 64); +} + +/** + * Check if an IPv6 address is multicast as defined in RFC 4291, section 2.7. + */ +static inline bool +rte_ipv6_addr_is_mcast(const struct rte_ipv6_addr *ip) +{ + return ip->a[0] == 0xff; +} + +/** + * Generate a link-local IPv6 address from an ethernet address as specified in + * RFC 2464, section 5. + */ +static inline void +rte_ipv6_llocal_from_ethernet(struct rte_ipv6_addr *ip, const struct rte_ether_addr *mac) +{ + ip->a[0] = 0xfe; + ip->a[1] = 0x80; + memset(&ip->a[2], 0, 6); + ip->a[8] = mac->addr_bytes[0]; + ip->a[9] = mac->addr_bytes[1]; + ip->a[10] = mac->addr_bytes[2]; + ip->a[11] = 0xff; + ip->a[12] = 0xfe; + ip->a[13] = mac->addr_bytes[3]; + ip->a[14] = mac->addr_bytes[4]; + ip->a[15] = mac->addr_bytes[5]; +} + +/** + * Convert a unicast or anycast IPv6 address to a solicited-node multicast + * address as defined in RFC 4291, section 2.7.1. + */ +static inline void +rte_ipv6_solnode_from_addr(struct rte_ipv6_addr *sol, const struct rte_ipv6_addr *ip) +{ + sol->a[0] = 0xff; + sol->a[1] = 0x02; + memset(&sol->a[2], 0, 9); + sol->a[11] = 0x01; + sol->a[12] = 0xff; + sol->a[13] = ip->a[13]; + sol->a[14] = ip->a[14]; + sol->a[15] = ip->a[15]; +} + +/** + * Generate a multicast ethernet address from a multicast IPv6 address as defined + * in RFC 2464, section 7. + */ +static inline void +rte_ether_mcast_from_ipv6(struct rte_ether_addr *mac, const struct rte_ipv6_addr *ip) +{ + mac->addr_bytes[0] = 0x33; + mac->addr_bytes[1] = 0x33; + mac->addr_bytes[2] = ip->a[12]; + mac->addr_bytes[3] = ip->a[13]; + mac->addr_bytes[4] = ip->a[14]; + mac->addr_bytes[5] = ip->a[15]; +} + +/** Well known multicast addresses as defined in RFC 4291, section 2.7.1. */ +#define RTE_IPV6_ADDR_ALLNODES_IFACE_LOCAL \ + ((struct rte_ipv6_addr){.a = {0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}) +#define RTE_IPV6_ADDR_ALLNODES_LINK_LOCAL \ + ((struct rte_ipv6_addr){.a = {0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}) +#define RTE_IPV6_ADDR_ALLROUTERS_IFACE_LOCAL \ + ((struct rte_ipv6_addr){.a = {0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}) +#define RTE_IPV6_ADDR_ALLROUTERS_LINK_LOCAL \ + ((struct rte_ipv6_addr){.a = {0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}) +#define RTE_IPV6_ADDR_ALLROUTERS_SITE_LOCAL \ + ((struct rte_ipv6_addr){.a = {0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}) + /** * IPv6 Header */ -- 2.46.0