Skip to contentSkip to conversation

Add generic socket address management to sunrpc.ko

PATCH In review Chuck Lever 2 participants 14 messages Jul 15, 2009 – Aug 3, 2009 4 messages not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Add generic socket address management to sunrpc.ko

Hi Trond-

This set of patches, for 2.6.32, introduces a set of exported functions to sunrpc.ko for converting between a socket address and either a standard presentation address string or an RPC universal address.

The universal address functions will be used to encode and decode RPCB_FOO and NFSv4 SETCLIENTID arguments. The other functions are part of a previous promise to deliver shared functions that can be used by upper-layer protocols to display and manipulate IP addresses.

The kernel's current printf formatters for IP addresses (%pI4 and %pI6) were designed specifically for kernel to user-space APIs that require a particular string format for socket addresses. Thus they are somewhat limited for the purposes of RPC. The formatter for IPv6 addresses, %pI6, does not support short-handing (see RFC 4291) or IPv6 scope IDs. Also, these printf formatters are unique per address family, so separate formatter strings are required for printing AF_INET and AF_INET6 addresses.

These patches have seen light testing here, and are also available in the cel-2.6.git linux.nfs.org repository under cel-ipv6-07152009.

The last patch in this series might go to Bruce, but it depends on the previous 9 here.

---

Chuck Lever (10):
NFSD: Support IPv6 addresses in write_failover_ip()
lockd: Replace nsm_display_address() with rpc_ntop()
lockd: Replace nlm_clear_port()
NFS: Replace nfs_set_port() with rpc_set_port()
NFS: Replace nfs_parse_ip_address() with rpc_pton()
SUNRPC: Use rpc_ntop() for constructing transport address strings
SUNRPC: Remove duplicate universal address generation
SUNRPC: Provide functions for managing universal addresses
SUNRPC: Move XDR data type size macros
SUNRPC: Clean up RPCBIND_MAXUADDRLEN definitions

 fs/lockd/host.c                 |   14 -
 fs/lockd/mon.c                  |   44 ---
 fs/nfs/internal.h               |   22 --
 fs/nfs/nfs4namespace.c          |    8 -
 fs/nfs/super.c                  |  154 ++---------
 fs/nfsd/nfsctl.c                |   21 +-
 include/linux/sunrpc/clnt.h     |   12 +
 include/linux/sunrpc/msg_prot.h |   18 +
 include/linux/sunrpc/xprt.h     |    1 
 net/sunrpc/Makefile             |    2 
 net/sunrpc/addr.c               |  524 +++++++++++++++++++++++++++++++++++++++
 net/sunrpc/rpcb_clnt.c          |  104 ++++----
 net/sunrpc/xprtrdma/transport.c |   53 +---
 net/sunrpc/xprtsock.c           |  127 +++------
 14 files changed, 705 insertions(+), 399 deletions(-)
 create mode 100644 net/sunrpc/addr.c
Signature · 1 line
Chuck Lever <[email protected]>

A message that is not in the archive (sent off-list or not yet received): 20090715212628.7582.98251.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org

#2 lore raw

PATCH 1/10 SUNRPC: Clean up RPCBIND_MAXUADDRLEN definitions

Clean up: Replace the single-integer definition of RPCBIND_MAXUADDRLEN with a definition that is based on previously defined address string sizes, and document the way this maximum is calculated. Also provide a separate macro for the size of the port number extension.

---

 include/linux/sunrpc/msg_prot.h |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
include/linux/sunrpc/msg_prot.h +17 -1
index 70df4f1..063e806 100644
@@ -189,7 +189,23 @@ typedef __be32	rpc_fraghdr;
  * Additionally, the two alternative forms specified in Section 2.2 of
  * [RFC2373] are also acceptable.
  */
-#define RPCBIND_MAXUADDRLEN	(56u)
+
+#include <linux/inet.h>
+
+/* Maximum size of the port number part of a universal address */
+#define RPCBIND_MAXUADDRPLEN	sizeof(".255.255")
+
+/* Maximum size of an IPv4 universal address */
+#define RPCBIND_MAXUADDR4LEN	\
+		(INET_ADDRSTRLEN + RPCBIND_MAXUADDRPLEN)
+
+/* Maximum size of an IPv6 universal address */
+#define RPCBIND_MAXUADDR6LEN	\
+		(INET6_ADDRSTRLEN + RPCBIND_MAXUADDRPLEN)
+
+#define __RPCB_MAXLEN(x, y) (x > y ? x : y)
+#define RPCBIND_MAXUADDRLEN	\
+		__RPCB_MAXLEN(RPCBIND_MAXUADDR4LEN, RPCBIND_MAXUADDR6LEN)
 
 #endif /* __KERNEL__ */
 #endif /* _LINUX_SUNRPC_MSGPROT_H_ */

A message that is not in the archive (sent off-list or not yet received): 20090715213654.7582.2418.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org

#4 lore raw

Re: [PATCH 01/10] SUNRPC: Clean up RPCBIND_MAXUADDRLEN definitions

Quoted 27 lines · Chuck Lever

Clean up: Replace the single-integer definition of RPCBIND_MAXUADDRLEN with a definition that is based on previously defined address string sizes, and document the way this maximum is calculated. Also provide a separate macro for the size of the port number extension.

---

 include/linux/sunrpc/msg_prot.h |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
include/linux/sunrpc/msg_prot.h +7 -1
index 70df4f1..063e806 100644
@@ -189,7 +189,23 @@ typedef __be32	rpc_fraghdr;
  * Additionally, the two alternative forms specified in Section 2.2 of
  * [RFC2373] are also acceptable.
  */
-#define RPCBIND_MAXUADDRLEN	(56u)
+
+#include <linux/inet.h>
+
+/* Maximum size of the port number part of a universal address */
+#define RPCBIND_MAXUADDRPLEN	sizeof(".255.255")
+
+/* Maximum size of an IPv4 universal address */

include/linux/sunrpc/msg_prot.h typedef __be32 rpc_fraghdr; · line 208

Quoted 10 lines
include/linux/sunrpc/msg_prot.h +10 -0
+#define RPCBIND_MAXUADDR4LEN	\
+		(INET_ADDRSTRLEN + RPCBIND_MAXUADDRPLEN)
+
+/* Maximum size of an IPv6 universal address */
+#define RPCBIND_MAXUADDR6LEN	\
+		(INET6_ADDRSTRLEN + RPCBIND_MAXUADDRPLEN)
+
+#define __RPCB_MAXLEN(x, y) (x > y ? x : y)
+#define RPCBIND_MAXUADDRLEN	\
+		__RPCB_MAXLEN(RPCBIND_MAXUADDR4LEN, RPCBIND_MAXUADDR6LEN)

Ewww... We _know_ that this will always evaluate to RPCBIND_MAXUADDR6LEN, so why pretend that we need the above "calculation"?

Quoted 3 lines
include/linux/sunrpc/msg_prot.h +0 -0
 #endif /* __KERNEL__ */
 #endif /* _LINUX_SUNRPC_MSGPROT_H_ */
 
#5 lore raw

PATCH 2/10 SUNRPC: Move XDR data type size macros

In reply to a message not in the archive

Clean up: To make subsequent patches cleaner, move the XDR data type size macros to the top of the file (similar to nfs4xdr.c) first.

---

 net/sunrpc/rpcb_clnt.c |   56 +++++++++++++++++++++++++++---------------------
 1 files changed, 31 insertions(+), 25 deletions(-)
