messenger.c 51 KB

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