messenger.c 53 KB

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