d542391
commit 0e94118815e8ff9c1142117764ee3e6cddba0395
d542391
Author: Chuck Lever <chuck.lever@oracle.com>
d542391
Date:   Fri Oct 1 15:04:20 2010 -0400
d542391
d542391
    libnfs.a: Allow multiple RPC listeners to share listener port number
d542391
    
d542391
    Normally, when "-p" is not specified on the mountd command line, the
d542391
    TI-RPC library chooses random port numbers for each listener.  If a
d542391
    port number _is_ specified on the command line, all the listeners
d542391
    will get the same port number, so SO_REUSEADDR needs to be set on
d542391
    each socket.
d542391
    
d542391
    Thus we can't let TI-RPC create the listener sockets for us in this
d542391
    case; we must create them ourselves and then set SO_REUSEADDR (and
d542391
    other socket options) by hand.
d542391
    
d542391
    Different versions of the same RPC program have to share the same
d542391
    listener and SVCXPRT, so we have to cache xprts we create, and re-use
d542391
    them when additional requests for registration come from the
d542391
    application.
d542391
    
d542391
    Though it doesn't look like it, this fix was "copied" from the legacy
d542391
    rpc_init() function.  It's more complicated for TI-RPC, of course,
d542391
    since you can have an arbitrary number of listeners, not just two
d542391
    (one for AF_INET UDP and one for AF_INET TCP).
d542391
    
d542391
    Fix for:
d542391
    
d542391
      https://bugzilla.linux-nfs.org/show_bug.cgi?id=190
d542391
    
d542391
    There have been no reports of problems with specifying statd's
d542391
    listener port, but I expect this is a problem for statd too.
d542391
    
d542391
    Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
d542391
d542391
diff --git a/support/nfs/svc_create.c b/support/nfs/svc_create.c
d542391
index 59ba505..fdc4846 100644
d542391
--- a/support/nfs/svc_create.c
d542391
+++ b/support/nfs/svc_create.c
d542391
@@ -27,6 +27,7 @@
d542391
 #include <memory.h>
d542391
 #include <signal.h>
d542391
 #include <unistd.h>
d542391
+#include <errno.h>
d542391
 #include <netdb.h>
d542391
 
d542391
 #include <netinet/in.h>
d542391
@@ -41,11 +42,68 @@
d542391
 #include "tcpwrapper.h"
d542391
 #endif
d542391
 
d542391
+#include "sockaddr.h"
d542391
 #include "rpcmisc.h"
d542391
 #include "xlog.h"
d542391
 
d542391
 #ifdef HAVE_LIBTIRPC
d542391
 
