xprtsock.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  1. /*
  2. * linux/net/sunrpc/xprtsock.c
  3. *
  4. * Client-side transport implementation for sockets.
  5. *
  6. * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com>
  7. * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com>
  8. * TCP NFS related read + write fixes
  9. * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
  10. *
  11. * Rewrite of larges part of the code in order to stabilize TCP stuff.
  12. * Fix behaviour when socket buffer is full.
  13. * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
  14. *
  15. * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
  16. *
  17. * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
  18. * <gilles.quillard@bull.net>
  19. */
  20. #include <linux/types.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/capability.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/errno.h>
  26. #include <linux/socket.h>
  27. #include <linux/in.h>
  28. #include <linux/net.h>
  29. #include <linux/mm.h>
  30. #include <linux/udp.h>
  31. #include <linux/tcp.h>
  32. #include <linux/sunrpc/clnt.h>
  33. #include <linux/sunrpc/sched.h>
  34. #include <linux/sunrpc/xprtsock.h>
  35. #include <linux/file.h>
  36. #include <net/sock.h>
  37. #include <net/checksum.h>
  38. #include <net/udp.h>
  39. #include <net/tcp.h>
  40. /*
  41. * xprtsock tunables
  42. */
  43. unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
  44. unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
  45. unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
  46. unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
  47. /*
  48. * We can register our own files under /proc/sys/sunrpc by
  49. * calling register_sysctl_table() again. The files in that
  50. * directory become the union of all files registered there.
  51. *
  52. * We simply need to make sure that we don't collide with
  53. * someone else's file names!
  54. */
  55. #ifdef RPC_DEBUG
  56. static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
  57. static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
  58. static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
  59. static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
  60. static struct ctl_table_header *sunrpc_table_header;
  61. /*
  62. * FIXME: changing the UDP slot table size should also resize the UDP
  63. * socket buffers for existing UDP transports
  64. */
  65. static ctl_table xs_tunables_table[] = {
  66. {
  67. .ctl_name = CTL_SLOTTABLE_UDP,
  68. .procname = "udp_slot_table_entries",
  69. .data = &xprt_udp_slot_table_entries,
  70. .maxlen = sizeof(unsigned int),
  71. .mode = 0644,
  72. .proc_handler = &proc_dointvec_minmax,
  73. .strategy = &sysctl_intvec,
  74. .extra1 = &min_slot_table_size,
  75. .extra2 = &max_slot_table_size
  76. },
  77. {
  78. .ctl_name = CTL_SLOTTABLE_TCP,
  79. .procname = "tcp_slot_table_entries",
  80. .data = &xprt_tcp_slot_table_entries,
  81. .maxlen = sizeof(unsigned int),
  82. .mode = 0644,
  83. .proc_handler = &proc_dointvec_minmax,
  84. .strategy = &sysctl_intvec,
  85. .extra1 = &min_slot_table_size,
  86. .extra2 = &max_slot_table_size
  87. },
  88. {
  89. .ctl_name = CTL_MIN_RESVPORT,
  90. .procname = "min_resvport",
  91. .data = &xprt_min_resvport,
  92. .maxlen = sizeof(unsigned int),
  93. .mode = 0644,
  94. .proc_handler = &proc_dointvec_minmax,
  95. .strategy = &sysctl_intvec,
  96. .extra1 = &xprt_min_resvport_limit,
  97. .extra2 = &xprt_max_resvport_limit
  98. },
  99. {
  100. .ctl_name = CTL_MAX_RESVPORT,
  101. .procname = "max_resvport",
  102. .data = &xprt_max_resvport,
  103. .maxlen = sizeof(unsigned int),
  104. .mode = 0644,
  105. .proc_handler = &proc_dointvec_minmax,
  106. .strategy = &sysctl_intvec,
  107. .extra1 = &xprt_min_resvport_limit,
  108. .extra2 = &xprt_max_resvport_limit
  109. },
  110. {
  111. .ctl_name = 0,
  112. },
  113. };
  114. static ctl_table sunrpc_table[] = {
  115. {
  116. .ctl_name = CTL_SUNRPC,
  117. .procname = "sunrpc",
  118. .mode = 0555,
  119. .child = xs_tunables_table
  120. },
  121. {
  122. .ctl_name = 0,
  123. },
  124. };
  125. #endif
  126. /*
  127. * How many times to try sending a request on a socket before waiting
  128. * for the socket buffer to clear.
  129. */
  130. #define XS_SENDMSG_RETRY (10U)
  131. /*
  132. * Time out for an RPC UDP socket connect. UDP socket connects are
  133. * synchronous, but we set a timeout anyway in case of resource
  134. * exhaustion on the local host.
  135. */
  136. #define XS_UDP_CONN_TO (5U * HZ)
  137. /*
  138. * Wait duration for an RPC TCP connection to be established. Solaris
  139. * NFS over TCP uses 60 seconds, for example, which is in line with how
  140. * long a server takes to reboot.
  141. */
  142. #define XS_TCP_CONN_TO (60U * HZ)
  143. /*
  144. * Wait duration for a reply from the RPC portmapper.
  145. */
  146. #define XS_BIND_TO (60U * HZ)
  147. /*
  148. * Delay if a UDP socket connect error occurs. This is most likely some
  149. * kind of resource problem on the local host.
  150. */
  151. #define XS_UDP_REEST_TO (2U * HZ)
  152. /*
  153. * The reestablish timeout allows clients to delay for a bit before attempting
  154. * to reconnect to a server that just dropped our connection.
  155. *
  156. * We implement an exponential backoff when trying to reestablish a TCP
  157. * transport connection with the server. Some servers like to drop a TCP
  158. * connection when they are overworked, so we start with a short timeout and
  159. * increase over time if the server is down or not responding.
  160. */
  161. #define XS_TCP_INIT_REEST_TO (3U * HZ)
  162. #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
  163. /*
  164. * TCP idle timeout; client drops the transport socket if it is idle
  165. * for this long. Note that we also timeout UDP sockets to prevent
  166. * holding port numbers when there is no RPC traffic.
  167. */
  168. #define XS_IDLE_DISC_TO (5U * 60 * HZ)
  169. #ifdef RPC_DEBUG
  170. # undef RPC_DEBUG_DATA
  171. # define RPCDBG_FACILITY RPCDBG_TRANS
  172. #endif
  173. #ifdef RPC_DEBUG_DATA
  174. static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
  175. {
  176. u8 *buf = (u8 *) packet;
  177. int j;
  178. dprintk("RPC: %s\n", msg);
  179. for (j = 0; j < count && j < 128; j += 4) {
  180. if (!(j & 31)) {
  181. if (j)
  182. dprintk("\n");
  183. dprintk("0x%04x ", j);
  184. }
  185. dprintk("%02x%02x%02x%02x ",
  186. buf[j], buf[j+1], buf[j+2], buf[j+3]);
  187. }
  188. dprintk("\n");
  189. }
  190. #else
  191. static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
  192. {
  193. /* NOP */
  194. }
  195. #endif
  196. struct sock_xprt {
  197. struct rpc_xprt xprt;
  198. /*
  199. * Network layer
  200. */
  201. struct socket * sock;
  202. struct sock * inet;
  203. /*
  204. * State of TCP reply receive
  205. */
  206. __be32 tcp_fraghdr,
  207. tcp_xid;
  208. u32 tcp_offset,
  209. tcp_reclen;
  210. unsigned long tcp_copied,
  211. tcp_flags;
  212. /*
  213. * Connection of transports
  214. */
  215. struct delayed_work connect_worker;
  216. struct sockaddr_storage addr;
  217. unsigned short port;
  218. /*
  219. * UDP socket buffer size parameters
  220. */
  221. size_t rcvsize,
  222. sndsize;
  223. /*
  224. * Saved socket callback addresses
  225. */
  226. void (*old_data_ready)(struct sock *, int);
  227. void (*old_state_change)(struct sock *);
  228. void (*old_write_space)(struct sock *);
  229. };
  230. /*
  231. * TCP receive state flags
  232. */
  233. #define TCP_RCV_LAST_FRAG (1UL << 0)
  234. #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
  235. #define TCP_RCV_COPY_XID (1UL << 2)
  236. #define TCP_RCV_COPY_DATA (1UL << 3)
  237. static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
  238. {
  239. return (struct sockaddr *) &xprt->addr;
  240. }
  241. static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
  242. {
  243. return (struct sockaddr_in *) &xprt->addr;
  244. }
  245. static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
  246. {
  247. return (struct sockaddr_in6 *) &xprt->addr;
  248. }
  249. static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt)
  250. {
  251. struct sockaddr_in *addr = xs_addr_in(xprt);
  252. char *buf;
  253. buf = kzalloc(20, GFP_KERNEL);
  254. if (buf) {
  255. snprintf(buf, 20, NIPQUAD_FMT,
  256. NIPQUAD(addr->sin_addr.s_addr));
  257. }
  258. xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
  259. buf = kzalloc(8, GFP_KERNEL);
  260. if (buf) {
  261. snprintf(buf, 8, "%u",
  262. ntohs(addr->sin_port));
  263. }
  264. xprt->address_strings[RPC_DISPLAY_PORT] = buf;
  265. buf = kzalloc(8, GFP_KERNEL);
  266. if (buf) {
  267. if (xprt->prot == IPPROTO_UDP)
  268. snprintf(buf, 8, "udp");
  269. else
  270. snprintf(buf, 8, "tcp");
  271. }
  272. xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
  273. buf = kzalloc(48, GFP_KERNEL);
  274. if (buf) {
  275. snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s",
  276. NIPQUAD(addr->sin_addr.s_addr),
  277. ntohs(addr->sin_port),
  278. xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
  279. }
  280. xprt->address_strings[RPC_DISPLAY_ALL] = buf;
  281. buf = kzalloc(10, GFP_KERNEL);
  282. if (buf) {
  283. snprintf(buf, 10, "%02x%02x%02x%02x",
  284. NIPQUAD(addr->sin_addr.s_addr));
  285. }
  286. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
  287. buf = kzalloc(8, GFP_KERNEL);
  288. if (buf) {
  289. snprintf(buf, 8, "%4hx",
  290. ntohs(addr->sin_port));
  291. }
  292. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
  293. buf = kzalloc(30, GFP_KERNEL);
  294. if (buf) {
  295. snprintf(buf, 30, NIPQUAD_FMT".%u.%u",
  296. NIPQUAD(addr->sin_addr.s_addr),
  297. ntohs(addr->sin_port) >> 8,
  298. ntohs(addr->sin_port) & 0xff);
  299. }
  300. xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
  301. xprt->address_strings[RPC_DISPLAY_NETID] =
  302. kstrdup(xprt->prot == IPPROTO_UDP ?
  303. RPCBIND_NETID_UDP : RPCBIND_NETID_TCP, GFP_KERNEL);
  304. }
  305. static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt)
  306. {
  307. struct sockaddr_in6 *addr = xs_addr_in6(xprt);
  308. char *buf;
  309. buf = kzalloc(40, GFP_KERNEL);
  310. if (buf) {
  311. snprintf(buf, 40, NIP6_FMT,
  312. NIP6(addr->sin6_addr));
  313. }
  314. xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
  315. buf = kzalloc(8, GFP_KERNEL);
  316. if (buf) {
  317. snprintf(buf, 8, "%u",
  318. ntohs(addr->sin6_port));
  319. }
  320. xprt->address_strings[RPC_DISPLAY_PORT] = buf;
  321. buf = kzalloc(8, GFP_KERNEL);
  322. if (buf) {
  323. if (xprt->prot == IPPROTO_UDP)
  324. snprintf(buf, 8, "udp");
  325. else
  326. snprintf(buf, 8, "tcp");
  327. }
  328. xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
  329. buf = kzalloc(64, GFP_KERNEL);
  330. if (buf) {
  331. snprintf(buf, 64, "addr="NIP6_FMT" port=%u proto=%s",
  332. NIP6(addr->sin6_addr),
  333. ntohs(addr->sin6_port),
  334. xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
  335. }
  336. xprt->address_strings[RPC_DISPLAY_ALL] = buf;
  337. buf = kzalloc(36, GFP_KERNEL);
  338. if (buf) {
  339. snprintf(buf, 36, NIP6_SEQFMT,
  340. NIP6(addr->sin6_addr));
  341. }
  342. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
  343. buf = kzalloc(8, GFP_KERNEL);
  344. if (buf) {
  345. snprintf(buf, 8, "%4hx",
  346. ntohs(addr->sin6_port));
  347. }
  348. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
  349. buf = kzalloc(50, GFP_KERNEL);
  350. if (buf) {
  351. snprintf(buf, 50, NIP6_FMT".%u.%u",
  352. NIP6(addr->sin6_addr),
  353. ntohs(addr->sin6_port) >> 8,
  354. ntohs(addr->sin6_port) & 0xff);
  355. }
  356. xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
  357. xprt->address_strings[RPC_DISPLAY_NETID] =
  358. kstrdup(xprt->prot == IPPROTO_UDP ?
  359. RPCBIND_NETID_UDP6 : RPCBIND_NETID_TCP6, GFP_KERNEL);
  360. }
  361. static void xs_free_peer_addresses(struct rpc_xprt *xprt)
  362. {
  363. int i;
  364. for (i = 0; i < RPC_DISPLAY_MAX; i++)
  365. kfree(xprt->address_strings[i]);
  366. }
  367. #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
  368. static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
  369. {
  370. struct msghdr msg = {
  371. .msg_name = addr,
  372. .msg_namelen = addrlen,
  373. .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
  374. };
  375. struct kvec iov = {
  376. .iov_base = vec->iov_base + base,
  377. .iov_len = vec->iov_len - base,
  378. };
  379. if (iov.iov_len != 0)
  380. return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
  381. return kernel_sendmsg(sock, &msg, NULL, 0, 0);
  382. }
  383. static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
  384. {
  385. struct page **ppage;
  386. unsigned int remainder;
  387. int err, sent = 0;
  388. remainder = xdr->page_len - base;
  389. base += xdr->page_base;
  390. ppage = xdr->pages + (base >> PAGE_SHIFT);
  391. base &= ~PAGE_MASK;
  392. for(;;) {
  393. unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
  394. int flags = XS_SENDMSG_FLAGS;
  395. remainder -= len;
  396. if (remainder != 0 || more)
  397. flags |= MSG_MORE;
  398. err = sock->ops->sendpage(sock, *ppage, base, len, flags);
  399. if (remainder == 0 || err != len)
  400. break;
  401. sent += err;
  402. ppage++;
  403. base = 0;
  404. }
  405. if (sent == 0)
  406. return err;
  407. if (err > 0)
  408. sent += err;
  409. return sent;
  410. }
  411. /**
  412. * xs_sendpages - write pages directly to a socket
  413. * @sock: socket to send on
  414. * @addr: UDP only -- address of destination
  415. * @addrlen: UDP only -- length of destination address
  416. * @xdr: buffer containing this request
  417. * @base: starting position in the buffer
  418. *
  419. */
  420. static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
  421. {
  422. unsigned int remainder = xdr->len - base;
  423. int err, sent = 0;
  424. if (unlikely(!sock))
  425. return -ENOTCONN;
  426. clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
  427. if (base != 0) {
  428. addr = NULL;
  429. addrlen = 0;
  430. }
  431. if (base < xdr->head[0].iov_len || addr != NULL) {
  432. unsigned int len = xdr->head[0].iov_len - base;
  433. remainder -= len;
  434. err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
  435. if (remainder == 0 || err != len)
  436. goto out;
  437. sent += err;
  438. base = 0;
  439. } else
  440. base -= xdr->head[0].iov_len;
  441. if (base < xdr->page_len) {
  442. unsigned int len = xdr->page_len - base;
  443. remainder -= len;
  444. err = xs_send_pagedata(sock, xdr, base, remainder != 0);
  445. if (remainder == 0 || err != len)
  446. goto out;
  447. sent += err;
  448. base = 0;
  449. } else
  450. base -= xdr->page_len;
  451. if (base >= xdr->tail[0].iov_len)
  452. return sent;
  453. err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
  454. out:
  455. if (sent == 0)
  456. return err;
  457. if (err > 0)
  458. sent += err;
  459. return sent;
  460. }
  461. /**
  462. * xs_nospace - place task on wait queue if transmit was incomplete
  463. * @task: task to put to sleep
  464. *
  465. */
  466. static void xs_nospace(struct rpc_task *task)
  467. {
  468. struct rpc_rqst *req = task->tk_rqstp;
  469. struct rpc_xprt *xprt = req->rq_xprt;
  470. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  471. dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
  472. task->tk_pid, req->rq_slen - req->rq_bytes_sent,
  473. req->rq_slen);
  474. if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
  475. /* Protect against races with write_space */
  476. spin_lock_bh(&xprt->transport_lock);
  477. /* Don't race with disconnect */
  478. if (!xprt_connected(xprt))
  479. task->tk_status = -ENOTCONN;
  480. else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
  481. xprt_wait_for_buffer_space(task);
  482. spin_unlock_bh(&xprt->transport_lock);
  483. } else
  484. /* Keep holding the socket if it is blocked */
  485. rpc_delay(task, HZ>>4);
  486. }
  487. /**
  488. * xs_udp_send_request - write an RPC request to a UDP socket
  489. * @task: address of RPC task that manages the state of an RPC request
  490. *
  491. * Return values:
  492. * 0: The request has been sent
  493. * EAGAIN: The socket was blocked, please call again later to
  494. * complete the request
  495. * ENOTCONN: Caller needs to invoke connect logic then call again
  496. * other: Some other error occured, the request was not sent
  497. */
  498. static int xs_udp_send_request(struct rpc_task *task)
  499. {
  500. struct rpc_rqst *req = task->tk_rqstp;
  501. struct rpc_xprt *xprt = req->rq_xprt;
  502. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  503. struct xdr_buf *xdr = &req->rq_snd_buf;
  504. int status;
  505. xs_pktdump("packet data:",
  506. req->rq_svec->iov_base,
  507. req->rq_svec->iov_len);
  508. req->rq_xtime = jiffies;
  509. status = xs_sendpages(transport->sock,
  510. xs_addr(xprt),
  511. xprt->addrlen, xdr,
  512. req->rq_bytes_sent);
  513. dprintk("RPC: xs_udp_send_request(%u) = %d\n",
  514. xdr->len - req->rq_bytes_sent, status);
  515. task->tk_bytes_sent += status;
  516. if (likely(status >= (int) req->rq_slen))
  517. return 0;
  518. /* Still some bytes left; set up for a retry later. */
  519. if (status > 0)
  520. status = -EAGAIN;
  521. switch (status) {
  522. case -ENETUNREACH:
  523. case -EPIPE:
  524. case -ECONNREFUSED:
  525. /* When the server has died, an ICMP port unreachable message
  526. * prompts ECONNREFUSED. */
  527. break;
  528. case -EAGAIN:
  529. xs_nospace(task);
  530. break;
  531. default:
  532. dprintk("RPC: sendmsg returned unrecognized error %d\n",
  533. -status);
  534. break;
  535. }
  536. return status;
  537. }
  538. static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
  539. {
  540. u32 reclen = buf->len - sizeof(rpc_fraghdr);
  541. rpc_fraghdr *base = buf->head[0].iov_base;
  542. *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
  543. }
  544. /**
  545. * xs_tcp_send_request - write an RPC request to a TCP socket
  546. * @task: address of RPC task that manages the state of an RPC request
  547. *
  548. * Return values:
  549. * 0: The request has been sent
  550. * EAGAIN: The socket was blocked, please call again later to
  551. * complete the request
  552. * ENOTCONN: Caller needs to invoke connect logic then call again
  553. * other: Some other error occured, the request was not sent
  554. *
  555. * XXX: In the case of soft timeouts, should we eventually give up
  556. * if sendmsg is not able to make progress?
  557. */
  558. static int xs_tcp_send_request(struct rpc_task *task)
  559. {
  560. struct rpc_rqst *req = task->tk_rqstp;
  561. struct rpc_xprt *xprt = req->rq_xprt;
  562. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  563. struct xdr_buf *xdr = &req->rq_snd_buf;
  564. int status;
  565. unsigned int retry = 0;
  566. xs_encode_tcp_record_marker(&req->rq_snd_buf);
  567. xs_pktdump("packet data:",
  568. req->rq_svec->iov_base,
  569. req->rq_svec->iov_len);
  570. /* Continue transmitting the packet/record. We must be careful
  571. * to cope with writespace callbacks arriving _after_ we have
  572. * called sendmsg(). */
  573. while (1) {
  574. req->rq_xtime = jiffies;
  575. status = xs_sendpages(transport->sock,
  576. NULL, 0, xdr, req->rq_bytes_sent);
  577. dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
  578. xdr->len - req->rq_bytes_sent, status);
  579. if (unlikely(status < 0))
  580. break;
  581. /* If we've sent the entire packet, immediately
  582. * reset the count of bytes sent. */
  583. req->rq_bytes_sent += status;
  584. task->tk_bytes_sent += status;
  585. if (likely(req->rq_bytes_sent >= req->rq_slen)) {
  586. req->rq_bytes_sent = 0;
  587. return 0;
  588. }
  589. status = -EAGAIN;
  590. if (retry++ > XS_SENDMSG_RETRY)
  591. break;
  592. }
  593. switch (status) {
  594. case -EAGAIN:
  595. xs_nospace(task);
  596. break;
  597. case -ECONNREFUSED:
  598. case -ECONNRESET:
  599. case -ENOTCONN:
  600. case -EPIPE:
  601. status = -ENOTCONN;
  602. break;
  603. default:
  604. dprintk("RPC: sendmsg returned unrecognized error %d\n",
  605. -status);
  606. xprt_disconnect(xprt);
  607. break;
  608. }
  609. return status;
  610. }
  611. /**
  612. * xs_tcp_release_xprt - clean up after a tcp transmission
  613. * @xprt: transport
  614. * @task: rpc task
  615. *
  616. * This cleans up if an error causes us to abort the transmission of a request.
  617. * In this case, the socket may need to be reset in order to avoid confusing
  618. * the server.
  619. */
  620. static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  621. {
  622. struct rpc_rqst *req;
  623. if (task != xprt->snd_task)
  624. return;
  625. if (task == NULL)
  626. goto out_release;
  627. req = task->tk_rqstp;
  628. if (req->rq_bytes_sent == 0)
  629. goto out_release;
  630. if (req->rq_bytes_sent == req->rq_snd_buf.len)
  631. goto out_release;
  632. set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
  633. out_release:
  634. xprt_release_xprt(xprt, task);
  635. }
  636. /**
  637. * xs_close - close a socket
  638. * @xprt: transport
  639. *
  640. * This is used when all requests are complete; ie, no DRC state remains
  641. * on the server we want to save.
  642. */
  643. static void xs_close(struct rpc_xprt *xprt)
  644. {
  645. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  646. struct socket *sock = transport->sock;
  647. struct sock *sk = transport->inet;
  648. if (!sk)
  649. goto clear_close_wait;
  650. dprintk("RPC: xs_close xprt %p\n", xprt);
  651. write_lock_bh(&sk->sk_callback_lock);
  652. transport->inet = NULL;
  653. transport->sock = NULL;
  654. sk->sk_user_data = NULL;
  655. sk->sk_data_ready = transport->old_data_ready;
  656. sk->sk_state_change = transport->old_state_change;
  657. sk->sk_write_space = transport->old_write_space;
  658. write_unlock_bh(&sk->sk_callback_lock);
  659. sk->sk_no_check = 0;
  660. sock_release(sock);
  661. clear_close_wait:
  662. smp_mb__before_clear_bit();
  663. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  664. smp_mb__after_clear_bit();
  665. }
  666. /**
  667. * xs_destroy - prepare to shutdown a transport
  668. * @xprt: doomed transport
  669. *
  670. */
  671. static void xs_destroy(struct rpc_xprt *xprt)
  672. {
  673. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  674. dprintk("RPC: xs_destroy xprt %p\n", xprt);
  675. cancel_rearming_delayed_work(&transport->connect_worker);
  676. xprt_disconnect(xprt);
  677. xs_close(xprt);
  678. xs_free_peer_addresses(xprt);
  679. kfree(xprt->slot);
  680. kfree(xprt);
  681. module_put(THIS_MODULE);
  682. }
  683. static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
  684. {
  685. return (struct rpc_xprt *) sk->sk_user_data;
  686. }
  687. /**
  688. * xs_udp_data_ready - "data ready" callback for UDP sockets
  689. * @sk: socket with data to read
  690. * @len: how much data to read
  691. *
  692. */
  693. static void xs_udp_data_ready(struct sock *sk, int len)
  694. {
  695. struct rpc_task *task;
  696. struct rpc_xprt *xprt;
  697. struct rpc_rqst *rovr;
  698. struct sk_buff *skb;
  699. int err, repsize, copied;
  700. u32 _xid;
  701. __be32 *xp;
  702. read_lock(&sk->sk_callback_lock);
  703. dprintk("RPC: xs_udp_data_ready...\n");
  704. if (!(xprt = xprt_from_sock(sk)))
  705. goto out;
  706. if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
  707. goto out;
  708. if (xprt->shutdown)
  709. goto dropit;
  710. repsize = skb->len - sizeof(struct udphdr);
  711. if (repsize < 4) {
  712. dprintk("RPC: impossible RPC reply size %d!\n", repsize);
  713. goto dropit;
  714. }
  715. /* Copy the XID from the skb... */
  716. xp = skb_header_pointer(skb, sizeof(struct udphdr),
  717. sizeof(_xid), &_xid);
  718. if (xp == NULL)
  719. goto dropit;
  720. /* Look up and lock the request corresponding to the given XID */
  721. spin_lock(&xprt->transport_lock);
  722. rovr = xprt_lookup_rqst(xprt, *xp);
  723. if (!rovr)
  724. goto out_unlock;
  725. task = rovr->rq_task;
  726. if ((copied = rovr->rq_private_buf.buflen) > repsize)
  727. copied = repsize;
  728. /* Suck it into the iovec, verify checksum if not done by hw. */
  729. if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
  730. goto out_unlock;
  731. /* Something worked... */
  732. dst_confirm(skb->dst);
  733. xprt_adjust_cwnd(task, copied);
  734. xprt_update_rtt(task);
  735. xprt_complete_rqst(task, copied);
  736. out_unlock:
  737. spin_unlock(&xprt->transport_lock);
  738. dropit:
  739. skb_free_datagram(sk, skb);
  740. out:
  741. read_unlock(&sk->sk_callback_lock);
  742. }
  743. static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
  744. {
  745. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  746. size_t len, used;
  747. char *p;
  748. p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
  749. len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
  750. used = xdr_skb_read_bits(desc, p, len);
  751. transport->tcp_offset += used;
  752. if (used != len)
  753. return;
  754. transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
  755. if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
  756. transport->tcp_flags |= TCP_RCV_LAST_FRAG;
  757. else
  758. transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
  759. transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
  760. transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
  761. transport->tcp_offset = 0;
  762. /* Sanity check of the record length */
  763. if (unlikely(transport->tcp_reclen < 4)) {
  764. dprintk("RPC: invalid TCP record fragment length\n");
  765. xprt_disconnect(xprt);
  766. return;
  767. }
  768. dprintk("RPC: reading TCP record fragment of length %d\n",
  769. transport->tcp_reclen);
  770. }
  771. static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
  772. {
  773. if (transport->tcp_offset == transport->tcp_reclen) {
  774. transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
  775. transport->tcp_offset = 0;
  776. if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
  777. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  778. transport->tcp_flags |= TCP_RCV_COPY_XID;
  779. transport->tcp_copied = 0;
  780. }
  781. }
  782. }
  783. static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
  784. {
  785. size_t len, used;
  786. char *p;
  787. len = sizeof(transport->tcp_xid) - transport->tcp_offset;
  788. dprintk("RPC: reading XID (%Zu bytes)\n", len);
  789. p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
  790. used = xdr_skb_read_bits(desc, p, len);
  791. transport->tcp_offset += used;
  792. if (used != len)
  793. return;
  794. transport->tcp_flags &= ~TCP_RCV_COPY_XID;
  795. transport->tcp_flags |= TCP_RCV_COPY_DATA;
  796. transport->tcp_copied = 4;
  797. dprintk("RPC: reading reply for XID %08x\n",
  798. ntohl(transport->tcp_xid));
  799. xs_tcp_check_fraghdr(transport);
  800. }
  801. static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
  802. {
  803. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  804. struct rpc_rqst *req;
  805. struct xdr_buf *rcvbuf;
  806. size_t len;
  807. ssize_t r;
  808. /* Find and lock the request corresponding to this xid */
  809. spin_lock(&xprt->transport_lock);
  810. req = xprt_lookup_rqst(xprt, transport->tcp_xid);
  811. if (!req) {
  812. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  813. dprintk("RPC: XID %08x request not found!\n",
  814. ntohl(transport->tcp_xid));
  815. spin_unlock(&xprt->transport_lock);
  816. return;
  817. }
  818. rcvbuf = &req->rq_private_buf;
  819. len = desc->count;
  820. if (len > transport->tcp_reclen - transport->tcp_offset) {
  821. struct xdr_skb_reader my_desc;
  822. len = transport->tcp_reclen - transport->tcp_offset;
  823. memcpy(&my_desc, desc, sizeof(my_desc));
  824. my_desc.count = len;
  825. r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
  826. &my_desc, xdr_skb_read_bits);
  827. desc->count -= r;
  828. desc->offset += r;
  829. } else
  830. r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
  831. desc, xdr_skb_read_bits);
  832. if (r > 0) {
  833. transport->tcp_copied += r;
  834. transport->tcp_offset += r;
  835. }
  836. if (r != len) {
  837. /* Error when copying to the receive buffer,
  838. * usually because we weren't able to allocate
  839. * additional buffer pages. All we can do now
  840. * is turn off TCP_RCV_COPY_DATA, so the request
  841. * will not receive any additional updates,
  842. * and time out.
  843. * Any remaining data from this record will
  844. * be discarded.
  845. */
  846. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  847. dprintk("RPC: XID %08x truncated request\n",
  848. ntohl(transport->tcp_xid));
  849. dprintk("RPC: xprt = %p, tcp_copied = %lu, "
  850. "tcp_offset = %u, tcp_reclen = %u\n",
  851. xprt, transport->tcp_copied,
  852. transport->tcp_offset, transport->tcp_reclen);
  853. goto out;
  854. }
  855. dprintk("RPC: XID %08x read %Zd bytes\n",
  856. ntohl(transport->tcp_xid), r);
  857. dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
  858. "tcp_reclen = %u\n", xprt, transport->tcp_copied,
  859. transport->tcp_offset, transport->tcp_reclen);
  860. if (transport->tcp_copied == req->rq_private_buf.buflen)
  861. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  862. else if (transport->tcp_offset == transport->tcp_reclen) {
  863. if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
  864. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  865. }
  866. out:
  867. if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
  868. xprt_complete_rqst(req->rq_task, transport->tcp_copied);
  869. spin_unlock(&xprt->transport_lock);
  870. xs_tcp_check_fraghdr(transport);
  871. }
  872. static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
  873. {
  874. size_t len;
  875. len = transport->tcp_reclen - transport->tcp_offset;
  876. if (len > desc->count)
  877. len = desc->count;
  878. desc->count -= len;
  879. desc->offset += len;
  880. transport->tcp_offset += len;
  881. dprintk("RPC: discarded %Zu bytes\n", len);
  882. xs_tcp_check_fraghdr(transport);
  883. }
  884. static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
  885. {
  886. struct rpc_xprt *xprt = rd_desc->arg.data;
  887. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  888. struct xdr_skb_reader desc = {
  889. .skb = skb,
  890. .offset = offset,
  891. .count = len,
  892. };
  893. dprintk("RPC: xs_tcp_data_recv started\n");
  894. do {
  895. /* Read in a new fragment marker if necessary */
  896. /* Can we ever really expect to get completely empty fragments? */
  897. if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
  898. xs_tcp_read_fraghdr(xprt, &desc);
  899. continue;
  900. }
  901. /* Read in the xid if necessary */
  902. if (transport->tcp_flags & TCP_RCV_COPY_XID) {
  903. xs_tcp_read_xid(transport, &desc);
  904. continue;
  905. }
  906. /* Read in the request data */
  907. if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
  908. xs_tcp_read_request(xprt, &desc);
  909. continue;
  910. }
  911. /* Skip over any trailing bytes on short reads */
  912. xs_tcp_read_discard(transport, &desc);
  913. } while (desc.count);
  914. dprintk("RPC: xs_tcp_data_recv done\n");
  915. return len - desc.count;
  916. }
  917. /**
  918. * xs_tcp_data_ready - "data ready" callback for TCP sockets
  919. * @sk: socket with data to read
  920. * @bytes: how much data to read
  921. *
  922. */
  923. static void xs_tcp_data_ready(struct sock *sk, int bytes)
  924. {
  925. struct rpc_xprt *xprt;
  926. read_descriptor_t rd_desc;
  927. dprintk("RPC: xs_tcp_data_ready...\n");
  928. read_lock(&sk->sk_callback_lock);
  929. if (!(xprt = xprt_from_sock(sk)))
  930. goto out;
  931. if (xprt->shutdown)
  932. goto out;
  933. /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
  934. rd_desc.arg.data = xprt;
  935. rd_desc.count = 65536;
  936. tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
  937. out:
  938. read_unlock(&sk->sk_callback_lock);
  939. }
  940. /**
  941. * xs_tcp_state_change - callback to handle TCP socket state changes
  942. * @sk: socket whose state has changed
  943. *
  944. */
  945. static void xs_tcp_state_change(struct sock *sk)
  946. {
  947. struct rpc_xprt *xprt;
  948. read_lock(&sk->sk_callback_lock);
  949. if (!(xprt = xprt_from_sock(sk)))
  950. goto out;
  951. dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
  952. dprintk("RPC: state %x conn %d dead %d zapped %d\n",
  953. sk->sk_state, xprt_connected(xprt),
  954. sock_flag(sk, SOCK_DEAD),
  955. sock_flag(sk, SOCK_ZAPPED));
  956. switch (sk->sk_state) {
  957. case TCP_ESTABLISHED:
  958. spin_lock_bh(&xprt->transport_lock);
  959. if (!xprt_test_and_set_connected(xprt)) {
  960. struct sock_xprt *transport = container_of(xprt,
  961. struct sock_xprt, xprt);
  962. /* Reset TCP record info */
  963. transport->tcp_offset = 0;
  964. transport->tcp_reclen = 0;
  965. transport->tcp_copied = 0;
  966. transport->tcp_flags =
  967. TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
  968. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  969. xprt_wake_pending_tasks(xprt, 0);
  970. }
  971. spin_unlock_bh(&xprt->transport_lock);
  972. break;
  973. case TCP_SYN_SENT:
  974. case TCP_SYN_RECV:
  975. break;
  976. case TCP_CLOSE_WAIT:
  977. /* Try to schedule an autoclose RPC calls */
  978. set_bit(XPRT_CLOSE_WAIT, &xprt->state);
  979. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  980. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  981. default:
  982. xprt_disconnect(xprt);
  983. }
  984. out:
  985. read_unlock(&sk->sk_callback_lock);
  986. }
  987. /**
  988. * xs_udp_write_space - callback invoked when socket buffer space
  989. * becomes available
  990. * @sk: socket whose state has changed
  991. *
  992. * Called when more output buffer space is available for this socket.
  993. * We try not to wake our writers until they can make "significant"
  994. * progress, otherwise we'll waste resources thrashing kernel_sendmsg
  995. * with a bunch of small requests.
  996. */
  997. static void xs_udp_write_space(struct sock *sk)
  998. {
  999. read_lock(&sk->sk_callback_lock);
  1000. /* from net/core/sock.c:sock_def_write_space */
  1001. if (sock_writeable(sk)) {
  1002. struct socket *sock;
  1003. struct rpc_xprt *xprt;
  1004. if (unlikely(!(sock = sk->sk_socket)))
  1005. goto out;
  1006. if (unlikely(!(xprt = xprt_from_sock(sk))))
  1007. goto out;
  1008. if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
  1009. goto out;
  1010. xprt_write_space(xprt);
  1011. }
  1012. out:
  1013. read_unlock(&sk->sk_callback_lock);
  1014. }
  1015. /**
  1016. * xs_tcp_write_space - callback invoked when socket buffer space
  1017. * becomes available
  1018. * @sk: socket whose state has changed
  1019. *
  1020. * Called when more output buffer space is available for this socket.
  1021. * We try not to wake our writers until they can make "significant"
  1022. * progress, otherwise we'll waste resources thrashing kernel_sendmsg
  1023. * with a bunch of small requests.
  1024. */
  1025. static void xs_tcp_write_space(struct sock *sk)
  1026. {
  1027. read_lock(&sk->sk_callback_lock);
  1028. /* from net/core/stream.c:sk_stream_write_space */
  1029. if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
  1030. struct socket *sock;
  1031. struct rpc_xprt *xprt;
  1032. if (unlikely(!(sock = sk->sk_socket)))
  1033. goto out;
  1034. if (unlikely(!(xprt = xprt_from_sock(sk))))
  1035. goto out;
  1036. if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
  1037. goto out;
  1038. xprt_write_space(xprt);
  1039. }
  1040. out:
  1041. read_unlock(&sk->sk_callback_lock);
  1042. }
  1043. static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
  1044. {
  1045. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1046. struct sock *sk = transport->inet;
  1047. if (transport->rcvsize) {
  1048. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  1049. sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
  1050. }
  1051. if (transport->sndsize) {
  1052. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  1053. sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
  1054. sk->sk_write_space(sk);
  1055. }
  1056. }
  1057. /**
  1058. * xs_udp_set_buffer_size - set send and receive limits
  1059. * @xprt: generic transport
  1060. * @sndsize: requested size of send buffer, in bytes
  1061. * @rcvsize: requested size of receive buffer, in bytes
  1062. *
  1063. * Set socket send and receive buffer size limits.
  1064. */
  1065. static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
  1066. {
  1067. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1068. transport->sndsize = 0;
  1069. if (sndsize)
  1070. transport->sndsize = sndsize + 1024;
  1071. transport->rcvsize = 0;
  1072. if (rcvsize)
  1073. transport->rcvsize = rcvsize + 1024;
  1074. xs_udp_do_set_buffer_size(xprt);
  1075. }
  1076. /**
  1077. * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
  1078. * @task: task that timed out
  1079. *
  1080. * Adjust the congestion window after a retransmit timeout has occurred.
  1081. */
  1082. static void xs_udp_timer(struct rpc_task *task)
  1083. {
  1084. xprt_adjust_cwnd(task, -ETIMEDOUT);
  1085. }
  1086. static unsigned short xs_get_random_port(void)
  1087. {
  1088. unsigned short range = xprt_max_resvport - xprt_min_resvport;
  1089. unsigned short rand = (unsigned short) net_random() % range;
  1090. return rand + xprt_min_resvport;
  1091. }
  1092. /**
  1093. * xs_set_port - reset the port number in the remote endpoint address
  1094. * @xprt: generic transport
  1095. * @port: new port number
  1096. *
  1097. */
  1098. static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
  1099. {
  1100. struct sockaddr *addr = xs_addr(xprt);
  1101. dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
  1102. switch (addr->sa_family) {
  1103. case AF_INET:
  1104. ((struct sockaddr_in *)addr)->sin_port = htons(port);
  1105. break;
  1106. case AF_INET6:
  1107. ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
  1108. break;
  1109. default:
  1110. BUG();
  1111. }
  1112. }
  1113. static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
  1114. {
  1115. struct sockaddr_in myaddr = {
  1116. .sin_family = AF_INET,
  1117. };
  1118. struct sockaddr_in *sa;
  1119. int err;
  1120. unsigned short port = transport->port;
  1121. if (!transport->xprt.resvport)
  1122. port = 0;
  1123. sa = (struct sockaddr_in *)&transport->addr;
  1124. myaddr.sin_addr = sa->sin_addr;
  1125. do {
  1126. myaddr.sin_port = htons(port);
  1127. err = kernel_bind(sock, (struct sockaddr *) &myaddr,
  1128. sizeof(myaddr));
  1129. if (!transport->xprt.resvport)
  1130. break;
  1131. if (err == 0) {
  1132. transport->port = port;
  1133. break;
  1134. }
  1135. if (port <= xprt_min_resvport)
  1136. port = xprt_max_resvport;
  1137. else
  1138. port--;
  1139. } while (err == -EADDRINUSE && port != transport->port);
  1140. dprintk("RPC: %s "NIPQUAD_FMT":%u: %s (%d)\n",
  1141. __FUNCTION__, NIPQUAD(myaddr.sin_addr),
  1142. port, err ? "failed" : "ok", err);
  1143. return err;
  1144. }
  1145. static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
  1146. {
  1147. struct sockaddr_in6 myaddr = {
  1148. .sin6_family = AF_INET6,
  1149. };
  1150. struct sockaddr_in6 *sa;
  1151. int err;
  1152. unsigned short port = transport->port;
  1153. if (!transport->xprt.resvport)
  1154. port = 0;
  1155. sa = (struct sockaddr_in6 *)&transport->addr;
  1156. myaddr.sin6_addr = sa->sin6_addr;
  1157. do {
  1158. myaddr.sin6_port = htons(port);
  1159. err = kernel_bind(sock, (struct sockaddr *) &myaddr,
  1160. sizeof(myaddr));
  1161. if (!transport->xprt.resvport)
  1162. break;
  1163. if (err == 0) {
  1164. transport->port = port;
  1165. break;
  1166. }
  1167. if (port <= xprt_min_resvport)
  1168. port = xprt_max_resvport;
  1169. else
  1170. port--;
  1171. } while (err == -EADDRINUSE && port != transport->port);
  1172. dprintk("RPC: xs_bind6 "NIP6_FMT":%u: %s (%d)\n",
  1173. NIP6(myaddr.sin6_addr), port, err ? "failed" : "ok", err);
  1174. return err;
  1175. }
  1176. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1177. static struct lock_class_key xs_key[2];
  1178. static struct lock_class_key xs_slock_key[2];
  1179. static inline void xs_reclassify_socket4(struct socket *sock)
  1180. {
  1181. struct sock *sk = sock->sk;
  1182. BUG_ON(sk->sk_lock.owner != NULL);
  1183. sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
  1184. &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
  1185. }
  1186. static inline void xs_reclassify_socket6(struct socket *sock)
  1187. {
  1188. struct sock *sk = sock->sk;
  1189. BUG_ON(sk->sk_lock.owner != NULL);
  1190. sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
  1191. &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
  1192. }
  1193. #else
  1194. static inline void xs_reclassify_socket4(struct socket *sock)
  1195. {
  1196. }
  1197. static inline void xs_reclassify_socket6(struct socket *sock)
  1198. {
  1199. }
  1200. #endif
  1201. static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
  1202. {
  1203. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1204. if (!transport->inet) {
  1205. struct sock *sk = sock->sk;
  1206. write_lock_bh(&sk->sk_callback_lock);
  1207. sk->sk_user_data = xprt;
  1208. transport->old_data_ready = sk->sk_data_ready;
  1209. transport->old_state_change = sk->sk_state_change;
  1210. transport->old_write_space = sk->sk_write_space;
  1211. sk->sk_data_ready = xs_udp_data_ready;
  1212. sk->sk_write_space = xs_udp_write_space;
  1213. sk->sk_no_check = UDP_CSUM_NORCV;
  1214. sk->sk_allocation = GFP_ATOMIC;
  1215. xprt_set_connected(xprt);
  1216. /* Reset to new socket */
  1217. transport->sock = sock;
  1218. transport->inet = sk;
  1219. write_unlock_bh(&sk->sk_callback_lock);
  1220. }
  1221. xs_udp_do_set_buffer_size(xprt);
  1222. }
  1223. /**
  1224. * xs_udp_connect_worker4 - set up a UDP socket
  1225. * @work: RPC transport to connect
  1226. *
  1227. * Invoked by a work queue tasklet.
  1228. */
  1229. static void xs_udp_connect_worker4(struct work_struct *work)
  1230. {
  1231. struct sock_xprt *transport =
  1232. container_of(work, struct sock_xprt, connect_worker.work);
  1233. struct rpc_xprt *xprt = &transport->xprt;
  1234. struct socket *sock = transport->sock;
  1235. int err, status = -EIO;
  1236. if (xprt->shutdown || !xprt_bound(xprt))
  1237. goto out;
  1238. /* Start by resetting any existing state */
  1239. xs_close(xprt);
  1240. if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
  1241. dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
  1242. goto out;
  1243. }
  1244. xs_reclassify_socket4(sock);
  1245. if (xs_bind4(transport, sock)) {
  1246. sock_release(sock);
  1247. goto out;
  1248. }
  1249. dprintk("RPC: worker connecting xprt %p to address: %s\n",
  1250. xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
  1251. xs_udp_finish_connecting(xprt, sock);
  1252. status = 0;
  1253. out:
  1254. xprt_wake_pending_tasks(xprt, status);
  1255. xprt_clear_connecting(xprt);
  1256. }
  1257. /**
  1258. * xs_udp_connect_worker6 - set up a UDP socket
  1259. * @work: RPC transport to connect
  1260. *
  1261. * Invoked by a work queue tasklet.
  1262. */
  1263. static void xs_udp_connect_worker6(struct work_struct *work)
  1264. {
  1265. struct sock_xprt *transport =
  1266. container_of(work, struct sock_xprt, connect_worker.work);
  1267. struct rpc_xprt *xprt = &transport->xprt;
  1268. struct socket *sock = transport->sock;
  1269. int err, status = -EIO;
  1270. if (xprt->shutdown || !xprt_bound(xprt))
  1271. goto out;
  1272. /* Start by resetting any existing state */
  1273. xs_close(xprt);
  1274. if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
  1275. dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
  1276. goto out;
  1277. }
  1278. xs_reclassify_socket6(sock);
  1279. if (xs_bind6(transport, sock) < 0) {
  1280. sock_release(sock);
  1281. goto out;
  1282. }
  1283. dprintk("RPC: worker connecting xprt %p to address: %s\n",
  1284. xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
  1285. xs_udp_finish_connecting(xprt, sock);
  1286. status = 0;
  1287. out:
  1288. xprt_wake_pending_tasks(xprt, status);
  1289. xprt_clear_connecting(xprt);
  1290. }
  1291. /*
  1292. * We need to preserve the port number so the reply cache on the server can
  1293. * find our cached RPC replies when we get around to reconnecting.
  1294. */
  1295. static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
  1296. {
  1297. int result;
  1298. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1299. struct sockaddr any;
  1300. dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
  1301. /*
  1302. * Disconnect the transport socket by doing a connect operation
  1303. * with AF_UNSPEC. This should return immediately...
  1304. */
  1305. memset(&any, 0, sizeof(any));
  1306. any.sa_family = AF_UNSPEC;
  1307. result = kernel_connect(transport->sock, &any, sizeof(any), 0);
  1308. if (result)
  1309. dprintk("RPC: AF_UNSPEC connect return code %d\n",
  1310. result);
  1311. }
  1312. static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
  1313. {
  1314. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1315. if (!transport->inet) {
  1316. struct sock *sk = sock->sk;
  1317. write_lock_bh(&sk->sk_callback_lock);
  1318. sk->sk_user_data = xprt;
  1319. transport->old_data_ready = sk->sk_data_ready;
  1320. transport->old_state_change = sk->sk_state_change;
  1321. transport->old_write_space = sk->sk_write_space;
  1322. sk->sk_data_ready = xs_tcp_data_ready;
  1323. sk->sk_state_change = xs_tcp_state_change;
  1324. sk->sk_write_space = xs_tcp_write_space;
  1325. sk->sk_allocation = GFP_ATOMIC;
  1326. /* socket options */
  1327. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  1328. sock_reset_flag(sk, SOCK_LINGER);
  1329. tcp_sk(sk)->linger2 = 0;
  1330. tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
  1331. xprt_clear_connected(xprt);
  1332. /* Reset to new socket */
  1333. transport->sock = sock;
  1334. transport->inet = sk;
  1335. write_unlock_bh(&sk->sk_callback_lock);
  1336. }
  1337. /* Tell the socket layer to start connecting... */
  1338. xprt->stat.connect_count++;
  1339. xprt->stat.connect_start = jiffies;
  1340. return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
  1341. }
  1342. /**
  1343. * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
  1344. * @work: RPC transport to connect
  1345. *
  1346. * Invoked by a work queue tasklet.
  1347. */
  1348. static void xs_tcp_connect_worker4(struct work_struct *work)
  1349. {
  1350. struct sock_xprt *transport =
  1351. container_of(work, struct sock_xprt, connect_worker.work);
  1352. struct rpc_xprt *xprt = &transport->xprt;
  1353. struct socket *sock = transport->sock;
  1354. int err, status = -EIO;
  1355. if (xprt->shutdown || !xprt_bound(xprt))
  1356. goto out;
  1357. if (!sock) {
  1358. /* start from scratch */
  1359. if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
  1360. dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
  1361. goto out;
  1362. }
  1363. xs_reclassify_socket4(sock);
  1364. if (xs_bind4(transport, sock) < 0) {
  1365. sock_release(sock);
  1366. goto out;
  1367. }
  1368. } else
  1369. /* "close" the socket, preserving the local port */
  1370. xs_tcp_reuse_connection(xprt);
  1371. dprintk("RPC: worker connecting xprt %p to address: %s\n",
  1372. xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
  1373. status = xs_tcp_finish_connecting(xprt, sock);
  1374. dprintk("RPC: %p connect status %d connected %d sock state %d\n",
  1375. xprt, -status, xprt_connected(xprt),
  1376. sock->sk->sk_state);
  1377. if (status < 0) {
  1378. switch (status) {
  1379. case -EINPROGRESS:
  1380. case -EALREADY:
  1381. goto out_clear;
  1382. case -ECONNREFUSED:
  1383. case -ECONNRESET:
  1384. /* retry with existing socket, after a delay */
  1385. break;
  1386. default:
  1387. /* get rid of existing socket, and retry */
  1388. xs_close(xprt);
  1389. break;
  1390. }
  1391. }
  1392. out:
  1393. xprt_wake_pending_tasks(xprt, status);
  1394. out_clear:
  1395. xprt_clear_connecting(xprt);
  1396. }
  1397. /**
  1398. * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
  1399. * @work: RPC transport to connect
  1400. *
  1401. * Invoked by a work queue tasklet.
  1402. */
  1403. static void xs_tcp_connect_worker6(struct work_struct *work)
  1404. {
  1405. struct sock_xprt *transport =
  1406. container_of(work, struct sock_xprt, connect_worker.work);
  1407. struct rpc_xprt *xprt = &transport->xprt;
  1408. struct socket *sock = transport->sock;
  1409. int err, status = -EIO;
  1410. if (xprt->shutdown || !xprt_bound(xprt))
  1411. goto out;
  1412. if (!sock) {
  1413. /* start from scratch */
  1414. if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
  1415. dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
  1416. goto out;
  1417. }
  1418. xs_reclassify_socket6(sock);
  1419. if (xs_bind6(transport, sock) < 0) {
  1420. sock_release(sock);
  1421. goto out;
  1422. }
  1423. } else
  1424. /* "close" the socket, preserving the local port */
  1425. xs_tcp_reuse_connection(xprt);
  1426. dprintk("RPC: worker connecting xprt %p to address: %s\n",
  1427. xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
  1428. status = xs_tcp_finish_connecting(xprt, sock);
  1429. dprintk("RPC: %p connect status %d connected %d sock state %d\n",
  1430. xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
  1431. if (status < 0) {
  1432. switch (status) {
  1433. case -EINPROGRESS:
  1434. case -EALREADY:
  1435. goto out_clear;
  1436. case -ECONNREFUSED:
  1437. case -ECONNRESET:
  1438. /* retry with existing socket, after a delay */
  1439. break;
  1440. default:
  1441. /* get rid of existing socket, and retry */
  1442. xs_close(xprt);
  1443. break;
  1444. }
  1445. }
  1446. out:
  1447. xprt_wake_pending_tasks(xprt, status);
  1448. out_clear:
  1449. xprt_clear_connecting(xprt);
  1450. }
  1451. /**
  1452. * xs_connect - connect a socket to a remote endpoint
  1453. * @task: address of RPC task that manages state of connect request
  1454. *
  1455. * TCP: If the remote end dropped the connection, delay reconnecting.
  1456. *
  1457. * UDP socket connects are synchronous, but we use a work queue anyway
  1458. * to guarantee that even unprivileged user processes can set up a
  1459. * socket on a privileged port.
  1460. *
  1461. * If a UDP socket connect fails, the delay behavior here prevents
  1462. * retry floods (hard mounts).
  1463. */
  1464. static void xs_connect(struct rpc_task *task)
  1465. {
  1466. struct rpc_xprt *xprt = task->tk_xprt;
  1467. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1468. if (xprt_test_and_set_connecting(xprt))
  1469. return;
  1470. if (transport->sock != NULL) {
  1471. dprintk("RPC: xs_connect delayed xprt %p for %lu "
  1472. "seconds\n",
  1473. xprt, xprt->reestablish_timeout / HZ);
  1474. queue_delayed_work(rpciod_workqueue,
  1475. &transport->connect_worker,
  1476. xprt->reestablish_timeout);
  1477. xprt->reestablish_timeout <<= 1;
  1478. if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
  1479. xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
  1480. } else {
  1481. dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
  1482. queue_delayed_work(rpciod_workqueue,
  1483. &transport->connect_worker, 0);
  1484. }
  1485. }
  1486. /**
  1487. * xs_udp_print_stats - display UDP socket-specifc stats
  1488. * @xprt: rpc_xprt struct containing statistics
  1489. * @seq: output file
  1490. *
  1491. */
  1492. static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  1493. {
  1494. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1495. seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
  1496. transport->port,
  1497. xprt->stat.bind_count,
  1498. xprt->stat.sends,
  1499. xprt->stat.recvs,
  1500. xprt->stat.bad_xids,
  1501. xprt->stat.req_u,
  1502. xprt->stat.bklog_u);
  1503. }
  1504. /**
  1505. * xs_tcp_print_stats - display TCP socket-specifc stats
  1506. * @xprt: rpc_xprt struct containing statistics
  1507. * @seq: output file
  1508. *
  1509. */
  1510. static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  1511. {
  1512. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1513. long idle_time = 0;
  1514. if (xprt_connected(xprt))
  1515. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  1516. seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
  1517. transport->port,
  1518. xprt->stat.bind_count,
  1519. xprt->stat.connect_count,
  1520. xprt->stat.connect_time,
  1521. idle_time,
  1522. xprt->stat.sends,
  1523. xprt->stat.recvs,
  1524. xprt->stat.bad_xids,
  1525. xprt->stat.req_u,
  1526. xprt->stat.bklog_u);
  1527. }
  1528. static struct rpc_xprt_ops xs_udp_ops = {
  1529. .set_buffer_size = xs_udp_set_buffer_size,
  1530. .reserve_xprt = xprt_reserve_xprt_cong,
  1531. .release_xprt = xprt_release_xprt_cong,
  1532. .rpcbind = rpcb_getport_async,
  1533. .set_port = xs_set_port,
  1534. .connect = xs_connect,
  1535. .buf_alloc = rpc_malloc,
  1536. .buf_free = rpc_free,
  1537. .send_request = xs_udp_send_request,
  1538. .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
  1539. .timer = xs_udp_timer,
  1540. .release_request = xprt_release_rqst_cong,
  1541. .close = xs_close,
  1542. .destroy = xs_destroy,
  1543. .print_stats = xs_udp_print_stats,
  1544. };
  1545. static struct rpc_xprt_ops xs_tcp_ops = {
  1546. .reserve_xprt = xprt_reserve_xprt,
  1547. .release_xprt = xs_tcp_release_xprt,
  1548. .rpcbind = rpcb_getport_async,
  1549. .set_port = xs_set_port,
  1550. .connect = xs_connect,
  1551. .buf_alloc = rpc_malloc,
  1552. .buf_free = rpc_free,
  1553. .send_request = xs_tcp_send_request,
  1554. .set_retrans_timeout = xprt_set_retrans_timeout_def,
  1555. .close = xs_close,
  1556. .destroy = xs_destroy,
  1557. .print_stats = xs_tcp_print_stats,
  1558. };
  1559. static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
  1560. unsigned int slot_table_size)
  1561. {
  1562. struct rpc_xprt *xprt;
  1563. struct sock_xprt *new;
  1564. if (args->addrlen > sizeof(xprt->addr)) {
  1565. dprintk("RPC: xs_setup_xprt: address too large\n");
  1566. return ERR_PTR(-EBADF);
  1567. }
  1568. new = kzalloc(sizeof(*new), GFP_KERNEL);
  1569. if (new == NULL) {
  1570. dprintk("RPC: xs_setup_xprt: couldn't allocate "
  1571. "rpc_xprt\n");
  1572. return ERR_PTR(-ENOMEM);
  1573. }
  1574. xprt = &new->xprt;
  1575. xprt->max_reqs = slot_table_size;
  1576. xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
  1577. if (xprt->slot == NULL) {
  1578. kfree(xprt);
  1579. dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
  1580. "table\n");
  1581. return ERR_PTR(-ENOMEM);
  1582. }
  1583. memcpy(&xprt->addr, args->dstaddr, args->addrlen);
  1584. xprt->addrlen = args->addrlen;
  1585. if (args->srcaddr)
  1586. memcpy(&new->addr, args->srcaddr, args->addrlen);
  1587. new->port = xs_get_random_port();
  1588. return xprt;
  1589. }
  1590. /**
  1591. * xs_setup_udp - Set up transport to use a UDP socket
  1592. * @args: rpc transport creation arguments
  1593. *
  1594. */
  1595. struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
  1596. {
  1597. struct sockaddr *addr = args->dstaddr;
  1598. struct rpc_xprt *xprt;
  1599. struct sock_xprt *transport;
  1600. xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
  1601. if (IS_ERR(xprt))
  1602. return xprt;
  1603. transport = container_of(xprt, struct sock_xprt, xprt);
  1604. xprt->prot = IPPROTO_UDP;
  1605. xprt->tsh_size = 0;
  1606. /* XXX: header size can vary due to auth type, IPv6, etc. */
  1607. xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
  1608. xprt->bind_timeout = XS_BIND_TO;
  1609. xprt->connect_timeout = XS_UDP_CONN_TO;
  1610. xprt->reestablish_timeout = XS_UDP_REEST_TO;
  1611. xprt->idle_timeout = XS_IDLE_DISC_TO;
  1612. xprt->ops = &xs_udp_ops;
  1613. if (args->timeout)
  1614. xprt->timeout = *args->timeout;
  1615. else
  1616. xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
  1617. switch (addr->sa_family) {
  1618. case AF_INET:
  1619. if (((struct sockaddr_in *)addr)->sin_port != htons(0))
  1620. xprt_set_bound(xprt);
  1621. INIT_DELAYED_WORK(&transport->connect_worker,
  1622. xs_udp_connect_worker4);
  1623. xs_format_ipv4_peer_addresses(xprt);
  1624. break;
  1625. case AF_INET6:
  1626. if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
  1627. xprt_set_bound(xprt);
  1628. INIT_DELAYED_WORK(&transport->connect_worker,
  1629. xs_udp_connect_worker6);
  1630. xs_format_ipv6_peer_addresses(xprt);
  1631. break;
  1632. default:
  1633. kfree(xprt);
  1634. return ERR_PTR(-EAFNOSUPPORT);
  1635. }
  1636. dprintk("RPC: set up transport to address %s\n",
  1637. xprt->address_strings[RPC_DISPLAY_ALL]);
  1638. if (try_module_get(THIS_MODULE))
  1639. return xprt;
  1640. kfree(xprt->slot);
  1641. kfree(xprt);
  1642. return ERR_PTR(-EINVAL);
  1643. }
  1644. /**
  1645. * xs_setup_tcp - Set up transport to use a TCP socket
  1646. * @args: rpc transport creation arguments
  1647. *
  1648. */
  1649. struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
  1650. {
  1651. struct sockaddr *addr = args->dstaddr;
  1652. struct rpc_xprt *xprt;
  1653. struct sock_xprt *transport;
  1654. xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
  1655. if (IS_ERR(xprt))
  1656. return xprt;
  1657. transport = container_of(xprt, struct sock_xprt, xprt);
  1658. xprt->prot = IPPROTO_TCP;
  1659. xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
  1660. xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
  1661. xprt->bind_timeout = XS_BIND_TO;
  1662. xprt->connect_timeout = XS_TCP_CONN_TO;
  1663. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  1664. xprt->idle_timeout = XS_IDLE_DISC_TO;
  1665. xprt->ops = &xs_tcp_ops;
  1666. if (args->timeout)
  1667. xprt->timeout = *args->timeout;
  1668. else
  1669. xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
  1670. switch (addr->sa_family) {
  1671. case AF_INET:
  1672. if (((struct sockaddr_in *)addr)->sin_port != htons(0))
  1673. xprt_set_bound(xprt);
  1674. INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
  1675. xs_format_ipv4_peer_addresses(xprt);
  1676. break;
  1677. case AF_INET6:
  1678. if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
  1679. xprt_set_bound(xprt);
  1680. INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
  1681. xs_format_ipv6_peer_addresses(xprt);
  1682. break;
  1683. default:
  1684. kfree(xprt);
  1685. return ERR_PTR(-EAFNOSUPPORT);
  1686. }
  1687. dprintk("RPC: set up transport to address %s\n",
  1688. xprt->address_strings[RPC_DISPLAY_ALL]);
  1689. if (try_module_get(THIS_MODULE))
  1690. return xprt;
  1691. kfree(xprt->slot);
  1692. kfree(xprt);
  1693. return ERR_PTR(-EINVAL);
  1694. }
  1695. static struct xprt_class xs_udp_transport = {
  1696. .list = LIST_HEAD_INIT(xs_udp_transport.list),
  1697. .name = "udp",
  1698. .owner = THIS_MODULE,
  1699. .ident = IPPROTO_UDP,
  1700. .setup = xs_setup_udp,
  1701. };
  1702. static struct xprt_class xs_tcp_transport = {
  1703. .list = LIST_HEAD_INIT(xs_tcp_transport.list),
  1704. .name = "tcp",
  1705. .owner = THIS_MODULE,
  1706. .ident = IPPROTO_TCP,
  1707. .setup = xs_setup_tcp,
  1708. };
  1709. /**
  1710. * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
  1711. *
  1712. */
  1713. int init_socket_xprt(void)
  1714. {
  1715. #ifdef RPC_DEBUG
  1716. if (!sunrpc_table_header)
  1717. sunrpc_table_header = register_sysctl_table(sunrpc_table);
  1718. #endif
  1719. xprt_register_transport(&xs_udp_transport);
  1720. xprt_register_transport(&xs_tcp_transport);
  1721. return 0;
  1722. }
  1723. /**
  1724. * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
  1725. *
  1726. */
  1727. void cleanup_socket_xprt(void)
  1728. {
  1729. #ifdef RPC_DEBUG
  1730. if (sunrpc_table_header) {
  1731. unregister_sysctl_table(sunrpc_table_header);
  1732. sunrpc_table_header = NULL;
  1733. }
  1734. #endif
  1735. xprt_unregister_transport(&xs_udp_transport);
  1736. xprt_unregister_transport(&xs_tcp_transport);
  1737. }