messenger.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/crc32c.h>
  4. #include <linux/ctype.h>
  5. #include <linux/highmem.h>
  6. #include <linux/inet.h>
  7. #include <linux/kthread.h>
  8. #include <linux/net.h>
  9. #include <linux/nsproxy.h>
  10. #include <linux/sched/mm.h>
  11. #include <linux/slab.h>
  12. #include <linux/socket.h>
  13. #include <linux/string.h>
  14. #ifdef CONFIG_BLOCK
  15. #include <linux/bio.h>
  16. #endif /* CONFIG_BLOCK */
  17. #include <linux/dns_resolver.h>
  18. #include <net/tcp.h>
  19. #include <linux/ceph/ceph_features.h>
  20. #include <linux/ceph/libceph.h>
  21. #include <linux/ceph/messenger.h>
  22. #include <linux/ceph/decode.h>
  23. #include <linux/ceph/pagelist.h>
  24. #include <linux/export.h>
  25. /*
  26. * Ceph uses the messenger to exchange ceph_msg messages with other
  27. * hosts in the system. The messenger provides ordered and reliable
  28. * delivery. We tolerate TCP disconnects by reconnecting (with
  29. * exponential backoff) in the case of a fault (disconnection, bad
  30. * crc, protocol error). Acks allow sent messages to be discarded by
  31. * the sender.
  32. */
  33. /*
  34. * We track the state of the socket on a given connection using
  35. * values defined below. The transition to a new socket state is
  36. * handled by a function which verifies we aren't coming from an
  37. * unexpected state.
  38. *
  39. * --------
  40. * | NEW* | transient initial state
  41. * --------
  42. * | con_sock_state_init()
  43. * v
  44. * ----------
  45. * | CLOSED | initialized, but no socket (and no
  46. * ---------- TCP connection)
  47. * ^ \
  48. * | \ con_sock_state_connecting()
  49. * | ----------------------
  50. * | \
  51. * + con_sock_state_closed() \
  52. * |+--------------------------- \
  53. * | \ \ \
  54. * | ----------- \ \
  55. * | | CLOSING | socket event; \ \
  56. * | ----------- await close \ \
  57. * | ^ \ |
  58. * | | \ |
  59. * | + con_sock_state_closing() \ |
  60. * | / \ | |
  61. * | / --------------- | |
  62. * | / \ v v
  63. * | / --------------
  64. * | / -----------------| CONNECTING | socket created, TCP
  65. * | | / -------------- connect initiated
  66. * | | | con_sock_state_connected()
  67. * | | v
  68. * -------------
  69. * | CONNECTED | TCP connection established
  70. * -------------
  71. *
  72. * State values for ceph_connection->sock_state; NEW is assumed to be 0.
  73. */
  74. #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
  75. #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
  76. #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
  77. #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
  78. #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
  79. /*
  80. * connection states
  81. */
  82. #define CON_STATE_CLOSED 1 /* -> PREOPEN */
  83. #define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
  84. #define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
  85. #define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
  86. #define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
  87. #define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
  88. /*
  89. * ceph_connection flag bits
  90. */
  91. #define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
  92. * messages on errors */
  93. #define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
  94. #define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
  95. #define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
  96. #define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
  97. static bool con_flag_valid(unsigned long con_flag)
  98. {
  99. switch (con_flag) {
  100. case CON_FLAG_LOSSYTX:
  101. case CON_FLAG_KEEPALIVE_PENDING:
  102. case CON_FLAG_WRITE_PENDING:
  103. case CON_FLAG_SOCK_CLOSED:
  104. case CON_FLAG_BACKOFF:
  105. return true;
  106. default:
  107. return false;
  108. }
  109. }
  110. static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
  111. {
  112. BUG_ON(!con_flag_valid(con_flag));
  113. clear_bit(con_flag, &con->flags);
  114. }
  115. static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
  116. {
  117. BUG_ON(!con_flag_valid(con_flag));
  118. set_bit(con_flag, &con->flags);
  119. }
  120. static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
  121. {
  122. BUG_ON(!con_flag_valid(con_flag));
  123. return test_bit(con_flag, &con->flags);
  124. }
  125. static bool con_flag_test_and_clear(struct ceph_connection *con,
  126. unsigned long con_flag)
  127. {
  128. BUG_ON(!con_flag_valid(con_flag));
  129. return test_and_clear_bit(con_flag, &con->flags);
  130. }
  131. static bool con_flag_test_and_set(struct ceph_connection *con,
  132. unsigned long con_flag)
  133. {
  134. BUG_ON(!con_flag_valid(con_flag));
  135. return test_and_set_bit(con_flag, &con->flags);
  136. }
  137. /* Slab caches for frequently-allocated structures */
  138. static struct kmem_cache *ceph_msg_cache;
  139. static struct kmem_cache *ceph_msg_data_cache;
  140. /* static tag bytes (protocol control messages) */
  141. static char tag_msg = CEPH_MSGR_TAG_MSG;
  142. static char tag_ack = CEPH_MSGR_TAG_ACK;
  143. static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
  144. static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
  145. #ifdef CONFIG_LOCKDEP
  146. static struct lock_class_key socket_class;
  147. #endif
  148. /*
  149. * When skipping (ignoring) a block of input we read it into a "skip
  150. * buffer," which is this many bytes in size.
  151. */
  152. #define SKIP_BUF_SIZE 1024
  153. static void queue_con(struct ceph_connection *con);
  154. static void cancel_con(struct ceph_connection *con);
  155. static void ceph_con_workfn(struct work_struct *);
  156. static void con_fault(struct ceph_connection *con);
  157. /*
  158. * Nicely render a sockaddr as a string. An array of formatted
  159. * strings is used, to approximate reentrancy.
  160. */
  161. #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
  162. #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
  163. #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
  164. #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
  165. static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
  166. static atomic_t addr_str_seq = ATOMIC_INIT(0);
  167. static struct page *zero_page; /* used in certain error cases */
  168. const char *ceph_pr_addr(const struct sockaddr_storage *ss)
  169. {
  170. int i;
  171. char *s;
  172. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  173. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  174. i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
  175. s = addr_str[i];
  176. switch (ss->ss_family) {
  177. case AF_INET:
  178. snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
  179. ntohs(in4->sin_port));
  180. break;
  181. case AF_INET6:
  182. snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
  183. ntohs(in6->sin6_port));
  184. break;
  185. default:
  186. snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
  187. ss->ss_family);
  188. }
  189. return s;
  190. }
  191. EXPORT_SYMBOL(ceph_pr_addr);
  192. static void encode_my_addr(struct ceph_messenger *msgr)
  193. {
  194. memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
  195. ceph_encode_addr(&msgr->my_enc_addr);
  196. }
  197. /*
  198. * work queue for all reading and writing to/from the socket.
  199. */
  200. static struct workqueue_struct *ceph_msgr_wq;
  201. static int ceph_msgr_slab_init(void)
  202. {
  203. BUG_ON(ceph_msg_cache);
  204. ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
  205. if (!ceph_msg_cache)
  206. return -ENOMEM;
  207. BUG_ON(ceph_msg_data_cache);
  208. ceph_msg_data_cache = KMEM_CACHE(ceph_msg_data, 0);
  209. if (ceph_msg_data_cache)
  210. return 0;
  211. kmem_cache_destroy(ceph_msg_cache);
  212. ceph_msg_cache = NULL;
  213. return -ENOMEM;
  214. }
  215. static void ceph_msgr_slab_exit(void)
  216. {
  217. BUG_ON(!ceph_msg_data_cache);
  218. kmem_cache_destroy(ceph_msg_data_cache);
  219. ceph_msg_data_cache = NULL;
  220. BUG_ON(!ceph_msg_cache);
  221. kmem_cache_destroy(ceph_msg_cache);
  222. ceph_msg_cache = NULL;
  223. }
  224. static void _ceph_msgr_exit(void)
  225. {
  226. if (ceph_msgr_wq) {
  227. destroy_workqueue(ceph_msgr_wq);
  228. ceph_msgr_wq = NULL;
  229. }
  230. BUG_ON(zero_page == NULL);
  231. put_page(zero_page);
  232. zero_page = NULL;
  233. ceph_msgr_slab_exit();
  234. }
  235. int ceph_msgr_init(void)
  236. {
  237. if (ceph_msgr_slab_init())
  238. return -ENOMEM;
  239. BUG_ON(zero_page != NULL);
  240. zero_page = ZERO_PAGE(0);
  241. get_page(zero_page);
  242. /*
  243. * The number of active work items is limited by the number of
  244. * connections, so leave @max_active at default.
  245. */
  246. ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
  247. if (ceph_msgr_wq)
  248. return 0;
  249. pr_err("msgr_init failed to create workqueue\n");
  250. _ceph_msgr_exit();
  251. return -ENOMEM;
  252. }
  253. EXPORT_SYMBOL(ceph_msgr_init);
  254. void ceph_msgr_exit(void)
  255. {
  256. BUG_ON(ceph_msgr_wq == NULL);
  257. _ceph_msgr_exit();
  258. }
  259. EXPORT_SYMBOL(ceph_msgr_exit);
  260. void ceph_msgr_flush(void)
  261. {
  262. flush_workqueue(ceph_msgr_wq);
  263. }
  264. EXPORT_SYMBOL(ceph_msgr_flush);
  265. /* Connection socket state transition functions */
  266. static void con_sock_state_init(struct ceph_connection *con)
  267. {
  268. int old_state;
  269. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
  270. if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
  271. printk("%s: unexpected old state %d\n", __func__, old_state);
  272. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  273. CON_SOCK_STATE_CLOSED);
  274. }
  275. static void con_sock_state_connecting(struct ceph_connection *con)
  276. {
  277. int old_state;
  278. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
  279. if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
  280. printk("%s: unexpected old state %d\n", __func__, old_state);
  281. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  282. CON_SOCK_STATE_CONNECTING);
  283. }
  284. static void con_sock_state_connected(struct ceph_connection *con)
  285. {
  286. int old_state;
  287. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
  288. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
  289. printk("%s: unexpected old state %d\n", __func__, old_state);
  290. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  291. CON_SOCK_STATE_CONNECTED);
  292. }
  293. static void con_sock_state_closing(struct ceph_connection *con)
  294. {
  295. int old_state;
  296. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
  297. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
  298. old_state != CON_SOCK_STATE_CONNECTED &&
  299. old_state != CON_SOCK_STATE_CLOSING))
  300. printk("%s: unexpected old state %d\n", __func__, old_state);
  301. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  302. CON_SOCK_STATE_CLOSING);
  303. }
  304. static void con_sock_state_closed(struct ceph_connection *con)
  305. {
  306. int old_state;
  307. old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
  308. if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
  309. old_state != CON_SOCK_STATE_CLOSING &&
  310. old_state != CON_SOCK_STATE_CONNECTING &&
  311. old_state != CON_SOCK_STATE_CLOSED))
  312. printk("%s: unexpected old state %d\n", __func__, old_state);
  313. dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
  314. CON_SOCK_STATE_CLOSED);
  315. }
  316. /*
  317. * socket callback functions
  318. */
  319. /* data available on socket, or listen socket received a connect */
  320. static void ceph_sock_data_ready(struct sock *sk)
  321. {
  322. struct ceph_connection *con = sk->sk_user_data;
  323. if (atomic_read(&con->msgr->stopping)) {
  324. return;
  325. }
  326. if (sk->sk_state != TCP_CLOSE_WAIT) {
  327. dout("%s on %p state = %lu, queueing work\n", __func__,
  328. con, con->state);
  329. queue_con(con);
  330. }
  331. }
  332. /* socket has buffer space for writing */
  333. static void ceph_sock_write_space(struct sock *sk)
  334. {
  335. struct ceph_connection *con = sk->sk_user_data;
  336. /* only queue to workqueue if there is data we want to write,
  337. * and there is sufficient space in the socket buffer to accept
  338. * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
  339. * doesn't get called again until try_write() fills the socket
  340. * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
  341. * and net/core/stream.c:sk_stream_write_space().
  342. */
  343. if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
  344. if (sk_stream_is_writeable(sk)) {
  345. dout("%s %p queueing write work\n", __func__, con);
  346. clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  347. queue_con(con);
  348. }
  349. } else {
  350. dout("%s %p nothing to write\n", __func__, con);
  351. }
  352. }
  353. /* socket's state has changed */
  354. static void ceph_sock_state_change(struct sock *sk)
  355. {
  356. struct ceph_connection *con = sk->sk_user_data;
  357. dout("%s %p state = %lu sk_state = %u\n", __func__,
  358. con, con->state, sk->sk_state);
  359. switch (sk->sk_state) {
  360. case TCP_CLOSE:
  361. dout("%s TCP_CLOSE\n", __func__);
  362. case TCP_CLOSE_WAIT:
  363. dout("%s TCP_CLOSE_WAIT\n", __func__);
  364. con_sock_state_closing(con);
  365. con_flag_set(con, CON_FLAG_SOCK_CLOSED);
  366. queue_con(con);
  367. break;
  368. case TCP_ESTABLISHED:
  369. dout("%s TCP_ESTABLISHED\n", __func__);
  370. con_sock_state_connected(con);
  371. queue_con(con);
  372. break;
  373. default: /* Everything else is uninteresting */
  374. break;
  375. }
  376. }
  377. /*
  378. * set up socket callbacks
  379. */
  380. static void set_sock_callbacks(struct socket *sock,
  381. struct ceph_connection *con)
  382. {
  383. struct sock *sk = sock->sk;
  384. sk->sk_user_data = con;
  385. sk->sk_data_ready = ceph_sock_data_ready;
  386. sk->sk_write_space = ceph_sock_write_space;
  387. sk->sk_state_change = ceph_sock_state_change;
  388. }
  389. /*
  390. * socket helpers
  391. */
  392. /*
  393. * initiate connection to a remote socket.
  394. */
  395. static int ceph_tcp_connect(struct ceph_connection *con)
  396. {
  397. struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
  398. struct socket *sock;
  399. unsigned int noio_flag;
  400. int ret;
  401. BUG_ON(con->sock);
  402. /* sock_create_kern() allocates with GFP_KERNEL */
  403. noio_flag = memalloc_noio_save();
  404. ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
  405. SOCK_STREAM, IPPROTO_TCP, &sock);
  406. memalloc_noio_restore(noio_flag);
  407. if (ret)
  408. return ret;
  409. sock->sk->sk_allocation = GFP_NOFS;
  410. #ifdef CONFIG_LOCKDEP
  411. lockdep_set_class(&sock->sk->sk_lock, &socket_class);
  412. #endif
  413. set_sock_callbacks(sock, con);
  414. dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
  415. con_sock_state_connecting(con);
  416. ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
  417. O_NONBLOCK);
  418. if (ret == -EINPROGRESS) {
  419. dout("connect %s EINPROGRESS sk_state = %u\n",
  420. ceph_pr_addr(&con->peer_addr.in_addr),
  421. sock->sk->sk_state);
  422. } else if (ret < 0) {
  423. pr_err("connect %s error %d\n",
  424. ceph_pr_addr(&con->peer_addr.in_addr), ret);
  425. sock_release(sock);
  426. return ret;
  427. }
  428. if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
  429. int optval = 1;
  430. ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
  431. (char *)&optval, sizeof(optval));
  432. if (ret)
  433. pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
  434. ret);
  435. }
  436. con->sock = sock;
  437. return 0;
  438. }
  439. static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
  440. {
  441. struct kvec iov = {buf, len};
  442. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  443. int r;
  444. iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, len);
  445. r = sock_recvmsg(sock, &msg, msg.msg_flags);
  446. if (r == -EAGAIN)
  447. r = 0;
  448. return r;
  449. }
  450. static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
  451. int page_offset, size_t length)
  452. {
  453. struct bio_vec bvec = {
  454. .bv_page = page,
  455. .bv_offset = page_offset,
  456. .bv_len = length
  457. };
  458. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  459. int r;
  460. BUG_ON(page_offset + length > PAGE_SIZE);
  461. iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, &bvec, 1, length);
  462. r = sock_recvmsg(sock, &msg, msg.msg_flags);
  463. if (r == -EAGAIN)
  464. r = 0;
  465. return r;
  466. }
  467. /*
  468. * write something. @more is true if caller will be sending more data
  469. * shortly.
  470. */
  471. static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
  472. size_t kvlen, size_t len, int more)
  473. {
  474. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  475. int r;
  476. if (more)
  477. msg.msg_flags |= MSG_MORE;
  478. else
  479. msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
  480. r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
  481. if (r == -EAGAIN)
  482. r = 0;
  483. return r;
  484. }
  485. static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
  486. int offset, size_t size, bool more)
  487. {
  488. int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
  489. int ret;
  490. ret = kernel_sendpage(sock, page, offset, size, flags);
  491. if (ret == -EAGAIN)
  492. ret = 0;
  493. return ret;
  494. }
  495. static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
  496. int offset, size_t size, bool more)
  497. {
  498. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  499. struct bio_vec bvec;
  500. int ret;
  501. /*
  502. * sendpage cannot properly handle pages with page_count == 0,
  503. * we need to fall back to sendmsg if that's the case.
  504. *
  505. * Same goes for slab pages: skb_can_coalesce() allows
  506. * coalescing neighboring slab objects into a single frag which
  507. * triggers one of hardened usercopy checks.
  508. */
  509. if (page_count(page) >= 1 && !PageSlab(page))
  510. return __ceph_tcp_sendpage(sock, page, offset, size, more);
  511. bvec.bv_page = page;
  512. bvec.bv_offset = offset;
  513. bvec.bv_len = size;
  514. if (more)
  515. msg.msg_flags |= MSG_MORE;
  516. else
  517. msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
  518. iov_iter_bvec(&msg.msg_iter, WRITE | ITER_BVEC, &bvec, 1, size);
  519. ret = sock_sendmsg(sock, &msg);
  520. if (ret == -EAGAIN)
  521. ret = 0;
  522. return ret;
  523. }
  524. /*
  525. * Shutdown/close the socket for the given connection.
  526. */
  527. static int con_close_socket(struct ceph_connection *con)
  528. {
  529. int rc = 0;
  530. dout("con_close_socket on %p sock %p\n", con, con->sock);
  531. if (con->sock) {
  532. rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
  533. sock_release(con->sock);
  534. con->sock = NULL;
  535. }
  536. /*
  537. * Forcibly clear the SOCK_CLOSED flag. It gets set
  538. * independent of the connection mutex, and we could have
  539. * received a socket close event before we had the chance to
  540. * shut the socket down.
  541. */
  542. con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
  543. con_sock_state_closed(con);
  544. return rc;
  545. }
  546. /*
  547. * Reset a connection. Discard all incoming and outgoing messages
  548. * and clear *_seq state.
  549. */
  550. static void ceph_msg_remove(struct ceph_msg *msg)
  551. {
  552. list_del_init(&msg->list_head);
  553. ceph_msg_put(msg);
  554. }
  555. static void ceph_msg_remove_list(struct list_head *head)
  556. {
  557. while (!list_empty(head)) {
  558. struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
  559. list_head);
  560. ceph_msg_remove(msg);
  561. }
  562. }
  563. static void reset_connection(struct ceph_connection *con)
  564. {
  565. /* reset connection, out_queue, msg_ and connect_seq */
  566. /* discard existing out_queue and msg_seq */
  567. dout("reset_connection %p\n", con);
  568. ceph_msg_remove_list(&con->out_queue);
  569. ceph_msg_remove_list(&con->out_sent);
  570. if (con->in_msg) {
  571. BUG_ON(con->in_msg->con != con);
  572. ceph_msg_put(con->in_msg);
  573. con->in_msg = NULL;
  574. }
  575. con->connect_seq = 0;
  576. con->out_seq = 0;
  577. if (con->out_msg) {
  578. BUG_ON(con->out_msg->con != con);
  579. ceph_msg_put(con->out_msg);
  580. con->out_msg = NULL;
  581. }
  582. con->in_seq = 0;
  583. con->in_seq_acked = 0;
  584. con->out_skip = 0;
  585. }
  586. /*
  587. * mark a peer down. drop any open connections.
  588. */
  589. void ceph_con_close(struct ceph_connection *con)
  590. {
  591. mutex_lock(&con->mutex);
  592. dout("con_close %p peer %s\n", con,
  593. ceph_pr_addr(&con->peer_addr.in_addr));
  594. con->state = CON_STATE_CLOSED;
  595. con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
  596. con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
  597. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  598. con_flag_clear(con, CON_FLAG_BACKOFF);
  599. reset_connection(con);
  600. con->peer_global_seq = 0;
  601. cancel_con(con);
  602. con_close_socket(con);
  603. mutex_unlock(&con->mutex);
  604. }
  605. EXPORT_SYMBOL(ceph_con_close);
  606. /*
  607. * Reopen a closed connection, with a new peer address.
  608. */
  609. void ceph_con_open(struct ceph_connection *con,
  610. __u8 entity_type, __u64 entity_num,
  611. struct ceph_entity_addr *addr)
  612. {
  613. mutex_lock(&con->mutex);
  614. dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
  615. WARN_ON(con->state != CON_STATE_CLOSED);
  616. con->state = CON_STATE_PREOPEN;
  617. con->peer_name.type = (__u8) entity_type;
  618. con->peer_name.num = cpu_to_le64(entity_num);
  619. memcpy(&con->peer_addr, addr, sizeof(*addr));
  620. con->delay = 0; /* reset backoff memory */
  621. mutex_unlock(&con->mutex);
  622. queue_con(con);
  623. }
  624. EXPORT_SYMBOL(ceph_con_open);
  625. /*
  626. * return true if this connection ever successfully opened
  627. */
  628. bool ceph_con_opened(struct ceph_connection *con)
  629. {
  630. return con->connect_seq > 0;
  631. }
  632. /*
  633. * initialize a new connection.
  634. */
  635. void ceph_con_init(struct ceph_connection *con, void *private,
  636. const struct ceph_connection_operations *ops,
  637. struct ceph_messenger *msgr)
  638. {
  639. dout("con_init %p\n", con);
  640. memset(con, 0, sizeof(*con));
  641. con->private = private;
  642. con->ops = ops;
  643. con->msgr = msgr;
  644. con_sock_state_init(con);
  645. mutex_init(&con->mutex);
  646. INIT_LIST_HEAD(&con->out_queue);
  647. INIT_LIST_HEAD(&con->out_sent);
  648. INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
  649. con->state = CON_STATE_CLOSED;
  650. }
  651. EXPORT_SYMBOL(ceph_con_init);
  652. /*
  653. * We maintain a global counter to order connection attempts. Get
  654. * a unique seq greater than @gt.
  655. */
  656. static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
  657. {
  658. u32 ret;
  659. spin_lock(&msgr->global_seq_lock);
  660. if (msgr->global_seq < gt)
  661. msgr->global_seq = gt;
  662. ret = ++msgr->global_seq;
  663. spin_unlock(&msgr->global_seq_lock);
  664. return ret;
  665. }
  666. static void con_out_kvec_reset(struct ceph_connection *con)
  667. {
  668. BUG_ON(con->out_skip);
  669. con->out_kvec_left = 0;
  670. con->out_kvec_bytes = 0;
  671. con->out_kvec_cur = &con->out_kvec[0];
  672. }
  673. static void con_out_kvec_add(struct ceph_connection *con,
  674. size_t size, void *data)
  675. {
  676. int index = con->out_kvec_left;
  677. BUG_ON(con->out_skip);
  678. BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
  679. con->out_kvec[index].iov_len = size;
  680. con->out_kvec[index].iov_base = data;
  681. con->out_kvec_left++;
  682. con->out_kvec_bytes += size;
  683. }
  684. /*
  685. * Chop off a kvec from the end. Return residual number of bytes for
  686. * that kvec, i.e. how many bytes would have been written if the kvec
  687. * hadn't been nuked.
  688. */
  689. static int con_out_kvec_skip(struct ceph_connection *con)
  690. {
  691. int off = con->out_kvec_cur - con->out_kvec;
  692. int skip = 0;
  693. if (con->out_kvec_bytes > 0) {
  694. skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
  695. BUG_ON(con->out_kvec_bytes < skip);
  696. BUG_ON(!con->out_kvec_left);
  697. con->out_kvec_bytes -= skip;
  698. con->out_kvec_left--;
  699. }
  700. return skip;
  701. }
  702. #ifdef CONFIG_BLOCK
  703. /*
  704. * For a bio data item, a piece is whatever remains of the next
  705. * entry in the current bio iovec, or the first entry in the next
  706. * bio in the list.
  707. */
  708. static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
  709. size_t length)
  710. {
  711. struct ceph_msg_data *data = cursor->data;
  712. struct bio *bio;
  713. BUG_ON(data->type != CEPH_MSG_DATA_BIO);
  714. bio = data->bio;
  715. BUG_ON(!bio);
  716. cursor->resid = min(length, data->bio_length);
  717. cursor->bio = bio;
  718. cursor->bvec_iter = bio->bi_iter;
  719. cursor->last_piece =
  720. cursor->resid <= bio_iter_len(bio, cursor->bvec_iter);
  721. }
  722. static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
  723. size_t *page_offset,
  724. size_t *length)
  725. {
  726. struct ceph_msg_data *data = cursor->data;
  727. struct bio *bio;
  728. struct bio_vec bio_vec;
  729. BUG_ON(data->type != CEPH_MSG_DATA_BIO);
  730. bio = cursor->bio;
  731. BUG_ON(!bio);
  732. bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
  733. *page_offset = (size_t) bio_vec.bv_offset;
  734. BUG_ON(*page_offset >= PAGE_SIZE);
  735. if (cursor->last_piece) /* pagelist offset is always 0 */
  736. *length = cursor->resid;
  737. else
  738. *length = (size_t) bio_vec.bv_len;
  739. BUG_ON(*length > cursor->resid);
  740. BUG_ON(*page_offset + *length > PAGE_SIZE);
  741. return bio_vec.bv_page;
  742. }
  743. static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
  744. size_t bytes)
  745. {
  746. struct bio *bio;
  747. struct bio_vec bio_vec;
  748. BUG_ON(cursor->data->type != CEPH_MSG_DATA_BIO);
  749. bio = cursor->bio;
  750. BUG_ON(!bio);
  751. bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
  752. /* Advance the cursor offset */
  753. BUG_ON(cursor->resid < bytes);
  754. cursor->resid -= bytes;
  755. bio_advance_iter(bio, &cursor->bvec_iter, bytes);
  756. if (bytes < bio_vec.bv_len)
  757. return false; /* more bytes to process in this segment */
  758. /* Move on to the next segment, and possibly the next bio */
  759. if (!cursor->bvec_iter.bi_size) {
  760. bio = bio->bi_next;
  761. cursor->bio = bio;
  762. if (bio)
  763. cursor->bvec_iter = bio->bi_iter;
  764. else
  765. memset(&cursor->bvec_iter, 0,
  766. sizeof(cursor->bvec_iter));
  767. }
  768. if (!cursor->last_piece) {
  769. BUG_ON(!cursor->resid);
  770. BUG_ON(!bio);
  771. /* A short read is OK, so use <= rather than == */
  772. if (cursor->resid <= bio_iter_len(bio, cursor->bvec_iter))
  773. cursor->last_piece = true;
  774. }
  775. return true;
  776. }
  777. #endif /* CONFIG_BLOCK */
  778. /*
  779. * For a page array, a piece comes from the first page in the array
  780. * that has not already been fully consumed.
  781. */
  782. static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
  783. size_t length)
  784. {
  785. struct ceph_msg_data *data = cursor->data;
  786. int page_count;
  787. BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
  788. BUG_ON(!data->pages);
  789. BUG_ON(!data->length);
  790. cursor->resid = min(length, data->length);
  791. page_count = calc_pages_for(data->alignment, (u64)data->length);
  792. cursor->page_offset = data->alignment & ~PAGE_MASK;
  793. cursor->page_index = 0;
  794. BUG_ON(page_count > (int)USHRT_MAX);
  795. cursor->page_count = (unsigned short)page_count;
  796. BUG_ON(length > SIZE_MAX - cursor->page_offset);
  797. cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
  798. }
  799. static struct page *
  800. ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
  801. size_t *page_offset, size_t *length)
  802. {
  803. struct ceph_msg_data *data = cursor->data;
  804. BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
  805. BUG_ON(cursor->page_index >= cursor->page_count);
  806. BUG_ON(cursor->page_offset >= PAGE_SIZE);
  807. *page_offset = cursor->page_offset;
  808. if (cursor->last_piece)
  809. *length = cursor->resid;
  810. else
  811. *length = PAGE_SIZE - *page_offset;
  812. return data->pages[cursor->page_index];
  813. }
  814. static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
  815. size_t bytes)
  816. {
  817. BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
  818. BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
  819. /* Advance the cursor page offset */
  820. cursor->resid -= bytes;
  821. cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
  822. if (!bytes || cursor->page_offset)
  823. return false; /* more bytes to process in the current page */
  824. if (!cursor->resid)
  825. return false; /* no more data */
  826. /* Move on to the next page; offset is already at 0 */
  827. BUG_ON(cursor->page_index >= cursor->page_count);
  828. cursor->page_index++;
  829. cursor->last_piece = cursor->resid <= PAGE_SIZE;
  830. return true;
  831. }
  832. /*
  833. * For a pagelist, a piece is whatever remains to be consumed in the
  834. * first page in the list, or the front of the next page.
  835. */
  836. static void
  837. ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
  838. size_t length)
  839. {
  840. struct ceph_msg_data *data = cursor->data;
  841. struct ceph_pagelist *pagelist;
  842. struct page *page;
  843. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  844. pagelist = data->pagelist;
  845. BUG_ON(!pagelist);
  846. if (!length)
  847. return; /* pagelist can be assigned but empty */
  848. BUG_ON(list_empty(&pagelist->head));
  849. page = list_first_entry(&pagelist->head, struct page, lru);
  850. cursor->resid = min(length, pagelist->length);
  851. cursor->page = page;
  852. cursor->offset = 0;
  853. cursor->last_piece = cursor->resid <= PAGE_SIZE;
  854. }
  855. static struct page *
  856. ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
  857. size_t *page_offset, size_t *length)
  858. {
  859. struct ceph_msg_data *data = cursor->data;
  860. struct ceph_pagelist *pagelist;
  861. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  862. pagelist = data->pagelist;
  863. BUG_ON(!pagelist);
  864. BUG_ON(!cursor->page);
  865. BUG_ON(cursor->offset + cursor->resid != pagelist->length);
  866. /* offset of first page in pagelist is always 0 */
  867. *page_offset = cursor->offset & ~PAGE_MASK;
  868. if (cursor->last_piece)
  869. *length = cursor->resid;
  870. else
  871. *length = PAGE_SIZE - *page_offset;
  872. return cursor->page;
  873. }
  874. static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
  875. size_t bytes)
  876. {
  877. struct ceph_msg_data *data = cursor->data;
  878. struct ceph_pagelist *pagelist;
  879. BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
  880. pagelist = data->pagelist;
  881. BUG_ON(!pagelist);
  882. BUG_ON(cursor->offset + cursor->resid != pagelist->length);
  883. BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
  884. /* Advance the cursor offset */
  885. cursor->resid -= bytes;
  886. cursor->offset += bytes;
  887. /* offset of first page in pagelist is always 0 */
  888. if (!bytes || cursor->offset & ~PAGE_MASK)
  889. return false; /* more bytes to process in the current page */
  890. if (!cursor->resid)
  891. return false; /* no more data */
  892. /* Move on to the next page */
  893. BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
  894. cursor->page = list_next_entry(cursor->page, lru);
  895. cursor->last_piece = cursor->resid <= PAGE_SIZE;
  896. return true;
  897. }
  898. /*
  899. * Message data is handled (sent or received) in pieces, where each
  900. * piece resides on a single page. The network layer might not
  901. * consume an entire piece at once. A data item's cursor keeps
  902. * track of which piece is next to process and how much remains to
  903. * be processed in that piece. It also tracks whether the current
  904. * piece is the last one in the data item.
  905. */
  906. static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
  907. {
  908. size_t length = cursor->total_resid;
  909. switch (cursor->data->type) {
  910. case CEPH_MSG_DATA_PAGELIST:
  911. ceph_msg_data_pagelist_cursor_init(cursor, length);
  912. break;
  913. case CEPH_MSG_DATA_PAGES:
  914. ceph_msg_data_pages_cursor_init(cursor, length);
  915. break;
  916. #ifdef CONFIG_BLOCK
  917. case CEPH_MSG_DATA_BIO:
  918. ceph_msg_data_bio_cursor_init(cursor, length);
  919. break;
  920. #endif /* CONFIG_BLOCK */
  921. case CEPH_MSG_DATA_NONE:
  922. default:
  923. /* BUG(); */
  924. break;
  925. }
  926. cursor->need_crc = true;
  927. }
  928. static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
  929. {
  930. struct ceph_msg_data_cursor *cursor = &msg->cursor;
  931. struct ceph_msg_data *data;
  932. BUG_ON(!length);
  933. BUG_ON(length > msg->data_length);
  934. BUG_ON(list_empty(&msg->data));
  935. cursor->data_head = &msg->data;
  936. cursor->total_resid = length;
  937. data = list_first_entry(&msg->data, struct ceph_msg_data, links);
  938. cursor->data = data;
  939. __ceph_msg_data_cursor_init(cursor);
  940. }
  941. /*
  942. * Return the page containing the next piece to process for a given
  943. * data item, and supply the page offset and length of that piece.
  944. * Indicate whether this is the last piece in this data item.
  945. */
  946. static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
  947. size_t *page_offset, size_t *length,
  948. bool *last_piece)
  949. {
  950. struct page *page;
  951. switch (cursor->data->type) {
  952. case CEPH_MSG_DATA_PAGELIST:
  953. page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
  954. break;
  955. case CEPH_MSG_DATA_PAGES:
  956. page = ceph_msg_data_pages_next(cursor, page_offset, length);
  957. break;
  958. #ifdef CONFIG_BLOCK
  959. case CEPH_MSG_DATA_BIO:
  960. page = ceph_msg_data_bio_next(cursor, page_offset, length);
  961. break;
  962. #endif /* CONFIG_BLOCK */
  963. case CEPH_MSG_DATA_NONE:
  964. default:
  965. page = NULL;
  966. break;
  967. }
  968. BUG_ON(!page);
  969. BUG_ON(*page_offset + *length > PAGE_SIZE);
  970. BUG_ON(!*length);
  971. if (last_piece)
  972. *last_piece = cursor->last_piece;
  973. return page;
  974. }
  975. /*
  976. * Returns true if the result moves the cursor on to the next piece
  977. * of the data item.
  978. */
  979. static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
  980. size_t bytes)
  981. {
  982. bool new_piece;
  983. BUG_ON(bytes > cursor->resid);
  984. switch (cursor->data->type) {
  985. case CEPH_MSG_DATA_PAGELIST:
  986. new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
  987. break;
  988. case CEPH_MSG_DATA_PAGES:
  989. new_piece = ceph_msg_data_pages_advance(cursor, bytes);
  990. break;
  991. #ifdef CONFIG_BLOCK
  992. case CEPH_MSG_DATA_BIO:
  993. new_piece = ceph_msg_data_bio_advance(cursor, bytes);
  994. break;
  995. #endif /* CONFIG_BLOCK */
  996. case CEPH_MSG_DATA_NONE:
  997. default:
  998. BUG();
  999. break;
  1000. }
  1001. cursor->total_resid -= bytes;
  1002. if (!cursor->resid && cursor->total_resid) {
  1003. WARN_ON(!cursor->last_piece);
  1004. BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
  1005. cursor->data = list_next_entry(cursor->data, links);
  1006. __ceph_msg_data_cursor_init(cursor);
  1007. new_piece = true;
  1008. }
  1009. cursor->need_crc = new_piece;
  1010. }
  1011. static size_t sizeof_footer(struct ceph_connection *con)
  1012. {
  1013. return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
  1014. sizeof(struct ceph_msg_footer) :
  1015. sizeof(struct ceph_msg_footer_old);
  1016. }
  1017. static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
  1018. {
  1019. BUG_ON(!msg);
  1020. BUG_ON(!data_len);
  1021. /* Initialize data cursor */
  1022. ceph_msg_data_cursor_init(msg, (size_t)data_len);
  1023. }
  1024. /*
  1025. * Prepare footer for currently outgoing message, and finish things
  1026. * off. Assumes out_kvec* are already valid.. we just add on to the end.
  1027. */
  1028. static void prepare_write_message_footer(struct ceph_connection *con)
  1029. {
  1030. struct ceph_msg *m = con->out_msg;
  1031. m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
  1032. dout("prepare_write_message_footer %p\n", con);
  1033. con_out_kvec_add(con, sizeof_footer(con), &m->footer);
  1034. if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
  1035. if (con->ops->sign_message)
  1036. con->ops->sign_message(m);
  1037. else
  1038. m->footer.sig = 0;
  1039. } else {
  1040. m->old_footer.flags = m->footer.flags;
  1041. }
  1042. con->out_more = m->more_to_follow;
  1043. con->out_msg_done = true;
  1044. }
  1045. /*
  1046. * Prepare headers for the next outgoing message.
  1047. */
  1048. static void prepare_write_message(struct ceph_connection *con)
  1049. {
  1050. struct ceph_msg *m;
  1051. u32 crc;
  1052. con_out_kvec_reset(con);
  1053. con->out_msg_done = false;
  1054. /* Sneak an ack in there first? If we can get it into the same
  1055. * TCP packet that's a good thing. */
  1056. if (con->in_seq > con->in_seq_acked) {
  1057. con->in_seq_acked = con->in_seq;
  1058. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  1059. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  1060. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  1061. &con->out_temp_ack);
  1062. }
  1063. BUG_ON(list_empty(&con->out_queue));
  1064. m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
  1065. con->out_msg = m;
  1066. BUG_ON(m->con != con);
  1067. /* put message on sent list */
  1068. ceph_msg_get(m);
  1069. list_move_tail(&m->list_head, &con->out_sent);
  1070. /*
  1071. * only assign outgoing seq # if we haven't sent this message
  1072. * yet. if it is requeued, resend with it's original seq.
  1073. */
  1074. if (m->needs_out_seq) {
  1075. m->hdr.seq = cpu_to_le64(++con->out_seq);
  1076. m->needs_out_seq = false;
  1077. if (con->ops->reencode_message)
  1078. con->ops->reencode_message(m);
  1079. }
  1080. dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
  1081. m, con->out_seq, le16_to_cpu(m->hdr.type),
  1082. le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
  1083. m->data_length);
  1084. WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
  1085. WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
  1086. /* tag + hdr + front + middle */
  1087. con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
  1088. con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
  1089. con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
  1090. if (m->middle)
  1091. con_out_kvec_add(con, m->middle->vec.iov_len,
  1092. m->middle->vec.iov_base);
  1093. /* fill in hdr crc and finalize hdr */
  1094. crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
  1095. con->out_msg->hdr.crc = cpu_to_le32(crc);
  1096. memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
  1097. /* fill in front and middle crc, footer */
  1098. crc = crc32c(0, m->front.iov_base, m->front.iov_len);
  1099. con->out_msg->footer.front_crc = cpu_to_le32(crc);
  1100. if (m->middle) {
  1101. crc = crc32c(0, m->middle->vec.iov_base,
  1102. m->middle->vec.iov_len);
  1103. con->out_msg->footer.middle_crc = cpu_to_le32(crc);
  1104. } else
  1105. con->out_msg->footer.middle_crc = 0;
  1106. dout("%s front_crc %u middle_crc %u\n", __func__,
  1107. le32_to_cpu(con->out_msg->footer.front_crc),
  1108. le32_to_cpu(con->out_msg->footer.middle_crc));
  1109. con->out_msg->footer.flags = 0;
  1110. /* is there a data payload? */
  1111. con->out_msg->footer.data_crc = 0;
  1112. if (m->data_length) {
  1113. prepare_message_data(con->out_msg, m->data_length);
  1114. con->out_more = 1; /* data + footer will follow */
  1115. } else {
  1116. /* no, queue up footer too and be done */
  1117. prepare_write_message_footer(con);
  1118. }
  1119. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1120. }
  1121. /*
  1122. * Prepare an ack.
  1123. */
  1124. static void prepare_write_ack(struct ceph_connection *con)
  1125. {
  1126. dout("prepare_write_ack %p %llu -> %llu\n", con,
  1127. con->in_seq_acked, con->in_seq);
  1128. con->in_seq_acked = con->in_seq;
  1129. con_out_kvec_reset(con);
  1130. con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
  1131. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  1132. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  1133. &con->out_temp_ack);
  1134. con->out_more = 1; /* more will follow.. eventually.. */
  1135. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1136. }
  1137. /*
  1138. * Prepare to share the seq during handshake
  1139. */
  1140. static void prepare_write_seq(struct ceph_connection *con)
  1141. {
  1142. dout("prepare_write_seq %p %llu -> %llu\n", con,
  1143. con->in_seq_acked, con->in_seq);
  1144. con->in_seq_acked = con->in_seq;
  1145. con_out_kvec_reset(con);
  1146. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  1147. con_out_kvec_add(con, sizeof (con->out_temp_ack),
  1148. &con->out_temp_ack);
  1149. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1150. }
  1151. /*
  1152. * Prepare to write keepalive byte.
  1153. */
  1154. static void prepare_write_keepalive(struct ceph_connection *con)
  1155. {
  1156. dout("prepare_write_keepalive %p\n", con);
  1157. con_out_kvec_reset(con);
  1158. if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
  1159. struct timespec now;
  1160. ktime_get_real_ts(&now);
  1161. con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
  1162. ceph_encode_timespec(&con->out_temp_keepalive2, &now);
  1163. con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
  1164. &con->out_temp_keepalive2);
  1165. } else {
  1166. con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
  1167. }
  1168. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1169. }
  1170. /*
  1171. * Connection negotiation.
  1172. */
  1173. static int get_connect_authorizer(struct ceph_connection *con)
  1174. {
  1175. struct ceph_auth_handshake *auth;
  1176. int auth_proto;
  1177. if (!con->ops->get_authorizer) {
  1178. con->auth = NULL;
  1179. con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
  1180. con->out_connect.authorizer_len = 0;
  1181. return 0;
  1182. }
  1183. auth = con->ops->get_authorizer(con, &auth_proto, con->auth_retry);
  1184. if (IS_ERR(auth))
  1185. return PTR_ERR(auth);
  1186. con->auth = auth;
  1187. con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
  1188. con->out_connect.authorizer_len = cpu_to_le32(auth->authorizer_buf_len);
  1189. return 0;
  1190. }
  1191. /*
  1192. * We connected to a peer and are saying hello.
  1193. */
  1194. static void prepare_write_banner(struct ceph_connection *con)
  1195. {
  1196. con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
  1197. con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
  1198. &con->msgr->my_enc_addr);
  1199. con->out_more = 0;
  1200. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1201. }
  1202. static void __prepare_write_connect(struct ceph_connection *con)
  1203. {
  1204. con_out_kvec_add(con, sizeof(con->out_connect), &con->out_connect);
  1205. if (con->auth)
  1206. con_out_kvec_add(con, con->auth->authorizer_buf_len,
  1207. con->auth->authorizer_buf);
  1208. con->out_more = 0;
  1209. con_flag_set(con, CON_FLAG_WRITE_PENDING);
  1210. }
  1211. static int prepare_write_connect(struct ceph_connection *con)
  1212. {
  1213. unsigned int global_seq = get_global_seq(con->msgr, 0);
  1214. int proto;
  1215. int ret;
  1216. switch (con->peer_name.type) {
  1217. case CEPH_ENTITY_TYPE_MON:
  1218. proto = CEPH_MONC_PROTOCOL;
  1219. break;
  1220. case CEPH_ENTITY_TYPE_OSD:
  1221. proto = CEPH_OSDC_PROTOCOL;
  1222. break;
  1223. case CEPH_ENTITY_TYPE_MDS:
  1224. proto = CEPH_MDSC_PROTOCOL;
  1225. break;
  1226. default:
  1227. BUG();
  1228. }
  1229. dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
  1230. con->connect_seq, global_seq, proto);
  1231. con->out_connect.features =
  1232. cpu_to_le64(from_msgr(con->msgr)->supported_features);
  1233. con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
  1234. con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
  1235. con->out_connect.global_seq = cpu_to_le32(global_seq);
  1236. con->out_connect.protocol_version = cpu_to_le32(proto);
  1237. con->out_connect.flags = 0;
  1238. ret = get_connect_authorizer(con);
  1239. if (ret)
  1240. return ret;
  1241. __prepare_write_connect(con);
  1242. return 0;
  1243. }
  1244. /*
  1245. * write as much of pending kvecs to the socket as we can.
  1246. * 1 -> done
  1247. * 0 -> socket full, but more to do
  1248. * <0 -> error
  1249. */
  1250. static int write_partial_kvec(struct ceph_connection *con)
  1251. {
  1252. int ret;
  1253. dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
  1254. while (con->out_kvec_bytes > 0) {
  1255. ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
  1256. con->out_kvec_left, con->out_kvec_bytes,
  1257. con->out_more);
  1258. if (ret <= 0)
  1259. goto out;
  1260. con->out_kvec_bytes -= ret;
  1261. if (con->out_kvec_bytes == 0)
  1262. break; /* done */
  1263. /* account for full iov entries consumed */
  1264. while (ret >= con->out_kvec_cur->iov_len) {
  1265. BUG_ON(!con->out_kvec_left);
  1266. ret -= con->out_kvec_cur->iov_len;
  1267. con->out_kvec_cur++;
  1268. con->out_kvec_left--;
  1269. }
  1270. /* and for a partially-consumed entry */
  1271. if (ret) {
  1272. con->out_kvec_cur->iov_len -= ret;
  1273. con->out_kvec_cur->iov_base += ret;
  1274. }
  1275. }
  1276. con->out_kvec_left = 0;
  1277. ret = 1;
  1278. out:
  1279. dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
  1280. con->out_kvec_bytes, con->out_kvec_left, ret);
  1281. return ret; /* done! */
  1282. }
  1283. static u32 ceph_crc32c_page(u32 crc, struct page *page,
  1284. unsigned int page_offset,
  1285. unsigned int length)
  1286. {
  1287. char *kaddr;
  1288. kaddr = kmap(page);
  1289. BUG_ON(kaddr == NULL);
  1290. crc = crc32c(crc, kaddr + page_offset, length);
  1291. kunmap(page);
  1292. return crc;
  1293. }
  1294. /*
  1295. * Write as much message data payload as we can. If we finish, queue
  1296. * up the footer.
  1297. * 1 -> done, footer is now queued in out_kvec[].
  1298. * 0 -> socket full, but more to do
  1299. * <0 -> error
  1300. */
  1301. static int write_partial_message_data(struct ceph_connection *con)
  1302. {
  1303. struct ceph_msg *msg = con->out_msg;
  1304. struct ceph_msg_data_cursor *cursor = &msg->cursor;
  1305. bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
  1306. u32 crc;
  1307. dout("%s %p msg %p\n", __func__, con, msg);
  1308. if (list_empty(&msg->data))
  1309. return -EINVAL;
  1310. /*
  1311. * Iterate through each page that contains data to be
  1312. * written, and send as much as possible for each.
  1313. *
  1314. * If we are calculating the data crc (the default), we will
  1315. * need to map the page. If we have no pages, they have
  1316. * been revoked, so use the zero page.
  1317. */
  1318. crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
  1319. while (cursor->resid) {
  1320. struct page *page;
  1321. size_t page_offset;
  1322. size_t length;
  1323. bool last_piece;
  1324. int ret;
  1325. page = ceph_msg_data_next(cursor, &page_offset, &length,
  1326. &last_piece);
  1327. ret = ceph_tcp_sendpage(con->sock, page, page_offset,
  1328. length, !last_piece);
  1329. if (ret <= 0) {
  1330. if (do_datacrc)
  1331. msg->footer.data_crc = cpu_to_le32(crc);
  1332. return ret;
  1333. }
  1334. if (do_datacrc && cursor->need_crc)
  1335. crc = ceph_crc32c_page(crc, page, page_offset, length);
  1336. ceph_msg_data_advance(cursor, (size_t)ret);
  1337. }
  1338. dout("%s %p msg %p done\n", __func__, con, msg);
  1339. /* prepare and queue up footer, too */
  1340. if (do_datacrc)
  1341. msg->footer.data_crc = cpu_to_le32(crc);
  1342. else
  1343. msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
  1344. con_out_kvec_reset(con);
  1345. prepare_write_message_footer(con);
  1346. return 1; /* must return > 0 to indicate success */
  1347. }
  1348. /*
  1349. * write some zeros
  1350. */
  1351. static int write_partial_skip(struct ceph_connection *con)
  1352. {
  1353. int ret;
  1354. dout("%s %p %d left\n", __func__, con, con->out_skip);
  1355. while (con->out_skip > 0) {
  1356. size_t size = min(con->out_skip, (int) PAGE_SIZE);
  1357. ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
  1358. if (ret <= 0)
  1359. goto out;
  1360. con->out_skip -= ret;
  1361. }
  1362. ret = 1;
  1363. out:
  1364. return ret;
  1365. }
  1366. /*
  1367. * Prepare to read connection handshake, or an ack.
  1368. */
  1369. static void prepare_read_banner(struct ceph_connection *con)
  1370. {
  1371. dout("prepare_read_banner %p\n", con);
  1372. con->in_base_pos = 0;
  1373. }
  1374. static void prepare_read_connect(struct ceph_connection *con)
  1375. {
  1376. dout("prepare_read_connect %p\n", con);
  1377. con->in_base_pos = 0;
  1378. }
  1379. static void prepare_read_ack(struct ceph_connection *con)
  1380. {
  1381. dout("prepare_read_ack %p\n", con);
  1382. con->in_base_pos = 0;
  1383. }
  1384. static void prepare_read_seq(struct ceph_connection *con)
  1385. {
  1386. dout("prepare_read_seq %p\n", con);
  1387. con->in_base_pos = 0;
  1388. con->in_tag = CEPH_MSGR_TAG_SEQ;
  1389. }
  1390. static void prepare_read_tag(struct ceph_connection *con)
  1391. {
  1392. dout("prepare_read_tag %p\n", con);
  1393. con->in_base_pos = 0;
  1394. con->in_tag = CEPH_MSGR_TAG_READY;
  1395. }
  1396. static void prepare_read_keepalive_ack(struct ceph_connection *con)
  1397. {
  1398. dout("prepare_read_keepalive_ack %p\n", con);
  1399. con->in_base_pos = 0;
  1400. }
  1401. /*
  1402. * Prepare to read a message.
  1403. */
  1404. static int prepare_read_message(struct ceph_connection *con)
  1405. {
  1406. dout("prepare_read_message %p\n", con);
  1407. BUG_ON(con->in_msg != NULL);
  1408. con->in_base_pos = 0;
  1409. con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
  1410. return 0;
  1411. }
  1412. static int read_partial(struct ceph_connection *con,
  1413. int end, int size, void *object)
  1414. {
  1415. while (con->in_base_pos < end) {
  1416. int left = end - con->in_base_pos;
  1417. int have = size - left;
  1418. int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
  1419. if (ret <= 0)
  1420. return ret;
  1421. con->in_base_pos += ret;
  1422. }
  1423. return 1;
  1424. }
  1425. /*
  1426. * Read all or part of the connect-side handshake on a new connection
  1427. */
  1428. static int read_partial_banner(struct ceph_connection *con)
  1429. {
  1430. int size;
  1431. int end;
  1432. int ret;
  1433. dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
  1434. /* peer's banner */
  1435. size = strlen(CEPH_BANNER);
  1436. end = size;
  1437. ret = read_partial(con, end, size, con->in_banner);
  1438. if (ret <= 0)
  1439. goto out;
  1440. size = sizeof (con->actual_peer_addr);
  1441. end += size;
  1442. ret = read_partial(con, end, size, &con->actual_peer_addr);
  1443. if (ret <= 0)
  1444. goto out;
  1445. size = sizeof (con->peer_addr_for_me);
  1446. end += size;
  1447. ret = read_partial(con, end, size, &con->peer_addr_for_me);
  1448. if (ret <= 0)
  1449. goto out;
  1450. out:
  1451. return ret;
  1452. }
  1453. static int read_partial_connect(struct ceph_connection *con)
  1454. {
  1455. int size;
  1456. int end;
  1457. int ret;
  1458. dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
  1459. size = sizeof (con->in_reply);
  1460. end = size;
  1461. ret = read_partial(con, end, size, &con->in_reply);
  1462. if (ret <= 0)
  1463. goto out;
  1464. if (con->auth) {
  1465. size = le32_to_cpu(con->in_reply.authorizer_len);
  1466. if (size > con->auth->authorizer_reply_buf_len) {
  1467. pr_err("authorizer reply too big: %d > %zu\n", size,
  1468. con->auth->authorizer_reply_buf_len);
  1469. ret = -EINVAL;
  1470. goto out;
  1471. }
  1472. end += size;
  1473. ret = read_partial(con, end, size,
  1474. con->auth->authorizer_reply_buf);
  1475. if (ret <= 0)
  1476. goto out;
  1477. }
  1478. dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
  1479. con, (int)con->in_reply.tag,
  1480. le32_to_cpu(con->in_reply.connect_seq),
  1481. le32_to_cpu(con->in_reply.global_seq));
  1482. out:
  1483. return ret;
  1484. }
  1485. /*
  1486. * Verify the hello banner looks okay.
  1487. */
  1488. static int verify_hello(struct ceph_connection *con)
  1489. {
  1490. if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
  1491. pr_err("connect to %s got bad banner\n",
  1492. ceph_pr_addr(&con->peer_addr.in_addr));
  1493. con->error_msg = "protocol error, bad banner";
  1494. return -1;
  1495. }
  1496. return 0;
  1497. }
  1498. static bool addr_is_blank(struct sockaddr_storage *ss)
  1499. {
  1500. struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
  1501. struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
  1502. switch (ss->ss_family) {
  1503. case AF_INET:
  1504. return addr->s_addr == htonl(INADDR_ANY);
  1505. case AF_INET6:
  1506. return ipv6_addr_any(addr6);
  1507. default:
  1508. return true;
  1509. }
  1510. }
  1511. static int addr_port(struct sockaddr_storage *ss)
  1512. {
  1513. switch (ss->ss_family) {
  1514. case AF_INET:
  1515. return ntohs(((struct sockaddr_in *)ss)->sin_port);
  1516. case AF_INET6:
  1517. return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
  1518. }
  1519. return 0;
  1520. }
  1521. static void addr_set_port(struct sockaddr_storage *ss, int p)
  1522. {
  1523. switch (ss->ss_family) {
  1524. case AF_INET:
  1525. ((struct sockaddr_in *)ss)->sin_port = htons(p);
  1526. break;
  1527. case AF_INET6:
  1528. ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
  1529. break;
  1530. }
  1531. }
  1532. /*
  1533. * Unlike other *_pton function semantics, zero indicates success.
  1534. */
  1535. static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
  1536. char delim, const char **ipend)
  1537. {
  1538. struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
  1539. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
  1540. memset(ss, 0, sizeof(*ss));
  1541. if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
  1542. ss->ss_family = AF_INET;
  1543. return 0;
  1544. }
  1545. if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
  1546. ss->ss_family = AF_INET6;
  1547. return 0;
  1548. }
  1549. return -EINVAL;
  1550. }
  1551. /*
  1552. * Extract hostname string and resolve using kernel DNS facility.
  1553. */
  1554. #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
  1555. static int ceph_dns_resolve_name(const char *name, size_t namelen,
  1556. struct sockaddr_storage *ss, char delim, const char **ipend)
  1557. {
  1558. const char *end, *delim_p;
  1559. char *colon_p, *ip_addr = NULL;
  1560. int ip_len, ret;
  1561. /*
  1562. * The end of the hostname occurs immediately preceding the delimiter or
  1563. * the port marker (':') where the delimiter takes precedence.
  1564. */
  1565. delim_p = memchr(name, delim, namelen);
  1566. colon_p = memchr(name, ':', namelen);
  1567. if (delim_p && colon_p)
  1568. end = delim_p < colon_p ? delim_p : colon_p;
  1569. else if (!delim_p && colon_p)
  1570. end = colon_p;
  1571. else {
  1572. end = delim_p;
  1573. if (!end) /* case: hostname:/ */
  1574. end = name + namelen;
  1575. }
  1576. if (end <= name)
  1577. return -EINVAL;
  1578. /* do dns_resolve upcall */
  1579. ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
  1580. if (ip_len > 0)
  1581. ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
  1582. else
  1583. ret = -ESRCH;
  1584. kfree(ip_addr);
  1585. *ipend = end;
  1586. pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
  1587. ret, ret ? "failed" : ceph_pr_addr(ss));
  1588. return ret;
  1589. }
  1590. #else
  1591. static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
  1592. struct sockaddr_storage *ss, char delim, const char **ipend)
  1593. {
  1594. return -EINVAL;
  1595. }
  1596. #endif
  1597. /*
  1598. * Parse a server name (IP or hostname). If a valid IP address is not found
  1599. * then try to extract a hostname to resolve using userspace DNS upcall.
  1600. */
  1601. static int ceph_parse_server_name(const char *name, size_t namelen,
  1602. struct sockaddr_storage *ss, char delim, const char **ipend)
  1603. {
  1604. int ret;
  1605. ret = ceph_pton(name, namelen, ss, delim, ipend);
  1606. if (ret)
  1607. ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
  1608. return ret;
  1609. }
  1610. /*
  1611. * Parse an ip[:port] list into an addr array. Use the default
  1612. * monitor port if a port isn't specified.
  1613. */
  1614. int ceph_parse_ips(const char *c, const char *end,
  1615. struct ceph_entity_addr *addr,
  1616. int max_count, int *count)
  1617. {
  1618. int i, ret = -EINVAL;
  1619. const char *p = c;
  1620. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  1621. for (i = 0; i < max_count; i++) {
  1622. const char *ipend;
  1623. struct sockaddr_storage *ss = &addr[i].in_addr;
  1624. int port;
  1625. char delim = ',';
  1626. if (*p == '[') {
  1627. delim = ']';
  1628. p++;
  1629. }
  1630. ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
  1631. if (ret)
  1632. goto bad;
  1633. ret = -EINVAL;
  1634. p = ipend;
  1635. if (delim == ']') {
  1636. if (*p != ']') {
  1637. dout("missing matching ']'\n");
  1638. goto bad;
  1639. }
  1640. p++;
  1641. }
  1642. /* port? */
  1643. if (p < end && *p == ':') {
  1644. port = 0;
  1645. p++;
  1646. while (p < end && *p >= '0' && *p <= '9') {
  1647. port = (port * 10) + (*p - '0');
  1648. p++;
  1649. }
  1650. if (port == 0)
  1651. port = CEPH_MON_PORT;
  1652. else if (port > 65535)
  1653. goto bad;
  1654. } else {
  1655. port = CEPH_MON_PORT;
  1656. }
  1657. addr_set_port(ss, port);
  1658. dout("parse_ips got %s\n", ceph_pr_addr(ss));
  1659. if (p == end)
  1660. break;
  1661. if (*p != ',')
  1662. goto bad;
  1663. p++;
  1664. }
  1665. if (p != end)
  1666. goto bad;
  1667. if (count)
  1668. *count = i + 1;
  1669. return 0;
  1670. bad:
  1671. pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
  1672. return ret;
  1673. }
  1674. EXPORT_SYMBOL(ceph_parse_ips);
  1675. static int process_banner(struct ceph_connection *con)
  1676. {
  1677. dout("process_banner on %p\n", con);
  1678. if (verify_hello(con) < 0)
  1679. return -1;
  1680. ceph_decode_addr(&con->actual_peer_addr);
  1681. ceph_decode_addr(&con->peer_addr_for_me);
  1682. /*
  1683. * Make sure the other end is who we wanted. note that the other
  1684. * end may not yet know their ip address, so if it's 0.0.0.0, give
  1685. * them the benefit of the doubt.
  1686. */
  1687. if (memcmp(&con->peer_addr, &con->actual_peer_addr,
  1688. sizeof(con->peer_addr)) != 0 &&
  1689. !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
  1690. con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
  1691. pr_warn("wrong peer, want %s/%d, got %s/%d\n",
  1692. ceph_pr_addr(&con->peer_addr.in_addr),
  1693. (int)le32_to_cpu(con->peer_addr.nonce),
  1694. ceph_pr_addr(&con->actual_peer_addr.in_addr),
  1695. (int)le32_to_cpu(con->actual_peer_addr.nonce));
  1696. con->error_msg = "wrong peer at address";
  1697. return -1;
  1698. }
  1699. /*
  1700. * did we learn our address?
  1701. */
  1702. if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
  1703. int port = addr_port(&con->msgr->inst.addr.in_addr);
  1704. memcpy(&con->msgr->inst.addr.in_addr,
  1705. &con->peer_addr_for_me.in_addr,
  1706. sizeof(con->peer_addr_for_me.in_addr));
  1707. addr_set_port(&con->msgr->inst.addr.in_addr, port);
  1708. encode_my_addr(con->msgr);
  1709. dout("process_banner learned my addr is %s\n",
  1710. ceph_pr_addr(&con->msgr->inst.addr.in_addr));
  1711. }
  1712. return 0;
  1713. }
  1714. static int process_connect(struct ceph_connection *con)
  1715. {
  1716. u64 sup_feat = from_msgr(con->msgr)->supported_features;
  1717. u64 req_feat = from_msgr(con->msgr)->required_features;
  1718. u64 server_feat = le64_to_cpu(con->in_reply.features);
  1719. int ret;
  1720. dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
  1721. if (con->auth) {
  1722. int len = le32_to_cpu(con->in_reply.authorizer_len);
  1723. /*
  1724. * Any connection that defines ->get_authorizer()
  1725. * should also define ->add_authorizer_challenge() and
  1726. * ->verify_authorizer_reply().
  1727. *
  1728. * See get_connect_authorizer().
  1729. */
  1730. if (con->in_reply.tag == CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
  1731. ret = con->ops->add_authorizer_challenge(
  1732. con, con->auth->authorizer_reply_buf, len);
  1733. if (ret < 0)
  1734. return ret;
  1735. con_out_kvec_reset(con);
  1736. __prepare_write_connect(con);
  1737. prepare_read_connect(con);
  1738. return 0;
  1739. }
  1740. if (len) {
  1741. ret = con->ops->verify_authorizer_reply(con);
  1742. if (ret < 0) {
  1743. con->error_msg = "bad authorize reply";
  1744. return ret;
  1745. }
  1746. }
  1747. }
  1748. switch (con->in_reply.tag) {
  1749. case CEPH_MSGR_TAG_FEATURES:
  1750. pr_err("%s%lld %s feature set mismatch,"
  1751. " my %llx < server's %llx, missing %llx\n",
  1752. ENTITY_NAME(con->peer_name),
  1753. ceph_pr_addr(&con->peer_addr.in_addr),
  1754. sup_feat, server_feat, server_feat & ~sup_feat);
  1755. con->error_msg = "missing required protocol features";
  1756. reset_connection(con);
  1757. return -1;
  1758. case CEPH_MSGR_TAG_BADPROTOVER:
  1759. pr_err("%s%lld %s protocol version mismatch,"
  1760. " my %d != server's %d\n",
  1761. ENTITY_NAME(con->peer_name),
  1762. ceph_pr_addr(&con->peer_addr.in_addr),
  1763. le32_to_cpu(con->out_connect.protocol_version),
  1764. le32_to_cpu(con->in_reply.protocol_version));
  1765. con->error_msg = "protocol version mismatch";
  1766. reset_connection(con);
  1767. return -1;
  1768. case CEPH_MSGR_TAG_BADAUTHORIZER:
  1769. con->auth_retry++;
  1770. dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
  1771. con->auth_retry);
  1772. if (con->auth_retry == 2) {
  1773. con->error_msg = "connect authorization failure";
  1774. return -1;
  1775. }
  1776. con_out_kvec_reset(con);
  1777. ret = prepare_write_connect(con);
  1778. if (ret < 0)
  1779. return ret;
  1780. prepare_read_connect(con);
  1781. break;
  1782. case CEPH_MSGR_TAG_RESETSESSION:
  1783. /*
  1784. * If we connected with a large connect_seq but the peer
  1785. * has no record of a session with us (no connection, or
  1786. * connect_seq == 0), they will send RESETSESION to indicate
  1787. * that they must have reset their session, and may have
  1788. * dropped messages.
  1789. */
  1790. dout("process_connect got RESET peer seq %u\n",
  1791. le32_to_cpu(con->in_reply.connect_seq));
  1792. pr_err("%s%lld %s connection reset\n",
  1793. ENTITY_NAME(con->peer_name),
  1794. ceph_pr_addr(&con->peer_addr.in_addr));
  1795. reset_connection(con);
  1796. con_out_kvec_reset(con);
  1797. ret = prepare_write_connect(con);
  1798. if (ret < 0)
  1799. return ret;
  1800. prepare_read_connect(con);
  1801. /* Tell ceph about it. */
  1802. mutex_unlock(&con->mutex);
  1803. pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
  1804. if (con->ops->peer_reset)
  1805. con->ops->peer_reset(con);
  1806. mutex_lock(&con->mutex);
  1807. if (con->state != CON_STATE_NEGOTIATING)
  1808. return -EAGAIN;
  1809. break;
  1810. case CEPH_MSGR_TAG_RETRY_SESSION:
  1811. /*
  1812. * If we sent a smaller connect_seq than the peer has, try
  1813. * again with a larger value.
  1814. */
  1815. dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
  1816. le32_to_cpu(con->out_connect.connect_seq),
  1817. le32_to_cpu(con->in_reply.connect_seq));
  1818. con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
  1819. con_out_kvec_reset(con);
  1820. ret = prepare_write_connect(con);
  1821. if (ret < 0)
  1822. return ret;
  1823. prepare_read_connect(con);
  1824. break;
  1825. case CEPH_MSGR_TAG_RETRY_GLOBAL:
  1826. /*
  1827. * If we sent a smaller global_seq than the peer has, try
  1828. * again with a larger value.
  1829. */
  1830. dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
  1831. con->peer_global_seq,
  1832. le32_to_cpu(con->in_reply.global_seq));
  1833. get_global_seq(con->msgr,
  1834. le32_to_cpu(con->in_reply.global_seq));
  1835. con_out_kvec_reset(con);
  1836. ret = prepare_write_connect(con);
  1837. if (ret < 0)
  1838. return ret;
  1839. prepare_read_connect(con);
  1840. break;
  1841. case CEPH_MSGR_TAG_SEQ:
  1842. case CEPH_MSGR_TAG_READY:
  1843. if (req_feat & ~server_feat) {
  1844. pr_err("%s%lld %s protocol feature mismatch,"
  1845. " my required %llx > server's %llx, need %llx\n",
  1846. ENTITY_NAME(con->peer_name),
  1847. ceph_pr_addr(&con->peer_addr.in_addr),
  1848. req_feat, server_feat, req_feat & ~server_feat);
  1849. con->error_msg = "missing required protocol features";
  1850. reset_connection(con);
  1851. return -1;
  1852. }
  1853. WARN_ON(con->state != CON_STATE_NEGOTIATING);
  1854. con->state = CON_STATE_OPEN;
  1855. con->auth_retry = 0; /* we authenticated; clear flag */
  1856. con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
  1857. con->connect_seq++;
  1858. con->peer_features = server_feat;
  1859. dout("process_connect got READY gseq %d cseq %d (%d)\n",
  1860. con->peer_global_seq,
  1861. le32_to_cpu(con->in_reply.connect_seq),
  1862. con->connect_seq);
  1863. WARN_ON(con->connect_seq !=
  1864. le32_to_cpu(con->in_reply.connect_seq));
  1865. if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
  1866. con_flag_set(con, CON_FLAG_LOSSYTX);
  1867. con->delay = 0; /* reset backoff memory */
  1868. if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
  1869. prepare_write_seq(con);
  1870. prepare_read_seq(con);
  1871. } else {
  1872. prepare_read_tag(con);
  1873. }
  1874. break;
  1875. case CEPH_MSGR_TAG_WAIT:
  1876. /*
  1877. * If there is a connection race (we are opening
  1878. * connections to each other), one of us may just have
  1879. * to WAIT. This shouldn't happen if we are the
  1880. * client.
  1881. */
  1882. con->error_msg = "protocol error, got WAIT as client";
  1883. return -1;
  1884. default:
  1885. con->error_msg = "protocol error, garbage tag during connect";
  1886. return -1;
  1887. }
  1888. return 0;
  1889. }
  1890. /*
  1891. * read (part of) an ack
  1892. */
  1893. static int read_partial_ack(struct ceph_connection *con)
  1894. {
  1895. int size = sizeof (con->in_temp_ack);
  1896. int end = size;
  1897. return read_partial(con, end, size, &con->in_temp_ack);
  1898. }
  1899. /*
  1900. * We can finally discard anything that's been acked.
  1901. */
  1902. static void process_ack(struct ceph_connection *con)
  1903. {
  1904. struct ceph_msg *m;
  1905. u64 ack = le64_to_cpu(con->in_temp_ack);
  1906. u64 seq;
  1907. bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ);
  1908. struct list_head *list = reconnect ? &con->out_queue : &con->out_sent;
  1909. /*
  1910. * In the reconnect case, con_fault() has requeued messages
  1911. * in out_sent. We should cleanup old messages according to
  1912. * the reconnect seq.
  1913. */
  1914. while (!list_empty(list)) {
  1915. m = list_first_entry(list, struct ceph_msg, list_head);
  1916. if (reconnect && m->needs_out_seq)
  1917. break;
  1918. seq = le64_to_cpu(m->hdr.seq);
  1919. if (seq > ack)
  1920. break;
  1921. dout("got ack for seq %llu type %d at %p\n", seq,
  1922. le16_to_cpu(m->hdr.type), m);
  1923. m->ack_stamp = jiffies;
  1924. ceph_msg_remove(m);
  1925. }
  1926. prepare_read_tag(con);
  1927. }
  1928. static int read_partial_message_section(struct ceph_connection *con,
  1929. struct kvec *section,
  1930. unsigned int sec_len, u32 *crc)
  1931. {
  1932. int ret, left;
  1933. BUG_ON(!section);
  1934. while (section->iov_len < sec_len) {
  1935. BUG_ON(section->iov_base == NULL);
  1936. left = sec_len - section->iov_len;
  1937. ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
  1938. section->iov_len, left);
  1939. if (ret <= 0)
  1940. return ret;
  1941. section->iov_len += ret;
  1942. }
  1943. if (section->iov_len == sec_len)
  1944. *crc = crc32c(0, section->iov_base, section->iov_len);
  1945. return 1;
  1946. }
  1947. static int read_partial_msg_data(struct ceph_connection *con)
  1948. {
  1949. struct ceph_msg *msg = con->in_msg;
  1950. struct ceph_msg_data_cursor *cursor = &msg->cursor;
  1951. bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
  1952. struct page *page;
  1953. size_t page_offset;
  1954. size_t length;
  1955. u32 crc = 0;
  1956. int ret;
  1957. BUG_ON(!msg);
  1958. if (list_empty(&msg->data))
  1959. return -EIO;
  1960. if (do_datacrc)
  1961. crc = con->in_data_crc;
  1962. while (cursor->resid) {
  1963. page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
  1964. ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
  1965. if (ret <= 0) {
  1966. if (do_datacrc)
  1967. con->in_data_crc = crc;
  1968. return ret;
  1969. }
  1970. if (do_datacrc)
  1971. crc = ceph_crc32c_page(crc, page, page_offset, ret);
  1972. ceph_msg_data_advance(cursor, (size_t)ret);
  1973. }
  1974. if (do_datacrc)
  1975. con->in_data_crc = crc;
  1976. return 1; /* must return > 0 to indicate success */
  1977. }
  1978. /*
  1979. * read (part of) a message.
  1980. */
  1981. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
  1982. static int read_partial_message(struct ceph_connection *con)
  1983. {
  1984. struct ceph_msg *m = con->in_msg;
  1985. int size;
  1986. int end;
  1987. int ret;
  1988. unsigned int front_len, middle_len, data_len;
  1989. bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
  1990. bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
  1991. u64 seq;
  1992. u32 crc;
  1993. dout("read_partial_message con %p msg %p\n", con, m);
  1994. /* header */
  1995. size = sizeof (con->in_hdr);
  1996. end = size;
  1997. ret = read_partial(con, end, size, &con->in_hdr);
  1998. if (ret <= 0)
  1999. return ret;
  2000. crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
  2001. if (cpu_to_le32(crc) != con->in_hdr.crc) {
  2002. pr_err("read_partial_message bad hdr crc %u != expected %u\n",
  2003. crc, con->in_hdr.crc);
  2004. return -EBADMSG;
  2005. }
  2006. front_len = le32_to_cpu(con->in_hdr.front_len);
  2007. if (front_len > CEPH_MSG_MAX_FRONT_LEN)
  2008. return -EIO;
  2009. middle_len = le32_to_cpu(con->in_hdr.middle_len);
  2010. if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
  2011. return -EIO;
  2012. data_len = le32_to_cpu(con->in_hdr.data_len);
  2013. if (data_len > CEPH_MSG_MAX_DATA_LEN)
  2014. return -EIO;
  2015. /* verify seq# */
  2016. seq = le64_to_cpu(con->in_hdr.seq);
  2017. if ((s64)seq - (s64)con->in_seq < 1) {
  2018. pr_info("skipping %s%lld %s seq %lld expected %lld\n",
  2019. ENTITY_NAME(con->peer_name),
  2020. ceph_pr_addr(&con->peer_addr.in_addr),
  2021. seq, con->in_seq + 1);
  2022. con->in_base_pos = -front_len - middle_len - data_len -
  2023. sizeof_footer(con);
  2024. con->in_tag = CEPH_MSGR_TAG_READY;
  2025. return 1;
  2026. } else if ((s64)seq - (s64)con->in_seq > 1) {
  2027. pr_err("read_partial_message bad seq %lld expected %lld\n",
  2028. seq, con->in_seq + 1);
  2029. con->error_msg = "bad message sequence # for incoming message";
  2030. return -EBADE;
  2031. }
  2032. /* allocate message? */
  2033. if (!con->in_msg) {
  2034. int skip = 0;
  2035. dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
  2036. front_len, data_len);
  2037. ret = ceph_con_in_msg_alloc(con, &skip);
  2038. if (ret < 0)
  2039. return ret;
  2040. BUG_ON(!con->in_msg ^ skip);
  2041. if (skip) {
  2042. /* skip this message */
  2043. dout("alloc_msg said skip message\n");
  2044. con->in_base_pos = -front_len - middle_len - data_len -
  2045. sizeof_footer(con);
  2046. con->in_tag = CEPH_MSGR_TAG_READY;
  2047. con->in_seq++;
  2048. return 1;
  2049. }
  2050. BUG_ON(!con->in_msg);
  2051. BUG_ON(con->in_msg->con != con);
  2052. m = con->in_msg;
  2053. m->front.iov_len = 0; /* haven't read it yet */
  2054. if (m->middle)
  2055. m->middle->vec.iov_len = 0;
  2056. /* prepare for data payload, if any */
  2057. if (data_len)
  2058. prepare_message_data(con->in_msg, data_len);
  2059. }
  2060. /* front */
  2061. ret = read_partial_message_section(con, &m->front, front_len,
  2062. &con->in_front_crc);
  2063. if (ret <= 0)
  2064. return ret;
  2065. /* middle */
  2066. if (m->middle) {
  2067. ret = read_partial_message_section(con, &m->middle->vec,
  2068. middle_len,
  2069. &con->in_middle_crc);
  2070. if (ret <= 0)
  2071. return ret;
  2072. }
  2073. /* (page) data */
  2074. if (data_len) {
  2075. ret = read_partial_msg_data(con);
  2076. if (ret <= 0)
  2077. return ret;
  2078. }
  2079. /* footer */
  2080. size = sizeof_footer(con);
  2081. end += size;
  2082. ret = read_partial(con, end, size, &m->footer);
  2083. if (ret <= 0)
  2084. return ret;
  2085. if (!need_sign) {
  2086. m->footer.flags = m->old_footer.flags;
  2087. m->footer.sig = 0;
  2088. }
  2089. dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
  2090. m, front_len, m->footer.front_crc, middle_len,
  2091. m->footer.middle_crc, data_len, m->footer.data_crc);
  2092. /* crc ok? */
  2093. if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
  2094. pr_err("read_partial_message %p front crc %u != exp. %u\n",
  2095. m, con->in_front_crc, m->footer.front_crc);
  2096. return -EBADMSG;
  2097. }
  2098. if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
  2099. pr_err("read_partial_message %p middle crc %u != exp %u\n",
  2100. m, con->in_middle_crc, m->footer.middle_crc);
  2101. return -EBADMSG;
  2102. }
  2103. if (do_datacrc &&
  2104. (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
  2105. con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
  2106. pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
  2107. con->in_data_crc, le32_to_cpu(m->footer.data_crc));
  2108. return -EBADMSG;
  2109. }
  2110. if (need_sign && con->ops->check_message_signature &&
  2111. con->ops->check_message_signature(m)) {
  2112. pr_err("read_partial_message %p signature check failed\n", m);
  2113. return -EBADMSG;
  2114. }
  2115. return 1; /* done! */
  2116. }
  2117. /*
  2118. * Process message. This happens in the worker thread. The callback should
  2119. * be careful not to do anything that waits on other incoming messages or it
  2120. * may deadlock.
  2121. */
  2122. static void process_message(struct ceph_connection *con)
  2123. {
  2124. struct ceph_msg *msg = con->in_msg;
  2125. BUG_ON(con->in_msg->con != con);
  2126. con->in_msg = NULL;
  2127. /* if first message, set peer_name */
  2128. if (con->peer_name.type == 0)
  2129. con->peer_name = msg->hdr.src;
  2130. con->in_seq++;
  2131. mutex_unlock(&con->mutex);
  2132. dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
  2133. msg, le64_to_cpu(msg->hdr.seq),
  2134. ENTITY_NAME(msg->hdr.src),
  2135. le16_to_cpu(msg->hdr.type),
  2136. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  2137. le32_to_cpu(msg->hdr.front_len),
  2138. le32_to_cpu(msg->hdr.data_len),
  2139. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  2140. con->ops->dispatch(con, msg);
  2141. mutex_lock(&con->mutex);
  2142. }
  2143. static int read_keepalive_ack(struct ceph_connection *con)
  2144. {
  2145. struct ceph_timespec ceph_ts;
  2146. size_t size = sizeof(ceph_ts);
  2147. int ret = read_partial(con, size, size, &ceph_ts);
  2148. if (ret <= 0)
  2149. return ret;
  2150. ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
  2151. prepare_read_tag(con);
  2152. return 1;
  2153. }
  2154. /*
  2155. * Write something to the socket. Called in a worker thread when the
  2156. * socket appears to be writeable and we have something ready to send.
  2157. */
  2158. static int try_write(struct ceph_connection *con)
  2159. {
  2160. int ret = 1;
  2161. dout("try_write start %p state %lu\n", con, con->state);
  2162. if (con->state != CON_STATE_PREOPEN &&
  2163. con->state != CON_STATE_CONNECTING &&
  2164. con->state != CON_STATE_NEGOTIATING &&
  2165. con->state != CON_STATE_OPEN)
  2166. return 0;
  2167. more:
  2168. dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
  2169. /* open the socket first? */
  2170. if (con->state == CON_STATE_PREOPEN) {
  2171. BUG_ON(con->sock);
  2172. con->state = CON_STATE_CONNECTING;
  2173. con_out_kvec_reset(con);
  2174. prepare_write_banner(con);
  2175. prepare_read_banner(con);
  2176. BUG_ON(con->in_msg);
  2177. con->in_tag = CEPH_MSGR_TAG_READY;
  2178. dout("try_write initiating connect on %p new state %lu\n",
  2179. con, con->state);
  2180. ret = ceph_tcp_connect(con);
  2181. if (ret < 0) {
  2182. con->error_msg = "connect error";
  2183. goto out;
  2184. }
  2185. }
  2186. more_kvec:
  2187. BUG_ON(!con->sock);
  2188. /* kvec data queued? */
  2189. if (con->out_kvec_left) {
  2190. ret = write_partial_kvec(con);
  2191. if (ret <= 0)
  2192. goto out;
  2193. }
  2194. if (con->out_skip) {
  2195. ret = write_partial_skip(con);
  2196. if (ret <= 0)
  2197. goto out;
  2198. }
  2199. /* msg pages? */
  2200. if (con->out_msg) {
  2201. if (con->out_msg_done) {
  2202. ceph_msg_put(con->out_msg);
  2203. con->out_msg = NULL; /* we're done with this one */
  2204. goto do_next;
  2205. }
  2206. ret = write_partial_message_data(con);
  2207. if (ret == 1)
  2208. goto more_kvec; /* we need to send the footer, too! */
  2209. if (ret == 0)
  2210. goto out;
  2211. if (ret < 0) {
  2212. dout("try_write write_partial_message_data err %d\n",
  2213. ret);
  2214. goto out;
  2215. }
  2216. }
  2217. do_next:
  2218. if (con->state == CON_STATE_OPEN) {
  2219. if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2220. prepare_write_keepalive(con);
  2221. goto more;
  2222. }
  2223. /* is anything else pending? */
  2224. if (!list_empty(&con->out_queue)) {
  2225. prepare_write_message(con);
  2226. goto more;
  2227. }
  2228. if (con->in_seq > con->in_seq_acked) {
  2229. prepare_write_ack(con);
  2230. goto more;
  2231. }
  2232. }
  2233. /* Nothing to do! */
  2234. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2235. dout("try_write nothing else to write.\n");
  2236. ret = 0;
  2237. out:
  2238. dout("try_write done on %p ret %d\n", con, ret);
  2239. return ret;
  2240. }
  2241. /*
  2242. * Read what we can from the socket.
  2243. */
  2244. static int try_read(struct ceph_connection *con)
  2245. {
  2246. int ret = -1;
  2247. more:
  2248. dout("try_read start on %p state %lu\n", con, con->state);
  2249. if (con->state != CON_STATE_CONNECTING &&
  2250. con->state != CON_STATE_NEGOTIATING &&
  2251. con->state != CON_STATE_OPEN)
  2252. return 0;
  2253. BUG_ON(!con->sock);
  2254. dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
  2255. con->in_base_pos);
  2256. if (con->state == CON_STATE_CONNECTING) {
  2257. dout("try_read connecting\n");
  2258. ret = read_partial_banner(con);
  2259. if (ret <= 0)
  2260. goto out;
  2261. ret = process_banner(con);
  2262. if (ret < 0)
  2263. goto out;
  2264. con->state = CON_STATE_NEGOTIATING;
  2265. /*
  2266. * Received banner is good, exchange connection info.
  2267. * Do not reset out_kvec, as sending our banner raced
  2268. * with receiving peer banner after connect completed.
  2269. */
  2270. ret = prepare_write_connect(con);
  2271. if (ret < 0)
  2272. goto out;
  2273. prepare_read_connect(con);
  2274. /* Send connection info before awaiting response */
  2275. goto out;
  2276. }
  2277. if (con->state == CON_STATE_NEGOTIATING) {
  2278. dout("try_read negotiating\n");
  2279. ret = read_partial_connect(con);
  2280. if (ret <= 0)
  2281. goto out;
  2282. ret = process_connect(con);
  2283. if (ret < 0)
  2284. goto out;
  2285. goto more;
  2286. }
  2287. WARN_ON(con->state != CON_STATE_OPEN);
  2288. if (con->in_base_pos < 0) {
  2289. /*
  2290. * skipping + discarding content.
  2291. *
  2292. * FIXME: there must be a better way to do this!
  2293. */
  2294. static char buf[SKIP_BUF_SIZE];
  2295. int skip = min((int) sizeof (buf), -con->in_base_pos);
  2296. dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
  2297. ret = ceph_tcp_recvmsg(con->sock, buf, skip);
  2298. if (ret <= 0)
  2299. goto out;
  2300. con->in_base_pos += ret;
  2301. if (con->in_base_pos)
  2302. goto more;
  2303. }
  2304. if (con->in_tag == CEPH_MSGR_TAG_READY) {
  2305. /*
  2306. * what's next?
  2307. */
  2308. ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
  2309. if (ret <= 0)
  2310. goto out;
  2311. dout("try_read got tag %d\n", (int)con->in_tag);
  2312. switch (con->in_tag) {
  2313. case CEPH_MSGR_TAG_MSG:
  2314. prepare_read_message(con);
  2315. break;
  2316. case CEPH_MSGR_TAG_ACK:
  2317. prepare_read_ack(con);
  2318. break;
  2319. case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
  2320. prepare_read_keepalive_ack(con);
  2321. break;
  2322. case CEPH_MSGR_TAG_CLOSE:
  2323. con_close_socket(con);
  2324. con->state = CON_STATE_CLOSED;
  2325. goto out;
  2326. default:
  2327. goto bad_tag;
  2328. }
  2329. }
  2330. if (con->in_tag == CEPH_MSGR_TAG_MSG) {
  2331. ret = read_partial_message(con);
  2332. if (ret <= 0) {
  2333. switch (ret) {
  2334. case -EBADMSG:
  2335. con->error_msg = "bad crc/signature";
  2336. /* fall through */
  2337. case -EBADE:
  2338. ret = -EIO;
  2339. break;
  2340. case -EIO:
  2341. con->error_msg = "io error";
  2342. break;
  2343. }
  2344. goto out;
  2345. }
  2346. if (con->in_tag == CEPH_MSGR_TAG_READY)
  2347. goto more;
  2348. process_message(con);
  2349. if (con->state == CON_STATE_OPEN)
  2350. prepare_read_tag(con);
  2351. goto more;
  2352. }
  2353. if (con->in_tag == CEPH_MSGR_TAG_ACK ||
  2354. con->in_tag == CEPH_MSGR_TAG_SEQ) {
  2355. /*
  2356. * the final handshake seq exchange is semantically
  2357. * equivalent to an ACK
  2358. */
  2359. ret = read_partial_ack(con);
  2360. if (ret <= 0)
  2361. goto out;
  2362. process_ack(con);
  2363. goto more;
  2364. }
  2365. if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
  2366. ret = read_keepalive_ack(con);
  2367. if (ret <= 0)
  2368. goto out;
  2369. goto more;
  2370. }
  2371. out:
  2372. dout("try_read done on %p ret %d\n", con, ret);
  2373. return ret;
  2374. bad_tag:
  2375. pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
  2376. con->error_msg = "protocol error, garbage tag";
  2377. ret = -1;
  2378. goto out;
  2379. }
  2380. /*
  2381. * Atomically queue work on a connection after the specified delay.
  2382. * Bump @con reference to avoid races with connection teardown.
  2383. * Returns 0 if work was queued, or an error code otherwise.
  2384. */
  2385. static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
  2386. {
  2387. if (!con->ops->get(con)) {
  2388. dout("%s %p ref count 0\n", __func__, con);
  2389. return -ENOENT;
  2390. }
  2391. if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
  2392. dout("%s %p - already queued\n", __func__, con);
  2393. con->ops->put(con);
  2394. return -EBUSY;
  2395. }
  2396. dout("%s %p %lu\n", __func__, con, delay);
  2397. return 0;
  2398. }
  2399. static void queue_con(struct ceph_connection *con)
  2400. {
  2401. (void) queue_con_delay(con, 0);
  2402. }
  2403. static void cancel_con(struct ceph_connection *con)
  2404. {
  2405. if (cancel_delayed_work(&con->work)) {
  2406. dout("%s %p\n", __func__, con);
  2407. con->ops->put(con);
  2408. }
  2409. }
  2410. static bool con_sock_closed(struct ceph_connection *con)
  2411. {
  2412. if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
  2413. return false;
  2414. #define CASE(x) \
  2415. case CON_STATE_ ## x: \
  2416. con->error_msg = "socket closed (con state " #x ")"; \
  2417. break;
  2418. switch (con->state) {
  2419. CASE(CLOSED);
  2420. CASE(PREOPEN);
  2421. CASE(CONNECTING);
  2422. CASE(NEGOTIATING);
  2423. CASE(OPEN);
  2424. CASE(STANDBY);
  2425. default:
  2426. pr_warn("%s con %p unrecognized state %lu\n",
  2427. __func__, con, con->state);
  2428. con->error_msg = "unrecognized con state";
  2429. BUG();
  2430. break;
  2431. }
  2432. #undef CASE
  2433. return true;
  2434. }
  2435. static bool con_backoff(struct ceph_connection *con)
  2436. {
  2437. int ret;
  2438. if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
  2439. return false;
  2440. ret = queue_con_delay(con, round_jiffies_relative(con->delay));
  2441. if (ret) {
  2442. dout("%s: con %p FAILED to back off %lu\n", __func__,
  2443. con, con->delay);
  2444. BUG_ON(ret == -ENOENT);
  2445. con_flag_set(con, CON_FLAG_BACKOFF);
  2446. }
  2447. return true;
  2448. }
  2449. /* Finish fault handling; con->mutex must *not* be held here */
  2450. static void con_fault_finish(struct ceph_connection *con)
  2451. {
  2452. dout("%s %p\n", __func__, con);
  2453. /*
  2454. * in case we faulted due to authentication, invalidate our
  2455. * current tickets so that we can get new ones.
  2456. */
  2457. if (con->auth_retry) {
  2458. dout("auth_retry %d, invalidating\n", con->auth_retry);
  2459. if (con->ops->invalidate_authorizer)
  2460. con->ops->invalidate_authorizer(con);
  2461. con->auth_retry = 0;
  2462. }
  2463. if (con->ops->fault)
  2464. con->ops->fault(con);
  2465. }
  2466. /*
  2467. * Do some work on a connection. Drop a connection ref when we're done.
  2468. */
  2469. static void ceph_con_workfn(struct work_struct *work)
  2470. {
  2471. struct ceph_connection *con = container_of(work, struct ceph_connection,
  2472. work.work);
  2473. bool fault;
  2474. mutex_lock(&con->mutex);
  2475. while (true) {
  2476. int ret;
  2477. if ((fault = con_sock_closed(con))) {
  2478. dout("%s: con %p SOCK_CLOSED\n", __func__, con);
  2479. break;
  2480. }
  2481. if (con_backoff(con)) {
  2482. dout("%s: con %p BACKOFF\n", __func__, con);
  2483. break;
  2484. }
  2485. if (con->state == CON_STATE_STANDBY) {
  2486. dout("%s: con %p STANDBY\n", __func__, con);
  2487. break;
  2488. }
  2489. if (con->state == CON_STATE_CLOSED) {
  2490. dout("%s: con %p CLOSED\n", __func__, con);
  2491. BUG_ON(con->sock);
  2492. break;
  2493. }
  2494. if (con->state == CON_STATE_PREOPEN) {
  2495. dout("%s: con %p PREOPEN\n", __func__, con);
  2496. BUG_ON(con->sock);
  2497. }
  2498. ret = try_read(con);
  2499. if (ret < 0) {
  2500. if (ret == -EAGAIN)
  2501. continue;
  2502. if (!con->error_msg)
  2503. con->error_msg = "socket error on read";
  2504. fault = true;
  2505. break;
  2506. }
  2507. ret = try_write(con);
  2508. if (ret < 0) {
  2509. if (ret == -EAGAIN)
  2510. continue;
  2511. if (!con->error_msg)
  2512. con->error_msg = "socket error on write";
  2513. fault = true;
  2514. }
  2515. break; /* If we make it to here, we're done */
  2516. }
  2517. if (fault)
  2518. con_fault(con);
  2519. mutex_unlock(&con->mutex);
  2520. if (fault)
  2521. con_fault_finish(con);
  2522. con->ops->put(con);
  2523. }
  2524. /*
  2525. * Generic error/fault handler. A retry mechanism is used with
  2526. * exponential backoff
  2527. */
  2528. static void con_fault(struct ceph_connection *con)
  2529. {
  2530. dout("fault %p state %lu to peer %s\n",
  2531. con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
  2532. pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  2533. ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
  2534. con->error_msg = NULL;
  2535. WARN_ON(con->state != CON_STATE_CONNECTING &&
  2536. con->state != CON_STATE_NEGOTIATING &&
  2537. con->state != CON_STATE_OPEN);
  2538. con_close_socket(con);
  2539. if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
  2540. dout("fault on LOSSYTX channel, marking CLOSED\n");
  2541. con->state = CON_STATE_CLOSED;
  2542. return;
  2543. }
  2544. if (con->in_msg) {
  2545. BUG_ON(con->in_msg->con != con);
  2546. ceph_msg_put(con->in_msg);
  2547. con->in_msg = NULL;
  2548. }
  2549. if (con->out_msg) {
  2550. BUG_ON(con->out_msg->con != con);
  2551. ceph_msg_put(con->out_msg);
  2552. con->out_msg = NULL;
  2553. }
  2554. /* Requeue anything that hasn't been acked */
  2555. list_splice_init(&con->out_sent, &con->out_queue);
  2556. /* If there are no messages queued or keepalive pending, place
  2557. * the connection in a STANDBY state */
  2558. if (list_empty(&con->out_queue) &&
  2559. !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
  2560. dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
  2561. con_flag_clear(con, CON_FLAG_WRITE_PENDING);
  2562. con->state = CON_STATE_STANDBY;
  2563. } else {
  2564. /* retry after a delay. */
  2565. con->state = CON_STATE_PREOPEN;
  2566. if (con->delay == 0)
  2567. con->delay = BASE_DELAY_INTERVAL;
  2568. else if (con->delay < MAX_DELAY_INTERVAL)
  2569. con->delay *= 2;
  2570. con_flag_set(con, CON_FLAG_BACKOFF);
  2571. queue_con(con);
  2572. }
  2573. }
  2574. /*
  2575. * initialize a new messenger instance
  2576. */
  2577. void ceph_messenger_init(struct ceph_messenger *msgr,
  2578. struct ceph_entity_addr *myaddr)
  2579. {
  2580. spin_lock_init(&msgr->global_seq_lock);
  2581. if (myaddr)
  2582. msgr->inst.addr = *myaddr;
  2583. /* select a random nonce */
  2584. msgr->inst.addr.type = 0;
  2585. get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
  2586. encode_my_addr(msgr);
  2587. atomic_set(&msgr->stopping, 0);
  2588. write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
  2589. dout("%s %p\n", __func__, msgr);
  2590. }
  2591. EXPORT_SYMBOL(ceph_messenger_init);
  2592. void ceph_messenger_fini(struct ceph_messenger *msgr)
  2593. {
  2594. put_net(read_pnet(&msgr->net));
  2595. }
  2596. EXPORT_SYMBOL(ceph_messenger_fini);
  2597. static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
  2598. {
  2599. if (msg->con)
  2600. msg->con->ops->put(msg->con);
  2601. msg->con = con ? con->ops->get(con) : NULL;
  2602. BUG_ON(msg->con != con);
  2603. }
  2604. static void clear_standby(struct ceph_connection *con)
  2605. {
  2606. /* come back from STANDBY? */
  2607. if (con->state == CON_STATE_STANDBY) {
  2608. dout("clear_standby %p and ++connect_seq\n", con);
  2609. con->state = CON_STATE_PREOPEN;
  2610. con->connect_seq++;
  2611. WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
  2612. WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
  2613. }
  2614. }
  2615. /*
  2616. * Queue up an outgoing message on the given connection.
  2617. */
  2618. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  2619. {
  2620. /* set src+dst */
  2621. msg->hdr.src = con->msgr->inst.name;
  2622. BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
  2623. msg->needs_out_seq = true;
  2624. mutex_lock(&con->mutex);
  2625. if (con->state == CON_STATE_CLOSED) {
  2626. dout("con_send %p closed, dropping %p\n", con, msg);
  2627. ceph_msg_put(msg);
  2628. mutex_unlock(&con->mutex);
  2629. return;
  2630. }
  2631. msg_con_set(msg, con);
  2632. BUG_ON(!list_empty(&msg->list_head));
  2633. list_add_tail(&msg->list_head, &con->out_queue);
  2634. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  2635. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  2636. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  2637. le32_to_cpu(msg->hdr.front_len),
  2638. le32_to_cpu(msg->hdr.middle_len),
  2639. le32_to_cpu(msg->hdr.data_len));
  2640. clear_standby(con);
  2641. mutex_unlock(&con->mutex);
  2642. /* if there wasn't anything waiting to send before, queue
  2643. * new work */
  2644. if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2645. queue_con(con);
  2646. }
  2647. EXPORT_SYMBOL(ceph_con_send);
  2648. /*
  2649. * Revoke a message that was previously queued for send
  2650. */
  2651. void ceph_msg_revoke(struct ceph_msg *msg)
  2652. {
  2653. struct ceph_connection *con = msg->con;
  2654. if (!con) {
  2655. dout("%s msg %p null con\n", __func__, msg);
  2656. return; /* Message not in our possession */
  2657. }
  2658. mutex_lock(&con->mutex);
  2659. if (!list_empty(&msg->list_head)) {
  2660. dout("%s %p msg %p - was on queue\n", __func__, con, msg);
  2661. list_del_init(&msg->list_head);
  2662. msg->hdr.seq = 0;
  2663. ceph_msg_put(msg);
  2664. }
  2665. if (con->out_msg == msg) {
  2666. BUG_ON(con->out_skip);
  2667. /* footer */
  2668. if (con->out_msg_done) {
  2669. con->out_skip += con_out_kvec_skip(con);
  2670. } else {
  2671. BUG_ON(!msg->data_length);
  2672. con->out_skip += sizeof_footer(con);
  2673. }
  2674. /* data, middle, front */
  2675. if (msg->data_length)
  2676. con->out_skip += msg->cursor.total_resid;
  2677. if (msg->middle)
  2678. con->out_skip += con_out_kvec_skip(con);
  2679. con->out_skip += con_out_kvec_skip(con);
  2680. dout("%s %p msg %p - was sending, will write %d skip %d\n",
  2681. __func__, con, msg, con->out_kvec_bytes, con->out_skip);
  2682. msg->hdr.seq = 0;
  2683. con->out_msg = NULL;
  2684. ceph_msg_put(msg);
  2685. }
  2686. mutex_unlock(&con->mutex);
  2687. }
  2688. /*
  2689. * Revoke a message that we may be reading data into
  2690. */
  2691. void ceph_msg_revoke_incoming(struct ceph_msg *msg)
  2692. {
  2693. struct ceph_connection *con = msg->con;
  2694. if (!con) {
  2695. dout("%s msg %p null con\n", __func__, msg);
  2696. return; /* Message not in our possession */
  2697. }
  2698. mutex_lock(&con->mutex);
  2699. if (con->in_msg == msg) {
  2700. unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
  2701. unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
  2702. unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
  2703. /* skip rest of message */
  2704. dout("%s %p msg %p revoked\n", __func__, con, msg);
  2705. con->in_base_pos = con->in_base_pos -
  2706. sizeof(struct ceph_msg_header) -
  2707. front_len -
  2708. middle_len -
  2709. data_len -
  2710. sizeof(struct ceph_msg_footer);
  2711. ceph_msg_put(con->in_msg);
  2712. con->in_msg = NULL;
  2713. con->in_tag = CEPH_MSGR_TAG_READY;
  2714. con->in_seq++;
  2715. } else {
  2716. dout("%s %p in_msg %p msg %p no-op\n",
  2717. __func__, con, con->in_msg, msg);
  2718. }
  2719. mutex_unlock(&con->mutex);
  2720. }
  2721. /*
  2722. * Queue a keepalive byte to ensure the tcp connection is alive.
  2723. */
  2724. void ceph_con_keepalive(struct ceph_connection *con)
  2725. {
  2726. dout("con_keepalive %p\n", con);
  2727. mutex_lock(&con->mutex);
  2728. clear_standby(con);
  2729. con_flag_set(con, CON_FLAG_KEEPALIVE_PENDING);
  2730. mutex_unlock(&con->mutex);
  2731. if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
  2732. queue_con(con);
  2733. }
  2734. EXPORT_SYMBOL(ceph_con_keepalive);
  2735. bool ceph_con_keepalive_expired(struct ceph_connection *con,
  2736. unsigned long interval)
  2737. {
  2738. if (interval > 0 &&
  2739. (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
  2740. struct timespec now;
  2741. struct timespec ts;
  2742. ktime_get_real_ts(&now);
  2743. jiffies_to_timespec(interval, &ts);
  2744. ts = timespec_add(con->last_keepalive_ack, ts);
  2745. return timespec_compare(&now, &ts) >= 0;
  2746. }
  2747. return false;
  2748. }
  2749. static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
  2750. {
  2751. struct ceph_msg_data *data;
  2752. if (WARN_ON(!ceph_msg_data_type_valid(type)))
  2753. return NULL;
  2754. data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
  2755. if (!data)
  2756. return NULL;
  2757. data->type = type;
  2758. INIT_LIST_HEAD(&data->links);
  2759. return data;
  2760. }
  2761. static void ceph_msg_data_destroy(struct ceph_msg_data *data)
  2762. {
  2763. if (!data)
  2764. return;
  2765. WARN_ON(!list_empty(&data->links));
  2766. if (data->type == CEPH_MSG_DATA_PAGELIST)
  2767. ceph_pagelist_release(data->pagelist);
  2768. kmem_cache_free(ceph_msg_data_cache, data);
  2769. }
  2770. void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
  2771. size_t length, size_t alignment)
  2772. {
  2773. struct ceph_msg_data *data;
  2774. BUG_ON(!pages);
  2775. BUG_ON(!length);
  2776. data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
  2777. BUG_ON(!data);
  2778. data->pages = pages;
  2779. data->length = length;
  2780. data->alignment = alignment & ~PAGE_MASK;
  2781. list_add_tail(&data->links, &msg->data);
  2782. msg->data_length += length;
  2783. }
  2784. EXPORT_SYMBOL(ceph_msg_data_add_pages);
  2785. void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
  2786. struct ceph_pagelist *pagelist)
  2787. {
  2788. struct ceph_msg_data *data;
  2789. BUG_ON(!pagelist);
  2790. BUG_ON(!pagelist->length);
  2791. data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
  2792. BUG_ON(!data);
  2793. data->pagelist = pagelist;
  2794. list_add_tail(&data->links, &msg->data);
  2795. msg->data_length += pagelist->length;
  2796. }
  2797. EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
  2798. #ifdef CONFIG_BLOCK
  2799. void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
  2800. size_t length)
  2801. {
  2802. struct ceph_msg_data *data;
  2803. BUG_ON(!bio);
  2804. data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
  2805. BUG_ON(!data);
  2806. data->bio = bio;
  2807. data->bio_length = length;
  2808. list_add_tail(&data->links, &msg->data);
  2809. msg->data_length += length;
  2810. }
  2811. EXPORT_SYMBOL(ceph_msg_data_add_bio);
  2812. #endif /* CONFIG_BLOCK */
  2813. /*
  2814. * construct a new message with given type, size
  2815. * the new msg has a ref count of 1.
  2816. */
  2817. struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
  2818. bool can_fail)
  2819. {
  2820. struct ceph_msg *m;
  2821. m = kmem_cache_zalloc(ceph_msg_cache, flags);
  2822. if (m == NULL)
  2823. goto out;
  2824. m->hdr.type = cpu_to_le16(type);
  2825. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  2826. m->hdr.front_len = cpu_to_le32(front_len);
  2827. INIT_LIST_HEAD(&m->list_head);
  2828. kref_init(&m->kref);
  2829. INIT_LIST_HEAD(&m->data);
  2830. /* front */
  2831. if (front_len) {
  2832. m->front.iov_base = ceph_kvmalloc(front_len, flags);
  2833. if (m->front.iov_base == NULL) {
  2834. dout("ceph_msg_new can't allocate %d bytes\n",
  2835. front_len);
  2836. goto out2;
  2837. }
  2838. } else {
  2839. m->front.iov_base = NULL;
  2840. }
  2841. m->front_alloc_len = m->front.iov_len = front_len;
  2842. dout("ceph_msg_new %p front %d\n", m, front_len);
  2843. return m;
  2844. out2:
  2845. ceph_msg_put(m);
  2846. out:
  2847. if (!can_fail) {
  2848. pr_err("msg_new can't create type %d front %d\n", type,
  2849. front_len);
  2850. WARN_ON(1);
  2851. } else {
  2852. dout("msg_new can't create type %d front %d\n", type,
  2853. front_len);
  2854. }
  2855. return NULL;
  2856. }
  2857. EXPORT_SYMBOL(ceph_msg_new);
  2858. /*
  2859. * Allocate "middle" portion of a message, if it is needed and wasn't
  2860. * allocated by alloc_msg. This allows us to read a small fixed-size
  2861. * per-type header in the front and then gracefully fail (i.e.,
  2862. * propagate the error to the caller based on info in the front) when
  2863. * the middle is too large.
  2864. */
  2865. static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  2866. {
  2867. int type = le16_to_cpu(msg->hdr.type);
  2868. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  2869. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  2870. ceph_msg_type_name(type), middle_len);
  2871. BUG_ON(!middle_len);
  2872. BUG_ON(msg->middle);
  2873. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  2874. if (!msg->middle)
  2875. return -ENOMEM;
  2876. return 0;
  2877. }
  2878. /*
  2879. * Allocate a message for receiving an incoming message on a
  2880. * connection, and save the result in con->in_msg. Uses the
  2881. * connection's private alloc_msg op if available.
  2882. *
  2883. * Returns 0 on success, or a negative error code.
  2884. *
  2885. * On success, if we set *skip = 1:
  2886. * - the next message should be skipped and ignored.
  2887. * - con->in_msg == NULL
  2888. * or if we set *skip = 0:
  2889. * - con->in_msg is non-null.
  2890. * On error (ENOMEM, EAGAIN, ...),
  2891. * - con->in_msg == NULL
  2892. */
  2893. static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
  2894. {
  2895. struct ceph_msg_header *hdr = &con->in_hdr;
  2896. int middle_len = le32_to_cpu(hdr->middle_len);
  2897. struct ceph_msg *msg;
  2898. int ret = 0;
  2899. BUG_ON(con->in_msg != NULL);
  2900. BUG_ON(!con->ops->alloc_msg);
  2901. mutex_unlock(&con->mutex);
  2902. msg = con->ops->alloc_msg(con, hdr, skip);
  2903. mutex_lock(&con->mutex);
  2904. if (con->state != CON_STATE_OPEN) {
  2905. if (msg)
  2906. ceph_msg_put(msg);
  2907. return -EAGAIN;
  2908. }
  2909. if (msg) {
  2910. BUG_ON(*skip);
  2911. msg_con_set(msg, con);
  2912. con->in_msg = msg;
  2913. } else {
  2914. /*
  2915. * Null message pointer means either we should skip
  2916. * this message or we couldn't allocate memory. The
  2917. * former is not an error.
  2918. */
  2919. if (*skip)
  2920. return 0;
  2921. con->error_msg = "error allocating memory for incoming message";
  2922. return -ENOMEM;
  2923. }
  2924. memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
  2925. if (middle_len && !con->in_msg->middle) {
  2926. ret = ceph_alloc_middle(con, con->in_msg);
  2927. if (ret < 0) {
  2928. ceph_msg_put(con->in_msg);
  2929. con->in_msg = NULL;
  2930. }
  2931. }
  2932. return ret;
  2933. }
  2934. /*
  2935. * Free a generically kmalloc'd message.
  2936. */
  2937. static void ceph_msg_free(struct ceph_msg *m)
  2938. {
  2939. dout("%s %p\n", __func__, m);
  2940. kvfree(m->front.iov_base);
  2941. kmem_cache_free(ceph_msg_cache, m);
  2942. }
  2943. static void ceph_msg_release(struct kref *kref)
  2944. {
  2945. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  2946. struct ceph_msg_data *data, *next;
  2947. dout("%s %p\n", __func__, m);
  2948. WARN_ON(!list_empty(&m->list_head));
  2949. msg_con_set(m, NULL);
  2950. /* drop middle, data, if any */
  2951. if (m->middle) {
  2952. ceph_buffer_put(m->middle);
  2953. m->middle = NULL;
  2954. }
  2955. list_for_each_entry_safe(data, next, &m->data, links) {
  2956. list_del_init(&data->links);
  2957. ceph_msg_data_destroy(data);
  2958. }
  2959. m->data_length = 0;
  2960. if (m->pool)
  2961. ceph_msgpool_put(m->pool, m);
  2962. else
  2963. ceph_msg_free(m);
  2964. }
  2965. struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
  2966. {
  2967. dout("%s %p (was %d)\n", __func__, msg,
  2968. kref_read(&msg->kref));
  2969. kref_get(&msg->kref);
  2970. return msg;
  2971. }
  2972. EXPORT_SYMBOL(ceph_msg_get);
  2973. void ceph_msg_put(struct ceph_msg *msg)
  2974. {
  2975. dout("%s %p (was %d)\n", __func__, msg,
  2976. kref_read(&msg->kref));
  2977. kref_put(&msg->kref, ceph_msg_release);
  2978. }
  2979. EXPORT_SYMBOL(ceph_msg_put);
  2980. void ceph_msg_dump(struct ceph_msg *msg)
  2981. {
  2982. pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
  2983. msg->front_alloc_len, msg->data_length);
  2984. print_hex_dump(KERN_DEBUG, "header: ",
  2985. DUMP_PREFIX_OFFSET, 16, 1,
  2986. &msg->hdr, sizeof(msg->hdr), true);
  2987. print_hex_dump(KERN_DEBUG, " front: ",
  2988. DUMP_PREFIX_OFFSET, 16, 1,
  2989. msg->front.iov_base, msg->front.iov_len, true);
  2990. if (msg->middle)
  2991. print_hex_dump(KERN_DEBUG, "middle: ",
  2992. DUMP_PREFIX_OFFSET, 16, 1,
  2993. msg->middle->vec.iov_base,
  2994. msg->middle->vec.iov_len, true);
  2995. print_hex_dump(KERN_DEBUG, "footer: ",
  2996. DUMP_PREFIX_OFFSET, 16, 1,
  2997. &msg->footer, sizeof(msg->footer), true);
  2998. }
  2999. EXPORT_SYMBOL(ceph_msg_dump);