1 commit 0e94118815e8ff9c1142117764ee3e6cddba0395
2 Author: Chuck Lever <chuck.lever@oracle.com>
3 Date: Fri Oct 1 15:04:20 2010 -0400
5 libnfs.a: Allow multiple RPC listeners to share listener port number
7 Normally, when "-p" is not specified on the mountd command line, the
8 TI-RPC library chooses random port numbers for each listener. If a
9 port number _is_ specified on the command line, all the listeners
10 will get the same port number, so SO_REUSEADDR needs to be set on
13 Thus we can't let TI-RPC create the listener sockets for us in this
14 case; we must create them ourselves and then set SO_REUSEADDR (and
15 other socket options) by hand.
17 Different versions of the same RPC program have to share the same
18 listener and SVCXPRT, so we have to cache xprts we create, and re-use
19 them when additional requests for registration come from the
22 Though it doesn't look like it, this fix was "copied" from the legacy
23 rpc_init() function. It's more complicated for TI-RPC, of course,
24 since you can have an arbitrary number of listeners, not just two
25 (one for AF_INET UDP and one for AF_INET TCP).
29 https://bugzilla.linux-nfs.org/show_bug.cgi?id=190
31 There have been no reports of problems with specifying statd's
32 listener port, but I expect this is a problem for statd too.
34 Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
36 diff --git a/support/nfs/svc_create.c b/support/nfs/svc_create.c
37 index 59ba505..fdc4846 100644
38 --- a/support/nfs/svc_create.c
39 +++ b/support/nfs/svc_create.c
47 #include <netinet/in.h>
49 #include "tcpwrapper.h"
52 +#include "sockaddr.h"
58 +#define SVC_CREATE_XPRT_CACHE_SIZE (8)
59 +static SVCXPRT *svc_create_xprt_cache[SVC_CREATE_XPRT_CACHE_SIZE] = { NULL, };
62 + * Cache an SVC xprt, in case there are more programs or versions to
63 + * register against it.
66 +svc_create_cache_xprt(SVCXPRT *xprt)
70 + /* Check if we've already got this one... */
71 + for (i = 0; i < SVC_CREATE_XPRT_CACHE_SIZE; i++)
72 + if (svc_create_xprt_cache[i] == xprt)
75 + /* No, we don't. Cache it. */
76 + for (i = 0; i < SVC_CREATE_XPRT_CACHE_SIZE; i++)
77 + if (svc_create_xprt_cache[i] == NULL) {
78 + svc_create_xprt_cache[i] = xprt;
82 + xlog(L_ERROR, "%s: Failed to cache an xprt", __func__);
86 + * Find a previously cached SVC xprt structure with the given bind address
87 + * and transport semantics.
89 + * Returns pointer to a SVC xprt.
91 + * If no matching SVC XPRT can be found, NULL is returned.
94 +svc_create_find_xprt(const struct sockaddr *bindaddr, const struct netconfig *nconf)
98 + for (i = 0; i < SVC_CREATE_XPRT_CACHE_SIZE; i++) {
99 + SVCXPRT *xprt = svc_create_xprt_cache[i];
100 + struct sockaddr *sap;
104 + if (strcmp(nconf->nc_netid, xprt->xp_netid) != 0)
106 + sap = (struct sockaddr *)xprt->xp_ltaddr.buf;
107 + if (!nfs_compare_sockaddr(bindaddr, sap))
115 * Set up an appropriate bind address, given @port and @nconf.
117 @@ -98,17 +156,112 @@ svc_create_bindaddr(struct netconfig *nconf, const uint16_t port)
122 + * Create a listener socket on a specific bindaddr, and set
123 + * special socket options to allow it to share the same port
124 + * as other listeners.
126 + * Returns an open, bound, and possibly listening network
127 + * socket on success.
129 + * Otherwise returns -1 if some error occurs.
132 +svc_create_sock(const struct sockaddr *sap, socklen_t salen,
133 + struct netconfig *nconf)
135 + int fd, type, protocol;
138 + switch(nconf->nc_semantics) {
142 + case NC_TPI_COTS_ORD:
143 + type = SOCK_STREAM;
146 + xlog(D_GENERAL, "%s: Unrecognized bind address semantics: %u",
147 + __func__, nconf->nc_semantics);
151 + if (strcmp(nconf->nc_proto, NC_UDP) == 0)
152 + protocol = (int)IPPROTO_UDP;
153 + else if (strcmp(nconf->nc_proto, NC_TCP) == 0)
154 + protocol = (int)IPPROTO_TCP;
156 + xlog(D_GENERAL, "%s: Unrecognized bind address protocol: %s",
157 + __func__, nconf->nc_proto);
161 + fd = socket((int)sap->sa_family, type, protocol);
163 + xlog(L_ERROR, "Could not make a socket: (%d) %m",
168 +#ifdef IPV6_SUPPORTED
169 + if (sap->sa_family == AF_INET6) {
170 + if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
171 + &one, sizeof(one)) == -1) {
172 + xlog(L_ERROR, "Failed to set IPV6_V6ONLY: (%d) %m",
178 +#endif /* IPV6_SUPPORTED */
180 + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
181 + &one, sizeof(one)) == -1) {
182 + xlog(L_ERROR, "Failed to set SO_REUSEADDR: (%d) %m",
188 + if (bind(fd, sap, salen) == -1) {
189 + xlog(L_ERROR, "Could not bind socket: (%d) %m",
195 + if (nconf->nc_semantics == NC_TPI_COTS_ORD)
196 + if (listen(fd, SOMAXCONN) == -1) {
197 + xlog(L_ERROR, "Could not listen on socket: (%d) %m",
207 + * The simple case is allowing the TI-RPC library to create a
208 + * transport itself, given just the bind address and transport
211 + * The port is chosen at random by the library; we don't know
212 + * what it is. So the new xprt cannot be cached here.
214 + * Returns the count of started listeners (one or zero).
217 -svc_create_nconf(const char *name, const rpcprog_t program,
218 +svc_create_nconf_rand_port(const char *name, const rpcprog_t program,
219 const rpcvers_t version,
220 void (*dispatch)(struct svc_req *, SVCXPRT *),
221 - const uint16_t port, struct netconfig *nconf)
222 + struct netconfig *nconf)
224 struct t_bind bindaddr;
228 - ai = svc_create_bindaddr(nconf, port);
229 + ai = svc_create_bindaddr(nconf, 0);
233 @@ -119,7 +272,7 @@ svc_create_nconf(const char *name, const rpcprog_t program,
236 xlog(D_GENERAL, "Failed to create listener xprt "
237 - "(%s, %u, %s)", name, version, nconf->nc_netid);
238 + "(%s, %u, %s)", name, version, nconf->nc_netid);
242 @@ -133,6 +286,81 @@ svc_create_nconf(const char *name, const rpcprog_t program,
247 + * If a port is specified on the command line, that port value will be
248 + * the same for all listeners created here. Create each listener socket
249 + * in advance and set SO_REUSEADDR, rather than allowing the RPC library
250 + * to create the listeners for us on a randomly chosen port (RPC_ANYFD).
252 + * Also, to support multiple RPC versions on the same listener, register
253 + * any new versions on the same transport that is already handling other
254 + * versions on the same bindaddr and transport. To accomplish this,
255 + * cache previously created xprts on a list, and check that list before
256 + * creating a new socket for this [program, version].
258 + * Returns the count of started listeners (one or zero).
261 +svc_create_nconf_fixed_port(const char *name, const rpcprog_t program,
262 + const rpcvers_t version,
263 + void (*dispatch)(struct svc_req *, SVCXPRT *),
264 + const uint16_t port, struct netconfig *nconf)
266 + struct addrinfo *ai;
269 + ai = svc_create_bindaddr(nconf, port);
273 + xprt = svc_create_find_xprt(ai->ai_addr, nconf);
274 + if (xprt == NULL) {
277 + fd = svc_create_sock(ai->ai_addr, ai->ai_addrlen, nconf);
281 + xprt = svc_tli_create(fd, nconf, NULL, 0, 0);
282 + if (xprt == NULL) {
283 + xlog(D_GENERAL, "Failed to create listener xprt "
284 + "(%s, %u, %s)", name, version, nconf->nc_netid);
290 + if (!svc_reg(xprt, program, version, dispatch, nconf)) {
291 + /* svc_reg(3) destroys @xprt in this case */
292 + xlog(D_GENERAL, "Failed to register (%s, %u, %s)",
293 + name, version, nconf->nc_netid);
297 + svc_create_cache_xprt(xprt);
308 +svc_create_nconf(const char *name, const rpcprog_t program,
309 + const rpcvers_t version,
310 + void (*dispatch)(struct svc_req *, SVCXPRT *),
311 + const uint16_t port, struct netconfig *nconf)
314 + return svc_create_nconf_fixed_port(name, program,
315 + version, dispatch, port, nconf);
317 + return svc_create_nconf_rand_port(name, program,
318 + version, dispatch, nconf);
322 * nfs_svc_create - start up RPC svc listeners
323 * @name: C string containing name of new service