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