1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/net/sunrpc/xprtsock.c 4 * 5 * Client-side transport implementation for sockets. 6 * 7 * TCP callback races fixes (C) 1998 Red Hat 8 * TCP send fixes (C) 1998 Red Hat 9 * TCP NFS related read + write fixes 10 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 11 * 12 * Rewrite of larges part of the code in order to stabilize TCP stuff. 13 * Fix behaviour when socket buffer is full. 14 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 15 * 16 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 17 * 18 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 19 * <gilles.quillard@bull.net> 20 */ 21 22 #include <linux/types.h> 23 #include <linux/string.h> 24 #include <linux/slab.h> 25 #include <linux/module.h> 26 #include <linux/capability.h> 27 #include <linux/pagemap.h> 28 #include <linux/errno.h> 29 #include <linux/socket.h> 30 #include <linux/in.h> 31 #include <linux/net.h> 32 #include <linux/mm.h> 33 #include <linux/un.h> 34 #include <linux/udp.h> 35 #include <linux/tcp.h> 36 #include <linux/sunrpc/clnt.h> 37 #include <linux/sunrpc/addr.h> 38 #include <linux/sunrpc/sched.h> 39 #include <linux/sunrpc/svcsock.h> 40 #include <linux/sunrpc/xprtsock.h> 41 #include <linux/file.h> 42 #ifdef CONFIG_SUNRPC_BACKCHANNEL 43 #include <linux/sunrpc/bc_xprt.h> 44 #endif 45 46 #include <net/sock.h> 47 #include <net/checksum.h> 48 #include <net/udp.h> 49 #include <net/tcp.h> 50 #include <linux/bvec.h> 51 #include <linux/highmem.h> 52 #include <linux/uio.h> 53 #include <linux/sched/mm.h> 54 55 #include <trace/events/sunrpc.h> 56 57 #include "sunrpc.h" 58 59 static void xs_close(struct rpc_xprt *xprt); 60 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 61 struct socket *sock); 62 63 /* 64 * xprtsock tunables 65 */ 66 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 67 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 68 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 69 70 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 71 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 72 73 #define XS_TCP_LINGER_TO (15U * HZ) 74 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 75 76 /* 77 * We can register our own files under /proc/sys/sunrpc by 78 * calling register_sysctl_table() again. The files in that 79 * directory become the union of all files registered there. 80 * 81 * We simply need to make sure that we don't collide with 82 * someone else's file names! 83 */ 84 85 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 86 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 87 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 88 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 89 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 90 91 static struct ctl_table_header *sunrpc_table_header; 92 93 /* 94 * FIXME: changing the UDP slot table size should also resize the UDP 95 * socket buffers for existing UDP transports 96 */ 97 static struct ctl_table xs_tunables_table[] = { 98 { 99 .procname = "udp_slot_table_entries", 100 .data = &xprt_udp_slot_table_entries, 101 .maxlen = sizeof(unsigned int), 102 .mode = 0644, 103 .proc_handler = proc_dointvec_minmax, 104 .extra1 = &min_slot_table_size, 105 .extra2 = &max_slot_table_size 106 }, 107 { 108 .procname = "tcp_slot_table_entries", 109 .data = &xprt_tcp_slot_table_entries, 110 .maxlen = sizeof(unsigned int), 111 .mode = 0644, 112 .proc_handler = proc_dointvec_minmax, 113 .extra1 = &min_slot_table_size, 114 .extra2 = &max_slot_table_size 115 }, 116 { 117 .procname = "tcp_max_slot_table_entries", 118 .data = &xprt_max_tcp_slot_table_entries, 119 .maxlen = sizeof(unsigned int), 120 .mode = 0644, 121 .proc_handler = proc_dointvec_minmax, 122 .extra1 = &min_slot_table_size, 123 .extra2 = &max_tcp_slot_table_limit 124 }, 125 { 126 .procname = "min_resvport", 127 .data = &xprt_min_resvport, 128 .maxlen = sizeof(unsigned int), 129 .mode = 0644, 130 .proc_handler = proc_dointvec_minmax, 131 .extra1 = &xprt_min_resvport_limit, 132 .extra2 = &xprt_max_resvport_limit 133 }, 134 { 135 .procname = "max_resvport", 136 .data = &xprt_max_resvport, 137 .maxlen = sizeof(unsigned int), 138 .mode = 0644, 139 .proc_handler = proc_dointvec_minmax, 140 .extra1 = &xprt_min_resvport_limit, 141 .extra2 = &xprt_max_resvport_limit 142 }, 143 { 144 .procname = "tcp_fin_timeout", 145 .data = &xs_tcp_fin_timeout, 146 .maxlen = sizeof(xs_tcp_fin_timeout), 147 .mode = 0644, 148 .proc_handler = proc_dointvec_jiffies, 149 }, 150 { }, 151 }; 152 153 static struct ctl_table sunrpc_table[] = { 154 { 155 .procname = "sunrpc", 156 .mode = 0555, 157 .child = xs_tunables_table 158 }, 159 { }, 160 }; 161 162 /* 163 * Wait duration for a reply from the RPC portmapper. 164 */ 165 #define XS_BIND_TO (60U * HZ) 166 167 /* 168 * Delay if a UDP socket connect error occurs. This is most likely some 169 * kind of resource problem on the local host. 170 */ 171 #define XS_UDP_REEST_TO (2U * HZ) 172 173 /* 174 * The reestablish timeout allows clients to delay for a bit before attempting 175 * to reconnect to a server that just dropped our connection. 176 * 177 * We implement an exponential backoff when trying to reestablish a TCP 178 * transport connection with the server. Some servers like to drop a TCP 179 * connection when they are overworked, so we start with a short timeout and 180 * increase over time if the server is down or not responding. 181 */ 182 #define XS_TCP_INIT_REEST_TO (3U * HZ) 183 184 /* 185 * TCP idle timeout; client drops the transport socket if it is idle 186 * for this long. Note that we also timeout UDP sockets to prevent 187 * holding port numbers when there is no RPC traffic. 188 */ 189 #define XS_IDLE_DISC_TO (5U * 60 * HZ) 190 191 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 192 # undef RPC_DEBUG_DATA 193 # define RPCDBG_FACILITY RPCDBG_TRANS 194 #endif 195 196 #ifdef RPC_DEBUG_DATA 197 static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 198 { 199 u8 *buf = (u8 *) packet; 200 int j; 201 202 dprintk("RPC: %s\n", msg); 203 for (j = 0; j < count && j < 128; j += 4) { 204 if (!(j & 31)) { 205 if (j) 206 dprintk("\n"); 207 dprintk("0x%04x ", j); 208 } 209 dprintk("%02x%02x%02x%02x ", 210 buf[j], buf[j+1], buf[j+2], buf[j+3]); 211 } 212 dprintk("\n"); 213 } 214 #else 215 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 216 { 217 /* NOP */ 218 } 219 #endif 220 221 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 222 { 223 return (struct rpc_xprt *) sk->sk_user_data; 224 } 225 226 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 227 { 228 return (struct sockaddr *) &xprt->addr; 229 } 230 231 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 232 { 233 return (struct sockaddr_un *) &xprt->addr; 234 } 235 236 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 237 { 238 return (struct sockaddr_in *) &xprt->addr; 239 } 240 241 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 242 { 243 return (struct sockaddr_in6 *) &xprt->addr; 244 } 245 246 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 247 { 248 struct sockaddr *sap = xs_addr(xprt); 249 struct sockaddr_in6 *sin6; 250 struct sockaddr_in *sin; 251 struct sockaddr_un *sun; 252 char buf[128]; 253 254 switch (sap->sa_family) { 255 case AF_LOCAL: 256 sun = xs_addr_un(xprt); 257 strlcpy(buf, sun->sun_path, sizeof(buf)); 258 xprt->address_strings[RPC_DISPLAY_ADDR] = 259 kstrdup(buf, GFP_KERNEL); 260 break; 261 case AF_INET: 262 (void)rpc_ntop(sap, buf, sizeof(buf)); 263 xprt->address_strings[RPC_DISPLAY_ADDR] = 264 kstrdup(buf, GFP_KERNEL); 265 sin = xs_addr_in(xprt); 266 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 267 break; 268 case AF_INET6: 269 (void)rpc_ntop(sap, buf, sizeof(buf)); 270 xprt->address_strings[RPC_DISPLAY_ADDR] = 271 kstrdup(buf, GFP_KERNEL); 272 sin6 = xs_addr_in6(xprt); 273 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 274 break; 275 default: 276 BUG(); 277 } 278 279 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 280 } 281 282 static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 283 { 284 struct sockaddr *sap = xs_addr(xprt); 285 char buf[128]; 286 287 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 288 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 289 290 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 291 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 292 } 293 294 static void xs_format_peer_addresses(struct rpc_xprt *xprt, 295 const char *protocol, 296 const char *netid) 297 { 298 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 299 xprt->address_strings[RPC_DISPLAY_NETID] = netid; 300 xs_format_common_peer_addresses(xprt); 301 xs_format_common_peer_ports(xprt); 302 } 303 304 static void xs_update_peer_port(struct rpc_xprt *xprt) 305 { 306 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 307 kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 308 309 xs_format_common_peer_ports(xprt); 310 } 311 312 static void xs_free_peer_addresses(struct rpc_xprt *xprt) 313 { 314 unsigned int i; 315 316 for (i = 0; i < RPC_DISPLAY_MAX; i++) 317 switch (i) { 318 case RPC_DISPLAY_PROTO: 319 case RPC_DISPLAY_NETID: 320 continue; 321 default: 322 kfree(xprt->address_strings[i]); 323 } 324 } 325 326 static size_t 327 xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) 328 { 329 size_t i,n; 330 331 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) 332 return want; 333 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; 334 for (i = 0; i < n; i++) { 335 if (buf->pages[i]) 336 continue; 337 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); 338 if (!buf->pages[i]) { 339 i *= PAGE_SIZE; 340 return i > buf->page_base ? i - buf->page_base : 0; 341 } 342 } 343 return want; 344 } 345 346 static ssize_t 347 xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 348 { 349 ssize_t ret; 350 if (seek != 0) 351 iov_iter_advance(&msg->msg_iter, seek); 352 ret = sock_recvmsg(sock, msg, flags); 353 return ret > 0 ? ret + seek : ret; 354 } 355 356 static ssize_t 357 xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 358 struct kvec *kvec, size_t count, size_t seek) 359 { 360 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count); 361 return xs_sock_recvmsg(sock, msg, flags, seek); 362 } 363 364 static ssize_t 365 xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 366 struct bio_vec *bvec, unsigned long nr, size_t count, 367 size_t seek) 368 { 369 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count); 370 return xs_sock_recvmsg(sock, msg, flags, seek); 371 } 372 373 static ssize_t 374 xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 375 size_t count) 376 { 377 iov_iter_discard(&msg->msg_iter, READ, count); 378 return sock_recvmsg(sock, msg, flags); 379 } 380 381 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 382 static void 383 xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 384 { 385 struct bvec_iter bi = { 386 .bi_size = count, 387 }; 388 struct bio_vec bv; 389 390 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); 391 for_each_bvec(bv, bvec, bi, bi) 392 flush_dcache_page(bv.bv_page); 393 } 394 #else 395 static inline void 396 xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) 397 { 398 } 399 #endif 400 401 static ssize_t 402 xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 403 struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 404 { 405 size_t want, seek_init = seek, offset = 0; 406 ssize_t ret; 407 408 if (seek < buf->head[0].iov_len) { 409 want = min_t(size_t, count, buf->head[0].iov_len); 410 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 411 if (ret <= 0) 412 goto sock_err; 413 offset += ret; 414 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 415 goto out; 416 if (ret != want) 417 goto out; 418 seek = 0; 419 } else { 420 seek -= buf->head[0].iov_len; 421 offset += buf->head[0].iov_len; 422 } 423 424 want = xs_alloc_sparse_pages(buf, 425 min_t(size_t, count - offset, buf->page_len), 426 GFP_NOWAIT); 427 if (seek < want) { 428 ret = xs_read_bvec(sock, msg, flags, buf->bvec, 429 xdr_buf_pagecount(buf), 430 want + buf->page_base, 431 seek + buf->page_base); 432 if (ret <= 0) 433 goto sock_err; 434 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); 435 offset += ret - buf->page_base; 436 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 437 goto out; 438 if (ret != want) 439 goto out; 440 seek = 0; 441 } else { 442 seek -= want; 443 offset += want; 444 } 445 446 if (seek < buf->tail[0].iov_len) { 447 want = min_t(size_t, count - offset, buf->tail[0].iov_len); 448 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 449 if (ret <= 0) 450 goto sock_err; 451 offset += ret; 452 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 453 goto out; 454 if (ret != want) 455 goto out; 456 } else 457 offset += buf->tail[0].iov_len; 458 ret = -EMSGSIZE; 459 out: 460 *read = offset - seek_init; 461 return ret; 462 sock_err: 463 offset += seek; 464 goto out; 465 } 466 467 static void 468 xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 469 { 470 if (!transport->recv.copied) { 471 if (buf->head[0].iov_len >= transport->recv.offset) 472 memcpy(buf->head[0].iov_base, 473 &transport->recv.xid, 474 transport->recv.offset); 475 transport->recv.copied = transport->recv.offset; 476 } 477 } 478 479 static bool 480 xs_read_stream_request_done(struct sock_xprt *transport) 481 { 482 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 483 } 484 485 static ssize_t 486 xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 487 int flags, struct rpc_rqst *req) 488 { 489 struct xdr_buf *buf = &req->rq_private_buf; 490 size_t want, read; 491 ssize_t ret; 492 493 xs_read_header(transport, buf); 494 495 want = transport->recv.len - transport->recv.offset; 496 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 497 transport->recv.copied + want, transport->recv.copied, 498 &read); 499 transport->recv.offset += read; 500 transport->recv.copied += read; 501 if (transport->recv.offset == transport->recv.len) { 502 if (xs_read_stream_request_done(transport)) 503 msg->msg_flags |= MSG_EOR; 504 return read; 505 } 506 507 switch (ret) { 508 default: 509 break; 510 case -EFAULT: 511 case -EMSGSIZE: 512 msg->msg_flags |= MSG_TRUNC; 513 return read; 514 case 0: 515 return -ESHUTDOWN; 516 } 517 return ret < 0 ? ret : read; 518 } 519 520 static size_t 521 xs_read_stream_headersize(bool isfrag) 522 { 523 if (isfrag) 524 return sizeof(__be32); 525 return 3 * sizeof(__be32); 526 } 527 528 static ssize_t 529 xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 530 int flags, size_t want, size_t seek) 531 { 532 struct kvec kvec = { 533 .iov_base = &transport->recv.fraghdr, 534 .iov_len = want, 535 }; 536 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 537 } 538 539 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 540 static ssize_t 541 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 542 { 543 struct rpc_xprt *xprt = &transport->xprt; 544 struct rpc_rqst *req; 545 ssize_t ret; 546 547 /* Look up and lock the request corresponding to the given XID */ 548 req = xprt_lookup_bc_request(xprt, transport->recv.xid); 549 if (!req) { 550 printk(KERN_WARNING "Callback slot table overflowed\n"); 551 return -ESHUTDOWN; 552 } 553 554 ret = xs_read_stream_request(transport, msg, flags, req); 555 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 556 xprt_complete_bc_request(req, transport->recv.copied); 557 558 return ret; 559 } 560 #else /* CONFIG_SUNRPC_BACKCHANNEL */ 561 static ssize_t 562 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 563 { 564 return -ESHUTDOWN; 565 } 566 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 567 568 static ssize_t 569 xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 570 { 571 struct rpc_xprt *xprt = &transport->xprt; 572 struct rpc_rqst *req; 573 ssize_t ret = 0; 574 575 /* Look up and lock the request corresponding to the given XID */ 576 spin_lock(&xprt->queue_lock); 577 req = xprt_lookup_rqst(xprt, transport->recv.xid); 578 if (!req) { 579 msg->msg_flags |= MSG_TRUNC; 580 goto out; 581 } 582 xprt_pin_rqst(req); 583 spin_unlock(&xprt->queue_lock); 584 585 ret = xs_read_stream_request(transport, msg, flags, req); 586 587 spin_lock(&xprt->queue_lock); 588 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 589 xprt_complete_rqst(req->rq_task, transport->recv.copied); 590 xprt_unpin_rqst(req); 591 out: 592 spin_unlock(&xprt->queue_lock); 593 return ret; 594 } 595 596 static ssize_t 597 xs_read_stream(struct sock_xprt *transport, int flags) 598 { 599 struct msghdr msg = { 0 }; 600 size_t want, read = 0; 601 ssize_t ret = 0; 602 603 if (transport->recv.len == 0) { 604 want = xs_read_stream_headersize(transport->recv.copied != 0); 605 ret = xs_read_stream_header(transport, &msg, flags, want, 606 transport->recv.offset); 607 if (ret <= 0) 608 goto out_err; 609 transport->recv.offset = ret; 610 if (transport->recv.offset != want) 611 return transport->recv.offset; 612 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 613 RPC_FRAGMENT_SIZE_MASK; 614 transport->recv.offset -= sizeof(transport->recv.fraghdr); 615 read = ret; 616 } 617 618 switch (be32_to_cpu(transport->recv.calldir)) { 619 default: 620 msg.msg_flags |= MSG_TRUNC; 621 break; 622 case RPC_CALL: 623 ret = xs_read_stream_call(transport, &msg, flags); 624 break; 625 case RPC_REPLY: 626 ret = xs_read_stream_reply(transport, &msg, flags); 627 } 628 if (msg.msg_flags & MSG_TRUNC) { 629 transport->recv.calldir = cpu_to_be32(-1); 630 transport->recv.copied = -1; 631 } 632 if (ret < 0) 633 goto out_err; 634 read += ret; 635 if (transport->recv.offset < transport->recv.len) { 636 if (!(msg.msg_flags & MSG_TRUNC)) 637 return read; 638 msg.msg_flags = 0; 639 ret = xs_read_discard(transport->sock, &msg, flags, 640 transport->recv.len - transport->recv.offset); 641 if (ret <= 0) 642 goto out_err; 643 transport->recv.offset += ret; 644 read += ret; 645 if (transport->recv.offset != transport->recv.len) 646 return read; 647 } 648 if (xs_read_stream_request_done(transport)) { 649 trace_xs_stream_read_request(transport); 650 transport->recv.copied = 0; 651 } 652 transport->recv.offset = 0; 653 transport->recv.len = 0; 654 return read; 655 out_err: 656 return ret != 0 ? ret : -ESHUTDOWN; 657 } 658 659 static __poll_t xs_poll_socket(struct sock_xprt *transport) 660 { 661 return transport->sock->ops->poll(NULL, transport->sock, NULL); 662 } 663 664 static bool xs_poll_socket_readable(struct sock_xprt *transport) 665 { 666 __poll_t events = xs_poll_socket(transport); 667 668 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); 669 } 670 671 static void xs_poll_check_readable(struct sock_xprt *transport) 672 { 673 674 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 675 if (!xs_poll_socket_readable(transport)) 676 return; 677 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 678 queue_work(xprtiod_workqueue, &transport->recv_worker); 679 } 680 681 static void xs_stream_data_receive(struct sock_xprt *transport) 682 { 683 size_t read = 0; 684 ssize_t ret = 0; 685 686 mutex_lock(&transport->recv_mutex); 687 if (transport->sock == NULL) 688 goto out; 689 for (;;) { 690 ret = xs_read_stream(transport, MSG_DONTWAIT); 691 if (ret < 0) 692 break; 693 read += ret; 694 cond_resched(); 695 } 696 xs_poll_check_readable(transport); 697 out: 698 mutex_unlock(&transport->recv_mutex); 699 trace_xs_stream_read_data(&transport->xprt, ret, read); 700 } 701 702 static void xs_stream_data_receive_workfn(struct work_struct *work) 703 { 704 struct sock_xprt *transport = 705 container_of(work, struct sock_xprt, recv_worker); 706 unsigned int pflags = memalloc_nofs_save(); 707 708 xs_stream_data_receive(transport); 709 memalloc_nofs_restore(pflags); 710 } 711 712 static void 713 xs_stream_reset_connect(struct sock_xprt *transport) 714 { 715 transport->recv.offset = 0; 716 transport->recv.len = 0; 717 transport->recv.copied = 0; 718 transport->xmit.offset = 0; 719 transport->xprt.stat.connect_count++; 720 transport->xprt.stat.connect_start = jiffies; 721 } 722 723 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 724 725 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more) 726 { 727 struct msghdr msg = { 728 .msg_name = addr, 729 .msg_namelen = addrlen, 730 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), 731 }; 732 struct kvec iov = { 733 .iov_base = vec->iov_base + base, 734 .iov_len = vec->iov_len - base, 735 }; 736 737 if (iov.iov_len != 0) 738 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 739 return kernel_sendmsg(sock, &msg, NULL, 0, 0); 740 } 741 742 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p) 743 { 744 ssize_t (*do_sendpage)(struct socket *sock, struct page *page, 745 int offset, size_t size, int flags); 746 struct page **ppage; 747 unsigned int remainder; 748 int err; 749 750 remainder = xdr->page_len - base; 751 base += xdr->page_base; 752 ppage = xdr->pages + (base >> PAGE_SHIFT); 753 base &= ~PAGE_MASK; 754 do_sendpage = sock->ops->sendpage; 755 if (!zerocopy) 756 do_sendpage = sock_no_sendpage; 757 for(;;) { 758 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 759 int flags = XS_SENDMSG_FLAGS; 760 761 remainder -= len; 762 if (more) 763 flags |= MSG_MORE; 764 if (remainder != 0) 765 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE; 766 err = do_sendpage(sock, *ppage, base, len, flags); 767 if (remainder == 0 || err != len) 768 break; 769 *sent_p += err; 770 ppage++; 771 base = 0; 772 } 773 if (err > 0) { 774 *sent_p += err; 775 err = 0; 776 } 777 return err; 778 } 779 780 /** 781 * xs_sendpages - write pages directly to a socket 782 * @sock: socket to send on 783 * @addr: UDP only -- address of destination 784 * @addrlen: UDP only -- length of destination address 785 * @xdr: buffer containing this request 786 * @base: starting position in the buffer 787 * @zerocopy: true if it is safe to use sendpage() 788 * @sent_p: return the total number of bytes successfully queued for sending 789 * 790 */ 791 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p) 792 { 793 unsigned int remainder = xdr->len - base; 794 int err = 0; 795 int sent = 0; 796 797 if (unlikely(!sock)) 798 return -ENOTSOCK; 799 800 if (base != 0) { 801 addr = NULL; 802 addrlen = 0; 803 } 804 805 if (base < xdr->head[0].iov_len || addr != NULL) { 806 unsigned int len = xdr->head[0].iov_len - base; 807 remainder -= len; 808 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); 809 if (remainder == 0 || err != len) 810 goto out; 811 *sent_p += err; 812 base = 0; 813 } else 814 base -= xdr->head[0].iov_len; 815 816 if (base < xdr->page_len) { 817 unsigned int len = xdr->page_len - base; 818 remainder -= len; 819 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent); 820 *sent_p += sent; 821 if (remainder == 0 || sent != len) 822 goto out; 823 base = 0; 824 } else 825 base -= xdr->page_len; 826 827 if (base >= xdr->tail[0].iov_len) 828 return 0; 829 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); 830 out: 831 if (err > 0) { 832 *sent_p += err; 833 err = 0; 834 } 835 return err; 836 } 837 838 /** 839 * xs_nospace - handle transmit was incomplete 840 * @req: pointer to RPC request 841 * 842 */ 843 static int xs_nospace(struct rpc_rqst *req) 844 { 845 struct rpc_xprt *xprt = req->rq_xprt; 846 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 847 struct sock *sk = transport->inet; 848 int ret = -EAGAIN; 849 850 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", 851 req->rq_task->tk_pid, 852 req->rq_slen - transport->xmit.offset, 853 req->rq_slen); 854 855 /* Protect against races with write_space */ 856 spin_lock_bh(&xprt->transport_lock); 857 858 /* Don't race with disconnect */ 859 if (xprt_connected(xprt)) { 860 /* wait for more buffer space */ 861 sk->sk_write_pending++; 862 xprt_wait_for_buffer_space(xprt); 863 } else 864 ret = -ENOTCONN; 865 866 spin_unlock_bh(&xprt->transport_lock); 867 868 /* Race breaker in case memory is freed before above code is called */ 869 if (ret == -EAGAIN) { 870 struct socket_wq *wq; 871 872 rcu_read_lock(); 873 wq = rcu_dereference(sk->sk_wq); 874 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); 875 rcu_read_unlock(); 876 877 sk->sk_write_space(sk); 878 } 879 return ret; 880 } 881 882 static void 883 xs_stream_prepare_request(struct rpc_rqst *req) 884 { 885 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO); 886 } 887 888 /* 889 * Determine if the previous message in the stream was aborted before it 890 * could complete transmission. 891 */ 892 static bool 893 xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) 894 { 895 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; 896 } 897 898 /* 899 * Construct a stream transport record marker in @buf. 900 */ 901 static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) 902 { 903 u32 reclen = buf->len - sizeof(rpc_fraghdr); 904 rpc_fraghdr *base = buf->head[0].iov_base; 905 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); 906 } 907 908 /** 909 * xs_local_send_request - write an RPC request to an AF_LOCAL socket 910 * @req: pointer to RPC request 911 * 912 * Return values: 913 * 0: The request has been sent 914 * EAGAIN: The socket was blocked, please call again later to 915 * complete the request 916 * ENOTCONN: Caller needs to invoke connect logic then call again 917 * other: Some other error occured, the request was not sent 918 */ 919 static int xs_local_send_request(struct rpc_rqst *req) 920 { 921 struct rpc_xprt *xprt = req->rq_xprt; 922 struct sock_xprt *transport = 923 container_of(xprt, struct sock_xprt, xprt); 924 struct xdr_buf *xdr = &req->rq_snd_buf; 925 int status; 926 int sent = 0; 927 928 /* Close the stream if the previous transmission was incomplete */ 929 if (xs_send_request_was_aborted(transport, req)) { 930 xs_close(xprt); 931 return -ENOTCONN; 932 } 933 934 xs_encode_stream_record_marker(&req->rq_snd_buf); 935 936 xs_pktdump("packet data:", 937 req->rq_svec->iov_base, req->rq_svec->iov_len); 938 939 req->rq_xtime = ktime_get(); 940 status = xs_sendpages(transport->sock, NULL, 0, xdr, 941 transport->xmit.offset, 942 true, &sent); 943 dprintk("RPC: %s(%u) = %d\n", 944 __func__, xdr->len - transport->xmit.offset, status); 945 946 if (status == -EAGAIN && sock_writeable(transport->inet)) 947 status = -ENOBUFS; 948 949 if (likely(sent > 0) || status == 0) { 950 transport->xmit.offset += sent; 951 req->rq_bytes_sent = transport->xmit.offset; 952 if (likely(req->rq_bytes_sent >= req->rq_slen)) { 953 req->rq_xmit_bytes_sent += transport->xmit.offset; 954 req->rq_bytes_sent = 0; 955 transport->xmit.offset = 0; 956 return 0; 957 } 958 status = -EAGAIN; 959 } 960 961 switch (status) { 962 case -ENOBUFS: 963 break; 964 case -EAGAIN: 965 status = xs_nospace(req); 966 break; 967 default: 968 dprintk("RPC: sendmsg returned unrecognized error %d\n", 969 -status); 970 /* fall through */ 971 case -EPIPE: 972 xs_close(xprt); 973 status = -ENOTCONN; 974 } 975 976 return status; 977 } 978 979 /** 980 * xs_udp_send_request - write an RPC request to a UDP socket 981 * @req: pointer to RPC request 982 * 983 * Return values: 984 * 0: The request has been sent 985 * EAGAIN: The socket was blocked, please call again later to 986 * complete the request 987 * ENOTCONN: Caller needs to invoke connect logic then call again 988 * other: Some other error occurred, the request was not sent 989 */ 990 static int xs_udp_send_request(struct rpc_rqst *req) 991 { 992 struct rpc_xprt *xprt = req->rq_xprt; 993 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 994 struct xdr_buf *xdr = &req->rq_snd_buf; 995 int sent = 0; 996 int status; 997 998 xs_pktdump("packet data:", 999 req->rq_svec->iov_base, 1000 req->rq_svec->iov_len); 1001 1002 if (!xprt_bound(xprt)) 1003 return -ENOTCONN; 1004 1005 if (!xprt_request_get_cong(xprt, req)) 1006 return -EBADSLT; 1007 1008 req->rq_xtime = ktime_get(); 1009 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, 1010 xdr, 0, true, &sent); 1011 1012 dprintk("RPC: xs_udp_send_request(%u) = %d\n", 1013 xdr->len, status); 1014 1015 /* firewall is blocking us, don't return -EAGAIN or we end up looping */ 1016 if (status == -EPERM) 1017 goto process_status; 1018 1019 if (status == -EAGAIN && sock_writeable(transport->inet)) 1020 status = -ENOBUFS; 1021 1022 if (sent > 0 || status == 0) { 1023 req->rq_xmit_bytes_sent += sent; 1024 if (sent >= req->rq_slen) 1025 return 0; 1026 /* Still some bytes left; set up for a retry later. */ 1027 status = -EAGAIN; 1028 } 1029 1030 process_status: 1031 switch (status) { 1032 case -ENOTSOCK: 1033 status = -ENOTCONN; 1034 /* Should we call xs_close() here? */ 1035 break; 1036 case -EAGAIN: 1037 status = xs_nospace(req); 1038 break; 1039 case -ENETUNREACH: 1040 case -ENOBUFS: 1041 case -EPIPE: 1042 case -ECONNREFUSED: 1043 case -EPERM: 1044 /* When the server has died, an ICMP port unreachable message 1045 * prompts ECONNREFUSED. */ 1046 break; 1047 default: 1048 dprintk("RPC: sendmsg returned unrecognized error %d\n", 1049 -status); 1050 } 1051 1052 return status; 1053 } 1054 1055 /** 1056 * xs_tcp_send_request - write an RPC request to a TCP socket 1057 * @req: pointer to RPC request 1058 * 1059 * Return values: 1060 * 0: The request has been sent 1061 * EAGAIN: The socket was blocked, please call again later to 1062 * complete the request 1063 * ENOTCONN: Caller needs to invoke connect logic then call again 1064 * other: Some other error occurred, the request was not sent 1065 * 1066 * XXX: In the case of soft timeouts, should we eventually give up 1067 * if sendmsg is not able to make progress? 1068 */ 1069 static int xs_tcp_send_request(struct rpc_rqst *req) 1070 { 1071 struct rpc_xprt *xprt = req->rq_xprt; 1072 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1073 struct xdr_buf *xdr = &req->rq_snd_buf; 1074 bool zerocopy = true; 1075 bool vm_wait = false; 1076 int status; 1077 int sent; 1078 1079 /* Close the stream if the previous transmission was incomplete */ 1080 if (xs_send_request_was_aborted(transport, req)) { 1081 if (transport->sock != NULL) 1082 kernel_sock_shutdown(transport->sock, SHUT_RDWR); 1083 return -ENOTCONN; 1084 } 1085 1086 xs_encode_stream_record_marker(&req->rq_snd_buf); 1087 1088 xs_pktdump("packet data:", 1089 req->rq_svec->iov_base, 1090 req->rq_svec->iov_len); 1091 /* Don't use zero copy if this is a resend. If the RPC call 1092 * completes while the socket holds a reference to the pages, 1093 * then we may end up resending corrupted data. 1094 */ 1095 if (req->rq_task->tk_flags & RPC_TASK_SENT) 1096 zerocopy = false; 1097 1098 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) 1099 xs_tcp_set_socket_timeouts(xprt, transport->sock); 1100 1101 /* Continue transmitting the packet/record. We must be careful 1102 * to cope with writespace callbacks arriving _after_ we have 1103 * called sendmsg(). */ 1104 req->rq_xtime = ktime_get(); 1105 while (1) { 1106 sent = 0; 1107 status = xs_sendpages(transport->sock, NULL, 0, xdr, 1108 transport->xmit.offset, 1109 zerocopy, &sent); 1110 1111 dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 1112 xdr->len - transport->xmit.offset, status); 1113 1114 /* If we've sent the entire packet, immediately 1115 * reset the count of bytes sent. */ 1116 transport->xmit.offset += sent; 1117 req->rq_bytes_sent = transport->xmit.offset; 1118 if (likely(req->rq_bytes_sent >= req->rq_slen)) { 1119 req->rq_xmit_bytes_sent += transport->xmit.offset; 1120 req->rq_bytes_sent = 0; 1121 transport->xmit.offset = 0; 1122 return 0; 1123 } 1124 1125 WARN_ON_ONCE(sent == 0 && status == 0); 1126 1127 if (status == -EAGAIN ) { 1128 /* 1129 * Return EAGAIN if we're sure we're hitting the 1130 * socket send buffer limits. 1131 */ 1132 if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) 1133 break; 1134 /* 1135 * Did we hit a memory allocation failure? 1136 */ 1137 if (sent == 0) { 1138 status = -ENOBUFS; 1139 if (vm_wait) 1140 break; 1141 /* Retry, knowing now that we're below the 1142 * socket send buffer limit 1143 */ 1144 vm_wait = true; 1145 } 1146 continue; 1147 } 1148 if (status < 0) 1149 break; 1150 vm_wait = false; 1151 } 1152 1153 switch (status) { 1154 case -ENOTSOCK: 1155 status = -ENOTCONN; 1156 /* Should we call xs_close() here? */ 1157 break; 1158 case -EAGAIN: 1159 status = xs_nospace(req); 1160 break; 1161 case -ECONNRESET: 1162 case -ECONNREFUSED: 1163 case -ENOTCONN: 1164 case -EADDRINUSE: 1165 case -ENOBUFS: 1166 case -EPIPE: 1167 break; 1168 default: 1169 dprintk("RPC: sendmsg returned unrecognized error %d\n", 1170 -status); 1171 } 1172 1173 return status; 1174 } 1175 1176 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) 1177 { 1178 transport->old_data_ready = sk->sk_data_ready; 1179 transport->old_state_change = sk->sk_state_change; 1180 transport->old_write_space = sk->sk_write_space; 1181 transport->old_error_report = sk->sk_error_report; 1182 } 1183 1184 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) 1185 { 1186 sk->sk_data_ready = transport->old_data_ready; 1187 sk->sk_state_change = transport->old_state_change; 1188 sk->sk_write_space = transport->old_write_space; 1189 sk->sk_error_report = transport->old_error_report; 1190 } 1191 1192 static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) 1193 { 1194 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1195 1196 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); 1197 } 1198 1199 static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) 1200 { 1201 smp_mb__before_atomic(); 1202 clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1203 clear_bit(XPRT_CLOSING, &xprt->state); 1204 xs_sock_reset_state_flags(xprt); 1205 smp_mb__after_atomic(); 1206 } 1207 1208 /** 1209 * xs_error_report - callback to handle TCP socket state errors 1210 * @sk: socket 1211 * 1212 * Note: we don't call sock_error() since there may be a rpc_task 1213 * using the socket, and so we don't want to clear sk->sk_err. 1214 */ 1215 static void xs_error_report(struct sock *sk) 1216 { 1217 struct rpc_xprt *xprt; 1218 int err; 1219 1220 read_lock_bh(&sk->sk_callback_lock); 1221 if (!(xprt = xprt_from_sock(sk))) 1222 goto out; 1223 1224 err = -sk->sk_err; 1225 if (err == 0) 1226 goto out; 1227 dprintk("RPC: xs_error_report client %p, error=%d...\n", 1228 xprt, -err); 1229 trace_rpc_socket_error(xprt, sk->sk_socket, err); 1230 xprt_wake_pending_tasks(xprt, err); 1231 out: 1232 read_unlock_bh(&sk->sk_callback_lock); 1233 } 1234 1235 static void xs_reset_transport(struct sock_xprt *transport) 1236 { 1237 struct socket *sock = transport->sock; 1238 struct sock *sk = transport->inet; 1239 struct rpc_xprt *xprt = &transport->xprt; 1240 1241 if (sk == NULL) 1242 return; 1243 1244 if (atomic_read(&transport->xprt.swapper)) 1245 sk_clear_memalloc(sk); 1246 1247 kernel_sock_shutdown(sock, SHUT_RDWR); 1248 1249 mutex_lock(&transport->recv_mutex); 1250 write_lock_bh(&sk->sk_callback_lock); 1251 transport->inet = NULL; 1252 transport->sock = NULL; 1253 1254 sk->sk_user_data = NULL; 1255 1256 xs_restore_old_callbacks(transport, sk); 1257 xprt_clear_connected(xprt); 1258 write_unlock_bh(&sk->sk_callback_lock); 1259 xs_sock_reset_connection_flags(xprt); 1260 mutex_unlock(&transport->recv_mutex); 1261 1262 trace_rpc_socket_close(xprt, sock); 1263 sock_release(sock); 1264 1265 xprt_disconnect_done(xprt); 1266 } 1267 1268 /** 1269 * xs_close - close a socket 1270 * @xprt: transport 1271 * 1272 * This is used when all requests are complete; ie, no DRC state remains 1273 * on the server we want to save. 1274 * 1275 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with 1276 * xs_reset_transport() zeroing the socket from underneath a writer. 1277 */ 1278 static void xs_close(struct rpc_xprt *xprt) 1279 { 1280 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1281 1282 dprintk("RPC: xs_close xprt %p\n", xprt); 1283 1284 xs_reset_transport(transport); 1285 xprt->reestablish_timeout = 0; 1286 } 1287 1288 static void xs_inject_disconnect(struct rpc_xprt *xprt) 1289 { 1290 dprintk("RPC: injecting transport disconnect on xprt=%p\n", 1291 xprt); 1292 xprt_disconnect_done(xprt); 1293 } 1294 1295 static void xs_xprt_free(struct rpc_xprt *xprt) 1296 { 1297 xs_free_peer_addresses(xprt); 1298 xprt_free(xprt); 1299 } 1300 1301 /** 1302 * xs_destroy - prepare to shutdown a transport 1303 * @xprt: doomed transport 1304 * 1305 */ 1306 static void xs_destroy(struct rpc_xprt *xprt) 1307 { 1308 struct sock_xprt *transport = container_of(xprt, 1309 struct sock_xprt, xprt); 1310 dprintk("RPC: xs_destroy xprt %p\n", xprt); 1311 1312 cancel_delayed_work_sync(&transport->connect_worker); 1313 xs_close(xprt); 1314 cancel_work_sync(&transport->recv_worker); 1315 xs_xprt_free(xprt); 1316 module_put(THIS_MODULE); 1317 } 1318 1319 /** 1320 * xs_udp_data_read_skb - receive callback for UDP sockets 1321 * @xprt: transport 1322 * @sk: socket 1323 * @skb: skbuff 1324 * 1325 */ 1326 static void xs_udp_data_read_skb(struct rpc_xprt *xprt, 1327 struct sock *sk, 1328 struct sk_buff *skb) 1329 { 1330 struct rpc_task *task; 1331 struct rpc_rqst *rovr; 1332 int repsize, copied; 1333 u32 _xid; 1334 __be32 *xp; 1335 1336 repsize = skb->len; 1337 if (repsize < 4) { 1338 dprintk("RPC: impossible RPC reply size %d!\n", repsize); 1339 return; 1340 } 1341 1342 /* Copy the XID from the skb... */ 1343 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); 1344 if (xp == NULL) 1345 return; 1346 1347 /* Look up and lock the request corresponding to the given XID */ 1348 spin_lock(&xprt->queue_lock); 1349 rovr = xprt_lookup_rqst(xprt, *xp); 1350 if (!rovr) 1351 goto out_unlock; 1352 xprt_pin_rqst(rovr); 1353 xprt_update_rtt(rovr->rq_task); 1354 spin_unlock(&xprt->queue_lock); 1355 task = rovr->rq_task; 1356 1357 if ((copied = rovr->rq_private_buf.buflen) > repsize) 1358 copied = repsize; 1359 1360 /* Suck it into the iovec, verify checksum if not done by hw. */ 1361 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { 1362 spin_lock(&xprt->queue_lock); 1363 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); 1364 goto out_unpin; 1365 } 1366 1367 1368 spin_lock_bh(&xprt->transport_lock); 1369 xprt_adjust_cwnd(xprt, task, copied); 1370 spin_unlock_bh(&xprt->transport_lock); 1371 spin_lock(&xprt->queue_lock); 1372 xprt_complete_rqst(task, copied); 1373 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); 1374 out_unpin: 1375 xprt_unpin_rqst(rovr); 1376 out_unlock: 1377 spin_unlock(&xprt->queue_lock); 1378 } 1379 1380 static void xs_udp_data_receive(struct sock_xprt *transport) 1381 { 1382 struct sk_buff *skb; 1383 struct sock *sk; 1384 int err; 1385 1386 mutex_lock(&transport->recv_mutex); 1387 sk = transport->inet; 1388 if (sk == NULL) 1389 goto out; 1390 for (;;) { 1391 skb = skb_recv_udp(sk, 0, 1, &err); 1392 if (skb == NULL) 1393 break; 1394 xs_udp_data_read_skb(&transport->xprt, sk, skb); 1395 consume_skb(skb); 1396 cond_resched(); 1397 } 1398 xs_poll_check_readable(transport); 1399 out: 1400 mutex_unlock(&transport->recv_mutex); 1401 } 1402 1403 static void xs_udp_data_receive_workfn(struct work_struct *work) 1404 { 1405 struct sock_xprt *transport = 1406 container_of(work, struct sock_xprt, recv_worker); 1407 unsigned int pflags = memalloc_nofs_save(); 1408 1409 xs_udp_data_receive(transport); 1410 memalloc_nofs_restore(pflags); 1411 } 1412 1413 /** 1414 * xs_data_ready - "data ready" callback for UDP sockets 1415 * @sk: socket with data to read 1416 * 1417 */ 1418 static void xs_data_ready(struct sock *sk) 1419 { 1420 struct rpc_xprt *xprt; 1421 1422 read_lock_bh(&sk->sk_callback_lock); 1423 dprintk("RPC: xs_data_ready...\n"); 1424 xprt = xprt_from_sock(sk); 1425 if (xprt != NULL) { 1426 struct sock_xprt *transport = container_of(xprt, 1427 struct sock_xprt, xprt); 1428 transport->old_data_ready(sk); 1429 /* Any data means we had a useful conversation, so 1430 * then we don't need to delay the next reconnect 1431 */ 1432 if (xprt->reestablish_timeout) 1433 xprt->reestablish_timeout = 0; 1434 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) 1435 queue_work(xprtiod_workqueue, &transport->recv_worker); 1436 } 1437 read_unlock_bh(&sk->sk_callback_lock); 1438 } 1439 1440 /* 1441 * Helper function to force a TCP close if the server is sending 1442 * junk and/or it has put us in CLOSE_WAIT 1443 */ 1444 static void xs_tcp_force_close(struct rpc_xprt *xprt) 1445 { 1446 xprt_force_disconnect(xprt); 1447 } 1448 1449 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1450 static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) 1451 { 1452 return PAGE_SIZE; 1453 } 1454 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 1455 1456 /** 1457 * xs_tcp_state_change - callback to handle TCP socket state changes 1458 * @sk: socket whose state has changed 1459 * 1460 */ 1461 static void xs_tcp_state_change(struct sock *sk) 1462 { 1463 struct rpc_xprt *xprt; 1464 struct sock_xprt *transport; 1465 1466 read_lock_bh(&sk->sk_callback_lock); 1467 if (!(xprt = xprt_from_sock(sk))) 1468 goto out; 1469 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 1470 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", 1471 sk->sk_state, xprt_connected(xprt), 1472 sock_flag(sk, SOCK_DEAD), 1473 sock_flag(sk, SOCK_ZAPPED), 1474 sk->sk_shutdown); 1475 1476 transport = container_of(xprt, struct sock_xprt, xprt); 1477 trace_rpc_socket_state_change(xprt, sk->sk_socket); 1478 switch (sk->sk_state) { 1479 case TCP_ESTABLISHED: 1480 spin_lock(&xprt->transport_lock); 1481 if (!xprt_test_and_set_connected(xprt)) { 1482 xprt->connect_cookie++; 1483 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 1484 xprt_clear_connecting(xprt); 1485 1486 xprt->stat.connect_count++; 1487 xprt->stat.connect_time += (long)jiffies - 1488 xprt->stat.connect_start; 1489 xprt_wake_pending_tasks(xprt, -EAGAIN); 1490 } 1491 spin_unlock(&xprt->transport_lock); 1492 break; 1493 case TCP_FIN_WAIT1: 1494 /* The client initiated a shutdown of the socket */ 1495 xprt->connect_cookie++; 1496 xprt->reestablish_timeout = 0; 1497 set_bit(XPRT_CLOSING, &xprt->state); 1498 smp_mb__before_atomic(); 1499 clear_bit(XPRT_CONNECTED, &xprt->state); 1500 clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1501 smp_mb__after_atomic(); 1502 break; 1503 case TCP_CLOSE_WAIT: 1504 /* The server initiated a shutdown of the socket */ 1505 xprt->connect_cookie++; 1506 clear_bit(XPRT_CONNECTED, &xprt->state); 1507 xs_tcp_force_close(xprt); 1508 /* fall through */ 1509 case TCP_CLOSING: 1510 /* 1511 * If the server closed down the connection, make sure that 1512 * we back off before reconnecting 1513 */ 1514 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1515 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 1516 break; 1517 case TCP_LAST_ACK: 1518 set_bit(XPRT_CLOSING, &xprt->state); 1519 smp_mb__before_atomic(); 1520 clear_bit(XPRT_CONNECTED, &xprt->state); 1521 smp_mb__after_atomic(); 1522 break; 1523 case TCP_CLOSE: 1524 if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 1525 &transport->sock_state)) 1526 xprt_clear_connecting(xprt); 1527 clear_bit(XPRT_CLOSING, &xprt->state); 1528 /* Trigger the socket release */ 1529 xs_tcp_force_close(xprt); 1530 } 1531 out: 1532 read_unlock_bh(&sk->sk_callback_lock); 1533 } 1534 1535 static void xs_write_space(struct sock *sk) 1536 { 1537 struct socket_wq *wq; 1538 struct rpc_xprt *xprt; 1539 1540 if (!sk->sk_socket) 1541 return; 1542 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1543 1544 if (unlikely(!(xprt = xprt_from_sock(sk)))) 1545 return; 1546 rcu_read_lock(); 1547 wq = rcu_dereference(sk->sk_wq); 1548 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 1549 goto out; 1550 1551 if (xprt_write_space(xprt)) 1552 sk->sk_write_pending--; 1553 out: 1554 rcu_read_unlock(); 1555 } 1556 1557 /** 1558 * xs_udp_write_space - callback invoked when socket buffer space 1559 * becomes available 1560 * @sk: socket whose state has changed 1561 * 1562 * Called when more output buffer space is available for this socket. 1563 * We try not to wake our writers until they can make "significant" 1564 * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1565 * with a bunch of small requests. 1566 */ 1567 static void xs_udp_write_space(struct sock *sk) 1568 { 1569 read_lock_bh(&sk->sk_callback_lock); 1570 1571 /* from net/core/sock.c:sock_def_write_space */ 1572 if (sock_writeable(sk)) 1573 xs_write_space(sk); 1574 1575 read_unlock_bh(&sk->sk_callback_lock); 1576 } 1577 1578 /** 1579 * xs_tcp_write_space - callback invoked when socket buffer space 1580 * becomes available 1581 * @sk: socket whose state has changed 1582 * 1583 * Called when more output buffer space is available for this socket. 1584 * We try not to wake our writers until they can make "significant" 1585 * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1586 * with a bunch of small requests. 1587 */ 1588 static void xs_tcp_write_space(struct sock *sk) 1589 { 1590 read_lock_bh(&sk->sk_callback_lock); 1591 1592 /* from net/core/stream.c:sk_stream_write_space */ 1593 if (sk_stream_is_writeable(sk)) 1594 xs_write_space(sk); 1595 1596 read_unlock_bh(&sk->sk_callback_lock); 1597 } 1598 1599 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1600 { 1601 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1602 struct sock *sk = transport->inet; 1603 1604 if (transport->rcvsize) { 1605 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 1606 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1607 } 1608 if (transport->sndsize) { 1609 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 1610 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1611 sk->sk_write_space(sk); 1612 } 1613 } 1614 1615 /** 1616 * xs_udp_set_buffer_size - set send and receive limits 1617 * @xprt: generic transport 1618 * @sndsize: requested size of send buffer, in bytes 1619 * @rcvsize: requested size of receive buffer, in bytes 1620 * 1621 * Set socket send and receive buffer size limits. 1622 */ 1623 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 1624 { 1625 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1626 1627 transport->sndsize = 0; 1628 if (sndsize) 1629 transport->sndsize = sndsize + 1024; 1630 transport->rcvsize = 0; 1631 if (rcvsize) 1632 transport->rcvsize = rcvsize + 1024; 1633 1634 xs_udp_do_set_buffer_size(xprt); 1635 } 1636 1637 /** 1638 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1639 * @xprt: controlling transport 1640 * @task: task that timed out 1641 * 1642 * Adjust the congestion window after a retransmit timeout has occurred. 1643 */ 1644 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 1645 { 1646 spin_lock_bh(&xprt->transport_lock); 1647 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1648 spin_unlock_bh(&xprt->transport_lock); 1649 } 1650 1651 static int xs_get_random_port(void) 1652 { 1653 unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1654 unsigned short range; 1655 unsigned short rand; 1656 1657 if (max < min) 1658 return -EADDRINUSE; 1659 range = max - min + 1; 1660 rand = (unsigned short) prandom_u32() % range; 1661 return rand + min; 1662 } 1663 1664 /** 1665 * xs_set_reuseaddr_port - set the socket's port and address reuse options 1666 * @sock: socket 1667 * 1668 * Note that this function has to be called on all sockets that share the 1669 * same port, and it must be called before binding. 1670 */ 1671 static void xs_sock_set_reuseport(struct socket *sock) 1672 { 1673 int opt = 1; 1674 1675 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1676 (char *)&opt, sizeof(opt)); 1677 } 1678 1679 static unsigned short xs_sock_getport(struct socket *sock) 1680 { 1681 struct sockaddr_storage buf; 1682 unsigned short port = 0; 1683 1684 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 1685 goto out; 1686 switch (buf.ss_family) { 1687 case AF_INET6: 1688 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 1689 break; 1690 case AF_INET: 1691 port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 1692 } 1693 out: 1694 return port; 1695 } 1696 1697 /** 1698 * xs_set_port - reset the port number in the remote endpoint address 1699 * @xprt: generic transport 1700 * @port: new port number 1701 * 1702 */ 1703 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 1704 { 1705 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1706 1707 rpc_set_port(xs_addr(xprt), port); 1708 xs_update_peer_port(xprt); 1709 } 1710 1711 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 1712 { 1713 if (transport->srcport == 0) 1714 transport->srcport = xs_sock_getport(sock); 1715 } 1716 1717 static int xs_get_srcport(struct sock_xprt *transport) 1718 { 1719 int port = transport->srcport; 1720 1721 if (port == 0 && transport->xprt.resvport) 1722 port = xs_get_random_port(); 1723 return port; 1724 } 1725 1726 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 1727 { 1728 if (transport->srcport != 0) 1729 transport->srcport = 0; 1730 if (!transport->xprt.resvport) 1731 return 0; 1732 if (port <= xprt_min_resvport || port > xprt_max_resvport) 1733 return xprt_max_resvport; 1734 return --port; 1735 } 1736 static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1737 { 1738 struct sockaddr_storage myaddr; 1739 int err, nloop = 0; 1740 int port = xs_get_srcport(transport); 1741 unsigned short last; 1742 1743 /* 1744 * If we are asking for any ephemeral port (i.e. port == 0 && 1745 * transport->xprt.resvport == 0), don't bind. Let the local 1746 * port selection happen implicitly when the socket is used 1747 * (for example at connect time). 1748 * 1749 * This ensures that we can continue to establish TCP 1750 * connections even when all local ephemeral ports are already 1751 * a part of some TCP connection. This makes no difference 1752 * for UDP sockets, but also doens't harm them. 1753 * 1754 * If we're asking for any reserved port (i.e. port == 0 && 1755 * transport->xprt.resvport == 1) xs_get_srcport above will 1756 * ensure that port is non-zero and we will bind as needed. 1757 */ 1758 if (port <= 0) 1759 return port; 1760 1761 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1762 do { 1763 rpc_set_port((struct sockaddr *)&myaddr, port); 1764 err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1765 transport->xprt.addrlen); 1766 if (err == 0) { 1767 transport->srcport = port; 1768 break; 1769 } 1770 last = port; 1771 port = xs_next_srcport(transport, port); 1772 if (port > last) 1773 nloop++; 1774 } while (err == -EADDRINUSE && nloop != 2); 1775 1776 if (myaddr.ss_family == AF_INET) 1777 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1778 &((struct sockaddr_in *)&myaddr)->sin_addr, 1779 port, err ? "failed" : "ok", err); 1780 else 1781 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1782 &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 1783 port, err ? "failed" : "ok", err); 1784 return err; 1785 } 1786 1787 /* 1788 * We don't support autobind on AF_LOCAL sockets 1789 */ 1790 static void xs_local_rpcbind(struct rpc_task *task) 1791 { 1792 xprt_set_bound(task->tk_xprt); 1793 } 1794 1795 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1796 { 1797 } 1798 1799 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1800 static struct lock_class_key xs_key[2]; 1801 static struct lock_class_key xs_slock_key[2]; 1802 1803 static inline void xs_reclassify_socketu(struct socket *sock) 1804 { 1805 struct sock *sk = sock->sk; 1806 1807 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1808 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1809 } 1810 1811 static inline void xs_reclassify_socket4(struct socket *sock) 1812 { 1813 struct sock *sk = sock->sk; 1814 1815 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 1816 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1817 } 1818 1819 static inline void xs_reclassify_socket6(struct socket *sock) 1820 { 1821 struct sock *sk = sock->sk; 1822 1823 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 1824 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1825 } 1826 1827 static inline void xs_reclassify_socket(int family, struct socket *sock) 1828 { 1829 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 1830 return; 1831 1832 switch (family) { 1833 case AF_LOCAL: 1834 xs_reclassify_socketu(sock); 1835 break; 1836 case AF_INET: 1837 xs_reclassify_socket4(sock); 1838 break; 1839 case AF_INET6: 1840 xs_reclassify_socket6(sock); 1841 break; 1842 } 1843 } 1844 #else 1845 static inline void xs_reclassify_socket(int family, struct socket *sock) 1846 { 1847 } 1848 #endif 1849 1850 static void xs_dummy_setup_socket(struct work_struct *work) 1851 { 1852 } 1853 1854 static struct socket *xs_create_sock(struct rpc_xprt *xprt, 1855 struct sock_xprt *transport, int family, int type, 1856 int protocol, bool reuseport) 1857 { 1858 struct socket *sock; 1859 int err; 1860 1861 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 1862 if (err < 0) { 1863 dprintk("RPC: can't create %d transport socket (%d).\n", 1864 protocol, -err); 1865 goto out; 1866 } 1867 xs_reclassify_socket(family, sock); 1868 1869 if (reuseport) 1870 xs_sock_set_reuseport(sock); 1871 1872 err = xs_bind(transport, sock); 1873 if (err) { 1874 sock_release(sock); 1875 goto out; 1876 } 1877 1878 return sock; 1879 out: 1880 return ERR_PTR(err); 1881 } 1882 1883 static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1884 struct socket *sock) 1885 { 1886 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1887 xprt); 1888 1889 if (!transport->inet) { 1890 struct sock *sk = sock->sk; 1891 1892 write_lock_bh(&sk->sk_callback_lock); 1893 1894 xs_save_old_callbacks(transport, sk); 1895 1896 sk->sk_user_data = xprt; 1897 sk->sk_data_ready = xs_data_ready; 1898 sk->sk_write_space = xs_udp_write_space; 1899 sock_set_flag(sk, SOCK_FASYNC); 1900 sk->sk_error_report = xs_error_report; 1901 1902 xprt_clear_connected(xprt); 1903 1904 /* Reset to new socket */ 1905 transport->sock = sock; 1906 transport->inet = sk; 1907 1908 write_unlock_bh(&sk->sk_callback_lock); 1909 } 1910 1911 xs_stream_reset_connect(transport); 1912 1913 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1914 } 1915 1916 /** 1917 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1918 * @transport: socket transport to connect 1919 */ 1920 static int xs_local_setup_socket(struct sock_xprt *transport) 1921 { 1922 struct rpc_xprt *xprt = &transport->xprt; 1923 struct socket *sock; 1924 int status = -EIO; 1925 1926 status = __sock_create(xprt->xprt_net, AF_LOCAL, 1927 SOCK_STREAM, 0, &sock, 1); 1928 if (status < 0) { 1929 dprintk("RPC: can't create AF_LOCAL " 1930 "transport socket (%d).\n", -status); 1931 goto out; 1932 } 1933 xs_reclassify_socket(AF_LOCAL, sock); 1934 1935 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1936 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1937 1938 status = xs_local_finish_connecting(xprt, sock); 1939 trace_rpc_socket_connect(xprt, sock, status); 1940 switch (status) { 1941 case 0: 1942 dprintk("RPC: xprt %p connected to %s\n", 1943 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1944 xprt->stat.connect_count++; 1945 xprt->stat.connect_time += (long)jiffies - 1946 xprt->stat.connect_start; 1947 xprt_set_connected(xprt); 1948 case -ENOBUFS: 1949 break; 1950 case -ENOENT: 1951 dprintk("RPC: xprt %p: socket %s does not exist\n", 1952 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1953 break; 1954 case -ECONNREFUSED: 1955 dprintk("RPC: xprt %p: connection refused for %s\n", 1956 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1957 break; 1958 default: 1959 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 1960 __func__, -status, 1961 xprt->address_strings[RPC_DISPLAY_ADDR]); 1962 } 1963 1964 out: 1965 xprt_clear_connecting(xprt); 1966 xprt_wake_pending_tasks(xprt, status); 1967 return status; 1968 } 1969 1970 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 1971 { 1972 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1973 int ret; 1974 1975 if (RPC_IS_ASYNC(task)) { 1976 /* 1977 * We want the AF_LOCAL connect to be resolved in the 1978 * filesystem namespace of the process making the rpc 1979 * call. Thus we connect synchronously. 1980 * 1981 * If we want to support asynchronous AF_LOCAL calls, 1982 * we'll need to figure out how to pass a namespace to 1983 * connect. 1984 */ 1985 rpc_exit(task, -ENOTCONN); 1986 return; 1987 } 1988 ret = xs_local_setup_socket(transport); 1989 if (ret && !RPC_IS_SOFTCONN(task)) 1990 msleep_interruptible(15000); 1991 } 1992 1993 #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 1994 /* 1995 * Note that this should be called with XPRT_LOCKED held (or when we otherwise 1996 * know that we have exclusive access to the socket), to guard against 1997 * races with xs_reset_transport. 1998 */ 1999 static void xs_set_memalloc(struct rpc_xprt *xprt) 2000 { 2001 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 2002 xprt); 2003 2004 /* 2005 * If there's no sock, then we have nothing to set. The 2006 * reconnecting process will get it for us. 2007 */ 2008 if (!transport->inet) 2009 return; 2010 if (atomic_read(&xprt->swapper)) 2011 sk_set_memalloc(transport->inet); 2012 } 2013 2014 /** 2015 * xs_enable_swap - Tag this transport as being used for swap. 2016 * @xprt: transport to tag 2017 * 2018 * Take a reference to this transport on behalf of the rpc_clnt, and 2019 * optionally mark it for swapping if it wasn't already. 2020 */ 2021 static int 2022 xs_enable_swap(struct rpc_xprt *xprt) 2023 { 2024 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2025 2026 if (atomic_inc_return(&xprt->swapper) != 1) 2027 return 0; 2028 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2029 return -ERESTARTSYS; 2030 if (xs->inet) 2031 sk_set_memalloc(xs->inet); 2032 xprt_release_xprt(xprt, NULL); 2033 return 0; 2034 } 2035 2036 /** 2037 * xs_disable_swap - Untag this transport as being used for swap. 2038 * @xprt: transport to tag 2039 * 2040 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 2041 * swapper refcount goes to 0, untag the socket as a memalloc socket. 2042 */ 2043 static void 2044 xs_disable_swap(struct rpc_xprt *xprt) 2045 { 2046 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2047 2048 if (!atomic_dec_and_test(&xprt->swapper)) 2049 return; 2050 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2051 return; 2052 if (xs->inet) 2053 sk_clear_memalloc(xs->inet); 2054 xprt_release_xprt(xprt, NULL); 2055 } 2056 #else 2057 static void xs_set_memalloc(struct rpc_xprt *xprt) 2058 { 2059 } 2060 2061 static int 2062 xs_enable_swap(struct rpc_xprt *xprt) 2063 { 2064 return -EINVAL; 2065 } 2066 2067 static void 2068 xs_disable_swap(struct rpc_xprt *xprt) 2069 { 2070 } 2071 #endif 2072 2073 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2074 { 2075 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2076 2077 if (!transport->inet) { 2078 struct sock *sk = sock->sk; 2079 2080 write_lock_bh(&sk->sk_callback_lock); 2081 2082 xs_save_old_callbacks(transport, sk); 2083 2084 sk->sk_user_data = xprt; 2085 sk->sk_data_ready = xs_data_ready; 2086 sk->sk_write_space = xs_udp_write_space; 2087 sock_set_flag(sk, SOCK_FASYNC); 2088 2089 xprt_set_connected(xprt); 2090 2091 /* Reset to new socket */ 2092 transport->sock = sock; 2093 transport->inet = sk; 2094 2095 xs_set_memalloc(xprt); 2096 2097 write_unlock_bh(&sk->sk_callback_lock); 2098 } 2099 xs_udp_do_set_buffer_size(xprt); 2100 2101 xprt->stat.connect_start = jiffies; 2102 } 2103 2104 static void xs_udp_setup_socket(struct work_struct *work) 2105 { 2106 struct sock_xprt *transport = 2107 container_of(work, struct sock_xprt, connect_worker.work); 2108 struct rpc_xprt *xprt = &transport->xprt; 2109 struct socket *sock; 2110 int status = -EIO; 2111 2112 sock = xs_create_sock(xprt, transport, 2113 xs_addr(xprt)->sa_family, SOCK_DGRAM, 2114 IPPROTO_UDP, false); 2115 if (IS_ERR(sock)) 2116 goto out; 2117 2118 dprintk("RPC: worker connecting xprt %p via %s to " 2119 "%s (port %s)\n", xprt, 2120 xprt->address_strings[RPC_DISPLAY_PROTO], 2121 xprt->address_strings[RPC_DISPLAY_ADDR], 2122 xprt->address_strings[RPC_DISPLAY_PORT]); 2123 2124 xs_udp_finish_connecting(xprt, sock); 2125 trace_rpc_socket_connect(xprt, sock, 0); 2126 status = 0; 2127 out: 2128 xprt_clear_connecting(xprt); 2129 xprt_unlock_connect(xprt, transport); 2130 xprt_wake_pending_tasks(xprt, status); 2131 } 2132 2133 /** 2134 * xs_tcp_shutdown - gracefully shut down a TCP socket 2135 * @xprt: transport 2136 * 2137 * Initiates a graceful shutdown of the TCP socket by calling the 2138 * equivalent of shutdown(SHUT_RDWR); 2139 */ 2140 static void xs_tcp_shutdown(struct rpc_xprt *xprt) 2141 { 2142 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2143 struct socket *sock = transport->sock; 2144 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 2145 2146 if (sock == NULL) 2147 return; 2148 switch (skst) { 2149 default: 2150 kernel_sock_shutdown(sock, SHUT_RDWR); 2151 trace_rpc_socket_shutdown(xprt, sock); 2152 break; 2153 case TCP_CLOSE: 2154 case TCP_TIME_WAIT: 2155 xs_reset_transport(transport); 2156 } 2157 } 2158 2159 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 2160 struct socket *sock) 2161 { 2162 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2163 unsigned int keepidle; 2164 unsigned int keepcnt; 2165 unsigned int opt_on = 1; 2166 unsigned int timeo; 2167 2168 spin_lock_bh(&xprt->transport_lock); 2169 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 2170 keepcnt = xprt->timeout->to_retries + 1; 2171 timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 2172 (xprt->timeout->to_retries + 1); 2173 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2174 spin_unlock_bh(&xprt->transport_lock); 2175 2176 /* TCP Keepalive options */ 2177 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 2178 (char *)&opt_on, sizeof(opt_on)); 2179 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 2180 (char *)&keepidle, sizeof(keepidle)); 2181 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 2182 (char *)&keepidle, sizeof(keepidle)); 2183 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 2184 (char *)&keepcnt, sizeof(keepcnt)); 2185 2186 /* TCP user timeout (see RFC5482) */ 2187 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 2188 (char *)&timeo, sizeof(timeo)); 2189 } 2190 2191 static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 2192 unsigned long connect_timeout, 2193 unsigned long reconnect_timeout) 2194 { 2195 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2196 struct rpc_timeout to; 2197 unsigned long initval; 2198 2199 spin_lock_bh(&xprt->transport_lock); 2200 if (reconnect_timeout < xprt->max_reconnect_timeout) 2201 xprt->max_reconnect_timeout = reconnect_timeout; 2202 if (connect_timeout < xprt->connect_timeout) { 2203 memcpy(&to, xprt->timeout, sizeof(to)); 2204 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 2205 /* Arbitrary lower limit */ 2206 if (initval < XS_TCP_INIT_REEST_TO << 1) 2207 initval = XS_TCP_INIT_REEST_TO << 1; 2208 to.to_initval = initval; 2209 to.to_maxval = initval; 2210 memcpy(&transport->tcp_timeout, &to, 2211 sizeof(transport->tcp_timeout)); 2212 xprt->timeout = &transport->tcp_timeout; 2213 xprt->connect_timeout = connect_timeout; 2214 } 2215 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2216 spin_unlock_bh(&xprt->transport_lock); 2217 } 2218 2219 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2220 { 2221 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2222 int ret = -ENOTCONN; 2223 2224 if (!transport->inet) { 2225 struct sock *sk = sock->sk; 2226 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 2227 2228 /* Avoid temporary address, they are bad for long-lived 2229 * connections such as NFS mounts. 2230 * RFC4941, section 3.6 suggests that: 2231 * Individual applications, which have specific 2232 * knowledge about the normal duration of connections, 2233 * MAY override this as appropriate. 2234 */ 2235 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2236 (char *)&addr_pref, sizeof(addr_pref)); 2237 2238 xs_tcp_set_socket_timeouts(xprt, sock); 2239 2240 write_lock_bh(&sk->sk_callback_lock); 2241 2242 xs_save_old_callbacks(transport, sk); 2243 2244 sk->sk_user_data = xprt; 2245 sk->sk_data_ready = xs_data_ready; 2246 sk->sk_state_change = xs_tcp_state_change; 2247 sk->sk_write_space = xs_tcp_write_space; 2248 sock_set_flag(sk, SOCK_FASYNC); 2249 sk->sk_error_report = xs_error_report; 2250 2251 /* socket options */ 2252 sock_reset_flag(sk, SOCK_LINGER); 2253 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2254 2255 xprt_clear_connected(xprt); 2256 2257 /* Reset to new socket */ 2258 transport->sock = sock; 2259 transport->inet = sk; 2260 2261 write_unlock_bh(&sk->sk_callback_lock); 2262 } 2263 2264 if (!xprt_bound(xprt)) 2265 goto out; 2266 2267 xs_set_memalloc(xprt); 2268 2269 /* Reset TCP record info */ 2270 xs_stream_reset_connect(transport); 2271 2272 /* Tell the socket layer to start connecting... */ 2273 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2274 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2275 switch (ret) { 2276 case 0: 2277 xs_set_srcport(transport, sock); 2278 /* fall through */ 2279 case -EINPROGRESS: 2280 /* SYN_SENT! */ 2281 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2282 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2283 break; 2284 case -EADDRNOTAVAIL: 2285 /* Source port number is unavailable. Try a new one! */ 2286 transport->srcport = 0; 2287 } 2288 out: 2289 return ret; 2290 } 2291 2292 /** 2293 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2294 * @work: queued work item 2295 * 2296 * Invoked by a work queue tasklet. 2297 */ 2298 static void xs_tcp_setup_socket(struct work_struct *work) 2299 { 2300 struct sock_xprt *transport = 2301 container_of(work, struct sock_xprt, connect_worker.work); 2302 struct socket *sock = transport->sock; 2303 struct rpc_xprt *xprt = &transport->xprt; 2304 int status = -EIO; 2305 2306 if (!sock) { 2307 sock = xs_create_sock(xprt, transport, 2308 xs_addr(xprt)->sa_family, SOCK_STREAM, 2309 IPPROTO_TCP, true); 2310 if (IS_ERR(sock)) { 2311 status = PTR_ERR(sock); 2312 goto out; 2313 } 2314 } 2315 2316 dprintk("RPC: worker connecting xprt %p via %s to " 2317 "%s (port %s)\n", xprt, 2318 xprt->address_strings[RPC_DISPLAY_PROTO], 2319 xprt->address_strings[RPC_DISPLAY_ADDR], 2320 xprt->address_strings[RPC_DISPLAY_PORT]); 2321 2322 status = xs_tcp_finish_connecting(xprt, sock); 2323 trace_rpc_socket_connect(xprt, sock, status); 2324 dprintk("RPC: %p connect status %d connected %d sock state %d\n", 2325 xprt, -status, xprt_connected(xprt), 2326 sock->sk->sk_state); 2327 switch (status) { 2328 default: 2329 printk("%s: connect returned unhandled error %d\n", 2330 __func__, status); 2331 /* fall through */ 2332 case -EADDRNOTAVAIL: 2333 /* We're probably in TIME_WAIT. Get rid of existing socket, 2334 * and retry 2335 */ 2336 xs_tcp_force_close(xprt); 2337 break; 2338 case 0: 2339 case -EINPROGRESS: 2340 case -EALREADY: 2341 xprt_unlock_connect(xprt, transport); 2342 return; 2343 case -EINVAL: 2344 /* Happens, for instance, if the user specified a link 2345 * local IPv6 address without a scope-id. 2346 */ 2347 case -ECONNREFUSED: 2348 case -ECONNRESET: 2349 case -ENETDOWN: 2350 case -ENETUNREACH: 2351 case -EHOSTUNREACH: 2352 case -EADDRINUSE: 2353 case -ENOBUFS: 2354 /* 2355 * xs_tcp_force_close() wakes tasks with -EIO. 2356 * We need to wake them first to ensure the 2357 * correct error code. 2358 */ 2359 xprt_wake_pending_tasks(xprt, status); 2360 xs_tcp_force_close(xprt); 2361 goto out; 2362 } 2363 status = -EAGAIN; 2364 out: 2365 xprt_clear_connecting(xprt); 2366 xprt_unlock_connect(xprt, transport); 2367 xprt_wake_pending_tasks(xprt, status); 2368 } 2369 2370 static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt) 2371 { 2372 unsigned long start, now = jiffies; 2373 2374 start = xprt->stat.connect_start + xprt->reestablish_timeout; 2375 if (time_after(start, now)) 2376 return start - now; 2377 return 0; 2378 } 2379 2380 static void xs_reconnect_backoff(struct rpc_xprt *xprt) 2381 { 2382 xprt->reestablish_timeout <<= 1; 2383 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 2384 xprt->reestablish_timeout = xprt->max_reconnect_timeout; 2385 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2386 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2387 } 2388 2389 /** 2390 * xs_connect - connect a socket to a remote endpoint 2391 * @xprt: pointer to transport structure 2392 * @task: address of RPC task that manages state of connect request 2393 * 2394 * TCP: If the remote end dropped the connection, delay reconnecting. 2395 * 2396 * UDP socket connects are synchronous, but we use a work queue anyway 2397 * to guarantee that even unprivileged user processes can set up a 2398 * socket on a privileged port. 2399 * 2400 * If a UDP socket connect fails, the delay behavior here prevents 2401 * retry floods (hard mounts). 2402 */ 2403 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2404 { 2405 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2406 unsigned long delay = 0; 2407 2408 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2409 2410 if (transport->sock != NULL) { 2411 dprintk("RPC: xs_connect delayed xprt %p for %lu " 2412 "seconds\n", 2413 xprt, xprt->reestablish_timeout / HZ); 2414 2415 /* Start by resetting any existing state */ 2416 xs_reset_transport(transport); 2417 2418 delay = xs_reconnect_delay(xprt); 2419 xs_reconnect_backoff(xprt); 2420 2421 } else 2422 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 2423 2424 queue_delayed_work(xprtiod_workqueue, 2425 &transport->connect_worker, 2426 delay); 2427 } 2428 2429 /** 2430 * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2431 * @xprt: rpc_xprt struct containing statistics 2432 * @seq: output file 2433 * 2434 */ 2435 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2436 { 2437 long idle_time = 0; 2438 2439 if (xprt_connected(xprt)) 2440 idle_time = (long)(jiffies - xprt->last_used) / HZ; 2441 2442 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 2443 "%llu %llu %lu %llu %llu\n", 2444 xprt->stat.bind_count, 2445 xprt->stat.connect_count, 2446 xprt->stat.connect_time / HZ, 2447 idle_time, 2448 xprt->stat.sends, 2449 xprt->stat.recvs, 2450 xprt->stat.bad_xids, 2451 xprt->stat.req_u, 2452 xprt->stat.bklog_u, 2453 xprt->stat.max_slots, 2454 xprt->stat.sending_u, 2455 xprt->stat.pending_u); 2456 } 2457 2458 /** 2459 * xs_udp_print_stats - display UDP socket-specifc stats 2460 * @xprt: rpc_xprt struct containing statistics 2461 * @seq: output file 2462 * 2463 */ 2464 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2465 { 2466 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2467 2468 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 2469 "%lu %llu %llu\n", 2470 transport->srcport, 2471 xprt->stat.bind_count, 2472 xprt->stat.sends, 2473 xprt->stat.recvs, 2474 xprt->stat.bad_xids, 2475 xprt->stat.req_u, 2476 xprt->stat.bklog_u, 2477 xprt->stat.max_slots, 2478 xprt->stat.sending_u, 2479 xprt->stat.pending_u); 2480 } 2481 2482 /** 2483 * xs_tcp_print_stats - display TCP socket-specifc stats 2484 * @xprt: rpc_xprt struct containing statistics 2485 * @seq: output file 2486 * 2487 */ 2488 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2489 { 2490 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2491 long idle_time = 0; 2492 2493 if (xprt_connected(xprt)) 2494 idle_time = (long)(jiffies - xprt->last_used) / HZ; 2495 2496 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 2497 "%llu %llu %lu %llu %llu\n", 2498 transport->srcport, 2499 xprt->stat.bind_count, 2500 xprt->stat.connect_count, 2501 xprt->stat.connect_time / HZ, 2502 idle_time, 2503 xprt->stat.sends, 2504 xprt->stat.recvs, 2505 xprt->stat.bad_xids, 2506 xprt->stat.req_u, 2507 xprt->stat.bklog_u, 2508 xprt->stat.max_slots, 2509 xprt->stat.sending_u, 2510 xprt->stat.pending_u); 2511 } 2512 2513 /* 2514 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 2515 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 2516 * to use the server side send routines. 2517 */ 2518 static int bc_malloc(struct rpc_task *task) 2519 { 2520 struct rpc_rqst *rqst = task->tk_rqstp; 2521 size_t size = rqst->rq_callsize; 2522 struct page *page; 2523 struct rpc_buffer *buf; 2524 2525 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 2526 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 2527 size); 2528 return -EINVAL; 2529 } 2530 2531 page = alloc_page(GFP_KERNEL); 2532 if (!page) 2533 return -ENOMEM; 2534 2535 buf = page_address(page); 2536 buf->len = PAGE_SIZE; 2537 2538 rqst->rq_buffer = buf->data; 2539 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 2540 return 0; 2541 } 2542 2543 /* 2544 * Free the space allocated in the bc_alloc routine 2545 */ 2546 static void bc_free(struct rpc_task *task) 2547 { 2548 void *buffer = task->tk_rqstp->rq_buffer; 2549 struct rpc_buffer *buf; 2550 2551 buf = container_of(buffer, struct rpc_buffer, data); 2552 free_page((unsigned long)buf); 2553 } 2554 2555 /* 2556 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 2557 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 2558 */ 2559 static int bc_sendto(struct rpc_rqst *req) 2560 { 2561 int len; 2562 struct xdr_buf *xbufp = &req->rq_snd_buf; 2563 struct rpc_xprt *xprt = req->rq_xprt; 2564 struct sock_xprt *transport = 2565 container_of(xprt, struct sock_xprt, xprt); 2566 struct socket *sock = transport->sock; 2567 unsigned long headoff; 2568 unsigned long tailoff; 2569 2570 xs_encode_stream_record_marker(xbufp); 2571 2572 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 2573 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 2574 len = svc_send_common(sock, xbufp, 2575 virt_to_page(xbufp->head[0].iov_base), headoff, 2576 xbufp->tail[0].iov_base, tailoff); 2577 2578 if (len != xbufp->len) { 2579 printk(KERN_NOTICE "Error sending entire callback!\n"); 2580 len = -EAGAIN; 2581 } 2582 2583 return len; 2584 } 2585 2586 /* 2587 * The send routine. Borrows from svc_send 2588 */ 2589 static int bc_send_request(struct rpc_rqst *req) 2590 { 2591 struct svc_xprt *xprt; 2592 int len; 2593 2594 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 2595 /* 2596 * Get the server socket associated with this callback xprt 2597 */ 2598 xprt = req->rq_xprt->bc_xprt; 2599 2600 /* 2601 * Grab the mutex to serialize data as the connection is shared 2602 * with the fore channel 2603 */ 2604 mutex_lock(&xprt->xpt_mutex); 2605 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 2606 len = -ENOTCONN; 2607 else 2608 len = bc_sendto(req); 2609 mutex_unlock(&xprt->xpt_mutex); 2610 2611 if (len > 0) 2612 len = 0; 2613 2614 return len; 2615 } 2616 2617 /* 2618 * The close routine. Since this is client initiated, we do nothing 2619 */ 2620 2621 static void bc_close(struct rpc_xprt *xprt) 2622 { 2623 } 2624 2625 /* 2626 * The xprt destroy routine. Again, because this connection is client 2627 * initiated, we do nothing 2628 */ 2629 2630 static void bc_destroy(struct rpc_xprt *xprt) 2631 { 2632 dprintk("RPC: bc_destroy xprt %p\n", xprt); 2633 2634 xs_xprt_free(xprt); 2635 module_put(THIS_MODULE); 2636 } 2637 2638 static const struct rpc_xprt_ops xs_local_ops = { 2639 .reserve_xprt = xprt_reserve_xprt, 2640 .release_xprt = xprt_release_xprt, 2641 .alloc_slot = xprt_alloc_slot, 2642 .free_slot = xprt_free_slot, 2643 .rpcbind = xs_local_rpcbind, 2644 .set_port = xs_local_set_port, 2645 .connect = xs_local_connect, 2646 .buf_alloc = rpc_malloc, 2647 .buf_free = rpc_free, 2648 .prepare_request = xs_stream_prepare_request, 2649 .send_request = xs_local_send_request, 2650 .set_retrans_timeout = xprt_set_retrans_timeout_def, 2651 .close = xs_close, 2652 .destroy = xs_destroy, 2653 .print_stats = xs_local_print_stats, 2654 .enable_swap = xs_enable_swap, 2655 .disable_swap = xs_disable_swap, 2656 }; 2657 2658 static const struct rpc_xprt_ops xs_udp_ops = { 2659 .set_buffer_size = xs_udp_set_buffer_size, 2660 .reserve_xprt = xprt_reserve_xprt_cong, 2661 .release_xprt = xprt_release_xprt_cong, 2662 .alloc_slot = xprt_alloc_slot, 2663 .free_slot = xprt_free_slot, 2664 .rpcbind = rpcb_getport_async, 2665 .set_port = xs_set_port, 2666 .connect = xs_connect, 2667 .buf_alloc = rpc_malloc, 2668 .buf_free = rpc_free, 2669 .send_request = xs_udp_send_request, 2670 .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 2671 .timer = xs_udp_timer, 2672 .release_request = xprt_release_rqst_cong, 2673 .close = xs_close, 2674 .destroy = xs_destroy, 2675 .print_stats = xs_udp_print_stats, 2676 .enable_swap = xs_enable_swap, 2677 .disable_swap = xs_disable_swap, 2678 .inject_disconnect = xs_inject_disconnect, 2679 }; 2680 2681 static const struct rpc_xprt_ops xs_tcp_ops = { 2682 .reserve_xprt = xprt_reserve_xprt, 2683 .release_xprt = xprt_release_xprt, 2684 .alloc_slot = xprt_alloc_slot, 2685 .free_slot = xprt_free_slot, 2686 .rpcbind = rpcb_getport_async, 2687 .set_port = xs_set_port, 2688 .connect = xs_connect, 2689 .buf_alloc = rpc_malloc, 2690 .buf_free = rpc_free, 2691 .prepare_request = xs_stream_prepare_request, 2692 .send_request = xs_tcp_send_request, 2693 .set_retrans_timeout = xprt_set_retrans_timeout_def, 2694 .close = xs_tcp_shutdown, 2695 .destroy = xs_destroy, 2696 .set_connect_timeout = xs_tcp_set_connect_timeout, 2697 .print_stats = xs_tcp_print_stats, 2698 .enable_swap = xs_enable_swap, 2699 .disable_swap = xs_disable_swap, 2700 .inject_disconnect = xs_inject_disconnect, 2701 #ifdef CONFIG_SUNRPC_BACKCHANNEL 2702 .bc_setup = xprt_setup_bc, 2703 .bc_maxpayload = xs_tcp_bc_maxpayload, 2704 .bc_free_rqst = xprt_free_bc_rqst, 2705 .bc_destroy = xprt_destroy_bc, 2706 #endif 2707 }; 2708 2709 /* 2710 * The rpc_xprt_ops for the server backchannel 2711 */ 2712 2713 static const struct rpc_xprt_ops bc_tcp_ops = { 2714 .reserve_xprt = xprt_reserve_xprt, 2715 .release_xprt = xprt_release_xprt, 2716 .alloc_slot = xprt_alloc_slot, 2717 .free_slot = xprt_free_slot, 2718 .buf_alloc = bc_malloc, 2719 .buf_free = bc_free, 2720 .send_request = bc_send_request, 2721 .set_retrans_timeout = xprt_set_retrans_timeout_def, 2722 .close = bc_close, 2723 .destroy = bc_destroy, 2724 .print_stats = xs_tcp_print_stats, 2725 .enable_swap = xs_enable_swap, 2726 .disable_swap = xs_disable_swap, 2727 .inject_disconnect = xs_inject_disconnect, 2728 }; 2729 2730 static int xs_init_anyaddr(const int family, struct sockaddr *sap) 2731 { 2732 static const struct sockaddr_in sin = { 2733 .sin_family = AF_INET, 2734 .sin_addr.s_addr = htonl(INADDR_ANY), 2735 }; 2736 static const struct sockaddr_in6 sin6 = { 2737 .sin6_family = AF_INET6, 2738 .sin6_addr = IN6ADDR_ANY_INIT, 2739 }; 2740 2741 switch (family) { 2742 case AF_LOCAL: 2743 break; 2744 case AF_INET: 2745 memcpy(sap, &sin, sizeof(sin)); 2746 break; 2747 case AF_INET6: 2748 memcpy(sap, &sin6, sizeof(sin6)); 2749 break; 2750 default: 2751 dprintk("RPC: %s: Bad address family\n", __func__); 2752 return -EAFNOSUPPORT; 2753 } 2754 return 0; 2755 } 2756 2757 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2758 unsigned int slot_table_size, 2759 unsigned int max_slot_table_size) 2760 { 2761 struct rpc_xprt *xprt; 2762 struct sock_xprt *new; 2763 2764 if (args->addrlen > sizeof(xprt->addr)) { 2765 dprintk("RPC: xs_setup_xprt: address too large\n"); 2766 return ERR_PTR(-EBADF); 2767 } 2768 2769 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2770 max_slot_table_size); 2771 if (xprt == NULL) { 2772 dprintk("RPC: xs_setup_xprt: couldn't allocate " 2773 "rpc_xprt\n"); 2774 return ERR_PTR(-ENOMEM); 2775 } 2776 2777 new = container_of(xprt, struct sock_xprt, xprt); 2778 mutex_init(&new->recv_mutex); 2779 memcpy(&xprt->addr, args->dstaddr, args->addrlen); 2780 xprt->addrlen = args->addrlen; 2781 if (args->srcaddr) 2782 memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 2783 else { 2784 int err; 2785 err = xs_init_anyaddr(args->dstaddr->sa_family, 2786 (struct sockaddr *)&new->srcaddr); 2787 if (err != 0) { 2788 xprt_free(xprt); 2789 return ERR_PTR(err); 2790 } 2791 } 2792 2793 return xprt; 2794 } 2795 2796 static const struct rpc_timeout xs_local_default_timeout = { 2797 .to_initval = 10 * HZ, 2798 .to_maxval = 10 * HZ, 2799 .to_retries = 2, 2800 }; 2801 2802 /** 2803 * xs_setup_local - Set up transport to use an AF_LOCAL socket 2804 * @args: rpc transport creation arguments 2805 * 2806 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2807 */ 2808 static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2809 { 2810 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2811 struct sock_xprt *transport; 2812 struct rpc_xprt *xprt; 2813 struct rpc_xprt *ret; 2814 2815 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2816 xprt_max_tcp_slot_table_entries); 2817 if (IS_ERR(xprt)) 2818 return xprt; 2819 transport = container_of(xprt, struct sock_xprt, xprt); 2820 2821 xprt->prot = 0; 2822 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2823 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2824 2825 xprt->bind_timeout = XS_BIND_TO; 2826 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2827 xprt->idle_timeout = XS_IDLE_DISC_TO; 2828 2829 xprt->ops = &xs_local_ops; 2830 xprt->timeout = &xs_local_default_timeout; 2831 2832 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 2833 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 2834 2835 switch (sun->sun_family) { 2836 case AF_LOCAL: 2837 if (sun->sun_path[0] != '/') { 2838 dprintk("RPC: bad AF_LOCAL address: %s\n", 2839 sun->sun_path); 2840 ret = ERR_PTR(-EINVAL); 2841 goto out_err; 2842 } 2843 xprt_set_bound(xprt); 2844 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 2845 ret = ERR_PTR(xs_local_setup_socket(transport)); 2846 if (ret) 2847 goto out_err; 2848 break; 2849 default: 2850 ret = ERR_PTR(-EAFNOSUPPORT); 2851 goto out_err; 2852 } 2853 2854 dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2855 xprt->address_strings[RPC_DISPLAY_ADDR]); 2856 2857 if (try_module_get(THIS_MODULE)) 2858 return xprt; 2859 ret = ERR_PTR(-EINVAL); 2860 out_err: 2861 xs_xprt_free(xprt); 2862 return ret; 2863 } 2864 2865 static const struct rpc_timeout xs_udp_default_timeout = { 2866 .to_initval = 5 * HZ, 2867 .to_maxval = 30 * HZ, 2868 .to_increment = 5 * HZ, 2869 .to_retries = 5, 2870 }; 2871 2872 /** 2873 * xs_setup_udp - Set up transport to use a UDP socket 2874 * @args: rpc transport creation arguments 2875 * 2876 */ 2877 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2878 { 2879 struct sockaddr *addr = args->dstaddr; 2880 struct rpc_xprt *xprt; 2881 struct sock_xprt *transport; 2882 struct rpc_xprt *ret; 2883 2884 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2885 xprt_udp_slot_table_entries); 2886 if (IS_ERR(xprt)) 2887 return xprt; 2888 transport = container_of(xprt, struct sock_xprt, xprt); 2889 2890 xprt->prot = IPPROTO_UDP; 2891 xprt->tsh_size = 0; 2892 /* XXX: header size can vary due to auth type, IPv6, etc. */ 2893 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2894 2895 xprt->bind_timeout = XS_BIND_TO; 2896 xprt->reestablish_timeout = XS_UDP_REEST_TO; 2897 xprt->idle_timeout = XS_IDLE_DISC_TO; 2898 2899 xprt->ops = &xs_udp_ops; 2900 2901 xprt->timeout = &xs_udp_default_timeout; 2902 2903 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 2904 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 2905 2906 switch (addr->sa_family) { 2907 case AF_INET: 2908 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 2909 xprt_set_bound(xprt); 2910 2911 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 2912 break; 2913 case AF_INET6: 2914 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 2915 xprt_set_bound(xprt); 2916 2917 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 2918 break; 2919 default: 2920 ret = ERR_PTR(-EAFNOSUPPORT); 2921 goto out_err; 2922 } 2923 2924 if (xprt_bound(xprt)) 2925 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2926 xprt->address_strings[RPC_DISPLAY_ADDR], 2927 xprt->address_strings[RPC_DISPLAY_PORT], 2928 xprt->address_strings[RPC_DISPLAY_PROTO]); 2929 else 2930 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2931 xprt->address_strings[RPC_DISPLAY_ADDR], 2932 xprt->address_strings[RPC_DISPLAY_PROTO]); 2933 2934 if (try_module_get(THIS_MODULE)) 2935 return xprt; 2936 ret = ERR_PTR(-EINVAL); 2937 out_err: 2938 xs_xprt_free(xprt); 2939 return ret; 2940 } 2941 2942 static const struct rpc_timeout xs_tcp_default_timeout = { 2943 .to_initval = 60 * HZ, 2944 .to_maxval = 60 * HZ, 2945 .to_retries = 2, 2946 }; 2947 2948 /** 2949 * xs_setup_tcp - Set up transport to use a TCP socket 2950 * @args: rpc transport creation arguments 2951 * 2952 */ 2953 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2954 { 2955 struct sockaddr *addr = args->dstaddr; 2956 struct rpc_xprt *xprt; 2957 struct sock_xprt *transport; 2958 struct rpc_xprt *ret; 2959 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 2960 2961 if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 2962 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 2963 2964 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2965 max_slot_table_size); 2966 if (IS_ERR(xprt)) 2967 return xprt; 2968 transport = container_of(xprt, struct sock_xprt, xprt); 2969 2970 xprt->prot = IPPROTO_TCP; 2971 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2972 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2973 2974 xprt->bind_timeout = XS_BIND_TO; 2975 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2976 xprt->idle_timeout = XS_IDLE_DISC_TO; 2977 2978 xprt->ops = &xs_tcp_ops; 2979 xprt->timeout = &xs_tcp_default_timeout; 2980 2981 xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 2982 xprt->connect_timeout = xprt->timeout->to_initval * 2983 (xprt->timeout->to_retries + 1); 2984 2985 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 2986 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 2987 2988 switch (addr->sa_family) { 2989 case AF_INET: 2990 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 2991 xprt_set_bound(xprt); 2992 2993 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 2994 break; 2995 case AF_INET6: 2996 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 2997 xprt_set_bound(xprt); 2998 2999 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 3000 break; 3001 default: 3002 ret = ERR_PTR(-EAFNOSUPPORT); 3003 goto out_err; 3004 } 3005 3006 if (xprt_bound(xprt)) 3007 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3008 xprt->address_strings[RPC_DISPLAY_ADDR], 3009 xprt->address_strings[RPC_DISPLAY_PORT], 3010 xprt->address_strings[RPC_DISPLAY_PROTO]); 3011 else 3012 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 3013 xprt->address_strings[RPC_DISPLAY_ADDR], 3014 xprt->address_strings[RPC_DISPLAY_PROTO]); 3015 3016 if (try_module_get(THIS_MODULE)) 3017 return xprt; 3018 ret = ERR_PTR(-EINVAL); 3019 out_err: 3020 xs_xprt_free(xprt); 3021 return ret; 3022 } 3023 3024 /** 3025 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 3026 * @args: rpc transport creation arguments 3027 * 3028 */ 3029 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3030 { 3031 struct sockaddr *addr = args->dstaddr; 3032 struct rpc_xprt *xprt; 3033 struct sock_xprt *transport; 3034 struct svc_sock *bc_sock; 3035 struct rpc_xprt *ret; 3036 3037 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3038 xprt_tcp_slot_table_entries); 3039 if (IS_ERR(xprt)) 3040 return xprt; 3041 transport = container_of(xprt, struct sock_xprt, xprt); 3042 3043 xprt->prot = IPPROTO_TCP; 3044 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3045 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3046 xprt->timeout = &xs_tcp_default_timeout; 3047 3048 /* backchannel */ 3049 xprt_set_bound(xprt); 3050 xprt->bind_timeout = 0; 3051 xprt->reestablish_timeout = 0; 3052 xprt->idle_timeout = 0; 3053 3054 xprt->ops = &bc_tcp_ops; 3055 3056 switch (addr->sa_family) { 3057 case AF_INET: 3058 xs_format_peer_addresses(xprt, "tcp", 3059 RPCBIND_NETID_TCP); 3060 break; 3061 case AF_INET6: 3062 xs_format_peer_addresses(xprt, "tcp", 3063 RPCBIND_NETID_TCP6); 3064 break; 3065 default: 3066 ret = ERR_PTR(-EAFNOSUPPORT); 3067 goto out_err; 3068 } 3069 3070 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3071 xprt->address_strings[RPC_DISPLAY_ADDR], 3072 xprt->address_strings[RPC_DISPLAY_PORT], 3073 xprt->address_strings[RPC_DISPLAY_PROTO]); 3074 3075 /* 3076 * Once we've associated a backchannel xprt with a connection, 3077 * we want to keep it around as long as the connection lasts, 3078 * in case we need to start using it for a backchannel again; 3079 * this reference won't be dropped until bc_xprt is destroyed. 3080 */ 3081 xprt_get(xprt); 3082 args->bc_xprt->xpt_bc_xprt = xprt; 3083 xprt->bc_xprt = args->bc_xprt; 3084 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 3085 transport->sock = bc_sock->sk_sock; 3086 transport->inet = bc_sock->sk_sk; 3087 3088 /* 3089 * Since we don't want connections for the backchannel, we set 3090 * the xprt status to connected 3091 */ 3092 xprt_set_connected(xprt); 3093 3094 if (try_module_get(THIS_MODULE)) 3095 return xprt; 3096 3097 args->bc_xprt->xpt_bc_xprt = NULL; 3098 args->bc_xprt->xpt_bc_xps = NULL; 3099 xprt_put(xprt); 3100 ret = ERR_PTR(-EINVAL); 3101 out_err: 3102 xs_xprt_free(xprt); 3103 return ret; 3104 } 3105 3106 static struct xprt_class xs_local_transport = { 3107 .list = LIST_HEAD_INIT(xs_local_transport.list), 3108 .name = "named UNIX socket", 3109 .owner = THIS_MODULE, 3110 .ident = XPRT_TRANSPORT_LOCAL, 3111 .setup = xs_setup_local, 3112 }; 3113 3114 static struct xprt_class xs_udp_transport = { 3115 .list = LIST_HEAD_INIT(xs_udp_transport.list), 3116 .name = "udp", 3117 .owner = THIS_MODULE, 3118 .ident = XPRT_TRANSPORT_UDP, 3119 .setup = xs_setup_udp, 3120 }; 3121 3122 static struct xprt_class xs_tcp_transport = { 3123 .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3124 .name = "tcp", 3125 .owner = THIS_MODULE, 3126 .ident = XPRT_TRANSPORT_TCP, 3127 .setup = xs_setup_tcp, 3128 }; 3129 3130 static struct xprt_class xs_bc_tcp_transport = { 3131 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3132 .name = "tcp NFSv4.1 backchannel", 3133 .owner = THIS_MODULE, 3134 .ident = XPRT_TRANSPORT_BC_TCP, 3135 .setup = xs_setup_bc_tcp, 3136 }; 3137 3138 /** 3139 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3140 * 3141 */ 3142 int init_socket_xprt(void) 3143 { 3144 if (!sunrpc_table_header) 3145 sunrpc_table_header = register_sysctl_table(sunrpc_table); 3146 3147 xprt_register_transport(&xs_local_transport); 3148 xprt_register_transport(&xs_udp_transport); 3149 xprt_register_transport(&xs_tcp_transport); 3150 xprt_register_transport(&xs_bc_tcp_transport); 3151 3152 return 0; 3153 } 3154 3155 /** 3156 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3157 * 3158 */ 3159 void cleanup_socket_xprt(void) 3160 { 3161 if (sunrpc_table_header) { 3162 unregister_sysctl_table(sunrpc_table_header); 3163 sunrpc_table_header = NULL; 3164 } 3165 3166 xprt_unregister_transport(&xs_local_transport); 3167 xprt_unregister_transport(&xs_udp_transport); 3168 xprt_unregister_transport(&xs_tcp_transport); 3169 xprt_unregister_transport(&xs_bc_tcp_transport); 3170 } 3171 3172 static int param_set_uint_minmax(const char *val, 3173 const struct kernel_param *kp, 3174 unsigned int min, unsigned int max) 3175 { 3176 unsigned int num; 3177 int ret; 3178 3179 if (!val) 3180 return -EINVAL; 3181 ret = kstrtouint(val, 0, &num); 3182 if (ret) 3183 return ret; 3184 if (num < min || num > max) 3185 return -EINVAL; 3186 *((unsigned int *)kp->arg) = num; 3187 return 0; 3188 } 3189 3190 static int param_set_portnr(const char *val, const struct kernel_param *kp) 3191 { 3192 return param_set_uint_minmax(val, kp, 3193 RPC_MIN_RESVPORT, 3194 RPC_MAX_RESVPORT); 3195 } 3196 3197 static const struct kernel_param_ops param_ops_portnr = { 3198 .set = param_set_portnr, 3199 .get = param_get_uint, 3200 }; 3201 3202 #define param_check_portnr(name, p) \ 3203 __param_check(name, p, unsigned int); 3204 3205 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3206 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3207 3208 static int param_set_slot_table_size(const char *val, 3209 const struct kernel_param *kp) 3210 { 3211 return param_set_uint_minmax(val, kp, 3212 RPC_MIN_SLOT_TABLE, 3213 RPC_MAX_SLOT_TABLE); 3214 } 3215 3216 static const struct kernel_param_ops param_ops_slot_table_size = { 3217 .set = param_set_slot_table_size, 3218 .get = param_get_uint, 3219 }; 3220 3221 #define param_check_slot_table_size(name, p) \ 3222 __param_check(name, p, unsigned int); 3223 3224 static int param_set_max_slot_table_size(const char *val, 3225 const struct kernel_param *kp) 3226 { 3227 return param_set_uint_minmax(val, kp, 3228 RPC_MIN_SLOT_TABLE, 3229 RPC_MAX_SLOT_TABLE_LIMIT); 3230 } 3231 3232 static const struct kernel_param_ops param_ops_max_slot_table_size = { 3233 .set = param_set_max_slot_table_size, 3234 .get = param_get_uint, 3235 }; 3236 3237 #define param_check_max_slot_table_size(name, p) \ 3238 __param_check(name, p, unsigned int); 3239 3240 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3241 slot_table_size, 0644); 3242 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3243 max_slot_table_size, 0644); 3244 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3245 slot_table_size, 0644); 3246