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 | /** |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 417 | * xs_tcp_release_xprt - clean up after a tcp transmission |
| 418 | * @xprt: transport |
| 419 | * @task: rpc task |
| 420 | * |
| 421 | * This cleans up if an error causes us to abort the transmission of a request. |
| 422 | * In this case, the socket may need to be reset in order to avoid confusing |
| 423 | * the server. |
| 424 | */ |
| 425 | static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
| 426 | { |
| 427 | struct rpc_rqst *req; |
| 428 | |
| 429 | if (task != xprt->snd_task) |
| 430 | return; |
| 431 | if (task == NULL) |
| 432 | goto out_release; |
| 433 | req = task->tk_rqstp; |
| 434 | if (req->rq_bytes_sent == 0) |
| 435 | goto out_release; |
| 436 | if (req->rq_bytes_sent == req->rq_snd_buf.len) |
| 437 | goto out_release; |
| 438 | set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); |
| 439 | out_release: |
| 440 | xprt_release_xprt(xprt, task); |
| 441 | } |
| 442 | |
| 443 | /** |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 444 | * xs_close - close a socket |
| 445 | * @xprt: transport |
| 446 | * |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 447 | * This is used when all requests are complete; ie, no DRC state remains |
| 448 | * on the server we want to save. |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 449 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 450 | static void xs_close(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 451 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 452 | struct socket *sock = xprt->sock; |
| 453 | struct sock *sk = xprt->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 454 | |
| 455 | if (!sk) |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 456 | goto clear_close_wait; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 457 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 458 | dprintk("RPC: xs_close xprt %p\n", xprt); |
| 459 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 460 | write_lock_bh(&sk->sk_callback_lock); |
| 461 | xprt->inet = NULL; |
| 462 | xprt->sock = NULL; |
| 463 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 464 | sk->sk_user_data = NULL; |
| 465 | sk->sk_data_ready = xprt->old_data_ready; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 466 | sk->sk_state_change = xprt->old_state_change; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 467 | sk->sk_write_space = xprt->old_write_space; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 468 | write_unlock_bh(&sk->sk_callback_lock); |
| 469 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 470 | sk->sk_no_check = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 471 | |
| 472 | sock_release(sock); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 473 | clear_close_wait: |
| 474 | smp_mb__before_clear_bit(); |
| 475 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 476 | smp_mb__after_clear_bit(); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 479 | /** |
| 480 | * xs_destroy - prepare to shutdown a transport |
| 481 | * @xprt: doomed transport |
| 482 | * |
| 483 | */ |
| 484 | static void xs_destroy(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 485 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 486 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
| 487 | |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 488 | cancel_delayed_work(&xprt->connect_worker); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 489 | flush_scheduled_work(); |
| 490 | |
| 491 | xprt_disconnect(xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 492 | xs_close(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 493 | kfree(xprt->slot); |
| 494 | } |
| 495 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 496 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 497 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 498 | return (struct rpc_xprt *) sk->sk_user_data; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * xs_udp_data_ready - "data ready" callback for UDP sockets |
| 503 | * @sk: socket with data to read |
| 504 | * @len: how much data to read |
| 505 | * |
| 506 | */ |
| 507 | static void xs_udp_data_ready(struct sock *sk, int len) |
| 508 | { |
| 509 | struct rpc_task *task; |
| 510 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 511 | struct rpc_rqst *rovr; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 512 | struct sk_buff *skb; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 513 | int err, repsize, copied; |
| 514 | u32 _xid, *xp; |
| 515 | |
| 516 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 517 | dprintk("RPC: xs_udp_data_ready...\n"); |
| 518 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 519 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 520 | |
| 521 | if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) |
| 522 | goto out; |
| 523 | |
| 524 | if (xprt->shutdown) |
| 525 | goto dropit; |
| 526 | |
| 527 | repsize = skb->len - sizeof(struct udphdr); |
| 528 | if (repsize < 4) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 529 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 530 | goto dropit; |
| 531 | } |
| 532 | |
| 533 | /* Copy the XID from the skb... */ |
| 534 | xp = skb_header_pointer(skb, sizeof(struct udphdr), |
| 535 | sizeof(_xid), &_xid); |
| 536 | if (xp == NULL) |
| 537 | goto dropit; |
| 538 | |
| 539 | /* Look up and lock the request corresponding to the given XID */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 540 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 541 | rovr = xprt_lookup_rqst(xprt, *xp); |
| 542 | if (!rovr) |
| 543 | goto out_unlock; |
| 544 | task = rovr->rq_task; |
| 545 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 546 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
| 547 | copied = repsize; |
| 548 | |
| 549 | /* Suck it into the iovec, verify checksum if not done by hw. */ |
| 550 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) |
| 551 | goto out_unlock; |
| 552 | |
| 553 | /* Something worked... */ |
| 554 | dst_confirm(skb->dst); |
| 555 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 556 | xprt_adjust_cwnd(task, copied); |
| 557 | xprt_update_rtt(task); |
| 558 | xprt_complete_rqst(task, copied); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 559 | |
| 560 | out_unlock: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 561 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 562 | dropit: |
| 563 | skb_free_datagram(sk, skb); |
| 564 | out: |
| 565 | read_unlock(&sk->sk_callback_lock); |
| 566 | } |
| 567 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 568 | 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] | 569 | { |
| 570 | if (len > desc->count) |
| 571 | len = desc->count; |
| 572 | if (skb_copy_bits(desc->skb, desc->offset, p, len)) { |
| 573 | dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n", |
| 574 | len, desc->count); |
| 575 | return 0; |
| 576 | } |
| 577 | desc->offset += len; |
| 578 | desc->count -= len; |
| 579 | dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n", |
| 580 | len, desc->count); |
| 581 | return len; |
| 582 | } |
| 583 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 584 | 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] | 585 | { |
| 586 | size_t len, used; |
| 587 | char *p; |
| 588 | |
| 589 | p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset; |
| 590 | len = sizeof(xprt->tcp_recm) - xprt->tcp_offset; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 591 | used = xs_tcp_copy_data(desc, p, len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 592 | xprt->tcp_offset += used; |
| 593 | if (used != len) |
| 594 | return; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 595 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 596 | xprt->tcp_reclen = ntohl(xprt->tcp_recm); |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 597 | if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 598 | xprt->tcp_flags |= XPRT_LAST_FRAG; |
| 599 | else |
| 600 | xprt->tcp_flags &= ~XPRT_LAST_FRAG; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 601 | xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; |
| 602 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 603 | xprt->tcp_flags &= ~XPRT_COPY_RECM; |
| 604 | xprt->tcp_offset = 0; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 605 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 606 | /* Sanity check of the record length */ |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 607 | if (unlikely(xprt->tcp_reclen < 4)) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 608 | dprintk("RPC: invalid TCP record fragment length\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 609 | xprt_disconnect(xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 610 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 611 | } |
| 612 | dprintk("RPC: reading TCP record fragment of length %d\n", |
| 613 | xprt->tcp_reclen); |
| 614 | } |
| 615 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 616 | static void xs_tcp_check_recm(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 617 | { |
| 618 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n", |
| 619 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags); |
| 620 | if (xprt->tcp_offset == xprt->tcp_reclen) { |
| 621 | xprt->tcp_flags |= XPRT_COPY_RECM; |
| 622 | xprt->tcp_offset = 0; |
| 623 | if (xprt->tcp_flags & XPRT_LAST_FRAG) { |
| 624 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 625 | xprt->tcp_flags |= XPRT_COPY_XID; |
| 626 | xprt->tcp_copied = 0; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 631 | 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] | 632 | { |
| 633 | size_t len, used; |
| 634 | char *p; |
| 635 | |
| 636 | len = sizeof(xprt->tcp_xid) - xprt->tcp_offset; |
| 637 | dprintk("RPC: reading XID (%Zu bytes)\n", len); |
| 638 | p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 639 | used = xs_tcp_copy_data(desc, p, len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 640 | xprt->tcp_offset += used; |
| 641 | if (used != len) |
| 642 | return; |
| 643 | xprt->tcp_flags &= ~XPRT_COPY_XID; |
| 644 | xprt->tcp_flags |= XPRT_COPY_DATA; |
| 645 | xprt->tcp_copied = 4; |
| 646 | dprintk("RPC: reading reply for XID %08x\n", |
| 647 | ntohl(xprt->tcp_xid)); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 648 | xs_tcp_check_recm(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 649 | } |
| 650 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 651 | 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] | 652 | { |
| 653 | struct rpc_rqst *req; |
| 654 | struct xdr_buf *rcvbuf; |
| 655 | size_t len; |
| 656 | ssize_t r; |
| 657 | |
| 658 | /* Find and lock the request corresponding to this xid */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 659 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 660 | req = xprt_lookup_rqst(xprt, xprt->tcp_xid); |
| 661 | if (!req) { |
| 662 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 663 | dprintk("RPC: XID %08x request not found!\n", |
| 664 | ntohl(xprt->tcp_xid)); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 665 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | |
| 669 | rcvbuf = &req->rq_private_buf; |
| 670 | len = desc->count; |
| 671 | if (len > xprt->tcp_reclen - xprt->tcp_offset) { |
| 672 | skb_reader_t my_desc; |
| 673 | |
| 674 | len = xprt->tcp_reclen - xprt->tcp_offset; |
| 675 | memcpy(&my_desc, desc, sizeof(my_desc)); |
| 676 | my_desc.count = len; |
| 677 | r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 678 | &my_desc, xs_tcp_copy_data); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 679 | desc->count -= r; |
| 680 | desc->offset += r; |
| 681 | } else |
| 682 | r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 683 | desc, xs_tcp_copy_data); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 684 | |
| 685 | if (r > 0) { |
| 686 | xprt->tcp_copied += r; |
| 687 | xprt->tcp_offset += r; |
| 688 | } |
| 689 | if (r != len) { |
| 690 | /* Error when copying to the receive buffer, |
| 691 | * usually because we weren't able to allocate |
| 692 | * additional buffer pages. All we can do now |
| 693 | * is turn off XPRT_COPY_DATA, so the request |
| 694 | * will not receive any additional updates, |
| 695 | * and time out. |
| 696 | * Any remaining data from this record will |
| 697 | * be discarded. |
| 698 | */ |
| 699 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 700 | dprintk("RPC: XID %08x truncated request\n", |
| 701 | ntohl(xprt->tcp_xid)); |
| 702 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n", |
| 703 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen); |
| 704 | goto out; |
| 705 | } |
| 706 | |
| 707 | dprintk("RPC: XID %08x read %Zd bytes\n", |
| 708 | ntohl(xprt->tcp_xid), r); |
| 709 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n", |
| 710 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen); |
| 711 | |
| 712 | if (xprt->tcp_copied == req->rq_private_buf.buflen) |
| 713 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 714 | else if (xprt->tcp_offset == xprt->tcp_reclen) { |
| 715 | if (xprt->tcp_flags & XPRT_LAST_FRAG) |
| 716 | xprt->tcp_flags &= ~XPRT_COPY_DATA; |
| 717 | } |
| 718 | |
| 719 | out: |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 720 | if (!(xprt->tcp_flags & XPRT_COPY_DATA)) |
| 721 | xprt_complete_rqst(req->rq_task, xprt->tcp_copied); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 722 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 723 | xs_tcp_check_recm(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 724 | } |
| 725 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 726 | 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] | 727 | { |
| 728 | size_t len; |
| 729 | |
| 730 | len = xprt->tcp_reclen - xprt->tcp_offset; |
| 731 | if (len > desc->count) |
| 732 | len = desc->count; |
| 733 | desc->count -= len; |
| 734 | desc->offset += len; |
| 735 | xprt->tcp_offset += len; |
| 736 | dprintk("RPC: discarded %Zu bytes\n", len); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 737 | xs_tcp_check_recm(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 738 | } |
| 739 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 740 | 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] | 741 | { |
| 742 | struct rpc_xprt *xprt = rd_desc->arg.data; |
| 743 | skb_reader_t desc = { |
| 744 | .skb = skb, |
| 745 | .offset = offset, |
| 746 | .count = len, |
| 747 | .csum = 0 |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 748 | }; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 749 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 750 | dprintk("RPC: xs_tcp_data_recv started\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 751 | do { |
| 752 | /* Read in a new fragment marker if necessary */ |
| 753 | /* Can we ever really expect to get completely empty fragments? */ |
| 754 | if (xprt->tcp_flags & XPRT_COPY_RECM) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 755 | xs_tcp_read_fraghdr(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 756 | continue; |
| 757 | } |
| 758 | /* Read in the xid if necessary */ |
| 759 | if (xprt->tcp_flags & XPRT_COPY_XID) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 760 | xs_tcp_read_xid(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 761 | continue; |
| 762 | } |
| 763 | /* Read in the request data */ |
| 764 | if (xprt->tcp_flags & XPRT_COPY_DATA) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 765 | xs_tcp_read_request(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 766 | continue; |
| 767 | } |
| 768 | /* Skip over any trailing bytes on short reads */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 769 | xs_tcp_read_discard(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 770 | } while (desc.count); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 771 | dprintk("RPC: xs_tcp_data_recv done\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 772 | return len - desc.count; |
| 773 | } |
| 774 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 775 | /** |
| 776 | * xs_tcp_data_ready - "data ready" callback for TCP sockets |
| 777 | * @sk: socket with data to read |
| 778 | * @bytes: how much data to read |
| 779 | * |
| 780 | */ |
| 781 | static void xs_tcp_data_ready(struct sock *sk, int bytes) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 782 | { |
| 783 | struct rpc_xprt *xprt; |
| 784 | read_descriptor_t rd_desc; |
| 785 | |
| 786 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 787 | dprintk("RPC: xs_tcp_data_ready...\n"); |
| 788 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 789 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 790 | if (xprt->shutdown) |
| 791 | goto out; |
| 792 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 793 | /* 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] | 794 | rd_desc.arg.data = xprt; |
| 795 | rd_desc.count = 65536; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 796 | tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 797 | out: |
| 798 | read_unlock(&sk->sk_callback_lock); |
| 799 | } |
| 800 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 801 | /** |
| 802 | * xs_tcp_state_change - callback to handle TCP socket state changes |
| 803 | * @sk: socket whose state has changed |
| 804 | * |
| 805 | */ |
| 806 | static void xs_tcp_state_change(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 807 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 808 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 809 | |
| 810 | read_lock(&sk->sk_callback_lock); |
| 811 | if (!(xprt = xprt_from_sock(sk))) |
| 812 | goto out; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 813 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 814 | dprintk("RPC: state %x conn %d dead %d zapped %d\n", |
| 815 | sk->sk_state, xprt_connected(xprt), |
| 816 | sock_flag(sk, SOCK_DEAD), |
| 817 | sock_flag(sk, SOCK_ZAPPED)); |
| 818 | |
| 819 | switch (sk->sk_state) { |
| 820 | case TCP_ESTABLISHED: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 821 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 822 | if (!xprt_test_and_set_connected(xprt)) { |
| 823 | /* Reset TCP record info */ |
| 824 | xprt->tcp_offset = 0; |
| 825 | xprt->tcp_reclen = 0; |
| 826 | xprt->tcp_copied = 0; |
| 827 | xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID; |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 828 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 829 | xprt_wake_pending_tasks(xprt, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 830 | } |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 831 | spin_unlock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 832 | break; |
| 833 | case TCP_SYN_SENT: |
| 834 | case TCP_SYN_RECV: |
| 835 | break; |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 836 | case TCP_CLOSE_WAIT: |
| 837 | /* Try to schedule an autoclose RPC calls */ |
| 838 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 839 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) |
| 840 | schedule_work(&xprt->task_cleanup); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 841 | default: |
| 842 | xprt_disconnect(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 843 | } |
| 844 | out: |
| 845 | read_unlock(&sk->sk_callback_lock); |
| 846 | } |
| 847 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 848 | /** |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 849 | * xs_udp_write_space - callback invoked when socket buffer space |
| 850 | * becomes available |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 851 | * @sk: socket whose state has changed |
| 852 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 853 | * Called when more output buffer space is available for this socket. |
| 854 | * We try not to wake our writers until they can make "significant" |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 855 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 856 | * with a bunch of small requests. |
| 857 | */ |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 858 | static void xs_udp_write_space(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 859 | { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 860 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 861 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 862 | /* from net/core/sock.c:sock_def_write_space */ |
| 863 | if (sock_writeable(sk)) { |
| 864 | struct socket *sock; |
| 865 | struct rpc_xprt *xprt; |
| 866 | |
| 867 | if (unlikely(!(sock = sk->sk_socket))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 868 | goto out; |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 869 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 870 | goto out; |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 871 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) |
| 872 | goto out; |
| 873 | |
| 874 | xprt_write_space(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 875 | } |
| 876 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 877 | out: |
| 878 | read_unlock(&sk->sk_callback_lock); |
| 879 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 880 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 881 | /** |
| 882 | * xs_tcp_write_space - callback invoked when socket buffer space |
| 883 | * becomes available |
| 884 | * @sk: socket whose state has changed |
| 885 | * |
| 886 | * Called when more output buffer space is available for this socket. |
| 887 | * We try not to wake our writers until they can make "significant" |
| 888 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
| 889 | * with a bunch of small requests. |
| 890 | */ |
| 891 | static void xs_tcp_write_space(struct sock *sk) |
| 892 | { |
| 893 | read_lock(&sk->sk_callback_lock); |
| 894 | |
| 895 | /* from net/core/stream.c:sk_stream_write_space */ |
| 896 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
| 897 | struct socket *sock; |
| 898 | struct rpc_xprt *xprt; |
| 899 | |
| 900 | if (unlikely(!(sock = sk->sk_socket))) |
| 901 | goto out; |
| 902 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
| 903 | goto out; |
| 904 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) |
| 905 | goto out; |
| 906 | |
| 907 | xprt_write_space(xprt); |
| 908 | } |
| 909 | |
| 910 | out: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 911 | read_unlock(&sk->sk_callback_lock); |
| 912 | } |
| 913 | |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 914 | static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 915 | { |
| 916 | struct sock *sk = xprt->inet; |
| 917 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 918 | if (xprt->rcvsize) { |
| 919 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
| 920 | sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2; |
| 921 | } |
| 922 | if (xprt->sndsize) { |
| 923 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
| 924 | sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2; |
| 925 | sk->sk_write_space(sk); |
| 926 | } |
| 927 | } |
| 928 | |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 929 | /** |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 930 | * xs_udp_set_buffer_size - set send and receive limits |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 931 | * @xprt: generic transport |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 932 | * @sndsize: requested size of send buffer, in bytes |
| 933 | * @rcvsize: requested size of receive buffer, in bytes |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 934 | * |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 935 | * Set socket send and receive buffer size limits. |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 936 | */ |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 937 | 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] | 938 | { |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 939 | xprt->sndsize = 0; |
| 940 | if (sndsize) |
| 941 | xprt->sndsize = sndsize + 1024; |
| 942 | xprt->rcvsize = 0; |
| 943 | if (rcvsize) |
| 944 | xprt->rcvsize = rcvsize + 1024; |
| 945 | |
| 946 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 949 | /** |
| 950 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport |
| 951 | * @task: task that timed out |
| 952 | * |
| 953 | * Adjust the congestion window after a retransmit timeout has occurred. |
| 954 | */ |
| 955 | static void xs_udp_timer(struct rpc_task *task) |
| 956 | { |
| 957 | xprt_adjust_cwnd(task, -ETIMEDOUT); |
| 958 | } |
| 959 | |
Chuck Lever | b85d880 | 2006-05-25 01:40:49 -0400 | [diff] [blame] | 960 | static unsigned short xs_get_random_port(void) |
| 961 | { |
| 962 | unsigned short range = xprt_max_resvport - xprt_min_resvport; |
| 963 | unsigned short rand = (unsigned short) net_random() % range; |
| 964 | return rand + xprt_min_resvport; |
| 965 | } |
| 966 | |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 967 | /** |
| 968 | * xs_set_port - reset the port number in the remote endpoint address |
| 969 | * @xprt: generic transport |
| 970 | * @port: new port number |
| 971 | * |
| 972 | */ |
| 973 | static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) |
| 974 | { |
| 975 | dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); |
| 976 | xprt->addr.sin_port = htons(port); |
| 977 | } |
| 978 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 979 | static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 980 | { |
| 981 | struct sockaddr_in myaddr = { |
| 982 | .sin_family = AF_INET, |
| 983 | }; |
Chuck Lever | 529b33c | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 984 | int err; |
| 985 | unsigned short port = xprt->port; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 986 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 987 | do { |
| 988 | myaddr.sin_port = htons(port); |
| 989 | err = sock->ops->bind(sock, (struct sockaddr *) &myaddr, |
| 990 | sizeof(myaddr)); |
| 991 | if (err == 0) { |
| 992 | xprt->port = port; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 993 | dprintk("RPC: xs_bindresvport bound to port %u\n", |
| 994 | port); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 995 | return 0; |
| 996 | } |
Chuck Lever | 529b33c | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 997 | if (port <= xprt_min_resvport) |
| 998 | port = xprt_max_resvport; |
| 999 | else |
| 1000 | port--; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1001 | } while (err == -EADDRINUSE && port != xprt->port); |
| 1002 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1003 | dprintk("RPC: can't bind to reserved port (%d).\n", -err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1004 | return err; |
| 1005 | } |
| 1006 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1007 | /** |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1008 | * xs_udp_connect_worker - set up a UDP socket |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1009 | * @args: RPC transport to connect |
| 1010 | * |
| 1011 | * Invoked by a work queue tasklet. |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1012 | */ |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1013 | static void xs_udp_connect_worker(void *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1014 | { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1015 | struct rpc_xprt *xprt = (struct rpc_xprt *) args; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1016 | struct socket *sock = xprt->sock; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1017 | int err, status = -EIO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1018 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame^] | 1019 | if (xprt->shutdown || !xprt_bound(xprt)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1020 | goto out; |
| 1021 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1022 | dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1023 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1024 | /* Start by resetting any existing state */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1025 | xs_close(xprt); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1026 | |
| 1027 | if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { |
| 1028 | dprintk("RPC: can't create UDP transport socket (%d).\n", -err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1029 | goto out; |
| 1030 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1031 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1032 | if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { |
| 1033 | sock_release(sock); |
| 1034 | goto out; |
| 1035 | } |
| 1036 | |
| 1037 | if (!xprt->inet) { |
| 1038 | struct sock *sk = sock->sk; |
| 1039 | |
| 1040 | write_lock_bh(&sk->sk_callback_lock); |
| 1041 | |
| 1042 | sk->sk_user_data = xprt; |
| 1043 | xprt->old_data_ready = sk->sk_data_ready; |
| 1044 | xprt->old_state_change = sk->sk_state_change; |
| 1045 | xprt->old_write_space = sk->sk_write_space; |
| 1046 | sk->sk_data_ready = xs_udp_data_ready; |
| 1047 | sk->sk_write_space = xs_udp_write_space; |
| 1048 | sk->sk_no_check = UDP_CSUM_NORCV; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 1049 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1050 | |
| 1051 | xprt_set_connected(xprt); |
| 1052 | |
| 1053 | /* Reset to new socket */ |
| 1054 | xprt->sock = sock; |
| 1055 | xprt->inet = sk; |
| 1056 | |
| 1057 | write_unlock_bh(&sk->sk_callback_lock); |
| 1058 | } |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1059 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1060 | status = 0; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1061 | out: |
| 1062 | xprt_wake_pending_tasks(xprt, status); |
| 1063 | xprt_clear_connecting(xprt); |
| 1064 | } |
| 1065 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1066 | /* |
| 1067 | * We need to preserve the port number so the reply cache on the server can |
| 1068 | * find our cached RPC replies when we get around to reconnecting. |
| 1069 | */ |
| 1070 | static void xs_tcp_reuse_connection(struct rpc_xprt *xprt) |
| 1071 | { |
| 1072 | int result; |
| 1073 | struct socket *sock = xprt->sock; |
| 1074 | struct sockaddr any; |
| 1075 | |
| 1076 | dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); |
| 1077 | |
| 1078 | /* |
| 1079 | * Disconnect the transport socket by doing a connect operation |
| 1080 | * with AF_UNSPEC. This should return immediately... |
| 1081 | */ |
| 1082 | memset(&any, 0, sizeof(any)); |
| 1083 | any.sa_family = AF_UNSPEC; |
| 1084 | result = sock->ops->connect(sock, &any, sizeof(any), 0); |
| 1085 | if (result) |
| 1086 | dprintk("RPC: AF_UNSPEC connect return code %d\n", |
| 1087 | result); |
| 1088 | } |
| 1089 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1090 | /** |
| 1091 | * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint |
| 1092 | * @args: RPC transport to connect |
| 1093 | * |
| 1094 | * Invoked by a work queue tasklet. |
| 1095 | */ |
| 1096 | static void xs_tcp_connect_worker(void *args) |
| 1097 | { |
| 1098 | struct rpc_xprt *xprt = (struct rpc_xprt *)args; |
| 1099 | struct socket *sock = xprt->sock; |
| 1100 | int err, status = -EIO; |
| 1101 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame^] | 1102 | if (xprt->shutdown || !xprt_bound(xprt)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1103 | goto out; |
| 1104 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1105 | dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt); |
| 1106 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1107 | if (!xprt->sock) { |
| 1108 | /* start from scratch */ |
| 1109 | if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { |
| 1110 | dprintk("RPC: can't create TCP transport socket (%d).\n", -err); |
| 1111 | goto out; |
| 1112 | } |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1113 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1114 | if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { |
| 1115 | sock_release(sock); |
| 1116 | goto out; |
| 1117 | } |
| 1118 | } else |
| 1119 | /* "close" the socket, preserving the local port */ |
| 1120 | xs_tcp_reuse_connection(xprt); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1121 | |
| 1122 | if (!xprt->inet) { |
| 1123 | struct sock *sk = sock->sk; |
| 1124 | |
| 1125 | write_lock_bh(&sk->sk_callback_lock); |
| 1126 | |
| 1127 | sk->sk_user_data = xprt; |
| 1128 | xprt->old_data_ready = sk->sk_data_ready; |
| 1129 | xprt->old_state_change = sk->sk_state_change; |
| 1130 | xprt->old_write_space = sk->sk_write_space; |
| 1131 | sk->sk_data_ready = xs_tcp_data_ready; |
| 1132 | sk->sk_state_change = xs_tcp_state_change; |
| 1133 | sk->sk_write_space = xs_tcp_write_space; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 1134 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1135 | |
| 1136 | /* socket options */ |
| 1137 | sk->sk_userlocks |= SOCK_BINDPORT_LOCK; |
| 1138 | sock_reset_flag(sk, SOCK_LINGER); |
| 1139 | tcp_sk(sk)->linger2 = 0; |
| 1140 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1141 | |
| 1142 | xprt_clear_connected(xprt); |
| 1143 | |
| 1144 | /* Reset to new socket */ |
| 1145 | xprt->sock = sock; |
| 1146 | xprt->inet = sk; |
| 1147 | |
| 1148 | write_unlock_bh(&sk->sk_callback_lock); |
| 1149 | } |
| 1150 | |
| 1151 | /* Tell the socket layer to start connecting... */ |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1152 | xprt->stat.connect_count++; |
| 1153 | xprt->stat.connect_start = jiffies; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1154 | status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr, |
| 1155 | sizeof(xprt->addr), O_NONBLOCK); |
| 1156 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 1157 | xprt, -status, xprt_connected(xprt), sock->sk->sk_state); |
| 1158 | if (status < 0) { |
| 1159 | switch (status) { |
| 1160 | case -EINPROGRESS: |
| 1161 | case -EALREADY: |
| 1162 | goto out_clear; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1163 | case -ECONNREFUSED: |
| 1164 | case -ECONNRESET: |
| 1165 | /* retry with existing socket, after a delay */ |
| 1166 | break; |
| 1167 | default: |
| 1168 | /* get rid of existing socket, and retry */ |
| 1169 | xs_close(xprt); |
| 1170 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | out: |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 1174 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1175 | out_clear: |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 1176 | xprt_clear_connecting(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1177 | } |
| 1178 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1179 | /** |
| 1180 | * xs_connect - connect a socket to a remote endpoint |
| 1181 | * @task: address of RPC task that manages state of connect request |
| 1182 | * |
| 1183 | * TCP: If the remote end dropped the connection, delay reconnecting. |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1184 | * |
| 1185 | * UDP socket connects are synchronous, but we use a work queue anyway |
| 1186 | * to guarantee that even unprivileged user processes can set up a |
| 1187 | * socket on a privileged port. |
| 1188 | * |
| 1189 | * If a UDP socket connect fails, the delay behavior here prevents |
| 1190 | * retry floods (hard mounts). |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1191 | */ |
| 1192 | static void xs_connect(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1193 | { |
| 1194 | struct rpc_xprt *xprt = task->tk_xprt; |
| 1195 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1196 | if (xprt_test_and_set_connecting(xprt)) |
| 1197 | return; |
| 1198 | |
| 1199 | if (xprt->sock != NULL) { |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1200 | dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n", |
| 1201 | xprt, xprt->reestablish_timeout / HZ); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1202 | schedule_delayed_work(&xprt->connect_worker, |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1203 | xprt->reestablish_timeout); |
| 1204 | xprt->reestablish_timeout <<= 1; |
| 1205 | if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) |
| 1206 | xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1207 | } else { |
| 1208 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
| 1209 | schedule_work(&xprt->connect_worker); |
| 1210 | |
| 1211 | /* flush_scheduled_work can sleep... */ |
| 1212 | if (!RPC_IS_ASYNC(task)) |
| 1213 | flush_scheduled_work(); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1217 | /** |
| 1218 | * xs_udp_print_stats - display UDP socket-specifc stats |
| 1219 | * @xprt: rpc_xprt struct containing statistics |
| 1220 | * @seq: output file |
| 1221 | * |
| 1222 | */ |
| 1223 | static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 1224 | { |
| 1225 | seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", |
| 1226 | xprt->port, |
| 1227 | xprt->stat.bind_count, |
| 1228 | xprt->stat.sends, |
| 1229 | xprt->stat.recvs, |
| 1230 | xprt->stat.bad_xids, |
| 1231 | xprt->stat.req_u, |
| 1232 | xprt->stat.bklog_u); |
| 1233 | } |
| 1234 | |
| 1235 | /** |
| 1236 | * xs_tcp_print_stats - display TCP socket-specifc stats |
| 1237 | * @xprt: rpc_xprt struct containing statistics |
| 1238 | * @seq: output file |
| 1239 | * |
| 1240 | */ |
| 1241 | static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 1242 | { |
| 1243 | long idle_time = 0; |
| 1244 | |
| 1245 | if (xprt_connected(xprt)) |
| 1246 | idle_time = (long)(jiffies - xprt->last_used) / HZ; |
| 1247 | |
| 1248 | seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", |
| 1249 | xprt->port, |
| 1250 | xprt->stat.bind_count, |
| 1251 | xprt->stat.connect_count, |
| 1252 | xprt->stat.connect_time, |
| 1253 | idle_time, |
| 1254 | xprt->stat.sends, |
| 1255 | xprt->stat.recvs, |
| 1256 | xprt->stat.bad_xids, |
| 1257 | xprt->stat.req_u, |
| 1258 | xprt->stat.bklog_u); |
| 1259 | } |
| 1260 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1261 | static struct rpc_xprt_ops xs_udp_ops = { |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1262 | .set_buffer_size = xs_udp_set_buffer_size, |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1263 | .reserve_xprt = xprt_reserve_xprt_cong, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1264 | .release_xprt = xprt_release_xprt_cong, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1265 | .set_port = xs_set_port, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1266 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 1267 | .buf_alloc = rpc_malloc, |
| 1268 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1269 | .send_request = xs_udp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1270 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1271 | .timer = xs_udp_timer, |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 1272 | .release_request = xprt_release_rqst_cong, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1273 | .close = xs_close, |
| 1274 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1275 | .print_stats = xs_udp_print_stats, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1276 | }; |
| 1277 | |
| 1278 | static struct rpc_xprt_ops xs_tcp_ops = { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1279 | .reserve_xprt = xprt_reserve_xprt, |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1280 | .release_xprt = xs_tcp_release_xprt, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1281 | .set_port = xs_set_port, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1282 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 1283 | .buf_alloc = rpc_malloc, |
| 1284 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1285 | .send_request = xs_tcp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1286 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1287 | .close = xs_close, |
| 1288 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1289 | .print_stats = xs_tcp_print_stats, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1290 | }; |
| 1291 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1292 | /** |
| 1293 | * xs_setup_udp - Set up transport to use a UDP socket |
| 1294 | * @xprt: transport to set up |
| 1295 | * @to: timeout parameters |
| 1296 | * |
| 1297 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1298 | int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to) |
| 1299 | { |
| 1300 | size_t slot_table_size; |
| 1301 | |
| 1302 | dprintk("RPC: setting up udp-ipv4 transport...\n"); |
| 1303 | |
| 1304 | xprt->max_reqs = xprt_udp_slot_table_entries; |
| 1305 | slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1306 | xprt->slot = kzalloc(slot_table_size, GFP_KERNEL); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1307 | if (xprt->slot == NULL) |
| 1308 | return -ENOMEM; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1309 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame^] | 1310 | if (ntohs(xprt->addr.sin_port) != 0) |
| 1311 | xprt_set_bound(xprt); |
Chuck Lever | b85d880 | 2006-05-25 01:40:49 -0400 | [diff] [blame] | 1312 | xprt->port = xs_get_random_port(); |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame^] | 1313 | |
| 1314 | xprt->prot = IPPROTO_UDP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1315 | xprt->tsh_size = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1316 | xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; |
| 1317 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
| 1318 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); |
| 1319 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1320 | INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1321 | xprt->bind_timeout = XS_BIND_TO; |
| 1322 | xprt->connect_timeout = XS_UDP_CONN_TO; |
| 1323 | xprt->reestablish_timeout = XS_UDP_REEST_TO; |
| 1324 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1325 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1326 | xprt->ops = &xs_udp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1327 | |
| 1328 | if (to) |
| 1329 | xprt->timeout = *to; |
| 1330 | else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1331 | xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1332 | |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1336 | /** |
| 1337 | * xs_setup_tcp - Set up transport to use a TCP socket |
| 1338 | * @xprt: transport to set up |
| 1339 | * @to: timeout parameters |
| 1340 | * |
| 1341 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1342 | int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to) |
| 1343 | { |
| 1344 | size_t slot_table_size; |
| 1345 | |
| 1346 | dprintk("RPC: setting up tcp-ipv4 transport...\n"); |
| 1347 | |
| 1348 | xprt->max_reqs = xprt_tcp_slot_table_entries; |
| 1349 | slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1350 | xprt->slot = kzalloc(slot_table_size, GFP_KERNEL); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1351 | if (xprt->slot == NULL) |
| 1352 | return -ENOMEM; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1353 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame^] | 1354 | if (ntohs(xprt->addr.sin_port) != 0) |
| 1355 | xprt_set_bound(xprt); |
Chuck Lever | b85d880 | 2006-05-25 01:40:49 -0400 | [diff] [blame] | 1356 | xprt->port = xs_get_random_port(); |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame^] | 1357 | |
| 1358 | xprt->prot = IPPROTO_TCP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1359 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1360 | xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1361 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1362 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1363 | INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1364 | xprt->bind_timeout = XS_BIND_TO; |
| 1365 | xprt->connect_timeout = XS_TCP_CONN_TO; |
| 1366 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 1367 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1368 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1369 | xprt->ops = &xs_tcp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1370 | |
| 1371 | if (to) |
| 1372 | xprt->timeout = *to; |
| 1373 | else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1374 | xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1375 | |
| 1376 | return 0; |
| 1377 | } |