net/sunrpc/rpcb_clnt.c +31 -25
index beee6da..ad1d731 100644
@@ -75,6 +75,37 @@ enum {
 #define RPCB_OWNER_STRING	"0"
 #define RPCB_MAXOWNERLEN	sizeof(RPCB_OWNER_STRING)
 
+/*
+ * XDR data type sizes
+ */
+#define RPCB_program_sz		(1)
+#define RPCB_version_sz		(1)
+#define RPCB_protocol_sz	(1)
+#define RPCB_port_sz		(1)
+#define RPCB_boolean_sz		(1)
+
+#define RPCB_netid_sz		(1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
+#define RPCB_addr_sz		(1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
+#define RPCB_ownerstring_sz	(1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
+
+/*
+ * XDR argument and result sizes
+ */
+#define RPCB_mappingargs_sz	(RPCB_program_sz + RPCB_version_sz + \
+				RPCB_protocol_sz + RPCB_port_sz)
+#define RPCB_getaddrargs_sz	(RPCB_program_sz + RPCB_version_sz + \
+				RPCB_netid_sz + RPCB_addr_sz + \
+				RPCB_ownerstring_sz)
+
+#define RPCB_getportres_sz	RPCB_port_sz
+#define RPCB_setres_sz		RPCB_boolean_sz
+
+/*
+ * Note that RFC 1833 does not put any size restrictions on the
+ * address string returned by the remote rpcbind database.
+ */
+#define RPCB_getaddrres_sz	RPCB_addr_sz
+
 static void			rpcb_getport_done(struct rpc_task *, void *);
 static void			rpcb_map_release(void *data);
 static struct rpc_program	rpcb_program;
@@ -768,31 +799,6 @@ out_err:
 	return -EIO;
 }
 
-#define RPCB_program_sz		(1u)
-#define RPCB_version_sz		(1u)
-#define RPCB_protocol_sz	(1u)
-#define RPCB_port_sz		(1u)
-#define RPCB_boolean_sz		(1u)
-
-#define RPCB_netid_sz		(1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
-#define RPCB_addr_sz		(1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
-#define RPCB_ownerstring_sz	(1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
-
-#define RPCB_mappingargs_sz	RPCB_program_sz+RPCB_version_sz+	\
-				RPCB_protocol_sz+RPCB_port_sz
-#define RPCB_getaddrargs_sz	RPCB_program_sz+RPCB_version_sz+	\
-				RPCB_netid_sz+RPCB_addr_sz+		\
-				RPCB_ownerstring_sz
-
-#define RPCB_setres_sz		RPCB_boolean_sz
-#define RPCB_getportres_sz	RPCB_port_sz
-
-/*
- * Note that RFC 1833 does not put any size restrictions on the
- * address string returned by the remote rpcbind database.
- */
-#define RPCB_getaddrres_sz	RPCB_addr_sz
-
 #define PROC(proc, argtype, restype)					\
 	[RPCBPROC_##proc] = {						\
 		.p_proc		= RPCBPROC_##proc,			\
#6 lore raw

PATCH 3/10 SUNRPC: Provide functions for managing universal addresses

In reply to a message not in the archive

Introduce a set of functions in the kernel's RPC implementation for converting between a socket address and either a standard presentation address string or an RPC universal address.

The universal address functions will be used to encode and decode RPCB_FOO and NFSv4 SETCLIENTID arguments. The other functions are part of a previous promise to deliver shared functions that can be used by upper-layer protocols to display and manipulate IP addresses.

The kernel's current address printf formatters were designed specifically for kernel to user-space APIs that require a particular string format for socket addresses, thus are somewhat limited for the purposes of sunrpc.ko. The formatter for IPv6 addresses, %pI6, does not support short-handing or scope IDs. Also, these printf formatters are unique per address family, so a separate formatter string is required for printing AF_INET and AF_INET6 addresses.

---

 fs/nfs/internal.h           |    2 
 include/linux/sunrpc/clnt.h |   12 +
 net/sunrpc/Makefile         |    2 
 net/sunrpc/addr.c           |  524 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 537 insertions(+), 3 deletions(-)
 create mode 100644 net/sunrpc/addr.c
fs/nfs/internal.h +0 -2
index d3823d7..a19b7b9 100644
@@ -370,8 +370,6 @@ unsigned int nfs_page_array_len(unsigned int base, size_t len)
 		PAGE_SIZE - 1) >> PAGE_SHIFT;
 }
 
-#define IPV6_SCOPE_DELIMITER	'%'
-
 /*
  * Set the port number in an address.  Be agnostic about the address
  * family.
include/linux/sunrpc/clnt.h +12 -0
index 37881f1..bf38c4a 100644
@@ -151,5 +151,17 @@ void		rpc_force_rebind(struct rpc_clnt *);
 size_t		rpc_peeraddr(struct rpc_clnt *, struct sockaddr *, size_t);
 const char	*rpc_peeraddr2str(struct rpc_clnt *, enum rpc_display_format_t);
 
+unsigned short	rpc_get_port(const struct sockaddr *sap);
+void		rpc_set_port(struct sockaddr *sap, const unsigned short port);
+size_t		rpc_ntop(const struct sockaddr *, char *, const size_t);
+size_t		rpc_pton(const char *, const size_t,
+			 struct sockaddr *, const size_t);
+char *		rpc_sockaddr2uaddr(const struct sockaddr *);
+size_t		rpc_uaddr2sockaddr(const char *, const size_t,
+				   struct sockaddr *, const size_t);
+
+#define IPV6_SCOPE_DELIMITER		'%'
+#define IPV6_SCOPE_ID_LEN		sizeof("%nnnnnnnnnn")
+
 #endif /* __KERNEL__ */
 #endif /* _LINUX_SUNRPC_CLNT_H */
net/sunrpc/Makefile +1 -1
index 7a594cc..6020f5b 100644
@@ -12,7 +12,7 @@ obj-$(CONFIG_SUNRPC_XPRT_RDMA) += xprtrdma/
 sunrpc-y := clnt.o xprt.o socklib.o xprtsock.o sched.o \
 	    auth.o auth_null.o auth_unix.o auth_generic.o \
 	    svc.o svcsock.o svcauth.o svcauth_unix.o \
-	    rpcb_clnt.o timer.o xdr.o \
+	    addr.o rpcb_clnt.o timer.o xdr.o \
 	    sunrpc_syms.o cache.o rpc_pipe.o \
 	    svc_xprt.o
 sunrpc-$(CONFIG_NFS_V4_1) += backchannel_rqst.o bc_svc.o
net/sunrpc/addr.c new file +524 -0

524 lines not on this page · raw message

A message that is not in the archive (sent off-list or not yet received): 20090715213709.7582.56522.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org

#8 lore raw

Re: [PATCH 03/10] SUNRPC: Provide functions for managing universal addresses

Quoted 128 lines · Chuck Lever

Introduce a set of functions in the kernel's RPC implementation for converting between a socket address and either a standard presentation address string or an RPC universal address.

The universal address functions will be used to encode and decode RPCB_FOO and NFSv4 SETCLIENTID arguments. The other functions are part of a previous promise to deliver shared functions that can be used by upper-layer protocols to display and manipulate IP addresses.

The kernel's current address printf formatters were designed specifically for kernel to user-space APIs that require a particular string format for socket addresses, thus are somewhat limited for the purposes of sunrpc.ko. The formatter for IPv6 addresses, %pI6, does not support short-handing or scope IDs. Also, these printf formatters are unique per address family, so a separate formatter string is required for printing AF_INET and AF_INET6 addresses.

---

 fs/nfs/internal.h           |    2 
 include/linux/sunrpc/clnt.h |   12 +
 net/sunrpc/Makefile         |    2 
 net/sunrpc/addr.c           |  524 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 537 insertions(+), 3 deletions(-)
 create mode 100644 net/sunrpc/addr.c
fs/nfs/internal.h +0 -2
index d3823d7..a19b7b9 100644
@@ -370,8 +370,6 @@ unsigned int nfs_page_array_len(unsigned int base, size_t len)
 		PAGE_SIZE - 1) >> PAGE_SHIFT;
 }
 
-#define IPV6_SCOPE_DELIMITER	'%'
-
 /*
  * Set the port number in an address.  Be agnostic about the address
  * family.
include/linux/sunrpc/clnt.h +12 -0
index 37881f1..bf38c4a 100644
@@ -151,5 +151,17 @@ void		rpc_force_rebind(struct rpc_clnt *);
 size_t		rpc_peeraddr(struct rpc_clnt *, struct sockaddr *, size_t);
 const char	*rpc_peeraddr2str(struct rpc_clnt *, enum rpc_display_format_t);
 
+unsigned short	rpc_get_port(const struct sockaddr *sap);
+void		rpc_set_port(struct sockaddr *sap, const unsigned short port);
+size_t		rpc_ntop(const struct sockaddr *, char *, const size_t);
+size_t		rpc_pton(const char *, const size_t,
+			 struct sockaddr *, const size_t);
+char *		rpc_sockaddr2uaddr(const struct sockaddr *);
+size_t		rpc_uaddr2sockaddr(const char *, const size_t,
+				   struct sockaddr *, const size_t);
+
+#define IPV6_SCOPE_DELIMITER		'%'
+#define IPV6_SCOPE_ID_LEN		sizeof("%nnnnnnnnnn")
+
 #endif /* __KERNEL__ */
 #endif /* _LINUX_SUNRPC_CLNT_H */
