Commit | Line | Data |
---|---|---|
a246b010 CL |
1 | /* |
2 | * linux/net/sunrpc/xprtsock.c | |
3 | * | |
4 | * Client-side transport implementation for sockets. | |
5 | * | |
113aa838 AC |
6 | * TCP callback races fixes (C) 1998 Red Hat |
7 | * TCP send fixes (C) 1998 Red Hat | |
a246b010 CL |
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> | |
8f9d5b1a CL |
16 | * |
17 | * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. | |
18 | * <gilles.quillard@bull.net> | |
a246b010 CL |
19 | */ |
20 | ||
21 | #include <linux/types.h> | |
22 | #include <linux/slab.h> | |
bc25571e | 23 | #include <linux/module.h> |
a246b010 | 24 | #include <linux/capability.h> |
a246b010 CL |
25 | #include <linux/pagemap.h> |
26 | #include <linux/errno.h> | |
27 | #include <linux/socket.h> | |
28 | #include <linux/in.h> | |
29 | #include <linux/net.h> | |
30 | #include <linux/mm.h> | |
31 | #include <linux/udp.h> | |
32 | #include <linux/tcp.h> | |
33 | #include <linux/sunrpc/clnt.h> | |
02107148 | 34 | #include <linux/sunrpc/sched.h> |
4cfc7e60 | 35 | #include <linux/sunrpc/svcsock.h> |
49c36fcc | 36 | #include <linux/sunrpc/xprtsock.h> |
a246b010 | 37 | #include <linux/file.h> |
44b98efd RL |
38 | #ifdef CONFIG_NFS_V4_1 |
39 | #include <linux/sunrpc/bc_xprt.h> | |
40 | #endif | |
a246b010 CL |
41 | |
42 | #include <net/sock.h> | |
43 | #include <net/checksum.h> | |
44 | #include <net/udp.h> | |
45 | #include <net/tcp.h> | |
46 | ||
4cfc7e60 | 47 | #include "sunrpc.h" |
c556b754 CL |
48 | /* |
49 | * xprtsock tunables | |
50 | */ | |
51 | unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; | |
52 | unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE; | |
53 | ||
54 | unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; | |
55 | unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; | |
56 | ||
7d1e8255 | 57 | #define XS_TCP_LINGER_TO (15U * HZ) |
25fe6142 | 58 | static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; |
7d1e8255 | 59 | |
fbf76683 CL |
60 | /* |
61 | * We can register our own files under /proc/sys/sunrpc by | |
62 | * calling register_sysctl_table() again. The files in that | |
63 | * directory become the union of all files registered there. | |
64 | * | |
65 | * We simply need to make sure that we don't collide with | |
66 | * someone else's file names! | |
67 | */ | |
68 | ||
69 | #ifdef RPC_DEBUG | |
70 | ||
71 | static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; | |
72 | static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; | |
73 | static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; | |
74 | static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; | |
75 | ||
76 | static struct ctl_table_header *sunrpc_table_header; | |
77 | ||
78 | /* | |
79 | * FIXME: changing the UDP slot table size should also resize the UDP | |
80 | * socket buffers for existing UDP transports | |
81 | */ | |
82 | static ctl_table xs_tunables_table[] = { | |
83 | { | |
fbf76683 CL |
84 | .procname = "udp_slot_table_entries", |
85 | .data = &xprt_udp_slot_table_entries, | |
86 | .maxlen = sizeof(unsigned int), | |
87 | .mode = 0644, | |
6d456111 | 88 | .proc_handler = proc_dointvec_minmax, |
fbf76683 CL |
89 | .extra1 = &min_slot_table_size, |
90 | .extra2 = &max_slot_table_size | |
91 | }, | |
92 | { | |
fbf76683 CL |
93 | .procname = "tcp_slot_table_entries", |
94 | .data = &xprt_tcp_slot_table_entries, | |
95 | .maxlen = sizeof(unsigned int), | |
96 | .mode = 0644, | |
6d456111 | 97 | .proc_handler = proc_dointvec_minmax, |
fbf76683 CL |
98 | .extra1 = &min_slot_table_size, |
99 | .extra2 = &max_slot_table_size | |
100 | }, | |
101 | { | |
fbf76683 CL |
102 | .procname = "min_resvport", |
103 | .data = &xprt_min_resvport, | |
104 | .maxlen = sizeof(unsigned int), | |
105 | .mode = 0644, | |
6d456111 | 106 | .proc_handler = proc_dointvec_minmax, |
fbf76683 CL |
107 | .extra1 = &xprt_min_resvport_limit, |
108 | .extra2 = &xprt_max_resvport_limit | |
109 | }, | |
110 | { | |
fbf76683 CL |
111 | .procname = "max_resvport", |
112 | .data = &xprt_max_resvport, | |
113 | .maxlen = sizeof(unsigned int), | |
114 | .mode = 0644, | |
6d456111 | 115 | .proc_handler = proc_dointvec_minmax, |
fbf76683 CL |
116 | .extra1 = &xprt_min_resvport_limit, |
117 | .extra2 = &xprt_max_resvport_limit | |
118 | }, | |
25fe6142 TM |
119 | { |
120 | .procname = "tcp_fin_timeout", | |
121 | .data = &xs_tcp_fin_timeout, | |
122 | .maxlen = sizeof(xs_tcp_fin_timeout), | |
123 | .mode = 0644, | |
6d456111 | 124 | .proc_handler = proc_dointvec_jiffies, |
fbf76683 | 125 | }, |
f8572d8f | 126 | { }, |
fbf76683 CL |
127 | }; |
128 | ||
129 | static ctl_table sunrpc_table[] = { | |
130 | { | |
fbf76683 CL |
131 | .procname = "sunrpc", |
132 | .mode = 0555, | |
133 | .child = xs_tunables_table | |
134 | }, | |
f8572d8f | 135 | { }, |
fbf76683 CL |
136 | }; |
137 | ||
138 | #endif | |
139 | ||
03bf4b70 CL |
140 | /* |
141 | * Wait duration for a reply from the RPC portmapper. | |
142 | */ | |
143 | #define XS_BIND_TO (60U * HZ) | |
144 | ||
145 | /* | |
146 | * Delay if a UDP socket connect error occurs. This is most likely some | |
147 | * kind of resource problem on the local host. | |
148 | */ | |
149 | #define XS_UDP_REEST_TO (2U * HZ) | |
150 | ||
151 | /* | |
152 | * The reestablish timeout allows clients to delay for a bit before attempting | |
153 | * to reconnect to a server that just dropped our connection. | |
154 | * | |
155 | * We implement an exponential backoff when trying to reestablish a TCP | |
156 | * transport connection with the server. Some servers like to drop a TCP | |
157 | * connection when they are overworked, so we start with a short timeout and | |
158 | * increase over time if the server is down or not responding. | |
159 | */ | |
160 | #define XS_TCP_INIT_REEST_TO (3U * HZ) | |
161 | #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) | |
162 | ||
163 | /* | |
164 | * TCP idle timeout; client drops the transport socket if it is idle | |
165 | * for this long. Note that we also timeout UDP sockets to prevent | |
166 | * holding port numbers when there is no RPC traffic. | |
167 | */ | |
168 | #define XS_IDLE_DISC_TO (5U * 60 * HZ) | |
169 | ||
a246b010 CL |
170 | #ifdef RPC_DEBUG |
171 | # undef RPC_DEBUG_DATA | |
9903cd1c | 172 | # define RPCDBG_FACILITY RPCDBG_TRANS |
a246b010 CL |
173 | #endif |
174 | ||
a246b010 | 175 | #ifdef RPC_DEBUG_DATA |
9903cd1c | 176 | static void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
a246b010 | 177 | { |
9903cd1c CL |
178 | u8 *buf = (u8 *) packet; |
179 | int j; | |
a246b010 | 180 | |
46121cf7 | 181 | dprintk("RPC: %s\n", msg); |
a246b010 CL |
182 | for (j = 0; j < count && j < 128; j += 4) { |
183 | if (!(j & 31)) { | |
184 | if (j) | |
185 | dprintk("\n"); | |
186 | dprintk("0x%04x ", j); | |
187 | } | |
188 | dprintk("%02x%02x%02x%02x ", | |
189 | buf[j], buf[j+1], buf[j+2], buf[j+3]); | |
190 | } | |
191 | dprintk("\n"); | |
192 | } | |
193 | #else | |
9903cd1c | 194 | static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
a246b010 CL |
195 | { |
196 | /* NOP */ | |
197 | } | |
198 | #endif | |
199 | ||
ffc2e518 CL |
200 | struct sock_xprt { |
201 | struct rpc_xprt xprt; | |
ee0ac0c2 CL |
202 | |
203 | /* | |
204 | * Network layer | |
205 | */ | |
206 | struct socket * sock; | |
207 | struct sock * inet; | |
51971139 CL |
208 | |
209 | /* | |
210 | * State of TCP reply receive | |
211 | */ | |
212 | __be32 tcp_fraghdr, | |
b76ce561 TM |
213 | tcp_xid, |
214 | tcp_calldir; | |
51971139 CL |
215 | |
216 | u32 tcp_offset, | |
217 | tcp_reclen; | |
218 | ||
219 | unsigned long tcp_copied, | |
220 | tcp_flags; | |
c8475461 CL |
221 | |
222 | /* | |
223 | * Connection of transports | |
224 | */ | |
34161db6 | 225 | struct delayed_work connect_worker; |
fbfffbd5 CL |
226 | struct sockaddr_storage srcaddr; |
227 | unsigned short srcport; | |
7c6e066e CL |
228 | |
229 | /* | |
230 | * UDP socket buffer size parameters | |
231 | */ | |
232 | size_t rcvsize, | |
233 | sndsize; | |
314dfd79 CL |
234 | |
235 | /* | |
236 | * Saved socket callback addresses | |
237 | */ | |
238 | void (*old_data_ready)(struct sock *, int); | |
239 | void (*old_state_change)(struct sock *); | |
240 | void (*old_write_space)(struct sock *); | |
2a9e1cfa | 241 | void (*old_error_report)(struct sock *); |
ffc2e518 CL |
242 | }; |
243 | ||
e136d092 CL |
244 | /* |
245 | * TCP receive state flags | |
246 | */ | |
247 | #define TCP_RCV_LAST_FRAG (1UL << 0) | |
248 | #define TCP_RCV_COPY_FRAGHDR (1UL << 1) | |
249 | #define TCP_RCV_COPY_XID (1UL << 2) | |
250 | #define TCP_RCV_COPY_DATA (1UL << 3) | |
f4a2e418 RL |
251 | #define TCP_RCV_READ_CALLDIR (1UL << 4) |
252 | #define TCP_RCV_COPY_CALLDIR (1UL << 5) | |
18dca02a RL |
253 | |
254 | /* | |
255 | * TCP RPC flags | |
256 | */ | |
f4a2e418 | 257 | #define TCP_RPC_REPLY (1UL << 6) |
e136d092 | 258 | |
95392c59 CL |
259 | static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) |
260 | { | |
261 | return (struct sockaddr *) &xprt->addr; | |
262 | } | |
263 | ||
264 | static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) | |
edb267a6 | 265 | { |
95392c59 CL |
266 | return (struct sockaddr_in *) &xprt->addr; |
267 | } | |
268 | ||
269 | static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) | |
270 | { | |
271 | return (struct sockaddr_in6 *) &xprt->addr; | |
272 | } | |
273 | ||
c877b849 | 274 | static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) |
edb267a6 | 275 | { |
c877b849 | 276 | struct sockaddr *sap = xs_addr(xprt); |
9dc3b095 CL |
277 | struct sockaddr_in6 *sin6; |
278 | struct sockaddr_in *sin; | |
c877b849 | 279 | char buf[128]; |
edb267a6 | 280 | |
c877b849 CL |
281 | (void)rpc_ntop(sap, buf, sizeof(buf)); |
282 | xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL); | |
fbfe3cc6 | 283 | |
9dc3b095 CL |
284 | switch (sap->sa_family) { |
285 | case AF_INET: | |
286 | sin = xs_addr_in(xprt); | |
fc0b5791 | 287 | snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); |
9dc3b095 CL |
288 | break; |
289 | case AF_INET6: | |
290 | sin6 = xs_addr_in6(xprt); | |
fc0b5791 | 291 | snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); |
9dc3b095 CL |
292 | break; |
293 | default: | |
294 | BUG(); | |
756805e7 | 295 | } |
9dc3b095 | 296 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); |
edb267a6 CL |
297 | } |
298 | ||
9dc3b095 | 299 | static void xs_format_common_peer_ports(struct rpc_xprt *xprt) |
4b6473fb | 300 | { |
9dc3b095 CL |
301 | struct sockaddr *sap = xs_addr(xprt); |
302 | char buf[128]; | |
4b6473fb | 303 | |
81160e66 | 304 | snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); |
c877b849 | 305 | xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); |
4b6473fb | 306 | |
81160e66 | 307 | snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); |
c877b849 CL |
308 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); |
309 | } | |
4b6473fb | 310 | |
9dc3b095 CL |
311 | static void xs_format_peer_addresses(struct rpc_xprt *xprt, |
312 | const char *protocol, | |
313 | const char *netid) | |
c877b849 | 314 | { |
b454ae90 | 315 | xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; |
b454ae90 | 316 | xprt->address_strings[RPC_DISPLAY_NETID] = netid; |
c877b849 | 317 | xs_format_common_peer_addresses(xprt); |
9dc3b095 | 318 | xs_format_common_peer_ports(xprt); |
edb267a6 | 319 | } |
4b6473fb | 320 | |
9dc3b095 | 321 | static void xs_update_peer_port(struct rpc_xprt *xprt) |
4b6473fb | 322 | { |
9dc3b095 CL |
323 | kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); |
324 | kfree(xprt->address_strings[RPC_DISPLAY_PORT]); | |
4417c8c4 | 325 | |
9dc3b095 | 326 | xs_format_common_peer_ports(xprt); |
edb267a6 CL |
327 | } |
328 | ||
329 | static void xs_free_peer_addresses(struct rpc_xprt *xprt) | |
330 | { | |
33e01dc7 CL |
331 | unsigned int i; |
332 | ||
333 | for (i = 0; i < RPC_DISPLAY_MAX; i++) | |
334 | switch (i) { | |
335 | case RPC_DISPLAY_PROTO: | |
336 | case RPC_DISPLAY_NETID: | |
337 | continue; | |
338 | default: | |
339 | kfree(xprt->address_strings[i]); | |
340 | } | |
edb267a6 CL |
341 | } |
342 | ||
b4b5cc85 CL |
343 | #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) |
344 | ||
24c5684b | 345 | static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) |
b4b5cc85 | 346 | { |
b4b5cc85 CL |
347 | struct msghdr msg = { |
348 | .msg_name = addr, | |
349 | .msg_namelen = addrlen, | |
24c5684b TM |
350 | .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), |
351 | }; | |
352 | struct kvec iov = { | |
353 | .iov_base = vec->iov_base + base, | |
354 | .iov_len = vec->iov_len - base, | |
b4b5cc85 CL |
355 | }; |
356 | ||
24c5684b | 357 | if (iov.iov_len != 0) |
b4b5cc85 CL |
358 | return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); |
359 | return kernel_sendmsg(sock, &msg, NULL, 0, 0); | |
360 | } | |
361 | ||
24c5684b | 362 | static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) |
b4b5cc85 | 363 | { |
24c5684b TM |
364 | struct page **ppage; |
365 | unsigned int remainder; | |
366 | int err, sent = 0; | |
367 | ||
368 | remainder = xdr->page_len - base; | |
369 | base += xdr->page_base; | |
370 | ppage = xdr->pages + (base >> PAGE_SHIFT); | |
371 | base &= ~PAGE_MASK; | |
372 | for(;;) { | |
373 | unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); | |
374 | int flags = XS_SENDMSG_FLAGS; | |
b4b5cc85 | 375 | |
24c5684b TM |
376 | remainder -= len; |
377 | if (remainder != 0 || more) | |
378 | flags |= MSG_MORE; | |
379 | err = sock->ops->sendpage(sock, *ppage, base, len, flags); | |
380 | if (remainder == 0 || err != len) | |
381 | break; | |
382 | sent += err; | |
383 | ppage++; | |
384 | base = 0; | |
385 | } | |
386 | if (sent == 0) | |
387 | return err; | |
388 | if (err > 0) | |
389 | sent += err; | |
390 | return sent; | |
b4b5cc85 CL |
391 | } |
392 | ||
9903cd1c CL |
393 | /** |
394 | * xs_sendpages - write pages directly to a socket | |
395 | * @sock: socket to send on | |
396 | * @addr: UDP only -- address of destination | |
397 | * @addrlen: UDP only -- length of destination address | |
398 | * @xdr: buffer containing this request | |
399 | * @base: starting position in the buffer | |
400 | * | |
a246b010 | 401 | */ |
24c5684b | 402 | static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) |
a246b010 | 403 | { |
24c5684b TM |
404 | unsigned int remainder = xdr->len - base; |
405 | int err, sent = 0; | |
a246b010 | 406 | |
262965f5 | 407 | if (unlikely(!sock)) |
fba91afb | 408 | return -ENOTSOCK; |
262965f5 CL |
409 | |
410 | clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); | |
24c5684b TM |
411 | if (base != 0) { |
412 | addr = NULL; | |
413 | addrlen = 0; | |
414 | } | |
262965f5 | 415 | |
24c5684b TM |
416 | if (base < xdr->head[0].iov_len || addr != NULL) { |
417 | unsigned int len = xdr->head[0].iov_len - base; | |
418 | remainder -= len; | |
419 | err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); | |
420 | if (remainder == 0 || err != len) | |
a246b010 | 421 | goto out; |
24c5684b | 422 | sent += err; |
a246b010 CL |
423 | base = 0; |
424 | } else | |
24c5684b | 425 | base -= xdr->head[0].iov_len; |
a246b010 | 426 | |
24c5684b TM |
427 | if (base < xdr->page_len) { |
428 | unsigned int len = xdr->page_len - base; | |
429 | remainder -= len; | |
430 | err = xs_send_pagedata(sock, xdr, base, remainder != 0); | |
431 | if (remainder == 0 || err != len) | |
a246b010 | 432 | goto out; |
24c5684b | 433 | sent += err; |
a246b010 | 434 | base = 0; |
24c5684b TM |
435 | } else |
436 | base -= xdr->page_len; | |
437 | ||
438 | if (base >= xdr->tail[0].iov_len) | |
439 | return sent; | |
440 | err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); | |
a246b010 | 441 | out: |
24c5684b TM |
442 | if (sent == 0) |
443 | return err; | |
444 | if (err > 0) | |
445 | sent += err; | |
446 | return sent; | |
a246b010 CL |
447 | } |
448 | ||
b6ddf64f TM |
449 | static void xs_nospace_callback(struct rpc_task *task) |
450 | { | |
451 | struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt); | |
452 | ||
453 | transport->inet->sk_write_pending--; | |
454 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); | |
455 | } | |
456 | ||
9903cd1c | 457 | /** |
262965f5 CL |
458 | * xs_nospace - place task on wait queue if transmit was incomplete |
459 | * @task: task to put to sleep | |
9903cd1c | 460 | * |
a246b010 | 461 | */ |
5e3771ce | 462 | static int xs_nospace(struct rpc_task *task) |
a246b010 | 463 | { |
262965f5 CL |
464 | struct rpc_rqst *req = task->tk_rqstp; |
465 | struct rpc_xprt *xprt = req->rq_xprt; | |
ee0ac0c2 | 466 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
5e3771ce | 467 | int ret = 0; |
a246b010 | 468 | |
46121cf7 | 469 | dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", |
262965f5 CL |
470 | task->tk_pid, req->rq_slen - req->rq_bytes_sent, |
471 | req->rq_slen); | |
472 | ||
b6ddf64f TM |
473 | /* Protect against races with write_space */ |
474 | spin_lock_bh(&xprt->transport_lock); | |
475 | ||
476 | /* Don't race with disconnect */ | |
477 | if (xprt_connected(xprt)) { | |
478 | if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { | |
5e3771ce | 479 | ret = -EAGAIN; |
b6ddf64f TM |
480 | /* |
481 | * Notify TCP that we're limited by the application | |
482 | * window size | |
483 | */ | |
484 | set_bit(SOCK_NOSPACE, &transport->sock->flags); | |
485 | transport->inet->sk_write_pending++; | |
486 | /* ...and wait for more buffer space */ | |
487 | xprt_wait_for_buffer_space(task, xs_nospace_callback); | |
488 | } | |
489 | } else { | |
490 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); | |
5e3771ce | 491 | ret = -ENOTCONN; |
b6ddf64f | 492 | } |
262965f5 | 493 | |
b6ddf64f | 494 | spin_unlock_bh(&xprt->transport_lock); |
5e3771ce | 495 | return ret; |
262965f5 CL |
496 | } |
497 | ||
498 | /** | |
499 | * xs_udp_send_request - write an RPC request to a UDP socket | |
500 | * @task: address of RPC task that manages the state of an RPC request | |
501 | * | |
502 | * Return values: | |
503 | * 0: The request has been sent | |
504 | * EAGAIN: The socket was blocked, please call again later to | |
505 | * complete the request | |
506 | * ENOTCONN: Caller needs to invoke connect logic then call again | |
507 | * other: Some other error occured, the request was not sent | |
508 | */ | |
509 | static int xs_udp_send_request(struct rpc_task *task) | |
510 | { | |
511 | struct rpc_rqst *req = task->tk_rqstp; | |
512 | struct rpc_xprt *xprt = req->rq_xprt; | |
ee0ac0c2 | 513 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
262965f5 CL |
514 | struct xdr_buf *xdr = &req->rq_snd_buf; |
515 | int status; | |
a246b010 | 516 | |
9903cd1c | 517 | xs_pktdump("packet data:", |
a246b010 CL |
518 | req->rq_svec->iov_base, |
519 | req->rq_svec->iov_len); | |
520 | ||
01d37c42 TM |
521 | if (!xprt_bound(xprt)) |
522 | return -ENOTCONN; | |
ee0ac0c2 | 523 | status = xs_sendpages(transport->sock, |
95392c59 | 524 | xs_addr(xprt), |
ee0ac0c2 CL |
525 | xprt->addrlen, xdr, |
526 | req->rq_bytes_sent); | |
a246b010 | 527 | |
46121cf7 | 528 | dprintk("RPC: xs_udp_send_request(%u) = %d\n", |
262965f5 | 529 | xdr->len - req->rq_bytes_sent, status); |
a246b010 | 530 | |
2199700f | 531 | if (status >= 0) { |
d60dbb20 | 532 | req->rq_xmit_bytes_sent += status; |
2199700f TM |
533 | if (status >= req->rq_slen) |
534 | return 0; | |
535 | /* Still some bytes left; set up for a retry later. */ | |
262965f5 | 536 | status = -EAGAIN; |
2199700f | 537 | } |
a246b010 | 538 | |
262965f5 | 539 | switch (status) { |
fba91afb TM |
540 | case -ENOTSOCK: |
541 | status = -ENOTCONN; | |
542 | /* Should we call xs_close() here? */ | |
543 | break; | |
b6ddf64f | 544 | case -EAGAIN: |
5e3771ce | 545 | status = xs_nospace(task); |
b6ddf64f | 546 | break; |
c8485e4d TM |
547 | default: |
548 | dprintk("RPC: sendmsg returned unrecognized error %d\n", | |
549 | -status); | |
262965f5 CL |
550 | case -ENETUNREACH: |
551 | case -EPIPE: | |
a246b010 CL |
552 | case -ECONNREFUSED: |
553 | /* When the server has died, an ICMP port unreachable message | |
9903cd1c | 554 | * prompts ECONNREFUSED. */ |
b6ddf64f | 555 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
a246b010 | 556 | } |
5fe46e9d | 557 | |
262965f5 | 558 | return status; |
a246b010 CL |
559 | } |
560 | ||
e06799f9 TM |
561 | /** |
562 | * xs_tcp_shutdown - gracefully shut down a TCP socket | |
563 | * @xprt: transport | |
564 | * | |
565 | * Initiates a graceful shutdown of the TCP socket by calling the | |
566 | * equivalent of shutdown(SHUT_WR); | |
567 | */ | |
568 | static void xs_tcp_shutdown(struct rpc_xprt *xprt) | |
569 | { | |
570 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); | |
571 | struct socket *sock = transport->sock; | |
572 | ||
573 | if (sock != NULL) | |
574 | kernel_sock_shutdown(sock, SHUT_WR); | |
575 | } | |
576 | ||
808012fb CL |
577 | static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) |
578 | { | |
579 | u32 reclen = buf->len - sizeof(rpc_fraghdr); | |
580 | rpc_fraghdr *base = buf->head[0].iov_base; | |
581 | *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); | |
582 | } | |
583 | ||
9903cd1c | 584 | /** |
262965f5 | 585 | * xs_tcp_send_request - write an RPC request to a TCP socket |
9903cd1c CL |
586 | * @task: address of RPC task that manages the state of an RPC request |
587 | * | |
588 | * Return values: | |
262965f5 CL |
589 | * 0: The request has been sent |
590 | * EAGAIN: The socket was blocked, please call again later to | |
591 | * complete the request | |
592 | * ENOTCONN: Caller needs to invoke connect logic then call again | |
593 | * other: Some other error occured, the request was not sent | |
9903cd1c CL |
594 | * |
595 | * XXX: In the case of soft timeouts, should we eventually give up | |
262965f5 | 596 | * if sendmsg is not able to make progress? |
9903cd1c | 597 | */ |
262965f5 | 598 | static int xs_tcp_send_request(struct rpc_task *task) |
a246b010 CL |
599 | { |
600 | struct rpc_rqst *req = task->tk_rqstp; | |
601 | struct rpc_xprt *xprt = req->rq_xprt; | |
ee0ac0c2 | 602 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
262965f5 | 603 | struct xdr_buf *xdr = &req->rq_snd_buf; |
b595bb15 | 604 | int status; |
a246b010 | 605 | |
808012fb | 606 | xs_encode_tcp_record_marker(&req->rq_snd_buf); |
a246b010 | 607 | |
262965f5 CL |
608 | xs_pktdump("packet data:", |
609 | req->rq_svec->iov_base, | |
610 | req->rq_svec->iov_len); | |
a246b010 CL |
611 | |
612 | /* Continue transmitting the packet/record. We must be careful | |
613 | * to cope with writespace callbacks arriving _after_ we have | |
262965f5 | 614 | * called sendmsg(). */ |
a246b010 | 615 | while (1) { |
ee0ac0c2 CL |
616 | status = xs_sendpages(transport->sock, |
617 | NULL, 0, xdr, req->rq_bytes_sent); | |
a246b010 | 618 | |
46121cf7 | 619 | dprintk("RPC: xs_tcp_send_request(%u) = %d\n", |
262965f5 | 620 | xdr->len - req->rq_bytes_sent, status); |
a246b010 | 621 | |
262965f5 | 622 | if (unlikely(status < 0)) |
a246b010 | 623 | break; |
a246b010 | 624 | |
262965f5 CL |
625 | /* If we've sent the entire packet, immediately |
626 | * reset the count of bytes sent. */ | |
627 | req->rq_bytes_sent += status; | |
d60dbb20 | 628 | req->rq_xmit_bytes_sent += status; |
262965f5 CL |
629 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { |
630 | req->rq_bytes_sent = 0; | |
631 | return 0; | |
632 | } | |
a246b010 | 633 | |
06b4b681 TM |
634 | if (status != 0) |
635 | continue; | |
a246b010 | 636 | status = -EAGAIN; |
06b4b681 | 637 | break; |
a246b010 CL |
638 | } |
639 | ||
262965f5 | 640 | switch (status) { |
fba91afb TM |
641 | case -ENOTSOCK: |
642 | status = -ENOTCONN; | |
643 | /* Should we call xs_close() here? */ | |
644 | break; | |
262965f5 | 645 | case -EAGAIN: |
5e3771ce | 646 | status = xs_nospace(task); |
262965f5 | 647 | break; |
c8485e4d TM |
648 | default: |
649 | dprintk("RPC: sendmsg returned unrecognized error %d\n", | |
650 | -status); | |
262965f5 | 651 | case -ECONNRESET: |
55420c24 | 652 | case -EPIPE: |
2a9e1cfa TM |
653 | xs_tcp_shutdown(xprt); |
654 | case -ECONNREFUSED: | |
262965f5 | 655 | case -ENOTCONN: |
b6ddf64f | 656 | clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags); |
a246b010 | 657 | } |
5fe46e9d | 658 | |
a246b010 CL |
659 | return status; |
660 | } | |
661 | ||
e0ab53de TM |
662 | /** |
663 | * xs_tcp_release_xprt - clean up after a tcp transmission | |
664 | * @xprt: transport | |
665 | * @task: rpc task | |
666 | * | |
667 | * This cleans up if an error causes us to abort the transmission of a request. | |
668 | * In this case, the socket may need to be reset in order to avoid confusing | |
669 | * the server. | |
670 | */ | |
671 | static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) | |
672 | { | |
673 | struct rpc_rqst *req; | |
674 | ||
675 | if (task != xprt->snd_task) | |
676 | return; | |
677 | if (task == NULL) | |
678 | goto out_release; | |
679 | req = task->tk_rqstp; | |
680 | if (req->rq_bytes_sent == 0) | |
681 | goto out_release; | |
682 | if (req->rq_bytes_sent == req->rq_snd_buf.len) | |
683 | goto out_release; | |
684 | set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); | |
685 | out_release: | |
686 | xprt_release_xprt(xprt, task); | |
687 | } | |
688 | ||
2a9e1cfa TM |
689 | static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) |
690 | { | |
691 | transport->old_data_ready = sk->sk_data_ready; | |
692 | transport->old_state_change = sk->sk_state_change; | |
693 | transport->old_write_space = sk->sk_write_space; | |
694 | transport->old_error_report = sk->sk_error_report; | |
695 | } | |
696 | ||
697 | static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) | |
698 | { | |
699 | sk->sk_data_ready = transport->old_data_ready; | |
700 | sk->sk_state_change = transport->old_state_change; | |
701 | sk->sk_write_space = transport->old_write_space; | |
702 | sk->sk_error_report = transport->old_error_report; | |
703 | } | |
704 | ||
fe315e76 | 705 | static void xs_reset_transport(struct sock_xprt *transport) |
a246b010 | 706 | { |
ee0ac0c2 CL |
707 | struct socket *sock = transport->sock; |
708 | struct sock *sk = transport->inet; | |
a246b010 | 709 | |
fe315e76 CL |
710 | if (sk == NULL) |
711 | return; | |
9903cd1c | 712 | |
a246b010 | 713 | write_lock_bh(&sk->sk_callback_lock); |
ee0ac0c2 CL |
714 | transport->inet = NULL; |
715 | transport->sock = NULL; | |
a246b010 | 716 | |
9903cd1c | 717 | sk->sk_user_data = NULL; |
2a9e1cfa TM |
718 | |
719 | xs_restore_old_callbacks(transport, sk); | |
a246b010 CL |
720 | write_unlock_bh(&sk->sk_callback_lock); |
721 | ||
9903cd1c | 722 | sk->sk_no_check = 0; |
a246b010 CL |
723 | |
724 | sock_release(sock); | |
fe315e76 CL |
725 | } |
726 | ||
727 | /** | |
728 | * xs_close - close a socket | |
729 | * @xprt: transport | |
730 | * | |
731 | * This is used when all requests are complete; ie, no DRC state remains | |
732 | * on the server we want to save. | |
f75e6745 TM |
733 | * |
734 | * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with | |
735 | * xs_reset_transport() zeroing the socket from underneath a writer. | |
fe315e76 CL |
736 | */ |
737 | static void xs_close(struct rpc_xprt *xprt) | |
738 | { | |
739 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); | |
740 | ||
741 | dprintk("RPC: xs_close xprt %p\n", xprt); | |
742 | ||
743 | xs_reset_transport(transport); | |
61d0a8e6 | 744 | xprt->reestablish_timeout = 0; |
fe315e76 | 745 | |
632e3bdc | 746 | smp_mb__before_clear_bit(); |
7d1e8255 | 747 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
632e3bdc | 748 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
3b948ae5 | 749 | clear_bit(XPRT_CLOSING, &xprt->state); |
632e3bdc | 750 | smp_mb__after_clear_bit(); |
62da3b24 | 751 | xprt_disconnect_done(xprt); |
a246b010 CL |
752 | } |
753 | ||
f75e6745 TM |
754 | static void xs_tcp_close(struct rpc_xprt *xprt) |
755 | { | |
756 | if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state)) | |
757 | xs_close(xprt); | |
758 | else | |
759 | xs_tcp_shutdown(xprt); | |
760 | } | |
761 | ||
9903cd1c CL |
762 | /** |
763 | * xs_destroy - prepare to shutdown a transport | |
764 | * @xprt: doomed transport | |
765 | * | |
766 | */ | |
767 | static void xs_destroy(struct rpc_xprt *xprt) | |
a246b010 | 768 | { |
c8475461 CL |
769 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
770 | ||
46121cf7 | 771 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
9903cd1c | 772 | |
c1384c9c | 773 | cancel_rearming_delayed_work(&transport->connect_worker); |
a246b010 | 774 | |
9903cd1c | 775 | xs_close(xprt); |
edb267a6 | 776 | xs_free_peer_addresses(xprt); |
e204e621 | 777 | xprt_free(xprt); |
bc25571e | 778 | module_put(THIS_MODULE); |
a246b010 CL |
779 | } |
780 | ||
9903cd1c CL |
781 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
782 | { | |
783 | return (struct rpc_xprt *) sk->sk_user_data; | |
784 | } | |
785 | ||
786 | /** | |
787 | * xs_udp_data_ready - "data ready" callback for UDP sockets | |
788 | * @sk: socket with data to read | |
789 | * @len: how much data to read | |
790 | * | |
a246b010 | 791 | */ |
9903cd1c | 792 | static void xs_udp_data_ready(struct sock *sk, int len) |
a246b010 | 793 | { |
9903cd1c CL |
794 | struct rpc_task *task; |
795 | struct rpc_xprt *xprt; | |
a246b010 | 796 | struct rpc_rqst *rovr; |
9903cd1c | 797 | struct sk_buff *skb; |
a246b010 | 798 | int err, repsize, copied; |
d8ed029d AD |
799 | u32 _xid; |
800 | __be32 *xp; | |
a246b010 CL |
801 | |
802 | read_lock(&sk->sk_callback_lock); | |
46121cf7 | 803 | dprintk("RPC: xs_udp_data_ready...\n"); |
9903cd1c | 804 | if (!(xprt = xprt_from_sock(sk))) |
a246b010 | 805 | goto out; |
a246b010 CL |
806 | |
807 | if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) | |
808 | goto out; | |
809 | ||
810 | if (xprt->shutdown) | |
811 | goto dropit; | |
812 | ||
813 | repsize = skb->len - sizeof(struct udphdr); | |
814 | if (repsize < 4) { | |
46121cf7 | 815 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
a246b010 CL |
816 | goto dropit; |
817 | } | |
818 | ||
819 | /* Copy the XID from the skb... */ | |
820 | xp = skb_header_pointer(skb, sizeof(struct udphdr), | |
821 | sizeof(_xid), &_xid); | |
822 | if (xp == NULL) | |
823 | goto dropit; | |
824 | ||
825 | /* Look up and lock the request corresponding to the given XID */ | |
4a0f8c04 | 826 | spin_lock(&xprt->transport_lock); |
a246b010 CL |
827 | rovr = xprt_lookup_rqst(xprt, *xp); |
828 | if (!rovr) | |
829 | goto out_unlock; | |
830 | task = rovr->rq_task; | |
831 | ||
a246b010 CL |
832 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
833 | copied = repsize; | |
834 | ||
835 | /* Suck it into the iovec, verify checksum if not done by hw. */ | |
1781f7f5 HX |
836 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { |
837 | UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS); | |
a246b010 | 838 | goto out_unlock; |
1781f7f5 HX |
839 | } |
840 | ||
841 | UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS); | |
a246b010 CL |
842 | |
843 | /* Something worked... */ | |
adf30907 | 844 | dst_confirm(skb_dst(skb)); |
a246b010 | 845 | |
1570c1e4 | 846 | xprt_adjust_cwnd(task, copied); |
1570c1e4 | 847 | xprt_complete_rqst(task, copied); |
a246b010 CL |
848 | |
849 | out_unlock: | |
4a0f8c04 | 850 | spin_unlock(&xprt->transport_lock); |
a246b010 CL |
851 | dropit: |
852 | skb_free_datagram(sk, skb); | |
853 | out: | |
854 | read_unlock(&sk->sk_callback_lock); | |
855 | } | |
856 | ||
dd456471 | 857 | static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) |
a246b010 | 858 | { |
51971139 | 859 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
a246b010 CL |
860 | size_t len, used; |
861 | char *p; | |
862 | ||
51971139 CL |
863 | p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; |
864 | len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; | |
9d292316 | 865 | used = xdr_skb_read_bits(desc, p, len); |
51971139 | 866 | transport->tcp_offset += used; |
a246b010 CL |
867 | if (used != len) |
868 | return; | |
808012fb | 869 | |
51971139 CL |
870 | transport->tcp_reclen = ntohl(transport->tcp_fraghdr); |
871 | if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) | |
e136d092 | 872 | transport->tcp_flags |= TCP_RCV_LAST_FRAG; |
a246b010 | 873 | else |
e136d092 | 874 | transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; |
51971139 | 875 | transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; |
808012fb | 876 | |
e136d092 | 877 | transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; |
51971139 | 878 | transport->tcp_offset = 0; |
808012fb | 879 | |
a246b010 | 880 | /* Sanity check of the record length */ |
18dca02a | 881 | if (unlikely(transport->tcp_reclen < 8)) { |
46121cf7 | 882 | dprintk("RPC: invalid TCP record fragment length\n"); |
3ebb067d | 883 | xprt_force_disconnect(xprt); |
9903cd1c | 884 | return; |
a246b010 | 885 | } |
46121cf7 | 886 | dprintk("RPC: reading TCP record fragment of length %d\n", |
51971139 | 887 | transport->tcp_reclen); |
a246b010 CL |
888 | } |
889 | ||
51971139 | 890 | static void xs_tcp_check_fraghdr(struct sock_xprt *transport) |
a246b010 | 891 | { |
51971139 | 892 | if (transport->tcp_offset == transport->tcp_reclen) { |
e136d092 | 893 | transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; |
51971139 | 894 | transport->tcp_offset = 0; |
e136d092 CL |
895 | if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { |
896 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; | |
897 | transport->tcp_flags |= TCP_RCV_COPY_XID; | |
51971139 | 898 | transport->tcp_copied = 0; |
a246b010 CL |
899 | } |
900 | } | |
901 | } | |
902 | ||
dd456471 | 903 | static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc) |
a246b010 CL |
904 | { |
905 | size_t len, used; | |
906 | char *p; | |
907 | ||
51971139 | 908 | len = sizeof(transport->tcp_xid) - transport->tcp_offset; |
46121cf7 | 909 | dprintk("RPC: reading XID (%Zu bytes)\n", len); |
51971139 | 910 | p = ((char *) &transport->tcp_xid) + transport->tcp_offset; |
9d292316 | 911 | used = xdr_skb_read_bits(desc, p, len); |
51971139 | 912 | transport->tcp_offset += used; |
a246b010 CL |
913 | if (used != len) |
914 | return; | |
e136d092 | 915 | transport->tcp_flags &= ~TCP_RCV_COPY_XID; |
f4a2e418 | 916 | transport->tcp_flags |= TCP_RCV_READ_CALLDIR; |
51971139 | 917 | transport->tcp_copied = 4; |
18dca02a RL |
918 | dprintk("RPC: reading %s XID %08x\n", |
919 | (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for" | |
920 | : "request with", | |
51971139 CL |
921 | ntohl(transport->tcp_xid)); |
922 | xs_tcp_check_fraghdr(transport); | |
a246b010 CL |
923 | } |
924 | ||
18dca02a RL |
925 | static inline void xs_tcp_read_calldir(struct sock_xprt *transport, |
926 | struct xdr_skb_reader *desc) | |
a246b010 | 927 | { |
18dca02a RL |
928 | size_t len, used; |
929 | u32 offset; | |
b76ce561 | 930 | char *p; |
18dca02a RL |
931 | |
932 | /* | |
933 | * We want transport->tcp_offset to be 8 at the end of this routine | |
934 | * (4 bytes for the xid and 4 bytes for the call/reply flag). | |
935 | * When this function is called for the first time, | |
936 | * transport->tcp_offset is 4 (after having already read the xid). | |
937 | */ | |
938 | offset = transport->tcp_offset - sizeof(transport->tcp_xid); | |
b76ce561 | 939 | len = sizeof(transport->tcp_calldir) - offset; |
18dca02a | 940 | dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len); |
b76ce561 TM |
941 | p = ((char *) &transport->tcp_calldir) + offset; |
942 | used = xdr_skb_read_bits(desc, p, len); | |
18dca02a RL |
943 | transport->tcp_offset += used; |
944 | if (used != len) | |
945 | return; | |
f4a2e418 | 946 | transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR; |
f4a2e418 RL |
947 | /* |
948 | * We don't yet have the XDR buffer, so we will write the calldir | |
949 | * out after we get the buffer from the 'struct rpc_rqst' | |
950 | */ | |
b76ce561 TM |
951 | switch (ntohl(transport->tcp_calldir)) { |
952 | case RPC_REPLY: | |
953 | transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; | |
954 | transport->tcp_flags |= TCP_RCV_COPY_DATA; | |
18dca02a | 955 | transport->tcp_flags |= TCP_RPC_REPLY; |
b76ce561 TM |
956 | break; |
957 | case RPC_CALL: | |
958 | transport->tcp_flags |= TCP_RCV_COPY_CALLDIR; | |
959 | transport->tcp_flags |= TCP_RCV_COPY_DATA; | |
18dca02a | 960 | transport->tcp_flags &= ~TCP_RPC_REPLY; |
b76ce561 TM |
961 | break; |
962 | default: | |
963 | dprintk("RPC: invalid request message type\n"); | |
964 | xprt_force_disconnect(&transport->xprt); | |
965 | } | |
18dca02a RL |
966 | xs_tcp_check_fraghdr(transport); |
967 | } | |
968 | ||
44b98efd RL |
969 | static inline void xs_tcp_read_common(struct rpc_xprt *xprt, |
970 | struct xdr_skb_reader *desc, | |
971 | struct rpc_rqst *req) | |
a246b010 | 972 | { |
44b98efd RL |
973 | struct sock_xprt *transport = |
974 | container_of(xprt, struct sock_xprt, xprt); | |
a246b010 CL |
975 | struct xdr_buf *rcvbuf; |
976 | size_t len; | |
977 | ssize_t r; | |
978 | ||
a246b010 | 979 | rcvbuf = &req->rq_private_buf; |
f4a2e418 RL |
980 | |
981 | if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) { | |
982 | /* | |
983 | * Save the RPC direction in the XDR buffer | |
984 | */ | |
f4a2e418 | 985 | memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied, |
b76ce561 TM |
986 | &transport->tcp_calldir, |
987 | sizeof(transport->tcp_calldir)); | |
988 | transport->tcp_copied += sizeof(transport->tcp_calldir); | |
f4a2e418 | 989 | transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR; |
a246b010 CL |
990 | } |
991 | ||
a246b010 | 992 | len = desc->count; |
51971139 | 993 | if (len > transport->tcp_reclen - transport->tcp_offset) { |
dd456471 | 994 | struct xdr_skb_reader my_desc; |
a246b010 | 995 | |
51971139 | 996 | len = transport->tcp_reclen - transport->tcp_offset; |
a246b010 CL |
997 | memcpy(&my_desc, desc, sizeof(my_desc)); |
998 | my_desc.count = len; | |
51971139 | 999 | r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, |
9d292316 | 1000 | &my_desc, xdr_skb_read_bits); |
a246b010 CL |
1001 | desc->count -= r; |
1002 | desc->offset += r; | |
1003 | } else | |
51971139 | 1004 | r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, |
9d292316 | 1005 | desc, xdr_skb_read_bits); |
a246b010 CL |
1006 | |
1007 | if (r > 0) { | |
51971139 CL |
1008 | transport->tcp_copied += r; |
1009 | transport->tcp_offset += r; | |
a246b010 CL |
1010 | } |
1011 | if (r != len) { | |
1012 | /* Error when copying to the receive buffer, | |
1013 | * usually because we weren't able to allocate | |
1014 | * additional buffer pages. All we can do now | |
e136d092 | 1015 | * is turn off TCP_RCV_COPY_DATA, so the request |
a246b010 CL |
1016 | * will not receive any additional updates, |
1017 | * and time out. | |
1018 | * Any remaining data from this record will | |
1019 | * be discarded. | |
1020 | */ | |
e136d092 | 1021 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
46121cf7 | 1022 | dprintk("RPC: XID %08x truncated request\n", |
51971139 | 1023 | ntohl(transport->tcp_xid)); |
46121cf7 CL |
1024 | dprintk("RPC: xprt = %p, tcp_copied = %lu, " |
1025 | "tcp_offset = %u, tcp_reclen = %u\n", | |
1026 | xprt, transport->tcp_copied, | |
1027 | transport->tcp_offset, transport->tcp_reclen); | |
44b98efd | 1028 | return; |
a246b010 CL |
1029 | } |
1030 | ||
46121cf7 | 1031 | dprintk("RPC: XID %08x read %Zd bytes\n", |
51971139 | 1032 | ntohl(transport->tcp_xid), r); |
46121cf7 CL |
1033 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " |
1034 | "tcp_reclen = %u\n", xprt, transport->tcp_copied, | |
1035 | transport->tcp_offset, transport->tcp_reclen); | |
51971139 CL |
1036 | |
1037 | if (transport->tcp_copied == req->rq_private_buf.buflen) | |
e136d092 | 1038 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
51971139 | 1039 | else if (transport->tcp_offset == transport->tcp_reclen) { |
e136d092 CL |
1040 | if (transport->tcp_flags & TCP_RCV_LAST_FRAG) |
1041 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; | |
a246b010 | 1042 | } |
44b98efd RL |
1043 | } |
1044 | ||
1045 | /* | |
1046 | * Finds the request corresponding to the RPC xid and invokes the common | |
1047 | * tcp read code to read the data. | |
1048 | */ | |
1049 | static inline int xs_tcp_read_reply(struct rpc_xprt *xprt, | |
1050 | struct xdr_skb_reader *desc) | |
1051 | { | |
1052 | struct sock_xprt *transport = | |
1053 | container_of(xprt, struct sock_xprt, xprt); | |
1054 | struct rpc_rqst *req; | |
1055 | ||
1056 | dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid)); | |
1057 | ||
1058 | /* Find and lock the request corresponding to this xid */ | |
1059 | spin_lock(&xprt->transport_lock); | |
1060 | req = xprt_lookup_rqst(xprt, transport->tcp_xid); | |
1061 | if (!req) { | |
1062 | dprintk("RPC: XID %08x request not found!\n", | |
1063 | ntohl(transport->tcp_xid)); | |
1064 | spin_unlock(&xprt->transport_lock); | |
1065 | return -1; | |
1066 | } | |
1067 | ||
1068 | xs_tcp_read_common(xprt, desc, req); | |
1069 | ||
e136d092 | 1070 | if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) |
51971139 | 1071 | xprt_complete_rqst(req->rq_task, transport->tcp_copied); |
44b98efd | 1072 | |
4a0f8c04 | 1073 | spin_unlock(&xprt->transport_lock); |
44b98efd RL |
1074 | return 0; |
1075 | } | |
1076 | ||
1077 | #if defined(CONFIG_NFS_V4_1) | |
1078 | /* | |
1079 | * Obtains an rpc_rqst previously allocated and invokes the common | |
1080 | * tcp read code to read the data. The result is placed in the callback | |
1081 | * queue. | |
1082 | * If we're unable to obtain the rpc_rqst we schedule the closing of the | |
1083 | * connection and return -1. | |
1084 | */ | |
1085 | static inline int xs_tcp_read_callback(struct rpc_xprt *xprt, | |
1086 | struct xdr_skb_reader *desc) | |
1087 | { | |
1088 | struct sock_xprt *transport = | |
1089 | container_of(xprt, struct sock_xprt, xprt); | |
1090 | struct rpc_rqst *req; | |
1091 | ||
1092 | req = xprt_alloc_bc_request(xprt); | |
1093 | if (req == NULL) { | |
1094 | printk(KERN_WARNING "Callback slot table overflowed\n"); | |
1095 | xprt_force_disconnect(xprt); | |
1096 | return -1; | |
1097 | } | |
1098 | ||
1099 | req->rq_xid = transport->tcp_xid; | |
1100 | dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid)); | |
1101 | xs_tcp_read_common(xprt, desc, req); | |
1102 | ||
1103 | if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) { | |
1104 | struct svc_serv *bc_serv = xprt->bc_serv; | |
1105 | ||
1106 | /* | |
1107 | * Add callback request to callback list. The callback | |
1108 | * service sleeps on the sv_cb_waitq waiting for new | |
1109 | * requests. Wake it up after adding enqueing the | |
1110 | * request. | |
1111 | */ | |
1112 | dprintk("RPC: add callback request to list\n"); | |
1113 | spin_lock(&bc_serv->sv_cb_lock); | |
1114 | list_add(&req->rq_bc_list, &bc_serv->sv_cb_list); | |
1115 | spin_unlock(&bc_serv->sv_cb_lock); | |
1116 | wake_up(&bc_serv->sv_cb_waitq); | |
1117 | } | |
1118 | ||
1119 | req->rq_private_buf.len = transport->tcp_copied; | |
1120 | ||
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, | |
1125 | struct xdr_skb_reader *desc) | |
1126 | { | |
1127 | struct sock_xprt *transport = | |
1128 | container_of(xprt, struct sock_xprt, xprt); | |
1129 | ||
1130 | return (transport->tcp_flags & TCP_RPC_REPLY) ? | |
1131 | xs_tcp_read_reply(xprt, desc) : | |
1132 | xs_tcp_read_callback(xprt, desc); | |
1133 | } | |
1134 | #else | |
1135 | static inline int _xs_tcp_read_data(struct rpc_xprt *xprt, | |
1136 | struct xdr_skb_reader *desc) | |
1137 | { | |
1138 | return xs_tcp_read_reply(xprt, desc); | |
1139 | } | |
1140 | #endif /* CONFIG_NFS_V4_1 */ | |
1141 | ||
1142 | /* | |
1143 | * Read data off the transport. This can be either an RPC_CALL or an | |
1144 | * RPC_REPLY. Relay the processing to helper functions. | |
1145 | */ | |
1146 | static void xs_tcp_read_data(struct rpc_xprt *xprt, | |
1147 | struct xdr_skb_reader *desc) | |
1148 | { | |
1149 | struct sock_xprt *transport = | |
1150 | container_of(xprt, struct sock_xprt, xprt); | |
1151 | ||
1152 | if (_xs_tcp_read_data(xprt, desc) == 0) | |
1153 | xs_tcp_check_fraghdr(transport); | |
1154 | else { | |
1155 | /* | |
1156 | * The transport_lock protects the request handling. | |
1157 | * There's no need to hold it to update the tcp_flags. | |
1158 | */ | |
1159 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; | |
1160 | } | |
a246b010 CL |
1161 | } |
1162 | ||
dd456471 | 1163 | static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc) |
a246b010 CL |
1164 | { |
1165 | size_t len; | |
1166 | ||
51971139 | 1167 | len = transport->tcp_reclen - transport->tcp_offset; |
a246b010 CL |
1168 | if (len > desc->count) |
1169 | len = desc->count; | |
1170 | desc->count -= len; | |
1171 | desc->offset += len; | |
51971139 | 1172 | transport->tcp_offset += len; |
46121cf7 | 1173 | dprintk("RPC: discarded %Zu bytes\n", len); |
51971139 | 1174 | xs_tcp_check_fraghdr(transport); |
a246b010 CL |
1175 | } |
1176 | ||
9903cd1c | 1177 | static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) |
a246b010 CL |
1178 | { |
1179 | struct rpc_xprt *xprt = rd_desc->arg.data; | |
51971139 | 1180 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
dd456471 | 1181 | struct xdr_skb_reader desc = { |
a246b010 CL |
1182 | .skb = skb, |
1183 | .offset = offset, | |
1184 | .count = len, | |
9903cd1c | 1185 | }; |
a246b010 | 1186 | |
46121cf7 | 1187 | dprintk("RPC: xs_tcp_data_recv started\n"); |
a246b010 CL |
1188 | do { |
1189 | /* Read in a new fragment marker if necessary */ | |
1190 | /* Can we ever really expect to get completely empty fragments? */ | |
e136d092 | 1191 | if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { |
9903cd1c | 1192 | xs_tcp_read_fraghdr(xprt, &desc); |
a246b010 CL |
1193 | continue; |
1194 | } | |
1195 | /* Read in the xid if necessary */ | |
e136d092 | 1196 | if (transport->tcp_flags & TCP_RCV_COPY_XID) { |
51971139 | 1197 | xs_tcp_read_xid(transport, &desc); |
a246b010 CL |
1198 | continue; |
1199 | } | |
18dca02a | 1200 | /* Read in the call/reply flag */ |
f4a2e418 | 1201 | if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) { |
18dca02a RL |
1202 | xs_tcp_read_calldir(transport, &desc); |
1203 | continue; | |
1204 | } | |
a246b010 | 1205 | /* Read in the request data */ |
e136d092 | 1206 | if (transport->tcp_flags & TCP_RCV_COPY_DATA) { |
44b98efd | 1207 | xs_tcp_read_data(xprt, &desc); |
a246b010 CL |
1208 | continue; |
1209 | } | |
1210 | /* Skip over any trailing bytes on short reads */ | |
51971139 | 1211 | xs_tcp_read_discard(transport, &desc); |
a246b010 | 1212 | } while (desc.count); |
46121cf7 | 1213 | dprintk("RPC: xs_tcp_data_recv done\n"); |
a246b010 CL |
1214 | return len - desc.count; |
1215 | } | |
1216 | ||
9903cd1c CL |
1217 | /** |
1218 | * xs_tcp_data_ready - "data ready" callback for TCP sockets | |
1219 | * @sk: socket with data to read | |
1220 | * @bytes: how much data to read | |
1221 | * | |
1222 | */ | |
1223 | static void xs_tcp_data_ready(struct sock *sk, int bytes) | |
a246b010 CL |
1224 | { |
1225 | struct rpc_xprt *xprt; | |
1226 | read_descriptor_t rd_desc; | |
ff2d7db8 | 1227 | int read; |
a246b010 | 1228 | |
46121cf7 CL |
1229 | dprintk("RPC: xs_tcp_data_ready...\n"); |
1230 | ||
a246b010 | 1231 | read_lock(&sk->sk_callback_lock); |
9903cd1c | 1232 | if (!(xprt = xprt_from_sock(sk))) |
a246b010 | 1233 | goto out; |
a246b010 CL |
1234 | if (xprt->shutdown) |
1235 | goto out; | |
1236 | ||
61d0a8e6 NB |
1237 | /* Any data means we had a useful conversation, so |
1238 | * the we don't need to delay the next reconnect | |
1239 | */ | |
1240 | if (xprt->reestablish_timeout) | |
1241 | xprt->reestablish_timeout = 0; | |
1242 | ||
9903cd1c | 1243 | /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ |
a246b010 | 1244 | rd_desc.arg.data = xprt; |
ff2d7db8 TM |
1245 | do { |
1246 | rd_desc.count = 65536; | |
1247 | read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); | |
1248 | } while (read > 0); | |
a246b010 CL |
1249 | out: |
1250 | read_unlock(&sk->sk_callback_lock); | |
1251 | } | |
1252 | ||
7d1e8255 TM |
1253 | /* |
1254 | * Do the equivalent of linger/linger2 handling for dealing with | |
1255 | * broken servers that don't close the socket in a timely | |
1256 | * fashion | |
1257 | */ | |
1258 | static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt, | |
1259 | unsigned long timeout) | |
1260 | { | |
1261 | struct sock_xprt *transport; | |
1262 | ||
1263 | if (xprt_test_and_set_connecting(xprt)) | |
1264 | return; | |
1265 | set_bit(XPRT_CONNECTION_ABORT, &xprt->state); | |
1266 | transport = container_of(xprt, struct sock_xprt, xprt); | |
1267 | queue_delayed_work(rpciod_workqueue, &transport->connect_worker, | |
1268 | timeout); | |
1269 | } | |
1270 | ||
1271 | static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt) | |
1272 | { | |
1273 | struct sock_xprt *transport; | |
1274 | ||
1275 | transport = container_of(xprt, struct sock_xprt, xprt); | |
1276 | ||
1277 | if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) || | |
1278 | !cancel_delayed_work(&transport->connect_worker)) | |
1279 | return; | |
1280 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); | |
1281 | xprt_clear_connecting(xprt); | |
1282 | } | |
1283 | ||
1284 | static void xs_sock_mark_closed(struct rpc_xprt *xprt) | |
1285 | { | |
1286 | smp_mb__before_clear_bit(); | |
1287 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); | |
1288 | clear_bit(XPRT_CLOSING, &xprt->state); | |
1289 | smp_mb__after_clear_bit(); | |
1290 | /* Mark transport as closed and wake up all pending tasks */ | |
1291 | xprt_disconnect_done(xprt); | |
1292 | } | |
1293 | ||
9903cd1c CL |
1294 | /** |
1295 | * xs_tcp_state_change - callback to handle TCP socket state changes | |
1296 | * @sk: socket whose state has changed | |
1297 | * | |
1298 | */ | |
1299 | static void xs_tcp_state_change(struct sock *sk) | |
a246b010 | 1300 | { |
9903cd1c | 1301 | struct rpc_xprt *xprt; |
a246b010 CL |
1302 | |
1303 | read_lock(&sk->sk_callback_lock); | |
1304 | if (!(xprt = xprt_from_sock(sk))) | |
1305 | goto out; | |
46121cf7 | 1306 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
669502ff | 1307 | dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", |
46121cf7 CL |
1308 | sk->sk_state, xprt_connected(xprt), |
1309 | sock_flag(sk, SOCK_DEAD), | |
669502ff AC |
1310 | sock_flag(sk, SOCK_ZAPPED), |
1311 | sk->sk_shutdown); | |
a246b010 CL |
1312 | |
1313 | switch (sk->sk_state) { | |
1314 | case TCP_ESTABLISHED: | |
4a0f8c04 | 1315 | spin_lock_bh(&xprt->transport_lock); |
a246b010 | 1316 | if (!xprt_test_and_set_connected(xprt)) { |
51971139 CL |
1317 | struct sock_xprt *transport = container_of(xprt, |
1318 | struct sock_xprt, xprt); | |
1319 | ||
a246b010 | 1320 | /* Reset TCP record info */ |
51971139 CL |
1321 | transport->tcp_offset = 0; |
1322 | transport->tcp_reclen = 0; | |
1323 | transport->tcp_copied = 0; | |
e136d092 CL |
1324 | transport->tcp_flags = |
1325 | TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; | |
51971139 | 1326 | |
2a491991 | 1327 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
a246b010 | 1328 | } |
4a0f8c04 | 1329 | spin_unlock_bh(&xprt->transport_lock); |
a246b010 | 1330 | break; |
3b948ae5 TM |
1331 | case TCP_FIN_WAIT1: |
1332 | /* The client initiated a shutdown of the socket */ | |
7c1d71cf | 1333 | xprt->connect_cookie++; |
663b8858 | 1334 | xprt->reestablish_timeout = 0; |
3b948ae5 TM |
1335 | set_bit(XPRT_CLOSING, &xprt->state); |
1336 | smp_mb__before_clear_bit(); | |
1337 | clear_bit(XPRT_CONNECTED, &xprt->state); | |
ef803670 | 1338 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
3b948ae5 | 1339 | smp_mb__after_clear_bit(); |
25fe6142 | 1340 | xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); |
a246b010 | 1341 | break; |
632e3bdc | 1342 | case TCP_CLOSE_WAIT: |
3b948ae5 | 1343 | /* The server initiated a shutdown of the socket */ |
66af1e55 | 1344 | xprt_force_disconnect(xprt); |
663b8858 | 1345 | case TCP_SYN_SENT: |
7c1d71cf | 1346 | xprt->connect_cookie++; |
663b8858 TM |
1347 | case TCP_CLOSING: |
1348 | /* | |
1349 | * If the server closed down the connection, make sure that | |
1350 | * we back off before reconnecting | |
1351 | */ | |
1352 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) | |
1353 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; | |
3b948ae5 TM |
1354 | break; |
1355 | case TCP_LAST_ACK: | |
670f9457 | 1356 | set_bit(XPRT_CLOSING, &xprt->state); |
25fe6142 | 1357 | xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout); |
3b948ae5 TM |
1358 | smp_mb__before_clear_bit(); |
1359 | clear_bit(XPRT_CONNECTED, &xprt->state); | |
1360 | smp_mb__after_clear_bit(); | |
1361 | break; | |
1362 | case TCP_CLOSE: | |
7d1e8255 TM |
1363 | xs_tcp_cancel_linger_timeout(xprt); |
1364 | xs_sock_mark_closed(xprt); | |
a246b010 CL |
1365 | } |
1366 | out: | |
1367 | read_unlock(&sk->sk_callback_lock); | |
1368 | } | |
1369 | ||
2a9e1cfa | 1370 | /** |
482f32e6 | 1371 | * xs_error_report - callback mainly for catching socket errors |
2a9e1cfa TM |
1372 | * @sk: socket |
1373 | */ | |
482f32e6 | 1374 | static void xs_error_report(struct sock *sk) |
2a9e1cfa TM |
1375 | { |
1376 | struct rpc_xprt *xprt; | |
1377 | ||
1378 | read_lock(&sk->sk_callback_lock); | |
2a9e1cfa TM |
1379 | if (!(xprt = xprt_from_sock(sk))) |
1380 | goto out; | |
1381 | dprintk("RPC: %s client %p...\n" | |
1382 | "RPC: error %d\n", | |
1383 | __func__, xprt, sk->sk_err); | |
482f32e6 | 1384 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
2a9e1cfa TM |
1385 | out: |
1386 | read_unlock(&sk->sk_callback_lock); | |
1387 | } | |
1388 | ||
1f0fa154 IJ |
1389 | static void xs_write_space(struct sock *sk) |
1390 | { | |
1391 | struct socket *sock; | |
1392 | struct rpc_xprt *xprt; | |
1393 | ||
1394 | if (unlikely(!(sock = sk->sk_socket))) | |
1395 | return; | |
1396 | clear_bit(SOCK_NOSPACE, &sock->flags); | |
1397 | ||
1398 | if (unlikely(!(xprt = xprt_from_sock(sk)))) | |
1399 | return; | |
1400 | if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0) | |
1401 | return; | |
1402 | ||
1403 | xprt_write_space(xprt); | |
1404 | } | |
1405 | ||
9903cd1c | 1406 | /** |
c7b2cae8 CL |
1407 | * xs_udp_write_space - callback invoked when socket buffer space |
1408 | * becomes available | |
9903cd1c CL |
1409 | * @sk: socket whose state has changed |
1410 | * | |
a246b010 CL |
1411 | * Called when more output buffer space is available for this socket. |
1412 | * We try not to wake our writers until they can make "significant" | |
c7b2cae8 | 1413 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
a246b010 CL |
1414 | * with a bunch of small requests. |
1415 | */ | |
c7b2cae8 | 1416 | static void xs_udp_write_space(struct sock *sk) |
a246b010 | 1417 | { |
a246b010 | 1418 | read_lock(&sk->sk_callback_lock); |
a246b010 | 1419 | |
c7b2cae8 | 1420 | /* from net/core/sock.c:sock_def_write_space */ |
1f0fa154 IJ |
1421 | if (sock_writeable(sk)) |
1422 | xs_write_space(sk); | |
a246b010 | 1423 | |
c7b2cae8 CL |
1424 | read_unlock(&sk->sk_callback_lock); |
1425 | } | |
a246b010 | 1426 | |
c7b2cae8 CL |
1427 | /** |
1428 | * xs_tcp_write_space - callback invoked when socket buffer space | |
1429 | * becomes available | |
1430 | * @sk: socket whose state has changed | |
1431 | * | |
1432 | * Called when more output buffer space is available for this socket. | |
1433 | * We try not to wake our writers until they can make "significant" | |
1434 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg | |
1435 | * with a bunch of small requests. | |
1436 | */ | |
1437 | static void xs_tcp_write_space(struct sock *sk) | |
1438 | { | |
1439 | read_lock(&sk->sk_callback_lock); | |
1440 | ||
1441 | /* from net/core/stream.c:sk_stream_write_space */ | |
1f0fa154 IJ |
1442 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) |
1443 | xs_write_space(sk); | |
b6ddf64f | 1444 | |
a246b010 CL |
1445 | read_unlock(&sk->sk_callback_lock); |
1446 | } | |
1447 | ||
470056c2 | 1448 | static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) |
a246b010 | 1449 | { |
ee0ac0c2 CL |
1450 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
1451 | struct sock *sk = transport->inet; | |
a246b010 | 1452 | |
7c6e066e | 1453 | if (transport->rcvsize) { |
a246b010 | 1454 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
7c6e066e | 1455 | sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; |
a246b010 | 1456 | } |
7c6e066e | 1457 | if (transport->sndsize) { |
a246b010 | 1458 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
7c6e066e | 1459 | sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; |
a246b010 CL |
1460 | sk->sk_write_space(sk); |
1461 | } | |
1462 | } | |
1463 | ||
43118c29 | 1464 | /** |
470056c2 | 1465 | * xs_udp_set_buffer_size - set send and receive limits |
43118c29 | 1466 | * @xprt: generic transport |
470056c2 CL |
1467 | * @sndsize: requested size of send buffer, in bytes |
1468 | * @rcvsize: requested size of receive buffer, in bytes | |
43118c29 | 1469 | * |
470056c2 | 1470 | * Set socket send and receive buffer size limits. |
43118c29 | 1471 | */ |
470056c2 | 1472 | static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) |
43118c29 | 1473 | { |
7c6e066e CL |
1474 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
1475 | ||
1476 | transport->sndsize = 0; | |
470056c2 | 1477 | if (sndsize) |
7c6e066e CL |
1478 | transport->sndsize = sndsize + 1024; |
1479 | transport->rcvsize = 0; | |
470056c2 | 1480 | if (rcvsize) |
7c6e066e | 1481 | transport->rcvsize = rcvsize + 1024; |
470056c2 CL |
1482 | |
1483 | xs_udp_do_set_buffer_size(xprt); | |
43118c29 CL |
1484 | } |
1485 | ||
46c0ee8b CL |
1486 | /** |
1487 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport | |
1488 | * @task: task that timed out | |
1489 | * | |
1490 | * Adjust the congestion window after a retransmit timeout has occurred. | |
1491 | */ | |
1492 | static void xs_udp_timer(struct rpc_task *task) | |
1493 | { | |
1494 | xprt_adjust_cwnd(task, -ETIMEDOUT); | |
1495 | } | |
1496 | ||
b85d8806 CL |
1497 | static unsigned short xs_get_random_port(void) |
1498 | { | |
1499 | unsigned short range = xprt_max_resvport - xprt_min_resvport; | |
1500 | unsigned short rand = (unsigned short) net_random() % range; | |
1501 | return rand + xprt_min_resvport; | |
1502 | } | |
1503 | ||
92200412 CL |
1504 | /** |
1505 | * xs_set_port - reset the port number in the remote endpoint address | |
1506 | * @xprt: generic transport | |
1507 | * @port: new port number | |
1508 | * | |
1509 | */ | |
1510 | static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) | |
1511 | { | |
46121cf7 | 1512 | dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); |
c4efcb1d | 1513 | |
9dc3b095 CL |
1514 | rpc_set_port(xs_addr(xprt), port); |
1515 | xs_update_peer_port(xprt); | |
92200412 CL |
1516 | } |
1517 | ||
5d4ec932 | 1518 | static unsigned short xs_get_srcport(struct sock_xprt *transport) |
67a391d7 | 1519 | { |
fbfffbd5 | 1520 | unsigned short port = transport->srcport; |
67a391d7 TM |
1521 | |
1522 | if (port == 0 && transport->xprt.resvport) | |
1523 | port = xs_get_random_port(); | |
1524 | return port; | |
1525 | } | |
1526 | ||
baaf4e48 | 1527 | static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) |
67a391d7 | 1528 | { |
fbfffbd5 CL |
1529 | if (transport->srcport != 0) |
1530 | transport->srcport = 0; | |
67a391d7 TM |
1531 | if (!transport->xprt.resvport) |
1532 | return 0; | |
1533 | if (port <= xprt_min_resvport || port > xprt_max_resvport) | |
1534 | return xprt_max_resvport; | |
1535 | return --port; | |
1536 | } | |
1537 | ||
7dc753f0 | 1538 | static int xs_bind4(struct sock_xprt *transport, struct socket *sock) |
a246b010 CL |
1539 | { |
1540 | struct sockaddr_in myaddr = { | |
1541 | .sin_family = AF_INET, | |
1542 | }; | |
d3bc9a1d | 1543 | struct sockaddr_in *sa; |
67a391d7 | 1544 | int err, nloop = 0; |
5d4ec932 | 1545 | unsigned short port = xs_get_srcport(transport); |
67a391d7 | 1546 | unsigned short last; |
a246b010 | 1547 | |
fbfffbd5 | 1548 | sa = (struct sockaddr_in *)&transport->srcaddr; |
d3bc9a1d | 1549 | myaddr.sin_addr = sa->sin_addr; |
a246b010 CL |
1550 | do { |
1551 | myaddr.sin_port = htons(port); | |
e6242e92 | 1552 | err = kernel_bind(sock, (struct sockaddr *) &myaddr, |
a246b010 | 1553 | sizeof(myaddr)); |
67a391d7 | 1554 | if (port == 0) |
d3bc9a1d | 1555 | break; |
a246b010 | 1556 | if (err == 0) { |
fbfffbd5 | 1557 | transport->srcport = port; |
d3bc9a1d | 1558 | break; |
a246b010 | 1559 | } |
67a391d7 | 1560 | last = port; |
baaf4e48 | 1561 | port = xs_next_srcport(transport, port); |
67a391d7 TM |
1562 | if (port > last) |
1563 | nloop++; | |
1564 | } while (err == -EADDRINUSE && nloop != 2); | |
21454aaa HH |
1565 | dprintk("RPC: %s %pI4:%u: %s (%d)\n", |
1566 | __func__, &myaddr.sin_addr, | |
7dc753f0 | 1567 | port, err ? "failed" : "ok", err); |
a246b010 CL |
1568 | return err; |
1569 | } | |
1570 | ||
90058d37 CL |
1571 | static int xs_bind6(struct sock_xprt *transport, struct socket *sock) |
1572 | { | |
1573 | struct sockaddr_in6 myaddr = { | |
1574 | .sin6_family = AF_INET6, | |
1575 | }; | |
1576 | struct sockaddr_in6 *sa; | |
67a391d7 | 1577 | int err, nloop = 0; |
5d4ec932 | 1578 | unsigned short port = xs_get_srcport(transport); |
67a391d7 | 1579 | unsigned short last; |
90058d37 | 1580 | |
fbfffbd5 | 1581 | sa = (struct sockaddr_in6 *)&transport->srcaddr; |
90058d37 CL |
1582 | myaddr.sin6_addr = sa->sin6_addr; |
1583 | do { | |
1584 | myaddr.sin6_port = htons(port); | |
1585 | err = kernel_bind(sock, (struct sockaddr *) &myaddr, | |
1586 | sizeof(myaddr)); | |
67a391d7 | 1587 | if (port == 0) |
90058d37 CL |
1588 | break; |
1589 | if (err == 0) { | |
fbfffbd5 | 1590 | transport->srcport = port; |
90058d37 CL |
1591 | break; |
1592 | } | |
67a391d7 | 1593 | last = port; |
baaf4e48 | 1594 | port = xs_next_srcport(transport, port); |
67a391d7 TM |
1595 | if (port > last) |
1596 | nloop++; | |
1597 | } while (err == -EADDRINUSE && nloop != 2); | |
5b095d98 | 1598 | dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n", |
fdb46ee7 | 1599 | &myaddr.sin6_addr, port, err ? "failed" : "ok", err); |
a246b010 CL |
1600 | return err; |
1601 | } | |
1602 | ||
ed07536e PZ |
1603 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
1604 | static struct lock_class_key xs_key[2]; | |
1605 | static struct lock_class_key xs_slock_key[2]; | |
1606 | ||
8945ee5e | 1607 | static inline void xs_reclassify_socket4(struct socket *sock) |
ed07536e PZ |
1608 | { |
1609 | struct sock *sk = sock->sk; | |
8945ee5e | 1610 | |
02b3d346 | 1611 | BUG_ON(sock_owned_by_user(sk)); |
8945ee5e CL |
1612 | sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", |
1613 | &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); | |
1614 | } | |
ed07536e | 1615 | |
8945ee5e CL |
1616 | static inline void xs_reclassify_socket6(struct socket *sock) |
1617 | { | |
1618 | struct sock *sk = sock->sk; | |
ed07536e | 1619 | |
f4921aff | 1620 | BUG_ON(sock_owned_by_user(sk)); |
8945ee5e CL |
1621 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", |
1622 | &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); | |
ed07536e PZ |
1623 | } |
1624 | #else | |
8945ee5e CL |
1625 | static inline void xs_reclassify_socket4(struct socket *sock) |
1626 | { | |
1627 | } | |
1628 | ||
1629 | static inline void xs_reclassify_socket6(struct socket *sock) | |
ed07536e PZ |
1630 | { |
1631 | } | |
1632 | #endif | |
1633 | ||
22f79326 PE |
1634 | static struct socket *xs_create_sock4(struct rpc_xprt *xprt, |
1635 | struct sock_xprt *transport, int type, int protocol) | |
1636 | { | |
1637 | struct socket *sock; | |
1638 | int err; | |
1639 | ||
1640 | err = __sock_create(xprt->xprt_net, PF_INET, type, protocol, &sock, 1); | |
1641 | if (err < 0) { | |
1642 | dprintk("RPC: can't create %d transport socket (%d).\n", | |
1643 | protocol, -err); | |
1644 | goto out; | |
1645 | } | |
1646 | xs_reclassify_socket4(sock); | |
1647 | ||
1648 | if (xs_bind4(transport, sock)) { | |
1649 | sock_release(sock); | |
1650 | goto out; | |
1651 | } | |
1652 | ||
1653 | return sock; | |
1654 | out: | |
1655 | return ERR_PTR(err); | |
1656 | } | |
1657 | ||
22d44a7d PE |
1658 | static struct socket *xs_create_sock6(struct rpc_xprt *xprt, |
1659 | struct sock_xprt *transport, int type, int protocol) | |
1660 | { | |
1661 | struct socket *sock; | |
1662 | int err; | |
1663 | ||
1664 | err = __sock_create(xprt->xprt_net, PF_INET6, type, protocol, &sock, 1); | |
1665 | if (err < 0) { | |
1666 | dprintk("RPC: can't create %d transport socket (%d).\n", | |
1667 | protocol, -err); | |
1668 | goto out; | |
1669 | } | |
1670 | xs_reclassify_socket6(sock); | |
1671 | ||
1672 | if (xs_bind6(transport, sock)) { | |
1673 | sock_release(sock); | |
1674 | goto out; | |
1675 | } | |
1676 | ||
1677 | return sock; | |
1678 | out: | |
1679 | return ERR_PTR(err); | |
1680 | } | |
1681 | ||
16be2d20 CL |
1682 | static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
1683 | { | |
1684 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); | |
1685 | ||
1686 | if (!transport->inet) { | |
1687 | struct sock *sk = sock->sk; | |
1688 | ||
1689 | write_lock_bh(&sk->sk_callback_lock); | |
1690 | ||
2a9e1cfa TM |
1691 | xs_save_old_callbacks(transport, sk); |
1692 | ||
16be2d20 | 1693 | sk->sk_user_data = xprt; |
16be2d20 CL |
1694 | sk->sk_data_ready = xs_udp_data_ready; |
1695 | sk->sk_write_space = xs_udp_write_space; | |
482f32e6 | 1696 | sk->sk_error_report = xs_error_report; |
16be2d20 CL |
1697 | sk->sk_no_check = UDP_CSUM_NORCV; |
1698 | sk->sk_allocation = GFP_ATOMIC; | |
1699 | ||
1700 | xprt_set_connected(xprt); | |
1701 | ||
1702 | /* Reset to new socket */ | |
1703 | transport->sock = sock; | |
1704 | transport->inet = sk; | |
1705 | ||
1706 | write_unlock_bh(&sk->sk_callback_lock); | |
1707 | } | |
1708 | xs_udp_do_set_buffer_size(xprt); | |
1709 | } | |
1710 | ||
b65c0310 PE |
1711 | static void xs_udp_setup_socket(struct sock_xprt *transport, |
1712 | struct socket *(*create_sock)(struct rpc_xprt *, | |
1713 | struct sock_xprt *)) | |
a246b010 | 1714 | { |
c8475461 | 1715 | struct rpc_xprt *xprt = &transport->xprt; |
ee0ac0c2 | 1716 | struct socket *sock = transport->sock; |
b65c0310 | 1717 | int status = -EIO; |
9903cd1c | 1718 | |
01d37c42 | 1719 | if (xprt->shutdown) |
b0d93ad5 | 1720 | goto out; |
9903cd1c | 1721 | |
b0d93ad5 | 1722 | /* Start by resetting any existing state */ |
fe315e76 | 1723 | xs_reset_transport(transport); |
b65c0310 PE |
1724 | sock = create_sock(xprt, transport); |
1725 | if (IS_ERR(sock)) | |
b0d93ad5 | 1726 | goto out; |
9903cd1c | 1727 | |
c740eff8 CL |
1728 | dprintk("RPC: worker connecting xprt %p via %s to " |
1729 | "%s (port %s)\n", xprt, | |
1730 | xprt->address_strings[RPC_DISPLAY_PROTO], | |
1731 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
1732 | xprt->address_strings[RPC_DISPLAY_PORT]); | |
edb267a6 | 1733 | |
16be2d20 | 1734 | xs_udp_finish_connecting(xprt, sock); |
b0d93ad5 CL |
1735 | status = 0; |
1736 | out: | |
b0d93ad5 | 1737 | xprt_clear_connecting(xprt); |
7d1e8255 | 1738 | xprt_wake_pending_tasks(xprt, status); |
a246b010 | 1739 | } |
a246b010 | 1740 | |
68e220bd | 1741 | /** |
b65c0310 | 1742 | * xs_udp_connect_worker4 - set up a UDP socket |
68e220bd CL |
1743 | * @work: RPC transport to connect |
1744 | * | |
1745 | * Invoked by a work queue tasklet. | |
1746 | */ | |
b65c0310 PE |
1747 | |
1748 | static struct socket *xs_create_udp_sock4(struct rpc_xprt *xprt, | |
1749 | struct sock_xprt *transport) | |
1750 | { | |
22f79326 | 1751 | return xs_create_sock4(xprt, transport, SOCK_DGRAM, IPPROTO_UDP); |
b65c0310 PE |
1752 | } |
1753 | ||
1754 | static void xs_udp_connect_worker4(struct work_struct *work) | |
68e220bd CL |
1755 | { |
1756 | struct sock_xprt *transport = | |
1757 | container_of(work, struct sock_xprt, connect_worker.work); | |
a246b010 | 1758 | |
b65c0310 PE |
1759 | xs_udp_setup_socket(transport, xs_create_udp_sock4); |
1760 | } | |
b0d93ad5 | 1761 | |
b65c0310 PE |
1762 | /** |
1763 | * xs_udp_connect_worker6 - set up a UDP socket | |
1764 | * @work: RPC transport to connect | |
1765 | * | |
1766 | * Invoked by a work queue tasklet. | |
1767 | */ | |
1768 | ||
1769 | static struct socket *xs_create_udp_sock6(struct rpc_xprt *xprt, | |
1770 | struct sock_xprt *transport) | |
1771 | { | |
22d44a7d | 1772 | return xs_create_sock6(xprt, transport, SOCK_DGRAM, IPPROTO_UDP); |
b65c0310 PE |
1773 | } |
1774 | ||
1775 | static void xs_udp_connect_worker6(struct work_struct *work) | |
1776 | { | |
1777 | struct sock_xprt *transport = | |
1778 | container_of(work, struct sock_xprt, connect_worker.work); | |
1779 | ||
1780 | xs_udp_setup_socket(transport, xs_create_udp_sock6); | |
a246b010 CL |
1781 | } |
1782 | ||
3167e12c CL |
1783 | /* |
1784 | * We need to preserve the port number so the reply cache on the server can | |
1785 | * find our cached RPC replies when we get around to reconnecting. | |
1786 | */ | |
58dddac9 | 1787 | static void xs_abort_connection(struct sock_xprt *transport) |
3167e12c CL |
1788 | { |
1789 | int result; | |
3167e12c CL |
1790 | struct sockaddr any; |
1791 | ||
58dddac9 | 1792 | dprintk("RPC: disconnecting xprt %p to reuse port\n", transport); |
3167e12c CL |
1793 | |
1794 | /* | |
1795 | * Disconnect the transport socket by doing a connect operation | |
1796 | * with AF_UNSPEC. This should return immediately... | |
1797 | */ | |
1798 | memset(&any, 0, sizeof(any)); | |
1799 | any.sa_family = AF_UNSPEC; | |
ee0ac0c2 | 1800 | result = kernel_connect(transport->sock, &any, sizeof(any), 0); |
7d1e8255 | 1801 | if (!result) |
58dddac9 | 1802 | xs_sock_mark_closed(&transport->xprt); |
7d1e8255 | 1803 | else |
46121cf7 | 1804 | dprintk("RPC: AF_UNSPEC connect return code %d\n", |
3167e12c CL |
1805 | result); |
1806 | } | |
1807 | ||
58dddac9 | 1808 | static void xs_tcp_reuse_connection(struct sock_xprt *transport) |
40d2549d TM |
1809 | { |
1810 | unsigned int state = transport->inet->sk_state; | |
1811 | ||
669502ff AC |
1812 | if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) { |
1813 | /* we don't need to abort the connection if the socket | |
1814 | * hasn't undergone a shutdown | |
1815 | */ | |
1816 | if (transport->inet->sk_shutdown == 0) | |
1817 | return; | |
1818 | dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n", | |
1819 | __func__, transport->inet->sk_shutdown); | |
1820 | } | |
1821 | if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) { | |
1822 | /* we don't need to abort the connection if the socket | |
1823 | * hasn't undergone a shutdown | |
1824 | */ | |
1825 | if (transport->inet->sk_shutdown == 0) | |
1826 | return; | |
1827 | dprintk("RPC: %s: ESTABLISHED/SYN_SENT " | |
1828 | "sk_shutdown set to %d\n", | |
1829 | __func__, transport->inet->sk_shutdown); | |
1830 | } | |
58dddac9 | 1831 | xs_abort_connection(transport); |
40d2549d TM |
1832 | } |
1833 | ||
16be2d20 | 1834 | static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
a246b010 | 1835 | { |
16be2d20 | 1836 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
edb267a6 | 1837 | |
ee0ac0c2 | 1838 | if (!transport->inet) { |
b0d93ad5 CL |
1839 | struct sock *sk = sock->sk; |
1840 | ||
1841 | write_lock_bh(&sk->sk_callback_lock); | |
1842 | ||
2a9e1cfa TM |
1843 | xs_save_old_callbacks(transport, sk); |
1844 | ||
b0d93ad5 | 1845 | sk->sk_user_data = xprt; |
b0d93ad5 CL |
1846 | sk->sk_data_ready = xs_tcp_data_ready; |
1847 | sk->sk_state_change = xs_tcp_state_change; | |
1848 | sk->sk_write_space = xs_tcp_write_space; | |
482f32e6 | 1849 | sk->sk_error_report = xs_error_report; |
b079fa7b | 1850 | sk->sk_allocation = GFP_ATOMIC; |
3167e12c CL |
1851 | |
1852 | /* socket options */ | |
1853 | sk->sk_userlocks |= SOCK_BINDPORT_LOCK; | |
1854 | sock_reset_flag(sk, SOCK_LINGER); | |
1855 | tcp_sk(sk)->linger2 = 0; | |
1856 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; | |
b0d93ad5 CL |
1857 | |
1858 | xprt_clear_connected(xprt); | |
1859 | ||
1860 | /* Reset to new socket */ | |
ee0ac0c2 CL |
1861 | transport->sock = sock; |
1862 | transport->inet = sk; | |
b0d93ad5 CL |
1863 | |
1864 | write_unlock_bh(&sk->sk_callback_lock); | |
1865 | } | |
1866 | ||
01d37c42 TM |
1867 | if (!xprt_bound(xprt)) |
1868 | return -ENOTCONN; | |
1869 | ||
b0d93ad5 | 1870 | /* Tell the socket layer to start connecting... */ |
262ca07d CL |
1871 | xprt->stat.connect_count++; |
1872 | xprt->stat.connect_start = jiffies; | |
95392c59 | 1873 | return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); |
16be2d20 CL |
1874 | } |
1875 | ||
9903cd1c | 1876 | /** |
b61d59ff TM |
1877 | * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint |
1878 | * @xprt: RPC transport to connect | |
1879 | * @transport: socket transport to connect | |
1880 | * @create_sock: function to create a socket of the correct type | |
9903cd1c CL |
1881 | * |
1882 | * Invoked by a work queue tasklet. | |
a246b010 | 1883 | */ |
a9f5f0f7 | 1884 | static void xs_tcp_setup_socket(struct sock_xprt *transport, |
b61d59ff TM |
1885 | struct socket *(*create_sock)(struct rpc_xprt *, |
1886 | struct sock_xprt *)) | |
a246b010 | 1887 | { |
ee0ac0c2 | 1888 | struct socket *sock = transport->sock; |
a9f5f0f7 | 1889 | struct rpc_xprt *xprt = &transport->xprt; |
b61d59ff | 1890 | int status = -EIO; |
a246b010 | 1891 | |
01d37c42 | 1892 | if (xprt->shutdown) |
a246b010 CL |
1893 | goto out; |
1894 | ||
ee0ac0c2 | 1895 | if (!sock) { |
7d1e8255 | 1896 | clear_bit(XPRT_CONNECTION_ABORT, &xprt->state); |
b61d59ff TM |
1897 | sock = create_sock(xprt, transport); |
1898 | if (IS_ERR(sock)) { | |
1899 | status = PTR_ERR(sock); | |
3167e12c CL |
1900 | goto out; |
1901 | } | |
7d1e8255 TM |
1902 | } else { |
1903 | int abort_and_exit; | |
a246b010 | 1904 | |
7d1e8255 TM |
1905 | abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT, |
1906 | &xprt->state); | |
3167e12c | 1907 | /* "close" the socket, preserving the local port */ |
58dddac9 | 1908 | xs_tcp_reuse_connection(transport); |
a246b010 | 1909 | |
7d1e8255 TM |
1910 | if (abort_and_exit) |
1911 | goto out_eagain; | |
1912 | } | |
a246b010 | 1913 | |
c740eff8 CL |
1914 | dprintk("RPC: worker connecting xprt %p via %s to " |
1915 | "%s (port %s)\n", xprt, | |
1916 | xprt->address_strings[RPC_DISPLAY_PROTO], | |
1917 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
1918 | xprt->address_strings[RPC_DISPLAY_PORT]); | |
edb267a6 | 1919 | |
16be2d20 | 1920 | status = xs_tcp_finish_connecting(xprt, sock); |
46121cf7 CL |
1921 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
1922 | xprt, -status, xprt_connected(xprt), | |
1923 | sock->sk->sk_state); | |
2a491991 | 1924 | switch (status) { |
f75e6745 TM |
1925 | default: |
1926 | printk("%s: connect returned unhandled error %d\n", | |
1927 | __func__, status); | |
1928 | case -EADDRNOTAVAIL: | |
1929 | /* We're probably in TIME_WAIT. Get rid of existing socket, | |
1930 | * and retry | |
1931 | */ | |
1932 | set_bit(XPRT_CONNECTION_CLOSE, &xprt->state); | |
1933 | xprt_force_disconnect(xprt); | |
88b5ed73 | 1934 | break; |
8a2cec29 TM |
1935 | case -ECONNREFUSED: |
1936 | case -ECONNRESET: | |
1937 | case -ENETUNREACH: | |
1938 | /* retry with existing socket, after a delay */ | |
2a491991 TM |
1939 | case 0: |
1940 | case -EINPROGRESS: | |
1941 | case -EALREADY: | |
7d1e8255 TM |
1942 | xprt_clear_connecting(xprt); |
1943 | return; | |
9fcfe0c8 TM |
1944 | case -EINVAL: |
1945 | /* Happens, for instance, if the user specified a link | |
1946 | * local IPv6 address without a scope-id. | |
1947 | */ | |
1948 | goto out; | |
a246b010 | 1949 | } |
7d1e8255 | 1950 | out_eagain: |
2a491991 | 1951 | status = -EAGAIN; |
a246b010 | 1952 | out: |
68e220bd | 1953 | xprt_clear_connecting(xprt); |
7d1e8255 | 1954 | xprt_wake_pending_tasks(xprt, status); |
68e220bd | 1955 | } |
b0d93ad5 | 1956 | |
b61d59ff TM |
1957 | static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt, |
1958 | struct sock_xprt *transport) | |
1959 | { | |
22f79326 | 1960 | return xs_create_sock4(xprt, transport, SOCK_STREAM, IPPROTO_TCP); |
68e220bd | 1961 | } |
b0d93ad5 | 1962 | |
68e220bd | 1963 | /** |
b61d59ff | 1964 | * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint |
68e220bd CL |
1965 | * @work: RPC transport to connect |
1966 | * | |
1967 | * Invoked by a work queue tasklet. | |
1968 | */ | |
b61d59ff | 1969 | static void xs_tcp_connect_worker4(struct work_struct *work) |
68e220bd CL |
1970 | { |
1971 | struct sock_xprt *transport = | |
1972 | container_of(work, struct sock_xprt, connect_worker.work); | |
3167e12c | 1973 | |
a9f5f0f7 | 1974 | xs_tcp_setup_socket(transport, xs_create_tcp_sock4); |
b61d59ff | 1975 | } |
b0d93ad5 | 1976 | |
b61d59ff TM |
1977 | static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt, |
1978 | struct sock_xprt *transport) | |
1979 | { | |
22d44a7d | 1980 | return xs_create_sock6(xprt, transport, SOCK_STREAM, IPPROTO_TCP); |
b61d59ff | 1981 | } |
b0d93ad5 | 1982 | |
b61d59ff TM |
1983 | /** |
1984 | * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint | |
1985 | * @work: RPC transport to connect | |
1986 | * | |
1987 | * Invoked by a work queue tasklet. | |
1988 | */ | |
1989 | static void xs_tcp_connect_worker6(struct work_struct *work) | |
1990 | { | |
1991 | struct sock_xprt *transport = | |
1992 | container_of(work, struct sock_xprt, connect_worker.work); | |
b0d93ad5 | 1993 | |
a9f5f0f7 | 1994 | xs_tcp_setup_socket(transport, xs_create_tcp_sock6); |
a246b010 CL |
1995 | } |
1996 | ||
9903cd1c CL |
1997 | /** |
1998 | * xs_connect - connect a socket to a remote endpoint | |
1999 | * @task: address of RPC task that manages state of connect request | |
2000 | * | |
2001 | * TCP: If the remote end dropped the connection, delay reconnecting. | |
03bf4b70 CL |
2002 | * |
2003 | * UDP socket connects are synchronous, but we use a work queue anyway | |
2004 | * to guarantee that even unprivileged user processes can set up a | |
2005 | * socket on a privileged port. | |
2006 | * | |
2007 | * If a UDP socket connect fails, the delay behavior here prevents | |
2008 | * retry floods (hard mounts). | |
9903cd1c CL |
2009 | */ |
2010 | static void xs_connect(struct rpc_task *task) | |
a246b010 CL |
2011 | { |
2012 | struct rpc_xprt *xprt = task->tk_xprt; | |
ee0ac0c2 | 2013 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
a246b010 | 2014 | |
09a21c41 | 2015 | if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) { |
46121cf7 CL |
2016 | dprintk("RPC: xs_connect delayed xprt %p for %lu " |
2017 | "seconds\n", | |
03bf4b70 | 2018 | xprt, xprt->reestablish_timeout / HZ); |
c1384c9c TM |
2019 | queue_delayed_work(rpciod_workqueue, |
2020 | &transport->connect_worker, | |
2021 | xprt->reestablish_timeout); | |
03bf4b70 | 2022 | xprt->reestablish_timeout <<= 1; |
61d0a8e6 NB |
2023 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
2024 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; | |
03bf4b70 CL |
2025 | if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) |
2026 | xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; | |
b0d93ad5 | 2027 | } else { |
46121cf7 | 2028 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
c1384c9c TM |
2029 | queue_delayed_work(rpciod_workqueue, |
2030 | &transport->connect_worker, 0); | |
a246b010 CL |
2031 | } |
2032 | } | |
2033 | ||
262ca07d CL |
2034 | /** |
2035 | * xs_udp_print_stats - display UDP socket-specifc stats | |
2036 | * @xprt: rpc_xprt struct containing statistics | |
2037 | * @seq: output file | |
2038 | * | |
2039 | */ | |
2040 | static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) | |
2041 | { | |
c8475461 CL |
2042 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
2043 | ||
262ca07d | 2044 | seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", |
fbfffbd5 | 2045 | transport->srcport, |
262ca07d CL |
2046 | xprt->stat.bind_count, |
2047 | xprt->stat.sends, | |
2048 | xprt->stat.recvs, | |
2049 | xprt->stat.bad_xids, | |
2050 | xprt->stat.req_u, | |
2051 | xprt->stat.bklog_u); | |
2052 | } | |
2053 | ||
2054 | /** | |
2055 | * xs_tcp_print_stats - display TCP socket-specifc stats | |
2056 | * @xprt: rpc_xprt struct containing statistics | |
2057 | * @seq: output file | |
2058 | * | |
2059 | */ | |
2060 | static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) | |
2061 | { | |
c8475461 | 2062 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
262ca07d CL |
2063 | long idle_time = 0; |
2064 | ||
2065 | if (xprt_connected(xprt)) | |
2066 | idle_time = (long)(jiffies - xprt->last_used) / HZ; | |
2067 | ||
2068 | seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", | |
fbfffbd5 | 2069 | transport->srcport, |
262ca07d CL |
2070 | xprt->stat.bind_count, |
2071 | xprt->stat.connect_count, | |
2072 | xprt->stat.connect_time, | |
2073 | idle_time, | |
2074 | xprt->stat.sends, | |
2075 | xprt->stat.recvs, | |
2076 | xprt->stat.bad_xids, | |
2077 | xprt->stat.req_u, | |
2078 | xprt->stat.bklog_u); | |
2079 | } | |
2080 | ||
4cfc7e60 RI |
2081 | /* |
2082 | * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason | |
2083 | * we allocate pages instead doing a kmalloc like rpc_malloc is because we want | |
2084 | * to use the server side send routines. | |
2085 | */ | |
5a51f13a | 2086 | static void *bc_malloc(struct rpc_task *task, size_t size) |
4cfc7e60 RI |
2087 | { |
2088 | struct page *page; | |
2089 | struct rpc_buffer *buf; | |
2090 | ||
2091 | BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer)); | |
2092 | page = alloc_page(GFP_KERNEL); | |
2093 | ||
2094 | if (!page) | |
2095 | return NULL; | |
2096 | ||
2097 | buf = page_address(page); | |
2098 | buf->len = PAGE_SIZE; | |
2099 | ||
2100 | return buf->data; | |
2101 | } | |
2102 | ||
2103 | /* | |
2104 | * Free the space allocated in the bc_alloc routine | |
2105 | */ | |
5a51f13a | 2106 | static void bc_free(void *buffer) |
4cfc7e60 RI |
2107 | { |
2108 | struct rpc_buffer *buf; | |
2109 | ||
2110 | if (!buffer) | |
2111 | return; | |
2112 | ||
2113 | buf = container_of(buffer, struct rpc_buffer, data); | |
2114 | free_page((unsigned long)buf); | |
2115 | } | |
2116 | ||
2117 | /* | |
2118 | * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex | |
2119 | * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. | |
2120 | */ | |
2121 | static int bc_sendto(struct rpc_rqst *req) | |
2122 | { | |
2123 | int len; | |
2124 | struct xdr_buf *xbufp = &req->rq_snd_buf; | |
2125 | struct rpc_xprt *xprt = req->rq_xprt; | |
2126 | struct sock_xprt *transport = | |
2127 | container_of(xprt, struct sock_xprt, xprt); | |
2128 | struct socket *sock = transport->sock; | |
2129 | unsigned long headoff; | |
2130 | unsigned long tailoff; | |
2131 | ||
2132 | /* | |
2133 | * Set up the rpc header and record marker stuff | |
2134 | */ | |
2135 | xs_encode_tcp_record_marker(xbufp); | |
2136 | ||
2137 | tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; | |
2138 | headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; | |
2139 | len = svc_send_common(sock, xbufp, | |
2140 | virt_to_page(xbufp->head[0].iov_base), headoff, | |
2141 | xbufp->tail[0].iov_base, tailoff); | |
2142 | ||
2143 | if (len != xbufp->len) { | |
2144 | printk(KERN_NOTICE "Error sending entire callback!\n"); | |
2145 | len = -EAGAIN; | |
2146 | } | |
2147 | ||
2148 | return len; | |
2149 | } | |
2150 | ||
2151 | /* | |
2152 | * The send routine. Borrows from svc_send | |
2153 | */ | |
2154 | static int bc_send_request(struct rpc_task *task) | |
2155 | { | |
2156 | struct rpc_rqst *req = task->tk_rqstp; | |
2157 | struct svc_xprt *xprt; | |
2158 | struct svc_sock *svsk; | |
2159 | u32 len; | |
2160 | ||
2161 | dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); | |
2162 | /* | |
2163 | * Get the server socket associated with this callback xprt | |
2164 | */ | |
2165 | xprt = req->rq_xprt->bc_xprt; | |
2166 | svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
2167 | ||
2168 | /* | |
2169 | * Grab the mutex to serialize data as the connection is shared | |
2170 | * with the fore channel | |
2171 | */ | |
2172 | if (!mutex_trylock(&xprt->xpt_mutex)) { | |
2173 | rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL); | |
2174 | if (!mutex_trylock(&xprt->xpt_mutex)) | |
2175 | return -EAGAIN; | |
2176 | rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task); | |
2177 | } | |
2178 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) | |
2179 | len = -ENOTCONN; | |
2180 | else | |
2181 | len = bc_sendto(req); | |
2182 | mutex_unlock(&xprt->xpt_mutex); | |
2183 | ||
2184 | if (len > 0) | |
2185 | len = 0; | |
2186 | ||
2187 | return len; | |
2188 | } | |
2189 | ||
2190 | /* | |
2191 | * The close routine. Since this is client initiated, we do nothing | |
2192 | */ | |
2193 | ||
2194 | static void bc_close(struct rpc_xprt *xprt) | |
2195 | { | |
4cfc7e60 RI |
2196 | } |
2197 | ||
2198 | /* | |
2199 | * The xprt destroy routine. Again, because this connection is client | |
2200 | * initiated, we do nothing | |
2201 | */ | |
2202 | ||
2203 | static void bc_destroy(struct rpc_xprt *xprt) | |
2204 | { | |
4cfc7e60 RI |
2205 | } |
2206 | ||
262965f5 | 2207 | static struct rpc_xprt_ops xs_udp_ops = { |
43118c29 | 2208 | .set_buffer_size = xs_udp_set_buffer_size, |
12a80469 | 2209 | .reserve_xprt = xprt_reserve_xprt_cong, |
49e9a890 | 2210 | .release_xprt = xprt_release_xprt_cong, |
45160d62 | 2211 | .rpcbind = rpcb_getport_async, |
92200412 | 2212 | .set_port = xs_set_port, |
262965f5 | 2213 | .connect = xs_connect, |
02107148 CL |
2214 | .buf_alloc = rpc_malloc, |
2215 | .buf_free = rpc_free, | |
262965f5 | 2216 | .send_request = xs_udp_send_request, |
fe3aca29 | 2217 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
46c0ee8b | 2218 | .timer = xs_udp_timer, |
a58dd398 | 2219 | .release_request = xprt_release_rqst_cong, |
262965f5 CL |
2220 | .close = xs_close, |
2221 | .destroy = xs_destroy, | |
262ca07d | 2222 | .print_stats = xs_udp_print_stats, |
262965f5 CL |
2223 | }; |
2224 | ||
2225 | static struct rpc_xprt_ops xs_tcp_ops = { | |
12a80469 | 2226 | .reserve_xprt = xprt_reserve_xprt, |
e0ab53de | 2227 | .release_xprt = xs_tcp_release_xprt, |
45160d62 | 2228 | .rpcbind = rpcb_getport_async, |
92200412 | 2229 | .set_port = xs_set_port, |
0b9e7943 | 2230 | .connect = xs_connect, |
02107148 CL |
2231 | .buf_alloc = rpc_malloc, |
2232 | .buf_free = rpc_free, | |
262965f5 | 2233 | .send_request = xs_tcp_send_request, |
fe3aca29 | 2234 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
f75e6745 | 2235 | .close = xs_tcp_close, |
9903cd1c | 2236 | .destroy = xs_destroy, |
262ca07d | 2237 | .print_stats = xs_tcp_print_stats, |
a246b010 CL |
2238 | }; |
2239 | ||
4cfc7e60 RI |
2240 | /* |
2241 | * The rpc_xprt_ops for the server backchannel | |
2242 | */ | |
2243 | ||
2244 | static struct rpc_xprt_ops bc_tcp_ops = { | |
2245 | .reserve_xprt = xprt_reserve_xprt, | |
2246 | .release_xprt = xprt_release_xprt, | |
2247 | .buf_alloc = bc_malloc, | |
2248 | .buf_free = bc_free, | |
2249 | .send_request = bc_send_request, | |
2250 | .set_retrans_timeout = xprt_set_retrans_timeout_def, | |
2251 | .close = bc_close, | |
2252 | .destroy = bc_destroy, | |
2253 | .print_stats = xs_tcp_print_stats, | |
2254 | }; | |
2255 | ||
3c341b0b | 2256 | static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, |
bc25571e | 2257 | unsigned int slot_table_size) |
c8541ecd CL |
2258 | { |
2259 | struct rpc_xprt *xprt; | |
ffc2e518 | 2260 | struct sock_xprt *new; |
c8541ecd | 2261 | |
96802a09 | 2262 | if (args->addrlen > sizeof(xprt->addr)) { |
46121cf7 | 2263 | dprintk("RPC: xs_setup_xprt: address too large\n"); |
c8541ecd CL |
2264 | return ERR_PTR(-EBADF); |
2265 | } | |
2266 | ||
37aa2133 | 2267 | xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size); |
bd1722d4 | 2268 | if (xprt == NULL) { |
46121cf7 CL |
2269 | dprintk("RPC: xs_setup_xprt: couldn't allocate " |
2270 | "rpc_xprt\n"); | |
c8541ecd CL |
2271 | return ERR_PTR(-ENOMEM); |
2272 | } | |
c8541ecd | 2273 | |
bd1722d4 | 2274 | new = container_of(xprt, struct sock_xprt, xprt); |
96802a09 FM |
2275 | memcpy(&xprt->addr, args->dstaddr, args->addrlen); |
2276 | xprt->addrlen = args->addrlen; | |
d3bc9a1d | 2277 | if (args->srcaddr) |
fbfffbd5 | 2278 | memcpy(&new->srcaddr, args->srcaddr, args->addrlen); |
c8541ecd CL |
2279 | |
2280 | return xprt; | |
2281 | } | |
2282 | ||
2881ae74 TM |
2283 | static const struct rpc_timeout xs_udp_default_timeout = { |
2284 | .to_initval = 5 * HZ, | |
2285 | .to_maxval = 30 * HZ, | |
2286 | .to_increment = 5 * HZ, | |
2287 | .to_retries = 5, | |
2288 | }; | |
2289 | ||
9903cd1c CL |
2290 | /** |
2291 | * xs_setup_udp - Set up transport to use a UDP socket | |
96802a09 | 2292 | * @args: rpc transport creation arguments |
9903cd1c CL |
2293 | * |
2294 | */ | |
483066d6 | 2295 | static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) |
a246b010 | 2296 | { |
8f9d5b1a | 2297 | struct sockaddr *addr = args->dstaddr; |
c8541ecd | 2298 | struct rpc_xprt *xprt; |
c8475461 | 2299 | struct sock_xprt *transport; |
0a68b0be | 2300 | struct rpc_xprt *ret; |
a246b010 | 2301 | |
96802a09 | 2302 | xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); |
c8541ecd CL |
2303 | if (IS_ERR(xprt)) |
2304 | return xprt; | |
c8475461 | 2305 | transport = container_of(xprt, struct sock_xprt, xprt); |
a246b010 | 2306 | |
ec739ef0 | 2307 | xprt->prot = IPPROTO_UDP; |
808012fb | 2308 | xprt->tsh_size = 0; |
a246b010 CL |
2309 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
2310 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); | |
2311 | ||
03bf4b70 | 2312 | xprt->bind_timeout = XS_BIND_TO; |
03bf4b70 CL |
2313 | xprt->reestablish_timeout = XS_UDP_REEST_TO; |
2314 | xprt->idle_timeout = XS_IDLE_DISC_TO; | |
a246b010 | 2315 | |
262965f5 | 2316 | xprt->ops = &xs_udp_ops; |
a246b010 | 2317 | |
ba7392bb | 2318 | xprt->timeout = &xs_udp_default_timeout; |
a246b010 | 2319 | |
8f9d5b1a CL |
2320 | switch (addr->sa_family) { |
2321 | case AF_INET: | |
2322 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) | |
2323 | xprt_set_bound(xprt); | |
2324 | ||
2325 | INIT_DELAYED_WORK(&transport->connect_worker, | |
2326 | xs_udp_connect_worker4); | |
9dc3b095 | 2327 | xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); |
8f9d5b1a CL |
2328 | break; |
2329 | case AF_INET6: | |
2330 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) | |
2331 | xprt_set_bound(xprt); | |
2332 | ||
2333 | INIT_DELAYED_WORK(&transport->connect_worker, | |
2334 | xs_udp_connect_worker6); | |
9dc3b095 | 2335 | xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); |
8f9d5b1a CL |
2336 | break; |
2337 | default: | |
0a68b0be BF |
2338 | ret = ERR_PTR(-EAFNOSUPPORT); |
2339 | goto out_err; | |
8f9d5b1a CL |
2340 | } |
2341 | ||
c740eff8 CL |
2342 | if (xprt_bound(xprt)) |
2343 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", | |
2344 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2345 | xprt->address_strings[RPC_DISPLAY_PORT], | |
2346 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
2347 | else | |
2348 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", | |
2349 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2350 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
edb267a6 | 2351 | |
bc25571e TT |
2352 | if (try_module_get(THIS_MODULE)) |
2353 | return xprt; | |
0a68b0be BF |
2354 | ret = ERR_PTR(-EINVAL); |
2355 | out_err: | |
e204e621 | 2356 | xprt_free(xprt); |
0a68b0be | 2357 | return ret; |
a246b010 CL |
2358 | } |
2359 | ||
2881ae74 TM |
2360 | static const struct rpc_timeout xs_tcp_default_timeout = { |
2361 | .to_initval = 60 * HZ, | |
2362 | .to_maxval = 60 * HZ, | |
2363 | .to_retries = 2, | |
2364 | }; | |
2365 | ||
9903cd1c CL |
2366 | /** |
2367 | * xs_setup_tcp - Set up transport to use a TCP socket | |
96802a09 | 2368 | * @args: rpc transport creation arguments |
9903cd1c CL |
2369 | * |
2370 | */ | |
483066d6 | 2371 | static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) |
a246b010 | 2372 | { |
8f9d5b1a | 2373 | struct sockaddr *addr = args->dstaddr; |
c8541ecd | 2374 | struct rpc_xprt *xprt; |
c8475461 | 2375 | struct sock_xprt *transport; |
0a68b0be | 2376 | struct rpc_xprt *ret; |
a246b010 | 2377 | |
96802a09 | 2378 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); |
c8541ecd CL |
2379 | if (IS_ERR(xprt)) |
2380 | return xprt; | |
c8475461 | 2381 | transport = container_of(xprt, struct sock_xprt, xprt); |
a246b010 | 2382 | |
ec739ef0 | 2383 | xprt->prot = IPPROTO_TCP; |
808012fb | 2384 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
808012fb | 2385 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
a246b010 | 2386 | |
03bf4b70 | 2387 | xprt->bind_timeout = XS_BIND_TO; |
03bf4b70 CL |
2388 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
2389 | xprt->idle_timeout = XS_IDLE_DISC_TO; | |
a246b010 | 2390 | |
262965f5 | 2391 | xprt->ops = &xs_tcp_ops; |
ba7392bb | 2392 | xprt->timeout = &xs_tcp_default_timeout; |
a246b010 | 2393 | |
8f9d5b1a CL |
2394 | switch (addr->sa_family) { |
2395 | case AF_INET: | |
2396 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) | |
2397 | xprt_set_bound(xprt); | |
2398 | ||
9dc3b095 CL |
2399 | INIT_DELAYED_WORK(&transport->connect_worker, |
2400 | xs_tcp_connect_worker4); | |
2401 | xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); | |
8f9d5b1a CL |
2402 | break; |
2403 | case AF_INET6: | |
2404 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) | |
2405 | xprt_set_bound(xprt); | |
2406 | ||
9dc3b095 CL |
2407 | INIT_DELAYED_WORK(&transport->connect_worker, |
2408 | xs_tcp_connect_worker6); | |
2409 | xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); | |
8f9d5b1a CL |
2410 | break; |
2411 | default: | |
0a68b0be BF |
2412 | ret = ERR_PTR(-EAFNOSUPPORT); |
2413 | goto out_err; | |
8f9d5b1a CL |
2414 | } |
2415 | ||
c740eff8 CL |
2416 | if (xprt_bound(xprt)) |
2417 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", | |
2418 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2419 | xprt->address_strings[RPC_DISPLAY_PORT], | |
2420 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
2421 | else | |
2422 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", | |
2423 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2424 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
2425 | ||
edb267a6 | 2426 | |
bc25571e TT |
2427 | if (try_module_get(THIS_MODULE)) |
2428 | return xprt; | |
0a68b0be BF |
2429 | ret = ERR_PTR(-EINVAL); |
2430 | out_err: | |
e204e621 | 2431 | xprt_free(xprt); |
0a68b0be | 2432 | return ret; |
a246b010 | 2433 | } |
282b32e1 | 2434 | |
f300baba AB |
2435 | /** |
2436 | * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket | |
2437 | * @args: rpc transport creation arguments | |
2438 | * | |
2439 | */ | |
2440 | static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) | |
2441 | { | |
2442 | struct sockaddr *addr = args->dstaddr; | |
2443 | struct rpc_xprt *xprt; | |
2444 | struct sock_xprt *transport; | |
2445 | struct svc_sock *bc_sock; | |
0a68b0be | 2446 | struct rpc_xprt *ret; |
f300baba | 2447 | |
f300baba AB |
2448 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); |
2449 | if (IS_ERR(xprt)) | |
2450 | return xprt; | |
2451 | transport = container_of(xprt, struct sock_xprt, xprt); | |
2452 | ||
2453 | xprt->prot = IPPROTO_TCP; | |
2454 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); | |
2455 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; | |
2456 | xprt->timeout = &xs_tcp_default_timeout; | |
2457 | ||
2458 | /* backchannel */ | |
2459 | xprt_set_bound(xprt); | |
2460 | xprt->bind_timeout = 0; | |
f300baba AB |
2461 | xprt->reestablish_timeout = 0; |
2462 | xprt->idle_timeout = 0; | |
2463 | ||
2464 | /* | |
2465 | * The backchannel uses the same socket connection as the | |
2466 | * forechannel | |
2467 | */ | |
2468 | xprt->bc_xprt = args->bc_xprt; | |
2469 | bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); | |
2470 | bc_sock->sk_bc_xprt = xprt; | |
2471 | transport->sock = bc_sock->sk_sock; | |
2472 | transport->inet = bc_sock->sk_sk; | |
2473 | ||
2474 | xprt->ops = &bc_tcp_ops; | |
2475 | ||
2476 | switch (addr->sa_family) { | |
2477 | case AF_INET: | |
2478 | xs_format_peer_addresses(xprt, "tcp", | |
2479 | RPCBIND_NETID_TCP); | |
2480 | break; | |
2481 | case AF_INET6: | |
2482 | xs_format_peer_addresses(xprt, "tcp", | |
2483 | RPCBIND_NETID_TCP6); | |
2484 | break; | |
2485 | default: | |
0a68b0be BF |
2486 | ret = ERR_PTR(-EAFNOSUPPORT); |
2487 | goto out_err; | |
f300baba AB |
2488 | } |
2489 | ||
2490 | if (xprt_bound(xprt)) | |
2491 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", | |
2492 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2493 | xprt->address_strings[RPC_DISPLAY_PORT], | |
2494 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
2495 | else | |
2496 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", | |
2497 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2498 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
2499 | ||
2500 | /* | |
2501 | * Since we don't want connections for the backchannel, we set | |
2502 | * the xprt status to connected | |
2503 | */ | |
2504 | xprt_set_connected(xprt); | |
2505 | ||
2506 | ||
2507 | if (try_module_get(THIS_MODULE)) | |
2508 | return xprt; | |
0a68b0be BF |
2509 | ret = ERR_PTR(-EINVAL); |
2510 | out_err: | |
e204e621 | 2511 | xprt_free(xprt); |
0a68b0be | 2512 | return ret; |
f300baba AB |
2513 | } |
2514 | ||
bc25571e TT |
2515 | static struct xprt_class xs_udp_transport = { |
2516 | .list = LIST_HEAD_INIT(xs_udp_transport.list), | |
2517 | .name = "udp", | |
2518 | .owner = THIS_MODULE, | |
f300baba | 2519 | .ident = XPRT_TRANSPORT_UDP, |
bc25571e TT |
2520 | .setup = xs_setup_udp, |
2521 | }; | |
2522 | ||
2523 | static struct xprt_class xs_tcp_transport = { | |
2524 | .list = LIST_HEAD_INIT(xs_tcp_transport.list), | |
2525 | .name = "tcp", | |
2526 | .owner = THIS_MODULE, | |
f300baba | 2527 | .ident = XPRT_TRANSPORT_TCP, |
bc25571e TT |
2528 | .setup = xs_setup_tcp, |
2529 | }; | |
2530 | ||
f300baba AB |
2531 | static struct xprt_class xs_bc_tcp_transport = { |
2532 | .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), | |
2533 | .name = "tcp NFSv4.1 backchannel", | |
2534 | .owner = THIS_MODULE, | |
2535 | .ident = XPRT_TRANSPORT_BC_TCP, | |
2536 | .setup = xs_setup_bc_tcp, | |
2537 | }; | |
2538 | ||
282b32e1 | 2539 | /** |
bc25571e | 2540 | * init_socket_xprt - set up xprtsock's sysctls, register with RPC client |
282b32e1 CL |
2541 | * |
2542 | */ | |
2543 | int init_socket_xprt(void) | |
2544 | { | |
fbf76683 | 2545 | #ifdef RPC_DEBUG |
2b1bec5f | 2546 | if (!sunrpc_table_header) |
0b4d4147 | 2547 | sunrpc_table_header = register_sysctl_table(sunrpc_table); |
fbf76683 CL |
2548 | #endif |
2549 | ||
bc25571e TT |
2550 | xprt_register_transport(&xs_udp_transport); |
2551 | xprt_register_transport(&xs_tcp_transport); | |
f300baba | 2552 | xprt_register_transport(&xs_bc_tcp_transport); |
bc25571e | 2553 | |
282b32e1 CL |
2554 | return 0; |
2555 | } | |
2556 | ||
2557 | /** | |
bc25571e | 2558 | * cleanup_socket_xprt - remove xprtsock's sysctls, unregister |
282b32e1 CL |
2559 | * |
2560 | */ | |
2561 | void cleanup_socket_xprt(void) | |
2562 | { | |
fbf76683 CL |
2563 | #ifdef RPC_DEBUG |
2564 | if (sunrpc_table_header) { | |
2565 | unregister_sysctl_table(sunrpc_table_header); | |
2566 | sunrpc_table_header = NULL; | |
2567 | } | |
2568 | #endif | |
bc25571e TT |
2569 | |
2570 | xprt_unregister_transport(&xs_udp_transport); | |
2571 | xprt_unregister_transport(&xs_tcp_transport); | |
f300baba | 2572 | xprt_unregister_transport(&xs_bc_tcp_transport); |
282b32e1 | 2573 | } |
cbf11071 | 2574 | |
9bbb9e5a RR |
2575 | static int param_set_uint_minmax(const char *val, |
2576 | const struct kernel_param *kp, | |
cbf11071 TM |
2577 | unsigned int min, unsigned int max) |
2578 | { | |
2579 | unsigned long num; | |
2580 | int ret; | |
2581 | ||
2582 | if (!val) | |
2583 | return -EINVAL; | |
2584 | ret = strict_strtoul(val, 0, &num); | |
2585 | if (ret == -EINVAL || num < min || num > max) | |
2586 | return -EINVAL; | |
2587 | *((unsigned int *)kp->arg) = num; | |
2588 | return 0; | |
2589 | } | |
2590 | ||
9bbb9e5a | 2591 | static int param_set_portnr(const char *val, const struct kernel_param *kp) |
cbf11071 TM |
2592 | { |
2593 | return param_set_uint_minmax(val, kp, | |
2594 | RPC_MIN_RESVPORT, | |
2595 | RPC_MAX_RESVPORT); | |
2596 | } | |
2597 | ||
9bbb9e5a RR |
2598 | static struct kernel_param_ops param_ops_portnr = { |
2599 | .set = param_set_portnr, | |
2600 | .get = param_get_uint, | |
2601 | }; | |
2602 | ||
cbf11071 TM |
2603 | #define param_check_portnr(name, p) \ |
2604 | __param_check(name, p, unsigned int); | |
2605 | ||
2606 | module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); | |
2607 | module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); | |
2608 | ||
9bbb9e5a RR |
2609 | static int param_set_slot_table_size(const char *val, |
2610 | const struct kernel_param *kp) | |
cbf11071 TM |
2611 | { |
2612 | return param_set_uint_minmax(val, kp, | |
2613 | RPC_MIN_SLOT_TABLE, | |
2614 | RPC_MAX_SLOT_TABLE); | |
2615 | } | |
2616 | ||
9bbb9e5a RR |
2617 | static struct kernel_param_ops param_ops_slot_table_size = { |
2618 | .set = param_set_slot_table_size, | |
2619 | .get = param_get_uint, | |
2620 | }; | |
2621 | ||
cbf11071 TM |
2622 | #define param_check_slot_table_size(name, p) \ |
2623 | __param_check(name, p, unsigned int); | |
2624 | ||
2625 | module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, | |
2626 | slot_table_size, 0644); | |
2627 | module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, | |
2628 | slot_table_size, 0644); | |
2629 |