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> |
| 22 | #include <linux/slab.h> |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 23 | #include <linux/module.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 24 | #include <linux/capability.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/socket.h> |
| 28 | #include <linux/in.h> |
| 29 | #include <linux/net.h> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/udp.h> |
| 32 | #include <linux/tcp.h> |
| 33 | #include <linux/sunrpc/clnt.h> |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 34 | #include <linux/sunrpc/sched.h> |
\"Talpey, Thomas\ | 49c36fc | 2007-09-10 13:47:31 -0400 | [diff] [blame] | 35 | #include <linux/sunrpc/xprtsock.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 36 | #include <linux/file.h> |
| 37 | |
| 38 | #include <net/sock.h> |
| 39 | #include <net/checksum.h> |
| 40 | #include <net/udp.h> |
| 41 | #include <net/tcp.h> |
| 42 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 43 | /* |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 44 | * xprtsock tunables |
| 45 | */ |
| 46 | unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
| 47 | unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
| 48 | |
| 49 | unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; |
| 50 | unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; |
| 51 | |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 52 | #define XS_TCP_LINGER_TO (15U * HZ) |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame^] | 53 | 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] | 54 | |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 55 | /* |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 56 | * We can register our own files under /proc/sys/sunrpc by |
| 57 | * calling register_sysctl_table() again. The files in that |
| 58 | * directory become the union of all files registered there. |
| 59 | * |
| 60 | * We simply need to make sure that we don't collide with |
| 61 | * someone else's file names! |
| 62 | */ |
| 63 | |
| 64 | #ifdef RPC_DEBUG |
| 65 | |
| 66 | static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; |
| 67 | static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; |
| 68 | static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; |
| 69 | static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; |
| 70 | |
| 71 | static struct ctl_table_header *sunrpc_table_header; |
| 72 | |
| 73 | /* |
| 74 | * FIXME: changing the UDP slot table size should also resize the UDP |
| 75 | * socket buffers for existing UDP transports |
| 76 | */ |
| 77 | static ctl_table xs_tunables_table[] = { |
| 78 | { |
| 79 | .ctl_name = CTL_SLOTTABLE_UDP, |
| 80 | .procname = "udp_slot_table_entries", |
| 81 | .data = &xprt_udp_slot_table_entries, |
| 82 | .maxlen = sizeof(unsigned int), |
| 83 | .mode = 0644, |
| 84 | .proc_handler = &proc_dointvec_minmax, |
| 85 | .strategy = &sysctl_intvec, |
| 86 | .extra1 = &min_slot_table_size, |
| 87 | .extra2 = &max_slot_table_size |
| 88 | }, |
| 89 | { |
| 90 | .ctl_name = CTL_SLOTTABLE_TCP, |
| 91 | .procname = "tcp_slot_table_entries", |
| 92 | .data = &xprt_tcp_slot_table_entries, |
| 93 | .maxlen = sizeof(unsigned int), |
| 94 | .mode = 0644, |
| 95 | .proc_handler = &proc_dointvec_minmax, |
| 96 | .strategy = &sysctl_intvec, |
| 97 | .extra1 = &min_slot_table_size, |
| 98 | .extra2 = &max_slot_table_size |
| 99 | }, |
| 100 | { |
| 101 | .ctl_name = CTL_MIN_RESVPORT, |
| 102 | .procname = "min_resvport", |
| 103 | .data = &xprt_min_resvport, |
| 104 | .maxlen = sizeof(unsigned int), |
| 105 | .mode = 0644, |
| 106 | .proc_handler = &proc_dointvec_minmax, |
| 107 | .strategy = &sysctl_intvec, |
| 108 | .extra1 = &xprt_min_resvport_limit, |
| 109 | .extra2 = &xprt_max_resvport_limit |
| 110 | }, |
| 111 | { |
| 112 | .ctl_name = CTL_MAX_RESVPORT, |
| 113 | .procname = "max_resvport", |
| 114 | .data = &xprt_max_resvport, |
| 115 | .maxlen = sizeof(unsigned int), |
| 116 | .mode = 0644, |
| 117 | .proc_handler = &proc_dointvec_minmax, |
| 118 | .strategy = &sysctl_intvec, |
| 119 | .extra1 = &xprt_min_resvport_limit, |
| 120 | .extra2 = &xprt_max_resvport_limit |
| 121 | }, |
| 122 | { |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame^] | 123 | .procname = "tcp_fin_timeout", |
| 124 | .data = &xs_tcp_fin_timeout, |
| 125 | .maxlen = sizeof(xs_tcp_fin_timeout), |
| 126 | .mode = 0644, |
| 127 | .proc_handler = &proc_dointvec_jiffies, |
| 128 | .strategy = sysctl_jiffies |
| 129 | }, |
| 130 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 131 | .ctl_name = 0, |
| 132 | }, |
| 133 | }; |
| 134 | |
| 135 | static ctl_table sunrpc_table[] = { |
| 136 | { |
| 137 | .ctl_name = CTL_SUNRPC, |
| 138 | .procname = "sunrpc", |
| 139 | .mode = 0555, |
| 140 | .child = xs_tunables_table |
| 141 | }, |
| 142 | { |
| 143 | .ctl_name = 0, |
| 144 | }, |
| 145 | }; |
| 146 | |
| 147 | #endif |
| 148 | |
| 149 | /* |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 150 | * Time out for an RPC UDP socket connect. UDP socket connects are |
| 151 | * synchronous, but we set a timeout anyway in case of resource |
| 152 | * exhaustion on the local host. |
| 153 | */ |
| 154 | #define XS_UDP_CONN_TO (5U * HZ) |
| 155 | |
| 156 | /* |
| 157 | * Wait duration for an RPC TCP connection to be established. Solaris |
| 158 | * NFS over TCP uses 60 seconds, for example, which is in line with how |
| 159 | * long a server takes to reboot. |
| 160 | */ |
| 161 | #define XS_TCP_CONN_TO (60U * HZ) |
| 162 | |
| 163 | /* |
| 164 | * Wait duration for a reply from the RPC portmapper. |
| 165 | */ |
| 166 | #define XS_BIND_TO (60U * HZ) |
| 167 | |
| 168 | /* |
| 169 | * Delay if a UDP socket connect error occurs. This is most likely some |
| 170 | * kind of resource problem on the local host. |
| 171 | */ |
| 172 | #define XS_UDP_REEST_TO (2U * HZ) |
| 173 | |
| 174 | /* |
| 175 | * The reestablish timeout allows clients to delay for a bit before attempting |
| 176 | * to reconnect to a server that just dropped our connection. |
| 177 | * |
| 178 | * We implement an exponential backoff when trying to reestablish a TCP |
| 179 | * transport connection with the server. Some servers like to drop a TCP |
| 180 | * connection when they are overworked, so we start with a short timeout and |
| 181 | * increase over time if the server is down or not responding. |
| 182 | */ |
| 183 | #define XS_TCP_INIT_REEST_TO (3U * HZ) |
| 184 | #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) |
| 185 | |
| 186 | /* |
| 187 | * TCP idle timeout; client drops the transport socket if it is idle |
| 188 | * for this long. Note that we also timeout UDP sockets to prevent |
| 189 | * holding port numbers when there is no RPC traffic. |
| 190 | */ |
| 191 | #define XS_IDLE_DISC_TO (5U * 60 * HZ) |
| 192 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 193 | #ifdef RPC_DEBUG |
| 194 | # undef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 195 | # define RPCDBG_FACILITY RPCDBG_TRANS |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 196 | #endif |
| 197 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 198 | #ifdef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 199 | static void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 200 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 201 | u8 *buf = (u8 *) packet; |
| 202 | int j; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 203 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 204 | dprintk("RPC: %s\n", msg); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 205 | for (j = 0; j < count && j < 128; j += 4) { |
| 206 | if (!(j & 31)) { |
| 207 | if (j) |
| 208 | dprintk("\n"); |
| 209 | dprintk("0x%04x ", j); |
| 210 | } |
| 211 | dprintk("%02x%02x%02x%02x ", |
| 212 | buf[j], buf[j+1], buf[j+2], buf[j+3]); |
| 213 | } |
| 214 | dprintk("\n"); |
| 215 | } |
| 216 | #else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 217 | static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 218 | { |
| 219 | /* NOP */ |
| 220 | } |
| 221 | #endif |
| 222 | |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 223 | struct sock_xprt { |
| 224 | struct rpc_xprt xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * Network layer |
| 228 | */ |
| 229 | struct socket * sock; |
| 230 | struct sock * inet; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * State of TCP reply receive |
| 234 | */ |
| 235 | __be32 tcp_fraghdr, |
| 236 | tcp_xid; |
| 237 | |
| 238 | u32 tcp_offset, |
| 239 | tcp_reclen; |
| 240 | |
| 241 | unsigned long tcp_copied, |
| 242 | tcp_flags; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * Connection of transports |
| 246 | */ |
Trond Myklebust | 34161db | 2006-12-07 15:48:15 -0500 | [diff] [blame] | 247 | struct delayed_work connect_worker; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 248 | struct sockaddr_storage addr; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 249 | unsigned short port; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * UDP socket buffer size parameters |
| 253 | */ |
| 254 | size_t rcvsize, |
| 255 | sndsize; |
Chuck Lever | 314dfd7 | 2006-12-05 16:35:34 -0500 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | * Saved socket callback addresses |
| 259 | */ |
| 260 | void (*old_data_ready)(struct sock *, int); |
| 261 | void (*old_state_change)(struct sock *); |
| 262 | void (*old_write_space)(struct sock *); |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 263 | void (*old_error_report)(struct sock *); |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 264 | }; |
| 265 | |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 266 | /* |
| 267 | * TCP receive state flags |
| 268 | */ |
| 269 | #define TCP_RCV_LAST_FRAG (1UL << 0) |
| 270 | #define TCP_RCV_COPY_FRAGHDR (1UL << 1) |
| 271 | #define TCP_RCV_COPY_XID (1UL << 2) |
| 272 | #define TCP_RCV_COPY_DATA (1UL << 3) |
| 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 | |
| 279 | static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) |
| 280 | { |
| 281 | return (struct sockaddr_in *) &xprt->addr; |
| 282 | } |
| 283 | |
| 284 | static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) |
| 285 | { |
| 286 | return (struct sockaddr_in6 *) &xprt->addr; |
| 287 | } |
| 288 | |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 289 | static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt, |
| 290 | const char *protocol, |
| 291 | const char *netid) |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 292 | { |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 293 | struct sockaddr_in *addr = xs_addr_in(xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 294 | char *buf; |
| 295 | |
| 296 | buf = kzalloc(20, GFP_KERNEL); |
| 297 | if (buf) { |
David S. Miller | e0db4a7 | 2008-11-02 23:57:06 -0800 | [diff] [blame] | 298 | snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 299 | } |
| 300 | xprt->address_strings[RPC_DISPLAY_ADDR] = buf; |
| 301 | |
| 302 | buf = kzalloc(8, GFP_KERNEL); |
| 303 | if (buf) { |
| 304 | snprintf(buf, 8, "%u", |
| 305 | ntohs(addr->sin_port)); |
| 306 | } |
| 307 | xprt->address_strings[RPC_DISPLAY_PORT] = buf; |
| 308 | |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 309 | xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 310 | |
| 311 | buf = kzalloc(48, GFP_KERNEL); |
| 312 | if (buf) { |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 313 | snprintf(buf, 48, "addr=%pI4 port=%u proto=%s", |
| 314 | &addr->sin_addr.s_addr, |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 315 | ntohs(addr->sin_port), |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 316 | protocol); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 317 | } |
| 318 | xprt->address_strings[RPC_DISPLAY_ALL] = buf; |
Chuck Lever | fbfe3cc | 2007-08-06 11:57:02 -0400 | [diff] [blame] | 319 | |
| 320 | buf = kzalloc(10, GFP_KERNEL); |
| 321 | if (buf) { |
| 322 | snprintf(buf, 10, "%02x%02x%02x%02x", |
| 323 | NIPQUAD(addr->sin_addr.s_addr)); |
| 324 | } |
| 325 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; |
| 326 | |
| 327 | buf = kzalloc(8, GFP_KERNEL); |
| 328 | if (buf) { |
| 329 | snprintf(buf, 8, "%4hx", |
| 330 | ntohs(addr->sin_port)); |
| 331 | } |
| 332 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; |
Chuck Lever | 756805e | 2007-08-16 16:03:26 -0400 | [diff] [blame] | 333 | |
| 334 | buf = kzalloc(30, GFP_KERNEL); |
| 335 | if (buf) { |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 336 | snprintf(buf, 30, "%pI4.%u.%u", |
| 337 | &addr->sin_addr.s_addr, |
Chuck Lever | 756805e | 2007-08-16 16:03:26 -0400 | [diff] [blame] | 338 | ntohs(addr->sin_port) >> 8, |
| 339 | ntohs(addr->sin_port) & 0xff); |
| 340 | } |
| 341 | xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; |
\"Talpey, Thomas\ | 4417c8c | 2007-09-10 13:43:05 -0400 | [diff] [blame] | 342 | |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 343 | xprt->address_strings[RPC_DISPLAY_NETID] = netid; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 346 | static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt, |
| 347 | const char *protocol, |
| 348 | const char *netid) |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 349 | { |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 350 | struct sockaddr_in6 *addr = xs_addr_in6(xprt); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 351 | char *buf; |
| 352 | |
| 353 | buf = kzalloc(40, GFP_KERNEL); |
| 354 | if (buf) { |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 355 | snprintf(buf, 40, "%pI6",&addr->sin6_addr); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 356 | } |
| 357 | xprt->address_strings[RPC_DISPLAY_ADDR] = buf; |
| 358 | |
| 359 | buf = kzalloc(8, GFP_KERNEL); |
| 360 | if (buf) { |
| 361 | snprintf(buf, 8, "%u", |
| 362 | ntohs(addr->sin6_port)); |
| 363 | } |
| 364 | xprt->address_strings[RPC_DISPLAY_PORT] = buf; |
| 365 | |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 366 | xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 367 | |
| 368 | buf = kzalloc(64, GFP_KERNEL); |
| 369 | if (buf) { |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 370 | snprintf(buf, 64, "addr=%pI6 port=%u proto=%s", |
Harvey Harrison | fdb46ee | 2008-10-28 16:10:17 -0700 | [diff] [blame] | 371 | &addr->sin6_addr, |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 372 | ntohs(addr->sin6_port), |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 373 | protocol); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 374 | } |
| 375 | xprt->address_strings[RPC_DISPLAY_ALL] = buf; |
| 376 | |
| 377 | buf = kzalloc(36, GFP_KERNEL); |
Harvey Harrison | b071195 | 2008-10-28 16:05:40 -0700 | [diff] [blame] | 378 | if (buf) |
Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 379 | snprintf(buf, 36, "%pi6", &addr->sin6_addr); |
Harvey Harrison | b071195 | 2008-10-28 16:05:40 -0700 | [diff] [blame] | 380 | |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 381 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; |
| 382 | |
| 383 | buf = kzalloc(8, GFP_KERNEL); |
| 384 | if (buf) { |
| 385 | snprintf(buf, 8, "%4hx", |
| 386 | ntohs(addr->sin6_port)); |
| 387 | } |
| 388 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; |
Chuck Lever | 756805e | 2007-08-16 16:03:26 -0400 | [diff] [blame] | 389 | |
| 390 | buf = kzalloc(50, GFP_KERNEL); |
| 391 | if (buf) { |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 392 | snprintf(buf, 50, "%pI6.%u.%u", |
Harvey Harrison | fdb46ee | 2008-10-28 16:10:17 -0700 | [diff] [blame] | 393 | &addr->sin6_addr, |
| 394 | ntohs(addr->sin6_port) >> 8, |
| 395 | ntohs(addr->sin6_port) & 0xff); |
Chuck Lever | 756805e | 2007-08-16 16:03:26 -0400 | [diff] [blame] | 396 | } |
| 397 | xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; |
\"Talpey, Thomas\ | 4417c8c | 2007-09-10 13:43:05 -0400 | [diff] [blame] | 398 | |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 399 | xprt->address_strings[RPC_DISPLAY_NETID] = netid; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static void xs_free_peer_addresses(struct rpc_xprt *xprt) |
| 403 | { |
Chuck Lever | 33e01dc | 2008-01-14 12:32:20 -0500 | [diff] [blame] | 404 | unsigned int i; |
| 405 | |
| 406 | for (i = 0; i < RPC_DISPLAY_MAX; i++) |
| 407 | switch (i) { |
| 408 | case RPC_DISPLAY_PROTO: |
| 409 | case RPC_DISPLAY_NETID: |
| 410 | continue; |
| 411 | default: |
| 412 | kfree(xprt->address_strings[i]); |
| 413 | } |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 416 | #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) |
| 417 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 418 | 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] | 419 | { |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 420 | struct msghdr msg = { |
| 421 | .msg_name = addr, |
| 422 | .msg_namelen = addrlen, |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 423 | .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), |
| 424 | }; |
| 425 | struct kvec iov = { |
| 426 | .iov_base = vec->iov_base + base, |
| 427 | .iov_len = vec->iov_len - base, |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 428 | }; |
| 429 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 430 | if (iov.iov_len != 0) |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 431 | return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); |
| 432 | return kernel_sendmsg(sock, &msg, NULL, 0, 0); |
| 433 | } |
| 434 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 435 | 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] | 436 | { |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 437 | struct page **ppage; |
| 438 | unsigned int remainder; |
| 439 | int err, sent = 0; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 440 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 441 | remainder = xdr->page_len - base; |
| 442 | base += xdr->page_base; |
| 443 | ppage = xdr->pages + (base >> PAGE_SHIFT); |
| 444 | base &= ~PAGE_MASK; |
| 445 | for(;;) { |
| 446 | unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); |
| 447 | int flags = XS_SENDMSG_FLAGS; |
| 448 | |
| 449 | remainder -= len; |
| 450 | if (remainder != 0 || more) |
| 451 | flags |= MSG_MORE; |
| 452 | err = sock->ops->sendpage(sock, *ppage, base, len, flags); |
| 453 | if (remainder == 0 || err != len) |
| 454 | break; |
| 455 | sent += err; |
| 456 | ppage++; |
| 457 | base = 0; |
| 458 | } |
| 459 | if (sent == 0) |
| 460 | return err; |
| 461 | if (err > 0) |
| 462 | sent += err; |
| 463 | return sent; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 464 | } |
| 465 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 466 | /** |
| 467 | * xs_sendpages - write pages directly to a socket |
| 468 | * @sock: socket to send on |
| 469 | * @addr: UDP only -- address of destination |
| 470 | * @addrlen: UDP only -- length of destination address |
| 471 | * @xdr: buffer containing this request |
| 472 | * @base: starting position in the buffer |
| 473 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 474 | */ |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 475 | 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] | 476 | { |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 477 | unsigned int remainder = xdr->len - base; |
| 478 | int err, sent = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 479 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 480 | if (unlikely(!sock)) |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 481 | return -ENOTSOCK; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 482 | |
| 483 | clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 484 | if (base != 0) { |
| 485 | addr = NULL; |
| 486 | addrlen = 0; |
| 487 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 488 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 489 | if (base < xdr->head[0].iov_len || addr != NULL) { |
| 490 | unsigned int len = xdr->head[0].iov_len - base; |
| 491 | remainder -= len; |
| 492 | err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); |
| 493 | if (remainder == 0 || err != len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 494 | goto out; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 495 | sent += err; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 496 | base = 0; |
| 497 | } else |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 498 | base -= xdr->head[0].iov_len; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 499 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 500 | if (base < xdr->page_len) { |
| 501 | unsigned int len = xdr->page_len - base; |
| 502 | remainder -= len; |
| 503 | err = xs_send_pagedata(sock, xdr, base, remainder != 0); |
| 504 | if (remainder == 0 || err != len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 505 | goto out; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 506 | sent += err; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 507 | base = 0; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 508 | } else |
| 509 | base -= xdr->page_len; |
| 510 | |
| 511 | if (base >= xdr->tail[0].iov_len) |
| 512 | return sent; |
| 513 | err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 514 | out: |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 515 | if (sent == 0) |
| 516 | return err; |
| 517 | if (err > 0) |
| 518 | sent += err; |
| 519 | return sent; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 520 | } |
| 521 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 522 | static void xs_nospace_callback(struct rpc_task *task) |
| 523 | { |
| 524 | struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); |
| 525 | |
| 526 | transport->inet->sk_write_pending--; |
| 527 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
| 528 | } |
| 529 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 530 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 531 | * xs_nospace - place task on wait queue if transmit was incomplete |
| 532 | * @task: task to put to sleep |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 533 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 534 | */ |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 535 | static int xs_nospace(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 536 | { |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 537 | struct rpc_rqst *req = task->tk_rqstp; |
| 538 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 539 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 540 | int ret = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 541 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 542 | dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 543 | task->tk_pid, req->rq_slen - req->rq_bytes_sent, |
| 544 | req->rq_slen); |
| 545 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 546 | /* Protect against races with write_space */ |
| 547 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 548 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 549 | /* Don't race with disconnect */ |
| 550 | if (xprt_connected(xprt)) { |
| 551 | if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 552 | ret = -EAGAIN; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 553 | /* |
| 554 | * Notify TCP that we're limited by the application |
| 555 | * window size |
| 556 | */ |
| 557 | set_bit(SOCK_NOSPACE, &transport->sock->flags); |
| 558 | transport->inet->sk_write_pending++; |
| 559 | /* ...and wait for more buffer space */ |
| 560 | xprt_wait_for_buffer_space(task, xs_nospace_callback); |
| 561 | } |
| 562 | } else { |
| 563 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 564 | ret = -ENOTCONN; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 565 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 566 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 567 | spin_unlock_bh(&xprt->transport_lock); |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 568 | return ret; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
| 572 | * xs_udp_send_request - write an RPC request to a UDP socket |
| 573 | * @task: address of RPC task that manages the state of an RPC request |
| 574 | * |
| 575 | * Return values: |
| 576 | * 0: The request has been sent |
| 577 | * EAGAIN: The socket was blocked, please call again later to |
| 578 | * complete the request |
| 579 | * ENOTCONN: Caller needs to invoke connect logic then call again |
| 580 | * other: Some other error occured, the request was not sent |
| 581 | */ |
| 582 | static int xs_udp_send_request(struct rpc_task *task) |
| 583 | { |
| 584 | struct rpc_rqst *req = task->tk_rqstp; |
| 585 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 586 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 587 | struct xdr_buf *xdr = &req->rq_snd_buf; |
| 588 | int status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 589 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 590 | xs_pktdump("packet data:", |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 591 | req->rq_svec->iov_base, |
| 592 | req->rq_svec->iov_len); |
| 593 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 594 | if (!xprt_bound(xprt)) |
| 595 | return -ENOTCONN; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 596 | status = xs_sendpages(transport->sock, |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 597 | xs_addr(xprt), |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 598 | xprt->addrlen, xdr, |
| 599 | req->rq_bytes_sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 600 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 601 | dprintk("RPC: xs_udp_send_request(%u) = %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 602 | xdr->len - req->rq_bytes_sent, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 603 | |
Trond Myklebust | 2199700 | 2007-10-01 11:43:37 -0400 | [diff] [blame] | 604 | if (status >= 0) { |
| 605 | task->tk_bytes_sent += status; |
| 606 | if (status >= req->rq_slen) |
| 607 | return 0; |
| 608 | /* Still some bytes left; set up for a retry later. */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 609 | status = -EAGAIN; |
Trond Myklebust | 2199700 | 2007-10-01 11:43:37 -0400 | [diff] [blame] | 610 | } |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 611 | if (!transport->sock) |
| 612 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 613 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 614 | switch (status) { |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 615 | case -ENOTSOCK: |
| 616 | status = -ENOTCONN; |
| 617 | /* Should we call xs_close() here? */ |
| 618 | break; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 619 | case -EAGAIN: |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 620 | status = xs_nospace(task); |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 621 | break; |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 622 | default: |
| 623 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 624 | -status); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 625 | case -ENETUNREACH: |
| 626 | case -EPIPE: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 627 | case -ECONNREFUSED: |
| 628 | /* When the server has died, an ICMP port unreachable message |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 629 | * prompts ECONNREFUSED. */ |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 630 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 631 | } |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 632 | out: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 633 | return status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 634 | } |
| 635 | |
Trond Myklebust | e06799f | 2007-11-05 15:44:12 -0500 | [diff] [blame] | 636 | /** |
| 637 | * xs_tcp_shutdown - gracefully shut down a TCP socket |
| 638 | * @xprt: transport |
| 639 | * |
| 640 | * Initiates a graceful shutdown of the TCP socket by calling the |
| 641 | * equivalent of shutdown(SHUT_WR); |
| 642 | */ |
| 643 | static void xs_tcp_shutdown(struct rpc_xprt *xprt) |
| 644 | { |
| 645 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 646 | struct socket *sock = transport->sock; |
| 647 | |
| 648 | if (sock != NULL) |
| 649 | kernel_sock_shutdown(sock, SHUT_WR); |
| 650 | } |
| 651 | |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 652 | static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) |
| 653 | { |
| 654 | u32 reclen = buf->len - sizeof(rpc_fraghdr); |
| 655 | rpc_fraghdr *base = buf->head[0].iov_base; |
| 656 | *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); |
| 657 | } |
| 658 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 659 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 660 | * xs_tcp_send_request - write an RPC request to a TCP socket |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 661 | * @task: address of RPC task that manages the state of an RPC request |
| 662 | * |
| 663 | * Return values: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 664 | * 0: The request has been sent |
| 665 | * EAGAIN: The socket was blocked, please call again later to |
| 666 | * complete the request |
| 667 | * ENOTCONN: Caller needs to invoke connect logic then call again |
| 668 | * other: Some other error occured, the request was not sent |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 669 | * |
| 670 | * XXX: In the case of soft timeouts, should we eventually give up |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 671 | * if sendmsg is not able to make progress? |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 672 | */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 673 | static int xs_tcp_send_request(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 674 | { |
| 675 | struct rpc_rqst *req = task->tk_rqstp; |
| 676 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 677 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 678 | struct xdr_buf *xdr = &req->rq_snd_buf; |
Chuck Lever | b595bb1 | 2007-08-06 11:56:42 -0400 | [diff] [blame] | 679 | int status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 680 | |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 681 | xs_encode_tcp_record_marker(&req->rq_snd_buf); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 682 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 683 | xs_pktdump("packet data:", |
| 684 | req->rq_svec->iov_base, |
| 685 | req->rq_svec->iov_len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 686 | |
| 687 | /* Continue transmitting the packet/record. We must be careful |
| 688 | * to cope with writespace callbacks arriving _after_ we have |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 689 | * called sendmsg(). */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 690 | while (1) { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 691 | status = xs_sendpages(transport->sock, |
| 692 | NULL, 0, xdr, req->rq_bytes_sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 693 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 694 | dprintk("RPC: xs_tcp_send_request(%u) = %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 695 | xdr->len - req->rq_bytes_sent, status); |
| 696 | |
| 697 | if (unlikely(status < 0)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 698 | break; |
| 699 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 700 | /* If we've sent the entire packet, immediately |
| 701 | * reset the count of bytes sent. */ |
| 702 | req->rq_bytes_sent += status; |
Chuck Lever | ef759a2 | 2006-03-20 13:44:17 -0500 | [diff] [blame] | 703 | task->tk_bytes_sent += status; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 704 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { |
| 705 | req->rq_bytes_sent = 0; |
| 706 | return 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 707 | } |
| 708 | |
Trond Myklebust | 06b4b68 | 2008-04-16 16:51:38 -0400 | [diff] [blame] | 709 | if (status != 0) |
| 710 | continue; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 711 | status = -EAGAIN; |
Trond Myklebust | 06b4b68 | 2008-04-16 16:51:38 -0400 | [diff] [blame] | 712 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 713 | } |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 714 | if (!transport->sock) |
| 715 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 716 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 717 | switch (status) { |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 718 | case -ENOTSOCK: |
| 719 | status = -ENOTCONN; |
| 720 | /* Should we call xs_close() here? */ |
| 721 | break; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 722 | case -EAGAIN: |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 723 | status = xs_nospace(task); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 724 | break; |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 725 | default: |
| 726 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 727 | -status); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 728 | case -ECONNRESET: |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 729 | xs_tcp_shutdown(xprt); |
| 730 | case -ECONNREFUSED: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 731 | case -ENOTCONN: |
| 732 | case -EPIPE: |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 733 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 734 | } |
Trond Myklebust | c8485e4 | 2009-03-11 14:37:59 -0400 | [diff] [blame] | 735 | out: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 736 | return status; |
| 737 | } |
| 738 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 739 | /** |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 740 | * xs_tcp_release_xprt - clean up after a tcp transmission |
| 741 | * @xprt: transport |
| 742 | * @task: rpc task |
| 743 | * |
| 744 | * This cleans up if an error causes us to abort the transmission of a request. |
| 745 | * In this case, the socket may need to be reset in order to avoid confusing |
| 746 | * the server. |
| 747 | */ |
| 748 | static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
| 749 | { |
| 750 | struct rpc_rqst *req; |
| 751 | |
| 752 | if (task != xprt->snd_task) |
| 753 | return; |
| 754 | if (task == NULL) |
| 755 | goto out_release; |
| 756 | req = task->tk_rqstp; |
| 757 | if (req->rq_bytes_sent == 0) |
| 758 | goto out_release; |
| 759 | if (req->rq_bytes_sent == req->rq_snd_buf.len) |
| 760 | goto out_release; |
| 761 | set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); |
| 762 | out_release: |
| 763 | xprt_release_xprt(xprt, task); |
| 764 | } |
| 765 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 766 | static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) |
| 767 | { |
| 768 | transport->old_data_ready = sk->sk_data_ready; |
| 769 | transport->old_state_change = sk->sk_state_change; |
| 770 | transport->old_write_space = sk->sk_write_space; |
| 771 | transport->old_error_report = sk->sk_error_report; |
| 772 | } |
| 773 | |
| 774 | static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) |
| 775 | { |
| 776 | sk->sk_data_ready = transport->old_data_ready; |
| 777 | sk->sk_state_change = transport->old_state_change; |
| 778 | sk->sk_write_space = transport->old_write_space; |
| 779 | sk->sk_error_report = transport->old_error_report; |
| 780 | } |
| 781 | |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 782 | static void xs_reset_transport(struct sock_xprt *transport) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 783 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 784 | struct socket *sock = transport->sock; |
| 785 | struct sock *sk = transport->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 786 | |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 787 | if (sk == NULL) |
| 788 | return; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 789 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 790 | write_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 791 | transport->inet = NULL; |
| 792 | transport->sock = NULL; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 793 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 794 | sk->sk_user_data = NULL; |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 795 | |
| 796 | xs_restore_old_callbacks(transport, sk); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 797 | write_unlock_bh(&sk->sk_callback_lock); |
| 798 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 799 | sk->sk_no_check = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 800 | |
| 801 | sock_release(sock); |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /** |
| 805 | * xs_close - close a socket |
| 806 | * @xprt: transport |
| 807 | * |
| 808 | * This is used when all requests are complete; ie, no DRC state remains |
| 809 | * on the server we want to save. |
| 810 | */ |
| 811 | static void xs_close(struct rpc_xprt *xprt) |
| 812 | { |
| 813 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 814 | |
| 815 | dprintk("RPC: xs_close xprt %p\n", xprt); |
| 816 | |
| 817 | xs_reset_transport(transport); |
| 818 | |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 819 | smp_mb__before_clear_bit(); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 820 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 821 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 822 | clear_bit(XPRT_CLOSING, &xprt->state); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 823 | smp_mb__after_clear_bit(); |
Trond Myklebust | 62da3b2 | 2007-11-06 18:44:20 -0500 | [diff] [blame] | 824 | xprt_disconnect_done(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 825 | } |
| 826 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 827 | /** |
| 828 | * xs_destroy - prepare to shutdown a transport |
| 829 | * @xprt: doomed transport |
| 830 | * |
| 831 | */ |
| 832 | static void xs_destroy(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 833 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 834 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 835 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 836 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 837 | |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 838 | cancel_rearming_delayed_work(&transport->connect_worker); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 839 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 840 | xs_close(xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 841 | xs_free_peer_addresses(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 842 | kfree(xprt->slot); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 843 | kfree(xprt); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 844 | module_put(THIS_MODULE); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 845 | } |
| 846 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 847 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 848 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 849 | return (struct rpc_xprt *) sk->sk_user_data; |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * xs_udp_data_ready - "data ready" callback for UDP sockets |
| 854 | * @sk: socket with data to read |
| 855 | * @len: how much data to read |
| 856 | * |
| 857 | */ |
| 858 | static void xs_udp_data_ready(struct sock *sk, int len) |
| 859 | { |
| 860 | struct rpc_task *task; |
| 861 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 862 | struct rpc_rqst *rovr; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 863 | struct sk_buff *skb; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 864 | int err, repsize, copied; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 865 | u32 _xid; |
| 866 | __be32 *xp; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 867 | |
| 868 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 869 | dprintk("RPC: xs_udp_data_ready...\n"); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 870 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 871 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 872 | |
| 873 | if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) |
| 874 | goto out; |
| 875 | |
| 876 | if (xprt->shutdown) |
| 877 | goto dropit; |
| 878 | |
| 879 | repsize = skb->len - sizeof(struct udphdr); |
| 880 | if (repsize < 4) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 881 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 882 | goto dropit; |
| 883 | } |
| 884 | |
| 885 | /* Copy the XID from the skb... */ |
| 886 | xp = skb_header_pointer(skb, sizeof(struct udphdr), |
| 887 | sizeof(_xid), &_xid); |
| 888 | if (xp == NULL) |
| 889 | goto dropit; |
| 890 | |
| 891 | /* Look up and lock the request corresponding to the given XID */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 892 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 893 | rovr = xprt_lookup_rqst(xprt, *xp); |
| 894 | if (!rovr) |
| 895 | goto out_unlock; |
| 896 | task = rovr->rq_task; |
| 897 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 898 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
| 899 | copied = repsize; |
| 900 | |
| 901 | /* Suck it into the iovec, verify checksum if not done by hw. */ |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 902 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { |
| 903 | UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 904 | goto out_unlock; |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 908 | |
| 909 | /* Something worked... */ |
| 910 | dst_confirm(skb->dst); |
| 911 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 912 | xprt_adjust_cwnd(task, copied); |
| 913 | xprt_update_rtt(task); |
| 914 | xprt_complete_rqst(task, copied); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 915 | |
| 916 | out_unlock: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 917 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 918 | dropit: |
| 919 | skb_free_datagram(sk, skb); |
| 920 | out: |
| 921 | read_unlock(&sk->sk_callback_lock); |
| 922 | } |
| 923 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 924 | 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] | 925 | { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 926 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 927 | size_t len, used; |
| 928 | char *p; |
| 929 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 930 | p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; |
| 931 | len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 932 | used = xdr_skb_read_bits(desc, p, len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 933 | transport->tcp_offset += used; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 934 | if (used != len) |
| 935 | return; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 936 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 937 | transport->tcp_reclen = ntohl(transport->tcp_fraghdr); |
| 938 | if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 939 | transport->tcp_flags |= TCP_RCV_LAST_FRAG; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 940 | else |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 941 | transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 942 | transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 943 | |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 944 | transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 945 | transport->tcp_offset = 0; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 946 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 947 | /* Sanity check of the record length */ |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 948 | if (unlikely(transport->tcp_reclen < 4)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 949 | dprintk("RPC: invalid TCP record fragment length\n"); |
Trond Myklebust | 3ebb067 | 2007-11-06 18:40:12 -0500 | [diff] [blame] | 950 | xprt_force_disconnect(xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 951 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 952 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 953 | dprintk("RPC: reading TCP record fragment of length %d\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 954 | transport->tcp_reclen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 955 | } |
| 956 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 957 | static void xs_tcp_check_fraghdr(struct sock_xprt *transport) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 958 | { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 959 | if (transport->tcp_offset == transport->tcp_reclen) { |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 960 | transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 961 | transport->tcp_offset = 0; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 962 | if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { |
| 963 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
| 964 | transport->tcp_flags |= TCP_RCV_COPY_XID; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 965 | transport->tcp_copied = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 970 | 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] | 971 | { |
| 972 | size_t len, used; |
| 973 | char *p; |
| 974 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 975 | len = sizeof(transport->tcp_xid) - transport->tcp_offset; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 976 | dprintk("RPC: reading XID (%Zu bytes)\n", len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 977 | p = ((char *) &transport->tcp_xid) + transport->tcp_offset; |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 978 | used = xdr_skb_read_bits(desc, p, len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 979 | transport->tcp_offset += used; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 980 | if (used != len) |
| 981 | return; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 982 | transport->tcp_flags &= ~TCP_RCV_COPY_XID; |
| 983 | transport->tcp_flags |= TCP_RCV_COPY_DATA; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 984 | transport->tcp_copied = 4; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 985 | dprintk("RPC: reading reply for XID %08x\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 986 | ntohl(transport->tcp_xid)); |
| 987 | xs_tcp_check_fraghdr(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 988 | } |
| 989 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 990 | static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 991 | { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 992 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 993 | struct rpc_rqst *req; |
| 994 | struct xdr_buf *rcvbuf; |
| 995 | size_t len; |
| 996 | ssize_t r; |
| 997 | |
| 998 | /* Find and lock the request corresponding to this xid */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 999 | spin_lock(&xprt->transport_lock); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1000 | req = xprt_lookup_rqst(xprt, transport->tcp_xid); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1001 | if (!req) { |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1002 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1003 | dprintk("RPC: XID %08x request not found!\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1004 | ntohl(transport->tcp_xid)); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1005 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | rcvbuf = &req->rq_private_buf; |
| 1010 | len = desc->count; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1011 | if (len > transport->tcp_reclen - transport->tcp_offset) { |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 1012 | struct xdr_skb_reader my_desc; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1013 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1014 | len = transport->tcp_reclen - transport->tcp_offset; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1015 | memcpy(&my_desc, desc, sizeof(my_desc)); |
| 1016 | my_desc.count = len; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1017 | r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 1018 | &my_desc, xdr_skb_read_bits); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1019 | desc->count -= r; |
| 1020 | desc->offset += r; |
| 1021 | } else |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1022 | r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 1023 | desc, xdr_skb_read_bits); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1024 | |
| 1025 | if (r > 0) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1026 | transport->tcp_copied += r; |
| 1027 | transport->tcp_offset += r; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1028 | } |
| 1029 | if (r != len) { |
| 1030 | /* Error when copying to the receive buffer, |
| 1031 | * usually because we weren't able to allocate |
| 1032 | * additional buffer pages. All we can do now |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1033 | * is turn off TCP_RCV_COPY_DATA, so the request |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1034 | * will not receive any additional updates, |
| 1035 | * and time out. |
| 1036 | * Any remaining data from this record will |
| 1037 | * be discarded. |
| 1038 | */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1039 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1040 | dprintk("RPC: XID %08x truncated request\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1041 | ntohl(transport->tcp_xid)); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1042 | dprintk("RPC: xprt = %p, tcp_copied = %lu, " |
| 1043 | "tcp_offset = %u, tcp_reclen = %u\n", |
| 1044 | xprt, transport->tcp_copied, |
| 1045 | transport->tcp_offset, transport->tcp_reclen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1046 | goto out; |
| 1047 | } |
| 1048 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1049 | dprintk("RPC: XID %08x read %Zd bytes\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1050 | ntohl(transport->tcp_xid), r); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1051 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " |
| 1052 | "tcp_reclen = %u\n", xprt, transport->tcp_copied, |
| 1053 | transport->tcp_offset, transport->tcp_reclen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1054 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1055 | if (transport->tcp_copied == req->rq_private_buf.buflen) |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1056 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1057 | else if (transport->tcp_offset == transport->tcp_reclen) { |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1058 | if (transport->tcp_flags & TCP_RCV_LAST_FRAG) |
| 1059 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | out: |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1063 | if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1064 | xprt_complete_rqst(req->rq_task, transport->tcp_copied); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1065 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1066 | xs_tcp_check_fraghdr(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 1069 | 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] | 1070 | { |
| 1071 | size_t len; |
| 1072 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1073 | len = transport->tcp_reclen - transport->tcp_offset; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1074 | if (len > desc->count) |
| 1075 | len = desc->count; |
| 1076 | desc->count -= len; |
| 1077 | desc->offset += len; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1078 | transport->tcp_offset += len; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1079 | dprintk("RPC: discarded %Zu bytes\n", len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1080 | xs_tcp_check_fraghdr(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1081 | } |
| 1082 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1083 | 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] | 1084 | { |
| 1085 | struct rpc_xprt *xprt = rd_desc->arg.data; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1086 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 1087 | struct xdr_skb_reader desc = { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1088 | .skb = skb, |
| 1089 | .offset = offset, |
| 1090 | .count = len, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1091 | }; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1092 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1093 | dprintk("RPC: xs_tcp_data_recv started\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1094 | do { |
| 1095 | /* Read in a new fragment marker if necessary */ |
| 1096 | /* Can we ever really expect to get completely empty fragments? */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1097 | if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1098 | xs_tcp_read_fraghdr(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1099 | continue; |
| 1100 | } |
| 1101 | /* Read in the xid if necessary */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1102 | if (transport->tcp_flags & TCP_RCV_COPY_XID) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1103 | xs_tcp_read_xid(transport, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1104 | continue; |
| 1105 | } |
| 1106 | /* Read in the request data */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1107 | if (transport->tcp_flags & TCP_RCV_COPY_DATA) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1108 | xs_tcp_read_request(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1109 | continue; |
| 1110 | } |
| 1111 | /* Skip over any trailing bytes on short reads */ |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1112 | xs_tcp_read_discard(transport, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1113 | } while (desc.count); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1114 | dprintk("RPC: xs_tcp_data_recv done\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1115 | return len - desc.count; |
| 1116 | } |
| 1117 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1118 | /** |
| 1119 | * xs_tcp_data_ready - "data ready" callback for TCP sockets |
| 1120 | * @sk: socket with data to read |
| 1121 | * @bytes: how much data to read |
| 1122 | * |
| 1123 | */ |
| 1124 | static void xs_tcp_data_ready(struct sock *sk, int bytes) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1125 | { |
| 1126 | struct rpc_xprt *xprt; |
| 1127 | read_descriptor_t rd_desc; |
Trond Myklebust | ff2d7db | 2008-02-25 21:40:51 -0800 | [diff] [blame] | 1128 | int read; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1129 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1130 | dprintk("RPC: xs_tcp_data_ready...\n"); |
| 1131 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1132 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1133 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1134 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1135 | if (xprt->shutdown) |
| 1136 | goto out; |
| 1137 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1138 | /* 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] | 1139 | rd_desc.arg.data = xprt; |
Trond Myklebust | ff2d7db | 2008-02-25 21:40:51 -0800 | [diff] [blame] | 1140 | do { |
| 1141 | rd_desc.count = 65536; |
| 1142 | read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); |
| 1143 | } while (read > 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1144 | out: |
| 1145 | read_unlock(&sk->sk_callback_lock); |
| 1146 | } |
| 1147 | |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1148 | /* |
| 1149 | * Do the equivalent of linger/linger2 handling for dealing with |
| 1150 | * broken servers that don't close the socket in a timely |
| 1151 | * fashion |
| 1152 | */ |
| 1153 | static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt, |
| 1154 | unsigned long timeout) |
| 1155 | { |
| 1156 | struct sock_xprt *transport; |
| 1157 | |
| 1158 | if (xprt_test_and_set_connecting(xprt)) |
| 1159 | return; |
| 1160 | set_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
| 1161 | transport = container_of(xprt, struct sock_xprt, xprt); |
| 1162 | queue_delayed_work(rpciod_workqueue, &transport->connect_worker, |
| 1163 | timeout); |
| 1164 | } |
| 1165 | |
| 1166 | static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt) |
| 1167 | { |
| 1168 | struct sock_xprt *transport; |
| 1169 | |
| 1170 | transport = container_of(xprt, struct sock_xprt, xprt); |
| 1171 | |
| 1172 | if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) || |
| 1173 | !cancel_delayed_work(&transport->connect_worker)) |
| 1174 | return; |
| 1175 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
| 1176 | xprt_clear_connecting(xprt); |
| 1177 | } |
| 1178 | |
| 1179 | static void xs_sock_mark_closed(struct rpc_xprt *xprt) |
| 1180 | { |
| 1181 | smp_mb__before_clear_bit(); |
| 1182 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 1183 | clear_bit(XPRT_CLOSING, &xprt->state); |
| 1184 | smp_mb__after_clear_bit(); |
| 1185 | /* Mark transport as closed and wake up all pending tasks */ |
| 1186 | xprt_disconnect_done(xprt); |
| 1187 | } |
| 1188 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1189 | /** |
| 1190 | * xs_tcp_state_change - callback to handle TCP socket state changes |
| 1191 | * @sk: socket whose state has changed |
| 1192 | * |
| 1193 | */ |
| 1194 | static void xs_tcp_state_change(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1195 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1196 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1197 | |
| 1198 | read_lock(&sk->sk_callback_lock); |
| 1199 | if (!(xprt = xprt_from_sock(sk))) |
| 1200 | goto out; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1201 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
| 1202 | dprintk("RPC: state %x conn %d dead %d zapped %d\n", |
| 1203 | sk->sk_state, xprt_connected(xprt), |
| 1204 | sock_flag(sk, SOCK_DEAD), |
| 1205 | sock_flag(sk, SOCK_ZAPPED)); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1206 | |
| 1207 | switch (sk->sk_state) { |
| 1208 | case TCP_ESTABLISHED: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1209 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1210 | if (!xprt_test_and_set_connected(xprt)) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1211 | struct sock_xprt *transport = container_of(xprt, |
| 1212 | struct sock_xprt, xprt); |
| 1213 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1214 | /* Reset TCP record info */ |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1215 | transport->tcp_offset = 0; |
| 1216 | transport->tcp_reclen = 0; |
| 1217 | transport->tcp_copied = 0; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1218 | transport->tcp_flags = |
| 1219 | TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1220 | |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1221 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1222 | } |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1223 | spin_unlock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1224 | break; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1225 | case TCP_FIN_WAIT1: |
| 1226 | /* The client initiated a shutdown of the socket */ |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 1227 | xprt->connect_cookie++; |
Trond Myklebust | 663b885 | 2008-01-01 18:42:12 -0500 | [diff] [blame] | 1228 | xprt->reestablish_timeout = 0; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1229 | set_bit(XPRT_CLOSING, &xprt->state); |
| 1230 | smp_mb__before_clear_bit(); |
| 1231 | clear_bit(XPRT_CONNECTED, &xprt->state); |
Trond Myklebust | ef80367 | 2007-12-31 16:19:17 -0500 | [diff] [blame] | 1232 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1233 | smp_mb__after_clear_bit(); |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame^] | 1234 | xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1235 | break; |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 1236 | case TCP_CLOSE_WAIT: |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1237 | /* The server initiated a shutdown of the socket */ |
Trond Myklebust | 66af1e55 | 2007-11-06 10:18:36 -0500 | [diff] [blame] | 1238 | xprt_force_disconnect(xprt); |
Trond Myklebust | 663b885 | 2008-01-01 18:42:12 -0500 | [diff] [blame] | 1239 | case TCP_SYN_SENT: |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 1240 | xprt->connect_cookie++; |
Trond Myklebust | 663b885 | 2008-01-01 18:42:12 -0500 | [diff] [blame] | 1241 | case TCP_CLOSING: |
| 1242 | /* |
| 1243 | * If the server closed down the connection, make sure that |
| 1244 | * we back off before reconnecting |
| 1245 | */ |
| 1246 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
| 1247 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1248 | break; |
| 1249 | case TCP_LAST_ACK: |
Trond Myklebust | 670f945 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 1250 | set_bit(XPRT_CLOSING, &xprt->state); |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame^] | 1251 | xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1252 | smp_mb__before_clear_bit(); |
| 1253 | clear_bit(XPRT_CONNECTED, &xprt->state); |
| 1254 | smp_mb__after_clear_bit(); |
| 1255 | break; |
| 1256 | case TCP_CLOSE: |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1257 | xs_tcp_cancel_linger_timeout(xprt); |
| 1258 | xs_sock_mark_closed(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1259 | } |
| 1260 | out: |
| 1261 | read_unlock(&sk->sk_callback_lock); |
| 1262 | } |
| 1263 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1264 | /** |
Trond Myklebust | 482f32e | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1265 | * xs_error_report - callback mainly for catching socket errors |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1266 | * @sk: socket |
| 1267 | */ |
Trond Myklebust | 482f32e | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1268 | static void xs_error_report(struct sock *sk) |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1269 | { |
| 1270 | struct rpc_xprt *xprt; |
| 1271 | |
| 1272 | read_lock(&sk->sk_callback_lock); |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1273 | if (!(xprt = xprt_from_sock(sk))) |
| 1274 | goto out; |
| 1275 | dprintk("RPC: %s client %p...\n" |
| 1276 | "RPC: error %d\n", |
| 1277 | __func__, xprt, sk->sk_err); |
Trond Myklebust | 482f32e | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1278 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1279 | out: |
| 1280 | read_unlock(&sk->sk_callback_lock); |
| 1281 | } |
| 1282 | |
| 1283 | /** |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1284 | * xs_udp_write_space - callback invoked when socket buffer space |
| 1285 | * becomes available |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1286 | * @sk: socket whose state has changed |
| 1287 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1288 | * Called when more output buffer space is available for this socket. |
| 1289 | * We try not to wake our writers until they can make "significant" |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1290 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1291 | * with a bunch of small requests. |
| 1292 | */ |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1293 | static void xs_udp_write_space(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1294 | { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1295 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1296 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1297 | /* from net/core/sock.c:sock_def_write_space */ |
| 1298 | if (sock_writeable(sk)) { |
| 1299 | struct socket *sock; |
| 1300 | struct rpc_xprt *xprt; |
| 1301 | |
| 1302 | if (unlikely(!(sock = sk->sk_socket))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1303 | goto out; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 1304 | clear_bit(SOCK_NOSPACE, &sock->flags); |
| 1305 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1306 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1307 | goto out; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 1308 | if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1309 | goto out; |
| 1310 | |
| 1311 | xprt_write_space(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1312 | } |
| 1313 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1314 | out: |
| 1315 | read_unlock(&sk->sk_callback_lock); |
| 1316 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1317 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1318 | /** |
| 1319 | * xs_tcp_write_space - callback invoked when socket buffer space |
| 1320 | * becomes available |
| 1321 | * @sk: socket whose state has changed |
| 1322 | * |
| 1323 | * Called when more output buffer space is available for this socket. |
| 1324 | * We try not to wake our writers until they can make "significant" |
| 1325 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
| 1326 | * with a bunch of small requests. |
| 1327 | */ |
| 1328 | static void xs_tcp_write_space(struct sock *sk) |
| 1329 | { |
| 1330 | read_lock(&sk->sk_callback_lock); |
| 1331 | |
| 1332 | /* from net/core/stream.c:sk_stream_write_space */ |
| 1333 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
| 1334 | struct socket *sock; |
| 1335 | struct rpc_xprt *xprt; |
| 1336 | |
| 1337 | if (unlikely(!(sock = sk->sk_socket))) |
| 1338 | goto out; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 1339 | clear_bit(SOCK_NOSPACE, &sock->flags); |
| 1340 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1341 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
| 1342 | goto out; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 1343 | if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1344 | goto out; |
| 1345 | |
| 1346 | xprt_write_space(xprt); |
| 1347 | } |
| 1348 | |
| 1349 | out: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1350 | read_unlock(&sk->sk_callback_lock); |
| 1351 | } |
| 1352 | |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1353 | static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1354 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1355 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1356 | struct sock *sk = transport->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1357 | |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1358 | if (transport->rcvsize) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1359 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1360 | sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1361 | } |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1362 | if (transport->sndsize) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1363 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1364 | sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1365 | sk->sk_write_space(sk); |
| 1366 | } |
| 1367 | } |
| 1368 | |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1369 | /** |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1370 | * xs_udp_set_buffer_size - set send and receive limits |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1371 | * @xprt: generic transport |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1372 | * @sndsize: requested size of send buffer, in bytes |
| 1373 | * @rcvsize: requested size of receive buffer, in bytes |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1374 | * |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1375 | * Set socket send and receive buffer size limits. |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1376 | */ |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1377 | 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] | 1378 | { |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1379 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1380 | |
| 1381 | transport->sndsize = 0; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1382 | if (sndsize) |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1383 | transport->sndsize = sndsize + 1024; |
| 1384 | transport->rcvsize = 0; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1385 | if (rcvsize) |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1386 | transport->rcvsize = rcvsize + 1024; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1387 | |
| 1388 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1391 | /** |
| 1392 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport |
| 1393 | * @task: task that timed out |
| 1394 | * |
| 1395 | * Adjust the congestion window after a retransmit timeout has occurred. |
| 1396 | */ |
| 1397 | static void xs_udp_timer(struct rpc_task *task) |
| 1398 | { |
| 1399 | xprt_adjust_cwnd(task, -ETIMEDOUT); |
| 1400 | } |
| 1401 | |
Chuck Lever | b85d880 | 2006-05-25 01:40:49 -0400 | [diff] [blame] | 1402 | static unsigned short xs_get_random_port(void) |
| 1403 | { |
| 1404 | unsigned short range = xprt_max_resvport - xprt_min_resvport; |
| 1405 | unsigned short rand = (unsigned short) net_random() % range; |
| 1406 | return rand + xprt_min_resvport; |
| 1407 | } |
| 1408 | |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1409 | /** |
| 1410 | * xs_set_port - reset the port number in the remote endpoint address |
| 1411 | * @xprt: generic transport |
| 1412 | * @port: new port number |
| 1413 | * |
| 1414 | */ |
| 1415 | static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) |
| 1416 | { |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 1417 | struct sockaddr *addr = xs_addr(xprt); |
Chuck Lever | c4efcb1 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 1418 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1419 | dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); |
Chuck Lever | c4efcb1 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 1420 | |
Chuck Lever | 2061200 | 2007-08-06 11:57:23 -0400 | [diff] [blame] | 1421 | switch (addr->sa_family) { |
| 1422 | case AF_INET: |
| 1423 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 1424 | break; |
| 1425 | case AF_INET6: |
| 1426 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
| 1427 | break; |
| 1428 | default: |
| 1429 | BUG(); |
| 1430 | } |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1433 | static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock) |
| 1434 | { |
| 1435 | unsigned short port = transport->port; |
| 1436 | |
| 1437 | if (port == 0 && transport->xprt.resvport) |
| 1438 | port = xs_get_random_port(); |
| 1439 | return port; |
| 1440 | } |
| 1441 | |
| 1442 | static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port) |
| 1443 | { |
| 1444 | if (transport->port != 0) |
| 1445 | transport->port = 0; |
| 1446 | if (!transport->xprt.resvport) |
| 1447 | return 0; |
| 1448 | if (port <= xprt_min_resvport || port > xprt_max_resvport) |
| 1449 | return xprt_max_resvport; |
| 1450 | return --port; |
| 1451 | } |
| 1452 | |
Chuck Lever | 7dc753f | 2007-08-06 11:57:28 -0400 | [diff] [blame] | 1453 | static int xs_bind4(struct sock_xprt *transport, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1454 | { |
| 1455 | struct sockaddr_in myaddr = { |
| 1456 | .sin_family = AF_INET, |
| 1457 | }; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1458 | struct sockaddr_in *sa; |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1459 | int err, nloop = 0; |
| 1460 | unsigned short port = xs_get_srcport(transport, sock); |
| 1461 | unsigned short last; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1462 | |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1463 | sa = (struct sockaddr_in *)&transport->addr; |
| 1464 | myaddr.sin_addr = sa->sin_addr; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1465 | do { |
| 1466 | myaddr.sin_port = htons(port); |
Sridhar Samudrala | e6242e9 | 2006-08-07 20:58:01 -0700 | [diff] [blame] | 1467 | err = kernel_bind(sock, (struct sockaddr *) &myaddr, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1468 | sizeof(myaddr)); |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1469 | if (port == 0) |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1470 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1471 | if (err == 0) { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1472 | transport->port = port; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1473 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1474 | } |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1475 | last = port; |
| 1476 | port = xs_next_srcport(transport, sock, port); |
| 1477 | if (port > last) |
| 1478 | nloop++; |
| 1479 | } while (err == -EADDRINUSE && nloop != 2); |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 1480 | dprintk("RPC: %s %pI4:%u: %s (%d)\n", |
| 1481 | __func__, &myaddr.sin_addr, |
Chuck Lever | 7dc753f | 2007-08-06 11:57:28 -0400 | [diff] [blame] | 1482 | port, err ? "failed" : "ok", err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1483 | return err; |
| 1484 | } |
| 1485 | |
Chuck Lever | 90058d3 | 2007-08-06 11:57:33 -0400 | [diff] [blame] | 1486 | static int xs_bind6(struct sock_xprt *transport, struct socket *sock) |
| 1487 | { |
| 1488 | struct sockaddr_in6 myaddr = { |
| 1489 | .sin6_family = AF_INET6, |
| 1490 | }; |
| 1491 | struct sockaddr_in6 *sa; |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1492 | int err, nloop = 0; |
| 1493 | unsigned short port = xs_get_srcport(transport, sock); |
| 1494 | unsigned short last; |
Chuck Lever | 90058d3 | 2007-08-06 11:57:33 -0400 | [diff] [blame] | 1495 | |
Chuck Lever | 90058d3 | 2007-08-06 11:57:33 -0400 | [diff] [blame] | 1496 | sa = (struct sockaddr_in6 *)&transport->addr; |
| 1497 | myaddr.sin6_addr = sa->sin6_addr; |
| 1498 | do { |
| 1499 | myaddr.sin6_port = htons(port); |
| 1500 | err = kernel_bind(sock, (struct sockaddr *) &myaddr, |
| 1501 | sizeof(myaddr)); |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1502 | if (port == 0) |
Chuck Lever | 90058d3 | 2007-08-06 11:57:33 -0400 | [diff] [blame] | 1503 | break; |
| 1504 | if (err == 0) { |
| 1505 | transport->port = port; |
| 1506 | break; |
| 1507 | } |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1508 | last = port; |
| 1509 | port = xs_next_srcport(transport, sock, port); |
| 1510 | if (port > last) |
| 1511 | nloop++; |
| 1512 | } while (err == -EADDRINUSE && nloop != 2); |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 1513 | dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n", |
Harvey Harrison | fdb46ee | 2008-10-28 16:10:17 -0700 | [diff] [blame] | 1514 | &myaddr.sin6_addr, port, err ? "failed" : "ok", err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1515 | return err; |
| 1516 | } |
| 1517 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1518 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 1519 | static struct lock_class_key xs_key[2]; |
| 1520 | static struct lock_class_key xs_slock_key[2]; |
| 1521 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1522 | static inline void xs_reclassify_socket4(struct socket *sock) |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1523 | { |
| 1524 | struct sock *sk = sock->sk; |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1525 | |
John Heffner | 02b3d34 | 2007-09-12 10:42:12 +0200 | [diff] [blame] | 1526 | BUG_ON(sock_owned_by_user(sk)); |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1527 | sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", |
| 1528 | &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); |
| 1529 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1530 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1531 | static inline void xs_reclassify_socket6(struct socket *sock) |
| 1532 | { |
| 1533 | struct sock *sk = sock->sk; |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1534 | |
Linus Torvalds | f4921af | 2007-10-15 10:46:05 -0700 | [diff] [blame] | 1535 | BUG_ON(sock_owned_by_user(sk)); |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1536 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", |
| 1537 | &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1538 | } |
| 1539 | #else |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1540 | static inline void xs_reclassify_socket4(struct socket *sock) |
| 1541 | { |
| 1542 | } |
| 1543 | |
| 1544 | static inline void xs_reclassify_socket6(struct socket *sock) |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1545 | { |
| 1546 | } |
| 1547 | #endif |
| 1548 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1549 | 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] | 1550 | { |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1551 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 1552 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1553 | if (!transport->inet) { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1554 | struct sock *sk = sock->sk; |
| 1555 | |
| 1556 | write_lock_bh(&sk->sk_callback_lock); |
| 1557 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1558 | xs_save_old_callbacks(transport, sk); |
| 1559 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1560 | sk->sk_user_data = xprt; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1561 | sk->sk_data_ready = xs_udp_data_ready; |
| 1562 | sk->sk_write_space = xs_udp_write_space; |
Trond Myklebust | 482f32e | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1563 | sk->sk_error_report = xs_error_report; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1564 | sk->sk_no_check = UDP_CSUM_NORCV; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 1565 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1566 | |
| 1567 | xprt_set_connected(xprt); |
| 1568 | |
| 1569 | /* Reset to new socket */ |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1570 | transport->sock = sock; |
| 1571 | transport->inet = sk; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1572 | |
| 1573 | write_unlock_bh(&sk->sk_callback_lock); |
| 1574 | } |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1575 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1576 | } |
| 1577 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1578 | /** |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1579 | * xs_udp_connect_worker4 - set up a UDP socket |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1580 | * @work: RPC transport to connect |
| 1581 | * |
| 1582 | * Invoked by a work queue tasklet. |
| 1583 | */ |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1584 | static void xs_udp_connect_worker4(struct work_struct *work) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1585 | { |
| 1586 | struct sock_xprt *transport = |
| 1587 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1588 | struct rpc_xprt *xprt = &transport->xprt; |
| 1589 | struct socket *sock = transport->sock; |
| 1590 | int err, status = -EIO; |
| 1591 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 1592 | if (xprt->shutdown) |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1593 | goto out; |
| 1594 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1595 | /* Start by resetting any existing state */ |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 1596 | xs_reset_transport(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1597 | |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 1598 | err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); |
| 1599 | if (err < 0) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1600 | dprintk("RPC: can't create UDP transport socket (%d).\n", -err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1601 | goto out; |
| 1602 | } |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1603 | xs_reclassify_socket4(sock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1604 | |
Chuck Lever | 7dc753f | 2007-08-06 11:57:28 -0400 | [diff] [blame] | 1605 | if (xs_bind4(transport, sock)) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1606 | sock_release(sock); |
| 1607 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1608 | } |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1609 | |
| 1610 | dprintk("RPC: worker connecting xprt %p to address: %s\n", |
| 1611 | xprt, xprt->address_strings[RPC_DISPLAY_ALL]); |
| 1612 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1613 | xs_udp_finish_connecting(xprt, sock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1614 | status = 0; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1615 | out: |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1616 | xprt_clear_connecting(xprt); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1617 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1618 | } |
| 1619 | |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1620 | /** |
| 1621 | * xs_udp_connect_worker6 - set up a UDP socket |
| 1622 | * @work: RPC transport to connect |
| 1623 | * |
| 1624 | * Invoked by a work queue tasklet. |
| 1625 | */ |
| 1626 | static void xs_udp_connect_worker6(struct work_struct *work) |
| 1627 | { |
| 1628 | struct sock_xprt *transport = |
| 1629 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1630 | struct rpc_xprt *xprt = &transport->xprt; |
| 1631 | struct socket *sock = transport->sock; |
| 1632 | int err, status = -EIO; |
| 1633 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 1634 | if (xprt->shutdown) |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1635 | goto out; |
| 1636 | |
| 1637 | /* Start by resetting any existing state */ |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 1638 | xs_reset_transport(transport); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1639 | |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 1640 | err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock); |
| 1641 | if (err < 0) { |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1642 | dprintk("RPC: can't create UDP transport socket (%d).\n", -err); |
| 1643 | goto out; |
| 1644 | } |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1645 | xs_reclassify_socket6(sock); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1646 | |
| 1647 | if (xs_bind6(transport, sock) < 0) { |
| 1648 | sock_release(sock); |
| 1649 | goto out; |
| 1650 | } |
| 1651 | |
| 1652 | dprintk("RPC: worker connecting xprt %p to address: %s\n", |
| 1653 | xprt, xprt->address_strings[RPC_DISPLAY_ALL]); |
| 1654 | |
| 1655 | xs_udp_finish_connecting(xprt, sock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1656 | status = 0; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1657 | out: |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1658 | xprt_clear_connecting(xprt); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1659 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1660 | } |
| 1661 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1662 | /* |
| 1663 | * We need to preserve the port number so the reply cache on the server can |
| 1664 | * find our cached RPC replies when we get around to reconnecting. |
| 1665 | */ |
Trond Myklebust | 40d2549 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 1666 | static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1667 | { |
| 1668 | int result; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1669 | struct sockaddr any; |
| 1670 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1671 | dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1672 | |
| 1673 | /* |
| 1674 | * Disconnect the transport socket by doing a connect operation |
| 1675 | * with AF_UNSPEC. This should return immediately... |
| 1676 | */ |
| 1677 | memset(&any, 0, sizeof(any)); |
| 1678 | any.sa_family = AF_UNSPEC; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1679 | result = kernel_connect(transport->sock, &any, sizeof(any), 0); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1680 | if (!result) |
| 1681 | xs_sock_mark_closed(xprt); |
| 1682 | else |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1683 | dprintk("RPC: AF_UNSPEC connect return code %d\n", |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1684 | result); |
| 1685 | } |
| 1686 | |
Trond Myklebust | 40d2549 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 1687 | static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport) |
| 1688 | { |
| 1689 | unsigned int state = transport->inet->sk_state; |
| 1690 | |
| 1691 | if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) |
| 1692 | return; |
| 1693 | if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) |
| 1694 | return; |
| 1695 | xs_abort_connection(xprt, transport); |
| 1696 | } |
| 1697 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1698 | 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] | 1699 | { |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1700 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 1701 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1702 | if (!transport->inet) { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1703 | struct sock *sk = sock->sk; |
| 1704 | |
| 1705 | write_lock_bh(&sk->sk_callback_lock); |
| 1706 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1707 | xs_save_old_callbacks(transport, sk); |
| 1708 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1709 | sk->sk_user_data = xprt; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1710 | sk->sk_data_ready = xs_tcp_data_ready; |
| 1711 | sk->sk_state_change = xs_tcp_state_change; |
| 1712 | sk->sk_write_space = xs_tcp_write_space; |
Trond Myklebust | 482f32e | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1713 | sk->sk_error_report = xs_error_report; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 1714 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1715 | |
| 1716 | /* socket options */ |
| 1717 | sk->sk_userlocks |= SOCK_BINDPORT_LOCK; |
| 1718 | sock_reset_flag(sk, SOCK_LINGER); |
| 1719 | tcp_sk(sk)->linger2 = 0; |
| 1720 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1721 | |
| 1722 | xprt_clear_connected(xprt); |
| 1723 | |
| 1724 | /* Reset to new socket */ |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1725 | transport->sock = sock; |
| 1726 | transport->inet = sk; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1727 | |
| 1728 | write_unlock_bh(&sk->sk_callback_lock); |
| 1729 | } |
| 1730 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 1731 | if (!xprt_bound(xprt)) |
| 1732 | return -ENOTCONN; |
| 1733 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1734 | /* Tell the socket layer to start connecting... */ |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1735 | xprt->stat.connect_count++; |
| 1736 | xprt->stat.connect_start = jiffies; |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 1737 | return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | /** |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1741 | * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1742 | * @work: RPC transport to connect |
| 1743 | * |
| 1744 | * Invoked by a work queue tasklet. |
| 1745 | */ |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1746 | static void xs_tcp_connect_worker4(struct work_struct *work) |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1747 | { |
| 1748 | struct sock_xprt *transport = |
| 1749 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1750 | struct rpc_xprt *xprt = &transport->xprt; |
| 1751 | struct socket *sock = transport->sock; |
| 1752 | int err, status = -EIO; |
| 1753 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 1754 | if (xprt->shutdown) |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1755 | goto out; |
| 1756 | |
| 1757 | if (!sock) { |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1758 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1759 | /* start from scratch */ |
| 1760 | if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { |
| 1761 | dprintk("RPC: can't create TCP transport socket (%d).\n", -err); |
| 1762 | goto out; |
| 1763 | } |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1764 | xs_reclassify_socket4(sock); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1765 | |
| 1766 | if (xs_bind4(transport, sock) < 0) { |
| 1767 | sock_release(sock); |
| 1768 | goto out; |
| 1769 | } |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1770 | } else { |
| 1771 | int abort_and_exit; |
| 1772 | |
| 1773 | abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, |
| 1774 | &xprt->state); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1775 | /* "close" the socket, preserving the local port */ |
Trond Myklebust | 40d2549 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 1776 | xs_tcp_reuse_connection(xprt, transport); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1777 | |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1778 | if (abort_and_exit) |
| 1779 | goto out_eagain; |
| 1780 | } |
| 1781 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1782 | dprintk("RPC: worker connecting xprt %p to address: %s\n", |
| 1783 | xprt, xprt->address_strings[RPC_DISPLAY_ALL]); |
| 1784 | |
| 1785 | status = xs_tcp_finish_connecting(xprt, sock); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1786 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 1787 | xprt, -status, xprt_connected(xprt), |
| 1788 | sock->sk->sk_state); |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1789 | switch (status) { |
Trond Myklebust | 8a2cec2 | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 1790 | case -ECONNREFUSED: |
| 1791 | case -ECONNRESET: |
| 1792 | case -ENETUNREACH: |
| 1793 | /* retry with existing socket, after a delay */ |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1794 | case 0: |
| 1795 | case -EINPROGRESS: |
| 1796 | case -EALREADY: |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1797 | xprt_clear_connecting(xprt); |
| 1798 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1799 | } |
Trond Myklebust | 8a2cec2 | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 1800 | /* get rid of existing socket, and retry */ |
| 1801 | xs_tcp_shutdown(xprt); |
| 1802 | printk("%s: connect returned unhandled error %d\n", |
| 1803 | __func__, status); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1804 | out_eagain: |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1805 | status = -EAGAIN; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1806 | out: |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 1807 | xprt_clear_connecting(xprt); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1808 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1809 | } |
| 1810 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1811 | /** |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1812 | * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint |
| 1813 | * @work: RPC transport to connect |
| 1814 | * |
| 1815 | * Invoked by a work queue tasklet. |
| 1816 | */ |
| 1817 | static void xs_tcp_connect_worker6(struct work_struct *work) |
| 1818 | { |
| 1819 | struct sock_xprt *transport = |
| 1820 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1821 | struct rpc_xprt *xprt = &transport->xprt; |
| 1822 | struct socket *sock = transport->sock; |
| 1823 | int err, status = -EIO; |
| 1824 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 1825 | if (xprt->shutdown) |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1826 | goto out; |
| 1827 | |
| 1828 | if (!sock) { |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1829 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1830 | /* start from scratch */ |
| 1831 | if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { |
| 1832 | dprintk("RPC: can't create TCP transport socket (%d).\n", -err); |
| 1833 | goto out; |
| 1834 | } |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1835 | xs_reclassify_socket6(sock); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1836 | |
| 1837 | if (xs_bind6(transport, sock) < 0) { |
| 1838 | sock_release(sock); |
| 1839 | goto out; |
| 1840 | } |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1841 | } else { |
| 1842 | int abort_and_exit; |
| 1843 | |
| 1844 | abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, |
| 1845 | &xprt->state); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1846 | /* "close" the socket, preserving the local port */ |
Trond Myklebust | 40d2549 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 1847 | xs_tcp_reuse_connection(xprt, transport); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1848 | |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1849 | if (abort_and_exit) |
| 1850 | goto out_eagain; |
| 1851 | } |
| 1852 | |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1853 | dprintk("RPC: worker connecting xprt %p to address: %s\n", |
| 1854 | xprt, xprt->address_strings[RPC_DISPLAY_ALL]); |
| 1855 | |
| 1856 | status = xs_tcp_finish_connecting(xprt, sock); |
| 1857 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 1858 | xprt, -status, xprt_connected(xprt), sock->sk->sk_state); |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1859 | switch (status) { |
Trond Myklebust | 8a2cec2 | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 1860 | case -ECONNREFUSED: |
| 1861 | case -ECONNRESET: |
| 1862 | case -ENETUNREACH: |
| 1863 | /* retry with existing socket, after a delay */ |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1864 | case 0: |
| 1865 | case -EINPROGRESS: |
| 1866 | case -EALREADY: |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1867 | xprt_clear_connecting(xprt); |
| 1868 | return; |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1869 | } |
Trond Myklebust | 8a2cec2 | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 1870 | /* get rid of existing socket, and retry */ |
| 1871 | xs_tcp_shutdown(xprt); |
| 1872 | printk("%s: connect returned unhandled error %d\n", |
| 1873 | __func__, status); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1874 | out_eagain: |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1875 | status = -EAGAIN; |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1876 | out: |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1877 | xprt_clear_connecting(xprt); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 1878 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | /** |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1882 | * xs_connect - connect a socket to a remote endpoint |
| 1883 | * @task: address of RPC task that manages state of connect request |
| 1884 | * |
| 1885 | * TCP: If the remote end dropped the connection, delay reconnecting. |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1886 | * |
| 1887 | * UDP socket connects are synchronous, but we use a work queue anyway |
| 1888 | * to guarantee that even unprivileged user processes can set up a |
| 1889 | * socket on a privileged port. |
| 1890 | * |
| 1891 | * If a UDP socket connect fails, the delay behavior here prevents |
| 1892 | * retry floods (hard mounts). |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1893 | */ |
| 1894 | static void xs_connect(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1895 | { |
| 1896 | struct rpc_xprt *xprt = task->tk_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1897 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1898 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1899 | if (xprt_test_and_set_connecting(xprt)) |
| 1900 | return; |
| 1901 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1902 | if (transport->sock != NULL) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1903 | dprintk("RPC: xs_connect delayed xprt %p for %lu " |
| 1904 | "seconds\n", |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1905 | xprt, xprt->reestablish_timeout / HZ); |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 1906 | queue_delayed_work(rpciod_workqueue, |
| 1907 | &transport->connect_worker, |
| 1908 | xprt->reestablish_timeout); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1909 | xprt->reestablish_timeout <<= 1; |
| 1910 | if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) |
| 1911 | xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1912 | } else { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1913 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 1914 | queue_delayed_work(rpciod_workqueue, |
| 1915 | &transport->connect_worker, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
Trond Myklebust | e06799f | 2007-11-05 15:44:12 -0500 | [diff] [blame] | 1919 | static void xs_tcp_connect(struct rpc_task *task) |
| 1920 | { |
| 1921 | struct rpc_xprt *xprt = task->tk_xprt; |
| 1922 | |
Trond Myklebust | e06799f | 2007-11-05 15:44:12 -0500 | [diff] [blame] | 1923 | /* Exit if we need to wait for socket shutdown to complete */ |
| 1924 | if (test_bit(XPRT_CLOSING, &xprt->state)) |
| 1925 | return; |
| 1926 | xs_connect(task); |
| 1927 | } |
| 1928 | |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1929 | /** |
| 1930 | * xs_udp_print_stats - display UDP socket-specifc stats |
| 1931 | * @xprt: rpc_xprt struct containing statistics |
| 1932 | * @seq: output file |
| 1933 | * |
| 1934 | */ |
| 1935 | static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 1936 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1937 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1938 | |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1939 | seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1940 | transport->port, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1941 | xprt->stat.bind_count, |
| 1942 | xprt->stat.sends, |
| 1943 | xprt->stat.recvs, |
| 1944 | xprt->stat.bad_xids, |
| 1945 | xprt->stat.req_u, |
| 1946 | xprt->stat.bklog_u); |
| 1947 | } |
| 1948 | |
| 1949 | /** |
| 1950 | * xs_tcp_print_stats - display TCP socket-specifc stats |
| 1951 | * @xprt: rpc_xprt struct containing statistics |
| 1952 | * @seq: output file |
| 1953 | * |
| 1954 | */ |
| 1955 | static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 1956 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1957 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1958 | long idle_time = 0; |
| 1959 | |
| 1960 | if (xprt_connected(xprt)) |
| 1961 | idle_time = (long)(jiffies - xprt->last_used) / HZ; |
| 1962 | |
| 1963 | seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1964 | transport->port, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1965 | xprt->stat.bind_count, |
| 1966 | xprt->stat.connect_count, |
| 1967 | xprt->stat.connect_time, |
| 1968 | idle_time, |
| 1969 | xprt->stat.sends, |
| 1970 | xprt->stat.recvs, |
| 1971 | xprt->stat.bad_xids, |
| 1972 | xprt->stat.req_u, |
| 1973 | xprt->stat.bklog_u); |
| 1974 | } |
| 1975 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1976 | static struct rpc_xprt_ops xs_udp_ops = { |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1977 | .set_buffer_size = xs_udp_set_buffer_size, |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1978 | .reserve_xprt = xprt_reserve_xprt_cong, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1979 | .release_xprt = xprt_release_xprt_cong, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 1980 | .rpcbind = rpcb_getport_async, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1981 | .set_port = xs_set_port, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1982 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 1983 | .buf_alloc = rpc_malloc, |
| 1984 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1985 | .send_request = xs_udp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1986 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1987 | .timer = xs_udp_timer, |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 1988 | .release_request = xprt_release_rqst_cong, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1989 | .close = xs_close, |
| 1990 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1991 | .print_stats = xs_udp_print_stats, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1992 | }; |
| 1993 | |
| 1994 | static struct rpc_xprt_ops xs_tcp_ops = { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1995 | .reserve_xprt = xprt_reserve_xprt, |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1996 | .release_xprt = xs_tcp_release_xprt, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 1997 | .rpcbind = rpcb_getport_async, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1998 | .set_port = xs_set_port, |
Trond Myklebust | e06799f | 2007-11-05 15:44:12 -0500 | [diff] [blame] | 1999 | .connect = xs_tcp_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 2000 | .buf_alloc = rpc_malloc, |
| 2001 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2002 | .send_request = xs_tcp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 2003 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
Trond Myklebust | e06799f | 2007-11-05 15:44:12 -0500 | [diff] [blame] | 2004 | .close = xs_tcp_shutdown, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2005 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2006 | .print_stats = xs_tcp_print_stats, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2007 | }; |
| 2008 | |
\"Talpey, Thomas\ | 3c341b0b | 2007-09-10 13:47:07 -0400 | [diff] [blame] | 2009 | static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2010 | unsigned int slot_table_size) |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2011 | { |
| 2012 | struct rpc_xprt *xprt; |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 2013 | struct sock_xprt *new; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2014 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2015 | if (args->addrlen > sizeof(xprt->addr)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2016 | dprintk("RPC: xs_setup_xprt: address too large\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2017 | return ERR_PTR(-EBADF); |
| 2018 | } |
| 2019 | |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 2020 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 2021 | if (new == NULL) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2022 | dprintk("RPC: xs_setup_xprt: couldn't allocate " |
| 2023 | "rpc_xprt\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2024 | return ERR_PTR(-ENOMEM); |
| 2025 | } |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 2026 | xprt = &new->xprt; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2027 | |
| 2028 | xprt->max_reqs = slot_table_size; |
| 2029 | xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); |
| 2030 | if (xprt->slot == NULL) { |
| 2031 | kfree(xprt); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2032 | dprintk("RPC: xs_setup_xprt: couldn't allocate slot " |
| 2033 | "table\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2034 | return ERR_PTR(-ENOMEM); |
| 2035 | } |
| 2036 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2037 | memcpy(&xprt->addr, args->dstaddr, args->addrlen); |
| 2038 | xprt->addrlen = args->addrlen; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 2039 | if (args->srcaddr) |
| 2040 | memcpy(&new->addr, args->srcaddr, args->addrlen); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2041 | |
| 2042 | return xprt; |
| 2043 | } |
| 2044 | |
Trond Myklebust | 2881ae7 | 2007-12-20 16:03:54 -0500 | [diff] [blame] | 2045 | static const struct rpc_timeout xs_udp_default_timeout = { |
| 2046 | .to_initval = 5 * HZ, |
| 2047 | .to_maxval = 30 * HZ, |
| 2048 | .to_increment = 5 * HZ, |
| 2049 | .to_retries = 5, |
| 2050 | }; |
| 2051 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2052 | /** |
| 2053 | * xs_setup_udp - Set up transport to use a UDP socket |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2054 | * @args: rpc transport creation arguments |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2055 | * |
| 2056 | */ |
Adrian Bunk | 483066d | 2007-10-24 18:24:02 +0200 | [diff] [blame] | 2057 | static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2058 | { |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2059 | struct sockaddr *addr = args->dstaddr; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2060 | struct rpc_xprt *xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2061 | struct sock_xprt *transport; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2062 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2063 | xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2064 | if (IS_ERR(xprt)) |
| 2065 | return xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2066 | transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2067 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 2068 | xprt->prot = IPPROTO_UDP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2069 | xprt->tsh_size = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2070 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
| 2071 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); |
| 2072 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2073 | xprt->bind_timeout = XS_BIND_TO; |
| 2074 | xprt->connect_timeout = XS_UDP_CONN_TO; |
| 2075 | xprt->reestablish_timeout = XS_UDP_REEST_TO; |
| 2076 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2077 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2078 | xprt->ops = &xs_udp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2079 | |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 2080 | xprt->timeout = &xs_udp_default_timeout; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2081 | |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2082 | switch (addr->sa_family) { |
| 2083 | case AF_INET: |
| 2084 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) |
| 2085 | xprt_set_bound(xprt); |
| 2086 | |
| 2087 | INIT_DELAYED_WORK(&transport->connect_worker, |
| 2088 | xs_udp_connect_worker4); |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 2089 | xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2090 | break; |
| 2091 | case AF_INET6: |
| 2092 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) |
| 2093 | xprt_set_bound(xprt); |
| 2094 | |
| 2095 | INIT_DELAYED_WORK(&transport->connect_worker, |
| 2096 | xs_udp_connect_worker6); |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 2097 | xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2098 | break; |
| 2099 | default: |
| 2100 | kfree(xprt); |
| 2101 | return ERR_PTR(-EAFNOSUPPORT); |
| 2102 | } |
| 2103 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2104 | dprintk("RPC: set up transport to address %s\n", |
Chuck Lever | 7559c7a | 2006-12-05 16:35:37 -0500 | [diff] [blame] | 2105 | xprt->address_strings[RPC_DISPLAY_ALL]); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 2106 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2107 | if (try_module_get(THIS_MODULE)) |
| 2108 | return xprt; |
| 2109 | |
| 2110 | kfree(xprt->slot); |
| 2111 | kfree(xprt); |
| 2112 | return ERR_PTR(-EINVAL); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2113 | } |
| 2114 | |
Trond Myklebust | 2881ae7 | 2007-12-20 16:03:54 -0500 | [diff] [blame] | 2115 | static const struct rpc_timeout xs_tcp_default_timeout = { |
| 2116 | .to_initval = 60 * HZ, |
| 2117 | .to_maxval = 60 * HZ, |
| 2118 | .to_retries = 2, |
| 2119 | }; |
| 2120 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2121 | /** |
| 2122 | * xs_setup_tcp - Set up transport to use a TCP socket |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2123 | * @args: rpc transport creation arguments |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2124 | * |
| 2125 | */ |
Adrian Bunk | 483066d | 2007-10-24 18:24:02 +0200 | [diff] [blame] | 2126 | static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2127 | { |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2128 | struct sockaddr *addr = args->dstaddr; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2129 | struct rpc_xprt *xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2130 | struct sock_xprt *transport; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2131 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2132 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2133 | if (IS_ERR(xprt)) |
| 2134 | return xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2135 | transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2136 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 2137 | xprt->prot = IPPROTO_TCP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2138 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2139 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2140 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2141 | xprt->bind_timeout = XS_BIND_TO; |
| 2142 | xprt->connect_timeout = XS_TCP_CONN_TO; |
| 2143 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 2144 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2145 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2146 | xprt->ops = &xs_tcp_ops; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 2147 | xprt->timeout = &xs_tcp_default_timeout; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2148 | |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2149 | switch (addr->sa_family) { |
| 2150 | case AF_INET: |
| 2151 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) |
| 2152 | xprt_set_bound(xprt); |
| 2153 | |
| 2154 | INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4); |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 2155 | xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2156 | break; |
| 2157 | case AF_INET6: |
| 2158 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) |
| 2159 | xprt_set_bound(xprt); |
| 2160 | |
| 2161 | INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6); |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 2162 | xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2163 | break; |
| 2164 | default: |
| 2165 | kfree(xprt); |
| 2166 | return ERR_PTR(-EAFNOSUPPORT); |
| 2167 | } |
| 2168 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2169 | dprintk("RPC: set up transport to address %s\n", |
Chuck Lever | 7559c7a | 2006-12-05 16:35:37 -0500 | [diff] [blame] | 2170 | xprt->address_strings[RPC_DISPLAY_ALL]); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 2171 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2172 | if (try_module_get(THIS_MODULE)) |
| 2173 | return xprt; |
| 2174 | |
| 2175 | kfree(xprt->slot); |
| 2176 | kfree(xprt); |
| 2177 | return ERR_PTR(-EINVAL); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2178 | } |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2179 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2180 | static struct xprt_class xs_udp_transport = { |
| 2181 | .list = LIST_HEAD_INIT(xs_udp_transport.list), |
| 2182 | .name = "udp", |
| 2183 | .owner = THIS_MODULE, |
\"Talpey, Thomas\ | 4fa016e | 2007-09-10 13:47:57 -0400 | [diff] [blame] | 2184 | .ident = IPPROTO_UDP, |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2185 | .setup = xs_setup_udp, |
| 2186 | }; |
| 2187 | |
| 2188 | static struct xprt_class xs_tcp_transport = { |
| 2189 | .list = LIST_HEAD_INIT(xs_tcp_transport.list), |
| 2190 | .name = "tcp", |
| 2191 | .owner = THIS_MODULE, |
\"Talpey, Thomas\ | 4fa016e | 2007-09-10 13:47:57 -0400 | [diff] [blame] | 2192 | .ident = IPPROTO_TCP, |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2193 | .setup = xs_setup_tcp, |
| 2194 | }; |
| 2195 | |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2196 | /** |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2197 | * init_socket_xprt - set up xprtsock's sysctls, register with RPC client |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2198 | * |
| 2199 | */ |
| 2200 | int init_socket_xprt(void) |
| 2201 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 2202 | #ifdef RPC_DEBUG |
Eric W. Biederman | 2b1bec5 | 2007-02-14 00:33:24 -0800 | [diff] [blame] | 2203 | if (!sunrpc_table_header) |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 2204 | sunrpc_table_header = register_sysctl_table(sunrpc_table); |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 2205 | #endif |
| 2206 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2207 | xprt_register_transport(&xs_udp_transport); |
| 2208 | xprt_register_transport(&xs_tcp_transport); |
| 2209 | |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2210 | return 0; |
| 2211 | } |
| 2212 | |
| 2213 | /** |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2214 | * cleanup_socket_xprt - remove xprtsock's sysctls, unregister |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2215 | * |
| 2216 | */ |
| 2217 | void cleanup_socket_xprt(void) |
| 2218 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 2219 | #ifdef RPC_DEBUG |
| 2220 | if (sunrpc_table_header) { |
| 2221 | unregister_sysctl_table(sunrpc_table_header); |
| 2222 | sunrpc_table_header = NULL; |
| 2223 | } |
| 2224 | #endif |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2225 | |
| 2226 | xprt_unregister_transport(&xs_udp_transport); |
| 2227 | xprt_unregister_transport(&xs_tcp_transport); |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 2228 | } |