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