Skip to contentSkip to conversation

net: introduce generic type and helpers for IP address

RFC PATCH In review Cong Wang 8 participants 25 messages Jun 27, 2013 – Jul 1, 2013

Subsystems (13)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

cover net: introduce generic type and helpers for IP address

As IPv6 becomes popular, more and more subsystems begin to support IPv6, therefore we need a generic IP address type, in case of duplicates. Also we will also need some helpers to compare, print, check the generic IP address.

This patchset introduce a new type union inet_addr as a union of IPv4 and IPv6 address, and some helper functions that will be used by existing code and in the future VXLAN module.

This patchset only does compile test, since it is still RFC.

Cong Wang (5):
net: introduce generic union inet_addr
net: introduce generic inet_pton()
inetpeer: use generic union inet_addr
sunrpc: use generic union inet_addr nfs,cifs: abstract generic inet_addr_equal_strict()

 Documentation/printk-formats.txt |    9 ++
 drivers/net/netconsole.c         |   22 +++---
 fs/cifs/connect.c                |   38 ++-------
 fs/dlm/lowcomms.c                |   24 +-----
 fs/nfs/client.c                  |   94 +--------------------
 fs/nfs/nfs4filelayoutdev.c       |   37 +--------
 fs/nfs/super.c                   |   31 +-------
 include/linux/netpoll.h          |    9 +--
 include/linux/sunrpc/addr.h      |  119 ++-------------------------
 include/net/inet_addr.h          |  164 ++++++++++++++++++++++++++++++++++++++
 include/net/inetpeer.h           |   29 ++-----
 lib/vsprintf.c                   |   18 ++++-
 net/core/netpoll.c               |   74 ++++++-----------
 net/ipv4/inetpeer.c              |   35 +++++---
 net/ipv4/tcp_metrics.c           |   92 +++++++++-------------
 15 files changed, 327 insertions(+), 468 deletions(-)
 create mode 100644 include/net/inet_addr.h
Signature · 1 line
1.7.7.6
author
#1 lore raw

RFC PATCH 1/5 net: introduce generic union inet_addr

Cc Daniel Borkmann <[email protected]>
---
 Documentation/printk-formats.txt |    9 +++++
 drivers/net/netconsole.c         |   22 ++++++-------
 include/linux/netpoll.h          |    9 +-----
 include/net/inet_addr.h          |   62 ++++++++++++++++++++++++++++++++++++++
 lib/vsprintf.c                   |   18 ++++++++++-
 net/core/netpoll.c               |   52 ++++++++++++++-----------------
 6 files changed, 122 insertions(+), 50 deletions(-)
 create mode 100644 include/net/inet_addr.h
Documentation/printk-formats.txt +9 -0
index 3af5ae6..26ff336 100644
@@ -121,6 +121,15 @@ IPv6 addresses:
 	print a compressed IPv6 address as described by
 	http://tools.ietf.org/html/rfc5952
 
+Generic IP addresses:
+
+	%pIg
+	%pig
+
+	For printing genernic IP address, which has union inet_addr type.
+	The 'Ig' specifier is same with 'I4' (for IPv4 addresses) or 'I6'
+	(for IPv6 address), 'ig' is same with 'i4' or 'i6'.
+
 UUID/GUID addresses:
 
 	%pUb	00010203-0405-0607-0809-0a0b0c0d0e0f
drivers/net/netconsole.c +10 -12
index 1d1d0a1..02d7389 100644
@@ -269,18 +269,12 @@ static ssize_t show_remote_port(struct netconsole_target *nt, char *buf)
 
 static ssize_t show_local_ip(struct netconsole_target *nt, char *buf)
 {
-	if (nt->np.ipv6)
-		return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6);
-	else
-		return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip);
+	return snprintf(buf, PAGE_SIZE, "%pIg\n", &nt->np.local_ip);
 }
 
 static ssize_t show_remote_ip(struct netconsole_target *nt, char *buf)
 {
-	if (nt->np.ipv6)
-		return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6);
-	else
-		return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip);
+	return snprintf(buf, PAGE_SIZE, "%pIg\n", &nt->np.remote_ip);
 }
 
 static ssize_t show_local_mac(struct netconsole_target *nt, char *buf)
