SUNRPC: add xprt switch API for printing formatted remote peer addresses

Add a new method to the transport switch API to provide a way to convert
the opaque contents of xprt->addr to a human-readable string.

Test plan:
Compile kernel with CONFIG_NFS enabled.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
This commit is contained in:
Chuck Lever 2006-08-22 20:06:18 -04:00 committed by Trond Myklebust
parent 39d7bbcb5b
commit edb267a688
2 changed files with 82 additions and 8 deletions

View file

@ -51,6 +51,14 @@ struct rpc_timeout {
unsigned char to_exponential; unsigned char to_exponential;
}; };
enum rpc_display_format_t {
RPC_DISPLAY_ADDR = 0,
RPC_DISPLAY_PORT,
RPC_DISPLAY_PROTO,
RPC_DISPLAY_ALL,
RPC_DISPLAY_MAX,
};
struct rpc_task; struct rpc_task;
struct rpc_xprt; struct rpc_xprt;
struct seq_file; struct seq_file;
@ -103,6 +111,7 @@ struct rpc_rqst {
struct rpc_xprt_ops { struct rpc_xprt_ops {
void (*set_buffer_size)(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize); void (*set_buffer_size)(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize);
char * (*print_addr)(struct rpc_xprt *xprt, enum rpc_display_format_t format);
int (*reserve_xprt)(struct rpc_task *task); int (*reserve_xprt)(struct rpc_task *task);
void (*release_xprt)(struct rpc_xprt *xprt, struct rpc_task *task); void (*release_xprt)(struct rpc_xprt *xprt, struct rpc_task *task);
void (*rpcbind)(struct rpc_task *task); void (*rpcbind)(struct rpc_task *task);
@ -207,6 +216,8 @@ struct rpc_xprt {
void (*old_data_ready)(struct sock *, int); void (*old_data_ready)(struct sock *, int);
void (*old_state_change)(struct sock *); void (*old_state_change)(struct sock *);
void (*old_write_space)(struct sock *); void (*old_write_space)(struct sock *);
char * address_strings[RPC_DISPLAY_MAX];
}; };
#define XPRT_LAST_FRAG (1 << 0) #define XPRT_LAST_FRAG (1 << 0)

View file

@ -125,6 +125,47 @@ static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
} }
#endif #endif
static void xs_format_peer_addresses(struct rpc_xprt *xprt)
{
struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
char *buf;
buf = kzalloc(20, GFP_KERNEL);
if (buf) {
snprintf(buf, 20, "%u.%u.%u.%u",
NIPQUAD(addr->sin_addr.s_addr));
}
xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
buf = kzalloc(8, GFP_KERNEL);
if (buf) {
snprintf(buf, 8, "%u",
ntohs(addr->sin_port));
}
xprt->address_strings[RPC_DISPLAY_PORT] = buf;
if (xprt->prot == IPPROTO_UDP)
xprt->address_strings[RPC_DISPLAY_PROTO] = "udp";
else
xprt->address_strings[RPC_DISPLAY_PROTO] = "tcp";
buf = kzalloc(48, GFP_KERNEL);
if (buf) {
snprintf(buf, 48, "addr=%u.%u.%u.%u port=%u proto=%s",
NIPQUAD(addr->sin_addr.s_addr),
ntohs(addr->sin_port),
xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
}
xprt->address_strings[RPC_DISPLAY_ALL] = buf;
}
static void xs_free_peer_addresses(struct rpc_xprt *xprt)
{
kfree(xprt->address_strings[RPC_DISPLAY_ADDR]);
kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
kfree(xprt->address_strings[RPC_DISPLAY_ALL]);
}
#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len) static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len)
@ -490,6 +531,7 @@ static void xs_destroy(struct rpc_xprt *xprt)
xprt_disconnect(xprt); xprt_disconnect(xprt);
xs_close(xprt); xs_close(xprt);
xs_free_peer_addresses(xprt);
kfree(xprt->slot); kfree(xprt->slot);
} }
@ -964,6 +1006,19 @@ static unsigned short xs_get_random_port(void)
return rand + xprt_min_resvport; return rand + xprt_min_resvport;
} }
/**
* xs_print_peer_address - format an IPv4 address for printing
* @xprt: generic transport
* @format: flags field indicating which parts of the address to render
*/
static char *xs_print_peer_address(struct rpc_xprt *xprt, enum rpc_display_format_t format)
{
if (xprt->address_strings[format] != NULL)
return xprt->address_strings[format];
else
return "unprintable";
}
/** /**
* xs_set_port - reset the port number in the remote endpoint address * xs_set_port - reset the port number in the remote endpoint address
* @xprt: generic transport * @xprt: generic transport
@ -1019,8 +1074,6 @@ static void xs_udp_connect_worker(void *args)
if (xprt->shutdown || !xprt_bound(xprt)) if (xprt->shutdown || !xprt_bound(xprt))
goto out; goto out;
dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt);
/* Start by resetting any existing state */ /* Start by resetting any existing state */
xs_close(xprt); xs_close(xprt);
@ -1034,6 +1087,9 @@ static void xs_udp_connect_worker(void *args)
goto out; goto out;
} }
dprintk("RPC: worker connecting xprt %p to address: %s\n",
xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
if (!xprt->inet) { if (!xprt->inet) {
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
@ -1102,8 +1158,6 @@ static void xs_tcp_connect_worker(void *args)
if (xprt->shutdown || !xprt_bound(xprt)) if (xprt->shutdown || !xprt_bound(xprt))
goto out; goto out;
dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt);
if (!xprt->sock) { if (!xprt->sock) {
/* start from scratch */ /* start from scratch */
if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
@ -1119,6 +1173,9 @@ static void xs_tcp_connect_worker(void *args)
/* "close" the socket, preserving the local port */ /* "close" the socket, preserving the local port */
xs_tcp_reuse_connection(xprt); xs_tcp_reuse_connection(xprt);
dprintk("RPC: worker connecting xprt %p to address: %s\n",
xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
if (!xprt->inet) { if (!xprt->inet) {
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
@ -1260,6 +1317,7 @@ static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
static struct rpc_xprt_ops xs_udp_ops = { static struct rpc_xprt_ops xs_udp_ops = {
.set_buffer_size = xs_udp_set_buffer_size, .set_buffer_size = xs_udp_set_buffer_size,
.print_addr = xs_print_peer_address,
.reserve_xprt = xprt_reserve_xprt_cong, .reserve_xprt = xprt_reserve_xprt_cong,
.release_xprt = xprt_release_xprt_cong, .release_xprt = xprt_release_xprt_cong,
.rpcbind = rpc_getport, .rpcbind = rpc_getport,
@ -1277,6 +1335,7 @@ static struct rpc_xprt_ops xs_udp_ops = {
}; };
static struct rpc_xprt_ops xs_tcp_ops = { static struct rpc_xprt_ops xs_tcp_ops = {
.print_addr = xs_print_peer_address,
.reserve_xprt = xprt_reserve_xprt, .reserve_xprt = xprt_reserve_xprt,
.release_xprt = xs_tcp_release_xprt, .release_xprt = xs_tcp_release_xprt,
.rpcbind = rpc_getport, .rpcbind = rpc_getport,
@ -1301,8 +1360,6 @@ int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
{ {
size_t slot_table_size; size_t slot_table_size;
dprintk("RPC: setting up udp-ipv4 transport...\n");
xprt->max_reqs = xprt_udp_slot_table_entries; xprt->max_reqs = xprt_udp_slot_table_entries;
slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
xprt->slot = kzalloc(slot_table_size, GFP_KERNEL); xprt->slot = kzalloc(slot_table_size, GFP_KERNEL);
@ -1332,6 +1389,10 @@ int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
else else
xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
xs_format_peer_addresses(xprt);
dprintk("RPC: set up transport to address %s\n",
xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
return 0; return 0;
} }
@ -1345,8 +1406,6 @@ int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
{ {
size_t slot_table_size; size_t slot_table_size;
dprintk("RPC: setting up tcp-ipv4 transport...\n");
xprt->max_reqs = xprt_tcp_slot_table_entries; xprt->max_reqs = xprt_tcp_slot_table_entries;
slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
xprt->slot = kzalloc(slot_table_size, GFP_KERNEL); xprt->slot = kzalloc(slot_table_size, GFP_KERNEL);
@ -1375,5 +1434,9 @@ int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
else else
xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
xs_format_peer_addresses(xprt);
dprintk("RPC: set up transport to address %s\n",
xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
return 0; return 0;
} }