messenger.c 56 KB

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