messenger.c 53 KB

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