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