blob: a4fafcbc6ea0b7aa0ffed313ad2935e78b85054b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Tuckerf6150c32007-12-30 21:07:57 -06008 * svc_xprt_enqueue procedure...
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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ärvinen172589c2007-08-28 15:50:33 -070022#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
24#include <linux/errno.h>
25#include <linux/fcntl.h>
26#include <linux/net.h>
27#include <linux/in.h>
28#include <linux/inet.h>
29#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070030#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/unistd.h>
32#include <linux/slab.h>
33#include <linux/netdevice.h>
34#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070035#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/sock.h>
38#include <net/checksum.h>
39#include <net/ip.h>
Chuck Leverb92503b2007-02-12 00:53:36 -080040#include <net/ipv6.h>
Chuck Leverb7872fe2008-04-14 12:27:01 -040041#include <net/tcp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070042#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
44#include <asm/ioctls.h>
45
46#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080047#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/sunrpc/xdr.h>
Chuck Leverc0401ea2008-04-14 12:27:30 -040049#include <linux/sunrpc/msg_prot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/sunrpc/svcsock.h>
51#include <linux/sunrpc/stats.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030052#include <linux/sunrpc/xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Tom Tucker360d87382007-12-30 21:07:17 -060054#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56
57static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
Chuck Lever6b174332007-02-12 00:53:28 -080058 int *errp, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void svc_udp_data_ready(struct sock *, int);
60static int svc_udp_recvfrom(struct svc_rqst *);
61static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060062static void svc_sock_detach(struct svc_xprt *);
Trond Myklebust69b6ba32008-12-23 16:30:11 -050063static void svc_tcp_sock_detach(struct svc_xprt *);
Tom Tucker755ccea2007-12-30 21:07:27 -060064static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Tom Tuckerb700cbb2007-12-30 21:07:42 -060066static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040067 struct net *, struct sockaddr *,
68 int, int);
Andy Adamson1f11a032011-01-06 02:04:26 +000069#if defined(CONFIG_NFS_V4_1)
70static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
71 struct net *, struct sockaddr *,
72 int, int);
73static void svc_bc_sock_free(struct svc_xprt *xprt);
74#endif /* CONFIG_NFS_V4_1 */
75
Peter Zijlstraed075362006-12-06 20:35:24 -080076#ifdef CONFIG_DEBUG_LOCK_ALLOC
77static struct lock_class_key svc_key[2];
78static struct lock_class_key svc_slock_key[2];
79
Tom Tucker0f0257e2007-12-30 21:08:27 -060080static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -080081{
82 struct sock *sk = sock->sk;
John Heffner02b3d342007-09-12 10:42:12 +020083 BUG_ON(sock_owned_by_user(sk));
Peter Zijlstraed075362006-12-06 20:35:24 -080084 switch (sk->sk_family) {
85 case AF_INET:
86 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060087 &svc_slock_key[0],
88 "sk_xprt.xpt_lock-AF_INET-NFSD",
89 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -080090 break;
91
92 case AF_INET6:
93 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -060094 &svc_slock_key[1],
95 "sk_xprt.xpt_lock-AF_INET6-NFSD",
96 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -080097 break;
98
99 default:
100 BUG();
101 }
102}
103#else
Tom Tucker0f0257e2007-12-30 21:08:27 -0600104static void svc_reclassify_socket(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -0800105{
106}
107#endif
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Release an skbuff after use
111 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600112static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Tom Tucker5148bf42007-12-30 21:07:25 -0600114 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (skb) {
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600117 struct svc_sock *svsk =
118 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tucker5148bf42007-12-30 21:07:25 -0600119 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000122 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Chuck Leverb92503b2007-02-12 00:53:36 -0800126union svc_pktinfo_u {
127 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800128 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800129};
David S. Millerbc375ea2007-04-12 13:35:59 -0700130#define SVC_PKTINFO_SPACE \
131 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800132
133static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
134{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600135 struct svc_sock *svsk =
136 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
137 switch (svsk->sk_sk->sk_family) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800138 case AF_INET: {
139 struct in_pktinfo *pki = CMSG_DATA(cmh);
140
141 cmh->cmsg_level = SOL_IP;
142 cmh->cmsg_type = IP_PKTINFO;
143 pki->ipi_ifindex = 0;
144 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
145 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
146 }
147 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800148
Chuck Leverb92503b2007-02-12 00:53:36 -0800149 case AF_INET6: {
150 struct in6_pktinfo *pki = CMSG_DATA(cmh);
151
152 cmh->cmsg_level = SOL_IPV6;
153 cmh->cmsg_type = IPV6_PKTINFO;
154 pki->ipi6_ifindex = 0;
155 ipv6_addr_copy(&pki->ipi6_addr,
156 &rqstp->rq_daddr.addr6);
157 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
158 }
159 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800160 }
Chuck Leverb92503b2007-02-12 00:53:36 -0800161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300164 * send routine intended to be shared by the fore- and back-channel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300166int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
167 struct page *headpage, unsigned long headoffset,
168 struct page *tailpage, unsigned long tailoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int result;
171 int size;
172 struct page **ppage = xdr->pages;
173 size_t base = xdr->page_base;
174 unsigned int pglen = xdr->page_len;
175 unsigned int flags = MSG_MORE;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300176 int slen;
177 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 slen = xdr->len;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* send head */
182 if (slen == xdr->head[0].iov_len)
183 flags = 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300184 len = kernel_sendpage(sock, headpage, headoffset,
NeilBrown44524352006-10-04 02:15:46 -0700185 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (len != xdr->head[0].iov_len)
187 goto out;
188 slen -= xdr->head[0].iov_len;
189 if (slen == 0)
190 goto out;
191
192 /* send page data */
193 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
194 while (pglen > 0) {
195 if (slen == size)
196 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700197 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (result > 0)
199 len += result;
200 if (result != size)
201 goto out;
202 slen -= size;
203 pglen -= size;
204 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
205 base = 0;
206 ppage++;
207 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* send tail */
210 if (xdr->tail[0].iov_len) {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300211 result = kernel_sendpage(sock, tailpage, tailoffset,
212 xdr->tail[0].iov_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (result > 0)
214 len += result;
215 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +0300216
217out:
218 return len;
219}
220
221
222/*
223 * Generic sendto routine
224 */
225static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
226{
227 struct svc_sock *svsk =
228 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
229 struct socket *sock = svsk->sk_sock;
230 union {
231 struct cmsghdr hdr;
232 long all[SVC_PKTINFO_SPACE / sizeof(long)];
233 } buffer;
234 struct cmsghdr *cmh = &buffer.hdr;
235 int len = 0;
236 unsigned long tailoff;
237 unsigned long headoff;
238 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
239
240 if (rqstp->rq_prot == IPPROTO_UDP) {
241 struct msghdr msg = {
242 .msg_name = &rqstp->rq_addr,
243 .msg_namelen = rqstp->rq_addrlen,
244 .msg_control = cmh,
245 .msg_controllen = sizeof(buffer),
246 .msg_flags = MSG_MORE,
247 };
248
249 svc_set_cmsg_data(rqstp, cmh);
250
251 if (sock_sendmsg(sock, &msg, 0) < 0)
252 goto out;
253 }
254
255 tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
256 headoff = 0;
257 len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
258 rqstp->rq_respages[0], tailoff);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800261 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600262 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800263 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 return len;
266}
267
268/*
NeilBrown80212d52006-10-02 02:17:47 -0700269 * Report socket names for nfsdfs
270 */
Chuck Levere7942b92009-04-23 19:32:48 -0400271static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
NeilBrown80212d52006-10-02 02:17:47 -0700272{
Chuck Lever017cb472009-04-23 19:33:03 -0400273 const struct sock *sk = svsk->sk_sk;
274 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
275 "udp" : "tcp";
NeilBrown80212d52006-10-02 02:17:47 -0700276 int len;
277
Chuck Lever017cb472009-04-23 19:33:03 -0400278 switch (sk->sk_family) {
Chuck Levere7942b92009-04-23 19:32:48 -0400279 case PF_INET:
280 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400281 proto_name,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000282 &inet_sk(sk)->inet_rcv_saddr,
283 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700284 break;
Chuck Lever58de2f82009-04-23 19:32:55 -0400285 case PF_INET6:
286 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400287 proto_name,
288 &inet6_sk(sk)->rcv_saddr,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000289 inet_sk(sk)->inet_num);
NeilBrown80212d52006-10-02 02:17:47 -0700290 break;
291 default:
Chuck Levere7942b92009-04-23 19:32:48 -0400292 len = snprintf(buf, remaining, "*unknown-%d*\n",
Chuck Lever017cb472009-04-23 19:33:03 -0400293 sk->sk_family);
NeilBrown80212d52006-10-02 02:17:47 -0700294 }
Chuck Levere7942b92009-04-23 19:32:48 -0400295
296 if (len >= remaining) {
297 *buf = '\0';
298 return -ENAMETOOLONG;
NeilBrown80212d52006-10-02 02:17:47 -0700299 }
300 return len;
301}
302
Chuck Lever8435d342009-04-23 19:32:40 -0400303/**
304 * svc_sock_names - construct a list of listener names in a string
305 * @serv: pointer to RPC service
306 * @buf: pointer to a buffer to fill in with socket names
307 * @buflen: size of the buffer to be filled
308 * @toclose: pointer to '\0'-terminated C string containing the name
309 * of a listener to be closed
310 *
311 * Fills in @buf with a '\n'-separated list of names of listener
312 * sockets. If @toclose is not NULL, the socket named by @toclose
313 * is closed, and is not included in the output list.
314 *
315 * Returns positive length of the socket name string, or a negative
316 * errno value on error.
317 */
318int svc_sock_names(struct svc_serv *serv, char *buf, const size_t buflen,
319 const char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700320{
NeilBrownb41b66d2006-10-02 02:17:48 -0700321 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700322 int len = 0;
323
324 if (!serv)
325 return 0;
Chuck Levere7942b92009-04-23 19:32:48 -0400326
NeilBrownaaf68cf2007-02-08 14:20:30 -0800327 spin_lock_bh(&serv->sv_lock);
Tom Tucker7a182082007-12-30 21:07:53 -0600328 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
Chuck Levere7942b92009-04-23 19:32:48 -0400329 int onelen = svc_one_sock_name(svsk, buf + len, buflen - len);
330 if (onelen < 0) {
331 len = onelen;
332 break;
333 }
NeilBrown39423022010-11-15 11:27:01 +1100334 if (toclose && strcmp(toclose, buf + len) == 0) {
NeilBrownb41b66d2006-10-02 02:17:48 -0700335 closesk = svsk;
NeilBrown39423022010-11-15 11:27:01 +1100336 svc_xprt_get(&closesk->sk_xprt);
337 } else
NeilBrownb41b66d2006-10-02 02:17:48 -0700338 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700339 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800340 spin_unlock_bh(&serv->sv_lock);
Chuck Levere7942b92009-04-23 19:32:48 -0400341
NeilBrown39423022010-11-15 11:27:01 +1100342 if (closesk) {
NeilBrown5680c442006-10-04 02:15:45 -0700343 /* Should unregister with portmap, but you cannot
344 * unregister just one protocol...
345 */
Tom Tucker7a182082007-12-30 21:07:53 -0600346 svc_close_xprt(&closesk->sk_xprt);
NeilBrown39423022010-11-15 11:27:01 +1100347 svc_xprt_put(&closesk->sk_xprt);
348 } else if (toclose)
NeilBrown37a03472006-10-04 02:15:44 -0700349 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700350 return len;
351}
Trond Myklebust24c37672008-12-23 16:30:12 -0500352EXPORT_SYMBOL_GPL(svc_sock_names);
NeilBrown80212d52006-10-02 02:17:47 -0700353
354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * Check input queue length
356 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600357static int svc_recv_available(struct svc_sock *svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct socket *sock = svsk->sk_sock;
360 int avail, err;
361
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700362 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 return (err >= 0)? avail : err;
365}
366
367/*
368 * Generic recvfrom routine.
369 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600370static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr,
371 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600373 struct svc_sock *svsk =
374 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Chuck Lever1ba95102007-02-12 00:53:31 -0800375 struct msghdr msg = {
376 .msg_flags = MSG_DONTWAIT,
377 };
378 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Tom Tucker260c1d12007-12-30 21:08:29 -0600380 rqstp->rq_xprt_hlen = 0;
381
Chuck Lever1ba95102007-02-12 00:53:31 -0800382 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
383 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800386 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return len;
388}
389
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800390static int svc_partial_recvfrom(struct svc_rqst *rqstp,
391 struct kvec *iov, int nr,
392 int buflen, unsigned int base)
393{
394 size_t save_iovlen;
395 void __user *save_iovbase;
396 unsigned int i;
397 int ret;
398
399 if (base == 0)
400 return svc_recvfrom(rqstp, iov, nr, buflen);
401
402 for (i = 0; i < nr; i++) {
403 if (iov[i].iov_len > base)
404 break;
405 base -= iov[i].iov_len;
406 }
407 save_iovlen = iov[i].iov_len;
408 save_iovbase = iov[i].iov_base;
409 iov[i].iov_len -= base;
410 iov[i].iov_base += base;
411 ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
412 iov[i].iov_len = save_iovlen;
413 iov[i].iov_base = save_iovbase;
414 return ret;
415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*
418 * Set socket snd and rcv buffer lengths
419 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600420static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
421 unsigned int rcv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423#if 0
424 mm_segment_t oldfs;
425 oldfs = get_fs(); set_fs(KERNEL_DS);
426 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
427 (char*)&snd, sizeof(snd));
428 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
429 (char*)&rcv, sizeof(rcv));
430#else
431 /* sock_setsockopt limits use to sysctl_?mem_max,
432 * which isn't acceptable. Until that is made conditional
433 * on not having CAP_SYS_RESOURCE or similar, we go direct...
434 * DaveM said I could!
435 */
436 lock_sock(sock->sk);
437 sock->sk->sk_sndbuf = snd * 2;
438 sock->sk->sk_rcvbuf = rcv * 2;
J. Bruce Fields7f421832009-05-27 18:51:06 -0400439 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
Trond Myklebust47fcb032009-05-18 17:47:56 -0400440 sock->sk->sk_write_space(sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 release_sock(sock->sk);
442#endif
443}
444/*
445 * INET callback when data has been received on the socket.
446 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600447static void svc_udp_data_ready(struct sock *sk, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Neil Brown939bb7e2005-09-13 01:25:39 -0700449 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000450 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Neil Brown939bb7e2005-09-13 01:25:39 -0700452 if (svsk) {
453 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600454 svsk, sk, count,
455 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
456 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600457 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700458 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000459 if (wq && waitqueue_active(wq))
460 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/*
464 * INET callback when space is newly available on the socket.
465 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600466static void svc_write_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000469 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (svsk) {
472 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600473 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Tom Tuckerf6150c32007-12-30 21:07:57 -0600474 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Eric Dumazeteaefd112011-02-18 03:26:36 +0000477 if (wq && waitqueue_active(wq)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700478 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 svsk);
Eric Dumazeteaefd112011-02-18 03:26:36 +0000480 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482}
483
Trond Myklebust47fcb032009-05-18 17:47:56 -0400484static void svc_tcp_write_space(struct sock *sk)
485{
486 struct socket *sock = sk->sk_socket;
487
488 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock)
489 clear_bit(SOCK_NOSPACE, &sock->flags);
490 svc_write_space(sk);
491}
492
Tom Tucker9dbc2402007-12-30 21:08:12 -0600493/*
Chuck Lever7702ce42009-07-13 10:54:26 -0400494 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
495 */
496static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
497 struct cmsghdr *cmh)
498{
499 struct in_pktinfo *pki = CMSG_DATA(cmh);
500 if (cmh->cmsg_type != IP_PKTINFO)
501 return 0;
502 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
503 return 1;
504}
505
506/*
507 * See net/ipv6/datagram.c : datagram_recv_ctl
508 */
509static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
510 struct cmsghdr *cmh)
511{
512 struct in6_pktinfo *pki = CMSG_DATA(cmh);
513 if (cmh->cmsg_type != IPV6_PKTINFO)
514 return 0;
515 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
516 return 1;
517}
518
519/*
Tom Tucker9dbc2402007-12-30 21:08:12 -0600520 * Copy the UDP datagram's destination address to the rqstp structure.
521 * The 'destination' address in this case is the address to which the
522 * peer sent the datagram, i.e. our local address. For multihomed
523 * hosts, this can change from msg to msg. Note that only the IP
524 * address changes, the port number should remain the same.
525 */
Chuck Lever7702ce42009-07-13 10:54:26 -0400526static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
527 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800528{
Chuck Lever7702ce42009-07-13 10:54:26 -0400529 switch (cmh->cmsg_level) {
530 case SOL_IP:
531 return svc_udp_get_dest_address4(rqstp, cmh);
532 case SOL_IPV6:
533 return svc_udp_get_dest_address6(rqstp, cmh);
Chuck Lever95756482007-02-12 00:53:38 -0800534 }
Chuck Lever7702ce42009-07-13 10:54:26 -0400535
536 return 0;
Chuck Lever95756482007-02-12 00:53:38 -0800537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/*
540 * Receive a datagram from a UDP socket.
541 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600542static int svc_udp_recvfrom(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Tom Tucker57b1d3b2007-12-30 21:08:22 -0600544 struct svc_sock *svsk =
545 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600546 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700548 union {
549 struct cmsghdr hdr;
550 long all[SVC_PKTINFO_SPACE / sizeof(long)];
551 } buffer;
552 struct cmsghdr *cmh = &buffer.hdr;
NeilBrown7a37f572007-03-06 01:42:21 -0800553 struct msghdr msg = {
554 .msg_name = svc_addr(rqstp),
555 .msg_control = cmh,
556 .msg_controllen = sizeof(buffer),
557 .msg_flags = MSG_DONTWAIT,
558 };
Chuck Leverabc5c442009-04-23 19:31:25 -0400559 size_t len;
560 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Tom Tucker02fc6c32007-12-30 21:07:48 -0600562 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* udp sockets need large rcvbuf as all pending
564 * requests are still in that buffer. sndbuf must
565 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700566 * for one reply per thread. We count all threads
567 * rather than threads in a particular pool, which
568 * provides an upper bound on the number of threads
569 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
571 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700572 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
573 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Tom Tucker02fc6c32007-12-30 21:07:48 -0600575 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700576 skb = NULL;
577 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
578 0, 0, MSG_PEEK | MSG_DONTWAIT);
579 if (err >= 0)
580 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
581
582 if (skb == NULL) {
583 if (err != -EAGAIN) {
584 /* possibly an icmp error */
585 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600586 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
NeilBrown05ed6902007-05-09 02:34:55 -0700588 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600590 len = svc_addr_len(svc_addr(rqstp));
Chuck Leverabc5c442009-04-23 19:31:25 -0400591 if (len == 0)
592 return -EAFNOSUPPORT;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600593 rqstp->rq_addrlen = len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700594 if (skb->tstamp.tv64 == 0) {
595 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800596 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 need that much accuracy */
598 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700599 svsk->sk_sk->sk_stamp = skb->tstamp;
Tom Tucker02fc6c32007-12-30 21:07:48 -0600600 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 len = skb->len - sizeof(struct udphdr);
603 rqstp->rq_arg.len = len;
604
Chuck Lever95756482007-02-12 00:53:38 -0800605 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Chuck Lever7702ce42009-07-13 10:54:26 -0400607 if (!svc_udp_get_dest_address(rqstp, cmh)) {
NeilBrown7a37f572007-03-06 01:42:21 -0800608 if (net_ratelimit())
Chuck Lever7702ce42009-07-13 10:54:26 -0400609 printk(KERN_WARNING
610 "svc: received unknown control message %d/%d; "
611 "dropping RPC reply datagram\n",
612 cmh->cmsg_level, cmh->cmsg_type);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000613 skb_free_datagram_locked(svsk->sk_sk, skb);
NeilBrown7a37f572007-03-06 01:42:21 -0800614 return 0;
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (skb_is_nonlinear(skb)) {
618 /* we have to copy */
619 local_bh_disable();
620 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
621 local_bh_enable();
622 /* checksum error */
Eric Dumazet9d410c72009-10-30 05:03:53 +0000623 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return 0;
625 }
626 local_bh_enable();
Eric Dumazet9d410c72009-10-30 05:03:53 +0000627 skb_free_datagram_locked(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 } else {
629 /* we can use it in-place */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600630 rqstp->rq_arg.head[0].iov_base = skb->data +
631 sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800633 if (skb_checksum_complete(skb)) {
Eric Dumazet9d410c72009-10-30 05:03:53 +0000634 skb_free_datagram_locked(svsk->sk_sk, skb);
Herbert Xufb286bb2005-11-10 13:01:24 -0800635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Tom Tucker5148bf42007-12-30 21:07:25 -0600637 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 rqstp->rq_arg.page_base = 0;
641 if (len <= rqstp->rq_arg.head[0].iov_len) {
642 rqstp->rq_arg.head[0].iov_len = len;
643 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700644 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 } else {
646 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700647 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700648 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
651 if (serv->sv_stats)
652 serv->sv_stats->netudpcnt++;
653
654 return len;
655}
656
657static int
658svc_udp_sendto(struct svc_rqst *rqstp)
659{
660 int error;
661
662 error = svc_sendto(rqstp, &rqstp->rq_res);
663 if (error == -ECONNREFUSED)
664 /* ICMP error on earlier request. */
665 error = svc_sendto(rqstp, &rqstp->rq_res);
666
667 return error;
668}
669
Tom Tuckere831fe62007-12-30 21:07:29 -0600670static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
671{
672}
673
Tom Tucker323bee32007-12-30 21:07:31 -0600674static int svc_udp_has_wspace(struct svc_xprt *xprt)
675{
676 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600677 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600678 unsigned long required;
679
680 /*
681 * Set the SOCK_NOSPACE flag before checking the available
682 * sock space.
683 */
684 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600685 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600686 if (required*2 > sock_wspace(svsk->sk_sk))
687 return 0;
688 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
689 return 1;
690}
691
Tom Tucker38a417c2007-12-30 21:07:36 -0600692static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
693{
694 BUG();
695 return NULL;
696}
697
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600698static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400699 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600700 struct sockaddr *sa, int salen,
701 int flags)
702{
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400703 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600704}
705
Tom Tucker360d87382007-12-30 21:07:17 -0600706static struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600707 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600708 .xpo_recvfrom = svc_udp_recvfrom,
709 .xpo_sendto = svc_udp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -0600710 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600711 .xpo_detach = svc_sock_detach,
712 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600713 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600714 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600715 .xpo_accept = svc_udp_accept,
Tom Tucker360d87382007-12-30 21:07:17 -0600716};
717
718static struct svc_xprt_class svc_udp_class = {
719 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600720 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600721 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600722 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600723};
724
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600725static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Chuck Lever7702ce42009-07-13 10:54:26 -0400727 int err, level, optname, one = 1;
NeilBrown7a37f572007-03-06 01:42:21 -0800728
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600729 svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600730 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
732 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800735 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * svc_udp_recvfrom will re-adjust if necessary
737 */
738 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600739 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
740 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Tom Tucker0f0257e2007-12-30 21:08:27 -0600742 /* data might have come in before data_ready set up */
743 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600744 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800745
NeilBrown7a37f572007-03-06 01:42:21 -0800746 /* make sure we get destination address info */
Chuck Lever7702ce42009-07-13 10:54:26 -0400747 switch (svsk->sk_sk->sk_family) {
748 case AF_INET:
749 level = SOL_IP;
750 optname = IP_PKTINFO;
751 break;
752 case AF_INET6:
753 level = SOL_IPV6;
754 optname = IPV6_RECVPKTINFO;
755 break;
756 default:
757 BUG();
758 }
759 err = kernel_setsockopt(svsk->sk_sock, level, optname,
760 (char *)&one, sizeof(one));
761 dprintk("svc: kernel_setsockopt returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764/*
765 * A data_ready event on a listening socket means there's a connection
766 * pending. Do not use state_change as a substitute for it.
767 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600768static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Neil Brown939bb7e2005-09-13 01:25:39 -0700770 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000771 wait_queue_head_t *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700774 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Neil Brown939bb7e2005-09-13 01:25:39 -0700776 /*
777 * This callback may called twice when a new connection
778 * is established as a child socket inherits everything
779 * from a parent LISTEN socket.
780 * 1) data_ready method of the parent socket will be called
781 * when one of child sockets become ESTABLISHED.
782 * 2) data_ready method of the child socket may be called
783 * when it receives data before the socket is accepted.
784 * In case of 2, we should ignore it silently.
785 */
786 if (sk->sk_state == TCP_LISTEN) {
787 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600788 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600789 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700790 } else
791 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700793
Eric Dumazeteaefd112011-02-18 03:26:36 +0000794 wq = sk_sleep(sk);
795 if (wq && waitqueue_active(wq))
796 wake_up_interruptible_all(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/*
800 * A state change on a connected socket means it's dying or dead.
801 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600802static void svc_tcp_state_change(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Neil Brown939bb7e2005-09-13 01:25:39 -0700804 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000805 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700808 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Neil Brown939bb7e2005-09-13 01:25:39 -0700810 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700812 else {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600813 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600814 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000816 if (wq && waitqueue_active(wq))
817 wake_up_interruptible_all(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Tom Tucker0f0257e2007-12-30 21:08:27 -0600820static void svc_tcp_data_ready(struct sock *sk, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Neil Brown939bb7e2005-09-13 01:25:39 -0700822 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Eric Dumazeteaefd112011-02-18 03:26:36 +0000823 wait_queue_head_t *wq = sk_sleep(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700826 sk, sk->sk_user_data);
827 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600828 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600829 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700830 }
Eric Dumazeteaefd112011-02-18 03:26:36 +0000831 if (wq && waitqueue_active(wq))
832 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/*
836 * Accept a TCP connection
837 */
Tom Tucker38a417c2007-12-30 21:07:36 -0600838static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Tom Tucker38a417c2007-12-30 21:07:36 -0600840 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800841 struct sockaddr_storage addr;
842 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600843 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct socket *sock = svsk->sk_sock;
845 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct svc_sock *newsvsk;
847 int err, slen;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300848 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
851 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -0600852 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Tom Tucker02fc6c32007-12-30 21:07:48 -0600854 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700855 err = kernel_accept(sock, &newsock, O_NONBLOCK);
856 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (err == -ENOMEM)
858 printk(KERN_WARNING "%s: no more sockets!\n",
859 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700860 else if (err != -EAGAIN && net_ratelimit())
861 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
862 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -0600863 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Tom Tucker02fc6c32007-12-30 21:07:48 -0600865 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800867 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (err < 0) {
869 if (net_ratelimit())
870 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
871 serv->sv_name, -err);
872 goto failed; /* aborted connection or whatever */
873 }
874
875 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800876 * hosts here, but when we get encryption, the IP of the host won't
877 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800879 if (!svc_port_is_privileged(sin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -0800881 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800882 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800883 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800885 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -0800886 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /* make sure that a write doesn't block forever when
889 * low on memory
890 */
891 newsock->sk->sk_sndtimeo = HZ*30;
892
Chuck Lever6b174332007-02-12 00:53:28 -0800893 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
894 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600896 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Frank van Maarseveena9747692007-07-09 22:21:39 +0200897 err = kernel_getsockname(newsock, sin, &slen);
898 if (unlikely(err < 0)) {
899 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
900 slen = offsetof(struct sockaddr, sa_data);
901 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600902 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -0800903
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600904 if (serv->sv_stats)
905 serv->sv_stats->nettcpconn++;
906
907 return &newsvsk->sk_xprt;
908
909failed:
910 sock_release(newsock);
911 return NULL;
912}
913
J. Bruce Fields31d68ef2011-02-24 11:25:33 -0800914static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
915{
916 unsigned int i, len, npages;
917
918 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
919 return 0;
920 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
921 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
922 for (i = 0; i < npages; i++) {
923 if (rqstp->rq_pages[i] != NULL)
924 put_page(rqstp->rq_pages[i]);
925 BUG_ON(svsk->sk_pages[i] == NULL);
926 rqstp->rq_pages[i] = svsk->sk_pages[i];
927 svsk->sk_pages[i] = NULL;
928 }
929 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
930 return len;
931}
932
933static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
934{
935 unsigned int i, len, npages;
936
937 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
938 return;
939 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
940 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
941 for (i = 0; i < npages; i++) {
942 svsk->sk_pages[i] = rqstp->rq_pages[i];
943 rqstp->rq_pages[i] = NULL;
944 }
945}
946
947static void svc_tcp_clear_pages(struct svc_sock *svsk)
948{
949 unsigned int i, len, npages;
950
951 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
952 goto out;
953 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
954 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
955 for (i = 0; i < npages; i++) {
956 BUG_ON(svsk->sk_pages[i] == NULL);
957 put_page(svsk->sk_pages[i]);
958 svsk->sk_pages[i] = NULL;
959 }
960out:
961 svsk->sk_tcplen = 0;
962}
963
Tom Tuckerf9f3cc42007-12-30 21:07:40 -0600964/*
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300965 * Receive data.
966 * If we haven't gotten the record length yet, get the next four bytes.
967 * Otherwise try to gobble up as much as possible up to the complete
968 * record length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300970static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600972 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400973 unsigned int want;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +0300974 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
J. Bruce Fields7f421832009-05-27 18:51:06 -0400976 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
977 /* sndbuf needs to have room for one request
978 * per thread, otherwise we can stall even when the
979 * network isn't a bottleneck.
980 *
981 * We count all threads rather than threads in a
982 * particular pool, which provides an upper bound
983 * on the number of threads which will access the socket.
984 *
985 * rcvbuf just needs to be able to hold a few requests.
986 * Normally they will be removed from the queue
987 * as soon a a complete request arrives.
988 */
989 svc_sock_setbufsize(svsk->sk_sock,
990 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
991 3 * serv->sv_max_mesg);
992
Tom Tucker02fc6c32007-12-30 21:07:48 -0600993 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Chuck Leverc0401ea2008-04-14 12:27:30 -0400995 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct kvec iov;
997
Trond Myklebust5ee78d42009-05-18 17:47:56 -0400998 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1000 iov.iov_len = want;
1001 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1002 goto error;
1003 svsk->sk_tcplen += len;
1004
1005 if (len < want) {
Chuck Leverc0401ea2008-04-14 12:27:30 -04001006 dprintk("svc: short recvfrom while reading record "
1007 "length (%d of %d)\n", len, want);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001008 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
1011 svsk->sk_reclen = ntohl(svsk->sk_reclen);
Chuck Leverc0401ea2008-04-14 12:27:30 -04001012 if (!(svsk->sk_reclen & RPC_LAST_STREAM_FRAGMENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* FIXME: technically, a record can be fragmented,
1014 * and non-terminal fragments will not have the top
1015 * bit set in the fragment length header.
1016 * But apparently no known nfs clients send fragmented
1017 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001018 if (net_ratelimit())
Chuck Leverc0401ea2008-04-14 12:27:30 -04001019 printk(KERN_NOTICE "RPC: multiple fragments "
1020 "per record not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto err_delete;
1022 }
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001023
Chuck Leverc0401ea2008-04-14 12:27:30 -04001024 svsk->sk_reclen &= RPC_FRAGMENT_SIZE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001026 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001027 if (net_ratelimit())
Chuck Leverc0401ea2008-04-14 12:27:30 -04001028 printk(KERN_NOTICE "RPC: "
1029 "fragment too large: 0x%08lx\n",
1030 (unsigned long)svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 goto err_delete;
1032 }
1033 }
1034
J. Bruce Fieldscc6c2122011-02-14 15:03:35 -05001035 if (svsk->sk_reclen < 8)
1036 goto err_delete; /* client is nuts. */
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 len = svsk->sk_reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001040 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001041error:
1042 dprintk("RPC: TCP recv_record got %d\n", len);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001043 return len;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001044err_delete:
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001045 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001046 return -EAGAIN;
1047}
1048
Trond Myklebust586c52c2009-05-18 17:47:56 -04001049static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001050{
Trond Myklebust586c52c2009-05-18 17:47:56 -04001051 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001052 struct rpc_rqst *req = NULL;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001053 struct kvec *src, *dst;
1054 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
J. Bruce Fields48e65552011-02-14 14:52:03 -05001055 __be32 xid;
1056 __be32 calldir;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001057
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001058 xid = *p++;
1059 calldir = *p;
1060
Trond Myklebust586c52c2009-05-18 17:47:56 -04001061 if (bc_xprt)
1062 req = xprt_lookup_rqst(bc_xprt, xid);
J. Bruce Fieldsd75faea2010-11-30 19:15:01 -05001063
Trond Myklebust586c52c2009-05-18 17:47:56 -04001064 if (!req) {
1065 printk(KERN_NOTICE
1066 "%s: Got unrecognized reply: "
1067 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1068 __func__, ntohl(calldir),
1069 bc_xprt, xid);
1070 return -EAGAIN;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001071 }
Trond Myklebust586c52c2009-05-18 17:47:56 -04001072
1073 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1074 /*
1075 * XXX!: cheating for now! Only copying HEAD.
1076 * But we know this is good enough for now (in fact, for any
1077 * callback reply in the forseeable future).
1078 */
1079 dst = &req->rq_private_buf.head[0];
1080 src = &rqstp->rq_arg.head[0];
1081 if (dst->iov_len < src->iov_len)
1082 return -EAGAIN; /* whatever; just giving up. */
1083 memcpy(dst->iov_base, src->iov_base, src->iov_len);
1084 xprt_complete_rqst(req->rq_task, svsk->sk_reclen);
1085 rqstp->rq_arg.len = 0;
1086 return 0;
1087}
1088
1089static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1090{
1091 int i = 0;
1092 int t = 0;
1093
1094 while (t < len) {
1095 vec[i].iov_base = page_address(pages[i]);
1096 vec[i].iov_len = PAGE_SIZE;
1097 i++;
1098 t += PAGE_SIZE;
1099 }
1100 return i;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001101}
1102
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001103
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001104/*
1105 * Receive data from a TCP socket.
1106 */
1107static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1108{
1109 struct svc_sock *svsk =
1110 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1111 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1112 int len;
1113 struct kvec *vec;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001114 unsigned int want, base;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001115 __be32 *p;
1116 __be32 calldir;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001117 int pnum;
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001118
1119 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1120 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1121 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1122 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1123
1124 len = svc_tcp_recv_record(svsk, rqstp);
1125 if (len < 0)
1126 goto error;
1127
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001128 base = svc_tcp_restore_pages(svsk, rqstp);
1129 want = svsk->sk_reclen - base;
1130
NeilBrown3cc03b12006-10-04 02:15:47 -07001131 vec = rqstp->rq_vec;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001132
Trond Myklebust586c52c2009-05-18 17:47:56 -04001133 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
1134 svsk->sk_reclen);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03001135
NeilBrown44524352006-10-04 02:15:46 -07001136 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /* Now receive data */
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001139 len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
1140 if (len >= 0)
1141 svsk->sk_tcplen += len;
1142 if (len != want) {
1143 if (len < 0 && len != -EAGAIN)
1144 goto err_other;
1145 svc_tcp_save_pages(svsk, rqstp);
1146 dprintk("svc: incomplete TCP record (%d of %d)\n",
1147 svsk->sk_tcplen, svsk->sk_reclen);
1148 goto err_noclose;
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001151 rqstp->rq_arg.len = svsk->sk_reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 rqstp->rq_arg.page_base = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001153 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1154 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 rqstp->rq_arg.page_len = 0;
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001156 } else
1157 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Tom Tucker5148bf42007-12-30 21:07:25 -06001159 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 rqstp->rq_prot = IPPROTO_TCP;
1161
Trond Myklebust586c52c2009-05-18 17:47:56 -04001162 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1163 calldir = p[1];
1164 if (calldir) {
1165 len = receive_cb_reply(svsk, rqstp);
1166 if (len < 0)
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001167 goto error;
Trond Myklebust586c52c2009-05-18 17:47:56 -04001168 }
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /* Reset TCP read info */
1171 svsk->sk_reclen = 0;
1172 svsk->sk_tcplen = 0;
Trond Myklebust0601f792009-05-18 17:47:56 -04001173 /* If we have more data, signal svc_xprt_enqueue() to try again */
1174 if (svc_recv_available(svsk) > sizeof(rpc_fraghdr))
1175 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Tom Tucker9dbc2402007-12-30 21:08:12 -06001178 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (serv->sv_stats)
1180 serv->sv_stats->nettcpcnt++;
1181
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001182 dprintk("svc: TCP complete record (%d bytes)\n", rqstp->rq_arg.len);
Trond Myklebust5ee78d42009-05-18 17:47:56 -04001183 return rqstp->rq_arg.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001185error:
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001186 if (len != -EAGAIN)
1187 goto err_other;
1188 dprintk("RPC: TCP recvfrom got EAGAIN\n");
Alexandros Batsakis8f55f3c2009-08-20 03:34:19 +03001189 return -EAGAIN;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001190err_other:
1191 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1192 svsk->sk_xprt.xpt_server->sv_name, -len);
1193 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1194err_noclose:
1195 return -EAGAIN; /* record not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198/*
1199 * Send out data on TCP socket.
1200 */
Tom Tucker0f0257e2007-12-30 21:08:27 -06001201static int svc_tcp_sendto(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct xdr_buf *xbufp = &rqstp->rq_res;
1204 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001205 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /* Set up the first element of the reply kvec.
1208 * Any other kvecs that may be in use have been taken
1209 * care of by the server implementation itself.
1210 */
1211 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1212 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sent = svc_sendto(rqstp, &rqstp->rq_res);
1215 if (sent != xbufp->len) {
Tom Tucker0f0257e2007-12-30 21:08:27 -06001216 printk(KERN_NOTICE
1217 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1218 "- shutting down socket\n",
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001219 rqstp->rq_xprt->xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 (sent<0)?"got error":"sent only",
1221 sent, xbufp->len);
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001222 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001223 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 sent = -EAGAIN;
1225 }
1226 return sent;
1227}
1228
Tom Tuckere831fe62007-12-30 21:07:29 -06001229/*
1230 * Setup response header. TCP has a 4B record length field.
1231 */
1232static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1233{
1234 struct kvec *resv = &rqstp->rq_res.head[0];
1235
1236 /* tcp needs a space for the record length... */
1237 svc_putnl(resv, 0);
1238}
1239
Tom Tucker323bee32007-12-30 21:07:31 -06001240static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1241{
Tom Tucker57b1d3b2007-12-30 21:08:22 -06001242 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Trond Myklebust47fcb032009-05-18 17:47:56 -04001243 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -06001244 int required;
Tom Tucker323bee32007-12-30 21:07:31 -06001245
Trond Myklebust47fcb032009-05-18 17:47:56 -04001246 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
1247 return 1;
1248 required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
1249 if (sk_stream_wspace(svsk->sk_sk) >= required)
1250 return 1;
Tom Tucker323bee32007-12-30 21:07:31 -06001251 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Trond Myklebust47fcb032009-05-18 17:47:56 -04001252 return 0;
Tom Tucker323bee32007-12-30 21:07:31 -06001253}
1254
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001255static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001256 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001257 struct sockaddr *sa, int salen,
1258 int flags)
1259{
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001260 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001261}
1262
Andy Adamson1f11a032011-01-06 02:04:26 +00001263#if defined(CONFIG_NFS_V4_1)
1264static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1265 struct net *, struct sockaddr *,
1266 int, int);
1267static void svc_bc_sock_free(struct svc_xprt *xprt);
1268
1269static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1270 struct net *net,
1271 struct sockaddr *sa, int salen,
1272 int flags)
1273{
1274 return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1275}
1276
1277static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1278{
1279}
1280
1281static struct svc_xprt_ops svc_tcp_bc_ops = {
1282 .xpo_create = svc_bc_tcp_create,
1283 .xpo_detach = svc_bc_tcp_sock_detach,
1284 .xpo_free = svc_bc_sock_free,
1285 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1286};
1287
1288static struct svc_xprt_class svc_tcp_bc_class = {
1289 .xcl_name = "tcp-bc",
1290 .xcl_owner = THIS_MODULE,
1291 .xcl_ops = &svc_tcp_bc_ops,
1292 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1293};
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001294
1295static void svc_init_bc_xprt_sock(void)
1296{
1297 svc_reg_xprt_class(&svc_tcp_bc_class);
1298}
1299
1300static void svc_cleanup_bc_xprt_sock(void)
1301{
1302 svc_unreg_xprt_class(&svc_tcp_bc_class);
1303}
1304#else /* CONFIG_NFS_V4_1 */
1305static void svc_init_bc_xprt_sock(void)
1306{
1307}
1308
1309static void svc_cleanup_bc_xprt_sock(void)
1310{
1311}
Andy Adamson1f11a032011-01-06 02:04:26 +00001312#endif /* CONFIG_NFS_V4_1 */
1313
Tom Tucker360d87382007-12-30 21:07:17 -06001314static struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001315 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001316 .xpo_recvfrom = svc_tcp_recvfrom,
1317 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001318 .xpo_release_rqst = svc_release_skb,
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001319 .xpo_detach = svc_tcp_sock_detach,
Tom Tucker755ccea2007-12-30 21:07:27 -06001320 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001321 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001322 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001323 .xpo_accept = svc_tcp_accept,
Tom Tucker360d87382007-12-30 21:07:17 -06001324};
1325
1326static struct svc_xprt_class svc_tcp_class = {
1327 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001328 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001329 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001330 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001331};
1332
1333void svc_init_xprt_sock(void)
1334{
1335 svc_reg_xprt_class(&svc_tcp_class);
1336 svc_reg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001337 svc_init_bc_xprt_sock();
Tom Tucker360d87382007-12-30 21:07:17 -06001338}
1339
1340void svc_cleanup_xprt_sock(void)
1341{
1342 svc_unreg_xprt_class(&svc_tcp_class);
1343 svc_unreg_xprt_class(&svc_udp_class);
Andy Adamson16b2d1e2011-01-06 02:04:27 +00001344 svc_cleanup_bc_xprt_sock();
Tom Tucker360d87382007-12-30 21:07:17 -06001345}
1346
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001347static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct sock *sk = svsk->sk_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001351 svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001352 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (sk->sk_state == TCP_LISTEN) {
1354 dprintk("setting up TCP socket for listening\n");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001355 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001357 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 } else {
1359 dprintk("setting up TCP socket for reading\n");
1360 sk->sk_state_change = svc_tcp_state_change;
1361 sk->sk_data_ready = svc_tcp_data_ready;
Trond Myklebust47fcb032009-05-18 17:47:56 -04001362 sk->sk_write_space = svc_tcp_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 svsk->sk_reclen = 0;
1365 svsk->sk_tcplen = 0;
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001366 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Chuck Leverb7872fe2008-04-14 12:27:01 -04001368 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
J. Bruce Fields7f421832009-05-27 18:51:06 -04001370 /* initialise setting must have enough space to
1371 * receive and respond to one request.
1372 * svc_tcp_recvfrom will re-adjust if necessary
1373 */
1374 svc_sock_setbufsize(svsk->sk_sock,
1375 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
1376 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
1377
1378 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001379 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001380 if (sk->sk_state != TCP_ESTABLISHED)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001381 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383}
1384
Tom Tucker0f0257e2007-12-30 21:08:27 -06001385void svc_sock_update_bufs(struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 /*
1388 * The number of server threads has changed. Update
1389 * rcvbuf and sndbuf accordingly on all sockets
1390 */
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001391 struct svc_sock *svsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 spin_lock_bh(&serv->sv_lock);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001394 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001395 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Pavel Emelyanov8f3a6de2010-10-05 23:30:19 +04001396 list_for_each_entry(svsk, &serv->sv_tempsocks, sk_xprt.xpt_list)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001397 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 spin_unlock_bh(&serv->sv_lock);
1399}
Trond Myklebust24c37672008-12-23 16:30:12 -05001400EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * Initialize socket for RPC use and create svc_sock struct
1404 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1405 */
Chuck Lever6b174332007-02-12 00:53:28 -08001406static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1407 struct socket *sock,
1408 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct svc_sock *svsk;
1411 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001412 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001415 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *errp = -ENOMEM;
1417 return NULL;
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 inet = sock->sk;
1421
1422 /* Register socket with portmapper */
1423 if (*errp >= 0 && pmap_register)
Chuck Leverbaf01ca2009-03-18 20:46:13 -04001424 *errp = svc_register(serv, inet->sk_family, inet->sk_protocol,
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001425 ntohs(inet_sk(inet)->inet_sport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 if (*errp < 0) {
1428 kfree(svsk);
1429 return NULL;
1430 }
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 inet->sk_user_data = svsk;
1433 svsk->sk_sock = sock;
1434 svsk->sk_sk = inet;
1435 svsk->sk_ostate = inet->sk_state_change;
1436 svsk->sk_odata = inet->sk_data_ready;
1437 svsk->sk_owspace = inet->sk_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /* Initialize the socket */
1440 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001441 svc_udp_init(svsk, serv);
J. Bruce Fields7f421832009-05-27 18:51:06 -04001442 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001443 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1446 svsk, svsk->sk_sk);
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return svsk;
1449}
1450
Chuck Leverbfba9ab2009-04-23 19:32:33 -04001451/**
1452 * svc_addsock - add a listener socket to an RPC service
1453 * @serv: pointer to RPC service to which to add a new listener
1454 * @fd: file descriptor of the new listener
1455 * @name_return: pointer to buffer to fill in with name of listener
1456 * @len: size of the buffer
1457 *
1458 * Fills in socket name and returns positive length of name if successful.
1459 * Name is terminated with '\n'. On error, returns a negative errno
1460 * value.
1461 */
1462int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1463 const size_t len)
NeilBrownb41b66d2006-10-02 02:17:48 -07001464{
1465 int err = 0;
1466 struct socket *so = sockfd_lookup(fd, &err);
1467 struct svc_sock *svsk = NULL;
1468
1469 if (!so)
1470 return err;
Aime Le Rouzic205ba422010-01-26 14:03:56 -05001471 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
NeilBrownb41b66d2006-10-02 02:17:48 -07001472 err = -EAFNOSUPPORT;
1473 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1474 so->sk->sk_protocol != IPPROTO_UDP)
1475 err = -EPROTONOSUPPORT;
1476 else if (so->state > SS_UNCONNECTED)
1477 err = -EISCONN;
1478 else {
Tom Tucker2da2c212008-11-23 09:58:08 -06001479 if (!try_module_get(THIS_MODULE))
1480 err = -ENOENT;
1481 else
1482 svsk = svc_setup_socket(serv, so, &err,
1483 SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001484 if (svsk) {
Tom Tucker9dbc2402007-12-30 21:08:12 -06001485 struct sockaddr_storage addr;
1486 struct sockaddr *sin = (struct sockaddr *)&addr;
1487 int salen;
1488 if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1489 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
Tom Tucker4e5caaa2007-12-30 21:08:20 -06001490 clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
1491 spin_lock_bh(&serv->sv_lock);
1492 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks);
1493 spin_unlock_bh(&serv->sv_lock);
Tom Tuckera6046f72007-12-30 21:08:01 -06001494 svc_xprt_received(&svsk->sk_xprt);
NeilBrownb41b66d2006-10-02 02:17:48 -07001495 err = 0;
Tom Tucker2da2c212008-11-23 09:58:08 -06001496 } else
1497 module_put(THIS_MODULE);
NeilBrownb41b66d2006-10-02 02:17:48 -07001498 }
1499 if (err) {
1500 sockfd_put(so);
1501 return err;
1502 }
Chuck Levere7942b92009-04-23 19:32:48 -04001503 return svc_one_sock_name(svsk, name_return, len);
NeilBrownb41b66d2006-10-02 02:17:48 -07001504}
1505EXPORT_SYMBOL_GPL(svc_addsock);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/*
1508 * Create socket for RPC service.
1509 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001510static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1511 int protocol,
Pavel Emelyanov62832c02010-09-29 16:04:18 +04001512 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001513 struct sockaddr *sin, int len,
1514 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct svc_sock *svsk;
1517 struct socket *sock;
1518 int error;
1519 int type;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001520 struct sockaddr_storage addr;
1521 struct sockaddr *newsin = (struct sockaddr *)&addr;
1522 int newlen;
Trond Myklebustc69da772009-03-30 18:59:17 -04001523 int family;
1524 int val;
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +03001525 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Chuck Leverad06e4b2007-02-12 00:53:32 -08001527 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1528 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001529 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1532 printk(KERN_WARNING "svc: only UDP and TCP "
1533 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001534 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Trond Myklebustc69da772009-03-30 18:59:17 -04001537 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1538 switch (sin->sa_family) {
1539 case AF_INET6:
1540 family = PF_INET6;
1541 break;
1542 case AF_INET:
1543 family = PF_INET;
1544 break;
1545 default:
1546 return ERR_PTR(-EINVAL);
1547 }
1548
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001549 error = __sock_create(net, family, type, protocol, &sock, 1);
Chuck Lever77f1f672007-02-12 00:53:39 -08001550 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001551 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Peter Zijlstraed075362006-12-06 20:35:24 -08001553 svc_reclassify_socket(sock);
1554
Trond Myklebustc69da772009-03-30 18:59:17 -04001555 /*
1556 * If this is an PF_INET6 listener, we want to avoid
1557 * getting requests from IPv4 remotes. Those should
1558 * be shunted to a PF_INET listener via rpcbind.
1559 */
1560 val = 1;
1561 if (family == PF_INET6)
1562 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1563 (char *)&val, sizeof(val));
1564
Eric Sesterhenn18114742006-09-28 14:37:07 -07001565 if (type == SOCK_STREAM)
Chuck Lever77f1f672007-02-12 00:53:39 -08001566 sock->sk->sk_reuse = 1; /* allow address reuse */
1567 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001568 if (error < 0)
1569 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Tom Tucker9dbc2402007-12-30 21:08:12 -06001571 newlen = len;
1572 error = kernel_getsockname(sock, newsin, &newlen);
1573 if (error < 0)
1574 goto bummer;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001577 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto bummer;
1579 }
1580
NeilBrowne79eff12007-02-12 00:53:30 -08001581 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
Tom Tucker9dbc2402007-12-30 21:08:12 -06001582 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001583 return (struct svc_xprt *)svsk;
NeilBrowne79eff12007-02-12 00:53:30 -08001584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586bummer:
1587 dprintk("svc: svc_create_socket error = %d\n", -error);
1588 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001589 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001593 * Detach the svc_sock from the socket so that no
1594 * more callbacks occur.
1595 */
1596static void svc_sock_detach(struct svc_xprt *xprt)
1597{
1598 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1599 struct sock *sk = svsk->sk_sk;
Eric Dumazeteaefd112011-02-18 03:26:36 +00001600 wait_queue_head_t *wq;
Tom Tucker755ccea2007-12-30 21:07:27 -06001601
1602 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1603
1604 /* put back the old socket callbacks */
1605 sk->sk_state_change = svsk->sk_ostate;
1606 sk->sk_data_ready = svsk->sk_odata;
1607 sk->sk_write_space = svsk->sk_owspace;
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001608
Eric Dumazeteaefd112011-02-18 03:26:36 +00001609 wq = sk_sleep(sk);
1610 if (wq && waitqueue_active(wq))
1611 wake_up_interruptible(wq);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001612}
1613
1614/*
1615 * Disconnect the socket, and reset the callbacks
1616 */
1617static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1618{
1619 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1620
1621 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1622
1623 svc_sock_detach(xprt);
1624
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001625 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1626 svc_tcp_clear_pages(svsk);
Trond Myklebust69b6ba32008-12-23 16:30:11 -05001627 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
J. Bruce Fields31d68ef2011-02-24 11:25:33 -08001628 }
Tom Tucker755ccea2007-12-30 21:07:27 -06001629}
1630
1631/*
1632 * Free the svc_sock's socket resources and the svc_sock itself.
1633 */
1634static void svc_sock_free(struct svc_xprt *xprt)
1635{
1636 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1637 dprintk("svc: svc_sock_free(%p)\n", svsk);
1638
Tom Tucker755ccea2007-12-30 21:07:27 -06001639 if (svsk->sk_sock->file)
1640 sockfd_put(svsk->sk_sock);
1641 else
1642 sock_release(svsk->sk_sock);
1643 kfree(svsk);
1644}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001645
Andy Adamson1f11a032011-01-06 02:04:26 +00001646#if defined(CONFIG_NFS_V4_1)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001647/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001648 * Create a back channel svc_xprt which shares the fore channel socket.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001649 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001650static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1651 int protocol,
1652 struct net *net,
1653 struct sockaddr *sin, int len,
1654 int flags)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001655{
1656 struct svc_sock *svsk;
Andy Adamson1f11a032011-01-06 02:04:26 +00001657 struct svc_xprt *xprt;
Benny Halevy7652e5a2009-04-01 09:23:09 -04001658
Andy Adamson1f11a032011-01-06 02:04:26 +00001659 if (protocol != IPPROTO_TCP) {
1660 printk(KERN_WARNING "svc: only TCP sockets"
1661 " supported on shared back channel\n");
1662 return ERR_PTR(-EINVAL);
1663 }
1664
Benny Halevy7652e5a2009-04-01 09:23:09 -04001665 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1666 if (!svsk)
Andy Adamson1f11a032011-01-06 02:04:26 +00001667 return ERR_PTR(-ENOMEM);
Benny Halevy7652e5a2009-04-01 09:23:09 -04001668
1669 xprt = &svsk->sk_xprt;
Andy Adamson1f11a032011-01-06 02:04:26 +00001670 svc_xprt_init(&svc_tcp_bc_class, xprt, serv);
1671
Andy Adamson4a19de02011-01-06 02:04:35 +00001672 serv->sv_bc_xprt = xprt;
Andy Adamson1f11a032011-01-06 02:04:26 +00001673
Benny Halevy7652e5a2009-04-01 09:23:09 -04001674 return xprt;
1675}
Benny Halevy7652e5a2009-04-01 09:23:09 -04001676
1677/*
Andy Adamson1f11a032011-01-06 02:04:26 +00001678 * Free a back channel svc_sock.
Benny Halevy7652e5a2009-04-01 09:23:09 -04001679 */
Andy Adamson1f11a032011-01-06 02:04:26 +00001680static void svc_bc_sock_free(struct svc_xprt *xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001681{
Andy Adamson778be232011-01-25 15:38:01 +00001682 if (xprt)
Benny Halevy7652e5a2009-04-01 09:23:09 -04001683 kfree(container_of(xprt, struct svc_sock, sk_xprt));
1684}
Andy Adamson1f11a032011-01-06 02:04:26 +00001685#endif /* CONFIG_NFS_V4_1 */