net/sunrpc/Makefile +1 -1
index 7a594cc..6020f5b 100644
@@ -12,7 +12,7 @@ obj-$(CONFIG_SUNRPC_XPRT_RDMA) += xprtrdma/
 sunrpc-y := clnt.o xprt.o socklib.o xprtsock.o sched.o \
 	    auth.o auth_null.o auth_unix.o auth_generic.o \
 	    svc.o svcsock.o svcauth.o svcauth_unix.o \
-	    rpcb_clnt.o timer.o xdr.o \
+	    addr.o rpcb_clnt.o timer.o xdr.o \
 	    sunrpc_syms.o cache.o rpc_pipe.o \
 	    svc_xprt.o
 sunrpc-$(CONFIG_NFS_V4_1) += backchannel_rqst.o bc_svc.o
net/sunrpc/addr.c new file +46 -0
new file mode 100644
index 0000000..9ddcffd
@@ -0,0 +1,524 @@
+/*
+ * Copyright 2009, Oracle.  All rights reserved.
+ *
+ * Convert socket addresses to presentation addresses and universal
+ * addresses, and vice versa.
+ *
+ * Universal addresses are introduced by RFC 1833 and further refined by
+ * recent RFCs describing NFSv4.  The universal address format is part
+ * of the external (network) interface provided by rpcbind version 3
+ * and 4, and by NFSv4.  Such an address is a string containing a
+ * presentation format IP address followed by a port number in
+ * "hibyte.lobyte" format.
+ *
+ * There are some challenges to constructing universal addresses.
+ * In particular, constructing a presentation address string for IPv4
+ * is straightforward, but it's not so easy for IPv6 addresses.  Such
+ * addresses can be "short-handed" to remove the longest string of
+ * zeros, and mapped v4 presentation addresses can appear in a special
+ * dotted-quad form.  Refer to RFC 4291, Section 2.2 for details on
+ * IPv6 presentation formats.
+ *
+ * IPv6 addresses can also include a scope ID, typically denoted by
+ * a '%' followed by a device name or a non-negative integer.
+ */
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/socket.h>
+#include <linux/in.h>
+#include <linux/in6.h>
+#include <linux/inet.h>
+
+#include <net/ipv6.h>
+
+#include <linux/sunrpc/clnt.h>
+
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+
+struct rpc_zeros {
+	unsigned int	count;
+	int		index;
+};
+
+static const struct rpc_zeros zeros_init = {
+	.count		= 0,
+	.index		= -1,

net/sunrpc/addr.c line 56

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+};
+
+static void rpc_find_longest_zero_run(const struct in6_addr *addr,
+				      struct rpc_zeros *longest)
+{
+	struct rpc_zeros zeros;
+	unsigned int i;
+
+	zeros = zeros_init;
+	*longest = zeros_init;

^^^^^^^^^^ Why do this when you could just set
longest->index to -1?

Quoted 28 lines
net/sunrpc/addr.c new file +28 -0
+
+	for (i = 0; i < ARRAY_SIZE(addr->s6_addr16); i++) {
+		if (addr->s6_addr16[i] == 0) {
+			if (zeros.index == -1)
+				zeros.index = i;
+			zeros.count++;
+		} else {
+			if (zeros.count > longest->count ||
+			    longest->index == -1)
+				*longest = zeros;
+			zeros = zeros_init;
+		}
+	}
+
+	/*
+	 * Maybe the last run of zeros didn't terminate.
+	 * Not likely for a host address.
+	 */
+	if (zeros.index != -1) {
+		if (zeros.count > longest->count ||
+		    longest->index == -1)
+			*longest = zeros;
+	}
+}
+
+/*
+ * Append a set of halfwords from @addr onto the string in @buf.
+ *

net/sunrpc/addr.c line 94

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+ * Returns the new length of the string in @buf, or zero if
+ * some error occurs.
+ */
+static size_t rpc_append_halfword6(const struct in6_addr *addr,
+				   const char *fmt,
+				   const unsigned int start,
+				   const unsigned int end,
+				   char *buf, const size_t buflen)
+{
+	char tmp[sizeof("ffff:") + 1];

You are passing 'fmt' as an argument, yet you assume it will have a format that makes sense of the above 'sizeof' calculation? Why not just drop the ':', and always use fmt=="%x"?

As far as I can see, that would simplify rpc_ntop6_shorthand too...

Quoted 50 lines
net/sunrpc/addr.c new file +50 -0
+	unsigned int i;
+	size_t len = 0;
+
+	for (i = start; i < end; i++) {
+		len = snprintf(tmp, sizeof(tmp), fmt,
+					ntohs(addr->s6_addr16[i]));
+		if (unlikely(len > sizeof(tmp)))
+			return 0;
+
+		len = strlcat(buf, tmp, buflen);
+		if (unlikely(len > buflen))
+			return 0;
+	}
+
+	return len;
+}
+
+/*
+ * This implementation depends on the Linux kernel's definition of
+ * in6_addr, which is a union of arrays that facilities dissecting
+ * the address into 16-bit chunks.
+ */
+static size_t rpc_ntop6_shorthand(const struct in6_addr *addr,
+				  struct rpc_zeros *longest,
+				  char *buf, const size_t buflen)
+{
+	size_t len;
+
+	/*
+	 * Left half, before the longest string of zeros
+	 */
+	buf[0] = '\0';
+	if (longest->index == 0) {
+		len = strlcat(buf, ":", buflen);
+		if (unlikely(len > buflen))
+			return 0;
+	} else {
+		len = rpc_append_halfword6(addr, "%x:",
+					0, longest->index, buf, buflen);
+		if (unlikely(len == 0))
+			return 0;
+	}
+
+	/*
+	 * Right half, after the longest string of zeros
+	 */
+	if (longest->index + longest->count >= ARRAY_SIZE(addr->s6_addr16)) {
+		len = strlcat(buf, ":", buflen);
+		if (unlikely(len > buflen))
+			return 0;

net/sunrpc/addr.c line 154

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+	} else {
+		len = rpc_append_halfword6(addr, ":%x",
+			longest->index + longest->count,
+			ARRAY_SIZE(addr->s6_addr16), buf, buflen);
+		if (unlikely(len == 0))
+			return 0;
+	}
+
+	return len;
+}

The shorthand format is, AFAICS, not an obligatory notation. Why should we implement it, if it involves all this complication?

