messenger.c 56 KB

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