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