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