messenger.c 71 KB

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