d542391
+#define SVC_CREATE_XPRT_CACHE_SIZE	(8)
d542391
+static SVCXPRT *svc_create_xprt_cache[SVC_CREATE_XPRT_CACHE_SIZE] = { NULL, };
d542391
+
d542391
+/*
d542391
+ * Cache an SVC xprt, in case there are more programs or versions to
d542391
+ * register against it.
d542391
+ */
d542391
+static void
d542391
+svc_create_cache_xprt(SVCXPRT *xprt)
d542391
+{
d542391
+	unsigned int i;
d542391
+
d542391
+	/* Check if we've already got this one... */
d542391
+	for (i = 0; i < SVC_CREATE_XPRT_CACHE_SIZE; i++)
d542391
+		if (svc_create_xprt_cache[i] == xprt)
d542391
+			return;
d542391
+
d542391
+	/* No, we don't.  Cache it. */
d542391
+	for (i = 0; i < SVC_CREATE_XPRT_CACHE_SIZE; i++)
d542391
+		if (svc_create_xprt_cache[i] == NULL) {
d542391
+			svc_create_xprt_cache[i] = xprt;
d542391
+			return;
d542391
+		}
d542391
+
d542391
+	xlog(L_ERROR, "%s: Failed to cache an xprt", __func__);
d542391
+}
d542391
+
d542391
+/*
d542391
+ * Find a previously cached SVC xprt structure with the given bind address
d542391
+ * and transport semantics.
d542391
+ *
d542391
+ * Returns pointer to a SVC xprt.
d542391
+ *
d542391
+ * If no matching SVC XPRT can be found, NULL is returned.
d542391
+ */
d542391
+static SVCXPRT *
d542391
+svc_create_find_xprt(const struct sockaddr *bindaddr, const struct netconfig *nconf)
d542391
+{
d542391
+	unsigned int i;
d542391
+
d542391
+	for (i = 0; i < SVC_CREATE_XPRT_CACHE_SIZE; i++) {
d542391
+		SVCXPRT *xprt = svc_create_xprt_cache[i];
d542391
+		struct sockaddr *sap;
d542391
+
d542391
+		if (xprt == NULL)
d542391
+			continue;
d542391
+		if (strcmp(nconf->nc_netid, xprt->xp_netid) != 0)
d542391
+			continue;
d542391
+		sap = (struct sockaddr *)xprt->xp_ltaddr.buf;
d542391
+		if (!nfs_compare_sockaddr(bindaddr, sap))
d542391
+			continue;
d542391
+		return xprt;
d542391
+	}
d542391
+	return NULL;
d542391
+}
d542391
+
d542391
 /*
d542391
  * Set up an appropriate bind address, given @port and @nconf.
d542391
  *
d542391
@@ -98,17 +156,112 @@ svc_create_bindaddr(struct netconfig *nconf, const uint16_t port)
d542391
 	return ai;
d542391
 }
d542391
 
d542391
+/*
d542391
+ * Create a listener socket on a specific bindaddr, and set
d542391
+ * special socket options to allow it to share the same port
d542391
+ * as other listeners.
d542391
+ *
d542391
+ * Returns an open, bound, and possibly listening network
d542391
+ * socket on success.
d542391
+ *
d542391
+ * Otherwise returns -1 if some error occurs.
d542391
+ */
d542391
+static int
d542391
+svc_create_sock(const struct sockaddr *sap, socklen_t salen,
d542391
+		struct netconfig *nconf)
d542391
+{
d542391
+	int fd, type, protocol;
d542391
+	int one = 1;
d542391
+
d542391
+	switch(nconf->nc_semantics) {
d542391
+	case NC_TPI_CLTS:
d542391
+		type = SOCK_DGRAM;
d542391
+		break;
d542391
+	case NC_TPI_COTS_ORD:
d542391
+		type = SOCK_STREAM;
d542391
+		break;
d542391
+	default:
d542391
+		xlog(D_GENERAL, "%s: Unrecognized bind address semantics: %u",
d542391
+			__func__, nconf->nc_semantics);
d542391
+		return -1;
d542391
+	}
d542391
+
d542391
+	if (strcmp(nconf->nc_proto, NC_UDP) == 0)
d542391
+		protocol = (int)IPPROTO_UDP;
d542391
+	else if (strcmp(nconf->nc_proto, NC_TCP) == 0)
d542391
+		protocol = (int)IPPROTO_TCP;
d542391
+	else {
d542391
+		xlog(D_GENERAL, "%s: Unrecognized bind address protocol: %s",
d542391
+			__func__, nconf->nc_proto);
d542391
+		return -1;
d542391
+	}
d542391
+
d542391
+	fd = socket((int)sap->sa_family, type, protocol);
d542391
+	if (fd == -1) {
d542391
+		xlog(L_ERROR, "Could not make a socket: (%d) %m",
d542391
+			errno);
d542391
+		return -1;
d542391
+	}
d542391
+
d542391
+#ifdef IPV6_SUPPORTED
d542391
+	if (sap->sa_family == AF_INET6) {
d542391
+		if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
d542391
+				&one, sizeof(one)) == -1) {
d542391
+			xlog(L_ERROR, "Failed to set IPV6_V6ONLY: (%d) %m",
d542391
+				errno);
d542391
+			(void)close(fd);
d542391
+			return -1;
d542391
+		}
d542391
+	}
d542391
+#endif	/* IPV6_SUPPORTED */
d542391
+
d542391
+	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
d542391
+		       &one, sizeof(one)) == -1) {
d542391
+		xlog(L_ERROR, "Failed to set SO_REUSEADDR: (%d) %m",
d542391
+			errno);
d542391
+		(void)close(fd);
d542391
+		return -1;
d542391
+	}
d542391
+
d542391
+	if (bind(fd, sap, salen) == -1) {
d542391
+		xlog(L_ERROR, "Could not bind socket: (%d) %m",
d542391
+			errno);
d542391
+		(void)close(fd);
d542391
+		return -1;
d542391
+	}
d542391
+
d542391
+	if (nconf->nc_semantics == NC_TPI_COTS_ORD)
d542391
+		if (listen(fd, SOMAXCONN) == -1) {
d542391
+			xlog(L_ERROR, "Could not listen on socket: (%d) %m",
d542391
+				errno);
d542391
+			(void)close(fd);
d542391
+			return -1;
d542391
+		}
d542391
+
d542391
+	return fd;
d542391
+}
d542391
+
d542391
+/*
d542391
+ * The simple case is allowing the TI-RPC library to create a
d542391
+ * transport itself, given just the bind address and transport
d542391
+ * semantics.
d542391
+ *
d542391
+ * The port is chosen at random by the library; we don't know
d542391
+ * what it is.  So the new xprt cannot be cached here.
d542391
+ *
d542391
+ * Returns the count of started listeners (one or zero).
d542391
+ */
d542391
 static unsigned int
