Commit beb59b68 authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by J. Bruce Fields

sunrpc: Merge the xs_bind code

There's the only difference betseen the xs_bind4 and the
xs_bind6 - the size of sockaddr structure they use.

Fortunatelly its size can be indirectly get from the transport.

Change since v1:
* use sockaddr_storage instead of sockaddr
* use rpc_set_port instead of manual port assigning
Signed-off-by: default avatarPavel Emelyanov <xemul@openvz.org>
Reviewed-by: default avatarChuck Lever <chuck.lever@oracle.com>
[bfields@redhat.com: fix address family initialization]
Signed-off-by: default avatarJ. Bruce Fields <bfields@redhat.com>
parent 573018c0
...@@ -1534,23 +1534,18 @@ static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned shor ...@@ -1534,23 +1534,18 @@ static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned shor
return xprt_max_resvport; return xprt_max_resvport;
return --port; return --port;
} }
static int xs_bind(struct sock_xprt *transport, struct socket *sock)
static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
{ {
struct sockaddr_in myaddr = { struct sockaddr_storage myaddr;
.sin_family = AF_INET,
};
struct sockaddr_in *sa;
int err, nloop = 0; int err, nloop = 0;
unsigned short port = xs_get_srcport(transport); unsigned short port = xs_get_srcport(transport);
unsigned short last; unsigned short last;
sa = (struct sockaddr_in *)&transport->srcaddr; memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
myaddr.sin_addr = sa->sin_addr;
do { do {
myaddr.sin_port = htons(port); rpc_set_port((struct sockaddr *)&myaddr, port);
err = kernel_bind(sock, (struct sockaddr *) &myaddr, err = kernel_bind(sock, (struct sockaddr *)&myaddr,
sizeof(myaddr)); transport->xprt.addrlen);
if (port == 0) if (port == 0)
break; break;
if (err == 0) { if (err == 0) {
...@@ -1562,44 +1557,19 @@ static int xs_bind4(struct sock_xprt *transport, struct socket *sock) ...@@ -1562,44 +1557,19 @@ static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
if (port > last) if (port > last)
nloop++; nloop++;
} while (err == -EADDRINUSE && nloop != 2); } while (err == -EADDRINUSE && nloop != 2);
dprintk("RPC: %s %pI4:%u: %s (%d)\n",
__func__, &myaddr.sin_addr,
port, err ? "failed" : "ok", err);
return err;
}
static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
{
struct sockaddr_in6 myaddr = {
.sin6_family = AF_INET6,
};
struct sockaddr_in6 *sa;
int err, nloop = 0;
unsigned short port = xs_get_srcport(transport);
unsigned short last;
sa = (struct sockaddr_in6 *)&transport->srcaddr; if (myaddr.ss_family == PF_INET)
myaddr.sin6_addr = sa->sin6_addr; dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
do { &((struct sockaddr_in *)&myaddr)->sin_addr,
myaddr.sin6_port = htons(port); port, err ? "failed" : "ok", err);
err = kernel_bind(sock, (struct sockaddr *) &myaddr, else
sizeof(myaddr)); dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
if (port == 0) &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
break; port, err ? "failed" : "ok", err);
if (err == 0) {
transport->srcport = port;
break;
}
last = port;
port = xs_next_srcport(transport, port);
if (port > last)
nloop++;
} while (err == -EADDRINUSE && nloop != 2);
dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
return err; return err;
} }
#ifdef CONFIG_DEBUG_LOCK_ALLOC #ifdef CONFIG_DEBUG_LOCK_ALLOC
static struct lock_class_key xs_key[2]; static struct lock_class_key xs_key[2];
static struct lock_class_key xs_slock_key[2]; static struct lock_class_key xs_slock_key[2];
...@@ -1643,9 +1613,10 @@ static struct socket *xs_create_sock4(struct rpc_xprt *xprt, ...@@ -1643,9 +1613,10 @@ static struct socket *xs_create_sock4(struct rpc_xprt *xprt,
protocol, -err); protocol, -err);
goto out; goto out;
} }
transport->srcaddr.ss_family = AF_INET;
xs_reclassify_socket4(sock); xs_reclassify_socket4(sock);
if (xs_bind4(transport, sock)) { if (xs_bind(transport, sock)) {
sock_release(sock); sock_release(sock);
goto out; goto out;
} }
...@@ -1667,9 +1638,10 @@ static struct socket *xs_create_sock6(struct rpc_xprt *xprt, ...@@ -1667,9 +1638,10 @@ static struct socket *xs_create_sock6(struct rpc_xprt *xprt,
protocol, -err); protocol, -err);
goto out; goto out;
} }
transport->srcaddr.ss_family = AF_INET6;
xs_reclassify_socket6(sock); xs_reclassify_socket6(sock);
if (xs_bind6(transport, sock)) { if (xs_bind(transport, sock)) {
sock_release(sock); sock_release(sock);
goto out; goto out;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment