xprtsock.c 85 KB

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