]>
Commit | Line | Data |
---|---|---|
a246b010 CL |
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> | |
55aa4f58 CL |
14 | * |
15 | * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> | |
a246b010 CL |
16 | */ |
17 | ||
18 | #include <linux/types.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/capability.h> | |
21 | #include <linux/sched.h> | |
22 | #include <linux/pagemap.h> | |
23 | #include <linux/errno.h> | |
24 | #include <linux/socket.h> | |
25 | #include <linux/in.h> | |
26 | #include <linux/net.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/udp.h> | |
29 | #include <linux/tcp.h> | |
30 | #include <linux/sunrpc/clnt.h> | |
31 | #include <linux/file.h> | |
32 | ||
33 | #include <net/sock.h> | |
34 | #include <net/checksum.h> | |
35 | #include <net/udp.h> | |
36 | #include <net/tcp.h> | |
37 | ||
262965f5 CL |
38 | /* |
39 | * How many times to try sending a request on a socket before waiting | |
40 | * for the socket buffer to clear. | |
41 | */ | |
42 | #define XS_SENDMSG_RETRY (10U) | |
43 | ||
a246b010 CL |
44 | #ifdef RPC_DEBUG |
45 | # undef RPC_DEBUG_DATA | |
9903cd1c | 46 | # define RPCDBG_FACILITY RPCDBG_TRANS |
a246b010 CL |
47 | #endif |
48 | ||
a246b010 | 49 | #ifdef RPC_DEBUG_DATA |
9903cd1c | 50 | static void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
a246b010 | 51 | { |
9903cd1c CL |
52 | u8 *buf = (u8 *) packet; |
53 | int j; | |
a246b010 CL |
54 | |
55 | dprintk("RPC: %s\n", msg); | |
56 | for (j = 0; j < count && j < 128; j += 4) { | |
57 | if (!(j & 31)) { | |
58 | if (j) | |
59 | dprintk("\n"); | |
60 | dprintk("0x%04x ", j); | |
61 | } | |
62 | dprintk("%02x%02x%02x%02x ", | |
63 | buf[j], buf[j+1], buf[j+2], buf[j+3]); | |
64 | } | |
65 | dprintk("\n"); | |
66 | } | |
67 | #else | |
9903cd1c | 68 | static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
a246b010 CL |
69 | { |
70 | /* NOP */ | |
71 | } | |
72 | #endif | |
73 | ||
b4b5cc85 CL |
74 | #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) |
75 | ||
76 | static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len) | |
77 | { | |
78 | struct kvec iov = { | |
79 | .iov_base = xdr->head[0].iov_base + base, | |
80 | .iov_len = len - base, | |
81 | }; | |
82 | struct msghdr msg = { | |
83 | .msg_name = addr, | |
84 | .msg_namelen = addrlen, | |
85 | .msg_flags = XS_SENDMSG_FLAGS, | |
86 | }; | |
87 | ||
88 | if (xdr->len > len) | |
89 | msg.msg_flags |= MSG_MORE; | |
90 | ||
91 | if (likely(iov.iov_len)) | |
92 | return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); | |
93 | return kernel_sendmsg(sock, &msg, NULL, 0, 0); | |
94 | } | |
95 | ||
96 | static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len) | |
97 | { | |
98 | struct kvec iov = { | |
99 | .iov_base = xdr->tail[0].iov_base + base, | |
100 | .iov_len = len - base, | |
101 | }; | |
102 | struct msghdr msg = { | |
103 | .msg_flags = XS_SENDMSG_FLAGS, | |
104 | }; | |
105 | ||
106 | return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); | |
107 | } | |
108 | ||
9903cd1c CL |
109 | /** |
110 | * xs_sendpages - write pages directly to a socket | |
111 | * @sock: socket to send on | |
112 | * @addr: UDP only -- address of destination | |
113 | * @addrlen: UDP only -- length of destination address | |
114 | * @xdr: buffer containing this request | |
115 | * @base: starting position in the buffer | |
116 | * | |
a246b010 | 117 | */ |
262965f5 | 118 | static inline int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) |
a246b010 CL |
119 | { |
120 | struct page **ppage = xdr->pages; | |
121 | unsigned int len, pglen = xdr->page_len; | |
122 | int err, ret = 0; | |
123 | ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int); | |
124 | ||
262965f5 CL |
125 | if (unlikely(!sock)) |
126 | return -ENOTCONN; | |
127 | ||
128 | clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); | |
129 | ||
a246b010 CL |
130 | len = xdr->head[0].iov_len; |
131 | if (base < len || (addr != NULL && base == 0)) { | |
b4b5cc85 | 132 | err = xs_send_head(sock, addr, addrlen, xdr, base, len); |
a246b010 CL |
133 | if (ret == 0) |
134 | ret = err; | |
135 | else if (err > 0) | |
136 | ret += err; | |
b4b5cc85 | 137 | if (err != (len - base)) |
a246b010 CL |
138 | goto out; |
139 | base = 0; | |
140 | } else | |
141 | base -= len; | |
142 | ||
b4b5cc85 | 143 | if (unlikely(pglen == 0)) |
a246b010 | 144 | goto copy_tail; |
b4b5cc85 | 145 | if (unlikely(base >= pglen)) { |
a246b010 CL |
146 | base -= pglen; |
147 | goto copy_tail; | |
148 | } | |
149 | if (base || xdr->page_base) { | |
150 | pglen -= base; | |
9903cd1c | 151 | base += xdr->page_base; |
a246b010 CL |
152 | ppage += base >> PAGE_CACHE_SHIFT; |
153 | base &= ~PAGE_CACHE_MASK; | |
154 | } | |
155 | ||
156 | sendpage = sock->ops->sendpage ? : sock_no_sendpage; | |
157 | do { | |
b4b5cc85 | 158 | int flags = XS_SENDMSG_FLAGS; |
a246b010 CL |
159 | |
160 | len = PAGE_CACHE_SIZE; | |
161 | if (base) | |
162 | len -= base; | |
163 | if (pglen < len) | |
164 | len = pglen; | |
165 | ||
166 | if (pglen != len || xdr->tail[0].iov_len != 0) | |
167 | flags |= MSG_MORE; | |
168 | ||
169 | /* Hmm... We might be dealing with highmem pages */ | |
170 | if (PageHighMem(*ppage)) | |
171 | sendpage = sock_no_sendpage; | |
172 | err = sendpage(sock, *ppage, base, len, flags); | |
173 | if (ret == 0) | |
174 | ret = err; | |
175 | else if (err > 0) | |
176 | ret += err; | |
177 | if (err != len) | |
178 | goto out; | |
179 | base = 0; | |
180 | ppage++; | |
181 | } while ((pglen -= len) != 0); | |
182 | copy_tail: | |
183 | len = xdr->tail[0].iov_len; | |
184 | if (base < len) { | |
b4b5cc85 | 185 | err = xs_send_tail(sock, xdr, base, len); |
a246b010 CL |
186 | if (ret == 0) |
187 | ret = err; | |
188 | else if (err > 0) | |
189 | ret += err; | |
190 | } | |
191 | out: | |
192 | return ret; | |
193 | } | |
194 | ||
9903cd1c | 195 | /** |
262965f5 CL |
196 | * xs_nospace - place task on wait queue if transmit was incomplete |
197 | * @task: task to put to sleep | |
9903cd1c | 198 | * |
a246b010 | 199 | */ |
262965f5 | 200 | static void xs_nospace(struct rpc_task *task) |
a246b010 | 201 | { |
262965f5 CL |
202 | struct rpc_rqst *req = task->tk_rqstp; |
203 | struct rpc_xprt *xprt = req->rq_xprt; | |
a246b010 | 204 | |
262965f5 CL |
205 | dprintk("RPC: %4d xmit incomplete (%u left of %u)\n", |
206 | task->tk_pid, req->rq_slen - req->rq_bytes_sent, | |
207 | req->rq_slen); | |
208 | ||
209 | if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) { | |
210 | /* Protect against races with write_space */ | |
211 | spin_lock_bh(&xprt->transport_lock); | |
212 | ||
213 | /* Don't race with disconnect */ | |
214 | if (!xprt_connected(xprt)) | |
215 | task->tk_status = -ENOTCONN; | |
216 | else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags)) | |
217 | xprt_wait_for_buffer_space(task); | |
218 | ||
219 | spin_unlock_bh(&xprt->transport_lock); | |
220 | } else | |
221 | /* Keep holding the socket if it is blocked */ | |
222 | rpc_delay(task, HZ>>4); | |
223 | } | |
224 | ||
225 | /** | |
226 | * xs_udp_send_request - write an RPC request to a UDP socket | |
227 | * @task: address of RPC task that manages the state of an RPC request | |
228 | * | |
229 | * Return values: | |
230 | * 0: The request has been sent | |
231 | * EAGAIN: The socket was blocked, please call again later to | |
232 | * complete the request | |
233 | * ENOTCONN: Caller needs to invoke connect logic then call again | |
234 | * other: Some other error occured, the request was not sent | |
235 | */ | |
236 | static int xs_udp_send_request(struct rpc_task *task) | |
237 | { | |
238 | struct rpc_rqst *req = task->tk_rqstp; | |
239 | struct rpc_xprt *xprt = req->rq_xprt; | |
240 | struct xdr_buf *xdr = &req->rq_snd_buf; | |
241 | int status; | |
a246b010 | 242 | |
9903cd1c | 243 | xs_pktdump("packet data:", |
a246b010 CL |
244 | req->rq_svec->iov_base, |
245 | req->rq_svec->iov_len); | |
246 | ||
262965f5 CL |
247 | req->rq_xtime = jiffies; |
248 | status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr, | |
249 | sizeof(xprt->addr), xdr, req->rq_bytes_sent); | |
a246b010 | 250 | |
262965f5 CL |
251 | dprintk("RPC: xs_udp_send_request(%u) = %d\n", |
252 | xdr->len - req->rq_bytes_sent, status); | |
a246b010 | 253 | |
262965f5 CL |
254 | if (likely(status >= (int) req->rq_slen)) |
255 | return 0; | |
a246b010 | 256 | |
262965f5 CL |
257 | /* Still some bytes left; set up for a retry later. */ |
258 | if (status > 0) | |
259 | status = -EAGAIN; | |
a246b010 | 260 | |
262965f5 CL |
261 | switch (status) { |
262 | case -ENETUNREACH: | |
263 | case -EPIPE: | |
a246b010 CL |
264 | case -ECONNREFUSED: |
265 | /* When the server has died, an ICMP port unreachable message | |
9903cd1c | 266 | * prompts ECONNREFUSED. */ |
a246b010 | 267 | break; |
262965f5 CL |
268 | case -EAGAIN: |
269 | xs_nospace(task); | |
a246b010 CL |
270 | break; |
271 | default: | |
262965f5 CL |
272 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
273 | -status); | |
9903cd1c | 274 | break; |
a246b010 | 275 | } |
262965f5 CL |
276 | |
277 | return status; | |
a246b010 CL |
278 | } |
279 | ||
808012fb CL |
280 | static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) |
281 | { | |
282 | u32 reclen = buf->len - sizeof(rpc_fraghdr); | |
283 | rpc_fraghdr *base = buf->head[0].iov_base; | |
284 | *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); | |
285 | } | |
286 | ||
9903cd1c | 287 | /** |
262965f5 | 288 | * xs_tcp_send_request - write an RPC request to a TCP socket |
9903cd1c CL |
289 | * @task: address of RPC task that manages the state of an RPC request |
290 | * | |
291 | * Return values: | |
262965f5 CL |
292 | * 0: The request has been sent |
293 | * EAGAIN: The socket was blocked, please call again later to | |
294 | * complete the request | |
295 | * ENOTCONN: Caller needs to invoke connect logic then call again | |
296 | * other: Some other error occured, the request was not sent | |
9903cd1c CL |
297 | * |
298 | * XXX: In the case of soft timeouts, should we eventually give up | |
262965f5 | 299 | * if sendmsg is not able to make progress? |
9903cd1c | 300 | */ |
262965f5 | 301 | static int xs_tcp_send_request(struct rpc_task *task) |
a246b010 CL |
302 | { |
303 | struct rpc_rqst *req = task->tk_rqstp; | |
304 | struct rpc_xprt *xprt = req->rq_xprt; | |
262965f5 | 305 | struct xdr_buf *xdr = &req->rq_snd_buf; |
a246b010 CL |
306 | int status, retry = 0; |
307 | ||
808012fb | 308 | xs_encode_tcp_record_marker(&req->rq_snd_buf); |
a246b010 | 309 | |
262965f5 CL |
310 | xs_pktdump("packet data:", |
311 | req->rq_svec->iov_base, | |
312 | req->rq_svec->iov_len); | |
a246b010 CL |
313 | |
314 | /* Continue transmitting the packet/record. We must be careful | |
315 | * to cope with writespace callbacks arriving _after_ we have | |
262965f5 | 316 | * called sendmsg(). */ |
a246b010 CL |
317 | while (1) { |
318 | req->rq_xtime = jiffies; | |
262965f5 CL |
319 | status = xs_sendpages(xprt->sock, NULL, 0, xdr, |
320 | req->rq_bytes_sent); | |
a246b010 | 321 | |
262965f5 CL |
322 | dprintk("RPC: xs_tcp_send_request(%u) = %d\n", |
323 | xdr->len - req->rq_bytes_sent, status); | |
a246b010 | 324 | |
262965f5 | 325 | if (unlikely(status < 0)) |
a246b010 | 326 | break; |
a246b010 | 327 | |
262965f5 CL |
328 | /* If we've sent the entire packet, immediately |
329 | * reset the count of bytes sent. */ | |
330 | req->rq_bytes_sent += status; | |
331 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { | |
332 | req->rq_bytes_sent = 0; | |
333 | return 0; | |
334 | } | |
a246b010 CL |
335 | |
336 | status = -EAGAIN; | |
262965f5 | 337 | if (retry++ > XS_SENDMSG_RETRY) |
a246b010 CL |
338 | break; |
339 | } | |
340 | ||
262965f5 CL |
341 | switch (status) { |
342 | case -EAGAIN: | |
343 | xs_nospace(task); | |
344 | break; | |
345 | case -ECONNREFUSED: | |
346 | case -ECONNRESET: | |
347 | case -ENOTCONN: | |
348 | case -EPIPE: | |
349 | status = -ENOTCONN; | |
350 | break; | |
351 | default: | |
352 | dprintk("RPC: sendmsg returned unrecognized error %d\n", | |
353 | -status); | |
43118c29 | 354 | xprt_disconnect(xprt); |
262965f5 | 355 | break; |
a246b010 | 356 | } |
262965f5 | 357 | |
a246b010 CL |
358 | return status; |
359 | } | |
360 | ||
9903cd1c CL |
361 | /** |
362 | * xs_close - close a socket | |
363 | * @xprt: transport | |
364 | * | |
a246b010 | 365 | */ |
9903cd1c | 366 | static void xs_close(struct rpc_xprt *xprt) |
a246b010 | 367 | { |
9903cd1c CL |
368 | struct socket *sock = xprt->sock; |
369 | struct sock *sk = xprt->inet; | |
a246b010 CL |
370 | |
371 | if (!sk) | |
372 | return; | |
373 | ||
9903cd1c CL |
374 | dprintk("RPC: xs_close xprt %p\n", xprt); |
375 | ||
a246b010 CL |
376 | write_lock_bh(&sk->sk_callback_lock); |
377 | xprt->inet = NULL; | |
378 | xprt->sock = NULL; | |
379 | ||
9903cd1c CL |
380 | sk->sk_user_data = NULL; |
381 | sk->sk_data_ready = xprt->old_data_ready; | |
a246b010 | 382 | sk->sk_state_change = xprt->old_state_change; |
9903cd1c | 383 | sk->sk_write_space = xprt->old_write_space; |
a246b010 CL |
384 | write_unlock_bh(&sk->sk_callback_lock); |
385 | ||
9903cd1c | 386 | sk->sk_no_check = 0; |
a246b010 CL |
387 | |
388 | sock_release(sock); | |
389 | } | |
390 | ||
9903cd1c CL |
391 | /** |
392 | * xs_destroy - prepare to shutdown a transport | |
393 | * @xprt: doomed transport | |
394 | * | |
395 | */ | |
396 | static void xs_destroy(struct rpc_xprt *xprt) | |
a246b010 | 397 | { |
9903cd1c CL |
398 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
399 | ||
55aa4f58 | 400 | cancel_delayed_work(&xprt->connect_worker); |
a246b010 CL |
401 | flush_scheduled_work(); |
402 | ||
403 | xprt_disconnect(xprt); | |
9903cd1c | 404 | xs_close(xprt); |
a246b010 CL |
405 | kfree(xprt->slot); |
406 | } | |
407 | ||
9903cd1c CL |
408 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
409 | { | |
410 | return (struct rpc_xprt *) sk->sk_user_data; | |
411 | } | |
412 | ||
413 | /** | |
414 | * xs_udp_data_ready - "data ready" callback for UDP sockets | |
415 | * @sk: socket with data to read | |
416 | * @len: how much data to read | |
417 | * | |
a246b010 | 418 | */ |
9903cd1c | 419 | static void xs_udp_data_ready(struct sock *sk, int len) |
a246b010 | 420 | { |
9903cd1c CL |
421 | struct rpc_task *task; |
422 | struct rpc_xprt *xprt; | |
a246b010 | 423 | struct rpc_rqst *rovr; |
9903cd1c | 424 | struct sk_buff *skb; |
a246b010 CL |
425 | int err, repsize, copied; |
426 | u32 _xid, *xp; | |
427 | ||
428 | read_lock(&sk->sk_callback_lock); | |
9903cd1c CL |
429 | dprintk("RPC: xs_udp_data_ready...\n"); |
430 | if (!(xprt = xprt_from_sock(sk))) | |
a246b010 | 431 | goto out; |
a246b010 CL |
432 | |
433 | if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) | |
434 | goto out; | |
435 | ||
436 | if (xprt->shutdown) | |
437 | goto dropit; | |
438 | ||
439 | repsize = skb->len - sizeof(struct udphdr); | |
440 | if (repsize < 4) { | |
9903cd1c | 441 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
a246b010 CL |
442 | goto dropit; |
443 | } | |
444 | ||
445 | /* Copy the XID from the skb... */ | |
446 | xp = skb_header_pointer(skb, sizeof(struct udphdr), | |
447 | sizeof(_xid), &_xid); | |
448 | if (xp == NULL) | |
449 | goto dropit; | |
450 | ||
451 | /* Look up and lock the request corresponding to the given XID */ | |
4a0f8c04 | 452 | spin_lock(&xprt->transport_lock); |
a246b010 CL |
453 | rovr = xprt_lookup_rqst(xprt, *xp); |
454 | if (!rovr) | |
455 | goto out_unlock; | |
456 | task = rovr->rq_task; | |
457 | ||
a246b010 CL |
458 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
459 | copied = repsize; | |
460 | ||
461 | /* Suck it into the iovec, verify checksum if not done by hw. */ | |
462 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) | |
463 | goto out_unlock; | |
464 | ||
465 | /* Something worked... */ | |
466 | dst_confirm(skb->dst); | |
467 | ||
1570c1e4 CL |
468 | xprt_adjust_cwnd(task, copied); |
469 | xprt_update_rtt(task); | |
470 | xprt_complete_rqst(task, copied); | |
a246b010 CL |
471 | |
472 | out_unlock: | |
4a0f8c04 | 473 | spin_unlock(&xprt->transport_lock); |
a246b010 CL |
474 | dropit: |
475 | skb_free_datagram(sk, skb); | |
476 | out: | |
477 | read_unlock(&sk->sk_callback_lock); | |
478 | } | |
479 | ||
9903cd1c | 480 | static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len) |
a246b010 CL |
481 | { |
482 | if (len > desc->count) | |
483 | len = desc->count; | |
484 | if (skb_copy_bits(desc->skb, desc->offset, p, len)) { | |
485 | dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n", | |
486 | len, desc->count); | |
487 | return 0; | |
488 | } | |
489 | desc->offset += len; | |
490 | desc->count -= len; | |
491 | dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n", | |
492 | len, desc->count); | |
493 | return len; | |
494 | } | |
495 | ||
9903cd1c | 496 | static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc) |
a246b010 CL |
497 | { |
498 | size_t len, used; | |
499 | char *p; | |
500 | ||
501 | p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset; | |
502 | len = sizeof(xprt->tcp_recm) - xprt->tcp_offset; | |
9903cd1c | 503 | used = xs_tcp_copy_data(desc, p, len); |
a246b010 CL |
504 | xprt->tcp_offset += used; |
505 | if (used != len) | |
506 | return; | |
808012fb | 507 | |
a246b010 | 508 | xprt->tcp_reclen = ntohl(xprt->tcp_recm); |
808012fb | 509 | if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) |
a246b010 CL |
510 | xprt->tcp_flags |= XPRT_LAST_FRAG; |
511 | else | |
512 | xprt->tcp_flags &= ~XPRT_LAST_FRAG; | |
808012fb CL |
513 | xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; |
514 | ||
a246b010 CL |
515 | xprt->tcp_flags &= ~XPRT_COPY_RECM; |
516 | xprt->tcp_offset = 0; | |
808012fb | 517 | |
a246b010 | 518 | /* Sanity check of the record length */ |
808012fb | 519 | if (unlikely(xprt->tcp_reclen < 4)) { |
9903cd1c | 520 | dprintk("RPC: invalid TCP record fragment length\n"); |
a246b010 | 521 | xprt_disconnect(xprt); |
9903cd1c | 522 | return; |
a246b010 CL |
523 | } |
524 | dprintk("RPC: reading TCP record fragment of length %d\n", | |
525 | xprt->tcp_reclen); | |
526 | } | |
527 | ||
9903cd1c | 528 | static void xs_tcp_check_recm(struct rpc_xprt *xprt) |
a246b010 CL |
529 | { |
530 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n", | |
531 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags); | |
532 | if (xprt->tcp_offset == xprt->tcp_reclen) { | |
533 | xprt->tcp_flags |= XPRT_COPY_RECM; | |
534 | xprt->tcp_offset = 0; | |
535 | if (xprt->tcp_flags & XPRT_LAST_FRAG) { | |
536 | xprt->tcp_flags &= ~XPRT_COPY_DATA; | |
537 | xprt->tcp_flags |= XPRT_COPY_XID; | |
538 | xprt->tcp_copied = 0; | |
539 | } | |
540 | } | |
541 | } | |
542 | ||
9903cd1c | 543 | static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc) |
a246b010 CL |
544 | { |
545 | size_t len, used; | |
546 | char *p; | |
547 | ||
548 | len = sizeof(xprt->tcp_xid) - xprt->tcp_offset; | |
549 | dprintk("RPC: reading XID (%Zu bytes)\n", len); | |
550 | p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset; | |
9903cd1c | 551 | used = xs_tcp_copy_data(desc, p, len); |
a246b010 CL |
552 | xprt->tcp_offset += used; |
553 | if (used != len) | |
554 | return; | |
555 | xprt->tcp_flags &= ~XPRT_COPY_XID; | |
556 | xprt->tcp_flags |= XPRT_COPY_DATA; | |
557 | xprt->tcp_copied = 4; | |
558 | dprintk("RPC: reading reply for XID %08x\n", | |
559 | ntohl(xprt->tcp_xid)); | |
9903cd1c | 560 | xs_tcp_check_recm(xprt); |
a246b010 CL |
561 | } |
562 | ||
9903cd1c | 563 | static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc) |
a246b010 CL |
564 | { |
565 | struct rpc_rqst *req; | |
566 | struct xdr_buf *rcvbuf; | |
567 | size_t len; | |
568 | ssize_t r; | |
569 | ||
570 | /* Find and lock the request corresponding to this xid */ | |
4a0f8c04 | 571 | spin_lock(&xprt->transport_lock); |
a246b010 CL |
572 | req = xprt_lookup_rqst(xprt, xprt->tcp_xid); |
573 | if (!req) { | |
574 | xprt->tcp_flags &= ~XPRT_COPY_DATA; | |
575 | dprintk("RPC: XID %08x request not found!\n", | |
576 | ntohl(xprt->tcp_xid)); | |
4a0f8c04 | 577 | spin_unlock(&xprt->transport_lock); |
a246b010 CL |
578 | return; |
579 | } | |
580 | ||
581 | rcvbuf = &req->rq_private_buf; | |
582 | len = desc->count; | |
583 | if (len > xprt->tcp_reclen - xprt->tcp_offset) { | |
584 | skb_reader_t my_desc; | |
585 | ||
586 | len = xprt->tcp_reclen - xprt->tcp_offset; | |
587 | memcpy(&my_desc, desc, sizeof(my_desc)); | |
588 | my_desc.count = len; | |
589 | r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied, | |
9903cd1c | 590 | &my_desc, xs_tcp_copy_data); |
a246b010 CL |
591 | desc->count -= r; |
592 | desc->offset += r; | |
593 | } else | |
594 | r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied, | |
9903cd1c | 595 | desc, xs_tcp_copy_data); |
a246b010 CL |
596 | |
597 | if (r > 0) { | |
598 | xprt->tcp_copied += r; | |
599 | xprt->tcp_offset += r; | |
600 | } | |
601 | if (r != len) { | |
602 | /* Error when copying to the receive buffer, | |
603 | * usually because we weren't able to allocate | |
604 | * additional buffer pages. All we can do now | |
605 | * is turn off XPRT_COPY_DATA, so the request | |
606 | * will not receive any additional updates, | |
607 | * and time out. | |
608 | * Any remaining data from this record will | |
609 | * be discarded. | |
610 | */ | |
611 | xprt->tcp_flags &= ~XPRT_COPY_DATA; | |
612 | dprintk("RPC: XID %08x truncated request\n", | |
613 | ntohl(xprt->tcp_xid)); | |
614 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n", | |
615 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen); | |
616 | goto out; | |
617 | } | |
618 | ||
619 | dprintk("RPC: XID %08x read %Zd bytes\n", | |
620 | ntohl(xprt->tcp_xid), r); | |
621 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n", | |
622 | xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen); | |
623 | ||
624 | if (xprt->tcp_copied == req->rq_private_buf.buflen) | |
625 | xprt->tcp_flags &= ~XPRT_COPY_DATA; | |
626 | else if (xprt->tcp_offset == xprt->tcp_reclen) { | |
627 | if (xprt->tcp_flags & XPRT_LAST_FRAG) | |
628 | xprt->tcp_flags &= ~XPRT_COPY_DATA; | |
629 | } | |
630 | ||
631 | out: | |
1570c1e4 CL |
632 | if (!(xprt->tcp_flags & XPRT_COPY_DATA)) |
633 | xprt_complete_rqst(req->rq_task, xprt->tcp_copied); | |
4a0f8c04 | 634 | spin_unlock(&xprt->transport_lock); |
9903cd1c | 635 | xs_tcp_check_recm(xprt); |
a246b010 CL |
636 | } |
637 | ||
9903cd1c | 638 | static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc) |
a246b010 CL |
639 | { |
640 | size_t len; | |
641 | ||
642 | len = xprt->tcp_reclen - xprt->tcp_offset; | |
643 | if (len > desc->count) | |
644 | len = desc->count; | |
645 | desc->count -= len; | |
646 | desc->offset += len; | |
647 | xprt->tcp_offset += len; | |
648 | dprintk("RPC: discarded %Zu bytes\n", len); | |
9903cd1c | 649 | xs_tcp_check_recm(xprt); |
a246b010 CL |
650 | } |
651 | ||
9903cd1c | 652 | static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) |
a246b010 CL |
653 | { |
654 | struct rpc_xprt *xprt = rd_desc->arg.data; | |
655 | skb_reader_t desc = { | |
656 | .skb = skb, | |
657 | .offset = offset, | |
658 | .count = len, | |
659 | .csum = 0 | |
9903cd1c | 660 | }; |
a246b010 | 661 | |
9903cd1c | 662 | dprintk("RPC: xs_tcp_data_recv started\n"); |
a246b010 CL |
663 | do { |
664 | /* Read in a new fragment marker if necessary */ | |
665 | /* Can we ever really expect to get completely empty fragments? */ | |
666 | if (xprt->tcp_flags & XPRT_COPY_RECM) { | |
9903cd1c | 667 | xs_tcp_read_fraghdr(xprt, &desc); |
a246b010 CL |
668 | continue; |
669 | } | |
670 | /* Read in the xid if necessary */ | |
671 | if (xprt->tcp_flags & XPRT_COPY_XID) { | |
9903cd1c | 672 | xs_tcp_read_xid(xprt, &desc); |
a246b010 CL |
673 | continue; |
674 | } | |
675 | /* Read in the request data */ | |
676 | if (xprt->tcp_flags & XPRT_COPY_DATA) { | |
9903cd1c | 677 | xs_tcp_read_request(xprt, &desc); |
a246b010 CL |
678 | continue; |
679 | } | |
680 | /* Skip over any trailing bytes on short reads */ | |
9903cd1c | 681 | xs_tcp_read_discard(xprt, &desc); |
a246b010 | 682 | } while (desc.count); |
9903cd1c | 683 | dprintk("RPC: xs_tcp_data_recv done\n"); |
a246b010 CL |
684 | return len - desc.count; |
685 | } | |
686 | ||
9903cd1c CL |
687 | /** |
688 | * xs_tcp_data_ready - "data ready" callback for TCP sockets | |
689 | * @sk: socket with data to read | |
690 | * @bytes: how much data to read | |
691 | * | |
692 | */ | |
693 | static void xs_tcp_data_ready(struct sock *sk, int bytes) | |
a246b010 CL |
694 | { |
695 | struct rpc_xprt *xprt; | |
696 | read_descriptor_t rd_desc; | |
697 | ||
698 | read_lock(&sk->sk_callback_lock); | |
9903cd1c CL |
699 | dprintk("RPC: xs_tcp_data_ready...\n"); |
700 | if (!(xprt = xprt_from_sock(sk))) | |
a246b010 | 701 | goto out; |
a246b010 CL |
702 | if (xprt->shutdown) |
703 | goto out; | |
704 | ||
9903cd1c | 705 | /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ |
a246b010 CL |
706 | rd_desc.arg.data = xprt; |
707 | rd_desc.count = 65536; | |
9903cd1c | 708 | tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); |
a246b010 CL |
709 | out: |
710 | read_unlock(&sk->sk_callback_lock); | |
711 | } | |
712 | ||
9903cd1c CL |
713 | /** |
714 | * xs_tcp_state_change - callback to handle TCP socket state changes | |
715 | * @sk: socket whose state has changed | |
716 | * | |
717 | */ | |
718 | static void xs_tcp_state_change(struct sock *sk) | |
a246b010 | 719 | { |
9903cd1c | 720 | struct rpc_xprt *xprt; |
a246b010 CL |
721 | |
722 | read_lock(&sk->sk_callback_lock); | |
723 | if (!(xprt = xprt_from_sock(sk))) | |
724 | goto out; | |
9903cd1c | 725 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
a246b010 CL |
726 | dprintk("RPC: state %x conn %d dead %d zapped %d\n", |
727 | sk->sk_state, xprt_connected(xprt), | |
728 | sock_flag(sk, SOCK_DEAD), | |
729 | sock_flag(sk, SOCK_ZAPPED)); | |
730 | ||
731 | switch (sk->sk_state) { | |
732 | case TCP_ESTABLISHED: | |
4a0f8c04 | 733 | spin_lock_bh(&xprt->transport_lock); |
a246b010 CL |
734 | if (!xprt_test_and_set_connected(xprt)) { |
735 | /* Reset TCP record info */ | |
736 | xprt->tcp_offset = 0; | |
737 | xprt->tcp_reclen = 0; | |
738 | xprt->tcp_copied = 0; | |
739 | xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID; | |
44fbac22 | 740 | xprt_wake_pending_tasks(xprt, 0); |
a246b010 | 741 | } |
4a0f8c04 | 742 | spin_unlock_bh(&xprt->transport_lock); |
a246b010 CL |
743 | break; |
744 | case TCP_SYN_SENT: | |
745 | case TCP_SYN_RECV: | |
746 | break; | |
747 | default: | |
748 | xprt_disconnect(xprt); | |
749 | break; | |
750 | } | |
751 | out: | |
752 | read_unlock(&sk->sk_callback_lock); | |
753 | } | |
754 | ||
9903cd1c | 755 | /** |
c7b2cae8 CL |
756 | * xs_udp_write_space - callback invoked when socket buffer space |
757 | * becomes available | |
9903cd1c CL |
758 | * @sk: socket whose state has changed |
759 | * | |
a246b010 CL |
760 | * Called when more output buffer space is available for this socket. |
761 | * We try not to wake our writers until they can make "significant" | |
c7b2cae8 | 762 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
a246b010 CL |
763 | * with a bunch of small requests. |
764 | */ | |
c7b2cae8 | 765 | static void xs_udp_write_space(struct sock *sk) |
a246b010 | 766 | { |
a246b010 | 767 | read_lock(&sk->sk_callback_lock); |
a246b010 | 768 | |
c7b2cae8 CL |
769 | /* from net/core/sock.c:sock_def_write_space */ |
770 | if (sock_writeable(sk)) { | |
771 | struct socket *sock; | |
772 | struct rpc_xprt *xprt; | |
773 | ||
774 | if (unlikely(!(sock = sk->sk_socket))) | |
a246b010 | 775 | goto out; |
c7b2cae8 CL |
776 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
777 | goto out; | |
778 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) | |
a246b010 | 779 | goto out; |
c7b2cae8 CL |
780 | |
781 | xprt_write_space(xprt); | |
a246b010 CL |
782 | } |
783 | ||
c7b2cae8 CL |
784 | out: |
785 | read_unlock(&sk->sk_callback_lock); | |
786 | } | |
a246b010 | 787 | |
c7b2cae8 CL |
788 | /** |
789 | * xs_tcp_write_space - callback invoked when socket buffer space | |
790 | * becomes available | |
791 | * @sk: socket whose state has changed | |
792 | * | |
793 | * Called when more output buffer space is available for this socket. | |
794 | * We try not to wake our writers until they can make "significant" | |
795 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg | |
796 | * with a bunch of small requests. | |
797 | */ | |
798 | static void xs_tcp_write_space(struct sock *sk) | |
799 | { | |
800 | read_lock(&sk->sk_callback_lock); | |
801 | ||
802 | /* from net/core/stream.c:sk_stream_write_space */ | |
803 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { | |
804 | struct socket *sock; | |
805 | struct rpc_xprt *xprt; | |
806 | ||
807 | if (unlikely(!(sock = sk->sk_socket))) | |
808 | goto out; | |
809 | if (unlikely(!(xprt = xprt_from_sock(sk)))) | |
810 | goto out; | |
811 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) | |
812 | goto out; | |
813 | ||
814 | xprt_write_space(xprt); | |
815 | } | |
816 | ||
817 | out: | |
a246b010 CL |
818 | read_unlock(&sk->sk_callback_lock); |
819 | } | |
820 | ||
9903cd1c | 821 | /** |
43118c29 | 822 | * xs_udp_set_buffer_size - set send and receive limits |
9903cd1c CL |
823 | * @xprt: generic transport |
824 | * | |
825 | * Set socket send and receive limits based on the | |
826 | * sndsize and rcvsize fields in the generic transport | |
43118c29 | 827 | * structure. |
a246b010 | 828 | */ |
43118c29 | 829 | static void xs_udp_set_buffer_size(struct rpc_xprt *xprt) |
a246b010 CL |
830 | { |
831 | struct sock *sk = xprt->inet; | |
832 | ||
a246b010 CL |
833 | if (xprt->rcvsize) { |
834 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; | |
835 | sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2; | |
836 | } | |
837 | if (xprt->sndsize) { | |
838 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; | |
839 | sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2; | |
840 | sk->sk_write_space(sk); | |
841 | } | |
842 | } | |
843 | ||
43118c29 CL |
844 | /** |
845 | * xs_tcp_set_buffer_size - set send and receive limits | |
846 | * @xprt: generic transport | |
847 | * | |
848 | * Nothing to do for TCP. | |
849 | */ | |
850 | static void xs_tcp_set_buffer_size(struct rpc_xprt *xprt) | |
851 | { | |
852 | return; | |
853 | } | |
854 | ||
46c0ee8b CL |
855 | /** |
856 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport | |
857 | * @task: task that timed out | |
858 | * | |
859 | * Adjust the congestion window after a retransmit timeout has occurred. | |
860 | */ | |
861 | static void xs_udp_timer(struct rpc_task *task) | |
862 | { | |
863 | xprt_adjust_cwnd(task, -ETIMEDOUT); | |
864 | } | |
865 | ||
9903cd1c | 866 | static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock) |
a246b010 CL |
867 | { |
868 | struct sockaddr_in myaddr = { | |
869 | .sin_family = AF_INET, | |
870 | }; | |
529b33c6 CL |
871 | int err; |
872 | unsigned short port = xprt->port; | |
a246b010 | 873 | |
a246b010 CL |
874 | do { |
875 | myaddr.sin_port = htons(port); | |
876 | err = sock->ops->bind(sock, (struct sockaddr *) &myaddr, | |
877 | sizeof(myaddr)); | |
878 | if (err == 0) { | |
879 | xprt->port = port; | |
9903cd1c CL |
880 | dprintk("RPC: xs_bindresvport bound to port %u\n", |
881 | port); | |
a246b010 CL |
882 | return 0; |
883 | } | |
529b33c6 CL |
884 | if (port <= xprt_min_resvport) |
885 | port = xprt_max_resvport; | |
886 | else | |
887 | port--; | |
a246b010 CL |
888 | } while (err == -EADDRINUSE && port != xprt->port); |
889 | ||
9903cd1c | 890 | dprintk("RPC: can't bind to reserved port (%d).\n", -err); |
a246b010 CL |
891 | return err; |
892 | } | |
893 | ||
b0d93ad5 CL |
894 | /** |
895 | * xs_udp_connect_worker - set up a UDP socket | |
896 | * @args: RPC transport to connect | |
897 | * | |
898 | * Invoked by a work queue tasklet. | |
899 | */ | |
900 | static void xs_udp_connect_worker(void *args) | |
a246b010 | 901 | { |
b0d93ad5 CL |
902 | struct rpc_xprt *xprt = (struct rpc_xprt *) args; |
903 | struct socket *sock = xprt->sock; | |
904 | int err, status = -EIO; | |
9903cd1c | 905 | |
b0d93ad5 CL |
906 | if (xprt->shutdown || xprt->addr.sin_port == 0) |
907 | goto out; | |
9903cd1c | 908 | |
b0d93ad5 | 909 | dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt); |
9903cd1c | 910 | |
b0d93ad5 CL |
911 | /* Start by resetting any existing state */ |
912 | xs_close(xprt); | |
9903cd1c | 913 | |
b0d93ad5 CL |
914 | if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { |
915 | dprintk("RPC: can't create UDP transport socket (%d).\n", -err); | |
916 | goto out; | |
917 | } | |
9903cd1c | 918 | |
b0d93ad5 CL |
919 | if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { |
920 | sock_release(sock); | |
921 | goto out; | |
922 | } | |
9903cd1c | 923 | |
b0d93ad5 CL |
924 | if (!xprt->inet) { |
925 | struct sock *sk = sock->sk; | |
a246b010 | 926 | |
b0d93ad5 | 927 | write_lock_bh(&sk->sk_callback_lock); |
a246b010 | 928 | |
b0d93ad5 CL |
929 | sk->sk_user_data = xprt; |
930 | xprt->old_data_ready = sk->sk_data_ready; | |
931 | xprt->old_state_change = sk->sk_state_change; | |
932 | xprt->old_write_space = sk->sk_write_space; | |
9903cd1c | 933 | sk->sk_data_ready = xs_udp_data_ready; |
c7b2cae8 | 934 | sk->sk_write_space = xs_udp_write_space; |
a246b010 | 935 | sk->sk_no_check = UDP_CSUM_NORCV; |
b0d93ad5 | 936 | |
a246b010 | 937 | xprt_set_connected(xprt); |
a246b010 | 938 | |
b0d93ad5 CL |
939 | /* Reset to new socket */ |
940 | xprt->sock = sock; | |
941 | xprt->inet = sk; | |
a246b010 | 942 | |
b0d93ad5 CL |
943 | write_unlock_bh(&sk->sk_callback_lock); |
944 | } | |
43118c29 | 945 | xs_udp_set_buffer_size(xprt); |
b0d93ad5 CL |
946 | status = 0; |
947 | out: | |
948 | xprt_wake_pending_tasks(xprt, status); | |
949 | xprt_clear_connecting(xprt); | |
a246b010 CL |
950 | } |
951 | ||
9903cd1c | 952 | /** |
b0d93ad5 | 953 | * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint |
9903cd1c CL |
954 | * @args: RPC transport to connect |
955 | * | |
956 | * Invoked by a work queue tasklet. | |
a246b010 | 957 | */ |
b0d93ad5 | 958 | static void xs_tcp_connect_worker(void *args) |
a246b010 CL |
959 | { |
960 | struct rpc_xprt *xprt = (struct rpc_xprt *)args; | |
961 | struct socket *sock = xprt->sock; | |
b0d93ad5 | 962 | int err, status = -EIO; |
a246b010 CL |
963 | |
964 | if (xprt->shutdown || xprt->addr.sin_port == 0) | |
965 | goto out; | |
966 | ||
b0d93ad5 | 967 | dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt); |
9903cd1c | 968 | |
b0d93ad5 | 969 | /* Start by resetting any existing socket state */ |
9903cd1c | 970 | xs_close(xprt); |
b0d93ad5 CL |
971 | |
972 | if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { | |
973 | dprintk("RPC: can't create TCP transport socket (%d).\n", -err); | |
a246b010 CL |
974 | goto out; |
975 | } | |
a246b010 | 976 | |
b0d93ad5 CL |
977 | if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { |
978 | sock_release(sock); | |
a246b010 | 979 | goto out; |
b0d93ad5 | 980 | } |
a246b010 | 981 | |
b0d93ad5 CL |
982 | if (!xprt->inet) { |
983 | struct sock *sk = sock->sk; | |
984 | ||
985 | write_lock_bh(&sk->sk_callback_lock); | |
986 | ||
987 | sk->sk_user_data = xprt; | |
988 | xprt->old_data_ready = sk->sk_data_ready; | |
989 | xprt->old_state_change = sk->sk_state_change; | |
990 | xprt->old_write_space = sk->sk_write_space; | |
991 | sk->sk_data_ready = xs_tcp_data_ready; | |
992 | sk->sk_state_change = xs_tcp_state_change; | |
993 | sk->sk_write_space = xs_tcp_write_space; | |
994 | tcp_sk(sk)->nonagle = 1; | |
995 | ||
996 | xprt_clear_connected(xprt); | |
997 | ||
998 | /* Reset to new socket */ | |
999 | xprt->sock = sock; | |
1000 | xprt->inet = sk; | |
1001 | ||
1002 | write_unlock_bh(&sk->sk_callback_lock); | |
1003 | } | |
1004 | ||
1005 | /* Tell the socket layer to start connecting... */ | |
a246b010 CL |
1006 | status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr, |
1007 | sizeof(xprt->addr), O_NONBLOCK); | |
1008 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", | |
1009 | xprt, -status, xprt_connected(xprt), sock->sk->sk_state); | |
1010 | if (status < 0) { | |
1011 | switch (status) { | |
1012 | case -EINPROGRESS: | |
1013 | case -EALREADY: | |
1014 | goto out_clear; | |
1015 | } | |
1016 | } | |
1017 | out: | |
44fbac22 | 1018 | xprt_wake_pending_tasks(xprt, status); |
a246b010 | 1019 | out_clear: |
2226feb6 | 1020 | xprt_clear_connecting(xprt); |
a246b010 CL |
1021 | } |
1022 | ||
9903cd1c CL |
1023 | /** |
1024 | * xs_connect - connect a socket to a remote endpoint | |
1025 | * @task: address of RPC task that manages state of connect request | |
1026 | * | |
1027 | * TCP: If the remote end dropped the connection, delay reconnecting. | |
1028 | */ | |
1029 | static void xs_connect(struct rpc_task *task) | |
a246b010 CL |
1030 | { |
1031 | struct rpc_xprt *xprt = task->tk_xprt; | |
1032 | ||
b0d93ad5 CL |
1033 | if (xprt_test_and_set_connecting(xprt)) |
1034 | return; | |
1035 | ||
1036 | if (xprt->sock != NULL) { | |
1037 | dprintk("RPC: xs_connect delayed xprt %p\n", xprt); | |
1038 | schedule_delayed_work(&xprt->connect_worker, | |
a246b010 | 1039 | RPC_REESTABLISH_TIMEOUT); |
b0d93ad5 CL |
1040 | } else { |
1041 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); | |
1042 | schedule_work(&xprt->connect_worker); | |
1043 | ||
1044 | /* flush_scheduled_work can sleep... */ | |
1045 | if (!RPC_IS_ASYNC(task)) | |
1046 | flush_scheduled_work(); | |
a246b010 CL |
1047 | } |
1048 | } | |
1049 | ||
262965f5 | 1050 | static struct rpc_xprt_ops xs_udp_ops = { |
43118c29 | 1051 | .set_buffer_size = xs_udp_set_buffer_size, |
12a80469 | 1052 | .reserve_xprt = xprt_reserve_xprt_cong, |
49e9a890 | 1053 | .release_xprt = xprt_release_xprt_cong, |
262965f5 CL |
1054 | .connect = xs_connect, |
1055 | .send_request = xs_udp_send_request, | |
fe3aca29 | 1056 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
46c0ee8b | 1057 | .timer = xs_udp_timer, |
a58dd398 | 1058 | .release_request = xprt_release_rqst_cong, |
262965f5 CL |
1059 | .close = xs_close, |
1060 | .destroy = xs_destroy, | |
1061 | }; | |
1062 | ||
1063 | static struct rpc_xprt_ops xs_tcp_ops = { | |
43118c29 | 1064 | .set_buffer_size = xs_tcp_set_buffer_size, |
12a80469 | 1065 | .reserve_xprt = xprt_reserve_xprt, |
49e9a890 | 1066 | .release_xprt = xprt_release_xprt, |
9903cd1c | 1067 | .connect = xs_connect, |
262965f5 | 1068 | .send_request = xs_tcp_send_request, |
fe3aca29 | 1069 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
9903cd1c CL |
1070 | .close = xs_close, |
1071 | .destroy = xs_destroy, | |
a246b010 CL |
1072 | }; |
1073 | ||
9903cd1c CL |
1074 | /** |
1075 | * xs_setup_udp - Set up transport to use a UDP socket | |
1076 | * @xprt: transport to set up | |
1077 | * @to: timeout parameters | |
1078 | * | |
1079 | */ | |
a246b010 CL |
1080 | int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to) |
1081 | { | |
1082 | size_t slot_table_size; | |
1083 | ||
1084 | dprintk("RPC: setting up udp-ipv4 transport...\n"); | |
1085 | ||
1086 | xprt->max_reqs = xprt_udp_slot_table_entries; | |
1087 | slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); | |
1088 | xprt->slot = kmalloc(slot_table_size, GFP_KERNEL); | |
1089 | if (xprt->slot == NULL) | |
1090 | return -ENOMEM; | |
1091 | memset(xprt->slot, 0, slot_table_size); | |
1092 | ||
1093 | xprt->prot = IPPROTO_UDP; | |
529b33c6 | 1094 | xprt->port = xprt_max_resvport; |
808012fb | 1095 | xprt->tsh_size = 0; |
a246b010 CL |
1096 | xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; |
1097 | /* XXX: header size can vary due to auth type, IPv6, etc. */ | |
1098 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); | |
1099 | ||
b0d93ad5 | 1100 | INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt); |
a246b010 | 1101 | |
262965f5 | 1102 | xprt->ops = &xs_udp_ops; |
a246b010 CL |
1103 | |
1104 | if (to) | |
1105 | xprt->timeout = *to; | |
1106 | else | |
9903cd1c | 1107 | xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); |
a246b010 CL |
1108 | |
1109 | return 0; | |
1110 | } | |
1111 | ||
9903cd1c CL |
1112 | /** |
1113 | * xs_setup_tcp - Set up transport to use a TCP socket | |
1114 | * @xprt: transport to set up | |
1115 | * @to: timeout parameters | |
1116 | * | |
1117 | */ | |
a246b010 CL |
1118 | int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to) |
1119 | { | |
1120 | size_t slot_table_size; | |
1121 | ||
1122 | dprintk("RPC: setting up tcp-ipv4 transport...\n"); | |
1123 | ||
1124 | xprt->max_reqs = xprt_tcp_slot_table_entries; | |
1125 | slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); | |
1126 | xprt->slot = kmalloc(slot_table_size, GFP_KERNEL); | |
1127 | if (xprt->slot == NULL) | |
1128 | return -ENOMEM; | |
1129 | memset(xprt->slot, 0, slot_table_size); | |
1130 | ||
1131 | xprt->prot = IPPROTO_TCP; | |
529b33c6 | 1132 | xprt->port = xprt_max_resvport; |
808012fb | 1133 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
a246b010 | 1134 | xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; |
808012fb | 1135 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
a246b010 | 1136 | |
b0d93ad5 | 1137 | INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt); |
a246b010 | 1138 | |
262965f5 | 1139 | xprt->ops = &xs_tcp_ops; |
a246b010 CL |
1140 | |
1141 | if (to) | |
1142 | xprt->timeout = *to; | |
1143 | else | |
9903cd1c | 1144 | xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); |
a246b010 CL |
1145 | |
1146 | return 0; | |
1147 | } |