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