messenger.c 57 KB

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