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 F03F445B69; Fri, 18 Oct 2024 16:08:52 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id DF2F940E15; Fri, 18 Oct 2024 16:07:04 +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 DF5B040670 for ; Fri, 18 Oct 2024 16:07:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729260420; 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=W2Ou2VASZIAZPlYGx13kP5weCKMr7Czs0o+Ag02Yjtw=; b=Z6We/o9/x6HSuFnHgU8NZldzDlcGIkeFzC3oOce4IOnrxdlQcNwbjV4O6Fy7KIe5b9H4PN K9y+CH8yWq4dzhRXX/oNBfXI1CeLsifaSHPzqFmJI2c068sVHhxGswImgoj/5YUc0R6YOZ DZMrXqDTKbTJ+sQXl4pcnmFp+cnSr1U= Received: from mx-prod-mc-05.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-647-aIAEv36sOE-6BOOeRIPlfQ-1; Fri, 18 Oct 2024 10:06:57 -0400 X-MC-Unique: aIAEv36sOE-6BOOeRIPlfQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 303421955EB5; Fri, 18 Oct 2024 14:06:56 +0000 (UTC) Received: from ringo.redhat.com (unknown [10.39.208.23]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EB2B119560A3; Fri, 18 Oct 2024 14:06:54 +0000 (UTC) From: Robin Jarry To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH dpdk v5 16/17] net: add utilities for well known IPv6 address types Date: Fri, 18 Oct 2024 16:05:52 +0200 Message-ID: <20241018140553.79789-17-rjarry@redhat.com> In-Reply-To: <20241018140553.79789-1-rjarry@redhat.com> References: <20240821162516.610624-17-rjarry@redhat.com> <20241018140553.79789-1-rjarry@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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 Acked-by: Stephen Hemminger --- app/test/test_net_ip6.c | 68 +++++++++++++ lib/net/rte_ip6.h | 209 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 277 insertions(+) diff --git a/app/test/test_net_ip6.c b/app/test/test_net_ip6.c index dcc80c1309d1..94033421ad0b 100644 --- a/app/test/test_net_ip6.c +++ b/app/test/test_net_ip6.c @@ -81,20 +81,85 @@ 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), ""); const struct rte_ipv6_addr ucast = RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x6239, 0xe1f4, 0x7a0b, 0x2371); 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), ""); const struct rte_ipv6_addr mcast = RTE_IPV6(0xff01, 0, 0, 0, 0, 0, 0, 1); 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), ""); const struct rte_ipv6_addr lo = RTE_IPV6_ADDR_LOOPBACK; 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), ""); const struct rte_ipv6_addr local = RTE_IPV6(0xfe80, 0, 0, 0, 0x5a84, 0xc52c, 0x6aef, 0x4639); 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 = {{0x04, 0x7b, 0xcb, 0x5c, 0x08, 0x44}}; + const struct rte_ipv6_addr local_ip = + RTE_IPV6(0xfe80, 0, 0, 0, 0x047b, 0xcbff, 0xfe5c, 0x0844); + 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 = + RTE_IPV6(0xfe80, 0, 0, 0, 0x047b, 0xcbff, 0xfe5c, 0x0844); + const struct rte_ipv6_addr llocal_sol = + RTE_IPV6(0xff02, 0, 0, 0, 0, 0x0001, 0xff5c, 0x0844); + rte_ipv6_solnode_from_addr(&sol, &llocal); + TEST_ASSERT(rte_ipv6_addr_eq(&sol, &llocal_sol), ""); + + const struct rte_ipv6_addr ucast = + RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x1b9f, 0x8071, 0x67cd, 0xbf20); + const struct rte_ipv6_addr ucast_sol = + RTE_IPV6(0xff02, 0, 0, 0, 0, 0x0001, 0xffcd, 0xbf20); + 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 = {{0x33, 0x33, 0xd3, 0x00, 0x02, 0x01}}; + const struct rte_ipv6_addr mcast_ip = + RTE_IPV6(0xff02, 0, 0, 0x0201, 0, 0, 0xd300, 0x0201); + 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; } @@ -105,6 +170,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 25a77277f050..12d33034a3f9 100644 --- a/lib/net/rte_ip6.h +++ b/lib/net/rte_ip6.h @@ -28,6 +28,7 @@ #include #endif +#include #include #include #include @@ -248,6 +249,214 @@ rte_ipv6_addr_is_unspec(const struct rte_ipv6_addr *ip) /** Loopback IPv6 address as defined in RFC 4291, section 2.5.3. */ #define RTE_IPV6_ADDR_LOOPBACK RTE_IPV6(0, 0, 0, 0, 0, 0, 0, 1) +/** + * Check if an IPv6 address is the loopback address as defined in RFC 4291, + * section 2.5.3. + * + * @param ip + * The address to check. + * @return + * @c true if the address is the loopback address (all zeroes except the last bit). + */ +static inline bool +rte_ipv6_addr_is_loopback(const struct rte_ipv6_addr *ip) +{ + struct rte_ipv6_addr loopback = RTE_IPV6_ADDR_LOOPBACK; + return rte_ipv6_addr_eq(ip, &loopback); +} + +/** + * Check if an IPv6 address is link-local as defined in RFC 4291, section 2.5.6. + * + * @param ip + * The address to check. + * @return + * @c true if the address is a link-local address. + */ +static inline bool +rte_ipv6_addr_is_linklocal(const struct rte_ipv6_addr *ip) +{ + return ip->a[0] == 0xfe && (ip->a[1] & 0xc0) == 0x80; +} + +/** + * Check if an IPv6 address is site-local as defined in RFC 4291, section 2.5.7. + * + * @param ip + * The address to check. + * @return + * @c true if the address is a site-local address. + */ +static inline bool +rte_ipv6_addr_is_sitelocal(const struct rte_ipv6_addr *ip) +{ + return ip->a[0] == 0xfe && (ip->a[1] & 0xc0) == 0xc0; +} + +/** + * Check if an IPv6 address is an IPv4-compatible address as defined in RFC 4291, + * section 2.5.5.1. + * + * @param ip + * The address to check. + * @return + * @c true if the address is an IPv4-compatible address. + */ +static inline bool +rte_ipv6_addr_is_v4compat(const struct rte_ipv6_addr *ip) +{ + const struct rte_ipv6_addr unspec = RTE_IPV6_ADDR_UNSPEC; + return rte_ipv6_addr_eq_prefix(ip, &unspec, 32) && !rte_ipv6_addr_is_loopback(ip); +} + +#define RTE_IPV6_ADDR_PREFIX_V4MAPPED RTE_IPV6(0, 0, 0, 0, 0, 0xffff, 0, 0) + +/** + * Check if an IPv6 address is an IPv4-mapped address as defined in RFC 4291, + * section 2.5.5.2. + * + * @param ip + * The address to check. + * @return + * @c true if the address is an IPv4-mapped address. + */ +static inline bool +rte_ipv6_addr_is_v4mapped(const struct rte_ipv6_addr *ip) +{ + const struct rte_ipv6_addr prefix = RTE_IPV6_ADDR_PREFIX_V4MAPPED; + return rte_ipv6_addr_eq_prefix(ip, &prefix, 32); +} + +/** + * Check if an IPv6 address is multicast as defined in RFC 4291, section 2.7. + * + * @param ip + * The address to check. + * @return + * @c true if the address is multicast. + */ +static inline bool +rte_ipv6_addr_is_mcast(const struct rte_ipv6_addr *ip) +{ + return ip->a[0] == 0xff; +} + +/** + * IPv6 multicast scope values as defined in RFC 4291, section 2.7. + */ +enum rte_ipv6_mc_scope { + /** Invalid multicast scope. */ + RTE_IPV6_MC_SCOPE_NONE = 0x00, + /** Interface-local multicast scope. */ + RTE_IPV6_MC_SCOPE_IFACELOCAL = 0x01, + /** Link-local multicast scope. */ + RTE_IPV6_MC_SCOPE_LINKLOCAL = 0x02, + /** Site-local multicast scope. */ + RTE_IPV6_MC_SCOPE_SITELOCAL = 0x05, + /** Organizational-local multicast scope. */ + RTE_IPV6_MC_SCOPE_ORGLOCAL = 0x08, + /** Global multicast scope. */ + RTE_IPV6_MC_SCOPE_GLOBAL = 0x0e, +} __rte_packed; + +/** + * Extract the IPv6 multicast scope value as defined in RFC 4291, section 2.7. + * + * @param ip + * The address from which to get the multicast scope. + * @return + * The multicast scope of the address, or #RTE_IPV6_MC_SCOPE_NONE if the + * address is not multicast. + */ +static inline enum rte_ipv6_mc_scope +rte_ipv6_mc_scope(const struct rte_ipv6_addr *ip) +{ + if (!rte_ipv6_addr_is_mcast(ip)) + return RTE_IPV6_MC_SCOPE_NONE; + return (enum rte_ipv6_mc_scope)(ip->a[1] & 0x0f); +} + +/** @name Well known multicast addresses */ +/**@{*/ +/** Interface-local all-nodes multicast address as defined in RFC 4291, section 2.7.1. */ +#define RTE_IPV6_ADDR_ALLNODES_IFACE_LOCAL RTE_IPV6(0xff01, 0, 0, 0, 0, 0, 0, 1) +/** Link-local all-nodes multicast address as defined in RFC 4291, section 2.7.1. */ +#define RTE_IPV6_ADDR_ALLNODES_LINK_LOCAL RTE_IPV6(0xff02, 0, 0, 0, 0, 0, 0, 1) +/** Interface-local all-routers multicast address as defined in RFC 4291, section 2.7.1. */ +#define RTE_IPV6_ADDR_ALLROUTERS_IFACE_LOCAL RTE_IPV6(0xff01, 0, 0, 0, 0, 0, 0, 2) +/** Link-local all-routers multicast address as defined in RFC 4291, section 2.7.1. */ +#define RTE_IPV6_ADDR_ALLROUTERS_LINK_LOCAL RTE_IPV6(0xff02, 0, 0, 0, 0, 0, 0, 2) +/** Site-local all-routers multicast address as defined in RFC 4291, section 2.7.1. */ +#define RTE_IPV6_ADDR_ALLROUTERS_SITE_LOCAL RTE_IPV6(0xff05, 0, 0, 0, 0, 0, 0, 2) +/**@}*/ + +/* + * Generate a link-local IPv6 address from an Ethernet address as specified in + * RFC 2464, section 5. + * + * @param[out] ip + * The link-local IPv6 address to generate. + * @param[in] mac + * An Ethernet address. + */ +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. + * + * @param[out] sol + * The IPv6 solicited-node multicast address to generate. + * @param[in] ip + * A unicast or anycast address. + */ +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. + * + * @param[out] mac + * The multicast Ethernet address to generate. + * @param[in] ip + * A multicast IPv6 address. + */ +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]; +} + /** * IPv6 Header */ -- 2.47.0