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