1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/net/sunrpc/xprtsock.c 4 * 5 * Client-side transport implementation for sockets. 6 * 7 * TCP callback races fixes (C) 1998 Red Hat 8 * TCP send fixes (C) 1998 Red Hat 9 * TCP NFS related read + write fixes 10 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 11 * 12 * Rewrite of larges part of the code in order to stabilize TCP stuff. 13 * Fix behaviour when socket buffer is full. 14 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 15 * 16 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 17 * 18 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. 19 * <gilles.quillard@bull.net> 20 */ 21 22 #include <linux/types.h> 23 #include <linux/string.h> 24 #include <linux/slab.h> 25 #include <linux/module.h> 26 #include <linux/capability.h> 27 #include <linux/pagemap.h> 28 #include <linux/errno.h> 29 #include <linux/socket.h> 30 #include <linux/in.h> 31 #include <linux/net.h> 32 #include <linux/mm.h> 33 #include <linux/un.h> 34 #include <linux/udp.h> 35 #include <linux/tcp.h> 36 #include <linux/sunrpc/clnt.h> 37 #include <linux/sunrpc/addr.h> 38 #include <linux/sunrpc/sched.h> 39 #include <linux/sunrpc/svcsock.h> 40 #include <linux/sunrpc/xprtsock.h> 41 #include <linux/file.h> 42 #ifdef CONFIG_SUNRPC_BACKCHANNEL 43 #include <linux/sunrpc/bc_xprt.h> 44 #endif 45 46 #include <net/sock.h> 47 #include <net/checksum.h> 48 #include <net/udp.h> 49 #include <net/tcp.h> 50 #include <linux/bvec.h> 51 #include <linux/uio.h> 52 53 #include <trace/events/sunrpc.h> 54 55 #include "sunrpc.h" 56 57 static void xs_close(struct rpc_xprt *xprt); 58 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 59 struct socket *sock); 60 61 /* 62 * xprtsock tunables 63 */ 64 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; 65 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; 66 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; 67 68 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; 69 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; 70 71 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 72 73 #define XS_TCP_LINGER_TO (15U * HZ) 74 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; 75 76 /* 77 * We can register our own files under /proc/sys/sunrpc by 78 * calling register_sysctl_table() again. The files in that 79 * directory become the union of all files registered there. 80 * 81 * We simply need to make sure that we don't collide with 82 * someone else's file names! 83 */ 84 85 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; 86 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; 87 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; 88 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; 89 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; 90 91 static struct ctl_table_header *sunrpc_table_header; 92 93 /* 94 * FIXME: changing the UDP slot table size should also resize the UDP 95 * socket buffers for existing UDP transports 96 */ 97 static struct ctl_table xs_tunables_table[] = { 98 { 99 .procname = "udp_slot_table_entries", 100 .data = &xprt_udp_slot_table_entries, 101 .maxlen = sizeof(unsigned int), 102 .mode = 0644, 103 .proc_handler = proc_dointvec_minmax, 104 .extra1 = &min_slot_table_size, 105 .extra2 = &max_slot_table_size 106 }, 107 { 108 .procname = "tcp_slot_table_entries", 109 .data = &xprt_tcp_slot_table_entries, 110 .maxlen = sizeof(unsigned int), 111 .mode = 0644, 112 .proc_handler = proc_dointvec_minmax, 113 .extra1 = &min_slot_table_size, 114 .extra2 = &max_slot_table_size 115 }, 116 { 117 .procname = "tcp_max_slot_table_entries", 118 .data = &xprt_max_tcp_slot_table_entries, 119 .maxlen = sizeof(unsigned int), 120 .mode = 0644, 121 .proc_handler = proc_dointvec_minmax, 122 .extra1 = &min_slot_table_size, 123 .extra2 = &max_tcp_slot_table_limit 124 }, 125 { 126 .procname = "min_resvport", 127 .data = &xprt_min_resvport, 128 .maxlen = sizeof(unsigned int), 129 .mode = 0644, 130 .proc_handler = proc_dointvec_minmax, 131 .extra1 = &xprt_min_resvport_limit, 132 .extra2 = &xprt_max_resvport_limit 133 }, 134 { 135 .procname = "max_resvport", 136 .data = &xprt_max_resvport, 137 .maxlen = sizeof(unsigned int), 138 .mode = 0644, 139 .proc_handler = proc_dointvec_minmax, 140 .extra1 = &xprt_min_resvport_limit, 141 .extra2 = &xprt_max_resvport_limit 142 }, 143 { 144 .procname = "tcp_fin_timeout", 145 .data = &xs_tcp_fin_timeout, 146 .maxlen = sizeof(xs_tcp_fin_timeout), 147 .mode = 0644, 148 .proc_handler = proc_dointvec_jiffies, 149 }, 150 { }, 151 }; 152 153 static struct ctl_table sunrpc_table[] = { 154 { 155 .procname = "sunrpc", 156 .mode = 0555, 157 .child = xs_tunables_table 158 }, 159 { }, 160 }; 161 162 #endif 163 164 /* 165 * Wait duration for a reply from the RPC portmapper. 166 */ 167 #define XS_BIND_TO (60U * HZ) 168 169 /* 170 * Delay if a UDP socket connect error occurs. This is most likely some 171 * kind of resource problem on the local host. 172 */ 173 #define XS_UDP_REEST_TO (2U * HZ) 174 175 /* 176 * The reestablish timeout allows clients to delay for a bit before attempting 177 * to reconnect to a server that just dropped our connection. 178 * 179 * We implement an exponential backoff when trying to reestablish a TCP 180 * transport connection with the server. Some servers like to drop a TCP 181 * connection when they are overworked, so we start with a short timeout and 182 * increase over time if the server is down or not responding. 183 */ 184 #define XS_TCP_INIT_REEST_TO (3U * HZ) 185 186 /* 187 * TCP idle timeout; client drops the transport socket if it is idle 188 * for this long. Note that we also timeout UDP sockets to prevent 189 * holding port numbers when there is no RPC traffic. 190 */ 191 #define XS_IDLE_DISC_TO (5U * 60 * HZ) 192 193 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 194 # undef RPC_DEBUG_DATA 195 # define RPCDBG_FACILITY RPCDBG_TRANS 196 #endif 197 198 #ifdef RPC_DEBUG_DATA 199 static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 200 { 201 u8 *buf = (u8 *) packet; 202 int j; 203 204 dprintk("RPC: %s\n", msg); 205 for (j = 0; j < count && j < 128; j += 4) { 206 if (!(j & 31)) { 207 if (j) 208 dprintk("\n"); 209 dprintk("0x%04x ", j); 210 } 211 dprintk("%02x%02x%02x%02x ", 212 buf[j], buf[j+1], buf[j+2], buf[j+3]); 213 } 214 dprintk("\n"); 215 } 216 #else 217 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 218 { 219 /* NOP */ 220 } 221 #endif 222 223 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 224 { 225 return (struct rpc_xprt *) sk->sk_user_data; 226 } 227 228 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) 229 { 230 return (struct sockaddr *) &xprt->addr; 231 } 232 233 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) 234 { 235 return (struct sockaddr_un *) &xprt->addr; 236 } 237 238 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) 239 { 240 return (struct sockaddr_in *) &xprt->addr; 241 } 242 243 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) 244 { 245 return (struct sockaddr_in6 *) &xprt->addr; 246 } 247 248 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) 249 { 250 struct sockaddr *sap = xs_addr(xprt); 251 struct sockaddr_in6 *sin6; 252 struct sockaddr_in *sin; 253 struct sockaddr_un *sun; 254 char buf[128]; 255 256 switch (sap->sa_family) { 257 case AF_LOCAL: 258 sun = xs_addr_un(xprt); 259 strlcpy(buf, sun->sun_path, sizeof(buf)); 260 xprt->address_strings[RPC_DISPLAY_ADDR] = 261 kstrdup(buf, GFP_KERNEL); 262 break; 263 case AF_INET: 264 (void)rpc_ntop(sap, buf, sizeof(buf)); 265 xprt->address_strings[RPC_DISPLAY_ADDR] = 266 kstrdup(buf, GFP_KERNEL); 267 sin = xs_addr_in(xprt); 268 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); 269 break; 270 case AF_INET6: 271 (void)rpc_ntop(sap, buf, sizeof(buf)); 272 xprt->address_strings[RPC_DISPLAY_ADDR] = 273 kstrdup(buf, GFP_KERNEL); 274 sin6 = xs_addr_in6(xprt); 275 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); 276 break; 277 default: 278 BUG(); 279 } 280 281 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); 282 } 283 284 static void xs_format_common_peer_ports(struct rpc_xprt *xprt) 285 { 286 struct sockaddr *sap = xs_addr(xprt); 287 char buf[128]; 288 289 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); 290 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); 291 292 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); 293 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); 294 } 295 296 static void xs_format_peer_addresses(struct rpc_xprt *xprt, 297 const char *protocol, 298 const char *netid) 299 { 300 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; 301 xprt->address_strings[RPC_DISPLAY_NETID] = netid; 302 xs_format_common_peer_addresses(xprt); 303 xs_format_common_peer_ports(xprt); 304 } 305 306 static void xs_update_peer_port(struct rpc_xprt *xprt) 307 { 308 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); 309 kfree(xprt->address_strings[RPC_DISPLAY_PORT]); 310 311 xs_format_common_peer_ports(xprt); 312 } 313 314 static void xs_free_peer_addresses(struct rpc_xprt *xprt) 315 { 316 unsigned int i; 317 318 for (i = 0; i < RPC_DISPLAY_MAX; i++) 319 switch (i) { 320 case RPC_DISPLAY_PROTO: 321 case RPC_DISPLAY_NETID: 322 continue; 323 default: 324 kfree(xprt->address_strings[i]); 325 } 326 } 327 328 static size_t 329 xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) 330 { 331 size_t i,n; 332 333 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) 334 return want; 335 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; 336 for (i = 0; i < n; i++) { 337 if (buf->pages[i]) 338 continue; 339 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); 340 if (!buf->pages[i]) { 341 i *= PAGE_SIZE; 342 return i > buf->page_base ? i - buf->page_base : 0; 343 } 344 } 345 return want; 346 } 347 348 static ssize_t 349 xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) 350 { 351 ssize_t ret; 352 if (seek != 0) 353 iov_iter_advance(&msg->msg_iter, seek); 354 ret = sock_recvmsg(sock, msg, flags); 355 return ret > 0 ? ret + seek : ret; 356 } 357 358 static ssize_t 359 xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, 360 struct kvec *kvec, size_t count, size_t seek) 361 { 362 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count); 363 return xs_sock_recvmsg(sock, msg, flags, seek); 364 } 365 366 static ssize_t 367 xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, 368 struct bio_vec *bvec, unsigned long nr, size_t count, 369 size_t seek) 370 { 371 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count); 372 return xs_sock_recvmsg(sock, msg, flags, seek); 373 } 374 375 static ssize_t 376 xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, 377 size_t count) 378 { 379 struct kvec kvec = { 0 }; 380 return xs_read_kvec(sock, msg, flags | MSG_TRUNC, &kvec, count, 0); 381 } 382 383 static ssize_t 384 xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, 385 struct xdr_buf *buf, size_t count, size_t seek, size_t *read) 386 { 387 size_t want, seek_init = seek, offset = 0; 388 ssize_t ret; 389 390 if (seek < buf->head[0].iov_len) { 391 want = min_t(size_t, count, buf->head[0].iov_len); 392 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); 393 if (ret <= 0) 394 goto sock_err; 395 offset += ret; 396 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 397 goto out; 398 if (ret != want) 399 goto out; 400 seek = 0; 401 } else { 402 seek -= buf->head[0].iov_len; 403 offset += buf->head[0].iov_len; 404 } 405 406 want = xs_alloc_sparse_pages(buf, 407 min_t(size_t, count - offset, buf->page_len), 408 GFP_NOWAIT); 409 if (seek < want) { 410 ret = xs_read_bvec(sock, msg, flags, buf->bvec, 411 xdr_buf_pagecount(buf), 412 want + buf->page_base, 413 seek + buf->page_base); 414 if (ret <= 0) 415 goto sock_err; 416 offset += ret - buf->page_base; 417 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 418 goto out; 419 if (ret != want) 420 goto out; 421 seek = 0; 422 } else { 423 seek -= want; 424 offset += want; 425 } 426 427 if (seek < buf->tail[0].iov_len) { 428 want = min_t(size_t, count - offset, buf->tail[0].iov_len); 429 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); 430 if (ret <= 0) 431 goto sock_err; 432 offset += ret; 433 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 434 goto out; 435 if (ret != want) 436 goto out; 437 } else 438 offset += buf->tail[0].iov_len; 439 ret = -EMSGSIZE; 440 out: 441 *read = offset - seek_init; 442 return ret; 443 sock_err: 444 offset += seek; 445 goto out; 446 } 447 448 static void 449 xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) 450 { 451 if (!transport->recv.copied) { 452 if (buf->head[0].iov_len >= transport->recv.offset) 453 memcpy(buf->head[0].iov_base, 454 &transport->recv.xid, 455 transport->recv.offset); 456 transport->recv.copied = transport->recv.offset; 457 } 458 } 459 460 static bool 461 xs_read_stream_request_done(struct sock_xprt *transport) 462 { 463 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); 464 } 465 466 static ssize_t 467 xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, 468 int flags, struct rpc_rqst *req) 469 { 470 struct xdr_buf *buf = &req->rq_private_buf; 471 size_t want, read; 472 ssize_t ret; 473 474 xs_read_header(transport, buf); 475 476 want = transport->recv.len - transport->recv.offset; 477 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, 478 transport->recv.copied + want, transport->recv.copied, 479 &read); 480 transport->recv.offset += read; 481 transport->recv.copied += read; 482 if (transport->recv.offset == transport->recv.len) { 483 if (xs_read_stream_request_done(transport)) 484 msg->msg_flags |= MSG_EOR; 485 return read; 486 } 487 488 switch (ret) { 489 default: 490 break; 491 case -EFAULT: 492 case -EMSGSIZE: 493 msg->msg_flags |= MSG_TRUNC; 494 return read; 495 case 0: 496 return -ESHUTDOWN; 497 } 498 return ret < 0 ? ret : read; 499 } 500 501 static size_t 502 xs_read_stream_headersize(bool isfrag) 503 { 504 if (isfrag) 505 return sizeof(__be32); 506 return 3 * sizeof(__be32); 507 } 508 509 static ssize_t 510 xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, 511 int flags, size_t want, size_t seek) 512 { 513 struct kvec kvec = { 514 .iov_base = &transport->recv.fraghdr, 515 .iov_len = want, 516 }; 517 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); 518 } 519 520 #if defined(CONFIG_SUNRPC_BACKCHANNEL) 521 static ssize_t 522 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 523 { 524 struct rpc_xprt *xprt = &transport->xprt; 525 struct rpc_rqst *req; 526 ssize_t ret; 527 528 /* Look up and lock the request corresponding to the given XID */ 529 req = xprt_lookup_bc_request(xprt, transport->recv.xid); 530 if (!req) { 531 printk(KERN_WARNING "Callback slot table overflowed\n"); 532 return -ESHUTDOWN; 533 } 534 535 ret = xs_read_stream_request(transport, msg, flags, req); 536 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 537 xprt_complete_bc_request(req, transport->recv.copied); 538 539 return ret; 540 } 541 #else /* CONFIG_SUNRPC_BACKCHANNEL */ 542 static ssize_t 543 xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) 544 { 545 return -ESHUTDOWN; 546 } 547 #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 548 549 static ssize_t 550 xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) 551 { 552 struct rpc_xprt *xprt = &transport->xprt; 553 struct rpc_rqst *req; 554 ssize_t ret = 0; 555 556 /* Look up and lock the request corresponding to the given XID */ 557 spin_lock(&xprt->queue_lock); 558 req = xprt_lookup_rqst(xprt, transport->recv.xid); 559 if (!req) { 560 msg->msg_flags |= MSG_TRUNC; 561 goto out; 562 } 563 xprt_pin_rqst(req); 564 spin_unlock(&xprt->queue_lock); 565 566 ret = xs_read_stream_request(transport, msg, flags, req); 567 568 spin_lock(&xprt->queue_lock); 569 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) 570 xprt_complete_rqst(req->rq_task, transport->recv.copied); 571 xprt_unpin_rqst(req); 572 out: 573 spin_unlock(&xprt->queue_lock); 574 return ret; 575 } 576 577 static ssize_t 578 xs_read_stream(struct sock_xprt *transport, int flags) 579 { 580 struct msghdr msg = { 0 }; 581 size_t want, read = 0; 582 ssize_t ret = 0; 583 584 if (transport->recv.len == 0) { 585 want = xs_read_stream_headersize(transport->recv.copied != 0); 586 ret = xs_read_stream_header(transport, &msg, flags, want, 587 transport->recv.offset); 588 if (ret <= 0) 589 goto out_err; 590 transport->recv.offset = ret; 591 if (transport->recv.offset != want) 592 return transport->recv.offset; 593 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & 594 RPC_FRAGMENT_SIZE_MASK; 595 transport->recv.offset -= sizeof(transport->recv.fraghdr); 596 read = ret; 597 } 598 599 switch (be32_to_cpu(transport->recv.calldir)) { 600 default: 601 msg.msg_flags |= MSG_TRUNC; 602 break; 603 case RPC_CALL: 604 ret = xs_read_stream_call(transport, &msg, flags); 605 break; 606 case RPC_REPLY: 607 ret = xs_read_stream_reply(transport, &msg, flags); 608 } 609 if (msg.msg_flags & MSG_TRUNC) { 610 transport->recv.calldir = cpu_to_be32(-1); 611 transport->recv.copied = -1; 612 } 613 if (ret < 0) 614 goto out_err; 615 read += ret; 616 if (transport->recv.offset < transport->recv.len) { 617 if (!(msg.msg_flags & MSG_TRUNC)) 618 return read; 619 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 read; 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->stat.connect_count++; 1456 xprt->stat.connect_time += (long)jiffies - 1457 xprt->stat.connect_start; 1458 xprt_wake_pending_tasks(xprt, -EAGAIN); 1459 } 1460 spin_unlock(&xprt->transport_lock); 1461 break; 1462 case TCP_FIN_WAIT1: 1463 /* The client initiated a shutdown of the socket */ 1464 xprt->connect_cookie++; 1465 xprt->reestablish_timeout = 0; 1466 set_bit(XPRT_CLOSING, &xprt->state); 1467 smp_mb__before_atomic(); 1468 clear_bit(XPRT_CONNECTED, &xprt->state); 1469 clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 1470 smp_mb__after_atomic(); 1471 break; 1472 case TCP_CLOSE_WAIT: 1473 /* The server initiated a shutdown of the socket */ 1474 xprt->connect_cookie++; 1475 clear_bit(XPRT_CONNECTED, &xprt->state); 1476 xs_tcp_force_close(xprt); 1477 /* fall through */ 1478 case TCP_CLOSING: 1479 /* 1480 * If the server closed down the connection, make sure that 1481 * we back off before reconnecting 1482 */ 1483 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 1484 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 1485 break; 1486 case TCP_LAST_ACK: 1487 set_bit(XPRT_CLOSING, &xprt->state); 1488 smp_mb__before_atomic(); 1489 clear_bit(XPRT_CONNECTED, &xprt->state); 1490 smp_mb__after_atomic(); 1491 break; 1492 case TCP_CLOSE: 1493 if (test_and_clear_bit(XPRT_SOCK_CONNECTING, 1494 &transport->sock_state)) 1495 xprt_clear_connecting(xprt); 1496 clear_bit(XPRT_CLOSING, &xprt->state); 1497 if (sk->sk_err) 1498 xprt_wake_pending_tasks(xprt, -sk->sk_err); 1499 /* Trigger the socket release */ 1500 xs_tcp_force_close(xprt); 1501 } 1502 out: 1503 read_unlock_bh(&sk->sk_callback_lock); 1504 } 1505 1506 static void xs_write_space(struct sock *sk) 1507 { 1508 struct socket_wq *wq; 1509 struct rpc_xprt *xprt; 1510 1511 if (!sk->sk_socket) 1512 return; 1513 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1514 1515 if (unlikely(!(xprt = xprt_from_sock(sk)))) 1516 return; 1517 rcu_read_lock(); 1518 wq = rcu_dereference(sk->sk_wq); 1519 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) 1520 goto out; 1521 1522 if (xprt_write_space(xprt)) 1523 sk->sk_write_pending--; 1524 out: 1525 rcu_read_unlock(); 1526 } 1527 1528 /** 1529 * xs_udp_write_space - callback invoked when socket buffer space 1530 * becomes available 1531 * @sk: socket whose state has changed 1532 * 1533 * Called when more output buffer space is available for this socket. 1534 * We try not to wake our writers until they can make "significant" 1535 * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1536 * with a bunch of small requests. 1537 */ 1538 static void xs_udp_write_space(struct sock *sk) 1539 { 1540 read_lock_bh(&sk->sk_callback_lock); 1541 1542 /* from net/core/sock.c:sock_def_write_space */ 1543 if (sock_writeable(sk)) 1544 xs_write_space(sk); 1545 1546 read_unlock_bh(&sk->sk_callback_lock); 1547 } 1548 1549 /** 1550 * xs_tcp_write_space - callback invoked when socket buffer space 1551 * becomes available 1552 * @sk: socket whose state has changed 1553 * 1554 * Called when more output buffer space is available for this socket. 1555 * We try not to wake our writers until they can make "significant" 1556 * progress, otherwise we'll waste resources thrashing kernel_sendmsg 1557 * with a bunch of small requests. 1558 */ 1559 static void xs_tcp_write_space(struct sock *sk) 1560 { 1561 read_lock_bh(&sk->sk_callback_lock); 1562 1563 /* from net/core/stream.c:sk_stream_write_space */ 1564 if (sk_stream_is_writeable(sk)) 1565 xs_write_space(sk); 1566 1567 read_unlock_bh(&sk->sk_callback_lock); 1568 } 1569 1570 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) 1571 { 1572 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1573 struct sock *sk = transport->inet; 1574 1575 if (transport->rcvsize) { 1576 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 1577 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; 1578 } 1579 if (transport->sndsize) { 1580 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 1581 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; 1582 sk->sk_write_space(sk); 1583 } 1584 } 1585 1586 /** 1587 * xs_udp_set_buffer_size - set send and receive limits 1588 * @xprt: generic transport 1589 * @sndsize: requested size of send buffer, in bytes 1590 * @rcvsize: requested size of receive buffer, in bytes 1591 * 1592 * Set socket send and receive buffer size limits. 1593 */ 1594 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) 1595 { 1596 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1597 1598 transport->sndsize = 0; 1599 if (sndsize) 1600 transport->sndsize = sndsize + 1024; 1601 transport->rcvsize = 0; 1602 if (rcvsize) 1603 transport->rcvsize = rcvsize + 1024; 1604 1605 xs_udp_do_set_buffer_size(xprt); 1606 } 1607 1608 /** 1609 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport 1610 * @task: task that timed out 1611 * 1612 * Adjust the congestion window after a retransmit timeout has occurred. 1613 */ 1614 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) 1615 { 1616 spin_lock_bh(&xprt->transport_lock); 1617 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); 1618 spin_unlock_bh(&xprt->transport_lock); 1619 } 1620 1621 static int xs_get_random_port(void) 1622 { 1623 unsigned short min = xprt_min_resvport, max = xprt_max_resvport; 1624 unsigned short range; 1625 unsigned short rand; 1626 1627 if (max < min) 1628 return -EADDRINUSE; 1629 range = max - min + 1; 1630 rand = (unsigned short) prandom_u32() % range; 1631 return rand + min; 1632 } 1633 1634 /** 1635 * xs_set_reuseaddr_port - set the socket's port and address reuse options 1636 * @sock: socket 1637 * 1638 * Note that this function has to be called on all sockets that share the 1639 * same port, and it must be called before binding. 1640 */ 1641 static void xs_sock_set_reuseport(struct socket *sock) 1642 { 1643 int opt = 1; 1644 1645 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, 1646 (char *)&opt, sizeof(opt)); 1647 } 1648 1649 static unsigned short xs_sock_getport(struct socket *sock) 1650 { 1651 struct sockaddr_storage buf; 1652 unsigned short port = 0; 1653 1654 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) 1655 goto out; 1656 switch (buf.ss_family) { 1657 case AF_INET6: 1658 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); 1659 break; 1660 case AF_INET: 1661 port = ntohs(((struct sockaddr_in *)&buf)->sin_port); 1662 } 1663 out: 1664 return port; 1665 } 1666 1667 /** 1668 * xs_set_port - reset the port number in the remote endpoint address 1669 * @xprt: generic transport 1670 * @port: new port number 1671 * 1672 */ 1673 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) 1674 { 1675 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); 1676 1677 rpc_set_port(xs_addr(xprt), port); 1678 xs_update_peer_port(xprt); 1679 } 1680 1681 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) 1682 { 1683 if (transport->srcport == 0) 1684 transport->srcport = xs_sock_getport(sock); 1685 } 1686 1687 static int xs_get_srcport(struct sock_xprt *transport) 1688 { 1689 int port = transport->srcport; 1690 1691 if (port == 0 && transport->xprt.resvport) 1692 port = xs_get_random_port(); 1693 return port; 1694 } 1695 1696 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) 1697 { 1698 if (transport->srcport != 0) 1699 transport->srcport = 0; 1700 if (!transport->xprt.resvport) 1701 return 0; 1702 if (port <= xprt_min_resvport || port > xprt_max_resvport) 1703 return xprt_max_resvport; 1704 return --port; 1705 } 1706 static int xs_bind(struct sock_xprt *transport, struct socket *sock) 1707 { 1708 struct sockaddr_storage myaddr; 1709 int err, nloop = 0; 1710 int port = xs_get_srcport(transport); 1711 unsigned short last; 1712 1713 /* 1714 * If we are asking for any ephemeral port (i.e. port == 0 && 1715 * transport->xprt.resvport == 0), don't bind. Let the local 1716 * port selection happen implicitly when the socket is used 1717 * (for example at connect time). 1718 * 1719 * This ensures that we can continue to establish TCP 1720 * connections even when all local ephemeral ports are already 1721 * a part of some TCP connection. This makes no difference 1722 * for UDP sockets, but also doens't harm them. 1723 * 1724 * If we're asking for any reserved port (i.e. port == 0 && 1725 * transport->xprt.resvport == 1) xs_get_srcport above will 1726 * ensure that port is non-zero and we will bind as needed. 1727 */ 1728 if (port <= 0) 1729 return port; 1730 1731 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); 1732 do { 1733 rpc_set_port((struct sockaddr *)&myaddr, port); 1734 err = kernel_bind(sock, (struct sockaddr *)&myaddr, 1735 transport->xprt.addrlen); 1736 if (err == 0) { 1737 transport->srcport = port; 1738 break; 1739 } 1740 last = port; 1741 port = xs_next_srcport(transport, port); 1742 if (port > last) 1743 nloop++; 1744 } while (err == -EADDRINUSE && nloop != 2); 1745 1746 if (myaddr.ss_family == AF_INET) 1747 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, 1748 &((struct sockaddr_in *)&myaddr)->sin_addr, 1749 port, err ? "failed" : "ok", err); 1750 else 1751 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, 1752 &((struct sockaddr_in6 *)&myaddr)->sin6_addr, 1753 port, err ? "failed" : "ok", err); 1754 return err; 1755 } 1756 1757 /* 1758 * We don't support autobind on AF_LOCAL sockets 1759 */ 1760 static void xs_local_rpcbind(struct rpc_task *task) 1761 { 1762 xprt_set_bound(task->tk_xprt); 1763 } 1764 1765 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) 1766 { 1767 } 1768 1769 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1770 static struct lock_class_key xs_key[2]; 1771 static struct lock_class_key xs_slock_key[2]; 1772 1773 static inline void xs_reclassify_socketu(struct socket *sock) 1774 { 1775 struct sock *sk = sock->sk; 1776 1777 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", 1778 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); 1779 } 1780 1781 static inline void xs_reclassify_socket4(struct socket *sock) 1782 { 1783 struct sock *sk = sock->sk; 1784 1785 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", 1786 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); 1787 } 1788 1789 static inline void xs_reclassify_socket6(struct socket *sock) 1790 { 1791 struct sock *sk = sock->sk; 1792 1793 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", 1794 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); 1795 } 1796 1797 static inline void xs_reclassify_socket(int family, struct socket *sock) 1798 { 1799 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) 1800 return; 1801 1802 switch (family) { 1803 case AF_LOCAL: 1804 xs_reclassify_socketu(sock); 1805 break; 1806 case AF_INET: 1807 xs_reclassify_socket4(sock); 1808 break; 1809 case AF_INET6: 1810 xs_reclassify_socket6(sock); 1811 break; 1812 } 1813 } 1814 #else 1815 static inline void xs_reclassify_socket(int family, struct socket *sock) 1816 { 1817 } 1818 #endif 1819 1820 static void xs_dummy_setup_socket(struct work_struct *work) 1821 { 1822 } 1823 1824 static struct socket *xs_create_sock(struct rpc_xprt *xprt, 1825 struct sock_xprt *transport, int family, int type, 1826 int protocol, bool reuseport) 1827 { 1828 struct socket *sock; 1829 int err; 1830 1831 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); 1832 if (err < 0) { 1833 dprintk("RPC: can't create %d transport socket (%d).\n", 1834 protocol, -err); 1835 goto out; 1836 } 1837 xs_reclassify_socket(family, sock); 1838 1839 if (reuseport) 1840 xs_sock_set_reuseport(sock); 1841 1842 err = xs_bind(transport, sock); 1843 if (err) { 1844 sock_release(sock); 1845 goto out; 1846 } 1847 1848 return sock; 1849 out: 1850 return ERR_PTR(err); 1851 } 1852 1853 static int xs_local_finish_connecting(struct rpc_xprt *xprt, 1854 struct socket *sock) 1855 { 1856 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1857 xprt); 1858 1859 if (!transport->inet) { 1860 struct sock *sk = sock->sk; 1861 1862 write_lock_bh(&sk->sk_callback_lock); 1863 1864 xs_save_old_callbacks(transport, sk); 1865 1866 sk->sk_user_data = xprt; 1867 sk->sk_data_ready = xs_data_ready; 1868 sk->sk_write_space = xs_udp_write_space; 1869 sock_set_flag(sk, SOCK_FASYNC); 1870 sk->sk_error_report = xs_error_report; 1871 sk->sk_allocation = GFP_NOIO; 1872 1873 xprt_clear_connected(xprt); 1874 1875 /* Reset to new socket */ 1876 transport->sock = sock; 1877 transport->inet = sk; 1878 1879 write_unlock_bh(&sk->sk_callback_lock); 1880 } 1881 1882 xs_stream_reset_connect(transport); 1883 1884 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); 1885 } 1886 1887 /** 1888 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint 1889 * @transport: socket transport to connect 1890 */ 1891 static int xs_local_setup_socket(struct sock_xprt *transport) 1892 { 1893 struct rpc_xprt *xprt = &transport->xprt; 1894 struct socket *sock; 1895 int status = -EIO; 1896 1897 status = __sock_create(xprt->xprt_net, AF_LOCAL, 1898 SOCK_STREAM, 0, &sock, 1); 1899 if (status < 0) { 1900 dprintk("RPC: can't create AF_LOCAL " 1901 "transport socket (%d).\n", -status); 1902 goto out; 1903 } 1904 xs_reclassify_socket(AF_LOCAL, sock); 1905 1906 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", 1907 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1908 1909 status = xs_local_finish_connecting(xprt, sock); 1910 trace_rpc_socket_connect(xprt, sock, status); 1911 switch (status) { 1912 case 0: 1913 dprintk("RPC: xprt %p connected to %s\n", 1914 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1915 xprt->stat.connect_count++; 1916 xprt->stat.connect_time += (long)jiffies - 1917 xprt->stat.connect_start; 1918 xprt_set_connected(xprt); 1919 case -ENOBUFS: 1920 break; 1921 case -ENOENT: 1922 dprintk("RPC: xprt %p: socket %s does not exist\n", 1923 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1924 break; 1925 case -ECONNREFUSED: 1926 dprintk("RPC: xprt %p: connection refused for %s\n", 1927 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); 1928 break; 1929 default: 1930 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", 1931 __func__, -status, 1932 xprt->address_strings[RPC_DISPLAY_ADDR]); 1933 } 1934 1935 out: 1936 xprt_clear_connecting(xprt); 1937 xprt_wake_pending_tasks(xprt, status); 1938 return status; 1939 } 1940 1941 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) 1942 { 1943 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 1944 int ret; 1945 1946 if (RPC_IS_ASYNC(task)) { 1947 /* 1948 * We want the AF_LOCAL connect to be resolved in the 1949 * filesystem namespace of the process making the rpc 1950 * call. Thus we connect synchronously. 1951 * 1952 * If we want to support asynchronous AF_LOCAL calls, 1953 * we'll need to figure out how to pass a namespace to 1954 * connect. 1955 */ 1956 rpc_exit(task, -ENOTCONN); 1957 return; 1958 } 1959 ret = xs_local_setup_socket(transport); 1960 if (ret && !RPC_IS_SOFTCONN(task)) 1961 msleep_interruptible(15000); 1962 } 1963 1964 #if IS_ENABLED(CONFIG_SUNRPC_SWAP) 1965 /* 1966 * Note that this should be called with XPRT_LOCKED held (or when we otherwise 1967 * know that we have exclusive access to the socket), to guard against 1968 * races with xs_reset_transport. 1969 */ 1970 static void xs_set_memalloc(struct rpc_xprt *xprt) 1971 { 1972 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, 1973 xprt); 1974 1975 /* 1976 * If there's no sock, then we have nothing to set. The 1977 * reconnecting process will get it for us. 1978 */ 1979 if (!transport->inet) 1980 return; 1981 if (atomic_read(&xprt->swapper)) 1982 sk_set_memalloc(transport->inet); 1983 } 1984 1985 /** 1986 * xs_enable_swap - Tag this transport as being used for swap. 1987 * @xprt: transport to tag 1988 * 1989 * Take a reference to this transport on behalf of the rpc_clnt, and 1990 * optionally mark it for swapping if it wasn't already. 1991 */ 1992 static int 1993 xs_enable_swap(struct rpc_xprt *xprt) 1994 { 1995 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 1996 1997 if (atomic_inc_return(&xprt->swapper) != 1) 1998 return 0; 1999 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2000 return -ERESTARTSYS; 2001 if (xs->inet) 2002 sk_set_memalloc(xs->inet); 2003 xprt_release_xprt(xprt, NULL); 2004 return 0; 2005 } 2006 2007 /** 2008 * xs_disable_swap - Untag this transport as being used for swap. 2009 * @xprt: transport to tag 2010 * 2011 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the 2012 * swapper refcount goes to 0, untag the socket as a memalloc socket. 2013 */ 2014 static void 2015 xs_disable_swap(struct rpc_xprt *xprt) 2016 { 2017 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); 2018 2019 if (!atomic_dec_and_test(&xprt->swapper)) 2020 return; 2021 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) 2022 return; 2023 if (xs->inet) 2024 sk_clear_memalloc(xs->inet); 2025 xprt_release_xprt(xprt, NULL); 2026 } 2027 #else 2028 static void xs_set_memalloc(struct rpc_xprt *xprt) 2029 { 2030 } 2031 2032 static int 2033 xs_enable_swap(struct rpc_xprt *xprt) 2034 { 2035 return -EINVAL; 2036 } 2037 2038 static void 2039 xs_disable_swap(struct rpc_xprt *xprt) 2040 { 2041 } 2042 #endif 2043 2044 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2045 { 2046 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2047 2048 if (!transport->inet) { 2049 struct sock *sk = sock->sk; 2050 2051 write_lock_bh(&sk->sk_callback_lock); 2052 2053 xs_save_old_callbacks(transport, sk); 2054 2055 sk->sk_user_data = xprt; 2056 sk->sk_data_ready = xs_data_ready; 2057 sk->sk_write_space = xs_udp_write_space; 2058 sock_set_flag(sk, SOCK_FASYNC); 2059 sk->sk_allocation = GFP_NOIO; 2060 2061 xprt_set_connected(xprt); 2062 2063 /* Reset to new socket */ 2064 transport->sock = sock; 2065 transport->inet = sk; 2066 2067 xs_set_memalloc(xprt); 2068 2069 write_unlock_bh(&sk->sk_callback_lock); 2070 } 2071 xs_udp_do_set_buffer_size(xprt); 2072 2073 xprt->stat.connect_start = jiffies; 2074 } 2075 2076 static void xs_udp_setup_socket(struct work_struct *work) 2077 { 2078 struct sock_xprt *transport = 2079 container_of(work, struct sock_xprt, connect_worker.work); 2080 struct rpc_xprt *xprt = &transport->xprt; 2081 struct socket *sock; 2082 int status = -EIO; 2083 2084 sock = xs_create_sock(xprt, transport, 2085 xs_addr(xprt)->sa_family, SOCK_DGRAM, 2086 IPPROTO_UDP, false); 2087 if (IS_ERR(sock)) 2088 goto out; 2089 2090 dprintk("RPC: worker connecting xprt %p via %s to " 2091 "%s (port %s)\n", xprt, 2092 xprt->address_strings[RPC_DISPLAY_PROTO], 2093 xprt->address_strings[RPC_DISPLAY_ADDR], 2094 xprt->address_strings[RPC_DISPLAY_PORT]); 2095 2096 xs_udp_finish_connecting(xprt, sock); 2097 trace_rpc_socket_connect(xprt, sock, 0); 2098 status = 0; 2099 out: 2100 xprt_unlock_connect(xprt, transport); 2101 xprt_clear_connecting(xprt); 2102 xprt_wake_pending_tasks(xprt, status); 2103 } 2104 2105 /** 2106 * xs_tcp_shutdown - gracefully shut down a TCP socket 2107 * @xprt: transport 2108 * 2109 * Initiates a graceful shutdown of the TCP socket by calling the 2110 * equivalent of shutdown(SHUT_RDWR); 2111 */ 2112 static void xs_tcp_shutdown(struct rpc_xprt *xprt) 2113 { 2114 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2115 struct socket *sock = transport->sock; 2116 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; 2117 2118 if (sock == NULL) 2119 return; 2120 switch (skst) { 2121 default: 2122 kernel_sock_shutdown(sock, SHUT_RDWR); 2123 trace_rpc_socket_shutdown(xprt, sock); 2124 break; 2125 case TCP_CLOSE: 2126 case TCP_TIME_WAIT: 2127 xs_reset_transport(transport); 2128 } 2129 } 2130 2131 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, 2132 struct socket *sock) 2133 { 2134 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2135 unsigned int keepidle; 2136 unsigned int keepcnt; 2137 unsigned int opt_on = 1; 2138 unsigned int timeo; 2139 2140 spin_lock_bh(&xprt->transport_lock); 2141 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); 2142 keepcnt = xprt->timeout->to_retries + 1; 2143 timeo = jiffies_to_msecs(xprt->timeout->to_initval) * 2144 (xprt->timeout->to_retries + 1); 2145 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2146 spin_unlock_bh(&xprt->transport_lock); 2147 2148 /* TCP Keepalive options */ 2149 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 2150 (char *)&opt_on, sizeof(opt_on)); 2151 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, 2152 (char *)&keepidle, sizeof(keepidle)); 2153 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, 2154 (char *)&keepidle, sizeof(keepidle)); 2155 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, 2156 (char *)&keepcnt, sizeof(keepcnt)); 2157 2158 /* TCP user timeout (see RFC5482) */ 2159 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, 2160 (char *)&timeo, sizeof(timeo)); 2161 } 2162 2163 static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, 2164 unsigned long connect_timeout, 2165 unsigned long reconnect_timeout) 2166 { 2167 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2168 struct rpc_timeout to; 2169 unsigned long initval; 2170 2171 spin_lock_bh(&xprt->transport_lock); 2172 if (reconnect_timeout < xprt->max_reconnect_timeout) 2173 xprt->max_reconnect_timeout = reconnect_timeout; 2174 if (connect_timeout < xprt->connect_timeout) { 2175 memcpy(&to, xprt->timeout, sizeof(to)); 2176 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); 2177 /* Arbitrary lower limit */ 2178 if (initval < XS_TCP_INIT_REEST_TO << 1) 2179 initval = XS_TCP_INIT_REEST_TO << 1; 2180 to.to_initval = initval; 2181 to.to_maxval = initval; 2182 memcpy(&transport->tcp_timeout, &to, 2183 sizeof(transport->tcp_timeout)); 2184 xprt->timeout = &transport->tcp_timeout; 2185 xprt->connect_timeout = connect_timeout; 2186 } 2187 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); 2188 spin_unlock_bh(&xprt->transport_lock); 2189 } 2190 2191 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) 2192 { 2193 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2194 int ret = -ENOTCONN; 2195 2196 if (!transport->inet) { 2197 struct sock *sk = sock->sk; 2198 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; 2199 2200 /* Avoid temporary address, they are bad for long-lived 2201 * connections such as NFS mounts. 2202 * RFC4941, section 3.6 suggests that: 2203 * Individual applications, which have specific 2204 * knowledge about the normal duration of connections, 2205 * MAY override this as appropriate. 2206 */ 2207 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, 2208 (char *)&addr_pref, sizeof(addr_pref)); 2209 2210 xs_tcp_set_socket_timeouts(xprt, sock); 2211 2212 write_lock_bh(&sk->sk_callback_lock); 2213 2214 xs_save_old_callbacks(transport, sk); 2215 2216 sk->sk_user_data = xprt; 2217 sk->sk_data_ready = xs_data_ready; 2218 sk->sk_state_change = xs_tcp_state_change; 2219 sk->sk_write_space = xs_tcp_write_space; 2220 sock_set_flag(sk, SOCK_FASYNC); 2221 sk->sk_error_report = xs_error_report; 2222 sk->sk_allocation = GFP_NOIO; 2223 2224 /* socket options */ 2225 sock_reset_flag(sk, SOCK_LINGER); 2226 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; 2227 2228 xprt_clear_connected(xprt); 2229 2230 /* Reset to new socket */ 2231 transport->sock = sock; 2232 transport->inet = sk; 2233 2234 write_unlock_bh(&sk->sk_callback_lock); 2235 } 2236 2237 if (!xprt_bound(xprt)) 2238 goto out; 2239 2240 xs_set_memalloc(xprt); 2241 2242 /* Reset TCP record info */ 2243 xs_stream_reset_connect(transport); 2244 2245 /* Tell the socket layer to start connecting... */ 2246 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); 2247 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); 2248 switch (ret) { 2249 case 0: 2250 xs_set_srcport(transport, sock); 2251 /* fall through */ 2252 case -EINPROGRESS: 2253 /* SYN_SENT! */ 2254 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2255 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2256 break; 2257 case -EADDRNOTAVAIL: 2258 /* Source port number is unavailable. Try a new one! */ 2259 transport->srcport = 0; 2260 } 2261 out: 2262 return ret; 2263 } 2264 2265 /** 2266 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint 2267 * 2268 * Invoked by a work queue tasklet. 2269 */ 2270 static void xs_tcp_setup_socket(struct work_struct *work) 2271 { 2272 struct sock_xprt *transport = 2273 container_of(work, struct sock_xprt, connect_worker.work); 2274 struct socket *sock = transport->sock; 2275 struct rpc_xprt *xprt = &transport->xprt; 2276 int status = -EIO; 2277 2278 if (!sock) { 2279 sock = xs_create_sock(xprt, transport, 2280 xs_addr(xprt)->sa_family, SOCK_STREAM, 2281 IPPROTO_TCP, true); 2282 if (IS_ERR(sock)) { 2283 status = PTR_ERR(sock); 2284 goto out; 2285 } 2286 } 2287 2288 dprintk("RPC: worker connecting xprt %p via %s to " 2289 "%s (port %s)\n", xprt, 2290 xprt->address_strings[RPC_DISPLAY_PROTO], 2291 xprt->address_strings[RPC_DISPLAY_ADDR], 2292 xprt->address_strings[RPC_DISPLAY_PORT]); 2293 2294 status = xs_tcp_finish_connecting(xprt, sock); 2295 trace_rpc_socket_connect(xprt, sock, status); 2296 dprintk("RPC: %p connect status %d connected %d sock state %d\n", 2297 xprt, -status, xprt_connected(xprt), 2298 sock->sk->sk_state); 2299 switch (status) { 2300 default: 2301 printk("%s: connect returned unhandled error %d\n", 2302 __func__, status); 2303 /* fall through */ 2304 case -EADDRNOTAVAIL: 2305 /* We're probably in TIME_WAIT. Get rid of existing socket, 2306 * and retry 2307 */ 2308 xs_tcp_force_close(xprt); 2309 break; 2310 case 0: 2311 case -EINPROGRESS: 2312 case -EALREADY: 2313 xprt_unlock_connect(xprt, transport); 2314 return; 2315 case -EINVAL: 2316 /* Happens, for instance, if the user specified a link 2317 * local IPv6 address without a scope-id. 2318 */ 2319 case -ECONNREFUSED: 2320 case -ECONNRESET: 2321 case -ENETDOWN: 2322 case -ENETUNREACH: 2323 case -EHOSTUNREACH: 2324 case -EADDRINUSE: 2325 case -ENOBUFS: 2326 /* 2327 * xs_tcp_force_close() wakes tasks with -EIO. 2328 * We need to wake them first to ensure the 2329 * correct error code. 2330 */ 2331 xprt_wake_pending_tasks(xprt, status); 2332 xs_tcp_force_close(xprt); 2333 goto out; 2334 } 2335 status = -EAGAIN; 2336 out: 2337 xprt_unlock_connect(xprt, transport); 2338 xprt_clear_connecting(xprt); 2339 xprt_wake_pending_tasks(xprt, status); 2340 } 2341 2342 static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt) 2343 { 2344 unsigned long start, now = jiffies; 2345 2346 start = xprt->stat.connect_start + xprt->reestablish_timeout; 2347 if (time_after(start, now)) 2348 return start - now; 2349 return 0; 2350 } 2351 2352 static void xs_reconnect_backoff(struct rpc_xprt *xprt) 2353 { 2354 xprt->reestablish_timeout <<= 1; 2355 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) 2356 xprt->reestablish_timeout = xprt->max_reconnect_timeout; 2357 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2358 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2359 } 2360 2361 /** 2362 * xs_connect - connect a socket to a remote endpoint 2363 * @xprt: pointer to transport structure 2364 * @task: address of RPC task that manages state of connect request 2365 * 2366 * TCP: If the remote end dropped the connection, delay reconnecting. 2367 * 2368 * UDP socket connects are synchronous, but we use a work queue anyway 2369 * to guarantee that even unprivileged user processes can set up a 2370 * socket on a privileged port. 2371 * 2372 * If a UDP socket connect fails, the delay behavior here prevents 2373 * retry floods (hard mounts). 2374 */ 2375 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) 2376 { 2377 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2378 unsigned long delay = 0; 2379 2380 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); 2381 2382 if (transport->sock != NULL) { 2383 dprintk("RPC: xs_connect delayed xprt %p for %lu " 2384 "seconds\n", 2385 xprt, xprt->reestablish_timeout / HZ); 2386 2387 /* Start by resetting any existing state */ 2388 xs_reset_transport(transport); 2389 2390 delay = xs_reconnect_delay(xprt); 2391 xs_reconnect_backoff(xprt); 2392 2393 } else 2394 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 2395 2396 queue_delayed_work(xprtiod_workqueue, 2397 &transport->connect_worker, 2398 delay); 2399 } 2400 2401 /** 2402 * xs_local_print_stats - display AF_LOCAL socket-specifc stats 2403 * @xprt: rpc_xprt struct containing statistics 2404 * @seq: output file 2405 * 2406 */ 2407 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2408 { 2409 long idle_time = 0; 2410 2411 if (xprt_connected(xprt)) 2412 idle_time = (long)(jiffies - xprt->last_used) / HZ; 2413 2414 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " 2415 "%llu %llu %lu %llu %llu\n", 2416 xprt->stat.bind_count, 2417 xprt->stat.connect_count, 2418 xprt->stat.connect_time / HZ, 2419 idle_time, 2420 xprt->stat.sends, 2421 xprt->stat.recvs, 2422 xprt->stat.bad_xids, 2423 xprt->stat.req_u, 2424 xprt->stat.bklog_u, 2425 xprt->stat.max_slots, 2426 xprt->stat.sending_u, 2427 xprt->stat.pending_u); 2428 } 2429 2430 /** 2431 * xs_udp_print_stats - display UDP socket-specifc stats 2432 * @xprt: rpc_xprt struct containing statistics 2433 * @seq: output file 2434 * 2435 */ 2436 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2437 { 2438 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2439 2440 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " 2441 "%lu %llu %llu\n", 2442 transport->srcport, 2443 xprt->stat.bind_count, 2444 xprt->stat.sends, 2445 xprt->stat.recvs, 2446 xprt->stat.bad_xids, 2447 xprt->stat.req_u, 2448 xprt->stat.bklog_u, 2449 xprt->stat.max_slots, 2450 xprt->stat.sending_u, 2451 xprt->stat.pending_u); 2452 } 2453 2454 /** 2455 * xs_tcp_print_stats - display TCP socket-specifc stats 2456 * @xprt: rpc_xprt struct containing statistics 2457 * @seq: output file 2458 * 2459 */ 2460 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) 2461 { 2462 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 2463 long idle_time = 0; 2464 2465 if (xprt_connected(xprt)) 2466 idle_time = (long)(jiffies - xprt->last_used) / HZ; 2467 2468 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " 2469 "%llu %llu %lu %llu %llu\n", 2470 transport->srcport, 2471 xprt->stat.bind_count, 2472 xprt->stat.connect_count, 2473 xprt->stat.connect_time / HZ, 2474 idle_time, 2475 xprt->stat.sends, 2476 xprt->stat.recvs, 2477 xprt->stat.bad_xids, 2478 xprt->stat.req_u, 2479 xprt->stat.bklog_u, 2480 xprt->stat.max_slots, 2481 xprt->stat.sending_u, 2482 xprt->stat.pending_u); 2483 } 2484 2485 /* 2486 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason 2487 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want 2488 * to use the server side send routines. 2489 */ 2490 static int bc_malloc(struct rpc_task *task) 2491 { 2492 struct rpc_rqst *rqst = task->tk_rqstp; 2493 size_t size = rqst->rq_callsize; 2494 struct page *page; 2495 struct rpc_buffer *buf; 2496 2497 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { 2498 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", 2499 size); 2500 return -EINVAL; 2501 } 2502 2503 page = alloc_page(GFP_KERNEL); 2504 if (!page) 2505 return -ENOMEM; 2506 2507 buf = page_address(page); 2508 buf->len = PAGE_SIZE; 2509 2510 rqst->rq_buffer = buf->data; 2511 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 2512 return 0; 2513 } 2514 2515 /* 2516 * Free the space allocated in the bc_alloc routine 2517 */ 2518 static void bc_free(struct rpc_task *task) 2519 { 2520 void *buffer = task->tk_rqstp->rq_buffer; 2521 struct rpc_buffer *buf; 2522 2523 buf = container_of(buffer, struct rpc_buffer, data); 2524 free_page((unsigned long)buf); 2525 } 2526 2527 /* 2528 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex 2529 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. 2530 */ 2531 static int bc_sendto(struct rpc_rqst *req) 2532 { 2533 int len; 2534 struct xdr_buf *xbufp = &req->rq_snd_buf; 2535 struct rpc_xprt *xprt = req->rq_xprt; 2536 struct sock_xprt *transport = 2537 container_of(xprt, struct sock_xprt, xprt); 2538 struct socket *sock = transport->sock; 2539 unsigned long headoff; 2540 unsigned long tailoff; 2541 2542 xs_encode_stream_record_marker(xbufp); 2543 2544 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; 2545 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; 2546 len = svc_send_common(sock, xbufp, 2547 virt_to_page(xbufp->head[0].iov_base), headoff, 2548 xbufp->tail[0].iov_base, tailoff); 2549 2550 if (len != xbufp->len) { 2551 printk(KERN_NOTICE "Error sending entire callback!\n"); 2552 len = -EAGAIN; 2553 } 2554 2555 return len; 2556 } 2557 2558 /* 2559 * The send routine. Borrows from svc_send 2560 */ 2561 static int bc_send_request(struct rpc_rqst *req) 2562 { 2563 struct svc_xprt *xprt; 2564 int len; 2565 2566 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); 2567 /* 2568 * Get the server socket associated with this callback xprt 2569 */ 2570 xprt = req->rq_xprt->bc_xprt; 2571 2572 /* 2573 * Grab the mutex to serialize data as the connection is shared 2574 * with the fore channel 2575 */ 2576 mutex_lock(&xprt->xpt_mutex); 2577 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) 2578 len = -ENOTCONN; 2579 else 2580 len = bc_sendto(req); 2581 mutex_unlock(&xprt->xpt_mutex); 2582 2583 if (len > 0) 2584 len = 0; 2585 2586 return len; 2587 } 2588 2589 /* 2590 * The close routine. Since this is client initiated, we do nothing 2591 */ 2592 2593 static void bc_close(struct rpc_xprt *xprt) 2594 { 2595 } 2596 2597 /* 2598 * The xprt destroy routine. Again, because this connection is client 2599 * initiated, we do nothing 2600 */ 2601 2602 static void bc_destroy(struct rpc_xprt *xprt) 2603 { 2604 dprintk("RPC: bc_destroy xprt %p\n", xprt); 2605 2606 xs_xprt_free(xprt); 2607 module_put(THIS_MODULE); 2608 } 2609 2610 static const struct rpc_xprt_ops xs_local_ops = { 2611 .reserve_xprt = xprt_reserve_xprt, 2612 .release_xprt = xprt_release_xprt, 2613 .alloc_slot = xprt_alloc_slot, 2614 .free_slot = xprt_free_slot, 2615 .rpcbind = xs_local_rpcbind, 2616 .set_port = xs_local_set_port, 2617 .connect = xs_local_connect, 2618 .buf_alloc = rpc_malloc, 2619 .buf_free = rpc_free, 2620 .prepare_request = xs_stream_prepare_request, 2621 .send_request = xs_local_send_request, 2622 .set_retrans_timeout = xprt_set_retrans_timeout_def, 2623 .close = xs_close, 2624 .destroy = xs_destroy, 2625 .print_stats = xs_local_print_stats, 2626 .enable_swap = xs_enable_swap, 2627 .disable_swap = xs_disable_swap, 2628 }; 2629 2630 static const struct rpc_xprt_ops xs_udp_ops = { 2631 .set_buffer_size = xs_udp_set_buffer_size, 2632 .reserve_xprt = xprt_reserve_xprt_cong, 2633 .release_xprt = xprt_release_xprt_cong, 2634 .alloc_slot = xprt_alloc_slot, 2635 .free_slot = xprt_free_slot, 2636 .rpcbind = rpcb_getport_async, 2637 .set_port = xs_set_port, 2638 .connect = xs_connect, 2639 .buf_alloc = rpc_malloc, 2640 .buf_free = rpc_free, 2641 .send_request = xs_udp_send_request, 2642 .set_retrans_timeout = xprt_set_retrans_timeout_rtt, 2643 .timer = xs_udp_timer, 2644 .release_request = xprt_release_rqst_cong, 2645 .close = xs_close, 2646 .destroy = xs_destroy, 2647 .print_stats = xs_udp_print_stats, 2648 .enable_swap = xs_enable_swap, 2649 .disable_swap = xs_disable_swap, 2650 .inject_disconnect = xs_inject_disconnect, 2651 }; 2652 2653 static const struct rpc_xprt_ops xs_tcp_ops = { 2654 .reserve_xprt = xprt_reserve_xprt, 2655 .release_xprt = xprt_release_xprt, 2656 .alloc_slot = xprt_alloc_slot, 2657 .free_slot = xprt_free_slot, 2658 .rpcbind = rpcb_getport_async, 2659 .set_port = xs_set_port, 2660 .connect = xs_connect, 2661 .buf_alloc = rpc_malloc, 2662 .buf_free = rpc_free, 2663 .prepare_request = xs_stream_prepare_request, 2664 .send_request = xs_tcp_send_request, 2665 .set_retrans_timeout = xprt_set_retrans_timeout_def, 2666 .close = xs_tcp_shutdown, 2667 .destroy = xs_destroy, 2668 .set_connect_timeout = xs_tcp_set_connect_timeout, 2669 .print_stats = xs_tcp_print_stats, 2670 .enable_swap = xs_enable_swap, 2671 .disable_swap = xs_disable_swap, 2672 .inject_disconnect = xs_inject_disconnect, 2673 #ifdef CONFIG_SUNRPC_BACKCHANNEL 2674 .bc_setup = xprt_setup_bc, 2675 .bc_up = xs_tcp_bc_up, 2676 .bc_maxpayload = xs_tcp_bc_maxpayload, 2677 .bc_free_rqst = xprt_free_bc_rqst, 2678 .bc_destroy = xprt_destroy_bc, 2679 #endif 2680 }; 2681 2682 /* 2683 * The rpc_xprt_ops for the server backchannel 2684 */ 2685 2686 static const struct rpc_xprt_ops bc_tcp_ops = { 2687 .reserve_xprt = xprt_reserve_xprt, 2688 .release_xprt = xprt_release_xprt, 2689 .alloc_slot = xprt_alloc_slot, 2690 .free_slot = xprt_free_slot, 2691 .buf_alloc = bc_malloc, 2692 .buf_free = bc_free, 2693 .send_request = bc_send_request, 2694 .set_retrans_timeout = xprt_set_retrans_timeout_def, 2695 .close = bc_close, 2696 .destroy = bc_destroy, 2697 .print_stats = xs_tcp_print_stats, 2698 .enable_swap = xs_enable_swap, 2699 .disable_swap = xs_disable_swap, 2700 .inject_disconnect = xs_inject_disconnect, 2701 }; 2702 2703 static int xs_init_anyaddr(const int family, struct sockaddr *sap) 2704 { 2705 static const struct sockaddr_in sin = { 2706 .sin_family = AF_INET, 2707 .sin_addr.s_addr = htonl(INADDR_ANY), 2708 }; 2709 static const struct sockaddr_in6 sin6 = { 2710 .sin6_family = AF_INET6, 2711 .sin6_addr = IN6ADDR_ANY_INIT, 2712 }; 2713 2714 switch (family) { 2715 case AF_LOCAL: 2716 break; 2717 case AF_INET: 2718 memcpy(sap, &sin, sizeof(sin)); 2719 break; 2720 case AF_INET6: 2721 memcpy(sap, &sin6, sizeof(sin6)); 2722 break; 2723 default: 2724 dprintk("RPC: %s: Bad address family\n", __func__); 2725 return -EAFNOSUPPORT; 2726 } 2727 return 0; 2728 } 2729 2730 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, 2731 unsigned int slot_table_size, 2732 unsigned int max_slot_table_size) 2733 { 2734 struct rpc_xprt *xprt; 2735 struct sock_xprt *new; 2736 2737 if (args->addrlen > sizeof(xprt->addr)) { 2738 dprintk("RPC: xs_setup_xprt: address too large\n"); 2739 return ERR_PTR(-EBADF); 2740 } 2741 2742 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, 2743 max_slot_table_size); 2744 if (xprt == NULL) { 2745 dprintk("RPC: xs_setup_xprt: couldn't allocate " 2746 "rpc_xprt\n"); 2747 return ERR_PTR(-ENOMEM); 2748 } 2749 2750 new = container_of(xprt, struct sock_xprt, xprt); 2751 mutex_init(&new->recv_mutex); 2752 memcpy(&xprt->addr, args->dstaddr, args->addrlen); 2753 xprt->addrlen = args->addrlen; 2754 if (args->srcaddr) 2755 memcpy(&new->srcaddr, args->srcaddr, args->addrlen); 2756 else { 2757 int err; 2758 err = xs_init_anyaddr(args->dstaddr->sa_family, 2759 (struct sockaddr *)&new->srcaddr); 2760 if (err != 0) { 2761 xprt_free(xprt); 2762 return ERR_PTR(err); 2763 } 2764 } 2765 2766 return xprt; 2767 } 2768 2769 static const struct rpc_timeout xs_local_default_timeout = { 2770 .to_initval = 10 * HZ, 2771 .to_maxval = 10 * HZ, 2772 .to_retries = 2, 2773 }; 2774 2775 /** 2776 * xs_setup_local - Set up transport to use an AF_LOCAL socket 2777 * @args: rpc transport creation arguments 2778 * 2779 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP 2780 */ 2781 static struct rpc_xprt *xs_setup_local(struct xprt_create *args) 2782 { 2783 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; 2784 struct sock_xprt *transport; 2785 struct rpc_xprt *xprt; 2786 struct rpc_xprt *ret; 2787 2788 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2789 xprt_max_tcp_slot_table_entries); 2790 if (IS_ERR(xprt)) 2791 return xprt; 2792 transport = container_of(xprt, struct sock_xprt, xprt); 2793 2794 xprt->prot = 0; 2795 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2796 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2797 2798 xprt->bind_timeout = XS_BIND_TO; 2799 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2800 xprt->idle_timeout = XS_IDLE_DISC_TO; 2801 2802 xprt->ops = &xs_local_ops; 2803 xprt->timeout = &xs_local_default_timeout; 2804 2805 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 2806 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); 2807 2808 switch (sun->sun_family) { 2809 case AF_LOCAL: 2810 if (sun->sun_path[0] != '/') { 2811 dprintk("RPC: bad AF_LOCAL address: %s\n", 2812 sun->sun_path); 2813 ret = ERR_PTR(-EINVAL); 2814 goto out_err; 2815 } 2816 xprt_set_bound(xprt); 2817 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); 2818 ret = ERR_PTR(xs_local_setup_socket(transport)); 2819 if (ret) 2820 goto out_err; 2821 break; 2822 default: 2823 ret = ERR_PTR(-EAFNOSUPPORT); 2824 goto out_err; 2825 } 2826 2827 dprintk("RPC: set up xprt to %s via AF_LOCAL\n", 2828 xprt->address_strings[RPC_DISPLAY_ADDR]); 2829 2830 if (try_module_get(THIS_MODULE)) 2831 return xprt; 2832 ret = ERR_PTR(-EINVAL); 2833 out_err: 2834 xs_xprt_free(xprt); 2835 return ret; 2836 } 2837 2838 static const struct rpc_timeout xs_udp_default_timeout = { 2839 .to_initval = 5 * HZ, 2840 .to_maxval = 30 * HZ, 2841 .to_increment = 5 * HZ, 2842 .to_retries = 5, 2843 }; 2844 2845 /** 2846 * xs_setup_udp - Set up transport to use a UDP socket 2847 * @args: rpc transport creation arguments 2848 * 2849 */ 2850 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) 2851 { 2852 struct sockaddr *addr = args->dstaddr; 2853 struct rpc_xprt *xprt; 2854 struct sock_xprt *transport; 2855 struct rpc_xprt *ret; 2856 2857 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, 2858 xprt_udp_slot_table_entries); 2859 if (IS_ERR(xprt)) 2860 return xprt; 2861 transport = container_of(xprt, struct sock_xprt, xprt); 2862 2863 xprt->prot = IPPROTO_UDP; 2864 xprt->tsh_size = 0; 2865 /* XXX: header size can vary due to auth type, IPv6, etc. */ 2866 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 2867 2868 xprt->bind_timeout = XS_BIND_TO; 2869 xprt->reestablish_timeout = XS_UDP_REEST_TO; 2870 xprt->idle_timeout = XS_IDLE_DISC_TO; 2871 2872 xprt->ops = &xs_udp_ops; 2873 2874 xprt->timeout = &xs_udp_default_timeout; 2875 2876 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); 2877 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); 2878 2879 switch (addr->sa_family) { 2880 case AF_INET: 2881 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 2882 xprt_set_bound(xprt); 2883 2884 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); 2885 break; 2886 case AF_INET6: 2887 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 2888 xprt_set_bound(xprt); 2889 2890 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); 2891 break; 2892 default: 2893 ret = ERR_PTR(-EAFNOSUPPORT); 2894 goto out_err; 2895 } 2896 2897 if (xprt_bound(xprt)) 2898 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2899 xprt->address_strings[RPC_DISPLAY_ADDR], 2900 xprt->address_strings[RPC_DISPLAY_PORT], 2901 xprt->address_strings[RPC_DISPLAY_PROTO]); 2902 else 2903 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2904 xprt->address_strings[RPC_DISPLAY_ADDR], 2905 xprt->address_strings[RPC_DISPLAY_PROTO]); 2906 2907 if (try_module_get(THIS_MODULE)) 2908 return xprt; 2909 ret = ERR_PTR(-EINVAL); 2910 out_err: 2911 xs_xprt_free(xprt); 2912 return ret; 2913 } 2914 2915 static const struct rpc_timeout xs_tcp_default_timeout = { 2916 .to_initval = 60 * HZ, 2917 .to_maxval = 60 * HZ, 2918 .to_retries = 2, 2919 }; 2920 2921 /** 2922 * xs_setup_tcp - Set up transport to use a TCP socket 2923 * @args: rpc transport creation arguments 2924 * 2925 */ 2926 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) 2927 { 2928 struct sockaddr *addr = args->dstaddr; 2929 struct rpc_xprt *xprt; 2930 struct sock_xprt *transport; 2931 struct rpc_xprt *ret; 2932 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; 2933 2934 if (args->flags & XPRT_CREATE_INFINITE_SLOTS) 2935 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; 2936 2937 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 2938 max_slot_table_size); 2939 if (IS_ERR(xprt)) 2940 return xprt; 2941 transport = container_of(xprt, struct sock_xprt, xprt); 2942 2943 xprt->prot = IPPROTO_TCP; 2944 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 2945 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 2946 2947 xprt->bind_timeout = XS_BIND_TO; 2948 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2949 xprt->idle_timeout = XS_IDLE_DISC_TO; 2950 2951 xprt->ops = &xs_tcp_ops; 2952 xprt->timeout = &xs_tcp_default_timeout; 2953 2954 xprt->max_reconnect_timeout = xprt->timeout->to_maxval; 2955 xprt->connect_timeout = xprt->timeout->to_initval * 2956 (xprt->timeout->to_retries + 1); 2957 2958 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); 2959 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); 2960 2961 switch (addr->sa_family) { 2962 case AF_INET: 2963 if (((struct sockaddr_in *)addr)->sin_port != htons(0)) 2964 xprt_set_bound(xprt); 2965 2966 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); 2967 break; 2968 case AF_INET6: 2969 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) 2970 xprt_set_bound(xprt); 2971 2972 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); 2973 break; 2974 default: 2975 ret = ERR_PTR(-EAFNOSUPPORT); 2976 goto out_err; 2977 } 2978 2979 if (xprt_bound(xprt)) 2980 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 2981 xprt->address_strings[RPC_DISPLAY_ADDR], 2982 xprt->address_strings[RPC_DISPLAY_PORT], 2983 xprt->address_strings[RPC_DISPLAY_PROTO]); 2984 else 2985 dprintk("RPC: set up xprt to %s (autobind) via %s\n", 2986 xprt->address_strings[RPC_DISPLAY_ADDR], 2987 xprt->address_strings[RPC_DISPLAY_PROTO]); 2988 2989 if (try_module_get(THIS_MODULE)) 2990 return xprt; 2991 ret = ERR_PTR(-EINVAL); 2992 out_err: 2993 xs_xprt_free(xprt); 2994 return ret; 2995 } 2996 2997 /** 2998 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket 2999 * @args: rpc transport creation arguments 3000 * 3001 */ 3002 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) 3003 { 3004 struct sockaddr *addr = args->dstaddr; 3005 struct rpc_xprt *xprt; 3006 struct sock_xprt *transport; 3007 struct svc_sock *bc_sock; 3008 struct rpc_xprt *ret; 3009 3010 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, 3011 xprt_tcp_slot_table_entries); 3012 if (IS_ERR(xprt)) 3013 return xprt; 3014 transport = container_of(xprt, struct sock_xprt, xprt); 3015 3016 xprt->prot = IPPROTO_TCP; 3017 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); 3018 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; 3019 xprt->timeout = &xs_tcp_default_timeout; 3020 3021 /* backchannel */ 3022 xprt_set_bound(xprt); 3023 xprt->bind_timeout = 0; 3024 xprt->reestablish_timeout = 0; 3025 xprt->idle_timeout = 0; 3026 3027 xprt->ops = &bc_tcp_ops; 3028 3029 switch (addr->sa_family) { 3030 case AF_INET: 3031 xs_format_peer_addresses(xprt, "tcp", 3032 RPCBIND_NETID_TCP); 3033 break; 3034 case AF_INET6: 3035 xs_format_peer_addresses(xprt, "tcp", 3036 RPCBIND_NETID_TCP6); 3037 break; 3038 default: 3039 ret = ERR_PTR(-EAFNOSUPPORT); 3040 goto out_err; 3041 } 3042 3043 dprintk("RPC: set up xprt to %s (port %s) via %s\n", 3044 xprt->address_strings[RPC_DISPLAY_ADDR], 3045 xprt->address_strings[RPC_DISPLAY_PORT], 3046 xprt->address_strings[RPC_DISPLAY_PROTO]); 3047 3048 /* 3049 * Once we've associated a backchannel xprt with a connection, 3050 * we want to keep it around as long as the connection lasts, 3051 * in case we need to start using it for a backchannel again; 3052 * this reference won't be dropped until bc_xprt is destroyed. 3053 */ 3054 xprt_get(xprt); 3055 args->bc_xprt->xpt_bc_xprt = xprt; 3056 xprt->bc_xprt = args->bc_xprt; 3057 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); 3058 transport->sock = bc_sock->sk_sock; 3059 transport->inet = bc_sock->sk_sk; 3060 3061 /* 3062 * Since we don't want connections for the backchannel, we set 3063 * the xprt status to connected 3064 */ 3065 xprt_set_connected(xprt); 3066 3067 if (try_module_get(THIS_MODULE)) 3068 return xprt; 3069 3070 args->bc_xprt->xpt_bc_xprt = NULL; 3071 args->bc_xprt->xpt_bc_xps = NULL; 3072 xprt_put(xprt); 3073 ret = ERR_PTR(-EINVAL); 3074 out_err: 3075 xs_xprt_free(xprt); 3076 return ret; 3077 } 3078 3079 static struct xprt_class xs_local_transport = { 3080 .list = LIST_HEAD_INIT(xs_local_transport.list), 3081 .name = "named UNIX socket", 3082 .owner = THIS_MODULE, 3083 .ident = XPRT_TRANSPORT_LOCAL, 3084 .setup = xs_setup_local, 3085 }; 3086 3087 static struct xprt_class xs_udp_transport = { 3088 .list = LIST_HEAD_INIT(xs_udp_transport.list), 3089 .name = "udp", 3090 .owner = THIS_MODULE, 3091 .ident = XPRT_TRANSPORT_UDP, 3092 .setup = xs_setup_udp, 3093 }; 3094 3095 static struct xprt_class xs_tcp_transport = { 3096 .list = LIST_HEAD_INIT(xs_tcp_transport.list), 3097 .name = "tcp", 3098 .owner = THIS_MODULE, 3099 .ident = XPRT_TRANSPORT_TCP, 3100 .setup = xs_setup_tcp, 3101 }; 3102 3103 static struct xprt_class xs_bc_tcp_transport = { 3104 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), 3105 .name = "tcp NFSv4.1 backchannel", 3106 .owner = THIS_MODULE, 3107 .ident = XPRT_TRANSPORT_BC_TCP, 3108 .setup = xs_setup_bc_tcp, 3109 }; 3110 3111 /** 3112 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client 3113 * 3114 */ 3115 int init_socket_xprt(void) 3116 { 3117 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 3118 if (!sunrpc_table_header) 3119 sunrpc_table_header = register_sysctl_table(sunrpc_table); 3120 #endif 3121 3122 xprt_register_transport(&xs_local_transport); 3123 xprt_register_transport(&xs_udp_transport); 3124 xprt_register_transport(&xs_tcp_transport); 3125 xprt_register_transport(&xs_bc_tcp_transport); 3126 3127 return 0; 3128 } 3129 3130 /** 3131 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister 3132 * 3133 */ 3134 void cleanup_socket_xprt(void) 3135 { 3136 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 3137 if (sunrpc_table_header) { 3138 unregister_sysctl_table(sunrpc_table_header); 3139 sunrpc_table_header = NULL; 3140 } 3141 #endif 3142 3143 xprt_unregister_transport(&xs_local_transport); 3144 xprt_unregister_transport(&xs_udp_transport); 3145 xprt_unregister_transport(&xs_tcp_transport); 3146 xprt_unregister_transport(&xs_bc_tcp_transport); 3147 } 3148 3149 static int param_set_uint_minmax(const char *val, 3150 const struct kernel_param *kp, 3151 unsigned int min, unsigned int max) 3152 { 3153 unsigned int num; 3154 int ret; 3155 3156 if (!val) 3157 return -EINVAL; 3158 ret = kstrtouint(val, 0, &num); 3159 if (ret) 3160 return ret; 3161 if (num < min || num > max) 3162 return -EINVAL; 3163 *((unsigned int *)kp->arg) = num; 3164 return 0; 3165 } 3166 3167 static int param_set_portnr(const char *val, const struct kernel_param *kp) 3168 { 3169 return param_set_uint_minmax(val, kp, 3170 RPC_MIN_RESVPORT, 3171 RPC_MAX_RESVPORT); 3172 } 3173 3174 static const struct kernel_param_ops param_ops_portnr = { 3175 .set = param_set_portnr, 3176 .get = param_get_uint, 3177 }; 3178 3179 #define param_check_portnr(name, p) \ 3180 __param_check(name, p, unsigned int); 3181 3182 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); 3183 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); 3184 3185 static int param_set_slot_table_size(const char *val, 3186 const struct kernel_param *kp) 3187 { 3188 return param_set_uint_minmax(val, kp, 3189 RPC_MIN_SLOT_TABLE, 3190 RPC_MAX_SLOT_TABLE); 3191 } 3192 3193 static const struct kernel_param_ops param_ops_slot_table_size = { 3194 .set = param_set_slot_table_size, 3195 .get = param_get_uint, 3196 }; 3197 3198 #define param_check_slot_table_size(name, p) \ 3199 __param_check(name, p, unsigned int); 3200 3201 static int param_set_max_slot_table_size(const char *val, 3202 const struct kernel_param *kp) 3203 { 3204 return param_set_uint_minmax(val, kp, 3205 RPC_MIN_SLOT_TABLE, 3206 RPC_MAX_SLOT_TABLE_LIMIT); 3207 } 3208 3209 static const struct kernel_param_ops param_ops_max_slot_table_size = { 3210 .set = param_set_max_slot_table_size, 3211 .get = param_get_uint, 3212 }; 3213 3214 #define param_check_max_slot_table_size(name, p) \ 3215 __param_check(name, p, unsigned int); 3216 3217 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, 3218 slot_table_size, 0644); 3219 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, 3220 max_slot_table_size, 0644); 3221 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, 3222 slot_table_size, 0644); 3223