messenger.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. #include "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/socket.h>
  9. #include <linux/string.h>
  10. #include <net/tcp.h>
  11. #include "super.h"
  12. #include "messenger.h"
  13. #include "decode.h"
  14. /*
  15. * Ceph uses the messenger to exchange ceph_msg messages with other
  16. * hosts in the system. The messenger provides ordered and reliable
  17. * delivery. We tolerate TCP disconnects by reconnecting (with
  18. * exponential backoff) in the case of a fault (disconnection, bad
  19. * crc, protocol error). Acks allow sent messages to be discarded by
  20. * the sender.
  21. */
  22. /* static tag bytes (protocol control messages) */
  23. static char tag_msg = CEPH_MSGR_TAG_MSG;
  24. static char tag_ack = CEPH_MSGR_TAG_ACK;
  25. static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
  26. static void queue_con(struct ceph_connection *con);
  27. static void con_work(struct work_struct *);
  28. static void ceph_fault(struct ceph_connection *con);
  29. const char *ceph_name_type_str(int t)
  30. {
  31. switch (t) {
  32. case CEPH_ENTITY_TYPE_MON: return "mon";
  33. case CEPH_ENTITY_TYPE_MDS: return "mds";
  34. case CEPH_ENTITY_TYPE_OSD: return "osd";
  35. case CEPH_ENTITY_TYPE_CLIENT: return "client";
  36. case CEPH_ENTITY_TYPE_ADMIN: return "admin";
  37. default: return "???";
  38. }
  39. }
  40. /*
  41. * nicely render a sockaddr as a string.
  42. */
  43. #define MAX_ADDR_STR 20
  44. static char addr_str[MAX_ADDR_STR][40];
  45. static DEFINE_SPINLOCK(addr_str_lock);
  46. static int last_addr_str;
  47. const char *pr_addr(const struct sockaddr_storage *ss)
  48. {
  49. int i;
  50. char *s;
  51. struct sockaddr_in *in4 = (void *)ss;
  52. unsigned char *quad = (void *)&in4->sin_addr.s_addr;
  53. struct sockaddr_in6 *in6 = (void *)ss;
  54. spin_lock(&addr_str_lock);
  55. i = last_addr_str++;
  56. if (last_addr_str == MAX_ADDR_STR)
  57. last_addr_str = 0;
  58. spin_unlock(&addr_str_lock);
  59. s = addr_str[i];
  60. switch (ss->ss_family) {
  61. case AF_INET:
  62. sprintf(s, "%u.%u.%u.%u:%u",
  63. (unsigned int)quad[0],
  64. (unsigned int)quad[1],
  65. (unsigned int)quad[2],
  66. (unsigned int)quad[3],
  67. (unsigned int)ntohs(in4->sin_port));
  68. break;
  69. case AF_INET6:
  70. sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
  71. in6->sin6_addr.s6_addr16[0],
  72. in6->sin6_addr.s6_addr16[1],
  73. in6->sin6_addr.s6_addr16[2],
  74. in6->sin6_addr.s6_addr16[3],
  75. in6->sin6_addr.s6_addr16[4],
  76. in6->sin6_addr.s6_addr16[5],
  77. in6->sin6_addr.s6_addr16[6],
  78. in6->sin6_addr.s6_addr16[7],
  79. (unsigned int)ntohs(in6->sin6_port));
  80. break;
  81. default:
  82. sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
  83. }
  84. return s;
  85. }
  86. static void encode_my_addr(struct ceph_messenger *msgr)
  87. {
  88. memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
  89. ceph_encode_addr(&msgr->my_enc_addr);
  90. }
  91. /*
  92. * work queue for all reading and writing to/from the socket.
  93. */
  94. struct workqueue_struct *ceph_msgr_wq;
  95. int __init ceph_msgr_init(void)
  96. {
  97. ceph_msgr_wq = create_workqueue("ceph-msgr");
  98. if (IS_ERR(ceph_msgr_wq)) {
  99. int ret = PTR_ERR(ceph_msgr_wq);
  100. pr_err("msgr_init failed to create workqueue: %d\n", ret);
  101. ceph_msgr_wq = NULL;
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. void ceph_msgr_exit(void)
  107. {
  108. destroy_workqueue(ceph_msgr_wq);
  109. }
  110. /*
  111. * socket callback functions
  112. */
  113. /* data available on socket, or listen socket received a connect */
  114. static void ceph_data_ready(struct sock *sk, int count_unused)
  115. {
  116. struct ceph_connection *con =
  117. (struct ceph_connection *)sk->sk_user_data;
  118. if (sk->sk_state != TCP_CLOSE_WAIT) {
  119. dout("ceph_data_ready on %p state = %lu, queueing work\n",
  120. con, con->state);
  121. queue_con(con);
  122. }
  123. }
  124. /* socket has buffer space for writing */
  125. static void ceph_write_space(struct sock *sk)
  126. {
  127. struct ceph_connection *con =
  128. (struct ceph_connection *)sk->sk_user_data;
  129. /* only queue to workqueue if there is data we want to write. */
  130. if (test_bit(WRITE_PENDING, &con->state)) {
  131. dout("ceph_write_space %p queueing write work\n", con);
  132. queue_con(con);
  133. } else {
  134. dout("ceph_write_space %p nothing to write\n", con);
  135. }
  136. /* since we have our own write_space, clear the SOCK_NOSPACE flag */
  137. clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  138. }
  139. /* socket's state has changed */
  140. static void ceph_state_change(struct sock *sk)
  141. {
  142. struct ceph_connection *con =
  143. (struct ceph_connection *)sk->sk_user_data;
  144. dout("ceph_state_change %p state = %lu sk_state = %u\n",
  145. con, con->state, sk->sk_state);
  146. if (test_bit(CLOSED, &con->state))
  147. return;
  148. switch (sk->sk_state) {
  149. case TCP_CLOSE:
  150. dout("ceph_state_change TCP_CLOSE\n");
  151. case TCP_CLOSE_WAIT:
  152. dout("ceph_state_change TCP_CLOSE_WAIT\n");
  153. if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
  154. if (test_bit(CONNECTING, &con->state))
  155. con->error_msg = "connection failed";
  156. else
  157. con->error_msg = "socket closed";
  158. queue_con(con);
  159. }
  160. break;
  161. case TCP_ESTABLISHED:
  162. dout("ceph_state_change TCP_ESTABLISHED\n");
  163. queue_con(con);
  164. break;
  165. }
  166. }
  167. /*
  168. * set up socket callbacks
  169. */
  170. static void set_sock_callbacks(struct socket *sock,
  171. struct ceph_connection *con)
  172. {
  173. struct sock *sk = sock->sk;
  174. sk->sk_user_data = (void *)con;
  175. sk->sk_data_ready = ceph_data_ready;
  176. sk->sk_write_space = ceph_write_space;
  177. sk->sk_state_change = ceph_state_change;
  178. }
  179. /*
  180. * socket helpers
  181. */
  182. /*
  183. * initiate connection to a remote socket.
  184. */
  185. static struct socket *ceph_tcp_connect(struct ceph_connection *con)
  186. {
  187. struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
  188. struct socket *sock;
  189. int ret;
  190. BUG_ON(con->sock);
  191. ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  192. if (ret)
  193. return ERR_PTR(ret);
  194. con->sock = sock;
  195. sock->sk->sk_allocation = GFP_NOFS;
  196. set_sock_callbacks(sock, con);
  197. dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
  198. ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
  199. if (ret == -EINPROGRESS) {
  200. dout("connect %s EINPROGRESS sk_state = %u\n",
  201. pr_addr(&con->peer_addr.in_addr),
  202. sock->sk->sk_state);
  203. ret = 0;
  204. }
  205. if (ret < 0) {
  206. pr_err("connect %s error %d\n",
  207. pr_addr(&con->peer_addr.in_addr), ret);
  208. sock_release(sock);
  209. con->sock = NULL;
  210. con->error_msg = "connect error";
  211. }
  212. if (ret < 0)
  213. return ERR_PTR(ret);
  214. return sock;
  215. }
  216. static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
  217. {
  218. struct kvec iov = {buf, len};
  219. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  220. return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
  221. }
  222. /*
  223. * write something. @more is true if caller will be sending more data
  224. * shortly.
  225. */
  226. static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
  227. size_t kvlen, size_t len, int more)
  228. {
  229. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
  230. if (more)
  231. msg.msg_flags |= MSG_MORE;
  232. else
  233. msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
  234. return kernel_sendmsg(sock, &msg, iov, kvlen, len);
  235. }
  236. /*
  237. * Shutdown/close the socket for the given connection.
  238. */
  239. static int con_close_socket(struct ceph_connection *con)
  240. {
  241. int rc;
  242. dout("con_close_socket on %p sock %p\n", con, con->sock);
  243. if (!con->sock)
  244. return 0;
  245. set_bit(SOCK_CLOSED, &con->state);
  246. rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
  247. sock_release(con->sock);
  248. con->sock = NULL;
  249. clear_bit(SOCK_CLOSED, &con->state);
  250. return rc;
  251. }
  252. /*
  253. * Reset a connection. Discard all incoming and outgoing messages
  254. * and clear *_seq state.
  255. */
  256. static void ceph_msg_remove(struct ceph_msg *msg)
  257. {
  258. list_del_init(&msg->list_head);
  259. ceph_msg_put(msg);
  260. }
  261. static void ceph_msg_remove_list(struct list_head *head)
  262. {
  263. while (!list_empty(head)) {
  264. struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
  265. list_head);
  266. ceph_msg_remove(msg);
  267. }
  268. }
  269. static void reset_connection(struct ceph_connection *con)
  270. {
  271. /* reset connection, out_queue, msg_ and connect_seq */
  272. /* discard existing out_queue and msg_seq */
  273. ceph_msg_remove_list(&con->out_queue);
  274. ceph_msg_remove_list(&con->out_sent);
  275. if (con->in_msg) {
  276. ceph_msg_put(con->in_msg);
  277. con->in_msg = NULL;
  278. }
  279. con->connect_seq = 0;
  280. con->out_seq = 0;
  281. if (con->out_msg) {
  282. ceph_msg_put(con->out_msg);
  283. con->out_msg = NULL;
  284. }
  285. con->in_seq = 0;
  286. }
  287. /*
  288. * mark a peer down. drop any open connections.
  289. */
  290. void ceph_con_close(struct ceph_connection *con)
  291. {
  292. dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
  293. set_bit(CLOSED, &con->state); /* in case there's queued work */
  294. clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
  295. mutex_lock(&con->mutex);
  296. reset_connection(con);
  297. mutex_unlock(&con->mutex);
  298. queue_con(con);
  299. }
  300. /*
  301. * Reopen a closed connection, with a new peer address.
  302. */
  303. void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
  304. {
  305. dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
  306. set_bit(OPENING, &con->state);
  307. clear_bit(CLOSED, &con->state);
  308. memcpy(&con->peer_addr, addr, sizeof(*addr));
  309. con->delay = 0; /* reset backoff memory */
  310. queue_con(con);
  311. }
  312. /*
  313. * generic get/put
  314. */
  315. struct ceph_connection *ceph_con_get(struct ceph_connection *con)
  316. {
  317. dout("con_get %p nref = %d -> %d\n", con,
  318. atomic_read(&con->nref), atomic_read(&con->nref) + 1);
  319. if (atomic_inc_not_zero(&con->nref))
  320. return con;
  321. return NULL;
  322. }
  323. void ceph_con_put(struct ceph_connection *con)
  324. {
  325. dout("con_put %p nref = %d -> %d\n", con,
  326. atomic_read(&con->nref), atomic_read(&con->nref) - 1);
  327. BUG_ON(atomic_read(&con->nref) == 0);
  328. if (atomic_dec_and_test(&con->nref)) {
  329. BUG_ON(con->sock);
  330. kfree(con);
  331. }
  332. }
  333. /*
  334. * initialize a new connection.
  335. */
  336. void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
  337. {
  338. dout("con_init %p\n", con);
  339. memset(con, 0, sizeof(*con));
  340. atomic_set(&con->nref, 1);
  341. con->msgr = msgr;
  342. mutex_init(&con->mutex);
  343. INIT_LIST_HEAD(&con->out_queue);
  344. INIT_LIST_HEAD(&con->out_sent);
  345. INIT_DELAYED_WORK(&con->work, con_work);
  346. }
  347. /*
  348. * We maintain a global counter to order connection attempts. Get
  349. * a unique seq greater than @gt.
  350. */
  351. static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
  352. {
  353. u32 ret;
  354. spin_lock(&msgr->global_seq_lock);
  355. if (msgr->global_seq < gt)
  356. msgr->global_seq = gt;
  357. ret = ++msgr->global_seq;
  358. spin_unlock(&msgr->global_seq_lock);
  359. return ret;
  360. }
  361. /*
  362. * Prepare footer for currently outgoing message, and finish things
  363. * off. Assumes out_kvec* are already valid.. we just add on to the end.
  364. */
  365. static void prepare_write_message_footer(struct ceph_connection *con, int v)
  366. {
  367. struct ceph_msg *m = con->out_msg;
  368. dout("prepare_write_message_footer %p\n", con);
  369. con->out_kvec_is_msg = true;
  370. con->out_kvec[v].iov_base = &m->footer;
  371. con->out_kvec[v].iov_len = sizeof(m->footer);
  372. con->out_kvec_bytes += sizeof(m->footer);
  373. con->out_kvec_left++;
  374. con->out_more = m->more_to_follow;
  375. con->out_msg_done = true;
  376. }
  377. /*
  378. * Prepare headers for the next outgoing message.
  379. */
  380. static void prepare_write_message(struct ceph_connection *con)
  381. {
  382. struct ceph_msg *m;
  383. int v = 0;
  384. con->out_kvec_bytes = 0;
  385. con->out_kvec_is_msg = true;
  386. con->out_msg_done = false;
  387. /* Sneak an ack in there first? If we can get it into the same
  388. * TCP packet that's a good thing. */
  389. if (con->in_seq > con->in_seq_acked) {
  390. con->in_seq_acked = con->in_seq;
  391. con->out_kvec[v].iov_base = &tag_ack;
  392. con->out_kvec[v++].iov_len = 1;
  393. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  394. con->out_kvec[v].iov_base = &con->out_temp_ack;
  395. con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
  396. con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
  397. }
  398. m = list_first_entry(&con->out_queue,
  399. struct ceph_msg, list_head);
  400. con->out_msg = m;
  401. if (test_bit(LOSSYTX, &con->state)) {
  402. /* put message on sent list */
  403. ceph_msg_get(m);
  404. list_move_tail(&m->list_head, &con->out_sent);
  405. } else {
  406. list_del_init(&m->list_head);
  407. }
  408. m->hdr.seq = cpu_to_le64(++con->out_seq);
  409. dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
  410. m, con->out_seq, le16_to_cpu(m->hdr.type),
  411. le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
  412. le32_to_cpu(m->hdr.data_len),
  413. m->nr_pages);
  414. BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
  415. /* tag + hdr + front + middle */
  416. con->out_kvec[v].iov_base = &tag_msg;
  417. con->out_kvec[v++].iov_len = 1;
  418. con->out_kvec[v].iov_base = &m->hdr;
  419. con->out_kvec[v++].iov_len = sizeof(m->hdr);
  420. con->out_kvec[v++] = m->front;
  421. if (m->middle)
  422. con->out_kvec[v++] = m->middle->vec;
  423. con->out_kvec_left = v;
  424. con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
  425. (m->middle ? m->middle->vec.iov_len : 0);
  426. con->out_kvec_cur = con->out_kvec;
  427. /* fill in crc (except data pages), footer */
  428. con->out_msg->hdr.crc =
  429. cpu_to_le32(crc32c(0, (void *)&m->hdr,
  430. sizeof(m->hdr) - sizeof(m->hdr.crc)));
  431. con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
  432. con->out_msg->footer.front_crc =
  433. cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
  434. if (m->middle)
  435. con->out_msg->footer.middle_crc =
  436. cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
  437. m->middle->vec.iov_len));
  438. else
  439. con->out_msg->footer.middle_crc = 0;
  440. con->out_msg->footer.data_crc = 0;
  441. dout("prepare_write_message front_crc %u data_crc %u\n",
  442. le32_to_cpu(con->out_msg->footer.front_crc),
  443. le32_to_cpu(con->out_msg->footer.middle_crc));
  444. /* is there a data payload? */
  445. if (le32_to_cpu(m->hdr.data_len) > 0) {
  446. /* initialize page iterator */
  447. con->out_msg_pos.page = 0;
  448. con->out_msg_pos.page_pos =
  449. le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
  450. con->out_msg_pos.data_pos = 0;
  451. con->out_msg_pos.did_page_crc = 0;
  452. con->out_more = 1; /* data + footer will follow */
  453. } else {
  454. /* no, queue up footer too and be done */
  455. prepare_write_message_footer(con, v);
  456. }
  457. set_bit(WRITE_PENDING, &con->state);
  458. }
  459. /*
  460. * Prepare an ack.
  461. */
  462. static void prepare_write_ack(struct ceph_connection *con)
  463. {
  464. dout("prepare_write_ack %p %llu -> %llu\n", con,
  465. con->in_seq_acked, con->in_seq);
  466. con->in_seq_acked = con->in_seq;
  467. con->out_kvec[0].iov_base = &tag_ack;
  468. con->out_kvec[0].iov_len = 1;
  469. con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
  470. con->out_kvec[1].iov_base = &con->out_temp_ack;
  471. con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
  472. con->out_kvec_left = 2;
  473. con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
  474. con->out_kvec_cur = con->out_kvec;
  475. con->out_more = 1; /* more will follow.. eventually.. */
  476. set_bit(WRITE_PENDING, &con->state);
  477. }
  478. /*
  479. * Prepare to write keepalive byte.
  480. */
  481. static void prepare_write_keepalive(struct ceph_connection *con)
  482. {
  483. dout("prepare_write_keepalive %p\n", con);
  484. con->out_kvec[0].iov_base = &tag_keepalive;
  485. con->out_kvec[0].iov_len = 1;
  486. con->out_kvec_left = 1;
  487. con->out_kvec_bytes = 1;
  488. con->out_kvec_cur = con->out_kvec;
  489. set_bit(WRITE_PENDING, &con->state);
  490. }
  491. /*
  492. * Connection negotiation.
  493. */
  494. static void prepare_connect_authorizer(struct ceph_connection *con)
  495. {
  496. void *auth_buf;
  497. int auth_len = 0;
  498. int auth_protocol = 0;
  499. mutex_unlock(&con->mutex);
  500. if (con->ops->get_authorizer)
  501. con->ops->get_authorizer(con, &auth_buf, &auth_len,
  502. &auth_protocol, &con->auth_reply_buf,
  503. &con->auth_reply_buf_len,
  504. con->auth_retry);
  505. mutex_lock(&con->mutex);
  506. con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
  507. con->out_connect.authorizer_len = cpu_to_le32(auth_len);
  508. con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
  509. con->out_kvec[con->out_kvec_left].iov_len = auth_len;
  510. con->out_kvec_left++;
  511. con->out_kvec_bytes += auth_len;
  512. }
  513. /*
  514. * We connected to a peer and are saying hello.
  515. */
  516. static void prepare_write_banner(struct ceph_messenger *msgr,
  517. struct ceph_connection *con)
  518. {
  519. int len = strlen(CEPH_BANNER);
  520. con->out_kvec[0].iov_base = CEPH_BANNER;
  521. con->out_kvec[0].iov_len = len;
  522. con->out_kvec[1].iov_base = &msgr->my_enc_addr;
  523. con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
  524. con->out_kvec_left = 2;
  525. con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
  526. con->out_kvec_cur = con->out_kvec;
  527. con->out_more = 0;
  528. set_bit(WRITE_PENDING, &con->state);
  529. }
  530. static void prepare_write_connect(struct ceph_messenger *msgr,
  531. struct ceph_connection *con,
  532. int after_banner)
  533. {
  534. unsigned global_seq = get_global_seq(con->msgr, 0);
  535. int proto;
  536. switch (con->peer_name.type) {
  537. case CEPH_ENTITY_TYPE_MON:
  538. proto = CEPH_MONC_PROTOCOL;
  539. break;
  540. case CEPH_ENTITY_TYPE_OSD:
  541. proto = CEPH_OSDC_PROTOCOL;
  542. break;
  543. case CEPH_ENTITY_TYPE_MDS:
  544. proto = CEPH_MDSC_PROTOCOL;
  545. break;
  546. default:
  547. BUG();
  548. }
  549. dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
  550. con->connect_seq, global_seq, proto);
  551. con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
  552. con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
  553. con->out_connect.global_seq = cpu_to_le32(global_seq);
  554. con->out_connect.protocol_version = cpu_to_le32(proto);
  555. con->out_connect.flags = 0;
  556. if (!after_banner) {
  557. con->out_kvec_left = 0;
  558. con->out_kvec_bytes = 0;
  559. }
  560. con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
  561. con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
  562. con->out_kvec_left++;
  563. con->out_kvec_bytes += sizeof(con->out_connect);
  564. con->out_kvec_cur = con->out_kvec;
  565. con->out_more = 0;
  566. set_bit(WRITE_PENDING, &con->state);
  567. prepare_connect_authorizer(con);
  568. }
  569. /*
  570. * write as much of pending kvecs to the socket as we can.
  571. * 1 -> done
  572. * 0 -> socket full, but more to do
  573. * <0 -> error
  574. */
  575. static int write_partial_kvec(struct ceph_connection *con)
  576. {
  577. int ret;
  578. dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
  579. while (con->out_kvec_bytes > 0) {
  580. ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
  581. con->out_kvec_left, con->out_kvec_bytes,
  582. con->out_more);
  583. if (ret <= 0)
  584. goto out;
  585. con->out_kvec_bytes -= ret;
  586. if (con->out_kvec_bytes == 0)
  587. break; /* done */
  588. while (ret > 0) {
  589. if (ret >= con->out_kvec_cur->iov_len) {
  590. ret -= con->out_kvec_cur->iov_len;
  591. con->out_kvec_cur++;
  592. con->out_kvec_left--;
  593. } else {
  594. con->out_kvec_cur->iov_len -= ret;
  595. con->out_kvec_cur->iov_base += ret;
  596. ret = 0;
  597. break;
  598. }
  599. }
  600. }
  601. con->out_kvec_left = 0;
  602. con->out_kvec_is_msg = false;
  603. ret = 1;
  604. out:
  605. dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
  606. con->out_kvec_bytes, con->out_kvec_left, ret);
  607. return ret; /* done! */
  608. }
  609. /*
  610. * Write as much message data payload as we can. If we finish, queue
  611. * up the footer.
  612. * 1 -> done, footer is now queued in out_kvec[].
  613. * 0 -> socket full, but more to do
  614. * <0 -> error
  615. */
  616. static int write_partial_msg_pages(struct ceph_connection *con)
  617. {
  618. struct ceph_msg *msg = con->out_msg;
  619. unsigned data_len = le32_to_cpu(msg->hdr.data_len);
  620. size_t len;
  621. int crc = con->msgr->nocrc;
  622. int ret;
  623. dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
  624. con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
  625. con->out_msg_pos.page_pos);
  626. while (con->out_msg_pos.page < con->out_msg->nr_pages) {
  627. struct page *page = NULL;
  628. void *kaddr = NULL;
  629. /*
  630. * if we are calculating the data crc (the default), we need
  631. * to map the page. if our pages[] has been revoked, use the
  632. * zero page.
  633. */
  634. if (msg->pages) {
  635. page = msg->pages[con->out_msg_pos.page];
  636. if (crc)
  637. kaddr = kmap(page);
  638. } else {
  639. page = con->msgr->zero_page;
  640. if (crc)
  641. kaddr = page_address(con->msgr->zero_page);
  642. }
  643. len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
  644. (int)(data_len - con->out_msg_pos.data_pos));
  645. if (crc && !con->out_msg_pos.did_page_crc) {
  646. void *base = kaddr + con->out_msg_pos.page_pos;
  647. u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
  648. BUG_ON(kaddr == NULL);
  649. con->out_msg->footer.data_crc =
  650. cpu_to_le32(crc32c(tmpcrc, base, len));
  651. con->out_msg_pos.did_page_crc = 1;
  652. }
  653. ret = kernel_sendpage(con->sock, page,
  654. con->out_msg_pos.page_pos, len,
  655. MSG_DONTWAIT | MSG_NOSIGNAL |
  656. MSG_MORE);
  657. if (crc && msg->pages)
  658. kunmap(page);
  659. if (ret <= 0)
  660. goto out;
  661. con->out_msg_pos.data_pos += ret;
  662. con->out_msg_pos.page_pos += ret;
  663. if (ret == len) {
  664. con->out_msg_pos.page_pos = 0;
  665. con->out_msg_pos.page++;
  666. con->out_msg_pos.did_page_crc = 0;
  667. }
  668. }
  669. dout("write_partial_msg_pages %p msg %p done\n", con, msg);
  670. /* prepare and queue up footer, too */
  671. if (!crc)
  672. con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
  673. con->out_kvec_bytes = 0;
  674. con->out_kvec_left = 0;
  675. con->out_kvec_cur = con->out_kvec;
  676. prepare_write_message_footer(con, 0);
  677. ret = 1;
  678. out:
  679. return ret;
  680. }
  681. /*
  682. * write some zeros
  683. */
  684. static int write_partial_skip(struct ceph_connection *con)
  685. {
  686. int ret;
  687. while (con->out_skip > 0) {
  688. struct kvec iov = {
  689. .iov_base = page_address(con->msgr->zero_page),
  690. .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
  691. };
  692. ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
  693. if (ret <= 0)
  694. goto out;
  695. con->out_skip -= ret;
  696. }
  697. ret = 1;
  698. out:
  699. return ret;
  700. }
  701. /*
  702. * Prepare to read connection handshake, or an ack.
  703. */
  704. static void prepare_read_banner(struct ceph_connection *con)
  705. {
  706. dout("prepare_read_banner %p\n", con);
  707. con->in_base_pos = 0;
  708. }
  709. static void prepare_read_connect(struct ceph_connection *con)
  710. {
  711. dout("prepare_read_connect %p\n", con);
  712. con->in_base_pos = 0;
  713. }
  714. static void prepare_read_connect_retry(struct ceph_connection *con)
  715. {
  716. dout("prepare_read_connect_retry %p\n", con);
  717. con->in_base_pos = strlen(CEPH_BANNER) + sizeof(con->actual_peer_addr)
  718. + sizeof(con->peer_addr_for_me);
  719. }
  720. static void prepare_read_ack(struct ceph_connection *con)
  721. {
  722. dout("prepare_read_ack %p\n", con);
  723. con->in_base_pos = 0;
  724. }
  725. static void prepare_read_tag(struct ceph_connection *con)
  726. {
  727. dout("prepare_read_tag %p\n", con);
  728. con->in_base_pos = 0;
  729. con->in_tag = CEPH_MSGR_TAG_READY;
  730. }
  731. /*
  732. * Prepare to read a message.
  733. */
  734. static int prepare_read_message(struct ceph_connection *con)
  735. {
  736. dout("prepare_read_message %p\n", con);
  737. BUG_ON(con->in_msg != NULL);
  738. con->in_base_pos = 0;
  739. con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
  740. return 0;
  741. }
  742. static int read_partial(struct ceph_connection *con,
  743. int *to, int size, void *object)
  744. {
  745. *to += size;
  746. while (con->in_base_pos < *to) {
  747. int left = *to - con->in_base_pos;
  748. int have = size - left;
  749. int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
  750. if (ret <= 0)
  751. return ret;
  752. con->in_base_pos += ret;
  753. }
  754. return 1;
  755. }
  756. /*
  757. * Read all or part of the connect-side handshake on a new connection
  758. */
  759. static int read_partial_banner(struct ceph_connection *con)
  760. {
  761. int ret, to = 0;
  762. dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
  763. /* peer's banner */
  764. ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
  765. if (ret <= 0)
  766. goto out;
  767. ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
  768. &con->actual_peer_addr);
  769. if (ret <= 0)
  770. goto out;
  771. ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
  772. &con->peer_addr_for_me);
  773. if (ret <= 0)
  774. goto out;
  775. out:
  776. return ret;
  777. }
  778. static int read_partial_connect(struct ceph_connection *con)
  779. {
  780. int ret, to = 0;
  781. dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
  782. ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
  783. if (ret <= 0)
  784. goto out;
  785. ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
  786. con->auth_reply_buf);
  787. if (ret <= 0)
  788. goto out;
  789. dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
  790. con, (int)con->in_reply.tag,
  791. le32_to_cpu(con->in_reply.connect_seq),
  792. le32_to_cpu(con->in_reply.global_seq));
  793. out:
  794. return ret;
  795. }
  796. /*
  797. * Verify the hello banner looks okay.
  798. */
  799. static int verify_hello(struct ceph_connection *con)
  800. {
  801. if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
  802. pr_err("connect to %s got bad banner\n",
  803. pr_addr(&con->peer_addr.in_addr));
  804. con->error_msg = "protocol error, bad banner";
  805. return -1;
  806. }
  807. return 0;
  808. }
  809. static bool addr_is_blank(struct sockaddr_storage *ss)
  810. {
  811. switch (ss->ss_family) {
  812. case AF_INET:
  813. return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
  814. case AF_INET6:
  815. return
  816. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
  817. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
  818. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
  819. ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
  820. }
  821. return false;
  822. }
  823. static int addr_port(struct sockaddr_storage *ss)
  824. {
  825. switch (ss->ss_family) {
  826. case AF_INET:
  827. return ntohs(((struct sockaddr_in *)ss)->sin_port);
  828. case AF_INET6:
  829. return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
  830. }
  831. return 0;
  832. }
  833. static void addr_set_port(struct sockaddr_storage *ss, int p)
  834. {
  835. switch (ss->ss_family) {
  836. case AF_INET:
  837. ((struct sockaddr_in *)ss)->sin_port = htons(p);
  838. case AF_INET6:
  839. ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
  840. }
  841. }
  842. /*
  843. * Parse an ip[:port] list into an addr array. Use the default
  844. * monitor port if a port isn't specified.
  845. */
  846. int ceph_parse_ips(const char *c, const char *end,
  847. struct ceph_entity_addr *addr,
  848. int max_count, int *count)
  849. {
  850. int i;
  851. const char *p = c;
  852. dout("parse_ips on '%.*s'\n", (int)(end-c), c);
  853. for (i = 0; i < max_count; i++) {
  854. const char *ipend;
  855. struct sockaddr_storage *ss = &addr[i].in_addr;
  856. struct sockaddr_in *in4 = (void *)ss;
  857. struct sockaddr_in6 *in6 = (void *)ss;
  858. int port;
  859. memset(ss, 0, sizeof(*ss));
  860. if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
  861. ',', &ipend)) {
  862. ss->ss_family = AF_INET;
  863. } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
  864. ',', &ipend)) {
  865. ss->ss_family = AF_INET6;
  866. } else {
  867. goto bad;
  868. }
  869. p = ipend;
  870. /* port? */
  871. if (p < end && *p == ':') {
  872. port = 0;
  873. p++;
  874. while (p < end && *p >= '0' && *p <= '9') {
  875. port = (port * 10) + (*p - '0');
  876. p++;
  877. }
  878. if (port > 65535 || port == 0)
  879. goto bad;
  880. } else {
  881. port = CEPH_MON_PORT;
  882. }
  883. addr_set_port(ss, port);
  884. dout("parse_ips got %s\n", pr_addr(ss));
  885. if (p == end)
  886. break;
  887. if (*p != ',')
  888. goto bad;
  889. p++;
  890. }
  891. if (p != end)
  892. goto bad;
  893. if (count)
  894. *count = i + 1;
  895. return 0;
  896. bad:
  897. pr_err("parse_ips bad ip '%s'\n", c);
  898. return -EINVAL;
  899. }
  900. static int process_banner(struct ceph_connection *con)
  901. {
  902. dout("process_banner on %p\n", con);
  903. if (verify_hello(con) < 0)
  904. return -1;
  905. ceph_decode_addr(&con->actual_peer_addr);
  906. ceph_decode_addr(&con->peer_addr_for_me);
  907. /*
  908. * Make sure the other end is who we wanted. note that the other
  909. * end may not yet know their ip address, so if it's 0.0.0.0, give
  910. * them the benefit of the doubt.
  911. */
  912. if (!ceph_entity_addr_is_local(&con->peer_addr,
  913. &con->actual_peer_addr) &&
  914. !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
  915. con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
  916. pr_err("wrong peer, want %s/%d, "
  917. "got %s/%d, wtf\n",
  918. pr_addr(&con->peer_addr.in_addr),
  919. con->peer_addr.nonce,
  920. pr_addr(&con->actual_peer_addr.in_addr),
  921. con->actual_peer_addr.nonce);
  922. con->error_msg = "protocol error, wrong peer";
  923. return -1;
  924. }
  925. /*
  926. * did we learn our address?
  927. */
  928. if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
  929. int port = addr_port(&con->msgr->inst.addr.in_addr);
  930. memcpy(&con->msgr->inst.addr.in_addr,
  931. &con->peer_addr_for_me.in_addr,
  932. sizeof(con->peer_addr_for_me.in_addr));
  933. addr_set_port(&con->msgr->inst.addr.in_addr, port);
  934. encode_my_addr(con->msgr);
  935. dout("process_banner learned my addr is %s\n",
  936. pr_addr(&con->msgr->inst.addr.in_addr));
  937. }
  938. set_bit(NEGOTIATING, &con->state);
  939. prepare_read_connect(con);
  940. return 0;
  941. }
  942. static int process_connect(struct ceph_connection *con)
  943. {
  944. dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
  945. switch (con->in_reply.tag) {
  946. case CEPH_MSGR_TAG_BADPROTOVER:
  947. dout("process_connect got BADPROTOVER my %d != their %d\n",
  948. le32_to_cpu(con->out_connect.protocol_version),
  949. le32_to_cpu(con->in_reply.protocol_version));
  950. pr_err("%s%lld %s protocol version mismatch,"
  951. " my %d != server's %d\n",
  952. ENTITY_NAME(con->peer_name),
  953. pr_addr(&con->peer_addr.in_addr),
  954. le32_to_cpu(con->out_connect.protocol_version),
  955. le32_to_cpu(con->in_reply.protocol_version));
  956. con->error_msg = "protocol version mismatch";
  957. reset_connection(con);
  958. set_bit(CLOSED, &con->state); /* in case there's queued work */
  959. mutex_unlock(&con->mutex);
  960. if (con->ops->bad_proto)
  961. con->ops->bad_proto(con);
  962. mutex_lock(&con->mutex);
  963. return -1;
  964. case CEPH_MSGR_TAG_BADAUTHORIZER:
  965. con->auth_retry++;
  966. dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
  967. con->auth_retry);
  968. if (con->auth_retry == 2) {
  969. con->error_msg = "connect authorization failure";
  970. reset_connection(con);
  971. set_bit(CLOSED, &con->state);
  972. return -1;
  973. }
  974. con->auth_retry = 1;
  975. prepare_write_connect(con->msgr, con, 0);
  976. prepare_read_connect_retry(con);
  977. break;
  978. case CEPH_MSGR_TAG_RESETSESSION:
  979. /*
  980. * If we connected with a large connect_seq but the peer
  981. * has no record of a session with us (no connection, or
  982. * connect_seq == 0), they will send RESETSESION to indicate
  983. * that they must have reset their session, and may have
  984. * dropped messages.
  985. */
  986. dout("process_connect got RESET peer seq %u\n",
  987. le32_to_cpu(con->in_connect.connect_seq));
  988. pr_err("%s%lld %s connection reset\n",
  989. ENTITY_NAME(con->peer_name),
  990. pr_addr(&con->peer_addr.in_addr));
  991. reset_connection(con);
  992. prepare_write_connect(con->msgr, con, 0);
  993. prepare_read_connect(con);
  994. /* Tell ceph about it. */
  995. mutex_unlock(&con->mutex);
  996. pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
  997. if (con->ops->peer_reset)
  998. con->ops->peer_reset(con);
  999. mutex_lock(&con->mutex);
  1000. break;
  1001. case CEPH_MSGR_TAG_RETRY_SESSION:
  1002. /*
  1003. * If we sent a smaller connect_seq than the peer has, try
  1004. * again with a larger value.
  1005. */
  1006. dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
  1007. le32_to_cpu(con->out_connect.connect_seq),
  1008. le32_to_cpu(con->in_connect.connect_seq));
  1009. con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
  1010. prepare_write_connect(con->msgr, con, 0);
  1011. prepare_read_connect(con);
  1012. break;
  1013. case CEPH_MSGR_TAG_RETRY_GLOBAL:
  1014. /*
  1015. * If we sent a smaller global_seq than the peer has, try
  1016. * again with a larger value.
  1017. */
  1018. dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
  1019. con->peer_global_seq,
  1020. le32_to_cpu(con->in_connect.global_seq));
  1021. get_global_seq(con->msgr,
  1022. le32_to_cpu(con->in_connect.global_seq));
  1023. prepare_write_connect(con->msgr, con, 0);
  1024. prepare_read_connect(con);
  1025. break;
  1026. case CEPH_MSGR_TAG_READY:
  1027. clear_bit(CONNECTING, &con->state);
  1028. con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
  1029. con->connect_seq++;
  1030. dout("process_connect got READY gseq %d cseq %d (%d)\n",
  1031. con->peer_global_seq,
  1032. le32_to_cpu(con->in_reply.connect_seq),
  1033. con->connect_seq);
  1034. WARN_ON(con->connect_seq !=
  1035. le32_to_cpu(con->in_reply.connect_seq));
  1036. if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
  1037. set_bit(LOSSYTX, &con->state);
  1038. prepare_read_tag(con);
  1039. break;
  1040. case CEPH_MSGR_TAG_WAIT:
  1041. /*
  1042. * If there is a connection race (we are opening
  1043. * connections to each other), one of us may just have
  1044. * to WAIT. This shouldn't happen if we are the
  1045. * client.
  1046. */
  1047. pr_err("process_connect peer connecting WAIT\n");
  1048. default:
  1049. pr_err("connect protocol error, will retry\n");
  1050. con->error_msg = "protocol error, garbage tag during connect";
  1051. return -1;
  1052. }
  1053. return 0;
  1054. }
  1055. /*
  1056. * read (part of) an ack
  1057. */
  1058. static int read_partial_ack(struct ceph_connection *con)
  1059. {
  1060. int to = 0;
  1061. return read_partial(con, &to, sizeof(con->in_temp_ack),
  1062. &con->in_temp_ack);
  1063. }
  1064. /*
  1065. * We can finally discard anything that's been acked.
  1066. */
  1067. static void process_ack(struct ceph_connection *con)
  1068. {
  1069. struct ceph_msg *m;
  1070. u64 ack = le64_to_cpu(con->in_temp_ack);
  1071. u64 seq;
  1072. while (!list_empty(&con->out_sent)) {
  1073. m = list_first_entry(&con->out_sent, struct ceph_msg,
  1074. list_head);
  1075. seq = le64_to_cpu(m->hdr.seq);
  1076. if (seq > ack)
  1077. break;
  1078. dout("got ack for seq %llu type %d at %p\n", seq,
  1079. le16_to_cpu(m->hdr.type), m);
  1080. ceph_msg_remove(m);
  1081. }
  1082. prepare_read_tag(con);
  1083. }
  1084. /*
  1085. * read (part of) a message.
  1086. */
  1087. static int read_partial_message(struct ceph_connection *con)
  1088. {
  1089. struct ceph_msg *m = con->in_msg;
  1090. void *p;
  1091. int ret;
  1092. int to, want, left;
  1093. unsigned front_len, middle_len, data_len, data_off;
  1094. int datacrc = con->msgr->nocrc;
  1095. dout("read_partial_message con %p msg %p\n", con, m);
  1096. /* header */
  1097. while (con->in_base_pos < sizeof(con->in_hdr)) {
  1098. left = sizeof(con->in_hdr) - con->in_base_pos;
  1099. ret = ceph_tcp_recvmsg(con->sock,
  1100. (char *)&con->in_hdr + con->in_base_pos,
  1101. left);
  1102. if (ret <= 0)
  1103. return ret;
  1104. con->in_base_pos += ret;
  1105. if (con->in_base_pos == sizeof(con->in_hdr)) {
  1106. u32 crc = crc32c(0, (void *)&con->in_hdr,
  1107. sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
  1108. if (crc != le32_to_cpu(con->in_hdr.crc)) {
  1109. pr_err("read_partial_message bad hdr "
  1110. " crc %u != expected %u\n",
  1111. crc, con->in_hdr.crc);
  1112. return -EBADMSG;
  1113. }
  1114. }
  1115. }
  1116. front_len = le32_to_cpu(con->in_hdr.front_len);
  1117. if (front_len > CEPH_MSG_MAX_FRONT_LEN)
  1118. return -EIO;
  1119. middle_len = le32_to_cpu(con->in_hdr.middle_len);
  1120. if (middle_len > CEPH_MSG_MAX_DATA_LEN)
  1121. return -EIO;
  1122. data_len = le32_to_cpu(con->in_hdr.data_len);
  1123. if (data_len > CEPH_MSG_MAX_DATA_LEN)
  1124. return -EIO;
  1125. /* allocate message? */
  1126. if (!con->in_msg) {
  1127. dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
  1128. con->in_hdr.front_len, con->in_hdr.data_len);
  1129. con->in_msg = con->ops->alloc_msg(con, &con->in_hdr);
  1130. if (!con->in_msg) {
  1131. /* skip this message */
  1132. pr_err("alloc_msg returned NULL, skipping message\n");
  1133. con->in_base_pos = -front_len - middle_len - data_len -
  1134. sizeof(m->footer);
  1135. con->in_tag = CEPH_MSGR_TAG_READY;
  1136. return 0;
  1137. }
  1138. if (IS_ERR(con->in_msg)) {
  1139. ret = PTR_ERR(con->in_msg);
  1140. con->in_msg = NULL;
  1141. con->error_msg = "out of memory for incoming message";
  1142. return ret;
  1143. }
  1144. m = con->in_msg;
  1145. m->front.iov_len = 0; /* haven't read it yet */
  1146. memcpy(&m->hdr, &con->in_hdr, sizeof(con->in_hdr));
  1147. }
  1148. /* front */
  1149. while (m->front.iov_len < front_len) {
  1150. BUG_ON(m->front.iov_base == NULL);
  1151. left = front_len - m->front.iov_len;
  1152. ret = ceph_tcp_recvmsg(con->sock, (char *)m->front.iov_base +
  1153. m->front.iov_len, left);
  1154. if (ret <= 0)
  1155. return ret;
  1156. m->front.iov_len += ret;
  1157. if (m->front.iov_len == front_len)
  1158. con->in_front_crc = crc32c(0, m->front.iov_base,
  1159. m->front.iov_len);
  1160. }
  1161. /* middle */
  1162. while (middle_len > 0 && (!m->middle ||
  1163. m->middle->vec.iov_len < middle_len)) {
  1164. if (m->middle == NULL) {
  1165. ret = -EOPNOTSUPP;
  1166. if (con->ops->alloc_middle)
  1167. ret = con->ops->alloc_middle(con, m);
  1168. if (ret < 0) {
  1169. pr_err("alloc_middle fail skipping payload\n");
  1170. con->in_base_pos = -middle_len - data_len
  1171. - sizeof(m->footer);
  1172. ceph_msg_put(con->in_msg);
  1173. con->in_msg = NULL;
  1174. con->in_tag = CEPH_MSGR_TAG_READY;
  1175. return 0;
  1176. }
  1177. m->middle->vec.iov_len = 0;
  1178. }
  1179. left = middle_len - m->middle->vec.iov_len;
  1180. ret = ceph_tcp_recvmsg(con->sock,
  1181. (char *)m->middle->vec.iov_base +
  1182. m->middle->vec.iov_len, left);
  1183. if (ret <= 0)
  1184. return ret;
  1185. m->middle->vec.iov_len += ret;
  1186. if (m->middle->vec.iov_len == middle_len)
  1187. con->in_middle_crc = crc32c(0, m->middle->vec.iov_base,
  1188. m->middle->vec.iov_len);
  1189. }
  1190. /* (page) data */
  1191. data_off = le16_to_cpu(m->hdr.data_off);
  1192. if (data_len == 0)
  1193. goto no_data;
  1194. if (m->nr_pages == 0) {
  1195. con->in_msg_pos.page = 0;
  1196. con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
  1197. con->in_msg_pos.data_pos = 0;
  1198. /* find pages for data payload */
  1199. want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
  1200. ret = -1;
  1201. mutex_unlock(&con->mutex);
  1202. if (con->ops->prepare_pages)
  1203. ret = con->ops->prepare_pages(con, m, want);
  1204. mutex_lock(&con->mutex);
  1205. if (ret < 0) {
  1206. dout("%p prepare_pages failed, skipping payload\n", m);
  1207. con->in_base_pos = -data_len - sizeof(m->footer);
  1208. ceph_msg_put(con->in_msg);
  1209. con->in_msg = NULL;
  1210. con->in_tag = CEPH_MSGR_TAG_READY;
  1211. return 0;
  1212. }
  1213. BUG_ON(m->nr_pages < want);
  1214. }
  1215. while (con->in_msg_pos.data_pos < data_len) {
  1216. left = min((int)(data_len - con->in_msg_pos.data_pos),
  1217. (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
  1218. BUG_ON(m->pages == NULL);
  1219. p = kmap(m->pages[con->in_msg_pos.page]);
  1220. ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
  1221. left);
  1222. if (ret > 0 && datacrc)
  1223. con->in_data_crc =
  1224. crc32c(con->in_data_crc,
  1225. p + con->in_msg_pos.page_pos, ret);
  1226. kunmap(m->pages[con->in_msg_pos.page]);
  1227. if (ret <= 0)
  1228. return ret;
  1229. con->in_msg_pos.data_pos += ret;
  1230. con->in_msg_pos.page_pos += ret;
  1231. if (con->in_msg_pos.page_pos == PAGE_SIZE) {
  1232. con->in_msg_pos.page_pos = 0;
  1233. con->in_msg_pos.page++;
  1234. }
  1235. }
  1236. no_data:
  1237. /* footer */
  1238. to = sizeof(m->hdr) + sizeof(m->footer);
  1239. while (con->in_base_pos < to) {
  1240. left = to - con->in_base_pos;
  1241. ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
  1242. (con->in_base_pos - sizeof(m->hdr)),
  1243. left);
  1244. if (ret <= 0)
  1245. return ret;
  1246. con->in_base_pos += ret;
  1247. }
  1248. dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
  1249. m, front_len, m->footer.front_crc, middle_len,
  1250. m->footer.middle_crc, data_len, m->footer.data_crc);
  1251. /* crc ok? */
  1252. if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
  1253. pr_err("read_partial_message %p front crc %u != exp. %u\n",
  1254. m, con->in_front_crc, m->footer.front_crc);
  1255. return -EBADMSG;
  1256. }
  1257. if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
  1258. pr_err("read_partial_message %p middle crc %u != exp %u\n",
  1259. m, con->in_middle_crc, m->footer.middle_crc);
  1260. return -EBADMSG;
  1261. }
  1262. if (datacrc &&
  1263. (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
  1264. con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
  1265. pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
  1266. con->in_data_crc, le32_to_cpu(m->footer.data_crc));
  1267. return -EBADMSG;
  1268. }
  1269. return 1; /* done! */
  1270. }
  1271. /*
  1272. * Process message. This happens in the worker thread. The callback should
  1273. * be careful not to do anything that waits on other incoming messages or it
  1274. * may deadlock.
  1275. */
  1276. static void process_message(struct ceph_connection *con)
  1277. {
  1278. struct ceph_msg *msg;
  1279. msg = con->in_msg;
  1280. con->in_msg = NULL;
  1281. /* if first message, set peer_name */
  1282. if (con->peer_name.type == 0)
  1283. con->peer_name = msg->hdr.src.name;
  1284. con->in_seq++;
  1285. mutex_unlock(&con->mutex);
  1286. dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
  1287. msg, le64_to_cpu(msg->hdr.seq),
  1288. ENTITY_NAME(msg->hdr.src.name),
  1289. le16_to_cpu(msg->hdr.type),
  1290. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1291. le32_to_cpu(msg->hdr.front_len),
  1292. le32_to_cpu(msg->hdr.data_len),
  1293. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  1294. con->ops->dispatch(con, msg);
  1295. mutex_lock(&con->mutex);
  1296. prepare_read_tag(con);
  1297. }
  1298. /*
  1299. * Write something to the socket. Called in a worker thread when the
  1300. * socket appears to be writeable and we have something ready to send.
  1301. */
  1302. static int try_write(struct ceph_connection *con)
  1303. {
  1304. struct ceph_messenger *msgr = con->msgr;
  1305. int ret = 1;
  1306. dout("try_write start %p state %lu nref %d\n", con, con->state,
  1307. atomic_read(&con->nref));
  1308. mutex_lock(&con->mutex);
  1309. more:
  1310. dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
  1311. /* open the socket first? */
  1312. if (con->sock == NULL) {
  1313. /*
  1314. * if we were STANDBY and are reconnecting _this_
  1315. * connection, bump connect_seq now. Always bump
  1316. * global_seq.
  1317. */
  1318. if (test_and_clear_bit(STANDBY, &con->state))
  1319. con->connect_seq++;
  1320. prepare_write_banner(msgr, con);
  1321. prepare_write_connect(msgr, con, 1);
  1322. prepare_read_banner(con);
  1323. set_bit(CONNECTING, &con->state);
  1324. clear_bit(NEGOTIATING, &con->state);
  1325. BUG_ON(con->in_msg);
  1326. con->in_tag = CEPH_MSGR_TAG_READY;
  1327. dout("try_write initiating connect on %p new state %lu\n",
  1328. con, con->state);
  1329. con->sock = ceph_tcp_connect(con);
  1330. if (IS_ERR(con->sock)) {
  1331. con->sock = NULL;
  1332. con->error_msg = "connect error";
  1333. ret = -1;
  1334. goto out;
  1335. }
  1336. }
  1337. more_kvec:
  1338. /* kvec data queued? */
  1339. if (con->out_skip) {
  1340. ret = write_partial_skip(con);
  1341. if (ret <= 0)
  1342. goto done;
  1343. if (ret < 0) {
  1344. dout("try_write write_partial_skip err %d\n", ret);
  1345. goto done;
  1346. }
  1347. }
  1348. if (con->out_kvec_left) {
  1349. ret = write_partial_kvec(con);
  1350. if (ret <= 0)
  1351. goto done;
  1352. }
  1353. /* msg pages? */
  1354. if (con->out_msg) {
  1355. if (con->out_msg_done) {
  1356. ceph_msg_put(con->out_msg);
  1357. con->out_msg = NULL; /* we're done with this one */
  1358. goto do_next;
  1359. }
  1360. ret = write_partial_msg_pages(con);
  1361. if (ret == 1)
  1362. goto more_kvec; /* we need to send the footer, too! */
  1363. if (ret == 0)
  1364. goto done;
  1365. if (ret < 0) {
  1366. dout("try_write write_partial_msg_pages err %d\n",
  1367. ret);
  1368. goto done;
  1369. }
  1370. }
  1371. do_next:
  1372. if (!test_bit(CONNECTING, &con->state)) {
  1373. /* is anything else pending? */
  1374. if (!list_empty(&con->out_queue)) {
  1375. prepare_write_message(con);
  1376. goto more;
  1377. }
  1378. if (con->in_seq > con->in_seq_acked) {
  1379. prepare_write_ack(con);
  1380. goto more;
  1381. }
  1382. if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
  1383. prepare_write_keepalive(con);
  1384. goto more;
  1385. }
  1386. }
  1387. /* Nothing to do! */
  1388. clear_bit(WRITE_PENDING, &con->state);
  1389. dout("try_write nothing else to write.\n");
  1390. done:
  1391. ret = 0;
  1392. out:
  1393. mutex_unlock(&con->mutex);
  1394. dout("try_write done on %p\n", con);
  1395. return ret;
  1396. }
  1397. /*
  1398. * Read what we can from the socket.
  1399. */
  1400. static int try_read(struct ceph_connection *con)
  1401. {
  1402. struct ceph_messenger *msgr;
  1403. int ret = -1;
  1404. if (!con->sock)
  1405. return 0;
  1406. if (test_bit(STANDBY, &con->state))
  1407. return 0;
  1408. dout("try_read start on %p\n", con);
  1409. msgr = con->msgr;
  1410. mutex_lock(&con->mutex);
  1411. more:
  1412. dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
  1413. con->in_base_pos);
  1414. if (test_bit(CONNECTING, &con->state)) {
  1415. if (!test_bit(NEGOTIATING, &con->state)) {
  1416. dout("try_read connecting\n");
  1417. ret = read_partial_banner(con);
  1418. if (ret <= 0)
  1419. goto done;
  1420. if (process_banner(con) < 0) {
  1421. ret = -1;
  1422. goto out;
  1423. }
  1424. }
  1425. ret = read_partial_connect(con);
  1426. if (ret <= 0)
  1427. goto done;
  1428. if (process_connect(con) < 0) {
  1429. ret = -1;
  1430. goto out;
  1431. }
  1432. goto more;
  1433. }
  1434. if (con->in_base_pos < 0) {
  1435. /*
  1436. * skipping + discarding content.
  1437. *
  1438. * FIXME: there must be a better way to do this!
  1439. */
  1440. static char buf[1024];
  1441. int skip = min(1024, -con->in_base_pos);
  1442. dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
  1443. ret = ceph_tcp_recvmsg(con->sock, buf, skip);
  1444. if (ret <= 0)
  1445. goto done;
  1446. con->in_base_pos += ret;
  1447. if (con->in_base_pos)
  1448. goto more;
  1449. }
  1450. if (con->in_tag == CEPH_MSGR_TAG_READY) {
  1451. /*
  1452. * what's next?
  1453. */
  1454. ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
  1455. if (ret <= 0)
  1456. goto done;
  1457. dout("try_read got tag %d\n", (int)con->in_tag);
  1458. switch (con->in_tag) {
  1459. case CEPH_MSGR_TAG_MSG:
  1460. prepare_read_message(con);
  1461. break;
  1462. case CEPH_MSGR_TAG_ACK:
  1463. prepare_read_ack(con);
  1464. break;
  1465. case CEPH_MSGR_TAG_CLOSE:
  1466. set_bit(CLOSED, &con->state); /* fixme */
  1467. goto done;
  1468. default:
  1469. goto bad_tag;
  1470. }
  1471. }
  1472. if (con->in_tag == CEPH_MSGR_TAG_MSG) {
  1473. ret = read_partial_message(con);
  1474. if (ret <= 0) {
  1475. switch (ret) {
  1476. case -EBADMSG:
  1477. con->error_msg = "bad crc";
  1478. ret = -EIO;
  1479. goto out;
  1480. case -EIO:
  1481. con->error_msg = "io error";
  1482. goto out;
  1483. default:
  1484. goto done;
  1485. }
  1486. }
  1487. if (con->in_tag == CEPH_MSGR_TAG_READY)
  1488. goto more;
  1489. process_message(con);
  1490. goto more;
  1491. }
  1492. if (con->in_tag == CEPH_MSGR_TAG_ACK) {
  1493. ret = read_partial_ack(con);
  1494. if (ret <= 0)
  1495. goto done;
  1496. process_ack(con);
  1497. goto more;
  1498. }
  1499. done:
  1500. ret = 0;
  1501. out:
  1502. mutex_unlock(&con->mutex);
  1503. dout("try_read done on %p\n", con);
  1504. return ret;
  1505. bad_tag:
  1506. pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
  1507. con->error_msg = "protocol error, garbage tag";
  1508. ret = -1;
  1509. goto out;
  1510. }
  1511. /*
  1512. * Atomically queue work on a connection. Bump @con reference to
  1513. * avoid races with connection teardown.
  1514. *
  1515. * There is some trickery going on with QUEUED and BUSY because we
  1516. * only want a _single_ thread operating on each connection at any
  1517. * point in time, but we want to use all available CPUs.
  1518. *
  1519. * The worker thread only proceeds if it can atomically set BUSY. It
  1520. * clears QUEUED and does it's thing. When it thinks it's done, it
  1521. * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
  1522. * (tries again to set BUSY).
  1523. *
  1524. * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
  1525. * try to queue work. If that fails (work is already queued, or BUSY)
  1526. * we give up (work also already being done or is queued) but leave QUEUED
  1527. * set so that the worker thread will loop if necessary.
  1528. */
  1529. static void queue_con(struct ceph_connection *con)
  1530. {
  1531. if (test_bit(DEAD, &con->state)) {
  1532. dout("queue_con %p ignoring: DEAD\n",
  1533. con);
  1534. return;
  1535. }
  1536. if (!con->ops->get(con)) {
  1537. dout("queue_con %p ref count 0\n", con);
  1538. return;
  1539. }
  1540. set_bit(QUEUED, &con->state);
  1541. if (test_bit(BUSY, &con->state)) {
  1542. dout("queue_con %p - already BUSY\n", con);
  1543. con->ops->put(con);
  1544. } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
  1545. dout("queue_con %p - already queued\n", con);
  1546. con->ops->put(con);
  1547. } else {
  1548. dout("queue_con %p\n", con);
  1549. }
  1550. }
  1551. /*
  1552. * Do some work on a connection. Drop a connection ref when we're done.
  1553. */
  1554. static void con_work(struct work_struct *work)
  1555. {
  1556. struct ceph_connection *con = container_of(work, struct ceph_connection,
  1557. work.work);
  1558. int backoff = 0;
  1559. more:
  1560. if (test_and_set_bit(BUSY, &con->state) != 0) {
  1561. dout("con_work %p BUSY already set\n", con);
  1562. goto out;
  1563. }
  1564. dout("con_work %p start, clearing QUEUED\n", con);
  1565. clear_bit(QUEUED, &con->state);
  1566. if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
  1567. dout("con_work CLOSED\n");
  1568. con_close_socket(con);
  1569. goto done;
  1570. }
  1571. if (test_and_clear_bit(OPENING, &con->state)) {
  1572. /* reopen w/ new peer */
  1573. dout("con_work OPENING\n");
  1574. con_close_socket(con);
  1575. }
  1576. if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
  1577. try_read(con) < 0 ||
  1578. try_write(con) < 0) {
  1579. backoff = 1;
  1580. ceph_fault(con); /* error/fault path */
  1581. }
  1582. done:
  1583. clear_bit(BUSY, &con->state);
  1584. dout("con->state=%lu\n", con->state);
  1585. if (test_bit(QUEUED, &con->state)) {
  1586. if (!backoff) {
  1587. dout("con_work %p QUEUED reset, looping\n", con);
  1588. goto more;
  1589. }
  1590. dout("con_work %p QUEUED reset, but just faulted\n", con);
  1591. clear_bit(QUEUED, &con->state);
  1592. }
  1593. dout("con_work %p done\n", con);
  1594. out:
  1595. con->ops->put(con);
  1596. }
  1597. /*
  1598. * Generic error/fault handler. A retry mechanism is used with
  1599. * exponential backoff
  1600. */
  1601. static void ceph_fault(struct ceph_connection *con)
  1602. {
  1603. pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
  1604. pr_addr(&con->peer_addr.in_addr), con->error_msg);
  1605. dout("fault %p state %lu to peer %s\n",
  1606. con, con->state, pr_addr(&con->peer_addr.in_addr));
  1607. if (test_bit(LOSSYTX, &con->state)) {
  1608. dout("fault on LOSSYTX channel\n");
  1609. goto out;
  1610. }
  1611. clear_bit(BUSY, &con->state); /* to avoid an improbable race */
  1612. mutex_lock(&con->mutex);
  1613. con_close_socket(con);
  1614. if (con->in_msg) {
  1615. ceph_msg_put(con->in_msg);
  1616. con->in_msg = NULL;
  1617. }
  1618. /* If there are no messages in the queue, place the connection
  1619. * in a STANDBY state (i.e., don't try to reconnect just yet). */
  1620. if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
  1621. dout("fault setting STANDBY\n");
  1622. set_bit(STANDBY, &con->state);
  1623. mutex_unlock(&con->mutex);
  1624. goto out;
  1625. }
  1626. /* Requeue anything that hasn't been acked, and retry after a
  1627. * delay. */
  1628. list_splice_init(&con->out_sent, &con->out_queue);
  1629. if (con->delay == 0)
  1630. con->delay = BASE_DELAY_INTERVAL;
  1631. else if (con->delay < MAX_DELAY_INTERVAL)
  1632. con->delay *= 2;
  1633. mutex_unlock(&con->mutex);
  1634. /* explicitly schedule work to try to reconnect again later. */
  1635. dout("fault queueing %p delay %lu\n", con, con->delay);
  1636. con->ops->get(con);
  1637. if (queue_delayed_work(ceph_msgr_wq, &con->work,
  1638. round_jiffies_relative(con->delay)) == 0)
  1639. con->ops->put(con);
  1640. out:
  1641. if (con->ops->fault)
  1642. con->ops->fault(con);
  1643. }
  1644. /*
  1645. * create a new messenger instance
  1646. */
  1647. struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
  1648. {
  1649. struct ceph_messenger *msgr;
  1650. msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
  1651. if (msgr == NULL)
  1652. return ERR_PTR(-ENOMEM);
  1653. spin_lock_init(&msgr->global_seq_lock);
  1654. /* the zero page is needed if a request is "canceled" while the message
  1655. * is being written over the socket */
  1656. msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1657. if (!msgr->zero_page) {
  1658. kfree(msgr);
  1659. return ERR_PTR(-ENOMEM);
  1660. }
  1661. kmap(msgr->zero_page);
  1662. if (myaddr)
  1663. msgr->inst.addr = *myaddr;
  1664. /* select a random nonce */
  1665. get_random_bytes(&msgr->inst.addr.nonce,
  1666. sizeof(msgr->inst.addr.nonce));
  1667. encode_my_addr(msgr);
  1668. dout("messenger_create %p\n", msgr);
  1669. return msgr;
  1670. }
  1671. void ceph_messenger_destroy(struct ceph_messenger *msgr)
  1672. {
  1673. dout("destroy %p\n", msgr);
  1674. kunmap(msgr->zero_page);
  1675. __free_page(msgr->zero_page);
  1676. kfree(msgr);
  1677. dout("destroyed messenger %p\n", msgr);
  1678. }
  1679. /*
  1680. * Queue up an outgoing message on the given connection.
  1681. */
  1682. void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
  1683. {
  1684. if (test_bit(CLOSED, &con->state)) {
  1685. dout("con_send %p closed, dropping %p\n", con, msg);
  1686. ceph_msg_put(msg);
  1687. return;
  1688. }
  1689. /* set src+dst */
  1690. msg->hdr.src.name = con->msgr->inst.name;
  1691. msg->hdr.src.addr = con->msgr->my_enc_addr;
  1692. msg->hdr.orig_src = msg->hdr.src;
  1693. msg->hdr.dst_erank = con->peer_addr.erank;
  1694. /* queue */
  1695. mutex_lock(&con->mutex);
  1696. BUG_ON(!list_empty(&msg->list_head));
  1697. list_add_tail(&msg->list_head, &con->out_queue);
  1698. dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
  1699. ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
  1700. ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
  1701. le32_to_cpu(msg->hdr.front_len),
  1702. le32_to_cpu(msg->hdr.middle_len),
  1703. le32_to_cpu(msg->hdr.data_len));
  1704. mutex_unlock(&con->mutex);
  1705. /* if there wasn't anything waiting to send before, queue
  1706. * new work */
  1707. if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
  1708. queue_con(con);
  1709. }
  1710. /*
  1711. * Revoke a message that was previously queued for send
  1712. */
  1713. void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
  1714. {
  1715. mutex_lock(&con->mutex);
  1716. if (!list_empty(&msg->list_head)) {
  1717. dout("con_revoke %p msg %p\n", con, msg);
  1718. list_del_init(&msg->list_head);
  1719. ceph_msg_put(msg);
  1720. msg->hdr.seq = 0;
  1721. if (con->out_msg == msg) {
  1722. ceph_msg_put(con->out_msg);
  1723. con->out_msg = NULL;
  1724. }
  1725. if (con->out_kvec_is_msg) {
  1726. con->out_skip = con->out_kvec_bytes;
  1727. con->out_kvec_is_msg = false;
  1728. }
  1729. } else {
  1730. dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
  1731. }
  1732. mutex_unlock(&con->mutex);
  1733. }
  1734. /*
  1735. * Revoke a page vector that we may be reading data into
  1736. */
  1737. void ceph_con_revoke_pages(struct ceph_connection *con, struct page **pages)
  1738. {
  1739. mutex_lock(&con->mutex);
  1740. if (con->in_msg && con->in_msg->pages == pages) {
  1741. unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
  1742. /* skip rest of message */
  1743. dout("con_revoke_pages %p msg %p pages %p revoked\n", con,
  1744. con->in_msg, pages);
  1745. if (con->in_msg_pos.data_pos < data_len)
  1746. con->in_base_pos = con->in_msg_pos.data_pos - data_len;
  1747. else
  1748. con->in_base_pos = con->in_base_pos -
  1749. sizeof(struct ceph_msg_header) -
  1750. sizeof(struct ceph_msg_footer);
  1751. con->in_msg->pages = NULL;
  1752. ceph_msg_put(con->in_msg);
  1753. con->in_msg = NULL;
  1754. con->in_tag = CEPH_MSGR_TAG_READY;
  1755. } else {
  1756. dout("con_revoke_pages %p msg %p pages %p no-op\n",
  1757. con, con->in_msg, pages);
  1758. }
  1759. mutex_unlock(&con->mutex);
  1760. }
  1761. /*
  1762. * Queue a keepalive byte to ensure the tcp connection is alive.
  1763. */
  1764. void ceph_con_keepalive(struct ceph_connection *con)
  1765. {
  1766. if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
  1767. test_and_set_bit(WRITE_PENDING, &con->state) == 0)
  1768. queue_con(con);
  1769. }
  1770. /*
  1771. * construct a new message with given type, size
  1772. * the new msg has a ref count of 1.
  1773. */
  1774. struct ceph_msg *ceph_msg_new(int type, int front_len,
  1775. int page_len, int page_off, struct page **pages)
  1776. {
  1777. struct ceph_msg *m;
  1778. m = kmalloc(sizeof(*m), GFP_NOFS);
  1779. if (m == NULL)
  1780. goto out;
  1781. kref_init(&m->kref);
  1782. INIT_LIST_HEAD(&m->list_head);
  1783. m->hdr.type = cpu_to_le16(type);
  1784. m->hdr.front_len = cpu_to_le32(front_len);
  1785. m->hdr.middle_len = 0;
  1786. m->hdr.data_len = cpu_to_le32(page_len);
  1787. m->hdr.data_off = cpu_to_le16(page_off);
  1788. m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
  1789. m->footer.front_crc = 0;
  1790. m->footer.middle_crc = 0;
  1791. m->footer.data_crc = 0;
  1792. m->front_max = front_len;
  1793. m->front_is_vmalloc = false;
  1794. m->more_to_follow = false;
  1795. m->pool = NULL;
  1796. /* front */
  1797. if (front_len) {
  1798. if (front_len > PAGE_CACHE_SIZE) {
  1799. m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
  1800. PAGE_KERNEL);
  1801. m->front_is_vmalloc = true;
  1802. } else {
  1803. m->front.iov_base = kmalloc(front_len, GFP_NOFS);
  1804. }
  1805. if (m->front.iov_base == NULL) {
  1806. pr_err("msg_new can't allocate %d bytes\n",
  1807. front_len);
  1808. goto out2;
  1809. }
  1810. } else {
  1811. m->front.iov_base = NULL;
  1812. }
  1813. m->front.iov_len = front_len;
  1814. /* middle */
  1815. m->middle = NULL;
  1816. /* data */
  1817. m->nr_pages = calc_pages_for(page_off, page_len);
  1818. m->pages = pages;
  1819. dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len,
  1820. m->nr_pages);
  1821. return m;
  1822. out2:
  1823. ceph_msg_put(m);
  1824. out:
  1825. pr_err("msg_new can't create type %d len %d\n", type, front_len);
  1826. return ERR_PTR(-ENOMEM);
  1827. }
  1828. /*
  1829. * Generic message allocator, for incoming messages.
  1830. */
  1831. struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
  1832. struct ceph_msg_header *hdr)
  1833. {
  1834. int type = le16_to_cpu(hdr->type);
  1835. int front_len = le32_to_cpu(hdr->front_len);
  1836. struct ceph_msg *msg = ceph_msg_new(type, front_len, 0, 0, NULL);
  1837. if (!msg) {
  1838. pr_err("unable to allocate msg type %d len %d\n",
  1839. type, front_len);
  1840. return ERR_PTR(-ENOMEM);
  1841. }
  1842. return msg;
  1843. }
  1844. /*
  1845. * Allocate "middle" portion of a message, if it is needed and wasn't
  1846. * allocated by alloc_msg. This allows us to read a small fixed-size
  1847. * per-type header in the front and then gracefully fail (i.e.,
  1848. * propagate the error to the caller based on info in the front) when
  1849. * the middle is too large.
  1850. */
  1851. int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
  1852. {
  1853. int type = le16_to_cpu(msg->hdr.type);
  1854. int middle_len = le32_to_cpu(msg->hdr.middle_len);
  1855. dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
  1856. ceph_msg_type_name(type), middle_len);
  1857. BUG_ON(!middle_len);
  1858. BUG_ON(msg->middle);
  1859. msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
  1860. if (!msg->middle)
  1861. return -ENOMEM;
  1862. return 0;
  1863. }
  1864. /*
  1865. * Free a generically kmalloc'd message.
  1866. */
  1867. void ceph_msg_kfree(struct ceph_msg *m)
  1868. {
  1869. dout("msg_kfree %p\n", m);
  1870. if (m->front_is_vmalloc)
  1871. vfree(m->front.iov_base);
  1872. else
  1873. kfree(m->front.iov_base);
  1874. kfree(m);
  1875. }
  1876. /*
  1877. * Drop a msg ref. Destroy as needed.
  1878. */
  1879. void ceph_msg_last_put(struct kref *kref)
  1880. {
  1881. struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
  1882. dout("ceph_msg_put last one on %p\n", m);
  1883. WARN_ON(!list_empty(&m->list_head));
  1884. /* drop middle, data, if any */
  1885. if (m->middle) {
  1886. ceph_buffer_put(m->middle);
  1887. m->middle = NULL;
  1888. }
  1889. m->nr_pages = 0;
  1890. m->pages = NULL;
  1891. if (m->pool)
  1892. ceph_msgpool_put(m->pool, m);
  1893. else
  1894. ceph_msg_kfree(m);
  1895. }
  1896. void ceph_msg_dump(struct ceph_msg *msg)
  1897. {
  1898. pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
  1899. msg->front_max, msg->nr_pages);
  1900. print_hex_dump(KERN_DEBUG, "header: ",
  1901. DUMP_PREFIX_OFFSET, 16, 1,
  1902. &msg->hdr, sizeof(msg->hdr), true);
  1903. print_hex_dump(KERN_DEBUG, " front: ",
  1904. DUMP_PREFIX_OFFSET, 16, 1,
  1905. msg->front.iov_base, msg->front.iov_len, true);
  1906. if (msg->middle)
  1907. print_hex_dump(KERN_DEBUG, "middle: ",
  1908. DUMP_PREFIX_OFFSET, 16, 1,
  1909. msg->middle->vec.iov_base,
  1910. msg->middle->vec.iov_len, true);
  1911. print_hex_dump(KERN_DEBUG, "footer: ",
  1912. DUMP_PREFIX_OFFSET, 16, 1,
  1913. &msg->footer, sizeof(msg->footer), true);
  1914. }