@@ -418,17 +412,19 @@ static ssize_t store_local_ip(struct netconsole_target *nt,
 
 	if (strnchr(buf, count, ':')) {
 		const char *end;
-		if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
+		if (in6_pton(buf, count, nt->np.local_ip.sin6.sin6_addr.s6_addr, -1, &end) > 0) {
 			if (*end && *end != '\n') {
 				printk(KERN_ERR "netconsole: invalid IPv6 address at: <%c>\n", *end);
 				return -EINVAL;
 			}
+			nt->np.local_ip.sa.sa_family = AF_INET6;
 			nt->np.ipv6 = true;
 		} else
 			return -EINVAL;
 	} else {
 		if (!nt->np.ipv6) {
-			nt->np.local_ip.ip = in_aton(buf);
+			nt->np.local_ip.sin.sin_addr.s_addr = in_aton(buf);
+			nt->np.local_ip.sa.sa_family = AF_INET;
 		} else
 			return -EINVAL;
 	}
@@ -449,17 +445,19 @@ static ssize_t store_remote_ip(struct netconsole_target *nt,
 
 	if (strnchr(buf, count, ':')) {
 		const char *end;
-		if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
+		if (in6_pton(buf, count, nt->np.remote_ip.sin6.sin6_addr.s6_addr, -1, &end) > 0) {
 			if (*end && *end != '\n') {
 				printk(KERN_ERR "netconsole: invalid IPv6 address at: <%c>\n", *end);
 				return -EINVAL;
 			}
+			nt->np.remote_ip.sa.sa_family = AF_INET6;
 			nt->np.ipv6 = true;
 		} else
 			return -EINVAL;
 	} else {
 		if (!nt->np.ipv6) {
-			nt->np.remote_ip.ip = in_aton(buf);
+			nt->np.remote_ip.sin.sin_addr.s_addr = in_aton(buf);
+			nt->np.remote_ip.sa.sa_family = AF_INET;
 		} else
 			return -EINVAL;
 	}
include/linux/netpoll.h +1 -8
index f3c7c24..3884834 100644
@@ -11,14 +11,7 @@
 #include <linux/interrupt.h>
 #include <linux/rcupdate.h>
 #include <linux/list.h>
-
-union inet_addr {
-	__u32		all[4];
-	__be32		ip;
-	__be32		ip6[4];
-	struct in_addr	in;
-	struct in6_addr	in6;
-};
+#include <net/inet_addr.h>
 
 struct netpoll {
 	struct net_device *dev;
include/net/inet_addr.h new file +62 -0

62 lines not on this page · raw message

lib/vsprintf.c +16 -2
index e149c64..6bcc7b9 100644
@@ -27,6 +27,7 @@
 #include <linux/uaccess.h>
 #include <linux/ioport.h>
 #include <net/addrconf.h>
+#include <net/inet_addr.h>
 
 #include <asm/page.h>		/* for PAGE_SIZE */
 #include <asm/sections.h>	/* for dereference_function_descriptor() */
@@ -1004,10 +1005,10 @@ int kptr_restrict __read_mostly;
  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
  *       with a dash-separated hex notation
  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
- * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
+ * - 'I' [46g] for IPv4/IPv6 addresses printed in the usual way
  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
- * - 'i' [46] for 'raw' IPv4/IPv6 addresses
+ * - 'i' [46g] for 'raw' IPv4/IPv6 addresses
  *       IPv6 omits the colons (01020304...0f)
  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
  * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
@@ -1093,6 +1094,17 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 			return ip6_addr_string(buf, end, ptr, spec, fmt);
 		case '4':
 			return ip4_addr_string(buf, end, ptr, spec, fmt);
+		case 'g': {
+				union inet_addr *ia = ptr;
+
+				if (ia->sa.sa_family == AF_INET6) {
+					ptr = &ia->sin6.sin6_addr;
+					return ip6_addr_string(buf, end, ptr, spec, fmt);
+				} else {
+					ptr = &ia->sin.sin_addr.s_addr;
+					return ip4_addr_string(buf, end, ptr, spec, fmt);
+				}
+			}
 		}
 		break;
 	case 'U':
@@ -1370,6 +1382,8 @@ qualifier:
  * %pI6 print an IPv6 address with colons
  * %pi6 print an IPv6 address without colons
  * %pI6c print an IPv6 address as specified by RFC 5952
+ * %pIg print generic IP address of union inet_addr, either %pI4 or %pI6
+ * %pig print generic IP address of union inet_addr, either %pi4 or %pi6
  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
  *   case.
  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
net/core/netpoll.c +24 -28

140 lines not on this page · raw message

Signature · 1 line
1.7.7.6
#2 lore raw

Re: [RFC Patch net-next 1/5] net: introduce generic union inet_addr

Quoted 2 lines · Cong Wang
Cc Daniel Borkmann <[email protected]>

I was about to answer for the Daniel's patch about %pig.

Daniel, could you resend your patch series to the LKML, since it touches lib/vsprintf.c.
Also, regarding to your patch 2/2, could it be possible to split it to two parts: first substitutes SCTP macros not related to IP addresses and second one is explicitly targeting against SCTP_DEBUG_PRINTK_IPADDR ? By the way, in some places in 2/2 you didn't change open coded function name to the '"%s: ...", __func__, ...'.

Cong, I don't think is a good idea to update lib/ code and net/ code in one patch, since that are logically a bit different. lib/ code sounds more common, it's better if it leads separately this series.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#3 lore raw

Re: [RFC Patch net-next 1/5] net: introduce generic union inet_addr

Quoted 8 lines · Andy Shevchenko
Quoted 2 lines · Cong Wang
Cc Daniel Borkmann <[email protected]>

I was about to answer for the Daniel's patch about %pig.

Daniel, could you resend your patch series to the LKML, since it touches lib/vsprintf.c.

Agreed. Will put lkml into CC for the vsprintf change.

Quoted 3 lines

Also, regarding to your patch 2/2, could it be possible to split it to two parts: first substitutes SCTP macros not related to IP addresses and second one is explicitly targeting against SCTP_DEBUG_PRINTK_IPADDR ?

Well, as Vlad, one of the SCTP maintainers, already went through this patch and I've already applied the few changes he requested, I'd like not to put too much burden on yet another round of review, as the rest of the code stays as is.

Quoted 2 lines

By the way, in some places in 2/2 you didn't change open coded function name to the '"%s: ...", __func__, ...'.

Right, but those messages are rather more of generic nature. I went through all of this in detail, and I think it's okay like that.

Quoted 3 lines

Cong, I don't think is a good idea to update lib/ code and net/ code in one patch, since that are logically a bit different. lib/ code sounds more common, it's better if it leads separately this series.

author
#5 lore raw

Re: [RFC Patch net-next 1/5] net: introduce generic union inet_addr

Quoted 4 lines · Joe Perches
Quoted 2 lines · Cong Wang
Cc Daniel Borkmann <[email protected]>
Quoted 9 lines

Hi Cong.

I think this is premature.

Please let the discussion and implementation
for %pI[gS]<foo> play out and be accepted
into next before using it in other subsystems.

Yeah, I already talked with Daniel off-list, and I agree his %pIS patch should go first.

(Also I apologized to him for forgetting to credit him when I submitted this patchset.)

author
#6 lore raw

RFC PATCH 2/5 net: introduce generic inet_pton()

In reply to Cong Wang
---
 include/net/inet_addr.h |   20 ++++++++++++++++++++
 net/core/netpoll.c      |   24 ++----------------------
 2 files changed, 22 insertions(+), 22 deletions(-)
include/net/inet_addr.h +20 -0
index 66a16fe..1379287 100644
@@ -4,6 +4,7 @@
 #include <linux/in.h>
 #include <linux/in6.h>
 #include <linux/socket.h>
+#include <linux/inet.h>
 #include <net/addrconf.h>
 
 union inet_addr {
@@ -59,4 +60,23 @@ static inline bool inet_addr_multicast(const union inet_addr *ipa)
 }
 #endif
 
+static inline int inet_pton(const char *str, union inet_addr *addr)
+{
+	const char *end;
+
+	if (!strchr(str, ':') &&
+	    in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
+		if (!*end)
+			return 0;
+	}
+	if (in6_pton(str, -1, addr->sin6.sin6_addr.s6_addr, -1, &end) > 0) {
+#if IS_ENABLED(CONFIG_IPV6)
+		if (!*end)
+			return 1;
+#else
+		return -1;
+#endif
+	}
+	return -1;
+}
 #endif
net/core/netpoll.c +2 -22
index c300358..6631b5e 100644
@@ -907,26 +907,6 @@ void netpoll_print_options(struct netpoll *np)
 }
 EXPORT_SYMBOL(netpoll_print_options);
 
-static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
-{
-	const char *end;
-
-	if (!strchr(str, ':') &&
-	    in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
-		if (!*end)
-			return 0;
-	}
-	if (in6_pton(str, -1, addr->sin6.sin6_addr.s6_addr, -1, &end) > 0) {
-#if IS_ENABLED(CONFIG_IPV6)
-		if (!*end)
-			return 1;
-#else
-		return -1;
-#endif
-	}
-	return -1;
-}
-
 int netpoll_parse_options(struct netpoll *np, char *opt)
 {
 	char *cur=opt, *delim;
@@ -946,7 +926,7 @@ int netpoll_parse_options(struct netpoll *np, char *opt)
 		if ((delim = strchr(cur, '/')) == NULL)
 			goto parse_failed;
 		*delim = 0;
-		ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
+		ipv6 = inet_pton(cur, &np->local_ip);
 		if (ipv6 < 0)
 			goto parse_failed;
 		else
@@ -982,7 +962,7 @@ int netpoll_parse_options(struct netpoll *np, char *opt)
 	if ((delim = strchr(cur, '/')) == NULL)
 		goto parse_failed;
 	*delim = 0;
-	ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
+	ipv6 = inet_pton(cur, &np->remote_ip);
 	if (ipv6 < 0)
 		goto parse_failed;
 	else if (np->ipv6 != (bool)ipv6)
Signature · 1 line
1.7.7.6
#7 lore raw

Re: [RFC Patch net-next 2/5] net: introduce generic inet_pton()

Hello.

Quoted 24 lines · Cong Wang
---
  include/net/inet_addr.h |   20 ++++++++++++++++++++
  net/core/netpoll.c      |   24 ++----------------------
  2 files changed, 22 insertions(+), 22 deletions(-)
include/net/inet_addr.h +3 -0
index 66a16fe..1379287 100644
@@ -4,6 +4,7 @@
  #include <linux/in.h>
  #include <linux/in6.h>
  #include <linux/socket.h>
+#include <linux/inet.h>
  #include <net/addrconf.h>
 
  union inet_addr {
@@ -59,4 +60,23 @@ static inline bool inet_addr_multicast(const union inet_addr *ipa)
  }
  #endif
 
+static inline int inet_pton(const char *str, union inet_addr *addr)
+{

include/net/inet_addr.h static inline bool inet_addr_multicast(const union inet_addr *ipa) · line 74

Quoted 10 lines
include/net/inet_addr.h +10 -0
+	const char *end;
+
+	if (!strchr(str, ':') &&
+	    in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
+		if (!*end)
+			return 0;
+	}
+	if (in6_pton(str, -1, addr->sin6.sin6_addr.s6_addr, -1, &end) > 0) {
+#if IS_ENABLED(CONFIG_IPV6)
+		if (!*end)

How about:

		if (IS_ENABLED(CONFIG_IPV6) && !*end)

include/net/inet_addr.h static inline bool inet_addr_multicast(const union inet_addr *ipa) · line 82

Quoted 8 lines
include/net/inet_addr.h +7 -0
+			return 1;
+#else
+		return -1;
+#endif
+	}
+	return -1;
+}
  #endif

WBR, Sergei

#8 lore raw

Re: [RFC Patch net-next 2/5] net: introduce generic inet_pton()

Quoted 10 lines · Sergei Shtylyov
Quoted 10 lines
---
  include/net/inet_addr.h |   20 ++++++++++++++++++++
  net/core/netpoll.c      |   24 ++----------------------
  2 files changed, 22 insertions(+), 22 deletions(-)
include/net/inet_addr.h +0 -0
index 66a16fe..1379287 100644

[...]

Quoted 5 lines
Quoted 5 lines
include/net/inet_addr.h +0 -0
@@ -59,4 +60,23 @@ static inline bool inet_addr_multicast(const union
 inet_addr *ipa)
  }
  #endif
 
Quoted 19 lines

include/net/inet_addr.h static inline bool inet_addr_multicast(const union inet_addr *ipa) · line 74

Quoted 12 lines
include/net/inet_addr.h +12 -0
+static inline int inet_pton(const char *str, union inet_addr *addr)
+{
+    const char *end;
+
+    if (!strchr(str, ':') &&
+        in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
+        if (!*end)
+            return 0;
+    }
+    if (in6_pton(str, -1, addr->sin6.sin6_addr.s6_addr, -1, &end) > 0) {
+#if IS_ENABLED(CONFIG_IPV6)
+        if (!*end)
    How about:

         if (IS_ENABLED(CONFIG_IPV6) && !*end)
Quoted 2 lines
include/net/inet_addr.h +2 -0
+            return 1;
+#else

Sorry, managed to miss #else... #if could be avoided anyway, tho
this could require an extra patch.

Quoted 6 lines
Quoted 6 lines
include/net/inet_addr.h +5 -0
+        return -1;
+#endif
+    }
+    return -1;
+}
  #endif

WBR, Sergei

#9 lore raw

Re: [RFC Patch net-next 2/5] net: introduce generic inet_pton()

In reply to Cong Wang
Quoted 31 lines · Cong Wang
---
 include/net/inet_addr.h |   20 ++++++++++++++++++++
 net/core/netpoll.c      |   24 ++----------------------
 2 files changed, 22 insertions(+), 22 deletions(-)
include/net/inet_addr.h +10 -0
index 66a16fe..1379287 100644
@@ -4,6 +4,7 @@
 #include <linux/in.h>
 #include <linux/in6.h>
 #include <linux/socket.h>
+#include <linux/inet.h>
 #include <net/addrconf.h>
 
 union inet_addr {
@@ -59,4 +60,23 @@ static inline bool inet_addr_multicast(const union inet_addr *ipa)
 }
 #endif
 
+static inline int inet_pton(const char *str, union inet_addr *addr)
+{
+	const char *end;
+
+	if (!strchr(str, ':') &&
+	    in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
+		if (!*end)
+			return 0;
+	}

include/net/inet_addr.h static inline bool inet_addr_multicast(const union inet_addr *ipa) · line 81

Quoted 10 lines
include/net/inet_addr.h +10 -0
+	if (in6_pton(str, -1, addr->sin6.sin6_addr.s6_addr, -1, &end) > 0) {
+#if IS_ENABLED(CONFIG_IPV6)
+		if (!*end)
+			return 1;
+#else
+		return -1;
+#endif
+	}
+	return -1;
+}

[...]

The return values for this are awful. I think the return value should be 0 for success or -EINVAL on error.

The caller can then check addr->sa.sa_family if they want to know the address family immediately.

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
author
#10 lore raw

Re: [RFC Patch net-next 2/5] net: introduce generic inet_pton()

Quoted 5 lines · Ben Hutchings

The return values for this are awful. I think the return value should be 0 for success or -EINVAL on error.

The caller can then check addr->sa.sa_family if they want to know the address family immediately.

Indeed, I will fix it.

Thanks!

#11 lore raw

Re: [RFC Patch net-next 2/5] net: introduce generic inet_pton()

In reply to Cong Wang
Quoted 19 lines · On Thu, 27 Jun 2013 14:43:15 +0800 Cong Wang <[email protected]> wrote:
---
 include/net/inet_addr.h |   20 ++++++++++++++++++++
 net/core/netpoll.c      |   24 ++----------------------
 2 files changed, 22 insertions(+), 22 deletions(-)
include/net/inet_addr.h +1 -0
index 66a16fe..1379287 100644
@@ -4,6 +4,7 @@
 #include <linux/in.h>
 #include <linux/in6.h>
 #include <linux/socket.h>
+#include <linux/inet.h>
 #include <net/addrconf.h>
 
 union inet_addr {

include/net/inet_addr.h static inline bool inet_addr_multicast(const union inet_addr *ipa) · line 65

Quoted 6 lines
include/net/inet_addr.h +2 -0
@@ -59,4 +60,23 @@ static inline bool inet_addr_multicast(const union inet_addr *ipa)
 }
 #endif
 
+static inline int inet_pton(const char *str, union inet_addr *addr)
+{
 

A couple of comments:
1. No reason for this to be inline
2. If function has same name as userspace it must have same arguments and return value. Either:
a. rename it to kinet_pton or some other name
b. make it work the same.

author
#12 lore raw

Re: [RFC Patch net-next 2/5] net: introduce generic inet_pton()

Quoted 7 lines · Stephen Hemminger

include/net/inet_addr.h static inline bool inet_addr_multicast(const union inet_addr *ipa) · line 64

Quoted 4 lines
include/net/inet_addr.h +2 -0
+static inline int inet_pton(const char *str, union inet_addr *addr)
+{

A couple of comments:
1. No reason for this to be inline

Okay, I will move them into net/core/utils.c.

Quoted 5 lines

2. If function has same name as userspace it must have same arguments and return value. Either:
a. rename it to kinet_pton or some other name
b. make it work the same.

Makes sense too me, I will try your option b) first, if it is over-kill I will fall back to option a).

Thanks!

author
#13 lore raw

RFC PATCH 3/5 inetpeer: use generic union inet_addr

In reply to Cong Wang
---
 include/net/inetpeer.h |   29 +++++----------
 net/ipv4/inetpeer.c    |   35 +++++++++++-------
 net/ipv4/tcp_metrics.c |   92 ++++++++++++++++++++----------------------------
 3 files changed, 68 insertions(+), 88 deletions(-)
include/net/inetpeer.h +9 -20
index 53f464d..7ec33fb 100644
@@ -13,24 +13,13 @@
 #include <linux/spinlock.h>
 #include <linux/rtnetlink.h>
 #include <net/ipv6.h>
+#include <net/inet_addr.h>
 #include <linux/atomic.h>
 
-struct inetpeer_addr_base {
-	union {
-		__be32			a4;
-		__be32			a6[4];
-	};
-};
-
-struct inetpeer_addr {
-	struct inetpeer_addr_base	addr;
-	__u16				family;
-};
-
 struct inet_peer {
 	/* group together avl_left,avl_right,v4daddr to speedup lookups */
 	struct inet_peer __rcu	*avl_left, *avl_right;
-	struct inetpeer_addr	daddr;
+	union inet_addr		daddr;
 	__u32			avl_height;
 
 	u32			metrics[RTAX_MAX];
@@ -133,17 +122,17 @@ static inline bool inet_metrics_new(const struct inet_peer *p)
 
 /* can be called with or without local BH being disabled */
 struct inet_peer *inet_getpeer(struct inet_peer_base *base,
-			       const struct inetpeer_addr *daddr,
+			       const union inet_addr *daddr,
 			       int create);
 
 static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
 						__be32 v4daddr,
 						int create)
 {
-	struct inetpeer_addr daddr;
+	union inet_addr daddr;
 
-	daddr.addr.a4 = v4daddr;
-	daddr.family = AF_INET;
+	daddr.sin.sin_addr.s_addr = v4daddr;
+	daddr.sa.sa_family = AF_INET;
 	return inet_getpeer(base, &daddr, create);
 }
 
@@ -151,10 +140,10 @@ static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
 						const struct in6_addr *v6daddr,
 						int create)
 {
-	struct inetpeer_addr daddr;
+	union inet_addr daddr;
 
-	*(struct in6_addr *)daddr.addr.a6 = *v6daddr;
-	daddr.family = AF_INET6;
+	daddr.sin6.sin6_addr = *v6daddr;
+	daddr.sa.sa_family = AF_INET6;
 	return inet_getpeer(base, &daddr, create);
 }
 
