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