Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/svcsock.c |
| 3 | * |
| 4 | * These are the RPC server socket internals. |
| 5 | * |
| 6 | * The server scheduling algorithm does not always distribute the load |
| 7 | * evenly when servicing a single client. May need to modify the |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 8 | * svc_xprt_enqueue procedure... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * TCP support is largely untested and may be a little slow. The problem |
| 11 | * is that we currently do two separate recvfrom's, one for the 4-byte |
| 12 | * record length, and the second for the actual record. This could possibly |
| 13 | * be improved by always reading a minimum size of around 100 bytes and |
| 14 | * tucking any superfluous bytes away in a temporary store. Still, that |
| 15 | * leaves write requests out in the rain. An alternative may be to peek at |
| 16 | * the first skb in the queue, and if it matches the next TCP sequence |
| 17 | * number, to extract the record marker. Yuck. |
| 18 | * |
| 19 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 20 | */ |
| 21 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/sched.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/errno.h> |
| 26 | #include <linux/fcntl.h> |
| 27 | #include <linux/net.h> |
| 28 | #include <linux/in.h> |
| 29 | #include <linux/inet.h> |
| 30 | #include <linux/udp.h> |
Andrew Morton | 91483c4 | 2005-08-09 20:20:07 -0700 | [diff] [blame] | 31 | #include <linux/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/unistd.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/skbuff.h> |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 36 | #include <linux/file.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 37 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <net/sock.h> |
| 39 | #include <net/checksum.h> |
| 40 | #include <net/ip.h> |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 41 | #include <net/ipv6.h> |
Chuck Lever | b7872fe | 2008-04-14 12:27:01 -0400 | [diff] [blame] | 42 | #include <net/tcp.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 43 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/uaccess.h> |
| 45 | #include <asm/ioctls.h> |
Eric Dumazet | 22911fc | 2012-06-27 00:23:44 +0000 | [diff] [blame] | 46 | #include <trace/events/skb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #include <linux/sunrpc/types.h> |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 49 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/sunrpc/xdr.h> |
Chuck Lever | c0401ea | 2008-04-14 12:27:30 -0400 | [diff] [blame] | 51 | #include <linux/sunrpc/msg_prot.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/sunrpc/svcsock.h> |
| 53 | #include <linux/sunrpc/stats.h> |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 54 | #include <linux/sunrpc/xprt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
H Hartley Sweeten | 177e4f9 | 2011-06-20 17:54:51 -0700 | [diff] [blame] | 56 | #include "sunrpc.h" |
| 57 | |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 58 | #define RPCDBG_FACILITY RPCDBG_SVCXPRT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *, |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 62 | int flags); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 63 | static void svc_udp_data_ready(struct sock *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static int svc_udp_recvfrom(struct svc_rqst *); |
| 65 | static int svc_udp_sendto(struct svc_rqst *); |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 66 | static void svc_sock_detach(struct svc_xprt *); |
Trond Myklebust | 69b6ba3 | 2008-12-23 16:30:11 -0500 | [diff] [blame] | 67 | static void svc_tcp_sock_detach(struct svc_xprt *); |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 68 | static void svc_sock_free(struct svc_xprt *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 70 | static struct svc_xprt *svc_create_socket(struct svc_serv *, int, |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 71 | struct net *, struct sockaddr *, |
| 72 | int, int); |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 73 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 74 | static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int, |
| 75 | struct net *, struct sockaddr *, |
| 76 | int, int); |
| 77 | static void svc_bc_sock_free(struct svc_xprt *xprt); |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 78 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 79 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 80 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 81 | static struct lock_class_key svc_key[2]; |
| 82 | static struct lock_class_key svc_slock_key[2]; |
| 83 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 84 | static void svc_reclassify_socket(struct socket *sock) |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 85 | { |
| 86 | struct sock *sk = sock->sk; |
Weston Andros Adamson | 1b7a181 | 2012-10-23 10:43:39 -0400 | [diff] [blame] | 87 | |
Hannes Frederic Sowa | fafc4e1 | 2016-04-08 15:11:27 +0200 | [diff] [blame] | 88 | if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) |
Weston Andros Adamson | 1b7a181 | 2012-10-23 10:43:39 -0400 | [diff] [blame] | 89 | return; |
| 90 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 91 | switch (sk->sk_family) { |
| 92 | case AF_INET: |
| 93 | sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD", |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 94 | &svc_slock_key[0], |
| 95 | "sk_xprt.xpt_lock-AF_INET-NFSD", |
| 96 | &svc_key[0]); |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 97 | break; |
| 98 | |
| 99 | case AF_INET6: |
| 100 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD", |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 101 | &svc_slock_key[1], |
| 102 | "sk_xprt.xpt_lock-AF_INET6-NFSD", |
| 103 | &svc_key[1]); |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 104 | break; |
| 105 | |
| 106 | default: |
| 107 | BUG(); |
| 108 | } |
| 109 | } |
| 110 | #else |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 111 | static void svc_reclassify_socket(struct socket *sock) |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 112 | { |
| 113 | } |
| 114 | #endif |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* |
| 117 | * Release an skbuff after use |
| 118 | */ |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 119 | static void svc_release_skb(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 121 | struct sk_buff *skb = rqstp->rq_xprt_ctxt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | if (skb) { |
Tom Tucker | 57b1d3b | 2007-12-30 21:08:22 -0600 | [diff] [blame] | 124 | struct svc_sock *svsk = |
| 125 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 126 | rqstp->rq_xprt_ctxt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | dprintk("svc: service %p, releasing skb %p\n", rqstp, skb); |
Eric Dumazet | 9d410c7 | 2009-10-30 05:03:53 +0000 | [diff] [blame] | 129 | skb_free_datagram_locked(svsk->sk_sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 133 | union svc_pktinfo_u { |
| 134 | struct in_pktinfo pkti; |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 135 | struct in6_pktinfo pkti6; |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 136 | }; |
David S. Miller | bc375ea | 2007-04-12 13:35:59 -0700 | [diff] [blame] | 137 | #define SVC_PKTINFO_SPACE \ |
| 138 | CMSG_SPACE(sizeof(union svc_pktinfo_u)) |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 139 | |
| 140 | static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh) |
| 141 | { |
Tom Tucker | 57b1d3b | 2007-12-30 21:08:22 -0600 | [diff] [blame] | 142 | struct svc_sock *svsk = |
| 143 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); |
| 144 | switch (svsk->sk_sk->sk_family) { |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 145 | case AF_INET: { |
| 146 | struct in_pktinfo *pki = CMSG_DATA(cmh); |
| 147 | |
| 148 | cmh->cmsg_level = SOL_IP; |
| 149 | cmh->cmsg_type = IP_PKTINFO; |
| 150 | pki->ipi_ifindex = 0; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 151 | pki->ipi_spec_dst.s_addr = |
| 152 | svc_daddr_in(rqstp)->sin_addr.s_addr; |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 153 | cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); |
| 154 | } |
| 155 | break; |
NeilBrown | 5a05ed7 | 2007-03-06 01:42:22 -0800 | [diff] [blame] | 156 | |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 157 | case AF_INET6: { |
| 158 | struct in6_pktinfo *pki = CMSG_DATA(cmh); |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 159 | struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp); |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 160 | |
| 161 | cmh->cmsg_level = SOL_IPV6; |
| 162 | cmh->cmsg_type = IPV6_PKTINFO; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 163 | pki->ipi6_ifindex = daddr->sin6_scope_id; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 164 | pki->ipi6_addr = daddr->sin6_addr; |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 165 | cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); |
| 166 | } |
| 167 | break; |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 168 | } |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 172 | * send routine intended to be shared by the fore- and back-channel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | */ |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 174 | int svc_send_common(struct socket *sock, struct xdr_buf *xdr, |
| 175 | struct page *headpage, unsigned long headoffset, |
| 176 | struct page *tailpage, unsigned long tailoffset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | int result; |
| 179 | int size; |
| 180 | struct page **ppage = xdr->pages; |
| 181 | size_t base = xdr->page_base; |
| 182 | unsigned int pglen = xdr->page_len; |
Trond Myklebust | 226453d | 2015-10-07 13:54:42 -0400 | [diff] [blame] | 183 | unsigned int flags = MSG_MORE | MSG_SENDPAGE_NOTLAST; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 184 | int slen; |
| 185 | int len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | slen = xdr->len; |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* send head */ |
| 190 | if (slen == xdr->head[0].iov_len) |
| 191 | flags = 0; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 192 | len = kernel_sendpage(sock, headpage, headoffset, |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 193 | xdr->head[0].iov_len, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (len != xdr->head[0].iov_len) |
| 195 | goto out; |
| 196 | slen -= xdr->head[0].iov_len; |
| 197 | if (slen == 0) |
| 198 | goto out; |
| 199 | |
| 200 | /* send page data */ |
| 201 | size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen; |
| 202 | while (pglen > 0) { |
| 203 | if (slen == size) |
| 204 | flags = 0; |
Sridhar Samudrala | e6242e9 | 2006-08-07 20:58:01 -0700 | [diff] [blame] | 205 | result = kernel_sendpage(sock, *ppage, base, size, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (result > 0) |
| 207 | len += result; |
| 208 | if (result != size) |
| 209 | goto out; |
| 210 | slen -= size; |
| 211 | pglen -= size; |
| 212 | size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen; |
| 213 | base = 0; |
| 214 | ppage++; |
| 215 | } |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* send tail */ |
| 218 | if (xdr->tail[0].iov_len) { |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 219 | result = kernel_sendpage(sock, tailpage, tailoffset, |
| 220 | xdr->tail[0].iov_len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (result > 0) |
| 222 | len += result; |
| 223 | } |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 224 | |
| 225 | out: |
| 226 | return len; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * Generic sendto routine |
| 232 | */ |
| 233 | static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr) |
| 234 | { |
| 235 | struct svc_sock *svsk = |
| 236 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); |
| 237 | struct socket *sock = svsk->sk_sock; |
| 238 | union { |
| 239 | struct cmsghdr hdr; |
| 240 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; |
| 241 | } buffer; |
| 242 | struct cmsghdr *cmh = &buffer.hdr; |
| 243 | int len = 0; |
| 244 | unsigned long tailoff; |
| 245 | unsigned long headoff; |
| 246 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
| 247 | |
| 248 | if (rqstp->rq_prot == IPPROTO_UDP) { |
| 249 | struct msghdr msg = { |
| 250 | .msg_name = &rqstp->rq_addr, |
| 251 | .msg_namelen = rqstp->rq_addrlen, |
| 252 | .msg_control = cmh, |
| 253 | .msg_controllen = sizeof(buffer), |
| 254 | .msg_flags = MSG_MORE, |
| 255 | }; |
| 256 | |
| 257 | svc_set_cmsg_data(rqstp, cmh); |
| 258 | |
Al Viro | d8725c8 | 2014-12-11 00:02:50 -0500 | [diff] [blame] | 259 | if (sock_sendmsg(sock, &msg) < 0) |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 260 | goto out; |
| 261 | } |
| 262 | |
| 263 | tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1); |
| 264 | headoff = 0; |
| 265 | len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff, |
| 266 | rqstp->rq_respages[0], tailoff); |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | out: |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 269 | dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n", |
Tom Tucker | 57b1d3b | 2007-12-30 21:08:22 -0600 | [diff] [blame] | 270 | svsk, xdr->head[0].iov_base, xdr->head[0].iov_len, |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 271 | xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | return len; |
| 274 | } |
| 275 | |
| 276 | /* |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 277 | * Report socket names for nfsdfs |
| 278 | */ |
Chuck Lever | e7942b9 | 2009-04-23 19:32:48 -0400 | [diff] [blame] | 279 | static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining) |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 280 | { |
Chuck Lever | 017cb47 | 2009-04-23 19:33:03 -0400 | [diff] [blame] | 281 | const struct sock *sk = svsk->sk_sk; |
| 282 | const char *proto_name = sk->sk_protocol == IPPROTO_UDP ? |
| 283 | "udp" : "tcp"; |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 284 | int len; |
| 285 | |
Chuck Lever | 017cb47 | 2009-04-23 19:33:03 -0400 | [diff] [blame] | 286 | switch (sk->sk_family) { |
Chuck Lever | e7942b9 | 2009-04-23 19:32:48 -0400 | [diff] [blame] | 287 | case PF_INET: |
| 288 | len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n", |
Chuck Lever | 017cb47 | 2009-04-23 19:33:03 -0400 | [diff] [blame] | 289 | proto_name, |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 290 | &inet_sk(sk)->inet_rcv_saddr, |
| 291 | inet_sk(sk)->inet_num); |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 292 | break; |
Eric Dumazet | c2bb06d | 2013-10-09 03:05:48 -0700 | [diff] [blame] | 293 | #if IS_ENABLED(CONFIG_IPV6) |
Chuck Lever | 58de2f8 | 2009-04-23 19:32:55 -0400 | [diff] [blame] | 294 | case PF_INET6: |
| 295 | len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n", |
Chuck Lever | 017cb47 | 2009-04-23 19:33:03 -0400 | [diff] [blame] | 296 | proto_name, |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 297 | &sk->sk_v6_rcv_saddr, |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 298 | inet_sk(sk)->inet_num); |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 299 | break; |
Eric Dumazet | c2bb06d | 2013-10-09 03:05:48 -0700 | [diff] [blame] | 300 | #endif |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 301 | default: |
Chuck Lever | e7942b9 | 2009-04-23 19:32:48 -0400 | [diff] [blame] | 302 | len = snprintf(buf, remaining, "*unknown-%d*\n", |
Chuck Lever | 017cb47 | 2009-04-23 19:33:03 -0400 | [diff] [blame] | 303 | sk->sk_family); |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 304 | } |
Chuck Lever | e7942b9 | 2009-04-23 19:32:48 -0400 | [diff] [blame] | 305 | |
| 306 | if (len >= remaining) { |
| 307 | *buf = '\0'; |
| 308 | return -ENAMETOOLONG; |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 309 | } |
| 310 | return len; |
| 311 | } |
| 312 | |
NeilBrown | 80212d5 | 2006-10-02 02:17:47 -0700 | [diff] [blame] | 313 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | * Generic recvfrom routine. |
| 315 | */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 316 | static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, |
| 317 | int buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Tom Tucker | 57b1d3b | 2007-12-30 21:08:22 -0600 | [diff] [blame] | 319 | struct svc_sock *svsk = |
| 320 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); |
Chuck Lever | 1ba9510 | 2007-02-12 00:53:31 -0800 | [diff] [blame] | 321 | struct msghdr msg = { |
| 322 | .msg_flags = MSG_DONTWAIT, |
| 323 | }; |
| 324 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Tom Tucker | 260c1d1 | 2007-12-30 21:08:29 -0600 | [diff] [blame] | 326 | rqstp->rq_xprt_hlen = 0; |
| 327 | |
Trond Myklebust | f8d1ff4 | 2014-08-03 13:03:13 -0400 | [diff] [blame] | 328 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Chuck Lever | 1ba9510 | 2007-02-12 00:53:31 -0800 | [diff] [blame] | 329 | len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen, |
| 330 | msg.msg_flags); |
Trond Myklebust | f8d1ff4 | 2014-08-03 13:03:13 -0400 | [diff] [blame] | 331 | /* If we read a full record, then assume there may be more |
| 332 | * data to read (stream based sockets only!) |
| 333 | */ |
| 334 | if (len == buflen) |
| 335 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n", |
Chuck Lever | 1ba9510 | 2007-02-12 00:53:31 -0800 | [diff] [blame] | 338 | svsk, iov[0].iov_base, iov[0].iov_len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return len; |
| 340 | } |
| 341 | |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 342 | static int svc_partial_recvfrom(struct svc_rqst *rqstp, |
| 343 | struct kvec *iov, int nr, |
| 344 | int buflen, unsigned int base) |
| 345 | { |
| 346 | size_t save_iovlen; |
Trond Myklebust | 09acfea | 2012-03-11 15:22:54 -0400 | [diff] [blame] | 347 | void *save_iovbase; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 348 | unsigned int i; |
| 349 | int ret; |
| 350 | |
| 351 | if (base == 0) |
| 352 | return svc_recvfrom(rqstp, iov, nr, buflen); |
| 353 | |
| 354 | for (i = 0; i < nr; i++) { |
| 355 | if (iov[i].iov_len > base) |
| 356 | break; |
| 357 | base -= iov[i].iov_len; |
| 358 | } |
| 359 | save_iovlen = iov[i].iov_len; |
| 360 | save_iovbase = iov[i].iov_base; |
| 361 | iov[i].iov_len -= base; |
| 362 | iov[i].iov_base += base; |
| 363 | ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen); |
| 364 | iov[i].iov_len = save_iovlen; |
| 365 | iov[i].iov_base = save_iovbase; |
| 366 | return ret; |
| 367 | } |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* |
| 370 | * Set socket snd and rcv buffer lengths |
| 371 | */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 372 | static void svc_sock_setbufsize(struct socket *sock, unsigned int snd, |
| 373 | unsigned int rcv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | #if 0 |
| 376 | mm_segment_t oldfs; |
| 377 | oldfs = get_fs(); set_fs(KERNEL_DS); |
| 378 | sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, |
| 379 | (char*)&snd, sizeof(snd)); |
| 380 | sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, |
| 381 | (char*)&rcv, sizeof(rcv)); |
| 382 | #else |
| 383 | /* sock_setsockopt limits use to sysctl_?mem_max, |
| 384 | * which isn't acceptable. Until that is made conditional |
| 385 | * on not having CAP_SYS_RESOURCE or similar, we go direct... |
| 386 | * DaveM said I could! |
| 387 | */ |
| 388 | lock_sock(sock->sk); |
| 389 | sock->sk->sk_sndbuf = snd * 2; |
| 390 | sock->sk->sk_rcvbuf = rcv * 2; |
Trond Myklebust | 47fcb03 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 391 | sock->sk->sk_write_space(sock->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | release_sock(sock->sk); |
| 393 | #endif |
| 394 | } |
Chuck Lever | 16e4d93 | 2014-05-19 13:40:22 -0400 | [diff] [blame] | 395 | |
| 396 | static int svc_sock_secure_port(struct svc_rqst *rqstp) |
| 397 | { |
| 398 | return svc_port_is_privileged(svc_addr(rqstp)); |
| 399 | } |
| 400 | |
J. Bruce Fields | 0442f14 | 2015-10-09 01:44:07 +0000 | [diff] [blame] | 401 | static bool sunrpc_waitqueue_active(wait_queue_head_t *wq) |
| 402 | { |
| 403 | if (!wq) |
| 404 | return false; |
| 405 | /* |
| 406 | * There should normally be a memory * barrier here--see |
| 407 | * wq_has_sleeper(). |
| 408 | * |
| 409 | * It appears that isn't currently necessary, though, basically |
| 410 | * because callers all appear to have sufficient memory barriers |
| 411 | * between the time the relevant change is made and the |
| 412 | * time they call these callbacks. |
| 413 | * |
| 414 | * The nfsd code itself doesn't actually explicitly wait on |
| 415 | * these waitqueues, but it may wait on them for example in |
| 416 | * sendpage() or sendmsg() calls. (And those may be the only |
| 417 | * places, since it it uses nonblocking reads.) |
| 418 | * |
| 419 | * Maybe we should add the memory barriers anyway, but these are |
| 420 | * hot paths so we'd need to be convinced there's no sigificant |
| 421 | * penalty. |
| 422 | */ |
| 423 | return waitqueue_active(wq); |
| 424 | } |
| 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | /* |
| 427 | * INET callback when data has been received on the socket. |
| 428 | */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 429 | static void svc_udp_data_ready(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 431 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 432 | wait_queue_head_t *wq = sk_sleep(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 434 | if (svsk) { |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 435 | dprintk("svc: socket %p(inet %p), busy=%d\n", |
| 436 | svsk, sk, |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 437 | test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); |
| 438 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 439 | svc_xprt_enqueue(&svsk->sk_xprt); |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 440 | } |
J. Bruce Fields | 0442f14 | 2015-10-09 01:44:07 +0000 | [diff] [blame] | 441 | if (sunrpc_waitqueue_active(wq)) |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 442 | wake_up_interruptible(wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* |
| 446 | * INET callback when space is newly available on the socket. |
| 447 | */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 448 | static void svc_write_space(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data); |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 451 | wait_queue_head_t *wq = sk_sleep(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | if (svsk) { |
| 454 | dprintk("svc: socket %p(inet %p), write_space busy=%d\n", |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 455 | svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 456 | svc_xprt_enqueue(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
J. Bruce Fields | 0442f14 | 2015-10-09 01:44:07 +0000 | [diff] [blame] | 459 | if (sunrpc_waitqueue_active(wq)) { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 460 | dprintk("RPC svc_write_space: someone sleeping on %p\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | svsk); |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 462 | wake_up_interruptible(wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Trond Myklebust | c7fb3f0 | 2014-07-24 23:59:32 -0400 | [diff] [blame] | 466 | static int svc_tcp_has_wspace(struct svc_xprt *xprt) |
| 467 | { |
| 468 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
| 469 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
| 470 | int required; |
| 471 | |
| 472 | if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) |
| 473 | return 1; |
| 474 | required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg; |
| 475 | if (sk_stream_wspace(svsk->sk_sk) >= required || |
| 476 | (sk_stream_min_wspace(svsk->sk_sk) == 0 && |
| 477 | atomic_read(&xprt->xpt_reserved) == 0)) |
| 478 | return 1; |
| 479 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); |
| 480 | return 0; |
| 481 | } |
| 482 | |
Trond Myklebust | 47fcb03 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 483 | static void svc_tcp_write_space(struct sock *sk) |
| 484 | { |
Trond Myklebust | c7fb3f0 | 2014-07-24 23:59:32 -0400 | [diff] [blame] | 485 | struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data); |
Trond Myklebust | 47fcb03 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 486 | struct socket *sock = sk->sk_socket; |
| 487 | |
Trond Myklebust | c7fb3f0 | 2014-07-24 23:59:32 -0400 | [diff] [blame] | 488 | if (!sk_stream_is_writeable(sk) || !sock) |
| 489 | return; |
| 490 | if (!svsk || svc_tcp_has_wspace(&svsk->sk_xprt)) |
Trond Myklebust | 47fcb03 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 491 | clear_bit(SOCK_NOSPACE, &sock->flags); |
| 492 | svc_write_space(sk); |
| 493 | } |
| 494 | |
Trond Myklebust | 5187768 | 2014-07-24 23:59:33 -0400 | [diff] [blame] | 495 | static void svc_tcp_adjust_wspace(struct svc_xprt *xprt) |
| 496 | { |
| 497 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
| 498 | |
| 499 | if (svc_tcp_has_wspace(xprt)) |
| 500 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); |
| 501 | } |
| 502 | |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 503 | /* |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 504 | * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo |
| 505 | */ |
| 506 | static int svc_udp_get_dest_address4(struct svc_rqst *rqstp, |
| 507 | struct cmsghdr *cmh) |
| 508 | { |
| 509 | struct in_pktinfo *pki = CMSG_DATA(cmh); |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 510 | struct sockaddr_in *daddr = svc_daddr_in(rqstp); |
| 511 | |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 512 | if (cmh->cmsg_type != IP_PKTINFO) |
| 513 | return 0; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 514 | |
| 515 | daddr->sin_family = AF_INET; |
| 516 | daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr; |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 517 | return 1; |
| 518 | } |
| 519 | |
| 520 | /* |
Tom Parkin | 73df66f | 2013-01-31 01:02:24 +0000 | [diff] [blame] | 521 | * See net/ipv6/datagram.c : ip6_datagram_recv_ctl |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 522 | */ |
| 523 | static int svc_udp_get_dest_address6(struct svc_rqst *rqstp, |
| 524 | struct cmsghdr *cmh) |
| 525 | { |
| 526 | struct in6_pktinfo *pki = CMSG_DATA(cmh); |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 527 | struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp); |
| 528 | |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 529 | if (cmh->cmsg_type != IPV6_PKTINFO) |
| 530 | return 0; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 531 | |
| 532 | daddr->sin6_family = AF_INET6; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 533 | daddr->sin6_addr = pki->ipi6_addr; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 534 | daddr->sin6_scope_id = pki->ipi6_ifindex; |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 535 | return 1; |
| 536 | } |
| 537 | |
| 538 | /* |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 539 | * Copy the UDP datagram's destination address to the rqstp structure. |
| 540 | * The 'destination' address in this case is the address to which the |
| 541 | * peer sent the datagram, i.e. our local address. For multihomed |
| 542 | * hosts, this can change from msg to msg. Note that only the IP |
| 543 | * address changes, the port number should remain the same. |
| 544 | */ |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 545 | static int svc_udp_get_dest_address(struct svc_rqst *rqstp, |
| 546 | struct cmsghdr *cmh) |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 547 | { |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 548 | switch (cmh->cmsg_level) { |
| 549 | case SOL_IP: |
| 550 | return svc_udp_get_dest_address4(rqstp, cmh); |
| 551 | case SOL_IPV6: |
| 552 | return svc_udp_get_dest_address6(rqstp, cmh); |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 553 | } |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 554 | |
| 555 | return 0; |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* |
| 559 | * Receive a datagram from a UDP socket. |
| 560 | */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 561 | static int svc_udp_recvfrom(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Tom Tucker | 57b1d3b | 2007-12-30 21:08:22 -0600 | [diff] [blame] | 563 | struct svc_sock *svsk = |
| 564 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 565 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | struct sk_buff *skb; |
David S. Miller | bc375ea | 2007-04-12 13:35:59 -0700 | [diff] [blame] | 567 | union { |
| 568 | struct cmsghdr hdr; |
| 569 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; |
| 570 | } buffer; |
| 571 | struct cmsghdr *cmh = &buffer.hdr; |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 572 | struct msghdr msg = { |
| 573 | .msg_name = svc_addr(rqstp), |
| 574 | .msg_control = cmh, |
| 575 | .msg_controllen = sizeof(buffer), |
| 576 | .msg_flags = MSG_DONTWAIT, |
| 577 | }; |
Chuck Lever | abc5c44 | 2009-04-23 19:31:25 -0400 | [diff] [blame] | 578 | size_t len; |
| 579 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 581 | if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | /* udp sockets need large rcvbuf as all pending |
| 583 | * requests are still in that buffer. sndbuf must |
| 584 | * also be large enough that there is enough space |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 585 | * for one reply per thread. We count all threads |
| 586 | * rather than threads in a particular pool, which |
| 587 | * provides an upper bound on the number of threads |
| 588 | * which will access the socket. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | */ |
| 590 | svc_sock_setbufsize(svsk->sk_sock, |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 591 | (serv->sv_nrthreads+3) * serv->sv_max_mesg, |
| 592 | (serv->sv_nrthreads+3) * serv->sv_max_mesg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 594 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
NeilBrown | 05ed690 | 2007-05-09 02:34:55 -0700 | [diff] [blame] | 595 | skb = NULL; |
| 596 | err = kernel_recvmsg(svsk->sk_sock, &msg, NULL, |
| 597 | 0, 0, MSG_PEEK | MSG_DONTWAIT); |
| 598 | if (err >= 0) |
| 599 | skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err); |
| 600 | |
| 601 | if (skb == NULL) { |
| 602 | if (err != -EAGAIN) { |
| 603 | /* possibly an icmp error */ |
| 604 | dprintk("svc: recvfrom returned error %d\n", -err); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 605 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
J. Bruce Fields | 9f9d2eb | 2012-08-17 21:35:24 -0400 | [diff] [blame] | 607 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 609 | len = svc_addr_len(svc_addr(rqstp)); |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 610 | rqstp->rq_addrlen = len; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 611 | if (skb->tstamp.tv64 == 0) { |
| 612 | skb->tstamp = ktime_get_real(); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 613 | /* Don't enable netstamp, sunrpc doesn't |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | need that much accuracy */ |
| 615 | } |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 616 | svsk->sk_sk->sk_stamp = skb->tstamp; |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 617 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Willem de Bruijn | 1da8c681 | 2016-04-07 11:44:58 -0400 | [diff] [blame] | 619 | len = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | rqstp->rq_arg.len = len; |
| 621 | |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 622 | rqstp->rq_prot = IPPROTO_UDP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 624 | if (!svc_udp_get_dest_address(rqstp, cmh)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 625 | net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n", |
| 626 | cmh->cmsg_level, cmh->cmsg_type); |
J. Bruce Fields | f23abfd | 2012-08-17 20:32:27 -0400 | [diff] [blame] | 627 | goto out_free; |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 628 | } |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 629 | rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
| 631 | if (skb_is_nonlinear(skb)) { |
| 632 | /* we have to copy */ |
| 633 | local_bh_disable(); |
| 634 | if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) { |
| 635 | local_bh_enable(); |
| 636 | /* checksum error */ |
Eric Dumazet | 22911fc | 2012-06-27 00:23:44 +0000 | [diff] [blame] | 637 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | local_bh_enable(); |
Eric Dumazet | 9d410c7 | 2009-10-30 05:03:53 +0000 | [diff] [blame] | 640 | skb_free_datagram_locked(svsk->sk_sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } else { |
| 642 | /* we can use it in-place */ |
Willem de Bruijn | 1da8c681 | 2016-04-07 11:44:58 -0400 | [diff] [blame] | 643 | rqstp->rq_arg.head[0].iov_base = skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | rqstp->rq_arg.head[0].iov_len = len; |
Eric Dumazet | 22911fc | 2012-06-27 00:23:44 +0000 | [diff] [blame] | 645 | if (skb_checksum_complete(skb)) |
| 646 | goto out_free; |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 647 | rqstp->rq_xprt_ctxt = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | rqstp->rq_arg.page_base = 0; |
| 651 | if (len <= rqstp->rq_arg.head[0].iov_len) { |
| 652 | rqstp->rq_arg.head[0].iov_len = len; |
| 653 | rqstp->rq_arg.page_len = 0; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 654 | rqstp->rq_respages = rqstp->rq_pages+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } else { |
| 656 | rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 657 | rqstp->rq_respages = rqstp->rq_pages + 1 + |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 658 | DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 660 | rqstp->rq_next_page = rqstp->rq_respages+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
| 662 | if (serv->sv_stats) |
| 663 | serv->sv_stats->netudpcnt++; |
| 664 | |
| 665 | return len; |
J. Bruce Fields | f23abfd | 2012-08-17 20:32:27 -0400 | [diff] [blame] | 666 | out_free: |
| 667 | trace_kfree_skb(skb, svc_udp_recvfrom); |
| 668 | skb_free_datagram_locked(svsk->sk_sk, skb); |
| 669 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static int |
| 673 | svc_udp_sendto(struct svc_rqst *rqstp) |
| 674 | { |
| 675 | int error; |
| 676 | |
| 677 | error = svc_sendto(rqstp, &rqstp->rq_res); |
| 678 | if (error == -ECONNREFUSED) |
| 679 | /* ICMP error on earlier request. */ |
| 680 | error = svc_sendto(rqstp, &rqstp->rq_res); |
| 681 | |
| 682 | return error; |
| 683 | } |
| 684 | |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 685 | static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp) |
| 686 | { |
| 687 | } |
| 688 | |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 689 | static int svc_udp_has_wspace(struct svc_xprt *xprt) |
| 690 | { |
| 691 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 692 | struct svc_serv *serv = xprt->xpt_server; |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 693 | unsigned long required; |
| 694 | |
| 695 | /* |
| 696 | * Set the SOCK_NOSPACE flag before checking the available |
| 697 | * sock space. |
| 698 | */ |
| 699 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); |
Tom Tucker | 7a90e8c | 2007-12-30 21:07:55 -0600 | [diff] [blame] | 700 | required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 701 | if (required*2 > sock_wspace(svsk->sk_sk)) |
| 702 | return 0; |
| 703 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); |
| 704 | return 1; |
| 705 | } |
| 706 | |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 707 | static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt) |
| 708 | { |
| 709 | BUG(); |
| 710 | return NULL; |
| 711 | } |
| 712 | |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 713 | static struct svc_xprt *svc_udp_create(struct svc_serv *serv, |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 714 | struct net *net, |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 715 | struct sockaddr *sa, int salen, |
| 716 | int flags) |
| 717 | { |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 718 | return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 719 | } |
| 720 | |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 721 | static struct svc_xprt_ops svc_udp_ops = { |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 722 | .xpo_create = svc_udp_create, |
Tom Tucker | 5d13799 | 2007-12-30 21:07:23 -0600 | [diff] [blame] | 723 | .xpo_recvfrom = svc_udp_recvfrom, |
| 724 | .xpo_sendto = svc_udp_sendto, |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 725 | .xpo_release_rqst = svc_release_skb, |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 726 | .xpo_detach = svc_sock_detach, |
| 727 | .xpo_free = svc_sock_free, |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 728 | .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr, |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 729 | .xpo_has_wspace = svc_udp_has_wspace, |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 730 | .xpo_accept = svc_udp_accept, |
Chuck Lever | 16e4d93 | 2014-05-19 13:40:22 -0400 | [diff] [blame] | 731 | .xpo_secure_port = svc_sock_secure_port, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | static struct svc_xprt_class svc_udp_class = { |
| 735 | .xcl_name = "udp", |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 736 | .xcl_owner = THIS_MODULE, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 737 | .xcl_ops = &svc_udp_ops, |
Tom Tucker | 4902315 | 2007-12-30 21:07:21 -0600 | [diff] [blame] | 738 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP, |
Chuck Lever | 3c45ddf | 2014-07-16 15:38:32 -0400 | [diff] [blame] | 739 | .xcl_ident = XPRT_TRANSPORT_UDP, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 740 | }; |
| 741 | |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 742 | static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 744 | int err, level, optname, one = 1; |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 745 | |
Stanislav Kinsbursky | bd4620d | 2011-12-06 14:19:10 +0300 | [diff] [blame] | 746 | svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class, |
| 747 | &svsk->sk_xprt, serv); |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 748 | clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | svsk->sk_sk->sk_data_ready = svc_udp_data_ready; |
| 750 | svsk->sk_sk->sk_write_space = svc_write_space; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | /* initialise setting must have enough space to |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 753 | * receive and respond to one request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | * svc_udp_recvfrom will re-adjust if necessary |
| 755 | */ |
| 756 | svc_sock_setbufsize(svsk->sk_sock, |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 757 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg, |
| 758 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 760 | /* data might have come in before data_ready set up */ |
| 761 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 762 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 763 | |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 764 | /* make sure we get destination address info */ |
Chuck Lever | 7702ce4 | 2009-07-13 10:54:26 -0400 | [diff] [blame] | 765 | switch (svsk->sk_sk->sk_family) { |
| 766 | case AF_INET: |
| 767 | level = SOL_IP; |
| 768 | optname = IP_PKTINFO; |
| 769 | break; |
| 770 | case AF_INET6: |
| 771 | level = SOL_IPV6; |
| 772 | optname = IPV6_RECVPKTINFO; |
| 773 | break; |
| 774 | default: |
| 775 | BUG(); |
| 776 | } |
| 777 | err = kernel_setsockopt(svsk->sk_sock, level, optname, |
| 778 | (char *)&one, sizeof(one)); |
| 779 | dprintk("svc: kernel_setsockopt returned %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* |
| 783 | * A data_ready event on a listening socket means there's a connection |
| 784 | * pending. Do not use state_change as a substitute for it. |
| 785 | */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 786 | static void svc_tcp_listen_data_ready(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 788 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 789 | wait_queue_head_t *wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
| 791 | dprintk("svc: socket %p TCP (listen) state change %d\n", |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 792 | sk, sk->sk_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 794 | /* |
| 795 | * This callback may called twice when a new connection |
| 796 | * is established as a child socket inherits everything |
| 797 | * from a parent LISTEN socket. |
| 798 | * 1) data_ready method of the parent socket will be called |
| 799 | * when one of child sockets become ESTABLISHED. |
| 800 | * 2) data_ready method of the child socket may be called |
| 801 | * when it receives data before the socket is accepted. |
| 802 | * In case of 2, we should ignore it silently. |
| 803 | */ |
| 804 | if (sk->sk_state == TCP_LISTEN) { |
| 805 | if (svsk) { |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 806 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 807 | svc_xprt_enqueue(&svsk->sk_xprt); |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 808 | } else |
| 809 | printk("svc: socket %p: no user data\n", sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 811 | |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 812 | wq = sk_sleep(sk); |
J. Bruce Fields | 0442f14 | 2015-10-09 01:44:07 +0000 | [diff] [blame] | 813 | if (sunrpc_waitqueue_active(wq)) |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 814 | wake_up_interruptible_all(wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* |
| 818 | * A state change on a connected socket means it's dying or dead. |
| 819 | */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 820 | static void svc_tcp_state_change(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 822 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 823 | wait_queue_head_t *wq = sk_sleep(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n", |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 826 | sk, sk->sk_state, sk->sk_user_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 828 | if (!svsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | printk("svc: socket %p: no user data\n", sk); |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 830 | else { |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 831 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 832 | svc_xprt_enqueue(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } |
J. Bruce Fields | 0442f14 | 2015-10-09 01:44:07 +0000 | [diff] [blame] | 834 | if (sunrpc_waitqueue_active(wq)) |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 835 | wake_up_interruptible_all(wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } |
| 837 | |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 838 | static void svc_tcp_data_ready(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 840 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 841 | wait_queue_head_t *wq = sk_sleep(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
| 843 | dprintk("svc: socket %p TCP data ready (svsk %p)\n", |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 844 | sk, sk->sk_user_data); |
| 845 | if (svsk) { |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 846 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 847 | svc_xprt_enqueue(&svsk->sk_xprt); |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 848 | } |
J. Bruce Fields | 0442f14 | 2015-10-09 01:44:07 +0000 | [diff] [blame] | 849 | if (sunrpc_waitqueue_active(wq)) |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 850 | wake_up_interruptible(wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Accept a TCP connection |
| 855 | */ |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 856 | static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | { |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 858 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 859 | struct sockaddr_storage addr; |
| 860 | struct sockaddr *sin = (struct sockaddr *) &addr; |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 861 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | struct socket *sock = svsk->sk_sock; |
| 863 | struct socket *newsock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | struct svc_sock *newsvsk; |
| 865 | int err, slen; |
Pavel Emelyanov | 5216a8e | 2008-02-21 10:57:45 +0300 | [diff] [blame] | 866 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | dprintk("svc: tcp_accept %p sock %p\n", svsk, sock); |
| 869 | if (!sock) |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 870 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 872 | clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
Sridhar Samudrala | e6242e9 | 2006-08-07 20:58:01 -0700 | [diff] [blame] | 873 | err = kernel_accept(sock, &newsock, O_NONBLOCK); |
| 874 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | if (err == -ENOMEM) |
| 876 | printk(KERN_WARNING "%s: no more sockets!\n", |
| 877 | serv->sv_name); |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 878 | else if (err != -EAGAIN) |
| 879 | net_warn_ratelimited("%s: accept failed (err %d)!\n", |
| 880 | serv->sv_name, -err); |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 881 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 883 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 885 | err = kernel_getpeername(newsock, sin, &slen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | if (err < 0) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 887 | net_warn_ratelimited("%s: peername failed (err %d)!\n", |
| 888 | serv->sv_name, -err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | goto failed; /* aborted connection or whatever */ |
| 890 | } |
| 891 | |
| 892 | /* Ideally, we would want to reject connections from unauthorized |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 893 | * hosts here, but when we get encryption, the IP of the host won't |
| 894 | * tell us anything. For now just warn about unpriv connections. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | */ |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 896 | if (!svc_port_is_privileged(sin)) { |
Kinglong Mee | a48fd0f | 2014-05-29 12:18:52 +0800 | [diff] [blame] | 897 | dprintk("%s: connect from unprivileged port: %s\n", |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 898 | serv->sv_name, |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 899 | __svc_print_addr(sin, buf, sizeof(buf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 901 | dprintk("%s: connect from %s\n", serv->sv_name, |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 902 | __svc_print_addr(sin, buf, sizeof(buf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | /* make sure that a write doesn't block forever when |
| 905 | * low on memory |
| 906 | */ |
| 907 | newsock->sk->sk_sndtimeo = HZ*30; |
| 908 | |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 909 | newsvsk = svc_setup_socket(serv, newsock, |
| 910 | (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY)); |
| 911 | if (IS_ERR(newsvsk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | goto failed; |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 913 | svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen); |
Frank van Maarseveen | a974769 | 2007-07-09 22:21:39 +0200 | [diff] [blame] | 914 | err = kernel_getsockname(newsock, sin, &slen); |
| 915 | if (unlikely(err < 0)) { |
| 916 | dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err); |
| 917 | slen = offsetof(struct sockaddr, sa_data); |
| 918 | } |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 919 | svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen); |
Chuck Lever | 067d781 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 920 | |
NeilBrown | ef11ce2 | 2014-05-12 11:22:47 +1000 | [diff] [blame] | 921 | if (sock_is_loopback(newsock->sk)) |
| 922 | set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags); |
| 923 | else |
| 924 | clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags); |
Tom Tucker | f9f3cc4 | 2007-12-30 21:07:40 -0600 | [diff] [blame] | 925 | if (serv->sv_stats) |
| 926 | serv->sv_stats->nettcpconn++; |
| 927 | |
| 928 | return &newsvsk->sk_xprt; |
| 929 | |
| 930 | failed: |
| 931 | sock_release(newsock); |
| 932 | return NULL; |
| 933 | } |
| 934 | |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 935 | static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp) |
| 936 | { |
| 937 | unsigned int i, len, npages; |
| 938 | |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 939 | if (svsk->sk_datalen == 0) |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 940 | return 0; |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 941 | len = svsk->sk_datalen; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 942 | npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 943 | for (i = 0; i < npages; i++) { |
| 944 | if (rqstp->rq_pages[i] != NULL) |
| 945 | put_page(rqstp->rq_pages[i]); |
| 946 | BUG_ON(svsk->sk_pages[i] == NULL); |
| 947 | rqstp->rq_pages[i] = svsk->sk_pages[i]; |
| 948 | svsk->sk_pages[i] = NULL; |
| 949 | } |
| 950 | rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]); |
| 951 | return len; |
| 952 | } |
| 953 | |
| 954 | static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp) |
| 955 | { |
| 956 | unsigned int i, len, npages; |
| 957 | |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 958 | if (svsk->sk_datalen == 0) |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 959 | return; |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 960 | len = svsk->sk_datalen; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 961 | npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 962 | for (i = 0; i < npages; i++) { |
| 963 | svsk->sk_pages[i] = rqstp->rq_pages[i]; |
| 964 | rqstp->rq_pages[i] = NULL; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | static void svc_tcp_clear_pages(struct svc_sock *svsk) |
| 969 | { |
| 970 | unsigned int i, len, npages; |
| 971 | |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 972 | if (svsk->sk_datalen == 0) |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 973 | goto out; |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 974 | len = svsk->sk_datalen; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 975 | npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 976 | for (i = 0; i < npages; i++) { |
J. Bruce Fields | cf3aa02 | 2013-06-26 11:09:06 -0400 | [diff] [blame] | 977 | if (svsk->sk_pages[i] == NULL) { |
| 978 | WARN_ON_ONCE(1); |
| 979 | continue; |
| 980 | } |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 981 | put_page(svsk->sk_pages[i]); |
| 982 | svsk->sk_pages[i] = NULL; |
| 983 | } |
| 984 | out: |
| 985 | svsk->sk_tcplen = 0; |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 986 | svsk->sk_datalen = 0; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Tom Tucker | f9f3cc4 | 2007-12-30 21:07:40 -0600 | [diff] [blame] | 989 | /* |
J. Bruce Fields | ad46ccf | 2012-12-03 16:30:42 -0500 | [diff] [blame] | 990 | * Receive fragment record header. |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 991 | * If we haven't gotten the record length yet, get the next four bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | */ |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 993 | static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | { |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 995 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
Trond Myklebust | 5ee78d4 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 996 | unsigned int want; |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 997 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
Chuck Lever | c0401ea | 2008-04-14 12:27:30 -0400 | [diff] [blame] | 999 | if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | struct kvec iov; |
| 1001 | |
Trond Myklebust | 5ee78d4 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1002 | want = sizeof(rpc_fraghdr) - svsk->sk_tcplen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen; |
| 1004 | iov.iov_len = want; |
| 1005 | if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0) |
| 1006 | goto error; |
| 1007 | svsk->sk_tcplen += len; |
| 1008 | |
| 1009 | if (len < want) { |
Chuck Lever | c0401ea | 2008-04-14 12:27:30 -0400 | [diff] [blame] | 1010 | dprintk("svc: short recvfrom while reading record " |
| 1011 | "length (%d of %d)\n", len, want); |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1012 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
J. Bruce Fields | cc248d4 | 2012-12-03 16:11:13 -0500 | [diff] [blame] | 1015 | dprintk("svc: TCP record, %d bytes\n", svc_sock_reclen(svsk)); |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1016 | if (svc_sock_reclen(svsk) + svsk->sk_datalen > |
| 1017 | serv->sv_max_mesg) { |
J. Bruce Fields | 3a28e33 | 2012-12-10 18:15:07 -0500 | [diff] [blame] | 1018 | net_notice_ratelimited("RPC: fragment too large: %d\n", |
| 1019 | svc_sock_reclen(svsk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | goto err_delete; |
| 1021 | } |
| 1022 | } |
| 1023 | |
J. Bruce Fields | cc248d4 | 2012-12-03 16:11:13 -0500 | [diff] [blame] | 1024 | return svc_sock_reclen(svsk); |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1025 | error: |
| 1026 | dprintk("RPC: TCP recv_record got %d\n", len); |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 1027 | return len; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1028 | err_delete: |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 1029 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 1030 | return -EAGAIN; |
| 1031 | } |
| 1032 | |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1033 | static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp) |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 1034 | { |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1035 | struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 1036 | struct rpc_rqst *req = NULL; |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1037 | struct kvec *src, *dst; |
| 1038 | __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base; |
J. Bruce Fields | 48e6555 | 2011-02-14 14:52:03 -0500 | [diff] [blame] | 1039 | __be32 xid; |
| 1040 | __be32 calldir; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 1041 | |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 1042 | xid = *p++; |
| 1043 | calldir = *p; |
| 1044 | |
Trond Myklebust | 093a146 | 2014-11-12 18:04:04 -0500 | [diff] [blame] | 1045 | if (!bc_xprt) |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1046 | return -EAGAIN; |
Trond Myklebust | 093a146 | 2014-11-12 18:04:04 -0500 | [diff] [blame] | 1047 | spin_lock_bh(&bc_xprt->transport_lock); |
| 1048 | req = xprt_lookup_rqst(bc_xprt, xid); |
| 1049 | if (!req) |
| 1050 | goto unlock_notfound; |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1051 | |
| 1052 | memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf)); |
| 1053 | /* |
| 1054 | * XXX!: cheating for now! Only copying HEAD. |
| 1055 | * But we know this is good enough for now (in fact, for any |
| 1056 | * callback reply in the forseeable future). |
| 1057 | */ |
| 1058 | dst = &req->rq_private_buf.head[0]; |
| 1059 | src = &rqstp->rq_arg.head[0]; |
| 1060 | if (dst->iov_len < src->iov_len) |
Trond Myklebust | 093a146 | 2014-11-12 18:04:04 -0500 | [diff] [blame] | 1061 | goto unlock_eagain; /* whatever; just giving up. */ |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1062 | memcpy(dst->iov_base, src->iov_base, src->iov_len); |
J. Bruce Fields | cc248d4 | 2012-12-03 16:11:13 -0500 | [diff] [blame] | 1063 | xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len); |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1064 | rqstp->rq_arg.len = 0; |
Trond Myklebust | 093a146 | 2014-11-12 18:04:04 -0500 | [diff] [blame] | 1065 | spin_unlock_bh(&bc_xprt->transport_lock); |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1066 | return 0; |
Trond Myklebust | 093a146 | 2014-11-12 18:04:04 -0500 | [diff] [blame] | 1067 | unlock_notfound: |
| 1068 | printk(KERN_NOTICE |
| 1069 | "%s: Got unrecognized reply: " |
| 1070 | "calldir 0x%x xpt_bc_xprt %p xid %08x\n", |
| 1071 | __func__, ntohl(calldir), |
| 1072 | bc_xprt, ntohl(xid)); |
| 1073 | unlock_eagain: |
| 1074 | spin_unlock_bh(&bc_xprt->transport_lock); |
| 1075 | return -EAGAIN; |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len) |
| 1079 | { |
| 1080 | int i = 0; |
| 1081 | int t = 0; |
| 1082 | |
| 1083 | while (t < len) { |
| 1084 | vec[i].iov_base = page_address(pages[i]); |
| 1085 | vec[i].iov_len = PAGE_SIZE; |
| 1086 | i++; |
| 1087 | t += PAGE_SIZE; |
| 1088 | } |
| 1089 | return i; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 1090 | } |
| 1091 | |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1092 | static void svc_tcp_fragment_received(struct svc_sock *svsk) |
| 1093 | { |
| 1094 | /* If we have more data, signal svc_xprt_enqueue() to try again */ |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1095 | dprintk("svc: TCP %s record (%d bytes)\n", |
| 1096 | svc_sock_final_rec(svsk) ? "final" : "nonfinal", |
| 1097 | svc_sock_reclen(svsk)); |
| 1098 | svsk->sk_tcplen = 0; |
| 1099 | svsk->sk_reclen = 0; |
| 1100 | } |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1101 | |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 1102 | /* |
| 1103 | * Receive data from a TCP socket. |
| 1104 | */ |
| 1105 | static int svc_tcp_recvfrom(struct svc_rqst *rqstp) |
| 1106 | { |
| 1107 | struct svc_sock *svsk = |
| 1108 | container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt); |
| 1109 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
| 1110 | int len; |
| 1111 | struct kvec *vec; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1112 | unsigned int want, base; |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1113 | __be32 *p; |
| 1114 | __be32 calldir; |
Trond Myklebust | 5ee78d4 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1115 | int pnum; |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 1116 | |
| 1117 | dprintk("svc: tcp_recv %p data %d conn %d close %d\n", |
| 1118 | svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags), |
| 1119 | test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags), |
| 1120 | test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)); |
| 1121 | |
| 1122 | len = svc_tcp_recv_record(svsk, rqstp); |
| 1123 | if (len < 0) |
| 1124 | goto error; |
| 1125 | |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1126 | base = svc_tcp_restore_pages(svsk, rqstp); |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1127 | want = svc_sock_reclen(svsk) - (svsk->sk_tcplen - sizeof(rpc_fraghdr)); |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1128 | |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 1129 | vec = rqstp->rq_vec; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 1130 | |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1131 | pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0], |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1132 | svsk->sk_datalen + want); |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 1133 | |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 1134 | rqstp->rq_respages = &rqstp->rq_pages[pnum]; |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 1135 | rqstp->rq_next_page = rqstp->rq_respages + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | |
| 1137 | /* Now receive data */ |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1138 | len = svc_partial_recvfrom(rqstp, vec, pnum, want, base); |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 1139 | if (len >= 0) { |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1140 | svsk->sk_tcplen += len; |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 1141 | svsk->sk_datalen += len; |
| 1142 | } |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1143 | if (len != want || !svc_sock_final_rec(svsk)) { |
J. Bruce Fields | be1e444 | 2012-08-09 18:12:28 -0400 | [diff] [blame] | 1144 | svc_tcp_save_pages(svsk, rqstp); |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1145 | if (len < 0 && len != -EAGAIN) |
J. Bruce Fields | ad46ccf | 2012-12-03 16:30:42 -0500 | [diff] [blame] | 1146 | goto err_delete; |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1147 | if (len == want) |
| 1148 | svc_tcp_fragment_received(svsk); |
| 1149 | else |
J. Bruce Fields | 3a28e33 | 2012-12-10 18:15:07 -0500 | [diff] [blame] | 1150 | dprintk("svc: incomplete TCP record (%d of %d)\n", |
| 1151 | (int)(svsk->sk_tcplen - sizeof(rpc_fraghdr)), |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1152 | svc_sock_reclen(svsk)); |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1153 | goto err_noclose; |
| 1154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | |
J. Bruce Fields | 1f691b0 | 2013-06-26 10:55:40 -0400 | [diff] [blame] | 1156 | if (svsk->sk_datalen < 8) { |
J. Bruce Fields | cf3aa02 | 2013-06-26 11:09:06 -0400 | [diff] [blame] | 1157 | svsk->sk_datalen = 0; |
J. Bruce Fields | ad46ccf | 2012-12-03 16:30:42 -0500 | [diff] [blame] | 1158 | goto err_delete; /* client is nuts. */ |
J. Bruce Fields | cf3aa02 | 2013-06-26 11:09:06 -0400 | [diff] [blame] | 1159 | } |
J. Bruce Fields | ad46ccf | 2012-12-03 16:30:42 -0500 | [diff] [blame] | 1160 | |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1161 | rqstp->rq_arg.len = svsk->sk_datalen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | rqstp->rq_arg.page_base = 0; |
Trond Myklebust | 5ee78d4 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1163 | if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) { |
| 1164 | rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | rqstp->rq_arg.page_len = 0; |
Trond Myklebust | 5ee78d4 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1166 | } else |
| 1167 | rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 1169 | rqstp->rq_xprt_ctxt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | rqstp->rq_prot = IPPROTO_TCP; |
Jeff Layton | 7501cc2 | 2014-11-19 07:51:15 -0500 | [diff] [blame] | 1171 | if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags)) |
| 1172 | set_bit(RQ_LOCAL, &rqstp->rq_flags); |
| 1173 | else |
| 1174 | clear_bit(RQ_LOCAL, &rqstp->rq_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1176 | p = (__be32 *)rqstp->rq_arg.head[0].iov_base; |
| 1177 | calldir = p[1]; |
J. Bruce Fields | 8985ef0 | 2011-04-09 10:03:10 -0400 | [diff] [blame] | 1178 | if (calldir) |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1179 | len = receive_cb_reply(svsk, rqstp); |
Trond Myklebust | 586c52c | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | /* Reset TCP read info */ |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 1182 | svsk->sk_datalen = 0; |
J. Bruce Fields | 836fbad | 2012-12-03 15:50:38 -0500 | [diff] [blame] | 1183 | svc_tcp_fragment_received(svsk); |
Trond Myklebust | 0601f79 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1184 | |
J. Bruce Fields | 8985ef0 | 2011-04-09 10:03:10 -0400 | [diff] [blame] | 1185 | if (len < 0) |
| 1186 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 1188 | svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | if (serv->sv_stats) |
| 1190 | serv->sv_stats->nettcpcnt++; |
| 1191 | |
Trond Myklebust | 5ee78d4 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1192 | return rqstp->rq_arg.len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
Alexandros Batsakis | 8f55f3c | 2009-08-20 03:34:19 +0300 | [diff] [blame] | 1194 | error: |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1195 | if (len != -EAGAIN) |
J. Bruce Fields | ad46ccf | 2012-12-03 16:30:42 -0500 | [diff] [blame] | 1196 | goto err_delete; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1197 | dprintk("RPC: TCP recvfrom got EAGAIN\n"); |
J. Bruce Fields | 9f9d2eb | 2012-08-17 21:35:24 -0400 | [diff] [blame] | 1198 | return 0; |
J. Bruce Fields | ad46ccf | 2012-12-03 16:30:42 -0500 | [diff] [blame] | 1199 | err_delete: |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1200 | printk(KERN_NOTICE "%s: recvfrom returned errno %d\n", |
| 1201 | svsk->sk_xprt.xpt_server->sv_name, -len); |
| 1202 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
| 1203 | err_noclose: |
J. Bruce Fields | 9f9d2eb | 2012-08-17 21:35:24 -0400 | [diff] [blame] | 1204 | return 0; /* record not complete */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * Send out data on TCP socket. |
| 1209 | */ |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1210 | static int svc_tcp_sendto(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | { |
| 1212 | struct xdr_buf *xbufp = &rqstp->rq_res; |
| 1213 | int sent; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1214 | __be32 reclen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | |
| 1216 | /* Set up the first element of the reply kvec. |
| 1217 | * Any other kvecs that may be in use have been taken |
| 1218 | * care of by the server implementation itself. |
| 1219 | */ |
| 1220 | reclen = htonl(0x80000000|((xbufp->len ) - 4)); |
| 1221 | memcpy(xbufp->head[0].iov_base, &reclen, 4); |
| 1222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | sent = svc_sendto(rqstp, &rqstp->rq_res); |
| 1224 | if (sent != xbufp->len) { |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1225 | printk(KERN_NOTICE |
| 1226 | "rpc-srv/tcp: %s: %s %d when sending %d bytes " |
| 1227 | "- shutting down socket\n", |
Tom Tucker | 57b1d3b | 2007-12-30 21:08:22 -0600 | [diff] [blame] | 1228 | rqstp->rq_xprt->xpt_server->sv_name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | (sent<0)?"got error":"sent only", |
| 1230 | sent, xbufp->len); |
Tom Tucker | 57b1d3b | 2007-12-30 21:08:22 -0600 | [diff] [blame] | 1231 | set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 1232 | svc_xprt_enqueue(rqstp->rq_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | sent = -EAGAIN; |
| 1234 | } |
| 1235 | return sent; |
| 1236 | } |
| 1237 | |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 1238 | /* |
| 1239 | * Setup response header. TCP has a 4B record length field. |
| 1240 | */ |
| 1241 | static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp) |
| 1242 | { |
| 1243 | struct kvec *resv = &rqstp->rq_res.head[0]; |
| 1244 | |
| 1245 | /* tcp needs a space for the record length... */ |
| 1246 | svc_putnl(resv, 0); |
| 1247 | } |
| 1248 | |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1249 | static struct svc_xprt *svc_tcp_create(struct svc_serv *serv, |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 1250 | struct net *net, |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1251 | struct sockaddr *sa, int salen, |
| 1252 | int flags) |
| 1253 | { |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 1254 | return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1255 | } |
| 1256 | |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1257 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1258 | static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int, |
| 1259 | struct net *, struct sockaddr *, |
| 1260 | int, int); |
| 1261 | static void svc_bc_sock_free(struct svc_xprt *xprt); |
| 1262 | |
| 1263 | static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv, |
| 1264 | struct net *net, |
| 1265 | struct sockaddr *sa, int salen, |
| 1266 | int flags) |
| 1267 | { |
| 1268 | return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags); |
| 1269 | } |
| 1270 | |
| 1271 | static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt) |
| 1272 | { |
| 1273 | } |
| 1274 | |
| 1275 | static struct svc_xprt_ops svc_tcp_bc_ops = { |
| 1276 | .xpo_create = svc_bc_tcp_create, |
| 1277 | .xpo_detach = svc_bc_tcp_sock_detach, |
| 1278 | .xpo_free = svc_bc_sock_free, |
| 1279 | .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr, |
Chuck Lever | 16e4d93 | 2014-05-19 13:40:22 -0400 | [diff] [blame] | 1280 | .xpo_secure_port = svc_sock_secure_port, |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1281 | }; |
| 1282 | |
| 1283 | static struct svc_xprt_class svc_tcp_bc_class = { |
| 1284 | .xcl_name = "tcp-bc", |
| 1285 | .xcl_owner = THIS_MODULE, |
| 1286 | .xcl_ops = &svc_tcp_bc_ops, |
| 1287 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, |
| 1288 | }; |
Andy Adamson | 16b2d1e | 2011-01-06 02:04:27 +0000 | [diff] [blame] | 1289 | |
| 1290 | static void svc_init_bc_xprt_sock(void) |
| 1291 | { |
| 1292 | svc_reg_xprt_class(&svc_tcp_bc_class); |
| 1293 | } |
| 1294 | |
| 1295 | static void svc_cleanup_bc_xprt_sock(void) |
| 1296 | { |
| 1297 | svc_unreg_xprt_class(&svc_tcp_bc_class); |
| 1298 | } |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1299 | #else /* CONFIG_SUNRPC_BACKCHANNEL */ |
Andy Adamson | 16b2d1e | 2011-01-06 02:04:27 +0000 | [diff] [blame] | 1300 | static void svc_init_bc_xprt_sock(void) |
| 1301 | { |
| 1302 | } |
| 1303 | |
| 1304 | static void svc_cleanup_bc_xprt_sock(void) |
| 1305 | { |
| 1306 | } |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1307 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1308 | |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1309 | static struct svc_xprt_ops svc_tcp_ops = { |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1310 | .xpo_create = svc_tcp_create, |
Tom Tucker | 5d13799 | 2007-12-30 21:07:23 -0600 | [diff] [blame] | 1311 | .xpo_recvfrom = svc_tcp_recvfrom, |
| 1312 | .xpo_sendto = svc_tcp_sendto, |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 1313 | .xpo_release_rqst = svc_release_skb, |
Trond Myklebust | 69b6ba3 | 2008-12-23 16:30:11 -0500 | [diff] [blame] | 1314 | .xpo_detach = svc_tcp_sock_detach, |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 1315 | .xpo_free = svc_sock_free, |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 1316 | .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr, |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 1317 | .xpo_has_wspace = svc_tcp_has_wspace, |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 1318 | .xpo_accept = svc_tcp_accept, |
Chuck Lever | 16e4d93 | 2014-05-19 13:40:22 -0400 | [diff] [blame] | 1319 | .xpo_secure_port = svc_sock_secure_port, |
Trond Myklebust | 5187768 | 2014-07-24 23:59:33 -0400 | [diff] [blame] | 1320 | .xpo_adjust_wspace = svc_tcp_adjust_wspace, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1321 | }; |
| 1322 | |
| 1323 | static struct svc_xprt_class svc_tcp_class = { |
| 1324 | .xcl_name = "tcp", |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1325 | .xcl_owner = THIS_MODULE, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1326 | .xcl_ops = &svc_tcp_ops, |
Tom Tucker | 4902315 | 2007-12-30 21:07:21 -0600 | [diff] [blame] | 1327 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, |
Chuck Lever | 3c45ddf | 2014-07-16 15:38:32 -0400 | [diff] [blame] | 1328 | .xcl_ident = XPRT_TRANSPORT_TCP, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1329 | }; |
| 1330 | |
| 1331 | void svc_init_xprt_sock(void) |
| 1332 | { |
| 1333 | svc_reg_xprt_class(&svc_tcp_class); |
| 1334 | svc_reg_xprt_class(&svc_udp_class); |
Andy Adamson | 16b2d1e | 2011-01-06 02:04:27 +0000 | [diff] [blame] | 1335 | svc_init_bc_xprt_sock(); |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | void svc_cleanup_xprt_sock(void) |
| 1339 | { |
| 1340 | svc_unreg_xprt_class(&svc_tcp_class); |
| 1341 | svc_unreg_xprt_class(&svc_udp_class); |
Andy Adamson | 16b2d1e | 2011-01-06 02:04:27 +0000 | [diff] [blame] | 1342 | svc_cleanup_bc_xprt_sock(); |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1343 | } |
| 1344 | |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1345 | static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | { |
| 1347 | struct sock *sk = svsk->sk_sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
Stanislav Kinsbursky | bd4620d | 2011-12-06 14:19:10 +0300 | [diff] [blame] | 1349 | svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class, |
| 1350 | &svsk->sk_xprt, serv); |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame] | 1351 | set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | if (sk->sk_state == TCP_LISTEN) { |
| 1353 | dprintk("setting up TCP socket for listening\n"); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1354 | set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | sk->sk_data_ready = svc_tcp_listen_data_ready; |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1356 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | } else { |
| 1358 | dprintk("setting up TCP socket for reading\n"); |
| 1359 | sk->sk_state_change = svc_tcp_state_change; |
| 1360 | sk->sk_data_ready = svc_tcp_data_ready; |
Trond Myklebust | 47fcb03 | 2009-05-18 17:47:56 -0400 | [diff] [blame] | 1361 | sk->sk_write_space = svc_tcp_write_space; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | svsk->sk_reclen = 0; |
| 1364 | svsk->sk_tcplen = 0; |
J. Bruce Fields | 8af345f | 2012-12-03 16:45:35 -0500 | [diff] [blame] | 1365 | svsk->sk_datalen = 0; |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1366 | memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
Chuck Lever | b7872fe | 2008-04-14 12:27:01 -0400 | [diff] [blame] | 1368 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1370 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1371 | if (sk->sk_state != TCP_ESTABLISHED) |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1372 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
Tom Tucker | 0f0257e | 2007-12-30 21:08:27 -0600 | [diff] [blame] | 1376 | void svc_sock_update_bufs(struct svc_serv *serv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | { |
| 1378 | /* |
| 1379 | * The number of server threads has changed. Update |
| 1380 | * rcvbuf and sndbuf accordingly on all sockets |
| 1381 | */ |
Pavel Emelyanov | 8f3a6de | 2010-10-05 23:30:19 +0400 | [diff] [blame] | 1382 | struct svc_sock *svsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | |
| 1384 | spin_lock_bh(&serv->sv_lock); |
Pavel Emelyanov | 8f3a6de | 2010-10-05 23:30:19 +0400 | [diff] [blame] | 1385 | list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1386 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | spin_unlock_bh(&serv->sv_lock); |
| 1388 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 1389 | EXPORT_SYMBOL_GPL(svc_sock_update_bufs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | |
| 1391 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | * Initialize socket for RPC use and create svc_sock struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | */ |
Chuck Lever | 6b17433 | 2007-02-12 00:53:28 -0800 | [diff] [blame] | 1394 | static struct svc_sock *svc_setup_socket(struct svc_serv *serv, |
| 1395 | struct socket *sock, |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 1396 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | { |
| 1398 | struct svc_sock *svsk; |
| 1399 | struct sock *inet; |
Chuck Lever | 6b17433 | 2007-02-12 00:53:28 -0800 | [diff] [blame] | 1400 | int pmap_register = !(flags & SVC_SOCK_ANONYMOUS); |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 1401 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
| 1403 | dprintk("svc: svc_setup_socket %p\n", sock); |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 1404 | svsk = kzalloc(sizeof(*svsk), GFP_KERNEL); |
| 1405 | if (!svsk) |
| 1406 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
| 1408 | inet = sock->sk; |
| 1409 | |
| 1410 | /* Register socket with portmapper */ |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 1411 | if (pmap_register) |
| 1412 | err = svc_register(serv, sock_net(sock->sk), inet->sk_family, |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 1413 | inet->sk_protocol, |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1414 | ntohs(inet_sk(inet)->inet_sport)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 1416 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | kfree(svsk); |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 1418 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | inet->sk_user_data = svsk; |
| 1422 | svsk->sk_sock = sock; |
| 1423 | svsk->sk_sk = inet; |
| 1424 | svsk->sk_ostate = inet->sk_state_change; |
| 1425 | svsk->sk_odata = inet->sk_data_ready; |
| 1426 | svsk->sk_owspace = inet->sk_write_space; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | |
| 1428 | /* Initialize the socket */ |
| 1429 | if (sock->type == SOCK_DGRAM) |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1430 | svc_udp_init(svsk, serv); |
Olga Kornievskaia | 9660439 | 2008-10-21 14:13:47 -0400 | [diff] [blame] | 1431 | else { |
| 1432 | /* initialise setting must have enough space to |
| 1433 | * receive and respond to one request. |
| 1434 | */ |
| 1435 | svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg, |
| 1436 | 4 * serv->sv_max_mesg); |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1437 | svc_tcp_init(svsk, serv); |
Olga Kornievskaia | 9660439 | 2008-10-21 14:13:47 -0400 | [diff] [blame] | 1438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | dprintk("svc: svc_setup_socket created %p (inet %p)\n", |
| 1441 | svsk, svsk->sk_sk); |
| 1442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | return svsk; |
| 1444 | } |
| 1445 | |
Stanislav Kinsbursky | 3064639 | 2014-02-26 16:50:01 +0300 | [diff] [blame] | 1446 | bool svc_alien_sock(struct net *net, int fd) |
| 1447 | { |
| 1448 | int err; |
| 1449 | struct socket *sock = sockfd_lookup(fd, &err); |
| 1450 | bool ret = false; |
| 1451 | |
| 1452 | if (!sock) |
| 1453 | goto out; |
| 1454 | if (sock_net(sock->sk) != net) |
| 1455 | ret = true; |
| 1456 | sockfd_put(sock); |
| 1457 | out: |
| 1458 | return ret; |
| 1459 | } |
| 1460 | EXPORT_SYMBOL_GPL(svc_alien_sock); |
| 1461 | |
Chuck Lever | bfba9ab | 2009-04-23 19:32:33 -0400 | [diff] [blame] | 1462 | /** |
| 1463 | * svc_addsock - add a listener socket to an RPC service |
| 1464 | * @serv: pointer to RPC service to which to add a new listener |
| 1465 | * @fd: file descriptor of the new listener |
| 1466 | * @name_return: pointer to buffer to fill in with name of listener |
| 1467 | * @len: size of the buffer |
| 1468 | * |
| 1469 | * Fills in socket name and returns positive length of name if successful. |
| 1470 | * Name is terminated with '\n'. On error, returns a negative errno |
| 1471 | * value. |
| 1472 | */ |
| 1473 | int svc_addsock(struct svc_serv *serv, const int fd, char *name_return, |
| 1474 | const size_t len) |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 1475 | { |
| 1476 | int err = 0; |
| 1477 | struct socket *so = sockfd_lookup(fd, &err); |
| 1478 | struct svc_sock *svsk = NULL; |
J. Bruce Fields | a8e1007 | 2012-08-13 18:01:03 -0400 | [diff] [blame] | 1479 | struct sockaddr_storage addr; |
| 1480 | struct sockaddr *sin = (struct sockaddr *)&addr; |
| 1481 | int salen; |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 1482 | |
| 1483 | if (!so) |
| 1484 | return err; |
J. Bruce Fields | a8e1007 | 2012-08-13 18:01:03 -0400 | [diff] [blame] | 1485 | err = -EAFNOSUPPORT; |
Aime Le Rouzic | 205ba42 | 2010-01-26 14:03:56 -0500 | [diff] [blame] | 1486 | if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6)) |
J. Bruce Fields | a8e1007 | 2012-08-13 18:01:03 -0400 | [diff] [blame] | 1487 | goto out; |
| 1488 | err = -EPROTONOSUPPORT; |
| 1489 | if (so->sk->sk_protocol != IPPROTO_TCP && |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 1490 | so->sk->sk_protocol != IPPROTO_UDP) |
J. Bruce Fields | a8e1007 | 2012-08-13 18:01:03 -0400 | [diff] [blame] | 1491 | goto out; |
| 1492 | err = -EISCONN; |
| 1493 | if (so->state > SS_UNCONNECTED) |
| 1494 | goto out; |
| 1495 | err = -ENOENT; |
| 1496 | if (!try_module_get(THIS_MODULE)) |
| 1497 | goto out; |
| 1498 | svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS); |
| 1499 | if (IS_ERR(svsk)) { |
| 1500 | module_put(THIS_MODULE); |
| 1501 | err = PTR_ERR(svsk); |
| 1502 | goto out; |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 1503 | } |
J. Bruce Fields | a8e1007 | 2012-08-13 18:01:03 -0400 | [diff] [blame] | 1504 | if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0) |
| 1505 | svc_xprt_set_local(&svsk->sk_xprt, sin, salen); |
J. Bruce Fields | 39b5530 | 2012-08-14 15:50:34 -0400 | [diff] [blame] | 1506 | svc_add_new_perm_xprt(serv, &svsk->sk_xprt); |
Chuck Lever | e7942b9 | 2009-04-23 19:32:48 -0400 | [diff] [blame] | 1507 | return svc_one_sock_name(svsk, name_return, len); |
J. Bruce Fields | a8e1007 | 2012-08-13 18:01:03 -0400 | [diff] [blame] | 1508 | out: |
| 1509 | sockfd_put(so); |
| 1510 | return err; |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 1511 | } |
| 1512 | EXPORT_SYMBOL_GPL(svc_addsock); |
| 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | /* |
| 1515 | * Create socket for RPC service. |
| 1516 | */ |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1517 | static struct svc_xprt *svc_create_socket(struct svc_serv *serv, |
| 1518 | int protocol, |
Pavel Emelyanov | 62832c0 | 2010-09-29 16:04:18 +0400 | [diff] [blame] | 1519 | struct net *net, |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1520 | struct sockaddr *sin, int len, |
| 1521 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | { |
| 1523 | struct svc_sock *svsk; |
| 1524 | struct socket *sock; |
| 1525 | int error; |
| 1526 | int type; |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 1527 | struct sockaddr_storage addr; |
| 1528 | struct sockaddr *newsin = (struct sockaddr *)&addr; |
| 1529 | int newlen; |
Trond Myklebust | c69da77 | 2009-03-30 18:59:17 -0400 | [diff] [blame] | 1530 | int family; |
| 1531 | int val; |
Pavel Emelyanov | 5216a8e | 2008-02-21 10:57:45 +0300 | [diff] [blame] | 1532 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 1534 | dprintk("svc: svc_create_socket(%s, %d, %s)\n", |
| 1535 | serv->sv_program->pg_name, protocol, |
Chuck Lever | 77f1f67 | 2007-02-12 00:53:39 -0800 | [diff] [blame] | 1536 | __svc_print_addr(sin, buf, sizeof(buf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
| 1538 | if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) { |
| 1539 | printk(KERN_WARNING "svc: only UDP and TCP " |
| 1540 | "sockets supported\n"); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1541 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
Trond Myklebust | c69da77 | 2009-03-30 18:59:17 -0400 | [diff] [blame] | 1544 | type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM; |
| 1545 | switch (sin->sa_family) { |
| 1546 | case AF_INET6: |
| 1547 | family = PF_INET6; |
| 1548 | break; |
| 1549 | case AF_INET: |
| 1550 | family = PF_INET; |
| 1551 | break; |
| 1552 | default: |
| 1553 | return ERR_PTR(-EINVAL); |
| 1554 | } |
| 1555 | |
Pavel Emelyanov | 14ec63c | 2010-09-29 16:06:57 +0400 | [diff] [blame] | 1556 | error = __sock_create(net, family, type, protocol, &sock, 1); |
Chuck Lever | 77f1f67 | 2007-02-12 00:53:39 -0800 | [diff] [blame] | 1557 | if (error < 0) |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1558 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1560 | svc_reclassify_socket(sock); |
| 1561 | |
Trond Myklebust | c69da77 | 2009-03-30 18:59:17 -0400 | [diff] [blame] | 1562 | /* |
| 1563 | * If this is an PF_INET6 listener, we want to avoid |
| 1564 | * getting requests from IPv4 remotes. Those should |
| 1565 | * be shunted to a PF_INET listener via rpcbind. |
| 1566 | */ |
| 1567 | val = 1; |
| 1568 | if (family == PF_INET6) |
| 1569 | kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY, |
| 1570 | (char *)&val, sizeof(val)); |
| 1571 | |
Eric Sesterhenn | 1811474 | 2006-09-28 14:37:07 -0700 | [diff] [blame] | 1572 | if (type == SOCK_STREAM) |
Pavel Emelyanov | 4a17fd5 | 2012-04-19 03:39:36 +0000 | [diff] [blame] | 1573 | sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */ |
Chuck Lever | 77f1f67 | 2007-02-12 00:53:39 -0800 | [diff] [blame] | 1574 | error = kernel_bind(sock, sin, len); |
Eric Sesterhenn | 1811474 | 2006-09-28 14:37:07 -0700 | [diff] [blame] | 1575 | if (error < 0) |
| 1576 | goto bummer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Tom Tucker | 9dbc240 | 2007-12-30 21:08:12 -0600 | [diff] [blame] | 1578 | newlen = len; |
| 1579 | error = kernel_getsockname(sock, newsin, &newlen); |
| 1580 | if (error < 0) |
| 1581 | goto bummer; |
| 1582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (protocol == IPPROTO_TCP) { |
Sridhar Samudrala | e6242e9 | 2006-08-07 20:58:01 -0700 | [diff] [blame] | 1584 | if ((error = kernel_listen(sock, 64)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | goto bummer; |
| 1586 | } |
| 1587 | |
J. Bruce Fields | 72c3537 | 2012-08-13 17:46:17 -0400 | [diff] [blame] | 1588 | svsk = svc_setup_socket(serv, sock, flags); |
J. Bruce Fields | a8e1007 | 2012-08-13 18:01:03 -0400 | [diff] [blame] | 1589 | if (IS_ERR(svsk)) { |
| 1590 | error = PTR_ERR(svsk); |
| 1591 | goto bummer; |
NeilBrown | e79eff1 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 1592 | } |
J. Bruce Fields | a8e1007 | 2012-08-13 18:01:03 -0400 | [diff] [blame] | 1593 | svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen); |
| 1594 | return (struct svc_xprt *)svsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | bummer: |
| 1596 | dprintk("svc: svc_create_socket error = %d\n", -error); |
| 1597 | sock_release(sock); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1598 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /* |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 1602 | * Detach the svc_sock from the socket so that no |
| 1603 | * more callbacks occur. |
| 1604 | */ |
| 1605 | static void svc_sock_detach(struct svc_xprt *xprt) |
| 1606 | { |
| 1607 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
| 1608 | struct sock *sk = svsk->sk_sk; |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 1609 | wait_queue_head_t *wq; |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 1610 | |
| 1611 | dprintk("svc: svc_sock_detach(%p)\n", svsk); |
| 1612 | |
| 1613 | /* put back the old socket callbacks */ |
| 1614 | sk->sk_state_change = svsk->sk_ostate; |
| 1615 | sk->sk_data_ready = svsk->sk_odata; |
| 1616 | sk->sk_write_space = svsk->sk_owspace; |
Trond Myklebust | 69b6ba3 | 2008-12-23 16:30:11 -0500 | [diff] [blame] | 1617 | |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 1618 | wq = sk_sleep(sk); |
J. Bruce Fields | 0442f14 | 2015-10-09 01:44:07 +0000 | [diff] [blame] | 1619 | if (sunrpc_waitqueue_active(wq)) |
Eric Dumazet | eaefd11 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 1620 | wake_up_interruptible(wq); |
Trond Myklebust | 69b6ba3 | 2008-12-23 16:30:11 -0500 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * Disconnect the socket, and reset the callbacks |
| 1625 | */ |
| 1626 | static void svc_tcp_sock_detach(struct svc_xprt *xprt) |
| 1627 | { |
| 1628 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
| 1629 | |
| 1630 | dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk); |
| 1631 | |
| 1632 | svc_sock_detach(xprt); |
| 1633 | |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1634 | if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) { |
| 1635 | svc_tcp_clear_pages(svsk); |
Trond Myklebust | 69b6ba3 | 2008-12-23 16:30:11 -0500 | [diff] [blame] | 1636 | kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR); |
J. Bruce Fields | 31d68ef | 2011-02-24 11:25:33 -0800 | [diff] [blame] | 1637 | } |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Free the svc_sock's socket resources and the svc_sock itself. |
| 1642 | */ |
| 1643 | static void svc_sock_free(struct svc_xprt *xprt) |
| 1644 | { |
| 1645 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
| 1646 | dprintk("svc: svc_sock_free(%p)\n", svsk); |
| 1647 | |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 1648 | if (svsk->sk_sock->file) |
| 1649 | sockfd_put(svsk->sk_sock); |
| 1650 | else |
| 1651 | sock_release(svsk->sk_sock); |
| 1652 | kfree(svsk); |
| 1653 | } |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1654 | |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1655 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1656 | /* |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1657 | * Create a back channel svc_xprt which shares the fore channel socket. |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1658 | */ |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1659 | static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv, |
| 1660 | int protocol, |
| 1661 | struct net *net, |
| 1662 | struct sockaddr *sin, int len, |
| 1663 | int flags) |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1664 | { |
| 1665 | struct svc_sock *svsk; |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1666 | struct svc_xprt *xprt; |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1667 | |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1668 | if (protocol != IPPROTO_TCP) { |
| 1669 | printk(KERN_WARNING "svc: only TCP sockets" |
| 1670 | " supported on shared back channel\n"); |
| 1671 | return ERR_PTR(-EINVAL); |
| 1672 | } |
| 1673 | |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1674 | svsk = kzalloc(sizeof(*svsk), GFP_KERNEL); |
| 1675 | if (!svsk) |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1676 | return ERR_PTR(-ENOMEM); |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1677 | |
| 1678 | xprt = &svsk->sk_xprt; |
Stanislav Kinsbursky | bd4620d | 2011-12-06 14:19:10 +0300 | [diff] [blame] | 1679 | svc_xprt_init(net, &svc_tcp_bc_class, xprt, serv); |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1680 | |
Andy Adamson | 4a19de0 | 2011-01-06 02:04:35 +0000 | [diff] [blame] | 1681 | serv->sv_bc_xprt = xprt; |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1682 | |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1683 | return xprt; |
| 1684 | } |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1685 | |
| 1686 | /* |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1687 | * Free a back channel svc_sock. |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1688 | */ |
Andy Adamson | 1f11a03 | 2011-01-06 02:04:26 +0000 | [diff] [blame] | 1689 | static void svc_bc_sock_free(struct svc_xprt *xprt) |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1690 | { |
Andy Adamson | 778be23 | 2011-01-25 15:38:01 +0000 | [diff] [blame] | 1691 | if (xprt) |
Benny Halevy | 7652e5a | 2009-04-01 09:23:09 -0400 | [diff] [blame] | 1692 | kfree(container_of(xprt, struct svc_sock, sk_xprt)); |
| 1693 | } |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1694 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |