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