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