messenger.c 88 KB

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