Quoted 7 lines
net/sunrpc/addr.c new file +7 -0
+
+static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
+				  char *buf, const int buflen)
+{
+	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
+	const struct in6_addr *addr = &sin6->sin6_addr;
+	struct rpc_zeros longest;

net/sunrpc/addr.c line 171

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+
+	/*
+	 * RFC 4291, Section 2.2.3
+	 *
+	 * Special presentation address format for mapped v4
+	 * addresses.
+	 */
+	if (ipv6_addr_v4mapped(addr))
+		return snprintf(buf, buflen, "::ffff:%pI4",
+					&addr->s6_addr32[3]);

Err... Won't that give you ::ffff:x.x.x.x ?

Quoted 11 lines
net/sunrpc/addr.c new file +11 -0
+	/*
+	 * RFC 4291, Section 2.2.2
+	 *
+	 * See if address can be short-handed.
+	 */
+	rpc_find_longest_zero_run(addr, &longest);
+	if (longest.index != -1 && longest.count > 1)
+		return rpc_ntop6_shorthand(addr, &longest, buf, buflen);
+
+	/*
+	 * RFC 4291, Section 2.2.1

net/sunrpc/addr.c line 193

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+	 *
+	 * Short-handing not possible, so spell out full
+	 * address.  We don't want leading zeros in each
+	 * halfword, so avoid %pI6.
+	 */
+	return snprintf(buf, buflen, "%x:%x:%x:%x:%x:%x:%x:%x",
+		ntohs(addr->s6_addr16[0]), ntohs(addr->s6_addr16[1]),
+		ntohs(addr->s6_addr16[2]), ntohs(addr->s6_addr16[3]),
+		ntohs(addr->s6_addr16[4]), ntohs(addr->s6_addr16[5]),
+		ntohs(addr->s6_addr16[6]), ntohs(addr->s6_addr16[7]));

Why not use %pI6 ?

Quoted 73 lines
net/sunrpc/addr.c new file +73 -0
+}
+
+static size_t rpc_ntop6(const struct sockaddr *sap,
+			char *buf, const size_t buflen)
+{
+	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
+	char scopebuf[IPV6_SCOPE_ID_LEN];
+	size_t len;
+	int rc;
+
+	len = rpc_ntop6_noscopeid(sap, buf, buflen);
+	if (unlikely(len == 0))
+		return len;
+
+	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
+	    !(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_SITELOCAL))
+		return len;
+
+	rc = snprintf(scopebuf, sizeof(scopebuf), "%c%u",
+			IPV6_SCOPE_DELIMITER, sin6->sin6_scope_id);
+	if (unlikely((size_t)rc > sizeof(scopebuf)))
+		return 0;
+
+	len += rc;
+	if (unlikely(len > buflen))
+		return 0;
+
+	strcat(buf, scopebuf);
+	return len;
+}
+
+#else	/* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
+
+static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
+				  char *buf, const int buflen)
+{
+	return 0;
+}
+
+static size_t rpc_ntop6(const struct sockaddr *sap,
+			char *buf, const size_t buflen)
+{
+	return 0;
+}
+
+#endif	/* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
+
+static int rpc_ntop4(const struct sockaddr *sap,
+		     char *buf, const size_t buflen)
+{
+	const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
+
+	return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
+}
+
+/**
+ * rpc_ntop - construct a presentation address in @buf
+ * @sap: socket address
+ * @buf: construction area
+ * @buflen: size of @buf, in bytes
+ *
+ * Plants a %NUL-terminated string in @buf and returns the length
+ * of the string, excluding the %NUL.  Otherwise zero is returned.
+ */
+size_t rpc_ntop(const struct sockaddr *sap, char *buf, const size_t buflen)
+{
+	char tmp[INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 1];
+	size_t len;
+
+	switch (sap->sa_family) {
+	case AF_INET:
+		len = rpc_ntop4(sap, tmp, sizeof(tmp));
+		break;

net/sunrpc/addr.c line 276

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+	case AF_INET6:
+		len = rpc_ntop6(sap, tmp, sizeof(tmp));
+		break;
+	default:
+		return 0;
+	}
+
+	if (unlikely(len == 0 || len > sizeof(tmp) || len > buflen))
+		return 0;
+	return strlcpy(buf, tmp, buflen);

Why two copies?

Quoted 38 lines
net/sunrpc/addr.c new file +2 -0
+/**
+ * rpc_sockaddr2uaddr - Construct a universal address string from

@sap.
+ * @sap: socket address
+ *
+ * Returns a %NUL-terminated string in dynamically allocated memory;
+ * otherwise NULL is returned if an error occurred. Caller must
+ * free the returned string.
+ */

net/sunrpc/addr.c new file +14 -0
+char *rpc_sockaddr2uaddr(const struct sockaddr *sap)
+{
+       char portbuf[RPCBIND_MAXUADDRPLEN + 1];
+       char addrbuf[RPCBIND_MAXUADDRLEN + 1];
+       unsigned short port;
+
+       switch (sap->sa_family) {
+       case AF_INET:
+               if (rpc_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
+                       return NULL;
+               port = ntohs(((struct sockaddr_in *)sap)->sin_port);
+               break;
+       case AF_INET6:
+               if (rpc_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf))

== 0)

net/sunrpc/addr.c new file +9 -0
+                       return NULL;
+               port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
+               break;
+       default:
+               return NULL;
+       }
+
+       if (snprintf(portbuf, sizeof(portbuf),
+                    ".%u.%u", port >> 8, port & 0xff) >

(int)sizeof(portbuf))

net/sunrpc/addr.c new file +3 -0
+               return NULL;
+
+       if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) >

sizeof(addrbuf))

net/sunrpc/addr.c line 433

Quoted 3 lines
net/sunrpc/addr.c new file +3 -0
+               return NULL;
+
+       return kstrdup(addrbuf, GFP_ATOMIC);

GFP_ATOMIC? Why?

Quoted 4 lines
net/sunrpc/addr.c new file +3 -0
+}
+EXPORT_SYMBOL_GPL(rpc_sockaddr2uaddr);
 
+unsigned short rpc_get_port(const struct sockaddr *sap)

net/sunrpc/addr.c line 511

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+{
+	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;
+}
+EXPORT_SYMBOL_GPL(rpc_get_port);

inline helper?

net/sunrpc/addr.c line 524

Quoted 13 lines
net/sunrpc/addr.c new file +13 -0
+
+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;
+	}
+}
+EXPORT_SYMBOL_GPL(rpc_set_port);

ditto

A message that is not in the archive (sent off-list or not yet received): 1247774056.12292.131.camel-rJ7iovZKK19ZJLDQqaL3InhyD016LWXt@public.gmane.org

#10 lore raw

Re: [PATCH 03/10] SUNRPC: Provide functions for managing universal addresses

Just to answer a few of the questions below...

Quoted 140 lines · Trond Myklebust
Quoted 139 lines · Chuck Lever

Introduce a set of functions in the kernel's RPC implementation for converting between a socket address and either a standard presentation address string or an RPC universal address.

The universal address functions will be used to encode and decode RPCB_FOO and NFSv4 SETCLIENTID arguments. The other functions are part of a previous promise to deliver shared functions that can be used by upper-layer protocols to display and manipulate IP addresses.

The kernel's current address printf formatters were designed specifically for kernel to user-space APIs that require a particular string format for socket addresses, thus are somewhat limited for the purposes of sunrpc.ko. The formatter for IPv6 addresses, %pI6, does not support short-handing or scope IDs. Also, these printf formatters are unique per address family, so a separate formatter string is required for printing AF_INET and AF_INET6 addresses.

---

fs/nfs/internal.h           |    2
include/linux/sunrpc/clnt.h |   12 +
net/sunrpc/Makefile         |    2
net/sunrpc/addr.c           |  524 +++++++++++++++++++++++++++++++++ 
++++++++++
4 files changed, 537 insertions(+), 3 deletions(-)

create mode 100644 net/sunrpc/addr.c

fs/nfs/internal.h +0 -2
index d3823d7..a19b7b9 100644
@@ -370,8 +370,6 @@ unsigned int nfs_page_array_len(unsigned int  
 base, size_t len)
 		PAGE_SIZE - 1) >> PAGE_SHIFT;
 }
 
-#define IPV6_SCOPE_DELIMITER	'%'
-
 /*
 * Set the port number in an address.  Be agnostic about the address

* family.

include/linux/sunrpc/clnt.h +12 -0
index 37881f1..bf38c4a 100644
@@ -151,5 +151,17 @@ void		rpc_force_rebind(struct rpc_clnt *);
 size_t		rpc_peeraddr(struct rpc_clnt *, struct sockaddr *, size_t);
 const char	*rpc_peeraddr2str(struct rpc_clnt *, enum rpc_display_format_t);
 
+unsigned short	rpc_get_port(const struct sockaddr *sap);
+void		rpc_set_port(struct sockaddr *sap, const unsigned short port);
+size_t		rpc_ntop(const struct sockaddr *, char *, const size_t);
+size_t		rpc_pton(const char *, const size_t,
+			 struct sockaddr *, const size_t);
+char *		rpc_sockaddr2uaddr(const struct sockaddr *);
+size_t		rpc_uaddr2sockaddr(const char *, const size_t,
+				   struct sockaddr *, const size_t);
+
+#define IPV6_SCOPE_DELIMITER		'%'
+#define IPV6_SCOPE_ID_LEN		sizeof("%nnnnnnnnnn")
+
 #endif /* __KERNEL__ */
 #endif /* _LINUX_SUNRPC_CLNT_H */
