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 | |
| 463 | dprintk("RPC: %4d received reply\n", task->tk_pid); |
| 464 | |
| 465 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
| 466 | copied = repsize; |
| 467 | |
| 468 | /* Suck it into the iovec, verify checksum if not done by hw. */ |
| 469 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) |
| 470 | goto out_unlock; |
| 471 | |
| 472 | /* Something worked... */ |
| 473 | dst_confirm(skb->dst); |
| 474 | |
| 475 | xprt_complete_rqst(xprt, rovr, copied); |
| 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: |
| 637 | if (!(xprt->tcp_flags & XPRT_COPY_DATA)) { |
| 638 | dprintk("RPC: %4d received reply complete\n", |
| 639 | req->rq_task->tk_pid); |
| 640 | xprt_complete_rqst(xprt, req, xprt->tcp_copied); |
| 641 | } |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 642 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 643 | xs_tcp_check_recm(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 644 | } |
| 645 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 646 | 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] | 647 | { |
| 648 | size_t len; |
| 649 | |
| 650 | len = xprt->tcp_reclen - xprt->tcp_offset; |
| 651 | if (len > desc->count) |
| 652 | len = desc->count; |
| 653 | desc->count -= len; |
| 654 | desc->offset += len; |
| 655 | xprt->tcp_offset += len; |
| 656 | dprintk("RPC: discarded %Zu bytes\n", len); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 657 | xs_tcp_check_recm(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 658 | } |
| 659 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 660 | 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] | 661 | { |
| 662 | struct rpc_xprt *xprt = rd_desc->arg.data; |
| 663 | skb_reader_t desc = { |
| 664 | .skb = skb, |
| 665 | .offset = offset, |
| 666 | .count = len, |
| 667 | .csum = 0 |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 668 | }; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 669 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 670 | dprintk("RPC: xs_tcp_data_recv started\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 671 | do { |
| 672 | /* Read in a new fragment marker if necessary */ |
| 673 | /* Can we ever really expect to get completely empty fragments? */ |
| 674 | if (xprt->tcp_flags & XPRT_COPY_RECM) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 675 | xs_tcp_read_fraghdr(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 676 | continue; |
| 677 | } |
| 678 | /* Read in the xid if necessary */ |
| 679 | if (xprt->tcp_flags & XPRT_COPY_XID) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 680 | xs_tcp_read_xid(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 681 | continue; |
| 682 | } |
| 683 | /* Read in the request data */ |
| 684 | if (xprt->tcp_flags & XPRT_COPY_DATA) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 685 | xs_tcp_read_request(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 686 | continue; |
| 687 | } |
| 688 | /* Skip over any trailing bytes on short reads */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 689 | xs_tcp_read_discard(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 690 | } while (desc.count); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 691 | dprintk("RPC: xs_tcp_data_recv done\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 692 | return len - desc.count; |
| 693 | } |
| 694 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 695 | /** |
| 696 | * xs_tcp_data_ready - "data ready" callback for TCP sockets |
| 697 | * @sk: socket with data to read |
| 698 | * @bytes: how much data to read |
| 699 | * |
| 700 | */ |
| 701 | static void xs_tcp_data_ready(struct sock *sk, int bytes) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 702 | { |
| 703 | struct rpc_xprt *xprt; |
| 704 | read_descriptor_t rd_desc; |
| 705 | |
| 706 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 707 | dprintk("RPC: xs_tcp_data_ready...\n"); |
| 708 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 709 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 710 | if (xprt->shutdown) |
| 711 | goto out; |
| 712 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 713 | /* 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] | 714 | rd_desc.arg.data = xprt; |
| 715 | rd_desc.count = 65536; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 716 | tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 717 | out: |
| 718 | read_unlock(&sk->sk_callback_lock); |
| 719 | } |
| 720 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 721 | /** |
| 722 | * xs_tcp_state_change - callback to handle TCP socket state changes |
| 723 | * @sk: socket whose state has changed |
| 724 | * |
| 725 | */ |
| 726 | static void xs_tcp_state_change(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 727 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 728 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 729 | |
| 730 | read_lock(&sk->sk_callback_lock); |
| 731 | if (!(xprt = xprt_from_sock(sk))) |
| 732 | goto out; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 733 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 734 | dprintk("RPC: state %x conn %d dead %d zapped %d\n", |
| 735 | sk->sk_state, xprt_connected(xprt), |
| 736 | sock_flag(sk, SOCK_DEAD), |
| 737 | sock_flag(sk, SOCK_ZAPPED)); |
| 738 | |
| 739 | switch (sk->sk_state) { |
| 740 | case TCP_ESTABLISHED: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 741 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 742 | if (!xprt_test_and_set_connected(xprt)) { |
| 743 | /* Reset TCP record info */ |
| 744 | xprt->tcp_offset = 0; |
| 745 | xprt->tcp_reclen = 0; |
| 746 | xprt->tcp_copied = 0; |
| 747 | xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID; |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 748 | xprt_wake_pending_tasks(xprt, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 749 | } |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 750 | spin_unlock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 751 | break; |
| 752 | case TCP_SYN_SENT: |
| 753 | case TCP_SYN_RECV: |
| 754 | break; |
| 755 | default: |
| 756 | xprt_disconnect(xprt); |
| 757 | break; |
| 758 | } |
| 759 | out: |
| 760 | read_unlock(&sk->sk_callback_lock); |
| 761 | } |
| 762 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 763 | /** |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 764 | * xs_udp_write_space - callback invoked when socket buffer space |
| 765 | * becomes available |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 766 | * @sk: socket whose state has changed |
| 767 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 768 | * Called when more output buffer space is available for this socket. |
| 769 | * We try not to wake our writers until they can make "significant" |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 770 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 771 | * with a bunch of small requests. |
| 772 | */ |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 773 | static void xs_udp_write_space(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 774 | { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 775 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 776 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 777 | /* from net/core/sock.c:sock_def_write_space */ |
| 778 | if (sock_writeable(sk)) { |
| 779 | struct socket *sock; |
| 780 | struct rpc_xprt *xprt; |
| 781 | |
| 782 | if (unlikely(!(sock = sk->sk_socket))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 783 | goto out; |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 784 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 785 | goto out; |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 786 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) |
| 787 | goto out; |
| 788 | |
| 789 | xprt_write_space(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 790 | } |
| 791 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 792 | out: |
| 793 | read_unlock(&sk->sk_callback_lock); |
| 794 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 795 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 796 | /** |
| 797 | * xs_tcp_write_space - callback invoked when socket buffer space |
| 798 | * becomes available |
| 799 | * @sk: socket whose state has changed |
| 800 | * |
| 801 | * Called when more output buffer space is available for this socket. |
| 802 | * We try not to wake our writers until they can make "significant" |
| 803 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
| 804 | * with a bunch of small requests. |
| 805 | */ |
| 806 | static void xs_tcp_write_space(struct sock *sk) |
| 807 | { |
| 808 | read_lock(&sk->sk_callback_lock); |
| 809 | |
| 810 | /* from net/core/stream.c:sk_stream_write_space */ |
| 811 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
| 812 | struct socket *sock; |
| 813 | struct rpc_xprt *xprt; |
| 814 | |
| 815 | if (unlikely(!(sock = sk->sk_socket))) |
| 816 | goto out; |
| 817 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
| 818 | goto out; |
| 819 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) |
| 820 | goto out; |
| 821 | |
| 822 | xprt_write_space(xprt); |
| 823 | } |
| 824 | |
| 825 | out: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 826 | read_unlock(&sk->sk_callback_lock); |
| 827 | } |
| 828 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 829 | /** |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 830 | * xs_udp_set_buffer_size - set send and receive limits |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 831 | * @xprt: generic transport |
| 832 | * |
| 833 | * Set socket send and receive limits based on the |
| 834 | * sndsize and rcvsize fields in the generic transport |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 835 | * structure. |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 836 | */ |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 837 | static void xs_udp_set_buffer_size(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 838 | { |
| 839 | struct sock *sk = xprt->inet; |
| 840 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 841 | if (xprt->rcvsize) { |
| 842 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
| 843 | sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2; |
| 844 | } |
| 845 | if (xprt->sndsize) { |
| 846 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
| 847 | sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2; |
| 848 | sk->sk_write_space(sk); |
| 849 | } |
| 850 | } |
| 851 | |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 852 | /** |
| 853 | * xs_tcp_set_buffer_size - set send and receive limits |
| 854 | * @xprt: generic transport |
| 855 | * |
| 856 | * Nothing to do for TCP. |
| 857 | */ |
| 858 | static void xs_tcp_set_buffer_size(struct rpc_xprt *xprt) |
| 859 | { |
| 860 | return; |
| 861 | } |
| 862 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 863 | /** |
| 864 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport |
| 865 | * @task: task that timed out |
| 866 | * |
| 867 | * Adjust the congestion window after a retransmit timeout has occurred. |
| 868 | */ |
| 869 | static void xs_udp_timer(struct rpc_task *task) |
| 870 | { |
| 871 | xprt_adjust_cwnd(task, -ETIMEDOUT); |
| 872 | } |
| 873 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 874 | static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 875 | { |
| 876 | struct sockaddr_in myaddr = { |
| 877 | .sin_family = AF_INET, |
| 878 | }; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 879 | int err, port; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 880 | |
| 881 | /* Were we already bound to a given port? Try to reuse it */ |
| 882 | port = xprt->port; |
| 883 | do { |
| 884 | myaddr.sin_port = htons(port); |
| 885 | err = sock->ops->bind(sock, (struct sockaddr *) &myaddr, |
| 886 | sizeof(myaddr)); |
| 887 | if (err == 0) { |
| 888 | xprt->port = port; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 889 | dprintk("RPC: xs_bindresvport bound to port %u\n", |
| 890 | port); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | if (--port == 0) |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 894 | port = XS_MAX_RESVPORT; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 895 | } while (err == -EADDRINUSE && port != xprt->port); |
| 896 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 897 | dprintk("RPC: can't bind to reserved port (%d).\n", -err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 898 | return err; |
| 899 | } |
| 900 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 901 | /** |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 902 | * xs_udp_connect_worker - set up a UDP socket |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 903 | * @args: RPC transport to connect |
| 904 | * |
| 905 | * Invoked by a work queue tasklet. |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 906 | */ |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 907 | static void xs_udp_connect_worker(void *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 908 | { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 909 | struct rpc_xprt *xprt = (struct rpc_xprt *) args; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 910 | struct socket *sock = xprt->sock; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 911 | int err, status = -EIO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 912 | |
| 913 | if (xprt->shutdown || xprt->addr.sin_port == 0) |
| 914 | goto out; |
| 915 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 916 | dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 917 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 918 | /* Start by resetting any existing state */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 919 | xs_close(xprt); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 920 | |
| 921 | if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { |
| 922 | dprintk("RPC: can't create UDP transport socket (%d).\n", -err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 923 | goto out; |
| 924 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 925 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 926 | if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { |
| 927 | sock_release(sock); |
| 928 | goto out; |
| 929 | } |
| 930 | |
| 931 | if (!xprt->inet) { |
| 932 | struct sock *sk = sock->sk; |
| 933 | |
| 934 | write_lock_bh(&sk->sk_callback_lock); |
| 935 | |
| 936 | sk->sk_user_data = xprt; |
| 937 | xprt->old_data_ready = sk->sk_data_ready; |
| 938 | xprt->old_state_change = sk->sk_state_change; |
| 939 | xprt->old_write_space = sk->sk_write_space; |
| 940 | sk->sk_data_ready = xs_udp_data_ready; |
| 941 | sk->sk_write_space = xs_udp_write_space; |
| 942 | sk->sk_no_check = UDP_CSUM_NORCV; |
| 943 | |
| 944 | xprt_set_connected(xprt); |
| 945 | |
| 946 | /* Reset to new socket */ |
| 947 | xprt->sock = sock; |
| 948 | xprt->inet = sk; |
| 949 | |
| 950 | write_unlock_bh(&sk->sk_callback_lock); |
| 951 | } |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 952 | xs_udp_set_buffer_size(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 953 | status = 0; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 954 | out: |
| 955 | xprt_wake_pending_tasks(xprt, status); |
| 956 | xprt_clear_connecting(xprt); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint |
| 961 | * @args: RPC transport to connect |
| 962 | * |
| 963 | * Invoked by a work queue tasklet. |
| 964 | */ |
| 965 | static void xs_tcp_connect_worker(void *args) |
| 966 | { |
| 967 | struct rpc_xprt *xprt = (struct rpc_xprt *)args; |
| 968 | struct socket *sock = xprt->sock; |
| 969 | int err, status = -EIO; |
| 970 | |
| 971 | if (xprt->shutdown || xprt->addr.sin_port == 0) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 972 | goto out; |
| 973 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 974 | dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt); |
| 975 | |
| 976 | /* Start by resetting any existing socket state */ |
| 977 | xs_close(xprt); |
| 978 | |
| 979 | if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { |
| 980 | dprintk("RPC: can't create TCP transport socket (%d).\n", -err); |
| 981 | goto out; |
| 982 | } |
| 983 | |
| 984 | if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { |
| 985 | sock_release(sock); |
| 986 | goto out; |
| 987 | } |
| 988 | |
| 989 | if (!xprt->inet) { |
| 990 | struct sock *sk = sock->sk; |
| 991 | |
| 992 | write_lock_bh(&sk->sk_callback_lock); |
| 993 | |
| 994 | sk->sk_user_data = xprt; |
| 995 | xprt->old_data_ready = sk->sk_data_ready; |
| 996 | xprt->old_state_change = sk->sk_state_change; |
| 997 | xprt->old_write_space = sk->sk_write_space; |
| 998 | sk->sk_data_ready = xs_tcp_data_ready; |
| 999 | sk->sk_state_change = xs_tcp_state_change; |
| 1000 | sk->sk_write_space = xs_tcp_write_space; |
| 1001 | tcp_sk(sk)->nonagle = 1; |
| 1002 | |
| 1003 | xprt_clear_connected(xprt); |
| 1004 | |
| 1005 | /* Reset to new socket */ |
| 1006 | xprt->sock = sock; |
| 1007 | xprt->inet = sk; |
| 1008 | |
| 1009 | write_unlock_bh(&sk->sk_callback_lock); |
| 1010 | } |
| 1011 | |
| 1012 | /* Tell the socket layer to start connecting... */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1013 | status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr, |
| 1014 | sizeof(xprt->addr), O_NONBLOCK); |
| 1015 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 1016 | xprt, -status, xprt_connected(xprt), sock->sk->sk_state); |
| 1017 | if (status < 0) { |
| 1018 | switch (status) { |
| 1019 | case -EINPROGRESS: |
| 1020 | case -EALREADY: |
| 1021 | goto out_clear; |
| 1022 | } |
| 1023 | } |
| 1024 | out: |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 1025 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1026 | out_clear: |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 1027 | xprt_clear_connecting(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1030 | /** |
| 1031 | * xs_connect - connect a socket to a remote endpoint |
| 1032 | * @task: address of RPC task that manages state of connect request |
| 1033 | * |
| 1034 | * TCP: If the remote end dropped the connection, delay reconnecting. |
| 1035 | */ |
| 1036 | static void xs_connect(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1037 | { |
| 1038 | struct rpc_xprt *xprt = task->tk_xprt; |
| 1039 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1040 | if (xprt_test_and_set_connecting(xprt)) |
| 1041 | return; |
| 1042 | |
| 1043 | if (xprt->sock != NULL) { |
| 1044 | dprintk("RPC: xs_connect delayed xprt %p\n", xprt); |
| 1045 | schedule_delayed_work(&xprt->connect_worker, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1046 | RPC_REESTABLISH_TIMEOUT); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1047 | } else { |
| 1048 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
| 1049 | schedule_work(&xprt->connect_worker); |
| 1050 | |
| 1051 | /* flush_scheduled_work can sleep... */ |
| 1052 | if (!RPC_IS_ASYNC(task)) |
| 1053 | flush_scheduled_work(); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1057 | static struct rpc_xprt_ops xs_udp_ops = { |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1058 | .set_buffer_size = xs_udp_set_buffer_size, |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1059 | .reserve_xprt = xprt_reserve_xprt_cong, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1060 | .release_xprt = xprt_release_xprt_cong, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1061 | .connect = xs_connect, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1062 | .send_request = xs_udp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1063 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 1064 | .timer = xs_udp_timer, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1065 | .close = xs_close, |
| 1066 | .destroy = xs_destroy, |
| 1067 | }; |
| 1068 | |
| 1069 | static struct rpc_xprt_ops xs_tcp_ops = { |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1070 | .set_buffer_size = xs_tcp_set_buffer_size, |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1071 | .reserve_xprt = xprt_reserve_xprt, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1072 | .release_xprt = xprt_release_xprt, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1073 | .connect = xs_connect, |
| 1074 | .send_request = xs_tcp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1075 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1076 | .close = xs_close, |
| 1077 | .destroy = xs_destroy, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1078 | }; |
| 1079 | |
| 1080 | extern unsigned int xprt_udp_slot_table_entries; |
| 1081 | extern unsigned int xprt_tcp_slot_table_entries; |
| 1082 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1083 | /** |
| 1084 | * xs_setup_udp - Set up transport to use a UDP socket |
| 1085 | * @xprt: transport to set up |
| 1086 | * @to: timeout parameters |
| 1087 | * |
| 1088 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1089 | int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to) |
| 1090 | { |
| 1091 | size_t slot_table_size; |
| 1092 | |
| 1093 | dprintk("RPC: setting up udp-ipv4 transport...\n"); |
| 1094 | |
| 1095 | xprt->max_reqs = xprt_udp_slot_table_entries; |
| 1096 | slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); |
| 1097 | xprt->slot = kmalloc(slot_table_size, GFP_KERNEL); |
| 1098 | if (xprt->slot == NULL) |
| 1099 | return -ENOMEM; |
| 1100 | memset(xprt->slot, 0, slot_table_size); |
| 1101 | |
| 1102 | xprt->prot = IPPROTO_UDP; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1103 | xprt->port = XS_MAX_RESVPORT; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1104 | xprt->tsh_size = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1105 | xprt->nocong = 0; |
| 1106 | xprt->cwnd = RPC_INITCWND; |
| 1107 | xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; |
| 1108 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
| 1109 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); |
| 1110 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1111 | INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1112 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1113 | xprt->ops = &xs_udp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1114 | |
| 1115 | if (to) |
| 1116 | xprt->timeout = *to; |
| 1117 | else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1118 | xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1119 | |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1123 | /** |
| 1124 | * xs_setup_tcp - Set up transport to use a TCP socket |
| 1125 | * @xprt: transport to set up |
| 1126 | * @to: timeout parameters |
| 1127 | * |
| 1128 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1129 | int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to) |
| 1130 | { |
| 1131 | size_t slot_table_size; |
| 1132 | |
| 1133 | dprintk("RPC: setting up tcp-ipv4 transport...\n"); |
| 1134 | |
| 1135 | xprt->max_reqs = xprt_tcp_slot_table_entries; |
| 1136 | slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); |
| 1137 | xprt->slot = kmalloc(slot_table_size, GFP_KERNEL); |
| 1138 | if (xprt->slot == NULL) |
| 1139 | return -ENOMEM; |
| 1140 | memset(xprt->slot, 0, slot_table_size); |
| 1141 | |
| 1142 | xprt->prot = IPPROTO_TCP; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1143 | xprt->port = XS_MAX_RESVPORT; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1144 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1145 | xprt->nocong = 1; |
| 1146 | xprt->cwnd = RPC_MAXCWND(xprt); |
| 1147 | xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1148 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1149 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1150 | INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1151 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1152 | xprt->ops = &xs_tcp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1153 | |
| 1154 | if (to) |
| 1155 | xprt->timeout = *to; |
| 1156 | else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1157 | xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1158 | |
| 1159 | return 0; |
| 1160 | } |