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