net/ipv4/inetpeer.c +21 -14
index 000e3d2..94a9ba7 100644
@@ -197,17 +197,24 @@ void __init inet_initpeers(void)
 	INIT_DEFERRABLE_WORK(&gc_work, inetpeer_gc_worker);
 }
 
-static int addr_compare(const struct inetpeer_addr *a,
-			const struct inetpeer_addr *b)
+static int addr_compare(const union inet_addr *a,
+			const union inet_addr *b)
 {
-	int i, n = (a->family == AF_INET ? 1 : 4);
+	int i;
 
-	for (i = 0; i < n; i++) {
-		if (a->addr.a6[i] == b->addr.a6[i])
-			continue;
-		if ((__force u32)a->addr.a6[i] < (__force u32)b->addr.a6[i])
+	if (a->sa.sa_family == AF_INET) {
+		if (a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr)
+			return 0;
+		if ((__force u32)a->sin.sin_addr.s_addr < (__force u32)b->sin.sin_addr.s_addr)
 			return -1;
-		return 1;
+	} else {
+		for (i = 0; i < 4; i++) {
+			if (a->sin6.sin6_addr.s6_addr32[i] == b->sin6.sin6_addr.s6_addr32[i])
+				continue;
+			if ((__force u32)a->sin6.sin6_addr.s6_addr32[i] < (__force u32)b->sin6.sin6_addr.s6_addr32[i])
+				return -1;
+			return 1;
+		}
 	}
 
 	return 0;
@@ -248,7 +255,7 @@ static int addr_compare(const struct inetpeer_addr *a,
  * But every pointer we follow is guaranteed to be valid thanks to RCU.
  * We exit from this function if number of links exceeds PEER_MAXDEPTH
  */
-static struct inet_peer *lookup_rcu(const struct inetpeer_addr *daddr,
+static struct inet_peer *lookup_rcu(const union inet_addr *daddr,
 				    struct inet_peer_base *base)
 {
 	struct inet_peer *u = rcu_dereference(base->root);
@@ -457,7 +464,7 @@ static int inet_peer_gc(struct inet_peer_base *base,
 }
 
 struct inet_peer *inet_getpeer(struct inet_peer_base *base,
-			       const struct inetpeer_addr *daddr,
+			       const union inet_addr *daddr,
 			       int create)
 {
 	struct inet_peer __rcu **stack[PEER_MAXDEPTH], ***stackptr;
@@ -465,7 +472,7 @@ struct inet_peer *inet_getpeer(struct inet_peer_base *base,
 	unsigned int sequence;
 	int invalidated, gccnt = 0;
 
-	flush_check(base, daddr->family);
+	flush_check(base, daddr->sa.sa_family);
 
 	/* Attempt a lockless lookup first.
 	 * Because of a concurrent writer, we might not find an existing entry.
@@ -505,9 +512,9 @@ relookup:
 		atomic_set(&p->refcnt, 1);
 		atomic_set(&p->rid, 0);
 		atomic_set(&p->ip_id_count,
-				(daddr->family == AF_INET) ?
-					secure_ip_id(daddr->addr.a4) :
-					secure_ipv6_id(daddr->addr.a6));
+				(daddr->sa.sa_family == AF_INET) ?
+					secure_ip_id(daddr->sin.sin_addr.s_addr) :
+					secure_ipv6_id(daddr->sin6.sin6_addr.s6_addr32));
 		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
 		p->rate_tokens = 0;
 		/* 60*HZ is arbitrary, but chosen enough high so that the first
net/ipv4/tcp_metrics.c +38 -54

226 lines not on this page · raw message

Signature · 1 line
1.7.7.6
#14 lore raw

Re: [RFC Patch net-next 3/5] inetpeer: use generic union inet_addr

Quoted 25 lines · Cong Wang
---
 include/net/inetpeer.h |   29 +++++----------
 net/ipv4/inetpeer.c    |   35 +++++++++++-------
 net/ipv4/tcp_metrics.c |   92 ++++++++++++++++++++----------------------------
 3 files changed, 68 insertions(+), 88 deletions(-)
include/net/inetpeer.h +1 -7
index 53f464d..7ec33fb 100644
@@ -13,24 +13,13 @@
 #include <linux/spinlock.h>
 #include <linux/rtnetlink.h>
 #include <net/ipv6.h>
+#include <net/inet_addr.h>
 #include <linux/atomic.h>
 
-struct inetpeer_addr_base {
-	union {
-		__be32			a4;
-		__be32			a6[4];
-	};
-};
-

include/net/inetpeer.h line 22

Quoted 10 lines
include/net/inetpeer.h +1 -6
-struct inetpeer_addr {
-	struct inetpeer_addr_base	addr;
-	__u16				family;
-};
-
 struct inet_peer {
 	/* group together avl_left,avl_right,v4daddr to speedup lookups */
 	struct inet_peer __rcu	*avl_left, *avl_right;
-	struct inetpeer_addr	daddr;
+	union inet_addr		daddr;

Please compare sizeof(struct inetpeer_addr) and sizeof(union inet_addr)

If I am not mistaken, its 20 bytes instead of 28

Yes, sockaddr_in6 is a bit bloated...

author
#15 lore raw

Re: [RFC Patch net-next 3/5] inetpeer: use generic union inet_addr

Quoted 9 lines · Eric Dumazet

include/net/inetpeer.h line 22

Quoted 5 lines · Cong Wang

struct inet_peer {

include/net/inetpeer.h +1 -1
 	/* group together avl_left,avl_right,v4daddr to speedup lookups */
 	struct inet_peer __rcu	*avl_left, *avl_right;
-	struct inetpeer_addr	daddr;
+	union inet_addr		daddr;

Please compare sizeof(struct inetpeer_addr) and sizeof(union inet_addr)

Quoted 4 lines

If I am not mistaken, its 20 bytes instead of 28

Yes, sockaddr_in6 is a bit bloated...

You are right.

Are you saying that I should rearrange the fields of struct inet_peer in case of cacheline miss?

Thanks!

author
#16 lore raw

RFC PATCH 4/5 sunrpc: use generic union inet_addr

In reply to Cong Wang
---
 include/linux/sunrpc/addr.h |  119 +++----------------------------------------
 include/net/inet_addr.h     |   56 ++++++++++++++++++++
 2 files changed, 64 insertions(+), 111 deletions(-)
include/linux/sunrpc/addr.h +8 -111
index 07d8e53..4c3602e 100644
@@ -11,6 +11,7 @@
 #include <linux/in.h>
 #include <linux/in6.h>
 #include <net/ipv6.h>
+#include <net/inet_addr.h>
 
 size_t		rpc_ntop(const struct sockaddr *, char *, const size_t);
 size_t		rpc_pton(struct net *, const char *, const size_t,
@@ -21,137 +22,33 @@ size_t		rpc_uaddr2sockaddr(struct net *, const char *, const size_t,
 
 static inline unsigned short rpc_get_port(const struct sockaddr *sap)
 {
-	switch (sap->sa_family) {
-	case AF_INET:
-		return ntohs(((struct sockaddr_in *)sap)->sin_port);
-	case AF_INET6:
-		return ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
-	}
-	return 0;
+	return inet_addr_get_port((const union inet_addr *)sap);
 }
 
 static inline void rpc_set_port(struct sockaddr *sap,
 				const unsigned short port)
 {
-	switch (sap->sa_family) {
-	case AF_INET:
-		((struct sockaddr_in *)sap)->sin_port = htons(port);
-		break;
-	case AF_INET6:
-		((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
-		break;
-	}
+	inet_addr_set_port((union inet_addr *)sap, port);
 }
 
 #define IPV6_SCOPE_DELIMITER		'%'
 #define IPV6_SCOPE_ID_LEN		sizeof("%nnnnnnnnnn")
 
-static inline bool __rpc_cmp_addr4(const struct sockaddr *sap1,
-				   const struct sockaddr *sap2)
-{
-	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sap1;
-	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sap2;
-
-	return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
-}
-
-static inline bool __rpc_copy_addr4(struct sockaddr *dst,
-				    const struct sockaddr *src)
-{
-	const struct sockaddr_in *ssin = (struct sockaddr_in *) src;
-	struct sockaddr_in *dsin = (struct sockaddr_in *) dst;
-
-	dsin->sin_family = ssin->sin_family;
-	dsin->sin_addr.s_addr = ssin->sin_addr.s_addr;
-	return true;
-}
-
-#if IS_ENABLED(CONFIG_IPV6)
-static inline bool __rpc_cmp_addr6(const struct sockaddr *sap1,
-				   const struct sockaddr *sap2)
-{
-	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sap1;
-	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sap2;
-
-	if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
-		return false;
-	else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
-		return sin1->sin6_scope_id == sin2->sin6_scope_id;
-
-	return true;
-}
-
-static inline bool __rpc_copy_addr6(struct sockaddr *dst,
-				    const struct sockaddr *src)
-{
-	const struct sockaddr_in6 *ssin6 = (const struct sockaddr_in6 *) src;
-	struct sockaddr_in6 *dsin6 = (struct sockaddr_in6 *) dst;
-
-	dsin6->sin6_family = ssin6->sin6_family;
-	dsin6->sin6_addr = ssin6->sin6_addr;
-	dsin6->sin6_scope_id = ssin6->sin6_scope_id;
-	return true;
-}
-#else	/* !(IS_ENABLED(CONFIG_IPV6) */
-static inline bool __rpc_cmp_addr6(const struct sockaddr *sap1,
-				   const struct sockaddr *sap2)
-{
-	return false;
-}
-
-static inline bool __rpc_copy_addr6(struct sockaddr *dst,
-				    const struct sockaddr *src)
-{
-	return false;
-}
-#endif	/* !(IS_ENABLED(CONFIG_IPV6) */
-
-/**
- * rpc_cmp_addr - compare the address portion of two sockaddrs.
- * @sap1: first sockaddr
- * @sap2: second sockaddr
- *
- * Just compares the family and address portion. Ignores port, but
- * compares the scope if it's a link-local address.
- *
- * Returns true if the addrs are equal, false if they aren't.
- */
 static inline bool rpc_cmp_addr(const struct sockaddr *sap1,
 				const struct sockaddr *sap2)
 {
-	if (sap1->sa_family == sap2->sa_family) {
-		switch (sap1->sa_family) {
-		case AF_INET:
-			return __rpc_cmp_addr4(sap1, sap2);
-		case AF_INET6:
-			return __rpc_cmp_addr6(sap1, sap2);
-		}
-	}
-	return false;
+	return inet_addr_equal((const union inet_addr *)sap1,
+			       (const union inet_addr *)sap2);
 }
 
-/**
- * rpc_copy_addr - copy the address portion of one sockaddr to another
- * @dst: destination sockaddr
- * @src: source sockaddr
- *
- * Just copies the address portion and family. Ignores port, scope, etc.
- * Caller is responsible for making certain that dst is large enough to hold
- * the address in src. Returns true if address family is supported. Returns
- * false otherwise.
- */
 static inline bool rpc_copy_addr(struct sockaddr *dst,
 				 const struct sockaddr *src)
 {
-	switch (src->sa_family) {
-	case AF_INET:
-		return __rpc_copy_addr4(dst, src);
-	case AF_INET6:
-		return __rpc_copy_addr6(dst, src);
-	}
-	return false;
+	return inet_addr_copy((union inet_addr *)dst,
+			      (const union inet_addr *)src);
 }
 
+
 /**
  * rpc_get_scope_id - return scopeid for a given sockaddr
  * @sa: sockaddr to get scopeid from
include/net/inet_addr.h +56 -0

62 lines not on this page · raw message

Signature · 1 line
1.7.7.6
#17 lore raw

Re: [RFC Patch net-next 4/5] sunrpc: use generic union inet_addr

On Thu, 2013-06-27 at 14:43 +0800, Cong Wang wrote:
[...]

Quoted 7 lines

-/**
- * rpc_cmp_addr - compare the address portion of two sockaddrs.
- * @sap1: first sockaddr
- * @sap2: second sockaddr
- *
- * Just compares the family and address portion. Ignores port, but
- * compares the scope if it's a link-local address.

You're removing the scope comparison.

Ben.

Quoted 17 lines

- * Returns true if the addrs are equal, false if they aren't.
- */
static inline bool rpc_cmp_addr(const struct sockaddr *sap1,

include/linux/sunrpc/addr.h +2 -9
 				const struct sockaddr *sap2)
 {
-	if (sap1->sa_family == sap2->sa_family) {
-		switch (sap1->sa_family) {
-		case AF_INET:
-			return __rpc_cmp_addr4(sap1, sap2);
-		case AF_INET6:
-			return __rpc_cmp_addr6(sap1, sap2);
-		}
-	}
-	return false;
+	return inet_addr_equal((const union inet_addr *)sap1,
+			       (const union inet_addr *)sap2);
 }

[...]

Signature · 8 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#18 lore raw

Re: [RFC Patch net-next 4/5] sunrpc: use generic union inet_addr

Quoted 11 lines · Ben Hutchings

On Thu, 2013-06-27 at 14:43 +0800, Cong Wang wrote:
[...]

Quoted 7 lines

-/**
- * rpc_cmp_addr - compare the address portion of two sockaddrs.
- * @sap1: first sockaddr
- * @sap2: second sockaddr
- *
- * Just compares the family and address portion. Ignores port, but
- * compares the scope if it's a link-local address.

You're removing the scope comparison.

Actually I added the following code in patch 5/5:

+ if (!ipv6_addr_equal(&a->sin6.sin6_addr,
&b->sin6.sin6_addr))
+ return false;
+ else if
(__ipv6_addr_needs_scope_id(__ipv6_addr_type(&a->sin6.sin6_addr)))
+ return a->sin6.sin6_scope_id ==
b->sin6.sin6_scope_id;

but I should move it to this patch, otherwise it is hard for review.

Thanks!

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#19 lore raw

Re: [RFC Patch net-next 4/5] sunrpc: use generic union inet_addr

In reply to Cong Wang

On Thu, 2013-06-27 at 14:43 +0800, Cong Wang wrote:
[...]

Quoted 21 lines
include/net/inet_addr.h +15 -0
@@ -60,6 +60,62 @@ static inline bool inet_addr_multicast(const union inet_addr *ipa)
 }
 #endif
 
+/**
+ * inet_addr_copy - copy the address portion of one sockaddr to another
+ * @dst: destination sockaddr
+ * @src: source sockaddr
+ *
+ * Just copies the address portion and family. Ignores port, scope, etc.
+ * Caller is responsible for making certain that dst is large enough to hold
+ * the address in src. Returns true if address family is supported. Returns
+ * false otherwise.
+ */
+static inline bool inet_addr_copy(union inet_addr *dst,
+				  const union inet_addr *src)
+{
+	dst->sa.sa_family = src->sa.sa_family;
+

include/net/inet_addr.h static inline bool inet_addr_multicast(const union inet_addr *ipa) · line 87

Quoted 10 lines
include/net/inet_addr.h +10 -0
+	switch (src->sa.sa_family) {
+	case AF_INET:
+		dst->sin.sin_addr.s_addr = src->sin.sin_addr.s_addr;
+		return true;
+#if IS_ENABLED(CONFIG_IPV6)
+	case AF_INET6:
+		dst->sin6.sin6_addr = src->sin6.sin6_addr;
+		dst->sin6.sin6_scope_id = src->sin6.sin6_scope_id;
+		return true;
+#endif

Most of the other functions work on IPv6 addresses without CONFIG_IPV6; why should this be different?

Ben.

Quoted 4 lines
include/net/inet_addr.h +4 -0
+	}
+
+	return false;
+}

[...]

Signature · 8 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#20 lore raw

Re: [RFC Patch net-next 4/5] sunrpc: use generic union inet_addr

Quoted 3 lines · Ben Hutchings

Most of the other functions work on IPv6 addresses without CONFIG_IPV6;
why should this be different?

Save few cycles for !CONFIG_IPV6? :-/

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#21 lore raw

RFC PATCH 5/5 nfs,cifs: abstract generic inet_addr_equal_strict()

In reply to Cong Wang
---
 fs/cifs/connect.c          |   38 ++++-------------
 fs/dlm/lowcomms.c          |   24 ++---------
 fs/nfs/client.c            |   94 ++-----------------------------------------
 fs/nfs/nfs4filelayoutdev.c |   37 ++---------------
 fs/nfs/super.c             |   31 +-------------
 include/net/inet_addr.h    |   32 +++++++++++++-
 6 files changed, 55 insertions(+), 201 deletions(-)
fs/cifs/connect.c +9 -29
index e3bc39b..ca6bcf82 100644
@@ -40,6 +40,7 @@
 #include <linux/module.h>
 #include <keys/user-type.h>
 #include <net/ipv6.h>
+#include <net/inet_addr.h>
 #include <linux/parser.h>
 
 #include "cifspdu.h"
@@ -1886,17 +1887,10 @@ srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
 {
 	switch (srcaddr->sa_family) {
 	case AF_UNSPEC:
-		return (rhs->sa_family == AF_UNSPEC);
-	case AF_INET: {
-		struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
-		struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
-		return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
-	}
-	case AF_INET6: {
-		struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
-		struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
-		return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
-	}
+	case AF_INET:
+	case AF_INET6:
+		return inet_addr_equal((union inet_addr *)srcaddr,
+				       (union inet_addr *)rhs);
 	default:
 		WARN_ON(1);
 		return false; /* don't expect to be here */
@@ -1943,24 +1937,10 @@ match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
 	      struct sockaddr *srcaddr)
 {
 	switch (addr->sa_family) {
-	case AF_INET: {
-		struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
-		struct sockaddr_in *srv_addr4 =
-					(struct sockaddr_in *)&server->dstaddr;
-
-		if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
-			return false;
-		break;
-	}
-	case AF_INET6: {
-		struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
-		struct sockaddr_in6 *srv_addr6 =
-					(struct sockaddr_in6 *)&server->dstaddr;
-
-		if (!ipv6_addr_equal(&addr6->sin6_addr,
-				     &srv_addr6->sin6_addr))
-			return false;
-		if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
+	case AF_INET:
+	case AF_INET6:
+		if (!inet_addr_equal((union inet_addr *)addr,
+				     (union inet_addr *)&server->dstaddr))
 			return false;
 		break;
 	}
fs/dlm/lowcomms.c +5 -19
index d0ccd2f..c27e36e 100644
@@ -55,6 +55,7 @@
 #include <linux/sctp.h>
 #include <net/sctp/sctp.h>
 #include <net/ipv6.h>
+#include <net/inet_addr.h>
 
 #include "dlm_internal.h"
 #include "lowcomms.h"
@@ -285,28 +286,13 @@ static struct dlm_node_addr *find_node_addr(int nodeid)
 static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
 {
 	switch (x->ss_family) {
-	case AF_INET: {
-		struct sockaddr_in *sinx = (struct sockaddr_in *)x;
-		struct sockaddr_in *siny = (struct sockaddr_in *)y;
-		if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
-			return 0;
-		if (sinx->sin_port != siny->sin_port)
-			return 0;
-		break;
-	}
-	case AF_INET6: {
-		struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
-		struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
-		if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
-			return 0;
-		if (sinx->sin6_port != siny->sin6_port)
-			return 0;
-		break;
-	}
+	case AF_INET:
+	case AF_INET6:
+		return inet_addr_equal_strict((union inet_addr *)x,
+					      (union inet_addr *)y);
 	default:
 		return 0;
 	}
-	return 1;
 }
 
 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
fs/nfs/client.c +5 -89

118 lines not on this page · raw message

fs/nfs/nfs4filelayoutdev.c +4 -33
index 661a0f6..6509160 100644
@@ -32,6 +32,7 @@
 #include <linux/vmalloc.h>
 #include <linux/module.h>
 #include <linux/sunrpc/addr.h>
+#include <net/inet_addr.h>
 
 #include "internal.h"
 #include "nfs4session.h"
@@ -74,44 +75,14 @@ print_ds(struct nfs4_pnfs_ds *ds)
 static bool
 same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
 {
-	struct sockaddr_in *a, *b;
-	struct sockaddr_in6 *a6, *b6;
-
-	if (addr1->sa_family != addr2->sa_family)
-		return false;
-
-	switch (addr1->sa_family) {
-	case AF_INET:
-		a = (struct sockaddr_in *)addr1;
-		b = (struct sockaddr_in *)addr2;
-
-		if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
-		    a->sin_port == b->sin_port)
-			return true;
-		break;
-
-	case AF_INET6:
-		a6 = (struct sockaddr_in6 *)addr1;
-		b6 = (struct sockaddr_in6 *)addr2;
-
-		/* LINKLOCAL addresses must have matching scope_id */
-		if (ipv6_addr_scope(&a6->sin6_addr) ==
-		    IPV6_ADDR_SCOPE_LINKLOCAL &&
-		    a6->sin6_scope_id != b6->sin6_scope_id)
-			return false;
-
-		if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
-		    a6->sin6_port == b6->sin6_port)
-			return true;
-		break;
-
-	default:
+	if (addr1->sa_family != AF_INET && addr1->sa_family != AF_INET6) {
 		dprintk("%s: unhandled address family: %u\n",
 			__func__, addr1->sa_family);
 		return false;
 	}
 
-	return false;
+	return inet_addr_equal_strict((union inet_addr *)addr1,
+				      (union inet_addr *)addr2);
 }
 
 static bool
fs/nfs/super.c +3 -28
index 2d7525f..eb58d8a 100644
@@ -49,6 +49,7 @@
 #include <linux/in6.h>
 #include <linux/slab.h>
 #include <net/ipv6.h>
+#include <net/inet_addr.h>
 #include <linux/netdevice.h>
 #include <linux/nfs_xdr.h>
 #include <linux/magic.h>
@@ -2318,34 +2319,8 @@ static int nfs_compare_super_address(struct nfs_server *server1,
 
 	sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
 	sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
-
-	if (sap1->sa_family != sap2->sa_family)
-		return 0;
-
-	switch (sap1->sa_family) {
-	case AF_INET: {
-		struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
-		struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
-		if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
-			return 0;
-		if (sin1->sin_port != sin2->sin_port)
-			return 0;
-		break;
-	}
-	case AF_INET6: {
-		struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
-		struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
-		if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
-			return 0;
-		if (sin1->sin6_port != sin2->sin6_port)
-			return 0;
-		break;
-	}
-	default:
-		return 0;
-	}
-
-	return 1;
+	return inet_addr_equal_strict((union inet_addr *)sap1,
+				      (union inet_addr *)sap2);
 }
 
 static int nfs_compare_super(struct super_block *sb, void *data)
include/net/inet_addr.h +29 -3
index 683e904..9889c27 100644
@@ -19,9 +19,15 @@ bool inet_addr_equal(const union inet_addr *a, const union inet_addr *b)
 {
 	if (a->sa.sa_family != b->sa.sa_family)
 		return false;
-	if (a->sa.sa_family == AF_INET6)
-		return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
-	else
+	if (a->sa.sa_family == AF_UNSPEC)
+		return true;
+	else if (a->sa.sa_family == AF_INET6) {
+		if (!ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr))
+			return false;
+		else if (__ipv6_addr_needs_scope_id(__ipv6_addr_type(&a->sin6.sin6_addr)))
+			return a->sin6.sin6_scope_id == b->sin6.sin6_scope_id;
+		return false;
+	} else
 		return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
 }
 
@@ -46,6 +52,8 @@ static inline bool inet_addr_multicast(const union inet_addr *ipa)
 static inline
 bool inet_addr_equal(const union inet_addr *a, const union inet_addr *b)
 {
+	if (a->sa.sa_family == AF_UNSPEC)
+		return a->sa.sa_family == b->sa.sa_family;
 	return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
 }
 
@@ -60,6 +68,24 @@ static inline bool inet_addr_multicast(const union inet_addr *ipa)
 }
 #endif
 
+static inline
+bool inet_addr_equal_strict(const union inet_addr *a, const union inet_addr *b)
+{
+	if (inet_addr_equal(a, b)) {
+		switch (a->sa.sa_family) {
+#if IS_ENABLED(CONFIG_IPV6)
+		case AF_INET6:
+			return a->sin6.sin6_port == b->sin6.sin6_port;
+#endif
+		case AF_INET:
+			return a->sin.sin_port == b->sin.sin_port;
+		default:
+			return true;
+		}
+	} else
+		return false;
+}
+
 /**
  * inet_addr_copy - copy the address portion of one sockaddr to another
  * @dst: destination sockaddr
Signature · 1 line
1.7.7.6
#22 lore raw
In reply to Cong Wang

Hi Amerigo,

Quoted 10 lines · Cong Wang

As IPv6 becomes popular, more and more subsystems begin to support IPv6, therefore we need a generic IP address type, in case of duplicates. Also we will also need some helpers to compare, print, check the generic IP address.

This patchset introduce a new type union inet_addr as a union of IPv4 and IPv6 address, and some helper functions that will be used by existing code and in the future VXLAN module.

This patchset only does compile test, since it is still RFC.

This patch already does that which I've sent yesterday before yours ...

[PATCH net-next 1/2] lib: vsprintf: add IPv4/v6 generic %pig/%pIg format specifier

... and resend with the set today in the morning as v2 with provided feedback applied. Can't you base yours on top of that?

Thanks !

Quoted 25 lines

Cong Wang (5):
net: introduce generic union inet_addr
net: introduce generic inet_pton()
inetpeer: use generic union inet_addr
sunrpc: use generic union inet_addr nfs,cifs: abstract generic inet_addr_equal_strict()

  Documentation/printk-formats.txt |    9 ++
  drivers/net/netconsole.c         |   22 +++---
  fs/cifs/connect.c                |   38 ++-------
  fs/dlm/lowcomms.c                |   24 +-----
  fs/nfs/client.c                  |   94 +--------------------
  fs/nfs/nfs4filelayoutdev.c       |   37 +--------
  fs/nfs/super.c                   |   31 +-------
  include/linux/netpoll.h          |    9 +--
  include/linux/sunrpc/addr.h      |  119 ++-------------------------
  include/net/inet_addr.h          |  164 ++++++++++++++++++++++++++++++++++++++
  include/net/inetpeer.h           |   29 ++-----
  lib/vsprintf.c                   |   18 ++++-
  net/core/netpoll.c               |   74 ++++++-----------
  net/ipv4/inetpeer.c              |   35 +++++---
  net/ipv4/tcp_metrics.c           |   92 +++++++++-------------
  15 files changed, 327 insertions(+), 468 deletions(-)

create mode 100644 include/net/inet_addr.h

author
#23 lore raw
Quoted 12 lines · Daniel Borkmann

Hi Amerigo,

Quoted 9 lines · Cong Wang

As IPv6 becomes popular, more and more subsystems begin to support IPv6, therefore we need a generic IP address type, in case of duplicates. Also we will also need some helpers to compare, print, check the generic IP address.

This patchset introduce a new type union inet_addr as a union of IPv4 and IPv6 address, and some helper functions that will be used by existing code and in the future VXLAN module.

Quoted 8 lines
Quoted 1 line

This patchset only does compile test, since it is still RFC.

This patch already does that which I've sent yesterday before yours ...

[PATCH net-next 1/2] lib: vsprintf: add IPv4/v6 generic %pig/%pIg format specifier

... and resend with the set today in the morning as v2 with provided feedback applied. Can't you base yours on top of that?

Since your patch is not yet merged, so why not just drop yours (of course, if no objection for mine)? :)

IOW, even if your patch got merged, then I would probably partially revert it when I rebase mine on top of yours, which seems ugly, right?

Again, I have no objection to your patch, just I don't want to partially revert it when I rebase on top of it.

What do you think?

#24 lore raw
Quoted 25 lines · Cong Wang
Quoted 12 lines · Daniel Borkmann

Hi Amerigo,

Quoted 9 lines · Cong Wang

As IPv6 becomes popular, more and more subsystems begin to support IPv6, therefore we need a generic IP address type, in case of duplicates. Also we will also need some helpers to compare, print, check the generic IP address.

This patchset introduce a new type union inet_addr as a union of IPv4 and IPv6 address, and some helper functions that will be used by existing code and in the future VXLAN module.

Quoted 8 lines
Quoted 1 line

This patchset only does compile test, since it is still RFC.

This patch already does that which I've sent yesterday before yours ...

[PATCH net-next 1/2] lib: vsprintf: add IPv4/v6 generic %pig/%pIg format specifier

... and resend with the set today in the morning as v2 with provided feedback applied. Can't you base yours on top of that?

Since your patch is not yet merged, so why not just drop yours (of course, if no objection for mine)? :)

Quoted 7 lines

IOW, even if your patch got merged, then I would probably partially revert it when I rebase mine on top of yours, which seems ugly, right?

Again, I have no objection to your patch, just I don't want to partially revert it when I rebase on top of it.

What do you think?

I'm sorry Amerigo, but I think this is totally uncalled for. I'm also not just taking your patches, extend them for example and submit them by myself requesting yours to be dropped instead. The likelihood that you came up with the same idea independently at the /very same time/ I consider quite low. So therefore, you must have done this after seeing this set. Please rebase them on top of this after we have agreed upon the two patches, even if it reverts stuff, and then we can further discuss your changes.

Thanks !