xprtsock.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/net/sunrpc/xprtsock.c
  4. *
  5. * Client-side transport implementation for sockets.
  6. *
  7. * TCP callback races fixes (C) 1998 Red Hat
  8. * TCP send fixes (C) 1998 Red Hat
  9. * TCP NFS related read + write fixes
  10. * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
  11. *
  12. * Rewrite of larges part of the code in order to stabilize TCP stuff.
  13. * Fix behaviour when socket buffer is full.
  14. * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
  15. *
  16. * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
  17. *
  18. * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
  19. * <gilles.quillard@bull.net>
  20. */
  21. #include <linux/types.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/capability.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/errno.h>
  28. #include <linux/socket.h>
  29. #include <linux/in.h>
  30. #include <linux/net.h>
  31. #include <linux/mm.h>
  32. #include <linux/un.h>
  33. #include <linux/udp.h>
  34. #include <linux/tcp.h>
  35. #include <linux/sunrpc/clnt.h>
  36. #include <linux/sunrpc/addr.h>
  37. #include <linux/sunrpc/sched.h>
  38. #include <linux/sunrpc/svcsock.h>
  39. #include <linux/sunrpc/xprtsock.h>
  40. #include <linux/file.h>
  41. #ifdef CONFIG_SUNRPC_BACKCHANNEL
  42. #include <linux/sunrpc/bc_xprt.h>
  43. #endif
  44. #include <net/sock.h>
  45. #include <net/checksum.h>
  46. #include <net/udp.h>
  47. #include <net/tcp.h>
  48. #include <trace/events/sunrpc.h>
  49. #include "sunrpc.h"
  50. static void xs_close(struct rpc_xprt *xprt);
  51. static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
  52. struct socket *sock);
  53. /*
  54. * xprtsock tunables
  55. */
  56. static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
  57. static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
  58. static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
  59. static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
  60. static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
  61. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  62. #define XS_TCP_LINGER_TO (15U * HZ)
  63. static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
  64. /*
  65. * We can register our own files under /proc/sys/sunrpc by
  66. * calling register_sysctl_table() again. The files in that
  67. * directory become the union of all files registered there.
  68. *
  69. * We simply need to make sure that we don't collide with
  70. * someone else's file names!
  71. */
  72. static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
  73. static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
  74. static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
  75. static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
  76. static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
  77. static struct ctl_table_header *sunrpc_table_header;
  78. /*
  79. * FIXME: changing the UDP slot table size should also resize the UDP
  80. * socket buffers for existing UDP transports
  81. */
  82. static struct ctl_table xs_tunables_table[] = {
  83. {
  84. .procname = "udp_slot_table_entries",
  85. .data = &xprt_udp_slot_table_entries,
  86. .maxlen = sizeof(unsigned int),
  87. .mode = 0644,
  88. .proc_handler = proc_dointvec_minmax,
  89. .extra1 = &min_slot_table_size,
  90. .extra2 = &max_slot_table_size
  91. },
  92. {
  93. .procname = "tcp_slot_table_entries",
  94. .data = &xprt_tcp_slot_table_entries,
  95. .maxlen = sizeof(unsigned int),
  96. .mode = 0644,
  97. .proc_handler = proc_dointvec_minmax,
  98. .extra1 = &min_slot_table_size,
  99. .extra2 = &max_slot_table_size
  100. },
  101. {
  102. .procname = "tcp_max_slot_table_entries",
  103. .data = &xprt_max_tcp_slot_table_entries,
  104. .maxlen = sizeof(unsigned int),
  105. .mode = 0644,
  106. .proc_handler = proc_dointvec_minmax,
  107. .extra1 = &min_slot_table_size,
  108. .extra2 = &max_tcp_slot_table_limit
  109. },
  110. {
  111. .procname = "min_resvport",
  112. .data = &xprt_min_resvport,
  113. .maxlen = sizeof(unsigned int),
  114. .mode = 0644,
  115. .proc_handler = proc_dointvec_minmax,
  116. .extra1 = &xprt_min_resvport_limit,
  117. .extra2 = &xprt_max_resvport_limit
  118. },
  119. {
  120. .procname = "max_resvport",
  121. .data = &xprt_max_resvport,
  122. .maxlen = sizeof(unsigned int),
  123. .mode = 0644,
  124. .proc_handler = proc_dointvec_minmax,
  125. .extra1 = &xprt_min_resvport_limit,
  126. .extra2 = &xprt_max_resvport_limit
  127. },
  128. {
  129. .procname = "tcp_fin_timeout",
  130. .data = &xs_tcp_fin_timeout,
  131. .maxlen = sizeof(xs_tcp_fin_timeout),
  132. .mode = 0644,
  133. .proc_handler = proc_dointvec_jiffies,
  134. },
  135. { },
  136. };
  137. static struct ctl_table sunrpc_table[] = {
  138. {
  139. .procname = "sunrpc",
  140. .mode = 0555,
  141. .child = xs_tunables_table
  142. },
  143. { },
  144. };
  145. #endif
  146. /*
  147. * Wait duration for a reply from the RPC portmapper.
  148. */
  149. #define XS_BIND_TO (60U * HZ)
  150. /*
  151. * Delay if a UDP socket connect error occurs. This is most likely some
  152. * kind of resource problem on the local host.
  153. */
  154. #define XS_UDP_REEST_TO (2U * HZ)
  155. /*
  156. * The reestablish timeout allows clients to delay for a bit before attempting
  157. * to reconnect to a server that just dropped our connection.
  158. *
  159. * We implement an exponential backoff when trying to reestablish a TCP
  160. * transport connection with the server. Some servers like to drop a TCP
  161. * connection when they are overworked, so we start with a short timeout and
  162. * increase over time if the server is down or not responding.
  163. */
  164. #define XS_TCP_INIT_REEST_TO (3U * HZ)
  165. /*
  166. * TCP idle timeout; client drops the transport socket if it is idle
  167. * for this long. Note that we also timeout UDP sockets to prevent
  168. * holding port numbers when there is no RPC traffic.
  169. */
  170. #define XS_IDLE_DISC_TO (5U * 60 * HZ)
  171. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  172. # undef RPC_DEBUG_DATA
  173. # define RPCDBG_FACILITY RPCDBG_TRANS
  174. #endif
  175. #ifdef RPC_DEBUG_DATA
  176. static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
  177. {
  178. u8 *buf = (u8 *) packet;
  179. int j;
  180. dprintk("RPC: %s\n", msg);
  181. for (j = 0; j < count && j < 128; j += 4) {
  182. if (!(j & 31)) {
  183. if (j)
  184. dprintk("\n");
  185. dprintk("0x%04x ", j);
  186. }
  187. dprintk("%02x%02x%02x%02x ",
  188. buf[j], buf[j+1], buf[j+2], buf[j+3]);
  189. }
  190. dprintk("\n");
  191. }
  192. #else
  193. static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
  194. {
  195. /* NOP */
  196. }
  197. #endif
  198. static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
  199. {
  200. return (struct rpc_xprt *) sk->sk_user_data;
  201. }
  202. static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
  203. {
  204. return (struct sockaddr *) &xprt->addr;
  205. }
  206. static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
  207. {
  208. return (struct sockaddr_un *) &xprt->addr;
  209. }
  210. static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
  211. {
  212. return (struct sockaddr_in *) &xprt->addr;
  213. }
  214. static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
  215. {
  216. return (struct sockaddr_in6 *) &xprt->addr;
  217. }
  218. static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
  219. {
  220. struct sockaddr *sap = xs_addr(xprt);
  221. struct sockaddr_in6 *sin6;
  222. struct sockaddr_in *sin;
  223. struct sockaddr_un *sun;
  224. char buf[128];
  225. switch (sap->sa_family) {
  226. case AF_LOCAL:
  227. sun = xs_addr_un(xprt);
  228. strlcpy(buf, sun->sun_path, sizeof(buf));
  229. xprt->address_strings[RPC_DISPLAY_ADDR] =
  230. kstrdup(buf, GFP_KERNEL);
  231. break;
  232. case AF_INET:
  233. (void)rpc_ntop(sap, buf, sizeof(buf));
  234. xprt->address_strings[RPC_DISPLAY_ADDR] =
  235. kstrdup(buf, GFP_KERNEL);
  236. sin = xs_addr_in(xprt);
  237. snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
  238. break;
  239. case AF_INET6:
  240. (void)rpc_ntop(sap, buf, sizeof(buf));
  241. xprt->address_strings[RPC_DISPLAY_ADDR] =
  242. kstrdup(buf, GFP_KERNEL);
  243. sin6 = xs_addr_in6(xprt);
  244. snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
  245. break;
  246. default:
  247. BUG();
  248. }
  249. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  250. }
  251. static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
  252. {
  253. struct sockaddr *sap = xs_addr(xprt);
  254. char buf[128];
  255. snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
  256. xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
  257. snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
  258. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
  259. }
  260. static void xs_format_peer_addresses(struct rpc_xprt *xprt,
  261. const char *protocol,
  262. const char *netid)
  263. {
  264. xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
  265. xprt->address_strings[RPC_DISPLAY_NETID] = netid;
  266. xs_format_common_peer_addresses(xprt);
  267. xs_format_common_peer_ports(xprt);
  268. }
  269. static void xs_update_peer_port(struct rpc_xprt *xprt)
  270. {
  271. kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
  272. kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
  273. xs_format_common_peer_ports(xprt);
  274. }
  275. static void xs_free_peer_addresses(struct rpc_xprt *xprt)
  276. {
  277. unsigned int i;
  278. for (i = 0; i < RPC_DISPLAY_MAX; i++)
  279. switch (i) {
  280. case RPC_DISPLAY_PROTO:
  281. case RPC_DISPLAY_NETID:
  282. continue;
  283. default:
  284. kfree(xprt->address_strings[i]);
  285. }
  286. }
  287. #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
  288. static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
  289. {
  290. struct msghdr msg = {
  291. .msg_name = addr,
  292. .msg_namelen = addrlen,
  293. .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
  294. };
  295. struct kvec iov = {
  296. .iov_base = vec->iov_base + base,
  297. .iov_len = vec->iov_len - base,
  298. };
  299. if (iov.iov_len != 0)
  300. return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
  301. return kernel_sendmsg(sock, &msg, NULL, 0, 0);
  302. }
  303. static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p)
  304. {
  305. ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
  306. int offset, size_t size, int flags);
  307. struct page **ppage;
  308. unsigned int remainder;
  309. int err;
  310. remainder = xdr->page_len - base;
  311. base += xdr->page_base;
  312. ppage = xdr->pages + (base >> PAGE_SHIFT);
  313. base &= ~PAGE_MASK;
  314. do_sendpage = sock->ops->sendpage;
  315. if (!zerocopy)
  316. do_sendpage = sock_no_sendpage;
  317. for(;;) {
  318. unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
  319. int flags = XS_SENDMSG_FLAGS;
  320. remainder -= len;
  321. if (more)
  322. flags |= MSG_MORE;
  323. if (remainder != 0)
  324. flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
  325. err = do_sendpage(sock, *ppage, base, len, flags);
  326. if (remainder == 0 || err != len)
  327. break;
  328. *sent_p += err;
  329. ppage++;
  330. base = 0;
  331. }
  332. if (err > 0) {
  333. *sent_p += err;
  334. err = 0;
  335. }
  336. return err;
  337. }
  338. /**
  339. * xs_sendpages - write pages directly to a socket
  340. * @sock: socket to send on
  341. * @addr: UDP only -- address of destination
  342. * @addrlen: UDP only -- length of destination address
  343. * @xdr: buffer containing this request
  344. * @base: starting position in the buffer
  345. * @zerocopy: true if it is safe to use sendpage()
  346. * @sent_p: return the total number of bytes successfully queued for sending
  347. *
  348. */
  349. static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p)
  350. {
  351. unsigned int remainder = xdr->len - base;
  352. int err = 0;
  353. int sent = 0;
  354. if (unlikely(!sock))
  355. return -ENOTSOCK;
  356. if (base != 0) {
  357. addr = NULL;
  358. addrlen = 0;
  359. }
  360. if (base < xdr->head[0].iov_len || addr != NULL) {
  361. unsigned int len = xdr->head[0].iov_len - base;
  362. remainder -= len;
  363. err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
  364. if (remainder == 0 || err != len)
  365. goto out;
  366. *sent_p += err;
  367. base = 0;
  368. } else
  369. base -= xdr->head[0].iov_len;
  370. if (base < xdr->page_len) {
  371. unsigned int len = xdr->page_len - base;
  372. remainder -= len;
  373. err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
  374. *sent_p += sent;
  375. if (remainder == 0 || sent != len)
  376. goto out;
  377. base = 0;
  378. } else
  379. base -= xdr->page_len;
  380. if (base >= xdr->tail[0].iov_len)
  381. return 0;
  382. err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
  383. out:
  384. if (err > 0) {
  385. *sent_p += err;
  386. err = 0;
  387. }
  388. return err;
  389. }
  390. static void xs_nospace_callback(struct rpc_task *task)
  391. {
  392. struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
  393. transport->inet->sk_write_pending--;
  394. }
  395. /**
  396. * xs_nospace - place task on wait queue if transmit was incomplete
  397. * @task: task to put to sleep
  398. *
  399. */
  400. static int xs_nospace(struct rpc_task *task)
  401. {
  402. struct rpc_rqst *req = task->tk_rqstp;
  403. struct rpc_xprt *xprt = req->rq_xprt;
  404. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  405. struct sock *sk = transport->inet;
  406. int ret = -EAGAIN;
  407. dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
  408. task->tk_pid, req->rq_slen - req->rq_bytes_sent,
  409. req->rq_slen);
  410. /* Protect against races with write_space */
  411. spin_lock_bh(&xprt->transport_lock);
  412. /* Don't race with disconnect */
  413. if (xprt_connected(xprt)) {
  414. /* wait for more buffer space */
  415. sk->sk_write_pending++;
  416. xprt_wait_for_buffer_space(task, xs_nospace_callback);
  417. } else
  418. ret = -ENOTCONN;
  419. spin_unlock_bh(&xprt->transport_lock);
  420. /* Race breaker in case memory is freed before above code is called */
  421. if (ret == -EAGAIN) {
  422. struct socket_wq *wq;
  423. rcu_read_lock();
  424. wq = rcu_dereference(sk->sk_wq);
  425. set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
  426. rcu_read_unlock();
  427. sk->sk_write_space(sk);
  428. }
  429. return ret;
  430. }
  431. /*
  432. * Construct a stream transport record marker in @buf.
  433. */
  434. static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
  435. {
  436. u32 reclen = buf->len - sizeof(rpc_fraghdr);
  437. rpc_fraghdr *base = buf->head[0].iov_base;
  438. *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
  439. }
  440. /**
  441. * xs_local_send_request - write an RPC request to an AF_LOCAL socket
  442. * @task: RPC task that manages the state of an RPC request
  443. *
  444. * Return values:
  445. * 0: The request has been sent
  446. * EAGAIN: The socket was blocked, please call again later to
  447. * complete the request
  448. * ENOTCONN: Caller needs to invoke connect logic then call again
  449. * other: Some other error occured, the request was not sent
  450. */
  451. static int xs_local_send_request(struct rpc_task *task)
  452. {
  453. struct rpc_rqst *req = task->tk_rqstp;
  454. struct rpc_xprt *xprt = req->rq_xprt;
  455. struct sock_xprt *transport =
  456. container_of(xprt, struct sock_xprt, xprt);
  457. struct xdr_buf *xdr = &req->rq_snd_buf;
  458. int status;
  459. int sent = 0;
  460. xs_encode_stream_record_marker(&req->rq_snd_buf);
  461. xs_pktdump("packet data:",
  462. req->rq_svec->iov_base, req->rq_svec->iov_len);
  463. status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent,
  464. true, &sent);
  465. dprintk("RPC: %s(%u) = %d\n",
  466. __func__, xdr->len - req->rq_bytes_sent, status);
  467. if (status == -EAGAIN && sock_writeable(transport->inet))
  468. status = -ENOBUFS;
  469. if (likely(sent > 0) || status == 0) {
  470. req->rq_bytes_sent += sent;
  471. req->rq_xmit_bytes_sent += sent;
  472. if (likely(req->rq_bytes_sent >= req->rq_slen)) {
  473. req->rq_bytes_sent = 0;
  474. return 0;
  475. }
  476. status = -EAGAIN;
  477. }
  478. switch (status) {
  479. case -ENOBUFS:
  480. break;
  481. case -EAGAIN:
  482. status = xs_nospace(task);
  483. break;
  484. default:
  485. dprintk("RPC: sendmsg returned unrecognized error %d\n",
  486. -status);
  487. case -EPIPE:
  488. xs_close(xprt);
  489. status = -ENOTCONN;
  490. }
  491. return status;
  492. }
  493. /**
  494. * xs_udp_send_request - write an RPC request to a UDP socket
  495. * @task: address of RPC task that manages the state of an RPC request
  496. *
  497. * Return values:
  498. * 0: The request has been sent
  499. * EAGAIN: The socket was blocked, please call again later to
  500. * complete the request
  501. * ENOTCONN: Caller needs to invoke connect logic then call again
  502. * other: Some other error occurred, the request was not sent
  503. */
  504. static int xs_udp_send_request(struct rpc_task *task)
  505. {
  506. struct rpc_rqst *req = task->tk_rqstp;
  507. struct rpc_xprt *xprt = req->rq_xprt;
  508. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  509. struct xdr_buf *xdr = &req->rq_snd_buf;
  510. int sent = 0;
  511. int status;
  512. xs_pktdump("packet data:",
  513. req->rq_svec->iov_base,
  514. req->rq_svec->iov_len);
  515. if (!xprt_bound(xprt))
  516. return -ENOTCONN;
  517. status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
  518. xdr, req->rq_bytes_sent, true, &sent);
  519. dprintk("RPC: xs_udp_send_request(%u) = %d\n",
  520. xdr->len - req->rq_bytes_sent, status);
  521. /* firewall is blocking us, don't return -EAGAIN or we end up looping */
  522. if (status == -EPERM)
  523. goto process_status;
  524. if (status == -EAGAIN && sock_writeable(transport->inet))
  525. status = -ENOBUFS;
  526. if (sent > 0 || status == 0) {
  527. req->rq_xmit_bytes_sent += sent;
  528. if (sent >= req->rq_slen)
  529. return 0;
  530. /* Still some bytes left; set up for a retry later. */
  531. status = -EAGAIN;
  532. }
  533. process_status:
  534. switch (status) {
  535. case -ENOTSOCK:
  536. status = -ENOTCONN;
  537. /* Should we call xs_close() here? */
  538. break;
  539. case -EAGAIN:
  540. status = xs_nospace(task);
  541. break;
  542. case -ENETUNREACH:
  543. case -ENOBUFS:
  544. case -EPIPE:
  545. case -ECONNREFUSED:
  546. case -EPERM:
  547. /* When the server has died, an ICMP port unreachable message
  548. * prompts ECONNREFUSED. */
  549. break;
  550. default:
  551. dprintk("RPC: sendmsg returned unrecognized error %d\n",
  552. -status);
  553. }
  554. return status;
  555. }
  556. /**
  557. * xs_tcp_send_request - write an RPC request to a TCP socket
  558. * @task: address of RPC task that manages the state of an RPC request
  559. *
  560. * Return values:
  561. * 0: The request has been sent
  562. * EAGAIN: The socket was blocked, please call again later to
  563. * complete the request
  564. * ENOTCONN: Caller needs to invoke connect logic then call again
  565. * other: Some other error occurred, the request was not sent
  566. *
  567. * XXX: In the case of soft timeouts, should we eventually give up
  568. * if sendmsg is not able to make progress?
  569. */
  570. static int xs_tcp_send_request(struct rpc_task *task)
  571. {
  572. struct rpc_rqst *req = task->tk_rqstp;
  573. struct rpc_xprt *xprt = req->rq_xprt;
  574. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  575. struct xdr_buf *xdr = &req->rq_snd_buf;
  576. bool zerocopy = true;
  577. bool vm_wait = false;
  578. int status;
  579. int sent;
  580. xs_encode_stream_record_marker(&req->rq_snd_buf);
  581. xs_pktdump("packet data:",
  582. req->rq_svec->iov_base,
  583. req->rq_svec->iov_len);
  584. /* Don't use zero copy if this is a resend. If the RPC call
  585. * completes while the socket holds a reference to the pages,
  586. * then we may end up resending corrupted data.
  587. */
  588. if (task->tk_flags & RPC_TASK_SENT)
  589. zerocopy = false;
  590. if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
  591. xs_tcp_set_socket_timeouts(xprt, transport->sock);
  592. /* Continue transmitting the packet/record. We must be careful
  593. * to cope with writespace callbacks arriving _after_ we have
  594. * called sendmsg(). */
  595. while (1) {
  596. sent = 0;
  597. status = xs_sendpages(transport->sock, NULL, 0, xdr,
  598. req->rq_bytes_sent, zerocopy, &sent);
  599. dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
  600. xdr->len - req->rq_bytes_sent, status);
  601. /* If we've sent the entire packet, immediately
  602. * reset the count of bytes sent. */
  603. req->rq_bytes_sent += sent;
  604. req->rq_xmit_bytes_sent += sent;
  605. if (likely(req->rq_bytes_sent >= req->rq_slen)) {
  606. req->rq_bytes_sent = 0;
  607. return 0;
  608. }
  609. WARN_ON_ONCE(sent == 0 && status == 0);
  610. if (status == -EAGAIN ) {
  611. /*
  612. * Return EAGAIN if we're sure we're hitting the
  613. * socket send buffer limits.
  614. */
  615. if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
  616. break;
  617. /*
  618. * Did we hit a memory allocation failure?
  619. */
  620. if (sent == 0) {
  621. status = -ENOBUFS;
  622. if (vm_wait)
  623. break;
  624. /* Retry, knowing now that we're below the
  625. * socket send buffer limit
  626. */
  627. vm_wait = true;
  628. }
  629. continue;
  630. }
  631. if (status < 0)
  632. break;
  633. vm_wait = false;
  634. }
  635. switch (status) {
  636. case -ENOTSOCK:
  637. status = -ENOTCONN;
  638. /* Should we call xs_close() here? */
  639. break;
  640. case -EAGAIN:
  641. status = xs_nospace(task);
  642. break;
  643. case -ECONNRESET:
  644. case -ECONNREFUSED:
  645. case -ENOTCONN:
  646. case -EADDRINUSE:
  647. case -ENOBUFS:
  648. case -EPIPE:
  649. break;
  650. default:
  651. dprintk("RPC: sendmsg returned unrecognized error %d\n",
  652. -status);
  653. }
  654. return status;
  655. }
  656. /**
  657. * xs_tcp_release_xprt - clean up after a tcp transmission
  658. * @xprt: transport
  659. * @task: rpc task
  660. *
  661. * This cleans up if an error causes us to abort the transmission of a request.
  662. * In this case, the socket may need to be reset in order to avoid confusing
  663. * the server.
  664. */
  665. static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  666. {
  667. struct rpc_rqst *req;
  668. if (task != xprt->snd_task)
  669. return;
  670. if (task == NULL)
  671. goto out_release;
  672. req = task->tk_rqstp;
  673. if (req == NULL)
  674. goto out_release;
  675. if (req->rq_bytes_sent == 0)
  676. goto out_release;
  677. if (req->rq_bytes_sent == req->rq_snd_buf.len)
  678. goto out_release;
  679. set_bit(XPRT_CLOSE_WAIT, &xprt->state);
  680. out_release:
  681. xprt_release_xprt(xprt, task);
  682. }
  683. static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
  684. {
  685. transport->old_data_ready = sk->sk_data_ready;
  686. transport->old_state_change = sk->sk_state_change;
  687. transport->old_write_space = sk->sk_write_space;
  688. transport->old_error_report = sk->sk_error_report;
  689. }
  690. static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
  691. {
  692. sk->sk_data_ready = transport->old_data_ready;
  693. sk->sk_state_change = transport->old_state_change;
  694. sk->sk_write_space = transport->old_write_space;
  695. sk->sk_error_report = transport->old_error_report;
  696. }
  697. static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
  698. {
  699. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  700. clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
  701. }
  702. static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
  703. {
  704. smp_mb__before_atomic();
  705. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  706. clear_bit(XPRT_CLOSING, &xprt->state);
  707. xs_sock_reset_state_flags(xprt);
  708. smp_mb__after_atomic();
  709. }
  710. static void xs_sock_mark_closed(struct rpc_xprt *xprt)
  711. {
  712. xs_sock_reset_connection_flags(xprt);
  713. /* Mark transport as closed and wake up all pending tasks */
  714. xprt_disconnect_done(xprt);
  715. }
  716. /**
  717. * xs_error_report - callback to handle TCP socket state errors
  718. * @sk: socket
  719. *
  720. * Note: we don't call sock_error() since there may be a rpc_task
  721. * using the socket, and so we don't want to clear sk->sk_err.
  722. */
  723. static void xs_error_report(struct sock *sk)
  724. {
  725. struct rpc_xprt *xprt;
  726. int err;
  727. read_lock_bh(&sk->sk_callback_lock);
  728. if (!(xprt = xprt_from_sock(sk)))
  729. goto out;
  730. err = -sk->sk_err;
  731. if (err == 0)
  732. goto out;
  733. /* Is this a reset event? */
  734. if (sk->sk_state == TCP_CLOSE)
  735. xs_sock_mark_closed(xprt);
  736. dprintk("RPC: xs_error_report client %p, error=%d...\n",
  737. xprt, -err);
  738. trace_rpc_socket_error(xprt, sk->sk_socket, err);
  739. xprt_wake_pending_tasks(xprt, err);
  740. out:
  741. read_unlock_bh(&sk->sk_callback_lock);
  742. }
  743. static void xs_reset_transport(struct sock_xprt *transport)
  744. {
  745. struct socket *sock = transport->sock;
  746. struct sock *sk = transport->inet;
  747. struct rpc_xprt *xprt = &transport->xprt;
  748. if (sk == NULL)
  749. return;
  750. if (atomic_read(&transport->xprt.swapper))
  751. sk_clear_memalloc(sk);
  752. kernel_sock_shutdown(sock, SHUT_RDWR);
  753. mutex_lock(&transport->recv_mutex);
  754. write_lock_bh(&sk->sk_callback_lock);
  755. transport->inet = NULL;
  756. transport->sock = NULL;
  757. sk->sk_user_data = NULL;
  758. xs_restore_old_callbacks(transport, sk);
  759. xprt_clear_connected(xprt);
  760. write_unlock_bh(&sk->sk_callback_lock);
  761. xs_sock_reset_connection_flags(xprt);
  762. mutex_unlock(&transport->recv_mutex);
  763. trace_rpc_socket_close(xprt, sock);
  764. sock_release(sock);
  765. }
  766. /**
  767. * xs_close - close a socket
  768. * @xprt: transport
  769. *
  770. * This is used when all requests are complete; ie, no DRC state remains
  771. * on the server we want to save.
  772. *
  773. * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
  774. * xs_reset_transport() zeroing the socket from underneath a writer.
  775. */
  776. static void xs_close(struct rpc_xprt *xprt)
  777. {
  778. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  779. dprintk("RPC: xs_close xprt %p\n", xprt);
  780. xs_reset_transport(transport);
  781. xprt->reestablish_timeout = 0;
  782. xprt_disconnect_done(xprt);
  783. }
  784. static void xs_inject_disconnect(struct rpc_xprt *xprt)
  785. {
  786. dprintk("RPC: injecting transport disconnect on xprt=%p\n",
  787. xprt);
  788. xprt_disconnect_done(xprt);
  789. }
  790. static void xs_xprt_free(struct rpc_xprt *xprt)
  791. {
  792. xs_free_peer_addresses(xprt);
  793. xprt_free(xprt);
  794. }
  795. /**
  796. * xs_destroy - prepare to shutdown a transport
  797. * @xprt: doomed transport
  798. *
  799. */
  800. static void xs_destroy(struct rpc_xprt *xprt)
  801. {
  802. struct sock_xprt *transport = container_of(xprt,
  803. struct sock_xprt, xprt);
  804. dprintk("RPC: xs_destroy xprt %p\n", xprt);
  805. cancel_delayed_work_sync(&transport->connect_worker);
  806. xs_close(xprt);
  807. cancel_work_sync(&transport->recv_worker);
  808. xs_xprt_free(xprt);
  809. module_put(THIS_MODULE);
  810. }
  811. static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
  812. {
  813. struct xdr_skb_reader desc = {
  814. .skb = skb,
  815. .offset = sizeof(rpc_fraghdr),
  816. .count = skb->len - sizeof(rpc_fraghdr),
  817. };
  818. if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
  819. return -1;
  820. if (desc.count)
  821. return -1;
  822. return 0;
  823. }
  824. /**
  825. * xs_local_data_read_skb
  826. * @xprt: transport
  827. * @sk: socket
  828. * @skb: skbuff
  829. *
  830. * Currently this assumes we can read the whole reply in a single gulp.
  831. */
  832. static void xs_local_data_read_skb(struct rpc_xprt *xprt,
  833. struct sock *sk,
  834. struct sk_buff *skb)
  835. {
  836. struct rpc_task *task;
  837. struct rpc_rqst *rovr;
  838. int repsize, copied;
  839. u32 _xid;
  840. __be32 *xp;
  841. repsize = skb->len - sizeof(rpc_fraghdr);
  842. if (repsize < 4) {
  843. dprintk("RPC: impossible RPC reply size %d\n", repsize);
  844. return;
  845. }
  846. /* Copy the XID from the skb... */
  847. xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
  848. if (xp == NULL)
  849. return;
  850. /* Look up and lock the request corresponding to the given XID */
  851. spin_lock(&xprt->recv_lock);
  852. rovr = xprt_lookup_rqst(xprt, *xp);
  853. if (!rovr)
  854. goto out_unlock;
  855. xprt_pin_rqst(rovr);
  856. spin_unlock(&xprt->recv_lock);
  857. task = rovr->rq_task;
  858. copied = rovr->rq_private_buf.buflen;
  859. if (copied > repsize)
  860. copied = repsize;
  861. if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
  862. dprintk("RPC: sk_buff copy failed\n");
  863. spin_lock(&xprt->recv_lock);
  864. goto out_unpin;
  865. }
  866. spin_lock(&xprt->recv_lock);
  867. xprt_complete_rqst(task, copied);
  868. out_unpin:
  869. xprt_unpin_rqst(rovr);
  870. out_unlock:
  871. spin_unlock(&xprt->recv_lock);
  872. }
  873. static void xs_local_data_receive(struct sock_xprt *transport)
  874. {
  875. struct sk_buff *skb;
  876. struct sock *sk;
  877. int err;
  878. mutex_lock(&transport->recv_mutex);
  879. sk = transport->inet;
  880. if (sk == NULL)
  881. goto out;
  882. for (;;) {
  883. skb = skb_recv_datagram(sk, 0, 1, &err);
  884. if (skb != NULL) {
  885. xs_local_data_read_skb(&transport->xprt, sk, skb);
  886. skb_free_datagram(sk, skb);
  887. continue;
  888. }
  889. if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
  890. break;
  891. }
  892. out:
  893. mutex_unlock(&transport->recv_mutex);
  894. }
  895. static void xs_local_data_receive_workfn(struct work_struct *work)
  896. {
  897. struct sock_xprt *transport =
  898. container_of(work, struct sock_xprt, recv_worker);
  899. xs_local_data_receive(transport);
  900. }
  901. /**
  902. * xs_udp_data_read_skb - receive callback for UDP sockets
  903. * @xprt: transport
  904. * @sk: socket
  905. * @skb: skbuff
  906. *
  907. */
  908. static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
  909. struct sock *sk,
  910. struct sk_buff *skb)
  911. {
  912. struct rpc_task *task;
  913. struct rpc_rqst *rovr;
  914. int repsize, copied;
  915. u32 _xid;
  916. __be32 *xp;
  917. repsize = skb->len;
  918. if (repsize < 4) {
  919. dprintk("RPC: impossible RPC reply size %d!\n", repsize);
  920. return;
  921. }
  922. /* Copy the XID from the skb... */
  923. xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
  924. if (xp == NULL)
  925. return;
  926. /* Look up and lock the request corresponding to the given XID */
  927. spin_lock(&xprt->recv_lock);
  928. rovr = xprt_lookup_rqst(xprt, *xp);
  929. if (!rovr)
  930. goto out_unlock;
  931. xprt_pin_rqst(rovr);
  932. spin_unlock(&xprt->recv_lock);
  933. task = rovr->rq_task;
  934. if ((copied = rovr->rq_private_buf.buflen) > repsize)
  935. copied = repsize;
  936. /* Suck it into the iovec, verify checksum if not done by hw. */
  937. if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
  938. spin_lock(&xprt->recv_lock);
  939. __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
  940. goto out_unpin;
  941. }
  942. spin_lock_bh(&xprt->transport_lock);
  943. xprt_adjust_cwnd(xprt, task, copied);
  944. spin_unlock_bh(&xprt->transport_lock);
  945. spin_lock(&xprt->recv_lock);
  946. xprt_complete_rqst(task, copied);
  947. __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
  948. out_unpin:
  949. xprt_unpin_rqst(rovr);
  950. out_unlock:
  951. spin_unlock(&xprt->recv_lock);
  952. }
  953. static void xs_udp_data_receive(struct sock_xprt *transport)
  954. {
  955. struct sk_buff *skb;
  956. struct sock *sk;
  957. int err;
  958. mutex_lock(&transport->recv_mutex);
  959. sk = transport->inet;
  960. if (sk == NULL)
  961. goto out;
  962. for (;;) {
  963. skb = skb_recv_udp(sk, 0, 1, &err);
  964. if (skb != NULL) {
  965. xs_udp_data_read_skb(&transport->xprt, sk, skb);
  966. consume_skb(skb);
  967. continue;
  968. }
  969. if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
  970. break;
  971. }
  972. out:
  973. mutex_unlock(&transport->recv_mutex);
  974. }
  975. static void xs_udp_data_receive_workfn(struct work_struct *work)
  976. {
  977. struct sock_xprt *transport =
  978. container_of(work, struct sock_xprt, recv_worker);
  979. xs_udp_data_receive(transport);
  980. }
  981. /**
  982. * xs_data_ready - "data ready" callback for UDP sockets
  983. * @sk: socket with data to read
  984. *
  985. */
  986. static void xs_data_ready(struct sock *sk)
  987. {
  988. struct rpc_xprt *xprt;
  989. read_lock_bh(&sk->sk_callback_lock);
  990. dprintk("RPC: xs_data_ready...\n");
  991. xprt = xprt_from_sock(sk);
  992. if (xprt != NULL) {
  993. struct sock_xprt *transport = container_of(xprt,
  994. struct sock_xprt, xprt);
  995. transport->old_data_ready(sk);
  996. /* Any data means we had a useful conversation, so
  997. * then we don't need to delay the next reconnect
  998. */
  999. if (xprt->reestablish_timeout)
  1000. xprt->reestablish_timeout = 0;
  1001. if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
  1002. queue_work(xprtiod_workqueue, &transport->recv_worker);
  1003. }
  1004. read_unlock_bh(&sk->sk_callback_lock);
  1005. }
  1006. /*
  1007. * Helper function to force a TCP close if the server is sending
  1008. * junk and/or it has put us in CLOSE_WAIT
  1009. */
  1010. static void xs_tcp_force_close(struct rpc_xprt *xprt)
  1011. {
  1012. xprt_force_disconnect(xprt);
  1013. }
  1014. static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
  1015. {
  1016. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1017. size_t len, used;
  1018. char *p;
  1019. p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
  1020. len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
  1021. used = xdr_skb_read_bits(desc, p, len);
  1022. transport->tcp_offset += used;
  1023. if (used != len)
  1024. return;
  1025. transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
  1026. if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
  1027. transport->tcp_flags |= TCP_RCV_LAST_FRAG;
  1028. else
  1029. transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
  1030. transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
  1031. transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
  1032. transport->tcp_offset = 0;
  1033. /* Sanity check of the record length */
  1034. if (unlikely(transport->tcp_reclen < 8)) {
  1035. dprintk("RPC: invalid TCP record fragment length\n");
  1036. xs_tcp_force_close(xprt);
  1037. return;
  1038. }
  1039. dprintk("RPC: reading TCP record fragment of length %d\n",
  1040. transport->tcp_reclen);
  1041. }
  1042. static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
  1043. {
  1044. if (transport->tcp_offset == transport->tcp_reclen) {
  1045. transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
  1046. transport->tcp_offset = 0;
  1047. if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
  1048. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1049. transport->tcp_flags |= TCP_RCV_COPY_XID;
  1050. transport->tcp_copied = 0;
  1051. }
  1052. }
  1053. }
  1054. static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
  1055. {
  1056. size_t len, used;
  1057. char *p;
  1058. len = sizeof(transport->tcp_xid) - transport->tcp_offset;
  1059. dprintk("RPC: reading XID (%zu bytes)\n", len);
  1060. p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
  1061. used = xdr_skb_read_bits(desc, p, len);
  1062. transport->tcp_offset += used;
  1063. if (used != len)
  1064. return;
  1065. transport->tcp_flags &= ~TCP_RCV_COPY_XID;
  1066. transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
  1067. transport->tcp_copied = 4;
  1068. dprintk("RPC: reading %s XID %08x\n",
  1069. (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
  1070. : "request with",
  1071. ntohl(transport->tcp_xid));
  1072. xs_tcp_check_fraghdr(transport);
  1073. }
  1074. static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
  1075. struct xdr_skb_reader *desc)
  1076. {
  1077. size_t len, used;
  1078. u32 offset;
  1079. char *p;
  1080. /*
  1081. * We want transport->tcp_offset to be 8 at the end of this routine
  1082. * (4 bytes for the xid and 4 bytes for the call/reply flag).
  1083. * When this function is called for the first time,
  1084. * transport->tcp_offset is 4 (after having already read the xid).
  1085. */
  1086. offset = transport->tcp_offset - sizeof(transport->tcp_xid);
  1087. len = sizeof(transport->tcp_calldir) - offset;
  1088. dprintk("RPC: reading CALL/REPLY flag (%zu bytes)\n", len);
  1089. p = ((char *) &transport->tcp_calldir) + offset;
  1090. used = xdr_skb_read_bits(desc, p, len);
  1091. transport->tcp_offset += used;
  1092. if (used != len)
  1093. return;
  1094. transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
  1095. /*
  1096. * We don't yet have the XDR buffer, so we will write the calldir
  1097. * out after we get the buffer from the 'struct rpc_rqst'
  1098. */
  1099. switch (ntohl(transport->tcp_calldir)) {
  1100. case RPC_REPLY:
  1101. transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
  1102. transport->tcp_flags |= TCP_RCV_COPY_DATA;
  1103. transport->tcp_flags |= TCP_RPC_REPLY;
  1104. break;
  1105. case RPC_CALL:
  1106. transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
  1107. transport->tcp_flags |= TCP_RCV_COPY_DATA;
  1108. transport->tcp_flags &= ~TCP_RPC_REPLY;
  1109. break;
  1110. default:
  1111. dprintk("RPC: invalid request message type\n");
  1112. xs_tcp_force_close(&transport->xprt);
  1113. }
  1114. xs_tcp_check_fraghdr(transport);
  1115. }
  1116. static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
  1117. struct xdr_skb_reader *desc,
  1118. struct rpc_rqst *req)
  1119. {
  1120. struct sock_xprt *transport =
  1121. container_of(xprt, struct sock_xprt, xprt);
  1122. struct xdr_buf *rcvbuf;
  1123. size_t len;
  1124. ssize_t r;
  1125. rcvbuf = &req->rq_private_buf;
  1126. if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
  1127. /*
  1128. * Save the RPC direction in the XDR buffer
  1129. */
  1130. memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
  1131. &transport->tcp_calldir,
  1132. sizeof(transport->tcp_calldir));
  1133. transport->tcp_copied += sizeof(transport->tcp_calldir);
  1134. transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
  1135. }
  1136. len = desc->count;
  1137. if (len > transport->tcp_reclen - transport->tcp_offset)
  1138. desc->count = transport->tcp_reclen - transport->tcp_offset;
  1139. r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
  1140. desc, xdr_skb_read_bits);
  1141. if (desc->count) {
  1142. /* Error when copying to the receive buffer,
  1143. * usually because we weren't able to allocate
  1144. * additional buffer pages. All we can do now
  1145. * is turn off TCP_RCV_COPY_DATA, so the request
  1146. * will not receive any additional updates,
  1147. * and time out.
  1148. * Any remaining data from this record will
  1149. * be discarded.
  1150. */
  1151. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1152. dprintk("RPC: XID %08x truncated request\n",
  1153. ntohl(transport->tcp_xid));
  1154. dprintk("RPC: xprt = %p, tcp_copied = %lu, "
  1155. "tcp_offset = %u, tcp_reclen = %u\n",
  1156. xprt, transport->tcp_copied,
  1157. transport->tcp_offset, transport->tcp_reclen);
  1158. return;
  1159. }
  1160. transport->tcp_copied += r;
  1161. transport->tcp_offset += r;
  1162. desc->count = len - r;
  1163. dprintk("RPC: XID %08x read %zd bytes\n",
  1164. ntohl(transport->tcp_xid), r);
  1165. dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
  1166. "tcp_reclen = %u\n", xprt, transport->tcp_copied,
  1167. transport->tcp_offset, transport->tcp_reclen);
  1168. if (transport->tcp_copied == req->rq_private_buf.buflen)
  1169. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1170. else if (transport->tcp_offset == transport->tcp_reclen) {
  1171. if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
  1172. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1173. }
  1174. }
  1175. /*
  1176. * Finds the request corresponding to the RPC xid and invokes the common
  1177. * tcp read code to read the data.
  1178. */
  1179. static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
  1180. struct xdr_skb_reader *desc)
  1181. {
  1182. struct sock_xprt *transport =
  1183. container_of(xprt, struct sock_xprt, xprt);
  1184. struct rpc_rqst *req;
  1185. dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
  1186. /* Find and lock the request corresponding to this xid */
  1187. spin_lock(&xprt->recv_lock);
  1188. req = xprt_lookup_rqst(xprt, transport->tcp_xid);
  1189. if (!req) {
  1190. dprintk("RPC: XID %08x request not found!\n",
  1191. ntohl(transport->tcp_xid));
  1192. spin_unlock(&xprt->recv_lock);
  1193. return -1;
  1194. }
  1195. xprt_pin_rqst(req);
  1196. spin_unlock(&xprt->recv_lock);
  1197. xs_tcp_read_common(xprt, desc, req);
  1198. spin_lock(&xprt->recv_lock);
  1199. if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
  1200. xprt_complete_rqst(req->rq_task, transport->tcp_copied);
  1201. xprt_unpin_rqst(req);
  1202. spin_unlock(&xprt->recv_lock);
  1203. return 0;
  1204. }
  1205. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1206. /*
  1207. * Obtains an rpc_rqst previously allocated and invokes the common
  1208. * tcp read code to read the data. The result is placed in the callback
  1209. * queue.
  1210. * If we're unable to obtain the rpc_rqst we schedule the closing of the
  1211. * connection and return -1.
  1212. */
  1213. static int xs_tcp_read_callback(struct rpc_xprt *xprt,
  1214. struct xdr_skb_reader *desc)
  1215. {
  1216. struct sock_xprt *transport =
  1217. container_of(xprt, struct sock_xprt, xprt);
  1218. struct rpc_rqst *req;
  1219. /* Look up the request corresponding to the given XID */
  1220. req = xprt_lookup_bc_request(xprt, transport->tcp_xid);
  1221. if (req == NULL) {
  1222. printk(KERN_WARNING "Callback slot table overflowed\n");
  1223. xprt_force_disconnect(xprt);
  1224. return -1;
  1225. }
  1226. dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
  1227. xs_tcp_read_common(xprt, desc, req);
  1228. if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
  1229. xprt_complete_bc_request(req, transport->tcp_copied);
  1230. return 0;
  1231. }
  1232. static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
  1233. struct xdr_skb_reader *desc)
  1234. {
  1235. struct sock_xprt *transport =
  1236. container_of(xprt, struct sock_xprt, xprt);
  1237. return (transport->tcp_flags & TCP_RPC_REPLY) ?
  1238. xs_tcp_read_reply(xprt, desc) :
  1239. xs_tcp_read_callback(xprt, desc);
  1240. }
  1241. static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
  1242. {
  1243. int ret;
  1244. ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
  1245. SVC_SOCK_ANONYMOUS);
  1246. if (ret < 0)
  1247. return ret;
  1248. return 0;
  1249. }
  1250. static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
  1251. {
  1252. return PAGE_SIZE;
  1253. }
  1254. #else
  1255. static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
  1256. struct xdr_skb_reader *desc)
  1257. {
  1258. return xs_tcp_read_reply(xprt, desc);
  1259. }
  1260. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1261. /*
  1262. * Read data off the transport. This can be either an RPC_CALL or an
  1263. * RPC_REPLY. Relay the processing to helper functions.
  1264. */
  1265. static void xs_tcp_read_data(struct rpc_xprt *xprt,
  1266. struct xdr_skb_reader *desc)
  1267. {
  1268. struct sock_xprt *transport =
  1269. container_of(xprt, struct sock_xprt, xprt);
  1270. if (_xs_tcp_read_data(xprt, desc) == 0)
  1271. xs_tcp_check_fraghdr(transport);
  1272. else {
  1273. /*
  1274. * The transport_lock protects the request handling.
  1275. * There's no need to hold it to update the tcp_flags.
  1276. */
  1277. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1278. }
  1279. }
  1280. static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
  1281. {
  1282. size_t len;
  1283. len = transport->tcp_reclen - transport->tcp_offset;
  1284. if (len > desc->count)
  1285. len = desc->count;
  1286. desc->count -= len;
  1287. desc->offset += len;
  1288. transport->tcp_offset += len;
  1289. dprintk("RPC: discarded %zu bytes\n", len);
  1290. xs_tcp_check_fraghdr(transport);
  1291. }
  1292. static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
  1293. {
  1294. struct rpc_xprt *xprt = rd_desc->arg.data;
  1295. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1296. struct xdr_skb_reader desc = {
  1297. .skb = skb,
  1298. .offset = offset,
  1299. .count = len,
  1300. };
  1301. dprintk("RPC: xs_tcp_data_recv started\n");
  1302. do {
  1303. trace_xs_tcp_data_recv(transport);
  1304. /* Read in a new fragment marker if necessary */
  1305. /* Can we ever really expect to get completely empty fragments? */
  1306. if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
  1307. xs_tcp_read_fraghdr(xprt, &desc);
  1308. continue;
  1309. }
  1310. /* Read in the xid if necessary */
  1311. if (transport->tcp_flags & TCP_RCV_COPY_XID) {
  1312. xs_tcp_read_xid(transport, &desc);
  1313. continue;
  1314. }
  1315. /* Read in the call/reply flag */
  1316. if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
  1317. xs_tcp_read_calldir(transport, &desc);
  1318. continue;
  1319. }
  1320. /* Read in the request data */
  1321. if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
  1322. xs_tcp_read_data(xprt, &desc);
  1323. continue;
  1324. }
  1325. /* Skip over any trailing bytes on short reads */
  1326. xs_tcp_read_discard(transport, &desc);
  1327. } while (desc.count);
  1328. trace_xs_tcp_data_recv(transport);
  1329. dprintk("RPC: xs_tcp_data_recv done\n");
  1330. return len - desc.count;
  1331. }
  1332. static void xs_tcp_data_receive(struct sock_xprt *transport)
  1333. {
  1334. struct rpc_xprt *xprt = &transport->xprt;
  1335. struct sock *sk;
  1336. read_descriptor_t rd_desc = {
  1337. .count = 2*1024*1024,
  1338. .arg.data = xprt,
  1339. };
  1340. unsigned long total = 0;
  1341. int loop;
  1342. int read = 0;
  1343. mutex_lock(&transport->recv_mutex);
  1344. sk = transport->inet;
  1345. if (sk == NULL)
  1346. goto out;
  1347. /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
  1348. for (loop = 0; loop < 64; loop++) {
  1349. lock_sock(sk);
  1350. read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
  1351. if (read <= 0) {
  1352. clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
  1353. release_sock(sk);
  1354. break;
  1355. }
  1356. release_sock(sk);
  1357. total += read;
  1358. rd_desc.count = 65536;
  1359. }
  1360. if (test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
  1361. queue_work(xprtiod_workqueue, &transport->recv_worker);
  1362. out:
  1363. mutex_unlock(&transport->recv_mutex);
  1364. trace_xs_tcp_data_ready(xprt, read, total);
  1365. }
  1366. static void xs_tcp_data_receive_workfn(struct work_struct *work)
  1367. {
  1368. struct sock_xprt *transport =
  1369. container_of(work, struct sock_xprt, recv_worker);
  1370. xs_tcp_data_receive(transport);
  1371. }
  1372. /**
  1373. * xs_tcp_state_change - callback to handle TCP socket state changes
  1374. * @sk: socket whose state has changed
  1375. *
  1376. */
  1377. static void xs_tcp_state_change(struct sock *sk)
  1378. {
  1379. struct rpc_xprt *xprt;
  1380. struct sock_xprt *transport;
  1381. read_lock_bh(&sk->sk_callback_lock);
  1382. if (!(xprt = xprt_from_sock(sk)))
  1383. goto out;
  1384. dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
  1385. dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
  1386. sk->sk_state, xprt_connected(xprt),
  1387. sock_flag(sk, SOCK_DEAD),
  1388. sock_flag(sk, SOCK_ZAPPED),
  1389. sk->sk_shutdown);
  1390. transport = container_of(xprt, struct sock_xprt, xprt);
  1391. trace_rpc_socket_state_change(xprt, sk->sk_socket);
  1392. switch (sk->sk_state) {
  1393. case TCP_ESTABLISHED:
  1394. spin_lock(&xprt->transport_lock);
  1395. if (!xprt_test_and_set_connected(xprt)) {
  1396. /* Reset TCP record info */
  1397. transport->tcp_offset = 0;
  1398. transport->tcp_reclen = 0;
  1399. transport->tcp_copied = 0;
  1400. transport->tcp_flags =
  1401. TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
  1402. xprt->connect_cookie++;
  1403. clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
  1404. xprt_clear_connecting(xprt);
  1405. xprt->stat.connect_count++;
  1406. xprt->stat.connect_time += (long)jiffies -
  1407. xprt->stat.connect_start;
  1408. xprt_wake_pending_tasks(xprt, -EAGAIN);
  1409. }
  1410. spin_unlock(&xprt->transport_lock);
  1411. break;
  1412. case TCP_FIN_WAIT1:
  1413. /* The client initiated a shutdown of the socket */
  1414. xprt->connect_cookie++;
  1415. xprt->reestablish_timeout = 0;
  1416. set_bit(XPRT_CLOSING, &xprt->state);
  1417. smp_mb__before_atomic();
  1418. clear_bit(XPRT_CONNECTED, &xprt->state);
  1419. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  1420. smp_mb__after_atomic();
  1421. break;
  1422. case TCP_CLOSE_WAIT:
  1423. /* The server initiated a shutdown of the socket */
  1424. xprt->connect_cookie++;
  1425. clear_bit(XPRT_CONNECTED, &xprt->state);
  1426. xs_tcp_force_close(xprt);
  1427. case TCP_CLOSING:
  1428. /*
  1429. * If the server closed down the connection, make sure that
  1430. * we back off before reconnecting
  1431. */
  1432. if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
  1433. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  1434. break;
  1435. case TCP_LAST_ACK:
  1436. set_bit(XPRT_CLOSING, &xprt->state);
  1437. smp_mb__before_atomic();
  1438. clear_bit(XPRT_CONNECTED, &xprt->state);
  1439. smp_mb__after_atomic();
  1440. break;
  1441. case TCP_CLOSE:
  1442. if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
  1443. &transport->sock_state))
  1444. xprt_clear_connecting(xprt);
  1445. if (sk->sk_err)
  1446. xprt_wake_pending_tasks(xprt, -sk->sk_err);
  1447. xs_sock_mark_closed(xprt);
  1448. }
  1449. out:
  1450. read_unlock_bh(&sk->sk_callback_lock);
  1451. }
  1452. static void xs_write_space(struct sock *sk)
  1453. {
  1454. struct socket_wq *wq;
  1455. struct rpc_xprt *xprt;
  1456. if (!sk->sk_socket)
  1457. return;
  1458. clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1459. if (unlikely(!(xprt = xprt_from_sock(sk))))
  1460. return;
  1461. rcu_read_lock();
  1462. wq = rcu_dereference(sk->sk_wq);
  1463. if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
  1464. goto out;
  1465. xprt_write_space(xprt);
  1466. out:
  1467. rcu_read_unlock();
  1468. }
  1469. /**
  1470. * xs_udp_write_space - callback invoked when socket buffer space
  1471. * becomes available
  1472. * @sk: socket whose state has changed
  1473. *
  1474. * Called when more output buffer space is available for this socket.
  1475. * We try not to wake our writers until they can make "significant"
  1476. * progress, otherwise we'll waste resources thrashing kernel_sendmsg
  1477. * with a bunch of small requests.
  1478. */
  1479. static void xs_udp_write_space(struct sock *sk)
  1480. {
  1481. read_lock_bh(&sk->sk_callback_lock);
  1482. /* from net/core/sock.c:sock_def_write_space */
  1483. if (sock_writeable(sk))
  1484. xs_write_space(sk);
  1485. read_unlock_bh(&sk->sk_callback_lock);
  1486. }
  1487. /**
  1488. * xs_tcp_write_space - callback invoked when socket buffer space
  1489. * becomes available
  1490. * @sk: socket whose state has changed
  1491. *
  1492. * Called when more output buffer space is available for this socket.
  1493. * We try not to wake our writers until they can make "significant"
  1494. * progress, otherwise we'll waste resources thrashing kernel_sendmsg
  1495. * with a bunch of small requests.
  1496. */
  1497. static void xs_tcp_write_space(struct sock *sk)
  1498. {
  1499. read_lock_bh(&sk->sk_callback_lock);
  1500. /* from net/core/stream.c:sk_stream_write_space */
  1501. if (sk_stream_is_writeable(sk))
  1502. xs_write_space(sk);
  1503. read_unlock_bh(&sk->sk_callback_lock);
  1504. }
  1505. static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
  1506. {
  1507. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1508. struct sock *sk = transport->inet;
  1509. if (transport->rcvsize) {
  1510. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  1511. sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
  1512. }
  1513. if (transport->sndsize) {
  1514. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  1515. sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
  1516. sk->sk_write_space(sk);
  1517. }
  1518. }
  1519. /**
  1520. * xs_udp_set_buffer_size - set send and receive limits
  1521. * @xprt: generic transport
  1522. * @sndsize: requested size of send buffer, in bytes
  1523. * @rcvsize: requested size of receive buffer, in bytes
  1524. *
  1525. * Set socket send and receive buffer size limits.
  1526. */
  1527. static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
  1528. {
  1529. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1530. transport->sndsize = 0;
  1531. if (sndsize)
  1532. transport->sndsize = sndsize + 1024;
  1533. transport->rcvsize = 0;
  1534. if (rcvsize)
  1535. transport->rcvsize = rcvsize + 1024;
  1536. xs_udp_do_set_buffer_size(xprt);
  1537. }
  1538. /**
  1539. * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
  1540. * @task: task that timed out
  1541. *
  1542. * Adjust the congestion window after a retransmit timeout has occurred.
  1543. */
  1544. static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
  1545. {
  1546. spin_lock_bh(&xprt->transport_lock);
  1547. xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
  1548. spin_unlock_bh(&xprt->transport_lock);
  1549. }
  1550. static int xs_get_random_port(void)
  1551. {
  1552. unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
  1553. unsigned short range;
  1554. unsigned short rand;
  1555. if (max < min)
  1556. return -EADDRINUSE;
  1557. range = max - min + 1;
  1558. rand = (unsigned short) prandom_u32() % range;
  1559. return rand + min;
  1560. }
  1561. /**
  1562. * xs_set_reuseaddr_port - set the socket's port and address reuse options
  1563. * @sock: socket
  1564. *
  1565. * Note that this function has to be called on all sockets that share the
  1566. * same port, and it must be called before binding.
  1567. */
  1568. static void xs_sock_set_reuseport(struct socket *sock)
  1569. {
  1570. int opt = 1;
  1571. kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
  1572. (char *)&opt, sizeof(opt));
  1573. }
  1574. static unsigned short xs_sock_getport(struct socket *sock)
  1575. {
  1576. struct sockaddr_storage buf;
  1577. int buflen;
  1578. unsigned short port = 0;
  1579. if (kernel_getsockname(sock, (struct sockaddr *)&buf, &buflen) < 0)
  1580. goto out;
  1581. switch (buf.ss_family) {
  1582. case AF_INET6:
  1583. port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
  1584. break;
  1585. case AF_INET:
  1586. port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
  1587. }
  1588. out:
  1589. return port;
  1590. }
  1591. /**
  1592. * xs_set_port - reset the port number in the remote endpoint address
  1593. * @xprt: generic transport
  1594. * @port: new port number
  1595. *
  1596. */
  1597. static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
  1598. {
  1599. dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
  1600. rpc_set_port(xs_addr(xprt), port);
  1601. xs_update_peer_port(xprt);
  1602. }
  1603. static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
  1604. {
  1605. if (transport->srcport == 0)
  1606. transport->srcport = xs_sock_getport(sock);
  1607. }
  1608. static int xs_get_srcport(struct sock_xprt *transport)
  1609. {
  1610. int port = transport->srcport;
  1611. if (port == 0 && transport->xprt.resvport)
  1612. port = xs_get_random_port();
  1613. return port;
  1614. }
  1615. static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
  1616. {
  1617. if (transport->srcport != 0)
  1618. transport->srcport = 0;
  1619. if (!transport->xprt.resvport)
  1620. return 0;
  1621. if (port <= xprt_min_resvport || port > xprt_max_resvport)
  1622. return xprt_max_resvport;
  1623. return --port;
  1624. }
  1625. static int xs_bind(struct sock_xprt *transport, struct socket *sock)
  1626. {
  1627. struct sockaddr_storage myaddr;
  1628. int err, nloop = 0;
  1629. int port = xs_get_srcport(transport);
  1630. unsigned short last;
  1631. /*
  1632. * If we are asking for any ephemeral port (i.e. port == 0 &&
  1633. * transport->xprt.resvport == 0), don't bind. Let the local
  1634. * port selection happen implicitly when the socket is used
  1635. * (for example at connect time).
  1636. *
  1637. * This ensures that we can continue to establish TCP
  1638. * connections even when all local ephemeral ports are already
  1639. * a part of some TCP connection. This makes no difference
  1640. * for UDP sockets, but also doens't harm them.
  1641. *
  1642. * If we're asking for any reserved port (i.e. port == 0 &&
  1643. * transport->xprt.resvport == 1) xs_get_srcport above will
  1644. * ensure that port is non-zero and we will bind as needed.
  1645. */
  1646. if (port <= 0)
  1647. return port;
  1648. memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
  1649. do {
  1650. rpc_set_port((struct sockaddr *)&myaddr, port);
  1651. err = kernel_bind(sock, (struct sockaddr *)&myaddr,
  1652. transport->xprt.addrlen);
  1653. if (err == 0) {
  1654. transport->srcport = port;
  1655. break;
  1656. }
  1657. last = port;
  1658. port = xs_next_srcport(transport, port);
  1659. if (port > last)
  1660. nloop++;
  1661. } while (err == -EADDRINUSE && nloop != 2);
  1662. if (myaddr.ss_family == AF_INET)
  1663. dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
  1664. &((struct sockaddr_in *)&myaddr)->sin_addr,
  1665. port, err ? "failed" : "ok", err);
  1666. else
  1667. dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
  1668. &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
  1669. port, err ? "failed" : "ok", err);
  1670. return err;
  1671. }
  1672. /*
  1673. * We don't support autobind on AF_LOCAL sockets
  1674. */
  1675. static void xs_local_rpcbind(struct rpc_task *task)
  1676. {
  1677. xprt_set_bound(task->tk_xprt);
  1678. }
  1679. static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
  1680. {
  1681. }
  1682. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1683. static struct lock_class_key xs_key[2];
  1684. static struct lock_class_key xs_slock_key[2];
  1685. static inline void xs_reclassify_socketu(struct socket *sock)
  1686. {
  1687. struct sock *sk = sock->sk;
  1688. sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
  1689. &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
  1690. }
  1691. static inline void xs_reclassify_socket4(struct socket *sock)
  1692. {
  1693. struct sock *sk = sock->sk;
  1694. sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
  1695. &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
  1696. }
  1697. static inline void xs_reclassify_socket6(struct socket *sock)
  1698. {
  1699. struct sock *sk = sock->sk;
  1700. sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
  1701. &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
  1702. }
  1703. static inline void xs_reclassify_socket(int family, struct socket *sock)
  1704. {
  1705. if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
  1706. return;
  1707. switch (family) {
  1708. case AF_LOCAL:
  1709. xs_reclassify_socketu(sock);
  1710. break;
  1711. case AF_INET:
  1712. xs_reclassify_socket4(sock);
  1713. break;
  1714. case AF_INET6:
  1715. xs_reclassify_socket6(sock);
  1716. break;
  1717. }
  1718. }
  1719. #else
  1720. static inline void xs_reclassify_socket(int family, struct socket *sock)
  1721. {
  1722. }
  1723. #endif
  1724. static void xs_dummy_setup_socket(struct work_struct *work)
  1725. {
  1726. }
  1727. static struct socket *xs_create_sock(struct rpc_xprt *xprt,
  1728. struct sock_xprt *transport, int family, int type,
  1729. int protocol, bool reuseport)
  1730. {
  1731. struct socket *sock;
  1732. int err;
  1733. err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
  1734. if (err < 0) {
  1735. dprintk("RPC: can't create %d transport socket (%d).\n",
  1736. protocol, -err);
  1737. goto out;
  1738. }
  1739. xs_reclassify_socket(family, sock);
  1740. if (reuseport)
  1741. xs_sock_set_reuseport(sock);
  1742. err = xs_bind(transport, sock);
  1743. if (err) {
  1744. sock_release(sock);
  1745. goto out;
  1746. }
  1747. return sock;
  1748. out:
  1749. return ERR_PTR(err);
  1750. }
  1751. static int xs_local_finish_connecting(struct rpc_xprt *xprt,
  1752. struct socket *sock)
  1753. {
  1754. struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
  1755. xprt);
  1756. if (!transport->inet) {
  1757. struct sock *sk = sock->sk;
  1758. write_lock_bh(&sk->sk_callback_lock);
  1759. xs_save_old_callbacks(transport, sk);
  1760. sk->sk_user_data = xprt;
  1761. sk->sk_data_ready = xs_data_ready;
  1762. sk->sk_write_space = xs_udp_write_space;
  1763. sock_set_flag(sk, SOCK_FASYNC);
  1764. sk->sk_error_report = xs_error_report;
  1765. sk->sk_allocation = GFP_NOIO;
  1766. xprt_clear_connected(xprt);
  1767. /* Reset to new socket */
  1768. transport->sock = sock;
  1769. transport->inet = sk;
  1770. write_unlock_bh(&sk->sk_callback_lock);
  1771. }
  1772. /* Tell the socket layer to start connecting... */
  1773. return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
  1774. }
  1775. /**
  1776. * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
  1777. * @transport: socket transport to connect
  1778. */
  1779. static int xs_local_setup_socket(struct sock_xprt *transport)
  1780. {
  1781. struct rpc_xprt *xprt = &transport->xprt;
  1782. struct socket *sock;
  1783. int status = -EIO;
  1784. status = __sock_create(xprt->xprt_net, AF_LOCAL,
  1785. SOCK_STREAM, 0, &sock, 1);
  1786. if (status < 0) {
  1787. dprintk("RPC: can't create AF_LOCAL "
  1788. "transport socket (%d).\n", -status);
  1789. goto out;
  1790. }
  1791. xs_reclassify_socket(AF_LOCAL, sock);
  1792. dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
  1793. xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
  1794. status = xs_local_finish_connecting(xprt, sock);
  1795. trace_rpc_socket_connect(xprt, sock, status);
  1796. switch (status) {
  1797. case 0:
  1798. dprintk("RPC: xprt %p connected to %s\n",
  1799. xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
  1800. xprt->stat.connect_count++;
  1801. xprt->stat.connect_time += (long)jiffies -
  1802. xprt->stat.connect_start;
  1803. xprt_set_connected(xprt);
  1804. case -ENOBUFS:
  1805. break;
  1806. case -ENOENT:
  1807. dprintk("RPC: xprt %p: socket %s does not exist\n",
  1808. xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
  1809. break;
  1810. case -ECONNREFUSED:
  1811. dprintk("RPC: xprt %p: connection refused for %s\n",
  1812. xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
  1813. break;
  1814. default:
  1815. printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
  1816. __func__, -status,
  1817. xprt->address_strings[RPC_DISPLAY_ADDR]);
  1818. }
  1819. out:
  1820. xprt_clear_connecting(xprt);
  1821. xprt_wake_pending_tasks(xprt, status);
  1822. return status;
  1823. }
  1824. static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
  1825. {
  1826. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1827. int ret;
  1828. if (RPC_IS_ASYNC(task)) {
  1829. /*
  1830. * We want the AF_LOCAL connect to be resolved in the
  1831. * filesystem namespace of the process making the rpc
  1832. * call. Thus we connect synchronously.
  1833. *
  1834. * If we want to support asynchronous AF_LOCAL calls,
  1835. * we'll need to figure out how to pass a namespace to
  1836. * connect.
  1837. */
  1838. rpc_exit(task, -ENOTCONN);
  1839. return;
  1840. }
  1841. ret = xs_local_setup_socket(transport);
  1842. if (ret && !RPC_IS_SOFTCONN(task))
  1843. msleep_interruptible(15000);
  1844. }
  1845. #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
  1846. /*
  1847. * Note that this should be called with XPRT_LOCKED held (or when we otherwise
  1848. * know that we have exclusive access to the socket), to guard against
  1849. * races with xs_reset_transport.
  1850. */
  1851. static void xs_set_memalloc(struct rpc_xprt *xprt)
  1852. {
  1853. struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
  1854. xprt);
  1855. /*
  1856. * If there's no sock, then we have nothing to set. The
  1857. * reconnecting process will get it for us.
  1858. */
  1859. if (!transport->inet)
  1860. return;
  1861. if (atomic_read(&xprt->swapper))
  1862. sk_set_memalloc(transport->inet);
  1863. }
  1864. /**
  1865. * xs_enable_swap - Tag this transport as being used for swap.
  1866. * @xprt: transport to tag
  1867. *
  1868. * Take a reference to this transport on behalf of the rpc_clnt, and
  1869. * optionally mark it for swapping if it wasn't already.
  1870. */
  1871. static int
  1872. xs_enable_swap(struct rpc_xprt *xprt)
  1873. {
  1874. struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
  1875. if (atomic_inc_return(&xprt->swapper) != 1)
  1876. return 0;
  1877. if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
  1878. return -ERESTARTSYS;
  1879. if (xs->inet)
  1880. sk_set_memalloc(xs->inet);
  1881. xprt_release_xprt(xprt, NULL);
  1882. return 0;
  1883. }
  1884. /**
  1885. * xs_disable_swap - Untag this transport as being used for swap.
  1886. * @xprt: transport to tag
  1887. *
  1888. * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
  1889. * swapper refcount goes to 0, untag the socket as a memalloc socket.
  1890. */
  1891. static void
  1892. xs_disable_swap(struct rpc_xprt *xprt)
  1893. {
  1894. struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
  1895. if (!atomic_dec_and_test(&xprt->swapper))
  1896. return;
  1897. if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
  1898. return;
  1899. if (xs->inet)
  1900. sk_clear_memalloc(xs->inet);
  1901. xprt_release_xprt(xprt, NULL);
  1902. }
  1903. #else
  1904. static void xs_set_memalloc(struct rpc_xprt *xprt)
  1905. {
  1906. }
  1907. static int
  1908. xs_enable_swap(struct rpc_xprt *xprt)
  1909. {
  1910. return -EINVAL;
  1911. }
  1912. static void
  1913. xs_disable_swap(struct rpc_xprt *xprt)
  1914. {
  1915. }
  1916. #endif
  1917. static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
  1918. {
  1919. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1920. if (!transport->inet) {
  1921. struct sock *sk = sock->sk;
  1922. write_lock_bh(&sk->sk_callback_lock);
  1923. xs_save_old_callbacks(transport, sk);
  1924. sk->sk_user_data = xprt;
  1925. sk->sk_data_ready = xs_data_ready;
  1926. sk->sk_write_space = xs_udp_write_space;
  1927. sock_set_flag(sk, SOCK_FASYNC);
  1928. sk->sk_allocation = GFP_NOIO;
  1929. xprt_set_connected(xprt);
  1930. /* Reset to new socket */
  1931. transport->sock = sock;
  1932. transport->inet = sk;
  1933. xs_set_memalloc(xprt);
  1934. write_unlock_bh(&sk->sk_callback_lock);
  1935. }
  1936. xs_udp_do_set_buffer_size(xprt);
  1937. xprt->stat.connect_start = jiffies;
  1938. }
  1939. static void xs_udp_setup_socket(struct work_struct *work)
  1940. {
  1941. struct sock_xprt *transport =
  1942. container_of(work, struct sock_xprt, connect_worker.work);
  1943. struct rpc_xprt *xprt = &transport->xprt;
  1944. struct socket *sock;
  1945. int status = -EIO;
  1946. sock = xs_create_sock(xprt, transport,
  1947. xs_addr(xprt)->sa_family, SOCK_DGRAM,
  1948. IPPROTO_UDP, false);
  1949. if (IS_ERR(sock))
  1950. goto out;
  1951. dprintk("RPC: worker connecting xprt %p via %s to "
  1952. "%s (port %s)\n", xprt,
  1953. xprt->address_strings[RPC_DISPLAY_PROTO],
  1954. xprt->address_strings[RPC_DISPLAY_ADDR],
  1955. xprt->address_strings[RPC_DISPLAY_PORT]);
  1956. xs_udp_finish_connecting(xprt, sock);
  1957. trace_rpc_socket_connect(xprt, sock, 0);
  1958. status = 0;
  1959. out:
  1960. xprt_clear_connecting(xprt);
  1961. xprt_unlock_connect(xprt, transport);
  1962. xprt_wake_pending_tasks(xprt, status);
  1963. }
  1964. /**
  1965. * xs_tcp_shutdown - gracefully shut down a TCP socket
  1966. * @xprt: transport
  1967. *
  1968. * Initiates a graceful shutdown of the TCP socket by calling the
  1969. * equivalent of shutdown(SHUT_RDWR);
  1970. */
  1971. static void xs_tcp_shutdown(struct rpc_xprt *xprt)
  1972. {
  1973. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1974. struct socket *sock = transport->sock;
  1975. if (sock == NULL)
  1976. return;
  1977. if (xprt_connected(xprt)) {
  1978. kernel_sock_shutdown(sock, SHUT_RDWR);
  1979. trace_rpc_socket_shutdown(xprt, sock);
  1980. } else
  1981. xs_reset_transport(transport);
  1982. }
  1983. static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
  1984. struct socket *sock)
  1985. {
  1986. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1987. unsigned int keepidle;
  1988. unsigned int keepcnt;
  1989. unsigned int opt_on = 1;
  1990. unsigned int timeo;
  1991. spin_lock_bh(&xprt->transport_lock);
  1992. keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
  1993. keepcnt = xprt->timeout->to_retries + 1;
  1994. timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
  1995. (xprt->timeout->to_retries + 1);
  1996. clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
  1997. spin_unlock_bh(&xprt->transport_lock);
  1998. /* TCP Keepalive options */
  1999. kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  2000. (char *)&opt_on, sizeof(opt_on));
  2001. kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
  2002. (char *)&keepidle, sizeof(keepidle));
  2003. kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
  2004. (char *)&keepidle, sizeof(keepidle));
  2005. kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
  2006. (char *)&keepcnt, sizeof(keepcnt));
  2007. /* TCP user timeout (see RFC5482) */
  2008. kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
  2009. (char *)&timeo, sizeof(timeo));
  2010. }
  2011. static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
  2012. unsigned long connect_timeout,
  2013. unsigned long reconnect_timeout)
  2014. {
  2015. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  2016. struct rpc_timeout to;
  2017. unsigned long initval;
  2018. spin_lock_bh(&xprt->transport_lock);
  2019. if (reconnect_timeout < xprt->max_reconnect_timeout)
  2020. xprt->max_reconnect_timeout = reconnect_timeout;
  2021. if (connect_timeout < xprt->connect_timeout) {
  2022. memcpy(&to, xprt->timeout, sizeof(to));
  2023. initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
  2024. /* Arbitrary lower limit */
  2025. if (initval < XS_TCP_INIT_REEST_TO << 1)
  2026. initval = XS_TCP_INIT_REEST_TO << 1;
  2027. to.to_initval = initval;
  2028. to.to_maxval = initval;
  2029. memcpy(&transport->tcp_timeout, &to,
  2030. sizeof(transport->tcp_timeout));
  2031. xprt->timeout = &transport->tcp_timeout;
  2032. xprt->connect_timeout = connect_timeout;
  2033. }
  2034. set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
  2035. spin_unlock_bh(&xprt->transport_lock);
  2036. }
  2037. static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
  2038. {
  2039. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  2040. int ret = -ENOTCONN;
  2041. if (!transport->inet) {
  2042. struct sock *sk = sock->sk;
  2043. unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
  2044. /* Avoid temporary address, they are bad for long-lived
  2045. * connections such as NFS mounts.
  2046. * RFC4941, section 3.6 suggests that:
  2047. * Individual applications, which have specific
  2048. * knowledge about the normal duration of connections,
  2049. * MAY override this as appropriate.
  2050. */
  2051. kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
  2052. (char *)&addr_pref, sizeof(addr_pref));
  2053. xs_tcp_set_socket_timeouts(xprt, sock);
  2054. write_lock_bh(&sk->sk_callback_lock);
  2055. xs_save_old_callbacks(transport, sk);
  2056. sk->sk_user_data = xprt;
  2057. sk->sk_data_ready = xs_data_ready;
  2058. sk->sk_state_change = xs_tcp_state_change;
  2059. sk->sk_write_space = xs_tcp_write_space;
  2060. sock_set_flag(sk, SOCK_FASYNC);
  2061. sk->sk_error_report = xs_error_report;
  2062. sk->sk_allocation = GFP_NOIO;
  2063. /* socket options */
  2064. sock_reset_flag(sk, SOCK_LINGER);
  2065. tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
  2066. xprt_clear_connected(xprt);
  2067. /* Reset to new socket */
  2068. transport->sock = sock;
  2069. transport->inet = sk;
  2070. write_unlock_bh(&sk->sk_callback_lock);
  2071. }
  2072. if (!xprt_bound(xprt))
  2073. goto out;
  2074. xs_set_memalloc(xprt);
  2075. /* Tell the socket layer to start connecting... */
  2076. set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
  2077. ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
  2078. switch (ret) {
  2079. case 0:
  2080. xs_set_srcport(transport, sock);
  2081. case -EINPROGRESS:
  2082. /* SYN_SENT! */
  2083. if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
  2084. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  2085. break;
  2086. case -EADDRNOTAVAIL:
  2087. /* Source port number is unavailable. Try a new one! */
  2088. transport->srcport = 0;
  2089. }
  2090. out:
  2091. return ret;
  2092. }
  2093. /**
  2094. * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
  2095. *
  2096. * Invoked by a work queue tasklet.
  2097. */
  2098. static void xs_tcp_setup_socket(struct work_struct *work)
  2099. {
  2100. struct sock_xprt *transport =
  2101. container_of(work, struct sock_xprt, connect_worker.work);
  2102. struct socket *sock = transport->sock;
  2103. struct rpc_xprt *xprt = &transport->xprt;
  2104. int status = -EIO;
  2105. if (!sock) {
  2106. sock = xs_create_sock(xprt, transport,
  2107. xs_addr(xprt)->sa_family, SOCK_STREAM,
  2108. IPPROTO_TCP, true);
  2109. if (IS_ERR(sock)) {
  2110. status = PTR_ERR(sock);
  2111. goto out;
  2112. }
  2113. }
  2114. dprintk("RPC: worker connecting xprt %p via %s to "
  2115. "%s (port %s)\n", xprt,
  2116. xprt->address_strings[RPC_DISPLAY_PROTO],
  2117. xprt->address_strings[RPC_DISPLAY_ADDR],
  2118. xprt->address_strings[RPC_DISPLAY_PORT]);
  2119. status = xs_tcp_finish_connecting(xprt, sock);
  2120. trace_rpc_socket_connect(xprt, sock, status);
  2121. dprintk("RPC: %p connect status %d connected %d sock state %d\n",
  2122. xprt, -status, xprt_connected(xprt),
  2123. sock->sk->sk_state);
  2124. switch (status) {
  2125. default:
  2126. printk("%s: connect returned unhandled error %d\n",
  2127. __func__, status);
  2128. case -EADDRNOTAVAIL:
  2129. /* We're probably in TIME_WAIT. Get rid of existing socket,
  2130. * and retry
  2131. */
  2132. xs_tcp_force_close(xprt);
  2133. break;
  2134. case 0:
  2135. case -EINPROGRESS:
  2136. case -EALREADY:
  2137. xprt_unlock_connect(xprt, transport);
  2138. return;
  2139. case -EINVAL:
  2140. /* Happens, for instance, if the user specified a link
  2141. * local IPv6 address without a scope-id.
  2142. */
  2143. case -ECONNREFUSED:
  2144. case -ECONNRESET:
  2145. case -ENETUNREACH:
  2146. case -EHOSTUNREACH:
  2147. case -EADDRINUSE:
  2148. case -ENOBUFS:
  2149. /*
  2150. * xs_tcp_force_close() wakes tasks with -EIO.
  2151. * We need to wake them first to ensure the
  2152. * correct error code.
  2153. */
  2154. xprt_wake_pending_tasks(xprt, status);
  2155. xs_tcp_force_close(xprt);
  2156. goto out;
  2157. }
  2158. status = -EAGAIN;
  2159. out:
  2160. xprt_clear_connecting(xprt);
  2161. xprt_unlock_connect(xprt, transport);
  2162. xprt_wake_pending_tasks(xprt, status);
  2163. }
  2164. static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
  2165. {
  2166. unsigned long start, now = jiffies;
  2167. start = xprt->stat.connect_start + xprt->reestablish_timeout;
  2168. if (time_after(start, now))
  2169. return start - now;
  2170. return 0;
  2171. }
  2172. static void xs_reconnect_backoff(struct rpc_xprt *xprt)
  2173. {
  2174. xprt->reestablish_timeout <<= 1;
  2175. if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
  2176. xprt->reestablish_timeout = xprt->max_reconnect_timeout;
  2177. if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
  2178. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  2179. }
  2180. /**
  2181. * xs_connect - connect a socket to a remote endpoint
  2182. * @xprt: pointer to transport structure
  2183. * @task: address of RPC task that manages state of connect request
  2184. *
  2185. * TCP: If the remote end dropped the connection, delay reconnecting.
  2186. *
  2187. * UDP socket connects are synchronous, but we use a work queue anyway
  2188. * to guarantee that even unprivileged user processes can set up a
  2189. * socket on a privileged port.
  2190. *
  2191. * If a UDP socket connect fails, the delay behavior here prevents
  2192. * retry floods (hard mounts).
  2193. */
  2194. static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
  2195. {
  2196. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  2197. unsigned long delay = 0;
  2198. WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
  2199. if (transport->sock != NULL) {
  2200. dprintk("RPC: xs_connect delayed xprt %p for %lu "
  2201. "seconds\n",
  2202. xprt, xprt->reestablish_timeout / HZ);
  2203. /* Start by resetting any existing state */
  2204. xs_reset_transport(transport);
  2205. delay = xs_reconnect_delay(xprt);
  2206. xs_reconnect_backoff(xprt);
  2207. } else
  2208. dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
  2209. queue_delayed_work(xprtiod_workqueue,
  2210. &transport->connect_worker,
  2211. delay);
  2212. }
  2213. /**
  2214. * xs_local_print_stats - display AF_LOCAL socket-specifc stats
  2215. * @xprt: rpc_xprt struct containing statistics
  2216. * @seq: output file
  2217. *
  2218. */
  2219. static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  2220. {
  2221. long idle_time = 0;
  2222. if (xprt_connected(xprt))
  2223. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  2224. seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
  2225. "%llu %llu %lu %llu %llu\n",
  2226. xprt->stat.bind_count,
  2227. xprt->stat.connect_count,
  2228. xprt->stat.connect_time,
  2229. idle_time,
  2230. xprt->stat.sends,
  2231. xprt->stat.recvs,
  2232. xprt->stat.bad_xids,
  2233. xprt->stat.req_u,
  2234. xprt->stat.bklog_u,
  2235. xprt->stat.max_slots,
  2236. xprt->stat.sending_u,
  2237. xprt->stat.pending_u);
  2238. }
  2239. /**
  2240. * xs_udp_print_stats - display UDP socket-specifc stats
  2241. * @xprt: rpc_xprt struct containing statistics
  2242. * @seq: output file
  2243. *
  2244. */
  2245. static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  2246. {
  2247. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  2248. seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
  2249. "%lu %llu %llu\n",
  2250. transport->srcport,
  2251. xprt->stat.bind_count,
  2252. xprt->stat.sends,
  2253. xprt->stat.recvs,
  2254. xprt->stat.bad_xids,
  2255. xprt->stat.req_u,
  2256. xprt->stat.bklog_u,
  2257. xprt->stat.max_slots,
  2258. xprt->stat.sending_u,
  2259. xprt->stat.pending_u);
  2260. }
  2261. /**
  2262. * xs_tcp_print_stats - display TCP socket-specifc stats
  2263. * @xprt: rpc_xprt struct containing statistics
  2264. * @seq: output file
  2265. *
  2266. */
  2267. static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  2268. {
  2269. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  2270. long idle_time = 0;
  2271. if (xprt_connected(xprt))
  2272. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  2273. seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
  2274. "%llu %llu %lu %llu %llu\n",
  2275. transport->srcport,
  2276. xprt->stat.bind_count,
  2277. xprt->stat.connect_count,
  2278. xprt->stat.connect_time,
  2279. idle_time,
  2280. xprt->stat.sends,
  2281. xprt->stat.recvs,
  2282. xprt->stat.bad_xids,
  2283. xprt->stat.req_u,
  2284. xprt->stat.bklog_u,
  2285. xprt->stat.max_slots,
  2286. xprt->stat.sending_u,
  2287. xprt->stat.pending_u);
  2288. }
  2289. /*
  2290. * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
  2291. * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
  2292. * to use the server side send routines.
  2293. */
  2294. static int bc_malloc(struct rpc_task *task)
  2295. {
  2296. struct rpc_rqst *rqst = task->tk_rqstp;
  2297. size_t size = rqst->rq_callsize;
  2298. struct page *page;
  2299. struct rpc_buffer *buf;
  2300. if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
  2301. WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
  2302. size);
  2303. return -EINVAL;
  2304. }
  2305. page = alloc_page(GFP_KERNEL);
  2306. if (!page)
  2307. return -ENOMEM;
  2308. buf = page_address(page);
  2309. buf->len = PAGE_SIZE;
  2310. rqst->rq_buffer = buf->data;
  2311. rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
  2312. return 0;
  2313. }
  2314. /*
  2315. * Free the space allocated in the bc_alloc routine
  2316. */
  2317. static void bc_free(struct rpc_task *task)
  2318. {
  2319. void *buffer = task->tk_rqstp->rq_buffer;
  2320. struct rpc_buffer *buf;
  2321. buf = container_of(buffer, struct rpc_buffer, data);
  2322. free_page((unsigned long)buf);
  2323. }
  2324. /*
  2325. * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
  2326. * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
  2327. */
  2328. static int bc_sendto(struct rpc_rqst *req)
  2329. {
  2330. int len;
  2331. struct xdr_buf *xbufp = &req->rq_snd_buf;
  2332. struct rpc_xprt *xprt = req->rq_xprt;
  2333. struct sock_xprt *transport =
  2334. container_of(xprt, struct sock_xprt, xprt);
  2335. struct socket *sock = transport->sock;
  2336. unsigned long headoff;
  2337. unsigned long tailoff;
  2338. xs_encode_stream_record_marker(xbufp);
  2339. tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
  2340. headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
  2341. len = svc_send_common(sock, xbufp,
  2342. virt_to_page(xbufp->head[0].iov_base), headoff,
  2343. xbufp->tail[0].iov_base, tailoff);
  2344. if (len != xbufp->len) {
  2345. printk(KERN_NOTICE "Error sending entire callback!\n");
  2346. len = -EAGAIN;
  2347. }
  2348. return len;
  2349. }
  2350. /*
  2351. * The send routine. Borrows from svc_send
  2352. */
  2353. static int bc_send_request(struct rpc_task *task)
  2354. {
  2355. struct rpc_rqst *req = task->tk_rqstp;
  2356. struct svc_xprt *xprt;
  2357. int len;
  2358. dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
  2359. /*
  2360. * Get the server socket associated with this callback xprt
  2361. */
  2362. xprt = req->rq_xprt->bc_xprt;
  2363. /*
  2364. * Grab the mutex to serialize data as the connection is shared
  2365. * with the fore channel
  2366. */
  2367. if (!mutex_trylock(&xprt->xpt_mutex)) {
  2368. rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
  2369. if (!mutex_trylock(&xprt->xpt_mutex))
  2370. return -EAGAIN;
  2371. rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
  2372. }
  2373. if (test_bit(XPT_DEAD, &xprt->xpt_flags))
  2374. len = -ENOTCONN;
  2375. else
  2376. len = bc_sendto(req);
  2377. mutex_unlock(&xprt->xpt_mutex);
  2378. if (len > 0)
  2379. len = 0;
  2380. return len;
  2381. }
  2382. /*
  2383. * The close routine. Since this is client initiated, we do nothing
  2384. */
  2385. static void bc_close(struct rpc_xprt *xprt)
  2386. {
  2387. }
  2388. /*
  2389. * The xprt destroy routine. Again, because this connection is client
  2390. * initiated, we do nothing
  2391. */
  2392. static void bc_destroy(struct rpc_xprt *xprt)
  2393. {
  2394. dprintk("RPC: bc_destroy xprt %p\n", xprt);
  2395. xs_xprt_free(xprt);
  2396. module_put(THIS_MODULE);
  2397. }
  2398. static const struct rpc_xprt_ops xs_local_ops = {
  2399. .reserve_xprt = xprt_reserve_xprt,
  2400. .release_xprt = xs_tcp_release_xprt,
  2401. .alloc_slot = xprt_alloc_slot,
  2402. .rpcbind = xs_local_rpcbind,
  2403. .set_port = xs_local_set_port,
  2404. .connect = xs_local_connect,
  2405. .buf_alloc = rpc_malloc,
  2406. .buf_free = rpc_free,
  2407. .send_request = xs_local_send_request,
  2408. .set_retrans_timeout = xprt_set_retrans_timeout_def,
  2409. .close = xs_close,
  2410. .destroy = xs_destroy,
  2411. .print_stats = xs_local_print_stats,
  2412. .enable_swap = xs_enable_swap,
  2413. .disable_swap = xs_disable_swap,
  2414. };
  2415. static const struct rpc_xprt_ops xs_udp_ops = {
  2416. .set_buffer_size = xs_udp_set_buffer_size,
  2417. .reserve_xprt = xprt_reserve_xprt_cong,
  2418. .release_xprt = xprt_release_xprt_cong,
  2419. .alloc_slot = xprt_alloc_slot,
  2420. .rpcbind = rpcb_getport_async,
  2421. .set_port = xs_set_port,
  2422. .connect = xs_connect,
  2423. .buf_alloc = rpc_malloc,
  2424. .buf_free = rpc_free,
  2425. .send_request = xs_udp_send_request,
  2426. .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
  2427. .timer = xs_udp_timer,
  2428. .release_request = xprt_release_rqst_cong,
  2429. .close = xs_close,
  2430. .destroy = xs_destroy,
  2431. .print_stats = xs_udp_print_stats,
  2432. .enable_swap = xs_enable_swap,
  2433. .disable_swap = xs_disable_swap,
  2434. .inject_disconnect = xs_inject_disconnect,
  2435. };
  2436. static const struct rpc_xprt_ops xs_tcp_ops = {
  2437. .reserve_xprt = xprt_reserve_xprt,
  2438. .release_xprt = xs_tcp_release_xprt,
  2439. .alloc_slot = xprt_lock_and_alloc_slot,
  2440. .rpcbind = rpcb_getport_async,
  2441. .set_port = xs_set_port,
  2442. .connect = xs_connect,
  2443. .buf_alloc = rpc_malloc,
  2444. .buf_free = rpc_free,
  2445. .send_request = xs_tcp_send_request,
  2446. .set_retrans_timeout = xprt_set_retrans_timeout_def,
  2447. .close = xs_tcp_shutdown,
  2448. .destroy = xs_destroy,
  2449. .set_connect_timeout = xs_tcp_set_connect_timeout,
  2450. .print_stats = xs_tcp_print_stats,
  2451. .enable_swap = xs_enable_swap,
  2452. .disable_swap = xs_disable_swap,
  2453. .inject_disconnect = xs_inject_disconnect,
  2454. #ifdef CONFIG_SUNRPC_BACKCHANNEL
  2455. .bc_setup = xprt_setup_bc,
  2456. .bc_up = xs_tcp_bc_up,
  2457. .bc_maxpayload = xs_tcp_bc_maxpayload,
  2458. .bc_free_rqst = xprt_free_bc_rqst,
  2459. .bc_destroy = xprt_destroy_bc,
  2460. #endif
  2461. };
  2462. /*
  2463. * The rpc_xprt_ops for the server backchannel
  2464. */
  2465. static const struct rpc_xprt_ops bc_tcp_ops = {
  2466. .reserve_xprt = xprt_reserve_xprt,
  2467. .release_xprt = xprt_release_xprt,
  2468. .alloc_slot = xprt_alloc_slot,
  2469. .buf_alloc = bc_malloc,
  2470. .buf_free = bc_free,
  2471. .send_request = bc_send_request,
  2472. .set_retrans_timeout = xprt_set_retrans_timeout_def,
  2473. .close = bc_close,
  2474. .destroy = bc_destroy,
  2475. .print_stats = xs_tcp_print_stats,
  2476. .enable_swap = xs_enable_swap,
  2477. .disable_swap = xs_disable_swap,
  2478. .inject_disconnect = xs_inject_disconnect,
  2479. };
  2480. static int xs_init_anyaddr(const int family, struct sockaddr *sap)
  2481. {
  2482. static const struct sockaddr_in sin = {
  2483. .sin_family = AF_INET,
  2484. .sin_addr.s_addr = htonl(INADDR_ANY),
  2485. };
  2486. static const struct sockaddr_in6 sin6 = {
  2487. .sin6_family = AF_INET6,
  2488. .sin6_addr = IN6ADDR_ANY_INIT,
  2489. };
  2490. switch (family) {
  2491. case AF_LOCAL:
  2492. break;
  2493. case AF_INET:
  2494. memcpy(sap, &sin, sizeof(sin));
  2495. break;
  2496. case AF_INET6:
  2497. memcpy(sap, &sin6, sizeof(sin6));
  2498. break;
  2499. default:
  2500. dprintk("RPC: %s: Bad address family\n", __func__);
  2501. return -EAFNOSUPPORT;
  2502. }
  2503. return 0;
  2504. }
  2505. static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
  2506. unsigned int slot_table_size,
  2507. unsigned int max_slot_table_size)
  2508. {
  2509. struct rpc_xprt *xprt;
  2510. struct sock_xprt *new;
  2511. if (args->addrlen > sizeof(xprt->addr)) {
  2512. dprintk("RPC: xs_setup_xprt: address too large\n");
  2513. return ERR_PTR(-EBADF);
  2514. }
  2515. xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
  2516. max_slot_table_size);
  2517. if (xprt == NULL) {
  2518. dprintk("RPC: xs_setup_xprt: couldn't allocate "
  2519. "rpc_xprt\n");
  2520. return ERR_PTR(-ENOMEM);
  2521. }
  2522. new = container_of(xprt, struct sock_xprt, xprt);
  2523. mutex_init(&new->recv_mutex);
  2524. memcpy(&xprt->addr, args->dstaddr, args->addrlen);
  2525. xprt->addrlen = args->addrlen;
  2526. if (args->srcaddr)
  2527. memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
  2528. else {
  2529. int err;
  2530. err = xs_init_anyaddr(args->dstaddr->sa_family,
  2531. (struct sockaddr *)&new->srcaddr);
  2532. if (err != 0) {
  2533. xprt_free(xprt);
  2534. return ERR_PTR(err);
  2535. }
  2536. }
  2537. return xprt;
  2538. }
  2539. static const struct rpc_timeout xs_local_default_timeout = {
  2540. .to_initval = 10 * HZ,
  2541. .to_maxval = 10 * HZ,
  2542. .to_retries = 2,
  2543. };
  2544. /**
  2545. * xs_setup_local - Set up transport to use an AF_LOCAL socket
  2546. * @args: rpc transport creation arguments
  2547. *
  2548. * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
  2549. */
  2550. static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
  2551. {
  2552. struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
  2553. struct sock_xprt *transport;
  2554. struct rpc_xprt *xprt;
  2555. struct rpc_xprt *ret;
  2556. xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
  2557. xprt_max_tcp_slot_table_entries);
  2558. if (IS_ERR(xprt))
  2559. return xprt;
  2560. transport = container_of(xprt, struct sock_xprt, xprt);
  2561. xprt->prot = 0;
  2562. xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
  2563. xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
  2564. xprt->bind_timeout = XS_BIND_TO;
  2565. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  2566. xprt->idle_timeout = XS_IDLE_DISC_TO;
  2567. xprt->ops = &xs_local_ops;
  2568. xprt->timeout = &xs_local_default_timeout;
  2569. INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn);
  2570. INIT_DELAYED_WORK(&transport->connect_worker,
  2571. xs_dummy_setup_socket);
  2572. switch (sun->sun_family) {
  2573. case AF_LOCAL:
  2574. if (sun->sun_path[0] != '/') {
  2575. dprintk("RPC: bad AF_LOCAL address: %s\n",
  2576. sun->sun_path);
  2577. ret = ERR_PTR(-EINVAL);
  2578. goto out_err;
  2579. }
  2580. xprt_set_bound(xprt);
  2581. xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
  2582. ret = ERR_PTR(xs_local_setup_socket(transport));
  2583. if (ret)
  2584. goto out_err;
  2585. break;
  2586. default:
  2587. ret = ERR_PTR(-EAFNOSUPPORT);
  2588. goto out_err;
  2589. }
  2590. dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
  2591. xprt->address_strings[RPC_DISPLAY_ADDR]);
  2592. if (try_module_get(THIS_MODULE))
  2593. return xprt;
  2594. ret = ERR_PTR(-EINVAL);
  2595. out_err:
  2596. xs_xprt_free(xprt);
  2597. return ret;
  2598. }
  2599. static const struct rpc_timeout xs_udp_default_timeout = {
  2600. .to_initval = 5 * HZ,
  2601. .to_maxval = 30 * HZ,
  2602. .to_increment = 5 * HZ,
  2603. .to_retries = 5,
  2604. };
  2605. /**
  2606. * xs_setup_udp - Set up transport to use a UDP socket
  2607. * @args: rpc transport creation arguments
  2608. *
  2609. */
  2610. static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
  2611. {
  2612. struct sockaddr *addr = args->dstaddr;
  2613. struct rpc_xprt *xprt;
  2614. struct sock_xprt *transport;
  2615. struct rpc_xprt *ret;
  2616. xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
  2617. xprt_udp_slot_table_entries);
  2618. if (IS_ERR(xprt))
  2619. return xprt;
  2620. transport = container_of(xprt, struct sock_xprt, xprt);
  2621. xprt->prot = IPPROTO_UDP;
  2622. xprt->tsh_size = 0;
  2623. /* XXX: header size can vary due to auth type, IPv6, etc. */
  2624. xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
  2625. xprt->bind_timeout = XS_BIND_TO;
  2626. xprt->reestablish_timeout = XS_UDP_REEST_TO;
  2627. xprt->idle_timeout = XS_IDLE_DISC_TO;
  2628. xprt->ops = &xs_udp_ops;
  2629. xprt->timeout = &xs_udp_default_timeout;
  2630. INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
  2631. INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
  2632. switch (addr->sa_family) {
  2633. case AF_INET:
  2634. if (((struct sockaddr_in *)addr)->sin_port != htons(0))
  2635. xprt_set_bound(xprt);
  2636. xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
  2637. break;
  2638. case AF_INET6:
  2639. if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
  2640. xprt_set_bound(xprt);
  2641. xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
  2642. break;
  2643. default:
  2644. ret = ERR_PTR(-EAFNOSUPPORT);
  2645. goto out_err;
  2646. }
  2647. if (xprt_bound(xprt))
  2648. dprintk("RPC: set up xprt to %s (port %s) via %s\n",
  2649. xprt->address_strings[RPC_DISPLAY_ADDR],
  2650. xprt->address_strings[RPC_DISPLAY_PORT],
  2651. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2652. else
  2653. dprintk("RPC: set up xprt to %s (autobind) via %s\n",
  2654. xprt->address_strings[RPC_DISPLAY_ADDR],
  2655. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2656. if (try_module_get(THIS_MODULE))
  2657. return xprt;
  2658. ret = ERR_PTR(-EINVAL);
  2659. out_err:
  2660. xs_xprt_free(xprt);
  2661. return ret;
  2662. }
  2663. static const struct rpc_timeout xs_tcp_default_timeout = {
  2664. .to_initval = 60 * HZ,
  2665. .to_maxval = 60 * HZ,
  2666. .to_retries = 2,
  2667. };
  2668. /**
  2669. * xs_setup_tcp - Set up transport to use a TCP socket
  2670. * @args: rpc transport creation arguments
  2671. *
  2672. */
  2673. static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
  2674. {
  2675. struct sockaddr *addr = args->dstaddr;
  2676. struct rpc_xprt *xprt;
  2677. struct sock_xprt *transport;
  2678. struct rpc_xprt *ret;
  2679. unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
  2680. if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
  2681. max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
  2682. xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
  2683. max_slot_table_size);
  2684. if (IS_ERR(xprt))
  2685. return xprt;
  2686. transport = container_of(xprt, struct sock_xprt, xprt);
  2687. xprt->prot = IPPROTO_TCP;
  2688. xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
  2689. xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
  2690. xprt->bind_timeout = XS_BIND_TO;
  2691. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  2692. xprt->idle_timeout = XS_IDLE_DISC_TO;
  2693. xprt->ops = &xs_tcp_ops;
  2694. xprt->timeout = &xs_tcp_default_timeout;
  2695. xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
  2696. xprt->connect_timeout = xprt->timeout->to_initval *
  2697. (xprt->timeout->to_retries + 1);
  2698. INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn);
  2699. INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
  2700. switch (addr->sa_family) {
  2701. case AF_INET:
  2702. if (((struct sockaddr_in *)addr)->sin_port != htons(0))
  2703. xprt_set_bound(xprt);
  2704. xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
  2705. break;
  2706. case AF_INET6:
  2707. if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
  2708. xprt_set_bound(xprt);
  2709. xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
  2710. break;
  2711. default:
  2712. ret = ERR_PTR(-EAFNOSUPPORT);
  2713. goto out_err;
  2714. }
  2715. if (xprt_bound(xprt))
  2716. dprintk("RPC: set up xprt to %s (port %s) via %s\n",
  2717. xprt->address_strings[RPC_DISPLAY_ADDR],
  2718. xprt->address_strings[RPC_DISPLAY_PORT],
  2719. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2720. else
  2721. dprintk("RPC: set up xprt to %s (autobind) via %s\n",
  2722. xprt->address_strings[RPC_DISPLAY_ADDR],
  2723. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2724. if (try_module_get(THIS_MODULE))
  2725. return xprt;
  2726. ret = ERR_PTR(-EINVAL);
  2727. out_err:
  2728. xs_xprt_free(xprt);
  2729. return ret;
  2730. }
  2731. /**
  2732. * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
  2733. * @args: rpc transport creation arguments
  2734. *
  2735. */
  2736. static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
  2737. {
  2738. struct sockaddr *addr = args->dstaddr;
  2739. struct rpc_xprt *xprt;
  2740. struct sock_xprt *transport;
  2741. struct svc_sock *bc_sock;
  2742. struct rpc_xprt *ret;
  2743. xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
  2744. xprt_tcp_slot_table_entries);
  2745. if (IS_ERR(xprt))
  2746. return xprt;
  2747. transport = container_of(xprt, struct sock_xprt, xprt);
  2748. xprt->prot = IPPROTO_TCP;
  2749. xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
  2750. xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
  2751. xprt->timeout = &xs_tcp_default_timeout;
  2752. /* backchannel */
  2753. xprt_set_bound(xprt);
  2754. xprt->bind_timeout = 0;
  2755. xprt->reestablish_timeout = 0;
  2756. xprt->idle_timeout = 0;
  2757. xprt->ops = &bc_tcp_ops;
  2758. switch (addr->sa_family) {
  2759. case AF_INET:
  2760. xs_format_peer_addresses(xprt, "tcp",
  2761. RPCBIND_NETID_TCP);
  2762. break;
  2763. case AF_INET6:
  2764. xs_format_peer_addresses(xprt, "tcp",
  2765. RPCBIND_NETID_TCP6);
  2766. break;
  2767. default:
  2768. ret = ERR_PTR(-EAFNOSUPPORT);
  2769. goto out_err;
  2770. }
  2771. dprintk("RPC: set up xprt to %s (port %s) via %s\n",
  2772. xprt->address_strings[RPC_DISPLAY_ADDR],
  2773. xprt->address_strings[RPC_DISPLAY_PORT],
  2774. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2775. /*
  2776. * Once we've associated a backchannel xprt with a connection,
  2777. * we want to keep it around as long as the connection lasts,
  2778. * in case we need to start using it for a backchannel again;
  2779. * this reference won't be dropped until bc_xprt is destroyed.
  2780. */
  2781. xprt_get(xprt);
  2782. args->bc_xprt->xpt_bc_xprt = xprt;
  2783. xprt->bc_xprt = args->bc_xprt;
  2784. bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
  2785. transport->sock = bc_sock->sk_sock;
  2786. transport->inet = bc_sock->sk_sk;
  2787. /*
  2788. * Since we don't want connections for the backchannel, we set
  2789. * the xprt status to connected
  2790. */
  2791. xprt_set_connected(xprt);
  2792. if (try_module_get(THIS_MODULE))
  2793. return xprt;
  2794. args->bc_xprt->xpt_bc_xprt = NULL;
  2795. args->bc_xprt->xpt_bc_xps = NULL;
  2796. xprt_put(xprt);
  2797. ret = ERR_PTR(-EINVAL);
  2798. out_err:
  2799. xs_xprt_free(xprt);
  2800. return ret;
  2801. }
  2802. static struct xprt_class xs_local_transport = {
  2803. .list = LIST_HEAD_INIT(xs_local_transport.list),
  2804. .name = "named UNIX socket",
  2805. .owner = THIS_MODULE,
  2806. .ident = XPRT_TRANSPORT_LOCAL,
  2807. .setup = xs_setup_local,
  2808. .netid = { "" },
  2809. };
  2810. static struct xprt_class xs_udp_transport = {
  2811. .list = LIST_HEAD_INIT(xs_udp_transport.list),
  2812. .name = "udp",
  2813. .owner = THIS_MODULE,
  2814. .ident = XPRT_TRANSPORT_UDP,
  2815. .setup = xs_setup_udp,
  2816. .netid = { "udp", "udp6", "" },
  2817. };
  2818. static struct xprt_class xs_tcp_transport = {
  2819. .list = LIST_HEAD_INIT(xs_tcp_transport.list),
  2820. .name = "tcp",
  2821. .owner = THIS_MODULE,
  2822. .ident = XPRT_TRANSPORT_TCP,
  2823. .setup = xs_setup_tcp,
  2824. .netid = { "tcp", "tcp6", "" },
  2825. };
  2826. static struct xprt_class xs_bc_tcp_transport = {
  2827. .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
  2828. .name = "tcp NFSv4.1 backchannel",
  2829. .owner = THIS_MODULE,
  2830. .ident = XPRT_TRANSPORT_BC_TCP,
  2831. .setup = xs_setup_bc_tcp,
  2832. .netid = { "" },
  2833. };
  2834. /**
  2835. * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
  2836. *
  2837. */
  2838. int init_socket_xprt(void)
  2839. {
  2840. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  2841. if (!sunrpc_table_header)
  2842. sunrpc_table_header = register_sysctl_table(sunrpc_table);
  2843. #endif
  2844. xprt_register_transport(&xs_local_transport);
  2845. xprt_register_transport(&xs_udp_transport);
  2846. xprt_register_transport(&xs_tcp_transport);
  2847. xprt_register_transport(&xs_bc_tcp_transport);
  2848. return 0;
  2849. }
  2850. /**
  2851. * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
  2852. *
  2853. */
  2854. void cleanup_socket_xprt(void)
  2855. {
  2856. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  2857. if (sunrpc_table_header) {
  2858. unregister_sysctl_table(sunrpc_table_header);
  2859. sunrpc_table_header = NULL;
  2860. }
  2861. #endif
  2862. xprt_unregister_transport(&xs_local_transport);
  2863. xprt_unregister_transport(&xs_udp_transport);
  2864. xprt_unregister_transport(&xs_tcp_transport);
  2865. xprt_unregister_transport(&xs_bc_tcp_transport);
  2866. }
  2867. static int param_set_uint_minmax(const char *val,
  2868. const struct kernel_param *kp,
  2869. unsigned int min, unsigned int max)
  2870. {
  2871. unsigned int num;
  2872. int ret;
  2873. if (!val)
  2874. return -EINVAL;
  2875. ret = kstrtouint(val, 0, &num);
  2876. if (ret)
  2877. return ret;
  2878. if (num < min || num > max)
  2879. return -EINVAL;
  2880. *((unsigned int *)kp->arg) = num;
  2881. return 0;
  2882. }
  2883. static int param_set_portnr(const char *val, const struct kernel_param *kp)
  2884. {
  2885. return param_set_uint_minmax(val, kp,
  2886. RPC_MIN_RESVPORT,
  2887. RPC_MAX_RESVPORT);
  2888. }
  2889. static const struct kernel_param_ops param_ops_portnr = {
  2890. .set = param_set_portnr,
  2891. .get = param_get_uint,
  2892. };
  2893. #define param_check_portnr(name, p) \
  2894. __param_check(name, p, unsigned int);
  2895. module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
  2896. module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
  2897. static int param_set_slot_table_size(const char *val,
  2898. const struct kernel_param *kp)
  2899. {
  2900. return param_set_uint_minmax(val, kp,
  2901. RPC_MIN_SLOT_TABLE,
  2902. RPC_MAX_SLOT_TABLE);
  2903. }
  2904. static const struct kernel_param_ops param_ops_slot_table_size = {
  2905. .set = param_set_slot_table_size,
  2906. .get = param_get_uint,
  2907. };
  2908. #define param_check_slot_table_size(name, p) \
  2909. __param_check(name, p, unsigned int);
  2910. static int param_set_max_slot_table_size(const char *val,
  2911. const struct kernel_param *kp)
  2912. {
  2913. return param_set_uint_minmax(val, kp,
  2914. RPC_MIN_SLOT_TABLE,
  2915. RPC_MAX_SLOT_TABLE_LIMIT);
  2916. }
  2917. static const struct kernel_param_ops param_ops_max_slot_table_size = {
  2918. .set = param_set_max_slot_table_size,
  2919. .get = param_get_uint,
  2920. };
  2921. #define param_check_max_slot_table_size(name, p) \
  2922. __param_check(name, p, unsigned int);
  2923. module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
  2924. slot_table_size, 0644);
  2925. module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
  2926. max_slot_table_size, 0644);
  2927. module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
  2928. slot_table_size, 0644);