messenger.c 57 KB

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