xprtsock.c 78 KB

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