svcsock.c 42 KB

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