xprtsock.c 83 KB

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