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