d542391
-svc_create_nconf(const char *name, const rpcprog_t program,
d542391
+svc_create_nconf_rand_port(const char *name, const rpcprog_t program,
d542391
 		const rpcvers_t version,
d542391
 		void (*dispatch)(struct svc_req *, SVCXPRT *),
d542391
-		const uint16_t port, struct netconfig *nconf)
d542391
+		struct netconfig *nconf)
d542391
 {
d542391
 	struct t_bind bindaddr;
d542391
 	struct addrinfo *ai;
d542391
 	SVCXPRT	*xprt;
d542391
 
d542391
-	ai = svc_create_bindaddr(nconf, port);
d542391
+	ai = svc_create_bindaddr(nconf, 0);
d542391
 	if (ai == NULL)
d542391
 		return 0;
d542391
 
d542391
@@ -119,7 +272,7 @@ svc_create_nconf(const char *name, const rpcprog_t program,
d542391
 	freeaddrinfo(ai);
d542391
 	if (xprt == NULL) {
d542391
 		xlog(D_GENERAL, "Failed to create listener xprt "
d542391
-				"(%s, %u, %s)", name, version, nconf->nc_netid);
d542391
+			"(%s, %u, %s)", name, version, nconf->nc_netid);
d542391
 		return 0;
d542391
 	}
d542391
 
d542391
@@ -133,6 +286,81 @@ svc_create_nconf(const char *name, const rpcprog_t program,
d542391
 	return 1;
d542391
 }
d542391
 
d542391
+/*
d542391
+ * If a port is specified on the command line, that port value will be
d542391
+ * the same for all listeners created here.  Create each listener socket
d542391
+ * in advance and set SO_REUSEADDR, rather than allowing the RPC library
d542391
+ * to create the listeners for us on a randomly chosen port (RPC_ANYFD).
d542391
+ *
d542391
+ * Also, to support multiple RPC versions on the same listener, register
d542391
+ * any new versions on the same transport that is already handling other
d542391
+ * versions on the same bindaddr and transport.  To accomplish this,
d542391
+ * cache previously created xprts on a list, and check that list before
d542391
+ * creating a new socket for this [program, version].
d542391
+ *
d542391
+ * Returns the count of started listeners (one or zero).
d542391
+ */
d542391
+static unsigned int
d542391
+svc_create_nconf_fixed_port(const char *name, const rpcprog_t program,
d542391
+		const rpcvers_t version,
d542391
+		void (*dispatch)(struct svc_req *, SVCXPRT *),
d542391
+		const uint16_t port, struct netconfig *nconf)
d542391
+{
d542391
+	struct addrinfo *ai;
d542391
+	SVCXPRT	*xprt;
d542391
+
d542391
+	ai = svc_create_bindaddr(nconf, port);
d542391
+	if (ai == NULL)
d542391
+		return 0;
d542391
+
d542391
+	xprt = svc_create_find_xprt(ai->ai_addr, nconf);
d542391
+	if (xprt == NULL) {
d542391
+		int fd;
d542391
+
d542391
+		fd = svc_create_sock(ai->ai_addr, ai->ai_addrlen, nconf);
d542391
+		if (fd == -1)
d542391
+			goto out_free;
d542391
+
d542391
+		xprt = svc_tli_create(fd, nconf, NULL, 0, 0);
d542391
+		if (xprt == NULL) {
d542391
+			xlog(D_GENERAL, "Failed to create listener xprt "
d542391
+				"(%s, %u, %s)", name, version, nconf->nc_netid);
d542391
+			(void)close(fd);
d542391
+			goto out_free;
d542391
+		}
d542391
+	}
d542391
+
d542391
+	if (!svc_reg(xprt, program, version, dispatch, nconf)) {
d542391
+		/* svc_reg(3) destroys @xprt in this case */
d542391
+		xlog(D_GENERAL, "Failed to register (%s, %u, %s)",
d542391
+				name, version, nconf->nc_netid);
d542391
+		goto out_free;
d542391
+	}
d542391
+
d542391
+	svc_create_cache_xprt(xprt);
d542391
+
d542391
+	freeaddrinfo(ai);
d542391
+	return 1;
d542391
+
d542391
+out_free:
d542391
+	freeaddrinfo(ai);
d542391
+	return 0;
d542391
+}
d542391
+
d542391
+static unsigned int
d542391
+svc_create_nconf(const char *name, const rpcprog_t program,
d542391
+		const rpcvers_t version,
d542391
+		void (*dispatch)(struct svc_req *, SVCXPRT *),
d542391
+		const uint16_t port, struct netconfig *nconf)
d542391
+{
d542391
+	if (port != 0)
d542391
+		return svc_create_nconf_fixed_port(name, program,
d542391
+			version, dispatch, port, nconf);
d542391
+
d542391
+	return svc_create_nconf_rand_port(name, program,
d542391
+			version, dispatch, nconf);
d542391
+}
d542391
+
d542391
 /**
d542391
  * nfs_svc_create - start up RPC svc listeners
d542391
  * @name: C string containing name of new service