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