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