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