messenger.c 57 KB

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