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