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 occured, 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 doens'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 case -ENOBUFS: 1879 break; 1880 case -ENOENT: 1881 dprintk("RPC: xprt %p: socket %s does not exist\n", 1882 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1883 break; 1884 case -ECONNREFUSED: 1885 dprintk("RPC: xprt %p: connection refused for %s\n", 1886 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1887 break; 1888 default: 1889 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 1890 __func__, -status, 1891 xprt->address_strings[RPC_DISPLAY_ADDR]); 1892 } 1893 1894 out: 1895 xprt_clear_connecting(xprt); 1896 xprt_wake_pending_tasks(xprt, status); 1897 return status; 1898 } 1899 1900 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 1901 { 1902 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1903 int ret; 1904 1905 if (RPC_IS_ASYNC(task)) { 1906 /* 1907 * We want the AF_LOCAL connect to be resolved in the 1908 * filesystem namespace of the process making the rpc 1909 * call. Thus we connect synchronously. 1910 * 1911 * If we want to support asynchronous AF_LOCAL calls, 1912 * we'll need to figure out how to pass a namespace to 1913 * connect. 1914 */ 1915 task->tk_rpc_status = -ENOTCONN; 1916 rpc_exit(task, -ENOTCONN); 1917 return; 1918 } 1919 ret = xs_local_setup_socket(transport); 1920 if (ret && !RPC_IS_SOFTCONN(task)) 1921 msleep_interruptible(15000); 1922 } 1923 1924 #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 1925 /* 1926 * Note that this should be called with XPRT_LOCKED held (or when we otherwise 1927 * know that we have exclusive access to the socket), to guard against 1928 * races with xs_reset_transport. 1929 */ 1930 static void xs_set_memalloc(struct rpc_xprt *xprt) 1931 { 1932 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1933 xprt); 1934 1935 /* 1936 * If there's no sock, then we have nothing to set. The 1937 * reconnecting process will get it for us. 1938 */ 1939 if (!transport->inet) 1940 return; 1941 if (atomic_read(&xprt->swapper)) 1942 sk_set_memalloc(transport->inet); 1943 } 1944 1945 /** 1946 * xs_enable_swap - Tag this transport as being used for swap. 1947 * @xprt: transport to tag 1948 * 1949 * Take a reference to this transport on behalf of the rpc_clnt, and 1950 * optionally mark it for swapping if it wasn't already. 1951 */ 1952 static int 1953 xs_enable_swap(struct rpc_xprt *xprt) 1954 { 1955 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 1956 1957 if (atomic_inc_return(&xprt->swapper) != 1) 1958 return 0; 1959 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 1960 return -ERESTARTSYS; 1961 if (xs->inet) 1962 sk_set_memalloc(xs->inet); 1963 xprt_release_xprt(xprt, NULL); 1964 return 0; 1965 } 1966 1967 /** 1968 * xs_disable_swap - Untag this transport as being used for swap. 1969 * @xprt: transport to tag 1970 * 1971 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 1972 * swapper refcount goes to 0, untag the socket as a memalloc socket. 1973 */ 1974 static void 1975 xs_disable_swap(struct rpc_xprt *xprt) 1976 { 1977 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 1978 1979 if (!atomic_dec_and_test(&xprt->swapper)) 1980 return; 1981 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 1982 return; 1983 if (xs->inet) 1984 sk_clear_memalloc(xs->inet); 1985 xprt_release_xprt(xprt, NULL); 1986 } 1987 #else 1988 static void xs_set_memalloc(struct rpc_xprt *xprt) 1989 { 1990 } 1991 1992 static int 1993 xs_enable_swap(struct rpc_xprt *xprt) 1994 { 1995 return -EINVAL; 1996 } 1997 1998 static void 1999 xs_disable_swap(struct rpc_xprt *xprt) 2000 { 2001 } 2002 #endif 2003 2004 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2005 { 2006 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2007 2008 if (!transport->inet) { 2009 struct sock *sk = sock->sk; 2010 2011 write_lock_bh(&sk->sk_callback_lock); 2012 2013 xs_save_old_callbacks(transport, sk); 2014 2015 sk->sk_user_data = xprt; 2016 sk->sk_data_ready = xs_data_ready; 2017 sk->sk_write_space = xs_udp_write_space; 2018 sock_set_flag(sk, SOCK_FASYNC); 2019 2020 xprt_set_connected(xprt); 2021 2022 /* Reset to new socket */ 2023 transport->sock = sock; 2024 transport->inet = sk; 2025 2026 xs_set_memalloc(xprt); 2027 2028 write_unlock_bh(&sk->sk_callback_lock); 2029 } 2030 xs_udp_do_set_buffer_size(xprt); 2031 2032 xprt->stat.connect_start = jiffies; 2033 } 2034 2035 static void xs_udp_setup_socket(struct work_struct *work) 2036 { 2037 struct sock_xprt *transport = 2038 container_of(work, struct sock_xprt, connect_worker.work); 2039 struct rpc_xprt *xprt = &transport->xprt; 2040 struct socket *sock; 2041 int status = -EIO; 2042 2043 sock = xs_create_sock(xprt, transport, 2044 xs_addr(xprt)->sa_family, SOCK_DGRAM, 2045 IPPROTO_UDP, false); 2046 if (IS_ERR(sock)) 2047 goto out; 2048 2049 dprintk("RPC: worker connecting xprt %p via %s to " 2050 "%s (port %s)\n", xprt, 2051 xprt->address_strings[RPC_DISPLAY_PROTO], 2052 xprt->address_strings[RPC_DISPLAY_ADDR], 2053 xprt->address_strings[RPC_DISPLAY_PORT]); 2054 2055 xs_udp_finish_connecting(xprt, sock); 2056 trace_rpc_socket_connect(xprt, sock, 0); 2057 status = 0; 2058 out: 2059 xprt_clear_connecting(xprt); 2060 xprt_unlock_connect(xprt, transport); 2061 xprt_wake_pending_tasks(xprt, status); 2062 } 2063 2064 /** 2065 * xs_tcp_shutdown - gracefully shut down a TCP socket 2066 * @xprt: transport 2067 * 2068 * Initiates a graceful shutdown of the TCP socket by calling the 2069 * equivalent of shutdown(SHUT_RDWR); 2070 */ 2071 static void xs_tcp_shutdown(struct rpc_xprt *xprt) 2072 { 2073 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2074 struct socket *sock = transport->sock; 2075 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 2076 2077 if (sock == NULL) 2078 return; 2079 switch (skst) { 2080 default: 2081 kernel_sock_shutdown(sock, SHUT_RDWR); 2082 trace_rpc_socket_shutdown(xprt, sock); 2083 break; 2084 case TCP_CLOSE: 2085 case TCP_TIME_WAIT: 2086 xs_reset_transport(transport); 2087 } 2088 } 2089 2090 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 2091 struct socket *sock) 2092 { 2093 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2094 unsigned int keepidle; 2095 unsigned int keepcnt; 2096 unsigned int timeo; 2097 2098 spin_lock(&xprt->transport_lock); 2099 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 2100 keepcnt = xprt->timeout->to_retries + 1; 2101 timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 2102 (xprt->timeout->to_retries + 1); 2103 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2104 spin_unlock(&xprt->transport_lock); 2105 2106 /* TCP Keepalive options */ 2107 sock_set_keepalive(sock->sk); 2108 tcp_sock_set_keepidle(sock->sk, keepidle); 2109 tcp_sock_set_keepintvl(sock->sk, keepidle); 2110 tcp_sock_set_keepcnt(sock->sk, keepcnt); 2111 2112 /* TCP user timeout (see RFC5482) */ 2113 tcp_sock_set_user_timeout(sock->sk, timeo); 2114 } 2115 2116 static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 2117 unsigned long connect_timeout, 2118 unsigned long reconnect_timeout) 2119 { 2120 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2121 struct rpc_timeout to; 2122 unsigned long initval; 2123 2124 spin_lock(&xprt->transport_lock); 2125 if (reconnect_timeout < xprt->max_reconnect_timeout) 2126 xprt->max_reconnect_timeout = reconnect_timeout; 2127 if (connect_timeout < xprt->connect_timeout) { 2128 memcpy(&to, xprt->timeout, sizeof(to)); 2129 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 2130 /* Arbitrary lower limit */ 2131 if (initval < XS_TCP_INIT_REEST_TO << 1) 2132 initval = XS_TCP_INIT_REEST_TO << 1; 2133 to.to_initval = initval; 2134 to.to_maxval = initval; 2135 memcpy(&transport->tcp_timeout, &to, 2136 sizeof(transport->tcp_timeout)); 2137 xprt->timeout = &transport->tcp_timeout; 2138 xprt->connect_timeout = connect_timeout; 2139 } 2140 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2141 spin_unlock(&xprt->transport_lock); 2142 } 2143 2144 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2145 { 2146 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2147 int ret = -ENOTCONN; 2148 2149 if (!transport->inet) { 2150 struct sock *sk = sock->sk; 2151 2152 /* Avoid temporary address, they are bad for long-lived 2153 * connections such as NFS mounts. 2154 * RFC4941, section 3.6 suggests that: 2155 * Individual applications, which have specific 2156 * knowledge about the normal duration of connections, 2157 * MAY override this as appropriate. 2158 */ 2159 if (xs_addr(xprt)->sa_family == PF_INET6) { 2160 ip6_sock_set_addr_preferences(sk, 2161 IPV6_PREFER_SRC_PUBLIC); 2162 } 2163 2164 xs_tcp_set_socket_timeouts(xprt, sock); 2165 2166 write_lock_bh(&sk->sk_callback_lock); 2167 2168 xs_save_old_callbacks(transport, sk); 2169 2170 sk->sk_user_data = xprt; 2171 sk->sk_data_ready = xs_data_ready; 2172 sk->sk_state_change = xs_tcp_state_change; 2173 sk->sk_write_space = xs_tcp_write_space; 2174 sock_set_flag(sk, SOCK_FASYNC); 2175 sk->sk_error_report = xs_error_report; 2176 2177 /* socket options */ 2178 sock_reset_flag(sk, SOCK_LINGER); 2179 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2180 2181 xprt_clear_connected(xprt); 2182 2183 /* Reset to new socket */ 2184 transport->sock = sock; 2185 transport->inet = sk; 2186 2187 write_unlock_bh(&sk->sk_callback_lock); 2188 } 2189 2190 if (!xprt_bound(xprt)) 2191 goto out; 2192 2193 xs_set_memalloc(xprt); 2194 2195 xs_stream_start_connect(transport); 2196 2197 /* Tell the socket layer to start connecting... */ 2198 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2199 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2200 switch (ret) { 2201 case 0: 2202 xs_set_srcport(transport, sock); 2203 fallthrough; 2204 case -EINPROGRESS: 2205 /* SYN_SENT! */ 2206 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2207 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2208 break; 2209 case -EADDRNOTAVAIL: 2210 /* Source port number is unavailable. Try a new one! */ 2211 transport->srcport = 0; 2212 } 2213 out: 2214 return ret; 2215 } 2216 2217 /** 2218 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2219 * @work: queued work item 2220 * 2221 * Invoked by a work queue tasklet. 2222 */ 2223 static void xs_tcp_setup_socket(struct work_struct *work) 2224 { 2225 struct sock_xprt *transport = 2226 container_of(work, struct sock_xprt, connect_worker.work); 2227 struct socket *sock = transport->sock; 2228 struct rpc_xprt *xprt = &transport->xprt; 2229 int status = -EIO; 2230 2231 if (!sock) { 2232 sock = xs_create_sock(xprt, transport, 2233 xs_addr(xprt)->sa_family, SOCK_STREAM, 2234 IPPROTO_TCP, true); 2235 if (IS_ERR(sock)) { 2236 status = PTR_ERR(sock); 2237 goto out; 2238 } 2239 } 2240 2241 dprintk("RPC: worker connecting xprt %p via %s to " 2242 "%s (port %s)\n", xprt, 2243 xprt->address_strings[RPC_DISPLAY_PROTO], 2244 xprt->address_strings[RPC_DISPLAY_ADDR], 2245 xprt->address_strings[RPC_DISPLAY_PORT]); 2246 2247 status = xs_tcp_finish_connecting(xprt, sock); 2248 trace_rpc_socket_connect(xprt, sock, status); 2249 dprintk("RPC: %p connect status %d connected %d sock state %d\n", 2250 xprt, -status, xprt_connected(xprt), 2251 sock->sk->sk_state); 2252 switch (status) { 2253 default: 2254 printk("%s: connect returned unhandled error %d\n", 2255 __func__, status); 2256 fallthrough; 2257 case -EADDRNOTAVAIL: 2258 /* We're probably in TIME_WAIT. Get rid of existing socket, 2259 * and retry 2260 */ 2261 xs_tcp_force_close(xprt); 2262 break; 2263 case 0: 2264 case -EINPROGRESS: 2265 case -EALREADY: 2266 xprt_unlock_connect(xprt, transport); 2267 return; 2268 case -EINVAL: 2269 /* Happens, for instance, if the user specified a link 2270 * local IPv6 address without a scope-id. 2271 */ 2272 case -ECONNREFUSED: 2273 case -ECONNRESET: 2274 case -ENETDOWN: 2275 case -ENETUNREACH: 2276 case -EHOSTUNREACH: 2277 case -EADDRINUSE: 2278 case -ENOBUFS: 2279 /* 2280 * xs_tcp_force_close() wakes tasks with -EIO. 2281 * We need to wake them first to ensure the 2282 * correct error code. 2283 */ 2284 xprt_wake_pending_tasks(xprt, status); 2285 xs_tcp_force_close(xprt); 2286 goto out; 2287 } 2288 status = -EAGAIN; 2289 out: 2290 xprt_clear_connecting(xprt); 2291 xprt_unlock_connect(xprt, transport); 2292 xprt_wake_pending_tasks(xprt, status); 2293 } 2294 2295 /** 2296 * xs_connect - connect a socket to a remote endpoint 2297 * @xprt: pointer to transport structure 2298 * @task: address of RPC task that manages state of connect request 2299 * 2300 * TCP: If the remote end dropped the connection, delay reconnecting. 2301 * 2302 * UDP socket connects are synchronous, but we use a work queue anyway 2303 * to guarantee that even unprivileged user processes can set up a 2304 * socket on a privileged port. 2305 * 2306 * If a UDP socket connect fails, the delay behavior here prevents 2307 * retry floods (hard mounts). 2308 */ 2309 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2310 { 2311 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2312 unsigned long delay = 0; 2313 2314 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2315 2316 if (transport->sock != NULL) { 2317 dprintk("RPC: xs_connect delayed xprt %p for %lu " 2318 "seconds\n", 2319 xprt, xprt->reestablish_timeout / HZ); 2320 2321 /* Start by resetting any existing state */ 2322 xs_reset_transport(transport); 2323 2324 delay = xprt_reconnect_delay(xprt); 2325 xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); 2326 2327 } else 2328 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 2329 2330 queue_delayed_work(xprtiod_workqueue, 2331 &transport->connect_worker, 2332 delay); 2333 } 2334 2335 static void xs_wake_disconnect(struct sock_xprt *transport) 2336 { 2337 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) 2338 xs_tcp_force_close(&transport->xprt); 2339 } 2340 2341 static void xs_wake_write(struct sock_xprt *transport) 2342 { 2343 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) 2344 xprt_write_space(&transport->xprt); 2345 } 2346 2347 static void xs_wake_error(struct sock_xprt *transport) 2348 { 2349 int sockerr; 2350 2351 if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 2352 return; 2353 mutex_lock(&transport->recv_mutex); 2354 if (transport->sock == NULL) 2355 goto out; 2356 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) 2357 goto out; 2358 sockerr = xchg(&transport->xprt_err, 0); 2359 if (sockerr < 0) 2360 xprt_wake_pending_tasks(&transport->xprt, sockerr); 2361 out: 2362 mutex_unlock(&transport->recv_mutex); 2363 } 2364 2365 static void xs_wake_pending(struct sock_xprt *transport) 2366 { 2367 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) 2368 xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); 2369 } 2370 2371 static void xs_error_handle(struct work_struct *work) 2372 { 2373 struct sock_xprt *transport = container_of(work, 2374 struct sock_xprt, error_worker); 2375 2376 xs_wake_disconnect(transport); 2377 xs_wake_write(transport); 2378 xs_wake_error(transport); 2379 xs_wake_pending(transport); 2380 } 2381 2382 /** 2383 * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2384 * @xprt: rpc_xprt struct containing statistics 2385 * @seq: output file 2386 * 2387 */ 2388 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2389 { 2390 long idle_time = 0; 2391 2392 if (xprt_connected(xprt)) 2393 idle_time = (long)(jiffies - xprt->last_used) / HZ; 2394 2395 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 2396 "%llu %llu %lu %llu %llu\n", 2397 xprt->stat.bind_count, 2398 xprt->stat.connect_count, 2399 xprt->stat.connect_time / HZ, 2400 idle_time, 2401 xprt->stat.sends, 2402 xprt->stat.recvs, 2403 xprt->stat.bad_xids, 2404 xprt->stat.req_u, 2405 xprt->stat.bklog_u, 2406 xprt->stat.max_slots, 2407 xprt->stat.sending_u, 2408 xprt->stat.pending_u); 2409 } 2410 2411 /** 2412 * xs_udp_print_stats - display UDP socket-specifc stats 2413 * @xprt: rpc_xprt struct containing statistics 2414 * @seq: output file 2415 * 2416 */ 2417 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2418 { 2419 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2420 2421 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 2422 "%lu %llu %llu\n", 2423 transport->srcport, 2424 xprt->stat.bind_count, 2425 xprt->stat.sends, 2426 xprt->stat.recvs, 2427 xprt->stat.bad_xids, 2428 xprt->stat.req_u, 2429 xprt->stat.bklog_u, 2430 xprt->stat.max_slots, 2431 xprt->stat.sending_u, 2432 xprt->stat.pending_u); 2433 } 2434 2435 /** 2436 * xs_tcp_print_stats - display TCP socket-specifc stats 2437 * @xprt: rpc_xprt struct containing statistics 2438 * @seq: output file 2439 * 2440 */ 2441 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2442 { 2443 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2444 long idle_time = 0; 2445 2446 if (xprt_connected(xprt)) 2447 idle_time = (long)(jiffies - xprt->last_used) / HZ; 2448 2449 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 2450 "%llu %llu %lu %llu %llu\n", 2451 transport->srcport, 2452 xprt->stat.bind_count, 2453 xprt->stat.connect_count, 2454 xprt->stat.connect_time / HZ, 2455 idle_time, 2456 xprt->stat.sends, 2457 xprt->stat.recvs, 2458 xprt->stat.bad_xids, 2459 xprt->stat.req_u, 2460 xprt->stat.bklog_u, 2461 xprt->stat.max_slots, 2462 xprt->stat.sending_u, 2463 xprt->stat.pending_u); 2464 } 2465 2466 /* 2467 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 2468 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 2469 * to use the server side send routines. 2470 */ 2471 static int bc_malloc(struct rpc_task *task) 2472 { 2473 struct rpc_rqst *rqst = task->tk_rqstp; 2474 size_t size = rqst->rq_callsize; 2475 struct page *page; 2476 struct rpc_buffer *buf; 2477 2478 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 2479 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 2480 size); 2481 return -EINVAL; 2482 } 2483 2484 page = alloc_page(GFP_KERNEL); 2485 if (!page) 2486 return -ENOMEM; 2487 2488 buf = page_address(page); 2489 buf->len = PAGE_SIZE; 2490 2491 rqst->rq_buffer = buf->data; 2492 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 2493 return 0; 2494 } 2495 2496 /* 2497 * Free the space allocated in the bc_alloc routine 2498 */ 2499 static void bc_free(struct rpc_task *task) 2500 { 2501 void *buffer = task->tk_rqstp->rq_buffer; 2502 struct rpc_buffer *buf; 2503 2504 buf = container_of(buffer, struct rpc_buffer, data); 2505 free_page((unsigned long)buf); 2506 } 2507 2508 static int bc_sendto(struct rpc_rqst *req) 2509 { 2510 struct xdr_buf *xdr = &req->rq_snd_buf; 2511 struct sock_xprt *transport = 2512 container_of(req->rq_xprt, struct sock_xprt, xprt); 2513 struct msghdr msg = { 2514 .msg_flags = 0, 2515 }; 2516 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | 2517 (u32)xdr->len); 2518 unsigned int sent = 0; 2519 int err; 2520 2521 req->rq_xtime = ktime_get(); 2522 err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent); 2523 xdr_free_bvec(xdr); 2524 if (err < 0 || sent != (xdr->len + sizeof(marker))) 2525 return -EAGAIN; 2526 return sent; 2527 } 2528 2529 /** 2530 * bc_send_request - Send a backchannel Call on a TCP socket 2531 * @req: rpc_rqst containing Call message to be sent 2532 * 2533 * xpt_mutex ensures @rqstp's whole message is written to the socket 2534 * without interruption. 2535 * 2536 * Return values: 2537 * %0 if the message was sent successfully 2538 * %ENOTCONN if the message was not sent 2539 */ 2540 static int bc_send_request(struct rpc_rqst *req) 2541 { 2542 struct svc_xprt *xprt; 2543 int len; 2544 2545 /* 2546 * Get the server socket associated with this callback xprt 2547 */ 2548 xprt = req->rq_xprt->bc_xprt; 2549 2550 /* 2551 * Grab the mutex to serialize data as the connection is shared 2552 * with the fore channel 2553 */ 2554 mutex_lock(&xprt->xpt_mutex); 2555 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 2556 len = -ENOTCONN; 2557 else 2558 len = bc_sendto(req); 2559 mutex_unlock(&xprt->xpt_mutex); 2560 2561 if (len > 0) 2562 len = 0; 2563 2564 return len; 2565 } 2566 2567 /* 2568 * The close routine. Since this is client initiated, we do nothing 2569 */ 2570 2571 static void bc_close(struct rpc_xprt *xprt) 2572 { 2573 xprt_disconnect_done(xprt); 2574 } 2575 2576 /* 2577 * The xprt destroy routine. Again, because this connection is client 2578 * initiated, we do nothing 2579 */ 2580 2581 static void bc_destroy(struct rpc_xprt *xprt) 2582 { 2583 dprintk("RPC: bc_destroy xprt %p\n", xprt); 2584 2585 xs_xprt_free(xprt); 2586 module_put(THIS_MODULE); 2587 } 2588 2589 static const struct rpc_xprt_ops xs_local_ops = { 2590 .reserve_xprt = xprt_reserve_xprt, 2591 .release_xprt = xprt_release_xprt, 2592 .alloc_slot = xprt_alloc_slot, 2593 .free_slot = xprt_free_slot, 2594 .rpcbind = xs_local_rpcbind, 2595 .set_port = xs_local_set_port, 2596 .connect = xs_local_connect, 2597 .buf_alloc = rpc_malloc, 2598 .buf_free = rpc_free, 2599 .prepare_request = xs_stream_prepare_request, 2600 .send_request = xs_local_send_request, 2601 .wait_for_reply_request = xprt_wait_for_reply_request_def, 2602 .close = xs_close, 2603 .destroy = xs_destroy, 2604 .print_stats = xs_local_print_stats, 2605 .enable_swap = xs_enable_swap, 2606 .disable_swap = xs_disable_swap, 2607 }; 2608 2609 static const struct rpc_xprt_ops xs_udp_ops = { 2610 .set_buffer_size = xs_udp_set_buffer_size, 2611 .reserve_xprt = xprt_reserve_xprt_cong, 2612 .release_xprt = xprt_release_xprt_cong, 2613 .alloc_slot = xprt_alloc_slot, 2614 .free_slot = xprt_free_slot, 2615 .rpcbind = rpcb_getport_async, 2616 .set_port = xs_set_port, 2617 .connect = xs_connect, 2618 .buf_alloc = rpc_malloc, 2619 .buf_free = rpc_free, 2620 .send_request = xs_udp_send_request, 2621 .wait_for_reply_request = xprt_wait_for_reply_request_rtt, 2622 .timer = xs_udp_timer, 2623 .release_request = xprt_release_rqst_cong, 2624 .close = xs_close, 2625 .destroy = xs_destroy, 2626 .print_stats = xs_udp_print_stats, 2627 .enable_swap = xs_enable_swap, 2628 .disable_swap = xs_disable_swap, 2629 .inject_disconnect = xs_inject_disconnect, 2630 }; 2631 2632 static const struct rpc_xprt_ops xs_tcp_ops = { 2633 .reserve_xprt = xprt_reserve_xprt, 2634 .release_xprt = xprt_release_xprt, 2635 .alloc_slot = xprt_alloc_slot, 2636 .free_slot = xprt_free_slot, 2637 .rpcbind = rpcb_getport_async, 2638 .set_port = xs_set_port, 2639 .connect = xs_connect, 2640 .buf_alloc = rpc_malloc, 2641 .buf_free = rpc_free, 2642 .prepare_request = xs_stream_prepare_request, 2643 .send_request = xs_tcp_send_request, 2644 .wait_for_reply_request = xprt_wait_for_reply_request_def, 2645 .close = xs_tcp_shutdown, 2646 .destroy = xs_destroy, 2647 .set_connect_timeout = xs_tcp_set_connect_timeout, 2648 .print_stats = xs_tcp_print_stats, 2649 .enable_swap = xs_enable_swap, 2650 .disable_swap = xs_disable_swap, 2651 .inject_disconnect = xs_inject_disconnect, 2652 #ifdef CONFIG_SUNRPC_BACKCHANNEL 2653 .bc_setup = xprt_setup_bc, 2654 .bc_maxpayload = xs_tcp_bc_maxpayload, 2655 .bc_num_slots = xprt_bc_max_slots, 2656 .bc_free_rqst = xprt_free_bc_rqst, 2657 .bc_destroy = xprt_destroy_bc, 2658 #endif 2659 }; 2660 2661 /* 2662 * The rpc_xprt_ops for the server backchannel 2663 */ 2664 2665 static const struct rpc_xprt_ops bc_tcp_ops = { 2666 .reserve_xprt = xprt_reserve_xprt, 2667 .release_xprt = xprt_release_xprt, 2668 .alloc_slot = xprt_alloc_slot, 2669 .free_slot = xprt_free_slot, 2670 .buf_alloc = bc_malloc, 2671 .buf_free = bc_free, 2672 .send_request = bc_send_request, 2673 .wait_for_reply_request = xprt_wait_for_reply_request_def, 2674 .close = bc_close, 2675 .destroy = bc_destroy, 2676 .print_stats = xs_tcp_print_stats, 2677 .enable_swap = xs_enable_swap, 2678 .disable_swap = xs_disable_swap, 2679 .inject_disconnect = xs_inject_disconnect, 2680 }; 2681 2682 static int xs_init_anyaddr(const int family, struct sockaddr *sap) 2683 { 2684 static const struct sockaddr_in sin = { 2685 .sin_family = AF_INET, 2686 .sin_addr.s_addr = htonl(INADDR_ANY), 2687 }; 2688 static const struct sockaddr_in6 sin6 = { 2689 .sin6_family = AF_INET6, 2690 .sin6_addr = IN6ADDR_ANY_INIT, 2691 }; 2692 2693 switch (family) { 2694 case AF_LOCAL: 2695 break; 2696 case AF_INET: 2697 memcpy(sap, &sin, sizeof(sin)); 2698 break; 2699 case AF_INET6: 2700 memcpy(sap, &sin6, sizeof(sin6)); 2701 break; 2702 default: 2703 dprintk("RPC: %s: Bad address family\n", __func__); 2704 return -EAFNOSUPPORT; 2705 } 2706 return 0; 2707 } 2708 2709 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2710 unsigned int slot_table_size, 2711 unsigned int max_slot_table_size) 2712 { 2713 struct rpc_xprt *xprt; 2714 struct sock_xprt *new; 2715 2716 if (args->addrlen > sizeof(xprt->addr)) { 2717 dprintk("RPC: xs_setup_xprt: address too large\n"); 2718 return ERR_PTR(-EBADF); 2719 } 2720 2721 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2722 max_slot_table_size); 2723 if (xprt == NULL) { 2724 dprintk("RPC: xs_setup_xprt: couldn't allocate " 2725 "rpc_xprt\n"); 2726 return ERR_PTR(-ENOMEM); 2727 } 2728 2729 new = container_of(xprt, struct sock_xprt, xprt); 2730 mutex_init(&new->recv_mutex); 2731 memcpy(&xprt->addr, args->dstaddr, args->addrlen); 2732 xprt->addrlen = args->addrlen; 2733 if (args->srcaddr) 2734 memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 2735 else { 2736 int err; 2737 err = xs_init_anyaddr(args->dstaddr->sa_family, 2738 (struct sockaddr *)&new->srcaddr); 2739 if (err != 0) { 2740 xprt_free(xprt); 2741 return ERR_PTR(err); 2742 } 2743 } 2744 2745 return xprt; 2746 } 2747 2748 static const struct rpc_timeout xs_local_default_timeout = { 2749 .to_initval = 10 * HZ, 2750 .to_maxval = 10 * HZ, 2751 .to_retries = 2, 2752 }; 2753 2754 /** 2755 * xs_setup_local - Set up transport to use an AF_LOCAL socket 2756 * @args: rpc transport creation arguments 2757 * 2758 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2759 */ 2760 static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2761 { 2762 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2763 struct sock_xprt *transport; 2764 struct rpc_xprt *xprt; 2765 struct rpc_xprt *ret; 2766 2767 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2768 xprt_max_tcp_slot_table_entries); 2769 if (IS_ERR(xprt)) 2770 return xprt; 2771 transport = container_of(xprt, struct sock_xprt, xprt); 2772 2773 xprt->prot = 0; 2774 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2775 2776 xprt->bind_timeout = XS_BIND_TO; 2777 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2778 xprt->idle_timeout = XS_IDLE_DISC_TO; 2779 2780 xprt->ops = &xs_local_ops; 2781 xprt->timeout = &xs_local_default_timeout; 2782 2783 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 2784 INIT_WORK(&transport->error_worker, xs_error_handle); 2785 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 2786 2787 switch (sun->sun_family) { 2788 case AF_LOCAL: 2789 if (sun->sun_path[0] != '/') { 2790 dprintk("RPC: bad AF_LOCAL address: %s\n", 2791 sun->sun_path); 2792 ret = ERR_PTR(-EINVAL); 2793 goto out_err; 2794 } 2795 xprt_set_bound(xprt); 2796 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 2797 ret = ERR_PTR(xs_local_setup_socket(transport)); 2798 if (ret) 2799 goto out_err; 2800 break; 2801 default: 2802 ret = ERR_PTR(-EAFNOSUPPORT); 2803 goto out_err; 2804 } 2805 2806 dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2807 xprt->address_strings[RPC_DISPLAY_ADDR]); 2808 2809 if (try_module_get(THIS_MODULE)) 2810 return xprt; 2811 ret = ERR_PTR(-EINVAL); 2812 out_err: 2813 xs_xprt_free(xprt); 2814 return ret; 2815 } 2816 2817 static const struct rpc_timeout xs_udp_default_timeout = { 2818 .to_initval = 5 * HZ, 2819 .to_maxval = 30 * HZ, 2820 .to_increment = 5 * HZ, 2821 .to_retries = 5, 2822 }; 2823 2824 /** 2825 * xs_setup_udp - Set up transport to use a UDP socket 2826 * @args: rpc transport creation arguments 2827 * 2828 */ 2829 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2830 { 2831 struct sockaddr *addr = args->dstaddr; 2832 struct rpc_xprt *xprt; 2833 struct sock_xprt *transport; 2834 struct rpc_xprt *ret; 2835 2836 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2837 xprt_udp_slot_table_entries); 2838 if (IS_ERR(xprt)) 2839 return xprt; 2840 transport = container_of(xprt, struct sock_xprt, xprt); 2841 2842 xprt->prot = IPPROTO_UDP; 2843 /* XXX: header size can vary due to auth type, IPv6, etc. */ 2844 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2845 2846 xprt->bind_timeout = XS_BIND_TO; 2847 xprt->reestablish_timeout = XS_UDP_REEST_TO; 2848 xprt->idle_timeout = XS_IDLE_DISC_TO; 2849 2850 xprt->ops = &xs_udp_ops; 2851 2852 xprt->timeout = &xs_udp_default_timeout; 2853 2854 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 2855 INIT_WORK(&transport->error_worker, xs_error_handle); 2856 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 2857 2858 switch (addr->sa_family) { 2859 case AF_INET: 2860 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 2861 xprt_set_bound(xprt); 2862 2863 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 2864 break; 2865 case AF_INET6: 2866 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 2867 xprt_set_bound(xprt); 2868 2869 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 2870 break; 2871 default: 2872 ret = ERR_PTR(-EAFNOSUPPORT); 2873 goto out_err; 2874 } 2875 2876 if (xprt_bound(xprt)) 2877 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2878 xprt->address_strings[RPC_DISPLAY_ADDR], 2879 xprt->address_strings[RPC_DISPLAY_PORT], 2880 xprt->address_strings[RPC_DISPLAY_PROTO]); 2881 else 2882 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2883 xprt->address_strings[RPC_DISPLAY_ADDR], 2884 xprt->address_strings[RPC_DISPLAY_PROTO]); 2885 2886 if (try_module_get(THIS_MODULE)) 2887 return xprt; 2888 ret = ERR_PTR(-EINVAL); 2889 out_err: 2890 xs_xprt_free(xprt); 2891 return ret; 2892 } 2893 2894 static const struct rpc_timeout xs_tcp_default_timeout = { 2895 .to_initval = 60 * HZ, 2896 .to_maxval = 60 * HZ, 2897 .to_retries = 2, 2898 }; 2899 2900 /** 2901 * xs_setup_tcp - Set up transport to use a TCP socket 2902 * @args: rpc transport creation arguments 2903 * 2904 */ 2905 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2906 { 2907 struct sockaddr *addr = args->dstaddr; 2908 struct rpc_xprt *xprt; 2909 struct sock_xprt *transport; 2910 struct rpc_xprt *ret; 2911 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 2912 2913 if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 2914 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 2915 2916 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2917 max_slot_table_size); 2918 if (IS_ERR(xprt)) 2919 return xprt; 2920 transport = container_of(xprt, struct sock_xprt, xprt); 2921 2922 xprt->prot = IPPROTO_TCP; 2923 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2924 2925 xprt->bind_timeout = XS_BIND_TO; 2926 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2927 xprt->idle_timeout = XS_IDLE_DISC_TO; 2928 2929 xprt->ops = &xs_tcp_ops; 2930 xprt->timeout = &xs_tcp_default_timeout; 2931 2932 xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 2933 xprt->connect_timeout = xprt->timeout->to_initval * 2934 (xprt->timeout->to_retries + 1); 2935 2936 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 2937 INIT_WORK(&transport->error_worker, xs_error_handle); 2938 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 2939 2940 switch (addr->sa_family) { 2941 case AF_INET: 2942 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 2943 xprt_set_bound(xprt); 2944 2945 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 2946 break; 2947 case AF_INET6: 2948 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 2949 xprt_set_bound(xprt); 2950 2951 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 2952 break; 2953 default: 2954 ret = ERR_PTR(-EAFNOSUPPORT); 2955 goto out_err; 2956 } 2957 2958 if (xprt_bound(xprt)) 2959 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2960 xprt->address_strings[RPC_DISPLAY_ADDR], 2961 xprt->address_strings[RPC_DISPLAY_PORT], 2962 xprt->address_strings[RPC_DISPLAY_PROTO]); 2963 else 2964 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2965 xprt->address_strings[RPC_DISPLAY_ADDR], 2966 xprt->address_strings[RPC_DISPLAY_PROTO]); 2967 2968 if (try_module_get(THIS_MODULE)) 2969 return xprt; 2970 ret = ERR_PTR(-EINVAL); 2971 out_err: 2972 xs_xprt_free(xprt); 2973 return ret; 2974 } 2975 2976 /** 2977 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 2978 * @args: rpc transport creation arguments 2979 * 2980 */ 2981 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 2982 { 2983 struct sockaddr *addr = args->dstaddr; 2984 struct rpc_xprt *xprt; 2985 struct sock_xprt *transport; 2986 struct svc_sock *bc_sock; 2987 struct rpc_xprt *ret; 2988 2989 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2990 xprt_tcp_slot_table_entries); 2991 if (IS_ERR(xprt)) 2992 return xprt; 2993 transport = container_of(xprt, struct sock_xprt, xprt); 2994 2995 xprt->prot = IPPROTO_TCP; 2996 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2997 xprt->timeout = &xs_tcp_default_timeout; 2998 2999 /* backchannel */ 3000 xprt_set_bound(xprt); 3001 xprt->bind_timeout = 0; 3002 xprt->reestablish_timeout = 0; 3003 xprt->idle_timeout = 0; 3004 3005 xprt->ops = &bc_tcp_ops; 3006 3007 switch (addr->sa_family) { 3008 case AF_INET: 3009 xs_format_peer_addresses(xprt, "tcp", 3010 RPCBIND_NETID_TCP); 3011 break; 3012 case AF_INET6: 3013 xs_format_peer_addresses(xprt, "tcp", 3014 RPCBIND_NETID_TCP6); 3015 break; 3016 default: 3017 ret = ERR_PTR(-EAFNOSUPPORT); 3018 goto out_err; 3019 } 3020 3021 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3022 xprt->address_strings[RPC_DISPLAY_ADDR], 3023 xprt->address_strings[RPC_DISPLAY_PORT], 3024 xprt->address_strings[RPC_DISPLAY_PROTO]); 3025 3026 /* 3027 * Once we've associated a backchannel xprt with a connection, 3028 * we want to keep it around as long as the connection lasts, 3029 * in case we need to start using it for a backchannel again; 3030 * this reference won't be dropped until bc_xprt is destroyed. 3031 */ 3032 xprt_get(xprt); 3033 args->bc_xprt->xpt_bc_xprt = xprt; 3034 xprt->bc_xprt = args->bc_xprt; 3035 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 3036 transport->sock = bc_sock->sk_sock; 3037 transport->inet = bc_sock->sk_sk; 3038 3039 /* 3040 * Since we don't want connections for the backchannel, we set 3041 * the xprt status to connected 3042 */ 3043 xprt_set_connected(xprt); 3044 3045 if (try_module_get(THIS_MODULE)) 3046 return xprt; 3047 3048 args->bc_xprt->xpt_bc_xprt = NULL; 3049 args->bc_xprt->xpt_bc_xps = NULL; 3050 xprt_put(xprt); 3051 ret = ERR_PTR(-EINVAL); 3052 out_err: 3053 xs_xprt_free(xprt); 3054 return ret; 3055 } 3056 3057 static struct xprt_class xs_local_transport = { 3058 .list = LIST_HEAD_INIT(xs_local_transport.list), 3059 .name = "named UNIX socket", 3060 .owner = THIS_MODULE, 3061 .ident = XPRT_TRANSPORT_LOCAL, 3062 .setup = xs_setup_local, 3063 .netid = { "" }, 3064 }; 3065 3066 static struct xprt_class xs_udp_transport = { 3067 .list = LIST_HEAD_INIT(xs_udp_transport.list), 3068 .name = "udp", 3069 .owner = THIS_MODULE, 3070 .ident = XPRT_TRANSPORT_UDP, 3071 .setup = xs_setup_udp, 3072 .netid = { "udp", "udp6", "" }, 3073 }; 3074 3075 static struct xprt_class xs_tcp_transport = { 3076 .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3077 .name = "tcp", 3078 .owner = THIS_MODULE, 3079 .ident = XPRT_TRANSPORT_TCP, 3080 .setup = xs_setup_tcp, 3081 .netid = { "tcp", "tcp6", "" }, 3082 }; 3083 3084 static struct xprt_class xs_bc_tcp_transport = { 3085 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3086 .name = "tcp NFSv4.1 backchannel", 3087 .owner = THIS_MODULE, 3088 .ident = XPRT_TRANSPORT_BC_TCP, 3089 .setup = xs_setup_bc_tcp, 3090 .netid = { "" }, 3091 }; 3092 3093 /** 3094 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3095 * 3096 */ 3097 int init_socket_xprt(void) 3098 { 3099 if (!sunrpc_table_header) 3100 sunrpc_table_header = register_sysctl_table(sunrpc_table); 3101 3102 xprt_register_transport(&xs_local_transport); 3103 xprt_register_transport(&xs_udp_transport); 3104 xprt_register_transport(&xs_tcp_transport); 3105 xprt_register_transport(&xs_bc_tcp_transport); 3106 3107 return 0; 3108 } 3109 3110 /** 3111 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3112 * 3113 */ 3114 void cleanup_socket_xprt(void) 3115 { 3116 if (sunrpc_table_header) { 3117 unregister_sysctl_table(sunrpc_table_header); 3118 sunrpc_table_header = NULL; 3119 } 3120 3121 xprt_unregister_transport(&xs_local_transport); 3122 xprt_unregister_transport(&xs_udp_transport); 3123 xprt_unregister_transport(&xs_tcp_transport); 3124 xprt_unregister_transport(&xs_bc_tcp_transport); 3125 } 3126 3127 static int param_set_uint_minmax(const char *val, 3128 const struct kernel_param *kp, 3129 unsigned int min, unsigned int max) 3130 { 3131 unsigned int num; 3132 int ret; 3133 3134 if (!val) 3135 return -EINVAL; 3136 ret = kstrtouint(val, 0, &num); 3137 if (ret) 3138 return ret; 3139 if (num < min || num > max) 3140 return -EINVAL; 3141 *((unsigned int *)kp->arg) = num; 3142 return 0; 3143 } 3144 3145 static int param_set_portnr(const char *val, const struct kernel_param *kp) 3146 { 3147 return param_set_uint_minmax(val, kp, 3148 RPC_MIN_RESVPORT, 3149 RPC_MAX_RESVPORT); 3150 } 3151 3152 static const struct kernel_param_ops param_ops_portnr = { 3153 .set = param_set_portnr, 3154 .get = param_get_uint, 3155 }; 3156 3157 #define param_check_portnr(name, p) \ 3158 __param_check(name, p, unsigned int); 3159 3160 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3161 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3162 3163 static int param_set_slot_table_size(const char *val, 3164 const struct kernel_param *kp) 3165 { 3166 return param_set_uint_minmax(val, kp, 3167 RPC_MIN_SLOT_TABLE, 3168 RPC_MAX_SLOT_TABLE); 3169 } 3170 3171 static const struct kernel_param_ops param_ops_slot_table_size = { 3172 .set = param_set_slot_table_size, 3173 .get = param_get_uint, 3174 }; 3175 3176 #define param_check_slot_table_size(name, p) \ 3177 __param_check(name, p, unsigned int); 3178 3179 static int param_set_max_slot_table_size(const char *val, 3180 const struct kernel_param *kp) 3181 { 3182 return param_set_uint_minmax(val, kp, 3183 RPC_MIN_SLOT_TABLE, 3184 RPC_MAX_SLOT_TABLE_LIMIT); 3185 } 3186 3187 static const struct kernel_param_ops param_ops_max_slot_table_size = { 3188 .set = param_set_max_slot_table_size, 3189 .get = param_get_uint, 3190 }; 3191 3192 #define param_check_max_slot_table_size(name, p) \ 3193 __param_check(name, p, unsigned int); 3194 3195 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3196 slot_table_size, 0644); 3197 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3198 max_slot_table_size, 0644); 3199 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3200 slot_table_size, 0644); 3201