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 | * |
| 6 | * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com> |
| 7 | * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com> |
| 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 | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/capability.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/socket.h> |
| 25 | #include <linux/in.h> |
| 26 | #include <linux/net.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/udp.h> |
| 29 | #include <linux/tcp.h> |
| 30 | #include <linux/sunrpc/clnt.h> |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 31 | #include <linux/sunrpc/sched.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 32 | #include <linux/file.h> |
| 33 | |
| 34 | #include <net/sock.h> |
| 35 | #include <net/checksum.h> |
| 36 | #include <net/udp.h> |
| 37 | #include <net/tcp.h> |
| 38 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 39 | /* |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 40 | * xprtsock tunables |
| 41 | */ |
| 42 | unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
| 43 | unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
| 44 | |
| 45 | unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; |
| 46 | unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; |
| 47 | |
| 48 | /* |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 49 | * How many times to try sending a request on a socket before waiting |
| 50 | * for the socket buffer to clear. |
| 51 | */ |
| 52 | #define XS_SENDMSG_RETRY (10U) |
| 53 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 54 | /* |
| 55 | * Time out for an RPC UDP socket connect. UDP socket connects are |
| 56 | * synchronous, but we set a timeout anyway in case of resource |
| 57 | * exhaustion on the local host. |
| 58 | */ |
| 59 | #define XS_UDP_CONN_TO (5U * HZ) |
| 60 | |
| 61 | /* |
| 62 | * Wait duration for an RPC TCP connection to be established. Solaris |
| 63 | * NFS over TCP uses 60 seconds, for example, which is in line with how |
| 64 | * long a server takes to reboot. |
| 65 | */ |
| 66 | #define XS_TCP_CONN_TO (60U * HZ) |
| 67 | |
| 68 | /* |
| 69 | * Wait duration for a reply from the RPC portmapper. |
| 70 | */ |
| 71 | #define XS_BIND_TO (60U * HZ) |
| 72 | |
| 73 | /* |
| 74 | * Delay if a UDP socket connect error occurs. This is most likely some |
| 75 | * kind of resource problem on the local host. |
| 76 | */ |
| 77 | #define XS_UDP_REEST_TO (2U * HZ) |
| 78 | |
| 79 | /* |
| 80 | * The reestablish timeout allows clients to delay for a bit before attempting |
| 81 | * to reconnect to a server that just dropped our connection. |
| 82 | * |
| 83 | * We implement an exponential backoff when trying to reestablish a TCP |
| 84 | * transport connection with the server. Some servers like to drop a TCP |
| 85 | * connection when they are overworked, so we start with a short timeout and |
| 86 | * increase over time if the server is down or not responding. |
| 87 | */ |
| 88 | #define XS_TCP_INIT_REEST_TO (3U * HZ) |
| 89 | #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) |
| 90 | |
| 91 | /* |
| 92 | * TCP idle timeout; client drops the transport socket if it is idle |
| 93 | * for this long. Note that we also timeout UDP sockets to prevent |
| 94 | * holding port numbers when there is no RPC traffic. |
| 95 | */ |
| 96 | #define XS_IDLE_DISC_TO (5U * 60 * HZ) |
| 97 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 98 | #ifdef RPC_DEBUG |
| 99 | # undef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 100 | # define RPCDBG_FACILITY RPCDBG_TRANS |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 101 | #endif |
| 102 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 103 | #ifdef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 104 | static void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 105 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 106 | u8 *buf = (u8 *) packet; |
| 107 | int j; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 108 | |
| 109 | dprintk("RPC: %s\n", msg); |
| 110 | for (j = 0; j < count && j < 128; j += 4) { |
| 111 | if (!(j & 31)) { |
| 112 | if (j) |
| 113 | dprintk("\n"); |
| 114 | dprintk("0x%04x ", j); |
| 115 | } |
| 116 | dprintk("%02x%02x%02x%02x ", |
| 117 | buf[j], buf[j+1], buf[j+2], buf[j+3]); |
| 118 | } |
| 119 | dprintk("\n"); |
| 120 | } |
| 121 | #else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 122 | static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 123 | { |
| 124 | /* NOP */ |
| 125 | } |
| 126 | #endif |
| 127 | |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 128 | #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) |
| 129 | |
| 130 | static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len) |
| 131 | { |
| 132 | struct kvec iov = { |
| 133 | .iov_base = xdr->head[0].iov_base + base, |
| 134 | .iov_len = len - base, |
| 135 | }; |
| 136 | struct msghdr msg = { |
| 137 | .msg_name = addr, |
| 138 | .msg_namelen = addrlen, |
| 139 | .msg_flags = XS_SENDMSG_FLAGS, |
| 140 | }; |
| 141 | |
| 142 | if (xdr->len > len) |
| 143 | msg.msg_flags |= MSG_MORE; |
| 144 | |
| 145 | if (likely(iov.iov_len)) |
| 146 | return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); |
| 147 | return kernel_sendmsg(sock, &msg, NULL, 0, 0); |
| 148 | } |
| 149 | |
| 150 | static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len) |
| 151 | { |
| 152 | struct kvec iov = { |
| 153 | .iov_base = xdr->tail[0].iov_base + base, |
| 154 | .iov_len = len - base, |
| 155 | }; |
| 156 | struct msghdr msg = { |
| 157 | .msg_flags = XS_SENDMSG_FLAGS, |
| 158 | }; |
| 159 | |
| 160 | return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); |
| 161 | } |
| 162 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 163 | /** |
| 164 | * xs_sendpages - write pages directly to a socket |
| 165 | * @sock: socket to send on |
| 166 | * @addr: UDP only -- address of destination |
| 167 | * @addrlen: UDP only -- length of destination address |
| 168 | * @xdr: buffer containing this request |
| 169 | * @base: starting position in the buffer |
| 170 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 171 | */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 172 | static inline 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] | 173 | { |
| 174 | struct page **ppage = xdr->pages; |
| 175 | unsigned int len, pglen = xdr->page_len; |
| 176 | int err, ret = 0; |
| 177 | ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int); |
| 178 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 179 | if (unlikely(!sock)) |
| 180 | return -ENOTCONN; |
| 181 | |
| 182 | clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); |
| 183 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 184 | len = xdr->head[0].iov_len; |
| 185 | if (base < len || (addr != NULL && base == 0)) { |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 186 | err = xs_send_head(sock, addr, addrlen, xdr, base, len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 187 | if (ret == 0) |
| 188 | ret = err; |
| 189 | else if (err > 0) |
| 190 | ret += err; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 191 | if (err != (len - base)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 192 | goto out; |
| 193 | base = 0; |
| 194 | } else |
| 195 | base -= len; |
| 196 | |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 197 | if (unlikely(pglen == 0)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 198 | goto copy_tail; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 199 | if (unlikely(base >= pglen)) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 200 | base -= pglen; |
| 201 | goto copy_tail; |
| 202 | } |
| 203 | if (base || xdr->page_base) { |
| 204 | pglen -= base; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 205 | base += xdr->page_base; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 206 | ppage += base >> PAGE_CACHE_SHIFT; |
| 207 | base &= ~PAGE_CACHE_MASK; |
| 208 | } |
| 209 | |
| 210 | sendpage = sock->ops->sendpage ? : sock_no_sendpage; |
| 211 | do { |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 212 | int flags = XS_SENDMSG_FLAGS; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 213 | |
| 214 | len = PAGE_CACHE_SIZE; |
| 215 | if (base) |
| 216 | len -= base; |
| 217 | if (pglen < len) |
| 218 | len = pglen; |
| 219 | |
| 220 | if (pglen != len || xdr->tail[0].iov_len != 0) |
| 221 | flags |= MSG_MORE; |
| 222 | |
| 223 | /* Hmm... We might be dealing with highmem pages */ |
| 224 | if (PageHighMem(*ppage)) |
| 225 | sendpage = sock_no_sendpage; |
| 226 | err = sendpage(sock, *ppage, base, len, flags); |
| 227 | if (ret == 0) |
| 228 | ret = err; |
| 229 | else if (err > 0) |
| 230 | ret += err; |
| 231 | if (err != len) |
| 232 | goto out; |
| 233 | base = 0; |
| 234 | ppage++; |
| 235 | } while ((pglen -= len) != 0); |
| 236 | copy_tail: |
| 237 | len = xdr->tail[0].iov_len; |
| 238 | if (base < len) { |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 239 | err = xs_send_tail(sock, xdr, base, len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 240 | if (ret == 0) |
| 241 | ret = err; |
| 242 | else if (err > 0) |
| 243 | ret += err; |
| 244 | } |
| 245 | out: |
| 246 | return ret; |
| 247 | } |
| 248 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 249 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 250 | * xs_nospace - place task on wait queue if transmit was incomplete |
| 251 | * @task: task to put to sleep |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 252 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 253 | */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 254 | static void xs_nospace(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 255 | { |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 256 | struct rpc_rqst *req = task->tk_rqstp; |
| 257 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 258 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 259 | dprintk("RPC: %4d xmit incomplete (%u left of %u)\n", |
| 260 | task->tk_pid, req->rq_slen - req->rq_bytes_sent, |
| 261 | req->rq_slen); |
| 262 | |
| 263 | if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) { |
| 264 | /* Protect against races with write_space */ |
| 265 | spin_lock_bh(&xprt->transport_lock); |
| 266 | |
| 267 | /* Don't race with disconnect */ |
| 268 | if (!xprt_connected(xprt)) |
| 269 | task->tk_status = -ENOTCONN; |
| 270 | else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags)) |
| 271 | xprt_wait_for_buffer_space(task); |
| 272 | |
| 273 | spin_unlock_bh(&xprt->transport_lock); |
| 274 | } else |
| 275 | /* Keep holding the socket if it is blocked */ |
| 276 | rpc_delay(task, HZ>>4); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * xs_udp_send_request - write an RPC request to a UDP socket |
| 281 | * @task: address of RPC task that manages the state of an RPC request |
| 282 | * |
| 283 | * Return values: |
| 284 | * 0: The request has been sent |
| 285 | * EAGAIN: The socket was blocked, please call again later to |
| 286 | * complete the request |
| 287 | * ENOTCONN: Caller needs to invoke connect logic then call again |
| 288 | * other: Some other error occured, the request was not sent |
| 289 | */ |
| 290 | static int xs_udp_send_request(struct rpc_task *task) |
| 291 | { |
| 292 | struct rpc_rqst *req = task->tk_rqstp; |
| 293 | struct rpc_xprt *xprt = req->rq_xprt; |
| 294 | struct xdr_buf *xdr = &req->rq_snd_buf; |
| 295 | int status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 296 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 297 | xs_pktdump("packet data:", |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 298 | req->rq_svec->iov_base, |
| 299 | req->rq_svec->iov_len); |
| 300 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 301 | req->rq_xtime = jiffies; |
| 302 | status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr, |
| 303 | sizeof(xprt->addr), xdr, req->rq_bytes_sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 304 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 305 | dprintk("RPC: xs_udp_send_request(%u) = %d\n", |
| 306 | xdr->len - req->rq_bytes_sent, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 307 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 308 | if (likely(status >= (int) req->rq_slen)) |
| 309 | return 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 310 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 311 | /* Still some bytes left; set up for a retry later. */ |
| 312 | if (status > 0) |
| 313 | status = -EAGAIN; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 314 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 315 | switch (status) { |
| 316 | case -ENETUNREACH: |
| 317 | case -EPIPE: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 318 | case -ECONNREFUSED: |
| 319 | /* When the server has died, an ICMP port unreachable message |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 320 | * prompts ECONNREFUSED. */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 321 | break; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 322 | case -EAGAIN: |
| 323 | xs_nospace(task); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 324 | break; |
| 325 | default: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 326 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 327 | -status); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 328 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 329 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 330 | |
| 331 | return status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 332 | } |
| 333 | |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 334 | static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) |
| 335 | { |
| 336 | u32 reclen = buf->len - sizeof(rpc_fraghdr); |
| 337 | rpc_fraghdr *base = buf->head[0].iov_base; |
| 338 | *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); |
| 339 | } |
| 340 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 341 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 342 | * xs_tcp_send_request - write an RPC request to a TCP socket |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 343 | * @task: address of RPC task that manages the state of an RPC request |
| 344 | * |
| 345 | * Return values: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 346 | * 0: The request has been sent |
| 347 | * EAGAIN: The socket was blocked, please call again later to |
| 348 | * complete the request |
| 349 | * ENOTCONN: Caller needs to invoke connect logic then call again |
| 350 | * other: Some other error occured, the request was not sent |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 351 | * |
| 352 | * XXX: In the case of soft timeouts, should we eventually give up |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 353 | * if sendmsg is not able to make progress? |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 354 | */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 355 | static int xs_tcp_send_request(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 356 | { |
| 357 | struct rpc_rqst *req = task->tk_rqstp; |
| 358 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 359 | struct xdr_buf *xdr = &req->rq_snd_buf; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 360 | int status, retry = 0; |
| 361 | |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 362 | xs_encode_tcp_record_marker(&req->rq_snd_buf); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 363 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 364 | xs_pktdump("packet data:", |
| 365 | req->rq_svec->iov_base, |
| 366 | req->rq_svec->iov_len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 367 | |
| 368 | /* Continue transmitting the packet/record. We must be careful |
| 369 | * to cope with writespace callbacks arriving _after_ we have |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 370 | * called sendmsg(). */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 371 | while (1) { |
| 372 | req->rq_xtime = jiffies; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 373 | status = xs_sendpages(xprt->sock, NULL, 0, xdr, |
| 374 | req->rq_bytes_sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 375 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 376 | dprintk("RPC: xs_tcp_send_request(%u) = %d\n", |
| 377 | xdr->len - req->rq_bytes_sent, status); |
| 378 | |
| 379 | if (unlikely(status < 0)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 380 | break; |
| 381 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 382 | /* If we've sent the entire packet, immediately |
| 383 | * reset the count of bytes sent. */ |
| 384 | req->rq_bytes_sent += status; |
| 385 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { |
| 386 | req->rq_bytes_sent = 0; |
| 387 | return 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 388 | } |
| 389 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 390 | status = -EAGAIN; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 391 | if (retry++ > XS_SENDMSG_RETRY) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 395 | switch (status) { |
| 396 | case -EAGAIN: |
| 397 | xs_nospace(task); |
| 398 | break; |
| 399 | case -ECONNREFUSED: |
| 400 | case -ECONNRESET: |
| 401 | case -ENOTCONN: |
| 402 | case -EPIPE: |
| 403 | status = -ENOTCONN; |
| 404 | break; |
| 405 | default: |
| 406 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 407 | -status); |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 408 | xprt_disconnect(xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 409 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 410 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 411 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 412 | return status; |
| 413 | } |
| 414 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 415 | /** |
| 416 | * xs_close - close a socket |
| 417 | * @xprt: transport |
| 418 | * |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 419 | * This is used when all requests are complete; ie, no DRC state remains |
| 420 | * on the server we want to save. |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 421 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 422 | static void xs_close(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 423 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 424 | struct socket *sock = xprt->sock; |
| 425 | struct sock *sk = xprt->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 426 | |
| 427 | if (!sk) |
| 428 | return; |
| 429 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 430 | dprintk("RPC: xs_close xprt %p\n", xprt); |
| 431 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 432 | write_lock_bh(&sk->sk_callback_lock); |
| 433 | xprt->inet = NULL; |
| 434 | xprt->sock = NULL; |
| 435 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 436 | sk->sk_user_data = NULL; |
| 437 | sk->sk_data_ready = xprt->old_data_ready; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 438 | sk->sk_state_change = xprt->old_state_change; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 439 | sk->sk_write_space = xprt->old_write_space; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 440 | write_unlock_bh(&sk->sk_callback_lock); |
| 441 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 442 | sk->sk_no_check = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 443 | |
| 444 | sock_release(sock); |
| 445 | } |
| 446 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 447 | /** |
| 448 | * xs_destroy - prepare to shutdown a transport |
| 449 | * @xprt: doomed transport |
| 450 | * |
| 451 | */ |
| 452 | static void xs_destroy(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 453 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 454 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
| 455 | |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 456 | cancel_delayed_work(&xprt->connect_worker); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 457 | flush_scheduled_work(); |
| 458 | |
| 459 | xprt_disconnect(xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 460 | xs_close(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 461 | kfree(xprt->slot); |
| 462 | } |
| 463 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 464 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 465 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 466 | return (struct rpc_xprt *) sk->sk_user_data; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * xs_udp_data_ready - "data ready" callback for UDP sockets |
| 471 | * @sk: socket with data to read |
| 472 | * @len: how much data to read |
| 473 | * |
| 474 | */ |
| 475 | static void xs_udp_data_ready(struct sock *sk, int len) |
| 476 | { |
| 477 | struct rpc_task *task; |
| 478 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 479 | struct rpc_rqst *rovr; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 480 | struct sk_buff *skb; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 481 | int err, repsize, copied; |
| 482 | u32 _xid, *xp; |
| 483 | |
| 484 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 485 | dprintk("RPC: xs_udp_data_ready...\n"); |
| 486 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 487 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 488 | |
| 489 | if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) |
| 490 | goto out; |
| 491 | |
| 492 | if (xprt->shutdown) |
| 493 | goto dropit; |
| 494 | |
| 495 | repsize = skb->len - sizeof(struct udphdr); |
| 496 | if (repsize < 4) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 497 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 498 | goto dropit; |
| 499 | } |
| 500 | |
| 501 | /* Copy the XID from the skb... */ |
| 502 | xp = skb_header_pointer(skb, sizeof(struct udphdr), |
| 503 | sizeof(_xid), &_xid); |
| 504 | if (xp == NULL) |
| 505 | goto dropit; |
| 506 | |
| 507 | /* Look up and lock the request corresponding to the given XID */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 508 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 509 | rovr = xprt_lookup_rqst(xprt, *xp); |
| 510 | if (!rovr) |
| 511 | goto out_unlock; |
| 512 | task = rovr->rq_task; |
| 513 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 514 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
| 515 | copied = repsize; |
| 516 | |
| 517 | /* Suck it into the iovec, verify checksum if not done by hw. */ |
| 518 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) |
| 519 | goto out_unlock; |
| 520 | |
| 521 | /* Something worked... */ |
| 522 | dst_confirm(skb->dst); |
| 523 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 524 | xprt_adjust_cwnd(task, copied); |
| 525 | xprt_update_rtt(task); |
| 526 | xprt_complete_rqst(task, copied); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 527 | |
| 528 | out_unlock: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 529 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 530 | dropit: |
| 531 | skb_free_datagram(sk, skb); |
| 532 | out: |
| 533 | read_unlock(&sk->sk_callback_lock); |
| 534 | } |
| 535 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 536 | static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 537 | { |
| 538 | if (len > desc->count) |
| 539 | len = desc->count; |
| 540 | if (skb_copy_bits(desc->skb, desc->offset, p, len)) { |
| 541 | dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n", |
| 542 | len, desc->count); |
| 543 | return 0; |
| 544 | } |
| 545 | desc->offset += len; |
| 546 | desc->count -= len; |
| 547 | dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n", |
| 548 | len, desc->count); |
| 549 | return len; |
| 550 | } |
| 551 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 552 | static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 553 | { |
| 554 | size_t len, used; |
| 555 | char *p; |
| 556 | |
| 557 | p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset; |
| 558 | len = sizeof(xprt->tcp_recm) - xprt->tcp_offset; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 559 | used = xs_tcp_copy_data(desc, p, len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 560 | xprt->tcp_offset += used; |
| 561 | if (used != len) |
| 562 | return; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 563 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 564 | xprt->tcp_reclen = ntohl(xprt->tcp_recm); |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 565 | if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 566 | xprt->tcp_flags |= XPRT_LAST_FRAG; |
| 567 | else |
| 568 | xprt->tcp_flags &= ~XPRT_LAST_FRAG; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 569 | xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; |
| 570 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 571 | xprt->tcp_flags &= ~XPRT_COPY_RECM; |
| 572 | xprt->tcp_offset = 0; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 573 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 574 | /* Sanity check of the record length */ |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 575 | if (unlikely(xprt->tcp_reclen < 4)) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 576 | dprintk("RPC: invalid TCP record fragment length\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 577 | xprt_disconnect(xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 578 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 579 | } |
| 580 | dprintk("RPC: reading TCP record fragment of length %d\n", |
| 581 | xprt->tcp_reclen); |
| 582 | } |
| 583 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 584 | static void xs_tcp_check_recm(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 585 | { |
| 586 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n", |
| 587 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags); |
| 588 | if (xprt->tcp_offset == xprt->tcp_reclen) { |
| 589 | xprt->tcp_flags |= XPRT_COPY_RECM; |
| 590 | xprt->tcp_offset = 0; |
| 591 | if (xprt->tcp_flags & XPRT_LAST_FRAG) { |
| 592 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 593 | xprt->tcp_flags |= XPRT_COPY_XID; |
| 594 | xprt->tcp_copied = 0; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 599 | static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 600 | { |
| 601 | size_t len, used; |
| 602 | char *p; |
| 603 | |
| 604 | len = sizeof(xprt->tcp_xid) - xprt->tcp_offset; |
| 605 | dprintk("RPC: reading XID (%Zu bytes)\n", len); |
| 606 | p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 607 | used = xs_tcp_copy_data(desc, p, len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 608 | xprt->tcp_offset += used; |
| 609 | if (used != len) |
| 610 | return; |
| 611 | xprt->tcp_flags &= ~XPRT_COPY_XID; |
| 612 | xprt->tcp_flags |= XPRT_COPY_DATA; |
| 613 | xprt->tcp_copied = 4; |
| 614 | dprintk("RPC: reading reply for XID %08x\n", |
| 615 | ntohl(xprt->tcp_xid)); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 616 | xs_tcp_check_recm(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 617 | } |
| 618 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 619 | static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 620 | { |
| 621 | struct rpc_rqst *req; |
| 622 | struct xdr_buf *rcvbuf; |
| 623 | size_t len; |
| 624 | ssize_t r; |
| 625 | |
| 626 | /* Find and lock the request corresponding to this xid */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 627 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 628 | req = xprt_lookup_rqst(xprt, xprt->tcp_xid); |
| 629 | if (!req) { |
| 630 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 631 | dprintk("RPC: XID %08x request not found!\n", |
| 632 | ntohl(xprt->tcp_xid)); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 633 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 634 | return; |
| 635 | } |
| 636 | |
| 637 | rcvbuf = &req->rq_private_buf; |
| 638 | len = desc->count; |
| 639 | if (len > xprt->tcp_reclen - xprt->tcp_offset) { |
| 640 | skb_reader_t my_desc; |
| 641 | |
| 642 | len = xprt->tcp_reclen - xprt->tcp_offset; |
| 643 | memcpy(&my_desc, desc, sizeof(my_desc)); |
| 644 | my_desc.count = len; |
| 645 | r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 646 | &my_desc, xs_tcp_copy_data); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 647 | desc->count -= r; |
| 648 | desc->offset += r; |
| 649 | } else |
| 650 | r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 651 | desc, xs_tcp_copy_data); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 652 | |
| 653 | if (r > 0) { |
| 654 | xprt->tcp_copied += r; |
| 655 | xprt->tcp_offset += r; |
| 656 | } |
| 657 | if (r != len) { |
| 658 | /* Error when copying to the receive buffer, |
| 659 | * usually because we weren't able to allocate |
| 660 | * additional buffer pages. All we can do now |
| 661 | * is turn off XPRT_COPY_DATA, so the request |
| 662 | * will not receive any additional updates, |
| 663 | * and time out. |
| 664 | * Any remaining data from this record will |
| 665 | * be discarded. |
| 666 | */ |
| 667 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 668 | dprintk("RPC: XID %08x truncated request\n", |
| 669 | ntohl(xprt->tcp_xid)); |
| 670 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n", |
| 671 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen); |
| 672 | goto out; |
| 673 | } |
| 674 | |
| 675 | dprintk("RPC: XID %08x read %Zd bytes\n", |
| 676 | ntohl(xprt->tcp_xid), r); |
| 677 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n", |
| 678 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen); |
| 679 | |
| 680 | if (xprt->tcp_copied == req->rq_private_buf.buflen) |
| 681 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 682 | else if (xprt->tcp_offset == xprt->tcp_reclen) { |
| 683 | if (xprt->tcp_flags & XPRT_LAST_FRAG) |
| 684 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 685 | } |
| 686 | |
| 687 | out: |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 688 | if (!(xprt->tcp_flags & XPRT_COPY_DATA)) |
| 689 | xprt_complete_rqst(req->rq_task, xprt->tcp_copied); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 690 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 691 | xs_tcp_check_recm(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 692 | } |
| 693 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 694 | static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 695 | { |
| 696 | size_t len; |
| 697 | |
| 698 | len = xprt->tcp_reclen - xprt->tcp_offset; |
| 699 | if (len > desc->count) |
| 700 | len = desc->count; |
| 701 | desc->count -= len; |
| 702 | desc->offset += len; |
| 703 | xprt->tcp_offset += len; |
| 704 | dprintk("RPC: discarded %Zu bytes\n", len); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 705 | xs_tcp_check_recm(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 708 | 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] | 709 | { |
| 710 | struct rpc_xprt *xprt = rd_desc->arg.data; |
| 711 | skb_reader_t desc = { |
| 712 | .skb = skb, |
| 713 | .offset = offset, |
| 714 | .count = len, |
| 715 | .csum = 0 |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 716 | }; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 717 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 718 | dprintk("RPC: xs_tcp_data_recv started\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 719 | do { |
| 720 | /* Read in a new fragment marker if necessary */ |
| 721 | /* Can we ever really expect to get completely empty fragments? */ |
| 722 | if (xprt->tcp_flags & XPRT_COPY_RECM) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 723 | xs_tcp_read_fraghdr(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 724 | continue; |
| 725 | } |
| 726 | /* Read in the xid if necessary */ |
| 727 | if (xprt->tcp_flags & XPRT_COPY_XID) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 728 | xs_tcp_read_xid(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 729 | continue; |
| 730 | } |
| 731 | /* Read in the request data */ |
| 732 | if (xprt->tcp_flags & XPRT_COPY_DATA) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 733 | xs_tcp_read_request(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 734 | continue; |
| 735 | } |
| 736 | /* Skip over any trailing bytes on short reads */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 737 | xs_tcp_read_discard(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 738 | } while (desc.count); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 739 | dprintk("RPC: xs_tcp_data_recv done\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 740 | return len - desc.count; |
| 741 | } |
| 742 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 743 | /** |
| 744 | * xs_tcp_data_ready - "data ready" callback for TCP sockets |
| 745 | * @sk: socket with data to read |
| 746 | * @bytes: how much data to read |
| 747 | * |
| 748 | */ |
| 749 | static void xs_tcp_data_ready(struct sock *sk, int bytes) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 750 | { |
| 751 | struct rpc_xprt *xprt; |
| 752 | read_descriptor_t rd_desc; |
| 753 | |
| 754 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 755 | dprintk("RPC: xs_tcp_data_ready...\n"); |
| 756 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 757 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 758 | if (xprt->shutdown) |
| 759 | goto out; |
| 760 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 761 | /* 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] | 762 | rd_desc.arg.data = xprt; |
| 763 | rd_desc.count = 65536; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 764 | tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 765 | out: |
| 766 | read_unlock(&sk->sk_callback_lock); |
| 767 | } |
| 768 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 769 | /** |
| 770 | * xs_tcp_state_change - callback to handle TCP socket state changes |
| 771 | * @sk: socket whose state has changed |
| 772 | * |
| 773 | */ |
| 774 | static void xs_tcp_state_change(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 775 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 776 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 777 | |
| 778 | read_lock(&sk->sk_callback_lock); |
| 779 | if (!(xprt = xprt_from_sock(sk))) |
| 780 | goto out; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 781 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 782 | dprintk("RPC: state %x conn %d dead %d zapped %d\n", |
| 783 | sk->sk_state, xprt_connected(xprt), |
| 784 | sock_flag(sk, SOCK_DEAD), |
| 785 | sock_flag(sk, SOCK_ZAPPED)); |
| 786 | |
| 787 | switch (sk->sk_state) { |
| 788 | case TCP_ESTABLISHED: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 789 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 790 | if (!xprt_test_and_set_connected(xprt)) { |
| 791 | /* Reset TCP record info */ |
| 792 | xprt->tcp_offset = 0; |
| 793 | xprt->tcp_reclen = 0; |
| 794 | xprt->tcp_copied = 0; |
| 795 | xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID; |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 796 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 797 | xprt_wake_pending_tasks(xprt, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 798 | } |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 799 | spin_unlock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 800 | break; |
| 801 | case TCP_SYN_SENT: |
| 802 | case TCP_SYN_RECV: |
| 803 | break; |
| 804 | default: |
| 805 | xprt_disconnect(xprt); |
| 806 | break; |
| 807 | } |
| 808 | out: |
| 809 | read_unlock(&sk->sk_callback_lock); |
| 810 | } |
| 811 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 812 | /** |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 813 | * xs_udp_write_space - callback invoked when socket buffer space |
| 814 | * becomes available |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 815 | * @sk: socket whose state has changed |
| 816 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 817 | * Called when more output buffer space is available for this socket. |
| 818 | * We try not to wake our writers until they can make "significant" |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 819 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 820 | * with a bunch of small requests. |
| 821 | */ |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 822 | static void xs_udp_write_space(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 823 | { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 824 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 825 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 826 | /* from net/core/sock.c:sock_def_write_space */ |
| 827 | if (sock_writeable(sk)) { |
| 828 | struct socket *sock; |
| 829 | struct rpc_xprt *xprt; |
| 830 | |
| 831 | if (unlikely(!(sock = sk->sk_socket))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 832 | goto out; |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 833 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 834 | goto out; |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 835 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) |
| 836 | goto out; |
| 837 | |
| 838 | xprt_write_space(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 839 | } |
| 840 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 841 | out: |
| 842 | read_unlock(&sk->sk_callback_lock); |
| 843 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 844 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 845 | /** |
| 846 | * xs_tcp_write_space - callback invoked when socket buffer space |
| 847 | * becomes available |
| 848 | * @sk: socket whose state has changed |
| 849 | * |
| 850 | * Called when more output buffer space is available for this socket. |
| 851 | * We try not to wake our writers until they can make "significant" |
| 852 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
| 853 | * with a bunch of small requests. |
| 854 | */ |
| 855 | static void xs_tcp_write_space(struct sock *sk) |
| 856 | { |
| 857 | read_lock(&sk->sk_callback_lock); |
| 858 | |
| 859 | /* from net/core/stream.c:sk_stream_write_space */ |
| 860 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
| 861 | struct socket *sock; |
| 862 | struct rpc_xprt *xprt; |
| 863 | |
| 864 | if (unlikely(!(sock = sk->sk_socket))) |
| 865 | goto out; |
| 866 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
| 867 | goto out; |
| 868 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) |
| 869 | goto out; |
| 870 | |
| 871 | xprt_write_space(xprt); |
| 872 | } |
| 873 | |
| 874 | out: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 875 | read_unlock(&sk->sk_callback_lock); |
| 876 | } |
| 877 | |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 878 | static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 879 | { |
| 880 | struct sock *sk = xprt->inet; |
| 881 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 882 | if (xprt->rcvsize) { |
| 883 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
| 884 | sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2; |
| 885 | } |
| 886 | if (xprt->sndsize) { |
| 887 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
| 888 | sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2; |
| 889 | sk->sk_write_space(sk); |
| 890 | } |
| 891 | } |
| 892 | |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 893 | /** |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 894 | * xs_udp_set_buffer_size - set send and receive limits |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 895 | * @xprt: generic transport |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 896 | * @sndsize: requested size of send buffer, in bytes |
| 897 | * @rcvsize: requested size of receive buffer, in bytes |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 898 | * |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 899 | * Set socket send and receive buffer size limits. |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 900 | */ |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 901 | 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] | 902 | { |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 903 | xprt->sndsize = 0; |
| 904 | if (sndsize) |
| 905 | xprt->sndsize = sndsize + 1024; |
| 906 | xprt->rcvsize = 0; |
| 907 | if (rcvsize) |
| 908 | xprt->rcvsize = rcvsize + 1024; |
| 909 | |
| 910 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 913 | /** |
| 914 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport |
| 915 | * @task: task that timed out |
| 916 | * |
| 917 | * Adjust the congestion window after a retransmit timeout has occurred. |
| 918 | */ |
| 919 | static void xs_udp_timer(struct rpc_task *task) |
| 920 | { |
| 921 | xprt_adjust_cwnd(task, -ETIMEDOUT); |
| 922 | } |
| 923 | |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame^] | 924 | /** |
| 925 | * xs_set_port - reset the port number in the remote endpoint address |
| 926 | * @xprt: generic transport |
| 927 | * @port: new port number |
| 928 | * |
| 929 | */ |
| 930 | static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) |
| 931 | { |
| 932 | dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); |
| 933 | xprt->addr.sin_port = htons(port); |
| 934 | } |
| 935 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 936 | static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 937 | { |
| 938 | struct sockaddr_in myaddr = { |
| 939 | .sin_family = AF_INET, |
| 940 | }; |
Chuck Lever | 529b33c | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 941 | int err; |
| 942 | unsigned short port = xprt->port; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 943 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 944 | do { |
| 945 | myaddr.sin_port = htons(port); |
| 946 | err = sock->ops->bind(sock, (struct sockaddr *) &myaddr, |
| 947 | sizeof(myaddr)); |
| 948 | if (err == 0) { |
| 949 | xprt->port = port; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 950 | dprintk("RPC: xs_bindresvport bound to port %u\n", |
| 951 | port); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 952 | return 0; |
| 953 | } |
Chuck Lever | 529b33c | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 954 | if (port <= xprt_min_resvport) |
| 955 | port = xprt_max_resvport; |
| 956 | else |
| 957 | port--; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 958 | } while (err == -EADDRINUSE && port != xprt->port); |
| 959 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 960 | dprintk("RPC: can't bind to reserved port (%d).\n", -err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 961 | return err; |
| 962 | } |
| 963 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 964 | /** |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 965 | * xs_udp_connect_worker - set up a UDP socket |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 966 | * @args: RPC transport to connect |
| 967 | * |
| 968 | * Invoked by a work queue tasklet. |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 969 | */ |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 970 | static void xs_udp_connect_worker(void *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 971 | { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 972 | struct rpc_xprt *xprt = (struct rpc_xprt *) args; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 973 | struct socket *sock = xprt->sock; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 974 | int err, status = -EIO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 975 | |
| 976 | if (xprt->shutdown || xprt->addr.sin_port == 0) |
| 977 | goto out; |
| 978 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 979 | dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 980 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 981 | /* Start by resetting any existing state */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 982 | xs_close(xprt); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 983 | |
| 984 | if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { |
| 985 | dprintk("RPC: can't create UDP transport socket (%d).\n", -err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 986 | goto out; |
| 987 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 988 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 989 | if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { |
| 990 | sock_release(sock); |
| 991 | goto out; |
| 992 | } |
| 993 | |
| 994 | if (!xprt->inet) { |
| 995 | struct sock *sk = sock->sk; |
| 996 | |
| 997 | write_lock_bh(&sk->sk_callback_lock); |
| 998 | |
| 999 | sk->sk_user_data = xprt; |
| 1000 | xprt->old_data_ready = sk->sk_data_ready; |
| 1001 | xprt->old_state_change = sk->sk_state_change; |
| 1002 | xprt->old_write_space = sk->sk_write_space; |
| 1003 | sk->sk_data_ready = xs_udp_data_ready; |
| 1004 | sk->sk_write_space = xs_udp_write_space; |
| 1005 | sk->sk_no_check = UDP_CSUM_NORCV; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 1006 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1007 | |
| 1008 | xprt_set_connected(xprt); |
| 1009 | |
| 1010 | /* Reset to new socket */ |
| 1011 | xprt->sock = sock; |
| 1012 | xprt->inet = sk; |
| 1013 | |
| 1014 | write_unlock_bh(&sk->sk_callback_lock); |
| 1015 | } |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1016 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1017 | status = 0; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1018 | out: |
| 1019 | xprt_wake_pending_tasks(xprt, status); |
| 1020 | xprt_clear_connecting(xprt); |
| 1021 | } |
| 1022 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1023 | /* |
| 1024 | * We need to preserve the port number so the reply cache on the server can |
| 1025 | * find our cached RPC replies when we get around to reconnecting. |
| 1026 | */ |
| 1027 | static void xs_tcp_reuse_connection(struct rpc_xprt *xprt) |
| 1028 | { |
| 1029 | int result; |
| 1030 | struct socket *sock = xprt->sock; |
| 1031 | struct sockaddr any; |
| 1032 | |
| 1033 | dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); |
| 1034 | |
| 1035 | /* |
| 1036 | * Disconnect the transport socket by doing a connect operation |
| 1037 | * with AF_UNSPEC. This should return immediately... |
| 1038 | */ |
| 1039 | memset(&any, 0, sizeof(any)); |
| 1040 | any.sa_family = AF_UNSPEC; |
| 1041 | result = sock->ops->connect(sock, &any, sizeof(any), 0); |
| 1042 | if (result) |
| 1043 | dprintk("RPC: AF_UNSPEC connect return code %d\n", |
| 1044 | result); |
| 1045 | } |
| 1046 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1047 | /** |
| 1048 | * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint |
| 1049 | * @args: RPC transport to connect |
| 1050 | * |
| 1051 | * Invoked by a work queue tasklet. |
| 1052 | */ |
| 1053 | static void xs_tcp_connect_worker(void *args) |
| 1054 | { |
| 1055 | struct rpc_xprt *xprt = (struct rpc_xprt *)args; |
| 1056 | struct socket *sock = xprt->sock; |
| 1057 | int err, status = -EIO; |
| 1058 | |
| 1059 | if (xprt->shutdown || xprt->addr.sin_port == 0) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1060 | goto out; |
| 1061 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1062 | dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt); |
| 1063 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1064 | if (!xprt->sock) { |
| 1065 | /* start from scratch */ |
| 1066 | if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { |
| 1067 | dprintk("RPC: can't create TCP transport socket (%d).\n", -err); |
| 1068 | goto out; |
| 1069 | } |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1070 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1071 | if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { |
| 1072 | sock_release(sock); |
| 1073 | goto out; |
| 1074 | } |
| 1075 | } else |
| 1076 | /* "close" the socket, preserving the local port */ |
| 1077 | xs_tcp_reuse_connection(xprt); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1078 | |
| 1079 | if (!xprt->inet) { |
| 1080 | struct sock *sk = sock->sk; |
| 1081 | |
| 1082 | write_lock_bh(&sk->sk_callback_lock); |
| 1083 | |
| 1084 | sk->sk_user_data = xprt; |
| 1085 | xprt->old_data_ready = sk->sk_data_ready; |
| 1086 | xprt->old_state_change = sk->sk_state_change; |
| 1087 | xprt->old_write_space = sk->sk_write_space; |
| 1088 | sk->sk_data_ready = xs_tcp_data_ready; |
| 1089 | sk->sk_state_change = xs_tcp_state_change; |
| 1090 | sk->sk_write_space = xs_tcp_write_space; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 1091 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1092 | |
| 1093 | /* socket options */ |
| 1094 | sk->sk_userlocks |= SOCK_BINDPORT_LOCK; |
| 1095 | sock_reset_flag(sk, SOCK_LINGER); |
| 1096 | tcp_sk(sk)->linger2 = 0; |
| 1097 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1098 | |
| 1099 | xprt_clear_connected(xprt); |
| 1100 | |
| 1101 | /* Reset to new socket */ |
| 1102 | xprt->sock = sock; |
| 1103 | xprt->inet = sk; |
| 1104 | |
| 1105 | write_unlock_bh(&sk->sk_callback_lock); |
| 1106 | } |
| 1107 | |
| 1108 | /* Tell the socket layer to start connecting... */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1109 | status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr, |
| 1110 | sizeof(xprt->addr), O_NONBLOCK); |
| 1111 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 1112 | xprt, -status, xprt_connected(xprt), sock->sk->sk_state); |
| 1113 | if (status < 0) { |
| 1114 | switch (status) { |
| 1115 | case -EINPROGRESS: |
| 1116 | case -EALREADY: |
| 1117 | goto out_clear; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1118 | case -ECONNREFUSED: |
| 1119 | case -ECONNRESET: |
| 1120 | /* retry with existing socket, after a delay */ |
| 1121 | break; |
| 1122 | default: |
| 1123 | /* get rid of existing socket, and retry */ |
| 1124 | xs_close(xprt); |
| 1125 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | out: |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 1129 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1130 | out_clear: |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 1131 | xprt_clear_connecting(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1132 | } |
| 1133 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1134 | /** |
| 1135 | * xs_connect - connect a socket to a remote endpoint |
| 1136 | * @task: address of RPC task that manages state of connect request |
| 1137 | * |
| 1138 | * TCP: If the remote end dropped the connection, delay reconnecting. |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1139 | * |
| 1140 | * UDP socket connects are synchronous, but we use a work queue anyway |
| 1141 | * to guarantee that even unprivileged user processes can set up a |
| 1142 | * socket on a privileged port. |
| 1143 | * |
| 1144 | * If a UDP socket connect fails, the delay behavior here prevents |
| 1145 | * retry floods (hard mounts). |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1146 | */ |
| 1147 | static void xs_connect(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1148 | { |
| 1149 | struct rpc_xprt *xprt = task->tk_xprt; |
| 1150 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1151 | if (xprt_test_and_set_connecting(xprt)) |
| 1152 | return; |
| 1153 | |
| 1154 | if (xprt->sock != NULL) { |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1155 | dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n", |
| 1156 | xprt, xprt->reestablish_timeout / HZ); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1157 | schedule_delayed_work(&xprt->connect_worker, |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1158 | xprt->reestablish_timeout); |
| 1159 | xprt->reestablish_timeout <<= 1; |
| 1160 | if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) |
| 1161 | xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1162 | } else { |
| 1163 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
| 1164 | schedule_work(&xprt->connect_worker); |
| 1165 | |
| 1166 | /* flush_scheduled_work can sleep... */ |
| 1167 | if (!RPC_IS_ASYNC(task)) |
| 1168 | flush_scheduled_work(); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1172 | static struct rpc_xprt_ops xs_udp_ops = { |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1173 | .set_buffer_size = xs_udp_set_buffer_size, |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1174 | .reserve_xprt = xprt_reserve_xprt_cong, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1175 | .release_xprt = xprt_release_xprt_cong, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame^] | 1176 | .set_port = xs_set_port, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1177 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 1178 | .buf_alloc = rpc_malloc, |
| 1179 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1180 | .send_request = xs_udp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1181 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1182 | .timer = xs_udp_timer, |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 1183 | .release_request = xprt_release_rqst_cong, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1184 | .close = xs_close, |
| 1185 | .destroy = xs_destroy, |
| 1186 | }; |
| 1187 | |
| 1188 | static struct rpc_xprt_ops xs_tcp_ops = { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1189 | .reserve_xprt = xprt_reserve_xprt, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1190 | .release_xprt = xprt_release_xprt, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame^] | 1191 | .set_port = xs_set_port, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1192 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 1193 | .buf_alloc = rpc_malloc, |
| 1194 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1195 | .send_request = xs_tcp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1196 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1197 | .close = xs_close, |
| 1198 | .destroy = xs_destroy, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1199 | }; |
| 1200 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1201 | /** |
| 1202 | * xs_setup_udp - Set up transport to use a UDP socket |
| 1203 | * @xprt: transport to set up |
| 1204 | * @to: timeout parameters |
| 1205 | * |
| 1206 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1207 | int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to) |
| 1208 | { |
| 1209 | size_t slot_table_size; |
| 1210 | |
| 1211 | dprintk("RPC: setting up udp-ipv4 transport...\n"); |
| 1212 | |
| 1213 | xprt->max_reqs = xprt_udp_slot_table_entries; |
| 1214 | slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); |
| 1215 | xprt->slot = kmalloc(slot_table_size, GFP_KERNEL); |
| 1216 | if (xprt->slot == NULL) |
| 1217 | return -ENOMEM; |
| 1218 | memset(xprt->slot, 0, slot_table_size); |
| 1219 | |
| 1220 | xprt->prot = IPPROTO_UDP; |
Chuck Lever | 529b33c | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 1221 | xprt->port = xprt_max_resvport; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1222 | xprt->tsh_size = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1223 | xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; |
| 1224 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
| 1225 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); |
| 1226 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1227 | INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1228 | xprt->bind_timeout = XS_BIND_TO; |
| 1229 | xprt->connect_timeout = XS_UDP_CONN_TO; |
| 1230 | xprt->reestablish_timeout = XS_UDP_REEST_TO; |
| 1231 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1232 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1233 | xprt->ops = &xs_udp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1234 | |
| 1235 | if (to) |
| 1236 | xprt->timeout = *to; |
| 1237 | else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1238 | xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1243 | /** |
| 1244 | * xs_setup_tcp - Set up transport to use a TCP socket |
| 1245 | * @xprt: transport to set up |
| 1246 | * @to: timeout parameters |
| 1247 | * |
| 1248 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1249 | int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to) |
| 1250 | { |
| 1251 | size_t slot_table_size; |
| 1252 | |
| 1253 | dprintk("RPC: setting up tcp-ipv4 transport...\n"); |
| 1254 | |
| 1255 | xprt->max_reqs = xprt_tcp_slot_table_entries; |
| 1256 | slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); |
| 1257 | xprt->slot = kmalloc(slot_table_size, GFP_KERNEL); |
| 1258 | if (xprt->slot == NULL) |
| 1259 | return -ENOMEM; |
| 1260 | memset(xprt->slot, 0, slot_table_size); |
| 1261 | |
| 1262 | xprt->prot = IPPROTO_TCP; |
Chuck Lever | 529b33c | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 1263 | xprt->port = xprt_max_resvport; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1264 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1265 | xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1266 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1267 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1268 | INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1269 | xprt->bind_timeout = XS_BIND_TO; |
| 1270 | xprt->connect_timeout = XS_TCP_CONN_TO; |
| 1271 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 1272 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1273 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1274 | xprt->ops = &xs_tcp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1275 | |
| 1276 | if (to) |
| 1277 | xprt->timeout = *to; |
| 1278 | else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1279 | xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1280 | |
| 1281 | return 0; |
| 1282 | } |