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