DPDK patches and discussions
 help / color / mirror / Atom feed
From: Robin Jarry <rjarry@redhat.com>
To: dev@dpdk.org
Subject: [PATCH dpdk v2 15/16] net: add utilities for well known ipv6 address types
Date: Tue,  1 Oct 2024 10:17:27 +0200	[thread overview]
Message-ID: <20241001081728.301272-16-rjarry@redhat.com> (raw)
In-Reply-To: <20241001081728.301272-1-rjarry@redhat.com>

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 <rjarry@redhat.com>
---

Notes:
    v2:
    
    - added more multicast helpers
    - fixed all routers well known multicast addresses

 app/test/test_net_ipv6.c |  74 +++++++++++++++++++
 lib/net/rte_ip6.h        | 149 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 223 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 c139193e93f3..c552fa54c095 100644
--- a/lib/net/rte_ip6.h
+++ b/lib/net/rte_ip6.h
@@ -28,6 +28,7 @@
 #include <netinet/ip6.h>
 #endif
 
+#include <rte_ether.h>
 #include <rte_byteorder.h>
 #include <rte_mbuf.h>
 #include <rte_memcpy.h>
@@ -162,6 +163,154 @@ rte_ipv6_addr_is_unspec(const struct rte_ipv6_addr *ip)
 	return rte_ipv6_addr_eq(ip, &unspec);
 }
 
+/** Loopback address as defined in RFC 4291, section 2.7.1. */
+#define RTE_IPV6_ADDR_LOOPBACK RTE_IPV6_ADDR(0, 0, 0, 0, 0, 0, 0, 0, 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.
+ */
+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.
+ */
+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.
+ */
+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.
+ */
+static inline bool
+rte_ipv6_addr_is_v4compat(const struct rte_ipv6_addr *ip)
+{
+	const rte_be32_t *a32 = (const rte_be32_t *)ip;
+	return a32[0] == 0 && a32[1] == 0 && a32[2] == 0 && a32[3] != 0 && a32[3] != RTE_BE32(1);
+}
+
+/**
+ * Check if an IPv6 address is an IPv4-mapped address as defined in RFC 4291,
+ * section 2.5.5.2.
+ */
+static inline bool
+rte_ipv6_addr_is_v4mapped(const struct rte_ipv6_addr *ip)
+{
+	const rte_be32_t *a32 = (const rte_be32_t *)ip;
+	return a32[0] == 0 && a32[1] == 0 && a32[2] == RTE_BE32(0x0000ffff);
+}
+
+/**
+ * IPv6 multicast scope values as defined in RFC 4291, section 2.7.
+ */
+typedef enum {
+	RTE_IPV6_MC_SCOPE_RESERVED = 0x00,
+	RTE_IPV6_MC_SCOPE_IFACELOCAL = 0x01,
+	RTE_IPV6_MC_SCOPE_LINKLOCAL = 0x02,
+	RTE_IPV6_MC_SCOPE_SITELOCAL = 0x05,
+	RTE_IPV6_MC_SCOPE_ORGLOCAL = 0x08,
+	RTE_IPV6_MC_SCOPE_GLOBAL = 0x0e,
+} __rte_packed rte_ipv6_mc_scope_t;
+
+/**
+ * Extract the IPv6 multicast scope value as defined in RFC 4291, section 2.7.
+ */
+static inline rte_ipv6_mc_scope_t
+rte_ipv6_mc_scope(const struct rte_ipv6_addr *ip)
+{
+	return (rte_ipv6_mc_scope_t)(ip->a[1] & 0x0f);
+}
+
+/**
+ * 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;
+}
+
+/** Well known multicast addresses as defined in RFC 4291, section 2.7.1. */
+#define RTE_IPV6_ADDR_ALLNODES_IFACE_LOCAL \
+	RTE_IPV6_ADDR(0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
+#define RTE_IPV6_ADDR_ALLNODES_LINK_LOCAL \
+	RTE_IPV6_ADDR(0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
+#define RTE_IPV6_ADDR_ALLROUTERS_IFACE_LOCAL \
+	RTE_IPV6_ADDR(0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2)
+#define RTE_IPV6_ADDR_ALLROUTERS_LINK_LOCAL \
+	RTE_IPV6_ADDR(0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2)
+#define RTE_IPV6_ADDR_ALLROUTERS_SITE_LOCAL \
+	RTE_IPV6_ADDR(0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2)
+
+/**
+ * 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];
+}
+
 /**
  * IPv6 Header
  */
-- 
2.46.1


  parent reply	other threads:[~2024-10-01  8:19 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-21 16:25 [PATCH dpdk v1 00/15] IPv6 APIs overhaul Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 01/15] net: split raw checksum functions in separate header Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 02/15] net: split ipv6 symbols " Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 03/15] net: add structure for ipv6 addresses Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 04/15] net: use ipv6 structure for header addresses Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 05/15] fib6,rib6,lpm6: use ipv6 addr struct Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 06/15] net: add ipv6 address utilities Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 07/15] fib6,rib6,lpm6: use ipv6 utils Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 08/15] graph,node: use ipv6 addr struct and utils Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 09/15] pipeline: use ipv6 addr struct Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 10/15] ipsec: " Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 11/15] thash: " Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 12/15] gro: " Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 13/15] rte_flow: " Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 14/15] rib6,fib6,lpm6: remove duplicate constants Robin Jarry
2024-08-21 16:25 ` [PATCH dpdk v1 15/15] net: add utilities for well known ipv6 address types Robin Jarry
2024-08-21 22:28 ` [PATCH dpdk v1 00/15] IPv6 APIs overhaul Morten Brørup
2024-08-22 14:13 ` Stephen Hemminger
2024-08-22 15:13   ` Morten Brørup
2024-08-22 15:27     ` Robin Jarry
2024-08-22 18:41       ` Morten Brørup
2024-08-22 15:14   ` Robin Jarry
2024-08-22 15:16   ` Robin Jarry
2024-10-01  8:17 ` [PATCH dpdk v2 00/16] " Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 01/16] net: split raw checksum functions in separate header Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 02/16] net: split ipv6 symbols " Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 03/16] net: add structure for ipv6 addresses Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 04/16] net: use ipv6 structure for header addresses Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 05/16] fib6,rib6,lpm6: use ipv6 addr struct Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 06/16] net: add ipv6 address utilities Robin Jarry
2024-10-01 15:35     ` Stephen Hemminger
2024-10-01  8:17   ` [PATCH dpdk v2 07/16] fib6,rib6,lpm6: use ipv6 utils Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 08/16] graph,node: use ipv6 addr struct and utils Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 09/16] pipeline: use ipv6 addr struct Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 10/16] ipsec: " Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 11/16] thash: " Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 12/16] gro: " Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 13/16] rte_flow: " Robin Jarry
2024-10-01  8:17   ` [PATCH dpdk v2 14/16] rib6,fib6,lpm6: remove duplicate constants Robin Jarry
2024-10-01  8:17   ` Robin Jarry [this message]
2024-10-01  8:17   ` [PATCH dpdk v2 16/16] ipv6: add function to check ipv6 version Robin Jarry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241001081728.301272-16-rjarry@redhat.com \
    --to=rjarry@redhat.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).