Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/xprtsock.c |
| 3 | * |
| 4 | * Client-side transport implementation for sockets. |
| 5 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 6 | * TCP callback races fixes (C) 1998 Red Hat |
| 7 | * TCP send fixes (C) 1998 Red Hat |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 8 | * TCP NFS related read + write fixes |
| 9 | * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> |
| 10 | * |
| 11 | * Rewrite of larges part of the code in order to stabilize TCP stuff. |
| 12 | * Fix behaviour when socket buffer is full. |
| 13 | * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 14 | * |
| 15 | * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 16 | * |
| 17 | * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. |
| 18 | * <gilles.quillard@bull.net> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/types.h> |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 22 | #include <linux/string.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 23 | #include <linux/slab.h> |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 25 | #include <linux/capability.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 26 | #include <linux/pagemap.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/socket.h> |
| 29 | #include <linux/in.h> |
| 30 | #include <linux/net.h> |
| 31 | #include <linux/mm.h> |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 32 | #include <linux/un.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 33 | #include <linux/udp.h> |
| 34 | #include <linux/tcp.h> |
| 35 | #include <linux/sunrpc/clnt.h> |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 36 | #include <linux/sunrpc/sched.h> |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 37 | #include <linux/sunrpc/svcsock.h> |
\"Talpey, Thomas\ | 49c36fc | 2007-09-10 13:47:31 -0400 | [diff] [blame] | 38 | #include <linux/sunrpc/xprtsock.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 39 | #include <linux/file.h> |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 40 | #ifdef CONFIG_SUNRPC_BACKCHANNEL |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 41 | #include <linux/sunrpc/bc_xprt.h> |
| 42 | #endif |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 43 | |
| 44 | #include <net/sock.h> |
| 45 | #include <net/checksum.h> |
| 46 | #include <net/udp.h> |
| 47 | #include <net/tcp.h> |
| 48 | |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 49 | #include "sunrpc.h" |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 50 | |
| 51 | static void xs_close(struct rpc_xprt *xprt); |
| 52 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 53 | /* |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 54 | * xprtsock tunables |
| 55 | */ |
Trond Myklebust | 09acfea | 2012-03-11 15:22:54 -0400 | [diff] [blame] | 56 | static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
| 57 | static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; |
| 58 | static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 59 | |
Trond Myklebust | 09acfea | 2012-03-11 15:22:54 -0400 | [diff] [blame] | 60 | static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; |
| 61 | static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 62 | |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 63 | #define XS_TCP_LINGER_TO (15U * HZ) |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 64 | static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 65 | |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 66 | /* |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 67 | * We can register our own files under /proc/sys/sunrpc by |
| 68 | * calling register_sysctl_table() again. The files in that |
| 69 | * directory become the union of all files registered there. |
| 70 | * |
| 71 | * We simply need to make sure that we don't collide with |
| 72 | * someone else's file names! |
| 73 | */ |
| 74 | |
| 75 | #ifdef RPC_DEBUG |
| 76 | |
| 77 | static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; |
| 78 | static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 79 | static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 80 | static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; |
| 81 | static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; |
| 82 | |
| 83 | static struct ctl_table_header *sunrpc_table_header; |
| 84 | |
| 85 | /* |
| 86 | * FIXME: changing the UDP slot table size should also resize the UDP |
| 87 | * socket buffers for existing UDP transports |
| 88 | */ |
| 89 | static ctl_table xs_tunables_table[] = { |
| 90 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 91 | .procname = "udp_slot_table_entries", |
| 92 | .data = &xprt_udp_slot_table_entries, |
| 93 | .maxlen = sizeof(unsigned int), |
| 94 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 95 | .proc_handler = proc_dointvec_minmax, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 96 | .extra1 = &min_slot_table_size, |
| 97 | .extra2 = &max_slot_table_size |
| 98 | }, |
| 99 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 100 | .procname = "tcp_slot_table_entries", |
| 101 | .data = &xprt_tcp_slot_table_entries, |
| 102 | .maxlen = sizeof(unsigned int), |
| 103 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 104 | .proc_handler = proc_dointvec_minmax, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 105 | .extra1 = &min_slot_table_size, |
| 106 | .extra2 = &max_slot_table_size |
| 107 | }, |
| 108 | { |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 109 | .procname = "tcp_max_slot_table_entries", |
| 110 | .data = &xprt_max_tcp_slot_table_entries, |
| 111 | .maxlen = sizeof(unsigned int), |
| 112 | .mode = 0644, |
| 113 | .proc_handler = proc_dointvec_minmax, |
| 114 | .extra1 = &min_slot_table_size, |
| 115 | .extra2 = &max_tcp_slot_table_limit |
| 116 | }, |
| 117 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 118 | .procname = "min_resvport", |
| 119 | .data = &xprt_min_resvport, |
| 120 | .maxlen = sizeof(unsigned int), |
| 121 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 122 | .proc_handler = proc_dointvec_minmax, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 123 | .extra1 = &xprt_min_resvport_limit, |
| 124 | .extra2 = &xprt_max_resvport_limit |
| 125 | }, |
| 126 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 127 | .procname = "max_resvport", |
| 128 | .data = &xprt_max_resvport, |
| 129 | .maxlen = sizeof(unsigned int), |
| 130 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 131 | .proc_handler = proc_dointvec_minmax, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 132 | .extra1 = &xprt_min_resvport_limit, |
| 133 | .extra2 = &xprt_max_resvport_limit |
| 134 | }, |
| 135 | { |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 136 | .procname = "tcp_fin_timeout", |
| 137 | .data = &xs_tcp_fin_timeout, |
| 138 | .maxlen = sizeof(xs_tcp_fin_timeout), |
| 139 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 140 | .proc_handler = proc_dointvec_jiffies, |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 141 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 142 | { }, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | static ctl_table sunrpc_table[] = { |
| 146 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 147 | .procname = "sunrpc", |
| 148 | .mode = 0555, |
| 149 | .child = xs_tunables_table |
| 150 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 151 | { }, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | #endif |
| 155 | |
| 156 | /* |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 157 | * Wait duration for a reply from the RPC portmapper. |
| 158 | */ |
| 159 | #define XS_BIND_TO (60U * HZ) |
| 160 | |
| 161 | /* |
| 162 | * Delay if a UDP socket connect error occurs. This is most likely some |
| 163 | * kind of resource problem on the local host. |
| 164 | */ |
| 165 | #define XS_UDP_REEST_TO (2U * HZ) |
| 166 | |
| 167 | /* |
| 168 | * The reestablish timeout allows clients to delay for a bit before attempting |
| 169 | * to reconnect to a server that just dropped our connection. |
| 170 | * |
| 171 | * We implement an exponential backoff when trying to reestablish a TCP |
| 172 | * transport connection with the server. Some servers like to drop a TCP |
| 173 | * connection when they are overworked, so we start with a short timeout and |
| 174 | * increase over time if the server is down or not responding. |
| 175 | */ |
| 176 | #define XS_TCP_INIT_REEST_TO (3U * HZ) |
| 177 | #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) |
| 178 | |
| 179 | /* |
| 180 | * TCP idle timeout; client drops the transport socket if it is idle |
| 181 | * for this long. Note that we also timeout UDP sockets to prevent |
| 182 | * holding port numbers when there is no RPC traffic. |
| 183 | */ |
| 184 | #define XS_IDLE_DISC_TO (5U * 60 * HZ) |
| 185 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 186 | #ifdef RPC_DEBUG |
| 187 | # undef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 188 | # define RPCDBG_FACILITY RPCDBG_TRANS |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 189 | #endif |
| 190 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 191 | #ifdef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 192 | static void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 193 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 194 | u8 *buf = (u8 *) packet; |
| 195 | int j; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 196 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 197 | dprintk("RPC: %s\n", msg); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 198 | for (j = 0; j < count && j < 128; j += 4) { |
| 199 | if (!(j & 31)) { |
| 200 | if (j) |
| 201 | dprintk("\n"); |
| 202 | dprintk("0x%04x ", j); |
| 203 | } |
| 204 | dprintk("%02x%02x%02x%02x ", |
| 205 | buf[j], buf[j+1], buf[j+2], buf[j+3]); |
| 206 | } |
| 207 | dprintk("\n"); |
| 208 | } |
| 209 | #else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 210 | static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 211 | { |
| 212 | /* NOP */ |
| 213 | } |
| 214 | #endif |
| 215 | |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 216 | struct sock_xprt { |
| 217 | struct rpc_xprt xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * Network layer |
| 221 | */ |
| 222 | struct socket * sock; |
| 223 | struct sock * inet; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * State of TCP reply receive |
| 227 | */ |
| 228 | __be32 tcp_fraghdr, |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 229 | tcp_xid, |
| 230 | tcp_calldir; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 231 | |
| 232 | u32 tcp_offset, |
| 233 | tcp_reclen; |
| 234 | |
| 235 | unsigned long tcp_copied, |
| 236 | tcp_flags; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Connection of transports |
| 240 | */ |
Trond Myklebust | 34161db | 2006-12-07 15:48:15 -0500 | [diff] [blame] | 241 | struct delayed_work connect_worker; |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 242 | struct sockaddr_storage srcaddr; |
| 243 | unsigned short srcport; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * UDP socket buffer size parameters |
| 247 | */ |
| 248 | size_t rcvsize, |
| 249 | sndsize; |
Chuck Lever | 314dfd7 | 2006-12-05 16:35:34 -0500 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * Saved socket callback addresses |
| 253 | */ |
| 254 | void (*old_data_ready)(struct sock *, int); |
| 255 | void (*old_state_change)(struct sock *); |
| 256 | void (*old_write_space)(struct sock *); |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 257 | }; |
| 258 | |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 259 | /* |
| 260 | * TCP receive state flags |
| 261 | */ |
| 262 | #define TCP_RCV_LAST_FRAG (1UL << 0) |
| 263 | #define TCP_RCV_COPY_FRAGHDR (1UL << 1) |
| 264 | #define TCP_RCV_COPY_XID (1UL << 2) |
| 265 | #define TCP_RCV_COPY_DATA (1UL << 3) |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 266 | #define TCP_RCV_READ_CALLDIR (1UL << 4) |
| 267 | #define TCP_RCV_COPY_CALLDIR (1UL << 5) |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * TCP RPC flags |
| 271 | */ |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 272 | #define TCP_RPC_REPLY (1UL << 6) |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 273 | |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 274 | static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 275 | { |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 276 | return (struct sockaddr *) &xprt->addr; |
| 277 | } |
| 278 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 279 | static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) |
| 280 | { |
| 281 | return (struct sockaddr_un *) &xprt->addr; |
| 282 | } |
| 283 | |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 284 | static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) |
| 285 | { |
| 286 | return (struct sockaddr_in *) &xprt->addr; |
| 287 | } |
| 288 | |
| 289 | static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) |
| 290 | { |
| 291 | return (struct sockaddr_in6 *) &xprt->addr; |
| 292 | } |
| 293 | |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 294 | static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 295 | { |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 296 | struct sockaddr *sap = xs_addr(xprt); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 297 | struct sockaddr_in6 *sin6; |
| 298 | struct sockaddr_in *sin; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 299 | struct sockaddr_un *sun; |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 300 | char buf[128]; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 301 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 302 | switch (sap->sa_family) { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 303 | case AF_LOCAL: |
| 304 | sun = xs_addr_un(xprt); |
| 305 | strlcpy(buf, sun->sun_path, sizeof(buf)); |
| 306 | xprt->address_strings[RPC_DISPLAY_ADDR] = |
| 307 | kstrdup(buf, GFP_KERNEL); |
| 308 | break; |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 309 | case AF_INET: |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 310 | (void)rpc_ntop(sap, buf, sizeof(buf)); |
| 311 | xprt->address_strings[RPC_DISPLAY_ADDR] = |
| 312 | kstrdup(buf, GFP_KERNEL); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 313 | sin = xs_addr_in(xprt); |
Joe Perches | fc0b5791 | 2010-03-08 12:15:28 -0800 | [diff] [blame] | 314 | snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 315 | break; |
| 316 | case AF_INET6: |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 317 | (void)rpc_ntop(sap, buf, sizeof(buf)); |
| 318 | xprt->address_strings[RPC_DISPLAY_ADDR] = |
| 319 | kstrdup(buf, GFP_KERNEL); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 320 | sin6 = xs_addr_in6(xprt); |
Joe Perches | fc0b5791 | 2010-03-08 12:15:28 -0800 | [diff] [blame] | 321 | snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 322 | break; |
| 323 | default: |
| 324 | BUG(); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 325 | } |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 326 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 327 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 330 | static void xs_format_common_peer_ports(struct rpc_xprt *xprt) |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 331 | { |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 332 | struct sockaddr *sap = xs_addr(xprt); |
| 333 | char buf[128]; |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 334 | |
Joe Perches | 81160e66 | 2010-03-08 12:15:59 -0800 | [diff] [blame] | 335 | snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 336 | xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 337 | |
Joe Perches | 81160e66 | 2010-03-08 12:15:59 -0800 | [diff] [blame] | 338 | snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 339 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); |
| 340 | } |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 341 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 342 | static void xs_format_peer_addresses(struct rpc_xprt *xprt, |
| 343 | const char *protocol, |
| 344 | const char *netid) |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 345 | { |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 346 | xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 347 | xprt->address_strings[RPC_DISPLAY_NETID] = netid; |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 348 | xs_format_common_peer_addresses(xprt); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 349 | xs_format_common_peer_ports(xprt); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 352 | static void xs_update_peer_port(struct rpc_xprt *xprt) |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 353 | { |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 354 | kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); |
| 355 | kfree(xprt->address_strings[RPC_DISPLAY_PORT]); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 356 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 357 | xs_format_common_peer_ports(xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static void xs_free_peer_addresses(struct rpc_xprt *xprt) |
| 361 | { |
Chuck Lever | 33e01dc | 2008-01-14 12:32:20 -0500 | [diff] [blame] | 362 | unsigned int i; |
| 363 | |
| 364 | for (i = 0; i < RPC_DISPLAY_MAX; i++) |
| 365 | switch (i) { |
| 366 | case RPC_DISPLAY_PROTO: |
| 367 | case RPC_DISPLAY_NETID: |
| 368 | continue; |
| 369 | default: |
| 370 | kfree(xprt->address_strings[i]); |
| 371 | } |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 374 | #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) |
| 375 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 376 | static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 377 | { |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 378 | struct msghdr msg = { |
| 379 | .msg_name = addr, |
| 380 | .msg_namelen = addrlen, |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 381 | .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), |
| 382 | }; |
| 383 | struct kvec iov = { |
| 384 | .iov_base = vec->iov_base + base, |
| 385 | .iov_len = vec->iov_len - base, |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 386 | }; |
| 387 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 388 | if (iov.iov_len != 0) |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 389 | return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); |
| 390 | return kernel_sendmsg(sock, &msg, NULL, 0, 0); |
| 391 | } |
| 392 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 393 | static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 394 | { |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 395 | struct page **ppage; |
| 396 | unsigned int remainder; |
| 397 | int err, sent = 0; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 398 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 399 | remainder = xdr->page_len - base; |
| 400 | base += xdr->page_base; |
| 401 | ppage = xdr->pages + (base >> PAGE_SHIFT); |
| 402 | base &= ~PAGE_MASK; |
| 403 | for(;;) { |
| 404 | unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); |
| 405 | int flags = XS_SENDMSG_FLAGS; |
| 406 | |
| 407 | remainder -= len; |
| 408 | if (remainder != 0 || more) |
| 409 | flags |= MSG_MORE; |
| 410 | err = sock->ops->sendpage(sock, *ppage, base, len, flags); |
| 411 | if (remainder == 0 || err != len) |
| 412 | break; |
| 413 | sent += err; |
| 414 | ppage++; |
| 415 | base = 0; |
| 416 | } |
| 417 | if (sent == 0) |
| 418 | return err; |
| 419 | if (err > 0) |
| 420 | sent += err; |
| 421 | return sent; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 424 | /** |
| 425 | * xs_sendpages - write pages directly to a socket |
| 426 | * @sock: socket to send on |
| 427 | * @addr: UDP only -- address of destination |
| 428 | * @addrlen: UDP only -- length of destination address |
| 429 | * @xdr: buffer containing this request |
| 430 | * @base: starting position in the buffer |
| 431 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 432 | */ |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 433 | static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 434 | { |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 435 | unsigned int remainder = xdr->len - base; |
| 436 | int err, sent = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 437 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 438 | if (unlikely(!sock)) |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 439 | return -ENOTSOCK; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 440 | |
| 441 | clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 442 | if (base != 0) { |
| 443 | addr = NULL; |
| 444 | addrlen = 0; |
| 445 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 446 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 447 | if (base < xdr->head[0].iov_len || addr != NULL) { |
| 448 | unsigned int len = xdr->head[0].iov_len - base; |
| 449 | remainder -= len; |
| 450 | err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); |
| 451 | if (remainder == 0 || err != len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 452 | goto out; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 453 | sent += err; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 454 | base = 0; |
| 455 | } else |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 456 | base -= xdr->head[0].iov_len; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 457 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 458 | if (base < xdr->page_len) { |
| 459 | unsigned int len = xdr->page_len - base; |
| 460 | remainder -= len; |
| 461 | err = xs_send_pagedata(sock, xdr, base, remainder != 0); |
| 462 | if (remainder == 0 || err != len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 463 | goto out; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 464 | sent += err; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 465 | base = 0; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 466 | } else |
| 467 | base -= xdr->page_len; |
| 468 | |
| 469 | if (base >= xdr->tail[0].iov_len) |
| 470 | return sent; |
| 471 | err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 472 | out: |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 473 | if (sent == 0) |
| 474 | return err; |
| 475 | if (err > 0) |
| 476 | sent += err; |
| 477 | return sent; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 478 | } |
| 479 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 480 | static void xs_nospace_callback(struct rpc_task *task) |
| 481 | { |
| 482 | struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); |
| 483 | |
| 484 | transport->inet->sk_write_pending--; |
| 485 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
| 486 | } |
| 487 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 488 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 489 | * xs_nospace - place task on wait queue if transmit was incomplete |
| 490 | * @task: task to put to sleep |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 491 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 492 | */ |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 493 | static int xs_nospace(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 494 | { |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 495 | struct rpc_rqst *req = task->tk_rqstp; |
| 496 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 497 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Trond Myklebust | 24ca9a8 | 2011-11-22 14:44:28 +0200 | [diff] [blame] | 498 | int ret = -EAGAIN; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 499 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 500 | dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 501 | task->tk_pid, req->rq_slen - req->rq_bytes_sent, |
| 502 | req->rq_slen); |
| 503 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 504 | /* Protect against races with write_space */ |
| 505 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 506 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 507 | /* Don't race with disconnect */ |
| 508 | if (xprt_connected(xprt)) { |
| 509 | if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { |
| 510 | /* |
| 511 | * Notify TCP that we're limited by the application |
| 512 | * window size |
| 513 | */ |
| 514 | set_bit(SOCK_NOSPACE, &transport->sock->flags); |
| 515 | transport->inet->sk_write_pending++; |
| 516 | /* ...and wait for more buffer space */ |
| 517 | xprt_wait_for_buffer_space(task, xs_nospace_callback); |
| 518 | } |
| 519 | } else { |
| 520 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 521 | ret = -ENOTCONN; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 522 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 523 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 524 | spin_unlock_bh(&xprt->transport_lock); |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 525 | return ret; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 526 | } |
| 527 | |
Chuck Lever | 61677ee | 2011-05-09 15:22:34 -0400 | [diff] [blame] | 528 | /* |
| 529 | * Construct a stream transport record marker in @buf. |
| 530 | */ |
| 531 | static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) |
| 532 | { |
| 533 | u32 reclen = buf->len - sizeof(rpc_fraghdr); |
| 534 | rpc_fraghdr *base = buf->head[0].iov_base; |
| 535 | *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); |
| 536 | } |
| 537 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 538 | /** |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 539 | * xs_local_send_request - write an RPC request to an AF_LOCAL socket |
| 540 | * @task: RPC task that manages the state of an RPC request |
| 541 | * |
| 542 | * Return values: |
| 543 | * 0: The request has been sent |
| 544 | * EAGAIN: The socket was blocked, please call again later to |
| 545 | * complete the request |
| 546 | * ENOTCONN: Caller needs to invoke connect logic then call again |
| 547 | * other: Some other error occured, the request was not sent |
| 548 | */ |
| 549 | static int xs_local_send_request(struct rpc_task *task) |
| 550 | { |
| 551 | struct rpc_rqst *req = task->tk_rqstp; |
| 552 | struct rpc_xprt *xprt = req->rq_xprt; |
| 553 | struct sock_xprt *transport = |
| 554 | container_of(xprt, struct sock_xprt, xprt); |
| 555 | struct xdr_buf *xdr = &req->rq_snd_buf; |
| 556 | int status; |
| 557 | |
| 558 | xs_encode_stream_record_marker(&req->rq_snd_buf); |
| 559 | |
| 560 | xs_pktdump("packet data:", |
| 561 | req->rq_svec->iov_base, req->rq_svec->iov_len); |
| 562 | |
| 563 | status = xs_sendpages(transport->sock, NULL, 0, |
| 564 | xdr, req->rq_bytes_sent); |
| 565 | dprintk("RPC: %s(%u) = %d\n", |
| 566 | __func__, xdr->len - req->rq_bytes_sent, status); |
| 567 | if (likely(status >= 0)) { |
| 568 | req->rq_bytes_sent += status; |
| 569 | req->rq_xmit_bytes_sent += status; |
| 570 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { |
| 571 | req->rq_bytes_sent = 0; |
| 572 | return 0; |
| 573 | } |
| 574 | status = -EAGAIN; |
| 575 | } |
| 576 | |
| 577 | switch (status) { |
| 578 | case -EAGAIN: |
| 579 | status = xs_nospace(task); |
| 580 | break; |
| 581 | default: |
| 582 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 583 | -status); |
| 584 | case -EPIPE: |
| 585 | xs_close(xprt); |
| 586 | status = -ENOTCONN; |
| 587 | } |
| 588 | |
| 589 | return status; |
| 590 | } |
| 591 | |
| 592 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 593 | * xs_udp_send_request - write an RPC request to a UDP socket |
| 594 | * @task: address of RPC task that manages the state of an RPC request |
| 595 | * |
| 596 | * Return values: |
| 597 | * 0: The request has been sent |
| 598 | * EAGAIN: The socket was blocked, please call again later to |
| 599 | * complete the request |
| 600 | * ENOTCONN: Caller needs to invoke connect logic then call again |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 601 | * other: Some other error occurred, the request was not sent |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 602 | */ |
| 603 | static int xs_udp_send_request(struct rpc_task *task) |
| 604 | { |
| 605 | struct rpc_rqst *req = task->tk_rqstp; |
| 606 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 607 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 608 | struct xdr_buf *xdr = &req->rq_snd_buf; |
| 609 | int status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 610 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 611 | xs_pktdump("packet data:", |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 612 | req->rq_svec->iov_base, |
| 613 | req->rq_svec->iov_len); |
| 614 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 615 | if (!xprt_bound(xprt)) |
| 616 | return -ENOTCONN; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 617 | status = xs_sendpages(transport->sock, |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 618 | xs_addr(xprt), |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 619 | xprt->addrlen, xdr, |
| 620 | req->rq_bytes_sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 621 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 622 | dprintk("RPC: xs_udp_send_request(%u) = %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 623 | xdr->len - req->rq_bytes_sent, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 624 | |
Trond Myklebust | 2199700 | 2007-10-01 11:43:37 -0400 | [diff] [blame] | 625 | if (status >= 0) { |
Trond Myklebust | d60dbb2 | 2010-05-13 12:51:49 -0400 | [diff] [blame] | 626 | req->rq_xmit_bytes_sent += status; |
Trond Myklebust | 2199700 | 2007-10-01 11:43:37 -0400 | [diff] [blame] | 627 | if (status >= req->rq_slen) |
| 628 | return 0; |
| 629 | /* Still some bytes left; set up for a retry later. */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 630 | status = -EAGAIN; |
Trond Myklebust | 2199700 | 2007-10-01 11:43:37 -0400 | [diff] [blame] | 631 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 632 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 633 | switch (status) { |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 634 | case -ENOTSOCK: |
| 635 | status = -ENOTCONN; |
| 636 | /* Should we call xs_close() here? */ |
| 637 | break; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 638 | case -EAGAIN: |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 639 | status = xs_nospace(task); |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 640 | break; |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 641 | default: |
| 642 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 643 | -status); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 644 | case -ENETUNREACH: |
| 645 | case -EPIPE: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 646 | case -ECONNREFUSED: |
| 647 | /* When the server has died, an ICMP port unreachable message |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 648 | * prompts ECONNREFUSED. */ |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 649 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 650 | } |
Bian Naimeng | 5fe46e9 | 2010-03-08 14:49:01 +0800 | [diff] [blame] | 651 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 652 | return status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 653 | } |
| 654 | |
Trond Myklebust | e06799f | 2007-11-05 15:44:12 -0500 | [diff] [blame] | 655 | /** |
| 656 | * xs_tcp_shutdown - gracefully shut down a TCP socket |
| 657 | * @xprt: transport |
| 658 | * |
| 659 | * Initiates a graceful shutdown of the TCP socket by calling the |
| 660 | * equivalent of shutdown(SHUT_WR); |
| 661 | */ |
| 662 | static void xs_tcp_shutdown(struct rpc_xprt *xprt) |
| 663 | { |
| 664 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 665 | struct socket *sock = transport->sock; |
| 666 | |
| 667 | if (sock != NULL) |
| 668 | kernel_sock_shutdown(sock, SHUT_WR); |
| 669 | } |
| 670 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 671 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 672 | * xs_tcp_send_request - write an RPC request to a TCP socket |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 673 | * @task: address of RPC task that manages the state of an RPC request |
| 674 | * |
| 675 | * Return values: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 676 | * 0: The request has been sent |
| 677 | * EAGAIN: The socket was blocked, please call again later to |
| 678 | * complete the request |
| 679 | * ENOTCONN: Caller needs to invoke connect logic then call again |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 680 | * other: Some other error occurred, the request was not sent |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 681 | * |
| 682 | * XXX: In the case of soft timeouts, should we eventually give up |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 683 | * if sendmsg is not able to make progress? |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 684 | */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 685 | static int xs_tcp_send_request(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 686 | { |
| 687 | struct rpc_rqst *req = task->tk_rqstp; |
| 688 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 689 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 690 | struct xdr_buf *xdr = &req->rq_snd_buf; |
Chuck Lever | b595bb1 | 2007-08-06 11:56:42 -0400 | [diff] [blame] | 691 | int status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 692 | |
Chuck Lever | 61677ee | 2011-05-09 15:22:34 -0400 | [diff] [blame] | 693 | xs_encode_stream_record_marker(&req->rq_snd_buf); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 694 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 695 | xs_pktdump("packet data:", |
| 696 | req->rq_svec->iov_base, |
| 697 | req->rq_svec->iov_len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 698 | |
| 699 | /* Continue transmitting the packet/record. We must be careful |
| 700 | * to cope with writespace callbacks arriving _after_ we have |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 701 | * called sendmsg(). */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 702 | while (1) { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 703 | status = xs_sendpages(transport->sock, |
| 704 | NULL, 0, xdr, req->rq_bytes_sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 705 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 706 | dprintk("RPC: xs_tcp_send_request(%u) = %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 707 | xdr->len - req->rq_bytes_sent, status); |
| 708 | |
| 709 | if (unlikely(status < 0)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 710 | break; |
| 711 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 712 | /* If we've sent the entire packet, immediately |
| 713 | * reset the count of bytes sent. */ |
| 714 | req->rq_bytes_sent += status; |
Trond Myklebust | d60dbb2 | 2010-05-13 12:51:49 -0400 | [diff] [blame] | 715 | req->rq_xmit_bytes_sent += status; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 716 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { |
| 717 | req->rq_bytes_sent = 0; |
| 718 | return 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 719 | } |
| 720 | |
Trond Myklebust | 06b4b68 | 2008-04-16 16:51:38 -0400 | [diff] [blame] | 721 | if (status != 0) |
| 722 | continue; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 723 | status = -EAGAIN; |
Trond Myklebust | 06b4b68 | 2008-04-16 16:51:38 -0400 | [diff] [blame] | 724 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 725 | } |
| 726 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 727 | switch (status) { |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 728 | case -ENOTSOCK: |
| 729 | status = -ENOTCONN; |
| 730 | /* Should we call xs_close() here? */ |
| 731 | break; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 732 | case -EAGAIN: |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 733 | status = xs_nospace(task); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 734 | break; |
| 735 | default: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 736 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 737 | -status); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 738 | case -ECONNRESET: |
Trond Myklebust | e06799f | 2007-11-05 15:44:12 -0500 | [diff] [blame] | 739 | xs_tcp_shutdown(xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 740 | case -ECONNREFUSED: |
| 741 | case -ENOTCONN: |
Trond Myklebust | b9d2bb2 | 2012-10-23 11:40:02 -0400 | [diff] [blame] | 742 | case -EPIPE: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 743 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 744 | } |
Bian Naimeng | 5fe46e9 | 2010-03-08 14:49:01 +0800 | [diff] [blame] | 745 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 746 | return status; |
| 747 | } |
| 748 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 749 | /** |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 750 | * xs_tcp_release_xprt - clean up after a tcp transmission |
| 751 | * @xprt: transport |
| 752 | * @task: rpc task |
| 753 | * |
| 754 | * This cleans up if an error causes us to abort the transmission of a request. |
| 755 | * In this case, the socket may need to be reset in order to avoid confusing |
| 756 | * the server. |
| 757 | */ |
| 758 | static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
| 759 | { |
| 760 | struct rpc_rqst *req; |
| 761 | |
| 762 | if (task != xprt->snd_task) |
| 763 | return; |
| 764 | if (task == NULL) |
| 765 | goto out_release; |
| 766 | req = task->tk_rqstp; |
Trond Myklebust | 43cedbf0e | 2011-07-17 16:01:03 -0400 | [diff] [blame] | 767 | if (req == NULL) |
| 768 | goto out_release; |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 769 | if (req->rq_bytes_sent == 0) |
| 770 | goto out_release; |
| 771 | if (req->rq_bytes_sent == req->rq_snd_buf.len) |
| 772 | goto out_release; |
| 773 | set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); |
| 774 | out_release: |
| 775 | xprt_release_xprt(xprt, task); |
| 776 | } |
| 777 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 778 | static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) |
| 779 | { |
| 780 | transport->old_data_ready = sk->sk_data_ready; |
| 781 | transport->old_state_change = sk->sk_state_change; |
| 782 | transport->old_write_space = sk->sk_write_space; |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) |
| 786 | { |
| 787 | sk->sk_data_ready = transport->old_data_ready; |
| 788 | sk->sk_state_change = transport->old_state_change; |
| 789 | sk->sk_write_space = transport->old_write_space; |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 790 | } |
| 791 | |
Chuck Lever | fe315e7 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 792 | static void xs_reset_transport(struct sock_xprt *transport) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 793 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 794 | struct socket *sock = transport->sock; |
| 795 | struct sock *sk = transport->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 796 | |
Chuck Lever | fe315e7 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 797 | if (sk == NULL) |
| 798 | return; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 799 | |
Trond Myklebust | 246408d | 2011-03-22 18:40:10 -0400 | [diff] [blame] | 800 | transport->srcport = 0; |
| 801 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 802 | write_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 803 | transport->inet = NULL; |
| 804 | transport->sock = NULL; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 805 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 806 | sk->sk_user_data = NULL; |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 807 | |
| 808 | xs_restore_old_callbacks(transport, sk); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 809 | write_unlock_bh(&sk->sk_callback_lock); |
| 810 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 811 | sk->sk_no_check = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 812 | |
| 813 | sock_release(sock); |
Chuck Lever | fe315e7 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /** |
| 817 | * xs_close - close a socket |
| 818 | * @xprt: transport |
| 819 | * |
| 820 | * This is used when all requests are complete; ie, no DRC state remains |
| 821 | * on the server we want to save. |
Trond Myklebust | f75e674 | 2009-04-21 17:18:20 -0400 | [diff] [blame] | 822 | * |
| 823 | * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with |
| 824 | * xs_reset_transport() zeroing the socket from underneath a writer. |
Chuck Lever | fe315e7 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 825 | */ |
| 826 | static void xs_close(struct rpc_xprt *xprt) |
| 827 | { |
| 828 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 829 | |
| 830 | dprintk("RPC: xs_close xprt %p\n", xprt); |
| 831 | |
| 832 | xs_reset_transport(transport); |
Neil Brown | 61d0a8e | 2009-09-23 14:36:37 -0400 | [diff] [blame] | 833 | xprt->reestablish_timeout = 0; |
Chuck Lever | fe315e7 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 834 | |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 835 | smp_mb__before_clear_bit(); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 836 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 837 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 838 | clear_bit(XPRT_CLOSING, &xprt->state); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 839 | smp_mb__after_clear_bit(); |
Trond Myklebust | 62da3b2 | 2007-11-06 18:44:20 -0500 | [diff] [blame] | 840 | xprt_disconnect_done(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 841 | } |
| 842 | |
Trond Myklebust | f75e674 | 2009-04-21 17:18:20 -0400 | [diff] [blame] | 843 | static void xs_tcp_close(struct rpc_xprt *xprt) |
| 844 | { |
| 845 | if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state)) |
| 846 | xs_close(xprt); |
| 847 | else |
| 848 | xs_tcp_shutdown(xprt); |
| 849 | } |
| 850 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 851 | /** |
| 852 | * xs_destroy - prepare to shutdown a transport |
| 853 | * @xprt: doomed transport |
| 854 | * |
| 855 | */ |
| 856 | static void xs_destroy(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 857 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 858 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 859 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 860 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 861 | |
Tejun Heo | afe2c51 | 2010-12-14 16:21:17 +0100 | [diff] [blame] | 862 | cancel_delayed_work_sync(&transport->connect_worker); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 863 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 864 | xs_close(xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 865 | xs_free_peer_addresses(xprt); |
Pavel Emelyanov | e204e62 | 2010-09-29 16:03:13 +0400 | [diff] [blame] | 866 | xprt_free(xprt); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 867 | module_put(THIS_MODULE); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 868 | } |
| 869 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 870 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 871 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 872 | return (struct rpc_xprt *) sk->sk_user_data; |
| 873 | } |
| 874 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 875 | static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb) |
| 876 | { |
| 877 | struct xdr_skb_reader desc = { |
| 878 | .skb = skb, |
| 879 | .offset = sizeof(rpc_fraghdr), |
| 880 | .count = skb->len - sizeof(rpc_fraghdr), |
| 881 | }; |
| 882 | |
| 883 | if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0) |
| 884 | return -1; |
| 885 | if (desc.count) |
| 886 | return -1; |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets |
| 892 | * @sk: socket with data to read |
| 893 | * @len: how much data to read |
| 894 | * |
| 895 | * Currently this assumes we can read the whole reply in a single gulp. |
| 896 | */ |
| 897 | static void xs_local_data_ready(struct sock *sk, int len) |
| 898 | { |
| 899 | struct rpc_task *task; |
| 900 | struct rpc_xprt *xprt; |
| 901 | struct rpc_rqst *rovr; |
| 902 | struct sk_buff *skb; |
| 903 | int err, repsize, copied; |
| 904 | u32 _xid; |
| 905 | __be32 *xp; |
| 906 | |
| 907 | read_lock_bh(&sk->sk_callback_lock); |
| 908 | dprintk("RPC: %s...\n", __func__); |
| 909 | xprt = xprt_from_sock(sk); |
| 910 | if (xprt == NULL) |
| 911 | goto out; |
| 912 | |
| 913 | skb = skb_recv_datagram(sk, 0, 1, &err); |
| 914 | if (skb == NULL) |
| 915 | goto out; |
| 916 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 917 | repsize = skb->len - sizeof(rpc_fraghdr); |
| 918 | if (repsize < 4) { |
| 919 | dprintk("RPC: impossible RPC reply size %d\n", repsize); |
| 920 | goto dropit; |
| 921 | } |
| 922 | |
| 923 | /* Copy the XID from the skb... */ |
| 924 | xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid); |
| 925 | if (xp == NULL) |
| 926 | goto dropit; |
| 927 | |
| 928 | /* Look up and lock the request corresponding to the given XID */ |
| 929 | spin_lock(&xprt->transport_lock); |
| 930 | rovr = xprt_lookup_rqst(xprt, *xp); |
| 931 | if (!rovr) |
| 932 | goto out_unlock; |
| 933 | task = rovr->rq_task; |
| 934 | |
| 935 | copied = rovr->rq_private_buf.buflen; |
| 936 | if (copied > repsize) |
| 937 | copied = repsize; |
| 938 | |
| 939 | if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) { |
| 940 | dprintk("RPC: sk_buff copy failed\n"); |
| 941 | goto out_unlock; |
| 942 | } |
| 943 | |
| 944 | xprt_complete_rqst(task, copied); |
| 945 | |
| 946 | out_unlock: |
| 947 | spin_unlock(&xprt->transport_lock); |
| 948 | dropit: |
| 949 | skb_free_datagram(sk, skb); |
| 950 | out: |
| 951 | read_unlock_bh(&sk->sk_callback_lock); |
| 952 | } |
| 953 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 954 | /** |
| 955 | * xs_udp_data_ready - "data ready" callback for UDP sockets |
| 956 | * @sk: socket with data to read |
| 957 | * @len: how much data to read |
| 958 | * |
| 959 | */ |
| 960 | static void xs_udp_data_ready(struct sock *sk, int len) |
| 961 | { |
| 962 | struct rpc_task *task; |
| 963 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 964 | struct rpc_rqst *rovr; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 965 | struct sk_buff *skb; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 966 | int err, repsize, copied; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 967 | u32 _xid; |
| 968 | __be32 *xp; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 969 | |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 970 | read_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 971 | dprintk("RPC: xs_udp_data_ready...\n"); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 972 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 973 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 974 | |
| 975 | if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) |
| 976 | goto out; |
| 977 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 978 | repsize = skb->len - sizeof(struct udphdr); |
| 979 | if (repsize < 4) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 980 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 981 | goto dropit; |
| 982 | } |
| 983 | |
| 984 | /* Copy the XID from the skb... */ |
| 985 | xp = skb_header_pointer(skb, sizeof(struct udphdr), |
| 986 | sizeof(_xid), &_xid); |
| 987 | if (xp == NULL) |
| 988 | goto dropit; |
| 989 | |
| 990 | /* Look up and lock the request corresponding to the given XID */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 991 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 992 | rovr = xprt_lookup_rqst(xprt, *xp); |
| 993 | if (!rovr) |
| 994 | goto out_unlock; |
| 995 | task = rovr->rq_task; |
| 996 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 997 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
| 998 | copied = repsize; |
| 999 | |
| 1000 | /* Suck it into the iovec, verify checksum if not done by hw. */ |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 1001 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { |
| 1002 | UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1003 | goto out_unlock; |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1007 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1008 | xprt_adjust_cwnd(task, copied); |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1009 | xprt_complete_rqst(task, copied); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1010 | |
| 1011 | out_unlock: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1012 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1013 | dropit: |
| 1014 | skb_free_datagram(sk, skb); |
| 1015 | out: |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1016 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1017 | } |
| 1018 | |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 1019 | /* |
| 1020 | * Helper function to force a TCP close if the server is sending |
| 1021 | * junk and/or it has put us in CLOSE_WAIT |
| 1022 | */ |
| 1023 | static void xs_tcp_force_close(struct rpc_xprt *xprt) |
| 1024 | { |
| 1025 | set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); |
| 1026 | xprt_force_disconnect(xprt); |
| 1027 | } |
| 1028 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 1029 | static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1030 | { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1031 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1032 | size_t len, used; |
| 1033 | char *p; |
| 1034 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1035 | p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; |
| 1036 | len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 1037 | used = xdr_skb_read_bits(desc, p, len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1038 | transport->tcp_offset += used; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1039 | if (used != len) |
| 1040 | return; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1041 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1042 | transport->tcp_reclen = ntohl(transport->tcp_fraghdr); |
| 1043 | if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1044 | transport->tcp_flags |= TCP_RCV_LAST_FRAG; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1045 | else |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1046 | transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1047 | transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1048 | |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1049 | transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1050 | transport->tcp_offset = 0; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1051 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1052 | /* Sanity check of the record length */ |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1053 | if (unlikely(transport->tcp_reclen < 8)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1054 | dprintk("RPC: invalid TCP record fragment length\n"); |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 1055 | xs_tcp_force_close(xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1056 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1057 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1058 | dprintk("RPC: reading TCP record fragment of length %d\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1059 | transport->tcp_reclen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1062 | static void xs_tcp_check_fraghdr(struct sock_xprt *transport) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1063 | { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1064 | if (transport->tcp_offset == transport->tcp_reclen) { |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1065 | transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1066 | transport->tcp_offset = 0; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1067 | if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { |
| 1068 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
| 1069 | transport->tcp_flags |= TCP_RCV_COPY_XID; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1070 | transport->tcp_copied = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 1075 | static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1076 | { |
| 1077 | size_t len, used; |
| 1078 | char *p; |
| 1079 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1080 | len = sizeof(transport->tcp_xid) - transport->tcp_offset; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1081 | dprintk("RPC: reading XID (%Zu bytes)\n", len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1082 | p = ((char *) &transport->tcp_xid) + transport->tcp_offset; |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 1083 | used = xdr_skb_read_bits(desc, p, len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1084 | transport->tcp_offset += used; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1085 | if (used != len) |
| 1086 | return; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1087 | transport->tcp_flags &= ~TCP_RCV_COPY_XID; |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 1088 | transport->tcp_flags |= TCP_RCV_READ_CALLDIR; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1089 | transport->tcp_copied = 4; |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1090 | dprintk("RPC: reading %s XID %08x\n", |
| 1091 | (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for" |
| 1092 | : "request with", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1093 | ntohl(transport->tcp_xid)); |
| 1094 | xs_tcp_check_fraghdr(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1095 | } |
| 1096 | |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1097 | static inline void xs_tcp_read_calldir(struct sock_xprt *transport, |
| 1098 | struct xdr_skb_reader *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1099 | { |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1100 | size_t len, used; |
| 1101 | u32 offset; |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 1102 | char *p; |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1103 | |
| 1104 | /* |
| 1105 | * We want transport->tcp_offset to be 8 at the end of this routine |
| 1106 | * (4 bytes for the xid and 4 bytes for the call/reply flag). |
| 1107 | * When this function is called for the first time, |
| 1108 | * transport->tcp_offset is 4 (after having already read the xid). |
| 1109 | */ |
| 1110 | offset = transport->tcp_offset - sizeof(transport->tcp_xid); |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 1111 | len = sizeof(transport->tcp_calldir) - offset; |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1112 | dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len); |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 1113 | p = ((char *) &transport->tcp_calldir) + offset; |
| 1114 | used = xdr_skb_read_bits(desc, p, len); |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1115 | transport->tcp_offset += used; |
| 1116 | if (used != len) |
| 1117 | return; |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 1118 | transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR; |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 1119 | /* |
| 1120 | * We don't yet have the XDR buffer, so we will write the calldir |
| 1121 | * out after we get the buffer from the 'struct rpc_rqst' |
| 1122 | */ |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 1123 | switch (ntohl(transport->tcp_calldir)) { |
| 1124 | case RPC_REPLY: |
| 1125 | transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; |
| 1126 | transport->tcp_flags |= TCP_RCV_COPY_DATA; |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1127 | transport->tcp_flags |= TCP_RPC_REPLY; |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 1128 | break; |
| 1129 | case RPC_CALL: |
| 1130 | transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; |
| 1131 | transport->tcp_flags |= TCP_RCV_COPY_DATA; |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1132 | transport->tcp_flags &= ~TCP_RPC_REPLY; |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 1133 | break; |
| 1134 | default: |
| 1135 | dprintk("RPC: invalid request message type\n"); |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 1136 | xs_tcp_force_close(&transport->xprt); |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 1137 | } |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1138 | xs_tcp_check_fraghdr(transport); |
| 1139 | } |
| 1140 | |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1141 | static inline void xs_tcp_read_common(struct rpc_xprt *xprt, |
| 1142 | struct xdr_skb_reader *desc, |
| 1143 | struct rpc_rqst *req) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1144 | { |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1145 | struct sock_xprt *transport = |
| 1146 | container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1147 | struct xdr_buf *rcvbuf; |
| 1148 | size_t len; |
| 1149 | ssize_t r; |
| 1150 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1151 | rcvbuf = &req->rq_private_buf; |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 1152 | |
| 1153 | if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) { |
| 1154 | /* |
| 1155 | * Save the RPC direction in the XDR buffer |
| 1156 | */ |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 1157 | memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied, |
Trond Myklebust | b76ce56 | 2010-06-16 13:57:32 -0400 | [diff] [blame] | 1158 | &transport->tcp_calldir, |
| 1159 | sizeof(transport->tcp_calldir)); |
| 1160 | transport->tcp_copied += sizeof(transport->tcp_calldir); |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 1161 | transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1162 | } |
| 1163 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1164 | len = desc->count; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1165 | if (len > transport->tcp_reclen - transport->tcp_offset) { |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 1166 | struct xdr_skb_reader my_desc; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1167 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1168 | len = transport->tcp_reclen - transport->tcp_offset; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1169 | memcpy(&my_desc, desc, sizeof(my_desc)); |
| 1170 | my_desc.count = len; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1171 | r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 1172 | &my_desc, xdr_skb_read_bits); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1173 | desc->count -= r; |
| 1174 | desc->offset += r; |
| 1175 | } else |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1176 | r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 1177 | desc, xdr_skb_read_bits); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1178 | |
| 1179 | if (r > 0) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1180 | transport->tcp_copied += r; |
| 1181 | transport->tcp_offset += r; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1182 | } |
| 1183 | if (r != len) { |
| 1184 | /* Error when copying to the receive buffer, |
| 1185 | * usually because we weren't able to allocate |
| 1186 | * additional buffer pages. All we can do now |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1187 | * is turn off TCP_RCV_COPY_DATA, so the request |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1188 | * will not receive any additional updates, |
| 1189 | * and time out. |
| 1190 | * Any remaining data from this record will |
| 1191 | * be discarded. |
| 1192 | */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1193 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1194 | dprintk("RPC: XID %08x truncated request\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1195 | ntohl(transport->tcp_xid)); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1196 | dprintk("RPC: xprt = %p, tcp_copied = %lu, " |
| 1197 | "tcp_offset = %u, tcp_reclen = %u\n", |
| 1198 | xprt, transport->tcp_copied, |
| 1199 | transport->tcp_offset, transport->tcp_reclen); |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1200 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1203 | dprintk("RPC: XID %08x read %Zd bytes\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1204 | ntohl(transport->tcp_xid), r); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1205 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " |
| 1206 | "tcp_reclen = %u\n", xprt, transport->tcp_copied, |
| 1207 | transport->tcp_offset, transport->tcp_reclen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1208 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1209 | if (transport->tcp_copied == req->rq_private_buf.buflen) |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1210 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1211 | else if (transport->tcp_offset == transport->tcp_reclen) { |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1212 | if (transport->tcp_flags & TCP_RCV_LAST_FRAG) |
| 1213 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1214 | } |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Finds the request corresponding to the RPC xid and invokes the common |
| 1219 | * tcp read code to read the data. |
| 1220 | */ |
| 1221 | static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, |
| 1222 | struct xdr_skb_reader *desc) |
| 1223 | { |
| 1224 | struct sock_xprt *transport = |
| 1225 | container_of(xprt, struct sock_xprt, xprt); |
| 1226 | struct rpc_rqst *req; |
| 1227 | |
| 1228 | dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid)); |
| 1229 | |
| 1230 | /* Find and lock the request corresponding to this xid */ |
| 1231 | spin_lock(&xprt->transport_lock); |
| 1232 | req = xprt_lookup_rqst(xprt, transport->tcp_xid); |
| 1233 | if (!req) { |
| 1234 | dprintk("RPC: XID %08x request not found!\n", |
| 1235 | ntohl(transport->tcp_xid)); |
| 1236 | spin_unlock(&xprt->transport_lock); |
| 1237 | return -1; |
| 1238 | } |
| 1239 | |
| 1240 | xs_tcp_read_common(xprt, desc, req); |
| 1241 | |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1242 | if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1243 | xprt_complete_rqst(req->rq_task, transport->tcp_copied); |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1244 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1245 | spin_unlock(&xprt->transport_lock); |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1249 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1250 | /* |
| 1251 | * Obtains an rpc_rqst previously allocated and invokes the common |
| 1252 | * tcp read code to read the data. The result is placed in the callback |
| 1253 | * queue. |
| 1254 | * If we're unable to obtain the rpc_rqst we schedule the closing of the |
| 1255 | * connection and return -1. |
| 1256 | */ |
| 1257 | static inline int xs_tcp_read_callback(struct rpc_xprt *xprt, |
| 1258 | struct xdr_skb_reader *desc) |
| 1259 | { |
| 1260 | struct sock_xprt *transport = |
| 1261 | container_of(xprt, struct sock_xprt, xprt); |
| 1262 | struct rpc_rqst *req; |
| 1263 | |
| 1264 | req = xprt_alloc_bc_request(xprt); |
| 1265 | if (req == NULL) { |
| 1266 | printk(KERN_WARNING "Callback slot table overflowed\n"); |
| 1267 | xprt_force_disconnect(xprt); |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
| 1271 | req->rq_xid = transport->tcp_xid; |
| 1272 | dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); |
| 1273 | xs_tcp_read_common(xprt, desc, req); |
| 1274 | |
| 1275 | if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) { |
| 1276 | struct svc_serv *bc_serv = xprt->bc_serv; |
| 1277 | |
| 1278 | /* |
| 1279 | * Add callback request to callback list. The callback |
| 1280 | * service sleeps on the sv_cb_waitq waiting for new |
| 1281 | * requests. Wake it up after adding enqueing the |
| 1282 | * request. |
| 1283 | */ |
| 1284 | dprintk("RPC: add callback request to list\n"); |
| 1285 | spin_lock(&bc_serv->sv_cb_lock); |
| 1286 | list_add(&req->rq_bc_list, &bc_serv->sv_cb_list); |
| 1287 | spin_unlock(&bc_serv->sv_cb_lock); |
| 1288 | wake_up(&bc_serv->sv_cb_waitq); |
| 1289 | } |
| 1290 | |
| 1291 | req->rq_private_buf.len = transport->tcp_copied; |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, |
| 1297 | struct xdr_skb_reader *desc) |
| 1298 | { |
| 1299 | struct sock_xprt *transport = |
| 1300 | container_of(xprt, struct sock_xprt, xprt); |
| 1301 | |
| 1302 | return (transport->tcp_flags & TCP_RPC_REPLY) ? |
| 1303 | xs_tcp_read_reply(xprt, desc) : |
| 1304 | xs_tcp_read_callback(xprt, desc); |
| 1305 | } |
| 1306 | #else |
| 1307 | static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, |
| 1308 | struct xdr_skb_reader *desc) |
| 1309 | { |
| 1310 | return xs_tcp_read_reply(xprt, desc); |
| 1311 | } |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1312 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1313 | |
| 1314 | /* |
| 1315 | * Read data off the transport. This can be either an RPC_CALL or an |
| 1316 | * RPC_REPLY. Relay the processing to helper functions. |
| 1317 | */ |
| 1318 | static void xs_tcp_read_data(struct rpc_xprt *xprt, |
| 1319 | struct xdr_skb_reader *desc) |
| 1320 | { |
| 1321 | struct sock_xprt *transport = |
| 1322 | container_of(xprt, struct sock_xprt, xprt); |
| 1323 | |
| 1324 | if (_xs_tcp_read_data(xprt, desc) == 0) |
| 1325 | xs_tcp_check_fraghdr(transport); |
| 1326 | else { |
| 1327 | /* |
| 1328 | * The transport_lock protects the request handling. |
| 1329 | * There's no need to hold it to update the tcp_flags. |
| 1330 | */ |
| 1331 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
| 1332 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1333 | } |
| 1334 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 1335 | static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1336 | { |
| 1337 | size_t len; |
| 1338 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1339 | len = transport->tcp_reclen - transport->tcp_offset; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1340 | if (len > desc->count) |
| 1341 | len = desc->count; |
| 1342 | desc->count -= len; |
| 1343 | desc->offset += len; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1344 | transport->tcp_offset += len; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1345 | dprintk("RPC: discarded %Zu bytes\n", len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1346 | xs_tcp_check_fraghdr(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1347 | } |
| 1348 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1349 | static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1350 | { |
| 1351 | struct rpc_xprt *xprt = rd_desc->arg.data; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1352 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 1353 | struct xdr_skb_reader desc = { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1354 | .skb = skb, |
| 1355 | .offset = offset, |
| 1356 | .count = len, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1357 | }; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1358 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1359 | dprintk("RPC: xs_tcp_data_recv started\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1360 | do { |
| 1361 | /* Read in a new fragment marker if necessary */ |
| 1362 | /* Can we ever really expect to get completely empty fragments? */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1363 | if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1364 | xs_tcp_read_fraghdr(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1365 | continue; |
| 1366 | } |
| 1367 | /* Read in the xid if necessary */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1368 | if (transport->tcp_flags & TCP_RCV_COPY_XID) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1369 | xs_tcp_read_xid(transport, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1370 | continue; |
| 1371 | } |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1372 | /* Read in the call/reply flag */ |
Ricardo Labiaga | f4a2e41 | 2009-04-01 09:22:54 -0400 | [diff] [blame] | 1373 | if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) { |
Ricardo Labiaga | 18dca02 | 2009-04-01 09:22:53 -0400 | [diff] [blame] | 1374 | xs_tcp_read_calldir(transport, &desc); |
| 1375 | continue; |
| 1376 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1377 | /* Read in the request data */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1378 | if (transport->tcp_flags & TCP_RCV_COPY_DATA) { |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1379 | xs_tcp_read_data(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1380 | continue; |
| 1381 | } |
| 1382 | /* Skip over any trailing bytes on short reads */ |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1383 | xs_tcp_read_discard(transport, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1384 | } while (desc.count); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1385 | dprintk("RPC: xs_tcp_data_recv done\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1386 | return len - desc.count; |
| 1387 | } |
| 1388 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1389 | /** |
| 1390 | * xs_tcp_data_ready - "data ready" callback for TCP sockets |
| 1391 | * @sk: socket with data to read |
| 1392 | * @bytes: how much data to read |
| 1393 | * |
| 1394 | */ |
| 1395 | static void xs_tcp_data_ready(struct sock *sk, int bytes) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1396 | { |
| 1397 | struct rpc_xprt *xprt; |
| 1398 | read_descriptor_t rd_desc; |
Trond Myklebust | ff2d7db | 2008-02-25 21:40:51 -0800 | [diff] [blame] | 1399 | int read; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1400 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1401 | dprintk("RPC: xs_tcp_data_ready...\n"); |
| 1402 | |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1403 | read_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1404 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1405 | goto out; |
Neil Brown | 61d0a8e | 2009-09-23 14:36:37 -0400 | [diff] [blame] | 1406 | /* Any data means we had a useful conversation, so |
| 1407 | * the we don't need to delay the next reconnect |
| 1408 | */ |
| 1409 | if (xprt->reestablish_timeout) |
| 1410 | xprt->reestablish_timeout = 0; |
| 1411 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1412 | /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1413 | rd_desc.arg.data = xprt; |
Trond Myklebust | ff2d7db | 2008-02-25 21:40:51 -0800 | [diff] [blame] | 1414 | do { |
| 1415 | rd_desc.count = 65536; |
| 1416 | read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); |
| 1417 | } while (read > 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1418 | out: |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1419 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1420 | } |
| 1421 | |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1422 | /* |
| 1423 | * Do the equivalent of linger/linger2 handling for dealing with |
| 1424 | * broken servers that don't close the socket in a timely |
| 1425 | * fashion |
| 1426 | */ |
| 1427 | static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt, |
| 1428 | unsigned long timeout) |
| 1429 | { |
| 1430 | struct sock_xprt *transport; |
| 1431 | |
| 1432 | if (xprt_test_and_set_connecting(xprt)) |
| 1433 | return; |
| 1434 | set_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
| 1435 | transport = container_of(xprt, struct sock_xprt, xprt); |
| 1436 | queue_delayed_work(rpciod_workqueue, &transport->connect_worker, |
| 1437 | timeout); |
| 1438 | } |
| 1439 | |
| 1440 | static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt) |
| 1441 | { |
| 1442 | struct sock_xprt *transport; |
| 1443 | |
| 1444 | transport = container_of(xprt, struct sock_xprt, xprt); |
| 1445 | |
| 1446 | if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) || |
| 1447 | !cancel_delayed_work(&transport->connect_worker)) |
| 1448 | return; |
| 1449 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
| 1450 | xprt_clear_connecting(xprt); |
| 1451 | } |
| 1452 | |
Trond Myklebust | 4bc1e68 | 2012-10-23 17:50:07 -0400 | [diff] [blame] | 1453 | static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1454 | { |
| 1455 | smp_mb__before_clear_bit(); |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 1456 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
| 1457 | clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1458 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 1459 | clear_bit(XPRT_CLOSING, &xprt->state); |
| 1460 | smp_mb__after_clear_bit(); |
Trond Myklebust | 4bc1e68 | 2012-10-23 17:50:07 -0400 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static void xs_sock_mark_closed(struct rpc_xprt *xprt) |
| 1464 | { |
| 1465 | xs_sock_reset_connection_flags(xprt); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1466 | /* Mark transport as closed and wake up all pending tasks */ |
| 1467 | xprt_disconnect_done(xprt); |
| 1468 | } |
| 1469 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1470 | /** |
| 1471 | * xs_tcp_state_change - callback to handle TCP socket state changes |
| 1472 | * @sk: socket whose state has changed |
| 1473 | * |
| 1474 | */ |
| 1475 | static void xs_tcp_state_change(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1476 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1477 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1478 | |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1479 | read_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1480 | if (!(xprt = xprt_from_sock(sk))) |
| 1481 | goto out; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1482 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
Andy Chittenden | 669502f | 2010-08-10 10:19:53 -0400 | [diff] [blame] | 1483 | dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1484 | sk->sk_state, xprt_connected(xprt), |
| 1485 | sock_flag(sk, SOCK_DEAD), |
Andy Chittenden | 669502f | 2010-08-10 10:19:53 -0400 | [diff] [blame] | 1486 | sock_flag(sk, SOCK_ZAPPED), |
| 1487 | sk->sk_shutdown); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1488 | |
| 1489 | switch (sk->sk_state) { |
| 1490 | case TCP_ESTABLISHED: |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1491 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1492 | if (!xprt_test_and_set_connected(xprt)) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1493 | struct sock_xprt *transport = container_of(xprt, |
| 1494 | struct sock_xprt, xprt); |
| 1495 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1496 | /* Reset TCP record info */ |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1497 | transport->tcp_offset = 0; |
| 1498 | transport->tcp_reclen = 0; |
| 1499 | transport->tcp_copied = 0; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1500 | transport->tcp_flags = |
| 1501 | TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1502 | |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1503 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1504 | } |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1505 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1506 | break; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1507 | case TCP_FIN_WAIT1: |
| 1508 | /* The client initiated a shutdown of the socket */ |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 1509 | xprt->connect_cookie++; |
Trond Myklebust | 663b885 | 2008-01-01 18:42:12 -0500 | [diff] [blame] | 1510 | xprt->reestablish_timeout = 0; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1511 | set_bit(XPRT_CLOSING, &xprt->state); |
| 1512 | smp_mb__before_clear_bit(); |
| 1513 | clear_bit(XPRT_CONNECTED, &xprt->state); |
Trond Myklebust | ef80367 | 2007-12-31 16:19:17 -0500 | [diff] [blame] | 1514 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1515 | smp_mb__after_clear_bit(); |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1516 | xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1517 | break; |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 1518 | case TCP_CLOSE_WAIT: |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1519 | /* The server initiated a shutdown of the socket */ |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 1520 | xprt->connect_cookie++; |
Trond Myklebust | d0bea45 | 2012-10-23 11:35:47 -0400 | [diff] [blame] | 1521 | clear_bit(XPRT_CONNECTED, &xprt->state); |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 1522 | xs_tcp_force_close(xprt); |
Trond Myklebust | 663b885 | 2008-01-01 18:42:12 -0500 | [diff] [blame] | 1523 | case TCP_CLOSING: |
| 1524 | /* |
| 1525 | * If the server closed down the connection, make sure that |
| 1526 | * we back off before reconnecting |
| 1527 | */ |
| 1528 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
| 1529 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1530 | break; |
| 1531 | case TCP_LAST_ACK: |
Trond Myklebust | 670f945 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 1532 | set_bit(XPRT_CLOSING, &xprt->state); |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1533 | xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1534 | smp_mb__before_clear_bit(); |
| 1535 | clear_bit(XPRT_CONNECTED, &xprt->state); |
| 1536 | smp_mb__after_clear_bit(); |
| 1537 | break; |
| 1538 | case TCP_CLOSE: |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1539 | xs_tcp_cancel_linger_timeout(xprt); |
| 1540 | xs_sock_mark_closed(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1541 | } |
| 1542 | out: |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1543 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1544 | } |
| 1545 | |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1546 | static void xs_write_space(struct sock *sk) |
| 1547 | { |
| 1548 | struct socket *sock; |
| 1549 | struct rpc_xprt *xprt; |
| 1550 | |
| 1551 | if (unlikely(!(sock = sk->sk_socket))) |
| 1552 | return; |
| 1553 | clear_bit(SOCK_NOSPACE, &sock->flags); |
| 1554 | |
| 1555 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
| 1556 | return; |
| 1557 | if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) |
| 1558 | return; |
| 1559 | |
| 1560 | xprt_write_space(xprt); |
| 1561 | } |
| 1562 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1563 | /** |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1564 | * xs_udp_write_space - callback invoked when socket buffer space |
| 1565 | * becomes available |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1566 | * @sk: socket whose state has changed |
| 1567 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1568 | * Called when more output buffer space is available for this socket. |
| 1569 | * We try not to wake our writers until they can make "significant" |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1570 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1571 | * with a bunch of small requests. |
| 1572 | */ |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1573 | static void xs_udp_write_space(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1574 | { |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1575 | read_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1576 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1577 | /* from net/core/sock.c:sock_def_write_space */ |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1578 | if (sock_writeable(sk)) |
| 1579 | xs_write_space(sk); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1580 | |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1581 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1582 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1583 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1584 | /** |
| 1585 | * xs_tcp_write_space - callback invoked when socket buffer space |
| 1586 | * becomes available |
| 1587 | * @sk: socket whose state has changed |
| 1588 | * |
| 1589 | * Called when more output buffer space is available for this socket. |
| 1590 | * We try not to wake our writers until they can make "significant" |
| 1591 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
| 1592 | * with a bunch of small requests. |
| 1593 | */ |
| 1594 | static void xs_tcp_write_space(struct sock *sk) |
| 1595 | { |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1596 | read_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1597 | |
| 1598 | /* from net/core/stream.c:sk_stream_write_space */ |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1599 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) |
| 1600 | xs_write_space(sk); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1601 | |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1602 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1605 | static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1606 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1607 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1608 | struct sock *sk = transport->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1609 | |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1610 | if (transport->rcvsize) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1611 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1612 | sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1613 | } |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1614 | if (transport->sndsize) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1615 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1616 | sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1617 | sk->sk_write_space(sk); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1621 | /** |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1622 | * xs_udp_set_buffer_size - set send and receive limits |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1623 | * @xprt: generic transport |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1624 | * @sndsize: requested size of send buffer, in bytes |
| 1625 | * @rcvsize: requested size of receive buffer, in bytes |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1626 | * |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1627 | * Set socket send and receive buffer size limits. |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1628 | */ |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1629 | static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1630 | { |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1631 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1632 | |
| 1633 | transport->sndsize = 0; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1634 | if (sndsize) |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1635 | transport->sndsize = sndsize + 1024; |
| 1636 | transport->rcvsize = 0; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1637 | if (rcvsize) |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1638 | transport->rcvsize = rcvsize + 1024; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1639 | |
| 1640 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1643 | /** |
| 1644 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport |
| 1645 | * @task: task that timed out |
| 1646 | * |
| 1647 | * Adjust the congestion window after a retransmit timeout has occurred. |
| 1648 | */ |
| 1649 | static void xs_udp_timer(struct rpc_task *task) |
| 1650 | { |
| 1651 | xprt_adjust_cwnd(task, -ETIMEDOUT); |
| 1652 | } |
| 1653 | |
Chuck Lever | b85d880 | 2006-05-25 01:40:49 -0400 | [diff] [blame] | 1654 | static unsigned short xs_get_random_port(void) |
| 1655 | { |
| 1656 | unsigned short range = xprt_max_resvport - xprt_min_resvport; |
| 1657 | unsigned short rand = (unsigned short) net_random() % range; |
| 1658 | return rand + xprt_min_resvport; |
| 1659 | } |
| 1660 | |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1661 | /** |
| 1662 | * xs_set_port - reset the port number in the remote endpoint address |
| 1663 | * @xprt: generic transport |
| 1664 | * @port: new port number |
| 1665 | * |
| 1666 | */ |
| 1667 | static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) |
| 1668 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1669 | dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); |
Chuck Lever | c4efcb1 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 1670 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 1671 | rpc_set_port(xs_addr(xprt), port); |
| 1672 | xs_update_peer_port(xprt); |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1673 | } |
| 1674 | |
Pavel Emelyanov | 5d4ec93 | 2010-10-04 16:51:23 +0400 | [diff] [blame] | 1675 | static unsigned short xs_get_srcport(struct sock_xprt *transport) |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1676 | { |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 1677 | unsigned short port = transport->srcport; |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1678 | |
| 1679 | if (port == 0 && transport->xprt.resvport) |
| 1680 | port = xs_get_random_port(); |
| 1681 | return port; |
| 1682 | } |
| 1683 | |
Pavel Emelyanov | baaf4e4 | 2010-10-04 16:51:56 +0400 | [diff] [blame] | 1684 | static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1685 | { |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 1686 | if (transport->srcport != 0) |
| 1687 | transport->srcport = 0; |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1688 | if (!transport->xprt.resvport) |
| 1689 | return 0; |
| 1690 | if (port <= xprt_min_resvport || port > xprt_max_resvport) |
| 1691 | return xprt_max_resvport; |
| 1692 | return --port; |
| 1693 | } |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1694 | static int xs_bind(struct sock_xprt *transport, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1695 | { |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1696 | struct sockaddr_storage myaddr; |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1697 | int err, nloop = 0; |
Pavel Emelyanov | 5d4ec93 | 2010-10-04 16:51:23 +0400 | [diff] [blame] | 1698 | unsigned short port = xs_get_srcport(transport); |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1699 | unsigned short last; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1700 | |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1701 | memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1702 | do { |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1703 | rpc_set_port((struct sockaddr *)&myaddr, port); |
| 1704 | err = kernel_bind(sock, (struct sockaddr *)&myaddr, |
| 1705 | transport->xprt.addrlen); |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1706 | if (port == 0) |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1707 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1708 | if (err == 0) { |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 1709 | transport->srcport = port; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1710 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1711 | } |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1712 | last = port; |
Pavel Emelyanov | baaf4e4 | 2010-10-04 16:51:56 +0400 | [diff] [blame] | 1713 | port = xs_next_srcport(transport, port); |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1714 | if (port > last) |
| 1715 | nloop++; |
| 1716 | } while (err == -EADDRINUSE && nloop != 2); |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1717 | |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1718 | if (myaddr.ss_family == AF_INET) |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1719 | dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, |
| 1720 | &((struct sockaddr_in *)&myaddr)->sin_addr, |
| 1721 | port, err ? "failed" : "ok", err); |
| 1722 | else |
| 1723 | dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, |
| 1724 | &((struct sockaddr_in6 *)&myaddr)->sin6_addr, |
| 1725 | port, err ? "failed" : "ok", err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1726 | return err; |
| 1727 | } |
| 1728 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1729 | /* |
| 1730 | * We don't support autobind on AF_LOCAL sockets |
| 1731 | */ |
| 1732 | static void xs_local_rpcbind(struct rpc_task *task) |
| 1733 | { |
| 1734 | xprt_set_bound(task->tk_xprt); |
| 1735 | } |
| 1736 | |
| 1737 | static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) |
| 1738 | { |
| 1739 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1740 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1741 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 1742 | static struct lock_class_key xs_key[2]; |
| 1743 | static struct lock_class_key xs_slock_key[2]; |
| 1744 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1745 | static inline void xs_reclassify_socketu(struct socket *sock) |
| 1746 | { |
| 1747 | struct sock *sk = sock->sk; |
| 1748 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1749 | sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", |
| 1750 | &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); |
| 1751 | } |
| 1752 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1753 | static inline void xs_reclassify_socket4(struct socket *sock) |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1754 | { |
| 1755 | struct sock *sk = sock->sk; |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1756 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1757 | sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", |
| 1758 | &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); |
| 1759 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1760 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1761 | static inline void xs_reclassify_socket6(struct socket *sock) |
| 1762 | { |
| 1763 | struct sock *sk = sock->sk; |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1764 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1765 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", |
| 1766 | &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1767 | } |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1768 | |
| 1769 | static inline void xs_reclassify_socket(int family, struct socket *sock) |
| 1770 | { |
Weston Andros Adamson | 1b7a181 | 2012-10-23 10:43:39 -0400 | [diff] [blame] | 1771 | WARN_ON_ONCE(sock_owned_by_user(sock->sk)); |
| 1772 | if (sock_owned_by_user(sock->sk)) |
| 1773 | return; |
| 1774 | |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1775 | switch (family) { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1776 | case AF_LOCAL: |
| 1777 | xs_reclassify_socketu(sock); |
| 1778 | break; |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1779 | case AF_INET: |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1780 | xs_reclassify_socket4(sock); |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1781 | break; |
| 1782 | case AF_INET6: |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1783 | xs_reclassify_socket6(sock); |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1784 | break; |
| 1785 | } |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1786 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1787 | #else |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1788 | static inline void xs_reclassify_socketu(struct socket *sock) |
| 1789 | { |
| 1790 | } |
| 1791 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1792 | static inline void xs_reclassify_socket4(struct socket *sock) |
| 1793 | { |
| 1794 | } |
| 1795 | |
| 1796 | static inline void xs_reclassify_socket6(struct socket *sock) |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1797 | { |
| 1798 | } |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1799 | |
| 1800 | static inline void xs_reclassify_socket(int family, struct socket *sock) |
| 1801 | { |
| 1802 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1803 | #endif |
| 1804 | |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1805 | static struct socket *xs_create_sock(struct rpc_xprt *xprt, |
| 1806 | struct sock_xprt *transport, int family, int type, int protocol) |
Pavel Emelyanov | 22f7932 | 2010-10-04 16:54:26 +0400 | [diff] [blame] | 1807 | { |
| 1808 | struct socket *sock; |
| 1809 | int err; |
| 1810 | |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1811 | err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); |
Pavel Emelyanov | 22f7932 | 2010-10-04 16:54:26 +0400 | [diff] [blame] | 1812 | if (err < 0) { |
| 1813 | dprintk("RPC: can't create %d transport socket (%d).\n", |
| 1814 | protocol, -err); |
| 1815 | goto out; |
| 1816 | } |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1817 | xs_reclassify_socket(family, sock); |
Pavel Emelyanov | 22f7932 | 2010-10-04 16:54:26 +0400 | [diff] [blame] | 1818 | |
Ben Hutchings | 4cea288 | 2011-02-22 21:54:34 +0000 | [diff] [blame] | 1819 | err = xs_bind(transport, sock); |
| 1820 | if (err) { |
Pavel Emelyanov | 22f7932 | 2010-10-04 16:54:26 +0400 | [diff] [blame] | 1821 | sock_release(sock); |
| 1822 | goto out; |
| 1823 | } |
| 1824 | |
| 1825 | return sock; |
| 1826 | out: |
| 1827 | return ERR_PTR(err); |
| 1828 | } |
| 1829 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1830 | static int xs_local_finish_connecting(struct rpc_xprt *xprt, |
| 1831 | struct socket *sock) |
| 1832 | { |
| 1833 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, |
| 1834 | xprt); |
| 1835 | |
| 1836 | if (!transport->inet) { |
| 1837 | struct sock *sk = sock->sk; |
| 1838 | |
| 1839 | write_lock_bh(&sk->sk_callback_lock); |
| 1840 | |
| 1841 | xs_save_old_callbacks(transport, sk); |
| 1842 | |
| 1843 | sk->sk_user_data = xprt; |
| 1844 | sk->sk_data_ready = xs_local_data_ready; |
| 1845 | sk->sk_write_space = xs_udp_write_space; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1846 | sk->sk_allocation = GFP_ATOMIC; |
| 1847 | |
| 1848 | xprt_clear_connected(xprt); |
| 1849 | |
| 1850 | /* Reset to new socket */ |
| 1851 | transport->sock = sock; |
| 1852 | transport->inet = sk; |
| 1853 | |
| 1854 | write_unlock_bh(&sk->sk_callback_lock); |
| 1855 | } |
| 1856 | |
| 1857 | /* Tell the socket layer to start connecting... */ |
| 1858 | xprt->stat.connect_count++; |
| 1859 | xprt->stat.connect_start = jiffies; |
| 1860 | return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); |
| 1861 | } |
| 1862 | |
| 1863 | /** |
| 1864 | * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint |
| 1865 | * @xprt: RPC transport to connect |
| 1866 | * @transport: socket transport to connect |
| 1867 | * @create_sock: function to create a socket of the correct type |
| 1868 | * |
| 1869 | * Invoked by a work queue tasklet. |
| 1870 | */ |
| 1871 | static void xs_local_setup_socket(struct work_struct *work) |
| 1872 | { |
| 1873 | struct sock_xprt *transport = |
| 1874 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1875 | struct rpc_xprt *xprt = &transport->xprt; |
| 1876 | struct socket *sock; |
| 1877 | int status = -EIO; |
| 1878 | |
Jeff Layton | 5cf02d0 | 2012-07-23 13:58:51 -0400 | [diff] [blame] | 1879 | current->flags |= PF_FSTRANS; |
| 1880 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1881 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
| 1882 | status = __sock_create(xprt->xprt_net, AF_LOCAL, |
| 1883 | SOCK_STREAM, 0, &sock, 1); |
| 1884 | if (status < 0) { |
| 1885 | dprintk("RPC: can't create AF_LOCAL " |
| 1886 | "transport socket (%d).\n", -status); |
| 1887 | goto out; |
| 1888 | } |
| 1889 | xs_reclassify_socketu(sock); |
| 1890 | |
| 1891 | dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", |
| 1892 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1893 | |
| 1894 | status = xs_local_finish_connecting(xprt, sock); |
| 1895 | switch (status) { |
| 1896 | case 0: |
| 1897 | dprintk("RPC: xprt %p connected to %s\n", |
| 1898 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1899 | xprt_set_connected(xprt); |
| 1900 | break; |
| 1901 | case -ENOENT: |
| 1902 | dprintk("RPC: xprt %p: socket %s does not exist\n", |
| 1903 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1904 | break; |
Trond Myklebust | 4a20a98 | 2012-12-15 17:02:29 -0500 | [diff] [blame] | 1905 | case -ECONNREFUSED: |
| 1906 | dprintk("RPC: xprt %p: connection refused for %s\n", |
| 1907 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1908 | break; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1909 | default: |
| 1910 | printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", |
| 1911 | __func__, -status, |
| 1912 | xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1913 | } |
| 1914 | |
| 1915 | out: |
| 1916 | xprt_clear_connecting(xprt); |
| 1917 | xprt_wake_pending_tasks(xprt, status); |
Jeff Layton | 5cf02d0 | 2012-07-23 13:58:51 -0400 | [diff] [blame] | 1918 | current->flags &= ~PF_FSTRANS; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1919 | } |
| 1920 | |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 1921 | #ifdef CONFIG_SUNRPC_SWAP |
| 1922 | static void xs_set_memalloc(struct rpc_xprt *xprt) |
| 1923 | { |
| 1924 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, |
| 1925 | xprt); |
| 1926 | |
| 1927 | if (xprt->swapper) |
| 1928 | sk_set_memalloc(transport->inet); |
| 1929 | } |
| 1930 | |
| 1931 | /** |
| 1932 | * xs_swapper - Tag this transport as being used for swap. |
| 1933 | * @xprt: transport to tag |
| 1934 | * @enable: enable/disable |
| 1935 | * |
| 1936 | */ |
| 1937 | int xs_swapper(struct rpc_xprt *xprt, int enable) |
| 1938 | { |
| 1939 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, |
| 1940 | xprt); |
| 1941 | int err = 0; |
| 1942 | |
| 1943 | if (enable) { |
| 1944 | xprt->swapper++; |
| 1945 | xs_set_memalloc(xprt); |
| 1946 | } else if (xprt->swapper) { |
| 1947 | xprt->swapper--; |
| 1948 | sk_clear_memalloc(transport->inet); |
| 1949 | } |
| 1950 | |
| 1951 | return err; |
| 1952 | } |
| 1953 | EXPORT_SYMBOL_GPL(xs_swapper); |
| 1954 | #else |
| 1955 | static void xs_set_memalloc(struct rpc_xprt *xprt) |
| 1956 | { |
| 1957 | } |
| 1958 | #endif |
| 1959 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1960 | static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1961 | { |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1962 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 1963 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1964 | if (!transport->inet) { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1965 | struct sock *sk = sock->sk; |
| 1966 | |
| 1967 | write_lock_bh(&sk->sk_callback_lock); |
| 1968 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1969 | xs_save_old_callbacks(transport, sk); |
| 1970 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1971 | sk->sk_user_data = xprt; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1972 | sk->sk_data_ready = xs_udp_data_ready; |
| 1973 | sk->sk_write_space = xs_udp_write_space; |
| 1974 | sk->sk_no_check = UDP_CSUM_NORCV; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 1975 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1976 | |
| 1977 | xprt_set_connected(xprt); |
| 1978 | |
| 1979 | /* Reset to new socket */ |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1980 | transport->sock = sock; |
| 1981 | transport->inet = sk; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1982 | |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 1983 | xs_set_memalloc(xprt); |
| 1984 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1985 | write_unlock_bh(&sk->sk_callback_lock); |
| 1986 | } |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1987 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
Pavel Emelyanov | 8c14ff2 | 2010-10-04 16:58:02 +0400 | [diff] [blame] | 1990 | static void xs_udp_setup_socket(struct work_struct *work) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1991 | { |
| 1992 | struct sock_xprt *transport = |
| 1993 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1994 | struct rpc_xprt *xprt = &transport->xprt; |
| 1995 | struct socket *sock = transport->sock; |
Pavel Emelyanov | b65c031 | 2010-10-04 16:53:46 +0400 | [diff] [blame] | 1996 | int status = -EIO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1997 | |
Jeff Layton | 5cf02d0 | 2012-07-23 13:58:51 -0400 | [diff] [blame] | 1998 | current->flags |= PF_FSTRANS; |
| 1999 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2000 | /* Start by resetting any existing state */ |
Chuck Lever | fe315e7 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 2001 | xs_reset_transport(transport); |
Pavel Emelyanov | 8c14ff2 | 2010-10-04 16:58:02 +0400 | [diff] [blame] | 2002 | sock = xs_create_sock(xprt, transport, |
| 2003 | xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP); |
Pavel Emelyanov | b65c031 | 2010-10-04 16:53:46 +0400 | [diff] [blame] | 2004 | if (IS_ERR(sock)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2005 | goto out; |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 2006 | |
Chuck Lever | c740eff | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2007 | dprintk("RPC: worker connecting xprt %p via %s to " |
| 2008 | "%s (port %s)\n", xprt, |
| 2009 | xprt->address_strings[RPC_DISPLAY_PROTO], |
| 2010 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2011 | xprt->address_strings[RPC_DISPLAY_PORT]); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 2012 | |
| 2013 | xs_udp_finish_connecting(xprt, sock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2014 | status = 0; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2015 | out: |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2016 | xprt_clear_connecting(xprt); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2017 | xprt_wake_pending_tasks(xprt, status); |
Jeff Layton | 5cf02d0 | 2012-07-23 13:58:51 -0400 | [diff] [blame] | 2018 | current->flags &= ~PF_FSTRANS; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2019 | } |
| 2020 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2021 | /* |
| 2022 | * We need to preserve the port number so the reply cache on the server can |
| 2023 | * find our cached RPC replies when we get around to reconnecting. |
| 2024 | */ |
Pavel Emelyanov | 58dddac | 2010-10-04 16:52:55 +0400 | [diff] [blame] | 2025 | static void xs_abort_connection(struct sock_xprt *transport) |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2026 | { |
| 2027 | int result; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2028 | struct sockaddr any; |
| 2029 | |
Pavel Emelyanov | 58dddac | 2010-10-04 16:52:55 +0400 | [diff] [blame] | 2030 | dprintk("RPC: disconnecting xprt %p to reuse port\n", transport); |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2031 | |
| 2032 | /* |
| 2033 | * Disconnect the transport socket by doing a connect operation |
| 2034 | * with AF_UNSPEC. This should return immediately... |
| 2035 | */ |
| 2036 | memset(&any, 0, sizeof(any)); |
| 2037 | any.sa_family = AF_UNSPEC; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2038 | result = kernel_connect(transport->sock, &any, sizeof(any), 0); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2039 | if (!result) |
Trond Myklebust | 4bc1e68 | 2012-10-23 17:50:07 -0400 | [diff] [blame] | 2040 | xs_sock_reset_connection_flags(&transport->xprt); |
| 2041 | dprintk("RPC: AF_UNSPEC connect return code %d\n", result); |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Pavel Emelyanov | 58dddac | 2010-10-04 16:52:55 +0400 | [diff] [blame] | 2044 | static void xs_tcp_reuse_connection(struct sock_xprt *transport) |
Trond Myklebust | 40d2549 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 2045 | { |
| 2046 | unsigned int state = transport->inet->sk_state; |
| 2047 | |
Andy Chittenden | 669502f | 2010-08-10 10:19:53 -0400 | [diff] [blame] | 2048 | if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) { |
| 2049 | /* we don't need to abort the connection if the socket |
| 2050 | * hasn't undergone a shutdown |
| 2051 | */ |
| 2052 | if (transport->inet->sk_shutdown == 0) |
| 2053 | return; |
| 2054 | dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n", |
| 2055 | __func__, transport->inet->sk_shutdown); |
| 2056 | } |
| 2057 | if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) { |
| 2058 | /* we don't need to abort the connection if the socket |
| 2059 | * hasn't undergone a shutdown |
| 2060 | */ |
| 2061 | if (transport->inet->sk_shutdown == 0) |
| 2062 | return; |
| 2063 | dprintk("RPC: %s: ESTABLISHED/SYN_SENT " |
| 2064 | "sk_shutdown set to %d\n", |
| 2065 | __func__, transport->inet->sk_shutdown); |
| 2066 | } |
Pavel Emelyanov | 58dddac | 2010-10-04 16:52:55 +0400 | [diff] [blame] | 2067 | xs_abort_connection(transport); |
Trond Myklebust | 40d2549 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 2068 | } |
| 2069 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2070 | static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2071 | { |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2072 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2073 | int ret = -ENOTCONN; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 2074 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2075 | if (!transport->inet) { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2076 | struct sock *sk = sock->sk; |
| 2077 | |
| 2078 | write_lock_bh(&sk->sk_callback_lock); |
| 2079 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 2080 | xs_save_old_callbacks(transport, sk); |
| 2081 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2082 | sk->sk_user_data = xprt; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2083 | sk->sk_data_ready = xs_tcp_data_ready; |
| 2084 | sk->sk_state_change = xs_tcp_state_change; |
| 2085 | sk->sk_write_space = xs_tcp_write_space; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 2086 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2087 | |
| 2088 | /* socket options */ |
| 2089 | sk->sk_userlocks |= SOCK_BINDPORT_LOCK; |
| 2090 | sock_reset_flag(sk, SOCK_LINGER); |
| 2091 | tcp_sk(sk)->linger2 = 0; |
| 2092 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2093 | |
| 2094 | xprt_clear_connected(xprt); |
| 2095 | |
| 2096 | /* Reset to new socket */ |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2097 | transport->sock = sock; |
| 2098 | transport->inet = sk; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2099 | |
| 2100 | write_unlock_bh(&sk->sk_callback_lock); |
| 2101 | } |
| 2102 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 2103 | if (!xprt_bound(xprt)) |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2104 | goto out; |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 2105 | |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2106 | xs_set_memalloc(xprt); |
| 2107 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2108 | /* Tell the socket layer to start connecting... */ |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2109 | xprt->stat.connect_count++; |
| 2110 | xprt->stat.connect_start = jiffies; |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2111 | ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); |
| 2112 | switch (ret) { |
| 2113 | case 0: |
| 2114 | case -EINPROGRESS: |
| 2115 | /* SYN_SENT! */ |
| 2116 | xprt->connect_cookie++; |
| 2117 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
| 2118 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 2119 | } |
| 2120 | out: |
| 2121 | return ret; |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | /** |
Trond Myklebust | b61d59f | 2009-03-11 14:38:04 -0400 | [diff] [blame] | 2125 | * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint |
| 2126 | * @xprt: RPC transport to connect |
| 2127 | * @transport: socket transport to connect |
| 2128 | * @create_sock: function to create a socket of the correct type |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2129 | * |
| 2130 | * Invoked by a work queue tasklet. |
| 2131 | */ |
Pavel Emelyanov | cdd518d | 2010-10-04 16:57:40 +0400 | [diff] [blame] | 2132 | static void xs_tcp_setup_socket(struct work_struct *work) |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2133 | { |
Pavel Emelyanov | cdd518d | 2010-10-04 16:57:40 +0400 | [diff] [blame] | 2134 | struct sock_xprt *transport = |
| 2135 | container_of(work, struct sock_xprt, connect_worker.work); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2136 | struct socket *sock = transport->sock; |
Pavel Emelyanov | a9f5f0f | 2010-10-04 16:52:25 +0400 | [diff] [blame] | 2137 | struct rpc_xprt *xprt = &transport->xprt; |
Trond Myklebust | b61d59f | 2009-03-11 14:38:04 -0400 | [diff] [blame] | 2138 | int status = -EIO; |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2139 | |
Jeff Layton | 5cf02d0 | 2012-07-23 13:58:51 -0400 | [diff] [blame] | 2140 | current->flags |= PF_FSTRANS; |
| 2141 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2142 | if (!sock) { |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2143 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
Pavel Emelyanov | cdd518d | 2010-10-04 16:57:40 +0400 | [diff] [blame] | 2144 | sock = xs_create_sock(xprt, transport, |
| 2145 | xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP); |
Trond Myklebust | b61d59f | 2009-03-11 14:38:04 -0400 | [diff] [blame] | 2146 | if (IS_ERR(sock)) { |
| 2147 | status = PTR_ERR(sock); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2148 | goto out; |
| 2149 | } |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2150 | } else { |
| 2151 | int abort_and_exit; |
| 2152 | |
| 2153 | abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, |
| 2154 | &xprt->state); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2155 | /* "close" the socket, preserving the local port */ |
Pavel Emelyanov | 58dddac | 2010-10-04 16:52:55 +0400 | [diff] [blame] | 2156 | xs_tcp_reuse_connection(transport); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2157 | |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2158 | if (abort_and_exit) |
| 2159 | goto out_eagain; |
| 2160 | } |
| 2161 | |
Chuck Lever | c740eff | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2162 | dprintk("RPC: worker connecting xprt %p via %s to " |
| 2163 | "%s (port %s)\n", xprt, |
| 2164 | xprt->address_strings[RPC_DISPLAY_PROTO], |
| 2165 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2166 | xprt->address_strings[RPC_DISPLAY_PORT]); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2167 | |
| 2168 | status = xs_tcp_finish_connecting(xprt, sock); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2169 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 2170 | xprt, -status, xprt_connected(xprt), |
| 2171 | sock->sk->sk_state); |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 2172 | switch (status) { |
Trond Myklebust | f75e674 | 2009-04-21 17:18:20 -0400 | [diff] [blame] | 2173 | default: |
| 2174 | printk("%s: connect returned unhandled error %d\n", |
| 2175 | __func__, status); |
| 2176 | case -EADDRNOTAVAIL: |
| 2177 | /* We're probably in TIME_WAIT. Get rid of existing socket, |
| 2178 | * and retry |
| 2179 | */ |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 2180 | xs_tcp_force_close(xprt); |
Trond Myklebust | 88b5ed7 | 2009-06-17 13:22:57 -0700 | [diff] [blame] | 2181 | break; |
Trond Myklebust | 8a2cec2 | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 2182 | case -ECONNREFUSED: |
| 2183 | case -ECONNRESET: |
| 2184 | case -ENETUNREACH: |
| 2185 | /* retry with existing socket, after a delay */ |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 2186 | case 0: |
| 2187 | case -EINPROGRESS: |
| 2188 | case -EALREADY: |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2189 | xprt_clear_connecting(xprt); |
Jeff Layton | 5cf02d0 | 2012-07-23 13:58:51 -0400 | [diff] [blame] | 2190 | current->flags &= ~PF_FSTRANS; |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2191 | return; |
Trond Myklebust | 9fcfe0c | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 2192 | case -EINVAL: |
| 2193 | /* Happens, for instance, if the user specified a link |
| 2194 | * local IPv6 address without a scope-id. |
| 2195 | */ |
| 2196 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2197 | } |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2198 | out_eagain: |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 2199 | status = -EAGAIN; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2200 | out: |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 2201 | xprt_clear_connecting(xprt); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2202 | xprt_wake_pending_tasks(xprt, status); |
Jeff Layton | 5cf02d0 | 2012-07-23 13:58:51 -0400 | [diff] [blame] | 2203 | current->flags &= ~PF_FSTRANS; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2204 | } |
| 2205 | |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 2206 | /** |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2207 | * xs_connect - connect a socket to a remote endpoint |
| 2208 | * @task: address of RPC task that manages state of connect request |
| 2209 | * |
| 2210 | * TCP: If the remote end dropped the connection, delay reconnecting. |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2211 | * |
| 2212 | * UDP socket connects are synchronous, but we use a work queue anyway |
| 2213 | * to guarantee that even unprivileged user processes can set up a |
| 2214 | * socket on a privileged port. |
| 2215 | * |
| 2216 | * If a UDP socket connect fails, the delay behavior here prevents |
| 2217 | * retry floods (hard mounts). |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2218 | */ |
| 2219 | static void xs_connect(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2220 | { |
| 2221 | struct rpc_xprt *xprt = task->tk_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2222 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2223 | |
Chuck Lever | 09a21c4 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 2224 | if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2225 | dprintk("RPC: xs_connect delayed xprt %p for %lu " |
| 2226 | "seconds\n", |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2227 | xprt, xprt->reestablish_timeout / HZ); |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 2228 | queue_delayed_work(rpciod_workqueue, |
| 2229 | &transport->connect_worker, |
| 2230 | xprt->reestablish_timeout); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2231 | xprt->reestablish_timeout <<= 1; |
Neil Brown | 61d0a8e | 2009-09-23 14:36:37 -0400 | [diff] [blame] | 2232 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
| 2233 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2234 | if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) |
| 2235 | xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2236 | } else { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2237 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 2238 | queue_delayed_work(rpciod_workqueue, |
| 2239 | &transport->connect_worker, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2243 | /** |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2244 | * xs_local_print_stats - display AF_LOCAL socket-specifc stats |
| 2245 | * @xprt: rpc_xprt struct containing statistics |
| 2246 | * @seq: output file |
| 2247 | * |
| 2248 | */ |
| 2249 | static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 2250 | { |
| 2251 | long idle_time = 0; |
| 2252 | |
| 2253 | if (xprt_connected(xprt)) |
| 2254 | idle_time = (long)(jiffies - xprt->last_used) / HZ; |
| 2255 | |
| 2256 | seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2257 | "%llu %llu %lu %llu %llu\n", |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2258 | xprt->stat.bind_count, |
| 2259 | xprt->stat.connect_count, |
| 2260 | xprt->stat.connect_time, |
| 2261 | idle_time, |
| 2262 | xprt->stat.sends, |
| 2263 | xprt->stat.recvs, |
| 2264 | xprt->stat.bad_xids, |
| 2265 | xprt->stat.req_u, |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2266 | xprt->stat.bklog_u, |
| 2267 | xprt->stat.max_slots, |
| 2268 | xprt->stat.sending_u, |
| 2269 | xprt->stat.pending_u); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /** |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2273 | * xs_udp_print_stats - display UDP socket-specifc stats |
| 2274 | * @xprt: rpc_xprt struct containing statistics |
| 2275 | * @seq: output file |
| 2276 | * |
| 2277 | */ |
| 2278 | static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 2279 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2280 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 2281 | |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2282 | seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " |
| 2283 | "%lu %llu %llu\n", |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2284 | transport->srcport, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2285 | xprt->stat.bind_count, |
| 2286 | xprt->stat.sends, |
| 2287 | xprt->stat.recvs, |
| 2288 | xprt->stat.bad_xids, |
| 2289 | xprt->stat.req_u, |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2290 | xprt->stat.bklog_u, |
| 2291 | xprt->stat.max_slots, |
| 2292 | xprt->stat.sending_u, |
| 2293 | xprt->stat.pending_u); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | /** |
| 2297 | * xs_tcp_print_stats - display TCP socket-specifc stats |
| 2298 | * @xprt: rpc_xprt struct containing statistics |
| 2299 | * @seq: output file |
| 2300 | * |
| 2301 | */ |
| 2302 | static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 2303 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2304 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2305 | long idle_time = 0; |
| 2306 | |
| 2307 | if (xprt_connected(xprt)) |
| 2308 | idle_time = (long)(jiffies - xprt->last_used) / HZ; |
| 2309 | |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2310 | seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " |
| 2311 | "%llu %llu %lu %llu %llu\n", |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2312 | transport->srcport, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2313 | xprt->stat.bind_count, |
| 2314 | xprt->stat.connect_count, |
| 2315 | xprt->stat.connect_time, |
| 2316 | idle_time, |
| 2317 | xprt->stat.sends, |
| 2318 | xprt->stat.recvs, |
| 2319 | xprt->stat.bad_xids, |
| 2320 | xprt->stat.req_u, |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2321 | xprt->stat.bklog_u, |
| 2322 | xprt->stat.max_slots, |
| 2323 | xprt->stat.sending_u, |
| 2324 | xprt->stat.pending_u); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2325 | } |
| 2326 | |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2327 | /* |
| 2328 | * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason |
| 2329 | * we allocate pages instead doing a kmalloc like rpc_malloc is because we want |
| 2330 | * to use the server side send routines. |
| 2331 | */ |
H Hartley Sweeten | 5a51f13 | 2010-01-14 15:38:31 -0700 | [diff] [blame] | 2332 | static void *bc_malloc(struct rpc_task *task, size_t size) |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2333 | { |
| 2334 | struct page *page; |
| 2335 | struct rpc_buffer *buf; |
| 2336 | |
Weston Andros Adamson | b8a13d0 | 2012-10-23 10:43:43 -0400 | [diff] [blame] | 2337 | WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer)); |
| 2338 | if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) |
| 2339 | return NULL; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2340 | |
Weston Andros Adamson | b8a13d0 | 2012-10-23 10:43:43 -0400 | [diff] [blame] | 2341 | page = alloc_page(GFP_KERNEL); |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2342 | if (!page) |
| 2343 | return NULL; |
| 2344 | |
| 2345 | buf = page_address(page); |
| 2346 | buf->len = PAGE_SIZE; |
| 2347 | |
| 2348 | return buf->data; |
| 2349 | } |
| 2350 | |
| 2351 | /* |
| 2352 | * Free the space allocated in the bc_alloc routine |
| 2353 | */ |
H Hartley Sweeten | 5a51f13 | 2010-01-14 15:38:31 -0700 | [diff] [blame] | 2354 | static void bc_free(void *buffer) |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2355 | { |
| 2356 | struct rpc_buffer *buf; |
| 2357 | |
| 2358 | if (!buffer) |
| 2359 | return; |
| 2360 | |
| 2361 | buf = container_of(buffer, struct rpc_buffer, data); |
| 2362 | free_page((unsigned long)buf); |
| 2363 | } |
| 2364 | |
| 2365 | /* |
| 2366 | * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex |
| 2367 | * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. |
| 2368 | */ |
| 2369 | static int bc_sendto(struct rpc_rqst *req) |
| 2370 | { |
| 2371 | int len; |
| 2372 | struct xdr_buf *xbufp = &req->rq_snd_buf; |
| 2373 | struct rpc_xprt *xprt = req->rq_xprt; |
| 2374 | struct sock_xprt *transport = |
| 2375 | container_of(xprt, struct sock_xprt, xprt); |
| 2376 | struct socket *sock = transport->sock; |
| 2377 | unsigned long headoff; |
| 2378 | unsigned long tailoff; |
| 2379 | |
Chuck Lever | 61677ee | 2011-05-09 15:22:34 -0400 | [diff] [blame] | 2380 | xs_encode_stream_record_marker(xbufp); |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2381 | |
| 2382 | tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; |
| 2383 | headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; |
| 2384 | len = svc_send_common(sock, xbufp, |
| 2385 | virt_to_page(xbufp->head[0].iov_base), headoff, |
| 2386 | xbufp->tail[0].iov_base, tailoff); |
| 2387 | |
| 2388 | if (len != xbufp->len) { |
| 2389 | printk(KERN_NOTICE "Error sending entire callback!\n"); |
| 2390 | len = -EAGAIN; |
| 2391 | } |
| 2392 | |
| 2393 | return len; |
| 2394 | } |
| 2395 | |
| 2396 | /* |
| 2397 | * The send routine. Borrows from svc_send |
| 2398 | */ |
| 2399 | static int bc_send_request(struct rpc_task *task) |
| 2400 | { |
| 2401 | struct rpc_rqst *req = task->tk_rqstp; |
| 2402 | struct svc_xprt *xprt; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2403 | u32 len; |
| 2404 | |
| 2405 | dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); |
| 2406 | /* |
| 2407 | * Get the server socket associated with this callback xprt |
| 2408 | */ |
| 2409 | xprt = req->rq_xprt->bc_xprt; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2410 | |
| 2411 | /* |
| 2412 | * Grab the mutex to serialize data as the connection is shared |
| 2413 | * with the fore channel |
| 2414 | */ |
| 2415 | if (!mutex_trylock(&xprt->xpt_mutex)) { |
| 2416 | rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL); |
| 2417 | if (!mutex_trylock(&xprt->xpt_mutex)) |
| 2418 | return -EAGAIN; |
| 2419 | rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task); |
| 2420 | } |
| 2421 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) |
| 2422 | len = -ENOTCONN; |
| 2423 | else |
| 2424 | len = bc_sendto(req); |
| 2425 | mutex_unlock(&xprt->xpt_mutex); |
| 2426 | |
| 2427 | if (len > 0) |
| 2428 | len = 0; |
| 2429 | |
| 2430 | return len; |
| 2431 | } |
| 2432 | |
| 2433 | /* |
| 2434 | * The close routine. Since this is client initiated, we do nothing |
| 2435 | */ |
| 2436 | |
| 2437 | static void bc_close(struct rpc_xprt *xprt) |
| 2438 | { |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | /* |
| 2442 | * The xprt destroy routine. Again, because this connection is client |
| 2443 | * initiated, we do nothing |
| 2444 | */ |
| 2445 | |
| 2446 | static void bc_destroy(struct rpc_xprt *xprt) |
| 2447 | { |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2448 | } |
| 2449 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2450 | static struct rpc_xprt_ops xs_local_ops = { |
| 2451 | .reserve_xprt = xprt_reserve_xprt, |
| 2452 | .release_xprt = xs_tcp_release_xprt, |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 2453 | .alloc_slot = xprt_alloc_slot, |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2454 | .rpcbind = xs_local_rpcbind, |
| 2455 | .set_port = xs_local_set_port, |
| 2456 | .connect = xs_connect, |
| 2457 | .buf_alloc = rpc_malloc, |
| 2458 | .buf_free = rpc_free, |
| 2459 | .send_request = xs_local_send_request, |
| 2460 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
| 2461 | .close = xs_close, |
| 2462 | .destroy = xs_destroy, |
| 2463 | .print_stats = xs_local_print_stats, |
| 2464 | }; |
| 2465 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2466 | static struct rpc_xprt_ops xs_udp_ops = { |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2467 | .set_buffer_size = xs_udp_set_buffer_size, |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 2468 | .reserve_xprt = xprt_reserve_xprt_cong, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 2469 | .release_xprt = xprt_release_xprt_cong, |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 2470 | .alloc_slot = xprt_alloc_slot, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 2471 | .rpcbind = rpcb_getport_async, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 2472 | .set_port = xs_set_port, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2473 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 2474 | .buf_alloc = rpc_malloc, |
| 2475 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2476 | .send_request = xs_udp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 2477 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 2478 | .timer = xs_udp_timer, |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 2479 | .release_request = xprt_release_rqst_cong, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2480 | .close = xs_close, |
| 2481 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2482 | .print_stats = xs_udp_print_stats, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2483 | }; |
| 2484 | |
| 2485 | static struct rpc_xprt_ops xs_tcp_ops = { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 2486 | .reserve_xprt = xprt_reserve_xprt, |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 2487 | .release_xprt = xs_tcp_release_xprt, |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 2488 | .alloc_slot = xprt_lock_and_alloc_slot, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 2489 | .rpcbind = rpcb_getport_async, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 2490 | .set_port = xs_set_port, |
Trond Myklebust | 0b9e794 | 2010-04-16 16:41:57 -0400 | [diff] [blame] | 2491 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 2492 | .buf_alloc = rpc_malloc, |
| 2493 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2494 | .send_request = xs_tcp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 2495 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
Trond Myklebust | f75e674 | 2009-04-21 17:18:20 -0400 | [diff] [blame] | 2496 | .close = xs_tcp_close, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2497 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2498 | .print_stats = xs_tcp_print_stats, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2499 | }; |
| 2500 | |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2501 | /* |
| 2502 | * The rpc_xprt_ops for the server backchannel |
| 2503 | */ |
| 2504 | |
| 2505 | static struct rpc_xprt_ops bc_tcp_ops = { |
| 2506 | .reserve_xprt = xprt_reserve_xprt, |
| 2507 | .release_xprt = xprt_release_xprt, |
Bryan Schumaker | 84e28a3 | 2012-09-24 13:39:01 -0400 | [diff] [blame] | 2508 | .alloc_slot = xprt_alloc_slot, |
J. Bruce Fields | 9276910 | 2012-03-23 15:25:03 -0400 | [diff] [blame] | 2509 | .rpcbind = xs_local_rpcbind, |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2510 | .buf_alloc = bc_malloc, |
| 2511 | .buf_free = bc_free, |
| 2512 | .send_request = bc_send_request, |
| 2513 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
| 2514 | .close = bc_close, |
| 2515 | .destroy = bc_destroy, |
| 2516 | .print_stats = xs_tcp_print_stats, |
| 2517 | }; |
| 2518 | |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2519 | static int xs_init_anyaddr(const int family, struct sockaddr *sap) |
| 2520 | { |
| 2521 | static const struct sockaddr_in sin = { |
| 2522 | .sin_family = AF_INET, |
| 2523 | .sin_addr.s_addr = htonl(INADDR_ANY), |
| 2524 | }; |
| 2525 | static const struct sockaddr_in6 sin6 = { |
| 2526 | .sin6_family = AF_INET6, |
| 2527 | .sin6_addr = IN6ADDR_ANY_INIT, |
| 2528 | }; |
| 2529 | |
| 2530 | switch (family) { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2531 | case AF_LOCAL: |
| 2532 | break; |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2533 | case AF_INET: |
| 2534 | memcpy(sap, &sin, sizeof(sin)); |
| 2535 | break; |
| 2536 | case AF_INET6: |
| 2537 | memcpy(sap, &sin6, sizeof(sin6)); |
| 2538 | break; |
| 2539 | default: |
| 2540 | dprintk("RPC: %s: Bad address family\n", __func__); |
| 2541 | return -EAFNOSUPPORT; |
| 2542 | } |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
\"Talpey, Thomas\ | 3c341b0b | 2007-09-10 13:47:07 -0400 | [diff] [blame] | 2546 | static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2547 | unsigned int slot_table_size, |
| 2548 | unsigned int max_slot_table_size) |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2549 | { |
| 2550 | struct rpc_xprt *xprt; |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 2551 | struct sock_xprt *new; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2552 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2553 | if (args->addrlen > sizeof(xprt->addr)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2554 | dprintk("RPC: xs_setup_xprt: address too large\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2555 | return ERR_PTR(-EBADF); |
| 2556 | } |
| 2557 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2558 | xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, |
| 2559 | max_slot_table_size); |
Pavel Emelyanov | bd1722d | 2010-09-29 16:02:43 +0400 | [diff] [blame] | 2560 | if (xprt == NULL) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2561 | dprintk("RPC: xs_setup_xprt: couldn't allocate " |
| 2562 | "rpc_xprt\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2563 | return ERR_PTR(-ENOMEM); |
| 2564 | } |
| 2565 | |
Pavel Emelyanov | bd1722d | 2010-09-29 16:02:43 +0400 | [diff] [blame] | 2566 | new = container_of(xprt, struct sock_xprt, xprt); |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2567 | memcpy(&xprt->addr, args->dstaddr, args->addrlen); |
| 2568 | xprt->addrlen = args->addrlen; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 2569 | if (args->srcaddr) |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2570 | memcpy(&new->srcaddr, args->srcaddr, args->addrlen); |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2571 | else { |
| 2572 | int err; |
| 2573 | err = xs_init_anyaddr(args->dstaddr->sa_family, |
| 2574 | (struct sockaddr *)&new->srcaddr); |
Stanislav Kinsbursky | 2aa1353 | 2011-11-10 14:33:23 +0300 | [diff] [blame] | 2575 | if (err != 0) { |
| 2576 | xprt_free(xprt); |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2577 | return ERR_PTR(err); |
Stanislav Kinsbursky | 2aa1353 | 2011-11-10 14:33:23 +0300 | [diff] [blame] | 2578 | } |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2579 | } |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2580 | |
| 2581 | return xprt; |
| 2582 | } |
| 2583 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2584 | static const struct rpc_timeout xs_local_default_timeout = { |
| 2585 | .to_initval = 10 * HZ, |
| 2586 | .to_maxval = 10 * HZ, |
| 2587 | .to_retries = 2, |
| 2588 | }; |
| 2589 | |
| 2590 | /** |
| 2591 | * xs_setup_local - Set up transport to use an AF_LOCAL socket |
| 2592 | * @args: rpc transport creation arguments |
| 2593 | * |
| 2594 | * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP |
| 2595 | */ |
| 2596 | static struct rpc_xprt *xs_setup_local(struct xprt_create *args) |
| 2597 | { |
| 2598 | struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; |
| 2599 | struct sock_xprt *transport; |
| 2600 | struct rpc_xprt *xprt; |
| 2601 | struct rpc_xprt *ret; |
| 2602 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2603 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
| 2604 | xprt_max_tcp_slot_table_entries); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2605 | if (IS_ERR(xprt)) |
| 2606 | return xprt; |
| 2607 | transport = container_of(xprt, struct sock_xprt, xprt); |
| 2608 | |
| 2609 | xprt->prot = 0; |
| 2610 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
| 2611 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
| 2612 | |
| 2613 | xprt->bind_timeout = XS_BIND_TO; |
| 2614 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 2615 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
| 2616 | |
| 2617 | xprt->ops = &xs_local_ops; |
| 2618 | xprt->timeout = &xs_local_default_timeout; |
| 2619 | |
| 2620 | switch (sun->sun_family) { |
| 2621 | case AF_LOCAL: |
| 2622 | if (sun->sun_path[0] != '/') { |
| 2623 | dprintk("RPC: bad AF_LOCAL address: %s\n", |
| 2624 | sun->sun_path); |
| 2625 | ret = ERR_PTR(-EINVAL); |
| 2626 | goto out_err; |
| 2627 | } |
| 2628 | xprt_set_bound(xprt); |
| 2629 | INIT_DELAYED_WORK(&transport->connect_worker, |
| 2630 | xs_local_setup_socket); |
| 2631 | xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); |
| 2632 | break; |
| 2633 | default: |
| 2634 | ret = ERR_PTR(-EAFNOSUPPORT); |
| 2635 | goto out_err; |
| 2636 | } |
| 2637 | |
| 2638 | dprintk("RPC: set up xprt to %s via AF_LOCAL\n", |
| 2639 | xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 2640 | |
| 2641 | if (try_module_get(THIS_MODULE)) |
| 2642 | return xprt; |
| 2643 | ret = ERR_PTR(-EINVAL); |
| 2644 | out_err: |
| 2645 | xprt_free(xprt); |
| 2646 | return ret; |
| 2647 | } |
| 2648 | |
Trond Myklebust | 2881ae7 | 2007-12-20 16:03:54 -0500 | [diff] [blame] | 2649 | static const struct rpc_timeout xs_udp_default_timeout = { |
| 2650 | .to_initval = 5 * HZ, |
| 2651 | .to_maxval = 30 * HZ, |
| 2652 | .to_increment = 5 * HZ, |
| 2653 | .to_retries = 5, |
| 2654 | }; |
| 2655 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2656 | /** |
| 2657 | * xs_setup_udp - Set up transport to use a UDP socket |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2658 | * @args: rpc transport creation arguments |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2659 | * |
| 2660 | */ |
Adrian Bunk | 483066d | 2007-10-24 18:24:02 +0200 | [diff] [blame] | 2661 | static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2662 | { |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2663 | struct sockaddr *addr = args->dstaddr; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2664 | struct rpc_xprt *xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2665 | struct sock_xprt *transport; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2666 | struct rpc_xprt *ret; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2667 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2668 | xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, |
| 2669 | xprt_udp_slot_table_entries); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2670 | if (IS_ERR(xprt)) |
| 2671 | return xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2672 | transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2673 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 2674 | xprt->prot = IPPROTO_UDP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2675 | xprt->tsh_size = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2676 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
| 2677 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); |
| 2678 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2679 | xprt->bind_timeout = XS_BIND_TO; |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2680 | xprt->reestablish_timeout = XS_UDP_REEST_TO; |
| 2681 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2682 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2683 | xprt->ops = &xs_udp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2684 | |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 2685 | xprt->timeout = &xs_udp_default_timeout; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2686 | |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2687 | switch (addr->sa_family) { |
| 2688 | case AF_INET: |
| 2689 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) |
| 2690 | xprt_set_bound(xprt); |
| 2691 | |
| 2692 | INIT_DELAYED_WORK(&transport->connect_worker, |
Pavel Emelyanov | 8c14ff2 | 2010-10-04 16:58:02 +0400 | [diff] [blame] | 2693 | xs_udp_setup_socket); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2694 | xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2695 | break; |
| 2696 | case AF_INET6: |
| 2697 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) |
| 2698 | xprt_set_bound(xprt); |
| 2699 | |
| 2700 | INIT_DELAYED_WORK(&transport->connect_worker, |
Pavel Emelyanov | 8c14ff2 | 2010-10-04 16:58:02 +0400 | [diff] [blame] | 2701 | xs_udp_setup_socket); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2702 | xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2703 | break; |
| 2704 | default: |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2705 | ret = ERR_PTR(-EAFNOSUPPORT); |
| 2706 | goto out_err; |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2707 | } |
| 2708 | |
Chuck Lever | c740eff | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2709 | if (xprt_bound(xprt)) |
| 2710 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", |
| 2711 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2712 | xprt->address_strings[RPC_DISPLAY_PORT], |
| 2713 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
| 2714 | else |
| 2715 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", |
| 2716 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2717 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 2718 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2719 | if (try_module_get(THIS_MODULE)) |
| 2720 | return xprt; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2721 | ret = ERR_PTR(-EINVAL); |
| 2722 | out_err: |
Pavel Emelyanov | e204e62 | 2010-09-29 16:03:13 +0400 | [diff] [blame] | 2723 | xprt_free(xprt); |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2724 | return ret; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2725 | } |
| 2726 | |
Trond Myklebust | 2881ae7 | 2007-12-20 16:03:54 -0500 | [diff] [blame] | 2727 | static const struct rpc_timeout xs_tcp_default_timeout = { |
| 2728 | .to_initval = 60 * HZ, |
| 2729 | .to_maxval = 60 * HZ, |
| 2730 | .to_retries = 2, |
| 2731 | }; |
| 2732 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2733 | /** |
| 2734 | * xs_setup_tcp - Set up transport to use a TCP socket |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2735 | * @args: rpc transport creation arguments |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2736 | * |
| 2737 | */ |
Adrian Bunk | 483066d | 2007-10-24 18:24:02 +0200 | [diff] [blame] | 2738 | static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2739 | { |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2740 | struct sockaddr *addr = args->dstaddr; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2741 | struct rpc_xprt *xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2742 | struct sock_xprt *transport; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2743 | struct rpc_xprt *ret; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2744 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2745 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
| 2746 | xprt_max_tcp_slot_table_entries); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2747 | if (IS_ERR(xprt)) |
| 2748 | return xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2749 | transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2750 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 2751 | xprt->prot = IPPROTO_TCP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2752 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2753 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2754 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2755 | xprt->bind_timeout = XS_BIND_TO; |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2756 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 2757 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2758 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2759 | xprt->ops = &xs_tcp_ops; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 2760 | xprt->timeout = &xs_tcp_default_timeout; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2761 | |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2762 | switch (addr->sa_family) { |
| 2763 | case AF_INET: |
| 2764 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) |
| 2765 | xprt_set_bound(xprt); |
| 2766 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2767 | INIT_DELAYED_WORK(&transport->connect_worker, |
Pavel Emelyanov | cdd518d | 2010-10-04 16:57:40 +0400 | [diff] [blame] | 2768 | xs_tcp_setup_socket); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2769 | xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2770 | break; |
| 2771 | case AF_INET6: |
| 2772 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) |
| 2773 | xprt_set_bound(xprt); |
| 2774 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2775 | INIT_DELAYED_WORK(&transport->connect_worker, |
Pavel Emelyanov | cdd518d | 2010-10-04 16:57:40 +0400 | [diff] [blame] | 2776 | xs_tcp_setup_socket); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2777 | xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2778 | break; |
| 2779 | default: |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2780 | ret = ERR_PTR(-EAFNOSUPPORT); |
| 2781 | goto out_err; |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2782 | } |
| 2783 | |
Chuck Lever | c740eff | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2784 | if (xprt_bound(xprt)) |
| 2785 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", |
| 2786 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2787 | xprt->address_strings[RPC_DISPLAY_PORT], |
| 2788 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
| 2789 | else |
| 2790 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", |
| 2791 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2792 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
| 2793 | |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 2794 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2795 | if (try_module_get(THIS_MODULE)) |
| 2796 | return xprt; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2797 | ret = ERR_PTR(-EINVAL); |
| 2798 | out_err: |
Pavel Emelyanov | e204e62 | 2010-09-29 16:03:13 +0400 | [diff] [blame] | 2799 | xprt_free(xprt); |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2800 | return ret; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2801 | } |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2802 | |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2803 | /** |
| 2804 | * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket |
| 2805 | * @args: rpc transport creation arguments |
| 2806 | * |
| 2807 | */ |
| 2808 | static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) |
| 2809 | { |
| 2810 | struct sockaddr *addr = args->dstaddr; |
| 2811 | struct rpc_xprt *xprt; |
| 2812 | struct sock_xprt *transport; |
| 2813 | struct svc_sock *bc_sock; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2814 | struct rpc_xprt *ret; |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2815 | |
J. Bruce Fields | f0418aa | 2010-12-08 13:48:19 -0500 | [diff] [blame] | 2816 | if (args->bc_xprt->xpt_bc_xprt) { |
| 2817 | /* |
| 2818 | * This server connection already has a backchannel |
| 2819 | * export; we can't create a new one, as we wouldn't be |
| 2820 | * able to match replies based on xid any more. So, |
| 2821 | * reuse the already-existing one: |
| 2822 | */ |
| 2823 | return args->bc_xprt->xpt_bc_xprt; |
| 2824 | } |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2825 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
| 2826 | xprt_tcp_slot_table_entries); |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2827 | if (IS_ERR(xprt)) |
| 2828 | return xprt; |
| 2829 | transport = container_of(xprt, struct sock_xprt, xprt); |
| 2830 | |
| 2831 | xprt->prot = IPPROTO_TCP; |
| 2832 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
| 2833 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
| 2834 | xprt->timeout = &xs_tcp_default_timeout; |
| 2835 | |
| 2836 | /* backchannel */ |
| 2837 | xprt_set_bound(xprt); |
| 2838 | xprt->bind_timeout = 0; |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2839 | xprt->reestablish_timeout = 0; |
| 2840 | xprt->idle_timeout = 0; |
| 2841 | |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2842 | xprt->ops = &bc_tcp_ops; |
| 2843 | |
| 2844 | switch (addr->sa_family) { |
| 2845 | case AF_INET: |
| 2846 | xs_format_peer_addresses(xprt, "tcp", |
| 2847 | RPCBIND_NETID_TCP); |
| 2848 | break; |
| 2849 | case AF_INET6: |
| 2850 | xs_format_peer_addresses(xprt, "tcp", |
| 2851 | RPCBIND_NETID_TCP6); |
| 2852 | break; |
| 2853 | default: |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2854 | ret = ERR_PTR(-EAFNOSUPPORT); |
| 2855 | goto out_err; |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2856 | } |
| 2857 | |
Pavel Emelyanov | 50fa0d4 | 2010-10-05 20:49:35 +0400 | [diff] [blame] | 2858 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", |
| 2859 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2860 | xprt->address_strings[RPC_DISPLAY_PORT], |
| 2861 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2862 | |
| 2863 | /* |
J. Bruce Fields | 99de8ea | 2010-12-08 12:45:44 -0500 | [diff] [blame] | 2864 | * Once we've associated a backchannel xprt with a connection, |
| 2865 | * we want to keep it around as long as long as the connection |
| 2866 | * lasts, in case we need to start using it for a backchannel |
| 2867 | * again; this reference won't be dropped until bc_xprt is |
| 2868 | * destroyed. |
| 2869 | */ |
| 2870 | xprt_get(xprt); |
| 2871 | args->bc_xprt->xpt_bc_xprt = xprt; |
| 2872 | xprt->bc_xprt = args->bc_xprt; |
| 2873 | bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); |
| 2874 | transport->sock = bc_sock->sk_sock; |
| 2875 | transport->inet = bc_sock->sk_sk; |
| 2876 | |
| 2877 | /* |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2878 | * Since we don't want connections for the backchannel, we set |
| 2879 | * the xprt status to connected |
| 2880 | */ |
| 2881 | xprt_set_connected(xprt); |
| 2882 | |
| 2883 | |
| 2884 | if (try_module_get(THIS_MODULE)) |
| 2885 | return xprt; |
J. Bruce Fields | 99de8ea | 2010-12-08 12:45:44 -0500 | [diff] [blame] | 2886 | xprt_put(xprt); |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2887 | ret = ERR_PTR(-EINVAL); |
| 2888 | out_err: |
Pavel Emelyanov | e204e62 | 2010-09-29 16:03:13 +0400 | [diff] [blame] | 2889 | xprt_free(xprt); |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2890 | return ret; |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2891 | } |
| 2892 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2893 | static struct xprt_class xs_local_transport = { |
| 2894 | .list = LIST_HEAD_INIT(xs_local_transport.list), |
| 2895 | .name = "named UNIX socket", |
| 2896 | .owner = THIS_MODULE, |
| 2897 | .ident = XPRT_TRANSPORT_LOCAL, |
| 2898 | .setup = xs_setup_local, |
| 2899 | }; |
| 2900 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2901 | static struct xprt_class xs_udp_transport = { |
| 2902 | .list = LIST_HEAD_INIT(xs_udp_transport.list), |
| 2903 | .name = "udp", |
| 2904 | .owner = THIS_MODULE, |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2905 | .ident = XPRT_TRANSPORT_UDP, |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2906 | .setup = xs_setup_udp, |
| 2907 | }; |
| 2908 | |
| 2909 | static struct xprt_class xs_tcp_transport = { |
| 2910 | .list = LIST_HEAD_INIT(xs_tcp_transport.list), |
| 2911 | .name = "tcp", |
| 2912 | .owner = THIS_MODULE, |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2913 | .ident = XPRT_TRANSPORT_TCP, |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2914 | .setup = xs_setup_tcp, |
| 2915 | }; |
| 2916 | |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2917 | static struct xprt_class xs_bc_tcp_transport = { |
| 2918 | .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), |
| 2919 | .name = "tcp NFSv4.1 backchannel", |
| 2920 | .owner = THIS_MODULE, |
| 2921 | .ident = XPRT_TRANSPORT_BC_TCP, |
| 2922 | .setup = xs_setup_bc_tcp, |
| 2923 | }; |
| 2924 | |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2925 | /** |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2926 | * init_socket_xprt - set up xprtsock's sysctls, register with RPC client |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2927 | * |
| 2928 | */ |
| 2929 | int init_socket_xprt(void) |
| 2930 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 2931 | #ifdef RPC_DEBUG |
Eric W. Biederman | 2b1bec5 | 2007-02-14 00:33:24 -0800 | [diff] [blame] | 2932 | if (!sunrpc_table_header) |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 2933 | sunrpc_table_header = register_sysctl_table(sunrpc_table); |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 2934 | #endif |
| 2935 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2936 | xprt_register_transport(&xs_local_transport); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2937 | xprt_register_transport(&xs_udp_transport); |
| 2938 | xprt_register_transport(&xs_tcp_transport); |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2939 | xprt_register_transport(&xs_bc_tcp_transport); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2940 | |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2941 | return 0; |
| 2942 | } |
| 2943 | |
| 2944 | /** |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2945 | * cleanup_socket_xprt - remove xprtsock's sysctls, unregister |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2946 | * |
| 2947 | */ |
| 2948 | void cleanup_socket_xprt(void) |
| 2949 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 2950 | #ifdef RPC_DEBUG |
| 2951 | if (sunrpc_table_header) { |
| 2952 | unregister_sysctl_table(sunrpc_table_header); |
| 2953 | sunrpc_table_header = NULL; |
| 2954 | } |
| 2955 | #endif |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2956 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2957 | xprt_unregister_transport(&xs_local_transport); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2958 | xprt_unregister_transport(&xs_udp_transport); |
| 2959 | xprt_unregister_transport(&xs_tcp_transport); |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 2960 | xprt_unregister_transport(&xs_bc_tcp_transport); |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2961 | } |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 2962 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 2963 | static int param_set_uint_minmax(const char *val, |
| 2964 | const struct kernel_param *kp, |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 2965 | unsigned int min, unsigned int max) |
| 2966 | { |
| 2967 | unsigned long num; |
| 2968 | int ret; |
| 2969 | |
| 2970 | if (!val) |
| 2971 | return -EINVAL; |
| 2972 | ret = strict_strtoul(val, 0, &num); |
| 2973 | if (ret == -EINVAL || num < min || num > max) |
| 2974 | return -EINVAL; |
| 2975 | *((unsigned int *)kp->arg) = num; |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 2979 | static int param_set_portnr(const char *val, const struct kernel_param *kp) |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 2980 | { |
| 2981 | return param_set_uint_minmax(val, kp, |
| 2982 | RPC_MIN_RESVPORT, |
| 2983 | RPC_MAX_RESVPORT); |
| 2984 | } |
| 2985 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 2986 | static struct kernel_param_ops param_ops_portnr = { |
| 2987 | .set = param_set_portnr, |
| 2988 | .get = param_get_uint, |
| 2989 | }; |
| 2990 | |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 2991 | #define param_check_portnr(name, p) \ |
| 2992 | __param_check(name, p, unsigned int); |
| 2993 | |
| 2994 | module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); |
| 2995 | module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); |
| 2996 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 2997 | static int param_set_slot_table_size(const char *val, |
| 2998 | const struct kernel_param *kp) |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 2999 | { |
| 3000 | return param_set_uint_minmax(val, kp, |
| 3001 | RPC_MIN_SLOT_TABLE, |
| 3002 | RPC_MAX_SLOT_TABLE); |
| 3003 | } |
| 3004 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 3005 | static struct kernel_param_ops param_ops_slot_table_size = { |
| 3006 | .set = param_set_slot_table_size, |
| 3007 | .get = param_get_uint, |
| 3008 | }; |
| 3009 | |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3010 | #define param_check_slot_table_size(name, p) \ |
| 3011 | __param_check(name, p, unsigned int); |
| 3012 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 3013 | static int param_set_max_slot_table_size(const char *val, |
| 3014 | const struct kernel_param *kp) |
| 3015 | { |
| 3016 | return param_set_uint_minmax(val, kp, |
| 3017 | RPC_MIN_SLOT_TABLE, |
| 3018 | RPC_MAX_SLOT_TABLE_LIMIT); |
| 3019 | } |
| 3020 | |
| 3021 | static struct kernel_param_ops param_ops_max_slot_table_size = { |
| 3022 | .set = param_set_max_slot_table_size, |
| 3023 | .get = param_get_uint, |
| 3024 | }; |
| 3025 | |
| 3026 | #define param_check_max_slot_table_size(name, p) \ |
| 3027 | __param_check(name, p, unsigned int); |
| 3028 | |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3029 | module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, |
| 3030 | slot_table_size, 0644); |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 3031 | module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, |
| 3032 | max_slot_table_size, 0644); |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3033 | module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, |
| 3034 | slot_table_size, 0644); |
| 3035 | |