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