messenger.c 53 KB

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