xprtsock.c 81 KB

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