svcsock.c 38 KB

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