messenger.c 72 KB

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