net/sunrpc/Makefile +1 -1
index 7a594cc..6020f5b 100644
@@ -12,7 +12,7 @@ obj-$(CONFIG_SUNRPC_XPRT_RDMA) += xprtrdma/
 sunrpc-y := clnt.o xprt.o socklib.o xprtsock.o sched.o \
 	    auth.o auth_null.o auth_unix.o auth_generic.o \
 	    svc.o svcsock.o svcauth.o svcauth_unix.o \
-	    rpcb_clnt.o timer.o xdr.o \
+	    addr.o rpcb_clnt.o timer.o xdr.o \
 	    sunrpc_syms.o cache.o rpc_pipe.o \
 	    svc_xprt.o

sunrpc-$(CONFIG_NFS_V4_1) += backchannel_rqst.o bc_svc.o

net/sunrpc/addr.c new file +7 -0
new file mode 100644
index 0000000..9ddcffd
@@ -0,0 +1,524 @@
+/*
+ * Copyright 2009, Oracle.  All rights reserved.
+ *
+ * Convert socket addresses to presentation addresses and universal
+ * addresses, and vice versa.
+ *
+ * Universal addresses are introduced by RFC 1833 and further  

refined by
+ * recent RFCs describing NFSv4. The universal address format is part
+ * of the external (network) interface provided by rpcbind version 3
+ * and 4, and by NFSv4. Such an address is a string containing a
+ * presentation format IP address followed by a port number in
+ * "hibyte.lobyte" format.
+ *
+ * There are some challenges to constructing universal addresses.
+ * In particular, constructing a presentation address string for IPv4
+ * is straightforward, but it's not so easy for IPv6 addresses. Such
+ * addresses can be "short-handed" to remove the longest string of
+ * zeros, and mapped v4 presentation addresses can appear in a special
+ * dotted-quad form. Refer to RFC 4291, Section 2.2 for details on
+ * IPv6 presentation formats.
+ *
+ * IPv6 addresses can also include a scope ID, typically denoted by
+ * a '%' followed by a device name or a non-negative integer.
+ */

net/sunrpc/addr.c new file +26 -0
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/socket.h>
+#include <linux/in.h>
+#include <linux/in6.h>
+#include <linux/inet.h>
+
+#include <net/ipv6.h>
+
+#include <linux/sunrpc/clnt.h>
+
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+
+struct rpc_zeros {
+	unsigned int	count;
+	int		index;
+};
+
+static const struct rpc_zeros zeros_init = {
+	.count		= 0,
+	.index		= -1,
+};
+
+static void rpc_find_longest_zero_run(const struct in6_addr *addr,
+				      struct rpc_zeros *longest)
Quoted 8 lines

net/sunrpc/addr.c line 56

Quoted 6 lines
net/sunrpc/addr.c new file +6 -0
+{
+	struct rpc_zeros zeros;
+	unsigned int i;
+
+	zeros = zeros_init;
+	*longest = zeros_init;

^^^^^^^^^^ Why do this when you could just set
longest->index to -1?

Documentation.

Quoted 43 lines
Quoted 28 lines
net/sunrpc/addr.c new file +28 -0
+
+	for (i = 0; i < ARRAY_SIZE(addr->s6_addr16); i++) {
+		if (addr->s6_addr16[i] == 0) {
+			if (zeros.index == -1)
+				zeros.index = i;
+			zeros.count++;
+		} else {
+			if (zeros.count > longest->count ||
+			    longest->index == -1)
+				*longest = zeros;
+			zeros = zeros_init;
+		}
+	}
+
+	/*
+	 * Maybe the last run of zeros didn't terminate.
+	 * Not likely for a host address.
+	 */
+	if (zeros.index != -1) {
+		if (zeros.count > longest->count ||
+		    longest->index == -1)
+			*longest = zeros;
+	}
+}
+
+/*
+ * Append a set of halfwords from @addr onto the string in @buf.
+ *

net/sunrpc/addr.c line 94

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+ * Returns the new length of the string in @buf, or zero if
+ * some error occurs.
+ */
+static size_t rpc_append_halfword6(const struct in6_addr *addr,
+				   const char *fmt,
+				   const unsigned int start,
+				   const unsigned int end,
+				   char *buf, const size_t buflen)
+{
+	char tmp[sizeof("ffff:") + 1];

You are passing 'fmt' as an argument, yet you assume it will have a format that makes sense of the above 'sizeof' calculation? Why not just drop the ':', and always use fmt=="%x"?

Quoted 1 line

As far as I can see, that would simplify rpc_ntop6_shorthand too...

OK.

Quoted 48 lines
Quoted 48 lines
net/sunrpc/addr.c new file +47 -0
+	unsigned int i;
+	size_t len = 0;
+
+	for (i = start; i < end; i++) {
+		len = snprintf(tmp, sizeof(tmp), fmt,
+					ntohs(addr->s6_addr16[i]));
+		if (unlikely(len > sizeof(tmp)))
+			return 0;
+
+		len = strlcat(buf, tmp, buflen);
+		if (unlikely(len > buflen))
+			return 0;
+	}
+
+	return len;
+}
+
+/*
+ * This implementation depends on the Linux kernel's definition of
+ * in6_addr, which is a union of arrays that facilities dissecting
+ * the address into 16-bit chunks.
+ */
+static size_t rpc_ntop6_shorthand(const struct in6_addr *addr,
+				  struct rpc_zeros *longest,
+				  char *buf, const size_t buflen)
+{
+	size_t len;
+
+	/*
+	 * Left half, before the longest string of zeros
+	 */
+	buf[0] = '\0';
+	if (longest->index == 0) {
+		len = strlcat(buf, ":", buflen);
+		if (unlikely(len > buflen))
+			return 0;
+	} else {
+		len = rpc_append_halfword6(addr, "%x:",
+					0, longest->index, buf, buflen);
+		if (unlikely(len == 0))
+			return 0;
+	}
+
+	/*
+	 * Right half, after the longest string of zeros
+	 */
+	if (longest->index + longest->count >= ARRAY_SIZE(addr- 
Quoted 1 line

s6_addr16)) {

Quoted 16 lines

net/sunrpc/addr.c line 154

Quoted 13 lines
net/sunrpc/addr.c new file +13 -0
+		len = strlcat(buf, ":", buflen);
+		if (unlikely(len > buflen))
+			return 0;
+	} else {
+		len = rpc_append_halfword6(addr, ":%x",
+			longest->index + longest->count,
+			ARRAY_SIZE(addr->s6_addr16), buf, buflen);
+		if (unlikely(len == 0))
+			return 0;
+	}
+
+	return len;
+}

The shorthand format is, AFAICS, not an obligatory notation. Why should we implement it, if it involves all this complication?

One reason is a shorthanded presentation address can be stored in significantly less memory, or sent on the network in fewer bytes. Another is that these addresses are stored in rpcbind, and advertised, so they are often looked at by humans.

IMO the present code is less complex than our RDMA/RPC implementation or even the kernel's full vsnprintf(), and once we get it right, it will probably never change again. It's no uglier than what's in xdr.c, for example. So I don't buy the "too complex" argument.

If we replace all this code with "%pI6" however, we should explicitly check for ANYADDR and loopback addresses and use shorthands for them: "::" and "::1", as the code in rpcb_clnt.c now does for ANYADDR.

