Commit c8541ecd authored by Chuck Lever's avatar Chuck Lever Committed by Trond Myklebust

SUNRPC: Make the transport-specific setup routine allocate rpc_xprt

Change the location where the rpc_xprt structure is allocated so each
transport implementation can allocate a private area from the same
chunk of memory.

Note also that xprt->ops->destroy, rather than xprt_destroy, is now
responsible for freeing rpc_xprt when the transport is destroyed.

Test plan:
Connectathon.
Signed-off-by: default avatarChuck Lever <chuck.lever@oracle.com>
Signed-off-by: default avatarTrond Myklebust <Trond.Myklebust@netapp.com>
parent e744cf2e
...@@ -265,8 +265,8 @@ void xprt_disconnect(struct rpc_xprt *xprt); ...@@ -265,8 +265,8 @@ void xprt_disconnect(struct rpc_xprt *xprt);
/* /*
* Socket transport setup operations * Socket transport setup operations
*/ */
int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to); struct rpc_xprt * xs_setup_udp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to);
int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to); struct rpc_xprt * xs_setup_tcp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to);
/* /*
* Reserved bit positions in xprt->state * Reserved bit positions in xprt->state
......
...@@ -891,39 +891,25 @@ void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long i ...@@ -891,39 +891,25 @@ void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long i
*/ */
struct rpc_xprt *xprt_create_transport(int proto, struct sockaddr *ap, size_t size, struct rpc_timeout *to) struct rpc_xprt *xprt_create_transport(int proto, struct sockaddr *ap, size_t size, struct rpc_timeout *to)
{ {
int result;
struct rpc_xprt *xprt; struct rpc_xprt *xprt;
struct rpc_rqst *req; struct rpc_rqst *req;
if ((xprt = kzalloc(sizeof(struct rpc_xprt), GFP_KERNEL)) == NULL) {
dprintk("RPC: xprt_create_transport: no memory\n");
return ERR_PTR(-ENOMEM);
}
if (size <= sizeof(xprt->addr)) {
memcpy(&xprt->addr, ap, size);
xprt->addrlen = size;
} else {
kfree(xprt);
dprintk("RPC: xprt_create_transport: address too large\n");
return ERR_PTR(-EBADF);
}
switch (proto) { switch (proto) {
case IPPROTO_UDP: case IPPROTO_UDP:
result = xs_setup_udp(xprt, to); xprt = xs_setup_udp(ap, size, to);
break; break;
case IPPROTO_TCP: case IPPROTO_TCP:
result = xs_setup_tcp(xprt, to); xprt = xs_setup_tcp(ap, size, to);
break; break;
default: default:
printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n", printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
proto); proto);
return ERR_PTR(-EIO); return ERR_PTR(-EIO);
} }
if (result) { if (IS_ERR(xprt)) {
kfree(xprt); dprintk("RPC: xprt_create_transport: failed, %ld\n",
dprintk("RPC: xprt_create_transport: failed, %d\n", result); -PTR_ERR(xprt));
return ERR_PTR(result); return xprt;
} }
kref_init(&xprt->kref); kref_init(&xprt->kref);
...@@ -969,8 +955,11 @@ static void xprt_destroy(struct kref *kref) ...@@ -969,8 +955,11 @@ static void xprt_destroy(struct kref *kref)
dprintk("RPC: destroying transport %p\n", xprt); dprintk("RPC: destroying transport %p\n", xprt);
xprt->shutdown = 1; xprt->shutdown = 1;
del_timer_sync(&xprt->timer); del_timer_sync(&xprt->timer);
/*
* Tear down transport state and free the rpc_xprt
*/
xprt->ops->destroy(xprt); xprt->ops->destroy(xprt);
kfree(xprt);
} }
/** /**
......
...@@ -518,6 +518,7 @@ static void xs_destroy(struct rpc_xprt *xprt) ...@@ -518,6 +518,7 @@ static void xs_destroy(struct rpc_xprt *xprt)
xs_close(xprt); xs_close(xprt);
xs_free_peer_addresses(xprt); xs_free_peer_addresses(xprt);
kfree(xprt->slot); kfree(xprt->slot);
kfree(xprt);
} }
static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
...@@ -1339,26 +1340,53 @@ static struct rpc_xprt_ops xs_tcp_ops = { ...@@ -1339,26 +1340,53 @@ static struct rpc_xprt_ops xs_tcp_ops = {
.print_stats = xs_tcp_print_stats, .print_stats = xs_tcp_print_stats,
}; };
static struct rpc_xprt *xs_setup_xprt(struct sockaddr *addr, size_t addrlen, unsigned int slot_table_size)
{
struct rpc_xprt *xprt;
if (addrlen > sizeof(xprt->addr)) {
dprintk("RPC: xs_setup_xprt: address too large\n");
return ERR_PTR(-EBADF);
}
xprt = kzalloc(sizeof(struct rpc_xprt), GFP_KERNEL);
if (xprt == NULL) {
dprintk("RPC: xs_setup_xprt: couldn't allocate rpc_xprt\n");
return ERR_PTR(-ENOMEM);
}
xprt->max_reqs = slot_table_size;
xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
if (xprt->slot == NULL) {
kfree(xprt);
dprintk("RPC: xs_setup_xprt: couldn't allocate slot table\n");
return ERR_PTR(-ENOMEM);
}
memcpy(&xprt->addr, addr, addrlen);
xprt->addrlen = addrlen;
xprt->port = xs_get_random_port();
return xprt;
}
/** /**
* xs_setup_udp - Set up transport to use a UDP socket * xs_setup_udp - Set up transport to use a UDP socket
* @xprt: transport to set up * @addr: address of remote server
* @addrlen: length of address in bytes
* @to: timeout parameters * @to: timeout parameters
* *
*/ */
int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to) struct rpc_xprt *xs_setup_udp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
{ {
size_t slot_table_size; struct rpc_xprt *xprt;
struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
xprt->max_reqs = xprt_udp_slot_table_entries; xprt = xs_setup_xprt(addr, addrlen, xprt_udp_slot_table_entries);
slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); if (IS_ERR(xprt))
xprt->slot = kzalloc(slot_table_size, GFP_KERNEL); return xprt;
if (xprt->slot == NULL)
return -ENOMEM;
if (ntohs(addr->sin_port) != 0) if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
xprt_set_bound(xprt); xprt_set_bound(xprt);
xprt->port = xs_get_random_port();
xprt->prot = IPPROTO_UDP; xprt->prot = IPPROTO_UDP;
xprt->tsh_size = 0; xprt->tsh_size = 0;
...@@ -1382,29 +1410,26 @@ int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to) ...@@ -1382,29 +1410,26 @@ int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
dprintk("RPC: set up transport to address %s\n", dprintk("RPC: set up transport to address %s\n",
xs_print_peer_address(xprt, RPC_DISPLAY_ALL)); xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
return 0; return xprt;
} }
/** /**
* xs_setup_tcp - Set up transport to use a TCP socket * xs_setup_tcp - Set up transport to use a TCP socket
* @xprt: transport to set up * @addr: address of remote server
* @addrlen: length of address in bytes
* @to: timeout parameters * @to: timeout parameters
* *
*/ */
int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to) struct rpc_xprt *xs_setup_tcp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
{ {
size_t slot_table_size; struct rpc_xprt *xprt;
struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
xprt->max_reqs = xprt_tcp_slot_table_entries; xprt = xs_setup_xprt(addr, addrlen, xprt_tcp_slot_table_entries);
slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); if (IS_ERR(xprt))
xprt->slot = kzalloc(slot_table_size, GFP_KERNEL); return xprt;
if (xprt->slot == NULL)
return -ENOMEM;
if (ntohs(addr->sin_port) != 0) if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
xprt_set_bound(xprt); xprt_set_bound(xprt);
xprt->port = xs_get_random_port();
xprt->prot = IPPROTO_TCP; xprt->prot = IPPROTO_TCP;
xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
...@@ -1427,5 +1452,5 @@ int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to) ...@@ -1427,5 +1452,5 @@ int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
dprintk("RPC: set up transport to address %s\n", dprintk("RPC: set up transport to address %s\n",
xs_print_peer_address(xprt, RPC_DISPLAY_ALL)); xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
return 0; return xprt;
} }
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