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