Quoted 11 lines
Quoted 11 lines
net/sunrpc/addr.c new file +11 -0
+
+static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
+				  char *buf, const int buflen)
+{
+	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
+	const struct in6_addr *addr = &sin6->sin6_addr;
+	struct rpc_zeros longest;
+
+	/*
+	 * RFC 4291, Section 2.2.3
+	 *
Quoted 8 lines

net/sunrpc/addr.c line 171

Quoted 6 lines
net/sunrpc/addr.c new file +6 -0
+	 * Special presentation address format for mapped v4
+	 * addresses.
+	 */
+	if (ipv6_addr_v4mapped(addr))
+		return snprintf(buf, buflen, "::ffff:%pI4",
+					&addr->s6_addr32[3]);

Err... Won't that give you ::ffff:x.x.x.x ?

Yes, that's a standard way to express a mapped IPv4 presentation address.

Quoted 15 lines
Quoted 15 lines
net/sunrpc/addr.c new file +15 -0
+	/*
+	 * RFC 4291, Section 2.2.2
+	 *
+	 * See if address can be short-handed.
+	 */
+	rpc_find_longest_zero_run(addr, &longest);
+	if (longest.index != -1 && longest.count > 1)
+		return rpc_ntop6_shorthand(addr, &longest, buf, buflen);
+
+	/*
+	 * RFC 4291, Section 2.2.1
+	 *
+	 * Short-handing not possible, so spell out full
+	 * address.  We don't want leading zeros in each
+	 * halfword, so avoid %pI6.
Quoted 8 lines

net/sunrpc/addr.c line 193

Quoted 6 lines
net/sunrpc/addr.c new file +6 -0
+	 */
+	return snprintf(buf, buflen, "%x:%x:%x:%x:%x:%x:%x:%x",
+		ntohs(addr->s6_addr16[0]), ntohs(addr->s6_addr16[1]),
+		ntohs(addr->s6_addr16[2]), ntohs(addr->s6_addr16[3]),
+		ntohs(addr->s6_addr16[4]), ntohs(addr->s6_addr16[5]),
+		ntohs(addr->s6_addr16[6]), ntohs(addr->s6_addr16[7]));

Why not use %pI6 ?

See the comment.

For some reason %pI4 does not add leading zeroes (eg. 010.000.000.004) but %pI6 does (eg. fe80:0000:0000:cd59:0017:0054:bda4:000f). Probably some networking-specific user space parser depends on it.

Leaving out the leading zeroes can shorten IPv6 presentation addresses significantly.

Quoted 78 lines
Quoted 78 lines
net/sunrpc/addr.c new file +65 -0
+}
+
+static size_t rpc_ntop6(const struct sockaddr *sap,
+			char *buf, const size_t buflen)
+{
+	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
+	char scopebuf[IPV6_SCOPE_ID_LEN];
+	size_t len;
+	int rc;
+
+	len = rpc_ntop6_noscopeid(sap, buf, buflen);
+	if (unlikely(len == 0))
+		return len;
+
+	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
+	    !(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_SITELOCAL))
+		return len;
+
+	rc = snprintf(scopebuf, sizeof(scopebuf), "%c%u",
+			IPV6_SCOPE_DELIMITER, sin6->sin6_scope_id);
+	if (unlikely((size_t)rc > sizeof(scopebuf)))
+		return 0;
+
+	len += rc;
+	if (unlikely(len > buflen))
+		return 0;
+
+	strcat(buf, scopebuf);
+	return len;
+}
+
+#else	/* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
+
+static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
+				  char *buf, const int buflen)
+{
+	return 0;
+}
+
+static size_t rpc_ntop6(const struct sockaddr *sap,
+			char *buf, const size_t buflen)
+{
+	return 0;
+}
+
+#endif	/* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
+
+static int rpc_ntop4(const struct sockaddr *sap,
+		     char *buf, const size_t buflen)
+{
+	const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
+
+	return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
+}
+
+/**
+ * rpc_ntop - construct a presentation address in @buf
+ * @sap: socket address
+ * @buf: construction area
+ * @buflen: size of @buf, in bytes
+ *
+ * Plants a %NUL-terminated string in @buf and returns the length
+ * of the string, excluding the %NUL.  Otherwise zero is returned.
+ */
+size_t rpc_ntop(const struct sockaddr *sap, char *buf, const  

size_t buflen)

net/sunrpc/addr.c new file +12 -0
+{
+	char tmp[INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 1];
+	size_t len;
+
+	switch (sap->sa_family) {
+	case AF_INET:
+		len = rpc_ntop4(sap, tmp, sizeof(tmp));
+		break;
+	case AF_INET6:
+		len = rpc_ntop6(sap, tmp, sizeof(tmp));
+		break;
+	default:
Quoted 8 lines

net/sunrpc/addr.c line 276

Quoted 6 lines
net/sunrpc/addr.c new file +6 -0
+		return 0;
+	}
+
+	if (unlikely(len == 0 || len > sizeof(tmp) || len > buflen))
+		return 0;
+	return strlcpy(buf, tmp, buflen);

Why two copies?

It simplifies the assumptions that rpc_ntop6() has to make. One can go away, if you like.

Quoted 37 lines
Quoted 37 lines

+/**
+ * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
+ * @sap: socket address
+ *
+ * Returns a %NUL-terminated string in dynamically allocated memory;
+ * otherwise NULL is returned if an error occurred. Caller must
+ * free the returned string.
+ */

net/sunrpc/addr.c new file +14 -0
+char *rpc_sockaddr2uaddr(const struct sockaddr *sap)
+{
+       char portbuf[RPCBIND_MAXUADDRPLEN + 1];
+       char addrbuf[RPCBIND_MAXUADDRLEN + 1];
+       unsigned short port;
+
+       switch (sap->sa_family) {
+       case AF_INET:
+               if (rpc_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
+                       return NULL;
+               port = ntohs(((struct sockaddr_in *)sap)->sin_port);
+               break;
+       case AF_INET6:
+               if (rpc_ntop6_noscopeid(sap, addrbuf,  

sizeof(addrbuf))
== 0)

net/sunrpc/addr.c new file +2 -0
+                       return NULL;
+               port = ntohs(((struct sockaddr_in6 *)sap)- 
Quoted 1 line

sin6_port);

net/sunrpc/addr.c new file +7 -0
+               break;
+       default:
+               return NULL;
+       }
+
+       if (snprintf(portbuf, sizeof(portbuf),
+                    ".%u.%u", port >> 8, port & 0xff) >

(int)sizeof(portbuf))
+ return NULL;

Quoted 8 lines

net/sunrpc/addr.c line 433

Quoted 6 lines
net/sunrpc/addr.c new file +2 -0
+
+       if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) >

sizeof(addrbuf))

net/sunrpc/addr.c new file +3 -0
+               return NULL;
+
+       return kstrdup(addrbuf, GFP_ATOMIC);

GFP_ATOMIC? Why?

No reason. Will fix.

Quoted 16 lines
Quoted 4 lines
net/sunrpc/addr.c new file +3 -0
+}
+EXPORT_SYMBOL_GPL(rpc_sockaddr2uaddr);
 
+unsigned short rpc_get_port(const struct sockaddr *sap)

net/sunrpc/addr.c line 511

Quoted 10 lines
net/sunrpc/addr.c new file +10 -0
+{
+	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;
+}
+EXPORT_SYMBOL_GPL(rpc_get_port);

inline helper?

OK.

Quoted 7 lines
Quoted 7 lines
net/sunrpc/addr.c new file +7 -0
+
+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;
Quoted 8 lines

net/sunrpc/addr.c line 524

Quoted 6 lines
net/sunrpc/addr.c new file +6 -0
+	case AF_INET6:
+		((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
+		break;
+	}
+}
+EXPORT_SYMBOL_GPL(rpc_set_port);

ditto

--
Chuck Lever
chuck[dot]lever[at]oracle[dot]com

#11 lore raw

PATCH 4/10 SUNRPC: Remove duplicate universal address generation

In reply to a message not in the archive

RPC universal address generation is currently done in several places:
rpcb_clnt.c, nfs4proc.c xprtsock.c, and xprtrdma.c. Remove the redundant cases that convert a socket address to a universal address. The nfs4proc.c case takes a pre-formatted presentation address string, not a socket address, so we'll leave that one.

Because the new uaddr constructor uses the recently introduced rpc_ntop(), it now supports proper "::" shorthanding for IPv6 addresses. This allows the kernel to register properly formed universal addresses with the local rpcbind service, in _all_ cases.

The kernel can now also send properly formed universal addresses in RPCB_GETADDR requests, and support link-local properly when encoding and decoding IPv6 addresses.

---

 include/linux/sunrpc/xprt.h     |    1 -
 net/sunrpc/rpcb_clnt.c          |   48 ++++++++++++++++++++++++---------------
 net/sunrpc/xprtrdma/transport.c |    8 -------
 net/sunrpc/xprtsock.c           |   18 ---------------
 4 files changed, 29 insertions(+), 46 deletions(-)
