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