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