include/linux/sunrpc/xprt.h +0 -1
index 1175d58..65fad95 100644
@@ -41,7 +41,6 @@ enum rpc_display_format_t {
 	RPC_DISPLAY_ALL,
 	RPC_DISPLAY_HEX_ADDR,
 	RPC_DISPLAY_HEX_PORT,
-	RPC_DISPLAY_UNIVERSAL_ADDR,
 	RPC_DISPLAY_NETID,
 	RPC_DISPLAY_MAX,
 };
net/sunrpc/rpcb_clnt.c +29 -19

100 lines not on this page · raw message

net/sunrpc/xprtrdma/transport.c +0 -8
index 1dd6123..537c210 100644
@@ -202,14 +202,6 @@ xprt_rdma_format_addresses(struct rpc_xprt *xprt)
 		snprintf(buf, 8, "%4hx", ntohs(addr->sin_port));
 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
 
-	buf = kzalloc(30, GFP_KERNEL);
-	if (buf)
-		snprintf(buf, 30, "%pI4.%u.%u",
-			&addr->sin_addr.s_addr,
-			ntohs(addr->sin_port) >> 8,
-			ntohs(addr->sin_port) & 0xff);
-	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
-
 	/* netid */
 	xprt->address_strings[RPC_DISPLAY_NETID] = "rdma";
 }
net/sunrpc/xprtsock.c +0 -18
index 83c73c4..a42c2ad 100644
@@ -341,15 +341,6 @@ static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
 	}
 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
 
-	buf = kzalloc(30, GFP_KERNEL);
-	if (buf) {
-		snprintf(buf, 30, "%pI4.%u.%u",
-				&addr->sin_addr.s_addr,
-				ntohs(addr->sin_port) >> 8,
-				ntohs(addr->sin_port) & 0xff);
-	}
-	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
-
 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
 }
 
@@ -397,15 +388,6 @@ static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
 	}
 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
 
-	buf = kzalloc(50, GFP_KERNEL);
-	if (buf) {
-		snprintf(buf, 50, "%pI6.%u.%u",
-			 &addr->sin6_addr,
-			 ntohs(addr->sin6_port) >> 8,
-			 ntohs(addr->sin6_port) & 0xff);
-	}
-	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
-
 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
 }
 
#12 lore raw

PATCH 5/10 SUNRPC: Use rpc_ntop() for constructing transport address strings

In reply to a message not in the archive

Clean up: In addition to using the new generic rpc_ntop() and rpc_get_port() functions, have the RPC client compute the presentation address buffer sizes dynamically using kstrdup().

---

 net/sunrpc/xprtrdma/transport.c |   45 ++++++----------
 net/sunrpc/xprtsock.c           |  109 ++++++++++++---------------------------
 2 files changed, 52 insertions(+), 102 deletions(-)
net/sunrpc/xprtrdma/transport.c +18 -27
index 537c210..5f9b867 100644
@@ -168,39 +168,30 @@ static struct rpc_xprt_ops xprt_rdma_procs;	/* forward reference */
 static void
 xprt_rdma_format_addresses(struct rpc_xprt *xprt)
 {
-	struct sockaddr_in *addr = (struct sockaddr_in *)
+	struct sockaddr *sap = (struct sockaddr *)
 					&rpcx_to_rdmad(xprt).addr;
-	char *buf;
+	struct sockaddr_in *sin = (struct sockaddr_in *)sap;
+	char buf[64];
 
-	buf = kzalloc(20, GFP_KERNEL);
-	if (buf)
-		snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
-	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
+	(void)rpc_ntop(sap, buf, sizeof(buf));
+	xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
 
-	buf = kzalloc(8, GFP_KERNEL);
-	if (buf)
-		snprintf(buf, 8, "%u", ntohs(addr->sin_port));
-	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
+	(void)snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
+	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
 
 	xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
 
-	buf = kzalloc(48, GFP_KERNEL);
-	if (buf)
-		snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
-			&addr->sin_addr.s_addr,
-			ntohs(addr->sin_port), "rdma");
-	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
-
-	buf = kzalloc(10, GFP_KERNEL);
-	if (buf)
-		snprintf(buf, 10, "%02x%02x%02x%02x",
-			NIPQUAD(addr->sin_addr.s_addr));
-	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
-
-	buf = kzalloc(8, GFP_KERNEL);
-	if (buf)
-		snprintf(buf, 8, "%4hx", ntohs(addr->sin_port));
-	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
+	(void)snprintf(buf, sizeof(buf), "addr=%s port=%s proto=rdma",
+			xprt->address_strings[RPC_DISPLAY_ADDR],
+			xprt->address_strings[RPC_DISPLAY_PORT]);
+	xprt->address_strings[RPC_DISPLAY_ALL] = kstrdup(buf, GFP_KERNEL);
+
+	(void)snprintf(buf, sizeof(buf), "%02x%02x%02x%02x",
+				NIPQUAD(sin->sin_addr.s_addr));
+	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
+
+	(void)snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
+	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
 
 	/* netid */
 	xprt->address_strings[RPC_DISPLAY_NETID] = "rdma";
net/sunrpc/xprtsock.c +34 -75

133 lines not on this page · raw message

#13 lore raw

PATCH 6/10 NFS: Replace nfs_parse_ip_address() with rpc_pton()

In reply to a message not in the archive

Clean up: Use the common routine now provided in sunrpc.ko for parsing mount addresses.

---

 fs/nfs/internal.h      |    1 
 fs/nfs/nfs4namespace.c |    6 +-
 fs/nfs/super.c         |  148 ++++++------------------------------------------
 3 files changed, 22 insertions(+), 133 deletions(-)
fs/nfs/internal.h +0 -1
index a19b7b9..8f8a760 100644
@@ -214,7 +214,6 @@ void nfs_zap_acl_cache(struct inode *inode);
 extern int nfs_wait_bit_killable(void *word);
 
 /* super.c */
-void nfs_parse_ip_address(char *, size_t, struct sockaddr *, size_t *);
 extern struct file_system_type nfs_xdev_fs_type;
 #ifdef CONFIG_NFS_V4
 extern struct file_system_type nfs4_xdev_fs_type;
fs/nfs/nfs4namespace.c +3 -3
index 2a2a0a7..43c86b7 100644
@@ -121,9 +121,9 @@ static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
 
 		if (memchr(buf->data, IPV6_SCOPE_DELIMITER, buf->len))
 			continue;
-		nfs_parse_ip_address(buf->data, buf->len,
-				mountdata->addr, &mountdata->addrlen);
-		if (mountdata->addr->sa_family == AF_UNSPEC)
+		mountdata->addrlen = rpc_pton(buf->data, buf->len,
+				mountdata->addr, mountdata->addrlen);
+		if (mountdata->addrlen == 0)
 			continue;
 		nfs_set_port(mountdata->addr, NFS_PORT);
 
fs/nfs/super.c +19 -129

178 lines not on this page · raw message

#14 lore raw

PATCH 7/10 NFS: Replace nfs_set_port() with rpc_set_port()

In reply to a message not in the archive

Clean up.

---

 fs/nfs/internal.h      |   19 -------------------
 fs/nfs/nfs4namespace.c |    2 +-
 fs/nfs/super.c         |    6 +++---
 3 files changed, 4 insertions(+), 23 deletions(-)
fs/nfs/internal.h +0 -19
index 8f8a760..b39e32a 100644
@@ -368,22 +368,3 @@ unsigned int nfs_page_array_len(unsigned int base, size_t len)
 	return ((unsigned long)len + (unsigned long)base +
 		PAGE_SIZE - 1) >> PAGE_SHIFT;
 }
-
-/*
- * Set the port number in an address.  Be agnostic about the address
- * family.
- */
-static inline void nfs_set_port(struct sockaddr *sap, unsigned short port)
-{
-	struct sockaddr_in *ap = (struct sockaddr_in *)sap;
-	struct sockaddr_in6 *ap6 = (struct sockaddr_in6 *)sap;
-
-	switch (sap->sa_family) {
-	case AF_INET:
-		ap->sin_port = htons(port);
-		break;
-	case AF_INET6:
-		ap6->sin6_port = htons(port);
-		break;
-	}
-}
fs/nfs/nfs4namespace.c +1 -1
index 43c86b7..ef22ee8 100644
@@ -125,7 +125,7 @@ static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
 				mountdata->addr, mountdata->addrlen);
 		if (mountdata->addrlen == 0)
 			continue;
