xprtsock.c 83 KB

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