xprtsock.c 88 KB

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