-		nfs_set_port(mountdata->addr, NFS_PORT);
+		rpc_set_port(mountdata->addr, NFS_PORT);
 
 		memcpy(page2, buf->data, buf->len);
 		page2[buf->len] = '\0';
fs/nfs/super.c +3 -3
index 354ef4c..5fd5a41 100644
@@ -1415,7 +1415,7 @@ static int nfs_try_mount(struct nfs_parsed_mount_data *args,
 	/*
 	 * autobind will be used if mount_server.port == 0
 	 */
-	nfs_set_port(request.sap, args->mount_server.port);
+	rpc_set_port(request.sap, args->mount_server.port);
 
 	/*
 	 * Now ask the mount server to map our export path
@@ -1704,7 +1704,7 @@ static int nfs_validate_mount_data(void *options,
 						&args->nfs_server.address))
 			goto out_no_address;
 
-		nfs_set_port((struct sockaddr *)&args->nfs_server.address,
+		rpc_set_port((struct sockaddr *)&args->nfs_server.address,
 				args->nfs_server.port);
 
 		nfs_set_mount_transport_protocol(args);
@@ -2337,7 +2337,7 @@ static int nfs4_validate_mount_data(void *options,
 						&args->nfs_server.address))
 			return -EINVAL;
 
-		nfs_set_port((struct sockaddr *)&args->nfs_server.address,
+		rpc_set_port((struct sockaddr *)&args->nfs_server.address,
 				args->nfs_server.port);
 
 		nfs_validate_transport_protocol(args);
#15 lore raw

PATCH 8/10 lockd: Replace nlm_clear_port()

In reply to a message not in the archive

Clean up: Use shared rpc_set_port() function instead of nlm_clear_port().

---

 fs/lockd/host.c |   14 +-------------
 1 files changed, 1 insertions(+), 13 deletions(-)
fs/lockd/host.c +1 -13
index 99d737b..7cb076a 100644
@@ -87,18 +87,6 @@ static unsigned int nlm_hash_address(const struct sockaddr *sap)
 	return hash & (NLM_HOST_NRHASH - 1);
 }
 
-static void nlm_clear_port(struct sockaddr *sap)
-{
-	switch (sap->sa_family) {
-	case AF_INET:
-		((struct sockaddr_in *)sap)->sin_port = 0;
-		break;
-	case AF_INET6:
-		((struct sockaddr_in6 *)sap)->sin6_port = 0;
-		break;
-	}
-}
-
 /*
  * Common host lookup routine for server & client
  */
@@ -177,7 +165,7 @@ static struct nlm_host *nlm_lookup_host(struct nlm_lookup_host_info *ni)
 	host->h_addrbuf    = nsm->sm_addrbuf;
 	memcpy(nlm_addr(host), ni->sap, ni->salen);
 	host->h_addrlen = ni->salen;
-	nlm_clear_port(nlm_addr(host));
+	rpc_set_port(nlm_addr(host), 0);
 	memcpy(nlm_srcaddr(host), ni->src_sap, ni->src_len);
 	host->h_version    = ni->version;
 	host->h_proto      = ni->protocol;
#16 lore raw

PATCH 9/10 lockd: Replace nsm_display_address() with rpc_ntop()

In reply to a message not in the archive

Clean up.

---

 fs/lockd/mon.c |   44 +++++---------------------------------------
 1 files changed, 5 insertions(+), 39 deletions(-)
fs/lockd/mon.c +5 -39
index 7fce1b5..30c9331 100644
@@ -61,43 +61,6 @@ static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm)
 	return (struct sockaddr *)&nsm->sm_addr;
 }
 
-static void nsm_display_ipv4_address(const struct sockaddr *sap, char *buf,
-				     const size_t len)
-{
-	const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
-	snprintf(buf, len, "%pI4", &sin->sin_addr.s_addr);
-}
-
-static void nsm_display_ipv6_address(const struct sockaddr *sap, char *buf,
-				     const size_t len)
-{
-	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
-
-	if (ipv6_addr_v4mapped(&sin6->sin6_addr))
-		snprintf(buf, len, "%pI4", &sin6->sin6_addr.s6_addr32[3]);
-	else if (sin6->sin6_scope_id != 0)
-		snprintf(buf, len, "%pI6%%%u", &sin6->sin6_addr,
-				sin6->sin6_scope_id);
-	else
-		snprintf(buf, len, "%pI6", &sin6->sin6_addr);
-}
-
-static void nsm_display_address(const struct sockaddr *sap,
-				char *buf, const size_t len)
-{
-	switch (sap->sa_family) {
-	case AF_INET:
-		nsm_display_ipv4_address(sap, buf, len);
-		break;
-	case AF_INET6:
-		nsm_display_ipv6_address(sap, buf, len);
-		break;
-	default:
-		snprintf(buf, len, "unsupported address family");
-		break;
-	}
-}
-
 static struct rpc_clnt *nsm_create(void)
 {
 	struct sockaddr_in sin = {
@@ -307,8 +270,11 @@ static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
 	memcpy(nsm_addr(new), sap, salen);
 	new->sm_addrlen = salen;
 	nsm_init_private(new);
-	nsm_display_address((const struct sockaddr *)&new->sm_addr,
-				new->sm_addrbuf, sizeof(new->sm_addrbuf));
+
+	if (rpc_ntop(nsm_addr(new), new->sm_addrbuf,
+					sizeof(new->sm_addrbuf)) == 0)
+		(void)snprintf(new->sm_addrbuf, sizeof(new->sm_addrbuf),
+				"unsupported address family");
 	memcpy(new->sm_name, hostname, hostname_len);
 	new->sm_name[hostname_len] = '\0';
 
#17 lore raw

PATCH 10/10 NFSD: Support IPv6 addresses in write_failover_ip()

In reply to a message not in the archive

In write_failover_ip(), replace the sscanf() with a call to the common sunrpc.ko presentation address parser.

---

 fs/nfsd/nfsctl.c |   21 +++++++--------------
 1 files changed, 7 insertions(+), 14 deletions(-)
fs/nfsd/nfsctl.c +7 -14
index 6d08475..7e906c5 100644
@@ -37,6 +37,7 @@
 #include <linux/nfsd/xdr.h>
 #include <linux/nfsd/syscall.h>
 #include <linux/lockd/lockd.h>
+#include <linux/sunrpc/clnt.h>
 
 #include <asm/uaccess.h>
 #include <net/ipv6.h>
@@ -490,22 +491,18 @@ static ssize_t write_getfd(struct file *file, char *buf, size_t size)
  *
  * Input:
  *			buf:	'\n'-terminated C string containing a
- *				presentation format IPv4 address
+ *				presentation format IP address
  *			size:	length of C string in @buf
  * Output:
  *	On success:	returns zero if all specified locks were released;
  *			returns one if one or more locks were not released
  *	On error:	return code is negative errno value
- *
- * Note: Only AF_INET client addresses are passed in
  */
 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
 {
-	struct sockaddr_in sin = {
-		.sin_family	= AF_INET,
-	};
-	int b1, b2, b3, b4;
-	char c;
+	struct sockaddr_storage address;
+	struct sockaddr *sap = (struct sockaddr *)&address;
+	size_t salen = sizeof(address);
 	char *fo_path;
 
 	/* sanity check */
@@ -519,14 +516,10 @@ static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
 	if (qword_get(&buf, fo_path, size) < 0)
 		return -EINVAL;
 
-	/* get ipv4 address */
-	if (sscanf(fo_path, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
-		return -EINVAL;
-	if (b1 > 255 || b2 > 255 || b3 > 255 || b4 > 255)
+	if (rpc_pton(fo_path, size, sap, salen) == 0)
 		return -EINVAL;
-	sin.sin_addr.s_addr = htonl((b1 << 24) | (b2 << 16) | (b3 << 8) | b4);
 
-	return nlmsvc_unlock_all_by_ip((struct sockaddr *)&sin);
+	return nlmsvc_unlock_all_by_ip(sap);
 }
 
 /**