PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/net/sunrpc/svcsock.c

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